# Linear

A workspace's issues from the back end, over Linear's GraphQL API: issues
listed with Linear's own filters by cursor, got by their identifier, made
and changed; comments; teams and their workflow states; and Linear's
webhooks, checked and each delivery done once in a
[job](back-end.md#later-job-fn). An [extension](../extensions/README.md):
`extensions/linear/`. Back to the [map](README.md).

```mote
use "pkg:linear/linear.mote"

// A report from the program's own page, made an issue in the team's triage.
server fn report(title: string, words: string): string requires signedIn limit 5 per hour
  let me = auth.me()?
  let i = linearIssueCreate({team: env("LINEAR_TEAM"), title, description: `${words}\n\nFrom ${me?.email ?? "someone"}.`, priority: 3}, "")?
  return i.identifier

// What is still open, every page of it.
server fn open(): string[] requires signedIn
  return linearAllIssues({team: {key: {eq: "ENG"}}, state: {type: {nin: ["completed", "canceled"]}}}, 500, "")?.map(i => `${i.identifier} ${i.title}`)

// Told when an issue changes in Linear: the person who reported it, when it is done.
fn linearIssueChanged(action: string, i: LinearIssue): bool
  if action == "update" && i.state.type == "completed"
    log.info("done", {issue: i.identifier})
  return true
```

```toml
[packages]
linear = "../mote/extensions/linear"
graphql = "../mote/extensions/graphql"     # its requests go through pkg:graphql
```

```sh
LINEAR_KEY=lin_api_… LINEAR_WEBHOOK_SECRET=lin_wh_… LINEAR_TEAM=<team id> bun dist/server/main.js
```

**Not met Linear.** Written from Linear's GraphQL schema and webhook
documentation as of 2026, and run against a stand-in
(`tests/svc_standin.mjs work`) that knows the queries the extension sends,
answers in Linear's shapes and errors, and is posted deliveries signed as
Linear signs them. The steps to check it with a real workspace are
[below](#checking-it-against-linear).

## The settings

| | |
|---|---|
| `LINEAR_KEY` | A personal API key (`lin_api_…`, Settings → Security & access → Personal API keys), a [secret](back-end.md#secrets): sent as the `Authorization` header as it is. Every call takes a `token` last: `""` for this key, or a person's OAuth access token (sent as `Bearer …`; OAuth is not this extension's). |
| `LINEAR_WEBHOOK_SECRET` | The webhook's signing secret (Settings → API → Webhooks), a secret. |
| `LINEAR_WAIT` | The longest wait for a spent limit to reset, in seconds: 30 unsaid. |
| `LINEAR_API` | Another address for the API (a stand-in). |

## The calls

| | |
|---|---|
| `linearIssues(filter, size, cursor, token)` | One page (50 unsaid, 250 at most), the latest changed first: `LinearIssues {issues, next}`. `filter` is Linear's `IssueFilter` (none for all). |
| `linearAllIssues(filter, most, token)` | Every page, up to `most`. |
| `linearIssue(id, token)` | One, by its id or identifier (`ENG-12`). |
| `linearIssueCreate(n, token)` | `n` is `LinearNew {team, title, description?, priority?, assignee?, state?, labels?, estimate?, due?, parent?, project?}` (ids; `description` Markdown). Only what is said is sent. |
| `linearIssueUpdate(id, c, token)` | `c` is `LinearChange`, each field optional. |
| `linearComment(issue, body, token)` · `linearComments(issue, cursor, token)` | A comment (Markdown) on an issue (its id); an issue's comments, 100 a page. |
| `linearTeams(token)` · `linearStates(team, token)` · `linearMe(token)` | The teams; a team's workflow states (`{id, name, type}`, type `backlog`, `unstarted`, `started`, `completed`, `canceled`, `triage`); who the key is. |
| `linearQuery(query, variables, token)` | Any other GraphQL request: its `data`, or the failure its first error is. |

`LinearIssue` is `{id, identifier, title, description, url, priority (0
none, 1 urgent … 4 low), estimate, due, state, team, teamKey, assignee,
assigneeName, labels, created, updated}`.

| Kind | |
|---|---|
| `"auth"` | The key is wrong or may not do this. |
| `"missing"` | No such issue, team, state or person. |
| `"invalid"` | Linear did not take what was asked (`err.data.path` names the field). |
| `"limited"` | The key's limit is spent (requests, or complexity, an hour): `err.data.reset` is when it resets. |
| `"linear"` · `"offline"` | Linear broke; no answer. |

Linear answers an error with a 200 or a 400 and a list of `errors`; the
first one's `userPresentableMessage` (else its `message`) is the sentence,
and its `extensions.code` the kind.

## Limits

Linear counts requests and the complexity of the queries, an hour, for each
key. A spent limit (`RATELIMITED`) holds every later call of the back end's
(`LinearHold`) until `X-RateLimit-Requests-Reset` (else a minute); a call
waits that out once when it is within `LINEAR_WAIT`, and fails `"limited"`
otherwise. The queries here ask only the fields they read, 50 to 250 nodes a
page, to keep the complexity low.

## Webhooks

`POST /linear/webhook`: the delivery's `Linear-Signature` must be the hex
HMAC-SHA256 of the body under `LINEAR_WEBHOOK_SECRET` (compared in constant
time by [`webhook.check`](back-end.md#webhooks)'s `"linear"`), and its `webhookTimestamp` within a minute of now (a replay
is refused). Each delivery is kept once by its `Linear-Delivery` id, answered
at once, and done in a job (`linearDeliver`, tried again if a hook fails):

| | |
|---|---|
| `fn linearIssueChanged(action: string, i: LinearIssue): bool` | An issue made, changed or removed (`"create"`, `"update"`, `"remove"`). |
| `fn linearEvent(e: LinearEventIn): bool` | Every delivery: `{action, type, data, updatedFrom, url, actor, at}` (`type` `"Issue"`, `"Comment"`, `"Project"`, …). |

At Linear: Settings → API → Webhooks → a URL `<site>/linear/webhook`, the
resources to hear (Issues, Comments, …); its signing secret is
`LINEAR_WEBHOOK_SECRET`.

## Left out

Projects, cycles, initiatives, documents and customers beyond what
`linearQuery` can ask; attachments and file uploads; issue relations;
subscriptions over a socket (Linear has none for this); OAuth (each call
takes a token).

**Unsure.** The error codes (`RATELIMITED`, `AUTHENTICATION_ERROR`,
`ENTITY_NOT_FOUND`, `INVALID_INPUT`) and the rate limit headers are as
Linear documented them when this was written; a code not known is
`"invalid"`.

## Checking it against Linear

1. A personal API key as `LINEAR_KEY`; `linearMe("")` is you,
   `linearTeams("")` your teams.
2. `linearIssueCreate({team: <id>, title: "Mote test", description: "**hi**"},
   "")`: the issue is in the team, its description bold.
3. `linearIssueUpdate(<its id>, {state: <a completed state's id>}, "")`, then
   `linearAllIssues({state: {type: {nin: ["completed", "canceled"]}}}, 500,
   "")` leaves it out.
4. `linearComment(<its id>, "On it", "")`; `linearComments` has it.
5. A webhook at Linear to `https://<a public address>/linear/webhook`; move
   the issue by hand: `linearIssueChanged("update", …)` is told, once.

Recorded in `tests/expect/real_svc_work.txt` (`tests/all.sh real_svc_work`):
teams, states and who; issues made, moved, listed open, three a page; a
comment; a team and an issue not there; another key refused; a spent limit
waited out; deliveries signed, again, unsigned and stale; and the
extension's own `mote test`.
