gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitfefaedd2fefaedd2log: tracker PR-only, HL25 startedmrefefaedd2/docs/apps-api.md

18.4 KB

  1. # Gitoria apps API: the contract (gitoria#38, children #40–#47)
  2. Version 1.5.1, 2026-11-05. 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.
  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": [{"app":{"id":"…","name":"Kunibert"},"context":"kunibert"}] }
  75. ```
  76. - `statuses` (v1.3): every status on the CURRENT `head`, from every app (see `status` in §5).
  77. - `required` (v1.4): what the owner marked "required before merge", each as app + context. 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. - `outdated` = the thread was written on an older head than the PR's current one (`thread.head != pull.head`), even when its
  117. file and line did not change. It is not line tracking; an app that wants that compares the file itself.
  118. - `thread` = `{"id","path","line","head":"<sha it was written on>","outdated":bool,"resolved":bool,"comments":[comment…]}`.
  119. - `comment` = `{"id","seq","author":{"type":"app"\|"user","id","name"},"body","created"}`. `seq` is an integer that grows strictly
  120. within a repo (over all comments of all PRs), so it works as a watermark. `id` is an opaque string: do not compare it.
  121. `created` has milliseconds (`2026-11-02T06:00:00.123Z`). Lists are ordered by `seq`.
  122. - **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`;
  123. 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
  124. gets `409 stale_head` (nothing stored). Your current verdict on a PR is your latest review; a `comment` review does not
  125. 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
  126. context line). `path` is the new-side path, after a rename. `<mergeBase>` is computed when the review arrives:
  127. `git merge-base <current target tip> <head>`, not the value from an older event.
  128. If any inline comment points at another line, the WHOLE review is refused: `422 invalid` with
  129. `details: [{"index":2,"path":"…","line":7,"reason":"line_not_in_diff"\|"no_such_file"}]`. Send it again without those comments,
  130. 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.
  131. - **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`;
  132. 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"
  133. for that app + context, the Merge button stays off until that status is `success` on the PR's current head (#46).
  134. - Bodies (review, comment, status summary) are Markdown, up to 65,536 characters. Gitoria shows them escaped (no raw HTML).
  135. - Paging: pass `next` as `cursor`. `limit` defaults to 50 and is at most 100.
  136. ## 6. Errors and limits (#47)
  137. Every error: `{"error":{"code":"…","message":"…","details":…}}`.
  138. | HTTP | code |
  139. |---|---|
  140. | 400 | `invalid_json`, `invalid_url` |
  141. | 401 | `unauthorized` (missing, unknown or expired token: get a new token) |
  142. | 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) |
  143. | 404 | `not_found` (also: another repo, a removed app) |
  144. | 409 | `stale_head`, `conflict` (merge refusal, with `details.reason`; see §7) |
  145. | 413 | `too_large` (request body over 1 MB) |
  146. | 422 | `invalid`, `checks_failed` (with `details`; see §7) |
  147. | 429 | `rate_limited`, with a `Retry-After` header (seconds) |
  148. Limits per app: 300 reads and 60 writes per minute.
  149. ## 7. User tokens (gtr_) (v1.5.1: #48, W49)
  150. 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
  151. (owner, repo member or organization member). Member reads never reveal whether a repo or a PR exists to non-members: a valid
  152. token of a non-member gets 404 `not_found`, also on public repos. This is on purpose, not a bug.
  153. Reads (W49, live dab74b0): `GET {api}/pulls`, `{api}/pulls/N`, `{api}/pulls/N/reviews`, `{api}/pulls/N/threads`,
  154. `{api}/pulls/N/comments`, `{api}/statuses/<sha>`. Shapes, order and paging are the same as for apps. A user sees every app's
  155. reviews and threads; an app still sees only its own. User-token reads are not rate-limited by the apps API (the app limits of §6
  156. count apps only). Back-channel writes, `GET {api}/deliveries`, redelivery and `POST {api}/apps/token` stay app-only.
  157. Merge (#48, live 1a687b1): `POST {api}/pulls/N/merge`, `Authorization: Bearer gtr_…`. Body optional: empty, or
  158. `{"head":"<40 hex>"}` = the PR head the caller looked at. No other fields (always a merge commit with the message
  159. `Merge branch '<source>' into <target>`; no method, no message). Required checks (§4 `required`) must be `success` on the head.
  160. | HTTP | code | meaning |
  161. |---|---|---|
  162. | 200 | — | `{"merged":true,"mergeCommit":"<sha>","pull":{…}}`; `pull` has the shape of `GET {api}/pulls/N`, read after the merge |
  163. | 400 | `invalid_json` | the body is not a JSON object |
  164. | 401 | `unauthorized` | missing or unknown token ("a user access token is required") |
  165. | 403 | `forbidden` | an app token ("apps cannot merge"), or a user who may read but not push |
  166. | 404 | `not_found` | repo not visible to this user, or no such PR |
  167. | 405 | `invalid` | not POST |
  168. | 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) |
  169. | 413 | `too_large` | body over 1 MB |
  170. | 422 | `invalid` | `head` is not 40 hex characters (`details`: field `head`) |
  171. | 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>)`. |
  172. Event: `pull_request` action `merged`, `sender` = the merging user (never an app).
  173. A valid user token on an app-only route (any write of §5, deliveries, redelivery or `POST {api}/apps/token`) gets 403
  174. `app_token_required` with the message `this route needs an app token (gat_); user tokens (gtr_) can read pulls, reviews,
  175. threads, comments and statuses and merge with POST /pulls/N/merge`. Missing, unknown or expired tokens stay 401 `unauthorized`.
  176. ## Changelog
  177. - v1.5.1 2026-11-05: valid user tokens on app-only routes get 403 `app_token_required`; unmet required checks get 422
  178. `checks_failed` with app, context and state in `details`; every 409 merge refusal carries `details.reason`.
  179. - v1.5 2026-11-05 (reviewed by the external reviewer): §7 user tokens: members merge with gtr_ (#48, deploy 1a687b1) and read pulls, reviews,
  180. threads, comments, statuses (W49, deploy dab74b0); known deviations (401 for gtr_ on app routes, 409 for failed required checks) stated.
  181. - v0 2026-11-02: first contract (Gitoria), before any code.
  182. - v1 2026-11-02 (Kunibert's points): `comment.seq` watermark and ms `created`; `GET …/pulls/N/comments`; exact diff for the line check
  183. (`-M -U3`, new-side path, merge-base at review time); explicit `project#n` listed without a tickets connection; common body on `ping`;
  184. status state `error`.
  185. - 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;
  186. "required before merge" names app + context; `required` on the pull = `[{"app":{"id","name"},"context"}]`. A removed app's required entry
  187. stays unmet until the owner changes it. The PUT is unchanged.
  188. - v1.3.1 2026-11-02 (Kunibert's question): stated what `outdated` means (written on an older head), as built in #44.
  189. - 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`;
  190. the pull object gets `statuses` (current head, all apps) and `required` (read-only). Built with #47.
  191. - v1.2.1 2026-11-02: clarified the HMAC key = the secret string's ASCII bytes (not hex-decoded).
  192. - 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
  193. = latest review, `GET …/pulls/N/reviews`; shallow fetch (v2 depth/deepen) stated; open points closed (retries, `context`, limits, drafts).
  194. - 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

  • fefaedd2log: tracker PR-only, HL25 startedmre
  • 21aa57calog: parkmre
  • e9f950a8reports: G25c soakmre
  • a7783686log: deploy 9f41ffcmre
  • 9f41ffc1log: W30b mergedmre
  • bef635c1Merge branch 'w30b'mre
  • 15b996a5Org list: keyed rows by @id, newest first, concat instead of hand loopmre
  • c4f6d9e3reports: G25c growth huntmre
  • b88e39f1log: privacy cleanup donemre
  • bfd7826ddocs: neutral example organisation namemre
  • 01b71c2edocs: neutral wording in missions, reports and logmre
  • 3eeea733log: 2026-11-09 PR flow plan and membersmre
  • e7c2ff1fdocs: neutral name for the apps-api reviewmre
  • 120b7abdlog: step 3 queuedmre
  • 358474bflog: P50 livemre
  • 477d7bdcMerge branch 'p50'mre
  • 00efc06fP50 reportmre
  • e3f85afblog: commit mention slipmre
  • 4d1a4cf4log: #25 numbersmre
  • 1af0a60ddocs: H25 run() correctionmre