Skip to content

Commit

Permalink
Add table github_stargazer closes #29
Browse files Browse the repository at this point in the history
  • Loading branch information
e-gineer committed May 15, 2021
1 parent bc4a26a commit 30f89ea
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
62 changes: 62 additions & 0 deletions docs/tables/github_stargazer.md
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;
```
68 changes: 68 additions & 0 deletions github/table_github_stargazer.go
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
}

0 comments on commit 30f89ea

Please sign in to comment.