Skip to content
Open
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
28 changes: 26 additions & 2 deletions pkg/license/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ func IsLicenseFile(filename string, overrideIgnore bool) (bool, float64) {
}

// CollectLicenseInfo collects license information from the given filesystem.
func CollectLicenseInfo(ctx context.Context, fsys fs.FS) ([]License, error) {
// If cfg is not nil, it will also include license paths specified in the configuration.
func CollectLicenseInfo(ctx context.Context, fsys fs.FS, cfg *config.Configuration) ([]License, error) {
log := clog.FromContext(ctx)

// Find all license-text files
Expand All @@ -232,6 +233,29 @@ func CollectLicenseInfo(ctx context.Context, fsys fs.FS) ([]License, error) {
return nil, fmt.Errorf("finding license files: %w", err)
}

// If configuration is provided, add any license-paths from the config
if cfg != nil {
// Build a set of existing paths to avoid duplicates
existingPaths := make(map[string]struct{})
for _, lf := range licenseFiles {
existingPaths[lf.Path] = struct{}{}
}

// Add license paths from the configuration
for _, cp := range cfg.Package.Copyright {
if cp.LicensePath != "" {
if _, exists := existingPaths[cp.LicensePath]; !exists {
licenseFiles = append(licenseFiles, LicenseFile{
Name: filepath.Base(cp.LicensePath),
Path: cp.LicensePath,
Weight: 1.0, // Neutral weight
})
existingPaths[cp.LicensePath] = struct{}{}
}
}
}
}

if len(licenseFiles) == 0 {
// No license files detected, no linting performed.
log.Debugf("no license files detected")
Expand Down Expand Up @@ -270,7 +294,7 @@ func LicenseCheck(ctx context.Context, cfg *config.Configuration, fsys fs.FS) ([
log := clog.FromContext(ctx)
log.Infof("checking license information")

detectedLicenses, err := CollectLicenseInfo(ctx, fsys)
detectedLicenses, err := CollectLicenseInfo(ctx, fsys, cfg)
if err != nil {
return nil, nil, fmt.Errorf("collecting license info: %w", err)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/license/license_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ func TestLicenseCheck(t *testing.T) {
Copyright: []config.Copyright{
{License: "Apache-2.0", LicensePath: "LICENSE-APACHE"},
{License: "MIT", LicensePath: "LICENSE-BSD"},
{License: "Apache-2.0", LicensePath: "ATYPICAL-FILENAME"},
{License: "GPL-2.0 OR GPL-3.0"},
},
},
Expand Down
Loading