Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vikrantyadav11/6b7eca3ed7f841920f3c6dfd0204142f to your computer and use it in GitHub Desktop.
Save vikrantyadav11/6b7eca3ed7f841920f3c6dfd0204142f to your computer and use it in GitHub Desktop.
// Get Story Points field ID
def storyPointsField = get("/rest/api/3/field")
.asObject(List)
.body
.find { it.name == "Story Points" }
.id
logger.info("Story Point ID : ${storyPointsField}")
// Cast issue.key to String
def eventIssue = Issues.getByKey(issue.key as String)
def issueType = eventIssue.issueType.name
if (issueType in ["Task", "Story", "Design Task"]) {
def parentId = eventIssue.parentId
if (!parentId) return
// Fetch parent issue key
def parentKey = get("/rest/api/3/issue/${parentId}")
.queryString("fields", "key")
.asObject(Map)
.body
.key
// Initialize sum
def childTaskSum = 0.0 // use Double
Issues.search("parent = ${parentKey}").each { child ->
// Avoid using the field ID that you retrived; use the field name directly
def spValue = child.getCustomFieldValue('Story Points')
def sp = spValue ? spValue.toString().toDouble() : 0
childTaskSum += sp
}
logger.info("Updating parent ${parentKey} with summed Story Points = ${childTaskSum}")
// Update the parent issue in one operation
put("/rest/api/3/issue/${parentKey}")
.header("Content-Type", "application/json")
.body([fields: [(storyPointsField): childTaskSum]])
.asString()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment