-
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
fb80ef9
commit 4e5eabc
Showing
15 changed files
with
265 additions
and
297 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,16 +1,10 @@ | ||
FROM golang:1.17 as builder | ||
FROM golang:1.13 | ||
|
||
ENV GO111MODULE=on \ | ||
GOPROXY=https://proxy.golang.org \ | ||
CGO_ENABLED=0 | ||
|
||
WORKDIR /build | ||
WORKDIR /src | ||
COPY . . | ||
RUN go mod download | ||
RUN go build -ldflags="-w -s" -o starcharts | ||
|
||
FROM alpine:3.14 | ||
ENV GO111MODULE=on | ||
|
||
COPY --from=builder /build/starcharts /starcharts | ||
RUN go build -o /bin/action | ||
|
||
ENTRYPOINT [ "/starcharts" ] | ||
ENTRYPOINT ["/bin/action"] |
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,145 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/base64" | ||
"sort" | ||
"time" | ||
|
||
"github.com/google/go-github/v39/github" | ||
"github.com/sethvargo/go-githubactions" | ||
"github.com/shurcooL/githubv4" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
type client struct { | ||
v3 *github.Client | ||
v4 *githubv4.Client | ||
} | ||
|
||
func newClient(token string) *client { | ||
ctx := context.TODO() | ||
|
||
src := oauth2.StaticTokenSource( | ||
&oauth2.Token{AccessToken: token}, | ||
) | ||
httpClient := oauth2.NewClient(ctx, src) | ||
|
||
return &client{ | ||
v3: github.NewClient(httpClient), | ||
v4: githubv4.NewClient(httpClient), | ||
} | ||
} | ||
|
||
func (c *client) getStarsCount(ctx context.Context, owner, repo string) (int, error) { | ||
r, _, err := c.v3.Repositories.Get(ctx, owner, repo) | ||
if err != nil { | ||
return 0, err | ||
} | ||
if r.StargazersCount == nil { | ||
return 0, nil | ||
} | ||
return *r.StargazersCount, nil | ||
} | ||
|
||
func (c *client) getBlob(ctx context.Context, owner, repo, sha, path string) (*github.Blob, error) { | ||
tree, _, err := c.v3.Git.GetTree(ctx, owner, repo, sha, true) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, ent := range tree.Entries { | ||
if ent.Path != nil && *ent.Path == path { | ||
blob, _, err := c.v3.Git.GetBlob(ctx, owner, repo, ent.GetSHA()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return blob, nil | ||
} | ||
} | ||
return nil, nil | ||
} | ||
|
||
type stargazer struct { | ||
StarredAt time.Time | ||
} | ||
|
||
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 | ||
ResetAt time.Time | ||
} | ||
} | ||
|
||
func (c *client) getStargazers(ctx context.Context, owner, repo string) ([]stargazer, error) { | ||
var q getStarsQuery | ||
|
||
variables := map[string]interface{}{ | ||
"owner": githubv4.String(owner), | ||
"name": githubv4.String(repo), | ||
"after": (*githubv4.String)(nil), | ||
} | ||
|
||
first := true | ||
|
||
var stars []stargazer | ||
for { | ||
err := c.v4.Query(ctx, &q, variables) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if first { | ||
githubactions.Infof("ratelimit_remaining=%d reset_at=\"%s\"\n", | ||
q.RateLimit.Remaining, q.RateLimit.ResetAt.Format(time.RFC1123)) | ||
githubactions.Infof("get stargazers...\n") | ||
first = false | ||
} | ||
|
||
stars = append(stars, q.Repository.Stargazers.Edges...) | ||
if !q.Repository.Stargazers.PageInfo.HasNextPage { | ||
break | ||
} | ||
variables["after"] = githubv4.NewString(q.Repository.Stargazers.PageInfo.EndCursor) | ||
} | ||
|
||
githubactions.Infof("ratelimit_remaining=%d reset_at=\"%s\"\n", | ||
q.RateLimit.Remaining, q.RateLimit.ResetAt.Format(time.RFC1123)) | ||
|
||
sort.Slice(stars, func(i, j int) bool { | ||
return stars[i].StarredAt.Before(stars[j].StarredAt) | ||
}) | ||
return stars, nil | ||
} | ||
|
||
func (c *client) createOrUpdate(ctx context.Context, owner, repo, sha, path, message string, blob *github.Blob, content []byte) error { | ||
if blob != nil { | ||
preContent, err := base64.StdEncoding.DecodeString(*blob.Content) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if bytes.Equal(preContent, content) { | ||
return nil | ||
} | ||
} | ||
|
||
opt := &github.RepositoryContentFileOptions{ | ||
Message: &message, | ||
Content: content, | ||
} | ||
if blob != nil { | ||
opt.SHA = blob.SHA | ||
} | ||
_, _, err := c.v3.Repositories.CreateFile(ctx, owner, repo, path, opt) | ||
return err | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.