Skip to content

Commit

Permalink
🐛 Print Info in Empty Repo Scans (ossf#3426)
Browse files Browse the repository at this point in the history
* issue 2157 changes

Signed-off-by: leec94 <leec94@bu.edu>

* incorporated feedback

Signed-off-by: leec94 <leec94@bu.edu>

* making the linter happy

Signed-off-by: leec94 <leec94@bu.edu>

* changing to local variable, testing still not working

Signed-off-by: leec94 <leec94@bu.edu>

* update tests to ignore date

Signed-off-by: leec94 <leec94@bu.edu>

* ran through linter

Signed-off-by: leec94 <leec94@bu.edu>

* resolving suggestions

Signed-off-by: leec94 <leec94@bu.edu>

---------

Signed-off-by: leec94 <leec94@bu.edu>
Signed-off-by: Allen Shearin <allen.p.shearin@gmail.com>
  • Loading branch information
leec94 authored and ashearin committed Nov 13, 2023
1 parent 5577041 commit db5a4b2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 22 deletions.
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 @@ import (
"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 @@ func getRepoCommitHash(r clients.RepoClient) (string, error) {
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 @@ func RunScorecard(ctx context.Context,
}
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 @@ func RunScorecard(ctx context.Context,
},
Date: time.Now(),
}

commitSHA, err := getRepoCommitHash(repoClient)

if errors.Is(err, errEmptyRepository) {
return ret, nil
} else if err != nil {
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"
}

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

0 comments on commit db5a4b2

Please sign in to comment.