Create a REST endpoint using Groovy Scriptrunner

I am trying to create a rest endpoint using Groovy Scriptrunner , need help.

my groovy code is adding user from a customfield to a group.

how to make it as a REST endpoint so that i can call it externally as a JIRA rest api

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){ }

 

 

1 answer

1 accepted

Hello Abyakta.

I already answered your request in our service desk, but I think this is a very good example to place here, so I'm going to repost this.

This is a REST GET endpoint that will successfully add a user to the group "jira-administrators"

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript

import javax.servlet.http.HttpServletRequest
import javax.ws.rs.core.Response

@BaseScript CustomEndpointDelegate delegate

addUserToJiraAdmins(httpMethod: "GET") { queryParams, body, HttpServletRequest request ->
    def userName = request.getParameter("userName")
    if (userName) {

        def groupManager = ComponentAccessor.getGroupManager()
        def jiraUserManager = ComponentAccessor.getUserManager()

        def user = jiraUserManager.getUserByName(userName)
        if(!user){
            return Response.noContent().build()
        }

        def group = groupManager.getGroup("jira-administrators")

        groupManager.addUserToGroup(user, group)

        return Response.ok(new JsonBuilder([user: userName, group: "jira-administrators"]).toString()).build()
    }
    else
        return Response.noContent().build()
}

You would test that this works by simply typing in your browser:

jiraBaseURL/rest/scriptrunner/latest/custom/addUserToJiraAdmins/?userName=YOURUSER

Please bear in mind that I made this as a REST request only so that you can test that it works right away, the best way of doing this would be following the REST conventions and use a PUT request, instead of a GET. 

You should also give our REST endpoint doco a look for further reference.

Regards,

Daniel

Thanks Daniel for the help. But the above Code can be run by any one . can i make it only for Admins .

 

 

Found it how to restrict it to specific group .

Hi Abyakta,

 

How did you solve this eventually, I'm quite interested?

 

Regards,

Marc

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 Jira Service Desk

How the Telegram Integration for Jira helps Sergey's team take their support efficiency to the bank

...+ reading Fantasy). The same is true for him at the bank he works for: Efficiency is key when time literally equals money. Read on to learn how Sergey makes most of the time he has by...

340 views 2 4
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