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
12 changes: 9 additions & 3 deletions cla-backend-go/github/github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,7 @@ func (u UserCommitSummary) getUserInfo(tagUser bool) string {
}

// SearchGithubUserByEmail searches for a GitHub user by email using the GitHub search API.
// Organizations are excluded because they can't author commits or sign a CLA.
// Returns the first found *github.User, or nil if not found or on error.
func SearchGithubUserByEmail(ctx context.Context, client *github.Client, email string) (*github.User, error) {
f := logrus.Fields{
Expand All @@ -801,7 +802,7 @@ func SearchGithubUserByEmail(ctx context.Context, client *github.Client, email s
}
log.WithFields(f).Debugf("Searching for GitHub user by email: %s", email)

query := fmt.Sprintf("%s in:email", email)
query := fmt.Sprintf("%s in:email type:user", email)
opts := &github.SearchOptions{
ListOptions: github.ListOptions{PerPage: 1},
}
Expand All @@ -816,8 +817,13 @@ func SearchGithubUserByEmail(ctx context.Context, client *github.Client, email s
log.WithFields(f).Debugf("No GitHub user found with email: %s", email)
return nil, nil
}
log.WithFields(f).Debugf("Found GitHub user by email: %s", *result.Users[0].Login)
return result.Users[0], nil
user := result.Users[0]
if strings.EqualFold(user.GetType(), "Organization") {
log.WithFields(f).Debugf("Ignoring GitHub organization found by email: %s", user.GetLogin())
return nil, nil
}
log.WithFields(f).Debugf("Found GitHub user by email: %s", user.GetLogin())
return user, nil
}

// GetGitHubUserByLogin fetches a GitHub user by their login (username).
Expand Down
30 changes: 30 additions & 0 deletions cla-backend-go/github/github_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,3 +447,33 @@ func TestIsValidGitHubUsernameAcceptsBotSuffix(t *testing.T) {
assert.False(t, IsValidGitHubUsername(username), username)
}
}

func TestSearchGithubUserByEmailExcludesOrganizationsFromQuery(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/search/users", r.URL.Path)
assert.Equal(t, "contact@example.com in:email type:user", r.URL.Query().Get("q"))
w.Header().Set("Content-Type", "application/json")
_, err := io.WriteString(w, `{"total_count": 1, "items": [{"login": "example-bot", "id": 2, "type": "User"}]}`)
assert.NoError(t, err)
}))
defer srv.Close()

user, err := SearchGithubUserByEmail(context.Background(), newTestGithubClient(t, srv), "contact@example.com")
assert.NoError(t, err)
if assert.NotNil(t, user) {
assert.Equal(t, "example-bot", user.GetLogin())
}
}

func TestSearchGithubUserByEmailIgnoresOrganizationResult(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, err := io.WriteString(w, `{"total_count": 1, "items": [{"login": "example", "id": 1, "type": "Organization"}]}`)
assert.NoError(t, err)
}))
defer srv.Close()

user, err := SearchGithubUserByEmail(context.Background(), newTestGithubClient(t, srv), "contact@example.com")
assert.NoError(t, err)
assert.Nil(t, user)
}
Loading