Search code examples
groovyjirajira-rest-apijira-pluginscriptrunner-for-jira

How to update the comment of an issue in Jira


I am trying to concatenate some text to a comment entered by the user. How can I do that? Here is my code below. I need to access the comment input by the user and then add new text to it. How can I access this? I am using Jira ScriptRunner custom postfunction to be executed when the user clicks on a transition.

import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.workflow.JiraWorkflow
import com.atlassian.jira.workflow.WorkflowManager
import org.apache.log4j.Logger
import com.atlassian.jira.component.ComponentAccessor


def log = Logger.getLogger("atlassian-jira.log")
log.warn("This is the last action ")

WorkflowManager workflowManager = ComponentAccessor.getWorkflowManager();
JiraWorkflow workflow = workflowManager.getWorkflow(issue);
List <Object> actions = workflow.getLinkedStep(issue.getStatus()).getActions(); 

def wfd = workflow.getDescriptor()
def actionName = wfd.getAction(transientVars["actionId"] as int).getName(); 

log.warn("This is the last action "+actionName)
def comment= "+++ added via workflow action "+"\""+actionName+"\"+++"

Solution

  • Here is the code transientVars is the way to go.

    import com.atlassian.jira.issue.comments.Comment
    import com.atlassian.jira.workflow.JiraWorkflow
    import com.atlassian.jira.workflow.WorkflowManager
    import org.apache.log4j.Logger
    import com.atlassian.jira.component.ComponentAccessor
    
    
    def log = Logger.getLogger("atlassian-jira.log")
    log.warn("This is the last action ")
    
    WorkflowManager workflowManager = ComponentAccessor.getWorkflowManager();
    JiraWorkflow workflow = workflowManager.getWorkflow(issue);
    
    def wfd = workflow.getDescriptor()
    def actionName = wfd.getAction(transientVars["actionId"] as int).getName(); 
    
    log.warn("This is the last action "+actionName)
    def comment= "+++ added via workflow action "+"\""+actionName+"\"+++"
    String content = transientVars["comment"] +"\n"+comment  as String
    log.warn("CONTENT"+ content)
    transientVars["comment"]= content