Create a Pull request using curl with a for loop in bash
Asked by cesar ramirez:

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

# curl [https://api.bitbucket.org/2.0/repositories/{mycompany}/$LIST/pullrequests/](https://api.bitbucket.org/2.0/repositories/{mycompany}/$REPOS/pullrequests/) \\  
# -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


Answers:
author: Patrik S
body: Hey [@cesar ramirez](/t5/user/viewprofilepage/user-id/4953152) ,

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](https://bitbucket.org/account/settings/), along with an [App Password](https://support.atlassian.com/bitbucket-cloud/docs/app-passwords/) (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](/t5/user/viewprofilepage/user-id/4953152) .

Kind regards,

Patrik S

comments: author: cesar ramirez
body: Thank you Patrick, I am in the process of testing it. I really appreciate your response.


author: cesar ramirez
body: How would you merge those PR?, I have these, but I get a syntax error.

#!/bin/bash

#Loop Works with array, but once we introuduce the API it doesn't

## declare an array variable  
declare -a REPOS=("name")

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

curl -v <https://api.bitbucket.org/2.0/repositories/{name}/>"$i"/pullrequests/{id_number}/merge' \\  
-u my username:token \\  
--request POST \\  
--header 'Content-Type: application/json' \\  
--data '{  
"type": "\<string\>",  
"message": "\<string\>",  
"close_source_branch": true,  
"merge_strategy": "merge_commit"  
}'

done


author: Patrik S
body: Hey [@cesar ramirez](/t5/user/viewprofilepage/user-id/4953152) ,

Could you share what is the specific error your script is returning?

Also, the Bitbucket API endpoint used to merge a pull request requires you to pass the pull request ID number as part of the URL, as documented [here](https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pullrequests/#api-repositories-workspace-repo-slug-pullrequests-pull-request-id-merge-post). Checking in the code you have shared, I don't see any variables being populated with the pull request ID number, and this is likely the reason why the API call is failing.

If you want to merge all the open pull requests in a repository programmatically, you would need to first get the PR IDs by calling the [List Pull Request endpoint](https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pullrequests/#api-repositories-workspace-repo-slug-pullrequests-get), and then use the list of PR IDs returned to then call the [Merge Pull Request endpoint](https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pullrequests/#api-repositories-workspace-repo-slug-pullrequests-pull-request-id-merge-post) for each PR ID.

Thank you, [@cesar ramirez](/t5/user/viewprofilepage/user-id/4953152) .

Kind regards,

Patrik S


Tags: bitbucket-cloud

cloud