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 docs/design/reconciler-backfill.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Checked with `gh` against this public repository on 2026-11-22: an item of `GET

With about 50 repositories, one run costs about 51 requests, so 4 runs an hour cost about 204 for one-page lists with no `304` and no repairs. That figure is a baseline: a truncated repository adds up to 49 more pages to an incremental walk and up to 199 more to the next sweep, and each repair candidate adds up to 3 event calls, so a bad hour costs more and the budget's floor stop at 500 remaining is what bounds it. The baseline is close to appendix E's 180 for the incremental line (`docs/architecture/appendix-e-budget-dedupe-and-retention.md:17`). A `304` with an `Authorization` header doesn't count against the primary limit (`docs/architecture/appendix-c-github-limit-evidence.md:39`), but it still takes a bucket token (`docs/architecture/appendix-e-budget-dedupe-and-retention.md:26`). Whether the token exchange counts against the installation's REST limit is unverified. The budget's floor stop at 500 remaining covers other use of the limit (`internal/budget/budget.go:136-149`).

Token plug-in: `main` builds one `*appauth.Source` and passes it as the `ghclient.TokenSource`. The client calls `Token` before each send and `Renew` once after a 401, and a second 401 returns a `*StatusError` (`internal/ghclient/ghclient.go:214-224`). A source failure surfaces as `ErrTokenSource` with no source text (`internal/ghclient/ghclient.go:263-280`), and the source logs its own stage and status (`internal/appauth/appauth.go:211-215`). A failed fetch clears the cache (`internal/appauth/appauth.go:217-219`), so the next call fetches again. Two callers that both see a 401 each call `Renew`, which serializes on the mutex and fetches twice (`internal/appauth/appauth.go:179-184`). That costs one extra exchange and needs no change.
Token plug-in: `main` builds one `*appauth.Source` and passes it as the `ghclient.TokenSource`. The client calls `Token` before each send and `Renew` once after a 401, and a second 401 returns a `*StatusError` (`internal/ghclient/ghclient.go:214-224`). A source failure surfaces as `ErrTokenSource` with no source text (`internal/ghclient/ghclient.go:263-280`), and the source logs its own stage and status (`internal/appauth/appauth.go:211-215`). If a refresh fails, `Token` returns the cached token while it is still valid and clears it once it expires. `Renew` reuses a valid token another caller fetched while it waited for the mutex. Two callers that both see a 401 usually share one exchange.

## 5. The idempotency key

Expand Down
5 changes: 4 additions & 1 deletion internal/appauth/appauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,16 @@ func (s *Source) fetch(ctx context.Context) (string, error) {
return s.failure(ctx, "sign", 0)
}

req, err := http.NewRequestWithContext(ctx, http.MethodPost, s.endpoint, nil)
const requestBody = `{"permissions":{"metadata":"read","pull_requests":"read"}}`

req, err := http.NewRequestWithContext(ctx, http.MethodPost, s.endpoint, strings.NewReader(requestBody))
if err != nil {
return s.failure(ctx, "request", 0)
}

req.Header.Set("Authorization", "Bearer "+assertion)
req.Header.Set("Accept", "application/vnd.github+json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Github-Api-Version", "2022-11-28")

res, err := s.http.Do(req)
Expand Down
40 changes: 38 additions & 2 deletions internal/appauth/appauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"encoding/json"
"encoding/pem"
"fmt"
"io"
"log/slog"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -102,11 +103,39 @@ func verifyRequest(t *testing.T, req *http.Request) {
}

if req.Header.Get("Accept") != "application/vnd.github+json" ||
req.Header.Get("Content-Type") != "application/json" ||
req.Header.Get("X-Github-Api-Version") != "2022-11-28" {
t.Error("missing API headers")
}
}

func verifyPermissions(t *testing.T, req *http.Request) {
t.Helper()

body, err := io.ReadAll(req.Body)
if err != nil {
t.Error("could not read the installation token request body")

return
}

var payload map[string]map[string]string

if json.Unmarshal(body, &payload) != nil {
t.Error("invalid installation token request body")

return
}

const readPermission = "read"

permissions := payload["permissions"]
if len(payload) != 1 || len(permissions) != 2 ||
permissions["metadata"] != readPermission || permissions["pull_requests"] != readPermission {
t.Error("expected only metadata and pull request read permissions")
}
}

func verifyAssertion(t *testing.T, req *http.Request, key *rsa.PrivateKey, now time.Time) {
t.Helper()

Expand Down Expand Up @@ -164,14 +193,21 @@ func TestTokenLifecycle(t *testing.T) {
var calls atomic.Int64

server := httptest.NewServer(http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
verifyPermissions(t, req)

count := calls.Add(1)

requests <- req

resp.WriteHeader(http.StatusCreated)

_, _ = fmt.Fprintf(resp, `{"token":"installation-%d","expires_at":%q}`,
count, now.Add(time.Hour).Format(time.RFC3339))
// The response grants fewer permissions than the request asks for.
_, _ = fmt.Fprintf(
resp,
`{"token":"installation-%d","expires_at":%q,"permissions":{"metadata":"read"}}`,
count,
now.Add(time.Hour).Format(time.RFC3339),
)
}))
t.Cleanup(server.Close)

Expand Down