Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,297,388
Community Members
 
Community Events
165
Community Groups

Create a Pull request using curl with a for loop in bash

Edited

How would I go to create a PR using curl but referring to a for loop so it would loop through multiple repos, declared in a variable.

 

#!/bin/bash
## declare an array variable
declare -a LIST=("repo1" "repo2")

## now loop through the above array
for i in "${REPOS[@]}"
do
   echo "$i"
   # or do whatever with individual element of the array

#      -u myuser:password \
#      --request POST \
#      --header 'Content-Type: application/json' \
#        --data '{
#            "title": "My_PR_Created",
#            "source": {  
#                "branch": {
#                  "name": "BranchForTest"
#         }
#     }
#  }'
done
Outcome: it runs but doesn't create the PR's

1 answer

0 votes
Patrik S Atlassian Team Jun 28, 2022

Hey @cesar ramirez ,

Welcome to Atlassian Community!

 

I did some tests in my own repositories, and was able to get the bash script working by using the following code : 

#!/bin/bash

 
REPOS=("repo_1" "repo_2")

 
# Loop the array of repos

for i in "${REPOS[@]}"

do

   printf "\n\n Attempting to create PR for repository:  $i \n\n"
 

  curl -v https://api.bitbucket.org/2.0/repositories/<workspace>/"$i"/pullrequests/ \

      -u bitbucket_username:app_password \

      --request POST \

      --header 'Content-Type: application/json' \

      --data '{

            "title": "My_PR_Created",

            "source": {

                "branch": {

                  "name": "name_of_source_branch"

         }

     }

  }'

done

I've added the verbose flag ( -v ) in the curl command so it prints out more information about the request, such as the status code and the response from the API, and you can use it to analyze if the request was successful or not, and if not, have more details about the failure reason such as authentication issue, or missing data in the body of the request.

Also note that you need to use your bitbucket username(not the e-mail address), which you can find in your account's personal settings, along with an App Password (not your account password)  as the credentials for authentication.

Could you please try using the above and let us know how it goes? 

Thank you, @cesar ramirez .

Kind regards,

Patrik S

Thank you Patrick, I am in the process of testing it. I really appreciate your response.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
TAGS
Community showcase
Published in Bitbucket

Git push size limits are coming to Bitbucket Cloud starting April 4th, 2022

Beginning on April 4th, we will be implementing push limits. This means that your push cannot be completed if it is over 3.5 GB. If you do attempt to complete a push that is over 3.5 GB, it will fail...

2,214 views 2 9
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you