如何用JIRARESTAPI创建Issue

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

如何⽤JIRARESTAPI创建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:8090/rest/api/2/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;
}
读取Excel的值然后遍历create issue
private static final String BASE_URL = "/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
参考⽂档:
感谢阅读,如果您觉得本⽂的内容对您的学习有所帮助,您可以点击右下⽅的推荐按钮,您的⿎励是我创作的动⼒。

相关文档
最新文档