I've just posted an issue by script runner. How to get the issue key in the issue ?

上原英知 November 11, 2020

I want to create the monthly running script that creates a monthly issue and subtasks of the issue. To create the subtasks, I think I have to retrieve the issue key from the issue. Could I get the issue key from the issue I've just posted to the project in Jira?

 

スクリーンショット (56).png

 

1 answer

1 accepted

1 vote
Answer accepted
Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 11, 2020

Hi @上原英知 ,

welcome to the Atlassian community!

You can get the issue key, you should be able to obtain it from the response of the post request.

See here.

Basically something like:

def resp = post("/rest/api/2/issue") 

...

String key = resp.body.key

But you should also check the response, if everything was ok during the request.

上原英知 November 11, 2020

Thanks for your reply!

I thought I could get the response by post() method-i.e.

'{"id":"10188",

"key":"ACCT-154",

"self":"https://japan-asset-management.atlassian.net/rest/api/2/issue/10188"}'

 

But I couldn't get the value of the key in the field by using the code - resp.properties.key or resp.body.key. What went wrong?

 

I tested the code below.

def projectKey = 'ACCT'
def taskType = get('/rest/api/2/issuetype').asObject(List).body.find { it['name'] == 'Task' }['id']

def resp = post('/rest/api/2/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

resp
assert resp.properties.key == "ACCT-154"

--------------------------------------------------------------------------------------------------

Result was below

org.codehaus.groovy.runtime.InvokerInvocationException: Assertion failed: assert resp.properties.key == "ACCT-154" | | | | | | | false | | null |

['blank':false,

'empty':false,

'class':class java.lang.String,

'bytes':[123, 34, 105, 100, 34, 58, 34, 49, 48, 49, 56, 56, 34, 44, 34, 107, 101, 121, 34, 58, 34, 65, 67, 67, 84, 45, 49, 53, 52, 34, 44, 34, 115, 101, 108, 102, 34, 58, 34, 104, 116, 116, 112, 115, 58, 47, 47, 106, 97, 112, 97, 110, 45, 97, 115, 115, 101, 116, 45, 109, 97, 110, 97, 103, 101, 109, 101, 110, 116, 46, 97, 116, 108, 97, 115, 115, 105, 97, 110, 46, 110, 101, 116, 47, 114, 101, 115, 116, 47, 97, 112, 105, 47, 50, 47, 105, 115, 115, 117, 101, 47, 49, 48, 49, 56, 56, 34, 125], 'latin1':true]

'{"id":"10188",

"key":"ACCT-154",

"self":"https://japan-asset-management.atlassian.net/rest/api/2/issue/10188"}' at ConsoleScriptExecution1_groovyProxy.run(Unknown Source) Caused by: Assertion failed:

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 11, 2020

Hi @上原英知 ,

I believe the difference is in the way, how the response is formatted.

In the example I've mentioned is:

def resp = post("/rest/api/2/issue")
.header("Content-Type", "application/json")
.body(...)
.asObject(Map)

But you are using:

 def resp = post('/rest/api/2/issue')
.header('Content-Type', 'application/json')
.body(...)
.asString().body

I don't know if there is any reason, why you want to be the response formatted as a string, but I think it would be much easier to work with objects. Then you should be able to obtain the key "using dots" like 

resp.body.key
上原英知 November 11, 2020

Thank you, it does work completely!

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events