Skip to content

Commit

Permalink
feat: add license header policy
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Rynhard <[email protected]>
  • Loading branch information
andrewrynhard committed Jan 13, 2019
1 parent 763d4d9 commit dea5c79
Show file tree
Hide file tree
Showing 17 changed files with 152 additions and 49 deletions.
5 changes: 5 additions & 0 deletions .conform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ policies:
scopes:
- policy
- '*'
- type: license
spec:
includeSuffixes:
- .go
headerFile: ./hack/LICENSE_HEADER.txt
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Some of the policies included are:
- Developer Certificate of Origin
- GPG signature
- **Conventional Commits**: Enforce [conventional commits](https://www.conventionalcommits.org) for all commit messages.
- **License Headers**: Enforce license headers on source code files.

## Getting Started

Expand All @@ -41,12 +42,23 @@ policies:
- "type"
scopes:
- "scope"
- type: license
spec:
includeSuffixes:
- .ext
excludeSuffixes:
- .exclude-ext-prefix.ext
headerFile: ./hack/LICENSE_HEADER.txt
```

In the same directory, run:

```bash
conform enforce
$ conform enforce
POLICY STATUS MESSAGE
commit PASS <none>
conventionalCommit PASS <none>
license PASS <none>
```

### License
Expand Down
15 changes: 3 additions & 12 deletions cmd/enforce.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
// Copyright © 2017 NAME HERE <EMAIL ADDRESS>
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/* 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 cmd

Expand Down
15 changes: 3 additions & 12 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
// Copyright © 2017 NAME HERE <EMAIL ADDRESS>
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/* 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 cmd

Expand Down
15 changes: 3 additions & 12 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
// Copyright © 2017 NAME HERE <EMAIL ADDRESS>
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/* 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 cmd

Expand Down
3 changes: 3 additions & 0 deletions hack/LICENSE_HEADER.txt
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/. */
4 changes: 4 additions & 0 deletions internal/constants/constants.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* 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 constants

const (
Expand Down
6 changes: 6 additions & 0 deletions internal/enforcer/enforcer.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* 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 enforcer

import (
Expand All @@ -9,6 +13,7 @@ import (
"github.com/autonomy/conform/internal/policy"
"github.com/autonomy/conform/internal/policy/commit"
"github.com/autonomy/conform/internal/policy/conventionalcommit"
"github.com/autonomy/conform/internal/policy/license"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"

Expand All @@ -31,6 +36,7 @@ type PolicyDeclaration struct {
var policyMap = map[string]policy.Policy{
"commit": &commit.Commit{},
"conventionalCommit": &conventionalcommit.Conventional{},
"license": &license.License{},
// "version": &version.Version{},
}

Expand Down
4 changes: 4 additions & 0 deletions internal/git/git.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* 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 git

import (
Expand Down
4 changes: 4 additions & 0 deletions internal/policy/commit/commit.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* 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 commit

import (
Expand Down
4 changes: 4 additions & 0 deletions internal/policy/conventionalcommit/conventionalcommit.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* 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 conventionalcommit

import (
Expand Down
4 changes: 4 additions & 0 deletions internal/policy/conventionalcommit/conventionalcommit_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* 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 conventionalcommit

import (
Expand Down
81 changes: 81 additions & 0 deletions internal/policy/license/license.go
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))
}
4 changes: 4 additions & 0 deletions internal/policy/policy.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* 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 policy

// Report summarizes the compliance of a policy.
Expand Down
4 changes: 4 additions & 0 deletions internal/policy/policy_options.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* 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 policy

// Option is a functional option used to pass in arguments to a Policy.
Expand Down
4 changes: 4 additions & 0 deletions internal/policy/version/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* 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 version

// Version defines the version policy to use and the options specific to the
Expand Down
15 changes: 3 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
// Copyright © 2017 NAME HERE <EMAIL ADDRESS>
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/* 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 main

Expand Down

0 comments on commit dea5c79

Please sign in to comment.