You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
I'm trying to set up a pull request build that calculates the files changed in a pull request on a certain path.
I'm trying to do this so that I can exercise any new tests introduced in the PR by running them multiple times (to ensure they are not flakey).
I know that the BITBUCKET_PR_DESTINATION_BRANCH variable exists, but it seems like that does not allow me to do what I want, because the merge that pipelines does for a pull-request build is not always the tip of the destination branch.
If the merge commit that happens on that build produces a merge, I can get the commit SHA using:
git rev-list --parents -n 1 HEAD | cut -d " " -f 3
I use that to grab the files like this:
git diff --name-only HEAD $(git rev-list --parents -n 1 HEAD | cut -d " " -f 3) -- test/
However, if the pull-request branch is up to date with the destination branch, no merge commit is produced, and the above does not work.
Is there any way to get the commit SHA Bitbucket Pipelines uses in the Build setup?
Or another easy way to produce a list of the changed files of a certain path in that PR?
Hello @Felix ,
Welcome to Atlassian Community!
In bitbucket pipelines, when you are using pull-request triggers pipelines it should always merge the tip of the destination branch with the current working branch (the source of the pull request). So in this case, to get the commit hash that is shown in the merge command at Build Setup stage you could use git log as below :
git log -n 1 --pretty=format:"%H" $BITBUCKET_PR_DESTINATION_BRANCH
This will print the tip of the destination branch of the pull request, which should match the commit hash presented in Build Setup.
As for getting the files that were changed in the source branch of the pull request, I think you could make use of the git three-way diff, as below :
git diff $BITBUCKET_PR_DESTINATION_BRANCH...HEAD
This will list all the files that were changed in the source branch of the Pull Request starting at the last common commit between the source and destination branch of the pull request.
Hope that helps! Let me know in case you have any questions.
Thank you, @Felix .