Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Jira Issue creation using REST API in Script Runner

Fabio Manzoni
Contributor
July 18, 2018

Hi everyone, 

I`m trying to do a test integration of two jira instances using Script Runner.

On first instance the code (see below) during the transaction call an event and this event run the code on listener in scriptrunner.

This code its ok with green symbol and run. But anything  happend. I knew that this is a simple test,  but I don`t know what is my mistake.

Can someone help me ?

 

import groovy.json.StreamingJsonBuilder
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import java.text.SimpleDateFormat
import java.text.DateFormat
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.comments.CommentManager

def baseURL = "http://localhost:8081/rest/api/2/issue";  

// localhost:8081 it`s the target instance
def authString = "admin:admin".getBytes().encodeBase64().toString();
def body_req = [
"""
{ "fields":
{
"project":
{
"key": "PT"
},
"summary": "My Issue.",
"description": "Creating of an issue using project keys and issue type names using the REST API",
"issuetype":
{
"name": "Bug"
}
}

}
"""
]

URL url = new URL(baseURL);
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setRequestProperty( "Authorization", "Basic ${authString}" );
connection.requestMethod = "POST";
connection.doOutput = true;
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
connection.outputStream.withWriter("UTF-8") { new StreamingJsonBuilder(it, body_req) }
connection.connect();

 

2 answers

1 accepted

2 votes
Answer accepted
Fabio Manzoni
Contributor
July 19, 2018

Problem resolved,

I just forget the / after issue.

before

def baseURL = "http://localhost:8081/rest/api/2/issue";  

after

def baseURL = "http://localhost:8081/rest/api/2/issue/";  

Vineela Durbha
Contributor
April 19, 2019

Hi @Fabio Manzoni 

I have used your script to create an issue but it is not throwing any error or creating the issue. Can you help me why is it so?

Fabio Manzoni
Contributor
December 13, 2019

@Vineela Durbha

You could resolve this.. sorry for delay.

0 votes
Matthias Gaiser _K15t_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 18, 2018

Hi Fabio,

I can't tell you the exact solution, but maybe how you can get there. Why don't you integrate some logging into your script as described in the Scriptrunner docs? You could furthermore write a try-catch block around everything beneath your import statements and log the error (or act accordingly).

Having said that, I'd like to ask you what you are accomplishing to do - obviously you want to create an issue on another instance. But what is your further intent? Keeping the two issues in sync?

We are developing an issue sync app: Backbone Issue Sync for Jira.
Backbone allows you to simply create issues in another Jira as well as keeping them in sync afterwards. You can configure in a UI all the data you want to exchange (fields, issue types, workflow states, ...) and don't have to wrestle with groovy scripts.

If you think that is helpful to you, give it a try. And let us know via support@k15t.com if you need a demo or any other help.

Cheers,
Matthias

Fabio Manzoni
Contributor
July 19, 2018

Hi Matthias, 

Good idea about put logging into my script. The intention its just sync issues in both instances, but buy another app is not an option at this moment.

I'll continue with my tests.

Thanks, 

Fabio

Matthias Gaiser _K15t_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 19, 2018

Fine with me, Fabio.

I simply want to point you to possible solutions which might save you some time. You choose yourself your best option. :)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events