From 4c10dca949a3317538e2b3a95aed64173681792f Mon Sep 17 00:00:00 2001 From: Jason Song Date: Thu, 1 Dec 2022 23:58:05 +0800 Subject: [PATCH] Check SPDX-License-Identifier (#21) Check `SPDX-License-Identifier` in file headers. Related to https://github.com/go-gitea/gitea/pull/21840 Co-authored-by: Jason Song Reviewed-on: https://gitea.com/gitea/gitea-vet/pulls/21 Reviewed-by: Lunny Xiao Reviewed-by: Xinyu Zhou Co-authored-by: Jason Song Co-committed-by: Jason Song --- .golangci.yml | 6 +----- Makefile | 4 ++-- checks/denylisted-imports.go | 3 +-- checks/imports.go | 3 +-- checks/license.go | 21 ++++++++++++++------- checks/migrations.go | 3 +-- checks/models.go | 3 +-- main.go | 3 +-- 8 files changed, 22 insertions(+), 24 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 704d99a..0914b19 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,6 +1,5 @@ linters: enable: - - deadcode - dogsled - dupl - errcheck @@ -9,15 +8,12 @@ linters: - gocritic - gocyclo - gofmt - - golint + - revive - gosimple - govet - - maligned - misspell - prealloc - staticcheck - - structcheck - typecheck - unparam - unused - - varcheck \ No newline at end of file diff --git a/Makefile b/Makefile index e9c9f4f..b60a001 100644 --- a/Makefile +++ b/Makefile @@ -17,6 +17,6 @@ vet: build lint: @hash golangci-lint > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ export BINARY="golangci-lint"; \ - curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(shell $(GO) env GOPATH)/bin v1.24.0; \ + curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell $(GO) env GOPATH)/bin v1.50.1; \ fi - golangci-lint run --timeout 5m \ No newline at end of file + golangci-lint run --timeout 5m diff --git a/checks/denylisted-imports.go b/checks/denylisted-imports.go index 7fcb137..9ad4ebe 100644 --- a/checks/denylisted-imports.go +++ b/checks/denylisted-imports.go @@ -1,6 +1,5 @@ // Copyright 2022 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. +// SPDX-License-Identifier: MIT package checks diff --git a/checks/imports.go b/checks/imports.go index 15563c8..17190c5 100644 --- a/checks/imports.go +++ b/checks/imports.go @@ -1,6 +1,5 @@ // Copyright 2020 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. +// SPDX-License-Identifier: MIT package checks diff --git a/checks/license.go b/checks/license.go index a3ae047..11679c2 100644 --- a/checks/license.go +++ b/checks/license.go @@ -1,6 +1,5 @@ // Copyright 2020 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. +// SPDX-License-Identifier: MIT package checks @@ -12,7 +11,9 @@ import ( ) var ( - header = regexp.MustCompile(`.*Copyright.*\d{4}.*(Gitea|Gogs)`) + copyrightRegx = regexp.MustCompile(`.*Copyright.*\d{4}.*(Gitea|Gogs)`) + identifierRegx = regexp.MustCompile(`SPDX-License-Identifier: [\w.-]+`) + goGenerate = "//go:generate" buildTag = "// +build" ) @@ -58,16 +59,22 @@ func runLicense(pass *analysis.Pass) (interface{}, error) { continue } - var check bool + var copyright, identifier bool for _, comment := range file.Comments[commentGroup].List { - if header.MatchString(comment.Text) { - check = true + if copyrightRegx.MatchString(comment.Text) { + copyright = true + } + if identifierRegx.MatchString(comment.Text) { + identifier = true } } - if !check { + if !copyright { pass.Reportf(file.Pos(), "Copyright did not match check") } + if !identifier { + pass.Reportf(file.Pos(), "SPDX-License-Identifier did not match check") + } } return nil, nil } diff --git a/checks/migrations.go b/checks/migrations.go index e3fe570..d139f79 100644 --- a/checks/migrations.go +++ b/checks/migrations.go @@ -1,6 +1,5 @@ // Copyright 2020 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. +// SPDX-License-Identifier: MIT package checks diff --git a/checks/models.go b/checks/models.go index e7fb608..acb83b1 100644 --- a/checks/models.go +++ b/checks/models.go @@ -1,6 +1,5 @@ // Copyright 2020 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. +// SPDX-License-Identifier: MIT package checks diff --git a/main.go b/main.go index 52cd30f..ae92b8f 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,5 @@ // Copyright 2020 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. +// SPDX-License-Identifier: MIT package main