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. The link a copy records on an item it copied, `onetaskgraph.copies`, is small and
81//! is kept in that same slot, written by that same update.
82//!
83//! **A caller's value can also be projected onto a board text field**, so the board can filter
84//! on it: each [`GitHubProjectsConfig::metadata_fields`] entry names a text field, a top-level
85//! metadata key and a path inside its value. The slot stays the value's one home — every read
86//! reports metadata from it alone — and the field follows it on every write of an item, a
87//! create, an update and a copy alike: a string is written, and not sent again while the field
88//! holds it — an empty string included, which is written as itself; nothing there or `null`
89//! clears a field holding a value; any other
90//! JSON type is refused before any mutation. It rides the ordered [`graphql::UPDATE_FIELDS`]
91//! write beside `Status`, `Priority` and the origin, so it adds no request; a key set on its
92//! own moves its field first and the body last, as every write of an existing item does. A
93//! board without the field, or with a non-text field of that name, is refused before any
94//! mutation pointing at `sources fields`, whose [`GitHubProjectsSource::fields`] creates it as
95//! a text field and never changes a field that is there.
96//!
97// 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.
98//! **Status.** `status_mapping` is per-instance configuration, in the shared grammar
99//! [`onetaskgraph_plugin_api::StatusMapping`] documents, from a status category to an
100//! option of the board's one `Status` field for a task and for a project: a bare name is
101//! the option for both kinds, `null` disables the category for both, and `{task, project}`
102//! names it per kind. A category the mapping does not mention keeps its shipped default for
103//! both kinds; one it mentions is exactly what it configures, so a per-kind object no longer
104//! gets the shipped default for the kind it leaves out. Two categories one kind would read
105//! back from one option are refused as the configuration is read, ignoring case, while one
106//! option may stand for different categories of the two kinds. Writes go by the kind of the
107//! item written: a status that kind has no option for, or whose option the board lacks, is
108//! refused before any mutation, naming the source, the kind, the category and the key
109//! `status_mapping.<category>.<kind>` — there is no fallback. `done` selects its mapped
110//! option and closes the issue as `COMPLETED`; `cancelled` selects its mapped option and
111//! closes it as `NOT_PLANNED`, for either kind. Every open category reopens a closed issue
112//! before selecting its option. Reads give a closed issue's reason precedence over its
113//! option, while an open issue's option decides its category through its own kind's
114//! mapping, and an option that mapping does not name reads as `unknown` under its own name.
115//! The guarded [`GitHubProjectsSource::status_options`] and
116//! [`GitHubProjectsSource::fields`] operations are the one path here that calls
117//! `updateProjectV2Field`: GitHub replaces the whole option list, so they preserve every
118//! existing option id and verify the field and item assignments immediately afterwards.
119//! They ask for both kinds' options, counting a terminal category's mapped option as
120//! configured because a terminal write refuses without it. No ordinary source read or
121//! write calls that mutation, whose
122//! `singleSelectOptions` *overwrites* a field's option set, so no addition is additive
123//! and a mistake destroys every item's status. A status this board cannot represent is a
124//! refusal naming the status and the instance instead.
125//!
126//! `unknown` has no shipped option because this source cannot preserve an open-ended
127//! status word: it writes an existing board option and never
128//! creates an option. An operator may map `unknown` to one existing option, in which case
129//! every unknown word lands on that option and reads back as `unknown` under the option's
130//! name. This differs from `local-md`, which writes and reads the original word itself.
131//!
132//! The shipped terminal options are exactly `done: Done` and `cancelled: Cancelled`.
133//! `done` also closes the issue because GitHub derives `subIssuesSummary.completed`
134//! and the board's own `Sub-issues progress` field from closed sub-issues: a plan whose
135//! finished tasks were only moved to a "Done" column would read 0% complete forever.
136// llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
137//!
138//! # What this source declares, field by field
139//!
140//! One verdict per field of [`Capabilities`], and what `Native` means when this source
141//! says it. *Proven* means a shared journey drives it against the real
142//! binary over this source's own row in `crates/onetaskgraph-e2e-support/src/fixtures.rs`, and
143//! `every_row_declares_exactly_what_its_plugin_reports` is what keeps this list and
144//! [`capabilities`](TaskSource::capabilities) from parting.
145//!
146//! | Field | Verdict |
147//! | --- | --- |
148//! | `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. |
149//! | `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. |
150//! | `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. |
151//! | `assets` | **Supported and native.** Image references travel as GitHub user attachments in the repository the task or document issue lives in. Uploads are authenticated and verified before the issue body is written. |
152//! | `priority` | **Supported and proven** by an instance configured with `priority_mapping`, and declared unsupported by one without it, which reports every task's priority as `none` and sends exactly the requests it sent before priorities existed. The priority is the board's single-select `Priority` field: no value is `none`, a mapped option is its level, matched case-insensitively, and an option the mapping does not name fails the read of that task, naming the option. A write selects the mapped option, or clears the value for `none`; a board without the field or the option is refused, pointing at `sources fields`, which is the one thing that creates either. |
153//! | `filter_by_priority` | **Supported and proven,** over the priority each task reads as — `none` for every task of an instance without `priority_mapping`. |
154//! | `filter_by_comment_activity` | **Supported, and exact** for comments created and for comments edited at or after `commented_since`, in every repository — of any owner — the board's items live in. Applied by asking a narrower question rather than by reading the board: GitHub's issue search scoped by `project:<owner>/<number>` alone, with an `updated:>=` qualifier, names the candidates, and each candidate's own comments confirm it, so `ProjectV2.items` is never read. That rests on GitHub moving an issue's `updatedAt` when a comment on it is added **or edited**, which the credentialed journey `an_edited_comment_moves_its_issue_and_is_selected_since` re-takes on every run of this lane. The search is an index that lags a write — the credentialed lane has watched it miss a newly commented issue for thirty seconds — so an issue this process itself commented on in the same command is a candidate whatever the search says, read by its own node if the search did not name it, and has its comments read rather than being ruled out by an `updatedAt` from before the comment. A comment another process wrote is found only once the index has it, so a caller asking again from its last instant should overlap the two generously. |
155//! | `orphan_tasks` | **Supported and proven.** A task issue with no `parent` is in no project. |
156//! | `filter_by_label` | **Supported and proven,** over the issue's own labels. |
157//! | `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` for the item's kind — a task query by the task half, a project query by the project half, `unknown` included. |
158//! | `filter_by_metadata` | **Supported, and asked of GitHub** — through the body and never through a projected `metadata_fields` text field, which is the board's to filter on and not this source's to read. A query naming metadata values is one board-scoped issue search with each value a quoted phrase `in:body` — GitHub's index covers the metadata comment at the end of the body, which is where caller metadata lives — and every candidate is confirmed against its own parsed metadata comment, so only an item holding that string at that key and path is returned. **A value with no letter or digit is refused** — the empty string, whitespace or punctuation alone — before any request, as a `SourceError::Refused` (wire kind `refused`) naming the value: GitHub's index holds words, so no bounded query can find such a value, and this source neither reads the whole board for it nor answers it as empty. |
159//! | `filter_by_origin` | **Supported, and asked of GitHub without enumerating the board.** The union of three reads, each confirmed by an exact match against the item's own origin field: the board's field filter over the `onetaskgraph.origin` text field, the issue search for the id as a phrase in the body where a write of this release mirrors it, and this process's own writes. See *Where a read-after-write guarantee comes from* for the window the three leave. |
160//! | `reconcile_status` | **Supported,** and proven by `onetaskgraph-github-projects-e2e`'s `status_reconcile.rs`: the board's own `ProjectV2.items(query:)` filter, `is:closed`, read page by page and never the unfiltered items, each candidate confirmed from its own item node — a closed task whose card is not at the option the mapping gives its closure's category. |
161//! | `complete_by_issue` | **Supported,** and proven by `status_complete.rs` there and in the engine's suite: each issue a reference names read in one aliased request per fifty — its repository, its number, its first page of board memberships with each card's `Status` read by name — and nothing else of the board. |
162//! | `search_title` | **Supported, and asked of GitHub for a task,** over `Issue.title`: a task query's text is one board-scoped issue search for it as a phrase `in:title`, every candidate confirmed by the case-insensitive substring rule. GitHub matches whole words, so a task holding the text only inside a longer word is not returned — a narrowing this source declares rather than hides. **A text with no letter or digit that is not blank is refused** — `--` for one — before any request, as the same `refused` error naming the text, for the reason a metadata value like it is; a blank text is not refused, and keeps the board read it always had, confirmed by the same substring rule. A project query's text, and a document query's text when the query is scoped to no project, is that same board-scoped search for the same phrase in the same fields, refused on the same terms, every candidate confirmed by its kind and by the same substring rule, so it narrows exactly as a task's does; a document query scoped to one project sends no search, reads that project's sub-issues and confirms its text over them by the substring rule alone, so it is neither narrowed to whole words nor refused for a text with no letter or digit. A board draft is not an issue, so no text search lists one, a draft titled as a document included. |
163//! | `search_content` | **Supported,** on the same terms, `in:body`, over the visible body — the trailing metadata comment is not part of what the substring rule confirms. |
164//! | `task_dependencies` | **Supported and proven,** in both directions: `blockedBy` and `blocking`. |
165//! | `project_dependencies` | **Supported and proven,** in both directions, over the same two connections, because a project here is an issue. |
166//! | `max_page_size` | **Supported and proven.** [`MAX_PAGE_SIZE`], GitHub's own connection maximum. |
167//!
168//! Image writes use `POST https://uploads.github.com/user-attachments/assets` with `name`,
169//! `content_type` and the issue repository's numeric `repository_id` as query parameters,
170//! the source token as `Authorization: Bearer`, the image content type as `Content-Type`,
171//! and the raw bytes as the request body. The numeric repository id is read at most once
172//! per repository per source instance. A loopback API endpoint also selects the loopback
173//! uploads host; production needs no assets repository, commits or extra configuration.
174//! Every returned JSON `url` is read with the same token before a body is written: only
175//! a 2xx verification succeeds. A refusal names the asset, URL and HTTP status; an
176//! anonymous 404 does not invalidate an authenticated 200. An upload refusal names the
177//! asset and HTTP status and says the token type may not be accepted by the upload
178//! endpoint. A classic PAT was measured accepted; no acceptance claim is made about
179//! fine-grained PATs or OAuth tokens.
180//! The body keeps the authored alt text while `./<name>` becomes the attachment URL,
181//! and `onetaskgraph.assets` records `{sha256, url}` by name. A matching destination
182//! SHA-256 reuses its URL without an upload or verifying read; changed bytes are uploaded
183//! and verified afresh, in the repository the issue already lives in on an update.
184//! An uploaded image renders for the viewers GitHub lets read it, like the issue text beside it.
185//! Uploads take the same mutation spacing as content writes. Both uploads and verification
186//! reads wait out classified rate limits within the configured per-call retry budget,
187//! on the supplied clock; verifying reads take no mutation slot.
188//!
189//! Nothing here is unsupported. `documents` and `comments` are not predicates — they say this
190//! source has documents and that its tasks have comments, both of which hold — and the three
191//! facts behind the uniform `Native` on the
192//! predicates beside it are recorded below rather than re-derived, because a reader who
193//! takes `Native` to mean *the remote service filters* will read that uniformity as a
194//! lie.
195//!
196//! First, the plugin contract defines `Support::Native` as *the source applies this
197//! predicate itself*, and says nothing about where it applies it. What the declaration
198//! promises the engine is capability rule 1 — a predicate declared `Native` **is** applied
199//! — so that the engine may push it down and apply nothing of its own.
200//!
201//! Second, this source can keep that promise for every predicate at no additional API
202//! cost, because whichever of the reads below answers a query has already read every
203//! candidate that query will return before it filters anything. Filtering those items is
204//! in-process work over data already in hand.
205//!
206//! Third, six task predicates are asked of GitHub as a narrower question and the rest are
207//! applied in process over what that question returned. A project filter has a relationship — a
208//! project's tasks are that issue's sub-issues, and asking the issue for them is both cheaper
209//! and exact. Comment activity is the issue search's `updated:` qualifier. A text search, and
210//! a search for metadata values, is the board-scoped issue search carrying the text and each
211//! value as quoted phrases; an origin is the board's own field filter over its origin field
212//! beside the same search for the id. **The text search narrows, and that is this source's
213//! declared semantics:** GitHub matches whole words where the substring rule this source and
214//! the local Markdown source confirm with would match inside one, so an item holding the text
215//! only inside a longer word is never a candidate. Every item returned does contain the text.
216//! A project query's text, and a document query's scoped to no project, is that same search
217//! and narrows on the same terms, its candidates confirmed by their kind as well.
218//! GitHub's issue search offers no qualifier for a label set, a status column or a priority,
219//! so those three are applied in process over the candidates, and a query carrying none of
220//! the six narrowing predicates reads the board. Declaring one `Unsupported` would make the
221//! engine compensate for work this source has already done, and declaring `projects` native
222//! while ignoring the filter (which this source once did) silently returns another project's
223//! tasks, because the engine trusts the declaration and applies nothing locally.
224//!
225//! # The three ways this source reaches an item, and what each costs
226//!
227//! A board read is charged for what its *nested* connections could return rather than for
228//! what was asked, so one whole-board read costs the same whether the question was about
229//! one project or about all of them. That is why a question about one project is never
230//! answered by reading the board:
231//!
232//! | The question | What is sent | What it costs |
233//! | --- | --- | --- |
234//! | one item, by its own id | [`graphql::ISSUE`] — `node(id:)`, carrying the field definitions of the boards it sits on and the far ends of its `blockedBy`, which is what a write of it needs — and, when that node is a board draft, [`graphql::DRAFT`] — the draft and the one board item it is | the item |
235//! | one task with its first page of comments, for `task show` and a comment listing | [`graphql::ISSUE_DETAIL`] — the same `node(id:)` read with the issue's `comments` | the item and a page of its comments |
236//! | several tasks with their comments, for `task show-many` | [`graphql::ISSUE_DETAILS`] — [`DETAIL_BATCH`] aliased `node(id:)` fields per request | each item and a page of its comments |
237//! | the board's own id and field definitions, for a write whose item does not carry them | [`graphql::BOARD_FIELDS`] — the board's `id` and `fields`, and no `items` — or, for a create that needs the repository's id too, [`graphql::CREATION_CONTEXT`], both in one request | the board's fields |
238//! | one project's tasks or documents | [`graphql::SUB_ISSUES`] — that issue's own `subIssues` | that project |
239//! | which projects this board holds | [`graphql::SEARCH_ISSUES`] — an issue search scoped to the board | the board's issues, without their board items |
240//! | which projects hold a text, or which documents do when no project narrows the question | [`graphql::SEARCH_ISSUES`] — the board-scoped search with the text as one quoted phrase, `in:title`, `in:body` or both, as a task's text is sent — walked to its end in pages of twenty | the issues that match |
241//! | which tasks were commented on since an instant | [`graphql::SEARCH_ISSUES`] — the same board-scoped search with an `updated:>=` qualifier — then [`graphql::ISSUE_COMMENTS`] for each candidate it names | the issues updated since, and their comments |
242//! | which tasks hold a text, or a metadata value | [`graphql::SEARCH_ISSUES`] — the board-scoped search with the text and each value as quoted phrases, `in:title`, `in:body` or both, and an `updated:>=` qualifier too when comment activity is asked for — in pages of twenty, only as many as the caller's rows need | the issues that match |
243//! | which tasks were copied from one origin | [`graphql::ORIGIN_LOOKUP`] — the board's own `items` under its field filter on the origin field, and the same board-scoped search for the id `in:body`, in one request, each paged at three | the carriers of that origin, which is one item |
244//! | every task, every document, every label, when nothing above narrows the question | [`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 |
245//! | 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 |
246//!
247//! The following standalone-ticket requests are pinned by the real CLI fixture journeys
248//! `follow_up_writes_resolve_each_item_once_and_batch_the_copy_fields` and
249//! `a_batched_detail_read_costs_one_request_and_one_point_per_detail_batch`, as request count
250//! equal to declared points equal to the row. They include the origin lookup and the
251//! field/repository discovery a create needs. A bound re-copy changes status, priority,
252//! content and metadata; comment recount means a subsequent detail read. Each request here
253//! costs one declared point. A membership beyond the embedded page can additionally require
254//! the one-point membership recovery described above. A bound re-copy of a task filed under a
255//! project adds one read, the engine confirming that project's link by its own id once per
256//! command; and the same-source far ends a write newly names — those that do not already block
257//! the item, whose own read answered for them — are read together by their own ids,
258//! [`DETAIL_BATCH`] to one [`graphql::ISSUE_DETAILS`] request, each new edge then one
259//! [`graphql::ADD_BLOCKED_BY`]. Both additions are rows of the table below, pinned by
260//! `a_bound_recopy_adds_one_project_read_and_batches_the_dependencies_it_newly_names`.
261//!
262//! **[`DETAIL_BATCH`] is 24**: the largest batch of [`graphql::ISSUE_DETAILS`] the node-count
263//! model prices at one point. Each aliased item is six of GitHub's aggregate, so 24 are 144,
264//! which rounds to one point, and 25 are 150, which rounds to two; `tests/point_cost.rs`
265//! holds both halves.
266//!
267//! **An existing item is written body last.** A bound re-copy and a `task update` send its
268//! board fields first — the `Status` option and the `Priority` together, in one request — then
269//! its parent and its `blockedBy`, and its title, body and state in one `updateIssue` last.
270//! GitHub runs no two requests as one, and runs a document's mutation fields in order without
271//! undoing an earlier field when a later one fails, so that order is what makes a write
272//! refused part-way leave the item's body, and every metadata key in it, exactly as it stood;
273//! the one piece of metadata written before the body, an origin a copy re-points, is put back
274//! when a later write is refused — and when putting it back is refused too, the write's own
275//! refusal names that key, what it now holds and what it held. `crates/onetaskgraph-github-projects-e2e/tests/e2e/write_order.rs` refuses each
276//! of those writes in turn, whole and as one aliased field failing after the one before it.
277//!
278//! **Two facts about GitHub the write rows rest on, each read off GitHub's published schema
279//! artifact <https://docs.github.com/public/fpt/schema.docs.graphql> on 2026-10-01 and pinned
280//! in `tests/fixtures/schema.graphql`, and the first then put to GitHub itself:**
281//!
282//! - **A board is accepted at creation but its item is not answered, so a create still files
283//! the issue itself: a new copy is 5 requests, and 4 with `--create`.**
284//! `CreateIssueInput.projectV2Ids: [ID!]` is declared there — "An array of Node IDs for
285//! Projects V2 associated with this issue", `@possibleTypes(concreteTypes: ["ProjectV2"])`.
286//! The credentialed journey `real_projects_v2_contract_writes_and_leaves_no_residue` was run
287//! against a real board on 2026-10-01 with a create sending the board there and reading the
288//! item off the payload's `Issue.projectItems`: every one of its four creates answered with
289//! no item on the board, so each went on to [`graphql::ADD_TO_BOARD`], and the fourth was
290//! refused "Content already exists in this project" — GitHub had filed the issue after
291//! answering, and refuses a second filing rather than answering with the item it holds. A
292//! create therefore sends no `projectV2Ids` and files the issue with
293//! `addProjectV2ItemById`, the one call whose answer names the board item. The saving that is
294//! real is the read before it: the board's fields and the repository's id together, in
295//! [`graphql::CREATION_CONTEXT`], at the point the repository is known.
296//! - **A comment still reads its target first, so a comment is 2 requests.**
297//! `AddCommentInput.subjectId: ID!` is declared there with
298//! `@possibleTypes(concreteTypes: ["Issue", "PullRequest"], abstractType:
299//! "IssueOrPullRequest")`. A board draft is no such subject and would be refused, but a
300//! project's issue, a document's issue, an issue on no board of this source and a pull
301//! request all are: GitHub writes the comment, so there is no refusal to map into "that is
302//! not a task of this board". [`graphql::ISSUE`] before [`graphql::ADD_COMMENT`] is what
303//! refuses those by name.
304//!
305//! | Verb | Requests / points | Documents |
306//! | --- | --- | --- |
307//! | new copy | 4 | ORIGIN_LOOKUPS (the origin, the board's fields and the repository's node and numeric ids together, a page of one carrier for each origin), CREATE_ISSUE filing the issue on the board and under its project, CREATED_BOARD_ITEMS once GitHub has added it, WRITE_FIELD_VALUES |
308//! | copy --create | 4 | CREATION_CONTEXT, CREATE_ISSUE, CREATED_BOARD_ITEMS, WRITE_FIELD_VALUES: the new copy with no origin to ask about |
309//! | bound copy | 3 | ISSUE (with the board's fields and the issue's `blockedBy`, so no BOARD_FIELDS or ISSUE_DEPENDENCIES), UPDATE_FIELDS when a board field differs, then UPDATE_ISSUE last carrying only the title, body or state that differs — none, when nothing does |
310//! | bound copy, filed under a project | 4 | the bound copy's three, and one ISSUE of the destination project its link names, read once per command |
311//! | bound copy, newly naming n dependencies | + ceil(n / DETAIL_BATCH) + n | ISSUE_DETAILS for the far ends that do not already block the item, DETAIL_BATCH (24) to a request (one alone is ISSUE), then one ADD_BLOCKED_BY each; a far end already blocking it is answered by its own read and costs nothing |
312//! | comment | 2 | ISSUE, ADD_COMMENT: the target is read first, because GitHub accepts a comment on any issue or pull request (see below) |
313//! | detail | 1 | ISSUE_DETAIL: the item and its first page of comments, for `task show` and `task comment list`; `--no-comments` is ISSUE alone |
314//! | batched detail | ceil(n / DETAIL_BATCH) | ISSUE_DETAILS: `task show-many` of `n` items, DETAIL_BATCH (24) at a time, comments included or not |
315//! | copy --comments, n changed tasks | ceil(n / COPY_DETAIL_BATCH) | ISSUE_COPY_DETAILS: each task with its first page of comments and its `blockedBy`, COPY_DETAIL_BATCH (21) at a time; a task whose comments run past that page adds one ISSUE_DETAIL per further page, and a task skipped from its listing costs nothing beyond it |
316//! | recount | 1 | ISSUE_DETAIL |
317//! | status | 2 | ISSUE, UPDATE_FIELD; a terminal status additionally updates issue state |
318//! | priority | 2 | ISSUE, UPDATE_FIELD or CLEAR_FIELD, with stored priority in the mutation response |
319//! | content | 2 | ISSUE, UPDATE_ISSUE |
320//! | metadata | 2 | ISSUE, UPDATE_ISSUE |
321//! | document metadata, `--expect` a version | 2 | ISSUE, then UPDATE_ISSUE only when the issue is still at that version — the precondition GitHub's own update cannot take — primed with what the read answered |
322//! | update | 3 | `task update` naming any of title, body, metadata, status and priority — all five included: ISSUE, UPDATE_FIELDS (the status option and the priority together), UPDATE_ISSUE (title, body with its metadata slot, and state) last |
323//! | record only | 1 | ISSUE |
324//!
325//! <!-- github-search-paging:start -->
326//! Board-scoped text, metadata, project-name and comment-activity searches send every
327//! page at `first = 20` (SEARCH_PAGE_SIZE), the SEARCH_ISSUES document's one-point
328//! ceiling. A later page is sent only when `hasNextPage` is true and the caller still
329//! needs rows. A page is never resized to the rows still needed: GitHub orders one
330//! search differently at different page sizes, so one fixed size makes a paged walk
331//! send exactly the requests one whole read sends, and the answer's order is the order
332//! those pages arrive in. A page below twenty would cost the same one point, and GitHub
333//! prices this document by rows, so twenty-row pages cost per row what 100-row pages do.
334//! Project-name lookup continues until an exact match or exhaustion. A task limit bounds
335//! returned and fetched pages: a limit is sliced from the pages it needs, and local
336//! confirmation can require more candidates than matching rows. Walking all pages
337//! returns the whole answer. The opaque version-4 source cursor carries GitHub's page
338//! cursor and how far into that page the last answer stopped, and resumes in the same
339//! process or a new one, without duplicates or gaps. It carries no rows: one process
340//! sends each page's search once, and a new process re-reads only the page it resumes
341//! in, then sends a further page once, never as a re-read, only when its limit still
342//! needs rows. Every request either walk sends is the one a whole read sends for that page. Own writes replace stale index
343//! copies and complete missing rows at exhaustion. Cache entries are whole GitHub pages,
344//! so a small answer cannot truncate a wider question. Origin pages remain three; whole-board sizing is unchanged.
345//! Read-after-write is a per-process guarantee. A cursor resumed in a new process is
346//! not required to include the original process's writes still omitted by the index.
347//! <!-- github-search-paging:end -->
348//!
349//! The board half of an issue — its board item's id, its `Status` option and this
350//! source's origin text field — rides along on `Issue.projectItems` in the first three, so
351//! an item reached any of those ways resolves through the same
352//! [`GitHubProjectsSource::resolve`] the board walk uses and reports the same title, the
353//! same status, the same labels and the same qualified id. That connection comes back a
354//! *page* at a time, at `BOARD_ITEMS_PAGE_SIZE`, so the entry for this board is looked for
355//! on the page in hand and — only if that page reports more of the connection — in the
356//! last row's read of that one issue's memberships, resumed from the page's own cursor and
357//! walked to exhaustion. An issue with no entry for *this* board is not this source's to
358//! report, which is what keeps an id naming another repository's issue from being answered
359//! as an item of this board; and because the page is where the search starts rather than
360//! where it ends, that answer is one about a connection read to exhaustion and never about
361//! an unread page. Nothing costs the extra read but an issue on more boards than a page
362//! holds: an issue this board really does not hold reports no next page, so its
363//! memberships are already exhausted where they arrived.
364//!
365//! **No document here selects the board's own `Labels` field, and nothing is lost by
366//! that.** An item's labels are read from its content alone, wherever that content is
367//! reached: the three documents above select `Issue.labels` on the fragment, and
368//! [`graphql::BOARD`] selects the same connection on the `... on Issue` arm of its
369//! `content`. A board's `Labels` field is not one anybody fills in: it is a built-in
370//! `ProjectV2FieldType`, it is absent from `ProjectV2CustomFieldType` so no project can
371//! create one, and `ProjectV2FieldValue` — the whole of what
372//! `updateProjectV2ItemFieldValue` accepts — offers no way to write one. So GitHub derives
373//! it from the content, for every content type it exists on, and there is nothing it can
374//! hold that the content does not already say: for an `Issue` it *is* that issue's own
375//! labels, so selecting it beside them unions a set with itself.
376//!
377//! **A draft loses nothing by that either**, which is the reasoning this paragraph once had
378//! backwards. `DraftIssue` exposes no `labels` field, and by the three schema facts above
379//! it cannot carry a board `Labels` value to be derived from one — so a draft has nothing
380//! to select *and nothing to lose*, and reports no labels at all. A `PullRequest` item is
381//! discarded by [`GitHubProjectsSource::resolve`] before labels are read. Both halves are
382//! held to that by tests in `tests/plugin.rs`: the four ways an item is reached report one
383//! label set, and that set is the fixture issue's own, by
384//! `an_item_reports_the_same_labels_title_status_and_id_however_it_is_reached`; and a board
385//! item whose content is a draft reports an empty set, by
386//! `a_board_item_whose_content_is_a_draft_reports_no_labels_at_all`. The absence of the
387//! selection is held over [`graphql::DOCUMENTS`] by
388//! `no_document_selects_the_boards_own_labels_field`.
389//!
390//! The whole-board row is still the board's own item connection, and deliberately: a
391//! **draft** board item is not an issue, so no search can list one, and the reads that have
392//! to answer for the whole board are the ones whose cost is the board's size anyway.
393//!
394//! **A question about one item this source already names by id never lists the board.**
395//! Whether that item is on this board, and what its board fields are, is answered by reading
396//! that item — its own `Issue.projectItems`, walked to exhaustion by
397//! [`GitHubProjectsSource::resolve_issue`], or a draft's own board item — and never by
398//! looking for it in [`graphql::BOARD`]'s `items` or in a listing this command already
399//! holds. That covers a write's destination, the project a new item is filed under, a
400//! same-source far end a dependency names, a status write, the dependency slot a draft keeps,
401//! and the delete that takes back an item a copy made. What such a write needs of the board
402//! and the item does not carry — the board's id, the `Status` and origin field definitions —
403//! comes from [`graphql::BOARD_FIELDS`], which reads no item at all. The reason is evidence,
404//! not economy alone: `ProjectV2.items` is a projection that lags the membership GitHub
405//! itself reports — an issue added with `addProjectV2ItemById` can be missing from it for
406//! minutes. Scanning this host's 842-item board has refused a document copy and an update
407//! even though the items' own reads named that board. A scan there gives the wrong answer
408//! as well as paying for every page. So a `board.items` lookup does not belong on any of
409//! those paths.
410//!
411//! **What a read may return is capped too, and that cap is on the document rather than on
412//! the board.** GitHub limits the number of nodes **one query may return** to
413//! [`NODE_COUNT_LIMIT`] and refuses a query above that before executing it: the answer is
414//! an error naming the connection the count crossed at, not a slow or a partial result.
415//! Every board this source reads is refused the same way, so no board is too big for these
416//! documents and none is small enough to save one that is over.
417//!
418//! The count is arithmetic over the document's own text: each connection contributes the
419//! `first:` it asks for, counts **multiply** down a nested path and **sum** across sibling
420//! paths. Those are [GitHub's published rules][node-limits] and this workspace does not
421//! restate them — `github-graphql-node-count` implements them, and
422//! [`worst_case_node_count`] under [`largest_page_sizes`] is where every node count here
423//! comes from. `every_document_this_source_sends_stays_under_githubs_node_limit`, in
424//! `tests/node_count.rs`, recomputes every document in [`graphql::DOCUMENTS`] from that
425//! same text on every run and fails naming any that reaches the limit — so a connection
426//! added to a shared fragment is caught there rather than by GitHub.
427//!
428//! What decides those counts is the page sizes: [`MAX_PAGE_SIZE`] on the outer page,
429//! `NESTED_PAGE_SIZE` on the connections hanging off one item, and
430//! `BOARD_ITEMS_PAGE_SIZE` on the page of an issue's board memberships a read carries.
431//! `$nestedFirst` is spent twice down one path of a board read, so that constant is
432//! effectively squared there, which is why it is the one the limit is most sensitive to.
433//! `BOARD_ITEMS_PAGE_SIZE` is small for a reason of its own, recorded beside it: what a
434//! page of memberships misses is recovered by one further read rather than refused, so it
435//! buys a bound every read pays for at the price of a request only a multi-board issue
436//! pays.
437//!
438//! **`nodeCount` and `cost` are two numbers against two limits, and both are computed
439//! offline here — per document, one document at a time.** `nodeCount` is the one above: the
440//! most nodes one query may return, checked per query and bounded by [`NODE_COUNT_LIMIT`].
441//! `cost` is rate-limit points, metered per hour across everything one credential does; it
442//! is what the two limiters [`Limiter`] tells apart meter, and a document under
443//! [`NODE_COUNT_LIMIT`] still says nothing about its price. [`worst_case_point_cost`] is
444//! that second number, and `tests/point_cost.rs` pins every document in
445//! [`graphql::DOCUMENTS`] at what it costs — there being no per-call point ceiling to hold
446//! one under, the pin itself is the check. The credentialed lane reconciles both figures
447//! against GitHub's own, off a probe it already sends.
448//!
449//! **What is pinned that way is a per-document price and never a session's.** The record in
450//! `session-cost.md` measures the two quantities a whole session can be counted in offline —
451//! **requests** and **worst-case nodes** — and neither is points. What one whole session
452//! consumes of the hourly point allowance is observable only from a credentialed run's own
453//! `x-ratelimit-*` headers, which is what [`accounting`] fills its per-budget figures from
454//! and what `tests/live.rs` prints at the end of every run.
455//!
456//! [node-limits]: https://docs.github.com/en/graphql/overview/rate-limits-and-node-limits-for-the-graphql-api
457//!
458//! **Where a read-after-write guarantee comes from, since neither of GitHub's two
459//! enumerations of a board can supply one alone.** Resolving a node id is strongly
460//! consistent, so a read by id and a project's own sub-issues are already current. The
461//! other two are not, and they are behind by different amounts and in different directions:
462//!
463//! - GitHub's **issue search** is an index and answers a write made moments ago with the
464//! value from before it — usually for a second or two.
465//! - **`ProjectV2.items`** is a projection GitHub rebuilds behind the write, and an item put
466//! on a board with `addProjectV2ItemById` can be **absent** from it — not present with its
467//! content withheld, absent, with the connection walked to its own `hasNextPage: false` —
468//! for *minutes*, while `Issue.projectItems` names the same membership at once.
469//!
470//! That second one is a measurement rather than a caution. This repository's own
471//! credentialed journey writes a project and waits for the board to report it, then writes a
472//! task and waits for the same thing seconds later on the same board: the project wait is
473//! answered through the search and converged in two or three attempts in each of three runs,
474//! and the task wait is answered through `ProjectV2.items` and converged in none of them
475//! inside thirty. Separately, an item added to a second and larger board was read back by
476//! `Issue.projectItems` on that board's own id while every one of that connection's nine
477//! pages, walked to exhaustion nine minutes after the add, did not name it. Reading a board
478//! through the lagging one alone is what had a board read deny an issue that had certainly
479//! landed on it.
480//!
481//! So [`GitHubProjectsSource::board`] is the **union** of both — each search result still
482//! admitted only on this board's own strongly-consistent `Issue.projectItems`, and neither
483//! enumeration dropped, because only `ProjectV2.items` lists a board draft and only the
484//! search reports what the projection is behind on. What closes the last
485//! gap, the one where both are behind, is [`GitHubProjectsSource::created`]: every read this
486//! source answers is completed with what this process itself wrote, so an item created
487//! seconds ago is reported whether or not GitHub has caught up. Nothing else is remembered,
488//! nothing is written down, and the record dies with the process. **A wait that has to
489//! observe GitHub's own data cannot be answered from that record** — which is why the
490//! credentialed journey asks through a source built afresh, and why the union above rather
491//! than a longer wait is what makes such a wait converge.
492//!
493//! **A narrowed read is the same bargain, stated for each of the three predicates it
494//! answers.** A read carrying a text, metadata or origin predicate asks GitHub's index rather
495//! than walking the board, and every such answer is completed with what this process wrote —
496//! its [`created`](GitHubProjectsSource::created) record and every existing item it wrote,
497//! each filtered by the same predicates as the rest — so an item this command wrote a moment
498//! ago is returned by a query that matches it whether or not the index has caught up. An item
499//! a caller holds the id of is read by that id, with `node(id:)`, which is strongly
500//! consistent. What is left is stated rather than papered over:
501//!
502//! | Read | Finds | Behind by |
503//! | --- | --- | --- |
504//! | text, metadata | the issue search for the phrases | what another process wrote in the last second or two, until GitHub indexes it |
505//! | origin, first read | the board's field filter over the origin field — every carrier, whichever release wrote it | what `ProjectV2.items` is behind on, which the measurements above put in minutes |
506//! | origin, second read | the issue search for the id in the body, where a write of this release mirrors it | a second or two, as any search |
507//! | origin, third read | this process's own writes | nothing |
508//!
509//! So an origin carrier another process added within the last second or two, before either
510//! index has it, can be missing from an origin query, and one written by the release before
511//! this one — its origin in the field alone — can be missing for as long as the board's own
512//! item connection is behind on it. A copy that must not duplicate its own earlier write
513//! relies on the link it records, not on either index. **A board draft is not an issue**, so
514//! a draft is never returned by a text, metadata or origin query, whatever it holds: no search
515//! lists one, the origin lookup drops any the board's own field filter names, and one this
516//! process wrote is not added back either.
517//!
518//! **The origin lives in the board field, and the body holds a mirror of it.** A write that
519//! carries an origin writes it to the `onetaskgraph.origin` text field and also into the
520//! body's metadata slot, so the issue search can find it in seconds. The field is
521//! authoritative: this source reads an item's origin from the field alone, so a slot that
522//! disagrees with it, or holds one where the field holds none, is never read as a second
523//! origin — and the release before this one reads the slot, drops that key's copy for the
524//! field's, and sees the same one origin.
525//!
526//! Filtering happens before paging, so a page of a filtered result is a page of the
527//! survivors rather than the survivors of a page. Label matching and the substring rule a
528//! text candidate is confirmed by answer the same question the same way the local Markdown
529//! source's do; which candidates a text search has to confirm is GitHub's word match, which
530//! is the one place the two sources can answer the same text differently.
531//!
532//! <!-- llmlint: ignore[contracts_have_one_source_or_a_drift_gate] The declaration itself
533//! has one source, `capabilities`, and the note above is the reasoning behind it rather
534//! than a second copy of it: without the three facts recorded here a reader takes the
535//! uniform `Native` for a lie and reverts it. The drift gate on the declaration is this
536//! crate's own capabilities test, which pins every field of it against a fully spelled-out
537//! `Capabilities` literal — a struct with no `Default`, so a field added to the contract
538//! fails to compile there rather than going unasserted. -->
539//! The fixture-server tests above run wherever this crate is selected; the credentialed
540//! lane runs in the same required check, beside them, and can fail it — it verifies the
541//! 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,
542//! one filed under neither, a label on one of the three and a closed status on another —
543//! because that shape is what tells an honoured predicate from an ignored one: a board
544//! holding a single project answers a project filter the same way whether or not this
545//! source applies it, which is exactly how the defect above went unseen.
546//!
547//! That lane writes only to the board `GH_PROJECTS_OWNER` and `GH_PROJECTS_NUMBER` name,
548//! and the scratch repository `GH_PROJECTS_REPOSITORY` names:
549//! `nickderobertis/onetaskgraph-live-scratch`. It refuses the core repository before any
550//! session or request, independently of the live demand. Fix that variable in the machine's
551//! onetaskgraph `secrets.env` or the environment it pushes from, such as ai-orchestrator's
552//! `.env`. The targets are declared once in `onetaskgraph_github_live`, the lane's own
553//! policy crate; absent credentials or nominations otherwise skip.
554//!
555//! GitHub Actions artifacts carry `ci-<run id>-<attempt>-<micros>`, naming their writing
556//! run and attempt; invalid Actions identity refuses before writing. Other runs retain
557//! `<host>-<process>-<micros>`. Own cleanup matches the whole stamp. Machine residue stays
558//! the owning machine's lock sweep's; Linear always keeps that form and is unaffected.
559//! The hourly janitor is cleanup, never a test lane: scratch CI residue is removed only
560//! after its run reads back as `completed`, immediately before the listed-artifact batch.
561//! Failed or incomplete ownership reads preserve residue. A 24-hour waiting period
562//! is a margin, never the ownership authorisation. Cleanup leaves machine stamps
563//! and the board's `onetaskgraph.origin` field untouched.
564//!
565//! # What a session of requests costs, and where the report is
566//!
567//! This source records **every** request it sends into [`accounting::Accounting`], at
568//! `send_once` — the one place a request leaves this crate, which is why a read path added
569//! later is counted without anybody remembering to count it. That is the whole of what this
570//! crate adds to the arrangement; [`accounting`] is where what a record carries, how a
571//! session's spend is arrived at, and what it deliberately does not know are set out.
572//!
573//! What one whole session of the live journey costs, counted that way against this crate's
574//! loopback fixture board, is written down in `session-cost.md` beside this crate — with the
575//! reduction it came out of, and with what it does and does not say about rate-limit points.
576//!
577//! [`GitHubProjectsSource::accounting`] is the read: a snapshot to hold and compare, which
578//! [`accounting::Session::report`] renders the session report from. It is on the ordinary
579//! code path — no environment variable, no feature, no build configuration — because an
580//! instrument nobody switches on measures nothing, and
581//! [`Plugin::build_recording_into`] is how a caller making its own calls beside this
582//! source's counts the whole session rather than this source's share. The credentialed lane
583//! in `tests/live.rs` does exactly that, and prints the report at the end of every run,
584//! passed or failed.
585//!
586//! **A live session refuses to start unless the account can afford it.** Before it does any
587//! of the work it exists to do, the journey makes one request — `GET /rate_limit`, which
588//! GitHub documents as not counting against the REST rate limit and which answers both of
589//! its budgets at once — and starts only if, for each of them, what remains minus this
590//! session's estimated cost is still at least
591//! `onetaskgraph_live::RETAINED_BUFFER` — twenty per cent — of that budget's whole
592//! allowance. A session that cannot **declines**: it did not run, so it is
593//! neither a pass nor a failing assertion, and it says which budget was short, that budget's
594//! limit, what remained, the estimate, the buffer and when it resets — then stops, without
595//! waiting for the budget to come back. The estimate is derived offline from
596//! `tests/fixtures/session-cost.txt` and a cost model stated in `tests/journey/budget.rs`,
597//! which is also where the published rule that model rests on is cited; the accounting
598//! above records the gate's own read like any other request, and
599//! [`accounting::Session::report`] prints the estimate beside what the session really spent.
600//!
601//! **GitHub is the authority on both of its own numbers, and the credentialed lane goes and
602//! asks it.** Everything above computes `nodeCount` and `cost` offline from a document's own
603//! text, which is what lets it run on every platform and on a pull request from a fork with
604//! no credential — and that is what actually stops a regression merging. But an offline
605//! arithmetic can only ever agree with itself: if GitHub changes its rules, this workspace
606//! goes on computing the old answer and nothing notices. So `tests/live.rs` reconciles them.
607//! GitHub's schema exposes `rateLimit(dryRun: true)`, whose `nodeCount` is *"the maximum
608//! number of nodes this query may return"* and whose `cost` is what that document would
609//! spend, both for a document **without executing it**, and the lane asks it for every query
610//! document this source sends, under the largest bindings this source sends, and fails when
611//! GitHub's figure and [`worst_case_node_count`] or [`worst_case_point_cost`] disagree. A
612//! mutation is skipped, because `rateLimit` is a field of `Query` and cannot be asked about
613//! one; the offline pins still cover it. It records what those calls reported about the
614//! account's own allowance, because whether asking is free is a thing to observe rather than
615//! to assume. Two quantities, not one: [`NODE_COUNT_LIMIT`] bounds `nodeCount` per query,
616//! and `cost` is metered against an hourly allowance the accounting above reads off a
617//! credentialed run's own response headers.
618//!
619//! **GitHub has two rate limiters and this source is refused by both, so nothing here
620//! treats them as one thing.** The primary budget is the hourly allowance `gh api
621//! rate_limit` reports; the secondary limiter is a burst limiter over content-generating
622//! requests, and *nothing* reports it. Which one refused decides the operator's next step,
623//! so [`Limiter`] is a type rather than a detail, and it is what [`MIN_MUTATION_INTERVAL_MS`],
624//! [`GitHubProjectsSource::board_cache`] and [`GitHubProjectsSource::graphql`] each answer
625//! one part of.
626#![deny(missing_docs)]
627
628use std::collections::BTreeMap;
629use std::sync::{Arc, Mutex};
630use std::time::Duration;
631
632use chrono::{DateTime, Utc};
633use onetaskgraph_plugin_api::{
634 Capabilities, Classification, ClosedAs, Comment, CommentBody, CommentOrigin, Completion,
635 ContradictedItem, Coverage, Cursor, DependencyEdge, DependencyEndpoint, DependencyKind,
636 DependencySupport, Designator, Direction, Document, DocumentQuery, DocumentVersionCoverage,
637 Health, HeldTask, IssueHolding, IssueRef, ItemKind, ItemWrite, Label, LabelFilter, Location,
638 MetadataKey, Metering, NativeId, NewComment, Page, PageRequest, Priority, Project,
639 ProjectFilter, ProjectQuery, ProjectVersionCoverage, Repository, SecretResolver, SharedClock,
640 SourceError, SourceName, SourcePlugin, Status, StatusCategory, StatusContradiction,
641 StatusMapping, Support, Task, TaskDetailRead, TaskQuery, TaskRef, TaskSource, TaskUpdate,
642 TaskUpdateOutcome, TextFields, TextQuery, UnmappedStatus, UpdatedField, VersionCoverage,
643 WaitCause, WriteJournal, WriteSupport, system_clock,
644};
645use reqwest::{Client, StatusCode, Url};
646use schemars::{Schema, schema_for};
647use secrecy::{ExposeSecret, SecretString};
648use serde::{Deserialize, Serialize};
649use serde_json::{Value, json};
650
651pub mod accounting;
652mod assets;
653pub mod change_token;
654mod expected;
655pub use expected::{CONTEXT_VERSION, WHOLE_MEMBERS, context_shapes};
656mod pacing;
657mod visibility;
658
659use accounting::Accounting;
660
661/// The registry name for this plugin.
662pub const KIND: &str = "github-projects";
663/// GitHub's maximum connection page size.
664pub const MAX_PAGE_SIZE: u32 = 100;
665/// Every page of a board-scoped narrowing search: 20 rows, one point of SEARCH_ISSUES, the
666/// most one point buys. GitHub prices that document by rows, so pages of 20 cost what pages
667/// of 100 cost per row, and a page of fewer than 20 costs the same one point.
668pub const SEARCH_PAGE_SIZE: u32 = 20;
669/// How many items one [`graphql::ISSUE_DETAILS`] request reads, each with the first page of
670/// its comments: the largest batch the node-count model prices at one point.
671///
672/// Each aliased item is resolved once, and what GitHub charges for it is the connections
673/// under it — its labels, its page of board memberships, the field values of each of those
674/// three memberships, and its comments: six requests' worth of the aggregate GitHub divides
675/// by a hundred and rounds. Twenty-four items come to 144, which rounds to one point;
676/// twenty-five come to 150, which rounds to two. `tests/point_cost.rs` prices the document at
677/// one point and fails if one item more would still be priced at one.
678pub const DETAIL_BATCH: usize = 24;
679/// How many issue references one [`graphql::ISSUES_BY_REFERENCE`] request reads.
680///
681/// Each reference asks GitHub for one connection — a page of its board memberships, each with
682/// its `Status` value read by name rather than through a connection of field values — so fifty
683/// come to fifty requests' worth of the aggregate GitHub divides by a hundred, which is its
684/// one-point minimum, and the batch a landing's closures are read in costs one point per fifty
685/// issues per source. `tests/point_cost.rs` prices the document at one point.
686pub const REFERENCE_BATCH: usize = 50;
687/// How many items one [`graphql::ISSUE_COPY_DETAILS`] request reads: each with the first page
688/// of its comments and its `blockedBy`, the largest batch the node-count model prices at one
689/// point.
690///
691/// The six requests' worth an item of [`DETAIL_BATCH`] costs, and its `blockedBy` connection
692/// besides: seven. Twenty-one items come to 147, which rounds to one point; twenty-two come to
693/// 154, which rounds to two. `tests/point_cost.rs` prices the document at one point and fails
694/// if one item more would still be priced at one.
695pub const COPY_DETAIL_BATCH: usize = 21;
696/// How many mutations one aliased request of a copy's creates and closes carries:
697/// [`graphql::CREATE_ISSUES`] and [`graphql::CLOSE_ISSUES`].
698///
699/// Eleven is what GitHub was seen to accept in one request — the timing spike
700/// (`board-copy-speed-2026-10-18-spike-copy-timing-report`) sent eleven aliased mutations of four
701/// kinds, three `createIssue`, three `addProjectV2ItemById`, three field updates, an
702/// `addSubIssue` and an `addBlockedBy`, and every one ran — and no more was established.
703pub const MUTATION_BATCH: usize = 11;
704/// How many aliased writes one request of a copy's board field values and blocked-by edges
705/// carries: [`graphql::WRITE_FIELD_VALUES`], [`graphql::ADD_BLOCKED_BYS`] and, for a request
706/// holding both kinds, [`graphql::WRITE_FIELDS_AND_EDGES`].
707///
708/// Nineteen is the most aliases the feasibility spike
709/// (`board-copy-gaps-2026-10-24-spike-feasibility-report`) sent in one mutation request and
710/// saw GitHub price at the one point every mutation request costs; no larger request was
711/// established, so a copy holding more is split.
712pub const WRITE_BATCH: usize = 19;
713/// How many issues one [`graphql::DELETE_ISSUES`] request deletes: one request of eleven
714/// aliased `deleteIssue` met `RESOURCE_LIMITS_EXCEEDED` from the eighth alias on, and batches
715/// of five succeeded (the feasibility spike's undo-batching run).
716pub const DELETE_BATCH: usize = 5;
717/// How many origins one [`graphql::ORIGIN_LOOKUPS`] request asks about: the nine records of a
718/// plan and the project's document, and no more, since each carries a page of the board's field
719/// filter and one of the issue search.
720pub const ORIGIN_BATCH: usize = 10;
721
722/// The most nodes any one document this source sends may be asked to return.
723///
724/// GitHub's own published per-query ceiling, taken from
725/// [`github_graphql_node_count::NODE_LIMIT`] rather than written out again here, so this
726/// workspace cannot hold a stale copy of somebody else's number. A query above it is
727/// **refused before it is executed**, whoever is asking and whatever board they are
728/// asking about — so this is a bound on the documents rather than a budget that runs out.
729///
730/// This is `nodeCount`, the maximum number of nodes *one query may return*. It is not
731/// `cost`, the rate-limit points a call spends against an hourly allowance shared by
732/// everything the credential does — two numbers against two limits, and this constant
733/// bounds only the first. The second is computed offline too, per document:
734/// [`worst_case_point_cost`], pinned for every document in [`graphql::DOCUMENTS`] by
735/// `tests/point_cost.rs`, and reconciled against GitHub's own `cost` by the credentialed
736/// lane. There is no constant like this one to hold a price under, because points are an
737/// hourly allowance rather than a per-call bound.
738///
739/// Neither is a session's price. What `session-cost.md` records of a whole session is its
740/// **requests** and its **worst-case nodes**; what a whole session spends in points is
741/// reported only by a credentialed run's own `x-ratelimit-*` headers, through
742/// [`accounting`]. The module section on the three ways this source reaches an item says how
743/// the count is arrived at, and which of the page sizes below decide it.
744pub const NODE_COUNT_LIMIT: u64 = github_graphql_node_count::NODE_LIMIT;
745
746/// Nested connection size for the connections that hang off one item.
747///
748/// It multiplies through every document that reaches an item under a page — the count
749/// rules multiply down a nested path — so it is the constant [`NODE_COUNT_LIMIT`] is most
750/// sensitive to. `tests/node_count.rs` is what holds the pair together: it recomputes
751/// every document under these constants and fails naming any that reaches the limit, so
752/// raising this is caught there rather than by GitHub.
753const NESTED_PAGE_SIZE: u32 = 50;
754/// How many of one issue's board memberships are read when an issue is reached directly.
755///
756/// An issue reached through a search or through its own node id carries its board half in
757/// `Issue.projectItems`, and only the entry for *this* board is read. This connection sits
758/// under a page of issues, so every point of it multiplies through the whole document and
759/// is paid for whether or not any issue is on a second board — which is why it is
760/// deliberately far smaller than [`NESTED_PAGE_SIZE`].
761///
762/// **Three, because what a page misses is now recovered rather than refused**, and the
763/// recovery is what the value is chosen against. An issue whose entry for this board sits
764/// past this page costs one further request — [`graphql::ISSUE_BOARD_ITEMS`], resumed from
765/// that page's own cursor — so the value trades a bound every read pays for a request only
766/// a multi-board issue pays. At one, a deployment whose issues commonly sit on two or more
767/// boards would pay that request *per issue*, which is order N against the one page per
768/// hundred issues a read costs today. At three it is only reached by an issue on four or
769/// more boards at once, which keeps the recovery path exceptional rather than routine for
770/// a plausible deployment.
771const BOARD_ITEMS_PAGE_SIZE: u32 = 3;
772/// How many carriers of one copy origin one page of [`graphql::ORIGIN_LOOKUP`] asks each of
773/// its two connections for.
774///
775/// An origin names one item, so the answer an origin lookup expects is one carrier, and a
776/// second is a duplicate a copy already takes the first of. Both connections are walked to
777/// exhaustion whatever this is, so it decides how many requests an unusual answer costs and
778/// never what the answer is. It is small because every point of it is paid on every lookup,
779/// and a copy makes one lookup per item it has no link for: at three, ten lookups cost fewer
780/// worst-case nodes than the one whole-board read they replaced.
781const ORIGIN_PAGE_SIZE: u32 = 3;
782/// How many carriers of each origin the batched [`graphql::ORIGIN_LOOKUPS`] asks each of its two
783/// connections for: one, the one carrier an origin names.
784///
785/// A second carrier still shows: the page then says there is more, and that origin is left to
786/// its own [`graphql::ORIGIN_LOOKUP`], which walks every page at `ORIGIN_PAGE_SIZE` — so a
787/// duplicate is found exactly as before, by the lookup that reads all of them. The feasibility
788/// spike (`board-copy-gaps-2026-10-24-spike-feasibility-report`) priced the batch of ten
789/// origins at two points with a first page of three and at one with a first page of one.
790const ORIGIN_LOOKUPS_PAGE_SIZE: u32 = 1;
791
792/// How many of one project's sub-issues one [`graphql::SUB_ISSUES`] page asks for: the most
793/// one point buys, since every row carries six connections GitHub charges for once per row.
794/// `session-cost.md` derives it; `tests/point_cost.rs` prices the document at one point and
795/// fails if one row more would still be priced at one.
796pub const MEMBER_PAGE_SIZE: u32 = 23;
797
798pub use github_graphql_node_count::{NodeCountError, Variables};
799
800/// The largest value this source can bind to each page-size variable its documents name.
801///
802/// Every `first:` in [`graphql`] reads one of these six, and each is capped at the
803/// constant above it wherever a caller's own limit could reach it — `$first` at
804/// [`MAX_PAGE_SIZE`], `$nestedFirst` at `NESTED_PAGE_SIZE`, `$boardItems` at
805/// `BOARD_ITEMS_PAGE_SIZE`, `$originFirst` at `ORIGIN_PAGE_SIZE`, `$lookupFirst` at
806/// `ORIGIN_LOOKUPS_PAGE_SIZE`, `$memberFirst` at [`MEMBER_PAGE_SIZE`]. So this is the worst case a caller can drive this source to,
807/// not one configuration of it, which is what makes a bound computed under it a bound on
808/// every read.
809pub fn largest_page_sizes() -> Variables {
810 Variables::from([
811 ("first".to_owned(), MAX_PAGE_SIZE),
812 ("nestedFirst".to_owned(), NESTED_PAGE_SIZE),
813 ("boardItems".to_owned(), BOARD_ITEMS_PAGE_SIZE),
814 ("originFirst".to_owned(), ORIGIN_PAGE_SIZE),
815 ("lookupFirst".to_owned(), ORIGIN_LOOKUPS_PAGE_SIZE),
816 ("memberFirst".to_owned(), MEMBER_PAGE_SIZE),
817 ])
818}
819
820/// The most nodes `document` could be asked to return, by GitHub's published rules.
821///
822/// Computed offline from the document's own text under [`largest_page_sizes`] — no
823/// network, no credential and no schema — by
824/// [`github_graphql_node_count::node_count`], which is where the rules themselves live.
825/// A document at or above [`NODE_COUNT_LIMIT`] is one GitHub refuses before executing, so
826/// this is what a check holds every document in [`graphql::DOCUMENTS`] below.
827///
828/// # Errors
829///
830/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
831/// no single operation, or binds a page size this source does not name — each of which is
832/// a defect in the document rather than a number.
833pub fn worst_case_node_count(document: &str) -> Result<u64, NodeCountError> {
834 node_count(document, &largest_page_sizes())
835}
836
837/// The most rate-limit points one call of `document` could spend, by GitHub's published
838/// rules.
839///
840/// Computed offline from the document's own text under [`largest_page_sizes`] — no
841/// network, no credential and no schema — by
842/// [`github_graphql_node_count::point_cost`], which is where the rules themselves live.
843/// This is `cost`, metered **per hour** against the allowance one credential shares across
844/// everything it does; it is not `nodeCount`, which is [`worst_case_node_count`] and is
845/// bounded per query by [`NODE_COUNT_LIMIT`]. There is no per-call ceiling to hold this
846/// under, so what `tests/point_cost.rs` does with it is pin every document in
847/// [`graphql::DOCUMENTS`] at what it costs, and the credentialed lane reconciles those
848/// figures against GitHub's own reported `cost`.
849///
850/// # Errors
851///
852/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
853/// no single operation, or binds a page size this source does not name — each of which is
854/// a defect in the document rather than a number.
855pub fn worst_case_point_cost(document: &str) -> Result<u64, NodeCountError> {
856 github_graphql_node_count::point_cost(document, &largest_page_sizes())
857}
858
859/// The most nodes `document` could be asked to return under `variables`.
860///
861/// [`worst_case_node_count`] is this under [`largest_page_sizes`], and the accounting in
862/// [`accounting`] is this under the bindings one request really sent — one spelling of the
863/// calculation, so a bound checked offline and a cost recorded at run time cannot come to
864/// disagree. The rules themselves live in [`github_graphql_node_count::node_count`].
865///
866/// # Errors
867///
868/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
869/// no single operation, or binds a page size `variables` does not name.
870pub fn node_count(document: &str, variables: &Variables) -> Result<u64, NodeCountError> {
871 github_graphql_node_count::node_count(document, variables)
872}
873
874/// The issue-title prefix that makes a board issue a document.
875///
876/// A GitHub Projects board has no document type — it holds issues — so the discriminator
877/// is the title, and this is the whole of it: an issue whose title begins with these bytes
878/// is a document and every other issue is the task or project the sub-issue rule makes it.
879///
880/// It is spelled **once**, here, and read rather than restated everywhere else — including
881/// by the shared journeys, which take it from this constant so a board fixture cannot
882/// drift from what this source reads. `docs/metadata.md` records the two consequences that
883/// are not obvious from the bytes: the reported title has this prefix taken off, exactly
884/// as the body's metadata slot is taken off `content`, and this prefix is read *before*
885/// the sub-issue rule, so a design issue with no sub-issues is never an empty project.
886pub const DESIGN_TITLE_PREFIX: &str = "DESIGN: ";
887
888/// Exact GraphQL query documents issued by this plugin.
889///
890/// Keeping the production documents here lets the pinned-schema test validate the same
891/// bytes that are sent to GitHub, rather than a test-only copy which could drift
892/// independently. [`STATUS_OPTIONS_UPDATE`] is the sole document that may rewrite a board
893/// field, and its guarded caller always supplies the complete existing option set with ids.
894pub mod graphql {
895 /// The board half of one item: the field values every document here reads it from.
896 ///
897 /// A macro for the same reason [`board_issue!`] below is one, a level further in. This
898 /// selection is needed by that fragment, by [`BOARD`] under the board's own `items`,
899 /// and by [`ISSUE_BOARD_ITEMS`] under a membership walk — and all three have to produce
900 /// *the same value*, because
901 /// [`GitHubProjectsSource::resolve`](super::GitHubProjectsSource) reads them through
902 /// one path. Three spellings of it is what would drift, so there is one.
903 ///
904 /// The `Status` option and this source's own origin text field are the whole of it. It
905 /// selects no `ProjectV2ItemFieldLabelValue`: GitHub derives that field from the item's
906 /// content, so it holds nothing the content's own `labels` do not already say, and it
907 /// would sit a label connection two page sizes deep.
908 macro_rules! board_item_values {
909 () => {
910 r#"fieldValues(first:$nestedFirst){nodes{
911 ... on ProjectV2ItemFieldSingleSelectValue{name field{
912 ... on ProjectV2SingleSelectField{id name options{id name}}
913 }}
914 ... on ProjectV2ItemFieldTextValue{text field{... on ProjectV2Field{id name}}}
915 }pageInfo{hasNextPage}}"#
916 };
917 }
918
919 /// The variable every document reading an issue declares for the relationship events its
920 /// change token counts, with its value as the default, so no caller binds it.
921 ///
922 /// A variable rather than a literal list because every argument a production document
923 /// passes is a declared variable, which `tests/schema.rs` holds them to, and every member of
924 /// its default to the pinned enum. Its default is the six event types GitHub records a
925 /// relationship added or removed with: a dependency on either end, as `blockedBy` and
926 /// `blocking`, so one a person makes from the far end moves this end's count too; and a
927 /// project membership on the task, as its parent, which GitHub records there whichever
928 /// issue the move was made from. A sub-issue's events on its parent are not counted: a
929 /// project carries no task token, and a copy writes nothing of a task its own sub-issues
930 /// decide. [`super::change_token`] says why the count is in the token.
931 macro_rules! relationship_events_variable {
932 () => {
933 ",$relationshipEvents:[IssueTimelineItemsItemType!]=[BLOCKED_BY_ADDED_EVENT,BLOCKED_BY_REMOVED_EVENT,BLOCKING_ADDED_EVENT,BLOCKING_REMOVED_EVENT,PARENT_ISSUE_ADDED_EVENT,PARENT_ISSUE_REMOVED_EVENT]"
934 };
935 }
936
937 /// Everything this source reads about one issue, wherever it reaches that issue.
938 ///
939 /// A macro rather than a constant so the three documents below can `concat!` it: one
940 /// spelling of these fields is what makes an issue read through the board-scoped
941 /// search, through its own node id, and through its project's sub-issue relationship
942 /// resolve to *the same* item, which is the whole of what
943 /// [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource) relies on.
944 ///
945 /// `projectItems` is what carries the board half of an issue: the board item's own id
946 /// and the [`board_item_values!`] above — the `Status` option and this source's origin
947 /// text field — that a `ProjectV2.items` read used to carry. It is asked for on the
948 /// issue rather than on the board, which is what makes the cost of a read proportional
949 /// to what was asked for instead of to the board's size.
950 ///
951 /// It carries a *page* of that connection, at `BOARD_ITEMS_PAGE_SIZE`, and its
952 /// `endCursor` is what [`ISSUE_BOARD_ITEMS`] resumes from when this board's entry is
953 /// not on that page: a page here is where the search for the entry starts rather than
954 /// where it ends.
955 ///
956 /// It does **not** select the board's `Labels` field value, and that is the whole of
957 /// what keeps the three documents below under [`NODE_COUNT_LIMIT`](super::NODE_COUNT_LIMIT):
958 /// a label connection there sits under `fieldValues` under `projectItems` under a page
959 /// of issues, spending `$nestedFirst` twice down one path, and took
960 /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] to 2,556,100 nodes against a limit of 500,000.
961 /// No label is lost — this is a fragment `on Issue`, whose own `labels` are selected
962 /// above, and that connection is where every label this source reports comes from. No
963 /// document in this module selects the board field any longer, [`BOARD`] included; the
964 /// module documentation records why nothing it could have held is lost.
965 macro_rules! board_issue {
966 () => {
967 concat!(
968 r#" fragment BoardIssue on Issue{__typename id number title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total} commentCount:comments{totalCount} relationshipEvents:timelineItems(itemTypes:$relationshipEvents){totalCount}
969 labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}
970 projectItems(first:$boardItems){nodes{id updatedAt project{id number}
971 "#,
972 board_item_values!(),
973 r#"}pageInfo{hasNextPage endCursor}}}"#
974 )
975 };
976 }
977
978 /// Every issue of one board, found by a search scoped to that board.
979 ///
980 /// This is how the projects a board holds are listed, and it selects no `items`
981 /// connection on `ProjectV2`: the board is a *qualifier of the search* rather than a
982 /// container walked page by page, so nothing nested inside a board item is paid for.
983 /// Which of the issues it returns is a project is then read off `parent` — GitHub
984 /// accepts `-has:parent` as a search qualifier and silently ignores it, so the
985 /// discriminator has to be applied to the field, which is a scalar on the issue and
986 /// costs nothing.
987 pub const SEARCH_ISSUES: &str = concat!(
988 r#"query($search:String!,$type:SearchType!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
989 relationship_events_variable!(),
990 r#"){
991 search(query:$search,type:$type,first:$first,after:$after){
992 pageInfo{hasNextPage endCursor}
993 nodes{__typename ...BoardIssue}
994 }
995 }"#,
996 board_issue!()
997 );
998
999 /// What a dependency read selects of each far end: enough to say which kind of item it
1000 /// is, its body included for the kind marker.
1001 macro_rules! related_issue {
1002 () => {
1003 " fragment Related on Issue{id title body parent{id} subIssuesSummary{total}}"
1004 };
1005 }
1006
1007 /// One issue by its own node id, which is what a qualified id names here — with what a
1008 /// write of it needs and the issue does not carry in `board_issue!`: the field
1009 /// definitions of the boards it sits on, and the far ends of its `blockedBy`.
1010 ///
1011 /// Strongly consistent, unlike the search above: GitHub's issue search is an index and
1012 /// answers a write made moments ago with the value from before it, and resolving a node
1013 /// id does not.
1014 ///
1015 /// **Why those two ride here and not on the fragment.** A copy or an update of an item
1016 /// reads it by its own id, and with them that one read answers everything the write
1017 /// needs: which option ids the board's `Status` and `Priority` fields hold — so no
1018 /// [`BOARD_FIELDS`] — and which issues block it, with each one's kind — so no
1019 /// [`ISSUE_DEPENDENCIES`]. On `board_issue!` they would sit under the hundred-issue
1020 /// pages of [`SEARCH_ISSUES`] and [`SUB_ISSUES`], multiplying both documents' price. Here
1021 /// they sit under one item, and this read is still one point.
1022 pub const ISSUE: &str = concat!(
1023 r#"query($id:ID!,$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1024 relationship_events_variable!(),
1025 r#"){
1026 node(id:$id){__typename ...BoardIssue ... on Issue{
1027 boards:projectItems(first:$boardItems){nodes{project{id number fields(first:$nestedFirst){nodes{
1028 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1029 ... on ProjectV2Field{__typename id name dataType}
1030 }pageInfo{hasNextPage}}}}}
1031 blockedBy(first:$first){nodes{...Related}pageInfo{hasNextPage endCursor}}
1032 }}
1033 }"#,
1034 board_issue!(),
1035 related_issue!()
1036 );
1037
1038 /// One document by its own node id, as [`ISSUE`] reads one, with the project it is filed
1039 /// under and that project's members beside it: the project issue whole, and a page of its
1040 /// sub-issues, each as [`SUB_ISSUES`] carries one but with no page of what blocks it.
1041 ///
1042 /// What a guarded write of a document's metadata reads, and what reading a document by its
1043 /// id is: a write to a board the boundary screens is held to who may read the document,
1044 /// which is the project's and every member's classification as much as its own, and here
1045 /// the one request that holds the version the write is compared against answers all of it.
1046 /// Its members carry no edges, which nothing classifying them reads.
1047 pub const DOCUMENT_IN_PROJECT: &str = concat!(
1048 r#"query($id:ID!,$first:Int!,$memberFirst:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1049 relationship_events_variable!(),
1050 r#"){
1051 node(id:$id){__typename ...BoardIssue ... on Issue{
1052 boards:projectItems(first:$boardItems){nodes{project{id number fields(first:$nestedFirst){nodes{
1053 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1054 ... on ProjectV2Field{__typename id name dataType}
1055 }pageInfo{hasNextPage}}}}}
1056 blockedBy(first:$first){nodes{...Related}pageInfo{hasNextPage endCursor}}
1057 project:parent{__typename ...BoardIssue
1058 subIssues(first:$memberFirst){pageInfo{hasNextPage endCursor} nodes{__typename ...BoardIssue}}
1059 }
1060 }}
1061 }"#,
1062 board_issue!(),
1063 related_issue!()
1064 );
1065
1066 /// One project's tasks: the sub-issues of the issue that project is, each with a page of
1067 /// what blocks it.
1068 ///
1069 /// Paged at `$memberFirst` — [`MEMBER_PAGE_SIZE`](super::MEMBER_PAGE_SIZE), the most rows
1070 /// one point buys — rather than at `$first`, because every row carries six connections and
1071 /// a hundred-row page cost six points to list a project of ten.
1072 ///
1073 /// The work this costs is the project's own size. Nothing about it grows as the board
1074 /// gains projects, or as those projects gain tasks.
1075 ///
1076 /// **Why `blockedBy` rides here and on no other page of issues.** What reads a project's
1077 /// tasks reads their edges next — `project graph` draws them, a copy carries them — and
1078 /// without them here that is one [`ISSUE_DEPENDENCIES`] per task, so the requests a graph
1079 /// costs grow with its tasks rather than with the pages of them. Carried here, a task
1080 /// blocked by no more than `$nestedFirst` issues answers its forward edges from this read,
1081 /// exactly as an [`ISSUE`] read of it does, and only one blocked by more is asked again.
1082 /// It adds a connection under each issue of the page — one rate-limit point per page,
1083 /// and `$nestedFirst` nodes per issue — and is kept off [`SEARCH_ISSUES`], whose pages
1084 /// answer questions that never read an edge.
1085 ///
1086 /// **The project issue itself rides here too**, with the page of what blocks it, because
1087 /// the reader of a project's tasks reads the project next: a copy writing the project reads
1088 /// it and its edges before it writes them. Carried on the request that already names the
1089 /// project, the first page answers that read and the dependency read after it, at the
1090 /// state GitHub answered in this command, rather than spending one more read on an item
1091 /// the cache's token cannot vouch for whole. Each sub-issue is kept the same way, so a
1092 /// copy's read of a task this listing carried is answered by it rather than sent again.
1093 /// Only a copy's read-before-write takes them; every other read by id reads GitHub.
1094 pub const SUB_ISSUES: &str = concat!(
1095 r#"query($id:ID!,$memberFirst:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1096 relationship_events_variable!(),
1097 r#"){
1098 node(id:$id){__typename ...BoardIssue
1099 ... on Issue{blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}
1100 subIssues(first:$memberFirst,after:$after){
1101 pageInfo{hasNextPage endCursor}
1102 nodes{__typename ...BoardIssue ... on Issue{
1103 blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}
1104 }}
1105 }}}
1106 }"#,
1107 board_issue!(),
1108 related_issue!()
1109 );
1110
1111 /// What a read of the board's own `items` selects of each item's content.
1112 ///
1113 /// A macro for the reason [`board_item_values!`] is one: [`BOARD`] and [`ORIGIN_LOOKUP`]
1114 /// both walk `ProjectV2.items` and hand each item to one resolver, so they select its
1115 /// content by one spelling.
1116 macro_rules! board_item_content {
1117 () => {
1118 r#" content{
1119 ... on Issue{__typename id number title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total} commentCount:comments{totalCount} relationshipEvents:timelineItems(itemTypes:$relationshipEvents){totalCount} labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}}
1120 ... on PullRequest{__typename id}
1121 ... on DraftIssue{__typename id title body createdAt updatedAt}
1122 }"#
1123 };
1124 }
1125
1126 /// Reads the board's fields and one page of its items.
1127 pub const BOARD: &str = concat!(
1128 r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$nestedFirst:Int!,$duplicates:Boolean!"#,
1129 relationship_events_variable!(),
1130 r#"){
1131 owner:repositoryOwner(login:$owner){
1132 ... on ProjectV2Owner{projectV2(number:$number){...Board}}
1133 }
1134 } fragment Board on ProjectV2 { id title
1135 fields(first:$nestedFirst){nodes{
1136 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1137 ... on ProjectV2Field{__typename id name dataType}
1138 }pageInfo{hasNextPage}}
1139 items(first:$first,after:$after){nodes{id updatedAt "#,
1140 board_item_values!(),
1141 board_item_content!(),
1142 r#"} pageInfo{hasNextPage endCursor}}
1143 }"#
1144 );
1145
1146 /// Every carrier of one copy origin, by two reads in one request, and nothing else of
1147 /// the board.
1148 ///
1149 /// **`originItems`** is the board's own items narrowed by its own field filter —
1150 /// `ProjectV2.items(query:)`, which GitHub's schema declares as "Search query for
1151 /// filtering items" — to those whose `onetaskgraph.origin` text field holds the
1152 /// qualified id, quoted. It reads the field every carrier already holds, whichever release
1153 /// wrote it, and matches it exactly: measured on 2026-09-29 against a 394-item board,
1154 /// the quoted, the unquoted and the bare-value spellings each returned exactly the one
1155 /// carrier and a prefix of the value returned none. It is `ProjectV2.items`, so it lags a
1156 /// fresh `addProjectV2ItemById` the way that connection does.
1157 ///
1158 /// **`search`** is the board-scoped issue search for the same id as a quoted phrase in
1159 /// the body, which is where this source mirrors the origin into its metadata slot. GitHub
1160 /// indexes that comment, and the index catches up with a write in a second or two rather
1161 /// than in minutes, so it finds a carrier another process wrote that the first read is
1162 /// still behind on.
1163 ///
1164 /// Each connection pages at `$originFirst`, its own small size — see `ORIGIN_PAGE_SIZE`
1165 /// — and resumes from its own cursor; a connection already walked to its end is resumed
1166 /// from its last cursor, which answers an empty page. Every candidate either read returns
1167 /// is confirmed against its own origin field before it is reported, so a token match of
1168 /// the search or anything else the filter admits never is.
1169 ///
1170 /// The root is aliased `originItems` rather than `owner`, so nothing counting the board's
1171 /// own whole reads counts this one among them.
1172 pub const ORIGIN_LOOKUP: &str = concat!(
1173 r#"query($owner:String!,$number:Int!,$filter:String!,$search:String!,$type:SearchType!,$originFirst:Int!,$itemsAfter:String,$searchAfter:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1174 relationship_events_variable!(),
1175 r#"){
1176 originItems:repositoryOwner(login:$owner){
1177 ... on ProjectV2Owner{projectV2(number:$number){
1178 items(first:$originFirst,after:$itemsAfter,query:$filter){nodes{id updatedAt "#,
1179 board_item_values!(),
1180 board_item_content!(),
1181 r#"} pageInfo{hasNextPage endCursor}}
1182 }}
1183 }
1184 search(query:$search,type:$type,first:$originFirst,after:$searchAfter){
1185 pageInfo{hasNextPage endCursor}
1186 nodes{__typename ...BoardIssue}
1187 }
1188 }"#,
1189 board_issue!()
1190 );
1191
1192 /// Every closed item of the board, by the board's own field filter — what
1193 /// `task status reconcile` reads, and nothing else of the board.
1194 ///
1195 /// **`ProjectV2.items(query:)` narrowed by `$filter`**, which this source binds to
1196 /// `is:closed` alone, so a board whose open items are most of it is not read whole to find
1197 /// the closed ones. It is narrowed rather than trusted: every item it answers is confirmed
1198 /// closed, with a reason and a card that contradict each other, from the item node it
1199 /// already carries, so a filter that also let open items through would cost reads and
1200 /// write nothing wrong.
1201 ///
1202 /// The board's own id and field definitions ride along, as [`BOARD`] carries them, so the
1203 /// option writes that follow need no read of the board. The root is aliased `closedItems`
1204 /// rather than `owner`, so nothing counting the board's own whole reads counts this one
1205 /// among them.
1206 pub const CLOSED_ITEMS: &str = concat!(
1207 r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$filter:String!,$nestedFirst:Int!,$duplicates:Boolean!"#,
1208 relationship_events_variable!(),
1209 r#"){
1210 closedItems:repositoryOwner(login:$owner){
1211 ... on ProjectV2Owner{projectV2(number:$number){id
1212 fields(first:$nestedFirst){nodes{
1213 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1214 ... on ProjectV2Field{__typename id name dataType}
1215 }pageInfo{hasNextPage}}
1216 items(first:$first,after:$after,query:$filter){nodes{id updatedAt "#,
1217 board_item_values!(),
1218 board_item_content!(),
1219 r#"} pageInfo{hasNextPage endCursor}}
1220 }}
1221 }
1222 }"#
1223 );
1224
1225 /// One alias of [`ISSUES_BY_REFERENCE`]: the issue `$owner<n>/$name<n>#$number<n>` names.
1226 macro_rules! reference_alias {
1227 ($n:literal) => {
1228 concat!(
1229 "\n r",
1230 stringify!($n),
1231 ":repository(owner:$owner",
1232 stringify!($n),
1233 ",name:$name",
1234 stringify!($n),
1235 "){issue(number:$number",
1236 stringify!($n),
1237 "){...HeldIssue}}"
1238 )
1239 };
1240 }
1241
1242 /// [`REFERENCE_BATCH`](super::REFERENCE_BATCH) issues at once, each by its repository and
1243 /// number — the one read `task status complete` makes of a source, and nothing else of
1244 /// the board: no `items`, no search and no project read.
1245 ///
1246 /// **Its own fragment, lighter than `board_issue!`, and that is what makes fifty
1247 /// references one point.** It selects no labels, and of each board membership only the
1248 /// `Status` value, by name, with `fieldValueByName` — so each alias asks GitHub for one
1249 /// connection, its page of memberships, where the whole fragment asks for five. What a
1250 /// completion writes is a card and, at most, the issue's state, so the labels and the other
1251 /// field values it leaves out are never what it needs; an item read this way is held for
1252 /// that write alone and never answers another read. An issue whose entry for this board is
1253 /// past its first page of memberships is walked with [`ISSUE_BOARD_ITEMS`], exactly as
1254 /// every other read of an issue recovers one.
1255 ///
1256 /// Fixed-size, as [`ISSUE_DETAILS`] is, so there is one document to price: a batch of
1257 /// fewer references binds the slots it has none for to its last one.
1258 pub const ISSUES_BY_REFERENCE: &str = concat!(
1259 r#"query($owner0:String!,$name0:String!,$number0:Int!,$owner1:String!,$name1:String!,$number1:Int!,$owner2:String!,$name2:String!,$number2:Int!,$owner3:String!,$name3:String!,$number3:Int!,$owner4:String!,$name4:String!,$number4:Int!,$owner5:String!,$name5:String!,$number5:Int!,$owner6:String!,$name6:String!,$number6:Int!,$owner7:String!,$name7:String!,$number7:Int!,$owner8:String!,$name8:String!,$number8:Int!,$owner9:String!,$name9:String!,$number9:Int!,$owner10:String!,$name10:String!,$number10:Int!,$owner11:String!,$name11:String!,$number11:Int!,$owner12:String!,$name12:String!,$number12:Int!,$owner13:String!,$name13:String!,$number13:Int!,$owner14:String!,$name14:String!,$number14:Int!,$owner15:String!,$name15:String!,$number15:Int!,$owner16:String!,$name16:String!,$number16:Int!,$owner17:String!,$name17:String!,$number17:Int!,$owner18:String!,$name18:String!,$number18:Int!,$owner19:String!,$name19:String!,$number19:Int!,$owner20:String!,$name20:String!,$number20:Int!,$owner21:String!,$name21:String!,$number21:Int!,$owner22:String!,$name22:String!,$number22:Int!,$owner23:String!,$name23:String!,$number23:Int!,$owner24:String!,$name24:String!,$number24:Int!,$owner25:String!,$name25:String!,$number25:Int!,$owner26:String!,$name26:String!,$number26:Int!,$owner27:String!,$name27:String!,$number27:Int!,$owner28:String!,$name28:String!,$number28:Int!,$owner29:String!,$name29:String!,$number29:Int!,$owner30:String!,$name30:String!,$number30:Int!,$owner31:String!,$name31:String!,$number31:Int!,$owner32:String!,$name32:String!,$number32:Int!,$owner33:String!,$name33:String!,$number33:Int!,$owner34:String!,$name34:String!,$number34:Int!,$owner35:String!,$name35:String!,$number35:Int!,$owner36:String!,$name36:String!,$number36:Int!,$owner37:String!,$name37:String!,$number37:Int!,$owner38:String!,$name38:String!,$number38:Int!,$owner39:String!,$name39:String!,$number39:Int!,$owner40:String!,$name40:String!,$number40:Int!,$owner41:String!,$name41:String!,$number41:Int!,$owner42:String!,$name42:String!,$number42:Int!,$owner43:String!,$name43:String!,$number43:Int!,$owner44:String!,$name44:String!,$number44:Int!,$owner45:String!,$name45:String!,$number45:Int!,$owner46:String!,$name46:String!,$number46:Int!,$owner47:String!,$name47:String!,$number47:Int!,$owner48:String!,$name48:String!,$number48:Int!,$owner49:String!,$name49:String!,$number49:Int!,$boardItems:Int!,$statusField:String!,$duplicates:Boolean!"#,
1260 relationship_events_variable!(),
1261 r#"){"#,
1262 reference_alias!(0),
1263 reference_alias!(1),
1264 reference_alias!(2),
1265 reference_alias!(3),
1266 reference_alias!(4),
1267 reference_alias!(5),
1268 reference_alias!(6),
1269 reference_alias!(7),
1270 reference_alias!(8),
1271 reference_alias!(9),
1272 reference_alias!(10),
1273 reference_alias!(11),
1274 reference_alias!(12),
1275 reference_alias!(13),
1276 reference_alias!(14),
1277 reference_alias!(15),
1278 reference_alias!(16),
1279 reference_alias!(17),
1280 reference_alias!(18),
1281 reference_alias!(19),
1282 reference_alias!(20),
1283 reference_alias!(21),
1284 reference_alias!(22),
1285 reference_alias!(23),
1286 reference_alias!(24),
1287 reference_alias!(25),
1288 reference_alias!(26),
1289 reference_alias!(27),
1290 reference_alias!(28),
1291 reference_alias!(29),
1292 reference_alias!(30),
1293 reference_alias!(31),
1294 reference_alias!(32),
1295 reference_alias!(33),
1296 reference_alias!(34),
1297 reference_alias!(35),
1298 reference_alias!(36),
1299 reference_alias!(37),
1300 reference_alias!(38),
1301 reference_alias!(39),
1302 reference_alias!(40),
1303 reference_alias!(41),
1304 reference_alias!(42),
1305 reference_alias!(43),
1306 reference_alias!(44),
1307 reference_alias!(45),
1308 reference_alias!(46),
1309 reference_alias!(47),
1310 reference_alias!(48),
1311 reference_alias!(49),
1312 "\n }",
1313 r#" fragment HeldIssue on Issue{__typename id number title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total} commentCount:comments{totalCount} relationshipEvents:timelineItems(itemTypes:$relationshipEvents){totalCount}
1314 projectItems(first:$boardItems){nodes{id updatedAt project{id number}
1315 status:fieldValueByName(name:$statusField){... on ProjectV2ItemFieldSingleSelectValue{name field{
1316 ... on ProjectV2SingleSelectField{id name options{id name}}
1317 }}}
1318 }pageInfo{hasNextPage endCursor}}}"#
1319 );
1320
1321 /// The board's own id and field definitions, and not one of its items.
1322 ///
1323 /// What a write needs of the board when the item it writes does not say: the id a field
1324 /// write and `addProjectV2ItemById` address, and the definitions of the `Status` and
1325 /// origin fields. It selects no `items`, so what it costs is the board's field list
1326 /// however many items the board holds — and it decides nothing about which items those
1327 /// are, which is the question a read of one item by its own id answers instead.
1328 ///
1329 /// The root is aliased `boardFields` rather than `owner`, so nothing counting the
1330 /// board's item reads by their root counts this one among them.
1331 pub const BOARD_FIELDS: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!){
1332 boardFields:repositoryOwner(login:$owner){
1333 ... on ProjectV2Owner{projectV2(number:$number){id
1334 fields(first:$nestedFirst){nodes{
1335 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1336 ... on ProjectV2Field{__typename id name dataType}
1337 }pageInfo{hasNextPage}}
1338 }}
1339 }
1340 }"#;
1341
1342 /// One board draft by its own node id, with the board item it sits in.
1343 ///
1344 /// A draft is not an issue, so [`ISSUE`] reaches it and reads nothing of it; this is the
1345 /// second read that answers it. `DraftIssue.projectV2Items` names the board item a draft
1346 /// is — GitHub links a draft to one item — with the same [`board_item_values!`] the
1347 /// issue fragment reads, so a draft reached by id resolves through the same resolver a
1348 /// board listing hands it to, and nothing has to list the board to find one.
1349 pub const DRAFT: &str = concat!(
1350 r#"query($id:ID!,$nestedFirst:Int!,$boardItems:Int!){
1351 node(id:$id){__typename ... on DraftIssue{id title body createdAt updatedAt
1352 projectV2Items(first:$boardItems){nodes{id project{id number}
1353 "#,
1354 board_item_values!(),
1355 r#"}pageInfo{hasNextPage endCursor}}}}
1356 }"#
1357 );
1358
1359 /// One issue's board memberships alone, walked past the page a read of it carried.
1360 ///
1361 /// The recovery read behind [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource):
1362 /// every document above carries a *page* of `Issue.projectItems`, and an issue on more
1363 /// boards than that page holds may have this board's entry past its end. This asks that
1364 /// one issue for its memberships and nothing else — the caller already holds the issue —
1365 /// so an answer of "this board does not hold it" is only ever given about a connection
1366 /// read to exhaustion.
1367 ///
1368 /// It selects the board item's id, its project number and the same
1369 /// [`board_item_values!`] the fragment does, because what it produces is handed to the
1370 /// very same resolver: an issue recovered this way reports the same title, the same
1371 /// status, the same labels and the same qualified id as one whose entry was on the
1372 /// page.
1373 ///
1374 /// `$first` rather than `$boardItems`: this document reads one issue, so nothing
1375 /// multiplies through it and the membership connection can be walked at
1376 /// [`MAX_PAGE_SIZE`](super::MAX_PAGE_SIZE) — which is what keeps the recovery to one
1377 /// further request for any issue a person really keeps.
1378 pub const ISSUE_BOARD_ITEMS: &str = concat!(
1379 r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!){
1380 node(id:$id){
1381 ... on Issue{projectItems(first:$first,after:$after){
1382 nodes{id updatedAt project{id number}
1383 "#,
1384 board_item_values!(),
1385 r#"}
1386 pageInfo{hasNextPage endCursor}}}
1387 }
1388 }"#
1389 );
1390 /// Whether the board's Project is public — half of what decides whether a write here can
1391 /// be read by anybody. Needs the `read:project` scope.
1392 pub const PROJECT_VISIBILITY: &str = r#"query($owner:String!,$number:Int!){visibility:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){public}}}}"#;
1393 /// Resolves the configured repository's node id, which creating an issue requires.
1394 pub const REPOSITORY: &str = r#"query($owner:String!,$name:String!){repository(owner:$owner,name:$name){id nameWithOwner}}"#;
1395 /// What creating an issue needs and has not read yet: the board's own id and field
1396 /// definitions, as [`BOARD_FIELDS`] reads them, and the node id of the repository the
1397 /// issue is created in, as [`REPOSITORY`] reads it — in one request.
1398 ///
1399 /// Sent at the point a create knows which repository it is for, when neither half is
1400 /// already known to this process; a create needing only one of them sends that one's own
1401 /// document. Neither half is kept past the process: a field's option ids are re-minted by
1402 /// `sources fields --apply`, so a copy of them held between runs would write the wrong
1403 /// status.
1404 pub const CREATION_CONTEXT: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!,$repositoryOwner:String!,$repositoryName:String!){
1405 boardFields:repositoryOwner(login:$owner){
1406 ... on ProjectV2Owner{projectV2(number:$number){id
1407 fields(first:$nestedFirst){nodes{
1408 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1409 ... on ProjectV2Field{__typename id name dataType}
1410 }pageInfo{hasNextPage}}
1411 }}
1412 }
1413 repository(owner:$repositoryOwner,name:$repositoryName){id nameWithOwner}
1414 }"#;
1415 /// Reads both dependency directions for one issue, with each far end's own kind — and
1416 /// the issue's own body, which is where an edge to another source is recorded, so that
1417 /// half of a dependency read needs no second read of the issue or of the board.
1418 pub const ISSUE_DEPENDENCIES: &str = concat!(
1419 r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
1420 ... on Issue{body
1421 blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1422 blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1423 }}}"#,
1424 related_issue!()
1425 );
1426 /// What one item depends on: [`ISSUE_DEPENDENCIES`] narrowed to `blockedBy`, so a
1427 /// dependency read asks GitHub for the one direction it answers. The issue's `body` rides
1428 /// along because its metadata slot is where an edge to another source is recorded — and a
1429 /// board draft's `body` too, with the one board item GitHub links a draft to and, aliased
1430 /// `board`, this board's own id, so a draft's recorded edges are read off this same answer
1431 /// and only when that item is this board's: no draft read and no board read follow it.
1432 ///
1433 /// The draft's board item is asked for under a root of its own, aliased `draft`, rather
1434 /// than beside its `body` under `node`. GitHub counts the type-conditional fragments of
1435 /// one selection as the largest of them — `blockedBy`'s 100 alone, where this workspace's
1436 /// calculation adds the draft's 3 — while separate roots are summed by both, so this is
1437 /// the spelling whose node count GitHub and the calculation agree on. Both price it at
1438 /// one point; the credentialed lane's reconciliation is what holds the two to that.
1439 pub const ISSUE_DEPENDS_ON: &str = concat!(
1440 r#"query($id:ID!,$first:Int!,$after:String,$owner:String!,$number:Int!,$boardItems:Int!){
1441 node(id:$id){__typename
1442 ... on Issue{body
1443 blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1444 }
1445 ... on DraftIssue{id body}}
1446 draft:node(id:$id){... on DraftIssue{projectV2Items(first:$boardItems){nodes{project{id number}}pageInfo{hasNextPage}}}}
1447 board:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){id}}}}"#,
1448 related_issue!()
1449 );
1450 /// What depends on one issue: [`ISSUE_DEPENDENCIES`] with its `blocking` alone. No `body`:
1451 /// the reverse of a recorded edge is derived from the far end, never written down here.
1452 pub const ISSUE_DEPENDED_ON_BY: &str = concat!(
1453 r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
1454 ... on Issue{
1455 blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1456 }}}"#,
1457 related_issue!()
1458 );
1459 /// Creates one issue. Outside a copy it is created on no board, and [`ADD_TO_BOARD`] files
1460 /// it; inside one, its input names the board in `projectV2Ids` and its project in
1461 /// `parentIssueId`, so this one request files it under both — see
1462 /// [`GitHubProjectsSource::create_filed_issue`](super::GitHubProjectsSource).
1463 pub const CREATE_ISSUE: &str =
1464 r#"mutation($input:CreateIssueInput!){createIssue(input:$input){issue{id number url}}}"#;
1465 /// Puts an existing issue on the configured board.
1466 pub const ADD_TO_BOARD: &str = r#"mutation($input:AddProjectV2ItemByIdInput!){addProjectV2ItemById(input:$input){item{id}}}"#;
1467 /// Updates an issue's visible fields and its open or closed state in one call.
1468 pub const UPDATE_ISSUE: &str =
1469 r#"mutation($input:UpdateIssueInput!){updateIssue(input:$input){issue{id}}}"#;
1470 /// Updates an existing draft's user-visible fields.
1471 pub const UPDATE_DRAFT: &str = r#"mutation($input:UpdateProjectV2DraftIssueInput!){updateProjectV2DraftIssue(input:$input){draftIssue{id}}}"#;
1472 /// Updates a text or single-select value on one project item.
1473 pub const UPDATE_FIELD: &str = r#"mutation($input:UpdateProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!){updateProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}}}}}"#;
1474 /// Writes up to six board fields and clears up to three in one ordered mutation: every
1475 /// aliased field is included by its own boolean, as [`FIELD_WRITE_SLOTS`](super::FIELD_WRITE_SLOTS)
1476 /// and [`FIELD_CLEAR_SLOTS`](super::FIELD_CLEAR_SLOTS) name them, writes before clears.
1477 pub const UPDATE_FIELDS: &str = r#"mutation($input:UpdateProjectV2ItemFieldValueInput!,$second:UpdateProjectV2ItemFieldValueInput!,$third:UpdateProjectV2ItemFieldValueInput!,$fourth:UpdateProjectV2ItemFieldValueInput!,$fifth:UpdateProjectV2ItemFieldValueInput!,$sixth:UpdateProjectV2ItemFieldValueInput!,$clear:ClearProjectV2ItemFieldValueInput!,$clearSecond:ClearProjectV2ItemFieldValueInput!,$clearThird:ClearProjectV2ItemFieldValueInput!,$writeFirst:Boolean!,$writeSecond:Boolean!,$writeThird:Boolean!,$writeFourth:Boolean!,$writeFifth:Boolean!,$writeSixth:Boolean!,$writeClear:Boolean!,$writeClearSecond:Boolean!,$writeClearThird:Boolean!){updateProjectV2ItemFieldValue(input:$input) @include(if:$writeFirst){projectV2Item{id}} second:updateProjectV2ItemFieldValue(input:$second) @include(if:$writeSecond){projectV2Item{id}} third:updateProjectV2ItemFieldValue(input:$third) @include(if:$writeThird){projectV2Item{id}} fourth:updateProjectV2ItemFieldValue(input:$fourth) @include(if:$writeFourth){projectV2Item{id}} fifth:updateProjectV2ItemFieldValue(input:$fifth) @include(if:$writeFifth){projectV2Item{id}} sixth:updateProjectV2ItemFieldValue(input:$sixth) @include(if:$writeSixth){projectV2Item{id}} cleared:clearProjectV2ItemFieldValue(input:$clear) @include(if:$writeClear){projectV2Item{id}} clearedSecond:clearProjectV2ItemFieldValue(input:$clearSecond) @include(if:$writeClearSecond){projectV2Item{id}} clearedThird:clearProjectV2ItemFieldValue(input:$clearThird) @include(if:$writeClearThird){projectV2Item{id}}}"#;
1478 /// Clears one project item's value of one field, which is what a `none` priority is.
1479 pub const CLEAR_FIELD: &str = r#"mutation($input:ClearProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!){clearProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}}}}}"#;
1480 /// Creates one field: a single-select one with its options, or a text one for a projected
1481 /// metadata value. Only the guarded field setup may use this document, and only for a
1482 /// field the board lacks.
1483 pub const CREATE_FIELD: &str = r#"mutation($input:CreateProjectV2FieldInput!){createProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id name options{id name color description}}}}}"#;
1484 /// Replaces a single-select field's options. Only the guarded field setup — the
1485 /// `status-options` and `fields` operations — may use this document, because GitHub
1486 /// treats the input as the complete option list.
1487 pub const STATUS_OPTIONS_UPDATE: &str = r#"mutation($input:UpdateProjectV2FieldInput!){updateProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id options{id name color description}}}}}"#;
1488 /// A fresh snapshot of the Status field and every board item's assignment.
1489 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}}}}}}"#;
1490 /// Files one issue under another as a sub-issue, which is what project membership is.
1491 pub const ADD_SUB_ISSUE: &str =
1492 r#"mutation($input:AddSubIssueInput!){addSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1493 /// Takes one issue back out of its parent.
1494 pub const REMOVE_SUB_ISSUE: &str = r#"mutation($input:RemoveSubIssueInput!){removeSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1495 /// Adds GitHub's native issue blocked-by relationship.
1496 pub const ADD_BLOCKED_BY: &str = r#"mutation($input:AddBlockedByInput!){addBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1497 /// One origin of [`ORIGIN_LOOKUPS`]: [`ORIGIN_LOOKUP`]'s two connections for the origin the
1498 /// `$filter<n>` and `$search<n>` of slot `<n>` name, sent only while `$ask<n>` holds.
1499 macro_rules! origin_lookup_alias {
1500 ($n:literal) => {
1501 concat!(
1502 "\n o",
1503 stringify!($n),
1504 ":repositoryOwner(login:$owner) @include(if:$ask",
1505 stringify!($n),
1506 "){... on ProjectV2Owner{projectV2(number:$number){items(first:$lookupFirst,query:$filter",
1507 stringify!($n),
1508 "){nodes{id updatedAt ",
1509 board_item_values!(),
1510 board_item_content!(),
1511 "} pageInfo{hasNextPage endCursor}}}}}\n s",
1512 stringify!($n),
1513 ":search(query:$search",
1514 stringify!($n),
1515 ",type:$type,first:$lookupFirst) @include(if:$ask",
1516 stringify!($n),
1517 "){pageInfo{hasNextPage endCursor} nodes{__typename ...BoardIssue}}"
1518 )
1519 };
1520 }
1521
1522 /// [`ORIGIN_LOOKUPS`], with `$extra_variables` declared after its own and `$extra_fields`
1523 /// selected beside them: one spelling of the batched lookup for the documents that send it.
1524 macro_rules! origin_lookups {
1525 ($extra_variables:literal, $extra_fields:literal) => {
1526 concat!(
1527 r#"query($owner:String!,$number:Int!,$type:SearchType!,$lookupFirst:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!,$repositoryOwner:String!,$repositoryName:String!,$filter0:String!,$filter1:String!,$filter2:String!,$filter3:String!,$filter4:String!,$filter5:String!,$filter6:String!,$filter7:String!,$filter8:String!,$filter9:String!,$search0:String!,$search1:String!,$search2:String!,$search3:String!,$search4:String!,$search5:String!,$search6:String!,$search7:String!,$search8:String!,$search9:String!,$ask0:Boolean!,$ask1:Boolean!,$ask2:Boolean!,$ask3:Boolean!,$ask4:Boolean!,$ask5:Boolean!,$ask6:Boolean!,$ask7:Boolean!,$ask8:Boolean!,$ask9:Boolean!"#,
1528 relationship_events_variable!(),
1529 $extra_variables,
1530 r#"){
1531 boardFields:repositoryOwner(login:$owner){
1532 ... on ProjectV2Owner{projectV2(number:$number){id
1533 fields(first:$nestedFirst){nodes{
1534 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1535 ... on ProjectV2Field{__typename id name dataType}
1536 }pageInfo{hasNextPage}}
1537 }}
1538 }
1539 repository(owner:$repositoryOwner,name:$repositoryName){id databaseId nameWithOwner}"#,
1540 $extra_fields,
1541 origin_lookup_alias!(0),
1542 origin_lookup_alias!(1),
1543 origin_lookup_alias!(2),
1544 origin_lookup_alias!(3),
1545 origin_lookup_alias!(4),
1546 origin_lookup_alias!(5),
1547 origin_lookup_alias!(6),
1548 origin_lookup_alias!(7),
1549 origin_lookup_alias!(8),
1550 origin_lookup_alias!(9),
1551 "\n }",
1552 board_issue!()
1553 )
1554 };
1555 }
1556
1557 /// The first page of [`ORIGIN_LOOKUP`] for up to [`ORIGIN_BATCH`](super::ORIGIN_BATCH)
1558 /// origins a copy is about to look for, and everything creating an item then reads —
1559 /// the board's own id and field definitions, as [`CREATION_CONTEXT`] reads them, and the node
1560 /// id and the numeric id of the repository an issue is created in — in one request.
1561 ///
1562 /// What a copy reads before it writes anything, so that finding none of its records on the
1563 /// board and creating every one costs this read and the writes. An origin whose page says
1564 /// there is more is left to its own [`ORIGIN_LOOKUP`]. The repository's numeric id is what an
1565 /// image upload names, so a copy carrying assets needs no `GET /repos/{owner}/{repo}` either.
1566 pub const ORIGIN_LOOKUPS: &str = origin_lookups!("", "");
1567
1568 /// [`ORIGIN_LOOKUPS`], with a page of the members of the project `$project` beside it, each
1569 /// trimmed to what classifying it reads — its id, its parent, its body and its repository,
1570 /// and no connection under it.
1571 ///
1572 /// What a document copy sends where the public boundary screens its write: the document is
1573 /// held to who may read its project, which is its members' classification as much as the
1574 /// project's own, and this request carries both the project — among the origins it asks —
1575 /// and its members. GitHub's own dry run prices it at the one point [`ORIGIN_LOOKUPS`]
1576 /// costs alone (`docs/measurements/board-copy-gaps-2026-10-24/coalesced-members-dry-run`).
1577 pub const ORIGIN_LOOKUPS_WITH_MEMBERS: &str = origin_lookups!(
1578 ",$project:ID!,$memberFirst:Int!",
1579 "\n members:node(id:$project){... on Issue{subIssues(first:$memberFirst){pageInfo{hasNextPage endCursor} nodes{id body parent{id} repository{nameWithOwner}}}}}"
1580 );
1581
1582 /// One slot `<n>` of a copy's aliased writes: `$<prefix><n>` is its input and `$send<n>`
1583 /// whether it is sent.
1584 macro_rules! write_slot {
1585 ($prefix:literal, $field:literal, $selection:literal, $n:literal) => {
1586 concat!(
1587 " ",
1588 $prefix,
1589 stringify!($n),
1590 ":",
1591 $field,
1592 "(input:$",
1593 $prefix,
1594 stringify!($n),
1595 ") @include(if:$send",
1596 stringify!($n),
1597 "){",
1598 $selection,
1599 "}"
1600 )
1601 };
1602 }
1603
1604 /// The variables of a copy's aliased writes: eleven inputs of `$type` named `$<prefix><n>`,
1605 /// and eleven `$send<n>`.
1606 macro_rules! write_variables {
1607 ($prefix:literal, $type:literal) => {
1608 concat!(
1609 "mutation(",
1610 "$",
1611 $prefix,
1612 "0:",
1613 $type,
1614 "!,$",
1615 $prefix,
1616 "1:",
1617 $type,
1618 "!,$",
1619 $prefix,
1620 "2:",
1621 $type,
1622 "!,$",
1623 $prefix,
1624 "3:",
1625 $type,
1626 "!,$",
1627 $prefix,
1628 "4:",
1629 $type,
1630 "!,$",
1631 $prefix,
1632 "5:",
1633 $type,
1634 "!,$",
1635 $prefix,
1636 "6:",
1637 $type,
1638 "!,$",
1639 $prefix,
1640 "7:",
1641 $type,
1642 "!,$",
1643 $prefix,
1644 "8:",
1645 $type,
1646 "!,$",
1647 $prefix,
1648 "9:",
1649 $type,
1650 "!,$",
1651 $prefix,
1652 "10:",
1653 $type,
1654 "!,",
1655 "$send0:Boolean!,$send1:Boolean!,$send2:Boolean!,$send3:Boolean!,$send4:Boolean!,",
1656 "$send5:Boolean!,$send6:Boolean!,$send7:Boolean!,$send8:Boolean!,$send9:Boolean!,",
1657 "$send10:Boolean!){"
1658 )
1659 };
1660 }
1661
1662 /// A copy's aliased writes of one kind: [`MUTATION_BATCH`](super::MUTATION_BATCH) slots of
1663 /// `$field`, each sent only while its `$send<n>` holds.
1664 macro_rules! write_batch {
1665 ($prefix:literal, $type:literal, $field:literal, $selection:literal) => {
1666 concat!(
1667 write_variables!($prefix, $type),
1668 write_slot!($prefix, $field, $selection, 0),
1669 write_slot!($prefix, $field, $selection, 1),
1670 write_slot!($prefix, $field, $selection, 2),
1671 write_slot!($prefix, $field, $selection, 3),
1672 write_slot!($prefix, $field, $selection, 4),
1673 write_slot!($prefix, $field, $selection, 5),
1674 write_slot!($prefix, $field, $selection, 6),
1675 write_slot!($prefix, $field, $selection, 7),
1676 write_slot!($prefix, $field, $selection, 8),
1677 write_slot!($prefix, $field, $selection, 9),
1678 write_slot!($prefix, $field, $selection, 10),
1679 "}"
1680 )
1681 };
1682 }
1683
1684 /// Creates up to [`MUTATION_BATCH`](super::MUTATION_BATCH) issues in one request, each filed on
1685 /// the board and under its project by the same `createIssue` — its `projectV2Ids` and
1686 /// `parentIssueId` — as [`CREATE_ISSUE`] creates one.
1687 pub const CREATE_ISSUES: &str = write_batch!(
1688 "create",
1689 "CreateIssueInput",
1690 "createIssue",
1691 "issue{id number url}"
1692 );
1693
1694 /// One issue of [`CREATED_BOARD_ITEMS`]: the board items the issue `$id<n>` names is on, with
1695 /// the `Status` value of each, read only while `$read<n>` holds.
1696 macro_rules! created_item_alias {
1697 ($n:literal) => {
1698 concat!(
1699 "\n i",
1700 stringify!($n),
1701 ":node(id:$id",
1702 stringify!($n),
1703 ") @include(if:$read",
1704 stringify!($n),
1705 "){... on Issue{id projectItems(first:$boardItems){nodes{id project{id} status:fieldValueByName(name:$statusName){... on ProjectV2ItemFieldSingleSelectValue{name}}}}}}"
1706 )
1707 };
1708 }
1709
1710 /// The board item of each issue a copy created, up to
1711 /// [`COPY_DETAIL_BATCH`](super::COPY_DETAIL_BATCH) at a time, with its `Status` as the board
1712 /// set it: what `createIssue`'s `projectV2Ids` files the issue as, which GitHub adds after
1713 /// answering the create, so its id comes from a read made once every create has been sent.
1714 pub const CREATED_BOARD_ITEMS: &str = concat!(
1715 r#"query($id0:ID!,$id1:ID!,$id2:ID!,$id3:ID!,$id4:ID!,$id5:ID!,$id6:ID!,$id7:ID!,$id8:ID!,$id9:ID!,$id10:ID!,$id11:ID!,$id12:ID!,$id13:ID!,$id14:ID!,$id15:ID!,$id16:ID!,$id17:ID!,$id18:ID!,$id19:ID!,$id20:ID!,$read0:Boolean!,$read1:Boolean!,$read2:Boolean!,$read3:Boolean!,$read4:Boolean!,$read5:Boolean!,$read6:Boolean!,$read7:Boolean!,$read8:Boolean!,$read9:Boolean!,$read10:Boolean!,$read11:Boolean!,$read12:Boolean!,$read13:Boolean!,$read14:Boolean!,$read15:Boolean!,$read16:Boolean!,$read17:Boolean!,$read18:Boolean!,$read19:Boolean!,$read20:Boolean!,$boardItems:Int!,$statusName:String!){"#,
1716 created_item_alias!(0),
1717 created_item_alias!(1),
1718 created_item_alias!(2),
1719 created_item_alias!(3),
1720 created_item_alias!(4),
1721 created_item_alias!(5),
1722 created_item_alias!(6),
1723 created_item_alias!(7),
1724 created_item_alias!(8),
1725 created_item_alias!(9),
1726 created_item_alias!(10),
1727 created_item_alias!(11),
1728 created_item_alias!(12),
1729 created_item_alias!(13),
1730 created_item_alias!(14),
1731 created_item_alias!(15),
1732 created_item_alias!(16),
1733 created_item_alias!(17),
1734 created_item_alias!(18),
1735 created_item_alias!(19),
1736 created_item_alias!(20),
1737 "\n }"
1738 );
1739
1740 /// The variables and the aliases of [`WRITE_BATCH`](super::WRITE_BATCH) slots: `$<prefix><n>`
1741 /// is a slot's input and `$<send><n>` whether it is sent, for the slot numbers given.
1742 macro_rules! wide_variables {
1743 ($prefix:literal, $type:literal, $send:literal; $($n:literal)+) => {
1744 concat!($("$", $prefix, stringify!($n), ":", $type, "!,$", $send, stringify!($n), ":Boolean!,",)+)
1745 };
1746 }
1747 macro_rules! wide_slots {
1748 ($prefix:literal, $field:literal, $selection:literal, $send:literal; $($n:literal)+) => {
1749 concat!($(
1750 " ", $prefix, stringify!($n), ":", $field, "(input:$", $prefix, stringify!($n),
1751 ") @include(if:$", $send, stringify!($n), "){", $selection, "}",
1752 )+)
1753 };
1754 }
1755
1756 /// Up to [`WRITE_BATCH`](super::WRITE_BATCH) board field values a copy writes on the items it
1757 /// created, each on its own item, in one ordered request.
1758 pub const WRITE_FIELD_VALUES: &str = concat!(
1759 "mutation(",
1760 wide_variables!("field", "UpdateProjectV2ItemFieldValueInput", "send"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1761 "){",
1762 wide_slots!("field", "updateProjectV2ItemFieldValue", "projectV2Item{id}", "send"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1763 "}"
1764 );
1765
1766 /// A copy's board field values and its blocked-by edges together, up to
1767 /// [`WRITE_BATCH`](super::WRITE_BATCH) of the two kinds between them, in one ordered request:
1768 /// every field value its `$sendField<n>` sends, then every edge its `$sendBlock<n>` sends.
1769 /// What a copy sends when one request of its writes holds both kinds; one holding a single
1770 /// kind is [`WRITE_FIELD_VALUES`] or [`ADD_BLOCKED_BYS`].
1771 pub const WRITE_FIELDS_AND_EDGES: &str = concat!(
1772 "mutation(",
1773 wide_variables!("field", "UpdateProjectV2ItemFieldValueInput", "sendField"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1774 wide_variables!("block", "AddBlockedByInput", "sendBlock"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1775 "){",
1776 wide_slots!("field", "updateProjectV2ItemFieldValue", "projectV2Item{id}", "sendField"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1777 wide_slots!("block", "addBlockedBy", "issue{id} blockingIssue{id}", "sendBlock"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1778 "}"
1779 );
1780
1781 /// Closes up to [`MUTATION_BATCH`](super::MUTATION_BATCH) issues a copy created at a closing
1782 /// status, each with its own `stateInput` and nothing else, once its option is selected.
1783 pub const CLOSE_ISSUES: &str =
1784 write_batch!("close", "UpdateIssueInput", "updateIssue", "issue{id}");
1785
1786 /// Up to [`WRITE_BATCH`](super::WRITE_BATCH) native blocked-by relationships a copy adds to the
1787 /// issues it created, in one request.
1788 pub const ADD_BLOCKED_BYS: &str = concat!(
1789 "mutation(",
1790 wide_variables!("block", "AddBlockedByInput", "send"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1791 "){",
1792 wide_slots!("block", "addBlockedBy", "issue{id} blockingIssue{id}", "send"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1793 "}"
1794 );
1795
1796 /// Removes one native issue blocked-by relationship.
1797 pub const REMOVE_BLOCKED_BY: &str = r#"mutation($input:RemoveBlockedByInput!){removeBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1798 /// Deletes one issue, which takes its board item with it.
1799 ///
1800 /// The engine sends this in one situation only: undoing a copy that could not finish,
1801 /// over the items that same copy created. Deleting the issue removes the board item
1802 /// too, so there is no second `deleteProjectV2Item` to keep in step with it.
1803 pub const DELETE_ISSUE: &str =
1804 r#"mutation($input:DeleteIssueInput!){deleteIssue(input:$input){repository{id}}}"#;
1805 /// Deletes up to [`DELETE_BATCH`](super::DELETE_BATCH) issues in one request, each as
1806 /// [`DELETE_ISSUE`] deletes one: what a write that created several issues and was refused
1807 /// part way sends to take them back.
1808 pub const DELETE_ISSUES: &str = concat!(
1809 "mutation(",
1810 wide_variables!("delete", "DeleteIssueInput", "send"; 0 1 2 3 4),
1811 "){",
1812 wide_slots!("delete", "deleteIssue", "repository{id}", "send"; 0 1 2 3 4),
1813 "}"
1814 );
1815
1816 /// Everything this source reads about one issue comment, wherever it reaches one.
1817 ///
1818 /// A macro for the reason [`board_issue!`] is one: a comment listed, a comment just added
1819 /// and a comment just edited are handed to one mapper, so they are selected by one
1820 /// spelling. `author` is `Actor`, which GitHub answers `null` for an account that no
1821 /// longer exists, and `login` is the one member every kind of actor carries.
1822 macro_rules! issue_comment {
1823 () => {
1824 "id author{login} createdAt updatedAt body url"
1825 };
1826 }
1827
1828 /// One task's comments: a page of its issue's own `comments` connection.
1829 ///
1830 /// **No `orderBy`, and that is what makes the page oldest first.** GitHub's only
1831 /// `IssueCommentOrder` field is `UPDATED_AT`, which would move a comment to the end of the
1832 /// list every time somebody edited it; left unordered the connection answers in the order
1833 /// the comments were written, which is the order GitHub documents for the same collection
1834 /// over REST — ascending id. Nothing multiplies through it, so `$first` is the whole of its
1835 /// node count and the caller's own page size is pushed straight down.
1836 pub const ISSUE_COMMENTS: &str = concat!(
1837 r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename ... on Issue{comments(first:$first,after:$after){nodes{"#,
1838 issue_comment!(),
1839 r#"}pageInfo{hasNextPage endCursor}}}}}"#
1840 );
1841 /// One issue by its own node id, with a page of its comments: what `task show` and a
1842 /// comment listing read, in one request.
1843 ///
1844 /// [`ISSUE`] and [`ISSUE_COMMENTS`] in one document, rather than one then the other. The
1845 /// comments are selected here and **not** on the shared `board_issue!` fragment, which
1846 /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] nest under a page of a hundred issues: a comment
1847 /// connection there would multiply through both of those documents' price, and neither
1848 /// needs one.
1849 pub const ISSUE_DETAIL: &str = concat!(
1850 r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1851 relationship_events_variable!(),
1852 r#"){
1853 node(id:$id){__typename ...BoardIssue ... on Issue{comments(first:$first,after:$after){nodes{"#,
1854 issue_comment!(),
1855 r#"}pageInfo{hasNextPage endCursor}}}}
1856 }"#,
1857 board_issue!()
1858 );
1859
1860 /// One alias of [`ISSUE_DETAILS`]: the item a batch's `$id<n>` names, with the first
1861 /// page of its comments when `$comments` asks for them.
1862 macro_rules! issue_details_alias {
1863 ($n:literal) => {
1864 concat!(
1865 "\n i",
1866 stringify!($n),
1867 ":node(id:$id",
1868 stringify!($n),
1869 "){__typename ...BoardIssue ... on Issue{comments(first:$first) @include(if:$comments){nodes{",
1870 issue_comment!(),
1871 "}pageInfo{hasNextPage endCursor}}}}"
1872 )
1873 };
1874 }
1875
1876 /// [`ISSUE_DETAIL`] for [`DETAIL_BATCH`](super::DETAIL_BATCH) items at once, each by its
1877 /// own node id, as one fixed-size document of aliased `node(id:)` fields.
1878 ///
1879 /// **Aliased `node(id:)` rather than `nodes(ids:)`, and that is what keeps its price
1880 /// honest.** The `github-graphql-node-count` model this workspace prices with treats a
1881 /// field that supplies neither `first` nor `last` as free, and `nodes(ids:)` supplies
1882 /// neither — so every connection under it would be priced at nothing and the pin in
1883 /// `tests/point_cost.rs` would understate what GitHub charges. Each alias here is the
1884 /// one-item read the model already prices, so the batch costs what its aliases cost.
1885 ///
1886 /// **Fixed-size, so there is one document to price.** A batch of fewer items binds the
1887 /// slots it has no item for to the last item it does, and reads that item again; the
1888 /// price is the document's, whatever its variables, so a short batch costs what a full
1889 /// one does and nothing more.
1890 pub const ISSUE_DETAILS: &str = concat!(
1891 r#"query($id0:ID!,$id1:ID!,$id2:ID!,$id3:ID!,$id4:ID!,$id5:ID!,$id6:ID!,$id7:ID!,$id8:ID!,$id9:ID!,$id10:ID!,$id11:ID!,$id12:ID!,$id13:ID!,$id14:ID!,$id15:ID!,$id16:ID!,$id17:ID!,$id18:ID!,$id19:ID!,$id20:ID!,$id21:ID!,$id22:ID!,$id23:ID!,$first:Int!,$comments:Boolean!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1892 relationship_events_variable!(),
1893 r#"){"#,
1894 issue_details_alias!(0),
1895 issue_details_alias!(1),
1896 issue_details_alias!(2),
1897 issue_details_alias!(3),
1898 issue_details_alias!(4),
1899 issue_details_alias!(5),
1900 issue_details_alias!(6),
1901 issue_details_alias!(7),
1902 issue_details_alias!(8),
1903 issue_details_alias!(9),
1904 issue_details_alias!(10),
1905 issue_details_alias!(11),
1906 issue_details_alias!(12),
1907 issue_details_alias!(13),
1908 issue_details_alias!(14),
1909 issue_details_alias!(15),
1910 issue_details_alias!(16),
1911 issue_details_alias!(17),
1912 issue_details_alias!(18),
1913 issue_details_alias!(19),
1914 issue_details_alias!(20),
1915 issue_details_alias!(21),
1916 issue_details_alias!(22),
1917 issue_details_alias!(23),
1918 "\n }",
1919 board_issue!()
1920 );
1921
1922 /// One alias of [`ISSUE_COPY_DETAILS`]: the item a batch's `$id<n>` names, with the first
1923 /// page of its comments and the issues it is blocked by.
1924 macro_rules! issue_copy_details_alias {
1925 ($n:literal) => {
1926 concat!(
1927 "\n i",
1928 stringify!($n),
1929 ":node(id:$id",
1930 stringify!($n),
1931 "){__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{",
1932 issue_comment!(),
1933 "}pageInfo{hasNextPage endCursor}} blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}}}"
1934 )
1935 };
1936 }
1937
1938 /// [`ISSUE_DETAILS`] as a copy reads the tasks it carries: each item with the first page of
1939 /// its comments and its `blockedBy` — the edges a copy reads of every task next, which
1940 /// [`ISSUE`] carries and [`ISSUE_DETAILS`] does not — [`COPY_DETAIL_BATCH`](super::COPY_DETAIL_BATCH)
1941 /// at a time, so a copy of a changed task costs this read and nothing else.
1942 ///
1943 /// A document of its own rather than a widening of [`ISSUE_DETAILS`], whose batch size is
1944 /// what `task show-many` is priced by and which reads no edge.
1945 ///
1946 /// `children`, read only while `$children` holds, is which issues the issue `$project` has
1947 /// as sub-issues — each one's id and title, the page `$nestedFirst` allows — so that a
1948 /// project copy, which reads its project and every task at once, knows from this same read
1949 /// which tasks the project holds at the destination, and its orphan check is answered by it
1950 /// rather than by a listing of the project.
1951 pub const ISSUE_COPY_DETAILS: &str = concat!(
1952 r#"query($id0:ID!,$id1:ID!,$id2:ID!,$id3:ID!,$id4:ID!,$id5:ID!,$id6:ID!,$id7:ID!,$id8:ID!,$id9:ID!,$id10:ID!,$id11:ID!,$id12:ID!,$id13:ID!,$id14:ID!,$id15:ID!,$id16:ID!,$id17:ID!,$id18:ID!,$id19:ID!,$id20:ID!,$project:ID!,$children:Boolean!,$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1953 relationship_events_variable!(),
1954 r#"){"#,
1955 issue_copy_details_alias!(0),
1956 issue_copy_details_alias!(1),
1957 issue_copy_details_alias!(2),
1958 issue_copy_details_alias!(3),
1959 issue_copy_details_alias!(4),
1960 issue_copy_details_alias!(5),
1961 issue_copy_details_alias!(6),
1962 issue_copy_details_alias!(7),
1963 issue_copy_details_alias!(8),
1964 issue_copy_details_alias!(9),
1965 issue_copy_details_alias!(10),
1966 issue_copy_details_alias!(11),
1967 issue_copy_details_alias!(12),
1968 issue_copy_details_alias!(13),
1969 issue_copy_details_alias!(14),
1970 issue_copy_details_alias!(15),
1971 issue_copy_details_alias!(16),
1972 issue_copy_details_alias!(17),
1973 issue_copy_details_alias!(18),
1974 issue_copy_details_alias!(19),
1975 issue_copy_details_alias!(20),
1976 "\n children:node(id:$project) @include(if:$children){... on Issue{id subIssues(first:$nestedFirst){nodes{id title} pageInfo{hasNextPage}}}}",
1977 "\n }",
1978 board_issue!(),
1979 related_issue!()
1980 );
1981
1982 /// Which issue one comment is on, read before that comment is edited or removed.
1983 ///
1984 /// GitHub's comment mutations take the comment's id and nothing else, so without this a
1985 /// comment id given against the wrong task would change a comment on another issue.
1986 pub const COMMENT_ISSUE: &str =
1987 r#"query($id:ID!){node(id:$id){__typename ... on IssueComment{id body issue{id}}}}"#;
1988 /// Adds one comment to an issue, signed as the account the token belongs to.
1989 pub const ADD_COMMENT: &str = concat!(
1990 r#"mutation($input:AddCommentInput!){addComment(input:$input){subject{id} commentEdge{node{"#,
1991 issue_comment!(),
1992 r#"}}}}"#
1993 );
1994 /// Replaces the body of one issue comment.
1995 pub const UPDATE_COMMENT: &str = concat!(
1996 r#"mutation($input:UpdateIssueCommentInput!){updateIssueComment(input:$input){issueComment{"#,
1997 issue_comment!(),
1998 r#"}}}"#
1999 );
2000 /// Removes one issue comment. Its payload carries nothing about the comment it removed.
2001 pub const DELETE_COMMENT: &str = r#"mutation($input:DeleteIssueCommentInput!){deleteIssueComment(input:$input){clientMutationId}}"#;
2002
2003 /// What a mutation of a write the store's cache expects selects of the issue it wrote:
2004 /// everything [`ISSUE_DETAIL`] reads of it, so its response shows the task whole — its
2005 /// change token's every part and the first page of its comments.
2006 ///
2007 /// Sent **only** while that write runs, in place of the document named beside each
2008 /// below, so every other write's documents, prices and request counts are what they
2009 /// were. GitHub's token waits for a task to settle (`change_token::SETTLED`), and a
2010 /// mutation's response is stamped seconds after the change it made, so against GitHub
2011 /// itself a response rarely determines a version; the cache is then left unserveable
2012 /// for the task, and its next read refreshes it.
2013 macro_rules! whole_issue {
2014 () => {
2015 concat!(
2016 r#"__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{"#,
2017 issue_comment!(),
2018 r#"}pageInfo{hasNextPage endCursor}}}"#
2019 )
2020 };
2021 }
2022
2023 /// The variables every document of `whole_issue!` declares beside its own.
2024 macro_rules! whole_variables {
2025 () => {
2026 concat!(
2027 r#",$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
2028 relationship_events_variable!()
2029 )
2030 };
2031 }
2032
2033 /// [`ADD_COMMENT`], selecting the whole issue commented on.
2034 pub const ADD_COMMENT_WHOLE: &str = concat!(
2035 r#"mutation($input:AddCommentInput!"#,
2036 whole_variables!(),
2037 r#"){addComment(input:$input){subject{id "#,
2038 whole_issue!(),
2039 r#"} commentEdge{node{"#,
2040 issue_comment!(),
2041 r#"}}}}"#,
2042 board_issue!()
2043 );
2044 /// [`UPDATE_COMMENT`], selecting the whole issue the comment is on.
2045 pub const UPDATE_COMMENT_WHOLE: &str = concat!(
2046 r#"mutation($input:UpdateIssueCommentInput!"#,
2047 whole_variables!(),
2048 r#"){updateIssueComment(input:$input){issueComment{"#,
2049 issue_comment!(),
2050 r#" issue{"#,
2051 whole_issue!(),
2052 r#"}}}}"#,
2053 board_issue!()
2054 );
2055 /// [`UPDATE_ISSUE`], selecting the whole issue updated.
2056 pub const UPDATE_ISSUE_WHOLE: &str = concat!(
2057 r#"mutation($input:UpdateIssueInput!"#,
2058 whole_variables!(),
2059 r#"){updateIssue(input:$input){issue{id "#,
2060 whole_issue!(),
2061 r#"}}}"#,
2062 board_issue!()
2063 );
2064 /// [`UPDATE_FIELD`], selecting the whole issue behind the board item written.
2065 pub const UPDATE_FIELD_WHOLE: &str = concat!(
2066 r#"mutation($input:UpdateProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
2067 whole_variables!(),
2068 r#"){updateProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
2069 whole_issue!(),
2070 r#"}}}}"#,
2071 board_issue!()
2072 );
2073 /// [`CLEAR_FIELD`], selecting the whole issue behind the board item cleared.
2074 pub const CLEAR_FIELD_WHOLE: &str = concat!(
2075 r#"mutation($input:ClearProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
2076 whole_variables!(),
2077 r#"){clearProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
2078 whole_issue!(),
2079 r#"}}}}"#,
2080 board_issue!()
2081 );
2082
2083 /// The document sent in `document`'s place while a write the store's cache expects runs,
2084 /// when it has one.
2085 #[must_use]
2086 pub fn whole_twin(document: &str) -> Option<&'static str> {
2087 [
2088 (ADD_COMMENT, ADD_COMMENT_WHOLE),
2089 (UPDATE_COMMENT, UPDATE_COMMENT_WHOLE),
2090 (UPDATE_ISSUE, UPDATE_ISSUE_WHOLE),
2091 (UPDATE_FIELD, UPDATE_FIELD_WHOLE),
2092 (CLEAR_FIELD, CLEAR_FIELD_WHOLE),
2093 ]
2094 .into_iter()
2095 .find(|(plain, _)| *plain == document)
2096 .map(|(_, whole)| whole)
2097 }
2098
2099 /// Every document above, with what this source is doing when it sends one.
2100 ///
2101 /// One list rather than a `match` beside the constants: a rate-limit diagnostic has to
2102 /// name the call that was refused, and a `match` with a catch-all arm would answer a
2103 /// document added later with "talking to GitHub" and never say so.
2104 ///
2105 /// `documents_are_all_inventoried` reads this file back and fails naming any `pub
2106 /// const` here that this list omits, so the two cannot part — which is the same guard
2107 /// `CATEGORIES` carries, in the one shape available to a set of `&str` constants.
2108 pub const DOCUMENTS: [(&str, &str); 54] = [
2109 (SEARCH_ISSUES, "searching this board's issues"),
2110 (ISSUE, "reading one issue"),
2111 (
2112 DOCUMENT_IN_PROJECT,
2113 "reading one document with its project and the project's members",
2114 ),
2115 (
2116 ISSUE_BOARD_ITEMS,
2117 "reading one issue's board memberships past the page it came with",
2118 ),
2119 (SUB_ISSUES, "reading a project's tasks"),
2120 (BOARD, "reading the board"),
2121 (ORIGIN_LOOKUP, "looking up the items copied from one origin"),
2122 (CLOSED_ITEMS, "reading the board's closed items"),
2123 (
2124 ISSUES_BY_REFERENCE,
2125 "reading a batch of issues by their references",
2126 ),
2127 (BOARD_FIELDS, "reading the board's fields"),
2128 (DRAFT, "reading one draft"),
2129 (REPOSITORY, "reading the destination repository"),
2130 (
2131 PROJECT_VISIBILITY,
2132 "reading whether the board's project is public",
2133 ),
2134 (
2135 CREATION_CONTEXT,
2136 "reading the board's fields and the destination repository",
2137 ),
2138 (ISSUE_DEPENDENCIES, "reading an issue's dependencies"),
2139 (ISSUE_DEPENDS_ON, "reading what an item depends on"),
2140 (ISSUE_DEPENDED_ON_BY, "reading what depends on an issue"),
2141 (CREATE_ISSUE, "creating an issue"),
2142 (
2143 ORIGIN_LOOKUPS,
2144 "looking up the items copied from several origins, and what creating one reads",
2145 ),
2146 (
2147 ORIGIN_LOOKUPS_WITH_MEMBERS,
2148 "looking up the items copied from several origins, with a project's members",
2149 ),
2150 (CREATE_ISSUES, "creating several issues together"),
2151 (
2152 CREATED_BOARD_ITEMS,
2153 "reading the board items of the issues a copy created",
2154 ),
2155 (
2156 WRITE_FIELD_VALUES,
2157 "writing board fields of the items a copy created",
2158 ),
2159 (
2160 WRITE_FIELDS_AND_EDGES,
2161 "writing board fields and dependencies of the items a copy created",
2162 ),
2163 (CLOSE_ISSUES, "closing the issues a copy created"),
2164 (ADD_BLOCKED_BYS, "recording several dependencies together"),
2165 (ADD_TO_BOARD, "adding an issue to the board"),
2166 (UPDATE_ISSUE, "updating an issue"),
2167 (UPDATE_DRAFT, "updating a draft item"),
2168 (UPDATE_FIELD, "writing a board field"),
2169 (UPDATE_FIELDS, "writing board fields together"),
2170 (CLEAR_FIELD, "clearing a board field"),
2171 (
2172 CREATE_FIELD,
2173 "creating a board single-select field with its options",
2174 ),
2175 (
2176 STATUS_OPTIONS_SNAPSHOT,
2177 "snapshotting board Status options and assignments",
2178 ),
2179 (
2180 STATUS_OPTIONS_UPDATE,
2181 "safely replacing the board Status option list",
2182 ),
2183 (ADD_SUB_ISSUE, "filing an issue under its project"),
2184 (REMOVE_SUB_ISSUE, "taking an issue out of its project"),
2185 (ADD_BLOCKED_BY, "recording a dependency"),
2186 (REMOVE_BLOCKED_BY, "removing a dependency"),
2187 (DELETE_ISSUE, "deleting an issue"),
2188 (DELETE_ISSUES, "deleting several issues together"),
2189 (ISSUE_COMMENTS, "reading a task's comments"),
2190 (ISSUE_DETAIL, "reading one issue with its comments"),
2191 (
2192 ISSUE_DETAILS,
2193 "reading a batch of issues with their comments",
2194 ),
2195 (
2196 ISSUE_COPY_DETAILS,
2197 "reading a batch of issues with their comments and dependencies for a copy",
2198 ),
2199 (COMMENT_ISSUE, "reading which issue a comment is on"),
2200 (ADD_COMMENT, "adding a comment"),
2201 (UPDATE_COMMENT, "editing a comment"),
2202 (DELETE_COMMENT, "deleting a comment"),
2203 (
2204 ADD_COMMENT_WHOLE,
2205 "adding a comment, answered with the whole issue",
2206 ),
2207 (
2208 UPDATE_COMMENT_WHOLE,
2209 "editing a comment, answered with the whole issue",
2210 ),
2211 (
2212 UPDATE_ISSUE_WHOLE,
2213 "updating an issue, answered with it whole",
2214 ),
2215 (
2216 UPDATE_FIELD_WHOLE,
2217 "writing a board field, answered with the whole issue",
2218 ),
2219 (
2220 CLEAR_FIELD_WHOLE,
2221 "clearing a board field, answered with the whole issue",
2222 ),
2223 ];
2224}
2225
2226/// One aliased mutation field of [`graphql::UPDATE_FIELDS`]: the key its answer comes back
2227/// under, the variable carrying its input, and the boolean variable that includes it.
2228#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2229pub struct FieldSlot {
2230 /// The key of the answer's `data` this field's payload is under.
2231 pub alias: &'static str,
2232 /// The variable carrying this field's input.
2233 pub variable: &'static str,
2234 /// The boolean variable whose `true` sends this field.
2235 pub include: &'static str,
2236}
2237
2238/// The value writes [`graphql::UPDATE_FIELDS`] carries, in the order GitHub runs them.
2239pub const FIELD_WRITE_SLOTS: [FieldSlot; 6] = [
2240 FieldSlot {
2241 alias: "updateProjectV2ItemFieldValue",
2242 variable: "input",
2243 include: "writeFirst",
2244 },
2245 FieldSlot {
2246 alias: "second",
2247 variable: "second",
2248 include: "writeSecond",
2249 },
2250 FieldSlot {
2251 alias: "third",
2252 variable: "third",
2253 include: "writeThird",
2254 },
2255 FieldSlot {
2256 alias: "fourth",
2257 variable: "fourth",
2258 include: "writeFourth",
2259 },
2260 FieldSlot {
2261 alias: "fifth",
2262 variable: "fifth",
2263 include: "writeFifth",
2264 },
2265 FieldSlot {
2266 alias: "sixth",
2267 variable: "sixth",
2268 include: "writeSixth",
2269 },
2270];
2271
2272/// The clears [`graphql::UPDATE_FIELDS`] carries, run after every write of the same request.
2273pub const FIELD_CLEAR_SLOTS: [FieldSlot; 3] = [
2274 FieldSlot {
2275 alias: "cleared",
2276 variable: "clear",
2277 include: "writeClear",
2278 },
2279 FieldSlot {
2280 alias: "clearedSecond",
2281 variable: "clearSecond",
2282 include: "writeClearSecond",
2283 },
2284 FieldSlot {
2285 alias: "clearedThird",
2286 variable: "clearThird",
2287 include: "writeClearThird",
2288 },
2289];
2290
2291/// Which of GitHub's two rate limiters refused a request.
2292///
2293/// Waiting is the whole answer to the primary budget, and polling is what *extends* the
2294/// secondary one — so an operator told the wrong one takes the wrong next step, which is
2295/// the whole reason this is carried rather than collapsed into "rate limited".
2296#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2297enum Limiter {
2298 /// The hourly API budget, which `gh api rate_limit` reports and a wait answers.
2299 Primary,
2300 /// The burst limiter over content-generating requests, which nothing reports.
2301 Secondary,
2302}
2303
2304/// The wordings GitHub answers a secondary rate limit with.
2305///
2306/// It sends them under a forbidden status, under a too-many-requests status, and inside
2307/// the `errors` of a *successful* response, which is why the text is what this matches on
2308/// rather than the status. `abuse detection` is the wording GitHub used before the
2309/// limiter was renamed and still returns from some endpoints; `submitted too quickly` is
2310/// what a burst of content creation is refused with.
2311///
2312/// This is GitHub's vocabulary rather than this source's, so it is pinned rather than
2313/// remembered: `tests/fixtures/rate-limits.json` records where each wording was read and
2314/// when, and the drift gate reconciles the two lists both ways. Public for that gate
2315/// alone — a caller has no use for it, and matching on a refusal is this source's job.
2316pub const SECONDARY_WORDINGS: [&str; 5] = [
2317 "secondary rate limit",
2318 "temporarily blocked from content creation",
2319 "abuse detection",
2320 "submitted too quickly",
2321 "exceeded a secondary",
2322];
2323
2324/// The wordings GitHub answers an exhausted primary budget with.
2325///
2326/// `rate_limited` is the `type` its GraphQL error carries, which is read as a field rather
2327/// than looked for in the response text. `api rate limit already exceeded` is what GraphQL
2328/// answers a request made once the hour's budget is spent — "API rate limit already exceeded
2329/// for user ID …" in the `errors` of an HTTP 200, with no `type` — and neither of the other
2330/// two phrases is a substring of it, so without it that answer read as a refusal that will
2331/// never lift. Pinned and gated exactly as [`SECONDARY_WORDINGS`] is, and public for the same
2332/// one reason.
2333pub const PRIMARY_WORDINGS: [&str; 4] = [
2334 "api rate limit exceeded",
2335 "api rate limit already exceeded",
2336 "rate limit exceeded",
2337 "rate_limited",
2338];
2339
2340/// What a response *says about itself*, which is the only place a refusal can be read.
2341///
2342/// Deliberately not the whole response body. A board is a place people write about their
2343/// own work, and a task on it titled "the secondary rate limit" would, matched across the
2344/// raw text, turn a perfectly good answer into a refusal this source then waited out and
2345/// reported. So the item data is never read: what is read is GitHub's own REST-style
2346/// `message` envelope, which is what a forbidden status carries, and the `message` and
2347/// `type` of each GraphQL error, which is where a *successful* response says it.
2348///
2349/// A body that is not JSON at all has nothing structured to read, so only a failing
2350/// response's own text is taken — a successful response that is not JSON is malformed
2351/// rather than refused, and [`GitHubProjectsSource::answer`] says so.
2352fn refusal_wording(status: StatusCode, body: &str) -> String {
2353 let Ok(parsed) = serde_json::from_str::<Value>(body) else {
2354 return if status.is_success() {
2355 String::new()
2356 } else {
2357 body.to_owned()
2358 };
2359 };
2360 let mut said: Vec<&str> = parsed
2361 .get("message")
2362 .and_then(Value::as_str)
2363 .into_iter()
2364 .collect();
2365 if let Some(errors) = parsed.get("errors").and_then(Value::as_array) {
2366 for error in errors {
2367 said.extend(
2368 ["message", "type"]
2369 .into_iter()
2370 .filter_map(|key| error.get(key).and_then(Value::as_str)),
2371 );
2372 }
2373 }
2374 said.join("; ")
2375}
2376
2377impl Limiter {
2378 /// Which limiter refused this response, or `None` when none of them did.
2379 ///
2380 /// The wording is read first and the status only decides what carries none of it,
2381 /// because GitHub answers a secondary limit with a forbidden status far more often
2382 /// than with too-many-requests — while a forbidden status saying nothing about a limit
2383 /// really is a credential this token lacks.
2384 ///
2385 /// A response is a refusal because of its status or its own wording. A spent budget
2386 /// only ever explains one; it never turns an answer into a refusal.
2387 fn classify(status: StatusCode, budget_exhausted: bool, body: &str) -> Option<Self> {
2388 let normalized = refusal_wording(status, body).to_ascii_lowercase();
2389 if SECONDARY_WORDINGS
2390 .iter()
2391 .any(|wording| normalized.contains(wording))
2392 {
2393 return Some(Self::Secondary);
2394 }
2395 if status == StatusCode::TOO_MANY_REQUESTS {
2396 return Some(Self::Primary);
2397 }
2398 // An exhausted budget *explains* a response that failed; it does not make one that
2399 // succeeded into a failure. GitHub sets `x-ratelimit-remaining: 0` on the last
2400 // request the budget allowed as well as on the ones it then refuses, so reading
2401 // the header alone threw away a good answer — and, once refusals were retried,
2402 // replayed a request that had already taken effect.
2403 if !status.is_success() && budget_exhausted {
2404 return Some(Self::Primary);
2405 }
2406 // A successful response saying it: GitHub reports a GraphQL rate limit in the
2407 // `errors` of an HTTP 200, where nothing about the status says so at all.
2408 if status.is_success()
2409 && PRIMARY_WORDINGS
2410 .iter()
2411 .any(|wording| normalized.contains(wording))
2412 {
2413 return Some(Self::Primary);
2414 }
2415 None
2416 }
2417
2418 /// What this limiter is called where an operator can look it up.
2419 const fn name(self) -> &'static str {
2420 match self {
2421 Self::Primary => "GitHub's primary API rate limit",
2422 Self::Secondary => "GitHub's secondary rate limit",
2423 }
2424 }
2425
2426 /// What the endpoint an operator would go and check says about this limiter.
2427 const fn where_to_look(self) -> &'static str {
2428 match self {
2429 Self::Primary => {
2430 "That is the budget `gh api rate_limit` reports, so that endpoint says when it \
2431 comes back."
2432 }
2433 Self::Secondary => {
2434 "That limiter is not the primary API budget: `gh api rate_limit` reports the \
2435 primary budget and does not report this one, so budget showing there says \
2436 nothing about this refusal, and every further attempt extends it."
2437 }
2438 }
2439 }
2440
2441 /// The next step this limiter actually calls for.
2442 const fn what_to_do(self) -> &'static str {
2443 match self {
2444 Self::Primary => {
2445 "wait for the reset `gh api rate_limit` reports, then run the command again."
2446 }
2447 Self::Secondary => {
2448 "leave this board alone for a few minutes, then run the command again — or \
2449 raise pacing.min_mutation_interval_ms on this source so it writes more slowly."
2450 }
2451 }
2452 }
2453}
2454
2455/// One rate-limit refusal, and the wait GitHub asked for if it asked for one.
2456#[derive(Debug, Clone, Copy)]
2457struct Limited {
2458 limiter: Limiter,
2459 hint: Option<u64>,
2460 /// Which header the hint came from: `retry-after`, or an exhausted primary budget's
2461 /// `x-ratelimit-reset`.
2462 hinted_by: Hint,
2463}
2464
2465/// Where a refusal's hint came from, which is what its wait is named after.
2466#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2467enum Hint {
2468 /// `retry-after`, or no hint at all.
2469 RetryAfter,
2470 /// An exhausted primary budget's `x-ratelimit-reset`.
2471 PrimaryReset,
2472}
2473
2474impl Limited {
2475 /// The wait this refusal is answered with, when the backing-off schedule is at `backoff`,
2476 /// and the cause it is named by: the hint, raised to the schedule's own next wait —
2477 /// GitHub really does send `retry-after: 0`, and retrying at once is the one move that
2478 /// extends a secondary limit — or the schedule alone when there is none.
2479 fn wait(self, backoff: Duration) -> (Duration, WaitCause) {
2480 match (self.hint, self.hinted_by) {
2481 (Some(hint), Hint::RetryAfter) => (
2482 Duration::from_secs(hint).max(backoff),
2483 pacing::cause::RETRY_AFTER,
2484 ),
2485 (Some(hint), Hint::PrimaryReset) => (
2486 Duration::from_secs(hint).max(backoff),
2487 pacing::cause::PRIMARY_RESET,
2488 ),
2489 (None, _) => (backoff, pacing::cause::BACKOFF),
2490 }
2491 }
2492
2493 /// What the caller is told once this source has waited as long as it may.
2494 ///
2495 /// Both limiters report as [`SourceError::RateLimited`], because that is what
2496 /// happened: the kind a caller matches on says a rate limit refused this, and nothing
2497 /// about *which* limiter it was makes it a different kind of failure. What differs is
2498 /// the operator's next step, and that is what the message carries — a secondary
2499 /// refusal read as a primary one sends an operator to `gh api rate_limit`, where the
2500 /// budget looks fine, and then back to retry the very burst that was refused.
2501 fn exhausted(
2502 self,
2503 doing: &str,
2504 waits: u32,
2505 waited: Duration,
2506 needed: Duration,
2507 budget: Duration,
2508 ) -> SourceError {
2509 SourceError::RateLimited {
2510 retry_after_seconds: self.hint,
2511 message: Some(format!(
2512 "{} refused this source while {doing}; it waited {} out over {} and was refused \
2513 again, and the next wait of {} would take it past the {} one call may spend \
2514 waiting. {} next: {}",
2515 self.limiter.name(),
2516 plural(waits, "refusal"),
2517 seconds(waited),
2518 seconds(needed),
2519 seconds(budget),
2520 self.limiter.where_to_look(),
2521 self.limiter.what_to_do(),
2522 )),
2523 }
2524 }
2525}
2526
2527/// One HTTP attempt's result, with what its response said about the rate limit.
2528///
2529/// The two travel together so the record and the outcome are written from the same place:
2530/// what a response said about the budget is only readable while that response is in hand,
2531/// and what the attempt *meant* is only decidable once its body has been read.
2532struct Attempted {
2533 result: Result<Value, Attempt>,
2534 limits: accounting::RateLimit,
2535 /// GitHub's own reported cost for this call, for a document that asked for it.
2536 reported_cost: Option<u64>,
2537 response_outcome: accounting::ResponseOutcome,
2538}
2539
2540/// One attempt's outcome: an error to report, or a rate limit to wait out.
2541enum Attempt {
2542 Failed(SourceError),
2543 Limited(Limited),
2544}
2545
2546fn plural(count: u32, thing: &str) -> String {
2547 if count == 1 {
2548 format!("{count} {thing}")
2549 } else {
2550 format!("{count} {thing}s")
2551 }
2552}
2553
2554fn seconds(duration: Duration) -> String {
2555 format!("{:.1}s", duration.as_secs_f64())
2556}
2557
2558/// The member [`stamp_served`] writes onto every node of a response that says when it last
2559/// changed: the `Date` that response was served at. No GitHub type has a field of this name.
2560const SERVED_AT: &str = "onetaskgraphServedAt";
2561
2562/// Write `served` onto every object of `data` carrying an `updatedAt`, so whatever reads a
2563/// node's time later knows the server's clock on the response that read it — which is what
2564/// decides whether that node's change token is given (see [`change_token::SETTLED`]).
2565fn stamp_served(data: &mut Value, served: &str) {
2566 match data {
2567 Value::Object(members) => {
2568 if members.contains_key("updatedAt") {
2569 members.insert(SERVED_AT.to_owned(), Value::String(served.to_owned()));
2570 }
2571 for member in members.values_mut() {
2572 stamp_served(member, served);
2573 }
2574 }
2575 Value::Array(items) => {
2576 for item in items {
2577 stamp_served(item, served);
2578 }
2579 }
2580 _ => {}
2581 }
2582}
2583
2584/// A header GitHub spells as a whole number of seconds, or `None` when this one is not.
2585///
2586/// A value that is present and unreadable is deliberately *not* an error. `retry-after` is
2587/// allowed by HTTP to be a date rather than a count, an intermediary can rewrite either
2588/// header, and neither is what makes a response a refusal — so the whole cost of one this
2589/// cannot read is that the refusal carries no hint and the backing-off schedule answers it
2590/// instead. Refusing the response over the header would turn a readable refusal into an
2591/// unreadable one, and refusing to *wait* would be the one wrong direction to fail in.
2592fn whole_seconds(value: Option<&reqwest::header::HeaderValue>) -> Option<u64> {
2593 value
2594 .and_then(|value| value.to_str().ok())
2595 .and_then(|value| value.trim().parse::<u64>().ok())
2596}
2597
2598/// A transport failure with every cause beneath it, outermost first.
2599///
2600/// The client's own text for a request it could not complete is `error sending request for
2601/// url (…)`, and the socket error that says why — a refused or reset connection, an exhausted
2602/// port range, a connection closed before its answer — is only in the chain of sources below
2603/// it. Those differ in what to do next, so all of them are kept.
2604pub(crate) fn transport_error(error: &(dyn std::error::Error + 'static)) -> String {
2605 let mut text = error.to_string();
2606 let mut cause = error.source();
2607 while let Some(next) = cause {
2608 let said = next.to_string();
2609 if !text.ends_with(&said) {
2610 text = format!("{text}: {said}");
2611 }
2612 cause = next.source();
2613 }
2614 text
2615}
2616
2617/// Every mutation this source sends creates content — an issue, a board item, a field of
2618/// one, a sub-issue link, a dependency, a comment — or edits or removes content of that
2619/// kind, and no query in [`graphql::DOCUMENTS`] does, so what the secondary limiter counts
2620/// and what the keyword says are the same set. That is what makes the keyword a sound test
2621/// rather than a convenient one: counting an edit or a removal the limiter might not have
2622/// counted costs a wait, and not counting one it did count costs the next fifty minutes.
2623pub(crate) fn is_mutation(query: &str) -> bool {
2624 query.trim_start().starts_with("mutation")
2625}
2626
2627/// What this source was doing, for a diagnostic that has to say so.
2628///
2629/// Read out of [`graphql::DOCUMENTS`], which is the inventory rather than a copy of it, so
2630/// a document added without a description is caught by that list's own gate instead of
2631/// falling through to the vague arm below.
2632fn operation_description(query: &str) -> &'static str {
2633 graphql::DOCUMENTS
2634 .iter()
2635 .find(|(document, _)| *document == query)
2636 .map_or("talking to GitHub", |(_, doing)| *doing)
2637}
2638
2639/// GitHub's published ceiling on content-generating requests in any one minute.
2640///
2641/// Pinned in `tests/fixtures/rate-limits.json` and gated against it, because it is
2642/// GitHub's number rather than this source's. This source paces at it over its window: a
2643/// content-creating request goes out at once while the minute's creations leave room for it,
2644/// and waits until they do otherwise — see the `pacing` module.
2645pub const CONTENT_CREATION_PER_MINUTE: u64 = 80;
2646/// The same ceiling as GitHub publishes it for any one hour, paced over its window as the
2647/// per-minute one is. Pinned beside its sibling so the gate would notice either one moving.
2648pub const CONTENT_CREATION_PER_HOUR: u64 = 500;
2649/// GitHub's published ceiling on secondary points in any one minute: what every GraphQL
2650/// request is charged against, a mutation [`SECONDARY_POINTS_PER_MUTATION`] and a query
2651/// [`SECONDARY_POINTS_PER_QUERY`]. Pinned and paced as [`CONTENT_CREATION_PER_MINUTE`] is.
2652pub const SECONDARY_POINTS_PER_MINUTE: u64 = 2_000;
2653/// The secondary points one GraphQL request carrying a mutation is charged.
2654pub const SECONDARY_POINTS_PER_MUTATION: u64 = 5;
2655/// The secondary points one GraphQL request carrying no mutation is charged.
2656pub const SECONDARY_POINTS_PER_QUERY: u64 = 1;
2657/// The fastest *steady* spacing of content-creating mutations that cannot exceed
2658/// [`CONTENT_CREATION_PER_MINUTE`], in milliseconds: 60000/80 is 750.
2659///
2660/// It is no longer the default. This source paces by GitHub's windows instead, so a copy's
2661/// few dozen writes go out as fast as GitHub answers them while the minute has room, rather
2662/// than 750 ms apart — which was most of a plan copy's wall time. An installation that wants
2663/// mutations spaced as well says so through `pacing.min_mutation_interval_ms`, and this is the
2664/// value that holds a long sustained burst under the per-minute bound by spacing alone.
2665pub const MIN_MUTATION_INTERVAL_MS: u64 = 60_000 / CONTENT_CREATION_PER_MINUTE;
2666/// First wait when a rate-limit refusal carries no hint; each further wait doubles it.
2667///
2668/// A doubling schedule from one second reaches a minute in six waits, which is GitHub's
2669/// own advice for a secondary limit — wait, and wait longer each time — without spending
2670/// the first minute of a transient refusal doing nothing.
2671pub const RETRY_BACKOFF_MS: u64 = 1_000;
2672/// Total time one call may spend waiting out rate limits before it reports a failure.
2673///
2674/// Two minutes is long enough to ride out the refusals a paced copy still collects and
2675/// short enough that a command an operator is watching returns. The bound is what makes
2676/// the wait a wait rather than a hang: a call refused past it ends in a diagnostic naming
2677/// the limiter, not in a process nobody can tell from a wedged one.
2678pub const RETRY_BUDGET_MS: u64 = 120_000;
2679
2680fn default_token_env() -> String {
2681 "GH_PROJECTS_TOKEN".to_owned()
2682}
2683fn default_endpoint() -> String {
2684 "https://api.github.com/graphql".to_owned()
2685}
2686
2687/// The name of a `Status` single-select option on the board.
2688///
2689/// Validated on the way in rather than checked later, so a blank option name — which
2690/// nothing on a board can be — is a state this type cannot hold.
2691#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2692#[serde(try_from = "String")]
2693#[schemars(extend("minLength" = 1))]
2694pub struct ColumnName(String);
2695
2696impl ColumnName {
2697 /// The option name, as the board spells it.
2698 fn as_str(&self) -> &str {
2699 &self.0
2700 }
2701}
2702
2703impl TryFrom<String> for ColumnName {
2704 type Error = String;
2705
2706 fn try_from(name: String) -> Result<Self, Self::Error> {
2707 if name.trim().is_empty() {
2708 return Err("a status_mapping option name cannot be blank".to_owned());
2709 }
2710 Ok(Self(name))
2711 }
2712}
2713
2714/// The two closed states this product can mean.
2715///
2716/// GitHub's `IssueClosedStateReason` also spells `DUPLICATE`, which nothing here ever writes:
2717/// an issue closed as one *reads* `cancelled`, because its work is another issue's, but this
2718/// product never decides on a person's behalf that two issues are the same work.
2719#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, schemars::JsonSchema)]
2720#[serde(rename_all = "kebab-case")]
2721pub enum ClosedState {
2722 /// `COMPLETED` — precisely done.
2723 Completed,
2724 /// `NOT_PLANNED` — precisely cancelled.
2725 NotPlanned,
2726}
2727
2728impl ClosedState {
2729 const fn reason(self) -> &'static str {
2730 match self {
2731 Self::Completed => "COMPLETED",
2732 Self::NotPlanned => "NOT_PLANNED",
2733 }
2734 }
2735}
2736
2737/// Configuration for one GitHub Projects v2 board.
2738///
2739/// It serializes to a document this same type reads back as itself, so a caller writing a
2740/// configuration — a consumer filing onto a board it configures — can build one here rather
2741/// than spell the shape again; an empty `metadata_fields` is left out, as one never set.
2742#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2743#[serde(default, deny_unknown_fields)]
2744pub struct GitHubProjectsConfig {
2745 /// Login of the user or organization which owns the board.
2746 pub owner: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates GitHub's owner grammar before private construction.
2747 /// The project number shown in the board's GitHub URL.
2748 pub project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` bounds this to a positive GraphQL Int.
2749 // 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.
2750 /// `owner/name` of the repository this source creates an issue in when the item's own
2751 /// `repositories` field does not decide it.
2752 ///
2753 /// An item naming exactly one repository is created there; a task or a document naming
2754 /// none or several is created in its parent project's repository; and a project, or a
2755 /// task or document with no parent, naming none or several is created here. A board
2756 /// has no repository of its own and `createIssue` requires one, so a write without
2757 /// this is refused naming the field. Reads never need it.
2758 pub repository: Option<String>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the `owner/name` grammar before private construction.
2759 // llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
2760 /// Environment variable containing a fine-grained token with Projects and Issues
2761 /// read/write plus Pull requests read-only access for every repository represented on
2762 /// the board.
2763 #[serde(default = "default_token_env")]
2764 pub token_env: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the environment-variable grammar.
2765 /// GraphQL endpoint. GitHub Enterprise installations may override it.
2766 #[serde(default = "default_endpoint")]
2767 pub endpoint: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` converts it to the private validated `Url`.
2768 /// Per-instance mapping from a status category to the option of the board's one
2769 /// `Status` field it lands on, for a task and for a project.
2770 ///
2771 /// The shared `StatusMapping` grammar: each value is one option name for both kinds,
2772 /// `null` to disable the category for both, or `{task, project}` naming it per kind,
2773 /// where a kind left out leaves the category unmapped for that kind. A category this
2774 /// does not mention keeps its shipped default for both kinds: `backlog` to "Backlog",
2775 /// `todo` to "Todo", `queued` to "Queued", `in-progress` to "In Progress", `done` to
2776 /// "Done" plus closed as completed, `cancelled` to "Cancelled" plus closed as not
2777 /// planned, and `draft` and `unknown` unmapped. A category it does mention gets no
2778 /// shipped default for a kind it leaves out. `done` and `cancelled` close the issue for
2779 /// either kind. No two categories may name one option for the same kind, ignoring case.
2780 /// `unknown` may name one existing option; every unknown word then lands on it and
2781 /// reads back as `unknown` under its name. Unlike `local-md`, this source cannot keep
2782 /// each unknown word because it never creates board options.
2783 #[serde(default)]
2784 pub status_mapping: StatusMapping,
2785 /// Per-instance mapping from a task's priority to an option of this board's
2786 /// single-select field named `Priority`.
2787 ///
2788 /// Absent, this source holds no priority: every task reads as `none`, and a write of any
2789 /// other priority is refused before it reaches this board. Present, each of `urgent`,
2790 /// `high`, `medium` and `low` it does not mention keeps its shipped default — `Urgent`,
2791 /// `High`, `Medium` and `Low` — and an item with no value in the `Priority` field reads
2792 /// as `none`, so writing `none` clears the value. Option names match case-insensitively;
2793 /// no two levels may name one option. Reads and writes never create the field or an
2794 /// option: `onetaskgraph sources fields <source> --apply` does, and a write naming one
2795 /// the board lacks is refused pointing there.
2796 #[serde(default)]
2797 pub priority_mapping: Option<PriorityMappingConfig>,
2798 /// Caller metadata values this source also writes to board text fields, so the board can
2799 /// filter on them.
2800 ///
2801 /// Absent or empty, nothing is projected. Each entry names a board text `field`, a
2802 /// top-level metadata `key` and an optional `path` of object keys walked inside that key's
2803 /// value. On every item write — create, update and copy, of a task, a project or a
2804 /// document — a string found there is written to the field, and nothing is sent when the
2805 /// field already holds it; nothing there, or `null`, clears the field when it holds a
2806 /// value; any other JSON type is refused before any mutation. The metadata comment in the
2807 /// issue body stays the one home of the value, and every read reports metadata from it.
2808 /// Reads and writes never create the field: `onetaskgraph sources fields <source>
2809 /// --apply` does, as a text field, and a write to a board lacking it, or holding a
2810 /// non-text field of that name, is refused pointing there.
2811 #[serde(default, skip_serializing_if = "Vec::is_empty")]
2812 // Kept in the schema as `"default": []` although a serialized configuration leaves an empty
2813 // list out, so both SDKs model an absent list as an empty one rather than as `null`.
2814 #[schemars(!skip_serializing_if)]
2815 pub metadata_fields: Vec<MetadataFieldConfig>,
2816 /// How fast this source writes, and how long it waits out a rate-limit refusal.
2817 ///
2818 /// Every field keeps its shipped default when it is absent, and the defaults are
2819 /// GitHub's own published limits rather than taste. See [`Pacing`].
2820 #[serde(default)]
2821 pub pacing: PacingConfig,
2822}
2823
2824/// Which option of the board's `Priority` field each priority lands on.
2825///
2826/// One member per level rather than a map, so a key that is not a level is refused where
2827/// the configuration is read, naming the levels there are. `none` is not a member: it is no
2828/// value in the field, not an option of it.
2829#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2830#[serde(default, deny_unknown_fields)]
2831pub struct PriorityMappingConfig {
2832 /// The option `urgent` lands on; `Urgent` when absent.
2833 pub urgent: Option<PriorityOptionName>,
2834 /// The option `high` lands on; `High` when absent.
2835 pub high: Option<PriorityOptionName>,
2836 /// The option `medium` lands on; `Medium` when absent.
2837 pub medium: Option<PriorityOptionName>,
2838 /// The option `low` lands on; `Low` when absent.
2839 pub low: Option<PriorityOptionName>,
2840}
2841
2842/// The name of an option of the board's `Priority` single-select field.
2843///
2844/// Validated on the way in, for the reason [`ColumnName`] is: nothing on a board can have a
2845/// blank name.
2846#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2847#[serde(try_from = "String")]
2848#[schemars(extend("minLength" = 1))]
2849pub struct PriorityOptionName(String);
2850
2851impl PriorityOptionName {
2852 /// The option name, as the board spells it.
2853 fn as_str(&self) -> &str {
2854 &self.0
2855 }
2856}
2857
2858impl TryFrom<String> for PriorityOptionName {
2859 type Error = String;
2860
2861 fn try_from(name: String) -> Result<Self, Self::Error> {
2862 if name.trim().is_empty() {
2863 return Err("a priority_mapping option name cannot be blank".to_owned());
2864 }
2865 Ok(Self(name))
2866 }
2867}
2868
2869/// The name of the board field a priority is held in.
2870pub const PRIORITY_FIELD: &str = "Priority";
2871
2872/// The four priorities a board option can hold, in the order a new `Priority` field lists
2873/// them. `none` is not among them: it is the field holding no value.
2874///
2875/// This list mirrors `Priority`, so it carries its own drift gate, in the shape [`CATEGORIES`]
2876/// does: [`level_position`] is a wildcard-free match, so a priority added to the shared
2877/// vocabulary fails to compile until it is placed there, and this crate's suite reconciles
2878/// this list and [`PriorityMappingConfig`]'s members against that enum's own derived schema.
2879pub const PRIORITY_LEVELS: [Priority; 4] = [
2880 Priority::Urgent,
2881 Priority::High,
2882 Priority::Medium,
2883 Priority::Low,
2884];
2885
2886/// Where one priority sits in [`PRIORITY_LEVELS`], or `None` for `none`, which is no option;
2887/// see that list for what this pins.
2888#[must_use]
2889pub const fn level_position(priority: Priority) -> Option<usize> {
2890 match priority {
2891 Priority::None => None,
2892 Priority::Urgent => Some(0),
2893 Priority::High => Some(1),
2894 Priority::Medium => Some(2),
2895 Priority::Low => Some(3),
2896 }
2897}
2898
2899/// This instance's complete priority-to-option mapping, read in both directions.
2900///
2901/// One option per level, held in [`PRIORITY_LEVELS`] order, once it is established that no
2902/// two levels name one option.
2903#[derive(Debug, Clone)]
2904struct PriorityMapping {
2905 options: [PriorityOptionName; 4],
2906}
2907
2908impl PriorityMapping {
2909 fn resolve(config: PriorityMappingConfig, instance: &SourceName) -> Result<Self, SourceError> {
2910 let shipped = |name: &str| PriorityOptionName(name.to_owned());
2911 let mapping = Self {
2912 options: [
2913 config.urgent.unwrap_or_else(|| shipped("Urgent")),
2914 config.high.unwrap_or_else(|| shipped("High")),
2915 config.medium.unwrap_or_else(|| shipped("Medium")),
2916 config.low.unwrap_or_else(|| shipped("Low")),
2917 ],
2918 };
2919 for (index, option) in mapping.options.iter().enumerate() {
2920 if let Some(earlier) = mapping.options[..index]
2921 .iter()
2922 .position(|other| other.as_str().eq_ignore_ascii_case(option.as_str()))
2923 {
2924 return Err(SourceError::Config {
2925 message: format!(
2926 "priority_mapping of source {instance} sends both {} and {} to the board \
2927 option {:?}; one option cannot read back as two priorities",
2928 PRIORITY_LEVELS[earlier],
2929 PRIORITY_LEVELS[index],
2930 option.as_str()
2931 ),
2932 });
2933 }
2934 }
2935 Ok(mapping)
2936 }
2937
2938 /// The option `priority` lands on, or `None` for `none`, which is no option at all.
2939 fn option(&self, priority: Priority) -> Option<&str> {
2940 level_position(priority).map(|index| self.options[index].as_str())
2941 }
2942
2943 /// The priority a board option name reports, or `None` when nothing maps to it.
2944 fn priority_of(&self, option: &str) -> Option<Priority> {
2945 self.options
2946 .iter()
2947 .position(|name| name.as_str().eq_ignore_ascii_case(option))
2948 .map(|index| PRIORITY_LEVELS[index])
2949 }
2950
2951 /// Every mapped option name, in the order a new `Priority` field lists them.
2952 fn names(&self) -> impl Iterator<Item = &str> {
2953 self.options.iter().map(PriorityOptionName::as_str)
2954 }
2955}
2956
2957/// One metadata value this source projects onto a board text field.
2958#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2959#[serde(deny_unknown_fields)]
2960pub struct MetadataFieldConfig {
2961 /// The name of the board text field the value is written to.
2962 pub field: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `MetadataField::resolve` refuses a blank, reserved, GitHub-owned or repeated name before the private validated entry is built.
2963 /// The top-level metadata key the value is read from. A key under the reserved
2964 /// `onetaskgraph.` prefix is refused.
2965 pub key: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `MetadataField::resolve` refuses a blank or reserved key before the private validated entry is built.
2966 /// Object keys walked inside that key's value, outermost first. Absent or empty, the
2967 /// key's own value is the one projected. Left out of a serialized entry when empty.
2968 #[serde(default, skip_serializing_if = "Vec::is_empty")]
2969 // Kept in the schema as `"default": []`, for the reason `metadata_fields`' is.
2970 #[schemars(!skip_serializing_if)]
2971 pub path: Vec<String>,
2972}
2973
2974/// The board fields no metadata value may be projected onto, compared ignoring case: the
2975/// three this source writes itself, and those GitHub owns on every board.
2976// llmlint: ignore[contracts_have_one_source_or_a_drift_gate] This list is the configuration contract `metadata_fields`' consumers agreed, refused at read so a mistaken entry is named early — not a mirror GitHub's field set has to stay in step with. What keeps a write off a field GitHub owns is the write's own check, `projection_field`, which refuses any field whose `dataType` is not `TEXT`: every built-in board field reports its own type (TITLE, ASSIGNEES, LABELS, …), and the live contract introspection holds GitHub to `ProjectV2Field.dataType`. So a built-in field GitHub adds later and this list lacks is still never written.
2977const UNPROJECTABLE_FIELDS: [&str; 13] = [
2978 STATUS_FIELD,
2979 PRIORITY_FIELD,
2980 ORIGIN_FIELD,
2981 "Title",
2982 "Assignees",
2983 "Labels",
2984 "Linked pull requests",
2985 "Milestone",
2986 "Repository",
2987 "Reviewers",
2988 "Parent issue",
2989 "Sub-issues progress",
2990 "Type",
2991];
2992
2993/// One validated [`MetadataFieldConfig`] entry.
2994#[derive(Debug, Clone)]
2995struct MetadataField {
2996 field: ProjectedField,
2997 key: ProjectedKey,
2998 path: Vec<String>,
2999}
3000
3001/// The name of a board text field a metadata value is projected onto: never blank, never one
3002/// this source or GitHub writes, and never one another entry of the same source names.
3003#[derive(Debug, Clone, PartialEq, Eq)]
3004struct ProjectedField(String);
3005
3006/// The top-level metadata key a projected value is read from: never blank, and never under
3007/// the namespace this product reserves.
3008#[derive(Debug, Clone, PartialEq, Eq)]
3009struct ProjectedKey(String);
3010
3011impl std::ops::Deref for ProjectedField {
3012 type Target = str;
3013 fn deref(&self) -> &str {
3014 &self.0
3015 }
3016}
3017
3018impl std::ops::Deref for ProjectedKey {
3019 type Target = str;
3020 fn deref(&self) -> &str {
3021 &self.0
3022 }
3023}
3024
3025/// What one item's metadata says one projected field should hold.
3026#[derive(Debug, Clone, PartialEq, Eq)]
3027enum Projected {
3028 /// A string to hold.
3029 Text(String),
3030 /// Nothing: the key or a step of the path is absent, or the value is `null`.
3031 Nothing,
3032}
3033
3034impl MetadataField {
3035 /// Validate every entry of one instance's `metadata_fields`, refusing the first that
3036 /// cannot stand, naming the source and the entry.
3037 fn resolve(
3038 configured: Vec<MetadataFieldConfig>,
3039 instance: &SourceName,
3040 ) -> Result<Vec<Self>, SourceError> {
3041 let mut resolved: Vec<Self> = Vec::with_capacity(configured.len());
3042 for (index, entry) in configured.into_iter().enumerate() {
3043 let refuse = |why: String| SourceError::Config {
3044 message: format!(
3045 "metadata_fields[{index}] of source {instance} (field {:?}, key {:?}) {why}",
3046 entry.field, entry.key
3047 ),
3048 };
3049 if entry.field.trim().is_empty() {
3050 return Err(refuse("names a blank field".to_owned()));
3051 }
3052 if entry.key.trim().is_empty() {
3053 return Err(refuse("names a blank key".to_owned()));
3054 }
3055 if entry.key.split('.').next() == Some(MetadataKey::RESERVED_NAMESPACE) {
3056 return Err(refuse(format!(
3057 "names a key under the reserved \"{}.\" prefix, which this product keeps \
3058 for its own keys; next: project a caller key instead",
3059 MetadataKey::RESERVED_NAMESPACE
3060 )));
3061 }
3062 if let Some(owned) = UNPROJECTABLE_FIELDS
3063 .iter()
3064 .find(|owned| owned.eq_ignore_ascii_case(&entry.field))
3065 {
3066 return Err(refuse(format!(
3067 "names the board field {owned:?}, which this source or GitHub already \
3068 writes; next: name a text field of your own"
3069 )));
3070 }
3071 if let Some(earlier) = resolved
3072 .iter()
3073 .position(|other| other.field.eq_ignore_ascii_case(&entry.field))
3074 {
3075 return Err(refuse(format!(
3076 "names the same board field as metadata_fields[{earlier}]; one field \
3077 cannot hold two values"
3078 )));
3079 }
3080 resolved.push(Self {
3081 field: ProjectedField(entry.field),
3082 key: ProjectedKey(entry.key),
3083 path: entry.path,
3084 });
3085 }
3086 Ok(resolved)
3087 }
3088
3089 /// The path as a refusal spells it: `[]` for the key's own value.
3090 fn spelled_path(&self) -> String {
3091 serde_json::to_string(&self.path).unwrap_or_else(|_| format!("{:?}", self.path))
3092 }
3093
3094 /// What `metadata` says this field should hold, or the refusal for a value no text field
3095 /// can hold.
3096 fn projected(
3097 &self,
3098 metadata: &BTreeMap<String, Value>,
3099 instance: &SourceName,
3100 ) -> Result<Projected, SourceError> {
3101 let mut value = metadata.get(&*self.key);
3102 for step in &self.path {
3103 value = value.and_then(|held| held.get(step.as_str()));
3104 }
3105 let found = match value {
3106 None | Some(Value::Null) => return Ok(Projected::Nothing),
3107 Some(Value::String(text)) => return Ok(Projected::Text(text.clone())),
3108 Some(Value::Bool(_)) => "a boolean",
3109 Some(Value::Number(_)) => "a number",
3110 Some(Value::Array(_)) => "an array",
3111 Some(Value::Object(_)) => "an object",
3112 };
3113 Err(SourceError::Refused {
3114 message: format!(
3115 "source {instance} projects metadata key {:?} at path {} onto its board text \
3116 field {:?}, and this item holds {found} there; only a string, null or nothing \
3117 can be written to a text field; next: store a string there, or remove the \
3118 value",
3119 &*self.key,
3120 self.spelled_path(),
3121 &*self.field
3122 ),
3123 })
3124 }
3125}
3126
3127/// What one item's `Priority` field says, read through this instance's mapping.
3128#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
3129enum HeldPriority {
3130 /// A priority this source reports: an option the mapping names, or no value (`none`).
3131 Read(Priority),
3132 /// An option the mapping does not name, which is never read as a level or as `none`.
3133 Unmapped(String),
3134}
3135
3136/// How fast this source writes, and how long it waits out a rate-limit refusal.
3137///
3138/// Configurable because a GitHub Enterprise installation sets its own limits and an
3139/// operator who has already been refused may want to go slower still — not because the
3140/// defaults are guesses.
3141#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3142#[serde(default, deny_unknown_fields)]
3143pub struct PacingConfig {
3144 /// Shortest interval between two content-creating mutations, in milliseconds.
3145 ///
3146 /// Zero sends them as fast as they are asked for, which is what a fixture server on
3147 /// loopback wants and what no board on github.com does. At most [`MAX_PACING_MS`].
3148 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.
3149 /// First wait when a rate-limit refusal carries no hint, in milliseconds. Each
3150 /// further wait of the same call doubles it. At most [`MAX_PACING_MS`], and never
3151 /// zero while there is a budget to spend, because a schedule of zero-length waits
3152 /// consumes none of it and so never ends.
3153 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.
3154 /// Total time one call may spend waiting out rate limits, in milliseconds.
3155 ///
3156 /// Zero reports the refusal rather than waiting at all. At most [`MAX_PACING_MS`]:
3157 /// the bound is what makes this a wait rather than a hang.
3158 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.
3159}
3160
3161/// The largest any pacing setting may be, in milliseconds.
3162///
3163/// One hour. GitHub's own harshest published bound on content-generating requests works
3164/// out at one every 7.2 seconds, so an hour is already three orders of magnitude past
3165/// anything a real limit asks for, and past it the settings stop describing pacing at all:
3166/// a wait budget beyond it is the unbounded wait this whole mechanism exists to replace,
3167/// and an interval beyond it is a command that never sends its second mutation. It also
3168/// keeps the clock arithmetic in [`GitHubProjectsSource::reserve_mutation_slot`] inside
3169/// what a `Duration` can hold on every platform.
3170pub const MAX_PACING_MS: u64 = 3_600_000;
3171
3172/// [`PacingConfig`] with every default resolved and every value checked, which is what the
3173/// source holds.
3174///
3175/// Unset, `min_mutation_interval_ms` spaces nothing, and every request is paced by GitHub's
3176/// secondary windows alone — see the `pacing` module; set, it spaces content-creating mutations
3177/// on top of those windows, [`MIN_MUTATION_INTERVAL_MS`] being the spacing that holds a sustained
3178/// burst under the per-minute bound by spacing alone; and zero sends every request as fast as it
3179/// is asked for, windows included.
3180#[derive(Debug, Clone, Copy)]
3181struct Pacing {
3182 /// Whether requests are held for GitHub's secondary windows: always, unless
3183 /// `min_mutation_interval_ms` is set to exactly zero, which has always asked for no
3184 /// pacing at all — what a fixture server on loopback wants and no board on github.com does.
3185 windows: bool,
3186 min_mutation_interval: Duration,
3187 retry_backoff: Duration,
3188 retry_budget: Duration,
3189}
3190
3191impl Pacing {
3192 /// Resolve one instance's pacing, refusing a configuration that would not pace at all.
3193 fn resolve(config: PacingConfig, instance: &SourceName) -> Result<Self, SourceError> {
3194 let bounded = |value: Option<u64>, default: u64, field: &str| match value {
3195 Some(value) if value > MAX_PACING_MS => Err(SourceError::Config {
3196 message: format!(
3197 "pacing.{field} of source {instance} is {value} ms, and the most any pacing \
3198 setting may be is {MAX_PACING_MS} ms — an hour, which is already far past \
3199 GitHub's own harshest published limit"
3200 ),
3201 }),
3202 Some(value) => Ok(Duration::from_millis(value)),
3203 None => Ok(Duration::from_millis(default)),
3204 };
3205 let retry_backoff = bounded(
3206 config.retry_backoff_ms,
3207 RETRY_BACKOFF_MS,
3208 "retry_backoff_ms",
3209 )?;
3210 let retry_budget = bounded(config.retry_budget_ms, RETRY_BUDGET_MS, "retry_budget_ms")?;
3211 if retry_backoff.is_zero() && !retry_budget.is_zero() {
3212 return Err(SourceError::Config {
3213 message: format!(
3214 "pacing.retry_backoff_ms of source {instance} is 0 while \
3215 pacing.retry_budget_ms is {} ms; a schedule of zero-length waits spends \
3216 none of that budget, so it would retry a refusal forever. Set a backoff of \
3217 at least 1 ms, or set retry_budget_ms to 0 to report a refusal without \
3218 waiting at all",
3219 retry_budget.as_millis()
3220 ),
3221 });
3222 }
3223 Ok(Self {
3224 windows: config.min_mutation_interval_ms != Some(0),
3225 min_mutation_interval: bounded(
3226 config.min_mutation_interval_ms,
3227 0,
3228 "min_mutation_interval_ms",
3229 )?,
3230 retry_backoff,
3231 retry_budget,
3232 })
3233 }
3234}
3235
3236/// Factory for [`GitHubProjectsSource`].
3237#[derive(Debug, Clone, Copy, Default)]
3238pub struct Plugin;
3239
3240impl SourcePlugin for Plugin {
3241 fn kind(&self) -> &'static str {
3242 KIND
3243 }
3244 /// The endpoint, the owner and the board's number: what names one board.
3245 fn remote_identity(&self, config: &Value) -> Option<String> {
3246 let config: GitHubProjectsConfig = serde_json::from_value(config.clone()).ok()?;
3247 Some(format!(
3248 "{}\n{}\n{}",
3249 config.endpoint, config.owner, config.project_number
3250 ))
3251 }
3252 fn config_schema(&self) -> Schema {
3253 schema_for!(GitHubProjectsConfig)
3254 }
3255 fn build(
3256 &self,
3257 name: &SourceName,
3258 config: &Value,
3259 secrets: &dyn SecretResolver,
3260 ) -> Result<Box<dyn TaskSource>, SourceError> {
3261 self.build_recording_into(name, config, secrets, Arc::new(Accounting::new()))
3262 }
3263
3264 fn build_with_clock(
3265 &self,
3266 name: &SourceName,
3267 config: &Value,
3268 secrets: &dyn SecretResolver,
3269 clock: SharedClock,
3270 ) -> Result<Box<dyn TaskSource>, SourceError> {
3271 self.build_recording_with_clock(name, config, secrets, Arc::new(Accounting::new()), clock)
3272 }
3273}
3274
3275/// The client `cell` holds, building it with `build` the first time it is asked for.
3276///
3277/// A failed build is the request's failure, with the message the source's construction gave it
3278/// before clients were deferred.
3279// llmlint: ignore[changed_behavior_has_e2e] A client build fails only when the TLS backend cannot initialise, which nothing outside the process can induce: a certificate bundle of garbage still builds one. That the build is deferred is proven end to end by `store_startup::a_command_builds_no_client_for_a_source_it_sends_nothing`.
3280fn built(
3281 cell: &std::sync::OnceLock<Client>,
3282 build: impl FnOnce() -> Result<Client, SourceError>,
3283) -> Result<&Client, SourceError> {
3284 if let Some(client) = cell.get() {
3285 return Ok(client);
3286 }
3287 let client = build()?;
3288 Ok(cell.get_or_init(|| client))
3289}
3290
3291impl Plugin {
3292 /// Build a source recording every request it sends into an accounting the caller holds.
3293 ///
3294 /// [`SourcePlugin::build`] is this with an accounting of its own, which is what the
3295 /// registry gets. This is for a caller that is also calling GitHub itself and wants one
3296 /// session total rather than two — see [`accounting`] and
3297 /// [`GitHubProjectsSource::recording_into`].
3298 ///
3299 /// # Errors
3300 ///
3301 /// Exactly [`SourcePlugin::build`]'s, with the same source name in front of each:
3302 /// [`SourceError::Config`] for configuration this plugin cannot use and
3303 /// [`SourceError::Auth`] for a credential it cannot find.
3304 pub fn build_recording_into(
3305 &self,
3306 name: &SourceName,
3307 config: &Value,
3308 secrets: &dyn SecretResolver,
3309 ledger: Arc<Accounting>,
3310 ) -> Result<Box<dyn TaskSource>, SourceError> {
3311 self.build_recording_with_clock(name, config, secrets, ledger, system_clock())
3312 }
3313
3314 fn build_recording_with_clock(
3315 &self,
3316 name: &SourceName,
3317 config: &Value,
3318 secrets: &dyn SecretResolver,
3319 ledger: Arc<Accounting>,
3320 clock: SharedClock,
3321 ) -> Result<Box<dyn TaskSource>, SourceError> {
3322 let config: GitHubProjectsConfig =
3323 serde_json::from_value(config.clone()).map_err(|e| SourceError::Config {
3324 message: format!("source {name}: {e}"),
3325 })?;
3326 let prefix = format!("source {name}: ");
3327 let mut source = GitHubProjectsSource::recording_into(name, config, secrets, ledger)
3328 .map_err(|error| match error {
3329 // The shared `StatusMapping::distinct` names the source itself.
3330 SourceError::Config { message } if message.starts_with(&prefix) => {
3331 SourceError::Config { message }
3332 }
3333 SourceError::Config { message } => SourceError::Config {
3334 message: format!("{prefix}{message}"),
3335 },
3336 SourceError::Auth { message } => SourceError::Auth {
3337 message: format!("source {name}: {message}"),
3338 },
3339 other => other,
3340 })?;
3341 source.clock = clock;
3342 Ok(Box::new(source))
3343 }
3344}
3345
3346/// Where a status category lands on this board, once configuration is resolved.
3347#[derive(Debug, Clone, PartialEq, Eq)]
3348enum StatusTarget {
3349 /// Not usable against this instance for this kind, and why.
3350 Disabled(UnmappedStatus),
3351 /// The board's `Status` option of this name.
3352 Column(ColumnName),
3353 /// A closed issue, with both its board option and the reason that says which closed it means.
3354 // 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, `BoardStatuses::resolve`, 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.
3355 Terminal(ColumnName, ClosedState),
3356}
3357
3358/// Every status category, in the order the vocabulary declares them.
3359///
3360/// This list mirrors `StatusCategory`, so it carries its own drift gate rather than a
3361/// reviewer's attention: [`category_position`] is a wildcard-free match, so a variant
3362/// added to the shared vocabulary fails to compile until it is named there, and this
3363/// crate's suite reconciles this list against that enum's own derived schema, which is
3364/// generated from the variants rather than written beside them. The schema is what
3365/// catches a list left one short — a list checking only the positions it already holds
3366/// would pass while every mapping indexed by the new position panicked.
3367pub const CATEGORIES: [StatusCategory; 8] = [
3368 StatusCategory::Draft,
3369 StatusCategory::Backlog,
3370 StatusCategory::Todo,
3371 StatusCategory::Queued,
3372 StatusCategory::InProgress,
3373 StatusCategory::Done,
3374 StatusCategory::Cancelled,
3375 StatusCategory::Unknown,
3376];
3377
3378/// Where one category sits in [`CATEGORIES`]; see that list for what this pins.
3379#[must_use]
3380pub const fn category_position(category: StatusCategory) -> usize {
3381 match category {
3382 StatusCategory::Draft => 0,
3383 StatusCategory::Backlog => 1,
3384 StatusCategory::Todo => 2,
3385 StatusCategory::Queued => 3,
3386 StatusCategory::InProgress => 4,
3387 StatusCategory::Done => 5,
3388 StatusCategory::Cancelled => 6,
3389 StatusCategory::Unknown => 7,
3390 }
3391}
3392
3393/// The spelling a status category is configured and reported under.
3394fn category_name(category: StatusCategory) -> &'static str {
3395 match category {
3396 StatusCategory::Draft => "draft",
3397 StatusCategory::Backlog => "backlog",
3398 StatusCategory::Todo => "todo",
3399 StatusCategory::Queued => "queued",
3400 StatusCategory::InProgress => "in-progress",
3401 StatusCategory::Done => "done",
3402 StatusCategory::Cancelled => "cancelled",
3403 StatusCategory::Unknown => "unknown",
3404 }
3405}
3406
3407/// A shipped default's option name.
3408///
3409/// The literals below are this file's own and non-blank, and they are validated by the
3410/// one constructor a configured name goes through rather than beside it.
3411fn shipped_column(name: &'static str) -> ColumnName {
3412 ColumnName::try_from(name.to_owned()).expect("a shipped default names a board option")
3413}
3414
3415/// The shipped default for one category this instance's `status_mapping` does not mention,
3416/// for either kind.
3417fn shipped_default(category: StatusCategory) -> StatusTarget {
3418 match category {
3419 StatusCategory::Backlog => StatusTarget::Column(shipped_column("Backlog")),
3420 StatusCategory::Todo => StatusTarget::Column(shipped_column("Todo")),
3421 StatusCategory::Queued => StatusTarget::Column(shipped_column("Queued")),
3422 StatusCategory::InProgress => StatusTarget::Column(shipped_column("In Progress")),
3423 StatusCategory::Done => {
3424 StatusTarget::Terminal(shipped_column("Done"), ClosedState::Completed)
3425 }
3426 StatusCategory::Cancelled => {
3427 StatusTarget::Terminal(shipped_column("Cancelled"), ClosedState::NotPlanned)
3428 }
3429 StatusCategory::Draft | StatusCategory::Unknown => {
3430 StatusTarget::Disabled(UnmappedStatus::Unconfigured)
3431 }
3432 }
3433}
3434
3435/// The two kinds a status is written and read for, each with its own half of the mapping.
3436const STATUS_KINDS: [ItemKind; 2] = [ItemKind::Task, ItemKind::Project];
3437
3438/// This instance's complete category-to-target mapping for each kind, read in both
3439/// directions.
3440///
3441/// One target per category per kind, held at that category's own [`category_position`], so
3442/// a category missing from the mapping, named twice in it, or filed out of order is a state
3443/// this type cannot hold rather than one [`Self::target`] has to defend against. Both kinds'
3444/// targets are options of the board's one `Status` field.
3445#[derive(Debug, Clone)]
3446struct BoardStatuses {
3447 tasks: [StatusTarget; CATEGORIES.len()],
3448 projects: [StatusTarget; CATEGORIES.len()],
3449}
3450
3451impl BoardStatuses {
3452 /// Resolve `configured` against the shipped defaults, refusing two categories one kind
3453 /// would read back from one option.
3454 ///
3455 /// A category the mapping does not mention keeps its shipped default for both kinds; one
3456 /// it does mention is exactly what it configures, so a per-kind object leaves the kind it
3457 /// omits unmapped rather than defaulted.
3458 fn resolve(configured: &StatusMapping, instance: &SourceName) -> Result<Self, SourceError> {
3459 let resolve_kind =
3460 |kind: ItemKind| -> Result<[StatusTarget; CATEGORIES.len()], SourceError> {
3461 // `CATEGORIES[position] == category` for every category — the crate's suite
3462 // asserts it — so mapping the list in order fills each category's own slot.
3463 let mut targets = CATEGORIES.map(shipped_default);
3464 for (slot, category) in targets.iter_mut().zip(CATEGORIES) {
3465 if !configured.mentions(category) {
3466 continue;
3467 }
3468 *slot = match configured.name_for(category, kind) {
3469 Err(why) => StatusTarget::Disabled(why),
3470 Ok(name) => {
3471 let option = ColumnName::try_from(name.as_str().to_owned())
3472 .map_err(|message| SourceError::Config { message })?;
3473 match category {
3474 StatusCategory::Done => {
3475 StatusTarget::Terminal(option, ClosedState::Completed)
3476 }
3477 StatusCategory::Cancelled => {
3478 StatusTarget::Terminal(option, ClosedState::NotPlanned)
3479 }
3480 _ => StatusTarget::Column(option),
3481 }
3482 }
3483 };
3484 }
3485 StatusMapping::distinct(
3486 instance,
3487 kind,
3488 CATEGORIES
3489 .iter()
3490 .zip(&targets)
3491 .filter_map(|(category, target)| target.option().map(|o| (*category, o))),
3492 )?;
3493 Ok(targets)
3494 };
3495 Ok(Self {
3496 tasks: resolve_kind(ItemKind::Task)?,
3497 projects: resolve_kind(ItemKind::Project)?,
3498 })
3499 }
3500
3501 /// Every category's target for `kind`, in category order.
3502 const fn targets(&self, kind: ItemKind) -> &[StatusTarget; CATEGORIES.len()] {
3503 match kind {
3504 ItemKind::Task => &self.tasks,
3505 ItemKind::Project => &self.projects,
3506 }
3507 }
3508
3509 fn target(&self, kind: ItemKind, category: StatusCategory) -> &StatusTarget {
3510 &self.targets(kind)[category_position(category)]
3511 }
3512
3513 /// The category a board option name reports for `kind`, or `None` when nothing of that
3514 /// kind maps to it.
3515 fn category_of(&self, kind: ItemKind, option: &str) -> Option<StatusCategory> {
3516 CATEGORIES.into_iter().find(|category| {
3517 self.target(kind, *category)
3518 .option()
3519 .is_some_and(|name| name.eq_ignore_ascii_case(option))
3520 })
3521 }
3522
3523 /// Every option name either kind maps a category to, each once ignoring case, in
3524 /// category order with a task's name before a project's — what the guarded setup asks
3525 /// the `Status` field to hold.
3526 fn wanted(&self) -> Vec<String> {
3527 let mut wanted: Vec<String> = Vec::new();
3528 for category in CATEGORIES {
3529 for kind in STATUS_KINDS {
3530 if let Some(name) = self.target(kind, category).option()
3531 && !wanted.iter().any(|held| held.eq_ignore_ascii_case(name))
3532 {
3533 wanted.push(name.to_owned());
3534 }
3535 }
3536 }
3537 wanted
3538 }
3539
3540 /// The status an item of `kind` reports, from the three things a read of it says: its
3541 /// board `Status` option, whether its issue is closed, and the reason it was closed with.
3542 ///
3543 /// **A closed issue's category follows its closure**, by [`closed_category`]: completed
3544 /// or no reason reads `done`, not planned and duplicate read `cancelled`, and any other
3545 /// reason reads `unknown`. `REOPENED`-while-closed is a state this source can never
3546 /// produce, so it is read permissively rather than refused — reads are faithful, and
3547 /// refusals belong on writes.
3548 ///
3549 /// **Its name follows the closure too, unless the option agrees with it.** The option's
3550 /// own name is kept when this kind's mapping reads that option as the same category, or
3551 /// does not name it at all — so a closed issue in a "Shipped" column reports `done`
3552 /// named `Shipped`. An option the mapping reads as another category contradicts the
3553 /// closure, and so does no option at all: the name is then the option the mapping gives
3554 /// the closure's category, so an issue closed as completed at `Todo` reads `done` named
3555 /// by the board's `done` option, and a destination reading `Todo` as todo is never handed
3556 /// it. An `unknown` closure keeps the option, or `Closed` without one.
3557 ///
3558 /// An open item's option reads through its own kind's mapping, and an option that mapping
3559 /// does not name reads as `Unknown` under its own name.
3560 ///
3561 /// One function of those three rather than of a response, so a narrow status write can
3562 /// answer what a re-read would report by applying it to the state it has just written.
3563 fn status(
3564 &self,
3565 kind: ItemKind,
3566 option: Option<&str>,
3567 closed: bool,
3568 reason: Option<&str>,
3569 ) -> Status {
3570 if closed {
3571 let category = closed_category(reason);
3572 let agrees = option.filter(|option| {
3573 category == StatusCategory::Unknown
3574 || self
3575 .category_of(kind, option)
3576 .is_none_or(|read| read == category)
3577 });
3578 let name = agrees.or_else(|| self.target(kind, category).option());
3579 let fallback = match category {
3580 StatusCategory::Done => "Done",
3581 StatusCategory::Cancelled => "Cancelled",
3582 _ => "Closed",
3583 };
3584 return Status {
3585 category,
3586 name: name.or(option).unwrap_or(fallback).to_owned(),
3587 };
3588 }
3589 let name = option.unwrap_or("Open").to_owned();
3590 Status {
3591 category: self
3592 .category_of(kind, &name)
3593 .unwrap_or(StatusCategory::Unknown),
3594 name,
3595 }
3596 }
3597
3598 /// The option a closed item's `Status` has to hold for its card to agree with its
3599 /// closure, and the category that closure reads as — or `None` when the item is open,
3600 /// its closure reads `unknown`, or its option already agrees.
3601 ///
3602 /// What `task status reconcile` writes: only `done` and `cancelled` closures are brought
3603 /// in line, each to the one option the mapping gives its category — an item with no
3604 /// option, or at one the mapping does not name, included.
3605 fn contradicted(
3606 &self,
3607 kind: ItemKind,
3608 option: Option<&str>,
3609 closed: bool,
3610 reason: Option<&str>,
3611 ) -> Option<(ClosedAs, &str)> {
3612 if !closed {
3613 return None;
3614 }
3615 let closed_as = match closed_category(reason) {
3616 StatusCategory::Done => ClosedAs::Done,
3617 StatusCategory::Cancelled => ClosedAs::Cancelled,
3618 _ => return None,
3619 };
3620 let wanted = self.target(kind, closed_as.category()).option()?;
3621 if option.is_some_and(|option| option.eq_ignore_ascii_case(wanted)) {
3622 return None;
3623 }
3624 Some((closed_as, wanted))
3625 }
3626}
3627
3628/// The category a closed issue reads as, from the reason it was closed with.
3629///
3630/// `COMPLETED`, and no reason at all, is finished work; `NOT_PLANNED` and `DUPLICATE` are both
3631/// work nobody will do here — a duplicate's work is the other issue's — and any other reason
3632/// is one this source cannot read a category from.
3633fn closed_category(reason: Option<&str>) -> StatusCategory {
3634 match reason {
3635 None | Some("COMPLETED") => StatusCategory::Done,
3636 Some("NOT_PLANNED" | "DUPLICATE") => StatusCategory::Cancelled,
3637 Some(_) => StatusCategory::Unknown,
3638 }
3639}
3640
3641impl BoardStatuses {
3642 /// For each kind, the option names it maps a category to that `existing` lacks, ignoring
3643 /// case; a kind lacking none is left out.
3644 fn missing_by_kind(&self, existing: &[StatusOption]) -> Vec<KindMissing> {
3645 STATUS_KINDS
3646 .into_iter()
3647 .filter_map(|kind| {
3648 let missing: Vec<String> = self
3649 .targets(kind)
3650 .iter()
3651 .filter_map(StatusTarget::option)
3652 .filter(|wanted| {
3653 !existing
3654 .iter()
3655 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3656 })
3657 .map(str::to_owned)
3658 .collect();
3659 (!missing.is_empty()).then_some(KindMissing { kind, missing })
3660 })
3661 .collect()
3662 }
3663}
3664
3665impl StatusTarget {
3666 /// The board option this target selects, or `None` for an unmapped one.
3667 fn option(&self) -> Option<&str> {
3668 match self {
3669 Self::Column(name) | Self::Terminal(name, _) => Some(name.as_str()),
3670 Self::Disabled(_) => None,
3671 }
3672 }
3673}
3674
3675// 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.
3676/// One repository this source can create an issue in, as `owner/name`.
3677///
3678/// Every `createIssue` this source sends names one of these: the item's own single
3679/// `repositories` entry, else its parent project issue's repository, else the configured
3680/// [`GitHubProjectsConfig::repository`]. [`GitHubProjectsSource::creation_target`] makes
3681/// that choice and says what it refuses before `createIssue`.
3682// llmlint: ignore-end[comments_earn_their_place, contracts_have_one_source_or_a_drift_gate]
3683#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
3684struct RepositoryTarget {
3685 owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
3686 name: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
3687}
3688
3689impl RepositoryTarget {
3690 fn parse(value: &str) -> Result<Self, SourceError> {
3691 let (owner, name) = value.split_once('/').ok_or_else(|| SourceError::Config {
3692 message: format!(
3693 "repository must be spelled owner/name; {value:?} names no repository"
3694 ),
3695 })?;
3696 if !valid_github_owner(owner) || !valid_github_repository_name(name) {
3697 return Err(SourceError::Config {
3698 message: format!(
3699 "repository must be spelled owner/name with a GitHub login and one \
3700 repository name; {value:?} is not"
3701 ),
3702 });
3703 }
3704 Ok(Self {
3705 owner: owner.to_owned(),
3706 name: name.to_owned(),
3707 })
3708 }
3709
3710 /// The one host whose repositories this source creates issues in, spelled once: it is
3711 /// what [`Self::origin`] renders and what [`Self::from_origin`] accepts.
3712 const HOST: &str = "github.com";
3713
3714 fn origin(&self) -> String {
3715 format!("{}/{}/{}", Self::HOST, self.owner, self.name)
3716 }
3717
3718 /// The repository a normalized origin names, or why it is none this source can create
3719 /// an issue in: another host, or more or fewer than `owner/name` under this one.
3720 fn from_origin(origin: &Repository) -> Result<Self, String> {
3721 let not_here = || {
3722 format!(
3723 "{} is not a {}/owner/name repository",
3724 origin.as_str(),
3725 Self::HOST
3726 )
3727 };
3728 let (host, rest) = origin.as_str().split_once('/').ok_or_else(not_here)?;
3729 if host != Self::HOST {
3730 return Err(not_here());
3731 }
3732 Self::parse(rest).map_err(|_| not_here())
3733 }
3734
3735 fn slug(&self) -> String {
3736 format!("{}/{}", self.owner, self.name)
3737 }
3738}
3739
3740/// A source which reads GitHub afresh for every operation.
3741pub struct GitHubProjectsSource {
3742 /// This source's configured name, used both to tell a far end naming this source
3743 /// from one naming a system it knows nothing about, and to name the instance a
3744 /// status refusal is about.
3745 name: SourceName,
3746 owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after full GitHub-owner validation.
3747 project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after GraphQL-Int validation.
3748 repository: Option<RepositoryTarget>,
3749 endpoint: Url,
3750 token: SecretString,
3751 credential_name: String, // llmlint: ignore[invalid_states_unrepresentable] Private diagnostic value constructed only after environment-name validation.
3752 statuses: BoardStatuses,
3753 /// Where each priority lands on this board, or `None` when this instance holds none.
3754 priorities: Option<PriorityMapping>,
3755 /// The metadata values this instance projects onto board text fields, in configured order.
3756 metadata_fields: Vec<MetadataField>,
3757 /// Built on the first request rather than with the source, as [`Self::client`] says.
3758 client: std::sync::OnceLock<Client>,
3759 /// Built on the first attachment request, as [`Self::asset_client`] says.
3760 asset_client: std::sync::OnceLock<Client>,
3761 /// Every item this source has created in this command, in the order it created them —
3762 /// dropped by [`TaskSource::end_command`].
3763 ///
3764 /// GitHub's `projectV2.items` is eventually consistent: an issue added to a board with
3765 /// `addProjectV2ItemById` is routinely absent from the very next read of that board, so
3766 /// a copy resolving a dependency on an item it had just created refused it as not
3767 /// found. A board read is completed from this — an item remembered here and absent from
3768 /// the read is added back, because the board really does hold it and only the read is
3769 /// behind.
3770 ///
3771 /// It is not a cache of a user's work: nothing is remembered that this process did not
3772 /// itself just write, it lives and dies with the process, and it is never consulted for
3773 /// an item this source did not create.
3774 created: Mutex<Vec<Resolved>>,
3775 /// Every item that already existed and that this source has written in this command, as
3776 /// it wrote it — dropped by [`TaskSource::end_command`].
3777 ///
3778 /// The other half of [`Self::created`], held on the same terms and for the reason a
3779 /// narrowed read needs it: an answer from GitHub's search or from the board's own field
3780 /// filter is an index behind a write this process made moments ago, so a query matching
3781 /// what this process just wrote onto an existing item would otherwise miss it. Nothing
3782 /// is remembered that this process did not itself just write.
3783 updated: Mutex<Vec<Resolved>>,
3784 /// Every issue this source has added a comment to or edited a comment of in this command
3785 /// — dropped by [`TaskSource::end_command`].
3786 ///
3787 /// A comment-activity read is narrowed by GitHub's issue search, whose `updated:` index
3788 /// lags the write that moved an issue's `updatedAt`, and neither [`Self::created`] nor
3789 /// [`Self::updated`] is moved by a comment, so an issue this process had just commented
3790 /// on was missing from such a read — or ruled out by the `updatedAt` its own record held
3791 /// from before — until the index caught up. Each id here is a candidate of every such
3792 /// search-narrowed read, and wherever it is a candidate its comments are read rather than
3793 /// it being ruled out by a stale `updatedAt`; that read is of the issue's own node, so it
3794 /// is current. It holds ids alone: nothing of a comment is remembered. A comment another
3795 /// process wrote is still found only once the index has it.
3796 commented: Mutex<Vec<NativeId>>,
3797 /// How fast this source writes, and how long it waits out a refusal.
3798 pacing: Pacing,
3799 /// When the last content-creating mutation finished, or the moment the furthest-out
3800 /// reserved slot releases the next one, whichever is later — so the one after it can be
3801 /// spaced from that, when `pacing.min_mutation_interval_ms` asks for a spacing. See
3802 /// [`GitHubProjectsSource::paced`] for why completion rather than release is what it is
3803 /// measured from.
3804 last_mutation: Mutex<Option<Duration>>,
3805 /// GitHub's secondary windows as this source has charged them; see the `pacing` module.
3806 windows: Mutex<pacing::Windows>,
3807 /// The tasks a project holds, as a copy's batched read of the project and every one of them
3808 /// found them — see [`TaskSource::read_ahead_for_copy`] — for the length of one command, so
3809 /// a listing of that project's tasks is answered without a read of its own.
3810 task_children: Mutex<BTreeMap<NativeId, Vec<Resolved>>>,
3811 /// The web address of each issue this source created in the current command, as its
3812 /// `createIssue` answered it — what [`TaskSource::written_location`] answers, without a
3813 /// request.
3814 written: Mutex<BTreeMap<NativeId, String>>,
3815 /// What a copy has held back of the items it created, between
3816 /// [`TaskSource::begin_deferred_writes`] and [`TaskSource::finish_writes`] — `None` while
3817 /// nothing is being held back, when every write lands whole as it is made.
3818 deferred: Mutex<Option<Deferred>>,
3819 clock: SharedClock,
3820 numeric_repositories: tokio::sync::Mutex<BTreeMap<RepositoryTarget, std::num::NonZeroU64>>,
3821 /// The board as this process last read it, for the length of one command — dropped by
3822 /// [`TaskSource::end_command`].
3823 ///
3824 /// A copy of a project used to re-read the whole board, paged, before writing each of
3825 /// its items, which is by far the largest part of a copy's request count and none of
3826 /// its work. Nothing else changes this board while a command runs — this source's own
3827 /// writes are the only writer — so one read answers them all.
3828 ///
3829 /// It is not a store of a user's work and it is not the cache the no-persistence
3830 /// invariant forbids: it lives and dies with the process exactly as `created` does,
3831 /// nothing is written down, and [`Self::board`] still completes it from `created`, so
3832 /// an item this command created and then depends on resolves whether or not GitHub's
3833 /// own eventually-consistent read has caught up. A write to an item already on the
3834 /// board updates the entry here too, so what this holds is the last read plus this
3835 /// process's own writes rather than a snapshot taken before them.
3836 board_cache: Mutex<Option<Board>>,
3837 /// Every issue this board's own search reported, for the length of one command — dropped
3838 /// by [`TaskSource::end_command`].
3839 ///
3840 /// The second half of a board read, and cached for the same reason and on the same
3841 /// terms as the first: it lives and dies with the process, nothing is written down, and
3842 /// a write this process makes updates the entry here exactly as it updates the one in
3843 /// [`Self::board_cache`]. One read answers every question a command asks, so a command
3844 /// that lists this board's projects and its tasks pays for one search rather than two.
3845 search_cache: Mutex<Option<Vec<Resolved>>>,
3846 /// What each narrowed question GitHub was asked answered, keyed by that question, for
3847 /// the length of one command — dropped by [`TaskSource::end_command`].
3848 ///
3849 /// The narrowed counterpart of [`Self::search_cache`], held on the same terms: it lives
3850 /// and dies with the process, nothing is written down, a write this process makes
3851 /// updates the entry here as it updates the other two, and every answer is completed
3852 /// with this process's own writes each time it is given. A command that asks the same
3853 /// narrowed question twice — a wait polling for its own items, a listing repeated after a
3854 /// write — pays for it once, which is what the whole-board read it replaced gave it.
3855 narrowed_cache: Mutex<BTreeMap<String, Vec<Resolved>>>,
3856 search_next: Mutex<BTreeMap<String, Option<String>>>,
3857 /// Records already resolved in this command, reused by writes and for comment identity.
3858 /// Explicit item reads still reach GitHub. Nothing is persisted, and
3859 /// [`TaskSource::end_command`] drops every record, so a write in the next command reads
3860 /// its item as a person has since left it.
3861 resolved_cache: Mutex<BTreeMap<NativeId, Resolved>>,
3862 /// What [`TaskSource::read_ahead_for_copy`] read, by id — `None` for an id that read
3863 /// answered as holding nothing — each taken by the next read of that item by id and by
3864 /// nothing else, dropped by any mutation naming it, and cleared by
3865 /// [`TaskSource::end_command`]. So a read answered from it is the copy's own
3866 /// read-before-write, made in the same request as the others' rather than on its own.
3867 read_ahead: Mutex<BTreeMap<NativeId, Option<Resolved>>>,
3868 /// The origins [`TaskSource::read_ahead_origins_for_copy`] was asked about and has not sent
3869 /// yet: held for the [`TaskSource::read_ahead_for_copy`] that follows it, which sends them —
3870 /// beside the members of the one project it names, when it names one — or for the next
3871 /// request this source makes, whichever comes first.
3872 pending_origins: Mutex<Vec<String>>,
3873 /// Each project's members as a lookup carrying them read them in this command, trimmed to
3874 /// their classification and repositories, taken by [`TaskSource::member_classifications`]
3875 /// of that project and by nothing else, and cleared by [`TaskSource::end_command`].
3876 member_classes: Mutex<BTreeMap<NativeId, Vec<MemberClass>>>,
3877 /// Every item a listing of one project's members carried whole in this command — the
3878 /// project issue and each of its sub-issues — kept for [`TaskSource::read_ahead_for_copy`]
3879 /// alone, which takes each one it is asked for into [`Self::read_ahead`] instead of reading
3880 /// it again. No other read is answered from it, so a read by id outside a copy's
3881 /// read-before-write reads GitHub as it always did. Dropped by any mutation naming an
3882 /// item, and cleared by [`TaskSource::end_command`].
3883 carried: Mutex<BTreeMap<NativeId, Resolved>>,
3884 /// Items an issue read by reference reached, held for the one status write each was read
3885 /// for and taken out by it alone — see [`Self::take_held`] — because that read selects only
3886 /// what such a write needs. Dropped by [`TaskSource::end_command`].
3887 held_cache: Mutex<BTreeMap<NativeId, Resolved>>,
3888 /// Each project's sub-issues as GitHub answered them, keyed by the selector they were
3889 /// asked for under, with the project that selector named — for the length of one command,
3890 /// dropped by [`TaskSource::end_command`].
3891 ///
3892 /// A caller pages through a project's tasks one engine page at a time, and every page
3893 /// is cut from the whole list of them, so without this each page walked every
3894 /// [`graphql::SUB_ISSUES`] page again and a project of `n` listing pages cost `n²`
3895 /// requests to read once. Held on the terms [`Self::narrowed_cache`] is: it lives and
3896 /// dies with the process, nothing is written down, a write this process makes updates or
3897 /// removes the entry here as it does there, and every answer is completed with this
3898 /// process's own writes each time it is given.
3899 children_cache: Mutex<ProjectChildren>,
3900 /// The board's own id and field definitions as this process last read them on their
3901 /// own, for the length of one command — dropped by [`TaskSource::end_command`].
3902 ///
3903 /// What a write needs of the board and its item does not say, read once per command
3904 /// rather than once per item written, on the terms [`Self::board_cache`] is held on: it
3905 /// lives and dies with the process and nothing is written down. It holds no item and so
3906 /// can answer no question about one — see [`Self::board_fields`].
3907 fields_cache: Mutex<Option<BoardFields>>,
3908 /// Each destination repository's node id, resolved once per repository
3909 /// rather than per issue created.
3910 ///
3911 /// A repository's node id does not change, and re-reading it for every issue of a copy
3912 /// spent one request per item on an answer this source already had. It is a map rather
3913 /// than one entry because a copy files each item in the repository its own
3914 /// `repositories` field names, so a plan across five repositories asks GitHub five
3915 /// times and not once per item.
3916 repository_cache: Mutex<BTreeMap<RepositoryTarget, String>>,
3917 /// What every request this source sends is recorded into.
3918 ///
3919 /// Ordinary code path, not a mode: [`Self::send_once`] records into it at the one place
3920 /// a request leaves this crate, so nothing has to be switched on for a session to be
3921 /// counted. It is shared rather than owned so a caller accounting for a whole session —
3922 /// its own schema verification, board lookups, residue sweep and cleanup beside this
3923 /// source's reads and writes — adds up one accounting instead of two. See
3924 /// [`accounting`] for what a record carries and what a session's spend is and is not.
3925 ledger: Arc<Accounting>,
3926 /// What [`expected`] holds for a write the store's cache expects: the journal while one is
3927 /// open, the issues primed from the cache, and the comments known on each.
3928 expected: expected::Held,
3929}
3930
3931/// GitHub's closed single-select color vocabulary.
3932#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3933#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
3934pub enum StatusOptionColor {
3935 /// Gray.
3936 Gray,
3937 /// Blue.
3938 Blue,
3939 /// Green.
3940 Green,
3941 /// Yellow.
3942 Yellow,
3943 /// Purple.
3944 Purple,
3945 /// Red.
3946 Red,
3947 /// Orange.
3948 Orange,
3949 /// Pink.
3950 Pink,
3951}
3952
3953/// Whether a guarded board setup — of the fields, or of the Status options alone — plans or
3954/// applies its additions.
3955#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3956pub enum SetupMode {
3957 /// Read without mutation.
3958 Plan,
3959 /// Apply and verify.
3960 Apply,
3961}
3962
3963/// The name [`SetupMode`] had when Status was the one field set up, kept so a caller written
3964/// against it goes on compiling.
3965pub type StatusOptionsMode = SetupMode;
3966
3967/// The explicit result of the requested operation.
3968#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3969#[serde(rename_all = "kebab-case")]
3970pub enum StatusOptionsOutcome {
3971 /// A read-only plan.
3972 Planned,
3973 /// Apply found nothing missing.
3974 Unchanged,
3975 /// Additions were applied and verified.
3976 Applied,
3977}
3978
3979/// A GitHub single-select option's opaque GraphQL node identifier.
3980#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3981#[serde(transparent)]
3982pub struct StatusOptionId(#[schemars(length(min = 1))] String);
3983
3984impl TryFrom<String> for StatusOptionId {
3985 type Error = String;
3986
3987 fn try_from(id: String) -> Result<Self, Self::Error> {
3988 if id.trim().is_empty() {
3989 return Err("a GitHub Status option id cannot be blank".to_owned());
3990 }
3991 Ok(Self(id))
3992 }
3993}
3994
3995/// One existing or proposed option in a guarded Status-field update.
3996#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3997pub struct StatusOption {
3998 /// GitHub's stable id.
3999 pub id: StatusOptionId,
4000 /// The visible option name.
4001 pub name: ColumnName,
4002 /// GitHub's single-select color token.
4003 pub color: StatusOptionColor,
4004 /// The option description, including an empty one.
4005 pub description: String,
4006}
4007
4008/// One board item's Status assignment, retained as recovery data.
4009#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4010pub struct StatusAssignment {
4011 /// The project item id whose assignment this is.
4012 // llmlint: ignore[invalid_states_unrepresentable] This opaque GraphQL node ID is
4013 // carried verbatim as operator recovery data; introducing a semantic type would claim
4014 // validation rules GitHub does not publish and no operation here interprets.
4015 pub item_id: String,
4016 /// The selected option, absent when the item has no status.
4017 #[serde(skip_serializing_if = "Option::is_none")]
4018 pub option: Option<AssignedStatusOption>,
4019}
4020
4021/// The inseparable id and name of an assigned option.
4022#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4023pub struct AssignedStatusOption {
4024 /// GitHub's stable id.
4025 pub id: StatusOptionId,
4026 /// The visible name.
4027 pub name: ColumnName,
4028}
4029
4030/// The plan and verified outcome of reconciling configured Status options.
4031#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4032pub struct StatusOptionsReport {
4033 /// The configured source name.
4034 pub source: SourceName,
4035 /// Configured option names absent before the operation.
4036 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a
4037 // `ColumnName` and has therefore already passed its nonblank validation; retaining the
4038 // serialized string here preserves the report's intentionally simple public contract.
4039 pub missing: Vec<String>,
4040 /// What the requested operation did.
4041 pub outcome: StatusOptionsOutcome,
4042 /// The complete option list observed before any mutation.
4043 pub existing: Vec<StatusOption>,
4044}
4045
4046#[derive(Debug, Clone, PartialEq, Eq)]
4047struct StatusSnapshot {
4048 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4049 // passed back as the mutation's project identity; a newtype could enforce no stronger
4050 // invariant because GitHub publishes no grammar for it.
4051 board_id: String,
4052 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4053 // passed back as the mutation's field identity; a newtype could enforce no stronger
4054 // invariant because GitHub publishes no grammar for it.
4055 field_id: String,
4056 options: Vec<StatusOption>,
4057 assignments: Vec<StatusAssignment>,
4058}
4059
4060/// The name of the board field a status is held in.
4061const STATUS_FIELD: &str = "Status";
4062
4063/// Every item's value of each field `report` names, as it stood before the setup wrote
4064/// anything — what a person puts back when the setup is refused part way.
4065fn recovery(report: &FieldsReport, before: &BoardSnapshot) -> Result<String, SourceError> {
4066 let assignments: BTreeMap<&str, Vec<StatusAssignment>> = report
4067 .fields
4068 .iter()
4069 .map(|field| (field.field.name(), before.assignments(field.field)))
4070 .collect();
4071 serde_json::to_string_pretty(&assignments).map_err(|error| SourceError::Malformed {
4072 message: format!("cannot render the pre-write field recovery snapshot: {error}"),
4073 })
4074}
4075
4076/// One board field the guarded setup reads and writes — every one it reads, and the only
4077/// ones it writes.
4078#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, schemars::JsonSchema)]
4079pub enum BoardField {
4080 /// The single-select `Status` field every instance's `status_mapping` resolves into.
4081 Status,
4082 /// The single-select `Priority` field an instance's `priority_mapping` resolves into.
4083 Priority,
4084}
4085
4086impl BoardField {
4087 /// The field's name on the board.
4088 #[must_use]
4089 pub const fn name(self) -> &'static str {
4090 match self {
4091 Self::Status => STATUS_FIELD,
4092 Self::Priority => PRIORITY_FIELD,
4093 }
4094 }
4095
4096 /// The field a board calls `name`, or `None` for one this setup does not own.
4097 fn named(name: &str) -> Option<Self> {
4098 [Self::Status, Self::Priority]
4099 .into_iter()
4100 .find(|field| field.name() == name)
4101 }
4102}
4103
4104/// What the guarded setup did to one field.
4105#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4106#[serde(rename_all = "kebab-case")]
4107pub enum FieldOutcome {
4108 /// A read-only plan.
4109 Planned,
4110 /// Apply found the field there with every configured option.
4111 Unchanged,
4112 /// Missing options were added to the field that was there, and verified.
4113 Applied,
4114 /// The field was not there; it was created holding the configured options, and verified.
4115 Created,
4116}
4117
4118/// One field's plan, or its verified outcome.
4119#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4120pub struct FieldReport {
4121 /// Which field.
4122 pub field: BoardField,
4123 /// Whether the board had the field before the operation.
4124 // llmlint: ignore[invalid_states_unrepresentable] `exists` beside `outcome` is the report's
4125 // wire shape as its consumer's contract fixes it — `{"field", "exists", "missing",
4126 // "outcome", "existing"}` — so folding one into the other would change a published JSON
4127 // shape. The contradictory pairings cannot be built: `GitHubProjectsSource::fields` is the
4128 // one constructor, and it derives `outcome` from `exists` in one match.
4129 pub exists: bool,
4130 /// Configured option names the field lacked before the operation — every one of them,
4131 /// in the order a new field lists them, when the field was not there at all.
4132 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
4133 // mapping name and has therefore already passed its nonblank validation; the serialized
4134 // string is the report's intentionally simple public contract, as `StatusOptionsReport`'s is.
4135 pub missing: Vec<String>,
4136 /// For the `Status` field, which item kind each missing name is configured for: one
4137 /// entry per kind `status_mapping` names a missing option for, task before project, each
4138 /// listing that kind's missing names in category order. A name both kinds use is in
4139 /// both. Empty — and left out of the JSON — when nothing is missing, and always for
4140 /// `Priority`, which only a task holds.
4141 #[serde(default, skip_serializing_if = "Vec::is_empty")]
4142 // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
4143 // both SDKs model an absent `kinds` as an empty list rather than as `null`.
4144 #[schemars(!skip_serializing_if)]
4145 pub kinds: Vec<KindMissing>,
4146 /// What the requested operation did.
4147 pub outcome: FieldOutcome,
4148 /// The field's complete option list observed before any mutation; empty when the field
4149 /// was not there.
4150 pub existing: Vec<StatusOption>,
4151}
4152
4153/// The `Status` option names one item kind's `status_mapping` names that the field lacked.
4154#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4155pub struct KindMissing {
4156 /// The kind these names are configured for.
4157 pub kind: ItemKind,
4158 /// The names that kind maps a category to and the field lacked, in category order.
4159 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
4160 // mapping name, as `FieldReport::missing`'s do, and the serialized string is the report's
4161 // intentionally simple public contract.
4162 pub missing: Vec<String>,
4163}
4164
4165/// The plan and verified outcome of setting up every field a source's configuration names.
4166#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4167pub struct FieldsReport {
4168 /// The configured source name.
4169 pub source: SourceName,
4170 /// `Status`, always, and `Priority` when the source sets `priority_mapping`.
4171 // llmlint: ignore[invalid_states_unrepresentable] A list is the report's wire shape as its
4172 // consumer's contract fixes it — `{"source", "fields": [...]}` — so a struct with one member
4173 // per field would change a published JSON shape. The states the list could hold and the
4174 // contract forbids cannot be built: `GitHubProjectsSource::fields` is the one constructor,
4175 // and it pushes `Status` first and exactly once, then `Priority` exactly when configured.
4176 pub fields: Vec<FieldReport>,
4177 /// Each board text field the source's `metadata_fields` projects a value onto, in
4178 /// configured order. Empty — and left out of the JSON — when it projects none.
4179 #[serde(default, skip_serializing_if = "Vec::is_empty")]
4180 // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
4181 // both SDKs model an absent list as an empty one rather than as `null`.
4182 #[schemars(!skip_serializing_if)]
4183 pub metadata_fields: Vec<MetadataFieldReport>,
4184}
4185
4186/// The type of a board field that is not a text field, as GitHub names it — its `dataType`, or
4187/// its GraphQL type when it has none — which is what a projected field's conflict reports.
4188///
4189/// Never empty, never beginning with whitespace, and never `TEXT`: a text field of that name is
4190/// no conflict.
4191// The schema states that rule as `NON_TEXT_FIELD_TYPE_PATTERN`, so a generated SDK model
4192// refuses exactly what `try_from` below does.
4193#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4194#[serde(transparent)]
4195pub struct NonTextFieldType(#[schemars(regex(pattern = NON_TEXT_FIELD_TYPE_PATTERN))] String);
4196
4197/// What a [`NonTextFieldType`] may be, as the emitted schema states it: a first character that
4198/// is not whitespace, and anything but exactly `TEXT` — spelled without a lookahead, which
4199/// neither SDK's validator can be relied on to support.
4200const NON_TEXT_FIELD_TYPE_PATTERN: &str =
4201 r"^(?:[^T\s]|T(?:[^E]|$)|TE(?:[^X]|$)|TEX(?:[^T]|$)|TEXT[\s\S])";
4202
4203impl TryFrom<String> for NonTextFieldType {
4204 type Error = String;
4205
4206 fn try_from(kind: String) -> Result<Self, Self::Error> {
4207 if kind.is_empty() || kind.starts_with(char::is_whitespace) {
4208 return Err("a board field's type cannot be blank or begin with whitespace".to_owned());
4209 }
4210 if kind == "TEXT" {
4211 return Err("a TEXT field is no conflict for a projected text field".to_owned());
4212 }
4213 Ok(Self(kind))
4214 }
4215}
4216
4217impl std::fmt::Display for NonTextFieldType {
4218 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4219 formatter.write_str(&self.0)
4220 }
4221}
4222
4223/// One projected metadata text field's plan, or its verified outcome.
4224///
4225/// The setup creates a missing one as a text field and never alters a field that is there:
4226/// a same-named field of another type is reported in `conflict` and left as it is.
4227#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4228pub struct MetadataFieldReport {
4229 /// The board field's name.
4230 pub field: String, // llmlint: ignore[invalid_states_unrepresentable] Copied from a configuration entry `MetadataField::resolve` already validated; the serialized string is the report's intentionally simple public contract, as `FieldReport::missing`'s names are.
4231 /// The metadata key the value is read from.
4232 pub key: String, // llmlint: ignore[invalid_states_unrepresentable] As `field`: a validated configuration value reported back as written.
4233 /// The object keys walked inside that key's value; empty for the key's own value.
4234 pub path: Vec<String>,
4235 /// Whether the board had a text field of that name before the operation.
4236 // llmlint: ignore[invalid_states_unrepresentable] `exists` beside `outcome` mirrors `FieldReport`'s published shape; `GitHubProjectsSource::fields` is the one constructor and derives `outcome` from `exists` and `conflict` in one match.
4237 pub exists: bool,
4238 /// The type of a field of that name that is not a text field, which the setup leaves as
4239 /// it is; absent — and left out of the JSON — when there is none.
4240 #[serde(default, skip_serializing_if = "Option::is_none")]
4241 pub conflict: Option<NonTextFieldType>,
4242 /// What the requested operation did: `planned`; `created` for a field that was not
4243 /// there; `unchanged` for one that was, and for a conflict, which is never changed.
4244 pub outcome: FieldOutcome,
4245}
4246
4247/// Which options one field is configured with, in the order a new field would list them.
4248struct FieldPlan {
4249 field: BoardField,
4250 wanted: Vec<String>,
4251}
4252
4253/// One single-select field as the guarded setup snapshots it.
4254#[derive(Debug, Clone, PartialEq, Eq)]
4255struct SnapshotField {
4256 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4257 // passed back as the mutation's field identity; a newtype could enforce no stronger
4258 // invariant because GitHub publishes no grammar for it.
4259 field_id: String,
4260 options: Vec<StatusOption>,
4261}
4262
4263/// Every single-select field of a board and every item's value of each.
4264#[derive(Debug, Clone, PartialEq, Eq)]
4265struct BoardSnapshot {
4266 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4267 // passed back as the mutation's project identity; a newtype could enforce no stronger
4268 // invariant because GitHub publishes no grammar for it.
4269 board_id: String,
4270 fields: BTreeMap<BoardField, SnapshotField>,
4271 /// Each board item's id, and its value of each field this setup owns that it holds one of.
4272 items: Vec<(String, BTreeMap<BoardField, AssignedStatusOption>)>,
4273}
4274
4275impl BoardSnapshot {
4276 /// Every item's value of `field`, in board order — the recovery data a drift refusal
4277 /// carries.
4278 fn assignments(&self, field: BoardField) -> Vec<StatusAssignment> {
4279 self.items
4280 .iter()
4281 .map(|(item_id, values)| StatusAssignment {
4282 item_id: item_id.clone(),
4283 option: values.get(&field).cloned(),
4284 })
4285 .collect()
4286 }
4287}
4288
4289impl GitHubProjectsSource {
4290 /// Report missing configured Status options and, when `apply` is true, add them with
4291 /// a whole-list mutation that preserves every existing id and verifies the result.
4292 ///
4293 /// # Errors
4294 ///
4295 /// Refuses a board without a single-select `Status` field. A post-write difference in
4296 /// any pre-existing option id or item assignment is refused with the complete pre-write
4297 /// assignment snapshot in the diagnostic for recovery.
4298 // llmlint: ignore[changed_behavior_has_e2e] The CLI journeys cover plan, no-op apply,
4299 // successful mutation, both drift refusals, source selection, missing Status, casing,
4300 // and paging. Transport errors remain the shared `graphql` boundary's behavior rather
4301 // than a new status-options behavior, and the pinned-schema test prevents valid GitHub
4302 // responses from entering the defensive malformed-response branches below.
4303 pub async fn status_options(
4304 &self,
4305 mode: StatusOptionsMode,
4306 ) -> Result<StatusOptionsReport, SourceError> {
4307 let before = self.status_snapshot().await?;
4308 // A terminal category's option is as configured as an open one's: a terminal
4309 // write validates it before closing and refuses when the board lacks it. Both
4310 // kinds' names are options of the one field, so both are asked for.
4311 let missing = self
4312 .statuses
4313 .wanted()
4314 .into_iter()
4315 .filter(|wanted| {
4316 !before
4317 .options
4318 .iter()
4319 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4320 })
4321 .collect::<Vec<_>>();
4322 let report = StatusOptionsReport {
4323 source: self.name.clone(),
4324 missing: missing.clone(),
4325 outcome: match (mode, missing.is_empty()) {
4326 (StatusOptionsMode::Plan, _) => StatusOptionsOutcome::Planned,
4327 (StatusOptionsMode::Apply, true) => StatusOptionsOutcome::Unchanged,
4328 (StatusOptionsMode::Apply, false) => StatusOptionsOutcome::Applied,
4329 },
4330 existing: before.options.clone(),
4331 };
4332 if mode == StatusOptionsMode::Plan || missing.is_empty() {
4333 return Ok(report);
4334 }
4335 let mut options = before
4336 .options
4337 .iter()
4338 .map(|option| {
4339 json!({
4340 "id": option.id, "name": option.name, "color": option.color,
4341 "description": option.description,
4342 })
4343 })
4344 .collect::<Vec<_>>();
4345 options.extend(missing.iter().map(|name| {
4346 json!({
4347 "name": name, "color": "GRAY", "description": ""
4348 })
4349 }));
4350 self.graphql(
4351 graphql::STATUS_OPTIONS_UPDATE,
4352 json!({"input": {
4353 "projectId": before.board_id, "fieldId": before.field_id,
4354 "singleSelectOptions": options,
4355 }}),
4356 )
4357 .await?;
4358 let after = self.status_snapshot().await?;
4359 let options_preserved = before
4360 .options
4361 .iter()
4362 .all(|old| after.options.iter().any(|new| new == old));
4363 let additions_present = missing.iter().all(|wanted| {
4364 after
4365 .options
4366 .iter()
4367 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4368 });
4369 if !options_preserved || !additions_present || after.assignments != before.assignments {
4370 let recovery = serde_json::to_string_pretty(&before.assignments).map_err(|error| {
4371 SourceError::Malformed {
4372 message: format!("cannot render pre-write Status recovery snapshot: {error}"),
4373 }
4374 })?;
4375 return Err(SourceError::Refused {
4376 message: format!(
4377 "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}"
4378 ),
4379 });
4380 }
4381 Ok(report)
4382 }
4383
4384 /// A fresh snapshot of the Status field and every board item's assignment of it.
4385 ///
4386 /// # Errors
4387 ///
4388 /// Refuses a board without a single-select `Status` field, and one the token cannot see.
4389 async fn status_snapshot(&self) -> Result<StatusSnapshot, SourceError> {
4390 // Status alone, as this operation has always read it: a `Priority` field is another
4391 // operation's, so nothing about it can refuse this one.
4392 let mut board = self.board_snapshot(&[BoardField::Status]).await?;
4393 let field = board
4394 .fields
4395 .remove(&BoardField::Status)
4396 .ok_or_else(|| self.no_status_field())?;
4397 Ok(StatusSnapshot {
4398 assignments: board.assignments(BoardField::Status),
4399 board_id: board.board_id,
4400 field_id: field.field_id,
4401 options: field.options,
4402 })
4403 }
4404
4405 /// The refusal a board with no `Status` field is answered with by the guarded setup.
4406 fn no_status_field(&self) -> SourceError {
4407 SourceError::Refused {
4408 message: format!("source {} board has no Status field", self.name),
4409 }
4410 }
4411
4412 // llmlint: ignore-block[changed_behavior_has_e2e] Valid snapshot shapes are exercised through
4413 // the real CLI loopback journey, including pagination. The individual malformed guards
4414 // are defensive validation of a schema-pinned third-party response, not separate user
4415 // journeys; drift and missing-field failures cover the operation's recovery behavior.
4416 /// A fresh snapshot of each of the `owned` fields on the board, with its options, and of
4417 /// every board item's value of each, walked to the end of the board's items. A field not
4418 /// in `owned` is read past whatever it holds.
4419 async fn board_snapshot(&self, owned: &[BoardField]) -> Result<BoardSnapshot, SourceError> {
4420 let mut after: Option<String> = None;
4421 let mut snapshot: Option<BoardSnapshot> = None;
4422 loop {
4423 let data = self
4424 .graphql(
4425 graphql::STATUS_OPTIONS_SNAPSHOT,
4426 json!({
4427 "owner": self.owner, "number": self.project_number,
4428 "first": MAX_PAGE_SIZE, "after": after, "nestedFirst": MAX_PAGE_SIZE,
4429 }),
4430 )
4431 .await?;
4432 let board = data
4433 .pointer("/owner/projectV2")
4434 .filter(|board| board.is_object())
4435 .ok_or_else(|| SourceError::Refused {
4436 message: format!(
4437 "source {} has no accessible GitHub Projects board",
4438 self.name
4439 ),
4440 })?;
4441 if board
4442 .pointer("/fields/pageInfo/hasNextPage")
4443 .and_then(Value::as_bool)
4444 != Some(false)
4445 {
4446 return Err(SourceError::Malformed {
4447 message:
4448 "GitHub project fields is incomplete or has malformed pageInfo.hasNextPage"
4449 .into(),
4450 });
4451 }
4452 let mut fields = BTreeMap::new();
4453 // Only the fields this setup owns, by name: a node the single-select fragment did not
4454 // match carries no name, and a person's own single-select field — a `Size`, a
4455 // `Team` — is none of this setup's business, so nothing about it can refuse one. A
4456 // `Status` or `Priority` field without its options is malformed, not absent.
4457 // llmlint: ignore[boundary_inputs_validated] The field page this loop reads is validated as complete immediately above: any `fields.pageInfo.hasNextPage` other than `false` is refused as malformed before a node is read, so an incomplete page is never taken for the board's whole field set.
4458 for (owned, field) in board
4459 .pointer("/fields/nodes")
4460 .and_then(Value::as_array)
4461 .ok_or_else(|| SourceError::Malformed {
4462 message: "GitHub project fields.nodes is not an array".into(),
4463 })?
4464 .iter()
4465 .filter_map(|field| {
4466 let named = BoardField::named(field.get("name")?.as_str()?)?;
4467 owned.contains(&named).then_some((named, field))
4468 })
4469 {
4470 let options = field
4471 .get("options")
4472 .and_then(Value::as_array)
4473 .ok_or_else(|| SourceError::Malformed {
4474 message: "GitHub single-select field options is not an array".into(),
4475 })?
4476 .iter()
4477 .map(|option| {
4478 Ok(StatusOption {
4479 id: StatusOptionId::try_from(required_str(option, "id")?.to_owned())
4480 .map_err(|message| SourceError::Malformed { message })?,
4481 name: ColumnName::try_from(required_str(option, "name")?.to_owned())
4482 .map_err(|message| SourceError::Malformed {
4483 message: format!(
4484 "GitHub single-select option name is invalid: {message}"
4485 ),
4486 })?,
4487 color: serde_json::from_value(
4488 option.get("color").cloned().unwrap_or(Value::Null),
4489 )
4490 .map_err(|error| {
4491 SourceError::Malformed {
4492 message: format!(
4493 "GitHub single-select option color is invalid: {error}"
4494 ),
4495 }
4496 })?,
4497 description: optional_str(option, "description")?
4498 .unwrap_or_default()
4499 .to_owned(),
4500 })
4501 })
4502 .collect::<Result<Vec<_>, SourceError>>()?;
4503 let snapshot = SnapshotField {
4504 field_id: required_nonblank_str(field, "id")?.to_owned(),
4505 options,
4506 };
4507 // A board's field names are unique, so a second one is an answer that cannot
4508 // say which field the setup would act on — refused rather than one chosen.
4509 if fields.insert(owned, snapshot).is_some() {
4510 return Err(SourceError::Malformed {
4511 message: format!(
4512 "GitHub answered two {} fields for this board",
4513 owned.name()
4514 ),
4515 });
4516 }
4517 }
4518 let board_id = required_nonblank_str(board, "id")?.to_owned();
4519 let current = snapshot.get_or_insert_with(|| BoardSnapshot {
4520 board_id,
4521 fields,
4522 items: Vec::new(),
4523 });
4524 let items = board
4525 .pointer("/items/nodes")
4526 .and_then(Value::as_array)
4527 .ok_or_else(|| SourceError::Malformed {
4528 message: "GitHub project items.nodes is not an array".into(),
4529 })?;
4530 for item in items {
4531 let field_values =
4532 item.get("fieldValues")
4533 .ok_or_else(|| SourceError::Malformed {
4534 message: "GitHub project item is missing fieldValues".into(),
4535 })?;
4536 if field_values
4537 .pointer("/pageInfo/hasNextPage")
4538 .and_then(Value::as_bool)
4539 != Some(false)
4540 {
4541 return Err(SourceError::Malformed {
4542 message: "GitHub project item fieldValues is incomplete or has malformed pageInfo.hasNextPage".into(),
4543 });
4544 }
4545 let values = item
4546 .pointer("/fieldValues/nodes")
4547 .and_then(Value::as_array)
4548 .ok_or_else(|| SourceError::Malformed {
4549 message: "GitHub project item fieldValues.nodes is not an array".into(),
4550 })?;
4551 let item_id = required_nonblank_str(item, "id")?;
4552 let mut assigned = BTreeMap::new();
4553 for value in values {
4554 let Some(field) = value
4555 .pointer("/field/name")
4556 .and_then(Value::as_str)
4557 .and_then(BoardField::named)
4558 .filter(|field| owned.contains(field))
4559 else {
4560 continue;
4561 };
4562 let held = assigned.insert(
4563 field,
4564 AssignedStatusOption {
4565 id: StatusOptionId::try_from(
4566 required_str(value, "optionId")?.to_owned(),
4567 )
4568 .map_err(|message| SourceError::Malformed { message })?,
4569 name: ColumnName::try_from(required_str(value, "name")?.to_owned())
4570 .map_err(|message| SourceError::Malformed {
4571 message: format!(
4572 "GitHub assigned {} name is invalid: {message}",
4573 field.name()
4574 ),
4575 })?,
4576 },
4577 );
4578 // An item holds one value of a field, so a second one leaves no way to
4579 // tell which it holds — and a verification or recovery built on either
4580 // could restore the wrong one.
4581 if held.is_some() {
4582 return Err(SourceError::Malformed {
4583 message: format!(
4584 "GitHub answered two {} values for board item {item_id}",
4585 field.name()
4586 ),
4587 });
4588 }
4589 }
4590 current.items.push((item_id.to_owned(), assigned));
4591 }
4592 let page = board.get("items").ok_or_else(|| SourceError::Malformed {
4593 message: "GitHub project is missing items".into(),
4594 })?;
4595 let has_next = page
4596 .pointer("/pageInfo/hasNextPage")
4597 .and_then(Value::as_bool)
4598 .ok_or_else(|| SourceError::Malformed {
4599 message: "GitHub project items.pageInfo.hasNextPage is not a boolean".into(),
4600 })?;
4601 if !has_next {
4602 break;
4603 }
4604 let next =
4605 required_nonblank_str(page.get("pageInfo").unwrap_or(&Value::Null), "endCursor")?;
4606 validate_cursor_progress(after.as_deref(), next)?;
4607 after = Some(next.to_owned());
4608 }
4609 snapshot.ok_or_else(|| SourceError::Malformed {
4610 message: "GitHub returned no board field snapshot".into(),
4611 })
4612 }
4613 // llmlint: ignore-end[changed_behavior_has_e2e]
4614
4615 /// Report every board field this source's configuration names and, with
4616 /// [`SetupMode::Apply`], set each up: add the options a field lacks, and create
4617 /// the `Priority` field when the board has none.
4618 ///
4619 /// The fields are `Status`, always, with the options `status_mapping` resolves to; and
4620 /// `Priority`, when `priority_mapping` is set, with its four mapped options — created in
4621 /// the order urgent, high, medium, low. An option a field already has keeps its id, name,
4622 /// color and description: the whole option list goes back with every existing id, because
4623 /// a re-minted id clears every item's value.
4624 ///
4625 /// # Errors
4626 ///
4627 /// Refuses a board without a single-select `Status` field. After an apply the board is
4628 /// read again, and a pre-existing option or any item's value of either field that moved is
4629 /// refused with the complete pre-write assignments in the diagnostic, for recovery.
4630 // llmlint: ignore[changed_behavior_has_e2e] The `sources fields` journeys drive plan,
4631 // unchanged apply, a created field, an added option to each field, drift refusal, a board
4632 // with no Status field and a non-github-projects source through the compiled CLI against
4633 // the loopback board. Transport errors are the shared `graphql` boundary's behavior.
4634 pub async fn fields(&self, mode: SetupMode) -> Result<FieldsReport, SourceError> {
4635 let owned: Vec<BoardField> = if self.priorities.is_some() {
4636 vec![BoardField::Status, BoardField::Priority]
4637 } else {
4638 vec![BoardField::Status]
4639 };
4640 let before = self.board_snapshot(&owned).await?;
4641 let mut plans = vec![FieldPlan {
4642 field: BoardField::Status,
4643 wanted: self.statuses.wanted(),
4644 }];
4645 if !before.fields.contains_key(&BoardField::Status) {
4646 return Err(self.no_status_field());
4647 }
4648 if let Some(mapping) = &self.priorities {
4649 plans.push(FieldPlan {
4650 field: BoardField::Priority,
4651 wanted: mapping.names().map(str::to_owned).collect(),
4652 });
4653 }
4654 // The snapshot reads single-select fields alone, so a field it did not find may still
4655 // be on the board under the name, of another type: creating one beside it would fail
4656 // part way, or leave two fields of one name. Asked of the board's own field list, and
4657 // only when a field is missing.
4658 if plans
4659 .iter()
4660 .any(|plan| !before.fields.contains_key(&plan.field))
4661 {
4662 let board = self.board_fields().await?;
4663 for plan in plans
4664 .iter()
4665 .filter(|plan| !before.fields.contains_key(&plan.field))
4666 {
4667 if let Some(field) = Board::field(&board.fields, plan.field.name())? {
4668 return Err(SourceError::Refused {
4669 message: format!(
4670 "source {}'s board has a {} field that is not a single-select field \
4671 (it is a {}), so it cannot hold this source's options; next: rename \
4672 or remove that field, then run this again",
4673 self.name,
4674 plan.field.name(),
4675 optional_str(field, "__typename")?.unwrap_or("field of another type")
4676 ),
4677 });
4678 }
4679 }
4680 }
4681 let mut reports = Vec::new();
4682 for plan in &plans {
4683 let held = before.fields.get(&plan.field);
4684 let existing = held.map(|field| field.options.clone()).unwrap_or_default();
4685 let mut missing: Vec<String> = Vec::new();
4686 for wanted in &plan.wanted {
4687 let present = existing
4688 .iter()
4689 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4690 || missing
4691 .iter()
4692 .any(|named| named.eq_ignore_ascii_case(wanted));
4693 if !present {
4694 missing.push(wanted.clone());
4695 }
4696 }
4697 let kinds = match plan.field {
4698 BoardField::Status => self.statuses.missing_by_kind(&existing),
4699 BoardField::Priority => Vec::new(),
4700 };
4701 reports.push(FieldReport {
4702 field: plan.field,
4703 exists: held.is_some(),
4704 kinds,
4705 outcome: match (mode, held.is_some(), missing.is_empty()) {
4706 (SetupMode::Plan, _, _) => FieldOutcome::Planned,
4707 (SetupMode::Apply, true, true) => FieldOutcome::Unchanged,
4708 (SetupMode::Apply, true, false) => FieldOutcome::Applied,
4709 (SetupMode::Apply, false, _) => FieldOutcome::Created,
4710 },
4711 missing,
4712 existing,
4713 });
4714 }
4715 // A projected field is a text field, which the single-select snapshot cannot see, so
4716 // it is asked of the board's own field list — and only when one is configured.
4717 let mut metadata_fields = Vec::new();
4718 if !self.metadata_fields.is_empty() {
4719 let board = self.board_fields().await?;
4720 for projection in &self.metadata_fields {
4721 let conflict = match Board::field(&board.fields, &projection.field)? {
4722 None => None,
4723 Some(field) => {
4724 let typename = optional_str(field, "__typename")?;
4725 let data_type = optional_str(field, "dataType")?;
4726 if typename == Some("ProjectV2Field") && data_type == Some("TEXT") {
4727 None
4728 } else {
4729 let named = data_type
4730 .filter(|kind| *kind != "TEXT")
4731 .or(typename)
4732 .unwrap_or("field of another type");
4733 Some(NonTextFieldType::try_from(named.to_owned()).map_err(
4734 |message| SourceError::Malformed {
4735 message: format!(
4736 "GitHub answered the board's {} field with a type this \
4737 setup cannot read: {message}",
4738 &*projection.field
4739 ),
4740 },
4741 )?)
4742 }
4743 }
4744 };
4745 let exists =
4746 conflict.is_none() && Board::field(&board.fields, &projection.field)?.is_some();
4747 metadata_fields.push(MetadataFieldReport {
4748 field: projection.field.to_string(),
4749 key: projection.key.to_string(),
4750 path: projection.path.clone(),
4751 exists,
4752 outcome: match (mode, exists || conflict.is_some()) {
4753 (SetupMode::Plan, _) => FieldOutcome::Planned,
4754 (SetupMode::Apply, true) => FieldOutcome::Unchanged,
4755 (SetupMode::Apply, false) => FieldOutcome::Created,
4756 },
4757 conflict,
4758 });
4759 }
4760 }
4761 let report = FieldsReport {
4762 source: self.name.clone(),
4763 fields: reports,
4764 metadata_fields,
4765 };
4766 let writes: Vec<&FieldReport> = report
4767 .fields
4768 .iter()
4769 .filter(|field| !field.missing.is_empty() || !field.exists)
4770 .collect();
4771 let creates: Vec<&MetadataFieldReport> = report
4772 .metadata_fields
4773 .iter()
4774 .filter(|field| field.outcome == FieldOutcome::Created)
4775 .collect();
4776 if mode == SetupMode::Plan || (writes.is_empty() && creates.is_empty()) {
4777 return Ok(report);
4778 }
4779 let mut landed: Vec<&str> = Vec::new();
4780 for field in &writes {
4781 let added = field
4782 .missing
4783 .iter()
4784 .map(|name| json!({"name": name, "color": "GRAY", "description": ""}));
4785 let sent = match before.fields.get(&field.field) {
4786 Some(held) => {
4787 let mut options = held
4788 .options
4789 .iter()
4790 .map(|option| {
4791 json!({
4792 "id": option.id, "name": option.name, "color": option.color,
4793 "description": option.description,
4794 })
4795 })
4796 .collect::<Vec<_>>();
4797 options.extend(added);
4798 self.graphql(
4799 graphql::STATUS_OPTIONS_UPDATE,
4800 json!({"input": {
4801 "projectId": before.board_id, "fieldId": held.field_id,
4802 "singleSelectOptions": options,
4803 }}),
4804 )
4805 .await
4806 }
4807 None => {
4808 self.graphql(
4809 graphql::CREATE_FIELD,
4810 json!({"input": {
4811 "projectId": before.board_id, "dataType": "SINGLE_SELECT",
4812 "name": field.field.name(),
4813 "singleSelectOptions": added.collect::<Vec<_>>(),
4814 }}),
4815 )
4816 .await
4817 }
4818 };
4819 // A mutation that failed does not establish that GitHub left its field as it was,
4820 // so every failure from here on carries the recovery data a drift refusal does.
4821 match sent {
4822 Ok(_) => landed.push(field.field.name()),
4823 Err(error) => {
4824 let changed = if landed.is_empty() {
4825 String::new()
4826 } else {
4827 format!("changed the {} field and then ", landed.join(" and "))
4828 };
4829 return Err(SourceError::Refused {
4830 message: format!(
4831 "the guarded field setup {changed}failed on the {} field, which it may \
4832 have changed part way: {error}; the pre-write item assignments \
4833 are:\n{}",
4834 field.field.name(),
4835 recovery(&report, &before)?
4836 ),
4837 });
4838 }
4839 }
4840 }
4841 for field in &creates {
4842 let sent = self
4843 .graphql(
4844 graphql::CREATE_FIELD,
4845 json!({"input": {
4846 "projectId": before.board_id, "dataType": "TEXT", "name": field.field,
4847 }}),
4848 )
4849 .await;
4850 if let Err(error) = sent {
4851 let changed = if landed.is_empty() {
4852 String::new()
4853 } else {
4854 format!("changed the {} field and then ", landed.join(" and "))
4855 };
4856 return Err(SourceError::Refused {
4857 message: format!(
4858 "the guarded field setup {changed}failed creating the {} text field: \
4859 {error}; run it again to create what is still missing",
4860 field.field
4861 ),
4862 });
4863 }
4864 landed.push(&field.field);
4865 }
4866 // The board has been written, so a verification read that fails leaves it unverified
4867 // rather than unchanged, and says what to put back.
4868 let after = match self.board_snapshot(&owned).await {
4869 Ok(after) => after,
4870 Err(error) => {
4871 return Err(SourceError::Refused {
4872 message: format!(
4873 "the guarded field setup changed the {} field and then could not read the \
4874 board back to verify it: {error}; the pre-write item assignments are:\n{}",
4875 landed.join(" and "),
4876 recovery(&report, &before)?
4877 ),
4878 });
4879 }
4880 };
4881 let mut moved = Vec::new();
4882 for field in &report.fields {
4883 let name = field.field.name();
4884 let now = after
4885 .fields
4886 .get(&field.field)
4887 .map(|held| held.options.as_slice())
4888 .unwrap_or_default();
4889 if !field.existing.iter().all(|old| now.contains(old)) {
4890 moved.push(format!(
4891 "a pre-existing {name} option id, name, color or description"
4892 ));
4893 }
4894 if !field.missing.iter().all(|wanted| {
4895 now.iter()
4896 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4897 }) {
4898 moved.push(format!("an added {name} option"));
4899 }
4900 if after.assignments(field.field) != before.assignments(field.field) {
4901 moved.push(format!("an item's {name} value"));
4902 }
4903 }
4904 if !creates.is_empty() {
4905 // Read afresh: the view held for this command is the one from before the creates.
4906 *self.fields_cache()? = None;
4907 let board = match self.board_fields().await {
4908 Ok(board) => board,
4909 Err(error) => {
4910 return Err(SourceError::Refused {
4911 message: format!(
4912 "the guarded field setup changed the {} field and then could not \
4913 read the board back to verify it: {error}; run it again to verify \
4914 it, which creates nothing a second time",
4915 landed.join(" and ")
4916 ),
4917 });
4918 }
4919 };
4920 for field in &creates {
4921 let text = Board::field(&board.fields, &field.field)?.is_some_and(|held| {
4922 held.get("__typename").and_then(Value::as_str) == Some("ProjectV2Field")
4923 && held.get("dataType").and_then(Value::as_str) == Some("TEXT")
4924 });
4925 if !text {
4926 moved.push(format!("the created {} text field", field.field));
4927 }
4928 }
4929 }
4930 if !moved.is_empty() {
4931 return Err(SourceError::Refused {
4932 message: format!(
4933 "GitHub changed {} after the guarded field setup; the pre-write item \
4934 assignments are:\n{}",
4935 moved.join(", "),
4936 recovery(&report, &before)?
4937 ),
4938 });
4939 }
4940 Ok(report)
4941 }
4942
4943 /// Validate configuration and capture the named credential without exposing it.
4944 ///
4945 /// # Errors
4946 ///
4947 /// Returns [`SourceError::Config`] for a configuration this instance cannot use and
4948 /// [`SourceError::Auth`] when the named credential is missing or empty.
4949 pub fn new(
4950 name: &SourceName,
4951 config: GitHubProjectsConfig,
4952 secrets: &dyn SecretResolver,
4953 ) -> Result<Self, SourceError> {
4954 Self::recording_into(name, config, secrets, Arc::new(Accounting::new()))
4955 }
4956
4957 /// The same, recording every request it sends into an accounting the caller holds too.
4958 ///
4959 /// [`Self::new`] is this with an accounting of its own. A caller that is also making
4960 /// its own calls to GitHub — a lane verifying a schema, sweeping residue or cleaning
4961 /// up — passes the one it records those into, so the session total accounts for the
4962 /// whole session rather than for this source's share of it.
4963 ///
4964 /// # Errors
4965 ///
4966 /// Exactly [`Self::new`]'s: [`SourceError::Config`] for a configuration this instance
4967 /// cannot use and [`SourceError::Auth`] when the named credential is missing or empty.
4968 pub fn recording_into(
4969 name: &SourceName,
4970 config: GitHubProjectsConfig,
4971 secrets: &dyn SecretResolver,
4972 ledger: Arc<Accounting>,
4973 ) -> Result<Self, SourceError> {
4974 if !valid_github_owner(&config.owner) {
4975 return Err(SourceError::Config {
4976 message: "owner must be 1-39 ASCII letters, digits, or single hyphens, and cannot start or end with a hyphen".into(),
4977 });
4978 }
4979 if config.project_number == 0 || config.project_number > i32::MAX as u32 {
4980 return Err(SourceError::Config {
4981 message: format!("project_number must be between 1 and {}", i32::MAX),
4982 });
4983 }
4984 if !valid_environment_name(&config.token_env) {
4985 return Err(SourceError::Config {
4986 message: "token_env must be a valid environment-variable name".into(),
4987 });
4988 }
4989 let repository = config
4990 .repository
4991 .as_deref()
4992 .map(RepositoryTarget::parse)
4993 .transpose()?;
4994 let endpoint = Url::parse(&config.endpoint).map_err(|e| SourceError::Config {
4995 message: format!("endpoint is not a valid URL: {e}"),
4996 })?;
4997 if endpoint.scheme() != "https"
4998 && !(endpoint.scheme() == "http"
4999 && endpoint
5000 .host_str()
5001 .is_some_and(|h| h == "127.0.0.1" || h == "localhost" || h == "::1"))
5002 {
5003 return Err(SourceError::Config {
5004 message:
5005 "endpoint must use HTTPS (HTTP is accepted only for a loopback test server)"
5006 .into(),
5007 });
5008 }
5009 let token = secrets.get(&config.token_env).filter(|token| !token.expose_secret().trim().is_empty()).ok_or_else(|| SourceError::Auth {
5010 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),
5011 })?;
5012 Ok(Self {
5013 name: name.clone(),
5014 owner: config.owner,
5015 project_number: config.project_number,
5016 repository,
5017 asset_client: std::sync::OnceLock::new(),
5018 endpoint,
5019 token,
5020 credential_name: config.token_env,
5021 statuses: BoardStatuses::resolve(&config.status_mapping, name)?,
5022 priorities: config
5023 .priority_mapping
5024 .map(|mapping| PriorityMapping::resolve(mapping, name))
5025 .transpose()?,
5026 metadata_fields: MetadataField::resolve(config.metadata_fields, name)?,
5027 client: std::sync::OnceLock::new(),
5028 created: Mutex::new(Vec::new()),
5029 updated: Mutex::new(Vec::new()),
5030 commented: Mutex::new(Vec::new()),
5031 pacing: Pacing::resolve(config.pacing, name)?,
5032 last_mutation: Mutex::new(None),
5033 windows: Mutex::new(pacing::Windows::default()),
5034 task_children: Mutex::new(BTreeMap::new()),
5035 written: Mutex::new(BTreeMap::new()),
5036 deferred: Mutex::new(None),
5037 clock: system_clock(),
5038 numeric_repositories: tokio::sync::Mutex::new(BTreeMap::new()),
5039 board_cache: Mutex::new(None),
5040 search_cache: Mutex::new(None),
5041 narrowed_cache: Mutex::new(BTreeMap::new()),
5042 resolved_cache: Mutex::new(BTreeMap::new()),
5043 read_ahead: Mutex::new(BTreeMap::new()),
5044 pending_origins: Mutex::new(Vec::new()),
5045 member_classes: Mutex::new(BTreeMap::new()),
5046 carried: Mutex::new(BTreeMap::new()),
5047 held_cache: Mutex::new(BTreeMap::new()),
5048 children_cache: Mutex::new(BTreeMap::new()),
5049 search_next: Mutex::new(BTreeMap::new()),
5050 fields_cache: Mutex::new(None),
5051 repository_cache: Mutex::new(BTreeMap::new()),
5052 ledger,
5053 expected: expected::Held::default(),
5054 })
5055 }
5056
5057 /// The client every API request goes through, built on the first one.
5058 ///
5059 /// Not with the source: building a client loads the system's certificate store, which
5060 /// costs tens of milliseconds, and a command that configures this board but never sends to
5061 /// it — a read of another source — should not pay that in its process.
5062 fn client(&self) -> Result<&Client, SourceError> {
5063 built(&self.client, || {
5064 Client::builder()
5065 .user_agent("onetaskgraph")
5066 .build()
5067 .map_err(|e| SourceError::Config {
5068 message: format!("cannot build HTTP client: {e}"),
5069 })
5070 })
5071 }
5072
5073 /// The client attachment requests go through, with its own redirect policy, built on the
5074 /// first one for the reason [`Self::client`] gives.
5075 fn asset_client(&self) -> Result<&Client, SourceError> {
5076 built(&self.asset_client, || assets::client(&self.endpoint))
5077 }
5078
5079 /// A snapshot of every request this source has sent, and what each cost.
5080 ///
5081 /// A value to hold and compare rather than a borrow of the accounting itself, so two
5082 /// of them can sit side by side. When this source was built with
5083 /// [`Self::recording_into`] the snapshot is the whole shared session, which is the
5084 /// point of building it that way.
5085 #[must_use]
5086 pub fn accounting(&self) -> accounting::Session {
5087 self.ledger.snapshot()
5088 }
5089
5090 /// Send one GraphQL document, pacing this source's own mutations and waiting out a
5091 /// rate limit rather than handing it straight back as an error.
5092 ///
5093 /// Retrying is safe for every document here, including the mutations, and the reason
5094 /// is that only a *refusal* is retried: [`Limiter::classify`] rules on a response
5095 /// GitHub sent, and a request GitHub refused for a rate limit did not run, so nothing
5096 /// this replays has already taken effect. An outcome this source cannot know — the
5097 /// send failed, or the body could not be read, so the mutation may well have landed —
5098 /// is [`Attempt::Failed`] in [`send_once`] and leaves this loop without a second
5099 /// attempt. A duplicate write would come from replaying one of those, and none is
5100 /// replayed.
5101 async fn graphql(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
5102 // Origins held back for a read ahead that never came are sent before anything else.
5103 if query != graphql::ORIGIN_LOOKUPS && query != graphql::ORIGIN_LOOKUPS_WITH_MEMBERS {
5104 Box::pin(self.send_pending_origins(None)).await?;
5105 }
5106 if !self.expected.journaling() {
5107 return self.graphql_sent(query, variables).await;
5108 }
5109 // While a write the store's cache expects runs, its mutations select the whole issue
5110 // they leave behind, so the response vouches for what the cache keeps — and every
5111 // request is recorded, so the write's parts are reported from their own responses.
5112 let (query, variables) = match graphql::whole_twin(query) {
5113 Some(twin) => (twin, expected::whole_variables(variables)),
5114 None => (query, variables),
5115 };
5116 let sent = self.graphql_sent(query, variables.clone()).await;
5117 self.journal(query, &variables, &sent).await;
5118 sent
5119 }
5120
5121 /// One document sent, retried while GitHub asks for a wait, exactly as asked.
5122 async fn graphql_sent(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
5123 if is_mutation(query)
5124 && ![
5125 graphql::ADD_COMMENT,
5126 graphql::UPDATE_COMMENT,
5127 graphql::DELETE_COMMENT,
5128 graphql::ADD_COMMENT_WHOLE,
5129 graphql::UPDATE_COMMENT_WHOLE,
5130 ]
5131 .contains(&query)
5132 {
5133 let mut cache = self.resolved_cache()?;
5134 let mut ahead = self.read_ahead()?;
5135 let mut carried = self.carried()?;
5136 for argument in ["input", "second", "third", "clear"] {
5137 if let Some(input) = variables.get(argument) {
5138 let names = |id: &NativeId, item_id: Option<&str>| {
5139 ["id", "issueId", "subjectId", "itemId"].iter().any(|key| {
5140 input
5141 .get(key)
5142 .and_then(Value::as_str)
5143 .is_some_and(|value| value == id.0 || Some(value) == item_id)
5144 })
5145 };
5146 cache.retain(|id, item| !names(id, Some(&item.item_id)));
5147 ahead.retain(|id, item| {
5148 !names(id, item.as_ref().map(|item| item.item_id.as_str()))
5149 });
5150 carried.retain(|id, item| !names(id, Some(&item.item_id)));
5151 }
5152 }
5153 }
5154 let doing = operation_description(query);
5155 let mutation = is_mutation(query);
5156 let charge = pacing::Charge::graphql(query, &variables);
5157 let mut waited = Duration::ZERO;
5158 let mut waits = 0_u32;
5159 let mut backoff = self.pacing.retry_backoff;
5160 loop {
5161 let ticket = self.pace(charge, mutation).await;
5162 let attempt = self.send_once(query, &variables, waits + 1).await;
5163 self.paced(ticket, mutation);
5164 let limited = match attempt {
5165 Ok(data) => return Ok(data),
5166 Err(Attempt::Failed(error)) => return Err(error),
5167 Err(Attempt::Limited(limited)) => limited,
5168 };
5169 let (wait, cause) = limited.wait(backoff);
5170 let remaining = self.pacing.retry_budget.saturating_sub(waited);
5171 // A wait of nothing spends none of the budget, so it is exhaustion rather
5172 // than a retry. `Pacing::resolve` rules out every way of configuring one
5173 // except a budget of zero, where reporting the first refusal is the ask.
5174 if wait.is_zero() || wait > remaining {
5175 return Err(limited.exhausted(
5176 doing,
5177 waits,
5178 waited,
5179 wait,
5180 self.pacing.retry_budget,
5181 ));
5182 }
5183 self.clock.sleep_because(wait, cause).await;
5184 waited += wait;
5185 waits += 1;
5186 backoff = backoff.saturating_mul(2);
5187 }
5188 }
5189
5190 /// Hold one request charging `charge` until every secondary window it charges has room for
5191 /// the whole of it — and, for a mutation of an instance configured with
5192 /// `pacing.min_mutation_interval_ms`, until that interval has passed since the last one
5193 /// finished — then count it, answering the ticket [`Self::paced`] finishes it with.
5194 ///
5195 /// The moment is decided and counted under the lock and waited out outside it, so two
5196 /// callers take two places rather than the same one, and no lock is held across an await.
5197 /// A request with room goes out at once: nothing here waits that a window does not ask for.
5198 async fn pace(&self, charge: pacing::Charge, mutation: bool) -> pacing::Ticket {
5199 let (wait, cause, ticket) = {
5200 // A poisoned lock here costs pacing, not correctness, and refusing the write over
5201 // it would turn an earlier failure into a second one for no gain.
5202 let mut windows = self
5203 .windows
5204 .lock()
5205 .unwrap_or_else(std::sync::PoisonError::into_inner);
5206 let now = self.clock.now();
5207 let (mut at, mut cause) = if self.pacing.windows {
5208 windows.room(now, charge)
5209 } else {
5210 (now, None)
5211 };
5212 if mutation && !self.pacing.min_mutation_interval.is_zero() {
5213 let mut last = self
5214 .last_mutation
5215 .lock()
5216 .unwrap_or_else(std::sync::PoisonError::into_inner);
5217 // `checked_add` rather than `+`: adding durations can panic on overflow, and
5218 // pacing is not worth a panic even at a bound `MAX_PACING_MS` already rules out.
5219 if let Some(earliest) = last
5220 .and_then(|previous| previous.checked_add(self.pacing.min_mutation_interval))
5221 && earliest > at
5222 {
5223 at = earliest;
5224 cause = Some(pacing::cause::MIN_INTERVAL);
5225 }
5226 *last = Some(at);
5227 }
5228 let ticket = windows.charge(at, charge);
5229 (at.saturating_sub(now), cause, ticket)
5230 };
5231 if let (false, Some(cause)) = (wait.is_zero(), cause) {
5232 self.clock.sleep_because(wait, cause).await;
5233 }
5234 ticket
5235 }
5236
5237 /// Record that the request `ticket` was charged for has finished, so each window — and
5238 /// the configured spacing — counts it from here rather than from the moment it left.
5239 ///
5240 /// This source can only choose when a request *departs*; GitHub's limiter counts when it
5241 /// *arrives*, and the two differ by whatever the request spent in transit. Counting from
5242 /// departure would hand the limiter a window that looked roomier than it is, so a source
5243 /// pacing exactly at a limit could still be seen arriving over it — which is how a copy
5244 /// paced well inside a board's threshold was once refused by it on a slower machine while
5245 /// passing on a quick one. The previous request had arrived before its response came back,
5246 /// so counting from here is never earlier than the limiter does, whatever transit costs
5247 /// and on whatever platform: the safe side of a limit that punishes being wrong by refusing
5248 /// reads for the next fifty minutes.
5249 ///
5250 /// A failed attempt is finished too: a request refused by the limiter still arrived, and
5251 /// one that never left costs only room nobody needed.
5252 fn paced(&self, ticket: pacing::Ticket, mutation: bool) {
5253 let now = self.clock.now();
5254 self.windows
5255 .lock()
5256 .unwrap_or_else(std::sync::PoisonError::into_inner)
5257 .finish(ticket, now);
5258 if mutation && !self.pacing.min_mutation_interval.is_zero() {
5259 let mut last = self
5260 .last_mutation
5261 .lock()
5262 .unwrap_or_else(std::sync::PoisonError::into_inner);
5263 // `max` rather than an assignment: a concurrent caller may already have reserved a
5264 // slot further out, and completing this request must never pull that slot back in.
5265 *last = Some(last.map_or(now, |reserved| reserved.max(now)));
5266 }
5267 }
5268
5269 /// One HTTP attempt, classified into an answer, a rate limit to wait out, or a
5270 /// failure that waiting cannot help — and recorded, whichever of the three it was.
5271 ///
5272 /// This is the one place a request leaves this crate, which is why the accounting is
5273 /// here rather than at each of the callers: a read path added later is counted without
5274 /// anybody remembering to count it, and
5275 /// `the_session_report_counts_every_request_the_board_served_and_what_each_cost` fails
5276 /// when one is not.
5277 async fn send_once(
5278 &self,
5279 query: &str,
5280 variables: &Value,
5281 number: u32,
5282 ) -> Result<Value, Attempt> {
5283 let executed = self.ledger.priced_document(query);
5284 let Attempted {
5285 result,
5286 limits,
5287 reported_cost,
5288 response_outcome,
5289 } = self.attempt(executed.as_ref(), variables).await;
5290 // No `otherwise` name: every document this source sends is one of its own, and the
5291 // inventory gate on `graphql::DOCUMENTS` is what keeps that true.
5292 let sending = accounting::Request::graphql(query, variables, None, reported_cost);
5293 let outcome = match &result {
5294 Ok(_) => accounting::Outcome::Answered,
5295 Err(Attempt::Limited(_)) => accounting::Outcome::RateLimited,
5296 Err(Attempt::Failed(_)) => accounting::Outcome::Refused,
5297 };
5298 self.ledger
5299 .record(sending.finished(outcome, limits).with_execution(
5300 Some(executed.into_owned()),
5301 response_outcome,
5302 number,
5303 ));
5304 result
5305 }
5306
5307 /// The attempt itself, with what its response said about the rate limit alongside.
5308 ///
5309 /// The two are returned together rather than recorded here because every one of the
5310 /// early exits below is a different outcome, and a record written at each of them is a
5311 /// record one of them can be added without.
5312 async fn attempt(&self, query: &str, variables: &Value) -> Attempted {
5313 let mut limits = accounting::RateLimit::default();
5314 let mut reported_cost = None;
5315 let mut response_outcome = accounting::ResponseOutcome::TransportError;
5316 let result = self
5317 .attempted(
5318 query,
5319 variables,
5320 &mut limits,
5321 &mut reported_cost,
5322 &mut response_outcome,
5323 )
5324 .await;
5325 Attempted {
5326 result,
5327 limits,
5328 reported_cost,
5329 response_outcome,
5330 }
5331 }
5332
5333 /// One HTTP attempt, filling in what its response said about the rate limit as it goes.
5334 async fn attempted(
5335 &self,
5336 query: &str,
5337 variables: &Value,
5338 limits: &mut accounting::RateLimit,
5339 reported_cost: &mut Option<u64>,
5340 response_outcome: &mut accounting::ResponseOutcome,
5341 ) -> Result<Value, Attempt> {
5342 let response = self
5343 .client()
5344 .map_err(Attempt::Failed)?
5345 .post(self.endpoint.clone())
5346 .bearer_auth(self.token.expose_secret())
5347 .json(&json!({"query": query, "variables": variables}))
5348 .send()
5349 .await
5350 .map_err(|e| {
5351 Attempt::Failed(SourceError::Unavailable {
5352 message: format!("GitHub GraphQL request failed: {}", transport_error(&e)),
5353 })
5354 })?;
5355 let status = response.status();
5356 if !status.is_success() {
5357 *response_outcome = accounting::ResponseOutcome::HttpError;
5358 }
5359 let header = |name: &str| whole_seconds(response.headers().get(name));
5360 let served = response
5361 .headers()
5362 .get(reqwest::header::DATE)
5363 .and_then(|value| value.to_str().ok())
5364 .and_then(|value| DateTime::parse_from_rfc2822(value).ok())
5365 .map(|at| at.with_timezone(&Utc));
5366 *limits = accounting::RateLimit::read(|name| {
5367 response
5368 .headers()
5369 .get(name)
5370 .and_then(|value| value.to_str().ok())
5371 .map(str::to_owned)
5372 });
5373 // Exactly `0` is exhaustion and everything else — a count, an empty value, bytes
5374 // that are not text at all — is "not known to be exhausted". This never makes a
5375 // response a refusal on its own: it says which limiter a refusal is attributed to
5376 // and where its hint comes from, so a value this cannot read costs a hint rather
5377 // than an answer.
5378 let exhausted = response
5379 .headers()
5380 .get("x-ratelimit-remaining")
5381 .and_then(|value| value.to_str().ok())
5382 == Some("0");
5383 // `retry-after` is what GitHub asks for when it asks; when it does not and the
5384 // primary budget is spent, `x-ratelimit-reset` says when that budget comes back,
5385 // which is the same question answered as an absolute time. Nothing else here is a
5386 // hint, and a schedule is what answers a refusal that carries none.
5387 let retry_after = header("retry-after");
5388 let hinted_by = if retry_after.is_some() || !exhausted {
5389 Hint::RetryAfter
5390 } else {
5391 Hint::PrimaryReset
5392 };
5393 let hint = retry_after.or_else(|| {
5394 exhausted
5395 .then(|| header("x-ratelimit-reset"))
5396 .flatten()
5397 .map(|reset| reset.saturating_sub(Utc::now().timestamp().max(0).unsigned_abs()))
5398 });
5399 // Read before it is parsed, because the evidence which tells a secondary rate
5400 // limit from a rejected credential is in the body of a response whose status says
5401 // only "forbidden" — and a non-success response was never parsed at all.
5402 let body = response.text().await.map_err(|e| {
5403 Attempt::Failed(SourceError::Unavailable {
5404 message: format!(
5405 "GitHub GraphQL response could not be read: {}",
5406 transport_error(&e)
5407 ),
5408 })
5409 })?;
5410 *reported_cost = serde_json::from_str::<Value>(&body)
5411 .ok()
5412 .as_ref()
5413 .and_then(|body| body.pointer("/data/rateLimit/cost"))
5414 .and_then(Value::as_u64);
5415 let parsed = serde_json::from_str::<Value>(&body).ok();
5416 *response_outcome = accounting::ResponseOutcome::of_graphql_reply(status, parsed.as_ref());
5417 if let Some(limiter) = Limiter::classify(status, exhausted, &body) {
5418 return Err(Attempt::Limited(Limited {
5419 limiter,
5420 hint,
5421 hinted_by,
5422 }));
5423 }
5424 if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {
5425 return Err(Attempt::Failed(SourceError::Auth {
5426 message: format!(
5427 "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"
5428 ),
5429 }));
5430 }
5431 if !status.is_success() {
5432 return Err(Attempt::Failed(SourceError::Unavailable {
5433 message: format!("GitHub GraphQL returned HTTP {status}"),
5434 }));
5435 }
5436 let answered = if query == graphql::ISSUES_BY_REFERENCE {
5437 self.answer_unresolved(&body)
5438 } else if query == graphql::CREATE_ISSUES {
5439 answer_with_errors(&body).map_or_else(|| self.answer(&body), Ok)
5440 } else {
5441 self.answer(&body)
5442 };
5443 let mut data = answered.map_err(Attempt::Failed)?;
5444 if let Some(served) = served {
5445 stamp_served(&mut data, &served.to_rfc3339());
5446 }
5447 Ok(data)
5448 }
5449
5450 /// What one successful HTTP response says, once its GraphQL errors are read.
5451 /// [`Self::answer`], for a read of issues by reference: a reference GitHub cannot resolve
5452 /// for this credential — a repository it cannot see, an issue number it does not hold —
5453 /// is answered `null` beside a `NOT_FOUND` error naming it, and that is an answer about
5454 /// that reference rather than a failure of the others. Any other error refuses the read,
5455 /// as there.
5456 fn answer_unresolved(&self, body: &str) -> Result<Value, SourceError> {
5457 let parsed: Option<Value> = serde_json::from_str(body).ok();
5458 // Each error is a `NOT_FOUND` whose path names one slot of the batch, `r<n>` or its
5459 // `issue`, and the answer holds `null` at exactly that path: anything else — another
5460 // kind of error, one naming no slot, or one beside a value it says is not there — is a
5461 // refusal of the whole read.
5462 let unresolved = |data: &Value, error: &Value| {
5463 error.get("type").and_then(Value::as_str) == Some("NOT_FOUND")
5464 && error
5465 .get("path")
5466 .and_then(Value::as_array)
5467 .is_some_and(|path| {
5468 let alias = path.first().and_then(Value::as_str).and_then(|alias| {
5469 alias
5470 .strip_prefix('r')
5471 .and_then(|slot| slot.parse::<usize>().ok())
5472 .filter(|slot| *slot < REFERENCE_BATCH)
5473 .map(|_| alias)
5474 });
5475 match (alias, path.get(1).and_then(Value::as_str), path.len()) {
5476 (Some(alias), None, 1) => data.get(alias).is_some_and(Value::is_null),
5477 (Some(alias), Some("issue"), 2) => data
5478 .get(alias)
5479 .and_then(|repository| repository.get("issue"))
5480 .is_some_and(Value::is_null),
5481 _ => false,
5482 }
5483 })
5484 };
5485 let unresolved_only = parsed.as_ref().is_some_and(|parsed| {
5486 parsed.get("data").is_some_and(|data| {
5487 data.is_object()
5488 && parsed
5489 .get("errors")
5490 .and_then(Value::as_array)
5491 .is_some_and(|errors| errors.iter().all(|error| unresolved(data, error)))
5492 })
5493 });
5494 match parsed {
5495 Some(parsed) if unresolved_only => Ok(parsed["data"].clone()),
5496 _ => self.answer(body),
5497 }
5498 }
5499
5500 fn answer(&self, body: &str) -> Result<Value, SourceError> {
5501 let body: Value = serde_json::from_str(body).map_err(|e| SourceError::Malformed {
5502 message: format!("GitHub returned invalid JSON: {e}"),
5503 })?;
5504 let errors = body
5505 .get("errors")
5506 .map(|value| {
5507 value.as_array().ok_or_else(|| SourceError::Malformed {
5508 message: "GitHub response errors is not an array".into(),
5509 })
5510 })
5511 .transpose()?;
5512 if let Some(errors) = errors.filter(|errors| !errors.is_empty()) {
5513 let messages = errors
5514 .iter()
5515 .filter_map(|e| e.get("message").and_then(Value::as_str))
5516 .collect::<Vec<_>>()
5517 .join("; ");
5518 let message = if messages.is_empty() {
5519 "GitHub returned GraphQL errors".into()
5520 } else {
5521 messages
5522 };
5523 let normalized = message.to_ascii_lowercase();
5524 if normalized.contains("resource not accessible") || normalized.contains("scope") {
5525 return Err(SourceError::Auth {
5526 message: format!(
5527 "{message}; grant {} Projects and Issues read/write plus Pull requests read-only access for every repository represented on the board",
5528 self.credential_name
5529 ),
5530 });
5531 }
5532 return Err(SourceError::Refused { message });
5533 }
5534 body.get("data")
5535 .filter(|data| data.is_object())
5536 .cloned()
5537 .ok_or_else(|| SourceError::Malformed {
5538 message: "GitHub response has no data object".into(),
5539 })
5540 }
5541
5542 // llmlint: ignore[boundary_inputs_validated] GitHub caps nested connections at 100 and
5543 // GraphQL cannot independently page them inside the outer item page. This source page is
5544 // deliberately bounded at that published maximum; the live drift journey exercises it.
5545 async fn board_page(
5546 &self,
5547 items_after: Option<&str>,
5548 items_first: u32,
5549 ) -> Result<Value, SourceError> {
5550 let data = self
5551 .graphql(
5552 graphql::BOARD,
5553 json!({"owner":self.owner,"number":self.project_number,
5554 "first":items_first.min(MAX_PAGE_SIZE),"after":items_after,
5555 "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
5556 )
5557 .await?;
5558 data.pointer("/owner/projectV2")
5559 .filter(|v| !v.is_null())
5560 .cloned()
5561 .ok_or_else(|| SourceError::Refused {
5562 message: format!(
5563 "GitHub project {}/{} was not found or is not visible to the token",
5564 self.owner, self.project_number
5565 ),
5566 })
5567 }
5568
5569 /// The search that finds the issues of this board, narrowed by `also` when it is
5570 /// given.
5571 ///
5572 /// `project:owner/number` is what scopes a search to one board, and `is:issue` is what
5573 /// keeps pull requests out of it: GitHub's `ISSUE` search type covers both, and a pull
5574 /// request is somebody's change rather than a unit of plan. `-has:parent` is *not*
5575 /// here on purpose — GitHub accepts it and silently ignores it, so a project is told
5576 /// from a task by the `parent` field each issue carries rather than by the search.
5577 fn board_search(&self, also: Option<&str>) -> String {
5578 let scope = format!("project:{}/{} is:issue", self.owner, self.project_number);
5579 match also {
5580 Some(also) => format!("{scope} {also}"),
5581 None => scope,
5582 }
5583 }
5584
5585 /// One issue this source reached directly, as the board item a read of the board would
5586 /// have produced — or `None` when this board does not hold it.
5587 ///
5588 /// The board half of an issue rides along on `Issue.projectItems`, so the value handed
5589 /// to [`Self::resolve`] is the very shape a `ProjectV2.items` read gives it: the board
5590 /// item's own id, that item's field values, and the issue as its content. One resolver
5591 /// for both routes is what makes an issue read through a search, through its own node
5592 /// id, or through its project's sub-issues report the same title, the same status, the
5593 /// same labels and the same qualified id.
5594 ///
5595 /// An issue with no entry for *this* board is not this source's to report, which is
5596 /// what keeps an id naming some other repository's issue from being answered as an item
5597 /// of this board. That answer is given about an **exhausted** connection and never
5598 /// about an unread page: the entry is looked for on the page in hand, and only if that
5599 /// page reports more of the connection, in [`Self::board_membership`]'s walk of the
5600 /// rest of it.
5601 async fn resolve_issue(&self, issue: &Value) -> Result<Option<Resolved>, SourceError> {
5602 self.resolve_issue_as(issue, true).await
5603 }
5604
5605 /// [`Self::resolve_issue`], remembering what it resolves for this command's later reads
5606 /// and writes only when `remember` says to.
5607 async fn resolve_issue_as(
5608 &self,
5609 issue: &Value,
5610 remember: bool,
5611 ) -> Result<Option<Resolved>, SourceError> {
5612 if optional_str(issue, "__typename")? != Some("Issue") {
5613 return Ok(None);
5614 }
5615 let memberships = issue
5616 .get("projectItems")
5617 .ok_or_else(|| SourceError::Malformed {
5618 message: "GitHub issue is missing projectItems".into(),
5619 })?;
5620 let nodes = memberships
5621 .get("nodes")
5622 .and_then(Value::as_array)
5623 .ok_or_else(|| SourceError::Malformed {
5624 message: "GitHub issue projectItems.nodes is not an array".into(),
5625 })?;
5626 let held = match self.board_entry(nodes) {
5627 Some(held) => held.clone(),
5628 None => {
5629 let info = memberships
5630 .get("pageInfo")
5631 .ok_or_else(|| SourceError::Malformed {
5632 message: "GitHub issue projectItems has no pageInfo".into(),
5633 })?;
5634 // The page held no entry for this board. Whether that means the issue is
5635 // not on it is a question about the rest of the connection, and only a
5636 // connection with no rest answers it here.
5637 if !required_bool(info, "hasNextPage")? {
5638 return Ok(None);
5639 }
5640 let cursor = required_str(info, "endCursor")?;
5641 validate_cursor_progress(None, cursor)?;
5642 let issue_id = required_str(issue, "id")?;
5643 match self.board_membership(issue_id, cursor).await? {
5644 Some(held) => held,
5645 None => return Ok(None),
5646 }
5647 }
5648 };
5649 // The membership's own `updatedAt` rides along: it is the board half of the item's change
5650 // token, which a field change moves where the issue's own time does not.
5651 let item = json!({
5652 "id": required_str(&held, "id")?,
5653 "updatedAt": held.get("updatedAt"),
5654 "project": held.get("project"),
5655 "fieldValues": held.get("fieldValues"),
5656 "content": issue,
5657 });
5658 if remember {
5659 self.resolve(&item)
5660 } else {
5661 self.resolve_unremembered(&item)
5662 }
5663 }
5664
5665 /// This board's own entry among one page of an issue's `Issue.projectItems`.
5666 ///
5667 /// One spelling of *which membership is this board's*, so the page a read carries and
5668 /// the pages [`Self::board_membership`] walks are searched by the same rule.
5669 fn board_entry<'a>(&self, nodes: &'a [Value]) -> Option<&'a Value> {
5670 nodes.iter().find(|node| {
5671 node.pointer("/project/number").and_then(Value::as_u64)
5672 == Some(u64::from(self.project_number))
5673 })
5674 }
5675
5676 /// The rest of one issue's board memberships, from `after`, for this board's entry.
5677 ///
5678 /// The recovery read: a page of memberships that holds no entry for this board says
5679 /// nothing about the memberships past it, so the connection is walked to exhaustion
5680 /// before an issue is reported as one this board does not hold. `Ok(None)` is that
5681 /// positive answer — the whole connection was read and no entry named this board —
5682 /// rather than a failure, and the walk is held to
5683 /// [`validate_cursor_progress`] like every other page walk here, so a source answering
5684 /// with a cursor that does not advance is refused instead of spun on.
5685 async fn board_membership(
5686 &self,
5687 issue: &str,
5688 after: &str,
5689 ) -> Result<Option<Value>, SourceError> {
5690 let mut after = after.to_owned();
5691 loop {
5692 let data = self
5693 .graphql(
5694 graphql::ISSUE_BOARD_ITEMS,
5695 json!({"id":issue,"first":MAX_PAGE_SIZE,"after":after,
5696 "nestedFirst":NESTED_PAGE_SIZE}),
5697 )
5698 .await?;
5699 let Some(connection) = data
5700 .pointer("/node/projectItems")
5701 .filter(|value| !value.is_null())
5702 else {
5703 // The id resolved to nothing, or to something with no memberships to walk —
5704 // which is the same answer as a connection holding no entry for this board.
5705 return Ok(None);
5706 };
5707 let nodes = connection
5708 .get("nodes")
5709 .and_then(Value::as_array)
5710 .ok_or_else(|| SourceError::Malformed {
5711 message: "GitHub issue projectItems.nodes is not an array".into(),
5712 })?;
5713 if let Some(held) = self.board_entry(nodes) {
5714 return Ok(Some(held.clone()));
5715 }
5716 let info = connection
5717 .get("pageInfo")
5718 .ok_or_else(|| SourceError::Malformed {
5719 message: "GitHub issue projectItems has no pageInfo".into(),
5720 })?;
5721 let next = required_bool(info, "hasNextPage")?
5722 .then(|| required_str(info, "endCursor"))
5723 .transpose()?;
5724 match next {
5725 Some(next) => {
5726 validate_cursor_progress(Some(&after), next)?;
5727 after = next.to_owned();
5728 }
5729 None => return Ok(None),
5730 }
5731 }
5732 }
5733
5734 /// One page of a board-scoped issue search, and where the next page resumes.
5735 async fn search_page(
5736 &self,
5737 search: &str,
5738 first: u32,
5739 after: Option<&str>,
5740 ) -> Result<(Vec<Resolved>, Option<String>), SourceError> {
5741 let data = self
5742 .graphql(
5743 graphql::SEARCH_ISSUES,
5744 json!({"search":search,"type":"ISSUE","first":first.min(MAX_PAGE_SIZE),
5745 "after":after,"nestedFirst":NESTED_PAGE_SIZE,
5746 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
5747 )
5748 .await?;
5749 let connection = data.get("search").ok_or_else(|| SourceError::Malformed {
5750 message: "GitHub search response has no search connection".into(),
5751 })?;
5752 let mut found = Vec::new();
5753 for node in connection
5754 .get("nodes")
5755 .and_then(Value::as_array)
5756 .ok_or_else(|| SourceError::Malformed {
5757 message: "GitHub search nodes is not an array".into(),
5758 })?
5759 {
5760 if let Some(resolved) = self.resolve_issue(node).await? {
5761 found.push(resolved);
5762 }
5763 }
5764 let info = connection
5765 .get("pageInfo")
5766 .ok_or_else(|| SourceError::Malformed {
5767 message: "GitHub search connection has no pageInfo".into(),
5768 })?;
5769 let next = required_bool(info, "hasNextPage")?
5770 .then(|| required_str(info, "endCursor"))
5771 .transpose()?
5772 .map(str::to_owned);
5773 if let Some(next) = &next {
5774 validate_cursor_progress(after, next)?;
5775 }
5776 Ok((found, next))
5777 }
5778
5779 /// Every issue this board holds, completed with what this run wrote.
5780 ///
5781 /// The completion is not an optimisation and it is not a cache: GitHub's issue search
5782 /// is an index and is eventually consistent, so an issue this run created seconds ago
5783 /// can be absent from it, and a project listed straight after being written would
5784 /// otherwise be missing from its own board. What is added back is only what this
5785 /// process itself wrote, out of [`Self::created`], which lives and dies with the
5786 /// process.
5787 async fn board_issues(&self) -> Result<Vec<Resolved>, SourceError> {
5788 let found = self.searched_issues().await?;
5789 self.completed_with_written(found, |_| true)
5790 }
5791
5792 /// Every issue this board's own search reports, walked to exhaustion, read once per
5793 /// source.
5794 ///
5795 /// The uncompleted half of [`Self::board_issues`], separated because [`Self::board`]
5796 /// needs it too and the two would otherwise walk the same search twice in one command.
5797 /// See [`Self::search_cache`] for why holding it is the same bargain holding the board
5798 /// is.
5799 async fn searched_issues(&self) -> Result<Vec<Resolved>, SourceError> {
5800 let cached = self.search_cache()?.clone();
5801 if let Some(held) = cached {
5802 return Ok(held);
5803 }
5804 let mut after: Option<String> = None;
5805 let mut found = Vec::new();
5806 let search = self.board_search(None);
5807 loop {
5808 let (page, next) = self
5809 .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
5810 .await?;
5811 found.extend(page);
5812 match next {
5813 Some(next) => after = Some(next),
5814 None => break,
5815 }
5816 }
5817 *self.search_cache()? = Some(found.clone());
5818 Ok(found)
5819 }
5820
5821 /// Every issue this board's own search reports with `also` beside its scope, walked to
5822 /// exhaustion — held for no later read, which may not be narrowed alike.
5823 async fn searched_narrowly(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
5824 let mut after: Option<String> = None;
5825 let mut found = Vec::new();
5826 let search = self.board_search(Some(also));
5827 loop {
5828 let (page, next) = self
5829 .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
5830 .await?;
5831 found.extend(page);
5832 match next {
5833 Some(next) => after = Some(next),
5834 None => return Ok(found),
5835 }
5836 }
5837 }
5838
5839 /// This process's own view of the board's issues, or the refusal a poisoned lock is.
5840 fn search_cache(
5841 &self,
5842 ) -> Result<std::sync::MutexGuard<'_, Option<Vec<Resolved>>>, SourceError> {
5843 self.search_cache
5844 .lock()
5845 .map_err(|_| SourceError::Unavailable {
5846 message: "this source's view of the board's issues was left inconsistent by an \
5847 earlier failure; next: run the command again"
5848 .into(),
5849 })
5850 }
5851
5852 /// `found`, with everything this run wrote that `keep` accepts and the read did not
5853 /// report.
5854 ///
5855 /// See [`Self::created`] and [`Self::board_issues`] for why a read has to be completed
5856 /// at all: the search index is behind, and a node read of an item filed moments ago can
5857 /// be too.
5858 fn completed_with_written(
5859 &self,
5860 mut found: Vec<Resolved>,
5861 keep: impl Fn(&Resolved) -> bool,
5862 ) -> Result<Vec<Resolved>, SourceError> {
5863 for own in self.created()?.iter().filter(|own| keep(own)) {
5864 if !found.iter().any(|item| item.id == own.id) {
5865 found.push(own.clone());
5866 }
5867 }
5868 Ok(found)
5869 }
5870
5871 /// What resolving one node id reached.
5872 ///
5873 /// Three answers rather than an `Option`, because a board *draft* is none of the other
5874 /// two: it is not an issue, so the issue fragment reads nothing of it, and a read of one
5875 /// is completed by a read of the draft itself rather than reported as nothing.
5876 async fn reach(&self, id: &NativeId) -> Result<Reached, SourceError> {
5877 let asked = self
5878 .graphql(
5879 graphql::ISSUE,
5880 json!({"id":id.0,"first":MAX_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
5881 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
5882 )
5883 .await;
5884 let data = match asked {
5885 Ok(data) => data,
5886 // A string that is not a node id at all is not a failure to report: it is an id
5887 // this board does not hold, which is what every read of one already answers.
5888 Err(error) if unresolvable_node(&error) => return Ok(Reached::Nothing),
5889 Err(error) => return Err(error),
5890 };
5891 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
5892 return Ok(Reached::Nothing);
5893 };
5894 if optional_str(node, "__typename")? == Some("DraftIssue") {
5895 return Ok(Reached::Draft);
5896 }
5897 Ok(match self.resolve_issue(node).await? {
5898 Some(item) => Reached::Held(Box::new(item)),
5899 None => Reached::Nothing,
5900 })
5901 }
5902
5903 /// One item of this board by its own id, whatever kind it is.
5904 ///
5905 /// Resolved from the identifier alone: no search, board-wide or otherwise. What this
5906 /// run wrote is read first, because a node read of an item created moments ago can
5907 /// still be behind the board field values written onto it — see [`Self::created`].
5908 async fn item_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
5909 if let Some(own) = self.created()?.iter().find(|own| own.id == *id) {
5910 return Ok(Some(own.clone()));
5911 }
5912 if let Some(held) = self.read_ahead()?.remove(id) {
5913 return Ok(held);
5914 }
5915 match self.reach(id).await? {
5916 Reached::Held(item) => Ok(Some(*item)),
5917 Reached::Nothing => Ok(None),
5918 Reached::Draft => self.draft_by_id(id).await,
5919 }
5920 }
5921
5922 /// [`Self::item_by_id`] for a document: one [`graphql::DOCUMENT_IN_PROJECT`] read, which
5923 /// answers the project it is filed under and that project's members beside it.
5924 ///
5925 /// Each is held for this command as a read of it would have been: the project for the
5926 /// next read of it by its id, and — when its page of members is the whole of them — the
5927 /// members for the next listing of that project. So a write held to who may read the
5928 /// document, which reads all three, is answered by this one request.
5929 async fn document_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
5930 if self.created()?.iter().any(|own| own.id == *id) || self.read_ahead()?.contains_key(id) {
5931 return self.item_by_id(id).await;
5932 }
5933 let asked = self
5934 .graphql(
5935 graphql::DOCUMENT_IN_PROJECT,
5936 json!({"id":id.0,"first":MAX_PAGE_SIZE,"memberFirst":MEMBER_PAGE_SIZE,
5937 "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
5938 "duplicates":true}),
5939 )
5940 .await;
5941 let data = match asked {
5942 Ok(data) => data,
5943 Err(error) if unresolvable_node(&error) => return Ok(None),
5944 Err(error) => return Err(error),
5945 };
5946 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
5947 return Ok(None);
5948 };
5949 if optional_str(node, "__typename")? == Some("DraftIssue") {
5950 return self.draft_by_id(id).await;
5951 }
5952 let Some(item) = self.resolve_issue(node).await? else {
5953 return Ok(None);
5954 };
5955 // An answer for another issue than the one asked about says nothing of that one.
5956 if item.id != *id {
5957 return Err(SourceError::Malformed {
5958 message: format!(
5959 "GitHub answered issue {} when asked for document {}",
5960 item.id.0, id.0
5961 ),
5962 });
5963 }
5964 if let Some(project) = node.get("project").filter(|value| !value.is_null())
5965 && let Some(parent) = self
5966 .resolve_issue(project)
5967 .await?
5968 .filter(|parent| Some(&parent.id) == item.parent.as_ref())
5969 {
5970 let members = project.get("subIssues").filter(|value| !value.is_null());
5971 let whole = members
5972 .and_then(|members| members.pointer("/pageInfo/hasNextPage"))
5973 .and_then(Value::as_bool)
5974 == Some(false);
5975 if whole {
5976 let nodes = members
5977 .and_then(|members| members.get("nodes"))
5978 .and_then(Value::as_array)
5979 .ok_or_else(|| SourceError::Malformed {
5980 message: "GitHub subIssues.nodes is not an array".into(),
5981 })?;
5982 let mut children = Vec::new();
5983 for member in nodes {
5984 if let Some(resolved) = self.resolve_issue(member).await? {
5985 children.push(resolved);
5986 }
5987 }
5988 // Held as the project's members only when every one resolved and says it is
5989 // filed under it: a page with any other answers nothing of all the members.
5990 if children.len() == nodes.len()
5991 && children
5992 .iter()
5993 .all(|child| child.parent.as_ref() == Some(&parent.id))
5994 {
5995 self.children_cache()?
5996 .insert(parent.id.clone(), (parent.id.clone(), children));
5997 }
5998 }
5999 self.read_ahead()?.insert(parent.id.clone(), Some(parent));
6000 }
6001 Ok(Some(item))
6002 }
6003
6004 /// Several items of this board, each by its own id, in order — what [`Self::item_by_id`]
6005 /// answers for each, read [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] rather
6006 /// than one request per id.
6007 ///
6008 /// What this run wrote answers first, as it does there, and only the rest is read. One id
6009 /// left to read is read by [`Self::item_by_id`] itself, which costs what a batch does. A
6010 /// batch GitHub refuses because one of its ids resolves to no node at all is read again one
6011 /// id at a time, so that id is answered as not held and the others as themselves; a draft
6012 /// is completed by a read of the draft, exactly as there.
6013 async fn items_by_ids(&self, ids: &[NativeId]) -> Result<Vec<Option<Resolved>>, SourceError> {
6014 let mut found: Vec<Option<Option<Resolved>>> = {
6015 let created = self.created()?;
6016 ids.iter()
6017 .map(|id| {
6018 created
6019 .iter()
6020 .find(|own| own.id == *id)
6021 .map(|own| Some(own.clone()))
6022 })
6023 .collect()
6024 };
6025 let unread: Vec<NativeId> = ids
6026 .iter()
6027 .zip(&found)
6028 .filter(|(_, found)| found.is_none())
6029 .map(|(id, _)| id.clone())
6030 .collect();
6031 let mut read = Vec::with_capacity(unread.len());
6032 if let [one] = unread.as_slice() {
6033 read.push(self.item_by_id(one).await?);
6034 } else {
6035 for batch in unread.chunks(DETAIL_BATCH) {
6036 let data = match self
6037 .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, None))
6038 .await
6039 {
6040 Ok(data) => data,
6041 Err(error) if unresolvable_node(&error) => {
6042 for id in batch {
6043 read.push(self.item_by_id(id).await?);
6044 }
6045 continue;
6046 }
6047 Err(error) => return Err(error),
6048 };
6049 for (slot, id) in batch.iter().enumerate() {
6050 let node =
6051 data.get(format!("i{slot}"))
6052 .ok_or_else(|| SourceError::Malformed {
6053 message: format!(
6054 "GitHub answered a batch read with no item for {}",
6055 id.0
6056 ),
6057 })?;
6058 read.push(if node.is_null() {
6059 None
6060 } else if optional_str(node, "__typename")? == Some("DraftIssue") {
6061 self.draft_by_id(id).await?
6062 } else {
6063 if optional_str(node, "__typename")? == Some("Issue")
6064 && required_str(node, "id")? != id.0
6065 {
6066 return Err(SourceError::Malformed {
6067 message: format!(
6068 "GitHub answered the read of {} with issue {}",
6069 id.0,
6070 required_str(node, "id")?
6071 ),
6072 });
6073 }
6074 self.resolve_issue(node).await?
6075 });
6076 }
6077 }
6078 }
6079 let mut read = read.into_iter();
6080 Ok(found
6081 .iter_mut()
6082 .map(|slot| slot.take().unwrap_or_else(|| read.next().flatten()))
6083 .collect())
6084 }
6085
6086 fn read_ahead(
6087 &self,
6088 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Option<Resolved>>>, SourceError> {
6089 self.read_ahead
6090 .lock()
6091 .map_err(|_| SourceError::Unavailable {
6092 message:
6093 "items read ahead for a copy were left inconsistent; run the command again"
6094 .into(),
6095 })
6096 }
6097
6098 fn resolved_cache(
6099 &self,
6100 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
6101 self.resolved_cache
6102 .lock()
6103 .map_err(|_| SourceError::Unavailable {
6104 message: "resolved item records were left inconsistent; run the command again"
6105 .into(),
6106 })
6107 }
6108
6109 /// Reuse a record this invocation already resolved. The mutation sender invalidates
6110 /// it before writing, so a partial failure cannot leave a pre-write binding behind.
6111 async fn bound_item(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6112 let cached = self.resolved_cache()?.get(id).cloned();
6113 if let Some(item) = cached {
6114 return Ok(Some(item));
6115 }
6116 self.item_by_id(id).await
6117 }
6118
6119 /// Take out the item an issue read by reference reached for `id`, when one is held: the
6120 /// status write it was read for is the one caller, so no other read or write is ever
6121 /// answered from a read that selected only what that write needs.
6122 fn take_held(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6123 Ok(self
6124 .held_cache
6125 .lock()
6126 .map_err(|_| SourceError::Unavailable {
6127 message: "items read by issue reference were left inconsistent; run the \
6128 command again"
6129 .into(),
6130 })?
6131 .remove(id))
6132 }
6133
6134 /// One board draft by its own id, with the board item it sits in — or `None` when no
6135 /// item of this board is that draft's.
6136 ///
6137 /// The same decision [`Self::resolve_issue`] makes for an issue, over the draft's own
6138 /// `projectV2Items`: an entry naming this board is what makes it this board's. GitHub
6139 /// links a draft to one board item, so the page this read carries is the whole of that
6140 /// connection, and a page that reports more than it holds is refused rather than read
6141 /// as an answer about memberships nobody read.
6142 async fn draft_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6143 let data = self
6144 .graphql(
6145 graphql::DRAFT,
6146 json!({"id":id.0,"nestedFirst":NESTED_PAGE_SIZE,
6147 "boardItems":BOARD_ITEMS_PAGE_SIZE}),
6148 )
6149 .await?;
6150 // Gone between the two reads is an answer — the draft is no longer there. Anything
6151 // else than the draft [`Self::reach`] was just told this id is, is not one.
6152 let Some(draft) = data.get("node").filter(|node| !node.is_null()) else {
6153 return Ok(None);
6154 };
6155 if optional_str(draft, "__typename")? != Some("DraftIssue") {
6156 return Err(SourceError::Malformed {
6157 message: format!(
6158 "GitHub answered {} as a draft and then as something else",
6159 id.0
6160 ),
6161 });
6162 }
6163 if required_str(draft, "id")? != id.0 {
6164 return Err(SourceError::Malformed {
6165 message: format!("GitHub answered a different draft for {}", id.0),
6166 });
6167 }
6168 let memberships = draft
6169 .get("projectV2Items")
6170 .ok_or_else(|| SourceError::Malformed {
6171 message: format!("GitHub draft {} is missing projectV2Items", id.0),
6172 })?;
6173 let nodes = memberships
6174 .get("nodes")
6175 .and_then(Value::as_array)
6176 .ok_or_else(|| SourceError::Malformed {
6177 message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
6178 })?;
6179 let info = memberships
6180 .get("pageInfo")
6181 .ok_or_else(|| SourceError::Malformed {
6182 message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
6183 })?;
6184 // Read whether or not this board's entry is on the page: a page claiming more than
6185 // the one item GitHub links a draft to is a malformed answer either way.
6186 if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
6187 return Err(SourceError::Malformed {
6188 message: format!(
6189 "GitHub draft {} reports more board items than the one GitHub links a draft \
6190 to",
6191 id.0
6192 ),
6193 });
6194 }
6195 if let Some(node) = nodes.first()
6196 && node
6197 .pointer("/project/number")
6198 .and_then(Value::as_u64)
6199 .is_none()
6200 {
6201 return Err(SourceError::Malformed {
6202 message: format!(
6203 "GitHub draft {} board item has no numeric project number",
6204 id.0
6205 ),
6206 });
6207 }
6208 let Some(held) = self.board_entry(nodes) else {
6209 return Ok(None);
6210 };
6211 if required_str(
6212 held.get("project").ok_or_else(|| SourceError::Malformed {
6213 message: format!("GitHub draft {} board item has no project", id.0),
6214 })?,
6215 "id",
6216 )? != self.board_fields().await?.id.as_str()
6217 {
6218 return Ok(None);
6219 }
6220 let item = json!({
6221 "id": required_str(held, "id")?,
6222 "project": held.get("project"),
6223 "fieldValues": held.get("fieldValues"),
6224 "content": draft,
6225 });
6226 self.resolve(&item)
6227 }
6228
6229 /// The board's own id and field definitions, for a write whose item does not carry
6230 /// them — never its items.
6231 ///
6232 /// A board this command has already listed supplies them, since it read them beside its
6233 /// items; otherwise they come from [`graphql::BOARD_FIELDS`], once per command. Neither
6234 /// is consulted about which items the board holds: see the module documentation for
6235 /// why a question about one known item is answered by reading that item.
6236 async fn board_fields(&self) -> Result<BoardFields, SourceError> {
6237 if let Some(board) = self.board_cache()?.as_ref() {
6238 return Ok(BoardFields {
6239 id: BoardId::parse(&board.id)?,
6240 fields: board.fields.clone(),
6241 });
6242 }
6243 if let Some(held) = self.fields_cache()?.clone() {
6244 return Ok(held);
6245 }
6246 let data = self
6247 .graphql(
6248 graphql::BOARD_FIELDS,
6249 json!({"owner":self.owner,"number":self.project_number,
6250 "nestedFirst":NESTED_PAGE_SIZE}),
6251 )
6252 .await?;
6253 self.fields_read(&data)
6254 }
6255
6256 /// The board's id and fields out of an answer carrying the `boardFields` root, held for
6257 /// the rest of this command.
6258 fn fields_read(&self, data: &Value) -> Result<BoardFields, SourceError> {
6259 let board = data
6260 .pointer("/boardFields/projectV2")
6261 .filter(|value| !value.is_null())
6262 .ok_or_else(|| SourceError::Refused {
6263 message: format!(
6264 "GitHub project {}/{} was not found or is not visible to the token",
6265 self.owner, self.project_number
6266 ),
6267 })?;
6268 let read = BoardFields {
6269 id: BoardId::parse(required_str(board, "id")?)?,
6270 fields: board.get("fields").cloned().unwrap_or(Value::Null),
6271 };
6272 *self.fields_cache()? = Some(read.clone());
6273 Ok(read)
6274 }
6275
6276 /// Read what creating an issue in `repository` needs and this command has not read yet —
6277 /// the board's fields and the repository's node id — in one request when it needs both.
6278 ///
6279 /// When either is already known this sends nothing, and the other is read by its own
6280 /// document where it is asked for, so no create reads anything twice.
6281 async fn creation_context(
6282 &self,
6283 repository: &RepositoryTarget,
6284 incoming: &Incoming<'_>,
6285 ) -> Result<(), SourceError> {
6286 let fields_known = self.board_cache()?.is_some() || self.fields_cache()?.is_some();
6287 if fields_known || self.repository_cache()?.contains_key(repository) {
6288 return Ok(());
6289 }
6290 let data = self
6291 .graphql(
6292 graphql::CREATION_CONTEXT,
6293 json!({"owner":self.owner,"number":self.project_number,
6294 "nestedFirst":NESTED_PAGE_SIZE,"repositoryOwner":repository.owner,
6295 "repositoryName":repository.name}),
6296 )
6297 .await?;
6298 self.fields_read(&data)?;
6299 self.repository_read(&data, repository, incoming)?;
6300 Ok(())
6301 }
6302
6303 /// This process's own view of the board's fields, or the refusal a poisoned lock is.
6304 fn fields_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<BoardFields>>, SourceError> {
6305 self.fields_cache
6306 .lock()
6307 .map_err(|_| SourceError::Unavailable {
6308 message: "this source's view of the board's fields was left inconsistent by an \
6309 earlier failure; next: run the command again"
6310 .into(),
6311 })
6312 }
6313
6314 /// What a write to `item` needs of the board, read off that item when it says enough and
6315 /// off [`Self::board_fields`] when it does not.
6316 ///
6317 /// A node read of an item names its board and carries the definition of every field it
6318 /// holds a value of — so an item naming its board, holding a value of the origin field,
6319 /// and, when the write carries a status, holding a `Status` value, needs no read of the
6320 /// board at all. **Nothing the item does not say is guessed:** a field it holds no value
6321 /// of may still be on the board, and a view reading it as absent would refuse a write the
6322 /// board can take or skip a field write the board needs, so such an item — and a create,
6323 /// which has no item yet — takes the board's fields from their own read instead.
6324 async fn fields_for(
6325 &self,
6326 item: Option<&Resolved>,
6327 writes_status: bool,
6328 selects_priority: bool,
6329 ) -> Result<BoardFields, SourceError> {
6330 if let Some(board) = item.and_then(Resolved::carried_board) {
6331 return Ok(board);
6332 }
6333 if let Some(item) = item
6334 && let Some(board_id) = item.named_board()
6335 && item.defines(ORIGIN_FIELD)
6336 && (!writes_status || item.defines("Status"))
6337 && (!selects_priority || item.defines(PRIORITY_FIELD))
6338 && self
6339 .metadata_fields
6340 .iter()
6341 .all(|projection| item.defines(&projection.field))
6342 {
6343 return Ok(BoardFields {
6344 id: board_id,
6345 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
6346 });
6347 }
6348 self.board_fields().await
6349 }
6350
6351 /// Everything filed under one issue of this board, walked to exhaustion — or `None`
6352 /// when that id names nothing here with a sub-issue relationship to walk.
6353 ///
6354 /// `None` and an empty answer are different: `None` is *this is not an issue of this
6355 /// GitHub*, which is what sends a project selector on to be read as a name, and an
6356 /// empty vector is a project that holds nothing.
6357 async fn sub_issues(&self, id: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
6358 let mut after: Option<String> = None;
6359 let mut children = Vec::new();
6360 // The project issue as the first page carried it, held once the walk has ended.
6361 let mut parent: Option<Resolved> = None;
6362 loop {
6363 let asked = self
6364 .graphql(
6365 graphql::SUB_ISSUES,
6366 json!({"id":id.0,"memberFirst":MEMBER_PAGE_SIZE,"after":after,
6367 "nestedFirst":NESTED_PAGE_SIZE,
6368 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
6369 )
6370 .await;
6371 let data = match asked {
6372 Ok(data) => data,
6373 // A string that is not a node id at all is not a failure to report: it is
6374 // the ordinary answer to a selector naming a project by its name.
6375 Err(error) if unresolvable_node(&error) => return Ok(None),
6376 Err(error) => return Err(error),
6377 };
6378 let Some(connection) = data
6379 .pointer("/node/subIssues")
6380 .filter(|value| !value.is_null())
6381 else {
6382 // No such node, or one with no sub-issue relationship — a board draft is
6383 // the one this board can really hold.
6384 return Ok(None);
6385 };
6386 if after.is_none()
6387 && let Some(node) = data.get("node").filter(|node| node.get("id").is_some())
6388 {
6389 parent = self
6390 .resolve_issue(node)
6391 .await?
6392 .filter(|resolved| resolved.id == *id);
6393 }
6394 for node in connection
6395 .get("nodes")
6396 .and_then(Value::as_array)
6397 .ok_or_else(|| SourceError::Malformed {
6398 message: "GitHub subIssues.nodes is not an array".into(),
6399 })?
6400 {
6401 if let Some(resolved) = self.resolve_issue(node).await? {
6402 children.push(resolved);
6403 }
6404 }
6405 let info = connection
6406 .get("pageInfo")
6407 .ok_or_else(|| SourceError::Malformed {
6408 message: "GitHub subIssues connection has no pageInfo".into(),
6409 })?;
6410 let next = required_bool(info, "hasNextPage")?
6411 .then(|| required_str(info, "endCursor"))
6412 .transpose()?;
6413 match next {
6414 Some(next) => {
6415 validate_cursor_progress(after.as_deref(), next)?;
6416 after = Some(next.to_owned());
6417 }
6418 None => {
6419 // Held only once the walk has ended, so one failing part way holds
6420 // nothing and every read it would have answered is made.
6421 // The project issue the first page carried answers the next read of the
6422 // project by its id in this command, as a read of it made now would: what
6423 // classifying a project for a write reads straight after its members.
6424 if let Some(parent) = &parent {
6425 self.read_ahead()?
6426 .entry(parent.id.clone())
6427 .or_insert_with(|| Some(parent.clone()));
6428 }
6429 let mut carried = self.carried()?;
6430 for item in parent.into_iter().chain(children.iter().cloned()) {
6431 carried.insert(item.id.clone(), item);
6432 }
6433 return Ok(Some(children));
6434 }
6435 }
6436 }
6437 }
6438
6439 fn carried(
6440 &self,
6441 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
6442 self.carried.lock().map_err(|_| SourceError::Unavailable {
6443 message: "items a project's listing carried were left inconsistent; run the \
6444 command again"
6445 .into(),
6446 })
6447 }
6448
6449 /// Which issue of this board a project *name* is, or `None` when none is.
6450 ///
6451 /// One bounded query which filters on that name at the server, rather than a walk of
6452 /// every issue the board holds. The name is compared again here: the qualifier narrows
6453 /// what GitHub sends, and this source decides what it names.
6454 async fn project_by_name(&self, name: &str) -> Result<Option<NativeId>, SourceError> {
6455 let search = self.board_search(Some(&title_qualifier(name)));
6456 let mut after = None;
6457 loop {
6458 let (candidates, next) = self
6459 .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
6460 .await?;
6461 if let Some(item) = candidates.into_iter().find(|item| {
6462 item.kind == BoardKind::Work(ItemKind::Project)
6463 && item.title.eq_ignore_ascii_case(name)
6464 }) {
6465 return Ok(Some(item.id));
6466 }
6467 match next {
6468 Some(next) => after = Some(next),
6469 None => return Ok(None),
6470 }
6471 }
6472 }
6473
6474 /// Everything filed under one project of this board: the sub-issues of the issue that
6475 /// project is.
6476 ///
6477 /// Tasks *and* documents, because a document filed under a project is a sub-issue of it
6478 /// too — the caller keeps the kind it asked for. Nothing about this grows as the board
6479 /// gains projects, or as another project gains tasks.
6480 ///
6481 /// A qualified id names the issue and is asked for its sub-issues directly: one
6482 /// request, no search of any kind. Only a selector GitHub cannot resolve that way is
6483 /// read as a project *name*, which costs the one bounded search
6484 /// [`Self::project_by_name`] makes.
6485 ///
6486 /// What GitHub answered is held for the rest of the command — see [`Self::children_cache`]
6487 /// — and each answer is still cut to the items whose parent is this project, so one this
6488 /// process has since filed elsewhere is not reported here, and completed with what this
6489 /// process filed under it.
6490 async fn project_children(&self, selector: &NativeId) -> Result<Vec<Resolved>, SourceError> {
6491 let held = self.children_cache()?.get(selector).cloned();
6492 let (project, children) = match held {
6493 Some(held) => held,
6494 None => {
6495 let answered = match self.sub_issues(selector).await? {
6496 Some(children) => (selector.clone(), children),
6497 None => match self.project_by_name(&selector.0).await? {
6498 Some(project) => {
6499 let children = self.sub_issues(&project).await?.unwrap_or_default();
6500 (project, children)
6501 }
6502 None => return Ok(Vec::new()),
6503 },
6504 };
6505 self.children_cache()?
6506 .insert(selector.clone(), answered.clone());
6507 answered
6508 }
6509 };
6510 let mut children: Vec<Resolved> = children
6511 .into_iter()
6512 .filter(|child| child.parent.as_ref() == Some(&project))
6513 .collect();
6514 for own in self.updated()?.iter() {
6515 if own.parent.as_ref() == Some(&project) && !children.iter().any(|c| c.id == own.id) {
6516 children.push(own.clone());
6517 }
6518 }
6519 self.completed_with_written(children, |own| own.parent.as_ref() == Some(&project))
6520 }
6521
6522 /// Every issue of this board GitHub's issue search reports updated at or after `since`,
6523 /// completed with what this run wrote — the candidates a comment-activity read confirms.
6524 ///
6525 /// Scoped by the board and by nothing else: `project:<owner>/<number>` reaches every issue
6526 /// on the board whatever repository, and whatever owner, it lives in, so no repository or
6527 /// owner qualifier is added and none is needed. What makes the `updated:` qualifier
6528 /// sufficient is a fact about GitHub rather than about this source: a comment written on an
6529 /// issue **and a comment edited on it** both move that issue's `updatedAt`. The credentialed
6530 /// journey `an_edited_comment_moves_its_issue_and_is_selected_since` in `tests/journey`
6531 /// re-takes that fact on every run of the lane, so a change on GitHub's side fails there
6532 /// rather than silently narrowing a caller's answer.
6533 ///
6534 /// The instant is written to the second, rounded down, which can only widen what the
6535 /// search returns; confirmation against each candidate's own comments is what makes the
6536 /// answer exact. The search is an index that lags a write by a second or two — the module
6537 /// documentation records it — so a caller that asks again from its last instant should
6538 /// overlap the two by more than that.
6539 async fn updated_since(&self, since: DateTime<Utc>) -> Result<Vec<Resolved>, SourceError> {
6540 let found = self.searched(&updated_qualifier(since)).await?;
6541 self.completed_with_written(found, |_| true)
6542 }
6543
6544 /// Every issue of this board GitHub's issue search reports for the board-scoped search
6545 /// narrowed by `also`, in pages of [`SEARCH_PAGE_SIZE`].
6546 ///
6547 /// Uncompleted: what this process wrote is added by the caller, which knows whether its
6548 /// own record is the fresher of the two.
6549 async fn searched(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
6550 let search = self.board_search(Some(also));
6551 let mut after: Option<String> = None;
6552 let mut found = Vec::new();
6553 loop {
6554 let (page, next) = self
6555 .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
6556 .await?;
6557 found.extend(page);
6558 match next {
6559 Some(next) => after = Some(next),
6560 None => return Ok(found),
6561 }
6562 }
6563 }
6564
6565 /// A bounded task answer; the versioned cursor carries the connection position, how
6566 /// many rows of the page starting there were already handed out, and the own-write ids
6567 /// already observed, including across a new source instance.
6568 ///
6569 /// Every page is sent at [`SEARCH_PAGE_SIZE`] whatever the caller's limit, and a limit is
6570 /// sliced from the pages it needs; why is the module documentation's paging contract.
6571 async fn search_tasks(
6572 &self,
6573 query: &TaskQuery,
6574 page: &PageRequest,
6575 also: &str,
6576 ) -> Result<Page<Task>, SourceError> {
6577 let mut position = match &page.cursor {
6578 None => SearchPosition::default(),
6579 Some(cursor) => serde_json::from_str::<SearchPosition>(&cursor.0)
6580 .ok()
6581 .filter(|position| {
6582 position.version == SEARCH_CURSOR_VERSION
6583 && position.connection.valid_resume(position.offset)
6584 })
6585 .ok_or_else(|| SourceError::Config {
6586 message: "page cursor is invalid".into(),
6587 })?,
6588 };
6589 let search = self.board_search(Some(also));
6590 let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
6591 let own = self.with_own_writes(Vec::new())?;
6592 // An issue this process commented on is a candidate of a comment-activity read
6593 // whether or not the search has caught up with the comment; see `Self::commented`.
6594 let commented = match query.commented_since {
6595 Some(_) => self.commented()?.clone(),
6596 None => Vec::new(),
6597 };
6598 for id in own.iter().map(|item| &item.id).chain(&commented) {
6599 if !position.own.contains(id) {
6600 position.own.push(id.clone());
6601 }
6602 }
6603 let mut tasks = Vec::new();
6604 while !position.connection.exhausted() && tasks.len() < limit {
6605 let first = SEARCH_PAGE_SIZE;
6606 // Page size is part of the key: a short cached answer cannot answer a wider ask.
6607 let key =
6608 serde_json::to_string(&("page", &search, &position.connection.after(), first))
6609 .expect("search page key is serializable");
6610 let cached = if query.commented_since.is_none() {
6611 self.narrowed_cache()?.get(&key).cloned()
6612 } else {
6613 None
6614 };
6615 let (found, next) = match cached {
6616 Some(found) => {
6617 let next = self
6618 .search_next
6619 .lock()
6620 .map_err(|_| SourceError::Unavailable {
6621 message:
6622 "search pagination was left inconsistent; run the command again"
6623 .into(),
6624 })?
6625 .get(&key)
6626 .cloned()
6627 .flatten();
6628 (found, next)
6629 }
6630 None => {
6631 let (found, next) = self
6632 .search_page(&search, first, position.connection.after())
6633 .await?;
6634 if query.commented_since.is_none() {
6635 self.search_next
6636 .lock()
6637 .map_err(|_| SourceError::Unavailable {
6638 message:
6639 "search pagination was left inconsistent; run the command again"
6640 .into(),
6641 })?
6642 .insert(key.clone(), next.clone());
6643 self.narrowed_cache()?.insert(key, found.clone());
6644 }
6645 (found, next)
6646 }
6647 };
6648 let rows = found.len();
6649 for mut item in found.into_iter().skip(position.offset) {
6650 if tasks.len() == limit {
6651 break;
6652 }
6653 position.offset += 1;
6654 if position.own.contains(&item.id) {
6655 if position.seen.contains(&item.id) {
6656 continue;
6657 }
6658 position.seen.push(item.id.clone());
6659 // The search's own copy of an issue this process only commented on is as
6660 // good as a node read of it, since its comments are read either way.
6661 let only_commented = commented.contains(&item.id)
6662 && !own.iter().any(|written| written.id == item.id);
6663 if !only_commented {
6664 let updated_at = item.updated_at;
6665 let Some(written) = self.search_written(&own, &item.id).await? else {
6666 continue;
6667 };
6668 item = written;
6669 item.updated_at = item.updated_at.max(updated_at);
6670 self.resolved_cache()?.insert(item.id.clone(), item.clone());
6671 }
6672 }
6673 if item.kind == BoardKind::Work(ItemKind::Task) {
6674 let task = item.task()?;
6675 if task_matches(&task, query, &query.project)
6676 && self.commented_since(&item, query.commented_since).await?
6677 {
6678 tasks.push(task);
6679 }
6680 }
6681 }
6682 if position.offset < rows {
6683 continue;
6684 }
6685 position.offset = 0;
6686 position.connection = match next {
6687 Some(after) => SearchConnection::Continuing {
6688 after: Cursor(after),
6689 },
6690 None => SearchConnection::Exhausted {},
6691 };
6692 }
6693 if position.connection.exhausted() {
6694 for id in position.own.clone() {
6695 if position.seen.contains(&id) {
6696 continue;
6697 }
6698 if tasks.len() == limit {
6699 break;
6700 }
6701 position.seen.push(id.clone());
6702 let Some(item) = self.search_written(&own, &id).await? else {
6703 continue;
6704 };
6705 if item.kind == BoardKind::Work(ItemKind::Task) {
6706 let task = item.task()?;
6707 if task_matches(&task, query, &query.project)
6708 && self.commented_since(&item, query.commented_since).await?
6709 {
6710 tasks.push(task);
6711 }
6712 }
6713 }
6714 }
6715 let more = !position.connection.exhausted()
6716 || position.own.iter().any(|id| !position.seen.contains(id));
6717 Ok(Page {
6718 items: tasks,
6719 next: more.then(|| {
6720 Cursor(serde_json::to_string(&position).expect("search position is serializable"))
6721 }),
6722 })
6723 }
6724
6725 /// A resumed process has the ids but no write records; resolve only a record the
6726 /// current page needs, by its uncached node read rather than the lagging search index.
6727 async fn search_written(
6728 &self,
6729 own: &[Resolved],
6730 id: &NativeId,
6731 ) -> Result<Option<Resolved>, SourceError> {
6732 match own.iter().find(|item| item.id == *id) {
6733 Some(item) => Ok(Some(item.clone())),
6734 None => self.item_by_id(id).await,
6735 }
6736 }
6737
6738 /// The candidates for a task query carrying a text, metadata or origin predicate, read
6739 /// without enumerating the board — or `None` for a query carrying none of the three, which
6740 /// keeps the reads it always had.
6741 ///
6742 /// An origin is answered by [`Self::origin_candidates`], whatever else the query carries,
6743 /// because it names at most a handful of items. Text and metadata are answered by one
6744 /// board-scoped issue search carrying every term — see [`narrowing_qualifiers`] — narrowed
6745 /// further by `updated:>=` when the query also asks for comment activity, since both
6746 /// qualifiers must hold of an issue the answer keeps. Every candidate is confirmed in
6747 /// process afterwards by the same predicates [`task_matches`] applies to every read.
6748 ///
6749 /// Completed with what this process wrote, its own record winning over the index's copy
6750 /// of the same item: see [`Self::with_own_writes`].
6751 async fn narrowed(&self, query: &TaskQuery) -> Result<Option<Vec<Resolved>>, SourceError> {
6752 self.send_pending_origins(None).await?;
6753 let asked = match (&query.origin, narrowing_qualifiers(query)) {
6754 (Some(origin), _) => Narrowing::Origin(origin.clone()),
6755 (None, Some(qualifiers)) => Narrowing::Search(match query.commented_since {
6756 Some(since) => format!("{} {qualifiers}", updated_qualifier(since)),
6757 None => qualifiers,
6758 }),
6759 (None, None) => return Ok(None),
6760 };
6761 // A question about comment activity is asked afresh every time, as it always was: it
6762 // is the one a caller polls from one source while waiting for the index, and an
6763 // answer held from the first poll would be the answer to every later one.
6764 let key = query.commented_since.is_none().then(|| asked.key());
6765 let cached = match &key {
6766 Some(key) => self.narrowed_cache()?.get(key).cloned(),
6767 None => None,
6768 };
6769 let found = match cached {
6770 Some(found) => found,
6771 None => {
6772 let found = match &asked {
6773 Narrowing::Origin(origin) => self.origin_candidates(origin).await?,
6774 Narrowing::Search(also) => self.searched(also).await?,
6775 };
6776 if let Some(key) = key {
6777 self.narrowed_cache()?.insert(key, found.clone());
6778 }
6779 found
6780 }
6781 };
6782 self.with_own_writes(found).map(Some)
6783 }
6784
6785 /// The candidates for a project or unscoped document query carrying a searchable text,
6786 /// read without enumerating the board — or `None` for a query with no text or a blank one,
6787 /// which keeps the read it always had.
6788 ///
6789 /// The text is sent as the very phrase a task query's text is — see [`text_qualifiers`] —
6790 /// in one board-scoped issue search walked to its end at [`SEARCH_PAGE_SIZE`], so what it
6791 /// costs is the issues that match and never the board. Its answer is held for the command
6792 /// under the same key [`Self::narrowed`] holds that search under, so a walk of the caller's
6793 /// pages asks GitHub once. Every candidate is confirmed afterwards by its kind and by the
6794 /// substring rule, exactly as an item of the wider read was, and is completed with what this
6795 /// process wrote: see [`Self::with_own_writes`].
6796 async fn text_searched(
6797 &self,
6798 text: Option<&TextQuery>,
6799 ) -> Result<Option<Vec<Resolved>>, SourceError> {
6800 let Some(also) = text_qualifiers(text) else {
6801 return Ok(None);
6802 };
6803 let key = Narrowing::Search(also.clone()).key();
6804 let cached = self.narrowed_cache()?.get(&key).cloned();
6805 let found = match cached {
6806 Some(found) => found,
6807 None => {
6808 let found = self.searched(&also).await?;
6809 self.narrowed_cache()?.insert(key, found.clone());
6810 found
6811 }
6812 };
6813 self.with_own_writes(found).map(Some)
6814 }
6815
6816 /// Every `kind` item of this board whose own origin is exactly `origin`: the candidates
6817 /// [`Self::narrowed`] reads for a task query carrying that origin — one
6818 /// [`graphql::ORIGIN_LOOKUP`] walk, held for the command under the same key, completed with
6819 /// what this process wrote — each confirmed against its own origin entry.
6820 ///
6821 /// What answers a project or document listing by origin, so finding every copy of one plan
6822 /// costs the items that carry its origin and never a page of the board.
6823 async fn copied_from(
6824 &self,
6825 origin: &str,
6826 kind: BoardKind,
6827 ) -> Result<Vec<Resolved>, SourceError> {
6828 let wanted = TaskQuery {
6829 origin: Some(origin.to_owned()),
6830 ..TaskQuery::default()
6831 };
6832 Ok(self
6833 .narrowed(&wanted)
6834 .await?
6835 .unwrap_or_default()
6836 .into_iter()
6837 .filter(|item| item.kind == kind && wanted.origin_matches(&item.metadata()))
6838 .collect())
6839 }
6840
6841 /// Every item of this board that may carry `origin` — a superset of those that do — found
6842 /// by [`graphql::ORIGIN_LOOKUP`] and never by enumerating the board.
6843 ///
6844 /// The union of the board's own field filter over the `onetaskgraph.origin` text field —
6845 /// which reads the field every carrier holds, whichever release wrote it — and the
6846 /// board-scoped issue search for the same id as a phrase in the body, where this source
6847 /// mirrors it. The caller adds the third read, this process's own writes. Candidates are
6848 /// returned unconfirmed; [`task_matches`] compares each one's own origin field with the
6849 /// query's, exactly.
6850 ///
6851 /// Both connections are walked to exhaustion, each from its own cursor. One that has
6852 /// already ended is sent its last cursor again, which answers an empty page, so the one
6853 /// document serves every page of either. What the two leave is stated in the module
6854 /// documentation: a carrier another process added within the last second or two, before
6855 /// either index has it.
6856 async fn origin_candidates(&self, origin: &str) -> Result<Vec<Resolved>, SourceError> {
6857 let filter = format!("{ORIGIN_FIELD}:{}", quoted(origin));
6858 let search = self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))));
6859 let mut items_after: Option<String> = None;
6860 let mut search_after: Option<String> = None;
6861 let mut found: Vec<Resolved> = Vec::new();
6862 let keep = |resolved: Resolved, found: &mut Vec<Resolved>| {
6863 if !found.iter().any(|held| held.id == resolved.id) {
6864 found.push(resolved);
6865 }
6866 };
6867 loop {
6868 let data = self
6869 .graphql(
6870 graphql::ORIGIN_LOOKUP,
6871 json!({"owner":self.owner,"number":self.project_number,"filter":filter,
6872 "search":search,"type":"ISSUE","originFirst":ORIGIN_PAGE_SIZE,
6873 "itemsAfter":items_after,"searchAfter":search_after,
6874 "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
6875 "duplicates":true}),
6876 )
6877 .await?;
6878 let items = data
6879 .pointer("/originItems/projectV2/items")
6880 .filter(|value| !value.is_null())
6881 .ok_or_else(|| SourceError::Refused {
6882 message: format!(
6883 "GitHub project {}/{} was not found or is not visible to the token",
6884 self.owner, self.project_number
6885 ),
6886 })?;
6887 for item in optional_nodes(Some(items), "project items")?
6888 .into_iter()
6889 .flatten()
6890 {
6891 // The board's own items list its drafts too, and a draft is not an issue: no
6892 // narrowed read answers with one, whatever its origin field holds.
6893 if let Some(resolved) = self.resolve(item)?
6894 && resolved.content_kind == ContentKind::Issue
6895 {
6896 keep(resolved, &mut found);
6897 }
6898 }
6899 let searched = data.get("search").ok_or_else(|| SourceError::Malformed {
6900 message: "GitHub search response has no search connection".into(),
6901 })?;
6902 for node in optional_nodes(Some(searched), "search")?
6903 .into_iter()
6904 .flatten()
6905 {
6906 if let Some(resolved) = self.resolve_issue(node).await? {
6907 keep(resolved, &mut found);
6908 }
6909 }
6910 let items_next = resumed(items, items_after.as_deref())?;
6911 let search_next = resumed(searched, search_after.as_deref())?;
6912 if !items_next.has_more() && !search_next.has_more() {
6913 return Ok(found);
6914 }
6915 items_after = items_next.cursor();
6916 search_after = search_next.cursor();
6917 }
6918 }
6919
6920 /// `found`, with every item this process created or wrote in its place, and every one of
6921 /// them the read did not report added.
6922 ///
6923 /// This process's own record wins over the read's copy of the same item, because a read
6924 /// of an item written moments ago can still be behind what was written onto it — the
6925 /// origin field included, which is the one a narrowed read is confirmed against — and a
6926 /// read that still names an item under a predicate this process's write moved it out of
6927 /// must not return it. The one thing the read knows that the record cannot is when GitHub
6928 /// last saw the item change, which is what a comment-activity read rules a candidate out
6929 /// by, so the read's `updatedAt` is kept when the record has none of its own. See
6930 /// [`Self::created`] and [`Self::updated`](GitHubProjectsSource::updated).
6931 fn with_own_writes(&self, mut found: Vec<Resolved>) -> Result<Vec<Resolved>, SourceError> {
6932 // A board draft is not an issue, so no narrowed read returns one, and this process
6933 // having written one does not make it an answer either.
6934 let own: Vec<Resolved> = self
6935 .created()?
6936 .iter()
6937 .chain(self.updated()?.iter())
6938 .filter(|own| own.content_kind == ContentKind::Issue)
6939 .cloned()
6940 .collect();
6941 for mut own in own {
6942 self.resolved_cache()?.insert(own.id.clone(), own.clone());
6943 match found.iter_mut().find(|read| read.id == own.id) {
6944 Some(read) => {
6945 own.updated_at = own.updated_at.max(read.updated_at);
6946 *read = own;
6947 }
6948 None => found.push(own),
6949 }
6950 }
6951 Ok(found)
6952 }
6953
6954 /// Whether `item` has a comment created or last edited at or after `since` — always, when
6955 /// there is no instant to hold it to.
6956 ///
6957 /// The candidate's own `updatedAt` is read first, because a comment written or edited at
6958 /// or after the instant moved it there: an issue not updated since holds no such comment,
6959 /// and its comments are never asked for — unless this process commented on it in this
6960 /// command, when the `updatedAt` held may predate that comment; see [`Self::commented`]. Otherwise its comments are walked, oldest first,
6961 /// only as far as the first that matches. A board draft is not an issue and has no
6962 /// comments, so it never matches.
6963 async fn commented_since(
6964 &self,
6965 item: &Resolved,
6966 since: Option<DateTime<Utc>>,
6967 ) -> Result<bool, SourceError> {
6968 let Some(since) = since else {
6969 return Ok(true);
6970 };
6971 if item.content_kind == ContentKind::DraftIssue {
6972 return Ok(false);
6973 }
6974 // An `updatedAt` this process's own record or a lagging index holds can predate a
6975 // comment this process wrote since, so only an issue it did not comment on is ruled
6976 // out by one.
6977 if item.updated_at.is_some_and(|updated| updated < since)
6978 && !self.commented()?.contains(&item.id)
6979 {
6980 return Ok(false);
6981 }
6982 let query = TaskQuery {
6983 commented_since: Some(since),
6984 ..TaskQuery::default()
6985 };
6986 let mut after: Option<String> = None;
6987 loop {
6988 let data = self
6989 .graphql(
6990 graphql::ISSUE_COMMENTS,
6991 json!({"id":item.id.0,"first":MAX_PAGE_SIZE,"after":after}),
6992 )
6993 .await?;
6994 let Some(connection) = data
6995 .get("node")
6996 .filter(|value| !value.is_null())
6997 .and_then(|node| node.get("comments"))
6998 .filter(|value| !value.is_null())
6999 else {
7000 // Removed since the search reported it: no longer an issue with comments.
7001 return Ok(false);
7002 };
7003 let comments = optional_nodes(Some(connection), "issue comments")?
7004 .into_iter()
7005 .flatten()
7006 .map(comment_from)
7007 .collect::<Result<Vec<_>, _>>()?;
7008 if query.comments_match(&comments) {
7009 return Ok(true);
7010 }
7011 match next_cursor(connection)? {
7012 Some(next) => {
7013 validate_cursor_progress(after.as_deref(), &next.0)?;
7014 after = Some(next.0);
7015 }
7016 None => return Ok(false),
7017 }
7018 }
7019 }
7020
7021 /// Every item on the board: the union of both enumerations GitHub offers of one.
7022 ///
7023 /// Neither contains the other, so neither is dropped — only `ProjectV2.items` lists a
7024 /// board **draft** and reads the board's own fields beside its items, and only the search
7025 /// reports an item that connection is behind on. The module documentation is where the lag and the
7026 /// measurements behind it are written down.
7027 ///
7028 /// A search result is admitted on the same terms as any other issue this source reaches
7029 /// directly — [`Self::resolve_issue`] keeps it only if that issue's own `projectItems`
7030 /// names *this* board — so an issue the index still believes is here after it was taken
7031 /// off is refused rather than reported.
7032 ///
7033 /// See [`Self::board_cache`]. Both completions happen on every call rather than once,
7034 /// which is what the cache could otherwise have broken.
7035 async fn board(&self) -> Result<Board, SourceError> {
7036 self.board_completed_by(None).await
7037 }
7038
7039 /// [`Self::board`], its search half narrowed by `also` when it is given — which an issue
7040 /// that half could add is then answered only if it holds.
7041 async fn board_completed_by(&self, also: Option<&str>) -> Result<Board, SourceError> {
7042 let cached = self.board_cache()?.clone();
7043 let mut board = match cached {
7044 Some(board) => board,
7045 None => {
7046 let read = self.read_board().await?;
7047 *self.board_cache()? = Some(read.clone());
7048 read
7049 }
7050 };
7051 let searched = match also {
7052 None => self.searched_issues().await?,
7053 Some(also) => self.searched_narrowly(also).await?,
7054 };
7055 for held in searched {
7056 if !board.items.iter().any(|item| item.id == held.id) {
7057 board.items.push(held);
7058 }
7059 }
7060 for own in self.created()?.iter() {
7061 if !board.items.iter().any(|item| item.id == own.id) {
7062 board.items.push(own.clone());
7063 }
7064 }
7065 Ok(board)
7066 }
7067
7068 /// This process's own view of the board, or the refusal a poisoned lock is.
7069 fn board_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<Board>>, SourceError> {
7070 self.board_cache
7071 .lock()
7072 .map_err(|_| SourceError::Unavailable {
7073 message: "this source's view of the board was left inconsistent by an earlier \
7074 failure; next: run the command again"
7075 .into(),
7076 })
7077 }
7078
7079 /// Bring this process's own view of the board up to an item it has just written.
7080 ///
7081 /// A created item goes to `created`, which is what completes a board read GitHub's own
7082 /// eventual consistency has left behind. An item that was already there is replaced
7083 /// where it sits, so a second write of it in the same command reads its real parent
7084 /// rather than the one it had before the first write.
7085 ///
7086 /// "Where it sits" is three places, and missing an earlier one leaves a stale record
7087 /// that wins: an item this same run created is held in `created` and not in the cached
7088 /// board, and `board` completes the cached board *from* `created`, so replacing only
7089 /// the cached copy of such an item replaces nothing and the read still reports the
7090 /// title it was created with. The search is the third, and it is the one an item the
7091 /// board's own projection is behind on sits in *alone* — which is exactly the item this
7092 /// source is least able to re-read, so leaving it out would put the stale title back on
7093 /// the only items the completion in [`Self::board`] exists for.
7094 fn remember_written(&self, item: Resolved, created: bool) -> Result<(), SourceError> {
7095 self.resolved_cache()?.insert(item.id.clone(), item.clone());
7096 if created {
7097 self.created()?.push(item);
7098 return Ok(());
7099 }
7100 {
7101 let mut own = self.created()?;
7102 if let Some(held) = own.iter_mut().find(|held| held.id == item.id) {
7103 *held = item;
7104 return Ok(());
7105 }
7106 }
7107 {
7108 let mut own = self.updated()?;
7109 match own.iter_mut().find(|held| held.id == item.id) {
7110 Some(held) => *held = item.clone(),
7111 None => own.push(item.clone()),
7112 }
7113 }
7114 if let Some(board) = self.board_cache()?.as_mut()
7115 && let Some(held) = board.items.iter_mut().find(|held| held.id == item.id)
7116 {
7117 *held = item.clone();
7118 }
7119 if let Some(found) = self.search_cache()?.as_mut()
7120 && let Some(held) = found.iter_mut().find(|held| held.id == item.id)
7121 {
7122 *held = item.clone();
7123 }
7124 for found in self.narrowed_cache()?.values_mut() {
7125 if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
7126 *held = item.clone();
7127 }
7128 }
7129 for (_, found) in self.children_cache()?.values_mut() {
7130 if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
7131 *held = item.clone();
7132 }
7133 }
7134 Ok(())
7135 }
7136
7137 /// Forget one item this process has just deleted, from every half of its own view.
7138 fn forget(&self, id: &NativeId) -> Result<(), SourceError> {
7139 if let Some(Deferred::Held { created, .. }) = self.deferred()?.as_mut() {
7140 created.retain(|held| held.content != *id);
7141 }
7142 self.resolved_cache()?.remove(id);
7143 self.read_ahead()?.remove(id);
7144 self.carried()?.remove(id);
7145 self.created()?.retain(|own| own.id != *id);
7146 self.updated()?.retain(|own| own.id != *id);
7147 self.commented()?.retain(|own| own != id);
7148 if let Some(board) = self.board_cache()?.as_mut() {
7149 board.items.retain(|item| item.id != *id);
7150 }
7151 if let Some(found) = self.search_cache()?.as_mut() {
7152 found.retain(|item| item.id != *id);
7153 }
7154 for found in self.narrowed_cache()?.values_mut() {
7155 found.retain(|item| item.id != *id);
7156 }
7157 for (_, found) in self.children_cache()?.values_mut() {
7158 found.retain(|item| item.id != *id);
7159 }
7160 Ok(())
7161 }
7162
7163 /// This process's own record of each narrowed answer, or the refusal a poisoned lock is.
7164 fn narrowed_cache(
7165 &self,
7166 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<String, Vec<Resolved>>>, SourceError> {
7167 self.narrowed_cache
7168 .lock()
7169 .map_err(|_| SourceError::Unavailable {
7170 message: "this source's view of a narrowed read was left inconsistent by an \
7171 earlier failure; next: run the command again"
7172 .into(),
7173 })
7174 }
7175
7176 /// This process's own record of each project's sub-issues, or the refusal a poisoned lock
7177 /// is.
7178 /// The tasks `project` holds, as a copy's batched read found them this command, when it did.
7179 fn held_task_children(&self, project: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
7180 Ok(self.task_children()?.get(project).cloned())
7181 }
7182
7183 fn task_children(
7184 &self,
7185 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Vec<Resolved>>>, SourceError> {
7186 self.task_children
7187 .lock()
7188 .map_err(|_| SourceError::Unavailable {
7189 message: "this source's record of a project's tasks was left inconsistent by an \
7190 earlier failure; next: run the command again"
7191 .into(),
7192 })
7193 }
7194
7195 fn children_cache(&self) -> Result<std::sync::MutexGuard<'_, ProjectChildren>, SourceError> {
7196 self.children_cache
7197 .lock()
7198 .map_err(|_| SourceError::Unavailable {
7199 message: "this source's view of a project's tasks was left inconsistent by an \
7200 earlier failure; next: run the command again"
7201 .into(),
7202 })
7203 }
7204
7205 /// Every page of the board, read from GitHub.
7206 async fn read_board(&self) -> Result<Board, SourceError> {
7207 let mut after: Option<String> = None;
7208 let mut items = Vec::new();
7209 let mut board;
7210 loop {
7211 let page = self.board_page(after.as_deref(), MAX_PAGE_SIZE).await?;
7212 for item in page
7213 .pointer("/items/nodes")
7214 .and_then(Value::as_array)
7215 .ok_or_else(|| SourceError::Malformed {
7216 message: "GitHub project items.nodes is not an array".into(),
7217 })?
7218 {
7219 if let Some(resolved) = self.resolve(item)? {
7220 items.push(resolved);
7221 }
7222 }
7223 let info = page
7224 .pointer("/items/pageInfo")
7225 .ok_or_else(|| SourceError::Malformed {
7226 message: "GitHub project items have no pageInfo".into(),
7227 })?;
7228 let has_next = required_bool(info, "hasNextPage")?;
7229 let next = has_next
7230 .then(|| required_str(info, "endCursor"))
7231 .transpose()?;
7232 board = page.clone();
7233 match next {
7234 Some(next) => {
7235 validate_cursor_progress(after.as_deref(), next)?;
7236 after = Some(next.to_owned());
7237 }
7238 None => break,
7239 }
7240 }
7241 Ok(Board {
7242 id: required_str(&board, "id")?.to_owned(),
7243 fields: board.get("fields").cloned().unwrap_or(Value::Null),
7244 items,
7245 })
7246 }
7247
7248 /// The existing items this source has written, for completing a narrowed read that is
7249 /// behind; see [`Self::updated`](GitHubProjectsSource::updated).
7250 fn updated(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
7251 self.updated.lock().map_err(|_| SourceError::Unavailable {
7252 message: "this source's record of what it wrote in this run was left inconsistent \
7253 by an earlier failure; next: run the command again"
7254 .into(),
7255 })
7256 }
7257
7258 /// The issues this source has commented on in this command; see
7259 /// [`Self::commented`](GitHubProjectsSource::commented).
7260 fn commented(&self) -> Result<std::sync::MutexGuard<'_, Vec<NativeId>>, SourceError> {
7261 self.commented.lock().map_err(|_| SourceError::Unavailable {
7262 message: "this source's record of what it commented on in this run was left \
7263 inconsistent by an earlier failure; next: run the command again"
7264 .into(),
7265 })
7266 }
7267
7268 /// Called only once GitHub has answered the comment write, so an issue whose comment
7269 /// failed is never made a candidate a later read would pay a node read for.
7270 fn remember_commented(&self, issue: &NativeId) -> Result<(), SourceError> {
7271 let mut commented = self.commented()?;
7272 if !commented.contains(issue) {
7273 commented.push(issue.clone());
7274 }
7275 Ok(())
7276 }
7277
7278 /// The items this source has created, for completing a board read that is behind.
7279 fn created(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
7280 self.created.lock().map_err(|_| SourceError::Unavailable {
7281 message: "this source's record of what it created in this run was left \
7282 inconsistent by an earlier failure; next: run the command again"
7283 .into(),
7284 })
7285 }
7286
7287 /// One board item as this source reports it, or `None` for content it ignores.
7288 ///
7289 /// A pull request is neither a project nor a task — it is somebody's change, not a
7290 /// unit of plan — and an item whose content the token cannot see has nothing to
7291 /// report at all.
7292 fn resolve(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
7293 let resolved = self.resolve_unremembered(item)?;
7294 if let Some(resolved) = &resolved {
7295 self.resolved_cache()?
7296 .insert(resolved.id.clone(), resolved.clone());
7297 }
7298 Ok(resolved)
7299 }
7300
7301 /// [`Self::resolve`], without remembering the item it resolves: what reading a token off a
7302 /// write's own response needs, which says nothing this process should answer a later read
7303 /// with.
7304 fn resolve_unremembered(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
7305 let content = item.get("content").ok_or_else(|| SourceError::Malformed {
7306 message: "GitHub project item is missing content".into(),
7307 })?;
7308 if content.is_null() {
7309 return Ok(None);
7310 }
7311 let content_kind = match required_str(content, "__typename")? {
7312 "Issue" => ContentKind::Issue,
7313 "DraftIssue" => ContentKind::DraftIssue,
7314 _ => return Ok(None),
7315 };
7316 let field_values = item
7317 .get("fieldValues")
7318 .ok_or_else(|| SourceError::Malformed {
7319 message: "GitHub project item is missing fieldValues".into(),
7320 })?;
7321 complete_connection(field_values, "project item field values", NESTED_PAGE_SIZE)?;
7322 let nodes = field_values
7323 .get("nodes")
7324 .and_then(Value::as_array)
7325 .ok_or_else(|| SourceError::Malformed {
7326 message: "GitHub project item fieldValues.nodes is not an array".into(),
7327 })?;
7328 if let Some(labels) = content.get("labels") {
7329 complete_connection(labels, "content labels", NESTED_PAGE_SIZE)?;
7330 }
7331 let raw_body = optional_str(content, "body")?.map(str::to_owned);
7332 let (body, slot) = metadata_body(raw_body.clone())?;
7333 let parent = optional_str(content.get("parent").unwrap_or(&Value::Null), "id")?
7334 .map(|id| NativeId(id.to_owned()));
7335 // A draft has no sub-issues to summarise, and GitHub's schema gives it no field
7336 // to read one from; it is a task, and never a project.
7337 let sub_issues = match content_kind {
7338 ContentKind::Issue => sub_issue_total(content)?,
7339 ContentKind::DraftIssue => 0,
7340 };
7341 let content_id = required_str(content, "id")?;
7342 let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
7343 message: format!("GitHub issue {content_id}: {message}"),
7344 })?;
7345 let raw_title = required_str(content, "title")?;
7346 let kind = BoardKind::of(raw_title, parent.is_some(), sub_issues, marked);
7347 // The title a person wrote, which for a document is the one without the prefix —
7348 // the same way `content` above is the body without this source's metadata slot.
7349 let title = match kind {
7350 BoardKind::Document => raw_title[DESIGN_TITLE_PREFIX.len()..].to_owned(),
7351 BoardKind::Work(_) => raw_title.to_owned(),
7352 };
7353 let own_repository = content
7354 .pointer("/repository/nameWithOwner")
7355 .and_then(Value::as_str)
7356 .map(|origin| Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST)))
7357 .transpose()
7358 .map_err(|message| SourceError::Malformed { message })?;
7359 let repositories = if slot.contains_key(Repository::METADATA_KEY) {
7360 Repository::from_metadata(&slot)
7361 .map_err(|message| SourceError::Malformed { message })?
7362 } else {
7363 own_repository.clone().into_iter().collect()
7364 };
7365 let classification = Classification::from_metadata(&slot)
7366 .map_err(|message| SourceError::Malformed { message })?;
7367 let id = NativeId(content_id.to_owned());
7368 // Read only for a task, because only a task has either list: a project or a
7369 // document holding one of these keys holds nothing this source reports, and the
7370 // keys are left out of its caller-visible metadata all the same.
7371 let (delivers, delivered_by) = if kind == BoardKind::Work(ItemKind::Task) {
7372 let listed = |key: &str| {
7373 TaskRef::from_value(key, &id, Some(&self.name), slot.get(key))
7374 .map_err(|message| SourceError::Malformed { message })
7375 };
7376 (
7377 listed(TaskRef::DELIVERS_KEY)?,
7378 listed(TaskRef::DELIVERED_BY_KEY)?,
7379 )
7380 } else {
7381 (Vec::new(), Vec::new())
7382 };
7383 let (option, closed, reason) = Self::status_parts(nodes, content)?;
7384 let priority = self.held_priority(nodes)?;
7385 let item_labels = labels(content)?;
7386 // Present when the item was reached through its own issue, whose board entry
7387 // names the board; a read of the board's own items has the board already. An
7388 // empty id names nothing a field write could address, so it is read as absent and
7389 // the write goes back to reading the board.
7390 let board_id = item
7391 .pointer("/project/id")
7392 .and_then(Value::as_str)
7393 .filter(|id| !id.is_empty());
7394 let resolved = Resolved {
7395 item_id: required_str(item, "id")?.to_owned(),
7396 id,
7397 content_kind,
7398 kind,
7399 title,
7400 body: body.filter(|value| !value.is_empty()),
7401 raw_body,
7402 status: self
7403 .statuses
7404 .status(kind.status_kind(), option, closed, reason),
7405 option: option.map(str::to_owned),
7406 priority,
7407 closed,
7408 reason: closed.then(|| reason.map(str::to_owned)).flatten(),
7409 delivers,
7410 delivered_by,
7411 labels: item_labels.clone(),
7412 parent,
7413 origin: text_field(nodes, ORIGIN_FIELD)?.filter(|value| !value.is_empty()),
7414 projected: self.held_projections(nodes)?,
7415 number: match content_kind {
7416 ContentKind::Issue => Some(issue_number(content)?),
7417 // A draft is filed in no repository, so nothing ever numbered it:
7418 // `DraftIssue` declares no `number` at all, exactly as it declares no
7419 // `subIssuesSummary` the branch above reads.
7420 ContentKind::DraftIssue => None,
7421 },
7422 url: optional_str(content, "url")?.map(str::to_owned),
7423 created_at: optional_time(content, "createdAt")?,
7424 updated_at: optional_time(content, "updatedAt")?,
7425 item_updated_at: optional_time(item, "updatedAt")?,
7426 comment_count: content
7427 .pointer("/commentCount/totalCount")
7428 .and_then(Value::as_u64),
7429 relationship_events: content
7430 .pointer("/relationshipEvents/totalCount")
7431 .and_then(Value::as_u64),
7432 // The issue node is stamped wherever it was read; a board item a read of the issue
7433 // put together from its memberships is not.
7434 served_at: match optional_time(content, SERVED_AT)? {
7435 Some(served) => Some(served),
7436 None => optional_time(item, SERVED_AT)?,
7437 },
7438 // Both connections were read to their end above — one reporting a further page is
7439 // refused — so these are every field value and every label the item holds. A
7440 // response that did not select the labels at all says nothing of them, and gives
7441 // no facets.
7442 facets: content.get("labels").is_some().then(|| {
7443 change_token::Facets::new(kind.facet(), facet_fields(nodes), &item_labels).digest()
7444 }),
7445 own_repository,
7446 repositories,
7447 classification,
7448 slot,
7449 board_id: board_id.map(str::to_owned),
7450 fields: field_definitions(nodes),
7451 board_fields: Self::carried_board_fields(content, board_id)?,
7452 blocked_by: carried_blocked_by(content)?,
7453 };
7454 Ok(Some(resolved))
7455 }
7456
7457 /// The field definitions of the board `board_id` names — the project this issue's own
7458 /// board item is on — off the `boards` page a read of an issue by its own id carries, or
7459 /// `None` when the read carried none, carried no entry for that board, or the board item
7460 /// named no board, which a write then answers by reading the board's fields itself.
7461 ///
7462 /// Matched by the board's node id and never by its number alone: a project number is
7463 /// unique only within its owner, so another owner's board numbered alike can sit on the
7464 /// same page, and its field and option ids address nothing on this one.
7465 fn carried_board_fields(
7466 content: &Value,
7467 board_id: Option<&str>,
7468 ) -> Result<Option<Value>, SourceError> {
7469 let (Some(nodes), Some(board_id)) = (
7470 content.pointer("/boards/nodes").and_then(Value::as_array),
7471 board_id,
7472 ) else {
7473 return Ok(None);
7474 };
7475 let Some(board) = nodes.iter().find_map(|node| {
7476 let project = node.get("project")?;
7477 (project.get("id").and_then(Value::as_str) == Some(board_id)).then_some(project)
7478 }) else {
7479 return Ok(None);
7480 };
7481 let Some(fields) = board.get("fields").filter(|fields| !fields.is_null()) else {
7482 return Ok(None);
7483 };
7484 complete_connection(fields, "board fields", NESTED_PAGE_SIZE)?;
7485 Ok(Some(fields.clone()))
7486 }
7487
7488 /// What one board item's `Priority` field says, through this instance's mapping.
7489 ///
7490 /// An instance with no mapping holds no priority, so every item reads as `none` whatever
7491 /// its board holds. With one, no value is `none`, a mapped option is its level, and an
7492 /// option the mapping does not name is kept as itself — never read as a level or as
7493 /// `none` — for a read of the task to report by name.
7494 fn held_priority(&self, field_values: &[Value]) -> Result<HeldPriority, SourceError> {
7495 let Some(mapping) = &self.priorities else {
7496 return Ok(HeldPriority::Read(Priority::None));
7497 };
7498 // A value of the field that names no option — a text field someone called `Priority` —
7499 // is malformed rather than `none`: reading it as no priority would let the next copy
7500 // clear one a person set.
7501 let Some(option) = field_values
7502 .iter()
7503 .find(|value| {
7504 value.pointer("/field/name").and_then(Value::as_str) == Some(PRIORITY_FIELD)
7505 })
7506 .map(|value| required_str(value, "name"))
7507 .transpose()?
7508 else {
7509 return Ok(HeldPriority::Read(Priority::None));
7510 };
7511 Ok(mapping.priority_of(option).map_or_else(
7512 || HeldPriority::Unmapped(option.to_owned()),
7513 HeldPriority::Read,
7514 ))
7515 }
7516
7517 /// What each projected metadata field holds for one board item, by field name — every text
7518 /// value GitHub answers, an empty one included.
7519 fn held_projections(
7520 &self,
7521 field_values: &[Value],
7522 ) -> Result<BTreeMap<String, String>, SourceError> {
7523 let mut held = BTreeMap::new();
7524 for projection in &self.metadata_fields {
7525 if let Some(text) = text_field(field_values, &projection.field)? {
7526 held.insert(projection.field.to_string(), text);
7527 }
7528 }
7529 Ok(held)
7530 }
7531
7532 /// What one board item's status is read from: its `Status` option, whether its issue
7533 /// is closed, and the reason it was closed with. [`BoardStatuses::status`] turns the
7534 /// three into the status it reports.
7535 fn status_parts<'a>(
7536 field_values: &'a [Value],
7537 content: &'a Value,
7538 ) -> Result<(Option<&'a str>, bool, Option<&'a str>), SourceError> {
7539 let option = field_values
7540 .iter()
7541 .find(|value| value.pointer("/field/name").and_then(Value::as_str) == Some("Status"))
7542 .map(|value| required_str(value, "name"))
7543 .transpose()?;
7544 let closed = optional_str(content, "state")? == Some("CLOSED");
7545 Ok((option, closed, optional_str(content, "stateReason")?))
7546 }
7547
7548 /// The board Status option this write selects, or the refusal that says why not.
7549 ///
7550 /// The mapped option is required for both open and terminal targets. A terminal write
7551 /// validates it before changing either representation, so it can never fall back to
7552 /// closing an issue whose board cannot display the matching status.
7553 ///
7554 /// Answers the field's id, the option's id, and the option's name as the board spells
7555 /// it — which is the name a read of the item reports once it sits there.
7556 fn column_for(
7557 &self,
7558 fields: &Value,
7559 kind: ItemKind,
7560 category: StatusCategory,
7561 target: &StatusTarget,
7562 ) -> Result<Option<(String, String, String)>, SourceError> {
7563 let Some(wanted) = target.option() else {
7564 return Ok(None);
7565 };
7566 let missing = |detail: &str| SourceError::Refused {
7567 message: format!(
7568 "{} status {} of source {} needs the board Status option {wanted:?}, and \
7569 {detail}; next: add that option to the board, which `onetaskgraph sources \
7570 fields {} --apply` does, or point status_mapping.{}.{} of this source at one \
7571 it has",
7572 kind.marker(),
7573 category_name(category),
7574 self.name,
7575 self.name,
7576 category_name(category),
7577 kind.marker()
7578 ),
7579 };
7580 let Some(field) = Board::field(fields, "Status")? else {
7581 return Err(missing("this board has no Status field"));
7582 };
7583 if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
7584 return Err(missing(
7585 "this board's Status field is not a single-select field",
7586 ));
7587 }
7588 let option = field
7589 .get("options")
7590 .and_then(Value::as_array)
7591 .and_then(|options| {
7592 options.iter().find(|option| {
7593 option
7594 .get("name")
7595 .and_then(Value::as_str)
7596 .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
7597 })
7598 });
7599 match option {
7600 None => Err(missing("this board does not have it")),
7601 Some(option) => Ok(Some((
7602 required_str(field, "id")?.to_owned(),
7603 required_str(option, "id")?.to_owned(),
7604 required_str(option, "name")?.to_owned(),
7605 ))),
7606 }
7607 }
7608
7609 /// The refusal a status that closes an issue is answered with over a board draft.
7610 fn closes_a_draft(&self, category: StatusCategory) -> SourceError {
7611 SourceError::Refused {
7612 message: format!(
7613 "status {} of source {} closes the item's issue, and GitHub draft items have \
7614 no open or closed state",
7615 category_name(category),
7616 self.name
7617 ),
7618 }
7619 }
7620
7621 /// What a status write to one item needs of the board: the board's id and the
7622 /// definition of its `Status` field, read off the item when the item says both.
7623 ///
7624 /// The same reasoning as [`Self::fields_for`]: a node read of the item names its board,
7625 /// and its `Status` value carries that field's definition, options and all. An item that
7626 /// does not say — no board id, or no `Status` value to read the field off — takes them
7627 /// from [`Self::board_fields`], which reads no item.
7628 async fn status_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
7629 if let Some(board) = item.carried_board() {
7630 return Ok(board);
7631 }
7632 if item.defines("Status")
7633 && let Some(board_id) = item.named_board()
7634 {
7635 return Ok(BoardFields {
7636 id: board_id,
7637 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
7638 });
7639 }
7640 self.board_fields().await
7641 }
7642
7643 /// Set one task's or one project's status and nothing else; see
7644 /// [`TaskSource::set_task_status`] and [`TaskSource::set_project_status`].
7645 async fn set_status(
7646 &self,
7647 id: &NativeId,
7648 kind: ItemKind,
7649 category: StatusCategory,
7650 ) -> Result<Option<Status>, SourceError> {
7651 // Refused before anything is read, in the words a write of the same status is.
7652 let target = self.resolved_target(kind, category)?;
7653 // An item read by issue reference carries only what this write needs, so what it
7654 // becomes is never remembered for a later read to be answered from.
7655 let held = self.take_held(id)?;
7656 let by_reference = held.is_some();
7657 let bound = match held {
7658 Some(item) => Some(item),
7659 None => self.bound_item(id).await?,
7660 };
7661 let Some(mut item) = bound.filter(|item| item.kind == BoardKind::Work(kind)) else {
7662 return Ok(None);
7663 };
7664 let board = self.status_board(&item).await?;
7665 let (field, option, name) = self
7666 .column_for(&board.fields, kind, category, &target)?
7667 .ok_or_else(|| SourceError::Malformed {
7668 message: format!(
7669 "status {} of source {} names no board Status option",
7670 category_name(category),
7671 self.name
7672 ),
7673 })?;
7674 if item.status.category == category && item.option.as_deref() == Some(&name) {
7675 return Ok(Some(item.status));
7676 }
7677 match &target {
7678 StatusTarget::Terminal(_, reason) => {
7679 if item.content_kind == ContentKind::DraftIssue {
7680 return Err(self.closes_a_draft(category));
7681 }
7682 // The option first, so a refused selection leaves an open issue open rather
7683 // than closed behind a card that still reads open.
7684 self.set_item_field(
7685 board.id.as_str(),
7686 &item.item_id,
7687 &field,
7688 json!({"singleSelectOptionId": option}),
7689 )
7690 .await?;
7691 // An issue already closed in a way that reads as this category keeps its state
7692 // and its reason: only its card was out of line, and closing it again would
7693 // overwrite a reason — a duplicate's — that says more than this write does.
7694 let already = item.closed && item.status.category == category;
7695 if !already {
7696 self.update_content(
7697 ContentKind::Issue,
7698 &item.id,
7699 json!({"stateInput": state_input(Some(&target))}),
7700 )
7701 .await?;
7702 }
7703 let reason = if already {
7704 item.reason.clone()
7705 } else {
7706 Some(reason.reason().to_owned())
7707 };
7708 item.closed = true;
7709 item.status = self
7710 .statuses
7711 .status(kind, Some(&name), true, reason.as_deref());
7712 item.option = Some(name);
7713 item.reason = reason;
7714 }
7715 StatusTarget::Column(_) => {
7716 // An option is what an open item's status is, so a closed issue is reopened
7717 // first — sitting closed in the column, it would read back as closed. A draft has
7718 // no state to reopen.
7719 if item.content_kind == ContentKind::Issue && item.closed {
7720 self.update_content(
7721 ContentKind::Issue,
7722 &item.id,
7723 json!({"stateInput": state_input(Some(&target))}),
7724 )
7725 .await?;
7726 item.closed = false;
7727 }
7728 self.set_item_field(
7729 board.id.as_str(),
7730 &item.item_id,
7731 &field,
7732 json!({"singleSelectOptionId": option}),
7733 )
7734 .await?;
7735 item.status = self.statuses.status(kind, Some(&name), false, None);
7736 item.option = Some(name);
7737 }
7738 StatusTarget::Disabled(_) => {
7739 unreachable!("resolved_target refused a disabled status")
7740 }
7741 }
7742 let status = item.status.clone();
7743 if !by_reference {
7744 self.remember_written(item, false)?;
7745 }
7746 Ok(Some(status))
7747 }
7748
7749 /// Replace one task's `delivered_by` and nothing else; see
7750 /// [`TaskSource::set_delivered_by`].
7751 ///
7752 /// One update of the body, which differs from the body GitHub holds only inside the
7753 /// metadata slot — see [`with_slot`]. A body that would not change is not sent at all.
7754 async fn replace_delivered_by(
7755 &self,
7756 id: &NativeId,
7757 delivered_by: &[TaskRef],
7758 ) -> Result<Option<()>, SourceError> {
7759 let entries = TaskRef::listed(
7760 TaskRef::DELIVERED_BY_KEY,
7761 id,
7762 Some(&self.name),
7763 delivered_by.to_vec(),
7764 )
7765 .map_err(|message| SourceError::Refused { message })?;
7766 let Some(mut item) = self
7767 .bound_item(id)
7768 .await?
7769 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
7770 else {
7771 return Ok(None);
7772 };
7773 let mut slot = item.slot.clone();
7774 set_task_list(&mut slot, TaskRef::DELIVERED_BY_KEY, &entries);
7775 self.write_slot(&mut item, &slot).await?;
7776 item.delivered_by = entries;
7777 self.remember_written(item, false)?;
7778 Ok(Some(()))
7779 }
7780
7781 /// Set one caller key of the metadata slot of one issue of `kind`, and nothing else;
7782 /// see [`TaskSource::set_task_metadata`].
7783 ///
7784 /// `None` when this board holds no item by that id, or holds one of another kind. The
7785 /// answer is the item as this source now reads it, so what a caller is told the key
7786 /// holds is what the slot holds.
7787 ///
7788 /// A key already holding the value is answered without a write, compared as JSON rather
7789 /// than as the body's bytes: a slot a person spelled with other whitespace would
7790 /// otherwise be re-encoded, which is a write that changes nothing the caller asked for.
7791 async fn set_slot_key(
7792 &self,
7793 id: &NativeId,
7794 kind: BoardKind,
7795 key: &MetadataKey,
7796 value: &Value,
7797 ) -> Result<Option<Resolved>, SourceError> {
7798 let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
7799 return Ok(None);
7800 };
7801 if item.slot.get(key.as_str()) == Some(value) {
7802 return Ok(Some(item));
7803 }
7804 let mut slot = item.slot.clone();
7805 slot.insert(key.as_str().to_owned(), value.clone());
7806 // A key a board text field projects moves that field first, so the body — the value's
7807 // one authoritative home — is written last, as every write of an existing item is.
7808 if self
7809 .metadata_fields
7810 .iter()
7811 .any(|projection| &*projection.key == key.as_str())
7812 {
7813 let board = self.projection_board(&item).await?;
7814 let (writes, projected) = self.projection_writes(
7815 &board.fields,
7816 Some(&item.projected),
7817 &slot,
7818 Some(&[key.as_str()]),
7819 )?;
7820 let (mut values, mut clears) = (Vec::new(), Vec::new());
7821 for write in &writes {
7822 write.join(&mut values, &mut clears);
7823 }
7824 self.set_item_fields(board.id.as_str(), &item.item_id, &values, &clears)
7825 .await?;
7826 item.projected = projected;
7827 }
7828 self.write_slot(&mut item, &slot).await?;
7829 self.remember_written(item.clone(), false)?;
7830 Ok(Some(item))
7831 }
7832
7833 /// What a projection write to `item` needs of the board, read off the item when it says
7834 /// enough, as [`Self::fields_for`] reads it.
7835 async fn projection_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
7836 if let Some(board) = item.carried_board() {
7837 return Ok(board);
7838 }
7839 if let Some(board_id) = item.named_board()
7840 && self
7841 .metadata_fields
7842 .iter()
7843 .all(|projection| item.defines(&projection.field))
7844 {
7845 return Ok(BoardFields {
7846 id: board_id,
7847 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
7848 });
7849 }
7850 self.board_fields().await
7851 }
7852
7853 /// Put `slot` in one item's metadata slot with a single update of its body, and bring
7854 /// `item` up to what that write left.
7855 ///
7856 /// The body sent differs from the body GitHub holds only inside the slot — see
7857 /// [`with_slot`] — and a body that would not change is not sent at all. It goes through
7858 /// the mutation the item's content takes, so a board draft's body is written with
7859 /// `updateProjectV2DraftIssue` exactly as an issue's is with `updateIssue`.
7860 async fn write_slot(
7861 &self,
7862 item: &mut Resolved,
7863 slot: &BTreeMap<String, Value>,
7864 ) -> Result<(), SourceError> {
7865 let held = item.raw_body.clone().unwrap_or_default();
7866 let body = with_slot(&held, slot)?;
7867 if body != held {
7868 self.update_content(item.content_kind, &item.id, json!({"body": body}))
7869 .await?;
7870 }
7871 let (visible, slot) = metadata_body(Some(body.clone()))?;
7872 item.body = visible.filter(|value| !value.is_empty());
7873 item.raw_body = Some(body);
7874 item.slot = slot;
7875 Ok(())
7876 }
7877
7878 /// This instance's target for a category written to an item of `kind`, refusing one
7879 /// that kind has no option for — before anything is read or written.
7880 ///
7881 /// Nothing here mutates the board's option set to make room for a status. GitHub
7882 /// documents `UpdateProjectV2FieldInput.singleSelectOptions` as *"provided values
7883 /// overwrite existing options"*, so no addition is additive and a mistake destroys the
7884 /// field and every item's status.
7885 fn resolved_target(
7886 &self,
7887 kind: ItemKind,
7888 category: StatusCategory,
7889 ) -> Result<StatusTarget, SourceError> {
7890 let target = self.statuses.target(kind, category).clone();
7891 let StatusTarget::Disabled(why) = target else {
7892 return Ok(target);
7893 };
7894 let refusal = why.refusal(&self.name, category, kind);
7895 // Why there is no shipped default, which is the question a person meeting this
7896 // refusal on a source that never mentioned the category asks.
7897 let shipped_none = match category {
7898 StatusCategory::Draft => Some(
7899 "draft has no shipped default because GitHub draft issues cannot have \
7900 sub-issues, and this source stores a project's tasks as its issue's sub-issues",
7901 ),
7902 StatusCategory::Unknown => Some(
7903 "unknown has no shipped default because this board keeps no open-ended status \
7904 word: every word classified unknown is written to the one board Status option \
7905 status_mapping.unknown names",
7906 ),
7907 _ => None,
7908 };
7909 Err(match (refusal, shipped_none, why) {
7910 (SourceError::Refused { message }, Some(note), UnmappedStatus::Unconfigured) => {
7911 SourceError::Refused {
7912 message: format!("{message}; {note}"),
7913 }
7914 }
7915 (refusal, _, _) => refusal,
7916 })
7917 }
7918
7919 /// What writing `priority` does to one item's `Priority` field on this board, or the
7920 /// refusal naming what the board lacks.
7921 ///
7922 /// `none` is no value, so it clears the field — and asks nothing of an item that holds
7923 /// none already, or of an item not created yet. Every other priority selects the option
7924 /// the mapping names, matched case-insensitively; a board with no `Priority` field, or
7925 /// without that option, is refused rather than given one: reads and writes never create
7926 /// a field or an option.
7927 fn priority_write(
7928 &self,
7929 fields: &Value,
7930 existing: Option<&Resolved>,
7931 priority: Priority,
7932 ) -> Result<Option<PriorityWrite>, SourceError> {
7933 let Some(mapping) = &self.priorities else {
7934 return Err(self.holds_no_priority());
7935 };
7936 let Some(wanted) = mapping.option(priority) else {
7937 if !existing.is_some_and(Resolved::holds_priority) {
7938 return Ok(None);
7939 }
7940 let field =
7941 Board::field(fields, PRIORITY_FIELD)?.ok_or_else(|| SourceError::Malformed {
7942 message: format!(
7943 "an item holding a {PRIORITY_FIELD} value was read without that field"
7944 ),
7945 })?;
7946 return Ok(Some(PriorityWrite::Clear {
7947 field: required_str(field, "id")?.to_owned(),
7948 }));
7949 };
7950 let missing = |detail: &str| SourceError::Refused {
7951 message: format!(
7952 "priority {priority} of source {} needs the board {PRIORITY_FIELD} option \
7953 {wanted:?}, and {detail}; run `onetaskgraph sources fields {} --apply` to add \
7954 it, or point priority_mapping.{priority} of this source at an option the board \
7955 has",
7956 self.name, self.name
7957 ),
7958 };
7959 let Some(field) = Board::field(fields, PRIORITY_FIELD)? else {
7960 return Err(missing(&format!(
7961 "this board has no {PRIORITY_FIELD} field"
7962 )));
7963 };
7964 if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
7965 return Err(missing(&format!(
7966 "this board's {PRIORITY_FIELD} field is not a single-select field"
7967 )));
7968 }
7969 // An options list that is absent or not a list is an answer this source cannot read,
7970 // not a board lacking the option: `sources fields --apply` is no remedy for it.
7971 let option = field
7972 .get("options")
7973 .and_then(Value::as_array)
7974 .ok_or_else(|| SourceError::Malformed {
7975 message: format!("GitHub {PRIORITY_FIELD} field options is not an array"),
7976 })?
7977 .iter()
7978 .find(|option| {
7979 option
7980 .get("name")
7981 .and_then(Value::as_str)
7982 .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
7983 })
7984 .ok_or_else(|| missing("this board does not have it"))?;
7985 Ok(Some(PriorityWrite::Select {
7986 field: required_str(field, "id")?.to_owned(),
7987 option: required_str(option, "id")?.to_owned(),
7988 }))
7989 }
7990
7991 /// What writing `metadata` does to each projected text field of one item, and what those
7992 /// fields hold after it — or the refusal, before anything is sent.
7993 ///
7994 /// `held` is what the item's fields hold now, `None` for an item not created yet. `keys`
7995 /// narrows the work to the entries projecting one of those metadata keys, for a write that
7996 /// touches only them; `None` is every entry. A string the field already holds, and nothing
7997 /// for a field holding nothing, sends nothing. A value no text field can hold is refused,
7998 /// and so is a board without the field, or with a field of that name that is not a text
7999 /// field: reads and writes never create a field.
8000 fn projection_writes(
8001 &self,
8002 fields: &Value,
8003 held: Option<&BTreeMap<String, String>>,
8004 metadata: &BTreeMap<String, Value>,
8005 keys: Option<&[&str]>,
8006 ) -> Result<(Vec<ProjectionWrite>, BTreeMap<String, String>), SourceError> {
8007 let mut projected = held.cloned().unwrap_or_default();
8008 let mut writes = Vec::new();
8009 for projection in self
8010 .metadata_fields
8011 .iter()
8012 .filter(|projection| keys.is_none_or(|keys| keys.contains(&&*projection.key)))
8013 {
8014 let wanted = projection.projected(metadata, &self.name)?;
8015 let field = self.projection_field(fields, projection)?;
8016 let holding = projected.get(&*projection.field);
8017 match wanted {
8018 Projected::Text(text) if holding != Some(&text) => {
8019 writes.push(ProjectionWrite::Set {
8020 field,
8021 text: text.clone(),
8022 });
8023 projected.insert(projection.field.to_string(), text);
8024 }
8025 Projected::Nothing if holding.is_some() => {
8026 writes.push(ProjectionWrite::Clear { field });
8027 projected.remove(&*projection.field);
8028 }
8029 Projected::Text(_) | Projected::Nothing => {}
8030 }
8031 }
8032 Ok((writes, projected))
8033 }
8034
8035 /// The id of the board text field one projection writes, or the refusal naming what the
8036 /// board has instead.
8037 fn projection_field(
8038 &self,
8039 fields: &Value,
8040 projection: &MetadataField,
8041 ) -> Result<String, SourceError> {
8042 let refuse = |detail: String| SourceError::Refused {
8043 message: format!(
8044 "source {} projects metadata key {:?} onto the board text field {:?}, and \
8045 {detail}; next: run `onetaskgraph sources fields {} --apply` to create it as a \
8046 text field, or name another field in metadata_fields",
8047 self.name, &*projection.key, &*projection.field, self.name
8048 ),
8049 };
8050 let Some(field) = Board::field(fields, &projection.field)? else {
8051 return Err(refuse("this board has no field of that name".to_owned()));
8052 };
8053 let typename = optional_str(field, "__typename")?.unwrap_or("field of another type");
8054 let data_type = optional_str(field, "dataType")?;
8055 if typename != "ProjectV2Field" || data_type != Some("TEXT") {
8056 return Err(refuse(format!(
8057 "this board's field of that name is not a text field (it is a {}), so it is \
8058 left as it is; rename or remove that field first",
8059 data_type.unwrap_or(typename)
8060 )));
8061 }
8062 Ok(required_str(field, "id")?.to_owned())
8063 }
8064
8065 /// Apply one priority write to one board item.
8066 async fn write_priority(
8067 &self,
8068 board_id: &str,
8069 item_id: &str,
8070 write: &PriorityWrite,
8071 ) -> Result<(), SourceError> {
8072 match write {
8073 PriorityWrite::Select { field, option } => {
8074 self.set_item_field(
8075 board_id,
8076 item_id,
8077 field,
8078 json!({"singleSelectOptionId": option}),
8079 )
8080 .await
8081 }
8082 PriorityWrite::Clear { field } => {
8083 let data = self
8084 .graphql(
8085 graphql::CLEAR_FIELD,
8086 json!({"input":{"projectId":board_id,"itemId":item_id,"fieldId":field},
8087 "readPriority":false,"priorityName":PRIORITY_FIELD}),
8088 )
8089 .await?;
8090 let returned = data
8091 .pointer("/clearProjectV2ItemFieldValue/projectV2Item")
8092 .ok_or_else(|| SourceError::Malformed {
8093 message: "GitHub field clear returned no project item".into(),
8094 })?;
8095 if required_str(returned, "id")? != item_id {
8096 return Err(SourceError::Malformed {
8097 message: "GitHub field clear returned the wrong project item".into(),
8098 });
8099 }
8100 Ok(())
8101 }
8102 }
8103 }
8104
8105 /// The refusal a priority is answered with by an instance configured with no
8106 /// `priority_mapping`, which holds none.
8107 fn holds_no_priority(&self) -> SourceError {
8108 SourceError::Refused {
8109 message: format!(
8110 "source {} holds no task priority: its configuration sets no priority_mapping; \
8111 next: set priority_mapping on this source, then run `onetaskgraph sources \
8112 fields {} --apply` to set its board up",
8113 self.name, self.name
8114 ),
8115 }
8116 }
8117
8118 /// Set one task's priority and nothing else; see [`TaskSource::set_task_priority`].
8119 ///
8120 /// One field write — a select, or a clear for `none` — and no title, body, label, state
8121 /// or `Status` request. Clearing a priority an item does not hold sends nothing.
8122 async fn set_priority(
8123 &self,
8124 id: &NativeId,
8125 priority: Priority,
8126 ) -> Result<Option<Priority>, SourceError> {
8127 if self.priorities.is_none() {
8128 return Err(self.holds_no_priority());
8129 }
8130 let Some(mut item) = self
8131 .bound_item(id)
8132 .await?
8133 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8134 else {
8135 return Ok(None);
8136 };
8137 if priority == Priority::None && !item.holds_priority() {
8138 return Ok(Some(priority));
8139 }
8140 // The item's own read carries the field's definition whenever it holds a value of
8141 // it, which a clear always does; a select onto an item holding none reads the board.
8142 let board = match (item.carried_board(), item.named_board()) {
8143 (Some(board), _) => board,
8144 (None, Some(id)) if item.defines(PRIORITY_FIELD) => BoardFields {
8145 id,
8146 fields: json!({"nodes": item.fields.clone(), "pageInfo": {"hasNextPage": false}}),
8147 },
8148 _ => self.board_fields().await?,
8149 };
8150 let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? else {
8151 return Ok(Some(priority));
8152 };
8153 let (document, root, input) = match write {
8154 PriorityWrite::Select { field, option } => (
8155 graphql::UPDATE_FIELD,
8156 "updateProjectV2ItemFieldValue",
8157 json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field,"value":{"singleSelectOptionId":option}}),
8158 ),
8159 PriorityWrite::Clear { field } => (
8160 graphql::CLEAR_FIELD,
8161 "clearProjectV2ItemFieldValue",
8162 json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field}),
8163 ),
8164 };
8165 let data = self
8166 .graphql(
8167 document,
8168 json!({"input":input,"readPriority":true,"priorityName":PRIORITY_FIELD}),
8169 )
8170 .await?;
8171 let returned = data
8172 .get(root)
8173 .and_then(|value| value.get("projectV2Item"))
8174 .ok_or_else(|| SourceError::Malformed {
8175 message: "GitHub priority write returned no project item".into(),
8176 })?;
8177 if required_str(returned, "id")? != item.item_id {
8178 return Err(SourceError::Malformed {
8179 message: "GitHub priority write returned the wrong project item".into(),
8180 });
8181 }
8182 let value = returned
8183 .get("fieldValueByName")
8184 .ok_or_else(|| SourceError::Malformed {
8185 message: "GitHub priority write returned no priority read-back".into(),
8186 })?;
8187 if !value.is_null()
8188 && value.pointer("/field/name").and_then(Value::as_str) != Some(PRIORITY_FIELD)
8189 {
8190 return Err(SourceError::Malformed {
8191 message: "GitHub priority read-back is not a Priority field value".into(),
8192 });
8193 }
8194 let values = if value.is_null() {
8195 Vec::new()
8196 } else {
8197 vec![value.clone()]
8198 };
8199 item.priority = self.held_priority(&values)?;
8200 let answer = item.task()?.priority;
8201 self.remember_written(item, false)?;
8202 Ok(Some(answer))
8203 }
8204
8205 /// Replace one task's visible body and nothing else; see
8206 /// [`TaskSource::set_task_content`].
8207 ///
8208 /// One update of the body, which differs from the body GitHub holds only outside the
8209 /// metadata slot — the slot is kept byte for byte, so every caller key and every list
8210 /// this source keeps there reads back as it was. A body that would not change is not
8211 /// sent at all.
8212 async fn replace_content(
8213 &self,
8214 id: &NativeId,
8215 content: &str,
8216 ) -> Result<Option<()>, SourceError> {
8217 let Some(mut item) = self
8218 .bound_item(id)
8219 .await?
8220 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8221 else {
8222 return Ok(None);
8223 };
8224 let held = item.raw_body.clone().unwrap_or_default();
8225 let body = with_content(&held, content)?;
8226 // Checked before anything is sent: content ending in what this source reads as its own
8227 // metadata slot would read back as metadata rather than as the content it was.
8228 let (visible, slot) = metadata_body(Some(body.clone()))?;
8229 if visible.as_deref().unwrap_or_default() != content || slot != item.slot {
8230 return Err(SourceError::Refused {
8231 message: format!(
8232 "this content ends in what source {} reads as its own metadata slot \
8233 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8234 as content; next: remove that trailing block from the content",
8235 self.name
8236 ),
8237 });
8238 }
8239 if body != held {
8240 self.update_content(item.content_kind, &item.id, json!({"body": body}))
8241 .await?;
8242 }
8243 item.body = visible.filter(|value| !value.is_empty());
8244 item.raw_body = Some(body);
8245 item.slot = slot;
8246 self.remember_written(item, false)?;
8247 Ok(Some(()))
8248 }
8249
8250 /// Apply one targeted update to one task; see [`TaskSource::update_task`].
8251 ///
8252 /// One read of the item — which carries the board's field definitions and the issue's
8253 /// `blockedBy`, so neither is read again — and then only what differs from it: the
8254 /// `Status` option and the `Priority` field together in one request, the `blockedBy`
8255 /// additions and removals the named edges differ by, and last one `updateIssue` carrying
8256 /// the title, the body — visible content and metadata slot together — and a state change.
8257 /// So an update naming any of title, body, metadata, status and priority is one read and
8258 /// at most two writes. The body goes last so that a write refused part-way leaves it, and
8259 /// the metadata in it, as it stood. A terminal status selects its option and then closes,
8260 /// as a whole write does; an open one selects its option and then reopens. The origin
8261 /// field is never written: an update is of an item that already exists, whose origin is
8262 /// what it is.
8263 ///
8264 /// The task answered is the item as those writes left it, built from the read and what was
8265 /// sent rather than read again — the same record a later read in this run answers from.
8266 async fn targeted_update(
8267 &self,
8268 id: &NativeId,
8269 update: &TaskUpdate,
8270 ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
8271 // Everything this source can refuse without reading the item is refused first, in the
8272 // words a whole write of the same fields is refused with.
8273 update.consistent()?;
8274 if update
8275 .title
8276 .as_deref()
8277 .is_some_and(|title| title.starts_with(DESIGN_TITLE_PREFIX))
8278 {
8279 return Err(SourceError::Refused {
8280 message: format!(
8281 "the title of this task begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
8282 spells a document, so it would read back as one rather than as a task; \
8283 retitle it",
8284 self.name
8285 ),
8286 });
8287 }
8288 if let Some(delivers) = &update.delivers {
8289 TaskRef::listed(
8290 TaskRef::DELIVERS_KEY,
8291 id,
8292 Some(&self.name),
8293 delivers.clone(),
8294 )
8295 .map_err(|message| SourceError::Refused { message })?;
8296 }
8297 if self.priorities.is_none()
8298 && update
8299 .priority
8300 .is_some_and(|priority| priority != Priority::None)
8301 {
8302 return Err(self.holds_no_priority());
8303 }
8304 let target = update
8305 .status
8306 .as_ref()
8307 .map(|status| self.resolved_target(ItemKind::Task, status.category))
8308 .transpose()?;
8309 let Some(mut item) = self
8310 .bound_item(id)
8311 .await?
8312 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8313 else {
8314 return Ok(None);
8315 };
8316 let before = item.task()?;
8317
8318 let mut status_move = None;
8319 if let (Some(status), Some(target)) = (&update.status, target) {
8320 let board = self.status_board(&item).await?;
8321 let (field, option, name) = self
8322 .column_for(&board.fields, ItemKind::Task, status.category, &target)?
8323 .ok_or_else(|| SourceError::Malformed {
8324 message: format!(
8325 "status {} of source {} names no board Status option",
8326 category_name(status.category),
8327 self.name
8328 ),
8329 })?;
8330 let terminal = matches!(target, StatusTarget::Terminal(_, _));
8331 if terminal && item.content_kind == ContentKind::DraftIssue {
8332 return Err(self.closes_a_draft(status.category));
8333 }
8334 let landed = match &target {
8335 StatusTarget::Terminal(_, reason) => {
8336 self.statuses
8337 .status(ItemKind::Task, Some(&name), true, Some(reason.reason()))
8338 }
8339 _ => self
8340 .statuses
8341 .status(ItemKind::Task, Some(&name), false, None),
8342 };
8343 let option_moves = item
8344 .option
8345 .as_deref()
8346 .is_none_or(|held| !held.eq_ignore_ascii_case(&name));
8347 let state_moves = item.content_kind == ContentKind::Issue
8348 && (item.closed != terminal || (terminal && item.status != landed));
8349 if let Some(moves) = Moves::of(option_moves, state_moves) {
8350 status_move = Some(StatusMove {
8351 board: board.id,
8352 field,
8353 option,
8354 name,
8355 target,
8356 landed,
8357 moves,
8358 });
8359 }
8360 }
8361
8362 let mut priority_move = None;
8363 if let Some(priority) = update.priority
8364 && self.priorities.is_some()
8365 && item.priority != HeldPriority::Read(priority)
8366 {
8367 let board = match (item.carried_board(), item.named_board()) {
8368 (Some(board), _) => board,
8369 (None, Some(board)) if item.defines(PRIORITY_FIELD) => BoardFields {
8370 id: board,
8371 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
8372 },
8373 _ => self.board_fields().await?,
8374 };
8375 if let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? {
8376 priority_move = Some((board.id, write, priority));
8377 }
8378 }
8379
8380 // The metadata keys this update names that a board text field projects, and what
8381 // that does to those fields — refused, as a whole write refuses it, before anything
8382 // is sent.
8383 let touched: Vec<&str> = update
8384 .metadata_set
8385 .keys()
8386 .chain(update.metadata_remove.iter())
8387 .map(MetadataKey::as_str)
8388 .collect();
8389 let mut projection_move = None;
8390 if self
8391 .metadata_fields
8392 .iter()
8393 .any(|projection| touched.contains(&&*projection.key))
8394 {
8395 let mut metadata = item.slot.clone();
8396 for (key, value) in &update.metadata_set {
8397 metadata.insert(key.as_str().to_owned(), value.clone());
8398 }
8399 for key in &update.metadata_remove {
8400 metadata.remove(key.as_str());
8401 }
8402 let board = self.projection_board(&item).await?;
8403 let (writes, projected) = self.projection_writes(
8404 &board.fields,
8405 Some(&item.projected),
8406 &metadata,
8407 Some(&touched),
8408 )?;
8409 projection_move = Some((board.id, writes, projected));
8410 }
8411
8412 // Resolved before the body is composed, because a far end `blockedBy` cannot name is
8413 // recorded in the slot, and the slot travels in the one body update below.
8414 let edges = match &update.depends_on {
8415 Some(edges) => Some(
8416 self.partition_edges(
8417 BoardKind::Work(ItemKind::Task),
8418 item.content_kind,
8419 item.blocked_by.as_deref(),
8420 edges,
8421 )
8422 .await?,
8423 ),
8424 None => None,
8425 };
8426
8427 let mut slot = item.slot.clone();
8428 for (key, value) in &update.metadata_set {
8429 slot.insert(key.as_str().to_owned(), value.clone());
8430 }
8431 for key in &update.metadata_remove {
8432 slot.remove(key.as_str());
8433 }
8434 if let Some(delivers) = &update.delivers {
8435 set_task_list(&mut slot, TaskRef::DELIVERS_KEY, delivers);
8436 }
8437 if let Some((_, recorded)) = &edges {
8438 record_edges(&mut slot, recorded);
8439 }
8440 let held = item.raw_body.clone().unwrap_or_default();
8441 let content = match &update.content {
8442 Some(content) => with_content(&held, content)?,
8443 None => held.clone(),
8444 };
8445 // A slot holding what it held is kept byte for byte, compared as JSON rather than as
8446 // the body's bytes, as a metadata write compares it: a slot a person spelled with
8447 // other whitespace would otherwise be re-encoded, which is a write nobody asked for.
8448 let body = if slot == item.slot {
8449 content
8450 } else {
8451 with_slot(&content, &slot)?
8452 };
8453 // Checked before anything is sent, as a content write checks it: content ending in
8454 // what this source reads as its own slot would read back as metadata.
8455 let (visible, read) = metadata_body(Some(body.clone()))?;
8456 let wanted = update.content.as_deref().or(item.body.as_deref());
8457 if visible.as_deref().unwrap_or_default() != wanted.unwrap_or_default() || read != slot {
8458 return Err(SourceError::Refused {
8459 message: format!(
8460 "this content ends in what source {} reads as its own metadata slot \
8461 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8462 as content; next: remove that trailing block from the content",
8463 self.name
8464 ),
8465 });
8466 }
8467 let recorded_moves =
8468 slot.get(DependencyEdge::RECORDED_KEY) != item.slot.get(DependencyEdge::RECORDED_KEY);
8469
8470 // One `updateIssue` carries all three, because every mutation spends the secondary
8471 // limiter and the title, body and state are one mutation's inputs.
8472 let mut fields = serde_json::Map::new();
8473 if let Some(title) = update.title.as_ref().filter(|title| **title != item.title) {
8474 fields.insert("title".to_owned(), json!(title));
8475 }
8476 if body != held {
8477 fields.insert("body".to_owned(), json!(body));
8478 }
8479 if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.state()) {
8480 fields.insert("stateInput".to_owned(), state_input(Some(&moving.target)));
8481 }
8482 // **The body is written last, and that is the guarantee a refusal part-way keeps.**
8483 // GitHub runs no two requests as one, and runs one document's mutation fields in order
8484 // without undoing an earlier field when a later one fails — so a body written before a
8485 // board field the board then refused would be left changed. Written after every other
8486 // write has landed, a refusal anywhere leaves the item's body, and every metadata key
8487 // it carries, exactly as they stood. So the `Status` option and the `Priority` field go
8488 // first, together in one request — a terminal option selected before the issue
8489 // closes, as a whole write does — then the `blockedBy` difference, then the body.
8490 let mut board_writes: Vec<(&BoardId, (String, Value))> = Vec::new();
8491 let mut clears: Vec<(&BoardId, &str)> = Vec::new();
8492 if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.option()) {
8493 board_writes.push((
8494 &moving.board,
8495 (
8496 moving.field.clone(),
8497 json!({"singleSelectOptionId": moving.option}),
8498 ),
8499 ));
8500 }
8501 match &priority_move {
8502 Some((board, PriorityWrite::Select { field, option }, _)) => board_writes.push((
8503 board,
8504 (field.clone(), json!({"singleSelectOptionId": option})),
8505 )),
8506 Some((board, PriorityWrite::Clear { field }, _)) => clears.push((board, field)),
8507 None => {}
8508 }
8509 if let Some((board, writes, _)) = &projection_move {
8510 for write in writes {
8511 let (mut values, mut cleared) = (Vec::new(), Vec::new());
8512 write.join(&mut values, &mut cleared);
8513 board_writes.extend(values.into_iter().map(|value| (board, value)));
8514 clears.extend(cleared.into_iter().map(|field| (board, field)));
8515 }
8516 }
8517 let mut boards: Vec<&BoardId> = board_writes.iter().map(|(board, _)| *board).collect();
8518 boards.extend(clears.iter().map(|(board, _)| *board));
8519 boards.dedup_by(|one, other| one.as_str() == other.as_str());
8520 for board in boards {
8521 let writes = board_writes
8522 .iter()
8523 .filter(|(on, _)| on.as_str() == board.as_str())
8524 .map(|(_, write)| write.clone())
8525 .collect::<Vec<_>>();
8526 let cleared = clears
8527 .iter()
8528 .filter(|(on, _)| on.as_str() == board.as_str())
8529 .map(|(_, field)| *field)
8530 .collect::<Vec<_>>();
8531 self.set_item_fields(board.as_str(), &item.item_id, &writes, &cleared)
8532 .await?;
8533 }
8534 let mut blocked_by_moved = false;
8535 if let Some((native, _)) = &edges
8536 && item.content_kind == ContentKind::Issue
8537 {
8538 blocked_by_moved = self
8539 .reconcile_blocked_by(
8540 &item.id,
8541 native,
8542 Issue::Existing(item.blocked_by.as_deref()),
8543 )
8544 .await?;
8545 }
8546 if !fields.is_empty() {
8547 self.update_content(item.content_kind, &item.id, Value::Object(fields))
8548 .await?;
8549 }
8550
8551 // The `blockedBy` this item was read with is what the write just changed, so it is no
8552 // answer any more: a later write in this command would reconcile against it — re-adding
8553 // what it kept and never removing what it dropped — and a forward-edge read would
8554 // answer with it. Left unknown, each asks GitHub instead.
8555 if blocked_by_moved {
8556 item.blocked_by = None;
8557 }
8558 if let Some(title) = &update.title {
8559 item.title.clone_from(title);
8560 }
8561 item.body = visible.filter(|value| !value.is_empty());
8562 item.raw_body = (!body.is_empty() || item.raw_body.is_some()).then_some(body);
8563 item.slot = slot;
8564 if let Some(delivers) = &update.delivers {
8565 item.delivers.clone_from(delivers);
8566 }
8567 if let Some(moving) = status_move {
8568 item.closed = matches!(moving.target, StatusTarget::Terminal(_, _))
8569 && item.content_kind == ContentKind::Issue;
8570 item.status = moving.landed;
8571 item.option = Some(moving.name);
8572 }
8573 if let Some((_, _, priority)) = priority_move {
8574 item.priority = HeldPriority::Read(priority);
8575 }
8576 if let Some((_, _, projected)) = projection_move {
8577 item.projected = projected;
8578 }
8579 let task = item.task()?;
8580 let mut written = update.changed(&before, &task);
8581 if blocked_by_moved || recorded_moves {
8582 written.insert(UpdatedField::DependsOn);
8583 }
8584 self.remember_written(item, false)?;
8585 Ok(Some(TaskUpdateOutcome {
8586 task,
8587 written,
8588 delivers_before: before.delivers,
8589 }))
8590 }
8591
8592 /// Replace one issue's visible body and its [`MetadataKey::TEMPLATE_KEY`] slot entry
8593 /// together, and nothing else; see [`TaskSource::set_task_rendering`].
8594 ///
8595 /// One update of the body: the content outside the slot, and inside it that one entry,
8596 /// every other entry kept as it was. This source keeps no template answers — an issue has
8597 /// no room beside itself that is not its body, and answers written there would duplicate
8598 /// what the content already says and count against GitHub's body limit — so `answers`
8599 /// reaches nothing here. A body that would not change is not sent at all.
8600 async fn replace_rendering(
8601 &self,
8602 id: &NativeId,
8603 kind: BoardKind,
8604 content: &str,
8605 provenance: &Value,
8606 assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
8607 ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
8608 let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
8609 return Ok(None);
8610 };
8611 let held = item.raw_body.clone().unwrap_or_default();
8612 let mut slot = item.slot.clone();
8613 slot.insert(MetadataKey::TEMPLATE_KEY.to_owned(), provenance.clone());
8614 let rewritten;
8615 let content = if let Some(assets) = assets {
8616 let uploads = self
8617 .upload_assets(item.own_repository.as_ref(), assets)
8618 .await?;
8619 rewritten =
8620 onetaskgraph_plugin_api::serve_asset_references(content, &mut slot, &uploads);
8621 rewritten.as_str()
8622 } else {
8623 content
8624 };
8625 let body = with_slot(&with_content(&held, content)?, &slot)?;
8626 // Checked before anything is sent, as a content write checks it.
8627 let (visible, read) = metadata_body(Some(body.clone()))?;
8628 if visible.as_deref().unwrap_or_default() != content || read != slot {
8629 return Err(SourceError::Refused {
8630 message: format!(
8631 "this content ends in what source {} reads as its own metadata slot \
8632 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8633 as content; next: remove that trailing block from the template",
8634 self.name
8635 ),
8636 });
8637 }
8638 if body != held {
8639 self.update_content(item.content_kind, &item.id, json!({"body": body}))
8640 .await?;
8641 }
8642 item.body = visible.filter(|value| !value.is_empty());
8643 item.raw_body = Some(body);
8644 item.slot = read;
8645 self.remember_written(item, false)?;
8646 Ok(Some(onetaskgraph_plugin_api::AssetsWritten {
8647 id: id.clone(),
8648 content: Some(content.to_owned()),
8649 }))
8650 }
8651
8652 async fn set_item_field(
8653 &self,
8654 board_id: &str,
8655 item_id: &str,
8656 field_id: &str,
8657 value: Value,
8658 ) -> Result<(), SourceError> {
8659 let data = self
8660 .graphql(
8661 graphql::UPDATE_FIELD,
8662 json!({"input":{
8663 "projectId":board_id,"itemId":item_id,"fieldId":field_id,"value":value
8664 },"readPriority":false,"priorityName":PRIORITY_FIELD}),
8665 )
8666 .await?;
8667 let returned = data
8668 .pointer("/updateProjectV2ItemFieldValue/projectV2Item")
8669 .ok_or_else(|| SourceError::Malformed {
8670 message: "GitHub field update returned no project item".into(),
8671 })?;
8672 if required_str(returned, "id")? != item_id {
8673 return Err(SourceError::Malformed {
8674 message: "GitHub field update returned the wrong project item".into(),
8675 });
8676 }
8677 Ok(())
8678 }
8679
8680 /// GitHub accepts one value per field mutation; aliases combine those mutations in
8681 /// one request — writes in the order given, then clears. A lone write is
8682 /// [`graphql::UPDATE_FIELD`] and a lone clear [`graphql::CLEAR_FIELD`]; more than one
8683 /// request's slots hold go in further requests of the same document, in order. Every
8684 /// returned item id is checked, including optional aliases.
8685 async fn set_item_fields(
8686 &self,
8687 board: &str,
8688 item: &str,
8689 fields: &[(String, Value)],
8690 clears: &[&str],
8691 ) -> Result<(), SourceError> {
8692 match (fields, clears) {
8693 ([], []) => return Ok(()),
8694 ([(field, value)], []) => {
8695 return self.set_item_field(board, item, field, value.clone()).await;
8696 }
8697 ([], [field]) => {
8698 return self
8699 .write_priority(
8700 board,
8701 item,
8702 &PriorityWrite::Clear {
8703 field: (*field).to_owned(),
8704 },
8705 )
8706 .await;
8707 }
8708 _ => {}
8709 }
8710 // Any field of the request stands in for an input a slot leaves out, which GitHub
8711 // still requires to be well formed although it never runs it.
8712 let filler = fields
8713 .first()
8714 .map_or_else(|| clears[0], |(field, _)| field.as_str());
8715 let mut writes = fields.iter();
8716 let mut cleared = clears.iter();
8717 loop {
8718 let batch: Vec<&(String, Value)> =
8719 writes.by_ref().take(FIELD_WRITE_SLOTS.len()).collect();
8720 let batch_clears: Vec<&&str> = cleared.by_ref().take(FIELD_CLEAR_SLOTS.len()).collect();
8721 if batch.is_empty() && batch_clears.is_empty() {
8722 return Ok(());
8723 }
8724 let mut variables = serde_json::Map::new();
8725 for (index, slot) in FIELD_WRITE_SLOTS.iter().enumerate() {
8726 let input = match batch.get(index) {
8727 Some((field, value)) => {
8728 json!({"projectId":board,"itemId":item,"fieldId":field,"value":value})
8729 }
8730 None => json!({"projectId":board,"itemId":item,"fieldId":filler,
8731 "value":{"text":""}}),
8732 };
8733 variables.insert(slot.variable.to_owned(), input);
8734 variables.insert(slot.include.to_owned(), json!(index < batch.len()));
8735 }
8736 for (index, slot) in FIELD_CLEAR_SLOTS.iter().enumerate() {
8737 let field = batch_clears.get(index).map_or(filler, |field| **field);
8738 variables.insert(
8739 slot.variable.to_owned(),
8740 json!({"projectId":board,"itemId":item,"fieldId":field}),
8741 );
8742 variables.insert(slot.include.to_owned(), json!(index < batch_clears.len()));
8743 }
8744 let data = self
8745 .graphql(graphql::UPDATE_FIELDS, Value::Object(variables))
8746 .await?;
8747 let sent = FIELD_WRITE_SLOTS[..batch.len()]
8748 .iter()
8749 .chain(&FIELD_CLEAR_SLOTS[..batch_clears.len()]);
8750 for slot in sent {
8751 let alias = slot.alias;
8752 let returned = data
8753 .get(alias)
8754 .and_then(|value| value.get("projectV2Item"))
8755 .ok_or_else(|| SourceError::Malformed {
8756 message: format!("GitHub field update {alias} returned no project item"),
8757 })?;
8758 if required_str(returned, "id")? != item {
8759 return Err(SourceError::Malformed {
8760 message: format!(
8761 "GitHub field update {alias} returned the wrong project item"
8762 ),
8763 });
8764 }
8765 }
8766 }
8767 }
8768
8769 async fn native_dependency_ids(&self, id: &NativeId) -> Result<Vec<String>, SourceError> {
8770 let mut after: Option<String> = None;
8771 let mut ids = Vec::new();
8772 loop {
8773 let data = self
8774 .graphql(
8775 graphql::ISSUE_DEPENDENCIES,
8776 json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after}),
8777 )
8778 .await?;
8779 let connection =
8780 data.pointer("/node/blockedBy")
8781 .ok_or_else(|| SourceError::Malformed {
8782 message: "GitHub dependency response has no blockedBy connection".into(),
8783 })?;
8784 ids.extend(
8785 connection
8786 .get("nodes")
8787 .and_then(Value::as_array)
8788 .ok_or_else(|| SourceError::Malformed {
8789 message: "GitHub dependency response nodes is not an array".into(),
8790 })?
8791 .iter()
8792 .map(|value| required_str(value, "id").map(str::to_owned))
8793 .collect::<Result<Vec<_>, _>>()?,
8794 );
8795 let next = next_cursor(connection)?;
8796 if let Some(next) = &next {
8797 validate_cursor_progress(after.as_deref(), &next.0)?;
8798 }
8799 after = next.map(|cursor| cursor.0);
8800 if after.is_none() {
8801 return Ok(ids);
8802 }
8803 }
8804 }
8805
8806 async fn dependencies(
8807 &self,
8808 id: &NativeId,
8809 near_kind: ItemKind,
8810 direction: Direction,
8811 page: &PageRequest,
8812 ) -> Result<Page<DependencyEdge>, SourceError> {
8813 validate_page(page)?;
8814 let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
8815 let cursor = page.cursor.as_ref().map(|c| c.0.as_str());
8816 let recorded = recorded_offset(cursor, direction)?;
8817 // What this issue is blocked by, when a read of it by its own id in this command
8818 // already carried the whole connection — a copy reads the item it writes before it
8819 // reads its edges — and the page asked for is the whole of it, or the recorded tail
8820 // after it. Answered from that read, in the shape the dependency read answers in;
8821 // anything else is asked of GitHub.
8822 let carried = match direction {
8823 Direction::DependsOn => self
8824 .resolved_cache()?
8825 .get(id)
8826 .filter(|item| item.content_kind == ContentKind::Issue)
8827 .and_then(|item| Some((item.blocked_by.clone()?, item.raw_body.clone()))),
8828 Direction::DependedOnBy => None,
8829 }
8830 .filter(|(nodes, _)| recorded.is_some() || (cursor.is_none() && nodes.len() <= limit));
8831 // Asked for even in the recorded phase, whose page reads nothing from the
8832 // connection: `__typename` is what says whether this item has a native
8833 // relationship at all, and that is what decides which far ends the reserved key is
8834 // allowed to hold.
8835 let data = match carried {
8836 Some((nodes, body)) => json!({"node":{"__typename":"Issue","body":body,
8837 "blockedBy":{"nodes":nodes,"pageInfo":{"hasNextPage":false,"endCursor":null}}}}),
8838 None => {
8839 let after = if recorded.is_some() { None } else { cursor };
8840 let first = page.limit.min(MAX_PAGE_SIZE);
8841 match direction {
8842 Direction::DependsOn => {
8843 self.graphql(
8844 graphql::ISSUE_DEPENDS_ON,
8845 json!({"id":id.0,"first":first,"after":after,"owner":self.owner,
8846 "number":self.project_number,
8847 "boardItems":BOARD_ITEMS_PAGE_SIZE}),
8848 )
8849 .await?
8850 }
8851 Direction::DependedOnBy => {
8852 self.graphql(
8853 graphql::ISSUE_DEPENDED_ON_BY,
8854 json!({"id":id.0,"first":first,"after":after}),
8855 )
8856 .await?
8857 }
8858 }
8859 }
8860 };
8861 let draft_slot = draft_slot(&data, id, self.project_number)?;
8862 let node =
8863 data.get("node")
8864 .filter(|v| !v.is_null())
8865 .ok_or_else(|| SourceError::Refused {
8866 message: format!(
8867 "GitHub item {} was not found or does not support dependencies",
8868 id.0
8869 ),
8870 })?;
8871 let connection_name = match direction {
8872 Direction::DependsOn => "blockedBy",
8873 Direction::DependedOnBy => "blocking",
8874 };
8875 // A draft has neither `blockedBy` nor `blocking`, so nothing it depends on can be
8876 // named natively and the reserved key may hold any far end. An issue's connections
8877 // hold issues, and this source reads them at the near item's own level.
8878 let natively_names = (required_str(node, "__typename")? == "Issue").then_some(near_kind);
8879 if let Some(offset) = recorded {
8880 return Ok(recorded_page(
8881 self.recorded_edges(
8882 id,
8883 near_kind,
8884 direction,
8885 natively_names,
8886 node,
8887 draft_slot.as_ref(),
8888 )
8889 .await?,
8890 offset,
8891 limit,
8892 ));
8893 }
8894 if natively_names.is_none() {
8895 return Ok(recorded_page(
8896 self.recorded_edges(
8897 id,
8898 near_kind,
8899 direction,
8900 natively_names,
8901 node,
8902 draft_slot.as_ref(),
8903 )
8904 .await?,
8905 0,
8906 limit,
8907 ));
8908 }
8909 let connection = node
8910 .get(connection_name)
8911 .ok_or_else(|| SourceError::Malformed {
8912 message: "GitHub dependency response is missing its connection".into(),
8913 })?;
8914 let nodes = connection
8915 .get("nodes")
8916 .and_then(Value::as_array)
8917 .ok_or_else(|| SourceError::Malformed {
8918 message: "GitHub dependency response nodes is not an array".into(),
8919 })?;
8920 // `from` depends on `to`, always. GitHub spells the same relationship from either
8921 // end — `blockedBy` lists what this item waits on, `blocking` lists what waits on
8922 // it — so the near item is `from` in one direction and `to` in the other.
8923 let items = nodes
8924 .iter()
8925 .map(|value| {
8926 let related = NativeId(required_str(value, "id")?.into());
8927 let related_kind = related_kind(value)?;
8928 let (from, to) = match direction {
8929 Direction::DependsOn => (
8930 DependencyEndpoint::from_native(id.clone(), near_kind),
8931 DependencyEndpoint::from_native(related, related_kind),
8932 ),
8933 Direction::DependedOnBy => (
8934 DependencyEndpoint::from_native(related, related_kind),
8935 DependencyEndpoint::from_native(id.clone(), near_kind),
8936 ),
8937 };
8938 Ok(DependencyEdge {
8939 from,
8940 to,
8941 kind: DependencyKind::Blocks,
8942 })
8943 })
8944 .collect::<Result<Vec<_>, SourceError>>()?;
8945 let mut next = next_cursor(connection)?;
8946 if let Some(next) = &next {
8947 validate_cursor_progress(cursor, &next.0)?;
8948 }
8949 // The recorded far ends ride on the last page of `blockedBy`, whose answer carried the
8950 // body they are recorded in, as far as the page has room — so they cost no request of
8951 // their own — and only what does not fit is resumed by a cursor.
8952 let mut items = items;
8953 if next.is_none() {
8954 let recorded = self
8955 .recorded_edges(
8956 id,
8957 near_kind,
8958 direction,
8959 natively_names,
8960 node,
8961 draft_slot.as_ref(),
8962 )
8963 .await?;
8964 let room = limit.saturating_sub(items.len());
8965 let total = recorded.len();
8966 items.extend(recorded.into_iter().take(room));
8967 if total > room {
8968 next = Some(Cursor(format!("{RECORDED_CURSOR}{room}")));
8969 }
8970 }
8971 Ok(Page { items, next })
8972 }
8973
8974 /// The edges this item records under [`DependencyEdge::RECORDED_KEY`], which is where
8975 /// a far end in another source has to live: no GitHub issue relationship can name one.
8976 ///
8977 /// Only forwards. The reverse of a recorded edge is derived from the far end, and this
8978 /// source never writes one down.
8979 ///
8980 /// The metadata lives in the item's own body slot, and `node` is the dependency read's
8981 /// own answer, which carries an issue's body — so an issue's recorded edges cost no
8982 /// request beyond the read already made, and reading the board for them would be a
8983 /// walk of every item for one field of one. A draft has no body in that answer, because
8984 /// a draft is not an issue, so a draft's are read off its own read by id — never off a
8985 /// listing of the board, which can be behind on the very item asked about.
8986 async fn recorded_edges(
8987 &self,
8988 id: &NativeId,
8989 near_kind: ItemKind,
8990 direction: Direction,
8991 natively_names: Option<ItemKind>,
8992 node: &Value,
8993 draft_slot: Option<&Option<BTreeMap<String, Value>>>,
8994 ) -> Result<Vec<DependencyEdge>, SourceError> {
8995 if direction != Direction::DependsOn {
8996 return Ok(Vec::new());
8997 }
8998 let slot = match (node.get("body"), draft_slot) {
8999 (Some(body), _) if natively_names.is_some() => {
9000 metadata_body(body.as_str().map(str::to_owned))?.1
9001 }
9002 // A draft the dependency read itself answered: this board's, or none of its.
9003 (_, Some(Some(slot))) => slot.clone(),
9004 (_, Some(None)) => return Ok(Vec::new()),
9005 _ => {
9006 let Some(item) = self.bound_item(id).await? else {
9007 return Ok(Vec::new());
9008 };
9009 item.slot
9010 }
9011 };
9012 DependencyEdge::recorded(&slot, id, near_kind, &self.name, natively_names)
9013 .map_err(|message| SourceError::Malformed { message })
9014 }
9015
9016 fn configured_repository(&self) -> Result<&RepositoryTarget, SourceError> {
9017 self.repository
9018 .as_ref()
9019 .ok_or_else(|| SourceError::Refused {
9020 message: format!(
9021 "source {} has no repository configured, and a GitHub Projects board has no \
9022 repository of its own to create an issue in; set repository: owner/name on \
9023 this source",
9024 self.name
9025 ),
9026 })
9027 }
9028
9029 /// The repository one new issue is created in, under the rule [`RepositoryTarget`]
9030 /// states.
9031 ///
9032 /// The fallback is demanded first, whichever arm answers: a write without a configured
9033 /// repository is refused naming the field exactly as it was before the rule existed,
9034 /// so a source that could not write before cannot write now, rather than writing for
9035 /// the one item whose own field happens to decide it.
9036 ///
9037 /// Everything this refuses is refused before `createIssue`, so a refusal leaves no
9038 /// issue behind: an entry that is not a repository on [`RepositoryTarget::HOST`], an
9039 /// entry owned by someone other than the owner of the parent issue's repository —
9040 /// GitHub accepts a sub-issue from another repository of the same owner and from no
9041 /// other, so `addSubIssue` would refuse it after the issue existed — a parent the
9042 /// board does not hold, and a parent that is a draft, which GitHub gives no sub-issues,
9043 /// both of which `addSubIssue` would likewise refuse too late. Whether the entry exists
9044 /// and is visible to the token is checked where its node id is resolved, still before
9045 /// `createIssue`. The parent is read by its own id through [`Self::item_by_id`] — never
9046 /// looked up in a listing of the board, which can be minutes behind an issue its own
9047 /// `projectItems` already places on it — and that read answers first from this process's
9048 /// own record, so a project created moments ago in this command answers though GitHub
9049 /// has not caught up.
9050 async fn creation_target(
9051 &self,
9052 incoming: &Incoming<'_>,
9053 ) -> Result<RepositoryTarget, SourceError> {
9054 let fallback = self.configured_repository()?;
9055 let what = |incoming: &Incoming<'_>| {
9056 format!(
9057 "{} {:?}",
9058 incoming.written.kind().describes(),
9059 incoming.title
9060 )
9061 };
9062 let parent = match incoming.parent {
9063 Some(parent) => Some(self.bound_item(parent).await?.ok_or_else(|| {
9064 SourceError::Refused {
9065 message: format!(
9066 "GitHub project issue {} was not found on the board of source {}, so {} \
9067 cannot be filed under it",
9068 parent.0,
9069 self.name,
9070 what(incoming)
9071 ),
9072 }
9073 })?),
9074 None => None,
9075 };
9076 let parents_repository = parent
9077 .as_ref()
9078 .map(|parent| {
9079 // A draft is on the board and so is found, but it has no repository to
9080 // place a task in and GitHub gives it no sub-issues, so `addSubIssue`
9081 // would refuse the task only once `createIssue` had made it.
9082 if parent.content_kind == ContentKind::DraftIssue {
9083 return Err(SourceError::Refused {
9084 message: format!(
9085 "GitHub project item {} on the board of source {} is a draft, \
9086 which cannot have sub-issues, so {} cannot be filed under it",
9087 parent.id.0,
9088 self.name,
9089 what(incoming)
9090 ),
9091 });
9092 }
9093 // An issue's repository is where a sub-issue is placed and whose owner it
9094 // is compared against, so a parent whose repository this source cannot
9095 // spell as `owner/name` — GitHub's login grammar is wider than this
9096 // source's floor — is one nothing can be filed under.
9097 parent
9098 .own_repository
9099 .as_ref()
9100 .and_then(|origin| RepositoryTarget::from_origin(origin).ok())
9101 .ok_or_else(|| SourceError::Malformed {
9102 message: format!(
9103 "GitHub project issue {} on the board of source {} is in {}, which \
9104 is not a {}/owner/name repository this source can place {} in",
9105 parent.id.0,
9106 self.name,
9107 parent
9108 .own_repository
9109 .as_ref()
9110 .map_or("no repository", Repository::as_str),
9111 RepositoryTarget::HOST,
9112 what(incoming)
9113 ),
9114 })
9115 })
9116 .transpose()?;
9117 match incoming.repositories {
9118 [named] => {
9119 let target =
9120 RepositoryTarget::from_origin(named).map_err(|_| SourceError::Refused {
9121 message: format!(
9122 "{} names repository {}, which is not a {}/owner/name repository \
9123 source {} can create an issue in; name one that is, or name none",
9124 what(incoming),
9125 named.as_str(),
9126 RepositoryTarget::HOST,
9127 self.name
9128 ),
9129 })?;
9130 if let Some(parents) = &parents_repository
9131 && parents.owner != target.owner
9132 {
9133 return Err(SourceError::Refused {
9134 message: format!(
9135 "{} names repository {}, owned by {}, but its project's issue is in \
9136 {}, owned by {}, and GitHub files a sub-issue only in a repository \
9137 of the same owner as its parent issue; name a repository of {}, or \
9138 name none",
9139 what(incoming),
9140 target.slug(),
9141 target.owner,
9142 parents.slug(),
9143 parents.owner,
9144 parents.owner
9145 ),
9146 });
9147 }
9148 Ok(target)
9149 }
9150 _ => Ok(parents_repository.unwrap_or_else(|| fallback.clone())),
9151 }
9152 }
9153
9154 /// The node id of the repository `incoming` is being created in, or the refusal naming
9155 /// the item and the repository the token cannot see.
9156 ///
9157 /// Resolved once per command per repository; see [`Self::repository_cache`].
9158 async fn repository_id(
9159 &self,
9160 repository: &RepositoryTarget,
9161 incoming: &Incoming<'_>,
9162 ) -> Result<String, SourceError> {
9163 if let Some(id) = self.repository_cache()?.get(repository).cloned() {
9164 return Ok(id);
9165 }
9166 let data = self
9167 .graphql(
9168 graphql::REPOSITORY,
9169 json!({"owner":repository.owner,"name":repository.name}),
9170 )
9171 .await?;
9172 self.repository_read(&data, repository, incoming)
9173 }
9174
9175 /// The repository's node id out of an answer carrying the `repository` root, held for
9176 /// the rest of this command, or the refusal naming the item that cannot be created in it.
9177 fn repository_read(
9178 &self,
9179 data: &Value,
9180 repository: &RepositoryTarget,
9181 incoming: &Incoming<'_>,
9182 ) -> Result<String, SourceError> {
9183 let node = data
9184 .get("repository")
9185 .filter(|value| !value.is_null())
9186 .ok_or_else(|| SourceError::Refused {
9187 message: format!(
9188 "GitHub repository {} was not found or is not visible to the token, so {} \
9189 {:?} cannot be created in it",
9190 repository.slug(),
9191 incoming.written.kind().describes(),
9192 incoming.title
9193 ),
9194 })?;
9195 let id = required_str(node, "id")?.to_owned();
9196 self.repository_cache()?
9197 .insert(repository.clone(), id.clone());
9198 Ok(id)
9199 }
9200
9201 fn repository_cache(
9202 &self,
9203 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<RepositoryTarget, String>>, SourceError> {
9204 self.repository_cache
9205 .lock()
9206 .map_err(|_| SourceError::Unavailable {
9207 message: "this source's record of the destination repository was left \
9208 inconsistent by an earlier failure; next: run the command again"
9209 .into(),
9210 })
9211 }
9212
9213 /// Create or update one board item, whichever kind it is.
9214 async fn write_item(
9215 &self,
9216 incoming: &Incoming<'_>,
9217 target: Option<&NativeId>,
9218 depends_on: &[DependencyEdge],
9219 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
9220 let prepared = self.prepare_write(incoming, target, depends_on).await?;
9221 let landed = match &prepared.destination {
9222 // Its content is written last, by `complete_write`, once everything else has landed.
9223 WriteDestination::Existing(item) => Landed {
9224 content_id: item.id.clone(),
9225 item_id: Some(item.item_id.clone()),
9226 url: item.url.clone(),
9227 number: item.number,
9228 },
9229 WriteDestination::New(target) => {
9230 if self.deferring()? {
9231 self.create_filed_issue(
9232 prepared.board.id.as_str(),
9233 target,
9234 incoming,
9235 &prepared.body,
9236 )
9237 .await?
9238 } else {
9239 self.create_and_file_issue(
9240 prepared.board.id.as_str(),
9241 target,
9242 incoming,
9243 &prepared.body,
9244 )
9245 .await?
9246 }
9247 }
9248 };
9249 self.complete_write(incoming, prepared, landed).await
9250 }
9251
9252 /// Everything one write of `incoming` reads and decides before it sends anything: what is
9253 /// there now, where a new item is created, the board's fields, and every value refused while
9254 /// nothing has been written — the item's body included.
9255 async fn prepare_write(
9256 &self,
9257 incoming: &Incoming<'_>,
9258 target: Option<&NativeId>,
9259 depends_on: &[DependencyEdge],
9260 ) -> Result<Prepared, SourceError> {
9261 // Refused before anything is read or written: a task or a project titled the way
9262 // this board spells a document would land as an issue this same source reads back
9263 // as a document, so the field this destination cannot carry is named rather than
9264 // written and silently reclassified.
9265 if let Written::Work(kind, _) = incoming.written
9266 && incoming.title.starts_with(DESIGN_TITLE_PREFIX)
9267 {
9268 return Err(SourceError::Refused {
9269 message: format!(
9270 "the title of this {} begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
9271 spells a document, so it would read back as one rather than as a {}; \
9272 retitle it, or copy it as a document",
9273 kind.marker(),
9274 self.name,
9275 kind.marker()
9276 ),
9277 });
9278 }
9279 // The destination is read by its own id, and whether this board holds it is decided
9280 // by that read — its own `projectItems` — rather than by whether a listing of the
9281 // board happens to include it yet. See the module documentation.
9282 // An existing issue is never moved; a new one is created where the rule says — and
9283 // knowing where is what lets the board's fields and that repository's id be read
9284 // together, before anything below needs either.
9285 let destination = match target {
9286 Some(target) => {
9287 WriteDestination::Existing(Box::new(self.bound_item(target).await?.ok_or_else(
9288 || SourceError::Refused {
9289 message: format!("GitHub destination item {} was not found", target.0),
9290 },
9291 )?))
9292 }
9293 None => {
9294 let target = self.creation_target(incoming).await?;
9295 self.creation_context(&target, incoming).await?;
9296 WriteDestination::New(target)
9297 }
9298 };
9299 let existing = destination.existing();
9300 let board = self
9301 .fields_for(
9302 existing,
9303 incoming.written.status().is_some(),
9304 incoming
9305 .priority
9306 .is_some_and(|priority| priority != Priority::None),
9307 )
9308 .await?;
9309 let status_target = incoming
9310 .written
9311 .work_status()
9312 .map(|(kind, status)| self.resolved_target(kind, status.category))
9313 .transpose()?;
9314 let column = match (incoming.written.work_status(), status_target.as_ref()) {
9315 (Some((kind, status)), Some(target)) => {
9316 self.column_for(&board.fields, kind, status.category, target)?
9317 }
9318 _ => None,
9319 };
9320 // Resolved before anything is created, for the reason the column above is: a
9321 // priority this board has no option for is refused while nothing has been written.
9322 let priority_write = match incoming.priority {
9323 Some(priority) => self.priority_write(&board.fields, existing, priority)?,
9324 None => None,
9325 };
9326 // And for the reason the priority is: a value no projected field can hold, or a board
9327 // that cannot hold the field, is refused while nothing has been written.
9328 let (projections, projected) = self.projection_writes(
9329 &board.fields,
9330 existing.map(|item| &item.projected),
9331 incoming.metadata,
9332 None,
9333 )?;
9334 let content_kind = existing.map_or(ContentKind::Issue, |item| item.content_kind);
9335 if content_kind == ContentKind::DraftIssue {
9336 if let (Some(StatusTarget::Terminal(_, _)), Some(status)) =
9337 (status_target.as_ref(), incoming.written.status())
9338 {
9339 return Err(self.closes_a_draft(status.category));
9340 }
9341 if incoming.parent.is_some() {
9342 return Err(SourceError::Refused {
9343 message: "GitHub draft items cannot be a project's sub-issue".into(),
9344 });
9345 }
9346 }
9347 match existing {
9348 Some(item) if content_kind == ContentKind::Issue => {
9349 if item.labels != incoming.labels {
9350 return Err(SourceError::Refused {
9351 message: "GitHub issue labels differ from the labels being written".into(),
9352 });
9353 }
9354 }
9355 _ => {
9356 if !incoming.labels.is_empty() {
9357 return Err(SourceError::Refused {
9358 message: "GitHub items created by this destination carry no labels".into(),
9359 });
9360 }
9361 }
9362 }
9363
9364 // The repository the issue really lives in is what the slot below is written against,
9365 // so a single entry that is where the issue is created travels as no key at all, and
9366 // the read side derives it back from the issue.
9367 let own_repository = match &destination {
9368 WriteDestination::Existing(item) => item.own_repository.clone(),
9369 WriteDestination::New(target) => Some(
9370 Repository::try_from(target.origin())
9371 .map_err(|message| SourceError::Config { message })?,
9372 ),
9373 };
9374 let (native, fallback) = self
9375 .partition_edges(
9376 incoming.written.kind(),
9377 content_kind,
9378 existing.and_then(|item| item.blocked_by.as_deref()),
9379 depends_on,
9380 )
9381 .await?;
9382 let mut slot = slot_metadata(incoming, own_repository.as_ref(), &fallback);
9383 let content = match incoming.assets {
9384 Some(assets) => {
9385 let uploads = self.upload_assets(own_repository.as_ref(), assets).await?;
9386 let rewritten = onetaskgraph_plugin_api::serve_asset_references(
9387 incoming.content.unwrap_or_default(),
9388 &mut slot,
9389 &uploads,
9390 );
9391 incoming.content.map(|_| rewritten)
9392 }
9393 None => incoming.content.map(str::to_owned),
9394 };
9395 let body = compose_body(content.as_deref(), &slot)?;
9396 // Read before anything is created, for the reason the field below is: a value
9397 // this destination cannot store has to refuse, and refusing after `createIssue`
9398 // would leave an issue behind that nothing asked for. The engine writes a
9399 // qualified id here; a caller handing this key anything else is told so rather
9400 // than having it silently stored as no origin at all.
9401 // 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.
9402 let origin = match incoming.metadata.get(ORIGIN_KEY) {
9403 None => "",
9404 Some(Value::String(origin)) => origin.as_str(),
9405 Some(other) => {
9406 return Err(SourceError::Refused {
9407 message: format!(
9408 "{ORIGIN_KEY} holds a qualified id spelled as a string, and this item's \
9409 is {other}"
9410 ),
9411 });
9412 }
9413 };
9414 // Resolved before anything is created: a board that cannot carry the copy origin
9415 // has to refuse the write, and refusing it after `createIssue` would leave an
9416 // issue behind that nothing asked for.
9417 let origin_field = match Board::field(&board.fields, ORIGIN_FIELD)? {
9418 Some(field) => {
9419 if required_str(field, "__typename")? != "ProjectV2Field" {
9420 return Err(SourceError::Refused {
9421 message: format!(
9422 "GitHub board source-owned {ORIGIN_FIELD} field is not a text field"
9423 ),
9424 });
9425 }
9426 Some(required_str(field, "id")?.to_owned())
9427 }
9428 None if incoming.metadata.contains_key(ORIGIN_KEY) => {
9429 return Err(SourceError::Refused {
9430 message: format!(
9431 "GitHub board has no source-owned {ORIGIN_FIELD} text field, and the \
9432 item carries {ORIGIN_KEY}; add a text field named {ORIGIN_FIELD} to \
9433 the board"
9434 ),
9435 });
9436 }
9437 None => None,
9438 };
9439
9440 Ok(Prepared {
9441 origin: origin.to_owned(),
9442 destination,
9443 board,
9444 status_target,
9445 column,
9446 priority_write,
9447 projections,
9448 projected,
9449 content_kind,
9450 own_repository,
9451 native,
9452 slot,
9453 content,
9454 body,
9455 origin_field,
9456 })
9457 }
9458
9459 /// Everything one write does once the item it writes exists: its board fields, its
9460 /// relationships and — for an item that was already there — its content last, then what this
9461 /// process remembers of it.
9462 ///
9463 /// While this source is holding writes back — see [`TaskSource::begin_deferred_writes`] — an
9464 /// item this command created has its fields, its closing and its edges held for
9465 /// [`TaskSource::finish_writes`] rather than sent here.
9466 async fn complete_write(
9467 &self,
9468 incoming: &Incoming<'_>,
9469 prepared: Prepared,
9470 landed: Landed,
9471 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
9472 let Prepared {
9473 destination,
9474 board,
9475 status_target,
9476 column,
9477 priority_write,
9478 projections,
9479 projected,
9480 content_kind,
9481 own_repository,
9482 native,
9483 slot,
9484 content,
9485 body,
9486 origin,
9487 origin_field,
9488 } = prepared;
9489 let existing = destination.existing();
9490 let origin = origin.as_str();
9491 let Landed {
9492 content_id,
9493 item_id,
9494 url,
9495 number,
9496 } = landed;
9497
9498 // Only what differs from the item as this write read it is sent: a priority the item
9499 // already holds is not, so an unchanged re-copy writes no field and a person's edit of
9500 // it since that read is left alone.
9501 let priority_write = priority_write.filter(|_| {
9502 !existing.is_some_and(|item| {
9503 incoming.priority.map(HeldPriority::Read) == Some(item.priority.clone())
9504 })
9505 });
9506 let written_option = column.as_ref().map(|(_, _, name)| name.clone());
9507 let column = column
9508 .filter(|(_, _, name)| existing.is_none_or(|item| item.option.as_ref() != Some(name)))
9509 .map(|(field, option, _)| (field, option));
9510 // Creating an item here is several calls — `createIssue`, which files it on the
9511 // board, then its board fields, the parent and the dependencies — and GitHub can fail
9512 // at any of them. Everything this source can refuse *before* the first of those is
9513 // already checked above, so what is left is GitHub itself failing part way. When it
9514 // does over an item this call created, the issue is taken back: a write that
9515 // refused must not leave an item behind that nobody asked for, and one that does
9516 // makes the retry create a second.
9517 // Whether the board-field write carrying a moved origin was answered as landing whole.
9518 // When it was refused, GitHub does not say which of its fields ran before the one that
9519 // failed, so the origin may or may not have moved.
9520 let mut origin_landed = false;
9521 // An item this command created while writes are held back has every part that can land
9522 // later held for `finish_writes` — its own create filed it on the board and under its
9523 // project already — and so does a later write of it in the same command.
9524 let holding = (existing.is_none() && self.deferring()?) || self.holds_back(&content_id)?;
9525 let landed = if holding {
9526 self.hold_back(
9527 &board.id,
9528 &content_id,
9529 existing,
9530 origin_field.as_deref(),
9531 origin,
9532 column,
9533 status_target.as_ref(),
9534 priority_write.as_ref(),
9535 &projections,
9536 &native,
9537 )
9538 } else if let Some(item_id) = item_id.as_deref() {
9539 self.finish_write(
9540 board.id.as_str(),
9541 incoming,
9542 &content_id,
9543 item_id,
9544 content_kind,
9545 existing,
9546 origin_field.as_deref(),
9547 origin,
9548 column,
9549 status_target.as_ref(),
9550 priority_write.as_ref(),
9551 &projections,
9552 &native,
9553 &mut origin_landed,
9554 )
9555 .await
9556 } else {
9557 // An issue a create filed on the board itself has no board item this process
9558 // knows until `finish_writes` reads it, so nothing but a held-back write may land
9559 // on it before then.
9560 Err(SourceError::Malformed {
9561 message: format!(
9562 "issue {} was written before the board item GitHub filed it as was read",
9563 content_id.0
9564 ),
9565 })
9566 };
9567 // An existing item's title, body and state go last, in one `updateIssue`, once its board
9568 // fields and its relationships have landed: a refusal of any of those then leaves its
9569 // body — and the metadata slot inside it — exactly as it stood.
9570 let landed = match (landed, existing) {
9571 (Ok(()), Some(item)) => {
9572 self.update_existing(item, incoming, &body, status_target.as_ref())
9573 .await
9574 }
9575 (landed, _) => landed,
9576 };
9577 if let Err(error) = landed {
9578 match existing {
9579 // Best effort, and the write's own failure is what the caller is told: a
9580 // refusal naming the tidy-up would hide why the write failed at all.
9581 None => {
9582 let _ = self.delete_issue(&content_id).await;
9583 }
9584 // The origin field is the one piece of an existing item's metadata written
9585 // before its body, so a write refused after it puts it back as it was. When
9586 // that is refused too, the write's own failure is still what the caller is
9587 // told — with what it left behind added, because the item's metadata is then
9588 // not as it stood and a caller retrying has to know which key moved.
9589 Some(item) => {
9590 let before = item.origin.as_deref().unwrap_or("");
9591 if let Some(field) = origin_field.as_deref()
9592 && before != origin
9593 && let Err(restore) = self
9594 .set_item_field(
9595 board.id.as_str(),
9596 &item.item_id,
9597 field,
9598 json!({"text": before}),
9599 )
9600 .await
9601 {
9602 let left = if origin_landed {
9603 format!(
9604 "its {ORIGIN_KEY} was moved to {origin:?} before that and could \
9605 not be put back to {before:?} ({restore}), so item {} still \
9606 holds {origin:?} there",
9607 item.id.0
9608 )
9609 } else {
9610 format!(
9611 "the refused write carried its {ORIGIN_KEY} from {before:?} to \
9612 {origin:?}, GitHub does not say whether that part of it ran, \
9613 and putting it back to {before:?} was refused ({restore}), so \
9614 item {} holds {origin:?} or {before:?} there",
9615 item.id.0
9616 )
9617 };
9618 return Err(noting(
9619 error,
9620 &format!(
9621 "; {left}; next: set {ORIGIN_KEY} on it back to {before:?}, or \
9622 run the write again"
9623 ),
9624 ));
9625 }
9626 }
9627 }
9628 return Err(error);
9629 }
9630
9631 let written_status = match (incoming.written.work_status(), status_target.as_ref()) {
9632 (Some((kind, _)), Some(StatusTarget::Terminal(_, reason))) => {
9633 self.statuses
9634 .status(kind, written_option.as_deref(), true, Some(reason.reason()))
9635 }
9636 (Some((kind, _)), Some(StatusTarget::Column(_))) => {
9637 self.statuses
9638 .status(kind, written_option.as_deref(), false, None)
9639 }
9640 (Some((_, status)), _) => status.clone(),
9641 (None, _) => Status {
9642 category: StatusCategory::Unknown,
9643 name: "Open".to_owned(),
9644 },
9645 };
9646
9647 // So the rest of this command reads what it just did rather than what the board
9648 // said before it. See `remember_written` for which half takes it.
9649 let remembered = Resolved {
9650 // Empty for an issue whose board item is not read yet: `finish_writes` reads it
9651 // and `learn_item` puts it here.
9652 // llmlint: ignore[invalid_states_unrepresentable] `Resolved` is serialised into the cache's stored write context, so its field type is a stored shape this change may not move; the empty id lives only until `finish_writes` reads the board item in this same command, and nothing addresses it before then — `complete_write` refuses any write naming no board item.
9653 item_id: item_id.unwrap_or_default(),
9654 id: content_id.clone(),
9655 content_kind,
9656 kind: incoming.written.kind(),
9657 title: incoming.title.to_owned(),
9658 // The visible half of the body this write composed, split back off it the
9659 // way a read splits it — so what this record reports is what a read of the
9660 // same issue reports, rather than the person's text with the metadata slot
9661 // still on the end of it.
9662 body: metadata_body(body.clone())?.0,
9663 raw_body: body.clone(),
9664 // A document has no status of its own; what it reads back as is whatever
9665 // the issue's own state says, which is what a re-read reports.
9666 status: written_status,
9667 option: written_option.or_else(|| existing.and_then(|item| item.option.clone())),
9668 priority: match incoming.priority {
9669 Some(priority) => HeldPriority::Read(priority),
9670 None => existing.map_or(HeldPriority::Read(Priority::None), |item| {
9671 item.priority.clone()
9672 }),
9673 },
9674 // What `state_input` asked for: closed for a terminal target, open for any other
9675 // status, and the issue's own state left as it was by a document write.
9676 closed: content_kind == ContentKind::Issue
9677 && match status_target.as_ref() {
9678 Some(StatusTarget::Terminal(_, _)) => true,
9679 Some(_) => false,
9680 None => existing.is_some_and(|item| item.closed),
9681 },
9682 reason: match status_target.as_ref() {
9683 Some(StatusTarget::Terminal(_, reason)) if content_kind == ContentKind::Issue => {
9684 Some(reason.reason().to_owned())
9685 }
9686 Some(_) => None,
9687 None => existing.and_then(|item| item.reason.clone()),
9688 },
9689 delivers: incoming.delivers.to_vec(),
9690 delivered_by: incoming.delivered_by.to_vec(),
9691 labels: incoming.labels.to_vec(),
9692 parent: incoming.parent.cloned(),
9693 origin: (!origin.is_empty()).then(|| origin.to_owned()),
9694 projected,
9695 number,
9696 // In the update path this is the item's own url, read off `existing` where the
9697 // record above was bound, so one expression serves both halves.
9698 url,
9699 created_at: existing.and_then(|item| item.created_at),
9700 updated_at: existing.and_then(|item| item.updated_at),
9701 // What this write moved is GitHub's to say, so a record of it gives no token until
9702 // it is read again.
9703 item_updated_at: None,
9704 comment_count: None,
9705 relationship_events: None,
9706 served_at: None,
9707 facets: None,
9708 own_repository,
9709 repositories: incoming.repositories.to_vec(),
9710 classification: incoming.classification,
9711 slot,
9712 board_id: Some(board.id.as_str().to_owned()),
9713 fields: board
9714 .fields
9715 .get("nodes")
9716 .and_then(Value::as_array)
9717 .cloned()
9718 .unwrap_or_default(),
9719 board_fields: Some(board.fields.clone()),
9720 // What this write left the relationship holding is known by id alone, and a
9721 // later read of its edges needs each far end's kind, so it reads them again.
9722 blocked_by: None,
9723 };
9724 self.remember_written(remembered, existing.is_none())?;
9725 Ok(onetaskgraph_plugin_api::AssetsWritten {
9726 id: content_id,
9727 content,
9728 })
9729 }
9730
9731 /// Everything a write does after the item exists: its board fields, its parent, and
9732 /// its dependencies.
9733 ///
9734 /// Split out of `write_item` so there is one place a failure past the point of no
9735 /// return is caught, rather than a tidy-up repeated at each `?` above.
9736 // llmlint: ignore[suppressions_justified] This is the tail of `write_item` lifted out
9737 // so there is one place a failure past the point of no return is caught, and its
9738 // arguments are exactly the values that tail already had in scope. Bundling them into a
9739 // struct would describe no concept — it would be "the arguments of this function" — and
9740 // would put the whole of `write_item`'s locals behind one more indirection.
9741 #[allow(clippy::too_many_arguments)]
9742 async fn finish_write(
9743 &self,
9744 board_id: &str,
9745 incoming: &Incoming<'_>,
9746 content_id: &NativeId,
9747 item_id: &str,
9748 content_kind: ContentKind,
9749 existing: Option<&Resolved>,
9750 origin_field: Option<&str>,
9751 origin: &str,
9752 column: Option<(String, String)>,
9753 status_target: Option<&StatusTarget>,
9754 priority: Option<&PriorityWrite>,
9755 projections: &[ProjectionWrite],
9756 native: &[String],
9757 origin_landed: &mut bool,
9758 ) -> Result<(), SourceError> {
9759 let (fields, clears) = field_writes(
9760 existing,
9761 origin_field,
9762 origin,
9763 column,
9764 priority,
9765 projections,
9766 );
9767 self.set_item_fields(board_id, item_id, &fields, &clears)
9768 .await?;
9769 *origin_landed = true;
9770
9771 // An existing issue closes in the `updateIssue` its write ends with; one created just
9772 // now closes here, once its option is selected.
9773 if existing.is_none()
9774 && content_kind == ContentKind::Issue
9775 && matches!(status_target, Some(StatusTarget::Terminal(_, _)))
9776 {
9777 self.update_content(
9778 ContentKind::Issue,
9779 content_id,
9780 json!({"stateInput":state_input(status_target)}),
9781 )
9782 .await?;
9783 }
9784
9785 if content_kind == ContentKind::Issue {
9786 self.reparent(
9787 existing.and_then(|item| item.parent.clone()),
9788 content_id,
9789 incoming.parent,
9790 )
9791 .await?;
9792 // A document takes part in no dependency graph, so writing one neither reads
9793 // nor changes the issue's own `blockedBy` relationships. Reconciling them
9794 // against the empty list a document write carries would *delete* whatever
9795 // relationships a person had made on that issue, which is a write nobody
9796 // asked for.
9797 if incoming.written.kind() != BoardKind::Document {
9798 let issue = match existing {
9799 Some(item) => Issue::Existing(item.blocked_by.as_deref()),
9800 None => Issue::Created,
9801 };
9802 self.reconcile_blocked_by(content_id, native, issue).await?;
9803 }
9804 }
9805 Ok(())
9806 }
9807
9808 /// Whether this source is holding writes back — see [`TaskSource::begin_deferred_writes`].
9809 fn deferring(&self) -> Result<bool, SourceError> {
9810 Ok(self.deferred()?.is_some())
9811 }
9812
9813 /// Whether `content` is an item this command created whose parts are held back.
9814 fn holds_back(&self, content: &NativeId) -> Result<bool, SourceError> {
9815 Ok(self
9816 .deferred()?
9817 .as_ref()
9818 .is_some_and(|deferred| deferred.holds(content)))
9819 }
9820
9821 fn deferred(&self) -> Result<std::sync::MutexGuard<'_, Option<Deferred>>, SourceError> {
9822 self.deferred.lock().map_err(|_| SourceError::Unavailable {
9823 message: "this source's record of the writes it is holding back was left \
9824 inconsistent by an earlier failure; next: run the command again"
9825 .into(),
9826 })
9827 }
9828
9829 /// Creates one issue filed on the board and under its project by the `createIssue` itself —
9830 /// `projectV2Ids` naming the board and `parentIssueId` the project — which is what a copy
9831 /// creates with: one request where adding it to the board and filing it under its project
9832 /// were two more. GitHub adds the board item after answering, so its id is not known here;
9833 /// [`TaskSource::finish_writes`] reads it.
9834 async fn create_filed_issue(
9835 &self,
9836 board_id: &str,
9837 repository: &RepositoryTarget,
9838 incoming: &Incoming<'_>,
9839 body: &Option<String>,
9840 ) -> Result<Landed, SourceError> {
9841 let repository_id = self.repository_id(repository, incoming).await?;
9842 let data = self
9843 .graphql(
9844 graphql::CREATE_ISSUE,
9845 json!({"input": filed_input(&repository_id, board_id, incoming, body)}),
9846 )
9847 .await?;
9848 let created = data
9849 .pointer("/createIssue/issue")
9850 .filter(|value| !value.is_null())
9851 .ok_or_else(|| SourceError::Malformed {
9852 message: "GitHub issue creation returned no issue".into(),
9853 })?;
9854 self.created_landed(created).await
9855 }
9856
9857 /// Keep the address `createIssue` answered for the issue `content`, for
9858 /// [`TaskSource::written_location`].
9859 fn note_written(&self, content: &NativeId, url: Option<&str>) {
9860 if let Some(url) = url {
9861 self.written
9862 .lock()
9863 .unwrap_or_else(std::sync::PoisonError::into_inner)
9864 .insert(content.clone(), url.to_owned());
9865 }
9866 }
9867
9868 /// What one `createIssue` answered, as the item it created — or, when its number cannot be
9869 /// read, the refusal, after taking the issue back.
9870 async fn created_landed(&self, created: &Value) -> Result<Landed, SourceError> {
9871 let content_id = NativeId(required_nonblank_str(created, "id")?.to_owned());
9872 let url = optional_str(created, "url")?.map(str::to_owned);
9873 let number = match created_issue_number(created) {
9874 Ok(number) => number,
9875 Err(error) => {
9876 let _ = self.delete_issue(&content_id).await;
9877 return Err(error);
9878 }
9879 };
9880 self.note_written(&content_id, url.as_deref());
9881 Ok(Landed {
9882 content_id,
9883 item_id: None,
9884 url,
9885 number,
9886 })
9887 }
9888
9889 /// Hold back everything a write of the created item `content` would send after creating it,
9890 /// for [`TaskSource::finish_writes`]: the same field values, closing and edges
9891 /// `finish_write` sends, the parent excepted, which its create already named. A later write
9892 /// of the same item replaces what it changes.
9893 // llmlint: ignore[suppressions_justified] The arguments are `finish_write`'s, which this holds back instead of sending, and they are exactly the values `complete_write` has in scope; bundling them would describe no concept.
9894 #[allow(clippy::too_many_arguments)]
9895 fn hold_back(
9896 &self,
9897 board_id: &BoardId,
9898 content: &NativeId,
9899 existing: Option<&Resolved>,
9900 origin_field: Option<&str>,
9901 origin: &str,
9902 column: Option<(String, String)>,
9903 status_target: Option<&StatusTarget>,
9904 priority: Option<&PriorityWrite>,
9905 projections: &[ProjectionWrite],
9906 native: &[String],
9907 ) -> Result<(), SourceError> {
9908 let status = column.map(|(field, option)| {
9909 let name = self
9910 .fields_cache()
9911 .ok()
9912 .and_then(|fields| {
9913 fields.as_ref().and_then(|board| {
9914 option_name(&board.fields, &field, &option).map(str::to_owned)
9915 })
9916 })
9917 .unwrap_or_default();
9918 (field, option, name)
9919 });
9920 let (fields, clears) =
9921 field_writes(existing, origin_field, origin, None, priority, projections);
9922 let close = match status_target {
9923 Some(StatusTarget::Terminal(_, reason)) => Some(*reason),
9924 _ => None,
9925 };
9926 let mut deferred = self.deferred()?;
9927 let deferred = deferred.get_or_insert_with(Deferred::default);
9928 if let Deferred::Nothing = deferred {
9929 *deferred = Deferred::Held {
9930 board: board_id.clone(),
9931 created: Vec::new(),
9932 };
9933 }
9934 let Deferred::Held { created, .. } = deferred else {
9935 unreachable!("held back on the line above");
9936 };
9937 let held = match created.iter().position(|held| held.content == *content) {
9938 Some(at) => &mut created[at],
9939 None => {
9940 created.push(HeldBack {
9941 content: content.clone(),
9942 fields: Vec::new(),
9943 status: None,
9944 clears: Vec::new(),
9945 close: None,
9946 edges: Vec::new(),
9947 });
9948 created.last_mut().expect("just held")
9949 }
9950 };
9951 for (field, value) in fields {
9952 held.fields.retain(|(held, _)| *held != field);
9953 held.clears.retain(|held| *held != field);
9954 held.fields.push((field, value));
9955 }
9956 for field in clears {
9957 held.fields.retain(|(held, _)| held != field);
9958 if !held.clears.iter().any(|held| held == field) {
9959 held.clears.push(field.to_owned());
9960 }
9961 }
9962 if status.is_some() {
9963 held.status = status;
9964 }
9965 if close.is_some() {
9966 held.close = close;
9967 }
9968 // Nothing of an item created in this command has been sent but its create, so the edges
9969 // it is to have are exactly the ones its latest write names.
9970 held.edges = native.iter().map(|far| NativeId(far.clone())).collect();
9971 Ok(())
9972 }
9973
9974 /// Create every task of `writes`, together, filed on the board and under each one's
9975 /// project: [`graphql::CREATE_ISSUES`], [`MUTATION_BATCH`] to a request.
9976 ///
9977 /// Each is prepared as [`Self::write_item`] prepares one — every refusal a write owes made
9978 /// before the first request — then created, then completed with its parts held back. A
9979 /// request refused part way leaves the issues it did create, which are taken back, with the
9980 /// rest of this call's, before the refusal is answered.
9981 async fn create_together(
9982 &self,
9983 writes: &[ItemWrite<Task>],
9984 ) -> Result<Vec<NativeId>, SourceError> {
9985 let incomings: Vec<Incoming<'_>> = writes
9986 .iter()
9987 .map(|write| self.task_incoming(write, None))
9988 .collect::<Result<_, _>>()?;
9989 let mut prepared = Vec::with_capacity(writes.len());
9990 for (incoming, write) in incomings.iter().zip(writes) {
9991 if write.target.is_some() {
9992 return Err(SourceError::Malformed {
9993 message: "a write naming its target was handed to a batch of creates".into(),
9994 });
9995 }
9996 prepared.push(
9997 self.prepare_write(incoming, None, &write.depends_on)
9998 .await?,
9999 );
10000 }
10001 let mut inputs = Vec::with_capacity(prepared.len());
10002 for (incoming, prepared) in incomings.iter().zip(&prepared) {
10003 let WriteDestination::New(target) = &prepared.destination else {
10004 return Err(SourceError::Malformed {
10005 message: "an update was decided for a write handed to a batch of creates"
10006 .into(),
10007 });
10008 };
10009 let repository_id = self.repository_id(target, incoming).await?;
10010 inputs.push(filed_input(
10011 &repository_id,
10012 prepared.board.id.as_str(),
10013 incoming,
10014 &prepared.body,
10015 ));
10016 }
10017 let mut landed: Vec<Landed> = Vec::with_capacity(inputs.len());
10018 let refused = 'create: {
10019 for chunk in inputs.chunks(MUTATION_BATCH) {
10020 let mut variables = serde_json::Map::new();
10021 for slot in 0..MUTATION_BATCH {
10022 variables.insert(
10023 format!("create{slot}"),
10024 chunk.get(slot).cloned().unwrap_or_else(|| chunk[0].clone()),
10025 );
10026 variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10027 }
10028 let data = match self
10029 .graphql(graphql::CREATE_ISSUES, Value::Object(variables))
10030 .await
10031 {
10032 Ok(data) => data,
10033 Err(error) => break 'create Some(error),
10034 };
10035 let mut refusal = data.get(ANSWER_ERRORS).map(|errors| {
10036 let said: Vec<&str> = errors
10037 .as_array()
10038 .into_iter()
10039 .flatten()
10040 .filter_map(|error| error.get("message").and_then(Value::as_str))
10041 .collect();
10042 SourceError::Refused {
10043 message: if said.is_empty() {
10044 "GitHub returned GraphQL errors".to_owned()
10045 } else {
10046 said.join("; ")
10047 },
10048 }
10049 });
10050 for slot in 0..chunk.len() {
10051 match data
10052 .pointer(&format!("/create{slot}/issue"))
10053 .filter(|value| !value.is_null())
10054 {
10055 Some(created) => match self.created_landed(created).await {
10056 Ok(created) => landed.push(created),
10057 Err(error) => {
10058 refusal.get_or_insert(error);
10059 }
10060 },
10061 // An alias GitHub refused answers nothing, and the refusal says why.
10062 None => {
10063 refusal.get_or_insert(SourceError::Malformed {
10064 message: "GitHub issue creation returned no issue".into(),
10065 });
10066 }
10067 }
10068 }
10069 if let Some(error) = refusal {
10070 break 'create Some(error);
10071 }
10072 }
10073 None
10074 };
10075 if let Some(error) = refused {
10076 // None of these ids reaches the caller, so an issue GitHub will not take back is
10077 // named here or by nobody.
10078 let ids: Vec<&NativeId> = landed.iter().map(|created| &created.content_id).collect();
10079 let left = self.delete_issues(&ids).await;
10080 let left: Vec<&str> = left.iter().map(|id| id.0.as_str()).collect();
10081 return Err(left_behind(error, &left));
10082 }
10083 let created: Vec<NativeId> = landed
10084 .iter()
10085 .map(|landed| landed.content_id.clone())
10086 .collect();
10087 let mut ids = Vec::with_capacity(landed.len());
10088 for ((incoming, prepared), landed) in incomings.iter().zip(prepared).zip(landed) {
10089 match self.complete_write(incoming, prepared, landed).await {
10090 Ok(written) => ids.push(written.id),
10091 // llmlint: ignore[changed_behavior_has_e2e] While writes are held back, which is the only time a batch is created, completing a created item only holds its parts back, and that fails on nothing but a poisoned lock, which no journey can cause; the issues are taken back regardless, because the caller is never handed their ids.
10092 Err(error) => {
10093 let ids: Vec<&NativeId> = created.iter().collect();
10094 let left = self.delete_issues(&ids).await;
10095 let left: Vec<&str> = left.iter().map(|id| id.0.as_str()).collect();
10096 return Err(left_behind(error, &left));
10097 }
10098 }
10099 }
10100 Ok(ids)
10101 }
10102
10103 /// Send everything held back since [`TaskSource::begin_deferred_writes`]: one read of the
10104 /// created items' board items, then their field values and edges together in requests of
10105 /// [`WRITE_BATCH`], then their closes in requests of [`MUTATION_BATCH`]. Anything refused
10106 /// takes back every issue held, [`DELETE_BATCH`] to a request, and answers the refusal.
10107 async fn send_held_back(&self) -> Result<(), SourceError> {
10108 let Some(Deferred::Held { board, created }) = self.deferred()?.take() else {
10109 return Ok(());
10110 };
10111 if created.is_empty() {
10112 return Ok(());
10113 }
10114 let sent = self.send_deferred(board.as_str(), &created).await;
10115 if let Err(error) = sent {
10116 // Best effort, and the failure is what the caller is told, as a write of one item
10117 // that fails part way takes back what it created: the copy that held these back
10118 // journalled every one, so one this cannot take back its undo tries again, and says
10119 // so when it cannot either.
10120 let ids: Vec<&NativeId> = created.iter().map(|held| &held.content).collect();
10121 let _ = self.delete_issues(&ids).await;
10122 return Err(error);
10123 }
10124 Ok(())
10125 }
10126
10127 /// The requests [`Self::send_held_back`] makes.
10128 async fn send_deferred(&self, board: &str, created: &[HeldBack]) -> Result<(), SourceError> {
10129 let items = self.created_board_items(board, created).await?;
10130 let mut fields: Vec<Value> = Vec::new();
10131 let mut closes: Vec<Value> = Vec::new();
10132 let mut edges: Vec<Value> = Vec::new();
10133 for held in created {
10134 let (item, status) = &items[&held.content];
10135 let mut values = held.fields.clone();
10136 // A status the board already shows — set by a workflow a person turned on to file
10137 // new items at it — is not written again.
10138 if let Some((field, option, name)) = &held.status
10139 && status.as_deref() != Some(name.as_str())
10140 {
10141 values.insert(0, (field.clone(), json!({"singleSelectOptionId": option})));
10142 }
10143 for (field, value) in values {
10144 fields.push(json!({"projectId":board,"itemId":item,"fieldId":field,"value":value}));
10145 }
10146 if let Some(reason) = held.close {
10147 closes.push(json!({"id": held.content.0, "stateInput": closed_input(reason)}));
10148 }
10149 for far in &held.edges {
10150 edges.push(json!({"issueId": held.content.0, "blockingIssueId": far.0}));
10151 }
10152 if !held.clears.is_empty() {
10153 let clears: Vec<&str> = held.clears.iter().map(String::as_str).collect();
10154 self.set_item_fields(board, item, &[], &clears).await?;
10155 }
10156 }
10157 // Each alias answers with what it wrote, which is what its input named: the board item a
10158 // field value is set on, the issue closed, the issue blocked and the one blocking it.
10159 let field_answer: &[(&str, &str)] = &[("projectV2Item", "itemId")];
10160 let close_answer: &[(&str, &str)] = &[("issue", "id")];
10161 let block_answer: &[(&str, &str)] =
10162 &[("issue", "issueId"), ("blockingIssue", "blockingIssueId")];
10163 // Every field value, then every edge, as one ordered list sent `WRITE_BATCH` aliases to a
10164 // request: a request holding both kinds is the one document carrying both, so a plan's
10165 // values and edges cost one request where they fit in one.
10166 let writes: Vec<HeldWrite<'_>> = fields
10167 .iter()
10168 .map(HeldWrite::Field)
10169 .chain(edges.iter().map(HeldWrite::Edge))
10170 .collect();
10171 for chunk in writes.chunks(WRITE_BATCH) {
10172 let values: Vec<&Value> = chunk
10173 .iter()
10174 .filter_map(|write| match write {
10175 HeldWrite::Field(input) => Some(*input),
10176 HeldWrite::Edge(_) => None,
10177 })
10178 .collect();
10179 let blocks: Vec<&Value> = chunk
10180 .iter()
10181 .filter_map(|write| match write {
10182 HeldWrite::Edge(input) => Some(*input),
10183 HeldWrite::Field(_) => None,
10184 })
10185 .collect();
10186 let (document, sends) = match (values.is_empty(), blocks.is_empty()) {
10187 (false, false) => (graphql::WRITE_FIELDS_AND_EDGES, ["sendField", "sendBlock"]),
10188 (false, true) => (graphql::WRITE_FIELD_VALUES, ["send", "send"]),
10189 _ => (graphql::ADD_BLOCKED_BYS, ["send", "send"]),
10190 };
10191 let mut variables = serde_json::Map::new();
10192 let kinds = [
10193 ("field", &values, sends[0], field_answer),
10194 ("block", &blocks, sends[1], block_answer),
10195 ];
10196 for (prefix, inputs, send, _) in &kinds {
10197 if inputs.is_empty() {
10198 continue;
10199 }
10200 for slot in 0..WRITE_BATCH {
10201 variables.insert(
10202 format!("{prefix}{slot}"),
10203 (*inputs.get(slot).unwrap_or(&inputs[0])).clone(),
10204 );
10205 variables.insert(format!("{send}{slot}"), json!(slot < inputs.len()));
10206 }
10207 }
10208 let data = self.graphql(document, Value::Object(variables)).await?;
10209 for (prefix, inputs, _, answers) in kinds {
10210 check_aliased_answers(document, &data, prefix, inputs, answers)?;
10211 }
10212 }
10213 for chunk in closes.chunks(MUTATION_BATCH) {
10214 let mut variables = serde_json::Map::new();
10215 for slot in 0..MUTATION_BATCH {
10216 variables.insert(
10217 format!("close{slot}"),
10218 chunk.get(slot).cloned().unwrap_or_else(|| chunk[0].clone()),
10219 );
10220 variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10221 }
10222 let data = self
10223 .graphql(graphql::CLOSE_ISSUES, Value::Object(variables))
10224 .await?;
10225 let inputs: Vec<&Value> = chunk.iter().collect();
10226 check_aliased_answers(graphql::CLOSE_ISSUES, &data, "close", &inputs, close_answer)?;
10227 }
10228 for held in created {
10229 self.learn_item(&held.content, &items[&held.content].0)?;
10230 }
10231 Ok(())
10232 }
10233
10234 /// The board item each of `created` is on this board, with its `Status` as the board set it:
10235 /// [`graphql::CREATED_BOARD_ITEMS`], read again — after a wait of a second, then two, four,
10236 /// eight and sixteen — for only those GitHub has not filed yet.
10237 ///
10238 /// **Membership is never assumed.** `createIssue`'s `projectV2Ids` files an issue some time
10239 /// after GitHub answered the create — the feasibility spike saw 0.45 to 1.84 seconds, and no
10240 /// create answer carrying the item — so only a read says it is there. One still absent once
10241 /// those reads are spent is filed with [`graphql::ADD_TO_BOARD`], one write each; GitHub
10242 /// filing it meanwhile refuses that "Content already exists in this project", which says it
10243 /// is placed, and one more read finds the item rather than a second one being made.
10244 async fn created_board_items(
10245 &self,
10246 board: &str,
10247 created: &[HeldBack],
10248 ) -> Result<BTreeMap<NativeId, (String, Option<String>)>, SourceError> {
10249 let mut found: BTreeMap<NativeId, (String, Option<String>)> = BTreeMap::new();
10250 let unfiled = |found: &BTreeMap<NativeId, (String, Option<String>)>| -> Vec<NativeId> {
10251 created
10252 .iter()
10253 .map(|held| held.content.clone())
10254 .filter(|id| !found.contains_key(id))
10255 .collect()
10256 };
10257 let mut wait = Duration::from_secs(1);
10258 for attempt in 0..6 {
10259 let absent = unfiled(&found);
10260 if absent.is_empty() {
10261 return Ok(found);
10262 }
10263 if attempt > 0 {
10264 self.clock
10265 .sleep_because(wait, pacing::cause::BOARD_FILING)
10266 .await;
10267 wait = wait.saturating_mul(2);
10268 }
10269 self.read_created_items(board, &absent, &mut found).await?;
10270 }
10271 let mut placed_meanwhile = Vec::new();
10272 for id in unfiled(&found) {
10273 match self
10274 .graphql(
10275 graphql::ADD_TO_BOARD,
10276 json!({"input":{"projectId":board,"contentId":id.0}}),
10277 )
10278 .await
10279 {
10280 Ok(added) => {
10281 let item = added
10282 .pointer("/addProjectV2ItemById/item")
10283 .filter(|value| !value.is_null())
10284 .ok_or_else(|| SourceError::Malformed {
10285 message: "GitHub board addition returned no project item".into(),
10286 })?;
10287 found.insert(id, (required_nonblank_str(item, "id")?.to_owned(), None));
10288 }
10289 Err(error) if already_on_the_board(&error) => placed_meanwhile.push(id),
10290 Err(error) => return Err(error),
10291 }
10292 }
10293 if !placed_meanwhile.is_empty() {
10294 self.read_created_items(board, &placed_meanwhile, &mut found)
10295 .await?;
10296 }
10297 let absent = unfiled(&found);
10298 if absent.is_empty() {
10299 return Ok(found);
10300 }
10301 let absent: Vec<&str> = absent.iter().map(|id| id.0.as_str()).collect();
10302 Err(SourceError::Unavailable {
10303 message: format!(
10304 "GitHub created {} filed on board {board} and, after 31 seconds and adding {} \
10305 to it, still answers {} on no item of it: {}; next: run the copy again once \
10306 GitHub has caught up",
10307 if absent.len() == 1 {
10308 "an issue"
10309 } else {
10310 "issues"
10311 },
10312 if absent.len() == 1 { "it" } else { "them" },
10313 if absent.len() == 1 { "it" } else { "them" },
10314 absent.join(", ")
10315 ),
10316 })
10317 }
10318
10319 /// One [`graphql::CREATED_BOARD_ITEMS`] read of each of `ids`, [`COPY_DETAIL_BATCH`] to a
10320 /// request, adding to `found` the board item of every one GitHub has filed on `board`.
10321 async fn read_created_items(
10322 &self,
10323 board: &str,
10324 ids: &[NativeId],
10325 found: &mut BTreeMap<NativeId, (String, Option<String>)>,
10326 ) -> Result<(), SourceError> {
10327 for chunk in ids.chunks(COPY_DETAIL_BATCH) {
10328 let mut variables = serde_json::Map::new();
10329 for slot in 0..COPY_DETAIL_BATCH {
10330 variables.insert(
10331 format!("id{slot}"),
10332 json!(chunk.get(slot).unwrap_or(&chunk[0]).0),
10333 );
10334 variables.insert(format!("read{slot}"), json!(slot < chunk.len()));
10335 }
10336 variables.insert("boardItems".into(), json!(BOARD_ITEMS_PAGE_SIZE));
10337 variables.insert("statusName".into(), json!(STATUS_FIELD));
10338 let data = self
10339 .graphql(graphql::CREATED_BOARD_ITEMS, Value::Object(variables))
10340 .await?;
10341 for (slot, id) in chunk.iter().enumerate() {
10342 // An answer for another issue than the one asked about says nothing of
10343 // where that one is filed, and its board item is not this one's.
10344 let node = data.get(format!("i{slot}")).filter(|node| !node.is_null());
10345 let named = node.map(|node| node.get("id").and_then(Value::as_str));
10346 if let Some(named) = named
10347 && named != Some(id.0.as_str())
10348 {
10349 return Err(SourceError::Malformed {
10350 message: format!(
10351 "GitHub answered the board items of {} when asked for those of {}",
10352 named.unwrap_or("an issue it did not name"),
10353 id.0
10354 ),
10355 });
10356 }
10357 // An issue GitHub does not answer yet is not filed yet; one it answers is
10358 // read whole, its connection, each board item's board and its status, and
10359 // any of them unreadable is refused rather than read as not filed.
10360 let Some(node) = node else { continue };
10361 let malformed = |what: &str| SourceError::Malformed {
10362 message: format!("GitHub answered the board items of {} with {what}", id.0),
10363 };
10364 let nodes = node
10365 .pointer("/projectItems/nodes")
10366 .and_then(Value::as_array)
10367 .ok_or_else(|| malformed("no list of board items"))?;
10368 let mut item = None;
10369 for node in nodes {
10370 let on = node
10371 .pointer("/project/id")
10372 .and_then(Value::as_str)
10373 .ok_or_else(|| malformed("a board item naming no board"))?;
10374 if on == board {
10375 item = Some(node);
10376 }
10377 }
10378 if let Some(item) = item {
10379 let status = match item.get("status") {
10380 None | Some(Value::Null) => None,
10381 Some(status) => Some(
10382 status
10383 .get("name")
10384 .and_then(Value::as_str)
10385 .ok_or_else(|| malformed("a status that names no option"))?
10386 .to_owned(),
10387 ),
10388 };
10389 found.insert(
10390 id.clone(),
10391 (required_nonblank_str(item, "id")?.to_owned(), status),
10392 );
10393 }
10394 }
10395 }
10396 Ok(())
10397 }
10398
10399 /// The board item id of `content`, which this process created and remembered without one,
10400 /// in every half of its own view.
10401 fn learn_item(&self, content: &NativeId, item: &str) -> Result<(), SourceError> {
10402 if let Some(held) = self.resolved_cache()?.get_mut(content) {
10403 held.item_id = item.to_owned();
10404 }
10405 if let Some(held) = self.created()?.iter_mut().find(|held| held.id == *content) {
10406 held.item_id = item.to_owned();
10407 }
10408 Ok(())
10409 }
10410
10411 /// Ask together which items each of `origins` names, and what creating an item reads; see
10412 /// [`TaskSource::read_ahead_origins_for_copy`]. An origin whose answer runs past its first
10413 /// page is left to its own lookup.
10414 async fn origins_ahead(&self, origins: &[String]) -> Result<(), SourceError> {
10415 if self.repository.is_none() {
10416 return Ok(());
10417 }
10418 let unasked: Vec<&String> = {
10419 let held = self.narrowed_cache()?;
10420 let mut unasked: Vec<&String> = Vec::new();
10421 for origin in origins {
10422 if !held.contains_key(&Narrowing::Origin(origin.clone()).key())
10423 && !unasked.contains(&origin)
10424 {
10425 unasked.push(origin);
10426 }
10427 }
10428 unasked
10429 };
10430 if unasked.is_empty() {
10431 return Ok(());
10432 }
10433 // One batch is held back for the read ahead that follows, which may carry a project's
10434 // members beside it in the same request.
10435 if unasked.len() <= ORIGIN_BATCH {
10436 let mut pending =
10437 self.pending_origins
10438 .lock()
10439 .map_err(|_| SourceError::Unavailable {
10440 message:
10441 "origins held for a read ahead were left inconsistent; run the command \
10442 again"
10443 .into(),
10444 })?;
10445 for origin in unasked {
10446 if !pending.contains(origin) {
10447 pending.push(origin.clone());
10448 }
10449 }
10450 return Ok(());
10451 }
10452 let unasked: Vec<String> = unasked.into_iter().cloned().collect();
10453 self.send_origin_lookups(&unasked, None).await
10454 }
10455
10456 /// Hold, for [`TaskSource::member_classifications`] of `project`, each member's
10457 /// classification and repositories as the members page `data` carries them — when that page
10458 /// is every member, each one names `project` as its parent, and every record it holds is
10459 /// readable. Any other page holds nothing, so the members are read as they always were.
10460 fn hold_member_classes(&self, project: &NativeId, data: &Value) -> Result<(), SourceError> {
10461 let Some(page) = data
10462 .pointer("/members/subIssues")
10463 .filter(|page| !page.is_null())
10464 else {
10465 return Ok(());
10466 };
10467 if page
10468 .pointer("/pageInfo/hasNextPage")
10469 .and_then(Value::as_bool)
10470 != Some(false)
10471 {
10472 return Ok(());
10473 }
10474 let Some(nodes) = page.get("nodes").and_then(Value::as_array) else {
10475 return Ok(());
10476 };
10477 let mut members = Vec::with_capacity(nodes.len());
10478 for node in nodes {
10479 if node.pointer("/parent/id").and_then(Value::as_str) != Some(project.0.as_str()) {
10480 return Ok(());
10481 }
10482 let Some(body) = node.get("body").and_then(Value::as_str) else {
10483 return Ok(());
10484 };
10485 // Every node here is filed under `project`, which `BoardKind::of` reads as one of its
10486 // tasks whatever its marker says, so each is a member the full read would classify.
10487 let Ok((_, slot)) = metadata_body(Some(body.to_owned())) else {
10488 return Ok(());
10489 };
10490 let Ok(classification) = Classification::from_metadata(&slot) else {
10491 return Ok(());
10492 };
10493 let repositories = if slot.contains_key(Repository::METADATA_KEY) {
10494 let Ok(repositories) = Repository::from_metadata(&slot) else {
10495 return Ok(());
10496 };
10497 repositories
10498 } else {
10499 let Some(origin) = node
10500 .pointer("/repository/nameWithOwner")
10501 .and_then(Value::as_str)
10502 else {
10503 return Ok(());
10504 };
10505 let Ok(own) = Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST))
10506 else {
10507 return Ok(());
10508 };
10509 vec![own]
10510 };
10511 members.push((classification, repositories));
10512 }
10513 self.member_classes
10514 .lock()
10515 .map_err(|_| SourceError::Unavailable {
10516 message: "a project's members held for its classification were left \
10517 inconsistent; run the command again"
10518 .into(),
10519 })?
10520 .insert(project.clone(), members);
10521 Ok(())
10522 }
10523
10524 /// Send every origin held back for the read ahead, beside the members of `members` when it
10525 /// names a project — or nothing, when none is held.
10526 async fn send_pending_origins(&self, members: Option<&NativeId>) -> Result<(), SourceError> {
10527 let pending = std::mem::take(&mut *self.pending_origins.lock().map_err(|_| {
10528 SourceError::Unavailable {
10529 message: "origins held for a read ahead were left inconsistent; run the command \
10530 again"
10531 .into(),
10532 }
10533 })?);
10534 if pending.is_empty() {
10535 return Ok(());
10536 }
10537 self.send_origin_lookups(&pending, members).await
10538 }
10539
10540 /// [`graphql::ORIGIN_LOOKUPS`] of `unasked`, [`ORIGIN_BATCH`] to a request — the first one
10541 /// [`graphql::ORIGIN_LOOKUPS_WITH_MEMBERS`], carrying the members of `members`, when it names
10542 /// a project — holding each origin's answer and, from a members page that is the whole of
10543 /// them, each member's classification.
10544 async fn send_origin_lookups(
10545 &self,
10546 unasked: &[String],
10547 members: Option<&NativeId>,
10548 ) -> Result<(), SourceError> {
10549 let Some(repository) = self.repository.clone() else {
10550 return Ok(());
10551 };
10552 let mut members = members;
10553 for chunk in unasked.chunks(ORIGIN_BATCH) {
10554 let mut variables = json!({"owner":self.owner,"number":self.project_number,
10555 "type":"ISSUE","lookupFirst":ORIGIN_LOOKUPS_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
10556 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true,
10557 "repositoryOwner":repository.owner,"repositoryName":repository.name});
10558 for slot in 0..ORIGIN_BATCH {
10559 let origin = chunk.get(slot).unwrap_or(&chunk[0]);
10560 variables[format!("filter{slot}")] =
10561 json!(format!("{ORIGIN_FIELD}:{}", quoted(origin)));
10562 variables[format!("search{slot}")] = json!(
10563 self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))))
10564 );
10565 variables[format!("ask{slot}")] = json!(slot < chunk.len());
10566 }
10567 let project = members.take();
10568 let document = match project {
10569 Some(project) => {
10570 variables["project"] = json!(project.0);
10571 variables["memberFirst"] = json!(MEMBER_PAGE_SIZE);
10572 graphql::ORIGIN_LOOKUPS_WITH_MEMBERS
10573 }
10574 None => graphql::ORIGIN_LOOKUPS,
10575 };
10576 let data = self.graphql(document, variables).await?;
10577 self.fields_read(&data)?;
10578 if let Some(project) = project {
10579 self.hold_member_classes(project, &data)?;
10580 }
10581 if let Some(node) = data.get("repository").filter(|value| !value.is_null()) {
10582 let id = required_nonblank_str(node, "id")?.to_owned();
10583 self.repository_cache()?.insert(repository.clone(), id);
10584 if let Some(numeric) = node
10585 .get("databaseId")
10586 .and_then(Value::as_u64)
10587 .and_then(std::num::NonZeroU64::new)
10588 {
10589 self.numeric_repositories
10590 .lock()
10591 .await
10592 .insert(repository.clone(), numeric);
10593 }
10594 }
10595 for (slot, origin) in chunk.iter().enumerate() {
10596 let items = data
10597 .pointer(&format!("/o{slot}/projectV2/items"))
10598 .filter(|value| !value.is_null())
10599 .ok_or_else(|| SourceError::Refused {
10600 message: format!(
10601 "GitHub project {}/{} was not found or is not visible to the token",
10602 self.owner, self.project_number
10603 ),
10604 })?;
10605 let searched =
10606 data.get(format!("s{slot}"))
10607 .ok_or_else(|| SourceError::Malformed {
10608 message: "GitHub search response has no search connection".into(),
10609 })?;
10610 if resumed(items, None)?.has_more() || resumed(searched, None)?.has_more() {
10611 continue;
10612 }
10613 let mut found: Vec<Resolved> = Vec::new();
10614 for item in optional_nodes(Some(items), "project items")?
10615 .into_iter()
10616 .flatten()
10617 {
10618 if let Some(resolved) = self.resolve(item)?
10619 && resolved.content_kind == ContentKind::Issue
10620 && !found.iter().any(|held| held.id == resolved.id)
10621 {
10622 found.push(resolved);
10623 }
10624 }
10625 for node in optional_nodes(Some(searched), "search")?
10626 .into_iter()
10627 .flatten()
10628 {
10629 if let Some(resolved) = self.resolve_issue(node).await?
10630 && !found.iter().any(|held| held.id == resolved.id)
10631 {
10632 found.push(resolved);
10633 }
10634 }
10635 self.narrowed_cache()?
10636 .insert(Narrowing::Origin((*origin).clone()).key(), found);
10637 }
10638 }
10639 Ok(())
10640 }
10641
10642 /// Delete every issue of `ids`, [`DELETE_BATCH`] to a request with
10643 /// [`graphql::DELETE_ISSUES`], and answer those still there: each a request's answer does not
10644 /// confirm, every one of a request refused whole included.
10645 async fn delete_issues(&self, ids: &[&NativeId]) -> Vec<NativeId> {
10646 let mut left = Vec::new();
10647 for chunk in ids.chunks(DELETE_BATCH) {
10648 let mut variables = serde_json::Map::new();
10649 for slot in 0..DELETE_BATCH {
10650 let id = chunk.get(slot).unwrap_or(&chunk[0]);
10651 variables.insert(format!("delete{slot}"), json!({"issueId": id.0}));
10652 variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10653 }
10654 let data = self
10655 .graphql(graphql::DELETE_ISSUES, Value::Object(variables))
10656 .await
10657 .unwrap_or(Value::Null);
10658 for (slot, id) in chunk.iter().enumerate() {
10659 // Confirmed only by the repository the deleted issue was in, named by a
10660 // non-blank id: anything else says nothing of whether the issue is gone.
10661 let confirmed = data
10662 .pointer(&format!("/delete{slot}/repository/id"))
10663 .and_then(Value::as_str)
10664 .is_some_and(|id| !id.trim().is_empty());
10665 if !confirmed {
10666 left.push((*id).clone());
10667 }
10668 }
10669 }
10670 left
10671 }
10672
10673 /// Delete one issue, which takes its board item with it.
10674 async fn delete_issue(&self, id: &NativeId) -> Result<(), SourceError> {
10675 let data = self
10676 .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
10677 .await?;
10678 data.pointer("/deleteIssue/repository")
10679 .filter(|value| !value.is_null())
10680 .ok_or_else(|| SourceError::Malformed {
10681 message: "GitHub issue deletion returned no repository".into(),
10682 })?;
10683 self.forget(id)?;
10684 Ok(())
10685 }
10686
10687 /// Remove one item this copy created, so a copy that could not finish leaves the board
10688 /// as it found it.
10689 ///
10690 /// Deleting the issue takes its board item with it, so there is no second mutation to
10691 /// keep in step. An id the board does not hold is not an error: the item is already
10692 /// gone, which is the state this asks for. Which that is, is decided by reading the item
10693 /// by its own id — a listing of the board can still be missing an item it holds, and
10694 /// reading that as *already gone* would leave behind the very item this was asked to
10695 /// take back.
10696 async fn delete_item(&self, id: &NativeId) -> Result<(), SourceError> {
10697 let Some(item) = self.bound_item(id).await? else {
10698 return Ok(());
10699 };
10700 if item.content_kind == ContentKind::DraftIssue {
10701 return Err(SourceError::Refused {
10702 message: format!(
10703 "GitHub item {} is a draft, and this source removes an item by deleting \
10704 its issue; next: remove it from the board by hand",
10705 id.0
10706 ),
10707 });
10708 }
10709 let data = self
10710 .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
10711 .await?;
10712 data.pointer("/deleteIssue/repository")
10713 .filter(|value| !value.is_null())
10714 .ok_or_else(|| SourceError::Malformed {
10715 message: "GitHub issue deletion returned no repository".into(),
10716 })?;
10717 self.forget(id)?;
10718 Ok(())
10719 }
10720
10721 /// The issue a comment call on `task` is about, or `None` when this board holds no such
10722 /// task.
10723 ///
10724 /// Resolved exactly as [`TaskSource::get_task`] resolves it, so the comment verbs and a
10725 /// read of the task cannot disagree about which ids name one: a project or a document of
10726 /// this board is not a task here either.
10727 ///
10728 /// A **draft** is a task with nowhere to keep a comment, because GitHub keeps comments on
10729 /// issues and a draft is not one. It is refused rather than answered with an empty page,
10730 /// which would read as a task nobody has commented on yet.
10731 async fn commented_issue(&self, task: &NativeId) -> Result<Option<NativeId>, SourceError> {
10732 let cached = self.resolved_cache()?.get(task).cloned();
10733 let Some(item) = (match cached {
10734 Some(item) => Some(item),
10735 None => self.item_by_id(task).await?,
10736 })
10737 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
10738 return Ok(None);
10739 };
10740 if item.content_kind == ContentKind::DraftIssue {
10741 return Err(self.draft_has_no_comments(task));
10742 }
10743 Ok(Some(item.id))
10744 }
10745
10746 /// The refusal a comment call on a board draft is answered with: GitHub keeps comments on
10747 /// issues, and a draft is not one.
10748 fn draft_has_no_comments(&self, task: &NativeId) -> SourceError {
10749 SourceError::Refused {
10750 message: format!(
10751 "task {} of source {} is a draft item on the board, and GitHub keeps \
10752 comments on issues alone, so a draft has none to read or write; next: \
10753 convert the draft to an issue on the board, then comment on the issue it \
10754 becomes",
10755 task.0, self.name
10756 ),
10757 }
10758 }
10759
10760 /// One task and a page of its comments, read with [`graphql::ISSUE_DETAIL`] in one
10761 /// request — or `None` when this board holds no task by that id.
10762 ///
10763 /// What `task show` and a comment listing read. A draft is a task with no comments, so it
10764 /// is answered with the draft and the refusal, at the price of the draft's own read.
10765 async fn issue_detail(
10766 &self,
10767 id: &NativeId,
10768 page: &PageRequest,
10769 ) -> Result<Option<TaskDetailRead>, SourceError> {
10770 let after = page.cursor.as_ref().map(|cursor| cursor.0.as_str());
10771 let asked = self
10772 .graphql(
10773 graphql::ISSUE_DETAIL,
10774 json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),"after":after,
10775 "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
10776 "duplicates":true}),
10777 )
10778 .await;
10779 let data = match asked {
10780 Ok(data) => data,
10781 Err(error) if unresolvable_node(&error) => return Ok(None),
10782 Err(error) => return Err(error),
10783 };
10784 // `node` is null for an id that names nothing, and absent only from an answer this
10785 // source cannot read — never the same thing.
10786 let node = data.get("node").ok_or_else(|| SourceError::Malformed {
10787 message: format!("GitHub answered the read of {} with no node", id.0),
10788 })?;
10789 self.detail_of(id, node, true, after).await
10790 }
10791
10792 /// Several tasks, each with the first page of its comments when `comments` is set, read
10793 /// [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] — one answer per id, in
10794 /// order.
10795 ///
10796 /// A batch GitHub refuses because one of its ids resolves to no node at all is read again
10797 /// one item at a time, so that id is answered as missing and the others as themselves; any
10798 /// other refusal is every id of that batch's answer.
10799 async fn issue_details(
10800 &self,
10801 ids: &[NativeId],
10802 comments: Option<&PageRequest>,
10803 ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
10804 let mut read = Vec::with_capacity(ids.len());
10805 for batch in ids.chunks(DETAIL_BATCH) {
10806 match self
10807 .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, comments))
10808 .await
10809 {
10810 Ok(data) => {
10811 for (slot, id) in batch.iter().enumerate() {
10812 // Every alias asked for is answered, null for an id naming nothing;
10813 // one missing is an answer this source cannot read.
10814 let read_one = match data.get(format!("i{slot}")) {
10815 Some(node) => self.detail_of(id, node, comments.is_some(), None).await,
10816 None => Err(SourceError::Malformed {
10817 message: format!(
10818 "GitHub answered a batch read with no item for {}",
10819 id.0
10820 ),
10821 }),
10822 };
10823 read.push(read_one);
10824 }
10825 }
10826 Err(error) if unresolvable_node(&error) => {
10827 for id in batch {
10828 read.push(match comments {
10829 Some(page) => self.issue_detail(id, page).await,
10830 None => self.task_read(id).await,
10831 });
10832 }
10833 }
10834 Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
10835 }
10836 }
10837 read
10838 }
10839
10840 /// One task and nothing of its comments, as [`TaskSource::get_task`] reads it.
10841 async fn task_read(&self, id: &NativeId) -> Result<Option<TaskDetailRead>, SourceError> {
10842 Ok(self.get_task(id).await?.map(|task| TaskDetailRead {
10843 task,
10844 comments: None,
10845 }))
10846 }
10847
10848 /// What one node a detail read reached says: the task this board holds by `id`, with the
10849 /// page of comments the node carries when `commented` — or `None` for a node that is no
10850 /// task of this board.
10851 ///
10852 /// Resolved as [`Self::item_by_id`] resolves an item: a draft is read again as a draft,
10853 /// and an item this process created answers from this process's own record, which a node
10854 /// read taken moments after the write can still be behind.
10855 async fn detail_of(
10856 &self,
10857 id: &NativeId,
10858 node: &Value,
10859 commented: bool,
10860 after: Option<&str>,
10861 ) -> Result<Option<TaskDetailRead>, SourceError> {
10862 if node.is_null() {
10863 return Ok(None);
10864 }
10865 let draft = optional_str(node, "__typename")? == Some("DraftIssue");
10866 // An issue answered under one id is that id's, or the answer is not one this source
10867 // can report: reporting another issue's task and comments under the qualified id asked
10868 // for would be the one wrong answer here. A draft's own read checks the same.
10869 if !draft
10870 && optional_str(node, "__typename")? == Some("Issue")
10871 && required_str(node, "id")? != id.0
10872 {
10873 return Err(SourceError::Malformed {
10874 message: format!(
10875 "GitHub answered the read of {} with issue {}",
10876 id.0,
10877 required_str(node, "id")?
10878 ),
10879 });
10880 }
10881 let item = if draft {
10882 self.draft_by_id(id).await?
10883 } else {
10884 self.resolve_issue(node).await?
10885 };
10886 // A read that carried the whole of what the issue is blocked by — a copy's — is kept
10887 // for this command, so the dependency read the copy makes next is answered from it.
10888 if let Some(read) = item.as_ref().filter(|item| item.blocked_by.is_some()) {
10889 self.resolved_cache()?.insert(read.id.clone(), read.clone());
10890 }
10891 let Some(item) = item.filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
10892 return Ok(None);
10893 };
10894 let own = self.created()?.iter().find(|own| own.id == *id).cloned();
10895 let task = own.unwrap_or(item).task()?;
10896 let comments = match (commented, draft) {
10897 (false, _) => None,
10898 (true, true) => Some(Err(self.draft_has_no_comments(id))),
10899 (true, false) => Some(comment_page(node, &id.0, after).map(Some)),
10900 };
10901 if commented && !draft && after.is_none() {
10902 self.expected.learn_comments(id, node);
10903 }
10904 Ok(Some(TaskDetailRead { task, comments }))
10905 }
10906
10907 /// Whether the comment `comment` is one of `issue`'s own.
10908 ///
10909 /// Read before an edit or a removal is sent, because GitHub's comment mutations take the
10910 /// comment's id and nothing else: a comment id given against the wrong task would
10911 /// otherwise change a comment on some other issue entirely. An id that names nothing, or
10912 /// names something that is not an issue comment, is a comment this task does not have —
10913 /// which is what GitHub refusing to resolve it means too.
10914 async fn comment_is_on(
10915 &self,
10916 issue: &NativeId,
10917 comment: &NativeId,
10918 ) -> Result<bool, SourceError> {
10919 Ok(self.comment_on(issue, comment).await?.is_some())
10920 }
10921
10922 /// The body the comment `comment` holds, exactly as GitHub holds it, when it is one of
10923 /// `issue`'s own — the read [`Self::comment_is_on`] makes, answering with what it selected.
10924 async fn comment_on(
10925 &self,
10926 issue: &NativeId,
10927 comment: &NativeId,
10928 ) -> Result<Option<String>, SourceError> {
10929 // A write the store's cache expects knows which comments its issue held, and their
10930 // bodies, at the version it expects: what this read would answer.
10931 if let Some(primed) = self.expected.primed_comment(issue, comment) {
10932 return Ok(primed);
10933 }
10934 let asked = self
10935 .graphql(graphql::COMMENT_ISSUE, json!({"id":comment.0}))
10936 .await;
10937 let data = match asked {
10938 Ok(data) => data,
10939 Err(error) if unresolvable_node(&error) => return Ok(None),
10940 Err(error) => return Err(error),
10941 };
10942 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
10943 return Ok(None);
10944 };
10945 if optional_str(node, "__typename")? != Some("IssueComment") {
10946 return Ok(None);
10947 }
10948 let on = node.get("issue").ok_or_else(|| SourceError::Malformed {
10949 message: format!("GitHub issue comment {} names no issue", comment.0),
10950 })?;
10951 if required_str(on, "id")? != issue.0 {
10952 return Ok(None);
10953 }
10954 Ok(Some(required_str(node, "body")?.to_owned()))
10955 }
10956
10957 /// Which far ends this item's own `blockedBy` relationship holds, and which it cannot.
10958 async fn partition_edges(
10959 &self,
10960 near_kind: BoardKind,
10961 near_content: ContentKind,
10962 carried: Option<&[Value]>,
10963 depends_on: &[DependencyEdge],
10964 ) -> Result<(Vec<String>, Vec<DependencyEdge>), SourceError> {
10965 let mut native = Vec::new();
10966 let mut fallback = Vec::new();
10967 let far_ends: Vec<(&DependencyEdge, &str, bool, Option<&Value>)> = depends_on
10968 .iter()
10969 .map(|edge| {
10970 // An opaque far end names another tracker, so it is never this board's own.
10971 let same_source = edge.to.designator().is_none()
10972 && edge
10973 .to
10974 .source()
10975 .is_none_or(|source| source == self.name.as_str());
10976 // A qualified id's source segment runs to its *first* colon — `GlobalId` and
10977 // `DependencyEndpoint::source` both read it that way — and a native id may hold
10978 // colons of its own, so the far end is everything after that one separator.
10979 // Splitting at the last would truncate `work:urn:task:7` to `7`.
10980 let far_id = if edge.to.is_qualified() {
10981 edge.to
10982 .id()
10983 .split_once(':')
10984 .map_or(edge.to.id(), |(_, native)| native)
10985 } else {
10986 edge.to.id()
10987 };
10988 // One that already blocks the near issue was answered by that issue's own
10989 // read, which carried each of its blockers' kinds — an issue every one — so it
10990 // is not read again.
10991 let blocking = carried.and_then(|nodes| {
10992 nodes
10993 .iter()
10994 .find(|node| node.get("id").and_then(Value::as_str) == Some(far_id))
10995 });
10996 (edge, far_id, same_source, blocking)
10997 })
10998 .collect();
10999 // Every other same-source far end is read by its own id, exactly as the item it is a
11000 // far end of is: whether this board holds it is that read's answer, never a listing's.
11001 // They are read together, [`DETAIL_BATCH`] to a request, rather than one each.
11002 let mut unread: Vec<NativeId> = Vec::new();
11003 for (_, far_id, same_source, blocking) in &far_ends {
11004 let id = NativeId((*far_id).to_owned());
11005 if *same_source && blocking.is_none() && !unread.contains(&id) {
11006 unread.push(id);
11007 }
11008 }
11009 let read: BTreeMap<NativeId, Option<Resolved>> = unread
11010 .iter()
11011 .cloned()
11012 .zip(self.items_by_ids(&unread).await?)
11013 .collect();
11014 for (edge, far_id, same_source, blocking) in far_ends {
11015 let far = match (same_source, blocking) {
11016 (false, _) => None,
11017 (true, Some(node)) => Some(FarEnd {
11018 kind: if required_str(node, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
11019 BoardKind::Document
11020 } else {
11021 BoardKind::Work(related_kind(node)?)
11022 },
11023 content_kind: ContentKind::Issue,
11024 }),
11025 (true, None) => {
11026 let read = read
11027 .get(&NativeId(far_id.to_owned()))
11028 .cloned()
11029 .flatten()
11030 .ok_or_else(|| SourceError::Refused {
11031 message: format!("GitHub dependency item {far_id} was not found"),
11032 })?;
11033 Some(FarEnd {
11034 kind: read.kind,
11035 content_kind: read.content_kind,
11036 })
11037 }
11038 };
11039 let far = far.as_ref();
11040 // The caller says which kind the far end is, and this board holds the far end
11041 // itself, so a disagreement is settled here rather than stored: recorded, the
11042 // wrong kind would read back as a cross-level edge that never existed; written
11043 // natively, it would name a relationship of a different level than the caller
11044 // asked for.
11045 //
11046 // A far end this board holds as a *document* fails the same comparison and is
11047 // refused by the same sentence: `ItemKind` has no document variant because
11048 // nothing may point at one, so no caller can name it correctly and the refusal
11049 // is the only honest answer.
11050 if let Some(disagreeing) = far.filter(|far| far.kind != BoardKind::Work(edge.to.kind)) {
11051 return Err(SourceError::Refused {
11052 message: format!(
11053 "GitHub dependency item {far_id} is a {} of this board, and this item \
11054 names it as a {}; record the kind it is",
11055 disagreeing.kind.describes(),
11056 edge.to.kind.marker()
11057 ),
11058 });
11059 }
11060 // A draft has neither `blockedBy` nor `blocking`, so no edge of one is native
11061 // however the far end is spelled — and one classified native here would be
11062 // written nowhere at all, because a draft's native reconciliation never runs.
11063 let native_here = near_content == ContentKind::Issue
11064 && far.is_some_and(|far| {
11065 far.content_kind == ContentKind::Issue
11066 && BoardKind::Work(edge.to.kind) == near_kind
11067 });
11068 if native_here {
11069 native.push(far_id.to_owned());
11070 } else {
11071 fallback.push(edge.clone());
11072 }
11073 }
11074 Ok((native, fallback))
11075 }
11076
11077 async fn update_existing(
11078 &self,
11079 item: &Resolved,
11080 incoming: &Incoming<'_>,
11081 body: &Option<String>,
11082 status_target: Option<&StatusTarget>,
11083 ) -> Result<(), SourceError> {
11084 let title = incoming.written_title();
11085 // A terminal status closes the issue here, in the same mutation as its body: its board
11086 // option was selected before this, so a close never lands on an item whose board cannot
11087 // show it.
11088 let mut fields = match item.content_kind {
11089 ContentKind::DraftIssue => json!({"title":title,"body":body}),
11090 ContentKind::Issue => json!({"title":title,"body":body,
11091 "stateInput":state_input(status_target)}),
11092 };
11093 // Only the members whose value differs from the item as this write read it are sent,
11094 // and nothing at all when none does — whether or not the store's cache expects a
11095 // version of it. A member left out is one GitHub leaves as it is, so a person's edit
11096 // of it since that read is kept rather than overwritten with what it already held,
11097 // and a re-copy of an unchanged item costs no mutation.
11098 if let Some(members) = fields.as_object_mut() {
11099 if members.get("title").and_then(Value::as_str) == Some(item.title.as_str()) {
11100 members.remove("title");
11101 }
11102 let body_unchanged = match (members.get("body"), item.raw_body.as_deref()) {
11103 (Some(Value::String(sent)), Some(held)) => sent == held,
11104 (Some(Value::Null), held) => held.is_none_or(str::is_empty),
11105 _ => false,
11106 };
11107 if body_unchanged {
11108 members.remove("body");
11109 }
11110 // An issue already in the state the target asks for keeps it: an open one stays
11111 // open, and a closed one whose closure reads as the target's category keeps its
11112 // reason — a duplicate's says more than this write would.
11113 let unchanged_state = match status_target {
11114 Some(StatusTarget::Terminal(_, reason)) => {
11115 item.closed
11116 && closed_category(item.reason.as_deref())
11117 == closed_category(Some(reason.reason()))
11118 }
11119 Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => !item.closed,
11120 None => true,
11121 };
11122 if unchanged_state {
11123 members.remove("stateInput");
11124 }
11125 if members.is_empty() {
11126 return Ok(());
11127 }
11128 }
11129 self.update_content(item.content_kind, &item.id, fields)
11130 .await
11131 }
11132
11133 /// Update one board item's content with exactly `fields` beside its id, through the
11134 /// mutation its kind takes: `updateIssue` for an issue, `updateProjectV2DraftIssue` for
11135 /// a draft.
11136 ///
11137 /// Every input field either mutation leaves out is a field GitHub leaves as it is, which
11138 /// is what lets a narrow write carry the one thing it changes and nothing else.
11139 async fn update_content(
11140 &self,
11141 kind: ContentKind,
11142 id: &NativeId,
11143 fields: Value,
11144 ) -> Result<(), SourceError> {
11145 let (operation, id_key, pointer) = match kind {
11146 ContentKind::DraftIssue => (
11147 graphql::UPDATE_DRAFT,
11148 "draftIssueId",
11149 "/updateProjectV2DraftIssue/draftIssue",
11150 ),
11151 ContentKind::Issue => (graphql::UPDATE_ISSUE, "id", "/updateIssue/issue"),
11152 };
11153 let mut input = fields;
11154 input[id_key] = json!(id.0);
11155 let data = self.graphql(operation, json!({"input":input})).await?;
11156 let returned = data
11157 .pointer(pointer)
11158 .ok_or_else(|| SourceError::Malformed {
11159 message: "GitHub item update returned no item".into(),
11160 })?;
11161 if required_str(returned, "id")? != id.0 {
11162 return Err(SourceError::Malformed {
11163 message: "GitHub item update returned the wrong item".into(),
11164 });
11165 }
11166 Ok(())
11167 }
11168
11169 /// Creates one issue, files it on the board, and reports what a read of it would say:
11170 /// its content id, its board item id, and the web address GitHub gave it.
11171 ///
11172 /// Two calls rather than one: `createIssue` answers with an issue that is on no board,
11173 /// and `addProjectV2ItemById` is what puts it there. Filing it at creation through
11174 /// `CreateIssueInput.projectV2Ids` was tried and is not done: GitHub answered with no
11175 /// board item, and the `addProjectV2ItemById` that then had to follow was refused
11176 /// "Content already exists in this project". A terminal status is not written here:
11177 /// `finish_write` selects its option first and closes the issue after, so a close never
11178 /// lands on an item whose board cannot show it.
11179 ///
11180 /// The address and the number come back here because this is the only place either is
11181 /// known before GitHub's own board read catches up — an item this run created answers
11182 /// the reads that follow it out of the record below, and one remembered without them
11183 /// would report no location and no key for the rest of the run.
11184 async fn create_and_file_issue(
11185 &self,
11186 board_id: &str,
11187 repository: &RepositoryTarget,
11188 incoming: &Incoming<'_>,
11189 body: &Option<String>,
11190 ) -> Result<Landed, SourceError> {
11191 let repository_id = self.repository_id(repository, incoming).await?;
11192 let data = self
11193 .graphql(
11194 graphql::CREATE_ISSUE,
11195 json!({"input":{
11196 "repositoryId":repository_id,"title":incoming.written_title(),"body":body
11197 }}),
11198 )
11199 .await?;
11200 let created = data
11201 .pointer("/createIssue/issue")
11202 .filter(|value| !value.is_null())
11203 .ok_or_else(|| SourceError::Malformed {
11204 message: "GitHub issue creation returned no issue".into(),
11205 })?;
11206 let content_id = NativeId(required_str(created, "id")?.to_owned());
11207 // Optional although GitHub's schema makes it non-null: the issue exists by now, so
11208 // a response without it is not worth failing a landed write over — the item simply
11209 // reports no location until the board read catches up, which is what it did before.
11210 let url = optional_str(created, "url")?.map(str::to_owned);
11211 // The issue exists from here on, so an unreadable number and a refused board
11212 // filing below each try, best effort, to take it back: an issue in the repository
11213 // that is on no board is an item nobody asked for and nothing here would find again.
11214 //
11215 // Its number is optional on the same terms its address is — a landed write is not
11216 // worth failing over a member that came back missing, and such an item reports no
11217 // handle until a board read catches up. A number that is *present* and is not an
11218 // unsigned integer is still a response this source cannot read.
11219 let number = match created_issue_number(created) {
11220 Ok(number) => number,
11221 Err(error) => {
11222 let _ = self.delete_issue(&content_id).await;
11223 return Err(error);
11224 }
11225 };
11226 let added = match self
11227 .graphql(
11228 graphql::ADD_TO_BOARD,
11229 json!({"input":{"projectId":board_id,"contentId":content_id.0}}),
11230 )
11231 .await
11232 {
11233 Ok(added) => added,
11234 Err(error) => {
11235 let _ = self.delete_issue(&content_id).await;
11236 return Err(error);
11237 }
11238 };
11239 let item = added
11240 .pointer("/addProjectV2ItemById/item")
11241 .filter(|value| !value.is_null())
11242 .ok_or_else(|| SourceError::Malformed {
11243 message: "GitHub board addition returned no project item".into(),
11244 })?;
11245 self.note_written(&content_id, url.as_deref());
11246 Ok(Landed {
11247 content_id,
11248 item_id: Some(required_str(item, "id")?.to_owned()),
11249 url,
11250 number,
11251 })
11252 }
11253
11254 /// Move one issue under the project it now belongs to, or out of the one it left.
11255 async fn reparent(
11256 &self,
11257 held: Option<NativeId>,
11258 child: &NativeId,
11259 wanted: Option<&NativeId>,
11260 ) -> Result<(), SourceError> {
11261 if held.as_ref() == wanted {
11262 return Ok(());
11263 }
11264 if let Some(held) = &held {
11265 self.sub_issue(graphql::REMOVE_SUB_ISSUE, held, child, "removeSubIssue")
11266 .await?;
11267 }
11268 if let Some(wanted) = wanted {
11269 self.sub_issue(graphql::ADD_SUB_ISSUE, wanted, child, "addSubIssue")
11270 .await?;
11271 }
11272 Ok(())
11273 }
11274
11275 async fn sub_issue(
11276 &self,
11277 operation: &str,
11278 parent: &NativeId,
11279 child: &NativeId,
11280 root: &str,
11281 ) -> Result<(), SourceError> {
11282 let data = self
11283 .graphql(
11284 operation,
11285 json!({"input":{"issueId":parent.0,"subIssueId":child.0}}),
11286 )
11287 .await?;
11288 let issue =
11289 data.pointer(&format!("/{root}/issue"))
11290 .ok_or_else(|| SourceError::Malformed {
11291 message: "GitHub sub-issue update returned no issue".into(),
11292 })?;
11293 let sub =
11294 data.pointer(&format!("/{root}/subIssue"))
11295 .ok_or_else(|| SourceError::Malformed {
11296 message: "GitHub sub-issue update returned no sub-issue".into(),
11297 })?;
11298 if required_str(issue, "id")? != parent.0 || required_str(sub, "id")? != child.0 {
11299 return Err(SourceError::Malformed {
11300 message: "GitHub sub-issue update returned the wrong issues".into(),
11301 });
11302 }
11303 Ok(())
11304 }
11305
11306 /// Bring one issue's `blockedBy` to exactly `native`, sending only the difference, and say
11307 /// whether there was one.
11308 ///
11309 /// An issue [`Issue::Created`] by this very write is blocked by nothing yet, so its
11310 /// relationships are not read: there is nothing a read of them could find.
11311 async fn reconcile_blocked_by(
11312 &self,
11313 content_id: &NativeId,
11314 native: &[String],
11315 issue: Issue<'_>,
11316 ) -> Result<bool, SourceError> {
11317 let current = match issue {
11318 Issue::Created => Vec::new(),
11319 Issue::Existing(Some(held)) => held
11320 .iter()
11321 .map(|far| required_str(far, "id").map(str::to_owned))
11322 .collect::<Result<Vec<_>, _>>()?,
11323 Issue::Existing(None) => self.native_dependency_ids(content_id).await?,
11324 };
11325 let mut changed = false;
11326 for (operation, far_id) in current
11327 .iter()
11328 .filter(|id| !native.contains(id))
11329 .map(|id| (graphql::REMOVE_BLOCKED_BY, id))
11330 .chain(
11331 native
11332 .iter()
11333 .filter(|id| !current.contains(id))
11334 .map(|id| (graphql::ADD_BLOCKED_BY, id)),
11335 )
11336 {
11337 let data = self
11338 .graphql(
11339 operation,
11340 json!({"input":{"issueId":content_id.0,"blockingIssueId":far_id}}),
11341 )
11342 .await?;
11343 let root = if operation == graphql::ADD_BLOCKED_BY {
11344 "addBlockedBy"
11345 } else {
11346 "removeBlockedBy"
11347 };
11348 let issue =
11349 data.pointer(&format!("/{root}/issue"))
11350 .ok_or_else(|| SourceError::Malformed {
11351 message: "GitHub dependency update returned no issue".into(),
11352 })?;
11353 let blocker = data
11354 .pointer(&format!("/{root}/blockingIssue"))
11355 .ok_or_else(|| SourceError::Malformed {
11356 message: "GitHub dependency update returned no blocking issue".into(),
11357 })?;
11358 if required_str(issue, "id")? != content_id.0 || required_str(blocker, "id")? != far_id
11359 {
11360 return Err(SourceError::Malformed {
11361 message: "GitHub dependency update returned the wrong issues".into(),
11362 });
11363 }
11364 changed = true;
11365 }
11366 Ok(changed)
11367 }
11368}
11369
11370/// The board field values one write sends, in the order GitHub runs them, and the fields it
11371/// clears after them: the origin field when it moves, the `Status` option `column` names, the
11372/// priority, then each projected field.
11373fn field_writes<'a>(
11374 existing: Option<&Resolved>,
11375 origin_field: Option<&str>,
11376 origin: &str,
11377 column: Option<(String, String)>,
11378 priority: Option<&'a PriorityWrite>,
11379 projections: &'a [ProjectionWrite],
11380) -> (Vec<(String, Value)>, Vec<&'a str>) {
11381 let mut fields = Vec::new();
11382 if let Some(field_id) = origin_field
11383 && existing.map_or(!origin.is_empty(), |item| {
11384 item.origin.as_deref().unwrap_or("") != origin
11385 })
11386 {
11387 fields.push((field_id.to_owned(), json!({"text":origin})));
11388 }
11389 if let Some((field_id, option_id)) = column {
11390 fields.push((field_id, json!({"singleSelectOptionId":option_id})));
11391 }
11392 let mut clears = Vec::new();
11393 match priority {
11394 Some(PriorityWrite::Select { field, option }) => {
11395 fields.push((field.clone(), json!({"singleSelectOptionId":option})));
11396 }
11397 Some(PriorityWrite::Clear { field }) => clears.push(field.as_str()),
11398 None => {}
11399 }
11400 for projection in projections {
11401 projection.join(&mut fields, &mut clears);
11402 }
11403 (fields, clears)
11404}
11405
11406/// Everything one write decided before sending anything: see
11407/// [`GitHubProjectsSource::prepare_write`].
11408struct Prepared {
11409 /// What the write lands on.
11410 destination: WriteDestination,
11411 board: BoardFields,
11412 status_target: Option<StatusTarget>,
11413 /// The `Status` field, the option and the option's name the status lands on.
11414 // llmlint: ignore[invalid_states_unrepresentable] Exactly what `column_for` returns, which predates this change and four callers share; this record only carries it from `prepare_write` to `complete_write`, which a retyping of that return would reach and this change does not need.
11415 column: Option<(String, String, String)>,
11416 priority_write: Option<PriorityWrite>,
11417 projections: Vec<ProjectionWrite>,
11418 projected: BTreeMap<String, String>,
11419 content_kind: ContentKind,
11420 own_repository: Option<Repository>,
11421 /// The far ends of the item's native blocked-by relationships.
11422 // llmlint: ignore[invalid_states_unrepresentable] What `partition_edges` answers and `finish_write` takes as `&[String]`, both of which predate this change; this record only carries the value between them.
11423 native: Vec<String>,
11424 slot: BTreeMap<String, Value>,
11425 content: Option<String>,
11426 body: Option<String>,
11427 /// The copy origin the item records, empty for none.
11428 // llmlint: ignore[invalid_states_unrepresentable] `finish_write` and `field_writes` take the origin as `&str`, empty for none, as they did before this change; this record only carries it to them.
11429 origin: String,
11430 origin_field: Option<String>,
11431}
11432
11433/// One write a copy held back of the kinds sent together: a board field value, or a blocked-by
11434/// edge, each its input.
11435enum HeldWrite<'a> {
11436 Field(&'a Value),
11437 Edge(&'a Value),
11438}
11439
11440/// Whether `error` is GitHub refusing to file an issue on a board because it is filed there
11441/// already — what `addProjectV2ItemById` answers once `createIssue`'s `projectV2Ids` has placed
11442/// it, rather than answering with the item it holds.
11443fn already_on_the_board(error: &SourceError) -> bool {
11444 error
11445 .to_string()
11446 .contains("Content already exists in this project")
11447}
11448
11449/// Hold every alias `inputs` sent under `prefix` in `data` to answering with what its input
11450/// named — each `(root, named)` of `answers` — and refuse one that answered nothing or another
11451/// item.
11452fn check_aliased_answers(
11453 document: &str,
11454 data: &Value,
11455 prefix: &str,
11456 inputs: &[&Value],
11457 answers: &[(&str, &str)],
11458) -> Result<(), SourceError> {
11459 for (slot, input) in inputs.iter().enumerate() {
11460 for (root, named) in answers {
11461 let answered = data
11462 .pointer(&format!("/{prefix}{slot}/{root}/id"))
11463 .and_then(Value::as_str)
11464 .ok_or_else(|| SourceError::Malformed {
11465 message: format!(
11466 "GitHub answered {} with nothing at {prefix}{slot}",
11467 operation_description(document)
11468 ),
11469 })?;
11470 let asked = input.get(named).and_then(Value::as_str).unwrap_or_default();
11471 if answered != asked {
11472 return Err(SourceError::Malformed {
11473 message: format!(
11474 "GitHub answered {} at {prefix}{slot} with {answered}, which is not \
11475 {asked}, the item it was asked to write",
11476 operation_description(document)
11477 ),
11478 });
11479 }
11480 }
11481 }
11482 Ok(())
11483}
11484
11485/// `error`, which stopped a batch of creates, and — when GitHub refused to take any of them
11486/// back — each issue of `left` it created that is still in the repository, which its caller,
11487/// never handed their ids, could not name.
11488fn left_behind(error: SourceError, left: &[&str]) -> SourceError {
11489 if left.is_empty() {
11490 return error;
11491 }
11492 SourceError::Refused {
11493 message: format!(
11494 "{error}; GitHub then refused to take back {}, which this write created and which \
11495 are still in the repository; next: delete {} before writing again",
11496 left.join(", "),
11497 if left.len() == 1 { "it" } else { "them" }
11498 ),
11499 }
11500}
11501
11502/// What one write lands on: the item it updates, as it stood, or where it creates a new one.
11503enum WriteDestination {
11504 /// The item as it stood, for an update.
11505 Existing(Box<Resolved>),
11506 /// Where a new item is created.
11507 New(RepositoryTarget),
11508}
11509
11510impl WriteDestination {
11511 /// The item an update writes over, as it stood; `None` for a create.
11512 fn existing(&self) -> Option<&Resolved> {
11513 match self {
11514 Self::Existing(item) => Some(item),
11515 Self::New(_) => None,
11516 }
11517 }
11518}
11519
11520/// What a copy has held back of the items it created; see
11521/// [`GitHubProjectsSource::hold_back`].
11522#[derive(Debug, Default)]
11523enum Deferred {
11524 /// Writes are being held back, and nothing has been held yet.
11525 #[default]
11526 Nothing,
11527 /// The items held back, every one of them on `board`.
11528 Held {
11529 /// The board every one of them is on.
11530 board: BoardId,
11531 /// Each created item, in the order it was created.
11532 created: Vec<HeldBack>,
11533 },
11534}
11535
11536impl Deferred {
11537 /// Whether `content` is one of the items held back.
11538 fn holds(&self, content: &NativeId) -> bool {
11539 match self {
11540 Self::Nothing => false,
11541 Self::Held { created, .. } => created.iter().any(|held| held.content == *content),
11542 }
11543 }
11544}
11545
11546/// One created item's parts that land once [`TaskSource::finish_writes`] is called.
11547#[derive(Debug)]
11548struct HeldBack {
11549 /// The issue.
11550 content: NativeId,
11551 /// Its board field values other than its `Status`, in the order they are written.
11552 fields: Vec<(String, Value)>,
11553 /// The `Status` field, and the option and its name that the item's status lands on.
11554 // llmlint: ignore[invalid_states_unrepresentable] The same triple `column_for` returns and `Prepared::column` carries, held back unchanged until it is sent.
11555 status: Option<(String, String, String)>,
11556 /// The fields it clears after those are written.
11557 clears: Vec<String>,
11558 /// The reason it is closed for, once its option is selected; `None` leaves it open.
11559 close: Option<ClosedState>,
11560 /// The issues it is blocked by.
11561 edges: Vec<NativeId>,
11562}
11563
11564/// The member [`answer_with_errors`] carries a response's GraphQL errors under, beside its data:
11565/// no GitHub type has a field of this name.
11566const ANSWER_ERRORS: &str = "onetaskgraphErrors";
11567
11568/// A response's `data` with its GraphQL errors beside it under [`ANSWER_ERRORS`], when it has
11569/// both — or `None`, for an answer [`GitHubProjectsSource::answer`] reads as it always has.
11570///
11571/// What [`graphql::CREATE_ISSUES`] is answered with: GitHub runs a document's mutation fields in
11572/// order and answers each one that ran, so a refusal of one alias leaves the issues the others
11573/// created named in the data beside it — the ids a copy needs to take them back.
11574fn answer_with_errors(body: &str) -> Option<Value> {
11575 let parsed: Value = serde_json::from_str(body).ok()?;
11576 let errors = parsed
11577 .get("errors")?
11578 .as_array()
11579 .filter(|errors| !errors.is_empty())?;
11580 let mut data = parsed.get("data").filter(|data| data.is_object())?.clone();
11581 data[ANSWER_ERRORS] = Value::Array(errors.clone());
11582 Some(data)
11583}
11584
11585/// The `createIssue` input that files the new issue on `board` and, when `incoming` names one,
11586/// under its project — see [`GitHubProjectsSource::create_filed_issue`].
11587fn filed_input(
11588 repository: &str,
11589 board: &str,
11590 incoming: &Incoming<'_>,
11591 body: &Option<String>,
11592) -> Value {
11593 let mut input = json!({"repositoryId":repository,"title":incoming.written_title(),
11594 "body":body,"projectV2Ids":[board]});
11595 if let Some(parent) = incoming.parent {
11596 input["parentIssueId"] = json!(parent.0);
11597 }
11598 input
11599}
11600
11601/// The name of option `option` of field `field` among a board's `fields`.
11602fn option_name<'a>(fields: &'a Value, field: &str, option: &str) -> Option<&'a str> {
11603 fields
11604 .get("nodes")?
11605 .as_array()?
11606 .iter()
11607 .find(|node| node.get("id").and_then(Value::as_str) == Some(field))?
11608 .get("options")?
11609 .as_array()?
11610 .iter()
11611 .find(|candidate| candidate.get("id").and_then(Value::as_str) == Some(option))?
11612 .get("name")?
11613 .as_str()
11614}
11615
11616/// What a write needs to know of one far end it names: which kind of item it is, and whether
11617/// it is an issue a native relationship can name.
11618struct FarEnd {
11619 kind: BoardKind,
11620 content_kind: ContentKind,
11621}
11622
11623/// Whether the issue one write reconciles was created by that write or was already there.
11624#[derive(Clone, Copy, PartialEq, Eq)]
11625enum Issue<'a> {
11626 /// Created by this write, so it holds no relationships yet.
11627 Created,
11628 /// On the board before this write, holding whatever relationships it holds — the far
11629 /// ends of its whole `blockedBy`, when the read that reached it carried them.
11630 Existing(Option<&'a [Value]>),
11631}
11632
11633/// What resolving one node id reached; see [`GitHubProjectsSource::reach`].
11634enum Reached {
11635 /// An issue this board holds, resolved into everything this source reports about it.
11636 Held(Box<Resolved>),
11637 /// Nothing this board holds: no such node, or a node on some other board.
11638 Nothing,
11639 /// A board draft, which [`graphql::ISSUE`] reaches and reads nothing of, so it is read
11640 /// again by [`GitHubProjectsSource::draft_by_id`].
11641 Draft,
11642}
11643
11644/// What GitHub says when a string is not a node id it can resolve.
11645///
11646/// Matched because it is the ordinary answer to a project selector naming a project by its
11647/// *name*, and reporting that as a failure would make naming one impossible. It is read
11648/// off the refusal GitHub sent, never guessed from the shape of the string: this source
11649/// does not define the syntax of a GitHub node id and would be wrong about it.
11650const UNRESOLVABLE_NODE: &str = "could not resolve to a node";
11651
11652/// `error` with `note` added to the end of what it says, its kind and every other member
11653/// unchanged — so a caller still branches on the failure that happened, and reads beside it
11654/// what that failure left behind.
11655fn noting(error: SourceError, note: &str) -> SourceError {
11656 match error {
11657 SourceError::Config { message } => SourceError::Config {
11658 message: message + note,
11659 },
11660 SourceError::Auth { message } => SourceError::Auth {
11661 message: message + note,
11662 },
11663 SourceError::Refused { message } => SourceError::Refused {
11664 message: message + note,
11665 },
11666 SourceError::RateLimited {
11667 retry_after_seconds,
11668 message,
11669 } => SourceError::RateLimited {
11670 retry_after_seconds,
11671 message: Some(message.unwrap_or_default() + note),
11672 },
11673 SourceError::Unavailable { message } => SourceError::Unavailable {
11674 message: message + note,
11675 },
11676 SourceError::Malformed { message } => SourceError::Malformed {
11677 message: message + note,
11678 },
11679 }
11680}
11681
11682/// The variables of one [`graphql::ISSUE_COPY_DETAILS`] request, as [`detail_batch`] binds its
11683/// own: a slot `batch` has no id for is bound to its last id, read again at no added price.
11684fn copy_detail_batch(batch: &[NativeId], comments: &PageRequest, children: bool) -> Value {
11685 let mut variables = serde_json::Map::new();
11686 for slot in 0..COPY_DETAIL_BATCH {
11687 let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
11688 variables.insert(format!("id{slot}"), json!(id));
11689 }
11690 variables.insert("project".to_owned(), json!(batch.first().map(|id| &id.0)));
11691 variables.insert("children".to_owned(), json!(children));
11692 variables.insert("first".to_owned(), json!(comments.limit.min(MAX_PAGE_SIZE)));
11693 variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
11694 variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
11695 variables.insert("duplicates".to_owned(), json!(true));
11696 Value::Object(variables)
11697}
11698
11699/// The variables of one [`graphql::ISSUE_DETAILS`] request over `batch` — at most
11700/// [`DETAIL_BATCH`] ids — each item with the first page of its comments when `comments` asks
11701/// for them.
11702///
11703/// The document is fixed-size, so a slot `batch` has no id for is bound to its last id, which
11704/// is read again at no added price.
11705fn detail_batch(batch: &[NativeId], comments: Option<&PageRequest>) -> Value {
11706 let mut variables = serde_json::Map::new();
11707 for slot in 0..DETAIL_BATCH {
11708 let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
11709 variables.insert(format!("id{slot}"), json!(id));
11710 }
11711 variables.insert(
11712 "first".to_owned(),
11713 json!(comments.map_or(MAX_PAGE_SIZE, |page| page.limit.min(MAX_PAGE_SIZE))),
11714 );
11715 variables.insert("comments".to_owned(), json!(comments.is_some()));
11716 variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
11717 variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
11718 variables.insert("duplicates".to_owned(), json!(true));
11719 Value::Object(variables)
11720}
11721
11722/// Whether this refusal is GitHub saying the id names no node at all.
11723fn unresolvable_node(error: &SourceError) -> bool {
11724 matches!(error, SourceError::Refused { message }
11725 if message.to_ascii_lowercase().contains(UNRESOLVABLE_NODE))
11726}
11727
11728/// One project name, as a search qualifier which filters on it at the server.
11729///
11730/// Quoted so the whole title is one phrase rather than a bag of words, with the two
11731/// characters GitHub's own quoting grammar gives a meaning inside a quoted phrase escaped
11732/// the way it documents. A title matched here is still compared for equality afterwards:
11733/// the qualifier narrows what the server sends, and this source decides what it names.
11734fn title_qualifier(name: &str) -> String {
11735 format!("in:title {}", quoted(name))
11736}
11737
11738/// `value` as one quoted phrase of a GitHub search or a board filter, with the two
11739/// characters GitHub's quoting grammar gives a meaning inside a quoted phrase escaped the way
11740/// it documents — so a value holding a qualifier's spelling is searched for rather than
11741/// obeyed.
11742fn quoted(value: &str) -> String {
11743 let escaped = value.replace('\\', "\\\\").replace('"', "\\\"");
11744 format!("\"{escaped}\"")
11745}
11746
11747/// The search qualifier for the issues updated at or after `since`.
11748///
11749/// Written to the second, rounded down, which can only widen what the search returns.
11750fn updated_qualifier(since: DateTime<Utc>) -> String {
11751 format!("updated:>={}", since.format("%Y-%m-%dT%H:%M:%S+00:00"))
11752}
11753
11754/// The search terms that narrow a board-scoped issue search to a task query's text and
11755/// metadata predicates, or `None` when it carries neither.
11756///
11757/// The text is one quoted phrase, searched `in:title`, `in:body` or both as its fields say,
11758/// and each metadata value is one more quoted phrase, which GitHub finds in the body because
11759/// its index covers the metadata comment the value is stored in. GitHub ANDs the phrases and
11760/// matches each in any field the `in:` qualifier names, so a query naming a title search and
11761/// a metadata value searches both fields for both — wider than asked, never narrower, and
11762/// every candidate is confirmed in process afterwards.
11763///
11764/// **This narrows a text search, and that is this source's declared semantics.** GitHub
11765/// matches whole tokens where a substring rule would match inside a word, so an item holding
11766/// the text only inside a longer word is not returned. A text of nothing but whitespace
11767/// matches every item, so it narrows nothing and is not sent.
11768fn narrowing_qualifiers(query: &TaskQuery) -> Option<String> {
11769 let text = query
11770 .text
11771 .as_ref()
11772 .filter(|text| !text.terms.trim().is_empty());
11773 if text.is_none() && query.metadata.is_empty() {
11774 return None;
11775 }
11776 let (title, body) = match text.map(|text| text.fields) {
11777 None => (false, true),
11778 Some(TextFields::Title) => (true, !query.metadata.is_empty()),
11779 Some(TextFields::Content) => (false, true),
11780 Some(TextFields::TitleOrContent) => (true, true),
11781 };
11782 let fields = match (title, body) {
11783 (true, true) => "in:title,body",
11784 (true, false) => "in:title",
11785 _ => "in:body",
11786 };
11787 let phrases = text
11788 .map(|text| text.terms.clone())
11789 .into_iter()
11790 .chain(
11791 query
11792 .metadata
11793 .iter()
11794 .map(|wanted| as_stored(wanted.value())),
11795 )
11796 .map(|phrase| quoted(&phrase))
11797 .collect::<Vec<_>>();
11798 Some(format!("{fields} {}", phrases.join(" ")))
11799}
11800
11801/// The search terms that narrow a board-scoped issue search to a project or document query's
11802/// text, or `None` when it has none or a blank one: the phrase, in the fields, a task query
11803/// carrying that text alone is sent as by [`narrowing_qualifiers`].
11804fn text_qualifiers(text: Option<&TextQuery>) -> Option<String> {
11805 narrowing_qualifiers(&TaskQuery {
11806 text: text.cloned(),
11807 ..TaskQuery::default()
11808 })
11809}
11810
11811/// Refuses a project or document query's text GitHub's issue search cannot find, before
11812/// anything is asked of GitHub, on exactly the terms [`refuse_unsearchable`] refuses a task
11813/// query's.
11814fn refuse_unsearchable_text(text: Option<&TextQuery>) -> Result<(), SourceError> {
11815 refuse_unsearchable(&TaskQuery {
11816 text: text.cloned(),
11817 ..TaskQuery::default()
11818 })
11819}
11820
11821/// Refuses a task query naming a text or a metadata value GitHub's issue search cannot find,
11822/// before anything is asked of GitHub.
11823///
11824/// GitHub's index holds words, so a phrase with no letter or digit names none to find, and no
11825/// bounded query answers it: sent, GitHub's answer to it is nothing this source may rely on;
11826/// left out, the search is every issue of the board. So this source says it cannot answer
11827/// rather than reading the board or answering nothing. A blank text is not refused: it narrows
11828/// nothing GitHub could search for, and keeps the board read it always had.
11829fn refuse_unsearchable(query: &TaskQuery) -> Result<(), SourceError> {
11830 const WHY: &str = "GitHub's issue search indexes words, so it cannot answer a value with no \
11831 letter or digit with a bounded query";
11832 if let Some(text) = &query.text
11833 && !text.terms.trim().is_empty()
11834 && !has_words(&text.terms)
11835 {
11836 return Err(SourceError::Refused {
11837 message: format!(
11838 "cannot search for the text {:?}: {WHY}; search for a text holding a letter or a digit",
11839 text.terms
11840 ),
11841 });
11842 }
11843 if let Some(wanted) = query
11844 .metadata
11845 .iter()
11846 .find(|wanted| !has_words(wanted.value()))
11847 {
11848 return Err(SourceError::Refused {
11849 message: format!(
11850 "cannot filter by the metadata value {:?} at {:?}: {WHY}; filter by a value holding a letter or a digit",
11851 wanted.value(),
11852 std::iter::once(wanted.key())
11853 .chain(wanted.path().iter().map(String::as_str))
11854 .collect::<Vec<_>>()
11855 .join("/"),
11856 ),
11857 });
11858 }
11859 Ok(())
11860}
11861
11862/// Whether GitHub's index could hold a word of `phrase`: whether it has a letter or a digit.
11863fn has_words(phrase: &str) -> bool {
11864 phrase.chars().any(char::is_alphanumeric)
11865}
11866
11867/// `value` spelled the way the metadata slot stores it: as the inside of its JSON string.
11868///
11869/// What GitHub indexes is the slot's JSON text, so a value holding a character JSON escapes —
11870/// a newline, a tab, a quote — is found by the escape the body holds and not by the character,
11871/// which GitHub's word match would read as different words.
11872fn as_stored(value: &str) -> String {
11873 let encoded = Value::String(value.to_owned()).to_string();
11874 encoded[1..encoded.len() - 1].to_owned()
11875}
11876
11877/// The one narrower question a task query carrying a text, metadata or origin predicate is
11878/// sent as.
11879enum Narrowing {
11880 /// Every carrier of this origin: [`graphql::ORIGIN_LOOKUP`].
11881 Origin(String),
11882 /// The board-scoped issue search narrowed by these qualifiers.
11883 Search(String),
11884}
11885
11886impl Narrowing {
11887 /// What this question is remembered under for the length of one command.
11888 fn key(&self) -> String {
11889 match self {
11890 Self::Origin(origin) => format!("origin {origin}"),
11891 Self::Search(also) => format!("search {also}"),
11892 }
11893 }
11894}
11895
11896/// Where one connection of [`graphql::ORIGIN_LOOKUP`] resumes.
11897enum Resumed {
11898 /// It reported another page, which starts after this cursor.
11899 More(String),
11900 /// It has ended. Sending this cursor again — the page's own end when it had one, and
11901 /// otherwise the cursor it was reached from — answers an empty page, so the one document
11902 /// can go on walking the other connection.
11903 Ended(Option<String>),
11904}
11905
11906impl Resumed {
11907 /// Whether the connection has another page.
11908 const fn has_more(&self) -> bool {
11909 matches!(self, Self::More(_))
11910 }
11911
11912 /// The cursor to send this connection next.
11913 fn cursor(self) -> Option<String> {
11914 match self {
11915 Self::More(next) => Some(next),
11916 Self::Ended(last) => last,
11917 }
11918 }
11919}
11920
11921/// Where `connection`, reached from `after`, resumes — refused when it reports another page
11922/// with no cursor to it, or from a cursor that does not advance.
11923fn resumed(connection: &Value, after: Option<&str>) -> Result<Resumed, SourceError> {
11924 let info = connection
11925 .get("pageInfo")
11926 .ok_or_else(|| SourceError::Malformed {
11927 message: "GitHub connection has no pageInfo".into(),
11928 })?;
11929 let end = optional_str(info, "endCursor")?;
11930 if required_bool(info, "hasNextPage")? {
11931 let next = end.ok_or_else(|| SourceError::Malformed {
11932 message: "GitHub connection reports another page and no endCursor".into(),
11933 })?;
11934 validate_cursor_progress(after, next)?;
11935 return Ok(Resumed::More(next.to_owned()));
11936 }
11937 Ok(Resumed::Ended(
11938 end.map(str::to_owned).or_else(|| after.map(str::to_owned)),
11939 ))
11940}
11941
11942/// The board, and every item on it this source reports.
11943#[derive(Clone)]
11944struct Board {
11945 id: String,
11946 fields: Value,
11947 items: Vec<Resolved>,
11948}
11949
11950/// What a write needs of the board and nothing more: its node id and its field
11951/// definitions, in the shape a read of the board's own `fields` gives them.
11952///
11953/// Deliberately no items. A write decides which item it writes, which parent it files
11954/// under and which far ends it names by reading each of them by its own id; this is the
11955/// half of the board those reads cannot carry, and holding no item is what keeps it from
11956/// ever being asked whether an item is there.
11957#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
11958struct BoardFields {
11959 id: BoardId,
11960 fields: Value,
11961}
11962
11963/// A board's node id: what a field write and `addProjectV2ItemById` address.
11964///
11965/// Never blank, because a blank one addresses no board — so an id GitHub answers blank is
11966/// refused where it is read, and one an item names blank is read as not named at all.
11967#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
11968#[serde(try_from = "String")]
11969struct BoardId(String);
11970
11971impl TryFrom<String> for BoardId {
11972 type Error = String;
11973
11974 /// The same refusal [`BoardId::parse`] makes, for an id read back from a kept context.
11975 fn try_from(id: String) -> Result<Self, Self::Error> {
11976 Self::parse(&id).map_err(|error| error.to_string())
11977 }
11978}
11979
11980/// Where one write left its item, for the record the rest of the command reads it out of.
11981///
11982/// A named record rather than a tuple because the update arm and the create arm each fill
11983/// all four, and two `Option`s of different meaning side by side in a tuple are two
11984/// positions a reader has to count.
11985struct Landed {
11986 /// The issue's own node id, which is the [`NativeId`] this source reports.
11987 content_id: NativeId,
11988 /// The board item's id, which is what a field write addresses — `None` for an issue its
11989 /// create filed on the board itself, whose board item GitHub adds after answering and
11990 /// [`TaskSource::finish_writes`] reads.
11991 // llmlint: ignore[invalid_states_unrepresentable] This field and the one below are `Resolved::item_id` and `Resolved::url` carried out of one call: the update arm assigns them from an existing `Resolved` and the whole record is assigned straight back into one. A newtype introduced here alone would be wrapped at both of those boundaries and unwrapped at every use, and would make this private record disagree with the type the same values have on the struct they come from and return to. Where the board item id gets a newtype is on `Resolved`, which is the contract's own shape and not this change's to move.
11992 item_id: Option<String>,
11993 /// The web address GitHub gave the issue, when it gave one.
11994 // llmlint: ignore[invalid_states_unrepresentable] The answer `Resolved::url` and the contract's `Task::url` already record: a web address this source never parses, resolves or compares — it reads GitHub's string and hands it back, and `Location::Url` is where the contract gives it a shape. Validating it here would have this plugin decide what GitHub may call an address.
11995 url: Option<String>,
11996 /// The issue's number on its repository, when GitHub reported one.
11997 number: Option<u64>,
11998}
11999
12000impl BoardId {
12001 fn parse(id: &str) -> Result<Self, SourceError> {
12002 if id.trim().is_empty() {
12003 return Err(SourceError::Malformed {
12004 message: "GitHub named a board with a blank node id".into(),
12005 });
12006 }
12007 Ok(Self(id.to_owned()))
12008 }
12009
12010 fn as_str(&self) -> &str {
12011 &self.0
12012 }
12013}
12014
12015impl Board {
12016 fn field<'a>(fields: &'a Value, name: &str) -> Result<Option<&'a Value>, SourceError> {
12017 complete_connection(fields, "project fields", NESTED_PAGE_SIZE)?;
12018 let nodes = fields
12019 .get("nodes")
12020 .and_then(Value::as_array)
12021 .ok_or_else(|| SourceError::Malformed {
12022 message: "GitHub project fields.nodes is not an array".into(),
12023 })?;
12024 Ok(nodes
12025 .iter()
12026 .find(|field| field.get("name").and_then(Value::as_str) == Some(name)))
12027 }
12028}
12029
12030/// One board item, resolved into everything this source reports about it.
12031///
12032/// Serializable because it is what [`TaskSource::write_context`] hands the store's cache and
12033/// [`TaskSource::prime_write`] takes back — see [`expected`].
12034#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
12035struct Resolved {
12036 item_id: String,
12037 id: NativeId,
12038 content_kind: ContentKind,
12039 kind: BoardKind,
12040 title: String,
12041 body: Option<String>,
12042 /// The body exactly as GitHub holds it, metadata slot and all, which is what a write
12043 /// that changes the slot alone has to keep byte for byte outside it.
12044 raw_body: Option<String>,
12045 status: Status,
12046 /// The name of the board `Status` option this item sits in, as the board spells it.
12047 option: Option<String>,
12048 /// What its `Priority` field says, read through this instance's mapping.
12049 priority: HeldPriority,
12050 /// Whether this item's issue is closed. A draft has no such state and is never closed.
12051 closed: bool,
12052 /// GitHub's reason for a closed issue's closure, as it spells it — `None` for an open
12053 /// issue, a draft, and an issue closed before GitHub recorded reasons.
12054 #[serde(default)]
12055 reason: Option<String>,
12056 /// The tasks this one delivers, read out of its slot. Empty for anything not a task.
12057 delivers: Vec<TaskRef>,
12058 /// Every task that delivers this one, read out of its slot. Empty for anything not a
12059 /// task.
12060 delivered_by: Vec<TaskRef>,
12061 labels: Vec<Label>,
12062 parent: Option<NativeId>,
12063 // 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.
12064 origin: Option<String>,
12065 /// What each board text field this instance projects metadata onto holds for this item,
12066 /// by field name — only the fields holding a value, an empty text included. What a write compares the
12067 /// item's metadata against, so a field already holding its value is not written again;
12068 /// never read back as metadata, which the slot alone reports.
12069 projected: BTreeMap<String, String>,
12070 /// The issue's own number on its repository, as GitHub reports it.
12071 ///
12072 /// `None` in exactly two cases: a draft, which has no number at all — `DraftIssue`
12073 /// declares none, and a draft is not filed in a repository to be numbered by one — and
12074 /// an issue this run created whose creating mutation answered without one, which is a
12075 /// response GitHub's own schema says cannot happen and which a landed write is not
12076 /// worth failing over. An `Issue` read off the board always has one.
12077 number: Option<u64>,
12078 url: Option<String>,
12079 created_at: Option<DateTime<Utc>>,
12080 updated_at: Option<DateTime<Utc>>,
12081 /// When the board item itself last changed — what a board field write moves — when the
12082 /// read that reached it said.
12083 item_updated_at: Option<DateTime<Utc>>,
12084 /// How many comments the issue holds, when the read that reached it said.
12085 comment_count: Option<u64>,
12086 /// How many relationship events the issue's timeline holds — every dependency, as
12087 /// `blockedBy` or `blocking`, and every project membership, as its parent, added or
12088 /// removed — when the read that reached it said.
12089 relationship_events: Option<u64>,
12090 /// The server's clock on the response that read it — its `Date` header — when the
12091 /// response carried one.
12092 served_at: Option<DateTime<Utc>>,
12093 /// The digest of its [`change_token::Facets`] — its own kind, its board field values and
12094 /// its labels — when the read that reached it carried all three whole.
12095 #[serde(default)]
12096 facets: Option<change_token::FacetsDigest>,
12097 own_repository: Option<Repository>,
12098 repositories: Vec<Repository>,
12099 classification: Classification,
12100 slot: BTreeMap<String, Value>,
12101 /// The node id of the board this item sits on, when the read that reached it said.
12102 board_id: Option<String>,
12103 /// The definition of every board field this item holds a value of, in the shape a read
12104 /// of the board's own `fields` gives one.
12105 ///
12106 /// Only the fields this item has a value in: a field it holds nothing of is not here,
12107 /// which says nothing about whether the board has it.
12108 fields: Vec<Value>,
12109 /// Every field the board this item sits on defines, as its own read of the board's
12110 /// `fields` gives them — when the read that reached the item carried them, which a read
12111 /// of it by its own id does. What a write of it needs of the board, then, needs no read
12112 /// of the board.
12113 board_fields: Option<Value>,
12114 /// The far ends of this issue's whole `blockedBy` connection, each as a dependency read
12115 /// selects one — when the read that reached it carried the connection to its end, which a
12116 /// read of it by its own id does for any issue blocked by no more than a page. What a
12117 /// write reconciles that relationship against, and what a read of its forward edges in
12118 /// the same command answers with.
12119 blocked_by: Option<Vec<Value>>,
12120}
12121
12122impl Resolved {
12123 /// The board this item's own read names it on, when that read named one this source can
12124 /// address.
12125 fn named_board(&self) -> Option<BoardId> {
12126 self.board_id
12127 .as_deref()
12128 .and_then(|id| BoardId::parse(id).ok())
12129 }
12130
12131 /// The board's id and every field it defines, when the read that reached this item
12132 /// carried both — which a read of it by its own id does.
12133 fn carried_board(&self) -> Option<BoardFields> {
12134 Some(BoardFields {
12135 id: self.named_board()?,
12136 fields: self.board_fields.clone()?,
12137 })
12138 }
12139
12140 /// Whether this item holds a value of the board field called `name`, and so carries
12141 /// that field's definition. `false` says nothing about whether the board has the field.
12142 fn defines(&self, name: &str) -> bool {
12143 self.fields
12144 .iter()
12145 .any(|field| field.get("name").and_then(Value::as_str) == Some(name))
12146 }
12147
12148 /// The metadata a caller sees: their own keys, plus the copy origin this source keeps
12149 /// in a field of its own, and none of the five keys that are only an encoding.
12150 ///
12151 /// The two delivery keys are left out for every kind, not only for a task: they are
12152 /// the encoding of [`Task::delivers`] and [`Task::delivered_by`], and a project or a
12153 /// document carrying one holds nothing a caller's own metadata could mean by it.
12154 fn metadata(&self) -> BTreeMap<String, Value> {
12155 let mut metadata = self.slot.clone();
12156 metadata.remove(Repository::METADATA_KEY);
12157 metadata.remove(DependencyEdge::RECORDED_KEY);
12158 metadata.remove(ItemKind::METADATA_KEY);
12159 metadata.remove(TaskRef::DELIVERS_KEY);
12160 metadata.remove(TaskRef::DELIVERED_BY_KEY);
12161 metadata.remove(Classification::METADATA_KEY);
12162 // The board field is the origin, and the body's copy of it is only a mirror for the
12163 // issue search to find: an item whose field holds none has none, whatever its body
12164 // says, so no reader ever sees two answers.
12165 metadata.remove(ORIGIN_KEY);
12166 if let Some(origin) = &self.origin {
12167 metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin.clone()));
12168 }
12169 metadata
12170 }
12171
12172 /// Where this item is, as a link a reader can open.
12173 ///
12174 /// A board is a hosted place and every issue on it has a web address, so that address
12175 /// is what "where is this?" means here — and [`Location::Url`] is what says which kind
12176 /// of place it is, so a reader knows to open it rather than to read a file out. It
12177 /// does not replace or derive from `url`: the field goes on reporting exactly what it
12178 /// reported before, and this says what that address *is*.
12179 ///
12180 /// An item GitHub gave no `url` for — a draft has none — reports no location at all
12181 /// rather than a third variant, which is the contract's "the source did not say". An
12182 /// issue this run created is not one of those: its address comes back from the
12183 /// creating mutation, so it is somewhere a reader can open from the moment it exists
12184 /// rather than from whenever the board read catches up.
12185 fn location(&self) -> Option<Location> {
12186 self.url.clone().map(Location::Url)
12187 }
12188
12189 /// The short handle this board's backend shows people for a task: the issue's number
12190 /// alone, as a decimal string.
12191 ///
12192 /// The number alone rather than `owner/repo#1043`, because that is the contract's
12193 /// value for this backend. A draft has no number and so no handle, which is the
12194 /// contract's *absent* rather than a handle of some other shape — and the native
12195 /// [`Task::id`] here is the issue's GraphQL node id, which this neither replaces nor
12196 /// derives from.
12197 fn key(&self) -> Option<String> {
12198 self.number.map(|number| number.to_string())
12199 }
12200
12201 /// This item's change token — see [`change_token`] — or `None` when the read that
12202 /// reached it did not carry all six of its parts, which a draft never does, did not say
12203 /// when GitHub served it, or found it changed too recently to have settled.
12204 fn version(&self) -> Option<String> {
12205 match self.content_kind {
12206 ContentKind::Issue => {
12207 let (issue, item) = (self.updated_at?, self.item_updated_at?);
12208 let parts = change_token::Parts {
12209 issue,
12210 item,
12211 comments: self.comment_count?,
12212 relationships: self.relationship_events?,
12213 facets: self.facets.clone()?,
12214 repository: self.own_repository.clone()?,
12215 };
12216 change_token::settled(issue, item, self.served_at?)
12217 .then(|| change_token::spell(&parts))
12218 }
12219 ContentKind::DraftIssue => None,
12220 }
12221 }
12222
12223 /// Whether its `Priority` field holds a value at all, mapped or not.
12224 fn holds_priority(&self) -> bool {
12225 self.priority != HeldPriority::Read(Priority::None)
12226 }
12227
12228 /// The task this item is.
12229 ///
12230 /// Fails for an item whose `Priority` field holds an option the mapping does not name:
12231 /// reading that as a level would be a guess, and reading it as `none` would let the next
12232 /// copy clear a priority a person set.
12233 fn task(&self) -> Result<Task, SourceError> {
12234 let priority = match &self.priority {
12235 HeldPriority::Read(priority) => *priority,
12236 HeldPriority::Unmapped(option) => {
12237 return Err(SourceError::Malformed {
12238 message: format!(
12239 "task {}{} sits in the board {PRIORITY_FIELD} option {option:?}, which \
12240 this source's priority_mapping does not name, so its priority cannot be \
12241 read; next: name {option:?} under priority_mapping, or move the item to \
12242 a mapped option",
12243 self.id,
12244 self.number
12245 .map(|number| format!(" (#{number})"))
12246 .unwrap_or_default()
12247 ),
12248 });
12249 }
12250 };
12251 Ok(Task {
12252 version: self.version(),
12253 id: self.id.clone(),
12254 key: self.key(),
12255 title: self.title.clone(),
12256 content: self.body.clone(),
12257 status: self.status.clone(),
12258 priority,
12259 labels: self.labels.clone(),
12260 project: self.parent.clone(),
12261 url: self.url.clone(),
12262 location: self.location(),
12263 created_at: self.created_at,
12264 updated_at: self.updated_at,
12265 metadata: self.metadata(),
12266 repositories: self.repositories.clone(),
12267 delivers: self.delivers.clone(),
12268 delivered_by: self.delivered_by.clone(),
12269 classification: self.classification,
12270 })
12271 }
12272
12273 fn project(&self) -> Project {
12274 Project {
12275 version: self.version(),
12276 id: self.id.clone(),
12277 title: self.title.clone(),
12278 content: self.body.clone(),
12279 status: self.status.clone(),
12280 labels: self.labels.clone(),
12281 url: self.url.clone(),
12282 location: self.location(),
12283 created_at: self.created_at,
12284 updated_at: self.updated_at,
12285 metadata: self.metadata(),
12286 repositories: self.repositories.clone(),
12287 classification: self.classification,
12288 }
12289 }
12290
12291 /// The same issue as a document: the project it is filed under, and no status and no
12292 /// dependencies, because a document is not work.
12293 fn document(&self) -> Document {
12294 Document {
12295 version: self.version(),
12296 id: self.id.clone(),
12297 title: self.title.clone(),
12298 content: self.body.clone(),
12299 project: self.parent.clone(),
12300 labels: self.labels.clone(),
12301 url: self.url.clone(),
12302 location: self.location(),
12303 created_at: self.created_at,
12304 updated_at: self.updated_at,
12305 metadata: self.metadata(),
12306 repositories: self.repositories.clone(),
12307 classification: self.classification,
12308 }
12309 }
12310}
12311
12312/// Where one targeted update moves an item's status, and which of its two halves move.
12313struct StatusMove {
12314 /// The board the item's `Status` field is on.
12315 board: BoardId,
12316 /// The `Status` field's id.
12317 field: String,
12318 /// The option's id.
12319 option: String,
12320 /// The option's name, as the board spells it.
12321 name: String,
12322 /// What the status asks of the issue's state.
12323 target: StatusTarget,
12324 /// The status the item reads as once it is there.
12325 landed: Status,
12326 /// Which of the status's two halves differ from what the item holds.
12327 moves: Moves,
12328}
12329
12330/// Which halves of an item's status one targeted update moves: its `Status` option, the open or
12331/// closed state of its issue, or both. A status neither half of which differs is no move at all,
12332/// and is not a value of this type.
12333#[derive(Clone, Copy, PartialEq, Eq)]
12334enum Moves {
12335 /// The option alone.
12336 Option,
12337 /// The issue's state alone: open, closed, or closed with another reason.
12338 State,
12339 /// Both.
12340 Both,
12341}
12342
12343impl Moves {
12344 /// What differs, or `None` when nothing does.
12345 const fn of(option: bool, state: bool) -> Option<Self> {
12346 match (option, state) {
12347 (true, true) => Some(Self::Both),
12348 (true, false) => Some(Self::Option),
12349 (false, true) => Some(Self::State),
12350 (false, false) => None,
12351 }
12352 }
12353
12354 /// Whether the option moves.
12355 const fn option(self) -> bool {
12356 matches!(self, Self::Option | Self::Both)
12357 }
12358
12359 /// Whether the issue's state moves.
12360 const fn state(self) -> bool {
12361 matches!(self, Self::State | Self::Both)
12362 }
12363}
12364
12365/// What one write is, and the status that comes with being it.
12366///
12367/// One value rather than a [`BoardKind`] beside an `Option<Status>`: a document has no
12368/// status and a task or a project always has one, so "a document carrying a status" and
12369/// "a task carrying none" are states a write cannot be in rather than states every use
12370/// site below has to defend against.
12371enum Written<'a> {
12372 /// A document, which is not work and so has no status at all.
12373 Document,
12374 /// A task or a project, and the status it is being written with.
12375 Work(ItemKind, &'a Status),
12376}
12377
12378impl Written<'_> {
12379 /// Which of the board's three kinds this write is.
12380 const fn kind(&self) -> BoardKind {
12381 match self {
12382 Self::Document => BoardKind::Document,
12383 Self::Work(kind, _) => BoardKind::Work(*kind),
12384 }
12385 }
12386
12387 /// The status this write carries. A document carries none, so a write of one says
12388 /// nothing about the issue's open or closed state and selects no board `Status`
12389 /// option.
12390 const fn status(&self) -> Option<&Status> {
12391 match self {
12392 Self::Document => None,
12393 Self::Work(_, status) => Some(status),
12394 }
12395 }
12396
12397 /// The status this write carries with the kind whose half of `status_mapping` it is
12398 /// written through.
12399 const fn work_status(&self) -> Option<(ItemKind, &Status)> {
12400 match self {
12401 Self::Document => None,
12402 Self::Work(kind, status) => Some((*kind, status)),
12403 }
12404 }
12405}
12406
12407/// The item being written, in the one shape all three write methods reach.
12408struct Incoming<'a> {
12409 written: Written<'a>,
12410 /// The title a person wrote. A document's goes onto the issue with
12411 /// [`DESIGN_TITLE_PREFIX`] put back, so a round trip returns the title that went in.
12412 title: &'a str,
12413 content: Option<&'a str>,
12414 assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
12415 labels: &'a [Label],
12416 metadata: &'a BTreeMap<String, Value>,
12417 repositories: &'a [Repository],
12418 /// Recorded in the slot while private, and nowhere while public.
12419 classification: Classification,
12420 parent: Option<&'a NativeId>,
12421 /// [`Task::delivers`], already checked. Empty for a project or a document, which is
12422 /// what keeps either key out of their slot.
12423 delivers: &'a [TaskRef],
12424 /// [`Task::delivered_by`], already checked. Empty for a project or a document.
12425 delivered_by: &'a [TaskRef],
12426 /// [`Task::priority`], for a task written to an instance that holds one; `None` for a
12427 /// project, a document, and every write to an instance with no `priority_mapping` —
12428 /// which is what keeps such a write's requests exactly what they were before.
12429 priority: Option<Priority>,
12430}
12431
12432/// What one write does to one projected metadata text field, by the field's id.
12433#[derive(Debug, Clone)]
12434enum ProjectionWrite {
12435 /// Write this text.
12436 Set {
12437 field: String, // llmlint: ignore[invalid_states_unrepresentable] A private opaque GraphQL field id passed straight back to GitHub, as `PriorityWrite`'s are; GitHub publishes no grammar for it.
12438 text: String,
12439 },
12440 /// Clear the field's value.
12441 Clear { field: String }, // llmlint: ignore[invalid_states_unrepresentable] As above.
12442}
12443
12444impl ProjectionWrite {
12445 /// Add this write to a field write's list of values and list of clears.
12446 fn join<'a>(&'a self, values: &mut Vec<(String, Value)>, clears: &mut Vec<&'a str>) {
12447 match self {
12448 Self::Set { field, text } => values.push((field.clone(), json!({"text": text}))),
12449 Self::Clear { field } => clears.push(field),
12450 }
12451 }
12452}
12453
12454/// What one write does to an item's `Priority` field.
12455enum PriorityWrite {
12456 /// Select this option of this field.
12457 Select {
12458 /// The `Priority` field's id.
12459 field: String,
12460 /// The mapped option's id.
12461 option: String,
12462 },
12463 /// Clear the field's value, which is what `none` is.
12464 Clear {
12465 /// The `Priority` field's id.
12466 field: String,
12467 },
12468}
12469
12470impl Incoming<'_> {
12471 /// The title this write puts on the issue.
12472 fn written_title(&self) -> String {
12473 match self.written {
12474 Written::Document => format!("{DESIGN_TITLE_PREFIX}{}", self.title),
12475 Written::Work(..) => self.title.to_owned(),
12476 }
12477 }
12478}
12479
12480#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
12481enum ContentKind {
12482 DraftIssue,
12483 Issue,
12484}
12485
12486/// What one board issue is: a document, or the work an [`ItemKind`] names.
12487///
12488/// A type of this source's own rather than an `ItemKind` with a third variant, because
12489/// `ItemKind` names what a dependency endpoint points at and nothing may point at a
12490/// document — the contract keeps a document out of that enum deliberately. Holding the
12491/// board's three answers in one value is what makes every place that asks "which is this?"
12492/// answer all three, rather than a `document: bool` beside a `kind` that means nothing for
12493/// two thirds of the board.
12494#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
12495enum BoardKind {
12496 /// An issue whose title begins [`DESIGN_TITLE_PREFIX`].
12497 Document,
12498 /// Every other issue, and every draft.
12499 Work(ItemKind),
12500}
12501
12502impl BoardKind {
12503 /// The kind of an item titled `title`, filed under a project or not, holding `sub_issues`
12504 /// sub-issues and carrying the kind marker `marked` in its slot.
12505 ///
12506 /// The design prefix is read *first*, before either of the two rules that separate a
12507 /// project from a task. A document is not work whatever sub-issues it has and whatever
12508 /// marker it carries, and reading the prefix later would make a design issue with none of
12509 /// either an empty project. Being a sub-issue wins outright over the rest, and no marker
12510 /// overrides it: an issue filed under a project is that project's task even when it has
12511 /// sub-issues of its own.
12512 fn of(title: &str, filed: bool, sub_issues: u64, marked: Option<ItemKind>) -> Self {
12513 if title.starts_with(DESIGN_TITLE_PREFIX) {
12514 Self::Document
12515 } else if filed {
12516 Self::Work(ItemKind::Task)
12517 } else if sub_issues > 0 || marked == Some(ItemKind::Project) {
12518 Self::Work(ItemKind::Project)
12519 } else {
12520 Self::Work(ItemKind::Task)
12521 }
12522 }
12523
12524 /// What the change token's facets call this kind.
12525 const fn facet(self) -> change_token::FacetKind {
12526 match self {
12527 Self::Document => change_token::FacetKind::Document,
12528 Self::Work(ItemKind::Project) => change_token::FacetKind::Project,
12529 Self::Work(ItemKind::Task) => change_token::FacetKind::Task,
12530 }
12531 }
12532
12533 /// Whose half of `status_mapping` an item of this kind reads its status through. A
12534 /// document has no status of its own, so the task half stands in for whatever the issue
12535 /// holds; nothing reports it.
12536 const fn status_kind(self) -> ItemKind {
12537 match self {
12538 Self::Document => ItemKind::Task,
12539 Self::Work(kind) => kind,
12540 }
12541 }
12542
12543 /// How a refusal names this kind to the person reading it.
12544 const fn describes(self) -> &'static str {
12545 match self {
12546 Self::Document => "document",
12547 Self::Work(kind) => kind.marker(),
12548 }
12549 }
12550}
12551
12552/// Whether `labels` satisfies `filter`, matching by name, case-insensitively.
12553///
12554/// This is the local Markdown source's `labels_match`, spelled the same way on purpose:
12555/// the shared cross-source journeys assert one answer to one question, so two sources
12556/// that disagree about what "carries the label bug" means fail them.
12557fn labels_match(labels: &[Label], filter: &LabelFilter) -> bool {
12558 let holds = |name: &String| {
12559 labels
12560 .iter()
12561 .any(|label| label.name.eq_ignore_ascii_case(name))
12562 };
12563 (filter.any_of.is_empty() || filter.any_of.iter().any(holds))
12564 && filter.all_of.iter().all(holds)
12565 && !filter.none_of.iter().any(holds)
12566}
12567
12568/// Whether `category` is one of `statuses`. An empty list is unfiltered rather than
12569/// "keeps nothing", which is what lets a `Vec<StatusCategory>` spell no filter at all.
12570fn status_matches(category: StatusCategory, statuses: &[StatusCategory]) -> bool {
12571 statuses.is_empty() || statuses.contains(&category)
12572}
12573
12574/// Whether `title`/`content` satisfies `query`, matching case-insensitively.
12575///
12576/// `content` is the item's own prose — the body with this source's trailing metadata
12577/// comment already taken off — so a search never matches an encoding the author of the
12578/// issue never wrote.
12579fn text_matches(title: &str, content: Option<&str>, query: &TextQuery) -> bool {
12580 let terms = query.terms.to_lowercase();
12581 let in_title = title.to_lowercase().contains(&terms);
12582 let in_content = content.is_some_and(|body| body.to_lowercase().contains(&terms));
12583 match query.fields {
12584 TextFields::Title => in_title,
12585 TextFields::Content => in_content,
12586 TextFields::TitleOrContent => in_title || in_content,
12587 }
12588}
12589
12590/// Whether `task` satisfies `query`, with `project` deciding the project predicate.
12591///
12592/// The project predicate is passed separately because a read narrowed to one project has
12593/// already answered it by asking *that project* for its own items — and re-applying it
12594/// there would compare the caller's selector, which may be a project's **name**, against
12595/// the id of the project that name resolved to, and keep nothing. Every other read passes
12596/// `query.project` and applies it here, which is what keeps `projects` a predicate this
12597/// source really does apply.
12598fn task_matches(task: &Task, query: &TaskQuery, project: &ProjectFilter) -> bool {
12599 labels_match(&task.labels, &query.labels)
12600 && status_matches(task.status.category, &query.statuses)
12601 && (query.priorities.is_empty() || query.priorities.contains(&task.priority))
12602 && match project {
12603 ProjectFilter::Any => true,
12604 ProjectFilter::Orphans => task.project.is_none(),
12605 ProjectFilter::Is(id) => task.project.as_ref() == Some(id),
12606 }
12607 && query
12608 .text
12609 .as_ref()
12610 .is_none_or(|text| text_matches(&task.title, task.content.as_deref(), text))
12611 // Against the parsed metadata slot, and against the origin field, which is where
12612 // `Resolved::metadata` reads each of them from.
12613 && query.metadata_matches(&task.metadata)
12614 && query.origin_matches(&task.metadata)
12615}
12616
12617fn project_matches(project: &Project, query: &ProjectQuery) -> bool {
12618 labels_match(&project.labels, &query.labels)
12619 && status_matches(project.status.category, &query.statuses)
12620 && query
12621 .text
12622 .as_ref()
12623 .is_none_or(|text| text_matches(&project.title, project.content.as_deref(), text))
12624}
12625
12626/// The same three predicates a task query carries, minus the status filter.
12627///
12628/// A document is not work, so it has no status for one to compare against and the query
12629/// type carries none. The project predicate is the same one — a design issue filed under a
12630/// project issue is in that project, and one filed under nothing is in none — so it is
12631/// spelled the same way here rather than answered differently.
12632fn document_matches(document: &Document, query: &DocumentQuery, project: &ProjectFilter) -> bool {
12633 labels_match(&document.labels, &query.labels)
12634 && match project {
12635 ProjectFilter::Any => true,
12636 ProjectFilter::Orphans => document.project.is_none(),
12637 ProjectFilter::Is(id) => document.project.as_ref() == Some(id),
12638 }
12639 && query
12640 .text
12641 .as_ref()
12642 .is_none_or(|text| text_matches(&document.title, document.content.as_deref(), text))
12643}
12644
12645/// Why a GitHub item's change token does not vouch for its forward edges.
12646///
12647/// A forward edge names its far end and that far end's kind, and the near item's token moves
12648/// with the edges it holds but says nothing of the far ends themselves: a far end turning from a
12649/// task into a project when it gains a sub-issue, retitled into a document, or deleted moves
12650/// nothing the near token reads, so an edge kept at the near version could name a far end that
12651/// is no longer what it said.
12652const FAR_ENDS_UNCOVERED: &str = "a github item's change token does not attest a far end's kind, \
12653 whether it became a document, or its deletion, so its forward edges are always read from \
12654 github";
12655
12656/// The issue-search qualifier that keeps open issues alone, which a status filter naming only
12657/// categories an open issue reads as narrows a board-scoped search with.
12658const OPEN_QUALIFIER: &str = "is:open";
12659
12660/// The board filter `task status reconcile` reads the board through: closed items alone.
12661const CLOSED_FILTER: &str = "is:closed";
12662
12663/// The variables of one [`graphql::ISSUES_BY_REFERENCE`] request for `batch`: each slot names
12664/// one reference, and a slot the batch has none for names its last one again.
12665fn reference_batch(batch: &[IssueRef]) -> Value {
12666 let mut variables = serde_json::Map::new();
12667 for slot in 0..REFERENCE_BATCH {
12668 let issue = batch.get(slot).or(batch.last());
12669 variables.insert(format!("owner{slot}"), json!(issue.map(|i| &i.owner)));
12670 variables.insert(format!("name{slot}"), json!(issue.map(|i| &i.name)));
12671 variables.insert(format!("number{slot}"), json!(issue.map(|i| i.number)));
12672 }
12673 variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
12674 variables.insert("statusField".to_owned(), json!(STATUS_FIELD));
12675 variables.insert("duplicates".to_owned(), json!(true));
12676 Value::Object(variables)
12677}
12678
12679/// One issue an issue read by reference answered, in the shape every other read of an issue
12680/// hands its resolver: each board membership's `Status` value, read by name, as the one field
12681/// value that membership carries.
12682///
12683/// A membership's `status` is `null` when its card sits at no option — or on a board with no
12684/// field of that name — and otherwise an option value naming its option; anything else is not
12685/// an answer to this read, and is refused rather than read as a card at no option.
12686fn held_issue(issue: &Value) -> Result<Value, SourceError> {
12687 let malformed = |what: &str| SourceError::Malformed {
12688 message: format!("GitHub answered an issue read by reference with {what}"),
12689 };
12690 let mut issue = issue.clone();
12691 let nodes = issue
12692 .pointer_mut("/projectItems/nodes")
12693 .and_then(Value::as_array_mut)
12694 .ok_or_else(|| malformed("no list of board memberships"))?;
12695 for node in nodes {
12696 let node = node
12697 .as_object_mut()
12698 .ok_or_else(|| malformed("a board membership that is not an object"))?;
12699 let status = match node.get("status") {
12700 None => return Err(malformed("a board membership with no status member")),
12701 Some(Value::Null) => None,
12702 Some(status) if status.get("name").is_some_and(Value::is_string) => {
12703 Some(status.clone())
12704 }
12705 // A single-select value of another field type answers `{}`; only an option value
12706 // carries a name.
12707 Some(status) if status.as_object().is_some_and(serde_json::Map::is_empty) => None,
12708 Some(_) => return Err(malformed("a membership status that names no option")),
12709 };
12710 node.insert(
12711 "fieldValues".to_owned(),
12712 json!({"nodes": status.into_iter().collect::<Vec<_>>(),
12713 "pageInfo": {"hasNextPage": false}}),
12714 );
12715 }
12716 Ok(issue)
12717}
12718
12719/// The issue one slot of a read by reference answered, `None` when GitHub answered that
12720/// slot's repository or issue `null` — which [`GitHubProjectsSource::answer_unresolved`]
12721/// admitted only beside a `NOT_FOUND` naming that very slot — and a refusal when the answer
12722/// leaves the slot, or its `issue` member, out: an answer missing a member is malformed rather
12723/// than a reference nothing holds.
12724fn referenced_issue(data: &Value, slot: usize) -> Result<Option<&Value>, SourceError> {
12725 let alias = format!("r{slot}");
12726 let repository = data.get(&alias).ok_or_else(|| SourceError::Malformed {
12727 message: format!("GitHub answered a read by reference with no {alias}"),
12728 })?;
12729 if repository.is_null() {
12730 return Ok(None);
12731 }
12732 let issue = repository
12733 .get("issue")
12734 .ok_or_else(|| SourceError::Malformed {
12735 message: format!("GitHub answered {alias} of a read by reference with no issue"),
12736 })?;
12737 if issue.is_null() {
12738 return Ok(None);
12739 }
12740 // `Repository.issue` is typed `Issue`, so anything else is not an answer to this read.
12741 if optional_str(issue, "__typename")? != Some("Issue") {
12742 return Err(SourceError::Malformed {
12743 message: format!(
12744 "GitHub answered {alias} of a read by reference with an issue whose __typename \
12745 is not Issue"
12746 ),
12747 });
12748 }
12749 Ok(Some(issue))
12750}
12751
12752/// A closure reason as a person reads it: `NOT_PLANNED` as `not planned`.
12753fn spoken_reason(reason: &str) -> String {
12754 reason.to_ascii_lowercase().replace('_', " ")
12755}
12756
12757impl GitHubProjectsSource {
12758 /// Every task and project of this board whose card contradicts its issue's closure, each
12759 /// judged against its own kind's half of `status_mapping`, read through the board's own
12760 /// `is:closed` filter, page by page — never the board's unfiltered `items`.
12761 ///
12762 /// Each item the filter answers is confirmed from its own node: an open one, one whose
12763 /// closure reads `unknown`, and one whose card is already in line are left out whatever
12764 /// the filter let through. The board's fields ride along and are held for the option
12765 /// writes that follow, so those need no read of the board.
12766 async fn contradictions(&self) -> Result<Vec<StatusContradiction>, SourceError> {
12767 let mut after: Option<String> = None;
12768 let mut found = Vec::new();
12769 loop {
12770 let data = self
12771 .graphql(
12772 graphql::CLOSED_ITEMS,
12773 json!({"owner":self.owner,"number":self.project_number,
12774 "first":MAX_PAGE_SIZE,"after":after,"filter":CLOSED_FILTER,
12775 "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
12776 )
12777 .await?;
12778 let board = data
12779 .pointer("/closedItems/projectV2")
12780 .filter(|value| !value.is_null())
12781 .ok_or_else(|| SourceError::Refused {
12782 message: format!(
12783 "GitHub project {}/{} was not found or is not visible to the token",
12784 self.owner, self.project_number
12785 ),
12786 })?;
12787 *self.fields_cache()? = Some(BoardFields {
12788 id: BoardId::parse(required_str(board, "id")?)?,
12789 fields: board.get("fields").cloned().unwrap_or(Value::Null),
12790 });
12791 let items = board.get("items").ok_or_else(|| SourceError::Malformed {
12792 message: "GitHub project closed items are missing".into(),
12793 })?;
12794 for item in items
12795 .get("nodes")
12796 .and_then(Value::as_array)
12797 .ok_or_else(|| SourceError::Malformed {
12798 message: "GitHub project items.nodes is not an array".into(),
12799 })?
12800 {
12801 let Some(resolved) = self.resolve(item)? else {
12802 continue;
12803 };
12804 let BoardKind::Work(kind) = resolved.kind else {
12805 continue;
12806 };
12807 let Some((closed_as, name)) = self.statuses.contradicted(
12808 kind,
12809 resolved.option.as_deref(),
12810 resolved.closed,
12811 resolved.reason.as_deref(),
12812 ) else {
12813 continue;
12814 };
12815 found.push(StatusContradiction {
12816 closed_as,
12817 to: name.to_owned(),
12818 option: resolved.option.clone(),
12819 item: match kind {
12820 ItemKind::Task => ContradictedItem::Task(resolved.task()?),
12821 ItemKind::Project => ContradictedItem::Project(resolved.project()),
12822 },
12823 });
12824 }
12825 let info = items
12826 .get("pageInfo")
12827 .ok_or_else(|| SourceError::Malformed {
12828 message: "GitHub project items have no pageInfo".into(),
12829 })?;
12830 if !required_bool(info, "hasNextPage")? {
12831 return Ok(found);
12832 }
12833 let next = required_str(info, "endCursor")?;
12834 validate_cursor_progress(after.as_deref(), next)?;
12835 after = Some(next.to_owned());
12836 }
12837 }
12838
12839 /// What this board holds for each issue in `issues`, once each in the order given, read
12840 /// [`REFERENCE_BATCH`] references at a time — and nothing else of the board.
12841 ///
12842 /// An issue GitHub cannot find for this source's credential, and one with no item on this
12843 /// board, are both absent. A closure that already says the work will not be done here —
12844 /// not planned, duplicate — is skipped: a landing does not overrule it. An item closed as
12845 /// completed whose card sits at its kind's `done` option is unchanged, and every other item
12846 /// is due a `done` write. A project item — a plan's home card, its issue holding the plan's
12847 /// tasks as sub-issues — is held under exactly these rules, judged against the project
12848 /// kind's half of `status_mapping`.
12849 async fn holdings(&self, issues: &[IssueRef]) -> Result<Vec<IssueHolding>, SourceError> {
12850 let mut distinct: Vec<IssueRef> = Vec::with_capacity(issues.len());
12851 for issue in issues {
12852 if !distinct.contains(issue) {
12853 distinct.push(issue.clone());
12854 }
12855 }
12856 let done = |kind: ItemKind| {
12857 self.statuses
12858 .target(kind, StatusCategory::Done)
12859 .option()
12860 .unwrap_or("Done")
12861 .to_owned()
12862 };
12863 // GitHub numbers an issue with a GraphQL `Int`, a signed 32-bit value, so a reference past
12864 // its range names no issue any repository can hold: it is answered absent and never sent,
12865 // where binding it would have GitHub refuse the whole batch.
12866 let readable: Vec<IssueRef> = distinct
12867 .iter()
12868 .filter(|issue| i32::try_from(issue.number).is_ok())
12869 .cloned()
12870 .collect();
12871 let mut holdings = Vec::with_capacity(readable.len());
12872 for batch in readable.chunks(REFERENCE_BATCH) {
12873 let data = self
12874 .graphql(graphql::ISSUES_BY_REFERENCE, reference_batch(batch))
12875 .await?;
12876 for (slot, issue) in batch.iter().enumerate() {
12877 let node = referenced_issue(&data, slot)?;
12878 let resolved = match node {
12879 Some(node) => self.resolve_issue_as(&held_issue(node)?, false).await?,
12880 None => None,
12881 };
12882 // A task and a project are both held; a document is not work, so nothing is.
12883 let Some((item, kind)) = resolved.and_then(|item| match item.kind {
12884 BoardKind::Work(kind) => Some((item, kind)),
12885 BoardKind::Document => None,
12886 }) else {
12887 holdings.push(IssueHolding {
12888 issue: issue.clone(),
12889 completion: Completion::Absent,
12890 });
12891 continue;
12892 };
12893 let task = match kind {
12894 ItemKind::Task => {
12895 let held = item.task()?;
12896 HeldTask {
12897 id: held.id,
12898 kind,
12899 option: item.option.clone(),
12900 classification: held.classification,
12901 repositories: held.repositories,
12902 delivers: held.delivers,
12903 }
12904 }
12905 // A project delivers no task, so what its write keeps in step is nothing.
12906 ItemKind::Project => {
12907 let held = item.project();
12908 HeldTask {
12909 id: held.id,
12910 kind,
12911 option: item.option.clone(),
12912 classification: held.classification,
12913 repositories: held.repositories,
12914 delivers: Vec::new(),
12915 }
12916 }
12917 };
12918 let done = done(kind);
12919 let completion = match (item.closed, item.reason.as_deref()) {
12920 (true, Some(reason @ ("NOT_PLANNED" | "DUPLICATE"))) => Completion::Skipped {
12921 task,
12922 reason: format!("closed as {}", spoken_reason(reason)),
12923 },
12924 (true, None | Some("COMPLETED"))
12925 if item
12926 .option
12927 .as_deref()
12928 .is_some_and(|option| option.eq_ignore_ascii_case(&done)) =>
12929 {
12930 Completion::Unchanged { task }
12931 }
12932 _ => {
12933 let kept = item.clone();
12934 self.held_cache
12935 .lock()
12936 .map_err(|_| SourceError::Unavailable {
12937 message: "items read by issue reference were left \
12938 inconsistent; run the command again"
12939 .into(),
12940 })?
12941 .insert(kept.id.clone(), kept);
12942 Completion::Due {
12943 task,
12944 to: done.clone(),
12945 }
12946 }
12947 };
12948 holdings.push(IssueHolding {
12949 issue: issue.clone(),
12950 completion,
12951 });
12952 }
12953 }
12954 let mut read = holdings.into_iter();
12955 let mut answered = Vec::with_capacity(distinct.len());
12956 for issue in distinct {
12957 if i32::try_from(issue.number).is_ok() {
12958 answered.push(read.next().ok_or_else(|| SourceError::Malformed {
12959 message: format!("no holding was read for {issue}"),
12960 })?);
12961 } else {
12962 answered.push(IssueHolding {
12963 issue,
12964 completion: Completion::Absent,
12965 });
12966 }
12967 }
12968 Ok(answered)
12969 }
12970}
12971
12972#[async_trait::async_trait]
12973impl TaskSource for GitHubProjectsSource {
12974 fn kind(&self) -> &'static str {
12975 KIND
12976 }
12977 fn order_versions(&self, a: &str, b: &str) -> Option<std::cmp::Ordering> {
12978 change_token::order(a, b)
12979 }
12980 /// A task's fields and comments: §4.13d of `docs/plugin-protocol.md` states, member by
12981 /// member, the change each part of the token moves for. Not its forward edges — see
12982 /// [`FAR_ENDS_UNCOVERED`].
12983 fn version_coverage(&self) -> VersionCoverage {
12984 VersionCoverage {
12985 forward_edges: Coverage::Uncovered {
12986 reason: FAR_ENDS_UNCOVERED.to_owned(),
12987 },
12988 ..VersionCoverage::whole()
12989 }
12990 }
12991 /// Every part, for a copy's skip: the relationship count moves for an edge added or removed
12992 /// and for a far end deleted, so the token sees every change to a task's edges but one — a
12993 /// far end's own kind flipping, by gaining a sub-issue or being retitled into a document,
12994 /// which `spike-coverage` measured stale 1 of 1. A skipping recopy does not carry that, and
12995 /// the next copy that reads the task does (`docs/plugin-protocol.md` §4.13d).
12996 // llmlint: ignore[names_match_behavior] The manager's ruling (c-d7838f2c) keeps 0.6.0's GitHub skip and has this plugin state that bounded claim through the coverage it reports, with the bound documented here, in §4.13d and by the copy_skip journey; a separate bounded representation would be a contract change beyond a defaulted addition, which the ruling reserves for the contract's owner, and a follow-up is drafted for a far-end-aware skip.
12997 fn copy_version_coverage(&self) -> VersionCoverage {
12998 VersionCoverage::whole()
12999 }
13000 /// A project's fields, and not its forward edges, for the same reason a task's are not.
13001 fn project_version_coverage(&self) -> ProjectVersionCoverage {
13002 ProjectVersionCoverage {
13003 fields: Coverage::Covered,
13004 forward_edges: Coverage::Uncovered {
13005 reason: FAR_ENDS_UNCOVERED.to_owned(),
13006 },
13007 }
13008 }
13009 /// A document's fields and the manifest of its image assets, which its metadata slot
13010 /// records: both are the issue's body, whose every edit moves the token's issue time.
13011 fn document_version_coverage(&self) -> DocumentVersionCoverage {
13012 DocumentVersionCoverage::whole()
13013 }
13014 fn write_context(&self, id: &NativeId) -> Option<Value> {
13015 self.context_of(id)
13016 }
13017 fn board_context(&self) -> Option<Value> {
13018 self.board_context_held()
13019 }
13020 fn prime_write(&self, id: &NativeId, context: &Value, board: Option<&Value>) -> bool {
13021 self.prime(id, context, board)
13022 }
13023 fn unprime(&self, id: &NativeId) {
13024 self.unprime_held(id);
13025 }
13026 fn begin_journal(&self) {
13027 self.expected.begin();
13028 }
13029 fn end_journal(&self) -> Option<WriteJournal> {
13030 Some(self.expected.end())
13031 }
13032 fn capabilities(&self) -> Capabilities {
13033 Capabilities {
13034 projects: Support::Native,
13035 documents: Support::Native,
13036 comments: Support::Native,
13037 assets: Support::Native,
13038 priority: if self.priorities.is_some() {
13039 Support::Native
13040 } else {
13041 Support::Unsupported
13042 },
13043 filter_by_priority: Support::Native,
13044 filter_by_comment_activity: Support::Native,
13045 filter_by_metadata: Support::Native,
13046 filter_by_origin: Support::Native,
13047 reconcile_status: Support::Native,
13048 complete_by_issue: Support::Native,
13049 orphan_tasks: Support::Native,
13050 filter_by_label: Support::Native,
13051 filter_by_status: Support::Native,
13052 search_title: Support::Native,
13053 search_content: Support::Native,
13054 task_dependencies: DependencySupport::BothDirections,
13055 project_dependencies: DependencySupport::BothDirections,
13056 max_page_size: MAX_PAGE_SIZE,
13057 }
13058 }
13059 async fn visibility(
13060 &self,
13061 target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13062 ) -> Result<onetaskgraph_plugin_api::Visibility, SourceError> {
13063 self.write_visibility(target).await
13064 }
13065 async fn health(&self) -> Result<Health, SourceError> {
13066 let board = self.board_page(None, 1).await?;
13067 Ok(Health {
13068 reachable: true,
13069 detail: Some(format!(
13070 "reading GitHub project {}/{} ({})",
13071 self.owner,
13072 self.project_number,
13073 required_str(&board, "title")?
13074 )),
13075 })
13076 }
13077 /// The board's own node id, digested: the one request reading its fields, or none when
13078 /// this command has read them already.
13079 async fn designator(&self) -> Result<Option<Designator>, SourceError> {
13080 let board = self.board_fields().await?;
13081 Designator::of_backend(KIND, board.id.as_str())
13082 .map(Some)
13083 .map_err(|message| SourceError::Malformed { message })
13084 }
13085 async fn get_task(&self, id: &NativeId) -> Result<Option<Task>, SourceError> {
13086 self.item_by_id(id)
13087 .await?
13088 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
13089 .map(|item| item.task())
13090 .transpose()
13091 }
13092 async fn task_assets(
13093 &self,
13094 id: &NativeId,
13095 ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
13096 self.held_assets(id, BoardKind::Work(ItemKind::Task)).await
13097 }
13098 async fn task_asset(
13099 &self,
13100 id: &NativeId,
13101 name: &onetaskgraph_plugin_api::AssetName,
13102 ) -> Result<Option<Vec<u8>>, SourceError> {
13103 self.held_asset(id, BoardKind::Work(ItemKind::Task), name)
13104 .await
13105 }
13106 async fn set_task_rendering_with_assets(
13107 &self,
13108 id: &NativeId,
13109 content: &str,
13110 provenance: &Value,
13111 _answers: &BTreeMap<String, Value>,
13112 assets: &onetaskgraph_plugin_api::AssetWrite,
13113 ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
13114 self.replace_rendering(
13115 id,
13116 BoardKind::Work(ItemKind::Task),
13117 content,
13118 provenance,
13119 Some(assets),
13120 )
13121 .await
13122 }
13123 async fn document_assets(
13124 &self,
13125 id: &NativeId,
13126 ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
13127 self.held_assets(id, BoardKind::Document).await
13128 }
13129 async fn document_asset(
13130 &self,
13131 id: &NativeId,
13132 name: &onetaskgraph_plugin_api::AssetName,
13133 ) -> Result<Option<Vec<u8>>, SourceError> {
13134 self.held_asset(id, BoardKind::Document, name).await
13135 }
13136 async fn set_document_rendering_with_assets(
13137 &self,
13138 id: &NativeId,
13139 content: &str,
13140 provenance: &Value,
13141 _answers: &BTreeMap<String, Value>,
13142 assets: &onetaskgraph_plugin_api::AssetWrite,
13143 ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
13144 self.replace_rendering(id, BoardKind::Document, content, provenance, Some(assets))
13145 .await
13146 }
13147 async fn get_project(&self, id: &NativeId) -> Result<Option<Project>, SourceError> {
13148 Ok(self
13149 .item_by_id(id)
13150 .await?
13151 .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
13152 .map(|item| item.project()))
13153 }
13154 async fn query_tasks(
13155 &self,
13156 query: &TaskQuery,
13157 page: &PageRequest,
13158 ) -> Result<Page<Task>, SourceError> {
13159 validate_page(page)?;
13160 refuse_unsearchable(query)?;
13161 // A closed issue reads `done`, `cancelled` or — closed for a reason that implies no
13162 // category — `unknown`; every other category reads only off an open issue or a draft. So
13163 // a status filter naming none of those three asks every issue search it sends for open
13164 // issues alone; each candidate's category is still confirmed from its own item below.
13165 let open_only = !query.statuses.is_empty()
13166 && query.statuses.iter().all(|category| {
13167 !matches!(
13168 category,
13169 StatusCategory::Done | StatusCategory::Cancelled | StatusCategory::Unknown
13170 )
13171 });
13172 if query.origin.is_none() && !matches!(query.project, ProjectFilter::Is(_)) {
13173 let mut qualifiers: Vec<String> = [
13174 query.commented_since.map(updated_qualifier),
13175 narrowing_qualifiers(query),
13176 ]
13177 .into_iter()
13178 .flatten()
13179 .collect();
13180 // A narrowed search never answers a draft, which is no issue, so narrowing it to
13181 // open issues too leaves out nothing such a search could have answered.
13182 if !qualifiers.is_empty() {
13183 if open_only {
13184 qualifiers.push(OPEN_QUALIFIER.to_owned());
13185 }
13186 return self.search_tasks(query, page, &qualifiers.join(" ")).await;
13187 }
13188 }
13189
13190 // A read narrowed to one project asks that project for its own tasks, so nothing
13191 // about it costs what the rest of the board holds. A read carrying a text, metadata
13192 // or origin predicate asks GitHub the narrower question those predicates are, and a
13193 // read narrowed to comment activity alone asks the board's own issue search for the
13194 // issues updated since, which is every issue a comment could have been written or
13195 // edited on since. Every other task read is a question about the whole board and is
13196 // answered by reading it.
13197 let (held, membership) = match (&query.project, query.commented_since) {
13198 (ProjectFilter::Is(project), _) => (
13199 match self.held_task_children(project)? {
13200 // Taken as `project_children` takes the project's own sub-issue read: with
13201 // every item this process wrote under it since, and none it moved away.
13202 Some(held) => self.completed_with_written(
13203 held.into_iter()
13204 .filter(|child| child.parent.as_ref() == Some(project))
13205 .collect(),
13206 |own| own.parent.as_ref() == Some(project),
13207 )?,
13208 None => self.project_children(project).await?,
13209 },
13210 // Answered by where these items came from; see `task_matches`.
13211 &ProjectFilter::Any,
13212 ),
13213 (ProjectFilter::Any | ProjectFilter::Orphans, since) => {
13214 match (self.narrowed(query).await?, since) {
13215 (Some(narrowed), _) => (narrowed, &query.project),
13216 (None, Some(since)) => (self.updated_since(since).await?, &query.project),
13217 // The board's own items list every draft; only the search half, which
13218 // completes them with issues they lag behind on, is narrowed — a closed
13219 // issue that half leaves out is at no category the filter keeps.
13220 (None, None) if open_only => (
13221 self.board_completed_by(Some(OPEN_QUALIFIER)).await?.items,
13222 &query.project,
13223 ),
13224 (None, None) => (self.board().await?.items, &query.project),
13225 }
13226 }
13227 };
13228 // Filtered before paged: a page of a filtered result is a page of the survivors,
13229 // never the survivors of a page.
13230 let mut tasks = Vec::new();
13231 for item in held
13232 .iter()
13233 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
13234 {
13235 let task = item.task()?;
13236 if task_matches(&task, query, membership)
13237 && self.commented_since(item, query.commented_since).await?
13238 {
13239 tasks.push(task);
13240 }
13241 }
13242 Ok(offset_page(
13243 tasks,
13244 numeric_cursor(page.cursor.as_ref())?,
13245 page.limit.min(MAX_PAGE_SIZE) as usize,
13246 ))
13247 }
13248 async fn query_projects(
13249 &self,
13250 query: &ProjectQuery,
13251 page: &PageRequest,
13252 ) -> Result<Page<Project>, SourceError> {
13253 validate_page(page)?;
13254 refuse_unsearchable_text(query.text.as_ref())?;
13255 // The projects a board holds are found by an issue search scoped to that board,
13256 // never by walking the board's own item connection: what tells a project from a
13257 // task is the `parent` each issue carries, which costs nothing to read. A query
13258 // carrying a text asks that search for the text too, so it reads the issues that
13259 // hold it rather than every issue of the board.
13260 let held = match self.text_searched(query.text.as_ref()).await? {
13261 Some(searched) => searched,
13262 None => self.board_issues().await?,
13263 };
13264 let projects = held
13265 .iter()
13266 .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
13267 .map(Resolved::project)
13268 .filter(|project| project_matches(project, query))
13269 .collect();
13270 Ok(offset_page(
13271 projects,
13272 numeric_cursor(page.cursor.as_ref())?,
13273 page.limit.min(MAX_PAGE_SIZE) as usize,
13274 ))
13275 }
13276 async fn query_projects_by_origin(
13277 &self,
13278 origin: &str,
13279 page: &PageRequest,
13280 ) -> Result<Page<Project>, SourceError> {
13281 validate_page(page)?;
13282 let projects = self
13283 .copied_from(origin, BoardKind::Work(ItemKind::Project))
13284 .await?
13285 .iter()
13286 .map(Resolved::project)
13287 .collect();
13288 Ok(offset_page(
13289 projects,
13290 numeric_cursor(page.cursor.as_ref())?,
13291 page.limit.min(MAX_PAGE_SIZE) as usize,
13292 ))
13293 }
13294 async fn get_document(&self, id: &NativeId) -> Result<Option<Document>, SourceError> {
13295 let Some(item) = self
13296 .document_by_id(id)
13297 .await?
13298 .filter(|item| item.kind == BoardKind::Document)
13299 else {
13300 return Ok(None);
13301 };
13302 self.expected.read_document(&item);
13303 Ok(Some(item.document()))
13304 }
13305 async fn query_documents_by_origin(
13306 &self,
13307 origin: &str,
13308 page: &PageRequest,
13309 ) -> Result<Page<Document>, SourceError> {
13310 validate_page(page)?;
13311 let documents = self
13312 .copied_from(origin, BoardKind::Document)
13313 .await?
13314 .iter()
13315 .map(Resolved::document)
13316 .collect();
13317 Ok(offset_page(
13318 documents,
13319 numeric_cursor(page.cursor.as_ref())?,
13320 page.limit.min(MAX_PAGE_SIZE) as usize,
13321 ))
13322 }
13323 async fn query_documents(
13324 &self,
13325 query: &DocumentQuery,
13326 page: &PageRequest,
13327 ) -> Result<Page<Document>, SourceError> {
13328 validate_page(page)?;
13329 // Narrowed to one project, this is the same sub-issue read a task list scoped to
13330 // that project makes — a document filed under a project is a sub-issue of it too,
13331 // and which of them come back is the kind this caller asked for. Unscoped, a query
13332 // carrying a text asks the board-scoped issue search for it, as a task query does,
13333 // and only one carrying none reads the board.
13334 let (held, membership) = match &query.project {
13335 ProjectFilter::Is(project) => (
13336 self.project_children(project).await?,
13337 // Answered by where these items came from; see `task_matches`.
13338 &ProjectFilter::Any,
13339 ),
13340 ProjectFilter::Any | ProjectFilter::Orphans => {
13341 refuse_unsearchable_text(query.text.as_ref())?;
13342 match self.text_searched(query.text.as_ref()).await? {
13343 Some(searched) => (searched, &query.project),
13344 None => (self.board().await?.items, &query.project),
13345 }
13346 }
13347 };
13348 // Filtered before paged, exactly as a task read is: a page of a filtered result is
13349 // a page of the survivors, never the survivors of a page.
13350 let documents = held
13351 .iter()
13352 .filter(|item| item.kind == BoardKind::Document)
13353 .map(Resolved::document)
13354 .filter(|document| document_matches(document, query, membership))
13355 .collect();
13356 Ok(offset_page(
13357 documents,
13358 numeric_cursor(page.cursor.as_ref())?,
13359 page.limit.min(MAX_PAGE_SIZE) as usize,
13360 ))
13361 }
13362 async fn labels(&self, page: &PageRequest) -> Result<Page<Label>, SourceError> {
13363 validate_page(page)?;
13364 let offset = numeric_cursor(page.cursor.as_ref())?;
13365 let mut labels = self
13366 .board()
13367 .await?
13368 .items
13369 .into_iter()
13370 .flat_map(|item| item.labels)
13371 .fold(Vec::new(), |mut all, label| {
13372 if !all.iter().any(|x: &Label| x.id == label.id) {
13373 all.push(label);
13374 }
13375 all
13376 });
13377 labels.sort_by(|a, b| a.name.cmp(&b.name).then(a.id.0.cmp(&b.id.0)));
13378 Ok(offset_page(
13379 labels,
13380 offset,
13381 page.limit.min(MAX_PAGE_SIZE) as usize,
13382 ))
13383 }
13384 async fn task_dependencies(
13385 &self,
13386 id: &NativeId,
13387 direction: Direction,
13388 page: &PageRequest,
13389 ) -> Result<Page<DependencyEdge>, SourceError> {
13390 self.dependencies(id, ItemKind::Task, direction, page).await
13391 }
13392 async fn project_dependencies(
13393 &self,
13394 id: &NativeId,
13395 direction: Direction,
13396 page: &PageRequest,
13397 ) -> Result<Page<DependencyEdge>, SourceError> {
13398 self.dependencies(id, ItemKind::Project, direction, page)
13399 .await
13400 }
13401
13402 fn writes(&self) -> WriteSupport {
13403 WriteSupport::Supported
13404 }
13405
13406 /// Create or update one task.
13407 ///
13408 /// Its `delivers` and `delivered_by` are checked before anything is read or written —
13409 /// neither may name the task itself or name one task twice — and land in the body's
13410 /// metadata slot under their reserved keys, in place of any caller metadata of those
13411 /// names.
13412 async fn write_task(&self, write: &ItemWrite<Task>) -> Result<NativeId, SourceError> {
13413 self.write_task_assets(write, None)
13414 .await
13415 .map(|written| written.id)
13416 }
13417
13418 async fn read_ahead_origins_for_copy(&self, origins: &[String]) -> Result<(), SourceError> {
13419 self.origins_ahead(origins).await
13420 }
13421
13422 async fn member_classifications(
13423 &self,
13424 project: &NativeId,
13425 ) -> Result<Option<Vec<(Classification, Vec<Repository>)>>, SourceError> {
13426 Ok(self
13427 .member_classes
13428 .lock()
13429 .map_err(|_| SourceError::Unavailable {
13430 message: "a project's members held for its classification were left \
13431 inconsistent; run the command again"
13432 .into(),
13433 })?
13434 .remove(project))
13435 }
13436
13437 /// The issue's own web address, as the `createIssue` that made it in this command answered
13438 /// it: a task, a project and a document are each an issue here.
13439 fn written_location(
13440 &self,
13441 _record: onetaskgraph_plugin_api::MetadataRecord,
13442 id: &NativeId,
13443 ) -> Option<Location> {
13444 self.written
13445 .lock()
13446 .unwrap_or_else(std::sync::PoisonError::into_inner)
13447 .get(id)
13448 .cloned()
13449 .map(Location::Url)
13450 }
13451
13452 /// [`MUTATION_BATCH`], while writes are held back: [`graphql::CREATE_ISSUES`] creates that many
13453 /// in one request, and a copy's tasks are created together only then, since their fields
13454 /// and edges are what is held back.
13455 fn create_batch_limit(&self) -> usize {
13456 if self.deferring().unwrap_or(false) {
13457 MUTATION_BATCH
13458 } else {
13459 1
13460 }
13461 }
13462
13463 // llmlint: ignore[invalid_states_unrepresentable] The signature is the plugin contract's additive `TaskSource::write_tasks`, which the manager approved taking `ItemWrite<Task>` as `write_task` does; a create-only input type would be a new contract type. A write naming an existing target is refused below before anything is sent.
13464 async fn write_tasks(&self, writes: &[ItemWrite<Task>]) -> Result<Vec<NativeId>, SourceError> {
13465 if let Some(write) = writes.iter().find(|write| write.target.is_some()) {
13466 return Err(SourceError::Refused {
13467 message: format!(
13468 "write_tasks creates tasks, and the write of {} names the existing task {}; \
13469 next: update it with write_task",
13470 write.item.id,
13471 write.target.as_ref().map_or("", |target| target.0.as_str())
13472 ),
13473 });
13474 }
13475 if !self.deferring()? {
13476 let mut created = Vec::with_capacity(writes.len());
13477 for write in writes {
13478 match self.write_task(write).await {
13479 Ok(id) => created.push(id),
13480 Err(error) => {
13481 let mut left: Vec<&str> = Vec::new();
13482 for id in &created {
13483 if self.delete_issue(id).await.is_err() {
13484 left.push(id.0.as_str());
13485 }
13486 }
13487 return Err(left_behind(error, &left));
13488 }
13489 }
13490 }
13491 return Ok(created);
13492 }
13493 self.create_together(writes).await
13494 }
13495
13496 fn begin_deferred_writes(&self) {
13497 if let Ok(mut deferred) = self.deferred.lock() {
13498 deferred.get_or_insert_with(Deferred::default);
13499 }
13500 }
13501
13502 async fn finish_writes(&self) -> Result<(), SourceError> {
13503 self.send_held_back().await
13504 }
13505
13506 async fn write_task_with_assets(
13507 &self,
13508 write: &ItemWrite<Task>,
13509 _answers: Option<&BTreeMap<String, Value>>,
13510 assets: &onetaskgraph_plugin_api::AssetWrite,
13511 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
13512 self.write_task_assets(write, Some(assets)).await
13513 }
13514
13515 async fn write_project(&self, write: &ItemWrite<Project>) -> Result<NativeId, SourceError> {
13516 self.write_item(
13517 &Incoming {
13518 written: Written::Work(ItemKind::Project, &write.item.status),
13519 title: &write.item.title,
13520 content: write.item.content.as_deref(),
13521 assets: None,
13522 labels: &write.item.labels,
13523 metadata: &write.item.metadata,
13524 repositories: &write.item.repositories,
13525 classification: write.item.classification,
13526 parent: None,
13527 delivers: &[],
13528 delivered_by: &[],
13529 priority: None,
13530 },
13531 write.target.as_ref(),
13532 &write.depends_on,
13533 )
13534 .await
13535 .map(|written| written.id)
13536 }
13537
13538 /// Create or update one document, which is one issue titled the way this board spells
13539 /// a document.
13540 ///
13541 /// Everything else is exactly a task write: caller metadata goes to the same canonical
13542 /// JSON slot at the end of the body and comes back with its JSON types intact, a key
13543 /// or a field this board cannot carry is refused by name rather than dropped, a target
13544 /// naming an issue this board does not hold is refused rather than created, and an
13545 /// issue this call created is taken back when the rest of the write fails.
13546 async fn write_document(&self, write: &ItemWrite<Document>) -> Result<NativeId, SourceError> {
13547 self.write_document_assets(write, None)
13548 .await
13549 .map(|written| written.id)
13550 }
13551
13552 async fn write_document_with_assets(
13553 &self,
13554 write: &ItemWrite<Document>,
13555 _answers: Option<&BTreeMap<String, Value>>,
13556 assets: &onetaskgraph_plugin_api::AssetWrite,
13557 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
13558 self.write_document_assets(write, Some(assets)).await
13559 }
13560
13561 /// Refused exactly as the write refuses it, from what the write reads: the mapping first,
13562 /// which reads nothing; then the board's `Status` option. Over an existing item that is
13563 /// read off the item, as the write reads it, and the item is held among this command's
13564 /// resolved records so the write that follows reuses that read rather than repeating it;
13565 /// an item that does not carry the field takes the board's fields, which are held once
13566 /// read. A create is checked against the board's fields only when this command already
13567 /// holds them, because a create reads them together with its repository, in one request,
13568 /// and refuses a missing option before it writes anything.
13569 async fn check_status_write(
13570 &self,
13571 kind: ItemKind,
13572 category: StatusCategory,
13573 target: Option<&NativeId>,
13574 ) -> Result<(), SourceError> {
13575 let status = self.resolved_target(kind, category)?;
13576 if status.option().is_none() {
13577 return Ok(());
13578 }
13579 let fields = match target {
13580 Some(target) => {
13581 // A target this board does not hold is the write's own refusal to make.
13582 let Some(item) = self.bound_item(target).await? else {
13583 return Ok(());
13584 };
13585 self.resolved_cache()?.insert(target.clone(), item.clone());
13586 self.fields_for(Some(&item), true, false).await?.fields
13587 }
13588 None => {
13589 let held = self
13590 .board_cache()?
13591 .as_ref()
13592 .map(|board| board.fields.clone());
13593 match held.or_else(|| {
13594 self.fields_cache()
13595 .ok()
13596 .and_then(|cache| cache.as_ref().map(|board| board.fields.clone()))
13597 }) {
13598 Some(fields) => fields,
13599 None => return Ok(()),
13600 }
13601 }
13602 };
13603 self.column_for(&fields, kind, category, &status)
13604 .map(|_| ())
13605 }
13606
13607 /// What a write of `status` reads back as: the `Status` option its category maps to for
13608 /// `kind`, and for a closing category the closure that option and its fixed reason read
13609 /// as — exactly what [`BoardStatuses::status`] reports of the item once written. A category
13610 /// this instance has no option for is answered as given, since writing it is refused.
13611 fn status_as_written(&self, kind: ItemKind, status: &Status) -> Status {
13612 match self.statuses.target(kind, status.category) {
13613 StatusTarget::Column(name) => {
13614 self.statuses.status(kind, Some(name.as_str()), false, None)
13615 }
13616 StatusTarget::Terminal(name, reason) => {
13617 self.statuses
13618 .status(kind, Some(name.as_str()), true, Some(reason.reason()))
13619 }
13620 StatusTarget::Disabled(_) => status.clone(),
13621 }
13622 }
13623
13624 /// Set one task's status alone.
13625 ///
13626 /// An open target reopens a closed issue with an `updateIssue` carrying only its
13627 /// `stateInput`, then selects the board option with `updateProjectV2ItemFieldValue`; a
13628 /// terminal target selects its mapped option, then closes with its fixed reason. No
13629 /// request carries a title, a body or a label. The status
13630 /// answered is what [`BoardStatuses::status`] reads off the state just written, which is
13631 /// what a re-read reports.
13632 async fn set_task_status(
13633 &self,
13634 id: &NativeId,
13635 category: StatusCategory,
13636 ) -> Result<Option<Status>, SourceError> {
13637 self.set_status(id, ItemKind::Task, category).await
13638 }
13639
13640 /// Set one task's status and answer the task with it, reading the item once: the read the
13641 /// task is answered from is handed to the status write, which would otherwise read the
13642 /// same issue again — two requests on a fresh process, the read and the field write,
13643 /// where the default's `get_task` before `set_task_status` sent three.
13644 async fn set_task_status_reading(
13645 &self,
13646 id: &NativeId,
13647 category: StatusCategory,
13648 ) -> Result<Option<Task>, SourceError> {
13649 // Refused before anything is read, in the words the write itself refuses it.
13650 self.resolved_target(ItemKind::Task, category)?;
13651 let Some(item) = self
13652 .bound_item(id)
13653 .await?
13654 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
13655 else {
13656 return Ok(None);
13657 };
13658 let mut task = item.task()?;
13659 self.held_cache
13660 .lock()
13661 .map_err(|_| SourceError::Unavailable {
13662 message: "items read by issue reference were left inconsistent; run the \
13663 command again"
13664 .into(),
13665 })?
13666 .insert(id.clone(), item);
13667 let Some(status) = self.set_status(id, ItemKind::Task, category).await? else {
13668 return Ok(None);
13669 };
13670 task.status = status;
13671 Ok(Some(task))
13672 }
13673
13674 /// Set one project's status alone, by exactly the paired write a task's status takes, for
13675 /// the project kind: a terminal category selects the project kind's mapped option and then
13676 /// closes the issue with the category's fixed reason — an issue already closed with a
13677 /// reason that reads that category is left as it is — and an open category reopens a closed
13678 /// issue and then selects its option. A board draft is refused a terminal category as a
13679 /// task's is.
13680 async fn set_project_status(
13681 &self,
13682 id: &NativeId,
13683 category: StatusCategory,
13684 ) -> Result<Option<Status>, SourceError> {
13685 self.set_status(id, ItemKind::Project, category).await
13686 }
13687
13688 /// Every task and project whose card contradicts its issue's closure, read through the
13689 /// board's own `is:closed` filter; see [`TaskSource::status_contradictions`].
13690 async fn status_contradictions(&self) -> Result<Vec<StatusContradiction>, SourceError> {
13691 self.contradictions().await
13692 }
13693
13694 /// What this board holds for each issue reference, one request per fifty of them; see
13695 /// [`TaskSource::issue_holdings`].
13696 async fn issue_holdings(&self, issues: &[IssueRef]) -> Result<Vec<IssueHolding>, SourceError> {
13697 self.holdings(issues).await
13698 }
13699
13700 /// Set one task's priority alone: one `updateProjectV2ItemFieldValue` selecting the
13701 /// mapped option of the board's `Priority` field, or one `clearProjectV2ItemFieldValue`
13702 /// for `none`. Refused by an instance with no `priority_mapping`.
13703 async fn set_task_priority(
13704 &self,
13705 id: &NativeId,
13706 priority: Priority,
13707 ) -> Result<Option<Priority>, SourceError> {
13708 self.set_priority(id, priority).await
13709 }
13710
13711 /// Replace one task's content with a single body update that keeps the metadata slot
13712 /// byte for byte.
13713 async fn set_task_content(
13714 &self,
13715 id: &NativeId,
13716 content: &str,
13717 ) -> Result<Option<()>, SourceError> {
13718 self.replace_content(id, content).await
13719 }
13720
13721 /// Replace one task issue's content and its provenance slot entry with a single body
13722 /// update. The answers are not kept: see `replace_rendering`.
13723 async fn set_task_rendering(
13724 &self,
13725 id: &NativeId,
13726 content: &str,
13727 provenance: &Value,
13728 _answers: &BTreeMap<String, Value>,
13729 ) -> Result<Option<()>, SourceError> {
13730 self.replace_rendering(
13731 id,
13732 BoardKind::Work(ItemKind::Task),
13733 content,
13734 provenance,
13735 None,
13736 )
13737 .await
13738 .map(|written| written.map(|_| ()))
13739 }
13740
13741 /// Replace one design-document issue's content and its provenance slot entry, on exactly
13742 /// the terms of [`set_task_rendering`](TaskSource::set_task_rendering).
13743 async fn set_document_rendering(
13744 &self,
13745 id: &NativeId,
13746 content: &str,
13747 provenance: &Value,
13748 _answers: &BTreeMap<String, Value>,
13749 ) -> Result<Option<()>, SourceError> {
13750 self.replace_rendering(id, BoardKind::Document, content, provenance, None)
13751 .await
13752 .map(|written| written.map(|_| ()))
13753 }
13754
13755 /// Replace one project issue's content and its provenance slot entry, on exactly the
13756 /// terms of [`set_task_rendering`](TaskSource::set_task_rendering).
13757 async fn set_project_rendering(
13758 &self,
13759 id: &NativeId,
13760 content: &str,
13761 provenance: &Value,
13762 _answers: &BTreeMap<String, Value>,
13763 ) -> Result<Option<()>, SourceError> {
13764 self.replace_rendering(
13765 id,
13766 BoardKind::Work(ItemKind::Project),
13767 content,
13768 provenance,
13769 None,
13770 )
13771 .await
13772 .map(|written| written.map(|_| ()))
13773 }
13774
13775 /// Apply a targeted update with one read of the item and a write only for what differs:
13776 /// the `Status` and `Priority` field writes in one request, the `blockedBy` difference,
13777 /// and last one `updateIssue` for title, body and state. See `targeted_update`.
13778 async fn update_task(
13779 &self,
13780 id: &NativeId,
13781 update: &TaskUpdate,
13782 ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
13783 self.targeted_update(id, update).await
13784 }
13785
13786 /// Replace one task's `delivered_by` with a single body update that changes the
13787 /// metadata slot and nothing outside it.
13788 async fn set_delivered_by(
13789 &self,
13790 id: &NativeId,
13791 delivered_by: &[TaskRef],
13792 ) -> Result<Option<()>, SourceError> {
13793 self.replace_delivered_by(id, delivered_by).await
13794 }
13795
13796 /// Set one key of one task issue's metadata with a single body update that changes the
13797 /// metadata slot and nothing outside it — no title, label, state or board field request —
13798 /// and sends nothing when the task already holds that value under the key.
13799 async fn set_task_metadata(
13800 &self,
13801 id: &NativeId,
13802 key: &MetadataKey,
13803 value: &Value,
13804 ) -> Result<Option<Task>, SourceError> {
13805 Ok(self
13806 .set_slot_key(id, BoardKind::Work(ItemKind::Task), key, value)
13807 .await?
13808 .map(|item| item.task())
13809 .transpose()?)
13810 }
13811
13812 /// Set one key of one project issue's metadata, on exactly the terms of
13813 /// [`set_task_metadata`](TaskSource::set_task_metadata).
13814 async fn set_project_metadata(
13815 &self,
13816 id: &NativeId,
13817 key: &MetadataKey,
13818 value: &Value,
13819 ) -> Result<Option<Project>, SourceError> {
13820 Ok(self
13821 .set_slot_key(id, BoardKind::Work(ItemKind::Project), key, value)
13822 .await?
13823 .map(|item| item.project()))
13824 }
13825
13826 /// Set one key of one design-document issue's metadata, on exactly the terms of
13827 /// [`set_task_metadata`](TaskSource::set_task_metadata).
13828 async fn set_document_metadata(
13829 &self,
13830 id: &NativeId,
13831 key: &MetadataKey,
13832 value: &Value,
13833 ) -> Result<Option<Document>, SourceError> {
13834 Ok(self
13835 .set_slot_key(id, BoardKind::Document, key, value)
13836 .await?
13837 .map(|item| item.document()))
13838 }
13839
13840 async fn delete_task(&self, id: &NativeId) -> Result<(), SourceError> {
13841 self.delete_item(id).await
13842 }
13843
13844 async fn delete_project(&self, id: &NativeId) -> Result<(), SourceError> {
13845 self.delete_item(id).await
13846 }
13847
13848 async fn delete_document(&self, id: &NativeId) -> Result<(), SourceError> {
13849 self.delete_item(id).await
13850 }
13851
13852 /// One page of the task issue's own comments, walked by GitHub's own cursor.
13853 ///
13854 /// Nothing here filters, so nothing has to be read ahead of the page: the caller's limit is
13855 /// the page GitHub is asked for and GitHub's `endCursor` is the cursor handed back.
13856 ///
13857 /// One request, [`graphql::ISSUE_DETAIL`]: the read that says the id names a task of this
13858 /// board is the read of its comments. A draft this process already resolved is refused
13859 /// without one.
13860 async fn task_comments(
13861 &self,
13862 task: &NativeId,
13863 page: &PageRequest,
13864 ) -> Result<Option<Page<Comment>>, SourceError> {
13865 validate_page(page)?;
13866 let cached = self.resolved_cache()?.get(task).cloned();
13867 if let Some(item) = cached {
13868 if item.kind != BoardKind::Work(ItemKind::Task) {
13869 return Ok(None);
13870 }
13871 if item.content_kind == ContentKind::DraftIssue {
13872 return Err(self.draft_has_no_comments(task));
13873 }
13874 }
13875 match self.issue_detail(task, page).await? {
13876 Some(TaskDetailRead {
13877 comments: Some(comments),
13878 ..
13879 }) => comments,
13880 _ => Ok(None),
13881 }
13882 }
13883
13884 /// Every id's task, with the first page of its comments when `comments` names it:
13885 /// [`DETAIL_BATCH`] items per [`graphql::ISSUE_DETAILS`] request, and one item with its
13886 /// comments in one [`graphql::ISSUE_DETAIL`] request.
13887 async fn get_task_details(
13888 &self,
13889 ids: &[NativeId],
13890 comments: Option<&PageRequest>,
13891 ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
13892 if let Some(page) = comments
13893 && let Err(error) = validate_page(page)
13894 {
13895 return ids.iter().map(|_| Err(error.clone())).collect();
13896 }
13897 match (ids, comments) {
13898 ([id], Some(page)) => vec![self.issue_detail(id, page).await],
13899 ([id], None) => vec![self.task_read(id).await],
13900 _ => self.issue_details(ids, comments).await,
13901 }
13902 }
13903
13904 /// Every id's task with the first page of its comments and its `blockedBy`, as a copy reads
13905 /// the tasks it carries: [`COPY_DETAIL_BATCH`] items per [`graphql::ISSUE_COPY_DETAILS`]
13906 /// request, so the dependency read a copy makes of each next is answered from this one.
13907 async fn get_task_details_for_copy(
13908 &self,
13909 ids: &[NativeId],
13910 comments: &PageRequest,
13911 ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
13912 // A page past the first is a page of one task's comments, which the detail read of it
13913 // alone answers.
13914 if comments.cursor.is_some() {
13915 return self.get_task_details(ids, Some(comments)).await;
13916 }
13917 if let Err(error) = validate_page(comments) {
13918 return ids.iter().map(|_| Err(error.clone())).collect();
13919 }
13920 let mut read = Vec::with_capacity(ids.len());
13921 for batch in ids.chunks(COPY_DETAIL_BATCH) {
13922 match self
13923 .graphql(
13924 graphql::ISSUE_COPY_DETAILS,
13925 copy_detail_batch(batch, comments, false),
13926 )
13927 .await
13928 {
13929 Ok(data) => {
13930 for (slot, id) in batch.iter().enumerate() {
13931 let read_one = match data.get(format!("i{slot}")) {
13932 Some(node) => self.detail_of(id, node, true, None).await,
13933 None => Err(SourceError::Malformed {
13934 message: format!(
13935 "GitHub answered a batch read with no item for {}",
13936 id.0
13937 ),
13938 }),
13939 };
13940 read.push(read_one);
13941 }
13942 }
13943 Err(error) if unresolvable_node(&error) => {
13944 for id in batch {
13945 read.push(self.issue_detail(id, comments).await);
13946 }
13947 }
13948 Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
13949 }
13950 }
13951 read
13952 }
13953
13954 /// Every id's item, whatever kind it is, read [`COPY_DETAIL_BATCH`] at a time with
13955 /// [`graphql::ISSUE_COPY_DETAILS`] — the document a copy reads the tasks it carries with —
13956 /// and held for the next read of each by id; see `read_ahead`.
13957 ///
13958 /// What this run wrote is never read: its own record answers, as it does for a read by id.
13959 /// Nor is an item a listing of its project carried whole earlier in this command — see
13960 /// `carried` — which is held in place of reading it.
13961 /// A batch GitHub refuses because one of its ids resolves to no node at all holds nothing,
13962 /// so each of its ids is read on its own and answered as that read answers; a draft, and an
13963 /// answer naming another issue than the one asked for, are left to their own reads too.
13964 async fn read_ahead_for_copy(&self, ids: &[NativeId]) -> Result<(), SourceError> {
13965 // The origins asked just before go out now, beside the members of the one project this
13966 // names when it names one; a project those origins answered whole is held for its next
13967 // read by id rather than read again here.
13968 let hinted = match ids {
13969 [one] => Some(one),
13970 _ => None,
13971 };
13972 self.send_pending_origins(hinted).await?;
13973 if let Some(project) = hinted {
13974 let answered = self.resolved_cache()?.get(project).cloned();
13975 if let Some(answered) = answered
13976 && answered.kind == BoardKind::Work(ItemKind::Project)
13977 {
13978 self.read_ahead()?.insert(project.clone(), Some(answered));
13979 return Ok(());
13980 }
13981 }
13982 let mut unread: Vec<NativeId> = Vec::new();
13983 // Read whole by a listing of its project in this command, which a mutation naming it
13984 // would have dropped: held as a batch would have held it, once every batch is answered.
13985 let mut listed: Vec<NativeId> = Vec::new();
13986 {
13987 let created = self.created()?;
13988 let held = self.read_ahead()?;
13989 let carried = self.carried()?;
13990 for id in ids {
13991 if unread.contains(id)
13992 || listed.contains(id)
13993 || held.contains_key(id)
13994 || created.iter().any(|own| own.id == *id)
13995 {
13996 continue;
13997 }
13998 if carried.contains_key(id) {
13999 listed.push(id.clone());
14000 } else {
14001 unread.push(id.clone());
14002 }
14003 }
14004 }
14005 // Nothing of an item's comments is wanted here: the smallest page keeps the read to
14006 // what the item itself costs.
14007 let comments = PageRequest {
14008 cursor: None,
14009 limit: 1,
14010 };
14011 // Held only once every batch has been answered, so a read-ahead that fails part way
14012 // holds nothing — what a listing carried included, which stays carried — and every
14013 // item is read on its own, as the method promises.
14014 let mut read: Vec<(NativeId, Option<Resolved>)> = Vec::new();
14015 // The issues the first id names as sub-issues, as `ISSUE_COPY_DETAILS`'s `children`
14016 // answered them: what a project copy's orphan check asks, when that first id is its
14017 // project.
14018 let mut children: Option<(NativeId, Vec<(String, String)>)> = None;
14019 for (index, batch) in unread.chunks(COPY_DETAIL_BATCH).enumerate() {
14020 let data = match self
14021 .graphql(
14022 graphql::ISSUE_COPY_DETAILS,
14023 copy_detail_batch(batch, &comments, index == 0),
14024 )
14025 .await
14026 {
14027 Ok(data) => data,
14028 Err(error) if unresolvable_node(&error) => continue,
14029 Err(error) => return Err(error),
14030 };
14031 for (slot, id) in batch.iter().enumerate() {
14032 let Some(node) = data.get(format!("i{slot}")) else {
14033 continue;
14034 };
14035 let held = if node.is_null() {
14036 None
14037 } else {
14038 match optional_str(node, "__typename")? {
14039 Some("DraftIssue") => continue,
14040 Some("Issue") if required_str(node, "id")? != id.0 => continue,
14041 _ => {}
14042 }
14043 // Remembered below with the rest, so a later batch refused leaves no
14044 // dependency read answered from this one either.
14045 self.resolve_issue_as(node, false).await?
14046 };
14047 read.push((id.clone(), held));
14048 }
14049 if index == 0
14050 && let Some(node) = data.get("children").filter(|node| !node.is_null())
14051 && node.get("id").and_then(Value::as_str) == batch.first().map(|id| id.0.as_str())
14052 && node.pointer("/subIssues/pageInfo/hasNextPage") == Some(&Value::Bool(false))
14053 {
14054 // A list GitHub did not answer is not an empty one: it would answer the project's
14055 // task listing with nothing, and the orphan check with no orphans.
14056 let listed = node
14057 .pointer("/subIssues/nodes")
14058 .and_then(Value::as_array)
14059 .ok_or_else(|| SourceError::Malformed {
14060 message: format!(
14061 "GitHub answered the sub-issues of project {} with no list of them",
14062 batch[0].0
14063 ),
14064 })?
14065 .iter()
14066 .map(|child| {
14067 Ok((
14068 required_str(child, "id")?.to_owned(),
14069 required_str(child, "title")?.to_owned(),
14070 ))
14071 })
14072 .collect::<Result<Vec<_>, SourceError>>()?;
14073 children = Some((batch[0].clone(), listed));
14074 }
14075 }
14076 // The project's tasks are known from this read alone when every child it named is held
14077 // from it, but for a document, which its title says is one and which a task listing
14078 // never answers: then a listing of the project's tasks is answered from them.
14079 if let Some((project, listed)) = children
14080 && read.iter().any(|(id, held)| {
14081 *id == project
14082 && held
14083 .as_ref()
14084 .is_some_and(|held| held.kind == BoardKind::Work(ItemKind::Project))
14085 })
14086 {
14087 let mut tasks = Vec::new();
14088 let mut whole = true;
14089 for (child, title) in &listed {
14090 match read.iter().find(|(id, _)| id.0 == *child) {
14091 Some((_, Some(held))) if held.parent.as_ref() == Some(&project) => {
14092 if held.kind == BoardKind::Work(ItemKind::Task) {
14093 tasks.push(held.clone());
14094 }
14095 }
14096 _ if title.starts_with(DESIGN_TITLE_PREFIX) => {}
14097 _ => whole = false,
14098 }
14099 }
14100 if whole {
14101 self.task_children()?.insert(project, tasks);
14102 }
14103 }
14104 let mut resolved = self.resolved_cache()?;
14105 let mut ahead = self.read_ahead()?;
14106 let mut carried = self.carried()?;
14107 for item in read.iter().filter_map(|(_, held)| held.as_ref()) {
14108 resolved.insert(item.id.clone(), item.clone());
14109 }
14110 // Taken only if still carried: a mutation naming it while the batches ran dropped it,
14111 // and its next read is then its own.
14112 for id in &listed {
14113 let Some(item) = carried.remove(id) else {
14114 continue;
14115 };
14116 // The whole of what it is blocked by came with it, so the dependency read a copy
14117 // makes next is answered from this one, as a copy's detail read is.
14118 if item.blocked_by.is_some() {
14119 resolved.insert(item.id.clone(), item.clone());
14120 }
14121 read.push((id.clone(), Some(item)));
14122 }
14123 for (id, held) in read {
14124 ahead.insert(id, held);
14125 }
14126 Ok(())
14127 }
14128
14129 /// Add one comment to the task's issue, as the account the token belongs to.
14130 ///
14131 /// The author is refused before anything is sent — not even the task is read — because
14132 /// no answer GitHub could give would make posting under another name than the one asked
14133 /// for the right outcome.
14134 async fn add_comment(
14135 &self,
14136 task: &NativeId,
14137 comment: &NewComment,
14138 ) -> Result<Option<Comment>, SourceError> {
14139 if let Some(author) = &comment.author {
14140 return Err(SourceError::Refused {
14141 message: format!(
14142 "source {} cannot post a comment as {author:?}: GitHub records the account \
14143 the token signs in as the author of every comment; next: leave --author \
14144 out, and the comment is posted as that account",
14145 self.name
14146 ),
14147 });
14148 }
14149 let Some(issue) = self.commented_issue(task).await? else {
14150 return Ok(None);
14151 };
14152 let data = self
14153 .graphql(
14154 graphql::ADD_COMMENT,
14155 json!({"input":{"subjectId":issue.0,"body":comment.body.as_str()}}),
14156 )
14157 .await?;
14158 let subject = data
14159 .pointer("/addComment/subject")
14160 .filter(|value| !value.is_null())
14161 .ok_or_else(|| SourceError::Malformed {
14162 message: "GitHub comment addition returned no subject".into(),
14163 })?;
14164 if required_str(subject, "id")? != issue.0 {
14165 return Err(SourceError::Malformed {
14166 message: "GitHub comment addition answered about another issue".into(),
14167 });
14168 }
14169 let added = data
14170 .pointer("/addComment/commentEdge/node")
14171 .filter(|value| !value.is_null())
14172 .ok_or_else(|| SourceError::Malformed {
14173 message: "GitHub comment addition returned no comment".into(),
14174 })?;
14175 let added = comment_from(added)?;
14176 self.remember_commented(&issue)?;
14177 Ok(Some(added))
14178 }
14179
14180 async fn edit_comment(
14181 &self,
14182 task: &NativeId,
14183 comment: &NativeId,
14184 body: &CommentBody,
14185 ) -> Result<Option<Comment>, SourceError> {
14186 let Some(issue) = self.commented_issue(task).await? else {
14187 return Ok(None);
14188 };
14189 let Some(held) = self.comment_on(&issue, comment).await? else {
14190 return Ok(None);
14191 };
14192 // A comment a copy wrote keeps its origin through an edit: its marker is carried over.
14193 let body = CommentOrigin::kept(&held, body)?;
14194 let data = self
14195 .graphql(
14196 graphql::UPDATE_COMMENT,
14197 json!({"input":{"id":comment.0,"body":body.as_str()}}),
14198 )
14199 .await?;
14200 let edited = data
14201 .pointer("/updateIssueComment/issueComment")
14202 .filter(|value| !value.is_null())
14203 .ok_or_else(|| SourceError::Malformed {
14204 message: "GitHub comment update returned no comment".into(),
14205 })?;
14206 let edited = comment_from(edited)?;
14207 if edited.id != *comment {
14208 return Err(SourceError::Malformed {
14209 message: "GitHub comment update returned the wrong comment".into(),
14210 });
14211 }
14212 self.remember_commented(&issue)?;
14213 Ok(Some(edited))
14214 }
14215
14216 async fn delete_comment(
14217 &self,
14218 task: &NativeId,
14219 comment: &NativeId,
14220 ) -> Result<Option<NativeId>, SourceError> {
14221 let Some(issue) = self.commented_issue(task).await? else {
14222 return Ok(None);
14223 };
14224 if !self.comment_is_on(&issue, comment).await? {
14225 return Ok(None);
14226 }
14227 let data = self
14228 .graphql(graphql::DELETE_COMMENT, json!({"input":{"id":comment.0}}))
14229 .await?;
14230 // The payload says nothing about the comment it removed, so what is checked is that
14231 // GitHub answered the mutation at all rather than leaving it unanswered.
14232 data.get("deleteIssueComment")
14233 .filter(|value| !value.is_null())
14234 .ok_or_else(|| SourceError::Malformed {
14235 message: "GitHub comment deletion returned no payload".into(),
14236 })?;
14237 Ok(Some(comment.clone()))
14238 }
14239
14240 /// Every request this source has recorded, and what each of GitHub's two budgets was
14241 /// attributed — read off the same accounting the session report is rendered from, so
14242 /// the two cannot count one request two ways.
14243 async fn metering(&self) -> Result<Option<Metering>, SourceError> {
14244 Ok(Some(self.ledger.snapshot().metering()))
14245 }
14246
14247 /// Drop every item, search answer and board read this source holds, so the next command
14248 /// reads the board as a person has since left it.
14249 ///
14250 /// Every one of those is held on the assumption that nothing but this source writes the
14251 /// board while a command runs, which stops being true the moment the command is over: a
14252 /// body a person edited would be overwritten from the record held here, and a card they
14253 /// moved would be read as still where this source left it. The board's own field
14254 /// definitions go too, because a person can add or delete a `Status` option and a write
14255 /// resolved against the held list would not re-read on a miss. What stays is what stays
14256 /// valid in normal use: each repository's node id, which a miss re-reads, the pacing of
14257 /// mutations, which is about GitHub's limiter rather than anybody's work, and the running
14258 /// accounting [`metering`](TaskSource::metering) answers from.
14259 ///
14260 /// Infallible in practice: a lock an earlier failure poisoned is cleared rather than
14261 /// refused, because clearing it is what puts it right.
14262 async fn end_command(&self) -> Result<(), SourceError> {
14263 fn clear<T: Default>(held: &Mutex<T>) {
14264 *held
14265 .lock()
14266 .unwrap_or_else(std::sync::PoisonError::into_inner) = T::default();
14267 held.clear_poison();
14268 }
14269 clear(&self.created);
14270 clear(&self.updated);
14271 clear(&self.commented);
14272 clear(&self.board_cache);
14273 clear(&self.search_cache);
14274 clear(&self.narrowed_cache);
14275 clear(&self.search_next);
14276 clear(&self.resolved_cache);
14277 clear(&self.read_ahead);
14278 clear(&self.pending_origins);
14279 clear(&self.member_classes);
14280 clear(&self.carried);
14281 clear(&self.held_cache);
14282 clear(&self.children_cache);
14283 clear(&self.task_children);
14284 clear(&self.written);
14285 clear(&self.fields_cache);
14286 self.expected.end_command();
14287 Ok(())
14288 }
14289}
14290
14291/// One member of a project as a classification-only read answers it: its classification and
14292/// the repositories its work changes.
14293type MemberClass = (Classification, Vec<Repository>);
14294
14295/// Each project's sub-issues as one command read them, keyed by the selector they were asked
14296/// for under, beside the project that selector named; see `children_cache`.
14297type ProjectChildren = BTreeMap<NativeId, (NativeId, Vec<Resolved>)>;
14298
14299/// One issue comment as the contract carries it.
14300///
14301/// `author` is absent both when GitHub answers `null` for an account that no longer exists
14302/// and when it answers an actor with no login, because either way the source did not say who
14303/// wrote it — which is what an absent author means, rather than an author called nothing.
14304///
14305/// A comment a copy wrote here carries its origin as the hidden marker at the end of its body
14306/// — GitHub sets every comment's author and times itself, so there is nowhere else to keep it —
14307/// and is read back with the marker taken out and reported as its origin.
14308fn comment_from(value: &Value) -> Result<Comment, SourceError> {
14309 Ok(Comment {
14310 origin: None,
14311 id: NativeId(required_str(value, "id")?.to_owned()),
14312 author: optional_str(value.get("author").unwrap_or(&Value::Null), "login")?
14313 .map(str::to_owned),
14314 created_at: optional_time(value, "createdAt")?,
14315 updated_at: optional_time(value, "updatedAt")?,
14316 body: required_str(value, "body")?.to_owned(),
14317 url: optional_str(value, "url")?.map(str::to_owned),
14318 }
14319 .unmarked())
14320}
14321
14322/// The page of comments one issue node carries, resumed from `after`.
14323fn comment_page(
14324 node: &Value,
14325 issue: &str,
14326 after: Option<&str>,
14327) -> Result<Page<Comment>, SourceError> {
14328 let connection = node
14329 .get("comments")
14330 .filter(|value| !value.is_null())
14331 .ok_or_else(|| SourceError::Malformed {
14332 message: format!("GitHub issue {issue} answered with no comments connection"),
14333 })?;
14334 let items = optional_nodes(Some(connection), "issue comments")?
14335 .into_iter()
14336 .flatten()
14337 .map(comment_from)
14338 .collect::<Result<Vec<_>, _>>()?;
14339 let next = next_cursor(connection)?;
14340 if let Some(next) = &next {
14341 validate_cursor_progress(after, &next.0)?;
14342 }
14343 Ok(Page { items, next })
14344}
14345
14346/// The far ends of an issue's whole `blockedBy` connection, when the read carried it to its
14347/// end — `None` when it carried none, or a page with more past it.
14348fn carried_blocked_by(content: &Value) -> Result<Option<Vec<Value>>, SourceError> {
14349 let Some(connection) = content.get("blockedBy").filter(|value| !value.is_null()) else {
14350 return Ok(None);
14351 };
14352 if next_cursor(connection)?.is_some() {
14353 return Ok(None);
14354 }
14355 Ok(Some(
14356 optional_nodes(Some(connection), "blocked-by issues")?
14357 .into_iter()
14358 .flatten()
14359 .cloned()
14360 .collect(),
14361 ))
14362}
14363
14364/// Where the recorded tail of a dependency walk resumes; see
14365/// [`GitHubProjectsSource::recorded_edges`].
14366const RECORDED_CURSOR: &str = "onetaskgraph.depends_on:";
14367
14368/// The board text field this source keeps a copy's origin in.
14369///
14370/// Named after the key it holds, and held to that name by the guard below rather than by
14371/// a reader noticing.
14372const ORIGIN_FIELD: &str = "onetaskgraph.origin";
14373
14374/// The metadata key that field holds.
14375///
14376/// The engine owns this key and spells it once as `GlobalId::ORIGIN_KEY`; a plugin never
14377/// constructs or interprets the qualified id it carries. This source names it only to
14378/// route it — a short, typed value belongs in a typed field rather than in the body slot
14379/// a caller's own prose shares.
14380///
14381/// Restated rather than imported, because no plugin crate may depend on the engine. What
14382/// keeps the two spellings one contract is `scripts/check-origin-key-spelling.sh`, a
14383/// target in `check`: it reads the engine's own literal and fails naming the file and the
14384/// line when a plugin's parts from it either way. Drift here has one symptom — a copy
14385/// that creates a second item every run instead of finding the one it wrote — and that is
14386/// too late to learn it.
14387const ORIGIN_KEY: &str = "onetaskgraph.origin";
14388
14389/// Where a recorded tail resumes, refusing a cursor no walk in `direction` reported.
14390///
14391/// The reserved key holds forward edges and nothing else — the reverse of a recorded edge
14392/// is derived from the far end, never written down on the near item — so only a forward
14393/// walk ever reports one of these cursors. A reverse read carrying one is resuming a walk
14394/// it did not come from, and it is told so rather than answered with an empty page that
14395/// reads as a walk which ended.
14396fn recorded_offset(
14397 cursor: Option<&str>,
14398 direction: Direction,
14399) -> Result<Option<usize>, SourceError> {
14400 cursor
14401 .and_then(|cursor| cursor.strip_prefix(RECORDED_CURSOR))
14402 .map(|offset| {
14403 if direction != Direction::DependsOn {
14404 return Err(SourceError::Config {
14405 message: format!(
14406 "{RECORDED_CURSOR}{offset} resumes recorded forward edges, which a \
14407 reverse dependency read never issues; resume it in the direction \
14408 that reported it"
14409 ),
14410 });
14411 }
14412 offset.parse().map_err(|_| SourceError::Config {
14413 message: format!("{RECORDED_CURSOR}{offset} is not a recorded-edge cursor"),
14414 })
14415 })
14416 .transpose()
14417}
14418
14419/// What a dependency read's own answer says of a board draft: `None` when the item is not a
14420/// draft, or the answer did not carry its board item; `Some(None)` for a draft whose one board
14421/// item is not on this board, which this source does not hold; and `Some(Some(slot))`, the
14422/// draft's metadata, for one it does — its board item naming this board by its number and by
14423/// the id the same answer read of the board, as [`GitHubProjectsSource::draft_by_id`] decides.
14424fn draft_slot(
14425 data: &Value,
14426 id: &NativeId,
14427 project_number: u32,
14428) -> Result<Option<Option<BTreeMap<String, Value>>>, SourceError> {
14429 let Some(draft) = data
14430 .get("node")
14431 .filter(|node| node.get("__typename").and_then(Value::as_str) == Some("DraftIssue"))
14432 else {
14433 return Ok(None);
14434 };
14435 let Some(items) = data.pointer("/draft/projectV2Items") else {
14436 return Ok(None);
14437 };
14438 if required_str(draft, "id")? != id.0 {
14439 return Err(SourceError::Malformed {
14440 message: format!("GitHub answered a different draft for {}", id.0),
14441 });
14442 }
14443 let nodes = items
14444 .get("nodes")
14445 .and_then(Value::as_array)
14446 .ok_or_else(|| SourceError::Malformed {
14447 message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
14448 })?;
14449 let info = items
14450 .get("pageInfo")
14451 .ok_or_else(|| SourceError::Malformed {
14452 message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
14453 })?;
14454 if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
14455 return Err(SourceError::Malformed {
14456 message: format!(
14457 "GitHub draft {} reports more board items than the one GitHub links a draft to",
14458 id.0
14459 ),
14460 });
14461 }
14462 let board = data
14463 .pointer("/board/projectV2/id")
14464 .and_then(Value::as_str)
14465 .ok_or_else(|| SourceError::Malformed {
14466 message: format!(
14467 "GitHub answered draft {} without this board's own id beside it",
14468 id.0
14469 ),
14470 })?;
14471 let mut held = false;
14472 for node in nodes {
14473 let project = node.get("project").ok_or_else(|| SourceError::Malformed {
14474 message: format!("GitHub draft {} board item has no project", id.0),
14475 })?;
14476 let number = project
14477 .get("number")
14478 .and_then(Value::as_u64)
14479 .ok_or_else(|| SourceError::Malformed {
14480 message: format!(
14481 "GitHub draft {} board item has no numeric project number",
14482 id.0
14483 ),
14484 })?;
14485 let project_id = required_str(project, "id")?;
14486 held |= number == u64::from(project_number) && project_id == board;
14487 }
14488 if !held {
14489 return Ok(Some(None));
14490 }
14491 Ok(Some(Some(
14492 metadata_body(optional_str(draft, "body")?.map(str::to_owned))?.1,
14493 )))
14494}
14495
14496fn recorded_page(edges: Vec<DependencyEdge>, offset: usize, limit: usize) -> Page<DependencyEdge> {
14497 let mut page = offset_page(edges, offset, limit.max(1));
14498 page.next = page
14499 .next
14500 .map(|cursor| Cursor(format!("{RECORDED_CURSOR}{}", cursor.0)));
14501 page
14502}
14503
14504/// The kind of one issue reached through a dependency connection.
14505///
14506/// The same questions the board scan asks, over the fields the dependency document
14507/// selects, and in the same order: the design prefix first, then a sub-issue is a task,
14508/// then anything with sub-issues or the marker is a project.
14509///
14510/// # Errors
14511///
14512/// A far end this board holds as a document is refused rather than reported. The two
14513/// answers that are not refusals would both be wrong: reporting it as a task names an id
14514/// no task read of this source can find, and reporting it as a project names one no
14515/// project read can. There is no third value to return — `ItemKind` has no document
14516/// variant, because nothing may point at a document — so the relationship itself is what
14517/// the person is told about.
14518fn related_kind(value: &Value) -> Result<ItemKind, SourceError> {
14519 let id = required_str(value, "id")?;
14520 if required_str(value, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
14521 return Err(SourceError::Refused {
14522 message: format!(
14523 "GitHub issue {id} is a document of this board — its title begins \
14524 {DESIGN_TITLE_PREFIX:?} — and nothing may depend on a document or be depended \
14525 on by one; next: remove that issue's blocking relationship on this board"
14526 ),
14527 });
14528 }
14529 let parent = optional_str(value.get("parent").unwrap_or(&Value::Null), "id")?;
14530 if parent.is_some() {
14531 return Ok(ItemKind::Task);
14532 }
14533 let (_, slot) = metadata_body(optional_str(value, "body")?.map(str::to_owned))?;
14534 let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
14535 message: format!("GitHub issue {id}: {message}"),
14536 })?;
14537 let sub_issues = sub_issue_total(value)?;
14538 Ok(if sub_issues > 0 || marked == Some(ItemKind::Project) {
14539 ItemKind::Project
14540 } else {
14541 ItemKind::Task
14542 })
14543}
14544
14545/// The `stateInput` that closes an issue for `reason`.
14546fn closed_input(reason: ClosedState) -> Value {
14547 json!({"value":"CLOSED","stateReason":reason.reason()})
14548}
14549
14550/// The `IssueStateUpdateInput` one status target asks for.
14551///
14552/// `stateInput` and `state` are mutually exclusive on `UpdateIssueInput`, and only this
14553/// one is ever sent. A non-terminal status always asks for `OPEN`, which is what reopens
14554/// a currently-closed issue: without that the item would read back `Unknown` and a copy
14555/// would report a change forever. A document has no status at all, and asks for neither.
14556fn state_input(target: Option<&StatusTarget>) -> Value {
14557 match target {
14558 Some(StatusTarget::Terminal(_, reason)) => closed_input(*reason),
14559 Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => json!({"value":"OPEN"}),
14560 // A document has no status, so a write of one says nothing about the issue's open
14561 // or closed state rather than forcing it open: `stateInput` is what carries that
14562 // instruction, and an explicit null asks for no change to it.
14563 None => Value::Null,
14564 }
14565}
14566
14567/// The metadata one write stores in the item's body slot.
14568///
14569/// The typed fields travel as themselves, so the three reserved keys are rebuilt here
14570/// rather than carried: the kind marker so an empty project stays readable, the
14571/// repository list only when it is not exactly the issue's own repository, and the far
14572/// ends no relationship here can name.
14573///
14574/// The copy origin is the one typed field that is also mirrored here, and only as a
14575/// mirror: it lands in the board's origin field as well, which stays the one every reader
14576/// takes it from, and it is here so that GitHub's issue search — which indexes this comment
14577/// and catches up with a write in seconds rather than minutes — can find the item by it.
14578/// A reader of the release before this one drops the slot's copy and reads the field, so an
14579/// item written here still reads with exactly one origin there.
14580fn slot_metadata(
14581 incoming: &Incoming<'_>,
14582 own_repository: Option<&Repository>,
14583 fallback: &[DependencyEdge],
14584) -> BTreeMap<String, Value> {
14585 let mut metadata = incoming.metadata.clone();
14586 match metadata.remove(ORIGIN_KEY) {
14587 Some(Value::String(origin)) if !origin.is_empty() => {
14588 metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin));
14589 }
14590 _ => {}
14591 }
14592 match incoming.written.kind() {
14593 BoardKind::Work(kind) => metadata.insert(
14594 ItemKind::METADATA_KEY.to_owned(),
14595 Value::String(kind.marker().to_owned()),
14596 ),
14597 // A document is told by its title, so it carries no kind marker: that key names
14598 // what a dependency endpoint points at, and nothing may point at a document.
14599 BoardKind::Document => metadata.remove(ItemKind::METADATA_KEY),
14600 };
14601 incoming.classification.record(&mut metadata);
14602 let derivable = own_repository
14603 .map(|own| incoming.repositories == [own.clone()])
14604 .unwrap_or(incoming.repositories.is_empty());
14605 if derivable {
14606 metadata.remove(Repository::METADATA_KEY);
14607 } else {
14608 metadata.insert(
14609 Repository::METADATA_KEY.to_owned(),
14610 Value::Array(
14611 incoming
14612 .repositories
14613 .iter()
14614 .map(|repository| Value::String(repository.as_str().to_owned()))
14615 .collect(),
14616 ),
14617 );
14618 }
14619 // The typed lists are what land, whatever the caller's own metadata held under their
14620 // keys: a key of either name travelling beside the field would otherwise be a second
14621 // answer to the same question, and the field is the one the contract names.
14622 for (key, entries) in [
14623 (TaskRef::DELIVERS_KEY, incoming.delivers),
14624 (TaskRef::DELIVERED_BY_KEY, incoming.delivered_by),
14625 ] {
14626 set_task_list(&mut metadata, key, entries);
14627 }
14628 record_edges(&mut metadata, fallback);
14629 metadata
14630}
14631
14632/// Hold the far ends no relationship here can name under [`DependencyEdge::RECORDED_KEY`] in
14633/// one slot's metadata, or no such key when there are none.
14634fn record_edges(metadata: &mut BTreeMap<String, Value>, fallback: &[DependencyEdge]) {
14635 if fallback.is_empty() {
14636 metadata.remove(DependencyEdge::RECORDED_KEY);
14637 } else {
14638 metadata.insert(
14639 DependencyEdge::RECORDED_KEY.to_owned(),
14640 Value::Array(
14641 fallback
14642 .iter()
14643 .map(|edge| {
14644 serde_json::to_value(&edge.to).expect("an endpoint renders as JSON")
14645 })
14646 .collect(),
14647 ),
14648 );
14649 }
14650}
14651
14652/// Every label one item carries, from its content's own connection and nowhere else.
14653///
14654/// There is no second place to read one from: no document this source sends selects the
14655/// board's built-in `Labels` field, because GitHub derives it from the content and a draft
14656/// cannot carry one at all. The module documentation records the three schema facts that
14657/// settle it.
14658fn labels(content: &Value) -> Result<Vec<Label>, SourceError> {
14659 optional_nodes(content.get("labels"), "content labels")?
14660 .into_iter()
14661 .flatten()
14662 .map(|v| {
14663 Ok(Label {
14664 id: NativeId(required_str(v, "id")?.to_owned()),
14665 name: required_str(v, "name")?.to_owned(),
14666 color: optional_str(v, "color")?.map(str::to_owned),
14667 })
14668 })
14669 .collect()
14670}
14671
14672/// Each board field value one item holds, as the change token's facets name it: the field's
14673/// name, with the single-select option's name or the text. A value of a type no read here
14674/// selects, and one naming no field, says nothing a person can rename and is left out.
14675fn facet_fields(field_values: &[Value]) -> Vec<(String, String)> {
14676 field_values
14677 .iter()
14678 .filter_map(|value| {
14679 let field = value.pointer("/field/name")?.as_str()?;
14680 let held = value.get("name").or_else(|| value.get("text"))?.as_str()?;
14681 Some((field.to_owned(), held.to_owned()))
14682 })
14683 .collect()
14684}
14685
14686/// The digest of the facets of the issue `content`, whose board item's values are
14687/// `field_values` — or `None` when the response did not carry, whole, every member they are read
14688/// from: the title, the parent, the sub-issue summary and the body its kind is read from, every
14689/// label, and every field value.
14690///
14691/// What a write's own response spells its token from, by exactly the rules a read takes them by.
14692fn facets_of(content: &Value, field_values: &Value) -> Option<change_token::FacetsDigest> {
14693 let ended = |connection: &Value| {
14694 connection.pointer("/pageInfo/hasNextPage") == Some(&Value::Bool(false))
14695 };
14696 let labels = content.get("labels").filter(|labels| ended(labels))?;
14697 if !ended(field_values) {
14698 return None;
14699 }
14700 let parent = content.get("parent")?;
14701 let (_, slot) = metadata_body(content.get("body")?.as_str().map(str::to_owned)).ok()?;
14702 let kind = BoardKind::of(
14703 content.get("title")?.as_str()?,
14704 !parent.is_null(),
14705 sub_issue_total(content).ok()?,
14706 ItemKind::from_metadata(&slot).ok()?,
14707 );
14708 let labels = self::labels(&json!({"labels": labels})).ok()?;
14709 let nodes = field_values.get("nodes")?.as_array()?;
14710 Some(change_token::Facets::new(kind.facet(), facet_fields(nodes), &labels).digest())
14711}
14712
14713/// The definition of each board field one item's values are values of, in the shape a read
14714/// of the board's own `fields` gives one.
14715///
14716/// A value names its field through a fragment on that field's own type, so the type is
14717/// known from which kind of value it is: a single-select value's field is a
14718/// `ProjectV2SingleSelectField`, options and all, and a text value's is a `ProjectV2Field`.
14719/// A value whose field carried no id, or an empty one, says nothing usable and is left out.
14720fn field_definitions(field_values: &[Value]) -> Vec<Value> {
14721 field_values
14722 .iter()
14723 .filter_map(|value| {
14724 let field = value.get("field")?.as_object()?;
14725 field.get("id")?.as_str().filter(|id| !id.is_empty())?;
14726 let typename = if value.get("text").is_some() {
14727 "ProjectV2Field"
14728 } else if value.get("name").is_some() {
14729 "ProjectV2SingleSelectField"
14730 } else {
14731 return None;
14732 };
14733 let mut defined = field.clone();
14734 defined.insert("__typename".to_owned(), json!(typename));
14735 // Only a text field holds a text value, so the value says the field's type too.
14736 if typename == "ProjectV2Field" {
14737 defined.insert("dataType".to_owned(), json!("TEXT"));
14738 }
14739 Some(Value::Object(defined))
14740 })
14741 .collect()
14742}
14743
14744fn text_field(field_values: &[Value], name: &str) -> Result<Option<String>, SourceError> {
14745 let Some(node) = field_values
14746 .iter()
14747 .find(|node| node.pointer("/field/name").and_then(Value::as_str) == Some(name))
14748 else {
14749 return Ok(None);
14750 };
14751 Ok(optional_str(node, "text")?.map(str::to_owned))
14752}
14753
14754fn valid_github_owner(owner: &str) -> bool {
14755 !owner.is_empty()
14756 && owner.len() <= 39
14757 && !owner.starts_with('-')
14758 && !owner.ends_with('-')
14759 && !owner.contains("--")
14760 && owner
14761 .bytes()
14762 .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
14763}
14764
14765/// GitHub's repository-name grammar: 1-100 ASCII letters, digits, `-`, `_` or `.`, and
14766/// neither of the two names a path segment already means.
14767fn valid_github_repository_name(name: &str) -> bool {
14768 !name.is_empty()
14769 && name.len() <= 100
14770 && name != "."
14771 && name != ".."
14772 && name
14773 .bytes()
14774 .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
14775}
14776
14777fn valid_environment_name(name: &str) -> bool {
14778 let mut bytes = name.bytes();
14779 bytes
14780 .next()
14781 .is_some_and(|byte| byte.is_ascii_alphabetic() || byte == b'_')
14782 && bytes.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
14783}
14784
14785/// How many sub-issues one issue has.
14786///
14787/// `Issue.subIssuesSummary` is `SubIssuesSummary!` and its `total` is `Int!`, so an
14788/// absent or non-integer one is a response this source cannot read — and reading it as
14789/// zero would classify a project as a task, which is exactly the mistake the marker
14790/// exists to keep from happening quietly.
14791fn sub_issue_total(issue: &Value) -> Result<u64, SourceError> {
14792 let summary = issue
14793 .get("subIssuesSummary")
14794 .ok_or_else(|| SourceError::Malformed {
14795 message: "GitHub issue is missing subIssuesSummary".into(),
14796 })?;
14797 summary
14798 .get("total")
14799 .and_then(Value::as_u64)
14800 .ok_or_else(|| SourceError::Malformed {
14801 message: "GitHub issue subIssuesSummary.total is not an unsigned integer".into(),
14802 })
14803}
14804
14805/// One issue's own `number`.
14806///
14807/// An issue always has one: GitHub declares `Issue.number` as `Int!` and every selection of
14808/// an issue in this module asks for it. So a read of one that comes back without it, or
14809/// with something that is not an unsigned integer, is a response this source cannot read —
14810/// absence here is **not** "this issue has no number". A draft is the content that has
14811/// none, and a draft never reaches this: the caller decides on `__typename` first, the way
14812/// it does for `subIssuesSummary`, which `DraftIssue` equally declares nothing for.
14813fn issue_number(issue: &Value) -> Result<u64, SourceError> {
14814 issue
14815 .get("number")
14816 .and_then(Value::as_u64)
14817 .ok_or_else(|| SourceError::Malformed {
14818 message: "GitHub issue number is missing or is not an unsigned integer".into(),
14819 })
14820}
14821
14822/// The `number` a creating mutation answered with, and `None` when it answered without one;
14823/// why a missing one is tolerated is at the call in `create_and_file_issue`.
14824fn created_issue_number(created: &Value) -> Result<Option<u64>, SourceError> {
14825 match created.get("number") {
14826 None | Some(Value::Null) => Ok(None),
14827 Some(value) => value
14828 .as_u64()
14829 .map(Some)
14830 .ok_or_else(|| SourceError::Malformed {
14831 message: "GitHub created issue number is not an unsigned integer".into(),
14832 }),
14833 }
14834}
14835
14836fn required_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
14837 value
14838 .get(field)
14839 .and_then(Value::as_str)
14840 .ok_or_else(|| SourceError::Malformed {
14841 message: format!("GitHub response is missing string field {field}"),
14842 })
14843}
14844
14845fn required_nonblank_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
14846 let found = required_str(value, field)?;
14847 if found.trim().is_empty() {
14848 return Err(SourceError::Malformed {
14849 message: format!("GitHub response has blank string field {field}"),
14850 });
14851 }
14852 Ok(found)
14853}
14854
14855/// The slot's delimiters, which `docs/metadata.md` settles once for every source that
14856/// needs one — Linear spells them too, in its own description field.
14857///
14858/// Restated rather than shared, because a plugin crate depends on the contract crate and
14859/// nothing else of this workspace. `scripts/check-metadata-slot-encoding.sh`, a target in
14860/// `check`, is what keeps the two one encoding: drift is otherwise quiet, since each
14861/// source round-trips its own writes perfectly well under its own spelling.
14862const METADATA_OPEN: &str = "<!-- onetaskgraph.metadata\n";
14863const METADATA_CLOSE: &str = "\n-->";
14864
14865/// What the composer puts between a non-empty visible body and the slot, and the one thing
14866/// the parser takes off the visible body when it takes the slot off — exactly once, so every
14867/// other trailing byte of the body comes back as it was written.
14868// 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.
14869const METADATA_SEPARATOR: &str = "\n\n";
14870
14871/// The visible body and the metadata slot at the end of it.
14872///
14873/// The encoding is the one `docs/metadata.md` settles for Linear, which is where its
14874/// reasons are. Only a comment at the very end is a slot; one in the middle is a person's
14875/// own content and is left alone. The visible body is everything before the slot less the
14876/// one [`METADATA_SEPARATOR`] the composer put there, byte for byte.
14877fn metadata_body(
14878 body: Option<String>,
14879) -> Result<(Option<String>, BTreeMap<String, Value>), SourceError> {
14880 let Some(body) = body else {
14881 return Ok((None, BTreeMap::new()));
14882 };
14883 let Some(slot) = slot_span(&body)? else {
14884 return Ok((Some(body), BTreeMap::new()));
14885 };
14886 let metadata =
14887 serde_json::from_str(&body[slot.encoded_start..slot.encoded_end]).map_err(|error| {
14888 SourceError::Malformed {
14889 message: format!(
14890 "invalid canonical JSON in GitHub issue onetaskgraph metadata slot: {error}"
14891 ),
14892 }
14893 })?;
14894 let before = &body[..slot.start];
14895 let visible = before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before);
14896 Ok(((!visible.is_empty()).then(|| visible.to_owned()), metadata))
14897}
14898
14899/// Where the metadata slot sits in one body, as byte offsets into it.
14900struct SlotSpan {
14901 /// Where [`METADATA_OPEN`] begins.
14902 start: usize,
14903 /// Where the encoded JSON begins, just past [`METADATA_OPEN`].
14904 encoded_start: usize,
14905 /// Where the encoded JSON ends, at the start of [`METADATA_CLOSE`].
14906 encoded_end: usize,
14907 /// Just past [`METADATA_CLOSE`].
14908 end: usize,
14909}
14910
14911/// The slot at the very end of `body`, or `None` when it has none.
14912///
14913/// The one reading of *where the slot is*, shared by [`metadata_body`], which reads it, and
14914/// [`with_slot`], which rewrites it — so the two cannot disagree about which comment is the
14915/// slot.
14916fn slot_span(body: &str) -> Result<Option<SlotSpan>, SourceError> {
14917 let Some(start) = body.rfind(METADATA_OPEN) else {
14918 return Ok(None);
14919 };
14920 let encoded_start = start + METADATA_OPEN.len();
14921 let Some(relative_end) = body[encoded_start..].find(METADATA_CLOSE) else {
14922 return Err(SourceError::Malformed {
14923 message: "unterminated onetaskgraph metadata slot in GitHub issue body".into(),
14924 });
14925 };
14926 let encoded_end = encoded_start + relative_end;
14927 let end = encoded_end + METADATA_CLOSE.len();
14928 if !body[end..].trim().is_empty() {
14929 return Ok(None);
14930 }
14931 Ok(Some(SlotSpan {
14932 start,
14933 encoded_start,
14934 encoded_end,
14935 end,
14936 }))
14937}
14938
14939/// `body` with its metadata slot holding exactly `metadata`, and every byte outside the
14940/// slot as it was.
14941///
14942/// A slot that is there has its JSON replaced in place; one that becomes empty is removed
14943/// together with the one [`METADATA_SEPARATOR`] separating it from the prose before it. A
14944/// body with no slot gains one the way [`compose_body`] writes it — after that separator,
14945/// or alone in an empty body — and a body with no slot that is given no metadata is
14946/// returned as it is.
14947fn with_slot(body: &str, metadata: &BTreeMap<String, Value>) -> Result<String, SourceError> {
14948 let encoded = if metadata.is_empty() {
14949 None
14950 } else {
14951 Some(
14952 serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
14953 message: error.to_string(),
14954 })?,
14955 )
14956 };
14957 Ok(match (slot_span(body)?, encoded) {
14958 (Some(slot), Some(encoded)) => format!(
14959 "{}{encoded}{}",
14960 &body[..slot.encoded_start],
14961 &body[slot.encoded_end..]
14962 ),
14963 (Some(slot), None) => {
14964 let before = &body[..slot.start];
14965 format!(
14966 "{}{}",
14967 before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before),
14968 &body[slot.end..]
14969 )
14970 }
14971 (None, None) => body.to_owned(),
14972 (None, Some(encoded)) if body.is_empty() => {
14973 format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
14974 }
14975 (None, Some(encoded)) => {
14976 format!("{body}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
14977 }
14978 })
14979}
14980
14981/// `body` with everything before its metadata slot replaced by `content`, and the slot
14982/// itself kept byte for byte.
14983///
14984/// The inverse of how [`metadata_body`] splits a body: the slot, when there is one, follows
14985/// `content` after the one [`METADATA_SEPARATOR`] the composer puts there — or alone, when
14986/// `content` is empty — so a read of the result reports `content` as the visible body and
14987/// the slot's metadata exactly as it was.
14988fn with_content(body: &str, content: &str) -> Result<String, SourceError> {
14989 let Some(slot) = slot_span(body)? else {
14990 return Ok(content.to_owned());
14991 };
14992 let kept = &body[slot.start..];
14993 Ok(if content.is_empty() {
14994 kept.to_owned()
14995 } else {
14996 format!("{content}{METADATA_SEPARATOR}{kept}")
14997 })
14998}
14999
15000/// Hold `entries` under `key` in one slot's metadata, or no such key when there are none.
15001fn set_task_list(metadata: &mut BTreeMap<String, Value>, key: &str, entries: &[TaskRef]) {
15002 if entries.is_empty() {
15003 metadata.remove(key);
15004 } else {
15005 metadata.insert(
15006 key.to_owned(),
15007 Value::Array(
15008 entries
15009 .iter()
15010 .map(|entry| Value::String(entry.as_str().to_owned()))
15011 .collect(),
15012 ),
15013 );
15014 }
15015}
15016
15017fn compose_body(
15018 content: Option<&str>,
15019 metadata: &BTreeMap<String, Value>,
15020) -> Result<Option<String>, SourceError> {
15021 let visible = content.unwrap_or_default();
15022 if metadata.is_empty() {
15023 return Ok((!visible.is_empty()).then(|| visible.to_owned()));
15024 }
15025 let encoded = serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
15026 message: error.to_string(),
15027 })?;
15028 Ok(Some(if visible.is_empty() {
15029 format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15030 } else {
15031 format!("{visible}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15032 }))
15033}
15034
15035fn required_bool(value: &Value, field: &str) -> Result<bool, SourceError> {
15036 value
15037 .get(field)
15038 .and_then(Value::as_bool)
15039 .ok_or_else(|| SourceError::Malformed {
15040 message: format!("GitHub response is missing boolean field {field}"),
15041 })
15042}
15043fn optional_str<'a>(value: &'a Value, field: &str) -> Result<Option<&'a str>, SourceError> {
15044 match value.get(field) {
15045 None | Some(Value::Null) => Ok(None),
15046 Some(value) => value
15047 .as_str()
15048 .map(Some)
15049 .ok_or_else(|| SourceError::Malformed {
15050 message: format!("GitHub response field {field} is not a string or null"),
15051 }),
15052 }
15053}
15054fn optional_nodes<'a>(
15055 connection: Option<&'a Value>,
15056 name: &str,
15057) -> Result<Option<&'a Vec<Value>>, SourceError> {
15058 match connection {
15059 None | Some(Value::Null) => Ok(None),
15060 Some(value) => value
15061 .get("nodes")
15062 .and_then(Value::as_array)
15063 .map(Some)
15064 .ok_or_else(|| SourceError::Malformed {
15065 message: format!("GitHub {name}.nodes is not an array"),
15066 }),
15067 }
15068}
15069fn complete_connection(connection: &Value, name: &str, size: u32) -> Result<(), SourceError> {
15070 let page_info = connection
15071 .get("pageInfo")
15072 .ok_or_else(|| SourceError::Malformed {
15073 message: format!("GitHub {name} has no pageInfo"),
15074 })?;
15075 if required_bool(page_info, "hasNextPage")? {
15076 return Err(SourceError::Malformed {
15077 message: format!(
15078 "GitHub {name} exceeds the supported nested connection size of {size}"
15079 ),
15080 });
15081 }
15082 Ok(())
15083}
15084fn optional_time(value: &Value, field: &str) -> Result<Option<DateTime<Utc>>, SourceError> {
15085 optional_str(value, field)?
15086 .map(|timestamp| {
15087 timestamp.parse().map_err(|error| SourceError::Malformed {
15088 message: format!("GitHub response field {field} is not a timestamp: {error}"),
15089 })
15090 })
15091 .transpose()
15092}
15093fn validate_page(page: &PageRequest) -> Result<(), SourceError> {
15094 if page.limit == 0 {
15095 Err(SourceError::Config {
15096 message: "page limit must be at least 1".into(),
15097 })
15098 } else {
15099 Ok(())
15100 }
15101}
15102fn next_cursor(connection: &Value) -> Result<Option<Cursor>, SourceError> {
15103 let page = connection
15104 .get("pageInfo")
15105 .filter(|value| value.is_object())
15106 .ok_or_else(|| SourceError::Malformed {
15107 message: "GitHub connection is missing pageInfo".into(),
15108 })?;
15109 if required_bool(page, "hasNextPage")? {
15110 let cursor = required_str(page, "endCursor")?;
15111 validate_cursor_progress(None, cursor)?;
15112 Ok(Some(Cursor(cursor.into())))
15113 } else {
15114 Ok(None)
15115 }
15116}
15117fn validate_cursor_progress(previous: Option<&str>, next: &str) -> Result<(), SourceError> {
15118 if next.is_empty() || previous == Some(next) {
15119 Err(SourceError::Malformed {
15120 message: "GitHub pagination cursor is empty or did not advance".into(),
15121 })
15122 } else {
15123 Ok(())
15124 }
15125}
15126/// The version of this plugin's opaque narrowing-search cursor.
15127pub const SEARCH_CURSOR_VERSION: u32 = 4;
15128
15129#[derive(Serialize, Deserialize)]
15130#[serde(tag = "state", rename_all = "snake_case", deny_unknown_fields)]
15131enum SearchConnection {
15132 Initial {},
15133 Continuing { after: Cursor },
15134 Exhausted {},
15135}
15136impl SearchConnection {
15137 fn after(&self) -> Option<&str> {
15138 match self {
15139 Self::Continuing { after } => Some(&after.0),
15140 _ => None,
15141 }
15142 }
15143 fn exhausted(&self) -> bool {
15144 matches!(self, Self::Exhausted { .. })
15145 }
15146 /// Whether a cursor naming this position, `offset` rows into its page, is one this
15147 /// plugin could have handed out: a page is resumed only part of the way through it — an
15148 /// offset of a whole page or more would skip rows nobody was given — an initial page
15149 /// only once some of it was handed out, and an exhausted connection has no page to be
15150 /// part of the way through.
15151 fn valid_resume(&self, offset: usize) -> bool {
15152 let within = offset < SEARCH_PAGE_SIZE as usize;
15153 match self {
15154 Self::Initial { .. } => offset > 0 && within,
15155 Self::Continuing { after } => !after.0.is_empty() && within,
15156 Self::Exhausted { .. } => offset == 0,
15157 }
15158 }
15159}
15160
15161/// Versioned source cursor. A zero offset and empty own-write ids are omitted.
15162#[derive(Serialize, Deserialize)]
15163#[serde(deny_unknown_fields)]
15164struct SearchPosition {
15165 version: u32,
15166 connection: SearchConnection,
15167 /// How many rows of the page `connection` starts were already handed out.
15168 #[serde(default, skip_serializing_if = "is_zero")]
15169 offset: usize,
15170 #[serde(default, skip_serializing_if = "Vec::is_empty")]
15171 seen: Vec<NativeId>,
15172 #[serde(default, skip_serializing_if = "Vec::is_empty")]
15173 own: Vec<NativeId>,
15174}
15175impl Default for SearchPosition {
15176 fn default() -> Self {
15177 Self {
15178 version: SEARCH_CURSOR_VERSION,
15179 connection: SearchConnection::Initial {},
15180 offset: 0,
15181 seen: Vec::new(),
15182 own: Vec::new(),
15183 }
15184 }
15185}
15186
15187fn is_zero(offset: &usize) -> bool {
15188 *offset == 0
15189}
15190
15191fn numeric_cursor(cursor: Option<&Cursor>) -> Result<usize, SourceError> {
15192 cursor.map_or(Ok(0), |c| {
15193 c.0.parse().map_err(|_| SourceError::Config {
15194 message: "page cursor is invalid".into(),
15195 })
15196 })
15197}
15198fn offset_page<T>(mut items: Vec<T>, offset: usize, limit: usize) -> Page<T> {
15199 if offset > items.len() {
15200 return Page::last(vec![]);
15201 }
15202 let tail = items.split_off(offset);
15203 let mut selected = tail;
15204 let next = (selected.len() > limit).then(|| Cursor((offset + limit).to_string()));
15205 selected.truncate(limit);
15206 Page {
15207 items: selected,
15208 next,
15209 }
15210}
15211
15212impl GitHubProjectsSource {
15213 async fn write_task_assets(
15214 &self,
15215 write: &ItemWrite<Task>,
15216 assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
15217 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
15218 self.write_item(
15219 &self.task_incoming(write, assets)?,
15220 write.target.as_ref(),
15221 &write.depends_on,
15222 )
15223 .await
15224 }
15225
15226 /// One task write as [`Self::write_item`] takes it, once what it delivers and its priority
15227 /// are known to be ones this source can write.
15228 fn task_incoming<'a>(
15229 &self,
15230 write: &'a ItemWrite<Task>,
15231 assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
15232 ) -> Result<Incoming<'a>, SourceError> {
15233 let near = write.target.as_ref().unwrap_or(&write.item.id);
15234 for (key, entries) in [
15235 (TaskRef::DELIVERS_KEY, &write.item.delivers),
15236 (TaskRef::DELIVERED_BY_KEY, &write.item.delivered_by),
15237 ] {
15238 TaskRef::listed(key, near, Some(&self.name), entries.clone())
15239 .map_err(|message| SourceError::Refused { message })?;
15240 }
15241 if self.priorities.is_none() && write.item.priority != Priority::None {
15242 return Err(self.holds_no_priority());
15243 }
15244 Ok(Incoming {
15245 written: Written::Work(ItemKind::Task, &write.item.status),
15246 title: &write.item.title,
15247 content: write.item.content.as_deref(),
15248 assets,
15249 labels: &write.item.labels,
15250 metadata: &write.item.metadata,
15251 repositories: &write.item.repositories,
15252 classification: write.item.classification,
15253 parent: write.item.project.as_ref(),
15254 delivers: &write.item.delivers,
15255 delivered_by: &write.item.delivered_by,
15256 priority: self.priorities.as_ref().map(|_| write.item.priority),
15257 })
15258 }
15259 async fn write_document_assets(
15260 &self,
15261 write: &ItemWrite<Document>,
15262 assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
15263 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
15264 // A document takes part in no dependency graph, so there is no far end to write
15265 // natively and none to record: a caller naming one is told so rather than having it
15266 // stored under the reserved key, where a later read would report an edge the
15267 // contract says cannot exist.
15268 if !write.depends_on.is_empty() {
15269 return Err(SourceError::Refused {
15270 message: format!(
15271 "this write names {} dependencies for a document, and a document takes \
15272 part in no dependency graph; next: put the dependency on the task or \
15273 project the document is about",
15274 write.depends_on.len()
15275 ),
15276 });
15277 }
15278 self.write_item(
15279 &Incoming {
15280 written: Written::Document,
15281 title: &write.item.title,
15282 content: write.item.content.as_deref(),
15283 assets,
15284 labels: &write.item.labels,
15285 metadata: &write.item.metadata,
15286 repositories: &write.item.repositories,
15287 classification: write.item.classification,
15288 parent: write.item.project.as_ref(),
15289 delivers: &[],
15290 delivered_by: &[],
15291 priority: None,
15292 },
15293 write.target.as_ref(),
15294 &[],
15295 )
15296 .await
15297 }
15298}
15299
15300/// What [`TaskSource::end_command`] leaves of this source's held state, asserted on the state
15301/// itself, for the two things no journey can observe.
15302///
15303/// The journeys in `crates/onetaskgraph-e2e/tests/e2e/end_command.rs` prove through the engine,
15304/// with and without the call, that a settlement, a board listing and a metadata search each
15305/// read afresh after it — the resolved records, the written-item overlay, the board and its
15306/// search, and the narrowed searches. What they cannot reach is the held field definitions,
15307/// because a status write naming an option a person deleted is refused the same whether or
15308/// not the list is held, and a poisoned lock, because nothing outside the source can panic
15309/// while one of its locks is held. So these assert those directly, and every other holder
15310/// beside them so a holder added later without a clear in the call fails here.
15311#[cfg(test)]
15312mod end_command_tests {
15313 use super::*;
15314
15315 struct Token;
15316
15317 impl SecretResolver for Token {
15318 fn get(&self, var: &str) -> Option<SecretString> {
15319 (var == "GH_PROJECTS_TOKEN").then(|| "test-token".into())
15320 }
15321 }
15322
15323 fn source() -> GitHubProjectsSource {
15324 let config = serde_json::from_value(json!({
15325 "owner": "octo-org", "project_number": 7, "repository": "acme/work",
15326 // Nothing here is sent: the source is only built and its state inspected.
15327 "endpoint": "http://127.0.0.1:9/graphql",
15328 }))
15329 .expect("a usable configuration");
15330 GitHubProjectsSource::new(&SourceName::new("work").unwrap(), config, &Token)
15331 .expect("the source builds")
15332 }
15333
15334 /// One issue as a board read answers it.
15335 fn resolved(source: &GitHubProjectsSource) -> Resolved {
15336 source
15337 .resolve(&json!({
15338 "id": "ITEM-1",
15339 "content": {"__typename": "Issue", "id": "I_1", "title": "Held",
15340 "body": "what a person may since have edited", "state": "OPEN",
15341 "stateReason": null, "url": null, "number": 1,
15342 "subIssuesSummary": {"total": 0},
15343 "labels": {"nodes": [], "pageInfo": {"hasNextPage": false}}},
15344 "fieldValues": {"nodes": [], "pageInfo": {"hasNextPage": false}},
15345 }))
15346 .expect("the item reads")
15347 .expect("an issue")
15348 }
15349
15350 /// Hold something in every holder the call clears, and the repository id it keeps.
15351 fn fill(source: &GitHubProjectsSource) {
15352 let item = resolved(source);
15353 source.created.lock().unwrap().push(item.clone());
15354 source.updated.lock().unwrap().push(item.clone());
15355 *source.board_cache.lock().unwrap() = Some(Board {
15356 id: "PVT-board".into(),
15357 fields: json!({"nodes": []}),
15358 items: vec![item.clone()],
15359 });
15360 *source.search_cache.lock().unwrap() = Some(vec![item.clone()]);
15361 source
15362 .narrowed_cache
15363 .lock()
15364 .unwrap()
15365 .insert("status:todo".into(), vec![item.clone()]);
15366 source.children_cache.lock().unwrap().insert(
15367 NativeId("P-1".into()),
15368 (NativeId("P-1".into()), vec![item.clone()]),
15369 );
15370 source
15371 .search_next
15372 .lock()
15373 .unwrap()
15374 .insert("status:todo".into(), Some("cursor".into()));
15375 source
15376 .resolved_cache
15377 .lock()
15378 .unwrap()
15379 .insert(item.id.clone(), item);
15380 *source.fields_cache.lock().unwrap() = Some(BoardFields {
15381 id: BoardId::parse("PVT-board").unwrap(),
15382 fields: json!({"nodes": []}),
15383 });
15384 source
15385 .repository_cache
15386 .lock()
15387 .unwrap()
15388 .insert(RepositoryTarget::parse("acme/work").unwrap(), "R_1".into());
15389 }
15390
15391 fn assert_dropped(source: &GitHubProjectsSource) {
15392 assert!(source.created().unwrap().is_empty(), "created");
15393 assert!(source.updated().unwrap().is_empty(), "updated");
15394 assert!(source.board_cache().unwrap().is_none(), "board");
15395 assert!(source.search_cache.lock().unwrap().is_none(), "search");
15396 assert!(source.narrowed_cache.lock().unwrap().is_empty(), "narrowed");
15397 assert!(
15398 source.children_cache.lock().unwrap().is_empty(),
15399 "project children"
15400 );
15401 assert!(
15402 source.search_next.lock().unwrap().is_empty(),
15403 "search paging"
15404 );
15405 assert!(
15406 source.resolved_cache().unwrap().is_empty(),
15407 "resolved records"
15408 );
15409 assert!(source.fields_cache().unwrap().is_none(), "board fields");
15410 assert_eq!(
15411 source.repository_cache().unwrap().len(),
15412 1,
15413 "a repository's node id stays valid and is kept"
15414 );
15415 }
15416
15417 fn end(source: &GitHubProjectsSource) {
15418 tokio::runtime::Builder::new_current_thread()
15419 .build()
15420 .unwrap()
15421 .block_on(source.end_command())
15422 .expect("the command ends");
15423 }
15424
15425 #[test]
15426 fn the_call_drops_every_item_search_and_board_read_and_keeps_repository_ids() {
15427 let source = source();
15428 fill(&source);
15429 end(&source);
15430 assert_dropped(&source);
15431 }
15432
15433 #[test]
15434 fn the_call_clears_a_lock_an_earlier_failure_poisoned() {
15435 fn poison<T: Send>(held: &Mutex<T>) {
15436 std::thread::scope(|scope| {
15437 let _ = scope
15438 .spawn(|| {
15439 let _guard = held.lock().unwrap();
15440 panic!("a failure while the lock is held");
15441 })
15442 .join();
15443 });
15444 assert!(held.is_poisoned());
15445 }
15446 let source = source();
15447 fill(&source);
15448 poison(&source.created);
15449 poison(&source.updated);
15450 poison(&source.board_cache);
15451 poison(&source.search_cache);
15452 poison(&source.narrowed_cache);
15453 poison(&source.search_next);
15454 poison(&source.resolved_cache);
15455 poison(&source.fields_cache);
15456 assert!(
15457 source.resolved_cache().is_err(),
15458 "a poisoned lock is refused before the call"
15459 );
15460 end(&source);
15461 assert_dropped(&source);
15462 }
15463}
15464
15465#[cfg(test)]
15466mod unprojectable_fields_tests {
15467 use super::*;
15468
15469 /// The field names `docs/metadata.md` says a `metadata_fields` entry may not name.
15470 fn documented() -> std::collections::BTreeSet<String> {
15471 let document = std::fs::read_to_string(
15472 std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../docs/metadata.md"),
15473 )
15474 .expect("docs/metadata.md is readable");
15475 let document = document.split_whitespace().collect::<Vec<_>>().join(" ");
15476 let lead = "a `field` equal, ignoring case, to ";
15477 let start = document.find(lead).expect("the refused-field sentence") + lead.len();
15478 let sentence = &document[start..];
15479 let sentence = &sentence[..sentence
15480 .find("; and two entries naming one field")
15481 .expect("the refused-field sentence ends at the duplicate rule")];
15482 let (own, github) = sentence
15483 .split_once(" or a field GitHub owns on every board (")
15484 .expect("the sentence names GitHub's own fields");
15485 own.split('`')
15486 .skip(1)
15487 .step_by(2)
15488 .chain(
15489 github
15490 .strip_suffix(')')
15491 .expect("GitHub's own fields close the sentence")
15492 .split(", "),
15493 )
15494 .map(str::to_owned)
15495 .collect()
15496 }
15497
15498 #[test]
15499 fn the_documented_refused_fields_are_exactly_the_ones_the_configuration_refuses() {
15500 let refused: std::collections::BTreeSet<String> = UNPROJECTABLE_FIELDS
15501 .iter()
15502 .map(|&name| name.to_owned())
15503 .collect();
15504 assert_eq!(documented(), refused, "the fields docs/metadata.md lists");
15505 let instance = SourceName::new("work").unwrap();
15506 for name in UNPROJECTABLE_FIELDS {
15507 for spelled in [name.to_owned(), name.to_uppercase()] {
15508 let entry = MetadataFieldConfig {
15509 field: spelled.clone(),
15510 key: "team.machine".to_owned(),
15511 path: Vec::new(),
15512 };
15513 let refused = MetadataField::resolve(vec![entry], &instance)
15514 .expect_err("an unprojectable field is refused");
15515 assert!(
15516 refused.to_string().contains(&format!("{name:?}")),
15517 "{spelled}: {refused}"
15518 );
15519 }
15520 }
15521 }
15522}