onetaskgraph_github_projects/lib.rs
1//! A stateless onetaskgraph source over one GitHub Projects v2 board.
2//!
3//! **A board is a container of projects, not a project.** Its own `title`,
4//! `shortDescription` and `readme` are never read as an item's fields and are never
5//! written: nothing in this source can rename the board a user configured.
6//!
7//! **A project is an issue and its tasks are that issue's sub-issues.** GitHub's schema
8//! decides that: `Issue` exposes `parent`, `subIssues` and `subIssuesSummary`, and
9//! `DraftIssue` exposes none of them. Creating an issue needs a `repositoryId`, and a
10//! board has none, so a write without [`GitHubProjectsConfig::repository`] is refused
11//! naming the field — but that repository is the *fallback*, not the home of every item.
12//!
13//! <!-- llmlint: ignore-block[contracts_have_one_source_or_a_drift_gate] The rule's one
14//! executable source is `GitHubProjectsSource::creation_target`; this is where a reader of
15//! the module meets it, and `tests/plugin.rs` drives every arm below against the loopback
16//! board and asserts on `createIssue`'s own `repositoryId`, so the prose cannot outlive a
17//! change to the rule. -->
18//! **Which repository an issue is created in is decided by the item's own `repositories`
19//! field, under one rule.** Exactly one entry names the repository the issue is created in:
20//! a task issue is where a person finds the work from the repository it changes, and one
21//! filed in a board's nominated repository is invisible from every other. Zero entries, or
22//! two or more, name none, so a task's or a document's issue is created in the repository
23//! its parent project's issue lives in — read from the board, or from this process's own
24//! record of a project it created earlier in the same command — and a project's issue, or
25//! a task or document written with no parent, is created in the configured `repository:`.
26//! What that rule refuses, it refuses before `createIssue`, so no issue is half-created. An
27//! existing issue is never moved: the update path leaves the issue where it is and records
28//! the list in the metadata slot when it differs, so the read side's derivation and the
29//! creation rule agree by construction.
30//! <!-- llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate] -->
31//!
32//! **A document is an ordinary issue whose title begins [`DESIGN_TITLE_PREFIX`].** A
33//! board has no document type and nothing but issues to hold one in, so the title is the
34//! discriminator and it is the whole of it. The title this source *reports* is the one a
35//! person wrote, with the prefix taken off — the same way the metadata slot is taken off
36//! the body so `content` is what the person wrote — and writing a document puts the prefix
37//! back, so a round trip returns the title that went in.
38//!
39//! **Telling a document from a project from a task.** The design prefix is read **first**:
40//! a document is never a project and never a task, whatever sub-issues it has or does not
41//! have. Only then does the rest apply — a board issue is a project when *either* it has
42//! sub-issues *or* it carries [`ItemKind::METADATA_KEY`]; otherwise it is a task. A
43//! sub-issue is always a task, whatever it carries. The marker is sufficient and never
44//! necessary: it is what makes an *empty* project — the state a project copy passes
45//! through between creating the project and filing its first task — readable as a
46//! project, while the sub-issue arm lets a person author a project on the board by hand
47//! with no knowledge of this product's metadata at all. Reading the prefix later than the
48//! sub-issue rule would make a design issue with no sub-issues an empty project, which is
49//! exactly the state that rule exists to catch. Pull requests are neither a project nor a
50//! task nor a document and are ignored.
51//!
52//! **A task's comments are its issue's comments.** They are read off `Issue.comments` and
53//! written with `addComment`, `updateIssueComment` and `deleteIssueComment`, and a comment's
54//! id is GitHub's own node id for the `IssueComment`. Two things GitHub decides are refused
55//! rather than papered over: a board **draft** is not an issue and has no comments at all, so
56//! a comment call on one is refused rather than answered with an empty page; and GitHub signs
57//! every comment as the account the token belongs to, so a comment handed an author of its
58//! own is refused rather than posted under another name. GitHub's comment mutations take the
59//! comment's id and nothing else, so an edit or a delete first reads which issue that comment
60//! is on, and a comment on some other issue is one this task does not have.
61//!
62//! **Where an entity is, is a link.** Every project, task and document this source reports
63//! carries a [`Location::Url`] naming the issue's own web address — the same address the
64//! `url` field already reports, in the shape that says a reader can open it. That is the
65//! contrast the location contract exists for: a reader holding an entity from this source
66//! is handed something to link to and one holding an entity from a folder of Markdown is
67//! handed a path, and neither has to know which plugin answered. It does not replace or
68//! derive from `url`; that field goes on reporting what it always reported.
69//!
70//! **Where metadata lives.** Short typed things go to typed fields and native relations:
71//! status to the board's `Status` single-select and the issue's own state, the copy
72//! origin to a source-owned `onetaskgraph.origin` text field, and dependencies to
73//! `blockedBy` and to sub-issue links. Unbounded caller JSON goes in a trailing
74//! `<!-- onetaskgraph.metadata ... -->` comment at the end of the issue body — the same
75//! encoding `docs/metadata.md` settles for Linear, not a second one. A ProjectV2 text
76//! field is length-bounded and `shortDescription` is capped at 300 characters, which is
77//! why neither can hold a caller's own prose. Setting one caller key on its own — on a task,
78//! a project or a document alike — is one update of the issue body that changes that slot
79//! and not one byte outside it, and it is not sent at all when the key already holds the
80//! value.
81//!
82// llmlint: ignore-block[contracts_have_one_source_or_a_drift_gate] This public module documentation is a required user-facing description; the loopback plugin tests and shared live journey drive StatusMapping resolution, both mutations, and observed read-back together.
83//! **Status.** `status_mapping` is per-instance configuration from a status category to
84//! `null` or a board `Status` option name. `done` selects its mapped option and closes the
85//! issue as `COMPLETED`; `cancelled` selects its mapped option and closes it as
86//! `NOT_PLANNED`. Every open category reopens a closed issue before selecting its option.
87//! A missing mapped option refuses the write before either representation changes. Reads
88//! give a closed issue's reason precedence over its option, while an open issue's option
89//! decides its category. The guarded [`GitHubProjectsSource::status_options`] operation is
90//! the one path here that calls `updateProjectV2Field`: GitHub replaces the whole option
91//! list, so it preserves every existing option id and verifies the field and item
92//! assignments immediately afterwards. It counts a terminal category's mapped option as
93//! configured, because a terminal write refuses without it. No ordinary source read or
94//! write calls that mutation, whose
95//! `singleSelectOptions` *overwrites* a field's option set, so no addition is additive
96//! and a mistake destroys every item's status. A status this board cannot represent is a
97//! refusal naming the status and the instance instead.
98//!
99//! `unknown` is disabled by default because this source cannot preserve an open-ended
100//! status word: it writes an existing board option and never
101//! creates an option. An operator may map `unknown` to one existing option, in which case
102//! every unknown word lands on that option and reads back as `unknown` under the option's
103//! name. This differs from `local-md`, which writes and reads the original word itself.
104//!
105//! The shipped terminal options are exactly `done: Done` and `cancelled: Cancelled`.
106//! `done` also closes the issue because GitHub derives `subIssuesSummary.completed`
107//! and the board's own `Sub-issues progress` field from closed sub-issues: a plan whose
108//! finished tasks were only moved to a "Done" column would read 0% complete forever.
109// llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
110//!
111//! # What this source declares, field by field
112//!
113//! One verdict per field of [`Capabilities`], and what `Native` means when this source
114//! says it. *Proven* means a shared journey drives it against the real
115//! binary over this source's own row in `crates/onetaskgraph/tests/e2e/fixtures.rs`, and
116//! `every_row_declares_exactly_what_its_plugin_reports` is what keeps this list and
117//! [`capabilities`](TaskSource::capabilities) from parting.
118//!
119//! | Field | Verdict |
120//! | --- | --- |
121//! | `projects` | **Supported and proven,** and the one predicate here that is pushed down rather than applied in process: a task's project is the issue it is a sub-issue of, so a listing scoped to one *asks that issue* for its own sub-issues. This is the field that was declared and then not applied, which silently returned another project's tasks. |
122//! | `documents` | **Supported and proven.** A board holds issues, so a document is one: the issue whose title begins [`DESIGN_TITLE_PREFIX`]. Reads, filters and paging answer on exactly the terms a task read does, and a write puts the prefix back. |
123//! | `comments` | **Supported and proven,** over the task issue's own comment connection, oldest first and paged by GitHub's own cursor; added, edited and removed through GitHub's comment mutations, paced as every other mutation is. A draft item has no comments on GitHub and is refused, and so is an author, because GitHub records the signed-in account as every comment's author. |
124//! | `orphan_tasks` | **Supported and proven.** A task issue with no `parent` is in no project. |
125//! | `filter_by_label` | **Supported and proven,** over the issue's own labels. |
126//! | `filter_by_status` | **Supported and proven,** over the board's `Status` option and the issue's open or closed state, through this instance's own `status_mapping`. |
127//! | `search_title` | **Supported and proven,** over `Issue.title`. |
128//! | `search_content` | **Supported and proven,** over the visible body — the trailing metadata comment is not part of it. |
129//! | `task_dependencies` | **Supported and proven,** in both directions: `blockedBy` and `blocking`. |
130//! | `project_dependencies` | **Supported and proven,** in both directions, over the same two connections, because a project here is an issue. |
131//! | `max_page_size` | **Supported and proven.** [`MAX_PAGE_SIZE`], GitHub's own connection maximum. |
132//!
133//! Nothing here is unsupported. `documents` and `comments` are not predicates — they say this
134//! source has documents and that its tasks have comments, both of which hold — and the three
135//! facts behind the uniform `Native` on the
136//! predicates beside it are recorded below rather than re-derived, because a reader who
137//! takes `Native` to mean *the remote service filters* will read that uniformity as a
138//! lie.
139//!
140//! First, the plugin contract defines `Support::Native` as *the source applies this
141//! predicate itself*, and says nothing about where it applies it. What the declaration
142//! promises the engine is capability rule 1 — a predicate declared `Native` **is** applied
143//! — so that the engine may push it down and apply nothing of its own.
144//!
145//! Second, this source can keep that promise for every predicate at no additional API
146//! cost, because whichever of the three reads below answers a query has already read every
147//! item that query could keep before it filters anything. Filtering those items is
148//! in-process work over data already in hand.
149//!
150//! Third, no predicate but `projects` could be pushed into the API even if that were
151//! wanted, and `projects` is pushed down: `ProjectV2.items` takes `first` and `after` and
152//! offers no filter argument of any kind, GitHub's issue search offers no qualifier for a
153//! label set, a status column or a substring of a body, and its title qualifier matches
154//! tokens where this source — and the local Markdown source beside it — match substrings,
155//! so pushing a search down would silently *narrow* the answer. What a project filter has
156//! instead is a relationship: a project's tasks are that issue's sub-issues, and asking
157//! the issue for them is both cheaper and exact. So there is one predicate this source
158//! applies by asking a narrower question, six it applies in process, and none it is unable
159//! to apply. Declaring one `Unsupported` would make the engine compensate for work this
160//! source has already done, and declaring `projects` native while ignoring the filter
161//! (which this source once did) silently returns another project's tasks, because the
162//! engine trusts the declaration and applies nothing locally.
163//!
164//! # The three ways this source reaches an item, and what each costs
165//!
166//! A board read is charged for what its *nested* connections could return rather than for
167//! what was asked, so one whole-board read costs the same whether the question was about
168//! one project or about all of them. That is why a question about one project is never
169//! answered by reading the board:
170//!
171//! | The question | What is sent | What it costs |
172//! | --- | --- | --- |
173//! | one item, by its own id | [`graphql::ISSUE`] — `node(id:)` | the item |
174//! | one project's tasks or documents | [`graphql::SUB_ISSUES`] — that issue's own `subIssues` | that project |
175//! | which projects this board holds | [`graphql::SEARCH_ISSUES`] — an issue search scoped to the board | the board's issues, without their board items |
176//! | every task, every document, every label | [`graphql::BOARD`] — the board's own `items` — **and** [`graphql::SEARCH_ISSUES`], because neither enumeration of a board is complete alone; see [`GitHubProjectsSource::board`] | the board, twice over |
177//! | which board item one issue is, past the page that came with it | [`graphql::ISSUE_BOARD_ITEMS`] — that issue's own `projectItems` | one issue's memberships |
178//!
179//! The board half of an issue — its board item's id, its `Status` option and this
180//! source's origin text field — rides along on `Issue.projectItems` in the first three, so
181//! an item reached any of those ways resolves through the same
182//! [`GitHubProjectsSource::resolve`] the board walk uses and reports the same title, the
183//! same status, the same labels and the same qualified id. That connection comes back a
184//! *page* at a time, at `BOARD_ITEMS_PAGE_SIZE`, so the entry for this board is looked for
185//! on the page in hand and — only if that page reports more of the connection — in the
186//! last row's read of that one issue's memberships, resumed from the page's own cursor and
187//! walked to exhaustion. An issue with no entry for *this* board is not this source's to
188//! report, which is what keeps an id naming another repository's issue from being answered
189//! as an item of this board; and because the page is where the search starts rather than
190//! where it ends, that answer is one about a connection read to exhaustion and never about
191//! an unread page. Nothing costs the extra read but an issue on more boards than a page
192//! holds: an issue this board really does not hold reports no next page, so its
193//! memberships are already exhausted where they arrived.
194//!
195//! **No document here selects the board's own `Labels` field, and nothing is lost by
196//! that.** An item's labels are read from its content alone, wherever that content is
197//! reached: the three documents above select `Issue.labels` on the fragment, and
198//! [`graphql::BOARD`] selects the same connection on the `... on Issue` arm of its
199//! `content`. A board's `Labels` field is not one anybody fills in: it is a built-in
200//! `ProjectV2FieldType`, it is absent from `ProjectV2CustomFieldType` so no project can
201//! create one, and `ProjectV2FieldValue` — the whole of what
202//! `updateProjectV2ItemFieldValue` accepts — offers no way to write one. So GitHub derives
203//! it from the content, for every content type it exists on, and there is nothing it can
204//! hold that the content does not already say: for an `Issue` it *is* that issue's own
205//! labels, so selecting it beside them unions a set with itself.
206//!
207//! **A draft loses nothing by that either**, which is the reasoning this paragraph once had
208//! backwards. `DraftIssue` exposes no `labels` field, and by the three schema facts above
209//! it cannot carry a board `Labels` value to be derived from one — so a draft has nothing
210//! to select *and nothing to lose*, and reports no labels at all. A `PullRequest` item is
211//! discarded by [`GitHubProjectsSource::resolve`] before labels are read. Both halves are
212//! held to that by tests in `tests/plugin.rs`: the four ways an item is reached report one
213//! label set, and that set is the fixture issue's own, by
214//! `an_item_reports_the_same_labels_title_status_and_id_however_it_is_reached`; and a board
215//! item whose content is a draft reports an empty set, by
216//! `a_board_item_whose_content_is_a_draft_reports_no_labels_at_all`. The absence of the
217//! selection is held over [`graphql::DOCUMENTS`] by
218//! `no_document_selects_the_boards_own_labels_field`.
219//!
220//! The last row is still the board's own item connection, and deliberately: a **draft**
221//! board item is not an issue, so no search and no node read can reach one, and the reads
222//! that have to answer for the whole board are the ones whose cost is the board's size
223//! anyway.
224//!
225//! **What a read may return is capped too, and that cap is on the document rather than on
226//! the board.** GitHub limits the number of nodes **one query may return** to
227//! [`NODE_COUNT_LIMIT`] and refuses a query above that before executing it: the answer is
228//! an error naming the connection the count crossed at, not a slow or a partial result.
229//! Every board this source reads is refused the same way, so no board is too big for these
230//! documents and none is small enough to save one that is over.
231//!
232//! The count is arithmetic over the document's own text: each connection contributes the
233//! `first:` it asks for, counts **multiply** down a nested path and **sum** across sibling
234//! paths. Those are [GitHub's published rules][node-limits] and this workspace does not
235//! restate them — `github-graphql-node-count` implements them, and
236//! [`worst_case_node_count`] under [`largest_page_sizes`] is where every node count here
237//! comes from. `every_document_this_source_sends_stays_under_githubs_node_limit`, in
238//! `tests/node_count.rs`, recomputes every document in [`graphql::DOCUMENTS`] from that
239//! same text on every run and fails naming any that reaches the limit — so a connection
240//! added to a shared fragment is caught there rather than by GitHub.
241//!
242//! What decides those counts is the page sizes: [`MAX_PAGE_SIZE`] on the outer page,
243//! `NESTED_PAGE_SIZE` on the connections hanging off one item, and
244//! `BOARD_ITEMS_PAGE_SIZE` on the page of an issue's board memberships a read carries.
245//! `$nestedFirst` is spent twice down one path of a board read, so that constant is
246//! effectively squared there, which is why it is the one the limit is most sensitive to.
247//! `BOARD_ITEMS_PAGE_SIZE` is small for a reason of its own, recorded beside it: what a
248//! page of memberships misses is recovered by one further read rather than refused, so it
249//! buys a bound every read pays for at the price of a request only a multi-board issue
250//! pays.
251//!
252//! **`nodeCount` and `cost` are two numbers against two limits, and both are computed
253//! offline here — per document, one document at a time.** `nodeCount` is the one above: the
254//! most nodes one query may return, checked per query and bounded by [`NODE_COUNT_LIMIT`].
255//! `cost` is rate-limit points, metered per hour across everything one credential does; it
256//! is what the two limiters [`Limiter`] tells apart meter, and a document under
257//! [`NODE_COUNT_LIMIT`] still says nothing about its price. [`worst_case_point_cost`] is
258//! that second number, and `tests/point_cost.rs` pins every document in
259//! [`graphql::DOCUMENTS`] at what it costs — there being no per-call point ceiling to hold
260//! one under, the pin itself is the check. The credentialed lane reconciles both figures
261//! against GitHub's own, off a probe it already sends.
262//!
263//! **What is pinned that way is a per-document price and never a session's.** The record in
264//! `session-cost.md` measures the two quantities a whole session can be counted in offline —
265//! **requests** and **worst-case nodes** — and neither is points. What one whole session
266//! consumes of the hourly point allowance is observable only from a credentialed run's own
267//! `x-ratelimit-*` headers, which is what [`accounting`] fills its per-budget figures from
268//! and what `tests/live.rs` prints at the end of every run.
269//!
270//! [node-limits]: https://docs.github.com/en/graphql/overview/rate-limits-and-node-limits-for-the-graphql-api
271//!
272//! **Where a read-after-write guarantee comes from, since neither of GitHub's two
273//! enumerations of a board can supply one alone.** Resolving a node id is strongly
274//! consistent, so a read by id and a project's own sub-issues are already current. The
275//! other two are not, and they are behind by different amounts and in different directions:
276//!
277//! - GitHub's **issue search** is an index and answers a write made moments ago with the
278//! value from before it — usually for a second or two.
279//! - **`ProjectV2.items`** is a projection GitHub rebuilds behind the write, and an item put
280//! on a board with `addProjectV2ItemById` can be **absent** from it — not present with its
281//! content withheld, absent, with the connection walked to its own `hasNextPage: false` —
282//! for *minutes*, while `Issue.projectItems` names the same membership at once.
283//!
284//! That second one is a measurement rather than a caution. This repository's own
285//! credentialed journey writes a project and waits for the board to report it, then writes a
286//! task and waits for the same thing seconds later on the same board: the project wait is
287//! answered through the search and converged in two or three attempts in each of three runs,
288//! and the task wait is answered through `ProjectV2.items` and converged in none of them
289//! inside thirty. Separately, an item added to a second and larger board was read back by
290//! `Issue.projectItems` on that board's own id while every one of that connection's nine
291//! pages, walked to exhaustion nine minutes after the add, did not name it. Reading a board
292//! through the lagging one alone is what had a board read deny an issue that had certainly
293//! landed on it.
294//!
295//! So [`GitHubProjectsSource::board`] is the **union** of both — each search result still
296//! admitted only on this board's own strongly-consistent `Issue.projectItems`, and neither
297//! enumeration dropped, because only `ProjectV2.items` reaches a board draft and the board's
298//! fields and only the search reports what the projection is behind on. What closes the last
299//! gap, the one where both are behind, is [`GitHubProjectsSource::created`]: every read this
300//! source answers is completed with what this process itself wrote, so an item created
301//! seconds ago is reported whether or not GitHub has caught up. Nothing else is remembered,
302//! nothing is written down, and the record dies with the process. **A wait that has to
303//! observe GitHub's own data cannot be answered from that record** — which is why the
304//! credentialed journey asks through a source built afresh, and why the union above rather
305//! than a longer wait is what makes such a wait converge.
306//!
307//! Filtering happens before paging, so a page of a filtered result is a page of the
308//! survivors rather than the survivors of a page. Label and text matching answer the same
309//! question the same way the local Markdown source's do, so one cross-source expectation
310//! holds for both.
311//!
312//! <!-- llmlint: ignore[contracts_have_one_source_or_a_drift_gate] The declaration itself
313//! has one source, `capabilities`, and the note above is the reasoning behind it rather
314//! than a second copy of it: without the three facts recorded here a reader takes the
315//! uniform `Native` for a lie and reverts it. The drift gate on the declaration is this
316//! crate's own capabilities test, which pins every field of it against a fully spelled-out
317//! `Capabilities` literal — a struct with no `Default`, so a field added to the contract
318//! fails to compile there rather than going unasserted. -->
319//! The fixture-server tests above run wherever this crate is selected; the credentialed
320//! lane runs in the same required check, beside them, and can fail it — it verifies the
321//! current schema, then drives every field of the table above against the real board. It builds its own fixture there — two projects, one task filed under each,
322//! one filed under neither, a label on one of the three and a closed status on another —
323//! because that shape is what tells an honoured predicate from an ignored one: a board
324//! holding a single project answers a project filter the same way whether or not this
325//! source applies it, which is exactly how the defect above went unseen.
326//!
327//! That lane writes only to the board `GH_PROJECTS_OWNER` and `GH_PROJECTS_NUMBER` name,
328//! and only into the repository `GH_PROJECTS_REPOSITORY` names, and skips — as it does
329//! without `GH_PROJECTS_TOKEN` — when any of them is absent. Requiring both to be
330//! nominated is what keeps a credentialed write lane off a board and a repository nobody
331//! nominated; it never asks GitHub which project was updated most recently. Before it
332//! starts, the lane also clears any item titled — and any repository label named — the way
333//! it titles and names its own artifacts, which is self-healing after an interrupted run:
334//! a process killed between its writes and its cleanup leaves artifacts the next run
335//! removes.
336//!
337//! # What a session of requests costs, and where the report is
338//!
339//! This source records **every** request it sends into [`accounting::Accounting`], at
340//! `send_once` — the one place a request leaves this crate, which is why a read path added
341//! later is counted without anybody remembering to count it. That is the whole of what this
342//! crate adds to the arrangement; [`accounting`] is where what a record carries, how a
343//! session's spend is arrived at, and what it deliberately does not know are set out.
344//!
345//! What one whole session of the live journey costs, counted that way against this crate's
346//! loopback fixture board, is written down in `session-cost.md` beside this crate — with the
347//! reduction it came out of, and with what it does and does not say about rate-limit points.
348//!
349//! [`GitHubProjectsSource::accounting`] is the read: a snapshot to hold and compare, which
350//! [`accounting::Session::report`] renders the session report from. It is on the ordinary
351//! code path — no environment variable, no feature, no build configuration — because an
352//! instrument nobody switches on measures nothing, and
353//! [`Plugin::build_recording_into`] is how a caller making its own calls beside this
354//! source's counts the whole session rather than this source's share. The credentialed lane
355//! in `tests/live.rs` does exactly that, and prints the report at the end of every run,
356//! passed or failed.
357//!
358//! **A live session refuses to start unless the account can afford it.** Before it does any
359//! of the work it exists to do, the journey makes one request — `GET /rate_limit`, which
360//! GitHub documents as not counting against the REST rate limit and which answers both of
361//! its budgets at once — and starts only if, for each of them, what remains minus this
362//! session's estimated cost is still at least
363//! `onetaskgraph_live::RETAINED_BUFFER` — twenty per cent — of that budget's whole
364//! allowance. A session that cannot **declines**: it did not run, so it is
365//! neither a pass nor a failing assertion, and it says which budget was short, that budget's
366//! limit, what remained, the estimate, the buffer and when it resets — then stops, without
367//! waiting for the budget to come back. The estimate is derived offline from
368//! `tests/fixtures/session-cost.txt` and a cost model stated in `tests/journey/budget.rs`,
369//! which is also where the published rule that model rests on is cited; the accounting
370//! above records the gate's own read like any other request, and
371//! [`accounting::Session::report`] prints the estimate beside what the session really spent.
372//!
373//! **GitHub is the authority on both of its own numbers, and the credentialed lane goes and
374//! asks it.** Everything above computes `nodeCount` and `cost` offline from a document's own
375//! text, which is what lets it run on every platform and on a pull request from a fork with
376//! no credential — and that is what actually stops a regression merging. But an offline
377//! arithmetic can only ever agree with itself: if GitHub changes its rules, this workspace
378//! goes on computing the old answer and nothing notices. So `tests/live.rs` reconciles them.
379//! GitHub's schema exposes `rateLimit(dryRun: true)`, whose `nodeCount` is *"the maximum
380//! number of nodes this query may return"* and whose `cost` is what that document would
381//! spend, both for a document **without executing it**, and the lane asks it for every query
382//! document this source sends, under the largest bindings this source sends, and fails when
383//! GitHub's figure and [`worst_case_node_count`] or [`worst_case_point_cost`] disagree. A
384//! mutation is skipped, because `rateLimit` is a field of `Query` and cannot be asked about
385//! one; the offline pins still cover it. It records what those calls reported about the
386//! account's own allowance, because whether asking is free is a thing to observe rather than
387//! to assume. Two quantities, not one: [`NODE_COUNT_LIMIT`] bounds `nodeCount` per query,
388//! and `cost` is metered against an hourly allowance the accounting above reads off a
389//! credentialed run's own response headers.
390//!
391//! **GitHub has two rate limiters and this source is refused by both, so nothing here
392//! treats them as one thing.** The primary budget is the hourly allowance `gh api
393//! rate_limit` reports; the secondary limiter is a burst limiter over content-generating
394//! requests, and *nothing* reports it. Which one refused decides the operator's next step,
395//! so [`Limiter`] is a type rather than a detail, and it is what [`MIN_MUTATION_INTERVAL_MS`],
396//! [`GitHubProjectsSource::board_cache`] and [`GitHubProjectsSource::graphql`] each answer
397//! one part of.
398#![deny(missing_docs)]
399
400use std::collections::BTreeMap;
401use std::sync::{Arc, Mutex};
402use std::time::{Duration, Instant};
403
404use chrono::{DateTime, Utc};
405use onetaskgraph_plugin_api::{
406 Capabilities, Comment, CommentBody, Cursor, DependencyEdge, DependencyEndpoint, DependencyKind,
407 DependencySupport, Direction, Document, DocumentQuery, Health, ItemKind, ItemWrite, Label,
408 LabelFilter, Location, MetadataKey, Metering, NativeId, NewComment, Page, PageRequest, Project,
409 ProjectFilter, ProjectQuery, Repository, SecretResolver, SourceError, SourceName, SourcePlugin,
410 Status, StatusCategory, Support, Task, TaskQuery, TaskRef, TaskSource, TextFields, TextQuery,
411 WriteSupport,
412};
413use reqwest::{Client, StatusCode, Url};
414use schemars::{Schema, schema_for};
415use secrecy::{ExposeSecret, SecretString};
416use serde::{Deserialize, Serialize};
417use serde_json::{Value, json};
418
419pub mod accounting;
420
421use accounting::Accounting;
422
423/// The registry name for this plugin.
424pub const KIND: &str = "github-projects";
425/// GitHub's maximum connection page size.
426pub const MAX_PAGE_SIZE: u32 = 100;
427
428/// The most nodes any one document this source sends may be asked to return.
429///
430/// GitHub's own published per-query ceiling, taken from
431/// [`github_graphql_node_count::NODE_LIMIT`] rather than written out again here, so this
432/// workspace cannot hold a stale copy of somebody else's number. A query above it is
433/// **refused before it is executed**, whoever is asking and whatever board they are
434/// asking about — so this is a bound on the documents rather than a budget that runs out.
435///
436/// This is `nodeCount`, the maximum number of nodes *one query may return*. It is not
437/// `cost`, the rate-limit points a call spends against an hourly allowance shared by
438/// everything the credential does — two numbers against two limits, and this constant
439/// bounds only the first. The second is computed offline too, per document:
440/// [`worst_case_point_cost`], pinned for every document in [`graphql::DOCUMENTS`] by
441/// `tests/point_cost.rs`, and reconciled against GitHub's own `cost` by the credentialed
442/// lane. There is no constant like this one to hold a price under, because points are an
443/// hourly allowance rather than a per-call bound.
444///
445/// Neither is a session's price. What `session-cost.md` records of a whole session is its
446/// **requests** and its **worst-case nodes**; what a whole session spends in points is
447/// reported only by a credentialed run's own `x-ratelimit-*` headers, through
448/// [`accounting`]. The module section on the three ways this source reaches an item says how
449/// the count is arrived at, and which of the page sizes below decide it.
450pub const NODE_COUNT_LIMIT: u64 = github_graphql_node_count::NODE_LIMIT;
451
452/// Nested connection size for the connections that hang off one item.
453///
454/// It multiplies through every document that reaches an item under a page — the count
455/// rules multiply down a nested path — so it is the constant [`NODE_COUNT_LIMIT`] is most
456/// sensitive to. `tests/node_count.rs` is what holds the pair together: it recomputes
457/// every document under these constants and fails naming any that reaches the limit, so
458/// raising this is caught there rather than by GitHub.
459const NESTED_PAGE_SIZE: u32 = 50;
460/// How many of one issue's board memberships are read when an issue is reached directly.
461///
462/// An issue reached through a search or through its own node id carries its board half in
463/// `Issue.projectItems`, and only the entry for *this* board is read. This connection sits
464/// under a page of issues, so every point of it multiplies through the whole document and
465/// is paid for whether or not any issue is on a second board — which is why it is
466/// deliberately far smaller than [`NESTED_PAGE_SIZE`].
467///
468/// **Three, because what a page misses is now recovered rather than refused**, and the
469/// recovery is what the value is chosen against. An issue whose entry for this board sits
470/// past this page costs one further request — [`graphql::ISSUE_BOARD_ITEMS`], resumed from
471/// that page's own cursor — so the value trades a bound every read pays for a request only
472/// a multi-board issue pays. At one, a deployment whose issues commonly sit on two or more
473/// boards would pay that request *per issue*, which is order N against the one page per
474/// hundred issues a read costs today. At three it is only reached by an issue on four or
475/// more boards at once, which keeps the recovery path exceptional rather than routine for
476/// a plausible deployment.
477const BOARD_ITEMS_PAGE_SIZE: u32 = 3;
478
479pub use github_graphql_node_count::{NodeCountError, Variables};
480
481/// The largest value this source can bind to each page-size variable its documents name.
482///
483/// Every `first:` in [`graphql`] reads one of these three, and each is capped at the
484/// constant above it wherever a caller's own limit could reach it — `$first` at
485/// [`MAX_PAGE_SIZE`], `$nestedFirst` at `NESTED_PAGE_SIZE`, `$boardItems` at
486/// `BOARD_ITEMS_PAGE_SIZE`. So this is the worst case a caller can drive this source to,
487/// not one configuration of it, which is what makes a bound computed under it a bound on
488/// every read.
489pub fn largest_page_sizes() -> Variables {
490 Variables::from([
491 ("first".to_owned(), MAX_PAGE_SIZE),
492 ("nestedFirst".to_owned(), NESTED_PAGE_SIZE),
493 ("boardItems".to_owned(), BOARD_ITEMS_PAGE_SIZE),
494 ])
495}
496
497/// The most nodes `document` could be asked to return, by GitHub's published rules.
498///
499/// Computed offline from the document's own text under [`largest_page_sizes`] — no
500/// network, no credential and no schema — by
501/// [`github_graphql_node_count::node_count`], which is where the rules themselves live.
502/// A document at or above [`NODE_COUNT_LIMIT`] is one GitHub refuses before executing, so
503/// this is what a check holds every document in [`graphql::DOCUMENTS`] below.
504///
505/// # Errors
506///
507/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
508/// no single operation, or binds a page size this source does not name — each of which is
509/// a defect in the document rather than a number.
510pub fn worst_case_node_count(document: &str) -> Result<u64, NodeCountError> {
511 node_count(document, &largest_page_sizes())
512}
513
514/// The most rate-limit points one call of `document` could spend, by GitHub's published
515/// rules.
516///
517/// Computed offline from the document's own text under [`largest_page_sizes`] — no
518/// network, no credential and no schema — by
519/// [`github_graphql_node_count::point_cost`], which is where the rules themselves live.
520/// This is `cost`, metered **per hour** against the allowance one credential shares across
521/// everything it does; it is not `nodeCount`, which is [`worst_case_node_count`] and is
522/// bounded per query by [`NODE_COUNT_LIMIT`]. There is no per-call ceiling to hold this
523/// under, so what `tests/point_cost.rs` does with it is pin every document in
524/// [`graphql::DOCUMENTS`] at what it costs, and the credentialed lane reconciles those
525/// figures against GitHub's own reported `cost`.
526///
527/// # Errors
528///
529/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
530/// no single operation, or binds a page size this source does not name — each of which is
531/// a defect in the document rather than a number.
532pub fn worst_case_point_cost(document: &str) -> Result<u64, NodeCountError> {
533 github_graphql_node_count::point_cost(document, &largest_page_sizes())
534}
535
536/// The most nodes `document` could be asked to return under `variables`.
537///
538/// [`worst_case_node_count`] is this under [`largest_page_sizes`], and the accounting in
539/// [`accounting`] is this under the bindings one request really sent — one spelling of the
540/// calculation, so a bound checked offline and a cost recorded at run time cannot come to
541/// disagree. The rules themselves live in [`github_graphql_node_count::node_count`].
542///
543/// # Errors
544///
545/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
546/// no single operation, or binds a page size `variables` does not name.
547pub fn node_count(document: &str, variables: &Variables) -> Result<u64, NodeCountError> {
548 github_graphql_node_count::node_count(document, variables)
549}
550
551/// The issue-title prefix that makes a board issue a document.
552///
553/// A GitHub Projects board has no document type — it holds issues — so the discriminator
554/// is the title, and this is the whole of it: an issue whose title begins with these bytes
555/// is a document and every other issue is the task or project the sub-issue rule makes it.
556///
557/// It is spelled **once**, here, and read rather than restated everywhere else — including
558/// by the shared journeys, which take it from this constant so a board fixture cannot
559/// drift from what this source reads. `docs/metadata.md` records the two consequences that
560/// are not obvious from the bytes: the reported title has this prefix taken off, exactly
561/// as the body's metadata slot is taken off `content`, and this prefix is read *before*
562/// the sub-issue rule, so a design issue with no sub-issues is never an empty project.
563pub const DESIGN_TITLE_PREFIX: &str = "DESIGN: ";
564
565/// Exact GraphQL query documents issued by this plugin.
566///
567/// Keeping the production documents here lets the pinned-schema test validate the same
568/// bytes that are sent to GitHub, rather than a test-only copy which could drift
569/// independently. [`STATUS_OPTIONS_UPDATE`] is the sole document that may rewrite a board
570/// field, and its guarded caller always supplies the complete existing option set with ids.
571pub mod graphql {
572 /// The board half of one item: the field values every document here reads it from.
573 ///
574 /// A macro for the same reason [`board_issue!`] below is one, a level further in. This
575 /// selection is needed by that fragment, by [`BOARD`] under the board's own `items`,
576 /// and by [`ISSUE_BOARD_ITEMS`] under a membership walk — and all three have to produce
577 /// *the same value*, because
578 /// [`GitHubProjectsSource::resolve`](super::GitHubProjectsSource) reads them through
579 /// one path. Three spellings of it is what would drift, so there is one.
580 ///
581 /// The `Status` option and this source's own origin text field are the whole of it. It
582 /// selects no `ProjectV2ItemFieldLabelValue`: GitHub derives that field from the item's
583 /// content, so it holds nothing the content's own `labels` do not already say, and it
584 /// would sit a label connection two page sizes deep.
585 macro_rules! board_item_values {
586 () => {
587 r#"fieldValues(first:$nestedFirst){nodes{
588 ... on ProjectV2ItemFieldSingleSelectValue{name field{
589 ... on ProjectV2SingleSelectField{id name options{id name}}
590 }}
591 ... on ProjectV2ItemFieldTextValue{text field{... on ProjectV2Field{id name}}}
592 }pageInfo{hasNextPage}}"#
593 };
594 }
595
596 /// Everything this source reads about one issue, wherever it reaches that issue.
597 ///
598 /// A macro rather than a constant so the three documents below can `concat!` it: one
599 /// spelling of these fields is what makes an issue read through the board-scoped
600 /// search, through its own node id, and through its project's sub-issue relationship
601 /// resolve to *the same* item, which is the whole of what
602 /// [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource) relies on.
603 ///
604 /// `projectItems` is what carries the board half of an issue: the board item's own id
605 /// and the [`board_item_values!`] above — the `Status` option and this source's origin
606 /// text field — that a `ProjectV2.items` read used to carry. It is asked for on the
607 /// issue rather than on the board, which is what makes the cost of a read proportional
608 /// to what was asked for instead of to the board's size.
609 ///
610 /// It carries a *page* of that connection, at `BOARD_ITEMS_PAGE_SIZE`, and its
611 /// `endCursor` is what [`ISSUE_BOARD_ITEMS`] resumes from when this board's entry is
612 /// not on that page: a page here is where the search for the entry starts rather than
613 /// where it ends.
614 ///
615 /// It does **not** select the board's `Labels` field value, and that is the whole of
616 /// what keeps the three documents below under [`NODE_COUNT_LIMIT`](super::NODE_COUNT_LIMIT):
617 /// a label connection there sits under `fieldValues` under `projectItems` under a page
618 /// of issues, spending `$nestedFirst` twice down one path, and took
619 /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] to 2,556,100 nodes against a limit of 500,000.
620 /// No label is lost — this is a fragment `on Issue`, whose own `labels` are selected
621 /// above, and that connection is where every label this source reports comes from. No
622 /// document in this module selects the board field any longer, [`BOARD`] included; the
623 /// module documentation records why nothing it could have held is lost.
624 macro_rules! board_issue {
625 () => {
626 concat!(
627 r#" fragment BoardIssue on Issue{__typename id title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total}
628 labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}
629 projectItems(first:$boardItems){nodes{id project{id number}
630 "#,
631 board_item_values!(),
632 r#"}pageInfo{hasNextPage endCursor}}}"#
633 )
634 };
635 }
636
637 /// Every issue of one board, found by a search scoped to that board.
638 ///
639 /// This is how the projects a board holds are listed, and it selects no `items`
640 /// connection on `ProjectV2`: the board is a *qualifier of the search* rather than a
641 /// container walked page by page, so nothing nested inside a board item is paid for.
642 /// Which of the issues it returns is a project is then read off `parent` — GitHub
643 /// accepts `-has:parent` as a search qualifier and silently ignores it, so the
644 /// discriminator has to be applied to the field, which is a scalar on the issue and
645 /// costs nothing.
646 pub const SEARCH_ISSUES: &str = concat!(
647 r#"query($search:String!,$type:SearchType!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!){
648 search(query:$search,type:$type,first:$first,after:$after){
649 pageInfo{hasNextPage endCursor}
650 nodes{__typename ...BoardIssue}
651 }
652 }"#,
653 board_issue!()
654 );
655
656 /// One issue by its own node id, which is what a qualified id names here.
657 ///
658 /// Strongly consistent, unlike the search above: GitHub's issue search is an index and
659 /// answers a write made moments ago with the value from before it, and resolving a node
660 /// id does not.
661 pub const ISSUE: &str = concat!(
662 r#"query($id:ID!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!){
663 node(id:$id){__typename ...BoardIssue}
664 }"#,
665 board_issue!()
666 );
667
668 /// One project's tasks: the sub-issues of the issue that project is.
669 ///
670 /// The work this costs is the project's own size. Nothing about it grows as the board
671 /// gains projects, or as those projects gain tasks.
672 pub const SUB_ISSUES: &str = concat!(
673 r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!){
674 node(id:$id){__typename
675 ... on Issue{subIssues(first:$first,after:$after){
676 pageInfo{hasNextPage endCursor}
677 nodes{__typename ...BoardIssue}
678 }}}
679 }"#,
680 board_issue!()
681 );
682
683 /// Reads the board's fields and one page of its items.
684 pub const BOARD: &str = concat!(
685 r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$nestedFirst:Int!,$duplicates:Boolean!){
686 owner:repositoryOwner(login:$owner){
687 ... on ProjectV2Owner{projectV2(number:$number){...Board}}
688 }
689 } fragment Board on ProjectV2 { id title
690 fields(first:$nestedFirst){nodes{
691 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
692 ... on ProjectV2Field{__typename id name}
693 }pageInfo{hasNextPage}}
694 items(first:$first,after:$after){nodes{id "#,
695 board_item_values!(),
696 r#" content{
697 ... on Issue{__typename id title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total} labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}}
698 ... on PullRequest{__typename id}
699 ... on DraftIssue{__typename id title body createdAt updatedAt}
700 }} pageInfo{hasNextPage endCursor}}
701 }"#
702 );
703
704 /// One issue's board memberships alone, walked past the page a read of it carried.
705 ///
706 /// The recovery read behind [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource):
707 /// every document above carries a *page* of `Issue.projectItems`, and an issue on more
708 /// boards than that page holds may have this board's entry past its end. This asks that
709 /// one issue for its memberships and nothing else — the caller already holds the issue —
710 /// so an answer of "this board does not hold it" is only ever given about a connection
711 /// read to exhaustion.
712 ///
713 /// It selects the board item's id, its project number and the same
714 /// [`board_item_values!`] the fragment does, because what it produces is handed to the
715 /// very same resolver: an issue recovered this way reports the same title, the same
716 /// status, the same labels and the same qualified id as one whose entry was on the
717 /// page.
718 ///
719 /// `$first` rather than `$boardItems`: this document reads one issue, so nothing
720 /// multiplies through it and the membership connection can be walked at
721 /// [`MAX_PAGE_SIZE`](super::MAX_PAGE_SIZE) — which is what keeps the recovery to one
722 /// further request for any issue a person really keeps.
723 pub const ISSUE_BOARD_ITEMS: &str = concat!(
724 r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!){
725 node(id:$id){
726 ... on Issue{projectItems(first:$first,after:$after){
727 nodes{id project{id number}
728 "#,
729 board_item_values!(),
730 r#"}
731 pageInfo{hasNextPage endCursor}}}
732 }
733 }"#
734 );
735 /// Resolves the configured repository's node id, which creating an issue requires.
736 pub const REPOSITORY: &str = r#"query($owner:String!,$name:String!){repository(owner:$owner,name:$name){id nameWithOwner}}"#;
737 /// Reads both dependency directions for one issue, with each far end's own kind — and
738 /// the issue's own body, which is where an edge to another source is recorded, so that
739 /// half of a dependency read needs no second read of the issue or of the board.
740 pub const ISSUE_DEPENDENCIES: &str = r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
741 ... on Issue{body
742 blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
743 blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
744 }}} fragment Related on Issue{id title body parent{id} subIssuesSummary{total}}"#;
745 /// Creates one issue in the configured repository.
746 pub const CREATE_ISSUE: &str =
747 r#"mutation($input:CreateIssueInput!){createIssue(input:$input){issue{id url}}}"#;
748 /// Puts an existing issue on the configured board.
749 pub const ADD_TO_BOARD: &str = r#"mutation($input:AddProjectV2ItemByIdInput!){addProjectV2ItemById(input:$input){item{id}}}"#;
750 /// Updates an issue's visible fields and its open or closed state in one call.
751 pub const UPDATE_ISSUE: &str =
752 r#"mutation($input:UpdateIssueInput!){updateIssue(input:$input){issue{id}}}"#;
753 /// Updates an existing draft's user-visible fields.
754 pub const UPDATE_DRAFT: &str = r#"mutation($input:UpdateProjectV2DraftIssueInput!){updateProjectV2DraftIssue(input:$input){draftIssue{id}}}"#;
755 /// Updates a text or single-select value on one project item.
756 pub const UPDATE_FIELD: &str = r#"mutation($input:UpdateProjectV2ItemFieldValueInput!){updateProjectV2ItemFieldValue(input:$input){projectV2Item{id}}}"#;
757 /// Replaces a single-select field's options. Only the guarded status-options operation
758 /// may use this document, because GitHub treats the input as the complete option list.
759 pub const STATUS_OPTIONS_UPDATE: &str = r#"mutation($input:UpdateProjectV2FieldInput!){updateProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id options{id name color description}}}}}"#;
760 /// A fresh snapshot of the Status field and every board item's assignment.
761 pub const STATUS_OPTIONS_SNAPSHOT: &str = r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$nestedFirst:Int!){owner:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){id fields(first:$nestedFirst){nodes{... on ProjectV2SingleSelectField{id name options{id name color description}}}pageInfo{hasNextPage}} items(first:$first,after:$after){nodes{id fieldValues(first:$nestedFirst){nodes{... on ProjectV2ItemFieldSingleSelectValue{name optionId field{... on ProjectV2SingleSelectField{id name}}}}pageInfo{hasNextPage}}}pageInfo{hasNextPage endCursor}}}}}}"#;
762 /// Files one issue under another as a sub-issue, which is what project membership is.
763 pub const ADD_SUB_ISSUE: &str =
764 r#"mutation($input:AddSubIssueInput!){addSubIssue(input:$input){issue{id} subIssue{id}}}"#;
765 /// Takes one issue back out of its parent.
766 pub const REMOVE_SUB_ISSUE: &str = r#"mutation($input:RemoveSubIssueInput!){removeSubIssue(input:$input){issue{id} subIssue{id}}}"#;
767 /// Adds GitHub's native issue blocked-by relationship.
768 pub const ADD_BLOCKED_BY: &str = r#"mutation($input:AddBlockedByInput!){addBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
769 /// Removes one native issue blocked-by relationship.
770 pub const REMOVE_BLOCKED_BY: &str = r#"mutation($input:RemoveBlockedByInput!){removeBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
771 /// Deletes one issue, which takes its board item with it.
772 ///
773 /// The engine sends this in one situation only: undoing a copy that could not finish,
774 /// over the items that same copy created. Deleting the issue removes the board item
775 /// too, so there is no second `deleteProjectV2Item` to keep in step with it.
776 pub const DELETE_ISSUE: &str =
777 r#"mutation($input:DeleteIssueInput!){deleteIssue(input:$input){repository{id}}}"#;
778
779 /// Everything this source reads about one issue comment, wherever it reaches one.
780 ///
781 /// A macro for the reason [`board_issue!`] is one: a comment listed, a comment just added
782 /// and a comment just edited are handed to one mapper, so they are selected by one
783 /// spelling. `author` is `Actor`, which GitHub answers `null` for an account that no
784 /// longer exists, and `login` is the one member every kind of actor carries.
785 macro_rules! issue_comment {
786 () => {
787 "id author{login} createdAt updatedAt body url"
788 };
789 }
790
791 /// One task's comments: a page of its issue's own `comments` connection.
792 ///
793 /// **No `orderBy`, and that is what makes the page oldest first.** GitHub's only
794 /// `IssueCommentOrder` field is `UPDATED_AT`, which would move a comment to the end of the
795 /// list every time somebody edited it; left unordered the connection answers in the order
796 /// the comments were written, which is the order GitHub documents for the same collection
797 /// over REST — ascending id. Nothing multiplies through it, so `$first` is the whole of its
798 /// node count and the caller's own page size is pushed straight down.
799 pub const ISSUE_COMMENTS: &str = concat!(
800 r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename ... on Issue{comments(first:$first,after:$after){nodes{"#,
801 issue_comment!(),
802 r#"}pageInfo{hasNextPage endCursor}}}}}"#
803 );
804 /// Which issue one comment is on, read before that comment is edited or removed.
805 ///
806 /// GitHub's comment mutations take the comment's id and nothing else, so without this a
807 /// comment id given against the wrong task would change a comment on another issue.
808 pub const COMMENT_ISSUE: &str =
809 r#"query($id:ID!){node(id:$id){__typename ... on IssueComment{id issue{id}}}}"#;
810 /// Adds one comment to an issue, signed as the account the token belongs to.
811 pub const ADD_COMMENT: &str = concat!(
812 r#"mutation($input:AddCommentInput!){addComment(input:$input){subject{id} commentEdge{node{"#,
813 issue_comment!(),
814 r#"}}}}"#
815 );
816 /// Replaces the body of one issue comment.
817 pub const UPDATE_COMMENT: &str = concat!(
818 r#"mutation($input:UpdateIssueCommentInput!){updateIssueComment(input:$input){issueComment{"#,
819 issue_comment!(),
820 r#"}}}"#
821 );
822 /// Removes one issue comment. Its payload carries nothing about the comment it removed.
823 pub const DELETE_COMMENT: &str = r#"mutation($input:DeleteIssueCommentInput!){deleteIssueComment(input:$input){clientMutationId}}"#;
824
825 /// Every document above, with what this source is doing when it sends one.
826 ///
827 /// One list rather than a `match` beside the constants: a rate-limit diagnostic has to
828 /// name the call that was refused, and a `match` with a catch-all arm would answer a
829 /// document added later with "talking to GitHub" and never say so.
830 ///
831 /// `documents_are_all_inventoried` reads this file back and fails naming any `pub
832 /// const` here that this list omits, so the two cannot part — which is the same guard
833 /// `CATEGORIES` carries, in the one shape available to a set of `&str` constants.
834 pub const DOCUMENTS: [(&str, &str); 24] = [
835 (SEARCH_ISSUES, "searching this board's issues"),
836 (ISSUE, "reading one issue"),
837 (
838 ISSUE_BOARD_ITEMS,
839 "reading one issue's board memberships past the page it came with",
840 ),
841 (SUB_ISSUES, "reading a project's tasks"),
842 (BOARD, "reading the board"),
843 (REPOSITORY, "reading the destination repository"),
844 (ISSUE_DEPENDENCIES, "reading an issue's dependencies"),
845 (CREATE_ISSUE, "creating an issue"),
846 (ADD_TO_BOARD, "adding an issue to the board"),
847 (UPDATE_ISSUE, "updating an issue"),
848 (UPDATE_DRAFT, "updating a draft item"),
849 (UPDATE_FIELD, "writing a board field"),
850 (
851 STATUS_OPTIONS_SNAPSHOT,
852 "snapshotting board Status options and assignments",
853 ),
854 (
855 STATUS_OPTIONS_UPDATE,
856 "safely replacing the board Status option list",
857 ),
858 (ADD_SUB_ISSUE, "filing an issue under its project"),
859 (REMOVE_SUB_ISSUE, "taking an issue out of its project"),
860 (ADD_BLOCKED_BY, "recording a dependency"),
861 (REMOVE_BLOCKED_BY, "removing a dependency"),
862 (DELETE_ISSUE, "deleting an issue"),
863 (ISSUE_COMMENTS, "reading a task's comments"),
864 (COMMENT_ISSUE, "reading which issue a comment is on"),
865 (ADD_COMMENT, "adding a comment"),
866 (UPDATE_COMMENT, "editing a comment"),
867 (DELETE_COMMENT, "deleting a comment"),
868 ];
869}
870
871/// Which of GitHub's two rate limiters refused a request.
872///
873/// Waiting is the whole answer to the primary budget, and polling is what *extends* the
874/// secondary one — so an operator told the wrong one takes the wrong next step, which is
875/// the whole reason this is carried rather than collapsed into "rate limited".
876#[derive(Debug, Clone, Copy, PartialEq, Eq)]
877enum Limiter {
878 /// The hourly API budget, which `gh api rate_limit` reports and a wait answers.
879 Primary,
880 /// The burst limiter over content-generating requests, which nothing reports.
881 Secondary,
882}
883
884/// The wordings GitHub answers a secondary rate limit with.
885///
886/// It sends them under a forbidden status, under a too-many-requests status, and inside
887/// the `errors` of a *successful* response, which is why the text is what this matches on
888/// rather than the status. `abuse detection` is the wording GitHub used before the
889/// limiter was renamed and still returns from some endpoints; `submitted too quickly` is
890/// what a burst of content creation is refused with.
891///
892/// This is GitHub's vocabulary rather than this source's, so it is pinned rather than
893/// remembered: `tests/fixtures/rate-limits.json` records where each wording was read and
894/// when, and the drift gate reconciles the two lists both ways. Public for that gate
895/// alone — a caller has no use for it, and matching on a refusal is this source's job.
896pub const SECONDARY_WORDINGS: [&str; 5] = [
897 "secondary rate limit",
898 "temporarily blocked from content creation",
899 "abuse detection",
900 "submitted too quickly",
901 "exceeded a secondary",
902];
903
904/// The wordings GitHub answers an exhausted primary budget with.
905///
906/// `rate_limited` is the `type` its GraphQL error carries, which is read as a field rather
907/// than looked for in the response text. Pinned and gated exactly as
908/// [`SECONDARY_WORDINGS`] is, and public for the same one reason.
909pub const PRIMARY_WORDINGS: [&str; 3] = [
910 "api rate limit exceeded",
911 "rate limit exceeded",
912 "rate_limited",
913];
914
915/// What a response *says about itself*, which is the only place a refusal can be read.
916///
917/// Deliberately not the whole response body. A board is a place people write about their
918/// own work, and a task on it titled "the secondary rate limit" would, matched across the
919/// raw text, turn a perfectly good answer into a refusal this source then waited out and
920/// reported. So the item data is never read: what is read is GitHub's own REST-style
921/// `message` envelope, which is what a forbidden status carries, and the `message` and
922/// `type` of each GraphQL error, which is where a *successful* response says it.
923///
924/// A body that is not JSON at all has nothing structured to read, so only a failing
925/// response's own text is taken — a successful response that is not JSON is malformed
926/// rather than refused, and [`GitHubProjectsSource::answer`] says so.
927fn refusal_wording(status: StatusCode, body: &str) -> String {
928 let Ok(parsed) = serde_json::from_str::<Value>(body) else {
929 return if status.is_success() {
930 String::new()
931 } else {
932 body.to_owned()
933 };
934 };
935 let mut said: Vec<&str> = parsed
936 .get("message")
937 .and_then(Value::as_str)
938 .into_iter()
939 .collect();
940 if let Some(errors) = parsed.get("errors").and_then(Value::as_array) {
941 for error in errors {
942 said.extend(
943 ["message", "type"]
944 .into_iter()
945 .filter_map(|key| error.get(key).and_then(Value::as_str)),
946 );
947 }
948 }
949 said.join("; ")
950}
951
952impl Limiter {
953 /// Which limiter refused this response, or `None` when none of them did.
954 ///
955 /// The wording is read first and the status only decides what carries none of it,
956 /// because GitHub answers a secondary limit with a forbidden status far more often
957 /// than with too-many-requests — while a forbidden status saying nothing about a limit
958 /// really is a credential this token lacks.
959 ///
960 /// A response is a refusal because of its status or its own wording. A spent budget
961 /// only ever explains one; it never turns an answer into a refusal.
962 fn classify(status: StatusCode, budget_exhausted: bool, body: &str) -> Option<Self> {
963 let normalized = refusal_wording(status, body).to_ascii_lowercase();
964 if SECONDARY_WORDINGS
965 .iter()
966 .any(|wording| normalized.contains(wording))
967 {
968 return Some(Self::Secondary);
969 }
970 if status == StatusCode::TOO_MANY_REQUESTS {
971 return Some(Self::Primary);
972 }
973 // An exhausted budget *explains* a response that failed; it does not make one that
974 // succeeded into a failure. GitHub sets `x-ratelimit-remaining: 0` on the last
975 // request the budget allowed as well as on the ones it then refuses, so reading
976 // the header alone threw away a good answer — and, once refusals were retried,
977 // replayed a request that had already taken effect.
978 if !status.is_success() && budget_exhausted {
979 return Some(Self::Primary);
980 }
981 // A successful response saying it: GitHub reports a GraphQL rate limit in the
982 // `errors` of an HTTP 200, where nothing about the status says so at all.
983 if status.is_success()
984 && PRIMARY_WORDINGS
985 .iter()
986 .any(|wording| normalized.contains(wording))
987 {
988 return Some(Self::Primary);
989 }
990 None
991 }
992
993 /// What this limiter is called where an operator can look it up.
994 const fn name(self) -> &'static str {
995 match self {
996 Self::Primary => "GitHub's primary API rate limit",
997 Self::Secondary => "GitHub's secondary rate limit",
998 }
999 }
1000
1001 /// What the endpoint an operator would go and check says about this limiter.
1002 const fn where_to_look(self) -> &'static str {
1003 match self {
1004 Self::Primary => {
1005 "That is the budget `gh api rate_limit` reports, so that endpoint says when it \
1006 comes back."
1007 }
1008 Self::Secondary => {
1009 "That limiter is not the primary API budget: `gh api rate_limit` reports the \
1010 primary budget and does not report this one, so budget showing there says \
1011 nothing about this refusal, and every further attempt extends it."
1012 }
1013 }
1014 }
1015
1016 /// The next step this limiter actually calls for.
1017 const fn what_to_do(self) -> &'static str {
1018 match self {
1019 Self::Primary => {
1020 "wait for the reset `gh api rate_limit` reports, then run the command again."
1021 }
1022 Self::Secondary => {
1023 "leave this board alone for a few minutes, then run the command again — or \
1024 raise pacing.min_mutation_interval_ms on this source so it writes more slowly."
1025 }
1026 }
1027 }
1028}
1029
1030/// One rate-limit refusal, and the wait GitHub asked for if it asked for one.
1031#[derive(Debug, Clone, Copy)]
1032struct Limited {
1033 limiter: Limiter,
1034 hint: Option<u64>,
1035}
1036
1037impl Limited {
1038 /// What the caller is told once this source has waited as long as it may.
1039 ///
1040 /// Both limiters report as [`SourceError::RateLimited`], because that is what
1041 /// happened: the kind a caller matches on says a rate limit refused this, and nothing
1042 /// about *which* limiter it was makes it a different kind of failure. What differs is
1043 /// the operator's next step, and that is what the message carries — a secondary
1044 /// refusal read as a primary one sends an operator to `gh api rate_limit`, where the
1045 /// budget looks fine, and then back to retry the very burst that was refused.
1046 fn exhausted(
1047 self,
1048 doing: &str,
1049 waits: u32,
1050 waited: Duration,
1051 needed: Duration,
1052 budget: Duration,
1053 ) -> SourceError {
1054 SourceError::RateLimited {
1055 retry_after_seconds: self.hint,
1056 message: Some(format!(
1057 "{} refused this source while {doing}; it waited {} out over {} and was refused \
1058 again, and the next wait of {} would take it past the {} one call may spend \
1059 waiting. {} next: {}",
1060 self.limiter.name(),
1061 plural(waits, "refusal"),
1062 seconds(waited),
1063 seconds(needed),
1064 seconds(budget),
1065 self.limiter.where_to_look(),
1066 self.limiter.what_to_do(),
1067 )),
1068 }
1069 }
1070}
1071
1072/// One HTTP attempt's result, with what its response said about the rate limit.
1073///
1074/// The two travel together so the record and the outcome are written from the same place:
1075/// what a response said about the budget is only readable while that response is in hand,
1076/// and what the attempt *meant* is only decidable once its body has been read.
1077struct Attempted {
1078 result: Result<Value, Attempt>,
1079 limits: accounting::RateLimit,
1080 /// GitHub's own reported cost for this call, for a document that asked for it.
1081 reported_cost: Option<u64>,
1082}
1083
1084/// One attempt's outcome: an error to report, or a rate limit to wait out.
1085enum Attempt {
1086 Failed(SourceError),
1087 Limited(Limited),
1088}
1089
1090fn plural(count: u32, thing: &str) -> String {
1091 if count == 1 {
1092 format!("{count} {thing}")
1093 } else {
1094 format!("{count} {thing}s")
1095 }
1096}
1097
1098fn seconds(duration: Duration) -> String {
1099 format!("{:.1}s", duration.as_secs_f64())
1100}
1101
1102/// A header GitHub spells as a whole number of seconds, or `None` when this one is not.
1103///
1104/// A value that is present and unreadable is deliberately *not* an error. `retry-after` is
1105/// allowed by HTTP to be a date rather than a count, an intermediary can rewrite either
1106/// header, and neither is what makes a response a refusal — so the whole cost of one this
1107/// cannot read is that the refusal carries no hint and the backing-off schedule answers it
1108/// instead. Refusing the response over the header would turn a readable refusal into an
1109/// unreadable one, and refusing to *wait* would be the one wrong direction to fail in.
1110fn whole_seconds(value: Option<&reqwest::header::HeaderValue>) -> Option<u64> {
1111 value
1112 .and_then(|value| value.to_str().ok())
1113 .and_then(|value| value.trim().parse::<u64>().ok())
1114}
1115
1116/// Every mutation this source sends creates content — an issue, a board item, a field of
1117/// one, a sub-issue link, a dependency, a comment — or edits or removes content of that
1118/// kind, and no query in [`graphql::DOCUMENTS`] does, so what the secondary limiter counts
1119/// and what the keyword says are the same set. That is what makes the keyword a sound test
1120/// rather than a convenient one: pacing an edit or a removal the limiter might not have
1121/// counted costs a wait, and not pacing one it did count costs the next fifty minutes.
1122fn is_mutation(query: &str) -> bool {
1123 query.trim_start().starts_with("mutation")
1124}
1125
1126/// What this source was doing, for a diagnostic that has to say so.
1127///
1128/// Read out of [`graphql::DOCUMENTS`], which is the inventory rather than a copy of it, so
1129/// a document added without a description is caught by that list's own gate instead of
1130/// falling through to the vague arm below.
1131fn operation_description(query: &str) -> &'static str {
1132 graphql::DOCUMENTS
1133 .iter()
1134 .find(|(document, _)| *document == query)
1135 .map_or("talking to GitHub", |(_, doing)| *doing)
1136}
1137
1138/// GitHub's published ceiling on content-generating requests, per minute.
1139///
1140/// Pinned in `tests/fixtures/rate-limits.json` and gated against it, because it is
1141/// GitHub's number rather than this source's: [`MIN_MUTATION_INTERVAL_MS`] is *derived*
1142/// from it, so a pacing value checked only against itself cannot go stale here.
1143pub const CONTENT_CREATION_PER_MINUTE: u64 = 80;
1144/// The same ceiling as GitHub publishes it per hour, which this source does **not** pace
1145/// at. See [`MIN_MUTATION_INTERVAL_MS`] for why the per-minute bound is the one that
1146/// governs; it is pinned beside its sibling so the gate would notice either one moving.
1147pub const CONTENT_CREATION_PER_HOUR: u64 = 500;
1148/// Shortest interval between two content-creating mutations, in milliseconds.
1149///
1150/// GitHub documents two secondary limits on content-generating requests:
1151/// [`CONTENT_CREATION_PER_MINUTE`] and [`CONTENT_CREATION_PER_HOUR`]. 60000/80 is 750, so
1152/// a mutation every 750 ms is the fastest rate that cannot exceed the per-minute bound,
1153/// and that is the bound a copy actually trips: a copy of one plan-sized project is a
1154/// burst of a few dozen mutations inside a few seconds. The hourly bound works out at one
1155/// every 7.2 seconds sustained, which no single copy reaches and which, used as the
1156/// spacing here, would turn an ordinary copy into an hour of waiting — so it is
1157/// deliberately *not* what this paces at. An installation that wants the hourly bound
1158/// honoured for a long sequence of copies says so through
1159/// `pacing.min_mutation_interval_ms`.
1160pub const MIN_MUTATION_INTERVAL_MS: u64 = 60_000 / CONTENT_CREATION_PER_MINUTE;
1161/// First wait when a rate-limit refusal carries no hint; each further wait doubles it.
1162///
1163/// A doubling schedule from one second reaches a minute in six waits, which is GitHub's
1164/// own advice for a secondary limit — wait, and wait longer each time — without spending
1165/// the first minute of a transient refusal doing nothing.
1166pub const RETRY_BACKOFF_MS: u64 = 1_000;
1167/// Total time one call may spend waiting out rate limits before it reports a failure.
1168///
1169/// Two minutes is long enough to ride out the refusals a paced copy still collects and
1170/// short enough that a command an operator is watching returns. The bound is what makes
1171/// the wait a wait rather than a hang: a call refused past it ends in a diagnostic naming
1172/// the limiter, not in a process nobody can tell from a wedged one.
1173pub const RETRY_BUDGET_MS: u64 = 120_000;
1174
1175fn default_token_env() -> String {
1176 "GH_PROJECTS_TOKEN".to_owned()
1177}
1178fn default_endpoint() -> String {
1179 "https://api.github.com/graphql".to_owned()
1180}
1181
1182/// Where one status category lands on this board.
1183///
1184/// `null` — an absent value — disables the category for this instance, and using a
1185/// disabled status is a refusal naming the status and the instance.
1186#[derive(Debug, Clone, Deserialize, schemars::JsonSchema)]
1187#[serde(untagged)]
1188pub enum StatusTargetConfig {
1189 /// The name of a `Status` single-select option already on the board.
1190 Column(ColumnName),
1191}
1192
1193/// The name of a `Status` single-select option on the board.
1194///
1195/// Validated on the way in rather than checked later, so a blank option name — which
1196/// nothing on a board can be — is a state this type cannot hold.
1197#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
1198#[serde(try_from = "String")]
1199#[schemars(extend("minLength" = 1))]
1200pub struct ColumnName(String);
1201
1202impl ColumnName {
1203 /// The option name, as the board spells it.
1204 fn as_str(&self) -> &str {
1205 &self.0
1206 }
1207}
1208
1209impl TryFrom<String> for ColumnName {
1210 type Error = String;
1211
1212 fn try_from(name: String) -> Result<Self, Self::Error> {
1213 if name.trim().is_empty() {
1214 return Err("a status_mapping option name cannot be blank".to_owned());
1215 }
1216 Ok(Self(name))
1217 }
1218}
1219
1220/// The two closed states this product can mean.
1221///
1222/// GitHub's `IssueClosedStateReason` also spells `DUPLICATE`, which is neither finished
1223/// work nor abandoned work, so nothing here ever writes it.
1224#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, schemars::JsonSchema)]
1225#[serde(rename_all = "kebab-case")]
1226pub enum ClosedState {
1227 /// `COMPLETED` — precisely done.
1228 Completed,
1229 /// `NOT_PLANNED` — precisely cancelled.
1230 NotPlanned,
1231}
1232
1233impl ClosedState {
1234 const fn reason(self) -> &'static str {
1235 match self {
1236 Self::Completed => "COMPLETED",
1237 Self::NotPlanned => "NOT_PLANNED",
1238 }
1239 }
1240}
1241
1242/// Configuration for one GitHub Projects v2 board.
1243#[derive(Debug, Clone, Default, Deserialize, schemars::JsonSchema)]
1244#[serde(default, deny_unknown_fields)]
1245pub struct GitHubProjectsConfig {
1246 /// Login of the user or organization which owns the board.
1247 pub owner: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates GitHub's owner grammar before private construction.
1248 /// The project number shown in the board's GitHub URL.
1249 pub project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` bounds this to a positive GraphQL Int.
1250 // llmlint: ignore-block[contracts_have_one_source_or_a_drift_gate] This doc is the field's schema description, which is what a person configuring the source reads, so it has to say when the field decides an issue's repository and when the item's own field does; the rule's one executable source is `GitHubProjectsSource::creation_target`, and `tests/plugin.rs` drives each case named here against the loopback board.
1251 /// `owner/name` of the repository this source creates an issue in when the item's own
1252 /// `repositories` field does not decide it.
1253 ///
1254 /// An item naming exactly one repository is created there; a task or a document naming
1255 /// none or several is created in its parent project's repository; and a project, or a
1256 /// task or document with no parent, naming none or several is created here. A board
1257 /// has no repository of its own and `createIssue` requires one, so a write without
1258 /// this is refused naming the field. Reads never need it.
1259 pub repository: Option<String>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the `owner/name` grammar before private construction.
1260 // llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
1261 /// Environment variable containing a fine-grained token with Projects and Issues
1262 /// read/write plus Pull requests read-only access for every repository represented on
1263 /// the board.
1264 #[serde(default = "default_token_env")]
1265 pub token_env: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the environment-variable grammar.
1266 /// GraphQL endpoint. GitHub Enterprise installations may override it.
1267 #[serde(default = "default_endpoint")]
1268 pub endpoint: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` converts it to the private validated `Url`.
1269 /// Per-instance mapping from a status category to where it lands on this board.
1270 ///
1271 /// A category this does not mention keeps its shipped default: `backlog` to
1272 /// "Backlog", `todo` to "Todo", `queued` to "Queued", `in-progress` to "In Progress",
1273 /// `done` to "Done" plus closed as completed, `cancelled` to "Cancelled" plus closed
1274 /// as not planned, and `draft` and `unknown` disabled. `unknown` may name one existing
1275 /// board option; every unknown word then lands on that option and reads back as
1276 /// `unknown` under its name. Unlike `local-md`, this source cannot keep each unknown
1277 /// word because it never creates board options.
1278 #[serde(default)]
1279 pub status_mapping: BTreeMap<String, Option<StatusTargetConfig>>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` parses each key into a `StatusCategory` and reports an unknown one against this instance.
1280 /// How fast this source writes, and how long it waits out a rate-limit refusal.
1281 ///
1282 /// Every field keeps its shipped default when it is absent, and the defaults are
1283 /// GitHub's own published limits rather than taste. See [`Pacing`].
1284 #[serde(default)]
1285 pub pacing: PacingConfig,
1286}
1287
1288/// How fast this source writes, and how long it waits out a rate-limit refusal.
1289///
1290/// Configurable because a GitHub Enterprise installation sets its own limits and an
1291/// operator who has already been refused may want to go slower still — not because the
1292/// defaults are guesses.
1293#[derive(Debug, Clone, Default, Deserialize, schemars::JsonSchema)]
1294#[serde(default, deny_unknown_fields)]
1295pub struct PacingConfig {
1296 /// Shortest interval between two content-creating mutations, in milliseconds.
1297 ///
1298 /// Zero sends them as fast as they are asked for, which is what a fixture server on
1299 /// loopback wants and what no board on github.com does. At most [`MAX_PACING_MS`].
1300 pub min_mutation_interval_ms: Option<u64>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `Pacing::resolve` bounds it to `MAX_PACING_MS` before the private validated `Pacing` is built.
1301 /// First wait when a rate-limit refusal carries no hint, in milliseconds. Each
1302 /// further wait of the same call doubles it. At most [`MAX_PACING_MS`], and never
1303 /// zero while there is a budget to spend, because a schedule of zero-length waits
1304 /// consumes none of it and so never ends.
1305 pub retry_backoff_ms: Option<u64>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `Pacing::resolve` refuses a non-progressing zero and bounds the rest before the private validated `Pacing` is built.
1306 /// Total time one call may spend waiting out rate limits, in milliseconds.
1307 ///
1308 /// Zero reports the refusal rather than waiting at all. At most [`MAX_PACING_MS`]:
1309 /// the bound is what makes this a wait rather than a hang.
1310 pub retry_budget_ms: Option<u64>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `Pacing::resolve` bounds it to `MAX_PACING_MS` before the private validated `Pacing` is built.
1311}
1312
1313/// The largest any pacing setting may be, in milliseconds.
1314///
1315/// One hour. GitHub's own harshest published bound on content-generating requests works
1316/// out at one every 7.2 seconds, so an hour is already three orders of magnitude past
1317/// anything a real limit asks for, and past it the settings stop describing pacing at all:
1318/// a wait budget beyond it is the unbounded wait this whole mechanism exists to replace,
1319/// and an interval beyond it is a command that never sends its second mutation. It also
1320/// keeps the clock arithmetic in [`GitHubProjectsSource::reserve_mutation_slot`] inside
1321/// what an `Instant` can hold on every platform.
1322pub const MAX_PACING_MS: u64 = 3_600_000;
1323
1324/// [`PacingConfig`] with every default resolved and every value checked, which is what the
1325/// source holds.
1326#[derive(Debug, Clone, Copy)]
1327struct Pacing {
1328 min_mutation_interval: Duration,
1329 retry_backoff: Duration,
1330 retry_budget: Duration,
1331}
1332
1333impl Pacing {
1334 /// Resolve one instance's pacing, refusing a configuration that would not pace at all.
1335 fn resolve(config: PacingConfig, instance: &SourceName) -> Result<Self, SourceError> {
1336 let bounded = |value: Option<u64>, default: u64, field: &str| match value {
1337 Some(value) if value > MAX_PACING_MS => Err(SourceError::Config {
1338 message: format!(
1339 "pacing.{field} of source {instance} is {value} ms, and the most any pacing \
1340 setting may be is {MAX_PACING_MS} ms — an hour, which is already far past \
1341 GitHub's own harshest published limit"
1342 ),
1343 }),
1344 Some(value) => Ok(Duration::from_millis(value)),
1345 None => Ok(Duration::from_millis(default)),
1346 };
1347 let retry_backoff = bounded(
1348 config.retry_backoff_ms,
1349 RETRY_BACKOFF_MS,
1350 "retry_backoff_ms",
1351 )?;
1352 let retry_budget = bounded(config.retry_budget_ms, RETRY_BUDGET_MS, "retry_budget_ms")?;
1353 if retry_backoff.is_zero() && !retry_budget.is_zero() {
1354 return Err(SourceError::Config {
1355 message: format!(
1356 "pacing.retry_backoff_ms of source {instance} is 0 while \
1357 pacing.retry_budget_ms is {} ms; a schedule of zero-length waits spends \
1358 none of that budget, so it would retry a refusal forever. Set a backoff of \
1359 at least 1 ms, or set retry_budget_ms to 0 to report a refusal without \
1360 waiting at all",
1361 retry_budget.as_millis()
1362 ),
1363 });
1364 }
1365 Ok(Self {
1366 min_mutation_interval: bounded(
1367 config.min_mutation_interval_ms,
1368 MIN_MUTATION_INTERVAL_MS,
1369 "min_mutation_interval_ms",
1370 )?,
1371 retry_backoff,
1372 retry_budget,
1373 })
1374 }
1375}
1376
1377/// Factory for [`GitHubProjectsSource`].
1378#[derive(Debug, Clone, Copy, Default)]
1379pub struct Plugin;
1380
1381impl SourcePlugin for Plugin {
1382 fn kind(&self) -> &'static str {
1383 KIND
1384 }
1385 fn config_schema(&self) -> Schema {
1386 schema_for!(GitHubProjectsConfig)
1387 }
1388 fn build(
1389 &self,
1390 name: &SourceName,
1391 config: &Value,
1392 secrets: &dyn SecretResolver,
1393 ) -> Result<Box<dyn TaskSource>, SourceError> {
1394 self.build_recording_into(name, config, secrets, Arc::new(Accounting::new()))
1395 }
1396}
1397
1398impl Plugin {
1399 /// Build a source recording every request it sends into an accounting the caller holds.
1400 ///
1401 /// [`SourcePlugin::build`] is this with an accounting of its own, which is what the
1402 /// registry gets. This is for a caller that is also calling GitHub itself and wants one
1403 /// session total rather than two — see [`accounting`] and
1404 /// [`GitHubProjectsSource::recording_into`].
1405 ///
1406 /// # Errors
1407 ///
1408 /// Exactly [`SourcePlugin::build`]'s, with the same source name in front of each:
1409 /// [`SourceError::Config`] for configuration this plugin cannot use and
1410 /// [`SourceError::Auth`] for a credential it cannot find.
1411 pub fn build_recording_into(
1412 &self,
1413 name: &SourceName,
1414 config: &Value,
1415 secrets: &dyn SecretResolver,
1416 ledger: Arc<Accounting>,
1417 ) -> Result<Box<dyn TaskSource>, SourceError> {
1418 let config: GitHubProjectsConfig =
1419 serde_json::from_value(config.clone()).map_err(|e| SourceError::Config {
1420 message: format!("source {name}: {e}"),
1421 })?;
1422 let source = GitHubProjectsSource::recording_into(name, config, secrets, ledger).map_err(
1423 |error| match error {
1424 SourceError::Config { message } => SourceError::Config {
1425 message: format!("source {name}: {message}"),
1426 },
1427 SourceError::Auth { message } => SourceError::Auth {
1428 message: format!("source {name}: {message}"),
1429 },
1430 other => other,
1431 },
1432 )?;
1433 Ok(Box::new(source))
1434 }
1435}
1436
1437/// Where a status category lands on this board, once configuration is resolved.
1438#[derive(Debug, Clone, PartialEq, Eq)]
1439enum StatusTarget {
1440 /// Not usable against this instance.
1441 Disabled,
1442 /// The board's `Status` option of this name.
1443 Column(ColumnName),
1444 /// A closed issue, with both its board option and the reason that says which closed it means.
1445 // llmlint: ignore[invalid_states_unrepresentable] The reason is fixed by the category — `done` closes as completed, `cancelled` as not planned — and this private enum is built in one place, `StatusMapping::new`, which pairs each from the category's own slot. Carrying the reason on the target is what lets every write site that holds only a target derive its `stateInput` from that one resolved model rather than re-deriving it from a category and risking a disagreement with the mapping.
1446 Terminal(ColumnName, ClosedState),
1447}
1448
1449/// Every status category, in the order the vocabulary declares them.
1450///
1451/// This list mirrors `StatusCategory`, so it carries its own drift gate rather than a
1452/// reviewer's attention: [`category_position`] is a wildcard-free match, so a variant
1453/// added to the shared vocabulary fails to compile until it is named there, and this
1454/// crate's suite reconciles this list against that enum's own derived schema, which is
1455/// generated from the variants rather than written beside them. The schema is what
1456/// catches a list left one short — a list checking only the positions it already holds
1457/// would pass while every mapping indexed by the new position panicked.
1458pub const CATEGORIES: [StatusCategory; 8] = [
1459 StatusCategory::Draft,
1460 StatusCategory::Backlog,
1461 StatusCategory::Todo,
1462 StatusCategory::Queued,
1463 StatusCategory::InProgress,
1464 StatusCategory::Done,
1465 StatusCategory::Cancelled,
1466 StatusCategory::Unknown,
1467];
1468
1469/// Where one category sits in [`CATEGORIES`]; see that list for what this pins.
1470#[must_use]
1471pub const fn category_position(category: StatusCategory) -> usize {
1472 match category {
1473 StatusCategory::Draft => 0,
1474 StatusCategory::Backlog => 1,
1475 StatusCategory::Todo => 2,
1476 StatusCategory::Queued => 3,
1477 StatusCategory::InProgress => 4,
1478 StatusCategory::Done => 5,
1479 StatusCategory::Cancelled => 6,
1480 StatusCategory::Unknown => 7,
1481 }
1482}
1483
1484/// The spelling a status category is configured and reported under.
1485fn category_name(category: StatusCategory) -> &'static str {
1486 match category {
1487 StatusCategory::Draft => "draft",
1488 StatusCategory::Backlog => "backlog",
1489 StatusCategory::Todo => "todo",
1490 StatusCategory::Queued => "queued",
1491 StatusCategory::InProgress => "in-progress",
1492 StatusCategory::Done => "done",
1493 StatusCategory::Cancelled => "cancelled",
1494 StatusCategory::Unknown => "unknown",
1495 }
1496}
1497
1498/// A shipped default's option name.
1499///
1500/// The literals below are this file's own and non-blank, and they are validated by the
1501/// one constructor a configured name goes through rather than beside it.
1502fn shipped_column(name: &'static str) -> ColumnName {
1503 ColumnName::try_from(name.to_owned()).expect("a shipped default names a board option")
1504}
1505
1506/// The shipped default for one category, before this instance's configuration.
1507fn shipped_default(category: StatusCategory) -> StatusTarget {
1508 match category {
1509 StatusCategory::Backlog => StatusTarget::Column(shipped_column("Backlog")),
1510 StatusCategory::Todo => StatusTarget::Column(shipped_column("Todo")),
1511 StatusCategory::Queued => StatusTarget::Column(shipped_column("Queued")),
1512 StatusCategory::InProgress => StatusTarget::Column(shipped_column("In Progress")),
1513 StatusCategory::Done => {
1514 StatusTarget::Terminal(shipped_column("Done"), ClosedState::Completed)
1515 }
1516 StatusCategory::Cancelled => {
1517 StatusTarget::Terminal(shipped_column("Cancelled"), ClosedState::NotPlanned)
1518 }
1519 StatusCategory::Draft | StatusCategory::Unknown => StatusTarget::Disabled,
1520 }
1521}
1522
1523/// This instance's complete category-to-target mapping, read in both directions.
1524///
1525/// One target per category, held at that category's own [`category_position`], so a
1526/// category missing from the mapping, named twice in it, or filed out of order is a
1527/// state this type cannot hold rather than one [`Self::target`] has to defend against.
1528#[derive(Debug, Clone)]
1529struct StatusMapping {
1530 targets: [StatusTarget; CATEGORIES.len()],
1531}
1532
1533impl StatusMapping {
1534 fn resolve(
1535 configured: BTreeMap<String, Option<StatusTargetConfig>>,
1536 instance: &SourceName,
1537 ) -> Result<Self, SourceError> {
1538 let mut overrides: BTreeMap<&'static str, Option<StatusTargetConfig>> = BTreeMap::new();
1539 for (key, value) in configured {
1540 let category = CATEGORIES
1541 .iter()
1542 .find(|category| category_name(**category) == key)
1543 .ok_or_else(|| SourceError::Config {
1544 message: format!(
1545 "status_mapping names {key:?}, which is not a status category of source \
1546 {instance}; the categories are {}",
1547 CATEGORIES
1548 .iter()
1549 .map(|category| category_name(*category))
1550 .collect::<Vec<_>>()
1551 .join(", ")
1552 ),
1553 })?;
1554 overrides.insert(category_name(*category), value);
1555 }
1556 // `CATEGORIES[position] == category` for every category — the crate's suite
1557 // asserts it — so mapping the list in order fills each category's own slot.
1558 let targets = CATEGORIES.map(|category| match overrides.remove(category_name(category)) {
1559 None => shipped_default(category),
1560 Some(None) => StatusTarget::Disabled,
1561 Some(Some(StatusTargetConfig::Column(option))) => match category {
1562 StatusCategory::Done => StatusTarget::Terminal(option, ClosedState::Completed),
1563 StatusCategory::Cancelled => {
1564 StatusTarget::Terminal(option, ClosedState::NotPlanned)
1565 }
1566 _ => StatusTarget::Column(option),
1567 },
1568 });
1569 let mapping = Self { targets };
1570 for (index, category) in CATEGORIES.into_iter().enumerate() {
1571 let option = match mapping.target(category) {
1572 StatusTarget::Column(option) | StatusTarget::Terminal(option, _) => option,
1573 StatusTarget::Disabled => continue,
1574 };
1575 if let Some(other) = CATEGORIES[..index].iter().find(|earlier| {
1576 matches!(mapping.target(**earlier), StatusTarget::Column(name) | StatusTarget::Terminal(name, _)
1577 if name.as_str().eq_ignore_ascii_case(option.as_str()))
1578 }) {
1579 return Err(SourceError::Config {
1580 message: format!(
1581 "status_mapping of source {instance} sends both {} and {} to the board \
1582 option {:?}; one option cannot read back as two categories",
1583 category_name(*other),
1584 category_name(category),
1585 option.as_str()
1586 ),
1587 });
1588 }
1589 }
1590 Ok(mapping)
1591 }
1592
1593 fn target(&self, category: StatusCategory) -> &StatusTarget {
1594 &self.targets[category_position(category)]
1595 }
1596
1597 /// The category a board option name reports, or `None` when nothing maps to it.
1598 fn category_of(&self, option: &str) -> Option<StatusCategory> {
1599 CATEGORIES.into_iter().find(|category| {
1600 matches!(self.target(*category), StatusTarget::Column(name) | StatusTarget::Terminal(name, _)
1601 if name.as_str().eq_ignore_ascii_case(option))
1602 })
1603 }
1604
1605 /// The status an item reports, from the three things a read of it says: its board
1606 /// `Status` option, whether its issue is closed, and the reason it was closed with.
1607 ///
1608 /// The closed state decides the category and the `Status` option decides the name, so
1609 /// a closed issue sitting in a "Shipped" column reports `done` named `Shipped`. A
1610 /// closed issue whose reason is `DUPLICATE` or `REOPENED` reports `Unknown`: a
1611 /// duplicate is not finished work, and calling it done is a lie the next copy would
1612 /// write back. `REOPENED`-while-closed is a state this source can never produce, so
1613 /// it is read permissively rather than refused — reads are faithful, and refusals
1614 /// belong on writes.
1615 ///
1616 /// One function of those three rather than of a response, so a narrow status write can
1617 /// answer what a re-read would report by applying it to the state it has just written.
1618 fn status(&self, option: Option<&str>, closed: bool, reason: Option<&str>) -> Status {
1619 if closed {
1620 let category = match reason {
1621 None | Some("COMPLETED") => StatusCategory::Done,
1622 Some("NOT_PLANNED") => StatusCategory::Cancelled,
1623 Some(_) => StatusCategory::Unknown,
1624 };
1625 let fallback = match category {
1626 StatusCategory::Done => "Done",
1627 StatusCategory::Cancelled => "Cancelled",
1628 _ => "Closed",
1629 };
1630 return Status {
1631 category,
1632 name: option.unwrap_or(fallback).to_owned(),
1633 };
1634 }
1635 let name = option.unwrap_or("Open").to_owned();
1636 Status {
1637 category: self.category_of(&name).unwrap_or(StatusCategory::Unknown),
1638 name,
1639 }
1640 }
1641}
1642
1643// llmlint: ignore-block[comments_earn_their_place, contracts_have_one_source_or_a_drift_gate] Every `createIssue` names one of these, and which one is the rule — a reader who reaches the type from `create_and_file_issue` gets the rule in one sentence here without the method's refusals, which stay on `creation_target`, the rule's one executable source; `tests/plugin.rs` drives every arm of it against the loopback board.
1644/// One repository this source can create an issue in, as `owner/name`.
1645///
1646/// Every `createIssue` this source sends names one of these: the item's own single
1647/// `repositories` entry, else its parent project issue's repository, else the configured
1648/// [`GitHubProjectsConfig::repository`]. [`GitHubProjectsSource::creation_target`] makes
1649/// that choice and says what it refuses before `createIssue`.
1650// llmlint: ignore-end[comments_earn_their_place, contracts_have_one_source_or_a_drift_gate]
1651#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
1652struct RepositoryTarget {
1653 owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
1654 name: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
1655}
1656
1657impl RepositoryTarget {
1658 fn parse(value: &str) -> Result<Self, SourceError> {
1659 let (owner, name) = value.split_once('/').ok_or_else(|| SourceError::Config {
1660 message: format!(
1661 "repository must be spelled owner/name; {value:?} names no repository"
1662 ),
1663 })?;
1664 if !valid_github_owner(owner) || !valid_github_repository_name(name) {
1665 return Err(SourceError::Config {
1666 message: format!(
1667 "repository must be spelled owner/name with a GitHub login and one \
1668 repository name; {value:?} is not"
1669 ),
1670 });
1671 }
1672 Ok(Self {
1673 owner: owner.to_owned(),
1674 name: name.to_owned(),
1675 })
1676 }
1677
1678 /// The one host whose repositories this source creates issues in, spelled once: it is
1679 /// what [`Self::origin`] renders and what [`Self::from_origin`] accepts.
1680 const HOST: &str = "github.com";
1681
1682 fn origin(&self) -> String {
1683 format!("{}/{}/{}", Self::HOST, self.owner, self.name)
1684 }
1685
1686 /// The repository a normalized origin names, or why it is none this source can create
1687 /// an issue in: another host, or more or fewer than `owner/name` under this one.
1688 fn from_origin(origin: &Repository) -> Result<Self, String> {
1689 let not_here = || {
1690 format!(
1691 "{} is not a {}/owner/name repository",
1692 origin.as_str(),
1693 Self::HOST
1694 )
1695 };
1696 let (host, rest) = origin.as_str().split_once('/').ok_or_else(not_here)?;
1697 if host != Self::HOST {
1698 return Err(not_here());
1699 }
1700 Self::parse(rest).map_err(|_| not_here())
1701 }
1702
1703 fn slug(&self) -> String {
1704 format!("{}/{}", self.owner, self.name)
1705 }
1706}
1707
1708/// A source which reads GitHub afresh for every operation.
1709pub struct GitHubProjectsSource {
1710 /// This source's configured name, used both to tell a far end naming this source
1711 /// from one naming a system it knows nothing about, and to name the instance a
1712 /// status refusal is about.
1713 name: SourceName,
1714 owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after full GitHub-owner validation.
1715 project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after GraphQL-Int validation.
1716 repository: Option<RepositoryTarget>,
1717 endpoint: Url,
1718 token: SecretString,
1719 credential_name: String, // llmlint: ignore[invalid_states_unrepresentable] Private diagnostic value constructed only after environment-name validation.
1720 statuses: StatusMapping,
1721 client: Client,
1722 /// Every item this source has created since it was built, in the order it created
1723 /// them.
1724 ///
1725 /// GitHub's `projectV2.items` is eventually consistent: an issue added to a board with
1726 /// `addProjectV2ItemById` is routinely absent from the very next read of that board, so
1727 /// a copy resolving a dependency on an item it had just created refused it as not
1728 /// found. A board read is completed from this — an item remembered here and absent from
1729 /// the read is added back, because the board really does hold it and only the read is
1730 /// behind.
1731 ///
1732 /// It is not a cache of a user's work: nothing is remembered that this process did not
1733 /// itself just write, it lives and dies with the process, and it is never consulted for
1734 /// an item this source did not create.
1735 created: Mutex<Vec<Resolved>>,
1736 /// How fast this source writes, and how long it waits out a refusal.
1737 pacing: Pacing,
1738 /// When the last content-creating mutation finished, or the moment the furthest-out
1739 /// reserved slot releases the next one, whichever is later — so the one after it can be
1740 /// spaced from that. See [`MIN_MUTATION_INTERVAL_MS`] for the interval and
1741 /// [`GitHubProjectsSource::finish_mutation`] for why completion rather than release is
1742 /// what it is measured from.
1743 last_mutation: Mutex<Option<Instant>>,
1744 /// The board as this process last read it, for the length of one command.
1745 ///
1746 /// A copy of a project used to re-read the whole board, paged, before writing each of
1747 /// its items, which is by far the largest part of a copy's request count and none of
1748 /// its work. Nothing else changes this board while a command runs — this source's own
1749 /// writes are the only writer — so one read answers them all.
1750 ///
1751 /// It is not a store of a user's work and it is not the cache the no-persistence
1752 /// invariant forbids: it lives and dies with the process exactly as `created` does,
1753 /// nothing is written down, and [`Self::board`] still completes it from `created`, so
1754 /// an item this command created and then depends on resolves whether or not GitHub's
1755 /// own eventually-consistent read has caught up. A write to an item already on the
1756 /// board updates the entry here too, so what this holds is the last read plus this
1757 /// process's own writes rather than a snapshot taken before them.
1758 board_cache: Mutex<Option<Board>>,
1759 /// Every issue this board's own search reported, for the length of one command.
1760 ///
1761 /// The second half of a board read, and cached for the same reason and on the same
1762 /// terms as the first: it lives and dies with the process, nothing is written down, and
1763 /// a write this process makes updates the entry here exactly as it updates the one in
1764 /// [`Self::board_cache`]. One read answers every question a command asks, so a command
1765 /// that lists this board's projects and its tasks pays for one search rather than two.
1766 search_cache: Mutex<Option<Vec<Resolved>>>,
1767 /// Each destination repository's node id, resolved once per repository
1768 /// rather than per issue created.
1769 ///
1770 /// A repository's node id does not change, and re-reading it for every issue of a copy
1771 /// spent one request per item on an answer this source already had. It is a map rather
1772 /// than one entry because a copy files each item in the repository its own
1773 /// `repositories` field names, so a plan across five repositories asks GitHub five
1774 /// times and not once per item.
1775 repository_cache: Mutex<BTreeMap<RepositoryTarget, String>>,
1776 /// What every request this source sends is recorded into.
1777 ///
1778 /// Ordinary code path, not a mode: [`Self::send_once`] records into it at the one place
1779 /// a request leaves this crate, so nothing has to be switched on for a session to be
1780 /// counted. It is shared rather than owned so a caller accounting for a whole session —
1781 /// its own schema verification, board lookups, residue sweep and cleanup beside this
1782 /// source's reads and writes — adds up one accounting instead of two. See
1783 /// [`accounting`] for what a record carries and what a session's spend is and is not.
1784 ledger: Arc<Accounting>,
1785}
1786
1787/// GitHub's closed single-select color vocabulary.
1788#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
1789#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
1790pub enum StatusOptionColor {
1791 /// Gray.
1792 Gray,
1793 /// Blue.
1794 Blue,
1795 /// Green.
1796 Green,
1797 /// Yellow.
1798 Yellow,
1799 /// Purple.
1800 Purple,
1801 /// Red.
1802 Red,
1803 /// Orange.
1804 Orange,
1805 /// Pink.
1806 Pink,
1807}
1808
1809/// Whether the guarded operation plans or applies additions.
1810#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1811pub enum StatusOptionsMode {
1812 /// Read without mutation.
1813 Plan,
1814 /// Apply and verify.
1815 Apply,
1816}
1817
1818/// The explicit result of the requested operation.
1819#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
1820#[serde(rename_all = "kebab-case")]
1821pub enum StatusOptionsOutcome {
1822 /// A read-only plan.
1823 Planned,
1824 /// Apply found nothing missing.
1825 Unchanged,
1826 /// Additions were applied and verified.
1827 Applied,
1828}
1829
1830/// A GitHub single-select option's opaque GraphQL node identifier.
1831#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
1832#[serde(transparent)]
1833pub struct StatusOptionId(#[schemars(length(min = 1))] String);
1834
1835impl TryFrom<String> for StatusOptionId {
1836 type Error = String;
1837
1838 fn try_from(id: String) -> Result<Self, Self::Error> {
1839 if id.trim().is_empty() {
1840 return Err("a GitHub Status option id cannot be blank".to_owned());
1841 }
1842 Ok(Self(id))
1843 }
1844}
1845
1846/// One existing or proposed option in a guarded Status-field update.
1847#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
1848pub struct StatusOption {
1849 /// GitHub's stable id.
1850 pub id: StatusOptionId,
1851 /// The visible option name.
1852 pub name: ColumnName,
1853 /// GitHub's single-select color token.
1854 pub color: StatusOptionColor,
1855 /// The option description, including an empty one.
1856 pub description: String,
1857}
1858
1859/// One board item's Status assignment, retained as recovery data.
1860#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
1861pub struct StatusAssignment {
1862 /// The project item id whose assignment this is.
1863 // llmlint: ignore[invalid_states_unrepresentable] This opaque GraphQL node ID is
1864 // carried verbatim as operator recovery data; introducing a semantic type would claim
1865 // validation rules GitHub does not publish and no operation here interprets.
1866 pub item_id: String,
1867 /// The selected option, absent when the item has no status.
1868 #[serde(skip_serializing_if = "Option::is_none")]
1869 pub option: Option<AssignedStatusOption>,
1870}
1871
1872/// The inseparable id and name of an assigned option.
1873#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
1874pub struct AssignedStatusOption {
1875 /// GitHub's stable id.
1876 pub id: StatusOptionId,
1877 /// The visible name.
1878 pub name: ColumnName,
1879}
1880
1881/// The plan and verified outcome of reconciling configured Status options.
1882#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
1883pub struct StatusOptionsReport {
1884 /// The configured source name.
1885 pub source: SourceName,
1886 /// Configured option names absent before the operation.
1887 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a
1888 // `ColumnName` and has therefore already passed its nonblank validation; retaining the
1889 // serialized string here preserves the report's intentionally simple public contract.
1890 pub missing: Vec<String>,
1891 /// What the requested operation did.
1892 pub outcome: StatusOptionsOutcome,
1893 /// The complete option list observed before any mutation.
1894 pub existing: Vec<StatusOption>,
1895}
1896
1897#[derive(Debug, Clone, PartialEq, Eq)]
1898struct StatusSnapshot {
1899 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
1900 // passed back as the mutation's project identity; a newtype could enforce no stronger
1901 // invariant because GitHub publishes no grammar for it.
1902 board_id: String,
1903 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
1904 // passed back as the mutation's field identity; a newtype could enforce no stronger
1905 // invariant because GitHub publishes no grammar for it.
1906 field_id: String,
1907 options: Vec<StatusOption>,
1908 assignments: Vec<StatusAssignment>,
1909}
1910
1911impl GitHubProjectsSource {
1912 /// Report missing configured Status options and, when `apply` is true, add them with
1913 /// a whole-list mutation that preserves every existing id and verifies the result.
1914 ///
1915 /// # Errors
1916 ///
1917 /// Refuses a board without a single-select `Status` field. A post-write difference in
1918 /// any pre-existing option id or item assignment is refused with the complete pre-write
1919 /// assignment snapshot in the diagnostic for recovery.
1920 // llmlint: ignore[changed_behavior_has_e2e] The CLI journeys cover plan, no-op apply,
1921 // successful mutation, both drift refusals, source selection, missing Status, casing,
1922 // and paging. Transport errors remain the shared `graphql` boundary's behavior rather
1923 // than a new status-options behavior, and the pinned-schema test prevents valid GitHub
1924 // responses from entering the defensive malformed-response branches below.
1925 pub async fn status_options(
1926 &self,
1927 mode: StatusOptionsMode,
1928 ) -> Result<StatusOptionsReport, SourceError> {
1929 let before = self.status_snapshot().await?;
1930 let configured = self
1931 .statuses
1932 .targets
1933 .iter()
1934 // A terminal category's option is as configured as an open one's: a terminal
1935 // write validates it before closing and refuses when the board lacks it.
1936 .filter_map(|target| match target {
1937 StatusTarget::Column(name) | StatusTarget::Terminal(name, _) => {
1938 Some(name.as_str().to_owned())
1939 }
1940 StatusTarget::Disabled => None,
1941 });
1942 let missing = configured
1943 .filter(|wanted| {
1944 !before
1945 .options
1946 .iter()
1947 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
1948 })
1949 .collect::<Vec<_>>();
1950 let report = StatusOptionsReport {
1951 source: self.name.clone(),
1952 missing: missing.clone(),
1953 outcome: match (mode, missing.is_empty()) {
1954 (StatusOptionsMode::Plan, _) => StatusOptionsOutcome::Planned,
1955 (StatusOptionsMode::Apply, true) => StatusOptionsOutcome::Unchanged,
1956 (StatusOptionsMode::Apply, false) => StatusOptionsOutcome::Applied,
1957 },
1958 existing: before.options.clone(),
1959 };
1960 if mode == StatusOptionsMode::Plan || missing.is_empty() {
1961 return Ok(report);
1962 }
1963 let mut options = before
1964 .options
1965 .iter()
1966 .map(|option| {
1967 json!({
1968 "id": option.id, "name": option.name, "color": option.color,
1969 "description": option.description,
1970 })
1971 })
1972 .collect::<Vec<_>>();
1973 options.extend(missing.iter().map(|name| {
1974 json!({
1975 "name": name, "color": "GRAY", "description": ""
1976 })
1977 }));
1978 self.graphql(
1979 graphql::STATUS_OPTIONS_UPDATE,
1980 json!({"input": {
1981 "projectId": before.board_id, "fieldId": before.field_id,
1982 "singleSelectOptions": options,
1983 }}),
1984 )
1985 .await?;
1986 let after = self.status_snapshot().await?;
1987 let options_preserved = before
1988 .options
1989 .iter()
1990 .all(|old| after.options.iter().any(|new| new == old));
1991 let additions_present = missing.iter().all(|wanted| {
1992 after
1993 .options
1994 .iter()
1995 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
1996 });
1997 if !options_preserved || !additions_present || after.assignments != before.assignments {
1998 let recovery = serde_json::to_string_pretty(&before.assignments).map_err(|error| {
1999 SourceError::Malformed {
2000 message: format!("cannot render pre-write Status recovery snapshot: {error}"),
2001 }
2002 })?;
2003 return Err(SourceError::Refused {
2004 message: format!(
2005 "GitHub changed a pre-existing Status option id, name, color or description, or an item assignment after the guarded update; the pre-write item assignment snapshot is:\n{recovery}"
2006 ),
2007 });
2008 }
2009 Ok(report)
2010 }
2011
2012 // llmlint: ignore-block[changed_behavior_has_e2e] Valid snapshot shapes are exercised through
2013 // the real CLI loopback journey, including pagination. The individual malformed guards
2014 // are defensive validation of a schema-pinned third-party response, not separate user
2015 // journeys; drift and missing-field failures cover the operation's recovery behavior.
2016 async fn status_snapshot(&self) -> Result<StatusSnapshot, SourceError> {
2017 let mut after: Option<String> = None;
2018 let mut snapshot: Option<StatusSnapshot> = None;
2019 loop {
2020 let data = self
2021 .graphql(
2022 graphql::STATUS_OPTIONS_SNAPSHOT,
2023 json!({
2024 "owner": self.owner, "number": self.project_number,
2025 "first": MAX_PAGE_SIZE, "after": after, "nestedFirst": MAX_PAGE_SIZE,
2026 }),
2027 )
2028 .await?;
2029 let board = data
2030 .pointer("/owner/projectV2")
2031 .filter(|board| board.is_object())
2032 .ok_or_else(|| SourceError::Refused {
2033 message: format!(
2034 "source {} has no accessible GitHub Projects board",
2035 self.name
2036 ),
2037 })?;
2038 if board
2039 .pointer("/fields/pageInfo/hasNextPage")
2040 .and_then(Value::as_bool)
2041 != Some(false)
2042 {
2043 return Err(SourceError::Malformed {
2044 message:
2045 "GitHub project fields is incomplete or has malformed pageInfo.hasNextPage"
2046 .into(),
2047 });
2048 }
2049 let field = board
2050 .pointer("/fields/nodes")
2051 .and_then(Value::as_array)
2052 .and_then(|fields| {
2053 fields
2054 .iter()
2055 .find(|field| field.get("name").and_then(Value::as_str) == Some("Status"))
2056 })
2057 .ok_or_else(|| SourceError::Refused {
2058 message: format!("source {} board has no Status field", self.name),
2059 })?;
2060 let options = field
2061 .get("options")
2062 .and_then(Value::as_array)
2063 .ok_or_else(|| SourceError::Malformed {
2064 message: "GitHub Status field options is not an array".into(),
2065 })?
2066 .iter()
2067 .map(|option| {
2068 Ok(StatusOption {
2069 id: StatusOptionId::try_from(required_str(option, "id")?.to_owned())
2070 .map_err(|message| SourceError::Malformed { message })?,
2071 name: ColumnName::try_from(required_str(option, "name")?.to_owned())
2072 .map_err(|message| SourceError::Malformed {
2073 message: format!("GitHub Status option name is invalid: {message}"),
2074 })?,
2075 color: serde_json::from_value(
2076 option.get("color").cloned().unwrap_or(Value::Null),
2077 )
2078 .map_err(|error| SourceError::Malformed {
2079 message: format!("GitHub Status option color is invalid: {error}"),
2080 })?,
2081 description: optional_str(option, "description")?
2082 .unwrap_or_default()
2083 .to_owned(),
2084 })
2085 })
2086 .collect::<Result<Vec<_>, SourceError>>()?;
2087 let board_id = required_nonblank_str(board, "id")?.to_owned();
2088 let field_id = required_nonblank_str(field, "id")?.to_owned();
2089 let current = snapshot.get_or_insert_with(|| StatusSnapshot {
2090 board_id,
2091 field_id,
2092 options,
2093 assignments: Vec::new(),
2094 });
2095 let items = board
2096 .pointer("/items/nodes")
2097 .and_then(Value::as_array)
2098 .ok_or_else(|| SourceError::Malformed {
2099 message: "GitHub project items.nodes is not an array".into(),
2100 })?;
2101 for item in items {
2102 let field_values =
2103 item.get("fieldValues")
2104 .ok_or_else(|| SourceError::Malformed {
2105 message: "GitHub project item is missing fieldValues".into(),
2106 })?;
2107 if field_values
2108 .pointer("/pageInfo/hasNextPage")
2109 .and_then(Value::as_bool)
2110 != Some(false)
2111 {
2112 return Err(SourceError::Malformed {
2113 message: "GitHub project item fieldValues is incomplete or has malformed pageInfo.hasNextPage".into(),
2114 });
2115 }
2116 let values = item
2117 .pointer("/fieldValues/nodes")
2118 .and_then(Value::as_array)
2119 .ok_or_else(|| SourceError::Malformed {
2120 message: "GitHub project item fieldValues.nodes is not an array".into(),
2121 })?;
2122 let status = values.iter().find(|value| {
2123 value.pointer("/field/name").and_then(Value::as_str) == Some("Status")
2124 });
2125 current.assignments.push(StatusAssignment {
2126 item_id: required_nonblank_str(item, "id")?.to_owned(),
2127 option: status
2128 .map(|value| {
2129 Ok(AssignedStatusOption {
2130 id: StatusOptionId::try_from(
2131 required_str(value, "optionId")?.to_owned(),
2132 )
2133 .map_err(|message| SourceError::Malformed { message })?,
2134 name: ColumnName::try_from(required_str(value, "name")?.to_owned())
2135 .map_err(|message| SourceError::Malformed {
2136 message: format!(
2137 "GitHub assigned Status name is invalid: {message}"
2138 ),
2139 })?,
2140 })
2141 })
2142 .transpose()?,
2143 });
2144 }
2145 let page = board.get("items").ok_or_else(|| SourceError::Malformed {
2146 message: "GitHub project is missing items".into(),
2147 })?;
2148 let has_next = page
2149 .pointer("/pageInfo/hasNextPage")
2150 .and_then(Value::as_bool)
2151 .ok_or_else(|| SourceError::Malformed {
2152 message: "GitHub project items.pageInfo.hasNextPage is not a boolean".into(),
2153 })?;
2154 if !has_next {
2155 break;
2156 }
2157 let next =
2158 required_nonblank_str(page.get("pageInfo").unwrap_or(&Value::Null), "endCursor")?;
2159 validate_cursor_progress(after.as_deref(), next)?;
2160 after = Some(next.to_owned());
2161 }
2162 snapshot.ok_or_else(|| SourceError::Malformed {
2163 message: "GitHub returned no Status snapshot".into(),
2164 })
2165 }
2166 // llmlint: ignore-end[changed_behavior_has_e2e]
2167
2168 /// Validate configuration and capture the named credential without exposing it.
2169 ///
2170 /// # Errors
2171 ///
2172 /// Returns [`SourceError::Config`] for a configuration this instance cannot use and
2173 /// [`SourceError::Auth`] when the named credential is missing or empty.
2174 pub fn new(
2175 name: &SourceName,
2176 config: GitHubProjectsConfig,
2177 secrets: &dyn SecretResolver,
2178 ) -> Result<Self, SourceError> {
2179 Self::recording_into(name, config, secrets, Arc::new(Accounting::new()))
2180 }
2181
2182 /// The same, recording every request it sends into an accounting the caller holds too.
2183 ///
2184 /// [`Self::new`] is this with an accounting of its own. A caller that is also making
2185 /// its own calls to GitHub — a lane verifying a schema, sweeping residue or cleaning
2186 /// up — passes the one it records those into, so the session total accounts for the
2187 /// whole session rather than for this source's share of it.
2188 ///
2189 /// # Errors
2190 ///
2191 /// Exactly [`Self::new`]'s: [`SourceError::Config`] for a configuration this instance
2192 /// cannot use and [`SourceError::Auth`] when the named credential is missing or empty.
2193 pub fn recording_into(
2194 name: &SourceName,
2195 config: GitHubProjectsConfig,
2196 secrets: &dyn SecretResolver,
2197 ledger: Arc<Accounting>,
2198 ) -> Result<Self, SourceError> {
2199 if !valid_github_owner(&config.owner) {
2200 return Err(SourceError::Config {
2201 message: "owner must be 1-39 ASCII letters, digits, or single hyphens, and cannot start or end with a hyphen".into(),
2202 });
2203 }
2204 if config.project_number == 0 || config.project_number > i32::MAX as u32 {
2205 return Err(SourceError::Config {
2206 message: format!("project_number must be between 1 and {}", i32::MAX),
2207 });
2208 }
2209 if !valid_environment_name(&config.token_env) {
2210 return Err(SourceError::Config {
2211 message: "token_env must be a valid environment-variable name".into(),
2212 });
2213 }
2214 let repository = config
2215 .repository
2216 .as_deref()
2217 .map(RepositoryTarget::parse)
2218 .transpose()?;
2219 let endpoint = Url::parse(&config.endpoint).map_err(|e| SourceError::Config {
2220 message: format!("endpoint is not a valid URL: {e}"),
2221 })?;
2222 if endpoint.scheme() != "https"
2223 && !(endpoint.scheme() == "http"
2224 && endpoint
2225 .host_str()
2226 .is_some_and(|h| h == "127.0.0.1" || h == "localhost" || h == "::1"))
2227 {
2228 return Err(SourceError::Config {
2229 message:
2230 "endpoint must use HTTPS (HTTP is accepted only for a loopback test server)"
2231 .into(),
2232 });
2233 }
2234 let token = secrets.get(&config.token_env).filter(|token| !token.expose_secret().trim().is_empty()).ok_or_else(|| SourceError::Auth {
2235 message: format!("environment variable {} is missing or empty; set it to a fine-grained GitHub token granting Projects and Issues read/write plus Pull requests read-only access for every repository represented on the board", config.token_env),
2236 })?;
2237 Ok(Self {
2238 name: name.clone(),
2239 owner: config.owner,
2240 project_number: config.project_number,
2241 repository,
2242 endpoint,
2243 token,
2244 credential_name: config.token_env,
2245 statuses: StatusMapping::resolve(config.status_mapping, name)?,
2246 client: Client::builder()
2247 .user_agent("onetaskgraph")
2248 .build()
2249 .map_err(|e| SourceError::Config {
2250 message: format!("cannot build HTTP client: {e}"),
2251 })?,
2252 created: Mutex::new(Vec::new()),
2253 pacing: Pacing::resolve(config.pacing, name)?,
2254 last_mutation: Mutex::new(None),
2255 board_cache: Mutex::new(None),
2256 search_cache: Mutex::new(None),
2257 repository_cache: Mutex::new(BTreeMap::new()),
2258 ledger,
2259 })
2260 }
2261
2262 /// A snapshot of every request this source has sent, and what each cost.
2263 ///
2264 /// A value to hold and compare rather than a borrow of the accounting itself, so two
2265 /// of them can sit side by side. When this source was built with
2266 /// [`Self::recording_into`] the snapshot is the whole shared session, which is the
2267 /// point of building it that way.
2268 #[must_use]
2269 pub fn accounting(&self) -> accounting::Session {
2270 self.ledger.snapshot()
2271 }
2272
2273 /// Send one GraphQL document, pacing this source's own mutations and waiting out a
2274 /// rate limit rather than handing it straight back as an error.
2275 ///
2276 /// Retrying is safe for every document here, including the mutations, and the reason
2277 /// is that only a *refusal* is retried: [`Limiter::classify`] rules on a response
2278 /// GitHub sent, and a request GitHub refused for a rate limit did not run, so nothing
2279 /// this replays has already taken effect. An outcome this source cannot know — the
2280 /// send failed, or the body could not be read, so the mutation may well have landed —
2281 /// is [`Attempt::Failed`] in [`send_once`] and leaves this loop without a second
2282 /// attempt. A duplicate write would come from replaying one of those, and none is
2283 /// replayed.
2284 async fn graphql(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
2285 let doing = operation_description(query);
2286 let mut waited = Duration::ZERO;
2287 let mut waits = 0_u32;
2288 let mut backoff = self.pacing.retry_backoff;
2289 loop {
2290 if is_mutation(query) {
2291 let spacing = self.reserve_mutation_slot();
2292 if !spacing.is_zero() {
2293 tokio::time::sleep(spacing).await;
2294 }
2295 }
2296 let attempt = self.send_once(query, &variables).await;
2297 if is_mutation(query) {
2298 self.finish_mutation();
2299 }
2300 let limited = match attempt {
2301 Ok(data) => return Ok(data),
2302 Err(Attempt::Failed(error)) => return Err(error),
2303 Err(Attempt::Limited(limited)) => limited,
2304 };
2305 // GitHub really does send `retry-after: 0`, and retrying at once is the one
2306 // move that extends a secondary limit, so a hint below the schedule's own next
2307 // wait is raised to it.
2308 let wait = match limited.hint {
2309 Some(hint) => Duration::from_secs(hint).max(backoff),
2310 None => backoff,
2311 };
2312 let remaining = self.pacing.retry_budget.saturating_sub(waited);
2313 // A wait of nothing spends none of the budget, so it is exhaustion rather
2314 // than a retry. `Pacing::resolve` rules out every way of configuring one
2315 // except a budget of zero, where reporting the first refusal is the ask.
2316 if wait.is_zero() || wait > remaining {
2317 return Err(limited.exhausted(
2318 doing,
2319 waits,
2320 waited,
2321 wait,
2322 self.pacing.retry_budget,
2323 ));
2324 }
2325 tokio::time::sleep(wait).await;
2326 waited += wait;
2327 waits += 1;
2328 backoff = backoff.saturating_mul(2);
2329 }
2330 }
2331
2332 /// The next moment a content-creating mutation may leave this source, as a wait from
2333 /// now.
2334 ///
2335 /// The slot is reserved under the lock and the waiting happens outside it, so two
2336 /// callers take two slots rather than the same one — and no lock is held across an
2337 /// await.
2338 ///
2339 /// The moment it is spaced from is the previous mutation's *completion*, which
2340 /// [`Self::finish_mutation`] records. See that method for why the release moment on its
2341 /// own is the wrong thing to measure from.
2342 fn reserve_mutation_slot(&self) -> Duration {
2343 if self.pacing.min_mutation_interval.is_zero() {
2344 return Duration::ZERO;
2345 }
2346 // A poisoned lock here costs pacing, not correctness, and refusing the write over
2347 // it would turn an earlier failure into a second one for no gain.
2348 let mut last = self
2349 .last_mutation
2350 .lock()
2351 .unwrap_or_else(std::sync::PoisonError::into_inner);
2352 let now = Instant::now();
2353 // `checked_add` rather than `+`: `Instant + Duration` panics on overflow, and
2354 // pacing is not worth a panic even at a bound `MAX_PACING_MS` already rules out.
2355 let at = last.map_or(now, |previous| {
2356 previous
2357 .checked_add(self.pacing.min_mutation_interval)
2358 .map_or(now, |earliest| earliest.max(now))
2359 });
2360 *last = Some(at);
2361 at.saturating_duration_since(now)
2362 }
2363
2364 /// Record that a content-creating mutation has finished, so the next one is spaced
2365 /// from here rather than from the moment this one was released.
2366 ///
2367 /// This source can only choose when a request *departs*; the limiter counts when it
2368 /// *arrives*, and the two differ by whatever the request spent in transit. Spacing one
2369 /// departure from the last therefore hands the limiter a gap of the interval less that
2370 /// transit, so a source pacing at 750 ms can still be seen arriving faster — which is
2371 /// exactly how a copy paced well inside a board's threshold was refused by it on a
2372 /// slower machine while passing on a quick one.
2373 ///
2374 /// Spacing from completion removes the subtraction rather than budgeting for it. The
2375 /// previous request had already arrived before its response came back, so its arrival
2376 /// is no later than this moment, and the next mutation is released at least the
2377 /// interval after this moment and arrives no earlier than it is released: the gap the
2378 /// limiter measures is therefore at least the interval, whatever transit costs and on
2379 /// whatever platform. The price is that a mutation's own round trip no longer counts
2380 /// towards its spacing, which makes this source slightly slower than the configured
2381 /// rate rather than slightly faster — the safe side of a limit that punishes being
2382 /// wrong by refusing reads for the next fifty minutes.
2383 ///
2384 /// A failed attempt is recorded too: a request refused by the limiter still arrived,
2385 /// and one that never left costs only a wait nobody needed.
2386 fn finish_mutation(&self) {
2387 if self.pacing.min_mutation_interval.is_zero() {
2388 return;
2389 }
2390 // A poisoned lock here costs pacing, not correctness, exactly as in the reservation.
2391 let mut last = self
2392 .last_mutation
2393 .lock()
2394 .unwrap_or_else(std::sync::PoisonError::into_inner);
2395 let now = Instant::now();
2396 // `max` rather than an assignment: a concurrent caller may already have reserved a
2397 // slot further out, and completing this request must never pull that slot back in.
2398 *last = Some(last.map_or(now, |reserved| reserved.max(now)));
2399 }
2400
2401 /// One HTTP attempt, classified into an answer, a rate limit to wait out, or a
2402 /// failure that waiting cannot help — and recorded, whichever of the three it was.
2403 ///
2404 /// This is the one place a request leaves this crate, which is why the accounting is
2405 /// here rather than at each of the callers: a read path added later is counted without
2406 /// anybody remembering to count it, and
2407 /// `the_session_report_counts_every_request_the_board_served_and_what_each_cost` fails
2408 /// when one is not.
2409 async fn send_once(&self, query: &str, variables: &Value) -> Result<Value, Attempt> {
2410 let Attempted {
2411 result,
2412 limits,
2413 reported_cost,
2414 } = self.attempt(query, variables).await;
2415 // No `otherwise` name: every document this source sends is one of its own, and the
2416 // inventory gate on `graphql::DOCUMENTS` is what keeps that true.
2417 let sending = accounting::Request::graphql(query, variables, None, reported_cost);
2418 let outcome = match &result {
2419 Ok(_) => accounting::Outcome::Answered,
2420 Err(Attempt::Limited(_)) => accounting::Outcome::RateLimited,
2421 Err(Attempt::Failed(_)) => accounting::Outcome::Refused,
2422 };
2423 self.ledger.record(sending.finished(outcome, limits));
2424 result
2425 }
2426
2427 /// The attempt itself, with what its response said about the rate limit alongside.
2428 ///
2429 /// The two are returned together rather than recorded here because every one of the
2430 /// early exits below is a different outcome, and a record written at each of them is a
2431 /// record one of them can be added without.
2432 async fn attempt(&self, query: &str, variables: &Value) -> Attempted {
2433 let mut limits = accounting::RateLimit::default();
2434 let mut reported_cost = None;
2435 let result = self
2436 .attempted(query, variables, &mut limits, &mut reported_cost)
2437 .await;
2438 Attempted {
2439 result,
2440 limits,
2441 reported_cost,
2442 }
2443 }
2444
2445 /// One HTTP attempt, filling in what its response said about the rate limit as it goes.
2446 async fn attempted(
2447 &self,
2448 query: &str,
2449 variables: &Value,
2450 limits: &mut accounting::RateLimit,
2451 reported_cost: &mut Option<u64>,
2452 ) -> Result<Value, Attempt> {
2453 let response = self
2454 .client
2455 .post(self.endpoint.clone())
2456 .bearer_auth(self.token.expose_secret())
2457 .json(&json!({"query": query, "variables": variables}))
2458 .send()
2459 .await
2460 .map_err(|e| {
2461 Attempt::Failed(SourceError::Unavailable {
2462 message: format!("GitHub GraphQL request failed: {e}"),
2463 })
2464 })?;
2465 let status = response.status();
2466 let header = |name: &str| whole_seconds(response.headers().get(name));
2467 *limits = accounting::RateLimit::read(|name| {
2468 response
2469 .headers()
2470 .get(name)
2471 .and_then(|value| value.to_str().ok())
2472 .map(str::to_owned)
2473 });
2474 // Exactly `0` is exhaustion and everything else — a count, an empty value, bytes
2475 // that are not text at all — is "not known to be exhausted". This never makes a
2476 // response a refusal on its own: it says which limiter a refusal is attributed to
2477 // and where its hint comes from, so a value this cannot read costs a hint rather
2478 // than an answer.
2479 let exhausted = response
2480 .headers()
2481 .get("x-ratelimit-remaining")
2482 .and_then(|value| value.to_str().ok())
2483 == Some("0");
2484 // `retry-after` is what GitHub asks for when it asks; when it does not and the
2485 // primary budget is spent, `x-ratelimit-reset` says when that budget comes back,
2486 // which is the same question answered as an absolute time. Nothing else here is a
2487 // hint, and a schedule is what answers a refusal that carries none.
2488 let hint = header("retry-after").or_else(|| {
2489 exhausted
2490 .then(|| header("x-ratelimit-reset"))
2491 .flatten()
2492 .map(|reset| reset.saturating_sub(Utc::now().timestamp().max(0).unsigned_abs()))
2493 });
2494 // Read before it is parsed, because the evidence which tells a secondary rate
2495 // limit from a rejected credential is in the body of a response whose status says
2496 // only "forbidden" — and a non-success response was never parsed at all.
2497 let body = response.text().await.map_err(|e| {
2498 Attempt::Failed(SourceError::Unavailable {
2499 message: format!("GitHub GraphQL response could not be read: {e}"),
2500 })
2501 })?;
2502 if let Some(limiter) = Limiter::classify(status, exhausted, &body) {
2503 return Err(Attempt::Limited(Limited { limiter, hint }));
2504 }
2505 if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {
2506 return Err(Attempt::Failed(SourceError::Auth {
2507 message: format!(
2508 "GitHub rejected the configured credential with HTTP {status}; grant it Projects and Issues read/write plus Pull requests read-only access for every repository represented on the board"
2509 ),
2510 }));
2511 }
2512 if !status.is_success() {
2513 return Err(Attempt::Failed(SourceError::Unavailable {
2514 message: format!("GitHub GraphQL returned HTTP {status}"),
2515 }));
2516 }
2517 // GitHub reports what a call cost only when the document asked it to, and no
2518 // document this source sends does — so this is `None` here and carries the figure
2519 // for a caller whose own document selects `rateLimit { cost }`. What it must never
2520 // pick up is a `dryRun` probe's cost, which is some other document's.
2521 *reported_cost = serde_json::from_str::<Value>(&body)
2522 .ok()
2523 .as_ref()
2524 .and_then(|body| body.pointer("/data/rateLimit/cost"))
2525 .and_then(Value::as_u64);
2526 self.answer(&body).map_err(Attempt::Failed)
2527 }
2528
2529 /// What one successful HTTP response says, once its GraphQL errors are read.
2530 fn answer(&self, body: &str) -> Result<Value, SourceError> {
2531 let body: Value = serde_json::from_str(body).map_err(|e| SourceError::Malformed {
2532 message: format!("GitHub returned invalid JSON: {e}"),
2533 })?;
2534 let errors = body
2535 .get("errors")
2536 .map(|value| {
2537 value.as_array().ok_or_else(|| SourceError::Malformed {
2538 message: "GitHub response errors is not an array".into(),
2539 })
2540 })
2541 .transpose()?;
2542 if let Some(errors) = errors.filter(|errors| !errors.is_empty()) {
2543 let messages = errors
2544 .iter()
2545 .filter_map(|e| e.get("message").and_then(Value::as_str))
2546 .collect::<Vec<_>>()
2547 .join("; ");
2548 let message = if messages.is_empty() {
2549 "GitHub returned GraphQL errors".into()
2550 } else {
2551 messages
2552 };
2553 let normalized = message.to_ascii_lowercase();
2554 if normalized.contains("resource not accessible") || normalized.contains("scope") {
2555 return Err(SourceError::Auth {
2556 message: format!(
2557 "{message}; grant {} Projects and Issues read/write plus Pull requests read-only access for every repository represented on the board",
2558 self.credential_name
2559 ),
2560 });
2561 }
2562 return Err(SourceError::Refused { message });
2563 }
2564 body.get("data")
2565 .filter(|data| data.is_object())
2566 .cloned()
2567 .ok_or_else(|| SourceError::Malformed {
2568 message: "GitHub response has no data object".into(),
2569 })
2570 }
2571
2572 // llmlint: ignore[boundary_inputs_validated] GitHub caps nested connections at 100 and
2573 // GraphQL cannot independently page them inside the outer item page. This source page is
2574 // deliberately bounded at that published maximum; the live drift journey exercises it.
2575 async fn board_page(
2576 &self,
2577 items_after: Option<&str>,
2578 items_first: u32,
2579 ) -> Result<Value, SourceError> {
2580 let data = self
2581 .graphql(
2582 graphql::BOARD,
2583 json!({"owner":self.owner,"number":self.project_number,
2584 "first":items_first.min(MAX_PAGE_SIZE),"after":items_after,
2585 "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
2586 )
2587 .await?;
2588 data.pointer("/owner/projectV2")
2589 .filter(|v| !v.is_null())
2590 .cloned()
2591 .ok_or_else(|| SourceError::Refused {
2592 message: format!(
2593 "GitHub project {}/{} was not found or is not visible to the token",
2594 self.owner, self.project_number
2595 ),
2596 })
2597 }
2598
2599 /// The search that finds the issues of this board, narrowed by `also` when it is
2600 /// given.
2601 ///
2602 /// `project:owner/number` is what scopes a search to one board, and `is:issue` is what
2603 /// keeps pull requests out of it: GitHub's `ISSUE` search type covers both, and a pull
2604 /// request is somebody's change rather than a unit of plan. `-has:parent` is *not*
2605 /// here on purpose — GitHub accepts it and silently ignores it, so a project is told
2606 /// from a task by the `parent` field each issue carries rather than by the search.
2607 fn board_search(&self, also: Option<&str>) -> String {
2608 let scope = format!("project:{}/{} is:issue", self.owner, self.project_number);
2609 match also {
2610 Some(also) => format!("{scope} {also}"),
2611 None => scope,
2612 }
2613 }
2614
2615 /// One issue this source reached directly, as the board item a read of the board would
2616 /// have produced — or `None` when this board does not hold it.
2617 ///
2618 /// The board half of an issue rides along on `Issue.projectItems`, so the value handed
2619 /// to [`Self::resolve`] is the very shape a `ProjectV2.items` read gives it: the board
2620 /// item's own id, that item's field values, and the issue as its content. One resolver
2621 /// for both routes is what makes an issue read through a search, through its own node
2622 /// id, or through its project's sub-issues report the same title, the same status, the
2623 /// same labels and the same qualified id.
2624 ///
2625 /// An issue with no entry for *this* board is not this source's to report, which is
2626 /// what keeps an id naming some other repository's issue from being answered as an item
2627 /// of this board. That answer is given about an **exhausted** connection and never
2628 /// about an unread page: the entry is looked for on the page in hand, and only if that
2629 /// page reports more of the connection, in [`Self::board_membership`]'s walk of the
2630 /// rest of it.
2631 async fn resolve_issue(&self, issue: &Value) -> Result<Option<Resolved>, SourceError> {
2632 if optional_str(issue, "__typename")? != Some("Issue") {
2633 return Ok(None);
2634 }
2635 let memberships = issue
2636 .get("projectItems")
2637 .ok_or_else(|| SourceError::Malformed {
2638 message: "GitHub issue is missing projectItems".into(),
2639 })?;
2640 let nodes = memberships
2641 .get("nodes")
2642 .and_then(Value::as_array)
2643 .ok_or_else(|| SourceError::Malformed {
2644 message: "GitHub issue projectItems.nodes is not an array".into(),
2645 })?;
2646 let held = match self.board_entry(nodes) {
2647 Some(held) => held.clone(),
2648 None => {
2649 let info = memberships
2650 .get("pageInfo")
2651 .ok_or_else(|| SourceError::Malformed {
2652 message: "GitHub issue projectItems has no pageInfo".into(),
2653 })?;
2654 // The page held no entry for this board. Whether that means the issue is
2655 // not on it is a question about the rest of the connection, and only a
2656 // connection with no rest answers it here.
2657 if !required_bool(info, "hasNextPage")? {
2658 return Ok(None);
2659 }
2660 let cursor = required_str(info, "endCursor")?;
2661 validate_cursor_progress(None, cursor)?;
2662 let issue_id = required_str(issue, "id")?;
2663 match self.board_membership(issue_id, cursor).await? {
2664 Some(held) => held,
2665 None => return Ok(None),
2666 }
2667 }
2668 };
2669 let item = json!({
2670 "id": required_str(&held, "id")?,
2671 "project": held.get("project"),
2672 "fieldValues": held.get("fieldValues"),
2673 "content": issue,
2674 });
2675 self.resolve(&item)
2676 }
2677
2678 /// This board's own entry among one page of an issue's `Issue.projectItems`.
2679 ///
2680 /// One spelling of *which membership is this board's*, so the page a read carries and
2681 /// the pages [`Self::board_membership`] walks are searched by the same rule.
2682 fn board_entry<'a>(&self, nodes: &'a [Value]) -> Option<&'a Value> {
2683 nodes.iter().find(|node| {
2684 node.pointer("/project/number").and_then(Value::as_u64)
2685 == Some(u64::from(self.project_number))
2686 })
2687 }
2688
2689 /// The rest of one issue's board memberships, from `after`, for this board's entry.
2690 ///
2691 /// The recovery read: a page of memberships that holds no entry for this board says
2692 /// nothing about the memberships past it, so the connection is walked to exhaustion
2693 /// before an issue is reported as one this board does not hold. `Ok(None)` is that
2694 /// positive answer — the whole connection was read and no entry named this board —
2695 /// rather than a failure, and the walk is held to
2696 /// [`validate_cursor_progress`] like every other page walk here, so a source answering
2697 /// with a cursor that does not advance is refused instead of spun on.
2698 async fn board_membership(
2699 &self,
2700 issue: &str,
2701 after: &str,
2702 ) -> Result<Option<Value>, SourceError> {
2703 let mut after = after.to_owned();
2704 loop {
2705 let data = self
2706 .graphql(
2707 graphql::ISSUE_BOARD_ITEMS,
2708 json!({"id":issue,"first":MAX_PAGE_SIZE,"after":after,
2709 "nestedFirst":NESTED_PAGE_SIZE}),
2710 )
2711 .await?;
2712 let Some(connection) = data
2713 .pointer("/node/projectItems")
2714 .filter(|value| !value.is_null())
2715 else {
2716 // The id resolved to nothing, or to something with no memberships to walk —
2717 // which is the same answer as a connection holding no entry for this board.
2718 return Ok(None);
2719 };
2720 let nodes = connection
2721 .get("nodes")
2722 .and_then(Value::as_array)
2723 .ok_or_else(|| SourceError::Malformed {
2724 message: "GitHub issue projectItems.nodes is not an array".into(),
2725 })?;
2726 if let Some(held) = self.board_entry(nodes) {
2727 return Ok(Some(held.clone()));
2728 }
2729 let info = connection
2730 .get("pageInfo")
2731 .ok_or_else(|| SourceError::Malformed {
2732 message: "GitHub issue projectItems has no pageInfo".into(),
2733 })?;
2734 let next = required_bool(info, "hasNextPage")?
2735 .then(|| required_str(info, "endCursor"))
2736 .transpose()?;
2737 match next {
2738 Some(next) => {
2739 validate_cursor_progress(Some(&after), next)?;
2740 after = next.to_owned();
2741 }
2742 None => return Ok(None),
2743 }
2744 }
2745 }
2746
2747 /// One page of a board-scoped issue search, and where the next page resumes.
2748 async fn search_page(
2749 &self,
2750 search: &str,
2751 first: u32,
2752 after: Option<&str>,
2753 ) -> Result<(Vec<Resolved>, Option<String>), SourceError> {
2754 let data = self
2755 .graphql(
2756 graphql::SEARCH_ISSUES,
2757 json!({"search":search,"type":"ISSUE","first":first.min(MAX_PAGE_SIZE),
2758 "after":after,"nestedFirst":NESTED_PAGE_SIZE,
2759 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
2760 )
2761 .await?;
2762 let connection = data.get("search").ok_or_else(|| SourceError::Malformed {
2763 message: "GitHub search response has no search connection".into(),
2764 })?;
2765 let mut found = Vec::new();
2766 for node in connection
2767 .get("nodes")
2768 .and_then(Value::as_array)
2769 .ok_or_else(|| SourceError::Malformed {
2770 message: "GitHub search nodes is not an array".into(),
2771 })?
2772 {
2773 if let Some(resolved) = self.resolve_issue(node).await? {
2774 found.push(resolved);
2775 }
2776 }
2777 let info = connection
2778 .get("pageInfo")
2779 .ok_or_else(|| SourceError::Malformed {
2780 message: "GitHub search connection has no pageInfo".into(),
2781 })?;
2782 let next = required_bool(info, "hasNextPage")?
2783 .then(|| required_str(info, "endCursor"))
2784 .transpose()?
2785 .map(str::to_owned);
2786 if let Some(next) = &next {
2787 validate_cursor_progress(after, next)?;
2788 }
2789 Ok((found, next))
2790 }
2791
2792 /// Every issue this board holds, completed with what this run wrote.
2793 ///
2794 /// The completion is not an optimisation and it is not a cache: GitHub's issue search
2795 /// is an index and is eventually consistent, so an issue this run created seconds ago
2796 /// can be absent from it, and a project listed straight after being written would
2797 /// otherwise be missing from its own board. What is added back is only what this
2798 /// process itself wrote, out of [`Self::created`], which lives and dies with the
2799 /// process.
2800 async fn board_issues(&self) -> Result<Vec<Resolved>, SourceError> {
2801 let found = self.searched_issues().await?;
2802 self.completed_with_written(found, |_| true)
2803 }
2804
2805 /// Every issue this board's own search reports, walked to exhaustion, read once per
2806 /// source.
2807 ///
2808 /// The uncompleted half of [`Self::board_issues`], separated because [`Self::board`]
2809 /// needs it too and the two would otherwise walk the same search twice in one command.
2810 /// See [`Self::search_cache`] for why holding it is the same bargain holding the board
2811 /// is.
2812 async fn searched_issues(&self) -> Result<Vec<Resolved>, SourceError> {
2813 let cached = self.search_cache()?.clone();
2814 if let Some(held) = cached {
2815 return Ok(held);
2816 }
2817 let mut after: Option<String> = None;
2818 let mut found = Vec::new();
2819 let search = self.board_search(None);
2820 loop {
2821 let (page, next) = self
2822 .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
2823 .await?;
2824 found.extend(page);
2825 match next {
2826 Some(next) => after = Some(next),
2827 None => break,
2828 }
2829 }
2830 *self.search_cache()? = Some(found.clone());
2831 Ok(found)
2832 }
2833
2834 /// This process's own view of the board's issues, or the refusal a poisoned lock is.
2835 fn search_cache(
2836 &self,
2837 ) -> Result<std::sync::MutexGuard<'_, Option<Vec<Resolved>>>, SourceError> {
2838 self.search_cache
2839 .lock()
2840 .map_err(|_| SourceError::Unavailable {
2841 message: "this source's view of the board's issues was left inconsistent by an \
2842 earlier failure; next: run the command again"
2843 .into(),
2844 })
2845 }
2846
2847 /// `found`, with everything this run wrote that `keep` accepts and the read did not
2848 /// report.
2849 ///
2850 /// See [`Self::created`] and [`Self::board_issues`] for why a read has to be completed
2851 /// at all: the search index is behind, and a node read of an item filed moments ago can
2852 /// be too.
2853 fn completed_with_written(
2854 &self,
2855 mut found: Vec<Resolved>,
2856 keep: impl Fn(&Resolved) -> bool,
2857 ) -> Result<Vec<Resolved>, SourceError> {
2858 for own in self.created()?.iter().filter(|own| keep(own)) {
2859 if !found.iter().any(|item| item.id == own.id) {
2860 found.push(own.clone());
2861 }
2862 }
2863 Ok(found)
2864 }
2865
2866 /// What resolving one node id reached.
2867 ///
2868 /// Three answers rather than an `Option`, because a board *draft* is none of the other
2869 /// two: it is not an issue, it has no node of its own this source can read the board
2870 /// half off, and its only home is the board's own item connection — so a read of one
2871 /// is completed from there rather than reported as nothing.
2872 async fn reach(&self, id: &NativeId) -> Result<Reached, SourceError> {
2873 let asked = self
2874 .graphql(
2875 graphql::ISSUE,
2876 json!({"id":id.0,"nestedFirst":NESTED_PAGE_SIZE,
2877 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
2878 )
2879 .await;
2880 let data = match asked {
2881 Ok(data) => data,
2882 // A string that is not a node id at all is not a failure to report: it is an id
2883 // this board does not hold, which is what every read of one already answers.
2884 Err(error) if unresolvable_node(&error) => return Ok(Reached::Nothing),
2885 Err(error) => return Err(error),
2886 };
2887 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
2888 return Ok(Reached::Nothing);
2889 };
2890 if optional_str(node, "__typename")? == Some("DraftIssue") {
2891 return Ok(Reached::Draft);
2892 }
2893 Ok(match self.resolve_issue(node).await? {
2894 Some(item) => Reached::Held(Box::new(item)),
2895 None => Reached::Nothing,
2896 })
2897 }
2898
2899 /// One item of this board by its own id, whatever kind it is.
2900 ///
2901 /// Resolved from the identifier alone: no search, board-wide or otherwise. What this
2902 /// run wrote is read first, because a node read of an item created moments ago can
2903 /// still be behind the board field values written onto it — see [`Self::created`].
2904 async fn item_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
2905 if let Some(own) = self.created()?.iter().find(|own| own.id == *id) {
2906 return Ok(Some(own.clone()));
2907 }
2908 match self.reach(id).await? {
2909 Reached::Held(item) => Ok(Some(*item)),
2910 Reached::Nothing => Ok(None),
2911 // The one read that still costs the board: a draft lives nowhere else.
2912 Reached::Draft => Ok(self
2913 .board()
2914 .await?
2915 .items
2916 .into_iter()
2917 .find(|item| item.id == *id)),
2918 }
2919 }
2920
2921 /// Everything filed under one issue of this board, walked to exhaustion — or `None`
2922 /// when that id names nothing here with a sub-issue relationship to walk.
2923 ///
2924 /// `None` and an empty answer are different: `None` is *this is not an issue of this
2925 /// GitHub*, which is what sends a project selector on to be read as a name, and an
2926 /// empty vector is a project that holds nothing.
2927 async fn sub_issues(&self, id: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
2928 let mut after: Option<String> = None;
2929 let mut children = Vec::new();
2930 loop {
2931 let asked = self
2932 .graphql(
2933 graphql::SUB_ISSUES,
2934 json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after,
2935 "nestedFirst":NESTED_PAGE_SIZE,
2936 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
2937 )
2938 .await;
2939 let data = match asked {
2940 Ok(data) => data,
2941 // A string that is not a node id at all is not a failure to report: it is
2942 // the ordinary answer to a selector naming a project by its name.
2943 Err(error) if unresolvable_node(&error) => return Ok(None),
2944 Err(error) => return Err(error),
2945 };
2946 let Some(connection) = data
2947 .pointer("/node/subIssues")
2948 .filter(|value| !value.is_null())
2949 else {
2950 // No such node, or one with no sub-issue relationship — a board draft is
2951 // the one this board can really hold.
2952 return Ok(None);
2953 };
2954 for node in connection
2955 .get("nodes")
2956 .and_then(Value::as_array)
2957 .ok_or_else(|| SourceError::Malformed {
2958 message: "GitHub subIssues.nodes is not an array".into(),
2959 })?
2960 {
2961 if let Some(resolved) = self.resolve_issue(node).await? {
2962 children.push(resolved);
2963 }
2964 }
2965 let info = connection
2966 .get("pageInfo")
2967 .ok_or_else(|| SourceError::Malformed {
2968 message: "GitHub subIssues connection has no pageInfo".into(),
2969 })?;
2970 let next = required_bool(info, "hasNextPage")?
2971 .then(|| required_str(info, "endCursor"))
2972 .transpose()?;
2973 match next {
2974 Some(next) => {
2975 validate_cursor_progress(after.as_deref(), next)?;
2976 after = Some(next.to_owned());
2977 }
2978 None => return Ok(Some(children)),
2979 }
2980 }
2981 }
2982
2983 /// Which issue of this board a project *name* is, or `None` when none is.
2984 ///
2985 /// One bounded query which filters on that name at the server, rather than a walk of
2986 /// every issue the board holds. The name is compared again here: the qualifier narrows
2987 /// what GitHub sends, and this source decides what it names.
2988 async fn project_by_name(&self, name: &str) -> Result<Option<NativeId>, SourceError> {
2989 let search = self.board_search(Some(&title_qualifier(name)));
2990 let (candidates, _) = self.search_page(&search, MAX_PAGE_SIZE, None).await?;
2991 Ok(candidates
2992 .into_iter()
2993 .find(|item| {
2994 item.kind == BoardKind::Work(ItemKind::Project)
2995 && item.title.eq_ignore_ascii_case(name)
2996 })
2997 .map(|item| item.id))
2998 }
2999
3000 /// Everything filed under one project of this board: the sub-issues of the issue that
3001 /// project is.
3002 ///
3003 /// Tasks *and* documents, because a document filed under a project is a sub-issue of it
3004 /// too — the caller keeps the kind it asked for. Nothing about this grows as the board
3005 /// gains projects, or as another project gains tasks.
3006 ///
3007 /// A qualified id names the issue and is asked for its sub-issues directly: one
3008 /// request, no search of any kind. Only a selector GitHub cannot resolve that way is
3009 /// read as a project *name*, which costs the one bounded search
3010 /// [`Self::project_by_name`] makes.
3011 async fn project_children(&self, selector: &NativeId) -> Result<Vec<Resolved>, SourceError> {
3012 let (project, children) = match self.sub_issues(selector).await? {
3013 Some(children) => (selector.clone(), children),
3014 None => match self.project_by_name(&selector.0).await? {
3015 Some(project) => {
3016 let children = self.sub_issues(&project).await?.unwrap_or_default();
3017 (project, children)
3018 }
3019 None => return Ok(Vec::new()),
3020 },
3021 };
3022 self.completed_with_written(children, |own| own.parent.as_ref() == Some(&project))
3023 }
3024
3025 /// Every item on the board: the union of both enumerations GitHub offers of one.
3026 ///
3027 /// Neither contains the other, so neither is dropped — only `ProjectV2.items` reaches a
3028 /// board **draft** and the board's own fields, and only the search reports an item that
3029 /// connection is behind on. The module documentation is where the lag and the
3030 /// measurements behind it are written down.
3031 ///
3032 /// A search result is admitted on the same terms as any other issue this source reaches
3033 /// directly — [`Self::resolve_issue`] keeps it only if that issue's own `projectItems`
3034 /// names *this* board — so an issue the index still believes is here after it was taken
3035 /// off is refused rather than reported.
3036 ///
3037 /// See [`Self::board_cache`]. Both completions happen on every call rather than once,
3038 /// which is what the cache could otherwise have broken.
3039 async fn board(&self) -> Result<Board, SourceError> {
3040 let cached = self.board_cache()?.clone();
3041 let mut board = match cached {
3042 Some(board) => board,
3043 None => {
3044 let read = self.read_board().await?;
3045 *self.board_cache()? = Some(read.clone());
3046 read
3047 }
3048 };
3049 for held in self.searched_issues().await? {
3050 if !board.items.iter().any(|item| item.id == held.id) {
3051 board.items.push(held);
3052 }
3053 }
3054 for own in self.created()?.iter() {
3055 if !board.items.iter().any(|item| item.id == own.id) {
3056 board.items.push(own.clone());
3057 }
3058 }
3059 Ok(board)
3060 }
3061
3062 /// This process's own view of the board, or the refusal a poisoned lock is.
3063 fn board_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<Board>>, SourceError> {
3064 self.board_cache
3065 .lock()
3066 .map_err(|_| SourceError::Unavailable {
3067 message: "this source's view of the board was left inconsistent by an earlier \
3068 failure; next: run the command again"
3069 .into(),
3070 })
3071 }
3072
3073 /// Bring this process's own view of the board up to an item it has just written.
3074 ///
3075 /// A created item goes to `created`, which is what completes a board read GitHub's own
3076 /// eventual consistency has left behind. An item that was already there is replaced
3077 /// where it sits, so a second write of it in the same command reads its real parent
3078 /// rather than the one it had before the first write.
3079 ///
3080 /// "Where it sits" is three places, and missing an earlier one leaves a stale record
3081 /// that wins: an item this same run created is held in `created` and not in the cached
3082 /// board, and `board` completes the cached board *from* `created`, so replacing only
3083 /// the cached copy of such an item replaces nothing and the read still reports the
3084 /// title it was created with. The search is the third, and it is the one an item the
3085 /// board's own projection is behind on sits in *alone* — which is exactly the item this
3086 /// source is least able to re-read, so leaving it out would put the stale title back on
3087 /// the only items the completion in [`Self::board`] exists for.
3088 fn remember_written(&self, item: Resolved, created: bool) -> Result<(), SourceError> {
3089 if created {
3090 self.created()?.push(item);
3091 return Ok(());
3092 }
3093 {
3094 let mut own = self.created()?;
3095 if let Some(held) = own.iter_mut().find(|held| held.id == item.id) {
3096 *held = item;
3097 return Ok(());
3098 }
3099 }
3100 if let Some(board) = self.board_cache()?.as_mut()
3101 && let Some(held) = board.items.iter_mut().find(|held| held.id == item.id)
3102 {
3103 *held = item.clone();
3104 }
3105 if let Some(found) = self.search_cache()?.as_mut()
3106 && let Some(held) = found.iter_mut().find(|held| held.id == item.id)
3107 {
3108 *held = item;
3109 }
3110 Ok(())
3111 }
3112
3113 /// Forget one item this process has just deleted, from every half of its own view.
3114 fn forget(&self, id: &NativeId) -> Result<(), SourceError> {
3115 self.created()?.retain(|own| own.id != *id);
3116 if let Some(board) = self.board_cache()?.as_mut() {
3117 board.items.retain(|item| item.id != *id);
3118 }
3119 if let Some(found) = self.search_cache()?.as_mut() {
3120 found.retain(|item| item.id != *id);
3121 }
3122 Ok(())
3123 }
3124
3125 /// Every page of the board, read from GitHub.
3126 async fn read_board(&self) -> Result<Board, SourceError> {
3127 let mut after: Option<String> = None;
3128 let mut items = Vec::new();
3129 let mut board;
3130 loop {
3131 let page = self.board_page(after.as_deref(), MAX_PAGE_SIZE).await?;
3132 for item in page
3133 .pointer("/items/nodes")
3134 .and_then(Value::as_array)
3135 .ok_or_else(|| SourceError::Malformed {
3136 message: "GitHub project items.nodes is not an array".into(),
3137 })?
3138 {
3139 if let Some(resolved) = self.resolve(item)? {
3140 items.push(resolved);
3141 }
3142 }
3143 let info = page
3144 .pointer("/items/pageInfo")
3145 .ok_or_else(|| SourceError::Malformed {
3146 message: "GitHub project items have no pageInfo".into(),
3147 })?;
3148 let has_next = required_bool(info, "hasNextPage")?;
3149 let next = has_next
3150 .then(|| required_str(info, "endCursor"))
3151 .transpose()?;
3152 board = page.clone();
3153 match next {
3154 Some(next) => {
3155 validate_cursor_progress(after.as_deref(), next)?;
3156 after = Some(next.to_owned());
3157 }
3158 None => break,
3159 }
3160 }
3161 Ok(Board {
3162 id: required_str(&board, "id")?.to_owned(),
3163 fields: board.get("fields").cloned().unwrap_or(Value::Null),
3164 items,
3165 })
3166 }
3167
3168 /// The items this source has created, for completing a board read that is behind.
3169 fn created(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
3170 self.created.lock().map_err(|_| SourceError::Unavailable {
3171 message: "this source's record of what it created in this run was left \
3172 inconsistent by an earlier failure; next: run the command again"
3173 .into(),
3174 })
3175 }
3176
3177 /// One board item as this source reports it, or `None` for content it ignores.
3178 ///
3179 /// A pull request is neither a project nor a task — it is somebody's change, not a
3180 /// unit of plan — and an item whose content the token cannot see has nothing to
3181 /// report at all.
3182 fn resolve(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
3183 let content = item.get("content").ok_or_else(|| SourceError::Malformed {
3184 message: "GitHub project item is missing content".into(),
3185 })?;
3186 if content.is_null() {
3187 return Ok(None);
3188 }
3189 let content_kind = match required_str(content, "__typename")? {
3190 "Issue" => ContentKind::Issue,
3191 "DraftIssue" => ContentKind::DraftIssue,
3192 _ => return Ok(None),
3193 };
3194 let field_values = item
3195 .get("fieldValues")
3196 .ok_or_else(|| SourceError::Malformed {
3197 message: "GitHub project item is missing fieldValues".into(),
3198 })?;
3199 complete_connection(field_values, "project item field values", NESTED_PAGE_SIZE)?;
3200 let nodes = field_values
3201 .get("nodes")
3202 .and_then(Value::as_array)
3203 .ok_or_else(|| SourceError::Malformed {
3204 message: "GitHub project item fieldValues.nodes is not an array".into(),
3205 })?;
3206 if let Some(labels) = content.get("labels") {
3207 complete_connection(labels, "content labels", NESTED_PAGE_SIZE)?;
3208 }
3209 let raw_body = optional_str(content, "body")?.map(str::to_owned);
3210 let (body, slot) = metadata_body(raw_body.clone())?;
3211 let parent = optional_str(content.get("parent").unwrap_or(&Value::Null), "id")?
3212 .map(|id| NativeId(id.to_owned()));
3213 // A draft has no sub-issues to summarise, and GitHub's schema gives it no field
3214 // to read one from; it is a task, and never a project.
3215 let sub_issues = match content_kind {
3216 ContentKind::Issue => sub_issue_total(content)?,
3217 ContentKind::DraftIssue => 0,
3218 };
3219 let content_id = required_str(content, "id")?;
3220 let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
3221 message: format!("GitHub issue {content_id}: {message}"),
3222 })?;
3223 let raw_title = required_str(content, "title")?;
3224 // The design prefix is read *first*, before either of the two rules that separate
3225 // a project from a task. A document is not work whatever sub-issues it has and
3226 // whatever marker it carries, and reading the prefix later would make a design
3227 // issue with none of either an empty project.
3228 let kind = if raw_title.starts_with(DESIGN_TITLE_PREFIX) {
3229 BoardKind::Document
3230 } else if parent.is_some() {
3231 // Being a sub-issue wins outright, and no marker overrides it: an issue filed
3232 // under a project is that project's task even when it has sub-issues of its
3233 // own.
3234 BoardKind::Work(ItemKind::Task)
3235 } else if sub_issues > 0 || marked == Some(ItemKind::Project) {
3236 BoardKind::Work(ItemKind::Project)
3237 } else {
3238 BoardKind::Work(ItemKind::Task)
3239 };
3240 // The title a person wrote, which for a document is the one without the prefix —
3241 // the same way `content` above is the body without this source's metadata slot.
3242 let title = match kind {
3243 BoardKind::Document => raw_title[DESIGN_TITLE_PREFIX.len()..].to_owned(),
3244 BoardKind::Work(_) => raw_title.to_owned(),
3245 };
3246 let own_repository = content
3247 .pointer("/repository/nameWithOwner")
3248 .and_then(Value::as_str)
3249 .map(|origin| Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST)))
3250 .transpose()
3251 .map_err(|message| SourceError::Malformed { message })?;
3252 let repositories = if slot.contains_key(Repository::METADATA_KEY) {
3253 Repository::from_metadata(&slot)
3254 .map_err(|message| SourceError::Malformed { message })?
3255 } else {
3256 own_repository.clone().into_iter().collect()
3257 };
3258 let id = NativeId(content_id.to_owned());
3259 // Read only for a task, because only a task has either list: a project or a
3260 // document holding one of these keys holds nothing this source reports, and the
3261 // keys are left out of its caller-visible metadata all the same.
3262 let (delivers, delivered_by) = if kind == BoardKind::Work(ItemKind::Task) {
3263 let listed = |key: &str| {
3264 TaskRef::from_value(key, &id, Some(&self.name), slot.get(key))
3265 .map_err(|message| SourceError::Malformed { message })
3266 };
3267 (
3268 listed(TaskRef::DELIVERS_KEY)?,
3269 listed(TaskRef::DELIVERED_BY_KEY)?,
3270 )
3271 } else {
3272 (Vec::new(), Vec::new())
3273 };
3274 let (option, closed, reason) = Self::status_parts(nodes, content)?;
3275 Ok(Some(Resolved {
3276 item_id: required_str(item, "id")?.to_owned(),
3277 id,
3278 content_kind,
3279 kind,
3280 title,
3281 body: body.filter(|value| !value.is_empty()),
3282 raw_body,
3283 status: self.statuses.status(option, closed, reason),
3284 option: option.map(str::to_owned),
3285 closed,
3286 delivers,
3287 delivered_by,
3288 labels: labels(content)?,
3289 parent,
3290 origin: text_field(nodes, ORIGIN_FIELD)?.filter(|value| !value.is_empty()),
3291 url: optional_str(content, "url")?.map(str::to_owned),
3292 created_at: optional_time(content, "createdAt")?,
3293 updated_at: optional_time(content, "updatedAt")?,
3294 own_repository,
3295 repositories,
3296 slot,
3297 // Present when the item was reached through its own issue, whose board entry
3298 // names the board; a read of the board's own items has the board already. An
3299 // empty id names nothing a field write could address, so it is read as absent and
3300 // the write goes back to reading the board.
3301 board_id: item
3302 .pointer("/project/id")
3303 .and_then(Value::as_str)
3304 .filter(|id| !id.is_empty())
3305 .map(str::to_owned),
3306 fields: field_definitions(nodes),
3307 }))
3308 }
3309
3310 /// What one board item's status is read from: its `Status` option, whether its issue
3311 /// is closed, and the reason it was closed with. [`StatusMapping::status`] turns the
3312 /// three into the status it reports.
3313 fn status_parts<'a>(
3314 field_values: &'a [Value],
3315 content: &'a Value,
3316 ) -> Result<(Option<&'a str>, bool, Option<&'a str>), SourceError> {
3317 let option = field_values
3318 .iter()
3319 .find(|value| value.pointer("/field/name").and_then(Value::as_str) == Some("Status"))
3320 .map(|value| required_str(value, "name"))
3321 .transpose()?;
3322 let closed = optional_str(content, "state")? == Some("CLOSED");
3323 Ok((option, closed, optional_str(content, "stateReason")?))
3324 }
3325
3326 /// The board Status option this write selects, or the refusal that says why not.
3327 ///
3328 /// The mapped option is required for both open and terminal targets. A terminal write
3329 /// validates it before changing either representation, so it can never fall back to
3330 /// closing an issue whose board cannot display the matching status.
3331 ///
3332 /// Answers the field's id, the option's id, and the option's name as the board spells
3333 /// it — which is the name a read of the item reports once it sits there.
3334 fn column_for(
3335 &self,
3336 board: &Board,
3337 status: &Status,
3338 target: &StatusTarget,
3339 ) -> Result<Option<(String, String, String)>, SourceError> {
3340 let wanted = match target {
3341 StatusTarget::Column(wanted) | StatusTarget::Terminal(wanted, _) => wanted.as_str(),
3342 StatusTarget::Disabled => return Ok(None),
3343 };
3344 let missing = |detail: &str| SourceError::Refused {
3345 message: format!(
3346 "status {} of source {} needs the board Status option {wanted:?}, and {detail}; add that option to the board, or point status_mapping.{} of this source at one it has",
3347 category_name(status.category),
3348 self.name,
3349 category_name(status.category)
3350 ),
3351 };
3352 let Some(field) = Board::field(&board.fields, "Status")? else {
3353 return Err(missing("this board has no Status field"));
3354 };
3355 if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
3356 return Err(missing(
3357 "this board's Status field is not a single-select field",
3358 ));
3359 }
3360 let option = field
3361 .get("options")
3362 .and_then(Value::as_array)
3363 .and_then(|options| {
3364 options.iter().find(|option| {
3365 option
3366 .get("name")
3367 .and_then(Value::as_str)
3368 .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
3369 })
3370 });
3371 match option {
3372 None => Err(missing("this board does not have it")),
3373 Some(option) => Ok(Some((
3374 required_str(field, "id")?.to_owned(),
3375 required_str(option, "id")?.to_owned(),
3376 required_str(option, "name")?.to_owned(),
3377 ))),
3378 }
3379 }
3380
3381 /// The refusal a status that closes an issue is answered with over a board draft.
3382 fn closes_a_draft(&self, category: StatusCategory) -> SourceError {
3383 SourceError::Refused {
3384 message: format!(
3385 "status {} of source {} closes the item's issue, and GitHub draft items have \
3386 no open or closed state",
3387 category_name(category),
3388 self.name
3389 ),
3390 }
3391 }
3392
3393 /// What a status write to one item needs of the board: the board's id and the
3394 /// definition of its `Status` field, read off the item when the item says both.
3395 ///
3396 /// The same reasoning as [`Self::board_for_update`]: a node read of the item names its
3397 /// board, and its `Status` value carries that field's definition, options and all. An
3398 /// item that does not say — no board id, or no `Status` value to read the field off —
3399 /// sends this back to reading the board, as does a board this command has already read.
3400 async fn status_board(&self, item: &Resolved) -> Result<Board, SourceError> {
3401 let defines_status = item
3402 .fields
3403 .iter()
3404 .any(|field| field.get("name").and_then(Value::as_str) == Some("Status"));
3405 if self.board_cache()?.is_none()
3406 && defines_status
3407 && let Some(board_id) = &item.board_id
3408 {
3409 return Ok(Board {
3410 id: board_id.clone(),
3411 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
3412 items: Vec::new(),
3413 });
3414 }
3415 self.board().await
3416 }
3417
3418 /// Set one task's status and nothing else; see [`TaskSource::set_task_status`].
3419 async fn set_status(
3420 &self,
3421 id: &NativeId,
3422 category: StatusCategory,
3423 ) -> Result<Option<Status>, SourceError> {
3424 // Refused before anything is read, in the words a write of the same status is.
3425 let target = self.resolved_target(category)?;
3426 let Some(mut item) = self
3427 .item_by_id(id)
3428 .await?
3429 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
3430 else {
3431 return Ok(None);
3432 };
3433 let board = self.status_board(&item).await?;
3434 let wanted = Status {
3435 category,
3436 name: category_name(category).to_owned(),
3437 };
3438 let (field, option, name) =
3439 self.column_for(&board, &wanted, &target)?
3440 .ok_or_else(|| SourceError::Malformed {
3441 message: format!(
3442 "status {} of source {} names no board Status option",
3443 category_name(category),
3444 self.name
3445 ),
3446 })?;
3447 match &target {
3448 StatusTarget::Terminal(_, reason) => {
3449 if item.content_kind == ContentKind::DraftIssue {
3450 return Err(self.closes_a_draft(category));
3451 }
3452 self.set_item_field(
3453 &board.id,
3454 &item.item_id,
3455 &field,
3456 json!({"singleSelectOptionId": option}),
3457 )
3458 .await?;
3459 self.update_content(
3460 ContentKind::Issue,
3461 &item.id,
3462 json!({"stateInput": state_input(Some(&target))}),
3463 )
3464 .await?;
3465 item.closed = true;
3466 item.status = self
3467 .statuses
3468 .status(Some(&name), true, Some(reason.reason()));
3469 item.option = Some(name);
3470 }
3471 StatusTarget::Column(_) => {
3472 // An option is what an open item's status is, so a closed issue is reopened
3473 // first — sitting closed in the column, it would read back as closed. A draft has
3474 // no state to reopen.
3475 if item.content_kind == ContentKind::Issue && item.closed {
3476 self.update_content(
3477 ContentKind::Issue,
3478 &item.id,
3479 json!({"stateInput": state_input(Some(&target))}),
3480 )
3481 .await?;
3482 item.closed = false;
3483 }
3484 self.set_item_field(
3485 &board.id,
3486 &item.item_id,
3487 &field,
3488 json!({"singleSelectOptionId": option}),
3489 )
3490 .await?;
3491 item.status = self.statuses.status(Some(&name), false, None);
3492 item.option = Some(name);
3493 }
3494 StatusTarget::Disabled => unreachable!("resolved_target refused a disabled status"),
3495 }
3496 let status = item.status.clone();
3497 self.remember_written(item, false)?;
3498 Ok(Some(status))
3499 }
3500
3501 /// Replace one task's `delivered_by` and nothing else; see
3502 /// [`TaskSource::set_delivered_by`].
3503 ///
3504 /// One update of the body, which differs from the body GitHub holds only inside the
3505 /// metadata slot — see [`with_slot`]. A body that would not change is not sent at all.
3506 async fn replace_delivered_by(
3507 &self,
3508 id: &NativeId,
3509 delivered_by: &[TaskRef],
3510 ) -> Result<Option<()>, SourceError> {
3511 let entries = TaskRef::listed(
3512 TaskRef::DELIVERED_BY_KEY,
3513 id,
3514 Some(&self.name),
3515 delivered_by.to_vec(),
3516 )
3517 .map_err(|message| SourceError::Refused { message })?;
3518 let Some(mut item) = self
3519 .item_by_id(id)
3520 .await?
3521 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
3522 else {
3523 return Ok(None);
3524 };
3525 let mut slot = item.slot.clone();
3526 set_task_list(&mut slot, TaskRef::DELIVERED_BY_KEY, &entries);
3527 self.write_slot(&mut item, &slot).await?;
3528 item.delivered_by = entries;
3529 self.remember_written(item, false)?;
3530 Ok(Some(()))
3531 }
3532
3533 /// Set one caller key of the metadata slot of one issue of `kind`, and nothing else;
3534 /// see [`TaskSource::set_task_metadata`].
3535 ///
3536 /// `None` when this board holds no item by that id, or holds one of another kind. The
3537 /// answer is the item as this source now reads it, so what a caller is told the key
3538 /// holds is what the slot holds.
3539 ///
3540 /// A key already holding the value is answered without a write, compared as JSON rather
3541 /// than as the body's bytes: a slot a person spelled with other whitespace would
3542 /// otherwise be re-encoded, which is a write that changes nothing the caller asked for.
3543 async fn set_slot_key(
3544 &self,
3545 id: &NativeId,
3546 kind: BoardKind,
3547 key: &MetadataKey,
3548 value: &Value,
3549 ) -> Result<Option<Resolved>, SourceError> {
3550 let Some(mut item) = self.item_by_id(id).await?.filter(|item| item.kind == kind) else {
3551 return Ok(None);
3552 };
3553 if item.slot.get(key.as_str()) == Some(value) {
3554 return Ok(Some(item));
3555 }
3556 let mut slot = item.slot.clone();
3557 slot.insert(key.as_str().to_owned(), value.clone());
3558 self.write_slot(&mut item, &slot).await?;
3559 self.remember_written(item.clone(), false)?;
3560 Ok(Some(item))
3561 }
3562
3563 /// Put `slot` in one item's metadata slot with a single update of its body, and bring
3564 /// `item` up to what that write left.
3565 ///
3566 /// The body sent differs from the body GitHub holds only inside the slot — see
3567 /// [`with_slot`] — and a body that would not change is not sent at all. It goes through
3568 /// the mutation the item's content takes, so a board draft's body is written with
3569 /// `updateProjectV2DraftIssue` exactly as an issue's is with `updateIssue`.
3570 async fn write_slot(
3571 &self,
3572 item: &mut Resolved,
3573 slot: &BTreeMap<String, Value>,
3574 ) -> Result<(), SourceError> {
3575 let held = item.raw_body.clone().unwrap_or_default();
3576 let body = with_slot(&held, slot)?;
3577 if body != held {
3578 self.update_content(item.content_kind, &item.id, json!({"body": body}))
3579 .await?;
3580 }
3581 let (visible, slot) = metadata_body(Some(body.clone()))?;
3582 item.body = visible.filter(|value| !value.is_empty());
3583 item.raw_body = Some(body);
3584 item.slot = slot;
3585 Ok(())
3586 }
3587
3588 /// This instance's target for a category, refusing one it has disabled.
3589 ///
3590 /// Nothing here mutates the board's option set to make room for a status. GitHub
3591 /// documents `UpdateProjectV2FieldInput.singleSelectOptions` as *"provided values
3592 /// overwrite existing options"*, so no addition is additive and a mistake destroys the
3593 /// field and every item's status.
3594 fn resolved_target(&self, category: StatusCategory) -> Result<StatusTarget, SourceError> {
3595 let target = self.statuses.target(category).clone();
3596 if target != StatusTarget::Disabled {
3597 return Ok(target);
3598 }
3599 Err(SourceError::Refused {
3600 message: if category == StatusCategory::Draft {
3601 format!(
3602 "status draft is disabled for source {}: draft is incompatible with this \
3603 integration because GitHub draft issues cannot have sub-issues, and this \
3604 source stores a project's tasks as its issue's sub-issues",
3605 self.name
3606 )
3607 } else if category == StatusCategory::Unknown {
3608 format!(
3609 "status {} is disabled for source {}; set status_mapping.{} of this source \
3610 to one board Status option name; every word classified unknown is written \
3611 to that one option",
3612 category_name(category),
3613 self.name,
3614 category_name(category)
3615 )
3616 } else {
3617 format!(
3618 "status {} is disabled for source {}; set status_mapping.{} of this source \
3619 to a board Status option name",
3620 category_name(category),
3621 self.name,
3622 category_name(category)
3623 )
3624 },
3625 })
3626 }
3627
3628 async fn set_item_field(
3629 &self,
3630 board_id: &str,
3631 item_id: &str,
3632 field_id: &str,
3633 value: Value,
3634 ) -> Result<(), SourceError> {
3635 let data = self
3636 .graphql(
3637 graphql::UPDATE_FIELD,
3638 json!({"input":{
3639 "projectId":board_id,"itemId":item_id,"fieldId":field_id,"value":value
3640 }}),
3641 )
3642 .await?;
3643 let returned = data
3644 .pointer("/updateProjectV2ItemFieldValue/projectV2Item")
3645 .ok_or_else(|| SourceError::Malformed {
3646 message: "GitHub field update returned no project item".into(),
3647 })?;
3648 if required_str(returned, "id")? != item_id {
3649 return Err(SourceError::Malformed {
3650 message: "GitHub field update returned the wrong project item".into(),
3651 });
3652 }
3653 Ok(())
3654 }
3655
3656 async fn native_dependency_ids(&self, id: &NativeId) -> Result<Vec<String>, SourceError> {
3657 let mut after: Option<String> = None;
3658 let mut ids = Vec::new();
3659 loop {
3660 let data = self
3661 .graphql(
3662 graphql::ISSUE_DEPENDENCIES,
3663 json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after}),
3664 )
3665 .await?;
3666 let connection =
3667 data.pointer("/node/blockedBy")
3668 .ok_or_else(|| SourceError::Malformed {
3669 message: "GitHub dependency response has no blockedBy connection".into(),
3670 })?;
3671 ids.extend(
3672 connection
3673 .get("nodes")
3674 .and_then(Value::as_array)
3675 .ok_or_else(|| SourceError::Malformed {
3676 message: "GitHub dependency response nodes is not an array".into(),
3677 })?
3678 .iter()
3679 .map(|value| required_str(value, "id").map(str::to_owned))
3680 .collect::<Result<Vec<_>, _>>()?,
3681 );
3682 let next = next_cursor(connection)?;
3683 if let Some(next) = &next {
3684 validate_cursor_progress(after.as_deref(), &next.0)?;
3685 }
3686 after = next.map(|cursor| cursor.0);
3687 if after.is_none() {
3688 return Ok(ids);
3689 }
3690 }
3691 }
3692
3693 async fn dependencies(
3694 &self,
3695 id: &NativeId,
3696 near_kind: ItemKind,
3697 direction: Direction,
3698 page: &PageRequest,
3699 ) -> Result<Page<DependencyEdge>, SourceError> {
3700 validate_page(page)?;
3701 let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
3702 let cursor = page.cursor.as_ref().map(|c| c.0.as_str());
3703 let recorded = recorded_offset(cursor, direction)?;
3704 // Asked for even in the recorded phase, whose page reads nothing from the
3705 // connection: `__typename` is what says whether this item has a native
3706 // relationship at all, and that is what decides which far ends the reserved key is
3707 // allowed to hold.
3708 let data = self
3709 .graphql(
3710 graphql::ISSUE_DEPENDENCIES,
3711 json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),
3712 "after":if recorded.is_some() {None} else {cursor}}),
3713 )
3714 .await?;
3715 let node =
3716 data.get("node")
3717 .filter(|v| !v.is_null())
3718 .ok_or_else(|| SourceError::Refused {
3719 message: format!(
3720 "GitHub item {} was not found or does not support dependencies",
3721 id.0
3722 ),
3723 })?;
3724 let connection_name = match direction {
3725 Direction::DependsOn => "blockedBy",
3726 Direction::DependedOnBy => "blocking",
3727 };
3728 // A draft has neither `blockedBy` nor `blocking`, so nothing it depends on can be
3729 // named natively and the reserved key may hold any far end. An issue's connections
3730 // hold issues, and this source reads them at the near item's own level.
3731 let natively_names = (required_str(node, "__typename")? == "Issue").then_some(near_kind);
3732 if let Some(offset) = recorded {
3733 return Ok(recorded_page(
3734 self.recorded_edges(id, near_kind, direction, natively_names, node)
3735 .await?,
3736 offset,
3737 limit,
3738 ));
3739 }
3740 if natively_names.is_none() {
3741 return Ok(recorded_page(
3742 self.recorded_edges(id, near_kind, direction, natively_names, node)
3743 .await?,
3744 0,
3745 limit,
3746 ));
3747 }
3748 let connection = node
3749 .get(connection_name)
3750 .ok_or_else(|| SourceError::Malformed {
3751 message: "GitHub dependency response is missing its connection".into(),
3752 })?;
3753 let nodes = connection
3754 .get("nodes")
3755 .and_then(Value::as_array)
3756 .ok_or_else(|| SourceError::Malformed {
3757 message: "GitHub dependency response nodes is not an array".into(),
3758 })?;
3759 // `from` depends on `to`, always. GitHub spells the same relationship from either
3760 // end — `blockedBy` lists what this item waits on, `blocking` lists what waits on
3761 // it — so the near item is `from` in one direction and `to` in the other.
3762 let items = nodes
3763 .iter()
3764 .map(|value| {
3765 let related = NativeId(required_str(value, "id")?.into());
3766 let related_kind = related_kind(value)?;
3767 let (from, to) = match direction {
3768 Direction::DependsOn => (
3769 DependencyEndpoint::from_native(id.clone(), near_kind),
3770 DependencyEndpoint::from_native(related, related_kind),
3771 ),
3772 Direction::DependedOnBy => (
3773 DependencyEndpoint::from_native(related, related_kind),
3774 DependencyEndpoint::from_native(id.clone(), near_kind),
3775 ),
3776 };
3777 Ok(DependencyEdge {
3778 from,
3779 to,
3780 kind: DependencyKind::Blocks,
3781 })
3782 })
3783 .collect::<Result<Vec<_>, SourceError>>()?;
3784 let mut next = next_cursor(connection)?;
3785 if let Some(next) = &next {
3786 validate_cursor_progress(cursor, &next.0)?;
3787 }
3788 if next.is_none()
3789 && !self
3790 .recorded_edges(id, near_kind, direction, natively_names, node)
3791 .await?
3792 .is_empty()
3793 {
3794 next = Some(Cursor(format!("{RECORDED_CURSOR}0")));
3795 }
3796 Ok(Page { items, next })
3797 }
3798
3799 /// The edges this item records under [`DependencyEdge::RECORDED_KEY`], which is where
3800 /// a far end in another source has to live: no GitHub issue relationship can name one.
3801 ///
3802 /// Only forwards. The reverse of a recorded edge is derived from the far end, and this
3803 /// source never writes one down.
3804 ///
3805 /// The metadata lives in the item's own body slot, and `node` is the dependency read's
3806 /// own answer, which carries an issue's body — so an issue's recorded edges cost no
3807 /// request beyond the read already made, and reading the board for them would be a
3808 /// walk of every item for one field of one. A draft has no body in that answer, because
3809 /// a draft lives only inside the board, so a draft's are read off the board as before.
3810 async fn recorded_edges(
3811 &self,
3812 id: &NativeId,
3813 near_kind: ItemKind,
3814 direction: Direction,
3815 natively_names: Option<ItemKind>,
3816 node: &Value,
3817 ) -> Result<Vec<DependencyEdge>, SourceError> {
3818 if direction != Direction::DependsOn {
3819 return Ok(Vec::new());
3820 }
3821 let slot = match node.get("body") {
3822 Some(body) if natively_names.is_some() => {
3823 metadata_body(body.as_str().map(str::to_owned))?.1
3824 }
3825 _ => {
3826 let Some(item) = self
3827 .board()
3828 .await?
3829 .items
3830 .into_iter()
3831 .find(|item| item.id == *id)
3832 else {
3833 return Ok(Vec::new());
3834 };
3835 item.slot
3836 }
3837 };
3838 DependencyEdge::recorded(&slot, id, near_kind, &self.name, natively_names)
3839 .map_err(|message| SourceError::Malformed { message })
3840 }
3841
3842 fn configured_repository(&self) -> Result<&RepositoryTarget, SourceError> {
3843 self.repository
3844 .as_ref()
3845 .ok_or_else(|| SourceError::Refused {
3846 message: format!(
3847 "source {} has no repository configured, and a GitHub Projects board has no \
3848 repository of its own to create an issue in; set repository: owner/name on \
3849 this source",
3850 self.name
3851 ),
3852 })
3853 }
3854
3855 /// The repository one new issue is created in, under the rule [`RepositoryTarget`]
3856 /// states.
3857 ///
3858 /// The fallback is demanded first, whichever arm answers: a write without a configured
3859 /// repository is refused naming the field exactly as it was before the rule existed,
3860 /// so a source that could not write before cannot write now, rather than writing for
3861 /// the one item whose own field happens to decide it.
3862 ///
3863 /// Everything this refuses is refused before `createIssue`, so a refusal leaves no
3864 /// issue behind: an entry that is not a repository on [`RepositoryTarget::HOST`], an
3865 /// entry owned by someone other than the owner of the parent issue's repository —
3866 /// GitHub accepts a sub-issue from another repository of the same owner and from no
3867 /// other, so `addSubIssue` would refuse it after the issue existed — a parent the
3868 /// board does not hold, and a parent that is a draft, which GitHub gives no sub-issues,
3869 /// both of which `addSubIssue` would likewise refuse too late. Whether the entry exists
3870 /// and is visible to the token is checked where its node id is resolved, still before
3871 /// `createIssue`. The parent is read off `board`, which is completed from
3872 /// this process's own record, so a project created moments ago in this command answers
3873 /// though GitHub's board read has not caught up.
3874 fn creation_target(
3875 &self,
3876 board: &Board,
3877 incoming: &Incoming<'_>,
3878 ) -> Result<RepositoryTarget, SourceError> {
3879 let fallback = self.configured_repository()?;
3880 let what = |incoming: &Incoming<'_>| {
3881 format!(
3882 "{} {:?}",
3883 incoming.written.kind().describes(),
3884 incoming.title
3885 )
3886 };
3887 let parent = incoming
3888 .parent
3889 .map(|parent| {
3890 board
3891 .items
3892 .iter()
3893 .find(|item| item.id == *parent)
3894 .ok_or_else(|| SourceError::Refused {
3895 message: format!(
3896 "GitHub project issue {} was not found on the board of source {}, \
3897 so {} cannot be filed under it",
3898 parent.0,
3899 self.name,
3900 what(incoming)
3901 ),
3902 })
3903 })
3904 .transpose()?;
3905 let parents_repository = parent
3906 .map(|parent| {
3907 // A draft is on the board and so is found, but it has no repository to
3908 // place a task in and GitHub gives it no sub-issues, so `addSubIssue`
3909 // would refuse the task only once `createIssue` had made it.
3910 if parent.content_kind == ContentKind::DraftIssue {
3911 return Err(SourceError::Refused {
3912 message: format!(
3913 "GitHub project item {} on the board of source {} is a draft, \
3914 which cannot have sub-issues, so {} cannot be filed under it",
3915 parent.id.0,
3916 self.name,
3917 what(incoming)
3918 ),
3919 });
3920 }
3921 // An issue's repository is where a sub-issue is placed and whose owner it
3922 // is compared against, so a parent whose repository this source cannot
3923 // spell as `owner/name` — GitHub's login grammar is wider than this
3924 // source's floor — is one nothing can be filed under.
3925 parent
3926 .own_repository
3927 .as_ref()
3928 .and_then(|origin| RepositoryTarget::from_origin(origin).ok())
3929 .ok_or_else(|| SourceError::Malformed {
3930 message: format!(
3931 "GitHub project issue {} on the board of source {} is in {}, which \
3932 is not a {}/owner/name repository this source can place {} in",
3933 parent.id.0,
3934 self.name,
3935 parent
3936 .own_repository
3937 .as_ref()
3938 .map_or("no repository", Repository::as_str),
3939 RepositoryTarget::HOST,
3940 what(incoming)
3941 ),
3942 })
3943 })
3944 .transpose()?;
3945 match incoming.repositories {
3946 [named] => {
3947 let target =
3948 RepositoryTarget::from_origin(named).map_err(|_| SourceError::Refused {
3949 message: format!(
3950 "{} names repository {}, which is not a {}/owner/name repository \
3951 source {} can create an issue in; name one that is, or name none",
3952 what(incoming),
3953 named.as_str(),
3954 RepositoryTarget::HOST,
3955 self.name
3956 ),
3957 })?;
3958 if let Some(parents) = &parents_repository
3959 && parents.owner != target.owner
3960 {
3961 return Err(SourceError::Refused {
3962 message: format!(
3963 "{} names repository {}, owned by {}, but its project's issue is in \
3964 {}, owned by {}, and GitHub files a sub-issue only in a repository \
3965 of the same owner as its parent issue; name a repository of {}, or \
3966 name none",
3967 what(incoming),
3968 target.slug(),
3969 target.owner,
3970 parents.slug(),
3971 parents.owner,
3972 parents.owner
3973 ),
3974 });
3975 }
3976 Ok(target)
3977 }
3978 _ => Ok(parents_repository.unwrap_or_else(|| fallback.clone())),
3979 }
3980 }
3981
3982 /// The node id of the repository `incoming` is being created in, or the refusal naming
3983 /// the item and the repository the token cannot see.
3984 ///
3985 /// Resolved once per command per repository; see [`Self::repository_cache`].
3986 async fn repository_id(
3987 &self,
3988 repository: &RepositoryTarget,
3989 incoming: &Incoming<'_>,
3990 ) -> Result<String, SourceError> {
3991 if let Some(id) = self.repository_cache()?.get(repository).cloned() {
3992 return Ok(id);
3993 }
3994 let data = self
3995 .graphql(
3996 graphql::REPOSITORY,
3997 json!({"owner":repository.owner,"name":repository.name}),
3998 )
3999 .await?;
4000 let node = data
4001 .get("repository")
4002 .filter(|value| !value.is_null())
4003 .ok_or_else(|| SourceError::Refused {
4004 message: format!(
4005 "GitHub repository {} was not found or is not visible to the token, so {} \
4006 {:?} cannot be created in it",
4007 repository.slug(),
4008 incoming.written.kind().describes(),
4009 incoming.title
4010 ),
4011 })?;
4012 let id = required_str(node, "id")?.to_owned();
4013 self.repository_cache()?
4014 .insert(repository.clone(), id.clone());
4015 Ok(id)
4016 }
4017
4018 fn repository_cache(
4019 &self,
4020 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<RepositoryTarget, String>>, SourceError> {
4021 self.repository_cache
4022 .lock()
4023 .map_err(|_| SourceError::Unavailable {
4024 message: "this source's record of the destination repository was left \
4025 inconsistent by an earlier failure; next: run the command again"
4026 .into(),
4027 })
4028 }
4029
4030 /// Create or update one board item, whichever kind it is.
4031 async fn write_item(
4032 &self,
4033 incoming: &Incoming<'_>,
4034 target: Option<&NativeId>,
4035 depends_on: &[DependencyEdge],
4036 ) -> Result<NativeId, SourceError> {
4037 // Refused before anything is read or written: a task or a project titled the way
4038 // this board spells a document would land as an issue this same source reads back
4039 // as a document, so the field this destination cannot carry is named rather than
4040 // written and silently reclassified.
4041 if let Written::Work(kind, _) = incoming.written
4042 && incoming.title.starts_with(DESIGN_TITLE_PREFIX)
4043 {
4044 return Err(SourceError::Refused {
4045 message: format!(
4046 "the title of this {} begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
4047 spells a document, so it would read back as one rather than as a {}; \
4048 retitle it, or copy it as a document",
4049 kind.marker(),
4050 self.name,
4051 kind.marker()
4052 ),
4053 });
4054 }
4055 let board = match target {
4056 Some(target) => match self.board_for_update(target, incoming, depends_on).await? {
4057 Some(board) => board,
4058 None => self.board().await?,
4059 },
4060 None => self.board().await?,
4061 };
4062 let status_target = incoming
4063 .written
4064 .status()
4065 .map(|status| self.resolved_target(status.category))
4066 .transpose()?;
4067 let column = match (incoming.written.status(), status_target.as_ref()) {
4068 (Some(status), Some(target)) => self.column_for(&board, status, target)?,
4069 _ => None,
4070 };
4071 let existing = target
4072 .map(|target| {
4073 board
4074 .items
4075 .iter()
4076 .find(|item| item.id == *target)
4077 .ok_or_else(|| SourceError::Refused {
4078 message: format!("GitHub destination item {} was not found", target.0),
4079 })
4080 })
4081 .transpose()?;
4082 let content_kind = existing.map_or(ContentKind::Issue, |item| item.content_kind);
4083 if content_kind == ContentKind::DraftIssue {
4084 if let (Some(StatusTarget::Terminal(_, _)), Some(status)) =
4085 (status_target.as_ref(), incoming.written.status())
4086 {
4087 return Err(self.closes_a_draft(status.category));
4088 }
4089 if incoming.parent.is_some() {
4090 return Err(SourceError::Refused {
4091 message: "GitHub draft items cannot be a project's sub-issue".into(),
4092 });
4093 }
4094 }
4095 match existing {
4096 Some(item) if content_kind == ContentKind::Issue => {
4097 if item.labels != incoming.labels {
4098 return Err(SourceError::Refused {
4099 message: "GitHub issue labels differ from the labels being written".into(),
4100 });
4101 }
4102 }
4103 _ => {
4104 if !incoming.labels.is_empty() {
4105 return Err(SourceError::Refused {
4106 message: "GitHub items created by this destination carry no labels".into(),
4107 });
4108 }
4109 }
4110 }
4111
4112 // An existing issue is never moved; a new one is created where the rule says. The
4113 // repository the issue really lives in is what the slot below is written against,
4114 // so a single entry that is where the issue is created travels as no key at all,
4115 // and the read side derives it back from the issue.
4116 let (own_repository, creation_target) = match existing {
4117 Some(item) => (item.own_repository.clone(), None),
4118 None => {
4119 let target = self.creation_target(&board, incoming)?;
4120 let origin = Repository::try_from(target.origin())
4121 .map_err(|message| SourceError::Config { message })?;
4122 (Some(origin), Some(target))
4123 }
4124 };
4125 let (native, fallback) = self
4126 .partition_edges(&board, incoming.written.kind(), content_kind, depends_on)
4127 .await?;
4128 let slot = slot_metadata(incoming, own_repository.as_ref(), &fallback);
4129 let body = compose_body(incoming.content, &slot)?;
4130 // Read before anything is created, for the reason the field below is: a value
4131 // this destination cannot store has to refuse, and refusing after `createIssue`
4132 // would leave an issue behind that nothing asked for. The engine writes a
4133 // qualified id here; a caller handing this key anything else is told so rather
4134 // than having it silently stored as no origin at all.
4135 // llmlint: ignore[boundary_inputs_validated, changed_behavior_has_e2e] The qualified id's syntax is the engine's and not this plugin's to police: `GlobalId` is deliberately absent from the contract crate because a plugin never sees a qualified id (AGENTS.md), no plugin crate may depend on the engine to parse one, and `docs/metadata.md` says the contents of this key are what no plugin constructs or interprets. What this boundary owns is whether the value is a string its text field can hold, and that is what it checks.
4136 let origin = match incoming.metadata.get(ORIGIN_KEY) {
4137 None => "",
4138 Some(Value::String(origin)) => origin.as_str(),
4139 Some(other) => {
4140 return Err(SourceError::Refused {
4141 message: format!(
4142 "{ORIGIN_KEY} holds a qualified id spelled as a string, and this item's \
4143 is {other}"
4144 ),
4145 });
4146 }
4147 };
4148 // Resolved before anything is created: a board that cannot carry the copy origin
4149 // has to refuse the write, and refusing it after `createIssue` would leave an
4150 // issue behind that nothing asked for.
4151 let origin_field = match Board::field(&board.fields, ORIGIN_FIELD)? {
4152 Some(field) => {
4153 if required_str(field, "__typename")? != "ProjectV2Field" {
4154 return Err(SourceError::Refused {
4155 message: format!(
4156 "GitHub board source-owned {ORIGIN_FIELD} field is not a text field"
4157 ),
4158 });
4159 }
4160 Some(required_str(field, "id")?.to_owned())
4161 }
4162 None if incoming.metadata.contains_key(ORIGIN_KEY) => {
4163 return Err(SourceError::Refused {
4164 message: format!(
4165 "GitHub board has no source-owned {ORIGIN_FIELD} text field, and the \
4166 item carries {ORIGIN_KEY}; add a text field named {ORIGIN_FIELD} to \
4167 the board"
4168 ),
4169 });
4170 }
4171 None => None,
4172 };
4173
4174 let (content_id, item_id, url) = match existing {
4175 Some(item) => {
4176 self.update_existing(item, incoming, &body, status_target.as_ref())
4177 .await?;
4178 (item.id.clone(), item.item_id.clone(), item.url.clone())
4179 }
4180 None => {
4181 let target = creation_target
4182 .as_ref()
4183 .ok_or_else(|| SourceError::Malformed {
4184 message: "a new item was decided without a repository to create it in"
4185 .into(),
4186 })?;
4187 self.create_and_file_issue(&board, target, incoming, &body)
4188 .await?
4189 }
4190 };
4191
4192 let written_option = column.as_ref().map(|(_, _, name)| name.clone());
4193 let column = column.map(|(field, option, _)| (field, option));
4194 // Creating an item here is several calls — `createIssue`, `addProjectV2ItemById`,
4195 // then each board field, the parent and the dependencies — and GitHub can fail at
4196 // any of them. Everything this source can refuse *before* the first of those is
4197 // already checked above, so what is left is GitHub itself failing part way. When it
4198 // does over an item this call created, the issue is taken back: a write that
4199 // refused must not leave an item behind that nobody asked for, and one that does
4200 // makes the retry create a second.
4201 let landed = self
4202 .finish_write(
4203 &board,
4204 incoming,
4205 &content_id,
4206 &item_id,
4207 content_kind,
4208 existing,
4209 origin_field.as_deref(),
4210 origin,
4211 column,
4212 status_target.as_ref(),
4213 &native,
4214 )
4215 .await;
4216 if let Err(error) = landed {
4217 if existing.is_none() {
4218 // Best effort, and the write's own failure is what the caller is told: a
4219 // refusal naming the tidy-up would hide why the write failed at all.
4220 let _ = self.delete_issue(&content_id).await;
4221 }
4222 return Err(error);
4223 }
4224
4225 let written_status = match (incoming.written.status(), status_target.as_ref()) {
4226 (Some(_), Some(StatusTarget::Terminal(_, reason))) => {
4227 self.statuses
4228 .status(written_option.as_deref(), true, Some(reason.reason()))
4229 }
4230 (Some(_), Some(StatusTarget::Column(_))) => {
4231 self.statuses.status(written_option.as_deref(), false, None)
4232 }
4233 (Some(status), _) => status.clone(),
4234 (None, _) => Status {
4235 category: StatusCategory::Unknown,
4236 name: "Open".to_owned(),
4237 },
4238 };
4239
4240 // So the rest of this command reads what it just did rather than what the board
4241 // said before it. See `remember_written` for which half takes it.
4242 let remembered = Resolved {
4243 item_id,
4244 id: content_id.clone(),
4245 content_kind,
4246 kind: incoming.written.kind(),
4247 title: incoming.title.to_owned(),
4248 // The visible half of the body this write composed, split back off it the
4249 // way a read splits it — so what this record reports is what a read of the
4250 // same issue reports, rather than the person's text with the metadata slot
4251 // still on the end of it.
4252 body: metadata_body(body.clone())?.0,
4253 raw_body: body.clone(),
4254 // A document has no status of its own; what it reads back as is whatever
4255 // the issue's own state says, which is what a re-read reports.
4256 status: written_status,
4257 option: written_option.or_else(|| existing.and_then(|item| item.option.clone())),
4258 // What `state_input` asked for: closed for a terminal target, open for any other
4259 // status, and the issue's own state left as it was by a document write.
4260 closed: content_kind == ContentKind::Issue
4261 && match status_target.as_ref() {
4262 Some(StatusTarget::Terminal(_, _)) => true,
4263 Some(_) => false,
4264 None => existing.is_some_and(|item| item.closed),
4265 },
4266 delivers: incoming.delivers.to_vec(),
4267 delivered_by: incoming.delivered_by.to_vec(),
4268 labels: incoming.labels.to_vec(),
4269 parent: incoming.parent.cloned(),
4270 origin: (!origin.is_empty()).then(|| origin.to_owned()),
4271 // In the update path this is the item's own url, read off `existing` where the
4272 // tuple above was bound, so one expression serves both halves.
4273 url,
4274 created_at: existing.and_then(|item| item.created_at),
4275 updated_at: existing.and_then(|item| item.updated_at),
4276 own_repository,
4277 repositories: incoming.repositories.to_vec(),
4278 slot,
4279 board_id: Some(board.id.clone()),
4280 fields: board
4281 .fields
4282 .get("nodes")
4283 .and_then(Value::as_array)
4284 .cloned()
4285 .unwrap_or_default(),
4286 };
4287 self.remember_written(remembered, existing.is_none())?;
4288 Ok(content_id)
4289 }
4290
4291 /// What an update of an item this board already holds needs of the board, read off that
4292 /// item rather than off the board — or `None` when the item cannot say enough, and the
4293 /// board has to be read after all.
4294 ///
4295 /// An update needs the item it updates, the board's own id for a field write, the
4296 /// definitions of the `Status` and origin fields it writes, and every same-source far end
4297 /// its edges name. The first three ride along on the item's own node read — its board
4298 /// entry names the board's id, and each field value on it carries the definition of the
4299 /// field it is a value of — and each far end is read by its own node id. So updating one
4300 /// item costs a read of that item rather than every page of the board, which is what a
4301 /// copy naming one member out of many is for.
4302 ///
4303 /// **Nothing the item does not say is guessed.** A field this item holds no value of may
4304 /// still be on the board, and a view that read it as absent would refuse a write the
4305 /// board can take, or skip a field write the board needs. So a board this command has
4306 /// already read is used as it is, and an item that does not name its board, holds no
4307 /// value of the origin field, or holds no `Status` value when the write carries a status,
4308 /// sends the write back to reading the board — exactly as every update read it before.
4309 async fn board_for_update(
4310 &self,
4311 target: &NativeId,
4312 incoming: &Incoming<'_>,
4313 depends_on: &[DependencyEdge],
4314 ) -> Result<Option<Board>, SourceError> {
4315 if self.board_cache()?.is_some() {
4316 return Ok(None);
4317 }
4318 let Some(existing) = self.item_by_id(target).await? else {
4319 return Ok(None);
4320 };
4321 let Some(board_id) = existing.board_id.clone() else {
4322 return Ok(None);
4323 };
4324 let defines = |name: &str| {
4325 existing
4326 .fields
4327 .iter()
4328 .any(|field| field.get("name").and_then(Value::as_str) == Some(name))
4329 };
4330 if !defines(ORIGIN_FIELD) || (incoming.written.status().is_some() && !defines("Status")) {
4331 return Ok(None);
4332 }
4333 let fields = json!({"nodes": existing.fields, "pageInfo": {"hasNextPage": false}});
4334 let mut items = vec![existing];
4335 for edge in depends_on {
4336 if !edge
4337 .to
4338 .source()
4339 .is_none_or(|source| source == self.name.as_str())
4340 {
4341 continue;
4342 }
4343 // The first colon, for the reason `partition_edges` gives.
4344 let far = NativeId(if edge.to.is_qualified() {
4345 edge.to
4346 .id()
4347 .split_once(':')
4348 .map_or(edge.to.id(), |(_, native)| native)
4349 .to_owned()
4350 } else {
4351 edge.to.id().to_owned()
4352 });
4353 if items.iter().any(|item| item.id == far) {
4354 continue;
4355 }
4356 match self.item_by_id(&far).await? {
4357 Some(item) => items.push(item),
4358 // Refused against the whole board, in the words that refusal has always had.
4359 None => return Ok(None),
4360 }
4361 }
4362 Ok(Some(Board {
4363 id: board_id,
4364 fields,
4365 items,
4366 }))
4367 }
4368
4369 /// Everything a write does after the item exists: its board fields, its parent, and
4370 /// its dependencies.
4371 ///
4372 /// Split out of `write_item` so there is one place a failure past the point of no
4373 /// return is caught, rather than a tidy-up repeated at each `?` above.
4374 // llmlint: ignore[suppressions_justified] This is the tail of `write_item` lifted out
4375 // so there is one place a failure past the point of no return is caught, and its
4376 // arguments are exactly the values that tail already had in scope. Bundling them into a
4377 // struct would describe no concept — it would be "the arguments of this function" — and
4378 // would put the whole of `write_item`'s locals behind one more indirection.
4379 #[allow(clippy::too_many_arguments)]
4380 async fn finish_write(
4381 &self,
4382 board: &Board,
4383 incoming: &Incoming<'_>,
4384 content_id: &NativeId,
4385 item_id: &str,
4386 content_kind: ContentKind,
4387 existing: Option<&Resolved>,
4388 origin_field: Option<&str>,
4389 origin: &str,
4390 column: Option<(String, String)>,
4391 status_target: Option<&StatusTarget>,
4392 native: &[String],
4393 ) -> Result<(), SourceError> {
4394 if let Some(field_id) = origin_field {
4395 self.set_item_field(&board.id, item_id, field_id, json!({"text":origin}))
4396 .await?;
4397 }
4398
4399 if let Some((field_id, option_id)) = column {
4400 self.set_item_field(
4401 &board.id,
4402 item_id,
4403 &field_id,
4404 json!({"singleSelectOptionId":option_id}),
4405 )
4406 .await?;
4407 }
4408
4409 if content_kind == ContentKind::Issue
4410 && matches!(status_target, Some(StatusTarget::Terminal(_, _)))
4411 {
4412 self.update_content(
4413 ContentKind::Issue,
4414 content_id,
4415 json!({"stateInput":state_input(status_target)}),
4416 )
4417 .await?;
4418 }
4419
4420 if content_kind == ContentKind::Issue {
4421 self.reparent(
4422 existing.and_then(|item| item.parent.clone()),
4423 content_id,
4424 incoming.parent,
4425 )
4426 .await?;
4427 // A document takes part in no dependency graph, so writing one neither reads
4428 // nor changes the issue's own `blockedBy` relationships. Reconciling them
4429 // against the empty list a document write carries would *delete* whatever
4430 // relationships a person had made on that issue, which is a write nobody
4431 // asked for.
4432 if incoming.written.kind() != BoardKind::Document {
4433 self.reconcile_blocked_by(content_id, native).await?;
4434 }
4435 }
4436 Ok(())
4437 }
4438
4439 /// Delete one issue, which takes its board item with it.
4440 async fn delete_issue(&self, id: &NativeId) -> Result<(), SourceError> {
4441 let data = self
4442 .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
4443 .await?;
4444 data.pointer("/deleteIssue/repository")
4445 .filter(|value| !value.is_null())
4446 .ok_or_else(|| SourceError::Malformed {
4447 message: "GitHub issue deletion returned no repository".into(),
4448 })?;
4449 self.forget(id)?;
4450 Ok(())
4451 }
4452
4453 /// Remove one item this copy created, so a copy that could not finish leaves the board
4454 /// as it found it.
4455 ///
4456 /// Deleting the issue takes its board item with it, so there is no second mutation to
4457 /// keep in step. An id the board does not hold is not an error: the item is already
4458 /// gone, which is the state this asks for.
4459 async fn delete_item(&self, id: &NativeId) -> Result<(), SourceError> {
4460 let board = self.board().await?;
4461 let Some(item) = board.items.iter().find(|item| item.id == *id) else {
4462 return Ok(());
4463 };
4464 if item.content_kind == ContentKind::DraftIssue {
4465 return Err(SourceError::Refused {
4466 message: format!(
4467 "GitHub item {} is a draft, and this source removes an item by deleting \
4468 its issue; next: remove it from the board by hand",
4469 id.0
4470 ),
4471 });
4472 }
4473 let data = self
4474 .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
4475 .await?;
4476 data.pointer("/deleteIssue/repository")
4477 .filter(|value| !value.is_null())
4478 .ok_or_else(|| SourceError::Malformed {
4479 message: "GitHub issue deletion returned no repository".into(),
4480 })?;
4481 self.forget(id)?;
4482 Ok(())
4483 }
4484
4485 /// The issue a comment call on `task` is about, or `None` when this board holds no such
4486 /// task.
4487 ///
4488 /// Resolved exactly as [`TaskSource::get_task`] resolves it, so the comment verbs and a
4489 /// read of the task cannot disagree about which ids name one: a project or a document of
4490 /// this board is not a task here either.
4491 ///
4492 /// A **draft** is a task with nowhere to keep a comment, because GitHub keeps comments on
4493 /// issues and a draft is not one. It is refused rather than answered with an empty page,
4494 /// which would read as a task nobody has commented on yet.
4495 async fn commented_issue(&self, task: &NativeId) -> Result<Option<NativeId>, SourceError> {
4496 let Some(item) = self
4497 .item_by_id(task)
4498 .await?
4499 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
4500 else {
4501 return Ok(None);
4502 };
4503 if item.content_kind == ContentKind::DraftIssue {
4504 return Err(SourceError::Refused {
4505 message: format!(
4506 "task {} of source {} is a draft item on the board, and GitHub keeps \
4507 comments on issues alone, so a draft has none to read or write; next: \
4508 convert the draft to an issue on the board, then comment on the issue it \
4509 becomes",
4510 task.0, self.name
4511 ),
4512 });
4513 }
4514 Ok(Some(item.id))
4515 }
4516
4517 /// Whether the comment `comment` is one of `issue`'s own.
4518 ///
4519 /// Read before an edit or a removal is sent, because GitHub's comment mutations take the
4520 /// comment's id and nothing else: a comment id given against the wrong task would
4521 /// otherwise change a comment on some other issue entirely. An id that names nothing, or
4522 /// names something that is not an issue comment, is a comment this task does not have —
4523 /// which is what GitHub refusing to resolve it means too.
4524 async fn comment_is_on(
4525 &self,
4526 issue: &NativeId,
4527 comment: &NativeId,
4528 ) -> Result<bool, SourceError> {
4529 let asked = self
4530 .graphql(graphql::COMMENT_ISSUE, json!({"id":comment.0}))
4531 .await;
4532 let data = match asked {
4533 Ok(data) => data,
4534 Err(error) if unresolvable_node(&error) => return Ok(false),
4535 Err(error) => return Err(error),
4536 };
4537 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
4538 return Ok(false);
4539 };
4540 if optional_str(node, "__typename")? != Some("IssueComment") {
4541 return Ok(false);
4542 }
4543 let on = node.get("issue").ok_or_else(|| SourceError::Malformed {
4544 message: format!("GitHub issue comment {} names no issue", comment.0),
4545 })?;
4546 Ok(required_str(on, "id")? == issue.0)
4547 }
4548
4549 /// Which far ends this item's own `blockedBy` relationship holds, and which it cannot.
4550 async fn partition_edges(
4551 &self,
4552 board: &Board,
4553 near_kind: BoardKind,
4554 near_content: ContentKind,
4555 depends_on: &[DependencyEdge],
4556 ) -> Result<(Vec<String>, Vec<DependencyEdge>), SourceError> {
4557 let mut native = Vec::new();
4558 let mut fallback = Vec::new();
4559 for edge in depends_on {
4560 let same_source = edge
4561 .to
4562 .source()
4563 .is_none_or(|source| source == self.name.as_str());
4564 // A qualified id's source segment runs to its *first* colon — `GlobalId` and
4565 // `DependencyEndpoint::source` both read it that way — and a native id may hold
4566 // colons of its own, so the far end is everything after that one separator.
4567 // Splitting at the last would truncate `work:urn:task:7` to `7`.
4568 let far_id = if edge.to.is_qualified() {
4569 edge.to
4570 .id()
4571 .split_once(':')
4572 .map_or(edge.to.id(), |(_, native)| native)
4573 } else {
4574 edge.to.id()
4575 };
4576 let far = if same_source {
4577 Some(
4578 board
4579 .items
4580 .iter()
4581 .find(|item| item.id.0 == far_id)
4582 .ok_or_else(|| SourceError::Refused {
4583 message: format!("GitHub dependency item {far_id} was not found"),
4584 })?,
4585 )
4586 } else {
4587 None
4588 };
4589 // The caller says which kind the far end is, and this board holds the far end
4590 // itself, so a disagreement is settled here rather than stored: recorded, the
4591 // wrong kind would read back as a cross-level edge that never existed; written
4592 // natively, it would name a relationship of a different level than the caller
4593 // asked for.
4594 //
4595 // A far end this board holds as a *document* fails the same comparison and is
4596 // refused by the same sentence: `ItemKind` has no document variant because
4597 // nothing may point at one, so no caller can name it correctly and the refusal
4598 // is the only honest answer.
4599 if let Some(disagreeing) = far.filter(|far| far.kind != BoardKind::Work(edge.to.kind)) {
4600 return Err(SourceError::Refused {
4601 message: format!(
4602 "GitHub dependency item {far_id} is a {} of this board, and this item \
4603 names it as a {}; record the kind it is",
4604 disagreeing.kind.describes(),
4605 edge.to.kind.marker()
4606 ),
4607 });
4608 }
4609 // A draft has neither `blockedBy` nor `blocking`, so no edge of one is native
4610 // however the far end is spelled — and one classified native here would be
4611 // written nowhere at all, because a draft's native reconciliation never runs.
4612 let native_here = near_content == ContentKind::Issue
4613 && far.is_some_and(|far| {
4614 far.content_kind == ContentKind::Issue
4615 && BoardKind::Work(edge.to.kind) == near_kind
4616 });
4617 if native_here {
4618 native.push(far_id.to_owned());
4619 } else {
4620 fallback.push(edge.clone());
4621 }
4622 }
4623 Ok((native, fallback))
4624 }
4625
4626 async fn update_existing(
4627 &self,
4628 item: &Resolved,
4629 incoming: &Incoming<'_>,
4630 body: &Option<String>,
4631 status_target: Option<&StatusTarget>,
4632 ) -> Result<(), SourceError> {
4633 let title = incoming.written_title();
4634 let mut fields = match item.content_kind {
4635 ContentKind::DraftIssue => json!({"title":title,"body":body}),
4636 ContentKind::Issue => json!({"title":title,"body":body,
4637 "stateInput":state_input(status_target)}),
4638 };
4639 if matches!(status_target, Some(StatusTarget::Terminal(_, _))) {
4640 fields
4641 .as_object_mut()
4642 .expect("update fields are an object")
4643 .remove("stateInput");
4644 }
4645 self.update_content(item.content_kind, &item.id, fields)
4646 .await
4647 }
4648
4649 /// Update one board item's content with exactly `fields` beside its id, through the
4650 /// mutation its kind takes: `updateIssue` for an issue, `updateProjectV2DraftIssue` for
4651 /// a draft.
4652 ///
4653 /// Every input field either mutation leaves out is a field GitHub leaves as it is, which
4654 /// is what lets a narrow write carry the one thing it changes and nothing else.
4655 async fn update_content(
4656 &self,
4657 kind: ContentKind,
4658 id: &NativeId,
4659 fields: Value,
4660 ) -> Result<(), SourceError> {
4661 let (operation, id_key, pointer) = match kind {
4662 ContentKind::DraftIssue => (
4663 graphql::UPDATE_DRAFT,
4664 "draftIssueId",
4665 "/updateProjectV2DraftIssue/draftIssue",
4666 ),
4667 ContentKind::Issue => (graphql::UPDATE_ISSUE, "id", "/updateIssue/issue"),
4668 };
4669 let mut input = fields;
4670 input[id_key] = json!(id.0);
4671 let data = self.graphql(operation, json!({"input":input})).await?;
4672 let returned = data
4673 .pointer(pointer)
4674 .ok_or_else(|| SourceError::Malformed {
4675 message: "GitHub item update returned no item".into(),
4676 })?;
4677 if required_str(returned, "id")? != id.0 {
4678 return Err(SourceError::Malformed {
4679 message: "GitHub item update returned the wrong item".into(),
4680 });
4681 }
4682 Ok(())
4683 }
4684
4685 /// Creates one issue, files it on the board, and reports what a read of it would say:
4686 /// its content id, its board item id, and the web address GitHub gave it.
4687 ///
4688 /// Two calls rather than one: `createIssue` needs a repository and answers with an
4689 /// issue that is on no board, and `addProjectV2ItemById` is what puts it there. A
4690 /// terminal status is not written here: `finish_write` selects its option first and
4691 /// closes the issue after, so a close never lands on an item whose board cannot show it.
4692 ///
4693 /// The address comes back here because this is the only place it is known before
4694 /// GitHub's own board read catches up — an item this run created answers the reads
4695 /// that follow it out of the record below, and one remembered without its address
4696 /// would report no location for the rest of the run.
4697 async fn create_and_file_issue(
4698 &self,
4699 board: &Board,
4700 repository: &RepositoryTarget,
4701 incoming: &Incoming<'_>,
4702 body: &Option<String>,
4703 ) -> Result<(NativeId, String, Option<String>), SourceError> {
4704 let repository_id = self.repository_id(repository, incoming).await?;
4705 let data = self
4706 .graphql(
4707 graphql::CREATE_ISSUE,
4708 json!({"input":{
4709 "repositoryId":repository_id,"title":incoming.written_title(),"body":body
4710 }}),
4711 )
4712 .await?;
4713 let created = data
4714 .pointer("/createIssue/issue")
4715 .filter(|value| !value.is_null())
4716 .ok_or_else(|| SourceError::Malformed {
4717 message: "GitHub issue creation returned no issue".into(),
4718 })?;
4719 let content_id = NativeId(required_str(created, "id")?.to_owned());
4720 // Optional although GitHub's schema makes it non-null: the issue exists by now, so
4721 // a response without it is not worth failing a landed write over — the item simply
4722 // reports no location until the board read catches up, which is what it did before.
4723 let url = optional_str(created, "url")?.map(str::to_owned);
4724 // The issue exists from here on, so a failure filing it on the board takes it
4725 // back: an issue in the repository that is on no board is an item nobody asked for
4726 // and nothing here would find again.
4727 let added = match self
4728 .graphql(
4729 graphql::ADD_TO_BOARD,
4730 json!({"input":{"projectId":board.id,"contentId":content_id.0}}),
4731 )
4732 .await
4733 {
4734 Ok(added) => added,
4735 Err(error) => {
4736 let _ = self.delete_issue(&content_id).await;
4737 return Err(error);
4738 }
4739 };
4740 let item = added
4741 .pointer("/addProjectV2ItemById/item")
4742 .filter(|value| !value.is_null())
4743 .ok_or_else(|| SourceError::Malformed {
4744 message: "GitHub board addition returned no project item".into(),
4745 })?;
4746 Ok((content_id, required_str(item, "id")?.to_owned(), url))
4747 }
4748
4749 /// Move one issue under the project it now belongs to, or out of the one it left.
4750 async fn reparent(
4751 &self,
4752 held: Option<NativeId>,
4753 child: &NativeId,
4754 wanted: Option<&NativeId>,
4755 ) -> Result<(), SourceError> {
4756 if held.as_ref() == wanted {
4757 return Ok(());
4758 }
4759 if let Some(held) = &held {
4760 self.sub_issue(graphql::REMOVE_SUB_ISSUE, held, child, "removeSubIssue")
4761 .await?;
4762 }
4763 if let Some(wanted) = wanted {
4764 self.sub_issue(graphql::ADD_SUB_ISSUE, wanted, child, "addSubIssue")
4765 .await?;
4766 }
4767 Ok(())
4768 }
4769
4770 async fn sub_issue(
4771 &self,
4772 operation: &str,
4773 parent: &NativeId,
4774 child: &NativeId,
4775 root: &str,
4776 ) -> Result<(), SourceError> {
4777 let data = self
4778 .graphql(
4779 operation,
4780 json!({"input":{"issueId":parent.0,"subIssueId":child.0}}),
4781 )
4782 .await?;
4783 let issue =
4784 data.pointer(&format!("/{root}/issue"))
4785 .ok_or_else(|| SourceError::Malformed {
4786 message: "GitHub sub-issue update returned no issue".into(),
4787 })?;
4788 let sub =
4789 data.pointer(&format!("/{root}/subIssue"))
4790 .ok_or_else(|| SourceError::Malformed {
4791 message: "GitHub sub-issue update returned no sub-issue".into(),
4792 })?;
4793 if required_str(issue, "id")? != parent.0 || required_str(sub, "id")? != child.0 {
4794 return Err(SourceError::Malformed {
4795 message: "GitHub sub-issue update returned the wrong issues".into(),
4796 });
4797 }
4798 Ok(())
4799 }
4800
4801 async fn reconcile_blocked_by(
4802 &self,
4803 content_id: &NativeId,
4804 native: &[String],
4805 ) -> Result<(), SourceError> {
4806 let current = self.native_dependency_ids(content_id).await?;
4807 for (operation, far_id) in current
4808 .iter()
4809 .filter(|id| !native.contains(id))
4810 .map(|id| (graphql::REMOVE_BLOCKED_BY, id))
4811 .chain(
4812 native
4813 .iter()
4814 .filter(|id| !current.contains(id))
4815 .map(|id| (graphql::ADD_BLOCKED_BY, id)),
4816 )
4817 {
4818 let data = self
4819 .graphql(
4820 operation,
4821 json!({"input":{"issueId":content_id.0,"blockingIssueId":far_id}}),
4822 )
4823 .await?;
4824 let root = if operation == graphql::ADD_BLOCKED_BY {
4825 "addBlockedBy"
4826 } else {
4827 "removeBlockedBy"
4828 };
4829 let issue =
4830 data.pointer(&format!("/{root}/issue"))
4831 .ok_or_else(|| SourceError::Malformed {
4832 message: "GitHub dependency update returned no issue".into(),
4833 })?;
4834 let blocker = data
4835 .pointer(&format!("/{root}/blockingIssue"))
4836 .ok_or_else(|| SourceError::Malformed {
4837 message: "GitHub dependency update returned no blocking issue".into(),
4838 })?;
4839 if required_str(issue, "id")? != content_id.0 || required_str(blocker, "id")? != far_id
4840 {
4841 return Err(SourceError::Malformed {
4842 message: "GitHub dependency update returned the wrong issues".into(),
4843 });
4844 }
4845 }
4846 Ok(())
4847 }
4848}
4849
4850/// What resolving one node id reached; see [`GitHubProjectsSource::reach`].
4851enum Reached {
4852 /// An issue this board holds, resolved into everything this source reports about it.
4853 Held(Box<Resolved>),
4854 /// Nothing this board holds: no such node, or a node on some other board.
4855 Nothing,
4856 /// A board draft, which exists only inside the board's own item connection.
4857 Draft,
4858}
4859
4860/// What GitHub says when a string is not a node id it can resolve.
4861///
4862/// Matched because it is the ordinary answer to a project selector naming a project by its
4863/// *name*, and reporting that as a failure would make naming one impossible. It is read
4864/// off the refusal GitHub sent, never guessed from the shape of the string: this source
4865/// does not define the syntax of a GitHub node id and would be wrong about it.
4866const UNRESOLVABLE_NODE: &str = "could not resolve to a node";
4867
4868/// Whether this refusal is GitHub saying the id names no node at all.
4869fn unresolvable_node(error: &SourceError) -> bool {
4870 matches!(error, SourceError::Refused { message }
4871 if message.to_ascii_lowercase().contains(UNRESOLVABLE_NODE))
4872}
4873
4874/// One project name, as a search qualifier which filters on it at the server.
4875///
4876/// Quoted so the whole title is one phrase rather than a bag of words, with the two
4877/// characters GitHub's own quoting grammar gives a meaning inside a quoted phrase escaped
4878/// the way it documents. A title matched here is still compared for equality afterwards:
4879/// the qualifier narrows what the server sends, and this source decides what it names.
4880fn title_qualifier(name: &str) -> String {
4881 let escaped = name.replace('\\', "\\\\").replace('"', "\\\"");
4882 format!("in:title \"{escaped}\"")
4883}
4884
4885/// The board, and every item on it this source reports.
4886#[derive(Clone)]
4887struct Board {
4888 id: String,
4889 fields: Value,
4890 items: Vec<Resolved>,
4891}
4892
4893impl Board {
4894 fn field<'a>(fields: &'a Value, name: &str) -> Result<Option<&'a Value>, SourceError> {
4895 complete_connection(fields, "project fields", NESTED_PAGE_SIZE)?;
4896 let nodes = fields
4897 .get("nodes")
4898 .and_then(Value::as_array)
4899 .ok_or_else(|| SourceError::Malformed {
4900 message: "GitHub project fields.nodes is not an array".into(),
4901 })?;
4902 Ok(nodes
4903 .iter()
4904 .find(|field| field.get("name").and_then(Value::as_str) == Some(name)))
4905 }
4906}
4907
4908/// One board item, resolved into everything this source reports about it.
4909#[derive(Clone)]
4910struct Resolved {
4911 item_id: String,
4912 id: NativeId,
4913 content_kind: ContentKind,
4914 kind: BoardKind,
4915 title: String,
4916 body: Option<String>,
4917 /// The body exactly as GitHub holds it, metadata slot and all, which is what a write
4918 /// that changes the slot alone has to keep byte for byte outside it.
4919 raw_body: Option<String>,
4920 status: Status,
4921 /// The name of the board `Status` option this item sits in, as the board spells it.
4922 option: Option<String>,
4923 /// Whether this item's issue is closed. A draft has no such state and is never closed.
4924 closed: bool,
4925 /// The tasks this one delivers, read out of its slot. Empty for anything not a task.
4926 delivers: Vec<TaskRef>,
4927 /// Every task that delivers this one, read out of its slot. Empty for anything not a
4928 /// task.
4929 delivered_by: Vec<TaskRef>,
4930 labels: Vec<Label>,
4931 parent: Option<NativeId>,
4932 // llmlint: ignore[invalid_states_unrepresentable] The write side's reason, read back: this is the engine's qualified id, taken out of a board text field and handed on untouched. A newtype here would have this plugin define the syntax of an id `docs/metadata.md` says no plugin ever constructs or interprets.
4933 origin: Option<String>,
4934 url: Option<String>,
4935 created_at: Option<DateTime<Utc>>,
4936 updated_at: Option<DateTime<Utc>>,
4937 own_repository: Option<Repository>,
4938 repositories: Vec<Repository>,
4939 slot: BTreeMap<String, Value>,
4940 /// The node id of the board this item sits on, when the read that reached it said.
4941 board_id: Option<String>,
4942 /// The definition of every board field this item holds a value of, in the shape a read
4943 /// of the board's own `fields` gives one.
4944 ///
4945 /// Only the fields this item has a value in: a field it holds nothing of is not here,
4946 /// which says nothing about whether the board has it.
4947 fields: Vec<Value>,
4948}
4949
4950impl Resolved {
4951 /// The metadata a caller sees: their own keys, plus the copy origin this source keeps
4952 /// in a field of its own, and none of the five keys that are only an encoding.
4953 ///
4954 /// The two delivery keys are left out for every kind, not only for a task: they are
4955 /// the encoding of [`Task::delivers`] and [`Task::delivered_by`], and a project or a
4956 /// document carrying one holds nothing a caller's own metadata could mean by it.
4957 fn metadata(&self) -> BTreeMap<String, Value> {
4958 let mut metadata = self.slot.clone();
4959 metadata.remove(Repository::METADATA_KEY);
4960 metadata.remove(DependencyEdge::RECORDED_KEY);
4961 metadata.remove(ItemKind::METADATA_KEY);
4962 metadata.remove(TaskRef::DELIVERS_KEY);
4963 metadata.remove(TaskRef::DELIVERED_BY_KEY);
4964 if let Some(origin) = &self.origin {
4965 metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin.clone()));
4966 }
4967 metadata
4968 }
4969
4970 /// Where this item is, as a link a reader can open.
4971 ///
4972 /// A board is a hosted place and every issue on it has a web address, so that address
4973 /// is what "where is this?" means here — and [`Location::Url`] is what says which kind
4974 /// of place it is, so a reader knows to open it rather than to read a file out. It
4975 /// does not replace or derive from `url`: the field goes on reporting exactly what it
4976 /// reported before, and this says what that address *is*.
4977 ///
4978 /// An item GitHub gave no `url` for — a draft has none — reports no location at all
4979 /// rather than a third variant, which is the contract's "the source did not say". An
4980 /// issue this run created is not one of those: its address comes back from the
4981 /// creating mutation, so it is somewhere a reader can open from the moment it exists
4982 /// rather than from whenever the board read catches up.
4983 fn location(&self) -> Option<Location> {
4984 self.url.clone().map(Location::Url)
4985 }
4986
4987 fn task(&self) -> Task {
4988 Task {
4989 id: self.id.clone(),
4990 title: self.title.clone(),
4991 content: self.body.clone(),
4992 status: self.status.clone(),
4993 labels: self.labels.clone(),
4994 project: self.parent.clone(),
4995 url: self.url.clone(),
4996 location: self.location(),
4997 created_at: self.created_at,
4998 updated_at: self.updated_at,
4999 metadata: self.metadata(),
5000 repositories: self.repositories.clone(),
5001 delivers: self.delivers.clone(),
5002 delivered_by: self.delivered_by.clone(),
5003 }
5004 }
5005
5006 fn project(&self) -> Project {
5007 Project {
5008 id: self.id.clone(),
5009 title: self.title.clone(),
5010 content: self.body.clone(),
5011 status: self.status.clone(),
5012 labels: self.labels.clone(),
5013 url: self.url.clone(),
5014 location: self.location(),
5015 created_at: self.created_at,
5016 updated_at: self.updated_at,
5017 metadata: self.metadata(),
5018 repositories: self.repositories.clone(),
5019 }
5020 }
5021
5022 /// The same issue as a document: the project it is filed under, and no status and no
5023 /// dependencies, because a document is not work.
5024 fn document(&self) -> Document {
5025 Document {
5026 id: self.id.clone(),
5027 title: self.title.clone(),
5028 content: self.body.clone(),
5029 project: self.parent.clone(),
5030 labels: self.labels.clone(),
5031 url: self.url.clone(),
5032 location: self.location(),
5033 created_at: self.created_at,
5034 updated_at: self.updated_at,
5035 metadata: self.metadata(),
5036 repositories: self.repositories.clone(),
5037 }
5038 }
5039}
5040
5041/// What one write is, and the status that comes with being it.
5042///
5043/// One value rather than a [`BoardKind`] beside an `Option<Status>`: a document has no
5044/// status and a task or a project always has one, so "a document carrying a status" and
5045/// "a task carrying none" are states a write cannot be in rather than states every use
5046/// site below has to defend against.
5047enum Written<'a> {
5048 /// A document, which is not work and so has no status at all.
5049 Document,
5050 /// A task or a project, and the status it is being written with.
5051 Work(ItemKind, &'a Status),
5052}
5053
5054impl Written<'_> {
5055 /// Which of the board's three kinds this write is.
5056 const fn kind(&self) -> BoardKind {
5057 match self {
5058 Self::Document => BoardKind::Document,
5059 Self::Work(kind, _) => BoardKind::Work(*kind),
5060 }
5061 }
5062
5063 /// The status this write carries. A document carries none, so a write of one says
5064 /// nothing about the issue's open or closed state and selects no board `Status`
5065 /// option.
5066 const fn status(&self) -> Option<&Status> {
5067 match self {
5068 Self::Document => None,
5069 Self::Work(_, status) => Some(status),
5070 }
5071 }
5072}
5073
5074/// The item being written, in the one shape all three write methods reach.
5075struct Incoming<'a> {
5076 written: Written<'a>,
5077 /// The title a person wrote. A document's goes onto the issue with
5078 /// [`DESIGN_TITLE_PREFIX`] put back, so a round trip returns the title that went in.
5079 title: &'a str,
5080 content: Option<&'a str>,
5081 labels: &'a [Label],
5082 metadata: &'a BTreeMap<String, Value>,
5083 repositories: &'a [Repository],
5084 parent: Option<&'a NativeId>,
5085 /// [`Task::delivers`], already checked. Empty for a project or a document, which is
5086 /// what keeps either key out of their slot.
5087 delivers: &'a [TaskRef],
5088 /// [`Task::delivered_by`], already checked. Empty for a project or a document.
5089 delivered_by: &'a [TaskRef],
5090}
5091
5092impl Incoming<'_> {
5093 /// The title this write puts on the issue.
5094 fn written_title(&self) -> String {
5095 match self.written {
5096 Written::Document => format!("{DESIGN_TITLE_PREFIX}{}", self.title),
5097 Written::Work(..) => self.title.to_owned(),
5098 }
5099 }
5100}
5101
5102#[derive(Clone, Copy, PartialEq, Eq)]
5103enum ContentKind {
5104 DraftIssue,
5105 Issue,
5106}
5107
5108/// What one board issue is: a document, or the work an [`ItemKind`] names.
5109///
5110/// A type of this source's own rather than an `ItemKind` with a third variant, because
5111/// `ItemKind` names what a dependency endpoint points at and nothing may point at a
5112/// document — the contract keeps a document out of that enum deliberately. Holding the
5113/// board's three answers in one value is what makes every place that asks "which is this?"
5114/// answer all three, rather than a `document: bool` beside a `kind` that means nothing for
5115/// two thirds of the board.
5116#[derive(Clone, Copy, PartialEq, Eq)]
5117enum BoardKind {
5118 /// An issue whose title begins [`DESIGN_TITLE_PREFIX`].
5119 Document,
5120 /// Every other issue, and every draft.
5121 Work(ItemKind),
5122}
5123
5124impl BoardKind {
5125 /// How a refusal names this kind to the person reading it.
5126 const fn describes(self) -> &'static str {
5127 match self {
5128 Self::Document => "document",
5129 Self::Work(kind) => kind.marker(),
5130 }
5131 }
5132}
5133
5134/// Whether `labels` satisfies `filter`, matching by name, case-insensitively.
5135///
5136/// This is the local Markdown source's `labels_match`, spelled the same way on purpose:
5137/// the shared cross-source journeys assert one answer to one question, so two sources
5138/// that disagree about what "carries the label bug" means fail them.
5139fn labels_match(labels: &[Label], filter: &LabelFilter) -> bool {
5140 let holds = |name: &String| {
5141 labels
5142 .iter()
5143 .any(|label| label.name.eq_ignore_ascii_case(name))
5144 };
5145 (filter.any_of.is_empty() || filter.any_of.iter().any(holds))
5146 && filter.all_of.iter().all(holds)
5147 && !filter.none_of.iter().any(holds)
5148}
5149
5150/// Whether `category` is one of `statuses`. An empty list is unfiltered rather than
5151/// "keeps nothing", which is what lets a `Vec<StatusCategory>` spell no filter at all.
5152fn status_matches(category: StatusCategory, statuses: &[StatusCategory]) -> bool {
5153 statuses.is_empty() || statuses.contains(&category)
5154}
5155
5156/// Whether `title`/`content` satisfies `query`, matching case-insensitively.
5157///
5158/// `content` is the item's own prose — the body with this source's trailing metadata
5159/// comment already taken off — so a search never matches an encoding the author of the
5160/// issue never wrote.
5161fn text_matches(title: &str, content: Option<&str>, query: &TextQuery) -> bool {
5162 let terms = query.terms.to_lowercase();
5163 let in_title = title.to_lowercase().contains(&terms);
5164 let in_content = content.is_some_and(|body| body.to_lowercase().contains(&terms));
5165 match query.fields {
5166 TextFields::Title => in_title,
5167 TextFields::Content => in_content,
5168 TextFields::TitleOrContent => in_title || in_content,
5169 }
5170}
5171
5172/// Whether `task` satisfies `query`, with `project` deciding the project predicate.
5173///
5174/// The project predicate is passed separately because a read narrowed to one project has
5175/// already answered it by asking *that project* for its own items — and re-applying it
5176/// there would compare the caller's selector, which may be a project's **name**, against
5177/// the id of the project that name resolved to, and keep nothing. Every other read passes
5178/// `query.project` and applies it here, which is what keeps `projects` a predicate this
5179/// source really does apply.
5180fn task_matches(task: &Task, query: &TaskQuery, project: &ProjectFilter) -> bool {
5181 labels_match(&task.labels, &query.labels)
5182 && status_matches(task.status.category, &query.statuses)
5183 && match project {
5184 ProjectFilter::Any => true,
5185 ProjectFilter::Orphans => task.project.is_none(),
5186 ProjectFilter::Is(id) => task.project.as_ref() == Some(id),
5187 }
5188 && query
5189 .text
5190 .as_ref()
5191 .is_none_or(|text| text_matches(&task.title, task.content.as_deref(), text))
5192}
5193
5194fn project_matches(project: &Project, query: &ProjectQuery) -> bool {
5195 labels_match(&project.labels, &query.labels)
5196 && status_matches(project.status.category, &query.statuses)
5197 && query
5198 .text
5199 .as_ref()
5200 .is_none_or(|text| text_matches(&project.title, project.content.as_deref(), text))
5201}
5202
5203/// The same three predicates a task query carries, minus the status filter.
5204///
5205/// A document is not work, so it has no status for one to compare against and the query
5206/// type carries none. The project predicate is the same one — a design issue filed under a
5207/// project issue is in that project, and one filed under nothing is in none — so it is
5208/// spelled the same way here rather than answered differently.
5209fn document_matches(document: &Document, query: &DocumentQuery, project: &ProjectFilter) -> bool {
5210 labels_match(&document.labels, &query.labels)
5211 && match project {
5212 ProjectFilter::Any => true,
5213 ProjectFilter::Orphans => document.project.is_none(),
5214 ProjectFilter::Is(id) => document.project.as_ref() == Some(id),
5215 }
5216 && query
5217 .text
5218 .as_ref()
5219 .is_none_or(|text| text_matches(&document.title, document.content.as_deref(), text))
5220}
5221
5222#[async_trait::async_trait]
5223impl TaskSource for GitHubProjectsSource {
5224 fn kind(&self) -> &'static str {
5225 KIND
5226 }
5227 fn capabilities(&self) -> Capabilities {
5228 Capabilities {
5229 projects: Support::Native,
5230 documents: Support::Native,
5231 comments: Support::Native,
5232 orphan_tasks: Support::Native,
5233 filter_by_label: Support::Native,
5234 filter_by_status: Support::Native,
5235 search_title: Support::Native,
5236 search_content: Support::Native,
5237 task_dependencies: DependencySupport::BothDirections,
5238 project_dependencies: DependencySupport::BothDirections,
5239 max_page_size: MAX_PAGE_SIZE,
5240 }
5241 }
5242 async fn health(&self) -> Result<Health, SourceError> {
5243 let board = self.board_page(None, 1).await?;
5244 Ok(Health {
5245 reachable: true,
5246 detail: Some(format!(
5247 "reading GitHub project {}/{} ({})",
5248 self.owner,
5249 self.project_number,
5250 required_str(&board, "title")?
5251 )),
5252 })
5253 }
5254 async fn get_task(&self, id: &NativeId) -> Result<Option<Task>, SourceError> {
5255 Ok(self
5256 .item_by_id(id)
5257 .await?
5258 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
5259 .map(|item| item.task()))
5260 }
5261 async fn get_project(&self, id: &NativeId) -> Result<Option<Project>, SourceError> {
5262 Ok(self
5263 .item_by_id(id)
5264 .await?
5265 .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
5266 .map(|item| item.project()))
5267 }
5268 async fn query_tasks(
5269 &self,
5270 query: &TaskQuery,
5271 page: &PageRequest,
5272 ) -> Result<Page<Task>, SourceError> {
5273 validate_page(page)?;
5274 // A read narrowed to one project asks that project for its own tasks, so nothing
5275 // about it costs what the rest of the board holds. Every other task read is a
5276 // question about the whole board and is answered by reading it.
5277 let (held, membership) = match &query.project {
5278 ProjectFilter::Is(project) => (
5279 self.project_children(project).await?,
5280 // Answered by where these items came from; see `task_matches`.
5281 &ProjectFilter::Any,
5282 ),
5283 ProjectFilter::Any | ProjectFilter::Orphans => {
5284 (self.board().await?.items, &query.project)
5285 }
5286 };
5287 // Filtered before paged: a page of a filtered result is a page of the survivors,
5288 // never the survivors of a page.
5289 let tasks = held
5290 .iter()
5291 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
5292 .map(Resolved::task)
5293 .filter(|task| task_matches(task, query, membership))
5294 .collect();
5295 Ok(offset_page(
5296 tasks,
5297 numeric_cursor(page.cursor.as_ref())?,
5298 page.limit.min(MAX_PAGE_SIZE) as usize,
5299 ))
5300 }
5301 async fn query_projects(
5302 &self,
5303 query: &ProjectQuery,
5304 page: &PageRequest,
5305 ) -> Result<Page<Project>, SourceError> {
5306 validate_page(page)?;
5307 // The projects a board holds are found by an issue search scoped to that board,
5308 // never by walking the board's own item connection: what tells a project from a
5309 // task is the `parent` each issue carries, which costs nothing to read.
5310 let projects = self
5311 .board_issues()
5312 .await?
5313 .iter()
5314 .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
5315 .map(Resolved::project)
5316 .filter(|project| project_matches(project, query))
5317 .collect();
5318 Ok(offset_page(
5319 projects,
5320 numeric_cursor(page.cursor.as_ref())?,
5321 page.limit.min(MAX_PAGE_SIZE) as usize,
5322 ))
5323 }
5324 async fn get_document(&self, id: &NativeId) -> Result<Option<Document>, SourceError> {
5325 Ok(self
5326 .item_by_id(id)
5327 .await?
5328 .filter(|item| item.kind == BoardKind::Document)
5329 .map(|item| item.document()))
5330 }
5331 async fn query_documents(
5332 &self,
5333 query: &DocumentQuery,
5334 page: &PageRequest,
5335 ) -> Result<Page<Document>, SourceError> {
5336 validate_page(page)?;
5337 // Narrowed to one project, this is the same sub-issue read a task list scoped to
5338 // that project makes — a document filed under a project is a sub-issue of it too,
5339 // and which of them come back is the kind this caller asked for.
5340 let (held, membership) = match &query.project {
5341 ProjectFilter::Is(project) => (
5342 self.project_children(project).await?,
5343 // Answered by where these items came from; see `task_matches`.
5344 &ProjectFilter::Any,
5345 ),
5346 ProjectFilter::Any | ProjectFilter::Orphans => {
5347 (self.board().await?.items, &query.project)
5348 }
5349 };
5350 // Filtered before paged, exactly as a task read is: a page of a filtered result is
5351 // a page of the survivors, never the survivors of a page.
5352 let documents = held
5353 .iter()
5354 .filter(|item| item.kind == BoardKind::Document)
5355 .map(Resolved::document)
5356 .filter(|document| document_matches(document, query, membership))
5357 .collect();
5358 Ok(offset_page(
5359 documents,
5360 numeric_cursor(page.cursor.as_ref())?,
5361 page.limit.min(MAX_PAGE_SIZE) as usize,
5362 ))
5363 }
5364 async fn labels(&self, page: &PageRequest) -> Result<Page<Label>, SourceError> {
5365 validate_page(page)?;
5366 let offset = numeric_cursor(page.cursor.as_ref())?;
5367 let mut labels = self
5368 .board()
5369 .await?
5370 .items
5371 .into_iter()
5372 .flat_map(|item| item.labels)
5373 .fold(Vec::new(), |mut all, label| {
5374 if !all.iter().any(|x: &Label| x.id == label.id) {
5375 all.push(label);
5376 }
5377 all
5378 });
5379 labels.sort_by(|a, b| a.name.cmp(&b.name).then(a.id.0.cmp(&b.id.0)));
5380 Ok(offset_page(
5381 labels,
5382 offset,
5383 page.limit.min(MAX_PAGE_SIZE) as usize,
5384 ))
5385 }
5386 async fn task_dependencies(
5387 &self,
5388 id: &NativeId,
5389 direction: Direction,
5390 page: &PageRequest,
5391 ) -> Result<Page<DependencyEdge>, SourceError> {
5392 self.dependencies(id, ItemKind::Task, direction, page).await
5393 }
5394 async fn project_dependencies(
5395 &self,
5396 id: &NativeId,
5397 direction: Direction,
5398 page: &PageRequest,
5399 ) -> Result<Page<DependencyEdge>, SourceError> {
5400 self.dependencies(id, ItemKind::Project, direction, page)
5401 .await
5402 }
5403
5404 fn writes(&self) -> WriteSupport {
5405 WriteSupport::Supported
5406 }
5407
5408 /// Create or update one task.
5409 ///
5410 /// Its `delivers` and `delivered_by` are checked before anything is read or written —
5411 /// neither may name the task itself or name one task twice — and land in the body's
5412 /// metadata slot under their reserved keys, in place of any caller metadata of those
5413 /// names.
5414 async fn write_task(&self, write: &ItemWrite<Task>) -> Result<NativeId, SourceError> {
5415 let near = write.target.as_ref().unwrap_or(&write.item.id);
5416 for (key, entries) in [
5417 (TaskRef::DELIVERS_KEY, &write.item.delivers),
5418 (TaskRef::DELIVERED_BY_KEY, &write.item.delivered_by),
5419 ] {
5420 TaskRef::listed(key, near, Some(&self.name), entries.clone())
5421 .map_err(|message| SourceError::Refused { message })?;
5422 }
5423 self.write_item(
5424 &Incoming {
5425 written: Written::Work(ItemKind::Task, &write.item.status),
5426 title: &write.item.title,
5427 content: write.item.content.as_deref(),
5428 labels: &write.item.labels,
5429 metadata: &write.item.metadata,
5430 repositories: &write.item.repositories,
5431 parent: write.item.project.as_ref(),
5432 delivers: &write.item.delivers,
5433 delivered_by: &write.item.delivered_by,
5434 },
5435 write.target.as_ref(),
5436 &write.depends_on,
5437 )
5438 .await
5439 }
5440
5441 async fn write_project(&self, write: &ItemWrite<Project>) -> Result<NativeId, SourceError> {
5442 self.write_item(
5443 &Incoming {
5444 written: Written::Work(ItemKind::Project, &write.item.status),
5445 title: &write.item.title,
5446 content: write.item.content.as_deref(),
5447 labels: &write.item.labels,
5448 metadata: &write.item.metadata,
5449 repositories: &write.item.repositories,
5450 parent: None,
5451 delivers: &[],
5452 delivered_by: &[],
5453 },
5454 write.target.as_ref(),
5455 &write.depends_on,
5456 )
5457 .await
5458 }
5459
5460 /// Create or update one document, which is one issue titled the way this board spells
5461 /// a document.
5462 ///
5463 /// Everything else is exactly a task write: caller metadata goes to the same canonical
5464 /// JSON slot at the end of the body and comes back with its JSON types intact, a key
5465 /// or a field this board cannot carry is refused by name rather than dropped, a target
5466 /// naming an issue this board does not hold is refused rather than created, and an
5467 /// issue this call created is taken back when the rest of the write fails.
5468 async fn write_document(&self, write: &ItemWrite<Document>) -> Result<NativeId, SourceError> {
5469 // A document takes part in no dependency graph, so there is no far end to write
5470 // natively and none to record: a caller naming one is told so rather than having it
5471 // stored under the reserved key, where a later read would report an edge the
5472 // contract says cannot exist.
5473 if !write.depends_on.is_empty() {
5474 return Err(SourceError::Refused {
5475 message: format!(
5476 "this write names {} dependencies for a document, and a document takes \
5477 part in no dependency graph; next: put the dependency on the task or \
5478 project the document is about",
5479 write.depends_on.len()
5480 ),
5481 });
5482 }
5483 self.write_item(
5484 &Incoming {
5485 written: Written::Document,
5486 title: &write.item.title,
5487 content: write.item.content.as_deref(),
5488 labels: &write.item.labels,
5489 metadata: &write.item.metadata,
5490 repositories: &write.item.repositories,
5491 parent: write.item.project.as_ref(),
5492 delivers: &[],
5493 delivered_by: &[],
5494 },
5495 write.target.as_ref(),
5496 &[],
5497 )
5498 .await
5499 }
5500
5501 /// Set one task's status alone.
5502 ///
5503 /// An open target reopens a closed issue with an `updateIssue` carrying only its
5504 /// `stateInput`, then selects the board option with `updateProjectV2ItemFieldValue`; a
5505 /// terminal target selects its mapped option, then closes with its fixed reason. No
5506 /// request carries a title, a body or a label. The status
5507 /// answered is what [`StatusMapping::status`] reads off the state just written, which is
5508 /// what a re-read reports.
5509 async fn set_task_status(
5510 &self,
5511 id: &NativeId,
5512 category: StatusCategory,
5513 ) -> Result<Option<Status>, SourceError> {
5514 self.set_status(id, category).await
5515 }
5516
5517 /// Replace one task's `delivered_by` with a single body update that changes the
5518 /// metadata slot and nothing outside it.
5519 async fn set_delivered_by(
5520 &self,
5521 id: &NativeId,
5522 delivered_by: &[TaskRef],
5523 ) -> Result<Option<()>, SourceError> {
5524 self.replace_delivered_by(id, delivered_by).await
5525 }
5526
5527 /// Set one key of one task issue's metadata with a single body update that changes the
5528 /// metadata slot and nothing outside it — no title, label, state or board field request —
5529 /// and sends nothing when the task already holds that value under the key.
5530 async fn set_task_metadata(
5531 &self,
5532 id: &NativeId,
5533 key: &MetadataKey,
5534 value: &Value,
5535 ) -> Result<Option<Task>, SourceError> {
5536 Ok(self
5537 .set_slot_key(id, BoardKind::Work(ItemKind::Task), key, value)
5538 .await?
5539 .map(|item| item.task()))
5540 }
5541
5542 /// Set one key of one project issue's metadata, on exactly the terms of
5543 /// [`set_task_metadata`](TaskSource::set_task_metadata).
5544 async fn set_project_metadata(
5545 &self,
5546 id: &NativeId,
5547 key: &MetadataKey,
5548 value: &Value,
5549 ) -> Result<Option<Project>, SourceError> {
5550 Ok(self
5551 .set_slot_key(id, BoardKind::Work(ItemKind::Project), key, value)
5552 .await?
5553 .map(|item| item.project()))
5554 }
5555
5556 /// Set one key of one design-document issue's metadata, on exactly the terms of
5557 /// [`set_task_metadata`](TaskSource::set_task_metadata).
5558 async fn set_document_metadata(
5559 &self,
5560 id: &NativeId,
5561 key: &MetadataKey,
5562 value: &Value,
5563 ) -> Result<Option<Document>, SourceError> {
5564 Ok(self
5565 .set_slot_key(id, BoardKind::Document, key, value)
5566 .await?
5567 .map(|item| item.document()))
5568 }
5569
5570 async fn delete_task(&self, id: &NativeId) -> Result<(), SourceError> {
5571 self.delete_item(id).await
5572 }
5573
5574 async fn delete_project(&self, id: &NativeId) -> Result<(), SourceError> {
5575 self.delete_item(id).await
5576 }
5577
5578 async fn delete_document(&self, id: &NativeId) -> Result<(), SourceError> {
5579 self.delete_item(id).await
5580 }
5581
5582 /// One page of the task issue's own comments, walked by GitHub's own cursor.
5583 ///
5584 /// Nothing here filters, so nothing has to be read ahead of the page: the caller's limit is
5585 /// the page GitHub is asked for and GitHub's `endCursor` is the cursor handed back.
5586 async fn task_comments(
5587 &self,
5588 task: &NativeId,
5589 page: &PageRequest,
5590 ) -> Result<Option<Page<Comment>>, SourceError> {
5591 validate_page(page)?;
5592 let Some(issue) = self.commented_issue(task).await? else {
5593 return Ok(None);
5594 };
5595 let after = page.cursor.as_ref().map(|cursor| cursor.0.as_str());
5596 let data = self
5597 .graphql(
5598 graphql::ISSUE_COMMENTS,
5599 json!({"id":issue.0,"first":page.limit.min(MAX_PAGE_SIZE),"after":after}),
5600 )
5601 .await?;
5602 // The issue was there a moment ago; one removed since is no longer a task here.
5603 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
5604 return Ok(None);
5605 };
5606 let connection = node
5607 .get("comments")
5608 .filter(|value| !value.is_null())
5609 .ok_or_else(|| SourceError::Malformed {
5610 message: format!(
5611 "GitHub issue {} answered with no comments connection",
5612 issue.0
5613 ),
5614 })?;
5615 let items = optional_nodes(Some(connection), "issue comments")?
5616 .into_iter()
5617 .flatten()
5618 .map(comment_from)
5619 .collect::<Result<Vec<_>, _>>()?;
5620 let next = next_cursor(connection)?;
5621 if let Some(next) = &next {
5622 validate_cursor_progress(after, &next.0)?;
5623 }
5624 Ok(Some(Page { items, next }))
5625 }
5626
5627 /// Add one comment to the task's issue, as the account the token belongs to.
5628 ///
5629 /// The author is refused before anything is sent — not even the task is read — because
5630 /// no answer GitHub could give would make posting under another name than the one asked
5631 /// for the right outcome.
5632 async fn add_comment(
5633 &self,
5634 task: &NativeId,
5635 comment: &NewComment,
5636 ) -> Result<Option<Comment>, SourceError> {
5637 if let Some(author) = &comment.author {
5638 return Err(SourceError::Refused {
5639 message: format!(
5640 "source {} cannot post a comment as {author:?}: GitHub records the account \
5641 the token signs in as the author of every comment; next: leave --author \
5642 out, and the comment is posted as that account",
5643 self.name
5644 ),
5645 });
5646 }
5647 let Some(issue) = self.commented_issue(task).await? else {
5648 return Ok(None);
5649 };
5650 let data = self
5651 .graphql(
5652 graphql::ADD_COMMENT,
5653 json!({"input":{"subjectId":issue.0,"body":comment.body.as_str()}}),
5654 )
5655 .await?;
5656 let subject = data
5657 .pointer("/addComment/subject")
5658 .filter(|value| !value.is_null())
5659 .ok_or_else(|| SourceError::Malformed {
5660 message: "GitHub comment addition returned no subject".into(),
5661 })?;
5662 if required_str(subject, "id")? != issue.0 {
5663 return Err(SourceError::Malformed {
5664 message: "GitHub comment addition answered about another issue".into(),
5665 });
5666 }
5667 let added = data
5668 .pointer("/addComment/commentEdge/node")
5669 .filter(|value| !value.is_null())
5670 .ok_or_else(|| SourceError::Malformed {
5671 message: "GitHub comment addition returned no comment".into(),
5672 })?;
5673 comment_from(added).map(Some)
5674 }
5675
5676 async fn edit_comment(
5677 &self,
5678 task: &NativeId,
5679 comment: &NativeId,
5680 body: &CommentBody,
5681 ) -> Result<Option<Comment>, SourceError> {
5682 let Some(issue) = self.commented_issue(task).await? else {
5683 return Ok(None);
5684 };
5685 if !self.comment_is_on(&issue, comment).await? {
5686 return Ok(None);
5687 }
5688 let data = self
5689 .graphql(
5690 graphql::UPDATE_COMMENT,
5691 json!({"input":{"id":comment.0,"body":body.as_str()}}),
5692 )
5693 .await?;
5694 let edited = data
5695 .pointer("/updateIssueComment/issueComment")
5696 .filter(|value| !value.is_null())
5697 .ok_or_else(|| SourceError::Malformed {
5698 message: "GitHub comment update returned no comment".into(),
5699 })?;
5700 let edited = comment_from(edited)?;
5701 if edited.id != *comment {
5702 return Err(SourceError::Malformed {
5703 message: "GitHub comment update returned the wrong comment".into(),
5704 });
5705 }
5706 Ok(Some(edited))
5707 }
5708
5709 async fn delete_comment(
5710 &self,
5711 task: &NativeId,
5712 comment: &NativeId,
5713 ) -> Result<Option<NativeId>, SourceError> {
5714 let Some(issue) = self.commented_issue(task).await? else {
5715 return Ok(None);
5716 };
5717 if !self.comment_is_on(&issue, comment).await? {
5718 return Ok(None);
5719 }
5720 let data = self
5721 .graphql(graphql::DELETE_COMMENT, json!({"input":{"id":comment.0}}))
5722 .await?;
5723 // The payload says nothing about the comment it removed, so what is checked is that
5724 // GitHub answered the mutation at all rather than leaving it unanswered.
5725 data.get("deleteIssueComment")
5726 .filter(|value| !value.is_null())
5727 .ok_or_else(|| SourceError::Malformed {
5728 message: "GitHub comment deletion returned no payload".into(),
5729 })?;
5730 Ok(Some(comment.clone()))
5731 }
5732
5733 /// Every request this source has recorded, and what each of GitHub's two budgets was
5734 /// attributed — read off the same accounting the session report is rendered from, so
5735 /// the two cannot count one request two ways.
5736 async fn metering(&self) -> Result<Option<Metering>, SourceError> {
5737 Ok(Some(self.ledger.snapshot().metering()))
5738 }
5739}
5740
5741/// One issue comment as the contract carries it.
5742///
5743/// `author` is absent both when GitHub answers `null` for an account that no longer exists
5744/// and when it answers an actor with no login, because either way the source did not say who
5745/// wrote it — which is what an absent author means, rather than an author called nothing.
5746fn comment_from(value: &Value) -> Result<Comment, SourceError> {
5747 Ok(Comment {
5748 id: NativeId(required_str(value, "id")?.to_owned()),
5749 author: optional_str(value.get("author").unwrap_or(&Value::Null), "login")?
5750 .map(str::to_owned),
5751 created_at: optional_time(value, "createdAt")?,
5752 updated_at: optional_time(value, "updatedAt")?,
5753 body: required_str(value, "body")?.to_owned(),
5754 url: optional_str(value, "url")?.map(str::to_owned),
5755 })
5756}
5757
5758/// Where the recorded tail of a dependency walk resumes; see
5759/// [`GitHubProjectsSource::recorded_edges`].
5760const RECORDED_CURSOR: &str = "onetaskgraph.depends_on:";
5761
5762/// The board text field this source keeps a copy's origin in.
5763///
5764/// Named after the key it holds, and held to that name by the guard below rather than by
5765/// a reader noticing.
5766const ORIGIN_FIELD: &str = "onetaskgraph.origin";
5767
5768/// The metadata key that field holds.
5769///
5770/// The engine owns this key and spells it once as `GlobalId::ORIGIN_KEY`; a plugin never
5771/// constructs or interprets the qualified id it carries. This source names it only to
5772/// route it — a short, typed value belongs in a typed field rather than in the body slot
5773/// a caller's own prose shares.
5774///
5775/// Restated rather than imported, because no plugin crate may depend on the engine. What
5776/// keeps the two spellings one contract is `scripts/check-origin-key-spelling.sh`, a
5777/// target in `check`: it reads the engine's own literal and fails naming the file and the
5778/// line when a plugin's parts from it either way. Drift here has one symptom — a copy
5779/// that creates a second item every run instead of finding the one it wrote — and that is
5780/// too late to learn it.
5781const ORIGIN_KEY: &str = "onetaskgraph.origin";
5782
5783/// Where a recorded tail resumes, refusing a cursor no walk in `direction` reported.
5784///
5785/// The reserved key holds forward edges and nothing else — the reverse of a recorded edge
5786/// is derived from the far end, never written down on the near item — so only a forward
5787/// walk ever reports one of these cursors. A reverse read carrying one is resuming a walk
5788/// it did not come from, and it is told so rather than answered with an empty page that
5789/// reads as a walk which ended.
5790fn recorded_offset(
5791 cursor: Option<&str>,
5792 direction: Direction,
5793) -> Result<Option<usize>, SourceError> {
5794 cursor
5795 .and_then(|cursor| cursor.strip_prefix(RECORDED_CURSOR))
5796 .map(|offset| {
5797 if direction != Direction::DependsOn {
5798 return Err(SourceError::Config {
5799 message: format!(
5800 "{RECORDED_CURSOR}{offset} resumes recorded forward edges, which a \
5801 reverse dependency read never issues; resume it in the direction \
5802 that reported it"
5803 ),
5804 });
5805 }
5806 offset.parse().map_err(|_| SourceError::Config {
5807 message: format!("{RECORDED_CURSOR}{offset} is not a recorded-edge cursor"),
5808 })
5809 })
5810 .transpose()
5811}
5812
5813fn recorded_page(edges: Vec<DependencyEdge>, offset: usize, limit: usize) -> Page<DependencyEdge> {
5814 let mut page = offset_page(edges, offset, limit.max(1));
5815 page.next = page
5816 .next
5817 .map(|cursor| Cursor(format!("{RECORDED_CURSOR}{}", cursor.0)));
5818 page
5819}
5820
5821/// The kind of one issue reached through a dependency connection.
5822///
5823/// The same questions the board scan asks, over the fields the dependency document
5824/// selects, and in the same order: the design prefix first, then a sub-issue is a task,
5825/// then anything with sub-issues or the marker is a project.
5826///
5827/// # Errors
5828///
5829/// A far end this board holds as a document is refused rather than reported. The two
5830/// answers that are not refusals would both be wrong: reporting it as a task names an id
5831/// no task read of this source can find, and reporting it as a project names one no
5832/// project read can. There is no third value to return — `ItemKind` has no document
5833/// variant, because nothing may point at a document — so the relationship itself is what
5834/// the person is told about.
5835fn related_kind(value: &Value) -> Result<ItemKind, SourceError> {
5836 let id = required_str(value, "id")?;
5837 if required_str(value, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
5838 return Err(SourceError::Refused {
5839 message: format!(
5840 "GitHub issue {id} is a document of this board — its title begins \
5841 {DESIGN_TITLE_PREFIX:?} — and nothing may depend on a document or be depended \
5842 on by one; next: remove that issue's blocking relationship on this board"
5843 ),
5844 });
5845 }
5846 let parent = optional_str(value.get("parent").unwrap_or(&Value::Null), "id")?;
5847 if parent.is_some() {
5848 return Ok(ItemKind::Task);
5849 }
5850 let (_, slot) = metadata_body(optional_str(value, "body")?.map(str::to_owned))?;
5851 let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
5852 message: format!("GitHub issue {id}: {message}"),
5853 })?;
5854 let sub_issues = sub_issue_total(value)?;
5855 Ok(if sub_issues > 0 || marked == Some(ItemKind::Project) {
5856 ItemKind::Project
5857 } else {
5858 ItemKind::Task
5859 })
5860}
5861
5862/// The `IssueStateUpdateInput` one status target asks for.
5863///
5864/// `stateInput` and `state` are mutually exclusive on `UpdateIssueInput`, and only this
5865/// one is ever sent. A non-terminal status always asks for `OPEN`, which is what reopens
5866/// a currently-closed issue: without that the item would read back `Unknown` and a copy
5867/// would report a change forever. A document has no status at all, and asks for neither.
5868fn state_input(target: Option<&StatusTarget>) -> Value {
5869 match target {
5870 Some(StatusTarget::Terminal(_, reason)) => {
5871 json!({"value":"CLOSED","stateReason":reason.reason()})
5872 }
5873 Some(StatusTarget::Column(_) | StatusTarget::Disabled) => json!({"value":"OPEN"}),
5874 // A document has no status, so a write of one says nothing about the issue's open
5875 // or closed state rather than forcing it open: `stateInput` is what carries that
5876 // instruction, and an explicit null asks for no change to it.
5877 None => Value::Null,
5878 }
5879}
5880
5881/// The metadata one write stores in the item's body slot.
5882///
5883/// The typed fields travel as themselves, so the three reserved keys are rebuilt here
5884/// rather than carried: the kind marker so an empty project stays readable, the
5885/// repository list only when it is not exactly the issue's own repository, and the far
5886/// ends no relationship here can name.
5887fn slot_metadata(
5888 incoming: &Incoming<'_>,
5889 own_repository: Option<&Repository>,
5890 fallback: &[DependencyEdge],
5891) -> BTreeMap<String, Value> {
5892 let mut metadata = incoming.metadata.clone();
5893 metadata.remove(ORIGIN_KEY);
5894 match incoming.written.kind() {
5895 BoardKind::Work(kind) => metadata.insert(
5896 ItemKind::METADATA_KEY.to_owned(),
5897 Value::String(kind.marker().to_owned()),
5898 ),
5899 // A document is told by its title, so it carries no kind marker: that key names
5900 // what a dependency endpoint points at, and nothing may point at a document.
5901 BoardKind::Document => metadata.remove(ItemKind::METADATA_KEY),
5902 };
5903 let derivable = own_repository
5904 .map(|own| incoming.repositories == [own.clone()])
5905 .unwrap_or(incoming.repositories.is_empty());
5906 if derivable {
5907 metadata.remove(Repository::METADATA_KEY);
5908 } else {
5909 metadata.insert(
5910 Repository::METADATA_KEY.to_owned(),
5911 Value::Array(
5912 incoming
5913 .repositories
5914 .iter()
5915 .map(|repository| Value::String(repository.as_str().to_owned()))
5916 .collect(),
5917 ),
5918 );
5919 }
5920 // The typed lists are what land, whatever the caller's own metadata held under their
5921 // keys: a key of either name travelling beside the field would otherwise be a second
5922 // answer to the same question, and the field is the one the contract names.
5923 for (key, entries) in [
5924 (TaskRef::DELIVERS_KEY, incoming.delivers),
5925 (TaskRef::DELIVERED_BY_KEY, incoming.delivered_by),
5926 ] {
5927 set_task_list(&mut metadata, key, entries);
5928 }
5929 if fallback.is_empty() {
5930 metadata.remove(DependencyEdge::RECORDED_KEY);
5931 } else {
5932 metadata.insert(
5933 DependencyEdge::RECORDED_KEY.to_owned(),
5934 Value::Array(
5935 fallback
5936 .iter()
5937 .map(|edge| json!({"id":edge.to.id(),"kind":edge.to.kind}))
5938 .collect(),
5939 ),
5940 );
5941 }
5942 metadata
5943}
5944
5945/// Every label one item carries, from its content's own connection and nowhere else.
5946///
5947/// There is no second place to read one from: no document this source sends selects the
5948/// board's built-in `Labels` field, because GitHub derives it from the content and a draft
5949/// cannot carry one at all. The module documentation records the three schema facts that
5950/// settle it.
5951fn labels(content: &Value) -> Result<Vec<Label>, SourceError> {
5952 optional_nodes(content.get("labels"), "content labels")?
5953 .into_iter()
5954 .flatten()
5955 .map(|v| {
5956 Ok(Label {
5957 id: NativeId(required_str(v, "id")?.to_owned()),
5958 name: required_str(v, "name")?.to_owned(),
5959 color: optional_str(v, "color")?.map(str::to_owned),
5960 })
5961 })
5962 .collect()
5963}
5964
5965/// The definition of each board field one item's values are values of, in the shape a read
5966/// of the board's own `fields` gives one.
5967///
5968/// A value names its field through a fragment on that field's own type, so the type is
5969/// known from which kind of value it is: a single-select value's field is a
5970/// `ProjectV2SingleSelectField`, options and all, and a text value's is a `ProjectV2Field`.
5971/// A value whose field carried no id, or an empty one, says nothing usable and is left out.
5972fn field_definitions(field_values: &[Value]) -> Vec<Value> {
5973 field_values
5974 .iter()
5975 .filter_map(|value| {
5976 let field = value.get("field")?.as_object()?;
5977 field.get("id")?.as_str().filter(|id| !id.is_empty())?;
5978 let typename = if value.get("text").is_some() {
5979 "ProjectV2Field"
5980 } else if value.get("name").is_some() {
5981 "ProjectV2SingleSelectField"
5982 } else {
5983 return None;
5984 };
5985 let mut defined = field.clone();
5986 defined.insert("__typename".to_owned(), json!(typename));
5987 Some(Value::Object(defined))
5988 })
5989 .collect()
5990}
5991
5992fn text_field(field_values: &[Value], name: &str) -> Result<Option<String>, SourceError> {
5993 let Some(node) = field_values
5994 .iter()
5995 .find(|node| node.pointer("/field/name").and_then(Value::as_str) == Some(name))
5996 else {
5997 return Ok(None);
5998 };
5999 Ok(optional_str(node, "text")?.map(str::to_owned))
6000}
6001
6002fn valid_github_owner(owner: &str) -> bool {
6003 !owner.is_empty()
6004 && owner.len() <= 39
6005 && !owner.starts_with('-')
6006 && !owner.ends_with('-')
6007 && !owner.contains("--")
6008 && owner
6009 .bytes()
6010 .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
6011}
6012
6013/// GitHub's repository-name grammar: 1-100 ASCII letters, digits, `-`, `_` or `.`, and
6014/// neither of the two names a path segment already means.
6015fn valid_github_repository_name(name: &str) -> bool {
6016 !name.is_empty()
6017 && name.len() <= 100
6018 && name != "."
6019 && name != ".."
6020 && name
6021 .bytes()
6022 .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
6023}
6024
6025fn valid_environment_name(name: &str) -> bool {
6026 let mut bytes = name.bytes();
6027 bytes
6028 .next()
6029 .is_some_and(|byte| byte.is_ascii_alphabetic() || byte == b'_')
6030 && bytes.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
6031}
6032
6033/// How many sub-issues one issue has.
6034///
6035/// `Issue.subIssuesSummary` is `SubIssuesSummary!` and its `total` is `Int!`, so an
6036/// absent or non-integer one is a response this source cannot read — and reading it as
6037/// zero would classify a project as a task, which is exactly the mistake the marker
6038/// exists to keep from happening quietly.
6039fn sub_issue_total(issue: &Value) -> Result<u64, SourceError> {
6040 let summary = issue
6041 .get("subIssuesSummary")
6042 .ok_or_else(|| SourceError::Malformed {
6043 message: "GitHub issue is missing subIssuesSummary".into(),
6044 })?;
6045 summary
6046 .get("total")
6047 .and_then(Value::as_u64)
6048 .ok_or_else(|| SourceError::Malformed {
6049 message: "GitHub issue subIssuesSummary.total is not an unsigned integer".into(),
6050 })
6051}
6052
6053fn required_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
6054 value
6055 .get(field)
6056 .and_then(Value::as_str)
6057 .ok_or_else(|| SourceError::Malformed {
6058 message: format!("GitHub response is missing string field {field}"),
6059 })
6060}
6061
6062fn required_nonblank_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
6063 let found = required_str(value, field)?;
6064 if found.trim().is_empty() {
6065 return Err(SourceError::Malformed {
6066 message: format!("GitHub response has blank string field {field}"),
6067 });
6068 }
6069 Ok(found)
6070}
6071
6072/// The slot's delimiters, which `docs/metadata.md` settles once for every source that
6073/// needs one — Linear spells them too, in its own description field.
6074///
6075/// Restated rather than shared, because a plugin crate depends on the contract crate and
6076/// nothing else of this workspace. `scripts/check-metadata-slot-encoding.sh`, a target in
6077/// `check`, is what keeps the two one encoding: drift is otherwise quiet, since each
6078/// source round-trips its own writes perfectly well under its own spelling.
6079const METADATA_OPEN: &str = "<!-- onetaskgraph.metadata\n";
6080const METADATA_CLOSE: &str = "\n-->";
6081
6082/// What the composer puts between a non-empty visible body and the slot, and the one thing
6083/// the parser takes off the visible body when it takes the slot off — exactly once, so every
6084/// other trailing byte of the body comes back as it was written.
6085// llmlint: ignore[contracts_have_one_source_or_a_drift_gate] How a composer lays the slot after prose is this source's own; `docs/metadata.md` and its gate settle only the delimiters, and no other source declares a separator to reconcile against.
6086const METADATA_SEPARATOR: &str = "\n\n";
6087
6088/// The visible body and the metadata slot at the end of it.
6089///
6090/// The encoding is the one `docs/metadata.md` settles for Linear, which is where its
6091/// reasons are. Only a comment at the very end is a slot; one in the middle is a person's
6092/// own content and is left alone. The visible body is everything before the slot less the
6093/// one [`METADATA_SEPARATOR`] the composer put there, byte for byte.
6094fn metadata_body(
6095 body: Option<String>,
6096) -> Result<(Option<String>, BTreeMap<String, Value>), SourceError> {
6097 let Some(body) = body else {
6098 return Ok((None, BTreeMap::new()));
6099 };
6100 let Some(slot) = slot_span(&body)? else {
6101 return Ok((Some(body), BTreeMap::new()));
6102 };
6103 let metadata =
6104 serde_json::from_str(&body[slot.encoded_start..slot.encoded_end]).map_err(|error| {
6105 SourceError::Malformed {
6106 message: format!(
6107 "invalid canonical JSON in GitHub issue onetaskgraph metadata slot: {error}"
6108 ),
6109 }
6110 })?;
6111 let before = &body[..slot.start];
6112 let visible = before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before);
6113 Ok(((!visible.is_empty()).then(|| visible.to_owned()), metadata))
6114}
6115
6116/// Where the metadata slot sits in one body, as byte offsets into it.
6117struct SlotSpan {
6118 /// Where [`METADATA_OPEN`] begins.
6119 start: usize,
6120 /// Where the encoded JSON begins, just past [`METADATA_OPEN`].
6121 encoded_start: usize,
6122 /// Where the encoded JSON ends, at the start of [`METADATA_CLOSE`].
6123 encoded_end: usize,
6124 /// Just past [`METADATA_CLOSE`].
6125 end: usize,
6126}
6127
6128/// The slot at the very end of `body`, or `None` when it has none.
6129///
6130/// The one reading of *where the slot is*, shared by [`metadata_body`], which reads it, and
6131/// [`with_slot`], which rewrites it — so the two cannot disagree about which comment is the
6132/// slot.
6133fn slot_span(body: &str) -> Result<Option<SlotSpan>, SourceError> {
6134 let Some(start) = body.rfind(METADATA_OPEN) else {
6135 return Ok(None);
6136 };
6137 let encoded_start = start + METADATA_OPEN.len();
6138 let Some(relative_end) = body[encoded_start..].find(METADATA_CLOSE) else {
6139 return Err(SourceError::Malformed {
6140 message: "unterminated onetaskgraph metadata slot in GitHub issue body".into(),
6141 });
6142 };
6143 let encoded_end = encoded_start + relative_end;
6144 let end = encoded_end + METADATA_CLOSE.len();
6145 if !body[end..].trim().is_empty() {
6146 return Ok(None);
6147 }
6148 Ok(Some(SlotSpan {
6149 start,
6150 encoded_start,
6151 encoded_end,
6152 end,
6153 }))
6154}
6155
6156/// `body` with its metadata slot holding exactly `metadata`, and every byte outside the
6157/// slot as it was.
6158///
6159/// A slot that is there has its JSON replaced in place; one that becomes empty is removed
6160/// together with the one [`METADATA_SEPARATOR`] separating it from the prose before it. A
6161/// body with no slot gains one the way [`compose_body`] writes it — after that separator,
6162/// or alone in an empty body — and a body with no slot that is given no metadata is
6163/// returned as it is.
6164fn with_slot(body: &str, metadata: &BTreeMap<String, Value>) -> Result<String, SourceError> {
6165 let encoded = if metadata.is_empty() {
6166 None
6167 } else {
6168 Some(
6169 serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
6170 message: error.to_string(),
6171 })?,
6172 )
6173 };
6174 Ok(match (slot_span(body)?, encoded) {
6175 (Some(slot), Some(encoded)) => format!(
6176 "{}{encoded}{}",
6177 &body[..slot.encoded_start],
6178 &body[slot.encoded_end..]
6179 ),
6180 (Some(slot), None) => {
6181 let before = &body[..slot.start];
6182 format!(
6183 "{}{}",
6184 before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before),
6185 &body[slot.end..]
6186 )
6187 }
6188 (None, None) => body.to_owned(),
6189 (None, Some(encoded)) if body.is_empty() => {
6190 format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
6191 }
6192 (None, Some(encoded)) => {
6193 format!("{body}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
6194 }
6195 })
6196}
6197
6198/// Hold `entries` under `key` in one slot's metadata, or no such key when there are none.
6199fn set_task_list(metadata: &mut BTreeMap<String, Value>, key: &str, entries: &[TaskRef]) {
6200 if entries.is_empty() {
6201 metadata.remove(key);
6202 } else {
6203 metadata.insert(
6204 key.to_owned(),
6205 Value::Array(
6206 entries
6207 .iter()
6208 .map(|entry| Value::String(entry.as_str().to_owned()))
6209 .collect(),
6210 ),
6211 );
6212 }
6213}
6214
6215fn compose_body(
6216 content: Option<&str>,
6217 metadata: &BTreeMap<String, Value>,
6218) -> Result<Option<String>, SourceError> {
6219 let visible = content.unwrap_or_default();
6220 if metadata.is_empty() {
6221 return Ok((!visible.is_empty()).then(|| visible.to_owned()));
6222 }
6223 let encoded = serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
6224 message: error.to_string(),
6225 })?;
6226 Ok(Some(if visible.is_empty() {
6227 format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
6228 } else {
6229 format!("{visible}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
6230 }))
6231}
6232
6233fn required_bool(value: &Value, field: &str) -> Result<bool, SourceError> {
6234 value
6235 .get(field)
6236 .and_then(Value::as_bool)
6237 .ok_or_else(|| SourceError::Malformed {
6238 message: format!("GitHub response is missing boolean field {field}"),
6239 })
6240}
6241fn optional_str<'a>(value: &'a Value, field: &str) -> Result<Option<&'a str>, SourceError> {
6242 match value.get(field) {
6243 None | Some(Value::Null) => Ok(None),
6244 Some(value) => value
6245 .as_str()
6246 .map(Some)
6247 .ok_or_else(|| SourceError::Malformed {
6248 message: format!("GitHub response field {field} is not a string or null"),
6249 }),
6250 }
6251}
6252fn optional_nodes<'a>(
6253 connection: Option<&'a Value>,
6254 name: &str,
6255) -> Result<Option<&'a Vec<Value>>, SourceError> {
6256 match connection {
6257 None | Some(Value::Null) => Ok(None),
6258 Some(value) => value
6259 .get("nodes")
6260 .and_then(Value::as_array)
6261 .map(Some)
6262 .ok_or_else(|| SourceError::Malformed {
6263 message: format!("GitHub {name}.nodes is not an array"),
6264 }),
6265 }
6266}
6267fn complete_connection(connection: &Value, name: &str, size: u32) -> Result<(), SourceError> {
6268 let page_info = connection
6269 .get("pageInfo")
6270 .ok_or_else(|| SourceError::Malformed {
6271 message: format!("GitHub {name} has no pageInfo"),
6272 })?;
6273 if required_bool(page_info, "hasNextPage")? {
6274 return Err(SourceError::Malformed {
6275 message: format!(
6276 "GitHub {name} exceeds the supported nested connection size of {size}"
6277 ),
6278 });
6279 }
6280 Ok(())
6281}
6282fn optional_time(value: &Value, field: &str) -> Result<Option<DateTime<Utc>>, SourceError> {
6283 optional_str(value, field)?
6284 .map(|timestamp| {
6285 timestamp.parse().map_err(|error| SourceError::Malformed {
6286 message: format!("GitHub response field {field} is not a timestamp: {error}"),
6287 })
6288 })
6289 .transpose()
6290}
6291fn validate_page(page: &PageRequest) -> Result<(), SourceError> {
6292 if page.limit == 0 {
6293 Err(SourceError::Config {
6294 message: "page limit must be at least 1".into(),
6295 })
6296 } else {
6297 Ok(())
6298 }
6299}
6300fn next_cursor(connection: &Value) -> Result<Option<Cursor>, SourceError> {
6301 let page = connection
6302 .get("pageInfo")
6303 .filter(|value| value.is_object())
6304 .ok_or_else(|| SourceError::Malformed {
6305 message: "GitHub connection is missing pageInfo".into(),
6306 })?;
6307 if required_bool(page, "hasNextPage")? {
6308 let cursor = required_str(page, "endCursor")?;
6309 validate_cursor_progress(None, cursor)?;
6310 Ok(Some(Cursor(cursor.into())))
6311 } else {
6312 Ok(None)
6313 }
6314}
6315fn validate_cursor_progress(previous: Option<&str>, next: &str) -> Result<(), SourceError> {
6316 if next.is_empty() || previous == Some(next) {
6317 Err(SourceError::Malformed {
6318 message: "GitHub pagination cursor is empty or did not advance".into(),
6319 })
6320 } else {
6321 Ok(())
6322 }
6323}
6324fn numeric_cursor(cursor: Option<&Cursor>) -> Result<usize, SourceError> {
6325 cursor.map_or(Ok(0), |c| {
6326 c.0.parse().map_err(|_| SourceError::Config {
6327 message: "page cursor is invalid".into(),
6328 })
6329 })
6330}
6331fn offset_page<T>(mut items: Vec<T>, offset: usize, limit: usize) -> Page<T> {
6332 if offset > items.len() {
6333 return Page::last(vec![]);
6334 }
6335 let tail = items.split_off(offset);
6336 let mut selected = tail;
6337 let next = (selected.len() > limit).then(|| Cursor((offset + limit).to_string()));
6338 selected.truncate(limit);
6339 Page {
6340 items: selected,
6341 next,
6342 }
6343}