简介

最近需要把一个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. WIFI功率修改

    1. 修改文件: mediatek/custom/工程/cgen/cfgdefault/CFG_WIFI_Default.h 2. 修改内容: {0x26, 0x26            ----- ...

  2. .NET涉及的一些名词

    本文在最为概略的层次上对.NET涉及的一些名词进行解释, 包括: 通用语言基础架构(Common Language Infrastructure, CLI). 虚拟执行系统(Virtual Execu ...

  3. 记录一个UDP收包丢包的问题

    这几天写GB28181平台接入层代码,对收到的PS包进行解包时,总是出现误码,最终导致rtsp点播服务中画面花屏. 分析了码流抓包数据之后,发现网络上没有丢包,遂认为PS流解包代码有bug,于是埋头分 ...

  4. Java篇-File类之常用操作

    /** * */ package com.io.file; import java.io.File; import java.io.IOException; /** * <pre> * & ...

  5. LintCode Anagrams

    (记得import java.util.HashMap及Arrays, 首先字符串若为空或者数量为零, 则返回一个空的LinkedList) 1. 把string变为char数组, 再进行排序, 之后 ...

  6. JDBC查询指定条件的数据

    使用select语句的条件查询,需要用到where子句. package qddx.JDBC; import java.sql.*; public class QueryById { public b ...

  7. Android驱动开发前的准备(四)

    源代码的下载和编译 4.1 下载.编译和测试Android源代码 4.2下载和编译linux内核源代码 4.1.1 配置Android源代码下载环境 (1) 创建一个用于存放下载脚本文件的目录 # m ...

  8. C#循环语句练习2

    1.羽毛球拍15元,球3元,水2元,有200元,每一种至少买一件,问有几种买法? 2.洗发水15元,牙刷5元,香皂2元,有150元,每一种至少买一件,问有几种买法? 3.用100元钱买100只鸡,公鸡 ...

  9. xamarin学习之页面布局

    在android应中,需要注意3个文件,他们分别是:Main.axml,String.xml和Activity.cs. 1.布局文件Main.axml ,该文件保存在Resouses/layout的目 ...

  10. 在树莓派上部署InfoPi

    如果仅仅想试用InfoPi,请参照此文在Windows上试用.在Windows上部署比在树莓派上部署简单得多. 先说明一下,我用的系统是Raspbian(2014-06-20发布的). 用户pi,工作 ...