Alexander Reifinger
Alexander Reifinger

Reputation: 532

Quickly find all Bitbucket Pull requests for a Jira issue

I would like to find all pull requests for a Jira issue. Obviously, this is a possible task, as Jira itself shows the information:

Screenshot Jira/Bitbucket integration

Currently, I retrieve a list of all merged and open pull requests via the Bitbucket API, and pattern match these to my issue number. This is time consuming, even more so as I have to load the pull requests in batches of 100 (Max limit in Bitbucket), and we have our code spread in several repositories.

There is an integration api call to bitbucket: /rest/jira/1.0/issues//commits, which will show all commits to this issue, but .../pullrequests is not available.

Does anyone know, how Jira retrieves this information?

Upvotes: 6

Views: 7773

Answers (4)

Prasannjeet Singh
Prasannjeet Singh

Reputation: 1457

Find all Pull Requests for a JIRA issue

My answer also uses the same dev-status API, but let me provide the entire steps for this to work. I will provide cURLs for API so you can translate it to any preferred language.

Sadly, I couldn't find any other way to use this API without your actual JIRA password, the JIRA token didn't work for me. If you could make it work without the actual password, let me know too.

I assume you already have the ticket-id with you and it's a numeric ID corresponding to each textual id (eg. STACK-100 may have an id that's something like 301736)

Step 1: Login to JIRA to fetch the JSESSIONID

This is where we first need to login to JIRA via our actual username and password, from where we extract this session id from cookies:

curl --location 'https://YOUR_JIRA_URL/login.jsp' \
--data-urlencode 'os_username=YOUR_USERNAME' \
--data-urlencode 'os_password=YOUR_PASSWORD' \
--data-urlencode 'os_destination=' \
--data-urlencode 'user_role=' \
--data-urlencode 'atl_token=' \
--data-urlencode 'login=Log In'

Don't forget to replace: YOUR_JIRA_URL, YOUR_USERNAME and YOUR_PASSWORD with the right values.

A successful request will result in an HTML response. Ignore it and look into the cookies of the response. There you'll find JSESSIONID and it's value. We use this to make our next request:

Step 2: Fetch Pull Requests via the JSESSIONID:

Execute this cURL:

curl --location 'https://YOUR_JIRA_URL/rest/dev-status/latest/issue/detail?issueId=ISSUE_ID&applicationType=stash&dataType=pullrequest' \
--header 'Cookie: JSESSIONID=EXTRACTED_JSESSIONID;'

This will give you the response that you need. Don't forget to replace: YOUR_JIRA_URL, ISSUE_ID and EXTRACTED_JSESSIONID with the correct values.

Note that I have used applicationType=stash, but it could be bitbucket or github as well.

Upvotes: 0

mdarwish
mdarwish

Reputation: 1

The correct API:

https://{jiraHost}/rest/dev-status/latest/issue/details?issueId={jiraIssueNumericId}&applicationType=<scm>&dataType=<option>

where <scm> could be bitbucket, stash or github
and dataType could be branch or pullrequest

Upvotes: 0

Pigalev Pavel
Pigalev Pavel

Reputation: 1185

https://github.com/jira-node/node-jira-client/issues/142

JIRA has an undocumented "dev-status" API that is usual when JIRA is integrated with other tools like Stash (Bitbucket Server)

At first you have to get jiraIssueNumericId. For example you can get it by getting issue info via Jira API that is well documented. The field you are looking for is "id". https://docs.atlassian.com/software/jira/docs/api/REST/7.6.1/?_ga=2.203378385.1940451621.1522669776-298439511.1476796418#api/2/issue-getIssue)

About methods of this Jira dev-status API:

  1. To get info about branches and pull requests related to an issue: https://{jiraHost}/rest/dev-status/latest/issue/detail?issueId={jiraIssueNumericId}&applicationType=stash&dataType=pullrequest
  2. To get info about commits related to an issue: https://{jiraHost}/rest/dev-status/latest/issue/detail?issueId={jiraIssueNumericId}&applicationType=stash&dataType=repository
  3. To get summary: https://{jiraHost}/rest/dev-status/latest/issue/summary?issueId={jiraIssueNumericId}

P.S. This API is actually used on issue page in Jira. Try clicking on pull-requests link to open popup window with a list of them. In development panel of your browser in Network tab you will find XHR-calls or these urls.

P.P.S. And yes, I've also struggled to find this info and I have no idea why it is undocumented.

Upvotes: 11

Ben
Ben

Reputation: 77

It looks like this endpoint used to be documented in the API https://developer.atlassian.com/static/rest/stash/2.6.0/stash-jira-integration-rest.html#idp21856

But I'm pretty sure its an internal API so you shouldn't rely on it being stable. The current documentation doesn't list it https://developer.atlassian.com/static/rest/bitbucket-server/4.13.0/bitbucket-rest.html

Upvotes: 0

Related Questions