Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-50194

Build userId missing for a job being built by other Jobs

    Details

    • Similar Issues:

      Description

      i have job1 with Jenkinsfile (example):

      #!groovy
      
      node('master') {
      try {
      stage('prepare') {
      }
      stage('check playbook') {
      }
      stage('deploy') {
      }
      stage('cleanup') {
      currentBuild.result = 'SUCCESS'
      deleteDir()
      build job: 'job2', wait: false
      }
      } catch(exc) {
      currentBuild.result = 'FAILURE'
      deleteDir()
      throw(exc)
      } finally {
      }
      }

      And job2 with Jenkinsfile:

      #!groovy
      
      node('master') {
      ...
      stage('jira update') {
      withEnv(['JIRA_SITE=LOCAL']) {
      version = [:]
      lastComment.eachMatch("(TEST)-[0-9]+") {
      a -> version[a[1]] = '';
      }
      version.each {
      projectKey, y -> println "project - $projectKey"
      def testVersion = [ name: nextTag, 
      released: false, 
      description: "${nextTag}", 
      project: projectKey ]
      def result = jiraNewVersion version: testVersion
      version[projectKey] = result.data
      }
      lastComment.eachMatch("(TEST)-[0-9]+") {
      a -> issueKey=a[0]; projectKey=a[1];
      
      try {
      def newVersion = version[projectKey]
      def testIssue = [fields: [fixVersions: [newVersion] ]]
      def editRes = jiraEditIssue idOrKey: issueKey, issue: testIssue
      println "version - $editRes"
      } catch(err) {
      echo "version error - $err"
      }
      
      try {
      def transitionInput = [transition: [id: 751]]
      def transRes = jiraTransitionIssue idOrKey: issueKey, input: transitionInput
      println "transition - $transRes"
      } catch(err) {
      echo "transition error - $err"
      }
      
      try {
      jiraAddComment idOrKey: issueKey, comment: "Success"
      } catch(err) {
      echo "comment error - $err"
      }
      }
      }
      }
      ...
      }

      If manually run job 2 (Build now) then in JIRA:

      Automatically created by: Matvey Kondratev from Build URL

      If manually run job1 then:

      Automatically created by: [~anonymous] from Build URL

      Maybe bug?

      /**
      * Return the current build user.
      *
      * @param causes build causes.
      * @return user name.
      */
      protected static String prepareBuildUser(List<Cause> causes) {
      String buildUser = "anonymous";
      if (causes != null && causes.size() > 0) {
      if (causes.get(0) instanceof UserIdCause) {
      buildUser = ((UserIdCause) causes.get(0)).getUserId();
      } else if (causes.get(0) instanceof UpstreamCause) {
      List<Cause> upstreamCauses = ((UpstreamCause) causes.get(0)).getUpstreamCauses();
      prepareBuildUser(upstreamCauses);
      }
      }
      return Util.fixEmpty(buildUser) == null ? "anonymous" : buildUser;
      }

      Jenkins log:

      INFO: While serving http://jenkins.local/job/ansible/job/job2/build: hudson.security.AccessDeniedException2: anonymous is missing the Job/Build permission

      JIRA log:

      2018-03-14 16:14:16,596 http-8080-11 WARN javabuild 974x2370843x1 zeo05a jenkins.local,127.0.0.1 /rest/api/2/issue/TEST-1 [labs.remoteapps.webhook.WebHookRegistrationManager] Failed to publish web-hooks for event com.atlassian.jira.event.issue.IssueEvent

      Jenkins: 2.73.1

      Jira Pipeline Steps: 1.3.1

      Jira: 5.1

        Attachments

        1. addCommentJob.png
          addCommentJob.png
          283 kB
        2. buildJob.png
          buildJob.png
          206 kB
        3. TEST1JIRA.png
          TEST1JIRA.png
          49 kB

          Activity

          Hide
          nrayapati Naresh Rayapati added a comment -

          PR: https://github.com/jenkinsci/jira-steps-plugin/pull/65

          Matvey Kondratev Thanks for reporting this, it is been a while that I wanted to fix this. 

          Show
          nrayapati Naresh Rayapati added a comment - PR: https://github.com/jenkinsci/jira-steps-plugin/pull/65 Matvey Kondratev Thanks for reporting this, it is been a while that I wanted to fix this. 
          Hide
          nrayapati Naresh Rayapati added a comment - - edited

          Merged above pull request, will release the artifact once after a bit of local testing. 

          Show
          nrayapati Naresh Rayapati added a comment - - edited Merged above pull request, will release the artifact once after a bit of local testing. 
          Hide
          nrayapati Naresh Rayapati added a comment -

          Released with 1.4.0, the plugin should be available soon for installation on update center. 

          Show
          nrayapati Naresh Rayapati added a comment - Released with 1.4.0, the plugin should be available soon for installation on update center. 
          Hide
          nrayapati Naresh Rayapati added a comment -
          • build job: 
            build 'jiraComment'
            
          • jiraAddComment job: 
            node {
             jiraAddComment comment: "testing", site: 'JIRA', idOrKey: 'TEST-1'
            }
            

          Attached screenshots for buildJob, jiraAttachment job and actual JIRA screenshot.

          Show
          nrayapati Naresh Rayapati added a comment - build job:  build 'jiraComment' jiraAddComment job:  node { jiraAddComment comment: "testing" , site: 'JIRA' , idOrKey: 'TEST-1' } Attached screenshots for buildJob, jiraAttachment job and actual JIRA screenshot.
          Hide
          mkondratev Matvey Kondratev added a comment - - edited

          On your log "started by user Jenkins". My build start by webhook from gitlab:

          Started by upstream project "job1" build number 126
          originally caused by:
           Started by remote host 192.168.0.10
          Obtained Jenkinsfile from git git@git.local:job2.git

          so, buildUser is null and the logic of this part of the code is correct.

          /**
          * Return the current build user.
          *
          * @param causes build causes.
          * @return user name.
          */
          protected static String prepareBuildUserId(List<Cause> causes) {
          String buildUser = "anonymous";
          if (causes != null && causes.size() > 0) {
          if (causes.get(0) instanceof UserIdCause) {
          buildUser = ((UserIdCause) causes.get(0)).getUserId();
          } else if (causes.get(0) instanceof UpstreamCause) {
          List<Cause> upstreamCauses = ((UpstreamCause) causes.get(0)).getUpstreamCauses();
          buildUser = prepareBuildUserId(upstreamCauses);
          }
          }
          return Util.fixEmpty(buildUser) == null ? "anonymous" : buildUser;
          }
          

          Maybe this will help: if started by remote host then buildUser = Jenkins.

          Show
          mkondratev Matvey Kondratev added a comment - - edited On your log "started by user Jenkins". My build start by webhook from gitlab: Started by upstream project "job1" build number 126 originally caused by: Started by remote host 192.168.0.10 Obtained Jenkinsfile from git git@git.local:job2.git so, buildUser is null and the logic of this part of the code is correct. /** * Return the current build user. * * @param causes build causes. * @ return user name. */ protected static String prepareBuildUserId(List<Cause> causes) { String buildUser = "anonymous" ; if (causes != null && causes.size() > 0) { if (causes.get(0) instanceof UserIdCause) { buildUser = ((UserIdCause) causes.get(0)).getUserId(); } else if (causes.get(0) instanceof UpstreamCause) { List<Cause> upstreamCauses = ((UpstreamCause) causes.get(0)).getUpstreamCauses(); buildUser = prepareBuildUserId(upstreamCauses); } } return Util.fixEmpty(buildUser) == null ? "anonymous" : buildUser; } Maybe this will help: if started by remote host then buildUser = Jenkins.
          Hide
          nrayapati Naresh Rayapati added a comment - - edited

          Matvey Kondratev I don't think defaulting it to Jenkins is any different from anonymous user, it would look like [/~Jenkins], not every JIRA instance would have Jenkins user being configured. how about using the user id from site?

          I am also planning to enhance it to tokenize this string for automated builds such that developers would be able to choose various environment variables based on their choice. such like CHANGE_USER, BUILD_USER, BUILD_ID and so on.

          Show
          nrayapati Naresh Rayapati added a comment - - edited Matvey Kondratev I don't think defaulting it to Jenkins is any different from anonymous user, it would look like [/~Jenkins] , not every JIRA instance would have Jenkins user being configured. how about using the user id from site? I am also planning to enhance it to tokenize this string for automated builds such that developers would be able to choose various environment variables based on their choice. such like CHANGE_USER, BUILD_USER, BUILD_ID and so on.
          Hide
          mkondratev Matvey Kondratev added a comment -

          Naresh Rayapati userid from jenkins users? 

          Show
          mkondratev Matvey Kondratev added a comment - Naresh Rayapati userid from jenkins users? 

            People

            • Assignee:
              nrayapati Naresh Rayapati
              Reporter:
              mkondratev Matvey Kondratev
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: