25
Feb

Update JIRA Story status via HttpClient

package JIRAExample;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class updateStoryStatus {

	public static void main(String[] args) throws ClientProtocolException, IOException {
		myFunction("1163131", "5", "Basic c3Jhc3o6ZmF0aGVyaGFkYWRvbmtleQ==");

	}

	private static void myFunction(String issueID, String status, String base64AuthHeader) {
		String statusV = "\"" + status + "\"";

		try {

			/*
			 * Status values 5 for => Open to resolved 
			 * 				 2 for => Resolved to closed 
			 * 				 3 for => Closed to reopen
			 */
			
			// issueID:   1163131
			// base64AuthHeader :   Basic xxxxxxxxx 
			//						where xxxxxxxxx above is ntid:password encrypted with Base64 Algorithm

			HttpPost post = new HttpPost("https://jira-compozed.allstate.com/rest/api/2/issue/" + issueID + "/transitions");

			String jsonLoad = "{" + "\"transition\"" + ":" + "{" + "\"id\"" + ":" + statusV + "}" + "}";
			post.setEntity(new StringEntity(jsonLoad));

			// add request headers
			post.addHeader("Content-Type", "application/json");
			post.addHeader("Authorization", base64AuthHeader);
			post.addHeader("X-Atlassian-Token", "nocheck");

			try (CloseableHttpClient httpClient = HttpClients.createDefault();
					CloseableHttpResponse response = httpClient.execute(post)) {
				if(response.getStatusLine().getStatusCode()==204) {
					System.out.println("Success!!");
				}
				System.out.println(response.getStatusLine().getStatusCode());
				System.out.println(response.getStatusLine().getReasonPhrase()); // OK
				System.out.println(response.getStatusLine().toString());
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}