-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathdatasource.go
More file actions
244 lines (203 loc) · 9.23 KB
/
Copy pathdatasource.go
File metadata and controls
244 lines (203 loc) · 9.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package github
import (
"context"
"fmt"
"strings"
"github.com/grafana/github-datasource/pkg/dfutil"
githubclient "github.com/grafana/github-datasource/pkg/github/client"
"github.com/grafana/github-datasource/pkg/github/projects"
"github.com/grafana/github-datasource/pkg/models"
"github.com/grafana/grafana-plugin-sdk-go/backend"
)
// Make sure Datasource implements required interfaces.
var (
_ backend.QueryDataHandler = (*Datasource)(nil)
_ backend.CheckHealthHandler = (*Datasource)(nil)
)
// Datasource handles requests to GitHub
type Datasource struct {
client *githubclient.Client
}
// HandleRepositoriesQuery is the query handler for listing GitHub Repositories
func (d *Datasource) HandleRepositoriesQuery(ctx context.Context, query *models.RepositoriesQuery, req backend.DataQuery) (dfutil.Framer, error) {
opt := models.ListRepositoriesOptions{
Owner: query.Owner,
Repository: query.Repository,
}
return GetAllRepositories(ctx, d.client, opt)
}
// HandleIssuesQuery is the query handler for listing GitHub Issues
func (d *Datasource) HandleIssuesQuery(ctx context.Context, query *models.IssuesQuery, req backend.DataQuery) (dfutil.Framer, error) {
opt := models.IssueOptionsWithRepo(query.Options, query.Owner, query.Repository)
return GetIssuesInRange(ctx, d.client, opt, req.TimeRange.From, req.TimeRange.To)
}
// HandleCommitsQuery is the query handler for listing GitHub Commits
func (d *Datasource) HandleCommitsQuery(ctx context.Context, query *models.CommitsQuery, req backend.DataQuery) (dfutil.Framer, error) {
opt := models.CommitsOptionsWithRepo(query.Options, query.Owner, query.Repository)
return GetCommitsInRange(ctx, d.client, opt, req.TimeRange.From, req.TimeRange.To)
}
// HandleTagsQuery is the query handler for listing GitHub Tags
func (d *Datasource) HandleTagsQuery(ctx context.Context, query *models.TagsQuery, req backend.DataQuery) (dfutil.Framer, error) {
opt := models.ListTagsOptions{
Repository: query.Repository,
Owner: query.Owner,
}
if req.TimeRange.From.Unix() <= 0 && req.TimeRange.To.Unix() <= 0 {
return GetAllTags(ctx, d.client, opt)
}
return GetTagsInRange(ctx, d.client, opt, req.TimeRange.From, req.TimeRange.To)
}
// HandleReleasesQuery is the query handler for listing GitHub Releases
func (d *Datasource) HandleReleasesQuery(ctx context.Context, query *models.ReleasesQuery, req backend.DataQuery) (dfutil.Framer, error) {
opt := models.ListReleasesOptions{
Repository: query.Repository,
Owner: query.Owner,
}
if req.TimeRange.From.Unix() <= 0 && req.TimeRange.To.Unix() <= 0 {
return GetAllReleases(ctx, d.client, opt)
}
return GetReleasesInRange(ctx, d.client, opt, req.TimeRange.From, req.TimeRange.To)
}
// HandlePullRequestsQuery is the query handler for listing GitHub PullRequests
func (d *Datasource) HandlePullRequestsQuery(ctx context.Context, query *models.PullRequestsQuery, req backend.DataQuery) (dfutil.Framer, error) {
opt := models.PullRequestOptionsWithRepo(query.Options, query.Owner, query.Repository)
if req.TimeRange.From.Unix() <= 0 && req.TimeRange.To.Unix() <= 0 {
return GetAllPullRequests(ctx, d.client, opt)
}
return GetPullRequestsInRange(ctx, d.client, opt, req.TimeRange.From, req.TimeRange.To)
}
// HandleContributorsQuery is the query handler for listing GitHub Contributors
func (d *Datasource) HandleContributorsQuery(ctx context.Context, query *models.ContributorsQuery, req backend.DataQuery) (dfutil.Framer, error) {
opt := models.ListContributorsOptions{
Owner: query.Owner,
Repository: query.Repository,
Query: query.Options.Query,
}
return GetAllContributors(ctx, d.client, opt)
}
// HandleLabelsQuery is the query handler for listing GitHub Labels
func (d *Datasource) HandleLabelsQuery(ctx context.Context, query *models.LabelsQuery, req backend.DataQuery) (dfutil.Framer, error) {
opt := models.ListLabelsOptions{
Repository: query.Repository,
Owner: query.Owner,
Query: query.Options.Query,
}
return GetAllLabels(ctx, d.client, opt)
}
// HandleMilestonesQuery is the query handler for listing GitHub Milestones
func (d *Datasource) HandleMilestonesQuery(ctx context.Context, query *models.MilestonesQuery, req backend.DataQuery) (dfutil.Framer, error) {
opt := models.ListMilestonesOptions{
Repository: query.Repository,
Owner: query.Owner,
Query: query.Options.Query,
}
return GetAllMilestones(ctx, d.client, opt)
}
// HandlePackagesQuery is the query handler for listing GitHub Packages
func (d *Datasource) HandlePackagesQuery(ctx context.Context, query *models.PackagesQuery, req backend.DataQuery) (dfutil.Framer, error) {
opt, err := models.PackagesOptionsWithRepo(query.Options, query.Owner, query.Repository)
if err != nil {
return nil, err
}
return GetAllPackages(ctx, d.client, opt)
}
// HandleVulnerabilitiesQuery is the query handler for listing GitHub Packages
func (d *Datasource) HandleVulnerabilitiesQuery(ctx context.Context, query *models.VulnerabilityQuery, req backend.DataQuery) (dfutil.Framer, error) {
opt := models.ListVulnerabilitiesOptions{
Repository: query.Repository,
Owner: query.Owner,
}
return GetAllVulnerabilities(ctx, d.client, opt)
}
// HandleProjectsQuery is the query handler for listing GitHub Projects
func (d *Datasource) HandleProjectsQuery(ctx context.Context, query *models.ProjectsQuery, req backend.DataQuery) (dfutil.Framer, error) {
opt := models.ProjectOptions{
Organization: query.Options.Organization,
Number: query.Options.Number,
User: query.Options.User,
Kind: query.Options.Kind,
Filters: query.Options.Filters,
}
if projects.ProjectNumber(query.Options.Number) > 0 {
return projects.GetAllProjectItems(ctx, d.client, opt)
}
return projects.GetAllProjects(ctx, d.client, opt)
}
// HandleStargazersQuery is the query handler for listing stargazers of a GitHub repository
func (d *Datasource) HandleStargazersQuery(ctx context.Context, query *models.StargazersQuery, req backend.DataQuery) (dfutil.Framer, error) {
opt := models.ListStargazersOptions{
Repository: query.Repository,
Owner: query.Owner,
}
return GetStargazers(ctx, d.client, opt, req.TimeRange)
}
// HandleWorkflowsQuery is the query handler for listing workflows of a GitHub repository
func (d *Datasource) HandleWorkflowsQuery(ctx context.Context, query *models.WorkflowsQuery, req backend.DataQuery) (dfutil.Framer, error) {
opt := models.ListWorkflowsOptions{
Repository: query.Repository,
Owner: query.Owner,
TimeField: query.Options.TimeField,
}
return GetWorkflows(ctx, d.client, opt, req.TimeRange)
}
// HandleWorkflowUsageQuery is the query handler for getting the usage information of a specific workflow
func (d *Datasource) HandleWorkflowUsageQuery(ctx context.Context, query *models.WorkflowUsageQuery, req backend.DataQuery) (dfutil.Framer, error) {
opt := models.WorkflowUsageOptions{
Repository: query.Repository,
Owner: query.Owner,
Workflow: query.Options.Workflow,
}
return GetWorkflowUsage(ctx, d.client, opt, req.TimeRange)
}
// HandleWorkflowRunsQuery is the query handler for listing workflow runs of a GitHub repository
func (d *Datasource) HandleWorkflowRunsQuery(ctx context.Context, query *models.WorkflowRunsQuery, req backend.DataQuery) (dfutil.Framer, error) {
opt := models.WorkflowRunsOptions{
Repository: query.Repository,
Owner: query.Owner,
Workflow: query.Options.Workflow,
Branch: query.Options.Branch,
Status: query.Options.Status,
}
return GetWorkflowRuns(ctx, d.client, opt, req.TimeRange)
}
// CheckHealth is the health check for GitHub
func (d *Datasource) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
_, err := GetAllRepositories(ctx, d.client, models.ListRepositoriesOptions{
Owner: "grafana",
Repository: "github-datasource",
})
if err != nil {
if strings.Contains(err.Error(), "401 Unauthorized") {
return newHealthResult(backend.HealthStatusError, "401 Unauthorized. Check your API key/Access token")
}
if strings.Contains(err.Error(), "404 Not Found") {
return newHealthResult(backend.HealthStatusError, "404 Not Found. Check the Github Enterprise Server URL")
}
if strings.HasSuffix(err.Error(), "no such host") {
return newHealthResult(backend.HealthStatusError, "Unable to reach the Github Enterprise Server URL from the Grafana server. Check the Github Enterprise Server URL and/or proxy settings")
}
return newHealthResult(backend.HealthStatusError, fmt.Sprintf("Health check failed. %s", err.Error()))
}
return newHealthResult(backend.HealthStatusOk, "Data source is working")
}
// QueryData runs the query
func (d *Datasource) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
m := GetQueryHandlers(&QueryHandler{
Datasource: *d,
})
return m.QueryData(ctx, req)
}
// NewDatasource creates a new datasource for handling queries
func NewDatasource(ctx context.Context, settings models.Settings) (*Datasource, error) {
client, err := githubclient.New(ctx, settings)
if err != nil {
return nil, err
}
return &Datasource{client: client}, nil
}
func newHealthResult(status backend.HealthStatus, message string) (*backend.CheckHealthResult, error) {
return &backend.CheckHealthResult{
Status: status,
Message: message,
}, nil
}