Wiki source code of ScriptRunner for JIRA plugin
Version 1.1 by Normann P. Nielsen on 2026/11/15 15:44
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | [[This Plugin>>https://marketplace.atlassian.com/plugins/com.onresolve.jira.groovy.groovyrunner]] gives some extremely nice features, like: | ||
| 2 | |||
| 3 | * Using prebuilt-in functions/tasks | ||
| 4 | * Running scripts in Transitions. | ||
| 5 | * Online (GUI) groovy scriprunner | ||
| 6 | |||
| 7 | |||
| 8 | {{error}}With JIRA 7 the "ComponentManager" is deprecated, use "ComponentAccessor" instead. | ||
| 9 | |||
| 10 | With JIRA 8 the "ComponentManager" is removed, use "ComponentAccessor" instead.{{/error}} | ||
| 11 | |||
| 12 | {{info}}The plugin has been purchased by Adaptavist in 2015 and is from version 4 Paid Product.{{/info}} | ||
| 13 | |||
| 14 | |||
| 15 | |||
| 16 | {{toc/}} | ||
| 17 | |||
| 18 | = Change Task Type = | ||
| 19 | |||
| 20 | This actually gets hold on a clone and changes the "Task Type" Custom Field to "Clone", whereas on the Master it is still "Master" | ||
| 21 | |||
| 22 | |||
| 23 | {{code}} | ||
| 24 | import com.atlassian.jira.ComponentAccessor | ||
| 25 | import com.atlassian.jira.issue.customfields.manager.OptionsManager | ||
| 26 | import com.atlassian.jira.issue.CustomFieldManager | ||
| 27 | |||
| 28 | componentManager (componentManager) ComponentAccessor.getComponentManager() | ||
| 29 | |||
| 30 | optionsManager = (optionsManager) ComponentAccessor.getOptionsManager() | ||
| 31 | customFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager() | ||
| 32 | def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Task Type'} | ||
| 33 | def fieldConfig = cf.getRelevantConfig(issue) | ||
| 34 | def optionClone = optionsManager.getOptions(fieldConfig).find {it.value == "Clone"} | ||
| 35 | issue.setCustomFieldValue(cf, optionClone) | ||
| 36 | {{/code}} | ||
| 37 | |||
| 38 | |||
| 39 | |||
| 40 | = Condition for Link = | ||
| 41 | |||
| 42 | This script sends "passesCondition = false" if there is an "Awaiting" Link type on the Issue. | ||
| 43 | |||
| 44 | |||
| 45 | {{code}} | ||
| 46 | import com.atlassian.jira.component.ComponentAccessor | ||
| 47 | import com.atlassian.jira.issue.link.IssueLink | ||
| 48 | import com.atlassian.jira.issue.link.IssueLinkManager | ||
| 49 | import com.atlassian.jira.issue.Issue | ||
| 50 | import org.apache.log4j.Category | ||
| 51 | |||
| 52 | IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager() | ||
| 53 | |||
| 54 | String IssueType = issue.getIssueType().name | ||
| 55 | String ProjectName = issue.getProjectObject().name | ||
| 56 | String IssueKey = issue.key | ||
| 57 | passesCondition = true | ||
| 58 | |||
| 59 | System.out.println("script=CheckTOPSLinkStatus.groovy Issuekey: " + IssueKey + " IssueType: " + IssueType + " ProjectName: " + ProjectName + " action=StartScript") | ||
| 60 | |||
| 61 | //Traverse all links | ||
| 62 | issueLinkManager.getOutwardLinks(issue.id)?.each {issueLink -> | ||
| 63 | String LinkTypeName = issueLink.issueLinkType.name | ||
| 64 | String LinkStatus = issueLink.getDestinationObject().getStatus().name | ||
| 65 | String LinkIssueKey = issueLink.getDestinationObject().key | ||
| 66 | System.out.println("script=CheckTOPSLinkStatus.groovy Issuekey: " + IssueKey + " Issue Link key: " + LinkIssueKey + " Issue Link Type: " + LinkTypeName + " Linked Issue Status: " + LinkStatus) | ||
| 67 | if (LinkTypeName == "Awaiting") | ||
| 68 | { | ||
| 69 | passesCondition = false | ||
| 70 | System.out.println("script=CheckTOPSLinkStatus.groovy Issuekey: " + IssueKey + " IssueType: " + IssueType + " ProjectName: " + ProjectName + " action=Hide Transition") | ||
| 71 | } | ||
| 72 | } | ||
| 73 | System.out.println("script=CheckTOPSLinkStatus.groovy Issuekey: " + IssueKey + " IssueType: " + IssueType + " ProjectName: " + ProjectName + " action=EndScript") | ||
| 74 | {{/code}} | ||
| 75 | |||
| 76 | |||
| 77 | |||
| 78 | = Setting Issue values after "Create Subtask" = | ||
| 79 | |||
| 80 | In This sample "transientVars" refers to the parent values | ||
| 81 | |||
| 82 | See issue[[ properties/methods reference>>https://docs.atlassian.com/jira/latest/com/atlassian/jira/issue/MutableIssue.html]] | ||
| 83 | |||
| 84 | |||
| 85 | {{code}} | ||
| 86 | issue.assignee == transientVars["issue"].assignee; | ||
| 87 | issue.setAssignee(null); | ||
| 88 | issue.setDescription("A new Description"); | ||
| 89 | issue.setSummary("Unit Test - " + transientVars["issue"].key) | ||
| 90 | {{/code}} | ||
| 91 | |||
| 92 | = Getting a Custom Field Value (String) = | ||
| 93 | |||
| 94 | |||
| 95 | {{code}} | ||
| 96 | import com.atlassian.jira.ComponentAccessor; | ||
| 97 | import com.atlassian.jira.issue.fields.CustomField | ||
| 98 | import com.atlassian.jira.issue.CustomFieldManager | ||
| 99 | customFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager() | ||
| 100 | CustomField UseCustomer = customFieldManager.getCustomFieldObject(10600) | ||
| 101 | |||
| 102 | System.out.println("Custom field value: " + issue.getCustomFieldValue(UseCustomer)) | ||
| 103 | {{/code}} | ||
| 104 | |||
| 105 | |||
| 106 | |||
| 107 | = Setting a Custom Field Value (String) = | ||
| 108 | |||
| 109 | Taken the value fetched above: | ||
| 110 | |||
| 111 | |||
| 112 | {{code}} | ||
| 113 | issue.setCustomFieldValue(Customer,issue.getCustomFieldValue(UseCustomer)) | ||
| 114 | {{/code}} | ||
| 115 | |||
| 116 | This does not work for all field types (like labels). | ||
| 117 | |||
| 118 | = Assigning a user = | ||
| 119 | |||
| 120 | |||
| 121 | {{code}} | ||
| 122 | String userName="pho" | ||
| 123 | def userManager = ComponentAccessor.getUserManager() | ||
| 124 | def user = userManager.getUserObject(userName) | ||
| 125 | issue.setAssignee(user) | ||
| 126 | |||
| 127 | //Update the issue - may not be nessesary | ||
| 128 | ComponentManager.getInstance().getIssueManager().updateIssue(ComponentManager.getInstance().jiraAuthenticationContext?.user, issue, EventDispatchOption.DO_NOT_DISPATCH, false) | ||
| 129 | ComponentManager.getInstance().getIndexManager().reIndex(issue) | ||
| 130 | {{/code}} | ||
| 131 | |||
| 132 | |||
| 133 | |||
| 134 | A broader picture (ref: [[https://answers.atlassian.com/questions/98433/assigning-issues-via-groovy]]): | ||
| 135 | |||
| 136 | |||
| 137 | {{code}} | ||
| 138 | import com.atlassian.jira.ComponentAccessor | ||
| 139 | import com.atlassian.jira.security.Permissions | ||
| 140 | import com.atlassian.jira.event.type.EventDispatchOption | ||
| 141 | import com.atlassian.jira.util.ImportUtils | ||
| 142 | |||
| 143 | //Examples, 8 is my subtask issue type id. Will be different for others | ||
| 144 | updateAssignee("PRJ-1485", "user1") | ||
| 145 | |||
| 146 | //Method to do all the work | ||
| 147 | def updateAssignee(parentId, userName) { | ||
| 148 | def issue = ComponentAccessor.getIssueManager().getIssueObject(parentId) | ||
| 149 | def userManager = ComponentAccessor.getUserManager() | ||
| 150 | def user = userManager.getUserObject(userName) | ||
| 151 | issue.setAssignee(user) | ||
| 152 | //Update the issue | ||
| 153 | ComponentAccessor.getIssueManager().updateIssue(ComponentAccessor.jiraAuthenticationContext?.user, issue, EventDispatchOption.DO_NOT_DISPATCH, false) | ||
| 154 | ComponentAccessor.getIndexManager().reIndex(issue) | ||
| 155 | } | ||
| 156 | {{/code}} | ||
| 157 | |||
| 158 | |||
| 159 | |||
| 160 | = Remove and Set labels = | ||
| 161 | |||
| 162 | |||
| 163 | {{code}} | ||
| 164 | import com.atlassian.jira.ComponentAccessor | ||
| 165 | import com.atlassian.jira.issue.fields.CustomField | ||
| 166 | import com.atlassian.jira.issue.CustomFieldManager | ||
| 167 | import com.atlassian.jira.issue.label.LabelManager | ||
| 168 | import org.springframework.util.StringUtils | ||
| 169 | import java.util.Arrays | ||
| 170 | import java.util.HashSet | ||
| 171 | import java.util.Set | ||
| 172 | |||
| 173 | labelManager = ComponentAccessor.getLabelManager() | ||
| 174 | CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager() | ||
| 175 | |||
| 176 | |||
| 177 | CustomField UseCustomer = customFieldManager.getCustomFieldObject(10600) | ||
| 178 | CustomField Customer = customFieldManager.getCustomFieldObject(10900) | ||
| 179 | |||
| 180 | labelManager.removeLabelsForCustomField(10900) | ||
| 181 | |||
| 182 | Set labelSet = new HashSet(); | ||
| 183 | labelSet.add(issue.getCustomFieldValue(UseCustomer)) | ||
| 184 | |||
| 185 | def authContext = ComponentAccessor.getJiraAuthenticationContext() | ||
| 186 | def user = authContext.getUser() | ||
| 187 | |||
| 188 | // issue.getCustomFieldValue(UseCustomer) | ||
| 189 | // Does not work - the "labelSet" is not a valid set - https://answers.atlassian.com/questions/332748/set-labels-in-jira | ||
| 190 | labelManager.setLabels(user, issue.id,labelSet, false,false) | ||
| 191 | |||
| 192 | {{/code}} | ||
| 193 | |||
| 194 | |||
| 195 | |||
| 196 | = Transist Linked Issues = | ||
| 197 | |||
| 198 | This is very nice, for "auto" progressing linked issues. (Idea and source from [[this>>https://answers.atlassian.com/questions/183942/groovy-script-for-linked-issue-transition]]) | ||
| 199 | |||
| 200 | |||
| 201 | {{code}} | ||
| 202 | import com.atlassian.jira.component.ComponentAccessor | ||
| 203 | import com.atlassian.jira.issue.link.IssueLink | ||
| 204 | |||
| 205 | def issueLinkManager = ComponentAccessor.getIssueLinkManager() | ||
| 206 | |||
| 207 | import org.apache.log4j.Category | ||
| 208 | import com.atlassian.jira.issue.comments.CommentManager | ||
| 209 | import com.opensymphony.workflow.WorkflowContext | ||
| 210 | import com.atlassian.jira.workflow.WorkflowTransitionUtil | ||
| 211 | import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl | ||
| 212 | import com.atlassian.jira.util.JiraUtils | ||
| 213 | |||
| 214 | def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction") | ||
| 215 | log.setLevel(org.apache.log4j.Level.DEBUG) | ||
| 216 | log.debug "debug statements" | ||
| 217 | |||
| 218 | String currentUser = ((WorkflowContext) transientVars.get("context")).getCaller() | ||
| 219 | WorkflowTransitionUtil workflowTransitionUtil = ( WorkflowTransitionUtil ) JiraUtils.loadComponent( WorkflowTransitionUtilImpl.class ) | ||
| 220 | |||
| 221 | |||
| 222 | issueLinkManager.getOutwardLinks(issue.id)?.each {issueLink -> | ||
| 223 | if (issueLink.issueLinkType.name == "parent-child") { | ||
| 224 | // Transist Issue | ||
| 225 | workflowTransitionUtil.setIssue(issueLink.getDestinationObject()); | ||
| 226 | workflowTransitionUtil.setUsername(currentUser); | ||
| 227 | workflowTransitionUtil.setAction (741) // 741 = Waiting | ||
| 228 | // validate and transition issue | ||
| 229 | workflowTransitionUtil.validate() | ||
| 230 | workflowTransitionUtil.progress() | ||
| 231 | // Add a comment so people have a clue why the child has been closed | ||
| 232 | CommentManager commentManager = (CommentManager) ComponentAccessor.getCommentManager() | ||
| 233 | String comment = "Status changed to *WAit for solution* as a result of the *Resolve* action being applied to the TD Issue." | ||
| 234 | commentManager.create(issueLink.getDestinationObject(), currentUser, comment, true) | ||
| 235 | } | ||
| 236 | } | ||
| 237 | {{/code}} | ||
| 238 | |||
| 239 | {{success}}The | ||
| 240 | |||
| 241 | |||
| 242 | {{code}} | ||
| 243 | workflowTransitionUtil.setIssue(issueLink.getDestinationObject()) | ||
| 244 | {{/code}} | ||
| 245 | |||
| 246 | should be | ||
| 247 | |||
| 248 | |||
| 249 | {{code}} | ||
| 250 | workflowTransitionUtil.setIssue(issueLink.getSourceObject()) | ||
| 251 | {{/code}} | ||
| 252 | |||
| 253 | Going "the other way". There is also an getInwardLinks(issue.id) collection.{{/success}} | ||
| 254 | |||
| 255 | |||
| 256 | |||
| 257 | = Get CurrentUser = | ||
| 258 | |||
| 259 | |||
| 260 | {{code}} | ||
| 261 | import com.atlassian.jira.issue.Issue | ||
| 262 | import com.atlassian.jira.ComponentManager | ||
| 263 | User = ComponentAccessor.getJiraAuthenticationContext().getUser().getName() | ||
| 264 | {{/code}} | ||
| 265 | |||
| 266 | = Send Custom Mail = | ||
| 267 | |||
| 268 | Some standard JIRA Field can be accessed very direct, other must be throught the | ||
| 269 | |||
| 270 | |||
| 271 | {{code}} | ||
| 272 | issue.getCustomFieldValue(componentManager.getCustomFieldManager().getCustomFieldObjectByName("Advisory Subject")) | ||
| 273 | {{/code}} | ||
| 274 | |||
| 275 | function. | ||
| 276 | |||
| 277 | **Subject** can be text or like: | ||
| 278 | |||
| 279 | |||
| 280 | {{code}} | ||
| 281 | ($issue) <% out << issue.getCustomFieldValue(com.atlassian.jira.component.ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Advisory Subject")) %> | ||
| 282 | {{/code}} | ||
| 283 | |||
| 284 | **Body** can be like: | ||
| 285 | |||
| 286 | |||
| 287 | {{code}} | ||
| 288 | <% out << issue.getCustomFieldValue(com.atlassian.jira.component.ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Mail Body")) %> | ||
| 289 | <br><br> | ||
| 290 | Best Regards, | ||
| 291 | <br><br> | ||
| 292 | GService Desk | ||
| 293 | <br> | ||
| 294 | <a href="http://sd.mydomain.dk">Service Desk</a> | ||
| 295 | <br> | ||
| 296 | You cant reply to this email. | ||
| 297 | {{/code}} | ||
| 298 | |||
| 299 | |||
| 300 | |||
| 301 | = Making Fields Required = | ||
| 302 | |||
| 303 | This script is a sample from a Validation Function | ||
| 304 | |||
| 305 | |||
| 306 | {{code}} | ||
| 307 | // This script makes a requirement: if the Custom Field "OprStatusShow" is set to public, Incident Start and Resolved must be set. | ||
| 308 | |||
| 309 | import com.opensymphony.workflow.InvalidInputException | ||
| 310 | import com.atlassian.jira.project.ProjectManager | ||
| 311 | import com.atlassian.jira.issue.CustomFieldManager | ||
| 312 | import com.atlassian.jira.issue.fields.CustomField | ||
| 313 | |||
| 314 | //Project "Support" Id = 10130 | ||
| 315 | if (issue.getProjectObject().getId() == 10130) { | ||
| 316 | |||
| 317 | CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager() | ||
| 318 | CustomField IncidentResolvedField = customFieldManager.getCustomFieldObject("customfield_12224") | ||
| 319 | CustomField IncidentStartField = customFieldManager.getCustomFieldObject("customfield_10091") | ||
| 320 | CustomField OprStatusShowField = customFieldManager.getCustomFieldObject("customfield_11820") | ||
| 321 | |||
| 322 | |||
| 323 | String IncidentResolved = (String)issue.getCustomFieldValue(IncidentResolvedField) | ||
| 324 | String IncidentStart = (String)issue.getCustomFieldValue(IncidentStartField) | ||
| 325 | String OprStatusShow = (String)issue.getCustomFieldValue(OprStatusShowField) | ||
| 326 | |||
| 327 | if (OprStatusShow == "Yes") { | ||
| 328 | |||
| 329 | if (IncidentStart == null || IncidentResolved == null) { | ||
| 330 | InvalidInputException e= new InvalidInputException(); | ||
| 331 | e.addError("Incident Start And Incident Resolved must not be empty") | ||
| 332 | throw e | ||
| 333 | } | ||
| 334 | } | ||
| 335 | } | ||
| 336 | {{/code}} | ||
| 337 | |||
| 338 | |||
| 339 | |||
| 340 | = Scripted Fields = | ||
| 341 | |||
| 342 | A "neat" but raw idea for a scripted field: [[https://answers.atlassian.com/questions/191893]] | ||
| 343 | |||
| 344 | |||
| 345 | |||
| 346 | = Links = | ||
| 347 | |||
| 348 | [[https://jamieechlin.atlassian.net/wiki/display/GRV/Post+Functions]] | ||
| 349 | |||
| 350 | [[http://quisapps.com/confluence/display/JSS/Scripting+Samples]] |