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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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 Nov 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.

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 Nov 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

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
Community showcase
Posted in Jira Software

Presenting the "Best of 2020" Jira Software roundup!

Catch up with Atlassian Product Managers in our 2020 Demo Den round-up! From Advanced Roadmaps to Code in Jira to Next-Gen Workflows, check out the videos below to help up-level your work in the new ...

7,184 views 8 28
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you