Advisor: Include deprecated and filter by plugin slug (#105383)
This commit is contained in:
@@ -276,7 +276,7 @@ func (r *FakePluginRepo) PluginInfo(ctx context.Context, pluginID string, compat
|
||||
return &repo.PluginInfo{}, nil
|
||||
}
|
||||
|
||||
func (r *FakePluginRepo) GetPluginsInfo(ctx context.Context, compatOpts repo.CompatOpts) ([]repo.PluginInfo, error) {
|
||||
func (r *FakePluginRepo) GetPluginsInfo(ctx context.Context, options repo.GetPluginsInfoOptions, compatOpts repo.CompatOpts) ([]repo.PluginInfo, error) {
|
||||
return []repo.PluginInfo{}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ type Service interface {
|
||||
// PluginVersion will return plugin version based on the requested information.
|
||||
PluginVersion(ctx context.Context, pluginID, version string, compatOpts CompatOpts) (VersionData, error)
|
||||
// GetPluginsInfo will return a list of plugins from grafana.com/api/plugins.
|
||||
GetPluginsInfo(ctx context.Context, compatOpts CompatOpts) ([]PluginInfo, error)
|
||||
GetPluginsInfo(ctx context.Context, options GetPluginsInfoOptions, compatOpts CompatOpts) ([]PluginInfo, error)
|
||||
}
|
||||
|
||||
type CompatOpts struct {
|
||||
|
||||
+48
-14
@@ -13,6 +13,10 @@ import (
|
||||
"github.com/grafana/grafana/pkg/plugins/log"
|
||||
)
|
||||
|
||||
const (
|
||||
getPluginsInfoMaxPlugins = 50
|
||||
)
|
||||
|
||||
type Manager struct {
|
||||
client *Client
|
||||
|
||||
@@ -132,25 +136,55 @@ func (m *Manager) grafanaCompatiblePluginVersions(ctx context.Context, pluginID
|
||||
return v.Versions, nil
|
||||
}
|
||||
|
||||
func (m *Manager) GetPluginsInfo(ctx context.Context, compatOpts CompatOpts) ([]PluginInfo, error) {
|
||||
type GetPluginsInfoOptions struct {
|
||||
IncludeDeprecated bool
|
||||
Plugins []string
|
||||
}
|
||||
|
||||
func (m *Manager) GetPluginsInfo(ctx context.Context, options GetPluginsInfoOptions, compatOpts CompatOpts) ([]PluginInfo, error) {
|
||||
u, err := url.Parse(m.client.grafanaComAPIURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
body, err := m.client.SendReq(ctx, u, compatOpts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
// If we are requesting more than 50 plugins, split the request into multiple requests to avoid
|
||||
// the URL limit (local testing shows that the URL is <2000 characters for 100 plugins, so 50 is
|
||||
// a safe limit).
|
||||
plugins := [][]string{}
|
||||
results := []PluginInfo{}
|
||||
if len(options.Plugins) > getPluginsInfoMaxPlugins {
|
||||
for i := 0; i < len(options.Plugins); i += getPluginsInfoMaxPlugins {
|
||||
plugins = append(plugins, options.Plugins[i:min(i+getPluginsInfoMaxPlugins, len(options.Plugins))])
|
||||
}
|
||||
} else {
|
||||
plugins = [][]string{options.Plugins}
|
||||
}
|
||||
for _, p := range plugins {
|
||||
q := u.Query()
|
||||
if options.IncludeDeprecated {
|
||||
q.Set("includeDeprecated", "true")
|
||||
}
|
||||
if len(p) > 0 {
|
||||
q.Set("slugIn", strings.Join(p, ","))
|
||||
}
|
||||
u.RawQuery = q.Encode()
|
||||
|
||||
body, err := m.client.SendReq(ctx, u, compatOpts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var v struct {
|
||||
Items []PluginInfo `json:"items"`
|
||||
}
|
||||
err = json.Unmarshal(body, &v)
|
||||
if err != nil {
|
||||
m.log.Error("Failed to unmarshal plugin repo response", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
results = append(results, v.Items...)
|
||||
}
|
||||
|
||||
var v struct {
|
||||
Items []PluginInfo `json:"items"`
|
||||
}
|
||||
err = json.Unmarshal(body, &v)
|
||||
if err != nil {
|
||||
m.log.Error("Failed to unmarshal plugin repo response", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return v.Items, nil
|
||||
return results, nil
|
||||
}
|
||||
|
||||
@@ -116,6 +116,7 @@ func TestPluginIndex(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
require.Equal(t, "12.0.0", r.Header.Get("grafana-version"))
|
||||
require.Equal(t, "grafana 12.0.0", r.Header.Get("User-Agent"))
|
||||
require.Equal(t, "includeDeprecated=true&slugIn=grafana-test-datasource", r.URL.RawQuery)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = fmt.Fprintf(w, `{"items": [{ "id": 1, "slug": "%s", "status": "active" }]}`, pluginID)
|
||||
@@ -127,7 +128,10 @@ func TestPluginIndex(t *testing.T) {
|
||||
BaseURL: srv.URL,
|
||||
Logger: log.NewTestPrettyLogger(),
|
||||
})
|
||||
pi, err := m.GetPluginsInfo(context.Background(), CompatOpts{
|
||||
pi, err := m.GetPluginsInfo(context.Background(), GetPluginsInfoOptions{
|
||||
IncludeDeprecated: true,
|
||||
Plugins: []string{pluginID},
|
||||
}, CompatOpts{
|
||||
grafanaVersion: "12.0.0",
|
||||
system: SystemCompatOpts{
|
||||
os: "darwin",
|
||||
|
||||
Reference in New Issue
Block a user