Chore: Making versioncheck url rely on config instead of being hardcoded (#85855)

* Making versioncheck url rely on config instead of being hardcoded

* Update pkg/services/updatechecker/plugins.go

Co-authored-by: Will Browne <wbrowne@users.noreply.github.com>

* making the names a bit more generic and using url.url library

* fixing tests

* fixing linting

---------

Co-authored-by: Will Browne <wbrowne@users.noreply.github.com>
This commit is contained in:
Timur Olzhabayev 2024-04-11 12:04:53 +02:00 committed by GitHub
parent 0ec8ccbf66
commit 116088c722
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 8 deletions

View File

@ -32,6 +32,7 @@ type PluginsService struct {
mutex sync.RWMutex mutex sync.RWMutex
log log.Logger log log.Logger
tracer tracing.Tracer tracer tracing.Tracer
updateCheckURL *url.URL
} }
func ProvidePluginsService(cfg *setting.Cfg, pluginStore pluginstore.Store, tracer tracing.Tracer) (*PluginsService, error) { func ProvidePluginsService(cfg *setting.Cfg, pluginStore pluginstore.Store, tracer tracing.Tracer) (*PluginsService, error) {
@ -45,6 +46,16 @@ func ProvidePluginsService(cfg *setting.Cfg, pluginStore pluginstore.Store, trac
return nil, err return nil, err
} }
updateCheckURL, err := url.JoinPath(cfg.GrafanaComAPIURL, "plugins", "versioncheck")
if err != nil {
return nil, err
}
parsedUpdateCheckURL, err := url.Parse(updateCheckURL)
if err != nil {
return nil, err
}
return &PluginsService{ return &PluginsService{
enabled: cfg.CheckForPluginUpdates, enabled: cfg.CheckForPluginUpdates,
grafanaVersion: cfg.BuildVersion, grafanaVersion: cfg.BuildVersion,
@ -53,6 +64,7 @@ func ProvidePluginsService(cfg *setting.Cfg, pluginStore pluginstore.Store, trac
tracer: tracer, tracer: tracer,
pluginStore: pluginStore, pluginStore: pluginStore,
availableUpdates: make(map[string]string), availableUpdates: make(map[string]string),
updateCheckURL: parsedUpdateCheckURL,
}, nil }, nil
} }
@ -113,13 +125,16 @@ func (s *PluginsService) instrumentedCheckForUpdates(ctx context.Context) {
func (s *PluginsService) checkForUpdates(ctx context.Context) error { func (s *PluginsService) checkForUpdates(ctx context.Context) error {
ctxLogger := s.log.FromContext(ctx) ctxLogger := s.log.FromContext(ctx)
ctxLogger.Debug("Checking for updates") ctxLogger.Debug("Preparing plugins eligible for version check")
localPlugins := s.pluginsEligibleForVersionCheck(ctx) localPlugins := s.pluginsEligibleForVersionCheck(ctx)
requestURL := "https://grafana.com/api/plugins/versioncheck?" + url.Values{ requestURL := s.updateCheckURL
"slugIn": []string{s.pluginIDsCSV(localPlugins)}, requestURLParameters := requestURL.Query()
"grafanaVersion": []string{s.grafanaVersion}, requestURLParameters.Set("slugIn", s.pluginIDsCSV((localPlugins)))
}.Encode() requestURLParameters.Set("grafanaVersion", s.grafanaVersion)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestURL, nil) requestURL.RawQuery = requestURLParameters.Encode()
ctxLogger.Debug("Checking for plugin updates", "url", requestURL)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestURL.String(), nil)
if err != nil { if err != nil {
return err return err
} }

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"io" "io"
"net/http" "net/http"
"net/url"
"strings" "strings"
"testing" "testing"
@ -17,6 +18,8 @@ import (
func TestPluginUpdateChecker_HasUpdate(t *testing.T) { func TestPluginUpdateChecker_HasUpdate(t *testing.T) {
t.Run("update is available", func(t *testing.T) { t.Run("update is available", func(t *testing.T) {
updateCheckURL, _ := url.Parse("https://grafana.com/api/plugins/versioncheck")
svc := PluginsService{ svc := PluginsService{
availableUpdates: map[string]string{ availableUpdates: map[string]string{
"test-ds": "1.0.0", "test-ds": "1.0.0",
@ -31,6 +34,7 @@ func TestPluginUpdateChecker_HasUpdate(t *testing.T) {
}, },
}, },
}, },
updateCheckURL: updateCheckURL,
} }
update, exists := svc.HasUpdate(context.Background(), "test-ds") update, exists := svc.HasUpdate(context.Background(), "test-ds")
@ -39,6 +43,8 @@ func TestPluginUpdateChecker_HasUpdate(t *testing.T) {
}) })
t.Run("update is not available", func(t *testing.T) { t.Run("update is not available", func(t *testing.T) {
updateCheckURL, _ := url.Parse("https://grafana.com/api/plugins/versioncheck")
svc := PluginsService{ svc := PluginsService{
availableUpdates: map[string]string{ availableUpdates: map[string]string{
"test-panel": "0.9.0", "test-panel": "0.9.0",
@ -66,6 +72,7 @@ func TestPluginUpdateChecker_HasUpdate(t *testing.T) {
}, },
}, },
}, },
updateCheckURL: updateCheckURL,
} }
update, exists := svc.HasUpdate(context.Background(), "test-ds") update, exists := svc.HasUpdate(context.Background(), "test-ds")
@ -82,6 +89,8 @@ func TestPluginUpdateChecker_HasUpdate(t *testing.T) {
}) })
t.Run("update is available but plugin is not in store", func(t *testing.T) { t.Run("update is available but plugin is not in store", func(t *testing.T) {
updateCheckURL, _ := url.Parse("https://grafana.com/api/plugins/versioncheck")
svc := PluginsService{ svc := PluginsService{
availableUpdates: map[string]string{ availableUpdates: map[string]string{
"test-panel": "0.9.0", "test-panel": "0.9.0",
@ -96,6 +105,7 @@ func TestPluginUpdateChecker_HasUpdate(t *testing.T) {
}, },
}, },
}, },
updateCheckURL: updateCheckURL,
} }
update, exists := svc.HasUpdate(context.Background(), "test-panel") update, exists := svc.HasUpdate(context.Background(), "test-panel")
@ -125,6 +135,8 @@ func TestPluginUpdateChecker_checkForUpdates(t *testing.T) {
} }
]` ]`
updateCheckURL, _ := url.Parse("https://grafana.com/api/plugins/versioncheck")
svc := PluginsService{ svc := PluginsService{
availableUpdates: map[string]string{ availableUpdates: map[string]string{
"test-app": "1.0.0", "test-app": "1.0.0",
@ -168,8 +180,9 @@ func TestPluginUpdateChecker_checkForUpdates(t *testing.T) {
httpClient: &fakeHTTPClient{ httpClient: &fakeHTTPClient{
fakeResp: jsonResp, fakeResp: jsonResp,
}, },
log: log.NewNopLogger(), log: log.NewNopLogger(),
tracer: tracing.InitializeTracerForTest(), tracer: tracing.InitializeTracerForTest(),
updateCheckURL: updateCheckURL,
} }
svc.instrumentedCheckForUpdates(context.Background()) svc.instrumentedCheckForUpdates(context.Background())