Search code examples
jirascriptrunner-for-jira

Jira Server: Button for creating a linked issue


In our project, one Jira issue of type Requirement is linked to n issues of type Functional Specification (FS). What I need is a button on a Requirement issue to create a FS issue and then link it automatically in a certain way to the Requirement issue.

By the way, we are still using Jira Server. We will probably move to Jira Cloud in some years (because Jira Server is discontinued), but I guess we will need to resolve this problem again when it happens.

Approaches:

  • There is a “Constrained create issue dialog” fragment in Adaptavist ScriptRunner. With this I can have a “create FS” button which works as it creates an issue with the specified type. But how can I link the new issue to the original one?
  • There is a “Clones an issue, and links” listener in Adaptavist ScriptRunner which can be specified to do exactly what I want including the correct linking. Is it possible to connect it somehow to a button? Currently I am listening to if a new comment body equals "createFS", which is not really an ideal solution. But at least I am able to delete the comment automatically. Condition: issue.issueType.name == 'Requirement' && event.getComment().getBody().equals('createFS') && com.atlassian.jira.component.ComponentAccessor.getCommentManager().delete(event.getComment())

Solution

  • Solution using Adaptavist ScriptRunner for Jira

    First step: a fragment to create the issue

    Jira administration → Manage apps → Fragments → Create Script Fragments → Constrained create issue dialog with

    Name Value Remark
    What section should this go in operations-work to have the button respectively menu item it in the “More” menu of the current issue
    Key create-linked-fs needed to be referenced in the second step
    Weight 1 Place in the menu in case you specify more custom buttons
    Condition issue.issueType.name == 'Requirement' matching type of current issue
    Issue Type Functional Specification type of the new issue

    Second step: a behaviour to link the two issues

    Behaviours → Add Behaviour

    • Initialiser:
    import com.atlassian.jira.component.ComponentAccessor
    
    def issueManager = ComponentAccessor.getIssueManager()
    
    if (getBehaviourContextId() == "create-linked-fs") {
        getFieldById("project-field").setReadOnly(true)
        getFieldById("issuetype-field").setReadOnly(true)
        def contextIssue = issueManager.getIssueObject(getContextIssueId())
        getFieldById("issuelinks-linktype").setFormValue("trace up to").setReadOnly(true)
        getFieldById("issuelinks-issues").setFormValue(contextIssue.key).setReadOnly(true)
    }
    
    • Mapping: Select which project(s) and issue type(s) apply.