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

0 vote

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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Marketplace Apps

Tips on how to choose the best estimation method for your planning

Planning and grooming sessions all come with their own sets of rules. Team members meet to estimate stories or other work items, all according to an agreed-upon process. And with every session comes ...

71 views 0 11
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