Authorization method to merge a PR in a pipeline when any changes, decline when none

John Warde January 17, 2025

This is my first foray into bitbucket pipelines.

In summary, when a pull request is created, I want a pipeline to decline the PR if it has no changes and merge the PR if there are changes.  And only target source branches starting with a specific name.

Basically, the curl API calls do not work, they appear to return nothing.  I have also tried using bearer token authorization where I created an Access token in the repository settings and copied that token into a repository variable and used that variable.

Is there a git command I could use to decline/merge the PR without credentials/API?

Hopefully the YML content renders correctly.

Thanks in advance for your help.

 

 

 

  pull-requests: '**': - step: name: Check PR Source Branch script: - if [[ ! $BITBUCKET_BRANCH == blah-* ]]; then echo "Skipping PR not from blah branch"; exit 0; fi - step: name: Check for Changes script: - git fetch origin $BITBUCKET_PR_DESTINATION_BRANCH - git diff --exit-code origin/$BITBUCKET_PR_DESTINATION_BRANCH || echo "Changes detected" - if git diff --exit-code origin/$BITBUCKET_PR_DESTINATION_BRANCH; then echo "No changes detected, declining PR"; RESPONSE=$(curl -X POST -u $BITBUCKET_USERNAME:$BITBUCKET_APP_PASSWORD https://api.bitbucket.org/2.0/repositories/$BITBUCKET_REPO_OWNER/$BITBUCKET_REPO_SLUG/pullrequests/$BITBUCKET_PR_ID/decline) echo "RESPONSE = [$RESPONSE]" exit 1; fi - step: name: Merge PR script: - echo "Changes detected, merging PR"; - RESPONSE=$(curl -X POST -u $BITBUCKET_USERNAME:$BITBUCKET_APP_PASSWORD https://api.bitbucket.org/2.0/repositories/$BITBUCKET_REPO_OWNER/$BITBUCKET_REPO_SLUG/pullrequests/$BITBUCKET_PR_ID/merge) - echo "RESPONSE = [$RESPONSE]" 

2 answers

1 accepted

1 vote
Answer accepted
John Warde January 21, 2025

Solved now.  I needed to set up an app password in my personal settings in bitbucket then create BITBUCKET_USERNAME and BITBUCKET_APP_PASSWORD repository variables, using the newly created app password from personal settings in BITBUCKET_APP_PASSWORD and the target bitbucket username in BITBUCKET_USERNAME.

0 votes
Syahrul
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 20, 2025

G'day, @John Warde 

I'm curious why you need to decline a pull request without any changes, as it's typically not possible to create a PR without modifications. Could you provide more context on this situation so I can offer more accurate advice

Regards,
Syahrul

John Warde January 21, 2025

It is possible through automation.  An external service, providing translations into PHP language files, creates an empty PR when there are no differences between the existing language files in the repo and what the external service has in store for our application language strings.

The root of the problem is, how do correctly authenticate the curl https://api.bitbucket.org/ API calls in a bitbucket pipeline?  What type credential do I use? Is there any other setup I need to do outside of the pipeline i.e. in repository settings etc..  I am able to successfully execute the same API calls from my local machine but not within the bitbucket pipeline enironment.

 


Hopefully the indentation is correct now below ...

  pull-requests:
'**':
- step:
name: Check PR Source Branch
script:
- if [[ ! $BITBUCKET_BRANCH == blah-* ]]; then echo "Skipping PR not from blah branch"; exit 0; fi
- step:
name: Check for Changes
script:
- git fetch origin $BITBUCKET_PR_DESTINATION_BRANCH
- git diff --exit-code origin/$BITBUCKET_PR_DESTINATION_BRANCH || echo "Changes detected"
- if git diff --exit-code origin/$BITBUCKET_PR_DESTINATION_BRANCH; then
echo "No changes detected, declining PR";
RESPONSE=$(curl -X POST -u $BITBUCKET_USERNAME:$BITBUCKET_APP_PASSWORD https://api.bitbucket.org/2.0/repositories/$BITBUCKET_REPO_OWNER/$BITBUCKET_REPO_SLUG/pullrequests/$BITBUCKET_PR_ID/decline)
echo "RESPONSE = [$RESPONSE]"
exit 1;
fi
- step:
name: Merge PR
script:
- echo "Changes detected, merging PR";
- RESPONSE=$(curl -X POST -u $BITBUCKET_USERNAME:$BITBUCKET_APP_PASSWORD https://api.bitbucket.org/2.0/repositories/$BITBUCKET_REPO_OWNER/$BITBUCKET_REPO_SLUG/pullrequests/$BITBUCKET_PR_ID/merge)
- echo "RESPONSE = [$RESPONSE]"

 

 

 

 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Product Admin Site Admin
TAGS
AUG Leaders

Atlassian Community Events