Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Print Info in Empty Repo Scans #3426

Merged
merged 12 commits into from
Sep 18, 2023
40 changes: 24 additions & 16 deletions pkg/scorecard.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
"github.com/ossf/scorecard/v4/probes/zrunner"
)

// errEmptyRepository indicates the repository is empty.
var errEmptyRepository = errors.New("repository empty")

func runEnabledChecks(ctx context.Context,
repo clients.Repo, raw *checker.RawResults, checksToRun checker.CheckNameToFnMap,
repoClient clients.RepoClient, ossFuzzRepoClient clients.RepoClient, ciiClient clients.CIIBestPracticesClient,
Expand Down Expand Up @@ -80,10 +83,10 @@
return "", sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("ListCommits:%v", err.Error()))
}

if len(commits) > 0 {
return commits[0].SHA, nil
if len(commits) == 0 {
return "", errEmptyRepository
}
return "", nil
return commits[0].SHA, nil
}

// RunScorecard runs enabled Scorecard checks on a Repo.
Expand All @@ -104,19 +107,6 @@
}
defer repoClient.Close()

commitSHA, err := getRepoCommitHash(repoClient)
if err != nil || commitSHA == "" {
return ScorecardResult{}, err
}
defaultBranch, err := repoClient.GetDefaultBranchName()
if err != nil {
if !errors.Is(err, clients.ErrUnsupportedFeature) {
return ScorecardResult{},
sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("GetDefaultBranchName:%v", err.Error()))
}
defaultBranch = "unknown"
}

versionInfo := version.GetVersionInfo()
ret := ScorecardResult{
Repo: RepoInfo{
Expand All @@ -129,6 +119,24 @@
},
Date: time.Now(),
}

commitSHA, err := getRepoCommitHash(repoClient)

if errors.Is(err, errEmptyRepository) {
return ret, nil
spencerschrock marked this conversation as resolved.
Show resolved Hide resolved
} else if err != nil {
return ScorecardResult{}, err
}

Check warning on line 129 in pkg/scorecard.go

View check run for this annotation

Codecov / codecov/patch

pkg/scorecard.go#L128-L129

Added lines #L128 - L129 were not covered by tests

defaultBranch, err := repoClient.GetDefaultBranchName()
if err != nil {
if !errors.Is(err, clients.ErrUnsupportedFeature) {
return ScorecardResult{},
sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("GetDefaultBranchName:%v", err.Error()))
}
defaultBranch = "unknown"

Check warning on line 137 in pkg/scorecard.go

View check run for this annotation

Codecov / codecov/patch

pkg/scorecard.go#L133-L137

Added lines #L133 - L137 were not covered by tests
}

resultsCh := make(chan checker.CheckResult)

// Set metadata for all checks to use. This is necessary
Expand Down
25 changes: 19 additions & 6 deletions pkg/scorecard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ package pkg

import (
"context"
"reflect"
"testing"

"github.com/golang/mock/gomock"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"

"github.com/ossf/scorecard/v4/clients"
"github.com/ossf/scorecard/v4/clients/localdir"
Expand All @@ -41,7 +42,7 @@ func Test_getRepoCommitHash(t *testing.T) {
{
name: "empty commit",
want: "",
wantErr: false,
wantErr: true,
},
}
for _, tt := range tests {
Expand Down Expand Up @@ -121,6 +122,7 @@ func Test_getRepoCommitHashLocal(t *testing.T) {
func TestRunScorecard(t *testing.T) {
t.Parallel()
type args struct {
uri string
commitSHA string
}
tests := []struct {
Expand All @@ -130,11 +132,19 @@ func TestRunScorecard(t *testing.T) {
wantErr bool
}{
{
name: "empty commits repos should return empty result",
name: "empty commits repos should return repo details but no checks",
args: args{
uri: "github.com/ossf/scorecard",
commitSHA: "",
},
want: ScorecardResult{},
want: ScorecardResult{
Repo: RepoInfo{
Name: "github.com/ossf/scorecard",
},
Scorecard: ScorecardInfo{
CommitSHA: "unknown",
},
},
wantErr: false,
},
}
Expand All @@ -146,6 +156,8 @@ func TestRunScorecard(t *testing.T) {
mockRepoClient := mockrepo.NewMockRepoClient(ctrl)
repo := mockrepo.NewMockRepo(ctrl)

repo.EXPECT().URI().Return(tt.args.uri).AnyTimes()

mockRepoClient.EXPECT().InitRepo(repo, tt.args.commitSHA, 0).Return(nil)

mockRepoClient.EXPECT().Close().DoAndReturn(func() error {
Expand All @@ -168,8 +180,9 @@ func TestRunScorecard(t *testing.T) {
t.Errorf("RunScorecard() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("RunScorecard() got = %v, want %v", got, tt.want)
ignoreDate := cmpopts.IgnoreFields(ScorecardResult{}, "Date")
if !cmp.Equal(got, tt.want, ignoreDate) {
t.Errorf("expected %v, got %v", got, cmp.Diff(tt.want, got, ignoreDate))
}
})
}
Expand Down
Loading