gitoria
All repositories: gitoria
21.7 KB
# Gitoria apps API: the contract (gitoria#38, children #40–#47)Version 1.6, 2026-11-15. Open points decided by the external reviewer (the reference for open questions, review: docs/apps-api-review.md). v1.2.1 is live (#40–#44, #46); the v1.3 additions come with #47. This file is the contract that gitoria is built against and Kunibert codes against.Marks: **[OPEN]** = the name or value may still change; everything else is fixed. Any change comes as a new versionof this file with a changelog line at the bottom, announced to Kunibert.## 0. Words- **App**: an outside program connected to ONE repo by that repo's owner. It acts under its own bot name and never as a person.- **Key** `gak_` + 40 hex: long-lived, given once at connect time, kept by the app. It is used only to get tokens.- **Token** `gat_` + 40 hex: short-lived (1 hour), used for every API call and for git fetch.- **Secret**: 64 hex, given once at connect time. It signs the events. Gitoria never sends it again.- **API base** `{api}`: comes in the connect answer and in every event (`repo.api`). Never build it yourself.Today it is `https://<slug>.gitoria.worldapi.org/api`; for an organization repo it is `https://<org>.gitoria.worldapi.org/api/<repo>`.- **PR number** `N`: 1, 2, 3 … per repo. It never changes and is never reused (#40).## 1. Connecting (#41)Two ways. Both end with the app holding `key`, `secret` and `api`.**A. Connect flow** (the tickets connect shape, with gitoria as the server):1. The app sends the owner's browser to`https://<repo host>/settings/apps/connect?app=<name>&hook=<receiver URL>&return=<URL>&state=<random>`.`<repo host>` is the repo's address; an organization repo uses `/<repo>/settings/apps/connect`.2. Gitoria signs the person in, checks that they own the repo, shows "Connect <name> to <repo>?", and on Yesredirects to `return?code=<one-time code>&state=<same state>`. The code works once and for 5 minutes.3. The app's SERVER calls `POST {repo address}/api/apps/exchange` with `{"code":"…"}` (no auth). The answer:`{"key":"gak_…","secret":"<64 hex>","api":"https://…/api","repo":{"id":"…","full":"kunibert-test"},"app":{"id":"…","name":"Kunibert"}}`.4. Gitoria sends a `ping` event at once.**B. Manual**: the owner opens Settings → Apps → Add, types a name and the receiver URL, and sees the key and thesecret once. This is the way for a conductor chat (`https://<hyperconductor>/api/push/<chatId>`).**Allowed URLs** (`hook` and `return`), no redirects followed: `https://` with host `worldapi.org` or one of its subdomains, or a URLthat starts with one of the HyperConductor bases configured in gitoria, exactly as written there, plain `http` included(the same list tickets uses in `TICKETS_PUSH_ALLOWED`, e.g. `http://100.77.141.84:8100`). The configured bases are listed in README.Anything else → 400 `invalid_url`.Re-connecting the same app name to the same repo replaces the old key and secret. The owner can remove an app (key andtokens die at once) or rotate its secret (Settings → Apps).## 2. Tokens (#42)`POST {api}/apps/token` with `Authorization: Bearer gak_…` → `200 {"token":"gat_…","expires":"2026-11-02T07:00:00Z"}`.The token lives 1 hour. Get a new one whenever you need it; old ones keep working until they expire.- API: `Authorization: Bearer gat_…`.- git over HTTPS: Basic auth, user `app` (any name is accepted), password `gat_…`.Example: `git -c http.extraHeader="Authorization: Basic $(printf 'app:%s' "$TOKEN" | base64 -w0)" fetch <clone URL> …`The token is **read-only**: fetch and clone work, a push is refused (403).- A token works for its own repo only. For any other repo every route answers 404, the same as for a repo that does not exist.## 3. Git (#40, #42)- Clone URL: `repo.clone` in every event (today `https://<slug>.gitoria.worldapi.org/<slug>.git`).- PR head: **`refs/pull/N/head`**. Gitoria keeps it pointing at the PR's head commit.- Base: **`refs/heads/<target>`** (`pull.baseRef`). `pull.mergeBase` is the merge-base sha at the time of the event.- Shallow fetch works: protocol v2 (`git -c protocol.version=2`) with `--depth` and `--deepen` (checked live 2026-11-02).- An event is sent only after `refs/pull/N/head` points at `pull.head`, so a fetch right after the event finds the commit.- What a PR is in gitoria: a commit whose subject starts `|||PR ` (target = main branch) or `|||PR|<branch>] ` on a source branch.Title = the rest of the subject; body = the rest of that commit's message.## 4. Events (#43)`POST <receiver URL>`, body = JSON (UTF-8), headers:| header | value ||---|---|| `Content-Type` | `application/json` || `User-Agent` | `gitoria` || `X-Push-Event` | the event name below || `X-Push-Delivery` | uuid; the SAME on a retry or redelivery || `X-Push-Signature-256` | `sha256=` + hex HMAC-SHA256(key = the secret's 64 hex characters as ASCII bytes, raw body bytes) |Delivery: any 2xx within 10 seconds counts as delivered. Otherwise gitoria retries after 1 min, 5 min, 30 min, 2 h and 6 h,then marks the delivery `failed`. Redirects are not followed. Order is not guaranteed: trust `pull.head`and the `time` field, and treat a repeated `X-Push-Delivery` as already handled. A receiver should answer at once and work afterwards.Common body for every event:```json{ "event": "pull_request", "action": "opened", "delivery": "<uuid>", "time": "2026-11-02T06:00:00Z","repo": { "id": "…", "full": "kunibert-test", "private": false, "api": "https://…/api", "clone": "https://…/kunibert-test.git" },"app": { "id": "…", "name": "Kunibert" },"sender": { "id": "…", "name": "Caramboleyo" } }````sender` = the gitoria user who caused the event (the pusher, the commenter, the person who merged); `null` if gitoria itself caused it.The **pull** object (in every PR event, and from `GET {api}/pulls/N`):```json{ "number": 3, "title": "…", "body": "…", "draft": false, "state": "open","author": { "id": "…", "name": "…" }, "source": "feature-x", "target": "main","head": "<40 hex>", "ref": "refs/pull/3/head", "baseRef": "refs/heads/main", "base": "<target tip sha>", "mergeBase": "<sha>","tickets": ["gitoria.worldapi.org#12"], "url": "https://…/pulls/3", "created": "…", "updated": "…","statuses": [status…], "required": [{"app":{"id":"…","name":"Kunibert"},"context":"kunibert"}],"approvers": [{"user":{"id":"…","name":"Caramboleyo"}}],"approvals": [{"user":{"id":"…","name":"Caramboleyo"},"head":"<40 hex>","created":"2026-11-15T06:00:00Z","current":true}] }```- `statuses` (v1.3): every status on the CURRENT `head`, from every app (see `status` in §5).- `required` (v1.4): what the owner marked "required before merge", each as app + context. For reading only: it explains why Merge is off.- `approvers` (v1.6): the people the owner listed as required approvers, each `{"user":{"id":"…","name":"…"}}`. This is separate from `required`, which retains its app + context shape.- `approvals` (v1.6): people's approvals for this PR, including earlier heads: `{"user":{"id":"…","name":"…"},"head":"<40 hex>","created":"<ISO UTC time>","current":true|false}`. A push makes an earlier approval stale (`current:false`); it no longer counts. A person can withdraw their current-head approval. Apps cannot approve as people.- These fields are additive: readers ignoring the new fields keep working. The v1.5.1 shapes of `pull.required` and `checks_failed` detail rows are unchanged.- `state`: `open` | `closed` | `merged`.- `draft`: gitoria has no drafts, so it is always `false`.- `author`: the gitoria user who pushed the `|||PR` commit; `id` is `null` and `name` is the commit author when gitoria cannot tell.- `tickets`: the tickets this PR names in its title or body, as `project#n`. An explicit `project#12` is always listed.A bare `#12` is listed only when the repo is connected to tickets, because only then does gitoria know the project.Read the tickets from tickets with your own token. Gitoria serves no ticket content.Events:| `X-Push-Event` | `action` | extra fields | when ||---|---|---|---|| `ping` | — | — | right after connecting, and on "Send ping" in Settings || `pull_request` | `opened` | `pull` | a new `|||PR` commit appears || `pull_request` | `reopened` | `pull` | a closed PR's branch comes back with its `|||PR` commit || `pull_request` | `synchronize` | `pull`, `before`, `after`, `forced` (bool) | the PR's branch moves (new commits or a force-push) || `pull_request` | `closed` | `pull` (state `closed`) | branch deleted, or the `|||PR` commit gone from it || `pull_request` | `merged` | `pull` (state `merged`), `mergeCommit` | Merge button, or a push that puts the PR into its target || `review_requested` | `requested` | `pull` | a person clicks "Review again" on the PR page || `review_comment` | `created` | `pull`, `thread`, `comment` | a person replies in one of YOUR threads || `pull_comment` | `created` | `pull`, `comment` | a person writes in the PR's conversation (for example `@kunibert reset`) || `review_thread` | `resolved` / `unresolved` | `pull`, `thread` | a person resolves or reopens one of YOUR threads || `pull_request` | `ready` | `pull` | reserved, only once drafts exist |An app gets no events about its own actions. The common body (with `repo.api`, `repo.clone` and `app`) comes with EVERY event, `ping` included.## 5. Back-channel (#44, #46). Every route: `Authorization: Bearer gat_…`; answers are JSON| route | request | answer ||---|---|---|| `GET {api}/pulls?state=open\|all&cursor=&limit=` | — | `{"pulls":[pull…],"next":"<cursor>"\|null}` || `GET {api}/pulls/N` | — | `pull` || `POST {api}/pulls/N/reviews` | `{"head":"<sha>","verdict":"approve"\|"request_changes"\|"comment","body":"…","comments":[{"path":"lib/a.hl","line":42,"body":"…"}]}` | `201 {"review":{"id","verdict","head","created"},"threads":[{"id","path","line"}]}` || `GET {api}/pulls/N/reviews?cursor=&limit=` | — | `{"reviews":[{"id","verdict","head","body","created"}…],"next":…}` (your own reviews, newest first) || `GET {api}/pulls/N/threads?cursor=&limit=` | — | `{"threads":[thread…],"next":…}` (your own threads only) || `POST {api}/pulls/N/threads/:id/replies` | `{"body":"…"}` | `201 {"comment":comment}` || `POST {api}/pulls/N/threads/:id/resolve` | `{"resolved":true\|false}` | `200 {"thread":thread}` || `GET {api}/pulls/N/comments?cursor=&limit=` | — | `{"comments":[comment…],"next":…}` (the PR's conversation, oldest first) || `POST {api}/pulls/N/comments` | `{"body":"…"}` | `201 {"comment":comment}` (in the PR's conversation) || `PUT {api}/statuses/<sha>` | `{"context":"kunibert","state":"pending"\|"success"\|"failure"\|"error","title":"…","summary":"…","url":null}` | `200 {"status":{…,"updated"}}` || `GET {api}/statuses/<sha>` (v1.3) | — | `{"statuses":[status…]}`: every context on that sha, from every app; 404 for a sha this repo does not have || `GET {api}/deliveries?cursor=` | — | `{"deliveries":[{"id","event","action","time","status":"delivered"\|"pending"\|"failed","tries","lastCode"}],"next":…}` || `POST {api}/deliveries/:id/redeliver` | — | `202` |- `status` = `{"context","state","title","summary","url","head","updated","app":{"id","name"}}`. Reading shows all apps' statuses; an app writes only its own.- `outdated` = the thread was written on an older head than the PR's current one (`thread.head != pull.head`), even when itsfile and line did not change. It is not line tracking; an app that wants that compares the file itself.- `thread` = `{"id","path","line","head":"<sha it was written on>","outdated":bool,"resolved":bool,"comments":[comment…]}`.- `comment` = `{"id","seq","author":{"type":"app"\|"user","id","name"},"body","created"}`. `seq` is an integer that grows strictlywithin a repo (over all comments of all PRs), so it works as a watermark. `id` is an opaque string: do not compare it.`created` has milliseconds (`2026-11-02T06:00:00.123Z`). Lists are ordered by `seq`.- **Review**: `head` may be the PR's current head or ANY earlier head of this PR. The line check uses the diff of that `head`;if it is not the current head, the review's threads are `outdated` at once. Only a sha that was never a head of this PRgets `409 stale_head` (nothing stored). Your current verdict on a PR is your latest review; a `comment` review does notclear an earlier `request_changes`. `line` is a line number on the NEW side (the file at `head`) that lies inside a hunk of `git diff -M -U3 <mergeBase> <head>` (an added line or acontext line). `path` is the new-side path, after a rename. `<mergeBase>` is computed when the review arrives:`git merge-base <current target tip> <head>`, not the value from an older event.If any inline comment points at another line, the WHOLE review is refused: `422 invalid` with`details: [{"index":2,"path":"…","line":7,"reason":"line_not_in_diff"\|"no_such_file"}]`. Send it again without those comments,or put them in the body. At most 100 inline comments per review. Gitoria shows the review under the app's name with an "app" mark.- **Status**: one status per (head sha, app, context): an app writes and replaces only its own; another app may use the same context name without touching it. `error` = the app could not do its check (shown apart from `failure`;it does not count as `success`). Setting no status at all is allowed too. A newer PUT replaces the older one. If the owner set "required before merge"for that app + context, the Merge button stays off until that status is `success` on the PR's current head (#46).- Bodies (review, comment, status summary) are Markdown, up to 65,536 characters. Gitoria shows them escaped (no raw HTML).- Paging: pass `next` as `cursor`. `limit` defaults to 50 and is at most 100.## 6. Errors and limits (#47)Every error: `{"error":{"code":"…","message":"…","details":…}}`.| HTTP | code ||---|---|| 400 | `invalid_json`, `invalid_url` || 401 | `unauthorized` (missing, unknown or expired token: get a new token) || 403 | `app_token_required` (valid user token on an app-only route), `forbidden` (§7: an app token on the merge route; a user who may not push) || 404 | `not_found` (also: another repo, a removed app) || 409 | `stale_head`, `conflict` (merge refusal, with `details.reason`; see §7) || 413 | `too_large` (request body over 1 MB) || 422 | `invalid`, `checks_failed` (with `details`; see §7) || 429 | `rate_limited`, with a `Retry-After` header (seconds) |Limits per app: 300 reads and 60 writes per minute.## 7. User tokens (gtr_) (v1.5.1: #48, W49)A user access token is `gtr_` + 40 hex. It has no expiry. Its user is a **member** of a repo when it may read AND push there(owner, repo member or organization member). Member reads never reveal whether a repo or a PR exists to non-members: a validtoken of a non-member gets 404 `not_found`, also on public repos. This is on purpose, not a bug.Reads (W49, live dab74b0): `GET {api}/pulls`, `{api}/pulls/N`, `{api}/pulls/N/reviews`, `{api}/pulls/N/threads`,`{api}/pulls/N/comments`, `{api}/statuses/<sha>`. Shapes, order and paging are the same as for apps. A user sees every app'sreviews and threads; an app still sees only its own. User-token reads are not rate-limited by the apps API (the app limits of §6count apps only). Back-channel writes, `GET {api}/deliveries`, redelivery and `POST {api}/apps/token` stay app-only.Merge (#48, live 1a687b1): `POST {api}/pulls/N/merge`, `Authorization: Bearer gtr_…`. Body optional: empty, or`{"head":"<40 hex>"}` = the PR head the caller looked at. No other fields (always a merge commit with the message`Merge branch '<source>' into <target>`; no method, no message).The OR rule is the creator's recorded decision in [gitoria#52](https://tickets.worldapi.org/gitoria.worldapi.org/52) and DECISIONS.md "PR approvals": "PR merges should not be possible without his or my approval" and "yes add the approve button for people". It applies only when the owner adds person entries. App-only repos keep the approve + success review workflow and their existing required-status rule; person approvals do not bypass app-only requirements. With person entries, merge is allowed when (there is at least one required app context AND all required app contexts are `success` on the head) OR (at least one listed person approved this PR's current head). With people only, a listed person's approval is required. With nothing required, merge behaves as before.| HTTP | code | meaning ||---|---|---|| 200 | — | `{"merged":true,"mergeCommit":"<sha>","pull":{…}}`; `pull` has the shape of `GET {api}/pulls/N`, read after the merge || 400 | `invalid_json` | the body is not a JSON object || 401 | `unauthorized` | missing or unknown token ("a user access token is required") || 403 | `forbidden` | an app token ("apps cannot merge"), or a user who may read but not push || 404 | `not_found` | repo not visible to this user, or no such PR || 405 | `invalid` | not POST || 409 | `conflict` | not merged; `details = {"reason":"…"}`: `head_mismatch` (re-read the PR and retry), `already_merged`, `branch_gone`, `target_missing`, `merge_conflict` (message names up to 5 files), `target_moved` (retry), or `git_error` (could not read/write) || 413 | `too_large` | body over 1 MB || 422 | `invalid` | `head` is not 40 hex characters (`details`: field `head`) || 422 | `checks_failed` | required checks are not success; `details` is a list of `{"app":{"id":"…","name":"…"},"context":"…","state":"missing"}` for each missing or failed context; state is `missing` when no status exists, otherwise `pending`, `failure` or `error`. The message still names each as `<app> · <context> (<state>)`. |When person entries exist and neither alternative is met, `error.approval` is present next to `error.details`:```json{"error":{"code":"checks_failed","message":"Not merged: … or an approval from Caramboleyo.","details":[{"app":{"id":"…","name":"Kunibert"},"context":"kunibert","state":"pending"}],"approval":{"users":[{"id":"…","name":"Caramboleyo"}],"state":"missing"}}}````details` remains app + context + state only (empty for person-only requirements). `approval` is omitted on app-only errors and when either alternative is met. Readers ignoring the new fields keep working.Event: `pull_request` action `merged`, `sender` = the merging user (never an app).A valid user token on an app-only route (any write of §5, deliveries, redelivery or `POST {api}/apps/token`) gets 403`app_token_required` with the message `this route needs an app token (gat_); user tokens (gtr_) can read pulls, reviews,threads, comments and statuses and merge with POST /pulls/N/merge`. Missing, unknown or expired tokens stay 401 `unauthorized`.## 8. Anonymous reads (v1.6, gitoria#51)A request with **no `Authorization` header at all** may read two routes of a **public** repo, answered exactly as for a member (same JSON, same 404 for an unknown PR or sha):`GET {api}/pulls/N` and `GET {api}/statuses/<sha>`.A private or missing repo answers 404 `not_found` ("no such repository"), the same body for both, so nobody learns that a private repo exists.Anonymous reads are not rate-limited and not counted in the app limits (the same data is on the public PR page).A request that carries any token keeps the member rules of §7: a valid `gtr_` of a non-member is 404, an unknown token is 401. The PR list `GET {api}/pulls`, reviews, threads, comments, writes, merge and deliveries still need a token (401 without).## Changelog- v1.6 2026-11-15: #52 adds separate `pull.approvers`, `pull.approvals` and `error.approval` fields; `pull.required` and error `details` retain their v1.5.1 app-only shapes. The recorded OR rule applies only with person entries. Readers ignoring new fields keep working.- v1.6 2026-11-12: §8 anonymous reads of `GET {api}/pulls/N` and `GET {api}/statuses/<sha>` on public repos (#51); private or missing repos are 404.- v1.5.1 2026-11-05: valid user tokens on app-only routes get 403 `app_token_required`; unmet required checks get 422`checks_failed` with app, context and state in `details`; every 409 merge refusal carries `details.reason`.- v1.5 2026-11-05 (reviewed by the external reviewer): §7 user tokens: members merge with gtr_ (#48, deploy 1a687b1) and read pulls, reviews,threads, comments, statuses (W49, deploy dab74b0); known deviations (401 for gtr_ on app routes, 409 for failed required checks) stated.- v0 2026-11-02: first contract (Gitoria), before any code.- v1 2026-11-02 (Kunibert's points): `comment.seq` watermark and ms `created`; `GET …/pulls/N/comments`; exact diff for the line check(`-M -U3`, new-side path, merge-base at review time); explicit `project#n` listed without a tickets connection; common body on `ping`;status state `error`.- v1.4 2026-11-02 (security, decided by the external reviewer): statuses are keyed by (sha, app, context), so no app can replace another app's status;"required before merge" names app + context; `required` on the pull = `[{"app":{"id","name"},"context"}]`. A removed app's required entrystays unmet until the owner changes it. The PUT is unchanged.- v1.3.1 2026-11-02 (Kunibert's question): stated what `outdated` means (written on an older head), as built in #44.- v1.3 2026-11-02 (Kunibert's wish, decided by the external reviewer): `GET {api}/statuses/<sha>` lists all contexts on a sha; `status` carries `app`;the pull object gets `statuses` (current head, all apps) and `required` (read-only). Built with #47.- v1.2.1 2026-11-02: clarified the HMAC key = the secret string's ASCII bytes (not hex-decoded).- v1.2 2026-11-02 (external reviewer): review on any earlier head (outdated threads, 409 only for a foreign sha); bodies 65,536; current verdict= latest review, `GET …/pulls/N/reviews`; shallow fetch (v2 depth/deepen) stated; open points closed (retries, `context`, limits, drafts).- v1.1 2026-11-02 (Apollo): the configured HyperConductor bases are allowed exactly as written, plain http included; https only for worldapi.org.
Branches
- f1-broken-json
- f2-api-bugs
- gp-gate-ports
- heap-revert
- hl25-records
- ident-chooser-selector
- mainmain branch
- p51-public-pr-reads
- records-1112
- s25-no-session-on-login-error
- v59
- w52-person-approval
- w52b-approval-followup
- w52c-header-phone
- w53-pulls-speed-pr
- w54-repo-rename-pr
- w55-rename-org
- w56-owner-members
- w62-pr-close
- w63-force-push
- w64-org-slash
Latest commits
- 0bab9e46|||PR header: ident status wraps on phones instead of clippingmre
- e7af3eceMerge branch 'w52b-approval-followup' into mainGitoria
- c01e66c6|||PR approvals follow-up: live Merge after status writes, helpers (#52)mre
- 841da497Merge branch 'w53-pulls-speed-pr' into mainGitoria
- 80307ae2|||PR pulls page cache: live PR numbers, complete scans onlymre
- 69141f65|||PR pulls page: cache the git part per repo by branch tipsmre
- 155861f6Merge branch 'w52-person-approval' into mainGitoria
- b5435de8|||PR person approvals: second Kunibert round (#52)mre
- 3a8b2fb8|||PR person approvals: Kunibert review fixes (#52)mre
- 2a89d8d0|||PR person approvals: Approve button for people; merge needs the required checks or a listed person's approval (#52)mre
- 83f0c953Report person approval verificationmre
- 3ae94b86Record browser failures and verify owned tab cleanupmre
- 54eec687Gate person approvals and merge alternativesmre
- 12b08829Add person approvals for pull request headsmre
- 55c84779Merge branch 'gp-gate-ports' into mainGitoria
- 57af42bbREADME: point to deploy.sh for the port listmre
- d63375d2README: gate commands with explicit portsmre
- feeaa701|||PR gates: explicit ports only, deploy.sh passes themmre
- 6cc1f943Merge branch 'records-1112' into mainGitoria
- 9448c438|||PR records: LOG, missions and reports since 11-09mre