extmkv
extmkv

Reputation: 2045

Jenkinsfile, get all modified files in the PR

I want to have a list of modified files in the PR, I tried a few solutions but I can only have access to the modified files in the last commits pushed to remote.

Example:

So, in this example I want to have the list of files from all the new commits in the PR.

Code:

  def changeLogSets = currentBuild.rawBuild.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]
            def files = new ArrayList(entry.affectedFiles)
            for (int k = 0; k < files.size(); k++) {
                def file = files[k]
                print file.path 
            }
        }
    }

Upvotes: 6

Views: 6483

Answers (2)

extmkv
extmkv

Reputation: 2045

I've found the solution using a git command:

/**
 * Compares the current branch and target branch and extract the changed files.
 *
 * @return the PR changed files.
 */
def getPRChangelog() {
    return sh(
            script: "git --no-pager diff origin/${params.target} --name-only",
            returnStdout: true
    ).split('\n')
}

Upvotes: 3

tkosinski
tkosinski

Reputation: 1696

Use the Git Plugin (if you aren't already). Then something like:

git diff --name-status $GIT_PREVIOUS_SUCCESSFUL_COMMIT..$GIT_COMMIT > allchanges

That will give you the list of all changes since the last successful build.

Upvotes: 1

Related Questions