Forums

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

Jira REST API - create issue from ScriptRunner

Rika Mandasari
Contributor
July 27, 2020

Hi, I have 2 Jira cloud instances;
https://progrezdev.atlassian.net and https://theelite.atlassian.net/

I want to create automation from https://progrezdev.atlassian.net through ScriptRunner for Jira Cloud to automatically create issue at https://theelite.atlassian.net/ if there is a transition (post function) from progrezdev's workflow.

I followed this two references;
https://library.adaptavist.com/entity/using-external-apis-in-sr (cloud code)
and
https://scriptrunner-docs.connect.adaptavist.com/jiracloud/#_static_type_checking(create an issue part)

But it turns out doesn't work.

I have tried with postman, the API Key works on it and I'm able to use POST on both sites, so I guest there's no problem with the API Token and permissions on both jira sites. Also checked the project key and issue type, they do exist on the site. 

Please advise, thanks

 

Here's the script i tried on Script Runner Console and run as current user

import groovy.json.JsonOutput

final externalUrl = "https://theelite.atlassian.net/"

def projectKey = 'TIS'
def taskType = get('/rest/api/3/issuetype').asObject(List).body.find { it['Task'] == 'Task' } [10033]




post('/rest/api/3/issue')
.header('Content-Type', 'application/json')
.body(
[
fields: [
summary : 'Task Summary',
description: "Don't forget to do this!.",
project : [
key: projectKey
],
issuetype : [
id: taskType
]
]
])
.asString().body

def get(def hostUrl, def endpointAndQuery) {
def token = "---inserted my token---------"
get("$hostUrl$endpointAndQuery")
.header('Accept', 'application/JSON')
.header('Authorization', "Bearer $token")
.asObject(Map)
.body
}

def post(def hostUrl, def endpointAndQuery, def bodyJson) {
post("$hostUrl$endpointAndQuery")
.header('Accept', 'application/JSON')
.header('x-api-key', '---inserted my token---------')
.basicAuth('----my email---', '---my password---')
.body(bodyJson)
.asObject(Map)
.body
}

def put(def hostUrl, def endpointAndQuery, def bodyJson) {
put("$hostUrl$endpointAndQuery")
.header('Accept', 'application/JSON')
.header('Authorization', 'Basic ' + '----my email---:---my password---'.bytes.encodeBase64().toString())
.body(bodyJson)
.asObject(Map)
.body
}

 

The result

 

java.lang.NullPointerException: Cannot invoke method getAt() on null object 
at Script1.run(Script1.groovy:6)
at Script1$run.call(Unknown Source)
at Script1$run.call(Unknown Source)
at com.adaptavist.sr.cloud.workflow.AbstractScript.evaluate(AbstractScript.groovy:39)
at com.adaptavist.sr.cloud.events.ScriptExecution.run(ScriptExecution.groovy:29)
at ConsoleScriptExecution1_groovyProxy.run(Unknown Source) 

 

The Logs

 

Serializing object into 'interface java.util.List'
GET /rest/api/3/issuetype asObject Request Duration: 1119ms
Cannot invoke method getAt() on null object on line 6
Class: com.adaptavist.sr.cloud.events.ConsoleScriptExecution, Config: [:]

 

1 answer

1 accepted

0 votes
Answer accepted
Kristian Walker _Adaptavist_
Community Champion
July 28, 2020

Hi Rika,

Thank you for your question.

I notice you have submitted the same question through our ScriptRunner for Jira Cloud support portal and we will continue to assist yourself on the support portal.

However, I can confirm that ScriptRunner for Jira Cloud uses the UniRest library for running Rest API Calls and you should use this structure for creating an issue.

I can confirm that we have an example located inside of the documentation page here which shows the structure that should be used to create an issue and you should use.

As for creating an issue with another system then you should use basic authorisation to authenticate with the other Jira Cloud instance.

Regards,

Kristian

Rika Mandasari
Contributor
July 28, 2020

Thanks Kristian,

 

do you mean this?

.header('Authorization', 'Basic ' + '----my email---:---my password--'.bytes.encodeBase64().toString())
Kristian Walker _Adaptavist_
Community Champion
July 28, 2020

Hi RIka,

I can confirm in your support ticket, we have responded and given an example of how to basic authorization but also that approach there may work allowing you to provide your email and password/token.

Also you must use the create syntax that we have provided an example of as this is the correct syntax for creating an issue using UniRest.

We will respond with any further questions you have on your support ticket.

Regards,

Kristian

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD Trial
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events