编辑JIRA问题Java Rest

时间:2015-03-01 01:15:09

标签: java rest jira

我试图谷歌这个,但似乎无法得到一个明确的答案。

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

任何人都可以提供编辑自定义或标准字段的完整示例,包括库声明吗?完全是REST和JIRA的新手。

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

我发现了这个:

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

但我不明白。

感谢您的帮助:)

3 个答案:

答案 0 :(得分:1)

使用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();

答案 1 :(得分:0)

管理以找到使用Jira REST和Jersey Library的方法。目前,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();
    }
}

答案 2 :(得分:0)

您可以通过将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。