JIRA:Automatically Linking Issues with a Script During Create Transition

We want to follow which issues found in which test and review sessions. In order to do this we have defined 2 new issue types called “Review Task”  and “Test Task”. We have also defined a new relation called “found in”.  As a  improvement we also do not want to manually link issues found during a test or review to its parent task. To make the process a little bit strait forward we have added a new custom field called “Parent Task ID” to our bug issue type. While creating a bug we just enter the parent task id to this field and a workflow post function automatically links two issues with each other. Also we check validly of parent task id field with another workflow validation script. The only problem is we do not have autocomplete for parent issue id field. Any improvement is welcome.

  • First of all you should install Script Runner plugin. This plugin allows you to customise your workflows with groovy scripts without writing a whole custom plugins.
  • Define a parent issue type and customise it in the way you want. We use Review Task and Test Task issue types.
  • Define a custom field named “Parent Task ID” and add it to every other issue type that you want to automatically associate with a parent issue type (the ones you have defined above).
  • For the Create transition of workflow add following validator to validate correctness of entered “Parent Task ID” field.
    def parentTaskID = cfValues['Parent Task ID'];
    if (parentTaskID == null || parentTaskID == '') {
        return true;
    } else {
        def parentIssue = ComponentAccessor.getIssueManager().
    		getIssueObject(parentTaskID);
        def isValid = parentIssue != null && 
             parentIssue.projectObject.key == issue.projectObject.key &&
              (parentIssue.issueObject.name == 'Review Task' || 
                parentIssue.issueObject.name == 'Test Task');
        return isValid;
    }

  • Create a linkIssues.groovy file with below scrip content and put it to home folder of your JIRA installation. For the default installation on windows it is the bin folder. After that add script post function to invoke linkIssues.groovy file for Create transition. As you see in the last screen shoot this step should come after “Create the issue originally” step otherwise you will not have an issue id for linking.
    def ComponentManager componentManager = ComponentManager.getInstance();
    def customFieldManager = componentManager.getCustomFieldManager();
    def parentTaskIDCF = customFieldManager.
            getCustomFieldObject('customfield_10517');//write your own custom field id here
    def parentTaskID = issue.getCustomFieldValue(parentTaskIDCF);
    if (parentTaskID == null || parentTaskID == '') {
        return true;
    } else {
        def issueManager = ComponentAccessor.getIssueManager();
        def parentIssue = issueManager.getIssueObject(parentTaskID);
        def isParentIssueValid = parentIssue != null && 
                parentIssue.projectObject.key == issue.projectObject.key && 
                (parentIssue.issueObject.name == 'Review Task' || 
                        parentIssue.issueObject.name == 'Test Task');
        if (isParentIssueValid) {
            def issueLinkManager = ComponentAccessor.getIssueLinkManager();
            def issueLinkTypeManager = componentManager.
                    getComponentInstanceOfType(IssueLinkTypeManager.class);
            def issueLinkType = issueLinkTypeManager.
                    getIssueLinkTypesByName('Found In').get(0);
            def authenticationContext = componentManager.
                    getComponentInstanceOfType(JiraAuthenticationContext.class);
            issueLinkManager.createIssueLink(
                    issue.id, parentIssue.id, issueLinkType.id, 1L, authenticationContext.user);
        }
    }

fdfafa