Skip to content

Commit 282b897

Browse files
Add support for multiple user emails (#52)
Co-authored-by: Oleksandr Redko <[email protected]>
1 parent 516ad51 commit 282b897

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

internal/app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type App struct {
3333

3434
type User struct {
3535
Name string
36-
Email string
36+
Emails []string
3737
Username string
3838
CreatedAt time.Time
3939
}

internal/gitlab.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"log"
7+
"slices"
78
"strings"
89
"time"
910

@@ -33,9 +34,19 @@ func (s *GitLab) CurrentUser(ctx context.Context) (*User, error) {
3334
return nil, fmt.Errorf("get current user: %w", err)
3435
}
3536

37+
emails, _, err := s.gitlabClient.Users.ListEmails(gitlab.WithContext(ctx))
38+
if err != nil {
39+
return nil, fmt.Errorf("get user emails: %w", err)
40+
}
41+
42+
emailAddresses := make([]string, 0, len(emails))
43+
for _, email := range emails {
44+
emailAddresses = append(emailAddresses, email.Email)
45+
}
46+
3647
return &User{
3748
Name: user.Name,
38-
Email: user.Email,
49+
Emails: emailAddresses,
3950
Username: user.Username,
4051
CreatedAt: *user.CreatedAt,
4152
}, nil
@@ -100,7 +111,7 @@ func (s *GitLab) HasUserContributions(ctx context.Context, user *User, projectID
100111
}
101112

102113
for _, c := range contrs {
103-
if strings.EqualFold(c.Email, user.Email) {
114+
if contains(user.Emails, c.Email) {
104115
return true
105116
}
106117
}
@@ -161,7 +172,7 @@ func (s *GitLab) fetchCommitPage(
161172
}
162173

163174
for _, comm := range comms {
164-
if !strings.EqualFold(comm.AuthorEmail, user.Email) || !strings.EqualFold(comm.CommitterEmail, user.Email) {
175+
if !contains(user.Emails, comm.AuthorEmail) || !contains(user.Emails, comm.CommitterEmail) {
165176
continue
166177
}
167178

@@ -182,3 +193,10 @@ func (s *GitLab) fetchCommitPage(
182193

183194
return commits, resp.NextPage, nil
184195
}
196+
197+
// contains checks if a string `v` is in the slice `s`, ignoring case.
198+
func contains(s []string, v string) bool {
199+
return slices.ContainsFunc(s, func(item string) bool {
200+
return strings.EqualFold(item, v)
201+
})
202+
}

internal/gitlab_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,17 @@ func newCurrentUser(t *testing.T, gitlabClient *gitlab.Client) *app.User {
4444
user, _, err := gitlabClient.Users.CurrentUser()
4545
require.NoError(t, err)
4646

47+
emails, _, err := gitlabClient.Users.ListEmails()
48+
require.NoError(t, err)
49+
50+
emailAddresses := make([]string, 0, len(emails))
51+
for _, email := range emails {
52+
emailAddresses = append(emailAddresses, email.Email)
53+
}
54+
4755
return &app.User{
4856
Name: user.Name,
49-
Email: user.Email,
57+
Emails: emailAddresses,
5058
CreatedAt: *user.CreatedAt,
5159
}
5260
}

0 commit comments

Comments
 (0)