java 调用JIRA api接口

JIRA 是Atlassian 公司开发的一款问题跟踪管理软件工具 。

java 调用JIRA Api接口自动化缺陷的新增、状态处理通知,以下是java 调用接口的工具类

1、JiraAPIUtil 工具类

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://127.0.0.1:8080";
    static String user = "admin";
    static String pwd = "admin";
    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");
        JiraAPIUtil.searchIssues("assignee=yuqf");
        Map<String, String> map = new HashMap<String, String>();
        map.put("assignee","{\"name\":\"username\"}");
        JiraAPIUtil.createIssue("XISO", "故障", "test111", "test", map);
        JiraAPIUtil.searchIssues("assignee=username");

    }

}
JiraAPIUtil

2、OkHttpUtils 工具类

import java.io.IOException;

import org.apache.commons.codec.binary.Base64;
import org.json.JSONException;
import org.json.JSONObject;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.legion.monitor.modules.task.entity.dto.JiraDTO;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class OkHttpUtils {
    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    public static void main(String[] args) throws IOException, JSONException {

        String json ="{\"jql\": \"key=CTE-1\"}";
        String post = post("http://47.112.23.110:8080/rest/api/2/search", json,encodeCredentials());
        ObjectMapper om = new ObjectMapper();
        JsonNode readTree = om.readTree(post);
       JsonNode issues = readTree.get("issues");
        if (issues.isArray()) {
            for (JsonNode iss : issues) {
                JsonNode status = iss.get("fields").get("status").get("name");
                System.out.println(status.asText());
            }
       }

    }
    public static String get(String url) throws IOException {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(url)
                .addHeader("Content-type", "application/json").build();
        Response response = client.newCall(request).execute();
        return response.body().string();
    }

    public static String post(String url, String json, String auth) throws IOException {
        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder().url(url)
                .addHeader("Authorization", "Basic "+ auth)
                .addHeader("Content-type", "application/json").post(body).build();
        Response response = client.newCall(request).execute();
        return response.body().string();
    }
    
    private static String encodeCredentials() {
        byte[] credentials = ("admin" + ':' + "admin").getBytes();
        return new String(Base64.encodeBase64(credentials));
    }
}
View Code

 

posted @ 2021-01-08 16:30  刷鱼不刷牙  阅读(1978)  评论(0编辑  收藏  举报