From 116088c7227ae465e3bc572fb90d20e393e7095a Mon Sep 17 00:00:00 2001 From: Timur Olzhabayev Date: Thu, 11 Apr 2024 12:04:53 +0200 Subject: [PATCH] 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 * making the names a bit more generic and using url.url library * fixing tests * fixing linting --------- Co-authored-by: Will Browne --- pkg/services/updatechecker/plugins.go | 27 +++++++++++++++++----- pkg/services/updatechecker/plugins_test.go | 17 ++++++++++++-- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/pkg/services/updatechecker/plugins.go b/pkg/services/updatechecker/plugins.go index 67a9f660124..1ff5f631c77 100644 --- a/pkg/services/updatechecker/plugins.go +++ b/pkg/services/updatechecker/plugins.go @@ -32,6 +32,7 @@ type PluginsService struct { mutex sync.RWMutex log log.Logger tracer tracing.Tracer + updateCheckURL *url.URL } 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 } + 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{ enabled: cfg.CheckForPluginUpdates, grafanaVersion: cfg.BuildVersion, @@ -53,6 +64,7 @@ func ProvidePluginsService(cfg *setting.Cfg, pluginStore pluginstore.Store, trac tracer: tracer, pluginStore: pluginStore, availableUpdates: make(map[string]string), + updateCheckURL: parsedUpdateCheckURL, }, nil } @@ -113,13 +125,16 @@ func (s *PluginsService) instrumentedCheckForUpdates(ctx context.Context) { func (s *PluginsService) checkForUpdates(ctx context.Context) error { ctxLogger := s.log.FromContext(ctx) - ctxLogger.Debug("Checking for updates") + ctxLogger.Debug("Preparing plugins eligible for version check") localPlugins := s.pluginsEligibleForVersionCheck(ctx) - requestURL := "https://grafana.com/api/plugins/versioncheck?" + url.Values{ - "slugIn": []string{s.pluginIDsCSV(localPlugins)}, - "grafanaVersion": []string{s.grafanaVersion}, - }.Encode() - req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestURL, nil) + requestURL := s.updateCheckURL + requestURLParameters := requestURL.Query() + requestURLParameters.Set("slugIn", s.pluginIDsCSV((localPlugins))) + requestURLParameters.Set("grafanaVersion", s.grafanaVersion) + 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 { return err } diff --git a/pkg/services/updatechecker/plugins_test.go b/pkg/services/updatechecker/plugins_test.go index e9fa4810eb8..370d3d160d9 100644 --- a/pkg/services/updatechecker/plugins_test.go +++ b/pkg/services/updatechecker/plugins_test.go @@ -4,6 +4,7 @@ import ( "context" "io" "net/http" + "net/url" "strings" "testing" @@ -17,6 +18,8 @@ import ( func TestPluginUpdateChecker_HasUpdate(t *testing.T) { t.Run("update is available", func(t *testing.T) { + updateCheckURL, _ := url.Parse("https://grafana.com/api/plugins/versioncheck") + svc := PluginsService{ availableUpdates: map[string]string{ "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") @@ -39,6 +43,8 @@ func TestPluginUpdateChecker_HasUpdate(t *testing.T) { }) t.Run("update is not available", func(t *testing.T) { + updateCheckURL, _ := url.Parse("https://grafana.com/api/plugins/versioncheck") + svc := PluginsService{ availableUpdates: map[string]string{ "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") @@ -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) { + updateCheckURL, _ := url.Parse("https://grafana.com/api/plugins/versioncheck") + svc := PluginsService{ availableUpdates: map[string]string{ "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") @@ -125,6 +135,8 @@ func TestPluginUpdateChecker_checkForUpdates(t *testing.T) { } ]` + updateCheckURL, _ := url.Parse("https://grafana.com/api/plugins/versioncheck") + svc := PluginsService{ availableUpdates: map[string]string{ "test-app": "1.0.0", @@ -168,8 +180,9 @@ func TestPluginUpdateChecker_checkForUpdates(t *testing.T) { httpClient: &fakeHTTPClient{ fakeResp: jsonResp, }, - log: log.NewNopLogger(), - tracer: tracing.InitializeTracerForTest(), + log: log.NewNopLogger(), + tracer: tracing.InitializeTracerForTest(), + updateCheckURL: updateCheckURL, } svc.instrumentedCheckForUpdates(context.Background())