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

Expose license-location-threshold as config item #34

Merged
merged 1 commit into from
May 29, 2021
Merged
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
4 changes: 4 additions & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ header: # `header` section is configurations for source codes license header.

comment: on-failure # on what condition license-eye will comment on the pull request, `on-failure`, `always`, `never`.

# license-location-threshold specifies the index threshold where the license header can be located,
# after all, a "header" cannot be TOO far from the file start.
license-location-threshold: 80

dependency:
files:
- go.mod
14 changes: 4 additions & 10 deletions pkg/header/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ import (
"github.com/bmatcuk/doublestar/v2"
)

var (
// LicenseLocationThreshold specifies the index threshold where the license header can be located,
// after all, a "header" cannot be TOO far from the file start.
LicenseLocationThreshold = 80
)

// Check checks the license headers of the specified paths/globs.
func Check(config *ConfigHeader, result *Result) error {
for _, pattern := range config.Paths {
Expand Down Expand Up @@ -129,7 +123,7 @@ func CheckFile(file string, config *ConfigHeader, result *Result) error {
content := lcs.NormalizeHeader(string(bs))
expected, pattern := config.NormalizedLicense(), config.NormalizedPattern()

if satisfy(content, expected, pattern) {
if satisfy(content, config, expected, pattern) {
result.Succeed(file)
} else {
logger.Log.Debugln("Content is:", content)
Expand All @@ -140,15 +134,15 @@ func CheckFile(file string, config *ConfigHeader, result *Result) error {
return nil
}

func satisfy(content, license string, pattern *regexp.Regexp) bool {
func satisfy(content string, config *ConfigHeader, license string, pattern *regexp.Regexp) bool {
if index := strings.Index(content, license); strings.TrimSpace(license) != "" && index >= 0 {
return index < LicenseLocationThreshold
return index < config.LicenseLocationThreshold
}

if pattern == nil {
return false
}
index := pattern.FindStringIndex(content)

return len(index) == 2 && index[0] < LicenseLocationThreshold
return len(index) == 2 && index[0] < config.LicenseLocationThreshold
}
8 changes: 8 additions & 0 deletions pkg/header/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ type ConfigHeader struct {
Paths []string `yaml:"paths"`
PathsIgnore []string `yaml:"paths-ignore"`
Comment CommentOption `yaml:"comment"`

// LicenseLocationThreshold specifies the index threshold where the license header can be located,
// after all, a "header" cannot be TOO far from the file start.
LicenseLocationThreshold int `yaml:"license-location-threshold"`
}

// NormalizedLicense returns the normalized string of the license content,
Expand Down Expand Up @@ -120,6 +124,10 @@ func (config *ConfigHeader) Finalize() error {
logger.Log.Debugln("Pattern is:", p)
}

if config.LicenseLocationThreshold <= 0 {
config.LicenseLocationThreshold = 80
}

return nil
}

Expand Down