MM-17888 Check plugin Helpers minimum server version comments (#12663)

Этот коммит содержится в:
Paulo Bittencourt
2019-10-30 03:34:29 -04:00
коммит произвёл Ben Schumacher
родитель 7cc1f19453
Коммит 7d0d7c304e
17 изменённых файлов: 729 добавлений и 119 удалений

Просмотреть файл

@@ -4,123 +4,69 @@
package main
import (
"bytes"
"fmt"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"go/ast"
"golang.org/x/tools/go/packages"
"github.com/pkg/errors"
)
const pluginPackagePath = "github.com/mattermost/mattermost-server/plugin"
type result struct {
Warnings []string
Errors []string
}
type checkFn func(pkgPath string) (result, error)
var checks = []checkFn{
checkAPIVersionComments,
checkHelpersVersionComments,
}
func main() {
if err := runCheck(pluginPackagePath); err != nil {
var res result
for _, check := range checks {
res = runCheck(res, check)
}
var msgs []string
msgs = append(msgs, res.Errors...)
msgs = append(msgs, res.Warnings...)
sort.Strings(msgs)
if len(msgs) > 0 {
fmt.Fprintln(os.Stderr, "#", pluginPackagePath)
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(os.Stderr, strings.Join(msgs, "\n"))
}
if len(res.Errors) > 0 {
os.Exit(1)
}
}
func runCheck(pkgPath string) error {
pkg, err := getPackage(pkgPath)
func runCheck(prev result, fn checkFn) result {
res, err := fn(pluginPackagePath)
if err != nil {
return err
prev.Errors = append(prev.Errors, err.Error())
return prev
}
apiInterface := findAPIInterface(pkg.Syntax)
if apiInterface == nil {
return errors.Errorf("could not find API interface in package %s", pkgPath)
if len(res.Warnings) > 0 {
prev.Warnings = append(prev.Warnings, mapWarnings(res.Warnings)...)
}
invalidMethods := findInvalidMethods(apiInterface.Methods.List)
if len(invalidMethods) > 0 {
return errors.New(renderErrorMessage(pkg, invalidMethods))
if len(res.Errors) > 0 {
prev.Errors = append(prev.Errors, res.Errors...)
}
return nil
return prev
}
func getPackage(pkgPath string) (*packages.Package, error) {
cfg := &packages.Config{
Mode: packages.NeedName | packages.NeedTypes | packages.NeedSyntax,
func mapWarnings(ss []string) []string {
var out []string
for _, s := range ss {
out = append(out, "[warn] "+s)
}
pkgs, err := packages.Load(cfg, pkgPath)
if err != nil {
return nil, err
}
if len(pkgs) == 0 {
return nil, errors.Errorf("could not find package %s", pkgPath)
}
return pkgs[0], nil
}
func findAPIInterface(files []*ast.File) *ast.InterfaceType {
for _, f := range files {
var iface *ast.InterfaceType
ast.Inspect(f, func(n ast.Node) bool {
if t, ok := n.(*ast.TypeSpec); ok {
if i, ok := t.Type.(*ast.InterfaceType); ok && t.Name.Name == "API" {
iface = i
return false
}
}
return true
})
if iface != nil {
return iface
}
}
return nil
}
func findInvalidMethods(methods []*ast.Field) []*ast.Field {
var invalid []*ast.Field
for _, m := range methods {
if !hasValidMinimumVersionComment(m.Doc.Text()) {
invalid = append(invalid, m)
}
}
return invalid
}
var versionRequirementRE = regexp.MustCompile(`^Minimum server version: \d+\.\d+(\.\d+)?$`)
func hasValidMinimumVersionComment(s string) bool {
lines := strings.Split(strings.TrimSpace(s), "\n")
if len(lines) > 0 {
lastLine := lines[len(lines)-1]
return versionRequirementRE.MatchString(lastLine)
}
return false
}
func renderErrorMessage(pkg *packages.Package, methods []*ast.Field) string {
cwd, _ := os.Getwd()
out := &bytes.Buffer{}
for _, m := range methods {
pos := pkg.Fset.Position(m.Pos())
filename, err := filepath.Rel(cwd, pos.Filename)
if err != nil {
// If deriving a relative path fails for some reason,
// we prefer to still print the absolute path to the file.
filename = pos.Filename
}
fmt.Fprintf(out,
"%s:%d:%d: missing a minimum server version comment\n",
filename,
pos.Line,
pos.Column,
)
}
return out.String()
return out
}