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

GitHub tree #133

Merged
merged 3 commits into from
Sep 19, 2022
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
3 changes: 2 additions & 1 deletion github/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ func Plugin(ctx context.Context) *plugin.Plugin {
"github_team": tableGitHubTeam(),
"github_traffic_view_daily": tableGitHubTrafficViewDaily(ctx),
"github_traffic_view_weekly": tableGitHubTrafficViewWeekly(ctx),
"github_tree": tableGitHubTree(ctx),
"github_user": tableGitHubUser(),
"github_workflow": tableGitHubWorkflow(ctx),
},
}
return p
}
}
87 changes: 87 additions & 0 deletions github/table_github_tree.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package github

import (
"context"

"github.com/google/go-github/v33/github"

"github.com/turbot/steampipe-plugin-sdk/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/plugin"
"github.com/turbot/steampipe-plugin-sdk/plugin/transform"
)

//// TABLE DEFINITION

func tableGitHubTree(ctx context.Context) *plugin.Table {
return &plugin.Table{
Name: "github_tree",
Description: "Tree in the given repository, lists files in the git tree",
Get: &plugin.GetConfig{
Copy link
Contributor

@bigdatasourav bigdatasourav Apr 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please replace get with the list as below -

List: &plugin.ListConfig{
			Hydrate:           tableGitHubTreeGet,
			ShouldIgnoreError: isNotFoundError([]string{"404"}),
			KeyColumns: []*plugin.KeyColumn{
				{Name: "repository_full_name", Require: plugin.Required},
				{Name: "sha", Require: plugin.Required},
				{Name: "recursive", Require: plugin.Optional},
			},
		},

KeyColumns: []*plugin.KeyColumn{
{Name: "repository_full_name", Require: plugin.Required},
{Name: "tree_sha", Require: plugin.Required},
{Name: "recursive", Require: plugin.Optional},
},
ShouldIgnoreError: isNotFoundError([]string{"404"}),
Hydrate: tableGitHubTreeGet,
},
Columns: []*plugin.Column{
// Top columns
{Name: "repository_full_name", Type: proto.ColumnType_STRING, Transform: transform.FromQual("repository_full_name"), Description: "Full name of the repository"},
{Name: "tree_sha", Type: proto.ColumnType_STRING, Transform: transform.FromQual("tree_sha"), Description: "Tree SHA"},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{Name: "tree_sha", Type: proto.ColumnType_STRING, Transform: transform.FromQual("tree_sha"), Description: "Tree SHA"},
{Name: "sha", Type: proto.ColumnType_STRING, Transform: transform.FromQual("sha"), Description: "Tree SHA"},

{Name: "recursive", Type: proto.ColumnType_BOOL, Transform: transform.FromQual("recursive"), Description: "Recursive tree content"},
{Name: "truncated", Type: proto.ColumnType_BOOL, Transform: transform.FromField("Truncated"), Description: "Whether results are truncated"},
{Name: "entries", Type: proto.ColumnType_JSON, Transform: transform.FromField("Entries"), Description: "Tree entries"},
},
}
}

//// GET FUNCTION

func tableGitHubTreeGet(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
logger := plugin.Logger(ctx)
logger.Trace("Connecting to client")
client := connect(ctx, d)

if h.Item != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this if-else block for hydrate data, as it is not required here.

item := h.Item
logger.Trace("Got hydrate data", item)
} else {
logger.Trace("No hydrate data")
}
quals := d.KeyColumnQuals
logger.Trace("Parsing key column quals", quals)
fullName := quals["repository_full_name"].GetStringValue()
sha := quals["tree_sha"].GetStringValue()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a check for empty values like below -

// check if fullName and sha is empty
	if fullName == "" || sha == "" {
		return nil, nil
	}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
sha := quals["tree_sha"].GetStringValue()
sha := quals["sha"].GetStringValue()

recursive := quals["recursive"].GetBoolValue()
owner, repo := parseRepoFullName(fullName)

type GetResponse struct {
tree *github.Tree
resp *github.Response
}

getTree := func(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
logger.Trace("Getting tree", "owner", owner, "repo", repo, "sha", sha)
tree, resp, err := client.Git.GetTree(ctx, owner, repo, sha, recursive)
return GetResponse{
tree: tree,
resp: resp,
}, err
}
getResponse, err := plugin.RetryHydrate(ctx, d, h, getTree, &plugin.RetryConfig{ShouldRetryError: shouldRetryError})

if err != nil {
logger.Error("Error getting tree", err)
return nil, err
}

getResp := getResponse.(GetResponse)
tree := getResp.tree
if tree != nil {
logger.Trace("Returning tree", tree)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
logger.Trace("Returning tree", tree)

return tree, nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return tree, nil
d.StreamListItem(ctx, tree)

}
logger.Error("Nothing found")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
logger.Error("Nothing found")

return nil, nil
}