Last Comment issue clone

Isabel Fonseca
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
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

 

 

2 answers

2 accepted

0 votes
Answer accepted
Isabel Fonseca
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.
June 13, 2023

Hellow @Graham Twine,

I apologize for the delay in replying.

Don't works, so i abandoned the idea and think in another solution.

State point

1- This listener it's Ok

// Events: Issue Commented; Issue Comment Edited; Issue Comment Deleted. And have a custom Field "Last Comment New Version":

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 New Version")
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())
}
2- Problem
When More\Clone in issue A, search for issues fails because in the list resulting from the search, in the column “Last Comment New Version” the last comment appears in issue B but referring to issue A. It should appear empty, because issue B has no comments.
Capture.GIF
Capture_B.GIFCapture_A.GIF
3- Solution NEW
Create another Listenner , one Event: Issue Created.
Goal: If one issue created, verify if is a clone. If so, clean de custom Field "Last Comment New Version". And this is it.
Script I created
What am i doing wrong? The script does not detect that it is a clone:Capture_Script.GIF
Log:
Capture_Logs.GIF
3- Others
Capture.GIFCapture2.GIF
Not found clones (issueLink).
 
Best regards,
Isabel Fonseca
Isabel Fonseca
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.
June 14, 2023

I already managed to solve:

Event: "issueLinkCreated" instead of "Issue Created"

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.link.IssueLinkCreatedEvent
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.user.ApplicationUser
import groovy.transform.Field
import com.atlassian.jira.issue.search.SearchException
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Level

final field = 'Last Comment New Version'

final String LINK_TYPE = 'Cloners'

// Set log level to INFO
log.setLevel(Level.INFO)

// Some components

def watcherManager = ComponentAccessor.watcherManager
def customFieldManager = ComponentAccessor.customFieldManager
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def searchService = ComponentAccessor.getComponentOfType(SearchService)
def issueService = ComponentAccessor.issueService
def issueManager = ComponentAccessor.issueManager

if (event instanceof IssueLinkCreatedEvent) {
    def issueLink = (event as IssueLinkCreatedEvent).issueLink

    if (!isApplicable(issueLink)) {
        return
    }

    def sourceIssue = issueLink.sourceObject // service management issue
    def destinationIssue = issueLink.destinationObject // core or software issue


    def customFieldObjectLastCommentId= customFieldManager.getCustomFieldObjectsByName(field).first()

    def issue1 = issueService.getIssue(user,sourceIssue.getId()).getIssue()
    def issue2 = issueService.getIssue(user,destinationIssue.getId()).getIssue()
   
    log.info("Found    Clone DE: ${issue2.key} Para: ${issue1.key} ")

    issue1.setCustomFieldValue(customFieldObjectLastCommentId,"")  

    issueManager.updateIssue(user,issue1,EventDispatchOption.DO_NOT_DISPATCH,false)
 
}





Boolean isApplicable(IssueLink issueLink) {
    def requiredLinkType = issueLink.issueLinkType.name == LINK_TYPE
    log.info("requiredLinkType ${requiredLinkType} Name ${issueLink.issueLinkType.name} ")
    requiredLinkType
}
Many thanks,
Best regards,
Isabel Fonseca
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}"
Isabel Fonseca
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 29, 2023

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

Like Collista Lewingdon likes this

Suggest an answer

Log in or Sign up to answer