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 projection;
658mod visibility;
659
660use accounting::Accounting;
661
662/// The registry name for this plugin.
663pub const KIND: &str = "github-projects";
664/// GitHub's maximum connection page size.
665pub const MAX_PAGE_SIZE: u32 = 100;
666/// Every page of a board-scoped narrowing search: 20 rows, one point of SEARCH_ISSUES, the
667/// most one point buys. GitHub prices that document by rows, so pages of 20 cost what pages
668/// of 100 cost per row, and a page of fewer than 20 costs the same one point.
669pub const SEARCH_PAGE_SIZE: u32 = 20;
670/// How many items one [`graphql::ISSUE_DETAILS`] request reads, each with the first page of
671/// its comments: the largest batch the node-count model prices at one point.
672///
673/// Each aliased item is resolved once, and what GitHub charges for it is the connections
674/// under it — its labels, its page of board memberships, the field values of each of those
675/// three memberships, and its comments: six requests' worth of the aggregate GitHub divides
676/// by a hundred and rounds. Twenty-four items come to 144, which rounds to one point;
677/// twenty-five come to 150, which rounds to two. `tests/point_cost.rs` prices the document at
678/// one point and fails if one item more would still be priced at one.
679pub const DETAIL_BATCH: usize = 24;
680/// How many issue references one [`graphql::ISSUES_BY_REFERENCE`] request reads.
681///
682/// Each reference asks GitHub for one connection — a page of its board memberships, each with
683/// its `Status` value read by name rather than through a connection of field values — so fifty
684/// come to fifty requests' worth of the aggregate GitHub divides by a hundred, which is its
685/// one-point minimum, and the batch a landing's closures are read in costs one point per fifty
686/// issues per source. `tests/point_cost.rs` prices the document at one point.
687pub const REFERENCE_BATCH: usize = 50;
688/// How many items one [`graphql::ISSUE_COPY_DETAILS`] request reads: each with the first page
689/// of its comments and its `blockedBy`, the largest batch the node-count model prices at one
690/// point.
691///
692/// The six requests' worth an item of [`DETAIL_BATCH`] costs, and its `blockedBy` connection
693/// besides: seven. Twenty-one items come to 147, which rounds to one point; twenty-two come to
694/// 154, which rounds to two. `tests/point_cost.rs` prices the document at one point and fails
695/// if one item more would still be priced at one.
696pub const COPY_DETAIL_BATCH: usize = 21;
697/// How many mutations one aliased request of a copy's creates, closes and updates carries:
698/// [`graphql::CREATE_ISSUES`], [`graphql::CLOSE_ISSUES`] and [`graphql::UPDATE_ISSUES`].
699///
700/// Eleven is what GitHub was seen to accept in one request — the timing spike
701/// (`board-copy-speed-2026-10-18-spike-copy-timing-report`) sent eleven aliased mutations of four
702/// kinds, three `createIssue`, three `addProjectV2ItemById`, three field updates, an
703/// `addSubIssue` and an `addBlockedBy`, and every one ran — and no more was established.
704pub const MUTATION_BATCH: usize = 11;
705/// How many aliased writes one request of a copy's board field values and blocked-by edges
706/// carries: [`graphql::WRITE_FIELD_VALUES`], [`graphql::ADD_BLOCKED_BYS`] and, for a request
707/// holding both kinds, [`graphql::WRITE_FIELDS_AND_EDGES`].
708///
709/// Nineteen is the most aliases the feasibility spike
710/// (`board-copy-gaps-2026-10-24-spike-feasibility-report`) sent in one mutation request and
711/// saw GitHub price at the one point every mutation request costs; no larger request was
712/// established, so a copy holding more is split.
713pub const WRITE_BATCH: usize = 19;
714/// How many issues one [`graphql::DELETE_ISSUES`] request deletes: one request of eleven
715/// aliased `deleteIssue` met `RESOURCE_LIMITS_EXCEEDED` from the eighth alias on, and batches
716/// of five succeeded (the feasibility spike's undo-batching run).
717pub const DELETE_BATCH: usize = 5;
718/// How many origins one [`graphql::ORIGIN_LOOKUPS`] request asks about: the nine records of a
719/// plan and the project's document, and no more, since each carries a page of the board's field
720/// filter and one of the issue search.
721pub const ORIGIN_BATCH: usize = 10;
722
723/// The most nodes any one document this source sends may be asked to return.
724///
725/// GitHub's own published per-query ceiling, taken from
726/// [`github_graphql_node_count::NODE_LIMIT`] rather than written out again here, so this
727/// workspace cannot hold a stale copy of somebody else's number. A query above it is
728/// **refused before it is executed**, whoever is asking and whatever board they are
729/// asking about — so this is a bound on the documents rather than a budget that runs out.
730///
731/// This is `nodeCount`, the maximum number of nodes *one query may return*. It is not
732/// `cost`, the rate-limit points a call spends against an hourly allowance shared by
733/// everything the credential does — two numbers against two limits, and this constant
734/// bounds only the first. The second is computed offline too, per document:
735/// [`worst_case_point_cost`], pinned for every document in [`graphql::DOCUMENTS`] by
736/// `tests/point_cost.rs`, and reconciled against GitHub's own `cost` by the credentialed
737/// lane. There is no constant like this one to hold a price under, because points are an
738/// hourly allowance rather than a per-call bound.
739///
740/// Neither is a session's price. What `session-cost.md` records of a whole session is its
741/// **requests** and its **worst-case nodes**; what a whole session spends in points is
742/// reported only by a credentialed run's own `x-ratelimit-*` headers, through
743/// [`accounting`]. The module section on the three ways this source reaches an item says how
744/// the count is arrived at, and which of the page sizes below decide it.
745pub const NODE_COUNT_LIMIT: u64 = github_graphql_node_count::NODE_LIMIT;
746
747/// Nested connection size for the connections that hang off one item.
748///
749/// It multiplies through every document that reaches an item under a page — the count
750/// rules multiply down a nested path — so it is the constant [`NODE_COUNT_LIMIT`] is most
751/// sensitive to. `tests/node_count.rs` is what holds the pair together: it recomputes
752/// every document under these constants and fails naming any that reaches the limit, so
753/// raising this is caught there rather than by GitHub.
754const NESTED_PAGE_SIZE: u32 = 50;
755/// How many of one issue's board memberships are read when an issue is reached directly.
756///
757/// An issue reached through a search or through its own node id carries its board half in
758/// `Issue.projectItems`, and only the entry for *this* board is read. This connection sits
759/// under a page of issues, so every point of it multiplies through the whole document and
760/// is paid for whether or not any issue is on a second board — which is why it is
761/// deliberately far smaller than [`NESTED_PAGE_SIZE`].
762///
763/// **Three, because what a page misses is now recovered rather than refused**, and the
764/// recovery is what the value is chosen against. An issue whose entry for this board sits
765/// past this page costs one further request — [`graphql::ISSUE_BOARD_ITEMS`], resumed from
766/// that page's own cursor — so the value trades a bound every read pays for a request only
767/// a multi-board issue pays. At one, a deployment whose issues commonly sit on two or more
768/// boards would pay that request *per issue*, which is order N against the one page per
769/// hundred issues a read costs today. At three it is only reached by an issue on four or
770/// more boards at once, which keeps the recovery path exceptional rather than routine for
771/// a plausible deployment.
772const BOARD_ITEMS_PAGE_SIZE: u32 = 3;
773/// How many carriers of one copy origin one page of [`graphql::ORIGIN_LOOKUP`] asks each of
774/// its two connections for.
775///
776/// An origin names one item, so the answer an origin lookup expects is one carrier, and a
777/// second is a duplicate a copy already takes the first of. Both connections are walked to
778/// exhaustion whatever this is, so it decides how many requests an unusual answer costs and
779/// never what the answer is. It is small because every point of it is paid on every lookup,
780/// and a copy makes one lookup per item it has no link for: at three, ten lookups cost fewer
781/// worst-case nodes than the one whole-board read they replaced.
782const ORIGIN_PAGE_SIZE: u32 = 3;
783/// How many carriers of each origin the batched [`graphql::ORIGIN_LOOKUPS`] asks each of its two
784/// connections for: one, the one carrier an origin names.
785///
786/// A second carrier still shows: the page then says there is more, and that origin is left to
787/// its own [`graphql::ORIGIN_LOOKUP`], which walks every page at `ORIGIN_PAGE_SIZE` — so a
788/// duplicate is found exactly as before, by the lookup that reads all of them. The feasibility
789/// spike (`board-copy-gaps-2026-10-24-spike-feasibility-report`) priced the batch of ten
790/// origins at two points with a first page of three and at one with a first page of one.
791const ORIGIN_LOOKUPS_PAGE_SIZE: u32 = 1;
792
793/// How many of one project's sub-issues one [`graphql::SUB_ISSUES`] page asks for: the most
794/// one point buys, since every row carries six connections GitHub charges for once per row.
795/// `session-cost.md` derives it; `tests/point_cost.rs` prices the document at one point and
796/// fails if one row more would still be priced at one.
797pub const MEMBER_PAGE_SIZE: u32 = 23;
798
799pub use github_graphql_node_count::{NodeCountError, Variables};
800
801/// The largest value this source can bind to each page-size variable its documents name.
802///
803/// Every `first:` in [`graphql`] reads one of these six, and each is capped at the
804/// constant above it wherever a caller's own limit could reach it — `$first` at
805/// [`MAX_PAGE_SIZE`], `$nestedFirst` at `NESTED_PAGE_SIZE`, `$boardItems` at
806/// `BOARD_ITEMS_PAGE_SIZE`, `$originFirst` at `ORIGIN_PAGE_SIZE`, `$lookupFirst` at
807/// `ORIGIN_LOOKUPS_PAGE_SIZE`, `$memberFirst` at [`MEMBER_PAGE_SIZE`]. So this is the worst case a caller can drive this source to,
808/// not one configuration of it, which is what makes a bound computed under it a bound on
809/// every read.
810pub fn largest_page_sizes() -> Variables {
811 Variables::from([
812 ("first".to_owned(), MAX_PAGE_SIZE),
813 ("nestedFirst".to_owned(), NESTED_PAGE_SIZE),
814 ("boardItems".to_owned(), BOARD_ITEMS_PAGE_SIZE),
815 ("originFirst".to_owned(), ORIGIN_PAGE_SIZE),
816 ("lookupFirst".to_owned(), ORIGIN_LOOKUPS_PAGE_SIZE),
817 ("memberFirst".to_owned(), MEMBER_PAGE_SIZE),
818 ])
819}
820
821/// The most nodes `document` could be asked to return, by GitHub's published rules.
822///
823/// Computed offline from the document's own text under [`largest_page_sizes`] — no
824/// network, no credential and no schema — by
825/// [`github_graphql_node_count::node_count`], which is where the rules themselves live.
826/// A document at or above [`NODE_COUNT_LIMIT`] is one GitHub refuses before executing, so
827/// this is what a check holds every document in [`graphql::DOCUMENTS`] below.
828///
829/// # Errors
830///
831/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
832/// no single operation, or binds a page size this source does not name — each of which is
833/// a defect in the document rather than a number.
834pub fn worst_case_node_count(document: &str) -> Result<u64, NodeCountError> {
835 node_count(document, &largest_page_sizes())
836}
837
838/// The most rate-limit points one call of `document` could spend, by GitHub's published
839/// rules.
840///
841/// Computed offline from the document's own text under [`largest_page_sizes`] — no
842/// network, no credential and no schema — by
843/// [`github_graphql_node_count::point_cost`], which is where the rules themselves live.
844/// This is `cost`, metered **per hour** against the allowance one credential shares across
845/// everything it does; it is not `nodeCount`, which is [`worst_case_node_count`] and is
846/// bounded per query by [`NODE_COUNT_LIMIT`]. There is no per-call ceiling to hold this
847/// under, so what `tests/point_cost.rs` does with it is pin every document in
848/// [`graphql::DOCUMENTS`] at what it costs, and the credentialed lane reconciles those
849/// figures against GitHub's own reported `cost`.
850///
851/// # Errors
852///
853/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
854/// no single operation, or binds a page size this source does not name — each of which is
855/// a defect in the document rather than a number.
856pub fn worst_case_point_cost(document: &str) -> Result<u64, NodeCountError> {
857 github_graphql_node_count::point_cost(document, &largest_page_sizes())
858}
859
860/// The most nodes `document` could be asked to return under `variables`.
861///
862/// [`worst_case_node_count`] is this under [`largest_page_sizes`], and the accounting in
863/// [`accounting`] is this under the bindings one request really sent — one spelling of the
864/// calculation, so a bound checked offline and a cost recorded at run time cannot come to
865/// disagree. The rules themselves live in [`github_graphql_node_count::node_count`].
866///
867/// # Errors
868///
869/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
870/// no single operation, or binds a page size `variables` does not name.
871pub fn node_count(document: &str, variables: &Variables) -> Result<u64, NodeCountError> {
872 github_graphql_node_count::node_count(document, variables)
873}
874
875/// The issue-title prefix that makes a board issue a document.
876///
877/// A GitHub Projects board has no document type — it holds issues — so the discriminator
878/// is the title, and this is the whole of it: an issue whose title begins with these bytes
879/// is a document and every other issue is the task or project the sub-issue rule makes it.
880///
881/// It is spelled **once**, here, and read rather than restated everywhere else — including
882/// by the shared journeys, which take it from this constant so a board fixture cannot
883/// drift from what this source reads. `docs/metadata.md` records the two consequences that
884/// are not obvious from the bytes: the reported title has this prefix taken off, exactly
885/// as the body's metadata slot is taken off `content`, and this prefix is read *before*
886/// the sub-issue rule, so a design issue with no sub-issues is never an empty project.
887pub const DESIGN_TITLE_PREFIX: &str = "DESIGN: ";
888
889/// Exact GraphQL query documents issued by this plugin.
890///
891/// Keeping the production documents here lets the pinned-schema test validate the same
892/// bytes that are sent to GitHub, rather than a test-only copy which could drift
893/// independently. [`STATUS_OPTIONS_UPDATE`] is the sole document that may rewrite a board
894/// field, and its guarded caller always supplies the complete existing option set with ids.
895pub mod graphql {
896 /// The board half of one item: the field values every document here reads it from.
897 ///
898 /// A macro for the same reason [`board_issue!`] below is one, a level further in. This
899 /// selection is needed by that fragment, by [`BOARD`] under the board's own `items`,
900 /// and by [`ISSUE_BOARD_ITEMS`] under a membership walk — and all three have to produce
901 /// *the same value*, because
902 /// [`GitHubProjectsSource::resolve`](super::GitHubProjectsSource) reads them through
903 /// one path. Three spellings of it is what would drift, so there is one.
904 ///
905 /// The `Status` option and this source's own origin text field are the whole of it. It
906 /// selects no `ProjectV2ItemFieldLabelValue`: GitHub derives that field from the item's
907 /// content, so it holds nothing the content's own `labels` do not already say, and it
908 /// would sit a label connection two page sizes deep.
909 macro_rules! board_item_values {
910 () => {
911 r#"fieldValues(first:$nestedFirst){nodes{
912 ... on ProjectV2ItemFieldSingleSelectValue{name field{
913 ... on ProjectV2SingleSelectField{id name options{id name}}
914 }}
915 ... on ProjectV2ItemFieldTextValue{text field{... on ProjectV2Field{id name}}}
916 }pageInfo{hasNextPage}}"#
917 };
918 }
919
920 /// The variable every document reading an issue declares for the relationship events its
921 /// change token counts, with its value as the default, so no caller binds it.
922 ///
923 /// A variable rather than a literal list because every argument a production document
924 /// passes is a declared variable, which `tests/schema.rs` holds them to, and every member of
925 /// its default to the pinned enum. Its default is the six event types GitHub records a
926 /// relationship added or removed with: a dependency on either end, as `blockedBy` and
927 /// `blocking`, so one a person makes from the far end moves this end's count too; and a
928 /// project membership on the task, as its parent, which GitHub records there whichever
929 /// issue the move was made from. A sub-issue's events on its parent are not counted: a
930 /// project carries no task token, and a copy writes nothing of a task its own sub-issues
931 /// decide. [`super::change_token`] says why the count is in the token.
932 macro_rules! relationship_events_variable {
933 () => {
934 ",$relationshipEvents:[IssueTimelineItemsItemType!]=[BLOCKED_BY_ADDED_EVENT,BLOCKED_BY_REMOVED_EVENT,BLOCKING_ADDED_EVENT,BLOCKING_REMOVED_EVENT,PARENT_ISSUE_ADDED_EVENT,PARENT_ISSUE_REMOVED_EVENT]"
935 };
936 }
937
938 /// Everything this source reads about one issue, wherever it reaches that issue.
939 ///
940 /// A macro rather than a constant so the three documents below can `concat!` it: one
941 /// spelling of these fields is what makes an issue read through the board-scoped
942 /// search, through its own node id, and through its project's sub-issue relationship
943 /// resolve to *the same* item, which is the whole of what
944 /// [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource) relies on.
945 ///
946 /// `projectItems` is what carries the board half of an issue: the board item's own id
947 /// and the [`board_item_values!`] above — the `Status` option and this source's origin
948 /// text field — that a `ProjectV2.items` read used to carry. It is asked for on the
949 /// issue rather than on the board, which is what makes the cost of a read proportional
950 /// to what was asked for instead of to the board's size.
951 ///
952 /// It carries a *page* of that connection, at `BOARD_ITEMS_PAGE_SIZE`, and its
953 /// `endCursor` is what [`ISSUE_BOARD_ITEMS`] resumes from when this board's entry is
954 /// not on that page: a page here is where the search for the entry starts rather than
955 /// where it ends.
956 ///
957 /// It does **not** select the board's `Labels` field value, and that is the whole of
958 /// what keeps the three documents below under [`NODE_COUNT_LIMIT`](super::NODE_COUNT_LIMIT):
959 /// a label connection there sits under `fieldValues` under `projectItems` under a page
960 /// of issues, spending `$nestedFirst` twice down one path, and took
961 /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] to 2,556,100 nodes against a limit of 500,000.
962 /// No label is lost — this is a fragment `on Issue`, whose own `labels` are selected
963 /// above, and that connection is where every label this source reports comes from. No
964 /// document in this module selects the board field any longer, [`BOARD`] included; the
965 /// module documentation records why nothing it could have held is lost.
966 macro_rules! board_issue {
967 () => {
968 concat!(
969 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}
970 labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}
971 projectItems(first:$boardItems){nodes{id updatedAt project{id number}
972 "#,
973 board_item_values!(),
974 r#"}pageInfo{hasNextPage endCursor}}}"#
975 )
976 };
977 }
978
979 /// Every issue of one board, found by a search scoped to that board.
980 ///
981 /// This is how the projects a board holds are listed, and it selects no `items`
982 /// connection on `ProjectV2`: the board is a *qualifier of the search* rather than a
983 /// container walked page by page, so nothing nested inside a board item is paid for.
984 /// Which of the issues it returns is a project is then read off `parent` — GitHub
985 /// accepts `-has:parent` as a search qualifier and silently ignores it, so the
986 /// discriminator has to be applied to the field, which is a scalar on the issue and
987 /// costs nothing.
988 pub const SEARCH_ISSUES: &str = concat!(
989 r#"query($search:String!,$type:SearchType!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
990 relationship_events_variable!(),
991 r#"){
992 search(query:$search,type:$type,first:$first,after:$after){
993 pageInfo{hasNextPage endCursor}
994 nodes{__typename ...BoardIssue}
995 }
996 }"#,
997 board_issue!()
998 );
999
1000 /// What a dependency read selects of each far end: enough to say which kind of item it
1001 /// is, its body included for the kind marker.
1002 macro_rules! related_issue {
1003 () => {
1004 " fragment Related on Issue{id title body parent{id} subIssuesSummary{total}}"
1005 };
1006 }
1007
1008 /// One issue by its own node id, which is what a qualified id names here — with what a
1009 /// write of it needs and the issue does not carry in `board_issue!`: the field
1010 /// definitions of the boards it sits on, and the far ends of its `blockedBy`.
1011 ///
1012 /// Strongly consistent, unlike the search above: GitHub's issue search is an index and
1013 /// answers a write made moments ago with the value from before it, and resolving a node
1014 /// id does not.
1015 ///
1016 /// **Why those two ride here and not on the fragment.** A copy or an update of an item
1017 /// reads it by its own id, and with them that one read answers everything the write
1018 /// needs: which option ids the board's `Status` and `Priority` fields hold — so no
1019 /// [`BOARD_FIELDS`] — and which issues block it, with each one's kind — so no
1020 /// [`ISSUE_DEPENDENCIES`]. On `board_issue!` they would sit under the hundred-issue
1021 /// pages of [`SEARCH_ISSUES`] and [`SUB_ISSUES`], multiplying both documents' price. Here
1022 /// they sit under one item, and this read is still one point.
1023 pub const ISSUE: &str = concat!(
1024 r#"query($id:ID!,$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1025 relationship_events_variable!(),
1026 r#"){
1027 node(id:$id){__typename ...BoardIssue ... on Issue{
1028 boards:projectItems(first:$boardItems){nodes{project{id number fields(first:$nestedFirst){nodes{
1029 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1030 ... on ProjectV2Field{__typename id name dataType}
1031 }pageInfo{hasNextPage}}}}}
1032 blockedBy(first:$first){nodes{...Related}pageInfo{hasNextPage endCursor}}
1033 }}
1034 }"#,
1035 board_issue!(),
1036 related_issue!()
1037 );
1038
1039 /// One document by its own node id, as [`ISSUE`] reads one, with the project it is filed
1040 /// under and that project's members beside it: the project issue whole, and a page of its
1041 /// sub-issues, each as [`SUB_ISSUES`] carries one but with no page of what blocks it.
1042 ///
1043 /// What a guarded write of a document's metadata reads, and what reading a document by its
1044 /// id is: a write to a board the boundary screens is held to who may read the document,
1045 /// which is the project's and every member's classification as much as its own, and here
1046 /// the one request that holds the version the write is compared against answers all of it.
1047 /// Its members carry no edges, which nothing classifying them reads.
1048 pub const DOCUMENT_IN_PROJECT: &str = concat!(
1049 r#"query($id:ID!,$first:Int!,$memberFirst:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1050 relationship_events_variable!(),
1051 r#"){
1052 node(id:$id){__typename ...BoardIssue ... on Issue{
1053 boards:projectItems(first:$boardItems){nodes{project{id number fields(first:$nestedFirst){nodes{
1054 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1055 ... on ProjectV2Field{__typename id name dataType}
1056 }pageInfo{hasNextPage}}}}}
1057 blockedBy(first:$first){nodes{...Related}pageInfo{hasNextPage endCursor}}
1058 project:parent{__typename ...BoardIssue
1059 subIssues(first:$memberFirst){pageInfo{hasNextPage endCursor} nodes{__typename ...BoardIssue}}
1060 }
1061 }}
1062 }"#,
1063 board_issue!(),
1064 related_issue!()
1065 );
1066
1067 /// One project's tasks: the sub-issues of the issue that project is, each with a page of
1068 /// what blocks it.
1069 ///
1070 /// Paged at `$memberFirst` — [`MEMBER_PAGE_SIZE`](super::MEMBER_PAGE_SIZE), the most rows
1071 /// one point buys — rather than at `$first`, because every row carries six connections and
1072 /// a hundred-row page cost six points to list a project of ten.
1073 ///
1074 /// The work this costs is the project's own size. Nothing about it grows as the board
1075 /// gains projects, or as those projects gain tasks.
1076 ///
1077 /// **Why `blockedBy` rides here and on no other page of issues.** What reads a project's
1078 /// tasks reads their edges next — `project graph` draws them, a copy carries them — and
1079 /// without them here that is one [`ISSUE_DEPENDENCIES`] per task, so the requests a graph
1080 /// costs grow with its tasks rather than with the pages of them. Carried here, a task
1081 /// blocked by no more than `$nestedFirst` issues answers its forward edges from this read,
1082 /// exactly as an [`ISSUE`] read of it does, and only one blocked by more is asked again.
1083 /// It adds a connection under each issue of the page — one rate-limit point per page,
1084 /// and `$nestedFirst` nodes per issue — and is kept off [`SEARCH_ISSUES`], whose pages
1085 /// answer questions that never read an edge.
1086 ///
1087 /// **The project issue itself rides here too**, with the page of what blocks it, because
1088 /// the reader of a project's tasks reads the project next: a copy writing the project reads
1089 /// it and its edges before it writes them. Carried on the request that already names the
1090 /// project, the first page answers that read and the dependency read after it, at the
1091 /// state GitHub answered in this command, rather than spending one more read on an item
1092 /// the cache's token cannot vouch for whole. Each sub-issue is kept the same way, so a
1093 /// copy's read of a task this listing carried is answered by it rather than sent again.
1094 /// Only a copy's read-before-write takes them; every other read by id reads GitHub.
1095 pub const SUB_ISSUES: &str = concat!(
1096 r#"query($id:ID!,$memberFirst:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1097 relationship_events_variable!(),
1098 r#"){
1099 node(id:$id){__typename ...BoardIssue
1100 ... on Issue{blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}
1101 subIssues(first:$memberFirst,after:$after){
1102 pageInfo{hasNextPage endCursor}
1103 nodes{__typename ...BoardIssue ... on Issue{
1104 blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}
1105 }}
1106 }}}
1107 }"#,
1108 board_issue!(),
1109 related_issue!()
1110 );
1111
1112 /// What a read of the board's own `items` selects of each item's content.
1113 ///
1114 /// A macro for the reason [`board_item_values!`] is one: [`BOARD`] and [`ORIGIN_LOOKUP`]
1115 /// both walk `ProjectV2.items` and hand each item to one resolver, so they select its
1116 /// content by one spelling.
1117 macro_rules! board_item_content {
1118 () => {
1119 r#" content{
1120 ... 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}}}
1121 ... on PullRequest{__typename id}
1122 ... on DraftIssue{__typename id title body createdAt updatedAt}
1123 }"#
1124 };
1125 }
1126
1127 /// Reads the board's fields and one page of its items.
1128 pub const BOARD: &str = concat!(
1129 r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$nestedFirst:Int!,$duplicates:Boolean!"#,
1130 relationship_events_variable!(),
1131 r#"){
1132 owner:repositoryOwner(login:$owner){
1133 ... on ProjectV2Owner{projectV2(number:$number){...Board}}
1134 }
1135 } fragment Board on ProjectV2 { id title
1136 fields(first:$nestedFirst){nodes{
1137 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1138 ... on ProjectV2Field{__typename id name dataType}
1139 }pageInfo{hasNextPage}}
1140 items(first:$first,after:$after){nodes{id updatedAt "#,
1141 board_item_values!(),
1142 board_item_content!(),
1143 r#"} pageInfo{hasNextPage endCursor}}
1144 }"#
1145 );
1146
1147 /// Every carrier of one copy origin, by two reads in one request, and nothing else of
1148 /// the board.
1149 ///
1150 /// **`originItems`** is the board's own items narrowed by its own field filter —
1151 /// `ProjectV2.items(query:)`, which GitHub's schema declares as "Search query for
1152 /// filtering items" — to those whose `onetaskgraph.origin` text field holds the
1153 /// qualified id, quoted. It reads the field every carrier already holds, whichever release
1154 /// wrote it, and matches it exactly: measured on 2026-09-29 against a 394-item board,
1155 /// the quoted, the unquoted and the bare-value spellings each returned exactly the one
1156 /// carrier and a prefix of the value returned none. It is `ProjectV2.items`, so it lags a
1157 /// fresh `addProjectV2ItemById` the way that connection does.
1158 ///
1159 /// **`search`** is the board-scoped issue search for the same id as a quoted phrase in
1160 /// the body, which is where this source mirrors the origin into its metadata slot. GitHub
1161 /// indexes that comment, and the index catches up with a write in a second or two rather
1162 /// than in minutes, so it finds a carrier another process wrote that the first read is
1163 /// still behind on.
1164 ///
1165 /// Each connection pages at `$originFirst`, its own small size — see `ORIGIN_PAGE_SIZE`
1166 /// — and resumes from its own cursor; a connection already walked to its end is resumed
1167 /// from its last cursor, which answers an empty page. Every candidate either read returns
1168 /// is confirmed against its own origin field before it is reported, so a token match of
1169 /// the search or anything else the filter admits never is.
1170 ///
1171 /// The root is aliased `originItems` rather than `owner`, so nothing counting the board's
1172 /// own whole reads counts this one among them.
1173 pub const ORIGIN_LOOKUP: &str = concat!(
1174 r#"query($owner:String!,$number:Int!,$filter:String!,$search:String!,$type:SearchType!,$originFirst:Int!,$itemsAfter:String,$searchAfter:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1175 relationship_events_variable!(),
1176 r#"){
1177 originItems:repositoryOwner(login:$owner){
1178 ... on ProjectV2Owner{projectV2(number:$number){
1179 items(first:$originFirst,after:$itemsAfter,query:$filter){nodes{id updatedAt "#,
1180 board_item_values!(),
1181 board_item_content!(),
1182 r#"} pageInfo{hasNextPage endCursor}}
1183 }}
1184 }
1185 search(query:$search,type:$type,first:$originFirst,after:$searchAfter){
1186 pageInfo{hasNextPage endCursor}
1187 nodes{__typename ...BoardIssue}
1188 }
1189 }"#,
1190 board_issue!()
1191 );
1192
1193 /// Every closed item of the board, by the board's own field filter — what
1194 /// `task status reconcile` reads, and nothing else of the board.
1195 ///
1196 /// **`ProjectV2.items(query:)` narrowed by `$filter`**, which this source binds to
1197 /// `is:closed` alone, so a board whose open items are most of it is not read whole to find
1198 /// the closed ones. It is narrowed rather than trusted: every item it answers is confirmed
1199 /// closed, with a reason and a card that contradict each other, from the item node it
1200 /// already carries, so a filter that also let open items through would cost reads and
1201 /// write nothing wrong.
1202 ///
1203 /// The board's own id and field definitions ride along, as [`BOARD`] carries them, so the
1204 /// option writes that follow need no read of the board. The root is aliased `closedItems`
1205 /// rather than `owner`, so nothing counting the board's own whole reads counts this one
1206 /// among them.
1207 pub const CLOSED_ITEMS: &str = concat!(
1208 r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$filter:String!,$nestedFirst:Int!,$duplicates:Boolean!"#,
1209 relationship_events_variable!(),
1210 r#"){
1211 closedItems:repositoryOwner(login:$owner){
1212 ... on ProjectV2Owner{projectV2(number:$number){id
1213 fields(first:$nestedFirst){nodes{
1214 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1215 ... on ProjectV2Field{__typename id name dataType}
1216 }pageInfo{hasNextPage}}
1217 items(first:$first,after:$after,query:$filter){nodes{id updatedAt "#,
1218 board_item_values!(),
1219 board_item_content!(),
1220 r#"} pageInfo{hasNextPage endCursor}}
1221 }}
1222 }
1223 }"#
1224 );
1225
1226 /// One alias of [`ISSUES_BY_REFERENCE`]: the issue `$owner<n>/$name<n>#$number<n>` names.
1227 macro_rules! reference_alias {
1228 ($n:literal) => {
1229 concat!(
1230 "\n r",
1231 stringify!($n),
1232 ":repository(owner:$owner",
1233 stringify!($n),
1234 ",name:$name",
1235 stringify!($n),
1236 "){issue(number:$number",
1237 stringify!($n),
1238 "){...HeldIssue}}"
1239 )
1240 };
1241 }
1242
1243 /// [`REFERENCE_BATCH`](super::REFERENCE_BATCH) issues at once, each by its repository and
1244 /// number — the one read `task status complete` makes of a source, and nothing else of
1245 /// the board: no `items`, no search and no project read.
1246 ///
1247 /// **Its own fragment, lighter than `board_issue!`, and that is what makes fifty
1248 /// references one point.** It selects no labels, and of each board membership only the
1249 /// `Status` value, by name, with `fieldValueByName` — so each alias asks GitHub for one
1250 /// connection, its page of memberships, where the whole fragment asks for five. What a
1251 /// completion writes is a card and, at most, the issue's state, so the labels and the other
1252 /// field values it leaves out are never what it needs; an item read this way is held for
1253 /// that write alone and never answers another read. An issue whose entry for this board is
1254 /// past its first page of memberships is walked with [`ISSUE_BOARD_ITEMS`], exactly as
1255 /// every other read of an issue recovers one.
1256 ///
1257 /// Fixed-size, as [`ISSUE_DETAILS`] is, so there is one document to price: a batch of
1258 /// fewer references binds the slots it has none for to its last one.
1259 pub const ISSUES_BY_REFERENCE: &str = concat!(
1260 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!"#,
1261 relationship_events_variable!(),
1262 r#"){"#,
1263 reference_alias!(0),
1264 reference_alias!(1),
1265 reference_alias!(2),
1266 reference_alias!(3),
1267 reference_alias!(4),
1268 reference_alias!(5),
1269 reference_alias!(6),
1270 reference_alias!(7),
1271 reference_alias!(8),
1272 reference_alias!(9),
1273 reference_alias!(10),
1274 reference_alias!(11),
1275 reference_alias!(12),
1276 reference_alias!(13),
1277 reference_alias!(14),
1278 reference_alias!(15),
1279 reference_alias!(16),
1280 reference_alias!(17),
1281 reference_alias!(18),
1282 reference_alias!(19),
1283 reference_alias!(20),
1284 reference_alias!(21),
1285 reference_alias!(22),
1286 reference_alias!(23),
1287 reference_alias!(24),
1288 reference_alias!(25),
1289 reference_alias!(26),
1290 reference_alias!(27),
1291 reference_alias!(28),
1292 reference_alias!(29),
1293 reference_alias!(30),
1294 reference_alias!(31),
1295 reference_alias!(32),
1296 reference_alias!(33),
1297 reference_alias!(34),
1298 reference_alias!(35),
1299 reference_alias!(36),
1300 reference_alias!(37),
1301 reference_alias!(38),
1302 reference_alias!(39),
1303 reference_alias!(40),
1304 reference_alias!(41),
1305 reference_alias!(42),
1306 reference_alias!(43),
1307 reference_alias!(44),
1308 reference_alias!(45),
1309 reference_alias!(46),
1310 reference_alias!(47),
1311 reference_alias!(48),
1312 reference_alias!(49),
1313 "\n }",
1314 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}
1315 projectItems(first:$boardItems){nodes{id updatedAt project{id number}
1316 status:fieldValueByName(name:$statusField){... on ProjectV2ItemFieldSingleSelectValue{name field{
1317 ... on ProjectV2SingleSelectField{id name options{id name}}
1318 }}}
1319 }pageInfo{hasNextPage endCursor}}}"#
1320 );
1321
1322 /// The board's own id and field definitions, and not one of its items.
1323 ///
1324 /// What a write needs of the board when the item it writes does not say: the id a field
1325 /// write and `addProjectV2ItemById` address, and the definitions of the `Status` and
1326 /// origin fields. It selects no `items`, so what it costs is the board's field list
1327 /// however many items the board holds — and it decides nothing about which items those
1328 /// are, which is the question a read of one item by its own id answers instead.
1329 ///
1330 /// The root is aliased `boardFields` rather than `owner`, so nothing counting the
1331 /// board's item reads by their root counts this one among them.
1332 pub const BOARD_FIELDS: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!){
1333 boardFields:repositoryOwner(login:$owner){
1334 ... on ProjectV2Owner{projectV2(number:$number){id
1335 fields(first:$nestedFirst){nodes{
1336 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1337 ... on ProjectV2Field{__typename id name dataType}
1338 }pageInfo{hasNextPage}}
1339 }}
1340 }
1341 }"#;
1342
1343 /// One board draft by its own node id, with the board item it sits in.
1344 ///
1345 /// A draft is not an issue, so [`ISSUE`] reaches it and reads nothing of it; this is the
1346 /// second read that answers it. `DraftIssue.projectV2Items` names the board item a draft
1347 /// is — GitHub links a draft to one item — with the same [`board_item_values!`] the
1348 /// issue fragment reads, so a draft reached by id resolves through the same resolver a
1349 /// board listing hands it to, and nothing has to list the board to find one.
1350 pub const DRAFT: &str = concat!(
1351 r#"query($id:ID!,$nestedFirst:Int!,$boardItems:Int!){
1352 node(id:$id){__typename ... on DraftIssue{id title body createdAt updatedAt
1353 projectV2Items(first:$boardItems){nodes{id project{id number}
1354 "#,
1355 board_item_values!(),
1356 r#"}pageInfo{hasNextPage endCursor}}}}
1357 }"#
1358 );
1359
1360 /// One issue's board memberships alone, walked past the page a read of it carried.
1361 ///
1362 /// The recovery read behind [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource):
1363 /// every document above carries a *page* of `Issue.projectItems`, and an issue on more
1364 /// boards than that page holds may have this board's entry past its end. This asks that
1365 /// one issue for its memberships and nothing else — the caller already holds the issue —
1366 /// so an answer of "this board does not hold it" is only ever given about a connection
1367 /// read to exhaustion.
1368 ///
1369 /// It selects the board item's id, its project number and the same
1370 /// [`board_item_values!`] the fragment does, because what it produces is handed to the
1371 /// very same resolver: an issue recovered this way reports the same title, the same
1372 /// status, the same labels and the same qualified id as one whose entry was on the
1373 /// page.
1374 ///
1375 /// `$first` rather than `$boardItems`: this document reads one issue, so nothing
1376 /// multiplies through it and the membership connection can be walked at
1377 /// [`MAX_PAGE_SIZE`](super::MAX_PAGE_SIZE) — which is what keeps the recovery to one
1378 /// further request for any issue a person really keeps.
1379 pub const ISSUE_BOARD_ITEMS: &str = concat!(
1380 r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!){
1381 node(id:$id){
1382 ... on Issue{projectItems(first:$first,after:$after){
1383 nodes{id updatedAt project{id number}
1384 "#,
1385 board_item_values!(),
1386 r#"}
1387 pageInfo{hasNextPage endCursor}}}
1388 }
1389 }"#
1390 );
1391 /// Whether the board's Project is public — half of what decides whether a write here can
1392 /// be read by anybody. Needs the `read:project` scope.
1393 pub const PROJECT_VISIBILITY: &str = r#"query($owner:String!,$number:Int!){visibility:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){public}}}}"#;
1394 /// Resolves the configured repository's node id, which creating an issue requires.
1395 pub const REPOSITORY: &str = r#"query($owner:String!,$name:String!){repository(owner:$owner,name:$name){id nameWithOwner}}"#;
1396 /// What creating an issue needs and has not read yet: the board's own id and field
1397 /// definitions, as [`BOARD_FIELDS`] reads them, and the node id of the repository the
1398 /// issue is created in, as [`REPOSITORY`] reads it — in one request.
1399 ///
1400 /// Sent at the point a create knows which repository it is for, when neither half is
1401 /// already known to this process; a create needing only one of them sends that one's own
1402 /// document. Neither half is kept past the process: a field's option ids are re-minted by
1403 /// `sources fields --apply`, so a copy of them held between runs would write the wrong
1404 /// status.
1405 pub const CREATION_CONTEXT: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!,$repositoryOwner:String!,$repositoryName:String!){
1406 boardFields:repositoryOwner(login:$owner){
1407 ... on ProjectV2Owner{projectV2(number:$number){id
1408 fields(first:$nestedFirst){nodes{
1409 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1410 ... on ProjectV2Field{__typename id name dataType}
1411 }pageInfo{hasNextPage}}
1412 }}
1413 }
1414 repository(owner:$repositoryOwner,name:$repositoryName){id nameWithOwner}
1415 }"#;
1416 /// Reads both dependency directions for one issue, with each far end's own kind — and
1417 /// the issue's own body, which is where an edge to another source is recorded, so that
1418 /// half of a dependency read needs no second read of the issue or of the board.
1419 pub const ISSUE_DEPENDENCIES: &str = concat!(
1420 r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
1421 ... on Issue{body
1422 blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1423 blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1424 }}}"#,
1425 related_issue!()
1426 );
1427 /// What one item depends on: [`ISSUE_DEPENDENCIES`] narrowed to `blockedBy`, so a
1428 /// dependency read asks GitHub for the one direction it answers. The issue's `body` rides
1429 /// along because its metadata slot is where an edge to another source is recorded — and a
1430 /// board draft's `body` too, with the one board item GitHub links a draft to and, aliased
1431 /// `board`, this board's own id, so a draft's recorded edges are read off this same answer
1432 /// and only when that item is this board's: no draft read and no board read follow it.
1433 ///
1434 /// The draft's board item is asked for under a root of its own, aliased `draft`, rather
1435 /// than beside its `body` under `node`. GitHub counts the type-conditional fragments of
1436 /// one selection as the largest of them — `blockedBy`'s 100 alone, where this workspace's
1437 /// calculation adds the draft's 3 — while separate roots are summed by both, so this is
1438 /// the spelling whose node count GitHub and the calculation agree on. Both price it at
1439 /// one point; the credentialed lane's reconciliation is what holds the two to that.
1440 pub const ISSUE_DEPENDS_ON: &str = concat!(
1441 r#"query($id:ID!,$first:Int!,$after:String,$owner:String!,$number:Int!,$boardItems:Int!){
1442 node(id:$id){__typename
1443 ... on Issue{body
1444 blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1445 }
1446 ... on DraftIssue{id body}}
1447 draft:node(id:$id){... on DraftIssue{projectV2Items(first:$boardItems){nodes{project{id number}}pageInfo{hasNextPage}}}}
1448 board:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){id}}}}"#,
1449 related_issue!()
1450 );
1451 /// What depends on one issue: [`ISSUE_DEPENDENCIES`] with its `blocking` alone. No `body`:
1452 /// the reverse of a recorded edge is derived from the far end, never written down here.
1453 pub const ISSUE_DEPENDED_ON_BY: &str = concat!(
1454 r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
1455 ... on Issue{
1456 blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1457 }}}"#,
1458 related_issue!()
1459 );
1460 /// Creates one issue. Outside a copy it is created on no board, and [`ADD_TO_BOARD`] files
1461 /// it; inside one, its input names the board in `projectV2Ids` and its project in
1462 /// `parentIssueId`, so this one request files it under both — see
1463 /// [`GitHubProjectsSource::create_filed_issue`](super::GitHubProjectsSource).
1464 pub const CREATE_ISSUE: &str =
1465 r#"mutation($input:CreateIssueInput!){createIssue(input:$input){issue{id number url}}}"#;
1466 /// Puts an existing issue on the configured board.
1467 pub const ADD_TO_BOARD: &str = r#"mutation($input:AddProjectV2ItemByIdInput!){addProjectV2ItemById(input:$input){item{id}}}"#;
1468 /// Updates an issue's visible fields and its open or closed state in one call.
1469 pub const UPDATE_ISSUE: &str =
1470 r#"mutation($input:UpdateIssueInput!){updateIssue(input:$input){issue{id}}}"#;
1471 /// Updates an existing draft's user-visible fields.
1472 pub const UPDATE_DRAFT: &str = r#"mutation($input:UpdateProjectV2DraftIssueInput!){updateProjectV2DraftIssue(input:$input){draftIssue{id}}}"#;
1473 /// Updates a text or single-select value on one project item.
1474 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}}}}}}}}"#;
1475 /// Writes up to six board fields and clears up to three in one ordered mutation: every
1476 /// aliased field is included by its own boolean, as [`FIELD_WRITE_SLOTS`](super::FIELD_WRITE_SLOTS)
1477 /// and [`FIELD_CLEAR_SLOTS`](super::FIELD_CLEAR_SLOTS) name them, writes before clears.
1478 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}}}"#;
1479 /// Clears one project item's value of one field, which is what a `none` priority is.
1480 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}}}}}}}}"#;
1481 /// Creates one field: a single-select one with its options, or a text one for a projected
1482 /// metadata value. Only the guarded field setup may use this document, and only for a
1483 /// field the board lacks.
1484 pub const CREATE_FIELD: &str = r#"mutation($input:CreateProjectV2FieldInput!){createProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id name options{id name color description}}}}}"#;
1485 /// Replaces a single-select field's options. Only the guarded field setup — the
1486 /// `status-options` and `fields` operations — may use this document, because GitHub
1487 /// treats the input as the complete option list.
1488 pub const STATUS_OPTIONS_UPDATE: &str = r#"mutation($input:UpdateProjectV2FieldInput!){updateProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id options{id name color description}}}}}"#;
1489 /// A fresh snapshot of the Status field and every board item's assignment.
1490 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}}}}}}"#;
1491 /// Files one issue under another as a sub-issue, which is what project membership is.
1492 pub const ADD_SUB_ISSUE: &str =
1493 r#"mutation($input:AddSubIssueInput!){addSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1494 /// Takes one issue back out of its parent.
1495 pub const REMOVE_SUB_ISSUE: &str = r#"mutation($input:RemoveSubIssueInput!){removeSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1496 /// Adds GitHub's native issue blocked-by relationship.
1497 pub const ADD_BLOCKED_BY: &str = r#"mutation($input:AddBlockedByInput!){addBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1498 /// One origin of [`ORIGIN_LOOKUPS`]: [`ORIGIN_LOOKUP`]'s two connections for the origin the
1499 /// `$filter<n>` and `$search<n>` of slot `<n>` name, sent only while `$ask<n>` holds.
1500 macro_rules! origin_lookup_alias {
1501 ($n:literal) => {
1502 concat!(
1503 "\n o",
1504 stringify!($n),
1505 ":repositoryOwner(login:$owner) @include(if:$ask",
1506 stringify!($n),
1507 "){... on ProjectV2Owner{projectV2(number:$number){items(first:$lookupFirst,query:$filter",
1508 stringify!($n),
1509 "){nodes{id updatedAt ",
1510 board_item_values!(),
1511 board_item_content!(),
1512 "} pageInfo{hasNextPage endCursor}}}}}\n s",
1513 stringify!($n),
1514 ":search(query:$search",
1515 stringify!($n),
1516 ",type:$type,first:$lookupFirst) @include(if:$ask",
1517 stringify!($n),
1518 "){pageInfo{hasNextPage endCursor} nodes{__typename ...BoardIssue}}"
1519 )
1520 };
1521 }
1522
1523 /// [`ORIGIN_LOOKUPS`], with `$extra_variables` declared after its own and `$extra_fields`
1524 /// selected beside them: one spelling of the batched lookup for the documents that send it.
1525 macro_rules! origin_lookups {
1526 ($extra_variables:literal, $extra_fields:literal) => {
1527 concat!(
1528 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!"#,
1529 relationship_events_variable!(),
1530 $extra_variables,
1531 r#"){
1532 boardFields:repositoryOwner(login:$owner){
1533 ... on ProjectV2Owner{projectV2(number:$number){id
1534 fields(first:$nestedFirst){nodes{
1535 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1536 ... on ProjectV2Field{__typename id name dataType}
1537 }pageInfo{hasNextPage}}
1538 }}
1539 }
1540 repository(owner:$repositoryOwner,name:$repositoryName){id databaseId nameWithOwner}"#,
1541 $extra_fields,
1542 origin_lookup_alias!(0),
1543 origin_lookup_alias!(1),
1544 origin_lookup_alias!(2),
1545 origin_lookup_alias!(3),
1546 origin_lookup_alias!(4),
1547 origin_lookup_alias!(5),
1548 origin_lookup_alias!(6),
1549 origin_lookup_alias!(7),
1550 origin_lookup_alias!(8),
1551 origin_lookup_alias!(9),
1552 "\n }",
1553 board_issue!()
1554 )
1555 };
1556 }
1557
1558 /// The first page of [`ORIGIN_LOOKUP`] for up to [`ORIGIN_BATCH`](super::ORIGIN_BATCH)
1559 /// origins a copy is about to look for, and everything creating an item then reads —
1560 /// the board's own id and field definitions, as [`CREATION_CONTEXT`] reads them, and the node
1561 /// id and the numeric id of the repository an issue is created in — in one request.
1562 ///
1563 /// What a copy reads before it writes anything, so that finding none of its records on the
1564 /// board and creating every one costs this read and the writes. An origin whose page says
1565 /// there is more is left to its own [`ORIGIN_LOOKUP`]. The repository's numeric id is what an
1566 /// image upload names, so a copy carrying assets needs no `GET /repos/{owner}/{repo}` either.
1567 pub const ORIGIN_LOOKUPS: &str = origin_lookups!("", "");
1568
1569 /// [`ORIGIN_LOOKUPS`], with a page of the members of the project `$project` beside it, each
1570 /// trimmed to what classifying it reads — its id, its parent, its body and its repository,
1571 /// and no connection under it.
1572 ///
1573 /// What a document copy sends where the public boundary screens its write: the document is
1574 /// held to who may read its project, which is its members' classification as much as the
1575 /// project's own, and this request carries both the project — among the origins it asks —
1576 /// and its members. GitHub's own dry run prices it at the one point [`ORIGIN_LOOKUPS`]
1577 /// costs alone (`docs/measurements/board-copy-gaps-2026-10-24/coalesced-members-dry-run`).
1578 pub const ORIGIN_LOOKUPS_WITH_MEMBERS: &str = origin_lookups!(
1579 ",$project:ID!,$memberFirst:Int!",
1580 "\n members:node(id:$project){... on Issue{subIssues(first:$memberFirst){pageInfo{hasNextPage endCursor} nodes{id body parent{id} repository{nameWithOwner}}}}}"
1581 );
1582
1583 /// One slot `<n>` of a copy's aliased writes: `$<prefix><n>` is its input and `$send<n>`
1584 /// whether it is sent.
1585 macro_rules! write_slot {
1586 ($prefix:literal, $field:literal, $selection:literal, $n:literal) => {
1587 concat!(
1588 " ",
1589 $prefix,
1590 stringify!($n),
1591 ":",
1592 $field,
1593 "(input:$",
1594 $prefix,
1595 stringify!($n),
1596 ") @include(if:$send",
1597 stringify!($n),
1598 "){",
1599 $selection,
1600 "}"
1601 )
1602 };
1603 }
1604
1605 /// The variables of a copy's aliased writes: eleven inputs of `$type` named `$<prefix><n>`,
1606 /// and eleven `$send<n>`.
1607 macro_rules! write_variables {
1608 ($prefix:literal, $type:literal) => {
1609 concat!(
1610 "mutation(",
1611 "$",
1612 $prefix,
1613 "0:",
1614 $type,
1615 "!,$",
1616 $prefix,
1617 "1:",
1618 $type,
1619 "!,$",
1620 $prefix,
1621 "2:",
1622 $type,
1623 "!,$",
1624 $prefix,
1625 "3:",
1626 $type,
1627 "!,$",
1628 $prefix,
1629 "4:",
1630 $type,
1631 "!,$",
1632 $prefix,
1633 "5:",
1634 $type,
1635 "!,$",
1636 $prefix,
1637 "6:",
1638 $type,
1639 "!,$",
1640 $prefix,
1641 "7:",
1642 $type,
1643 "!,$",
1644 $prefix,
1645 "8:",
1646 $type,
1647 "!,$",
1648 $prefix,
1649 "9:",
1650 $type,
1651 "!,$",
1652 $prefix,
1653 "10:",
1654 $type,
1655 "!,",
1656 "$send0:Boolean!,$send1:Boolean!,$send2:Boolean!,$send3:Boolean!,$send4:Boolean!,",
1657 "$send5:Boolean!,$send6:Boolean!,$send7:Boolean!,$send8:Boolean!,$send9:Boolean!,",
1658 "$send10:Boolean!){"
1659 )
1660 };
1661 }
1662
1663 /// A copy's aliased writes of one kind: [`MUTATION_BATCH`](super::MUTATION_BATCH) slots of
1664 /// `$field`, each sent only while its `$send<n>` holds.
1665 macro_rules! write_batch {
1666 ($prefix:literal, $type:literal, $field:literal, $selection:literal) => {
1667 concat!(
1668 write_variables!($prefix, $type),
1669 write_slot!($prefix, $field, $selection, 0),
1670 write_slot!($prefix, $field, $selection, 1),
1671 write_slot!($prefix, $field, $selection, 2),
1672 write_slot!($prefix, $field, $selection, 3),
1673 write_slot!($prefix, $field, $selection, 4),
1674 write_slot!($prefix, $field, $selection, 5),
1675 write_slot!($prefix, $field, $selection, 6),
1676 write_slot!($prefix, $field, $selection, 7),
1677 write_slot!($prefix, $field, $selection, 8),
1678 write_slot!($prefix, $field, $selection, 9),
1679 write_slot!($prefix, $field, $selection, 10),
1680 "}"
1681 )
1682 };
1683 }
1684
1685 /// Creates up to [`MUTATION_BATCH`](super::MUTATION_BATCH) issues in one request, each filed on
1686 /// the board and under its project by the same `createIssue` — its `projectV2Ids` and
1687 /// `parentIssueId` — as [`CREATE_ISSUE`] creates one.
1688 pub const CREATE_ISSUES: &str = write_batch!(
1689 "create",
1690 "CreateIssueInput",
1691 "createIssue",
1692 "issue{id number url}"
1693 );
1694
1695 /// One issue of [`CREATED_BOARD_ITEMS`]: the board items the issue `$id<n>` names is on, with
1696 /// the `Status` value of each, read only while `$read<n>` holds.
1697 macro_rules! created_item_alias {
1698 ($n:literal) => {
1699 concat!(
1700 "\n i",
1701 stringify!($n),
1702 ":node(id:$id",
1703 stringify!($n),
1704 ") @include(if:$read",
1705 stringify!($n),
1706 "){... on Issue{id projectItems(first:$boardItems){nodes{id project{id} status:fieldValueByName(name:$statusName){... on ProjectV2ItemFieldSingleSelectValue{name}}}}}}"
1707 )
1708 };
1709 }
1710
1711 /// The board item of each issue a copy created, up to
1712 /// [`COPY_DETAIL_BATCH`](super::COPY_DETAIL_BATCH) at a time, with its `Status` as the board
1713 /// set it: what `createIssue`'s `projectV2Ids` files the issue as, which GitHub adds after
1714 /// answering the create, so its id comes from a read made once every create has been sent.
1715 pub const CREATED_BOARD_ITEMS: &str = concat!(
1716 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!){"#,
1717 created_item_alias!(0),
1718 created_item_alias!(1),
1719 created_item_alias!(2),
1720 created_item_alias!(3),
1721 created_item_alias!(4),
1722 created_item_alias!(5),
1723 created_item_alias!(6),
1724 created_item_alias!(7),
1725 created_item_alias!(8),
1726 created_item_alias!(9),
1727 created_item_alias!(10),
1728 created_item_alias!(11),
1729 created_item_alias!(12),
1730 created_item_alias!(13),
1731 created_item_alias!(14),
1732 created_item_alias!(15),
1733 created_item_alias!(16),
1734 created_item_alias!(17),
1735 created_item_alias!(18),
1736 created_item_alias!(19),
1737 created_item_alias!(20),
1738 "\n }"
1739 );
1740
1741 /// The variables and the aliases of [`WRITE_BATCH`](super::WRITE_BATCH) slots: `$<prefix><n>`
1742 /// is a slot's input and `$<send><n>` whether it is sent, for the slot numbers given.
1743 macro_rules! wide_variables {
1744 ($prefix:literal, $type:literal, $send:literal; $($n:literal)+) => {
1745 concat!($("$", $prefix, stringify!($n), ":", $type, "!,$", $send, stringify!($n), ":Boolean!,",)+)
1746 };
1747 }
1748 macro_rules! wide_slots {
1749 ($prefix:literal, $field:literal, $selection:literal, $send:literal; $($n:literal)+) => {
1750 concat!($(
1751 " ", $prefix, stringify!($n), ":", $field, "(input:$", $prefix, stringify!($n),
1752 ") @include(if:$", $send, stringify!($n), "){", $selection, "}",
1753 )+)
1754 };
1755 }
1756
1757 /// Up to [`WRITE_BATCH`](super::WRITE_BATCH) board field values a copy writes on the items it
1758 /// created, each on its own item, in one ordered request.
1759 pub const WRITE_FIELD_VALUES: &str = concat!(
1760 "mutation(",
1761 wide_variables!("field", "UpdateProjectV2ItemFieldValueInput", "send"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1762 "){",
1763 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),
1764 "}"
1765 );
1766
1767 /// A copy's board field values and its blocked-by edges together, up to
1768 /// [`WRITE_BATCH`](super::WRITE_BATCH) of the two kinds between them, in one ordered request:
1769 /// every field value its `$sendField<n>` sends, then every edge its `$sendBlock<n>` sends.
1770 /// What a copy sends when one request of its writes holds both kinds; one holding a single
1771 /// kind is [`WRITE_FIELD_VALUES`] or [`ADD_BLOCKED_BYS`].
1772 pub const WRITE_FIELDS_AND_EDGES: &str = concat!(
1773 "mutation(",
1774 wide_variables!("field", "UpdateProjectV2ItemFieldValueInput", "sendField"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1775 wide_variables!("block", "AddBlockedByInput", "sendBlock"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1776 "){",
1777 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),
1778 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),
1779 "}"
1780 );
1781
1782 /// Closes up to [`MUTATION_BATCH`](super::MUTATION_BATCH) issues a copy created at a closing
1783 /// status, each with its own `stateInput` and nothing else, once its option is selected.
1784 pub const CLOSE_ISSUES: &str =
1785 write_batch!("close", "UpdateIssueInput", "updateIssue", "issue{id}");
1786
1787 /// Updates up to [`MUTATION_BATCH`](super::MUTATION_BATCH) existing issues a copy rewrites,
1788 /// each with the members its own [`UPDATE_ISSUE`] would have sent, in one request.
1789 pub const UPDATE_ISSUES: &str =
1790 write_batch!("update", "UpdateIssueInput", "updateIssue", "issue{id}");
1791
1792 /// Up to [`WRITE_BATCH`](super::WRITE_BATCH) native blocked-by relationships a copy adds to the
1793 /// issues it created, in one request.
1794 pub const ADD_BLOCKED_BYS: &str = concat!(
1795 "mutation(",
1796 wide_variables!("block", "AddBlockedByInput", "send"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1797 "){",
1798 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),
1799 "}"
1800 );
1801
1802 /// Removes one native issue blocked-by relationship.
1803 pub const REMOVE_BLOCKED_BY: &str = r#"mutation($input:RemoveBlockedByInput!){removeBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1804 /// Deletes one issue, which takes its board item with it.
1805 ///
1806 /// The engine sends this in one situation only: undoing a copy that could not finish,
1807 /// over the items that same copy created. Deleting the issue removes the board item
1808 /// too, so there is no second `deleteProjectV2Item` to keep in step with it.
1809 pub const DELETE_ISSUE: &str =
1810 r#"mutation($input:DeleteIssueInput!){deleteIssue(input:$input){repository{id}}}"#;
1811 /// Deletes up to [`DELETE_BATCH`](super::DELETE_BATCH) issues in one request, each as
1812 /// [`DELETE_ISSUE`] deletes one: what a write that created several issues and was refused
1813 /// part way sends to take them back.
1814 pub const DELETE_ISSUES: &str = concat!(
1815 "mutation(",
1816 wide_variables!("delete", "DeleteIssueInput", "send"; 0 1 2 3 4),
1817 "){",
1818 wide_slots!("delete", "deleteIssue", "repository{id}", "send"; 0 1 2 3 4),
1819 "}"
1820 );
1821
1822 /// Everything this source reads about one issue comment, wherever it reaches one.
1823 ///
1824 /// A macro for the reason [`board_issue!`] is one: a comment listed, a comment just added
1825 /// and a comment just edited are handed to one mapper, so they are selected by one
1826 /// spelling. `author` is `Actor`, which GitHub answers `null` for an account that no
1827 /// longer exists, and `login` is the one member every kind of actor carries.
1828 macro_rules! issue_comment {
1829 () => {
1830 "id author{login} createdAt updatedAt body url"
1831 };
1832 }
1833
1834 /// One task's comments: a page of its issue's own `comments` connection.
1835 ///
1836 /// **No `orderBy`, and that is what makes the page oldest first.** GitHub's only
1837 /// `IssueCommentOrder` field is `UPDATED_AT`, which would move a comment to the end of the
1838 /// list every time somebody edited it; left unordered the connection answers in the order
1839 /// the comments were written, which is the order GitHub documents for the same collection
1840 /// over REST — ascending id. Nothing multiplies through it, so `$first` is the whole of its
1841 /// node count and the caller's own page size is pushed straight down.
1842 pub const ISSUE_COMMENTS: &str = concat!(
1843 r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename ... on Issue{comments(first:$first,after:$after){nodes{"#,
1844 issue_comment!(),
1845 r#"}pageInfo{hasNextPage endCursor}}}}}"#
1846 );
1847 /// One issue by its own node id, with a page of its comments: what `task show` and a
1848 /// comment listing read, in one request.
1849 ///
1850 /// [`ISSUE`] and [`ISSUE_COMMENTS`] in one document, rather than one then the other. The
1851 /// comments are selected here and **not** on the shared `board_issue!` fragment, which
1852 /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] nest under a page of a hundred issues: a comment
1853 /// connection there would multiply through both of those documents' price, and neither
1854 /// needs one.
1855 pub const ISSUE_DETAIL: &str = concat!(
1856 r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1857 relationship_events_variable!(),
1858 r#"){
1859 node(id:$id){__typename ...BoardIssue ... on Issue{comments(first:$first,after:$after){nodes{"#,
1860 issue_comment!(),
1861 r#"}pageInfo{hasNextPage endCursor}}}}
1862 }"#,
1863 board_issue!()
1864 );
1865
1866 /// One alias of [`ISSUE_DETAILS`]: the item a batch's `$id<n>` names, with the first
1867 /// page of its comments when `$comments` asks for them.
1868 macro_rules! issue_details_alias {
1869 ($n:literal) => {
1870 concat!(
1871 "\n i",
1872 stringify!($n),
1873 ":node(id:$id",
1874 stringify!($n),
1875 "){__typename ...BoardIssue ... on Issue{comments(first:$first) @include(if:$comments){nodes{",
1876 issue_comment!(),
1877 "}pageInfo{hasNextPage endCursor}}}}"
1878 )
1879 };
1880 }
1881
1882 /// [`ISSUE_DETAIL`] for [`DETAIL_BATCH`](super::DETAIL_BATCH) items at once, each by its
1883 /// own node id, as one fixed-size document of aliased `node(id:)` fields.
1884 ///
1885 /// **Aliased `node(id:)` rather than `nodes(ids:)`, and that is what keeps its price
1886 /// honest.** The `github-graphql-node-count` model this workspace prices with treats a
1887 /// field that supplies neither `first` nor `last` as free, and `nodes(ids:)` supplies
1888 /// neither — so every connection under it would be priced at nothing and the pin in
1889 /// `tests/point_cost.rs` would understate what GitHub charges. Each alias here is the
1890 /// one-item read the model already prices, so the batch costs what its aliases cost.
1891 ///
1892 /// **Fixed-size, so there is one document to price.** A batch of fewer items binds the
1893 /// slots it has no item for to the last item it does, and reads that item again; the
1894 /// price is the document's, whatever its variables, so a short batch costs what a full
1895 /// one does and nothing more.
1896 pub const ISSUE_DETAILS: &str = concat!(
1897 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!"#,
1898 relationship_events_variable!(),
1899 r#"){"#,
1900 issue_details_alias!(0),
1901 issue_details_alias!(1),
1902 issue_details_alias!(2),
1903 issue_details_alias!(3),
1904 issue_details_alias!(4),
1905 issue_details_alias!(5),
1906 issue_details_alias!(6),
1907 issue_details_alias!(7),
1908 issue_details_alias!(8),
1909 issue_details_alias!(9),
1910 issue_details_alias!(10),
1911 issue_details_alias!(11),
1912 issue_details_alias!(12),
1913 issue_details_alias!(13),
1914 issue_details_alias!(14),
1915 issue_details_alias!(15),
1916 issue_details_alias!(16),
1917 issue_details_alias!(17),
1918 issue_details_alias!(18),
1919 issue_details_alias!(19),
1920 issue_details_alias!(20),
1921 issue_details_alias!(21),
1922 issue_details_alias!(22),
1923 issue_details_alias!(23),
1924 "\n }",
1925 board_issue!()
1926 );
1927
1928 /// One alias of [`ISSUE_COPY_DETAILS`]: the item a batch's `$id<n>` names, with the first
1929 /// page of its comments and the issues it is blocked by.
1930 macro_rules! issue_copy_details_alias {
1931 ($n:literal) => {
1932 concat!(
1933 "\n i",
1934 stringify!($n),
1935 ":node(id:$id",
1936 stringify!($n),
1937 "){__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{",
1938 issue_comment!(),
1939 "}pageInfo{hasNextPage endCursor}} blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}}}"
1940 )
1941 };
1942 }
1943
1944 /// [`ISSUE_DETAILS`] as a copy reads the tasks it carries: each item with the first page of
1945 /// its comments and its `blockedBy` — the edges a copy reads of every task next, which
1946 /// [`ISSUE`] carries and [`ISSUE_DETAILS`] does not — [`COPY_DETAIL_BATCH`](super::COPY_DETAIL_BATCH)
1947 /// at a time, so a copy of a changed task costs this read and nothing else.
1948 ///
1949 /// A document of its own rather than a widening of [`ISSUE_DETAILS`], whose batch size is
1950 /// what `task show-many` is priced by and which reads no edge.
1951 ///
1952 /// `children`, read only while `$children` holds, is which issues the issue `$project` has
1953 /// as sub-issues — each one's id and title, the page `$nestedFirst` allows — so that a
1954 /// project copy, which reads its project and every task at once, knows from this same read
1955 /// which tasks the project holds at the destination, and its orphan check is answered by it
1956 /// rather than by a listing of the project.
1957 pub const ISSUE_COPY_DETAILS: &str = concat!(
1958 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!"#,
1959 relationship_events_variable!(),
1960 r#"){"#,
1961 issue_copy_details_alias!(0),
1962 issue_copy_details_alias!(1),
1963 issue_copy_details_alias!(2),
1964 issue_copy_details_alias!(3),
1965 issue_copy_details_alias!(4),
1966 issue_copy_details_alias!(5),
1967 issue_copy_details_alias!(6),
1968 issue_copy_details_alias!(7),
1969 issue_copy_details_alias!(8),
1970 issue_copy_details_alias!(9),
1971 issue_copy_details_alias!(10),
1972 issue_copy_details_alias!(11),
1973 issue_copy_details_alias!(12),
1974 issue_copy_details_alias!(13),
1975 issue_copy_details_alias!(14),
1976 issue_copy_details_alias!(15),
1977 issue_copy_details_alias!(16),
1978 issue_copy_details_alias!(17),
1979 issue_copy_details_alias!(18),
1980 issue_copy_details_alias!(19),
1981 issue_copy_details_alias!(20),
1982 "\n children:node(id:$project) @include(if:$children){... on Issue{id subIssues(first:$nestedFirst){nodes{id title} pageInfo{hasNextPage}}}}",
1983 "\n }",
1984 board_issue!(),
1985 related_issue!()
1986 );
1987
1988 /// Which issue one comment is on, read before that comment is edited or removed.
1989 ///
1990 /// GitHub's comment mutations take the comment's id and nothing else, so without this a
1991 /// comment id given against the wrong task would change a comment on another issue.
1992 pub const COMMENT_ISSUE: &str =
1993 r#"query($id:ID!){node(id:$id){__typename ... on IssueComment{id body issue{id}}}}"#;
1994 /// Adds one comment to an issue, signed as the account the token belongs to.
1995 pub const ADD_COMMENT: &str = concat!(
1996 r#"mutation($input:AddCommentInput!){addComment(input:$input){subject{id} commentEdge{node{"#,
1997 issue_comment!(),
1998 r#"}}}}"#
1999 );
2000 /// Replaces the body of one issue comment.
2001 pub const UPDATE_COMMENT: &str = concat!(
2002 r#"mutation($input:UpdateIssueCommentInput!){updateIssueComment(input:$input){issueComment{"#,
2003 issue_comment!(),
2004 r#"}}}"#
2005 );
2006 /// Removes one issue comment. Its payload carries nothing about the comment it removed.
2007 pub const DELETE_COMMENT: &str = r#"mutation($input:DeleteIssueCommentInput!){deleteIssueComment(input:$input){clientMutationId}}"#;
2008
2009 /// What a mutation of a write the store's cache expects selects of the issue it wrote:
2010 /// everything [`ISSUE_DETAIL`] reads of it, so its response shows the task whole — its
2011 /// change token's every part and the first page of its comments.
2012 ///
2013 /// Sent **only** while that write runs, in place of the document named beside each
2014 /// below, so every other write's documents, prices and request counts are what they
2015 /// were. GitHub's token waits for a task to settle (`change_token::SETTLED`), and a
2016 /// mutation's response is stamped seconds after the change it made, so against GitHub
2017 /// itself a response rarely determines a version; the cache is then left unserveable
2018 /// for the task, and its next read refreshes it.
2019 macro_rules! whole_issue {
2020 () => {
2021 concat!(
2022 r#"__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{"#,
2023 issue_comment!(),
2024 r#"}pageInfo{hasNextPage endCursor}}}"#
2025 )
2026 };
2027 }
2028
2029 /// The variables every document of `whole_issue!` declares beside its own.
2030 macro_rules! whole_variables {
2031 () => {
2032 concat!(
2033 r#",$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
2034 relationship_events_variable!()
2035 )
2036 };
2037 }
2038
2039 /// [`ADD_COMMENT`], selecting the whole issue commented on.
2040 pub const ADD_COMMENT_WHOLE: &str = concat!(
2041 r#"mutation($input:AddCommentInput!"#,
2042 whole_variables!(),
2043 r#"){addComment(input:$input){subject{id "#,
2044 whole_issue!(),
2045 r#"} commentEdge{node{"#,
2046 issue_comment!(),
2047 r#"}}}}"#,
2048 board_issue!()
2049 );
2050 /// [`UPDATE_COMMENT`], selecting the whole issue the comment is on.
2051 pub const UPDATE_COMMENT_WHOLE: &str = concat!(
2052 r#"mutation($input:UpdateIssueCommentInput!"#,
2053 whole_variables!(),
2054 r#"){updateIssueComment(input:$input){issueComment{"#,
2055 issue_comment!(),
2056 r#" issue{"#,
2057 whole_issue!(),
2058 r#"}}}}"#,
2059 board_issue!()
2060 );
2061 /// [`UPDATE_ISSUE`], selecting the whole issue updated.
2062 pub const UPDATE_ISSUE_WHOLE: &str = concat!(
2063 r#"mutation($input:UpdateIssueInput!"#,
2064 whole_variables!(),
2065 r#"){updateIssue(input:$input){issue{id "#,
2066 whole_issue!(),
2067 r#"}}}"#,
2068 board_issue!()
2069 );
2070 /// [`UPDATE_FIELD`], selecting the whole issue behind the board item written.
2071 pub const UPDATE_FIELD_WHOLE: &str = concat!(
2072 r#"mutation($input:UpdateProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
2073 whole_variables!(),
2074 r#"){updateProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
2075 whole_issue!(),
2076 r#"}}}}"#,
2077 board_issue!()
2078 );
2079 /// [`CLEAR_FIELD`], selecting the whole issue behind the board item cleared.
2080 pub const CLEAR_FIELD_WHOLE: &str = concat!(
2081 r#"mutation($input:ClearProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
2082 whole_variables!(),
2083 r#"){clearProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
2084 whole_issue!(),
2085 r#"}}}}"#,
2086 board_issue!()
2087 );
2088
2089 /// The document sent in `document`'s place while a write the store's cache expects runs,
2090 /// when it has one.
2091 #[must_use]
2092 pub fn whole_twin(document: &str) -> Option<&'static str> {
2093 [
2094 (ADD_COMMENT, ADD_COMMENT_WHOLE),
2095 (UPDATE_COMMENT, UPDATE_COMMENT_WHOLE),
2096 (UPDATE_ISSUE, UPDATE_ISSUE_WHOLE),
2097 (UPDATE_FIELD, UPDATE_FIELD_WHOLE),
2098 (CLEAR_FIELD, CLEAR_FIELD_WHOLE),
2099 ]
2100 .into_iter()
2101 .find(|(plain, _)| *plain == document)
2102 .map(|(_, whole)| whole)
2103 }
2104
2105 /// Every document above, with what this source is doing when it sends one.
2106 ///
2107 /// One list rather than a `match` beside the constants: a rate-limit diagnostic has to
2108 /// name the call that was refused, and a `match` with a catch-all arm would answer a
2109 /// document added later with "talking to GitHub" and never say so.
2110 ///
2111 /// `documents_are_all_inventoried` reads this file back and fails naming any `pub
2112 /// const` here that this list omits, so the two cannot part — which is the same guard
2113 /// `CATEGORIES` carries, in the one shape available to a set of `&str` constants.
2114 pub const DOCUMENTS: [(&str, &str); 55] = [
2115 (SEARCH_ISSUES, "searching this board's issues"),
2116 (ISSUE, "reading one issue"),
2117 (
2118 DOCUMENT_IN_PROJECT,
2119 "reading one document with its project and the project's members",
2120 ),
2121 (
2122 ISSUE_BOARD_ITEMS,
2123 "reading one issue's board memberships past the page it came with",
2124 ),
2125 (SUB_ISSUES, "reading a project's tasks"),
2126 (BOARD, "reading the board"),
2127 (ORIGIN_LOOKUP, "looking up the items copied from one origin"),
2128 (CLOSED_ITEMS, "reading the board's closed items"),
2129 (
2130 ISSUES_BY_REFERENCE,
2131 "reading a batch of issues by their references",
2132 ),
2133 (BOARD_FIELDS, "reading the board's fields"),
2134 (DRAFT, "reading one draft"),
2135 (REPOSITORY, "reading the destination repository"),
2136 (
2137 PROJECT_VISIBILITY,
2138 "reading whether the board's project is public",
2139 ),
2140 (
2141 CREATION_CONTEXT,
2142 "reading the board's fields and the destination repository",
2143 ),
2144 (ISSUE_DEPENDENCIES, "reading an issue's dependencies"),
2145 (ISSUE_DEPENDS_ON, "reading what an item depends on"),
2146 (ISSUE_DEPENDED_ON_BY, "reading what depends on an issue"),
2147 (CREATE_ISSUE, "creating an issue"),
2148 (
2149 ORIGIN_LOOKUPS,
2150 "looking up the items copied from several origins, and what creating one reads",
2151 ),
2152 (
2153 ORIGIN_LOOKUPS_WITH_MEMBERS,
2154 "looking up the items copied from several origins, with a project's members",
2155 ),
2156 (CREATE_ISSUES, "creating several issues together"),
2157 (
2158 CREATED_BOARD_ITEMS,
2159 "reading the board items of the issues a copy created",
2160 ),
2161 (
2162 WRITE_FIELD_VALUES,
2163 "writing board fields of the items a copy created",
2164 ),
2165 (
2166 WRITE_FIELDS_AND_EDGES,
2167 "writing board fields and dependencies of the items a copy created",
2168 ),
2169 (CLOSE_ISSUES, "closing the issues a copy created"),
2170 (UPDATE_ISSUES, "updating several issues together"),
2171 (ADD_BLOCKED_BYS, "recording several dependencies together"),
2172 (ADD_TO_BOARD, "adding an issue to the board"),
2173 (UPDATE_ISSUE, "updating an issue"),
2174 (UPDATE_DRAFT, "updating a draft item"),
2175 (UPDATE_FIELD, "writing a board field"),
2176 (UPDATE_FIELDS, "writing board fields together"),
2177 (CLEAR_FIELD, "clearing a board field"),
2178 (
2179 CREATE_FIELD,
2180 "creating a board single-select field with its options",
2181 ),
2182 (
2183 STATUS_OPTIONS_SNAPSHOT,
2184 "snapshotting board Status options and assignments",
2185 ),
2186 (
2187 STATUS_OPTIONS_UPDATE,
2188 "safely replacing the board Status option list",
2189 ),
2190 (ADD_SUB_ISSUE, "filing an issue under its project"),
2191 (REMOVE_SUB_ISSUE, "taking an issue out of its project"),
2192 (ADD_BLOCKED_BY, "recording a dependency"),
2193 (REMOVE_BLOCKED_BY, "removing a dependency"),
2194 (DELETE_ISSUE, "deleting an issue"),
2195 (DELETE_ISSUES, "deleting several issues together"),
2196 (ISSUE_COMMENTS, "reading a task's comments"),
2197 (ISSUE_DETAIL, "reading one issue with its comments"),
2198 (
2199 ISSUE_DETAILS,
2200 "reading a batch of issues with their comments",
2201 ),
2202 (
2203 ISSUE_COPY_DETAILS,
2204 "reading a batch of issues with their comments and dependencies for a copy",
2205 ),
2206 (COMMENT_ISSUE, "reading which issue a comment is on"),
2207 (ADD_COMMENT, "adding a comment"),
2208 (UPDATE_COMMENT, "editing a comment"),
2209 (DELETE_COMMENT, "deleting a comment"),
2210 (
2211 ADD_COMMENT_WHOLE,
2212 "adding a comment, answered with the whole issue",
2213 ),
2214 (
2215 UPDATE_COMMENT_WHOLE,
2216 "editing a comment, answered with the whole issue",
2217 ),
2218 (
2219 UPDATE_ISSUE_WHOLE,
2220 "updating an issue, answered with it whole",
2221 ),
2222 (
2223 UPDATE_FIELD_WHOLE,
2224 "writing a board field, answered with the whole issue",
2225 ),
2226 (
2227 CLEAR_FIELD_WHOLE,
2228 "clearing a board field, answered with the whole issue",
2229 ),
2230 ];
2231}
2232
2233/// One aliased mutation field of [`graphql::UPDATE_FIELDS`]: the key its answer comes back
2234/// under, the variable carrying its input, and the boolean variable that includes it.
2235#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2236pub struct FieldSlot {
2237 /// The key of the answer's `data` this field's payload is under.
2238 pub alias: &'static str,
2239 /// The variable carrying this field's input.
2240 pub variable: &'static str,
2241 /// The boolean variable whose `true` sends this field.
2242 pub include: &'static str,
2243}
2244
2245/// The value writes [`graphql::UPDATE_FIELDS`] carries, in the order GitHub runs them.
2246pub const FIELD_WRITE_SLOTS: [FieldSlot; 6] = [
2247 FieldSlot {
2248 alias: "updateProjectV2ItemFieldValue",
2249 variable: "input",
2250 include: "writeFirst",
2251 },
2252 FieldSlot {
2253 alias: "second",
2254 variable: "second",
2255 include: "writeSecond",
2256 },
2257 FieldSlot {
2258 alias: "third",
2259 variable: "third",
2260 include: "writeThird",
2261 },
2262 FieldSlot {
2263 alias: "fourth",
2264 variable: "fourth",
2265 include: "writeFourth",
2266 },
2267 FieldSlot {
2268 alias: "fifth",
2269 variable: "fifth",
2270 include: "writeFifth",
2271 },
2272 FieldSlot {
2273 alias: "sixth",
2274 variable: "sixth",
2275 include: "writeSixth",
2276 },
2277];
2278
2279/// The clears [`graphql::UPDATE_FIELDS`] carries, run after every write of the same request.
2280pub const FIELD_CLEAR_SLOTS: [FieldSlot; 3] = [
2281 FieldSlot {
2282 alias: "cleared",
2283 variable: "clear",
2284 include: "writeClear",
2285 },
2286 FieldSlot {
2287 alias: "clearedSecond",
2288 variable: "clearSecond",
2289 include: "writeClearSecond",
2290 },
2291 FieldSlot {
2292 alias: "clearedThird",
2293 variable: "clearThird",
2294 include: "writeClearThird",
2295 },
2296];
2297
2298/// Which of GitHub's two rate limiters refused a request.
2299///
2300/// Waiting is the whole answer to the primary budget, and polling is what *extends* the
2301/// secondary one — so an operator told the wrong one takes the wrong next step, which is
2302/// the whole reason this is carried rather than collapsed into "rate limited".
2303#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2304enum Limiter {
2305 /// The hourly API budget, which `gh api rate_limit` reports and a wait answers.
2306 Primary,
2307 /// The burst limiter over content-generating requests, which nothing reports.
2308 Secondary,
2309}
2310
2311/// The wordings GitHub answers a secondary rate limit with.
2312///
2313/// It sends them under a forbidden status, under a too-many-requests status, and inside
2314/// the `errors` of a *successful* response, which is why the text is what this matches on
2315/// rather than the status. `abuse detection` is the wording GitHub used before the
2316/// limiter was renamed and still returns from some endpoints; `submitted too quickly` is
2317/// what a burst of content creation is refused with.
2318///
2319/// This is GitHub's vocabulary rather than this source's, so it is pinned rather than
2320/// remembered: `tests/fixtures/rate-limits.json` records where each wording was read and
2321/// when, and the drift gate reconciles the two lists both ways. Public for that gate
2322/// alone — a caller has no use for it, and matching on a refusal is this source's job.
2323pub const SECONDARY_WORDINGS: [&str; 5] = [
2324 "secondary rate limit",
2325 "temporarily blocked from content creation",
2326 "abuse detection",
2327 "submitted too quickly",
2328 "exceeded a secondary",
2329];
2330
2331/// The wordings GitHub answers an exhausted primary budget with.
2332///
2333/// `rate_limited` is the `type` its GraphQL error carries, which is read as a field rather
2334/// than looked for in the response text. `api rate limit already exceeded` is what GraphQL
2335/// answers a request made once the hour's budget is spent — "API rate limit already exceeded
2336/// for user ID …" in the `errors` of an HTTP 200, with no `type` — and neither of the other
2337/// two phrases is a substring of it, so without it that answer read as a refusal that will
2338/// never lift. Pinned and gated exactly as [`SECONDARY_WORDINGS`] is, and public for the same
2339/// one reason.
2340pub const PRIMARY_WORDINGS: [&str; 4] = [
2341 "api rate limit exceeded",
2342 "api rate limit already exceeded",
2343 "rate limit exceeded",
2344 "rate_limited",
2345];
2346
2347/// What a response *says about itself*, which is the only place a refusal can be read.
2348///
2349/// Deliberately not the whole response body. A board is a place people write about their
2350/// own work, and a task on it titled "the secondary rate limit" would, matched across the
2351/// raw text, turn a perfectly good answer into a refusal this source then waited out and
2352/// reported. So the item data is never read: what is read is GitHub's own REST-style
2353/// `message` envelope, which is what a forbidden status carries, and the `message` and
2354/// `type` of each GraphQL error, which is where a *successful* response says it.
2355///
2356/// A body that is not JSON at all has nothing structured to read, so only a failing
2357/// response's own text is taken — a successful response that is not JSON is malformed
2358/// rather than refused, and [`GitHubProjectsSource::answer`] says so.
2359fn refusal_wording(status: StatusCode, body: &str) -> String {
2360 let Ok(parsed) = serde_json::from_str::<Value>(body) else {
2361 return if status.is_success() {
2362 String::new()
2363 } else {
2364 body.to_owned()
2365 };
2366 };
2367 let mut said: Vec<&str> = parsed
2368 .get("message")
2369 .and_then(Value::as_str)
2370 .into_iter()
2371 .collect();
2372 if let Some(errors) = parsed.get("errors").and_then(Value::as_array) {
2373 for error in errors {
2374 said.extend(
2375 ["message", "type"]
2376 .into_iter()
2377 .filter_map(|key| error.get(key).and_then(Value::as_str)),
2378 );
2379 }
2380 }
2381 said.join("; ")
2382}
2383
2384impl Limiter {
2385 /// Which limiter refused this response, or `None` when none of them did.
2386 ///
2387 /// The wording is read first and the status only decides what carries none of it,
2388 /// because GitHub answers a secondary limit with a forbidden status far more often
2389 /// than with too-many-requests — while a forbidden status saying nothing about a limit
2390 /// really is a credential this token lacks.
2391 ///
2392 /// A response is a refusal because of its status or its own wording. A spent budget
2393 /// only ever explains one; it never turns an answer into a refusal.
2394 fn classify(status: StatusCode, budget_exhausted: bool, body: &str) -> Option<Self> {
2395 let normalized = refusal_wording(status, body).to_ascii_lowercase();
2396 if SECONDARY_WORDINGS
2397 .iter()
2398 .any(|wording| normalized.contains(wording))
2399 {
2400 return Some(Self::Secondary);
2401 }
2402 if status == StatusCode::TOO_MANY_REQUESTS {
2403 return Some(Self::Primary);
2404 }
2405 // An exhausted budget *explains* a response that failed; it does not make one that
2406 // succeeded into a failure. GitHub sets `x-ratelimit-remaining: 0` on the last
2407 // request the budget allowed as well as on the ones it then refuses, so reading
2408 // the header alone threw away a good answer — and, once refusals were retried,
2409 // replayed a request that had already taken effect.
2410 if !status.is_success() && budget_exhausted {
2411 return Some(Self::Primary);
2412 }
2413 // A successful response saying it: GitHub reports a GraphQL rate limit in the
2414 // `errors` of an HTTP 200, where nothing about the status says so at all.
2415 if status.is_success()
2416 && PRIMARY_WORDINGS
2417 .iter()
2418 .any(|wording| normalized.contains(wording))
2419 {
2420 return Some(Self::Primary);
2421 }
2422 None
2423 }
2424
2425 /// What this limiter is called where an operator can look it up.
2426 const fn name(self) -> &'static str {
2427 match self {
2428 Self::Primary => "GitHub's primary API rate limit",
2429 Self::Secondary => "GitHub's secondary rate limit",
2430 }
2431 }
2432
2433 /// What the endpoint an operator would go and check says about this limiter.
2434 const fn where_to_look(self) -> &'static str {
2435 match self {
2436 Self::Primary => {
2437 "That is the budget `gh api rate_limit` reports, so that endpoint says when it \
2438 comes back."
2439 }
2440 Self::Secondary => {
2441 "That limiter is not the primary API budget: `gh api rate_limit` reports the \
2442 primary budget and does not report this one, so budget showing there says \
2443 nothing about this refusal, and every further attempt extends it."
2444 }
2445 }
2446 }
2447
2448 /// The next step this limiter actually calls for.
2449 const fn what_to_do(self) -> &'static str {
2450 match self {
2451 Self::Primary => {
2452 "wait for the reset `gh api rate_limit` reports, then run the command again."
2453 }
2454 Self::Secondary => {
2455 "leave this board alone for a few minutes, then run the command again — or \
2456 raise pacing.min_mutation_interval_ms on this source so it writes more slowly."
2457 }
2458 }
2459 }
2460}
2461
2462/// One rate-limit refusal, and the wait GitHub asked for if it asked for one.
2463#[derive(Debug, Clone, Copy)]
2464struct Limited {
2465 limiter: Limiter,
2466 hint: Option<u64>,
2467 /// Which header the hint came from: `retry-after`, or an exhausted primary budget's
2468 /// `x-ratelimit-reset`.
2469 hinted_by: Hint,
2470}
2471
2472/// Where a refusal's hint came from, which is what its wait is named after.
2473#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2474enum Hint {
2475 /// `retry-after`, or no hint at all.
2476 RetryAfter,
2477 /// An exhausted primary budget's `x-ratelimit-reset`.
2478 PrimaryReset,
2479}
2480
2481impl Limited {
2482 /// The wait this refusal is answered with, when the backing-off schedule is at `backoff`,
2483 /// and the cause it is named by: the hint, raised to the schedule's own next wait —
2484 /// GitHub really does send `retry-after: 0`, and retrying at once is the one move that
2485 /// extends a secondary limit — or the schedule alone when there is none.
2486 fn wait(self, backoff: Duration) -> (Duration, WaitCause) {
2487 match (self.hint, self.hinted_by) {
2488 (Some(hint), Hint::RetryAfter) => (
2489 Duration::from_secs(hint).max(backoff),
2490 pacing::cause::RETRY_AFTER,
2491 ),
2492 (Some(hint), Hint::PrimaryReset) => (
2493 Duration::from_secs(hint).max(backoff),
2494 pacing::cause::PRIMARY_RESET,
2495 ),
2496 (None, _) => (backoff, pacing::cause::BACKOFF),
2497 }
2498 }
2499
2500 /// What the caller is told once this source has waited as long as it may.
2501 ///
2502 /// Both limiters report as [`SourceError::RateLimited`], because that is what
2503 /// happened: the kind a caller matches on says a rate limit refused this, and nothing
2504 /// about *which* limiter it was makes it a different kind of failure. What differs is
2505 /// the operator's next step, and that is what the message carries — a secondary
2506 /// refusal read as a primary one sends an operator to `gh api rate_limit`, where the
2507 /// budget looks fine, and then back to retry the very burst that was refused.
2508 fn exhausted(
2509 self,
2510 doing: &str,
2511 waits: u32,
2512 waited: Duration,
2513 needed: Duration,
2514 budget: Duration,
2515 ) -> SourceError {
2516 SourceError::RateLimited {
2517 retry_after_seconds: self.hint,
2518 message: Some(format!(
2519 "{} refused this source while {doing}; it waited {} out over {} and was refused \
2520 again, and the next wait of {} would take it past the {} one call may spend \
2521 waiting. {} next: {}",
2522 self.limiter.name(),
2523 plural(waits, "refusal"),
2524 seconds(waited),
2525 seconds(needed),
2526 seconds(budget),
2527 self.limiter.where_to_look(),
2528 self.limiter.what_to_do(),
2529 )),
2530 }
2531 }
2532}
2533
2534/// One HTTP attempt's result, with what its response said about the rate limit.
2535///
2536/// The two travel together so the record and the outcome are written from the same place:
2537/// what a response said about the budget is only readable while that response is in hand,
2538/// and what the attempt *meant* is only decidable once its body has been read.
2539struct Attempted {
2540 result: Result<Value, Attempt>,
2541 limits: accounting::RateLimit,
2542 /// GitHub's own reported cost for this call, for a document that asked for it.
2543 reported_cost: Option<u64>,
2544 response_outcome: accounting::ResponseOutcome,
2545}
2546
2547/// One attempt's outcome: an error to report, or a rate limit to wait out.
2548enum Attempt {
2549 Failed(SourceError),
2550 Limited(Limited),
2551}
2552
2553fn plural(count: u32, thing: &str) -> String {
2554 if count == 1 {
2555 format!("{count} {thing}")
2556 } else {
2557 format!("{count} {thing}s")
2558 }
2559}
2560
2561fn seconds(duration: Duration) -> String {
2562 format!("{:.1}s", duration.as_secs_f64())
2563}
2564
2565/// The member [`stamp_served`] writes onto every node of a response that says when it last
2566/// changed: the `Date` that response was served at. No GitHub type has a field of this name.
2567const SERVED_AT: &str = "onetaskgraphServedAt";
2568
2569/// Write `served` onto every object of `data` carrying an `updatedAt`, so whatever reads a
2570/// node's time later knows the server's clock on the response that read it — which is what
2571/// decides whether that node's change token is given (see [`change_token::SETTLED`]).
2572fn stamp_served(data: &mut Value, served: &str) {
2573 match data {
2574 Value::Object(members) => {
2575 if members.contains_key("updatedAt") {
2576 members.insert(SERVED_AT.to_owned(), Value::String(served.to_owned()));
2577 }
2578 for member in members.values_mut() {
2579 stamp_served(member, served);
2580 }
2581 }
2582 Value::Array(items) => {
2583 for item in items {
2584 stamp_served(item, served);
2585 }
2586 }
2587 _ => {}
2588 }
2589}
2590
2591/// A header GitHub spells as a whole number of seconds, or `None` when this one is not.
2592///
2593/// A value that is present and unreadable is deliberately *not* an error. `retry-after` is
2594/// allowed by HTTP to be a date rather than a count, an intermediary can rewrite either
2595/// header, and neither is what makes a response a refusal — so the whole cost of one this
2596/// cannot read is that the refusal carries no hint and the backing-off schedule answers it
2597/// instead. Refusing the response over the header would turn a readable refusal into an
2598/// unreadable one, and refusing to *wait* would be the one wrong direction to fail in.
2599fn whole_seconds(value: Option<&reqwest::header::HeaderValue>) -> Option<u64> {
2600 value
2601 .and_then(|value| value.to_str().ok())
2602 .and_then(|value| value.trim().parse::<u64>().ok())
2603}
2604
2605/// A transport failure with every cause beneath it, outermost first.
2606///
2607/// The client's own text for a request it could not complete is `error sending request for
2608/// url (…)`, and the socket error that says why — a refused or reset connection, an exhausted
2609/// port range, a connection closed before its answer — is only in the chain of sources below
2610/// it. Those differ in what to do next, so all of them are kept.
2611pub(crate) fn transport_error(error: &(dyn std::error::Error + 'static)) -> String {
2612 let mut text = error.to_string();
2613 let mut cause = error.source();
2614 while let Some(next) = cause {
2615 let said = next.to_string();
2616 if !text.ends_with(&said) {
2617 text = format!("{text}: {said}");
2618 }
2619 cause = next.source();
2620 }
2621 text
2622}
2623
2624/// Every mutation this source sends creates content — an issue, a board item, a field of
2625/// one, a sub-issue link, a dependency, a comment — or edits or removes content of that
2626/// kind, and no query in [`graphql::DOCUMENTS`] does, so what the secondary limiter counts
2627/// and what the keyword says are the same set. That is what makes the keyword a sound test
2628/// rather than a convenient one: counting an edit or a removal the limiter might not have
2629/// counted costs a wait, and not counting one it did count costs the next fifty minutes.
2630pub(crate) fn is_mutation(query: &str) -> bool {
2631 query.trim_start().starts_with("mutation")
2632}
2633
2634/// What this source was doing, for a diagnostic that has to say so.
2635///
2636/// Read out of [`graphql::DOCUMENTS`], which is the inventory rather than a copy of it, so
2637/// a document added without a description is caught by that list's own gate instead of
2638/// falling through to the vague arm below.
2639fn operation_description(query: &str) -> &'static str {
2640 graphql::DOCUMENTS
2641 .iter()
2642 .find(|(document, _)| *document == query)
2643 .map_or("talking to GitHub", |(_, doing)| *doing)
2644}
2645
2646/// GitHub's published ceiling on content-generating requests in any one minute.
2647///
2648/// Pinned in `tests/fixtures/rate-limits.json` and gated against it, because it is
2649/// GitHub's number rather than this source's. This source paces at it over its window: a
2650/// content-creating request goes out at once while the minute's creations leave room for it,
2651/// and waits until they do otherwise — see the `pacing` module.
2652pub const CONTENT_CREATION_PER_MINUTE: u64 = 80;
2653/// The same ceiling as GitHub publishes it for any one hour, paced over its window as the
2654/// per-minute one is. Pinned beside its sibling so the gate would notice either one moving.
2655pub const CONTENT_CREATION_PER_HOUR: u64 = 500;
2656/// GitHub's published ceiling on secondary points in any one minute: what every GraphQL
2657/// request is charged against, a mutation [`SECONDARY_POINTS_PER_MUTATION`] and a query
2658/// [`SECONDARY_POINTS_PER_QUERY`]. Pinned and paced as [`CONTENT_CREATION_PER_MINUTE`] is.
2659pub const SECONDARY_POINTS_PER_MINUTE: u64 = 2_000;
2660/// The secondary points one GraphQL request carrying a mutation is charged.
2661pub const SECONDARY_POINTS_PER_MUTATION: u64 = 5;
2662/// The secondary points one GraphQL request carrying no mutation is charged.
2663pub const SECONDARY_POINTS_PER_QUERY: u64 = 1;
2664/// The fastest *steady* spacing of content-creating mutations that cannot exceed
2665/// [`CONTENT_CREATION_PER_MINUTE`], in milliseconds: 60000/80 is 750.
2666///
2667/// It is no longer the default. This source paces by GitHub's windows instead, so a copy's
2668/// few dozen writes go out as fast as GitHub answers them while the minute has room, rather
2669/// than 750 ms apart — which was most of a plan copy's wall time. An installation that wants
2670/// mutations spaced as well says so through `pacing.min_mutation_interval_ms`, and this is the
2671/// value that holds a long sustained burst under the per-minute bound by spacing alone.
2672pub const MIN_MUTATION_INTERVAL_MS: u64 = 60_000 / CONTENT_CREATION_PER_MINUTE;
2673/// First wait when a rate-limit refusal carries no hint; each further wait doubles it.
2674///
2675/// A doubling schedule from one second reaches a minute in six waits, which is GitHub's
2676/// own advice for a secondary limit — wait, and wait longer each time — without spending
2677/// the first minute of a transient refusal doing nothing.
2678pub const RETRY_BACKOFF_MS: u64 = 1_000;
2679/// Total time one call may spend waiting out rate limits before it reports a failure.
2680///
2681/// Two minutes is long enough to ride out the refusals a paced copy still collects and
2682/// short enough that a command an operator is watching returns. The bound is what makes
2683/// the wait a wait rather than a hang: a call refused past it ends in a diagnostic naming
2684/// the limiter, not in a process nobody can tell from a wedged one.
2685pub const RETRY_BUDGET_MS: u64 = 120_000;
2686
2687fn default_token_env() -> String {
2688 "GH_PROJECTS_TOKEN".to_owned()
2689}
2690fn default_endpoint() -> String {
2691 "https://api.github.com/graphql".to_owned()
2692}
2693
2694/// The name of a `Status` single-select option on the board.
2695///
2696/// Validated on the way in rather than checked later, so a blank option name — which
2697/// nothing on a board can be — is a state this type cannot hold.
2698#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2699#[serde(try_from = "String")]
2700#[schemars(extend("minLength" = 1))]
2701pub struct ColumnName(String);
2702
2703impl ColumnName {
2704 /// The option name, as the board spells it.
2705 fn as_str(&self) -> &str {
2706 &self.0
2707 }
2708}
2709
2710impl TryFrom<String> for ColumnName {
2711 type Error = String;
2712
2713 fn try_from(name: String) -> Result<Self, Self::Error> {
2714 if name.trim().is_empty() {
2715 return Err("a status_mapping option name cannot be blank".to_owned());
2716 }
2717 Ok(Self(name))
2718 }
2719}
2720
2721/// The two closed states this product can mean.
2722///
2723/// GitHub's `IssueClosedStateReason` also spells `DUPLICATE`, which nothing here ever writes:
2724/// an issue closed as one *reads* `cancelled`, because its work is another issue's, but this
2725/// product never decides on a person's behalf that two issues are the same work.
2726#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, schemars::JsonSchema)]
2727#[serde(rename_all = "kebab-case")]
2728pub enum ClosedState {
2729 /// `COMPLETED` — precisely done.
2730 Completed,
2731 /// `NOT_PLANNED` — precisely cancelled.
2732 NotPlanned,
2733}
2734
2735impl ClosedState {
2736 const fn reason(self) -> &'static str {
2737 match self {
2738 Self::Completed => "COMPLETED",
2739 Self::NotPlanned => "NOT_PLANNED",
2740 }
2741 }
2742}
2743
2744/// Configuration for one GitHub Projects v2 board.
2745///
2746/// It serializes to a document this same type reads back as itself, so a caller writing a
2747/// configuration — a consumer filing onto a board it configures — can build one here rather
2748/// than spell the shape again; an empty `metadata_fields` is left out, as one never set.
2749#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2750#[serde(default, deny_unknown_fields)]
2751pub struct GitHubProjectsConfig {
2752 /// Login of the user or organization which owns the board.
2753 pub owner: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates GitHub's owner grammar before private construction.
2754 /// The project number shown in the board's GitHub URL.
2755 pub project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` bounds this to a positive GraphQL Int.
2756 // 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.
2757 /// `owner/name` of the repository this source creates an issue in when the item's own
2758 /// `repositories` field does not decide it.
2759 ///
2760 /// An item naming exactly one repository is created there — on a source declared
2761 /// `visibility: private`, only when that repository reads private, and otherwise as an
2762 /// item naming none is; a task or a document naming none or several is created in its
2763 /// parent project's repository; and a project, or a task or document with no parent,
2764 /// naming none or several is created here. A board
2765 /// has no repository of its own and `createIssue` requires one, so a write without
2766 /// this is refused naming the field. Reads never need it.
2767 pub repository: Option<String>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the `owner/name` grammar before private construction.
2768 // llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
2769 /// Environment variable containing a fine-grained token with Projects and Issues
2770 /// read/write plus Pull requests read-only access for every repository represented on
2771 /// the board.
2772 #[serde(default = "default_token_env")]
2773 pub token_env: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the environment-variable grammar.
2774 /// GraphQL endpoint. GitHub Enterprise installations may override it.
2775 #[serde(default = "default_endpoint")]
2776 pub endpoint: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` converts it to the private validated `Url`.
2777 /// Per-instance mapping from a status category to the option of the board's one
2778 /// `Status` field it lands on, for a task and for a project.
2779 ///
2780 /// The shared `StatusMapping` grammar: each value is one option name for both kinds,
2781 /// `null` to disable the category for both, or `{task, project}` naming it per kind,
2782 /// where a kind left out leaves the category unmapped for that kind. A category this
2783 /// does not mention keeps its shipped default for both kinds: `backlog` to "Backlog",
2784 /// `todo` to "Todo", `queued` to "Queued", `in-progress` to "In Progress", `done` to
2785 /// "Done" plus closed as completed, `cancelled` to "Cancelled" plus closed as not
2786 /// planned, and `draft` and `unknown` unmapped. A category it does mention gets no
2787 /// shipped default for a kind it leaves out. `done` and `cancelled` close the issue for
2788 /// either kind. No two categories may name one option for the same kind, ignoring case.
2789 /// `unknown` may name one existing option; every unknown word then lands on it and
2790 /// reads back as `unknown` under its name. Unlike `local-md`, this source cannot keep
2791 /// each unknown word because it never creates board options.
2792 #[serde(default)]
2793 pub status_mapping: StatusMapping,
2794 /// Per-instance mapping from a task's priority to an option of this board's
2795 /// single-select field named `Priority`.
2796 ///
2797 /// Absent, this source holds no priority: every task reads as `none`, and a write of any
2798 /// other priority is refused before it reaches this board. Present, each of `urgent`,
2799 /// `high`, `medium` and `low` it does not mention keeps its shipped default — `Urgent`,
2800 /// `High`, `Medium` and `Low` — and an item with no value in the `Priority` field reads
2801 /// as `none`, so writing `none` clears the value. Option names match case-insensitively;
2802 /// no two levels may name one option. Reads and writes never create the field or an
2803 /// option: `onetaskgraph sources fields <source> --apply` does, and a write naming one
2804 /// the board lacks is refused pointing there.
2805 #[serde(default)]
2806 pub priority_mapping: Option<PriorityMappingConfig>,
2807 /// Caller metadata values this source also writes to board text fields, so the board can
2808 /// filter on them.
2809 ///
2810 /// Absent or empty, nothing is projected. Each entry names a board text `field`, a
2811 /// top-level metadata `key` and an optional `path` of object keys walked inside that key's
2812 /// value. On every item write — create, update and copy, of a task, a project or a
2813 /// document — a string found there is written to the field, and nothing is sent when the
2814 /// field already holds it; nothing there, or `null`, clears the field when it holds a
2815 /// value; any other JSON type is refused before any mutation. The metadata comment in the
2816 /// issue body stays the one home of the value, and every read reports metadata from it.
2817 /// Reads and writes never create the field: `onetaskgraph sources fields <source>
2818 /// --apply` does, as a text field, and a write to a board lacking it, or holding a
2819 /// non-text field of that name, is refused pointing there.
2820 #[serde(default, skip_serializing_if = "Vec::is_empty")]
2821 // Kept in the schema as `"default": []` although a serialized configuration leaves an empty
2822 // list out, so both SDKs model an absent list as an empty one rather than as `null`.
2823 #[schemars(!skip_serializing_if)]
2824 pub metadata_fields: Vec<MetadataFieldConfig>,
2825 /// How fast this source writes, and how long it waits out a rate-limit refusal.
2826 ///
2827 /// Every field keeps its shipped default when it is absent, and the defaults are
2828 /// GitHub's own published limits rather than taste. See [`Pacing`].
2829 #[serde(default)]
2830 pub pacing: PacingConfig,
2831}
2832
2833/// Which option of the board's `Priority` field each priority lands on.
2834///
2835/// One member per level rather than a map, so a key that is not a level is refused where
2836/// the configuration is read, naming the levels there are. `none` is not a member: it is no
2837/// value in the field, not an option of it.
2838#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2839#[serde(default, deny_unknown_fields)]
2840pub struct PriorityMappingConfig {
2841 /// The option `urgent` lands on; `Urgent` when absent.
2842 pub urgent: Option<PriorityOptionName>,
2843 /// The option `high` lands on; `High` when absent.
2844 pub high: Option<PriorityOptionName>,
2845 /// The option `medium` lands on; `Medium` when absent.
2846 pub medium: Option<PriorityOptionName>,
2847 /// The option `low` lands on; `Low` when absent.
2848 pub low: Option<PriorityOptionName>,
2849}
2850
2851/// The name of an option of the board's `Priority` single-select field.
2852///
2853/// Validated on the way in, for the reason [`ColumnName`] is: nothing on a board can have a
2854/// blank name.
2855#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2856#[serde(try_from = "String")]
2857#[schemars(extend("minLength" = 1))]
2858pub struct PriorityOptionName(String);
2859
2860impl PriorityOptionName {
2861 /// The option name, as the board spells it.
2862 fn as_str(&self) -> &str {
2863 &self.0
2864 }
2865}
2866
2867impl TryFrom<String> for PriorityOptionName {
2868 type Error = String;
2869
2870 fn try_from(name: String) -> Result<Self, Self::Error> {
2871 if name.trim().is_empty() {
2872 return Err("a priority_mapping option name cannot be blank".to_owned());
2873 }
2874 Ok(Self(name))
2875 }
2876}
2877
2878/// The name of the board field a priority is held in.
2879pub const PRIORITY_FIELD: &str = "Priority";
2880
2881/// The four priorities a board option can hold, in the order a new `Priority` field lists
2882/// them. `none` is not among them: it is the field holding no value.
2883///
2884/// This list mirrors `Priority`, so it carries its own drift gate, in the shape [`CATEGORIES`]
2885/// does: [`level_position`] is a wildcard-free match, so a priority added to the shared
2886/// vocabulary fails to compile until it is placed there, and this crate's suite reconciles
2887/// this list and [`PriorityMappingConfig`]'s members against that enum's own derived schema.
2888pub const PRIORITY_LEVELS: [Priority; 4] = [
2889 Priority::Urgent,
2890 Priority::High,
2891 Priority::Medium,
2892 Priority::Low,
2893];
2894
2895/// Where one priority sits in [`PRIORITY_LEVELS`], or `None` for `none`, which is no option;
2896/// see that list for what this pins.
2897#[must_use]
2898pub const fn level_position(priority: Priority) -> Option<usize> {
2899 match priority {
2900 Priority::None => None,
2901 Priority::Urgent => Some(0),
2902 Priority::High => Some(1),
2903 Priority::Medium => Some(2),
2904 Priority::Low => Some(3),
2905 }
2906}
2907
2908/// This instance's complete priority-to-option mapping, read in both directions.
2909///
2910/// One option per level, held in [`PRIORITY_LEVELS`] order, once it is established that no
2911/// two levels name one option.
2912#[derive(Debug, Clone)]
2913struct PriorityMapping {
2914 options: [PriorityOptionName; 4],
2915}
2916
2917impl PriorityMapping {
2918 fn resolve(config: PriorityMappingConfig, instance: &SourceName) -> Result<Self, SourceError> {
2919 let shipped = |name: &str| PriorityOptionName(name.to_owned());
2920 let mapping = Self {
2921 options: [
2922 config.urgent.unwrap_or_else(|| shipped("Urgent")),
2923 config.high.unwrap_or_else(|| shipped("High")),
2924 config.medium.unwrap_or_else(|| shipped("Medium")),
2925 config.low.unwrap_or_else(|| shipped("Low")),
2926 ],
2927 };
2928 for (index, option) in mapping.options.iter().enumerate() {
2929 if let Some(earlier) = mapping.options[..index]
2930 .iter()
2931 .position(|other| other.as_str().eq_ignore_ascii_case(option.as_str()))
2932 {
2933 return Err(SourceError::Config {
2934 message: format!(
2935 "priority_mapping of source {instance} sends both {} and {} to the board \
2936 option {:?}; one option cannot read back as two priorities",
2937 PRIORITY_LEVELS[earlier],
2938 PRIORITY_LEVELS[index],
2939 option.as_str()
2940 ),
2941 });
2942 }
2943 }
2944 Ok(mapping)
2945 }
2946
2947 /// The option `priority` lands on, or `None` for `none`, which is no option at all.
2948 fn option(&self, priority: Priority) -> Option<&str> {
2949 level_position(priority).map(|index| self.options[index].as_str())
2950 }
2951
2952 /// The priority a board option name reports, or `None` when nothing maps to it.
2953 fn priority_of(&self, option: &str) -> Option<Priority> {
2954 self.options
2955 .iter()
2956 .position(|name| name.as_str().eq_ignore_ascii_case(option))
2957 .map(|index| PRIORITY_LEVELS[index])
2958 }
2959
2960 /// Every mapped option name, in the order a new `Priority` field lists them.
2961 fn names(&self) -> impl Iterator<Item = &str> {
2962 self.options.iter().map(PriorityOptionName::as_str)
2963 }
2964}
2965
2966/// One metadata value this source projects onto a board text field.
2967#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2968#[serde(deny_unknown_fields)]
2969pub struct MetadataFieldConfig {
2970 /// The name of the board text field the value is written to.
2971 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.
2972 /// The top-level metadata key the value is read from. A key under the reserved
2973 /// `onetaskgraph.` prefix is refused.
2974 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.
2975 /// Object keys walked inside that key's value, outermost first. Absent or empty, the
2976 /// key's own value is the one projected. Left out of a serialized entry when empty.
2977 #[serde(default, skip_serializing_if = "Vec::is_empty")]
2978 // Kept in the schema as `"default": []`, for the reason `metadata_fields`' is.
2979 #[schemars(!skip_serializing_if)]
2980 pub path: Vec<String>,
2981}
2982
2983/// The board fields no metadata value may be projected onto, compared ignoring case: the
2984/// three this source writes itself, and those GitHub owns on every board.
2985// 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.
2986const UNPROJECTABLE_FIELDS: [&str; 13] = [
2987 STATUS_FIELD,
2988 PRIORITY_FIELD,
2989 ORIGIN_FIELD,
2990 "Title",
2991 "Assignees",
2992 "Labels",
2993 "Linked pull requests",
2994 "Milestone",
2995 "Repository",
2996 "Reviewers",
2997 "Parent issue",
2998 "Sub-issues progress",
2999 "Type",
3000];
3001
3002/// One validated [`MetadataFieldConfig`] entry.
3003#[derive(Debug, Clone)]
3004struct MetadataField {
3005 field: ProjectedField,
3006 key: ProjectedKey,
3007 path: Vec<String>,
3008}
3009
3010/// The name of a board text field a metadata value is projected onto: never blank, never one
3011/// this source or GitHub writes, and never one another entry of the same source names.
3012#[derive(Debug, Clone, PartialEq, Eq)]
3013struct ProjectedField(String);
3014
3015/// The top-level metadata key a projected value is read from: never blank, and never under
3016/// the namespace this product reserves.
3017#[derive(Debug, Clone, PartialEq, Eq)]
3018struct ProjectedKey(String);
3019
3020impl std::ops::Deref for ProjectedField {
3021 type Target = str;
3022 fn deref(&self) -> &str {
3023 &self.0
3024 }
3025}
3026
3027impl std::ops::Deref for ProjectedKey {
3028 type Target = str;
3029 fn deref(&self) -> &str {
3030 &self.0
3031 }
3032}
3033
3034/// What one item's metadata says one projected field should hold.
3035#[derive(Debug, Clone, PartialEq, Eq)]
3036enum Projected {
3037 /// A string to hold.
3038 Text(String),
3039 /// Nothing: the key or a step of the path is absent, or the value is `null`.
3040 Nothing,
3041}
3042
3043impl MetadataField {
3044 /// Validate every entry of one instance's `metadata_fields`, refusing the first that
3045 /// cannot stand, naming the source and the entry.
3046 fn resolve(
3047 configured: Vec<MetadataFieldConfig>,
3048 instance: &SourceName,
3049 ) -> Result<Vec<Self>, SourceError> {
3050 let mut resolved: Vec<Self> = Vec::with_capacity(configured.len());
3051 for (index, entry) in configured.into_iter().enumerate() {
3052 let refuse = |why: String| SourceError::Config {
3053 message: format!(
3054 "metadata_fields[{index}] of source {instance} (field {:?}, key {:?}) {why}",
3055 entry.field, entry.key
3056 ),
3057 };
3058 if entry.field.trim().is_empty() {
3059 return Err(refuse("names a blank field".to_owned()));
3060 }
3061 if entry.key.trim().is_empty() {
3062 return Err(refuse("names a blank key".to_owned()));
3063 }
3064 if entry.key.split('.').next() == Some(MetadataKey::RESERVED_NAMESPACE) {
3065 return Err(refuse(format!(
3066 "names a key under the reserved \"{}.\" prefix, which this product keeps \
3067 for its own keys; next: project a caller key instead",
3068 MetadataKey::RESERVED_NAMESPACE
3069 )));
3070 }
3071 if let Some(owned) = UNPROJECTABLE_FIELDS
3072 .iter()
3073 .find(|owned| owned.eq_ignore_ascii_case(&entry.field))
3074 {
3075 return Err(refuse(format!(
3076 "names the board field {owned:?}, which this source or GitHub already \
3077 writes; next: name a text field of your own"
3078 )));
3079 }
3080 if let Some(earlier) = resolved
3081 .iter()
3082 .position(|other| other.field.eq_ignore_ascii_case(&entry.field))
3083 {
3084 return Err(refuse(format!(
3085 "names the same board field as metadata_fields[{earlier}]; one field \
3086 cannot hold two values"
3087 )));
3088 }
3089 resolved.push(Self {
3090 field: ProjectedField(entry.field),
3091 key: ProjectedKey(entry.key),
3092 path: entry.path,
3093 });
3094 }
3095 Ok(resolved)
3096 }
3097
3098 /// The path as a refusal spells it: `[]` for the key's own value.
3099 fn spelled_path(&self) -> String {
3100 serde_json::to_string(&self.path).unwrap_or_else(|_| format!("{:?}", self.path))
3101 }
3102
3103 /// What `metadata` says this field should hold, or the refusal for a value no text field
3104 /// can hold.
3105 fn projected(
3106 &self,
3107 metadata: &BTreeMap<String, Value>,
3108 instance: &SourceName,
3109 ) -> Result<Projected, SourceError> {
3110 let mut value = metadata.get(&*self.key);
3111 for step in &self.path {
3112 value = value.and_then(|held| held.get(step.as_str()));
3113 }
3114 let found = match value {
3115 None | Some(Value::Null) => return Ok(Projected::Nothing),
3116 Some(Value::String(text)) => return Ok(Projected::Text(text.clone())),
3117 Some(Value::Bool(_)) => "a boolean",
3118 Some(Value::Number(_)) => "a number",
3119 Some(Value::Array(_)) => "an array",
3120 Some(Value::Object(_)) => "an object",
3121 };
3122 Err(SourceError::Refused {
3123 message: format!(
3124 "source {instance} projects metadata key {:?} at path {} onto its board text \
3125 field {:?}, and this item holds {found} there; only a string, null or nothing \
3126 can be written to a text field; next: store a string there, or remove the \
3127 value",
3128 &*self.key,
3129 self.spelled_path(),
3130 &*self.field
3131 ),
3132 })
3133 }
3134}
3135
3136/// What one item's `Priority` field says, read through this instance's mapping.
3137#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
3138enum HeldPriority {
3139 /// A priority this source reports: an option the mapping names, or no value (`none`).
3140 Read(Priority),
3141 /// An option the mapping does not name, which is never read as a level or as `none`.
3142 Unmapped(String),
3143}
3144
3145/// How fast this source writes, and how long it waits out a rate-limit refusal.
3146///
3147/// Configurable because a GitHub Enterprise installation sets its own limits and an
3148/// operator who has already been refused may want to go slower still — not because the
3149/// defaults are guesses.
3150#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3151#[serde(default, deny_unknown_fields)]
3152pub struct PacingConfig {
3153 /// Shortest interval between two content-creating mutations, in milliseconds.
3154 ///
3155 /// Zero sends them as fast as they are asked for, which is what a fixture server on
3156 /// loopback wants and what no board on github.com does. At most [`MAX_PACING_MS`].
3157 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.
3158 /// First wait when a rate-limit refusal carries no hint, in milliseconds. Each
3159 /// further wait of the same call doubles it. At most [`MAX_PACING_MS`], and never
3160 /// zero while there is a budget to spend, because a schedule of zero-length waits
3161 /// consumes none of it and so never ends.
3162 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.
3163 /// Total time one call may spend waiting out rate limits, in milliseconds.
3164 ///
3165 /// Zero reports the refusal rather than waiting at all. At most [`MAX_PACING_MS`]:
3166 /// the bound is what makes this a wait rather than a hang.
3167 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.
3168}
3169
3170/// The largest any pacing setting may be, in milliseconds.
3171///
3172/// One hour. GitHub's own harshest published bound on content-generating requests works
3173/// out at one every 7.2 seconds, so an hour is already three orders of magnitude past
3174/// anything a real limit asks for, and past it the settings stop describing pacing at all:
3175/// a wait budget beyond it is the unbounded wait this whole mechanism exists to replace,
3176/// and an interval beyond it is a command that never sends its second mutation. It also
3177/// keeps the clock arithmetic in [`GitHubProjectsSource::reserve_mutation_slot`] inside
3178/// what a `Duration` can hold on every platform.
3179pub const MAX_PACING_MS: u64 = 3_600_000;
3180
3181/// [`PacingConfig`] with every default resolved and every value checked, which is what the
3182/// source holds.
3183///
3184/// Unset, `min_mutation_interval_ms` spaces nothing, and every request is paced by GitHub's
3185/// secondary windows alone — see the `pacing` module; set, it spaces content-creating mutations
3186/// on top of those windows, [`MIN_MUTATION_INTERVAL_MS`] being the spacing that holds a sustained
3187/// burst under the per-minute bound by spacing alone; and zero sends every request as fast as it
3188/// is asked for, windows included.
3189#[derive(Debug, Clone, Copy)]
3190struct Pacing {
3191 /// Whether requests are held for GitHub's secondary windows: always, unless
3192 /// `min_mutation_interval_ms` is set to exactly zero, which has always asked for no
3193 /// pacing at all — what a fixture server on loopback wants and no board on github.com does.
3194 windows: bool,
3195 min_mutation_interval: Duration,
3196 retry_backoff: Duration,
3197 retry_budget: Duration,
3198}
3199
3200impl Pacing {
3201 /// Resolve one instance's pacing, refusing a configuration that would not pace at all.
3202 fn resolve(config: PacingConfig, instance: &SourceName) -> Result<Self, SourceError> {
3203 let bounded = |value: Option<u64>, default: u64, field: &str| match value {
3204 Some(value) if value > MAX_PACING_MS => Err(SourceError::Config {
3205 message: format!(
3206 "pacing.{field} of source {instance} is {value} ms, and the most any pacing \
3207 setting may be is {MAX_PACING_MS} ms — an hour, which is already far past \
3208 GitHub's own harshest published limit"
3209 ),
3210 }),
3211 Some(value) => Ok(Duration::from_millis(value)),
3212 None => Ok(Duration::from_millis(default)),
3213 };
3214 let retry_backoff = bounded(
3215 config.retry_backoff_ms,
3216 RETRY_BACKOFF_MS,
3217 "retry_backoff_ms",
3218 )?;
3219 let retry_budget = bounded(config.retry_budget_ms, RETRY_BUDGET_MS, "retry_budget_ms")?;
3220 if retry_backoff.is_zero() && !retry_budget.is_zero() {
3221 return Err(SourceError::Config {
3222 message: format!(
3223 "pacing.retry_backoff_ms of source {instance} is 0 while \
3224 pacing.retry_budget_ms is {} ms; a schedule of zero-length waits spends \
3225 none of that budget, so it would retry a refusal forever. Set a backoff of \
3226 at least 1 ms, or set retry_budget_ms to 0 to report a refusal without \
3227 waiting at all",
3228 retry_budget.as_millis()
3229 ),
3230 });
3231 }
3232 Ok(Self {
3233 windows: config.min_mutation_interval_ms != Some(0),
3234 min_mutation_interval: bounded(
3235 config.min_mutation_interval_ms,
3236 0,
3237 "min_mutation_interval_ms",
3238 )?,
3239 retry_backoff,
3240 retry_budget,
3241 })
3242 }
3243}
3244
3245/// Factory for [`GitHubProjectsSource`].
3246#[derive(Debug, Clone, Copy, Default)]
3247pub struct Plugin;
3248
3249impl SourcePlugin for Plugin {
3250 fn kind(&self) -> &'static str {
3251 KIND
3252 }
3253 /// The endpoint, the owner and the board's number: what names one board.
3254 fn remote_identity(&self, config: &Value) -> Option<String> {
3255 let config: GitHubProjectsConfig = serde_json::from_value(config.clone()).ok()?;
3256 Some(format!(
3257 "{}\n{}\n{}",
3258 config.endpoint, config.owner, config.project_number
3259 ))
3260 }
3261 fn config_schema(&self) -> Schema {
3262 schema_for!(GitHubProjectsConfig)
3263 }
3264 fn build(
3265 &self,
3266 name: &SourceName,
3267 config: &Value,
3268 secrets: &dyn SecretResolver,
3269 ) -> Result<Box<dyn TaskSource>, SourceError> {
3270 self.build_recording_into(name, config, secrets, Arc::new(Accounting::new()))
3271 }
3272
3273 fn build_with_clock(
3274 &self,
3275 name: &SourceName,
3276 config: &Value,
3277 secrets: &dyn SecretResolver,
3278 clock: SharedClock,
3279 ) -> Result<Box<dyn TaskSource>, SourceError> {
3280 self.build_recording_with_clock(name, config, secrets, Arc::new(Accounting::new()), clock)
3281 }
3282}
3283
3284/// The client `cell` holds, building it with `build` the first time it is asked for.
3285///
3286/// A failed build is the request's failure, with the message the source's construction gave it
3287/// before clients were deferred.
3288// 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`.
3289fn built(
3290 cell: &std::sync::OnceLock<Client>,
3291 build: impl FnOnce() -> Result<Client, SourceError>,
3292) -> Result<&Client, SourceError> {
3293 if let Some(client) = cell.get() {
3294 return Ok(client);
3295 }
3296 let client = build()?;
3297 Ok(cell.get_or_init(|| client))
3298}
3299
3300impl Plugin {
3301 /// Build a source recording every request it sends into an accounting the caller holds.
3302 ///
3303 /// [`SourcePlugin::build`] is this with an accounting of its own, which is what the
3304 /// registry gets. This is for a caller that is also calling GitHub itself and wants one
3305 /// session total rather than two — see [`accounting`] and
3306 /// [`GitHubProjectsSource::recording_into`].
3307 ///
3308 /// # Errors
3309 ///
3310 /// Exactly [`SourcePlugin::build`]'s, with the same source name in front of each:
3311 /// [`SourceError::Config`] for configuration this plugin cannot use and
3312 /// [`SourceError::Auth`] for a credential it cannot find.
3313 pub fn build_recording_into(
3314 &self,
3315 name: &SourceName,
3316 config: &Value,
3317 secrets: &dyn SecretResolver,
3318 ledger: Arc<Accounting>,
3319 ) -> Result<Box<dyn TaskSource>, SourceError> {
3320 self.build_recording_with_clock(name, config, secrets, ledger, system_clock())
3321 }
3322
3323 fn build_recording_with_clock(
3324 &self,
3325 name: &SourceName,
3326 config: &Value,
3327 secrets: &dyn SecretResolver,
3328 ledger: Arc<Accounting>,
3329 clock: SharedClock,
3330 ) -> Result<Box<dyn TaskSource>, SourceError> {
3331 let config: GitHubProjectsConfig =
3332 serde_json::from_value(config.clone()).map_err(|e| SourceError::Config {
3333 message: format!("source {name}: {e}"),
3334 })?;
3335 let prefix = format!("source {name}: ");
3336 let mut source = GitHubProjectsSource::recording_into(name, config, secrets, ledger)
3337 .map_err(|error| match error {
3338 // The shared `StatusMapping::distinct` names the source itself.
3339 SourceError::Config { message } if message.starts_with(&prefix) => {
3340 SourceError::Config { message }
3341 }
3342 SourceError::Config { message } => SourceError::Config {
3343 message: format!("{prefix}{message}"),
3344 },
3345 SourceError::Auth { message } => SourceError::Auth {
3346 message: format!("source {name}: {message}"),
3347 },
3348 other => other,
3349 })?;
3350 source.clock = clock;
3351 Ok(Box::new(source))
3352 }
3353}
3354
3355/// Where a status category lands on this board, once configuration is resolved.
3356#[derive(Debug, Clone, PartialEq, Eq)]
3357enum StatusTarget {
3358 /// Not usable against this instance for this kind, and why.
3359 Disabled(UnmappedStatus),
3360 /// The board's `Status` option of this name.
3361 Column(ColumnName),
3362 /// A closed issue, with both its board option and the reason that says which closed it means.
3363 // 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.
3364 Terminal(ColumnName, ClosedState),
3365}
3366
3367/// Every status category, in the order the vocabulary declares them.
3368///
3369/// This list mirrors `StatusCategory`, so it carries its own drift gate rather than a
3370/// reviewer's attention: [`category_position`] is a wildcard-free match, so a variant
3371/// added to the shared vocabulary fails to compile until it is named there, and this
3372/// crate's suite reconciles this list against that enum's own derived schema, which is
3373/// generated from the variants rather than written beside them. The schema is what
3374/// catches a list left one short — a list checking only the positions it already holds
3375/// would pass while every mapping indexed by the new position panicked.
3376pub const CATEGORIES: [StatusCategory; 8] = [
3377 StatusCategory::Draft,
3378 StatusCategory::Backlog,
3379 StatusCategory::Todo,
3380 StatusCategory::Queued,
3381 StatusCategory::InProgress,
3382 StatusCategory::Done,
3383 StatusCategory::Cancelled,
3384 StatusCategory::Unknown,
3385];
3386
3387/// Where one category sits in [`CATEGORIES`]; see that list for what this pins.
3388#[must_use]
3389pub const fn category_position(category: StatusCategory) -> usize {
3390 match category {
3391 StatusCategory::Draft => 0,
3392 StatusCategory::Backlog => 1,
3393 StatusCategory::Todo => 2,
3394 StatusCategory::Queued => 3,
3395 StatusCategory::InProgress => 4,
3396 StatusCategory::Done => 5,
3397 StatusCategory::Cancelled => 6,
3398 StatusCategory::Unknown => 7,
3399 }
3400}
3401
3402/// The spelling a status category is configured and reported under.
3403fn category_name(category: StatusCategory) -> &'static str {
3404 match category {
3405 StatusCategory::Draft => "draft",
3406 StatusCategory::Backlog => "backlog",
3407 StatusCategory::Todo => "todo",
3408 StatusCategory::Queued => "queued",
3409 StatusCategory::InProgress => "in-progress",
3410 StatusCategory::Done => "done",
3411 StatusCategory::Cancelled => "cancelled",
3412 StatusCategory::Unknown => "unknown",
3413 }
3414}
3415
3416/// A shipped default's option name.
3417///
3418/// The literals below are this file's own and non-blank, and they are validated by the
3419/// one constructor a configured name goes through rather than beside it.
3420fn shipped_column(name: &'static str) -> ColumnName {
3421 ColumnName::try_from(name.to_owned()).expect("a shipped default names a board option")
3422}
3423
3424/// The shipped default for one category this instance's `status_mapping` does not mention,
3425/// for either kind.
3426fn shipped_default(category: StatusCategory) -> StatusTarget {
3427 match category {
3428 StatusCategory::Backlog => StatusTarget::Column(shipped_column("Backlog")),
3429 StatusCategory::Todo => StatusTarget::Column(shipped_column("Todo")),
3430 StatusCategory::Queued => StatusTarget::Column(shipped_column("Queued")),
3431 StatusCategory::InProgress => StatusTarget::Column(shipped_column("In Progress")),
3432 StatusCategory::Done => {
3433 StatusTarget::Terminal(shipped_column("Done"), ClosedState::Completed)
3434 }
3435 StatusCategory::Cancelled => {
3436 StatusTarget::Terminal(shipped_column("Cancelled"), ClosedState::NotPlanned)
3437 }
3438 StatusCategory::Draft | StatusCategory::Unknown => {
3439 StatusTarget::Disabled(UnmappedStatus::Unconfigured)
3440 }
3441 }
3442}
3443
3444/// The two kinds a status is written and read for, each with its own half of the mapping.
3445const STATUS_KINDS: [ItemKind; 2] = [ItemKind::Task, ItemKind::Project];
3446
3447/// This instance's complete category-to-target mapping for each kind, read in both
3448/// directions.
3449///
3450/// One target per category per kind, held at that category's own [`category_position`], so
3451/// a category missing from the mapping, named twice in it, or filed out of order is a state
3452/// this type cannot hold rather than one [`Self::target`] has to defend against. Both kinds'
3453/// targets are options of the board's one `Status` field.
3454#[derive(Debug, Clone)]
3455struct BoardStatuses {
3456 tasks: [StatusTarget; CATEGORIES.len()],
3457 projects: [StatusTarget; CATEGORIES.len()],
3458}
3459
3460impl BoardStatuses {
3461 /// Resolve `configured` against the shipped defaults, refusing two categories one kind
3462 /// would read back from one option.
3463 ///
3464 /// A category the mapping does not mention keeps its shipped default for both kinds; one
3465 /// it does mention is exactly what it configures, so a per-kind object leaves the kind it
3466 /// omits unmapped rather than defaulted.
3467 fn resolve(configured: &StatusMapping, instance: &SourceName) -> Result<Self, SourceError> {
3468 let resolve_kind =
3469 |kind: ItemKind| -> Result<[StatusTarget; CATEGORIES.len()], SourceError> {
3470 // `CATEGORIES[position] == category` for every category — the crate's suite
3471 // asserts it — so mapping the list in order fills each category's own slot.
3472 let mut targets = CATEGORIES.map(shipped_default);
3473 for (slot, category) in targets.iter_mut().zip(CATEGORIES) {
3474 if !configured.mentions(category) {
3475 continue;
3476 }
3477 *slot = match configured.name_for(category, kind) {
3478 Err(why) => StatusTarget::Disabled(why),
3479 Ok(name) => {
3480 let option = ColumnName::try_from(name.as_str().to_owned())
3481 .map_err(|message| SourceError::Config { message })?;
3482 match category {
3483 StatusCategory::Done => {
3484 StatusTarget::Terminal(option, ClosedState::Completed)
3485 }
3486 StatusCategory::Cancelled => {
3487 StatusTarget::Terminal(option, ClosedState::NotPlanned)
3488 }
3489 _ => StatusTarget::Column(option),
3490 }
3491 }
3492 };
3493 }
3494 StatusMapping::distinct(
3495 instance,
3496 kind,
3497 CATEGORIES
3498 .iter()
3499 .zip(&targets)
3500 .filter_map(|(category, target)| target.option().map(|o| (*category, o))),
3501 )?;
3502 Ok(targets)
3503 };
3504 Ok(Self {
3505 tasks: resolve_kind(ItemKind::Task)?,
3506 projects: resolve_kind(ItemKind::Project)?,
3507 })
3508 }
3509
3510 /// Every category's target for `kind`, in category order.
3511 const fn targets(&self, kind: ItemKind) -> &[StatusTarget; CATEGORIES.len()] {
3512 match kind {
3513 ItemKind::Task => &self.tasks,
3514 ItemKind::Project => &self.projects,
3515 }
3516 }
3517
3518 fn target(&self, kind: ItemKind, category: StatusCategory) -> &StatusTarget {
3519 &self.targets(kind)[category_position(category)]
3520 }
3521
3522 /// The category a board option name reports for `kind`, or `None` when nothing of that
3523 /// kind maps to it.
3524 fn category_of(&self, kind: ItemKind, option: &str) -> Option<StatusCategory> {
3525 CATEGORIES.into_iter().find(|category| {
3526 self.target(kind, *category)
3527 .option()
3528 .is_some_and(|name| name.eq_ignore_ascii_case(option))
3529 })
3530 }
3531
3532 /// Every option name either kind maps a category to, each once ignoring case, in
3533 /// category order with a task's name before a project's — what the guarded setup asks
3534 /// the `Status` field to hold.
3535 fn wanted(&self) -> Vec<String> {
3536 let mut wanted: Vec<String> = Vec::new();
3537 for category in CATEGORIES {
3538 for kind in STATUS_KINDS {
3539 if let Some(name) = self.target(kind, category).option()
3540 && !wanted.iter().any(|held| held.eq_ignore_ascii_case(name))
3541 {
3542 wanted.push(name.to_owned());
3543 }
3544 }
3545 }
3546 wanted
3547 }
3548
3549 /// The status an item of `kind` reports, from the three things a read of it says: its
3550 /// board `Status` option, whether its issue is closed, and the reason it was closed with.
3551 ///
3552 /// **A closed issue's category follows its closure**, by [`closed_category`]: completed
3553 /// or no reason reads `done`, not planned and duplicate read `cancelled`, and any other
3554 /// reason reads `unknown`. `REOPENED`-while-closed is a state this source can never
3555 /// produce, so it is read permissively rather than refused — reads are faithful, and
3556 /// refusals belong on writes.
3557 ///
3558 /// **Its name follows the closure too, unless the option agrees with it.** The option's
3559 /// own name is kept when this kind's mapping reads that option as the same category, or
3560 /// does not name it at all — so a closed issue in a "Shipped" column reports `done`
3561 /// named `Shipped`. An option the mapping reads as another category contradicts the
3562 /// closure, and so does no option at all: the name is then the option the mapping gives
3563 /// the closure's category, so an issue closed as completed at `Todo` reads `done` named
3564 /// by the board's `done` option, and a destination reading `Todo` as todo is never handed
3565 /// it. An `unknown` closure keeps the option, or `Closed` without one.
3566 ///
3567 /// An open item's option reads through its own kind's mapping, and an option that mapping
3568 /// does not name reads as `Unknown` under its own name.
3569 ///
3570 /// One function of those three rather than of a response, so a narrow status write can
3571 /// answer what a re-read would report by applying it to the state it has just written.
3572 fn status(
3573 &self,
3574 kind: ItemKind,
3575 option: Option<&str>,
3576 closed: bool,
3577 reason: Option<&str>,
3578 ) -> Status {
3579 if closed {
3580 let category = closed_category(reason);
3581 let agrees = option.filter(|option| {
3582 category == StatusCategory::Unknown
3583 || self
3584 .category_of(kind, option)
3585 .is_none_or(|read| read == category)
3586 });
3587 let name = agrees.or_else(|| self.target(kind, category).option());
3588 let fallback = match category {
3589 StatusCategory::Done => "Done",
3590 StatusCategory::Cancelled => "Cancelled",
3591 _ => "Closed",
3592 };
3593 return Status {
3594 category,
3595 name: name.or(option).unwrap_or(fallback).to_owned(),
3596 };
3597 }
3598 let name = option.unwrap_or("Open").to_owned();
3599 Status {
3600 category: self
3601 .category_of(kind, &name)
3602 .unwrap_or(StatusCategory::Unknown),
3603 name,
3604 }
3605 }
3606
3607 /// The option a closed item's `Status` has to hold for its card to agree with its
3608 /// closure, and the category that closure reads as — or `None` when the item is open,
3609 /// its closure reads `unknown`, or its option already agrees.
3610 ///
3611 /// What `task status reconcile` writes: only `done` and `cancelled` closures are brought
3612 /// in line, each to the one option the mapping gives its category — an item with no
3613 /// option, or at one the mapping does not name, included.
3614 fn contradicted(
3615 &self,
3616 kind: ItemKind,
3617 option: Option<&str>,
3618 closed: bool,
3619 reason: Option<&str>,
3620 ) -> Option<(ClosedAs, &str)> {
3621 if !closed {
3622 return None;
3623 }
3624 let closed_as = match closed_category(reason) {
3625 StatusCategory::Done => ClosedAs::Done,
3626 StatusCategory::Cancelled => ClosedAs::Cancelled,
3627 _ => return None,
3628 };
3629 let wanted = self.target(kind, closed_as.category()).option()?;
3630 if option.is_some_and(|option| option.eq_ignore_ascii_case(wanted)) {
3631 return None;
3632 }
3633 Some((closed_as, wanted))
3634 }
3635}
3636
3637/// The category a closed issue reads as, from the reason it was closed with.
3638///
3639/// `COMPLETED`, and no reason at all, is finished work; `NOT_PLANNED` and `DUPLICATE` are both
3640/// work nobody will do here — a duplicate's work is the other issue's — and any other reason
3641/// is one this source cannot read a category from.
3642fn closed_category(reason: Option<&str>) -> StatusCategory {
3643 match reason {
3644 None | Some("COMPLETED") => StatusCategory::Done,
3645 Some("NOT_PLANNED" | "DUPLICATE") => StatusCategory::Cancelled,
3646 Some(_) => StatusCategory::Unknown,
3647 }
3648}
3649
3650impl BoardStatuses {
3651 /// For each kind, the option names it maps a category to that `existing` lacks, ignoring
3652 /// case; a kind lacking none is left out.
3653 fn missing_by_kind(&self, existing: &[StatusOption]) -> Vec<KindMissing> {
3654 STATUS_KINDS
3655 .into_iter()
3656 .filter_map(|kind| {
3657 let missing: Vec<String> = self
3658 .targets(kind)
3659 .iter()
3660 .filter_map(StatusTarget::option)
3661 .filter(|wanted| {
3662 !existing
3663 .iter()
3664 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3665 })
3666 .map(str::to_owned)
3667 .collect();
3668 (!missing.is_empty()).then_some(KindMissing { kind, missing })
3669 })
3670 .collect()
3671 }
3672}
3673
3674impl StatusTarget {
3675 /// The board option this target selects, or `None` for an unmapped one.
3676 fn option(&self) -> Option<&str> {
3677 match self {
3678 Self::Column(name) | Self::Terminal(name, _) => Some(name.as_str()),
3679 Self::Disabled(_) => None,
3680 }
3681 }
3682}
3683
3684// 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.
3685/// One repository this source can create an issue in, as `owner/name`.
3686///
3687/// Every `createIssue` this source sends names one of these: the item's own single
3688/// `repositories` entry — on a source declared private, only when it reads private — else
3689/// its parent project issue's repository, else the configured
3690/// [`GitHubProjectsConfig::repository`]. [`GitHubProjectsSource::creation_target`] makes
3691/// that choice and says what it refuses before `createIssue`.
3692// llmlint: ignore-end[comments_earn_their_place, contracts_have_one_source_or_a_drift_gate]
3693#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
3694#[serde(try_from = "String", into = "String")]
3695struct RepositoryTarget {
3696 owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
3697 name: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
3698}
3699
3700impl RepositoryTarget {
3701 fn parse(value: &str) -> Result<Self, SourceError> {
3702 let (owner, name) = value.split_once('/').ok_or_else(|| SourceError::Config {
3703 message: format!(
3704 "repository must be spelled owner/name; {value:?} names no repository"
3705 ),
3706 })?;
3707 if !valid_github_owner(owner) || !valid_github_repository_name(name) {
3708 return Err(SourceError::Config {
3709 message: format!(
3710 "repository must be spelled owner/name with a GitHub login and one \
3711 repository name; {value:?} is not"
3712 ),
3713 });
3714 }
3715 Ok(Self {
3716 owner: owner.to_owned(),
3717 name: name.to_owned(),
3718 })
3719 }
3720
3721 /// The one host whose repositories this source creates issues in, spelled once: it is
3722 /// what [`Self::origin`] renders and what [`Self::from_origin`] accepts.
3723 const HOST: &str = "github.com";
3724
3725 fn origin(&self) -> String {
3726 format!("{}/{}/{}", Self::HOST, self.owner, self.name)
3727 }
3728
3729 /// The repository a normalized origin names, or why it is none this source can create
3730 /// an issue in: another host, or more or fewer than `owner/name` under this one.
3731 fn from_origin(origin: &Repository) -> Result<Self, String> {
3732 let not_here = || {
3733 format!(
3734 "{} is not a {}/owner/name repository",
3735 origin.as_str(),
3736 Self::HOST
3737 )
3738 };
3739 let (host, rest) = origin.as_str().split_once('/').ok_or_else(not_here)?;
3740 if host != Self::HOST {
3741 return Err(not_here());
3742 }
3743 Self::parse(rest).map_err(|_| not_here())
3744 }
3745
3746 fn slug(&self) -> String {
3747 format!("{}/{}", self.owner, self.name)
3748 }
3749}
3750
3751/// Read back from a kept context as the `owner/name` it was written as, refused unless
3752/// [`RepositoryTarget::parse`] takes it.
3753impl TryFrom<String> for RepositoryTarget {
3754 type Error = String;
3755
3756 fn try_from(slug: String) -> Result<Self, Self::Error> {
3757 Self::parse(&slug).map_err(|error| error.to_string())
3758 }
3759}
3760
3761impl From<RepositoryTarget> for String {
3762 fn from(target: RepositoryTarget) -> Self {
3763 target.slug()
3764 }
3765}
3766
3767/// A source which reads GitHub afresh for every operation.
3768pub struct GitHubProjectsSource {
3769 /// This source's configured name, used both to tell a far end naming this source
3770 /// from one naming a system it knows nothing about, and to name the instance a
3771 /// status refusal is about.
3772 name: SourceName,
3773 owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after full GitHub-owner validation.
3774 project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after GraphQL-Int validation.
3775 repository: Option<RepositoryTarget>,
3776 endpoint: Url,
3777 token: SecretString,
3778 credential_name: String, // llmlint: ignore[invalid_states_unrepresentable] Private diagnostic value constructed only after environment-name validation.
3779 statuses: BoardStatuses,
3780 /// Where each priority lands on this board, or `None` when this instance holds none.
3781 priorities: Option<PriorityMapping>,
3782 /// The metadata values this instance projects onto board text fields, in configured order.
3783 metadata_fields: Vec<MetadataField>,
3784 /// Built on the first request rather than with the source, as [`Self::client`] says.
3785 client: std::sync::OnceLock<Client>,
3786 /// Built on the first attachment request, as [`Self::asset_client`] says.
3787 asset_client: std::sync::OnceLock<Client>,
3788 /// Every item this source has created in this command, in the order it created them —
3789 /// dropped by [`TaskSource::end_command`].
3790 ///
3791 /// GitHub's `projectV2.items` is eventually consistent: an issue added to a board with
3792 /// `addProjectV2ItemById` is routinely absent from the very next read of that board, so
3793 /// a copy resolving a dependency on an item it had just created refused it as not
3794 /// found. A board read is completed from this — an item remembered here and absent from
3795 /// the read is added back, because the board really does hold it and only the read is
3796 /// behind.
3797 ///
3798 /// It is not a cache of a user's work: nothing is remembered that this process did not
3799 /// itself just write, it lives and dies with the process, and it is never consulted for
3800 /// an item this source did not create.
3801 created: Mutex<Vec<Resolved>>,
3802 /// Every item that already existed and that this source has written in this command, as
3803 /// it wrote it — dropped by [`TaskSource::end_command`].
3804 ///
3805 /// The other half of [`Self::created`], held on the same terms and for the reason a
3806 /// narrowed read needs it: an answer from GitHub's search or from the board's own field
3807 /// filter is an index behind a write this process made moments ago, so a query matching
3808 /// what this process just wrote onto an existing item would otherwise miss it. Nothing
3809 /// is remembered that this process did not itself just write.
3810 updated: Mutex<Vec<Resolved>>,
3811 /// Every issue this source has added a comment to or edited a comment of in this command
3812 /// — dropped by [`TaskSource::end_command`].
3813 ///
3814 /// A comment-activity read is narrowed by GitHub's issue search, whose `updated:` index
3815 /// lags the write that moved an issue's `updatedAt`, and neither [`Self::created`] nor
3816 /// [`Self::updated`] is moved by a comment, so an issue this process had just commented
3817 /// on was missing from such a read — or ruled out by the `updatedAt` its own record held
3818 /// from before — until the index caught up. Each id here is a candidate of every such
3819 /// search-narrowed read, and wherever it is a candidate its comments are read rather than
3820 /// it being ruled out by a stale `updatedAt`; that read is of the issue's own node, so it
3821 /// is current. It holds ids alone: nothing of a comment is remembered. A comment another
3822 /// process wrote is still found only once the index has it.
3823 commented: Mutex<Vec<NativeId>>,
3824 /// How fast this source writes, and how long it waits out a refusal.
3825 pacing: Pacing,
3826 /// When the last content-creating mutation finished, or the moment the furthest-out
3827 /// reserved slot releases the next one, whichever is later — so the one after it can be
3828 /// spaced from that, when `pacing.min_mutation_interval_ms` asks for a spacing. See
3829 /// [`GitHubProjectsSource::paced`] for why completion rather than release is what it is
3830 /// measured from.
3831 last_mutation: Mutex<Option<Duration>>,
3832 /// GitHub's secondary windows as this source has charged them; see the `pacing` module.
3833 windows: Mutex<pacing::Windows>,
3834 /// The tasks a project holds, as a copy's batched read of the project and every one of them
3835 /// found them — see [`TaskSource::read_ahead_for_copy`] — for the length of one command, so
3836 /// a listing of that project's tasks is answered without a read of its own.
3837 task_children: Mutex<BTreeMap<NativeId, Vec<Resolved>>>,
3838 /// The web address of each issue this source created in the current command, as its
3839 /// `createIssue` answered it — what [`TaskSource::written_location`] answers, without a
3840 /// request.
3841 written: Mutex<BTreeMap<NativeId, String>>,
3842 /// What a copy has held back of the items it created, between
3843 /// [`TaskSource::begin_deferred_writes`] and [`TaskSource::finish_writes`] — `None` while
3844 /// nothing is being held back, when every write lands whole as it is made.
3845 deferred: Mutex<Option<Deferred>>,
3846 /// Each existing issue's update held back while writes are, in the order it was made: sent
3847 /// together by [`Self::send_held_updates`] before any other request goes out, so nothing
3848 /// this source sends or reads ever sees the board without them.
3849 held_updates: Mutex<Vec<HeldUpdate>>,
3850 clock: SharedClock,
3851 /// Whether the configuration declares this source private, as
3852 /// [`TaskSource::declare_visibility`] told it: what decides where a new issue whose one
3853 /// named repository reads public is created — see [`Self::creation_rule`].
3854 declared_private: std::sync::atomic::AtomicBool,
3855 numeric_repositories: tokio::sync::Mutex<BTreeMap<RepositoryTarget, std::num::NonZeroU64>>,
3856 /// The board as this process last read it, for the length of one command — dropped by
3857 /// [`TaskSource::end_command`].
3858 ///
3859 /// A copy of a project used to re-read the whole board, paged, before writing each of
3860 /// its items, which is by far the largest part of a copy's request count and none of
3861 /// its work. Nothing else changes this board while a command runs — this source's own
3862 /// writes are the only writer — so one read answers them all.
3863 ///
3864 /// It is not a store of a user's work and it is not the cache the no-persistence
3865 /// invariant forbids: it lives and dies with the process exactly as `created` does,
3866 /// nothing is written down, and [`Self::board`] still completes it from `created`, so
3867 /// an item this command created and then depends on resolves whether or not GitHub's
3868 /// own eventually-consistent read has caught up. A write to an item already on the
3869 /// board updates the entry here too, so what this holds is the last read plus this
3870 /// process's own writes rather than a snapshot taken before them.
3871 board_cache: Mutex<Option<Board>>,
3872 /// Every issue this board's own search reported, for the length of one command — dropped
3873 /// by [`TaskSource::end_command`].
3874 ///
3875 /// The second half of a board read, and cached for the same reason and on the same
3876 /// terms as the first: it lives and dies with the process, nothing is written down, and
3877 /// a write this process makes updates the entry here exactly as it updates the one in
3878 /// [`Self::board_cache`]. One read answers every question a command asks, so a command
3879 /// that lists this board's projects and its tasks pays for one search rather than two.
3880 search_cache: Mutex<Option<Vec<Resolved>>>,
3881 /// What each narrowed question GitHub was asked answered, keyed by that question, for
3882 /// the length of one command — dropped by [`TaskSource::end_command`].
3883 ///
3884 /// The narrowed counterpart of [`Self::search_cache`], held on the same terms: it lives
3885 /// and dies with the process, nothing is written down, a write this process makes
3886 /// updates the entry here as it updates the other two, and every answer is completed
3887 /// with this process's own writes each time it is given. A command that asks the same
3888 /// narrowed question twice — a wait polling for its own items, a listing repeated after a
3889 /// write — pays for it once, which is what the whole-board read it replaced gave it.
3890 narrowed_cache: Mutex<BTreeMap<String, Vec<Resolved>>>,
3891 search_next: Mutex<BTreeMap<String, Option<String>>>,
3892 /// Records already resolved in this command, reused by writes and for comment identity.
3893 /// Explicit item reads still reach GitHub. Nothing is persisted, and
3894 /// [`TaskSource::end_command`] drops every record, so a write in the next command reads
3895 /// its item as a person has since left it.
3896 resolved_cache: Mutex<BTreeMap<NativeId, Resolved>>,
3897 /// What [`TaskSource::read_ahead_for_copy`] read, by id — `None` for an id that read
3898 /// answered as holding nothing — each taken by the next read of that item by id and by
3899 /// nothing else, dropped by any mutation naming it, and cleared by
3900 /// [`TaskSource::end_command`]. So a read answered from it is the copy's own
3901 /// read-before-write, made in the same request as the others' rather than on its own.
3902 read_ahead: Mutex<BTreeMap<NativeId, Option<Resolved>>>,
3903 /// The origins [`TaskSource::read_ahead_origins_for_copy`] was asked about and has not sent
3904 /// yet: held for the [`TaskSource::read_ahead_for_copy`] that follows it, which sends them —
3905 /// beside the members of the one project it names, when it names one — or for the next
3906 /// request this source makes, whichever comes first.
3907 pending_origins: Mutex<Vec<String>>,
3908 /// Each project's members as a lookup carrying them read them in this command, trimmed to
3909 /// their classification and repositories, taken by [`TaskSource::member_classifications`]
3910 /// of that project and by nothing else, and cleared by [`TaskSource::end_command`].
3911 member_classes: Mutex<BTreeMap<NativeId, Vec<MemberClass>>>,
3912 /// Every item a listing of one project's members carried whole in this command — the
3913 /// project issue and each of its sub-issues — kept for [`TaskSource::read_ahead_for_copy`]
3914 /// alone, which takes each one it is asked for into [`Self::read_ahead`] instead of reading
3915 /// it again. No other read is answered from it, so a read by id outside a copy's
3916 /// read-before-write reads GitHub as it always did. Dropped by any mutation naming an
3917 /// item, and cleared by [`TaskSource::end_command`].
3918 carried: Mutex<BTreeMap<NativeId, Resolved>>,
3919 /// Items an issue read by reference reached, held for the one status write each was read
3920 /// for and taken out by it alone — see [`Self::take_held`] — because that read selects only
3921 /// what such a write needs. Dropped by [`TaskSource::end_command`].
3922 held_cache: Mutex<BTreeMap<NativeId, Resolved>>,
3923 /// Each project's sub-issues as GitHub answered them, keyed by the selector they were
3924 /// asked for under, with the project that selector named — for the length of one command,
3925 /// dropped by [`TaskSource::end_command`].
3926 ///
3927 /// A caller pages through a project's tasks one engine page at a time, and every page
3928 /// is cut from the whole list of them, so without this each page walked every
3929 /// [`graphql::SUB_ISSUES`] page again and a project of `n` listing pages cost `n²`
3930 /// requests to read once. Held on the terms [`Self::narrowed_cache`] is: it lives and
3931 /// dies with the process, nothing is written down, a write this process makes updates or
3932 /// removes the entry here as it does there, and every answer is completed with this
3933 /// process's own writes each time it is given.
3934 children_cache: Mutex<ProjectChildren>,
3935 /// The board's own id and field definitions as this process last read them on their
3936 /// own, for the length of one command — dropped by [`TaskSource::end_command`].
3937 ///
3938 /// What a write needs of the board and its item does not say, read once per command
3939 /// rather than once per item written, on the terms [`Self::board_cache`] is held on: it
3940 /// lives and dies with the process and nothing is written down. It holds no item and so
3941 /// can answer no question about one — see [`Self::board_fields`].
3942 fields_cache: Mutex<Option<BoardFields>>,
3943 /// Each destination repository's node id, resolved once per repository
3944 /// rather than per issue created.
3945 ///
3946 /// A repository's node id does not change, and re-reading it for every issue of a copy
3947 /// spent one request per item on an answer this source already had. It is a map rather
3948 /// than one entry because a copy files each item in the repository its own
3949 /// `repositories` field names, so a plan across five repositories asks GitHub five
3950 /// times and not once per item.
3951 repository_cache: Mutex<BTreeMap<RepositoryTarget, String>>,
3952 /// What every request this source sends is recorded into.
3953 ///
3954 /// Ordinary code path, not a mode: [`Self::send_once`] records into it at the one place
3955 /// a request leaves this crate, so nothing has to be switched on for a session to be
3956 /// counted. It is shared rather than owned so a caller accounting for a whole session —
3957 /// its own schema verification, board lookups, residue sweep and cleanup beside this
3958 /// source's reads and writes — adds up one accounting instead of two. See
3959 /// [`accounting`] for what a record carries and what a session's spend is and is not.
3960 ledger: Arc<Accounting>,
3961 /// What [`expected`] holds for a write the store's cache expects: the journal while one is
3962 /// open, the issues primed from the cache, and the comments known on each.
3963 expected: expected::Held,
3964 /// What [`projection`] holds for a producer's projection: a kept placement awaiting its
3965 /// setup-context token, and where the last create was placed.
3966 projection: projection::Held,
3967}
3968
3969/// The board item a single field update's answer names, refused unless it is `item_id`.
3970fn field_update_item<'a>(data: &'a Value, item_id: &str) -> Result<&'a Value, SourceError> {
3971 let returned = data
3972 .pointer("/updateProjectV2ItemFieldValue/projectV2Item")
3973 .ok_or_else(|| SourceError::Malformed {
3974 message: "GitHub field update returned no project item".into(),
3975 })?;
3976 if required_str(returned, "id")? != item_id {
3977 return Err(SourceError::Malformed {
3978 message: "GitHub field update returned the wrong project item".into(),
3979 });
3980 }
3981 Ok(returned)
3982}
3983
3984/// GitHub's closed single-select color vocabulary.
3985#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3986#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
3987pub enum StatusOptionColor {
3988 /// Gray.
3989 Gray,
3990 /// Blue.
3991 Blue,
3992 /// Green.
3993 Green,
3994 /// Yellow.
3995 Yellow,
3996 /// Purple.
3997 Purple,
3998 /// Red.
3999 Red,
4000 /// Orange.
4001 Orange,
4002 /// Pink.
4003 Pink,
4004}
4005
4006/// Whether a guarded board setup — of the fields, or of the Status options alone — plans or
4007/// applies its additions.
4008#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4009pub enum SetupMode {
4010 /// Read without mutation.
4011 Plan,
4012 /// Apply and verify.
4013 Apply,
4014}
4015
4016/// The name [`SetupMode`] had when Status was the one field set up, kept so a caller written
4017/// against it goes on compiling.
4018pub type StatusOptionsMode = SetupMode;
4019
4020/// The explicit result of the requested operation.
4021#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4022#[serde(rename_all = "kebab-case")]
4023pub enum StatusOptionsOutcome {
4024 /// A read-only plan.
4025 Planned,
4026 /// Apply found nothing missing.
4027 Unchanged,
4028 /// Additions were applied and verified.
4029 Applied,
4030}
4031
4032/// A GitHub single-select option's opaque GraphQL node identifier.
4033#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4034#[serde(transparent)]
4035pub struct StatusOptionId(#[schemars(length(min = 1))] String);
4036
4037impl TryFrom<String> for StatusOptionId {
4038 type Error = String;
4039
4040 fn try_from(id: String) -> Result<Self, Self::Error> {
4041 if id.trim().is_empty() {
4042 return Err("a GitHub Status option id cannot be blank".to_owned());
4043 }
4044 Ok(Self(id))
4045 }
4046}
4047
4048/// One existing or proposed option in a guarded Status-field update.
4049#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4050pub struct StatusOption {
4051 /// GitHub's stable id.
4052 pub id: StatusOptionId,
4053 /// The visible option name.
4054 pub name: ColumnName,
4055 /// GitHub's single-select color token.
4056 pub color: StatusOptionColor,
4057 /// The option description, including an empty one.
4058 pub description: String,
4059}
4060
4061/// One board item's Status assignment, retained as recovery data.
4062#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4063pub struct StatusAssignment {
4064 /// The project item id whose assignment this is.
4065 // llmlint: ignore[invalid_states_unrepresentable] This opaque GraphQL node ID is
4066 // carried verbatim as operator recovery data; introducing a semantic type would claim
4067 // validation rules GitHub does not publish and no operation here interprets.
4068 pub item_id: String,
4069 /// The selected option, absent when the item has no status.
4070 #[serde(skip_serializing_if = "Option::is_none")]
4071 pub option: Option<AssignedStatusOption>,
4072}
4073
4074/// The inseparable id and name of an assigned option.
4075#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4076pub struct AssignedStatusOption {
4077 /// GitHub's stable id.
4078 pub id: StatusOptionId,
4079 /// The visible name.
4080 pub name: ColumnName,
4081}
4082
4083/// The plan and verified outcome of reconciling configured Status options.
4084#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4085pub struct StatusOptionsReport {
4086 /// The configured source name.
4087 pub source: SourceName,
4088 /// Configured option names absent before the operation.
4089 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a
4090 // `ColumnName` and has therefore already passed its nonblank validation; retaining the
4091 // serialized string here preserves the report's intentionally simple public contract.
4092 pub missing: Vec<String>,
4093 /// What the requested operation did.
4094 pub outcome: StatusOptionsOutcome,
4095 /// The complete option list observed before any mutation.
4096 pub existing: Vec<StatusOption>,
4097}
4098
4099#[derive(Debug, Clone, PartialEq, Eq)]
4100struct StatusSnapshot {
4101 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4102 // passed back as the mutation's project identity; a newtype could enforce no stronger
4103 // invariant because GitHub publishes no grammar for it.
4104 board_id: String,
4105 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4106 // passed back as the mutation's field identity; a newtype could enforce no stronger
4107 // invariant because GitHub publishes no grammar for it.
4108 field_id: String,
4109 options: Vec<StatusOption>,
4110 assignments: Vec<StatusAssignment>,
4111}
4112
4113/// The name of the board field a status is held in.
4114const STATUS_FIELD: &str = "Status";
4115
4116/// Every item's value of each field `report` names, as it stood before the setup wrote
4117/// anything — what a person puts back when the setup is refused part way.
4118fn recovery(report: &FieldsReport, before: &BoardSnapshot) -> Result<String, SourceError> {
4119 let assignments: BTreeMap<&str, Vec<StatusAssignment>> = report
4120 .fields
4121 .iter()
4122 .map(|field| (field.field.name(), before.assignments(field.field)))
4123 .collect();
4124 serde_json::to_string_pretty(&assignments).map_err(|error| SourceError::Malformed {
4125 message: format!("cannot render the pre-write field recovery snapshot: {error}"),
4126 })
4127}
4128
4129/// One board field the guarded setup reads and writes — every one it reads, and the only
4130/// ones it writes.
4131#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, schemars::JsonSchema)]
4132pub enum BoardField {
4133 /// The single-select `Status` field every instance's `status_mapping` resolves into.
4134 Status,
4135 /// The single-select `Priority` field an instance's `priority_mapping` resolves into.
4136 Priority,
4137}
4138
4139impl BoardField {
4140 /// The field's name on the board.
4141 #[must_use]
4142 pub const fn name(self) -> &'static str {
4143 match self {
4144 Self::Status => STATUS_FIELD,
4145 Self::Priority => PRIORITY_FIELD,
4146 }
4147 }
4148
4149 /// The field a board calls `name`, or `None` for one this setup does not own.
4150 fn named(name: &str) -> Option<Self> {
4151 [Self::Status, Self::Priority]
4152 .into_iter()
4153 .find(|field| field.name() == name)
4154 }
4155}
4156
4157/// What the guarded setup did to one field.
4158#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4159#[serde(rename_all = "kebab-case")]
4160pub enum FieldOutcome {
4161 /// A read-only plan.
4162 Planned,
4163 /// Apply found the field there with every configured option.
4164 Unchanged,
4165 /// Missing options were added to the field that was there, and verified.
4166 Applied,
4167 /// The field was not there; it was created holding the configured options, and verified.
4168 Created,
4169}
4170
4171/// One field's plan, or its verified outcome.
4172#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4173pub struct FieldReport {
4174 /// Which field.
4175 pub field: BoardField,
4176 /// Whether the board had the field before the operation.
4177 // llmlint: ignore[invalid_states_unrepresentable] `exists` beside `outcome` is the report's
4178 // wire shape as its consumer's contract fixes it — `{"field", "exists", "missing",
4179 // "outcome", "existing"}` — so folding one into the other would change a published JSON
4180 // shape. The contradictory pairings cannot be built: `GitHubProjectsSource::fields` is the
4181 // one constructor, and it derives `outcome` from `exists` in one match.
4182 pub exists: bool,
4183 /// Configured option names the field lacked before the operation — every one of them,
4184 /// in the order a new field lists them, when the field was not there at all.
4185 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
4186 // mapping name and has therefore already passed its nonblank validation; the serialized
4187 // string is the report's intentionally simple public contract, as `StatusOptionsReport`'s is.
4188 pub missing: Vec<String>,
4189 /// For the `Status` field, which item kind each missing name is configured for: one
4190 /// entry per kind `status_mapping` names a missing option for, task before project, each
4191 /// listing that kind's missing names in category order. A name both kinds use is in
4192 /// both. Empty — and left out of the JSON — when nothing is missing, and always for
4193 /// `Priority`, which only a task holds.
4194 #[serde(default, skip_serializing_if = "Vec::is_empty")]
4195 // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
4196 // both SDKs model an absent `kinds` as an empty list rather than as `null`.
4197 #[schemars(!skip_serializing_if)]
4198 pub kinds: Vec<KindMissing>,
4199 /// What the requested operation did.
4200 pub outcome: FieldOutcome,
4201 /// The field's complete option list observed before any mutation; empty when the field
4202 /// was not there.
4203 pub existing: Vec<StatusOption>,
4204}
4205
4206/// The `Status` option names one item kind's `status_mapping` names that the field lacked.
4207#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4208pub struct KindMissing {
4209 /// The kind these names are configured for.
4210 pub kind: ItemKind,
4211 /// The names that kind maps a category to and the field lacked, in category order.
4212 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
4213 // mapping name, as `FieldReport::missing`'s do, and the serialized string is the report's
4214 // intentionally simple public contract.
4215 pub missing: Vec<String>,
4216}
4217
4218/// The plan and verified outcome of setting up every field a source's configuration names.
4219#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4220pub struct FieldsReport {
4221 /// The configured source name.
4222 pub source: SourceName,
4223 /// `Status`, always, and `Priority` when the source sets `priority_mapping`.
4224 // llmlint: ignore[invalid_states_unrepresentable] A list is the report's wire shape as its
4225 // consumer's contract fixes it — `{"source", "fields": [...]}` — so a struct with one member
4226 // per field would change a published JSON shape. The states the list could hold and the
4227 // contract forbids cannot be built: `GitHubProjectsSource::fields` is the one constructor,
4228 // and it pushes `Status` first and exactly once, then `Priority` exactly when configured.
4229 pub fields: Vec<FieldReport>,
4230 /// Each board text field the source's `metadata_fields` projects a value onto, in
4231 /// configured order. Empty — and left out of the JSON — when it projects none.
4232 #[serde(default, skip_serializing_if = "Vec::is_empty")]
4233 // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
4234 // both SDKs model an absent list as an empty one rather than as `null`.
4235 #[schemars(!skip_serializing_if)]
4236 pub metadata_fields: Vec<MetadataFieldReport>,
4237}
4238
4239/// The type of a board field that is not a text field, as GitHub names it — its `dataType`, or
4240/// its GraphQL type when it has none — which is what a projected field's conflict reports.
4241///
4242/// Never empty, never beginning with whitespace, and never `TEXT`: a text field of that name is
4243/// no conflict.
4244// The schema states that rule as `NON_TEXT_FIELD_TYPE_PATTERN`, so a generated SDK model
4245// refuses exactly what `try_from` below does.
4246#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4247#[serde(transparent)]
4248pub struct NonTextFieldType(#[schemars(regex(pattern = NON_TEXT_FIELD_TYPE_PATTERN))] String);
4249
4250/// What a [`NonTextFieldType`] may be, as the emitted schema states it: a first character that
4251/// is not whitespace, and anything but exactly `TEXT` — spelled without a lookahead, which
4252/// neither SDK's validator can be relied on to support.
4253const NON_TEXT_FIELD_TYPE_PATTERN: &str =
4254 r"^(?:[^T\s]|T(?:[^E]|$)|TE(?:[^X]|$)|TEX(?:[^T]|$)|TEXT[\s\S])";
4255
4256impl TryFrom<String> for NonTextFieldType {
4257 type Error = String;
4258
4259 fn try_from(kind: String) -> Result<Self, Self::Error> {
4260 if kind.is_empty() || kind.starts_with(char::is_whitespace) {
4261 return Err("a board field's type cannot be blank or begin with whitespace".to_owned());
4262 }
4263 if kind == "TEXT" {
4264 return Err("a TEXT field is no conflict for a projected text field".to_owned());
4265 }
4266 Ok(Self(kind))
4267 }
4268}
4269
4270impl std::fmt::Display for NonTextFieldType {
4271 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4272 formatter.write_str(&self.0)
4273 }
4274}
4275
4276/// One projected metadata text field's plan, or its verified outcome.
4277///
4278/// The setup creates a missing one as a text field and never alters a field that is there:
4279/// a same-named field of another type is reported in `conflict` and left as it is.
4280#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4281pub struct MetadataFieldReport {
4282 /// The board field's name.
4283 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.
4284 /// The metadata key the value is read from.
4285 pub key: String, // llmlint: ignore[invalid_states_unrepresentable] As `field`: a validated configuration value reported back as written.
4286 /// The object keys walked inside that key's value; empty for the key's own value.
4287 pub path: Vec<String>,
4288 /// Whether the board had a text field of that name before the operation.
4289 // 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.
4290 pub exists: bool,
4291 /// The type of a field of that name that is not a text field, which the setup leaves as
4292 /// it is; absent — and left out of the JSON — when there is none.
4293 #[serde(default, skip_serializing_if = "Option::is_none")]
4294 pub conflict: Option<NonTextFieldType>,
4295 /// What the requested operation did: `planned`; `created` for a field that was not
4296 /// there; `unchanged` for one that was, and for a conflict, which is never changed.
4297 pub outcome: FieldOutcome,
4298}
4299
4300/// Which options one field is configured with, in the order a new field would list them.
4301struct FieldPlan {
4302 field: BoardField,
4303 wanted: Vec<String>,
4304}
4305
4306/// One single-select field as the guarded setup snapshots it.
4307#[derive(Debug, Clone, PartialEq, Eq)]
4308struct SnapshotField {
4309 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4310 // passed back as the mutation's field identity; a newtype could enforce no stronger
4311 // invariant because GitHub publishes no grammar for it.
4312 field_id: String,
4313 options: Vec<StatusOption>,
4314}
4315
4316/// Every single-select field of a board and every item's value of each.
4317#[derive(Debug, Clone, PartialEq, Eq)]
4318struct BoardSnapshot {
4319 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4320 // passed back as the mutation's project identity; a newtype could enforce no stronger
4321 // invariant because GitHub publishes no grammar for it.
4322 board_id: String,
4323 fields: BTreeMap<BoardField, SnapshotField>,
4324 /// Each board item's id, and its value of each field this setup owns that it holds one of.
4325 items: Vec<(String, BTreeMap<BoardField, AssignedStatusOption>)>,
4326}
4327
4328impl BoardSnapshot {
4329 /// Every item's value of `field`, in board order — the recovery data a drift refusal
4330 /// carries.
4331 fn assignments(&self, field: BoardField) -> Vec<StatusAssignment> {
4332 self.items
4333 .iter()
4334 .map(|(item_id, values)| StatusAssignment {
4335 item_id: item_id.clone(),
4336 option: values.get(&field).cloned(),
4337 })
4338 .collect()
4339 }
4340}
4341
4342impl GitHubProjectsSource {
4343 /// Report missing configured Status options and, when `apply` is true, add them with
4344 /// a whole-list mutation that preserves every existing id and verifies the result.
4345 ///
4346 /// # Errors
4347 ///
4348 /// Refuses a board without a single-select `Status` field. A post-write difference in
4349 /// any pre-existing option id or item assignment is refused with the complete pre-write
4350 /// assignment snapshot in the diagnostic for recovery.
4351 // llmlint: ignore[changed_behavior_has_e2e] The CLI journeys cover plan, no-op apply,
4352 // successful mutation, both drift refusals, source selection, missing Status, casing,
4353 // and paging. Transport errors remain the shared `graphql` boundary's behavior rather
4354 // than a new status-options behavior, and the pinned-schema test prevents valid GitHub
4355 // responses from entering the defensive malformed-response branches below.
4356 pub async fn status_options(
4357 &self,
4358 mode: StatusOptionsMode,
4359 ) -> Result<StatusOptionsReport, SourceError> {
4360 let before = self.status_snapshot().await?;
4361 // A terminal category's option is as configured as an open one's: a terminal
4362 // write validates it before closing and refuses when the board lacks it. Both
4363 // kinds' names are options of the one field, so both are asked for.
4364 let missing = self
4365 .statuses
4366 .wanted()
4367 .into_iter()
4368 .filter(|wanted| {
4369 !before
4370 .options
4371 .iter()
4372 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4373 })
4374 .collect::<Vec<_>>();
4375 let report = StatusOptionsReport {
4376 source: self.name.clone(),
4377 missing: missing.clone(),
4378 outcome: match (mode, missing.is_empty()) {
4379 (StatusOptionsMode::Plan, _) => StatusOptionsOutcome::Planned,
4380 (StatusOptionsMode::Apply, true) => StatusOptionsOutcome::Unchanged,
4381 (StatusOptionsMode::Apply, false) => StatusOptionsOutcome::Applied,
4382 },
4383 existing: before.options.clone(),
4384 };
4385 if mode == StatusOptionsMode::Plan || missing.is_empty() {
4386 return Ok(report);
4387 }
4388 let mut options = before
4389 .options
4390 .iter()
4391 .map(|option| {
4392 json!({
4393 "id": option.id, "name": option.name, "color": option.color,
4394 "description": option.description,
4395 })
4396 })
4397 .collect::<Vec<_>>();
4398 options.extend(missing.iter().map(|name| {
4399 json!({
4400 "name": name, "color": "GRAY", "description": ""
4401 })
4402 }));
4403 self.graphql(
4404 graphql::STATUS_OPTIONS_UPDATE,
4405 json!({"input": {
4406 "projectId": before.board_id, "fieldId": before.field_id,
4407 "singleSelectOptions": options,
4408 }}),
4409 )
4410 .await?;
4411 let after = self.status_snapshot().await?;
4412 let options_preserved = before
4413 .options
4414 .iter()
4415 .all(|old| after.options.iter().any(|new| new == old));
4416 let additions_present = missing.iter().all(|wanted| {
4417 after
4418 .options
4419 .iter()
4420 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4421 });
4422 if !options_preserved || !additions_present || after.assignments != before.assignments {
4423 let recovery = serde_json::to_string_pretty(&before.assignments).map_err(|error| {
4424 SourceError::Malformed {
4425 message: format!("cannot render pre-write Status recovery snapshot: {error}"),
4426 }
4427 })?;
4428 return Err(SourceError::Refused {
4429 message: format!(
4430 "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}"
4431 ),
4432 });
4433 }
4434 Ok(report)
4435 }
4436
4437 /// A fresh snapshot of the Status field and every board item's assignment of it.
4438 ///
4439 /// # Errors
4440 ///
4441 /// Refuses a board without a single-select `Status` field, and one the token cannot see.
4442 async fn status_snapshot(&self) -> Result<StatusSnapshot, SourceError> {
4443 // Status alone, as this operation has always read it: a `Priority` field is another
4444 // operation's, so nothing about it can refuse this one.
4445 let mut board = self.board_snapshot(&[BoardField::Status]).await?;
4446 let field = board
4447 .fields
4448 .remove(&BoardField::Status)
4449 .ok_or_else(|| self.no_status_field())?;
4450 Ok(StatusSnapshot {
4451 assignments: board.assignments(BoardField::Status),
4452 board_id: board.board_id,
4453 field_id: field.field_id,
4454 options: field.options,
4455 })
4456 }
4457
4458 /// The refusal a board with no `Status` field is answered with by the guarded setup.
4459 fn no_status_field(&self) -> SourceError {
4460 SourceError::Refused {
4461 message: format!("source {} board has no Status field", self.name),
4462 }
4463 }
4464
4465 // llmlint: ignore-block[changed_behavior_has_e2e] Valid snapshot shapes are exercised through
4466 // the real CLI loopback journey, including pagination. The individual malformed guards
4467 // are defensive validation of a schema-pinned third-party response, not separate user
4468 // journeys; drift and missing-field failures cover the operation's recovery behavior.
4469 /// A fresh snapshot of each of the `owned` fields on the board, with its options, and of
4470 /// every board item's value of each, walked to the end of the board's items. A field not
4471 /// in `owned` is read past whatever it holds.
4472 async fn board_snapshot(&self, owned: &[BoardField]) -> Result<BoardSnapshot, SourceError> {
4473 let mut after: Option<String> = None;
4474 let mut snapshot: Option<BoardSnapshot> = None;
4475 loop {
4476 let data = self
4477 .graphql(
4478 graphql::STATUS_OPTIONS_SNAPSHOT,
4479 json!({
4480 "owner": self.owner, "number": self.project_number,
4481 "first": MAX_PAGE_SIZE, "after": after, "nestedFirst": MAX_PAGE_SIZE,
4482 }),
4483 )
4484 .await?;
4485 let board = data
4486 .pointer("/owner/projectV2")
4487 .filter(|board| board.is_object())
4488 .ok_or_else(|| SourceError::Refused {
4489 message: format!(
4490 "source {} has no accessible GitHub Projects board",
4491 self.name
4492 ),
4493 })?;
4494 if board
4495 .pointer("/fields/pageInfo/hasNextPage")
4496 .and_then(Value::as_bool)
4497 != Some(false)
4498 {
4499 return Err(SourceError::Malformed {
4500 message:
4501 "GitHub project fields is incomplete or has malformed pageInfo.hasNextPage"
4502 .into(),
4503 });
4504 }
4505 let mut fields = BTreeMap::new();
4506 // Only the fields this setup owns, by name: a node the single-select fragment did not
4507 // match carries no name, and a person's own single-select field — a `Size`, a
4508 // `Team` — is none of this setup's business, so nothing about it can refuse one. A
4509 // `Status` or `Priority` field without its options is malformed, not absent.
4510 // 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.
4511 for (owned, field) in board
4512 .pointer("/fields/nodes")
4513 .and_then(Value::as_array)
4514 .ok_or_else(|| SourceError::Malformed {
4515 message: "GitHub project fields.nodes is not an array".into(),
4516 })?
4517 .iter()
4518 .filter_map(|field| {
4519 let named = BoardField::named(field.get("name")?.as_str()?)?;
4520 owned.contains(&named).then_some((named, field))
4521 })
4522 {
4523 let options = field
4524 .get("options")
4525 .and_then(Value::as_array)
4526 .ok_or_else(|| SourceError::Malformed {
4527 message: "GitHub single-select field options is not an array".into(),
4528 })?
4529 .iter()
4530 .map(|option| {
4531 Ok(StatusOption {
4532 id: StatusOptionId::try_from(required_str(option, "id")?.to_owned())
4533 .map_err(|message| SourceError::Malformed { message })?,
4534 name: ColumnName::try_from(required_str(option, "name")?.to_owned())
4535 .map_err(|message| SourceError::Malformed {
4536 message: format!(
4537 "GitHub single-select option name is invalid: {message}"
4538 ),
4539 })?,
4540 color: serde_json::from_value(
4541 option.get("color").cloned().unwrap_or(Value::Null),
4542 )
4543 .map_err(|error| {
4544 SourceError::Malformed {
4545 message: format!(
4546 "GitHub single-select option color is invalid: {error}"
4547 ),
4548 }
4549 })?,
4550 description: optional_str(option, "description")?
4551 .unwrap_or_default()
4552 .to_owned(),
4553 })
4554 })
4555 .collect::<Result<Vec<_>, SourceError>>()?;
4556 let snapshot = SnapshotField {
4557 field_id: required_nonblank_str(field, "id")?.to_owned(),
4558 options,
4559 };
4560 // A board's field names are unique, so a second one is an answer that cannot
4561 // say which field the setup would act on — refused rather than one chosen.
4562 if fields.insert(owned, snapshot).is_some() {
4563 return Err(SourceError::Malformed {
4564 message: format!(
4565 "GitHub answered two {} fields for this board",
4566 owned.name()
4567 ),
4568 });
4569 }
4570 }
4571 let board_id = required_nonblank_str(board, "id")?.to_owned();
4572 let current = snapshot.get_or_insert_with(|| BoardSnapshot {
4573 board_id,
4574 fields,
4575 items: Vec::new(),
4576 });
4577 let items = board
4578 .pointer("/items/nodes")
4579 .and_then(Value::as_array)
4580 .ok_or_else(|| SourceError::Malformed {
4581 message: "GitHub project items.nodes is not an array".into(),
4582 })?;
4583 for item in items {
4584 let field_values =
4585 item.get("fieldValues")
4586 .ok_or_else(|| SourceError::Malformed {
4587 message: "GitHub project item is missing fieldValues".into(),
4588 })?;
4589 if field_values
4590 .pointer("/pageInfo/hasNextPage")
4591 .and_then(Value::as_bool)
4592 != Some(false)
4593 {
4594 return Err(SourceError::Malformed {
4595 message: "GitHub project item fieldValues is incomplete or has malformed pageInfo.hasNextPage".into(),
4596 });
4597 }
4598 let values = item
4599 .pointer("/fieldValues/nodes")
4600 .and_then(Value::as_array)
4601 .ok_or_else(|| SourceError::Malformed {
4602 message: "GitHub project item fieldValues.nodes is not an array".into(),
4603 })?;
4604 let item_id = required_nonblank_str(item, "id")?;
4605 let mut assigned = BTreeMap::new();
4606 for value in values {
4607 let Some(field) = value
4608 .pointer("/field/name")
4609 .and_then(Value::as_str)
4610 .and_then(BoardField::named)
4611 .filter(|field| owned.contains(field))
4612 else {
4613 continue;
4614 };
4615 let held = assigned.insert(
4616 field,
4617 AssignedStatusOption {
4618 id: StatusOptionId::try_from(
4619 required_str(value, "optionId")?.to_owned(),
4620 )
4621 .map_err(|message| SourceError::Malformed { message })?,
4622 name: ColumnName::try_from(required_str(value, "name")?.to_owned())
4623 .map_err(|message| SourceError::Malformed {
4624 message: format!(
4625 "GitHub assigned {} name is invalid: {message}",
4626 field.name()
4627 ),
4628 })?,
4629 },
4630 );
4631 // An item holds one value of a field, so a second one leaves no way to
4632 // tell which it holds — and a verification or recovery built on either
4633 // could restore the wrong one.
4634 if held.is_some() {
4635 return Err(SourceError::Malformed {
4636 message: format!(
4637 "GitHub answered two {} values for board item {item_id}",
4638 field.name()
4639 ),
4640 });
4641 }
4642 }
4643 current.items.push((item_id.to_owned(), assigned));
4644 }
4645 let page = board.get("items").ok_or_else(|| SourceError::Malformed {
4646 message: "GitHub project is missing items".into(),
4647 })?;
4648 let has_next = page
4649 .pointer("/pageInfo/hasNextPage")
4650 .and_then(Value::as_bool)
4651 .ok_or_else(|| SourceError::Malformed {
4652 message: "GitHub project items.pageInfo.hasNextPage is not a boolean".into(),
4653 })?;
4654 if !has_next {
4655 break;
4656 }
4657 let next =
4658 required_nonblank_str(page.get("pageInfo").unwrap_or(&Value::Null), "endCursor")?;
4659 validate_cursor_progress(after.as_deref(), next)?;
4660 after = Some(next.to_owned());
4661 }
4662 snapshot.ok_or_else(|| SourceError::Malformed {
4663 message: "GitHub returned no board field snapshot".into(),
4664 })
4665 }
4666 // llmlint: ignore-end[changed_behavior_has_e2e]
4667
4668 /// Report every board field this source's configuration names and, with
4669 /// [`SetupMode::Apply`], set each up: add the options a field lacks, and create
4670 /// the `Priority` field when the board has none.
4671 ///
4672 /// The fields are `Status`, always, with the options `status_mapping` resolves to; and
4673 /// `Priority`, when `priority_mapping` is set, with its four mapped options — created in
4674 /// the order urgent, high, medium, low. An option a field already has keeps its id, name,
4675 /// color and description: the whole option list goes back with every existing id, because
4676 /// a re-minted id clears every item's value.
4677 ///
4678 /// # Errors
4679 ///
4680 /// Refuses a board without a single-select `Status` field. After an apply the board is
4681 /// read again, and a pre-existing option or any item's value of either field that moved is
4682 /// refused with the complete pre-write assignments in the diagnostic, for recovery.
4683 // llmlint: ignore[changed_behavior_has_e2e] The `sources fields` journeys drive plan,
4684 // unchanged apply, a created field, an added option to each field, drift refusal, a board
4685 // with no Status field and a non-github-projects source through the compiled CLI against
4686 // the loopback board. Transport errors are the shared `graphql` boundary's behavior.
4687 pub async fn fields(&self, mode: SetupMode) -> Result<FieldsReport, SourceError> {
4688 let owned: Vec<BoardField> = if self.priorities.is_some() {
4689 vec![BoardField::Status, BoardField::Priority]
4690 } else {
4691 vec![BoardField::Status]
4692 };
4693 let before = self.board_snapshot(&owned).await?;
4694 let mut plans = vec![FieldPlan {
4695 field: BoardField::Status,
4696 wanted: self.statuses.wanted(),
4697 }];
4698 if !before.fields.contains_key(&BoardField::Status) {
4699 return Err(self.no_status_field());
4700 }
4701 if let Some(mapping) = &self.priorities {
4702 plans.push(FieldPlan {
4703 field: BoardField::Priority,
4704 wanted: mapping.names().map(str::to_owned).collect(),
4705 });
4706 }
4707 // The snapshot reads single-select fields alone, so a field it did not find may still
4708 // be on the board under the name, of another type: creating one beside it would fail
4709 // part way, or leave two fields of one name. Asked of the board's own field list, and
4710 // only when a field is missing.
4711 if plans
4712 .iter()
4713 .any(|plan| !before.fields.contains_key(&plan.field))
4714 {
4715 let board = self.board_fields().await?;
4716 for plan in plans
4717 .iter()
4718 .filter(|plan| !before.fields.contains_key(&plan.field))
4719 {
4720 if let Some(field) = Board::field(&board.fields, plan.field.name())? {
4721 return Err(SourceError::Refused {
4722 message: format!(
4723 "source {}'s board has a {} field that is not a single-select field \
4724 (it is a {}), so it cannot hold this source's options; next: rename \
4725 or remove that field, then run this again",
4726 self.name,
4727 plan.field.name(),
4728 optional_str(field, "__typename")?.unwrap_or("field of another type")
4729 ),
4730 });
4731 }
4732 }
4733 }
4734 let mut reports = Vec::new();
4735 for plan in &plans {
4736 let held = before.fields.get(&plan.field);
4737 let existing = held.map(|field| field.options.clone()).unwrap_or_default();
4738 let mut missing: Vec<String> = Vec::new();
4739 for wanted in &plan.wanted {
4740 let present = existing
4741 .iter()
4742 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4743 || missing
4744 .iter()
4745 .any(|named| named.eq_ignore_ascii_case(wanted));
4746 if !present {
4747 missing.push(wanted.clone());
4748 }
4749 }
4750 let kinds = match plan.field {
4751 BoardField::Status => self.statuses.missing_by_kind(&existing),
4752 BoardField::Priority => Vec::new(),
4753 };
4754 reports.push(FieldReport {
4755 field: plan.field,
4756 exists: held.is_some(),
4757 kinds,
4758 outcome: match (mode, held.is_some(), missing.is_empty()) {
4759 (SetupMode::Plan, _, _) => FieldOutcome::Planned,
4760 (SetupMode::Apply, true, true) => FieldOutcome::Unchanged,
4761 (SetupMode::Apply, true, false) => FieldOutcome::Applied,
4762 (SetupMode::Apply, false, _) => FieldOutcome::Created,
4763 },
4764 missing,
4765 existing,
4766 });
4767 }
4768 // A projected field is a text field, which the single-select snapshot cannot see, so
4769 // it is asked of the board's own field list — and only when one is configured.
4770 let mut metadata_fields = Vec::new();
4771 if !self.metadata_fields.is_empty() {
4772 let board = self.board_fields().await?;
4773 for projection in &self.metadata_fields {
4774 let conflict = match Board::field(&board.fields, &projection.field)? {
4775 None => None,
4776 Some(field) => {
4777 let typename = optional_str(field, "__typename")?;
4778 let data_type = optional_str(field, "dataType")?;
4779 if typename == Some("ProjectV2Field") && data_type == Some("TEXT") {
4780 None
4781 } else {
4782 let named = data_type
4783 .filter(|kind| *kind != "TEXT")
4784 .or(typename)
4785 .unwrap_or("field of another type");
4786 Some(NonTextFieldType::try_from(named.to_owned()).map_err(
4787 |message| SourceError::Malformed {
4788 message: format!(
4789 "GitHub answered the board's {} field with a type this \
4790 setup cannot read: {message}",
4791 &*projection.field
4792 ),
4793 },
4794 )?)
4795 }
4796 }
4797 };
4798 let exists =
4799 conflict.is_none() && Board::field(&board.fields, &projection.field)?.is_some();
4800 metadata_fields.push(MetadataFieldReport {
4801 field: projection.field.to_string(),
4802 key: projection.key.to_string(),
4803 path: projection.path.clone(),
4804 exists,
4805 outcome: match (mode, exists || conflict.is_some()) {
4806 (SetupMode::Plan, _) => FieldOutcome::Planned,
4807 (SetupMode::Apply, true) => FieldOutcome::Unchanged,
4808 (SetupMode::Apply, false) => FieldOutcome::Created,
4809 },
4810 conflict,
4811 });
4812 }
4813 }
4814 let report = FieldsReport {
4815 source: self.name.clone(),
4816 fields: reports,
4817 metadata_fields,
4818 };
4819 let writes: Vec<&FieldReport> = report
4820 .fields
4821 .iter()
4822 .filter(|field| !field.missing.is_empty() || !field.exists)
4823 .collect();
4824 let creates: Vec<&MetadataFieldReport> = report
4825 .metadata_fields
4826 .iter()
4827 .filter(|field| field.outcome == FieldOutcome::Created)
4828 .collect();
4829 if mode == SetupMode::Plan || (writes.is_empty() && creates.is_empty()) {
4830 return Ok(report);
4831 }
4832 let mut landed: Vec<&str> = Vec::new();
4833 for field in &writes {
4834 let added = field
4835 .missing
4836 .iter()
4837 .map(|name| json!({"name": name, "color": "GRAY", "description": ""}));
4838 let sent = match before.fields.get(&field.field) {
4839 Some(held) => {
4840 let mut options = held
4841 .options
4842 .iter()
4843 .map(|option| {
4844 json!({
4845 "id": option.id, "name": option.name, "color": option.color,
4846 "description": option.description,
4847 })
4848 })
4849 .collect::<Vec<_>>();
4850 options.extend(added);
4851 self.graphql(
4852 graphql::STATUS_OPTIONS_UPDATE,
4853 json!({"input": {
4854 "projectId": before.board_id, "fieldId": held.field_id,
4855 "singleSelectOptions": options,
4856 }}),
4857 )
4858 .await
4859 }
4860 None => {
4861 self.graphql(
4862 graphql::CREATE_FIELD,
4863 json!({"input": {
4864 "projectId": before.board_id, "dataType": "SINGLE_SELECT",
4865 "name": field.field.name(),
4866 "singleSelectOptions": added.collect::<Vec<_>>(),
4867 }}),
4868 )
4869 .await
4870 }
4871 };
4872 // A mutation that failed does not establish that GitHub left its field as it was,
4873 // so every failure from here on carries the recovery data a drift refusal does.
4874 match sent {
4875 Ok(_) => landed.push(field.field.name()),
4876 Err(error) => {
4877 let changed = if landed.is_empty() {
4878 String::new()
4879 } else {
4880 format!("changed the {} field and then ", landed.join(" and "))
4881 };
4882 return Err(SourceError::Refused {
4883 message: format!(
4884 "the guarded field setup {changed}failed on the {} field, which it may \
4885 have changed part way: {error}; the pre-write item assignments \
4886 are:\n{}",
4887 field.field.name(),
4888 recovery(&report, &before)?
4889 ),
4890 });
4891 }
4892 }
4893 }
4894 for field in &creates {
4895 let sent = self
4896 .graphql(
4897 graphql::CREATE_FIELD,
4898 json!({"input": {
4899 "projectId": before.board_id, "dataType": "TEXT", "name": field.field,
4900 }}),
4901 )
4902 .await;
4903 if let Err(error) = sent {
4904 let changed = if landed.is_empty() {
4905 String::new()
4906 } else {
4907 format!("changed the {} field and then ", landed.join(" and "))
4908 };
4909 return Err(SourceError::Refused {
4910 message: format!(
4911 "the guarded field setup {changed}failed creating the {} text field: \
4912 {error}; run it again to create what is still missing",
4913 field.field
4914 ),
4915 });
4916 }
4917 landed.push(&field.field);
4918 }
4919 // The board has been written, so a verification read that fails leaves it unverified
4920 // rather than unchanged, and says what to put back.
4921 let after = match self.board_snapshot(&owned).await {
4922 Ok(after) => after,
4923 Err(error) => {
4924 return Err(SourceError::Refused {
4925 message: format!(
4926 "the guarded field setup changed the {} field and then could not read the \
4927 board back to verify it: {error}; the pre-write item assignments are:\n{}",
4928 landed.join(" and "),
4929 recovery(&report, &before)?
4930 ),
4931 });
4932 }
4933 };
4934 let mut moved = Vec::new();
4935 for field in &report.fields {
4936 let name = field.field.name();
4937 let now = after
4938 .fields
4939 .get(&field.field)
4940 .map(|held| held.options.as_slice())
4941 .unwrap_or_default();
4942 if !field.existing.iter().all(|old| now.contains(old)) {
4943 moved.push(format!(
4944 "a pre-existing {name} option id, name, color or description"
4945 ));
4946 }
4947 if !field.missing.iter().all(|wanted| {
4948 now.iter()
4949 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4950 }) {
4951 moved.push(format!("an added {name} option"));
4952 }
4953 if after.assignments(field.field) != before.assignments(field.field) {
4954 moved.push(format!("an item's {name} value"));
4955 }
4956 }
4957 if !creates.is_empty() {
4958 // Read afresh: the view held for this command is the one from before the creates.
4959 *self.fields_cache()? = None;
4960 let board = match self.board_fields().await {
4961 Ok(board) => board,
4962 Err(error) => {
4963 return Err(SourceError::Refused {
4964 message: format!(
4965 "the guarded field setup changed the {} field and then could not \
4966 read the board back to verify it: {error}; run it again to verify \
4967 it, which creates nothing a second time",
4968 landed.join(" and ")
4969 ),
4970 });
4971 }
4972 };
4973 for field in &creates {
4974 let text = Board::field(&board.fields, &field.field)?.is_some_and(|held| {
4975 held.get("__typename").and_then(Value::as_str) == Some("ProjectV2Field")
4976 && held.get("dataType").and_then(Value::as_str) == Some("TEXT")
4977 });
4978 if !text {
4979 moved.push(format!("the created {} text field", field.field));
4980 }
4981 }
4982 }
4983 if !moved.is_empty() {
4984 return Err(SourceError::Refused {
4985 message: format!(
4986 "GitHub changed {} after the guarded field setup; the pre-write item \
4987 assignments are:\n{}",
4988 moved.join(", "),
4989 recovery(&report, &before)?
4990 ),
4991 });
4992 }
4993 Ok(report)
4994 }
4995
4996 /// Validate configuration and capture the named credential without exposing it.
4997 ///
4998 /// # Errors
4999 ///
5000 /// Returns [`SourceError::Config`] for a configuration this instance cannot use and
5001 /// [`SourceError::Auth`] when the named credential is missing or empty.
5002 pub fn new(
5003 name: &SourceName,
5004 config: GitHubProjectsConfig,
5005 secrets: &dyn SecretResolver,
5006 ) -> Result<Self, SourceError> {
5007 Self::recording_into(name, config, secrets, Arc::new(Accounting::new()))
5008 }
5009
5010 /// The same, recording every request it sends into an accounting the caller holds too.
5011 ///
5012 /// [`Self::new`] is this with an accounting of its own. A caller that is also making
5013 /// its own calls to GitHub — a lane verifying a schema, sweeping residue or cleaning
5014 /// up — passes the one it records those into, so the session total accounts for the
5015 /// whole session rather than for this source's share of it.
5016 ///
5017 /// # Errors
5018 ///
5019 /// Exactly [`Self::new`]'s: [`SourceError::Config`] for a configuration this instance
5020 /// cannot use and [`SourceError::Auth`] when the named credential is missing or empty.
5021 pub fn recording_into(
5022 name: &SourceName,
5023 config: GitHubProjectsConfig,
5024 secrets: &dyn SecretResolver,
5025 ledger: Arc<Accounting>,
5026 ) -> Result<Self, SourceError> {
5027 if !valid_github_owner(&config.owner) {
5028 return Err(SourceError::Config {
5029 message: "owner must be 1-39 ASCII letters, digits, or single hyphens, and cannot start or end with a hyphen".into(),
5030 });
5031 }
5032 if config.project_number == 0 || config.project_number > i32::MAX as u32 {
5033 return Err(SourceError::Config {
5034 message: format!("project_number must be between 1 and {}", i32::MAX),
5035 });
5036 }
5037 if !valid_environment_name(&config.token_env) {
5038 return Err(SourceError::Config {
5039 message: "token_env must be a valid environment-variable name".into(),
5040 });
5041 }
5042 let repository = config
5043 .repository
5044 .as_deref()
5045 .map(RepositoryTarget::parse)
5046 .transpose()?;
5047 let endpoint = Url::parse(&config.endpoint).map_err(|e| SourceError::Config {
5048 message: format!("endpoint is not a valid URL: {e}"),
5049 })?;
5050 if endpoint.scheme() != "https"
5051 && !(endpoint.scheme() == "http"
5052 && endpoint
5053 .host_str()
5054 .is_some_and(|h| h == "127.0.0.1" || h == "localhost" || h == "::1"))
5055 {
5056 return Err(SourceError::Config {
5057 message:
5058 "endpoint must use HTTPS (HTTP is accepted only for a loopback test server)"
5059 .into(),
5060 });
5061 }
5062 let token = secrets.get(&config.token_env).filter(|token| !token.expose_secret().trim().is_empty()).ok_or_else(|| SourceError::Auth {
5063 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),
5064 })?;
5065 Ok(Self {
5066 name: name.clone(),
5067 owner: config.owner,
5068 project_number: config.project_number,
5069 repository,
5070 asset_client: std::sync::OnceLock::new(),
5071 endpoint,
5072 token,
5073 credential_name: config.token_env,
5074 statuses: BoardStatuses::resolve(&config.status_mapping, name)?,
5075 priorities: config
5076 .priority_mapping
5077 .map(|mapping| PriorityMapping::resolve(mapping, name))
5078 .transpose()?,
5079 metadata_fields: MetadataField::resolve(config.metadata_fields, name)?,
5080 client: std::sync::OnceLock::new(),
5081 created: Mutex::new(Vec::new()),
5082 updated: Mutex::new(Vec::new()),
5083 commented: Mutex::new(Vec::new()),
5084 pacing: Pacing::resolve(config.pacing, name)?,
5085 last_mutation: Mutex::new(None),
5086 windows: Mutex::new(pacing::Windows::default()),
5087 task_children: Mutex::new(BTreeMap::new()),
5088 written: Mutex::new(BTreeMap::new()),
5089 deferred: Mutex::new(None),
5090 held_updates: Mutex::new(Vec::new()),
5091 clock: system_clock(),
5092 declared_private: std::sync::atomic::AtomicBool::new(false),
5093 numeric_repositories: tokio::sync::Mutex::new(BTreeMap::new()),
5094 board_cache: Mutex::new(None),
5095 search_cache: Mutex::new(None),
5096 narrowed_cache: Mutex::new(BTreeMap::new()),
5097 resolved_cache: Mutex::new(BTreeMap::new()),
5098 read_ahead: Mutex::new(BTreeMap::new()),
5099 pending_origins: Mutex::new(Vec::new()),
5100 member_classes: Mutex::new(BTreeMap::new()),
5101 carried: Mutex::new(BTreeMap::new()),
5102 held_cache: Mutex::new(BTreeMap::new()),
5103 children_cache: Mutex::new(BTreeMap::new()),
5104 search_next: Mutex::new(BTreeMap::new()),
5105 fields_cache: Mutex::new(None),
5106 repository_cache: Mutex::new(BTreeMap::new()),
5107 ledger,
5108 expected: expected::Held::default(),
5109 projection: projection::Held::default(),
5110 })
5111 }
5112
5113 /// The client every API request goes through, built on the first one.
5114 ///
5115 /// Not with the source: building a client loads the system's certificate store, which
5116 /// costs tens of milliseconds, and a command that configures this board but never sends to
5117 /// it — a read of another source — should not pay that in its process.
5118 fn client(&self) -> Result<&Client, SourceError> {
5119 built(&self.client, || {
5120 Client::builder()
5121 .user_agent("onetaskgraph")
5122 .build()
5123 .map_err(|e| SourceError::Config {
5124 message: format!("cannot build HTTP client: {e}"),
5125 })
5126 })
5127 }
5128
5129 /// The client attachment requests go through, with its own redirect policy, built on the
5130 /// first one for the reason [`Self::client`] gives.
5131 fn asset_client(&self) -> Result<&Client, SourceError> {
5132 built(&self.asset_client, || assets::client(&self.endpoint))
5133 }
5134
5135 /// A snapshot of every request this source has sent, and what each cost.
5136 ///
5137 /// A value to hold and compare rather than a borrow of the accounting itself, so two
5138 /// of them can sit side by side. When this source was built with
5139 /// [`Self::recording_into`] the snapshot is the whole shared session, which is the
5140 /// point of building it that way.
5141 #[must_use]
5142 pub fn accounting(&self) -> accounting::Session {
5143 self.ledger.snapshot()
5144 }
5145
5146 /// Send one GraphQL document, pacing this source's own mutations and waiting out a
5147 /// rate limit rather than handing it straight back as an error.
5148 ///
5149 /// Retrying is safe for every document here, including the mutations, and the reason
5150 /// is that only a *refusal* is retried: [`Limiter::classify`] rules on a response
5151 /// GitHub sent, and a request GitHub refused for a rate limit did not run, so nothing
5152 /// this replays has already taken effect. An outcome this source cannot know — the
5153 /// send failed, or the body could not be read, so the mutation may well have landed —
5154 /// is [`Attempt::Failed`] in [`send_once`] and leaves this loop without a second
5155 /// attempt. A duplicate write would come from replaying one of those, and none is
5156 /// replayed.
5157 async fn graphql(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
5158 // Updates held back go out before any other request, so whatever it reads or writes
5159 // sees the board as though each had been sent when it was made.
5160 Box::pin(self.send_held_updates()).await?;
5161 // Origins held back for a read ahead that never came are sent before anything else.
5162 if query != graphql::ORIGIN_LOOKUPS && query != graphql::ORIGIN_LOOKUPS_WITH_MEMBERS {
5163 Box::pin(self.send_pending_origins(None)).await?;
5164 }
5165 if !self.expected.journaling() {
5166 return self.graphql_sent(query, variables).await;
5167 }
5168 // While a write the store's cache expects runs, its mutations select the whole issue
5169 // they leave behind, so the response vouches for what the cache keeps — and every
5170 // request is recorded, so the write's parts are reported from their own responses.
5171 let (query, variables) = match graphql::whole_twin(query) {
5172 Some(twin) => (twin, expected::whole_variables(variables)),
5173 None => (query, variables),
5174 };
5175 let sent = self.graphql_sent(query, variables.clone()).await;
5176 self.journal(query, &variables, &sent).await;
5177 sent
5178 }
5179
5180 /// One document sent, retried while GitHub asks for a wait, exactly as asked.
5181 async fn graphql_sent(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
5182 if is_mutation(query)
5183 && ![
5184 graphql::ADD_COMMENT,
5185 graphql::UPDATE_COMMENT,
5186 graphql::DELETE_COMMENT,
5187 graphql::ADD_COMMENT_WHOLE,
5188 graphql::UPDATE_COMMENT_WHOLE,
5189 ]
5190 .contains(&query)
5191 {
5192 let mut cache = self.resolved_cache()?;
5193 let mut ahead = self.read_ahead()?;
5194 let mut carried = self.carried()?;
5195 for argument in ["input", "second", "third", "clear"] {
5196 if let Some(input) = variables.get(argument) {
5197 let names = |id: &NativeId, item_id: Option<&str>| {
5198 ["id", "issueId", "subjectId", "itemId"].iter().any(|key| {
5199 input
5200 .get(key)
5201 .and_then(Value::as_str)
5202 .is_some_and(|value| value == id.0 || Some(value) == item_id)
5203 })
5204 };
5205 cache.retain(|id, item| !names(id, Some(&item.item_id)));
5206 ahead.retain(|id, item| {
5207 !names(id, item.as_ref().map(|item| item.item_id.as_str()))
5208 });
5209 carried.retain(|id, item| !names(id, Some(&item.item_id)));
5210 }
5211 }
5212 }
5213 let doing = operation_description(query);
5214 let mutation = is_mutation(query);
5215 let charge = pacing::Charge::graphql(query, &variables);
5216 let mut waited = Duration::ZERO;
5217 let mut waits = 0_u32;
5218 let mut backoff = self.pacing.retry_backoff;
5219 loop {
5220 let ticket = self.pace(charge, mutation).await;
5221 let attempt = self.send_once(query, &variables, waits + 1).await;
5222 self.paced(ticket, mutation);
5223 let limited = match attempt {
5224 Ok(data) => return Ok(data),
5225 Err(Attempt::Failed(error)) => return Err(error),
5226 Err(Attempt::Limited(limited)) => limited,
5227 };
5228 let (wait, cause) = limited.wait(backoff);
5229 let remaining = self.pacing.retry_budget.saturating_sub(waited);
5230 // A wait of nothing spends none of the budget, so it is exhaustion rather
5231 // than a retry. `Pacing::resolve` rules out every way of configuring one
5232 // except a budget of zero, where reporting the first refusal is the ask.
5233 if wait.is_zero() || wait > remaining {
5234 return Err(limited.exhausted(
5235 doing,
5236 waits,
5237 waited,
5238 wait,
5239 self.pacing.retry_budget,
5240 ));
5241 }
5242 self.clock.sleep_because(wait, cause).await;
5243 waited += wait;
5244 waits += 1;
5245 backoff = backoff.saturating_mul(2);
5246 }
5247 }
5248
5249 /// Hold one request charging `charge` until every secondary window it charges has room for
5250 /// the whole of it — and, for a mutation of an instance configured with
5251 /// `pacing.min_mutation_interval_ms`, until that interval has passed since the last one
5252 /// finished — then count it, answering the ticket [`Self::paced`] finishes it with.
5253 ///
5254 /// The moment is decided and counted under the lock and waited out outside it, so two
5255 /// callers take two places rather than the same one, and no lock is held across an await.
5256 /// A request with room goes out at once: nothing here waits that a window does not ask for.
5257 async fn pace(&self, charge: pacing::Charge, mutation: bool) -> pacing::Ticket {
5258 let (wait, cause, ticket) = {
5259 // A poisoned lock here costs pacing, not correctness, and refusing the write over
5260 // it would turn an earlier failure into a second one for no gain.
5261 let mut windows = self
5262 .windows
5263 .lock()
5264 .unwrap_or_else(std::sync::PoisonError::into_inner);
5265 let now = self.clock.now();
5266 let (mut at, mut cause) = if self.pacing.windows {
5267 windows.room(now, charge)
5268 } else {
5269 (now, None)
5270 };
5271 if mutation && !self.pacing.min_mutation_interval.is_zero() {
5272 let mut last = self
5273 .last_mutation
5274 .lock()
5275 .unwrap_or_else(std::sync::PoisonError::into_inner);
5276 // `checked_add` rather than `+`: adding durations can panic on overflow, and
5277 // pacing is not worth a panic even at a bound `MAX_PACING_MS` already rules out.
5278 if let Some(earliest) = last
5279 .and_then(|previous| previous.checked_add(self.pacing.min_mutation_interval))
5280 && earliest > at
5281 {
5282 at = earliest;
5283 cause = Some(pacing::cause::MIN_INTERVAL);
5284 }
5285 *last = Some(at);
5286 }
5287 let ticket = windows.charge(at, charge);
5288 (at.saturating_sub(now), cause, ticket)
5289 };
5290 if let (false, Some(cause)) = (wait.is_zero(), cause) {
5291 self.clock.sleep_because(wait, cause).await;
5292 }
5293 ticket
5294 }
5295
5296 /// Record that the request `ticket` was charged for has finished, so each window — and
5297 /// the configured spacing — counts it from here rather than from the moment it left.
5298 ///
5299 /// This source can only choose when a request *departs*; GitHub's limiter counts when it
5300 /// *arrives*, and the two differ by whatever the request spent in transit. Counting from
5301 /// departure would hand the limiter a window that looked roomier than it is, so a source
5302 /// pacing exactly at a limit could still be seen arriving over it — which is how a copy
5303 /// paced well inside a board's threshold was once refused by it on a slower machine while
5304 /// passing on a quick one. The previous request had arrived before its response came back,
5305 /// so counting from here is never earlier than the limiter does, whatever transit costs
5306 /// and on whatever platform: the safe side of a limit that punishes being wrong by refusing
5307 /// reads for the next fifty minutes.
5308 ///
5309 /// A failed attempt is finished too: a request refused by the limiter still arrived, and
5310 /// one that never left costs only room nobody needed.
5311 fn paced(&self, ticket: pacing::Ticket, mutation: bool) {
5312 let now = self.clock.now();
5313 self.windows
5314 .lock()
5315 .unwrap_or_else(std::sync::PoisonError::into_inner)
5316 .finish(ticket, now);
5317 if mutation && !self.pacing.min_mutation_interval.is_zero() {
5318 let mut last = self
5319 .last_mutation
5320 .lock()
5321 .unwrap_or_else(std::sync::PoisonError::into_inner);
5322 // `max` rather than an assignment: a concurrent caller may already have reserved a
5323 // slot further out, and completing this request must never pull that slot back in.
5324 *last = Some(last.map_or(now, |reserved| reserved.max(now)));
5325 }
5326 }
5327
5328 /// One HTTP attempt, classified into an answer, a rate limit to wait out, or a
5329 /// failure that waiting cannot help — and recorded, whichever of the three it was.
5330 ///
5331 /// This is the one place a request leaves this crate, which is why the accounting is
5332 /// here rather than at each of the callers: a read path added later is counted without
5333 /// anybody remembering to count it, and
5334 /// `the_session_report_counts_every_request_the_board_served_and_what_each_cost` fails
5335 /// when one is not.
5336 async fn send_once(
5337 &self,
5338 query: &str,
5339 variables: &Value,
5340 number: u32,
5341 ) -> Result<Value, Attempt> {
5342 let executed = self.ledger.priced_document(query);
5343 let Attempted {
5344 result,
5345 limits,
5346 reported_cost,
5347 response_outcome,
5348 } = self.attempt(executed.as_ref(), variables).await;
5349 // No `otherwise` name: every document this source sends is one of its own, and the
5350 // inventory gate on `graphql::DOCUMENTS` is what keeps that true.
5351 let sending = accounting::Request::graphql(query, variables, None, reported_cost);
5352 let outcome = match &result {
5353 Ok(_) => accounting::Outcome::Answered,
5354 Err(Attempt::Limited(_)) => accounting::Outcome::RateLimited,
5355 Err(Attempt::Failed(_)) => accounting::Outcome::Refused,
5356 };
5357 self.ledger
5358 .record(sending.finished(outcome, limits).with_execution(
5359 Some(executed.into_owned()),
5360 response_outcome,
5361 number,
5362 ));
5363 result
5364 }
5365
5366 /// The attempt itself, with what its response said about the rate limit alongside.
5367 ///
5368 /// The two are returned together rather than recorded here because every one of the
5369 /// early exits below is a different outcome, and a record written at each of them is a
5370 /// record one of them can be added without.
5371 async fn attempt(&self, query: &str, variables: &Value) -> Attempted {
5372 let mut limits = accounting::RateLimit::default();
5373 let mut reported_cost = None;
5374 let mut response_outcome = accounting::ResponseOutcome::TransportError;
5375 let result = self
5376 .attempted(
5377 query,
5378 variables,
5379 &mut limits,
5380 &mut reported_cost,
5381 &mut response_outcome,
5382 )
5383 .await;
5384 Attempted {
5385 result,
5386 limits,
5387 reported_cost,
5388 response_outcome,
5389 }
5390 }
5391
5392 /// One HTTP attempt, filling in what its response said about the rate limit as it goes.
5393 async fn attempted(
5394 &self,
5395 query: &str,
5396 variables: &Value,
5397 limits: &mut accounting::RateLimit,
5398 reported_cost: &mut Option<u64>,
5399 response_outcome: &mut accounting::ResponseOutcome,
5400 ) -> Result<Value, Attempt> {
5401 let response = self
5402 .client()
5403 .map_err(Attempt::Failed)?
5404 .post(self.endpoint.clone())
5405 .bearer_auth(self.token.expose_secret())
5406 .json(&json!({"query": query, "variables": variables}))
5407 .send()
5408 .await
5409 .map_err(|e| {
5410 Attempt::Failed(SourceError::Unavailable {
5411 message: format!("GitHub GraphQL request failed: {}", transport_error(&e)),
5412 })
5413 })?;
5414 let status = response.status();
5415 if !status.is_success() {
5416 *response_outcome = accounting::ResponseOutcome::HttpError;
5417 }
5418 let header = |name: &str| whole_seconds(response.headers().get(name));
5419 let served = response
5420 .headers()
5421 .get(reqwest::header::DATE)
5422 .and_then(|value| value.to_str().ok())
5423 .and_then(|value| DateTime::parse_from_rfc2822(value).ok())
5424 .map(|at| at.with_timezone(&Utc));
5425 *limits = accounting::RateLimit::read(|name| {
5426 response
5427 .headers()
5428 .get(name)
5429 .and_then(|value| value.to_str().ok())
5430 .map(str::to_owned)
5431 });
5432 // Exactly `0` is exhaustion and everything else — a count, an empty value, bytes
5433 // that are not text at all — is "not known to be exhausted". This never makes a
5434 // response a refusal on its own: it says which limiter a refusal is attributed to
5435 // and where its hint comes from, so a value this cannot read costs a hint rather
5436 // than an answer.
5437 let exhausted = response
5438 .headers()
5439 .get("x-ratelimit-remaining")
5440 .and_then(|value| value.to_str().ok())
5441 == Some("0");
5442 // `retry-after` is what GitHub asks for when it asks; when it does not and the
5443 // primary budget is spent, `x-ratelimit-reset` says when that budget comes back,
5444 // which is the same question answered as an absolute time. Nothing else here is a
5445 // hint, and a schedule is what answers a refusal that carries none.
5446 let retry_after = header("retry-after");
5447 let hinted_by = if retry_after.is_some() || !exhausted {
5448 Hint::RetryAfter
5449 } else {
5450 Hint::PrimaryReset
5451 };
5452 let hint = retry_after.or_else(|| {
5453 exhausted
5454 .then(|| header("x-ratelimit-reset"))
5455 .flatten()
5456 .map(|reset| reset.saturating_sub(Utc::now().timestamp().max(0).unsigned_abs()))
5457 });
5458 // Read before it is parsed, because the evidence which tells a secondary rate
5459 // limit from a rejected credential is in the body of a response whose status says
5460 // only "forbidden" — and a non-success response was never parsed at all.
5461 let body = response.text().await.map_err(|e| {
5462 Attempt::Failed(SourceError::Unavailable {
5463 message: format!(
5464 "GitHub GraphQL response could not be read: {}",
5465 transport_error(&e)
5466 ),
5467 })
5468 })?;
5469 *reported_cost = serde_json::from_str::<Value>(&body)
5470 .ok()
5471 .as_ref()
5472 .and_then(|body| body.pointer("/data/rateLimit/cost"))
5473 .and_then(Value::as_u64);
5474 let parsed = serde_json::from_str::<Value>(&body).ok();
5475 *response_outcome = accounting::ResponseOutcome::of_graphql_reply(status, parsed.as_ref());
5476 if let Some(limiter) = Limiter::classify(status, exhausted, &body) {
5477 return Err(Attempt::Limited(Limited {
5478 limiter,
5479 hint,
5480 hinted_by,
5481 }));
5482 }
5483 if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {
5484 return Err(Attempt::Failed(SourceError::Auth {
5485 message: format!(
5486 "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"
5487 ),
5488 }));
5489 }
5490 if !status.is_success() {
5491 return Err(Attempt::Failed(SourceError::Unavailable {
5492 message: format!("GitHub GraphQL returned HTTP {status}"),
5493 }));
5494 }
5495 let answered = if query == graphql::ISSUES_BY_REFERENCE {
5496 self.answer_unresolved(&body)
5497 } else if query == graphql::CREATE_ISSUES {
5498 answer_with_errors(&body).map_or_else(|| self.answer(&body), Ok)
5499 } else {
5500 self.answer(&body)
5501 };
5502 let mut data = answered.map_err(Attempt::Failed)?;
5503 if let Some(served) = served {
5504 stamp_served(&mut data, &served.to_rfc3339());
5505 }
5506 Ok(data)
5507 }
5508
5509 /// What one successful HTTP response says, once its GraphQL errors are read.
5510 /// [`Self::answer`], for a read of issues by reference: a reference GitHub cannot resolve
5511 /// for this credential — a repository it cannot see, an issue number it does not hold —
5512 /// is answered `null` beside a `NOT_FOUND` error naming it, and that is an answer about
5513 /// that reference rather than a failure of the others. Any other error refuses the read,
5514 /// as there.
5515 fn answer_unresolved(&self, body: &str) -> Result<Value, SourceError> {
5516 let parsed: Option<Value> = serde_json::from_str(body).ok();
5517 // Each error is a `NOT_FOUND` whose path names one slot of the batch, `r<n>` or its
5518 // `issue`, and the answer holds `null` at exactly that path: anything else — another
5519 // kind of error, one naming no slot, or one beside a value it says is not there — is a
5520 // refusal of the whole read.
5521 let unresolved = |data: &Value, error: &Value| {
5522 error.get("type").and_then(Value::as_str) == Some("NOT_FOUND")
5523 && error
5524 .get("path")
5525 .and_then(Value::as_array)
5526 .is_some_and(|path| {
5527 let alias = path.first().and_then(Value::as_str).and_then(|alias| {
5528 alias
5529 .strip_prefix('r')
5530 .and_then(|slot| slot.parse::<usize>().ok())
5531 .filter(|slot| *slot < REFERENCE_BATCH)
5532 .map(|_| alias)
5533 });
5534 match (alias, path.get(1).and_then(Value::as_str), path.len()) {
5535 (Some(alias), None, 1) => data.get(alias).is_some_and(Value::is_null),
5536 (Some(alias), Some("issue"), 2) => data
5537 .get(alias)
5538 .and_then(|repository| repository.get("issue"))
5539 .is_some_and(Value::is_null),
5540 _ => false,
5541 }
5542 })
5543 };
5544 let unresolved_only = parsed.as_ref().is_some_and(|parsed| {
5545 parsed.get("data").is_some_and(|data| {
5546 data.is_object()
5547 && parsed
5548 .get("errors")
5549 .and_then(Value::as_array)
5550 .is_some_and(|errors| errors.iter().all(|error| unresolved(data, error)))
5551 })
5552 });
5553 match parsed {
5554 Some(parsed) if unresolved_only => Ok(parsed["data"].clone()),
5555 _ => self.answer(body),
5556 }
5557 }
5558
5559 fn answer(&self, body: &str) -> Result<Value, SourceError> {
5560 let body: Value = serde_json::from_str(body).map_err(|e| SourceError::Malformed {
5561 message: format!("GitHub returned invalid JSON: {e}"),
5562 })?;
5563 let errors = body
5564 .get("errors")
5565 .map(|value| {
5566 value.as_array().ok_or_else(|| SourceError::Malformed {
5567 message: "GitHub response errors is not an array".into(),
5568 })
5569 })
5570 .transpose()?;
5571 if let Some(errors) = errors.filter(|errors| !errors.is_empty()) {
5572 let messages = errors
5573 .iter()
5574 .filter_map(|e| e.get("message").and_then(Value::as_str))
5575 .collect::<Vec<_>>()
5576 .join("; ");
5577 let message = if messages.is_empty() {
5578 "GitHub returned GraphQL errors".into()
5579 } else {
5580 messages
5581 };
5582 let normalized = message.to_ascii_lowercase();
5583 if normalized.contains("resource not accessible") || normalized.contains("scope") {
5584 return Err(SourceError::Auth {
5585 message: format!(
5586 "{message}; grant {} Projects and Issues read/write plus Pull requests read-only access for every repository represented on the board",
5587 self.credential_name
5588 ),
5589 });
5590 }
5591 return Err(SourceError::Refused { message });
5592 }
5593 body.get("data")
5594 .filter(|data| data.is_object())
5595 .cloned()
5596 .ok_or_else(|| SourceError::Malformed {
5597 message: "GitHub response has no data object".into(),
5598 })
5599 }
5600
5601 // llmlint: ignore[boundary_inputs_validated] GitHub caps nested connections at 100 and
5602 // GraphQL cannot independently page them inside the outer item page. This source page is
5603 // deliberately bounded at that published maximum; the live drift journey exercises it.
5604 async fn board_page(
5605 &self,
5606 items_after: Option<&str>,
5607 items_first: u32,
5608 ) -> Result<Value, SourceError> {
5609 let data = self
5610 .graphql(
5611 graphql::BOARD,
5612 json!({"owner":self.owner,"number":self.project_number,
5613 "first":items_first.min(MAX_PAGE_SIZE),"after":items_after,
5614 "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
5615 )
5616 .await?;
5617 data.pointer("/owner/projectV2")
5618 .filter(|v| !v.is_null())
5619 .cloned()
5620 .ok_or_else(|| SourceError::Refused {
5621 message: format!(
5622 "GitHub project {}/{} was not found or is not visible to the token",
5623 self.owner, self.project_number
5624 ),
5625 })
5626 }
5627
5628 /// The search that finds the issues of this board, narrowed by `also` when it is
5629 /// given.
5630 ///
5631 /// `project:owner/number` is what scopes a search to one board, and `is:issue` is what
5632 /// keeps pull requests out of it: GitHub's `ISSUE` search type covers both, and a pull
5633 /// request is somebody's change rather than a unit of plan. `-has:parent` is *not*
5634 /// here on purpose — GitHub accepts it and silently ignores it, so a project is told
5635 /// from a task by the `parent` field each issue carries rather than by the search.
5636 fn board_search(&self, also: Option<&str>) -> String {
5637 let scope = format!("project:{}/{} is:issue", self.owner, self.project_number);
5638 match also {
5639 Some(also) => format!("{scope} {also}"),
5640 None => scope,
5641 }
5642 }
5643
5644 /// One issue this source reached directly, as the board item a read of the board would
5645 /// have produced — or `None` when this board does not hold it.
5646 ///
5647 /// The board half of an issue rides along on `Issue.projectItems`, so the value handed
5648 /// to [`Self::resolve`] is the very shape a `ProjectV2.items` read gives it: the board
5649 /// item's own id, that item's field values, and the issue as its content. One resolver
5650 /// for both routes is what makes an issue read through a search, through its own node
5651 /// id, or through its project's sub-issues report the same title, the same status, the
5652 /// same labels and the same qualified id.
5653 ///
5654 /// An issue with no entry for *this* board is not this source's to report, which is
5655 /// what keeps an id naming some other repository's issue from being answered as an item
5656 /// of this board. That answer is given about an **exhausted** connection and never
5657 /// about an unread page: the entry is looked for on the page in hand, and only if that
5658 /// page reports more of the connection, in [`Self::board_membership`]'s walk of the
5659 /// rest of it.
5660 async fn resolve_issue(&self, issue: &Value) -> Result<Option<Resolved>, SourceError> {
5661 self.resolve_issue_as(issue, true).await
5662 }
5663
5664 /// [`Self::resolve_issue`], remembering what it resolves for this command's later reads
5665 /// and writes only when `remember` says to.
5666 async fn resolve_issue_as(
5667 &self,
5668 issue: &Value,
5669 remember: bool,
5670 ) -> Result<Option<Resolved>, SourceError> {
5671 if optional_str(issue, "__typename")? != Some("Issue") {
5672 return Ok(None);
5673 }
5674 let memberships = issue
5675 .get("projectItems")
5676 .ok_or_else(|| SourceError::Malformed {
5677 message: "GitHub issue is missing projectItems".into(),
5678 })?;
5679 let nodes = memberships
5680 .get("nodes")
5681 .and_then(Value::as_array)
5682 .ok_or_else(|| SourceError::Malformed {
5683 message: "GitHub issue projectItems.nodes is not an array".into(),
5684 })?;
5685 let held = match self.board_entry(nodes) {
5686 Some(held) => held.clone(),
5687 None => {
5688 let info = memberships
5689 .get("pageInfo")
5690 .ok_or_else(|| SourceError::Malformed {
5691 message: "GitHub issue projectItems has no pageInfo".into(),
5692 })?;
5693 // The page held no entry for this board. Whether that means the issue is
5694 // not on it is a question about the rest of the connection, and only a
5695 // connection with no rest answers it here.
5696 if !required_bool(info, "hasNextPage")? {
5697 return Ok(None);
5698 }
5699 let cursor = required_str(info, "endCursor")?;
5700 validate_cursor_progress(None, cursor)?;
5701 let issue_id = required_str(issue, "id")?;
5702 match self.board_membership(issue_id, cursor).await? {
5703 Some(held) => held,
5704 None => return Ok(None),
5705 }
5706 }
5707 };
5708 // The membership's own `updatedAt` rides along: it is the board half of the item's change
5709 // token, which a field change moves where the issue's own time does not.
5710 let item = json!({
5711 "id": required_str(&held, "id")?,
5712 "updatedAt": held.get("updatedAt"),
5713 "project": held.get("project"),
5714 "fieldValues": held.get("fieldValues"),
5715 "content": issue,
5716 });
5717 if remember {
5718 self.resolve(&item)
5719 } else {
5720 self.resolve_unremembered(&item)
5721 }
5722 }
5723
5724 /// This board's own entry among one page of an issue's `Issue.projectItems`.
5725 ///
5726 /// One spelling of *which membership is this board's*, so the page a read carries and
5727 /// the pages [`Self::board_membership`] walks are searched by the same rule.
5728 fn board_entry<'a>(&self, nodes: &'a [Value]) -> Option<&'a Value> {
5729 nodes.iter().find(|node| {
5730 node.pointer("/project/number").and_then(Value::as_u64)
5731 == Some(u64::from(self.project_number))
5732 })
5733 }
5734
5735 /// The rest of one issue's board memberships, from `after`, for this board's entry.
5736 ///
5737 /// The recovery read: a page of memberships that holds no entry for this board says
5738 /// nothing about the memberships past it, so the connection is walked to exhaustion
5739 /// before an issue is reported as one this board does not hold. `Ok(None)` is that
5740 /// positive answer — the whole connection was read and no entry named this board —
5741 /// rather than a failure, and the walk is held to
5742 /// [`validate_cursor_progress`] like every other page walk here, so a source answering
5743 /// with a cursor that does not advance is refused instead of spun on.
5744 async fn board_membership(
5745 &self,
5746 issue: &str,
5747 after: &str,
5748 ) -> Result<Option<Value>, SourceError> {
5749 let mut after = after.to_owned();
5750 loop {
5751 let data = self
5752 .graphql(
5753 graphql::ISSUE_BOARD_ITEMS,
5754 json!({"id":issue,"first":MAX_PAGE_SIZE,"after":after,
5755 "nestedFirst":NESTED_PAGE_SIZE}),
5756 )
5757 .await?;
5758 let Some(connection) = data
5759 .pointer("/node/projectItems")
5760 .filter(|value| !value.is_null())
5761 else {
5762 // The id resolved to nothing, or to something with no memberships to walk —
5763 // which is the same answer as a connection holding no entry for this board.
5764 return Ok(None);
5765 };
5766 let nodes = connection
5767 .get("nodes")
5768 .and_then(Value::as_array)
5769 .ok_or_else(|| SourceError::Malformed {
5770 message: "GitHub issue projectItems.nodes is not an array".into(),
5771 })?;
5772 if let Some(held) = self.board_entry(nodes) {
5773 return Ok(Some(held.clone()));
5774 }
5775 let info = connection
5776 .get("pageInfo")
5777 .ok_or_else(|| SourceError::Malformed {
5778 message: "GitHub issue projectItems has no pageInfo".into(),
5779 })?;
5780 let next = required_bool(info, "hasNextPage")?
5781 .then(|| required_str(info, "endCursor"))
5782 .transpose()?;
5783 match next {
5784 Some(next) => {
5785 validate_cursor_progress(Some(&after), next)?;
5786 after = next.to_owned();
5787 }
5788 None => return Ok(None),
5789 }
5790 }
5791 }
5792
5793 /// One page of a board-scoped issue search, and where the next page resumes.
5794 async fn search_page(
5795 &self,
5796 search: &str,
5797 first: u32,
5798 after: Option<&str>,
5799 ) -> Result<(Vec<Resolved>, Option<String>), SourceError> {
5800 let data = self
5801 .graphql(
5802 graphql::SEARCH_ISSUES,
5803 json!({"search":search,"type":"ISSUE","first":first.min(MAX_PAGE_SIZE),
5804 "after":after,"nestedFirst":NESTED_PAGE_SIZE,
5805 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
5806 )
5807 .await?;
5808 let connection = data.get("search").ok_or_else(|| SourceError::Malformed {
5809 message: "GitHub search response has no search connection".into(),
5810 })?;
5811 let mut found = Vec::new();
5812 for node in connection
5813 .get("nodes")
5814 .and_then(Value::as_array)
5815 .ok_or_else(|| SourceError::Malformed {
5816 message: "GitHub search nodes is not an array".into(),
5817 })?
5818 {
5819 if let Some(resolved) = self.resolve_issue(node).await? {
5820 found.push(resolved);
5821 }
5822 }
5823 let info = connection
5824 .get("pageInfo")
5825 .ok_or_else(|| SourceError::Malformed {
5826 message: "GitHub search connection has no pageInfo".into(),
5827 })?;
5828 let next = required_bool(info, "hasNextPage")?
5829 .then(|| required_str(info, "endCursor"))
5830 .transpose()?
5831 .map(str::to_owned);
5832 if let Some(next) = &next {
5833 validate_cursor_progress(after, next)?;
5834 }
5835 Ok((found, next))
5836 }
5837
5838 /// Every issue this board holds, completed with what this run wrote.
5839 ///
5840 /// The completion is not an optimisation and it is not a cache: GitHub's issue search
5841 /// is an index and is eventually consistent, so an issue this run created seconds ago
5842 /// can be absent from it, and a project listed straight after being written would
5843 /// otherwise be missing from its own board. What is added back is only what this
5844 /// process itself wrote, out of [`Self::created`], which lives and dies with the
5845 /// process.
5846 async fn board_issues(&self) -> Result<Vec<Resolved>, SourceError> {
5847 let found = self.searched_issues().await?;
5848 self.completed_with_written(found, |_| true)
5849 }
5850
5851 /// Every issue this board's own search reports, walked to exhaustion, read once per
5852 /// source.
5853 ///
5854 /// The uncompleted half of [`Self::board_issues`], separated because [`Self::board`]
5855 /// needs it too and the two would otherwise walk the same search twice in one command.
5856 /// See [`Self::search_cache`] for why holding it is the same bargain holding the board
5857 /// is.
5858 async fn searched_issues(&self) -> Result<Vec<Resolved>, SourceError> {
5859 let cached = self.search_cache()?.clone();
5860 if let Some(held) = cached {
5861 return Ok(held);
5862 }
5863 let mut after: Option<String> = None;
5864 let mut found = Vec::new();
5865 let search = self.board_search(None);
5866 loop {
5867 let (page, next) = self
5868 .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
5869 .await?;
5870 found.extend(page);
5871 match next {
5872 Some(next) => after = Some(next),
5873 None => break,
5874 }
5875 }
5876 *self.search_cache()? = Some(found.clone());
5877 Ok(found)
5878 }
5879
5880 /// Every issue this board's own search reports with `also` beside its scope, walked to
5881 /// exhaustion — held for no later read, which may not be narrowed alike.
5882 async fn searched_narrowly(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
5883 let mut after: Option<String> = None;
5884 let mut found = Vec::new();
5885 let search = self.board_search(Some(also));
5886 loop {
5887 let (page, next) = self
5888 .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
5889 .await?;
5890 found.extend(page);
5891 match next {
5892 Some(next) => after = Some(next),
5893 None => return Ok(found),
5894 }
5895 }
5896 }
5897
5898 /// This process's own view of the board's issues, or the refusal a poisoned lock is.
5899 fn search_cache(
5900 &self,
5901 ) -> Result<std::sync::MutexGuard<'_, Option<Vec<Resolved>>>, SourceError> {
5902 self.search_cache
5903 .lock()
5904 .map_err(|_| SourceError::Unavailable {
5905 message: "this source's view of the board's issues was left inconsistent by an \
5906 earlier failure; next: run the command again"
5907 .into(),
5908 })
5909 }
5910
5911 /// `found`, with everything this run wrote that `keep` accepts and the read did not
5912 /// report.
5913 ///
5914 /// See [`Self::created`] and [`Self::board_issues`] for why a read has to be completed
5915 /// at all: the search index is behind, and a node read of an item filed moments ago can
5916 /// be too.
5917 fn completed_with_written(
5918 &self,
5919 mut found: Vec<Resolved>,
5920 keep: impl Fn(&Resolved) -> bool,
5921 ) -> Result<Vec<Resolved>, SourceError> {
5922 for own in self.created()?.iter().filter(|own| keep(own)) {
5923 if !found.iter().any(|item| item.id == own.id) {
5924 found.push(own.clone());
5925 }
5926 }
5927 Ok(found)
5928 }
5929
5930 /// What resolving one node id reached.
5931 ///
5932 /// Three answers rather than an `Option`, because a board *draft* is none of the other
5933 /// two: it is not an issue, so the issue fragment reads nothing of it, and a read of one
5934 /// is completed by a read of the draft itself rather than reported as nothing.
5935 async fn reach(&self, id: &NativeId) -> Result<Reached, SourceError> {
5936 let asked = self
5937 .graphql(
5938 graphql::ISSUE,
5939 json!({"id":id.0,"first":MAX_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
5940 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
5941 )
5942 .await;
5943 let data = match asked {
5944 Ok(data) => data,
5945 // A string that is not a node id at all is not a failure to report: it is an id
5946 // this board does not hold, which is what every read of one already answers.
5947 Err(error) if unresolvable_node(&error) => return Ok(Reached::Nothing),
5948 Err(error) => return Err(error),
5949 };
5950 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
5951 return Ok(Reached::Nothing);
5952 };
5953 if optional_str(node, "__typename")? == Some("DraftIssue") {
5954 return Ok(Reached::Draft);
5955 }
5956 Ok(match self.resolve_issue(node).await? {
5957 Some(item) => Reached::Held(Box::new(item)),
5958 None => Reached::Nothing,
5959 })
5960 }
5961
5962 /// One item of this board by its own id, whatever kind it is.
5963 ///
5964 /// Resolved from the identifier alone: no search, board-wide or otherwise. What this
5965 /// run wrote is read first, because a node read of an item created moments ago can
5966 /// still be behind the board field values written onto it — see [`Self::created`].
5967 async fn item_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
5968 if let Some(own) = self.created()?.iter().find(|own| own.id == *id) {
5969 return Ok(Some(own.clone()));
5970 }
5971 if let Some(held) = self.read_ahead()?.remove(id) {
5972 return Ok(held);
5973 }
5974 match self.reach(id).await? {
5975 Reached::Held(item) => Ok(Some(*item)),
5976 Reached::Nothing => Ok(None),
5977 Reached::Draft => self.draft_by_id(id).await,
5978 }
5979 }
5980
5981 /// [`Self::item_by_id`] for a document: one [`graphql::DOCUMENT_IN_PROJECT`] read, which
5982 /// answers the project it is filed under and that project's members beside it.
5983 ///
5984 /// Each is held for this command as a read of it would have been: the project for the
5985 /// next read of it by its id, and — when its page of members is the whole of them — the
5986 /// members for the next listing of that project. So a write held to who may read the
5987 /// document, which reads all three, is answered by this one request.
5988 async fn document_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
5989 if self.created()?.iter().any(|own| own.id == *id) || self.read_ahead()?.contains_key(id) {
5990 return self.item_by_id(id).await;
5991 }
5992 let asked = self
5993 .graphql(
5994 graphql::DOCUMENT_IN_PROJECT,
5995 json!({"id":id.0,"first":MAX_PAGE_SIZE,"memberFirst":MEMBER_PAGE_SIZE,
5996 "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
5997 "duplicates":true}),
5998 )
5999 .await;
6000 let data = match asked {
6001 Ok(data) => data,
6002 Err(error) if unresolvable_node(&error) => return Ok(None),
6003 Err(error) => return Err(error),
6004 };
6005 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
6006 return Ok(None);
6007 };
6008 if optional_str(node, "__typename")? == Some("DraftIssue") {
6009 return self.draft_by_id(id).await;
6010 }
6011 let Some(item) = self.resolve_issue(node).await? else {
6012 return Ok(None);
6013 };
6014 // An answer for another issue than the one asked about says nothing of that one.
6015 if item.id != *id {
6016 return Err(SourceError::Malformed {
6017 message: format!(
6018 "GitHub answered issue {} when asked for document {}",
6019 item.id.0, id.0
6020 ),
6021 });
6022 }
6023 if let Some(project) = node.get("project").filter(|value| !value.is_null())
6024 && let Some(parent) = self
6025 .resolve_issue(project)
6026 .await?
6027 .filter(|parent| Some(&parent.id) == item.parent.as_ref())
6028 {
6029 let members = project.get("subIssues").filter(|value| !value.is_null());
6030 let whole = members
6031 .and_then(|members| members.pointer("/pageInfo/hasNextPage"))
6032 .and_then(Value::as_bool)
6033 == Some(false);
6034 if whole {
6035 let nodes = members
6036 .and_then(|members| members.get("nodes"))
6037 .and_then(Value::as_array)
6038 .ok_or_else(|| SourceError::Malformed {
6039 message: "GitHub subIssues.nodes is not an array".into(),
6040 })?;
6041 let mut children = Vec::new();
6042 for member in nodes {
6043 if let Some(resolved) = self.resolve_issue(member).await? {
6044 children.push(resolved);
6045 }
6046 }
6047 // Held as the project's members only when every one resolved and says it is
6048 // filed under it: a page with any other answers nothing of all the members.
6049 if children.len() == nodes.len()
6050 && children
6051 .iter()
6052 .all(|child| child.parent.as_ref() == Some(&parent.id))
6053 {
6054 self.children_cache()?
6055 .insert(parent.id.clone(), (parent.id.clone(), children));
6056 }
6057 }
6058 self.read_ahead()?.insert(parent.id.clone(), Some(parent));
6059 }
6060 Ok(Some(item))
6061 }
6062
6063 /// Several items of this board, each by its own id, in order — what [`Self::item_by_id`]
6064 /// answers for each, read [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] rather
6065 /// than one request per id.
6066 ///
6067 /// What this run wrote answers first, as it does there, and only the rest is read. One id
6068 /// left to read is read by [`Self::item_by_id`] itself, which costs what a batch does. A
6069 /// batch GitHub refuses because one of its ids resolves to no node at all is read again one
6070 /// id at a time, so that id is answered as not held and the others as themselves; a draft
6071 /// is completed by a read of the draft, exactly as there.
6072 async fn items_by_ids(&self, ids: &[NativeId]) -> Result<Vec<Option<Resolved>>, SourceError> {
6073 let mut found: Vec<Option<Option<Resolved>>> = {
6074 let created = self.created()?;
6075 ids.iter()
6076 .map(|id| {
6077 created
6078 .iter()
6079 .find(|own| own.id == *id)
6080 .map(|own| Some(own.clone()))
6081 })
6082 .collect()
6083 };
6084 let unread: Vec<NativeId> = ids
6085 .iter()
6086 .zip(&found)
6087 .filter(|(_, found)| found.is_none())
6088 .map(|(id, _)| id.clone())
6089 .collect();
6090 let mut read = Vec::with_capacity(unread.len());
6091 if let [one] = unread.as_slice() {
6092 read.push(self.item_by_id(one).await?);
6093 } else {
6094 for batch in unread.chunks(DETAIL_BATCH) {
6095 let data = match self
6096 .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, None))
6097 .await
6098 {
6099 Ok(data) => data,
6100 Err(error) if unresolvable_node(&error) => {
6101 for id in batch {
6102 read.push(self.item_by_id(id).await?);
6103 }
6104 continue;
6105 }
6106 Err(error) => return Err(error),
6107 };
6108 for (slot, id) in batch.iter().enumerate() {
6109 let node =
6110 data.get(format!("i{slot}"))
6111 .ok_or_else(|| SourceError::Malformed {
6112 message: format!(
6113 "GitHub answered a batch read with no item for {}",
6114 id.0
6115 ),
6116 })?;
6117 read.push(if node.is_null() {
6118 None
6119 } else if optional_str(node, "__typename")? == Some("DraftIssue") {
6120 self.draft_by_id(id).await?
6121 } else {
6122 if optional_str(node, "__typename")? == Some("Issue")
6123 && required_str(node, "id")? != id.0
6124 {
6125 return Err(SourceError::Malformed {
6126 message: format!(
6127 "GitHub answered the read of {} with issue {}",
6128 id.0,
6129 required_str(node, "id")?
6130 ),
6131 });
6132 }
6133 self.resolve_issue(node).await?
6134 });
6135 }
6136 }
6137 }
6138 let mut read = read.into_iter();
6139 Ok(found
6140 .iter_mut()
6141 .map(|slot| slot.take().unwrap_or_else(|| read.next().flatten()))
6142 .collect())
6143 }
6144
6145 fn read_ahead(
6146 &self,
6147 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Option<Resolved>>>, SourceError> {
6148 self.read_ahead
6149 .lock()
6150 .map_err(|_| SourceError::Unavailable {
6151 message:
6152 "items read ahead for a copy were left inconsistent; run the command again"
6153 .into(),
6154 })
6155 }
6156
6157 fn resolved_cache(
6158 &self,
6159 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
6160 self.resolved_cache
6161 .lock()
6162 .map_err(|_| SourceError::Unavailable {
6163 message: "resolved item records were left inconsistent; run the command again"
6164 .into(),
6165 })
6166 }
6167
6168 /// Reuse a record this invocation already resolved. The mutation sender invalidates
6169 /// it before writing, so a partial failure cannot leave a pre-write binding behind.
6170 async fn bound_item(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6171 let cached = self.resolved_cache()?.get(id).cloned();
6172 if let Some(item) = cached {
6173 return Ok(Some(item));
6174 }
6175 self.item_by_id(id).await
6176 }
6177
6178 /// Take out the item an issue read by reference reached for `id`, when one is held: the
6179 /// status write it was read for is the one caller, so no other read or write is ever
6180 /// answered from a read that selected only what that write needs.
6181 fn take_held(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6182 Ok(self
6183 .held_cache
6184 .lock()
6185 .map_err(|_| SourceError::Unavailable {
6186 message: "items read by issue reference were left inconsistent; run the \
6187 command again"
6188 .into(),
6189 })?
6190 .remove(id))
6191 }
6192
6193 /// One board draft by its own id, with the board item it sits in — or `None` when no
6194 /// item of this board is that draft's.
6195 ///
6196 /// The same decision [`Self::resolve_issue`] makes for an issue, over the draft's own
6197 /// `projectV2Items`: an entry naming this board is what makes it this board's. GitHub
6198 /// links a draft to one board item, so the page this read carries is the whole of that
6199 /// connection, and a page that reports more than it holds is refused rather than read
6200 /// as an answer about memberships nobody read.
6201 async fn draft_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6202 let data = self
6203 .graphql(
6204 graphql::DRAFT,
6205 json!({"id":id.0,"nestedFirst":NESTED_PAGE_SIZE,
6206 "boardItems":BOARD_ITEMS_PAGE_SIZE}),
6207 )
6208 .await?;
6209 // Gone between the two reads is an answer — the draft is no longer there. Anything
6210 // else than the draft [`Self::reach`] was just told this id is, is not one.
6211 let Some(draft) = data.get("node").filter(|node| !node.is_null()) else {
6212 return Ok(None);
6213 };
6214 if optional_str(draft, "__typename")? != Some("DraftIssue") {
6215 return Err(SourceError::Malformed {
6216 message: format!(
6217 "GitHub answered {} as a draft and then as something else",
6218 id.0
6219 ),
6220 });
6221 }
6222 if required_str(draft, "id")? != id.0 {
6223 return Err(SourceError::Malformed {
6224 message: format!("GitHub answered a different draft for {}", id.0),
6225 });
6226 }
6227 let memberships = draft
6228 .get("projectV2Items")
6229 .ok_or_else(|| SourceError::Malformed {
6230 message: format!("GitHub draft {} is missing projectV2Items", id.0),
6231 })?;
6232 let nodes = memberships
6233 .get("nodes")
6234 .and_then(Value::as_array)
6235 .ok_or_else(|| SourceError::Malformed {
6236 message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
6237 })?;
6238 let info = memberships
6239 .get("pageInfo")
6240 .ok_or_else(|| SourceError::Malformed {
6241 message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
6242 })?;
6243 // Read whether or not this board's entry is on the page: a page claiming more than
6244 // the one item GitHub links a draft to is a malformed answer either way.
6245 if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
6246 return Err(SourceError::Malformed {
6247 message: format!(
6248 "GitHub draft {} reports more board items than the one GitHub links a draft \
6249 to",
6250 id.0
6251 ),
6252 });
6253 }
6254 if let Some(node) = nodes.first()
6255 && node
6256 .pointer("/project/number")
6257 .and_then(Value::as_u64)
6258 .is_none()
6259 {
6260 return Err(SourceError::Malformed {
6261 message: format!(
6262 "GitHub draft {} board item has no numeric project number",
6263 id.0
6264 ),
6265 });
6266 }
6267 let Some(held) = self.board_entry(nodes) else {
6268 return Ok(None);
6269 };
6270 if required_str(
6271 held.get("project").ok_or_else(|| SourceError::Malformed {
6272 message: format!("GitHub draft {} board item has no project", id.0),
6273 })?,
6274 "id",
6275 )? != self.board_fields().await?.id.as_str()
6276 {
6277 return Ok(None);
6278 }
6279 let item = json!({
6280 "id": required_str(held, "id")?,
6281 "project": held.get("project"),
6282 "fieldValues": held.get("fieldValues"),
6283 "content": draft,
6284 });
6285 self.resolve(&item)
6286 }
6287
6288 /// The board's own id and field definitions, for a write whose item does not carry
6289 /// them — never its items.
6290 ///
6291 /// A board this command has already listed supplies them, since it read them beside its
6292 /// items; otherwise they come from [`graphql::BOARD_FIELDS`], once per command. Neither
6293 /// is consulted about which items the board holds: see the module documentation for
6294 /// why a question about one known item is answered by reading that item.
6295 async fn board_fields(&self) -> Result<BoardFields, SourceError> {
6296 if let Some(board) = self.board_cache()?.as_ref() {
6297 return Ok(BoardFields {
6298 id: BoardId::parse(&board.id)?,
6299 fields: board.fields.clone(),
6300 });
6301 }
6302 if let Some(held) = self.fields_cache()?.clone() {
6303 return Ok(held);
6304 }
6305 let data = self
6306 .graphql(
6307 graphql::BOARD_FIELDS,
6308 json!({"owner":self.owner,"number":self.project_number,
6309 "nestedFirst":NESTED_PAGE_SIZE}),
6310 )
6311 .await?;
6312 self.fields_read(&data)
6313 }
6314
6315 /// The board's id and fields out of an answer carrying the `boardFields` root, held for
6316 /// the rest of this command.
6317 fn fields_read(&self, data: &Value) -> Result<BoardFields, SourceError> {
6318 let board = data
6319 .pointer("/boardFields/projectV2")
6320 .filter(|value| !value.is_null())
6321 .ok_or_else(|| SourceError::Refused {
6322 message: format!(
6323 "GitHub project {}/{} was not found or is not visible to the token",
6324 self.owner, self.project_number
6325 ),
6326 })?;
6327 let read = BoardFields {
6328 id: BoardId::parse(required_str(board, "id")?)?,
6329 fields: board.get("fields").cloned().unwrap_or(Value::Null),
6330 };
6331 *self.fields_cache()? = Some(read.clone());
6332 Ok(read)
6333 }
6334
6335 /// Read what creating an issue in `repository` needs and this command has not read yet —
6336 /// the board's fields and the repository's node id — in one request when it needs both.
6337 ///
6338 /// When either is already known this sends nothing, and the other is read by its own
6339 /// document where it is asked for, so no create reads anything twice.
6340 async fn creation_context(
6341 &self,
6342 repository: &RepositoryTarget,
6343 incoming: &Incoming<'_>,
6344 ) -> Result<(), SourceError> {
6345 let fields_known = self.board_cache()?.is_some() || self.fields_cache()?.is_some();
6346 if fields_known || self.repository_cache()?.contains_key(repository) {
6347 return Ok(());
6348 }
6349 // A placement an earlier process kept is taken only while the project issue this create
6350 // has just read shows its setup-context token unchanged; see `projection`.
6351 if self.placed_by_kept(repository, incoming.parent) {
6352 return Ok(());
6353 }
6354 let data = self
6355 .graphql(
6356 graphql::CREATION_CONTEXT,
6357 json!({"owner":self.owner,"number":self.project_number,
6358 "nestedFirst":NESTED_PAGE_SIZE,"repositoryOwner":repository.owner,
6359 "repositoryName":repository.name}),
6360 )
6361 .await?;
6362 self.fields_read(&data)?;
6363 self.repository_read(&data, repository, incoming)?;
6364 Ok(())
6365 }
6366
6367 /// This process's own view of the board's fields, or the refusal a poisoned lock is.
6368 fn fields_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<BoardFields>>, SourceError> {
6369 self.fields_cache
6370 .lock()
6371 .map_err(|_| SourceError::Unavailable {
6372 message: "this source's view of the board's fields was left inconsistent by an \
6373 earlier failure; next: run the command again"
6374 .into(),
6375 })
6376 }
6377
6378 /// What a write to `item` needs of the board, read off that item when it says enough and
6379 /// off [`Self::board_fields`] when it does not.
6380 ///
6381 /// A node read of an item names its board and carries the definition of every field it
6382 /// holds a value of — so an item naming its board, holding a value of the origin field
6383 /// when the write carries an origin, and, when the write carries a status, holding a
6384 /// `Status` value, needs no read of the board at all. **Nothing the item does not say is
6385 /// guessed:** a field it holds no value of may still be on the board, and a view reading it
6386 /// as absent would refuse a write the board can take or skip a field write the board
6387 /// needs, so such an item — and a create, which has no item yet — takes the board's fields
6388 /// from their own read instead. A write carrying no origin to an item holding none — every
6389 /// write out of a source declared private — writes no origin whatever the board defines,
6390 /// so it needs no definition of that field.
6391 async fn fields_for(
6392 &self,
6393 item: Option<&Resolved>,
6394 writes_origin: bool,
6395 writes_status: bool,
6396 selects_priority: bool,
6397 ) -> Result<BoardFields, SourceError> {
6398 if let Some(board) = item.and_then(Resolved::carried_board) {
6399 return Ok(board);
6400 }
6401 if let Some(item) = item
6402 && let Some(board_id) = item.named_board()
6403 && (!writes_origin || item.defines(ORIGIN_FIELD))
6404 && (!writes_status || item.defines("Status"))
6405 && (!selects_priority || item.defines(PRIORITY_FIELD))
6406 && self
6407 .metadata_fields
6408 .iter()
6409 .all(|projection| item.defines(&projection.field))
6410 {
6411 return Ok(BoardFields {
6412 id: board_id,
6413 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
6414 });
6415 }
6416 self.board_fields().await
6417 }
6418
6419 /// Everything filed under one issue of this board, walked to exhaustion — or `None`
6420 /// when that id names nothing here with a sub-issue relationship to walk.
6421 ///
6422 /// `None` and an empty answer are different: `None` is *this is not an issue of this
6423 /// GitHub*, which is what sends a project selector on to be read as a name, and an
6424 /// empty vector is a project that holds nothing.
6425 async fn sub_issues(&self, id: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
6426 let mut after: Option<String> = None;
6427 let mut children = Vec::new();
6428 // The project issue as the first page carried it, held once the walk has ended.
6429 let mut parent: Option<Resolved> = None;
6430 loop {
6431 let asked = self
6432 .graphql(
6433 graphql::SUB_ISSUES,
6434 json!({"id":id.0,"memberFirst":MEMBER_PAGE_SIZE,"after":after,
6435 "nestedFirst":NESTED_PAGE_SIZE,
6436 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
6437 )
6438 .await;
6439 let data = match asked {
6440 Ok(data) => data,
6441 // A string that is not a node id at all is not a failure to report: it is
6442 // the ordinary answer to a selector naming a project by its name.
6443 Err(error) if unresolvable_node(&error) => return Ok(None),
6444 Err(error) => return Err(error),
6445 };
6446 let Some(connection) = data
6447 .pointer("/node/subIssues")
6448 .filter(|value| !value.is_null())
6449 else {
6450 // No such node, or one with no sub-issue relationship — a board draft is
6451 // the one this board can really hold.
6452 return Ok(None);
6453 };
6454 if after.is_none()
6455 && let Some(node) = data.get("node").filter(|node| node.get("id").is_some())
6456 {
6457 parent = self
6458 .resolve_issue(node)
6459 .await?
6460 .filter(|resolved| resolved.id == *id);
6461 }
6462 for node in connection
6463 .get("nodes")
6464 .and_then(Value::as_array)
6465 .ok_or_else(|| SourceError::Malformed {
6466 message: "GitHub subIssues.nodes is not an array".into(),
6467 })?
6468 {
6469 if let Some(resolved) = self.resolve_issue(node).await? {
6470 children.push(resolved);
6471 }
6472 }
6473 let info = connection
6474 .get("pageInfo")
6475 .ok_or_else(|| SourceError::Malformed {
6476 message: "GitHub subIssues connection has no pageInfo".into(),
6477 })?;
6478 let next = required_bool(info, "hasNextPage")?
6479 .then(|| required_str(info, "endCursor"))
6480 .transpose()?;
6481 match next {
6482 Some(next) => {
6483 validate_cursor_progress(after.as_deref(), next)?;
6484 after = Some(next.to_owned());
6485 }
6486 None => {
6487 // Held only once the walk has ended, so one failing part way holds
6488 // nothing and every read it would have answered is made.
6489 // The project issue the first page carried answers the next read of the
6490 // project by its id in this command, as a read of it made now would: what
6491 // classifying a project for a write reads straight after its members.
6492 if let Some(parent) = &parent {
6493 self.read_ahead()?
6494 .entry(parent.id.clone())
6495 .or_insert_with(|| Some(parent.clone()));
6496 }
6497 let mut carried = self.carried()?;
6498 for item in parent.into_iter().chain(children.iter().cloned()) {
6499 carried.insert(item.id.clone(), item);
6500 }
6501 return Ok(Some(children));
6502 }
6503 }
6504 }
6505 }
6506
6507 fn carried(
6508 &self,
6509 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
6510 self.carried.lock().map_err(|_| SourceError::Unavailable {
6511 message: "items a project's listing carried were left inconsistent; run the \
6512 command again"
6513 .into(),
6514 })
6515 }
6516
6517 /// Which issue of this board a project *name* is, or `None` when none is.
6518 ///
6519 /// One bounded query which filters on that name at the server, rather than a walk of
6520 /// every issue the board holds. The name is compared again here: the qualifier narrows
6521 /// what GitHub sends, and this source decides what it names.
6522 async fn project_by_name(&self, name: &str) -> Result<Option<NativeId>, SourceError> {
6523 let search = self.board_search(Some(&title_qualifier(name)));
6524 let mut after = None;
6525 loop {
6526 let (candidates, next) = self
6527 .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
6528 .await?;
6529 if let Some(item) = candidates.into_iter().find(|item| {
6530 item.kind == BoardKind::Work(ItemKind::Project)
6531 && item.title.eq_ignore_ascii_case(name)
6532 }) {
6533 return Ok(Some(item.id));
6534 }
6535 match next {
6536 Some(next) => after = Some(next),
6537 None => return Ok(None),
6538 }
6539 }
6540 }
6541
6542 /// Everything filed under one project of this board: the sub-issues of the issue that
6543 /// project is.
6544 ///
6545 /// Tasks *and* documents, because a document filed under a project is a sub-issue of it
6546 /// too — the caller keeps the kind it asked for. Nothing about this grows as the board
6547 /// gains projects, or as another project gains tasks.
6548 ///
6549 /// A qualified id names the issue and is asked for its sub-issues directly: one
6550 /// request, no search of any kind. Only a selector GitHub cannot resolve that way is
6551 /// read as a project *name*, which costs the one bounded search
6552 /// [`Self::project_by_name`] makes.
6553 ///
6554 /// What GitHub answered is held for the rest of the command — see [`Self::children_cache`]
6555 /// — and each answer is still cut to the items whose parent is this project, so one this
6556 /// process has since filed elsewhere is not reported here, and completed with what this
6557 /// process filed under it.
6558 async fn project_children(&self, selector: &NativeId) -> Result<Vec<Resolved>, SourceError> {
6559 let held = self.children_cache()?.get(selector).cloned();
6560 let (project, children) = match held {
6561 Some(held) => held,
6562 None => {
6563 let answered = match self.sub_issues(selector).await? {
6564 Some(children) => (selector.clone(), children),
6565 None => match self.project_by_name(&selector.0).await? {
6566 Some(project) => {
6567 let children = self.sub_issues(&project).await?.unwrap_or_default();
6568 (project, children)
6569 }
6570 None => return Ok(Vec::new()),
6571 },
6572 };
6573 self.children_cache()?
6574 .insert(selector.clone(), answered.clone());
6575 answered
6576 }
6577 };
6578 let mut children: Vec<Resolved> = children
6579 .into_iter()
6580 .filter(|child| child.parent.as_ref() == Some(&project))
6581 .collect();
6582 for own in self.updated()?.iter() {
6583 if own.parent.as_ref() == Some(&project) && !children.iter().any(|c| c.id == own.id) {
6584 children.push(own.clone());
6585 }
6586 }
6587 self.completed_with_written(children, |own| own.parent.as_ref() == Some(&project))
6588 }
6589
6590 /// Every issue of this board GitHub's issue search reports updated at or after `since`,
6591 /// completed with what this run wrote — the candidates a comment-activity read confirms.
6592 ///
6593 /// Scoped by the board and by nothing else: `project:<owner>/<number>` reaches every issue
6594 /// on the board whatever repository, and whatever owner, it lives in, so no repository or
6595 /// owner qualifier is added and none is needed. What makes the `updated:` qualifier
6596 /// sufficient is a fact about GitHub rather than about this source: a comment written on an
6597 /// issue **and a comment edited on it** both move that issue's `updatedAt`. The credentialed
6598 /// journey `an_edited_comment_moves_its_issue_and_is_selected_since` in `tests/journey`
6599 /// re-takes that fact on every run of the lane, so a change on GitHub's side fails there
6600 /// rather than silently narrowing a caller's answer.
6601 ///
6602 /// The instant is written to the second, rounded down, which can only widen what the
6603 /// search returns; confirmation against each candidate's own comments is what makes the
6604 /// answer exact. The search is an index that lags a write by a second or two — the module
6605 /// documentation records it — so a caller that asks again from its last instant should
6606 /// overlap the two by more than that.
6607 async fn updated_since(&self, since: DateTime<Utc>) -> Result<Vec<Resolved>, SourceError> {
6608 let found = self.searched(&updated_qualifier(since)).await?;
6609 self.completed_with_written(found, |_| true)
6610 }
6611
6612 /// Every issue of this board GitHub's issue search reports for the board-scoped search
6613 /// narrowed by `also`, in pages of [`SEARCH_PAGE_SIZE`].
6614 ///
6615 /// Uncompleted: what this process wrote is added by the caller, which knows whether its
6616 /// own record is the fresher of the two.
6617 async fn searched(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
6618 let search = self.board_search(Some(also));
6619 let mut after: Option<String> = None;
6620 let mut found = Vec::new();
6621 loop {
6622 let (page, next) = self
6623 .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
6624 .await?;
6625 found.extend(page);
6626 match next {
6627 Some(next) => after = Some(next),
6628 None => return Ok(found),
6629 }
6630 }
6631 }
6632
6633 /// A bounded task answer; the versioned cursor carries the connection position, how
6634 /// many rows of the page starting there were already handed out, and the own-write ids
6635 /// already observed, including across a new source instance.
6636 ///
6637 /// Every page is sent at [`SEARCH_PAGE_SIZE`] whatever the caller's limit, and a limit is
6638 /// sliced from the pages it needs; why is the module documentation's paging contract.
6639 async fn search_tasks(
6640 &self,
6641 query: &TaskQuery,
6642 page: &PageRequest,
6643 also: &str,
6644 ) -> Result<Page<Task>, SourceError> {
6645 let mut position = match &page.cursor {
6646 None => SearchPosition::default(),
6647 Some(cursor) => serde_json::from_str::<SearchPosition>(&cursor.0)
6648 .ok()
6649 .filter(|position| {
6650 position.version == SEARCH_CURSOR_VERSION
6651 && position.connection.valid_resume(position.offset)
6652 })
6653 .ok_or_else(|| SourceError::Config {
6654 message: "page cursor is invalid".into(),
6655 })?,
6656 };
6657 let search = self.board_search(Some(also));
6658 let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
6659 let own = self.with_own_writes(Vec::new())?;
6660 // An issue this process commented on is a candidate of a comment-activity read
6661 // whether or not the search has caught up with the comment; see `Self::commented`.
6662 let commented = match query.commented_since {
6663 Some(_) => self.commented()?.clone(),
6664 None => Vec::new(),
6665 };
6666 for id in own.iter().map(|item| &item.id).chain(&commented) {
6667 if !position.own.contains(id) {
6668 position.own.push(id.clone());
6669 }
6670 }
6671 let mut tasks = Vec::new();
6672 while !position.connection.exhausted() && tasks.len() < limit {
6673 let first = SEARCH_PAGE_SIZE;
6674 // Page size is part of the key: a short cached answer cannot answer a wider ask.
6675 let key =
6676 serde_json::to_string(&("page", &search, &position.connection.after(), first))
6677 .expect("search page key is serializable");
6678 let cached = if query.commented_since.is_none() {
6679 self.narrowed_cache()?.get(&key).cloned()
6680 } else {
6681 None
6682 };
6683 let (found, next) = match cached {
6684 Some(found) => {
6685 let next = self
6686 .search_next
6687 .lock()
6688 .map_err(|_| SourceError::Unavailable {
6689 message:
6690 "search pagination was left inconsistent; run the command again"
6691 .into(),
6692 })?
6693 .get(&key)
6694 .cloned()
6695 .flatten();
6696 (found, next)
6697 }
6698 None => {
6699 let (found, next) = self
6700 .search_page(&search, first, position.connection.after())
6701 .await?;
6702 if query.commented_since.is_none() {
6703 self.search_next
6704 .lock()
6705 .map_err(|_| SourceError::Unavailable {
6706 message:
6707 "search pagination was left inconsistent; run the command again"
6708 .into(),
6709 })?
6710 .insert(key.clone(), next.clone());
6711 self.narrowed_cache()?.insert(key, found.clone());
6712 }
6713 (found, next)
6714 }
6715 };
6716 let rows = found.len();
6717 for mut item in found.into_iter().skip(position.offset) {
6718 if tasks.len() == limit {
6719 break;
6720 }
6721 position.offset += 1;
6722 if position.own.contains(&item.id) {
6723 if position.seen.contains(&item.id) {
6724 continue;
6725 }
6726 position.seen.push(item.id.clone());
6727 // The search's own copy of an issue this process only commented on is as
6728 // good as a node read of it, since its comments are read either way.
6729 let only_commented = commented.contains(&item.id)
6730 && !own.iter().any(|written| written.id == item.id);
6731 if !only_commented {
6732 let updated_at = item.updated_at;
6733 let Some(written) = self.search_written(&own, &item.id).await? else {
6734 continue;
6735 };
6736 item = written;
6737 item.updated_at = item.updated_at.max(updated_at);
6738 self.resolved_cache()?.insert(item.id.clone(), item.clone());
6739 }
6740 }
6741 if item.kind == BoardKind::Work(ItemKind::Task) {
6742 let task = item.task()?;
6743 if task_matches(&task, query, &query.project)
6744 && self.commented_since(&item, query.commented_since).await?
6745 {
6746 tasks.push(task);
6747 }
6748 }
6749 }
6750 if position.offset < rows {
6751 continue;
6752 }
6753 position.offset = 0;
6754 position.connection = match next {
6755 Some(after) => SearchConnection::Continuing {
6756 after: Cursor(after),
6757 },
6758 None => SearchConnection::Exhausted {},
6759 };
6760 }
6761 if position.connection.exhausted() {
6762 for id in position.own.clone() {
6763 if position.seen.contains(&id) {
6764 continue;
6765 }
6766 if tasks.len() == limit {
6767 break;
6768 }
6769 position.seen.push(id.clone());
6770 let Some(item) = self.search_written(&own, &id).await? else {
6771 continue;
6772 };
6773 if item.kind == BoardKind::Work(ItemKind::Task) {
6774 let task = item.task()?;
6775 if task_matches(&task, query, &query.project)
6776 && self.commented_since(&item, query.commented_since).await?
6777 {
6778 tasks.push(task);
6779 }
6780 }
6781 }
6782 }
6783 let more = !position.connection.exhausted()
6784 || position.own.iter().any(|id| !position.seen.contains(id));
6785 Ok(Page {
6786 items: tasks,
6787 next: more.then(|| {
6788 Cursor(serde_json::to_string(&position).expect("search position is serializable"))
6789 }),
6790 })
6791 }
6792
6793 /// A resumed process has the ids but no write records; resolve only a record the
6794 /// current page needs, by its uncached node read rather than the lagging search index.
6795 async fn search_written(
6796 &self,
6797 own: &[Resolved],
6798 id: &NativeId,
6799 ) -> Result<Option<Resolved>, SourceError> {
6800 match own.iter().find(|item| item.id == *id) {
6801 Some(item) => Ok(Some(item.clone())),
6802 None => self.item_by_id(id).await,
6803 }
6804 }
6805
6806 /// The candidates for a task query carrying a text, metadata or origin predicate, read
6807 /// without enumerating the board — or `None` for a query carrying none of the three, which
6808 /// keeps the reads it always had.
6809 ///
6810 /// An origin is answered by [`Self::origin_candidates`], whatever else the query carries,
6811 /// because it names at most a handful of items. Text and metadata are answered by one
6812 /// board-scoped issue search carrying every term — see [`narrowing_qualifiers`] — narrowed
6813 /// further by `updated:>=` when the query also asks for comment activity, since both
6814 /// qualifiers must hold of an issue the answer keeps. Every candidate is confirmed in
6815 /// process afterwards by the same predicates [`task_matches`] applies to every read.
6816 ///
6817 /// Completed with what this process wrote, its own record winning over the index's copy
6818 /// of the same item: see [`Self::with_own_writes`].
6819 async fn narrowed(&self, query: &TaskQuery) -> Result<Option<Vec<Resolved>>, SourceError> {
6820 self.send_pending_origins(None).await?;
6821 let asked = match (&query.origin, narrowing_qualifiers(query)) {
6822 (Some(origin), _) => Narrowing::Origin(origin.clone()),
6823 (None, Some(qualifiers)) => Narrowing::Search(match query.commented_since {
6824 Some(since) => format!("{} {qualifiers}", updated_qualifier(since)),
6825 None => qualifiers,
6826 }),
6827 (None, None) => return Ok(None),
6828 };
6829 // A question about comment activity is asked afresh every time, as it always was: it
6830 // is the one a caller polls from one source while waiting for the index, and an
6831 // answer held from the first poll would be the answer to every later one.
6832 let key = query.commented_since.is_none().then(|| asked.key());
6833 let cached = match &key {
6834 Some(key) => self.narrowed_cache()?.get(key).cloned(),
6835 None => None,
6836 };
6837 let found = match cached {
6838 Some(found) => found,
6839 None => {
6840 let found = match &asked {
6841 Narrowing::Origin(origin) => self.origin_candidates(origin).await?,
6842 Narrowing::Search(also) => self.searched(also).await?,
6843 };
6844 if let Some(key) = key {
6845 self.narrowed_cache()?.insert(key, found.clone());
6846 }
6847 found
6848 }
6849 };
6850 self.with_own_writes(found).map(Some)
6851 }
6852
6853 /// The candidates for a project or unscoped document query carrying a searchable text,
6854 /// read without enumerating the board — or `None` for a query with no text or a blank one,
6855 /// which keeps the read it always had.
6856 ///
6857 /// The text is sent as the very phrase a task query's text is — see [`text_qualifiers`] —
6858 /// in one board-scoped issue search walked to its end at [`SEARCH_PAGE_SIZE`], so what it
6859 /// costs is the issues that match and never the board. Its answer is held for the command
6860 /// under the same key [`Self::narrowed`] holds that search under, so a walk of the caller's
6861 /// pages asks GitHub once. Every candidate is confirmed afterwards by its kind and by the
6862 /// substring rule, exactly as an item of the wider read was, and is completed with what this
6863 /// process wrote: see [`Self::with_own_writes`].
6864 async fn text_searched(
6865 &self,
6866 text: Option<&TextQuery>,
6867 ) -> Result<Option<Vec<Resolved>>, SourceError> {
6868 let Some(also) = text_qualifiers(text) else {
6869 return Ok(None);
6870 };
6871 let key = Narrowing::Search(also.clone()).key();
6872 let cached = self.narrowed_cache()?.get(&key).cloned();
6873 let found = match cached {
6874 Some(found) => found,
6875 None => {
6876 let found = self.searched(&also).await?;
6877 self.narrowed_cache()?.insert(key, found.clone());
6878 found
6879 }
6880 };
6881 self.with_own_writes(found).map(Some)
6882 }
6883
6884 /// Every `kind` item of this board whose own origin is exactly `origin`: the candidates
6885 /// [`Self::narrowed`] reads for a task query carrying that origin — one
6886 /// [`graphql::ORIGIN_LOOKUP`] walk, held for the command under the same key, completed with
6887 /// what this process wrote — each confirmed against its own origin entry.
6888 ///
6889 /// What answers a project or document listing by origin, so finding every copy of one plan
6890 /// costs the items that carry its origin and never a page of the board.
6891 async fn copied_from(
6892 &self,
6893 origin: &str,
6894 kind: BoardKind,
6895 ) -> Result<Vec<Resolved>, SourceError> {
6896 let wanted = TaskQuery {
6897 origin: Some(origin.to_owned()),
6898 ..TaskQuery::default()
6899 };
6900 Ok(self
6901 .narrowed(&wanted)
6902 .await?
6903 .unwrap_or_default()
6904 .into_iter()
6905 .filter(|item| item.kind == kind && wanted.origin_matches(&item.metadata()))
6906 .collect())
6907 }
6908
6909 /// Every item of this board that may carry `origin` — a superset of those that do — found
6910 /// by [`graphql::ORIGIN_LOOKUP`] and never by enumerating the board.
6911 ///
6912 /// The union of the board's own field filter over the `onetaskgraph.origin` text field —
6913 /// which reads the field every carrier holds, whichever release wrote it — and the
6914 /// board-scoped issue search for the same id as a phrase in the body, where this source
6915 /// mirrors it. The caller adds the third read, this process's own writes. Candidates are
6916 /// returned unconfirmed; [`task_matches`] compares each one's own origin field with the
6917 /// query's, exactly.
6918 ///
6919 /// Both connections are walked to exhaustion, each from its own cursor. One that has
6920 /// already ended is sent its last cursor again, which answers an empty page, so the one
6921 /// document serves every page of either. What the two leave is stated in the module
6922 /// documentation: a carrier another process added within the last second or two, before
6923 /// either index has it.
6924 async fn origin_candidates(&self, origin: &str) -> Result<Vec<Resolved>, SourceError> {
6925 let filter = format!("{ORIGIN_FIELD}:{}", quoted(origin));
6926 let search = self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))));
6927 let mut items_after: Option<String> = None;
6928 let mut search_after: Option<String> = None;
6929 let mut found: Vec<Resolved> = Vec::new();
6930 let keep = |resolved: Resolved, found: &mut Vec<Resolved>| {
6931 if !found.iter().any(|held| held.id == resolved.id) {
6932 found.push(resolved);
6933 }
6934 };
6935 loop {
6936 let data = self
6937 .graphql(
6938 graphql::ORIGIN_LOOKUP,
6939 json!({"owner":self.owner,"number":self.project_number,"filter":filter,
6940 "search":search,"type":"ISSUE","originFirst":ORIGIN_PAGE_SIZE,
6941 "itemsAfter":items_after,"searchAfter":search_after,
6942 "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
6943 "duplicates":true}),
6944 )
6945 .await?;
6946 let items = data
6947 .pointer("/originItems/projectV2/items")
6948 .filter(|value| !value.is_null())
6949 .ok_or_else(|| SourceError::Refused {
6950 message: format!(
6951 "GitHub project {}/{} was not found or is not visible to the token",
6952 self.owner, self.project_number
6953 ),
6954 })?;
6955 for item in optional_nodes(Some(items), "project items")?
6956 .into_iter()
6957 .flatten()
6958 {
6959 // The board's own items list its drafts too, and a draft is not an issue: no
6960 // narrowed read answers with one, whatever its origin field holds.
6961 if let Some(resolved) = self.resolve(item)?
6962 && resolved.content_kind == ContentKind::Issue
6963 {
6964 keep(resolved, &mut found);
6965 }
6966 }
6967 let searched = data.get("search").ok_or_else(|| SourceError::Malformed {
6968 message: "GitHub search response has no search connection".into(),
6969 })?;
6970 for node in optional_nodes(Some(searched), "search")?
6971 .into_iter()
6972 .flatten()
6973 {
6974 if let Some(resolved) = self.resolve_issue(node).await? {
6975 keep(resolved, &mut found);
6976 }
6977 }
6978 let items_next = resumed(items, items_after.as_deref())?;
6979 let search_next = resumed(searched, search_after.as_deref())?;
6980 if !items_next.has_more() && !search_next.has_more() {
6981 return Ok(found);
6982 }
6983 items_after = items_next.cursor();
6984 search_after = search_next.cursor();
6985 }
6986 }
6987
6988 /// `found`, with every item this process created or wrote in its place, and every one of
6989 /// them the read did not report added.
6990 ///
6991 /// This process's own record wins over the read's copy of the same item, because a read
6992 /// of an item written moments ago can still be behind what was written onto it — the
6993 /// origin field included, which is the one a narrowed read is confirmed against — and a
6994 /// read that still names an item under a predicate this process's write moved it out of
6995 /// must not return it. The one thing the read knows that the record cannot is when GitHub
6996 /// last saw the item change, which is what a comment-activity read rules a candidate out
6997 /// by, so the read's `updatedAt` is kept when the record has none of its own. See
6998 /// [`Self::created`] and [`Self::updated`](GitHubProjectsSource::updated).
6999 fn with_own_writes(&self, mut found: Vec<Resolved>) -> Result<Vec<Resolved>, SourceError> {
7000 // A board draft is not an issue, so no narrowed read returns one, and this process
7001 // having written one does not make it an answer either.
7002 let own: Vec<Resolved> = self
7003 .created()?
7004 .iter()
7005 .chain(self.updated()?.iter())
7006 .filter(|own| own.content_kind == ContentKind::Issue)
7007 .cloned()
7008 .collect();
7009 for mut own in own {
7010 self.resolved_cache()?.insert(own.id.clone(), own.clone());
7011 match found.iter_mut().find(|read| read.id == own.id) {
7012 Some(read) => {
7013 own.updated_at = own.updated_at.max(read.updated_at);
7014 *read = own;
7015 }
7016 None => found.push(own),
7017 }
7018 }
7019 Ok(found)
7020 }
7021
7022 /// Whether `item` has a comment created or last edited at or after `since` — always, when
7023 /// there is no instant to hold it to.
7024 ///
7025 /// The candidate's own `updatedAt` is read first, because a comment written or edited at
7026 /// or after the instant moved it there: an issue not updated since holds no such comment,
7027 /// and its comments are never asked for — unless this process commented on it in this
7028 /// command, when the `updatedAt` held may predate that comment; see [`Self::commented`]. Otherwise its comments are walked, oldest first,
7029 /// only as far as the first that matches. A board draft is not an issue and has no
7030 /// comments, so it never matches.
7031 async fn commented_since(
7032 &self,
7033 item: &Resolved,
7034 since: Option<DateTime<Utc>>,
7035 ) -> Result<bool, SourceError> {
7036 let Some(since) = since else {
7037 return Ok(true);
7038 };
7039 if item.content_kind == ContentKind::DraftIssue {
7040 return Ok(false);
7041 }
7042 // An `updatedAt` this process's own record or a lagging index holds can predate a
7043 // comment this process wrote since, so only an issue it did not comment on is ruled
7044 // out by one.
7045 if item.updated_at.is_some_and(|updated| updated < since)
7046 && !self.commented()?.contains(&item.id)
7047 {
7048 return Ok(false);
7049 }
7050 let query = TaskQuery {
7051 commented_since: Some(since),
7052 ..TaskQuery::default()
7053 };
7054 let mut after: Option<String> = None;
7055 loop {
7056 let data = self
7057 .graphql(
7058 graphql::ISSUE_COMMENTS,
7059 json!({"id":item.id.0,"first":MAX_PAGE_SIZE,"after":after}),
7060 )
7061 .await?;
7062 let Some(connection) = data
7063 .get("node")
7064 .filter(|value| !value.is_null())
7065 .and_then(|node| node.get("comments"))
7066 .filter(|value| !value.is_null())
7067 else {
7068 // Removed since the search reported it: no longer an issue with comments.
7069 return Ok(false);
7070 };
7071 let comments = optional_nodes(Some(connection), "issue comments")?
7072 .into_iter()
7073 .flatten()
7074 .map(comment_from)
7075 .collect::<Result<Vec<_>, _>>()?;
7076 if query.comments_match(&comments) {
7077 return Ok(true);
7078 }
7079 match next_cursor(connection)? {
7080 Some(next) => {
7081 validate_cursor_progress(after.as_deref(), &next.0)?;
7082 after = Some(next.0);
7083 }
7084 None => return Ok(false),
7085 }
7086 }
7087 }
7088
7089 /// Every item on the board: the union of both enumerations GitHub offers of one.
7090 ///
7091 /// Neither contains the other, so neither is dropped — only `ProjectV2.items` lists a
7092 /// board **draft** and reads the board's own fields beside its items, and only the search
7093 /// reports an item that connection is behind on. The module documentation is where the lag and the
7094 /// measurements behind it are written down.
7095 ///
7096 /// A search result is admitted on the same terms as any other issue this source reaches
7097 /// directly — [`Self::resolve_issue`] keeps it only if that issue's own `projectItems`
7098 /// names *this* board — so an issue the index still believes is here after it was taken
7099 /// off is refused rather than reported.
7100 ///
7101 /// See [`Self::board_cache`]. Both completions happen on every call rather than once,
7102 /// which is what the cache could otherwise have broken.
7103 async fn board(&self) -> Result<Board, SourceError> {
7104 self.board_completed_by(None).await
7105 }
7106
7107 /// [`Self::board`], its search half narrowed by `also` when it is given — which an issue
7108 /// that half could add is then answered only if it holds.
7109 async fn board_completed_by(&self, also: Option<&str>) -> Result<Board, SourceError> {
7110 let cached = self.board_cache()?.clone();
7111 let mut board = match cached {
7112 Some(board) => board,
7113 None => {
7114 let read = self.read_board().await?;
7115 *self.board_cache()? = Some(read.clone());
7116 read
7117 }
7118 };
7119 let searched = match also {
7120 None => self.searched_issues().await?,
7121 Some(also) => self.searched_narrowly(also).await?,
7122 };
7123 for held in searched {
7124 if !board.items.iter().any(|item| item.id == held.id) {
7125 board.items.push(held);
7126 }
7127 }
7128 for own in self.created()?.iter() {
7129 if !board.items.iter().any(|item| item.id == own.id) {
7130 board.items.push(own.clone());
7131 }
7132 }
7133 Ok(board)
7134 }
7135
7136 /// This process's own view of the board, or the refusal a poisoned lock is.
7137 fn board_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<Board>>, SourceError> {
7138 self.board_cache
7139 .lock()
7140 .map_err(|_| SourceError::Unavailable {
7141 message: "this source's view of the board was left inconsistent by an earlier \
7142 failure; next: run the command again"
7143 .into(),
7144 })
7145 }
7146
7147 /// Bring this process's own view of the board up to an item it has just written.
7148 ///
7149 /// A created item goes to `created`, which is what completes a board read GitHub's own
7150 /// eventual consistency has left behind. An item that was already there is replaced
7151 /// where it sits, so a second write of it in the same command reads its real parent
7152 /// rather than the one it had before the first write.
7153 ///
7154 /// "Where it sits" is three places, and missing an earlier one leaves a stale record
7155 /// that wins: an item this same run created is held in `created` and not in the cached
7156 /// board, and `board` completes the cached board *from* `created`, so replacing only
7157 /// the cached copy of such an item replaces nothing and the read still reports the
7158 /// title it was created with. The search is the third, and it is the one an item the
7159 /// board's own projection is behind on sits in *alone* — which is exactly the item this
7160 /// source is least able to re-read, so leaving it out would put the stale title back on
7161 /// the only items the completion in [`Self::board`] exists for.
7162 fn remember_written(&self, item: Resolved, created: bool) -> Result<(), SourceError> {
7163 self.resolved_cache()?.insert(item.id.clone(), item.clone());
7164 if created {
7165 self.created()?.push(item);
7166 return Ok(());
7167 }
7168 {
7169 let mut own = self.created()?;
7170 if let Some(held) = own.iter_mut().find(|held| held.id == item.id) {
7171 *held = item;
7172 return Ok(());
7173 }
7174 }
7175 {
7176 let mut own = self.updated()?;
7177 match own.iter_mut().find(|held| held.id == item.id) {
7178 Some(held) => *held = item.clone(),
7179 None => own.push(item.clone()),
7180 }
7181 }
7182 if let Some(board) = self.board_cache()?.as_mut()
7183 && let Some(held) = board.items.iter_mut().find(|held| held.id == item.id)
7184 {
7185 *held = item.clone();
7186 }
7187 if let Some(found) = self.search_cache()?.as_mut()
7188 && let Some(held) = found.iter_mut().find(|held| held.id == item.id)
7189 {
7190 *held = item.clone();
7191 }
7192 for found in self.narrowed_cache()?.values_mut() {
7193 if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
7194 *held = item.clone();
7195 }
7196 }
7197 for (_, found) in self.children_cache()?.values_mut() {
7198 if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
7199 *held = item.clone();
7200 }
7201 }
7202 Ok(())
7203 }
7204
7205 /// Forget one item this process has just deleted, from every half of its own view.
7206 fn forget(&self, id: &NativeId) -> Result<(), SourceError> {
7207 if let Some(Deferred::Held { created, .. }) = self.deferred()?.as_mut() {
7208 created.retain(|held| held.content != *id);
7209 }
7210 self.resolved_cache()?.remove(id);
7211 self.read_ahead()?.remove(id);
7212 self.carried()?.remove(id);
7213 self.created()?.retain(|own| own.id != *id);
7214 self.updated()?.retain(|own| own.id != *id);
7215 self.commented()?.retain(|own| own != id);
7216 if let Some(board) = self.board_cache()?.as_mut() {
7217 board.items.retain(|item| item.id != *id);
7218 }
7219 if let Some(found) = self.search_cache()?.as_mut() {
7220 found.retain(|item| item.id != *id);
7221 }
7222 for found in self.narrowed_cache()?.values_mut() {
7223 found.retain(|item| item.id != *id);
7224 }
7225 for (_, found) in self.children_cache()?.values_mut() {
7226 found.retain(|item| item.id != *id);
7227 }
7228 Ok(())
7229 }
7230
7231 /// This process's own record of each narrowed answer, or the refusal a poisoned lock is.
7232 fn narrowed_cache(
7233 &self,
7234 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<String, Vec<Resolved>>>, SourceError> {
7235 self.narrowed_cache
7236 .lock()
7237 .map_err(|_| SourceError::Unavailable {
7238 message: "this source's view of a narrowed read was left inconsistent by an \
7239 earlier failure; next: run the command again"
7240 .into(),
7241 })
7242 }
7243
7244 /// This process's own record of each project's sub-issues, or the refusal a poisoned lock
7245 /// is.
7246 /// The tasks `project` holds, as a copy's batched read found them this command, when it did.
7247 fn held_task_children(&self, project: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
7248 Ok(self.task_children()?.get(project).cloned())
7249 }
7250
7251 fn task_children(
7252 &self,
7253 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Vec<Resolved>>>, SourceError> {
7254 self.task_children
7255 .lock()
7256 .map_err(|_| SourceError::Unavailable {
7257 message: "this source's record of a project's tasks was left inconsistent by an \
7258 earlier failure; next: run the command again"
7259 .into(),
7260 })
7261 }
7262
7263 fn children_cache(&self) -> Result<std::sync::MutexGuard<'_, ProjectChildren>, SourceError> {
7264 self.children_cache
7265 .lock()
7266 .map_err(|_| SourceError::Unavailable {
7267 message: "this source's view of a project's tasks was left inconsistent by an \
7268 earlier failure; next: run the command again"
7269 .into(),
7270 })
7271 }
7272
7273 /// Every page of the board, read from GitHub.
7274 async fn read_board(&self) -> Result<Board, SourceError> {
7275 let mut after: Option<String> = None;
7276 let mut items = Vec::new();
7277 let mut board;
7278 loop {
7279 let page = self.board_page(after.as_deref(), MAX_PAGE_SIZE).await?;
7280 for item in page
7281 .pointer("/items/nodes")
7282 .and_then(Value::as_array)
7283 .ok_or_else(|| SourceError::Malformed {
7284 message: "GitHub project items.nodes is not an array".into(),
7285 })?
7286 {
7287 if let Some(resolved) = self.resolve(item)? {
7288 items.push(resolved);
7289 }
7290 }
7291 let info = page
7292 .pointer("/items/pageInfo")
7293 .ok_or_else(|| SourceError::Malformed {
7294 message: "GitHub project items have no pageInfo".into(),
7295 })?;
7296 let has_next = required_bool(info, "hasNextPage")?;
7297 let next = has_next
7298 .then(|| required_str(info, "endCursor"))
7299 .transpose()?;
7300 board = page.clone();
7301 match next {
7302 Some(next) => {
7303 validate_cursor_progress(after.as_deref(), next)?;
7304 after = Some(next.to_owned());
7305 }
7306 None => break,
7307 }
7308 }
7309 Ok(Board {
7310 id: required_str(&board, "id")?.to_owned(),
7311 fields: board.get("fields").cloned().unwrap_or(Value::Null),
7312 items,
7313 })
7314 }
7315
7316 /// The existing items this source has written, for completing a narrowed read that is
7317 /// behind; see [`Self::updated`](GitHubProjectsSource::updated).
7318 fn updated(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
7319 self.updated.lock().map_err(|_| SourceError::Unavailable {
7320 message: "this source's record of what it wrote in this run was left inconsistent \
7321 by an earlier failure; next: run the command again"
7322 .into(),
7323 })
7324 }
7325
7326 /// The issues this source has commented on in this command; see
7327 /// [`Self::commented`](GitHubProjectsSource::commented).
7328 fn commented(&self) -> Result<std::sync::MutexGuard<'_, Vec<NativeId>>, SourceError> {
7329 self.commented.lock().map_err(|_| SourceError::Unavailable {
7330 message: "this source's record of what it commented on in this run was left \
7331 inconsistent by an earlier failure; next: run the command again"
7332 .into(),
7333 })
7334 }
7335
7336 /// Called only once GitHub has answered the comment write, so an issue whose comment
7337 /// failed is never made a candidate a later read would pay a node read for.
7338 fn remember_commented(&self, issue: &NativeId) -> Result<(), SourceError> {
7339 let mut commented = self.commented()?;
7340 if !commented.contains(issue) {
7341 commented.push(issue.clone());
7342 }
7343 Ok(())
7344 }
7345
7346 /// The items this source has created, for completing a board read that is behind.
7347 fn created(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
7348 self.created.lock().map_err(|_| SourceError::Unavailable {
7349 message: "this source's record of what it created in this run was left \
7350 inconsistent by an earlier failure; next: run the command again"
7351 .into(),
7352 })
7353 }
7354
7355 /// One board item as this source reports it, or `None` for content it ignores.
7356 ///
7357 /// A pull request is neither a project nor a task — it is somebody's change, not a
7358 /// unit of plan — and an item whose content the token cannot see has nothing to
7359 /// report at all.
7360 fn resolve(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
7361 let resolved = self.resolve_unremembered(item)?;
7362 if let Some(resolved) = &resolved {
7363 self.resolved_cache()?
7364 .insert(resolved.id.clone(), resolved.clone());
7365 }
7366 Ok(resolved)
7367 }
7368
7369 /// [`Self::resolve`], without remembering the item it resolves: what reading a token off a
7370 /// write's own response needs, which says nothing this process should answer a later read
7371 /// with.
7372 fn resolve_unremembered(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
7373 let content = item.get("content").ok_or_else(|| SourceError::Malformed {
7374 message: "GitHub project item is missing content".into(),
7375 })?;
7376 if content.is_null() {
7377 return Ok(None);
7378 }
7379 let content_kind = match required_str(content, "__typename")? {
7380 "Issue" => ContentKind::Issue,
7381 "DraftIssue" => ContentKind::DraftIssue,
7382 _ => return Ok(None),
7383 };
7384 let field_values = item
7385 .get("fieldValues")
7386 .ok_or_else(|| SourceError::Malformed {
7387 message: "GitHub project item is missing fieldValues".into(),
7388 })?;
7389 complete_connection(field_values, "project item field values", NESTED_PAGE_SIZE)?;
7390 let nodes = field_values
7391 .get("nodes")
7392 .and_then(Value::as_array)
7393 .ok_or_else(|| SourceError::Malformed {
7394 message: "GitHub project item fieldValues.nodes is not an array".into(),
7395 })?;
7396 if let Some(labels) = content.get("labels") {
7397 complete_connection(labels, "content labels", NESTED_PAGE_SIZE)?;
7398 }
7399 let raw_body = optional_str(content, "body")?.map(str::to_owned);
7400 let (body, slot) = metadata_body(raw_body.clone())?;
7401 let parent = optional_str(content.get("parent").unwrap_or(&Value::Null), "id")?
7402 .map(|id| NativeId(id.to_owned()));
7403 // A draft has no sub-issues to summarise, and GitHub's schema gives it no field
7404 // to read one from; it is a task, and never a project.
7405 let sub_issues = match content_kind {
7406 ContentKind::Issue => sub_issue_total(content)?,
7407 ContentKind::DraftIssue => 0,
7408 };
7409 let content_id = required_str(content, "id")?;
7410 let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
7411 message: format!("GitHub issue {content_id}: {message}"),
7412 })?;
7413 let raw_title = required_str(content, "title")?;
7414 let kind = BoardKind::of(raw_title, parent.is_some(), sub_issues, marked);
7415 // The title a person wrote, which for a document is the one without the prefix —
7416 // the same way `content` above is the body without this source's metadata slot.
7417 let title = match kind {
7418 BoardKind::Document => raw_title[DESIGN_TITLE_PREFIX.len()..].to_owned(),
7419 BoardKind::Work(_) => raw_title.to_owned(),
7420 };
7421 let own_repository = content
7422 .pointer("/repository/nameWithOwner")
7423 .and_then(Value::as_str)
7424 .map(|origin| Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST)))
7425 .transpose()
7426 .map_err(|message| SourceError::Malformed { message })?;
7427 let repositories = if slot.contains_key(Repository::METADATA_KEY) {
7428 Repository::from_metadata(&slot)
7429 .map_err(|message| SourceError::Malformed { message })?
7430 } else {
7431 own_repository.clone().into_iter().collect()
7432 };
7433 let classification = Classification::from_metadata(&slot)
7434 .map_err(|message| SourceError::Malformed { message })?;
7435 let id = NativeId(content_id.to_owned());
7436 // Read only for a task, because only a task has either list: a project or a
7437 // document holding one of these keys holds nothing this source reports, and the
7438 // keys are left out of its caller-visible metadata all the same.
7439 let (delivers, delivered_by) = if kind == BoardKind::Work(ItemKind::Task) {
7440 let listed = |key: &str| {
7441 TaskRef::from_value(key, &id, Some(&self.name), slot.get(key))
7442 .map_err(|message| SourceError::Malformed { message })
7443 };
7444 (
7445 listed(TaskRef::DELIVERS_KEY)?,
7446 listed(TaskRef::DELIVERED_BY_KEY)?,
7447 )
7448 } else {
7449 (Vec::new(), Vec::new())
7450 };
7451 let (option, closed, reason) = Self::status_parts(nodes, content)?;
7452 let priority = self.held_priority(nodes)?;
7453 let item_labels = labels(content)?;
7454 // Present when the item was reached through its own issue, whose board entry
7455 // names the board; a read of the board's own items has the board already. An
7456 // empty id names nothing a field write could address, so it is read as absent and
7457 // the write goes back to reading the board.
7458 let board_id = item
7459 .pointer("/project/id")
7460 .and_then(Value::as_str)
7461 .filter(|id| !id.is_empty());
7462 let resolved = Resolved {
7463 item_id: required_str(item, "id")?.to_owned(),
7464 id,
7465 content_kind,
7466 kind,
7467 title,
7468 body: body.filter(|value| !value.is_empty()),
7469 raw_body,
7470 status: self
7471 .statuses
7472 .status(kind.status_kind(), option, closed, reason),
7473 option: option.map(str::to_owned),
7474 priority,
7475 closed,
7476 reason: closed.then(|| reason.map(str::to_owned)).flatten(),
7477 delivers,
7478 delivered_by,
7479 labels: item_labels.clone(),
7480 parent,
7481 origin: text_field(nodes, ORIGIN_FIELD)?.filter(|value| !value.is_empty()),
7482 projected: self.held_projections(nodes)?,
7483 number: match content_kind {
7484 ContentKind::Issue => Some(issue_number(content)?),
7485 // A draft is filed in no repository, so nothing ever numbered it:
7486 // `DraftIssue` declares no `number` at all, exactly as it declares no
7487 // `subIssuesSummary` the branch above reads.
7488 ContentKind::DraftIssue => None,
7489 },
7490 url: optional_str(content, "url")?.map(str::to_owned),
7491 created_at: optional_time(content, "createdAt")?,
7492 updated_at: optional_time(content, "updatedAt")?,
7493 item_updated_at: optional_time(item, "updatedAt")?,
7494 comment_count: content
7495 .pointer("/commentCount/totalCount")
7496 .and_then(Value::as_u64),
7497 relationship_events: content
7498 .pointer("/relationshipEvents/totalCount")
7499 .and_then(Value::as_u64),
7500 // The issue node is stamped wherever it was read; a board item a read of the issue
7501 // put together from its memberships is not.
7502 served_at: match optional_time(content, SERVED_AT)? {
7503 Some(served) => Some(served),
7504 None => optional_time(item, SERVED_AT)?,
7505 },
7506 // Both connections were read to their end above — one reporting a further page is
7507 // refused — so these are every field value and every label the item holds. A
7508 // response that did not select the labels at all says nothing of them, and gives
7509 // no facets.
7510 facets: content.get("labels").is_some().then(|| {
7511 change_token::Facets::new(kind.facet(), facet_fields(nodes), &item_labels).digest()
7512 }),
7513 own_repository,
7514 repositories,
7515 classification,
7516 slot,
7517 board_id: board_id.map(str::to_owned),
7518 fields: field_definitions(nodes),
7519 board_fields: Self::carried_board_fields(content, board_id)?,
7520 blocked_by: carried_blocked_by(content)?,
7521 };
7522 Ok(Some(resolved))
7523 }
7524
7525 /// The field definitions of the board `board_id` names — the project this issue's own
7526 /// board item is on — off the `boards` page a read of an issue by its own id carries, or
7527 /// `None` when the read carried none, carried no entry for that board, or the board item
7528 /// named no board, which a write then answers by reading the board's fields itself.
7529 ///
7530 /// Matched by the board's node id and never by its number alone: a project number is
7531 /// unique only within its owner, so another owner's board numbered alike can sit on the
7532 /// same page, and its field and option ids address nothing on this one.
7533 fn carried_board_fields(
7534 content: &Value,
7535 board_id: Option<&str>,
7536 ) -> Result<Option<Value>, SourceError> {
7537 let (Some(nodes), Some(board_id)) = (
7538 content.pointer("/boards/nodes").and_then(Value::as_array),
7539 board_id,
7540 ) else {
7541 return Ok(None);
7542 };
7543 let Some(board) = nodes.iter().find_map(|node| {
7544 let project = node.get("project")?;
7545 (project.get("id").and_then(Value::as_str) == Some(board_id)).then_some(project)
7546 }) else {
7547 return Ok(None);
7548 };
7549 let Some(fields) = board.get("fields").filter(|fields| !fields.is_null()) else {
7550 return Ok(None);
7551 };
7552 complete_connection(fields, "board fields", NESTED_PAGE_SIZE)?;
7553 Ok(Some(fields.clone()))
7554 }
7555
7556 /// What one board item's `Priority` field says, through this instance's mapping.
7557 ///
7558 /// An instance with no mapping holds no priority, so every item reads as `none` whatever
7559 /// its board holds. With one, no value is `none`, a mapped option is its level, and an
7560 /// option the mapping does not name is kept as itself — never read as a level or as
7561 /// `none` — for a read of the task to report by name.
7562 fn held_priority(&self, field_values: &[Value]) -> Result<HeldPriority, SourceError> {
7563 let Some(mapping) = &self.priorities else {
7564 return Ok(HeldPriority::Read(Priority::None));
7565 };
7566 // A value of the field that names no option — a text field someone called `Priority` —
7567 // is malformed rather than `none`: reading it as no priority would let the next copy
7568 // clear one a person set.
7569 let Some(option) = field_values
7570 .iter()
7571 .find(|value| {
7572 value.pointer("/field/name").and_then(Value::as_str) == Some(PRIORITY_FIELD)
7573 })
7574 .map(|value| required_str(value, "name"))
7575 .transpose()?
7576 else {
7577 return Ok(HeldPriority::Read(Priority::None));
7578 };
7579 Ok(mapping.priority_of(option).map_or_else(
7580 || HeldPriority::Unmapped(option.to_owned()),
7581 HeldPriority::Read,
7582 ))
7583 }
7584
7585 /// What each projected metadata field holds for one board item, by field name — every text
7586 /// value GitHub answers, an empty one included.
7587 fn held_projections(
7588 &self,
7589 field_values: &[Value],
7590 ) -> Result<BTreeMap<String, String>, SourceError> {
7591 let mut held = BTreeMap::new();
7592 for projection in &self.metadata_fields {
7593 if let Some(text) = text_field(field_values, &projection.field)? {
7594 held.insert(projection.field.to_string(), text);
7595 }
7596 }
7597 Ok(held)
7598 }
7599
7600 /// What one board item's status is read from: its `Status` option, whether its issue
7601 /// is closed, and the reason it was closed with. [`BoardStatuses::status`] turns the
7602 /// three into the status it reports.
7603 fn status_parts<'a>(
7604 field_values: &'a [Value],
7605 content: &'a Value,
7606 ) -> Result<(Option<&'a str>, bool, Option<&'a str>), SourceError> {
7607 let option = field_values
7608 .iter()
7609 .find(|value| value.pointer("/field/name").and_then(Value::as_str) == Some("Status"))
7610 .map(|value| required_str(value, "name"))
7611 .transpose()?;
7612 let closed = optional_str(content, "state")? == Some("CLOSED");
7613 Ok((option, closed, optional_str(content, "stateReason")?))
7614 }
7615
7616 /// The board Status option this write selects, or the refusal that says why not.
7617 ///
7618 /// The mapped option is required for both open and terminal targets. A terminal write
7619 /// validates it before changing either representation, so it can never fall back to
7620 /// closing an issue whose board cannot display the matching status.
7621 ///
7622 /// Answers the field's id, the option's id, and the option's name as the board spells
7623 /// it — which is the name a read of the item reports once it sits there.
7624 fn column_for(
7625 &self,
7626 fields: &Value,
7627 kind: ItemKind,
7628 category: StatusCategory,
7629 target: &StatusTarget,
7630 ) -> Result<Option<(String, String, String)>, SourceError> {
7631 let Some(wanted) = target.option() else {
7632 return Ok(None);
7633 };
7634 let missing = |detail: &str| SourceError::Refused {
7635 message: format!(
7636 "{} status {} of source {} needs the board Status option {wanted:?}, and \
7637 {detail}; next: add that option to the board, which `onetaskgraph sources \
7638 fields {} --apply` does, or point status_mapping.{}.{} of this source at one \
7639 it has",
7640 kind.marker(),
7641 category_name(category),
7642 self.name,
7643 self.name,
7644 category_name(category),
7645 kind.marker()
7646 ),
7647 };
7648 let Some(field) = Board::field(fields, "Status")? else {
7649 return Err(missing("this board has no Status field"));
7650 };
7651 if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
7652 return Err(missing(
7653 "this board's Status field is not a single-select field",
7654 ));
7655 }
7656 let option = field
7657 .get("options")
7658 .and_then(Value::as_array)
7659 .and_then(|options| {
7660 options.iter().find(|option| {
7661 option
7662 .get("name")
7663 .and_then(Value::as_str)
7664 .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
7665 })
7666 });
7667 match option {
7668 None => Err(missing("this board does not have it")),
7669 Some(option) => Ok(Some((
7670 required_str(field, "id")?.to_owned(),
7671 required_str(option, "id")?.to_owned(),
7672 required_str(option, "name")?.to_owned(),
7673 ))),
7674 }
7675 }
7676
7677 /// The refusal a status that closes an issue is answered with over a board draft.
7678 fn closes_a_draft(&self, category: StatusCategory) -> SourceError {
7679 SourceError::Refused {
7680 message: format!(
7681 "status {} of source {} closes the item's issue, and GitHub draft items have \
7682 no open or closed state",
7683 category_name(category),
7684 self.name
7685 ),
7686 }
7687 }
7688
7689 /// What a status write to one item needs of the board: the board's id and the
7690 /// definition of its `Status` field, read off the item when the item says both.
7691 ///
7692 /// The same reasoning as [`Self::fields_for`]: a node read of the item names its board,
7693 /// and its `Status` value carries that field's definition, options and all. An item that
7694 /// does not say — no board id, or no `Status` value to read the field off — takes them
7695 /// from [`Self::board_fields`], which reads no item.
7696 async fn status_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
7697 if let Some(board) = item.carried_board() {
7698 return Ok(board);
7699 }
7700 if item.defines("Status")
7701 && let Some(board_id) = item.named_board()
7702 {
7703 return Ok(BoardFields {
7704 id: board_id,
7705 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
7706 });
7707 }
7708 self.board_fields().await
7709 }
7710
7711 /// Set one task's or one project's status and nothing else; see
7712 /// [`TaskSource::set_task_status`] and [`TaskSource::set_project_status`].
7713 async fn set_status(
7714 &self,
7715 id: &NativeId,
7716 kind: ItemKind,
7717 category: StatusCategory,
7718 ) -> Result<Option<Status>, SourceError> {
7719 // Refused before anything is read, in the words a write of the same status is.
7720 let target = self.resolved_target(kind, category)?;
7721 // An item read by issue reference carries only what this write needs, so what it
7722 // becomes is never remembered for a later read to be answered from.
7723 let held = self.take_held(id)?;
7724 let by_reference = held.is_some();
7725 let bound = match held {
7726 Some(item) => Some(item),
7727 None => self.bound_item(id).await?,
7728 };
7729 let Some(mut item) = bound.filter(|item| item.kind == BoardKind::Work(kind)) else {
7730 return Ok(None);
7731 };
7732 let board = self.status_board(&item).await?;
7733 let (field, option, name) = self
7734 .column_for(&board.fields, kind, category, &target)?
7735 .ok_or_else(|| SourceError::Malformed {
7736 message: format!(
7737 "status {} of source {} names no board Status option",
7738 category_name(category),
7739 self.name
7740 ),
7741 })?;
7742 if item.status.category == category && item.option.as_deref() == Some(&name) {
7743 return Ok(Some(item.status));
7744 }
7745 match &target {
7746 StatusTarget::Terminal(_, reason) => {
7747 if item.content_kind == ContentKind::DraftIssue {
7748 return Err(self.closes_a_draft(category));
7749 }
7750 // The option first, so a refused selection leaves an open issue open rather
7751 // than closed behind a card that still reads open.
7752 self.set_item_field(
7753 board.id.as_str(),
7754 &item.item_id,
7755 &field,
7756 json!({"singleSelectOptionId": option}),
7757 )
7758 .await?;
7759 // An issue already closed in a way that reads as this category keeps its state
7760 // and its reason: only its card was out of line, and closing it again would
7761 // overwrite a reason — a duplicate's — that says more than this write does.
7762 let already = item.closed && item.status.category == category;
7763 if !already {
7764 self.update_content(
7765 ContentKind::Issue,
7766 &item.id,
7767 json!({"stateInput": state_input(Some(&target))}),
7768 )
7769 .await?;
7770 }
7771 let reason = if already {
7772 item.reason.clone()
7773 } else {
7774 Some(reason.reason().to_owned())
7775 };
7776 item.closed = true;
7777 item.status = self
7778 .statuses
7779 .status(kind, Some(&name), true, reason.as_deref());
7780 item.option = Some(name);
7781 item.reason = reason;
7782 }
7783 StatusTarget::Column(_) => {
7784 // An option is what an open item's status is, so a closed issue is reopened
7785 // first — sitting closed in the column, it would read back as closed. A draft has
7786 // no state to reopen.
7787 if item.content_kind == ContentKind::Issue && item.closed {
7788 self.update_content(
7789 ContentKind::Issue,
7790 &item.id,
7791 json!({"stateInput": state_input(Some(&target))}),
7792 )
7793 .await?;
7794 item.closed = false;
7795 }
7796 self.set_item_field(
7797 board.id.as_str(),
7798 &item.item_id,
7799 &field,
7800 json!({"singleSelectOptionId": option}),
7801 )
7802 .await?;
7803 item.status = self.statuses.status(kind, Some(&name), false, None);
7804 item.option = Some(name);
7805 }
7806 StatusTarget::Disabled(_) => {
7807 unreachable!("resolved_target refused a disabled status")
7808 }
7809 }
7810 let status = item.status.clone();
7811 if !by_reference {
7812 self.remember_written(item, false)?;
7813 }
7814 Ok(Some(status))
7815 }
7816
7817 /// Replace one task's `delivered_by` and nothing else; see
7818 /// [`TaskSource::set_delivered_by`].
7819 ///
7820 /// One update of the body, which differs from the body GitHub holds only inside the
7821 /// metadata slot — see [`with_slot`]. A body that would not change is not sent at all.
7822 async fn replace_delivered_by(
7823 &self,
7824 id: &NativeId,
7825 delivered_by: &[TaskRef],
7826 ) -> Result<Option<()>, SourceError> {
7827 let entries = TaskRef::listed(
7828 TaskRef::DELIVERED_BY_KEY,
7829 id,
7830 Some(&self.name),
7831 delivered_by.to_vec(),
7832 )
7833 .map_err(|message| SourceError::Refused { message })?;
7834 let Some(mut item) = self
7835 .bound_item(id)
7836 .await?
7837 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
7838 else {
7839 return Ok(None);
7840 };
7841 let mut slot = item.slot.clone();
7842 set_task_list(&mut slot, TaskRef::DELIVERED_BY_KEY, &entries);
7843 self.write_slot(&mut item, &slot).await?;
7844 item.delivered_by = entries;
7845 self.remember_written(item, false)?;
7846 Ok(Some(()))
7847 }
7848
7849 /// Set one caller key of the metadata slot of one issue of `kind`, and nothing else;
7850 /// see [`TaskSource::set_task_metadata`].
7851 ///
7852 /// `None` when this board holds no item by that id, or holds one of another kind. The
7853 /// answer is the item as this source now reads it, so what a caller is told the key
7854 /// holds is what the slot holds.
7855 ///
7856 /// A key already holding the value is answered without a write, compared as JSON rather
7857 /// than as the body's bytes: a slot a person spelled with other whitespace would
7858 /// otherwise be re-encoded, which is a write that changes nothing the caller asked for.
7859 async fn set_slot_key(
7860 &self,
7861 id: &NativeId,
7862 kind: BoardKind,
7863 key: &MetadataKey,
7864 value: &Value,
7865 ) -> Result<Option<Resolved>, SourceError> {
7866 let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
7867 return Ok(None);
7868 };
7869 if item.slot.get(key.as_str()) == Some(value) {
7870 return Ok(Some(item));
7871 }
7872 let mut slot = item.slot.clone();
7873 slot.insert(key.as_str().to_owned(), value.clone());
7874 // A key a board text field projects moves that field first, so the body — the value's
7875 // one authoritative home — is written last, as every write of an existing item is.
7876 if self
7877 .metadata_fields
7878 .iter()
7879 .any(|projection| &*projection.key == key.as_str())
7880 {
7881 let board = self.projection_board(&item).await?;
7882 let (writes, projected) = self.projection_writes(
7883 &board.fields,
7884 Some(&item.projected),
7885 &slot,
7886 Some(&[key.as_str()]),
7887 )?;
7888 let (mut values, mut clears) = (Vec::new(), Vec::new());
7889 for write in &writes {
7890 write.join(&mut values, &mut clears);
7891 }
7892 self.set_item_fields(board.id.as_str(), &item.item_id, &values, &clears)
7893 .await?;
7894 item.projected = projected;
7895 }
7896 self.write_slot(&mut item, &slot).await?;
7897 self.remember_written(item.clone(), false)?;
7898 Ok(Some(item))
7899 }
7900
7901 /// What a projection write to `item` needs of the board, read off the item when it says
7902 /// enough, as [`Self::fields_for`] reads it.
7903 async fn projection_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
7904 if let Some(board) = item.carried_board() {
7905 return Ok(board);
7906 }
7907 if let Some(board_id) = item.named_board()
7908 && self
7909 .metadata_fields
7910 .iter()
7911 .all(|projection| item.defines(&projection.field))
7912 {
7913 return Ok(BoardFields {
7914 id: board_id,
7915 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
7916 });
7917 }
7918 self.board_fields().await
7919 }
7920
7921 /// Put `slot` in one item's metadata slot with a single update of its body, and bring
7922 /// `item` up to what that write left.
7923 ///
7924 /// The body sent differs from the body GitHub holds only inside the slot — see
7925 /// [`with_slot`] — and a body that would not change is not sent at all. It goes through
7926 /// the mutation the item's content takes, so a board draft's body is written with
7927 /// `updateProjectV2DraftIssue` exactly as an issue's is with `updateIssue`.
7928 async fn write_slot(
7929 &self,
7930 item: &mut Resolved,
7931 slot: &BTreeMap<String, Value>,
7932 ) -> Result<(), SourceError> {
7933 let held = item.raw_body.clone().unwrap_or_default();
7934 let body = with_slot(&held, slot)?;
7935 if body != held {
7936 self.update_content(item.content_kind, &item.id, json!({"body": body}))
7937 .await?;
7938 }
7939 let (visible, slot) = metadata_body(Some(body.clone()))?;
7940 item.body = visible.filter(|value| !value.is_empty());
7941 item.raw_body = Some(body);
7942 item.slot = slot;
7943 Ok(())
7944 }
7945
7946 /// This instance's target for a category written to an item of `kind`, refusing one
7947 /// that kind has no option for — before anything is read or written.
7948 ///
7949 /// Nothing here mutates the board's option set to make room for a status. GitHub
7950 /// documents `UpdateProjectV2FieldInput.singleSelectOptions` as *"provided values
7951 /// overwrite existing options"*, so no addition is additive and a mistake destroys the
7952 /// field and every item's status.
7953 fn resolved_target(
7954 &self,
7955 kind: ItemKind,
7956 category: StatusCategory,
7957 ) -> Result<StatusTarget, SourceError> {
7958 let target = self.statuses.target(kind, category).clone();
7959 let StatusTarget::Disabled(why) = target else {
7960 return Ok(target);
7961 };
7962 let refusal = why.refusal(&self.name, category, kind);
7963 // Why there is no shipped default, which is the question a person meeting this
7964 // refusal on a source that never mentioned the category asks.
7965 let shipped_none = match category {
7966 StatusCategory::Draft => Some(
7967 "draft has no shipped default because GitHub draft issues cannot have \
7968 sub-issues, and this source stores a project's tasks as its issue's sub-issues",
7969 ),
7970 StatusCategory::Unknown => Some(
7971 "unknown has no shipped default because this board keeps no open-ended status \
7972 word: every word classified unknown is written to the one board Status option \
7973 status_mapping.unknown names",
7974 ),
7975 _ => None,
7976 };
7977 Err(match (refusal, shipped_none, why) {
7978 (SourceError::Refused { message }, Some(note), UnmappedStatus::Unconfigured) => {
7979 SourceError::Refused {
7980 message: format!("{message}; {note}"),
7981 }
7982 }
7983 (refusal, _, _) => refusal,
7984 })
7985 }
7986
7987 /// What writing `priority` does to one item's `Priority` field on this board, or the
7988 /// refusal naming what the board lacks.
7989 ///
7990 /// `none` is no value, so it clears the field — and asks nothing of an item that holds
7991 /// none already, or of an item not created yet. Every other priority selects the option
7992 /// the mapping names, matched case-insensitively; a board with no `Priority` field, or
7993 /// without that option, is refused rather than given one: reads and writes never create
7994 /// a field or an option.
7995 fn priority_write(
7996 &self,
7997 fields: &Value,
7998 existing: Option<&Resolved>,
7999 priority: Priority,
8000 ) -> Result<Option<PriorityWrite>, SourceError> {
8001 let Some(mapping) = &self.priorities else {
8002 return Err(self.holds_no_priority());
8003 };
8004 let Some(wanted) = mapping.option(priority) else {
8005 if !existing.is_some_and(Resolved::holds_priority) {
8006 return Ok(None);
8007 }
8008 let field =
8009 Board::field(fields, PRIORITY_FIELD)?.ok_or_else(|| SourceError::Malformed {
8010 message: format!(
8011 "an item holding a {PRIORITY_FIELD} value was read without that field"
8012 ),
8013 })?;
8014 return Ok(Some(PriorityWrite::Clear {
8015 field: required_str(field, "id")?.to_owned(),
8016 }));
8017 };
8018 let missing = |detail: &str| SourceError::Refused {
8019 message: format!(
8020 "priority {priority} of source {} needs the board {PRIORITY_FIELD} option \
8021 {wanted:?}, and {detail}; run `onetaskgraph sources fields {} --apply` to add \
8022 it, or point priority_mapping.{priority} of this source at an option the board \
8023 has",
8024 self.name, self.name
8025 ),
8026 };
8027 let Some(field) = Board::field(fields, PRIORITY_FIELD)? else {
8028 return Err(missing(&format!(
8029 "this board has no {PRIORITY_FIELD} field"
8030 )));
8031 };
8032 if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
8033 return Err(missing(&format!(
8034 "this board's {PRIORITY_FIELD} field is not a single-select field"
8035 )));
8036 }
8037 // An options list that is absent or not a list is an answer this source cannot read,
8038 // not a board lacking the option: `sources fields --apply` is no remedy for it.
8039 let option = field
8040 .get("options")
8041 .and_then(Value::as_array)
8042 .ok_or_else(|| SourceError::Malformed {
8043 message: format!("GitHub {PRIORITY_FIELD} field options is not an array"),
8044 })?
8045 .iter()
8046 .find(|option| {
8047 option
8048 .get("name")
8049 .and_then(Value::as_str)
8050 .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
8051 })
8052 .ok_or_else(|| missing("this board does not have it"))?;
8053 Ok(Some(PriorityWrite::Select {
8054 field: required_str(field, "id")?.to_owned(),
8055 option: required_str(option, "id")?.to_owned(),
8056 }))
8057 }
8058
8059 /// What writing `metadata` does to each projected text field of one item, and what those
8060 /// fields hold after it — or the refusal, before anything is sent.
8061 ///
8062 /// `held` is what the item's fields hold now, `None` for an item not created yet. `keys`
8063 /// narrows the work to the entries projecting one of those metadata keys, for a write that
8064 /// touches only them; `None` is every entry. A string the field already holds, and nothing
8065 /// for a field holding nothing, sends nothing. A value no text field can hold is refused,
8066 /// and so is a board without the field, or with a field of that name that is not a text
8067 /// field: reads and writes never create a field.
8068 fn projection_writes(
8069 &self,
8070 fields: &Value,
8071 held: Option<&BTreeMap<String, String>>,
8072 metadata: &BTreeMap<String, Value>,
8073 keys: Option<&[&str]>,
8074 ) -> Result<(Vec<ProjectionWrite>, BTreeMap<String, String>), SourceError> {
8075 let mut projected = held.cloned().unwrap_or_default();
8076 let mut writes = Vec::new();
8077 for projection in self
8078 .metadata_fields
8079 .iter()
8080 .filter(|projection| keys.is_none_or(|keys| keys.contains(&&*projection.key)))
8081 {
8082 let wanted = projection.projected(metadata, &self.name)?;
8083 let field = self.projection_field(fields, projection)?;
8084 let holding = projected.get(&*projection.field);
8085 match wanted {
8086 Projected::Text(text) if holding != Some(&text) => {
8087 writes.push(ProjectionWrite::Set {
8088 field,
8089 text: text.clone(),
8090 });
8091 projected.insert(projection.field.to_string(), text);
8092 }
8093 Projected::Nothing if holding.is_some() => {
8094 writes.push(ProjectionWrite::Clear { field });
8095 projected.remove(&*projection.field);
8096 }
8097 Projected::Text(_) | Projected::Nothing => {}
8098 }
8099 }
8100 Ok((writes, projected))
8101 }
8102
8103 /// The id of the board text field one projection writes, or the refusal naming what the
8104 /// board has instead.
8105 fn projection_field(
8106 &self,
8107 fields: &Value,
8108 projection: &MetadataField,
8109 ) -> Result<String, SourceError> {
8110 let refuse = |detail: String| SourceError::Refused {
8111 message: format!(
8112 "source {} projects metadata key {:?} onto the board text field {:?}, and \
8113 {detail}; next: run `onetaskgraph sources fields {} --apply` to create it as a \
8114 text field, or name another field in metadata_fields",
8115 self.name, &*projection.key, &*projection.field, self.name
8116 ),
8117 };
8118 let Some(field) = Board::field(fields, &projection.field)? else {
8119 return Err(refuse("this board has no field of that name".to_owned()));
8120 };
8121 let typename = optional_str(field, "__typename")?.unwrap_or("field of another type");
8122 let data_type = optional_str(field, "dataType")?;
8123 if typename != "ProjectV2Field" || data_type != Some("TEXT") {
8124 return Err(refuse(format!(
8125 "this board's field of that name is not a text field (it is a {}), so it is \
8126 left as it is; rename or remove that field first",
8127 data_type.unwrap_or(typename)
8128 )));
8129 }
8130 Ok(required_str(field, "id")?.to_owned())
8131 }
8132
8133 /// Apply one priority write to one board item.
8134 async fn write_priority(
8135 &self,
8136 board_id: &str,
8137 item_id: &str,
8138 write: &PriorityWrite,
8139 ) -> Result<(), SourceError> {
8140 match write {
8141 PriorityWrite::Select { field, option } => {
8142 self.set_item_field(
8143 board_id,
8144 item_id,
8145 field,
8146 json!({"singleSelectOptionId": option}),
8147 )
8148 .await
8149 }
8150 PriorityWrite::Clear { field } => {
8151 let data = self
8152 .graphql(
8153 graphql::CLEAR_FIELD,
8154 json!({"input":{"projectId":board_id,"itemId":item_id,"fieldId":field},
8155 "readPriority":false,"priorityName":PRIORITY_FIELD}),
8156 )
8157 .await?;
8158 let returned = data
8159 .pointer("/clearProjectV2ItemFieldValue/projectV2Item")
8160 .ok_or_else(|| SourceError::Malformed {
8161 message: "GitHub field clear returned no project item".into(),
8162 })?;
8163 if required_str(returned, "id")? != item_id {
8164 return Err(SourceError::Malformed {
8165 message: "GitHub field clear returned the wrong project item".into(),
8166 });
8167 }
8168 Ok(())
8169 }
8170 }
8171 }
8172
8173 /// The refusal a priority is answered with by an instance configured with no
8174 /// `priority_mapping`, which holds none.
8175 fn holds_no_priority(&self) -> SourceError {
8176 SourceError::Refused {
8177 message: format!(
8178 "source {} holds no task priority: its configuration sets no priority_mapping; \
8179 next: set priority_mapping on this source, then run `onetaskgraph sources \
8180 fields {} --apply` to set its board up",
8181 self.name, self.name
8182 ),
8183 }
8184 }
8185
8186 /// Set one task's priority and nothing else; see [`TaskSource::set_task_priority`].
8187 ///
8188 /// One field write — a select, or a clear for `none` — and no title, body, label, state
8189 /// or `Status` request. Clearing a priority an item does not hold sends nothing.
8190 async fn set_priority(
8191 &self,
8192 id: &NativeId,
8193 priority: Priority,
8194 ) -> Result<Option<Priority>, SourceError> {
8195 if self.priorities.is_none() {
8196 return Err(self.holds_no_priority());
8197 }
8198 let Some(mut item) = self
8199 .bound_item(id)
8200 .await?
8201 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8202 else {
8203 return Ok(None);
8204 };
8205 if priority == Priority::None && !item.holds_priority() {
8206 return Ok(Some(priority));
8207 }
8208 // The item's own read carries the field's definition whenever it holds a value of
8209 // it, which a clear always does; a select onto an item holding none reads the board.
8210 let board = match (item.carried_board(), item.named_board()) {
8211 (Some(board), _) => board,
8212 (None, Some(id)) if item.defines(PRIORITY_FIELD) => BoardFields {
8213 id,
8214 fields: json!({"nodes": item.fields.clone(), "pageInfo": {"hasNextPage": false}}),
8215 },
8216 _ => self.board_fields().await?,
8217 };
8218 let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? else {
8219 return Ok(Some(priority));
8220 };
8221 let (document, root, input) = match write {
8222 PriorityWrite::Select { field, option } => (
8223 graphql::UPDATE_FIELD,
8224 "updateProjectV2ItemFieldValue",
8225 json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field,"value":{"singleSelectOptionId":option}}),
8226 ),
8227 PriorityWrite::Clear { field } => (
8228 graphql::CLEAR_FIELD,
8229 "clearProjectV2ItemFieldValue",
8230 json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field}),
8231 ),
8232 };
8233 let data = self
8234 .graphql(
8235 document,
8236 json!({"input":input,"readPriority":true,"priorityName":PRIORITY_FIELD}),
8237 )
8238 .await?;
8239 let returned = data
8240 .get(root)
8241 .and_then(|value| value.get("projectV2Item"))
8242 .ok_or_else(|| SourceError::Malformed {
8243 message: "GitHub priority write returned no project item".into(),
8244 })?;
8245 if required_str(returned, "id")? != item.item_id {
8246 return Err(SourceError::Malformed {
8247 message: "GitHub priority write returned the wrong project item".into(),
8248 });
8249 }
8250 let value = returned
8251 .get("fieldValueByName")
8252 .ok_or_else(|| SourceError::Malformed {
8253 message: "GitHub priority write returned no priority read-back".into(),
8254 })?;
8255 if !value.is_null()
8256 && value.pointer("/field/name").and_then(Value::as_str) != Some(PRIORITY_FIELD)
8257 {
8258 return Err(SourceError::Malformed {
8259 message: "GitHub priority read-back is not a Priority field value".into(),
8260 });
8261 }
8262 let values = if value.is_null() {
8263 Vec::new()
8264 } else {
8265 vec![value.clone()]
8266 };
8267 item.priority = self.held_priority(&values)?;
8268 let answer = item.task()?.priority;
8269 self.remember_written(item, false)?;
8270 Ok(Some(answer))
8271 }
8272
8273 /// Replace one task's visible body and nothing else; see
8274 /// [`TaskSource::set_task_content`].
8275 ///
8276 /// One update of the body, which differs from the body GitHub holds only outside the
8277 /// metadata slot — the slot is kept byte for byte, so every caller key and every list
8278 /// this source keeps there reads back as it was. A body that would not change is not
8279 /// sent at all.
8280 async fn replace_content(
8281 &self,
8282 id: &NativeId,
8283 content: &str,
8284 ) -> Result<Option<()>, SourceError> {
8285 let Some(mut item) = self
8286 .bound_item(id)
8287 .await?
8288 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8289 else {
8290 return Ok(None);
8291 };
8292 let held = item.raw_body.clone().unwrap_or_default();
8293 let body = with_content(&held, content)?;
8294 // Checked before anything is sent: content ending in what this source reads as its own
8295 // metadata slot would read back as metadata rather than as the content it was.
8296 let (visible, slot) = metadata_body(Some(body.clone()))?;
8297 if visible.as_deref().unwrap_or_default() != content || slot != item.slot {
8298 return Err(SourceError::Refused {
8299 message: format!(
8300 "this content ends in what source {} reads as its own metadata slot \
8301 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8302 as content; next: remove that trailing block from the content",
8303 self.name
8304 ),
8305 });
8306 }
8307 if body != held {
8308 self.update_content(item.content_kind, &item.id, json!({"body": body}))
8309 .await?;
8310 }
8311 item.body = visible.filter(|value| !value.is_empty());
8312 item.raw_body = Some(body);
8313 item.slot = slot;
8314 self.remember_written(item, false)?;
8315 Ok(Some(()))
8316 }
8317
8318 /// Apply one targeted update to one task; see [`TaskSource::update_task`].
8319 ///
8320 /// One read of the item — which carries the board's field definitions and the issue's
8321 /// `blockedBy`, so neither is read again — and then only what differs from it: the
8322 /// `Status` option and the `Priority` field together in one request, the `blockedBy`
8323 /// additions and removals the named edges differ by, and last one `updateIssue` carrying
8324 /// the title, the body — visible content and metadata slot together — and a state change.
8325 /// So an update naming any of title, body, metadata, status and priority is one read and
8326 /// at most two writes. The body goes last so that a write refused part-way leaves it, and
8327 /// the metadata in it, as it stood. A terminal status selects its option and then closes,
8328 /// as a whole write does; an open one selects its option and then reopens. The origin
8329 /// field is never written: an update is of an item that already exists, whose origin is
8330 /// what it is.
8331 ///
8332 /// The task answered is the item as those writes left it, built from the read and what was
8333 /// sent rather than read again — the same record a later read in this run answers from.
8334 async fn targeted_update(
8335 &self,
8336 id: &NativeId,
8337 update: &TaskUpdate,
8338 ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
8339 // Everything this source can refuse without reading the item is refused first, in the
8340 // words a whole write of the same fields is refused with.
8341 update.consistent()?;
8342 if update
8343 .title
8344 .as_deref()
8345 .is_some_and(|title| title.starts_with(DESIGN_TITLE_PREFIX))
8346 {
8347 return Err(SourceError::Refused {
8348 message: format!(
8349 "the title of this task begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
8350 spells a document, so it would read back as one rather than as a task; \
8351 retitle it",
8352 self.name
8353 ),
8354 });
8355 }
8356 if let Some(delivers) = &update.delivers {
8357 TaskRef::listed(
8358 TaskRef::DELIVERS_KEY,
8359 id,
8360 Some(&self.name),
8361 delivers.clone(),
8362 )
8363 .map_err(|message| SourceError::Refused { message })?;
8364 }
8365 if self.priorities.is_none()
8366 && update
8367 .priority
8368 .is_some_and(|priority| priority != Priority::None)
8369 {
8370 return Err(self.holds_no_priority());
8371 }
8372 let target = update
8373 .status
8374 .as_ref()
8375 .map(|status| self.resolved_target(ItemKind::Task, status.category))
8376 .transpose()?;
8377 let Some(mut item) = self
8378 .bound_item(id)
8379 .await?
8380 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8381 else {
8382 return Ok(None);
8383 };
8384 let before = item.task()?;
8385
8386 let mut status_move = None;
8387 if let (Some(status), Some(target)) = (&update.status, target) {
8388 let board = self.status_board(&item).await?;
8389 let (field, option, name) = self
8390 .column_for(&board.fields, ItemKind::Task, status.category, &target)?
8391 .ok_or_else(|| SourceError::Malformed {
8392 message: format!(
8393 "status {} of source {} names no board Status option",
8394 category_name(status.category),
8395 self.name
8396 ),
8397 })?;
8398 let terminal = matches!(target, StatusTarget::Terminal(_, _));
8399 if terminal && item.content_kind == ContentKind::DraftIssue {
8400 return Err(self.closes_a_draft(status.category));
8401 }
8402 let landed = match &target {
8403 StatusTarget::Terminal(_, reason) => {
8404 self.statuses
8405 .status(ItemKind::Task, Some(&name), true, Some(reason.reason()))
8406 }
8407 _ => self
8408 .statuses
8409 .status(ItemKind::Task, Some(&name), false, None),
8410 };
8411 let option_moves = item
8412 .option
8413 .as_deref()
8414 .is_none_or(|held| !held.eq_ignore_ascii_case(&name));
8415 let state_moves = item.content_kind == ContentKind::Issue
8416 && (item.closed != terminal || (terminal && item.status != landed));
8417 if let Some(moves) = Moves::of(option_moves, state_moves) {
8418 status_move = Some(StatusMove {
8419 board: board.id,
8420 field,
8421 option,
8422 name,
8423 target,
8424 landed,
8425 moves,
8426 });
8427 }
8428 }
8429
8430 let mut priority_move = None;
8431 if let Some(priority) = update.priority
8432 && self.priorities.is_some()
8433 && item.priority != HeldPriority::Read(priority)
8434 {
8435 let board = match (item.carried_board(), item.named_board()) {
8436 (Some(board), _) => board,
8437 (None, Some(board)) if item.defines(PRIORITY_FIELD) => BoardFields {
8438 id: board,
8439 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
8440 },
8441 _ => self.board_fields().await?,
8442 };
8443 if let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? {
8444 priority_move = Some((board.id, write, priority));
8445 }
8446 }
8447
8448 // The metadata keys this update names that a board text field projects, and what
8449 // that does to those fields — refused, as a whole write refuses it, before anything
8450 // is sent.
8451 let touched: Vec<&str> = update
8452 .metadata_set
8453 .keys()
8454 .chain(update.metadata_remove.iter())
8455 .map(MetadataKey::as_str)
8456 .collect();
8457 let mut projection_move = None;
8458 if self
8459 .metadata_fields
8460 .iter()
8461 .any(|projection| touched.contains(&&*projection.key))
8462 {
8463 let mut metadata = item.slot.clone();
8464 for (key, value) in &update.metadata_set {
8465 metadata.insert(key.as_str().to_owned(), value.clone());
8466 }
8467 for key in &update.metadata_remove {
8468 metadata.remove(key.as_str());
8469 }
8470 let board = self.projection_board(&item).await?;
8471 let (writes, projected) = self.projection_writes(
8472 &board.fields,
8473 Some(&item.projected),
8474 &metadata,
8475 Some(&touched),
8476 )?;
8477 projection_move = Some((board.id, writes, projected));
8478 }
8479
8480 // Resolved before the body is composed, because a far end `blockedBy` cannot name is
8481 // recorded in the slot, and the slot travels in the one body update below.
8482 let edges = match &update.depends_on {
8483 Some(edges) => Some(
8484 self.partition_edges(
8485 BoardKind::Work(ItemKind::Task),
8486 item.content_kind,
8487 item.blocked_by.as_deref(),
8488 edges,
8489 )
8490 .await?,
8491 ),
8492 None => None,
8493 };
8494
8495 // A record primed from what a producer wrote holds no body anybody read, so an update
8496 // changing the body reads it first: by the `Status` write's own answer when the update
8497 // moves the option — the mutation answering with the issue whole — and otherwise by
8498 // reading the issue. The body is then composed against what GitHub holds now.
8499 let mut option_selected = false;
8500 // The mark stays until a read lands, so an update that writes no body, or one whose
8501 // read fails, leaves the next update of it to read the body too.
8502 if (update.content.is_some()
8503 || !update.metadata_set.is_empty()
8504 || !update.metadata_remove.is_empty()
8505 || update.delivers.is_some()
8506 || edges.is_some())
8507 && self.is_unread(id)
8508 {
8509 let live = match status_move.as_ref().filter(|moving| moving.moves.option()) {
8510 Some(moving) => {
8511 option_selected = true;
8512 Some(
8513 self.select_reading_body(
8514 moving.board.as_str(),
8515 &item.item_id,
8516 &moving.field,
8517 &moving.option,
8518 )
8519 .await?,
8520 )
8521 }
8522 None => {
8523 self.forget(id)?;
8524 let Some(read) = self.item_by_id(id).await? else {
8525 return Ok(None);
8526 };
8527 read.raw_body
8528 }
8529 };
8530 let (visible, read_slot) = metadata_body(live.clone())?;
8531 self.mark_read(id);
8532 item.raw_body = live;
8533 item.body = visible;
8534 item.slot = read_slot;
8535 }
8536
8537 let mut slot = item.slot.clone();
8538 for (key, value) in &update.metadata_set {
8539 slot.insert(key.as_str().to_owned(), value.clone());
8540 }
8541 for key in &update.metadata_remove {
8542 slot.remove(key.as_str());
8543 }
8544 if let Some(delivers) = &update.delivers {
8545 set_task_list(&mut slot, TaskRef::DELIVERS_KEY, delivers);
8546 }
8547 if let Some((_, recorded)) = &edges {
8548 record_edges(&mut slot, recorded);
8549 }
8550 let held = item.raw_body.clone().unwrap_or_default();
8551 let content = match &update.content {
8552 Some(content) => with_content(&held, content)?,
8553 None => held.clone(),
8554 };
8555 // A slot holding what it held is kept byte for byte, compared as JSON rather than as
8556 // the body's bytes, as a metadata write compares it: a slot a person spelled with
8557 // other whitespace would otherwise be re-encoded, which is a write nobody asked for.
8558 let body = if slot == item.slot {
8559 content
8560 } else {
8561 with_slot(&content, &slot)?
8562 };
8563 // Checked before anything is sent, as a content write checks it: content ending in
8564 // what this source reads as its own slot would read back as metadata.
8565 let (visible, read) = metadata_body(Some(body.clone()))?;
8566 let wanted = update.content.as_deref().or(item.body.as_deref());
8567 if visible.as_deref().unwrap_or_default() != wanted.unwrap_or_default() || read != slot {
8568 return Err(SourceError::Refused {
8569 message: format!(
8570 "this content ends in what source {} reads as its own metadata slot \
8571 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8572 as content; next: remove that trailing block from the content",
8573 self.name
8574 ),
8575 });
8576 }
8577 let recorded_moves =
8578 slot.get(DependencyEdge::RECORDED_KEY) != item.slot.get(DependencyEdge::RECORDED_KEY);
8579
8580 // One `updateIssue` carries all three, because every mutation spends the secondary
8581 // limiter and the title, body and state are one mutation's inputs.
8582 let mut fields = serde_json::Map::new();
8583 if let Some(title) = update.title.as_ref().filter(|title| **title != item.title) {
8584 fields.insert("title".to_owned(), json!(title));
8585 }
8586 if body != held {
8587 fields.insert("body".to_owned(), json!(body));
8588 }
8589 if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.state()) {
8590 fields.insert("stateInput".to_owned(), state_input(Some(&moving.target)));
8591 }
8592 // **The body is written last, and that is the guarantee a refusal part-way keeps.**
8593 // GitHub runs no two requests as one, and runs one document's mutation fields in order
8594 // without undoing an earlier field when a later one fails — so a body written before a
8595 // board field the board then refused would be left changed. Written after every other
8596 // write has landed, a refusal anywhere leaves the item's body, and every metadata key
8597 // it carries, exactly as they stood. So the `Status` option and the `Priority` field go
8598 // first, together in one request — a terminal option selected before the issue
8599 // closes, as a whole write does — then the `blockedBy` difference, then the body.
8600 let mut board_writes: Vec<(&BoardId, (String, Value))> = Vec::new();
8601 let mut clears: Vec<(&BoardId, &str)> = Vec::new();
8602 if let Some(moving) = status_move
8603 .as_ref()
8604 .filter(|moving| moving.moves.option() && !option_selected)
8605 {
8606 board_writes.push((
8607 &moving.board,
8608 (
8609 moving.field.clone(),
8610 json!({"singleSelectOptionId": moving.option}),
8611 ),
8612 ));
8613 }
8614 match &priority_move {
8615 Some((board, PriorityWrite::Select { field, option }, _)) => board_writes.push((
8616 board,
8617 (field.clone(), json!({"singleSelectOptionId": option})),
8618 )),
8619 Some((board, PriorityWrite::Clear { field }, _)) => clears.push((board, field)),
8620 None => {}
8621 }
8622 if let Some((board, writes, _)) = &projection_move {
8623 for write in writes {
8624 let (mut values, mut cleared) = (Vec::new(), Vec::new());
8625 write.join(&mut values, &mut cleared);
8626 board_writes.extend(values.into_iter().map(|value| (board, value)));
8627 clears.extend(cleared.into_iter().map(|field| (board, field)));
8628 }
8629 }
8630 let mut boards: Vec<&BoardId> = board_writes.iter().map(|(board, _)| *board).collect();
8631 boards.extend(clears.iter().map(|(board, _)| *board));
8632 boards.dedup_by(|one, other| one.as_str() == other.as_str());
8633 for board in boards {
8634 let writes = board_writes
8635 .iter()
8636 .filter(|(on, _)| on.as_str() == board.as_str())
8637 .map(|(_, write)| write.clone())
8638 .collect::<Vec<_>>();
8639 let cleared = clears
8640 .iter()
8641 .filter(|(on, _)| on.as_str() == board.as_str())
8642 .map(|(_, field)| *field)
8643 .collect::<Vec<_>>();
8644 self.set_item_fields(board.as_str(), &item.item_id, &writes, &cleared)
8645 .await?;
8646 }
8647 let mut blocked_by_moved = false;
8648 if let Some((native, _)) = &edges
8649 && item.content_kind == ContentKind::Issue
8650 {
8651 blocked_by_moved = self
8652 .reconcile_blocked_by(
8653 &item.id,
8654 native,
8655 Issue::Existing(item.blocked_by.as_deref()),
8656 )
8657 .await?;
8658 }
8659 if !fields.is_empty() {
8660 self.update_content(item.content_kind, &item.id, Value::Object(fields))
8661 .await?;
8662 }
8663
8664 // The `blockedBy` this item was read with is what the write just changed, so it is no
8665 // answer any more: a later write in this command would reconcile against it — re-adding
8666 // what it kept and never removing what it dropped — and a forward-edge read would
8667 // answer with it. Left unknown, each asks GitHub instead.
8668 if blocked_by_moved {
8669 item.blocked_by = None;
8670 }
8671 if let Some(title) = &update.title {
8672 item.title.clone_from(title);
8673 }
8674 item.body = visible.filter(|value| !value.is_empty());
8675 item.raw_body = (!body.is_empty() || item.raw_body.is_some()).then_some(body);
8676 item.slot = slot;
8677 if let Some(delivers) = &update.delivers {
8678 item.delivers.clone_from(delivers);
8679 }
8680 if let Some(moving) = status_move {
8681 item.closed = matches!(moving.target, StatusTarget::Terminal(_, _))
8682 && item.content_kind == ContentKind::Issue;
8683 item.status = moving.landed;
8684 item.option = Some(moving.name);
8685 }
8686 if let Some((_, _, priority)) = priority_move {
8687 item.priority = HeldPriority::Read(priority);
8688 }
8689 if let Some((_, _, projected)) = projection_move {
8690 item.projected = projected;
8691 }
8692 let task = item.task()?;
8693 let mut written = update.changed(&before, &task);
8694 if blocked_by_moved || recorded_moves {
8695 written.insert(UpdatedField::DependsOn);
8696 }
8697 self.remember_written(item, false)?;
8698 Ok(Some(TaskUpdateOutcome {
8699 task,
8700 written,
8701 delivers_before: before.delivers,
8702 }))
8703 }
8704
8705 /// Replace one issue's visible body and its [`MetadataKey::TEMPLATE_KEY`] slot entry
8706 /// together, and nothing else; see [`TaskSource::set_task_rendering`].
8707 ///
8708 /// One update of the body: the content outside the slot, and inside it that one entry,
8709 /// every other entry kept as it was. This source keeps no template answers — an issue has
8710 /// no room beside itself that is not its body, and answers written there would duplicate
8711 /// what the content already says and count against GitHub's body limit — so `answers`
8712 /// reaches nothing here. A body that would not change is not sent at all.
8713 async fn replace_rendering(
8714 &self,
8715 id: &NativeId,
8716 kind: BoardKind,
8717 content: &str,
8718 provenance: &Value,
8719 assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
8720 ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
8721 let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
8722 return Ok(None);
8723 };
8724 let held = item.raw_body.clone().unwrap_or_default();
8725 let mut slot = item.slot.clone();
8726 slot.insert(MetadataKey::TEMPLATE_KEY.to_owned(), provenance.clone());
8727 let rewritten;
8728 let content = if let Some(assets) = assets {
8729 let uploads = self
8730 .upload_assets(item.own_repository.as_ref(), assets)
8731 .await?;
8732 rewritten =
8733 onetaskgraph_plugin_api::serve_asset_references(content, &mut slot, &uploads);
8734 rewritten.as_str()
8735 } else {
8736 content
8737 };
8738 let body = with_slot(&with_content(&held, content)?, &slot)?;
8739 // Checked before anything is sent, as a content write checks it.
8740 let (visible, read) = metadata_body(Some(body.clone()))?;
8741 if visible.as_deref().unwrap_or_default() != content || read != slot {
8742 return Err(SourceError::Refused {
8743 message: format!(
8744 "this content ends in what source {} reads as its own metadata slot \
8745 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8746 as content; next: remove that trailing block from the template",
8747 self.name
8748 ),
8749 });
8750 }
8751 if body != held {
8752 self.update_content(item.content_kind, &item.id, json!({"body": body}))
8753 .await?;
8754 }
8755 item.body = visible.filter(|value| !value.is_empty());
8756 item.raw_body = Some(body);
8757 item.slot = read;
8758 self.remember_written(item, false)?;
8759 Ok(Some(onetaskgraph_plugin_api::AssetsWritten {
8760 id: id.clone(),
8761 content: Some(content.to_owned()),
8762 }))
8763 }
8764
8765 async fn set_item_field(
8766 &self,
8767 board_id: &str,
8768 item_id: &str,
8769 field_id: &str,
8770 value: Value,
8771 ) -> Result<(), SourceError> {
8772 let data = self
8773 .graphql(
8774 graphql::UPDATE_FIELD,
8775 json!({"input":{
8776 "projectId":board_id,"itemId":item_id,"fieldId":field_id,"value":value
8777 },"readPriority":false,"priorityName":PRIORITY_FIELD}),
8778 )
8779 .await?;
8780 field_update_item(&data, item_id).map(|_| ())
8781 }
8782
8783 /// GitHub accepts one value per field mutation; aliases combine those mutations in
8784 /// one request — writes in the order given, then clears. A lone write is
8785 /// [`graphql::UPDATE_FIELD`] and a lone clear [`graphql::CLEAR_FIELD`]; more than one
8786 /// request's slots hold go in further requests of the same document, in order. Every
8787 /// returned item id is checked, including optional aliases.
8788 async fn set_item_fields(
8789 &self,
8790 board: &str,
8791 item: &str,
8792 fields: &[(String, Value)],
8793 clears: &[&str],
8794 ) -> Result<(), SourceError> {
8795 match (fields, clears) {
8796 ([], []) => return Ok(()),
8797 ([(field, value)], []) => {
8798 return self.set_item_field(board, item, field, value.clone()).await;
8799 }
8800 ([], [field]) => {
8801 return self
8802 .write_priority(
8803 board,
8804 item,
8805 &PriorityWrite::Clear {
8806 field: (*field).to_owned(),
8807 },
8808 )
8809 .await;
8810 }
8811 _ => {}
8812 }
8813 // Any field of the request stands in for an input a slot leaves out, which GitHub
8814 // still requires to be well formed although it never runs it.
8815 let filler = fields
8816 .first()
8817 .map_or_else(|| clears[0], |(field, _)| field.as_str());
8818 let mut writes = fields.iter();
8819 let mut cleared = clears.iter();
8820 loop {
8821 let batch: Vec<&(String, Value)> =
8822 writes.by_ref().take(FIELD_WRITE_SLOTS.len()).collect();
8823 let batch_clears: Vec<&&str> = cleared.by_ref().take(FIELD_CLEAR_SLOTS.len()).collect();
8824 if batch.is_empty() && batch_clears.is_empty() {
8825 return Ok(());
8826 }
8827 let mut variables = serde_json::Map::new();
8828 for (index, slot) in FIELD_WRITE_SLOTS.iter().enumerate() {
8829 let input = match batch.get(index) {
8830 Some((field, value)) => {
8831 json!({"projectId":board,"itemId":item,"fieldId":field,"value":value})
8832 }
8833 None => json!({"projectId":board,"itemId":item,"fieldId":filler,
8834 "value":{"text":""}}),
8835 };
8836 variables.insert(slot.variable.to_owned(), input);
8837 variables.insert(slot.include.to_owned(), json!(index < batch.len()));
8838 }
8839 for (index, slot) in FIELD_CLEAR_SLOTS.iter().enumerate() {
8840 let field = batch_clears.get(index).map_or(filler, |field| **field);
8841 variables.insert(
8842 slot.variable.to_owned(),
8843 json!({"projectId":board,"itemId":item,"fieldId":field}),
8844 );
8845 variables.insert(slot.include.to_owned(), json!(index < batch_clears.len()));
8846 }
8847 let data = self
8848 .graphql(graphql::UPDATE_FIELDS, Value::Object(variables))
8849 .await?;
8850 let sent = FIELD_WRITE_SLOTS[..batch.len()]
8851 .iter()
8852 .chain(&FIELD_CLEAR_SLOTS[..batch_clears.len()]);
8853 for slot in sent {
8854 let alias = slot.alias;
8855 let returned = data
8856 .get(alias)
8857 .and_then(|value| value.get("projectV2Item"))
8858 .ok_or_else(|| SourceError::Malformed {
8859 message: format!("GitHub field update {alias} returned no project item"),
8860 })?;
8861 if required_str(returned, "id")? != item {
8862 return Err(SourceError::Malformed {
8863 message: format!(
8864 "GitHub field update {alias} returned the wrong project item"
8865 ),
8866 });
8867 }
8868 }
8869 }
8870 }
8871
8872 async fn native_dependency_ids(&self, id: &NativeId) -> Result<Vec<String>, SourceError> {
8873 let mut after: Option<String> = None;
8874 let mut ids = Vec::new();
8875 loop {
8876 let data = self
8877 .graphql(
8878 graphql::ISSUE_DEPENDENCIES,
8879 json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after}),
8880 )
8881 .await?;
8882 let connection =
8883 data.pointer("/node/blockedBy")
8884 .ok_or_else(|| SourceError::Malformed {
8885 message: "GitHub dependency response has no blockedBy connection".into(),
8886 })?;
8887 ids.extend(
8888 connection
8889 .get("nodes")
8890 .and_then(Value::as_array)
8891 .ok_or_else(|| SourceError::Malformed {
8892 message: "GitHub dependency response nodes is not an array".into(),
8893 })?
8894 .iter()
8895 .map(|value| required_str(value, "id").map(str::to_owned))
8896 .collect::<Result<Vec<_>, _>>()?,
8897 );
8898 let next = next_cursor(connection)?;
8899 if let Some(next) = &next {
8900 validate_cursor_progress(after.as_deref(), &next.0)?;
8901 }
8902 after = next.map(|cursor| cursor.0);
8903 if after.is_none() {
8904 return Ok(ids);
8905 }
8906 }
8907 }
8908
8909 async fn dependencies(
8910 &self,
8911 id: &NativeId,
8912 near_kind: ItemKind,
8913 direction: Direction,
8914 page: &PageRequest,
8915 ) -> Result<Page<DependencyEdge>, SourceError> {
8916 validate_page(page)?;
8917 let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
8918 let cursor = page.cursor.as_ref().map(|c| c.0.as_str());
8919 let recorded = recorded_offset(cursor, direction)?;
8920 // What this issue is blocked by, when a read of it by its own id in this command
8921 // already carried the whole connection — a copy reads the item it writes before it
8922 // reads its edges — and the page asked for is the whole of it, or the recorded tail
8923 // after it. Answered from that read, in the shape the dependency read answers in;
8924 // anything else is asked of GitHub.
8925 let carried = match direction {
8926 Direction::DependsOn => self
8927 .resolved_cache()?
8928 .get(id)
8929 .filter(|item| item.content_kind == ContentKind::Issue)
8930 .and_then(|item| Some((item.blocked_by.clone()?, item.raw_body.clone()))),
8931 Direction::DependedOnBy => None,
8932 }
8933 .filter(|(nodes, _)| recorded.is_some() || (cursor.is_none() && nodes.len() <= limit));
8934 // Asked for even in the recorded phase, whose page reads nothing from the
8935 // connection: `__typename` is what says whether this item has a native
8936 // relationship at all, and that is what decides which far ends the reserved key is
8937 // allowed to hold.
8938 let data = match carried {
8939 Some((nodes, body)) => json!({"node":{"__typename":"Issue","body":body,
8940 "blockedBy":{"nodes":nodes,"pageInfo":{"hasNextPage":false,"endCursor":null}}}}),
8941 None => {
8942 let after = if recorded.is_some() { None } else { cursor };
8943 let first = page.limit.min(MAX_PAGE_SIZE);
8944 match direction {
8945 Direction::DependsOn => {
8946 self.graphql(
8947 graphql::ISSUE_DEPENDS_ON,
8948 json!({"id":id.0,"first":first,"after":after,"owner":self.owner,
8949 "number":self.project_number,
8950 "boardItems":BOARD_ITEMS_PAGE_SIZE}),
8951 )
8952 .await?
8953 }
8954 Direction::DependedOnBy => {
8955 self.graphql(
8956 graphql::ISSUE_DEPENDED_ON_BY,
8957 json!({"id":id.0,"first":first,"after":after}),
8958 )
8959 .await?
8960 }
8961 }
8962 }
8963 };
8964 let draft_slot = draft_slot(&data, id, self.project_number)?;
8965 let node =
8966 data.get("node")
8967 .filter(|v| !v.is_null())
8968 .ok_or_else(|| SourceError::Refused {
8969 message: format!(
8970 "GitHub item {} was not found or does not support dependencies",
8971 id.0
8972 ),
8973 })?;
8974 let connection_name = match direction {
8975 Direction::DependsOn => "blockedBy",
8976 Direction::DependedOnBy => "blocking",
8977 };
8978 // A draft has neither `blockedBy` nor `blocking`, so nothing it depends on can be
8979 // named natively and the reserved key may hold any far end. An issue's connections
8980 // hold issues, and this source reads them at the near item's own level.
8981 let natively_names = (required_str(node, "__typename")? == "Issue").then_some(near_kind);
8982 if let Some(offset) = recorded {
8983 return Ok(recorded_page(
8984 self.recorded_edges(
8985 id,
8986 near_kind,
8987 direction,
8988 natively_names,
8989 node,
8990 draft_slot.as_ref(),
8991 )
8992 .await?,
8993 offset,
8994 limit,
8995 ));
8996 }
8997 if natively_names.is_none() {
8998 return Ok(recorded_page(
8999 self.recorded_edges(
9000 id,
9001 near_kind,
9002 direction,
9003 natively_names,
9004 node,
9005 draft_slot.as_ref(),
9006 )
9007 .await?,
9008 0,
9009 limit,
9010 ));
9011 }
9012 let connection = node
9013 .get(connection_name)
9014 .ok_or_else(|| SourceError::Malformed {
9015 message: "GitHub dependency response is missing its connection".into(),
9016 })?;
9017 let nodes = connection
9018 .get("nodes")
9019 .and_then(Value::as_array)
9020 .ok_or_else(|| SourceError::Malformed {
9021 message: "GitHub dependency response nodes is not an array".into(),
9022 })?;
9023 // `from` depends on `to`, always. GitHub spells the same relationship from either
9024 // end — `blockedBy` lists what this item waits on, `blocking` lists what waits on
9025 // it — so the near item is `from` in one direction and `to` in the other.
9026 let items = nodes
9027 .iter()
9028 .map(|value| {
9029 let related = NativeId(required_str(value, "id")?.into());
9030 let related_kind = related_kind(value)?;
9031 let (from, to) = match direction {
9032 Direction::DependsOn => (
9033 DependencyEndpoint::from_native(id.clone(), near_kind),
9034 DependencyEndpoint::from_native(related, related_kind),
9035 ),
9036 Direction::DependedOnBy => (
9037 DependencyEndpoint::from_native(related, related_kind),
9038 DependencyEndpoint::from_native(id.clone(), near_kind),
9039 ),
9040 };
9041 Ok(DependencyEdge {
9042 from,
9043 to,
9044 kind: DependencyKind::Blocks,
9045 })
9046 })
9047 .collect::<Result<Vec<_>, SourceError>>()?;
9048 let mut next = next_cursor(connection)?;
9049 if let Some(next) = &next {
9050 validate_cursor_progress(cursor, &next.0)?;
9051 }
9052 // The recorded far ends ride on the last page of `blockedBy`, whose answer carried the
9053 // body they are recorded in, as far as the page has room — so they cost no request of
9054 // their own — and only what does not fit is resumed by a cursor.
9055 let mut items = items;
9056 if next.is_none() {
9057 let recorded = self
9058 .recorded_edges(
9059 id,
9060 near_kind,
9061 direction,
9062 natively_names,
9063 node,
9064 draft_slot.as_ref(),
9065 )
9066 .await?;
9067 let room = limit.saturating_sub(items.len());
9068 let total = recorded.len();
9069 items.extend(recorded.into_iter().take(room));
9070 if total > room {
9071 next = Some(Cursor(format!("{RECORDED_CURSOR}{room}")));
9072 }
9073 }
9074 Ok(Page { items, next })
9075 }
9076
9077 /// The edges this item records under [`DependencyEdge::RECORDED_KEY`], which is where
9078 /// a far end in another source has to live: no GitHub issue relationship can name one.
9079 ///
9080 /// Only forwards. The reverse of a recorded edge is derived from the far end, and this
9081 /// source never writes one down.
9082 ///
9083 /// The metadata lives in the item's own body slot, and `node` is the dependency read's
9084 /// own answer, which carries an issue's body — so an issue's recorded edges cost no
9085 /// request beyond the read already made, and reading the board for them would be a
9086 /// walk of every item for one field of one. A draft has no body in that answer, because
9087 /// a draft is not an issue, so a draft's are read off its own read by id — never off a
9088 /// listing of the board, which can be behind on the very item asked about.
9089 async fn recorded_edges(
9090 &self,
9091 id: &NativeId,
9092 near_kind: ItemKind,
9093 direction: Direction,
9094 natively_names: Option<ItemKind>,
9095 node: &Value,
9096 draft_slot: Option<&Option<BTreeMap<String, Value>>>,
9097 ) -> Result<Vec<DependencyEdge>, SourceError> {
9098 if direction != Direction::DependsOn {
9099 return Ok(Vec::new());
9100 }
9101 let slot = match (node.get("body"), draft_slot) {
9102 (Some(body), _) if natively_names.is_some() => {
9103 metadata_body(body.as_str().map(str::to_owned))?.1
9104 }
9105 // A draft the dependency read itself answered: this board's, or none of its.
9106 (_, Some(Some(slot))) => slot.clone(),
9107 (_, Some(None)) => return Ok(Vec::new()),
9108 _ => {
9109 let Some(item) = self.bound_item(id).await? else {
9110 return Ok(Vec::new());
9111 };
9112 item.slot
9113 }
9114 };
9115 DependencyEdge::recorded(&slot, id, near_kind, &self.name, natively_names)
9116 .map_err(|message| SourceError::Malformed { message })
9117 }
9118
9119 fn configured_repository(&self) -> Result<&RepositoryTarget, SourceError> {
9120 self.repository
9121 .as_ref()
9122 .ok_or_else(|| SourceError::Refused {
9123 message: format!(
9124 "source {} has no repository configured, and a GitHub Projects board has no \
9125 repository of its own to create an issue in; set repository: owner/name on \
9126 this source",
9127 self.name
9128 ),
9129 })
9130 }
9131
9132 /// The repository one new issue is created in, under the rule [`RepositoryTarget`]
9133 /// states.
9134 ///
9135 /// The fallback is demanded first, whichever arm answers: a write without a configured
9136 /// repository is refused naming the field exactly as it was before the rule existed,
9137 /// so a source that could not write before cannot write now, rather than writing for
9138 /// the one item whose own field happens to decide it.
9139 ///
9140 /// Everything this refuses is refused before `createIssue`, so a refusal leaves no
9141 /// issue behind: an entry that is not a repository on [`RepositoryTarget::HOST`], an
9142 /// entry owned by someone other than the owner of the parent issue's repository —
9143 /// GitHub accepts a sub-issue from another repository of the same owner and from no
9144 /// other, so `addSubIssue` would refuse it after the issue existed — a parent the
9145 /// board does not hold, and a parent that is a draft, which GitHub gives no sub-issues,
9146 /// both of which `addSubIssue` would likewise refuse too late. Whether the entry exists
9147 /// and is visible to the token is checked where its node id is resolved, still before
9148 /// `createIssue`. The parent is read by its own id through [`Self::item_by_id`] — never
9149 /// looked up in a listing of the board, which can be minutes behind an issue its own
9150 /// `projectItems` already places on it — and that read answers first from this process's
9151 /// own record, so a project created moments ago in this command answers though GitHub
9152 /// has not caught up.
9153 async fn creation_target(
9154 &self,
9155 incoming: &Incoming<'_>,
9156 ) -> Result<RepositoryTarget, SourceError> {
9157 let what = format!(
9158 "{} {:?}",
9159 incoming.written.kind().describes(),
9160 incoming.title
9161 );
9162 self.creation_rule(incoming.repositories, incoming.parent, &what)
9163 .await
9164 }
9165
9166 /// [`Self::creation_target`]'s rule over what it reads of the item — its `repositories`
9167 /// and the project issue it is filed under — with `what` naming the item in a refusal: the
9168 /// one place the rule is, so the create, [`TaskSource::creation_repository`] and the
9169 /// write-time read of who can read the issue ([`TaskSource::visibility`]) cannot answer
9170 /// differently.
9171 ///
9172 /// On a source declared private, an item's one named repository is read for its
9173 /// visibility first — once per engine hold, see the `visibility` module — and one that
9174 /// does not read private decides nothing: the issue is created in the parent's
9175 /// repository, else the configured one. A read that cannot be answered refuses.
9176 async fn creation_rule(
9177 &self,
9178 repositories: &[Repository],
9179 parent: Option<&NativeId>,
9180 what: &str,
9181 ) -> Result<RepositoryTarget, SourceError> {
9182 let parent = match parent {
9183 Some(parent) => Some(self.bound_item(parent).await?.ok_or_else(|| {
9184 SourceError::Refused {
9185 message: format!(
9186 "GitHub project issue {} was not found on the board of source {}, so {} \
9187 cannot be filed under it",
9188 parent.0, self.name, what
9189 ),
9190 }
9191 })?),
9192 None => None,
9193 };
9194 let parents_repository = parent
9195 .as_ref()
9196 .map(|parent| {
9197 // A draft is on the board and so is found, but it has no repository to
9198 // place a task in and GitHub gives it no sub-issues, so `addSubIssue`
9199 // would refuse the task only once `createIssue` had made it.
9200 if parent.content_kind == ContentKind::DraftIssue {
9201 return Err(SourceError::Refused {
9202 message: format!(
9203 "GitHub project item {} on the board of source {} is a draft, \
9204 which cannot have sub-issues, so {} cannot be filed under it",
9205 parent.id.0, self.name, what
9206 ),
9207 });
9208 }
9209 // An issue's repository is where a sub-issue is placed and whose owner it
9210 // is compared against, so a parent whose repository this source cannot
9211 // spell as `owner/name` — GitHub's login grammar is wider than this
9212 // source's floor — is one nothing can be filed under.
9213 parent
9214 .own_repository
9215 .as_ref()
9216 .and_then(|origin| RepositoryTarget::from_origin(origin).ok())
9217 .ok_or_else(|| SourceError::Malformed {
9218 message: format!(
9219 "GitHub project issue {} on the board of source {} is in {}, which \
9220 is not a {}/owner/name repository this source can place {} in",
9221 parent.id.0,
9222 self.name,
9223 parent
9224 .own_repository
9225 .as_ref()
9226 .map_or("no repository", Repository::as_str),
9227 RepositoryTarget::HOST,
9228 what
9229 ),
9230 })
9231 })
9232 .transpose()?;
9233 self.creation_rule_in_repository(repositories, parents_repository, what)
9234 .await
9235 }
9236
9237 async fn creation_rule_in_repository(
9238 &self,
9239 repositories: &[Repository],
9240 parents_repository: Option<RepositoryTarget>,
9241 what: &str,
9242 ) -> Result<RepositoryTarget, SourceError> {
9243 let fallback = self.configured_repository()?;
9244 match repositories {
9245 [named] => {
9246 let target =
9247 RepositoryTarget::from_origin(named).map_err(|_| SourceError::Refused {
9248 message: format!(
9249 "{} names repository {}, which is not a {}/owner/name repository \
9250 source {} can create an issue in; name one that is, or name none",
9251 what,
9252 named.as_str(),
9253 RepositoryTarget::HOST,
9254 self.name
9255 ),
9256 })?;
9257 // On a source declared private, an issue whose one named repository does not
9258 // read private is created where an item naming none is: the item still names
9259 // that repository in its metadata slot, and its issue stays private.
9260 if self.is_declared_private()
9261 && self.repository_visibility(&target).await?
9262 != onetaskgraph_plugin_api::Visibility::Private
9263 {
9264 return Ok(parents_repository.unwrap_or_else(|| fallback.clone()));
9265 }
9266 if let Some(parents) = &parents_repository
9267 && parents.owner != target.owner
9268 {
9269 return Err(SourceError::Refused {
9270 message: format!(
9271 "{} names repository {}, owned by {}, but its project's issue is in \
9272 {}, owned by {}, and GitHub files a sub-issue only in a repository \
9273 of the same owner as its parent issue; name a repository of {}, or \
9274 name none",
9275 what,
9276 target.slug(),
9277 target.owner,
9278 parents.slug(),
9279 parents.owner,
9280 parents.owner
9281 ),
9282 });
9283 }
9284 Ok(target)
9285 }
9286 _ => Ok(parents_repository.unwrap_or_else(|| fallback.clone())),
9287 }
9288 }
9289
9290 /// The node id of the repository `incoming` is being created in, or the refusal naming
9291 /// the item and the repository the token cannot see.
9292 ///
9293 /// Resolved once per command per repository; see [`Self::repository_cache`].
9294 async fn repository_id(
9295 &self,
9296 repository: &RepositoryTarget,
9297 incoming: &Incoming<'_>,
9298 ) -> Result<String, SourceError> {
9299 if let Some(id) = self.repository_cache()?.get(repository).cloned() {
9300 return Ok(id);
9301 }
9302 let data = self
9303 .graphql(
9304 graphql::REPOSITORY,
9305 json!({"owner":repository.owner,"name":repository.name}),
9306 )
9307 .await?;
9308 self.repository_read(&data, repository, incoming)
9309 }
9310
9311 /// The repository's node id out of an answer carrying the `repository` root, held for
9312 /// the rest of this command, or the refusal naming the item that cannot be created in it.
9313 fn repository_read(
9314 &self,
9315 data: &Value,
9316 repository: &RepositoryTarget,
9317 incoming: &Incoming<'_>,
9318 ) -> Result<String, SourceError> {
9319 let node = data
9320 .get("repository")
9321 .filter(|value| !value.is_null())
9322 .ok_or_else(|| SourceError::Refused {
9323 message: format!(
9324 "GitHub repository {} was not found or is not visible to the token, so {} \
9325 {:?} cannot be created in it",
9326 repository.slug(),
9327 incoming.written.kind().describes(),
9328 incoming.title
9329 ),
9330 })?;
9331 let id = required_str(node, "id")?.to_owned();
9332 self.repository_cache()?
9333 .insert(repository.clone(), id.clone());
9334 Ok(id)
9335 }
9336
9337 fn repository_cache(
9338 &self,
9339 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<RepositoryTarget, String>>, SourceError> {
9340 self.repository_cache
9341 .lock()
9342 .map_err(|_| SourceError::Unavailable {
9343 message: "this source's record of the destination repository was left \
9344 inconsistent by an earlier failure; next: run the command again"
9345 .into(),
9346 })
9347 }
9348
9349 /// Create or update one board item, whichever kind it is.
9350 async fn write_item(
9351 &self,
9352 incoming: &Incoming<'_>,
9353 target: Option<&NativeId>,
9354 depends_on: &[DependencyEdge],
9355 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
9356 let prepared = self.prepare_write(incoming, target, depends_on).await?;
9357 let landed = match &prepared.destination {
9358 // Its content is written last, by `complete_write`, once everything else has landed.
9359 WriteDestination::Existing(item) => Landed {
9360 content_id: item.id.clone(),
9361 item_id: Some(item.item_id.clone()),
9362 url: item.url.clone(),
9363 number: item.number,
9364 },
9365 WriteDestination::New(target) => {
9366 if self.deferring()? {
9367 self.create_filed_issue(
9368 prepared.board.id.as_str(),
9369 target,
9370 incoming,
9371 &prepared.body,
9372 )
9373 .await?
9374 } else {
9375 self.create_and_file_issue(
9376 prepared.board.id.as_str(),
9377 target,
9378 incoming,
9379 &prepared.body,
9380 )
9381 .await?
9382 }
9383 }
9384 };
9385 self.complete_write(incoming, prepared, landed).await
9386 }
9387
9388 /// Everything one write of `incoming` reads and decides before it sends anything: what is
9389 /// there now, where a new item is created, the board's fields, and every value refused while
9390 /// nothing has been written — the item's body included.
9391 async fn prepare_write(
9392 &self,
9393 incoming: &Incoming<'_>,
9394 target: Option<&NativeId>,
9395 depends_on: &[DependencyEdge],
9396 ) -> Result<Prepared, SourceError> {
9397 // Refused before anything is read or written: a task or a project titled the way
9398 // this board spells a document would land as an issue this same source reads back
9399 // as a document, so the field this destination cannot carry is named rather than
9400 // written and silently reclassified.
9401 if let Written::Work(kind, _) = incoming.written
9402 && incoming.title.starts_with(DESIGN_TITLE_PREFIX)
9403 {
9404 return Err(SourceError::Refused {
9405 message: format!(
9406 "the title of this {} begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
9407 spells a document, so it would read back as one rather than as a {}; \
9408 retitle it, or copy it as a document",
9409 kind.marker(),
9410 self.name,
9411 kind.marker()
9412 ),
9413 });
9414 }
9415 // The destination is read by its own id, and whether this board holds it is decided
9416 // by that read — its own `projectItems` — rather than by whether a listing of the
9417 // board happens to include it yet. See the module documentation.
9418 // An existing issue is never moved; a new one is created where the rule says — and
9419 // knowing where is what lets the board's fields and that repository's id be read
9420 // together, before anything below needs either.
9421 let destination = match target {
9422 Some(target) => {
9423 WriteDestination::Existing(Box::new(self.bound_item(target).await?.ok_or_else(
9424 || SourceError::Refused {
9425 message: format!("GitHub destination item {} was not found", target.0),
9426 },
9427 )?))
9428 }
9429 None => {
9430 let target = self.creation_target(incoming).await?;
9431 self.creation_context(&target, incoming).await?;
9432 WriteDestination::New(target)
9433 }
9434 };
9435 let existing = destination.existing();
9436 let board = self
9437 .fields_for(
9438 existing,
9439 incoming.metadata.contains_key(ORIGIN_KEY),
9440 incoming.written.status().is_some(),
9441 incoming
9442 .priority
9443 .is_some_and(|priority| priority != Priority::None),
9444 )
9445 .await?;
9446 let status_target = incoming
9447 .written
9448 .work_status()
9449 .map(|(kind, status)| self.resolved_target(kind, status.category))
9450 .transpose()?;
9451 let column = match (incoming.written.work_status(), status_target.as_ref()) {
9452 (Some((kind, status)), Some(target)) => {
9453 self.column_for(&board.fields, kind, status.category, target)?
9454 }
9455 _ => None,
9456 };
9457 // Resolved before anything is created, for the reason the column above is: a
9458 // priority this board has no option for is refused while nothing has been written.
9459 let priority_write = match incoming.priority {
9460 Some(priority) => self.priority_write(&board.fields, existing, priority)?,
9461 None => None,
9462 };
9463 // And for the reason the priority is: a value no projected field can hold, or a board
9464 // that cannot hold the field, is refused while nothing has been written.
9465 let (projections, projected) = self.projection_writes(
9466 &board.fields,
9467 existing.map(|item| &item.projected),
9468 incoming.metadata,
9469 None,
9470 )?;
9471 let content_kind = existing.map_or(ContentKind::Issue, |item| item.content_kind);
9472 if content_kind == ContentKind::DraftIssue {
9473 if let (Some(StatusTarget::Terminal(_, _)), Some(status)) =
9474 (status_target.as_ref(), incoming.written.status())
9475 {
9476 return Err(self.closes_a_draft(status.category));
9477 }
9478 if incoming.parent.is_some() {
9479 return Err(SourceError::Refused {
9480 message: "GitHub draft items cannot be a project's sub-issue".into(),
9481 });
9482 }
9483 }
9484 match existing {
9485 Some(item) if content_kind == ContentKind::Issue => {
9486 if item.labels != incoming.labels {
9487 return Err(SourceError::Refused {
9488 message: "GitHub issue labels differ from the labels being written".into(),
9489 });
9490 }
9491 }
9492 _ => {
9493 if !incoming.labels.is_empty() {
9494 return Err(SourceError::Refused {
9495 message: "GitHub items created by this destination carry no labels".into(),
9496 });
9497 }
9498 }
9499 }
9500
9501 // The repository the issue really lives in is what the slot below is written against,
9502 // so a single entry that is where the issue is created travels as no key at all, and
9503 // the read side derives it back from the issue.
9504 let own_repository = match &destination {
9505 WriteDestination::Existing(item) => item.own_repository.clone(),
9506 WriteDestination::New(target) => Some(
9507 Repository::try_from(target.origin())
9508 .map_err(|message| SourceError::Config { message })?,
9509 ),
9510 };
9511 let (native, fallback) = self
9512 .partition_edges(
9513 incoming.written.kind(),
9514 content_kind,
9515 existing.and_then(|item| item.blocked_by.as_deref()),
9516 depends_on,
9517 )
9518 .await?;
9519 let mut slot = slot_metadata(incoming, own_repository.as_ref(), &fallback);
9520 let content = match incoming.assets {
9521 Some(assets) => {
9522 let uploads = self.upload_assets(own_repository.as_ref(), assets).await?;
9523 let rewritten = onetaskgraph_plugin_api::serve_asset_references(
9524 incoming.content.unwrap_or_default(),
9525 &mut slot,
9526 &uploads,
9527 );
9528 incoming.content.map(|_| rewritten)
9529 }
9530 None => incoming.content.map(str::to_owned),
9531 };
9532 let body = compose_body(content.as_deref(), &slot)?;
9533 // Read before anything is created, for the reason the field below is: a value
9534 // this destination cannot store has to refuse, and refusing after `createIssue`
9535 // would leave an issue behind that nothing asked for. The engine writes a
9536 // qualified id here; a caller handing this key anything else is told so rather
9537 // than having it silently stored as no origin at all.
9538 // 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.
9539 let origin = match incoming.metadata.get(ORIGIN_KEY) {
9540 None => "",
9541 Some(Value::String(origin)) => origin.as_str(),
9542 Some(other) => {
9543 return Err(SourceError::Refused {
9544 message: format!(
9545 "{ORIGIN_KEY} holds a qualified id spelled as a string, and this item's \
9546 is {other}"
9547 ),
9548 });
9549 }
9550 };
9551 // Resolved before anything is created: a board that cannot carry the copy origin
9552 // has to refuse the write, and refusing it after `createIssue` would leave an
9553 // issue behind that nothing asked for.
9554 let origin_field = match Board::field(&board.fields, ORIGIN_FIELD)? {
9555 Some(field) => {
9556 if required_str(field, "__typename")? != "ProjectV2Field" {
9557 return Err(SourceError::Refused {
9558 message: format!(
9559 "GitHub board source-owned {ORIGIN_FIELD} field is not a text field"
9560 ),
9561 });
9562 }
9563 Some(required_str(field, "id")?.to_owned())
9564 }
9565 None if incoming.metadata.contains_key(ORIGIN_KEY) => {
9566 return Err(SourceError::Refused {
9567 message: format!(
9568 "GitHub board has no source-owned {ORIGIN_FIELD} text field, and the \
9569 item carries {ORIGIN_KEY}; add a text field named {ORIGIN_FIELD} to \
9570 the board"
9571 ),
9572 });
9573 }
9574 None => None,
9575 };
9576
9577 Ok(Prepared {
9578 origin: origin.to_owned(),
9579 destination,
9580 board,
9581 status_target,
9582 column,
9583 priority_write,
9584 projections,
9585 projected,
9586 content_kind,
9587 own_repository,
9588 native,
9589 slot,
9590 content,
9591 body,
9592 origin_field,
9593 })
9594 }
9595
9596 /// Everything one write does once the item it writes exists: its board fields, its
9597 /// relationships and — for an item that was already there — its content last, then what this
9598 /// process remembers of it.
9599 ///
9600 /// While this source is holding writes back — see [`TaskSource::begin_deferred_writes`] — an
9601 /// item this command created has its fields, its closing and its edges held for
9602 /// [`TaskSource::finish_writes`] rather than sent here.
9603 async fn complete_write(
9604 &self,
9605 incoming: &Incoming<'_>,
9606 prepared: Prepared,
9607 landed: Landed,
9608 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
9609 let Prepared {
9610 destination,
9611 board,
9612 status_target,
9613 column,
9614 priority_write,
9615 projections,
9616 projected,
9617 content_kind,
9618 own_repository,
9619 native,
9620 slot,
9621 content,
9622 body,
9623 origin,
9624 origin_field,
9625 } = prepared;
9626 let existing = destination.existing();
9627 let origin = origin.as_str();
9628 let Landed {
9629 content_id,
9630 item_id,
9631 url,
9632 number,
9633 } = landed;
9634
9635 // Only what differs from the item as this write read it is sent: a priority the item
9636 // already holds is not, so an unchanged re-copy writes no field and a person's edit of
9637 // it since that read is left alone.
9638 let priority_write = priority_write.filter(|_| {
9639 !existing.is_some_and(|item| {
9640 incoming.priority.map(HeldPriority::Read) == Some(item.priority.clone())
9641 })
9642 });
9643 let written_option = column.as_ref().map(|(_, _, name)| name.clone());
9644 let column = column
9645 .filter(|(_, _, name)| existing.is_none_or(|item| item.option.as_ref() != Some(name)))
9646 .map(|(field, option, _)| (field, option));
9647 // Creating an item here is several calls — `createIssue`, which files it on the
9648 // board, then its board fields, the parent and the dependencies — and GitHub can fail
9649 // at any of them. Everything this source can refuse *before* the first of those is
9650 // already checked above, so what is left is GitHub itself failing part way. When it
9651 // does over an item this call created, the issue is taken back: a write that
9652 // refused must not leave an item behind that nobody asked for, and one that does
9653 // makes the retry create a second.
9654 // Whether the board-field write carrying a moved origin was answered as landing whole.
9655 // When it was refused, GitHub does not say which of its fields ran before the one that
9656 // failed, so the origin may or may not have moved.
9657 let mut origin_landed = false;
9658 // An item this command created while writes are held back has every part that can land
9659 // later held for `finish_writes` — its own create filed it on the board and under its
9660 // project already — and so does a later write of it in the same command.
9661 let holding = (existing.is_none() && self.deferring()?) || self.holds_back(&content_id)?;
9662 let landed = if holding {
9663 self.hold_back(
9664 &board.id,
9665 &content_id,
9666 existing,
9667 origin_field.as_deref(),
9668 origin,
9669 column,
9670 status_target.as_ref(),
9671 priority_write.as_ref(),
9672 &projections,
9673 &native,
9674 )
9675 } else if let Some(item_id) = item_id.as_deref() {
9676 self.finish_write(
9677 board.id.as_str(),
9678 incoming,
9679 &content_id,
9680 item_id,
9681 content_kind,
9682 existing,
9683 origin_field.as_deref(),
9684 origin,
9685 column,
9686 status_target.as_ref(),
9687 priority_write.as_ref(),
9688 &projections,
9689 &native,
9690 &mut origin_landed,
9691 )
9692 .await
9693 } else {
9694 // An issue a create filed on the board itself has no board item this process
9695 // knows until `finish_writes` reads it, so nothing but a held-back write may land
9696 // on it before then.
9697 Err(SourceError::Malformed {
9698 message: format!(
9699 "issue {} was written before the board item GitHub filed it as was read",
9700 content_id.0
9701 ),
9702 })
9703 };
9704 // An existing item's title, body and state go last, in one `updateIssue`, once its board
9705 // fields and its relationships have landed: a refusal of any of those then leaves its
9706 // body — and the metadata slot inside it — exactly as it stood.
9707 let landed = match (landed, existing) {
9708 (Ok(()), Some(item)) => {
9709 self.update_existing(item, incoming, &body, status_target.as_ref())
9710 .await
9711 }
9712 (landed, _) => landed,
9713 };
9714 if let Err(error) = landed {
9715 match existing {
9716 // Best effort, and the write's own failure is what the caller is told: a
9717 // refusal naming the tidy-up would hide why the write failed at all.
9718 None => {
9719 let _ = self.delete_issue(&content_id).await;
9720 }
9721 // The origin field is the one piece of an existing item's metadata written
9722 // before its body, so a write refused after it puts it back as it was. When
9723 // that is refused too, the write's own failure is still what the caller is
9724 // told — with what it left behind added, because the item's metadata is then
9725 // not as it stood and a caller retrying has to know which key moved.
9726 Some(item) => {
9727 let before = item.origin.as_deref().unwrap_or("");
9728 if let Some(field) = origin_field.as_deref()
9729 && before != origin
9730 && let Err(restore) = self
9731 .set_item_field(
9732 board.id.as_str(),
9733 &item.item_id,
9734 field,
9735 json!({"text": before}),
9736 )
9737 .await
9738 {
9739 let left = if origin_landed {
9740 format!(
9741 "its {ORIGIN_KEY} was moved to {origin:?} before that and could \
9742 not be put back to {before:?} ({restore}), so item {} still \
9743 holds {origin:?} there",
9744 item.id.0
9745 )
9746 } else {
9747 format!(
9748 "the refused write carried its {ORIGIN_KEY} from {before:?} to \
9749 {origin:?}, GitHub does not say whether that part of it ran, \
9750 and putting it back to {before:?} was refused ({restore}), so \
9751 item {} holds {origin:?} or {before:?} there",
9752 item.id.0
9753 )
9754 };
9755 return Err(noting(
9756 error,
9757 &format!(
9758 "; {left}; next: set {ORIGIN_KEY} on it back to {before:?}, or \
9759 run the write again"
9760 ),
9761 ));
9762 }
9763 }
9764 }
9765 return Err(error);
9766 }
9767
9768 let written_status = match (incoming.written.work_status(), status_target.as_ref()) {
9769 (Some((kind, _)), Some(StatusTarget::Terminal(_, reason))) => {
9770 self.statuses
9771 .status(kind, written_option.as_deref(), true, Some(reason.reason()))
9772 }
9773 (Some((kind, _)), Some(StatusTarget::Column(_))) => {
9774 self.statuses
9775 .status(kind, written_option.as_deref(), false, None)
9776 }
9777 (Some((_, status)), _) => status.clone(),
9778 (None, _) => Status {
9779 category: StatusCategory::Unknown,
9780 name: "Open".to_owned(),
9781 },
9782 };
9783
9784 // So the rest of this command reads what it just did rather than what the board
9785 // said before it. See `remember_written` for which half takes it.
9786 let remembered = Resolved {
9787 // Empty for an issue whose board item is not read yet: `finish_writes` reads it
9788 // and `learn_item` puts it here.
9789 // 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.
9790 item_id: item_id.unwrap_or_default(),
9791 id: content_id.clone(),
9792 content_kind,
9793 kind: incoming.written.kind(),
9794 title: incoming.title.to_owned(),
9795 // The visible half of the body this write composed, split back off it the
9796 // way a read splits it — so what this record reports is what a read of the
9797 // same issue reports, rather than the person's text with the metadata slot
9798 // still on the end of it.
9799 body: metadata_body(body.clone())?.0,
9800 raw_body: body.clone(),
9801 // A document has no status of its own; what it reads back as is whatever
9802 // the issue's own state says, which is what a re-read reports.
9803 status: written_status,
9804 option: written_option.or_else(|| existing.and_then(|item| item.option.clone())),
9805 priority: match incoming.priority {
9806 Some(priority) => HeldPriority::Read(priority),
9807 None => existing.map_or(HeldPriority::Read(Priority::None), |item| {
9808 item.priority.clone()
9809 }),
9810 },
9811 // What `state_input` asked for: closed for a terminal target, open for any other
9812 // status, and the issue's own state left as it was by a document write.
9813 closed: content_kind == ContentKind::Issue
9814 && match status_target.as_ref() {
9815 Some(StatusTarget::Terminal(_, _)) => true,
9816 Some(_) => false,
9817 None => existing.is_some_and(|item| item.closed),
9818 },
9819 reason: match status_target.as_ref() {
9820 Some(StatusTarget::Terminal(_, reason)) if content_kind == ContentKind::Issue => {
9821 Some(reason.reason().to_owned())
9822 }
9823 Some(_) => None,
9824 None => existing.and_then(|item| item.reason.clone()),
9825 },
9826 delivers: incoming.delivers.to_vec(),
9827 delivered_by: incoming.delivered_by.to_vec(),
9828 labels: incoming.labels.to_vec(),
9829 parent: incoming.parent.cloned(),
9830 origin: (!origin.is_empty()).then(|| origin.to_owned()),
9831 projected,
9832 number,
9833 // In the update path this is the item's own url, read off `existing` where the
9834 // record above was bound, so one expression serves both halves.
9835 url,
9836 created_at: existing.and_then(|item| item.created_at),
9837 updated_at: existing.and_then(|item| item.updated_at),
9838 // What this write moved is GitHub's to say, so a record of it gives no token until
9839 // it is read again.
9840 item_updated_at: None,
9841 comment_count: None,
9842 relationship_events: None,
9843 served_at: None,
9844 facets: None,
9845 own_repository,
9846 repositories: incoming.repositories.to_vec(),
9847 classification: incoming.classification,
9848 slot,
9849 board_id: Some(board.id.as_str().to_owned()),
9850 fields: board
9851 .fields
9852 .get("nodes")
9853 .and_then(Value::as_array)
9854 .cloned()
9855 .unwrap_or_default(),
9856 board_fields: Some(board.fields.clone()),
9857 // What this write left the relationship holding is known by id alone, and a
9858 // later read of its edges needs each far end's kind, so it reads them again.
9859 blocked_by: None,
9860 };
9861 self.remember_written(remembered, existing.is_none())?;
9862 Ok(onetaskgraph_plugin_api::AssetsWritten {
9863 id: content_id,
9864 content,
9865 })
9866 }
9867
9868 /// Everything a write does after the item exists: its board fields, its parent, and
9869 /// its dependencies.
9870 ///
9871 /// Split out of `write_item` so there is one place a failure past the point of no
9872 /// return is caught, rather than a tidy-up repeated at each `?` above.
9873 // llmlint: ignore[suppressions_justified] This is the tail of `write_item` lifted out
9874 // so there is one place a failure past the point of no return is caught, and its
9875 // arguments are exactly the values that tail already had in scope. Bundling them into a
9876 // struct would describe no concept — it would be "the arguments of this function" — and
9877 // would put the whole of `write_item`'s locals behind one more indirection.
9878 #[allow(clippy::too_many_arguments)]
9879 async fn finish_write(
9880 &self,
9881 board_id: &str,
9882 incoming: &Incoming<'_>,
9883 content_id: &NativeId,
9884 item_id: &str,
9885 content_kind: ContentKind,
9886 existing: Option<&Resolved>,
9887 origin_field: Option<&str>,
9888 origin: &str,
9889 column: Option<(String, String)>,
9890 status_target: Option<&StatusTarget>,
9891 priority: Option<&PriorityWrite>,
9892 projections: &[ProjectionWrite],
9893 native: &[String],
9894 origin_landed: &mut bool,
9895 ) -> Result<(), SourceError> {
9896 let (fields, clears) = field_writes(
9897 existing,
9898 origin_field,
9899 origin,
9900 column,
9901 priority,
9902 projections,
9903 );
9904 self.set_item_fields(board_id, item_id, &fields, &clears)
9905 .await?;
9906 *origin_landed = true;
9907
9908 // An existing issue closes in the `updateIssue` its write ends with; one created just
9909 // now closes here, once its option is selected.
9910 if existing.is_none()
9911 && content_kind == ContentKind::Issue
9912 && matches!(status_target, Some(StatusTarget::Terminal(_, _)))
9913 {
9914 self.update_content(
9915 ContentKind::Issue,
9916 content_id,
9917 json!({"stateInput":state_input(status_target)}),
9918 )
9919 .await?;
9920 }
9921
9922 if content_kind == ContentKind::Issue {
9923 self.reparent(
9924 existing.and_then(|item| item.parent.clone()),
9925 content_id,
9926 incoming.parent,
9927 )
9928 .await?;
9929 // A document takes part in no dependency graph, so writing one neither reads
9930 // nor changes the issue's own `blockedBy` relationships. Reconciling them
9931 // against the empty list a document write carries would *delete* whatever
9932 // relationships a person had made on that issue, which is a write nobody
9933 // asked for.
9934 if incoming.written.kind() != BoardKind::Document {
9935 let issue = match existing {
9936 Some(item) => Issue::Existing(item.blocked_by.as_deref()),
9937 None => Issue::Created,
9938 };
9939 self.reconcile_blocked_by(content_id, native, issue).await?;
9940 }
9941 }
9942 Ok(())
9943 }
9944
9945 /// Whether this source is holding writes back — see [`TaskSource::begin_deferred_writes`].
9946 fn deferring(&self) -> Result<bool, SourceError> {
9947 Ok(self.deferred()?.is_some())
9948 }
9949
9950 /// Whether `content` is an item this command created whose parts are held back.
9951 fn holds_back(&self, content: &NativeId) -> Result<bool, SourceError> {
9952 Ok(self
9953 .deferred()?
9954 .as_ref()
9955 .is_some_and(|deferred| deferred.holds(content)))
9956 }
9957
9958 fn deferred(&self) -> Result<std::sync::MutexGuard<'_, Option<Deferred>>, SourceError> {
9959 self.deferred.lock().map_err(|_| SourceError::Unavailable {
9960 message: "this source's record of the writes it is holding back was left \
9961 inconsistent by an earlier failure; next: run the command again"
9962 .into(),
9963 })
9964 }
9965
9966 /// Creates one issue filed on the board and under its project by the `createIssue` itself —
9967 /// `projectV2Ids` naming the board and `parentIssueId` the project — which is what a copy
9968 /// creates with: one request where adding it to the board and filing it under its project
9969 /// were two more. GitHub adds the board item after answering, so its id is not known here;
9970 /// [`TaskSource::finish_writes`] reads it.
9971 async fn create_filed_issue(
9972 &self,
9973 board_id: &str,
9974 repository: &RepositoryTarget,
9975 incoming: &Incoming<'_>,
9976 body: &Option<String>,
9977 ) -> Result<Landed, SourceError> {
9978 let repository_id = self.repository_id(repository, incoming).await?;
9979 let data = self
9980 .graphql(
9981 graphql::CREATE_ISSUE,
9982 json!({"input": filed_input(&repository_id, board_id, incoming, body)}),
9983 )
9984 .await?;
9985 let created = data
9986 .pointer("/createIssue/issue")
9987 .filter(|value| !value.is_null())
9988 .ok_or_else(|| SourceError::Malformed {
9989 message: "GitHub issue creation returned no issue".into(),
9990 })?;
9991 self.created_landed(created).await
9992 }
9993
9994 /// Keep the address `createIssue` answered for the issue `content`, for
9995 /// [`TaskSource::written_location`].
9996 fn note_written(&self, content: &NativeId, url: Option<&str>) {
9997 if let Some(url) = url {
9998 self.written
9999 .lock()
10000 .unwrap_or_else(std::sync::PoisonError::into_inner)
10001 .insert(content.clone(), url.to_owned());
10002 }
10003 }
10004
10005 /// What one `createIssue` answered, as the item it created — or, when its number cannot be
10006 /// read, the refusal, after taking the issue back.
10007 async fn created_landed(&self, created: &Value) -> Result<Landed, SourceError> {
10008 let content_id = NativeId(required_nonblank_str(created, "id")?.to_owned());
10009 let url = optional_str(created, "url")?.map(str::to_owned);
10010 let number = match created_issue_number(created) {
10011 Ok(number) => number,
10012 Err(error) => {
10013 let _ = self.delete_issue(&content_id).await;
10014 return Err(error);
10015 }
10016 };
10017 self.note_written(&content_id, url.as_deref());
10018 Ok(Landed {
10019 content_id,
10020 item_id: None,
10021 url,
10022 number,
10023 })
10024 }
10025
10026 /// Hold back everything a write of the created item `content` would send after creating it,
10027 /// for [`TaskSource::finish_writes`]: the same field values, closing and edges
10028 /// `finish_write` sends, the parent excepted, which its create already named. A later write
10029 /// of the same item replaces what it changes.
10030 // 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.
10031 #[allow(clippy::too_many_arguments)]
10032 fn hold_back(
10033 &self,
10034 board_id: &BoardId,
10035 content: &NativeId,
10036 existing: Option<&Resolved>,
10037 origin_field: Option<&str>,
10038 origin: &str,
10039 column: Option<(String, String)>,
10040 status_target: Option<&StatusTarget>,
10041 priority: Option<&PriorityWrite>,
10042 projections: &[ProjectionWrite],
10043 native: &[String],
10044 ) -> Result<(), SourceError> {
10045 let status = column.map(|(field, option)| {
10046 let name = self
10047 .fields_cache()
10048 .ok()
10049 .and_then(|fields| {
10050 fields.as_ref().and_then(|board| {
10051 option_name(&board.fields, &field, &option).map(str::to_owned)
10052 })
10053 })
10054 .unwrap_or_default();
10055 (field, option, name)
10056 });
10057 let (fields, clears) =
10058 field_writes(existing, origin_field, origin, None, priority, projections);
10059 let close = match status_target {
10060 Some(StatusTarget::Terminal(_, reason)) => Some(*reason),
10061 _ => None,
10062 };
10063 let mut deferred = self.deferred()?;
10064 let deferred = deferred.get_or_insert_with(Deferred::default);
10065 if let Deferred::Nothing = deferred {
10066 *deferred = Deferred::Held {
10067 board: board_id.clone(),
10068 created: Vec::new(),
10069 };
10070 }
10071 let Deferred::Held { created, .. } = deferred else {
10072 unreachable!("held back on the line above");
10073 };
10074 let held = match created.iter().position(|held| held.content == *content) {
10075 Some(at) => &mut created[at],
10076 None => {
10077 created.push(HeldBack {
10078 content: content.clone(),
10079 fields: Vec::new(),
10080 status: None,
10081 clears: Vec::new(),
10082 close: None,
10083 edges: Vec::new(),
10084 });
10085 created.last_mut().expect("just held")
10086 }
10087 };
10088 for (field, value) in fields {
10089 held.fields.retain(|(held, _)| *held != field);
10090 held.clears.retain(|held| *held != field);
10091 held.fields.push((field, value));
10092 }
10093 for field in clears {
10094 held.fields.retain(|(held, _)| held != field);
10095 if !held.clears.iter().any(|held| held == field) {
10096 held.clears.push(field.to_owned());
10097 }
10098 }
10099 if status.is_some() {
10100 held.status = status;
10101 }
10102 if close.is_some() {
10103 held.close = close;
10104 }
10105 // Nothing of an item created in this command has been sent but its create, so the edges
10106 // it is to have are exactly the ones its latest write names.
10107 held.edges = native.iter().map(|far| NativeId(far.clone())).collect();
10108 Ok(())
10109 }
10110
10111 /// Create every task of `writes`, together, filed on the board and under each one's
10112 /// project: [`graphql::CREATE_ISSUES`], [`MUTATION_BATCH`] to a request.
10113 ///
10114 /// Each is prepared as [`Self::write_item`] prepares one — every refusal a write owes made
10115 /// before the first request — then created, then completed with its parts held back. A
10116 /// request refused part way leaves the issues it did create, which are taken back, with the
10117 /// rest of this call's, before the refusal is answered.
10118 async fn create_together(
10119 &self,
10120 writes: &[ItemWrite<Task>],
10121 ) -> Result<Vec<NativeId>, SourceError> {
10122 let incomings: Vec<Incoming<'_>> = writes
10123 .iter()
10124 .map(|write| self.task_incoming(write, None))
10125 .collect::<Result<_, _>>()?;
10126 let mut prepared = Vec::with_capacity(writes.len());
10127 for (incoming, write) in incomings.iter().zip(writes) {
10128 if write.target.is_some() {
10129 return Err(SourceError::Malformed {
10130 message: "a write naming its target was handed to a batch of creates".into(),
10131 });
10132 }
10133 prepared.push(
10134 self.prepare_write(incoming, None, &write.depends_on)
10135 .await?,
10136 );
10137 }
10138 let mut inputs = Vec::with_capacity(prepared.len());
10139 for (incoming, prepared) in incomings.iter().zip(&prepared) {
10140 let WriteDestination::New(target) = &prepared.destination else {
10141 return Err(SourceError::Malformed {
10142 message: "an update was decided for a write handed to a batch of creates"
10143 .into(),
10144 });
10145 };
10146 let repository_id = self.repository_id(target, incoming).await?;
10147 inputs.push(filed_input(
10148 &repository_id,
10149 prepared.board.id.as_str(),
10150 incoming,
10151 &prepared.body,
10152 ));
10153 }
10154 let mut landed: Vec<Landed> = Vec::with_capacity(inputs.len());
10155 let refused = 'create: {
10156 for chunk in inputs.chunks(MUTATION_BATCH) {
10157 let mut variables = serde_json::Map::new();
10158 for slot in 0..MUTATION_BATCH {
10159 variables.insert(
10160 format!("create{slot}"),
10161 chunk.get(slot).cloned().unwrap_or_else(|| chunk[0].clone()),
10162 );
10163 variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10164 }
10165 let data = match self
10166 .graphql(graphql::CREATE_ISSUES, Value::Object(variables))
10167 .await
10168 {
10169 Ok(data) => data,
10170 Err(error) => break 'create Some(error),
10171 };
10172 let mut refusal = data.get(ANSWER_ERRORS).map(|errors| {
10173 let said: Vec<&str> = errors
10174 .as_array()
10175 .into_iter()
10176 .flatten()
10177 .filter_map(|error| error.get("message").and_then(Value::as_str))
10178 .collect();
10179 SourceError::Refused {
10180 message: if said.is_empty() {
10181 "GitHub returned GraphQL errors".to_owned()
10182 } else {
10183 said.join("; ")
10184 },
10185 }
10186 });
10187 for slot in 0..chunk.len() {
10188 match data
10189 .pointer(&format!("/create{slot}/issue"))
10190 .filter(|value| !value.is_null())
10191 {
10192 Some(created) => match self.created_landed(created).await {
10193 Ok(created) => landed.push(created),
10194 Err(error) => {
10195 refusal.get_or_insert(error);
10196 }
10197 },
10198 // An alias GitHub refused answers nothing, and the refusal says why.
10199 None => {
10200 refusal.get_or_insert(SourceError::Malformed {
10201 message: "GitHub issue creation returned no issue".into(),
10202 });
10203 }
10204 }
10205 }
10206 if let Some(error) = refusal {
10207 break 'create Some(error);
10208 }
10209 }
10210 None
10211 };
10212 if let Some(error) = refused {
10213 // None of these ids reaches the caller, so an issue GitHub will not take back is
10214 // named here or by nobody.
10215 let ids: Vec<&NativeId> = landed.iter().map(|created| &created.content_id).collect();
10216 let left = self.delete_issues(&ids).await;
10217 let left: Vec<&str> = left.iter().map(|id| id.0.as_str()).collect();
10218 return Err(left_behind(error, &left));
10219 }
10220 let created: Vec<NativeId> = landed
10221 .iter()
10222 .map(|landed| landed.content_id.clone())
10223 .collect();
10224 let mut ids = Vec::with_capacity(landed.len());
10225 for ((incoming, prepared), landed) in incomings.iter().zip(prepared).zip(landed) {
10226 match self.complete_write(incoming, prepared, landed).await {
10227 Ok(written) => ids.push(written.id),
10228 // 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.
10229 Err(error) => {
10230 let ids: Vec<&NativeId> = created.iter().collect();
10231 let left = self.delete_issues(&ids).await;
10232 let left: Vec<&str> = left.iter().map(|id| id.0.as_str()).collect();
10233 return Err(left_behind(error, &left));
10234 }
10235 }
10236 }
10237 Ok(ids)
10238 }
10239
10240 /// Hold back the update of the existing issue `issue` — `members`, what its own
10241 /// [`graphql::UPDATE_ISSUE`] would send beside its id — while writes are being held back — so the updates a copy makes one after
10242 /// another go to GitHub as one request. Whatever this source held of the issue is dropped
10243 /// as the mutation itself would drop it. Answers whether it was held: never while a write
10244 /// the store's cache expects runs, which records each mutation's own answer, and never for
10245 /// an issue an update is already held for, which that one is sent ahead of.
10246 fn hold_update(&self, issue: &NativeId, members: &Value) -> Result<bool, SourceError> {
10247 if self.expected.journaling() || self.deferred()?.is_none() {
10248 return Ok(false);
10249 }
10250 let mut held = self
10251 .held_updates
10252 .lock()
10253 .map_err(|_| SourceError::Unavailable {
10254 message: "issue updates held back were left inconsistent; run the command again"
10255 .into(),
10256 })?;
10257 if held.iter().any(|earlier| earlier.issue == *issue) {
10258 return Ok(false);
10259 }
10260 let named = |value: &str| issue.0 == value;
10261 self.resolved_cache()?
10262 .retain(|id, item| !named(&id.0) && !named(&item.item_id));
10263 self.read_ahead()?.retain(|id, item| {
10264 !named(&id.0) && !item.as_ref().is_some_and(|item| named(&item.item_id))
10265 });
10266 self.carried()?
10267 .retain(|id, item| !named(&id.0) && !named(&item.item_id));
10268 held.push(HeldUpdate::new(issue, members));
10269 Ok(true)
10270 }
10271
10272 /// Send every update [`Self::hold_update`] held back: one alone as [`graphql::UPDATE_ISSUE`]
10273 /// sends it, several as aliased `updateIssue`s of [`graphql::UPDATE_ISSUES`],
10274 /// [`MUTATION_BATCH`] to a request. Each counts one write, as it would alone.
10275 async fn send_held_updates(&self) -> Result<(), SourceError> {
10276 let held = std::mem::take(&mut *self.held_updates.lock().map_err(|_| {
10277 SourceError::Unavailable {
10278 message: "issue updates held back were left inconsistent; run the command again"
10279 .into(),
10280 }
10281 })?);
10282 if let [one] = held.as_slice() {
10283 return self
10284 .send_update(
10285 graphql::UPDATE_ISSUE,
10286 "/updateIssue/issue",
10287 &one.issue,
10288 one.input.clone(),
10289 )
10290 .await;
10291 }
10292 for chunk in held.chunks(MUTATION_BATCH) {
10293 let mut variables = serde_json::Map::new();
10294 for slot in 0..MUTATION_BATCH {
10295 variables.insert(
10296 format!("update{slot}"),
10297 chunk.get(slot).unwrap_or(&chunk[0]).input.clone(),
10298 );
10299 variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10300 }
10301 let data = self
10302 .graphql(graphql::UPDATE_ISSUES, Value::Object(variables))
10303 .await?;
10304 let inputs: Vec<&Value> = chunk.iter().map(|held| &held.input).collect();
10305 check_aliased_answers(
10306 graphql::UPDATE_ISSUES,
10307 &data,
10308 "update",
10309 &inputs,
10310 &[("issue", "id")],
10311 )?;
10312 }
10313 Ok(())
10314 }
10315
10316 /// Send everything held back since [`TaskSource::begin_deferred_writes`]: one read of the
10317 /// created items' board items, then their field values and edges together in requests of
10318 /// [`WRITE_BATCH`], then their closes in requests of [`MUTATION_BATCH`]. Anything refused
10319 /// takes back every issue held, [`DELETE_BATCH`] to a request, and answers the refusal.
10320 async fn send_held_back(&self) -> Result<(), SourceError> {
10321 let Some(Deferred::Held { board, created }) = self.deferred()?.take() else {
10322 return Ok(());
10323 };
10324 if created.is_empty() {
10325 return Ok(());
10326 }
10327 let sent = self.send_deferred(board.as_str(), &created).await;
10328 if let Err(error) = sent {
10329 // Best effort, and the failure is what the caller is told, as a write of one item
10330 // that fails part way takes back what it created: the copy that held these back
10331 // journalled every one, so one this cannot take back its undo tries again, and says
10332 // so when it cannot either.
10333 let ids: Vec<&NativeId> = created.iter().map(|held| &held.content).collect();
10334 let _ = self.delete_issues(&ids).await;
10335 return Err(error);
10336 }
10337 Ok(())
10338 }
10339
10340 /// The requests [`Self::send_held_back`] makes.
10341 async fn send_deferred(&self, board: &str, created: &[HeldBack]) -> Result<(), SourceError> {
10342 let items = self.created_board_items(board, created).await?;
10343 let mut fields: Vec<Value> = Vec::new();
10344 let mut closes: Vec<Value> = Vec::new();
10345 let mut edges: Vec<Value> = Vec::new();
10346 for held in created {
10347 let (item, status) = &items[&held.content];
10348 let mut values = held.fields.clone();
10349 // A status the board already shows — set by a workflow a person turned on to file
10350 // new items at it — is not written again.
10351 if let Some((field, option, name)) = &held.status
10352 && status.as_deref() != Some(name.as_str())
10353 {
10354 values.insert(0, (field.clone(), json!({"singleSelectOptionId": option})));
10355 }
10356 for (field, value) in values {
10357 fields.push(json!({"projectId":board,"itemId":item,"fieldId":field,"value":value}));
10358 }
10359 if let Some(reason) = held.close {
10360 closes.push(json!({"id": held.content.0, "stateInput": closed_input(reason)}));
10361 }
10362 for far in &held.edges {
10363 edges.push(json!({"issueId": held.content.0, "blockingIssueId": far.0}));
10364 }
10365 if !held.clears.is_empty() {
10366 let clears: Vec<&str> = held.clears.iter().map(String::as_str).collect();
10367 self.set_item_fields(board, item, &[], &clears).await?;
10368 }
10369 }
10370 // Each alias answers with what it wrote, which is what its input named: the board item a
10371 // field value is set on, the issue closed, the issue blocked and the one blocking it.
10372 let field_answer: &[(&str, &str)] = &[("projectV2Item", "itemId")];
10373 let close_answer: &[(&str, &str)] = &[("issue", "id")];
10374 let block_answer: &[(&str, &str)] =
10375 &[("issue", "issueId"), ("blockingIssue", "blockingIssueId")];
10376 // Every field value, then every edge, as one ordered list sent `WRITE_BATCH` aliases to a
10377 // request: a request holding both kinds is the one document carrying both, so a plan's
10378 // values and edges cost one request where they fit in one.
10379 let writes: Vec<HeldWrite<'_>> = fields
10380 .iter()
10381 .map(HeldWrite::Field)
10382 .chain(edges.iter().map(HeldWrite::Edge))
10383 .collect();
10384 for chunk in writes.chunks(WRITE_BATCH) {
10385 let values: Vec<&Value> = chunk
10386 .iter()
10387 .filter_map(|write| match write {
10388 HeldWrite::Field(input) => Some(*input),
10389 HeldWrite::Edge(_) => None,
10390 })
10391 .collect();
10392 let blocks: Vec<&Value> = chunk
10393 .iter()
10394 .filter_map(|write| match write {
10395 HeldWrite::Edge(input) => Some(*input),
10396 HeldWrite::Field(_) => None,
10397 })
10398 .collect();
10399 let (document, sends) = match (values.is_empty(), blocks.is_empty()) {
10400 (false, false) => (graphql::WRITE_FIELDS_AND_EDGES, ["sendField", "sendBlock"]),
10401 (false, true) => (graphql::WRITE_FIELD_VALUES, ["send", "send"]),
10402 _ => (graphql::ADD_BLOCKED_BYS, ["send", "send"]),
10403 };
10404 let mut variables = serde_json::Map::new();
10405 let kinds = [
10406 ("field", &values, sends[0], field_answer),
10407 ("block", &blocks, sends[1], block_answer),
10408 ];
10409 for (prefix, inputs, send, _) in &kinds {
10410 if inputs.is_empty() {
10411 continue;
10412 }
10413 for slot in 0..WRITE_BATCH {
10414 variables.insert(
10415 format!("{prefix}{slot}"),
10416 (*inputs.get(slot).unwrap_or(&inputs[0])).clone(),
10417 );
10418 variables.insert(format!("{send}{slot}"), json!(slot < inputs.len()));
10419 }
10420 }
10421 let data = self.graphql(document, Value::Object(variables)).await?;
10422 for (prefix, inputs, _, answers) in kinds {
10423 check_aliased_answers(document, &data, prefix, inputs, answers)?;
10424 }
10425 }
10426 for chunk in closes.chunks(MUTATION_BATCH) {
10427 let mut variables = serde_json::Map::new();
10428 for slot in 0..MUTATION_BATCH {
10429 variables.insert(
10430 format!("close{slot}"),
10431 chunk.get(slot).cloned().unwrap_or_else(|| chunk[0].clone()),
10432 );
10433 variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10434 }
10435 let data = self
10436 .graphql(graphql::CLOSE_ISSUES, Value::Object(variables))
10437 .await?;
10438 let inputs: Vec<&Value> = chunk.iter().collect();
10439 check_aliased_answers(graphql::CLOSE_ISSUES, &data, "close", &inputs, close_answer)?;
10440 }
10441 for held in created {
10442 self.learn_item(&held.content, &items[&held.content].0)?;
10443 }
10444 Ok(())
10445 }
10446
10447 /// The board item each of `created` is on this board, with its `Status` as the board set it:
10448 /// [`graphql::CREATED_BOARD_ITEMS`], read again — after a wait of a second, then two, four,
10449 /// eight and sixteen — for only those GitHub has not filed yet.
10450 ///
10451 /// **Membership is never assumed.** `createIssue`'s `projectV2Ids` files an issue some time
10452 /// after GitHub answered the create — the feasibility spike saw 0.45 to 1.84 seconds, and no
10453 /// create answer carrying the item — so only a read says it is there. One still absent once
10454 /// those reads are spent is filed with [`graphql::ADD_TO_BOARD`], one write each; GitHub
10455 /// filing it meanwhile refuses that "Content already exists in this project", which says it
10456 /// is placed, and one more read finds the item rather than a second one being made.
10457 async fn created_board_items(
10458 &self,
10459 board: &str,
10460 created: &[HeldBack],
10461 ) -> Result<BTreeMap<NativeId, (String, Option<String>)>, SourceError> {
10462 let mut found: BTreeMap<NativeId, (String, Option<String>)> = BTreeMap::new();
10463 let unfiled = |found: &BTreeMap<NativeId, (String, Option<String>)>| -> Vec<NativeId> {
10464 created
10465 .iter()
10466 .map(|held| held.content.clone())
10467 .filter(|id| !found.contains_key(id))
10468 .collect()
10469 };
10470 let mut wait = Duration::from_secs(1);
10471 for attempt in 0..6 {
10472 let absent = unfiled(&found);
10473 if absent.is_empty() {
10474 return Ok(found);
10475 }
10476 if attempt > 0 {
10477 self.clock
10478 .sleep_because(wait, pacing::cause::BOARD_FILING)
10479 .await;
10480 wait = wait.saturating_mul(2);
10481 }
10482 self.read_created_items(board, &absent, &mut found).await?;
10483 }
10484 let mut placed_meanwhile = Vec::new();
10485 for id in unfiled(&found) {
10486 match self
10487 .graphql(
10488 graphql::ADD_TO_BOARD,
10489 json!({"input":{"projectId":board,"contentId":id.0}}),
10490 )
10491 .await
10492 {
10493 Ok(added) => {
10494 let item = added
10495 .pointer("/addProjectV2ItemById/item")
10496 .filter(|value| !value.is_null())
10497 .ok_or_else(|| SourceError::Malformed {
10498 message: "GitHub board addition returned no project item".into(),
10499 })?;
10500 found.insert(id, (required_nonblank_str(item, "id")?.to_owned(), None));
10501 }
10502 Err(error) if already_on_the_board(&error) => placed_meanwhile.push(id),
10503 Err(error) => return Err(error),
10504 }
10505 }
10506 if !placed_meanwhile.is_empty() {
10507 self.read_created_items(board, &placed_meanwhile, &mut found)
10508 .await?;
10509 }
10510 let absent = unfiled(&found);
10511 if absent.is_empty() {
10512 return Ok(found);
10513 }
10514 let absent: Vec<&str> = absent.iter().map(|id| id.0.as_str()).collect();
10515 Err(SourceError::Unavailable {
10516 message: format!(
10517 "GitHub created {} filed on board {board} and, after 31 seconds and adding {} \
10518 to it, still answers {} on no item of it: {}; next: run the copy again once \
10519 GitHub has caught up",
10520 if absent.len() == 1 {
10521 "an issue"
10522 } else {
10523 "issues"
10524 },
10525 if absent.len() == 1 { "it" } else { "them" },
10526 if absent.len() == 1 { "it" } else { "them" },
10527 absent.join(", ")
10528 ),
10529 })
10530 }
10531
10532 /// One [`graphql::CREATED_BOARD_ITEMS`] read of each of `ids`, [`COPY_DETAIL_BATCH`] to a
10533 /// request, adding to `found` the board item of every one GitHub has filed on `board`.
10534 async fn read_created_items(
10535 &self,
10536 board: &str,
10537 ids: &[NativeId],
10538 found: &mut BTreeMap<NativeId, (String, Option<String>)>,
10539 ) -> Result<(), SourceError> {
10540 for chunk in ids.chunks(COPY_DETAIL_BATCH) {
10541 let mut variables = serde_json::Map::new();
10542 for slot in 0..COPY_DETAIL_BATCH {
10543 variables.insert(
10544 format!("id{slot}"),
10545 json!(chunk.get(slot).unwrap_or(&chunk[0]).0),
10546 );
10547 variables.insert(format!("read{slot}"), json!(slot < chunk.len()));
10548 }
10549 variables.insert("boardItems".into(), json!(BOARD_ITEMS_PAGE_SIZE));
10550 variables.insert("statusName".into(), json!(STATUS_FIELD));
10551 let data = self
10552 .graphql(graphql::CREATED_BOARD_ITEMS, Value::Object(variables))
10553 .await?;
10554 for (slot, id) in chunk.iter().enumerate() {
10555 // An answer for another issue than the one asked about says nothing of
10556 // where that one is filed, and its board item is not this one's.
10557 let node = data.get(format!("i{slot}")).filter(|node| !node.is_null());
10558 let named = node.map(|node| node.get("id").and_then(Value::as_str));
10559 if let Some(named) = named
10560 && named != Some(id.0.as_str())
10561 {
10562 return Err(SourceError::Malformed {
10563 message: format!(
10564 "GitHub answered the board items of {} when asked for those of {}",
10565 named.unwrap_or("an issue it did not name"),
10566 id.0
10567 ),
10568 });
10569 }
10570 // An issue GitHub does not answer yet is not filed yet; one it answers is
10571 // read whole, its connection, each board item's board and its status, and
10572 // any of them unreadable is refused rather than read as not filed.
10573 let Some(node) = node else { continue };
10574 let malformed = |what: &str| SourceError::Malformed {
10575 message: format!("GitHub answered the board items of {} with {what}", id.0),
10576 };
10577 let nodes = node
10578 .pointer("/projectItems/nodes")
10579 .and_then(Value::as_array)
10580 .ok_or_else(|| malformed("no list of board items"))?;
10581 let mut item = None;
10582 for node in nodes {
10583 let on = node
10584 .pointer("/project/id")
10585 .and_then(Value::as_str)
10586 .ok_or_else(|| malformed("a board item naming no board"))?;
10587 if on == board {
10588 item = Some(node);
10589 }
10590 }
10591 if let Some(item) = item {
10592 let status = match item.get("status") {
10593 None | Some(Value::Null) => None,
10594 Some(status) => Some(
10595 status
10596 .get("name")
10597 .and_then(Value::as_str)
10598 .ok_or_else(|| malformed("a status that names no option"))?
10599 .to_owned(),
10600 ),
10601 };
10602 found.insert(
10603 id.clone(),
10604 (required_nonblank_str(item, "id")?.to_owned(), status),
10605 );
10606 }
10607 }
10608 }
10609 Ok(())
10610 }
10611
10612 /// The board item id of `content`, which this process created and remembered without one,
10613 /// in every half of its own view.
10614 fn learn_item(&self, content: &NativeId, item: &str) -> Result<(), SourceError> {
10615 if let Some(held) = self.resolved_cache()?.get_mut(content) {
10616 held.item_id = item.to_owned();
10617 }
10618 if let Some(held) = self.created()?.iter_mut().find(|held| held.id == *content) {
10619 held.item_id = item.to_owned();
10620 }
10621 Ok(())
10622 }
10623
10624 /// Ask together which items each of `origins` names, and what creating an item reads; see
10625 /// [`TaskSource::read_ahead_origins_for_copy`]. An origin whose answer runs past its first
10626 /// page is left to its own lookup.
10627 async fn origins_ahead(&self, origins: &[String]) -> Result<(), SourceError> {
10628 if self.repository.is_none() {
10629 return Ok(());
10630 }
10631 let unasked: Vec<&String> = {
10632 let held = self.narrowed_cache()?;
10633 let mut unasked: Vec<&String> = Vec::new();
10634 for origin in origins {
10635 if !held.contains_key(&Narrowing::Origin(origin.clone()).key())
10636 && !unasked.contains(&origin)
10637 {
10638 unasked.push(origin);
10639 }
10640 }
10641 unasked
10642 };
10643 if unasked.is_empty() {
10644 return Ok(());
10645 }
10646 // One batch is held back for the read ahead that follows, which may carry a project's
10647 // members beside it in the same request.
10648 if unasked.len() <= ORIGIN_BATCH {
10649 let mut pending =
10650 self.pending_origins
10651 .lock()
10652 .map_err(|_| SourceError::Unavailable {
10653 message:
10654 "origins held for a read ahead were left inconsistent; run the command \
10655 again"
10656 .into(),
10657 })?;
10658 for origin in unasked {
10659 if !pending.contains(origin) {
10660 pending.push(origin.clone());
10661 }
10662 }
10663 return Ok(());
10664 }
10665 let unasked: Vec<String> = unasked.into_iter().cloned().collect();
10666 self.send_origin_lookups(&unasked, None).await
10667 }
10668
10669 /// Hold, for [`TaskSource::member_classifications`] of `project`, each member's
10670 /// classification and repositories as the members page `data` carries them — when that page
10671 /// is every member, each one names `project` as its parent, and every record it holds is
10672 /// readable. Any other page holds nothing, so the members are read as they always were.
10673 fn hold_member_classes(&self, project: &NativeId, data: &Value) -> Result<(), SourceError> {
10674 let Some(page) = data
10675 .pointer("/members/subIssues")
10676 .filter(|page| !page.is_null())
10677 else {
10678 return Ok(());
10679 };
10680 if page
10681 .pointer("/pageInfo/hasNextPage")
10682 .and_then(Value::as_bool)
10683 != Some(false)
10684 {
10685 return Ok(());
10686 }
10687 let Some(nodes) = page.get("nodes").and_then(Value::as_array) else {
10688 return Ok(());
10689 };
10690 let mut members = Vec::with_capacity(nodes.len());
10691 for node in nodes {
10692 if node.pointer("/parent/id").and_then(Value::as_str) != Some(project.0.as_str()) {
10693 return Ok(());
10694 }
10695 let Some(body) = node.get("body").and_then(Value::as_str) else {
10696 return Ok(());
10697 };
10698 // Every node here is filed under `project`, which `BoardKind::of` reads as one of its
10699 // tasks whatever its marker says, so each is a member the full read would classify.
10700 let Ok((_, slot)) = metadata_body(Some(body.to_owned())) else {
10701 return Ok(());
10702 };
10703 let Ok(classification) = Classification::from_metadata(&slot) else {
10704 return Ok(());
10705 };
10706 let repositories = if slot.contains_key(Repository::METADATA_KEY) {
10707 let Ok(repositories) = Repository::from_metadata(&slot) else {
10708 return Ok(());
10709 };
10710 repositories
10711 } else {
10712 let Some(origin) = node
10713 .pointer("/repository/nameWithOwner")
10714 .and_then(Value::as_str)
10715 else {
10716 return Ok(());
10717 };
10718 let Ok(own) = Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST))
10719 else {
10720 return Ok(());
10721 };
10722 vec![own]
10723 };
10724 members.push((classification, repositories));
10725 }
10726 self.member_classes
10727 .lock()
10728 .map_err(|_| SourceError::Unavailable {
10729 message: "a project's members held for its classification were left \
10730 inconsistent; run the command again"
10731 .into(),
10732 })?
10733 .insert(project.clone(), members);
10734 Ok(())
10735 }
10736
10737 /// Send every origin held back for the read ahead, beside the members of `members` when it
10738 /// names a project — or nothing, when none is held.
10739 async fn send_pending_origins(&self, members: Option<&NativeId>) -> Result<(), SourceError> {
10740 let pending = std::mem::take(&mut *self.pending_origins.lock().map_err(|_| {
10741 SourceError::Unavailable {
10742 message: "origins held for a read ahead were left inconsistent; run the command \
10743 again"
10744 .into(),
10745 }
10746 })?);
10747 if pending.is_empty() {
10748 return Ok(());
10749 }
10750 self.send_origin_lookups(&pending, members).await
10751 }
10752
10753 /// [`graphql::ORIGIN_LOOKUPS`] of `unasked`, [`ORIGIN_BATCH`] to a request — the first one
10754 /// [`graphql::ORIGIN_LOOKUPS_WITH_MEMBERS`], carrying the members of `members`, when it names
10755 /// a project — holding each origin's answer and, from a members page that is the whole of
10756 /// them, each member's classification.
10757 async fn send_origin_lookups(
10758 &self,
10759 unasked: &[String],
10760 members: Option<&NativeId>,
10761 ) -> Result<(), SourceError> {
10762 let Some(repository) = self.repository.clone() else {
10763 return Ok(());
10764 };
10765 let mut members = members;
10766 for chunk in unasked.chunks(ORIGIN_BATCH) {
10767 let mut variables = json!({"owner":self.owner,"number":self.project_number,
10768 "type":"ISSUE","lookupFirst":ORIGIN_LOOKUPS_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
10769 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true,
10770 "repositoryOwner":repository.owner,"repositoryName":repository.name});
10771 for slot in 0..ORIGIN_BATCH {
10772 let origin = chunk.get(slot).unwrap_or(&chunk[0]);
10773 variables[format!("filter{slot}")] =
10774 json!(format!("{ORIGIN_FIELD}:{}", quoted(origin)));
10775 variables[format!("search{slot}")] = json!(
10776 self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))))
10777 );
10778 variables[format!("ask{slot}")] = json!(slot < chunk.len());
10779 }
10780 let project = members.take();
10781 let document = match project {
10782 Some(project) => {
10783 variables["project"] = json!(project.0);
10784 variables["memberFirst"] = json!(MEMBER_PAGE_SIZE);
10785 graphql::ORIGIN_LOOKUPS_WITH_MEMBERS
10786 }
10787 None => graphql::ORIGIN_LOOKUPS,
10788 };
10789 let data = self.graphql(document, variables).await?;
10790 self.fields_read(&data)?;
10791 if let Some(project) = project {
10792 self.hold_member_classes(project, &data)?;
10793 }
10794 if let Some(node) = data.get("repository").filter(|value| !value.is_null()) {
10795 let id = required_nonblank_str(node, "id")?.to_owned();
10796 self.repository_cache()?.insert(repository.clone(), id);
10797 if let Some(numeric) = node
10798 .get("databaseId")
10799 .and_then(Value::as_u64)
10800 .and_then(std::num::NonZeroU64::new)
10801 {
10802 self.numeric_repositories
10803 .lock()
10804 .await
10805 .insert(repository.clone(), numeric);
10806 }
10807 }
10808 for (slot, origin) in chunk.iter().enumerate() {
10809 let items = data
10810 .pointer(&format!("/o{slot}/projectV2/items"))
10811 .filter(|value| !value.is_null())
10812 .ok_or_else(|| SourceError::Refused {
10813 message: format!(
10814 "GitHub project {}/{} was not found or is not visible to the token",
10815 self.owner, self.project_number
10816 ),
10817 })?;
10818 let searched =
10819 data.get(format!("s{slot}"))
10820 .ok_or_else(|| SourceError::Malformed {
10821 message: "GitHub search response has no search connection".into(),
10822 })?;
10823 if resumed(items, None)?.has_more() || resumed(searched, None)?.has_more() {
10824 continue;
10825 }
10826 let mut found: Vec<Resolved> = Vec::new();
10827 for item in optional_nodes(Some(items), "project items")?
10828 .into_iter()
10829 .flatten()
10830 {
10831 if let Some(resolved) = self.resolve(item)?
10832 && resolved.content_kind == ContentKind::Issue
10833 && !found.iter().any(|held| held.id == resolved.id)
10834 {
10835 found.push(resolved);
10836 }
10837 }
10838 for node in optional_nodes(Some(searched), "search")?
10839 .into_iter()
10840 .flatten()
10841 {
10842 if let Some(resolved) = self.resolve_issue(node).await?
10843 && !found.iter().any(|held| held.id == resolved.id)
10844 {
10845 found.push(resolved);
10846 }
10847 }
10848 self.narrowed_cache()?
10849 .insert(Narrowing::Origin((*origin).clone()).key(), found);
10850 }
10851 }
10852 Ok(())
10853 }
10854
10855 /// Delete every issue of `ids`, [`DELETE_BATCH`] to a request with
10856 /// [`graphql::DELETE_ISSUES`], and answer those still there: each a request's answer does not
10857 /// confirm, every one of a request refused whole included.
10858 async fn delete_issues(&self, ids: &[&NativeId]) -> Vec<NativeId> {
10859 let mut left = Vec::new();
10860 for chunk in ids.chunks(DELETE_BATCH) {
10861 let mut variables = serde_json::Map::new();
10862 for slot in 0..DELETE_BATCH {
10863 let id = chunk.get(slot).unwrap_or(&chunk[0]);
10864 variables.insert(format!("delete{slot}"), json!({"issueId": id.0}));
10865 variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10866 }
10867 let data = self
10868 .graphql(graphql::DELETE_ISSUES, Value::Object(variables))
10869 .await
10870 .unwrap_or(Value::Null);
10871 for (slot, id) in chunk.iter().enumerate() {
10872 // Confirmed only by the repository the deleted issue was in, named by a
10873 // non-blank id: anything else says nothing of whether the issue is gone.
10874 let confirmed = data
10875 .pointer(&format!("/delete{slot}/repository/id"))
10876 .and_then(Value::as_str)
10877 .is_some_and(|id| !id.trim().is_empty());
10878 if !confirmed {
10879 left.push((*id).clone());
10880 }
10881 }
10882 }
10883 left
10884 }
10885
10886 /// Delete one issue, which takes its board item with it.
10887 async fn delete_issue(&self, id: &NativeId) -> Result<(), SourceError> {
10888 let data = self
10889 .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
10890 .await?;
10891 data.pointer("/deleteIssue/repository")
10892 .filter(|value| !value.is_null())
10893 .ok_or_else(|| SourceError::Malformed {
10894 message: "GitHub issue deletion returned no repository".into(),
10895 })?;
10896 self.forget(id)?;
10897 Ok(())
10898 }
10899
10900 /// Remove one item this copy created, so a copy that could not finish leaves the board
10901 /// as it found it.
10902 ///
10903 /// Deleting the issue takes its board item with it, so there is no second mutation to
10904 /// keep in step. An id the board does not hold is not an error: the item is already
10905 /// gone, which is the state this asks for. Which that is, is decided by reading the item
10906 /// by its own id — a listing of the board can still be missing an item it holds, and
10907 /// reading that as *already gone* would leave behind the very item this was asked to
10908 /// take back.
10909 async fn delete_item(&self, id: &NativeId) -> Result<(), SourceError> {
10910 let Some(item) = self.bound_item(id).await? else {
10911 return Ok(());
10912 };
10913 if item.content_kind == ContentKind::DraftIssue {
10914 return Err(SourceError::Refused {
10915 message: format!(
10916 "GitHub item {} is a draft, and this source removes an item by deleting \
10917 its issue; next: remove it from the board by hand",
10918 id.0
10919 ),
10920 });
10921 }
10922 let data = self
10923 .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
10924 .await?;
10925 data.pointer("/deleteIssue/repository")
10926 .filter(|value| !value.is_null())
10927 .ok_or_else(|| SourceError::Malformed {
10928 message: "GitHub issue deletion returned no repository".into(),
10929 })?;
10930 self.forget(id)?;
10931 Ok(())
10932 }
10933
10934 /// The issue a comment call on `task` is about, or `None` when this board holds no such
10935 /// task.
10936 ///
10937 /// Resolved exactly as [`TaskSource::get_task`] resolves it, so the comment verbs and a
10938 /// read of the task cannot disagree about which ids name one: a project or a document of
10939 /// this board is not a task here either.
10940 ///
10941 /// A **draft** is a task with nowhere to keep a comment, because GitHub keeps comments on
10942 /// issues and a draft is not one. It is refused rather than answered with an empty page,
10943 /// which would read as a task nobody has commented on yet.
10944 async fn commented_issue(&self, task: &NativeId) -> Result<Option<NativeId>, SourceError> {
10945 let cached = self.resolved_cache()?.get(task).cloned();
10946 let Some(item) = (match cached {
10947 Some(item) => Some(item),
10948 None => self.item_by_id(task).await?,
10949 })
10950 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
10951 return Ok(None);
10952 };
10953 if item.content_kind == ContentKind::DraftIssue {
10954 return Err(self.draft_has_no_comments(task));
10955 }
10956 Ok(Some(item.id))
10957 }
10958
10959 /// The refusal a comment call on a board draft is answered with: GitHub keeps comments on
10960 /// issues, and a draft is not one.
10961 fn draft_has_no_comments(&self, task: &NativeId) -> SourceError {
10962 SourceError::Refused {
10963 message: format!(
10964 "task {} of source {} is a draft item on the board, and GitHub keeps \
10965 comments on issues alone, so a draft has none to read or write; next: \
10966 convert the draft to an issue on the board, then comment on the issue it \
10967 becomes",
10968 task.0, self.name
10969 ),
10970 }
10971 }
10972
10973 /// One task and a page of its comments, read with [`graphql::ISSUE_DETAIL`] in one
10974 /// request — or `None` when this board holds no task by that id.
10975 ///
10976 /// What `task show` and a comment listing read. A draft is a task with no comments, so it
10977 /// is answered with the draft and the refusal, at the price of the draft's own read.
10978 async fn issue_detail(
10979 &self,
10980 id: &NativeId,
10981 page: &PageRequest,
10982 ) -> Result<Option<TaskDetailRead>, SourceError> {
10983 let after = page.cursor.as_ref().map(|cursor| cursor.0.as_str());
10984 let asked = self
10985 .graphql(
10986 graphql::ISSUE_DETAIL,
10987 json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),"after":after,
10988 "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
10989 "duplicates":true}),
10990 )
10991 .await;
10992 let data = match asked {
10993 Ok(data) => data,
10994 Err(error) if unresolvable_node(&error) => return Ok(None),
10995 Err(error) => return Err(error),
10996 };
10997 // `node` is null for an id that names nothing, and absent only from an answer this
10998 // source cannot read — never the same thing.
10999 let node = data.get("node").ok_or_else(|| SourceError::Malformed {
11000 message: format!("GitHub answered the read of {} with no node", id.0),
11001 })?;
11002 self.detail_of(id, node, true, after).await
11003 }
11004
11005 /// Several tasks, each with the first page of its comments when `comments` is set, read
11006 /// [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] — one answer per id, in
11007 /// order.
11008 ///
11009 /// A batch GitHub refuses because one of its ids resolves to no node at all is read again
11010 /// one item at a time, so that id is answered as missing and the others as themselves; any
11011 /// other refusal is every id of that batch's answer.
11012 async fn issue_details(
11013 &self,
11014 ids: &[NativeId],
11015 comments: Option<&PageRequest>,
11016 ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
11017 let mut read = Vec::with_capacity(ids.len());
11018 for batch in ids.chunks(DETAIL_BATCH) {
11019 match self
11020 .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, comments))
11021 .await
11022 {
11023 Ok(data) => {
11024 for (slot, id) in batch.iter().enumerate() {
11025 // Every alias asked for is answered, null for an id naming nothing;
11026 // one missing is an answer this source cannot read.
11027 let read_one = match data.get(format!("i{slot}")) {
11028 Some(node) => self.detail_of(id, node, comments.is_some(), None).await,
11029 None => Err(SourceError::Malformed {
11030 message: format!(
11031 "GitHub answered a batch read with no item for {}",
11032 id.0
11033 ),
11034 }),
11035 };
11036 read.push(read_one);
11037 }
11038 }
11039 Err(error) if unresolvable_node(&error) => {
11040 for id in batch {
11041 read.push(match comments {
11042 Some(page) => self.issue_detail(id, page).await,
11043 None => self.task_read(id).await,
11044 });
11045 }
11046 }
11047 Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
11048 }
11049 }
11050 read
11051 }
11052
11053 /// One task and nothing of its comments, as [`TaskSource::get_task`] reads it.
11054 async fn task_read(&self, id: &NativeId) -> Result<Option<TaskDetailRead>, SourceError> {
11055 Ok(self.get_task(id).await?.map(|task| TaskDetailRead {
11056 task,
11057 comments: None,
11058 }))
11059 }
11060
11061 /// What one node a detail read reached says: the task this board holds by `id`, with the
11062 /// page of comments the node carries when `commented` — or `None` for a node that is no
11063 /// task of this board.
11064 ///
11065 /// Resolved as [`Self::item_by_id`] resolves an item: a draft is read again as a draft,
11066 /// and an item this process created answers from this process's own record, which a node
11067 /// read taken moments after the write can still be behind.
11068 async fn detail_of(
11069 &self,
11070 id: &NativeId,
11071 node: &Value,
11072 commented: bool,
11073 after: Option<&str>,
11074 ) -> Result<Option<TaskDetailRead>, SourceError> {
11075 if node.is_null() {
11076 return Ok(None);
11077 }
11078 let draft = optional_str(node, "__typename")? == Some("DraftIssue");
11079 // An issue answered under one id is that id's, or the answer is not one this source
11080 // can report: reporting another issue's task and comments under the qualified id asked
11081 // for would be the one wrong answer here. A draft's own read checks the same.
11082 if !draft
11083 && optional_str(node, "__typename")? == Some("Issue")
11084 && required_str(node, "id")? != id.0
11085 {
11086 return Err(SourceError::Malformed {
11087 message: format!(
11088 "GitHub answered the read of {} with issue {}",
11089 id.0,
11090 required_str(node, "id")?
11091 ),
11092 });
11093 }
11094 let item = if draft {
11095 self.draft_by_id(id).await?
11096 } else {
11097 self.resolve_issue(node).await?
11098 };
11099 // A read that carried the whole of what the issue is blocked by — a copy's — is kept
11100 // for this command, so the dependency read the copy makes next is answered from it.
11101 if let Some(read) = item.as_ref().filter(|item| item.blocked_by.is_some()) {
11102 self.resolved_cache()?.insert(read.id.clone(), read.clone());
11103 }
11104 let Some(item) = item.filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
11105 return Ok(None);
11106 };
11107 let own = self.created()?.iter().find(|own| own.id == *id).cloned();
11108 let task = own.unwrap_or(item).task()?;
11109 let comments = match (commented, draft) {
11110 (false, _) => None,
11111 (true, true) => Some(Err(self.draft_has_no_comments(id))),
11112 (true, false) => Some(comment_page(node, &id.0, after).map(Some)),
11113 };
11114 if commented && !draft && after.is_none() {
11115 self.expected.learn_comments(id, node);
11116 }
11117 Ok(Some(TaskDetailRead { task, comments }))
11118 }
11119
11120 /// Whether the comment `comment` is one of `issue`'s own.
11121 ///
11122 /// Read before an edit or a removal is sent, because GitHub's comment mutations take the
11123 /// comment's id and nothing else: a comment id given against the wrong task would
11124 /// otherwise change a comment on some other issue entirely. An id that names nothing, or
11125 /// names something that is not an issue comment, is a comment this task does not have —
11126 /// which is what GitHub refusing to resolve it means too.
11127 async fn comment_is_on(
11128 &self,
11129 issue: &NativeId,
11130 comment: &NativeId,
11131 ) -> Result<bool, SourceError> {
11132 Ok(self.comment_on(issue, comment).await?.is_some())
11133 }
11134
11135 /// The body the comment `comment` holds, exactly as GitHub holds it, when it is one of
11136 /// `issue`'s own — the read [`Self::comment_is_on`] makes, answering with what it selected.
11137 async fn comment_on(
11138 &self,
11139 issue: &NativeId,
11140 comment: &NativeId,
11141 ) -> Result<Option<String>, SourceError> {
11142 // A write the store's cache expects knows which comments its issue held, and their
11143 // bodies, at the version it expects: what this read would answer.
11144 if let Some(primed) = self.expected.primed_comment(issue, comment) {
11145 return Ok(primed);
11146 }
11147 let asked = self
11148 .graphql(graphql::COMMENT_ISSUE, json!({"id":comment.0}))
11149 .await;
11150 let data = match asked {
11151 Ok(data) => data,
11152 Err(error) if unresolvable_node(&error) => return Ok(None),
11153 Err(error) => return Err(error),
11154 };
11155 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
11156 return Ok(None);
11157 };
11158 if optional_str(node, "__typename")? != Some("IssueComment") {
11159 return Ok(None);
11160 }
11161 let on = node.get("issue").ok_or_else(|| SourceError::Malformed {
11162 message: format!("GitHub issue comment {} names no issue", comment.0),
11163 })?;
11164 if required_str(on, "id")? != issue.0 {
11165 return Ok(None);
11166 }
11167 Ok(Some(required_str(node, "body")?.to_owned()))
11168 }
11169
11170 /// Which far ends this item's own `blockedBy` relationship holds, and which it cannot.
11171 async fn partition_edges(
11172 &self,
11173 near_kind: BoardKind,
11174 near_content: ContentKind,
11175 carried: Option<&[Value]>,
11176 depends_on: &[DependencyEdge],
11177 ) -> Result<(Vec<String>, Vec<DependencyEdge>), SourceError> {
11178 let mut native = Vec::new();
11179 let mut fallback = Vec::new();
11180 let far_ends: Vec<(&DependencyEdge, &str, bool, Option<&Value>)> = depends_on
11181 .iter()
11182 .map(|edge| {
11183 // An opaque far end names another tracker, so it is never this board's own.
11184 let same_source = edge.to.designator().is_none()
11185 && edge
11186 .to
11187 .source()
11188 .is_none_or(|source| source == self.name.as_str());
11189 // A qualified id's source segment runs to its *first* colon — `GlobalId` and
11190 // `DependencyEndpoint::source` both read it that way — and a native id may hold
11191 // colons of its own, so the far end is everything after that one separator.
11192 // Splitting at the last would truncate `work:urn:task:7` to `7`.
11193 let far_id = if edge.to.is_qualified() {
11194 edge.to
11195 .id()
11196 .split_once(':')
11197 .map_or(edge.to.id(), |(_, native)| native)
11198 } else {
11199 edge.to.id()
11200 };
11201 // One that already blocks the near issue was answered by that issue's own
11202 // read, which carried each of its blockers' kinds — an issue every one — so it
11203 // is not read again.
11204 let blocking = carried.and_then(|nodes| {
11205 nodes
11206 .iter()
11207 .find(|node| node.get("id").and_then(Value::as_str) == Some(far_id))
11208 });
11209 (edge, far_id, same_source, blocking)
11210 })
11211 .collect();
11212 // Every other same-source far end is read by its own id, exactly as the item it is a
11213 // far end of is: whether this board holds it is that read's answer, never a listing's.
11214 // They are read together, [`DETAIL_BATCH`] to a request, rather than one each.
11215 let mut unread: Vec<NativeId> = Vec::new();
11216 for (_, far_id, same_source, blocking) in &far_ends {
11217 let id = NativeId((*far_id).to_owned());
11218 if *same_source && blocking.is_none() && !unread.contains(&id) {
11219 unread.push(id);
11220 }
11221 }
11222 let read: BTreeMap<NativeId, Option<Resolved>> = unread
11223 .iter()
11224 .cloned()
11225 .zip(self.items_by_ids(&unread).await?)
11226 .collect();
11227 for (edge, far_id, same_source, blocking) in far_ends {
11228 let far = match (same_source, blocking) {
11229 (false, _) => None,
11230 (true, Some(node)) => Some(FarEnd {
11231 kind: if required_str(node, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
11232 BoardKind::Document
11233 } else {
11234 BoardKind::Work(related_kind(node)?)
11235 },
11236 content_kind: ContentKind::Issue,
11237 }),
11238 (true, None) => {
11239 let read = read
11240 .get(&NativeId(far_id.to_owned()))
11241 .cloned()
11242 .flatten()
11243 .ok_or_else(|| SourceError::Refused {
11244 message: format!("GitHub dependency item {far_id} was not found"),
11245 })?;
11246 Some(FarEnd {
11247 kind: read.kind,
11248 content_kind: read.content_kind,
11249 })
11250 }
11251 };
11252 let far = far.as_ref();
11253 // The caller says which kind the far end is, and this board holds the far end
11254 // itself, so a disagreement is settled here rather than stored: recorded, the
11255 // wrong kind would read back as a cross-level edge that never existed; written
11256 // natively, it would name a relationship of a different level than the caller
11257 // asked for.
11258 //
11259 // A far end this board holds as a *document* fails the same comparison and is
11260 // refused by the same sentence: `ItemKind` has no document variant because
11261 // nothing may point at one, so no caller can name it correctly and the refusal
11262 // is the only honest answer.
11263 if let Some(disagreeing) = far.filter(|far| far.kind != BoardKind::Work(edge.to.kind)) {
11264 return Err(SourceError::Refused {
11265 message: format!(
11266 "GitHub dependency item {far_id} is a {} of this board, and this item \
11267 names it as a {}; record the kind it is",
11268 disagreeing.kind.describes(),
11269 edge.to.kind.marker()
11270 ),
11271 });
11272 }
11273 // A draft has neither `blockedBy` nor `blocking`, so no edge of one is native
11274 // however the far end is spelled — and one classified native here would be
11275 // written nowhere at all, because a draft's native reconciliation never runs.
11276 let native_here = near_content == ContentKind::Issue
11277 && far.is_some_and(|far| {
11278 far.content_kind == ContentKind::Issue
11279 && BoardKind::Work(edge.to.kind) == near_kind
11280 });
11281 if native_here {
11282 native.push(far_id.to_owned());
11283 } else {
11284 fallback.push(edge.clone());
11285 }
11286 }
11287 Ok((native, fallback))
11288 }
11289
11290 async fn update_existing(
11291 &self,
11292 item: &Resolved,
11293 incoming: &Incoming<'_>,
11294 body: &Option<String>,
11295 status_target: Option<&StatusTarget>,
11296 ) -> Result<(), SourceError> {
11297 let title = incoming.written_title();
11298 // A terminal status closes the issue here, in the same mutation as its body: its board
11299 // option was selected before this, so a close never lands on an item whose board cannot
11300 // show it.
11301 let mut fields = match item.content_kind {
11302 ContentKind::DraftIssue => json!({"title":title,"body":body}),
11303 ContentKind::Issue => json!({"title":title,"body":body,
11304 "stateInput":state_input(status_target)}),
11305 };
11306 // Only the members whose value differs from the item as this write read it are sent,
11307 // and nothing at all when none does — whether or not the store's cache expects a
11308 // version of it. A member left out is one GitHub leaves as it is, so a person's edit
11309 // of it since that read is kept rather than overwritten with what it already held,
11310 // and a re-copy of an unchanged item costs no mutation.
11311 if let Some(members) = fields.as_object_mut() {
11312 if members.get("title").and_then(Value::as_str) == Some(item.title.as_str()) {
11313 members.remove("title");
11314 }
11315 let body_unchanged = match (members.get("body"), item.raw_body.as_deref()) {
11316 (Some(Value::String(sent)), Some(held)) => sent == held,
11317 (Some(Value::Null), held) => held.is_none_or(str::is_empty),
11318 _ => false,
11319 };
11320 if body_unchanged {
11321 members.remove("body");
11322 }
11323 // An issue already in the state the target asks for keeps it: an open one stays
11324 // open, and a closed one whose closure reads as the target's category keeps its
11325 // reason — a duplicate's says more than this write would.
11326 let unchanged_state = match status_target {
11327 Some(StatusTarget::Terminal(_, reason)) => {
11328 item.closed
11329 && closed_category(item.reason.as_deref())
11330 == closed_category(Some(reason.reason()))
11331 }
11332 Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => !item.closed,
11333 None => true,
11334 };
11335 if unchanged_state {
11336 members.remove("stateInput");
11337 }
11338 if members.is_empty() {
11339 return Ok(());
11340 }
11341 }
11342 self.update_content(item.content_kind, &item.id, fields)
11343 .await
11344 }
11345
11346 /// Update one board item's content with exactly `fields` beside its id, through the
11347 /// mutation its kind takes: `updateIssue` for an issue, `updateProjectV2DraftIssue` for
11348 /// a draft.
11349 ///
11350 /// Every input field either mutation leaves out is a field GitHub leaves as it is, which
11351 /// is what lets a narrow write carry the one thing it changes and nothing else.
11352 async fn update_content(
11353 &self,
11354 kind: ContentKind,
11355 id: &NativeId,
11356 fields: Value,
11357 ) -> Result<(), SourceError> {
11358 let (operation, id_key, pointer) = match kind {
11359 ContentKind::DraftIssue => (
11360 graphql::UPDATE_DRAFT,
11361 "draftIssueId",
11362 "/updateProjectV2DraftIssue/draftIssue",
11363 ),
11364 ContentKind::Issue => (graphql::UPDATE_ISSUE, "id", "/updateIssue/issue"),
11365 };
11366 if kind == ContentKind::Issue && self.hold_update(id, &fields)? {
11367 return Ok(());
11368 }
11369 let mut input = fields;
11370 input[id_key] = json!(id.0);
11371 self.send_update(operation, pointer, id, input).await
11372 }
11373
11374 /// Send one item's update, `input`, as `operation`, and hold GitHub's answer at `pointer`
11375 /// to naming that item, `id`.
11376 async fn send_update(
11377 &self,
11378 operation: &str,
11379 pointer: &str,
11380 id: &NativeId,
11381 input: Value,
11382 ) -> Result<(), SourceError> {
11383 let data = self.graphql(operation, json!({"input":input})).await?;
11384 let returned = data
11385 .pointer(pointer)
11386 .ok_or_else(|| SourceError::Malformed {
11387 message: "GitHub item update returned no item".into(),
11388 })?;
11389 if required_str(returned, "id")? != id.0 {
11390 return Err(SourceError::Malformed {
11391 message: "GitHub item update returned the wrong item".into(),
11392 });
11393 }
11394 Ok(())
11395 }
11396
11397 /// Creates one issue, files it on the board, and reports what a read of it would say:
11398 /// its content id, its board item id, and the web address GitHub gave it.
11399 ///
11400 /// Two calls rather than one: `createIssue` answers with an issue that is on no board,
11401 /// and `addProjectV2ItemById` is what puts it there. Filing it at creation through
11402 /// `CreateIssueInput.projectV2Ids` was tried and is not done: GitHub answered with no
11403 /// board item, and the `addProjectV2ItemById` that then had to follow was refused
11404 /// "Content already exists in this project". A terminal status is not written here:
11405 /// `finish_write` selects its option first and closes the issue after, so a close never
11406 /// lands on an item whose board cannot show it.
11407 ///
11408 /// The address and the number come back here because this is the only place either is
11409 /// known before GitHub's own board read catches up — an item this run created answers
11410 /// the reads that follow it out of the record below, and one remembered without them
11411 /// would report no location and no key for the rest of the run.
11412 async fn create_and_file_issue(
11413 &self,
11414 board_id: &str,
11415 repository: &RepositoryTarget,
11416 incoming: &Incoming<'_>,
11417 body: &Option<String>,
11418 ) -> Result<Landed, SourceError> {
11419 let repository_id = self.repository_id(repository, incoming).await?;
11420 let data = self
11421 .graphql(
11422 graphql::CREATE_ISSUE,
11423 json!({"input":{
11424 "repositoryId":repository_id,"title":incoming.written_title(),"body":body
11425 }}),
11426 )
11427 .await?;
11428 let created = data
11429 .pointer("/createIssue/issue")
11430 .filter(|value| !value.is_null())
11431 .ok_or_else(|| SourceError::Malformed {
11432 message: "GitHub issue creation returned no issue".into(),
11433 })?;
11434 let content_id = NativeId(required_str(created, "id")?.to_owned());
11435 // Optional although GitHub's schema makes it non-null: the issue exists by now, so
11436 // a response without it is not worth failing a landed write over — the item simply
11437 // reports no location until the board read catches up, which is what it did before.
11438 let url = optional_str(created, "url")?.map(str::to_owned);
11439 // The issue exists from here on, so an unreadable number and a refused board
11440 // filing below each try, best effort, to take it back: an issue in the repository
11441 // that is on no board is an item nobody asked for and nothing here would find again.
11442 //
11443 // Its number is optional on the same terms its address is — a landed write is not
11444 // worth failing over a member that came back missing, and such an item reports no
11445 // handle until a board read catches up. A number that is *present* and is not an
11446 // unsigned integer is still a response this source cannot read.
11447 let number = match created_issue_number(created) {
11448 Ok(number) => number,
11449 Err(error) => {
11450 let _ = self.delete_issue(&content_id).await;
11451 return Err(error);
11452 }
11453 };
11454 let added = match self
11455 .graphql(
11456 graphql::ADD_TO_BOARD,
11457 json!({"input":{"projectId":board_id,"contentId":content_id.0}}),
11458 )
11459 .await
11460 {
11461 Ok(added) => added,
11462 Err(error) => {
11463 let _ = self.delete_issue(&content_id).await;
11464 return Err(error);
11465 }
11466 };
11467 let item = added
11468 .pointer("/addProjectV2ItemById/item")
11469 .filter(|value| !value.is_null())
11470 .ok_or_else(|| SourceError::Malformed {
11471 message: "GitHub board addition returned no project item".into(),
11472 })?;
11473 self.note_written(&content_id, url.as_deref());
11474 Ok(Landed {
11475 content_id,
11476 item_id: Some(required_str(item, "id")?.to_owned()),
11477 url,
11478 number,
11479 })
11480 }
11481
11482 /// Move one issue under the project it now belongs to, or out of the one it left.
11483 async fn reparent(
11484 &self,
11485 held: Option<NativeId>,
11486 child: &NativeId,
11487 wanted: Option<&NativeId>,
11488 ) -> Result<(), SourceError> {
11489 if held.as_ref() == wanted {
11490 return Ok(());
11491 }
11492 if let Some(held) = &held {
11493 self.sub_issue(graphql::REMOVE_SUB_ISSUE, held, child, "removeSubIssue")
11494 .await?;
11495 }
11496 if let Some(wanted) = wanted {
11497 self.sub_issue(graphql::ADD_SUB_ISSUE, wanted, child, "addSubIssue")
11498 .await?;
11499 }
11500 Ok(())
11501 }
11502
11503 async fn sub_issue(
11504 &self,
11505 operation: &str,
11506 parent: &NativeId,
11507 child: &NativeId,
11508 root: &str,
11509 ) -> Result<(), SourceError> {
11510 let data = self
11511 .graphql(
11512 operation,
11513 json!({"input":{"issueId":parent.0,"subIssueId":child.0}}),
11514 )
11515 .await?;
11516 let issue =
11517 data.pointer(&format!("/{root}/issue"))
11518 .ok_or_else(|| SourceError::Malformed {
11519 message: "GitHub sub-issue update returned no issue".into(),
11520 })?;
11521 let sub =
11522 data.pointer(&format!("/{root}/subIssue"))
11523 .ok_or_else(|| SourceError::Malformed {
11524 message: "GitHub sub-issue update returned no sub-issue".into(),
11525 })?;
11526 if required_str(issue, "id")? != parent.0 || required_str(sub, "id")? != child.0 {
11527 return Err(SourceError::Malformed {
11528 message: "GitHub sub-issue update returned the wrong issues".into(),
11529 });
11530 }
11531 Ok(())
11532 }
11533
11534 /// Bring one issue's `blockedBy` to exactly `native`, sending only the difference, and say
11535 /// whether there was one.
11536 ///
11537 /// An issue [`Issue::Created`] by this very write is blocked by nothing yet, so its
11538 /// relationships are not read: there is nothing a read of them could find.
11539 async fn reconcile_blocked_by(
11540 &self,
11541 content_id: &NativeId,
11542 native: &[String],
11543 issue: Issue<'_>,
11544 ) -> Result<bool, SourceError> {
11545 let current = match issue {
11546 Issue::Created => Vec::new(),
11547 Issue::Existing(Some(held)) => held
11548 .iter()
11549 .map(|far| required_str(far, "id").map(str::to_owned))
11550 .collect::<Result<Vec<_>, _>>()?,
11551 Issue::Existing(None) => self.native_dependency_ids(content_id).await?,
11552 };
11553 let mut changed = false;
11554 for (operation, far_id) in current
11555 .iter()
11556 .filter(|id| !native.contains(id))
11557 .map(|id| (graphql::REMOVE_BLOCKED_BY, id))
11558 .chain(
11559 native
11560 .iter()
11561 .filter(|id| !current.contains(id))
11562 .map(|id| (graphql::ADD_BLOCKED_BY, id)),
11563 )
11564 {
11565 let data = self
11566 .graphql(
11567 operation,
11568 json!({"input":{"issueId":content_id.0,"blockingIssueId":far_id}}),
11569 )
11570 .await?;
11571 let root = if operation == graphql::ADD_BLOCKED_BY {
11572 "addBlockedBy"
11573 } else {
11574 "removeBlockedBy"
11575 };
11576 let issue =
11577 data.pointer(&format!("/{root}/issue"))
11578 .ok_or_else(|| SourceError::Malformed {
11579 message: "GitHub dependency update returned no issue".into(),
11580 })?;
11581 let blocker = data
11582 .pointer(&format!("/{root}/blockingIssue"))
11583 .ok_or_else(|| SourceError::Malformed {
11584 message: "GitHub dependency update returned no blocking issue".into(),
11585 })?;
11586 if required_str(issue, "id")? != content_id.0 || required_str(blocker, "id")? != far_id
11587 {
11588 return Err(SourceError::Malformed {
11589 message: "GitHub dependency update returned the wrong issues".into(),
11590 });
11591 }
11592 changed = true;
11593 }
11594 Ok(changed)
11595 }
11596}
11597
11598/// The board field values one write sends, in the order GitHub runs them, and the fields it
11599/// clears after them: the origin field when it moves, the `Status` option `column` names, the
11600/// priority, then each projected field.
11601fn field_writes<'a>(
11602 existing: Option<&Resolved>,
11603 origin_field: Option<&str>,
11604 origin: &str,
11605 column: Option<(String, String)>,
11606 priority: Option<&'a PriorityWrite>,
11607 projections: &'a [ProjectionWrite],
11608) -> (Vec<(String, Value)>, Vec<&'a str>) {
11609 let mut fields = Vec::new();
11610 if let Some(field_id) = origin_field
11611 && existing.map_or(!origin.is_empty(), |item| {
11612 item.origin.as_deref().unwrap_or("") != origin
11613 })
11614 {
11615 fields.push((field_id.to_owned(), json!({"text":origin})));
11616 }
11617 if let Some((field_id, option_id)) = column {
11618 fields.push((field_id, json!({"singleSelectOptionId":option_id})));
11619 }
11620 let mut clears = Vec::new();
11621 match priority {
11622 Some(PriorityWrite::Select { field, option }) => {
11623 fields.push((field.clone(), json!({"singleSelectOptionId":option})));
11624 }
11625 Some(PriorityWrite::Clear { field }) => clears.push(field.as_str()),
11626 None => {}
11627 }
11628 for projection in projections {
11629 projection.join(&mut fields, &mut clears);
11630 }
11631 (fields, clears)
11632}
11633
11634/// Everything one write decided before sending anything: see
11635/// [`GitHubProjectsSource::prepare_write`].
11636struct Prepared {
11637 /// What the write lands on.
11638 destination: WriteDestination,
11639 board: BoardFields,
11640 status_target: Option<StatusTarget>,
11641 /// The `Status` field, the option and the option's name the status lands on.
11642 // 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.
11643 column: Option<(String, String, String)>,
11644 priority_write: Option<PriorityWrite>,
11645 projections: Vec<ProjectionWrite>,
11646 projected: BTreeMap<String, String>,
11647 content_kind: ContentKind,
11648 own_repository: Option<Repository>,
11649 /// The far ends of the item's native blocked-by relationships.
11650 // 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.
11651 native: Vec<String>,
11652 slot: BTreeMap<String, Value>,
11653 content: Option<String>,
11654 body: Option<String>,
11655 /// The copy origin the item records, empty for none.
11656 // 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.
11657 origin: String,
11658 origin_field: Option<String>,
11659}
11660
11661/// One write a copy held back of the kinds sent together: a board field value, or a blocked-by
11662/// edge, each its input.
11663enum HeldWrite<'a> {
11664 Field(&'a Value),
11665 Edge(&'a Value),
11666}
11667
11668/// Whether `error` is GitHub refusing to file an issue on a board because it is filed there
11669/// already — what `addProjectV2ItemById` answers once `createIssue`'s `projectV2Ids` has placed
11670/// it, rather than answering with the item it holds.
11671fn already_on_the_board(error: &SourceError) -> bool {
11672 error
11673 .to_string()
11674 .contains("Content already exists in this project")
11675}
11676
11677/// Hold every alias `inputs` sent under `prefix` in `data` to answering with what its input
11678/// named — each `(root, named)` of `answers` — and refuse one that answered nothing or another
11679/// item.
11680fn check_aliased_answers(
11681 document: &str,
11682 data: &Value,
11683 prefix: &str,
11684 inputs: &[&Value],
11685 answers: &[(&str, &str)],
11686) -> Result<(), SourceError> {
11687 for (slot, input) in inputs.iter().enumerate() {
11688 for (root, named) in answers {
11689 let answered = data
11690 .pointer(&format!("/{prefix}{slot}/{root}/id"))
11691 .and_then(Value::as_str)
11692 .ok_or_else(|| SourceError::Malformed {
11693 message: format!(
11694 "GitHub answered {} with nothing at {prefix}{slot}",
11695 operation_description(document)
11696 ),
11697 })?;
11698 let asked = input.get(named).and_then(Value::as_str).unwrap_or_default();
11699 if answered != asked {
11700 return Err(SourceError::Malformed {
11701 message: format!(
11702 "GitHub answered {} at {prefix}{slot} with {answered}, which is not \
11703 {asked}, the item it was asked to write",
11704 operation_description(document)
11705 ),
11706 });
11707 }
11708 }
11709 }
11710 Ok(())
11711}
11712
11713/// `error`, which stopped a batch of creates, and — when GitHub refused to take any of them
11714/// back — each issue of `left` it created that is still in the repository, which its caller,
11715/// never handed their ids, could not name.
11716fn left_behind(error: SourceError, left: &[&str]) -> SourceError {
11717 if left.is_empty() {
11718 return error;
11719 }
11720 SourceError::Refused {
11721 message: format!(
11722 "{error}; GitHub then refused to take back {}, which this write created and which \
11723 are still in the repository; next: delete {} before writing again",
11724 left.join(", "),
11725 if left.len() == 1 { "it" } else { "them" }
11726 ),
11727 }
11728}
11729
11730/// What one write lands on: the item it updates, as it stood, or where it creates a new one.
11731enum WriteDestination {
11732 /// The item as it stood, for an update.
11733 Existing(Box<Resolved>),
11734 /// Where a new item is created.
11735 New(RepositoryTarget),
11736}
11737
11738impl WriteDestination {
11739 /// The item an update writes over, as it stood; `None` for a create.
11740 fn existing(&self) -> Option<&Resolved> {
11741 match self {
11742 Self::Existing(item) => Some(item),
11743 Self::New(_) => None,
11744 }
11745 }
11746}
11747
11748/// One existing issue's update held back by [`GitHubProjectsSource::hold_update`]: the issue,
11749/// and the `UpdateIssueInput` its own [`graphql::UPDATE_ISSUE`] would have sent — the members
11750/// it writes beside that issue's id, which only [`Self::new`] sets.
11751struct HeldUpdate {
11752 issue: NativeId,
11753 // llmlint: ignore[invalid_states_unrepresentable] The members are exactly what `update_content` would have sent at once, which every one of its callers builds as GraphQL variables; holding them back changes when they are sent, not what they may be, and only `new` builds one, fixing the issue's id. Typing `UpdateIssueInput` would retype every caller of `update_content`, a change of its own.
11754 input: Value,
11755}
11756
11757impl HeldUpdate {
11758 /// The update writing `members` — an `UpdateIssueInput` without its id — to `issue`.
11759 fn new(issue: &NativeId, members: &Value) -> Self {
11760 let mut input = members.clone();
11761 input["id"] = json!(issue.0);
11762 Self {
11763 issue: issue.clone(),
11764 input,
11765 }
11766 }
11767}
11768
11769/// What a copy has held back of the items it created; see
11770/// [`GitHubProjectsSource::hold_back`].
11771#[derive(Debug, Default)]
11772enum Deferred {
11773 /// Writes are being held back, and nothing has been held yet.
11774 #[default]
11775 Nothing,
11776 /// The items held back, every one of them on `board`.
11777 Held {
11778 /// The board every one of them is on.
11779 board: BoardId,
11780 /// Each created item, in the order it was created.
11781 created: Vec<HeldBack>,
11782 },
11783}
11784
11785impl Deferred {
11786 /// Whether `content` is one of the items held back.
11787 fn holds(&self, content: &NativeId) -> bool {
11788 match self {
11789 Self::Nothing => false,
11790 Self::Held { created, .. } => created.iter().any(|held| held.content == *content),
11791 }
11792 }
11793}
11794
11795/// One created item's parts that land once [`TaskSource::finish_writes`] is called.
11796#[derive(Debug)]
11797struct HeldBack {
11798 /// The issue.
11799 content: NativeId,
11800 /// Its board field values other than its `Status`, in the order they are written.
11801 fields: Vec<(String, Value)>,
11802 /// The `Status` field, and the option and its name that the item's status lands on.
11803 // llmlint: ignore[invalid_states_unrepresentable] The same triple `column_for` returns and `Prepared::column` carries, held back unchanged until it is sent.
11804 status: Option<(String, String, String)>,
11805 /// The fields it clears after those are written.
11806 clears: Vec<String>,
11807 /// The reason it is closed for, once its option is selected; `None` leaves it open.
11808 close: Option<ClosedState>,
11809 /// The issues it is blocked by.
11810 edges: Vec<NativeId>,
11811}
11812
11813/// The member [`answer_with_errors`] carries a response's GraphQL errors under, beside its data:
11814/// no GitHub type has a field of this name.
11815const ANSWER_ERRORS: &str = "onetaskgraphErrors";
11816
11817/// A response's `data` with its GraphQL errors beside it under [`ANSWER_ERRORS`], when it has
11818/// both — or `None`, for an answer [`GitHubProjectsSource::answer`] reads as it always has.
11819///
11820/// What [`graphql::CREATE_ISSUES`] is answered with: GitHub runs a document's mutation fields in
11821/// order and answers each one that ran, so a refusal of one alias leaves the issues the others
11822/// created named in the data beside it — the ids a copy needs to take them back.
11823fn answer_with_errors(body: &str) -> Option<Value> {
11824 let parsed: Value = serde_json::from_str(body).ok()?;
11825 let errors = parsed
11826 .get("errors")?
11827 .as_array()
11828 .filter(|errors| !errors.is_empty())?;
11829 let mut data = parsed.get("data").filter(|data| data.is_object())?.clone();
11830 data[ANSWER_ERRORS] = Value::Array(errors.clone());
11831 Some(data)
11832}
11833
11834/// The `createIssue` input that files the new issue on `board` and, when `incoming` names one,
11835/// under its project — see [`GitHubProjectsSource::create_filed_issue`].
11836fn filed_input(
11837 repository: &str,
11838 board: &str,
11839 incoming: &Incoming<'_>,
11840 body: &Option<String>,
11841) -> Value {
11842 let mut input = json!({"repositoryId":repository,"title":incoming.written_title(),
11843 "body":body,"projectV2Ids":[board]});
11844 if let Some(parent) = incoming.parent {
11845 input["parentIssueId"] = json!(parent.0);
11846 }
11847 input
11848}
11849
11850/// The name of option `option` of field `field` among a board's `fields`.
11851fn option_name<'a>(fields: &'a Value, field: &str, option: &str) -> Option<&'a str> {
11852 fields
11853 .get("nodes")?
11854 .as_array()?
11855 .iter()
11856 .find(|node| node.get("id").and_then(Value::as_str) == Some(field))?
11857 .get("options")?
11858 .as_array()?
11859 .iter()
11860 .find(|candidate| candidate.get("id").and_then(Value::as_str) == Some(option))?
11861 .get("name")?
11862 .as_str()
11863}
11864
11865/// What a write needs to know of one far end it names: which kind of item it is, and whether
11866/// it is an issue a native relationship can name.
11867struct FarEnd {
11868 kind: BoardKind,
11869 content_kind: ContentKind,
11870}
11871
11872/// Whether the issue one write reconciles was created by that write or was already there.
11873#[derive(Clone, Copy, PartialEq, Eq)]
11874enum Issue<'a> {
11875 /// Created by this write, so it holds no relationships yet.
11876 Created,
11877 /// On the board before this write, holding whatever relationships it holds — the far
11878 /// ends of its whole `blockedBy`, when the read that reached it carried them.
11879 Existing(Option<&'a [Value]>),
11880}
11881
11882/// What resolving one node id reached; see [`GitHubProjectsSource::reach`].
11883enum Reached {
11884 /// An issue this board holds, resolved into everything this source reports about it.
11885 Held(Box<Resolved>),
11886 /// Nothing this board holds: no such node, or a node on some other board.
11887 Nothing,
11888 /// A board draft, which [`graphql::ISSUE`] reaches and reads nothing of, so it is read
11889 /// again by [`GitHubProjectsSource::draft_by_id`].
11890 Draft,
11891}
11892
11893/// What GitHub says when a string is not a node id it can resolve.
11894///
11895/// Matched because it is the ordinary answer to a project selector naming a project by its
11896/// *name*, and reporting that as a failure would make naming one impossible. It is read
11897/// off the refusal GitHub sent, never guessed from the shape of the string: this source
11898/// does not define the syntax of a GitHub node id and would be wrong about it.
11899const UNRESOLVABLE_NODE: &str = "could not resolve to a node";
11900
11901/// `error` with `note` added to the end of what it says, its kind and every other member
11902/// unchanged — so a caller still branches on the failure that happened, and reads beside it
11903/// what that failure left behind.
11904fn noting(error: SourceError, note: &str) -> SourceError {
11905 match error {
11906 SourceError::Config { message } => SourceError::Config {
11907 message: message + note,
11908 },
11909 SourceError::Auth { message } => SourceError::Auth {
11910 message: message + note,
11911 },
11912 SourceError::Refused { message } => SourceError::Refused {
11913 message: message + note,
11914 },
11915 SourceError::RateLimited {
11916 retry_after_seconds,
11917 message,
11918 } => SourceError::RateLimited {
11919 retry_after_seconds,
11920 message: Some(message.unwrap_or_default() + note),
11921 },
11922 SourceError::Unavailable { message } => SourceError::Unavailable {
11923 message: message + note,
11924 },
11925 SourceError::Malformed { message } => SourceError::Malformed {
11926 message: message + note,
11927 },
11928 }
11929}
11930
11931/// The variables of one [`graphql::ISSUE_COPY_DETAILS`] request, as [`detail_batch`] binds its
11932/// own: a slot `batch` has no id for is bound to its last id, read again at no added price.
11933fn copy_detail_batch(batch: &[NativeId], comments: &PageRequest, children: bool) -> Value {
11934 let mut variables = serde_json::Map::new();
11935 for slot in 0..COPY_DETAIL_BATCH {
11936 let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
11937 variables.insert(format!("id{slot}"), json!(id));
11938 }
11939 variables.insert("project".to_owned(), json!(batch.first().map(|id| &id.0)));
11940 variables.insert("children".to_owned(), json!(children));
11941 variables.insert("first".to_owned(), json!(comments.limit.min(MAX_PAGE_SIZE)));
11942 variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
11943 variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
11944 variables.insert("duplicates".to_owned(), json!(true));
11945 Value::Object(variables)
11946}
11947
11948/// The variables of one [`graphql::ISSUE_DETAILS`] request over `batch` — at most
11949/// [`DETAIL_BATCH`] ids — each item with the first page of its comments when `comments` asks
11950/// for them.
11951///
11952/// The document is fixed-size, so a slot `batch` has no id for is bound to its last id, which
11953/// is read again at no added price.
11954fn detail_batch(batch: &[NativeId], comments: Option<&PageRequest>) -> Value {
11955 let mut variables = serde_json::Map::new();
11956 for slot in 0..DETAIL_BATCH {
11957 let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
11958 variables.insert(format!("id{slot}"), json!(id));
11959 }
11960 variables.insert(
11961 "first".to_owned(),
11962 json!(comments.map_or(MAX_PAGE_SIZE, |page| page.limit.min(MAX_PAGE_SIZE))),
11963 );
11964 variables.insert("comments".to_owned(), json!(comments.is_some()));
11965 variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
11966 variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
11967 variables.insert("duplicates".to_owned(), json!(true));
11968 Value::Object(variables)
11969}
11970
11971/// Whether this refusal is GitHub saying the id names no node at all.
11972fn unresolvable_node(error: &SourceError) -> bool {
11973 matches!(error, SourceError::Refused { message }
11974 if message.to_ascii_lowercase().contains(UNRESOLVABLE_NODE))
11975}
11976
11977/// One project name, as a search qualifier which filters on it at the server.
11978///
11979/// Quoted so the whole title is one phrase rather than a bag of words, with the two
11980/// characters GitHub's own quoting grammar gives a meaning inside a quoted phrase escaped
11981/// the way it documents. A title matched here is still compared for equality afterwards:
11982/// the qualifier narrows what the server sends, and this source decides what it names.
11983fn title_qualifier(name: &str) -> String {
11984 format!("in:title {}", quoted(name))
11985}
11986
11987/// `value` as one quoted phrase of a GitHub search or a board filter, with the two
11988/// characters GitHub's quoting grammar gives a meaning inside a quoted phrase escaped the way
11989/// it documents — so a value holding a qualifier's spelling is searched for rather than
11990/// obeyed.
11991fn quoted(value: &str) -> String {
11992 let escaped = value.replace('\\', "\\\\").replace('"', "\\\"");
11993 format!("\"{escaped}\"")
11994}
11995
11996/// The search qualifier for the issues updated at or after `since`.
11997///
11998/// Written to the second, rounded down, which can only widen what the search returns.
11999fn updated_qualifier(since: DateTime<Utc>) -> String {
12000 format!("updated:>={}", since.format("%Y-%m-%dT%H:%M:%S+00:00"))
12001}
12002
12003/// The search terms that narrow a board-scoped issue search to a task query's text and
12004/// metadata predicates, or `None` when it carries neither.
12005///
12006/// The text is one quoted phrase, searched `in:title`, `in:body` or both as its fields say,
12007/// and each metadata value is one more quoted phrase, which GitHub finds in the body because
12008/// its index covers the metadata comment the value is stored in. GitHub ANDs the phrases and
12009/// matches each in any field the `in:` qualifier names, so a query naming a title search and
12010/// a metadata value searches both fields for both — wider than asked, never narrower, and
12011/// every candidate is confirmed in process afterwards.
12012///
12013/// **This narrows a text search, and that is this source's declared semantics.** GitHub
12014/// matches whole tokens where a substring rule would match inside a word, so an item holding
12015/// the text only inside a longer word is not returned. A text of nothing but whitespace
12016/// matches every item, so it narrows nothing and is not sent.
12017fn narrowing_qualifiers(query: &TaskQuery) -> Option<String> {
12018 let text = query
12019 .text
12020 .as_ref()
12021 .filter(|text| !text.terms.trim().is_empty());
12022 if text.is_none() && query.metadata.is_empty() {
12023 return None;
12024 }
12025 let (title, body) = match text.map(|text| text.fields) {
12026 None => (false, true),
12027 Some(TextFields::Title) => (true, !query.metadata.is_empty()),
12028 Some(TextFields::Content) => (false, true),
12029 Some(TextFields::TitleOrContent) => (true, true),
12030 };
12031 let fields = match (title, body) {
12032 (true, true) => "in:title,body",
12033 (true, false) => "in:title",
12034 _ => "in:body",
12035 };
12036 let phrases = text
12037 .map(|text| text.terms.clone())
12038 .into_iter()
12039 .chain(
12040 query
12041 .metadata
12042 .iter()
12043 .map(|wanted| as_stored(wanted.value())),
12044 )
12045 .map(|phrase| quoted(&phrase))
12046 .collect::<Vec<_>>();
12047 Some(format!("{fields} {}", phrases.join(" ")))
12048}
12049
12050/// The search terms that narrow a board-scoped issue search to a project or document query's
12051/// text, or `None` when it has none or a blank one: the phrase, in the fields, a task query
12052/// carrying that text alone is sent as by [`narrowing_qualifiers`].
12053fn text_qualifiers(text: Option<&TextQuery>) -> Option<String> {
12054 narrowing_qualifiers(&TaskQuery {
12055 text: text.cloned(),
12056 ..TaskQuery::default()
12057 })
12058}
12059
12060/// Refuses a project or document query's text GitHub's issue search cannot find, before
12061/// anything is asked of GitHub, on exactly the terms [`refuse_unsearchable`] refuses a task
12062/// query's.
12063fn refuse_unsearchable_text(text: Option<&TextQuery>) -> Result<(), SourceError> {
12064 refuse_unsearchable(&TaskQuery {
12065 text: text.cloned(),
12066 ..TaskQuery::default()
12067 })
12068}
12069
12070/// Refuses a task query naming a text or a metadata value GitHub's issue search cannot find,
12071/// before anything is asked of GitHub.
12072///
12073/// GitHub's index holds words, so a phrase with no letter or digit names none to find, and no
12074/// bounded query answers it: sent, GitHub's answer to it is nothing this source may rely on;
12075/// left out, the search is every issue of the board. So this source says it cannot answer
12076/// rather than reading the board or answering nothing. A blank text is not refused: it narrows
12077/// nothing GitHub could search for, and keeps the board read it always had.
12078fn refuse_unsearchable(query: &TaskQuery) -> Result<(), SourceError> {
12079 const WHY: &str = "GitHub's issue search indexes words, so it cannot answer a value with no \
12080 letter or digit with a bounded query";
12081 if let Some(text) = &query.text
12082 && !text.terms.trim().is_empty()
12083 && !has_words(&text.terms)
12084 {
12085 return Err(SourceError::Refused {
12086 message: format!(
12087 "cannot search for the text {:?}: {WHY}; search for a text holding a letter or a digit",
12088 text.terms
12089 ),
12090 });
12091 }
12092 if let Some(wanted) = query
12093 .metadata
12094 .iter()
12095 .find(|wanted| !has_words(wanted.value()))
12096 {
12097 return Err(SourceError::Refused {
12098 message: format!(
12099 "cannot filter by the metadata value {:?} at {:?}: {WHY}; filter by a value holding a letter or a digit",
12100 wanted.value(),
12101 std::iter::once(wanted.key())
12102 .chain(wanted.path().iter().map(String::as_str))
12103 .collect::<Vec<_>>()
12104 .join("/"),
12105 ),
12106 });
12107 }
12108 Ok(())
12109}
12110
12111/// Whether GitHub's index could hold a word of `phrase`: whether it has a letter or a digit.
12112fn has_words(phrase: &str) -> bool {
12113 phrase.chars().any(char::is_alphanumeric)
12114}
12115
12116/// `value` spelled the way the metadata slot stores it: as the inside of its JSON string.
12117///
12118/// What GitHub indexes is the slot's JSON text, so a value holding a character JSON escapes —
12119/// a newline, a tab, a quote — is found by the escape the body holds and not by the character,
12120/// which GitHub's word match would read as different words.
12121fn as_stored(value: &str) -> String {
12122 let encoded = Value::String(value.to_owned()).to_string();
12123 encoded[1..encoded.len() - 1].to_owned()
12124}
12125
12126/// The one narrower question a task query carrying a text, metadata or origin predicate is
12127/// sent as.
12128enum Narrowing {
12129 /// Every carrier of this origin: [`graphql::ORIGIN_LOOKUP`].
12130 Origin(String),
12131 /// The board-scoped issue search narrowed by these qualifiers.
12132 Search(String),
12133}
12134
12135impl Narrowing {
12136 /// What this question is remembered under for the length of one command.
12137 fn key(&self) -> String {
12138 match self {
12139 Self::Origin(origin) => format!("origin {origin}"),
12140 Self::Search(also) => format!("search {also}"),
12141 }
12142 }
12143}
12144
12145/// Where one connection of [`graphql::ORIGIN_LOOKUP`] resumes.
12146enum Resumed {
12147 /// It reported another page, which starts after this cursor.
12148 More(String),
12149 /// It has ended. Sending this cursor again — the page's own end when it had one, and
12150 /// otherwise the cursor it was reached from — answers an empty page, so the one document
12151 /// can go on walking the other connection.
12152 Ended(Option<String>),
12153}
12154
12155impl Resumed {
12156 /// Whether the connection has another page.
12157 const fn has_more(&self) -> bool {
12158 matches!(self, Self::More(_))
12159 }
12160
12161 /// The cursor to send this connection next.
12162 fn cursor(self) -> Option<String> {
12163 match self {
12164 Self::More(next) => Some(next),
12165 Self::Ended(last) => last,
12166 }
12167 }
12168}
12169
12170/// Where `connection`, reached from `after`, resumes — refused when it reports another page
12171/// with no cursor to it, or from a cursor that does not advance.
12172fn resumed(connection: &Value, after: Option<&str>) -> Result<Resumed, SourceError> {
12173 let info = connection
12174 .get("pageInfo")
12175 .ok_or_else(|| SourceError::Malformed {
12176 message: "GitHub connection has no pageInfo".into(),
12177 })?;
12178 let end = optional_str(info, "endCursor")?;
12179 if required_bool(info, "hasNextPage")? {
12180 let next = end.ok_or_else(|| SourceError::Malformed {
12181 message: "GitHub connection reports another page and no endCursor".into(),
12182 })?;
12183 validate_cursor_progress(after, next)?;
12184 return Ok(Resumed::More(next.to_owned()));
12185 }
12186 Ok(Resumed::Ended(
12187 end.map(str::to_owned).or_else(|| after.map(str::to_owned)),
12188 ))
12189}
12190
12191/// The board, and every item on it this source reports.
12192#[derive(Clone)]
12193struct Board {
12194 id: String,
12195 fields: Value,
12196 items: Vec<Resolved>,
12197}
12198
12199/// What a write needs of the board and nothing more: its node id and its field
12200/// definitions, in the shape a read of the board's own `fields` gives them.
12201///
12202/// Deliberately no items. A write decides which item it writes, which parent it files
12203/// under and which far ends it names by reading each of them by its own id; this is the
12204/// half of the board those reads cannot carry, and holding no item is what keeps it from
12205/// ever being asked whether an item is there.
12206#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
12207struct BoardFields {
12208 id: BoardId,
12209 fields: Value,
12210}
12211
12212/// A board's node id: what a field write and `addProjectV2ItemById` address.
12213///
12214/// Never blank, because a blank one addresses no board — so an id GitHub answers blank is
12215/// refused where it is read, and one an item names blank is read as not named at all.
12216#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
12217#[serde(try_from = "String")]
12218struct BoardId(String);
12219
12220impl TryFrom<String> for BoardId {
12221 type Error = String;
12222
12223 /// The same refusal [`BoardId::parse`] makes, for an id read back from a kept context.
12224 fn try_from(id: String) -> Result<Self, Self::Error> {
12225 Self::parse(&id).map_err(|error| error.to_string())
12226 }
12227}
12228
12229/// Where one write left its item, for the record the rest of the command reads it out of.
12230///
12231/// A named record rather than a tuple because the update arm and the create arm each fill
12232/// all four, and two `Option`s of different meaning side by side in a tuple are two
12233/// positions a reader has to count.
12234struct Landed {
12235 /// The issue's own node id, which is the [`NativeId`] this source reports.
12236 content_id: NativeId,
12237 /// The board item's id, which is what a field write addresses — `None` for an issue its
12238 /// create filed on the board itself, whose board item GitHub adds after answering and
12239 /// [`TaskSource::finish_writes`] reads.
12240 // 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.
12241 item_id: Option<String>,
12242 /// The web address GitHub gave the issue, when it gave one.
12243 // 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.
12244 url: Option<String>,
12245 /// The issue's number on its repository, when GitHub reported one.
12246 number: Option<u64>,
12247}
12248
12249impl BoardId {
12250 fn parse(id: &str) -> Result<Self, SourceError> {
12251 if id.trim().is_empty() {
12252 return Err(SourceError::Malformed {
12253 message: "GitHub named a board with a blank node id".into(),
12254 });
12255 }
12256 Ok(Self(id.to_owned()))
12257 }
12258
12259 fn as_str(&self) -> &str {
12260 &self.0
12261 }
12262}
12263
12264impl Board {
12265 fn field<'a>(fields: &'a Value, name: &str) -> Result<Option<&'a Value>, SourceError> {
12266 complete_connection(fields, "project fields", NESTED_PAGE_SIZE)?;
12267 let nodes = fields
12268 .get("nodes")
12269 .and_then(Value::as_array)
12270 .ok_or_else(|| SourceError::Malformed {
12271 message: "GitHub project fields.nodes is not an array".into(),
12272 })?;
12273 Ok(nodes
12274 .iter()
12275 .find(|field| field.get("name").and_then(Value::as_str) == Some(name)))
12276 }
12277}
12278
12279/// One board item, resolved into everything this source reports about it.
12280///
12281/// Serializable because it is what [`TaskSource::write_context`] hands the store's cache and
12282/// [`TaskSource::prime_write`] takes back — see [`expected`].
12283#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
12284struct Resolved {
12285 item_id: String,
12286 id: NativeId,
12287 content_kind: ContentKind,
12288 kind: BoardKind,
12289 title: String,
12290 body: Option<String>,
12291 /// The body exactly as GitHub holds it, metadata slot and all, which is what a write
12292 /// that changes the slot alone has to keep byte for byte outside it.
12293 raw_body: Option<String>,
12294 status: Status,
12295 /// The name of the board `Status` option this item sits in, as the board spells it.
12296 option: Option<String>,
12297 /// What its `Priority` field says, read through this instance's mapping.
12298 priority: HeldPriority,
12299 /// Whether this item's issue is closed. A draft has no such state and is never closed.
12300 closed: bool,
12301 /// GitHub's reason for a closed issue's closure, as it spells it — `None` for an open
12302 /// issue, a draft, and an issue closed before GitHub recorded reasons.
12303 #[serde(default)]
12304 reason: Option<String>,
12305 /// The tasks this one delivers, read out of its slot. Empty for anything not a task.
12306 delivers: Vec<TaskRef>,
12307 /// Every task that delivers this one, read out of its slot. Empty for anything not a
12308 /// task.
12309 delivered_by: Vec<TaskRef>,
12310 labels: Vec<Label>,
12311 parent: Option<NativeId>,
12312 // 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.
12313 origin: Option<String>,
12314 /// What each board text field this instance projects metadata onto holds for this item,
12315 /// by field name — only the fields holding a value, an empty text included. What a write compares the
12316 /// item's metadata against, so a field already holding its value is not written again;
12317 /// never read back as metadata, which the slot alone reports.
12318 projected: BTreeMap<String, String>,
12319 /// The issue's own number on its repository, as GitHub reports it.
12320 ///
12321 /// `None` in exactly two cases: a draft, which has no number at all — `DraftIssue`
12322 /// declares none, and a draft is not filed in a repository to be numbered by one — and
12323 /// an issue this run created whose creating mutation answered without one, which is a
12324 /// response GitHub's own schema says cannot happen and which a landed write is not
12325 /// worth failing over. An `Issue` read off the board always has one.
12326 number: Option<u64>,
12327 url: Option<String>,
12328 created_at: Option<DateTime<Utc>>,
12329 updated_at: Option<DateTime<Utc>>,
12330 /// When the board item itself last changed — what a board field write moves — when the
12331 /// read that reached it said.
12332 item_updated_at: Option<DateTime<Utc>>,
12333 /// How many comments the issue holds, when the read that reached it said.
12334 comment_count: Option<u64>,
12335 /// How many relationship events the issue's timeline holds — every dependency, as
12336 /// `blockedBy` or `blocking`, and every project membership, as its parent, added or
12337 /// removed — when the read that reached it said.
12338 relationship_events: Option<u64>,
12339 /// The server's clock on the response that read it — its `Date` header — when the
12340 /// response carried one.
12341 served_at: Option<DateTime<Utc>>,
12342 /// The digest of its [`change_token::Facets`] — its own kind, its board field values and
12343 /// its labels — when the read that reached it carried all three whole.
12344 #[serde(default)]
12345 facets: Option<change_token::FacetsDigest>,
12346 own_repository: Option<Repository>,
12347 repositories: Vec<Repository>,
12348 classification: Classification,
12349 slot: BTreeMap<String, Value>,
12350 /// The node id of the board this item sits on, when the read that reached it said.
12351 board_id: Option<String>,
12352 /// The definition of every board field this item holds a value of, in the shape a read
12353 /// of the board's own `fields` gives one.
12354 ///
12355 /// Only the fields this item has a value in: a field it holds nothing of is not here,
12356 /// which says nothing about whether the board has it.
12357 fields: Vec<Value>,
12358 /// Every field the board this item sits on defines, as its own read of the board's
12359 /// `fields` gives them — when the read that reached the item carried them, which a read
12360 /// of it by its own id does. What a write of it needs of the board, then, needs no read
12361 /// of the board.
12362 board_fields: Option<Value>,
12363 /// The far ends of this issue's whole `blockedBy` connection, each as a dependency read
12364 /// selects one — when the read that reached it carried the connection to its end, which a
12365 /// read of it by its own id does for any issue blocked by no more than a page. What a
12366 /// write reconciles that relationship against, and what a read of its forward edges in
12367 /// the same command answers with.
12368 blocked_by: Option<Vec<Value>>,
12369}
12370
12371impl Resolved {
12372 /// The board this item's own read names it on, when that read named one this source can
12373 /// address.
12374 fn named_board(&self) -> Option<BoardId> {
12375 self.board_id
12376 .as_deref()
12377 .and_then(|id| BoardId::parse(id).ok())
12378 }
12379
12380 /// The board's id and every field it defines, when the read that reached this item
12381 /// carried both — which a read of it by its own id does.
12382 fn carried_board(&self) -> Option<BoardFields> {
12383 Some(BoardFields {
12384 id: self.named_board()?,
12385 fields: self.board_fields.clone()?,
12386 })
12387 }
12388
12389 /// Whether this item holds a value of the board field called `name`, and so carries
12390 /// that field's definition. `false` says nothing about whether the board has the field.
12391 fn defines(&self, name: &str) -> bool {
12392 self.fields
12393 .iter()
12394 .any(|field| field.get("name").and_then(Value::as_str) == Some(name))
12395 }
12396
12397 /// The metadata a caller sees: their own keys, plus the copy origin this source keeps
12398 /// in a field of its own, and none of the five keys that are only an encoding.
12399 ///
12400 /// The two delivery keys are left out for every kind, not only for a task: they are
12401 /// the encoding of [`Task::delivers`] and [`Task::delivered_by`], and a project or a
12402 /// document carrying one holds nothing a caller's own metadata could mean by it.
12403 fn metadata(&self) -> BTreeMap<String, Value> {
12404 let mut metadata = self.slot.clone();
12405 metadata.remove(Repository::METADATA_KEY);
12406 metadata.remove(DependencyEdge::RECORDED_KEY);
12407 metadata.remove(ItemKind::METADATA_KEY);
12408 metadata.remove(TaskRef::DELIVERS_KEY);
12409 metadata.remove(TaskRef::DELIVERED_BY_KEY);
12410 metadata.remove(Classification::METADATA_KEY);
12411 // The board field is the origin, and the body's copy of it is only a mirror for the
12412 // issue search to find: an item whose field holds none has none, whatever its body
12413 // says, so no reader ever sees two answers.
12414 metadata.remove(ORIGIN_KEY);
12415 if let Some(origin) = &self.origin {
12416 metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin.clone()));
12417 }
12418 metadata
12419 }
12420
12421 /// Where this item is, as a link a reader can open.
12422 ///
12423 /// A board is a hosted place and every issue on it has a web address, so that address
12424 /// is what "where is this?" means here — and [`Location::Url`] is what says which kind
12425 /// of place it is, so a reader knows to open it rather than to read a file out. It
12426 /// does not replace or derive from `url`: the field goes on reporting exactly what it
12427 /// reported before, and this says what that address *is*.
12428 ///
12429 /// An item GitHub gave no `url` for — a draft has none — reports no location at all
12430 /// rather than a third variant, which is the contract's "the source did not say". An
12431 /// issue this run created is not one of those: its address comes back from the
12432 /// creating mutation, so it is somewhere a reader can open from the moment it exists
12433 /// rather than from whenever the board read catches up.
12434 fn location(&self) -> Option<Location> {
12435 self.url.clone().map(Location::Url)
12436 }
12437
12438 /// The short handle this board's backend shows people for a task: the issue's number
12439 /// alone, as a decimal string.
12440 ///
12441 /// The number alone rather than `owner/repo#1043`, because that is the contract's
12442 /// value for this backend. A draft has no number and so no handle, which is the
12443 /// contract's *absent* rather than a handle of some other shape — and the native
12444 /// [`Task::id`] here is the issue's GraphQL node id, which this neither replaces nor
12445 /// derives from.
12446 fn key(&self) -> Option<String> {
12447 self.number.map(|number| number.to_string())
12448 }
12449
12450 /// This item's change token — see [`change_token`] — or `None` when the read that
12451 /// reached it did not carry all six of its parts, which a draft never does, did not say
12452 /// when GitHub served it, or found it changed too recently to have settled.
12453 fn version(&self) -> Option<String> {
12454 match self.content_kind {
12455 ContentKind::Issue => {
12456 let (issue, item) = (self.updated_at?, self.item_updated_at?);
12457 let parts = change_token::Parts {
12458 issue,
12459 item,
12460 comments: self.comment_count?,
12461 relationships: self.relationship_events?,
12462 facets: self.facets.clone()?,
12463 repository: self.own_repository.clone()?,
12464 };
12465 change_token::settled(issue, item, self.served_at?)
12466 .then(|| change_token::spell(&parts))
12467 }
12468 ContentKind::DraftIssue => None,
12469 }
12470 }
12471
12472 /// Whether its `Priority` field holds a value at all, mapped or not.
12473 fn holds_priority(&self) -> bool {
12474 self.priority != HeldPriority::Read(Priority::None)
12475 }
12476
12477 /// The task this item is.
12478 ///
12479 /// Fails for an item whose `Priority` field holds an option the mapping does not name:
12480 /// reading that as a level would be a guess, and reading it as `none` would let the next
12481 /// copy clear a priority a person set.
12482 fn task(&self) -> Result<Task, SourceError> {
12483 let priority = match &self.priority {
12484 HeldPriority::Read(priority) => *priority,
12485 HeldPriority::Unmapped(option) => {
12486 return Err(SourceError::Malformed {
12487 message: format!(
12488 "task {}{} sits in the board {PRIORITY_FIELD} option {option:?}, which \
12489 this source's priority_mapping does not name, so its priority cannot be \
12490 read; next: name {option:?} under priority_mapping, or move the item to \
12491 a mapped option",
12492 self.id,
12493 self.number
12494 .map(|number| format!(" (#{number})"))
12495 .unwrap_or_default()
12496 ),
12497 });
12498 }
12499 };
12500 Ok(Task {
12501 version: self.version(),
12502 id: self.id.clone(),
12503 key: self.key(),
12504 title: self.title.clone(),
12505 content: self.body.clone(),
12506 status: self.status.clone(),
12507 priority,
12508 labels: self.labels.clone(),
12509 project: self.parent.clone(),
12510 url: self.url.clone(),
12511 location: self.location(),
12512 created_at: self.created_at,
12513 updated_at: self.updated_at,
12514 metadata: self.metadata(),
12515 repositories: self.repositories.clone(),
12516 delivers: self.delivers.clone(),
12517 delivered_by: self.delivered_by.clone(),
12518 classification: self.classification,
12519 })
12520 }
12521
12522 fn project(&self) -> Project {
12523 Project {
12524 version: self.version(),
12525 id: self.id.clone(),
12526 title: self.title.clone(),
12527 content: self.body.clone(),
12528 status: self.status.clone(),
12529 labels: self.labels.clone(),
12530 url: self.url.clone(),
12531 location: self.location(),
12532 created_at: self.created_at,
12533 updated_at: self.updated_at,
12534 metadata: self.metadata(),
12535 repositories: self.repositories.clone(),
12536 classification: self.classification,
12537 }
12538 }
12539
12540 /// The same issue as a document: the project it is filed under, and no status and no
12541 /// dependencies, because a document is not work.
12542 fn document(&self) -> Document {
12543 Document {
12544 version: self.version(),
12545 id: self.id.clone(),
12546 title: self.title.clone(),
12547 content: self.body.clone(),
12548 project: self.parent.clone(),
12549 labels: self.labels.clone(),
12550 url: self.url.clone(),
12551 location: self.location(),
12552 created_at: self.created_at,
12553 updated_at: self.updated_at,
12554 metadata: self.metadata(),
12555 repositories: self.repositories.clone(),
12556 classification: self.classification,
12557 }
12558 }
12559}
12560
12561/// Where one targeted update moves an item's status, and which of its two halves move.
12562struct StatusMove {
12563 /// The board the item's `Status` field is on.
12564 board: BoardId,
12565 /// The `Status` field's id.
12566 field: String,
12567 /// The option's id.
12568 option: String,
12569 /// The option's name, as the board spells it.
12570 name: String,
12571 /// What the status asks of the issue's state.
12572 target: StatusTarget,
12573 /// The status the item reads as once it is there.
12574 landed: Status,
12575 /// Which of the status's two halves differ from what the item holds.
12576 moves: Moves,
12577}
12578
12579/// Which halves of an item's status one targeted update moves: its `Status` option, the open or
12580/// closed state of its issue, or both. A status neither half of which differs is no move at all,
12581/// and is not a value of this type.
12582#[derive(Clone, Copy, PartialEq, Eq)]
12583enum Moves {
12584 /// The option alone.
12585 Option,
12586 /// The issue's state alone: open, closed, or closed with another reason.
12587 State,
12588 /// Both.
12589 Both,
12590}
12591
12592impl Moves {
12593 /// What differs, or `None` when nothing does.
12594 const fn of(option: bool, state: bool) -> Option<Self> {
12595 match (option, state) {
12596 (true, true) => Some(Self::Both),
12597 (true, false) => Some(Self::Option),
12598 (false, true) => Some(Self::State),
12599 (false, false) => None,
12600 }
12601 }
12602
12603 /// Whether the option moves.
12604 const fn option(self) -> bool {
12605 matches!(self, Self::Option | Self::Both)
12606 }
12607
12608 /// Whether the issue's state moves.
12609 const fn state(self) -> bool {
12610 matches!(self, Self::State | Self::Both)
12611 }
12612}
12613
12614/// What one write is, and the status that comes with being it.
12615///
12616/// One value rather than a [`BoardKind`] beside an `Option<Status>`: a document has no
12617/// status and a task or a project always has one, so "a document carrying a status" and
12618/// "a task carrying none" are states a write cannot be in rather than states every use
12619/// site below has to defend against.
12620enum Written<'a> {
12621 /// A document, which is not work and so has no status at all.
12622 Document,
12623 /// A task or a project, and the status it is being written with.
12624 Work(ItemKind, &'a Status),
12625}
12626
12627impl Written<'_> {
12628 /// Which of the board's three kinds this write is.
12629 const fn kind(&self) -> BoardKind {
12630 match self {
12631 Self::Document => BoardKind::Document,
12632 Self::Work(kind, _) => BoardKind::Work(*kind),
12633 }
12634 }
12635
12636 /// The status this write carries. A document carries none, so a write of one says
12637 /// nothing about the issue's open or closed state and selects no board `Status`
12638 /// option.
12639 const fn status(&self) -> Option<&Status> {
12640 match self {
12641 Self::Document => None,
12642 Self::Work(_, status) => Some(status),
12643 }
12644 }
12645
12646 /// The status this write carries with the kind whose half of `status_mapping` it is
12647 /// written through.
12648 const fn work_status(&self) -> Option<(ItemKind, &Status)> {
12649 match self {
12650 Self::Document => None,
12651 Self::Work(kind, status) => Some((*kind, status)),
12652 }
12653 }
12654}
12655
12656/// The item being written, in the one shape all three write methods reach.
12657struct Incoming<'a> {
12658 written: Written<'a>,
12659 /// The title a person wrote. A document's goes onto the issue with
12660 /// [`DESIGN_TITLE_PREFIX`] put back, so a round trip returns the title that went in.
12661 title: &'a str,
12662 content: Option<&'a str>,
12663 assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
12664 labels: &'a [Label],
12665 metadata: &'a BTreeMap<String, Value>,
12666 repositories: &'a [Repository],
12667 /// Recorded in the slot while private, and nowhere while public.
12668 classification: Classification,
12669 parent: Option<&'a NativeId>,
12670 /// [`Task::delivers`], already checked. Empty for a project or a document, which is
12671 /// what keeps either key out of their slot.
12672 delivers: &'a [TaskRef],
12673 /// [`Task::delivered_by`], already checked. Empty for a project or a document.
12674 delivered_by: &'a [TaskRef],
12675 /// [`Task::priority`], for a task written to an instance that holds one; `None` for a
12676 /// project, a document, and every write to an instance with no `priority_mapping` —
12677 /// which is what keeps such a write's requests exactly what they were before.
12678 priority: Option<Priority>,
12679}
12680
12681/// What one write does to one projected metadata text field, by the field's id.
12682#[derive(Debug, Clone)]
12683enum ProjectionWrite {
12684 /// Write this text.
12685 Set {
12686 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.
12687 text: String,
12688 },
12689 /// Clear the field's value.
12690 Clear { field: String }, // llmlint: ignore[invalid_states_unrepresentable] As above.
12691}
12692
12693impl ProjectionWrite {
12694 /// Add this write to a field write's list of values and list of clears.
12695 fn join<'a>(&'a self, values: &mut Vec<(String, Value)>, clears: &mut Vec<&'a str>) {
12696 match self {
12697 Self::Set { field, text } => values.push((field.clone(), json!({"text": text}))),
12698 Self::Clear { field } => clears.push(field),
12699 }
12700 }
12701}
12702
12703/// What one write does to an item's `Priority` field.
12704enum PriorityWrite {
12705 /// Select this option of this field.
12706 Select {
12707 /// The `Priority` field's id.
12708 field: String,
12709 /// The mapped option's id.
12710 option: String,
12711 },
12712 /// Clear the field's value, which is what `none` is.
12713 Clear {
12714 /// The `Priority` field's id.
12715 field: String,
12716 },
12717}
12718
12719impl Incoming<'_> {
12720 /// The title this write puts on the issue.
12721 fn written_title(&self) -> String {
12722 match self.written {
12723 Written::Document => format!("{DESIGN_TITLE_PREFIX}{}", self.title),
12724 Written::Work(..) => self.title.to_owned(),
12725 }
12726 }
12727}
12728
12729#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
12730enum ContentKind {
12731 DraftIssue,
12732 Issue,
12733}
12734
12735/// What one board issue is: a document, or the work an [`ItemKind`] names.
12736///
12737/// A type of this source's own rather than an `ItemKind` with a third variant, because
12738/// `ItemKind` names what a dependency endpoint points at and nothing may point at a
12739/// document — the contract keeps a document out of that enum deliberately. Holding the
12740/// board's three answers in one value is what makes every place that asks "which is this?"
12741/// answer all three, rather than a `document: bool` beside a `kind` that means nothing for
12742/// two thirds of the board.
12743#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
12744enum BoardKind {
12745 /// An issue whose title begins [`DESIGN_TITLE_PREFIX`].
12746 Document,
12747 /// Every other issue, and every draft.
12748 Work(ItemKind),
12749}
12750
12751impl BoardKind {
12752 /// The kind of an item titled `title`, filed under a project or not, holding `sub_issues`
12753 /// sub-issues and carrying the kind marker `marked` in its slot.
12754 ///
12755 /// The design prefix is read *first*, before either of the two rules that separate a
12756 /// project from a task. A document is not work whatever sub-issues it has and whatever
12757 /// marker it carries, and reading the prefix later would make a design issue with none of
12758 /// either an empty project. Being a sub-issue wins outright over the rest, and no marker
12759 /// overrides it: an issue filed under a project is that project's task even when it has
12760 /// sub-issues of its own.
12761 fn of(title: &str, filed: bool, sub_issues: u64, marked: Option<ItemKind>) -> Self {
12762 if title.starts_with(DESIGN_TITLE_PREFIX) {
12763 Self::Document
12764 } else if filed {
12765 Self::Work(ItemKind::Task)
12766 } else if sub_issues > 0 || marked == Some(ItemKind::Project) {
12767 Self::Work(ItemKind::Project)
12768 } else {
12769 Self::Work(ItemKind::Task)
12770 }
12771 }
12772
12773 /// What the change token's facets call this kind.
12774 const fn facet(self) -> change_token::FacetKind {
12775 match self {
12776 Self::Document => change_token::FacetKind::Document,
12777 Self::Work(ItemKind::Project) => change_token::FacetKind::Project,
12778 Self::Work(ItemKind::Task) => change_token::FacetKind::Task,
12779 }
12780 }
12781
12782 /// Whose half of `status_mapping` an item of this kind reads its status through. A
12783 /// document has no status of its own, so the task half stands in for whatever the issue
12784 /// holds; nothing reports it.
12785 const fn status_kind(self) -> ItemKind {
12786 match self {
12787 Self::Document => ItemKind::Task,
12788 Self::Work(kind) => kind,
12789 }
12790 }
12791
12792 /// How a refusal names this kind to the person reading it.
12793 const fn describes(self) -> &'static str {
12794 match self {
12795 Self::Document => "document",
12796 Self::Work(kind) => kind.marker(),
12797 }
12798 }
12799}
12800
12801/// Whether `labels` satisfies `filter`, matching by name, case-insensitively.
12802///
12803/// This is the local Markdown source's `labels_match`, spelled the same way on purpose:
12804/// the shared cross-source journeys assert one answer to one question, so two sources
12805/// that disagree about what "carries the label bug" means fail them.
12806fn labels_match(labels: &[Label], filter: &LabelFilter) -> bool {
12807 let holds = |name: &String| {
12808 labels
12809 .iter()
12810 .any(|label| label.name.eq_ignore_ascii_case(name))
12811 };
12812 (filter.any_of.is_empty() || filter.any_of.iter().any(holds))
12813 && filter.all_of.iter().all(holds)
12814 && !filter.none_of.iter().any(holds)
12815}
12816
12817/// Whether `category` is one of `statuses`. An empty list is unfiltered rather than
12818/// "keeps nothing", which is what lets a `Vec<StatusCategory>` spell no filter at all.
12819fn status_matches(category: StatusCategory, statuses: &[StatusCategory]) -> bool {
12820 statuses.is_empty() || statuses.contains(&category)
12821}
12822
12823/// Whether `title`/`content` satisfies `query`, matching case-insensitively.
12824///
12825/// `content` is the item's own prose — the body with this source's trailing metadata
12826/// comment already taken off — so a search never matches an encoding the author of the
12827/// issue never wrote.
12828fn text_matches(title: &str, content: Option<&str>, query: &TextQuery) -> bool {
12829 let terms = query.terms.to_lowercase();
12830 let in_title = title.to_lowercase().contains(&terms);
12831 let in_content = content.is_some_and(|body| body.to_lowercase().contains(&terms));
12832 match query.fields {
12833 TextFields::Title => in_title,
12834 TextFields::Content => in_content,
12835 TextFields::TitleOrContent => in_title || in_content,
12836 }
12837}
12838
12839/// Whether `task` satisfies `query`, with `project` deciding the project predicate.
12840///
12841/// The project predicate is passed separately because a read narrowed to one project has
12842/// already answered it by asking *that project* for its own items — and re-applying it
12843/// there would compare the caller's selector, which may be a project's **name**, against
12844/// the id of the project that name resolved to, and keep nothing. Every other read passes
12845/// `query.project` and applies it here, which is what keeps `projects` a predicate this
12846/// source really does apply.
12847fn task_matches(task: &Task, query: &TaskQuery, project: &ProjectFilter) -> bool {
12848 labels_match(&task.labels, &query.labels)
12849 && status_matches(task.status.category, &query.statuses)
12850 && (query.priorities.is_empty() || query.priorities.contains(&task.priority))
12851 && match project {
12852 ProjectFilter::Any => true,
12853 ProjectFilter::Orphans => task.project.is_none(),
12854 ProjectFilter::Is(id) => task.project.as_ref() == Some(id),
12855 }
12856 && query
12857 .text
12858 .as_ref()
12859 .is_none_or(|text| text_matches(&task.title, task.content.as_deref(), text))
12860 // Against the parsed metadata slot, and against the origin field, which is where
12861 // `Resolved::metadata` reads each of them from.
12862 && query.metadata_matches(&task.metadata)
12863 && query.origin_matches(&task.metadata)
12864}
12865
12866fn project_matches(project: &Project, query: &ProjectQuery) -> bool {
12867 labels_match(&project.labels, &query.labels)
12868 && status_matches(project.status.category, &query.statuses)
12869 && query
12870 .text
12871 .as_ref()
12872 .is_none_or(|text| text_matches(&project.title, project.content.as_deref(), text))
12873}
12874
12875/// The same three predicates a task query carries, minus the status filter.
12876///
12877/// A document is not work, so it has no status for one to compare against and the query
12878/// type carries none. The project predicate is the same one — a design issue filed under a
12879/// project issue is in that project, and one filed under nothing is in none — so it is
12880/// spelled the same way here rather than answered differently.
12881fn document_matches(document: &Document, query: &DocumentQuery, project: &ProjectFilter) -> bool {
12882 labels_match(&document.labels, &query.labels)
12883 && match project {
12884 ProjectFilter::Any => true,
12885 ProjectFilter::Orphans => document.project.is_none(),
12886 ProjectFilter::Is(id) => document.project.as_ref() == Some(id),
12887 }
12888 && query
12889 .text
12890 .as_ref()
12891 .is_none_or(|text| text_matches(&document.title, document.content.as_deref(), text))
12892}
12893
12894/// Why a GitHub item's change token does not vouch for its forward edges.
12895///
12896/// A forward edge names its far end and that far end's kind, and the near item's token moves
12897/// with the edges it holds but says nothing of the far ends themselves: a far end turning from a
12898/// task into a project when it gains a sub-issue, retitled into a document, or deleted moves
12899/// nothing the near token reads, so an edge kept at the near version could name a far end that
12900/// is no longer what it said.
12901const FAR_ENDS_UNCOVERED: &str = "a github item's change token does not attest a far end's kind, \
12902 whether it became a document, or its deletion, so its forward edges are always read from \
12903 github";
12904
12905/// The issue-search qualifier that keeps open issues alone, which a status filter naming only
12906/// categories an open issue reads as narrows a board-scoped search with.
12907const OPEN_QUALIFIER: &str = "is:open";
12908
12909/// The board filter `task status reconcile` reads the board through: closed items alone.
12910const CLOSED_FILTER: &str = "is:closed";
12911
12912/// The variables of one [`graphql::ISSUES_BY_REFERENCE`] request for `batch`: each slot names
12913/// one reference, and a slot the batch has none for names its last one again.
12914fn reference_batch(batch: &[IssueRef]) -> Value {
12915 let mut variables = serde_json::Map::new();
12916 for slot in 0..REFERENCE_BATCH {
12917 let issue = batch.get(slot).or(batch.last());
12918 variables.insert(format!("owner{slot}"), json!(issue.map(|i| &i.owner)));
12919 variables.insert(format!("name{slot}"), json!(issue.map(|i| &i.name)));
12920 variables.insert(format!("number{slot}"), json!(issue.map(|i| i.number)));
12921 }
12922 variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
12923 variables.insert("statusField".to_owned(), json!(STATUS_FIELD));
12924 variables.insert("duplicates".to_owned(), json!(true));
12925 Value::Object(variables)
12926}
12927
12928/// One issue an issue read by reference answered, in the shape every other read of an issue
12929/// hands its resolver: each board membership's `Status` value, read by name, as the one field
12930/// value that membership carries.
12931///
12932/// A membership's `status` is `null` when its card sits at no option — or on a board with no
12933/// field of that name — and otherwise an option value naming its option; anything else is not
12934/// an answer to this read, and is refused rather than read as a card at no option.
12935fn held_issue(issue: &Value) -> Result<Value, SourceError> {
12936 let malformed = |what: &str| SourceError::Malformed {
12937 message: format!("GitHub answered an issue read by reference with {what}"),
12938 };
12939 let mut issue = issue.clone();
12940 let nodes = issue
12941 .pointer_mut("/projectItems/nodes")
12942 .and_then(Value::as_array_mut)
12943 .ok_or_else(|| malformed("no list of board memberships"))?;
12944 for node in nodes {
12945 let node = node
12946 .as_object_mut()
12947 .ok_or_else(|| malformed("a board membership that is not an object"))?;
12948 let status = match node.get("status") {
12949 None => return Err(malformed("a board membership with no status member")),
12950 Some(Value::Null) => None,
12951 Some(status) if status.get("name").is_some_and(Value::is_string) => {
12952 Some(status.clone())
12953 }
12954 // A single-select value of another field type answers `{}`; only an option value
12955 // carries a name.
12956 Some(status) if status.as_object().is_some_and(serde_json::Map::is_empty) => None,
12957 Some(_) => return Err(malformed("a membership status that names no option")),
12958 };
12959 node.insert(
12960 "fieldValues".to_owned(),
12961 json!({"nodes": status.into_iter().collect::<Vec<_>>(),
12962 "pageInfo": {"hasNextPage": false}}),
12963 );
12964 }
12965 Ok(issue)
12966}
12967
12968/// The issue one slot of a read by reference answered, `None` when GitHub answered that
12969/// slot's repository or issue `null` — which [`GitHubProjectsSource::answer_unresolved`]
12970/// admitted only beside a `NOT_FOUND` naming that very slot — and a refusal when the answer
12971/// leaves the slot, or its `issue` member, out: an answer missing a member is malformed rather
12972/// than a reference nothing holds.
12973fn referenced_issue(data: &Value, slot: usize) -> Result<Option<&Value>, SourceError> {
12974 let alias = format!("r{slot}");
12975 let repository = data.get(&alias).ok_or_else(|| SourceError::Malformed {
12976 message: format!("GitHub answered a read by reference with no {alias}"),
12977 })?;
12978 if repository.is_null() {
12979 return Ok(None);
12980 }
12981 let issue = repository
12982 .get("issue")
12983 .ok_or_else(|| SourceError::Malformed {
12984 message: format!("GitHub answered {alias} of a read by reference with no issue"),
12985 })?;
12986 if issue.is_null() {
12987 return Ok(None);
12988 }
12989 // `Repository.issue` is typed `Issue`, so anything else is not an answer to this read.
12990 if optional_str(issue, "__typename")? != Some("Issue") {
12991 return Err(SourceError::Malformed {
12992 message: format!(
12993 "GitHub answered {alias} of a read by reference with an issue whose __typename \
12994 is not Issue"
12995 ),
12996 });
12997 }
12998 Ok(Some(issue))
12999}
13000
13001/// A closure reason as a person reads it: `NOT_PLANNED` as `not planned`.
13002fn spoken_reason(reason: &str) -> String {
13003 reason.to_ascii_lowercase().replace('_', " ")
13004}
13005
13006impl GitHubProjectsSource {
13007 /// Every task and project of this board whose card contradicts its issue's closure, each
13008 /// judged against its own kind's half of `status_mapping`, read through the board's own
13009 /// `is:closed` filter, page by page — never the board's unfiltered `items`.
13010 ///
13011 /// Each item the filter answers is confirmed from its own node: an open one, one whose
13012 /// closure reads `unknown`, and one whose card is already in line are left out whatever
13013 /// the filter let through. The board's fields ride along and are held for the option
13014 /// writes that follow, so those need no read of the board.
13015 async fn contradictions(&self) -> Result<Vec<StatusContradiction>, SourceError> {
13016 let mut after: Option<String> = None;
13017 let mut found = Vec::new();
13018 loop {
13019 let data = self
13020 .graphql(
13021 graphql::CLOSED_ITEMS,
13022 json!({"owner":self.owner,"number":self.project_number,
13023 "first":MAX_PAGE_SIZE,"after":after,"filter":CLOSED_FILTER,
13024 "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
13025 )
13026 .await?;
13027 let board = data
13028 .pointer("/closedItems/projectV2")
13029 .filter(|value| !value.is_null())
13030 .ok_or_else(|| SourceError::Refused {
13031 message: format!(
13032 "GitHub project {}/{} was not found or is not visible to the token",
13033 self.owner, self.project_number
13034 ),
13035 })?;
13036 *self.fields_cache()? = Some(BoardFields {
13037 id: BoardId::parse(required_str(board, "id")?)?,
13038 fields: board.get("fields").cloned().unwrap_or(Value::Null),
13039 });
13040 let items = board.get("items").ok_or_else(|| SourceError::Malformed {
13041 message: "GitHub project closed items are missing".into(),
13042 })?;
13043 for item in items
13044 .get("nodes")
13045 .and_then(Value::as_array)
13046 .ok_or_else(|| SourceError::Malformed {
13047 message: "GitHub project items.nodes is not an array".into(),
13048 })?
13049 {
13050 let Some(resolved) = self.resolve(item)? else {
13051 continue;
13052 };
13053 let BoardKind::Work(kind) = resolved.kind else {
13054 continue;
13055 };
13056 let Some((closed_as, name)) = self.statuses.contradicted(
13057 kind,
13058 resolved.option.as_deref(),
13059 resolved.closed,
13060 resolved.reason.as_deref(),
13061 ) else {
13062 continue;
13063 };
13064 found.push(StatusContradiction {
13065 closed_as,
13066 to: name.to_owned(),
13067 option: resolved.option.clone(),
13068 item: match kind {
13069 ItemKind::Task => ContradictedItem::Task(resolved.task()?),
13070 ItemKind::Project => ContradictedItem::Project(resolved.project()),
13071 },
13072 });
13073 }
13074 let info = items
13075 .get("pageInfo")
13076 .ok_or_else(|| SourceError::Malformed {
13077 message: "GitHub project items have no pageInfo".into(),
13078 })?;
13079 if !required_bool(info, "hasNextPage")? {
13080 return Ok(found);
13081 }
13082 let next = required_str(info, "endCursor")?;
13083 validate_cursor_progress(after.as_deref(), next)?;
13084 after = Some(next.to_owned());
13085 }
13086 }
13087
13088 /// What this board holds for each issue in `issues`, once each in the order given, read
13089 /// [`REFERENCE_BATCH`] references at a time — and nothing else of the board.
13090 ///
13091 /// An issue GitHub cannot find for this source's credential, and one with no item on this
13092 /// board, are both absent. A closure that already says the work will not be done here —
13093 /// not planned, duplicate — is skipped: a landing does not overrule it. An item closed as
13094 /// completed whose card sits at its kind's `done` option is unchanged, and every other item
13095 /// is due a `done` write. A project item — a plan's home card, its issue holding the plan's
13096 /// tasks as sub-issues — is held under exactly these rules, judged against the project
13097 /// kind's half of `status_mapping`.
13098 async fn holdings(&self, issues: &[IssueRef]) -> Result<Vec<IssueHolding>, SourceError> {
13099 let mut distinct: Vec<IssueRef> = Vec::with_capacity(issues.len());
13100 for issue in issues {
13101 if !distinct.contains(issue) {
13102 distinct.push(issue.clone());
13103 }
13104 }
13105 let done = |kind: ItemKind| {
13106 self.statuses
13107 .target(kind, StatusCategory::Done)
13108 .option()
13109 .unwrap_or("Done")
13110 .to_owned()
13111 };
13112 // GitHub numbers an issue with a GraphQL `Int`, a signed 32-bit value, so a reference past
13113 // its range names no issue any repository can hold: it is answered absent and never sent,
13114 // where binding it would have GitHub refuse the whole batch.
13115 let readable: Vec<IssueRef> = distinct
13116 .iter()
13117 .filter(|issue| i32::try_from(issue.number).is_ok())
13118 .cloned()
13119 .collect();
13120 let mut holdings = Vec::with_capacity(readable.len());
13121 for batch in readable.chunks(REFERENCE_BATCH) {
13122 let data = self
13123 .graphql(graphql::ISSUES_BY_REFERENCE, reference_batch(batch))
13124 .await?;
13125 for (slot, issue) in batch.iter().enumerate() {
13126 let node = referenced_issue(&data, slot)?;
13127 let resolved = match node {
13128 Some(node) => self.resolve_issue_as(&held_issue(node)?, false).await?,
13129 None => None,
13130 };
13131 // A task and a project are both held; a document is not work, so nothing is.
13132 let Some((item, kind)) = resolved.and_then(|item| match item.kind {
13133 BoardKind::Work(kind) => Some((item, kind)),
13134 BoardKind::Document => None,
13135 }) else {
13136 holdings.push(IssueHolding {
13137 issue: issue.clone(),
13138 completion: Completion::Absent,
13139 });
13140 continue;
13141 };
13142 let task = match kind {
13143 ItemKind::Task => {
13144 let held = item.task()?;
13145 HeldTask {
13146 id: held.id,
13147 kind,
13148 option: item.option.clone(),
13149 classification: held.classification,
13150 repositories: held.repositories,
13151 delivers: held.delivers,
13152 }
13153 }
13154 // A project delivers no task, so what its write keeps in step is nothing.
13155 ItemKind::Project => {
13156 let held = item.project();
13157 HeldTask {
13158 id: held.id,
13159 kind,
13160 option: item.option.clone(),
13161 classification: held.classification,
13162 repositories: held.repositories,
13163 delivers: Vec::new(),
13164 }
13165 }
13166 };
13167 let done = done(kind);
13168 let completion = match (item.closed, item.reason.as_deref()) {
13169 (true, Some(reason @ ("NOT_PLANNED" | "DUPLICATE"))) => Completion::Skipped {
13170 task,
13171 reason: format!("closed as {}", spoken_reason(reason)),
13172 },
13173 (true, None | Some("COMPLETED"))
13174 if item
13175 .option
13176 .as_deref()
13177 .is_some_and(|option| option.eq_ignore_ascii_case(&done)) =>
13178 {
13179 Completion::Unchanged { task }
13180 }
13181 _ => {
13182 let kept = item.clone();
13183 self.held_cache
13184 .lock()
13185 .map_err(|_| SourceError::Unavailable {
13186 message: "items read by issue reference were left \
13187 inconsistent; run the command again"
13188 .into(),
13189 })?
13190 .insert(kept.id.clone(), kept);
13191 Completion::Due {
13192 task,
13193 to: done.clone(),
13194 }
13195 }
13196 };
13197 holdings.push(IssueHolding {
13198 issue: issue.clone(),
13199 completion,
13200 });
13201 }
13202 }
13203 let mut read = holdings.into_iter();
13204 let mut answered = Vec::with_capacity(distinct.len());
13205 for issue in distinct {
13206 if i32::try_from(issue.number).is_ok() {
13207 answered.push(read.next().ok_or_else(|| SourceError::Malformed {
13208 message: format!("no holding was read for {issue}"),
13209 })?);
13210 } else {
13211 answered.push(IssueHolding {
13212 issue,
13213 completion: Completion::Absent,
13214 });
13215 }
13216 }
13217 Ok(answered)
13218 }
13219}
13220
13221#[async_trait::async_trait]
13222impl TaskSource for GitHubProjectsSource {
13223 fn kind(&self) -> &'static str {
13224 KIND
13225 }
13226 fn order_versions(&self, a: &str, b: &str) -> Option<std::cmp::Ordering> {
13227 change_token::order(a, b)
13228 }
13229 /// A task's fields and comments: §4.13d of `docs/plugin-protocol.md` states, member by
13230 /// member, the change each part of the token moves for. Not its forward edges — see
13231 /// [`FAR_ENDS_UNCOVERED`].
13232 fn version_coverage(&self) -> VersionCoverage {
13233 VersionCoverage {
13234 forward_edges: Coverage::Uncovered {
13235 reason: FAR_ENDS_UNCOVERED.to_owned(),
13236 },
13237 ..VersionCoverage::whole()
13238 }
13239 }
13240 /// Every part, for a copy's skip: the relationship count moves for an edge added or removed
13241 /// and for a far end deleted, so the token sees every change to a task's edges but one — a
13242 /// far end's own kind flipping, by gaining a sub-issue or being retitled into a document,
13243 /// which `spike-coverage` measured stale 1 of 1. A skipping recopy does not carry that, and
13244 /// the next copy that reads the task does (`docs/plugin-protocol.md` §4.13d).
13245 // 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.
13246 fn copy_version_coverage(&self) -> VersionCoverage {
13247 VersionCoverage::whole()
13248 }
13249 /// A project's fields, and not its forward edges, for the same reason a task's are not.
13250 fn project_version_coverage(&self) -> ProjectVersionCoverage {
13251 ProjectVersionCoverage {
13252 fields: Coverage::Covered,
13253 forward_edges: Coverage::Uncovered {
13254 reason: FAR_ENDS_UNCOVERED.to_owned(),
13255 },
13256 }
13257 }
13258 /// A document's fields and the manifest of its image assets, which its metadata slot
13259 /// records: both are the issue's body, whose every edit moves the token's issue time.
13260 fn document_version_coverage(&self) -> DocumentVersionCoverage {
13261 DocumentVersionCoverage::whole()
13262 }
13263 fn write_context(&self, id: &NativeId) -> Option<Value> {
13264 self.context_of(id)
13265 }
13266 fn board_context(&self) -> Option<Value> {
13267 self.board_context_held()
13268 }
13269 fn prime_write(&self, id: &NativeId, context: &Value, board: Option<&Value>) -> bool {
13270 self.prime(id, context, board)
13271 }
13272 fn unprime(&self, id: &NativeId) {
13273 self.unprime_held(id);
13274 }
13275 fn placement_context(&self) -> Option<Value> {
13276 self.placement_held()
13277 }
13278 fn prime_placement(&self, context: &Value) -> bool {
13279 self.prime_placement_held(context)
13280 }
13281 fn written_context(&self, id: &NativeId) -> Option<Value> {
13282 self.written_held(id)
13283 }
13284 fn prime_written(&self, id: &NativeId, context: &Value, task: &Task) -> bool {
13285 self.prime_written_held(id, context, task)
13286 }
13287 fn begin_journal(&self) {
13288 self.expected.begin();
13289 }
13290 fn end_journal(&self) -> Option<WriteJournal> {
13291 Some(self.expected.end())
13292 }
13293 fn capabilities(&self) -> Capabilities {
13294 Capabilities {
13295 projects: Support::Native,
13296 documents: Support::Native,
13297 comments: Support::Native,
13298 assets: Support::Native,
13299 priority: if self.priorities.is_some() {
13300 Support::Native
13301 } else {
13302 Support::Unsupported
13303 },
13304 filter_by_priority: Support::Native,
13305 filter_by_comment_activity: Support::Native,
13306 filter_by_metadata: Support::Native,
13307 filter_by_origin: Support::Native,
13308 reconcile_status: Support::Native,
13309 complete_by_issue: Support::Native,
13310 orphan_tasks: Support::Native,
13311 filter_by_label: Support::Native,
13312 filter_by_status: Support::Native,
13313 search_title: Support::Native,
13314 search_content: Support::Native,
13315 task_dependencies: DependencySupport::BothDirections,
13316 project_dependencies: DependencySupport::BothDirections,
13317 max_page_size: MAX_PAGE_SIZE,
13318 }
13319 }
13320 async fn visibility(
13321 &self,
13322 target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13323 ) -> Result<onetaskgraph_plugin_api::Visibility, SourceError> {
13324 self.write_visibility(target).await
13325 }
13326 async fn visibility_with_project(
13327 &self,
13328 target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13329 project: &onetaskgraph_plugin_api::WriteTarget<'_>,
13330 ) -> Result<onetaskgraph_plugin_api::Visibility, SourceError> {
13331 self.write_visibility_with_project(target, project).await
13332 }
13333 fn declare_visibility(&self, declared: onetaskgraph_plugin_api::Visibility) {
13334 self.declared_private.store(
13335 declared == onetaskgraph_plugin_api::Visibility::Private,
13336 std::sync::atomic::Ordering::SeqCst,
13337 );
13338 }
13339 fn scope_visibility<'a>(
13340 &'a self,
13341 operation: std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send + 'a>>,
13342 ) -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send + 'a>> {
13343 self.scope_visibility_reads(operation)
13344 }
13345
13346 /// Where a create of `target` would file its issue, by [`Self::creation_rule`] — the rule
13347 /// the create applies — reading the parent project's issue only when `target` names one.
13348 async fn creation_repository(
13349 &self,
13350 target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13351 ) -> Result<Option<Repository>, SourceError> {
13352 let onetaskgraph_plugin_api::WriteTarget::New {
13353 repositories,
13354 project,
13355 } = target
13356 else {
13357 return Ok(None);
13358 };
13359 let target = self
13360 .creation_rule(repositories, *project, "a new item")
13361 .await?;
13362 Repository::try_from(target.origin())
13363 .map(Some)
13364 .map_err(|message| SourceError::Malformed { message })
13365 }
13366 async fn health(&self) -> Result<Health, SourceError> {
13367 let board = self.board_page(None, 1).await?;
13368 Ok(Health {
13369 reachable: true,
13370 detail: Some(format!(
13371 "reading GitHub project {}/{} ({})",
13372 self.owner,
13373 self.project_number,
13374 required_str(&board, "title")?
13375 )),
13376 })
13377 }
13378 /// The board's own node id, digested: the one request reading its fields, or none when
13379 /// this command has read them already.
13380 async fn designator(&self) -> Result<Option<Designator>, SourceError> {
13381 let board = self.board_fields().await?;
13382 Designator::of_backend(KIND, board.id.as_str())
13383 .map(Some)
13384 .map_err(|message| SourceError::Malformed { message })
13385 }
13386 async fn get_task(&self, id: &NativeId) -> Result<Option<Task>, SourceError> {
13387 self.item_by_id(id)
13388 .await?
13389 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
13390 .map(|item| item.task())
13391 .transpose()
13392 }
13393 async fn task_assets(
13394 &self,
13395 id: &NativeId,
13396 ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
13397 self.held_assets(id, BoardKind::Work(ItemKind::Task)).await
13398 }
13399 async fn task_asset(
13400 &self,
13401 id: &NativeId,
13402 name: &onetaskgraph_plugin_api::AssetName,
13403 ) -> Result<Option<Vec<u8>>, SourceError> {
13404 self.held_asset(id, BoardKind::Work(ItemKind::Task), name)
13405 .await
13406 }
13407 async fn set_task_rendering_with_assets(
13408 &self,
13409 id: &NativeId,
13410 content: &str,
13411 provenance: &Value,
13412 _answers: &BTreeMap<String, Value>,
13413 assets: &onetaskgraph_plugin_api::AssetWrite,
13414 ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
13415 self.replace_rendering(
13416 id,
13417 BoardKind::Work(ItemKind::Task),
13418 content,
13419 provenance,
13420 Some(assets),
13421 )
13422 .await
13423 }
13424 async fn document_assets(
13425 &self,
13426 id: &NativeId,
13427 ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
13428 self.held_assets(id, BoardKind::Document).await
13429 }
13430 async fn document_asset(
13431 &self,
13432 id: &NativeId,
13433 name: &onetaskgraph_plugin_api::AssetName,
13434 ) -> Result<Option<Vec<u8>>, SourceError> {
13435 self.held_asset(id, BoardKind::Document, name).await
13436 }
13437 async fn set_document_rendering_with_assets(
13438 &self,
13439 id: &NativeId,
13440 content: &str,
13441 provenance: &Value,
13442 _answers: &BTreeMap<String, Value>,
13443 assets: &onetaskgraph_plugin_api::AssetWrite,
13444 ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
13445 self.replace_rendering(id, BoardKind::Document, content, provenance, Some(assets))
13446 .await
13447 }
13448 async fn get_project(&self, id: &NativeId) -> Result<Option<Project>, SourceError> {
13449 Ok(self
13450 .item_by_id(id)
13451 .await?
13452 .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
13453 .map(|item| item.project()))
13454 }
13455 async fn query_tasks(
13456 &self,
13457 query: &TaskQuery,
13458 page: &PageRequest,
13459 ) -> Result<Page<Task>, SourceError> {
13460 validate_page(page)?;
13461 refuse_unsearchable(query)?;
13462 // A closed issue reads `done`, `cancelled` or — closed for a reason that implies no
13463 // category — `unknown`; every other category reads only off an open issue or a draft. So
13464 // a status filter naming none of those three asks every issue search it sends for open
13465 // issues alone; each candidate's category is still confirmed from its own item below.
13466 let open_only = !query.statuses.is_empty()
13467 && query.statuses.iter().all(|category| {
13468 !matches!(
13469 category,
13470 StatusCategory::Done | StatusCategory::Cancelled | StatusCategory::Unknown
13471 )
13472 });
13473 if query.origin.is_none() && !matches!(query.project, ProjectFilter::Is(_)) {
13474 let mut qualifiers: Vec<String> = [
13475 query.commented_since.map(updated_qualifier),
13476 narrowing_qualifiers(query),
13477 ]
13478 .into_iter()
13479 .flatten()
13480 .collect();
13481 // A narrowed search never answers a draft, which is no issue, so narrowing it to
13482 // open issues too leaves out nothing such a search could have answered.
13483 if !qualifiers.is_empty() {
13484 if open_only {
13485 qualifiers.push(OPEN_QUALIFIER.to_owned());
13486 }
13487 return self.search_tasks(query, page, &qualifiers.join(" ")).await;
13488 }
13489 }
13490
13491 // A read narrowed to one project asks that project for its own tasks, so nothing
13492 // about it costs what the rest of the board holds. A read carrying a text, metadata
13493 // or origin predicate asks GitHub the narrower question those predicates are, and a
13494 // read narrowed to comment activity alone asks the board's own issue search for the
13495 // issues updated since, which is every issue a comment could have been written or
13496 // edited on since. Every other task read is a question about the whole board and is
13497 // answered by reading it.
13498 let (held, membership) = match (&query.project, query.commented_since) {
13499 (ProjectFilter::Is(project), _) => (
13500 match self.held_task_children(project)? {
13501 // Taken as `project_children` takes the project's own sub-issue read: with
13502 // every item this process wrote under it since, and none it moved away.
13503 Some(held) => self.completed_with_written(
13504 held.into_iter()
13505 .filter(|child| child.parent.as_ref() == Some(project))
13506 .collect(),
13507 |own| own.parent.as_ref() == Some(project),
13508 )?,
13509 None => self.project_children(project).await?,
13510 },
13511 // Answered by where these items came from; see `task_matches`.
13512 &ProjectFilter::Any,
13513 ),
13514 (ProjectFilter::Any | ProjectFilter::Orphans, since) => {
13515 match (self.narrowed(query).await?, since) {
13516 (Some(narrowed), _) => (narrowed, &query.project),
13517 (None, Some(since)) => (self.updated_since(since).await?, &query.project),
13518 // The board's own items list every draft; only the search half, which
13519 // completes them with issues they lag behind on, is narrowed — a closed
13520 // issue that half leaves out is at no category the filter keeps.
13521 (None, None) if open_only => (
13522 self.board_completed_by(Some(OPEN_QUALIFIER)).await?.items,
13523 &query.project,
13524 ),
13525 (None, None) => (self.board().await?.items, &query.project),
13526 }
13527 }
13528 };
13529 // Filtered before paged: a page of a filtered result is a page of the survivors,
13530 // never the survivors of a page.
13531 let mut tasks = Vec::new();
13532 for item in held
13533 .iter()
13534 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
13535 {
13536 let task = item.task()?;
13537 if task_matches(&task, query, membership)
13538 && self.commented_since(item, query.commented_since).await?
13539 {
13540 tasks.push(task);
13541 }
13542 }
13543 Ok(offset_page(
13544 tasks,
13545 numeric_cursor(page.cursor.as_ref())?,
13546 page.limit.min(MAX_PAGE_SIZE) as usize,
13547 ))
13548 }
13549 async fn query_projects(
13550 &self,
13551 query: &ProjectQuery,
13552 page: &PageRequest,
13553 ) -> Result<Page<Project>, SourceError> {
13554 validate_page(page)?;
13555 refuse_unsearchable_text(query.text.as_ref())?;
13556 // The projects a board holds are found by an issue search scoped to that board,
13557 // never by walking the board's own item connection: what tells a project from a
13558 // task is the `parent` each issue carries, which costs nothing to read. A query
13559 // carrying a text asks that search for the text too, so it reads the issues that
13560 // hold it rather than every issue of the board.
13561 let held = match self.text_searched(query.text.as_ref()).await? {
13562 Some(searched) => searched,
13563 None => self.board_issues().await?,
13564 };
13565 let projects = held
13566 .iter()
13567 .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
13568 .map(Resolved::project)
13569 .filter(|project| project_matches(project, query))
13570 .collect();
13571 Ok(offset_page(
13572 projects,
13573 numeric_cursor(page.cursor.as_ref())?,
13574 page.limit.min(MAX_PAGE_SIZE) as usize,
13575 ))
13576 }
13577 async fn query_projects_by_origin(
13578 &self,
13579 origin: &str,
13580 page: &PageRequest,
13581 ) -> Result<Page<Project>, SourceError> {
13582 validate_page(page)?;
13583 let projects = self
13584 .copied_from(origin, BoardKind::Work(ItemKind::Project))
13585 .await?
13586 .iter()
13587 .map(Resolved::project)
13588 .collect();
13589 Ok(offset_page(
13590 projects,
13591 numeric_cursor(page.cursor.as_ref())?,
13592 page.limit.min(MAX_PAGE_SIZE) as usize,
13593 ))
13594 }
13595 async fn get_document(&self, id: &NativeId) -> Result<Option<Document>, SourceError> {
13596 let Some(item) = self
13597 .document_by_id(id)
13598 .await?
13599 .filter(|item| item.kind == BoardKind::Document)
13600 else {
13601 return Ok(None);
13602 };
13603 self.expected.read_document(&item);
13604 Ok(Some(item.document()))
13605 }
13606 async fn query_documents_by_origin(
13607 &self,
13608 origin: &str,
13609 page: &PageRequest,
13610 ) -> Result<Page<Document>, SourceError> {
13611 validate_page(page)?;
13612 let documents = self
13613 .copied_from(origin, BoardKind::Document)
13614 .await?
13615 .iter()
13616 .map(Resolved::document)
13617 .collect();
13618 Ok(offset_page(
13619 documents,
13620 numeric_cursor(page.cursor.as_ref())?,
13621 page.limit.min(MAX_PAGE_SIZE) as usize,
13622 ))
13623 }
13624 async fn query_documents(
13625 &self,
13626 query: &DocumentQuery,
13627 page: &PageRequest,
13628 ) -> Result<Page<Document>, SourceError> {
13629 validate_page(page)?;
13630 // Narrowed to one project, this is the same sub-issue read a task list scoped to
13631 // that project makes — a document filed under a project is a sub-issue of it too,
13632 // and which of them come back is the kind this caller asked for. Unscoped, a query
13633 // carrying a text asks the board-scoped issue search for it, as a task query does,
13634 // and only one carrying none reads the board.
13635 let (held, membership) = match &query.project {
13636 ProjectFilter::Is(project) => (
13637 self.project_children(project).await?,
13638 // Answered by where these items came from; see `task_matches`.
13639 &ProjectFilter::Any,
13640 ),
13641 ProjectFilter::Any | ProjectFilter::Orphans => {
13642 refuse_unsearchable_text(query.text.as_ref())?;
13643 match self.text_searched(query.text.as_ref()).await? {
13644 Some(searched) => (searched, &query.project),
13645 None => (self.board().await?.items, &query.project),
13646 }
13647 }
13648 };
13649 // Filtered before paged, exactly as a task read is: a page of a filtered result is
13650 // a page of the survivors, never the survivors of a page.
13651 let documents = held
13652 .iter()
13653 .filter(|item| item.kind == BoardKind::Document)
13654 .map(Resolved::document)
13655 .filter(|document| document_matches(document, query, membership))
13656 .collect();
13657 Ok(offset_page(
13658 documents,
13659 numeric_cursor(page.cursor.as_ref())?,
13660 page.limit.min(MAX_PAGE_SIZE) as usize,
13661 ))
13662 }
13663 async fn labels(&self, page: &PageRequest) -> Result<Page<Label>, SourceError> {
13664 validate_page(page)?;
13665 let offset = numeric_cursor(page.cursor.as_ref())?;
13666 let mut labels = self
13667 .board()
13668 .await?
13669 .items
13670 .into_iter()
13671 .flat_map(|item| item.labels)
13672 .fold(Vec::new(), |mut all, label| {
13673 if !all.iter().any(|x: &Label| x.id == label.id) {
13674 all.push(label);
13675 }
13676 all
13677 });
13678 labels.sort_by(|a, b| a.name.cmp(&b.name).then(a.id.0.cmp(&b.id.0)));
13679 Ok(offset_page(
13680 labels,
13681 offset,
13682 page.limit.min(MAX_PAGE_SIZE) as usize,
13683 ))
13684 }
13685 async fn task_dependencies(
13686 &self,
13687 id: &NativeId,
13688 direction: Direction,
13689 page: &PageRequest,
13690 ) -> Result<Page<DependencyEdge>, SourceError> {
13691 self.dependencies(id, ItemKind::Task, direction, page).await
13692 }
13693 async fn project_dependencies(
13694 &self,
13695 id: &NativeId,
13696 direction: Direction,
13697 page: &PageRequest,
13698 ) -> Result<Page<DependencyEdge>, SourceError> {
13699 self.dependencies(id, ItemKind::Project, direction, page)
13700 .await
13701 }
13702
13703 fn writes(&self) -> WriteSupport {
13704 WriteSupport::Supported
13705 }
13706
13707 /// Create or update one task.
13708 ///
13709 /// Its `delivers` and `delivered_by` are checked before anything is read or written —
13710 /// neither may name the task itself or name one task twice — and land in the body's
13711 /// metadata slot under their reserved keys, in place of any caller metadata of those
13712 /// names.
13713 async fn write_task(&self, write: &ItemWrite<Task>) -> Result<NativeId, SourceError> {
13714 self.write_task_assets(write, None)
13715 .await
13716 .map(|written| written.id)
13717 }
13718
13719 async fn read_ahead_origins_for_copy(&self, origins: &[String]) -> Result<(), SourceError> {
13720 self.origins_ahead(origins).await
13721 }
13722
13723 async fn member_classifications(
13724 &self,
13725 project: &NativeId,
13726 ) -> Result<Option<Vec<(Classification, Vec<Repository>)>>, SourceError> {
13727 Ok(self
13728 .member_classes
13729 .lock()
13730 .map_err(|_| SourceError::Unavailable {
13731 message: "a project's members held for its classification were left \
13732 inconsistent; run the command again"
13733 .into(),
13734 })?
13735 .remove(project))
13736 }
13737
13738 /// The issue's own web address, as the `createIssue` that made it in this command answered
13739 /// it: a task, a project and a document are each an issue here.
13740 fn written_location(
13741 &self,
13742 _record: onetaskgraph_plugin_api::MetadataRecord,
13743 id: &NativeId,
13744 ) -> Option<Location> {
13745 self.written
13746 .lock()
13747 .unwrap_or_else(std::sync::PoisonError::into_inner)
13748 .get(id)
13749 .cloned()
13750 .map(Location::Url)
13751 }
13752
13753 /// [`MUTATION_BATCH`], while writes are held back: [`graphql::CREATE_ISSUES`] creates that many
13754 /// in one request, and a copy's tasks are created together only then, since their fields
13755 /// and edges are what is held back.
13756 fn create_batch_limit(&self) -> usize {
13757 if self.deferring().unwrap_or(false) {
13758 MUTATION_BATCH
13759 } else {
13760 1
13761 }
13762 }
13763
13764 // 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.
13765 async fn write_tasks(&self, writes: &[ItemWrite<Task>]) -> Result<Vec<NativeId>, SourceError> {
13766 if let Some(write) = writes.iter().find(|write| write.target.is_some()) {
13767 return Err(SourceError::Refused {
13768 message: format!(
13769 "write_tasks creates tasks, and the write of {} names the existing task {}; \
13770 next: update it with write_task",
13771 write.item.id,
13772 write.target.as_ref().map_or("", |target| target.0.as_str())
13773 ),
13774 });
13775 }
13776 if !self.deferring()? {
13777 let mut created = Vec::with_capacity(writes.len());
13778 for write in writes {
13779 match self.write_task(write).await {
13780 Ok(id) => created.push(id),
13781 Err(error) => {
13782 let mut left: Vec<&str> = Vec::new();
13783 for id in &created {
13784 if self.delete_issue(id).await.is_err() {
13785 left.push(id.0.as_str());
13786 }
13787 }
13788 return Err(left_behind(error, &left));
13789 }
13790 }
13791 }
13792 return Ok(created);
13793 }
13794 self.create_together(writes).await
13795 }
13796
13797 fn begin_deferred_writes(&self) {
13798 if let Ok(mut deferred) = self.deferred.lock() {
13799 deferred.get_or_insert_with(Deferred::default);
13800 }
13801 }
13802
13803 async fn finish_writes(&self) -> Result<(), SourceError> {
13804 self.send_held_updates().await?;
13805 self.send_held_back().await
13806 }
13807
13808 async fn write_task_with_assets(
13809 &self,
13810 write: &ItemWrite<Task>,
13811 _answers: Option<&BTreeMap<String, Value>>,
13812 assets: &onetaskgraph_plugin_api::AssetWrite,
13813 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
13814 self.write_task_assets(write, Some(assets)).await
13815 }
13816
13817 async fn write_project(&self, write: &ItemWrite<Project>) -> Result<NativeId, SourceError> {
13818 self.write_item(
13819 &Incoming {
13820 written: Written::Work(ItemKind::Project, &write.item.status),
13821 title: &write.item.title,
13822 content: write.item.content.as_deref(),
13823 assets: None,
13824 labels: &write.item.labels,
13825 metadata: &write.item.metadata,
13826 repositories: &write.item.repositories,
13827 classification: write.item.classification,
13828 parent: None,
13829 delivers: &[],
13830 delivered_by: &[],
13831 priority: None,
13832 },
13833 write.target.as_ref(),
13834 &write.depends_on,
13835 )
13836 .await
13837 .map(|written| written.id)
13838 }
13839
13840 /// Create or update one document, which is one issue titled the way this board spells
13841 /// a document.
13842 ///
13843 /// Everything else is exactly a task write: caller metadata goes to the same canonical
13844 /// JSON slot at the end of the body and comes back with its JSON types intact, a key
13845 /// or a field this board cannot carry is refused by name rather than dropped, a target
13846 /// naming an issue this board does not hold is refused rather than created, and an
13847 /// issue this call created is taken back when the rest of the write fails.
13848 async fn write_document(&self, write: &ItemWrite<Document>) -> Result<NativeId, SourceError> {
13849 self.write_document_assets(write, None)
13850 .await
13851 .map(|written| written.id)
13852 }
13853
13854 async fn write_document_with_assets(
13855 &self,
13856 write: &ItemWrite<Document>,
13857 _answers: Option<&BTreeMap<String, Value>>,
13858 assets: &onetaskgraph_plugin_api::AssetWrite,
13859 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
13860 self.write_document_assets(write, Some(assets)).await
13861 }
13862
13863 /// Refused exactly as the write refuses it, from what the write reads: the mapping first,
13864 /// which reads nothing; then the board's `Status` option. Over an existing item that is
13865 /// read off the item, as the write reads it, and the item is held among this command's
13866 /// resolved records so the write that follows reuses that read rather than repeating it;
13867 /// an item that does not carry the field takes the board's fields, which are held once
13868 /// read. A create is checked against the board's fields only when this command already
13869 /// holds them, because a create reads them together with its repository, in one request,
13870 /// and refuses a missing option before it writes anything.
13871 async fn check_status_write(
13872 &self,
13873 kind: ItemKind,
13874 category: StatusCategory,
13875 target: Option<&NativeId>,
13876 ) -> Result<(), SourceError> {
13877 let status = self.resolved_target(kind, category)?;
13878 if status.option().is_none() {
13879 return Ok(());
13880 }
13881 let fields = match target {
13882 Some(target) => {
13883 // A target this board does not hold is the write's own refusal to make.
13884 let Some(item) = self.bound_item(target).await? else {
13885 return Ok(());
13886 };
13887 self.resolved_cache()?.insert(target.clone(), item.clone());
13888 // Only the `Status` field decides whether the write can land.
13889 self.fields_for(Some(&item), false, true, false)
13890 .await?
13891 .fields
13892 }
13893 None => {
13894 let held = self
13895 .board_cache()?
13896 .as_ref()
13897 .map(|board| board.fields.clone());
13898 match held.or_else(|| {
13899 self.fields_cache()
13900 .ok()
13901 .and_then(|cache| cache.as_ref().map(|board| board.fields.clone()))
13902 }) {
13903 Some(fields) => fields,
13904 None => return Ok(()),
13905 }
13906 }
13907 };
13908 self.column_for(&fields, kind, category, &status)
13909 .map(|_| ())
13910 }
13911
13912 /// What a write of `status` reads back as: the `Status` option its category maps to for
13913 /// `kind`, and for a closing category the closure that option and its fixed reason read
13914 /// as — exactly what [`BoardStatuses::status`] reports of the item once written. A category
13915 /// this instance has no option for is answered as given, since writing it is refused.
13916 fn status_as_written(&self, kind: ItemKind, status: &Status) -> Status {
13917 match self.statuses.target(kind, status.category) {
13918 StatusTarget::Column(name) => {
13919 self.statuses.status(kind, Some(name.as_str()), false, None)
13920 }
13921 StatusTarget::Terminal(name, reason) => {
13922 self.statuses
13923 .status(kind, Some(name.as_str()), true, Some(reason.reason()))
13924 }
13925 StatusTarget::Disabled(_) => status.clone(),
13926 }
13927 }
13928
13929 /// Set one task's status alone.
13930 ///
13931 /// An open target reopens a closed issue with an `updateIssue` carrying only its
13932 /// `stateInput`, then selects the board option with `updateProjectV2ItemFieldValue`; a
13933 /// terminal target selects its mapped option, then closes with its fixed reason. No
13934 /// request carries a title, a body or a label. The status
13935 /// answered is what [`BoardStatuses::status`] reads off the state just written, which is
13936 /// what a re-read reports.
13937 async fn set_task_status(
13938 &self,
13939 id: &NativeId,
13940 category: StatusCategory,
13941 ) -> Result<Option<Status>, SourceError> {
13942 self.set_status(id, ItemKind::Task, category).await
13943 }
13944
13945 /// Set one task's status and answer the task with it, reading the item once: the read the
13946 /// task is answered from is handed to the status write, which would otherwise read the
13947 /// same issue again — two requests on a fresh process, the read and the field write,
13948 /// where the default's `get_task` before `set_task_status` sent three.
13949 async fn set_task_status_reading(
13950 &self,
13951 id: &NativeId,
13952 category: StatusCategory,
13953 ) -> Result<Option<Task>, SourceError> {
13954 // Refused before anything is read, in the words the write itself refuses it.
13955 self.resolved_target(ItemKind::Task, category)?;
13956 let Some(item) = self
13957 .bound_item(id)
13958 .await?
13959 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
13960 else {
13961 return Ok(None);
13962 };
13963 let mut task = item.task()?;
13964 self.held_cache
13965 .lock()
13966 .map_err(|_| SourceError::Unavailable {
13967 message: "items read by issue reference were left inconsistent; run the \
13968 command again"
13969 .into(),
13970 })?
13971 .insert(id.clone(), item);
13972 let Some(status) = self.set_status(id, ItemKind::Task, category).await? else {
13973 return Ok(None);
13974 };
13975 task.status = status;
13976 Ok(Some(task))
13977 }
13978
13979 /// Set one project's status alone, by exactly the paired write a task's status takes, for
13980 /// the project kind: a terminal category selects the project kind's mapped option and then
13981 /// closes the issue with the category's fixed reason — an issue already closed with a
13982 /// reason that reads that category is left as it is — and an open category reopens a closed
13983 /// issue and then selects its option. A board draft is refused a terminal category as a
13984 /// task's is.
13985 async fn set_project_status(
13986 &self,
13987 id: &NativeId,
13988 category: StatusCategory,
13989 ) -> Result<Option<Status>, SourceError> {
13990 self.set_status(id, ItemKind::Project, category).await
13991 }
13992
13993 /// Every task and project whose card contradicts its issue's closure, read through the
13994 /// board's own `is:closed` filter; see [`TaskSource::status_contradictions`].
13995 async fn status_contradictions(&self) -> Result<Vec<StatusContradiction>, SourceError> {
13996 self.contradictions().await
13997 }
13998
13999 /// What this board holds for each issue reference, one request per fifty of them; see
14000 /// [`TaskSource::issue_holdings`].
14001 async fn issue_holdings(&self, issues: &[IssueRef]) -> Result<Vec<IssueHolding>, SourceError> {
14002 self.holdings(issues).await
14003 }
14004
14005 /// Set one task's priority alone: one `updateProjectV2ItemFieldValue` selecting the
14006 /// mapped option of the board's `Priority` field, or one `clearProjectV2ItemFieldValue`
14007 /// for `none`. Refused by an instance with no `priority_mapping`.
14008 async fn set_task_priority(
14009 &self,
14010 id: &NativeId,
14011 priority: Priority,
14012 ) -> Result<Option<Priority>, SourceError> {
14013 self.set_priority(id, priority).await
14014 }
14015
14016 /// Replace one task's content with a single body update that keeps the metadata slot
14017 /// byte for byte.
14018 async fn set_task_content(
14019 &self,
14020 id: &NativeId,
14021 content: &str,
14022 ) -> Result<Option<()>, SourceError> {
14023 self.replace_content(id, content).await
14024 }
14025
14026 /// Replace one task issue's content and its provenance slot entry with a single body
14027 /// update. The answers are not kept: see `replace_rendering`.
14028 async fn set_task_rendering(
14029 &self,
14030 id: &NativeId,
14031 content: &str,
14032 provenance: &Value,
14033 _answers: &BTreeMap<String, Value>,
14034 ) -> Result<Option<()>, SourceError> {
14035 self.replace_rendering(
14036 id,
14037 BoardKind::Work(ItemKind::Task),
14038 content,
14039 provenance,
14040 None,
14041 )
14042 .await
14043 .map(|written| written.map(|_| ()))
14044 }
14045
14046 /// Replace one design-document issue's content and its provenance slot entry, on exactly
14047 /// the terms of [`set_task_rendering`](TaskSource::set_task_rendering).
14048 async fn set_document_rendering(
14049 &self,
14050 id: &NativeId,
14051 content: &str,
14052 provenance: &Value,
14053 _answers: &BTreeMap<String, Value>,
14054 ) -> Result<Option<()>, SourceError> {
14055 self.replace_rendering(id, BoardKind::Document, content, provenance, None)
14056 .await
14057 .map(|written| written.map(|_| ()))
14058 }
14059
14060 /// Replace one project issue's content and its provenance slot entry, on exactly the
14061 /// terms of [`set_task_rendering`](TaskSource::set_task_rendering).
14062 async fn set_project_rendering(
14063 &self,
14064 id: &NativeId,
14065 content: &str,
14066 provenance: &Value,
14067 _answers: &BTreeMap<String, Value>,
14068 ) -> Result<Option<()>, SourceError> {
14069 self.replace_rendering(
14070 id,
14071 BoardKind::Work(ItemKind::Project),
14072 content,
14073 provenance,
14074 None,
14075 )
14076 .await
14077 .map(|written| written.map(|_| ()))
14078 }
14079
14080 /// Apply a targeted update with one read of the item and a write only for what differs:
14081 /// the `Status` and `Priority` field writes in one request, the `blockedBy` difference,
14082 /// and last one `updateIssue` for title, body and state. See `targeted_update`.
14083 async fn update_task(
14084 &self,
14085 id: &NativeId,
14086 update: &TaskUpdate,
14087 ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
14088 self.targeted_update(id, update).await
14089 }
14090
14091 /// Replace one task's `delivered_by` with a single body update that changes the
14092 /// metadata slot and nothing outside it.
14093 async fn set_delivered_by(
14094 &self,
14095 id: &NativeId,
14096 delivered_by: &[TaskRef],
14097 ) -> Result<Option<()>, SourceError> {
14098 self.replace_delivered_by(id, delivered_by).await
14099 }
14100
14101 /// Set one key of one task issue's metadata with a single body update that changes the
14102 /// metadata slot and nothing outside it — no title, label, state or board field request —
14103 /// and sends nothing when the task already holds that value under the key.
14104 async fn set_task_metadata(
14105 &self,
14106 id: &NativeId,
14107 key: &MetadataKey,
14108 value: &Value,
14109 ) -> Result<Option<Task>, SourceError> {
14110 Ok(self
14111 .set_slot_key(id, BoardKind::Work(ItemKind::Task), key, value)
14112 .await?
14113 .map(|item| item.task())
14114 .transpose()?)
14115 }
14116
14117 /// Set one key of one project issue's metadata, on exactly the terms of
14118 /// [`set_task_metadata`](TaskSource::set_task_metadata).
14119 async fn set_project_metadata(
14120 &self,
14121 id: &NativeId,
14122 key: &MetadataKey,
14123 value: &Value,
14124 ) -> Result<Option<Project>, SourceError> {
14125 Ok(self
14126 .set_slot_key(id, BoardKind::Work(ItemKind::Project), key, value)
14127 .await?
14128 .map(|item| item.project()))
14129 }
14130
14131 /// Set one key of one design-document issue's metadata, on exactly the terms of
14132 /// [`set_task_metadata`](TaskSource::set_task_metadata).
14133 async fn set_document_metadata(
14134 &self,
14135 id: &NativeId,
14136 key: &MetadataKey,
14137 value: &Value,
14138 ) -> Result<Option<Document>, SourceError> {
14139 Ok(self
14140 .set_slot_key(id, BoardKind::Document, key, value)
14141 .await?
14142 .map(|item| item.document()))
14143 }
14144
14145 async fn delete_task(&self, id: &NativeId) -> Result<(), SourceError> {
14146 self.delete_item(id).await
14147 }
14148
14149 async fn delete_project(&self, id: &NativeId) -> Result<(), SourceError> {
14150 self.delete_item(id).await
14151 }
14152
14153 async fn delete_document(&self, id: &NativeId) -> Result<(), SourceError> {
14154 self.delete_item(id).await
14155 }
14156
14157 /// One page of the task issue's own comments, walked by GitHub's own cursor.
14158 ///
14159 /// Nothing here filters, so nothing has to be read ahead of the page: the caller's limit is
14160 /// the page GitHub is asked for and GitHub's `endCursor` is the cursor handed back.
14161 ///
14162 /// One request, [`graphql::ISSUE_DETAIL`]: the read that says the id names a task of this
14163 /// board is the read of its comments. A draft this process already resolved is refused
14164 /// without one.
14165 async fn task_comments(
14166 &self,
14167 task: &NativeId,
14168 page: &PageRequest,
14169 ) -> Result<Option<Page<Comment>>, SourceError> {
14170 validate_page(page)?;
14171 let cached = self.resolved_cache()?.get(task).cloned();
14172 if let Some(item) = cached {
14173 if item.kind != BoardKind::Work(ItemKind::Task) {
14174 return Ok(None);
14175 }
14176 if item.content_kind == ContentKind::DraftIssue {
14177 return Err(self.draft_has_no_comments(task));
14178 }
14179 }
14180 match self.issue_detail(task, page).await? {
14181 Some(TaskDetailRead {
14182 comments: Some(comments),
14183 ..
14184 }) => comments,
14185 _ => Ok(None),
14186 }
14187 }
14188
14189 /// Every id's task, with the first page of its comments when `comments` names it:
14190 /// [`DETAIL_BATCH`] items per [`graphql::ISSUE_DETAILS`] request, and one item with its
14191 /// comments in one [`graphql::ISSUE_DETAIL`] request.
14192 async fn get_task_details(
14193 &self,
14194 ids: &[NativeId],
14195 comments: Option<&PageRequest>,
14196 ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
14197 if let Some(page) = comments
14198 && let Err(error) = validate_page(page)
14199 {
14200 return ids.iter().map(|_| Err(error.clone())).collect();
14201 }
14202 match (ids, comments) {
14203 ([id], Some(page)) => vec![self.issue_detail(id, page).await],
14204 ([id], None) => vec![self.task_read(id).await],
14205 _ => self.issue_details(ids, comments).await,
14206 }
14207 }
14208
14209 /// Every id's task with the first page of its comments and its `blockedBy`, as a copy reads
14210 /// the tasks it carries: [`COPY_DETAIL_BATCH`] items per [`graphql::ISSUE_COPY_DETAILS`]
14211 /// request, so the dependency read a copy makes of each next is answered from this one.
14212 async fn get_task_details_for_copy(
14213 &self,
14214 ids: &[NativeId],
14215 comments: &PageRequest,
14216 ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
14217 // A page past the first is a page of one task's comments, which the detail read of it
14218 // alone answers.
14219 if comments.cursor.is_some() {
14220 return self.get_task_details(ids, Some(comments)).await;
14221 }
14222 if let Err(error) = validate_page(comments) {
14223 return ids.iter().map(|_| Err(error.clone())).collect();
14224 }
14225 let mut read = Vec::with_capacity(ids.len());
14226 for batch in ids.chunks(COPY_DETAIL_BATCH) {
14227 match self
14228 .graphql(
14229 graphql::ISSUE_COPY_DETAILS,
14230 copy_detail_batch(batch, comments, false),
14231 )
14232 .await
14233 {
14234 Ok(data) => {
14235 for (slot, id) in batch.iter().enumerate() {
14236 let read_one = match data.get(format!("i{slot}")) {
14237 Some(node) => self.detail_of(id, node, true, None).await,
14238 None => Err(SourceError::Malformed {
14239 message: format!(
14240 "GitHub answered a batch read with no item for {}",
14241 id.0
14242 ),
14243 }),
14244 };
14245 read.push(read_one);
14246 }
14247 }
14248 Err(error) if unresolvable_node(&error) => {
14249 for id in batch {
14250 read.push(self.issue_detail(id, comments).await);
14251 }
14252 }
14253 Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
14254 }
14255 }
14256 read
14257 }
14258
14259 /// Every id's item, whatever kind it is, read [`COPY_DETAIL_BATCH`] at a time with
14260 /// [`graphql::ISSUE_COPY_DETAILS`] — the document a copy reads the tasks it carries with —
14261 /// and held for the next read of each by id; see `read_ahead`.
14262 ///
14263 /// What this run wrote is never read: its own record answers, as it does for a read by id.
14264 /// Nor is an item a listing of its project carried whole earlier in this command — see
14265 /// `carried` — which is held in place of reading it.
14266 /// A batch GitHub refuses because one of its ids resolves to no node at all holds nothing,
14267 /// so each of its ids is read on its own and answered as that read answers; a draft, and an
14268 /// answer naming another issue than the one asked for, are left to their own reads too.
14269 async fn read_ahead_for_copy(&self, ids: &[NativeId]) -> Result<(), SourceError> {
14270 // The origins asked just before go out now, beside the members of the one project this
14271 // names when it names one; a project those origins answered whole is held for its next
14272 // read by id rather than read again here.
14273 let hinted = match ids {
14274 [one] => Some(one),
14275 _ => None,
14276 };
14277 self.send_pending_origins(hinted).await?;
14278 if let Some(project) = hinted {
14279 let answered = self.resolved_cache()?.get(project).cloned();
14280 if let Some(answered) = answered
14281 && answered.kind == BoardKind::Work(ItemKind::Project)
14282 {
14283 self.read_ahead()?.insert(project.clone(), Some(answered));
14284 return Ok(());
14285 }
14286 }
14287 let mut unread: Vec<NativeId> = Vec::new();
14288 // Read whole by a listing of its project in this command, which a mutation naming it
14289 // would have dropped: held as a batch would have held it, once every batch is answered.
14290 let mut listed: Vec<NativeId> = Vec::new();
14291 {
14292 let created = self.created()?;
14293 let held = self.read_ahead()?;
14294 let carried = self.carried()?;
14295 for id in ids {
14296 if unread.contains(id)
14297 || listed.contains(id)
14298 || held.contains_key(id)
14299 || created.iter().any(|own| own.id == *id)
14300 {
14301 continue;
14302 }
14303 // An issue a listing carried without the whole of what blocks it is read in a
14304 // batch too, which carries that, rather than having its dependency read sent
14305 // on its own once the copy reconciles its edges.
14306 let whole = carried.get(id).is_some_and(|item| {
14307 item.content_kind != ContentKind::Issue || item.blocked_by.is_some()
14308 });
14309 if whole {
14310 listed.push(id.clone());
14311 } else {
14312 unread.push(id.clone());
14313 }
14314 }
14315 }
14316 // Nothing of an item's comments is wanted here: the smallest page keeps the read to
14317 // what the item itself costs.
14318 let comments = PageRequest {
14319 cursor: None,
14320 limit: 1,
14321 };
14322 // Held only once every batch has been answered, so a read-ahead that fails part way
14323 // holds nothing — what a listing carried included, which stays carried — and every
14324 // item is read on its own, as the method promises.
14325 let mut read: Vec<(NativeId, Option<Resolved>)> = Vec::new();
14326 // The issues the first id names as sub-issues, as `ISSUE_COPY_DETAILS`'s `children`
14327 // answered them: what a project copy's orphan check asks, when that first id is its
14328 // project.
14329 let mut children: Option<(NativeId, Vec<(String, String)>)> = None;
14330 for (index, batch) in unread.chunks(COPY_DETAIL_BATCH).enumerate() {
14331 let data = match self
14332 .graphql(
14333 graphql::ISSUE_COPY_DETAILS,
14334 copy_detail_batch(batch, &comments, index == 0),
14335 )
14336 .await
14337 {
14338 Ok(data) => data,
14339 Err(error) if unresolvable_node(&error) => continue,
14340 Err(error) => return Err(error),
14341 };
14342 for (slot, id) in batch.iter().enumerate() {
14343 let Some(node) = data.get(format!("i{slot}")) else {
14344 continue;
14345 };
14346 let held = if node.is_null() {
14347 None
14348 } else {
14349 match optional_str(node, "__typename")? {
14350 Some("DraftIssue") => continue,
14351 Some("Issue") if required_str(node, "id")? != id.0 => continue,
14352 _ => {}
14353 }
14354 // Remembered below with the rest, so a later batch refused leaves no
14355 // dependency read answered from this one either.
14356 self.resolve_issue_as(node, false).await?
14357 };
14358 read.push((id.clone(), held));
14359 }
14360 if index == 0
14361 && let Some(node) = data.get("children").filter(|node| !node.is_null())
14362 && node.get("id").and_then(Value::as_str) == batch.first().map(|id| id.0.as_str())
14363 && node.pointer("/subIssues/pageInfo/hasNextPage") == Some(&Value::Bool(false))
14364 {
14365 // A list GitHub did not answer is not an empty one: it would answer the project's
14366 // task listing with nothing, and the orphan check with no orphans.
14367 let listed = node
14368 .pointer("/subIssues/nodes")
14369 .and_then(Value::as_array)
14370 .ok_or_else(|| SourceError::Malformed {
14371 message: format!(
14372 "GitHub answered the sub-issues of project {} with no list of them",
14373 batch[0].0
14374 ),
14375 })?
14376 .iter()
14377 .map(|child| {
14378 Ok((
14379 required_str(child, "id")?.to_owned(),
14380 required_str(child, "title")?.to_owned(),
14381 ))
14382 })
14383 .collect::<Result<Vec<_>, SourceError>>()?;
14384 children = Some((batch[0].clone(), listed));
14385 }
14386 }
14387 // The project's tasks are known from this read alone when every child it named is held
14388 // from it, but for a document, which its title says is one and which a task listing
14389 // never answers: then a listing of the project's tasks is answered from them.
14390 if let Some((project, listed)) = children
14391 && read.iter().any(|(id, held)| {
14392 *id == project
14393 && held
14394 .as_ref()
14395 .is_some_and(|held| held.kind == BoardKind::Work(ItemKind::Project))
14396 })
14397 {
14398 let mut tasks = Vec::new();
14399 let mut whole = true;
14400 for (child, title) in &listed {
14401 match read.iter().find(|(id, _)| id.0 == *child) {
14402 Some((_, Some(held))) if held.parent.as_ref() == Some(&project) => {
14403 if held.kind == BoardKind::Work(ItemKind::Task) {
14404 tasks.push(held.clone());
14405 }
14406 }
14407 _ if title.starts_with(DESIGN_TITLE_PREFIX) => {}
14408 _ => whole = false,
14409 }
14410 }
14411 if whole {
14412 self.task_children()?.insert(project, tasks);
14413 }
14414 }
14415 let mut resolved = self.resolved_cache()?;
14416 let mut ahead = self.read_ahead()?;
14417 let mut carried = self.carried()?;
14418 for item in read.iter().filter_map(|(_, held)| held.as_ref()) {
14419 resolved.insert(item.id.clone(), item.clone());
14420 }
14421 // Taken only if still carried: a mutation naming it while the batches ran dropped it,
14422 // and its next read is then its own.
14423 for id in &listed {
14424 let Some(item) = carried.remove(id) else {
14425 continue;
14426 };
14427 // The whole of what it is blocked by came with it, so the dependency read a copy
14428 // makes next is answered from this one, as a copy's detail read is.
14429 if item.blocked_by.is_some() {
14430 resolved.insert(item.id.clone(), item.clone());
14431 }
14432 read.push((id.clone(), Some(item)));
14433 }
14434 for (id, held) in read {
14435 ahead.insert(id, held);
14436 }
14437 Ok(())
14438 }
14439
14440 /// Add one comment to the task's issue, as the account the token belongs to.
14441 ///
14442 /// The author is refused before anything is sent — not even the task is read — because
14443 /// no answer GitHub could give would make posting under another name than the one asked
14444 /// for the right outcome.
14445 async fn add_comment(
14446 &self,
14447 task: &NativeId,
14448 comment: &NewComment,
14449 ) -> Result<Option<Comment>, SourceError> {
14450 if let Some(author) = &comment.author {
14451 return Err(SourceError::Refused {
14452 message: format!(
14453 "source {} cannot post a comment as {author:?}: GitHub records the account \
14454 the token signs in as the author of every comment; next: leave --author \
14455 out, and the comment is posted as that account",
14456 self.name
14457 ),
14458 });
14459 }
14460 let Some(issue) = self.commented_issue(task).await? else {
14461 return Ok(None);
14462 };
14463 let data = self
14464 .graphql(
14465 graphql::ADD_COMMENT,
14466 json!({"input":{"subjectId":issue.0,"body":comment.body.as_str()}}),
14467 )
14468 .await?;
14469 let subject = data
14470 .pointer("/addComment/subject")
14471 .filter(|value| !value.is_null())
14472 .ok_or_else(|| SourceError::Malformed {
14473 message: "GitHub comment addition returned no subject".into(),
14474 })?;
14475 if required_str(subject, "id")? != issue.0 {
14476 return Err(SourceError::Malformed {
14477 message: "GitHub comment addition answered about another issue".into(),
14478 });
14479 }
14480 let added = data
14481 .pointer("/addComment/commentEdge/node")
14482 .filter(|value| !value.is_null())
14483 .ok_or_else(|| SourceError::Malformed {
14484 message: "GitHub comment addition returned no comment".into(),
14485 })?;
14486 let added = comment_from(added)?;
14487 self.remember_commented(&issue)?;
14488 Ok(Some(added))
14489 }
14490
14491 async fn edit_comment(
14492 &self,
14493 task: &NativeId,
14494 comment: &NativeId,
14495 body: &CommentBody,
14496 ) -> Result<Option<Comment>, SourceError> {
14497 let Some(issue) = self.commented_issue(task).await? else {
14498 return Ok(None);
14499 };
14500 let Some(held) = self.comment_on(&issue, comment).await? else {
14501 return Ok(None);
14502 };
14503 // A comment a copy wrote keeps its origin through an edit: its marker is carried over.
14504 let body = CommentOrigin::kept(&held, body)?;
14505 let data = self
14506 .graphql(
14507 graphql::UPDATE_COMMENT,
14508 json!({"input":{"id":comment.0,"body":body.as_str()}}),
14509 )
14510 .await?;
14511 let edited = data
14512 .pointer("/updateIssueComment/issueComment")
14513 .filter(|value| !value.is_null())
14514 .ok_or_else(|| SourceError::Malformed {
14515 message: "GitHub comment update returned no comment".into(),
14516 })?;
14517 let edited = comment_from(edited)?;
14518 if edited.id != *comment {
14519 return Err(SourceError::Malformed {
14520 message: "GitHub comment update returned the wrong comment".into(),
14521 });
14522 }
14523 self.remember_commented(&issue)?;
14524 Ok(Some(edited))
14525 }
14526
14527 async fn delete_comment(
14528 &self,
14529 task: &NativeId,
14530 comment: &NativeId,
14531 ) -> Result<Option<NativeId>, SourceError> {
14532 let Some(issue) = self.commented_issue(task).await? else {
14533 return Ok(None);
14534 };
14535 if !self.comment_is_on(&issue, comment).await? {
14536 return Ok(None);
14537 }
14538 let data = self
14539 .graphql(graphql::DELETE_COMMENT, json!({"input":{"id":comment.0}}))
14540 .await?;
14541 // The payload says nothing about the comment it removed, so what is checked is that
14542 // GitHub answered the mutation at all rather than leaving it unanswered.
14543 data.get("deleteIssueComment")
14544 .filter(|value| !value.is_null())
14545 .ok_or_else(|| SourceError::Malformed {
14546 message: "GitHub comment deletion returned no payload".into(),
14547 })?;
14548 Ok(Some(comment.clone()))
14549 }
14550
14551 /// Every request this source has recorded, and what each of GitHub's two budgets was
14552 /// attributed — read off the same accounting the session report is rendered from, so
14553 /// the two cannot count one request two ways.
14554 async fn metering(&self) -> Result<Option<Metering>, SourceError> {
14555 Ok(Some(self.ledger.snapshot().metering()))
14556 }
14557
14558 /// Drop every item, search answer and board read this source holds, so the next command
14559 /// reads the board as a person has since left it.
14560 ///
14561 /// Every one of those is held on the assumption that nothing but this source writes the
14562 /// board while a command runs, which stops being true the moment the command is over: a
14563 /// body a person edited would be overwritten from the record held here, and a card they
14564 /// moved would be read as still where this source left it. The board's own field
14565 /// definitions go too, because a person can add or delete a `Status` option and a write
14566 /// resolved against the held list would not re-read on a miss. What stays is what stays
14567 /// valid in normal use: each repository's node id, which a miss re-reads, the pacing of
14568 /// mutations, which is about GitHub's limiter rather than anybody's work, and the running
14569 /// accounting [`metering`](TaskSource::metering) answers from.
14570 ///
14571 /// Infallible in practice: a lock an earlier failure poisoned is cleared rather than
14572 /// refused, because clearing it is what puts it right.
14573 async fn end_command(&self) -> Result<(), SourceError> {
14574 fn clear<T: Default>(held: &Mutex<T>) {
14575 *held
14576 .lock()
14577 .unwrap_or_else(std::sync::PoisonError::into_inner) = T::default();
14578 held.clear_poison();
14579 }
14580 clear(&self.created);
14581 clear(&self.updated);
14582 clear(&self.commented);
14583 clear(&self.board_cache);
14584 clear(&self.search_cache);
14585 clear(&self.narrowed_cache);
14586 clear(&self.search_next);
14587 clear(&self.resolved_cache);
14588 clear(&self.read_ahead);
14589 clear(&self.pending_origins);
14590 clear(&self.member_classes);
14591 clear(&self.carried);
14592 clear(&self.held_cache);
14593 clear(&self.children_cache);
14594 clear(&self.task_children);
14595 clear(&self.written);
14596 clear(&self.fields_cache);
14597 self.expected.end_command();
14598 Ok(())
14599 }
14600}
14601
14602/// One member of a project as a classification-only read answers it: its classification and
14603/// the repositories its work changes.
14604type MemberClass = (Classification, Vec<Repository>);
14605
14606/// Each project's sub-issues as one command read them, keyed by the selector they were asked
14607/// for under, beside the project that selector named; see `children_cache`.
14608type ProjectChildren = BTreeMap<NativeId, (NativeId, Vec<Resolved>)>;
14609
14610/// One issue comment as the contract carries it.
14611///
14612/// `author` is absent both when GitHub answers `null` for an account that no longer exists
14613/// and when it answers an actor with no login, because either way the source did not say who
14614/// wrote it — which is what an absent author means, rather than an author called nothing.
14615///
14616/// A comment a copy wrote here carries its origin as the hidden marker at the end of its body
14617/// — GitHub sets every comment's author and times itself, so there is nowhere else to keep it —
14618/// and is read back with the marker taken out and reported as its origin.
14619fn comment_from(value: &Value) -> Result<Comment, SourceError> {
14620 Ok(Comment {
14621 origin: None,
14622 id: NativeId(required_str(value, "id")?.to_owned()),
14623 author: optional_str(value.get("author").unwrap_or(&Value::Null), "login")?
14624 .map(str::to_owned),
14625 created_at: optional_time(value, "createdAt")?,
14626 updated_at: optional_time(value, "updatedAt")?,
14627 body: required_str(value, "body")?.to_owned(),
14628 url: optional_str(value, "url")?.map(str::to_owned),
14629 }
14630 .unmarked())
14631}
14632
14633/// The page of comments one issue node carries, resumed from `after`.
14634fn comment_page(
14635 node: &Value,
14636 issue: &str,
14637 after: Option<&str>,
14638) -> Result<Page<Comment>, SourceError> {
14639 let connection = node
14640 .get("comments")
14641 .filter(|value| !value.is_null())
14642 .ok_or_else(|| SourceError::Malformed {
14643 message: format!("GitHub issue {issue} answered with no comments connection"),
14644 })?;
14645 let items = optional_nodes(Some(connection), "issue comments")?
14646 .into_iter()
14647 .flatten()
14648 .map(comment_from)
14649 .collect::<Result<Vec<_>, _>>()?;
14650 let next = next_cursor(connection)?;
14651 if let Some(next) = &next {
14652 validate_cursor_progress(after, &next.0)?;
14653 }
14654 Ok(Page { items, next })
14655}
14656
14657/// The far ends of an issue's whole `blockedBy` connection, when the read carried it to its
14658/// end — `None` when it carried none, or a page with more past it.
14659fn carried_blocked_by(content: &Value) -> Result<Option<Vec<Value>>, SourceError> {
14660 let Some(connection) = content.get("blockedBy").filter(|value| !value.is_null()) else {
14661 return Ok(None);
14662 };
14663 if next_cursor(connection)?.is_some() {
14664 return Ok(None);
14665 }
14666 Ok(Some(
14667 optional_nodes(Some(connection), "blocked-by issues")?
14668 .into_iter()
14669 .flatten()
14670 .cloned()
14671 .collect(),
14672 ))
14673}
14674
14675/// Where the recorded tail of a dependency walk resumes; see
14676/// [`GitHubProjectsSource::recorded_edges`].
14677const RECORDED_CURSOR: &str = "onetaskgraph.depends_on:";
14678
14679/// The board text field this source keeps a copy's origin in.
14680///
14681/// Named after the key it holds, and held to that name by the guard below rather than by
14682/// a reader noticing.
14683const ORIGIN_FIELD: &str = "onetaskgraph.origin";
14684
14685/// The metadata key that field holds.
14686///
14687/// The engine owns this key and spells it once as `GlobalId::ORIGIN_KEY`; a plugin never
14688/// constructs or interprets the qualified id it carries. This source names it only to
14689/// route it — a short, typed value belongs in a typed field rather than in the body slot
14690/// a caller's own prose shares.
14691///
14692/// Restated rather than imported, because no plugin crate may depend on the engine. What
14693/// keeps the two spellings one contract is `scripts/check-origin-key-spelling.sh`, a
14694/// target in `check`: it reads the engine's own literal and fails naming the file and the
14695/// line when a plugin's parts from it either way. Drift here has one symptom — a copy
14696/// that creates a second item every run instead of finding the one it wrote — and that is
14697/// too late to learn it.
14698const ORIGIN_KEY: &str = "onetaskgraph.origin";
14699
14700/// Where a recorded tail resumes, refusing a cursor no walk in `direction` reported.
14701///
14702/// The reserved key holds forward edges and nothing else — the reverse of a recorded edge
14703/// is derived from the far end, never written down on the near item — so only a forward
14704/// walk ever reports one of these cursors. A reverse read carrying one is resuming a walk
14705/// it did not come from, and it is told so rather than answered with an empty page that
14706/// reads as a walk which ended.
14707fn recorded_offset(
14708 cursor: Option<&str>,
14709 direction: Direction,
14710) -> Result<Option<usize>, SourceError> {
14711 cursor
14712 .and_then(|cursor| cursor.strip_prefix(RECORDED_CURSOR))
14713 .map(|offset| {
14714 if direction != Direction::DependsOn {
14715 return Err(SourceError::Config {
14716 message: format!(
14717 "{RECORDED_CURSOR}{offset} resumes recorded forward edges, which a \
14718 reverse dependency read never issues; resume it in the direction \
14719 that reported it"
14720 ),
14721 });
14722 }
14723 offset.parse().map_err(|_| SourceError::Config {
14724 message: format!("{RECORDED_CURSOR}{offset} is not a recorded-edge cursor"),
14725 })
14726 })
14727 .transpose()
14728}
14729
14730/// What a dependency read's own answer says of a board draft: `None` when the item is not a
14731/// draft, or the answer did not carry its board item; `Some(None)` for a draft whose one board
14732/// item is not on this board, which this source does not hold; and `Some(Some(slot))`, the
14733/// draft's metadata, for one it does — its board item naming this board by its number and by
14734/// the id the same answer read of the board, as [`GitHubProjectsSource::draft_by_id`] decides.
14735fn draft_slot(
14736 data: &Value,
14737 id: &NativeId,
14738 project_number: u32,
14739) -> Result<Option<Option<BTreeMap<String, Value>>>, SourceError> {
14740 let Some(draft) = data
14741 .get("node")
14742 .filter(|node| node.get("__typename").and_then(Value::as_str) == Some("DraftIssue"))
14743 else {
14744 return Ok(None);
14745 };
14746 let Some(items) = data.pointer("/draft/projectV2Items") else {
14747 return Ok(None);
14748 };
14749 if required_str(draft, "id")? != id.0 {
14750 return Err(SourceError::Malformed {
14751 message: format!("GitHub answered a different draft for {}", id.0),
14752 });
14753 }
14754 let nodes = items
14755 .get("nodes")
14756 .and_then(Value::as_array)
14757 .ok_or_else(|| SourceError::Malformed {
14758 message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
14759 })?;
14760 let info = items
14761 .get("pageInfo")
14762 .ok_or_else(|| SourceError::Malformed {
14763 message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
14764 })?;
14765 if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
14766 return Err(SourceError::Malformed {
14767 message: format!(
14768 "GitHub draft {} reports more board items than the one GitHub links a draft to",
14769 id.0
14770 ),
14771 });
14772 }
14773 let board = data
14774 .pointer("/board/projectV2/id")
14775 .and_then(Value::as_str)
14776 .ok_or_else(|| SourceError::Malformed {
14777 message: format!(
14778 "GitHub answered draft {} without this board's own id beside it",
14779 id.0
14780 ),
14781 })?;
14782 let mut held = false;
14783 for node in nodes {
14784 let project = node.get("project").ok_or_else(|| SourceError::Malformed {
14785 message: format!("GitHub draft {} board item has no project", id.0),
14786 })?;
14787 let number = project
14788 .get("number")
14789 .and_then(Value::as_u64)
14790 .ok_or_else(|| SourceError::Malformed {
14791 message: format!(
14792 "GitHub draft {} board item has no numeric project number",
14793 id.0
14794 ),
14795 })?;
14796 let project_id = required_str(project, "id")?;
14797 held |= number == u64::from(project_number) && project_id == board;
14798 }
14799 if !held {
14800 return Ok(Some(None));
14801 }
14802 Ok(Some(Some(
14803 metadata_body(optional_str(draft, "body")?.map(str::to_owned))?.1,
14804 )))
14805}
14806
14807fn recorded_page(edges: Vec<DependencyEdge>, offset: usize, limit: usize) -> Page<DependencyEdge> {
14808 let mut page = offset_page(edges, offset, limit.max(1));
14809 page.next = page
14810 .next
14811 .map(|cursor| Cursor(format!("{RECORDED_CURSOR}{}", cursor.0)));
14812 page
14813}
14814
14815/// The kind of one issue reached through a dependency connection.
14816///
14817/// The same questions the board scan asks, over the fields the dependency document
14818/// selects, and in the same order: the design prefix first, then a sub-issue is a task,
14819/// then anything with sub-issues or the marker is a project.
14820///
14821/// # Errors
14822///
14823/// A far end this board holds as a document is refused rather than reported. The two
14824/// answers that are not refusals would both be wrong: reporting it as a task names an id
14825/// no task read of this source can find, and reporting it as a project names one no
14826/// project read can. There is no third value to return — `ItemKind` has no document
14827/// variant, because nothing may point at a document — so the relationship itself is what
14828/// the person is told about.
14829fn related_kind(value: &Value) -> Result<ItemKind, SourceError> {
14830 let id = required_str(value, "id")?;
14831 if required_str(value, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
14832 return Err(SourceError::Refused {
14833 message: format!(
14834 "GitHub issue {id} is a document of this board — its title begins \
14835 {DESIGN_TITLE_PREFIX:?} — and nothing may depend on a document or be depended \
14836 on by one; next: remove that issue's blocking relationship on this board"
14837 ),
14838 });
14839 }
14840 let parent = optional_str(value.get("parent").unwrap_or(&Value::Null), "id")?;
14841 if parent.is_some() {
14842 return Ok(ItemKind::Task);
14843 }
14844 let (_, slot) = metadata_body(optional_str(value, "body")?.map(str::to_owned))?;
14845 let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
14846 message: format!("GitHub issue {id}: {message}"),
14847 })?;
14848 let sub_issues = sub_issue_total(value)?;
14849 Ok(if sub_issues > 0 || marked == Some(ItemKind::Project) {
14850 ItemKind::Project
14851 } else {
14852 ItemKind::Task
14853 })
14854}
14855
14856/// The `stateInput` that closes an issue for `reason`.
14857fn closed_input(reason: ClosedState) -> Value {
14858 json!({"value":"CLOSED","stateReason":reason.reason()})
14859}
14860
14861/// The `IssueStateUpdateInput` one status target asks for.
14862///
14863/// `stateInput` and `state` are mutually exclusive on `UpdateIssueInput`, and only this
14864/// one is ever sent. A non-terminal status always asks for `OPEN`, which is what reopens
14865/// a currently-closed issue: without that the item would read back `Unknown` and a copy
14866/// would report a change forever. A document has no status at all, and asks for neither.
14867fn state_input(target: Option<&StatusTarget>) -> Value {
14868 match target {
14869 Some(StatusTarget::Terminal(_, reason)) => closed_input(*reason),
14870 Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => json!({"value":"OPEN"}),
14871 // A document has no status, so a write of one says nothing about the issue's open
14872 // or closed state rather than forcing it open: `stateInput` is what carries that
14873 // instruction, and an explicit null asks for no change to it.
14874 None => Value::Null,
14875 }
14876}
14877
14878/// The metadata one write stores in the item's body slot.
14879///
14880/// The typed fields travel as themselves, so the three reserved keys are rebuilt here
14881/// rather than carried: the kind marker so an empty project stays readable, the
14882/// repository list only when it is not exactly the issue's own repository, and the far
14883/// ends no relationship here can name.
14884///
14885/// The copy origin is the one typed field that is also mirrored here, and only as a
14886/// mirror: it lands in the board's origin field as well, which stays the one every reader
14887/// takes it from, and it is here so that GitHub's issue search — which indexes this comment
14888/// and catches up with a write in seconds rather than minutes — can find the item by it.
14889/// A reader of the release before this one drops the slot's copy and reads the field, so an
14890/// item written here still reads with exactly one origin there.
14891fn slot_metadata(
14892 incoming: &Incoming<'_>,
14893 own_repository: Option<&Repository>,
14894 fallback: &[DependencyEdge],
14895) -> BTreeMap<String, Value> {
14896 let mut metadata = incoming.metadata.clone();
14897 match metadata.remove(ORIGIN_KEY) {
14898 Some(Value::String(origin)) if !origin.is_empty() => {
14899 metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin));
14900 }
14901 _ => {}
14902 }
14903 match incoming.written.kind() {
14904 BoardKind::Work(kind) => metadata.insert(
14905 ItemKind::METADATA_KEY.to_owned(),
14906 Value::String(kind.marker().to_owned()),
14907 ),
14908 // A document is told by its title, so it carries no kind marker: that key names
14909 // what a dependency endpoint points at, and nothing may point at a document.
14910 BoardKind::Document => metadata.remove(ItemKind::METADATA_KEY),
14911 };
14912 incoming.classification.record(&mut metadata);
14913 let derivable = own_repository
14914 .map(|own| incoming.repositories == [own.clone()])
14915 .unwrap_or(incoming.repositories.is_empty());
14916 if derivable {
14917 metadata.remove(Repository::METADATA_KEY);
14918 } else {
14919 metadata.insert(
14920 Repository::METADATA_KEY.to_owned(),
14921 Value::Array(
14922 incoming
14923 .repositories
14924 .iter()
14925 .map(|repository| Value::String(repository.as_str().to_owned()))
14926 .collect(),
14927 ),
14928 );
14929 }
14930 // The typed lists are what land, whatever the caller's own metadata held under their
14931 // keys: a key of either name travelling beside the field would otherwise be a second
14932 // answer to the same question, and the field is the one the contract names.
14933 for (key, entries) in [
14934 (TaskRef::DELIVERS_KEY, incoming.delivers),
14935 (TaskRef::DELIVERED_BY_KEY, incoming.delivered_by),
14936 ] {
14937 set_task_list(&mut metadata, key, entries);
14938 }
14939 record_edges(&mut metadata, fallback);
14940 metadata
14941}
14942
14943/// Hold the far ends no relationship here can name under [`DependencyEdge::RECORDED_KEY`] in
14944/// one slot's metadata, or no such key when there are none.
14945fn record_edges(metadata: &mut BTreeMap<String, Value>, fallback: &[DependencyEdge]) {
14946 if fallback.is_empty() {
14947 metadata.remove(DependencyEdge::RECORDED_KEY);
14948 } else {
14949 metadata.insert(
14950 DependencyEdge::RECORDED_KEY.to_owned(),
14951 Value::Array(
14952 fallback
14953 .iter()
14954 .map(|edge| {
14955 serde_json::to_value(&edge.to).expect("an endpoint renders as JSON")
14956 })
14957 .collect(),
14958 ),
14959 );
14960 }
14961}
14962
14963/// Every label one item carries, from its content's own connection and nowhere else.
14964///
14965/// There is no second place to read one from: no document this source sends selects the
14966/// board's built-in `Labels` field, because GitHub derives it from the content and a draft
14967/// cannot carry one at all. The module documentation records the three schema facts that
14968/// settle it.
14969fn labels(content: &Value) -> Result<Vec<Label>, SourceError> {
14970 optional_nodes(content.get("labels"), "content labels")?
14971 .into_iter()
14972 .flatten()
14973 .map(|v| {
14974 Ok(Label {
14975 id: NativeId(required_str(v, "id")?.to_owned()),
14976 name: required_str(v, "name")?.to_owned(),
14977 color: optional_str(v, "color")?.map(str::to_owned),
14978 })
14979 })
14980 .collect()
14981}
14982
14983/// Each board field value one item holds, as the change token's facets name it: the field's
14984/// name, with the single-select option's name or the text. A value of a type no read here
14985/// selects, and one naming no field, says nothing a person can rename and is left out.
14986fn facet_fields(field_values: &[Value]) -> Vec<(String, String)> {
14987 field_values
14988 .iter()
14989 .filter_map(|value| {
14990 let field = value.pointer("/field/name")?.as_str()?;
14991 let held = value.get("name").or_else(|| value.get("text"))?.as_str()?;
14992 Some((field.to_owned(), held.to_owned()))
14993 })
14994 .collect()
14995}
14996
14997/// The digest of the facets of the issue `content`, whose board item's values are
14998/// `field_values` — or `None` when the response did not carry, whole, every member they are read
14999/// from: the title, the parent, the sub-issue summary and the body its kind is read from, every
15000/// label, and every field value.
15001///
15002/// What a write's own response spells its token from, by exactly the rules a read takes them by.
15003fn facets_of(content: &Value, field_values: &Value) -> Option<change_token::FacetsDigest> {
15004 let ended = |connection: &Value| {
15005 connection.pointer("/pageInfo/hasNextPage") == Some(&Value::Bool(false))
15006 };
15007 let labels = content.get("labels").filter(|labels| ended(labels))?;
15008 if !ended(field_values) {
15009 return None;
15010 }
15011 let parent = content.get("parent")?;
15012 let (_, slot) = metadata_body(content.get("body")?.as_str().map(str::to_owned)).ok()?;
15013 let kind = BoardKind::of(
15014 content.get("title")?.as_str()?,
15015 !parent.is_null(),
15016 sub_issue_total(content).ok()?,
15017 ItemKind::from_metadata(&slot).ok()?,
15018 );
15019 let labels = self::labels(&json!({"labels": labels})).ok()?;
15020 let nodes = field_values.get("nodes")?.as_array()?;
15021 Some(change_token::Facets::new(kind.facet(), facet_fields(nodes), &labels).digest())
15022}
15023
15024/// The definition of each board field one item's values are values of, in the shape a read
15025/// of the board's own `fields` gives one.
15026///
15027/// A value names its field through a fragment on that field's own type, so the type is
15028/// known from which kind of value it is: a single-select value's field is a
15029/// `ProjectV2SingleSelectField`, options and all, and a text value's is a `ProjectV2Field`.
15030/// A value whose field carried no id, or an empty one, says nothing usable and is left out.
15031fn field_definitions(field_values: &[Value]) -> Vec<Value> {
15032 field_values
15033 .iter()
15034 .filter_map(|value| {
15035 let field = value.get("field")?.as_object()?;
15036 field.get("id")?.as_str().filter(|id| !id.is_empty())?;
15037 let typename = if value.get("text").is_some() {
15038 "ProjectV2Field"
15039 } else if value.get("name").is_some() {
15040 "ProjectV2SingleSelectField"
15041 } else {
15042 return None;
15043 };
15044 let mut defined = field.clone();
15045 defined.insert("__typename".to_owned(), json!(typename));
15046 // Only a text field holds a text value, so the value says the field's type too.
15047 if typename == "ProjectV2Field" {
15048 defined.insert("dataType".to_owned(), json!("TEXT"));
15049 }
15050 Some(Value::Object(defined))
15051 })
15052 .collect()
15053}
15054
15055fn text_field(field_values: &[Value], name: &str) -> Result<Option<String>, SourceError> {
15056 let Some(node) = field_values
15057 .iter()
15058 .find(|node| node.pointer("/field/name").and_then(Value::as_str) == Some(name))
15059 else {
15060 return Ok(None);
15061 };
15062 Ok(optional_str(node, "text")?.map(str::to_owned))
15063}
15064
15065fn valid_github_owner(owner: &str) -> bool {
15066 !owner.is_empty()
15067 && owner.len() <= 39
15068 && !owner.starts_with('-')
15069 && !owner.ends_with('-')
15070 && !owner.contains("--")
15071 && owner
15072 .bytes()
15073 .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
15074}
15075
15076/// GitHub's repository-name grammar: 1-100 ASCII letters, digits, `-`, `_` or `.`, and
15077/// neither of the two names a path segment already means.
15078fn valid_github_repository_name(name: &str) -> bool {
15079 !name.is_empty()
15080 && name.len() <= 100
15081 && name != "."
15082 && name != ".."
15083 && name
15084 .bytes()
15085 .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
15086}
15087
15088fn valid_environment_name(name: &str) -> bool {
15089 let mut bytes = name.bytes();
15090 bytes
15091 .next()
15092 .is_some_and(|byte| byte.is_ascii_alphabetic() || byte == b'_')
15093 && bytes.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
15094}
15095
15096/// How many sub-issues one issue has.
15097///
15098/// `Issue.subIssuesSummary` is `SubIssuesSummary!` and its `total` is `Int!`, so an
15099/// absent or non-integer one is a response this source cannot read — and reading it as
15100/// zero would classify a project as a task, which is exactly the mistake the marker
15101/// exists to keep from happening quietly.
15102fn sub_issue_total(issue: &Value) -> Result<u64, SourceError> {
15103 let summary = issue
15104 .get("subIssuesSummary")
15105 .ok_or_else(|| SourceError::Malformed {
15106 message: "GitHub issue is missing subIssuesSummary".into(),
15107 })?;
15108 summary
15109 .get("total")
15110 .and_then(Value::as_u64)
15111 .ok_or_else(|| SourceError::Malformed {
15112 message: "GitHub issue subIssuesSummary.total is not an unsigned integer".into(),
15113 })
15114}
15115
15116/// One issue's own `number`.
15117///
15118/// An issue always has one: GitHub declares `Issue.number` as `Int!` and every selection of
15119/// an issue in this module asks for it. So a read of one that comes back without it, or
15120/// with something that is not an unsigned integer, is a response this source cannot read —
15121/// absence here is **not** "this issue has no number". A draft is the content that has
15122/// none, and a draft never reaches this: the caller decides on `__typename` first, the way
15123/// it does for `subIssuesSummary`, which `DraftIssue` equally declares nothing for.
15124fn issue_number(issue: &Value) -> Result<u64, SourceError> {
15125 issue
15126 .get("number")
15127 .and_then(Value::as_u64)
15128 .ok_or_else(|| SourceError::Malformed {
15129 message: "GitHub issue number is missing or is not an unsigned integer".into(),
15130 })
15131}
15132
15133/// The `number` a creating mutation answered with, and `None` when it answered without one;
15134/// why a missing one is tolerated is at the call in `create_and_file_issue`.
15135fn created_issue_number(created: &Value) -> Result<Option<u64>, SourceError> {
15136 match created.get("number") {
15137 None | Some(Value::Null) => Ok(None),
15138 Some(value) => value
15139 .as_u64()
15140 .map(Some)
15141 .ok_or_else(|| SourceError::Malformed {
15142 message: "GitHub created issue number is not an unsigned integer".into(),
15143 }),
15144 }
15145}
15146
15147fn required_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
15148 value
15149 .get(field)
15150 .and_then(Value::as_str)
15151 .ok_or_else(|| SourceError::Malformed {
15152 message: format!("GitHub response is missing string field {field}"),
15153 })
15154}
15155
15156fn required_nonblank_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
15157 let found = required_str(value, field)?;
15158 if found.trim().is_empty() {
15159 return Err(SourceError::Malformed {
15160 message: format!("GitHub response has blank string field {field}"),
15161 });
15162 }
15163 Ok(found)
15164}
15165
15166/// The slot's delimiters, which `docs/metadata.md` settles once for every source that
15167/// needs one — Linear spells them too, in its own description field.
15168///
15169/// Restated rather than shared, because a plugin crate depends on the contract crate and
15170/// nothing else of this workspace. `scripts/check-metadata-slot-encoding.sh`, a target in
15171/// `check`, is what keeps the two one encoding: drift is otherwise quiet, since each
15172/// source round-trips its own writes perfectly well under its own spelling.
15173const METADATA_OPEN: &str = "<!-- onetaskgraph.metadata\n";
15174const METADATA_CLOSE: &str = "\n-->";
15175
15176/// What the composer puts between a non-empty visible body and the slot, and the one thing
15177/// the parser takes off the visible body when it takes the slot off — exactly once, so every
15178/// other trailing byte of the body comes back as it was written.
15179// 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.
15180const METADATA_SEPARATOR: &str = "\n\n";
15181
15182/// The visible body and the metadata slot at the end of it.
15183///
15184/// The encoding is the one `docs/metadata.md` settles for Linear, which is where its
15185/// reasons are. Only a comment at the very end is a slot; one in the middle is a person's
15186/// own content and is left alone. The visible body is everything before the slot less the
15187/// one [`METADATA_SEPARATOR`] the composer put there, byte for byte.
15188fn metadata_body(
15189 body: Option<String>,
15190) -> Result<(Option<String>, BTreeMap<String, Value>), SourceError> {
15191 let Some(body) = body else {
15192 return Ok((None, BTreeMap::new()));
15193 };
15194 let Some(slot) = slot_span(&body)? else {
15195 return Ok((Some(body), BTreeMap::new()));
15196 };
15197 let metadata =
15198 serde_json::from_str(&body[slot.encoded_start..slot.encoded_end]).map_err(|error| {
15199 SourceError::Malformed {
15200 message: format!(
15201 "invalid canonical JSON in GitHub issue onetaskgraph metadata slot: {error}"
15202 ),
15203 }
15204 })?;
15205 let before = &body[..slot.start];
15206 let visible = before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before);
15207 Ok(((!visible.is_empty()).then(|| visible.to_owned()), metadata))
15208}
15209
15210/// Where the metadata slot sits in one body, as byte offsets into it.
15211struct SlotSpan {
15212 /// Where [`METADATA_OPEN`] begins.
15213 start: usize,
15214 /// Where the encoded JSON begins, just past [`METADATA_OPEN`].
15215 encoded_start: usize,
15216 /// Where the encoded JSON ends, at the start of [`METADATA_CLOSE`].
15217 encoded_end: usize,
15218 /// Just past [`METADATA_CLOSE`].
15219 end: usize,
15220}
15221
15222/// The slot at the very end of `body`, or `None` when it has none.
15223///
15224/// The one reading of *where the slot is*, shared by [`metadata_body`], which reads it, and
15225/// [`with_slot`], which rewrites it — so the two cannot disagree about which comment is the
15226/// slot.
15227fn slot_span(body: &str) -> Result<Option<SlotSpan>, SourceError> {
15228 let Some(start) = body.rfind(METADATA_OPEN) else {
15229 return Ok(None);
15230 };
15231 let encoded_start = start + METADATA_OPEN.len();
15232 let Some(relative_end) = body[encoded_start..].find(METADATA_CLOSE) else {
15233 return Err(SourceError::Malformed {
15234 message: "unterminated onetaskgraph metadata slot in GitHub issue body".into(),
15235 });
15236 };
15237 let encoded_end = encoded_start + relative_end;
15238 let end = encoded_end + METADATA_CLOSE.len();
15239 if !body[end..].trim().is_empty() {
15240 return Ok(None);
15241 }
15242 Ok(Some(SlotSpan {
15243 start,
15244 encoded_start,
15245 encoded_end,
15246 end,
15247 }))
15248}
15249
15250/// `body` with its metadata slot holding exactly `metadata`, and every byte outside the
15251/// slot as it was.
15252///
15253/// A slot that is there has its JSON replaced in place; one that becomes empty is removed
15254/// together with the one [`METADATA_SEPARATOR`] separating it from the prose before it. A
15255/// body with no slot gains one the way [`compose_body`] writes it — after that separator,
15256/// or alone in an empty body — and a body with no slot that is given no metadata is
15257/// returned as it is.
15258fn with_slot(body: &str, metadata: &BTreeMap<String, Value>) -> Result<String, SourceError> {
15259 let encoded = if metadata.is_empty() {
15260 None
15261 } else {
15262 Some(
15263 serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
15264 message: error.to_string(),
15265 })?,
15266 )
15267 };
15268 Ok(match (slot_span(body)?, encoded) {
15269 (Some(slot), Some(encoded)) => format!(
15270 "{}{encoded}{}",
15271 &body[..slot.encoded_start],
15272 &body[slot.encoded_end..]
15273 ),
15274 (Some(slot), None) => {
15275 let before = &body[..slot.start];
15276 format!(
15277 "{}{}",
15278 before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before),
15279 &body[slot.end..]
15280 )
15281 }
15282 (None, None) => body.to_owned(),
15283 (None, Some(encoded)) if body.is_empty() => {
15284 format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15285 }
15286 (None, Some(encoded)) => {
15287 format!("{body}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15288 }
15289 })
15290}
15291
15292/// `body` with everything before its metadata slot replaced by `content`, and the slot
15293/// itself kept byte for byte.
15294///
15295/// The inverse of how [`metadata_body`] splits a body: the slot, when there is one, follows
15296/// `content` after the one [`METADATA_SEPARATOR`] the composer puts there — or alone, when
15297/// `content` is empty — so a read of the result reports `content` as the visible body and
15298/// the slot's metadata exactly as it was.
15299fn with_content(body: &str, content: &str) -> Result<String, SourceError> {
15300 let Some(slot) = slot_span(body)? else {
15301 return Ok(content.to_owned());
15302 };
15303 let kept = &body[slot.start..];
15304 Ok(if content.is_empty() {
15305 kept.to_owned()
15306 } else {
15307 format!("{content}{METADATA_SEPARATOR}{kept}")
15308 })
15309}
15310
15311/// Hold `entries` under `key` in one slot's metadata, or no such key when there are none.
15312fn set_task_list(metadata: &mut BTreeMap<String, Value>, key: &str, entries: &[TaskRef]) {
15313 if entries.is_empty() {
15314 metadata.remove(key);
15315 } else {
15316 metadata.insert(
15317 key.to_owned(),
15318 Value::Array(
15319 entries
15320 .iter()
15321 .map(|entry| Value::String(entry.as_str().to_owned()))
15322 .collect(),
15323 ),
15324 );
15325 }
15326}
15327
15328fn compose_body(
15329 content: Option<&str>,
15330 metadata: &BTreeMap<String, Value>,
15331) -> Result<Option<String>, SourceError> {
15332 let visible = content.unwrap_or_default();
15333 if metadata.is_empty() {
15334 return Ok((!visible.is_empty()).then(|| visible.to_owned()));
15335 }
15336 let encoded = serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
15337 message: error.to_string(),
15338 })?;
15339 Ok(Some(if visible.is_empty() {
15340 format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15341 } else {
15342 format!("{visible}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15343 }))
15344}
15345
15346fn required_bool(value: &Value, field: &str) -> Result<bool, SourceError> {
15347 value
15348 .get(field)
15349 .and_then(Value::as_bool)
15350 .ok_or_else(|| SourceError::Malformed {
15351 message: format!("GitHub response is missing boolean field {field}"),
15352 })
15353}
15354fn optional_str<'a>(value: &'a Value, field: &str) -> Result<Option<&'a str>, SourceError> {
15355 match value.get(field) {
15356 None | Some(Value::Null) => Ok(None),
15357 Some(value) => value
15358 .as_str()
15359 .map(Some)
15360 .ok_or_else(|| SourceError::Malformed {
15361 message: format!("GitHub response field {field} is not a string or null"),
15362 }),
15363 }
15364}
15365fn optional_nodes<'a>(
15366 connection: Option<&'a Value>,
15367 name: &str,
15368) -> Result<Option<&'a Vec<Value>>, SourceError> {
15369 match connection {
15370 None | Some(Value::Null) => Ok(None),
15371 Some(value) => value
15372 .get("nodes")
15373 .and_then(Value::as_array)
15374 .map(Some)
15375 .ok_or_else(|| SourceError::Malformed {
15376 message: format!("GitHub {name}.nodes is not an array"),
15377 }),
15378 }
15379}
15380fn complete_connection(connection: &Value, name: &str, size: u32) -> Result<(), SourceError> {
15381 let page_info = connection
15382 .get("pageInfo")
15383 .ok_or_else(|| SourceError::Malformed {
15384 message: format!("GitHub {name} has no pageInfo"),
15385 })?;
15386 if required_bool(page_info, "hasNextPage")? {
15387 return Err(SourceError::Malformed {
15388 message: format!(
15389 "GitHub {name} exceeds the supported nested connection size of {size}"
15390 ),
15391 });
15392 }
15393 Ok(())
15394}
15395fn optional_time(value: &Value, field: &str) -> Result<Option<DateTime<Utc>>, SourceError> {
15396 optional_str(value, field)?
15397 .map(|timestamp| {
15398 timestamp.parse().map_err(|error| SourceError::Malformed {
15399 message: format!("GitHub response field {field} is not a timestamp: {error}"),
15400 })
15401 })
15402 .transpose()
15403}
15404fn validate_page(page: &PageRequest) -> Result<(), SourceError> {
15405 if page.limit == 0 {
15406 Err(SourceError::Config {
15407 message: "page limit must be at least 1".into(),
15408 })
15409 } else {
15410 Ok(())
15411 }
15412}
15413fn next_cursor(connection: &Value) -> Result<Option<Cursor>, SourceError> {
15414 let page = connection
15415 .get("pageInfo")
15416 .filter(|value| value.is_object())
15417 .ok_or_else(|| SourceError::Malformed {
15418 message: "GitHub connection is missing pageInfo".into(),
15419 })?;
15420 if required_bool(page, "hasNextPage")? {
15421 let cursor = required_str(page, "endCursor")?;
15422 validate_cursor_progress(None, cursor)?;
15423 Ok(Some(Cursor(cursor.into())))
15424 } else {
15425 Ok(None)
15426 }
15427}
15428fn validate_cursor_progress(previous: Option<&str>, next: &str) -> Result<(), SourceError> {
15429 if next.is_empty() || previous == Some(next) {
15430 Err(SourceError::Malformed {
15431 message: "GitHub pagination cursor is empty or did not advance".into(),
15432 })
15433 } else {
15434 Ok(())
15435 }
15436}
15437/// The version of this plugin's opaque narrowing-search cursor.
15438pub const SEARCH_CURSOR_VERSION: u32 = 4;
15439
15440#[derive(Serialize, Deserialize)]
15441#[serde(tag = "state", rename_all = "snake_case", deny_unknown_fields)]
15442enum SearchConnection {
15443 Initial {},
15444 Continuing { after: Cursor },
15445 Exhausted {},
15446}
15447impl SearchConnection {
15448 fn after(&self) -> Option<&str> {
15449 match self {
15450 Self::Continuing { after } => Some(&after.0),
15451 _ => None,
15452 }
15453 }
15454 fn exhausted(&self) -> bool {
15455 matches!(self, Self::Exhausted { .. })
15456 }
15457 /// Whether a cursor naming this position, `offset` rows into its page, is one this
15458 /// plugin could have handed out: a page is resumed only part of the way through it — an
15459 /// offset of a whole page or more would skip rows nobody was given — an initial page
15460 /// only once some of it was handed out, and an exhausted connection has no page to be
15461 /// part of the way through.
15462 fn valid_resume(&self, offset: usize) -> bool {
15463 let within = offset < SEARCH_PAGE_SIZE as usize;
15464 match self {
15465 Self::Initial { .. } => offset > 0 && within,
15466 Self::Continuing { after } => !after.0.is_empty() && within,
15467 Self::Exhausted { .. } => offset == 0,
15468 }
15469 }
15470}
15471
15472/// Versioned source cursor. A zero offset and empty own-write ids are omitted.
15473#[derive(Serialize, Deserialize)]
15474#[serde(deny_unknown_fields)]
15475struct SearchPosition {
15476 version: u32,
15477 connection: SearchConnection,
15478 /// How many rows of the page `connection` starts were already handed out.
15479 #[serde(default, skip_serializing_if = "is_zero")]
15480 offset: usize,
15481 #[serde(default, skip_serializing_if = "Vec::is_empty")]
15482 seen: Vec<NativeId>,
15483 #[serde(default, skip_serializing_if = "Vec::is_empty")]
15484 own: Vec<NativeId>,
15485}
15486impl Default for SearchPosition {
15487 fn default() -> Self {
15488 Self {
15489 version: SEARCH_CURSOR_VERSION,
15490 connection: SearchConnection::Initial {},
15491 offset: 0,
15492 seen: Vec::new(),
15493 own: Vec::new(),
15494 }
15495 }
15496}
15497
15498fn is_zero(offset: &usize) -> bool {
15499 *offset == 0
15500}
15501
15502fn numeric_cursor(cursor: Option<&Cursor>) -> Result<usize, SourceError> {
15503 cursor.map_or(Ok(0), |c| {
15504 c.0.parse().map_err(|_| SourceError::Config {
15505 message: "page cursor is invalid".into(),
15506 })
15507 })
15508}
15509fn offset_page<T>(mut items: Vec<T>, offset: usize, limit: usize) -> Page<T> {
15510 if offset > items.len() {
15511 return Page::last(vec![]);
15512 }
15513 let tail = items.split_off(offset);
15514 let mut selected = tail;
15515 let next = (selected.len() > limit).then(|| Cursor((offset + limit).to_string()));
15516 selected.truncate(limit);
15517 Page {
15518 items: selected,
15519 next,
15520 }
15521}
15522
15523impl GitHubProjectsSource {
15524 async fn write_task_assets(
15525 &self,
15526 write: &ItemWrite<Task>,
15527 assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
15528 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
15529 self.write_item(
15530 &self.task_incoming(write, assets)?,
15531 write.target.as_ref(),
15532 &write.depends_on,
15533 )
15534 .await
15535 }
15536
15537 /// One task write as [`Self::write_item`] takes it, once what it delivers and its priority
15538 /// are known to be ones this source can write.
15539 fn task_incoming<'a>(
15540 &self,
15541 write: &'a ItemWrite<Task>,
15542 assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
15543 ) -> Result<Incoming<'a>, SourceError> {
15544 let near = write.target.as_ref().unwrap_or(&write.item.id);
15545 for (key, entries) in [
15546 (TaskRef::DELIVERS_KEY, &write.item.delivers),
15547 (TaskRef::DELIVERED_BY_KEY, &write.item.delivered_by),
15548 ] {
15549 TaskRef::listed(key, near, Some(&self.name), entries.clone())
15550 .map_err(|message| SourceError::Refused { message })?;
15551 }
15552 if self.priorities.is_none() && write.item.priority != Priority::None {
15553 return Err(self.holds_no_priority());
15554 }
15555 Ok(Incoming {
15556 written: Written::Work(ItemKind::Task, &write.item.status),
15557 title: &write.item.title,
15558 content: write.item.content.as_deref(),
15559 assets,
15560 labels: &write.item.labels,
15561 metadata: &write.item.metadata,
15562 repositories: &write.item.repositories,
15563 classification: write.item.classification,
15564 parent: write.item.project.as_ref(),
15565 delivers: &write.item.delivers,
15566 delivered_by: &write.item.delivered_by,
15567 priority: self.priorities.as_ref().map(|_| write.item.priority),
15568 })
15569 }
15570 async fn write_document_assets(
15571 &self,
15572 write: &ItemWrite<Document>,
15573 assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
15574 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
15575 // A document takes part in no dependency graph, so there is no far end to write
15576 // natively and none to record: a caller naming one is told so rather than having it
15577 // stored under the reserved key, where a later read would report an edge the
15578 // contract says cannot exist.
15579 if !write.depends_on.is_empty() {
15580 return Err(SourceError::Refused {
15581 message: format!(
15582 "this write names {} dependencies for a document, and a document takes \
15583 part in no dependency graph; next: put the dependency on the task or \
15584 project the document is about",
15585 write.depends_on.len()
15586 ),
15587 });
15588 }
15589 self.write_item(
15590 &Incoming {
15591 written: Written::Document,
15592 title: &write.item.title,
15593 content: write.item.content.as_deref(),
15594 assets,
15595 labels: &write.item.labels,
15596 metadata: &write.item.metadata,
15597 repositories: &write.item.repositories,
15598 classification: write.item.classification,
15599 parent: write.item.project.as_ref(),
15600 delivers: &[],
15601 delivered_by: &[],
15602 priority: None,
15603 },
15604 write.target.as_ref(),
15605 &[],
15606 )
15607 .await
15608 }
15609}
15610
15611/// What [`TaskSource::end_command`] leaves of this source's held state, asserted on the state
15612/// itself, for the two things no journey can observe.
15613///
15614/// The journeys in `crates/onetaskgraph-e2e/tests/e2e/end_command.rs` prove through the engine,
15615/// with and without the call, that a settlement, a board listing and a metadata search each
15616/// read afresh after it — the resolved records, the written-item overlay, the board and its
15617/// search, and the narrowed searches. What they cannot reach is the held field definitions,
15618/// because a status write naming an option a person deleted is refused the same whether or
15619/// not the list is held, and a poisoned lock, because nothing outside the source can panic
15620/// while one of its locks is held. So these assert those directly, and every other holder
15621/// beside them so a holder added later without a clear in the call fails here.
15622#[cfg(test)]
15623mod end_command_tests {
15624 use super::*;
15625
15626 struct Token;
15627
15628 impl SecretResolver for Token {
15629 fn get(&self, var: &str) -> Option<SecretString> {
15630 (var == "GH_PROJECTS_TOKEN").then(|| "test-token".into())
15631 }
15632 }
15633
15634 fn source() -> GitHubProjectsSource {
15635 let config = serde_json::from_value(json!({
15636 "owner": "octo-org", "project_number": 7, "repository": "acme/work",
15637 // Nothing here is sent: the source is only built and its state inspected.
15638 "endpoint": "http://127.0.0.1:9/graphql",
15639 }))
15640 .expect("a usable configuration");
15641 GitHubProjectsSource::new(&SourceName::new("work").unwrap(), config, &Token)
15642 .expect("the source builds")
15643 }
15644
15645 /// One issue as a board read answers it.
15646 fn resolved(source: &GitHubProjectsSource) -> Resolved {
15647 source
15648 .resolve(&json!({
15649 "id": "ITEM-1",
15650 "content": {"__typename": "Issue", "id": "I_1", "title": "Held",
15651 "body": "what a person may since have edited", "state": "OPEN",
15652 "stateReason": null, "url": null, "number": 1,
15653 "subIssuesSummary": {"total": 0},
15654 "labels": {"nodes": [], "pageInfo": {"hasNextPage": false}}},
15655 "fieldValues": {"nodes": [], "pageInfo": {"hasNextPage": false}},
15656 }))
15657 .expect("the item reads")
15658 .expect("an issue")
15659 }
15660
15661 /// Hold something in every holder the call clears, and the repository id it keeps.
15662 fn fill(source: &GitHubProjectsSource) {
15663 let item = resolved(source);
15664 source.created.lock().unwrap().push(item.clone());
15665 source.updated.lock().unwrap().push(item.clone());
15666 *source.board_cache.lock().unwrap() = Some(Board {
15667 id: "PVT-board".into(),
15668 fields: json!({"nodes": []}),
15669 items: vec![item.clone()],
15670 });
15671 *source.search_cache.lock().unwrap() = Some(vec![item.clone()]);
15672 source
15673 .narrowed_cache
15674 .lock()
15675 .unwrap()
15676 .insert("status:todo".into(), vec![item.clone()]);
15677 source.children_cache.lock().unwrap().insert(
15678 NativeId("P-1".into()),
15679 (NativeId("P-1".into()), vec![item.clone()]),
15680 );
15681 source
15682 .search_next
15683 .lock()
15684 .unwrap()
15685 .insert("status:todo".into(), Some("cursor".into()));
15686 source
15687 .resolved_cache
15688 .lock()
15689 .unwrap()
15690 .insert(item.id.clone(), item);
15691 *source.fields_cache.lock().unwrap() = Some(BoardFields {
15692 id: BoardId::parse("PVT-board").unwrap(),
15693 fields: json!({"nodes": []}),
15694 });
15695 source
15696 .repository_cache
15697 .lock()
15698 .unwrap()
15699 .insert(RepositoryTarget::parse("acme/work").unwrap(), "R_1".into());
15700 }
15701
15702 fn assert_dropped(source: &GitHubProjectsSource) {
15703 assert!(source.created().unwrap().is_empty(), "created");
15704 assert!(source.updated().unwrap().is_empty(), "updated");
15705 assert!(source.board_cache().unwrap().is_none(), "board");
15706 assert!(source.search_cache.lock().unwrap().is_none(), "search");
15707 assert!(source.narrowed_cache.lock().unwrap().is_empty(), "narrowed");
15708 assert!(
15709 source.children_cache.lock().unwrap().is_empty(),
15710 "project children"
15711 );
15712 assert!(
15713 source.search_next.lock().unwrap().is_empty(),
15714 "search paging"
15715 );
15716 assert!(
15717 source.resolved_cache().unwrap().is_empty(),
15718 "resolved records"
15719 );
15720 assert!(source.fields_cache().unwrap().is_none(), "board fields");
15721 assert_eq!(
15722 source.repository_cache().unwrap().len(),
15723 1,
15724 "a repository's node id stays valid and is kept"
15725 );
15726 }
15727
15728 fn end(source: &GitHubProjectsSource) {
15729 tokio::runtime::Builder::new_current_thread()
15730 .build()
15731 .unwrap()
15732 .block_on(source.end_command())
15733 .expect("the command ends");
15734 }
15735
15736 #[test]
15737 fn the_call_drops_every_item_search_and_board_read_and_keeps_repository_ids() {
15738 let source = source();
15739 fill(&source);
15740 end(&source);
15741 assert_dropped(&source);
15742 }
15743
15744 #[test]
15745 fn the_call_clears_a_lock_an_earlier_failure_poisoned() {
15746 fn poison<T: Send>(held: &Mutex<T>) {
15747 std::thread::scope(|scope| {
15748 let _ = scope
15749 .spawn(|| {
15750 let _guard = held.lock().unwrap();
15751 panic!("a failure while the lock is held");
15752 })
15753 .join();
15754 });
15755 assert!(held.is_poisoned());
15756 }
15757 let source = source();
15758 fill(&source);
15759 poison(&source.created);
15760 poison(&source.updated);
15761 poison(&source.board_cache);
15762 poison(&source.search_cache);
15763 poison(&source.narrowed_cache);
15764 poison(&source.search_next);
15765 poison(&source.resolved_cache);
15766 poison(&source.fields_cache);
15767 assert!(
15768 source.resolved_cache().is_err(),
15769 "a poisoned lock is refused before the call"
15770 );
15771 end(&source);
15772 assert_dropped(&source);
15773 }
15774}
15775
15776#[cfg(test)]
15777mod unprojectable_fields_tests {
15778 use super::*;
15779
15780 /// The field names `docs/metadata.md` says a `metadata_fields` entry may not name.
15781 fn documented() -> std::collections::BTreeSet<String> {
15782 let document = std::fs::read_to_string(
15783 std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../docs/metadata.md"),
15784 )
15785 .expect("docs/metadata.md is readable");
15786 let document = document.split_whitespace().collect::<Vec<_>>().join(" ");
15787 let lead = "a `field` equal, ignoring case, to ";
15788 let start = document.find(lead).expect("the refused-field sentence") + lead.len();
15789 let sentence = &document[start..];
15790 let sentence = &sentence[..sentence
15791 .find("; and two entries naming one field")
15792 .expect("the refused-field sentence ends at the duplicate rule")];
15793 let (own, github) = sentence
15794 .split_once(" or a field GitHub owns on every board (")
15795 .expect("the sentence names GitHub's own fields");
15796 own.split('`')
15797 .skip(1)
15798 .step_by(2)
15799 .chain(
15800 github
15801 .strip_suffix(')')
15802 .expect("GitHub's own fields close the sentence")
15803 .split(", "),
15804 )
15805 .map(str::to_owned)
15806 .collect()
15807 }
15808
15809 #[test]
15810 fn the_documented_refused_fields_are_exactly_the_ones_the_configuration_refuses() {
15811 let refused: std::collections::BTreeSet<String> = UNPROJECTABLE_FIELDS
15812 .iter()
15813 .map(|&name| name.to_owned())
15814 .collect();
15815 assert_eq!(documented(), refused, "the fields docs/metadata.md lists");
15816 let instance = SourceName::new("work").unwrap();
15817 for name in UNPROJECTABLE_FIELDS {
15818 for spelled in [name.to_owned(), name.to_uppercase()] {
15819 let entry = MetadataFieldConfig {
15820 field: spelled.clone(),
15821 key: "team.machine".to_owned(),
15822 path: Vec::new(),
15823 };
15824 let refused = MetadataField::resolve(vec![entry], &instance)
15825 .expect_err("an unprojectable field is refused");
15826 assert!(
15827 refused.to_string().contains(&format!("{name:?}")),
15828 "{spelled}: {refused}"
15829 );
15830 }
15831 }
15832 }
15833}