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

✨ enforce check scores are between the min and max #3769

Merged
merged 3 commits into from
Jan 19, 2024
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
21 changes: 11 additions & 10 deletions checker/check_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"math"

sce "github.com/ossf/scorecard/v4/errors"
"github.com/ossf/scorecard/v4/finding"
"github.com/ossf/scorecard/v4/rule"
)
Expand Down Expand Up @@ -175,8 +176,15 @@ func NormalizeReason(reason string, score int) string {

// CreateResultWithScore is used when
// the check runs without runtime errors, and we want to assign a
// specific score.
// specific score. The score must be between [MinResultScore] and [MaxResultScore].
// Callers who want [InconclusiveResultScore] must use [CreateInconclusiveResult] instead.
//
// Passing an invalid score results in a runtime error result as if created by [CreateRuntimeErrorResult].
func CreateResultWithScore(name, reason string, score int) CheckResult {
if score < MinResultScore || score > MaxResultScore {
err := sce.CreateInternal(sce.ErrScorecardInternal, fmt.Sprintf("invalid score (%d), please report this", score))
return CreateRuntimeErrorResult(name, err)
}
return CheckResult{
Name: name,
Version: 2,
Expand All @@ -193,15 +201,8 @@ func CreateResultWithScore(name, reason string, score int) CheckResult {
// the number of tests that succeeded.
func CreateProportionalScoreResult(name, reason string, b, t int) CheckResult {
score := CreateProportionalScore(b, t)
return CheckResult{
Name: name,
// Old structure.
// New structure.
Version: 2,
Error: nil,
Score: score,
Reason: NormalizeReason(reason, score),
}
reason = NormalizeReason(reason, score)
return CreateResultWithScore(name, reason, score)
}

// CreateMaxScoreResult is used when
Expand Down
70 changes: 68 additions & 2 deletions checker/check_result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"

sce "github.com/ossf/scorecard/v4/errors"
)

func TestAggregateScores(t *testing.T) {
Expand Down Expand Up @@ -493,12 +496,58 @@ func TestCreateResultWithScore(t *testing.T) {
Score: 1,
},
},
{
name: "inconclusive score is not valid",
args: args{
name: "name",
reason: "reason",
score: InconclusiveResultScore,
},
want: CheckResult{
Name: "name",
Reason: "internal error: invalid score (-1), please report this",
Version: 2,
Score: -1,
Error: sce.ErrScorecardInternal,
},
},
{
name: "score too low",
args: args{
name: "name",
reason: "reason",
score: -3,
},
want: CheckResult{
Name: "name",
Reason: "internal error: invalid score (-3), please report this",
Version: 2,
Score: -1,
Error: sce.ErrScorecardInternal,
},
},
{
name: "score too high",
args: args{
name: "name",
reason: "reason",
score: MaxResultScore + 2,
},
want: CheckResult{
Name: "name",
Reason: "internal error: invalid score (12), please report this",
Version: 2,
Score: -1,
Error: sce.ErrScorecardInternal,
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := CreateResultWithScore(tt.args.name, tt.args.reason, tt.args.score); !cmp.Equal(got, tt.want) {
got := CreateResultWithScore(tt.args.name, tt.args.reason, tt.args.score)
if !cmp.Equal(got, tt.want, cmpopts.EquateErrors()) {
t.Errorf("CreateResultWithScore() = %v, want %v", got, cmp.Diff(got, tt.want))
}
})
Expand Down Expand Up @@ -548,12 +597,29 @@ func TestCreateProportionalScoreResult(t *testing.T) {
Version: 2,
},
},
{
name: "negative proportion, score too low",
args: args{
name: "name",
reason: "reason",
b: -2,
t: 1,
},
want: CheckResult{
Name: "name",
Reason: "internal error: invalid score (-20), please report this",
Version: 2,
Score: -1,
Error: sce.ErrScorecardInternal,
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := CreateProportionalScoreResult(tt.args.name, tt.args.reason, tt.args.b, tt.args.t); !cmp.Equal(got, tt.want) {
got := CreateProportionalScoreResult(tt.args.name, tt.args.reason, tt.args.b, tt.args.t)
if !cmp.Equal(got, tt.want, cmpopts.EquateErrors()) {
t.Errorf("CreateProportionalScoreResult() = %v, want %v", got, cmp.Diff(got, tt.want))
}
})
Expand Down
Loading