-
Notifications
You must be signed in to change notification settings - Fork 66
feat: Add deployments query type
#610
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
twu
wants to merge
5
commits into
main
Choose a base branch
from
twu/add-deployment-query-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'grafana-github-datasource': patch | ||
| --- | ||
|
|
||
| Add deployments query type | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,124 @@ | ||||||
| package github | ||||||
|
|
||||||
| import ( | ||||||
| "context" | ||||||
| "fmt" | ||||||
| "time" | ||||||
|
|
||||||
| googlegithub "github.com/google/go-github/v81/github" | ||||||
| "github.com/grafana/grafana-plugin-sdk-go/data" | ||||||
|
|
||||||
| "github.com/grafana/github-datasource/pkg/models" | ||||||
| ) | ||||||
|
|
||||||
| // DeploymentsWrapper is a list of GitHub deployments | ||||||
| type DeploymentsWrapper []*googlegithub.Deployment | ||||||
|
|
||||||
| // Frames converts the list of deployments to a Grafana DataFrame | ||||||
| func (deployments DeploymentsWrapper) Frames() data.Frames { | ||||||
| frame := data.NewFrame( | ||||||
| "deployments", | ||||||
| data.NewField("id", nil, []*int64{}), | ||||||
| data.NewField("sha", nil, []*string{}), | ||||||
| data.NewField("ref", nil, []*string{}), | ||||||
| data.NewField("task", nil, []*string{}), | ||||||
| data.NewField("environment", nil, []*string{}), | ||||||
| data.NewField("description", nil, []*string{}), | ||||||
| data.NewField("creator", nil, []*string{}), | ||||||
| data.NewField("created_at", nil, []*time.Time{}), | ||||||
| data.NewField("updated_at", nil, []*time.Time{}), | ||||||
| data.NewField("url", nil, []*string{}), | ||||||
| data.NewField("statuses_url", nil, []*string{}), | ||||||
| ) | ||||||
|
|
||||||
| for _, deployment := range deployments { | ||||||
| var creator *string | ||||||
| if deployment.Creator != nil { | ||||||
| creatorLogin := deployment.Creator.GetLogin() | ||||||
| if creatorLogin != "" { | ||||||
| creator = &creatorLogin | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| frame.AppendRow( | ||||||
| deployment.ID, | ||||||
| deployment.SHA, | ||||||
| deployment.Ref, | ||||||
| deployment.Task, | ||||||
| deployment.Environment, | ||||||
| deployment.Description, | ||||||
| creator, | ||||||
| deployment.CreatedAt.GetTime(), | ||||||
| deployment.UpdatedAt.GetTime(), | ||||||
| deployment.URL, | ||||||
| deployment.StatusesURL, | ||||||
| ) | ||||||
| } | ||||||
|
|
||||||
| frame.Meta = &data.FrameMeta{PreferredVisualization: data.VisTypeTable} | ||||||
| return data.Frames{frame} | ||||||
| } | ||||||
|
|
||||||
| // GetAllDeployments retrieves every deployment from a repository | ||||||
| func GetAllDeployments(ctx context.Context, client models.Client, opts models.ListDeploymentsOptions) (DeploymentsWrapper, error) { | ||||||
| if opts.Owner == "" || opts.Repository == "" { | ||||||
| return nil, nil | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be an error? |
||||||
| } | ||||||
|
|
||||||
| deployments := []*googlegithub.Deployment{} | ||||||
|
|
||||||
| // Build the list options with filters | ||||||
| listOpts := &googlegithub.DeploymentsListOptions{ | ||||||
| ListOptions: googlegithub.ListOptions{PerPage: 100}, | ||||||
| } | ||||||
|
|
||||||
| if opts.SHA != "" { | ||||||
| listOpts.SHA = opts.SHA | ||||||
| } | ||||||
| if opts.Ref != "" { | ||||||
| listOpts.Ref = opts.Ref | ||||||
| } | ||||||
| if opts.Task != "" { | ||||||
| listOpts.Task = opts.Task | ||||||
| } | ||||||
| if opts.Environment != "" { | ||||||
| listOpts.Environment = opts.Environment | ||||||
| } | ||||||
|
|
||||||
| page := 1 | ||||||
| for page != 0 { | ||||||
| listOpts.Page = page | ||||||
| deploymentsPage, resp, err := client.ListDeployments(ctx, opts.Owner, opts.Repository, listOpts) | ||||||
| if err != nil { | ||||||
| return nil, fmt.Errorf("listing deployments: opts=%+v %w", opts, err) | ||||||
| } | ||||||
|
|
||||||
| deployments = append(deployments, deploymentsPage...) | ||||||
|
|
||||||
| if resp == nil || resp.NextPage == 0 { | ||||||
| break | ||||||
| } | ||||||
| page = resp.NextPage | ||||||
| } | ||||||
|
|
||||||
| return DeploymentsWrapper(deployments), nil | ||||||
| } | ||||||
|
|
||||||
| // GetDeploymentsInRange retrieves every deployment from the repository and then returns the ones that fall within the given time range. | ||||||
| func GetDeploymentsInRange(ctx context.Context, client models.Client, opts models.ListDeploymentsOptions, from time.Time, to time.Time) (DeploymentsWrapper, error) { | ||||||
| deployments, err := GetAllDeployments(ctx, client, opts) | ||||||
| if err != nil { | ||||||
| return nil, err | ||||||
| } | ||||||
|
|
||||||
| filtered := []*googlegithub.Deployment{} | ||||||
|
|
||||||
| for _, deployment := range deployments { | ||||||
| createdAt := deployment.CreatedAt.GetTime() | ||||||
| if createdAt != nil && !createdAt.Before(from) && !createdAt.After(to) { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
potential nil pointer? |
||||||
| filtered = append(filtered, deployment) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return DeploymentsWrapper(filtered), nil | ||||||
| } | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/grafana/github-datasource/pkg/dfutil" | ||
| "github.com/grafana/github-datasource/pkg/models" | ||
| "github.com/grafana/grafana-plugin-sdk-go/backend" | ||
| ) | ||
|
|
||
| func (s *QueryHandler) handleDeploymentsQuery(ctx context.Context, q backend.DataQuery) backend.DataResponse { | ||
| query := &models.DeploymentsQuery{} | ||
| if err := UnmarshalQuery(q.JSON, query); err != nil { | ||
| return *err | ||
| } | ||
| return dfutil.FrameResponseWithError(s.Datasource.HandleDeploymentsQuery(ctx, query, q)) | ||
| } | ||
|
|
||
| // HandleDeployments handles the plugin query for github Deployments | ||
| func (s *QueryHandler) HandleDeployments(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) { | ||
| return &backend.QueryDataResponse{ | ||
| Responses: processQueries(ctx, req, s.handleDeploymentsQuery), | ||
| }, nil | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor ... adds funcitonality