-
Notifications
You must be signed in to change notification settings - Fork 7.4k
/
helpers_plugin.go
80 lines (64 loc) · 2.14 KB
/
helpers_plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package plugin
import (
"net/http"
"net/url"
"path"
"time"
"github.com/blang/semver"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/pkg/errors"
)
// InstallPluginFromURL implements Helpers.InstallPluginFromURL.
func (p *HelpersImpl) InstallPluginFromURL(downloadURL string, replace bool) (*model.Manifest, error) {
err := p.ensureServerVersion("5.18.0")
if err != nil {
return nil, err
}
parsedURL, err := url.Parse(downloadURL)
if err != nil {
return nil, errors.Wrap(err, "error while parsing url")
}
client := &http.Client{Timeout: time.Hour}
response, err := client.Get(parsedURL.String())
if err != nil {
return nil, errors.Wrap(err, "unable to download the plugin")
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return nil, errors.Errorf("received %d status code while downloading plugin from server", response.StatusCode)
}
manifest, installError := p.API.InstallPlugin(response.Body, replace)
if installError != nil {
return nil, errors.Wrap(err, "unable to install plugin on server")
}
return manifest, nil
}
func (p *HelpersImpl) ensureServerVersion(required string) error {
serverVersion := p.API.GetServerVersion()
currentVersion := semver.MustParse(serverVersion)
requiredVersion := semver.MustParse(required)
if currentVersion.LT(requiredVersion) {
return errors.Errorf("incompatible server version for plugin, minimum required version: %s, current version: %s", required, serverVersion)
}
return nil
}
// GetPluginAssetURL implements GetPluginAssetURL.
func (p *HelpersImpl) GetPluginAssetURL(pluginID, asset string) (string, error) {
if len(pluginID) == 0 {
return "", errors.New("empty pluginID provided")
}
if len(asset) == 0 {
return "", errors.New("empty asset name provided")
}
siteURL := *p.API.GetConfig().ServiceSettings.SiteURL
if siteURL == "" {
return "", errors.New("no SiteURL configured by the server")
}
u, err := url.Parse(siteURL + path.Join("/", pluginID, asset))
if err != nil {
return "", err
}
return u.String(), nil
}