如何用 JIRA REST API 创建 Issue
简介
最近需要把一个Excel里的issues list全部到JIRA上create 一遍, 总不能手动创建百十来个issues吧, 本文讲述一下如果调用JIRA提供的Rest API 来自动创建issues.
下面是官网的一个例子,用curl 来创建的。
Request
curl -D- -u fred:fred -X POST --data {see below} -H "Content-Type: application/json" http://localhost:/rest/api//issue/
Data
{
"fields": {
"project":
{
"key": "TEST"
},
"summary": "REST ye merry gentlemen.",
"description": "Creating of an issue using project keys and issue type names using the REST API",
"issuetype": {
"name": "Bug"
}
}
}
Response
{
"id":"39000",
"key":"TEST-101",
"self":"http://localhost:8090/rest/api/2/issue/39000"
}
下面我用Apache Http Client 写的Java 代码
public static String executePostRequest(String url, String postBody) {
ResponseHandler<String> handler = new BasicResponseHandler();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Authorization", basic_auth);
httpPost.setHeader("Content-type", "application/json");
StringEntity entity = new StringEntity(postBody, "UTF-8");
entity.setContentType("application/json;charset=UTF-8");
httpPost.setEntity(entity);
String responseBody = null;
HttpResponse response = null;
try
{
response = httpclient.execute(httpPost);
responseBody = handler.handleResponse(response);
} catch (IOException e)
{
// TODO Auto-generated catch block
System.out.println(e.getMessage() + " status code: " + response.getStatusLine().getStatusCode());
}
String ticketNum = null;
try
{
if (responseBody != null)
{
ticketNum = new JSONObject(responseBody).getString("key");
}
} catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Ticket Number: " + ticketNum);
return ticketNum;
}
##转载注明出处:http://www.cnblogs.com/wade-xu/p/6096902.html
读取Excel的值 然后遍历create issue
private static final String BASE_URL = "http://jira.test.com/rest/api/2/issue";
private static String basic_auth = "Basic ";
private static CloseableHttpClient httpclient = null; public static void main(String[] args)
{
String username = "wadexu";
String password = "xxxxx";
httpclient = HttpClients.createDefault(); basic_auth += Base64Util.encodeBase64String(username + ":" + password);
System.out.println("basic_auth: " + basic_auth); List<List<String>> recordList = ExcelReader.readFromExcel("C:/Users/wadexu/Desktop/issues.xlsx"); for(int i = 1; i < recordList.size(); i++) {
createIssueViaSheet(recordList.get(i));
}
} public static void createIssueViaSheet(List<String> list) { String postBody = "{\"fields\": { \"project\": { \"key\": \"" + list.get(7) + "\" }, "
+ "\"summary\": \"This attribute" + list.get(0) + "has a serialization issue\", "
+ "\"description\": \"Please fix this issue. \", "
+ "\"issuetype\": {\"name\": \"Defect\"}, "
+ "\"versions\": [{\"name\": \"1234\"}], \"components\": "
+ "[{\"name\": \"" + list.get(6) + "\"}], \"customfield_10030\": "
+ "[{\"value\": \"Internal Issue\"}], \"customfield_10001\": {\"value\": \"New\"}, \"customfield_10002\": "
+ "{\"value\": \"3-Medium\"}}}"; String issueNumber = createIssue(postBody); if (issueNumber != null && !"".equalsIgnoreCase(issueNumber)) {
addWatchers(issueNumber, "\"wadexu\"");
}
} public static String createIssue(String postBody) {
return executePostRequest(BASE_URL, postBody);
}
add watchers to JIRA issue is also a rest API provided by JIRA
public static void addWatchers(String issueNumber, String postBody) {
String url = BASE_URL + "/" + issueNumber + "/watchers";
executePostRequest(url, postBody);
}
My post body template as below:
{
"fields": {
"project": {"key": "ABC"},
"summary": "The attribute has a serialization issue",
"description": "Please fix this issue.",
"issuetype": {"name": "Defect"},
"versions": [{"name": "1234"}],
"components": [{"name": "ABC Service"}],
"customfield_11030": [{"value": "Internal Issue"}],
"customfield_10020": {"value": "New"},
"customfield_10002": {"value": "3-Medium"},
"customfield_11082": [{"value": "QA Landscape"}]
}
}
这些fields 要注意,有得是多选, 有得单选, 加不加[] 很容易出错导致400 bad request
##转载注明出处:http://www.cnblogs.com/wade-xu/p/6096902.html
参考文档:
https://developer.atlassian.com/jiradev/jira-apis/jira-rest-apis
感谢阅读,如果您觉得本文的内容对您的学习有所帮助,您可以点击右下方的推荐按钮,您的鼓励是我创作的动力。
如何用 JIRA REST API 创建 Issue的更多相关文章
- PowerShell调用jira rest api实现jira统计自动化
通过调用JIRA Rest web api实现统计自动化,首先进行登录模拟: $content = @{username='用户名';password='密码'} $JSON=$content|con ...
- PowerShell调用jira rest api实现对个人提交bug数的统计
通过PowerShell的invoke-webrequest和net.client联合实现个人指定项目jira提交数的统计,其中涉及到了JSON对象的提交,代码如下: $content = @{use ...
- soapUI系列之—-07 调用JIRA Rest API接口【例】
一.调用JIRA接口------实现过滤器搜索问题 1. 在SoapUI中新建 REST Project, 在URI 中输入登录接口的 url (任意一个 Rest 接口的 url 都可以): 2. ...
- 如何用Java8 Stream API找到心仪的女朋友
传统的的Java 集合操作是有些啰嗦的,当我们需要对结合元素进行过滤,排序等操作的时候,通常需要写好几行代码以及定义临时变量. 而Java8 Stream API 可以极大简化这一操作,代码行数少,且 ...
- 使用Azure REST API创建虚拟机
Hollis Yao, Shihao Rong 使用REST API创建虚拟机之前,首先要确保Azure订阅中已经建好了"云服务"和"存储账号".如果没有的话 ...
- 使用hbase的api创建表时出现的异常
/usr/lib/jvm/java-7-openjdk-amd64/bin/java -Didea.launcher.port=7538 -Didea.launcher.bin.path=/usr/l ...
- (译)iPhone: 用公开API创建带小数点的数字键盘 (OS 3.0, OS 4.0)
(译)iPhone: 用公开API创建带小数点的数字键盘 (OS 3.0, OS 4.0) 更新:ios4.1现在已经将这个做到SDK了.你可以设置键盘类型为UIKeyboardTypeDecimal ...
- JavaEE Tutorials (11) - 使用Criteria API创建查询
11.1Criteria和Metamodel API概述16811.2使用Metamodel API为实体类建模170 11.2.1使用元模型类17011.3使用Criteria API和Metamo ...
- 使用Win32 API创建不规则形状&带透明色的窗口
前一阵突然想起了9月份电面某公司实习时的二面题,大概就是说怎么用Win32 API实现一个透明的窗口,估计当时我的脑残答案肯定让面试官哭笑不得吧.所以本人决定好好研究下这个问题.经过一下午的摸索,基本 ...
随机推荐
- 各种报错各种坑 webpack让我在学习的过程中一度想要放弃
由于拓展部分不是必须的,只是可以增强用户体验,但是有些时候页面给分页预留的位置不够,这个时候我们就可以通过设置来除去这一部分 子分区由两种创建方法,一种是不定义每个子分区子分区的名字和路径由分区决定, ...
- jQuery判断一个字符串中是否包含一个字符串(一)
var key = 'java'; var str = "hello,javascript,welcome to my world"; if(key.indexOf(str)!=- ...
- web安全之sqlload_file()和into outfile()
load_file() 条件:要有file_priv权限 知道文件的绝对路径 能使用union 对web目录有读权限 如果过滤啦单引号,则可以将函数中的字符进行hex编码 步骤: 1.读/etc/in ...
- BeanUtils.copyProperties() 用法
BeanUtils提供对Java反射和自省API的包装.其主要目的是利用反射机制对JavaBean的属性进行处理.我们知道,一个JavaBean通常包含了大量的属性,很多情况下,对JavaBean的处 ...
- 关于C语言的问卷调查(补交)
你对自己的未来有什么规划?做了哪些准备?(还是处于比较迷茫的状态:我做的准备是吧自己对计算机的兴趣提起来!) 你认为什么是学习?学习有什么用?现在学习动力如何?为什么?(学习就是学自己不会的东西:增加 ...
- 表连接,如何先筛选再 join
想先筛选,再join ,语法如下 select * form tab1 left join tab2 on (tab1.size = tab2.size and tab2.name='AAA') 注 ...
- sql语句 decimal(18,0)什么意思
decimal(18,0)18是定点精度,0是小数位数.decimal(a,b)a指定指定小数点左边和右边可以存储的十进制数字的最大个数,最大精度38.b指定小数点右边可以存储的十进制数字的最大个数. ...
- 51nod 1228 序列求和 ( 1^k+2^k+3^k+...+n^k )
C为组合数,B为伯努利数 具体推到过程略 参考博客:http://blog.csdn.net/acdreamers/article/details/38929067# (我的式子和博客中的不一样,不过 ...
- mac快捷键
切换 1. 应用程序切换 command tab 2.应用程序中的窗口间切换 command ~ ===================== 通用 1. 隐藏窗口 command H 2. 最小化窗口 ...
- etcd第一集
网站:https://github.com/coreos/etcd 一些观点:https://yq.aliyun.com/articles/11035 1.etcd是键值存储仓库,配置共享和服务发现2 ...