How to update the Jira issue description field using the custom listener and grovvy script?

1 minute read

Are you looking to update the Jira issue description field in an automated manner? If yes, the below script will help you update the description field. If the description field has a value, this script will append the new description to the existing description or add the new description to the description field. 

Groovy script:

// Namespaces
import com.atlassian.jira.config.SubTaskManager
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.security.JiraAuthenticationContext
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.security.JiraAuthenticationContext
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.issue.index.IssueIndexingService


IssueEvent events = event as IssueEvent;
JiraAuthenticationContext authContext = ComponentAccessor.getJiraAuthenticationContext();
ApplicationUser appUser = authContext.getLoggedInUser();
ComponentAccessor componentAccessor = new ComponentAccessor();
IssueManager issueManager = ComponentAccessor.getIssueManager();
SubTaskManager subTaskManager = ComponentAccessor.getSubTaskManager();


if(issue.issueType.name == "Story" || issue.issueType.name == "Task"){
    
    MutableIssue mIssue = events.getIssue() as MutableIssue;
    def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService);
    String existingDescription = issue.getDescription();
    String newDescription;
    if(existingDescription != null){
        newDescription = existingDescription + " " + "user is codetoliveblog user";
    }
    else{
        newDescription = "user is new user";
    }

    mIssue.setDescription(newDescription);
    issueManager.updateIssue(appUser, mIssue, EventDispatchOption.ISSUE_UPDATED, false);
    boolean wasIndexing = ImportUtils.isIndexIssues();
    ImportUtils.setIndexIssues(true); 
    issueIndexingService.reIndex(issueManager.getIssueObject(mIssue.id));
    ImportUtils.setIndexIssues(wasIndexing);
}

Leave a comment