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:
- 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
- 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}]
- 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()