-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add table github_stargazer closes #29
- Loading branch information
Showing
2 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Table: github_stargazer | ||
|
||
Stargazers are users who have starred the repository. | ||
|
||
The `github_stargazer` table can be used to query stargazers belonging to a repository, and **you must specify which repository** with `where repository_full_name='owner/repository'`. | ||
|
||
|
||
## Examples | ||
|
||
### List the stargazers of a repository | ||
|
||
```sql | ||
select | ||
user_login, | ||
starred_at | ||
from | ||
github_stargazer | ||
where | ||
repository_full_name = 'turbot/steampipe' | ||
order by | ||
starred_at desc; | ||
``` | ||
|
||
### New stargazers by month | ||
|
||
```sql | ||
select | ||
to_char(starred_at, 'YYYY-MM') as month, | ||
count(*) | ||
from | ||
github_stargazer | ||
where | ||
repository_full_name = 'turbot/steampipe' | ||
group by | ||
month | ||
order by | ||
month; | ||
``` | ||
|
||
### List stargazers with their contact information | ||
|
||
```sql | ||
select | ||
u.login, | ||
s.starred_at, | ||
u.name, | ||
u.company, | ||
u.email, | ||
u.html_url, | ||
u.twitter_username, | ||
u.blog, | ||
u.location, | ||
u.bio | ||
from | ||
github_stargazer as s, | ||
github_user as u | ||
where | ||
s.repository_full_name = 'turbot/steampipe' | ||
and s.user_login = u.login | ||
order by | ||
s.starred_at desc; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package github | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"time" | ||
|
||
"github.com/google/go-github/v33/github" | ||
"github.com/sethvargo/go-retry" | ||
|
||
"github.com/turbot/steampipe-plugin-sdk/grpc/proto" | ||
"github.com/turbot/steampipe-plugin-sdk/plugin" | ||
"github.com/turbot/steampipe-plugin-sdk/plugin/transform" | ||
) | ||
|
||
func tableGitHubStargazer(ctx context.Context) *plugin.Table { | ||
return &plugin.Table{ | ||
Name: "github_stargazer", | ||
Description: "Stargazers are users who have starred the repository.", | ||
List: &plugin.ListConfig{ | ||
KeyColumns: plugin.SingleColumn("repository_full_name"), | ||
Hydrate: tableGitHubStargazerList, | ||
}, | ||
Columns: []*plugin.Column{ | ||
// Top columns | ||
{Name: "repository_full_name", Type: proto.ColumnType_STRING, Hydrate: repositoryFullNameQual, Transform: transform.FromValue(), Description: "Full name of the repository that contains the stargazer."}, | ||
{Name: "starred_at", Type: proto.ColumnType_TIMESTAMP, Transform: transform.FromField("StarredAt").Transform(convertTimestamp), Description: "Time when the stargazer was created."}, | ||
{Name: "user_login", Type: proto.ColumnType_STRING, Transform: transform.FromField("User.Login"), Description: "The login name of the user who starred the repository."}, | ||
// No extra useful data over login - {Name: "user", Type: proto.ColumnType_JSON, Transform: transform.FromField("User"), Description: "Details of the user who starred the repository."}, | ||
}, | ||
} | ||
} | ||
|
||
func tableGitHubStargazerList(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { | ||
client := connect(ctx, d) | ||
fullName := d.KeyColumnQuals["repository_full_name"].GetStringValue() | ||
s := strings.Split(fullName, "/") | ||
owner := s[0] | ||
repo := s[1] | ||
opts := &github.ListOptions{PerPage: 100} | ||
for { | ||
var stargazers []*github.Stargazer | ||
var resp *github.Response | ||
b, err := retry.NewFibonacci(100 * time.Millisecond) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = retry.Do(ctx, retry.WithMaxRetries(10, b), func(ctx context.Context) error { | ||
var err error | ||
stargazers, resp, err = client.Activity.ListStargazers(ctx, owner, repo, opts) | ||
if _, ok := err.(*github.RateLimitError); ok { | ||
return retry.RetryableError(err) | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, i := range stargazers { | ||
d.StreamListItem(ctx, i) | ||
} | ||
if resp.NextPage == 0 { | ||
break | ||
} | ||
opts.Page = resp.NextPage | ||
} | ||
return nil, nil | ||
} |