ScriptRunner how to add Comment, using Script Fragment Web Item

Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 5, 2021

Hi ,

I am trying to add a Custom Web Item, i have added the button , but comment is not adding but flag is showing. How can i add Comment using REST Endpoint of Script Runner. 

Please suggest. 

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonOutput
import groovy.transform.BaseScript

import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

@BaseScript CustomEndpointDelegate delegate

approve(httpMethod: "GET") { MultivaluedMap queryParams ->

// the details of getting and modifying the current issue are ommitted for brevity
def issueId = queryParams.getFirst("issueId") as String // use the issueId to retrieve this issue

def flag = [
type : 'success',
title: "Issue approved",
close: 'auto',
body : "This issue "+issueId+" has been approved for release"
]

Response.ok(JsonOutput.toJson(flag)).build()
}

1 answer

2 votes
John Chin
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 8, 2021

Hi @Vikrant Yadav 

In the script, you can add something like this:

import com.atlassian.jira.component.ComponentAccessor

def commentManager = ComponentAccessor.getCommentManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def issueManager = ComponentAccessor.issueManager
def issue = issueManager.getIssueObject(issueId)

String reporter = issue.getReporter().getUsername()

commentManager.create(issue,user,"Hi [~reporter], This issue "+issueId+" has been approved for release",true)

I hope this helps.

Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 8, 2021

@John Chin  thanks a lot mate for your help!

 

I have tried to add below script but it's not working, not adding comment..when clicking on script fragment button. 

 

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonOutput
import groovy.transform.BaseScript
import com.atlassian.jira.component.ComponentAccessor
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

@BaseScript CustomEndpointDelegate delegate

approve(httpMethod: "GET") { MultivaluedMap queryParams ->
def issueId = queryParams.getFirst("issueId") as String
def commentManager = ComponentAccessor.getCommentManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def issueManager = ComponentAccessor.issueManager
def issue = issueManager.getIssueObject(issueId)

String reporter = issue.getReporter().getUsername()

commentManager.create(issue,user,"Hi [~reporter], This issue "+issueId+" has been approved for release",true)


def flag = [
type : 'success',
title: "Issue approved",
close: 'auto',
body : "This issue "+issueId+" has been approved for release"
]

Response.ok(JsonOutput.toJson(flag)).build()
}

John Chin
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 8, 2021

Hi Vikrant,

Do you see any errors in the JIRA log (via $JIRAHOME/log/application-jira.log) ? 

If you debug the script, do you received the issueId information?

For example, add the debug log in your script:

def issueId = queryParams.getFirst("issueId") as String

log.warn "issueId:" + issueId

Suggest an answer

Log in or Sign up to answer