forked from siderolabs/conform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add license header policy (siderolabs#105)
Signed-off-by: Andrew Rynhard <andrew@andrewrynhard.com>
- Loading branch information
1 parent
763d4d9
commit f5ed717
Showing
17 changed files
with
152 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
internal/policy/conventionalcommit/conventionalcommit_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package license | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/autonomy/conform/internal/policy" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// License implements the policy.Policy interface and enforces source code | ||
// license headers. | ||
type License struct { | ||
// IncludeSuffixes is the regex used to find files that the license policy | ||
// should be applied to. | ||
IncludeSuffixes []string `mapstructure:"includeSuffixes"` | ||
// ExcludeSuffixes is the Suffixes used to find files that the license policy | ||
// should not be applied to. | ||
ExcludeSuffixes []string `mapstructure:"excludeSuffixes"` | ||
// LicenseHeaderFile is the path to the license header file. | ||
LicenseHeaderFile string `mapstructure:"headerFile"` | ||
} | ||
|
||
// Compliance implements the policy.Policy.Compliance function. | ||
func (l *License) Compliance(options *policy.Options) (report policy.Report) { | ||
var err error | ||
|
||
report = policy.Report{} | ||
|
||
var value []byte | ||
if value, err = ioutil.ReadFile(l.LicenseHeaderFile); err != nil { | ||
report.Errors = append(report.Errors, errors.Errorf("Failed to open %s", l.LicenseHeaderFile)) | ||
} | ||
|
||
err = filepath.Walk(".", func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
if info.Mode().IsRegular() { | ||
// Skip excluded suffixes. | ||
for _, suffix := range l.ExcludeSuffixes { | ||
if strings.HasSuffix(info.Name(), suffix) { | ||
continue | ||
} | ||
} | ||
// Check files matching the included suffixes. | ||
for _, suffix := range l.IncludeSuffixes { | ||
if strings.HasSuffix(info.Name(), suffix) { | ||
var contents []byte | ||
if contents, err = ioutil.ReadFile(path); err != nil { | ||
report.Errors = append(report.Errors, errors.Errorf("Failed to open %s", path)) | ||
return nil | ||
} | ||
ValidateLicenseHeader(&report, info.Name(), contents, value) | ||
} | ||
} | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
report.Errors = append(report.Errors, errors.Errorf("Failed to walk directory: %v", err)) | ||
} | ||
|
||
return report | ||
} | ||
|
||
// ValidateLicenseHeader checks the header of a file and ensures it contains the | ||
// provided value. | ||
func ValidateLicenseHeader(report *policy.Report, name string, contents, value []byte) { | ||
if bytes.HasPrefix(contents, value) { | ||
return | ||
} | ||
report.Errors = append(report.Errors, errors.Errorf("File %s does not contain a license header", name)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters