forge
Clone
https://git.tahinli.com/Tahinli/forge.git
0 watching·0 forks·Rust·AGPL-3.0·9.7 MiB·37e985d·Tahinli·4 hours ago·363 commits
| 1 | # forge JSON API |
| 2 | |
| 3 | A small JSON API under `/api/v1`. It is forge's own; it does not copy another forge's API. |
| 4 | The examples use the base URL from `config/forge.toml`, `http://127.0.0.1:7660`. |
| 5 | |
| 6 | ## Authentication |
| 7 | |
| 8 | Requests authenticate with a personal access token. Create one at **Settings → Tokens** |
| 9 | (`/settings/tokens`). Send it any of these ways: |
| 10 | |
| 11 | ``` |
| 12 | Authorization: Bearer forge_... |
| 13 | Authorization: token forge_... |
| 14 | Authorization: Basic base64(any-name:forge_...) |
| 15 | ``` |
| 16 | |
| 17 | The Basic form takes the token as the password and ignores the user name. |
| 18 | |
| 19 | - A token needs the `repo` scope. It is the only scope the token form offers. A token without it |
| 20 | gets `403`. |
| 21 | - Session cookies are never read by this API. A browser that is logged in cannot call these |
| 22 | routes by itself, so CSRF does not apply. Without a token, a write gets `401`. |
| 23 | - Anonymous reads of public repos need no token. Anonymous calls that cannot read a repo get |
| 24 | `401`, as git does, so a private repo looks the same as a missing one to an anonymous caller. |
| 25 | A signed-in user with no access to a repo gets `404`. |
| 26 | - Failed authentication counts against the client IP. It shares the budget git authentication |
| 27 | uses, so after too many failures the API answers `429` with `Retry-After`. |
| 28 | |
| 29 | Errors are JSON: `{"error": "message"}`. |
| 30 | |
| 31 | | Status | Meaning | |
| 32 | | --- | --- | |
| 33 | | 400 | The body is not valid JSON. | |
| 34 | | 401 | No token, an unknown or expired token, or an anonymous caller who cannot use the repo. | |
| 35 | | 403 | The token lacks the `repo` scope, or the user has read-only access and tried to write. | |
| 36 | | 404 | The repo or the commit does not exist, or the signed-in user cannot see the repo. | |
| 37 | | 413 | The request body is too large: over 16 KiB for statuses, over 64 KiB for release JSON, or an asset over `releases.max_asset_mb`. | |
| 38 | | 422 | A field has an invalid value, or `sha` is not a full commit id. | |
| 39 | | 429 | Too many failed authentication attempts from this IP. | |
| 40 | |
| 41 | ## Commit statuses |
| 42 | |
| 43 | A status reports the result of one check (for example a CI job) on one commit. Each commit can |
| 44 | have one status per **context**. Reporting again for the same context replaces the earlier |
| 45 | report, so the latest report wins. |
| 46 | |
| 47 | A status has these fields: |
| 48 | |
| 49 | | Field | Notes | |
| 50 | | --- | --- | |
| 51 | | `state` | `pending`, `success`, `failure` or `error`. | |
| 52 | | `context` | Name of the check, 1 to 255 characters. Defaults to `default`. | |
| 53 | | `description` | Optional, at most 1024 characters. | |
| 54 | | `target_url` | Optional, `http` or `https` only, at most 2048 bytes. Shown as a link. | |
| 55 | |
| 56 | The combined state of a commit is worked out from every context: |
| 57 | |
| 58 | - any `failure` or `error` gives `failure`; |
| 59 | - otherwise any `pending` gives `pending`; |
| 60 | - otherwise `success`; |
| 61 | - a commit with no statuses has `state: null`. |
| 62 | |
| 63 | ### Report a status |
| 64 | |
| 65 | `POST /api/v1/repos/{owner}/{repo}/statuses/{sha}` needs a token with write access to the repo. |
| 66 | `sha` is the full 40-character (or 64-character, SHA-256) commit id. Upper-case hex is accepted. |
| 67 | |
| 68 | ```sh |
| 69 | curl -X POST http://127.0.0.1:7660/api/v1/repos/tahinli/demo/statuses/$SHA \ |
| 70 | -H "Authorization: Bearer $FORGE_TOKEN" \ |
| 71 | -H "Content-Type: application/json" \ |
| 72 | -d '{"state": "success", "context": "ci/build", "description": "Passed in 42s", |
| 73 | "target_url": "https://ci.example.com/runs/7"}' |
| 74 | ``` |
| 75 | |
| 76 | Response, `201 Created`: |
| 77 | |
| 78 | ```json |
| 79 | { |
| 80 | "id": "01J...", |
| 81 | "sha": "0123...", |
| 82 | "context": "ci/build", |
| 83 | "state": "success", |
| 84 | "description": "Passed in 42s", |
| 85 | "target_url": "https://ci.example.com/runs/7", |
| 86 | "created_at": "2026-10-15T10:00:00Z", |
| 87 | "updated_at": "2026-10-15T10:00:00Z" |
| 88 | } |
| 89 | ``` |
| 90 | |
| 91 | Only `state` is required. A commit that is not in the repo gets `404`. |
| 92 | |
| 93 | ### Read the statuses of a commit |
| 94 | |
| 95 | `GET /api/v1/repos/{owner}/{repo}/commits/{sha}/statuses` needs no token for a public repo. A |
| 96 | token is needed for a private one. |
| 97 | |
| 98 | ```sh |
| 99 | curl http://127.0.0.1:7660/api/v1/repos/tahinli/demo/commits/$SHA/statuses \ |
| 100 | -H "Authorization: Bearer $FORGE_TOKEN" |
| 101 | ``` |
| 102 | |
| 103 | Response, `200 OK`: |
| 104 | |
| 105 | ```json |
| 106 | { |
| 107 | "state": "failure", |
| 108 | "statuses": [ |
| 109 | { |
| 110 | "id": "01J...", |
| 111 | "sha": "0123...", |
| 112 | "context": "ci/build", |
| 113 | "state": "failure", |
| 114 | "description": "2 tests failed", |
| 115 | "target_url": "https://ci.example.com/runs/8", |
| 116 | "created_at": "2026-10-15T10:05:00Z", |
| 117 | "updated_at": "2026-10-15T10:05:00Z" |
| 118 | }, |
| 119 | { |
| 120 | "id": "01J...", |
| 121 | "sha": "0123...", |
| 122 | "context": "lint", |
| 123 | "state": "success", |
| 124 | "description": null, |
| 125 | "target_url": null, |
| 126 | "created_at": "2026-10-15T10:01:00Z", |
| 127 | "updated_at": "2026-10-15T10:01:00Z" |
| 128 | } |
| 129 | ] |
| 130 | } |
| 131 | ``` |
| 132 | |
| 133 | `statuses` is ordered by `context`. The list is empty and `state` is `null` when nothing has |
| 134 | reported on the commit. |
| 135 | |
| 136 | ## Releases |
| 137 | |
| 138 | A release names a git tag and carries notes and files. Scripts and CI jobs can manage releases |
| 139 | with a token. The same release can instead be published from a tag run with no script: see the |
| 140 | declarative `[release]` table in `docs/ci.md`. |
| 141 | |
| 142 | | Method | Path | Access | Answers | |
| 143 | | --- | --- | --- | --- | |
| 144 | | `GET` | `/api/v1/repos/{owner}/{repo}/releases` | public, or a token with read access | `200`, the list, newest first | |
| 145 | | `GET` | `/api/v1/repos/{owner}/{repo}/releases/tags/{tag}` | public, or a token with read access | `200`, one release | |
| 146 | | `POST` | `/api/v1/repos/{owner}/{repo}/releases` | write | `201`, the new release | |
| 147 | | `PATCH` | `/api/v1/repos/{owner}/{repo}/releases/{id}` | write | `200`, the release | |
| 148 | | `DELETE` | `/api/v1/repos/{owner}/{repo}/releases/{id}` | write | `204` | |
| 149 | | `PUT` | `/api/v1/repos/{owner}/{repo}/releases/{id}/assets/{asset}` | write | `201` when new, `200` when it replaces a file of that name | |
| 150 | | `DELETE` | `/api/v1/repos/{owner}/{repo}/releases/{id}/assets/{asset}` | write | `204` | |
| 151 | |
| 152 | In the asset routes `{asset}` is the file name for `PUT` and the asset `id` (from the release JSON) |
| 153 | for `DELETE`. `GET /api/v1/repos/{owner}/{repo}/releases/{id}/assets/{asset}` answers one asset, with the asset `id` as `{asset}`. |
| 154 | A release `{id}` and an asset `id` are the integers in the JSON; the ULID in `uid` works too. The list takes `page` and `limit` and always |
| 155 | sends `X-Total-Count` and `Link`; without them it returns everything. |
| 156 | |
| 157 | Drafts are visible only to callers with write access: they are left out of the list, and fetching |
| 158 | one by tag gets `404` for anyone else. A release `{id}` must belong to the repo in the path; |
| 159 | another repo's id gets `404`. |
| 160 | |
| 161 | A release has these fields: |
| 162 | |
| 163 | | Field | Notes | |
| 164 | | --- | --- | |
| 165 | | `tag` | A git tag name: letters, digits and `. _ - +`. | |
| 166 | | `target` | The commit the tag names. Set when the release is made. | |
| 167 | | `title` | 1 to 200 characters. Defaults to the tag on create. | |
| 168 | | `body` | Notes, at most 64 KiB of text. Defaults to empty. | |
| 169 | | `draft` | A draft is hidden from readers until it is published. Defaults to `false`. | |
| 170 | | `prerelease` | Defaults to `false`. | |
| 171 | |
| 172 | ### Release JSON |
| 173 | |
| 174 | ```json |
| 175 | { |
| 176 | "id": 281474976710, |
| 177 | "uid": "01J...", |
| 178 | "tag": "v1.2.0", |
| 179 | "tag_name": "v1.2.0", |
| 180 | "target": "0123...", |
| 181 | "target_commitish": "0123...", |
| 182 | "title": "v1.2.0", |
| 183 | "name": "v1.2.0", |
| 184 | "body": "Built by CI", |
| 185 | "draft": false, |
| 186 | "prerelease": false, |
| 187 | "author": {"id": 5120043, "login": "tahinli", "username": "tahinli", "...": "..."}, |
| 188 | "author_name": "tahinli", |
| 189 | "created_at": "2026-10-19T10:00:00Z", |
| 190 | "published_at": "2026-10-19T10:00:00Z", |
| 191 | "html_url": "http://127.0.0.1:7660/tahinli/demo/releases/tag/v1.2.0", |
| 192 | "assets": [ |
| 193 | { |
| 194 | "id": 1099511627, |
| 195 | "uid": "01J...", |
| 196 | "name": "forge-linux-x86_64.tar.gz", |
| 197 | "size": 1048576, |
| 198 | "sha256": "9f86...", |
| 199 | "content_type": "application/gzip", |
| 200 | "download_count": 3, |
| 201 | "created_at": "2026-10-19T10:01:00Z", |
| 202 | "download_url": "http://127.0.0.1:7660/tahinli/demo/releases/download/v1.2.0/forge-linux-x86_64.tar.gz", |
| 203 | "browser_download_url": "http://127.0.0.1:7660/tahinli/demo/releases/download/v1.2.0/forge-linux-x86_64.tar.gz" |
| 204 | } |
| 205 | ] |
| 206 | } |
| 207 | ``` |
| 208 | |
| 209 | `published_at` is `null` for a draft. `download_url` (also sent as `browser_download_url`) is the |
| 210 | web download route, so it works with the same visibility rules as the release page, and it |
| 211 | accepts a token (`Authorization: token`, Bearer or Basic) for private repos. `id` is an integer and `author` is a user object. `uid` and `author_name` keep the forge values. |
| 212 | |
| 213 | ### Create a release |
| 214 | |
| 215 | `POST /api/v1/repos/{owner}/{repo}/releases` takes JSON. Only `tag` is required. `target` is a |
| 216 | branch name or a full commit id; it is used only when the tag does not exist yet, and it defaults |
| 217 | to the default branch. An existing tag is adopted as it is. A second release for the same tag |
| 218 | gets `409`. |
| 219 | |
| 220 | ```sh |
| 221 | curl -X POST http://127.0.0.1:7660/api/v1/repos/tahinli/demo/releases \ |
| 222 | -H "Authorization: Bearer $FORGE_TOKEN" \ |
| 223 | -H "Content-Type: application/json" \ |
| 224 | -d '{"tag": "v1.2.0", "target": "main", "title": "Version 1.2", "body": "Fixes.", "draft": true}' |
| 225 | ``` |
| 226 | |
| 227 | ### Edit a release |
| 228 | |
| 229 | `PATCH .../releases/{id}` changes only the fields it sends. Omitted fields keep their values. |
| 230 | `tag` renames the release. A published release keeps its tag, so it must pick a new one; a draft |
| 231 | can also move its tag with `target`. |
| 232 | |
| 233 | ```sh |
| 234 | curl -X PATCH http://127.0.0.1:7660/api/v1/repos/tahinli/demo/releases/$ID \ |
| 235 | -H "Authorization: Bearer $FORGE_TOKEN" \ |
| 236 | -H "Content-Type: application/json" \ |
| 237 | -d '{"draft": false}' |
| 238 | ``` |
| 239 | |
| 240 | ### Get releases |
| 241 | |
| 242 | ```sh |
| 243 | curl http://127.0.0.1:7660/api/v1/repos/tahinli/demo/releases/tags/v1.2.0 |
| 244 | ``` |
| 245 | |
| 246 | ### Upload an asset |
| 247 | |
| 248 | `PUT .../releases/{id}/assets/{asset}` sends the file as the raw request body. The body is |
| 249 | streamed to storage, so large files are not held in memory. `Content-Type` is stored as the |
| 250 | asset's content type; a missing one is stored as `application/octet-stream`. The name may use |
| 251 | letters, digits and `. _ - +`. An upload over `releases.max_asset_mb` gets `413`. Uploading the |
| 252 | same name again replaces the file and keeps its download count. |
| 253 | |
| 254 | ```sh |
| 255 | curl -X PUT http://127.0.0.1:7660/api/v1/repos/tahinli/demo/releases/$ID/assets/forge-linux-x86_64.tar.gz \ |
| 256 | -H "Authorization: Bearer $FORGE_TOKEN" \ |
| 257 | -H "Content-Type: application/gzip" \ |
| 258 | --data-binary @dist/forge-linux-x86_64.tar.gz |
| 259 | ``` |
| 260 | |
| 261 | ### Delete an asset or a release |
| 262 | |
| 263 | ```sh |
| 264 | curl -X DELETE http://127.0.0.1:7660/api/v1/repos/tahinli/demo/releases/$ID/assets/$ASSET_ID \ |
| 265 | -H "Authorization: Bearer $FORGE_TOKEN" |
| 266 | |
| 267 | curl -X DELETE http://127.0.0.1:7660/api/v1/repos/tahinli/demo/releases/$ID \ |
| 268 | -H "Authorization: Bearer $FORGE_TOKEN" |
| 269 | ``` |
| 270 | |
| 271 | Deleting a release removes its files. The git tag is kept. |
| 272 | |
| 273 | ### CI job script |
| 274 | |
| 275 | A job that builds artifacts and publishes them as a release. The token is a repo secret named |
| 276 | `FORGE_TOKEN` with the `repo` scope, and the job exposes it as an environment variable. The |
| 277 | release is created as a draft, the files are uploaded, and only then is it published, so readers |
| 278 | never see a half-filled release. The script needs `curl` and `jq`. |
| 279 | |
| 280 | ```sh |
| 281 | #!/bin/sh |
| 282 | set -eu |
| 283 | |
| 284 | BASE="http://127.0.0.1:7660/api/v1/repos/tahinli/demo" |
| 285 | AUTH="Authorization: Bearer $FORGE_TOKEN" |
| 286 | TAG="v1.2.0" |
| 287 | SHA="$(git rev-parse HEAD)" |
| 288 | |
| 289 | cargo build --release |
| 290 | mkdir -p dist |
| 291 | cp target/release/forge dist/forge-linux-x86_64 |
| 292 | tar -C dist -czf dist/forge-linux-x86_64.tar.gz forge-linux-x86_64 |
| 293 | |
| 294 | # 1. Create the release as a draft on this commit. |
| 295 | ID="$(curl -fsS -X POST "$BASE/releases" -H "$AUTH" -H "Content-Type: application/json" \ |
| 296 | -d "{\"tag\": \"$TAG\", \"target\": \"$SHA\", \"draft\": true}" | jq -r .id)" |
| 297 | |
| 298 | # 2. Upload each file. A re-run replaces files of the same name. |
| 299 | for file in dist/forge-linux-x86_64.tar.gz; do |
| 300 | curl -fsS -X PUT "$BASE/releases/$ID/assets/$(basename "$file")" -H "$AUTH" \ |
| 301 | -H "Content-Type: application/octet-stream" --data-binary "@$file" > /dev/null |
| 302 | done |
| 303 | |
| 304 | # 3. Publish. |
| 305 | curl -fsS -X PATCH "$BASE/releases/$ID" -H "$AUTH" -H "Content-Type: application/json" \ |
| 306 | -d '{"draft": false}' > /dev/null |
| 307 | echo "published $TAG" |
| 308 | ``` |
| 309 | |
| 310 | If the job runs on the tag itself, the tag already exists and `target` is ignored. A simpler |
| 311 | alternative needs no script at all: a `[release]` table in `ci.toml` publishes the release from |
| 312 | a tag run and attaches the selected artifacts. See `docs/ci.md`, "Releases". |
| 313 | |
| 314 | ## Tags |
| 315 | |
| 316 | ``` |
| 317 | POST /api/v1/repos/{owner}/{repo}/tags {"tag_name": "v0.5.0", "target": "main"} |
| 318 | GET /api/v1/repos/{owner}/{repo}/tags?limit=<n> |
| 319 | ``` |
| 320 | |
| 321 | Creating needs write access: a token with `repo` scope from a user who can push. `target` is a |
| 322 | branch name or a full commit sha and defaults to the default branch. The tag is lightweight. The |
| 323 | answer is `201 {"name": "v0.5.0", "sha": "<commit>"}`, `409` when the tag exists, `422` for a bad |
| 324 | name or target. A tag made here runs the same post-update path as a `git push` of the tag, so CI |
| 325 | `[on] tags`, `[release]`, webhooks and push mirrors all fire. |
| 326 | |
| 327 | The list needs read access, is newest commit first, and gives `[{"name", "sha"}]`. `limit` |
| 328 | defaults to 30, at most 100. |
| 329 | |
| 330 | ## CI runs |
| 331 | |
| 332 | Read-only. The same read access as the run page: anonymous on a public repository, a token with |
| 333 | `repo` scope otherwise. |
| 334 | |
| 335 | ``` |
| 336 | GET /api/v1/repos/{owner}/{repo}/ci/runs?ref=<ref>&limit=<n> |
| 337 | GET /api/v1/repos/{owner}/{repo}/ci/runs/{run_number} |
| 338 | GET /api/v1/repos/{owner}/{repo}/ci/runs/{run_number}/events |
| 339 | ``` |
| 340 | |
| 341 | The list is newest first. `ref` is optional and matches a full ref (`refs/tags/v1.2.0`) or a short |
| 342 | branch or tag name (`v1.2.0`). `limit` defaults to 20, at most 100. |
| 343 | |
| 344 | A run: |
| 345 | |
| 346 | ```json |
| 347 | { |
| 348 | "number": 42, |
| 349 | "ref": "refs/tags/v1.2.0", |
| 350 | "sha": "4f1c…", |
| 351 | "trigger": "push", |
| 352 | "state": "running", |
| 353 | "created_at": "2026-12-01T19:00:00Z", |
| 354 | "started_at": "2026-12-01T19:00:04Z", |
| 355 | "finished_at": null, |
| 356 | "jobs": [ |
| 357 | { |
| 358 | "id": "01k…", |
| 359 | "name": "build (x86_64-v3-linux)", |
| 360 | "state": "running", |
| 361 | "started_at": "2026-12-01T19:00:04Z", |
| 362 | "finished_at": null, |
| 363 | "exit_code": null, |
| 364 | "steps": [ |
| 365 | { "name": "fetch", "state": "success", "started_at": "…", "finished_at": "…" }, |
| 366 | { "name": "build", "state": "running", "started_at": "…", "finished_at": null } |
| 367 | ] |
| 368 | } |
| 369 | ] |
| 370 | } |
| 371 | ``` |
| 372 | |
| 373 | Run states: `waiting_approval`, `queued`, `running`, `success`, `failure`, `cancelled`, `error`. |
| 374 | Job states: `blocked` (waiting on `needs`), `queued`, `running`, `success`, `failure`, `cancelled`, |
| 375 | `error`, `skipped`. Step states: `running`, `success`, `failure`, `cancelled`. Steps come from `::group::` |
| 376 | markers in the job log. `started_at` of the run is its first job's start. |
| 377 | |
| 378 | `events` is a server-sent event stream. It sends `event: run` with the whole run JSON on connect and |
| 379 | again whenever it changes, and ends after the run reaches a finished state. Close it then: a |
| 380 | browser `EventSource` would otherwise reconnect and get the final state again. |
| 381 | |
| 382 | A job's log streams from `GET /{owner}/{repo}/ci/jobs/{job_id}/events?plain=1`, with the same access |
| 383 | rules. `event: log` carries whole lines of plain text (ANSI codes stripped); its `id` is the byte |
| 384 | offset after the chunk, so reconnecting with `Last-Event-ID` (or `?from=<offset>`) resumes without |
| 385 | gaps or repeats. `event: changed` tells a browser to re-render the page and can be ignored by an API |
| 386 | client: job state comes from the run stream. The stream stays open after the job ends; close it |
| 387 | when the run JSON shows the job finished. `GET /{owner}/{repo}/ci/jobs/{job_id}/log.txt` returns the |
| 388 | whole log at once. |