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

Add column security to table github_community_profile Closes #179 #180

Merged
merged 3 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 13 additions & 0 deletions docs/tables/github_community_profile.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,16 @@ from
where
repository_full_name = 'turbot/steampipe';
```

### List repositories having the security file

```sql
select
repository_full_name,
security ->> 'html_url' as security_file_url,
security ->> 'name' as security_file_name
from
github_community_profile c
join github_my_repository r on r.full_name = c.repository_full_name
where security is not null;
```
29 changes: 27 additions & 2 deletions github/table_github_community_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package github

import (
"context"
"strings"

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

Expand All @@ -17,8 +18,8 @@ func tableGitHubCommunityProfile(ctx context.Context) *plugin.Table {
Name: "github_community_profile",
Description: "Community profile information for the given repository.",
List: &plugin.ListConfig{
KeyColumns: plugin.SingleColumn("repository_full_name"),
Hydrate: tableGitHubCommunityProfileList,
KeyColumns: plugin.SingleColumn("repository_full_name"),
Hydrate: tableGitHubCommunityProfileList,
ShouldIgnoreError: isNotFoundError([]string{"404"}),
},
Columns: []*plugin.Column{
Expand All @@ -31,6 +32,7 @@ func tableGitHubCommunityProfile(ctx context.Context) *plugin.Table {
{Name: "pull_request_template", Type: proto.ColumnType_JSON, Transform: transform.FromField("Files.PullRequestTemplate"), Description: "Pull request template for the repository."},
{Name: "license", Type: proto.ColumnType_JSON, Transform: transform.FromField("Files.License"), Description: "License for the repository."},
{Name: "readme", Type: proto.ColumnType_JSON, Transform: transform.FromField("Files.Readme"), Description: "README for the repository."},
{Name: "security", Type: proto.ColumnType_JSON, Transform: transform.FromValue(), Description: "Security for the repository.", Hydrate: securityFileGet},
{Name: "updated_at", Type: proto.ColumnType_TIMESTAMP, Description: "Time when the community profile was last updated."},
},
}
Expand Down Expand Up @@ -69,3 +71,26 @@ func tableGitHubCommunityProfileList(ctx context.Context, d *plugin.QueryData, h
d.StreamListItem(ctx, result)
return nil, nil
}

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

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

optionalSecurityMDNames := []string{"SECURITY.md", "security.md", "Security.md"}

for _, optionalName := range optionalSecurityMDNames {
fileContent, _, _, err := client.Repositories.GetContents(ctx, owner, repo, optionalName, &github.RepositoryContentGetOptions{})

if fileContent != nil {
return fileContent, nil
} else if err != nil && strings.Contains(err.Error(), "Not Found") {
return nil, nil
} else if err != nil {
plugin.Logger(ctx).Error("github_community_profile.securityFileGet", "api_error", err)
return nil, err
}
}
return nil, nil
}