Update any event change(field change, status change etc) to the 'Relates To" Ticket

Saurabh Patel June 10, 2022

Hi All,

I have a project X which creates a "relates to" ticket(another project Y) for certain conditions on Components.

I need a way(scriptrunner etc.) for adding a comment in project Y ticket for any changes(e.g. field change, Transition change etc. excluding comments) in the "Is Related to" ticket in project X 

1 answer

1 accepted

1 vote
Answer accepted
Marco Franzen June 10, 2022

Hi

you could try to create a ScriptRunner Listener - Custom Listener

 

I would configure it like this:

Applied To: Global or your Project X

Events: Issue Updated & Issue transitioned

Script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue


def commentManager = ComponentAccessor.getCommentManager()
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def userManager = ComponentAccessor.getUserManager()

def sourceProject = "Project X"
def targetProject = "Project Y"
def relatesToLinkId = 10000L // your relates to link id
def validInwardLinks = [relatesToLinkId]
def validOutwardLinks = [relatesToLinkId]

// check if the issue is in project...
if (!sourceProject.equals(event.project.name)) {
log.info("Issue not in project \"${sourceProject}\".")
return
}

def issue = event.getIssue()

// get the linked issues
def linkedIssues = issueLinkManager
.getInwardLinks(issue.getId())
.findAll { it.getIssueLinkType().getId() in validInwardLinks }
.collect { it.getSourceObject() }
linkedIssues += issueLinkManager
.getOutwardLinks(issue.getId())
.findAll { it.getIssueLinkType().getId() in validOutwardLinks }
.collect { it.getDestinationObject() }

// return if there are no linked issues
if (linkedIssues.isEmpty()) {
log.info("No linked issue found for issue \"${issue.getKey()}\".")
return
}

// return is there is not exactly one linked issue, choose one of them...
// if (linkedIssues.size() != 1) {
// log.info("There is not exactly one linked issue for \"${issue.getKey()}\". Skipping synchronisation...")
// return
// }

// a list of fields that you might not want to sync
def fieldsToIgnore = ["description"]


// get the changed fields
def childChangeItem = event?.getChangeLog()?.getRelated("ChildChangeItem")?.findAll { item -> !fieldsToIgnore.contains(item.field) }


if (childChangeItem == null || childChangeItem.isEmpty()) {
log.info("No fields changed for issue \"${issue.getKey()}\".")
return
}


// get the author of the changes
def authorName = event.getUser().getName()
def message = "Some changes were made to issue ${issue.getKey()} by user ${authorName}\r\n\r\n"

// add the field changes to the message
for (changeItem in childChangeItem) {
def fieldName = changeItem.field
def fromValue = changeItem.oldstring
def toValue = changeItem.newstring

message += "Field \"${fieldName}\" was changed from \"${fromValue}\" to \"${toValue}\"\r\n"
}

// the user that is used to add the comment
def automationUser = userManager.getUserByKey("automation_user")

for (linkedIssue in linkedIssues) {
commentManager.create(linkedIssue, automationUser, message, false)
}
Saurabh Patel June 13, 2022

Hi Marco,

This works like a charm! The code did not have a single error or warning that I had to correct.
Many Many Thanks!

Just one thing, I was not able to Find "Issue Transitioned" Event, So I added "All issue events"

Marco Franzen June 13, 2022

@Saurabh Patel Nice to hear that it works!

I tried to avoid using the "All issue events" event because it might result in duplicated comments.
For example, if you change the assignee,  i think an "Issue Updated" and "Issue Assigned" event will be triggered.

I had to translate the Issue Events because my Jira instance is not in english. "Issue Moved" should be the correct translation instead of "Issue Transitioned"

Like Saurabh Patel likes this
Saurabh Patel June 13, 2022

Issue moved works when issue is moved from one project to another.
But Using "Generic Event" also serves the purpose.

Like Marco Franzen likes this
Saurabh Patel June 13, 2022

Hi @Marco Franzen

is there a way, this code can be modified that it also updates the value of the linked issue fields in projext Y, as soon as the value of the same field in the project X changes. I am new to scriptrunner and tried few things but that did not work. 

Marco Franzen June 13, 2022

Yes that should be possible. I haven't tested it, but the code could look something like this:


import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue

...
...
...

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()
def issueManaer = ComponentAccessor.getIssueManager()


for (issueToUpdate in linkedIssues) {
// for system fields... do this for every field you want to sync
def summaryUpdate = childChangeItem?.find {it.field == "summary"}
if (summaryUpdate) {
issueToUpdate.setSummary(summaryUpdate.newstring)
}


// for custom fields... maybe create a list and iterate through the fields you want to sync, or copy past that code for every field
def customFieldUpdate = childChangeItem?.find {it.field == "<your custom field name>"}
if (customFieldUpdate) {
def customField = customFieldManager.getCustomFieldObject("customfield_<ID>").first()
def possibleCFValues = optionsManager.getOptions(customField.getRelevantConfig(issue))
def newCFValue = possibleCFValues.find {it.value == customFieldUpdate.newstring}
issueToUpdate.setCustomFieldValue(customField, newCFValue)
}

issueManaer.updateIssue(event.user, ((MutableIssue) issue), EventDispatchOption.ISSUE_UPDATED, false)
}

 

Links to some part of the documentation:

IssueManager 
CustomFieldManager 
OptionsManager 

 

 

But I wonder why the ticket is not shown directly to the user if you want to sync most of the fields.
If you dont want the users of Project Y to see all issues of Project X, then you could also control this via the "Issue Security". You dont have to answer this question, but I think it might be a bit confusing for new users and jira admins if fields get synced between different projects.

Saurabh Patel June 14, 2022

This script does not seem to work. Can you please tweak the same or test it at your side

Suggest an answer

Log in or Sign up to answer