Skip to content

Commit

Permalink
Merge v2 branch (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
maolonglong authored Nov 17, 2021
1 parent fb80ef9 commit 4e5eabc
Show file tree
Hide file tree
Showing 15 changed files with 265 additions and 297 deletions.
15 changes: 0 additions & 15 deletions .github/workflows/release.yml

This file was deleted.

16 changes: 5 additions & 11 deletions Dockerfile
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"]
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

## 入参

| 参数 | 描述 | 是否必传 | 默认值 |
| :--------------: | :---------------------------: | :------: | :----------------------------------: |
| `github_token` | 用于提交时身份验证的 token || |
| `svg_path` | 星图的保存路径 || `STARCHARTS.svg` |
| `commit_message` | 提交信息 || `chore: update starcharts [skip ci]` |
| `stars_change` | 更新至少需要的 stars 数变化值 || `1` |
| 参数 | 描述 | 是否必传 | 默认值 |
| :--------------: | :---------------------------: | :------: | :------------------------------------: |
| `github_token` | 用于提交时身份验证的 token || |
| `svg_path` | 星图的保存路径 || `"STARCHARTS.svg"` |
| `commit_message` | 提交信息 || `"chore: update starcharts [skip ci]"` |
| `stars_change` | 更新至少需要的 stars 数变化值 || `"1"` |
| `repo` | 生成其他仓库的星图 || `""` |

## 示例

Expand All @@ -31,7 +32,11 @@ jobs:
- uses: MaoLongLong/actions-starcharts@main
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
svg_path: STARCHARTS.svg
# 自定义 token 减少被限流的可能
# github_token: ${{ secrets.GH_TOKEN }}
svg_path: images/starcharts.svg
# stars_change: "100"
# repo: "doocs/advanced-java"
```

## 效果
Expand All @@ -43,6 +48,6 @@ jobs:
## TODO

- [x] 修复由于 GitHub V3 API 分页限制,无法获取 40K stars 以上数据的问题
- [ ] 部分操作仍然依赖 GitHub API V3,打算全部替换为 V4
- [ ] 由于 Actions 中调用 V4 API 有 1000 的次数限制,所以它暂时只支持到 100K stars 的仓库
- [x] ~~部分操作仍然依赖 GitHub API V3,打算全部替换为 V4~~ 还是 v3 用的顺手点,所以暂时就 `getStargazers()` 用的 v4
- [x] ~~由于 Actions 中调用 V4 API 有 1000 的次数限制,所以它暂时只支持到 100K stars 的仓库~~[自定义 token](https://github.com/settings/tokens/new?scopes=repo&description=starcharts)解决
- [ ] 为了文明使用 GitHub API,暂时没有使用多 goroutine,所以生成速度较慢
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ inputs:
required: false
default: "1"

repo:
description: Generate starcharts for other repository
required: false
default: ""

runs:
using: docker
image: Dockerfile
9 changes: 4 additions & 5 deletions internal/chart/chart.go → chart.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package chart
package main

import (
"fmt"
"io"
"time"

"github.com/maolonglong/actions-starcharts/internal/client"
"github.com/wcharczuk/go-chart"
"github.com/wcharczuk/go-chart/drawing"
)

func WriteStarsChart(stars []client.Stargazer, w io.Writer) error {
func writeStarsChart(stars []stargazer, w io.Writer) error {
var series = chart.TimeSeries{
Style: chart.Style{
Show: true,
Expand Down Expand Up @@ -60,13 +59,13 @@ func WriteStarsChart(stars []client.Stargazer, w io.Writer) error {
A: 255,
},
},
ValueFormatter: IntValueFormatter,
ValueFormatter: intValueFormatter,
},
Series: []chart.Series{series},
}
return graph.Render(chart.SVG, w)
}

func IntValueFormatter(v interface{}) string {
func intValueFormatter(v interface{}) string {
return fmt.Sprintf("%.0f", v)
}
145 changes: 145 additions & 0 deletions client.go
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
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.17

require (
github.com/google/go-github/v39 v39.1.0
github.com/sethvargo/go-githubactions v0.5.0
github.com/shurcooL/githubv4 v0.0.0-20210922025249-6831e00d857f
github.com/spf13/cast v1.4.1
github.com/wcharczuk/go-chart v2.0.1+incompatible
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/sethvargo/go-githubactions v0.5.0 h1:leSeIPIMDJXHvRY2EOOEFRS9/aa7k0m4oxY7W4dig5g=
github.com/sethvargo/go-githubactions v0.5.0/go.mod h1:ugCoIFQjs7HxIwwYiY7ty6H9T+7Z4ey481HxqA3VRKE=
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4=
github.com/shopspring/decimal v0.0.0-20200227202807-02e2044944cc/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/shurcooL/githubv4 v0.0.0-20210922025249-6831e00d857f h1:q4b8/GCL8Ksl+okhFKSd8DVBQNc0XExjxTO68nK0c3Y=
Expand Down
19 changes: 0 additions & 19 deletions internal/action/action.go

This file was deleted.

44 changes: 0 additions & 44 deletions internal/client/blob.go

This file was deleted.

29 changes: 0 additions & 29 deletions internal/client/client.go

This file was deleted.

Loading

0 comments on commit 4e5eabc

Please sign in to comment.