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");
}
}