I've written a ScriptRunner event handler, how do I enable it and know it is working? Edited

I've written a ScriptRunner event handler, how do I enable it and know it is working?

 

Here is the sctipt. I added logging but I'm not seeing anything.

log.info("eWebRequest Form Event Fired")

import com.atlassian.confluence.event.events.content.page.PageEvent
import com.atlassian.confluence.spaces.Space
import com.atlassian.confluence.pages.Page

def event = event as PageEvent

Page page = event.getPage()
Space space = page.getSpace()

if (page.getSpace() != 'eWebProjects') {
log.info("eWebRequest Form Event - 1")
return
}

// Is the page-type eWebRequest?
import com.atlassian.applinks.api.ApplicationLink
import com.atlassian.applinks.api.ApplicationLinkService
import com.atlassian.applinks.api.application.confluence.ConfluenceApplicationType
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 com.atlassian.sal.api.net.ResponseHandler
import com.atlassian.sal.api.net.ReturningResponseHandler
import groovy.json.JsonBuilder

//import com.atlassian.confluence.pages.CommentManager

def ApplicationLink getPrimaryConfluenceLink() {
def applicationLinkService = ComponentLocator.getComponent(ApplicationLinkService.class)
final ApplicationLink conflLink = applicationLinkService.getPrimaryApplicationLink(ConfluenceApplicationType.class);
conflLink
}

def confluenceLink = getPrimaryConfluenceLink()
assert confluenceLink // must have a working app link set up

def authenticatedRequestFactory = confluenceLink.createAuthenticatedRequestFactory()

// Get create content response body as a map
def contentResponse = authenticatedRequestFactory
.createRequest(Request.MethodType.POST, "rest/api/content/" + page.id + "/page-type?")
.addHeader("Content-Type", "application/json")
.executeAndReturn(new ReturningResponseHandler<Response, Map>(){
@Override
Map handle(Response response) throws ResponseException {
if (response.statusCode == HttpURLConnection.HTTP_NOT_FOUND)
{
log.info("eWebRequest Form Event - 2")
return
}

if (response.statusCode != HttpURLConnection.HTTP_OK) {
log.info("eWebRequest Form Event - 3")
throw new Exception("Get page-type failed: " + response.getResponseBodyAsString())
}

return response.getEntity(Map)
}
})

if (contentResponse.value != 'eWebRequest')
{
log.info("eWebRequest Form Event - 4")
return
}

// TESTING ONLY
log.info("eWebRequest Form Event Success")

1 answer

1 accepted

0 vote

Hello, 

If you haven't read over it already this section of the documentation contains a lot of useful information about custom event handlers, including how to write and use them. I would recommend reading it over. 

For testing, if you haven't done so already, try setting up a development environment with logging for your instance. It will making the debugging process much easier in the long run.

Please let me know if you have any further questions!

Regards,

Jenna 

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Kesha Thillainayagam
Posted Friday in Confluence

We want to hear how your non-technical teams are using Confluence!

Hi Community! Kesha (kay-sha) from the Confluence marketing team here! Can you share stories with us on how your non-technical (think Marketing, Sales, HR, legal, etc.) teams are using Confluen...

297 views 11 10
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you