Forums

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

I can't get the comment created in an issueLinkCreatedEvent

Ignacio Flores April 8, 2021

Hi there! with my script I want to replicate the comment entered in the destination issue when I generating a link

The code gives me an error in the following script line:

def lastcomment =event.getComment()

No signature of method: java.lang.String.getComment() is applicable for argument types: () values: []

 

I leave the complete code:

 

import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.jira.event.issue.link.IssueLinkCreatedEvent
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.event.issue.IssueEvent


def log = Logger.getLogger("com.nolddor")
log.setLevel(Level.DEBUG)


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

def event = event as IssueLinkCreatedEvent
def issueLink = event.issueLink
def sourceIssue = event.issueLink.sourceObject
def destinationIssue = event.issueLink.destinationObject
def lastcomment =event.getComment()
def comments = ComponentAccessor.commentManager.getComments(sourceIssue)
String lclc = comments.body.last()
log.debug(lclc)
//def commentAuthor = comments.getAuthorApplicationUser()


if (comments) {
//commentManager.create(destinationIssue, loggedInUser, "sdf", true)
def autor= comments.last().getAuthorApplicationUser()
log.warn(autor)
//commentManager.addCommentToObject()
//commentManager.create(destinationIssue, comments.authorApplicationUser, comments.body, true);
commentManager.create(issueLink.getDestinationObject(), autor, lclc, true)
}


log.debug "An IssueLinkCreate event has been catched"
log.debug "Issue (from): ${sourceIssue.key}"
log.debug "Issue (to): ${destinationIssue.key}"

1 answer

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
April 9, 2021

Hi @Ignacio Flores,

You are encountering the error because there is no getComment() method in the bound variable event.

Instead, it would be best if you used the Comment Manager to get the Comment, i.e.:-

def commentManager = ComponentAccessor.commentManager
.
.
.
def comments = commentManager.getComments(sourceIssue)

 

If you intend to pass the latest comment from the Source Issue to the Destination Issue, you can try this working code:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue

def commentManager = ComponentAccessor.commentManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def issueLink = event.issueLink

def destinationIssue = issueLink.destinationObject as MutableIssue
def sourceIssue = issueLink.sourceObject as MutableIssue

def comments = commentManager.getComments(sourceIssue)
def latestComment = comments.last()

commentManager.create(destinationIssue, loggedInUser, latestComment.body, true)

Please note, this sample code is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a print screen of the Listener configuration:-

listener_config.png

Below are some print screens of the test:-

1) The destination issue before it is linked to the source issue:-

before_link.png

2) The source issue, with the comments:-

source_issue.png

3) The destination issue after it is linked to the source issue with only the latest comment from the source issue:-

after_link.png

 

I hope this helps to solve your question. :)

 

Thank you and Kind Regards,

Ram

Ignacio Flores April 9, 2021

@Ram Kumar Aravindakshan _Adaptavist_ 

Hi there! Thank you very much for your prompt reply friend. Indeed, what you show me I can do, but what I need is that at the moment I am creating the link, at that very moment I am going to add a comment, that comment is the one that I want to be transferred to the destination issue. Not the last comment before creating the link, but the comment entered when generating the link. Excuse me if I don't explain it well

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
April 9, 2021

Hi @Ignacio Flores 

Could you please specify which issue link type do you want to copy the latest comment from?

For example, from the outward link  (blocks) to the inward link  (is blocked by)?

Ignacio Flores April 12, 2021

hi @Ram Kumar Aravindakshan _Adaptavist_ 

From the outward link (Aprueba) to the inward link (Aprobado por)

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
April 18, 2021

@Ignacio Flores 

What you are trying to achieve cannot be performed because the comment that is added in the dialog box when creating the outward link, i.e.:-

link_dialog_box.png

is not committed to the Database at the time the event is fired.

The alternate approach I would suggest is first to add a comment in the comment area, i.e.:-

comment.png

and then link your issue. By making this approach, you can be sure that the Listener will be able to take the last comment and add it to the linked issue.

 

I hope this helps to solve your question. :)

 

Thank you and Kind Regards,

Ram

Ignacio Flores April 21, 2021

@Ram Kumar Aravindakshan _Adaptavist_ 

 

Works for me. Thanks to you!

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
April 21, 2021

Hi @Ignacio Flores

Great!. Since the suggested solution works for you, please accept the answer.

Thank you and Kind Regards,

Ram

Suggest an answer

Log in or Sign up to answer