feat: check for dependency level

This commit is contained in:
Jason Song 2023-01-16 10:56:16 +08:00
parent 2b1561217f
commit 26c845daad
No known key found for this signature in database
GPG Key ID: 8402EEEE4511A8B5
2 changed files with 54 additions and 0 deletions

53
checks/dependencies.go Normal file
View File

@ -0,0 +1,53 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package checks
import (
"strings"
"golang.org/x/tools/go/analysis"
)
var Dependencies = &analysis.Analyzer{
Name: "dependencies",
Doc: "check for dependency level",
Run: runDependencies,
}
func runDependencies(pass *analysis.Pass) (interface{}, error) {
level := getLevel(pass.Pkg.Path())
if level == 0 {
return nil, nil
}
for _, file := range pass.Files {
for _, im := range file.Imports {
if getLevel(im.Path.Value) > level {
pass.Reportf(im.Pos(), "import %v", im.Path.Value)
}
}
}
return nil, nil
}
// See https://docs.gitea.io/zh-cn/guidelines-backend/#package-dependencies
var dependencyLevels = map[string]int{
"code.gitea.io/gitea/cmd": 5,
"code.gitea.io/gitea/routers": 4,
"code.gitea.io/gitea/services": 3,
"code.gitea.io/gitea/models": 2,
"code.gitea.io/gitea/modules": 1,
}
func getLevel(pkg string) int {
pkg = strings.Trim(pkg, "\"")
if strings.HasSuffix(pkg, "/test") || strings.HasSuffix(pkg, "_test") {
return 0
}
for k, v := range dependencyLevels {
if strings.HasPrefix(pkg, k) {
return v
}
}
return 0
}

View File

@ -15,5 +15,6 @@ func main() {
checks.License,
checks.Migrations,
checks.ModelsSession,
checks.Dependencies,
)
}