From b01e0251cea2def663a33a6d9bed42035b958b35 Mon Sep 17 00:00:00 2001 From: Eugene Dzhurinsky Date: Thu, 28 Jun 2018 18:44:35 -0400 Subject: [PATCH] Added the method to get the profile of the currently logged in user. https://developer.atlassian.com/cloud/jira/platform/rest/#api-api-2-myself-get Signed-off-by: Eugene Dzhurinsky --- user.go | 17 +++++++++++++++++ user_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/user.go b/user.go index 7b2bd59..10fe5e0 100644 --- a/user.go +++ b/user.go @@ -99,6 +99,23 @@ func (s *UserService) GetGroups(username string) (*[]UserGroup, *Response, error return userGroups, resp, nil } +// Get information about the current logged-in user +// +// JIRA API docs: https://developer.atlassian.com/cloud/jira/platform/rest/#api-api-2-myself-get +func (s *UserService) GetSelf() (*User, *Response, error) { + const apiEndpoint = "rest/api/2/myself" + req, err := s.client.NewRequest("GET", apiEndpoint, nil) + if err != nil { + return nil, nil, err + } + var user User + resp, err := s.client.Do(req, &user) + if err != nil { + return nil, resp, NewJiraError(resp, err) + } + return &user, resp, nil +} + // Find searches for user info from JIRA: // It can find users by email, username or name // diff --git a/user_test.go b/user_test.go index 5a7a6f8..d3a4d81 100644 --- a/user_test.go +++ b/user_test.go @@ -74,6 +74,34 @@ func TestUserService_GetGroups(t *testing.T) { } } +func TestUserService_GetSelf(t *testing.T) { + setup() + defer teardown() + testMux.HandleFunc("/rest/api/2/myself", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testRequestURL(t, r, "/rest/api/2/myself") + + w.WriteHeader(http.StatusCreated) + 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":[ + {"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.GetSelf(); err != nil { + t.Errorf("Error given: %s", err) + } else if user == nil { + t.Error("Expected user groups. []UserGroup is nil") + } else if user.Name != "fred" || + !user.Active || + user.DisplayName != "Fred F. User" { + t.Errorf("User JSON deserialized incorrectly") + } +} + func TestUserService_Find_Success(t *testing.T) { setup() defer teardown()