REST endpoint using ScriptRunner. Edited

Can we make an existing Groovy scripts as an REST endpoint.

if yes what changes need to be done to the groovy script.

1 answer

This widget could not be displayed.

You'll need to import the scripts into the REST area, but yes.

https://scriptrunner.adaptavist.com/4.3.3/jira/rest-endpoints.html lists the things you'll need to look at to make a script provide a REST endpoint.

I am not able to understand the 4th and 5th part 

- parameters which are provided to your method body
- the body of your method, where you will return a javax.ws.rs.core.Response object

Hi Nic,

I am trying a very simple groovy code as rest endpoint.

adding a user to a group

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.user.ApplicationUser

@BaseScript CustomEndpointDelegate delegate

addUserToGroup(httpMethod: "PUT", groups: ["jira-administrators"]) { MultivaluedMap queryParams, String body, HttpServletRequest request ->


GroupManager groupManager = ComponentAccessor.getGroupManager();
try
    {
    groupManager.addUserToGroup(((ApplicationUser) issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("ABCD"))).getDirectoryUser() , groupManager.getGroup("Group"))
    } catch (NullPointerException e){ }

Can you please help

Hi Nic,

I am trying to create a small REST endpoint adding user to group

can you please help.

Here is the code i am trying.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.user.ApplicationUser

@BaseScript CustomEndpointDelegate delegate

addUserToGroup(httpMethod: "PUT", groups: ["jira-administrators"]) { MultivaluedMap queryParams, String body, HttpServletRequest request ->


GroupManager groupManager = ComponentAccessor.getGroupManager();
try
    {
    groupManager.addUserToGroup(((ApplicationUser) issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("ABCD"))).getDirectoryUser() , groupManager.getGroup("Group"))
    } catch (NullPointerException e){ }

My guess is that is throwing an error inside the "try".  The error should show you what is going wrong (I'd expect a casting error to be the problem, although I've not tried it so that's a very wooly instinctive thing)

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Published yesterday in Marketplace Apps

The 7 hacks of highly successful automation

...there's anything I've learnt from working, it's that people are lazy! No offense to anyone reading this, but it's true and we can all admit it. The easier you make something for someone, the more...

164 views 0 12
Read article

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