Listing All JIRA Transitions via API

浪子不回头ぞ 提交于 2019-12-01 07:34:49

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"
            }
        ]
    }
]

But it doesn't give you exactly list of transitions that any issue can take at any time, and I'm not sure that such method exist in API.

You can list the transitions of a given ticket via this endpoint:

/rest/api/2/issue/${issueIdOrKey}/transitions

For a more in depth explanation take a look here: Does the JIRA REST API require submitting a transition ID when transitioning an issue?

public void changeStatus(IssueRestClient iRestClient,
        List<Statuses> JiraStatuses, String key) {
    String status = "To Do";
    for (Statuses statuses : vOneToJiraStatuses) {
        if (1 == statuses.compareTo(status)) {
            try {
                String _transition = statuses.getTransition();
                Issue issue = iRestClient.getIssue(key).get();
                Transition transition = getTransition(iRestClient, issue,
                        _transition);
                if (!(isBlankOrNull(transition))) {

                    if (!(issue.getStatus().getName()
                            .equalsIgnoreCase(_transition)))
                        transition(transition, issue, null, iRestClient,
                                status);
                }
            } catch (Exception e) {
                Constants.ERROR.info(Level.INFO, e);

            }
            break;
        }
    }

}

List is a pojo implementation where statuses and transitions defined in xml are injected through setter/constructor.

private void transition(Transition transition, Issue issue,
        FieldInput fieldInput, IssueRestClient issueRestClient,
        String status) throws Exception {

    if (isBlankOrNull(fieldInput)) {
        TransitionInput transitionInput = new TransitionInput(
                transition.getId());

                issueRestClient.transition(issue, transitionInput).claim();
        Constants.REPORT.info("Status Updated for : " + issue.getKey());
    } else {
        TransitionInput transitionInput = new TransitionInput(
                transition.getId());
        issueRestClient.transition(issue, transitionInput).claim();
        Constants.REPORT.info("Status Updated for : " + issue.getKey());

    }
}

public Transition getTransition(IssueRestClient issueRestClient,
        Issue issue, String _transition) {

        Promise<Iterable<Transition>> ptransitions = issueRestClient
            .getTransitions(issue);
    Iterable<Transition> transitions = ptransitions.claim();
    for (Transition transition : transitions) {

        if (transition.getName().equalsIgnoreCase(_transition)) {
            return transition;
        }
    }
    return null;
}

In Short using Transition API of JIRA we can fetch all the transitions to set statuses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!