Skip to main content
Example scripts
arrow icon
To homepage
Jira
Data centre icon
Data Center

Transition an Issue when a Zephyr Scale Test Case is Updated

Features
Listeners
Created 1 year ago, Updated 0 day(s) ago
App in script
ScriptRunner For Jira
ScriptRunner For Jira
by Adaptavist
Compatibility
compatibility bullet
Jira (7.7 - 8.6)
compatibility bullet
ScriptRunner For Jira (5.6.14)
Language |
groovy
@WithPlugin("com.kanoah.test-manager")
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.adaptavist.tm4j.api.event.testcase.TestCaseChangedEvent
import com.adaptavist.tm4j.api.service.status.StatusService
import com.adaptavist.tm4j.api.service.testcase.TestCaseService
import com.adaptavist.tm4j.api.service.tracelink.TraceLinkService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.opensymphony.workflow.loader.ActionDescriptor
import com.atlassian.jira.issue.Issue

def testCaseService = ComponentAccessor.getOSGiComponentInstanceOfType(TestCaseService)
def statusService = ComponentAccessor.getOSGiComponentInstanceOfType(StatusService)
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def event = event as TestCaseChangedEvent
final testCaseId = event.id
final testCaseModel = testCaseService.getTestCaseModelById(currentUser.key, testCaseId).result
final testCaseStatusId = testCaseModel.statusId
final testCaseStatusModel = statusService.getTestCaseStatusModelById(currentUser.key, testCaseStatusId).result

final doneAction = "Done"
final inProgressAction = "In Progress"
final toDoAction = "To Do"

if (testCaseStatusModel.deprecated) {
    issueTransition(doneAction, currentUser, testCaseId)
} else if (testCaseStatusModel.draft) {
    issueTransition(toDoAction, currentUser, testCaseId)
} else {
    issueTransition(inProgressAction, currentUser, testCaseId)
}

def issueTransition(String actionName, ApplicationUser currentUser, Long testCaseId) {
    def issueManager = ComponentAccessor.issueManager
    def issueService = ComponentAccessor.issueService
    def traceLinkService = ComponentAccessor.getOSGiComponentInstanceOfType(TraceLinkService)
    def traceLinks = traceLinkService.getTraceLinkModelsByTestCaseId(currentUser.key, testCaseId).result
    def traceLinksWithIssues = traceLinks.findAll { tl -> tl.issueId != null }
    def issueIds = traceLinksWithIssues*.issueId
    issueIds.each { issueId ->
        def issue = issueManager.getIssueObject(issueId)
        def issueInputParameters = issueService.newIssueInputParameters()
        final action = getAction(actionName, issue)
        if (!action) {
            throw new RuntimeException("Action '$actionName' Not Found")
        }
        def validateTransition = issueService.validateTransition(currentUser, issue.id, action.id, issueInputParameters)
        if (validateTransition.valid) {
            issueService.transition(currentUser, validateTransition)
        }
    }
}

ActionDescriptor getAction(String actionName, Issue issue) {
    def workflowManager = ComponentAccessor.workflowManager.getWorkflow(issue)
    def actions = workflowManager.getActionsByName(actionName)
    actions.find { action -> action.name == actionName }
}
Having an issue with this script?
Report it here