From b76fe5557ff26a350feea237148f65dd645c8cdd Mon Sep 17 00:00:00 2001 From: Reinier Hartog Date: Tue, 30 Apr 2024 09:06:00 +0200 Subject: [PATCH 1/3] Add table github_blob --- docs/tables/github_blob.md | 58 ++++++++++++++++++++++++++++++ github/plugin.go | 3 +- github/table_github_blob.go | 72 +++++++++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 docs/tables/github_blob.md create mode 100644 github/table_github_blob.go diff --git a/docs/tables/github_blob.md b/docs/tables/github_blob.md new file mode 100644 index 0000000..6df93b1 --- /dev/null +++ b/docs/tables/github_blob.md @@ -0,0 +1,58 @@ +--- +title: "Steampipe Table: github_blob - Query GitHub Repositories using SQL" +description: "Allows users to query GitHub Repositories, specifically the blob content." +--- + +# Table: github_tree - Query GitHub Repositories using SQL + +GitHub Repositories are a fundamental resource in GitHub. They allow users to host and manage their codebase, track changes, and collaborate with other users. Each repository contains a tree structure that represents the file and directory hierarchy. + +## Table Usage Guide + +The `github_blob` table provides the contents of files within GitHub Repositories. As a developer or project manager, explore each repository's contents. Utilize it to uncover information about specific files in the repository, such as configuration files. + +**Important Notes** +- You must specify the `repository_full_name` and `blob_sha` columns in `where` or `join` clause to query the table. + +## Examples + +### List blob contents for a specific file +Explore the specific elements within the 'turbot/steampipe' repository by pinpointing specific files using a unique identifier. This helps understand the contents of the repository and makes it easy to access and analyze the file's content. + +```sql+postgres +select + tree_sha, + truncated, + path, + mode, + type, + sha, + decode(content, encoding) as content +from + github_tree t +left outer join + github_blob b on b.repository_full_name = t.repository_full_name and b.blob_sha = t.sha +where + t.repository_full_name = 'turbot/steampipe' + and tree_sha = '0f200416c44b8b85277d973bff933efa8ef7803a' + and path = 'Makefile'; +``` + +```sql+sqlite +select + tree_sha, + truncated, + path, + mode, + type, + sha, + decode(content, encoding) as content +from + github_tree t +left outer join + github_blob b on b.repository_full_name = t.repository_full_name and b.blob_sha = t.sha +where + t.repository_full_name = 'turbot/steampipe' + and tree_sha = '0f200416c44b8b85277d973bff933efa8ef7803a' + and path = 'Makefile'; +``` \ No newline at end of file diff --git a/github/plugin.go b/github/plugin.go index 1c8b911..7e32547 100644 --- a/github/plugin.go +++ b/github/plugin.go @@ -22,6 +22,7 @@ func Plugin(ctx context.Context) *plugin.Plugin { "github_actions_repository_secret": tableGitHubActionsRepositorySecret(), "github_actions_repository_workflow_run": tableGitHubActionsRepositoryWorkflowRun(), "github_audit_log": tableGitHubAuditLog(), + "github_blob": tableGitHubBlob(), "github_branch": tableGitHubBranch(), "github_branch_protection": tableGitHubBranchProtection(), "github_code_owner": tableGitHubCodeOwner(), @@ -42,7 +43,7 @@ func Plugin(ctx context.Context) *plugin.Plugin { "github_organization_dependabot_alert": tableGitHubOrganizationDependabotAlert(), "github_organization_external_identity": tableGitHubOrganizationExternalIdentity(), "github_organization_member": tableGitHubOrganizationMember(), - "github_organization_collaborator": tableGitHubOrganizationCollaborator(), + "github_organization_collaborator": tableGitHubOrganizationCollaborator(), "github_pull_request": tableGitHubPullRequest(), "github_pull_request_comment": tableGitHubPullRequestComment(), "github_pull_request_review": tableGitHubPullRequestReview(), diff --git a/github/table_github_blob.go b/github/table_github_blob.go new file mode 100644 index 0000000..71f0204 --- /dev/null +++ b/github/table_github_blob.go @@ -0,0 +1,72 @@ +package github + +import ( + "context" + + "github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto" + "github.com/turbot/steampipe-plugin-sdk/v5/plugin" + "github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform" +) + +func tableGitHubBlob() *plugin.Table { + return &plugin.Table{ + Name: "github_blob", + Description: "Gets a blob from a repository.", + List: &plugin.ListConfig{ + Hydrate: tableGitHubBlobList, + ShouldIgnoreError: isNotFoundError([]string{"404"}), + KeyColumns: []*plugin.KeyColumn{ + {Name: "repository_full_name", Require: plugin.Required}, + {Name: "blob_sha", Require: plugin.Required}, + }, + }, + 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 that contains the blob."}, + {Name: "blob_sha", Type: proto.ColumnType_STRING, Transform: transform.FromField("Sha"), Description: "SHA1 of the blob."}, + // Other columns + {Name: "node_id", Type: proto.ColumnType_STRING, Transform: transform.FromField("NodeID"), Description: "The node ID of the blob."}, + {Name: "url", Type: proto.ColumnType_STRING, Description: "URL of the blob."}, + {Name: "content", Type: proto.ColumnType_STRING, Description: "The encoded content of the blob."}, + {Name: "encoding", Type: proto.ColumnType_STRING, Description: "The encoding of the blob."}, + {Name: "size", Type: proto.ColumnType_INT, Description: "Size of the blob."}, + }, + } +} + +type blobRow struct { + Sha string + NodeID string + Url string + Content string + Encoding string + Size int +} + +func tableGitHubBlobList(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { + logger := plugin.Logger(ctx) + client := connect(ctx, d) + + quals := d.EqualsQuals + fullName := quals["repository_full_name"].GetStringValue() + sha := quals["blob_sha"].GetStringValue() + owner, repo := parseRepoFullName(fullName) + + blob, _, err := client.Git.GetBlob(ctx, owner, repo, sha) + if err != nil { + logger.Error("github_blob.tableGitHubBlobList", "api_error", err) + return nil, err + } + + if blob != nil { + d.StreamListItem(ctx, blobRow{ + Sha: *blob.SHA, + NodeID: *blob.NodeID, + Content: *blob.Content, + Encoding: *blob.Encoding, + Size: *blob.Size, + }) + } + + return nil, nil +} From 576970c437f6ffe1369c559a28ce0e29540c42e1 Mon Sep 17 00:00:00 2001 From: Reinier Hartog Date: Tue, 30 Apr 2024 10:40:34 +0200 Subject: [PATCH 2/3] Update docs/tables/github_blob.md Co-authored-by: Ved misra <47312748+misraved@users.noreply.github.com> --- docs/tables/github_blob.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/tables/github_blob.md b/docs/tables/github_blob.md index 6df93b1..0a37b06 100644 --- a/docs/tables/github_blob.md +++ b/docs/tables/github_blob.md @@ -3,7 +3,7 @@ title: "Steampipe Table: github_blob - Query GitHub Repositories using SQL" description: "Allows users to query GitHub Repositories, specifically the blob content." --- -# Table: github_tree - Query GitHub Repositories using SQL +# Table: github_blob - Query GitHub Repositories using SQL GitHub Repositories are a fundamental resource in GitHub. They allow users to host and manage their codebase, track changes, and collaborate with other users. Each repository contains a tree structure that represents the file and directory hierarchy. From 030548df1579db542261e25769ff9e2176510cc4 Mon Sep 17 00:00:00 2001 From: Reinier Hartog Date: Tue, 30 Apr 2024 10:40:40 +0200 Subject: [PATCH 3/3] Update docs/tables/github_blob.md Co-authored-by: Ved misra <47312748+misraved@users.noreply.github.com> --- docs/tables/github_blob.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/tables/github_blob.md b/docs/tables/github_blob.md index 0a37b06..76dbbbe 100644 --- a/docs/tables/github_blob.md +++ b/docs/tables/github_blob.md @@ -1,5 +1,5 @@ --- -title: "Steampipe Table: github_blob - Query GitHub Repositories using SQL" +title: "Steampipe Table: github_blob - Query GitHub blobs using SQL" description: "Allows users to query GitHub Repositories, specifically the blob content." ---