gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit1c3fa8421c3fa842gitoria#45 #47: worker briefsmre1c3fa842/docs/apps-api.md

13.9 KB

  1. # Gitoria apps API: the contract (gitoria#38, children #40–#47)
  2. Version 1.3, 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.
  3. Marks: **[OPEN]** = the name or value may still change; everything else is fixed. Any change comes as a new version
  4. of this file with a changelog line at the bottom, announced to Kunibert.
  5. ## 0. Words
  6. - **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.
  7. - **Key** `gak_` + 40 hex: long-lived, given once at connect time, kept by the app. It is used only to get tokens.
  8. - **Token** `gat_` + 40 hex: short-lived (1 hour), used for every API call and for git fetch.
  9. - **Secret**: 64 hex, given once at connect time. It signs the events. Gitoria never sends it again.
  10. - **API base** `{api}`: comes in the connect answer and in every event (`repo.api`). Never build it yourself.
  11. Today it is `https://<slug>.gitoria.worldapi.org/api`; for an organization repo it is `https://<org>.gitoria.worldapi.org/api/<repo>`.
  12. - **PR number** `N`: 1, 2, 3 … per repo. It never changes and is never reused (#40).
  13. ## 1. Connecting (#41)
  14. Two ways. Both end with the app holding `key`, `secret` and `api`.
  15. **A. Connect flow** (the tickets connect shape, with gitoria as the server):
  16. 1. The app sends the owner's browser to
  17. `https://<repo host>/settings/apps/connect?app=<name>&hook=<receiver URL>&return=<URL>&state=<random>`.
  18. `<repo host>` is the repo's address; an organization repo uses `/<repo>/settings/apps/connect`.
  19. 2. Gitoria signs the person in, checks that they own the repo, shows "Connect <name> to <repo>?", and on Yes
  20. redirects to `return?code=<one-time code>&state=<same state>`. The code works once and for 5 minutes.
  21. 3. The app's SERVER calls `POST {repo address}/api/apps/exchange` with `{"code":"…"}` (no auth). The answer:
  22. `{"key":"gak_…","secret":"<64 hex>","api":"https://…/api","repo":{"id":"…","full":"kunibert-test"},"app":{"id":"…","name":"Kunibert"}}`.
  23. 4. Gitoria sends a `ping` event at once.
  24. **B. Manual**: the owner opens Settings → Apps → Add, types a name and the receiver URL, and sees the key and the
  25. secret once. This is the way for a conductor chat (`https://<hyperconductor>/api/push/<chatId>`).
  26. **Allowed URLs** (`hook` and `return`), no redirects followed: `https://` with host `worldapi.org` or one of its subdomains, or a URL
  27. that starts with one of the HyperConductor bases configured in gitoria, exactly as written there, plain `http` included
  28. (the same list tickets uses in `TICKETS_PUSH_ALLOWED`, e.g. `http://100.77.141.84:8100`). The configured bases are listed in README.
  29. Anything else → 400 `invalid_url`.
  30. Re-connecting the same app name to the same repo replaces the old key and secret. The owner can remove an app (key and
  31. tokens die at once) or rotate its secret (Settings → Apps).
  32. ## 2. Tokens (#42)
  33. `POST {api}/apps/token` with `Authorization: Bearer gak_…` → `200 {"token":"gat_…","expires":"2026-11-02T07:00:00Z"}`.
  34. The token lives 1 hour. Get a new one whenever you need it; old ones keep working until they expire.
  35. - API: `Authorization: Bearer gat_…`.
  36. - git over HTTPS: Basic auth, user `app` (any name is accepted), password `gat_…`.
  37. Example: `git -c http.extraHeader="Authorization: Basic $(printf 'app:%s' "$TOKEN" | base64 -w0)" fetch <clone URL> …`
  38. The token is **read-only**: fetch and clone work, a push is refused (403).
  39. - 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.
  40. ## 3. Git (#40, #42)
  41. - Clone URL: `repo.clone` in every event (today `https://<slug>.gitoria.worldapi.org/<slug>.git`).
  42. - PR head: **`refs/pull/N/head`**. Gitoria keeps it pointing at the PR's head commit.
  43. - Base: **`refs/heads/<target>`** (`pull.baseRef`). `pull.mergeBase` is the merge-base sha at the time of the event.
  44. - Shallow fetch works: protocol v2 (`git -c protocol.version=2`) with `--depth` and `--deepen` (checked live 2026-11-02).
  45. - An event is sent only after `refs/pull/N/head` points at `pull.head`, so a fetch right after the event finds the commit.
  46. - What a PR is in gitoria: a commit whose subject starts `|||PR ` (target = main branch) or `|||PR|<branch>] ` on a source branch.
  47. Title = the rest of the subject; body = the rest of that commit's message.
  48. ## 4. Events (#43)
  49. `POST <receiver URL>`, body = JSON (UTF-8), headers:
  50. | header | value |
  51. |---|---|
  52. | `Content-Type` | `application/json` |
  53. | `User-Agent` | `gitoria` |
  54. | `X-Push-Event` | the event name below |
  55. | `X-Push-Delivery` | uuid; the SAME on a retry or redelivery |
  56. | `X-Push-Signature-256` | `sha256=` + hex HMAC-SHA256(key = the secret's 64 hex characters as ASCII bytes, raw body bytes) |
  57. Delivery: any 2xx within 10 seconds counts as delivered. Otherwise gitoria retries after 1 min, 5 min, 30 min, 2 h and 6 h,
  58. then marks the delivery `failed`. Redirects are not followed. Order is not guaranteed: trust `pull.head`
  59. and the `time` field, and treat a repeated `X-Push-Delivery` as already handled. A receiver should answer at once and work afterwards.
  60. Common body for every event:
  61. ```json
  62. { "event": "pull_request", "action": "opened", "delivery": "<uuid>", "time": "2026-11-02T06:00:00Z",
  63. "repo": { "id": "…", "full": "kunibert-test", "private": false, "api": "https://…/api", "clone": "https://…/kunibert-test.git" },
  64. "app": { "id": "…", "name": "Kunibert" },
  65. "sender": { "id": "…", "name": "Caramboleyo" } }
  66. ```
  67. `sender` = the gitoria user who caused the event (the pusher, the commenter, the person who merged); `null` if gitoria itself caused it.
  68. The **pull** object (in every PR event, and from `GET {api}/pulls/N`):
  69. ```json
  70. { "number": 3, "title": "…", "body": "…", "draft": false, "state": "open",
  71. "author": { "id": "…", "name": "…" }, "source": "feature-x", "target": "main",
  72. "head": "<40 hex>", "ref": "refs/pull/3/head", "baseRef": "refs/heads/main", "base": "<target tip sha>", "mergeBase": "<sha>",
  73. "tickets": ["gitoria.worldapi.org#12"], "url": "https://…/pulls/3", "created": "…", "updated": "…",
  74. "statuses": [status…], "required": ["kunibert"] }
  75. ```
  76. - `statuses` (v1.3): every status on the CURRENT `head`, from every app (see `status` in §5).
  77. - `required` (v1.3): the contexts the owner marked "required before merge". For reading only: it explains why Merge is off.
  78. - `state`: `open` | `closed` | `merged`.
  79. - `draft`: gitoria has no drafts, so it is always `false`.
  80. - `author`: the gitoria user who pushed the `|||PR` commit; `id` is `null` and `name` is the commit author when gitoria cannot tell.
  81. - `tickets`: the tickets this PR names in its title or body, as `project#n`. An explicit `project#12` is always listed.
  82. A bare `#12` is listed only when the repo is connected to tickets, because only then does gitoria know the project.
  83. Read the tickets from tickets with your own token. Gitoria serves no ticket content.
  84. Events:
  85. | `X-Push-Event` | `action` | extra fields | when |
  86. |---|---|---|---|
  87. | `ping` | — | — | right after connecting, and on "Send ping" in Settings |
  88. | `pull_request` | `opened` | `pull` | a new `|||PR` commit appears |
  89. | `pull_request` | `reopened` | `pull` | a closed PR's branch comes back with its `|||PR` commit |
  90. | `pull_request` | `synchronize` | `pull`, `before`, `after`, `forced` (bool) | the PR's branch moves (new commits or a force-push) |
  91. | `pull_request` | `closed` | `pull` (state `closed`) | branch deleted, or the `|||PR` commit gone from it |
  92. | `pull_request` | `merged` | `pull` (state `merged`), `mergeCommit` | Merge button, or a push that puts the PR into its target |
  93. | `review_requested` | `requested` | `pull` | a person clicks "Review again" on the PR page |
  94. | `review_comment` | `created` | `pull`, `thread`, `comment` | a person replies in one of YOUR threads |
  95. | `pull_comment` | `created` | `pull`, `comment` | a person writes in the PR's conversation (for example `@kunibert reset`) |
  96. | `review_thread` | `resolved` / `unresolved` | `pull`, `thread` | a person resolves or reopens one of YOUR threads |
  97. | `pull_request` | `ready` | `pull` | reserved, only once drafts exist |
  98. 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.
  99. ## 5. Back-channel (#44, #46). Every route: `Authorization: Bearer gat_…`; answers are JSON
  100. | route | request | answer |
  101. |---|---|---|
  102. | `GET {api}/pulls?state=open\|all&cursor=&limit=` | — | `{"pulls":[pull…],"next":"<cursor>"\|null}` |
  103. | `GET {api}/pulls/N` | — | `pull` |
  104. | `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"}]}` |
  105. | `GET {api}/pulls/N/reviews?cursor=&limit=` | — | `{"reviews":[{"id","verdict","head","body","created"}…],"next":…}` (your own reviews, newest first) |
  106. | `GET {api}/pulls/N/threads?cursor=&limit=` | — | `{"threads":[thread…],"next":…}` (your own threads only) |
  107. | `POST {api}/pulls/N/threads/:id/replies` | `{"body":"…"}` | `201 {"comment":comment}` |
  108. | `POST {api}/pulls/N/threads/:id/resolve` | `{"resolved":true\|false}` | `200 {"thread":thread}` |
  109. | `GET {api}/pulls/N/comments?cursor=&limit=` | — | `{"comments":[comment…],"next":…}` (the PR's conversation, oldest first) |
  110. | `POST {api}/pulls/N/comments` | `{"body":"…"}` | `201 {"comment":comment}` (in the PR's conversation) |
  111. | `PUT {api}/statuses/<sha>` | `{"context":"kunibert","state":"pending"\|"success"\|"failure"\|"error","title":"…","summary":"…","url":null}` | `200 {"status":{…,"updated"}}` |
  112. | `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 |
  113. | `GET {api}/deliveries?cursor=` | — | `{"deliveries":[{"id","event","action","time","status":"delivered"\|"pending"\|"failed","tries","lastCode"}],"next":…}` |
  114. | `POST {api}/deliveries/:id/redeliver` | — | `202` |
  115. - `status` = `{"context","state","title","summary","url","head","updated","app":{"id","name"}}`. Reading shows all apps' statuses; an app writes only its own.
  116. - `thread` = `{"id","path","line","head":"<sha it was written on>","outdated":bool,"resolved":bool,"comments":[comment…]}`.
  117. - `comment` = `{"id","seq","author":{"type":"app"\|"user","id","name"},"body","created"}`. `seq` is an integer that grows strictly
  118. within a repo (over all comments of all PRs), so it works as a watermark. `id` is an opaque string: do not compare it.
  119. `created` has milliseconds (`2026-11-02T06:00:00.123Z`). Lists are ordered by `seq`.
  120. - **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`;
  121. 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 PR
  122. gets `409 stale_head` (nothing stored). Your current verdict on a PR is your latest review; a `comment` review does not
  123. clear 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 a
  124. context line). `path` is the new-side path, after a rename. `<mergeBase>` is computed when the review arrives:
  125. `git merge-base <current target tip> <head>`, not the value from an older event.
  126. If any inline comment points at another line, the WHOLE review is refused: `422 invalid` with
  127. `details: [{"index":2,"path":"…","line":7,"reason":"line_not_in_diff"\|"no_such_file"}]`. Send it again without those comments,
  128. 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.
  129. - **Status**: one status per (head sha, context). `error` = the app could not do its check (shown apart from `failure`;
  130. 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"
  131. for the context, the Merge button stays off until that status is `success` on the PR's current head (#46).
  132. - Bodies (review, comment, status summary) are Markdown, up to 65,536 characters. Gitoria shows them escaped (no raw HTML).
  133. - Paging: pass `next` as `cursor`. `limit` defaults to 50 and is at most 100.
  134. ## 6. Errors and limits (#47)
  135. Every error: `{"error":{"code":"…","message":"…","details":…}}`.
  136. | HTTP | code |
  137. |---|---|
  138. | 400 | `invalid_json`, `invalid_url` |
  139. | 401 | `unauthorized` (missing, unknown or expired token: get a new token) |
  140. | 404 | `not_found` (also: another repo, a removed app) |
  141. | 409 | `stale_head` |
  142. | 413 | `too_large` (request body over 1 MB) |
  143. | 422 | `invalid` (with `details`) |
  144. | 429 | `rate_limited`, with a `Retry-After` header (seconds) |
  145. Limits per app: 300 reads and 60 writes per minute.
  146. ## Changelog
  147. - v0 2026-11-02: first contract (Gitoria), before any code.
  148. - v1 2026-11-02 (Kunibert's points): `comment.seq` watermark and ms `created`; `GET …/pulls/N/comments`; exact diff for the line check
  149. (`-M -U3`, new-side path, merge-base at review time); explicit `project#n` listed without a tickets connection; common body on `ping`;
  150. status state `error`.
  151. - v1.3 2026-11-02 (Kunibert's wish, decided by Vinzenz): `GET {api}/statuses/<sha>` lists all contexts on a sha; `status` carries `app`;
  152. the pull object gets `statuses` (current head, all apps) and `required` (read-only). Built with #47.
  153. - v1.2.1 2026-11-02: clarified the HMAC key = the secret string's ASCII bytes (not hex-decoded).
  154. - v1.2 2026-11-02 (Vinzenz): review on any earlier head (outdated threads, 409 only for a foreign sha); bodies 65,536; current verdict
  155. = latest review, `GET …/pulls/N/reviews`; shallow fetch (v2 depth/deepen) stated; open points closed (retries, `context`, limits, drafts).
  156. - v1.1 2026-11-02 (Apollo): the configured HyperConductor bases are allowed exactly as written, plain http included; https only for worldapi.org.

Branches

Latest commits

  • 1c3fa842gitoria#45 #47: worker briefsmre
  • 0a46bf7dgitoria#47: apps API contract v1.3 (status read route, statuses + required on the pull)mre
  • 3373f7c3gitoria#38: push API contract v1.2.1, reviews, worker briefs and logmre
  • 34143d2cgitoria#46: closing brace of appComments lost in the mergemre
  • 54927c9cgitoria#46: PR statuses and required checks before merge (merge w46-status)mre
  • 187e1871gitoria#46: badges without decorative bordersmre
  • 6b022798gitoria#46: tests/status.mjs in GATESmre
  • c6162f00gitoria#46: status badges on /pulls, Required before merge in Settingsmre
  • e5a0b64egitoria#46: PUT statuses/<sha>, required contexts block the merge on the servermre
  • 26a140e3gitoria#44: reviews, inline threads, replies, conversation for apps (merge w44-reviews)mre
  • 888dbf87gitoria#44: unknown API paths answer the contract's JSON 404 and sit above the page routesmre
  • ea587d90gitoria#44: reviews with inline threads, replies, resolve and the PR conversation for apps (lib/reviews.hl, tests/reviews.mjs)mre
  • 49533a50gitoria#43: signed events to connected apps (merge w43-events)mre
  • 1a1e87d5gitoria#43: the sender never writes the apps row; Settings shows the newest deliverymre
  • cd442066gitoria#43: app-auth unit test reads the apps table from its new filemre
  • 5dccbe38gitoria#43: in-flight rows are left alone by the other process; gate in deploy.sh; READMEmre
  • 8c8805f3merge main (gitoria#42), delivery log and redelivery routes, sweep fixmre
  • 5a996ea8gitoria#42: short-lived app tokens, read-only git fetch with them (merge w42-app-token)mre
  • afdb0cbfgitoria#43: rename the moved helper (name clash in the browser build)mre
  • 15e01e94gitoria#43: tests/events.mjs and its probemre