TESTEVERYTHING

Monday 20 May 2013

Create issue in JIRA using Http Client via REST API services/ from JAVA



JIRA is a well known Software-suite. In short, JIRA is a proprietary issue tracking product, maintained and developed by Atlassian. It is commonly used for bug tracking, issue tracking, and project management



The JIRA Remote API Calls


I was surprised how easy it was to get the Remote API up and running. Basically you just enable the ‘Accept remote API calls’ option in your General Configuration settings under the Administration tab. The moment you turn it on, the following URL should give you a nice response


Note: It supports only in JIRA >5.0.

Here in am using http client 4.1.2

Here is  the code:

import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.util.EntityUtils;

public class CreateIssue {

 public static void main(String[] args) throws Exception {
  CreateIssue.CreateIssueJira();
 }
public static void CreateIssueJira() throws ClientProtocolException,
   IOException {
  String host = "https://rajivkumarnandvani.atlassian.net/";
  // int port = 8080;
  String userName = "EnterUserName";
  String password = "EnterPassword";
  String ResponseData;
  DefaultHttpClient httpClient = new DefaultHttpClient();
  String jsonObj = "{"
    + "\"fields\": {"
    + "\"project\":"
    + "{"
    + "\"key\": \"DEMO\""
    + "},"
    + "\"summary\": \"REST ye merry gentlemen.\","
    + "\"description\": \"Creating of an issue using project keys and issue type names using the REST API\","
    + "\"issuetype\": {" + "\"name\": \"Bug\"" + "}" + "}" + "}";

  HttpHost targetHost = new HttpHost("rajivkumarnandvani.atlassian.net",
    -1, "https");

  httpClient.getCredentialsProvider().setCredentials(
    new AuthScope(targetHost.getHostName(), targetHost.getPort(),
      targetHost.getSchemeName()),
    new UsernamePasswordCredentials(userName, password));

  HttpPost httpPost = new HttpPost(
    "https://rajivkumarnandvani.atlassian.net/rest/api/2/issue/");
  StringEntity entity = new StringEntity(jsonObj);
  entity.setContentType("application/json");
  httpPost.setEntity(entity);

  // Create AuthCache instance
  AuthCache authCache = new BasicAuthCache();
  // Generate BASIC scheme object and add it to the local auth cache
  BasicScheme basicAuth = new BasicScheme();

  authCache.put(targetHost, basicAuth);

  // Add AuthCache to the execution context
  BasicHttpContext localcontext = new BasicHttpContext();
  localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);

  try {

   HttpResponse httpResponse = httpClient.execute(httpPost,
     localcontext);
   HttpEntity entitydata = httpResponse.getEntity();
   ResponseData = new String(EntityUtils.toByteArray(entitydata));
   System.out.println(ResponseData);
  } catch (ClientProtocolException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  httpClient.getConnectionManager().shutdown();
 }
}




Which one is right ?

Translate







Tweet