Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Trying to create a listener to update fix versions of issues linked to a story

Eric Sebian
Contributor
June 18, 2020

I attempted to recreate a script that Tarun Sapra showed in one of his videos to update fix versions of issues linked to a story. I copied it word for word but it does not worked. it's not working at all. I'm hoping that Tarun will be so kind as to shed some light on the possible issue. I added logging info and I see in the logs when it finds an issue that's a story, but when it gets to logs.info "*****change = $change" is says that it's null. Please advise.

 

import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.issue.link.IssueLinkManager;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.event.type.EventDispatchOption;
import org.apache.log4j.Logger
import org.apache.log4j.Level

def logs = Logger.getLogger("com.onresolve.scriptrunner.runner.ScriptRunnerImpl")
logs.setLevel(Level.INFO)

def issue = event.issue

if(issue.issueType.name == "Story") {
logs.info "*****Issue $issue is a story"
def projectKey = issue.projectObject.key;

def change = event?.getChangeLog()?.getRelated("ChildChangeItem").find {it.field == "Fix Version/s"}
logs.info "*****change = $change"
//def change_issue = issue?.getChangeLog()?.getRelated("ChildChangeItem").find {it.field == "Fix Version/s"}
//logs.info "*****change_issue = $change_issue"
def issueLinkManager = ComponentAccessor.issueLinkManager;
def issueManager = ComponentAccessor.issueManager;

if(change) {
issueLinkManager.getOutwardLinks(issue.getId()).each {IssueLink issueLink -> if (issueLink.getIssueLinkType().getName() == "Epic-Story Link") {

def destinationIssue = (MutableIssue)issueLink.getDestinationObject();
if(destinationIssue.projectObject.key.equals(projectKey)) {

destinationIssue.setFixVersions(issue.fixVersions)
logs.info "*****setting up fixVersions $issue.fixVersions"
issueManager.updateIssue(event.getUser(), destinationIssue, EventDispatchOption.ISSUE_UPDATED, false)
}
}
}
}
}

 

1 answer

0 votes
Paul Stallworth (Denver)
Community Champion
June 18, 2020

I'm assuming you're trying to run this from a Listener since you're using the event binding.  I dropped this into the editor for a Customer Listener and was able to resolve all the missing dependencies.  You don't get a binding for everything, I think just issue and event in this case.  So I don't think MutableIssue nor IssueLink are available (at least they were throwing errors on my end).  Same for ComponentAccessor and others.  The code below at least doesn't throw any warnings or errors, hopefully it helps you get a little further down the path.

import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.issue.link.IssueLinkManager;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.event.type.EventDispatchOption;
import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("com.onresolve.scriptrunner.runner.ScriptRunnerImpl")
log.setLevel(Level.INFO)

def issue = event.issue

if(issue.issueType.name == "Story") {
def projectKey = issue.projectObject.key;

def change = event?.getChangeLog()?.getRelated("ChildChangeItem").find {it.field == "Fix Version"}
def issueLinkManager = ComponentAccessor.issueLinkManager;
def issueManager = ComponentAccessor.issueManager;

if(change) {
issueLinkManager.getOutwardLinks(issue.getId()).each {IssueLink issueLink -> if (issueLink.getIssueLinkType().getName() == "Epic-Story Link") {

def destinationIssue = (MutableIssue)issueLink.getDestinationObject();
if(destinationIssue.projectObject.key.equals(projectKey)) {

destinationIssue.setFixVersions(issue.fixVersions)
log.info "setting up fixVersions $issue.fixVersions"
issueManager.updateIssue(event.getUser(), destinationIssue, EventDispatchOption.ISSUE_UPDATED, false)
}
}
}
}
}

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
7.13.5
TAGS
AUG Leaders

Atlassian Community Events