Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,560,636
Community Members
 
Community Events
185
Community Groups

Last Comment issue clone

Good afternoon,
I have a situation in jira in Listener, which I need help with.
Current solution ScriptRunner\ Listenner (Events: Issue Commented, Issue Comment Edited, Issue Comment Deleted):
 import com.atlassian.jira.component.ComponentAccessor

import org.apache.log4j.Level

import com.atlassian.jira.issue.ModifiedValue

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

// Set log level to INFO

log.setLevel(Level.INFO)

def issue = event.issue

def commentManager = ComponentAccessor.commentManager

def comment = commentManager.getLastComment(issue)

def customFieldManager = ComponentAccessor.customFieldManager

// Get the custom field

def customField = ComponentAccessor.customFieldManager.customFieldObjects.findByName("Last Comment")

if (comment) {

comment.body

customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), comment.body), new DefaultIssueChangeHolder())

} else {

customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), ""), new DefaultIssueChangeHolder())

}
Test failure: Basically, when I have issua A with comment(s), and I clone it, it creates issue B.
PROBLEM: When I search for issues, in the list resulting from the search, in the column “Last Comment” the last comment appears in issue B but referring to issue A. It should appear empty, because issue B has no comments.
I've already investigated and found some solutions, but it ends up generating other problems in the combinations of possible actions on comments.
Resolution attempt:
 import com.atlassian.jira.component.ComponentAccessor

import org.apache.log4j.Level

import com.atlassian.jira.issue.ModifiedValue

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

import com.atlassian.jira.issue.search.SearchException

// Set log level to INFO

log.setLevel(Level.INFO)

def issue = event.issue

def customFieldManager = ComponentAccessor.customFieldManager

// Get the custom field 

def customField = ComponentAccessor.customFieldManager.customFieldObjects.findByName("Last Comment")

def clonedIssues = ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getId())?.findAll {it.issueLinkType.outward == "clones"}

try {

if (!clonedIssues) {

log.warn('CLONE no.') 

def commentManager = ComponentAccessor.commentManager

def comment = commentManager.getLastComment(issue)

if (comment){ 

customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), comment.body), new DefaultIssueChangeHolder())

} else {

customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), ""), new DefaultIssueChangeHolder())

}        

} else {

log.warn('CLONE yes.') 

def commentManager = ComponentAccessor.commentManager

// fails on next line if it comes from clone. Because it brings comment from the original issue, not the clone !!!

def comment = commentManager.getLastComment(issue)

if (comment){ 

customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), comment.body), new DefaultIssueChangeHolder())

} else {

customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), ""), new DefaultIssueChangeHolder())

}        

}

 

} catch (SearchException e) {

e.printStackTrace()

null

}
I can't identify this “clone” action to load comment with nothing (because it catches issue A and not B, line 70 in Resolution attempt )
Do you have any suggestions?
Best Regards,
Isabel Fonseca

 

 

1 answer

1 accepted

0 votes
Answer accepted
Graham Twine
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 26, 2023

Hello @Isabel Fonseca ,

 

def comment = commentManager.getLastComment(issue)

 

I think you are looking to inspect the cloned issue.

issue is coming from the original issue at event.issue. This is correct issue is the original issue and not the cloned  issue.

def comment = commentManager.getLastComment(clonedIssues.get(0))

 

The example I have shared above is a bit naive as it blindly grabs the first issue from the list.

clonedIssues is a collection of issues that contains 0 to n  depending on the number of outward  links from the  original issue of type "clones" as defined by "findAll"

 

This may be fine for your use case but it may be worth logging the result

def clonedIssues = ComponentAccessor
.issueLinkManager
.getOutwardLinks(issue.getId())?
.findAll {it.issueLinkType.outward == "clones"}
log.info("Found ${clonedIssues?.size()} clones with outward links to issue ${issue.key}"

I'll look into it, thank you very much.

Like Collista Lewingdon likes this

Suggest an answer

Log in or Sign up to answer