diff --git a/onpremise/issue.go b/onpremise/issue.go index d1bca7aa..d7227e80 100644 --- a/onpremise/issue.go +++ b/onpremise/issue.go @@ -250,7 +250,7 @@ type Watches struct { type Watcher struct { Self string `json:"self,omitempty" structs:"self,omitempty"` Name string `json:"name,omitempty" structs:"name,omitempty"` - AccountID string `json:"accountId,omitempty" structs:"accountId,omitempty"` + Key string `json:"key,omitempty" structs:"key,omitempty"` DisplayName string `json:"displayName,omitempty" structs:"displayName,omitempty"` Active bool `json:"active,omitempty" structs:"active,omitempty"` } @@ -1364,8 +1364,8 @@ func (s *IssueService) GetWatchers(ctx context.Context, issueID string) (*[]User result := []User{} for _, watcher := range watches.Watchers { var user *User - if watcher.AccountID != "" { - user, resp, err = s.client.User.GetByAccountID(context.Background(), watcher.AccountID) + if watcher.Name != "" { + user, resp, err = s.client.User.Get(context.Background(), watcher.Key) if err != nil { return nil, resp, NewJiraError(resp, err) } diff --git a/onpremise/issue_test.go b/onpremise/issue_test.go index 36b6aa31..4e19e4cf 100644 --- a/onpremise/issue_test.go +++ b/onpremise/issue_test.go @@ -1590,14 +1590,14 @@ func TestIssueService_GetWatchers(t *testing.T) { testMethod(t, r, http.MethodGet) testRequestURL(t, r, "/rest/api/2/issue/10002/watchers") - fmt.Fprint(w, `{"self":"http://www.example.com/jira/rest/api/2/issue/EX-1/watchers","isWatching":false,"watchCount":1,"watchers":[{"self":"http://www.example.com/jira/rest/api/2/user?accountId=000000000000000000000000","accountId": "000000000000000000000000","displayName":"Fred F. User","active":false}]}`) + fmt.Fprint(w, `{"self":"http://www.example.com/jira/rest/api/2/issue/EX-1/watchers","isWatching":false,"watchCount":1,"watchers":[{"self":"http://www.example.com/jira/rest/api/2/user?username=fred","name": "fred","displayName":"Fred F. User","active":false}]}`) }) testMux.HandleFunc("/rest/api/2/user", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodGet) - testRequestURL(t, r, "/rest/api/2/user?accountId=000000000000000000000000") + testRequestURL(t, r, "/rest/api/2/user?key=") - fmt.Fprint(w, `{"self":"http://www.example.com/jira/rest/api/2/user?accountId=000000000000000000000000","key":"fred","accountId": "000000000000000000000000", + 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":[ @@ -1619,8 +1619,8 @@ func TestIssueService_GetWatchers(t *testing.T) { t.Errorf("Expected 1 watcher, got: %d", len(*watchers)) return } - if (*watchers)[0].AccountID != "000000000000000000000000" { - t.Error("Expected watcher accountId 000000000000000000000000") + if (*watchers)[0].Name != "fred" { + t.Error("Expected watcher accountId fred") } } @@ -1631,14 +1631,14 @@ func TestIssueService_DeprecatedGetWatchers(t *testing.T) { testMethod(t, r, http.MethodGet) testRequestURL(t, r, "/rest/api/2/issue/10002/watchers") - fmt.Fprint(w, `{"self":"http://www.example.com/jira/rest/api/2/issue/EX-1/watchers","isWatching":false,"watchCount":1,"watchers":[{"self":"http://www.example.com/jira/rest/api/2/user?accountId=000000000000000000000000", "accountId": "000000000000000000000000", "displayName":"Fred F. User","active":false}]}`) + fmt.Fprint(w, `{"self":"http://www.example.com/jira/rest/api/2/issue/EX-1/watchers","isWatching":false,"watchCount":1,"watchers":[{"self":"http://www.example.com/jira/rest/api/2/user?username=fred","name": "fred","displayName":"Fred F. User","active":false}]}`) }) testMux.HandleFunc("/rest/api/2/user", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodGet) - testRequestURL(t, r, "/rest/api/2/user?accountId=000000000000000000000000") + testRequestURL(t, r, "/rest/api/2/user?key=") - fmt.Fprint(w, `{"self":"http://www.example.com/jira/rest/api/2/user?accountId=000000000000000000000000", "accountId": "000000000000000000000000", "key": "", "name": "", "emailAddress":"fred@example.com","avatarUrls":{"48x48":"http://www.example.com/jira/secure/useravatar?size=large&ownerId=fred", + fmt.Fprint(w, `{"self":"http://www.example.com/jira/rest/api/2/user?username=fred", "name": "fred", "key": "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", @@ -1659,8 +1659,8 @@ func TestIssueService_DeprecatedGetWatchers(t *testing.T) { t.Errorf("Expected 1 watcher, got: %d", len(*watchers)) return } - if (*watchers)[0].AccountID != "000000000000000000000000" { - t.Error("Expected accountId 000000000000000000000000") + if (*watchers)[0].Name != "fred" { + t.Error("Expected accountId fred") } } diff --git a/onpremise/user.go b/onpremise/user.go index bcbe30aa..23a24dc6 100644 --- a/onpremise/user.go +++ b/onpremise/user.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "html" "net/http" ) @@ -44,14 +45,12 @@ type userSearch []userSearchParam type userSearchF func(userSearch) userSearch -// Get gets user info from Jira using its Account Id +// Get gets user info from Jira using its key // -// Jira API docs: https://developer.atlassian.com/cloud/jira/platform/rest/v2/#api-rest-api-2-user-get +// Jira API doc: https://docs.atlassian.com/software/jira/docs/api/REST/9.9.0/#api/2/user-getUser // -// 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) Get(ctx context.Context, accountId string) (*User, *Response, error) { - apiEndpoint := fmt.Sprintf("/rest/api/2/user?accountId=%s", accountId) +func (s *UserService) Get(ctx context.Context, key string) (*User, *Response, error) { + apiEndpoint := fmt.Sprintf("/rest/api/2/user?key=%s", html.EscapeString(key)) req, err := s.client.NewRequest(ctx, http.MethodGet, apiEndpoint, nil) if err != nil { return nil, nil, err @@ -65,15 +64,12 @@ func (s *UserService) Get(ctx context.Context, accountId string) (*User, *Respon return user, resp, nil } -// GetByAccountID gets user info from Jira -// Searching by another parameter that is not accountId is deprecated, -// but this method is kept for backwards compatibility -// Jira API docs: https://docs.atlassian.com/jira/REST/cloud/#api/2/user-getUser +// GetByUsername gets user info from Jira // -// 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) GetByAccountID(ctx context.Context, accountID string) (*User, *Response, error) { - apiEndpoint := fmt.Sprintf("/rest/api/2/user?accountId=%s", accountID) +// Jira API doc: https://docs.atlassian.com/software/jira/docs/api/REST/9.9.0/#api/2/user-getUser +// +func (s *UserService) GetByUsername(ctx context.Context, username string) (*User, *Response, error) { + apiEndpoint := fmt.Sprintf("/rest/api/2/user?username=%s", html.EscapeString(username)) req, err := s.client.NewRequest(ctx, http.MethodGet, apiEndpoint, nil) if err != nil { return nil, nil, err diff --git a/onpremise/user_test.go b/onpremise/user_test.go index 273bc3a0..a223853d 100644 --- a/onpremise/user_test.go +++ b/onpremise/user_test.go @@ -12,7 +12,7 @@ func TestUserService_Get_Success(t *testing.T) { defer teardown() testMux.HandleFunc("/rest/api/2/user", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodGet) - testRequestURL(t, r, "/rest/api/2/user?accountId=000000000000000000000000") + testRequestURL(t, r, "/rest/api/2/user?key=") 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", @@ -23,21 +23,21 @@ func TestUserService_Get_Success(t *testing.T) { }]},"applicationRoles":{"size":1,"items":[]},"expand":"groups,applicationRoles"}`) }) - if user, _, err := testClient.User.Get(context.Background(), "000000000000000000000000"); err != nil { + if user, _, err := testClient.User.Get(context.Background(), "fred"); err != nil { t.Errorf("Error given: %s", err) } else if user == nil { t.Error("Expected user. User is nil") } } -func TestUserService_GetByAccountID_Success(t *testing.T) { +func TestUserService_GetByUsername_Success(t *testing.T) { setup() defer teardown() testMux.HandleFunc("/rest/api/2/user", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodGet) - testRequestURL(t, r, "/rest/api/2/user?accountId=000000000000000000000000") + testRequestURL(t, r, "/rest/api/2/user?username=fred") - fmt.Fprint(w, `{"self":"http://www.example.com/jira/rest/api/2/user?accountId=000000000000000000000000","accountId": "000000000000000000000000", + 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":[ @@ -46,7 +46,7 @@ func TestUserService_GetByAccountID_Success(t *testing.T) { }]},"applicationRoles":{"size":1,"items":[]},"expand":"groups,applicationRoles"}`) }) - if user, _, err := testClient.User.GetByAccountID(context.Background(), "000000000000000000000000"); err != nil { + if user, _, err := testClient.User.GetByUsername(context.Background(), "fred"); err != nil { t.Errorf("Error given: %s", err) } else if user == nil { t.Error("Expected user. User is nil")