Heads up! ScriptRunner documentation is moving to docs.adaptavist.com. Adaptavist will keep this site up for a bit, but no future updates to documentation will be published here. ScriptRunner 6.20.0 will be the last release to link to scriptrunner.adaptavist.com for in-app help.

Create Jira issue when a page or blog post is approved

You can register Event Listeners to fire on events provided by 3rd party plugins.

In the example below, once the final ApprovalApprovedEvent gets fired (not a partial approval), a new Jira issue will be created (on behalf of the current user) in the linked Jira instance.

Have a look at Comala Workflow’s JavaDoc for more information/more events.

comala workflows create jira issue listener
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 com.comalatech.workflow.event.approval.ApprovalApprovedEvent
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import groovy.json.JsonBuilder

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


def event = event as ApprovalApprovedEvent

if (!event.isPartial()) {
    def appLinkService = ComponentLocator.getComponent(ApplicationLinkService)
    def appLink = appLinkService.getPrimaryApplicationLink(JiraApplicationType) (1)
    def applicationLinkRequestFactory = appLink.createAuthenticatedRequestFactory()

    def approvalComment = event.approval.comment
    def body = new JsonBuilder([
        fields: [ (2)
            project    : [key: "PROJECT_KEY"],
            summary    : "Confluence Page Created",
            description: "A page has been created and approved by administrators with comment: \"${approvalComment}\"",
            issuetype  : [name: "Story"]

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

    request.execute(new ResponseHandler<Response>() { (3)
        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. Further examples of this can be found here.
3 Executes the request
Jira and Confluence need to be properly linked for this to work. If you havent done so, you can find instructions here.

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.