mirror of
https://github.com/interviewstreet/go-jira.git
synced 2025-01-24 03:16:18 +02:00
Added functions to provide optional parameters to the user search.
Signed-off-by: Eugene Dzhurinsky <jdevelop@gmail.com>
This commit is contained in:
parent
0298784c46
commit
ccc70f9aab
60
user.go
60
user.go
@ -33,6 +33,15 @@ type UserGroup struct {
|
||||
Name string `json:"name,omitempty" structs:"name,omitempty"`
|
||||
}
|
||||
|
||||
type userSearchParam struct {
|
||||
name string
|
||||
value string
|
||||
}
|
||||
|
||||
type userSearch []userSearchParam
|
||||
|
||||
type userSearchF func(userSearch) userSearch
|
||||
|
||||
// Get gets user info from JIRA
|
||||
//
|
||||
// JIRA API docs: https://docs.atlassian.com/jira/REST/cloud/#api/2/user-getUser
|
||||
@ -116,12 +125,59 @@ func (s *UserService) GetSelf() (*User, *Response, error) {
|
||||
return &user, resp, nil
|
||||
}
|
||||
|
||||
// WithMaxResults sets the max results to return
|
||||
func WithMaxResults(maxResults int) userSearchF {
|
||||
return func(s userSearch) userSearch {
|
||||
s = append(s, userSearchParam{name: "maxResults", value: fmt.Sprintf("%d", maxResults)})
|
||||
return s
|
||||
}
|
||||
}
|
||||
|
||||
// WithStartAt set the start pager
|
||||
func WithStartAt(startAt int) userSearchF {
|
||||
return func(s userSearch) userSearch {
|
||||
s = append(s, userSearchParam{name: "startAt", value: fmt.Sprintf("%d", startAt)})
|
||||
return s
|
||||
}
|
||||
}
|
||||
|
||||
// WithActive sets the active users lookup
|
||||
func WithActive(active bool) userSearchF {
|
||||
return func(s userSearch) userSearch {
|
||||
s = append(s, userSearchParam{name: "includeActive", value: fmt.Sprintf("%t", active)})
|
||||
return s
|
||||
}
|
||||
}
|
||||
|
||||
// WithInactive sets the inactive users lookup
|
||||
func WithInactive(inactive bool) userSearchF {
|
||||
return func(s userSearch) userSearch {
|
||||
s = append(s, userSearchParam{name: "includeInactive", value: fmt.Sprintf("%t", inactive)})
|
||||
return s
|
||||
}
|
||||
}
|
||||
|
||||
// Find searches for user info from JIRA:
|
||||
// It can find users by email, username or name
|
||||
//
|
||||
// JIRA API docs: https://docs.atlassian.com/jira/REST/cloud/#api/2/user-findUsers
|
||||
func (s *UserService) Find(property string) ([]User, *Response, error) {
|
||||
apiEndpoint := fmt.Sprintf("/rest/api/2/user/search?username=%s", property)
|
||||
func (s *UserService) Find(property string, tweaks ...userSearchF) ([]User, *Response, error) {
|
||||
search := []userSearchParam{
|
||||
{
|
||||
name: "username",
|
||||
value: property,
|
||||
},
|
||||
}
|
||||
for _, f := range tweaks {
|
||||
search = f(search)
|
||||
}
|
||||
|
||||
var queryString = ""
|
||||
for _, param := range search {
|
||||
queryString += param.name + "=" + param.value + "&"
|
||||
}
|
||||
|
||||
apiEndpoint := fmt.Sprintf("/rest/api/2/user/search?" + queryString[:len(queryString)-1])
|
||||
req, err := s.client.NewRequest("GET", apiEndpoint, nil)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
|
23
user_test.go
23
user_test.go
@ -124,3 +124,26 @@ func TestUserService_Find_Success(t *testing.T) {
|
||||
t.Error("Expected user. User is nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserService_Find_SuccessParams(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
testMux.HandleFunc("/rest/api/2/user/search", func(w http.ResponseWriter, r *http.Request) {
|
||||
testMethod(t, r, "GET")
|
||||
testRequestURL(t, r, "/rest/api/2/user/search?username=fred@example.com&startAt=100&maxResults=1000")
|
||||
|
||||
fmt.Fprint(w, `[{"self":"http://www.example.com/jira/rest/api/2/user?username=fred","key":"fred",
|
||||
"name":"fred","emailAddress":"fred@example.com","avatarUrls":{"48x48":"http://www.example.com/jira/secure/useravatar?size=large&ownerId=fred",
|
||||
"24x24":"http://www.example.com/jira/secure/useravatar?size=small&ownerId=fred","16x16":"http://www.example.com/jira/secure/useravatar?size=xsmall&ownerId=fred",
|
||||
"32x32":"http://www.example.com/jira/secure/useravatar?size=medium&ownerId=fred"},"displayName":"Fred F. User","active":true,"timeZone":"Australia/Sydney","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"
|
||||
}]},"applicationRoles":{"size":1,"items":[]},"expand":"groups,applicationRoles"}]`)
|
||||
})
|
||||
|
||||
if user, _, err := testClient.User.Find("fred@example.com", WithStartAt(100), WithMaxResults(1000)); err != nil {
|
||||
t.Errorf("Error given: %s", err)
|
||||
} else if user == nil {
|
||||
t.Error("Expected user. User is nil")
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user