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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,460,309
Community Members
 
Community Events
176
Community Groups

Responding to Confluence Events in Jira

At a recent ScriptRunner Champion Hour, I talked about how to respond to events in Confluence to update Jira.

The path we demonstrated was to use a combination of an Event Listener in Confluence and REST Endpoint in Jira. The details of using the App Links API are detailed in our documentation for ScriptRunner for Confluence (and for ScriptRunner for Jira, if you need to go the other direction). Here is the code I used.

The basic idea is that if a page in Confluence has a Jira Issue macro in it, then when someone likes that page, they should also automatically vote for the corresponding issue in Jira. Complexities like handling multiple issue macros or doing completely different operations in Jira & Confluence are left as exercises to the reader. :)

Set up a listener in ScriptRunner for Confluence

import com.atlassian.applinks.api.ApplicationLinkResponseHandler
import com.atlassian.applinks.api.ApplicationLinkService
import com.atlassian.applinks.api.application.jira.JiraApplicationType
import com.atlassian.confluence.event.events.like.LikeEvent
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.sal.api.net.Request
import com.atlassian.sal.api.net.Response
import com.atlassian.sal.api.net.ResponseException
import groovy.json.JsonSlurper

LikeEvent event = event
def content = event.content

def xmlStorageFormat = content.bodyAsString
def document = new XmlSlurper(false, false)
    .parseText(xmlStorageFormat)
    .declareNamespace([ac: "http://atlassian.com/content"])

def macros = document.'ac:structured-macro'
def issueKey = macros.children().find { element ->
    element.'@ac:name' == "key"
}?.text()

log.debug "Found link to issue key ${issueKey} in page ${content.title}"

if (issueKey) {
    /*
    For more details on using the App Links API, see
    https://docs.adaptavist.com/sr4js/latest/integrations/atlassian-products/via-app-links
    https://docs.adaptavist.com/sr4c/latest/integrations/atlassian/connect-to-atlassian-via-app-links
    */
    def appLinkService = ComponentLocator.getComponent(ApplicationLinkService)
    def appLink = appLinkService.getPrimaryApplicationLink(JiraApplicationType)
    def applicationLinkRequestFactory = appLink.createAuthenticatedRequestFactory()
    def request = applicationLinkRequestFactory.createRequest(
        Request.MethodType.PUT,
        "/rest/scriptrunner/latest/custom/voteForIssue?issueKey=${issueKey}"
    )

    def handler = new ApplicationLinkResponseHandler() {
        @Override
        Map credentialsRequired(Response response) throws ResponseException {
            return null
        }

        @Override
        Map handle(Response response) throws ResponseException {
            assert response.statusCode == 200
            new JsonSlurper().parseText(response.getResponseBodyAsString()) as Map
        }
    }

    request.execute(handler)
}

Then a REST Endpoint in Jira:

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.transform.BaseScript

import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

@BaseScript
CustomEndpointDelegate customEndpointDelegate

voteForIssue(
    httpMethod: 'PUT',
    groups: ['jira-software-users']
) { MultivaluedMap queryParams ->
    def voteManager = ComponentAccessor.voteManager
    def issueService = ComponentAccessor.issueService
    def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
    def issueKey = queryParams.getFirst('issueKey')?.toString()
    def issue = issueService.getIssue(loggedInUser, issueKey).getIssue()
    voteManager.addVote(loggedInUser, issue)

    return Response.ok("Successfully voted for issue ${issueKey}").build()
}

 

0 comments

Comment

Log in or Sign up to comment
TAGS

Atlassian Community Events