2017-01-29 18:28:04 +02:00
|
|
|
package jira
|
|
|
|
|
|
|
|
import (
|
2020-05-03 15:38:32 +02:00
|
|
|
"context"
|
2017-01-29 18:28:04 +02:00
|
|
|
"fmt"
|
2018-03-16 12:01:13 +02:00
|
|
|
"net/url"
|
2017-01-29 18:28:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// GroupService handles Groups for the JIRA instance / API.
|
|
|
|
//
|
|
|
|
// JIRA API docs: https://docs.atlassian.com/jira/REST/server/#api/2/group
|
|
|
|
type GroupService struct {
|
|
|
|
client *Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// groupMembersResult is only a small wrapper around the Group* methods
|
|
|
|
// to be able to parse the results
|
|
|
|
type groupMembersResult struct {
|
|
|
|
StartAt int `json:"startAt"`
|
|
|
|
MaxResults int `json:"maxResults"`
|
|
|
|
Total int `json:"total"`
|
|
|
|
Members []GroupMember `json:"values"`
|
|
|
|
}
|
|
|
|
|
2017-11-28 04:56:49 +02:00
|
|
|
// Group represents a JIRA group
|
|
|
|
type Group struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Properties groupProperties `json:"properties"`
|
|
|
|
AdditionalProperties bool `json:"additionalProperties"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type groupProperties struct {
|
|
|
|
Name groupPropertiesName `json:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type groupPropertiesName struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
}
|
|
|
|
|
2017-01-29 18:28:04 +02:00
|
|
|
// GroupMember reflects a single member of a group
|
|
|
|
type GroupMember struct {
|
|
|
|
Self string `json:"self,omitempty"`
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Key string `json:"key,omitempty"`
|
2019-07-12 21:08:17 +02:00
|
|
|
AccountID string `json:"accountId,omitempty"`
|
2017-01-29 18:28:04 +02:00
|
|
|
EmailAddress string `json:"emailAddress,omitempty"`
|
|
|
|
DisplayName string `json:"displayName,omitempty"`
|
|
|
|
Active bool `json:"active,omitempty"`
|
|
|
|
TimeZone string `json:"timeZone,omitempty"`
|
2019-07-12 21:08:17 +02:00
|
|
|
AccountType string `json:"accountType,omitempty"`
|
2017-01-29 18:28:04 +02:00
|
|
|
}
|
|
|
|
|
2018-06-08 18:00:22 +02:00
|
|
|
// GroupSearchOptions specifies the optional parameters for the Get Group methods
|
2018-03-16 12:01:13 +02:00
|
|
|
type GroupSearchOptions struct {
|
|
|
|
StartAt int
|
|
|
|
MaxResults int
|
|
|
|
IncludeInactiveUsers bool
|
|
|
|
}
|
|
|
|
|
2020-05-03 15:38:32 +02:00
|
|
|
// GetWithContext returns a paginated list of users who are members of the specified group and its subgroups.
|
2017-01-29 18:28:04 +02:00
|
|
|
// Users in the page are ordered by user names.
|
|
|
|
// User of this resource is required to have sysadmin or admin permissions.
|
|
|
|
//
|
|
|
|
// JIRA API docs: https://docs.atlassian.com/jira/REST/server/#api/2/group-getUsersFromGroup
|
2018-03-16 12:01:13 +02:00
|
|
|
//
|
2018-03-13 11:49:27 +02:00
|
|
|
// WARNING: This API only returns the first page of group members
|
2020-05-03 15:38:32 +02:00
|
|
|
func (s *GroupService) GetWithContext(ctx context.Context, name string) ([]GroupMember, *Response, error) {
|
2018-03-16 12:01:13 +02:00
|
|
|
apiEndpoint := fmt.Sprintf("/rest/api/2/group/member?groupname=%s", url.QueryEscape(name))
|
2020-05-03 15:38:32 +02:00
|
|
|
req, err := s.client.NewRequestWithContext(ctx, "GET", apiEndpoint, nil)
|
2017-01-29 18:28:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
group := new(groupMembersResult)
|
2017-12-14 09:24:47 +02:00
|
|
|
resp, err := s.client.Do(req, group)
|
2017-01-29 18:28:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
2018-03-13 11:49:27 +02:00
|
|
|
return group.Members, resp, nil
|
|
|
|
}
|
|
|
|
|
2020-05-03 15:38:32 +02:00
|
|
|
// Get wraps GetWithContext using the background context.
|
|
|
|
func (s *GroupService) Get(name string) ([]GroupMember, *Response, error) {
|
|
|
|
return s.GetWithContext(context.Background(), name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetWithOptionsWithContext returns a paginated list of members of the specified group and its subgroups.
|
2018-03-13 11:49:27 +02:00
|
|
|
// Users in the page are ordered by user names.
|
|
|
|
// User of this resource is required to have sysadmin or admin permissions.
|
|
|
|
//
|
|
|
|
// JIRA API docs: https://docs.atlassian.com/jira/REST/server/#api/2/group-getUsersFromGroup
|
2020-05-03 15:38:32 +02:00
|
|
|
func (s *GroupService) GetWithOptionsWithContext(ctx context.Context, name string, options *GroupSearchOptions) ([]GroupMember, *Response, error) {
|
2018-03-16 12:01:13 +02:00
|
|
|
var apiEndpoint string
|
|
|
|
if options == nil {
|
|
|
|
apiEndpoint = fmt.Sprintf("/rest/api/2/group/member?groupname=%s", url.QueryEscape(name))
|
|
|
|
} else {
|
|
|
|
apiEndpoint = fmt.Sprintf(
|
|
|
|
"/rest/api/2/group/member?groupname=%s&startAt=%d&maxResults=%d&includeInactiveUsers=%t",
|
|
|
|
url.QueryEscape(name),
|
|
|
|
options.StartAt,
|
|
|
|
options.MaxResults,
|
|
|
|
options.IncludeInactiveUsers,
|
|
|
|
)
|
2018-03-13 11:49:27 +02:00
|
|
|
}
|
2020-05-03 15:38:32 +02:00
|
|
|
req, err := s.client.NewRequestWithContext(ctx, "GET", apiEndpoint, nil)
|
2018-03-13 11:49:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2017-12-14 10:00:58 +02:00
|
|
|
|
2018-03-13 11:49:27 +02:00
|
|
|
group := new(groupMembersResult)
|
|
|
|
resp, err := s.client.Do(req, group)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
2017-12-14 10:00:58 +02:00
|
|
|
}
|
2018-03-16 12:01:13 +02:00
|
|
|
return group.Members, resp, nil
|
2017-01-29 18:28:04 +02:00
|
|
|
}
|
2017-11-28 04:56:49 +02:00
|
|
|
|
2020-05-03 15:38:32 +02:00
|
|
|
// GetWithOptions wraps GetWithOptionsWithContext using the background context.
|
|
|
|
func (s *GroupService) GetWithOptions(name string, options *GroupSearchOptions) ([]GroupMember, *Response, error) {
|
|
|
|
return s.GetWithOptionsWithContext(context.Background(), name, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddWithContext adds user to group
|
2017-11-28 04:56:49 +02:00
|
|
|
//
|
|
|
|
// JIRA API docs: https://docs.atlassian.com/jira/REST/cloud/#api/2/group-addUserToGroup
|
2020-05-03 15:38:32 +02:00
|
|
|
func (s *GroupService) AddWithContext(ctx context.Context, groupname string, username string) (*Group, *Response, error) {
|
2017-11-29 09:10:42 +02:00
|
|
|
apiEndpoint := fmt.Sprintf("/rest/api/2/group/user?groupname=%s", groupname)
|
2017-11-28 04:56:49 +02:00
|
|
|
var user struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
user.Name = username
|
2020-05-03 15:38:32 +02:00
|
|
|
req, err := s.client.NewRequestWithContext(ctx, "POST", apiEndpoint, &user)
|
2017-11-28 04:56:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
responseGroup := new(Group)
|
|
|
|
resp, err := s.client.Do(req, responseGroup)
|
|
|
|
if err != nil {
|
|
|
|
jerr := NewJiraError(resp, err)
|
|
|
|
return nil, resp, jerr
|
|
|
|
}
|
|
|
|
|
|
|
|
return responseGroup, resp, nil
|
|
|
|
}
|
|
|
|
|
2020-05-03 15:38:32 +02:00
|
|
|
// Add wraps AddWithContext using the background context.
|
|
|
|
func (s *GroupService) Add(groupname string, username string) (*Group, *Response, error) {
|
|
|
|
return s.AddWithContext(context.Background(), groupname, username)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveWithContext removes user from group
|
2017-11-28 04:56:49 +02:00
|
|
|
//
|
|
|
|
// JIRA API docs: https://docs.atlassian.com/jira/REST/cloud/#api/2/group-removeUserFromGroup
|
2020-05-03 15:38:32 +02:00
|
|
|
func (s *GroupService) RemoveWithContext(ctx context.Context, groupname string, username string) (*Response, error) {
|
2017-11-29 09:10:42 +02:00
|
|
|
apiEndpoint := fmt.Sprintf("/rest/api/2/group/user?groupname=%s&username=%s", groupname, username)
|
2020-05-03 15:38:32 +02:00
|
|
|
req, err := s.client.NewRequestWithContext(ctx, "DELETE", apiEndpoint, nil)
|
2017-11-28 04:56:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := s.client.Do(req, nil)
|
|
|
|
if err != nil {
|
|
|
|
jerr := NewJiraError(resp, err)
|
|
|
|
return resp, jerr
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
2020-05-03 15:38:32 +02:00
|
|
|
|
|
|
|
// Remove wraps RemoveWithContext using the background context.
|
|
|
|
func (s *GroupService) Remove(groupname string, username string) (*Response, error) {
|
|
|
|
return s.RemoveWithContext(context.Background(), groupname, username)
|
|
|
|
}
|