Our project is using protected branches and requires the base branch of a PR to be up-to-date with the target branch in order to merge. We are also using Jenkins to build the unmerged head of the PR because the plugin we use will automatically rebuild all open PRs when the target branch changes, which can quickly clog up a pipeline. So, in the case that a PR is opened without being up-to-date with the target branch, we want to be able to stop the Jenkins pipeline right away and notify the committer that they need to merge first.
So, using the GitHub API, I would like to be able to tell if a pull request is up-to-date with the target branch. The closest thing to this seems to be the "mergeable" attribute on a pull request, but that looks like it only indicates whether a safe auto-merge CAN be done, not whether the branch is already up-to-date.
Is there a direct API json tag that can be looked at? If not, is there a simple way to check this manually with git commands?
I don't know whether GitHub exposes this information through their API, but you can detect this manually with Git commands. You want to find what is known as the merge base, and ensure that this commit is the same as the tip of master
(or whatever your main branch is).
In the form of a bash script, it would look something like this:
if [ $(git merge-base @ master) == $(git rev-parse master) ]
then
echo "Your branch is up to date."
exit 0
else
echo "You need to merge / rebase."
exit 1
fi
If you include this script as a build step, then the exit values should cause Jenkins to fail the job if necessary.
As mentioned in Dmitry's answer , with newer versions of Git you can use the --is-ancestor
flag for git merge-base
to simplify it to one command. The script would then look like this:
if git merge-base --is-ancestor master @
then
echo "Your branch is up to date."
exit 0
else
echo "You need to merge / rebase."
exit 1
fi
If you want to use github API. Any API that returns the pull request object will have the mergeable_state
field in it. If its value is behind
, that means the base branch is updated after the pull request is created. ie: pull request branch is out of date.
Here's mergestatestatus explanation
If you are handling webhook responses in Jenkings server, most pull request events like, pull request created, edited, closed or issue_comment events will include the mergeable_state
information in the pull request object.
This is a very common problem for people who want to implement a workflow where merge are serialized.
This is something that can be solved using Mergify easily. It offers exactly what you need via its strict workflow . Mergify takes in charge the update of your out-of-date pull request and order the merge of the valid pull requests for you. We built Mergify to solve exactly this problem in the first place actually!
(Disclaimer: I'm one of the Mergify author)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.