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

it isn't possible to get file changelist from PR in mutibranch project #561

Closed saytik closed 2 years ago

saytik commented 2 years ago

Jenkins and plugins versions report

Environment ```text Jenkins: 2.319.3 OS: Linux - 4.15.0-161-generic --- ace-editor:1.1 ansicolor:1.0.1 antisamy-markup-formatter:2.7 apache-httpcomponents-client-4-api:4.5.13-1.0 authentication-tokens:1.4 bitbucket:214.v2fd4234d0554 bitbucket-pullrequest-builder:1.5.0 bootstrap4-api:4.6.0-3 bootstrap5-api:5.1.3-5 bouncycastle-api:2.25 branch-api:2.7.0 build-with-parameters:1.6 caffeine-api:2.9.2-29.v717aac953ff3 checks-api:1.7.2 cloudbees-bitbucket-branch-source:751.vda_24678a_f781 cloudbees-folder:6.18 command-launcher:1.6 config-file-provider:3.9.0 credentials:1074.v60e6c29b_b_44b_ credentials-binding:1.27.1 display-url-api:2.3.5 durable-task:493.v195aefbb0ff2 echarts-api:5.3.0-2 email-ext:2.87 external-monitor-job:191.v363d0d1efdf8 font-awesome-api:6.0.0-1 git:4.10.3 git-client:3.11.0 git-server:1.10 greenballs:1.15.1 handlebars:3.0.8 handy-uri-templates-2-api:2.1.8-1.0 jackson2-api:2.13.1-246.va8a9f3eaf46a javax-activation-api:1.2.0-2 javax-mail-api:1.6.2-5 jaxb:2.3.0.1 jdk-tool:1.5 jnr-posix-api:3.1.7-1 jquery-detached:1.2.1 jquery3-api:3.6.0-2 jsch:0.1.55.2 junit:1.54 ldap:2.7 lockable-resources:2.14 mailer:408.vd726a_1130320 matrix-auth:2.6.9 matrix-project:1.20 mercurial:2.16 momentjs:1.1.1 pam-auth:1.7 pipeline-build-step:2.15 pipeline-graph-analysis:188.v3a01e7973f2c pipeline-input-step:446.vf27b_0b_83500e pipeline-milestone-step:100.v60a_03cd446e1 pipeline-model-api:1.9.3 pipeline-model-definition:1.9.3 pipeline-model-extensions:1.9.3 pipeline-rest-api:2.21 pipeline-stage-step:291.vf0a8a7aeeb50 pipeline-stage-tags-metadata:1.9.3 pipeline-stage-view:2.21 plain-credentials:1.8 plugin-util-api:2.14.0 popper-api:1.16.1-2 popper2-api:2.11.2-1 postbuild-task:1.9 rebuild:1.33 scm-api:595.vd5a_df5eb_0e39 scm-filter-branch-pr:0.5.1 script-security:1131.v8b_b_5eda_c328e slack:602.v0da_f7458945d snakeyaml-api:1.29.1 ssh-credentials:1.19 ssh-slaves:1.33.0 sshd:3.1.0 structs:308.v852b473a2b8c timestamper:1.17 token-macro:277.v7c8f82a_d66b_3 trilead-api:1.0.13 windows-slaves:1.8 workflow-aggregator:2.6 workflow-api:1136.v7f5f1759dc16 workflow-basic-steps:2.24 workflow-cps:2648.va9433432b33c workflow-cps-global-lib:552.vd9cc05b8a2e1 workflow-durable-task-step:1121.va_65b_d2701486 workflow-job:1145.v7f2433caa07f workflow-multibranch:706.vd43c65dec013 workflow-scm-step:2.13 workflow-step-api:622.vb_8e7c15b_c95a_ workflow-support:813.vb_d7c3d2984a_0 ```

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

ubuntu 20.04

Reproduction steps

  1. configure multibranch project with Jenkinsfile, that will be triggered on PR
  2. Try to get files changelist
Jenkinsfile ```text def boolean sourceChanged(String module) { def target_branch = env.CHANGE_TARGET; def pr_ref = env.BRANCH_NAME; if (target_branch == null) { echo "No target branch defined..."; return true; } def TARGET = sh(returnStdout: true, script: "git rev-parse upstream/${target_branch}").trim() def HEAD = sh(returnStdout: true, script: "git rev-parse origin/${pr_ref}").trim() echo "Checking for source changes between ${TARGET} (${target_branch}) and ${HEAD} (${pr_ref})..."; return sh(returnStatus: true, script: "git diff --exit-code --name-only ${TARGET}...${HEAD} {module}") == 1; } pipeline { //for Jenkins SCM polling + PR agent any options { timestamps () } stages { stage ('Checkout git') { steps { checkout scm } } stage('check-files-changelist') { steps { sourceChanged("jenkins/") } } } } ```

Expected Results

get a changelist of files changed between PR and target branch

Actual Results

getting an error:

git rev-parse upstream/develop
14:58:48  fatal: ambiguous argument 'upstream/develop': unknown revision or path not in the working tree.
14:58:48  Use '--' to separate paths from revisions, like this:
14:58:48  'git <command> [<revision>...] -- [<file>...]'
14:58:48  upstream/develop

Anything else?

Hello.

I can't get files changelist between PR BRANCH and TARGET_BRANCH

def boolean sourceChanged(String module) {
    def target_branch = env.CHANGE_TARGET;
    def pr_ref        = env.BRANCH_NAME;
    if (target_branch == null) {
        echo "No target branch defined...";
        return true;
    }
    def TARGET = sh(returnStdout: true, script: "git rev-parse upstream/${target_branch}").trim()
    def HEAD   = sh(returnStdout: true, script: "git rev-parse origin/${pr_ref}").trim()
    echo "Checking for source changes between ${TARGET} (${target_branch}) and ${HEAD} (${pr_ref})...";
    return sh(returnStatus: true, script: "git diff --exit-code --name-only ${TARGET}...${HEAD} {module}") == 1;
}

But I'm getting an error:

git rev-parse upstream/develop
14:58:48  fatal: ambiguous argument 'upstream/develop': unknown revision or path not in the working tree.
14:58:48  Use '--' to separate paths from revisions, like this:
14:58:48  'git <command> [<revision>...] -- [<file>...]'
14:58:48  upstream/develop

jenkins build "Changes" section always is empty:


Changes
No changes in any of the builds.

Could someone help me, how can I get full files changelist between PR and TARGET_BRANCH in Jenkinsfile and make some conditions on it ?

KalleOlaviNiemitalo commented 2 years ago

Does this project build the source branch of the pull request, or does it build the result of merging the pull request?

If it builds only the source branch, then I think Jenkins might not even fetch the target branch to the repository clone on the agent. You might then have to make the pipeline run git fetch commands within Git Username and Password Binding.

saytik commented 2 years ago

Thanks for your answer. Yes, we run mutibranch jenkins job to run tests when PR is created or when new commit is created in PR source branch. I'll try to use git fetch and come back with the result

screenshot-2022-02-18-13-07-22

.

saytik commented 2 years ago

@KalleOlaviNiemitalo Thanks! It now works, I've added "git fetch origin ${TARGET_BRANCH}" and changed " upstream/${target_branch}" to "origin/${target_branch}" in the function sourceChanged()

def boolean sourceChanged(String module) {
    def target_branch = env.CHANGE_TARGET;
    def pr_ref        = env.BRANCH_NAME;

    if (target_branch == null) {
        echo "No target branch defined...";
        return true;
    }

    def TARGET = sh(returnStdout: true, script: "git rev-parse origin/${target_branch}").trim()
    def HEAD   = sh(returnStdout: true, script: "git rev-parse origin/${pr_ref}").trim()

    echo "Checking for source changes between ${TARGET} (${target_branch}) and ${HEAD} (${pr_ref})...";
    return sh(returnStatus: true, script: "git diff --exit-code --name-only ${TARGET}...${HEAD} ") == 1;
}

pipeline {
    stages {
        stage ('Checkout git') {
            steps {
                checkout scm
                sh '''
                    #!/bin/sh -x
                    set -e  # error out if fails
                    git fetch origin ${TARGET_BRANCH}
                '''
            }
        }
        stage('Build') {
            steps {
                sourceChanged("jenkins/")
        }  }
}

I'll continue to work how to compare changelist with regex (grep). It would be nice to have some ready function for these purposes