Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FindAssignable method to User #655

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions cloud/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,22 @@ func WithAccountId(accountId string) UserSearchF {
}
}

// WithIssueKey sets the issue key to search
func WithIssueKey(issueKey string) UserSearchF {
return func(s UserSearch) UserSearch {
s = append(s, UserSearchParam{name: "issueKey", value: issueKey})
return s
}
}

// WithProject sets the project to search
func WithProject(project string) UserSearchF {
return func(s UserSearch) UserSearch {
s = append(s, UserSearchParam{name: "project", value: project})
return s
}
}

// WithProperty sets the property (Property keys are specified by path) to search
func WithProperty(property string) UserSearchF {
return func(s UserSearch) UserSearch {
Expand Down Expand Up @@ -301,3 +317,35 @@ func (s *UserService) Find(ctx context.Context, property string, tweaks ...UserS
}
return users, resp, nil
}

// FindAssignable gets all assignable users for an issue:
//
// Accepts username, project, issueKey, startAt, and maxResults as tweaks
//
// https://docs.atlassian.com/software/jira/docs/api/REST/1000.1568.0/#api/2/user-findAssignableUsers
//
// TODO Double check this method if this works as expected, is using the latest API and the response is complete
// This double check effort is done for v2 - Remove this two lines if this is completed.
func (s *UserService) FindAssignable(ctx context.Context, tweaks ...UserSearchF) ([]User, *Response, error) {
search := []UserSearchParam{}
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/assignable/search?%s", queryString)
req, err := s.client.NewRequest(ctx, http.MethodGet, apiEndpoint, nil)
if err != nil {
return nil, nil, err
}

users := []User{}
resp, err := s.client.Do(req, &users)
if err != nil {
return nil, resp, NewJiraError(resp, err)
}
return users, resp, nil
}
23 changes: 23 additions & 0 deletions cloud/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,26 @@ func TestUserService_Find_SuccessParams(t *testing.T) {
t.Error("Expected user. User is nil")
}
}

func TestUserService_FindAssignable_Success(t *testing.T) {
setup()
defer teardown()
testMux.HandleFunc("/rest/api/2/user/assignable/search", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
testRequestURL(t, r, "/rest/api/2/user/assignable/[email protected]&project=DAPHNE&issueKey=DAPHNE-11&maxResults=100&startAt=0")

fmt.Fprint(w, `[{"self":"http://www.example.com/jira/rest/api/2/user?accountId=000000000000000000000000","key":"fred",
"name":"fred","emailAddress":"[email protected]","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.FindAssignable(context.Background(), WithUsername("[email protected]"), WithProject("DAPHNE"), WithIssueKey("DAPHNE-11"), WithMaxResults(100), WithStartAt(0)); err != nil {
t.Errorf("Error given: %s", err)
} else if user == nil {
t.Error("Expected user. User is nil")
}
}