JIRA REST java client API實際應用

來源:互聯網
上載者:User

標籤:指定   部分   artifact   pre   span   version   tco   java   函數   

[本文出自天外歸雲的部落格園]

前提

1、需要安裝maven環境;

2、在本地建立maven項目並修改maven設定檔“pom.xml”,添加如下內容:

<dependency>    <groupId>com.atlassian.jira</groupId>    <artifactId>jira-rest-java-client</artifactId>    <version>0.5-m6</version></dependency><dependency>    <groupId>com.atlassian.util.concurrent</groupId>    <artifactId>atlassian-util-concurrent</artifactId>    <version>3.0.0</version></dependency><!-- https://mvnrepository.com/artifact/org.sharegov/mjson --><dependency>    <groupId>org.sharegov</groupId>    <artifactId>mjson</artifactId>    <version>1.4.1</version></dependency>

以上三個依賴中,前兩個是用來和jira進行互動的類庫,最後一個是可以讓我能夠像在python中處理json一樣處理json的mjson類庫。

封裝方法

對JIRA的操作進行簡單封裝,樣本如下:

package com.mockCommon.util;import java.net.URI;import java.net.URISyntaxException;import java.util.ArrayList;import java.util.List;import org.joda.time.DateTime;import com.atlassian.jira.rest.client.JiraRestClient;import com.atlassian.jira.rest.client.NullProgressMonitor;import com.atlassian.jira.rest.client.domain.Comment;import com.atlassian.jira.rest.client.domain.Issue;import com.atlassian.jira.rest.client.internal.jersey.JerseyJiraRestClientFactory;import mjson.Json;public class JiraUtil {    /**     * 登入JIRA並返回指定的JiraRestClient對象     *      * @param username     * @param password     * @return     * @throws URISyntaxException     */    public static JiraRestClient login_jira(String username, String password) throws URISyntaxException {        final JerseyJiraRestClientFactory factory = new JerseyJiraRestClientFactory();        final URI jiraServerUri = new URI("http://jira.ms.netease.com");        final JiraRestClient restClient = factory.createWithBasicHttpAuthentication(jiraServerUri, username, password);        return restClient;    }    /**     * 擷取並返回指定的Issue對象     *      * @param issueNum     * @param username     * @param password     * @return     * @throws URISyntaxException     */    public static Issue get_issue(String issueNum, String username, String password) throws URISyntaxException {        final JiraRestClient restClient = login_jira(username, password);        final NullProgressMonitor pm = new NullProgressMonitor();        final Issue issue = restClient.getIssueClient().getIssue(issueNum, pm);        return issue;    }    /**     * 擷取指定JIRA備忘部分的內容     *      * @param issue     * @param username     * @param password     * @return     * @throws URISyntaxException     */    public static List<String> get_comments_body(Issue issue, String username, String password)            throws URISyntaxException {        List<String> comments = new ArrayList<String>();        for (Comment comment : issue.getComments()) {            comments.add(comment.getBody().toString());        }        return comments;    }    /**     * 擷取指定JIRA的建立時間     *      * @param issueNum     * @param username     * @param password     * @return     * @throws URISyntaxException     */    public static DateTime get_create_time(Issue issue, String username, String password) throws URISyntaxException {        return issue.getCreationDate();    }    /**     * 擷取指定JIRA的描述部分     *      * @param issueNum     * @param username     * @param password     * @return     * @throws URISyntaxException     */    public static String get_description(Issue issue, String username, String password) throws URISyntaxException {        return issue.getDescription();    }    /**     * 擷取指定JIRA的標題     *      * @param issueNum     * @param username     * @param password     * @return     * @throws URISyntaxException     */    public static String get_summary(Issue issue, String username, String password) throws URISyntaxException {        return issue.getSummary();    }    /**     * 擷取指定JIRA的報告人的displayName並返回String類型對象     *      * @param issueNum     * @param username     * @param password     * @return     * @throws URISyntaxException     */    public static String get_reporter(Issue issue, String username, String password) throws URISyntaxException {        return issue.getReporter().getDisplayName();    }    /**     * 擷取指定JIRA的指派人的displayName並返回String類型對象     *      * @param issue     * @param username     * @param password     * @return     * @throws URISyntaxException     */    public static String get_assignee(Issue issue, String username, String password) throws URISyntaxException {        Json json = Json.read(issue.getFieldByName("指派給").getValue().toString()).asJsonList().get(0);        return json.at("displayName").toString();    }    /**     * 測試函數     *      * @param args     * @throws URISyntaxException     */    public static void main(String[] args) throws URISyntaxException {        String username = "使用者名稱";        String password = "密碼";        String issueNum = "JIRA號";        final Issue issue = get_issue(issueNum, username, password);        List<String> jiraCommentsBody = get_comments_body(issue, username, password);        DateTime jiraCreateTime = get_create_time(issue, username, password);        String description = get_description(issue, username, password);        String summary = get_summary(issue, username, password);        String reporter = get_reporter(issue, username, password);        String assignee = get_assignee(issue, username, password);        System.out.println(assignee);    }}

以上代碼中在get_assignee()方法部分用到了mjson中的Json對象,也是主要用來處理json的對象。首先將返回結果(一個包含在list中的json對象)轉換為jsonList並取第一個元素,也就是其中包含的json對象。然後將json對象中的displayName屬性值取出來。

代碼中“使用者名稱”、“密碼”、“JIRA號”部分為需要替換的部分。

JIRA REST java client API實際應用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.