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
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
}

View File

@ -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())