-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b60c1a
commit 88ba2bf
Showing
8 changed files
with
131 additions
and
85 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
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
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
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
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
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,64 @@ | ||
package client | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/shurcooL/githubv4" | ||
) | ||
|
||
type Stargazer struct { | ||
StarredAt time.Time | ||
} | ||
|
||
/* | ||
repository(name: $name, owner: $owner) { | ||
stargazers(first: 100, after: $after) { | ||
edges { | ||
starredAt | ||
} | ||
pageInfo { | ||
endCursor | ||
hasNextPage | ||
} | ||
} | ||
} | ||
*/ | ||
type getStarsQuery struct { | ||
Repository struct { | ||
Stargazers struct { | ||
Edges []Stargazer | ||
PageInfo struct { | ||
EndCursor githubv4.String | ||
HasNextPage bool | ||
} | ||
} `graphql:"stargazers(first: 100, after: $after)"` | ||
} `graphql:"repository(owner: $owner, name: $name)"` | ||
RateLimit struct { | ||
Remaining int | ||
} | ||
} | ||
|
||
type Blob struct { | ||
Oid string | ||
Text string | ||
} | ||
|
||
/* | ||
{ | ||
repository(owner: $owner, name: $name) { | ||
object(expression: $expression) { | ||
... on Blob { | ||
oid | ||
text | ||
} | ||
} | ||
} | ||
} | ||
*/ | ||
type getFileSHAQuery struct { | ||
Repository struct { | ||
Object struct { | ||
Blob `graphql:"... on Blob"` | ||
} `graphql:"object(expression: $expression)"` | ||
} `graphql:"repository(owner: $owner, name: $name)"` | ||
} |
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 |
---|---|---|
@@ -1,56 +1,37 @@ | ||
package client | ||
|
||
import ( | ||
"log" | ||
"sort" | ||
"sync" | ||
|
||
"github.com/google/go-github/v39/github" | ||
"golang.org/x/sync/errgroup" | ||
"github.com/shurcooL/githubv4" | ||
) | ||
|
||
func (c *Client) GetStargazers(owner, repo string) ([]*github.Stargazer, error) { | ||
r, _, err := c.g.Repositories.Get(c.ctx, owner, repo) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
stars := make([]*github.Stargazer, 0, r.GetStargazersCount()) | ||
var mu sync.Mutex | ||
var g errgroup.Group | ||
sem := make(chan struct{}, 4) | ||
func (c *Client) GetStargazers(owner, name string) ([]Stargazer, error) { | ||
var q getStarsQuery | ||
|
||
lastPage := getLastPage(r.GetStargazersCount(), defaultPerPage) | ||
for page := 1; page <= lastPage; page++ { | ||
sem <- struct{}{} | ||
page := page | ||
g.Go(func() error { | ||
defer func() { <-sem }() | ||
opt := &github.ListOptions{ | ||
Page: page, | ||
PerPage: defaultPerPage, | ||
} | ||
result, _, err := c.g.Activity.ListStargazers(c.ctx, owner, repo, opt) | ||
if err != nil { | ||
return err | ||
} | ||
mu.Lock() | ||
stars = append(stars, result...) | ||
mu.Unlock() | ||
return nil | ||
}) | ||
variables := map[string]interface{}{ | ||
"owner": githubv4.String(owner), | ||
"name": githubv4.String(name), | ||
"after": (*githubv4.String)(nil), | ||
} | ||
|
||
err = g.Wait() | ||
if err != nil { | ||
return nil, err | ||
var stars []Stargazer | ||
for { | ||
err := c.g.Query(c.ctx, &q, variables) | ||
if err != nil { | ||
return nil, err | ||
} | ||
stars = append(stars, q.Repository.Stargazers.Edges...) | ||
log.Printf("get stargazers: completed=%v, ratelimit_remaining=%v", len(stars), q.RateLimit.Remaining) | ||
if !q.Repository.Stargazers.PageInfo.HasNextPage { | ||
break | ||
} | ||
variables["after"] = githubv4.NewString(q.Repository.Stargazers.PageInfo.EndCursor) | ||
} | ||
|
||
sort.Slice(stars, func(i, j int) bool { | ||
return stars[i].StarredAt.Before(stars[j].StarredAt.Time) | ||
return stars[i].StarredAt.Before(stars[j].StarredAt) | ||
}) | ||
return stars, nil | ||
} | ||
|
||
func getLastPage(total, perPage int) int { | ||
return (total + perPage - 1) / perPage | ||
} |
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