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,368,635
Community Members
 
Community Events
168
Community Groups

List of last merged PRs via API

Hi All,

 

I'm trying to get a list of MERGED Pull Requests.
However using the API command returns nothing -

`curl -XGET -u '<username>:<app password>' -H 'Accept: application/json' https://api.bitbucket.org/2.0/repositories/workspace/repo/pullrequests -d '{"state": "MERGED"}`

 

I've created a fresh app password with read/write permissions for all resources and it still doesn't return any data.
There are something like 10-20 at least.
I would also need to filter them to date and time values.

Please advise.

1 answer

1 accepted

0 votes
Answer accepted
seanaty Atlassian Team Jan 05, 2022

it still doesn't return any data.

Is it an empty JSON object? A page object with zero items? Is there an HTTP status code? I'm sure the request is returning some type of data that would be useful for diagnosing.

My guess is the issue is how you're creating the request in curl. Here is the documentation I'm seeing for the -d flag of curl.

-d, --data <data>

       (HTTP MQTT) Sends the specified data in a POST request to the HTTP server, in the same way that a browser
       does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the
       data to the server using the content-type application/x-www-form-urlencoded.  Compare to -F, --form. 

For the GET request, data should be query parameters. 

To get only merged pull requests you need to construct a query and pass it in as the `q` parameter, being sure to URL-encode the value. For more details about how to filter and sort the API read here: https://developer.atlassian.com/cloud/bitbucket/rest/intro/#filtering

But you essentially want to add these params to the query string

q=state%3D%22MERGED%22&sort=-updated_on

 Breaking this down, you're passing in 2 parameters. q for the query (filtering the results) and sort for, well, sorting. The q parameter, when URL-decoded is `state="MERGED"`. It's passed in as a string in this manner because you could have other filtering criteria that you'd like to add with AND, OR, etc. It's kinda like SQL. The value of the sort parameter is the field you'd like to sort by and a minus sign to indicate reverse chronological order, the making the most recently updated PR show up as the first.

@seanaty 
Really sorry for the delayed response.

I did try your modification but curl just exits without any output whatsoever and in Postman I just get either a 401 (Authorization denied) with an API key and secret or a 200 with my bitbucket username and app password with the error -

"The requested repository either does not exist or you do not have access. If you believe this repository exists and you have access, make sure you're authenticated."

I'm not sure what I'm supposed to use to authenticate
curl -XGET -u 'avjp2EfnjpBLYMC9g4:5EqbSvaNe79GcLLSq3RNL4L8fxbfcqZC' --url 'https://api.bitbucket.org/2.0/repositories/{workspace}/{repo}/pullrequests?q=state%3D%22MERGED%22&sort=-updated_on'

Any other mistakes I'm making?
Please do let me know.

seanaty Atlassian Team Jan 20, 2022

If you are seeing "The requested repository either does not exist or you do not have access. If you believe this repository exists and you have access, make sure you're authenticated." it's likely that you're getting either a 404 or 403 response and not a 200. (adding -v to curl will let you see all the headers including the response code).

There are 2 things that can be happening and you want to troubleshoot both of them.

1. Ensure that you're authenticating correctly.

a. Ensure you're using the right username. Your username is specific to Bitbucket only and is unique globally. You can view it and change it here: https://bitbucket.org/account/settings/username/change/ (I don't recommend changing it!). The name listed here is what you should be using as username.

b. Ensure you're using the right app password. In the same account go to https://bitbucket.org/account/settings/app-passwords/ to create the app password. It may go without saying but the app password has to be from the same account that the username is from. If you have multiple accounts perhaps this is getting mixed up.

c. verify that authentication is working correctly and as expected by making a request to https://api.bitbucket.org/2.0/user. This is an important step because the API you're attempting to use can be accessible without authentication in the case of public repositories but to make a request to this API you must be authenticated and it will tell you who you are authenticated as.

2. Ensure repository URL is correct and the authenticated user has access.

a. Ensure that the user from step 1 can access this repository in the UI by going to bitbucket.org/<workspace>/<repo>/

b. Ensure that <workspace>/<repo> in your curl request is without typos.

It seems like a lot of things to check, but if you do all these checks I'm pretty certain you'll find the problem.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
TAGS

Atlassian Community Events