Jira Api对接:缺陷上传附件和关联sprint

Posted leo_hou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jira Api对接:缺陷上传附件和关联sprint相关的知识,希望对你有一定的参考价值。

    缺陷创建完成后,首先为了表述缺陷现象或者初步排查结果我们一般会上传一些附件;其次我们会把缺陷关联到sprint方便晨会时快速查看前一天遗留的问题。本文就针对这两个问题简述如何使用jira api
一、上传附件

public static void main(String[] args){
        Map<String, InputStream> attachments = new HashMap<String, InputStream>();
        try {
        //fileName是附件名称,filePath 是文件路径
            attachments.put("fileName", new FileInputStream(new File("filePath")));
            issueUploadAttachment(issueKey,attachments);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
/**
     * issue上传附件
     *
     * @param issueKey---创建缺陷成功后返回的key.
     * @param attachments
     * @throws Exception
     */
    public static void issueUploadAttachment(String issueKey, Map<String, InputStream> attachments) throws Exception {

        Map<String, String> headers = new HashMap<>();
       //添加鉴权
        headers.put("Authorization", "Basic xxxx");
        for (String key : attachments.keySet()) {
            //上传附件
            uploadAttachment("http://you jira address:port/rest/api/2/issue/" + issueKey + "/attachments", headers, attachments.get(key), key);
        }
    }
    /**
     * 上传附件,附件需要特殊处理下,重写http请求方法
     *
     * @param url
     * @param headers
     * @param inputStream
     * @param fileName
     * @throws Exception
     */
    public static void uploadAttachment(String url, Map<String, String> headers,
                                        InputStream inputStream, String fileName) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        try {
            HttpPost httppost = new HttpPost(url);
            for (String key : headers.keySet()) {
                httppost.setHeader(key, headers.get(key));
            }
            httppost.setHeader("X-Atlassian-Token", "no-check");
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.addBinaryBody("file", inputStream, ContentType.create("multipart/form-data", Consts.UTF_8),
                    fileName);
            HttpEntity reqEntity = builder.build();
            httppost.setEntity(reqEntity);
            response = httpclient.execute(httppost);
            if (response != null && response.getStatusLine() != null
                    && response.getStatusLine().getStatusCode() == 200) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    String responseEntityStr = EntityUtils.toString(response.getEntity());
                    JSONArray jsonArray = JSONArray.fromObject(responseEntityStr);
                    if (jsonArray != null && jsonArray.size() > 0) {
                        EntityUtils.consume(resEntity);
                        return;
                    }
                }
            }
            logger.error("issue uploadAttachments fail");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            response.close();
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

二、关联sprint

/**
     * 移动issues到sprint
     *
     * @param jiraSprintId    sprintId
     * @param issueKey   创建缺陷返回的key
     */
    public static void moveIssuesToSprint(String jiraSprintId, String issueKey) {

        JSONArray jsonArray = new JSONArray();
        jsonArray.add(issueKey);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("issues", jsonArray);
        //jsonObject 作为body请求 
        httpClient("post", "http://you jira address:port/rest/agile/1.0/sprint/" + jiraSprintId + "/issue", jsonObject.toString());
    }

 

 

  

更多文章请关注公众号

以上是关于Jira Api对接:缺陷上传附件和关联sprint的主要内容,如果未能解决你的问题,请参考以下文章

Jira Api对接:获取项目和sprint

(番外篇)Jira Api对接:jira-workflow中节点校验条件实践

将附件添加到 Jira 的 api

Jira Api对接:修改任务状态

PHP 项目对接视频号原来如此简单,小白也能轻松完成带附件

odoo 附件上传与下载