除了常用的项目jar包之外,jira所需的3个核心jar包:jms-1.1.jar、jmxri.jar、jmxtools.jar
具体代码下载地址如:https://download.csdn.net/download/liqi_q/10304356
长用接口API如下:
jira_api_project http://tbjira.sinas.com/issue/rest/api/2/project
jira_api_versions http://tbjira.sinas.com/issue/rest/api/2/project/{key}/versions
jira_api_components http://tbjira.sinas.com/issue/rest/api/2/project/{key}/components
jira_api_products http://tbjira.sinas.com/issue/rest/mailruselectsrv/1.0/mailruselectsrv/getinfo?cfid=11700&project={key}
jira_api_newissue http://tbjira.sinas.com/issue/rest/api/2/issue
jira_api_issue_search http://tbjira.sinas.com/issue/rest/api/2/search
jira_api_editissue http://tbjira.sinas.com/issue/rest/api/2/issue/{key}
jira_api_getissueinfo http://tbjira.sinas.com/issue/rest/api/2/issue/{key}
jira_api_createmeta https://tbjira.sinas.com/issue/rest/api/2/issue/createmeta?projectKeys={key}&issuetypeNames=Bug&expand=projects.issuetypes.fields
jira_api_addcomment http://tbjira.sinas.com/issue/rest/api/2/issue/{key}/comment
jira_api_getcomment http://tbjira.sinas.com/issue/rest/api/2/issue/{key}/comment
jira_api_getissuelinktype http://tbjira.sinas.com/issue/rest/api/2/issueLinkType
jira_api_addissuelink http://tbjira.sinas.com/issue/rest/api/2/issueLink
jira_api_getbugstatus http://tbjira.sinas.com/issue/rest/api/2/issue/{key}/transitions
jira_api_updateissuestatus http://tbjira.sinas.com/issue/rest/api/2/issue/{key}/transitions
jira_api_deleteissuelink http://tbjira.sinas.com/issue/rest/api/2/issueLink/{key}
jira_api_remotelink http://tbjira.sinas.com/issue/rest/api/2/issue/{key}/remotelink
封装调用工具
package ltms.ws.tools;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.DatatypeConverter;
import ltms.bug.model.JiraBug;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
public class TestAir {
public static void main(String[] args) throws Exception {
// String requestUrl="http://tbjira.sinas.com:8081/issue/login.jsp";
// Map<String,String> loginMap = new HashMap<String,String>();
// loginMap.put("os_username", "username");
// loginMap.put("os_password", "passwd");
//
// String requestParma = JSON.toJSONString(loginMap);
// System.out.println(requestParma);
// String ss = sendPost2(requestUrl, requestParma);
// System.out.println(ss);
remoteLink();
}
public static void remoteLink() throws Exception{
String apiUrl = "http://tbjira.sinas.com:8081/issue/rest/api/2/issue/{key}/remotelink";
// 替换参数
apiUrl = apiUrl.replace("{key}", "TLAUTOTEST-467");
// 生成JSON格式参数
Map rootMap = new HashMap();
Map obj = new HashMap();
obj.put("url", "http://fanyi.bdstatic.com/static/translation/img/header/logo_cbfea26.png");
obj.put("title", "5555");
obj.put("summary", "asummay");
rootMap.put("object", obj);
String paramsStr = JSONUtils.toJSONString(rootMap);
String strResult =sendPost2(apiUrl, paramsStr);
com.alibaba.fastjson.JSONObject jsonObj = JSON.parseObject(strResult);
System.out.println( jsonObj.getString("id") +"-------"+ jsonObj.getString("self"));
}
public static String createIssues() throws Exception{
JiraBug jiraBug = new JiraBug();
jiraBug.setProjectName("TLAUTOTEST");
jiraBug.setJiraProject("TLAUTOTEST");
jiraBug.setSummary("5555555555555");
jiraBug.setDescription("5555555555");
jiraBug.setHwStatus("10148");
jiraBug.setComponent("10609");
jiraBug.setBugType("10123");
jiraBug.setBugRange("10125");
jiraBug.setTestType("10215");
jiraBug.setImportance("10103");
jiraBug.setVersion(15736);
jiraBug.setProducts("10313");
String nsssjons = newIssueToJson(jiraBug);
nsssjons = "{\"fields\":{\"summary\":\"阿斯蒂芬\",\"issuetype\":{\"name\":\"Bug\"},\"components\":[{\"id\":\"10609\"}],\"project\":{\"key\":\"TLAUTOTEST\"},\"description\":\"嗯嗯嗯\",\"customfield_10121\":{\"id\":\"10215\"},\"priority\":{\"name\":\"Highest\"},\"customfield_10100\":{\"id\":\"10100\"},\"customfield_10200\":{\"id\":\"10313\"},\"customfield_10102\":{\"id\":\"10123\"},\"customfield_10103\":{\"id\":\"10125\"},\"customfield_10107\":{\"id\":\"10148\"},\"versions\":[{\"id\":\"10215\"}]}}";
String rsults = sendPost2("http://tbjira.sinas.com:8081/issue/rest/api/2/issue",nsssjons);
System.out.println("添加bug任务返回结果:"+rsults);
return rsults;
}
//HttpRest Post方式请求接口
public static String sendPost2(String uri,String jsonObj) throws Exception{
String resStr = null;
int statusCode=0;
HttpClient htpClient = new HttpClient();
//PutMethod putMethod = new PutMethod(uri);
PostMethod postMethod = new PostMethod(uri);
try{
postMethod.addRequestHeader( "Content-Type","application/json" );
postMethod.getParams().setParameter( HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8");
// 获取验证用户信息
String strAuth = "username:passwd";
String encoding =DatatypeConverter.printBase64Binary(strAuth.getBytes("UTF-8"));
postMethod.addRequestHeader("Authorization", "Basic " +encoding);
postMethod.setRequestBody( jsonObj );
statusCode = htpClient.executeMethod( postMethod );
System.out.println(statusCode);
byte[] responseBody = postMethod.getResponseBody();
resStr = new String(responseBody,"UTF-8");
System.out.println(resStr);
if(statusCode != 201){
return null;
}
}catch(Exception e){
e.printStackTrace();
throw e;
}finally{
postMethod.releaseConnection();
}
return resStr;
}
//HttpRest Get方式请求接口
public static String sendGet(String url,String param) throws Exception{
String result = "";
BufferedReader in = null;
try {
// 获取验证用户信息
String strAuth = "username:passwd";
String encoding =DatatypeConverter.printBase64Binary(strAuth.getBytes("UTF-8"));
String urlNameString = url;
if (null!=param && param.length()>0){
urlNameString = url + "?" + param;
}
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.setRequestProperty("Authorization", "Basic " +encoding);
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
/*for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}*/
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream(),"utf-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
throw e;
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
//构建提交issue的json格式数据
public static String newIssueToJson(JiraBug jiraBug){
Map mapTemp= new HashMap();
Map mapRoot = new HashMap();
// fields 节点
mapRoot.put("fields", new HashMap());
// fields - project 节点 【项目,必选】
toMap(mapRoot.get("fields")).put("project", new HashMap());
// fields - project - key 节点
toMap(toMap(mapRoot.get("fields")).get("project")).put("key", jiraBug.getProjectName());//jiraBug.getJiraProject()
// fields - summary 节点
toMap(mapRoot.get("fields")).put("summary", jiraBug.getSummary());
// fields - issuetype 节点 【问题类型,必选】
toMap(mapRoot.get("fields")).put("issuetype", new HashMap());
// fields - issuetype - name 节点
toMap(toMap(mapRoot.get("fields")).get("issuetype")).put("name", "Bug"); // 默认值
// fields - issuetype 节点 【问题类型,必选】
toMap(mapRoot.get("fields")).put("priority", new HashMap());
// fields - issuetype - name 节点
toMap(toMap(mapRoot.get("fields")).get("priority")).put("name", "Highest");
toMap(mapRoot.get("fields")).put("customfield_10107", new HashMap());
toMap(toMap(mapRoot.get("fields")).get("customfield_10107")).put("id", jiraBug.getHwStatus());
// fields - customfield_10901 节点 【Test Type,必选】
toMap(mapRoot.get("fields")).put("customfield_10121", new HashMap());
toMap(toMap(mapRoot.get("fields")).get("customfield_10121")).put("id", jiraBug.getTestType());
toMap(mapRoot.get("fields")).put("customfield_10100", new HashMap());
toMap(toMap(mapRoot.get("fields")).get("customfield_10100")).put("id", jiraBug.getImportance());
toMap(mapRoot.get("fields")).put("customfield_10200", new HashMap());
toMap(toMap(mapRoot.get("fields")).get("customfield_10200")).put("id", jiraBug.getProducts());
toMap(mapRoot.get("fields")).put("customfield_10102", new HashMap());
toMap(toMap(mapRoot.get("fields")).get("customfield_10102")).put("id", jiraBug.getBugType());
toMap(mapRoot.get("fields")).put("customfield_10103", new HashMap());
toMap(toMap(mapRoot.get("fields")).get("customfield_10103")).put("id", jiraBug.getBugRange());
List lstComponents = new ArrayList();
lstComponents.add(new HashMap());
toMap(mapRoot.get("fields")).put("components", lstComponents);
toMap(toList(toMap(mapRoot.get("fields")).get("components")).get(0)).put("id", jiraBug.getComponent());
List versionList = new ArrayList();
versionList.add(new HashMap());
toMap(mapRoot.get("fields")).put("versions", versionList);
toMap(toList(toMap(mapRoot.get("fields")).get("versions")).get(0)).put("id", jiraBug.getVersion()+"");
// fields - labels 节点 【描述】
toMap(mapRoot.get("fields")).put("description", jiraBug.getDescription());
// 生成Json字符串
return JSON.toJSONString(mapRoot);
}
public static Map toMap(Object o){
return (Map)o;
}
public static List toList(Object o){
return (List)o;
}
}
实体类:
package com.cn.jira.entity; import java.util.Calendar; public class JiraBug implements java.io.Serializable { private String bugId; private String bugKey; private String projectName; private String jiraProject; private String hunter; private String affectsVersion; private Calendar created; private String component; private String bugRange; private String repetitionProbability; private String bugType; private String testType; private String importance; private String importanceType; private String hwStatus; private String ueImpactLevel; private String appVersion; private String coreVersion; private String caseId; private String city; private String products; private String needVerifyProducts; private String summary; private String description; private String ueas; private String comment; private String status; private String jfName; private String jfPath; public String getJfName() { return jfName; } public String getJfPath() { return jfPath; } public void setJfName(String jfName) { this.jfName = jfName; } public void setJfPath(String jfPath) { this.jfPath = jfPath; } /** * @return the bugKey */ public String getBugKey() { return bugKey; } /** * @param bugKey the bugKey to set */ public void setBugKey(String bugKey) { this.bugKey = bugKey; } public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } public String getBugId() { return bugId; } public void setBugId(String bugId) { this.bugId = bugId; } public String getProjectName() { return projectName; } public void setProjectName(String projectName) { this.projectName = projectName; } public String getJiraProject() { return jiraProject; } public void setJiraProject(String jiraProject) { this.jiraProject = jiraProject; } public String getHunter() { return hunter; } public void setHunter(String hunter) { this.hunter = hunter; } public String getAffectsVersion() { return affectsVersion; } public void setAffectsVersion(String affectsVersion) { this.affectsVersion = affectsVersion; } public Calendar getCreated() { return created; } public void setCreated(Calendar created) { this.created = created; } public String getComponent() { return component; } public void setComponent(String component) { this.component = component; } public String getBugRange() { return bugRange; } public void setBugRange(String bugRange) { this.bugRange = bugRange; } public String getRepetitionProbability() { return repetitionProbability; } public void setRepetitionProbability(String repetitionProbability) { this.repetitionProbability = repetitionProbability; } public String getBugType() { return bugType; } public void setBugType(String bugType) { this.bugType = bugType; } public String getTestType() { return testType; } public void setTestType(String testType) { this.testType = testType; } public String getImportance() { return importance; } public void setImportance(String importance) { this.importance = importance; } public String getImportanceType() { return importanceType; } public void setImportanceType(String importanceType) { this.importanceType = importanceType; } public String getHwStatus() { return hwStatus; } public void setHwStatus(String hwStatus) { this.hwStatus = hwStatus; } public String getUeImpactLevel() { return ueImpactLevel; } public void setUeImpactLevel(String ueImpactLevel) { this.ueImpactLevel = ueImpactLevel; } public String getAppVersion() { return appVersion; } public void setAppVersion(String appVersion) { this.appVersion = appVersion; } public String getCoreVersion() { return coreVersion; } public void setCoreVersion(String coreVersion) { this.coreVersion = coreVersion; } public String getCaseId() { return caseId; } public void setCaseId(String caseId) { this.caseId = caseId; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getProducts() { return products; } public void setProducts(String products) { this.products = products; } public String getNeedVerifyProducts() { return needVerifyProducts; } public void setNeedVerifyProducts(String needVerifyProducts) { this.needVerifyProducts = needVerifyProducts; } public String getSummary() { return summary; } public void setSummary(String summary) { this.summary = summary; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } /** * @return the ueas */ public String getUeas() { return ueas; } /** * @param ueas the ueas to set */ public void setUeas(String ueas) { this.ueas = ueas; } /** * @return the status */ public String getStatus() { return status; } /** * @param status the status to set */ public void setStatus(String status) { this.status = status; } }