Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

ScriptRunner how to add Comment, using Script Fragment Web Item

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

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.

@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()
}

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
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you