-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathrender_template.go
365 lines (304 loc) · 7.8 KB
/
render_template.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// This script generates README.md
// from README.md.tmpl, table.md.tmpl, and the data in data/.
// It fetches statistics about GitHub repositories from GitHub.
// For the script to work, the environment variable GITHUB_TOKEN must contain
// a valid personal access token (see https://github.com/settings/tokens).
//
// Copyright (c) D. Bohdan 2017-2019, 2020, 2023-2025.
// License: MIT.
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"html"
"io"
"log"
"net/http"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"text/template"
"time"
"github.com/BurntSushi/toml"
"github.com/yuin/goldmark"
)
type entry struct {
Name string `toml:"name"`
URL string `toml:"url"`
DB string `toml:"db"`
API string `toml:"api"`
Lang string `toml:"lang"`
License string `toml:"license"`
Stats string `toml:"stats"`
Notes string `toml:"notes"`
}
type tableRow struct {
Cells []string
}
type table struct {
Rows []tableRow
}
type repo struct {
Name string
Owner string
}
type projStats struct {
Name string
DefaultBranchRef struct {
Target struct {
AuthoredDate time.Time
History struct {
TotalCount int
}
}
}
Stargazers struct {
TotalCount int
}
}
type gitHubResponse struct {
Data map[string]projStats
Message string
Errors []map[string]interface{}
}
const (
readmeTemplateFile = "README.md.tmpl"
tableTemplateFile = "table.md.tmpl"
repoQuery = `r%d: repository(owner: "%s", name: "%s") { ...ProjStats }`
projStatsFragment = `
fragment ProjStats on Repository {
name
defaultBranchRef {
target {
... on Commit {
authoredDate
history {
totalCount
}
}
}
}
stargazers {
totalCount
}
}
`
)
const (
colProjectName = iota
colDatabase
colAPIType
colLanguage
colLicense
colStats
colNotes
colCount
)
var (
gitHubURL = regexp.MustCompile("https?://github.com/([a-zA-Z0-9-]+)/([a-zA-Z0-9_-]+)/?")
pTag = regexp.MustCompile(`(?s)^\s*<p>(.*?)</p>\s*$`)
safeRepoPattern = regexp.MustCompile(`^[a-zA-Z0-9_-]+$`)
)
// loadEntries loads entry data from TOML files that match the glob pattern.
func loadEntries(glob string) (entries []entry, err error) {
matches, err := filepath.Glob(glob)
if err != nil {
return nil, fmt.Errorf("loading entries from glob pattern: %w", err)
}
entries = make([]entry, len(matches))
for i, match := range matches {
buf, err := os.ReadFile(match)
if err != nil {
return nil, fmt.Errorf("reading file %s: %w", match, err)
}
err = toml.Unmarshal(buf, &entries[i])
if err != nil {
return nil, fmt.Errorf("parsing TOML file %s: %w", match, err)
}
}
return entries, nil
}
// entriesToTable converts a slice of entries to a representation of an HTML table.
func entriesToTable(entries []entry) *table {
rows := make([]tableRow, len(entries))
for i, ent := range entries {
cells := make([]string, colCount)
cells[colProjectName] = fmt.Sprintf("[%s](%s)",
html.EscapeString(ent.Name),
html.EscapeString(ent.URL))
cells[colDatabase] = html.EscapeString(ent.DB)
cells[colAPIType] = html.EscapeString(ent.API)
cells[colLanguage] = html.EscapeString(ent.Lang)
cells[colLicense] = html.EscapeString(ent.License)
// Deliberately allow HTML in stats and notes.
cells[colStats] = ent.Stats
cells[colNotes] = ent.Notes
rows[i] = tableRow{Cells: cells}
}
return &table{Rows: rows}
}
// trimP converts "<p>foo</p>" to "foo".
func trimP(html string) (res string) {
found := pTag.FindStringSubmatch(html)
if len(found) == 2 {
return found[1]
}
return html
}
// format converts the table to HTML using tmpl.
func (t *table) format(tmpl template.Template) (string, error) {
for i := range t.Rows {
for j, cell := range t.Rows[i].Cells {
var buf bytes.Buffer
if err := goldmark.Convert([]byte(cell), &buf); err != nil {
return "", fmt.Errorf("converting Markdown: %w", err)
}
t.Rows[i].Cells[j] = trimP(buf.String())
}
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, t); err != nil {
return "", fmt.Errorf("executing template: %w", err)
}
return buf.String(), nil
}
// queryGitHub perform a GraphQL query against the GitHub API v4.
func queryGitHub(token, query string) ([]byte, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
wrapper := map[string]string{"query": query}
jsonReq, err := json.Marshal(wrapper)
if err != nil {
return nil, fmt.Errorf("marshal query: %w", err)
}
req, err := http.NewRequestWithContext(
ctx,
http.MethodPost,
"https://api.github.com/graphql",
bytes.NewReader(jsonReq),
)
if err != nil {
return nil, fmt.Errorf("create request: %w", err)
}
req.Header.Set("Authorization", "bearer "+token)
req.Header.Set("Content-Type", "application/graphql")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("execute request: %w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("read response: %w", err)
}
return body, nil
}
// buildStatsQuery builds a GraphQL query
// to fetch the GitHub repository statistics for several repositories at once.
func buildStatsQuery(repos []repo) (string, error) {
var sb strings.Builder
sb.WriteString("{\n")
for i, r := range repos {
if r.Name == "" {
continue
}
if !safeRepoPattern.MatchString(r.Owner) || !safeRepoPattern.MatchString(r.Name) {
return "", fmt.Errorf("invalid repo name or owner %v", r)
}
fmt.Fprintf(&sb, repoQuery, i, r.Owner, r.Name)
sb.WriteString("\n")
}
sb.WriteString("}\n")
sb.WriteString(projStatsFragment)
return sb.String(), nil
}
// fetchGitHubStats retrieves the repository statistics
// (the number of stars and commits)
// for the repositories in repos and returns them formatted as HTML.
func fetchGitHubStats(token string, repos []repo) (statsHTML []string, err error) {
query, err := buildStatsQuery(repos)
if err != nil {
return nil, err
}
respBody, err := queryGitHub(token, query)
if err != nil {
return nil, err
}
var respData gitHubResponse
err = json.Unmarshal(respBody, &respData)
if err != nil {
return nil, err
}
if respData.Message != "" {
return nil, fmt.Errorf("GitHub API error: %s", respData.Message)
}
if len(respData.Errors) > 0 {
return nil, fmt.Errorf("GitHub GraphQL error: %s",
respData.Errors[0]["message"].(string))
}
statsHTML = make([]string, len(repos))
for k, v := range respData.Data {
i, err := strconv.Atoi(k[1:])
if err != nil {
return nil, err
}
latestCommitDate := v.DefaultBranchRef.Target.AuthoredDate.
Format(time.DateOnly)
statsHTML[i] = fmt.Sprintf(
"%d ★; %d commits, latest %s",
v.Stargazers.TotalCount,
v.DefaultBranchRef.Target.History.TotalCount,
latestCommitDate)
}
return statsHTML, nil
}
func main() {
readmeTmpl, err := template.ParseFiles(readmeTemplateFile)
if err != nil {
log.Fatal(err)
}
tableTmpl, err := template.ParseFiles(tableTemplateFile)
if err != nil {
log.Fatal(err)
}
entries, err := loadEntries("data/*.toml")
if err != nil {
log.Fatal(err)
}
repos := make([]repo, len(entries))
for i, ent := range entries {
found := gitHubURL.FindStringSubmatch(ent.URL)
if len(found) == 3 {
repos[i].Owner = found[1]
repos[i].Name = found[2]
} else {
entries[i].Stats = "n/a"
}
}
statsHTML, err := fetchGitHubStats(os.Getenv("GITHUB_TOKEN"), repos)
if err != nil {
log.Fatal(err)
}
for i, s := range statsHTML {
if s != "" {
entries[i].Stats = s
}
}
tbl := entriesToTable(entries)
tableHTML, err := tbl.format(*tableTmpl)
if err != nil {
log.Fatal(err)
}
data := map[string]interface{}{
"date": time.Now().Format(time.DateOnly),
"table": tableHTML,
}
err = readmeTmpl.Execute(os.Stdout, data)
if err != nil {
log.Fatal(err)
}
}