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 a ContentApproveEvent gets fired, 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.onresolve.scriptrunner.runner.customisers.WithPlugin
import groovy.json.JsonBuilder
import com.comalatech.workflow.event.ContentApproveEvent
import static com.atlassian.sal.api.net.Request.MethodType.POST

@WithPlugin("com.comalatech.workflow")

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

def event = event as ContentApproveEvent
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"]
    ]
]).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. 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.