Skip to content

Instantly share code, notes, and snippets.

@jechlin
Created October 19, 2016 08:36
Show Gist options
  • Select an option

  • Save jechlin/573aa77aadacfd5ed05f0adc5744727a to your computer and use it in GitHub Desktop.

Select an option

Save jechlin/573aa77aadacfd5ed05f0adc5744727a to your computer and use it in GitHub Desktop.
package examples.answers
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.onresolve.scriptrunner.runner.customisers.ContextBaseScript
import groovy.transform.BaseScript
@BaseScript ContextBaseScript context
issue = getIssueOrDefault("JRA-3") as MutableIssue
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def issueService = ComponentAccessor.getIssueService()
def currentUser = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
def updateInitiative = {Issue initiative ->
if (initiative.issueType.name == "Initiative") {
log.debug("found an initiaive - key is ${initiative.key}")
def parameters = issueService.newIssueInputParameters()
def result = issueService.validateTransition(currentUser, initiative.id, 4, parameters)
issueService.transition(currentUser, result)
}
}
issueLinkManager.getOutwardLinks(issue.id).each { link ->
def initiative = link.destinationObject
updateInitiative(initiative)
}
issueLinkManager.getInwardLinks(issue.id).each { link ->
def initiative = link.sourceObject
updateInitiative(initiative)
}
@SxMShaDoW
Copy link

Auto-Close Epics if all the stories are closed.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.onresolve.scriptrunner.runner.customisers.ContextBaseScript
import groovy.transform.BaseScript

@BaseScript ContextBaseScript context

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def issueService = ComponentAccessor.getIssueService()

def currentUser = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()

def isEpicInProgress = {Issue epic -> 
    if (epic.issueType.name == "Epic") {
        if (epic.statusObject.name != "Closed") {
            return true
        } else {
            return false
        }
}
}

def isStoryClosed = {Issue story -> 
    if (story.statusObject.name == "Closed") {
        return true
     } else {
        return false
    }
}

def updateEpic = {Issue epic ->
    if (isEpicInProgress) {
            def parameters = issueService.newIssueInputParameters()
            def result = issueService.validateTransition(currentUser, epic.id, 31, parameters)
            issueService.transition(currentUser, result)
        }
}

def isEpicClosable = {Issue epic ->
    def finalResult = true

    issueLinkManager.getOutwardLinks(epic.id).each { link ->
        def story = link.destinationObject
        closedStory = isStoryClosed(story)
        finalResult = closedStory && finalResult
    }

    return finalResult
}

issueLinkManager.getInwardLinks(issue.id).each { link ->
        def linkTypeId = link.getLinkTypeId()
        def isEpicOfLinkType = 10300
        if (isEpicOfLinkType == linkTypeId) {
            def epic = link.sourceObject
            if (isEpicInProgress(epic)) {
                if (isEpicClosable(epic)) {
                    updateEpic(epic)
                }
            }
        }
 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment