[MM-2307] Move plugin helpers from mattermost-server repository into mattermost-plugin-api (#17870)

Этот коммит содержится в:
Ben Schumacher
2021-08-06 11:54:56 +02:00
коммит произвёл GitHub
родитель 28137b3048
Коммит bf1f60d309
19 изменённых файлов: 67 добавлений и 2801 удалений

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

@@ -1,137 +0,0 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package main
import (
"fmt"
"go/ast"
"go/token"
"go/types"
"github.com/pkg/errors"
"github.com/mattermost/mattermost-server/v6/plugin/checker/internal/asthelpers"
"github.com/mattermost/mattermost-server/v6/plugin/checker/internal/version"
)
func checkHelpersVersionComments(pkgPath string) (result, error) {
pkg, err := asthelpers.GetPackage(pkgPath)
if err != nil {
return result{}, err
}
api, apiIdent, err := asthelpers.FindInterfaceWithIdent("API", pkg.Syntax)
if err != nil {
return result{}, err
}
apiObj := pkg.TypesInfo.ObjectOf(apiIdent)
if apiObj == nil {
return result{}, errors.New("could not find type object for API interface")
}
helpers, err := asthelpers.FindInterface("Helpers", pkg.Syntax)
if err != nil {
return result{}, err
}
apiVersions := mapMinimumVersionsByMethodName(api.Methods.List)
helpersPositions := mapPositionsByMethodName(helpers.Methods.List)
helpersVersions := mapMinimumVersionsByMethodName(helpers.Methods.List)
implMethods := asthelpers.FindReceiverMethods("HelpersImpl", pkg.Syntax)
implVersions := mapEffectiveVersionByMethod(pkg.TypesInfo, apiObj.Type(), apiVersions, implMethods)
return validateMethods(pkg.Fset, helpersPositions, helpersVersions, implVersions), nil
}
func validateMethods(
fset *token.FileSet,
helpersPositions map[string]token.Pos,
helpersVersions map[string]version.V,
implVersions map[string]version.V,
) result {
var res result
for name, helperVer := range helpersVersions {
pos := helpersPositions[name]
implVer, ok := implVersions[name]
if !ok {
res.Errors = append(res.Errors, renderWithFilePosition(
fset,
pos,
fmt.Sprintf("missing implementation for method %s", name)),
)
continue
}
if helperVer == "" {
res.Errors = append(res.Errors, renderWithFilePosition(
fset,
pos,
fmt.Sprintf("missing a minimum server version comment on method %s", name)),
)
continue
}
if helperVer == implVer {
continue
}
if helperVer.LessThan(implVer) {
res.Errors = append(res.Errors, renderWithFilePosition(
fset,
pos,
fmt.Sprintf("documented minimum server version too low on method %s", name)),
)
} else {
res.Warnings = append(res.Warnings, renderWithFilePosition(
fset,
pos,
fmt.Sprintf("documented minimum server version too high on method %s", name)),
)
}
}
return res
}
func mapEffectiveVersionByMethod(info *types.Info, apiType types.Type, versions map[string]version.V, methods []*ast.FuncDecl) map[string]version.V {
effectiveVersions := map[string]version.V{}
for _, m := range methods {
apiMethodsCalled := asthelpers.FindMethodsCalledOnType(info, apiType, m)
effectiveVersions[m.Name.Name] = getEffectiveMinimumVersion(versions, apiMethodsCalled)
}
return effectiveVersions
}
func mapMinimumVersionsByMethodName(methods []*ast.Field) map[string]version.V {
versions := map[string]version.V{}
for _, m := range methods {
versions[m.Names[0].Name] = version.V(version.ExtractMinimumVersionFromComment(m.Doc.Text()))
}
return versions
}
func mapPositionsByMethodName(methods []*ast.Field) map[string]token.Pos {
pos := map[string]token.Pos{}
for _, m := range methods {
pos[m.Names[0].Name] = m.Pos()
}
return pos
}
func getEffectiveMinimumVersion(info map[string]version.V, methods []string) version.V {
var highest version.V
for _, m := range methods {
if current, ok := info[m]; ok {
if current.GreaterThanOrEqualTo(highest) {
highest = current
}
}
}
return highest
}

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

@@ -1,48 +0,0 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestCheckHelpersVersionComments(t *testing.T) {
testCases := []struct {
name, pkgPath string
expected result
err string
}{
{
name: "valid versions",
pkgPath: "github.com/mattermost/mattermost-server/v6/plugin/checker/internal/test/valid",
expected: result{},
},
{
name: "invalid versions",
pkgPath: "github.com/mattermost/mattermost-server/v6/plugin/checker/internal/test/invalid",
expected: result{
Errors: []string{"internal/test/invalid/invalid.go:20:2: documented minimum server version too low on method LowerVersionMethod"},
Warnings: []string{"internal/test/invalid/invalid.go:23:2: documented minimum server version too high on method HigherVersionMethod"},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
assert := assert.New(t)
res, err := checkHelpersVersionComments(tc.pkgPath)
assert.Equal(tc.expected, res)
if tc.err != "" {
assert.EqualError(err, tc.err)
} else {
assert.NoError(err)
}
})
}
}

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

@@ -21,7 +21,6 @@ type checkFn func(pkgPath string) (result, error)
var checks = []checkFn{
checkAPIVersionComments,
checkHelpersVersionComments,
}
func main() {