-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.go
198 lines (180 loc) · 6.04 KB
/
role.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package jira
import (
"context"
"encoding/json"
"fmt"
"strings"
)
// RoleService handles roles for the Jira instance / API.
//
// Jira API docs: https://developer.atlassian.com/cloud/jira/platform/rest/v3/#api-group-Role
type RoleService struct {
client *Client
}
// Role represents a Jira product role
type Role struct {
Self string `json:"self" structs:"self"`
Name string `json:"name" structs:"name"`
ID int `json:"id" structs:"id"`
Description string `json:"description" structs:"description"`
Actors []*Actor `json:"actors" structs:"actors"`
}
// Actor represents a Jira actor
type Actor struct {
ID int `json:"id" structs:"id"`
DisplayName string `json:"displayName" structs:"displayName"`
Type string `json:"type" structs:"type"`
Name string `json:"name" structs:"name"`
AvatarURL string `json:"avatarUrl" structs:"avatarUrl"`
ActorUser *ActorUser `json:"actorUser" structs:"actoruser"`
}
// ActorUser contains the account id of the actor/user
type ActorUser struct {
AccountID string `json:"accountId" structs:"accountId"`
}
// GetListWithContext returns a list of all available project roles
//
// Jira API docs: https://developer.atlassian.com/cloud/jira/platform/rest/v3/#api-api-3-role-get
func (s *RoleService) GetListWithContext(ctx context.Context) (*[]Role, *Response, error) {
apiEndpoint := "rest/api/3/role"
req, err := s.client.NewRequestWithContext(ctx, "GET", apiEndpoint, nil)
if err != nil {
return nil, nil, err
}
roles := new([]Role)
resp, err := s.client.Do(req, roles)
if err != nil {
jerr := NewJiraError(resp, err)
return nil, resp, jerr
}
return roles, resp, err
}
// GetList wraps GetListWithContext using the background context.
func (s *RoleService) GetList() (*[]Role, *Response, error) {
return s.GetListWithContext(context.Background())
}
// GetWithContext retreives a single Role from Jira
//
// Jira API docs: https://developer.atlassian.com/cloud/jira/platform/rest/v3/#api-api-3-role-id-get
func (s *RoleService) GetWithContext(ctx context.Context, roleID int) (*Role, *Response, error) {
apiEndpoint := fmt.Sprintf("rest/api/3/role/%d", roleID)
req, err := s.client.NewRequestWithContext(ctx, "GET", apiEndpoint, nil)
if err != nil {
return nil, nil, err
}
role := new(Role)
resp, err := s.client.Do(req, role)
if err != nil {
jerr := NewJiraError(resp, err)
return nil, resp, jerr
}
if role.Self == "" {
return nil, resp, fmt.Errorf("no role with ID %d found", roleID)
}
return role, resp, err
}
// Get wraps GetWithContext using the background context.
func (s *RoleService) Get(roleID int) (*Role, *Response, error) {
return s.GetWithContext(context.Background(), roleID)
}
type RoleType struct {
Name string
Rollnk string
ID string
}
// GetRolesForProjectWithContext returns a list of all available roles for a project
// /rest/api/2/project/{projectIdOrKey}/role
// JIRA API docs: https://developer.atlassian.com/cloud/jira/platform/rest/v3/#api-api-3-role-get
func (s *RoleService) GetRolesForProjectWithContext(ctx context.Context, proj string) (*[]RoleType, *Response, error) {
var rl []RoleType
apiEndpoint := fmt.Sprintf("/rest/api/latest/project/%s/role", proj)
req, err := s.client.NewRequestWithContext(ctx, "GET", apiEndpoint, nil)
if err != nil {
return nil, nil, err
}
resp, err := s.client.Do2(req)
if err != nil {
//jerr := NewJiraError(resp, err)
return nil, nil, nil
}
var doc interface{}
if err2 := json.Unmarshal(resp, &doc); err2 != nil {
return nil, nil, nil
}
// Should be a better way of doing this:
for k, v := range doc.(map[string]interface{}) {
var r RoleType
r.Name = k
r.Rollnk = v.(string)
pos := strings.LastIndex(r.Rollnk, "/role/")
adjustedPos := pos + len("/role/")
r.ID = r.Rollnk[adjustedPos:len(r.Rollnk)]
rl = append(rl, r)
}
return &rl, nil, err
}
// GetActorsForProjectRoleWithContext /rest/api/2/project/{projectIdOrKey}/role/{id}
func (s *RoleService) GetActorsForProjectRoleWithContext(ctx context.Context, proj string, roleid string) (*Role, *Response, error) {
apiEndpoint := fmt.Sprintf("/rest/api/latest/project/%s/role/%s", proj, roleid)
req, err := s.client.NewRequestWithContext(ctx, "GET", apiEndpoint, nil)
if err != nil {
return nil, nil, err
}
roles := new(Role)
resp, err := s.client.Do(req, roles)
if err != nil {
jerr := NewJiraError(resp, err)
return nil, resp, jerr
}
return roles, resp, err
}
type GroupAddType struct {
Group []string `json:"group"`
}
// AddActorsForProjectRoleWithContext /rest/api/2/project/{projectIdOrKey}/role/{id}
func (s *RoleService) AddActorsForProjectRoleWithContext(ctx context.Context, proj string, roleid string, actor string) (*Role, *Response, error) {
apiEndpoint := fmt.Sprintf("/rest/api/2/project/%s/role/%s", proj, roleid)
var payload = new(GroupAddType)
payload.Group = append(payload.Group, actor)
req, err := s.client.NewRequestWithContext(ctx, "POST", apiEndpoint, payload)
if err != nil {
return nil, nil, err
}
roles := new(Role)
resp, err := s.client.Do(req, roles)
if err != nil {
jerr := NewJiraError(resp, err)
return nil, resp, jerr
}
return roles, resp, err
/*
var user struct {
Name string `json:"name"`
}
user.Name = username
req, err := s.client.NewRequestWithContext(ctx, "POST", apiEndpoint, &user)
if err != nil {
return nil, nil, err
}
*/
//groups := new(AddGroupsResponseType)
//c.doRequest("POST", u, payload, &groups)
}
type GroupRemoveType struct {
User []string `json:"user"`
}
// RemoveUserActorsForProjectRole DELETE /rest/project/{projectIdOrKey}/role/{id}
func (s *RoleService) RemoveUserActorsForProjectRole(proj string, roleid int, user string) (*Role, *Response, error) {
ctx := context.Background()
apiEndpoint := fmt.Sprintf("/rest/api/2/project/%s/role/%v?user=%s", proj, roleid, user)
req, err := s.client.NewRequestWithContext(ctx, "DELETE", apiEndpoint, nil)
if err != nil {
return nil, nil, err
}
resp, err := s.client.Do(req, nil)
if err != nil {
jerr := NewJiraError(resp, err)
return nil, resp, jerr
}
return nil, resp, err
}