The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Create JIRA Tickets Via Groovy And JIRA REST API Help

supers98
Contributor
February 14, 2022

Hello Atlassian Community,

I am wondering what is the base code to set up a POST request to create a JIRA issue/ticket.
I am having trouble setting up the HTTP connection and authentication via Groovy code. Any help would be great! I know the API link/end points but after no clue how to set it up. I already have the payload for the POST request too.

1 answer

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

4 votes
PD Sheehan
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 Champions.
February 15, 2022

If you want to have a groovy code running on an environment to make a request to itself (i.e. same server/instance), you can use the "TrustedRequestFactory"

Here is a sample script that makes use of that:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.properties.APKeys
import com.atlassian.sal.api.net.Response
import com.atlassian.sal.api.net.ResponseException
import com.atlassian.sal.api.net.ReturningResponseHandler
import com.atlassian.sal.api.net.TrustedRequest
import com.atlassian.sal.api.net.TrustedRequestFactory
import com.atlassian.sal.api.net.Request
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
import groovyx.net.http.ContentType
import groovyx.net.http.URIBuilder


def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def baseUrl = ComponentAccessor.applicationProperties.getString(APKeys.JIRA_BASEURL)
def trustedRequestFactory = ComponentAccessor.getOSGiComponentInstanceOfType(TrustedRequestFactory)

def payload = new JsonBuilder([payloadField: 'payload value']).toString()
def endPointPath = '/rest/api/...'
def url = baseUrl + endPointPath

def request = trustedRequestFactory.createTrustedRequest(Request.MethodType.POST, url) as TrustedRequest
request.addTrustedTokenAuthentication(new URIBuilder(baseUrl).host, currentUser.name)
request.addHeader("Content-Type", ContentType.JSON.toString())
request.addHeader("X-Atlassian-Token", 'no-check')
request.setRequestBody(payload)

def response = request.executeAndReturn(new ReturningResponseHandler<Response, Object>() {
Object handle(Response response) throws ResponseException {
if (response.statusCode != HttpURLConnection.HTTP_OK) {
log.error "Received an error while posting to the rest api. StatusCode=$response.statusCode. Response Body: $response.responseBodyAsString"
return null
} else {
def jsonResp = new JsonSlurper().parseText(response.responseBodyAsString)
log.info "REST API reports success: $jsonResp"
return jsonResp
}
}
})
return response
Ken McClean
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 Champions.
December 21, 2022

This code example has been immensely helpful to me. Thanks for sharing it!

Like PD Sheehan likes this
Jean-Christophe Gonzales
January 26, 2023

So do I, I have spent hours to get such a code example, many, many thanks !