(三)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());
    }

图片

更多文章请关注公众号
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

测试步行人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值