Blog - Isos Technology

Jira Admin Tip #1: A Groovy Way to Find All Workflows Associated with an Issue Type

Written by Amanda Kirk | Jun 22, 2021

Here at Isos Technology, we've seen use cases for pretty much everything. One use case that has come up for both my clients and myself as an admin has been identifying which workflows are applied to a specific issue type across the instance.Say you want to deprecate an issue type or do some other form of analysis on its usage. If you go to the analyze the issue types in the admin UI of Jira, it will only show you the related issue type schemes for each issue type, so you can't really gain any useful information on which workflows are applied to the issue type from the UI.

So, in this blog post, I will show you how to quickly identify all workflows applied to an issue type across your Jira instance using a simple Groovy script! To run it, just pull up a Groovy console in Jira (note that a Groovy console comes with some typical apps like JMWE and may not be available out-of-the-box).

Let's jump right in: 

 

import com.atlassian.jira.component.ComponentAccessor
   
def schemeManager = ComponentAccessor.workflowSchemeManager
def issueTypeSchemeManager = ComponentAccessor.issueTypeSchemeManager
def sb = new StringBuffer()
 
//enter the issuetype id of the issue type you are searching for here
def issuetypeId = "10103"
//will store the workflows we find associated with your issue type
def workflowsFound = []
   
//iterates over each workflow scheme
schemeManager.schemeObjects.each { scheme->
   
    //iterates over each workflow scheme entity mapping
    scheme.getEntities()?.each { entity ->
 
        //parse the entity mapping to extract the issue type and workflow referenced
        def foundIssuetypeId = (entity.toString().split("=")[2]).split()[0]
        def foundWorkflow = (entity.toString().split("=")[3]).minus("TemplateId")
         
        //if this mapping references our issue type, add to results
        if(foundIssuetypeId == issuetypeId){
         workflowsFound.add(foundWorkflow.trim())
        }
       
        //if isssue type is part of default mapping check if it exists in a project associated w this workflow
        if(foundIssuetypeId == "0"){
          //iteratess over projects associated with this workflow
          schemeManager.getProjects(scheme).each { project ->
            def issueTypes = issueTypeSchemeManager.getIssueTypesForProject(project)
            //iterates over issuetypes in the project
            issueTypes.each {
              //if issuetype used here, add to results
              if(it.id.toString() == issuetypeId){
                workflowsFound.add(foundWorkflow.trim())
              }
            }
          }
        }
       
    }
}
 
//output unique list of workflows
(workflowsFound.unique()).each{
  sb.append(it + "\n")
}
return sb.toString()

 

As you can see from above, line 8 is the only place you need to edit the script to gather the needed information. Enter the issue type ID for the issue type you're searching for in place of the "1" I currently have in there. What the script does is iterate over all workflow mappings (i.e. workflow schemes, which by definition map workflows to issue types in a project) to see which workflows map to the issue type ID you specify.

One big caveat to this method is that workflow schemes will output a value of "0" when evaluating the default workflow mapping of a workflow scheme. So to capture these, we have a second "if" statement to see if we're dealing with a default workflow. If we are, we still need to check if the workflow is actually associated with the issue type you specify, since the projects using this default workflow may not even use the issue type. Let me run you through an example of this edge case for clarity. I can have a workflow scheme shown below:

 

 

If I am searching for all workflows associated with the Change issue type, we would want to detect the CS: JSSD IT Support Workflow (v2) workflow above. As noted, when we iterate over the workflow schemes, the API will return "0" for issue type ID when evaluating this workflow, since it is the default workflow in this workflow scheme (it's not associated with any issue types not specified elsewhere in the workflow scheme mappings). So if all we get from the API is a zero, we know we hit a default mapping but we don't know if the project is actually using the Change issue type! To check this, our code finds all projects using the workflow and makes sure at least one of them is using the Change issue type, in this example.

After our code finds all the workflows utilizing your specified issue type, we remove any duplicates from our list and output it to the console. Here's an example of output from my script above:

 

WORKFLOWS FOUND: 
ABOOK: Simple Issue Tracking Workflow
CS: JSD IT Support Workflow (v2)
Marketing Event Workflow
Simple Issue Tracking Workflow
Simple Issue Tracking Workflow w/Selected for Development
Simple Issue Tracking Workflow w/planning and hold

 

And voilĂ ! It's as simple as that! I hope these script snippets can help speed your admin tasks or client needs along. And as always, happy automating!