如何用 JIRA REST API 创建 Issue

发布时间:2017-3-31 2:41:32 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"如何用 JIRA REST API 创建 Issue ",主要涉及到如何用 JIRA REST API 创建 Issue 方面的内容,对于如何用 JIRA REST API 创建 Issue 感兴趣的同学可以参考一下。

{   "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

感谢阅读,如果您觉得本文的内容对您的学习有所帮助,您可以点击右下方的推荐按钮,您的鼓励是我创作的动力。

上一篇:数据库备份定期删除程序的开发。
下一篇:java 成长之路

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。

好贷网好贷款