Skip to content

Commit

Permalink
Dependencies check should report unknown licneses (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
kezhenxu94 authored Apr 9, 2023
1 parent 5b7ee17 commit 43f8bb8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 29 deletions.
1 change: 1 addition & 0 deletions assets/compatibility/Apache-2.0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ compatible:
- Unlicense.txt
- HPND.txt
- MulanPSL-2.0.txt
- MIT

incompatible:
- Unknown
Expand Down
74 changes: 47 additions & 27 deletions pkg/deps/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package deps

import (
"fmt"
"math"
"path/filepath"
"strings"

Expand Down Expand Up @@ -73,34 +74,35 @@ func Check(mainLicenseSpdxID string, config *ConfigDeps) error {
return CheckWithMatrix(mainLicenseSpdxID, &matrix, &report)
}

func CheckWithMatrix(mainLicenseSpdxID string, matrix *CompatibilityMatrix, report *Report) error {
var incompatibleResults []*Result
for _, result := range append(report.Resolved, report.Skipped...) {
compare := func(list []string, spdxID string) bool {
for _, com := range list {
if spdxID == com {
return true
}
}
return false
}
compareAll := func(spdxIDs []string, compare func(spdxID string) bool) bool {
for _, spdxID := range spdxIDs {
if !compare(spdxID) {
return false
}
}
func compare(list []string, spdxID string) bool {
for _, com := range list {
if spdxID == com {
return true
}
compareAny := func(spdxIDs []string, compare func(spdxID string) bool) bool {
for _, spdxID := range spdxIDs {
if compare(spdxID) {
return true
}
}
}
return false
}
func compareAll(spdxIDs []string, compare func(spdxID string) bool) bool {
for _, spdxID := range spdxIDs {
if !compare(spdxID) {
return false
}
}
return true
}
func compareAny(spdxIDs []string, compare func(spdxID string) bool) bool {
for _, spdxID := range spdxIDs {
if compare(spdxID) {
return true
}
}
return false
}

func CheckWithMatrix(mainLicenseSpdxID string, matrix *CompatibilityMatrix, report *Report) error {
var incompatibleResults []*Result
var unknownResults []*Result
for _, result := range append(report.Resolved, report.Skipped...) {
operator, spdxIDs := parseLicenseExpression(result.LicenseSpdxID)

switch operator {
Expand Down Expand Up @@ -134,16 +136,34 @@ func CheckWithMatrix(mainLicenseSpdxID string, matrix *CompatibilityMatrix, repo
}
if incompatible := compare(matrix.Incompatible, spdxIDs[0]); incompatible {
incompatibleResults = append(incompatibleResults, result)
continue
}
unknownResults = append(unknownResults, result)
}
}

if len(incompatibleResults) > 0 {
str := ""
if len(incompatibleResults) > 0 || len(unknownResults) > 0 {
dWidth, lWidth := float64(len("Dependency")), float64(len("License"))
for _, r := range incompatibleResults {
str += fmt.Sprintf("\nLicense: %v Dependency: %v", r.LicenseSpdxID, r.Dependency)
dWidth = math.Max(float64(len(r.Dependency)), dWidth)
lWidth = math.Max(float64(len(r.LicenseSpdxID)), lWidth)
}
return fmt.Errorf("the following licenses are incompatible with the main license: %v %v", mainLicenseSpdxID, str)
for _, r := range unknownResults {
dWidth = math.Max(float64(len(r.Dependency)), dWidth)
lWidth = math.Max(float64(len(r.LicenseSpdxID)), lWidth)
}

rowTemplate := fmt.Sprintf("%%-%dv | %%%dv\n", int(dWidth), int(lWidth))
s := fmt.Sprintf(rowTemplate, "Dependency", "License")
s += fmt.Sprintf(rowTemplate, strings.Repeat("-", int(dWidth)), strings.Repeat("-", int(lWidth)))
for _, r := range incompatibleResults {
s += fmt.Sprintf(rowTemplate, r.Dependency, r.LicenseSpdxID)
}
for _, r := range unknownResults {
s += fmt.Sprintf(rowTemplate, r.Dependency, r.LicenseSpdxID)
}

return fmt.Errorf("the following licenses are unknown or incompatible with the main license, please check manually: %v\n%v", mainLicenseSpdxID, s)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/deps/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestCheckWithMatrix(t *testing.T) {
},
}); err == nil {
t.Errorf("Should return error")
} else if !strings.Contains(err.Error(), "License: LGPL-2.0 Dependency: Bar") {
} else if !strings.Contains(err.Error(), "Bar | LGPL-2.0") {
t.Errorf("Should return error and contains dependency Bar, now is `%s`", err.Error())
}

Expand All @@ -98,7 +98,7 @@ func TestCheckWithMatrix(t *testing.T) {
},
}); err == nil {
t.Errorf("Should return error")
} else if !strings.Contains(err.Error(), "License: Unknown Dependency: Bar") {
} else if !strings.Contains(err.Error(), "Bar | Unknown") {
t.Errorf("Should return error and has dependency Bar, now is `%s`", err.Error())
}

Expand Down

0 comments on commit 43f8bb8

Please # to comment.