JIRA REST API -- JAVA

curl调用代码

package com.keegoo.jira;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;


/**
 * JIRA API 工具类
 *
 * @author ***
 *
 */
public class JiraAPIUtil {

    static String uri = "http://jira.***.***";
    static String user = "***";
    static String pwd = "***";
    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 {

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

        return result.toString();
    }

    /**
     * 活动工单信息
     *
     * @param issueKey
     *            工单key
     * @return
     * @throws IOException
     */
    public static String getIssue(String issueKey) throws IOException {

        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
     *            工单负责人
     * @param map
     *            工单参数map,key为参数名称,value为参数值,参数值必须自带双引号 比如: map.put("assignee",
     *            "{\"name\":\"username\"}"); map.put("summary",
     *            "\"summary00002\"");
     * @return
     * @throws IOException
     */
    public static String createIssue(String projectKey, String issueType,
                                     String description, String summary,
                                     Map<String, String> map) throws IOException {
        String fields = "";
        if (map != null && map.size() > 0) {
            StringBuffer fieldsB = new StringBuffer();
            for (Map.Entry<String, String> entry : 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);

        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
     * @param comment  注释说明
     * @return
     * @throws IOException
     */
    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;
    }

    public static void main(String[] args) throws IOException {
        // 通过key获取工单内容
//        JiraAPIUtil.getIssue("NQCP-126");

        Map<String, String> map = new HashMap<String, String>();
        map.put("customfield_10109","{\"name\":\"username\"}");
        JiraAPIUtil.createIssue("NQCP", "提测", "测试curl方式生成jira工单描述", "测试curl方式生成jira工单主题", map);

//         Map<String,String> map = new HashMap<String,String>();
//         map.put("assignee", "{\"name\":\"username\"}");
//         map.put("summary", "\"summary00002\"");
//         JiraAPIUtil.editIssue("NQCP-38", map);

//      JiraAPIUtil.searchIssues("assignee=username");
//      System.out.println("*****************************");
//      JiraAPIUtil.searchIssues("assignee=username+order+by+duedate");
//      System.out.println("*****************************");

//      JiraAPIUtil.addComments("MMQ-50", "测试jira调用生成备注");
//      JiraAPIUtil.deleteIssueByKey("NQCP-38");
//        JiraAPIUtil.addAttachment("NQCP-39", "d://myfile01.txt");  //linux路径:   /home/boss/myfile.txt
    }

}

运行打印

页面效果

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,930评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,853评论 18 139
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,196评论 4 61
  • 写作看似一个低门槛的艺术,谁都可以写。有张纸有支笔可以写;没有纸笔有电脑手机也能写;什么都没有也可以写,比如“出口...
    薛薛闲扯阅读 174评论 0 2
  • 午后,我坐在奶茶店的二楼,透过窗窥视着街上的来来往往。阳光照射在我的身上,我热的焦灼、烦躁。 我并没有选择推开窗,...
    后畏小生阅读 1,365评论 22 24