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 support to the Get User API functions for Key and Username #635

Open
wants to merge 2 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions onpremise/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down Expand Up @@ -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)
}
Expand Down
20 changes: 10 additions & 10 deletions onpremise/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":[
Expand All @@ -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")
}
}

Expand All @@ -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",
Expand All @@ -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")
}
}

Expand Down
24 changes: 10 additions & 14 deletions onpremise/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"html"
"net/http"
)

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions onpremise/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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":[
Expand All @@ -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")
Expand Down