Forums

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

Scriptunner, create a listener that will update description with latest comment

Sharif Ismail August 29, 2020

I'm trying to create a listener that will update description with latest comment. Below are my codes.

when the listener triggered, the description are still not updated with the last comment 

import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.bc.issue.IssueService.UpdateValidationResult
import com.atlassian.jira.bc.issue.IssueService

//Get some component
IssueManager im = ComponentAccessor.getIssueManager()
IssueService issueService = ComponentAccessor.getIssueService();
MutableIssue issue = im.getIssueObject("DP-30")

if(issue){

//Fetch latest comment
def commentManager = ComponentAccessor.commentManager
def comment = commentManager.getLastComment(issue)



if (comment) {
comment.body
issue.setDescription(comment.body)
}



}

else {

return "Issue doesn't exist"
}

 

2 answers

1 accepted

2 votes
Answer accepted
Kevin Johnson
Community Champion
August 30, 2020

@Sharif Ismail Hope this could help. Create a Listener like thisupdate.PNGUse this code to Update the Description with Author name, Time&Date, Comment body

import com.atlassian.jira.component.ComponentAccessor

// Get some components
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def issueService = ComponentAccessor.issueService

// Set variables
def issue = event.issue
def issueId = issue.id
def comment = event.getComment()
def commentBody = event.getComment().getBody()

def issueInputParameters = issueService.newIssueInputParameters()

if(issue.getDescription() != null){
issueInputParameters.setDescription(issue.getDescription()+"\n"+ comment.getAuthorFullName() + " - " + comment.getCreated() +"\n"+ commentBody);
}
else{
issueInputParameters.setDescription(comment.getAuthorFullName() + " - " + comment.getCreated() +"\n"+ commentBody);
}

def updateValidationResult = issueService.validateUpdate(loggedInUser, issueId, issueInputParameters)

assert updateValidationResult.isValid() : updateValidationResult.errorCollection

def issueResult = issueService.update(loggedInUser, updateValidationResult)

assert issueResult.isValid() : issueResult.errorCollection
Sharif Ismail August 30, 2020

Thank you kevin johnson , it works like a charm

I have a few questions:

1.I'm new towards scriptrunner , so each time I want to try out the features I would test my codes in the console , for your codes I did the same , but there was lot of errors . But when I tried it in the listener , It doesn't have any problem and works finely. Do you know why ?

2.Starting from the line of declaring updateValidationResult as a variable until the last line of the your code, correct me if im wrong but from my understanding what you did is trying to validate each changes of any field in issue . right ?

why and when should I use this validation process ? 

Kevin Johnson
Community Champion
August 30, 2020

1. I'm assuming that you mean you have tried this in the console of Scriptrunner Plugin, the thing is, the code works in this Listener mainly with something known as the event, since the event is not valid in the console, it throws an error, if you paste the code in the console, you can clearly see the errors starting to show off only from the event-part.

2. Almost correct, here in the updateValidationResult the parameters given inside are checked if they are valid and updated to the issue, if not to throw an error using assert, once the parameters are valid, the issue gets updated and the result is validated.

You can use validation as a precaution where ever you feel like there might be an error. 

0 votes
Nic Brough -Adaptavist-
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.
August 30, 2020

Out of curiosity, why?

Why would you want to do this horrible thing to your users?  If you genuinely do have some strange need to stuff all the commentary on an issue into the description, why not just turn off the permission to comment and train your users to edit the description when they want to add a comment?

Sharif Ismail August 30, 2020

I'm a newbie towards scriptrunner , as I don't have any on going project in JIRA. I try to simulate some situation for me to exercise and practice on Scriptrunner, so this is one of my simulation sir

Suggest an answer

Log in or Sign up to answer