还不了解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");
}
return issueSt;
}

  

这里创建工单信息的文件我存放在了我电脑E盘根目录下面,名字为data.json

data.json的内容如下

{
"fields": {
"summary": "test008",
"issuetype": {
"name": "Task"
},
"project": {
"key": "MIN"
},
"description": "测试008",
"assignee": {
"name": "jwg"
}
}
}

  

  1. summary:工单主题
  2. issuetype:问题类型,问题类型是jira项目中存在的类型
  3. project:工单所属项目,工单所属项目是Jira中已经创建的项目
  4. description:工单描述,一些描述信息
  5. assignee:工单负责人,这个工单的负责人是谁

注意:data.json格式必须为json格式

4、更新工单信息:

/**
* 更新工单
*
* @param issueKey
* 工单key
* @param map
* 工单参数map,key为参数名称,value为参数值,参数值必须自带双引号 比如: map.put("assignee",
* "{\"name\":\"username\"}"); map.put("summary",
* "\"summary00002\"");
* @return
* @throws IOException
*/
public static String editIssue(String issueKey, Map<String, String> map)
throws IOException { StringBuffer fieldsB = new StringBuffer();
for (Map.Entry<String, String> entry : 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); return issueSt;
}

  

5、查询工单信息:

/**
* 查询工单
* @param jql
* assignee=username
* assignee=username&startAt=2&maxResults=2
* assignee=username+order+by+duedate
* project=projectKey+order+by+duedate&fields=id,key
* @return
* @throws IOException
*/
public static String searchIssues(String jql) throws IOException{
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); return issueSt;
}

  

6、为工单增加注释:

/**
* 为工单增加注释说明
* @param issueKey 工单key *
*/
public static String addComments(String issueKey,String comments) throws IOException{
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); return issueSt;
}

  

7、删除工单:

/**
* 删除工单
* @param issueKey 工单key
* @return
* @throws IOException
*/
public static String deleteIssueByKey(String issueKey) throws IOException{
String command = "curl -D- -u " + user + ":" + pwd
+ " -X DELETE -H \"Content-Type: application/json\" \"" + uri
+ "/rest/api/2/issue/" + issueKey + "\""; String issueSt = executeShell(command); return issueSt;
}

  

8、上传附件:

/**
* 上传附件
* @param issueKey 工单key
* @param filepath 文件路径
* @return
* @throws IOException
*/
public static String addAttachment(String issueKey,String filepath) throws IOException{
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); return issueSt;
}

  

项目的完整代码如下:

 import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map; /**
* JIRA REST API 工具类
* https://developer.atlassian.com/jiradev/jira-apis/jira-rest-apis/jira-rest-api-tutorials
* https://docs.atlassian.com/jira/REST/7.0-SNAPSHOT/
* @author 用代码征服天下
*
*/
public class JiraTest { public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//获取工单信息,参数为问题名称
JiraAPIUtil.getIssue("OAM-5402"); JiraAPIUtil.createIssue("MIN", "Task", "jwg", "工单描述","工单主题");
//
// Map<String,String> map = new HashMap<String,String>();
// map.put("assignee", "{\"name\":\"username\"}");
// map.put("summary", "\"summary00002\"");
// JiraAPIUtil.editIssue("MIN-1", map); JiraAPIUtil.searchIssues("assignee=username");
System.out.println("*****************************");
JiraAPIUtil.searchIssues("assignee=username+order+by+duedate");
System.out.println("*****************************"); JiraAPIUtil.addComments("MIN-1", "123456哈哈哈哈");
JiraAPIUtil.deleteIssueByKey("NQCP-38");
JiraAPIUtil.addAttachment("NQCP-39", "d://myfile01.json"); //linux路径: /home/boss/myfile.txt
JiraAPIUtil.addComments("createmeta", "123456哈哈哈哈");
} static String uri = "http://localhost:8080"; //Jira服务器地址
static String user = "jwg";//Jira用户名
static String pwd = "jwg123456";//Jira密码
static String osname = System.getProperty("os.name").toLowerCase(); //获取操作系统名称 /**
* 执行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();
} /**
* 活动工单信息
*
* @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; } /**
* 创建工单
*
* @param projectKey
* 项目key
* @param issueType
* 工单类型 name
* @param description
* 工单描述
* @param summary
* 工单主题
// * @param assignee
* 工单负责人
* 工单参数map,key为参数名称,value为参数值,参数值必须自带双引号 比如: map.put("assignee",
* "{\"name\":\"username\"}"); map.put("summary",
* "\"summary00002\"");
* @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");
}
return issueSt;
} /**
* 更新工单
*
* @param issueKey
* 工单key
* @param map
* 工单参数map,key为参数名称,value为参数值,参数值必须自带双引号 比如: map.put("assignee",
* "{\"name\":\"username\"}"); map.put("summary",
* "\"summary00002\"");
* @return
* @throws IOException
*/
public static String editIssue(String issueKey, Map<String, String> map)
throws IOException { StringBuffer fieldsB = new StringBuffer();
for (Map.Entry<String, String> entry : 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); return issueSt;
} /**
* 查询工单
* @param jql
* assignee=username
* assignee=username&startAt=2&maxResults=2
* assignee=username+order+by+duedate
* project=projectKey+order+by+duedate&fields=id,key
* @return
* @throws IOException
*/
public static String searchIssues(String jql) throws IOException{
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); return issueSt;
} /**
* 为工单增加注释说明
* @param issueKey 工单key *
*/
public static String addComments(String issueKey,String comments) throws IOException{
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); return issueSt;
} /**
* 删除工单
* @param issueKey 工单key
* @return
* @throws IOException
*/
public static String deleteIssueByKey(String issueKey) throws IOException{
String command = "curl -D- -u " + user + ":" + pwd
+ " -X DELETE -H \"Content-Type: application/json\" \"" + uri
+ "/rest/api/2/issue/" + issueKey + "\""; String issueSt = executeShell(command); return issueSt;
} /**
* 上传附件
* @param issueKey 工单key
* @param filepath 文件路径
* @return
* @throws IOException
*/
public static String addAttachment(String issueKey,String filepath) throws IOException{
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); return issueSt;
} }

Jira完整代码

Java——Java连接Jira,创建、修改、删除工单信息的更多相关文章

  1. Java连接Jira,创建、修改、删除工单信息

    还不了解Jira是什么的同学可以看一下这篇文章:https://www.cnblogs.com/wgblog-code/p/11750767.html 本篇文章主要介绍如何使用Java操作Jira,包 ...

  2. java操作文件的创建、删除、遍历

    java操作文件的创建.删除.遍历: package test; import java.io.File; import java.io.IOException; import java.util.A ...

  3. oracle11g创建修改删除表

    oracle11g创建修改删除表 我的数据库名字: ORCL         密码:123456 1.模式 2.创建表 3.表约束 4.修改表 5.删除表 1.模式 set oracle_sid=OR ...

  4. Linux创建修改删除用户和组

    Linux 创建修改删除用户和组 介绍 在日常的维护过程中创建用户操作用的相对会多一些,但是在这个过程中涉及到的知识点就不单单就是useradd了,接下来就来详细了解账号管理的相关信息. 用户信息 先 ...

  5. MySQL进阶11--DDL数据库定义语言--库创建/修改/删除--表的创建/修改/删除/复制

    /*进阶 11 DDL 数据库定义语言 库和表的管理 一:库的管理:创建/修改/删除 二:表的管理:创建/修改/删除 创建: CREATE DATABASE [IF NOT EXISTS] 库名; 修 ...

  6. Linux基础学习-用户的创建修改删除

    用户添加修改删除 1 useradd添加用户 添加一个新用户hehe,指定uid为3000,家目录为/home/haha [root@qdlinux ~]# useradd -u 3000 -d /h ...

  7. ElasticSearch.net NEST批量创建修改删除索引完整示例

    本示例采用Elasticsearch+Nest 网上查了很多资料,发现用C#调用Elasticsearch搜索引擎的功能代码很分散,功能不完整,多半是非常简单的操作,没有成型的应用示例.比如新增或修改 ...

  8. SQL Server 创建 修改 删除数据表

    1. 图形界面方式操作数据表 (1)创建和修改数据表 列名中如果有两个以上单词时,最好用下划线连接,否则可能会给将来的查询维护带来不便.我们公司美国佬做的数据库就很烦,所有列名都有空格,一旦忘记用方括 ...

  9. Mysql创建修改删除-表

    创建表之前要链接到库  例如  库名为 student use student; 连接结束可以查看此库中所有表 show tables; 创建表 create table student( id in ...

随机推荐

  1. DVWA渗透笔记

    Command Injection Low <?php if( isset( $_POST[ 'Submit' ] ) ) { // Get input $target = $_REQUEST[ ...

  2. VXLAN 基础教程:在 Linux 上配置 VXLAN 网络

    上篇文章结尾提到 Linux 是支持 VXLAN 的,我们可以使用 Linux 搭建基于 VXLAN 的 overlay 网络,以此来加深对 VXLAN 的理解,毕竟光说不练假把式. 1. 点对点的 ...

  3. 【题解】POJ3041 Asteroids - 图论 - 二分图匹配

    声明:本博客所有题解都参照了网络资料或其他博客,仅为博主想加深理解而写,如有疑问欢迎与博主讨论✧。٩(ˊᗜˋ)و✧*。 POJ3041 Asteroids 题目描述 假如你现在正处在一个 \(N*N\ ...

  4. python列表简介

    什么是列表?如何使用列表?https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range 列表相关知识: ...

  5. mongoDB(一)——mongoDB安装部署和常用shell命令

    1.mongoDB简介 mongoDB 是由C++语言编写的,是一种分布式的面向文档存储的开源nosql数据库.nosql是Not Only SQL的缩写,是对不同于传统的关系型数据库的数据库管理系统 ...

  6. 基于Koa实现留言版demo

    学习node.koa,随手做了一个留言板demo. 基本功能如下: 未登录用户可以查看主题列表和主题内容. 用户注册和登录功能. 登录用户可以发表.修改.删除自己的主题. 登录用户主题列表下方有发表主 ...

  7. ThinkPHP框架初步掌握

    为了帮老师用ThinkSNS二次开发一个微博系统,专门花了几天学习ThinkPHP框架,现在将一些ThinkPHP入门知识作以记录. 首先声明: 本文不是完全教程,只是将开发中碰到的问题作以总结,如果 ...

  8. 以命令行界面启动 Ubuntu

    1. /etc/default/grub 将GRUB_CMDLINE_LINUX_DEFAULT一行中改为"quiet splash 3" 2. update-grub 3. 重启

  9. 抖音人脸识别Autojs脚本

    title: 抖音人脸识别Autojs脚本 用Autojs写的抖音人脸颜值检测脚本 ​ 疫情期间宅家久了,昨天闲着没事(好吧,有事情,但是我不想做) ,消费之火熊熊燃烧.一咬牙把Autojs入正了.我 ...

  10. 利用python画出SJF调度图

    最先发布在csdn.本人原创. https://blog.csdn.net/weixin_43906799/article/details/105510046 SJF算法: 最短作业优先(SJF)调度 ...