Skip to content

Commit

Permalink
feat: add stars_change
Browse files Browse the repository at this point in the history
  • Loading branch information
maolonglong committed Sep 30, 2021
1 parent 1105cd1 commit fb80ef9
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 7 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

## 入参

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

## 示例

Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ inputs:
required: false
default: "chore: update starcharts [skip ci]"

stars_change:
description: Update only when the current stars has increased by at least stars_change since last time
required: false
default: "1"

runs:
using: docker
image: Dockerfile
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.17
require (
github.com/google/go-github/v39 v39.1.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
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f
)
Expand All @@ -15,6 +16,7 @@ require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/shurcooL/graphql v0.0.0-20200928012149-18c5c3165e3a // indirect
github.com/stretchr/testify v1.5.1 // indirect
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/image v0.0.0-20190802002840-cff245a6509b // indirect
golang.org/x/net v0.0.0-20210929193557-e81a3d93ecf6 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfc
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
Expand Down Expand Up @@ -274,6 +275,7 @@ github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTw
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
Expand Down Expand Up @@ -311,6 +313,8 @@ github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4k
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cast v1.4.1 h1:s0hze+J0196ZfEMTs80N7UlFt0BDuQ7Q+JDnHiMWKdA=
github.com/spf13/cast v1.4.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v1.1.1/go.mod h1:WnodtKOvamDL/PwE2M4iKs8aMDBZ5Q5klgD3qfVJQMI=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
Expand All @@ -322,6 +326,7 @@ github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoH
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tilinna/clock v1.0.2/go.mod h1:ZsP7BcY7sEEz7ktc0IVy8Us6boDrK8VradlKRUGfOao=
Expand Down Expand Up @@ -647,6 +652,7 @@ gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bl
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
4 changes: 2 additions & 2 deletions internal/client/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func (c *Client) CreateOrUpdate(owner, repo, sha, path, message string, content []byte) error {
b, err := c.getBlob(owner, repo, sha, path)
b, err := c.GetBlob(owner, repo, sha, path)
if err != nil {
return err
}
Expand All @@ -30,7 +30,7 @@ func (c *Client) CreateOrUpdate(owner, repo, sha, path, message string, content
return err
}

func (c *Client) getBlob(owner, repo, sha, path string) (Blob, error) {
func (c *Client) GetBlob(owner, repo, sha, path string) (Blob, error) {
var q getFileSHAQuery
err := c.g.Query(c.ctx, &q, map[string]interface{}{
"owner": githubv4.String(owner),
Expand Down
17 changes: 17 additions & 0 deletions internal/client/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,20 @@ type getFileSHAQuery struct {
} `graphql:"object(expression: $expression)"`
} `graphql:"repository(owner: $owner, name: $name)"`
}

/*
query GetStarTotal($name: String!, $owner: String!) {
repository(name: $name, owner: $owner) {
stargazers {
totalCount
}
}
}
*/
type getStarTotalQuery struct {
Repository struct {
Stargazers struct {
TotalCount int
}
} `graphql:"repository(owner: $owner, name: $name)"`
}
9 changes: 9 additions & 0 deletions internal/client/star.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ import (
"github.com/shurcooL/githubv4"
)

func (c *Client) GetStarTotal(owner, name string) (int, error) {
var q getStarTotalQuery
err := c.g.Query(c.ctx, &q, map[string]interface{}{
"owner": githubv4.String(owner),
"name": githubv4.String(name),
})
return q.Repository.Stargazers.TotalCount, err
}

func (c *Client) GetStargazers(owner, name string) ([]Stargazer, error) {
var q getStarsQuery

Expand Down
31 changes: 31 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package main
import (
"bytes"
"context"
"fmt"
"log"
"os"

"github.com/maolonglong/actions-starcharts/internal/action"
"github.com/maolonglong/actions-starcharts/internal/chart"
"github.com/maolonglong/actions-starcharts/internal/client"
"github.com/spf13/cast"
)

func main() {
Expand All @@ -17,13 +20,41 @@ func main() {
svgPath := action.GetInput("svg_path")
commitMessage := action.GetInput("commit_message")

starsChange := cast.ToInt(action.GetInput("stars_change"))
if starsChange < 1 {
starsChange = 1
}

client := client.New(context.Background(), token)

cur, err := client.GetStarTotal(owner, name)
if err != nil {
log.Fatal("get stars total count failed: ", err.Error())
}
if cur == 0 {
log.Println("not enough stars")
os.Exit(0)
}

b, err := client.GetBlob(owner, name, sha, svgPath)
if err != nil {
log.Fatal("get blob failed: ", err.Error())
}

var old int
fmt.Sscanf(b.Text, "<!-- stars: %d -->", &old)
log.Printf("old stars: %v, cur stars: %v", old, cur)
if abs(cur-old) < starsChange {
os.Exit(0)
}

stars, err := client.GetStargazers(owner, name)
if err != nil {
log.Fatal("get stargazers failed: ", err.Error())
}

buf := new(bytes.Buffer)
buf.WriteString(fmt.Sprintf("<!-- stars: %d -->\n", len(stars)))
err = chart.WriteStarsChart(stars, buf)
if err != nil {
log.Fatal("write stargazers chart failed: ", err.Error())
Expand Down
8 changes: 8 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

func abs(x int) int {
if x < 0 {
return -x
}
return x
}

0 comments on commit fb80ef9

Please sign in to comment.