/src/main/resources/com/onresolve/jira/groovy/canned/workflow/postfunctions/UpdateBlockedIssues.groovy
Groovy | 124 lines | 99 code | 24 blank | 1 comment | 10 complexity | 0c8a690b295f2c04d283bcc457dca5f2 MD5 | raw file
- package com.onresolve.jira.groovy.canned.workflow.postfunctions
-
- import com.atlassian.jira.ComponentManager
- import com.atlassian.jira.issue.Issue
-
- import com.atlassian.jira.util.ErrorCollection
-
- import com.onresolve.jira.groovy.canned.CannedScript
- import com.onresolve.jira.groovy.canned.utils.CannedScriptUtils
-
- import org.apache.log4j.Category
- import com.atlassian.jira.issue.link.IssueLinkManager
-
- import com.onresolve.jira.groovy.canned.utils.WorkflowUtils
-
- import com.atlassian.jira.issue.link.IssueLink
- import com.atlassian.jira.issue.MutableIssue
- import com.atlassian.jira.issue.comments.CommentManager
- import com.atlassian.jira.util.SimpleErrorCollection
- import com.atlassian.crowd.embedded.api.User
-
- class UpdateBlockedIssues implements CannedScript{
-
- ComponentManager componentManager = ComponentManager.getInstance()
- Category log = Category.getInstance(UpdateBlockedIssues.class)
- def projectManager = componentManager.getProjectManager()
- public static final String FIELD_LINK_TYPE = 'FIELD_LINK_TYPE'
- public static final String FIELD_COMMENT = 'FIELD_COMMENT'
-
- String getName() {
- "Adds a comment to all blocked issues when this issue is transitioned."
- }
-
- public String getHelpUrl() {
- "https://studio.plugins.atlassian.com/wiki/display/GRV/Built-In+Scripts#Built-InScripts-Updateblockingissues"
- }
-
- String getDescription() {
- "Useful for alerting participants of other issues that a blocker is resolved, etc." + """<br>
- This function should be put on the Resolve transition (or similar).
- """
- }
-
- List getCategories() {
- ["Function"]
- }
-
-
- List getParameters(Map params) {
- [
- // possibly require addition comment and additional-code fields here
- [
- Name:FIELD_LINK_TYPE,
- Label:'Issue Link Type',
- Type: "list",
- Description:"What link types to look for... e.g. <b><i>blocks</i> will add a comment on all issues that this one blocks</b>",
- Values: CannedScriptUtils.getAllLinkTypesWithDirections(true)
- ],
- [
- Name:FIELD_COMMENT,
- Label: 'Comment',
- Description: 'Comment to add to linked issues. Click an example below.',
- Type:'mediumtext',
- Examples:[
- "Blocking issue ABC-123 resolved with resolution Fixed.":
- "Blocking issue \$issue resolved with resolution <% out << issue.resolution?.name %>."
- ],
- ]
- ]
-
- }
-
- public ErrorCollection doValidate(Map params, boolean forPreview) {
- SimpleErrorCollection errorCollection = new SimpleErrorCollection()
- if (! params[FIELD_COMMENT]) {
- errorCollection.addError(FIELD_COMMENT, "You must provide a comment. Editing this function and clicking the example link will copy an example.")
- }
- if (! params[FIELD_LINK_TYPE]) {
- errorCollection.addError(FIELD_LINK_TYPE, "You must provide a link type.")
- }
- errorCollection
- }
-
- Map doScript(Map params) {
- Issue issue = params['issue'] as Issue
- String linkTypeStr = params[FIELD_LINK_TYPE] as String
- String commentTemplate = params[FIELD_COMMENT] as String
- String linkTypeId = ""
- String linkDirection = ""
- (linkTypeId, linkDirection) = linkTypeStr.split(/ /) as List
-
- IssueLinkManager linkMgr = ComponentManager.getInstance().getIssueLinkManager()
- CommentManager commentManager = componentManager.getCommentManager()
-
- User currentUser = WorkflowUtils.getUser(params)
-
- List links = []
- if (linkDirection == CannedScriptUtils.OUTWARD_FIELD_NAME) {
- links = linkMgr.getOutwardLinks(issue.id)
- }
- else if (linkDirection == CannedScriptUtils.INWARD_FIELD_NAME) {
- links = linkMgr.getInwardLinks(issue.id)
- }
-
- links.each {IssueLink link ->
- if (link.getLinkTypeId() == linkTypeId as Long) {
- MutableIssue destIssue = linkDirection == CannedScriptUtils.OUTWARD_FIELD_NAME ? link.getDestinationObject() : link.getSourceObject()
- Writable template = WorkflowUtils.mergeTemplate(params, commentTemplate)
- log.info ("Create comment on ${destIssue.key}")
- commentManager.create(destIssue, currentUser.getName(), template.toString(), true)
- }
- }
-
- [:]
- }
-
- String getDescription(Map params, boolean forPreview) {
- getName()
- }
-
- public Boolean isFinalParamsPage(Map params) {
- true
- }
- }