Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Create an issue in JIRA REST Java Client

Martin Zdarsky June 26, 2012

Please, allow creating issues in JRJC. REST API already has this capability.

If it is possible, let me know how it is done. I didn't get it.

thank you

17 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Jobin Kuruvilla [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 26, 2012
Martin Zdarsky June 26, 2012

I have seen that one. I'd prefer if Atlassian improved the JRJC instead

Cheers

2 votes
Sabelo Dlangamandla December 5, 2012

The newly released JRJC API supports adding a new issue, this is how I did it and hope it will help somebody else.

//populate issue fields
com.atlassian.jira.rest.client.domain.input.IssueInputBuilder issueBuilder = new IssueInputBuilder("ProjectKey", "IssueType");
issueBuilder.setDescription("issue description");
issueBuilder.setSummary("issue summary");

//create the client
BasicHttpAuthenticationHandler myAuthenticationHandler = new BasicHttpAuthenticationHandler("jira.username", "jira.password");
JerseyJiraRestClientFactory factory = new JerseyJiraRestClientFactory();
URI jiraServerUri = new URI("http//jira.yourdomain.com");
JiraRestClient restClient = factory.create(jiraServerUri, myAuthenticationHandler);

//create the issue
NullProgressMonitor pm = new NullProgressMonitor();
com.atlassian.jira.rest.client.domain.input.IssueInput issueInput = issueBuilder.build();
BasicIssue bIssue = restClient.getIssueClient().createIssue(issueInput, pm);

//get the newly created issue
com.atlassian.jira.rest.client.domain.Issue jIssue = restClient.getIssueClient().getIssue(bIssue.getKey(), pm);

Aarthy S December 11, 2012

Hi,

In which version of JIRA does creating an issue this way work for you? I get the below error with JIRA 4.4.3

com.atlassian.jira.rest.client.RestClientException: com.sun.jersey.api.client.UniformInterfaceException: Client response status: 405

Thanks in advance.

Sabelo Dlangamandla December 11, 2012

Hi, I am using JIRA 5.2, but according to the 'release notes' for JRJC v1.0 this functionality on JIRA v4.2 - 5.1.8. is supported. As a trouble-shooting question: are you able to retrieve an existing issue when using the 'JiraRestClient' object without getting this exception? E.g. com.atlassian.jira.rest.client.domain.Issue jIssue = restClient.getIssueClient().getIssue("Existing issue key", pm);

Aarthy S December 11, 2012

Yes, I can get an existing issue using the JiraRestClient without any exception. I can get a project's details as well. Only the issue creation throws exception. I saw the supported versions too, but not sure if all features are supported across all versions.

I tried adding a comment to an existing issue and got the same exception.

Thanks.

Sabelo Dlangamandla December 11, 2012

Am afraid I cant help any further since in my case it worked for both 'comment' and 'issue', I would assume you have tried 'curl' and 'Jersey Client' with success. If that is the case then we can assume its a bug within the JRJC that affects your JIRA version.

Aarthy S December 12, 2012

Ok, thanks for your help. I had no luck creating issue using the REST API with Jersey Client.. but the wiki clearly states that this is available only from JIRA version 5. (mine is 4.4.3)

I will post my question to the forum and see.

1 vote
sreram April 22, 2013

Hi am using Jira 5.2 and apart from the fields you have mentioned, mandatory fields include 1.Steps to reproduce, 2) Detection and discipline and 3) Defect severity. How to set these values when creating a new issue from JRJC

Sabelo Dlangamandla April 22, 2013

//If its a multi-select field on jira

issueBuilder.setFieldValue("customfield_1234", ComplexIssueInputFieldValue.with("value", "selected value"));

//If its a not a multi-select field, normal field

issueBuilder.setFieldValue("customfield_1234", "your value");

Note: 'customfield_1234' is the ID of the field name in Jira e.g. 'Steps to reproduce' might have and id of '1234'

I prefer that since the name of the field can be changed whereas the ID hardly changes unless u delete that field.

0 votes
sreram May 21, 2013

@sabza, ya i agree. It throws 400(Bad request) when username/password given wrong when connecting via JRJC and i can able to get the actual cause from the exception. My question is, when wrong uname/password given twice/thrice to connect JIRA via JRJC , then i cant connect to JIRA again from JRJC unless i login using GUI by giving captcha. Is there any alternative approach to deal this case programatically rather than using GUI of jira for login.??

Thanks,

sre

Sabelo Dlangamandla May 21, 2013

The is no other way you can deal with that in JRJC since the purpose of CAPTCHA is to prevent unwanted internet bots from accessing JIRA thereby causing catastrophe. If you dont want CAPTCHA you can disable it, for more information have a look at: https://confluence.atlassian.com/display/JIRA/Enabling+Public+Signup+and+CAPTCHA. I hope this brought some clarity to your question.

0 votes
sreram May 16, 2013

@sabza ya i tried that and it works. Could you please answer to my earlier post which deals with the wrong username/password problem.

0 votes
sreram May 15, 2013

@sabza as mentioned by you in your earlier post, for multiselect value issueBuilder.setFieldValue("id of the custom filed",ComplexIssueInputFieldValue.with(String key,object value)

if my values were A,B,C in multiselect and if i need to select A in the list my value will be "A" and what about the key ??

id of the custom field = customfiled_1234 (for ex)

value= "A"

key ? . Thanks in advance

Sabelo Dlangamandla May 15, 2013

My example looked like this: issueBuilder.setFieldValue("customfield_1234", ComplexIssueInputFieldValue.with("value", "selected value"));

So in your case it would be something like:issueBuilder.setFieldValue("customfield_1234", ComplexIssueInputFieldValue.with("value", "A"));

0 votes
sreram May 14, 2013

@sabza I solved that issue and now it is working fine. I have a quick question . If username/password given wrong twice/thrice in JIRA GUI it asks for captcha. But when connecting via JRJC if i intentionally give wrong username/password it throws UniformInterfaceException. If i again hardcode it to correct password/username, it throws the same exception unless i login via GUI and give captcha. How to solve this programatically ? is there any possible way to handle it ?

Sabelo Dlangamandla May 16, 2013

Am not sure what your really question is, but will respond based on UniformInterfaceException. Any request (e.g POST, GET, DELETE) to JIRA that does not result to a response with a status of say 200 (OK), meaning the request was not successful would be handled by UniformInterfaceException. Meaning whether you supplied a wrong password or sent a wrong query to JIRA it would be handled by UniformInterfaceException. Then you can dig deeper on the exceptions to get the actual cause.

0 votes
sreram May 13, 2013

ya i have included both 'jersey-client-1.5.jar' and 'jersey-apache-client-1.5.jar' .

Sabelo Dlangamandla May 13, 2013

Your problem still lies on the 'jersey-apache-client-?.jar' you can try to replace your 1.5 version with a newer version, say 1.10/11/... I have tested with that (1.10) and I don't have issues.

0 votes
sreram May 12, 2013

Okay. ya i have included jersey-apache-client 1.5.jar

0 votes
sreram May 12, 2013

I am getting the below exception while executing the create issue code. My jira is running in http://localhost:8080 . I hope i have included all the dependencies mentioned. am using Netbeans 7.3 IDE.

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/httpclient/auth/AuthScope
	at com.sun.jersey.client.apache.config.ApacheHttpClientState.setCredentials(ApacheHttpClientState.java:121)
	at com.atlassian.jira.rest.client.auth.BasicHttpAuthenticationHandler.configure(BasicHttpAuthenticationHandler.java:42)
	at com.atlassian.jira.rest.client.internal.jersey.JerseyJiraRestClient.<init>(JerseyJiraRestClient.java:65)
	at com.atlassian.jira.rest.client.internal.jersey.JerseyJiraRestClientFactory.create(JerseyJiraRestClientFactory.java:34)
	at my.jira.jira.main(jira.java:43)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.httpclient.auth.AuthScope
	at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
	... 5 more
Java Result: 1

Expecting your reply for this post and for the earlier one. thanks in advance

Sabelo Dlangamandla May 12, 2013

Check if the 'jersey-apache-client-v?.jar' is included, am currently using 'jersey-apache-client-1.10.jar' You also need to include 'jersey-client-v?.jar' am using 'jersey-client-1.5.jar'

0 votes
sreram May 12, 2013

@sabza Thanks . yes you are right. while passing (String,Long) where project key is of type string and how yo get the value of issuetype to denote it as Long. In the form it is like Bug,new feature as i mentioned earlier. If i want to denote the issue type as "Bug" what i need to do. sorry if it is silly question i want to clear the basic out of this.

will JRJC 1.0 supports JIRA 5.2.11 ? but when i go through the pom.xml i found version 5.1. But in your earlier post you mentioned you worked out the create issue in version 5.2.

Sabelo Dlangamandla May 12, 2013

To get the ID of the IssueType check my response to your question on this date ' Apr 23 at 09:01 AM' Instead of choosing 'Fields' choose 'IssueType' then on the issue type select the 'Edit' then you will see the ID on the URL. Alternatively you can get it through code:

Iterable<com.atlassian.jira.rest.client.domain.IssueType> allIssueTypes = restClient.getMetadataClient().getIssueTypes(pm);

for (com.atlassian.jira.rest.client.domain.IssueType jIssueType : allIssueTypes) {
if(  jIssueType.getName().equals("Bug"){                
           Long issueTypeID = jIssueType.getId();
           String issueTypeName = jIssueType.getName();
           String issueTypeDescription = jIssueType.getDescription();
     }
}

0 votes
sreram May 12, 2013

com.atlassian.jira.rest.client.domain.input.IssueInputBuilder issueBuilder = new IssueInputBuilder("ProjectKey", "IssueType");

it didn't accept (String,String) . It asks for (String,Long). But the template in JIRA is like project and issuetype like (Bug,new feature,etc) . then what will be the value of those issuetype . if so how to find it. Pls help on this

Sabelo Dlangamandla May 12, 2013

Yes, 'IssueInputBuilder' object does not support (String, String) constructor, pass the project key as string type and the issue type id as Long type. Alternatively you can create a BasicIssueType object and BasicProject object and pass them as parameters of the 'IssueInputBuilder' object. e.g. IssueInputBuilder iib = new IssueInputBuilder("CCZA", 1L)

0 votes
sreram April 22, 2013

sure.Okay thanks for your immediate response.

0 votes
sreram April 22, 2013

@sabza Okay thanks :) 'll try it . may i know how to find the id of a particular field? eg the fields i mentioned earlier. and i more quick question, should i need to make remote API connection on in jira to access jira using JRJC programatically.. Thanks in advance

Sabelo Dlangamandla April 22, 2013

Log-in to Jira with administrative permissions, then go to 'Administration' on the top-right corner. Then go 'Issues-->Fields' a list of custom fields would be displayed then choose the one you want find its ID, then click 'Configure' on the gear image. Once that is done you can check the URL, the last number on the URL is the ID of the field. The URL will look something like: http://jira.atlassian.com/secure/admin/ConfigureCustomField!default.jspa?customFieldId=10500.

To answer your question about connecting, look on the lines below '//create the client' on the response above.

0 votes
Sabelo Dlangamandla February 3, 2013

Did you include the jar 'json-lib-2.4-jdk15.jar' available at http://sourceforge.net/projects/json-lib/files/latest/download?source=files

0 votes
spsivanandam February 2, 2013

Hi,

I am getting the below exception while using the above code.

Feb03,20132:15:25 PM com.sun.jersey.api.client.RequestWriter$RequestEntityWriterImpl<init> SEVERE: A message body writer forJavaclass org.codehaus.jettison.json.JSONObject, and Java type class org.codehaus.jettison.json.JSONObject, and MIME media type application/octet-stream was not found Feb03,20132:15:25 PM com.sun.jersey.api.client.RequestWriter$RequestEntityWriterImpl<init> SEVERE:The registered message body writers compatible with the MIME media type are: application/octet-stream ->
Can u please help on this?
Sabelo Dlangamandla February 3, 2013

Did you include the jar 'json-lib-2.4-jdk15.jar' available at http://sourceforge.net/projects/json-lib/files/latest/download?source=files

sajeev May 28, 2015

I too had this issue. I have added json-lib-2.4-jdk15.jar in the build path. Still the exception is throwing from the same code segment. "final Issue issue = restClient.getIssueClient().getIssue("RAD-1", pm);"

0 votes
Sabelo Dlangamandla December 11, 2012

According to the 'release notes' for JRJC v1.0 this functionality on JIRA v4.2 - 5.1.8. is supported. Check:https://marketplace.atlassian.com/plugins/com.atlassian.jira.jira-rest-java-client;jsessionid=1eu5ccv8t8lam1mbazqli3wpb9

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events