diff --git a/cloud/user.go b/cloud/user.go index 1a8fea84..8d9efac0 100644 --- a/cloud/user.go +++ b/cloud/user.go @@ -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 { @@ -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 +} diff --git a/cloud/user_test.go b/cloud/user_test.go index 7e5fdb26..a7259c62 100644 --- a/cloud/user_test.go +++ b/cloud/user_test.go @@ -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/search?username=fred@example.com&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":"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.FindAssignable(context.Background(), WithUsername("fred@example.com"), 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") + } +}