diff --git a/checks/migrations.go b/checks/migrations.go index d139f79..000b3fb 100644 --- a/checks/migrations.go +++ b/checks/migrations.go @@ -4,7 +4,9 @@ package checks import ( + "bufio" "errors" + "os" "os/exec" "strings" @@ -24,6 +26,8 @@ var ( migrationImpBlockList = []string{ "code.gitea.io/gitea/modules/structs", } + migrationImpPrefix = "code.gitea.io/gitea/models/migrations/v" + migrationFile = "migrations.go" ) func checkMigrations(pass *analysis.Pass) (interface{}, error) { @@ -56,13 +60,39 @@ func checkMigrations(pass *analysis.Pass) (interface{}, error) { } imps := strings.Split(string(impsOut), "\n") + migrationCount := 0 for _, imp := range imps { if stringInSlice(imp, migrationImpBlockList) { pass.Reportf(0, "code.gitea.io/gitea/models/migrations cannot import the following packages: %s", migrationImpBlockList) return nil, nil } + + if strings.HasPrefix(imp, migrationImpPrefix) { + goFilesCmd := exec.Command("go", "list", "-f", `{{join .GoFiles "\n"}}`, imp) + goFilesOut, err := goFilesCmd.Output() + if err != nil { + return nil, err + } + // the last item is empty we need to ignore it + migrationCount += len(strings.Split(string(goFilesOut), "\n")) - 1 + } } + mf, err := os.Open(migrationFile) + if err != nil { + return nil, err + } + defer mf.Close() + + l := bufio.NewScanner(mf) + for l.Scan() { + if strings.Contains(l.Text(), "NewMigration(\"") { + migrationCount-- + } + } + if migrationCount != 0 { + pass.Reportf(0, "migration files count does not match migrations lengths in %s", migrationFile) + } return nil, nil }