GH-9737: Allow setting min_server_version in plugin manifest (#9743)

* Add github.com/blang/semver as vendor

* Add MinServerVersion check for plugins

* Add tests for MinServerVersion in manifest

* Move logic to model/manifest.go & add tests
Этот коммит содержится в:
Hanzei
2018-11-05 14:29:25 +01:00
коммит произвёл Joram Wilander
родитель bce7a7c73d
Коммит 789b2f72dd
14 изменённых файлов: 1272 добавлений и 5 удалений

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

@@ -5,6 +5,7 @@ package model
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
@@ -12,6 +13,7 @@ import (
"path/filepath"
"strings"
"github.com/blang/semver"
"gopkg.in/yaml.v2"
)
@@ -111,6 +113,11 @@ type Manifest struct {
// A version number for your plugin. Semantic versioning is recommended: http://semver.org
Version string `json:"version" yaml:"version"`
// The minimum Mattermost server version required for your plugin.
//
// Minimum server version: 5.6
MinServerVersion string `json:"min_server_version,omitempty" yaml:"min_server_version,omitempty"`
// Server defines the server-side portion of your plugin.
Server *ManifestServer `json:"server,omitempty" yaml:"server,omitempty"`
@@ -242,6 +249,18 @@ func (m *Manifest) HasWebapp() bool {
return m.Webapp != nil
}
func (m *Manifest) MeetMinServerVersion(serverVersion string) (bool, error) {
minServerVersion, err := semver.Parse(m.MinServerVersion)
if err != nil {
return false, errors.New("failed to parse MinServerVersion")
}
sv := semver.MustParse(serverVersion)
if sv.LT(minServerVersion) {
return false, nil
}
return true, nil
}
// FindManifest will find and parse the manifest in a given directory.
//
// In all cases other than a does-not-exist error, path is set to the path of the manifest file that was

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

@@ -63,7 +63,8 @@ func TestFindManifest(t *testing.T) {
func TestManifestUnmarshal(t *testing.T) {
expected := Manifest{
Id: "theid",
Id: "theid",
MinServerVersion: "5.6.0",
Server: &ManifestServer{
Executable: "theexecutable",
Executables: &ManifestExecutables{
@@ -101,6 +102,7 @@ func TestManifestUnmarshal(t *testing.T) {
var yamlResult Manifest
require.NoError(t, yaml.Unmarshal([]byte(`
id: theid
min_server_version: 5.6.0
server:
executable: theexecutable
executables:
@@ -129,6 +131,7 @@ settings_schema:
var jsonResult Manifest
require.NoError(t, json.Unmarshal([]byte(`{
"id": "theid",
"min_server_version": "5.6.0",
"server": {
"executable": "theexecutable",
"executables": {
@@ -246,10 +249,11 @@ func TestManifestHasClient(t *testing.T) {
func TestManifestClientManifest(t *testing.T) {
manifest := &Manifest{
Id: "theid",
Name: "thename",
Description: "thedescription",
Version: "0.0.1",
Id: "theid",
Name: "thename",
Description: "thedescription",
Version: "0.0.1",
MinServerVersion: "5.6.0",
Server: &ManifestServer{
Executable: "theexecutable",
},
@@ -284,6 +288,7 @@ func TestManifestClientManifest(t *testing.T) {
assert.Equal(t, manifest.Id, sanitized.Id)
assert.Equal(t, manifest.Version, sanitized.Version)
assert.Equal(t, manifest.MinServerVersion, sanitized.MinServerVersion)
assert.Equal(t, "/static/theid/theid_000102030405060708090a0b0c0d0e0f_bundle.js", sanitized.Webapp.BundlePath)
assert.Equal(t, manifest.Webapp.BundleHash, sanitized.Webapp.BundleHash)
assert.Equal(t, manifest.SettingsSchema, sanitized.SettingsSchema)
@@ -293,6 +298,7 @@ func TestManifestClientManifest(t *testing.T) {
assert.NotEmpty(t, manifest.Id)
assert.NotEmpty(t, manifest.Version)
assert.NotEmpty(t, manifest.MinServerVersion)
assert.NotEmpty(t, manifest.Webapp)
assert.NotEmpty(t, manifest.Name)
assert.NotEmpty(t, manifest.Description)
@@ -594,3 +600,53 @@ func TestManifestHasWebapp(t *testing.T) {
})
}
}
func TestManifestMeetMinServerVersion(t *testing.T) {
for name, test := range map[string]struct {
MinServerVersion string
ServerVersion string
ShouldError bool
ShouldFulfill bool
}{
"generously fulfilled": {
MinServerVersion: "5.5.0",
ServerVersion: "5.6.0",
ShouldError: false,
ShouldFulfill: true,
},
"exactly fulfilled": {
MinServerVersion: "5.6.0",
ServerVersion: "5.6.0",
ShouldError: false,
ShouldFulfill: true,
},
"not fulfilled": {
MinServerVersion: "5.6.0",
ServerVersion: "5.5.0",
ShouldError: false,
ShouldFulfill: false,
},
"fail to parse MinServerVersion": {
MinServerVersion: "abc",
ServerVersion: "5.5.0",
ShouldError: true,
},
} {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
manifest := Manifest{
MinServerVersion: test.MinServerVersion,
}
fulfilled, err := manifest.MeetMinServerVersion(test.ServerVersion)
if test.ShouldError {
assert.NotNil(err)
assert.False(fulfilled)
return
}
assert.Nil(err)
assert.Equal(test.ShouldFulfill, fulfilled)
})
}
}