jenkinsci / bitbucket-branch-source-plugin

Bitbucket Branch Source Plugin
https://plugins.jenkins.io/cloudbees-bitbucket-branch-source
MIT License
216 stars 352 forks source link

Is there a way to retrieve payload user name at pipeline run? #413

Open csturiale opened 3 years ago

csturiale commented 3 years ago

Your checklist for this issue

Description

Is there a way to retrieve payload user name at pipeline run? At the moment the following variables are not set:

Any hint on how to retrieve the user who initiated the change?

Thanks

janisliepins commented 3 years ago

I am also interested in this - is there any reason why payload from Bitbucket Webhook is not exposed? E.g. like in this plugin: https://plugins.jenkins.io/bitbucket/

$BITBUCKET_PAYLOAD

gmshake commented 3 years ago

Is there a way to retrieve payload user name at pipeline run? At the moment the following variables are not set:

  • CHANGE_AUTHOR
  • CHANGE_ID
  • CHANGE_AUTHOR_DISPLAY_NAME
  • CHANGE_AUTHOR_EMAIL

Any hint on how to retrieve the user who initiated the change?

Hi, @csturiale, these variables are set in environment. Example pipeline:

#!/usr/bin/env groovy
pipeline {
    agent any
    stages {
        stage('Initialize') {
            steps {
                sh 'printenv'
            }
        }
    }
}
gmshake commented 3 years ago

I am also interested in this - is there any reason why payload from Bitbucket Webhook is not exposed? E.g. like in this plugin: https://plugins.jenkins.io/bitbucket/

$BITBUCKET_PAYLOAD

@janisliepins I'm not the creator of this plugin, it works great. I can not answer your question but I think it is a design choice. Do you have any use cases requiring the $BITBUCKET_PAYLOAD ?

janisliepins commented 3 years ago

I am also interested in this - is there any reason why payload from Bitbucket Webhook is not exposed? E.g. like in this plugin: https://plugins.jenkins.io/bitbucket/ $BITBUCKET_PAYLOAD

@janisliepins I'm not the creator of this plugin, it works great. I can not answer your question but I think it is a design choice. Do you have any use cases requiring the $BITBUCKET_PAYLOAD ?

Currently there is no way how Jenkins can distinguish between branch created/deleted and push event - they all classify as Branch Event when pipeline gets triggered. If we could get payload information then we could distinguish those events pretty easy + there is also other useful information about that event, different examples:

https://support.atlassian.com/bitbucket-cloud/docs/event-payloads/

csturiale commented 3 years ago
pipeline {
  agent any
  stages {
      stage('Initialize') {
          steps {
              sh 'printenv'
          }
      }
  }
}

Hi @gmshake thanks but in my case it's not working, I don't know if it's linked to the fact that I'm using jenkins kubernetes plugin in order to provision nodes. The following Jenkinsfile does not produce previous env variables:

podTemplate(showRawYaml: false, imagePullSecrets: ['mysecret'], containers: [
        containerTemplate(name: 'jnlp', image: "myregistry/jenkins/jnlp-slave:4.3-4-uid", args: '${computer.jnlpmac} ${computer.name}')
]) {
    node(POD_LABEL) {
        def e = checkout scm
        echo "${e}" // contains GIT_BRANCH, GIT_COMMIT, GIT_PREVIOUS_COMMIT, GIT_PREVIOUS_SUCCESSFUL_COMMIT, GIT_URL but not the Author
        sh 'printenv'
    }
}

So in my use cases there is also the need to retrieve user who pushed the code

bitwiseman commented 3 years ago

Is there a way to retrieve payload user name at pipeline run? At the moment the following variables are not set:

  • CHANGE_AUTHOR
  • CHANGE_ID
  • CHANGE_AUTHOR_DISPLAY_NAME
  • CHANGE_AUTHOR_EMAIL

@csturiale I think those values are only set for pull requests. But if you are not seeing them on PRs, then this is a bug.

csturiale commented 3 years ago

Thanks @bitwiseman ,now it is more clear, but my question is: how can I retrieve the actor ( user ) who pushed the code? The actor is sent by Bitbucket to Jenkins.

bitwiseman commented 3 years ago

@csturiale GIT_COMMITTER_NAME, GIT_AUTHOR_NAME, GIT_COMMITTER_EMAIL, GIT_AUTHOR_EMAIL

csturiale commented 3 years ago

@bitwiseman I'm getting only the followings: GIT_BRANCH:mybranch, GIT_COMMIT:xxxxxxxxxxxxx, GIT_PREVIOUS_COMMIT:xxxxxxxxxxxxx, GIT_URL:https://mybitbucketurl/scm/MYPROJ/myrepo.git

My pipeline contains the following as mentioned before:

podTemplate(showRawYaml: false, imagePullSecrets: ['mysecret'], containers: [
        containerTemplate(name: 'jnlp', image: "myregistry/jenkins/jnlp-slave:4.3-4-uid", args: '${computer.jnlpmac} ${computer.name}')
]) {
    node(POD_LABEL) {
        def e = checkout scm
        echo "${e}" // contains GIT_BRANCH, GIT_COMMIT, GIT_PREVIOUS_COMMIT, GIT_PREVIOUS_SUCCESSFUL_COMMIT, GIT_URL but not the Author
        sh 'printenv'
    }
}
yobushka commented 3 years ago

@bitwiseman I'm getting only the followings: GIT_BRANCH:mybranch, GIT_COMMIT:xxxxxxxxxxxxx, GIT_PREVIOUS_COMMIT:xxxxxxxxxxxxx, GIT_URL:https://mybitbucketurl/scm/MYPROJ/myrepo.git

Same in my case for bitbucket cloud.

janisliepins commented 3 years ago

@bitwiseman I'm getting only the followings: GIT_BRANCH:mybranch, GIT_COMMIT:xxxxxxxxxxxxx, GIT_PREVIOUS_COMMIT:xxxxxxxxxxxxx, GIT_URL:https://mybitbucketurl/scm/MYPROJ/myrepo.git

Same in my case for bitbucket cloud.

@yobushka @csturiale

You can get those from build revision object:

SCMRevisionAction action = build.getAction(SCMRevisionAction)

action.revision.author action.revision.message action.revision.hash

Still it would nice that actual payload from Bitbucket webhook would be exposed in pipeline ....

bitwiseman commented 3 years ago

@yobushka @janisliepins @csturiale Are you all using Pipeline Jobs or Multibranch Pipeline Jobs?

csturiale commented 3 years ago

@bitwiseman , I'm using Multibranch Pipeline

janisliepins commented 3 years ago

@yobushka @janisliepins @csturiale Are you all using Pipeline Jobs or Multibranch Pipeline Jobs?

Multibranch. But I think this plugin is only compatible with Multibranch pipelines only (including organization folders)

csturiale commented 3 years ago

SCMRevisionAction action = build.getAction(SCMRevisionAction)

action.revision.author action.revision.message action.revision.hash

Thanks @janisliepins , this result can also be achieved by running sh "git --no-pager show -s --format='%an'" or something like that.

Our need is to retrieve, from the pipeline, the authenticated username who pushed the code in Bitbucket . It means if locally I put the following git configuration:

[user]
     name = test  
     email = test  

I can retrieve csturiale ( this is present in the payload ) instead of test as username.

AvalakhGit commented 3 years ago

Get the same problem - can't get this variables:

Using: Jenkins - v. 2.63.3 (on win machine) Bitbucket branch source plugin - v. 2.9.7 Bitbucket server - 7.8.1 Webhook to Jenkins for Bitbucket - v. 5.8.0

Jenkinsfile contains: echo(env.getEnvironment().collect({environmentVariable -> "${environmentVariable.key} = ${environmentVariable.value}"}).join("\n"))

But the output is missing these variables. MOHAMI plugin support for bitbucket tells me:

"Unfortunately, I cannot provide a qualified answer here, since this functionality (setting changes metadata from Bitbucket) is implemented by BranchSource plugin - I would ask their support if it’s possible."

Without waiting for their answer, duplicate mine here.

robross0606 commented 3 years ago

I have a similar need but for a different use case. I need to get other details out of the payload, such as the "slug" name and project key so I can derive webhook calls back to Bitbucket for things like reporting test and lint violations. This information is all available in the incoming payload but not exposed by the plugin. Adding it as JSON in something like BITBUCKET_PAYLOAD would be ideal.

oleh-ozimok commented 3 years ago

Hi, you can use my fork for this: https://github.com/oleh-ozimok/bitbucket-branch-source-plugin/tree/bitbucket-payload-in-job

println(currentBuild.getBuildCauses('com.cloudbees.jenkins.plugins.bitbucket.hooks.WebhookCause'))

robross0606 commented 3 years ago

Now I've hit the same problem as others. I need to be able to send email notifications to the Commit Authors but, for whatever reason, this is utterly incorrect when the project is checked out via the Branch Source Plugin. For whatever reason, it thinks the Jenkins server itself is the committer:

[2021-07-16T13:59:43.163Z] GIT_AUTHOR_EMAIL=ci@company.com
[2021-07-16T13:59:43.163Z] GIT_AUTHOR_NAME=Jenkins CI
[2021-07-16T13:59:43.163Z] GIT_BRANCH=master
[2021-07-16T13:59:43.163Z] GIT_COMMIT=57550ddc449c6e48148c2e376f40f333b7d44ee4
[2021-07-16T13:59:43.163Z] GIT_COMMITTER_EMAIL=ci@company.com
[2021-07-16T13:59:43.163Z] GIT_COMMITTER_NAME=Jenkins CI
[2021-07-16T13:59:43.163Z] GIT_LOCAL_BRANCH=master
[2021-07-16T13:59:43.163Z] GIT_PREVIOUS_COMMIT=57550ddc449c6e48148c2e376f40f333b7d44ee4
[2021-07-16T13:59:43.163Z] GIT_PREVIOUS_SUCCESSFUL_COMMIT=5d63a2ab45ec877a1723d5f940dae3ade4b841ba

This renders any pipeline build using this plugin semi-useless when using something like emailExt since it has no idea who the "culprits" or even "developers" are.

robross0606 commented 3 years ago

Hi, you can use my fork for this: https://github.com/oleh-ozimok/bitbucket-branch-source-plugin/tree/bitbucket-payload-in-job

println(currentBuild.getBuildCauses('com.cloudbees.jenkins.plugins.bitbucket.hooks.WebhookCause'))

Is there any plan to PR this fork back into the mainline?

robross0606 commented 3 years ago

You can get those from build revision object:

SCMRevisionAction action = build.getAction(SCMRevisionAction)

action.revision.author action.revision.message action.revision.hash

I've been trying to use this but cannot seem to figure out where they are usable. I keep getting errors like this in my pipeline:

WorkflowScript: 36: unable to resolve class SCMRevisionAction 

 @ line 36, column 23.

       SCMRevisionAction action = build.getAction(SCMRevisionAction)
KalleOlaviNiemitalo commented 2 years ago

@robross0606, SCMRevisionAction is defined in the jenkins.scm.api package; try import jenkins.scm.api.SCMRevisionAction at the top of your pipeline. However, I don't know whether Jenkins allows access to that class from untrusted pipelines by default.

Impakt commented 2 years ago

it doesn't work. groovy.lang.MissingPropertyException: No such property: author for class: com.cloudbees.jenkins.plugins.bitbucket.PullRequestSCMRevision