0

How to get all the status/transition in Jira API ?

http://example.com/rest/api/2/workflow/ returns

[{"name":"jira","description":"The default JIRA workflow.","steps":5,"default":true}]
No id is present in the output, so not able to get workflow details/components.

http://example.com/jira/rest/api/2/issue/PROJ-1/transitions provides only issue specific transition, I need future transitions too.

Any pointer or suggestion would be appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Pradnya Mhatre
  • 208
  • 2
  • 8

2 Answers2

0

I can't mark question as duplicated, so here is repost:

You can get all transitions for project with /rest/api/2/project/{projectIdOrKey}/statuses endpoint. Here is response example, look at "statuses" array:

[
    {
        "self": "http://localhost:8090/jira/rest/api/2.0/issueType/3",
        "id": "3",
        "name": "Task",
        "subtask": false,
        "statuses": [
            {
                "self": "http://localhost:8090/jira/rest/api/2.0/status/10000",
                "description": "The issue is currently being worked on.",
                "iconUrl": "http://localhost:8090/jira/images/icons/progress.gif",
                "name": "In Progress",
                "id": "10000"
            },
            {
                "self": "http://localhost:8090/jira/rest/api/2.0/status/5",
                "description": "The issue is closed.",
                "iconUrl": "http://localhost:8090/jira/images/icons/closed.gif",
                "name": "Closed",
                "id": "5"
            }
        ]
    }
]
Community
  • 1
  • 1
Bushikot
  • 783
  • 3
  • 10
  • 26
  • There can be 10 statuses in my jira Instance, but my workflow might be having only 3 statuses like Open, In progress and Complete. I want to know workflow associated statuses. – Pradnya Mhatre Aug 03 '16 at 11:51
0

If you know the name of the workflow, you can get all of its transitions with:

GET /rest/api/2/workflow/search?workflowName={workflow_name}&expand=transitions

See https://developer.atlassian.com/cloud/jira/platform/rest/v2/api-group-workflows/#api-rest-api-2-workflow-search-get for more details.

However, if all you have is an issue_key, how do you find all transitions (not just the ones available based on its status)? There are three steps:

  1. Get the issue's project_id and issue_type_id
  • GET /rest/api/2/issue/{issue_key}
  • project_id = issue.fields.project.id
  • issue_type_id = issue.fields.issuetype.id
  1. Get the workflow name associated with the issue type from the project's workflow scheme
  • GET /rest/api/2/workflowshceme/project?projectId={project_id}
  • workflow_name = values[0].workflowScheme.issueTypeMappings[{issue_type_id}]
  1. Now at last we can get the transitions
  • GET /rest/api/2/workflow/search?workflowName={workflow_name}&expand=transitions

Here is all of that in python:

import sys
import requests
import json
# NOTE: jira_integration encapsulates how we connect to our Jira account
#       adapt this script as you see fit
from jira_integration import JIRA_API_BASE_URL, JIRA_AUTH_HEADERS


def getFromJira(url_path):
    response = requests.get(f'{JIRA_API_BASE_URL}{url_path}', headers=JIRA_AUTH_HEADERS)
    
    if not response.status_code  == 200:
        raise RuntimeError(f'getFromJira() was not successful: {response.status_code} {response.reason}\n{response.json()}') 
    
    return response.json()


def main():
    if len(sys.argv) == 2:
        issue_key = sys.argv[1]
    else:
        print('Please provide the issue key')
        return

    try:
        # Step 1
        issue = getFromJira(f'/rest/api/2/issue/{issue_key}')
        project_id = issue['fields']['project']['id']
        issue_type_id = issue['fields']['issuetype']['id']

        # Step 2
        search_results = getFromJira(f'/rest/api/2/workflowscheme/project?projectId={project_id}')
        workflow_scheme_project_association = search_results['values'][0]
        workflow_name = workflow_scheme_project_association['workflowScheme']['issueTypeMappings'][issue_type_id]

        # Step 3
        search_results = getFromJira(f'/rest/api/2/workflow/search?workflowName={workflow_name}&expand=transitions')
        transitions = search_results['values'][0]

        print(json.dumps(transitions, indent=4))
        
    except RuntimeError as ex:
        print(ex)


if __name__ == '__main__':
    main()
biscuit314
  • 2,384
  • 2
  • 21
  • 29