diff --git a/CLAUDE.md b/CLAUDE.md index bc44eab..6b7d8f6 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -38,7 +38,7 @@ depending on which TTL "wins". see, or with Dependabot disabled, shouldn't blank out every other repo's data) — but propagates a failure of `Repos` itself, since nothing else can proceed without it. -- `internal/adoption` — opt-in (`ADOPTION_REPOS`) third domain on its own Fetcher (default 1h): stars/forks/watchers, release asset `download_count`, and the 14-day traffic API as per-UTC-day series so Prometheus keeps the history GitHub drops. A 403 on traffic (token lacks Administration read) only sets `github_repo_traffic_ok` 0; it never fails the poll. Registered separately (`collector.Adoption`). +- `internal/adoption` — opt-in (`ADOPTION_REPOS`, names and/or `public` = every public non-archived non-fork org repo, expanded on each poll from `Client.Repos`, which does not paginate past 100) third domain on its own Fetcher (default 1h): stars/forks/watchers, release asset `download_count`, and the 14-day traffic API as per-UTC-day series so Prometheus keeps the history GitHub drops. A 403 on traffic (token lacks Administration read) only sets `github_repo_traffic_ok` 0; it never fails the poll. Registered separately (`collector.Adoption`). - `internal/collector` — adapts the runner and org `Summary` types into Prometheus metrics for `/metrics`, on one shared `prometheus.Collector`. - `internal/config` — env var parsing (see README's Configuration table). diff --git a/README.md b/README.md index 354b3bc..557c27d 100644 --- a/README.md +++ b/README.md @@ -106,7 +106,8 @@ sum by (conclusion) (increase(github_workflow_runs_total[1d])) ## Adoption stats Optional, off unless `ADOPTION_REPOS` is set (comma-separated repo names in -`GITHUB_ORG`). Polled on its own `ADOPTION_CACHE_TTL` (default 1h), since +`GITHUB_ORG`; the entry `public` stands for every public, non-archived, non-fork repo of the org, +looked up on each poll so new repos appear on their own, and mixes with names, e.g. `public,some-private-repo`). Polled on its own `ADOPTION_CACHE_TTL` (default 1h), since none of it moves fast. Everything is public GitHub data or your own repo traffic; nothing is collected from the tracked project's users. @@ -147,7 +148,7 @@ window it sees and alerts on the lowest. | `RUNNER_CACHE_MAX_STALE` | `5m` | | | `ORG_CACHE_TTL` | `5m` | | | `ORG_CACHE_MAX_STALE` | `30m` | | -| `ADOPTION_REPOS` | | no; enables [adoption stats](#adoption-stats) | +| `ADOPTION_REPOS` | | no; enables [adoption stats](#adoption-stats). Repo names, and/or `public` for all public repos | | `ADOPTION_CACHE_TTL` | `1h` | | | `ADOPTION_CACHE_MAX_STALE` | `3h` | | diff --git a/internal/adoption/adoption.go b/internal/adoption/adoption.go index 17ab189..510495c 100644 --- a/internal/adoption/adoption.go +++ b/internal/adoption/adoption.go @@ -5,6 +5,8 @@ package adoption import ( "context" + "sort" + "strings" "time" "github.com/drumandbytes/github-actions-runner-exporter/internal/github" @@ -50,6 +52,7 @@ type Summary struct { // Client is the subset of *github.Client this package calls; tests fake it. type Client interface { + Repos(ctx context.Context) ([]github.Repo, error) RepoInfo(ctx context.Context, repo string) (github.RepoInfo, error) OpenPRCount(ctx context.Context, repo string) (int, error) Releases(ctx context.Context, repo string) ([]github.Release, error) @@ -59,10 +62,51 @@ type Client interface { TrafficPaths(ctx context.Context, repo string) ([]github.PopularPath, error) } +// AllPublic, as an entry in the repo list, stands for every public, non-archived, non-fork repo of +// the org, looked up on each poll so new repos appear without a config change. +const AllPublic = "public" + +// resolve expands AllPublic and drops duplicates; other entries are repo names. +func resolve(ctx context.Context, client Client, repos []string) ([]string, error) { + seen := map[string]bool{} + var out []string + add := func(name string) { + if !seen[name] { + seen[name] = true + out = append(out, name) + } + } + for _, name := range repos { + if !strings.EqualFold(name, AllPublic) { + add(name) + continue + } + all, err := client.Repos(ctx) + if err != nil { + return nil, err + } + var public []string + for _, r := range all { + if !r.Private && !r.Archived && !r.Fork { + public = append(public, r.Name) + } + } + sort.Strings(public) + for _, n := range public { + add(n) + } + } + return out, nil +} + // Build fails only when a repo's basic info or releases can't be read; a // missing traffic permission just leaves TrafficOK false. func Build(ctx context.Context, client Client, repos []string) (Summary, error) { s := Summary{GeneratedAt: time.Now()} + repos, err := resolve(ctx, client, repos) + if err != nil { + return Summary{}, err + } for _, name := range repos { info, err := client.RepoInfo(ctx, name) if err != nil { diff --git a/internal/adoption/adoption_test.go b/internal/adoption/adoption_test.go index 4968f1a..553d8d9 100644 --- a/internal/adoption/adoption_test.go +++ b/internal/adoption/adoption_test.go @@ -3,13 +3,19 @@ package adoption import ( "context" "errors" + "reflect" "testing" "time" "github.com/drumandbytes/github-actions-runner-exporter/internal/github" ) -type fake struct{ trafficErr error } +type fake struct { + trafficErr error + repos []github.Repo +} + +func (f fake) Repos(context.Context) ([]github.Repo, error) { return f.repos, nil } func (fake) RepoInfo(context.Context, string) (github.RepoInfo, error) { return github.RepoInfo{Stars: 5, Forks: 1, Watchers: 2, OpenIssuesAndPRs: 4}, nil @@ -56,3 +62,33 @@ func TestBuildTrafficForbidden(t *testing.T) { t.Fatalf("%+v", s.Repos[0]) } } + +func TestAllPublicExpandsToPublicNonArchivedNonForkRepos(t *testing.T) { + f := fake{repos: []github.Repo{ + {Name: "zeta"}, + {Name: "alpha"}, + {Name: "secret", Private: true}, + {Name: "old", Archived: true}, + {Name: "someone-elses", Fork: true}, + }} + got, err := resolve(context.Background(), f, []string{"public"}) + if err != nil || !reflect.DeepEqual(got, []string{"alpha", "zeta"}) { + t.Fatalf("got %v, %v", got, err) + } +} + +func TestAllPublicMixesWithNamesWithoutDuplicates(t *testing.T) { + f := fake{repos: []github.Repo{{Name: "alpha"}, {Name: "delistd"}}} + got, err := resolve(context.Background(), f, []string{"delistd", "PUBLIC"}) + if err != nil || !reflect.DeepEqual(got, []string{"delistd", "alpha"}) { + t.Fatalf("got %v, %v", got, err) + } +} + +func TestBuildWithAllPublicCoversEachRepo(t *testing.T) { + f := fake{repos: []github.Repo{{Name: "a"}, {Name: "b"}}} + s, err := Build(context.Background(), f, []string{"public"}) + if err != nil || len(s.Repos) != 2 || s.Repos[1].Name != "b" { + t.Fatalf("%+v, %v", s.Repos, err) + } +} diff --git a/internal/github/client.go b/internal/github/client.go index 0749fe3..93fc843 100644 --- a/internal/github/client.go +++ b/internal/github/client.go @@ -116,7 +116,7 @@ func (c *Client) Runners(ctx context.Context) ([]Runner, error) { return out.Runners, nil } -// Repos returns the org's non-fork repos. No pagination; add it past 100. +// Repos returns the org's repos, forks included (see Repo.Fork). No pagination; add it past 100. func (c *Client) Repos(ctx context.Context) ([]Repo, error) { var out []Repo url := fmt.Sprintf("%s/orgs/%s/repos?per_page=100&type=all", c.baseURL, c.org) diff --git a/internal/github/types.go b/internal/github/types.go index 86f161e..7231b0a 100644 --- a/internal/github/types.go +++ b/internal/github/types.go @@ -27,6 +27,7 @@ type Repo struct { Name string `json:"name"` Private bool `json:"private"` Archived bool `json:"archived"` + Fork bool `json:"fork"` } // Workflow is a repo's workflow definition (not a run of it).