REST endpoint using ScriptRunner.

Abyakta Lenka June 23, 2017

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 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 23, 2017

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.

Abyakta Lenka June 23, 2017

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
Abyakta Lenka July 8, 2017

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

Abyakta Lenka July 8, 2017

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){ }
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 9, 2017

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
TAGS
AUG Leaders

Atlassian Community Events