From 658194310d37285a216da9b049ebc742a900471e Mon Sep 17 00:00:00 2001 From: Simon Murray Date: Mon, 5 Feb 2024 13:25:31 +0000 Subject: [PATCH] Fix Licenses Make the license checker a bit more flexible as regards management of copyrights etc. --- .github/workflows/pull-request.yaml | 6 +- charts/core/Chart.yaml | 4 +- hack/check_license/main.go | 95 +++++++++++++++---- pkg/apis/argoproj/v1alpha1/doc.go | 1 + pkg/apis/argoproj/v1alpha1/register.go | 1 + pkg/apis/argoproj/v1alpha1/types.go | 1 + pkg/apis/unikorn/v1alpha1/doc.go | 1 + pkg/apis/unikorn/v1alpha1/fake/doc.go | 1 + pkg/apis/unikorn/v1alpha1/fake/register.go | 1 + .../v1alpha1/helmapplication_helpers.go | 1 + .../unikorn/v1alpha1/helmapplication_types.go | 1 + pkg/apis/unikorn/v1alpha1/helpers.go | 1 + pkg/apis/unikorn/v1alpha1/interfaces.go | 1 + pkg/apis/unikorn/v1alpha1/register.go | 1 + pkg/apis/unikorn/v1alpha1/types.go | 1 + pkg/cd/context.go | 1 + pkg/cd/errors.go | 1 + pkg/cd/interfaces.go | 1 + pkg/cd/types.go | 1 + pkg/client/context.go | 1 + pkg/constants/constants.go | 1 + pkg/docs/document/helpers.go | 1 + pkg/docs/document/types.go | 1 + pkg/docs/formatter/helpers.go | 1 + pkg/docs/formatter/html.go | 1 + pkg/docs/formatter/interfaces.go | 1 + pkg/docs/formatter/markdown.go | 1 + pkg/errors/errors.go | 1 + pkg/provisioners/errors.go | 1 + pkg/provisioners/types.go | 1 + pkg/provisioners/util/scheduling.go | 1 + pkg/util/interfaces.go | 1 + pkg/util/retry/retry.go | 1 + pkg/util/trie/trie.go | 1 + pkg/util/util.go | 1 + 35 files changed, 114 insertions(+), 23 deletions(-) diff --git a/.github/workflows/pull-request.yaml b/.github/workflows/pull-request.yaml index bdd072a..4efb185 100644 --- a/.github/workflows/pull-request.yaml +++ b/.github/workflows/pull-request.yaml @@ -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: @@ -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 diff --git a/charts/core/Chart.yaml b/charts/core/Chart.yaml index 2cc8757..cad0a58 100644 --- a/charts/core/Chart.yaml +++ b/charts/core/Chart.yaml @@ -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 diff --git a/hack/check_license/main.go b/hack/check_license/main.go index 50d053f..865385f 100644 --- a/hack/check_license/main.go +++ b/hack/check_license/main.go @@ -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. @@ -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 @@ -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 ( @@ -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. @@ -169,6 +227,7 @@ func checkGoLicense() error { } if err := checkGoLicenseFile(path); err != nil { + fmt.Println("Errors in file", path) fmt.Println(err) hasErrors = true diff --git a/pkg/apis/argoproj/v1alpha1/doc.go b/pkg/apis/argoproj/v1alpha1/doc.go index dae573d..3a3491c 100644 --- a/pkg/apis/argoproj/v1alpha1/doc.go +++ b/pkg/apis/argoproj/v1alpha1/doc.go @@ -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. diff --git a/pkg/apis/argoproj/v1alpha1/register.go b/pkg/apis/argoproj/v1alpha1/register.go index ee4626f..36742f6 100644 --- a/pkg/apis/argoproj/v1alpha1/register.go +++ b/pkg/apis/argoproj/v1alpha1/register.go @@ -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. diff --git a/pkg/apis/argoproj/v1alpha1/types.go b/pkg/apis/argoproj/v1alpha1/types.go index 3f5ac30..27e0aa9 100644 --- a/pkg/apis/argoproj/v1alpha1/types.go +++ b/pkg/apis/argoproj/v1alpha1/types.go @@ -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. diff --git a/pkg/apis/unikorn/v1alpha1/doc.go b/pkg/apis/unikorn/v1alpha1/doc.go index f154610..133a8ce 100644 --- a/pkg/apis/unikorn/v1alpha1/doc.go +++ b/pkg/apis/unikorn/v1alpha1/doc.go @@ -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. diff --git a/pkg/apis/unikorn/v1alpha1/fake/doc.go b/pkg/apis/unikorn/v1alpha1/fake/doc.go index fc1bcf3..8414a02 100644 --- a/pkg/apis/unikorn/v1alpha1/fake/doc.go +++ b/pkg/apis/unikorn/v1alpha1/fake/doc.go @@ -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. diff --git a/pkg/apis/unikorn/v1alpha1/fake/register.go b/pkg/apis/unikorn/v1alpha1/fake/register.go index 613276c..5842cd7 100644 --- a/pkg/apis/unikorn/v1alpha1/fake/register.go +++ b/pkg/apis/unikorn/v1alpha1/fake/register.go @@ -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. diff --git a/pkg/apis/unikorn/v1alpha1/helmapplication_helpers.go b/pkg/apis/unikorn/v1alpha1/helmapplication_helpers.go index 2f83988..13476de 100644 --- a/pkg/apis/unikorn/v1alpha1/helmapplication_helpers.go +++ b/pkg/apis/unikorn/v1alpha1/helmapplication_helpers.go @@ -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. diff --git a/pkg/apis/unikorn/v1alpha1/helmapplication_types.go b/pkg/apis/unikorn/v1alpha1/helmapplication_types.go index 0375d80..506f1b4 100644 --- a/pkg/apis/unikorn/v1alpha1/helmapplication_types.go +++ b/pkg/apis/unikorn/v1alpha1/helmapplication_types.go @@ -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. diff --git a/pkg/apis/unikorn/v1alpha1/helpers.go b/pkg/apis/unikorn/v1alpha1/helpers.go index ec8bdf1..2d9ab00 100644 --- a/pkg/apis/unikorn/v1alpha1/helpers.go +++ b/pkg/apis/unikorn/v1alpha1/helpers.go @@ -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. diff --git a/pkg/apis/unikorn/v1alpha1/interfaces.go b/pkg/apis/unikorn/v1alpha1/interfaces.go index ac4aab4..46917e0 100644 --- a/pkg/apis/unikorn/v1alpha1/interfaces.go +++ b/pkg/apis/unikorn/v1alpha1/interfaces.go @@ -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. diff --git a/pkg/apis/unikorn/v1alpha1/register.go b/pkg/apis/unikorn/v1alpha1/register.go index ca7e48c..115db87 100644 --- a/pkg/apis/unikorn/v1alpha1/register.go +++ b/pkg/apis/unikorn/v1alpha1/register.go @@ -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. diff --git a/pkg/apis/unikorn/v1alpha1/types.go b/pkg/apis/unikorn/v1alpha1/types.go index 70623ad..cebc5a5 100644 --- a/pkg/apis/unikorn/v1alpha1/types.go +++ b/pkg/apis/unikorn/v1alpha1/types.go @@ -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. diff --git a/pkg/cd/context.go b/pkg/cd/context.go index 3fc4bc3..e299f90 100644 --- a/pkg/cd/context.go +++ b/pkg/cd/context.go @@ -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. diff --git a/pkg/cd/errors.go b/pkg/cd/errors.go index bc92b2a..5fdc4c5 100644 --- a/pkg/cd/errors.go +++ b/pkg/cd/errors.go @@ -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. diff --git a/pkg/cd/interfaces.go b/pkg/cd/interfaces.go index 0438867..5921446 100644 --- a/pkg/cd/interfaces.go +++ b/pkg/cd/interfaces.go @@ -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. diff --git a/pkg/cd/types.go b/pkg/cd/types.go index df31ac6..77e5077 100644 --- a/pkg/cd/types.go +++ b/pkg/cd/types.go @@ -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. diff --git a/pkg/client/context.go b/pkg/client/context.go index 3ef3b1c..d1ec825 100644 --- a/pkg/client/context.go +++ b/pkg/client/context.go @@ -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. diff --git a/pkg/constants/constants.go b/pkg/constants/constants.go index 06eb2ce..3b99fcd 100644 --- a/pkg/constants/constants.go +++ b/pkg/constants/constants.go @@ -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. diff --git a/pkg/docs/document/helpers.go b/pkg/docs/document/helpers.go index d6e8a09..20e5fda 100644 --- a/pkg/docs/document/helpers.go +++ b/pkg/docs/document/helpers.go @@ -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. diff --git a/pkg/docs/document/types.go b/pkg/docs/document/types.go index e828c10..69bb067 100644 --- a/pkg/docs/document/types.go +++ b/pkg/docs/document/types.go @@ -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. diff --git a/pkg/docs/formatter/helpers.go b/pkg/docs/formatter/helpers.go index d6d539c..2e5b26e 100644 --- a/pkg/docs/formatter/helpers.go +++ b/pkg/docs/formatter/helpers.go @@ -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. diff --git a/pkg/docs/formatter/html.go b/pkg/docs/formatter/html.go index 96f3b3f..830b209 100644 --- a/pkg/docs/formatter/html.go +++ b/pkg/docs/formatter/html.go @@ -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. diff --git a/pkg/docs/formatter/interfaces.go b/pkg/docs/formatter/interfaces.go index bad5a97..5d2ff98 100644 --- a/pkg/docs/formatter/interfaces.go +++ b/pkg/docs/formatter/interfaces.go @@ -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. diff --git a/pkg/docs/formatter/markdown.go b/pkg/docs/formatter/markdown.go index 841583e..4461ad8 100644 --- a/pkg/docs/formatter/markdown.go +++ b/pkg/docs/formatter/markdown.go @@ -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. diff --git a/pkg/errors/errors.go b/pkg/errors/errors.go index f58ae57..c4f5bc7 100644 --- a/pkg/errors/errors.go +++ b/pkg/errors/errors.go @@ -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. diff --git a/pkg/provisioners/errors.go b/pkg/provisioners/errors.go index 8baab22..859f716 100644 --- a/pkg/provisioners/errors.go +++ b/pkg/provisioners/errors.go @@ -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. diff --git a/pkg/provisioners/types.go b/pkg/provisioners/types.go index 12424f9..732872d 100644 --- a/pkg/provisioners/types.go +++ b/pkg/provisioners/types.go @@ -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. diff --git a/pkg/provisioners/util/scheduling.go b/pkg/provisioners/util/scheduling.go index bdf9a8c..0918b48 100644 --- a/pkg/provisioners/util/scheduling.go +++ b/pkg/provisioners/util/scheduling.go @@ -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. diff --git a/pkg/util/interfaces.go b/pkg/util/interfaces.go index 8ff11d5..9ed6236 100644 --- a/pkg/util/interfaces.go +++ b/pkg/util/interfaces.go @@ -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. diff --git a/pkg/util/retry/retry.go b/pkg/util/retry/retry.go index 7fc17e9..6304cf6 100644 --- a/pkg/util/retry/retry.go +++ b/pkg/util/retry/retry.go @@ -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. diff --git a/pkg/util/trie/trie.go b/pkg/util/trie/trie.go index 4ca1273..e8f912c 100644 --- a/pkg/util/trie/trie.go +++ b/pkg/util/trie/trie.go @@ -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. diff --git a/pkg/util/util.go b/pkg/util/util.go index 1bcdd49..e3fb66d 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -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.