Skip to content
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

refactor: utilise plugin-wide DefaultRetryConfig #322

Merged
merged 1 commit into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions github/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ import (
)

func shouldRetryError(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData, err error) bool {
if _, ok := err.(*github.AbuseRateLimitError); ok {
if e, ok := err.(*github.AbuseRateLimitError); ok {
var retryAfter *time.Duration
if err.(*github.AbuseRateLimitError).RetryAfter != nil {
retryAfter = err.(*github.AbuseRateLimitError).RetryAfter
if e.RetryAfter != nil {
retryAfter = e.RetryAfter
}
plugin.Logger(ctx).Debug("github_errors.shouldRetryError", "abuse_rate_limit_error", err, "retry_after", retryAfter)
return true
}

if _, ok := err.(*github.RateLimitError); ok {
if e, ok := err.(*github.RateLimitError); ok {
// Get the limit reset timestamp if returned
var resetAfter time.Time
if err.(*github.RateLimitError).Rate.String() != "" {
resetAfter = err.(*github.RateLimitError).Rate.Reset.Time
if e.Rate.String() != "" {
resetAfter = e.Rate.Reset.Time
}

// Get the remaining time
Expand Down Expand Up @@ -61,7 +61,7 @@ func retryConfig() *plugin.RetryConfig {
}
}

// function which returns an ErrorPredicate for Github API calls
// function which returns an ErrorPredicate for GitHub API calls
func isNotFoundError(notFoundErrors []string) plugin.ErrorPredicate {
return func(err error) bool {
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions github/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ func Plugin(ctx context.Context) *plugin.Plugin {
NewInstance: ConfigInstance,
Schema: ConfigSchema,
},
DefaultTransform: transform.FromGo(),
DefaultTransform: transform.FromGo(),
DefaultRetryConfig: retryConfig(),
TableMap: map[string]*plugin.Table{
"github_actions_artifact": tableGitHubActionsArtifact(),
"github_actions_repository_runner": tableGitHubActionsRepositoryRunner(),
Expand Down Expand Up @@ -56,7 +57,7 @@ func Plugin(ctx context.Context) *plugin.Plugin {
"github_search_code": tableGitHubSearchCode(),
"github_search_commit": tableGitHubSearchCommit(),
"github_search_issue": tableGitHubSearchIssue(),
"github_search_label": tableGitHubSearchLable(),
"github_search_label": tableGitHubSearchLabel(),
"github_search_pull_request": tableGitHubSearchPullRequest(),
"github_search_repository": tableGitHubSearchRepository(),
"github_search_topic": tableGitHubSearchTopic(),
Expand Down
44 changes: 2 additions & 42 deletions github/table_github_actions_artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
)

//// TABLE DEFINITION

func tableGitHubActionsArtifact() *plugin.Table {
return &plugin.Table{
Name: "github_actions_artifact",
Expand Down Expand Up @@ -43,19 +41,11 @@ func tableGitHubActionsArtifact() *plugin.Table {
}
}

//// LIST FUNCTION

func tableGitHubArtifactList(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
client := connect(ctx, d)

fullName := d.EqualsQuals["repository_full_name"].GetStringValue()
owner, repo := parseRepoFullName(fullName)

type ListPageResponse struct {
artifacts *github.ArtifactList
resp *github.Response
}

opts := &github.ListOptions{PerPage: 100}

limit := d.QueryContext.Limit
Expand All @@ -65,24 +55,12 @@ func tableGitHubArtifactList(ctx context.Context, d *plugin.QueryData, h *plugin
}
}

listPage := func(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
artifacts, resp, err := client.Actions.ListArtifacts(ctx, owner, repo, opts)
return ListPageResponse{
artifacts: artifacts,
resp: resp,
}, err
}

for {
listPageResponse, err := plugin.RetryHydrate(ctx, d, h, listPage, retryConfig())
artifacts, resp, err := client.Actions.ListArtifacts(ctx, owner, repo, opts)
if err != nil {
return nil, err
}

listResponse := listPageResponse.(ListPageResponse)
artifacts := listResponse.artifacts
resp := listResponse.resp

for _, i := range artifacts.Artifacts {
if i != nil {
d.StreamListItem(ctx, i)
Expand All @@ -104,8 +82,6 @@ func tableGitHubArtifactList(ctx context.Context, d *plugin.QueryData, h *plugin
return nil, nil
}

//// HYDRATE FUNCTIONS

func tableGitHubArtifactGet(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
id := d.EqualsQuals["id"].GetInt64Value()
fullName := d.EqualsQuals["repository_full_name"].GetStringValue()
Expand All @@ -120,26 +96,10 @@ func tableGitHubArtifactGet(ctx context.Context, d *plugin.QueryData, h *plugin.

client := connect(ctx, d)

type GetResponse struct {
artifact *github.Artifact
resp *github.Response
}

getDetails := func(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
detail, resp, err := client.Actions.GetArtifact(ctx, owner, repo, id)
return GetResponse{
artifact: detail,
resp: resp,
}, err
}

getResponse, err := plugin.RetryHydrate(ctx, d, h, getDetails, retryConfig())
artifact, _, err := client.Actions.GetArtifact(ctx, owner, repo, id)
if err != nil {
return nil, err
}

getResp := getResponse.(GetResponse)
artifact := getResp.artifact

return artifact, nil
}
45 changes: 3 additions & 42 deletions github/table_github_actions_repository_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
)

//// TABLE DEFINITION

func tableGitHubActionsRepositoryRunner() *plugin.Table {
return &plugin.Table{
Name: "github_actions_repository_runner",
Expand Down Expand Up @@ -39,19 +37,11 @@ func tableGitHubActionsRepositoryRunner() *plugin.Table {
}
}

//// LIST FUNCTION

func tableGitHubRunnerList(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
client := connect(ctx, d)

orgName := d.EqualsQuals["repository_full_name"].GetStringValue()
owner, repo := parseRepoFullName(orgName)

type ListPageResponse struct {
runners *github.Runners
resp *github.Response
}

opts := &github.ListOptions{PerPage: 100}

limit := d.QueryContext.Limit
Expand All @@ -61,24 +51,12 @@ func tableGitHubRunnerList(ctx context.Context, d *plugin.QueryData, h *plugin.H
}
}

listPage := func(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
runners, resp, err := client.Actions.ListRunners(ctx, owner, repo, opts)
return ListPageResponse{
runners: runners,
resp: resp,
}, err
}

for {
listPageResponse, err := plugin.RetryHydrate(ctx, d, h, listPage, retryConfig())
runners, resp, err := client.Actions.ListRunners(ctx, owner, repo, opts)
if err != nil {
return nil, err
}

listResponse := listPageResponse.(ListPageResponse)
runners := listResponse.runners
resp := listResponse.resp

for _, i := range runners.Runners {
if i != nil {
d.StreamListItem(ctx, i)
Expand All @@ -100,8 +78,6 @@ func tableGitHubRunnerList(ctx context.Context, d *plugin.QueryData, h *plugin.H
return nil, nil
}

//// HYDRATE FUNCTIONS

func tableGitHubRunnerGet(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
runnerId := d.EqualsQuals["id"].GetInt64Value()
orgName := d.EqualsQuals["repository_full_name"].GetStringValue()
Expand All @@ -116,25 +92,10 @@ func tableGitHubRunnerGet(ctx context.Context, d *plugin.QueryData, h *plugin.Hy

client := connect(ctx, d)

type GetResponse struct {
runner *github.Runner
resp *github.Response
}

getDetails := func(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
detail, resp, err := client.Actions.GetRunner(ctx, owner, repo, runnerId)
return GetResponse{
runner: detail,
resp: resp,
}, err
}

getResponse, err := plugin.RetryHydrate(ctx, d, h, getDetails, retryConfig())
runner, _, err := client.Actions.GetRunner(ctx, owner, repo, runnerId)
if err != nil {
return nil, err
}

getResp := getResponse.(GetResponse)

return getResp.runner, nil
return runner, nil
}
26 changes: 1 addition & 25 deletions github/table_github_actions_repository_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
)

//// TABLE DEFINITION

func tableGitHubActionsRepositorySecret() *plugin.Table {
return &plugin.Table{
Name: "github_actions_repository_secret",
Expand Down Expand Up @@ -40,19 +38,11 @@ func tableGitHubActionsRepositorySecret() *plugin.Table {
}
}

//// LIST FUNCTION

func tableGitHubRepoSecretList(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
client := connect(ctx, d)

orgName := d.EqualsQuals["repository_full_name"].GetStringValue()
owner, repo := parseRepoFullName(orgName)

type ListPageResponse struct {
secrets *github.Secrets
resp *github.Response
}

opts := &github.ListOptions{PerPage: 100}

limit := d.QueryContext.Limit
Expand All @@ -62,24 +52,12 @@ func tableGitHubRepoSecretList(ctx context.Context, d *plugin.QueryData, h *plug
}
}

listPage := func(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
secrets, resp, err := client.Actions.ListRepoSecrets(ctx, owner, repo, opts)
return ListPageResponse{
secrets: secrets,
resp: resp,
}, err
}

for {
listPageResponse, err := plugin.RetryHydrate(ctx, d, h, listPage, retryConfig())
secrets, resp, err := client.Actions.ListRepoSecrets(ctx, owner, repo, opts)
if err != nil {
return nil, err
}

listResponse := listPageResponse.(ListPageResponse)
secrets := listResponse.secrets
resp := listResponse.resp

for _, i := range secrets.Secrets {
if i != nil {
d.StreamListItem(ctx, i)
Expand All @@ -101,8 +79,6 @@ func tableGitHubRepoSecretList(ctx context.Context, d *plugin.QueryData, h *plug
return nil, nil
}

//// HYDRATE FUNCTIONS

func tableGitHubRepoSecretGet(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
name := d.EqualsQuals["name"].GetStringValue()
orgName := d.EqualsQuals["repository_full_name"].GetStringValue()
Expand Down
44 changes: 3 additions & 41 deletions github/table_github_actions_repository_workflow_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
)

//// TABLE DEFINITION

func tableGitHubActionsRepositoryWorkflowRun() *plugin.Table {
return &plugin.Table{
Name: "github_actions_repository_workflow_run",
Expand Down Expand Up @@ -65,19 +63,11 @@ func tableGitHubActionsRepositoryWorkflowRun() *plugin.Table {
}
}

//// LIST FUNCTION

func tableGitHubRepoWorkflowRunList(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
client := connect(ctx, d)

orgName := d.EqualsQuals["repository_full_name"].GetStringValue()
owner, repo := parseRepoFullName(orgName)

type ListPageResponse struct {
workflowRuns *github.WorkflowRuns
resp *github.Response
}

opts := &github.ListWorkflowRunsOptions{
ListOptions: github.ListOptions{PerPage: 100},
}
Expand Down Expand Up @@ -115,23 +105,12 @@ func tableGitHubRepoWorkflowRunList(ctx context.Context, d *plugin.QueryData, h
}
}

listPage := func(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
workflowRuns, resp, err := client.Actions.ListRepositoryWorkflowRuns(ctx, owner, repo, opts)
return ListPageResponse{
workflowRuns: workflowRuns,
resp: resp,
}, err
}

for {
listPageResponse, err := plugin.RetryHydrate(ctx, d, h, listPage, retryConfig())
workflowRuns, resp, err := client.Actions.ListRepositoryWorkflowRuns(ctx, owner, repo, opts)
if err != nil {
return nil, err
}

listResponse := listPageResponse.(ListPageResponse)
workflowRuns := listResponse.workflowRuns
resp := listResponse.resp
for _, i := range workflowRuns.WorkflowRuns {
if i != nil {
d.StreamListItem(ctx, i)
Expand All @@ -153,8 +132,6 @@ func tableGitHubRepoWorkflowRunList(ctx context.Context, d *plugin.QueryData, h
return nil, nil
}

//// HYDRATE FUNCTIONS

func tableGitHubRepoWorkflowRunGet(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
runId := d.EqualsQuals["id"].GetInt64Value()
orgName := d.EqualsQuals["repository_full_name"].GetStringValue()
Expand All @@ -169,25 +146,10 @@ func tableGitHubRepoWorkflowRunGet(ctx context.Context, d *plugin.QueryData, h *

client := connect(ctx, d)

type GetResponse struct {
workflowRun *github.WorkflowRun
resp *github.Response
}

getDetails := func(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
detail, resp, err := client.Actions.GetWorkflowRunByID(ctx, owner, repo, runId)
return GetResponse{
workflowRun: detail,
resp: resp,
}, err
}

getResponse, err := plugin.RetryHydrate(ctx, d, h, getDetails, retryConfig())
workflowRun, _, err := client.Actions.GetWorkflowRunByID(ctx, owner, repo, runId)
if err != nil {
return nil, err
}

getResp := getResponse.(GetResponse)

return getResp.workflowRun, nil
return workflowRun, nil
}
Loading