I've just discovered this behaviour too, and (using Chris' code as inspiration) I came up with the following:
def scmToBuildWith=null
pipeline {
...
options {
...
skipDefaultCheckout true }
stages {
stage ('Calculate Changelog') {
when { not { environment name: 'CHANGE_TARGET', value: '' }
expression { return !currentBuild.previousBuild }
}
steps { script {
scmToBuildWith=scm
def targetBranchForPR = env.CHANGE_TARGET
hudson.util.DescribableList scmExtensions=scmToBuildWith.getExtensions()
def githubReposWeFetchFrom = scmToBuildWith.userRemoteConfigs
def firstRepoWeFetchFrom = githubReposWeFetchFrom?githubReposWeFetchFrom[0]:null
def probablyCalledOrigin = firstRepoWeFetchFrom?firstRepoWeFetchFrom.name:'origin'
println "Calculating changelog against ${probablyCalledOrigin}/${targetBranchForPR}"
def changeLogCalc = new hudson.plugins.git.extensions.impl.ChangelogToBranch(new hudson.plugins.git.ChangelogToBranchOptions(probablyCalledOrigin, targetBranchForPR))
scmExtensions.replace(changeLogCalc)
}
}
}
stage ('Checkout') {
steps {
checkout scmToBuildWith
}
}
...
This results in the official changes for a PR build to be the differences between the PR and the main branch.
You'll probably need an admin to "approve" some script functions too:
method hudson.util.DescribableList replace hudson.model.Describable
new hudson.plugins.git.ChangelogToBranchOptions java.lang.String java.lang.String
new hudson.plugins.git.extensions.impl.ChangelogToBranch hudson.plugins.git.ChangelogToBranchOptions
To be honest, this is the kind of functionality I'd expect to have been built into the github multi-branch source plugin so that it wasn't necessary to reconfigure the SCM stuff "on the fly" like this, but at least pipelines make this sort of workaround possible...
PS. It's necessary to make a copy of scm and use that thereafter because every reference to it results in a fresh instance containing only the original data - no changes "stick".
I was able to work around this issue by defining a function for my pipeline:
and then using:
in my pipeline stages