简介

最近需要把一个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. php判断json对象是否存在的方法

    在实际测试中php读取json数组时 使用简单的 if 或者 array_key_exists 去判断对象是否存在是会报错的,以下是google搜寻的正确判断方法 实际上出现报错只是我对php还不是很 ...

  2. Javaweb学习随笔_JSP的九大内置对象

    JSP内置对象整理 1. 九大内置对象: out,request,response,session,application,page,pageContext,config,Exception. 1.1 ...

  3. 点击input框,不让手机软键盘弹出的办法

    设置readonly="" <input type="text" readonly="" placeholder="请输入邮 ...

  4. Linux Pthread 深入解析(转-度娘818)

    Linux Pthread 深入解析   Outline - 1.线程特点 - 2.pthread创建 - 3.pthread终止         - 4.mutex互斥量使用框架         - ...

  5. 反射【类Class、成员变量Field、方法Method】

    Class<?> c = Class.forName("main.Main$MyClass"); //要包名+类名 Object obj = c.newInstance ...

  6. BZOJ平推计划

    学习VFK大神推BZOJ,记录一下学习的东西 1004: burnside:一个置换群的等价计数=(每个置换的置换后等价情况数)/置换总数,每个置换的置换后等价情况数就是置换后没变的数 模意义下的除法 ...

  7. Docker搭建Java Web运行环境

    1. 前提条件 安装了Docker的64位Linux 操作系统 Linux操作系统镜像 Linux版本的JDK压缩包 Linux版本的Tomcat压缩包 2. 启动容器 容器是在镜像的基础上来运行的, ...

  8. 更改localhost默认打开的index.html的地址三步曲

    首先说明,我的Apache安装路径是F:\software installing\Apache2.2 解释一下,localhost默认打开的是安装路径下index.html 也就是路径F:\softw ...

  9. rhel 5.8 and 6.4 yum配置

    rhel 5.8 and 6.4 yum配置 6.4 [Packages]name=Packagesenabled=1gpgcheck=0baseurl=file:///iso 5.8 [Packag ...

  10. 从css谈模块化

    模块化是现今我们随处都可以听到的一个名词,什么是模块化?为什么我们需要模块化?这是本系列文章我们要弄明白的一个问题.我们也借这部分内容,顺带回顾一下前端的发展历程. 说实话,模块化这个主题有点大,我一 ...