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

Adding a license confidence override threshold #145

Merged
merged 2 commits into from
Sep 4, 2024
Merged
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
24 changes: 24 additions & 0 deletions backend/internal/license/license_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ func WithConfidenceThreshold(threshold float32) Opt {
}
}

func WithConfidenceOverrideThreshold(threshold float32) Opt {
return func(config *Config) error {
if threshold < 0 || threshold > 1 {
return fmt.Errorf("invalid threshold: %f", threshold)
}
config.ConfidenceOverrideThreshold = threshold
return nil
}
}

func WithCompatibleLicenses(licenses ...string) Opt {
return func(config *Config) error {
config.CompatibleLicenses = append(config.CompatibleLicenses, licenses...)
Expand All @@ -129,19 +139,28 @@ func WithCompatibleLicenses(licenses ...string) Opt {
type Config struct {
CompatibleLicenses []string
ConfidenceThreshold float32
// ConfidenceOverrideThreshold is the limit at which a detected license overrides all other detected licenses.
// Defaults to 98%.
ConfidenceOverrideThreshold float32
}

func (c *Config) ApplyDefaults() error {
if c.ConfidenceThreshold == 0.0 {
c.ConfidenceThreshold = 0.9
}
if c.ConfidenceOverrideThreshold == 0 {
c.ConfidenceOverrideThreshold = 0.98
}
return nil
}

func (c *Config) Validate() error {
if len(c.CompatibleLicenses) == 0 {
return fmt.Errorf("no licenses configured")
}
if c.ConfidenceOverrideThreshold < c.ConfidenceThreshold {
return fmt.Errorf("the confidence override threshold (%f) is lower than the confidence threshold (%f)", c.ConfidenceOverrideThreshold, c.ConfidenceThreshold)
}
return nil
}

Expand Down Expand Up @@ -230,6 +249,11 @@ func (d detector) Detect(_ context.Context, repository fs.ReadDirFS, detectOptio
if err := detectCfg.LinkFetcher(&l); err != nil {
return nil, err
}
if match.Confidence >= d.config.ConfidenceOverrideThreshold {
return []License{
l,
}, nil
}
result = append(result, l)
}
}
Expand Down
Loading