Recently, the project needs to operate the issue of JIRA, such as obtaining issue, changing the status of issue to Done, etc. After Google, THE OFFICIAL website of JIRA provides SDK. Without further ado, we can directly look at the code.

1 Introducing Maven dependencies

<dependency>
     <groupId>com.atlassian.jira</groupId>
     <artifactId>jira-rest-java-client-core</artifactId>
     <version>5.1.6</version>
</dependency>
<dependency>
     <groupId>io.atlassian.fugue</groupId>
    <artifactId>fugue</artifactId>
    <version>4.7.2</version>
</dependency>
Copy the code

2 Obtain the Jira client

/**
 * 获取 JiraRestClient
 *
 * @paramUrl Your JIRA system url *@paramUser Jira User name *@paramPassword Jira password *@return* /
public JiraRestClient getJiraRestClient(String url, String user, String password) {
    AsynchronousJiraRestClientFactory factory = new AsynchronousJiraRestClientFactory();
    JiraRestClient jiraRestClient = factory.createWithBasicHttpAuthentication(
            URI.create(url), user, password
    );
    return jiraRestClient;
}
Copy the code

3 Get the Jira Issue

public Issue getIssue(String issueKey) {
    IssueRestClient issueClient = getJiraRestClient().getIssueClient();
    return issueClient.getIssue(issueKey).claim();
}
Copy the code

4 Change the JIRA Issue status

/** * Change jira issue status **@paramIssue jira issue, which can be obtained by getIssue method above@paramStatus Indicates the status to be changed, for example, Done */
public void updateIssueStatus(Issue issue, String status) {
    IssueRestClient client = getJiraRestClient().getIssueClient();

    Iterable<Transition> transitions = client.getTransitions(issue).claim();

    for (Transition t : transitions) {
        if (t.getName().equals(status) {
            TransitionInput input = new TransitionInput(t.getId());
            client.transition(issue, input).claim();

            return; }}}Copy the code