Commenting collection of issues

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

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.

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());

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)

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)

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

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 :)

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. ):

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?

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);

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,856 views 12 18
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot