Skip to content

Commit

Permalink
fix: [CDS-67745]: fix find user email api for bitbucket in go-scm (#255)
Browse files Browse the repository at this point in the history
Co-authored-by: Mohit Garg <[email protected]>
  • Loading branch information
shalini-agr and mohitg0795 authored Apr 28, 2023
1 parent de2a54e commit 194f53f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
8 changes: 8 additions & 0 deletions scm/driver/bitbucket/testdata/userEmail.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Values": [
{
"email": "[email protected]",
"is_primary": true
}
]
}
22 changes: 21 additions & 1 deletion scm/driver/bitbucket/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,24 @@ func (s *userService) FindLogin(ctx context.Context, login string) (*scm.User, *
}

func (s *userService) FindEmail(ctx context.Context) (string, *scm.Response, error) {
return "", nil, scm.ErrNotSupported
out := new(emails)
res, err := s.client.do(ctx, "GET", "2.0/user/emails", nil, &out)
return convertEmailList(out), res, err
}

func (s *userService) ListEmail(context.Context, scm.ListOptions) ([]*scm.Email, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}

func convertEmailList(from *emails) string {
for _, v := range from.Values {
if v.IsPrimary == true {
return v.Email
}
}
return ""
}

type user struct {
// The `username` field is no longer available after 29 April 2019 in
// accordance with GDPR regulations. See:
Expand All @@ -53,6 +64,15 @@ type user struct {
UUID string `json:"uuid"`
}

type email struct {
Email string `json:"email"`
IsPrimary bool `json:"is_primary"`
}

type emails struct {
Values []*email `json:"values"`
}

func convertUser(from *user) *scm.User {
return &scm.User{
Avatar: fmt.Sprintf("https://bitbucket.org/account/%s/avatar/32/", from.Username),
Expand Down
21 changes: 18 additions & 3 deletions scm/driver/bitbucket/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,24 @@ func TestUserLoginFind(t *testing.T) {
}

func TestUserFindEmail(t *testing.T) {
defer gock.Off()

gock.New("https://api.bitbucket.org").
Get("/2.0/user/emails").
Reply(200).
Type("application/json").
File("testdata/userEmail.json")

client, _ := New("https://api.bitbucket.org")
_, _, err := client.Users.FindEmail(context.Background())
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
got, _, err := client.Users.FindEmail(context.Background())
if err != nil {
t.Error(err)
}

want := "[email protected]"

if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}

0 comments on commit 194f53f

Please sign in to comment.