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

0 votes
Accepted answer

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 Sign up to answer
Community showcase
Published Nov 27, 2018 in Portfolio for Jira

Introducing a new planning experience in Portfolio for Jira (Server/DC)

In the past, Portfolio for Jira required a high degree of detail–foresight that was unrealistic for many businesses to   have–in   order to produce a reliable long-term roadmap. We're tur...

2,417 views 15 19
Read article

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