feat: update licenses to including license content when SPDX expressions are unable to be determined (#3366)

---------
Signed-off-by: HeyeOpenSource <opensource@heye-international.com>
Signed-off-by: Christopher Phillips <32073428+spiffcs@users.noreply.github.com>
Co-authored-by: Christopher Phillips <32073428+spiffcs@users.noreply.github.com>
This commit is contained in:
HeyeOpenSource
2025-01-28 18:05:27 +00:00
committed by GitHub
co-authored by Christopher Phillips
parent 58dc43de86
commit f7e767fc25
21 changed files with 3291 additions and 43 deletions
+11 -12
View File
@@ -12,7 +12,7 @@ import (
const coverageThreshold = 75 // determined by experimentation
type Scanner interface {
IdentifyLicenseIDs(context.Context, io.Reader) ([]string, error)
IdentifyLicenseIDs(context.Context, io.Reader) ([]string, []byte, error)
}
var _ Scanner = (*scanner)(nil)
@@ -35,34 +35,33 @@ func NewDefaultScanner() Scanner {
}
}
// TestingOnlyScanner returns a scanner that uses the built-in license scanner from the licensecheck package.
// THIS IS ONLY MEANT FOR TEST CODE, NOT PRODUCTION CODE.
func TestingOnlyScanner() Scanner {
return &scanner{
coverageThreshold: coverageThreshold,
scanner: licensecheck.Scan,
func NewScanner(scan func([]byte) licensecheck.Coverage, coverage float64) Scanner {
return scanner{
coverageThreshold: coverage,
scanner: scan,
}
}
func (s scanner) IdentifyLicenseIDs(_ context.Context, reader io.Reader) ([]string, error) {
func (s scanner) IdentifyLicenseIDs(_ context.Context, reader io.Reader) ([]string, []byte, error) {
if s.scanner == nil {
return nil, nil
return nil, nil, nil
}
content, err := io.ReadAll(reader)
if err != nil {
return nil, err
return nil, nil, err
}
cov := s.scanner(content)
if cov.Percent < s.coverageThreshold {
// unknown or no licenses here?
return nil, nil
// => return binary content
return nil, content, nil
}
var ids []string
for _, m := range cov.Match {
ids = append(ids, m.ID)
}
return ids, nil
return ids, nil, nil
}