Skip to content
Open
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
4 changes: 2 additions & 2 deletions design/workstreams/provider-github/LLD/github-provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ Implemented in GH-S1-T04; the details are in [§Decisions taken in GH-S1-T04](#d
- **GraphQL by hand, no new dependency.** The queries are constants in `observe.go`, posted by the new `tokens.Minter.GraphQL(ctx, token, op, query, vars, out)` and decoded with `encoding/json`, rather than through `githubv4`: errors then map like every other call (HTTP errors as in T01; a response with `errors` is a `*tokens.GraphQLError`, `NOT_FOUND` → `ErrNotFound`, `FORBIDDEN` → `tokens.ErrForbidden` and `provider.ErrForbidden`, a permission problem callers can tell from a transient one, `RATE_LIMITED` → `*provider.RetryableError`), the recorded responses under `testdata/graphql` are plain JSON, and `githubtest` answers by operation name. **A response with errors fails even with partial data**: an observation missing a field could make a rule merge. The endpoint is `<api>/graphql`, or `https://<host>/api/graphql` for a GHE REST base `…/api/v3` (`tokens.GraphQLURL`). `tokens` also gained `Do` (any method with an installation token, optional body and decoding), `NextPage` (the Link `rel="next"` query), and 410 (a deleted issue) now maps to `ErrNotFound`.
- **`Observe(ref)`** is one `Observe` query on `repository.issueOrPullRequest(number)`, so a ref of the other kind (an issue ref naming a PR) is `ErrNotFound`, as the fake does, and an empty kind is filled in. Follow-up queries (`ObserveComments`, `ObserveThreads`, by node ID and cursor) read comments and review threads past the first 100, up to 10 more pages each (`ObserveComments` went with the markers' read-back: [§P3](#decisions-taken-with-p3-no-list-no-marker-read-back)). Labels (first 100), sub-issues (GitHub caps them at 100) and reviews (`last: 100`, not the LLD's 20, so an old approval isn't cut off by a chatty review) are one page.
- Issue: state, labels, author, `subIssues` → `SubIssues`, `closedByPullRequestsReferences` (open and closed) → `LinkedPRs`, `comments.totalCount` → `Comments`.
- PR: `isDraft`, `headRefOid`, `baseRefName`, labels, author, `mergedAt`, `mergeCommit` (only once merged). `checks` rolls up the last commit's check suites that have check runs, **keeping only the latest suite of each workflow** (fx/checks-latest-suite): a GitHub Actions suite's kind is its App, workflow and event (`app{databaseId}`, `workflowRun{workflow{databaseId}, event}`); the latest is the highest workflow run number, then the newest `createdAt`, then the highest `databaseId`, and a suite with runs isn't superseded by a later one without. A suite whose workflow can't be told (no App, a null `workflowRun`: another App's suite such as CodeRabbit's, or an Actions suite whose run was deleted; a null `workflow`) is **never superseded and supersedes nothing**, so a failing suite can't hide behind an unrelated green one. So a run cancelled by its concurrency group, or a failed run re-triggered by a later green one, no longer turns a green PR into `failure` (compliance-framework/api#584: a cancelled `preview` run beside its green successor). A re-run (`rerun-failed-jobs`, re-run all jobs) is a new attempt of the same workflow run and GitHub keeps it in the same check suite, whose status and conclusion are the latest attempt's (checked on GitHub: a run's `check_suite_id` is unchanged by a re-run, and that suite went from failure to success), so attempts need no handling. The event is part of the kind because a `push` and a `pull_request` run of one workflow are separate checks (separate concurrency groups in practice): a failing `push` run isn't hidden by a green `pull_request` one. Of the kept suites: `failure` once one concluded failing (failure, timed out, cancelled, action required, startup failure, stale), else `pending` while one runs, else `success` (success, neutral, skipped); `settled` for `success` and `failure`; no such suite leaves `checks` empty. **Not `statusCheckRollup`** (fx/gh-observe-perms): it also covers commit statuses (`statuses: read`), which the Apps aren't granted, and check suites are what `pr.checks.completed` reports, so commit statuses don't count. `approvedBy` is each reviewer's **latest opinion** (`APPROVED`, `CHANGES_REQUESTED`, `DISMISSED`; comments and pending reviews don't change it) when it is an approval, at that review's commit, sorted. The factory's Apps map to their identity (`ccf-factory-reviewer[bot]` → `reviewer`, `ccf-factory[bot]` → `author`, from the slugs T02 learns), anyone else to their login; a deleted account counts for nobody. `unresolvedThreads` counts `isResolved: false` across all pages.
- PR: `isDraft`, `headRefOid`, `baseRefName`, labels, author, `mergedAt`, `mergeCommit` (only once merged). `checks` rolls up the last commit's check suites, **keeping only the latest suite of each workflow** (fx/checks-latest-suite): a GitHub Actions suite's kind is its App, workflow and event (`app{databaseId}`, `workflowRun{workflow{databaseId}, event}`); the latest is the highest workflow run number, then the newest `createdAt`, then the highest `databaseId`. **A suite of a known kind takes part whether or not it has check runs yet** (fx/checks-supersede-without-jobs): a workflow run's suite exists before its jobs do, so a later run supersedes from its creation and counts as `pending` until it concludes. Skipping it until its first job started let the run it superseded count as the latest in between: on compliance-framework/api#587 (v0.6.0), `preview` run 388's suite was created at 12:56:43 and run 387 (4 check runs) was cancelled at 12:56:44; the observation on 387's `check_suite` webhook at 12:56:48 still saw no check runs in 388's suite (its first job started at 12:56:47), so it rolled up `failure, settled` and a catalog rule dispatched a fix on a PR that went green at 12:57:05. A kind-known suite completed without check runs counts by its conclusion: GitHub completes a run without creating jobs when it failed to start (`startup_failure`, an invalid workflow file), was cancelled before a job started (`cancelled`, usually by its concurrency group; a later run of the same workflow and event then supersedes it) or awaits approval (`action_required`); none passed, so none is ignored. So consumers now see `failure, settled` for a fork PR awaiting workflow approval (`action_required`), for every PR while a workflow file on the default branch is invalid (`startup_failure`), and for a run cancelled before its first job by a concurrency group shared across workflows or events (its successor is another kind, so it doesn't supersede it); where the run had jobs these were already `failure`. Kept on review: fork PRs get no `pr.checks.completed`, and the catalog rules that act on `failure` are for the factory's own PRs. A suite whose workflow can't be told (no App, a null `workflowRun`: another App's suite such as CodeRabbit's, or an Actions suite whose run was deleted; a null `workflow`) is **never superseded and supersedes nothing**, so a failing suite can't hide behind an unrelated green one; it counts only with check runs, because GitHub leaves such a suite queued for every App that could run checks (Actions with no workflow for the event) and those never complete. So a run cancelled by its concurrency group, or a failed run re-triggered by a later green one, no longer turns a green PR into `failure` (compliance-framework/api#584: a cancelled `preview` run beside its green successor). A re-run (`rerun-failed-jobs`, re-run all jobs) is a new attempt of the same workflow run and GitHub keeps it in the same check suite, whose status and conclusion are the latest attempt's (checked on GitHub: a run's `check_suite_id` is unchanged by a re-run, and that suite went from failure to success), so attempts need no handling. The event is part of the kind because a `push` and a `pull_request` run of one workflow are separate checks (separate concurrency groups in practice): a failing `push` run isn't hidden by a green `pull_request` one. Of the kept suites: `failure` once one concluded failing (failure, timed out, cancelled, action required, startup failure, stale), else `pending` while one runs, else `success` (success, neutral, skipped); `settled` for `success` and `failure`; no such suite leaves `checks` empty. **Not `statusCheckRollup`** (fx/gh-observe-perms): it also covers commit statuses (`statuses: read`), which the Apps aren't granted, and check suites are what `pr.checks.completed` reports, so commit statuses don't count. `approvedBy` is each reviewer's **latest opinion** (`APPROVED`, `CHANGES_REQUESTED`, `DISMISSED`; comments and pending reviews don't change it) when it is an approval, at that review's commit, sorted. The factory's Apps map to their identity (`ccf-factory-reviewer[bot]` → `reviewer`, `ccf-factory[bot]` → `author`, from the slugs T02 learns), anyone else to their login; a deleted account counts for nobody. `unresolvedThreads` counts `isResolved: false` across all pages.
- Author: `<provider>:<login>` with REST's spelling (GraphQL's Bot `ccf-factory` is `ccf-factory[bot]`, as in webhooks), `isBot`, and `isFactory`/`identity` for the factory's Apps; nil for a deleted account.
- Projections (removed with P3: [§P3](#decisions-taken-with-p3-no-list-no-marker-read-back)): the v1 markers among the comments **written by the projecting App** (the author identity); a look-alike by anyone else, or a marker of another version, is ignored (the latter with a warning). Of two markers for one work item the oldest comment wins. Comment bodies are read only for that and never returned.
- **Other kinds**, because the work-item view and the rule controller's probe treat any error but `ErrNotFound` as fatal: a `repo` link (an event without an object) is open, or closed once archived (`ObserveRepo`, `metadata: read`); releases and deployments are immutable records, `closed` with no call. Any other kind is `ErrInvalid`.
Expand Down Expand Up @@ -200,7 +200,7 @@ P3 §No rebuild from markers removed the work-item rebuild, the only reader of `

## Decisions taken in fx/ci-rerun-effect

- **`ci.rerun`** (`effects_ci.go`) re-runs CI on a PR so an agent can treat a failure as a flake. It acts on a **PR** (in `prOps`: an issue target is `ErrInvalid`, an issue number `ErrNotFound`), as the request's identity with **`{actions: write, pull_requests: read}`** on the repo. Only the `ccf-factory` App (author) is meant to have Actions: write, so a reviewer task asking for it fails at the mint with `ErrInvalid`. Steps: read the PR (closed or merged: `ErrInvalid`) for its head SHA; list the head's workflow runs (`GET …/actions/runs?head_sha=`, `per_page=100`, up to 5 pages); keep the **latest run per workflow and event** (`latestRuns`: the same superseding rule and `runOrder` as `checksOf`, so a run its concurrency group cancelled isn't re-run when a later run of that workflow exists); of those, take the ones `completed` with `failure`, `timed_out`, `cancelled` or `startup_failure`. **A run whose `run_attempt` is `MaxRunAttempts` (3) or more is skipped**: the provider never takes a run past its third attempt, a safety net for rule-driven use. `failure` and `timed_out` get `rerun-failed-jobs`; `cancelled` and `startup_failure` get `rerun` (a run cancelled by its concurrency group, or one that failed to start, can have no failed job). **A run GitHub refuses** (403 or 422 on its re-run: over 30 days old, already re-running, …) **is skipped and the others are still re-run**. If at least one run was re-run, the op succeeds, and when some were skipped (refused or capped) the outcome is `partially-rerun: skipped run <id> (refused|attempt <n>), …`. If nothing was re-run and something was refused, the op is `ErrInvalid` with every refusal. Otherwise, with nothing to re-run (green, running or capped), it succeeds with outcome **`nothing-to-rerun`**. The result's object is the PR and its URL the PR's checks page.
- **`ci.rerun`** (`effects_ci.go`) re-runs CI on a PR so an agent can treat a failure as a flake. It acts on a **PR** (in `prOps`: an issue target is `ErrInvalid`, an issue number `ErrNotFound`), as the request's identity with **`{actions: write, pull_requests: read}`** on the repo. Only the `ccf-factory` App (author) is meant to have Actions: write, so a reviewer task asking for it fails at the mint with `ErrInvalid`. Steps: read the PR (closed or merged: `ErrInvalid`) for its head SHA; list the head's workflow runs (`GET …/actions/runs?head_sha=`, `per_page=100`, up to 5 pages); keep the **latest run per workflow and event** (`latestRuns`: the same superseding rule and `runOrder` as `checksOf`, so a run its concurrency group cancelled isn't re-run when a later run of that workflow exists; the runs API lists a run from its creation, jobs or not, so a later run without jobs yet supersedes here as it does in `checksOf`); of those, take the ones `completed` with `failure`, `timed_out`, `cancelled` or `startup_failure`. **A run whose `run_attempt` is `MaxRunAttempts` (3) or more is skipped**: the provider never takes a run past its third attempt, a safety net for rule-driven use. `failure` and `timed_out` get `rerun-failed-jobs`; `cancelled` and `startup_failure` get `rerun` (a run cancelled by its concurrency group, or one that failed to start, can have no failed job). **A run GitHub refuses** (403 or 422 on its re-run: over 30 days old, already re-running, …) **is skipped and the others are still re-run**. If at least one run was re-run, the op succeeds, and when some were skipped (refused or capped) the outcome is `partially-rerun: skipped run <id> (refused|attempt <n>), …`. If nothing was re-run and something was refused, the op is `ErrInvalid` with every refusal. Otherwise, with nothing to re-run (green, running or capped), it succeeds with outcome **`nothing-to-rerun`**. The result's object is the PR and its URL the PR's checks page.
- **Idempotency: once per task per key on the Effector path; per (key, head SHA) in the provider.** On the agent path, `CreateEffect` (`internal/server/effects.go`) replays any recorded `effectID(task, key)` before the provider is called, and every successful Apply is recorded (`nothing-to-rerun` included). So **`--key k` acts once per task**, whatever the head. To re-run a new head in the same task, use a per-head key (`--key rerun-<sha>`, as the catalog does) or omit `--key` (each successful call then gets a fresh key). This is the contract; the head SHA is deliberately not folded into the server's effect key. In the provider, the effective key is `<key>@<head SHA>`, and the Conn remembers the effective keys it re-ran something with (`rerunLog`, the last 1024): the same key on the same head is a replay (`Replayed`, nothing posted). That covers what the store doesn't: a rule's effect is applied before it is recorded and again when its reconcile retries (rule keys are per event), and an Effector call whose Apply succeeded but whose record failed is applied again. A re-run leaves nothing on GitHub that could carry the key (the endpoints take no body), so the replay can't be read back from GitHub as the other ops' are. Nothing to re-run isn't remembered. The memory belongs to the process: a replay it has forgotten still posts nothing while the runs it re-ran are queued or running, because only completed runs are re-run. A retry inside `Apply` (rate limit, 5xx) re-lists the runs, so a run re-run before the failure is queued by then and isn't posted again.
- **Errors.** A 404 (the PR or a run) is `ErrNotFound`. A 403 that isn't a rate limit is `ErrInvalid`. When GitHub says the integration can't access the resource, or the installation wasn't granted the permission at the mint, the message names **Actions: write** (or **Pull requests: read** for the PR read). Any other 403 (a run over 30 days old, a run already running) keeps GitHub's message. A 422 is `ErrInvalid`. On a run's re-run, 403 and 422 skip that run rather than fail the op (see above). Rate limits and 5xx are retried, as for the other ops.
- **Deploy prerequisite.** The `ccf-factory` App needs **Actions: write**, a manual change in the GitHub App's settings that each installation then accepts (see [rollout.md §GitHub Apps](../../poc-rollout/LLD/rollout.md)).
Expand Down
4 changes: 3 additions & 1 deletion internal/provider/github/effects_ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ func (c *Conn) headRuns(ctx context.Context, tok, repo, sha string) ([]ghWorkflo

// latestRuns are the runs no later run of the same workflow and event supersedes, in their
// order: checksOf's rule for check suites (a run cancelled by its concurrency group, or
// re-triggered, is superseded by the later run).
// re-triggered, is superseded by the later run). The runs API lists a run from its creation,
// before it has jobs, so a later run supersedes here from the start as its suite does in
// latestSuites.
func latestRuns(runs []ghWorkflowRun) []ghWorkflowRun {
kind := func(r ghWorkflowRun) string { return strconv.FormatInt(r.WorkflowID, 10) + "/" + r.Event }
order := func(r ghWorkflowRun) runOrder { return runOrder{number: r.RunNumber, created: r.CreatedAt, id: r.ID} }
Expand Down
5 changes: 4 additions & 1 deletion internal/provider/github/effects_ci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ func TestApplyCIRerunNothing(t *testing.T) {
}
e.srv.AddWorkflowRuns(docs,
wfRun(1, ciWorkflow, 10, "completed", "failure"), wfRun(2, ciWorkflow, 11, "completed", "success"), // superseded failure
wfRun(3, preview, 20, "queued", ""), wfRun(4, lint, 30, "completed", "skipped"))
wfRun(3, preview, 20, "queued", ""), wfRun(4, lint, 30, "completed", "skipped"),
// compliance-framework/api#587: a run its concurrency group cancelled, superseded by a
// later run that has no jobs yet (the runs API lists it all the same).
wfRun(5, preview, 19, "completed", "cancelled"))
if res, err := c.Apply(ctx, ciRerun("")); err != nil || res.Outcome != github.OutcomeNothingToRerun || res.Replayed {
t.Fatalf("green and running: %+v, %v", res, err)
}
Expand Down
Loading