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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

@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

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 ? 

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

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?

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
TAGS
Community showcase
Posted in Jira Core

How to manage many similar workflows?

I have multiple projects that use variations of the same base workflow. The variations depend on the requirements of the project or issue type. The variations mostly come in the form of new statuses ...

4,574 views 12 8
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you