Background Image

Scripts

A number of script snippets can be found on this page, which can be useful for copy/pasting repetitive pieces of code on JIRA and other systems.

ScriptRunner - Set up Logging

Set up logging on ScriptRunner. Can be used in Post Functions and used to see status through the Edit Workflow views.

Insert the following script snippet at the beginning of the Scriptrunner Script to enable logging:

import org.apache.log4j.Category;
def Category log = Category.getInstance("com.onresolve.jira.groovy");
log.setLevel(org.apache.log4j.Level.DEBUG);
log.debug("First Log Line");

The following is shown in the 'View Post Functions' page when editing or viewing a Workflow:

2016-07-21 14:51:56,521 DEBUG [jira.groovy]: First Log Line

ScriptRunner - Add a VIP Flag to customers using a Scripted Field

Add a VIP Flag to users or customers in a certain group in the JIRA User Directory or AD to prioritise users who need fast support.

Insert the following script snippet on the Scripted Field page and set the Renderer to HTML:

// This is using the group 'vip-customers' in JIRA/AD
// Make sure you check if the user exists first (if you allow unknown reporters.
if(userUtil.getGroupNamesForUser(reporter.name).contains("vip-customers")) {
    return "VIP";
}

If the person is in the group, a VIP Flag is shown in the field as follows:

ScriptRunner - Comment on a JIRA Issue in a Post Function

Allows you to place a comment on an issue during a transition using a Post Function.

Place the following in a Custom Post Function script, additional issue action script or in a Listener etc.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.JiraAuthenticationContext

def commentManager = ComponentAccessor.getCommentManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
commentManager.create(issue, user, "Approved", true)

A comment will be added under the user that initiated the transition.

-
Powershell - Delete files older than X days old

Use this script snippet to delete files in a folder that are older than a predefined amount of days. This works best as a scheduled task in Windows to help with common maintenance tasks.

Insert the following script snippet into a 'ps1' file and use your task scheduler to run it on a schedule. You can change the 'AddDays(-5)' to a different number of days previous to filter the deletion.

Get-ChildItem –Path  “D:\JIRA_HOME\export\” –Recurse | Where-Object CreationTime –lt (Get-Date).AddDays(-5) | Remove-Item

The files will be removed and the result will be logged. You can test the script by appending '–WhatIf' to the end of the statement in a command line window.

-
ScriptRunner - Delete all inactive Workflow Schemes and Workflows

If you want to clean up JIRA by removing Workflow Schemes which have no mapped Projects and Workflows which have no mapped Workflow Schemes, it can take a very long time to remove them manually.

Using this script will do it in seconds.

Please be careful as this will remove any of the inactive workflows and schemes.

Place the following script in the Script Console in ScriptRunner and press Run.

import com.atlassian.jira.component.ComponentAccessor
def sb = new StringBuffer()

def workflowSchemeManager = ComponentAccessor.workflowSchemeManager
def workflowManager = ComponentAccessor.workflowManager

workflowSchemeManager.assignableSchemes.each {
    if(!it.isDefault() && !workflowSchemeManager.isActive(it)) {
        sb.append("Deleting Workflow Scheme: ${it.name}\n")
        workflowSchemeManager.deleteWorkflowScheme(it)
    }
}

workflowManager.workflows.each {
    if(!it.isSystemWorkflow()) {
        def schemes = workflowSchemeManager.getSchemesForWorkflow(it)
            if(schemes.size() == 0) {
                sb.append("Deleting workflow: ${it.name}\n")
                workflowManager.deleteWorkflow(it)
            }
        }
}

return sb.toString()

The Workflows will be printed as they are deleted.

Checks are in place to prevent any deletion of a system or active element.

-

© Jack Graves 2016