From 10b26fc600f16f41fdf07c5e8f2ba38e801ea188 Mon Sep 17 00:00:00 2001 From: Azeem Shaikh Date: Wed, 25 May 2022 01:04:29 +0000 Subject: [PATCH] Replace `clients.Contributor` with `clients.User` --- checker/raw_result.go | 2 +- checks/contributors_test.go | 12 ++++++------ checks/raw/contributors.go | 6 +++--- clients/contributor.go | 23 ----------------------- clients/githubrepo/client.go | 2 +- clients/githubrepo/contributors.go | 10 ++++------ clients/localdir/client.go | 2 +- clients/mockclients/repo_client.go | 4 ++-- clients/repo_client.go | 2 +- clients/user.go | 5 ++++- 10 files changed, 23 insertions(+), 45 deletions(-) delete mode 100644 clients/contributor.go diff --git a/checker/raw_result.go b/checker/raw_result.go index cb50e86cff79..c0c9eeeddc43 100644 --- a/checker/raw_result.go +++ b/checker/raw_result.go @@ -65,7 +65,7 @@ type CodeReviewData struct { // ContributorsData represents contributor information. type ContributorsData struct { - Users []clients.Contributor + Users []clients.User } // VulnerabilitiesData contains the raw results diff --git a/checks/contributors_test.go b/checks/contributors_test.go index c0cb205692b6..df81343db9b0 100644 --- a/checks/contributors_test.go +++ b/checks/contributors_test.go @@ -34,13 +34,13 @@ func TestContributors(t *testing.T) { tests := []struct { err error name string - contrib []clients.Contributor + contrib []clients.User expected checker.CheckResult }{ { err: nil, name: "Two contributors without company", - contrib: []clients.Contributor{ + contrib: []clients.User{ { Organizations: []clients.User{ { @@ -59,7 +59,7 @@ func TestContributors(t *testing.T) { { err: nil, name: "Valid contributors with enough contributors and companies", - contrib: []clients.Contributor{ + contrib: []clients.User{ { Companies: []string{"company1"}, @@ -140,7 +140,7 @@ func TestContributors(t *testing.T) { { err: nil, name: "No contributors", - contrib: []clients.Contributor{}, + contrib: []clients.User{}, expected: checker.CheckResult{ Score: 0, }, @@ -148,7 +148,7 @@ func TestContributors(t *testing.T) { { err: errors.New("error"), name: "Error getting contributors", - contrib: []clients.Contributor{}, + contrib: []clients.User{}, expected: checker.CheckResult{ Score: -1, }, @@ -161,7 +161,7 @@ func TestContributors(t *testing.T) { t.Parallel() ctrl := gomock.NewController(t) mockRepo := mockrepo.NewMockRepoClient(ctrl) - mockRepo.EXPECT().ListContributors().DoAndReturn(func() ([]clients.Contributor, error) { + mockRepo.EXPECT().ListContributors().DoAndReturn(func() ([]clients.User, error) { if tt.err != nil { return nil, tt.err } diff --git a/checks/raw/contributors.go b/checks/raw/contributors.go index b02e2b6443a7..9f149abad5f6 100644 --- a/checks/raw/contributors.go +++ b/checks/raw/contributors.go @@ -24,7 +24,7 @@ import ( // Contributors retrieves the raw data for the Contributors check. func Contributors(c clients.RepoClient) (checker.ContributorsData, error) { - var users []clients.Contributor + var users []clients.User contribs, err := c.ListContributors() if err != nil { @@ -32,8 +32,8 @@ func Contributors(c clients.RepoClient) (checker.ContributorsData, error) { } for _, contrib := range contribs { - user := clients.Contributor{ - User: contrib.User, + user := clients.User{ + Login: contrib.Login, NumContributions: contrib.NumContributions, } diff --git a/clients/contributor.go b/clients/contributor.go deleted file mode 100644 index 20aab69f878e..000000000000 --- a/clients/contributor.go +++ /dev/null @@ -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 -} diff --git a/clients/githubrepo/client.go b/clients/githubrepo/client.go index c380017e013a..4c736615453b 100644 --- a/clients/githubrepo/client.go +++ b/clients/githubrepo/client.go @@ -135,7 +135,7 @@ func (client *Client) ListReleases() ([]clients.Release, error) { } // ListContributors implements RepoClient.ListContributors. -func (client *Client) ListContributors() ([]clients.Contributor, error) { +func (client *Client) ListContributors() ([]clients.User, error) { return client.contributors.getContributors() } diff --git a/clients/githubrepo/contributors.go b/clients/githubrepo/contributors.go index 25d83d9a6864..7b25adce8c57 100644 --- a/clients/githubrepo/contributors.go +++ b/clients/githubrepo/contributors.go @@ -31,7 +31,7 @@ type contributorsHandler struct { ctx context.Context errSetup error repourl *repoURL - contributors []clients.Contributor + contributors []clients.User } func (handler *contributorsHandler) init(ctx context.Context, repourl *repoURL) { @@ -58,11 +58,9 @@ func (handler *contributorsHandler) setup() error { if contrib.GetLogin() == "" { continue } - contributor := clients.Contributor{ + contributor := clients.User{ NumContributions: contrib.GetContributions(), - User: clients.User{ - Login: contrib.GetLogin(), - }, + Login: contrib.GetLogin(), } orgs, _, err := handler.ghClient.Organizations.List(handler.ctx, contrib.GetLogin(), nil) // This call can fail due to token scopes. So ignore error. @@ -85,7 +83,7 @@ func (handler *contributorsHandler) setup() error { return handler.errSetup } -func (handler *contributorsHandler) getContributors() ([]clients.Contributor, error) { +func (handler *contributorsHandler) getContributors() ([]clients.User, error) { if err := handler.setup(); err != nil { return nil, fmt.Errorf("error during contributorsHandler.setup: %w", err) } diff --git a/clients/localdir/client.go b/clients/localdir/client.go index e34a9f8dd096..77dc6b5fb1f2 100644 --- a/clients/localdir/client.go +++ b/clients/localdir/client.go @@ -182,7 +182,7 @@ func (client *localDirClient) ListReleases() ([]clients.Release, error) { } // 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) } diff --git a/clients/mockclients/repo_client.go b/clients/mockclients/repo_client.go index 1691c2394db4..0bb920d578e3 100644 --- a/clients/mockclients/repo_client.go +++ b/clients/mockclients/repo_client.go @@ -168,10 +168,10 @@ func (mr *MockRepoClientMockRecorder) ListCommits() *gomock.Call { } // ListContributors mocks base method. -func (m *MockRepoClient) ListContributors() ([]clients.Contributor, error) { +func (m *MockRepoClient) ListContributors() ([]clients.User, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ListContributors") - ret0, _ := ret[0].([]clients.Contributor) + ret0, _ := ret[0].([]clients.User) ret1, _ := ret[1].(error) return ret0, ret1 } diff --git a/clients/repo_client.go b/clients/repo_client.go index 0e8e2ff7b137..884c5162bf78 100644 --- a/clients/repo_client.go +++ b/clients/repo_client.go @@ -37,7 +37,7 @@ type RepoClient interface { ListCommits() ([]Commit, error) ListIssues() ([]Issue, error) ListReleases() ([]Release, error) - ListContributors() ([]Contributor, error) + ListContributors() ([]User, error) ListSuccessfulWorkflowRuns(filename string) ([]WorkflowRun, error) ListCheckRunsForRef(ref string) ([]CheckRun, error) ListStatuses(ref string) ([]Status, error) diff --git a/clients/user.go b/clients/user.go index a7508d7cd140..e342605aeebe 100644 --- a/clients/user.go +++ b/clients/user.go @@ -16,7 +16,10 @@ package clients // User represents a Git user. type User struct { - Login string + Login string + Companies []string + Organizations []User + NumContributions int } // RepoAssociation is how a user is associated with a repository.