Skip to content

Commit

Permalink
feat: update policy
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Phillips <[email protected]>
  • Loading branch information
spiffcs committed Oct 3, 2024
1 parent 72c76ed commit 85ff284
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
10 changes: 5 additions & 5 deletions grant/evalutation/license_evaluation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ func Test_checkLicense(t *testing.T) {
}
}{
{
name: "should reject denied licenses",
license: grant.License{Name: "MIT"},
name: "should reject denied licenses when SPDX expressions and CheckNON SPDX is False",
license: grant.License{ID: "MIT", SPDXExpression: "MIT", LicenseID: "MIT"},
// Only allow OSI licenses.
config: EvaluationConfig{CheckNonSPDX: true, Policy: grant.DefaultPolicy()},
config: EvaluationConfig{CheckNonSPDX: false, Policy: grant.DefaultPolicy().SetMatchNonSPDX(false)},
wants: struct {
Pass bool
Reasons []Reason
Expand All @@ -73,10 +73,10 @@ func Test_checkLicense(t *testing.T) {
},
},
{
name: "should reject denied licenses when CheckNonSPDX is also false",
name: "should reject denied licenses when CheckNonSPDX is also true",
license: grant.License{Name: "foobar"},
// Only allow OSI licenses.
config: EvaluationConfig{CheckNonSPDX: false, Policy: grant.DefaultPolicy()},
config: EvaluationConfig{CheckNonSPDX: true, Policy: grant.DefaultPolicy().SetMatchNonSPDX(true)},
wants: struct {
Pass bool
Reasons []Reason
Expand Down
13 changes: 12 additions & 1 deletion grant/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ func (p Policy) IsEmpty() bool {
// IsDenied returns true if the given license is denied by the policy
func (p Policy) IsDenied(license License, pkg *Package) (bool, *Rule) {
for _, rule := range p.Rules {
// ignore non spdx licenses if the rule is configured to not match on non spdx
isSPDX := license.IsSPDX()
matchNonSPDX := p.MatchNonSPDX
if !matchNonSPDX && !isSPDX {
continue
}
var toMatch string

if license.IsSPDX() {
toMatch = strings.ToLower(license.LicenseID)
} else {
Expand Down Expand Up @@ -82,3 +87,9 @@ func (p Policy) IsDenied(license License, pkg *Package) (bool, *Rule) {
}
return false, nil
}

// SetMatchNonSPDX updates the match option for the given policy
func (p Policy) SetMatchNonSPDX(matchNonSPDX bool) Policy {
p.MatchNonSPDX = matchNonSPDX
return p
}

0 comments on commit 85ff284

Please sign in to comment.