Forums

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

Script to update linked issues status to "Done" of parent project using its sub projects

Rohit Datir June 4, 2024

Hello everyone, I want to ask a question, suppose i have 3 projects named IT, Jira and DevOps project in Jira Data Center. I want a Jira runner script in which I want IT project issues to be automatically transitioned when both Jira and DevOps projects issues are marked done.

By the way both project issues are linked issues which are linked to IT project issues as either "relates to" or "is blocked by".

I have tried the script for it but even though Jira and DevOps project issues are in backlog the IT project issues are getting automatically done as soon as I run the script on console

Please look at the script and help me to correct the script

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.issue.IssueInputParametersImpl
import com.atlassian.jira.user.ApplicationUser

// Get Jira components
def issueManager = ComponentAccessor.getIssueManager()
def projectManager = ComponentAccessor.getProjectManager()
def authenticationContext = ComponentAccessor.getJiraAuthenticationContext()
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def issueService = ComponentAccessor.getIssueService()
def currentUser = authenticationContext.loggedInUser
def userManager = ComponentAccessor.getUserManager()

// Define the project keys
def itProjectKey = "IT"
def jiraProjectKey = "JI"
def devOpsProjectKey = "DEV"

// Define the link type names and "Done" status ID
def relatesToLinkTypeName = "relates to"
def blockedByLinkTypeName = "is blocked by"
def doneStatusId = "31" // Updated to "Done" status ID

// Define the ID of the "Done" transition for IT project
def itDoneTransitionId = 41

// Get all issues in the IT project
def itProject = projectManager.getProjectByCurrentKey(itProjectKey)
def itProjectIssues = issueManager.getIssueObjects(issueManager.getIssueIdsForProject(itProject.id))

itProjectIssues.each { itIssue ->
    // Initialize variables to track status of linked issues
    def allJiraLinkedIssuesDone = true
    def allDevOpsLinkedIssuesDone = true

    // Get all outward links for the current IT issue
    def links = issueLinkManager.getOutwardLinks(itIssue.id)

    links.each { link ->
        // Get the linked issue
        def linkedIssue = link.destinationObject
       
        // Check if the linked issue is in Jira project
        if (linkedIssue.projectObject.key == jiraProjectKey && link.issueLinkType.name == relatesToLinkTypeName) {
            // Check if the linked issue is not in "Done" status
            if (linkedIssue.status.id != doneStatusId) {
                allJiraLinkedIssuesDone = false
            }
        }
       
        // Check if the linked issue is in DevOps project
        if (linkedIssue.projectObject.key == devOpsProjectKey && link.issueLinkType.name == blockedByLinkTypeName) {
            // Check if the linked issue is not in "Done" status
            if (linkedIssue.status.id != doneStatusId) {
                allDevOpsLinkedIssuesDone = false
            }
        }
    }

    // Only transition the IT issue if all linked issues in both projects are in "Done" status
    if (allJiraLinkedIssuesDone && allDevOpsLinkedIssuesDone) {
        def issueInputParameters = new IssueInputParametersImpl()
        def transitionValidationResult = issueService.validateTransition(currentUser, itIssue.id, itDoneTransitionId, issueInputParameters)
       
        if (transitionValidationResult.isValid()) {
            def transitionResult = issueService.transition(currentUser, transitionValidationResult)
           
            if (transitionResult.isValid()) {
                log.info("Successfully transitioned IT issue ${itIssue.key} to Done.")
            } else {
                def errorCollection = transitionResult.errorCollection
                log.error("Failed to transition IT issue ${itIssue.key} to Done: ${errorCollection}")
            }
        } else {
            def errorCollection = transitionValidationResult.errorCollection
            log.error("Transition validation failed for IT issue ${itIssue.key}: ${errorCollection}")
        }
    } else {
        log.info("IT issue ${itIssue.key} was not transitioned because not all linked issues are in 'Done' status.")
    }
}

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