简体   繁体   English

编辑JIRA问题Java Rest

[英]Edit JIRA Issues Java Rest

I have tried to google this, but can't seem to get a good clear answer on this. 我试图用谷歌搜索,但是似乎无法对此给出一个很好的明确答案。

I'm trying to edit JIRA issues using Java via the JIRA REST API. 我正在尝试通过JIRA REST API使用Java编辑JIRA问题。

Can anyone provide a full example of editing a custom or standard field including library declarations? 谁能提供编辑自定义字段或标准字段(包括库声明)的完整示例? Completely new to REST and JIRA. REST和JIRA的全新内容。

I can't use plugins as I'll be working with multiple JIRA instances and I don't control the JIRA server I'm connecting to. 我无法使用插件,因为我将使用多个JIRA实例,并且无法控制要连接的JIRA服务器。

I found this: 我找到了这个:

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

But I don't understand it. 但是我不明白。

Thanks for your help :) 谢谢你的帮助 :)

Updating labels and summary with JJRC 4.0.0: 使用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();

Manged to find a way using the Jira REST and the Jersey Library. 设法找到一种使用Jira REST和Jersey图书馆的方法。 Currently, Jira's Java API supports reading and creating tickets but not editing. 目前,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();
    }
}

You can Edit Jira issue by writing your Json to OutputStream and check for the response code. 您可以通过将Json写入OutputStream并检查响应代码来编辑Jira问题。 If it is 401 then your jira issue will be successfully edited. 如果是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");
    }
}

you can't pass your Json directly to httpConnection. 您不能将您的Json直接传递给httpConnection。 So convert it to byte array and then write to OutputStream. 因此,将其转换为字节数组,然后写入OutputStream。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM