bitbucket-branch-source-plugin icon indicating copy to clipboard operation
bitbucket-branch-source-plugin copied to clipboard

Add comment to Pull Request

Open jdomag opened this issue 3 years ago • 3 comments

Is there any simple way of detecting that job has started as a result of Pull Request creation (not regular push to the branch) and fetch PR id to use Bitbucket API to comment back to PR? I would like to do some operations and post a result of it to PR to make life of reviewer easier.

jdomag avatar Aug 26 '21 20:08 jdomag

Unsure if this is relevant to this specific plugin, but I hacked up a script a while back which was referenced within a build for this. It's worth reading into the CHANGE_BRANCH var when building PRs.

Its some terrible bash, but hopefully you get the gist

# Get the PR id
PR_ID=$(curl -s https://api.bitbucket.org/2.0/repositories/${BB_WSPACE}/${JOB_NAME}/pullrequests \
    -u ${BB_USER} \
    jq -r '.values[] | "\(.id) \(.source.branch)"' | grep ${CHANGE_BRANCH} | awk {'print $1'})

# Comment on the PR id
bitbucket_comment () {
  curl -s https://api.bitbucket.org/2.0/repositories/${BB_WSPACE}/${JOB_NAME}/pullrequests/${PR_ID}/comments \
  -u ${BB_USER} \
  --request POST --header 'Content-Type: application/json' \
  -d '{"content":{"raw":"'"$1"'"}}'
}

bitbucket_comment "Jenkins PR comment...."

alwesto avatar Aug 26 '21 21:08 alwesto

@jdomag PR id is available as env.CHANGE_ID. Some other variables are availaible as well: https://javadoc.jenkins.io/plugin/pipeline-model-definition/org/jenkinsci/plugins/pipeline/modeldefinition/when/utils/EnvironmentNames.html

Jenkins httpRequest step can be used to perform the request like:

httpRequest url: url, contentType: 'APPLICATION_JSON', httpMode: 'POST', authentication: 'my-auth-token', requestBody: groovy.json.JsonOutput.toJson([text: ":warning: Jenkins Build failed $BUILD_URL"])

but I agree, this would be a nice feature for this plugin.

mguillem avatar Sep 13 '21 12:09 mguillem

@jdomag PR id is available as env.CHANGE_ID. Some other variables are availaible as well: https://javadoc.jenkins.io/plugin/pipeline-model-definition/org/jenkinsci/plugins/pipeline/modeldefinition/when/utils/EnvironmentNames.html

Jenkins httpRequest step can be used to perform the request like:

httpRequest url: url, contentType: 'APPLICATION_JSON', httpMode: 'POST', authentication: 'my-auth-token', requestBody: groovy.json.JsonOutput.toJson([text: ":warning: Jenkins Build failed $BUILD_URL"])

but I agree, this would be a nice feature for this plugin.

Are you sure it's avaliable via bitbucket branch source plugin? I can see those env in MultiBranch Pipeline only not in Bitbucket Team/Project: https://www.jenkins.io/doc/book/pipeline/multibranch/

jdomag avatar Sep 13 '21 13:09 jdomag