Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Seeking Advice on Automating Status Changes in Jira

Amit
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 4, 2024
How can I automate the transition of the MCA project to the "Done" status in Jira once both the BCA and CA projects have reached the "Done" status, considering that MCA is linked to BCA and CA, using Script Runner
Current Setup: I've written a ScriptRunner script in Groovy that checks the statuses of linked issues in BCA and CA projects. If both BCA and CA are in the "Done" status, it directly transitions the MCA issue to "Done", without waiting for BCA and CA statuses?
Groovy Script:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink
import org.apache.log4j.Level
import org.apache.log4j.Logger

// Set the logger level
def logger = Logger.getLogger("com.onresolve.scriptrunner.runner.ScriptRunnerImpl")
logger.setLevel(Level.INFO)

// Define the keys of the BCA and CA issues
def bcaIssueKey = "BCA-1" // Update with the relevant BCA issue key
def caIssueKey = "CA-1"   // Update with the relevant CA issue key

// Fetch the BCA and CA issues
def issueManager = ComponentAccessor.issueManager
def bcaIssue = issueManager.getIssueByCurrentKey(bcaIssueKey)
def caIssue = issueManager.getIssueByCurrentKey(caIssueKey)

// Check if BCA and CA issues exist
if (!bcaIssue) {
    logger.error("BCA issue with key ${bcaIssueKey} not found.")
    return
}
if (!caIssue) {
    logger.error("CA issue with key ${caIssueKey} not found.")
    return
}

// Get the statuses of BCA and CA issues
def bcaStatus = bcaIssue.status.name
def caStatus = caIssue.status.name

// Check if both BCA and CA are done
if (bcaStatus == "Done" && caStatus == "Done") {
    // Define the project keys
    def bcaProjectKey = "BCA"
    def caProjectKey = "CA"
    def mcaProjectKey = "MCA"

    // Define the link type names and "Done" status ID
    def relatesToLinkTypeName = "relates to"
    def blockedByLinkTypeName = "Blocked by"
    def doneStatusId = "10002"

    // Define the ID of the "Done" transition for MCA project
    def mcaDoneTransitionId = 41

    // Define the key of the MCA issue
    def mcaIssueKey = "MCA-2" // Update with the relevant MCA issue key

    // Fetch the MCA issue
    def mcaIssue = issueManager.getIssueByCurrentKey(mcaIssueKey)
    if (!mcaIssue) {
        logger.error("MCA issue with key ${mcaIssueKey} not found.")
        return
    }

    // Get all links of the MCA issue
    def issueLinkManager = ComponentAccessor.issueLinkManager
    def mcaLinks = issueLinkManager.getOutwardLinks(mcaIssue.id) + issueLinkManager.getInwardLinks(mcaIssue.id)

    def allBcaLinkedIssuesDone = true
    def allCaLinkedIssuesDone = true

    mcaLinks.each { link ->
        def linkedIssue = (link.sourceObject.id == mcaIssue.id) ? link.destinationObject : link.sourceObject

        if (linkedIssue.projectObject.key == bcaProjectKey && link.issueLinkType.name == relatesToLinkTypeName) {
            // Get all links of the BCA issue
            def bcaLinks = issueLinkManager.getOutwardLinks(linkedIssue.id) + issueLinkManager.getInwardLinks(linkedIssue.id)
            bcaLinks.each { bcaLink ->
                def bcaLinkedIssue = (bcaLink.sourceObject.id == linkedIssue.id) ? bcaLink.destinationObject : bcaLink.sourceObject
                if (bcaLinkedIssue.projectObject.key == caProjectKey && bcaLink.issueLinkType.name == blockedByLinkTypeName) {
                    if (bcaLinkedIssue.status.id != doneStatusId) {
                        allCaLinkedIssuesDone = false
                        logger.info("Issue ${bcaLinkedIssue.key} in project ${caProjectKey} is not Done. Current status ID: ${bcaLinkedIssue.status.id}")
                    }
                }
            }
            if (linkedIssue.status.id != doneStatusId) {
                allBcaLinkedIssuesDone = false
                logger.info("Issue ${linkedIssue.key} in project ${bcaProjectKey} is not Done. Current status ID: ${linkedIssue.status.id}")
            }
        }
    }

    // Only transition the MCA issue if both sets of linked issues are all done
    if (allBcaLinkedIssuesDone && allCaLinkedIssuesDone) {
        logger.info("All linked issues are done. Transitioning issue ${mcaIssue.key} to Done")

        def issueService = ComponentAccessor.issueService
        def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
        def transitionValidationResult = issueService.validateTransition(user, mcaIssue.id, mcaDoneTransitionId, issueService.newIssueInputParameters())

        if (transitionValidationResult.isValid()) {
            def transitionResult = issueService.transition(user, transitionValidationResult)

            if (transitionResult.isValid()) {
                logger.info("Successfully transitioned MCA issue ${mcaIssue.key} to Done.")
            } else {
                def errorCollection = transitionResult.errorCollection
                logger.error("Failed to transition MCA issue ${mcaIssue.key} to Done: ${errorCollection}")
            }
        } else {
            def errorCollection = transitionValidationResult.errorCollection
            logger.error("Transition validation failed for MCA issue ${mcaIssue.key}: ${errorCollection}")
        }
    } else {
        logger.info("MCA issue ${mcaIssue.key} was not transitioned because not all linked issues are done.")
    }
} else {
    logger.info("BCA and CA must be done before setting MCA to done.")
}
status changing only MCA.png

0 answers

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events