Skip to content

Commit

Permalink
Add github_blob table (#430)
Browse files Browse the repository at this point in the history

Co-authored-by: Ved misra <[email protected]>
  • Loading branch information
rmhartog and misraved authored May 17, 2024
1 parent 20986e7 commit 1dc44f2
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 1 deletion.
58 changes: 58 additions & 0 deletions docs/tables/github_blob.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
title: "Steampipe Table: github_blob - Query GitHub blobs using SQL"
description: "Allows users to query GitHub Repositories, specifically the blob content."
---

# 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.

## 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';
```
3 changes: 2 additions & 1 deletion github/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down
72 changes: 72 additions & 0 deletions github/table_github_blob.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 1dc44f2

Please sign in to comment.