-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
102 lines (85 loc) · 2.33 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"bytes"
"context"
"encoding/base64"
"fmt"
"os"
"strconv"
"strings"
"github.com/sethvargo/go-githubactions"
)
func main() {
ghCtx, err := githubactions.Context()
if err != nil {
githubactions.Fatalf("get github context failed")
}
sha := ghCtx.SHA
owner, name := getRepo()
token := githubactions.GetInput("github_token")
if token == "" {
githubactions.Fatalf("missing input 'github_token'")
}
svgPath := githubactions.GetInput("svg_path")
if svgPath == "" {
svgPath = "STARCHARTS.svg"
}
commitMessage := githubactions.GetInput("commit_message")
if commitMessage == "" {
commitMessage = "chore: update starcharts [skip ci]"
}
starsChange, err := strconv.Atoi(githubactions.GetInput("stars_change"))
if err != nil || starsChange < 1 {
starsChange = 1
}
targetOwner, targetName := owner, name
repo := githubactions.GetInput("repo")
if repo != "" {
a := strings.SplitN(repo, "/", 2)
if len(a) != 2 {
githubactions.Fatalf("invalid repo: %v", repo)
}
targetOwner, targetName = a[0], a[1]
}
ctx := context.TODO()
cli := newClient(token)
cur, err := cli.getStarsCount(ctx, targetOwner, targetName)
if err != nil {
githubactions.Fatalf("failed to get stars count: %v", err)
}
if cur == 0 {
githubactions.Warningf("not enough stars")
os.Exit(0)
}
b, err := cli.getBlob(ctx, owner, name, sha, svgPath)
if err != nil {
githubactions.Fatalf("failed to get blob: %v", err)
}
if b != nil {
preContent, err := base64.StdEncoding.DecodeString(*b.Content)
if err != nil {
githubactions.Fatalf("failed to decode base64 string: %v", err)
}
var old int
fmt.Sscanf(string(preContent), "<!-- stars: %d -->", &old)
githubactions.Infof("stars_old=%d stars_cur=%d", old, cur)
if abs(cur-old) < starsChange {
os.Exit(0)
}
}
stars, err := cli.getStargazers(ctx, targetOwner, targetName)
if err != nil {
githubactions.Fatalf("failed to get stargazers: %v", err)
}
buf := new(bytes.Buffer)
buf.WriteString(fmt.Sprintf("<!-- stars: %d -->\n", len(stars)))
err = writeStarsChart(stars, buf)
if err != nil {
githubactions.Fatalf("failed to write svg: %v", err)
}
err = cli.createOrUpdate(ctx, owner, name, sha, svgPath, commitMessage, b, buf.Bytes())
if err != nil {
githubactions.Fatalf("failed to update content: %v", err)
}
githubactions.Infof("update success!")
}