Commenting collection of issues

Andrew Pechnikov
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.
December 26, 2012

Good day time dear collegues!

Developing jira plugin at the moment. So i need to comment Collection of issues. I use Comment manager and my code is pretty easy:

List<Issue> issues = results.getIssues();
Iterator<Issue> issuesIt = issues.iterator();
String gotCommentBody = "Start";
SimpleErrorCollection error = new SimpleErrorCollection();
while(issuesIt.hasNext())
{
	Issue issue = issuesIt.next();
	if(gotCommentBody!="")
	{
		CommentManager cm =  ComponentManager.getComponentInstanceOfType(CommentManager.class);
		Comment comment = cm.create(issue, "victor", gotCommentBody, false);// issue, author, text, throw event
	}
}

And i've got an error here!
The first run of the cycle completes correctly and comments issue
At the second run of the cycle cm.create method throws "Cannot set or mutate an ImmutableGenericValue"
Does anyone one now what object is immutable? And why it only appears on the second run?
Thank you in advance!

Regards, Andrew.

4 answers

1 accepted

0 votes
Answer accepted
Andrew Pechnikov
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.
January 20, 2013

The problem was solved. I didn't undestand the reason. May be user object wasn't saved correctly or i just needed to restructurise my code.

3 votes
Denis Yaparov February 19, 2014

Hi,

Just met and solved the same issue. Adding this for the future.

The problem is your old code to try add a comment to a DocumentIssueImpl, returned by Lucene search. Jira adds a new comment, but after it Jira calls IssueImpl.setUpdated, and this operation is forbidden for read-only class.

The solution is simple - get the real issue from DB before adding a comment:

Issue issueDoc = issuesIt.next();
Issue issue = issueManager.getIssueObject(issueDoc.getId());

CJ Edwards August 8, 2019

This solved my problem as well, thanks!

0 votes
dleng
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.
December 27, 2012

Have you tried calling this after creating the comment:

cm.update(comment, false);

cm.update will attempt to persist the comment changes in the database.

Not sure why you are getting ImmutableGenericValue errors, since you are creating new comments, and not updating existing comments. (In which case you will need to call getMutableComment to get a mutable object of the comment that you can use to call update methods)

Andrew Pechnikov
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.
December 27, 2012

Daniel, thank you very much for your response. I tried to update comment but the result is absolutely the same:(

dleng
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.
December 27, 2012

I moved the CommentManager create out of the loop. Doesn't seem like you need to keep recreating it for each comment you want to create. Efficiency :)

dleng
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.
December 27, 2012

Ok then how about this:

CommentManager cm =  ComponentManager.getComponentInstanceOfType(CommentManager.class);
while(issuesIt.hasNext())
{
    Issue issue = issuesIt.next();
    if(gotCommentBody!="")
    {
        Comment comment = cm.create(issue, "victor", " ", false);
        MutableComment mComment = cm.getMutableComment(comment.getId());
        mComment.setBody(gotCommentBody);
        cm.update(comment, false);
    }
}

Still cannot guarantee that it works. But its worth a shot. If cant work you may try changing cm.update (mComment, false) instead. Also, not sure if that will work. ):

Andrew Pechnikov
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.
December 27, 2012

Thank you, i tried a litlle modified version of this code, now tried yours. Nothing changed. I can't underst and why the second iterarion takes comment as immutable value. And the programm stops working on this line Comment comment = cm.create(issue, "victor", " ", false);
So it's useless to change the other ones) May be i need somehow to clear smth's memmory or cache?

Priyanka Lavania
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 10, 2015

Hi, I am facing similar issue though I am able to add the comment but just getting Exception. The plugin failed exception java.lang.UnsupportedOperationException: Cannot set or mutate an ImmutableGenericValue java.lang.UnsupportedOperationException: Cannot set or mutate an ImmutableGenericValue Code Snippet: commentManager.create(issuecnt, user, comment, false); and if I try to change Issue object to MutablIssue then I get below exception and comment is also not created: The plugin failed exception java.lang.ClassCastException: com.atlassian.jira.issue.DocumentIssueImpl cannot be cast to com.atlassian.jira.issue.MutableIssue java.lang.ClassCastException: com.atlassian.jira.issue.DocumentIssueImpl cannot be cast to com.atlassian.jira.issue.MutableIssue Code Snippet: commentManager.create(((MutableIssue) issuecnt), user, comment, false);

0 votes
Andrew Pechnikov
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.
December 26, 2012
And this is log

2012-12-27 18:23:07,299 QuartzWorker-0 ERROR ServiceRunner croc.apechnikov.TwitterMonitorImpl:job [atlassian.jira.service.ServiceRunner] An error occured while trying to run service 'croc.apechnikov.TwitterMonitorImpl:job'. Cannot set or mutate an ImmutableGenericValue java.lang.UnsupportedOperationException: Cannot set or mutate an ImmutableGenericValue at com.atlassian.jira.util.ofbiz.ImmutableGenericValue.set(ImmutableGenericValue.java:75) at org.ofbiz.core.entity.GenericEntity.set(GenericEntity.java:229) at com.atlassian.jira.issue.IssueImpl.updateGV(IssueImpl.java:1270) at com.atlassian.jira.issue.IssueImpl.setUpdated(IssueImpl.java:908) at com.atlassian.jira.issue.comments.DefaultCommentManager.create(DefaultCommentManager.java:209) at com.atlassian.jira.issue.comments.DefaultCommentManager.create(DefaultCommentManager.java:166) at com.atlassian.jira.issue.comments.DefaultCommentManager.create(DefaultCommentManager.java:160) at com.atlassian.jira.issue.comments.DefaultCommentManager.create(DefaultCommentManager.java:154) at com.atlassian.jira.issue.comments.DefaultCommentManager.create(DefaultCommentManager.java:148) at croc.apechnikov.TwitterMonitorImpl.action(TwitterMonitorImpl.java:173) at croc.apechnikov.Job.execute(Job.java:29) at com.atlassian.sal.jira.scheduling.JiraPluginSchedulerService.run(JiraPluginSchedulerService.java:93) at com.atlassian.jira.service.JiraServiceContainerImpl.run(JiraServiceContainerImpl.java:61) at com.atlassian.jira.service.ServiceRunner.execute(ServiceRunner.java:47) at org.quartz.core.JobRunShell.run(JobRunShell.java:195) at com.atlassian.multitenant.quartz.MultiTenantThreadPool$MultiTenantRunnable.run(MultiTenantThreadPool.java:72) at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:520)

Suggest an answer

Log in or Sign up to answer