Skip to content

Commit f6d864f

Browse files
authored
PMM-5471 add license checks (#413)
* PMM-5471 fix files without license headers * PMM-5471 include license check in github workflow * PMM-5471 update copyright year * PMM-5471 support multiple copyright years in license checker * PMM-5471 ignore copyright year in check * PMM-5471 use regexp to check copyright pattern * PMM-5471 restrict regex year
1 parent 4464a78 commit f6d864f

14 files changed

+232
-33
lines changed

.github/check-license.go

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,53 +21,61 @@
2121
package main
2222

2323
import (
24-
"bufio"
2524
"flag"
2625
"fmt"
2726
"io"
2827
"log"
2928
"os"
3029
"path/filepath"
3130
"regexp"
32-
"runtime"
3331
)
3432

35-
func getHeader() string {
36-
_, file, _, ok := runtime.Caller(0)
37-
if !ok {
38-
panic("runtime.Caller(0) failed")
39-
}
40-
f, err := os.Open(file)
41-
if err != nil {
42-
log.Fatal(err)
43-
}
44-
defer f.Close()
33+
var (
34+
generatedHeader = regexp.MustCompile(`^// Code generated .* DO NOT EDIT\.`)
4535

46-
var header string
47-
s := bufio.NewScanner(f)
48-
for s.Scan() {
49-
if s.Text() == "" {
50-
break
51-
}
52-
header += s.Text() + "\n"
53-
}
54-
header += "\n"
55-
if err := s.Err(); err != nil {
56-
log.Fatal(err)
57-
}
58-
return header
59-
}
36+
copyrightText = `// mongodb_exporter
37+
// Copyright (C) 2022 Percona LLC
38+
//
39+
// This program is free software: you can redistribute it and/or modify
40+
// it under the terms of the GNU Affero General Public License as published by
41+
// the Free Software Foundation, either version 3 of the License, or
42+
// (at your option) any later version.
43+
//
44+
// This program is distributed in the hope that it will be useful,
45+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
46+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
47+
// GNU Affero General Public License for more details.
48+
//
49+
// You should have received a copy of the GNU Affero General Public License
50+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
51+
`
6052

61-
var generatedHeader = regexp.MustCompile(`^// Code generated .* DO NOT EDIT\.`)
53+
copyrightPattern = regexp.MustCompile(`^// mongodb_exporter
54+
// Copyright \(C\) 20\d{2} Percona LLC
55+
//
56+
// This program is free software: you can redistribute it and/or modify
57+
// it under the terms of the GNU Affero General Public License as published by
58+
// the Free Software Foundation, either version 3 of the License, or
59+
// \(at your option\) any later version.
60+
//
61+
// This program is distributed in the hope that it will be useful,
62+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
63+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
64+
// GNU Affero General Public License for more details.
65+
//
66+
// You should have received a copy of the GNU Affero General Public License
67+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
68+
`)
69+
)
6270

63-
func checkHeader(path string, header string) bool {
71+
func checkHeader(path string) bool {
6472
f, err := os.Open(path)
6573
if err != nil {
6674
log.Fatal(err)
6775
}
6876
defer f.Close()
6977

70-
actual := make([]byte, len(header))
78+
actual := make([]byte, len(copyrightText))
7179
_, err = io.ReadFull(f, actual)
7280
if err == io.ErrUnexpectedEOF {
7381
err = nil // some files are shorter than license header
@@ -81,7 +89,7 @@ func checkHeader(path string, header string) bool {
8189
return true
8290
}
8391

84-
if header != string(actual) {
92+
if !copyrightPattern.Match(actual) {
8593
log.Print(path)
8694
return false
8795
}
@@ -96,8 +104,6 @@ func main() {
96104
}
97105
flag.Parse()
98106

99-
header := getHeader()
100-
101107
ok := true
102108
filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
103109
if err != nil {
@@ -113,7 +119,7 @@ func main() {
113119
}
114120

115121
if filepath.Ext(info.Name()) == ".go" {
116-
if !checkHeader(path, header) {
122+
if !checkHeader(path) {
117123
ok = false
118124
}
119125
}

.github/workflows/go.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,4 @@ jobs:
8383
# use GITHUB_TOKEN because only it has access to GitHub Checks API
8484
bin/golangci-lint run -c=.golangci-required.yml --out-format=line-number | env REVIEWDOG_GITHUB_API_TOKEN=${{ secrets.GITHUB_TOKEN }} bin/reviewdog -f=golangci-lint -level=error -reporter=github-pr-check
8585
bin/golangci-lint run -c=.golangci.yml --out-format=line-number | env REVIEWDOG_GITHUB_API_TOKEN=${{ secrets.GITHUB_TOKEN }} bin/reviewdog -f=golangci-lint -level=error -reporter=github-pr-review
86+
make check-license

exporter/common.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
// mongodb_exporter
2+
// Copyright (C) 2022 Percona LLC
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
117
package exporter
218

319
import (

exporter/common_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
// mongodb_exporter
2+
// Copyright (C) 2022 Percona LLC
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
117
package exporter
218

319
import (

exporter/debug.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
// mongodb_exporter
2+
// Copyright (C) 2022 Percona LLC
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
117
package exporter
218

319
import (

exporter/debug_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
// mongodb_exporter
2+
// Copyright (C) 2022 Percona LLC
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
117
package exporter
218

319
import (

exporter/secondary_lag_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
// mongodb_exporter
2+
// Copyright (C) 2022 Percona LLC
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
117
package exporter
218

319
import (

exporter/utils_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
// mongodb_exporter
2+
// Copyright (C) 2022 Percona LLC
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
117
package exporter
218

319
import (

exporter/v1_compatibility.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
// mongodb_exporter
2+
// Copyright (C) 2022 Percona LLC
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
117
package exporter
218

319
import (

exporter/v1_compatibility_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
// mongodb_exporter
2+
// Copyright (C) 2022 Percona LLC
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
117
package exporter
218

319
import (

0 commit comments

Comments
 (0)