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

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

Scriptrunner - Sum Edited

Hi Everyone, 

 

I'm actually using the Scriptrunner app in a Jira Cloud Instance.

I was interested in one script : Summing the amount of Story Point in a Project with multiple condition. I found this script but it is about summing it for one user.

 

def SPRINT_ID = current()
// make search
def result = get('/rest/api/2/search')
.queryString('jql', "sprint = ${SPRINT_ID}")
.header('Content-Type', 'application/json')
.asObject(Map)
assert result.status == 200

// helper method for clarity
def sumOfOriginalEstimate = { fields ->
fields*.timeoriginalestimate.inject(0) { acc, estimate ->
acc + (estimate ?: 0) // default to 0
}
}

def issuesByAssignee = result.body.issues*.fields.groupBy { it.assignee?.name }

issuesByAssignee.collectEntries { k, v ->
[(k): sumOfOriginalEstimate(v)]
}

 

Furthermore, i have 5 errors : 2020-02-18_16h01_23.png2020-02-18_16h01_15.png2020-02-18_16h01_10.png2020-02-18_16h01_04.png2020-02-18_16h00_57.png

 

My second question is : Can i share it in a field in confluence ? 

 

Thanks in advance

1 answer

1 accepted

0 votes
Answer accepted

Hi Sebastien,

Thank you for your question.

I am unable to comment on the code provided above but can confirm from looking at this, that it does not appear to be valid code for ScriptRunner for Jira Cloud.

I can confirm if you wanted to sum up the story points for a project that you would need to write a script listener script which returned all the issues for a project and summed up the story points for each issue and saved them as a field on an issue.

I can confirm that we have an example in the documentation page located here which shows how to sum up the story points on all sub tasks and to store them on a parent issue and that this script will provide you with a reference guide that can be adapted to achieve your requirement. 

Using this script you will be able to change the JQL search that is run to get all the issues for the project and then to sum the value up on these and to save the value to a field on a Jira Issue.

As for your second field of sharing the field in Confluence, once you have an issue with this value saved in a field then you will be to show this on a confluence page by displaying the issue using the Jira Issues Macro which confluence provides out of the box as described here.

I hope this information helps.

Regards,

Kristian

Hello Kristian, thanks for your answer.

 

I'm looking forward to your documentation and found this code : 

 

if (!issue.fields.issuetype.subtask) { (1)
return
}

// Get the parent issue as a Map
def parent = (issue.fields as Map).parent as Map

// Retrieve all the subtasks of this issue's parent
def parentKey = parent.key

def allSubtasks = get("/rest/api/2/search")
.queryString("jql", "parent=${parentKey}")
.queryString("fields", "parent,timeestimate") (2)
.asObject(Map)
.body
.issues as List<Map>
logger.info("Total subtasks for ${parentKey}: ${allSubtasks.size()}")

// Sum the estimates
def estimate = allSubtasks.collect { Map subtask ->
subtask.fields.timeestimate ?: 0 (3)
}.sum()
logger.info("Summed estimate: ${estimate}")

// Get the field ids
def fields = get('/rest/api/2/field')
.asObject(List)
.body as List<Map>

def summedEstimateField = fields.find { it.name == "Summed Subtask Estimate" }.id (4)
logger.info("Custom field ID to update: ${summedEstimateField}")

// Now update the parent issue
def result = put("/rest/api/2/issue/${parentKey}")
.header('Content-Type', 'application/json')
.body([
fields: [
(summedEstimateField): estimate
]
])
.asString()

// check that updating the parent issue worked
assert result.status >= 200 && result.status < 300

Howewer, i still have errors on it : 

 

2020-02-19_10h55_54.png2020-02-19_10h55_48.png

Thanks in advance for your help, 

Sébastien

Hi Sebastian,

I can confirm I have reviewed the errors shown in the attached screenshots and I can confirm that the static type checking warnings which you are getting when accessing the fields, is just a static type checking warning and this should not stop the script from running.

Static type checking warnings occur when ScriptRunner cannot fully understand the code syntax even tho it is valid and you find more information about static type checking in the documentation page located here.

These warnings should not prevent the script from running and I would ask you to try running the script to see if it runs as expected.

Finally, I can confirm anywhere where you see a number in brackets such as (1) or (2) that this should be removed as these just highlight lines in the script where we explain what it does below the script in the documentation and these should not be copied when taking the example code.

Regards,

Kristian

Hello Kristian, thanks for your help and sorry for my poor level in code.

 

if (!(issue.fields.issuetype.subtask && issue.fields.project.key == 'Workflow')) {
return
}

// Get the parent issue as a Map
def parent = (issue.fields as Map).parent as Map

// Retrieve all the subtasks of this issue's parent
def parentKey = parent.key

def allSubtasks = get("/rest/api/2/search")
.queryString("jql", "parent=${parentKey}")
.queryString("fields", "parent,timeestimate")
.asObject(Map)
.body
.issues as List<Map>
logger.info("Total subtasks for ${parentKey}: ${allSubtasks.size()}")

// Sum the estimates
def estimate = allSubtasks.collect { Map subtask ->
subtask.fields.timeestimate ?: 0
}.sum()
logger.info("Summed estimate: ${estimate}")

// Get the field ids
def fields = get('/rest/api/2/field')
.asObject(List)
.body as List<Map>

def summedEstimateField = fields.find { it.name == "Summed Subtask Estimate" }.id
logger.info("Custom field ID to update: ${summedEstimateField}")

// Now update the parent issue
def result = put("/rest/api/2/issue/${parentKey}")
.header('Content-Type', 'application/json')
.body([
fields: [
(summedEstimateField): estimate
]
])
.asString()

// check that updating the parent issue worked
assert result.status >= 200 && result.status < 300

I have some errors running this code : that's the result : 



groovy.lang.MissingPropertyException: No such property: issue for class: Script1 at Script1.run(Script1.groovy:1) at Script1$run.call(Unknown Source) at Script1$run.call(Unknown Source) at com.adaptavist.sr.cloud.workflow.AbstractScript.evaluate(AbstractScript.groovy:34) at com.adaptavist.sr.cloud.events.ScriptExecution.run(ScriptExecution.groovy:29) at ConsoleScriptExecution1_groovyProxy.run(Unknown Source)

Hi Sebastian,

Can you please confirm how you are running the code as I can confirm if it is a Script Listener then it will need to be configured on the issue updated event to ensure the issue object exists when the script is run.

If it is a script console script then you will need to make a rest call to get the issue at the top of your script so that the issue object exists  when you call fields on it.

I hope this information helps.

Regards,

Kristian

Hello Kristian, 

 

I'm running it in the "script console" but i'm searching how to display it in a custom field.

How can i make a rest call ?

 

Thanks, 


Sébastien

Hi Sebastian,

Thank you for your response.

I can confirm that example is designed for a Script Listener.

If you run it on the script console you will need to get the issue and I can confirm you can see an example of how to get an issue by clicking on the Get Issue Fields link in the Examples section below the code box on the Script Console screen as this will display an example script which shows how to return an issue.

I can also confirm that to see how to update an issue on the Script Console by clicking the Update Issue link in the Examples section below the code box on the Script Console screen as this will display an example script which shows how to update an issue. 

Please, note I do not have a complete example to achieve your requirements but you can use all the examples I have shared to learn how to create the script which you require.

I hope this information helps.

Regards,

Kristian

Hello Kristian,

 

Thanks for your help !

 

Regards, 

Hi Sebastian,

You are welcome.

Regards,

Kristian

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Marketplace Apps & Integrations

5 mobile apps for Jira Cloud to boost productivity

  It’s very important to have access to the workflow process from anywhere. Especially if you manage the work of others. There is no difference whether you’re out of office, or drive a ca...

203 views 2 5
Read article

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