mogoli mogoli - 4 months ago 42
Java Question

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.

Can anyone provide a full example of editing a custom or standard field including library declarations? Completely new to REST and 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.

I found this:

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 :)

Answer

Manged to find a way using the Jira REST and the Jersey Library. Currently, Jira's Java API supports reading and creating tickets but not editing.

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