Skip to content

Commit

Permalink
Update github_stargazer table
Browse files Browse the repository at this point in the history
  • Loading branch information
bigdatasourav committed Nov 3, 2023
1 parent 036bf28 commit a5e2627
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
47 changes: 47 additions & 0 deletions github/stargazer_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package github

import (
"context"
"fmt"
"slices"

"github.com/shurcooL/githubv4"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
)

func extractStargazerFromHydrateItem(h *plugin.HydrateData) (Stargazer, error) {
if str, ok := h.Item.(Stargazer); ok {
return str, nil
} else {
return Stargazer{}, fmt.Errorf("unable to parse hydrate item %v as a Stargazer", h.Item)
}
}

func appendStargazerColumnIncludes(m *map[string]interface{}, cols []string) {
(*m)["includeStargazerStarredAt"] = githubv4.Boolean(slices.Contains(cols, "starred_at"))
(*m)["includeStargazerNode"] = githubv4.Boolean(slices.Contains(cols, "user_login") || slices.Contains(cols, "user_detail"))
}

func strHydrateStarredAt(_ context.Context, _ *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
str, err := extractStargazerFromHydrateItem(h)
if err != nil {
return nil, err
}
return str.StarredAt, nil
}

func strHydrateUserLogin(_ context.Context, _ *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
str, err := extractStargazerFromHydrateItem(h)
if err != nil {
return nil, err
}
return str.Node.Login, nil
}

func strHydrateUser(_ context.Context, _ *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
str, err := extractStargazerFromHydrateItem(h)
if err != nil {
return nil, err
}
return str.Node, nil
}
18 changes: 12 additions & 6 deletions github/table_github_stargazer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package github

import (
"context"

"github.com/shurcooL/githubv4"
"github.com/turbot/steampipe-plugin-github/github/models"

Expand All @@ -21,13 +22,18 @@ func tableGitHubStargazer() *plugin.Table {
},
Columns: []*plugin.Column{
{Name: "repository_full_name", Type: proto.ColumnType_STRING, Transform: transform.FromQual("repository_full_name"), 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("Node.Login"), Description: "The login name of the user who starred the repository."},
{Name: "user_detail", Type: proto.ColumnType_JSON, Transform: transform.FromField("Node"), Description: "Details of the user who starred the repository."},
{Name: "starred_at", Type: proto.ColumnType_TIMESTAMP, Transform: transform.FromValue().Transform(convertTimestamp), Hydrate: strHydrateStarredAt, Description: "Time when the stargazer was created."},
{Name: "user_login", Type: proto.ColumnType_STRING, Transform: transform.FromValue(), Hydrate: strHydrateUserLogin, Description: "The login name of the user who starred the repository."},
{Name: "user_detail", Type: proto.ColumnType_JSON, Transform: transform.FromValue(), Hydrate: strHydrateUser, Description: "Details of the user who starred the repository."},
},
}
}

type Stargazer struct {
StarredAt models.NullableTime `graphql:"starredAt @include(if:$includeStargazerStarredAt)" json:"starred_at"`
Node models.BasicUser `graphql:"node @include(if:$includeStargazerNode)" json:"ndoe"`
}

func tableGitHubStargazerList(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
fullName := d.EqualsQuals["repository_full_name"].GetStringValue()
owner, repo := parseRepoFullName(fullName)
Expand All @@ -39,8 +45,7 @@ func tableGitHubStargazerList(ctx context.Context, d *plugin.QueryData, h *plugi
TotalCount int
PageInfo models.PageInfo
Edges []struct {
StarredAt models.NullableTime
Node models.BasicUser
Stargazer
}
} `graphql:"stargazers(first: $pageSize, after: $cursor)"`
} `graphql:"repository(owner: $owner, name: $repo)"`
Expand All @@ -53,6 +58,7 @@ func tableGitHubStargazerList(ctx context.Context, d *plugin.QueryData, h *plugi
"pageSize": githubv4.Int(pageSize),
"cursor": (*githubv4.String)(nil),
}
appendStargazerColumnIncludes(&variables, d.QueryContext.Columns)

client := connectV4(ctx, d)
for {
Expand All @@ -64,7 +70,7 @@ func tableGitHubStargazerList(ctx context.Context, d *plugin.QueryData, h *plugi
}

for _, sg := range query.Repository.Stargazers.Edges {
d.StreamListItem(ctx, sg)
d.StreamListItem(ctx, Stargazer{sg.StarredAt, sg.Node})

// Context can be cancelled due to manual cancellation or the limit has been hit
if d.RowsRemaining(ctx) == 0 {
Expand Down

0 comments on commit a5e2627

Please sign in to comment.