Skip to content

Commit

Permalink
feat: [CDS-69341]: add find user email api for github in go-scm (#256)
Browse files Browse the repository at this point in the history
* feat: [CDS-69341]: add find user email api for github in go-scm
  • Loading branch information
shalini-agr authored May 15, 2023
1 parent 194f53f commit 5877f38
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
13 changes: 11 additions & 2 deletions scm/driver/github/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func (s *userService) FindLogin(ctx context.Context, login string) (*scm.User, *
}

func (s *userService) FindEmail(ctx context.Context) (string, *scm.Response, error) {
user, res, err := s.Find(ctx)
return user.Email, res, err
out, res, err := s.ListEmail(ctx, scm.ListOptions{})
return returnPrimaryEmail(out), res, err
}

func (s *userService) ListEmail(ctx context.Context, opts scm.ListOptions) ([]*scm.Email, *scm.Response, error) {
Expand Down Expand Up @@ -69,6 +69,15 @@ func convertUser(from *user) *scm.User {
}
}

func returnPrimaryEmail(from []*scm.Email) string {
for _, v := range from {
if v.Primary == true {
return v.Value
}
}
return ""
}

// helper function to convert from the github email list to
// the common email structure.
func convertEmailList(from []*email) []*scm.Email {
Expand Down
4 changes: 2 additions & 2 deletions scm/driver/github/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ func TestUserEmailFind(t *testing.T) {
defer gock.Off()

gock.New("https://api.github.com").
Get("/user").
Get("/user/emails").
Reply(200).
Type("application/json").
SetHeader("X-GitHub-Request-Id", "DD0E:6011:12F21A8:1926790:5A2064E2").
SetHeader("X-RateLimit-Limit", "60").
SetHeader("X-RateLimit-Remaining", "59").
SetHeader("X-RateLimit-Reset", "1512076018").
File("testdata/user.json")
File("testdata/emails.json")

client := NewDefault()
result, res, err := client.Users.FindEmail(context.Background())
Expand Down

0 comments on commit 5877f38

Please sign in to comment.