-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
222 lines (200 loc) · 5.14 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"bytes"
"context"
"errors"
"fmt"
"html/template"
"image"
"image/jpeg"
"image/png"
"io/ioutil"
"net/http"
"os"
"github.com/google/go-github/v39/github"
"github.com/kkyr/fig"
"github.com/otiai10/copy"
"github.com/po3rin/smartcircle"
"golang.org/x/image/draw"
"golang.org/x/oauth2"
)
type config struct {
OAuth string `fig:"oauth" validate:"required"`
Org string `fig:"org" default:"shopware"`
Repo string `fig:"repo" default:"platform"`
Template string `fig:"template" default:"template.gohtml"`
Excluded []string `fig:"excluded" default:"[]"`
AvatarSize int `fig:"avatarSize" default:"100"`
CropCircleAvatar bool `fig:"cropCircleAvatar" default:"false"`
}
type swagContributor struct {
Name string
ProfileURL string
AvatarURL string
}
type PageData struct {
Org string
Repo string
Contributors []swagContributor
}
var excludes map[string]struct{}
var cfg config
func main() {
err := fig.Load(&cfg,
fig.File("config.yaml"),
fig.Dirs("."),
)
if err != nil {
displayHelp()
fmt.Println(err.Error())
os.Exit(1)
}
excludes = make(map[string]struct{})
for _, login := range cfg.Excluded {
excludes[login] = struct{}{}
}
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: cfg.OAuth},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
opt := &github.ListContributorsOptions{
ListOptions: github.ListOptions{PerPage: 100},
}
var allContribs []*github.Contributor
for {
contribs, resp, err := client.Repositories.ListContributors(context.Background(), cfg.Org, cfg.Repo, opt)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
allContribs = append(allContribs, contribs...)
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
var scs []swagContributor
for _, contrib := range allContribs {
if _, ok := excludes[contrib.GetLogin()]; ok {
continue
}
scs = append(scs, swagContributor{
Name: contrib.GetLogin(),
ProfileURL: contrib.GetHTMLURL(),
AvatarURL: contrib.GetAvatarURL(),
})
}
fmt.Println("Number of contributors: ", len(allContribs))
fmt.Println("Number of contributors not excluded: ", len(scs))
fmt.Println("Downloading avatars")
_ = os.RemoveAll("imgCache")
err = os.MkdirAll("imgCache", 0755)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
for _, sc := range scs {
err := downloadFile(sc.AvatarURL, sc.Name)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}
fmt.Println("Building output")
tmpl := template.Must(template.ParseFiles(cfg.Template))
buf := new(bytes.Buffer)
err = tmpl.Execute(buf, PageData{
Org: cfg.Org,
Repo: cfg.Repo,
Contributors: scs,
})
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
_ = os.RemoveAll("output")
err = os.MkdirAll("output", 0755)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
err = ioutil.WriteFile(fmt.Sprintf("output/%s", cfg.Template), buf.Bytes(), 0666)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
err = copy.Copy("imgCache", "output/avatars")
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
_ = os.RemoveAll("imgCache")
fmt.Println("Done\nOutput is in folder output")
}
func downloadFile(url, name string) error {
response, err := http.Get(url)
if err != nil {
return err
}
defer response.Body.Close()
if response.StatusCode != 200 {
return errors.New(fmt.Sprintf("Received non 200 response code: %d", response.StatusCode))
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return errors.New(fmt.Sprintf("Could not read avatar for %s from %s", name, url))
}
cType := http.DetectContentType(body)
var srcImg image.Image
switch cType {
case "image/jpg":
srcImg, err = jpeg.Decode(bytes.NewReader(body))
if err != nil {
return err
}
case "image/jpeg":
srcImg, err = jpeg.Decode(bytes.NewReader(body))
if err != nil {
return err
}
case "image/png":
srcImg, err = png.Decode(bytes.NewReader(body))
if err != nil {
return err
}
}
rect := image.Rect(0, 0, cfg.AvatarSize, cfg.AvatarSize)
img := image.NewRGBA(rect)
draw.BiLinear.Scale(img, rect, srcImg, srcImg.Bounds(), draw.Over, nil)
if cfg.CropCircleAvatar {
c, err := smartcircle.NewCropper(smartcircle.Params{Src: img})
if err != nil {
return err
}
cImg, err := c.CropCircle()
if err != nil {
return err
}
buf := new(bytes.Buffer)
if err := png.Encode(buf, cImg); err != nil {
return err
}
return ioutil.WriteFile(fmt.Sprintf("imgCache/%s.png", name), buf.Bytes(), 0666)
}
buf := new(bytes.Buffer)
if err := jpeg.Encode(buf, img, &jpeg.Options{Quality: 90}); err != nil {
return err
}
return ioutil.WriteFile(fmt.Sprintf("imgCache/%s.jpg", name), buf.Bytes(), 0666)
}
func displayHelp() {
fmt.Print(`
Could not read your github oauth token.
Did you set it in config.yaml?
If you need to generate a token follow these instructions:
https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token
After that, rename config.yaml.dist to config.yaml and set the token in the file.
`)
}