Replace clients.Contributor with clients.User (#1957)

Co-authored-by: Azeem Shaikh <azeems@google.com>
This commit is contained in:
Azeem Shaikh 2022-05-31 13:19:14 -07:00 committed by GitHub
parent f712144d00
commit 1d9cd05476
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 25 additions and 47 deletions

View File

@ -65,7 +65,7 @@ type CodeReviewData struct {
// ContributorsData represents contributor information. // ContributorsData represents contributor information.
type ContributorsData struct { type ContributorsData struct {
Users []clients.Contributor Users []clients.User
} }
// VulnerabilitiesData contains the raw results // VulnerabilitiesData contains the raw results

View File

@ -34,13 +34,13 @@ func TestContributors(t *testing.T) {
tests := []struct { tests := []struct {
err error err error
name string name string
contrib []clients.Contributor contrib []clients.User
expected checker.CheckResult expected checker.CheckResult
}{ }{
{ {
err: nil, err: nil,
name: "Two contributors without company", name: "Two contributors without company",
contrib: []clients.Contributor{ contrib: []clients.User{
{ {
Organizations: []clients.User{ Organizations: []clients.User{
{ {
@ -59,7 +59,7 @@ func TestContributors(t *testing.T) {
{ {
err: nil, err: nil,
name: "Valid contributors with enough contributors and companies", name: "Valid contributors with enough contributors and companies",
contrib: []clients.Contributor{ contrib: []clients.User{
{ {
Companies: []string{"company1"}, Companies: []string{"company1"},
@ -140,7 +140,7 @@ func TestContributors(t *testing.T) {
{ {
err: nil, err: nil,
name: "No contributors", name: "No contributors",
contrib: []clients.Contributor{}, contrib: []clients.User{},
expected: checker.CheckResult{ expected: checker.CheckResult{
Score: 0, Score: 0,
}, },
@ -148,7 +148,7 @@ func TestContributors(t *testing.T) {
{ {
err: errors.New("error"), err: errors.New("error"),
name: "Error getting contributors", name: "Error getting contributors",
contrib: []clients.Contributor{}, contrib: []clients.User{},
expected: checker.CheckResult{ expected: checker.CheckResult{
Score: -1, Score: -1,
}, },
@ -161,7 +161,7 @@ func TestContributors(t *testing.T) {
t.Parallel() t.Parallel()
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
mockRepo := mockrepo.NewMockRepoClient(ctrl) mockRepo := mockrepo.NewMockRepoClient(ctrl)
mockRepo.EXPECT().ListContributors().DoAndReturn(func() ([]clients.Contributor, error) { mockRepo.EXPECT().ListContributors().DoAndReturn(func() ([]clients.User, error) {
if tt.err != nil { if tt.err != nil {
return nil, tt.err return nil, tt.err
} }

View File

@ -24,7 +24,7 @@ import (
// Contributors retrieves the raw data for the Contributors check. // Contributors retrieves the raw data for the Contributors check.
func Contributors(c clients.RepoClient) (checker.ContributorsData, error) { func Contributors(c clients.RepoClient) (checker.ContributorsData, error) {
var users []clients.Contributor var users []clients.User
contribs, err := c.ListContributors() contribs, err := c.ListContributors()
if err != nil { if err != nil {
@ -32,8 +32,8 @@ func Contributors(c clients.RepoClient) (checker.ContributorsData, error) {
} }
for _, contrib := range contribs { for _, contrib := range contribs {
user := clients.Contributor{ user := clients.User{
User: contrib.User, Login: contrib.Login,
NumContributions: contrib.NumContributions, NumContributions: contrib.NumContributions,
} }

View File

@ -21,6 +21,6 @@ type Commit struct {
CommittedDate time.Time CommittedDate time.Time
Message string Message string
SHA string SHA string
Committer User
AssociatedMergeRequest PullRequest AssociatedMergeRequest PullRequest
Committer User
} }

View File

@ -1,23 +0,0 @@
// Copyright 2021 Security Scorecard Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package clients
// Contributor represents a contributor to a repo.
type Contributor struct {
Companies []string
User User
Organizations []User
NumContributions int
}

View File

@ -138,7 +138,7 @@ func (client *Client) ListReleases() ([]clients.Release, error) {
} }
// ListContributors implements RepoClient.ListContributors. // ListContributors implements RepoClient.ListContributors.
func (client *Client) ListContributors() ([]clients.Contributor, error) { func (client *Client) ListContributors() ([]clients.User, error) {
return client.contributors.getContributors() return client.contributors.getContributors()
} }

View File

@ -31,7 +31,7 @@ type contributorsHandler struct {
ctx context.Context ctx context.Context
errSetup error errSetup error
repourl *repoURL repourl *repoURL
contributors []clients.Contributor contributors []clients.User
} }
func (handler *contributorsHandler) init(ctx context.Context, repourl *repoURL) { func (handler *contributorsHandler) init(ctx context.Context, repourl *repoURL) {
@ -58,11 +58,9 @@ func (handler *contributorsHandler) setup() error {
if contrib.GetLogin() == "" { if contrib.GetLogin() == "" {
continue continue
} }
contributor := clients.Contributor{ contributor := clients.User{
NumContributions: contrib.GetContributions(), NumContributions: contrib.GetContributions(),
User: clients.User{ Login: contrib.GetLogin(),
Login: contrib.GetLogin(),
},
} }
orgs, _, err := handler.ghClient.Organizations.List(handler.ctx, contrib.GetLogin(), nil) orgs, _, err := handler.ghClient.Organizations.List(handler.ctx, contrib.GetLogin(), nil)
// This call can fail due to token scopes. So ignore error. // This call can fail due to token scopes. So ignore error.
@ -85,7 +83,7 @@ func (handler *contributorsHandler) setup() error {
return handler.errSetup return handler.errSetup
} }
func (handler *contributorsHandler) getContributors() ([]clients.Contributor, error) { func (handler *contributorsHandler) getContributors() ([]clients.User, error) {
if err := handler.setup(); err != nil { if err := handler.setup(); err != nil {
return nil, fmt.Errorf("error during contributorsHandler.setup: %w", err) return nil, fmt.Errorf("error during contributorsHandler.setup: %w", err)
} }

View File

@ -185,7 +185,7 @@ func (client *localDirClient) ListReleases() ([]clients.Release, error) {
} }
// ListContributors implements RepoClient.ListContributors. // ListContributors implements RepoClient.ListContributors.
func (client *localDirClient) ListContributors() ([]clients.Contributor, error) { func (client *localDirClient) ListContributors() ([]clients.User, error) {
return nil, fmt.Errorf("ListContributors: %w", clients.ErrUnsupportedFeature) return nil, fmt.Errorf("ListContributors: %w", clients.ErrUnsupportedFeature)
} }

View File

@ -168,10 +168,10 @@ func (mr *MockRepoClientMockRecorder) ListCommits() *gomock.Call {
} }
// ListContributors mocks base method. // ListContributors mocks base method.
func (m *MockRepoClient) ListContributors() ([]clients.Contributor, error) { func (m *MockRepoClient) ListContributors() ([]clients.User, error) {
m.ctrl.T.Helper() m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ListContributors") ret := m.ctrl.Call(m, "ListContributors")
ret0, _ := ret[0].([]clients.Contributor) ret0, _ := ret[0].([]clients.User)
ret1, _ := ret[1].(error) ret1, _ := ret[1].(error)
return ret0, ret1 return ret0, ret1
} }

View File

@ -37,7 +37,7 @@ type RepoClient interface {
ListCommits() ([]Commit, error) ListCommits() ([]Commit, error)
ListIssues() ([]Issue, error) ListIssues() ([]Issue, error)
ListReleases() ([]Release, error) ListReleases() ([]Release, error)
ListContributors() ([]Contributor, error) ListContributors() ([]User, error)
ListSuccessfulWorkflowRuns(filename string) ([]WorkflowRun, error) ListSuccessfulWorkflowRuns(filename string) ([]WorkflowRun, error)
ListCheckRunsForRef(ref string) ([]CheckRun, error) ListCheckRunsForRef(ref string) ([]CheckRun, error)
ListStatuses(ref string) ([]Status, error) ListStatuses(ref string) ([]Status, error)

View File

@ -16,7 +16,10 @@ package clients
// User represents a Git user. // User represents a Git user.
type User struct { type User struct {
Login string Login string
Companies []string
Organizations []User
NumContributions int
} }
// RepoAssociation is how a user is associated with a repository. // RepoAssociation is how a user is associated with a repository.

View File

@ -247,7 +247,7 @@ func (r *jsonScorecardRawResult) addContributorsRawResults(cr *checker.Contribut
for _, user := range cr.Users { for _, user := range cr.Users {
u := jsonUser{ u := jsonUser{
Login: user.User.Login, Login: user.Login,
NumContributions: user.NumContributions, NumContributions: user.NumContributions,
} }