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 all Get hydrates #167

Closed
wants to merge 4 commits into from
Closed
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
41 changes: 12 additions & 29 deletions github/table_github_actions_artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,39 +107,22 @@ func tableGitHubArtifactList(ctx context.Context, d *plugin.QueryData, h *plugin
//// HYDRATE FUNCTIONS

func tableGitHubArtifactGet(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
id := d.KeyColumnQuals["id"].GetInt64Value()
fullName := d.KeyColumnQuals["repository_full_name"].GetStringValue()

// Empty check for the parameters
if id == 0 || fullName == "" {
return nil, nil
}
getDetails := func(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData, client *github.Client) (interface{}, error) {
id := d.KeyColumnQuals["id"].GetInt64Value()
fullName := d.KeyColumnQuals["repository_full_name"].GetStringValue()

owner, repo := parseRepoFullName(fullName)
plugin.Logger(ctx).Trace("tableGitHubArtifactGet", "owner", owner, "repo", repo, "id", id)

client := connect(ctx, d)
// Empty check for the parameters
if id == 0 || fullName == "" {
return nil, nil
}

type GetResponse struct {
artifact *github.Artifact
resp *github.Response
}
owner, repo := parseRepoFullName(fullName)

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
}
plugin.Logger(ctx).Trace("tableGitHubArtifactGet", "owner", owner, "repo", repo, "id", id)
detail, _, err := client.Actions.GetArtifact(ctx, owner, repo, id)

getResponse, err := plugin.RetryHydrate(ctx, d, h, getDetails, &plugin.RetryConfig{ShouldRetryError: shouldRetryError})
if err != nil {
return nil, err
return detail, err
}

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

return artifact, nil
return getGitHubItem(ctx, d, h, getDetails)
}
40 changes: 12 additions & 28 deletions github/table_github_actions_repository_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,38 +103,22 @@ func tableGitHubRunnerList(ctx context.Context, d *plugin.QueryData, h *plugin.H
//// HYDRATE FUNCTIONS

func tableGitHubRunnerGet(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
runnerId := d.KeyColumnQuals["id"].GetInt64Value()
orgName := d.KeyColumnQuals["repository_full_name"].GetStringValue()

// Empty check for the parameter
if runnerId == 0 || orgName == "" {
return nil, nil
}
getDetails := func(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData, client *github.Client) (interface{}, error) {
runnerId := d.KeyColumnQuals["id"].GetInt64Value()
fullname := d.KeyColumnQuals["repository_full_name"].GetStringValue()

owner, repo := parseRepoFullName(orgName)
plugin.Logger(ctx).Trace("tableGitHubRunnerGet", "owner", owner, "repo", repo, "runnerId", runnerId)

client := connect(ctx, d)
// Empty check for the parameter
if runnerId == 0 || fullname == "" {
return nil, nil
}

type GetResponse struct {
runner *github.Runner
resp *github.Response
}
owner, repo := parseRepoFullName(fullname)

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
}
plugin.Logger(ctx).Trace("tableGitHubRunnerGet", "owner", owner, "repo", repo, "runnerId", runnerId)
detail, _, err := client.Actions.GetRunner(ctx, owner, repo, runnerId)

getResponse, err := plugin.RetryHydrate(ctx, d, h, getDetails, &plugin.RetryConfig{ShouldRetryError: shouldRetryError})
if err != nil {
return nil, err
return detail, err
}

getResp := getResponse.(GetResponse)

return getResp.runner, nil
return getGitHubItem(ctx, d, h, getDetails)
}
42 changes: 13 additions & 29 deletions github/table_github_actions_repository_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func tableGitHubActionsRepositorySecret(ctx context.Context) *plugin.Table {
{Name: "repository_full_name", Type: proto.ColumnType_STRING, Transform: transform.FromQual("repository_full_name"), Description: "Full name of the repository that contains the secrets."},
{Name: "name", Type: proto.ColumnType_STRING, Description: "The name of the secret."},
{Name: "visibility", Type: proto.ColumnType_STRING, Description: "The visibility of the secret."},
{Name: "selected_repositories_url", Type: proto.ColumnType_STRING, Transform: transform.FromField("SelectedRepositoriesURL"),Description: "The GitHub URL of the repository."},
{Name: "selected_repositories_url", Type: proto.ColumnType_STRING, Transform: transform.FromField("SelectedRepositoriesURL"), Description: "The GitHub URL of the repository."},

// Other columns
{Name: "created_at", Type: proto.ColumnType_TIMESTAMP, Transform: transform.FromField("CreatedAt").Transform(convertTimestamp), Description: "Time when the secret was created."},
Expand Down Expand Up @@ -104,37 +104,21 @@ func tableGitHubRepoSecretList(ctx context.Context, d *plugin.QueryData, h *plug
//// HYDRATE FUNCTIONS

func tableGitHubRepoSecretGet(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
name := d.KeyColumnQuals["name"].GetStringValue()
orgName := d.KeyColumnQuals["repository_full_name"].GetStringValue()

// Empty check for the parameters
if name == "" || orgName == "" {
return nil, nil
}
owner, repo := parseRepoFullName(orgName)
plugin.Logger(ctx).Trace("tableGitHubRepoSecretGet", "owner", owner, "repo", repo, "name", name)

client := connect(ctx, d)
getDetails := func(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData, client *github.Client) (interface{}, error) {
name := d.KeyColumnQuals["name"].GetStringValue()
fullname := d.KeyColumnQuals["repository_full_name"].GetStringValue()

type GetResponse struct {
secret *github.Secret
resp *github.Response
}
// Empty check for the parameters
if name == "" || fullname == "" {
return nil, nil
}
owner, repo := parseRepoFullName(fullname)
plugin.Logger(ctx).Trace("tableGitHubRepoSecretGet", "owner", owner, "repo", repo, "name", name)

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

getResponse, err := plugin.RetryHydrate(ctx, d, h, getDetails, &plugin.RetryConfig{ShouldRetryError: shouldRetryError})
if err != nil {
return nil, err
return detail, err
}

getResp := getResponse.(GetResponse)

return getResp.secret, nil
return getGitHubItem(ctx, d, h, getDetails)
}
40 changes: 12 additions & 28 deletions github/table_github_actions_repository_workflow_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,38 +156,22 @@ func tableGitHubRepoWorkflowRunList(ctx context.Context, d *plugin.QueryData, h
//// HYDRATE FUNCTIONS

func tableGitHubRepoWorkflowRunGet(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
runId := d.KeyColumnQuals["id"].GetInt64Value()
orgName := d.KeyColumnQuals["repository_full_name"].GetStringValue()

// Empty check for the parameters
if runId == 0 || orgName == "" {
return nil, nil
}
getDetails := func(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData, client *github.Client) (interface{}, error) {
runId := d.KeyColumnQuals["id"].GetInt64Value()
fullname := d.KeyColumnQuals["repository_full_name"].GetStringValue()

owner, repo := parseRepoFullName(orgName)
plugin.Logger(ctx).Trace("tableGitHubRepoWorkflowRunGet", "owner", owner, "repo", repo, "runId", runId)

client := connect(ctx, d)
// Empty check for the parameters
if runId == 0 || fullname == "" {
return nil, nil
}

type GetResponse struct {
workflowRun *github.WorkflowRun
resp *github.Response
}
owner, repo := parseRepoFullName(fullname)
plugin.Logger(ctx).Trace("tableGitHubRepoWorkflowRunGet", "owner", owner, "repo", repo, "runId", runId)

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
}
detail, _, err := client.Actions.GetWorkflowRunByID(ctx, owner, repo, runId)

getResponse, err := plugin.RetryHydrate(ctx, d, h, getDetails, &plugin.RetryConfig{ShouldRetryError: shouldRetryError})
if err != nil {
return nil, err
return detail, err
}

getResp := getResponse.(GetResponse)

return getResp.workflowRun, nil
return getGitHubItem(ctx, d, h, getDetails)
}
54 changes: 15 additions & 39 deletions github/table_github_branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,61 +45,37 @@ func tableGitHubBranchProtection(ctx context.Context) *plugin.Table {
//// LIST FUNCTION

func tableGitHubRepositoryBranchProtectionGet(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
logger := plugin.Logger(ctx)
quals := d.KeyColumnQuals

fullName := quals["repository_full_name"].GetStringValue()
owner, repo := parseRepoFullName(fullName)
getDetails := func(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData, client *github.Client) (interface{}, error) {
fullName := d.KeyColumnQuals["repository_full_name"].GetStringValue()
owner, repo := parseRepoFullName(fullName)

branchName := ""

if h.Item != nil {
b := h.Item.(*github.Branch)
branchName = *b.Name
} else {
branchName = quals["name"].GetStringValue()
}
branchName := ""

logger.Trace("tableGitHubRepositoryBranchProtectionGet", "owner", owner, "repo", repo, "branchName", branchName)

client := connect(ctx, d)
if h.Item != nil {
b := h.Item.(*github.Branch)
branchName = *b.Name
} else {
branchName = d.KeyColumnQuals["name"].GetStringValue()
}

type GetResponse struct {
protection *github.Protection
}
plugin.Logger(ctx).Trace("tableGitHubRepositoryBranchProtectionGet", "owner", owner, "repo", repo, "branchName", branchName)
detail, _, err := client.Repositories.GetBranchProtection(ctx, owner, repo, branchName)

get := func(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
protection, _, err := client.Repositories.GetBranchProtection(ctx, owner, repo, branchName)
if err != nil {
// For private and archived repositories, users who do not have owner/admin access will get the below error
// 403 Upgrade to GitHub Pro or make this repository public to enable this feature.
// For repository owners the API will return nil if the repository is private and archived
if strings.Contains(err.Error(), "404") || strings.Contains(err.Error(), "Upgrade to GitHub Pro") || strings.Contains(err.Error(), "branch is not protected") {
return nil, nil
}

return nil, err
}

return GetResponse{
protection: protection,
}, err
}

getDetails, err := plugin.RetryHydrate(ctx, d, h, get, &plugin.RetryConfig{ShouldRetryError: shouldRetryError})
if err != nil {
return nil, err
return detail, err
}

if getDetails == nil {
return nil, nil
}
getResp := getDetails.(GetResponse)
protection := getResp.protection

if protection != nil {
d.StreamLeafListItem(ctx, protection)
}
return nil, nil
return streamGitHubListOrItem(ctx, d, h, getDetails)
}

func branchNameQual(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
Expand Down
65 changes: 23 additions & 42 deletions github/table_github_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,54 +150,35 @@ func tableGitHubCommitList(ctx context.Context, d *plugin.QueryData, h *plugin.H
//// HYDRATE FUNCTIONS

func tableGitHubCommitGet(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
var owner, repo string
var sha string
getDetails := func(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData, client *github.Client) (interface{}, error) {
var owner, repo string
var sha string

logger := plugin.Logger(ctx)
quals := d.KeyColumnQuals
quals := d.KeyColumnQuals

if h.Item != nil {
commit := h.Item.(*github.RepositoryCommit)
sha = *commit.SHA
} else {
sha = d.KeyColumnQuals["sha"].GetStringValue()
}
fullName := quals["repository_full_name"].GetStringValue()

// Return nil, if no input provided
if fullName == "" || sha == "" {
return nil, nil
}

owner, repo = parseRepoFullName(fullName)
logger.Trace("tableGitHubCommitGet", "owner", owner, "repo", repo, "sha", sha)

client := connect(ctx, d)
if h.Item != nil {
commit := h.Item.(*github.RepositoryCommit)
sha = *commit.SHA
} else {
sha = d.KeyColumnQuals["sha"].GetStringValue()
}
fullName := quals["repository_full_name"].GetStringValue()

opts := &github.ListOptions{
PerPage: 100,
}
// Return nil, if no input provided
if fullName == "" || sha == "" {
return nil, nil
}

type GetResponse struct {
commit *github.RepositoryCommit
resp *github.Response
}
opts := &github.ListOptions{
PerPage: 100,
}

getDetails := func(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
detail, resp, err := client.Repositories.GetCommit(ctx, owner, repo, sha, opts)
return GetResponse{
commit: detail,
resp: resp,
}, err
}
owner, repo = parseRepoFullName(fullName)
plugin.Logger(ctx).Trace("tableGitHubCommitGet", "owner", owner, "repo", repo, "sha", sha)
detail, _, err := client.Repositories.GetCommit(ctx, owner, repo, sha, opts)

getResponse, err := plugin.RetryHydrate(ctx, d, h, getDetails, &plugin.RetryConfig{ShouldRetryError: shouldRetryError})
if err != nil {
return nil, err
return detail, err
}

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

return commit, nil
return getGitHubItem(ctx, d, h, getDetails)
}
Loading