October 7, 2022

ScriptRunner Listener for IssueLinkCreatedEvent or IssueLinkDeletedEvent: no current issue?

I have struggled a bit with implementing a Link Listener with Jira / Scriptrunner and share my learning here.

Scenario

I wanted to implement a Listener triggered when a link is changed in an issue.
I have used as shown below IssueLinkDeletedEvent and IssueLinkCreatedEvent.

You can get the 2 issues for the updated link with

Issue destinationIssue = event.getIssueLink().getDestinationObject()
Issue sourceIssue = event.getIssueLink().getSourceObject()

or

def issue = event.issueLink.sourceObject

The Source is the issue on the starting side of the Inward link - independently from the edited original issue.

For example for a Test Link Test Case (source)<-> Requirement (destination) (Inward:tests; Outward: is tested by) 

The question which issue is triggering the issuelink event as in the Atlassian Forum How to get current issue when I use IssueLinkCreatedEvent event or IssueLinkCreatedEvent doesn't have either parent issue or issue-link type information is irrelevant because a link can be created from both sides.

To safe-proof the event and link direction I have implemented in the listener some consistency checks regarding the link types and link direction as shown below:

// check for IssueLinkType Name= Test - if no Test, exit
if (!event.issueLink.issueLinkType.name.equals("Tests")) {
    mylog.debug "Link not of Type Tests but ${event.issueLink.issueLinkType.name}->exit"
    return
}

Issue issue = event.getIssueLink().getSourceObject()

// check for SourceIssue Type = Test - if no, exit (wrong link direction chosen)
if (!issue.issueType.name.toLowerCase().equals("test")) {
    mylog.warn "Source issue ${issue.key} not of Type Test but ${issue.issueType.name} ->exit"
    return
}

Issue destinationIssue = event.getIssueLink().getDestinationObject()
// check for Destination Issue Type = Requirement - if no, exit
if (!destinationIssue.issueType.name.toLowerCase().contains("requirement")) {
    mylog.warn "Destination issue ${destinationIssue.key} not of Type Requirement but ${destinationIssue.issueType.name} ->exit"
    return
}

See also

IssueLinkCreatedEvent doesn't have either parent issue or issue-link type information

Solved: How to get current issue when I use IssueLinkCreat...

No comments:

Post a Comment