java访问jira接口_java 调用JIRA api接口

该博客展示了如何使用Java调用JIRA API来执行各种工单操作,包括获取工单信息、创建工单、更新工单、查询工单、添加注释、删除工单以及上传附件。示例代码详细解释了每个操作的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.util.HashMap;importjava.util.Map;/*** JIRA API 工具类

*

*@author***

**/

public classJiraAPIUtil {static String uri = "http://127.0.0.1:8080";static String user = "admin";static String pwd = "admin";static String osname = System.getProperty("os.name").toLowerCase();/*** 执行shell脚本

*

*@paramcommand

*@return*@throwsIOException*/

private static String executeShell(String command) throwsIOException {

StringBuffer result= newStringBuffer();

Process process= null;

InputStream is= null;

BufferedReader br= null;

String line= null;try{if (osname.indexOf("windows") >= 0) {

process= new ProcessBuilder("cmd.exe", "/c", command).start();

System.out.println("cmd.exe /c " + command); //安装Cygwin,使windows可以执行linux命令

} else{

process= new ProcessBuilder("/bin/sh", "-c", command).start();

System.out.println("/bin/sh -c " +command);

}

is=process.getInputStream();

br= new BufferedReader(new InputStreamReader(is, "UTF-8"));while ((line = br.readLine()) != null) {

System.out.println(line);

result.append(line);

}

}catch(Exception e) {//TODO Auto-generated catch block

e.printStackTrace();

}finally{

br.close();

process.destroy();

is.close();

}returnresult.toString();

}/*** 活动工单信息

*

*@paramissueKey

* 工单key

*@return*@throwsIOException*/

public static String getIssue(String issueKey) throwsIOException {

String command= "curl -D- -u " + user + ":" +pwd+ " -X GET -H \"Content-Type: application/json\" \"" +uri+ "/rest/api/2/issue/" + issueKey + "\"";

String issueSt=executeShell(command);returnissueSt;

}/*** 创建工单

*

*@paramprojectKey

* 项目key

*@paramissueType

* 工单类型 name

*@paramdescription

* 工单描述

*@paramsummary

* 工单主题

*@paramassignee

* 工单负责人

*@parammap

* 工单参数map,key为参数名称,value为参数值,参数值必须自带双引号 比如: map.put("assignee",

* "{\"name\":\"username\"}"); map.put("summary",

* "\"summary00002\"");

*@return*@throwsIOException*/

public staticString createIssue(String projectKey, String issueType,

String description, String summary,

Map map) throwsIOException {

String fields= "";if (map != null && map.size() > 0) {

StringBuffer fieldsB= newStringBuffer();for (Map.Entryentry : map.entrySet()) {

fieldsB.append(",\"").append(entry.getKey()).append("\":")

.append(entry.getValue());

}

fields=fieldsB.toString();

}

String command= "curl -D- -u " + user + ":" +pwd+ " -X POST --data '{\"fields\": {\"project\":{ \"key\": \""

+ projectKey + "\"},\"summary\": \"" +summary+ "\",\"description\": \"" +description+ "\",\"issuetype\": {\"name\": \"" + issueType + "\"}"

+ fields + "}}' -H \"Content-Type: application/json\" \"" +uri+ "/rest/api/2/issue/\"";

String issueSt=executeShell(command);returnissueSt;

}/*** 更新工单

*

*@paramissueKey

* 工单key

*@parammap

* 工单参数map,key为参数名称,value为参数值,参数值必须自带双引号 比如: map.put("assignee",

* "{\"name\":\"username\"}"); map.put("summary",

* "\"summary00002\"");

*@return*@throwsIOException*/

public static String editIssue(String issueKey, Mapmap)throwsIOException {

StringBuffer fieldsB= newStringBuffer();for (Map.Entryentry : map.entrySet()) {

fieldsB.append("\"").append(entry.getKey()).append("\":")

.append(entry.getValue()).append(",");

}

String fields=fieldsB.toString();

fields= fields.substring(0, fields.length() - 1);

String command= "curl -D- -u " + user + ":" +pwd+ " -X PUT --data '{\"fields\": { " +fields+ "}}' -H \"Content-Type: application/json\" \"" +uri+ "/rest/api/2/issue/" + issueKey + "\"";

String issueSt=executeShell(command);returnissueSt;

}/*** 查询工单

*@paramjql

* assignee=username

* assignee=username&startAt=2&maxResults=2

* assignee=username+order+by+duedate

* project=projectKey+order+by+duedate&fields=id,key

*@return*@throwsIOException*/

public static String searchIssues(String jql) throwsIOException{

String command= "curl -D- -u " + user + ":" +pwd+ " -X GET -H \"Content-Type: application/json\" \"" +uri+ "/rest/api/2/search?jql=" + jql + "\"";

String issueSt=executeShell(command);returnissueSt;

}/*** 为工单增加注释说明

*@paramissueKey 工单key

*@paramcomment 注释说明

*@return*@throwsIOException*/

public static String addComments(String issueKey,String comments) throwsIOException{

String command= "curl -D- -u " + user + ":" +pwd+ " -X PUT --data '{\"update\": { \"comment\": [ { \"add\": { \"body\":\""+comments+"\" } } ] }}' -H \"Content-Type: application/json\" \"" +uri+ "/rest/api/2/issue/" + issueKey + "\"";

String issueSt=executeShell(command);returnissueSt;

}/*** 删除工单

*@paramissueKey 工单key

*@return*@throwsIOException*/

public static String deleteIssueByKey(String issueKey) throwsIOException{

String command= "curl -D- -u " + user + ":" +pwd+ " -X DELETE -H \"Content-Type: application/json\" \"" +uri+ "/rest/api/2/issue/" + issueKey + "\"";

String issueSt=executeShell(command);returnissueSt;

}/*** 上传附件

*@paramissueKey 工单key

*@paramfilepath 文件路径

*@return*@throwsIOException*/

public static String addAttachment(String issueKey,String filepath) throwsIOException{

String command= "curl -D- -u " + user + ":" +pwd+ " -X POST -H \"X-Atlassian-Token: nocheck\" -F \"file=@"+filepath+"\" \"" +uri+ "/rest/api/2/issue/" + issueKey + "/attachments\"";

String issueSt=executeShell(command);returnissueSt;

}public static void main(String[] args) throwsIOException {//通过key获取工单内容

JiraAPIUtil.getIssue("NQCP-126");

JiraAPIUtil.searchIssues("assignee=yuqf");

Map map = new HashMap();

map.put("assignee","{\"name\":\"username\"}");

JiraAPIUtil.createIssue("XISO", "故障", "test111", "test", map);

JiraAPIUtil.searchIssues("assignee=username");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值