howto call a groovy code from Scriptrunner for Confluence macro

I want to write a macro with Scriptrunner. It should place a button over a table. On pressing that button i want to read some of the fields of that table and send it to a REST Service.

I know how to place that button from the examples provided from Adaptavist. I also know how to write groovy code to have a function to feed the REST Service. 

I struggle where to put the groovy code, so that I can call it, when the button is pressed. Can sonone provide an example, please?

 

1 answer

So, there are a few things you might be asking; I'll try to address each.

Per the documentation on custom macros, there are three code blocks that define your macro.

The first takes some Groovy code that should return an HTML string. The HTML will be what ultimately renders on the page, and in your case, should probably include an <button> tag.

The second takes some JavaScript code. This is where you'd write the code that read the contents of your table and sent the data to an external REST service.

The third block is where you can put CSS, to style your macro.

If you're asking where you put the code for your REST Service, that would be in a custom REST Endpoint, which is a separate piece of configuration from your macro.

Does that make sense?

To expand a bit, the javascript to read your table might look something like this:

AJS.toInit(function () {
let buttonSelector = 'button#myCustomMacroButton';
let button = document.querySelector(buttonSelector);

function clickHandler(clickEvent) {
let tableElements = document.querySelectorAll(buttonSelector + ' ~ table td');
let tableData = [];
tableElements.forEach(function (tdElement) {
tableData.push(tdElement.innerHTML);
});
AJS.$.ajax({
type: "POST",
url : AJS.params.contextPath + "/rest/scriptrunner/latest/custom/myCustomEndpoint",
data: {
tableData: tableData
}
}).done(function (data, status, jqXhr) {
console.log("Retrieved response from custom rest endpoint: ");
console.log(data);
console.log(status);
});
}

button.addEventListener('click', clickHandler);
});

The handler function passed to "done" is what happens whenever your data comes back from the server. I assume you'd want something more than just logging the data to the console, but that's just an example.

Your REST Endpoint would need to receive that data and do something with it.

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import org.codehaus.jackson.map.ObjectMapper
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

@BaseScript CustomEndpointDelegate delegate

myCustomEndpoint(
httpMethod: "POST", groups: ["confluence-administrators"]
) { MultivaluedMap queryParams, String body ->

def mapper = new ObjectMapper()
def data = mapper.readValue(body, Map)

return Response.ok("Received data: $data").build()
}

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...

309 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