JIRA Rest JAVA Client API实现问题管理及自定义字段(原创)
JIRA是一个缺陷跟踪管理系统,被广泛应用于缺陷跟踪、客户服务、需求收集、流程审批、任务跟踪、项目跟踪和敏捷管理等工作领域,当我们需要把第三方业务系统集成进来时,可以调用他的API。
JIRA本身的API非常强大,但它是一个底层的API体系,并不是一个易用的接口,如果要开发和拓展,所以需要我们二次包装。
jira官方为解决这个问题,推出了方便强大的java client library(目前只有java客户端库,没有.Net类库)
jira的Rest API 最新文档官网.
JIRA 6.4.12 REST API documentation
https://docs.atlassian.com/jira/REST/latest/
JIRA REST API Tutorials:
https://developer.atlassian.com/jiradev/jira-apis/jira-rest-apis/jira-rest-api-tutorials
如果是编写java桌面或web应用,jira提供了更方便的方式(Client类库),JIRA REST Java Client is a Java library (usable from any JVM language) which allows to easily talk to any JIRA 4.2+ instance using new (and still evolving) REST API.
JIRA Java Client library
https://ecosystem.atlassian.net/wiki/display/JRJC/Home
如果使用Client类库,可以方便应用各种现成的jira实体类(如项目、问题、备注、自定义字段......),不需要再重复造轮子,大幅提升效率。
首先,必须要了解JIRA api的接口结构,其中<resource-name>可以理解成api的方法,比如project,就是项目信息,user就是用户信息,issue就是问题信息....
http://hostname/rest/<api-name>/<api-version>/<resource-name>
/rest/
. Hence, if your JIRA site is running at:还先要搞清楚jira api的 认证体系,摘自官网:
the first step in using the JIRA REST API is to authenticate a user account with your JIRA site. For the purposes of this tutorial we will use HTTP BASIC Authentication, but any authentication that works against JIRA will work against the REST API. This includes:
- OAuth
- HTTP Cookies
- Trusted Applications
- os_username/os_password query parameters
为方便使用,我们采用Basic Auth
Basic Auth headers
If you need to you may construct and send basic auth headers yourself. To do this you need to perform the following steps:
- Build a string of the form username:password
- Base64 encode the string
- Supply an "Authorization" header with content "Basic " followed by the encoded string. For example, the string "fred:fred" encodes to "ZnJlZDpmcmVk" in base64, so you would make the request as follows.
一个curl的例子,注意红色字符串是对“username:password”的Base64编码
curl -D- -X GET -H "Authorization: Basic ZnJlZDpmcmVk" -H "Content-Type: application/json" "http://kelpie9:8081/rest/api/2/issue/QA-31"
JAVA Client类库实现的API DEMO
package jiraTEST; import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException; import org.apache.commons.codec.binary.Base32;
import org.joda.time.DateTime; import com.atlassian.jira.rest.client.IssueRestClient;
import com.atlassian.jira.rest.client.JiraRestClient;
import com.atlassian.jira.rest.client.SearchRestClient;
import com.atlassian.jira.rest.client.domain.BasicIssue;
import com.atlassian.jira.rest.client.domain.BasicProject;
import com.atlassian.jira.rest.client.domain.BasicUser;
import com.atlassian.jira.rest.client.domain.Comment;
import com.atlassian.jira.rest.client.domain.Field;
import com.atlassian.jira.rest.client.domain.Issue;
import com.atlassian.jira.rest.client.domain.Project;
import com.atlassian.jira.rest.client.domain.SearchResult;
import com.atlassian.jira.rest.client.domain.Transition;
import com.atlassian.jira.rest.client.domain.input.ComplexIssueInputFieldValue;
import com.atlassian.jira.rest.client.domain.input.FieldInput;
import com.atlassian.jira.rest.client.domain.input.IssueInput;
import com.atlassian.jira.rest.client.domain.input.IssueInputBuilder;
import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory;
import com.atlassian.util.concurrent.Promise;
import com.google.common.collect.Lists; public class CvteJiraDemo { public static String BaseURL = "http://jira-test:8080/";
public static String User = "admin";
public static String Password = "admin";
private static URI jiraServerUri = URI
.create("http://jira-test:8080/rest/api/2/");
private static boolean quiet = false;
private static final long BUG_TYPE_ID = 1L; // JIRA magic value
private static final long TASK_TYPE_ID = 3L; // JIRA magic value
private static final DateTime DUE_DATE = new DateTime();
private static final String PRIORITY = "Trivial";
private static final String DESCRIPTION = "description"; public static void main(String[] args) throws InterruptedException,
ExecutionException { final AsynchronousJiraRestClientFactory factory = new AsynchronousJiraRestClientFactory();
URI jiraServerUri;
try {
jiraServerUri = new URI(BaseURL);
final JiraRestClient restClient = (JiraRestClient) factory
.createWithBasicHttpAuthentication(jiraServerUri, User,
Password);
getAllProjects(restClient);
getProject(restClient, "DEMO");
getIssue(restClient, "FEEDBACK-14");
getIssueFields(restClient, "FEEDBACK-27");
addIssue(restClient, "FEEDBACK", "AAAAB");
addIssueComplex(restClient, "FEEDBACK",DUE_DATE.toString()); } catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally { }
} private static void println(Object o) {
if (!quiet) {
System.out.println(o);
}
} private static void parseArgs(String[] argsArray) throws URISyntaxException {
final List<String> args = Lists.newArrayList(argsArray);
if (args.contains("-q")) {
quiet = true;
args.remove(args.indexOf("-q"));
} if (!args.isEmpty()) {
jiraServerUri = new URI(args.get());
}
} private static Transition getTransitionByName(
Iterable<Transition> transitions, String transitionName) {
for (Transition transition : transitions) {
if (transition.getName().equals(transitionName)) {
return transition;
}
}
return null;
} // 得到所有项目信息
private static void getAllProjects(final JiraRestClient restClient)
throws InterruptedException, ExecutionException {
try { Promise<Iterable<BasicProject>> list = restClient
.getProjectClient().getAllProjects();
Iterable<BasicProject> a = list.get();
Iterator<BasicProject> it = a.iterator();
while (it.hasNext()) {
System.out.println(it.next());
} } finally {
}
} // 得到单一项目信息
private static void getProject(final JiraRestClient restClient,
String porjectKEY) throws InterruptedException, ExecutionException {
try { Project project = restClient.getProjectClient()
.getProject(porjectKEY).get();
System.out.println(project); } finally {
}
} // 得到单一问题信息
private static void getIssue(final JiraRestClient restClient,
String issueKEY) throws InterruptedException, ExecutionException {
try { Promise<Issue> list = restClient.getIssueClient()
.getIssue(issueKEY);
Issue issue = list.get();
System.out.println(issue); } finally {
}
} // 创建问题
public static BasicIssue createIssue(final JiraRestClient jiraRestClient,
IssueInput newIssue) {
BasicIssue basicIssue = jiraRestClient.getIssueClient()
.createIssue(newIssue).claim();
return basicIssue;
} //添加备注到问题
public static void addCommentToIssue(final JiraRestClient jiraRestClient,Issue issue, String comment) {
IssueRestClient issueClient = jiraRestClient.getIssueClient();
issueClient.addComment(issue.getCommentsUri(), Comment.valueOf(comment)).claim();
} //删除问题,目前找不到对应API
public static void deleteIssue(final JiraRestClient jiraRestClient, Issue issue) {
IssueRestClient issueClient = jiraRestClient.getIssueClient();
//issueClient.deleteIssue(issue.getKey(), false).claim();
} //通过标题获取问题
public static Iterable findIssuesByLabel(final JiraRestClient jiraRestClient, String label) {
SearchRestClient searchClient = jiraRestClient.getSearchClient();
String jql = "labels%3D"+label;
com.atlassian.jira.rest.client.domain.SearchResult results = ((SearchRestClient) jiraRestClient).searchJql(jql).claim();
return results.getIssues();
} //通过KEY获取问题
public static Issue findIssueByIssueKey(final JiraRestClient jiraRestClient, String issueKey) {
SearchRestClient searchClient = jiraRestClient.getSearchClient();
String jql = "issuekey = \"" + issueKey + "\"";
SearchResult results = searchClient.searchJql(jql).claim();
return (Issue) results.getIssues().iterator().next();
} // 创建问题 :仅有简单问题名称
private static void addIssue(final JiraRestClient restClient,
String porjectKEY, String issueName) throws InterruptedException,
ExecutionException {
try {
IssueInputBuilder builder = new IssueInputBuilder(porjectKEY,
TASK_TYPE_ID, issueName);
builder.setDescription("issue description");
final IssueInput input = builder.build(); try {
// create issue
final IssueRestClient client = restClient.getIssueClient();
final BasicIssue issue = client.createIssue(input).claim();
final Issue actual = client.getIssue(issue.getKey()).claim();
System.out.println(actual);
} finally {
if (restClient != null) {
// restClient.close();
}
} } finally {
}
} // 创建问题 :包含自定义字段
private static void addIssueComplex(final JiraRestClient restClient,
String porjectKEY, String issueName) throws InterruptedException,
ExecutionException {
try {
IssueInputBuilder builder = new IssueInputBuilder(porjectKEY,
TASK_TYPE_ID, issueName);
builder.setDescription("issue description");
// builder.setFieldValue("priority", ComplexIssueInputFieldValue.with("name", PRIORITY));
//单行文本
builder.setFieldValue("customfield_10042", "单行文本测试"); //单选字段
builder.setFieldValue("customfield_10043", ComplexIssueInputFieldValue.with("value", "一般")); //数值自定义字段
builder.setFieldValue("customfield_10044", 100.08); //用户选择自定义字段
builder.setFieldValue("customfield_10045", ComplexIssueInputFieldValue.with("name", "admin"));
//用户选择自定义字段(多选)
Map<String, Object> user1 = new HashMap<String, Object>();
user1.put("name", "admin");
Map<String, Object> user2 = new HashMap<String, Object>();
user2.put("name", "wangxn");
ArrayList peoples = new ArrayList();
peoples.add(user1);
peoples.add(user2);
builder.setFieldValue("customfield_10047", peoples); //设定父问题
Map<String, Object> parent = new HashMap<String, Object>();
parent.put("key", "FEEDBACK-25");
FieldInput parentField = new FieldInput("parent", new ComplexIssueInputFieldValue(parent));
builder.setFieldInput(parentField); final IssueInput input = builder.build();
try {
final IssueRestClient client = restClient.getIssueClient();
final BasicIssue issue = client.createIssue(input).claim();
final Issue actual = client.getIssue(issue.getKey()).claim();
System.out.println(actual);
} finally {
if (restClient != null) {
// restClient.close();
}
} } finally {
}
} //获取问题的所有字段
private static void getIssueFields(final JiraRestClient restClient,
String issueKEY) throws InterruptedException, ExecutionException {
try { Promise<Issue> list = restClient.getIssueClient()
.getIssue(issueKEY);
Issue issue = list.get();
Iterable<Field> fields = issue.getFields();
Iterator<Field> it = fields.iterator();
while (it.hasNext()) {
System.out.println(it.next());
} } finally {
}
} }
JIRA Rest JAVA Client API实现问题管理及自定义字段(原创)的更多相关文章
- JIRA REST java client API实际应用
[本文出自天外归云的博客园] 前提 1.需要安装maven环境: 2.在本地创建maven项目并修改maven配置文件“pom.xml”,添加如下内容: <dependency> < ...
- Memcached Java Client API详解
针对Memcached官方网站提供的java_memcached-release_2.0.1版本进行阅读分析,Memcached Java客户端lib库主要提供的调用类是SockIOPool和MemC ...
- ES系列十五、ES常用Java Client API
一.简介 1.先看ES的架构图 二.ES支持的客户端连接方式 1.REST API http请求,例如,浏览器请求get方法:利用Postman等工具发起REST请求:java 发起httpClien ...
- Java 9 揭秘(14. HTTP/2 Client API)
Tips 做一个终身学习的人. 在此章中,主要介绍以下内容: 什么是HTTP/2 Client API 如何创建HTTP客户端 如何使HTTP请求 如何接收HTTP响应 如何创建WebSocket的e ...
- Elasticsearch Java Rest Client API 整理总结 (一)——Document API
目录 引言 概述 High REST Client 起步 兼容性 Java Doc 地址 Maven 配置 依赖 初始化 文档 API Index API GET API Exists API Del ...
- Elasticsearch Java Rest Client API 整理总结 (二) —— SearchAPI
目录 引言 Search APIs Search API Search Request 可选参数 使用 SearchSourceBuilder 构建查询条件 指定排序 高亮请求 聚合请求 建议请求 R ...
- Elasticsearch Java Rest Client API 整理总结 (三)——Building Queries
目录 上篇回顾 Building Queries 匹配所有的查询 全文查询 Full Text Queries 什么是全文查询? Match 全文查询 API 列表 基于词项的查询 Term Term ...
- JAVA HDFS API Client 连接HA
如果Hadoop开启HA,那么用Java Client连接Hive的时候,需要指定一些额外的参数 package cn.itacst.hadoop.hdfs; import java.io.FileI ...
- Kafka 0.9+Zookeeper3.4.6集群搭建、配置,新Client API的使用要点,高可用性测试,以及各种坑 (转载)
Kafka 0.9版本对java client的api做出了较大调整,本文主要总结了Kafka 0.9在集群搭建.高可用性.新API方面的相关过程和细节,以及本人在安装调试过程中踩出的各种坑. 关于K ...
随机推荐
- 8.15 自定义tr行 滚动 信息行的滚动
<table class="zixun-con-table"> <tr class="hover"> <th style=&quo ...
- 记录如何用abd,用电脑输出手机操作信号
0.http://www.wmzhe.com/soft-39913.html 去这里下载最新版的adb.旧版本很多不好使.一定最新的. 1.用豌豆荚装好驱动 2.开启usb调试.具体方法可以百度到 3 ...
- 第五章:动词(Les verbes)
★及物动词(Les verbes transitifs) 主语发出的动作作用于人或物,它又分为两类,直接及物动词和间接及物动词. ()直接及物动词:动词直接带宾语,不需要介词引导.如: ...
- Radius 中 与Response Authernticator 与 Message-Authenticator的计算
/* String RequestStr3 = @"01 00 00 9E EB B2 E8 D9 1E 52 10 03 FB E1 52 39 27 58 93 F0 01 0E 33 ...
- MODIS产品分析和数据处理
ENVI+IDL 17种MODIS产品的功能解释 https://wenku.baidu.com/view/6fd329dcf524ccbff0218440.html ENVI读取MODIS数据大致步 ...
- Ubuntu14.04下安装Cuda8.0
https://blog.csdn.net/sinat_19628145/article/details/60475696 https://developer.nvidia.com/cuda-down ...
- day03(接口,多态)
接口: 概念:是功能的集合,可以当做引用数据类型的一种.比抽象类更加抽象. 接口的成员: 成员变量:必须使用final修饰 默认被 public &a ...
- (有点递归的感觉)RGCDQ--hdu--5317
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5317 感觉好神奇呀,一堆数在一起可以找到规律,学长讲完后,觉得自己是如此的so young f[x] ...
- Android AppCompat 需要 API 级别 11
为了兼容性使用 AppCompat 支持库.现在我试图override AppCompat 中的一些项来建立自己的Theme,在values文件下的style.xml里添加如下内容. <!-- ...
- 一个CTO谈自己的技术架构体系
理解算法背后的世界观 我比较擅长的领域可能是数据和架构两个方面吧. 大数据方面就是数据挖掘.数据分析等领域,我现在带着极光推送整个的数据团队,需要把握数据团队的技术方向,还需要做很多算法方面的研究. ...