Files
awesome-coding-agents/readme.go
T
tiennm99 62cbdd7a4a fix: harden GitHub fetcher and history I/O, add canonical keying
GitHub fetcher (github.go):
- add 30s HTTP client timeout (was http.DefaultClient with no bound)
- chunk GraphQL alias requests at 50 repos to stay clear of abuse detection
- abort the run on any partial GraphQL error or missing repo rather than
  silently shrinking the README and poisoning the next delta
- retry transient failures (network, 5xx, 429) with 2s/4s/8s backoff

History layer (history.go):
- key snapshots by canonical owner/repo from agents.yml instead of the
  rename-resolved NameWithOwner returned by the API; carry a lazy
  migration map so existing aaif-goose/goose entries fold into block/goose
  on next read with no manual data edit
- tighten the 7d delta window to (cutoff-3d, cutoff] so a missed cron week
  no longer mislabels a 90d-old comparison as Delta7d
- replace the snapshots[:0] aliased filter loop with slices.DeleteFunc
- log malformed JSONL lines to stderr with line numbers instead of
  silently skipping them
- write history.jsonl atomically via tmp file + rename so a crash
  mid-write can no longer truncate accumulated history

Plus collapse a few redundant fmt.Errorf wraps, drop a named Config type
that was used once, inline the single-call sortByStars helper with a
deterministic tiebreaker on canonical key, and use filepath.Base instead
of hand-rolling a basename.

Includes unit tests covering the 7d window edges, canonical-key
migration, atomic write path, malformed-line tolerance, YAML validation,
and markdown cell escaping.
2026-05-14 15:49:05 +07:00

94 lines
2.0 KiB
Go

package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"text/template"
"time"
)
type Row struct {
Rank int
NameWithOwner string
URL string
Stars int
Delta7d int
HasDelta bool
Language string
PushedAt string
Description string
Category string
}
func renderReadme(tmplPath, outPath string, stats []Stat, deltas map[string]int) error {
rows := make([]Row, len(stats))
for i, s := range stats {
// Deltas are keyed by CanonicalKey (owner/repo from agents.yml).
delta, has := deltas[s.CanonicalKey]
rows[i] = Row{
Rank: i + 1,
NameWithOwner: s.NameWithOwner,
URL: s.URL,
Stars: s.Stars,
Delta7d: delta,
HasDelta: has,
Language: s.Language,
PushedAt: s.PushedAt.Format("2006-01-02"),
Description: sanitizeCell(s.Description),
Category: s.Category,
}
}
funcs := template.FuncMap{
"formatDelta": func(d int, has bool) string {
if !has {
return "—"
}
if d > 0 {
return fmt.Sprintf("+%d", d)
}
if d < 0 {
return fmt.Sprintf("%d", d)
}
return "0"
},
"formatStars": func(n int) string {
switch {
case n < 1000:
return fmt.Sprintf("%d", n)
case n < 1_000_000:
return fmt.Sprintf("%.1fk", float64(n)/1000)
default:
return fmt.Sprintf("%.1fM", float64(n)/1_000_000)
}
},
}
tmpl, err := template.New("").Funcs(funcs).ParseFiles(tmplPath)
if err != nil {
return err
}
f, err := os.Create(outPath)
if err != nil {
return err
}
defer f.Close()
return tmpl.ExecuteTemplate(f, filepath.Base(tmplPath), map[string]any{
"Rows": rows,
"UpdatedAt": time.Now().UTC().Format("2006-01-02 15:04 UTC"),
"Total": len(rows),
})
}
// sanitizeCell escapes pipe and newline characters so descriptions stay in one table cell.
func sanitizeCell(s string) string {
s = strings.ReplaceAll(s, "|", "\\|")
s = strings.ReplaceAll(s, "\n", " ")
s = strings.ReplaceAll(s, "\r", " ")
return strings.TrimSpace(s)
}