Skip to content

Commit 87beffa

Browse files
authored
Support organisations query handler (#662)
Adding an organisations query handler to support ds abstraction. At the moment this will not be exposed to the frontend as a query type but it can be in the future.
1 parent c03f95e commit 87beffa

9 files changed

Lines changed: 80 additions & 2 deletions

File tree

pkg/github/codescanning_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ func (m *mockClient) ListDeployments(ctx context.Context, owner, repo string, op
5656
return nil, nil, nil
5757
}
5858

59+
func (m *mockClient) ListAllOrgRepositories(ctx context.Context, opts *googlegithub.ListOptions) ([]*googlegithub.Repository, *googlegithub.Response, error) {
60+
return nil, nil, nil
61+
}
62+
5963
func TestGetCodeScanningAlerts(t *testing.T) {
6064
var (
6165
ctx = context.Background()

pkg/github/datasource.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,15 @@ func (d *Datasource) HandleDeploymentsQuery(ctx context.Context, query *models.D
229229
return GetDeploymentsInRange(ctx, d.client, opt, req.TimeRange.From, req.TimeRange.To)
230230
}
231231

232+
// HandleOrganizationsQuery is the query handler for listing GitHub Organizations
233+
func (d *Datasource) HandleOrganizationsQuery(ctx context.Context, query *models.OrganizationsQuery, req backend.DataQuery) (dfutil.Framer, error) {
234+
orgs, err := GetAllOrganizations(ctx, d.client)
235+
if err != nil {
236+
return nil, err
237+
}
238+
return Organizations(orgs), nil
239+
}
240+
232241
// CheckHealth is the health check for GitHub
233242
func (d *Datasource) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
234243
_, err := GetAllRepositories(ctx, d.client, models.ListRepositoriesOptions{

pkg/github/organizations.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,35 @@ import (
1010

1111
// An Organization is a single GitHub organization
1212
type Organization struct {
13-
Name string
13+
Login string
14+
Name string
15+
Description string
16+
URL string `graphql:"url"`
1417
}
1518

1619
// Organizations is a slice of GitHub Organizations
1720
type Organizations []Organization
1821

1922
// Frames converts the list of Organizations to a Grafana DataFrame
2023
func (c Organizations) Frames() data.Frames {
21-
return data.Frames{}
24+
frame := data.NewFrame(
25+
"organizations",
26+
data.NewField("login", nil, []string{}),
27+
data.NewField("name", nil, []string{}),
28+
data.NewField("description", nil, []string{}),
29+
data.NewField("url", nil, []string{}),
30+
)
31+
32+
for _, org := range c {
33+
frame.AppendRow(
34+
org.Login,
35+
org.Name,
36+
org.Description,
37+
org.URL,
38+
)
39+
}
40+
41+
return data.Frames{frame}
2242
}
2343

2444
// QueryListOrganizations is the GraphQL query for listing organizations
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package github
2+
3+
import (
4+
"context"
5+
6+
"github.com/grafana/github-datasource/pkg/dfutil"
7+
"github.com/grafana/github-datasource/pkg/models"
8+
"github.com/grafana/grafana-plugin-sdk-go/backend"
9+
)
10+
11+
func (s *QueryHandler) handleOrganizationsQuery(ctx context.Context, q backend.DataQuery) backend.DataResponse {
12+
query := &models.OrganizationsQuery{}
13+
if err := UnmarshalQuery(q.JSON, query); err != nil {
14+
return *err
15+
}
16+
return dfutil.FrameResponseWithError(s.Datasource.HandleOrganizationsQuery(ctx, query, q))
17+
}
18+
19+
// HandleOrganizations handles the plugin query for GitHub Organizations
20+
func (s *QueryHandler) HandleOrganizations(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
21+
return &backend.QueryDataResponse{
22+
Responses: processQueries(ctx, req, s.handleOrganizationsQuery),
23+
}, nil
24+
}

pkg/github/query_handler.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func GetQueryHandlers(s *QueryHandler) *datasource.QueryTypeMux {
6363
mux.HandleFunc(models.QueryTypeWorkflowRuns, s.HandleWorkflowRuns)
6464
mux.HandleFunc(models.QueryTypeCodeScanning, s.HandleCodeScanning)
6565
mux.HandleFunc(models.QueryTypeDeployments, s.HandleDeployments)
66+
mux.HandleFunc(models.QueryTypeOrganizations, s.HandleOrganizations)
6667

6768
return mux
6869
}

pkg/models/query.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,7 @@ type DeploymentsQuery struct {
161161
Query
162162
Options ListDeploymentsOptions `json:"options"`
163163
}
164+
165+
// OrganizationsQuery is used when querying for GitHub organizations
166+
type OrganizationsQuery struct {
167+
}

pkg/plugin/datasource.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type Datasource interface {
2929
HandleWorkflowUsageQuery(context.Context, *models.WorkflowUsageQuery, backend.DataQuery) (dfutil.Framer, error)
3030
HandleWorkflowRunsQuery(context.Context, *models.WorkflowRunsQuery, backend.DataQuery) (dfutil.Framer, error)
3131
HandleDeploymentsQuery(context.Context, *models.DeploymentsQuery, backend.DataQuery) (dfutil.Framer, error)
32+
HandleOrganizationsQuery(context.Context, *models.OrganizationsQuery, backend.DataQuery) (dfutil.Framer, error)
3233
CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error)
3334
QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error)
3435
}

pkg/plugin/datasource_caching.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,16 @@ func (c *CachedDatasource) HandleDeploymentsQuery(ctx context.Context, q *models
272272
return c.saveCache(req, f, err)
273273
}
274274

275+
// HandleOrganizationsQuery is the cache wrapper for the organizations query handler
276+
func (c *CachedDatasource) HandleOrganizationsQuery(ctx context.Context, q *models.OrganizationsQuery, req backend.DataQuery) (dfutil.Framer, error) {
277+
if value, err := c.getCache(req); err == nil {
278+
return value, err
279+
}
280+
281+
f, err := c.datasource.HandleOrganizationsQuery(ctx, q, req)
282+
return c.saveCache(req, f, err)
283+
}
284+
275285
// CheckHealth forwards the request to the datasource and does not perform any caching
276286
func (c *CachedDatasource) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
277287
return c.datasource.CheckHealth(ctx, req)

pkg/testutil/client.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,8 @@ func (c *TestClient) ListAlertsForOrg(ctx context.Context, owner string, opts *g
8181
func (c *TestClient) ListDeployments(ctx context.Context, owner, repo string, opts *googlegithub.DeploymentsListOptions) ([]*googlegithub.Deployment, *googlegithub.Response, error) {
8282
panic("unimplemented")
8383
}
84+
85+
// ListAllOrgRepositories is not implemented because it is not being used in tests at the moment.
86+
func (c *TestClient) ListAllOrgRepositories(ctx context.Context, opts *googlegithub.ListOptions) ([]*googlegithub.Repository, *googlegithub.Response, error) {
87+
panic("unimplemented")
88+
}

0 commit comments

Comments
 (0)