Skip to content

Commit

Permalink
Fix Licenses
Browse files Browse the repository at this point in the history
Make the license checker a bit more flexible as regards management of
copyrights etc.
  • Loading branch information
spjmurray committed Feb 5, 2024
1 parent 212f497 commit 6581943
Show file tree
Hide file tree
Showing 35 changed files with 114 additions and 23 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ jobs:
cache: true
- name: Touch
run: make touch
# - name: License Checker
# run: make license
- name: License Checker
run: make license
Runtime:
runs-on: ubuntu-latest
steps:
Expand All @@ -41,7 +41,7 @@ jobs:
- name: Golang CI/Helm Lint
run: make lint
- name: Build CRDs
run: make charts/unikorn-core/crds
run: make charts/core/crds
- name: Build Generated Code
run: make generate
- name: Generated Code Checked In
Expand Down
4 changes: 2 additions & 2 deletions charts/core/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: A Helm chart for deploying Unikorn Core

type: application

version: v0.1.0
appVersion: v0.1.0
version: v0.1.1
appVersion: v0.1.1

icon: https://assets.unikorn-cloud.org/images/logos/dark-on-light/icon.svg
95 changes: 77 additions & 18 deletions hack/check_license/main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -24,17 +25,16 @@ import (
"go/token"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
)

const (
// goApache2LicenseHeader is an exact match for a license header.
// TODO: may want to make this fuzzy (e.g. the date) and make it
// a regex match.
goApache2LicenseHeader = `/*
Copyright 2022-2024 EscherCloud.

Licensed under the Apache License, Version 2.0 (the "License");
// apache2LicenseHeader is an exact match for a license header.
apache2LicenseHeader = `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
Expand All @@ -44,8 +44,7 @@ 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.
*/`
limitations under the License.`
)

var (
Expand Down Expand Up @@ -90,22 +89,81 @@ func glob(extension string) ([]string, error) {
return files, nil
}

//nolint:cyclop
func checkFirstComment(comment *ast.Comment) error {
if comment.Slash != 1 {
return fmt.Errorf("%w: first comment starts at %d, expected 1", errFirstCommentNotLicense, comment.Slash)
}

if !strings.HasPrefix(comment.Text, "/*") {
return fmt.Errorf("%w: first comment doesn't start with a C style comment", errFirstCommentNotLicense)
}

text := strings.TrimSpace(comment.Text[2 : len(comment.Text)-2])

if !strings.HasSuffix(text, apache2LicenseHeader) {
return fmt.Errorf("%w: first comment doesn't end with a license", errFirstCommentNotLicense)
}

header := strings.TrimSuffix(text, apache2LicenseHeader)

headerLines := strings.Split(header, "\n")

// Expect a copyright, newline, a blank line, newline, nothing!.
if len(headerLines) < 3 {
return fmt.Errorf("%w: first comment must have a copyright and space before the license", errFirstCommentNotLicense)
}

lastHeaderLineIndex := len(headerLines) - 2

if headerLines[lastHeaderLineIndex] != "" {
return fmt.Errorf("%w: first comment doesn't have a space between copyrights and license", errFirstCommentNotLicense)
}

copyrights := headerLines[:lastHeaderLineIndex]

re := regexp.MustCompile(`^Copyright (\d{4})(?:-(\d{4}))? (.*)$`)

for _, copyright := range copyrights {
if !re.MatchString(copyright) {
return fmt.Errorf("%w: copyright not correctly formatted '%s'", errFirstCommentNotLicense, copyright)
}
}

lastCopyright := copyrights[lastHeaderLineIndex-1]

matches := re.FindStringSubmatch(lastCopyright)

yearString := matches[1]

if matches[2] != "" {
yearString = matches[2]
}

year, err := strconv.Atoi(yearString)
if err != nil {
return fmt.Errorf("%w: copyright doesn't contain a valid year '%s'", errFirstCommentNotLicense, yearString)
}

if year != time.Now().Year() {
return fmt.Errorf("%w: copyright doesn't contain this year %d", errFirstCommentNotLicense, year)
}

if matches[3] != "the Unikorn Authors." {
return fmt.Errorf("%w: copyright isn't for the right organization '%s'", errFirstCommentNotLicense, matches[3])
}

return nil
}

// checkGoLicenseInComments checks the AST for a license header anywhere the top
// level (because autogenerated code does what it likes).
func checkGoLicenseInComments(path string, file *ast.File) error {
if len(file.Comments) == 0 {
return fmt.Errorf("%s: %w", path, errNoComments)
}

for _, comment := range file.Comments {
for _, item := range comment.List {
if item.Text == goApache2LicenseHeader {
return nil
}
}
return fmt.Errorf("%w: %s", errNoComments, path)
}

return fmt.Errorf("%s: %w", path, errFirstCommentNotLicense)
return checkFirstComment(file.Comments[0].List[0])
}

// checkGoLicenseFile parses a source file and checks there is a license header in there.
Expand Down Expand Up @@ -169,6 +227,7 @@ func checkGoLicense() error {
}

if err := checkGoLicenseFile(path); err != nil {
fmt.Println("Errors in file", path)
fmt.Println(err)

hasErrors = true
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/argoproj/v1alpha1/doc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/argoproj/v1alpha1/register.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/argoproj/v1alpha1/types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/unikorn/v1alpha1/doc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/unikorn/v1alpha1/fake/doc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/unikorn/v1alpha1/fake/register.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/unikorn/v1alpha1/helmapplication_helpers.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/unikorn/v1alpha1/helmapplication_types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/unikorn/v1alpha1/helpers.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/unikorn/v1alpha1/interfaces.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/unikorn/v1alpha1/register.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/unikorn/v1alpha1/types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions pkg/cd/context.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions pkg/cd/errors.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions pkg/cd/interfaces.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions pkg/cd/types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions pkg/client/context.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions pkg/docs/document/helpers.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions pkg/docs/document/types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions pkg/docs/formatter/helpers.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions pkg/docs/formatter/html.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions pkg/docs/formatter/interfaces.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions pkg/docs/formatter/markdown.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions pkg/errors/errors.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions pkg/provisioners/errors.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions pkg/provisioners/types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions pkg/provisioners/util/scheduling.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions pkg/util/interfaces.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions pkg/util/retry/retry.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions pkg/util/trie/trie.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down

0 comments on commit 6581943

Please sign in to comment.