Snippets

Adaptavist Jira Cloud Script Listiner Calculated Field To Count The Number Of Labels On an Issue

Updated by Kristian Walker

File JiraCloudCalculatedFieldLabelsCount.groovy Modified

  • Ignore whitespace
  • Hide word diff
+/*
+ * This script listiner which should be configured on the issue updated event shows how you can have a field to calculate and show the number of labels on an issue. 
+ * "All right, title and interest in this code snippet shall remain the exclusive intellectual property of Adaptavist Group Ltd and its affiliates. Customers with a valid ScriptRunner 
+ * license shall be granted a  non-exclusive, non-transferable, freely revocable right to use this code snippet only within their own instance of Atlassian products. This licensing notice cannot be removed
+ * or amended and must be included in any circumstances where the code snippet is shared by You or a third party." 
+ */
+
 // get custom fields
 def customFields = get("/rest/api/2/field")
         .asObject(List)
Created by Kristian Walker

File JiraCloudCalculatedFieldLabelsCount.groovy Added

  • Ignore whitespace
  • Hide word diff
+// get custom fields
+def customFields = get("/rest/api/2/field")
+        .asObject(List)
+        .body
+        .findAll { (it as Map).custom } as List<Map>
+
+// Specify the Name of the custom type field that will out the calcuated value
+def outputCfId = customFields.find { it.name == '<NumberFieldNameHere>' }?.id
+
+// Specify the project where the calculated field will go
+def projectKey = "<ProjectKeyHere>"
+
+if (issue == null || ((Map)issue.fields.project).key != projectKey) {
+    logger.info("Wrong Project \${issue.fields.project.key}")
+    return
+}
+
+// store the number of labels
+def NumberofLabels =  (issue.fields.labels.size)
+int output = NumberofLabels.toInteger()
+
+if (output == (issue.fields[outputCfId] as Integer)) {
+    logger.info("already been updated")
+    return
+}
+
+// Update the issue with the calculated value
+put("/rest/api/2/issue/${issue.key}")
+        .header("Content-Type", "application/json")
+        .body([
+        fields:[
+                (outputCfId): output
+        ]
+])
+        .asString()
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.