Set a comment Internal on Service Desk with groovy

René Illner November 25, 2015

Hi all,

I will append a generated internal comment by a subtask-transition-postfunction to the parent-task with the following groovy:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.entity.property.JsonEntityPropertyManager
import com.atlassian.jira.bc.issue.comment.property.CommentPropertyService
import com.atlassian.jira.user.ApplicationUser
import org.apache.log4j.Category
import groovy.json.JsonSlurper

final SD_PUBLIC_COMMENT = "sd.public.comment"
def commentManager = ComponentAccessor.getCommentManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

MutableIssue parentIssue = (MutableIssue)issue.getParentObject()

def parentComment = commentManager.create(
    parentIssue,
    user,
    "{color:red}*Prio1 Escalation Request is approved now*{color} with the following comment:\n\n"+transientVars.comment.toString(),
    false
) 

log.debug " DEBUG: Comment added for parentIssue " + parentIssue.key + ": Comment.id=" + parentComment.id.toString()

// ??? => Don't know the right entityName: "sd.comment.property" or "CommentProperty" (found with commentPropertyService.getProperty..)
// Result of commentPropertyService.getProperty..: commentProperty: EntityPropertyImpl[id=10604,entityName=CommentProperty,entityId=10337,key=sd.public.comment,value={"internal":"true"},
// Next line didn't run 
//JsonEntityPropertyManager.put((ApplicationUser)user, "sd.comment.property", parentComment.id, "sd.public.comment", "{\"internal\":\"true\"}" , (java.util.function.BiFunction) null, false)
// Try with deprecated method with less arguments
//JsonEntityPropertyManager.put("sd.comment.property", (Long)parentComment.id, "sd.public.comment", "{'internal':'true'}")
JsonEntityPropertyManager.put("sd.comment.property", (Long)parentComment.id, SD_PUBLIC_COMMENT, "{'internal':'true'}")

The comment is appended at the parent-issue but the set-to-internal didn't work with the following error:

groovy.lang.MissingMethodException: No signature of method: static com.atlassian.jira.entity.property.JsonEntityPropertyManager.put() is applicable for argument types: (java.lang.String, java.lang.Long, java.lang.String, java.lang.String) values: [CommentProperty, 10372, sd.public.comment, {'internal':'true'}]

Possible solutions: wait(), any(), dump(), grep(), find(), putAt(java.lang.String, java.lang.Object)

Does anyone has an idea how to fix this? Thanks in advance.

Kind regards

René

3 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
René Illner April 18, 2016

Thank you Jamie and Kristian,

I have solved it with the following script:

import com.atlassian.jira.component.ComponentAccessor; 
import com.atlassian.jira.issue.comments.CommentManager; 
import com.atlassian.jira.issue.comments.Comment; 
import com.atlassian.jira.issue.Issue; 
import com.atlassian.jira.issue.MutableIssue; 
import com.atlassian.jira.bc.issue.comment.property.CommentPropertyService; 
import com.atlassian.jira.user.ApplicationUser; 
import com.atlassian.jira.util.json.JSONObject; 
import groovy.transform.Field; 
import org.apache.log4j.Logger; 
import org.apache.log4j.LogManager;

final org.apache.log4j.Logger log4jLog = LogManager.getLogger("com.atlassian.jira.groovy"); 
String logPrefix = "[Approved_Escalation] "; 
@Field ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
 
if ( issue.isSubTask() ) {  
MutableIssue parentIssue = (MutableIssue)issue.getParentObject()
	if( parentIssue ) {
		//add comment to parentIssue   
		Map<String, JSONObject> commentPropertyInternal = new HashMap<String, JSONObject>();
		commentPropertyInternal.put("sd.public.comment", new JSONObject("{\"internal\":\"true\"}"));
		def commentManager = ComponentAccessor.getCommentManager()   
		def parentComment = commentManager.create(
			parentIssue, // Issue issue,
			currentUser, // ApplicationUser author,
			"{color:red}*Issue escalated*{color} (Sub-Task ${issue.key})\n\n*{color:#707070}Comment:{color}*\n{color:red}" + transientVars.comment.toString() + "{color}", //String body,
			null, //String groupLevel,
			null, //Long roleLevelId,
			Calendar.getInstance().getTime(), //Date created,
			commentPropertyInternal, //Map<String,JSONObject> commentProperties,
			true // dispatchEvent)
		)
		log4jLog.debug(logPrefix + "Comment added for parentIssue : " + parentIssue.key + ": Comment.id=" + parentComment.id.toString());
	}
}

Thanks

 René

0 votes
JamieA
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.
November 26, 2015

You need to get the CommentPropertyService - example here: https://scriptrunner.adaptavist.com/latest/jira/recipes/misc/jira-service-desk.html

You're on the right track.

0 votes
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 26, 2015

Hi René,

 

Have you looked at the API documentation here, It looks as if you have specified the parameters incorrectly for the method.

It looks as if the parameters should be:

.put(ApplicationUser user,
       @Nonnull
       String entityName,
       @Nonnull
       Long entityId,
       @Nonnull
       String key,
       @Nullable
       String json,
       com.atlassian.fugue.Function2<ApplicationUser,EntityProperty,? extends EntityPropertySetEvent> eventFunction,
       boolean dispatchEvent)
I hope this helps.

Thanks

Kristian

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events