简介

最近需要把一个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的更多相关文章

  1. PowerShell调用jira rest api实现jira统计自动化

    通过调用JIRA Rest web api实现统计自动化,首先进行登录模拟: $content = @{username='用户名';password='密码'} $JSON=$content|con ...

  2. PowerShell调用jira rest api实现对个人提交bug数的统计

    通过PowerShell的invoke-webrequest和net.client联合实现个人指定项目jira提交数的统计,其中涉及到了JSON对象的提交,代码如下: $content = @{use ...

  3. soapUI系列之—-07 调用JIRA Rest API接口【例】

    一.调用JIRA接口------实现过滤器搜索问题 1. 在SoapUI中新建 REST Project, 在URI 中输入登录接口的 url (任意一个 Rest 接口的 url 都可以): 2. ...

  4. 如何用Java8 Stream API找到心仪的女朋友

    传统的的Java 集合操作是有些啰嗦的,当我们需要对结合元素进行过滤,排序等操作的时候,通常需要写好几行代码以及定义临时变量. 而Java8 Stream API 可以极大简化这一操作,代码行数少,且 ...

  5. 使用Azure REST API创建虚拟机

    Hollis Yao, Shihao Rong  使用REST API创建虚拟机之前,首先要确保Azure订阅中已经建好了"云服务"和"存储账号".如果没有的话 ...

  6. 使用hbase的api创建表时出现的异常

    /usr/lib/jvm/java-7-openjdk-amd64/bin/java -Didea.launcher.port=7538 -Didea.launcher.bin.path=/usr/l ...

  7. (译)iPhone: 用公开API创建带小数点的数字键盘 (OS 3.0, OS 4.0)

    (译)iPhone: 用公开API创建带小数点的数字键盘 (OS 3.0, OS 4.0) 更新:ios4.1现在已经将这个做到SDK了.你可以设置键盘类型为UIKeyboardTypeDecimal ...

  8. JavaEE Tutorials (11) - 使用Criteria API创建查询

    11.1Criteria和Metamodel API概述16811.2使用Metamodel API为实体类建模170 11.2.1使用元模型类17011.3使用Criteria API和Metamo ...

  9. 使用Win32 API创建不规则形状&带透明色的窗口

    前一阵突然想起了9月份电面某公司实习时的二面题,大概就是说怎么用Win32 API实现一个透明的窗口,估计当时我的脑残答案肯定让面试官哭笑不得吧.所以本人决定好好研究下这个问题.经过一下午的摸索,基本 ...

随机推荐

  1. TextRank 自动文摘

    前不久做了有关自动文摘的学习,采用方法是TextRank算法,整理和大家分享. 一. 关于自动文摘 利用计算机将大量的文本进行处理,产生简洁.精炼内容的过程就是文本摘要,人们可通过阅读摘要来把握文本主 ...

  2. 浏览器功能记住账号和密码解决方法(HTML解决方式)

    1.在input标签里应用html5的新特性autocomplete="off"  注:对chrome不管用.其他浏览器没试. 2.如果是一个输入框那就在当前input标签后面(一 ...

  3. NSLog 自定义 屏蔽

    1.如何自定义NSLog呢? 直接在工程的XXX_Prefix.pch中加入以下语句(就相当于在全局中定义了)#define NSLog NSLog(@"#%s##%d#",str ...

  4. MIT JOS学习笔记03:kernel 02(2016.11.08)

    未经许可谢绝以任何形式对本文内容进行转载! 本篇接着上一篇对kernel的分析. (5)pte_t * pgdir_walk(pde_t *pgdir, const void *va, int cre ...

  5. 开发adobe ane分享

    最近的项目使用adobe air开发,不可能避免的要使用到ane,项目初期的时候,使用了网上搜索到的了一些开源ane,最后发现,很多都不完善,要么版本太久,要么BUG很多,无人维护,所以下决心自己开发 ...

  6. T24基础-基本命令(1)

    如果你不知道什么是T24,那这篇文章对你意义不大.如果你所在银行IT刚好就准备使用或已经使用T24作为银行核心系统,那我的文章对你会很有帮助. 1. LIST  这个语句相当于SQL里的“select ...

  7. Spell-DBC

    Spell.dbc 1  ID2  Attributes               属性3  AttributesEx             属性 4  AttributesExB         ...

  8. linux 目录定义

    /     根目录,存放系统命令和用户数据等(如果下面挂载点没有单独的分区,它们都将在根目录的分区中) /boot     boot loader 的静态链接文件,存放与Linux启动相关的程序/ho ...

  9. 0x00linux32位汇编初入--前期准备

    0x00汇编初入--前期准备 一.汇编工具 在linux平台下常用的编译器为as,连接器为ld,使用的文本编辑器为vim,汇编语法为att 以下是一些工具: addr2line 把地址转换为文件名和行 ...

  10. Perl/Nagios – Can’t locate utils.pm in @INC

    While trying to use a Nagios plugin I got an error saying that “Can’t locate utils.pm in @INC”. Foll ...