diff options
Diffstat (limited to 'vendor/gitea.com/jolheiser/gitea-vet/checks')
-rw-r--r-- | vendor/gitea.com/jolheiser/gitea-vet/checks/imports.go | 54 | ||||
-rw-r--r-- | vendor/gitea.com/jolheiser/gitea-vet/checks/license.go | 73 |
2 files changed, 127 insertions, 0 deletions
diff --git a/vendor/gitea.com/jolheiser/gitea-vet/checks/imports.go b/vendor/gitea.com/jolheiser/gitea-vet/checks/imports.go new file mode 100644 index 0000000000..6ef5975e2e --- /dev/null +++ b/vendor/gitea.com/jolheiser/gitea-vet/checks/imports.go @@ -0,0 +1,54 @@ +// 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. + +package checks + +import ( + "strings" + + "golang.org/x/tools/go/analysis" +) + +var Imports = &analysis.Analyzer{ + Name: "imports", + Doc: "check for import order.", + Run: runImports, +} + +func runImports(pass *analysis.Pass) (interface{}, error) { + for _, file := range pass.Files { + level := 0 + for _, im := range file.Imports { + var lvl int + val := im.Path.Value + if importHasPrefix(val, "code.gitea.io") { + lvl = 2 + } else if strings.Contains(val, ".") { + lvl = 3 + } else { + lvl = 1 + } + + if lvl < level { + pass.Reportf(file.Pos(), "Imports are sorted wrong") + break + } + level = lvl + } + } + return nil, nil +} + +func importHasPrefix(s, p string) bool { + return strings.HasPrefix(s, "\""+p) +} + +func sliceHasPrefix(s string, prefixes ...string) bool { + for _, p := range prefixes { + if importHasPrefix(s, p) { + return true + } + } + return false +} diff --git a/vendor/gitea.com/jolheiser/gitea-vet/checks/license.go b/vendor/gitea.com/jolheiser/gitea-vet/checks/license.go new file mode 100644 index 0000000000..83f9e3ca03 --- /dev/null +++ b/vendor/gitea.com/jolheiser/gitea-vet/checks/license.go @@ -0,0 +1,73 @@ +// 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. + +package checks + +import ( + "regexp" + "strings" + + "golang.org/x/tools/go/analysis" +) + +var ( + header = regexp.MustCompile(`.*Copyright.*\d{4}.*(Gitea|Gogs)`) + goGenerate = "//go:generate" + buildTag = "// +build" +) + +var License = &analysis.Analyzer{ + Name: "license", + Doc: "check for a copyright header.", + Run: runLicense, +} + +func runLicense(pass *analysis.Pass) (interface{}, error) { + for _, file := range pass.Files { + if len(file.Comments) == 0 { + pass.Reportf(file.Pos(), "Copyright not found") + continue + } + + if len(file.Comments[0].List) == 0 { + pass.Reportf(file.Pos(), "Copyright not found or wrong") + continue + } + + commentGroup := 0 + if strings.HasPrefix(file.Comments[0].List[0].Text, goGenerate) { + if len(file.Comments[0].List) > 1 { + pass.Reportf(file.Pos(), "Must be an empty line between the go:generate and the Copyright") + continue + } + commentGroup++ + } + + if strings.HasPrefix(file.Comments[0].List[0].Text, buildTag) { + commentGroup++ + } + + if len(file.Comments) < commentGroup+1 { + pass.Reportf(file.Pos(), "Copyright not found") + continue + } + + if len(file.Comments[commentGroup].List) < 1 { + pass.Reportf(file.Pos(), "Copyright not found or wrong") + continue + } + + var check bool + for _, comment := range file.Comments[commentGroup].List { + if header.MatchString(comment.Text) { + check = true + } + } + + if !check { + pass.Reportf(file.Pos(), "Copyright did not match check") + } + } + return nil, nil +} |