forked from go-jira/jira
-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.go
55 lines (49 loc) · 1.62 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
package jira
import (
"encoding/json"
"fmt"
"net/url"
"strings"
"github.com/go-jira/jira/jiradata"
)
type UserSearchOptions struct {
Query string `yaml:"query,omitempty" json:"query,omitempty"`
Username string `yaml:"username,omitempty" json:"username,omitempty"`
AccountID string `yaml:"accountId,omitempty" json:"accountId,omitempty"`
StartAt int `yaml:"startAt,omitempty" json:"startAt,omitempty"`
MaxResults int `yaml:"max-results,omitempty" json:"max-results,omitempty"`
Property string `yaml:"property,omitempty" json:"property,omitempty"`
}
// https://developer.atlassian.com/cloud/jira/platform/rest/v2/#api-rest-api-2-user-search-get
func UserSearch(ua HttpClient, endpoint string, opts *UserSearchOptions) ([]*jiradata.User, error) {
uri := URLJoin(endpoint, "rest/api/2/user/search")
params := []string{}
if opts.Query != "" {
params = append(params, "query="+url.QueryEscape(opts.Query))
}
if opts.AccountID != "" {
params = append(params, "accountId="+url.QueryEscape(opts.AccountID))
}
if opts.StartAt != 0 {
params = append(params, fmt.Sprintf("startAt=%d", opts.StartAt))
}
if opts.MaxResults != 0 {
params = append(params, fmt.Sprintf("maxResults=%d", opts.MaxResults))
}
if opts.Property != "" {
params = append(params, "property="+url.QueryEscape(opts.Property))
}
if len(params) > 0 {
uri += "?" + strings.Join(params, "&")
}
resp, err := ua.GetJSON(uri)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
results := []*jiradata.User{}
return results, json.NewDecoder(resp.Body).Decode(&results)
}
return nil, responseError(resp)
}