Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

slackUserIdsFromCommitters() returns empty list for pipeline projects #930

Open
enaess opened this issue Oct 27, 2023 · 4 comments
Open
Labels

Comments

@enaess
Copy link

enaess commented Oct 27, 2023

Jenkins and plugins versions report

Environment
Jenkins: 2.414.3
OS: Linux - 5.15.0-87-generic
Java: 11.0.21 - Amazon.com Inc. (OpenJDK 64-Bit Server VM)
---
Parameterized-Remote-Trigger:3.2.0
active-directory:2.33
ant:497.v94e7d9fffa_b_9
antisamy-markup-formatter:162.v0e6ec0fcfcf6
apache-httpcomponents-client-4-api:4.5.14-208.v438351942757
apache-httpcomponents-client-5-api:5.2.1-1.1
artifactory:4.0.0
authentication-tokens:1.53.v1c90fd9191a_b_
azure-cli:0.9
azure-credentials:293.vb_d506148f506
azure-sdk:132.v62b_48eb_6f32f
blueocean-commons:1.27.8
blueocean-rest:1.27.8
bootstrap5-api:5.3.2-2
bouncycastle-api:2.29
branch-api:2.1128.v717130d4f816
build-timeout:1.31
caffeine-api:3.1.8-133.v17b_1ff2e0599
checks-api:2.0.2
cloud-stats:320.v96b_65297a_4b_b_
cloudbees-bitbucket-branch-source:848.v42c6a_317eda_e
cloudbees-folder:6.858.v898218f3609d
command-launcher:107.v773860566e2e
commons-lang3-api:3.13.0-62.v7d18e55f51e2
commons-text-api:1.10.0-78.v3e7b_ea_d5a_fe1
config-file-provider:959.vcff671a_4518b_
copyartifact:722.v0662a_9b_e22a_c
credentials:1293.vff276f713473
credentials-binding:636.v55f1275c7b_27
display-url-api:2.200.vb_9327d658781
docker-build-step:2.10
docker-commons:439.va_3cb_0a_6a_fb_29
docker-java-api:3.3.1-79.v20b_53427e041
docker-plugin:1.5
docker-workflow:572.v950f58993843
durable-task:523.va_a_22cf15d5e0
echarts-api:5.4.0-7
email-ext:2.102
favorite:2.4.3
font-awesome-api:6.4.2-1
git:5.2.0
git-client:4.5.0
gradle:2.8.2
handy-uri-templates-2-api:2.1.8-22.v77d5b_75e6953
htmlpublisher:1.32
http_request:1.18
instance-identity:173.va_37c494ec4e5
ionicons-api:56.v1b_1c8c49374e
jackson2-api:2.15.3-366.vfe8d1fa_f8c87
jakarta-activation-api:2.0.1-3
jakarta-mail-api:2.0.1-3
javadoc:243.vb_b_503b_b_45537
javax-activation-api:1.2.0-6
javax-mail-api:1.6.2-9
jaxb:2.3.9-1
jdk-tool:73.vddf737284550
jfrog:1.5.0
jjwt-api:0.11.5-77.v646c772fddb_0
jquery3-api:3.7.1-1
jsch:0.2.8-65.v052c39de79b_2
junit:1240.vf9529b_881428
ldap:701.vf8619de9160a_
mailer:463.vedf8358e006b_
matrix-auth:3.2.1
matrix-project:818.v7eb_e657db_924
maven-plugin:3.23
metrics:4.2.18-442.v02e107157925
mina-sshd-api-common:2.10.0-69.v28e3e36d18eb_
mina-sshd-api-core:2.10.0-69.v28e3e36d18eb_
okhttp-api:4.11.0-157.v6852a_a_fa_ec11
p4:1.14.3
pam-auth:1.10
parameterized-trigger:2.46
pipeline-build-step:505.v5f0844d8d126
pipeline-graph-analysis:202.va_d268e64deb_3
pipeline-graph-view:202.v6da_a_9e590325
pipeline-groovy-lib:689.veec561a_dee13
pipeline-input-step:477.v339683a_8d55e
pipeline-milestone-step:111.v449306f708b_7
pipeline-model-api:2.2144.v077a_d1928a_40
pipeline-model-definition:2.2144.v077a_d1928a_40
pipeline-model-extensions:2.2144.v077a_d1928a_40
pipeline-rest-api:2.33
pipeline-stage-step:305.ve96d0205c1c6
pipeline-stage-tags-metadata:2.2144.v077a_d1928a_40
pipeline-stage-view:2.33
pipeline-utility-steps:2.16.0
plain-credentials:143.v1b_df8b_d3b_e48
plugin-util-api:3.6.0
pubsub-light:1.17
resource-disposer:0.23
scm-api:676.v886669a_199a_a_
script-security:1275.v23895f409fb_d
slack:684.v833089650554
snakeyaml-api:2.2-111.vc6598e30cc65
sse-gateway:1.26
ssh-credentials:308.ve4497b_ccd8f4
ssh-slaves:2.916.vd17b_43357ce4
sshd:3.312.v1c601b_c83b_0e
structs:325.vcb_307d2a_2782
timestamper:1.26
token-macro:384.vf35b_f26814ec
trilead-api:2.84.v72119de229b_7
variant:60.v7290fc0eb_b_cd
windows-azure-storage:386.v673495b0a5de
workflow-aggregator:596.v8c21c963d92d
workflow-api:1283.v99c10937efcb_
workflow-basic-steps:1042.ve7b_140c4a_e0c
workflow-cps:3802.vd42b_fcf00b_a_c
workflow-durable-task-step:1289.v4d3e7b_01546b_
workflow-job:1348.v32a_a_f150910e
workflow-multibranch:756.v891d88f2cd46
workflow-scm-step:415.v434365564324
workflow-step-api:639.v6eca_cd8c04a_a_
workflow-support:865.v43e78cc44e0d
ws-cleanup:0.45

What Operating System are you using (both controller, and any agents involved in the problem)?

Ubuntu Linux 22.04 for all systems involved

Reproduction steps

  1. Create a Jenkins pipeline job
  2. Paste the pipeline script into the box
pipeline {
    agent {
        label 'java-builder'
    }
    parameters {
        string(name: 'change', defaultValue: 'now', description: 'Sync the build to a particular changelist number')
    }
    environment {
        P4_CRED = credentials('Perforce-Credentials')
    }
    options {
        skipDefaultCheckout()
        timeout(time: 20)
    }
    stages {
        stage('Checkout') {
            steps {
                checkout perforce(
                    credential: 'Perforce-Credentials',
                    populate: autoClean (
                        delete: true,
                        pin: change,
                        quiet: true,
                        replace: true,
                        tidy: true),
                    workspace: manualSpec(
                        name: 'jenkins-${NODE_NAME}-${JOB_NAME}-${EXECUTOR_NUMBER}',
                        spec: clientSpec(
                            view: '''
                                //Path/To/Your/Depot/... //${P4_CLIENT}/...
                                '''
                        )
                    )
                )
            }
        }
        stage('Build') {
            steps {
                script  {
                    def userIds = slackUserIdsFromCommitters(botUser:true, tokenCredentialId: 'Jenkins-Bot-Secret')
                    println userIds

                    def changeLogSets = currentBuild.changeSets
                    for (int i = 0; i < changeLogSets.size(); i++) {
                        def entries = changeLogSets[i].items
                        for (int j = 0; j < entries.length; j++) {
                            def entry = entries[j]
                            // hudson.model.User user = entry.author
                            def email2 = hudson.tasks.MailAddressResolver.resolve(entry.author)
                            
                            def resolver = new jenkins.plugins.slack.user.EmailSlackUserIdResolver()
                            resolver.setMailAddressResolvers(hudson.tasks.MailAddressResolver.all())
                            def email1 = resolver.resolveUserId(entry.author)
                            println "${entry.author.id} - ${email1} - ${email2}"
                        }
                    }
                }
            }
        }
    }
}

Expected Results

The results after calling slackUserIdsFromCommitters() should return a list, I shouldn't have to resolve the changeSets myself and call this.

Actual Results

slackUserIdsFromCommitters() seems to always return an empty list. No log messages are seen when I comment out the section from currentBuild.changeSets and down. However, I am getting a authorization token missing log-message if I enable the code. This indicates that the problem isn't in calling the functions that tries to reach out to the slack servers, but before that.

I suspect that the way I configured my project, as a pipeline project and that I skip the default checkout is causing this issue. The following code seems suspect to me SlackUserIdResolver.java:

 87     @SuppressWarnings("unchecked")
 88     public List<String> resolveUserIdsForRun(Run run) {
 89         if (run instanceof RunWithSCM) {
 90             RunWithSCM r = (RunWithSCM) run;
 91             return resolveUserIdsForChangeLogSets(r.getChangeSets());
 92         } else if (run instanceof AbstractBuild) {
 93             AbstractBuild build = (AbstractBuild) run;
 94             return resolveUserIdsForChangeLogSets(build.getChangeSets());
 95         } else {
 96             return Collections.emptyList();
 97         }
 98     }

I know for a fact that the changeSets isn't empty when I trigger a build where I'd expect there to be a list of changes here. Not entirely sure how I can make this work.

Anything else?

No response

@enaess enaess added the bug label Oct 27, 2023
@enaess
Copy link
Author

enaess commented Oct 27, 2023

With some more tweaks to the above code, I was able to create a EmailSlackUserIdResolver with appropriate HttpClient and Autorization tokens, the rest api to slack works and the user ids was resolved.

@spmiller
Copy link

Hi @enaess, we have the same problem on our Jenkins instance -- can you please post your working solution?

@enaess
Copy link
Author

enaess commented Jun 14, 2024

Try pasting in the following into your Jenkins pipeline script to see if fixes it

// Until slack-plugin fixes the slackUserIdsFromCommitters() function, see: https://github.com/jenkinsci/slack-plugin/issues/930
//    this does require a few approvals in the script sandbox.
@NonCPS
def getSlackUsers(changesets) {
    def resolver = new jenkins.plugins.slack.user.EmailSlackUserIdResolver(
        "${env.BOT_CRED_PSW}",       
        jenkins.plugins.slack.HttpClient.getCloseableHttpClient(null))
    resolver.setMailAddressResolvers(hudson.tasks.MailAddressResolver.all())
    return resolver.resolveUserIdsForChangeLogSets(currentBuild.getChangeSets()).toSet().collect { "<@$it>" }.join(', ')
}

Usage: in the failure block of your pipeline:

        failure {
            script {
                def userIdsString = getSlackUsers(currentBuild.changeSets)
                if (!userIdsString.isEmpty()) {
                    slackSend (color: '#FF0000', 
                               channel: '#your-slack-channel-here',
                               message: "FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (<${env.BUILD_URL}|Open>), Committers: $userIdsString")
                }
....

@spmiller
Copy link

Thank you @enaess!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants