Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`, 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/adoption` — opt-in (`ADOPTION_REPOS`, names and/or `public` = every public non-archived non-fork org repo, expanded on each poll from `Client.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/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).
Expand Down
51 changes: 33 additions & 18 deletions internal/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,34 +106,49 @@ func (c *Client) getPage(ctx context.Context, url string, out interface{}) (next
return "", nil
}

// Runners returns the org's self-hosted runners. No pagination; add it past 100.
// Runners returns the org's self-hosted runners, following pagination.
func (c *Client) Runners(ctx context.Context) ([]Runner, error) {
var out listRunnersResponse
url := fmt.Sprintf("%s/orgs/%s/actions/runners?per_page=100", c.baseURL, c.org)
if err := c.get(ctx, url, &out); err != nil {
return nil, err
next := fmt.Sprintf("%s/orgs/%s/actions/runners?per_page=100", c.baseURL, c.org)
var runners []Runner
for next != "" {
var out listRunnersResponse
var err error
if next, err = c.getPage(ctx, next, &out); err != nil {
return nil, err
}
runners = append(runners, out.Runners...)
}
return out.Runners, nil
return runners, nil
}

// Repos returns the org's repos, forks included (see Repo.Fork). No pagination; add it past 100.
// Repos returns the org's repos, forks included (see Repo.Fork), following pagination.
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)
if err := c.get(ctx, url, &out); err != nil {
return nil, err
next := fmt.Sprintf("%s/orgs/%s/repos?per_page=100&type=all", c.baseURL, c.org)
var repos []Repo
for next != "" {
var out []Repo
var err error
if next, err = c.getPage(ctx, next, &out); err != nil {
return nil, err
}
repos = append(repos, out...)
}
return out, nil
return repos, nil
}

// Workflows returns a repo's workflow definitions. No pagination; add it past 100.
// Workflows returns a repo's workflow definitions, following pagination.
func (c *Client) Workflows(ctx context.Context, repo string) ([]Workflow, error) {
var out listWorkflowsResponse
url := fmt.Sprintf("%s/repos/%s/%s/actions/workflows?per_page=100", c.baseURL, c.org, repo)
if err := c.get(ctx, url, &out); err != nil {
return nil, err
next := fmt.Sprintf("%s/repos/%s/%s/actions/workflows?per_page=100", c.baseURL, c.org, repo)
var workflows []Workflow
for next != "" {
var out listWorkflowsResponse
var err error
if next, err = c.getPage(ctx, next, &out); err != nil {
return nil, err
}
workflows = append(workflows, out.Workflows...)
}
return out.Workflows, nil
return workflows, nil
}

// LatestRunForWorkflow returns one workflow's most recent run, ok=false if it
Expand Down
45 changes: 45 additions & 0 deletions internal/github/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,48 @@ func TestReleasesPaginates(t *testing.T) {
t.Fatalf("releases = %+v", rels)
}
}

// pagedServer serves body1 on the first request and body2 on page=2, with a Link header between them.
func pagedServer(body1, body2 string) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("page") == "" {
w.Header().Set("Link", fmt.Sprintf(`<http://%s%s?page=2>; rel="next"`, r.Host, r.URL.Path))
_, _ = w.Write([]byte(body1))
return
}
_, _ = w.Write([]byte(body2))
}))
}

func TestReposPaginates(t *testing.T) {
srv := pagedServer(`[{"name":"a"}]`, `[{"name":"b","fork":true}]`)
defer srv.Close()
c := NewClient("org", "token", time.Second)
c.baseURL = srv.URL
repos, err := c.Repos(context.Background())
if err != nil || len(repos) != 2 || repos[1].Name != "b" || !repos[1].Fork {
t.Fatalf("repos = %+v, %v", repos, err)
}
}

func TestRunnersPaginates(t *testing.T) {
srv := pagedServer(`{"total_count":2,"runners":[{"name":"r1"}]}`, `{"total_count":2,"runners":[{"name":"r2"}]}`)
defer srv.Close()
c := NewClient("org", "token", time.Second)
c.baseURL = srv.URL
runners, err := c.Runners(context.Background())
if err != nil || len(runners) != 2 || runners[1].Name != "r2" {
t.Fatalf("runners = %+v, %v", runners, err)
}
}

func TestWorkflowsPaginates(t *testing.T) {
srv := pagedServer(`{"total_count":2,"workflows":[{"id":1,"name":"ci"}]}`, `{"total_count":2,"workflows":[{"id":2,"name":"deploy"}]}`)
defer srv.Close()
c := NewClient("org", "token", time.Second)
c.baseURL = srv.URL
wfs, err := c.Workflows(context.Background(), "repo")
if err != nil || len(wfs) != 2 || wfs[1].ID != 2 {
t.Fatalf("workflows = %+v, %v", wfs, err)
}
}