Skip to content

Commit

Permalink
Add support for multiple user emails
Browse files Browse the repository at this point in the history
The code now fetches a list of user emails instead of a single email address per user. The user structure and corresponding parts of the code have been updated to handle and check multiple email addresses. Additionally, a helper function is added to check for the existence of an email in an email list.
  • Loading branch information
fdobrovolny committed Feb 29, 2024
1 parent 8a4c509 commit aa36b2c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion internal/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type App struct {

type User struct {
Name string
Email string
Emails []string
Username string
CreatedAt time.Time
}
Expand Down
30 changes: 27 additions & 3 deletions internal/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,22 @@ func (s *GitLab) CurrentUser(ctx context.Context) (*User, error) {
return nil, fmt.Errorf("get current user: %w", err)
}

// Get the list of email addresses for the user.
emails, _, err := s.gitlabClient.Users.ListEmails(gitlab.WithContext(ctx))
if err != nil {
return nil, fmt.Errorf("get user emails: %w", err)
}

// Extract email addresses from the response.
emailAddresses := make([]string, 0, 10)

for _, email := range emails {
emailAddresses = append(emailAddresses, email.Email)
}

return &User{
Name: user.Name,
Email: user.Email,
Emails: emailAddresses,
Username: user.Username,
CreatedAt: *user.CreatedAt,
}, nil
Expand Down Expand Up @@ -100,7 +113,7 @@ func (s *GitLab) HasUserContributions(ctx context.Context, user *User, projectID
}

for _, c := range contrs {
if strings.EqualFold(c.Email, user.Email) {
if contains(user.Emails, c.Email) {
return true
}
}
Expand Down Expand Up @@ -162,7 +175,7 @@ func (s *GitLab) fetchCommitPage(
}

for _, comm := range comms {
if !strings.EqualFold(comm.AuthorEmail, user.Email) || !strings.EqualFold(comm.CommitterEmail, user.Email) {
if !contains(user.Emails, comm.AuthorEmail) || !contains(user.Emails, comm.CommitterEmail) {
continue
}

Expand All @@ -183,3 +196,14 @@ func (s *GitLab) fetchCommitPage(

return commits, resp.NextPage, nil
}

// Helper function to check if an email exists in the list.
func contains(emails []string, email string) bool {
for _, e := range emails {
if strings.EqualFold(e, email) {
return true
}
}

return false
}
11 changes: 10 additions & 1 deletion internal/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,18 @@ func newCurrentUser(t *testing.T, gitlabClient *gitlab.Client) *app.User {
user, _, err := gitlabClient.Users.CurrentUser()
require.NoError(t, err)

emails, _, err := gitlabClient.Users.ListEmails()
require.NoError(t, err)

emailAddresses := make([]string, 0, 10)

for _, email := range emails {
emailAddresses = append(emailAddresses, email.Email)
}

return &app.User{
Name: user.Name,
Email: user.Email,
Emails: emailAddresses,
CreatedAt: *user.CreatedAt,
}
}

0 comments on commit aa36b2c

Please sign in to comment.