最近須要把一個Excel裏的issues list所有到JIRA上create 一遍, 總不能手動建立百十來個issues吧, 本文講述一下若是調用JIRA提供的Rest API 來自動建立issues.html
下面是官網的一個例子,用curl 來建立的。json
curl -D- -u fred:fred -X POST --data {see below} -H "Content-Type: application/json" http://localhost:8090/rest/api/2/issue/
{ "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" } } }
{ "id":"39000", "key":"TEST-101", "self":"http://localhost:8090/rest/api/2/issue/39000" }
下面我用Apache Http Client 寫的Java 代碼api
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 app
讀取Excel的值 而後遍歷create issuecurl
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 JIRAide
public static void addWatchers(String issueNumber, String postBody) { String url = BASE_URL + "/" + issueNumber + "/watchers"; executePostRequest(url, postBody); }
My post body template as below:post
{ "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 this
https://developer.atlassian.com/jiradev/jira-apis/jira-rest-apisurl
感謝閱讀,若是您以爲本文的內容對您的學習有所幫助,您能夠點擊右下方的推薦按鈕,您的鼓勵是我創做的動力。