Skip to content

Commit

Permalink
fix(IssueService.GetWatchers): UserService.GetByAccountID support acc…
Browse files Browse the repository at this point in the history
…ountId params
  • Loading branch information
clementlecorre authored and ghostsquad committed Mar 11, 2020
1 parent a90dd87 commit 436469b
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 3 deletions.
15 changes: 12 additions & 3 deletions issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,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"`
DisplayName string `json:"displayName,omitempty" structs:"displayName,omitempty"`
Active bool `json:"active,omitempty" structs:"active,omitempty"`
}
Expand Down Expand Up @@ -1236,9 +1237,17 @@ func (s *IssueService) GetWatchers(issueID string) (*[]User, *Response, error) {
result := []User{}
user := new(User)
for _, watcher := range watches.Watchers {
user, resp, err = s.client.User.Get(watcher.Name)
if err != nil {
return nil, resp, NewJiraError(resp, err)
if watcher.AccountID != "" {
user, resp, err = s.client.User.GetByAccountID(watcher.AccountID)
if err != nil {
return nil, resp, NewJiraError(resp, err)
}
} else {
// try fallback deprecated method
user, resp, err = s.client.User.Get(watcher.Name)
if err != nil {
return nil, resp, NewJiraError(resp, err)
}
}
result = append(result, *user)
}
Expand Down
41 changes: 41 additions & 0 deletions issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1495,6 +1495,47 @@ func TestIssueService_GetWorklogs(t *testing.T) {
}

func TestIssueService_GetWatchers(t *testing.T) {
setup()
defer teardown()
testMux.HandleFunc("/rest/api/2/issue/10002/watchers", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
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}]}`)
})

testMux.HandleFunc("/rest/api/2/user", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testRequestURL(t, r, "/rest/api/2/user?accountId=000000000000000000000000")

fmt.Fprint(w, `{"self":"http://www.example.com/jira/rest/api/2/user?accountId=000000000000000000000000","key":"fred","accountId": "000000000000000000000000",
"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"}`)
})

watchers, _, err := testClient.Issue.GetWatchers("10002")
if err != nil {
t.Errorf("Error given: %s", err)
return
}
if watchers == nil {
t.Error("Expected watchers. Watchers is nil")
return
}
if len(*watchers) != 1 {
t.Errorf("Expected 1 watcher, got: %d", len(*watchers))
return
}
if (*watchers)[0].AccountID != "000000000000000000000000" {
t.Error("Expected watcher accountId 000000000000000000000000")
}
}

func TestIssueService_DeprecatedGetWatchers(t *testing.T) {
setup()
defer teardown()
testMux.HandleFunc("/rest/api/2/issue/10002/watchers", func(w http.ResponseWriter, r *http.Request) {
Expand Down
22 changes: 22 additions & 0 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ 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
//
// /!\ Deprecation notice: https://developer.atlassian.com/cloud/jira/platform/deprecation-notice-user-privacy-api-migration-guide/
// By 29 April 2019, we will remove personal data from the API that is used to identify users,
// such as username and userKey, and instead use the Atlassian account ID (accountId).
func (s *UserService) Get(username string) (*User, *Response, error) {
apiEndpoint := fmt.Sprintf("/rest/api/2/user?username=%s", username)
req, err := s.client.NewRequest("GET", apiEndpoint, nil)
Expand All @@ -65,6 +69,24 @@ func (s *UserService) Get(username string) (*User, *Response, error) {
return user, resp, nil
}

// Get gets user info from JIRA
//
// JIRA API docs: https://docs.atlassian.com/jira/REST/cloud/#api/2/user-getUser
func (s *UserService) GetByAccountID(accountID string) (*User, *Response, error) {
apiEndpoint := fmt.Sprintf("/rest/api/2/user?accountId=%s", accountID)
req, err := s.client.NewRequest("GET", apiEndpoint, nil)
if err != nil {
return nil, nil, err
}

user := new(User)
resp, err := s.client.Do(req, user)
if err != nil {
return nil, resp, NewJiraError(resp, err)
}
return user, resp, nil
}

// Create creates an user in JIRA.
//
// JIRA API docs: https://docs.atlassian.com/jira/REST/cloud/#api/2/user-createUser
Expand Down
23 changes: 23 additions & 0 deletions user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,29 @@ func TestUserService_Get_Success(t *testing.T) {
}
}

func TestUserService_GetByAccountID_Success(t *testing.T) {
setup()
defer teardown()
testMux.HandleFunc("/rest/api/2/user", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testRequestURL(t, r, "/rest/api/2/user?accountId=000000000000000000000000")

fmt.Fprint(w, `{"self":"http://www.example.com/jira/rest/api/2/user?accountId=000000000000000000000000","accountId": "000000000000000000000000",
"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.GetByAccountID("000000000000000000000000"); err != nil {
t.Errorf("Error given: %s", err)
} else if user == nil {
t.Error("Expected user. User is nil")
}
}

func TestUserService_Create(t *testing.T) {
setup()
defer teardown()
Expand Down

0 comments on commit 436469b

Please sign in to comment.