Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": "test@harness.io",
"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 := "test@harness.io"

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