0

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

CCovey
  • 799
  • 1
  • 10
  • 17
mogoli
  • 2,153
  • 6
  • 26
  • 41

3 Answers3

1

Updating labels and summary with 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();
ynka
  • 1,457
  • 1
  • 11
  • 27
  • Hi @ynka, to do the same for dates ? I tried Joda Datetime and doesn't work... Any idea ? The API is not really useful, we have to guess some object types todo the updates... – user3774109 Jan 18 '19 at 12:20
0

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();
    }
}
mogoli
  • 2,153
  • 6
  • 26
  • 41
0

You can Edit Jira issue by writing your Json to OutputStream and check for the response code. If it is 401 then your jira issue will be successfully edited.

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. So convert it to byte array and then write to OutputStream.

Harshad Holkar
  • 511
  • 5
  • 16