<img height="1" width="1" style="display:none;" alt="" src="https://px.ads.linkedin.com/collect/?pid=299788&amp;fmt=gif">

Cleaning Up Schemes and Workflows with Groovy

Atlassian, Jira

When it's time to audit your Jira instance to search for areas of improvement or cleanup, the findings can be unsettling... and even getting to the findings themselves can be a pain. Going through your Jira instance manually, searching for schemes that can be removed or consolidated is not one of the funnest tasks a Jira Admin can undertake. Luckily, there are always ways to automate tedious processes and I am going to tell you about one of them today.

First off, there will be a time investment in automation, as there normally is. The time it takes to understand and implement the automation may take longer than it would to manually go through and check what needs to be cleaned up, at least for the first time around. In the long run, though, it will save you tons of time on these repetitious admin tasks. Once you automate a process, you will be able to reuse the automation for subsequent audits and cleanups, making the previously daunting tasks more efficient and seamless than ever!

The groovy script I am going to show you can be customized or expanded upon to meet your specific needs, but to exemplify how simple it can be to automate some cleanup, I will start my sharing how to identify and delete unused workflows. Let's take a look!

import com.atlassian.jira.component.ComponentAccessor
ef workflowManager = ComponentAccessor.workflowManager
def schemeManager = ComponentAccessor.workflowSchemeManager
def sb = new StringBuffer()
workflowManager.workflows.each {
//for each worklow, get the schemes associated with it
def schemes = schemeManager.getSchemesForWorkflow(it)
//if not used in any scheme, delete the workflow
if(schemes.size() == 0) {
sb.append("Deleting workflow: ${it.name}\n")
//workflowManager.deleteWorkflow(it)
}
}
return sb.toString()

In the code above, we begin by utilizing the workflowManager to iterate over all workflows in our Jira instance. We then use the the workflowSchemeManager  to access the schemes associated with each of those workflows. If we find that a workflow is associated with no schemes, showing that it is not being used, then we delete that workflow. As simple as that!

Warning!

Be very careful when using any line of code that deletes items from your Jira instance. Always test that the items returned by your code are indeed what should be deleted before adding in the line that does the deleting! You will see we have our line that does the actual deletion commented out for now.

Now let me walk you through how you would go about figuring out how to automate the cleanup of even more items in Jira! Looking into accessing other data using the Jira API, I was able to find out how to easily clean up issue type schemes, as well! I started by looking into the IssueTypeSchemeManager to see what methods were available. My aim here was to determine what projects each issue type scheme was tied to. If an issue type scheme was not associated with any projects, then it would be safe to delete since it was not being used anywhere. Below is a screenshot of the IssueTypeSchemeManager documentation:

From there, I found the getAllSchemes() function, which allows me to iterate through all of the issue type schemes in my Jira instance. To figure out how I would then work with that list of issue type schemes, I clicked on the FieldConfigScheme object that the getAllSchemes() function returns to find out more about how to work with these objects. Again, all of this is found in the documentation:

Since our goal is to get the list of projects associated with the issue type scheme, you can easily see in the documentation that there is a method called getAssociatedProjectObjects() that should do the trick! Here is the information I found on this method:

Aha! This method indeed returns the projects associated with the issue type scheme in question. Thus, so far what we have is the following code, which should print all the issue type schemes associated with zero projects:

import com.atlassian.jira.component.ComponentAccessor
def issueTypeSchemeManager = ComponentAccessor.getIssueTypeSchemeManager()
def sb = new StringBuffer()
issueTypeSchemeManager.getAllSchemes().each {
def projects = it.getAssociatedProjectObjects()
if(projects.size() == 0) {
sb.append("Found empty issue type scheme: ${it.getName()}\n")
}
}
return sb.toString()

The output of this script gave me the following list of projects, which were supposedly unused:

Found project: Default Issue Type Scheme
Found project: IMSD: JIRA Service Desk Issue Type Scheme
Found project: SAFEPORT: Kanban Issue Type Scheme

But wait....the Default Issue Type Scheme shows up! Clearly this issue type scheme should not be removed, so we know there is something off or missing from our code.

Looking at the issue type schemes within Jira, I then noticed that instead of listing any projects using the Default Issue Type Scheme, since this is a global scheme, it just says "Global (all unconfigured projects)" instead of listing any projects. So it would make sense that our code would not return any project objects for this issue type scheme.

This is, of course, something we want to account for in our code, so let's add an exception for global issue type schemes so that we do not detect them as "unused". We can accomplish this by adding the following code to our condition (which, again, was easily found by looking at the methods available to these objects in the documentation):

import com.atlassian.jira.component.ComponentAccessor
def issueTypeSchemeManager = ComponentAccessor.getIssueTypeSchemeManager()
def sb = new StringBuffer()
issueTypeSchemeManager.getAllSchemes().each {
def projects = it.getAssociatedProjectObjects()
if(projects.size() == 0 && !(it.isGlobal())) {
sb.append("Deleting empty issue type scheme: ${it.getName()}\n")
//issueTypeSchemeManager.deleteScheme(it)
}
}
return sb.toString()

This code gives us what we need by ignoring global issue type schemes! You can see now that the code returns only the unused issue type schemes, and these can safely be removed:

Found project: IMSD: JIRA Service Desk Issue Type Scheme
Found project: SAFEPORT: Kanban Issue Type Scheme

Conclusion

I hope that this walkthrough into automating some Jira cleanup tasks has been useful and helped spark some curiosity. If you get inspired and want to investigate automating more cleanup tasks, I suggest you peruse the documentation, looking into how to access information on other artifacts in Jira such as issue type schemes, screens, or permission schemes. Not only is automation a good time investment, but it can be one of the funnest ways to spend your time at work, solving problems while also using your creativity! Remember to test your code thoroughly, as we found above, some edge cases may help you discover attributes of objects that you would otherwise be unaware of and sufficient testing ensures your code is behaving as expected. Happy Automation, everyone!

Managing JIRA at Scale White Paper

TAGS: Atlassian, Jira

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Subscribe to Our Newsletter

Recent Blog Posts