gitoria
All repositories: gitoria
14.8 KB
# Gitoria apps API: the contract (gitoria#38, children #40–#47)Version 1.4, 2026-11-02. Open points decided by Vinzenz (the reference for open questions, review: docs/apps-api-review-vinzenz.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"}] }```- `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.- `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) || 404 | `not_found` (also: another repo, a removed app) || 409 | `stale_head` || 413 | `too_large` (request body over 1 MB) || 422 | `invalid` (with `details`) || 429 | `rate_limited`, with a `Retry-After` header (seconds) |Limits per app: 300 reads and 60 writes per minute.## Changelog- 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 Vinzenz): 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 Vinzenz): `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 (Vinzenz): 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
- 1075c38cW48 mission, logmre
- 17bdd0d6V37/V39 missions, logmre
- 146de5f8Re-vendor gitoria to Hybriel set-day39 (51e921d7)mre
- e874e44dtests/status-keyed: app token lives 60 s so it outlives the gate's two restartsmre
- 270b8d61deploy.sh: DEPLOY_STOP_START=1 stops, waits for the old pid, then starts (mpackdb freeze, replaces the standby swap)mre
- 3f9235b8project.hl: drop the dummy /pulls/:n component route (hybriel#294 is fixed in day37)mre
- c90daf19Re-vendor gitoria to Hybriel set-day37 (f6b3d45b)mre
- 03b5bb08W39 report to reports/, drop scratch repro, mission + logmre
- 154d333fMerge w39-create-api: POST /api/repos with a user token (#39)mre
- 149dd512Rewrite W39 reportmre
- e5385075Expect 401 for POST /api/repos without a tokenmre
- 1a76055cDocument repository creation APImre
- 4d779b3bAdd create-api gatemre
- 88563122Add POST /api/repos for user tokensmre
- b00860e3Create repositories with member checks in createRepoInmre
- 0a38f236Report repository creation API blockermre
- b5f72187Record malformed JSON blocker for repository creationmre
- a2ae8302logmre
- 6947cd58logmre
- 2175e48clogmre