首页 > 代码库 > JIRA REST java client API实际应用
JIRA REST java client API实际应用
[本文出自天外归云的博客园]
前提
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个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。