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

Can you call a rest endpoint from a custom listener?

Peterr
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
July 17, 2020

I have a fragment(button) that calls a rest endpoint, this works great and is pretty simple. The Rest Endpoint queries data and returns the result which updates the issue. This works great, for certain conditions we want to setup a listener to automatically initiate the logic defined in the rest endpoint, struggling with how to reference this.

Within a listener is it possible to call an already defined rest endpoint script instead of re-doing it? What format is it expecting to pass a string parameter such as an issue key?

This seems like it should be an easy reference I'm missing, perhaps it's not possible to reference the endpoints from the listeners?   Thanks

@BaseScript CustomEndpointDelegate delegate

doSomething(issue.key)

The above will error and adding {} after it will create a child reference within the existing script and also update the permission of the endpoint to anonymous. 

com.atlassian.jira.event.issue.IssueEvent, file: null
groovy.lang.MissingMethodException: No signature of method

 

1 answer

1 vote
Italo Qualisoni [e-Core]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 17, 2020

 

Hi @Peterr ,

It's is possible to your Listener call your Rest endpoint, you basically will need to access using REST API; Instead of rewriting the rest endpoint method in the Listener, you can call the rest endpoint using REST.

Here is an example about how you can do a POST using Scriptrunner to integrate with Slack, imagine that you need to change from Slack to your Rest Endpoint passing the data you have an example that I have here in my localhost environment that return all components from projectKey Auto (http://192.168.0.17:8080/rest/scriptrunner/latest/custom/getComponent?projectKey=AUTO) you will need to change to your endpoint;

 

I think that your RES Endpoint expect a GET function, but you get the idea. I recommend you to test first before implementing this in production.

Peterr
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
July 22, 2020

Hello,

I feel my issue has more to do with permissions on the GET request to the ScriptRunner Rest Endpoint.  You can restrict the endpoint to various groups, however if I have a listener running that gets triggered from an automated workflow action and a ticket is created/updated the listener logic executes.  When it executes it is doing so as the system but not necessarily a particular user that I can restrict the rest endpoint to. 

I found that if I remove any permission restriction it sets the group to anonymous and can be called, this would not be viable for us.  Is there a security group restriction that we can limit system internal calls or any other secure means to have it perform the call locally without setting it to anonymous or hard-coding credentials? 

Thanks!

Italo Qualisoni [e-Core]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 22, 2020

Hi @Peterr ,

This is a great question, I think it might be possible to execute rest api calls authenticated as user in JIRA Server but I'm not sure how.

Maybe you can do is to have a shared code with the logic you need using Script Editor https://scriptrunner.adaptavist.com/6.4.0/jira/script-editor.html and use the shared code in your Listener and your Rest Endpoint, making this way the call through JAVA API.

I'll do a quick example here and share with you.

Italo Qualisoni [e-Core]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 22, 2020

So the first step is to access your script editor

http://192.168.0.17:8080/plugins/servlet/scriptrunner/admin/scriptEditor

Then you can create a folder to organize your code, in my example I created a helper folder.

Then you can add here the code that does all the heavy work and you would like to share it with your Listener and Rest endpoint, in my case my code will return all components from a project key, and it can filter by what the user is typing ( I had this from a previous question),

Here you can find my code:

 https://gist.github.com/ItaloQualisoni/1c9791eb08f4e112a6a8b09015ce208e

scripteditorUntitled.png

 

Then you can use this code importing your new class in your Rest endpoint , workflow condition/validator/post-function or Listener, simple example here how to import and run in Console:

consoleUntitled.png

And you can then reuse in your Listener in the same way, below is how I did in my example:

https://gist.github.com/ItaloQualisoni/4c9e049f309c34b999a04afcd681727a

 

Notice that I've used a singleton pattern so all usage will use the same object, but feels free to test and do as you need. This way to can leave the REST endpoint for the integration you are looking for and if you need to access the shared code you can import and do it through JAVA API instead of calling a rest function.

 

Let me know if you have any question on that and if that helps you

Italo Qualisoni [e-Core]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 22, 2020

More about Script Editor here: https://www.youtube.com/watch?v=Zq9ek71Zp6I

Michael Aglas January 26, 2023

this doesn't solve the problem calling a restricted Scriptrunner REST Endpoint from a workflow event while it is an anonymous user...

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events