Creating an Issue in JIRA from other applications, like custom built UI for instance, is a common behaviour. It is pretty easy because JIRA provides us with the SOAP or XML/RPC interfaces. SOAP is recommended as it supports more operations which you might be using along with the Create!
Creating a JIRA SOAP client is pretty easy and is detailed in my previous tutorial. I am going to assume that you have the client created and a sample test done to ensure you are connected to your JIRA.
The first step is to create a SOAP Session and get the authentication token by logging in using the SOAP method login. Again, please refer to the source code at the end of the tutorial or to my previous post on how to do that!
Before we proceed, let us have a quick look at the createIssue method in the SOAP API . As you can see, it accepts the token and a RemoteIssue object. The latter should be populated with all the necessary things needed for you issue, at-least all the mandatory fields! This is how we populate the RemoteIssue:
RemoteIssue issue = new RemoteIssue(); issue.setProject(PROJECT_KEY); issue.setType(ISSUE_TYPE_ID); issue.setSummary("Test Issue via J Tricks tutorial");
That should be the bare minimum unless you have some other fields set as mandatory. You can now invoke the createIssue method as follows:
RemoteIssue createdIssue = jiraSoapService.createIssue(authToken, issue); System.out.println("\tSuccessfully created issue " + createdIssue.getKey());
The method accepts the RemoteIssue object we just created and returns a RemoteIssue for further processing or verification. In real life, you might need to add a lot more to the issue like 'Fix for versions' and 'Components'. Here is how we do that:
// Add remote compoments - Should be an array of RemoteComponent RemoteComponent component = new RemoteComponent(); component.setId(COMPONENT_ID); issue.setComponents(new RemoteComponent[] { component }); // Add remote versions - Should be an array of RemoteVersion RemoteVersion version = new RemoteVersion(); version.setId(VERSION_ID); issue.setFixVersions(new RemoteVersion[] { version });
Remember, the COMPONENT_ID and VERSION_ID should be the database unique ids and not name. You can find this in the URL while you are editing them or just by hovering over the action link.
If you need to apply a security level for the issue, you need to use the createIssueWithSecurityLevel method where you need to pass an additional parameter for the securityLevel. May be one thing worth mentioning here! For the status, you shouldn't directly set it. Instead use the progressWorkflowAction method whcih we can look at at a later time. That is it! We have the code for creating an issue via SOAP using the standard JIRA fields. Custom fields are not hard but it is better to cover in a separate post. Let us just make this short and sweet ;) Download few examples with full source below. And don't forget to post your comments/feedback!
23 Comments
u203963
4/13/2011 03:48:15 am
I made the sopaclient according to your tutorial. I was successfull in making it, was able to build the jar file etc. BUT, your java classes in com.jtricks are mostly mainclasses (which are normally directly startable in eclipse). SOAP seems to be a WebService and there the jar file is not startable like java -jar xxxx.jar. HOW DO I RUN for example the login class?? My WSDL points to my local jira instance and i guess all the rest should be ok. I just don't know how to start it up!! Or is that only possible via RPC plugin? IF yes, why the main classes? Thanks
Reply
J-Tricks
4/13/2011 07:54:19 am
I am not sure if I understood the question correctly, but you can just run the main class from your Eclipse itself if you want. The classes do different things as explained in them - Create issue, update it etc etc.
Reply
u203963
4/13/2011 08:30:19 pm
Thanks a lot. That was exactly what i wanted to know. It was strange because Eclipse did not offer me the possibility of 'run as java application' and that's why i thought that it must be started somehow different. I will have to try again. Thanks a lot. Mario
Reply
Hi. After consuming lot's of your tutorials, i made a step forward in understanding how jira itself and soap client in special are working. I am able now to read most of the content via soap api BUT, in heavens sake, how do i make getvalue() with remoteFieldValue(). I am able to read all the names and id's of the fields (normal, not customized) from my JIRA Installation but havent find out how to get the values. Lets say i have 10 fields, i have the names and the id's. How do i get the values? I would be infinity thankfull of a little example.
Reply
J-Tricks
4/29/2011 05:12:15 am
RemoteIssue has a method getCustomFieldValues which can be used for it.
Reply
u203963
5/1/2011 09:49:36 pm
Thank you. But i have alread learned from your printAllCFs class how to read
Reply
J-Tricks
5/1/2011 09:58:22 pm
Normal fields are all accessible from RemoteIssue, isn't it?
Reply
u203963
5/2/2011 08:39:38 pm
Sorry, i am stupid!! remoteFieldValue.getValues()) gave me the idea that there must be something else than the getters of RemoteIssue.
Reply
J-Tricks
6/29/2011 09:06:02 pm
@Kranti,
Reply
Dixon
8/23/2011 09:21:33 am
I don't understand how to build this. Is it not possible to have a SOAP GUI app run and remotely connect to Jira? I can run it with ALL the jar's maven imports packaged in my runnable jar, but I don't want a 100 MB jar file. Is there any way around this? Is that the wsdl file?
Reply
J-Tricks
8/23/2011 10:36:52 am
@Dixon Download the attached source here and run mvn -Pbuildclient. You will get a client jar with dependencies, less than 3MB in size. That is all you need.
Reply
Eddie
3/9/2012 04:00:40 am
Hey everyone, how do i get the "authToken" part of "RemoteIssue createdIssue = jiraSoapService.createIssue(authToken, issue);" ?
Reply
J-Tricks
3/9/2012 04:22:31 am
Eddie, You can find it here: http://www.j-tricks.com/1/post/2010/08/jira-soap-client.html
Reply
5/20/2012 09:06:00 pm
You gave great points here. Thank you for your great article and I enjoyed reading it keep the good work.I hope can share with you together more wonderful blog,and looking forward to your next article.
Reply
Heat
5/30/2012 09:20:13 pm
Hi, some great articles here and sample code to get a beginner started!
Reply
J-Tricks
5/31/2012 01:46:10 am
Which version of JIRA is it? Labels used to be custom fields but are now a system field.
Reply
Heat
5/31/2012 01:53:59 am
Sorry I should have included version of Jira is 4.3.3, so I think the labels are now a system field, as they are not being returned as custom fields.
Reply
J-Tricks
5/31/2012 04:01:45 am
Unfortunately, yes. REST seems to be the only way! RemoteIssue doesn't expose the labels.
Reply
Aalap
9/26/2012 01:27:50 pm
I am trying to create a issue with a date in past and I tried using setCreated(cal) but it does not seem to set the past date for when the bug was created. Any ideas?
Reply
J-Tricks
9/26/2012 02:18:59 pm
Hi,
Reply
Achour
8/2/2013 04:48:37 am
try to create issue through SOAP Jira v 4.4.0 ,but I have this error
Reply
Your comment will be posted after it is approved.
Leave a Reply. |
AuthorJobin Kuruvilla - Works in Adaptavist as Head of DevOps Professional Services. Categories
All
Archives
October 2016
|