1

We are continuously receiving user creation/removal in our JIRA instance as the team is growing.

I am looking for possible options to automate this using groovy script.

Currently, we are having a separate project in JIRA handling these requests. Team will raise the tickets with user name, required roles & Manager will be approve request in the project and then it will be picked by JIRA admin to create/remove the users.

Then JIRA admin will close the ticket.

I am looking to automate the JIRA admin work, Once the ticket is raised by team & moved to a certain status(like Approval), JIRA should create the users automatically. How this can be achieved using groovy script?

Thank you.

CH BHARATH KUMAR
  • 111
  • 1
  • 11

3 Answers3

0

You should be able to use the Jira API to do what you want. The Jira REST API Reference has everything you need.

For user creation, a POST request to the rest/api/3/user endpoint should work while a DELETE request to the same endpoint should remove the user. This guide by Atlassian has detailed information on creating users via that endpoint.

The REST API reference also has endpoints to automatically transition the state of the Jira issue. A GET request to the /rest/api/3/issue/{issueIdOrKey}/transitions endpoint shows you all possible transitions for an issue while a POST request to the same endpiont allows you to transition the issue.

M B
  • 2,700
  • 2
  • 15
  • 20
  • @M B Is there any Groovy script to do this activity ? – CH BHARATH KUMAR Jan 30 '23 at 08:09
  • 1
    There are no pre-written scripts to do this for you. Take a look at [this article](https://www.baeldung.com/groovy-web-services) on how you can call APIs (including the Jira API) using Groovy. – M B Jan 30 '23 at 08:15
0

I have used below groovy script to create the user and add to particular group


import com.atlassian.jira.component.ComponentAccessor

def userName = "user2"
def groupNametoAdd = "jira-servicedesk-users"
def userToAdd = ComponentAccessor.getUserManager().getUserByName(userName)
def groupName = ComponentAccessor.getGroupManager().getGroup(groupNametoAdd)


if (!userToAdd) {
    log.error("User doesn't exist")
    return
}

if (ComponentAccessor.getGroupManager().getGroupsForUser(userName).contains(groupName)) {
    log.error("User: $userToAdd.username already in the group: $groupName.name")
    return
}



if(!groupName) {
    log.error("Group doesn't exist")
} else {
    ComponentAccessor.groupManager.addUserToGroup(userToAdd, groupName)
    log.error("User: $userToAdd.username added to the group: $groupName.name")
}

CH BHARATH KUMAR
  • 111
  • 1
  • 11
0

In case you run approvals in Jira Service Management project (previously known as Jira Service Desk) then you can take advantage of build in Automation for Jira with two steps:

  1. When: Approval completed
  2. Then: Send web request

Example: Approval Automation rule

Web request can do API call to create user, documentation is here: https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-users/#api-rest-api-3-user-post

In web request you can interpolate data from issue. If you have custom field "New user email" then you can use it in request payload: Create user web request

What you may need to know:

"Approval completed" - trigger is only available in Jira Service Management projects. Other Jira projects do not have build-in approval management solution.In case you would like to run approvals in other Jira project type you could use Approval Path for Jira. You can put Web Request and call Automation for Jira via webhook: Approval Path for Jira - create user with webhook to Jira Automation

Jira Automation rule will be triggered but Approval Path webhook / http request.

Krzysztof Bogdan
  • 916
  • 7
  • 16