If you have a Jira instance linked to your Confluence instance (see instructions on linking apps), it is possible to interact with it using Jira’s REST API.

These examples make the requests on behalf of the current user. Click here for other ways of making the request.

Also, see the examples of integrating Confluence from the Jira perspective.

Creating a Jira project

This example utilises Event Listeners and hooks up the SpaceCreateEvent, responding to it by creating a project in Jira.

Check our video guide:

import com.atlassian.applinks.api.ApplicationLinkService
import com.atlassian.applinks.api.application.jira.JiraApplicationType
import com.atlassian.confluence.event.events.space.SpaceCreateEvent
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.sal.api.net.Response
import com.atlassian.sal.api.net.ResponseException
import com.atlassian.sal.api.net.ResponseHandler
import groovy.json.JsonBuilder

import static com.atlassian.sal.api.net.Request.MethodType.POST

def appLinkService = ComponentLocator.getComponent(ApplicationLinkService)
def appLink = appLinkService.getPrimaryApplicationLink(JiraApplicationType) (1)
def applicationLinkRequestFactory = appLink.createAuthenticatedRequestFactory()

def event = event as SpaceCreateEvent
def space = event.space

def input = new JsonBuilder([ (2)
    projectTypeKey: "business",
    projectTemplateKey : "com.atlassian.jira-core-project-templates:jira-core-task-management",
    name: space.name,
    key: space.key,
    lead: event.space.creator.name,
]).toString()

def request = applicationLinkRequestFactory.createRequest(POST, "/rest/api/2/project")
    .addHeader("Content-Type", "application/json")
    .setEntity(input)

request.execute(new ResponseHandler<Response>() { (3)
    @Override
    void handle(Response response) throws ResponseException {
        if ( response.statusCode != 201) {
            log.error ("Creating jira project failed: ${response.responseBodyAsString}")
        }
    }
})
1 Retrieves the primary Jira application link
2 The project parameters
3 Executes the request

Creating a Jira issue

The script below is an example of creating an issue and can be used via the Script Console, Event Listeners, etc.

import com.atlassian.applinks.api.ApplicationLinkService
import com.atlassian.applinks.api.application.jira.JiraApplicationType
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.sal.api.net.Response
import com.atlassian.sal.api.net.ResponseException
import com.atlassian.sal.api.net.ResponseHandler
import groovy.json.JsonBuilder

import static com.atlassian.sal.api.net.Request.MethodType.POST

def appLinkService = ComponentLocator.getComponent(ApplicationLinkService)
def appLink = appLinkService.getPrimaryApplicationLink(JiraApplicationType) (1)
def applicationLinkRequestFactory = appLink.createAuthenticatedRequestFactory()

def body = new JsonBuilder([
    fields: [ (2)
              project     : [key: "PROJECT_KEY"],
              summary     : "Perform a release",
              description : "Build and deploy a release",
              issuetype   : [name: "Story"]
    ]
]).toString()

def request = applicationLinkRequestFactory.createRequest(POST, "/rest/api/2/issue")
    .addHeader("Content-Type", "application/json")
    .setEntity(body)

request.execute(new ResponseHandler<Response>() { (3)
    @Override
    void handle(Response response) throws ResponseException {
        if (response.statusCode != 201) {
            log.error("Creating Jira issue failed: ${response.responseBodyAsString}")
        }
    }
})
1 Retrieves the primary Jira application link
2 Specify values for the issue’s fields here
3 Executes the request

Have questions? Visit the Atlassian Community to connect, share, and learn with other Atlassian users and experts, including Adaptavist staff.

Ask a question about ScriptRunner for JIRA, Bitbucket Server, or Confluence.

Want to learn more? Check out courses on Adaptavist Learn, an online platform to onboard and train new users for Atlassian solutions.