还不了解Jira是什么的同学可以看一下这篇文章:https://www.cnblogs.com/wgblog-code/p/11750767.html
本篇文章主要介绍如何使用Java操作Jira,包括获取连接,创建、修改、删除工单信息
1、获取Jira连接并执行请求:
/**
* 执行shell脚本
*
* @param command
* @return
* @throws IOException
*/
private static String executeShell(String command) throws IOException {
StringBuffer result = new StringBuffer();
Process process = null;
InputStream is = null;
BufferedReader br = null;
String line = null;
try {
//windows平台下用cmd发生请求
if (osname.indexOf("windows") >= 0) {
process = new ProcessBuilder("cmd.exe", "/c", command).start();
System.out.println("cmd.exe /c " + command); //安装Cygwin,使windows可以执行linux命令
} else {
//linux平台下执行请求
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();
}
//返回结果
return result.toString();
}
2、获取获得工单信息:
/**
* 活动工单信息
*
* @param issueKey
* 工单key
* @return
* @throws IOException
*/
public static String getIssue(String issueKey) throws IOException {
/**
* jira的请求格式:
* curl -u 用户名:密码 -X 请求类型 --data @文件的路径 -H "Content-Type: application/json" 请求路径
*
* 官方示例:curl -u admin:admin -X POST --data @data.txt -H "Content-Type: application/json" http://localhost:8080/jira/rest/api/2/issue/
*
* 注意:--data后面的 @符合一定不能少
*/
String command = "curl -D- -u " + user + ":" + pwd
+ " -X GET -H \"Content-Type: application/json\" \"" + uri
+ "/rest/api/2/issue/" + issueKey + "\"";
String issueSt = executeShell(command);
return issueSt;
}
3、创建工单信息:
/**
* 创建工单
*
* @param projectKey
* 项目key
* @param issueType
* 工单类型 name
* @param description
* 工单描述
* @param summary
* 工单主题
// * @param assignee
* 工单负责人
*
* @return
* @throws IOException
*/
public static String createIssue(String projectKey, String issueType,String user,
String description, String summary) throws IOException {
String command="curl -D- -u " + user + ":" + pwd
+ " -X POST --data @E:\\data.json -H \"Content-Type: application/json\" \"" + uri
+ "/rest/api/2/issue/\"";
String issueSt = executeShell(command);
if (!issueSt.contains("errorMessages")) {
System.out.println("success");
}else {
System.out.println("error")