编辑 JIRA 问题 Java 休息

Edit JIRA Issues Java Rest

我已经尝试 google 这个,但似乎无法得到一个明确的答案。

我正在尝试通过 JIRA REST API 使用 Java 编辑 JIRA 问题 API。

谁能提供编辑自定义或标准字段(包括库声明)的完整示例?对 REST 和 JIRA 完全陌生。

我无法使用插件,因为我将使用多个 JIRA 实例,而且我无法控制我正在连接的 JIRA 服务器。

我发现了这个:

https://answers.atlassian.com/questions/127302/update-issue-with-jira-rest-java-client-2-0-0-m5

但是我看不懂

感谢您的帮助:)

设法找到一种使用 Jira REST 和 Jersey 库的方法。目前,Jira 的 Java API 支持读取和创建工单,但不支持编辑。

package jiraAlerting;

import javax.net.ssl.TrustManager;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;


public class restLab {

    WebResource webResource;
    static {
        //for localhost testing only
        javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
        new javax.net.ssl.HostnameVerifier(){

            public boolean verify(String hostname,
                    javax.net.ssl.SSLSession sslSession) {
                if (hostname.equals("your host here")) {
                    return true;
                }
                return false;
            }
        });
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        restLab rl = new restLab();

        //rl.connectToJiraViaRest();

        rl.editJiraTicket();
    }

    public void connectToJiraViaRest(){
        //System.setProperty("javax.net.ssl.trustStore", "C:/SSL/clientkeystore.jks");

        Client client = Client.create();
        client.addFilter(new HTTPBasicAuthFilter("username","password"));

        webResource = client.resource("https://host/jira/rest/api/2/issue/issueID");

    }

    public void editJiraTicket(){
        connectToJiraViaRest();

        ClientResponse response = webResource.type("application/json").put(ClientResponse.class,"{\"fields\":{\"customfield_11420\":{\"value\" :\"No\"}}}");
        //"{\"fields\":{\"customfield_11420\":\"Yes\"}}"
        response.close();
    }
}

您可以通过将 Json 写入 OutputStream 并检查响应代码来编辑 Jira 问题。如果是 401 那么你的 jira 问题将被成功编辑。

public String getAuthantication(String username, String password) {
    String auth = new String(Base64.encode(username + ":" + password));
    return auth;
}

public static HttpURLConnection urlConnection(URL url, String encoding) throws IOException {
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestProperty("Authorization", "Basic " + encoding);
    connection.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
    connection.setRequestProperty("Content-Type", "application/json");
    return connection;
}

public void updateJiraIssue(JiraCQCredential jiraCq) throws IOException {

    String summary = "Edit Jira Issue";
    String description = "Testing of Jira Edit";
    String assigne = "Any name who has jira account";
    String issueType = "Bug";

    String encodedAuthorizedUser = getAuthantication("pass your username", "pass your password");
    URL url = new URL("http://bmh1060149:8181/rest/api/2/issue/WFM-90");
    HttpURLConnection httpConnection = urlConnection(url, encodedAuthorizedUser);
    httpConnection.setRequestMethod("PUT");

    String jsonData = "{\"fields\" : {\"summary\":" + "\"" + summary + "\"" + ",\"description\": " + "\""
            + description + "\"" + " ," + "\"issuetype\": {\"name\": " + "\"" + issueType + "\""
            + " },\"assignee\": {\"name\":" + "\"" + assigne + "\"" + ",\"emailAddress\": \"abc@gmail.com\"}}}";

    byte[] outputBytes = jsonData.getBytes("UTF-8");

    httpConnection.setDoOutput(true);
    OutputStream out = httpConnection.getOutputStream();
    out.write(outputBytes);
    int responseCode = httpConnection.getResponseCode();
    if(responseCode == 401){
        System.out.println("Issue updated successfully with the mentioned fields");
    }
}

您不能将 Json 直接传递给 httpConnection。所以将其转换为字节数组,然后写入OutputStream。

使用 JJRC 4.0.0 更新标签和摘要:

JiraRestClientFactory factory = new AsynchronousJiraRestClientFactory();
URI uri = new URI(JIRA_URL);
client = factory.createWithBasicHttpAuthentication(uri, JIRA_ADMIN_USERNAME, JIRA_ADMIN_PASSWORD);
Map<String, FieldInput> map = new HashMap<>();
String[] labels = {"label1", "label2"};
map.put("labels", new FieldInput("labels", Arrays.asList(labels)));
map.put("summary", new FieldInput("summary", "issue summary"));
IssueInput newValues = new IssueInput(map);
client.getIssueClient().updateIssue("XX-1000", newValues).claim();