Add migrations check #5
11
README.md
11
README.md
@ -1,10 +1,11 @@
|
|||||||
# gitea-vet
|
# gitea-vet
|
||||||
|
|
||||||
[![Build Status](https://drone.gitea.com/api/badges/jolheiser/gitea-vet/status.svg)](https://drone.gitea.com/jolheiser/gitea-vet)
|
[![Build Status](https://drone.gitea.com/api/badges/gitea/gitea-vet/status.svg)](https://drone.gitea.com/gitea/gitea-vet)
|
||||||
|
|
||||||
`go vet` tool for Gitea
|
`go vet` tool for Gitea
|
||||||
|
|
||||||
| Analyzer | Description |
|
| Analyzer | Description |
|
||||||
|----------|---------------------------------------------------------------------|
|
|------------|-----------------------------------------------------------------------------|
|
||||||
| Imports | Checks for import sorting. stdlib->code.gitea.io->other |
|
| Imports | Checks for import sorting. stdlib->code.gitea.io->other |
|
||||||
| License | Checks file headers for some form of `Copyright...YYYY...Gitea/Gogs`|
|
| License | Checks file headers for some form of `Copyright...YYYY...Gitea/Gogs` |
|
||||||
|
| Migrations | Checks for black-listed packages in `code.gitea.io/gitea/models/migrations` |
|
||||||
|
@ -12,7 +12,7 @@ import (
|
|||||||
|
|
||||||
var Imports = &analysis.Analyzer{
|
var Imports = &analysis.Analyzer{
|
||||||
Name: "imports",
|
Name: "imports",
|
||||||
Doc: "check for import order.",
|
Doc: "check for import order",
|
||||||
Run: runImports,
|
Run: runImports,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ var (
|
|||||||
|
|
||||||
var License = &analysis.Analyzer{
|
var License = &analysis.Analyzer{
|
||||||
Name: "license",
|
Name: "license",
|
||||||
Doc: "check for a copyright header.",
|
Doc: "check for a copyright header",
|
||||||
Run: runLicense,
|
Run: runLicense,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
59
checks/migrations.go
Normal file
59
checks/migrations.go
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
// 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 (
|
||||||
|
"errors"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"golang.org/x/tools/go/analysis"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Migrations = &analysis.Analyzer{
|
||||||
|
Name: "migrations",
|
||||||
|
Doc: "check migrations for black-listed packages.",
|
||||||
|
Run: checkMigrations,
|
||||||
|
}
|
||||||
|
|
||||||
|
var migrationBlacklist = []string{
|
||||||
|
"code.gitea.io/gitea/models",
|
||||||
|
"code.gitea.io/gitea/modules/structs",
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkMigrations(pass *analysis.Pass) (interface{}, error) {
|
||||||
|
if !strings.EqualFold(pass.Pkg.Path(), "code.gitea.io/gitea/models/migrations") {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := exec.LookPath("go"); err != nil {
|
||||||
|
return nil, errors.New("go was not found in the PATH")
|
||||||
|
}
|
||||||
|
|
||||||
|
depsCmd := exec.Command("go", "list", "-f", `{{join .Deps "\n"}}`, "code.gitea.io/gitea/models/migrations")
|
||||||
|
depsOut, err := depsCmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
deps := strings.Split(string(depsOut), "\n")
|
||||||
|
for _, dep := range deps {
|
||||||
|
if stringInSlice(dep, migrationBlacklist) {
|
||||||
|
pass.Reportf(0, "code.gitea.io/gitea/models/migrations cannot depend on the following packages: %s", migrationBlacklist)
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func stringInSlice(needle string, haystack []string) bool {
|
||||||
|
for _, h := range haystack {
|
||||||
|
if strings.EqualFold(needle, h) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
1
main.go
1
main.go
@ -14,5 +14,6 @@ func main() {
|
|||||||
unitchecker.Main(
|
unitchecker.Main(
|
||||||
checks.Imports,
|
checks.Imports,
|
||||||
checks.License,
|
checks.License,
|
||||||
|
checks.Migrations,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user