How to change issue data with ScriptRunner groovy scripts

I made a .groovy file with which I want to change the issue summary on comment event. If the comment contains some string 'x' and that string 'x' isn't in the summary of issue, then add it to the beggining of issue summary:

Here is the code for doing this, but it doesn't seem to work, what can be the cause? I put the script in /WEB-INF/classes and in the log file isn't anything recorded.

Can I change any issue data with .groovy scripts (the file is groovy extension, but the code syntax is in JAVA)?

I made before custom listenters like this, but it was just fetching the data, it wasn't writting anything to database.

public class AddAutoIssueKeyToIssueSummary extends AbstractIssueEventListener implements IssueEventListener {

protected void addIssueKeyToIssueSummary(com.atlassian.jira.event.issue.IssueEvent event){

int stringIndex;
String issueKeyString = "HHOO-";
String temp="";

com.atlassian.jira.issue.MutableIssue eventIssue = event.getIssue();
if(!eventIssue.getSummary().contains(issueKeyString)){

String commentText = ComponentAccessor.getCommentManager().getComments(eventIssue).last().getBody();

if(commentText.indexOf(issueKeyString) != -1){
stringIndex = commentText.indexOf(issueKeyString);

for(int i = stringIndex + issueKeyString.length(); i < commentText.length(); ++i){

if(Character.isDigit(commentText.charAt(i)) ){
temp += commentText.charAt(i);
}
else
break;
}

}
}
if(temp.length() > 0){
eventIssue.setSummary(temp + " " + eventIssue.getSummary());
}

}

// IssueEventListener implementation -----

public void issueCommented(IssueEvent e){

addIssueKeyToIssueSummary(e);
}

//whether administrators can delete this listener
public boolean isInternal()
{
return true;
}
}

3 answers

Did you create the custom listener in the SciptRunner ScriptListener admin panel? Did it compile without errors? Did you check the log file for errors when running this?

You might need to call updateIssue to persist the changes you've made to the MutableIssue:

 

Use IssueManager updateIssue call.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption

...
		
if(temp.length() &gt; 0){
	eventIssue.setSummary(temp + " " + eventIssue.getSummary());
			
	ComponentAccessor.getIssueManager().updateIssue(user, eventIssue, EventDispatchOption.DO_NOT_DISPATCH, true)
}

Hi Andrew, thanks for the tip, I will give it a shot. Best Regards, Ivan

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

77 views 0 5
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