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();
}
}
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.

二、关联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());
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

(三)Jira Api对接:缺陷上传附件和关联sprint_workflow

(三)Jira Api对接:缺陷上传附件和关联sprint_公众号_02



QR Code
微信扫一扫,欢迎咨询~

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 155-2731-8020
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

手机不正确

公司不为空