forked from hekima/go-jira-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.go
298 lines (244 loc) · 8 KB
/
users.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package gojira
import (
"encoding/json"
"encoding/xml"
"fmt"
"net/url"
"strings"
"time"
)
const (
user_url = "/user"
user_search_url = "/user/search"
grp_url = "/group"
// http://example.com:8080/jira/rest/api/2/user/assignable/multiProjectSearch [GET]
// http://example.com:8080/jira/rest/api/2/user/assignable/search [GET]
// http://example.com:8080/jira/rest/api/2/user/avatar [POST, PUT]
// http://example.com:8080/jira/rest/api/2/user/avatar/temporary [POST, POST]
// http://example.com:8080/jira/rest/api/2/user/avatar/{id} [DELETE]
// http://example.com:8080/jira/rest/api/2/user/avatars [GET]
// http://example.com:8080/jira/rest/api/2/user/picker [GET]
// http://example.com:8080/jira/rest/api/2/user/viewissue/search [GET]
)
type User struct {
Self string `json:"self"`
Name string `json:"name"`
EmailAddress string `json:"emailAddress"`
DisplayName string `json:"displayName"`
Active bool `json:"active"`
TimeZone string `json:"timeZone"`
AvatarUrls map[string]string `json:"avatarUrls"`
Expand string `json:"expand"`
Groups *Groups `json:"groups"`
Errors []string `json:"errorMessages"`
// "groups": {
// "size": 3,
// "items": [
// {
// "name": "jira-user",
// "self": "http://www.example.com/jira/rest/api/2/group?groupname=jira-user"
// },
// {
// "name": "jira-admin",
// "self": "http://www.example.com/jira/rest/api/2/group?groupname=jira-admin"
// },
// {
// "name": "important",
// "self": "http://www.example.com/jira/rest/api/2/group?groupname=important"
// }
// ]
// }
}
// Assignee is a helper method for abstracting IssueFields.Assignee
// when building data for CreateIssue
func (u *User) Assignee() *IssueUser {
return &IssueUser{Name: u.Name}
}
/*
Returns a user. This resource cannot be accessed anonymously.
GET http://example.com:8080/jira/rest/api/2/user?username=USERNAME
Parameters
username string The username
Usage
user, err := jira.User("username")
if err != nil {
fmt.Println(err.Error())
}
fmt.Printf("%+v\n", user)
*/
func (j *Jira) User(username string) (*User, error) {
url := j.BaseUrl + j.ApiPath + user_url + "?username=" + username
contents := j.buildAndExecRequest("GET", url, nil)
user := new(User)
err := json.Unmarshal(contents, &user)
if err != nil {
fmt.Println("%s", err)
}
return user, err
}
/*
Returns a user with groups. This resource cannot be accessed anonymously.
GET http://example.com:8080/jira/rest/api/2/user?username=USERNAME&expand=groups
Parameters
username string The username
Usage
user, err := jira.User("username")
if err != nil {
fmt.Println(err.Error())
}
fmt.Printf("%+v\n", user)
*/
func (j *Jira) UserExt(username string) (*User, error) {
url := j.BaseUrl + j.ApiPath + user_url + "?username=" + username + "&expand=groups"
contents := j.buildAndExecRequest("GET", url, nil)
user := new(User)
err := json.Unmarshal(contents, &user)
if err != nil {
return nil, err
}
if user.Self == "" {
return nil, fmt.Errorf("User not found.")
}
return user, err
}
/*
Returns a list of users that match the search string. This resource cannot be accessed anonymously.
GET http://example.com:8080/jira/rest/api/2/user/search
Parameters
username string A query string used to search username, name or e-mail address
startAt int The index of the first user to return (0-based)
maxResults int The maximum number of users to return (defaults to 50).
The maximum allowed value is 1000.
If you specify a value that is higher than this number,
your search results will be truncated.
includeActive boolean If true, then active users are included in the results (default true)
includeInactive boolean If true, then inactive users are included in the results (default false)
*/
func (j *Jira) SearchUser(username string, startAt int, maxResults int, includeActive bool, includeInactive bool) {
url := j.BaseUrl + j.ApiPath + user_url + "?username=" + username
contents := j.buildAndExecRequest("GET", url, nil)
fmt.Println(string(contents))
// @todo
}
func (j *Jira) UserActivity(user string) (ActivityFeed, error) {
url := j.BaseUrl + j.ActivityPath + "?streams=" + url.QueryEscape("user IS "+user)
return j.Activity(url)
}
func (j *Jira) Activity(url string) (ActivityFeed, error) {
contents := j.buildAndExecRequest("GET", url, nil)
var activity ActivityFeed
err := xml.Unmarshal(contents, &activity)
if err != nil {
fmt.Println("%s", err)
}
return activity, err
}
type ActivityItem struct {
Title string `xml:"title"json:"title"`
Id string `xml:"id"json:"id"`
Link []Link `xml:"link"json:"link"`
Updated time.Time `xml:"updated"json:"updated"`
Author Person `xml:"author"json:"author"`
Summary Text `xml:"summary"json:"summary"`
Category Category `xml:"category"json:"category"`
}
type ActivityFeed struct {
XMLName xml.Name `xml:"http://www.w3.org/2005/Atom feed"json:"xml_name"`
Title string `xml:"title"json:"title"`
Id string `xml:"id"json:"id"`
Link []Link `xml:"link"json:"link"`
Updated time.Time `xml:"updated,attr"json:"updated"`
Author Person `xml:"author"json:"author"`
Entries []*ActivityItem `xml:"entry"json:"entries"`
}
type Category struct {
Term string `xml:"term,attr"json:"term"`
}
type Link struct {
Rel string `xml:"rel,attr,omitempty"json:"rel"`
Href string `xml:"href,attr"json:"href"`
}
type Person struct {
Name string `xml:"name"json:"name"`
URI string `xml:"uri"json:"uri"`
Email string `xml:"email"json:"email"`
InnerXML string `xml:",innerxml"json:"inner_xml"`
}
type Text struct {
Type string `xml:"type,attr,omitempty"json:"type"`
Body string `xml:",chardata"json:"body"`
}
type Groups struct {
Size int `json:"size"`
Items []Group `json:"items"`
}
type Group struct {
Name string `json:"name"`
Self string `json:"self"`
Users *Users `json:"users"`
}
type Users struct {
Size int `json:"size"`
Items []User `json:"items"`
}
/*
Returns user names within group. This resource cannot be accessed anonymously.
GET http://example.com:8080/jira/rest/api/2/group?groupname=NAME&expand=users [GET]
Parameters
groupname string The groupname
Usage
users, err := jira.UsersInGroup("groupname")
if err != nil {
fmt.Println(err.Error())
}
fmt.Printf("%+v\n", users)
*/
func (j *Jira) UsersInGroup(groupname string) (*Group, error) {
url := j.BaseUrl + j.ApiPath + grp_url + "?groupname=" + groupname + "&expand=users"
contents := j.buildAndExecRequest("GET", url, nil)
group := new(Group)
err := json.Unmarshal(contents, &group)
if err != nil {
fmt.Println("%s", err)
}
return group, err
}
/*
Adds user to group.
POST /rest/api/2/group/user?groupname=NAME
Parameters
group name
user name
*/
func (j *Jira) AddUserToGroup(groupname string, username string) error {
data := strings.NewReader(fmt.Sprintf("{\"name\":\"%s\"}", username))
url := j.BaseUrl + j.ApiPath + grp_url + "/user?groupname=" + groupname
j.buildAndExecRequest("POST", url, data)
return nil
}
/*
Adds user
POST /rest/api/2/user
Parameters
user name
full name
email
*/
func (j *Jira) AddUser(username string, fullname string, email string) error {
data := strings.NewReader(fmt.Sprintf("{\"name\":\"%s\",\"emailAddress\":\"%s\",\"displayName\":\"%s\"}", username, email, fullname))
url := j.BaseUrl + j.ApiPath + user_url
j.buildAndExecRequest("POST", url, data)
return nil
}
/*
removes user from group.
DELETE /rest/api/2/group/user?groupname=NAME&username=NAME
Parameters
group name
user name
*/
func (j *Jira) DelUserFromGroup(groupname string, username string) error {
url := j.BaseUrl + j.ApiPath + grp_url + "/user?groupname=" + groupname + "&username=" + username
j.buildAndExecRequest("DELETE", url, nil)
return nil
}