1package gerrit
2
3import (
4	"fmt"
5)
6
7// ListGroupMembersOptions specifies the different options for the ListGroupMembers call.
8//
9// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-groups.html#group-members
10type ListGroupMembersOptions struct {
11	// To resolve the included groups of a group recursively and to list all members the parameter recursive can be set.
12	// Members from included external groups and from included groups which are not visible to the calling user are ignored.
13	Recursive bool `url:"recursive,omitempty"`
14}
15
16// MembersInput entity contains information about accounts that should be added as members to a group or that should be deleted from the group
17type MembersInput struct {
18	OneMember string   `json:"_one_member,omitempty"`
19	Members   []string `json:"members,omitempty"`
20}
21
22// ListGroupMembers lists the direct members of a Gerrit internal group.
23// The entries in the list are sorted by full name, preferred email and id.
24//
25// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-groups.html#group-members
26func (s *GroupsService) ListGroupMembers(groupID string, opt *ListGroupMembersOptions) (*[]AccountInfo, *Response, error) {
27	u := fmt.Sprintf("groups/%s/members/", groupID)
28
29	u, err := addOptions(u, opt)
30	if err != nil {
31		return nil, nil, err
32	}
33
34	req, err := s.client.NewRequest("GET", u, nil)
35	if err != nil {
36		return nil, nil, err
37	}
38
39	v := new([]AccountInfo)
40	resp, err := s.client.Do(req, v)
41	if err != nil {
42		return nil, resp, err
43	}
44
45	return v, resp, err
46}
47
48// GetGroupMember retrieves a group member.
49//
50// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-groups.html#get-group-member
51func (s *GroupsService) GetGroupMember(groupID, accountID string) (*AccountInfo, *Response, error) {
52	u := fmt.Sprintf("groups/%s/members/%s", groupID, accountID)
53
54	req, err := s.client.NewRequest("GET", u, nil)
55	if err != nil {
56		return nil, nil, err
57	}
58
59	v := new(AccountInfo)
60	resp, err := s.client.Do(req, v)
61	if err != nil {
62		return nil, resp, err
63	}
64
65	return v, resp, err
66}
67
68// AddGroupMember adds a user as member to a Gerrit internal group.
69//
70// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-groups.html#add-group-member
71func (s *GroupsService) AddGroupMember(groupID, accountID string) (*AccountInfo, *Response, error) {
72	u := fmt.Sprintf("groups/%s/members/%s", groupID, accountID)
73
74	req, err := s.client.NewRequest("PUT", u, nil)
75	if err != nil {
76		return nil, nil, err
77	}
78
79	v := new(AccountInfo)
80	resp, err := s.client.Do(req, v)
81	if err != nil {
82		return nil, resp, err
83	}
84
85	return v, resp, err
86}
87
88// AddGroupMembers adds one or several users to a Gerrit internal group.
89// The users to be added to the group must be provided in the request body as a MembersInput entity.
90//
91// As response a list of detailed AccountInfo entities is returned that describes the group members that were specified in the MembersInput.
92// An AccountInfo entity is returned for each user specified in the input, independently of whether the user was newly added to the group or whether the user was already a member of the group.
93//
94// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-groups.html#_add_group_members
95func (s *GroupsService) AddGroupMembers(groupID string, input *MembersInput) (*[]AccountInfo, *Response, error) {
96	u := fmt.Sprintf("groups/%s/members", groupID)
97
98	req, err := s.client.NewRequest("POST", u, input)
99	if err != nil {
100		return nil, nil, err
101	}
102
103	v := new([]AccountInfo)
104	resp, err := s.client.Do(req, v)
105	if err != nil {
106		return nil, resp, err
107	}
108
109	return v, resp, err
110}
111
112// DeleteGroupMember deletes a user from a Gerrit internal group.
113//
114// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-groups.html#delete-group-member
115func (s *GroupsService) DeleteGroupMember(groupID, accountID string) (*Response, error) {
116	u := fmt.Sprintf("groups/%s/members/%s'", groupID, accountID)
117	return s.client.DeleteRequest(u, nil)
118}
119
120// DeleteGroupMembers delete one or several users from a Gerrit internal group.
121// The users to be deleted from the group must be provided in the request body as a MembersInput entity.
122//
123// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-groups.html#delete-group-members
124func (s *GroupsService) DeleteGroupMembers(groupID string, input *MembersInput) (*Response, error) {
125	u := fmt.Sprintf("groups/%s/members.delete'", groupID)
126
127	req, err := s.client.NewRequest("POST", u, input)
128	if err != nil {
129		return nil, err
130	}
131
132	return s.client.Do(req, nil)
133}
134