onetaskgraph_github_projects/lib.rs
1//! A stateless onetaskgraph source over one GitHub Projects v2 board.
2//!
3//! **A board is a container of projects, not a project.** Its own `title`,
4//! `shortDescription` and `readme` are never read as an item's fields and are never
5//! written: nothing in this source can rename the board a user configured.
6//!
7//! **A project is an issue and its tasks are that issue's sub-issues.** GitHub's schema
8//! decides that: `Issue` exposes `parent`, `subIssues` and `subIssuesSummary`, and
9//! `DraftIssue` exposes none of them. Creating an issue needs a `repositoryId`, and a
10//! board has none, so a write without [`GitHubProjectsConfig::repository`] is refused
11//! naming the field — but that repository is the *fallback*, not the home of every item.
12//!
13//! <!-- llmlint: ignore-block[contracts_have_one_source_or_a_drift_gate] The rule's one
14//! executable source is `GitHubProjectsSource::creation_target`; this is where a reader of
15//! the module meets it, and `tests/plugin.rs` drives every arm below against the loopback
16//! board and asserts on `createIssue`'s own `repositoryId`, so the prose cannot outlive a
17//! change to the rule. -->
18//! **Which repository an issue is created in is decided by the item's own `repositories`
19//! field, under one rule.** Exactly one entry names the repository the issue is created in:
20//! a task issue is where a person finds the work from the repository it changes, and one
21//! filed in a board's nominated repository is invisible from every other. Zero entries, or
22//! two or more, name none, so a task's or a document's issue is created in the repository
23//! its parent project's issue lives in — read from the board, or from this process's own
24//! record of a project it created earlier in the same command — and a project's issue, or
25//! a task or document written with no parent, is created in the configured `repository:`.
26//! What that rule refuses, it refuses before `createIssue`, so no issue is half-created. An
27//! existing issue is never moved: the update path leaves the issue where it is and records
28//! the list in the metadata slot when it differs, so the read side's derivation and the
29//! creation rule agree by construction.
30//! <!-- llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate] -->
31//!
32//! **A document is an ordinary issue whose title begins [`DESIGN_TITLE_PREFIX`].** A
33//! board has no document type and nothing but issues to hold one in, so the title is the
34//! discriminator and it is the whole of it. The title this source *reports* is the one a
35//! person wrote, with the prefix taken off — the same way the metadata slot is taken off
36//! the body so `content` is what the person wrote — and writing a document puts the prefix
37//! back, so a round trip returns the title that went in.
38//!
39//! **Telling a document from a project from a task.** The design prefix is read **first**:
40//! a document is never a project and never a task, whatever sub-issues it has or does not
41//! have. Only then does the rest apply — a board issue is a project when *either* it has
42//! sub-issues *or* it carries [`ItemKind::METADATA_KEY`]; otherwise it is a task. A
43//! sub-issue is always a task, whatever it carries. The marker is sufficient and never
44//! necessary: it is what makes an *empty* project — the state a project copy passes
45//! through between creating the project and filing its first task — readable as a
46//! project, while the sub-issue arm lets a person author a project on the board by hand
47//! with no knowledge of this product's metadata at all. Reading the prefix later than the
48//! sub-issue rule would make a design issue with no sub-issues an empty project, which is
49//! exactly the state that rule exists to catch. Pull requests are neither a project nor a
50//! task nor a document and are ignored.
51//!
52//! **A task's comments are its issue's comments.** They are read off `Issue.comments` and
53//! written with `addComment`, `updateIssueComment` and `deleteIssueComment`, and a comment's
54//! id is GitHub's own node id for the `IssueComment`. Two things GitHub decides are refused
55//! rather than papered over: a board **draft** is not an issue and has no comments at all, so
56//! a comment call on one is refused rather than answered with an empty page; and GitHub signs
57//! every comment as the account the token belongs to, so a comment handed an author of its
58//! own is refused rather than posted under another name. GitHub's comment mutations take the
59//! comment's id and nothing else, so an edit or a delete first reads which issue that comment
60//! is on, and a comment on some other issue is one this task does not have.
61//!
62//! **Where an entity is, is a link.** Every project, task and document this source reports
63//! carries a [`Location::Url`] naming the issue's own web address — the same address the
64//! `url` field already reports, in the shape that says a reader can open it. That is the
65//! contrast the location contract exists for: a reader holding an entity from this source
66//! is handed something to link to and one holding an entity from a folder of Markdown is
67//! handed a path, and neither has to know which plugin answered. It does not replace or
68//! derive from `url`; that field goes on reporting what it always reported.
69//!
70//! **Where metadata lives.** Short typed things go to typed fields and native relations:
71//! status to the board's `Status` single-select and the issue's own state, the copy
72//! origin to a source-owned `onetaskgraph.origin` text field, and dependencies to
73//! `blockedBy` and to sub-issue links. Unbounded caller JSON goes in a trailing
74//! `<!-- onetaskgraph.metadata ... -->` comment at the end of the issue body — the same
75//! encoding `docs/metadata.md` settles for Linear, not a second one. A ProjectV2 text
76//! field is length-bounded and `shortDescription` is capped at 300 characters, which is
77//! why neither can hold a caller's own prose. Setting one caller key on its own — on a task,
78//! a project or a document alike — is one update of the issue body that changes that slot
79//! and not one byte outside it, and it is not sent at all when the key already holds the
80//! value. The link a copy records on an item it copied, `onetaskgraph.copies`, is small and
81//! is kept in that same slot, written by that same update.
82//!
83//! **A caller's value can also be projected onto a board text field**, so the board can filter
84//! on it: each [`GitHubProjectsConfig::metadata_fields`] entry names a text field, a top-level
85//! metadata key and a path inside its value. The slot stays the value's one home — every read
86//! reports metadata from it alone — and the field follows it on every write of an item, a
87//! create, an update and a copy alike: a string is written, and not sent again while the field
88//! holds it — an empty string included, which is written as itself; nothing there or `null`
89//! clears a field holding a value; any other
90//! JSON type is refused before any mutation. It rides the ordered [`graphql::UPDATE_FIELDS`]
91//! write beside `Status`, `Priority` and the origin, so it adds no request; a key set on its
92//! own moves its field first and the body last, as every write of an existing item does. A
93//! board without the field, or with a non-text field of that name, is refused before any
94//! mutation pointing at `sources fields`, whose [`GitHubProjectsSource::fields`] creates it as
95//! a text field and never changes a field that is there.
96//!
97// llmlint: ignore-block[contracts_have_one_source_or_a_drift_gate] This public module documentation is a required user-facing description; the loopback plugin tests and shared live journey drive StatusMapping resolution, both mutations, and observed read-back together.
98//! **Status.** `status_mapping` is per-instance configuration, in the shared grammar
99//! [`onetaskgraph_plugin_api::StatusMapping`] documents, from a status category to an
100//! option of the board's one `Status` field for a task and for a project: a bare name is
101//! the option for both kinds, `null` disables the category for both, and `{task, project}`
102//! names it per kind. A category the mapping does not mention keeps its shipped default for
103//! both kinds; one it mentions is exactly what it configures, so a per-kind object no longer
104//! gets the shipped default for the kind it leaves out. Two categories one kind would read
105//! back from one option are refused as the configuration is read, ignoring case, while one
106//! option may stand for different categories of the two kinds. Writes go by the kind of the
107//! item written: a status that kind has no option for, or whose option the board lacks, is
108//! refused before any mutation, naming the source, the kind, the category and the key
109//! `status_mapping.<category>.<kind>` — there is no fallback. `done` selects its mapped
110//! option and closes the issue as `COMPLETED`; `cancelled` selects its mapped option and
111//! closes it as `NOT_PLANNED`, for either kind. Every open category reopens a closed issue
112//! before selecting its option. Reads give a closed issue's reason precedence over its
113//! option, while an open issue's option decides its category through its own kind's
114//! mapping, and an option that mapping does not name reads as `unknown` under its own name.
115//! The guarded [`GitHubProjectsSource::status_options`] and
116//! [`GitHubProjectsSource::fields`] operations are the one path here that calls
117//! `updateProjectV2Field`: GitHub replaces the whole option list, so they preserve every
118//! existing option id and verify the field and item assignments immediately afterwards.
119//! They ask for both kinds' options, counting a terminal category's mapped option as
120//! configured because a terminal write refuses without it. No ordinary source read or
121//! write calls that mutation, whose
122//! `singleSelectOptions` *overwrites* a field's option set, so no addition is additive
123//! and a mistake destroys every item's status. A status this board cannot represent is a
124//! refusal naming the status and the instance instead.
125//!
126//! `unknown` has no shipped option because this source cannot preserve an open-ended
127//! status word: it writes an existing board option and never
128//! creates an option. An operator may map `unknown` to one existing option, in which case
129//! every unknown word lands on that option and reads back as `unknown` under the option's
130//! name. This differs from `local-md`, which writes and reads the original word itself.
131//!
132//! The shipped terminal options are exactly `done: Done` and `cancelled: Cancelled`.
133//! `done` also closes the issue because GitHub derives `subIssuesSummary.completed`
134//! and the board's own `Sub-issues progress` field from closed sub-issues: a plan whose
135//! finished tasks were only moved to a "Done" column would read 0% complete forever.
136// llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
137//!
138//! # What this source declares, field by field
139//!
140//! One verdict per field of [`Capabilities`], and what `Native` means when this source
141//! says it. *Proven* means a shared journey drives it against the real
142//! binary over this source's own row in `crates/onetaskgraph-e2e-support/src/fixtures.rs`, and
143//! `every_row_declares_exactly_what_its_plugin_reports` is what keeps this list and
144//! [`capabilities`](TaskSource::capabilities) from parting.
145//!
146//! | Field | Verdict |
147//! | --- | --- |
148//! | `projects` | **Supported and proven,** and the one predicate here that is pushed down rather than applied in process: a task's project is the issue it is a sub-issue of, so a listing scoped to one *asks that issue* for its own sub-issues. This is the field that was declared and then not applied, which silently returned another project's tasks. |
149//! | `documents` | **Supported and proven.** A board holds issues, so a document is one: the issue whose title begins [`DESIGN_TITLE_PREFIX`]. Reads, filters and paging answer on exactly the terms a task read does, and a write puts the prefix back. |
150//! | `comments` | **Supported and proven,** over the task issue's own comment connection, oldest first and paged by GitHub's own cursor; added, edited and removed through GitHub's comment mutations, paced as every other mutation is. A draft item has no comments on GitHub and is refused, and so is an author, because GitHub records the signed-in account as every comment's author. |
151//! | `assets` | **Supported and native.** Image references travel as GitHub user attachments in the repository the task or document issue lives in. Uploads are authenticated and verified before the issue body is written. |
152//! | `priority` | **Supported and proven** by an instance configured with `priority_mapping`, and declared unsupported by one without it, which reports every task's priority as `none` and sends exactly the requests it sent before priorities existed. The priority is the board's single-select `Priority` field: no value is `none`, a mapped option is its level, matched case-insensitively, and an option the mapping does not name fails the read of that task, naming the option. A write selects the mapped option, or clears the value for `none`; a board without the field or the option is refused, pointing at `sources fields`, which is the one thing that creates either. |
153//! | `filter_by_priority` | **Supported and proven,** over the priority each task reads as — `none` for every task of an instance without `priority_mapping`. |
154//! | `filter_by_comment_activity` | **Supported, and exact** for comments created and for comments edited at or after `commented_since`, in every repository — of any owner — the board's items live in. Applied by asking a narrower question rather than by reading the board: GitHub's issue search scoped by `project:<owner>/<number>` alone, with an `updated:>=` qualifier, names the candidates, and each candidate's own comments confirm it, so `ProjectV2.items` is never read. That rests on GitHub moving an issue's `updatedAt` when a comment on it is added **or edited**, which the credentialed journey `an_edited_comment_moves_its_issue_and_is_selected_since` re-takes on every run of this lane. The search is an index that lags a write — the credentialed lane has watched it miss a newly commented issue for thirty seconds — so an issue this process itself commented on in the same command is a candidate whatever the search says, read by its own node if the search did not name it, and has its comments read rather than being ruled out by an `updatedAt` from before the comment. A comment another process wrote is found only once the index has it, so a caller asking again from its last instant should overlap the two generously. |
155//! | `orphan_tasks` | **Supported and proven.** A task issue with no `parent` is in no project. |
156//! | `filter_by_label` | **Supported and proven,** over the issue's own labels. |
157//! | `filter_by_status` | **Supported and proven,** over the board's `Status` option and the issue's open or closed state, through this instance's own `status_mapping` for the item's kind — a task query by the task half, a project query by the project half, `unknown` included. |
158//! | `filter_by_metadata` | **Supported, and asked of GitHub** — through the body and never through a projected `metadata_fields` text field, which is the board's to filter on and not this source's to read. A query naming metadata values is one board-scoped issue search with each value a quoted phrase `in:body` — GitHub's index covers the metadata comment at the end of the body, which is where caller metadata lives — and every candidate is confirmed against its own parsed metadata comment, so only an item holding that string at that key and path is returned. **A value with no letter or digit is refused** — the empty string, whitespace or punctuation alone — before any request, as a `SourceError::Refused` (wire kind `refused`) naming the value: GitHub's index holds words, so no bounded query can find such a value, and this source neither reads the whole board for it nor answers it as empty. |
159//! | `filter_by_origin` | **Supported, and asked of GitHub without enumerating the board.** The union of three reads, each confirmed by an exact match against the item's own origin field: the board's field filter over the `onetaskgraph.origin` text field, the issue search for the id as a phrase in the body where a write of this release mirrors it, and this process's own writes. See *Where a read-after-write guarantee comes from* for the window the three leave. |
160//! | `reconcile_status` | **Supported,** and proven by `onetaskgraph-github-projects-e2e`'s `status_reconcile.rs`: the board's own `ProjectV2.items(query:)` filter, `is:closed`, read page by page and never the unfiltered items, each candidate confirmed from its own item node — a closed task whose card is not at the option the mapping gives its closure's category. |
161//! | `complete_by_issue` | **Supported,** and proven by `status_complete.rs` there and in the engine's suite: each issue a reference names read in one aliased request per fifty — its repository, its number, its first page of board memberships with each card's `Status` read by name — and nothing else of the board. |
162//! | `search_title` | **Supported, and asked of GitHub for a task,** over `Issue.title`: a task query's text is one board-scoped issue search for it as a phrase `in:title`, every candidate confirmed by the case-insensitive substring rule. GitHub matches whole words, so a task holding the text only inside a longer word is not returned — a narrowing this source declares rather than hides. **A text with no letter or digit that is not blank is refused** — `--` for one — before any request, as the same `refused` error naming the text, for the reason a metadata value like it is; a blank text is not refused, and keeps the board read it always had, confirmed by the same substring rule. A project query's text, and a document query's text when the query is scoped to no project, is that same board-scoped search for the same phrase in the same fields, refused on the same terms, every candidate confirmed by its kind and by the same substring rule, so it narrows exactly as a task's does; a document query scoped to one project sends no search, reads that project's sub-issues and confirms its text over them by the substring rule alone, so it is neither narrowed to whole words nor refused for a text with no letter or digit. A board draft is not an issue, so no text search lists one, a draft titled as a document included. |
163//! | `search_content` | **Supported,** on the same terms, `in:body`, over the visible body — the trailing metadata comment is not part of what the substring rule confirms. |
164//! | `task_dependencies` | **Supported and proven,** in both directions: `blockedBy` and `blocking`. |
165//! | `project_dependencies` | **Supported and proven,** in both directions, over the same two connections, because a project here is an issue. |
166//! | `max_page_size` | **Supported and proven.** [`MAX_PAGE_SIZE`], GitHub's own connection maximum. |
167//!
168//! Image writes use `POST https://uploads.github.com/user-attachments/assets` with `name`,
169//! `content_type` and the issue repository's numeric `repository_id` as query parameters,
170//! the source token as `Authorization: Bearer`, the image content type as `Content-Type`,
171//! and the raw bytes as the request body. The numeric repository id is read at most once
172//! per repository per source instance. A loopback API endpoint also selects the loopback
173//! uploads host; production needs no assets repository, commits or extra configuration.
174//! Every returned JSON `url` is read with the same token before a body is written: only
175//! a 2xx verification succeeds. A refusal names the asset, URL and HTTP status; an
176//! anonymous 404 does not invalidate an authenticated 200. An upload refusal names the
177//! asset and HTTP status and says the token type may not be accepted by the upload
178//! endpoint. A classic PAT was measured accepted; no acceptance claim is made about
179//! fine-grained PATs or OAuth tokens.
180//! The body keeps the authored alt text while `./<name>` becomes the attachment URL,
181//! and `onetaskgraph.assets` records `{sha256, url}` by name. A matching destination
182//! SHA-256 reuses its URL without an upload or verifying read; changed bytes are uploaded
183//! and verified afresh, in the repository the issue already lives in on an update.
184//! An uploaded image renders for the viewers GitHub lets read it, like the issue text beside it.
185//! Uploads take the same mutation spacing as content writes. Both uploads and verification
186//! reads wait out classified rate limits within the configured per-call retry budget,
187//! on the supplied clock; verifying reads take no mutation slot.
188//!
189//! Nothing here is unsupported. `documents` and `comments` are not predicates — they say this
190//! source has documents and that its tasks have comments, both of which hold — and the three
191//! facts behind the uniform `Native` on the
192//! predicates beside it are recorded below rather than re-derived, because a reader who
193//! takes `Native` to mean *the remote service filters* will read that uniformity as a
194//! lie.
195//!
196//! First, the plugin contract defines `Support::Native` as *the source applies this
197//! predicate itself*, and says nothing about where it applies it. What the declaration
198//! promises the engine is capability rule 1 — a predicate declared `Native` **is** applied
199//! — so that the engine may push it down and apply nothing of its own.
200//!
201//! Second, this source can keep that promise for every predicate at no additional API
202//! cost, because whichever of the reads below answers a query has already read every
203//! candidate that query will return before it filters anything. Filtering those items is
204//! in-process work over data already in hand.
205//!
206//! Third, six task predicates are asked of GitHub as a narrower question and the rest are
207//! applied in process over what that question returned. A project filter has a relationship — a
208//! project's tasks are that issue's sub-issues, and asking the issue for them is both cheaper
209//! and exact. Comment activity is the issue search's `updated:` qualifier. A text search, and
210//! a search for metadata values, is the board-scoped issue search carrying the text and each
211//! value as quoted phrases; an origin is the board's own field filter over its origin field
212//! beside the same search for the id. **The text search narrows, and that is this source's
213//! declared semantics:** GitHub matches whole words where the substring rule this source and
214//! the local Markdown source confirm with would match inside one, so an item holding the text
215//! only inside a longer word is never a candidate. Every item returned does contain the text.
216//! A project query's text, and a document query's scoped to no project, is that same search
217//! and narrows on the same terms, its candidates confirmed by their kind as well.
218//! GitHub's issue search offers no qualifier for a label set, a status column or a priority,
219//! so those three are applied in process over the candidates, and a query carrying none of
220//! the six narrowing predicates reads the board. Declaring one `Unsupported` would make the
221//! engine compensate for work this source has already done, and declaring `projects` native
222//! while ignoring the filter (which this source once did) silently returns another project's
223//! tasks, because the engine trusts the declaration and applies nothing locally.
224//!
225//! # The three ways this source reaches an item, and what each costs
226//!
227//! A board read is charged for what its *nested* connections could return rather than for
228//! what was asked, so one whole-board read costs the same whether the question was about
229//! one project or about all of them. That is why a question about one project is never
230//! answered by reading the board:
231//!
232//! | The question | What is sent | What it costs |
233//! | --- | --- | --- |
234//! | one item, by its own id | [`graphql::ISSUE`] — `node(id:)`, carrying the field definitions of the boards it sits on and the far ends of its `blockedBy`, which is what a write of it needs — and, when that node is a board draft, [`graphql::DRAFT`] — the draft and the one board item it is | the item |
235//! | one task with its first page of comments, for `task show` and a comment listing | [`graphql::ISSUE_DETAIL`] — the same `node(id:)` read with the issue's `comments` | the item and a page of its comments |
236//! | several tasks with their comments, for `task show-many` | [`graphql::ISSUE_DETAILS`] — [`DETAIL_BATCH`] aliased `node(id:)` fields per request | each item and a page of its comments |
237//! | the board's own id and field definitions, for a write whose item does not carry them | [`graphql::BOARD_FIELDS`] — the board's `id` and `fields`, and no `items` — or, for a create that needs the repository's id too, [`graphql::CREATION_CONTEXT`], both in one request | the board's fields |
238//! | one project's tasks or documents | [`graphql::SUB_ISSUES`] — that issue's own `subIssues` | that project |
239//! | which projects this board holds | [`graphql::SEARCH_ISSUES`] — an issue search scoped to the board | the board's issues, without their board items |
240//! | which projects hold a text, or which documents do when no project narrows the question | [`graphql::SEARCH_ISSUES`] — the board-scoped search with the text as one quoted phrase, `in:title`, `in:body` or both, as a task's text is sent — walked to its end in pages of twenty | the issues that match |
241//! | which tasks were commented on since an instant | [`graphql::SEARCH_ISSUES`] — the same board-scoped search with an `updated:>=` qualifier — then [`graphql::ISSUE_COMMENTS`] for each candidate it names | the issues updated since, and their comments |
242//! | which tasks hold a text, or a metadata value | [`graphql::SEARCH_ISSUES`] — the board-scoped search with the text and each value as quoted phrases, `in:title`, `in:body` or both, and an `updated:>=` qualifier too when comment activity is asked for — in pages of twenty, only as many as the caller's rows need | the issues that match |
243//! | which tasks were copied from one origin | [`graphql::ORIGIN_LOOKUP`] — the board's own `items` under its field filter on the origin field, and the same board-scoped search for the id `in:body`, in one request, each paged at three | the carriers of that origin, which is one item |
244//! | every task, every document, every label, when nothing above narrows the question | [`graphql::BOARD`] — the board's own `items` — **and** [`graphql::SEARCH_ISSUES`], because neither enumeration of a board is complete alone; see [`GitHubProjectsSource::board`] | the board, twice over |
245//! | which board item one issue is, past the page that came with it | [`graphql::ISSUE_BOARD_ITEMS`] — that issue's own `projectItems` | one issue's memberships |
246//!
247//! The following standalone-ticket requests are pinned by the real CLI fixture journeys
248//! `follow_up_writes_resolve_each_item_once_and_batch_the_copy_fields` and
249//! `a_batched_detail_read_costs_one_request_and_one_point_per_detail_batch`, as request count
250//! equal to declared points equal to the row. They include the origin lookup and the
251//! field/repository discovery a create needs. A bound re-copy changes status, priority,
252//! content and metadata; comment recount means a subsequent detail read. Each request here
253//! costs one declared point. A membership beyond the embedded page can additionally require
254//! the one-point membership recovery described above. A bound re-copy of a task filed under a
255//! project adds one read, the engine confirming that project's link by its own id once per
256//! command; and the same-source far ends a write newly names — those that do not already block
257//! the item, whose own read answered for them — are read together by their own ids,
258//! [`DETAIL_BATCH`] to one [`graphql::ISSUE_DETAILS`] request, each new edge then one
259//! [`graphql::ADD_BLOCKED_BY`]. Both additions are rows of the table below, pinned by
260//! `a_bound_recopy_adds_one_project_read_and_batches_the_dependencies_it_newly_names`.
261//!
262//! **[`DETAIL_BATCH`] is 24**: the largest batch of [`graphql::ISSUE_DETAILS`] the node-count
263//! model prices at one point. Each aliased item is six of GitHub's aggregate, so 24 are 144,
264//! which rounds to one point, and 25 are 150, which rounds to two; `tests/point_cost.rs`
265//! holds both halves.
266//!
267//! **An existing item is written body last.** A bound re-copy and a `task update` send its
268//! board fields first — the `Status` option and the `Priority` together, in one request — then
269//! its parent and its `blockedBy`, and its title, body and state in one `updateIssue` last.
270//! GitHub runs no two requests as one, and runs a document's mutation fields in order without
271//! undoing an earlier field when a later one fails, so that order is what makes a write
272//! refused part-way leave the item's body, and every metadata key in it, exactly as it stood;
273//! the one piece of metadata written before the body, an origin a copy re-points, is put back
274//! when a later write is refused — and when putting it back is refused too, the write's own
275//! refusal names that key, what it now holds and what it held. `crates/onetaskgraph-github-projects-e2e/tests/e2e/write_order.rs` refuses each
276//! of those writes in turn, whole and as one aliased field failing after the one before it.
277//!
278//! **Two facts about GitHub the write rows rest on, each read off GitHub's published schema
279//! artifact <https://docs.github.com/public/fpt/schema.docs.graphql> on 2026-10-01 and pinned
280//! in `tests/fixtures/schema.graphql`, and the first then put to GitHub itself:**
281//!
282//! - **A board is accepted at creation but its item is not answered, so a create still files
283//! the issue itself: a new copy is 5 requests, and 4 with `--create`.**
284//! `CreateIssueInput.projectV2Ids: [ID!]` is declared there — "An array of Node IDs for
285//! Projects V2 associated with this issue", `@possibleTypes(concreteTypes: ["ProjectV2"])`.
286//! The credentialed journey `real_projects_v2_contract_writes_and_leaves_no_residue` was run
287//! against a real board on 2026-10-01 with a create sending the board there and reading the
288//! item off the payload's `Issue.projectItems`: every one of its four creates answered with
289//! no item on the board, so each went on to [`graphql::ADD_TO_BOARD`], and the fourth was
290//! refused "Content already exists in this project" — GitHub had filed the issue after
291//! answering, and refuses a second filing rather than answering with the item it holds. A
292//! create therefore sends no `projectV2Ids` and files the issue with
293//! `addProjectV2ItemById`, the one call whose answer names the board item. The saving that is
294//! real is the read before it: the board's fields and the repository's id together, in
295//! [`graphql::CREATION_CONTEXT`], at the point the repository is known.
296//! - **A comment still reads its target first, so a comment is 2 requests.**
297//! `AddCommentInput.subjectId: ID!` is declared there with
298//! `@possibleTypes(concreteTypes: ["Issue", "PullRequest"], abstractType:
299//! "IssueOrPullRequest")`. A board draft is no such subject and would be refused, but a
300//! project's issue, a document's issue, an issue on no board of this source and a pull
301//! request all are: GitHub writes the comment, so there is no refusal to map into "that is
302//! not a task of this board". [`graphql::ISSUE`] before [`graphql::ADD_COMMENT`] is what
303//! refuses those by name.
304//!
305//! | Verb | Requests / points | Documents |
306//! | --- | --- | --- |
307//! | new copy | 4 | ORIGIN_LOOKUPS (the origin, the board's fields and the repository's node and numeric ids together, a page of one carrier for each origin), CREATE_ISSUE filing the issue on the board and under its project, CREATED_BOARD_ITEMS once GitHub has added it, WRITE_FIELD_VALUES |
308//! | copy --create | 4 | CREATION_CONTEXT, CREATE_ISSUE, CREATED_BOARD_ITEMS, WRITE_FIELD_VALUES: the new copy with no origin to ask about |
309//! | bound copy | 3 | ISSUE (with the board's fields and the issue's `blockedBy`, so no BOARD_FIELDS or ISSUE_DEPENDENCIES), UPDATE_FIELDS when a board field differs, then UPDATE_ISSUE last carrying only the title, body or state that differs — none, when nothing does |
310//! | bound copy, filed under a project | 4 | the bound copy's three, and one ISSUE of the destination project its link names, read once per command |
311//! | bound copy, newly naming n dependencies | + ceil(n / DETAIL_BATCH) + n | ISSUE_DETAILS for the far ends that do not already block the item, DETAIL_BATCH (24) to a request (one alone is ISSUE), then one ADD_BLOCKED_BY each; a far end already blocking it is answered by its own read and costs nothing |
312//! | comment | 2 | ISSUE, ADD_COMMENT: the target is read first, because GitHub accepts a comment on any issue or pull request (see below) |
313//! | detail | 1 | ISSUE_DETAIL: the item and its first page of comments, for `task show` and `task comment list`; `--no-comments` is ISSUE alone |
314//! | batched detail | ceil(n / DETAIL_BATCH) | ISSUE_DETAILS: `task show-many` of `n` items, DETAIL_BATCH (24) at a time, comments included or not |
315//! | copy --comments, n changed tasks | ceil(n / COPY_DETAIL_BATCH) | ISSUE_COPY_DETAILS: each task with its first page of comments and its `blockedBy`, COPY_DETAIL_BATCH (21) at a time; a task whose comments run past that page adds one ISSUE_DETAIL per further page, and a task skipped from its listing costs nothing beyond it |
316//! | recount | 1 | ISSUE_DETAIL |
317//! | status | 2 | ISSUE, UPDATE_FIELD; a terminal status additionally updates issue state |
318//! | priority | 2 | ISSUE, UPDATE_FIELD or CLEAR_FIELD, with stored priority in the mutation response |
319//! | content | 2 | ISSUE, UPDATE_ISSUE |
320//! | metadata | 2 | ISSUE, UPDATE_ISSUE |
321//! | document metadata, `--expect` a version | 2 | ISSUE, then UPDATE_ISSUE only when the issue is still at that version — the precondition GitHub's own update cannot take — primed with what the read answered |
322//! | update | 3 | `task update` naming any of title, body, metadata, status and priority — all five included: ISSUE, UPDATE_FIELDS (the status option and the priority together), UPDATE_ISSUE (title, body with its metadata slot, and state) last |
323//! | record only | 1 | ISSUE |
324//!
325//! <!-- github-search-paging:start -->
326//! Board-scoped text, metadata, project-name and comment-activity searches send every
327//! page at `first = 20` (SEARCH_PAGE_SIZE), the SEARCH_ISSUES document's one-point
328//! ceiling. A later page is sent only when `hasNextPage` is true and the caller still
329//! needs rows. A page is never resized to the rows still needed: GitHub orders one
330//! search differently at different page sizes, so one fixed size makes a paged walk
331//! send exactly the requests one whole read sends, and the answer's order is the order
332//! those pages arrive in. A page below twenty would cost the same one point, and GitHub
333//! prices this document by rows, so twenty-row pages cost per row what 100-row pages do.
334//! Project-name lookup continues until an exact match or exhaustion. A task limit bounds
335//! returned and fetched pages: a limit is sliced from the pages it needs, and local
336//! confirmation can require more candidates than matching rows. Walking all pages
337//! returns the whole answer. The opaque version-4 source cursor carries GitHub's page
338//! cursor and how far into that page the last answer stopped, and resumes in the same
339//! process or a new one, without duplicates or gaps. It carries no rows: one process
340//! sends each page's search once, and a new process re-reads only the page it resumes
341//! in, then sends a further page once, never as a re-read, only when its limit still
342//! needs rows. Every request either walk sends is the one a whole read sends for that page. Own writes replace stale index
343//! copies and complete missing rows at exhaustion. Cache entries are whole GitHub pages,
344//! so a small answer cannot truncate a wider question. Origin pages remain three; whole-board sizing is unchanged.
345//! Read-after-write is a per-process guarantee. A cursor resumed in a new process is
346//! not required to include the original process's writes still omitted by the index.
347//! <!-- github-search-paging:end -->
348//!
349//! The board half of an issue — its board item's id, its `Status` option and this
350//! source's origin text field — rides along on `Issue.projectItems` in the first three, so
351//! an item reached any of those ways resolves through the same
352//! [`GitHubProjectsSource::resolve`] the board walk uses and reports the same title, the
353//! same status, the same labels and the same qualified id. That connection comes back a
354//! *page* at a time, at `BOARD_ITEMS_PAGE_SIZE`, so the entry for this board is looked for
355//! on the page in hand and — only if that page reports more of the connection — in the
356//! last row's read of that one issue's memberships, resumed from the page's own cursor and
357//! walked to exhaustion. An issue with no entry for *this* board is not this source's to
358//! report, which is what keeps an id naming another repository's issue from being answered
359//! as an item of this board; and because the page is where the search starts rather than
360//! where it ends, that answer is one about a connection read to exhaustion and never about
361//! an unread page. Nothing costs the extra read but an issue on more boards than a page
362//! holds: an issue this board really does not hold reports no next page, so its
363//! memberships are already exhausted where they arrived.
364//!
365//! **No document here selects the board's own `Labels` field, and nothing is lost by
366//! that.** An item's labels are read from its content alone, wherever that content is
367//! reached: the three documents above select `Issue.labels` on the fragment, and
368//! [`graphql::BOARD`] selects the same connection on the `... on Issue` arm of its
369//! `content`. A board's `Labels` field is not one anybody fills in: it is a built-in
370//! `ProjectV2FieldType`, it is absent from `ProjectV2CustomFieldType` so no project can
371//! create one, and `ProjectV2FieldValue` — the whole of what
372//! `updateProjectV2ItemFieldValue` accepts — offers no way to write one. So GitHub derives
373//! it from the content, for every content type it exists on, and there is nothing it can
374//! hold that the content does not already say: for an `Issue` it *is* that issue's own
375//! labels, so selecting it beside them unions a set with itself.
376//!
377//! **A draft loses nothing by that either**, which is the reasoning this paragraph once had
378//! backwards. `DraftIssue` exposes no `labels` field, and by the three schema facts above
379//! it cannot carry a board `Labels` value to be derived from one — so a draft has nothing
380//! to select *and nothing to lose*, and reports no labels at all. A `PullRequest` item is
381//! discarded by [`GitHubProjectsSource::resolve`] before labels are read. Both halves are
382//! held to that by tests in `tests/plugin.rs`: the four ways an item is reached report one
383//! label set, and that set is the fixture issue's own, by
384//! `an_item_reports_the_same_labels_title_status_and_id_however_it_is_reached`; and a board
385//! item whose content is a draft reports an empty set, by
386//! `a_board_item_whose_content_is_a_draft_reports_no_labels_at_all`. The absence of the
387//! selection is held over [`graphql::DOCUMENTS`] by
388//! `no_document_selects_the_boards_own_labels_field`.
389//!
390//! The whole-board row is still the board's own item connection, and deliberately: a
391//! **draft** board item is not an issue, so no search can list one, and the reads that have
392//! to answer for the whole board are the ones whose cost is the board's size anyway.
393//!
394//! **A question about one item this source already names by id never lists the board.**
395//! Whether that item is on this board, and what its board fields are, is answered by reading
396//! that item — its own `Issue.projectItems`, walked to exhaustion by
397//! [`GitHubProjectsSource::resolve_issue`], or a draft's own board item — and never by
398//! looking for it in [`graphql::BOARD`]'s `items` or in a listing this command already
399//! holds. That covers a write's destination, the project a new item is filed under, a
400//! same-source far end a dependency names, a status write, the dependency slot a draft keeps,
401//! and the delete that takes back an item a copy made. What such a write needs of the board
402//! and the item does not carry — the board's id, the `Status` and origin field definitions —
403//! comes from [`graphql::BOARD_FIELDS`], which reads no item at all. The reason is evidence,
404//! not economy alone: `ProjectV2.items` is a projection that lags the membership GitHub
405//! itself reports — an issue added with `addProjectV2ItemById` can be missing from it for
406//! minutes. Scanning this host's 842-item board has refused a document copy and an update
407//! even though the items' own reads named that board. A scan there gives the wrong answer
408//! as well as paying for every page. So a `board.items` lookup does not belong on any of
409//! those paths.
410//!
411//! **What a read may return is capped too, and that cap is on the document rather than on
412//! the board.** GitHub limits the number of nodes **one query may return** to
413//! [`NODE_COUNT_LIMIT`] and refuses a query above that before executing it: the answer is
414//! an error naming the connection the count crossed at, not a slow or a partial result.
415//! Every board this source reads is refused the same way, so no board is too big for these
416//! documents and none is small enough to save one that is over.
417//!
418//! The count is arithmetic over the document's own text: each connection contributes the
419//! `first:` it asks for, counts **multiply** down a nested path and **sum** across sibling
420//! paths. Those are [GitHub's published rules][node-limits] and this workspace does not
421//! restate them — `github-graphql-node-count` implements them, and
422//! [`worst_case_node_count`] under [`largest_page_sizes`] is where every node count here
423//! comes from. `every_document_this_source_sends_stays_under_githubs_node_limit`, in
424//! `tests/node_count.rs`, recomputes every document in [`graphql::DOCUMENTS`] from that
425//! same text on every run and fails naming any that reaches the limit — so a connection
426//! added to a shared fragment is caught there rather than by GitHub.
427//!
428//! What decides those counts is the page sizes: [`MAX_PAGE_SIZE`] on the outer page,
429//! `NESTED_PAGE_SIZE` on the connections hanging off one item, and
430//! `BOARD_ITEMS_PAGE_SIZE` on the page of an issue's board memberships a read carries.
431//! `$nestedFirst` is spent twice down one path of a board read, so that constant is
432//! effectively squared there, which is why it is the one the limit is most sensitive to.
433//! `BOARD_ITEMS_PAGE_SIZE` is small for a reason of its own, recorded beside it: what a
434//! page of memberships misses is recovered by one further read rather than refused, so it
435//! buys a bound every read pays for at the price of a request only a multi-board issue
436//! pays.
437//!
438//! **`nodeCount` and `cost` are two numbers against two limits, and both are computed
439//! offline here — per document, one document at a time.** `nodeCount` is the one above: the
440//! most nodes one query may return, checked per query and bounded by [`NODE_COUNT_LIMIT`].
441//! `cost` is rate-limit points, metered per hour across everything one credential does; it
442//! is what the two limiters [`Limiter`] tells apart meter, and a document under
443//! [`NODE_COUNT_LIMIT`] still says nothing about its price. [`worst_case_point_cost`] is
444//! that second number, and `tests/point_cost.rs` pins every document in
445//! [`graphql::DOCUMENTS`] at what it costs — there being no per-call point ceiling to hold
446//! one under, the pin itself is the check. The credentialed lane reconciles both figures
447//! against GitHub's own, off a probe it already sends.
448//!
449//! **What is pinned that way is a per-document price and never a session's.** The record in
450//! `session-cost.md` measures the two quantities a whole session can be counted in offline —
451//! **requests** and **worst-case nodes** — and neither is points. What one whole session
452//! consumes of the hourly point allowance is observable only from a credentialed run's own
453//! `x-ratelimit-*` headers, which is what [`accounting`] fills its per-budget figures from
454//! and what `tests/live.rs` prints at the end of every run.
455//!
456//! [node-limits]: https://docs.github.com/en/graphql/overview/rate-limits-and-node-limits-for-the-graphql-api
457//!
458//! **Where a read-after-write guarantee comes from, since neither of GitHub's two
459//! enumerations of a board can supply one alone.** Resolving a node id is strongly
460//! consistent, so a read by id and a project's own sub-issues are already current. The
461//! other two are not, and they are behind by different amounts and in different directions:
462//!
463//! - GitHub's **issue search** is an index and answers a write made moments ago with the
464//! value from before it — usually for a second or two.
465//! - **`ProjectV2.items`** is a projection GitHub rebuilds behind the write, and an item put
466//! on a board with `addProjectV2ItemById` can be **absent** from it — not present with its
467//! content withheld, absent, with the connection walked to its own `hasNextPage: false` —
468//! for *minutes*, while `Issue.projectItems` names the same membership at once.
469//!
470//! That second one is a measurement rather than a caution. This repository's own
471//! credentialed journey writes a project and waits for the board to report it, then writes a
472//! task and waits for the same thing seconds later on the same board: the project wait is
473//! answered through the search and converged in two or three attempts in each of three runs,
474//! and the task wait is answered through `ProjectV2.items` and converged in none of them
475//! inside thirty. Separately, an item added to a second and larger board was read back by
476//! `Issue.projectItems` on that board's own id while every one of that connection's nine
477//! pages, walked to exhaustion nine minutes after the add, did not name it. Reading a board
478//! through the lagging one alone is what had a board read deny an issue that had certainly
479//! landed on it.
480//!
481//! So [`GitHubProjectsSource::board`] is the **union** of both — each search result still
482//! admitted only on this board's own strongly-consistent `Issue.projectItems`, and neither
483//! enumeration dropped, because only `ProjectV2.items` lists a board draft and only the
484//! search reports what the projection is behind on. What closes the last
485//! gap, the one where both are behind, is [`GitHubProjectsSource::created`]: every read this
486//! source answers is completed with what this process itself wrote, so an item created
487//! seconds ago is reported whether or not GitHub has caught up. Nothing else is remembered,
488//! nothing is written down, and the record dies with the process. **A wait that has to
489//! observe GitHub's own data cannot be answered from that record** — which is why the
490//! credentialed journey asks through a source built afresh, and why the union above rather
491//! than a longer wait is what makes such a wait converge.
492//!
493//! **A narrowed read is the same bargain, stated for each of the three predicates it
494//! answers.** A read carrying a text, metadata or origin predicate asks GitHub's index rather
495//! than walking the board, and every such answer is completed with what this process wrote —
496//! its [`created`](GitHubProjectsSource::created) record and every existing item it wrote,
497//! each filtered by the same predicates as the rest — so an item this command wrote a moment
498//! ago is returned by a query that matches it whether or not the index has caught up. An item
499//! a caller holds the id of is read by that id, with `node(id:)`, which is strongly
500//! consistent. What is left is stated rather than papered over:
501//!
502//! | Read | Finds | Behind by |
503//! | --- | --- | --- |
504//! | text, metadata | the issue search for the phrases | what another process wrote in the last second or two, until GitHub indexes it |
505//! | origin, first read | the board's field filter over the origin field — every carrier, whichever release wrote it | what `ProjectV2.items` is behind on, which the measurements above put in minutes |
506//! | origin, second read | the issue search for the id in the body, where a write of this release mirrors it | a second or two, as any search |
507//! | origin, third read | this process's own writes | nothing |
508//!
509//! So an origin carrier another process added within the last second or two, before either
510//! index has it, can be missing from an origin query, and one written by the release before
511//! this one — its origin in the field alone — can be missing for as long as the board's own
512//! item connection is behind on it. A copy that must not duplicate its own earlier write
513//! relies on the link it records, not on either index. **A board draft is not an issue**, so
514//! a draft is never returned by a text, metadata or origin query, whatever it holds: no search
515//! lists one, the origin lookup drops any the board's own field filter names, and one this
516//! process wrote is not added back either.
517//!
518//! **The origin lives in the board field, and the body holds a mirror of it.** A write that
519//! carries an origin writes it to the `onetaskgraph.origin` text field and also into the
520//! body's metadata slot, so the issue search can find it in seconds. The field is
521//! authoritative: this source reads an item's origin from the field alone, so a slot that
522//! disagrees with it, or holds one where the field holds none, is never read as a second
523//! origin — and the release before this one reads the slot, drops that key's copy for the
524//! field's, and sees the same one origin.
525//!
526//! Filtering happens before paging, so a page of a filtered result is a page of the
527//! survivors rather than the survivors of a page. Label matching and the substring rule a
528//! text candidate is confirmed by answer the same question the same way the local Markdown
529//! source's do; which candidates a text search has to confirm is GitHub's word match, which
530//! is the one place the two sources can answer the same text differently.
531//!
532//! <!-- llmlint: ignore[contracts_have_one_source_or_a_drift_gate] The declaration itself
533//! has one source, `capabilities`, and the note above is the reasoning behind it rather
534//! than a second copy of it: without the three facts recorded here a reader takes the
535//! uniform `Native` for a lie and reverts it. The drift gate on the declaration is this
536//! crate's own capabilities test, which pins every field of it against a fully spelled-out
537//! `Capabilities` literal — a struct with no `Default`, so a field added to the contract
538//! fails to compile there rather than going unasserted. -->
539//! The fixture-server tests above run wherever this crate is selected; the credentialed
540//! lane runs in the same required check, beside them, and can fail it — it verifies the
541//! current schema, then drives every field of the table above against the real board. It builds its own fixture there — two projects, one task filed under each,
542//! one filed under neither, a label on one of the three and a closed status on another —
543//! because that shape is what tells an honoured predicate from an ignored one: a board
544//! holding a single project answers a project filter the same way whether or not this
545//! source applies it, which is exactly how the defect above went unseen.
546//!
547//! That lane writes only to the board `GH_PROJECTS_OWNER` and `GH_PROJECTS_NUMBER` name,
548//! and the scratch repository `GH_PROJECTS_REPOSITORY` names:
549//! `nickderobertis/onetaskgraph-live-scratch`. It refuses the core repository before any
550//! session or request, independently of the live demand. Fix that variable in the machine's
551//! onetaskgraph `secrets.env` or the environment it pushes from, such as ai-orchestrator's
552//! `.env`. The targets are declared once in `onetaskgraph_github_live`, the lane's own
553//! policy crate; absent credentials or nominations otherwise skip.
554//!
555//! GitHub Actions artifacts carry `ci-<run id>-<attempt>-<micros>`, naming their writing
556//! run and attempt; invalid Actions identity refuses before writing. Other runs retain
557//! `<host>-<process>-<micros>`. Own cleanup matches the whole stamp. Machine residue stays
558//! the owning machine's lock sweep's; Linear always keeps that form and is unaffected.
559//! The hourly janitor is cleanup, never a test lane: scratch CI residue is removed only
560//! after its run reads back as `completed`, immediately before the listed-artifact batch.
561//! Failed or incomplete ownership reads preserve residue. A 24-hour waiting period
562//! is a margin, never the ownership authorisation. Cleanup leaves machine stamps
563//! and the board's `onetaskgraph.origin` field untouched.
564//!
565//! # What a session of requests costs, and where the report is
566//!
567//! This source records **every** request it sends into [`accounting::Accounting`], at
568//! `send_once` — the one place a request leaves this crate, which is why a read path added
569//! later is counted without anybody remembering to count it. That is the whole of what this
570//! crate adds to the arrangement; [`accounting`] is where what a record carries, how a
571//! session's spend is arrived at, and what it deliberately does not know are set out.
572//!
573//! What one whole session of the live journey costs, counted that way against this crate's
574//! loopback fixture board, is written down in `session-cost.md` beside this crate — with the
575//! reduction it came out of, and with what it does and does not say about rate-limit points.
576//!
577//! [`GitHubProjectsSource::accounting`] is the read: a snapshot to hold and compare, which
578//! [`accounting::Session::report`] renders the session report from. It is on the ordinary
579//! code path — no environment variable, no feature, no build configuration — because an
580//! instrument nobody switches on measures nothing, and
581//! [`Plugin::build_recording_into`] is how a caller making its own calls beside this
582//! source's counts the whole session rather than this source's share. The credentialed lane
583//! in `tests/live.rs` does exactly that, and prints the report at the end of every run,
584//! passed or failed.
585//!
586//! **A live session refuses to start unless the account can afford it.** Before it does any
587//! of the work it exists to do, the journey makes one request — `GET /rate_limit`, which
588//! GitHub documents as not counting against the REST rate limit and which answers both of
589//! its budgets at once — and starts only if, for each of them, what remains minus this
590//! session's estimated cost is still at least
591//! `onetaskgraph_live::RETAINED_BUFFER` — twenty per cent — of that budget's whole
592//! allowance. A session that cannot **declines**: it did not run, so it is
593//! neither a pass nor a failing assertion, and it says which budget was short, that budget's
594//! limit, what remained, the estimate, the buffer and when it resets — then stops, without
595//! waiting for the budget to come back. The estimate is derived offline from
596//! `tests/fixtures/session-cost.txt` and a cost model stated in `tests/journey/budget.rs`,
597//! which is also where the published rule that model rests on is cited; the accounting
598//! above records the gate's own read like any other request, and
599//! [`accounting::Session::report`] prints the estimate beside what the session really spent.
600//!
601//! **GitHub is the authority on both of its own numbers, and the credentialed lane goes and
602//! asks it.** Everything above computes `nodeCount` and `cost` offline from a document's own
603//! text, which is what lets it run on every platform and on a pull request from a fork with
604//! no credential — and that is what actually stops a regression merging. But an offline
605//! arithmetic can only ever agree with itself: if GitHub changes its rules, this workspace
606//! goes on computing the old answer and nothing notices. So `tests/live.rs` reconciles them.
607//! GitHub's schema exposes `rateLimit(dryRun: true)`, whose `nodeCount` is *"the maximum
608//! number of nodes this query may return"* and whose `cost` is what that document would
609//! spend, both for a document **without executing it**, and the lane asks it for every query
610//! document this source sends, under the largest bindings this source sends, and fails when
611//! GitHub's figure and [`worst_case_node_count`] or [`worst_case_point_cost`] disagree. A
612//! mutation is skipped, because `rateLimit` is a field of `Query` and cannot be asked about
613//! one; the offline pins still cover it. It records what those calls reported about the
614//! account's own allowance, because whether asking is free is a thing to observe rather than
615//! to assume. Two quantities, not one: [`NODE_COUNT_LIMIT`] bounds `nodeCount` per query,
616//! and `cost` is metered against an hourly allowance the accounting above reads off a
617//! credentialed run's own response headers.
618//!
619//! **GitHub has two rate limiters and this source is refused by both, so nothing here
620//! treats them as one thing.** The primary budget is the hourly allowance `gh api
621//! rate_limit` reports; the secondary limiter is a burst limiter over content-generating
622//! requests, and *nothing* reports it. Which one refused decides the operator's next step,
623//! so [`Limiter`] is a type rather than a detail, and it is what [`MIN_MUTATION_INTERVAL_MS`],
624//! [`GitHubProjectsSource::board_cache`] and [`GitHubProjectsSource::graphql`] each answer
625//! one part of.
626#![deny(missing_docs)]
627
628use std::collections::BTreeMap;
629use std::sync::{Arc, Mutex};
630use std::time::Duration;
631
632use chrono::{DateTime, Utc};
633use onetaskgraph_plugin_api::{
634 Capabilities, Classification, ClosedAs, Comment, CommentBody, CommentOrigin, Completion,
635 ContradictedItem, Coverage, Cursor, DependencyEdge, DependencyEndpoint, DependencyKind,
636 DependencySupport, Designator, Direction, Document, DocumentQuery, DocumentVersionCoverage,
637 Health, HeldTask, IssueHolding, IssueRef, ItemKind, ItemWrite, Label, LabelFilter, Location,
638 MetadataKey, Metering, NativeId, NewComment, Page, PageRequest, Priority, Project,
639 ProjectFilter, ProjectQuery, ProjectVersionCoverage, Repository, SecretResolver, SharedClock,
640 SourceError, SourceName, SourcePlugin, Status, StatusCategory, StatusContradiction,
641 StatusMapping, Support, Task, TaskDetailRead, TaskQuery, TaskRef, TaskSource, TaskUpdate,
642 TaskUpdateOutcome, TextFields, TextQuery, UnmappedStatus, UpdatedField, VersionCoverage,
643 WaitCause, WriteJournal, WriteSupport, system_clock,
644};
645use reqwest::{Client, StatusCode, Url};
646use schemars::{Schema, schema_for};
647use secrecy::{ExposeSecret, SecretString};
648use serde::{Deserialize, Serialize};
649use serde_json::{Value, json};
650
651pub mod accounting;
652mod assets;
653pub mod change_token;
654mod expected;
655pub use expected::{CONTEXT_VERSION, WHOLE_MEMBERS, context_shapes};
656mod pacing;
657mod visibility;
658
659use accounting::Accounting;
660
661/// The registry name for this plugin.
662pub const KIND: &str = "github-projects";
663/// GitHub's maximum connection page size.
664pub const MAX_PAGE_SIZE: u32 = 100;
665/// Every page of a board-scoped narrowing search: 20 rows, one point of SEARCH_ISSUES, the
666/// most one point buys. GitHub prices that document by rows, so pages of 20 cost what pages
667/// of 100 cost per row, and a page of fewer than 20 costs the same one point.
668pub const SEARCH_PAGE_SIZE: u32 = 20;
669/// How many items one [`graphql::ISSUE_DETAILS`] request reads, each with the first page of
670/// its comments: the largest batch the node-count model prices at one point.
671///
672/// Each aliased item is resolved once, and what GitHub charges for it is the connections
673/// under it — its labels, its page of board memberships, the field values of each of those
674/// three memberships, and its comments: six requests' worth of the aggregate GitHub divides
675/// by a hundred and rounds. Twenty-four items come to 144, which rounds to one point;
676/// twenty-five come to 150, which rounds to two. `tests/point_cost.rs` prices the document at
677/// one point and fails if one item more would still be priced at one.
678pub const DETAIL_BATCH: usize = 24;
679/// How many issue references one [`graphql::ISSUES_BY_REFERENCE`] request reads.
680///
681/// Each reference asks GitHub for one connection — a page of its board memberships, each with
682/// its `Status` value read by name rather than through a connection of field values — so fifty
683/// come to fifty requests' worth of the aggregate GitHub divides by a hundred, which is its
684/// one-point minimum, and the batch a landing's closures are read in costs one point per fifty
685/// issues per source. `tests/point_cost.rs` prices the document at one point.
686pub const REFERENCE_BATCH: usize = 50;
687/// How many items one [`graphql::ISSUE_COPY_DETAILS`] request reads: each with the first page
688/// of its comments and its `blockedBy`, the largest batch the node-count model prices at one
689/// point.
690///
691/// The six requests' worth an item of [`DETAIL_BATCH`] costs, and its `blockedBy` connection
692/// besides: seven. Twenty-one items come to 147, which rounds to one point; twenty-two come to
693/// 154, which rounds to two. `tests/point_cost.rs` prices the document at one point and fails
694/// if one item more would still be priced at one.
695pub const COPY_DETAIL_BATCH: usize = 21;
696/// How many mutations one aliased request of a copy's creates, closes and updates carries:
697/// [`graphql::CREATE_ISSUES`], [`graphql::CLOSE_ISSUES`] and [`graphql::UPDATE_ISSUES`].
698///
699/// Eleven is what GitHub was seen to accept in one request — the timing spike
700/// (`board-copy-speed-2026-10-18-spike-copy-timing-report`) sent eleven aliased mutations of four
701/// kinds, three `createIssue`, three `addProjectV2ItemById`, three field updates, an
702/// `addSubIssue` and an `addBlockedBy`, and every one ran — and no more was established.
703pub const MUTATION_BATCH: usize = 11;
704/// How many aliased writes one request of a copy's board field values and blocked-by edges
705/// carries: [`graphql::WRITE_FIELD_VALUES`], [`graphql::ADD_BLOCKED_BYS`] and, for a request
706/// holding both kinds, [`graphql::WRITE_FIELDS_AND_EDGES`].
707///
708/// Nineteen is the most aliases the feasibility spike
709/// (`board-copy-gaps-2026-10-24-spike-feasibility-report`) sent in one mutation request and
710/// saw GitHub price at the one point every mutation request costs; no larger request was
711/// established, so a copy holding more is split.
712pub const WRITE_BATCH: usize = 19;
713/// How many issues one [`graphql::DELETE_ISSUES`] request deletes: one request of eleven
714/// aliased `deleteIssue` met `RESOURCE_LIMITS_EXCEEDED` from the eighth alias on, and batches
715/// of five succeeded (the feasibility spike's undo-batching run).
716pub const DELETE_BATCH: usize = 5;
717/// How many origins one [`graphql::ORIGIN_LOOKUPS`] request asks about: the nine records of a
718/// plan and the project's document, and no more, since each carries a page of the board's field
719/// filter and one of the issue search.
720pub const ORIGIN_BATCH: usize = 10;
721
722/// The most nodes any one document this source sends may be asked to return.
723///
724/// GitHub's own published per-query ceiling, taken from
725/// [`github_graphql_node_count::NODE_LIMIT`] rather than written out again here, so this
726/// workspace cannot hold a stale copy of somebody else's number. A query above it is
727/// **refused before it is executed**, whoever is asking and whatever board they are
728/// asking about — so this is a bound on the documents rather than a budget that runs out.
729///
730/// This is `nodeCount`, the maximum number of nodes *one query may return*. It is not
731/// `cost`, the rate-limit points a call spends against an hourly allowance shared by
732/// everything the credential does — two numbers against two limits, and this constant
733/// bounds only the first. The second is computed offline too, per document:
734/// [`worst_case_point_cost`], pinned for every document in [`graphql::DOCUMENTS`] by
735/// `tests/point_cost.rs`, and reconciled against GitHub's own `cost` by the credentialed
736/// lane. There is no constant like this one to hold a price under, because points are an
737/// hourly allowance rather than a per-call bound.
738///
739/// Neither is a session's price. What `session-cost.md` records of a whole session is its
740/// **requests** and its **worst-case nodes**; what a whole session spends in points is
741/// reported only by a credentialed run's own `x-ratelimit-*` headers, through
742/// [`accounting`]. The module section on the three ways this source reaches an item says how
743/// the count is arrived at, and which of the page sizes below decide it.
744pub const NODE_COUNT_LIMIT: u64 = github_graphql_node_count::NODE_LIMIT;
745
746/// Nested connection size for the connections that hang off one item.
747///
748/// It multiplies through every document that reaches an item under a page — the count
749/// rules multiply down a nested path — so it is the constant [`NODE_COUNT_LIMIT`] is most
750/// sensitive to. `tests/node_count.rs` is what holds the pair together: it recomputes
751/// every document under these constants and fails naming any that reaches the limit, so
752/// raising this is caught there rather than by GitHub.
753const NESTED_PAGE_SIZE: u32 = 50;
754/// How many of one issue's board memberships are read when an issue is reached directly.
755///
756/// An issue reached through a search or through its own node id carries its board half in
757/// `Issue.projectItems`, and only the entry for *this* board is read. This connection sits
758/// under a page of issues, so every point of it multiplies through the whole document and
759/// is paid for whether or not any issue is on a second board — which is why it is
760/// deliberately far smaller than [`NESTED_PAGE_SIZE`].
761///
762/// **Three, because what a page misses is now recovered rather than refused**, and the
763/// recovery is what the value is chosen against. An issue whose entry for this board sits
764/// past this page costs one further request — [`graphql::ISSUE_BOARD_ITEMS`], resumed from
765/// that page's own cursor — so the value trades a bound every read pays for a request only
766/// a multi-board issue pays. At one, a deployment whose issues commonly sit on two or more
767/// boards would pay that request *per issue*, which is order N against the one page per
768/// hundred issues a read costs today. At three it is only reached by an issue on four or
769/// more boards at once, which keeps the recovery path exceptional rather than routine for
770/// a plausible deployment.
771const BOARD_ITEMS_PAGE_SIZE: u32 = 3;
772/// How many carriers of one copy origin one page of [`graphql::ORIGIN_LOOKUP`] asks each of
773/// its two connections for.
774///
775/// An origin names one item, so the answer an origin lookup expects is one carrier, and a
776/// second is a duplicate a copy already takes the first of. Both connections are walked to
777/// exhaustion whatever this is, so it decides how many requests an unusual answer costs and
778/// never what the answer is. It is small because every point of it is paid on every lookup,
779/// and a copy makes one lookup per item it has no link for: at three, ten lookups cost fewer
780/// worst-case nodes than the one whole-board read they replaced.
781const ORIGIN_PAGE_SIZE: u32 = 3;
782/// How many carriers of each origin the batched [`graphql::ORIGIN_LOOKUPS`] asks each of its two
783/// connections for: one, the one carrier an origin names.
784///
785/// A second carrier still shows: the page then says there is more, and that origin is left to
786/// its own [`graphql::ORIGIN_LOOKUP`], which walks every page at `ORIGIN_PAGE_SIZE` — so a
787/// duplicate is found exactly as before, by the lookup that reads all of them. The feasibility
788/// spike (`board-copy-gaps-2026-10-24-spike-feasibility-report`) priced the batch of ten
789/// origins at two points with a first page of three and at one with a first page of one.
790const ORIGIN_LOOKUPS_PAGE_SIZE: u32 = 1;
791
792/// How many of one project's sub-issues one [`graphql::SUB_ISSUES`] page asks for: the most
793/// one point buys, since every row carries six connections GitHub charges for once per row.
794/// `session-cost.md` derives it; `tests/point_cost.rs` prices the document at one point and
795/// fails if one row more would still be priced at one.
796pub const MEMBER_PAGE_SIZE: u32 = 23;
797
798pub use github_graphql_node_count::{NodeCountError, Variables};
799
800/// The largest value this source can bind to each page-size variable its documents name.
801///
802/// Every `first:` in [`graphql`] reads one of these six, and each is capped at the
803/// constant above it wherever a caller's own limit could reach it — `$first` at
804/// [`MAX_PAGE_SIZE`], `$nestedFirst` at `NESTED_PAGE_SIZE`, `$boardItems` at
805/// `BOARD_ITEMS_PAGE_SIZE`, `$originFirst` at `ORIGIN_PAGE_SIZE`, `$lookupFirst` at
806/// `ORIGIN_LOOKUPS_PAGE_SIZE`, `$memberFirst` at [`MEMBER_PAGE_SIZE`]. So this is the worst case a caller can drive this source to,
807/// not one configuration of it, which is what makes a bound computed under it a bound on
808/// every read.
809pub fn largest_page_sizes() -> Variables {
810 Variables::from([
811 ("first".to_owned(), MAX_PAGE_SIZE),
812 ("nestedFirst".to_owned(), NESTED_PAGE_SIZE),
813 ("boardItems".to_owned(), BOARD_ITEMS_PAGE_SIZE),
814 ("originFirst".to_owned(), ORIGIN_PAGE_SIZE),
815 ("lookupFirst".to_owned(), ORIGIN_LOOKUPS_PAGE_SIZE),
816 ("memberFirst".to_owned(), MEMBER_PAGE_SIZE),
817 ])
818}
819
820/// The most nodes `document` could be asked to return, by GitHub's published rules.
821///
822/// Computed offline from the document's own text under [`largest_page_sizes`] — no
823/// network, no credential and no schema — by
824/// [`github_graphql_node_count::node_count`], which is where the rules themselves live.
825/// A document at or above [`NODE_COUNT_LIMIT`] is one GitHub refuses before executing, so
826/// this is what a check holds every document in [`graphql::DOCUMENTS`] below.
827///
828/// # Errors
829///
830/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
831/// no single operation, or binds a page size this source does not name — each of which is
832/// a defect in the document rather than a number.
833pub fn worst_case_node_count(document: &str) -> Result<u64, NodeCountError> {
834 node_count(document, &largest_page_sizes())
835}
836
837/// The most rate-limit points one call of `document` could spend, by GitHub's published
838/// rules.
839///
840/// Computed offline from the document's own text under [`largest_page_sizes`] — no
841/// network, no credential and no schema — by
842/// [`github_graphql_node_count::point_cost`], which is where the rules themselves live.
843/// This is `cost`, metered **per hour** against the allowance one credential shares across
844/// everything it does; it is not `nodeCount`, which is [`worst_case_node_count`] and is
845/// bounded per query by [`NODE_COUNT_LIMIT`]. There is no per-call ceiling to hold this
846/// under, so what `tests/point_cost.rs` does with it is pin every document in
847/// [`graphql::DOCUMENTS`] at what it costs, and the credentialed lane reconciles those
848/// figures against GitHub's own reported `cost`.
849///
850/// # Errors
851///
852/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
853/// no single operation, or binds a page size this source does not name — each of which is
854/// a defect in the document rather than a number.
855pub fn worst_case_point_cost(document: &str) -> Result<u64, NodeCountError> {
856 github_graphql_node_count::point_cost(document, &largest_page_sizes())
857}
858
859/// The most nodes `document` could be asked to return under `variables`.
860///
861/// [`worst_case_node_count`] is this under [`largest_page_sizes`], and the accounting in
862/// [`accounting`] is this under the bindings one request really sent — one spelling of the
863/// calculation, so a bound checked offline and a cost recorded at run time cannot come to
864/// disagree. The rules themselves live in [`github_graphql_node_count::node_count`].
865///
866/// # Errors
867///
868/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
869/// no single operation, or binds a page size `variables` does not name.
870pub fn node_count(document: &str, variables: &Variables) -> Result<u64, NodeCountError> {
871 github_graphql_node_count::node_count(document, variables)
872}
873
874/// The issue-title prefix that makes a board issue a document.
875///
876/// A GitHub Projects board has no document type — it holds issues — so the discriminator
877/// is the title, and this is the whole of it: an issue whose title begins with these bytes
878/// is a document and every other issue is the task or project the sub-issue rule makes it.
879///
880/// It is spelled **once**, here, and read rather than restated everywhere else — including
881/// by the shared journeys, which take it from this constant so a board fixture cannot
882/// drift from what this source reads. `docs/metadata.md` records the two consequences that
883/// are not obvious from the bytes: the reported title has this prefix taken off, exactly
884/// as the body's metadata slot is taken off `content`, and this prefix is read *before*
885/// the sub-issue rule, so a design issue with no sub-issues is never an empty project.
886pub const DESIGN_TITLE_PREFIX: &str = "DESIGN: ";
887
888/// Exact GraphQL query documents issued by this plugin.
889///
890/// Keeping the production documents here lets the pinned-schema test validate the same
891/// bytes that are sent to GitHub, rather than a test-only copy which could drift
892/// independently. [`STATUS_OPTIONS_UPDATE`] is the sole document that may rewrite a board
893/// field, and its guarded caller always supplies the complete existing option set with ids.
894pub mod graphql {
895 /// The board half of one item: the field values every document here reads it from.
896 ///
897 /// A macro for the same reason [`board_issue!`] below is one, a level further in. This
898 /// selection is needed by that fragment, by [`BOARD`] under the board's own `items`,
899 /// and by [`ISSUE_BOARD_ITEMS`] under a membership walk — and all three have to produce
900 /// *the same value*, because
901 /// [`GitHubProjectsSource::resolve`](super::GitHubProjectsSource) reads them through
902 /// one path. Three spellings of it is what would drift, so there is one.
903 ///
904 /// The `Status` option and this source's own origin text field are the whole of it. It
905 /// selects no `ProjectV2ItemFieldLabelValue`: GitHub derives that field from the item's
906 /// content, so it holds nothing the content's own `labels` do not already say, and it
907 /// would sit a label connection two page sizes deep.
908 macro_rules! board_item_values {
909 () => {
910 r#"fieldValues(first:$nestedFirst){nodes{
911 ... on ProjectV2ItemFieldSingleSelectValue{name field{
912 ... on ProjectV2SingleSelectField{id name options{id name}}
913 }}
914 ... on ProjectV2ItemFieldTextValue{text field{... on ProjectV2Field{id name}}}
915 }pageInfo{hasNextPage}}"#
916 };
917 }
918
919 /// The variable every document reading an issue declares for the relationship events its
920 /// change token counts, with its value as the default, so no caller binds it.
921 ///
922 /// A variable rather than a literal list because every argument a production document
923 /// passes is a declared variable, which `tests/schema.rs` holds them to, and every member of
924 /// its default to the pinned enum. Its default is the six event types GitHub records a
925 /// relationship added or removed with: a dependency on either end, as `blockedBy` and
926 /// `blocking`, so one a person makes from the far end moves this end's count too; and a
927 /// project membership on the task, as its parent, which GitHub records there whichever
928 /// issue the move was made from. A sub-issue's events on its parent are not counted: a
929 /// project carries no task token, and a copy writes nothing of a task its own sub-issues
930 /// decide. [`super::change_token`] says why the count is in the token.
931 macro_rules! relationship_events_variable {
932 () => {
933 ",$relationshipEvents:[IssueTimelineItemsItemType!]=[BLOCKED_BY_ADDED_EVENT,BLOCKED_BY_REMOVED_EVENT,BLOCKING_ADDED_EVENT,BLOCKING_REMOVED_EVENT,PARENT_ISSUE_ADDED_EVENT,PARENT_ISSUE_REMOVED_EVENT]"
934 };
935 }
936
937 /// Everything this source reads about one issue, wherever it reaches that issue.
938 ///
939 /// A macro rather than a constant so the three documents below can `concat!` it: one
940 /// spelling of these fields is what makes an issue read through the board-scoped
941 /// search, through its own node id, and through its project's sub-issue relationship
942 /// resolve to *the same* item, which is the whole of what
943 /// [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource) relies on.
944 ///
945 /// `projectItems` is what carries the board half of an issue: the board item's own id
946 /// and the [`board_item_values!`] above — the `Status` option and this source's origin
947 /// text field — that a `ProjectV2.items` read used to carry. It is asked for on the
948 /// issue rather than on the board, which is what makes the cost of a read proportional
949 /// to what was asked for instead of to the board's size.
950 ///
951 /// It carries a *page* of that connection, at `BOARD_ITEMS_PAGE_SIZE`, and its
952 /// `endCursor` is what [`ISSUE_BOARD_ITEMS`] resumes from when this board's entry is
953 /// not on that page: a page here is where the search for the entry starts rather than
954 /// where it ends.
955 ///
956 /// It does **not** select the board's `Labels` field value, and that is the whole of
957 /// what keeps the three documents below under [`NODE_COUNT_LIMIT`](super::NODE_COUNT_LIMIT):
958 /// a label connection there sits under `fieldValues` under `projectItems` under a page
959 /// of issues, spending `$nestedFirst` twice down one path, and took
960 /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] to 2,556,100 nodes against a limit of 500,000.
961 /// No label is lost — this is a fragment `on Issue`, whose own `labels` are selected
962 /// above, and that connection is where every label this source reports comes from. No
963 /// document in this module selects the board field any longer, [`BOARD`] included; the
964 /// module documentation records why nothing it could have held is lost.
965 macro_rules! board_issue {
966 () => {
967 concat!(
968 r#" fragment BoardIssue on Issue{__typename id number title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total} commentCount:comments{totalCount} relationshipEvents:timelineItems(itemTypes:$relationshipEvents){totalCount}
969 labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}
970 projectItems(first:$boardItems){nodes{id updatedAt project{id number}
971 "#,
972 board_item_values!(),
973 r#"}pageInfo{hasNextPage endCursor}}}"#
974 )
975 };
976 }
977
978 /// Every issue of one board, found by a search scoped to that board.
979 ///
980 /// This is how the projects a board holds are listed, and it selects no `items`
981 /// connection on `ProjectV2`: the board is a *qualifier of the search* rather than a
982 /// container walked page by page, so nothing nested inside a board item is paid for.
983 /// Which of the issues it returns is a project is then read off `parent` — GitHub
984 /// accepts `-has:parent` as a search qualifier and silently ignores it, so the
985 /// discriminator has to be applied to the field, which is a scalar on the issue and
986 /// costs nothing.
987 pub const SEARCH_ISSUES: &str = concat!(
988 r#"query($search:String!,$type:SearchType!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
989 relationship_events_variable!(),
990 r#"){
991 search(query:$search,type:$type,first:$first,after:$after){
992 pageInfo{hasNextPage endCursor}
993 nodes{__typename ...BoardIssue}
994 }
995 }"#,
996 board_issue!()
997 );
998
999 /// What a dependency read selects of each far end: enough to say which kind of item it
1000 /// is, its body included for the kind marker.
1001 macro_rules! related_issue {
1002 () => {
1003 " fragment Related on Issue{id title body parent{id} subIssuesSummary{total}}"
1004 };
1005 }
1006
1007 /// One issue by its own node id, which is what a qualified id names here — with what a
1008 /// write of it needs and the issue does not carry in `board_issue!`: the field
1009 /// definitions of the boards it sits on, and the far ends of its `blockedBy`.
1010 ///
1011 /// Strongly consistent, unlike the search above: GitHub's issue search is an index and
1012 /// answers a write made moments ago with the value from before it, and resolving a node
1013 /// id does not.
1014 ///
1015 /// **Why those two ride here and not on the fragment.** A copy or an update of an item
1016 /// reads it by its own id, and with them that one read answers everything the write
1017 /// needs: which option ids the board's `Status` and `Priority` fields hold — so no
1018 /// [`BOARD_FIELDS`] — and which issues block it, with each one's kind — so no
1019 /// [`ISSUE_DEPENDENCIES`]. On `board_issue!` they would sit under the hundred-issue
1020 /// pages of [`SEARCH_ISSUES`] and [`SUB_ISSUES`], multiplying both documents' price. Here
1021 /// they sit under one item, and this read is still one point.
1022 pub const ISSUE: &str = concat!(
1023 r#"query($id:ID!,$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1024 relationship_events_variable!(),
1025 r#"){
1026 node(id:$id){__typename ...BoardIssue ... on Issue{
1027 boards:projectItems(first:$boardItems){nodes{project{id number fields(first:$nestedFirst){nodes{
1028 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1029 ... on ProjectV2Field{__typename id name dataType}
1030 }pageInfo{hasNextPage}}}}}
1031 blockedBy(first:$first){nodes{...Related}pageInfo{hasNextPage endCursor}}
1032 }}
1033 }"#,
1034 board_issue!(),
1035 related_issue!()
1036 );
1037
1038 /// One document by its own node id, as [`ISSUE`] reads one, with the project it is filed
1039 /// under and that project's members beside it: the project issue whole, and a page of its
1040 /// sub-issues, each as [`SUB_ISSUES`] carries one but with no page of what blocks it.
1041 ///
1042 /// What a guarded write of a document's metadata reads, and what reading a document by its
1043 /// id is: a write to a board the boundary screens is held to who may read the document,
1044 /// which is the project's and every member's classification as much as its own, and here
1045 /// the one request that holds the version the write is compared against answers all of it.
1046 /// Its members carry no edges, which nothing classifying them reads.
1047 pub const DOCUMENT_IN_PROJECT: &str = concat!(
1048 r#"query($id:ID!,$first:Int!,$memberFirst:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1049 relationship_events_variable!(),
1050 r#"){
1051 node(id:$id){__typename ...BoardIssue ... on Issue{
1052 boards:projectItems(first:$boardItems){nodes{project{id number fields(first:$nestedFirst){nodes{
1053 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1054 ... on ProjectV2Field{__typename id name dataType}
1055 }pageInfo{hasNextPage}}}}}
1056 blockedBy(first:$first){nodes{...Related}pageInfo{hasNextPage endCursor}}
1057 project:parent{__typename ...BoardIssue
1058 subIssues(first:$memberFirst){pageInfo{hasNextPage endCursor} nodes{__typename ...BoardIssue}}
1059 }
1060 }}
1061 }"#,
1062 board_issue!(),
1063 related_issue!()
1064 );
1065
1066 /// One project's tasks: the sub-issues of the issue that project is, each with a page of
1067 /// what blocks it.
1068 ///
1069 /// Paged at `$memberFirst` — [`MEMBER_PAGE_SIZE`](super::MEMBER_PAGE_SIZE), the most rows
1070 /// one point buys — rather than at `$first`, because every row carries six connections and
1071 /// a hundred-row page cost six points to list a project of ten.
1072 ///
1073 /// The work this costs is the project's own size. Nothing about it grows as the board
1074 /// gains projects, or as those projects gain tasks.
1075 ///
1076 /// **Why `blockedBy` rides here and on no other page of issues.** What reads a project's
1077 /// tasks reads their edges next — `project graph` draws them, a copy carries them — and
1078 /// without them here that is one [`ISSUE_DEPENDENCIES`] per task, so the requests a graph
1079 /// costs grow with its tasks rather than with the pages of them. Carried here, a task
1080 /// blocked by no more than `$nestedFirst` issues answers its forward edges from this read,
1081 /// exactly as an [`ISSUE`] read of it does, and only one blocked by more is asked again.
1082 /// It adds a connection under each issue of the page — one rate-limit point per page,
1083 /// and `$nestedFirst` nodes per issue — and is kept off [`SEARCH_ISSUES`], whose pages
1084 /// answer questions that never read an edge.
1085 ///
1086 /// **The project issue itself rides here too**, with the page of what blocks it, because
1087 /// the reader of a project's tasks reads the project next: a copy writing the project reads
1088 /// it and its edges before it writes them. Carried on the request that already names the
1089 /// project, the first page answers that read and the dependency read after it, at the
1090 /// state GitHub answered in this command, rather than spending one more read on an item
1091 /// the cache's token cannot vouch for whole. Each sub-issue is kept the same way, so a
1092 /// copy's read of a task this listing carried is answered by it rather than sent again.
1093 /// Only a copy's read-before-write takes them; every other read by id reads GitHub.
1094 pub const SUB_ISSUES: &str = concat!(
1095 r#"query($id:ID!,$memberFirst:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1096 relationship_events_variable!(),
1097 r#"){
1098 node(id:$id){__typename ...BoardIssue
1099 ... on Issue{blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}
1100 subIssues(first:$memberFirst,after:$after){
1101 pageInfo{hasNextPage endCursor}
1102 nodes{__typename ...BoardIssue ... on Issue{
1103 blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}
1104 }}
1105 }}}
1106 }"#,
1107 board_issue!(),
1108 related_issue!()
1109 );
1110
1111 /// What a read of the board's own `items` selects of each item's content.
1112 ///
1113 /// A macro for the reason [`board_item_values!`] is one: [`BOARD`] and [`ORIGIN_LOOKUP`]
1114 /// both walk `ProjectV2.items` and hand each item to one resolver, so they select its
1115 /// content by one spelling.
1116 macro_rules! board_item_content {
1117 () => {
1118 r#" content{
1119 ... on Issue{__typename id number title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total} commentCount:comments{totalCount} relationshipEvents:timelineItems(itemTypes:$relationshipEvents){totalCount} labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}}
1120 ... on PullRequest{__typename id}
1121 ... on DraftIssue{__typename id title body createdAt updatedAt}
1122 }"#
1123 };
1124 }
1125
1126 /// Reads the board's fields and one page of its items.
1127 pub const BOARD: &str = concat!(
1128 r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$nestedFirst:Int!,$duplicates:Boolean!"#,
1129 relationship_events_variable!(),
1130 r#"){
1131 owner:repositoryOwner(login:$owner){
1132 ... on ProjectV2Owner{projectV2(number:$number){...Board}}
1133 }
1134 } fragment Board on ProjectV2 { id title
1135 fields(first:$nestedFirst){nodes{
1136 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1137 ... on ProjectV2Field{__typename id name dataType}
1138 }pageInfo{hasNextPage}}
1139 items(first:$first,after:$after){nodes{id updatedAt "#,
1140 board_item_values!(),
1141 board_item_content!(),
1142 r#"} pageInfo{hasNextPage endCursor}}
1143 }"#
1144 );
1145
1146 /// Every carrier of one copy origin, by two reads in one request, and nothing else of
1147 /// the board.
1148 ///
1149 /// **`originItems`** is the board's own items narrowed by its own field filter —
1150 /// `ProjectV2.items(query:)`, which GitHub's schema declares as "Search query for
1151 /// filtering items" — to those whose `onetaskgraph.origin` text field holds the
1152 /// qualified id, quoted. It reads the field every carrier already holds, whichever release
1153 /// wrote it, and matches it exactly: measured on 2026-09-29 against a 394-item board,
1154 /// the quoted, the unquoted and the bare-value spellings each returned exactly the one
1155 /// carrier and a prefix of the value returned none. It is `ProjectV2.items`, so it lags a
1156 /// fresh `addProjectV2ItemById` the way that connection does.
1157 ///
1158 /// **`search`** is the board-scoped issue search for the same id as a quoted phrase in
1159 /// the body, which is where this source mirrors the origin into its metadata slot. GitHub
1160 /// indexes that comment, and the index catches up with a write in a second or two rather
1161 /// than in minutes, so it finds a carrier another process wrote that the first read is
1162 /// still behind on.
1163 ///
1164 /// Each connection pages at `$originFirst`, its own small size — see `ORIGIN_PAGE_SIZE`
1165 /// — and resumes from its own cursor; a connection already walked to its end is resumed
1166 /// from its last cursor, which answers an empty page. Every candidate either read returns
1167 /// is confirmed against its own origin field before it is reported, so a token match of
1168 /// the search or anything else the filter admits never is.
1169 ///
1170 /// The root is aliased `originItems` rather than `owner`, so nothing counting the board's
1171 /// own whole reads counts this one among them.
1172 pub const ORIGIN_LOOKUP: &str = concat!(
1173 r#"query($owner:String!,$number:Int!,$filter:String!,$search:String!,$type:SearchType!,$originFirst:Int!,$itemsAfter:String,$searchAfter:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1174 relationship_events_variable!(),
1175 r#"){
1176 originItems:repositoryOwner(login:$owner){
1177 ... on ProjectV2Owner{projectV2(number:$number){
1178 items(first:$originFirst,after:$itemsAfter,query:$filter){nodes{id updatedAt "#,
1179 board_item_values!(),
1180 board_item_content!(),
1181 r#"} pageInfo{hasNextPage endCursor}}
1182 }}
1183 }
1184 search(query:$search,type:$type,first:$originFirst,after:$searchAfter){
1185 pageInfo{hasNextPage endCursor}
1186 nodes{__typename ...BoardIssue}
1187 }
1188 }"#,
1189 board_issue!()
1190 );
1191
1192 /// Every closed item of the board, by the board's own field filter — what
1193 /// `task status reconcile` reads, and nothing else of the board.
1194 ///
1195 /// **`ProjectV2.items(query:)` narrowed by `$filter`**, which this source binds to
1196 /// `is:closed` alone, so a board whose open items are most of it is not read whole to find
1197 /// the closed ones. It is narrowed rather than trusted: every item it answers is confirmed
1198 /// closed, with a reason and a card that contradict each other, from the item node it
1199 /// already carries, so a filter that also let open items through would cost reads and
1200 /// write nothing wrong.
1201 ///
1202 /// The board's own id and field definitions ride along, as [`BOARD`] carries them, so the
1203 /// option writes that follow need no read of the board. The root is aliased `closedItems`
1204 /// rather than `owner`, so nothing counting the board's own whole reads counts this one
1205 /// among them.
1206 pub const CLOSED_ITEMS: &str = concat!(
1207 r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$filter:String!,$nestedFirst:Int!,$duplicates:Boolean!"#,
1208 relationship_events_variable!(),
1209 r#"){
1210 closedItems:repositoryOwner(login:$owner){
1211 ... on ProjectV2Owner{projectV2(number:$number){id
1212 fields(first:$nestedFirst){nodes{
1213 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1214 ... on ProjectV2Field{__typename id name dataType}
1215 }pageInfo{hasNextPage}}
1216 items(first:$first,after:$after,query:$filter){nodes{id updatedAt "#,
1217 board_item_values!(),
1218 board_item_content!(),
1219 r#"} pageInfo{hasNextPage endCursor}}
1220 }}
1221 }
1222 }"#
1223 );
1224
1225 /// One alias of [`ISSUES_BY_REFERENCE`]: the issue `$owner<n>/$name<n>#$number<n>` names.
1226 macro_rules! reference_alias {
1227 ($n:literal) => {
1228 concat!(
1229 "\n r",
1230 stringify!($n),
1231 ":repository(owner:$owner",
1232 stringify!($n),
1233 ",name:$name",
1234 stringify!($n),
1235 "){issue(number:$number",
1236 stringify!($n),
1237 "){...HeldIssue}}"
1238 )
1239 };
1240 }
1241
1242 /// [`REFERENCE_BATCH`](super::REFERENCE_BATCH) issues at once, each by its repository and
1243 /// number — the one read `task status complete` makes of a source, and nothing else of
1244 /// the board: no `items`, no search and no project read.
1245 ///
1246 /// **Its own fragment, lighter than `board_issue!`, and that is what makes fifty
1247 /// references one point.** It selects no labels, and of each board membership only the
1248 /// `Status` value, by name, with `fieldValueByName` — so each alias asks GitHub for one
1249 /// connection, its page of memberships, where the whole fragment asks for five. What a
1250 /// completion writes is a card and, at most, the issue's state, so the labels and the other
1251 /// field values it leaves out are never what it needs; an item read this way is held for
1252 /// that write alone and never answers another read. An issue whose entry for this board is
1253 /// past its first page of memberships is walked with [`ISSUE_BOARD_ITEMS`], exactly as
1254 /// every other read of an issue recovers one.
1255 ///
1256 /// Fixed-size, as [`ISSUE_DETAILS`] is, so there is one document to price: a batch of
1257 /// fewer references binds the slots it has none for to its last one.
1258 pub const ISSUES_BY_REFERENCE: &str = concat!(
1259 r#"query($owner0:String!,$name0:String!,$number0:Int!,$owner1:String!,$name1:String!,$number1:Int!,$owner2:String!,$name2:String!,$number2:Int!,$owner3:String!,$name3:String!,$number3:Int!,$owner4:String!,$name4:String!,$number4:Int!,$owner5:String!,$name5:String!,$number5:Int!,$owner6:String!,$name6:String!,$number6:Int!,$owner7:String!,$name7:String!,$number7:Int!,$owner8:String!,$name8:String!,$number8:Int!,$owner9:String!,$name9:String!,$number9:Int!,$owner10:String!,$name10:String!,$number10:Int!,$owner11:String!,$name11:String!,$number11:Int!,$owner12:String!,$name12:String!,$number12:Int!,$owner13:String!,$name13:String!,$number13:Int!,$owner14:String!,$name14:String!,$number14:Int!,$owner15:String!,$name15:String!,$number15:Int!,$owner16:String!,$name16:String!,$number16:Int!,$owner17:String!,$name17:String!,$number17:Int!,$owner18:String!,$name18:String!,$number18:Int!,$owner19:String!,$name19:String!,$number19:Int!,$owner20:String!,$name20:String!,$number20:Int!,$owner21:String!,$name21:String!,$number21:Int!,$owner22:String!,$name22:String!,$number22:Int!,$owner23:String!,$name23:String!,$number23:Int!,$owner24:String!,$name24:String!,$number24:Int!,$owner25:String!,$name25:String!,$number25:Int!,$owner26:String!,$name26:String!,$number26:Int!,$owner27:String!,$name27:String!,$number27:Int!,$owner28:String!,$name28:String!,$number28:Int!,$owner29:String!,$name29:String!,$number29:Int!,$owner30:String!,$name30:String!,$number30:Int!,$owner31:String!,$name31:String!,$number31:Int!,$owner32:String!,$name32:String!,$number32:Int!,$owner33:String!,$name33:String!,$number33:Int!,$owner34:String!,$name34:String!,$number34:Int!,$owner35:String!,$name35:String!,$number35:Int!,$owner36:String!,$name36:String!,$number36:Int!,$owner37:String!,$name37:String!,$number37:Int!,$owner38:String!,$name38:String!,$number38:Int!,$owner39:String!,$name39:String!,$number39:Int!,$owner40:String!,$name40:String!,$number40:Int!,$owner41:String!,$name41:String!,$number41:Int!,$owner42:String!,$name42:String!,$number42:Int!,$owner43:String!,$name43:String!,$number43:Int!,$owner44:String!,$name44:String!,$number44:Int!,$owner45:String!,$name45:String!,$number45:Int!,$owner46:String!,$name46:String!,$number46:Int!,$owner47:String!,$name47:String!,$number47:Int!,$owner48:String!,$name48:String!,$number48:Int!,$owner49:String!,$name49:String!,$number49:Int!,$boardItems:Int!,$statusField:String!,$duplicates:Boolean!"#,
1260 relationship_events_variable!(),
1261 r#"){"#,
1262 reference_alias!(0),
1263 reference_alias!(1),
1264 reference_alias!(2),
1265 reference_alias!(3),
1266 reference_alias!(4),
1267 reference_alias!(5),
1268 reference_alias!(6),
1269 reference_alias!(7),
1270 reference_alias!(8),
1271 reference_alias!(9),
1272 reference_alias!(10),
1273 reference_alias!(11),
1274 reference_alias!(12),
1275 reference_alias!(13),
1276 reference_alias!(14),
1277 reference_alias!(15),
1278 reference_alias!(16),
1279 reference_alias!(17),
1280 reference_alias!(18),
1281 reference_alias!(19),
1282 reference_alias!(20),
1283 reference_alias!(21),
1284 reference_alias!(22),
1285 reference_alias!(23),
1286 reference_alias!(24),
1287 reference_alias!(25),
1288 reference_alias!(26),
1289 reference_alias!(27),
1290 reference_alias!(28),
1291 reference_alias!(29),
1292 reference_alias!(30),
1293 reference_alias!(31),
1294 reference_alias!(32),
1295 reference_alias!(33),
1296 reference_alias!(34),
1297 reference_alias!(35),
1298 reference_alias!(36),
1299 reference_alias!(37),
1300 reference_alias!(38),
1301 reference_alias!(39),
1302 reference_alias!(40),
1303 reference_alias!(41),
1304 reference_alias!(42),
1305 reference_alias!(43),
1306 reference_alias!(44),
1307 reference_alias!(45),
1308 reference_alias!(46),
1309 reference_alias!(47),
1310 reference_alias!(48),
1311 reference_alias!(49),
1312 "\n }",
1313 r#" fragment HeldIssue on Issue{__typename id number title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total} commentCount:comments{totalCount} relationshipEvents:timelineItems(itemTypes:$relationshipEvents){totalCount}
1314 projectItems(first:$boardItems){nodes{id updatedAt project{id number}
1315 status:fieldValueByName(name:$statusField){... on ProjectV2ItemFieldSingleSelectValue{name field{
1316 ... on ProjectV2SingleSelectField{id name options{id name}}
1317 }}}
1318 }pageInfo{hasNextPage endCursor}}}"#
1319 );
1320
1321 /// The board's own id and field definitions, and not one of its items.
1322 ///
1323 /// What a write needs of the board when the item it writes does not say: the id a field
1324 /// write and `addProjectV2ItemById` address, and the definitions of the `Status` and
1325 /// origin fields. It selects no `items`, so what it costs is the board's field list
1326 /// however many items the board holds — and it decides nothing about which items those
1327 /// are, which is the question a read of one item by its own id answers instead.
1328 ///
1329 /// The root is aliased `boardFields` rather than `owner`, so nothing counting the
1330 /// board's item reads by their root counts this one among them.
1331 pub const BOARD_FIELDS: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!){
1332 boardFields:repositoryOwner(login:$owner){
1333 ... on ProjectV2Owner{projectV2(number:$number){id
1334 fields(first:$nestedFirst){nodes{
1335 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1336 ... on ProjectV2Field{__typename id name dataType}
1337 }pageInfo{hasNextPage}}
1338 }}
1339 }
1340 }"#;
1341
1342 /// One board draft by its own node id, with the board item it sits in.
1343 ///
1344 /// A draft is not an issue, so [`ISSUE`] reaches it and reads nothing of it; this is the
1345 /// second read that answers it. `DraftIssue.projectV2Items` names the board item a draft
1346 /// is — GitHub links a draft to one item — with the same [`board_item_values!`] the
1347 /// issue fragment reads, so a draft reached by id resolves through the same resolver a
1348 /// board listing hands it to, and nothing has to list the board to find one.
1349 pub const DRAFT: &str = concat!(
1350 r#"query($id:ID!,$nestedFirst:Int!,$boardItems:Int!){
1351 node(id:$id){__typename ... on DraftIssue{id title body createdAt updatedAt
1352 projectV2Items(first:$boardItems){nodes{id project{id number}
1353 "#,
1354 board_item_values!(),
1355 r#"}pageInfo{hasNextPage endCursor}}}}
1356 }"#
1357 );
1358
1359 /// One issue's board memberships alone, walked past the page a read of it carried.
1360 ///
1361 /// The recovery read behind [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource):
1362 /// every document above carries a *page* of `Issue.projectItems`, and an issue on more
1363 /// boards than that page holds may have this board's entry past its end. This asks that
1364 /// one issue for its memberships and nothing else — the caller already holds the issue —
1365 /// so an answer of "this board does not hold it" is only ever given about a connection
1366 /// read to exhaustion.
1367 ///
1368 /// It selects the board item's id, its project number and the same
1369 /// [`board_item_values!`] the fragment does, because what it produces is handed to the
1370 /// very same resolver: an issue recovered this way reports the same title, the same
1371 /// status, the same labels and the same qualified id as one whose entry was on the
1372 /// page.
1373 ///
1374 /// `$first` rather than `$boardItems`: this document reads one issue, so nothing
1375 /// multiplies through it and the membership connection can be walked at
1376 /// [`MAX_PAGE_SIZE`](super::MAX_PAGE_SIZE) — which is what keeps the recovery to one
1377 /// further request for any issue a person really keeps.
1378 pub const ISSUE_BOARD_ITEMS: &str = concat!(
1379 r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!){
1380 node(id:$id){
1381 ... on Issue{projectItems(first:$first,after:$after){
1382 nodes{id updatedAt project{id number}
1383 "#,
1384 board_item_values!(),
1385 r#"}
1386 pageInfo{hasNextPage endCursor}}}
1387 }
1388 }"#
1389 );
1390 /// Whether the board's Project is public — half of what decides whether a write here can
1391 /// be read by anybody. Needs the `read:project` scope.
1392 pub const PROJECT_VISIBILITY: &str = r#"query($owner:String!,$number:Int!){visibility:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){public}}}}"#;
1393 /// Resolves the configured repository's node id, which creating an issue requires.
1394 pub const REPOSITORY: &str = r#"query($owner:String!,$name:String!){repository(owner:$owner,name:$name){id nameWithOwner}}"#;
1395 /// What creating an issue needs and has not read yet: the board's own id and field
1396 /// definitions, as [`BOARD_FIELDS`] reads them, and the node id of the repository the
1397 /// issue is created in, as [`REPOSITORY`] reads it — in one request.
1398 ///
1399 /// Sent at the point a create knows which repository it is for, when neither half is
1400 /// already known to this process; a create needing only one of them sends that one's own
1401 /// document. Neither half is kept past the process: a field's option ids are re-minted by
1402 /// `sources fields --apply`, so a copy of them held between runs would write the wrong
1403 /// status.
1404 pub const CREATION_CONTEXT: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!,$repositoryOwner:String!,$repositoryName:String!){
1405 boardFields:repositoryOwner(login:$owner){
1406 ... on ProjectV2Owner{projectV2(number:$number){id
1407 fields(first:$nestedFirst){nodes{
1408 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1409 ... on ProjectV2Field{__typename id name dataType}
1410 }pageInfo{hasNextPage}}
1411 }}
1412 }
1413 repository(owner:$repositoryOwner,name:$repositoryName){id nameWithOwner}
1414 }"#;
1415 /// Reads both dependency directions for one issue, with each far end's own kind — and
1416 /// the issue's own body, which is where an edge to another source is recorded, so that
1417 /// half of a dependency read needs no second read of the issue or of the board.
1418 pub const ISSUE_DEPENDENCIES: &str = concat!(
1419 r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
1420 ... on Issue{body
1421 blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1422 blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1423 }}}"#,
1424 related_issue!()
1425 );
1426 /// What one item depends on: [`ISSUE_DEPENDENCIES`] narrowed to `blockedBy`, so a
1427 /// dependency read asks GitHub for the one direction it answers. The issue's `body` rides
1428 /// along because its metadata slot is where an edge to another source is recorded — and a
1429 /// board draft's `body` too, with the one board item GitHub links a draft to and, aliased
1430 /// `board`, this board's own id, so a draft's recorded edges are read off this same answer
1431 /// and only when that item is this board's: no draft read and no board read follow it.
1432 ///
1433 /// The draft's board item is asked for under a root of its own, aliased `draft`, rather
1434 /// than beside its `body` under `node`. GitHub counts the type-conditional fragments of
1435 /// one selection as the largest of them — `blockedBy`'s 100 alone, where this workspace's
1436 /// calculation adds the draft's 3 — while separate roots are summed by both, so this is
1437 /// the spelling whose node count GitHub and the calculation agree on. Both price it at
1438 /// one point; the credentialed lane's reconciliation is what holds the two to that.
1439 pub const ISSUE_DEPENDS_ON: &str = concat!(
1440 r#"query($id:ID!,$first:Int!,$after:String,$owner:String!,$number:Int!,$boardItems:Int!){
1441 node(id:$id){__typename
1442 ... on Issue{body
1443 blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1444 }
1445 ... on DraftIssue{id body}}
1446 draft:node(id:$id){... on DraftIssue{projectV2Items(first:$boardItems){nodes{project{id number}}pageInfo{hasNextPage}}}}
1447 board:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){id}}}}"#,
1448 related_issue!()
1449 );
1450 /// What depends on one issue: [`ISSUE_DEPENDENCIES`] with its `blocking` alone. No `body`:
1451 /// the reverse of a recorded edge is derived from the far end, never written down here.
1452 pub const ISSUE_DEPENDED_ON_BY: &str = concat!(
1453 r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
1454 ... on Issue{
1455 blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1456 }}}"#,
1457 related_issue!()
1458 );
1459 /// Creates one issue. Outside a copy it is created on no board, and [`ADD_TO_BOARD`] files
1460 /// it; inside one, its input names the board in `projectV2Ids` and its project in
1461 /// `parentIssueId`, so this one request files it under both — see
1462 /// [`GitHubProjectsSource::create_filed_issue`](super::GitHubProjectsSource).
1463 pub const CREATE_ISSUE: &str =
1464 r#"mutation($input:CreateIssueInput!){createIssue(input:$input){issue{id number url}}}"#;
1465 /// Puts an existing issue on the configured board.
1466 pub const ADD_TO_BOARD: &str = r#"mutation($input:AddProjectV2ItemByIdInput!){addProjectV2ItemById(input:$input){item{id}}}"#;
1467 /// Updates an issue's visible fields and its open or closed state in one call.
1468 pub const UPDATE_ISSUE: &str =
1469 r#"mutation($input:UpdateIssueInput!){updateIssue(input:$input){issue{id}}}"#;
1470 /// Updates an existing draft's user-visible fields.
1471 pub const UPDATE_DRAFT: &str = r#"mutation($input:UpdateProjectV2DraftIssueInput!){updateProjectV2DraftIssue(input:$input){draftIssue{id}}}"#;
1472 /// Updates a text or single-select value on one project item.
1473 pub const UPDATE_FIELD: &str = r#"mutation($input:UpdateProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!){updateProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}}}}}"#;
1474 /// Writes up to six board fields and clears up to three in one ordered mutation: every
1475 /// aliased field is included by its own boolean, as [`FIELD_WRITE_SLOTS`](super::FIELD_WRITE_SLOTS)
1476 /// and [`FIELD_CLEAR_SLOTS`](super::FIELD_CLEAR_SLOTS) name them, writes before clears.
1477 pub const UPDATE_FIELDS: &str = r#"mutation($input:UpdateProjectV2ItemFieldValueInput!,$second:UpdateProjectV2ItemFieldValueInput!,$third:UpdateProjectV2ItemFieldValueInput!,$fourth:UpdateProjectV2ItemFieldValueInput!,$fifth:UpdateProjectV2ItemFieldValueInput!,$sixth:UpdateProjectV2ItemFieldValueInput!,$clear:ClearProjectV2ItemFieldValueInput!,$clearSecond:ClearProjectV2ItemFieldValueInput!,$clearThird:ClearProjectV2ItemFieldValueInput!,$writeFirst:Boolean!,$writeSecond:Boolean!,$writeThird:Boolean!,$writeFourth:Boolean!,$writeFifth:Boolean!,$writeSixth:Boolean!,$writeClear:Boolean!,$writeClearSecond:Boolean!,$writeClearThird:Boolean!){updateProjectV2ItemFieldValue(input:$input) @include(if:$writeFirst){projectV2Item{id}} second:updateProjectV2ItemFieldValue(input:$second) @include(if:$writeSecond){projectV2Item{id}} third:updateProjectV2ItemFieldValue(input:$third) @include(if:$writeThird){projectV2Item{id}} fourth:updateProjectV2ItemFieldValue(input:$fourth) @include(if:$writeFourth){projectV2Item{id}} fifth:updateProjectV2ItemFieldValue(input:$fifth) @include(if:$writeFifth){projectV2Item{id}} sixth:updateProjectV2ItemFieldValue(input:$sixth) @include(if:$writeSixth){projectV2Item{id}} cleared:clearProjectV2ItemFieldValue(input:$clear) @include(if:$writeClear){projectV2Item{id}} clearedSecond:clearProjectV2ItemFieldValue(input:$clearSecond) @include(if:$writeClearSecond){projectV2Item{id}} clearedThird:clearProjectV2ItemFieldValue(input:$clearThird) @include(if:$writeClearThird){projectV2Item{id}}}"#;
1478 /// Clears one project item's value of one field, which is what a `none` priority is.
1479 pub const CLEAR_FIELD: &str = r#"mutation($input:ClearProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!){clearProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}}}}}"#;
1480 /// Creates one field: a single-select one with its options, or a text one for a projected
1481 /// metadata value. Only the guarded field setup may use this document, and only for a
1482 /// field the board lacks.
1483 pub const CREATE_FIELD: &str = r#"mutation($input:CreateProjectV2FieldInput!){createProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id name options{id name color description}}}}}"#;
1484 /// Replaces a single-select field's options. Only the guarded field setup — the
1485 /// `status-options` and `fields` operations — may use this document, because GitHub
1486 /// treats the input as the complete option list.
1487 pub const STATUS_OPTIONS_UPDATE: &str = r#"mutation($input:UpdateProjectV2FieldInput!){updateProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id options{id name color description}}}}}"#;
1488 /// A fresh snapshot of the Status field and every board item's assignment.
1489 pub const STATUS_OPTIONS_SNAPSHOT: &str = r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$nestedFirst:Int!){owner:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){id fields(first:$nestedFirst){nodes{... on ProjectV2SingleSelectField{id name options{id name color description}}}pageInfo{hasNextPage}} items(first:$first,after:$after){nodes{id fieldValues(first:$nestedFirst){nodes{... on ProjectV2ItemFieldSingleSelectValue{name optionId field{... on ProjectV2SingleSelectField{id name}}}}pageInfo{hasNextPage}}}pageInfo{hasNextPage endCursor}}}}}}"#;
1490 /// Files one issue under another as a sub-issue, which is what project membership is.
1491 pub const ADD_SUB_ISSUE: &str =
1492 r#"mutation($input:AddSubIssueInput!){addSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1493 /// Takes one issue back out of its parent.
1494 pub const REMOVE_SUB_ISSUE: &str = r#"mutation($input:RemoveSubIssueInput!){removeSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1495 /// Adds GitHub's native issue blocked-by relationship.
1496 pub const ADD_BLOCKED_BY: &str = r#"mutation($input:AddBlockedByInput!){addBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1497 /// One origin of [`ORIGIN_LOOKUPS`]: [`ORIGIN_LOOKUP`]'s two connections for the origin the
1498 /// `$filter<n>` and `$search<n>` of slot `<n>` name, sent only while `$ask<n>` holds.
1499 macro_rules! origin_lookup_alias {
1500 ($n:literal) => {
1501 concat!(
1502 "\n o",
1503 stringify!($n),
1504 ":repositoryOwner(login:$owner) @include(if:$ask",
1505 stringify!($n),
1506 "){... on ProjectV2Owner{projectV2(number:$number){items(first:$lookupFirst,query:$filter",
1507 stringify!($n),
1508 "){nodes{id updatedAt ",
1509 board_item_values!(),
1510 board_item_content!(),
1511 "} pageInfo{hasNextPage endCursor}}}}}\n s",
1512 stringify!($n),
1513 ":search(query:$search",
1514 stringify!($n),
1515 ",type:$type,first:$lookupFirst) @include(if:$ask",
1516 stringify!($n),
1517 "){pageInfo{hasNextPage endCursor} nodes{__typename ...BoardIssue}}"
1518 )
1519 };
1520 }
1521
1522 /// [`ORIGIN_LOOKUPS`], with `$extra_variables` declared after its own and `$extra_fields`
1523 /// selected beside them: one spelling of the batched lookup for the documents that send it.
1524 macro_rules! origin_lookups {
1525 ($extra_variables:literal, $extra_fields:literal) => {
1526 concat!(
1527 r#"query($owner:String!,$number:Int!,$type:SearchType!,$lookupFirst:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!,$repositoryOwner:String!,$repositoryName:String!,$filter0:String!,$filter1:String!,$filter2:String!,$filter3:String!,$filter4:String!,$filter5:String!,$filter6:String!,$filter7:String!,$filter8:String!,$filter9:String!,$search0:String!,$search1:String!,$search2:String!,$search3:String!,$search4:String!,$search5:String!,$search6:String!,$search7:String!,$search8:String!,$search9:String!,$ask0:Boolean!,$ask1:Boolean!,$ask2:Boolean!,$ask3:Boolean!,$ask4:Boolean!,$ask5:Boolean!,$ask6:Boolean!,$ask7:Boolean!,$ask8:Boolean!,$ask9:Boolean!"#,
1528 relationship_events_variable!(),
1529 $extra_variables,
1530 r#"){
1531 boardFields:repositoryOwner(login:$owner){
1532 ... on ProjectV2Owner{projectV2(number:$number){id
1533 fields(first:$nestedFirst){nodes{
1534 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1535 ... on ProjectV2Field{__typename id name dataType}
1536 }pageInfo{hasNextPage}}
1537 }}
1538 }
1539 repository(owner:$repositoryOwner,name:$repositoryName){id databaseId nameWithOwner}"#,
1540 $extra_fields,
1541 origin_lookup_alias!(0),
1542 origin_lookup_alias!(1),
1543 origin_lookup_alias!(2),
1544 origin_lookup_alias!(3),
1545 origin_lookup_alias!(4),
1546 origin_lookup_alias!(5),
1547 origin_lookup_alias!(6),
1548 origin_lookup_alias!(7),
1549 origin_lookup_alias!(8),
1550 origin_lookup_alias!(9),
1551 "\n }",
1552 board_issue!()
1553 )
1554 };
1555 }
1556
1557 /// The first page of [`ORIGIN_LOOKUP`] for up to [`ORIGIN_BATCH`](super::ORIGIN_BATCH)
1558 /// origins a copy is about to look for, and everything creating an item then reads —
1559 /// the board's own id and field definitions, as [`CREATION_CONTEXT`] reads them, and the node
1560 /// id and the numeric id of the repository an issue is created in — in one request.
1561 ///
1562 /// What a copy reads before it writes anything, so that finding none of its records on the
1563 /// board and creating every one costs this read and the writes. An origin whose page says
1564 /// there is more is left to its own [`ORIGIN_LOOKUP`]. The repository's numeric id is what an
1565 /// image upload names, so a copy carrying assets needs no `GET /repos/{owner}/{repo}` either.
1566 pub const ORIGIN_LOOKUPS: &str = origin_lookups!("", "");
1567
1568 /// [`ORIGIN_LOOKUPS`], with a page of the members of the project `$project` beside it, each
1569 /// trimmed to what classifying it reads — its id, its parent, its body and its repository,
1570 /// and no connection under it.
1571 ///
1572 /// What a document copy sends where the public boundary screens its write: the document is
1573 /// held to who may read its project, which is its members' classification as much as the
1574 /// project's own, and this request carries both the project — among the origins it asks —
1575 /// and its members. GitHub's own dry run prices it at the one point [`ORIGIN_LOOKUPS`]
1576 /// costs alone (`docs/measurements/board-copy-gaps-2026-10-24/coalesced-members-dry-run`).
1577 pub const ORIGIN_LOOKUPS_WITH_MEMBERS: &str = origin_lookups!(
1578 ",$project:ID!,$memberFirst:Int!",
1579 "\n members:node(id:$project){... on Issue{subIssues(first:$memberFirst){pageInfo{hasNextPage endCursor} nodes{id body parent{id} repository{nameWithOwner}}}}}"
1580 );
1581
1582 /// One slot `<n>` of a copy's aliased writes: `$<prefix><n>` is its input and `$send<n>`
1583 /// whether it is sent.
1584 macro_rules! write_slot {
1585 ($prefix:literal, $field:literal, $selection:literal, $n:literal) => {
1586 concat!(
1587 " ",
1588 $prefix,
1589 stringify!($n),
1590 ":",
1591 $field,
1592 "(input:$",
1593 $prefix,
1594 stringify!($n),
1595 ") @include(if:$send",
1596 stringify!($n),
1597 "){",
1598 $selection,
1599 "}"
1600 )
1601 };
1602 }
1603
1604 /// The variables of a copy's aliased writes: eleven inputs of `$type` named `$<prefix><n>`,
1605 /// and eleven `$send<n>`.
1606 macro_rules! write_variables {
1607 ($prefix:literal, $type:literal) => {
1608 concat!(
1609 "mutation(",
1610 "$",
1611 $prefix,
1612 "0:",
1613 $type,
1614 "!,$",
1615 $prefix,
1616 "1:",
1617 $type,
1618 "!,$",
1619 $prefix,
1620 "2:",
1621 $type,
1622 "!,$",
1623 $prefix,
1624 "3:",
1625 $type,
1626 "!,$",
1627 $prefix,
1628 "4:",
1629 $type,
1630 "!,$",
1631 $prefix,
1632 "5:",
1633 $type,
1634 "!,$",
1635 $prefix,
1636 "6:",
1637 $type,
1638 "!,$",
1639 $prefix,
1640 "7:",
1641 $type,
1642 "!,$",
1643 $prefix,
1644 "8:",
1645 $type,
1646 "!,$",
1647 $prefix,
1648 "9:",
1649 $type,
1650 "!,$",
1651 $prefix,
1652 "10:",
1653 $type,
1654 "!,",
1655 "$send0:Boolean!,$send1:Boolean!,$send2:Boolean!,$send3:Boolean!,$send4:Boolean!,",
1656 "$send5:Boolean!,$send6:Boolean!,$send7:Boolean!,$send8:Boolean!,$send9:Boolean!,",
1657 "$send10:Boolean!){"
1658 )
1659 };
1660 }
1661
1662 /// A copy's aliased writes of one kind: [`MUTATION_BATCH`](super::MUTATION_BATCH) slots of
1663 /// `$field`, each sent only while its `$send<n>` holds.
1664 macro_rules! write_batch {
1665 ($prefix:literal, $type:literal, $field:literal, $selection:literal) => {
1666 concat!(
1667 write_variables!($prefix, $type),
1668 write_slot!($prefix, $field, $selection, 0),
1669 write_slot!($prefix, $field, $selection, 1),
1670 write_slot!($prefix, $field, $selection, 2),
1671 write_slot!($prefix, $field, $selection, 3),
1672 write_slot!($prefix, $field, $selection, 4),
1673 write_slot!($prefix, $field, $selection, 5),
1674 write_slot!($prefix, $field, $selection, 6),
1675 write_slot!($prefix, $field, $selection, 7),
1676 write_slot!($prefix, $field, $selection, 8),
1677 write_slot!($prefix, $field, $selection, 9),
1678 write_slot!($prefix, $field, $selection, 10),
1679 "}"
1680 )
1681 };
1682 }
1683
1684 /// Creates up to [`MUTATION_BATCH`](super::MUTATION_BATCH) issues in one request, each filed on
1685 /// the board and under its project by the same `createIssue` — its `projectV2Ids` and
1686 /// `parentIssueId` — as [`CREATE_ISSUE`] creates one.
1687 pub const CREATE_ISSUES: &str = write_batch!(
1688 "create",
1689 "CreateIssueInput",
1690 "createIssue",
1691 "issue{id number url}"
1692 );
1693
1694 /// One issue of [`CREATED_BOARD_ITEMS`]: the board items the issue `$id<n>` names is on, with
1695 /// the `Status` value of each, read only while `$read<n>` holds.
1696 macro_rules! created_item_alias {
1697 ($n:literal) => {
1698 concat!(
1699 "\n i",
1700 stringify!($n),
1701 ":node(id:$id",
1702 stringify!($n),
1703 ") @include(if:$read",
1704 stringify!($n),
1705 "){... on Issue{id projectItems(first:$boardItems){nodes{id project{id} status:fieldValueByName(name:$statusName){... on ProjectV2ItemFieldSingleSelectValue{name}}}}}}"
1706 )
1707 };
1708 }
1709
1710 /// The board item of each issue a copy created, up to
1711 /// [`COPY_DETAIL_BATCH`](super::COPY_DETAIL_BATCH) at a time, with its `Status` as the board
1712 /// set it: what `createIssue`'s `projectV2Ids` files the issue as, which GitHub adds after
1713 /// answering the create, so its id comes from a read made once every create has been sent.
1714 pub const CREATED_BOARD_ITEMS: &str = concat!(
1715 r#"query($id0:ID!,$id1:ID!,$id2:ID!,$id3:ID!,$id4:ID!,$id5:ID!,$id6:ID!,$id7:ID!,$id8:ID!,$id9:ID!,$id10:ID!,$id11:ID!,$id12:ID!,$id13:ID!,$id14:ID!,$id15:ID!,$id16:ID!,$id17:ID!,$id18:ID!,$id19:ID!,$id20:ID!,$read0:Boolean!,$read1:Boolean!,$read2:Boolean!,$read3:Boolean!,$read4:Boolean!,$read5:Boolean!,$read6:Boolean!,$read7:Boolean!,$read8:Boolean!,$read9:Boolean!,$read10:Boolean!,$read11:Boolean!,$read12:Boolean!,$read13:Boolean!,$read14:Boolean!,$read15:Boolean!,$read16:Boolean!,$read17:Boolean!,$read18:Boolean!,$read19:Boolean!,$read20:Boolean!,$boardItems:Int!,$statusName:String!){"#,
1716 created_item_alias!(0),
1717 created_item_alias!(1),
1718 created_item_alias!(2),
1719 created_item_alias!(3),
1720 created_item_alias!(4),
1721 created_item_alias!(5),
1722 created_item_alias!(6),
1723 created_item_alias!(7),
1724 created_item_alias!(8),
1725 created_item_alias!(9),
1726 created_item_alias!(10),
1727 created_item_alias!(11),
1728 created_item_alias!(12),
1729 created_item_alias!(13),
1730 created_item_alias!(14),
1731 created_item_alias!(15),
1732 created_item_alias!(16),
1733 created_item_alias!(17),
1734 created_item_alias!(18),
1735 created_item_alias!(19),
1736 created_item_alias!(20),
1737 "\n }"
1738 );
1739
1740 /// The variables and the aliases of [`WRITE_BATCH`](super::WRITE_BATCH) slots: `$<prefix><n>`
1741 /// is a slot's input and `$<send><n>` whether it is sent, for the slot numbers given.
1742 macro_rules! wide_variables {
1743 ($prefix:literal, $type:literal, $send:literal; $($n:literal)+) => {
1744 concat!($("$", $prefix, stringify!($n), ":", $type, "!,$", $send, stringify!($n), ":Boolean!,",)+)
1745 };
1746 }
1747 macro_rules! wide_slots {
1748 ($prefix:literal, $field:literal, $selection:literal, $send:literal; $($n:literal)+) => {
1749 concat!($(
1750 " ", $prefix, stringify!($n), ":", $field, "(input:$", $prefix, stringify!($n),
1751 ") @include(if:$", $send, stringify!($n), "){", $selection, "}",
1752 )+)
1753 };
1754 }
1755
1756 /// Up to [`WRITE_BATCH`](super::WRITE_BATCH) board field values a copy writes on the items it
1757 /// created, each on its own item, in one ordered request.
1758 pub const WRITE_FIELD_VALUES: &str = concat!(
1759 "mutation(",
1760 wide_variables!("field", "UpdateProjectV2ItemFieldValueInput", "send"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1761 "){",
1762 wide_slots!("field", "updateProjectV2ItemFieldValue", "projectV2Item{id}", "send"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1763 "}"
1764 );
1765
1766 /// A copy's board field values and its blocked-by edges together, up to
1767 /// [`WRITE_BATCH`](super::WRITE_BATCH) of the two kinds between them, in one ordered request:
1768 /// every field value its `$sendField<n>` sends, then every edge its `$sendBlock<n>` sends.
1769 /// What a copy sends when one request of its writes holds both kinds; one holding a single
1770 /// kind is [`WRITE_FIELD_VALUES`] or [`ADD_BLOCKED_BYS`].
1771 pub const WRITE_FIELDS_AND_EDGES: &str = concat!(
1772 "mutation(",
1773 wide_variables!("field", "UpdateProjectV2ItemFieldValueInput", "sendField"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1774 wide_variables!("block", "AddBlockedByInput", "sendBlock"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1775 "){",
1776 wide_slots!("field", "updateProjectV2ItemFieldValue", "projectV2Item{id}", "sendField"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1777 wide_slots!("block", "addBlockedBy", "issue{id} blockingIssue{id}", "sendBlock"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1778 "}"
1779 );
1780
1781 /// Closes up to [`MUTATION_BATCH`](super::MUTATION_BATCH) issues a copy created at a closing
1782 /// status, each with its own `stateInput` and nothing else, once its option is selected.
1783 pub const CLOSE_ISSUES: &str =
1784 write_batch!("close", "UpdateIssueInput", "updateIssue", "issue{id}");
1785
1786 /// Updates up to [`MUTATION_BATCH`](super::MUTATION_BATCH) existing issues a copy rewrites,
1787 /// each with the members its own [`UPDATE_ISSUE`] would have sent, in one request.
1788 pub const UPDATE_ISSUES: &str =
1789 write_batch!("update", "UpdateIssueInput", "updateIssue", "issue{id}");
1790
1791 /// Up to [`WRITE_BATCH`](super::WRITE_BATCH) native blocked-by relationships a copy adds to the
1792 /// issues it created, in one request.
1793 pub const ADD_BLOCKED_BYS: &str = concat!(
1794 "mutation(",
1795 wide_variables!("block", "AddBlockedByInput", "send"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1796 "){",
1797 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),
1798 "}"
1799 );
1800
1801 /// Removes one native issue blocked-by relationship.
1802 pub const REMOVE_BLOCKED_BY: &str = r#"mutation($input:RemoveBlockedByInput!){removeBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1803 /// Deletes one issue, which takes its board item with it.
1804 ///
1805 /// The engine sends this in one situation only: undoing a copy that could not finish,
1806 /// over the items that same copy created. Deleting the issue removes the board item
1807 /// too, so there is no second `deleteProjectV2Item` to keep in step with it.
1808 pub const DELETE_ISSUE: &str =
1809 r#"mutation($input:DeleteIssueInput!){deleteIssue(input:$input){repository{id}}}"#;
1810 /// Deletes up to [`DELETE_BATCH`](super::DELETE_BATCH) issues in one request, each as
1811 /// [`DELETE_ISSUE`] deletes one: what a write that created several issues and was refused
1812 /// part way sends to take them back.
1813 pub const DELETE_ISSUES: &str = concat!(
1814 "mutation(",
1815 wide_variables!("delete", "DeleteIssueInput", "send"; 0 1 2 3 4),
1816 "){",
1817 wide_slots!("delete", "deleteIssue", "repository{id}", "send"; 0 1 2 3 4),
1818 "}"
1819 );
1820
1821 /// Everything this source reads about one issue comment, wherever it reaches one.
1822 ///
1823 /// A macro for the reason [`board_issue!`] is one: a comment listed, a comment just added
1824 /// and a comment just edited are handed to one mapper, so they are selected by one
1825 /// spelling. `author` is `Actor`, which GitHub answers `null` for an account that no
1826 /// longer exists, and `login` is the one member every kind of actor carries.
1827 macro_rules! issue_comment {
1828 () => {
1829 "id author{login} createdAt updatedAt body url"
1830 };
1831 }
1832
1833 /// One task's comments: a page of its issue's own `comments` connection.
1834 ///
1835 /// **No `orderBy`, and that is what makes the page oldest first.** GitHub's only
1836 /// `IssueCommentOrder` field is `UPDATED_AT`, which would move a comment to the end of the
1837 /// list every time somebody edited it; left unordered the connection answers in the order
1838 /// the comments were written, which is the order GitHub documents for the same collection
1839 /// over REST — ascending id. Nothing multiplies through it, so `$first` is the whole of its
1840 /// node count and the caller's own page size is pushed straight down.
1841 pub const ISSUE_COMMENTS: &str = concat!(
1842 r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename ... on Issue{comments(first:$first,after:$after){nodes{"#,
1843 issue_comment!(),
1844 r#"}pageInfo{hasNextPage endCursor}}}}}"#
1845 );
1846 /// One issue by its own node id, with a page of its comments: what `task show` and a
1847 /// comment listing read, in one request.
1848 ///
1849 /// [`ISSUE`] and [`ISSUE_COMMENTS`] in one document, rather than one then the other. The
1850 /// comments are selected here and **not** on the shared `board_issue!` fragment, which
1851 /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] nest under a page of a hundred issues: a comment
1852 /// connection there would multiply through both of those documents' price, and neither
1853 /// needs one.
1854 pub const ISSUE_DETAIL: &str = concat!(
1855 r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1856 relationship_events_variable!(),
1857 r#"){
1858 node(id:$id){__typename ...BoardIssue ... on Issue{comments(first:$first,after:$after){nodes{"#,
1859 issue_comment!(),
1860 r#"}pageInfo{hasNextPage endCursor}}}}
1861 }"#,
1862 board_issue!()
1863 );
1864
1865 /// One alias of [`ISSUE_DETAILS`]: the item a batch's `$id<n>` names, with the first
1866 /// page of its comments when `$comments` asks for them.
1867 macro_rules! issue_details_alias {
1868 ($n:literal) => {
1869 concat!(
1870 "\n i",
1871 stringify!($n),
1872 ":node(id:$id",
1873 stringify!($n),
1874 "){__typename ...BoardIssue ... on Issue{comments(first:$first) @include(if:$comments){nodes{",
1875 issue_comment!(),
1876 "}pageInfo{hasNextPage endCursor}}}}"
1877 )
1878 };
1879 }
1880
1881 /// [`ISSUE_DETAIL`] for [`DETAIL_BATCH`](super::DETAIL_BATCH) items at once, each by its
1882 /// own node id, as one fixed-size document of aliased `node(id:)` fields.
1883 ///
1884 /// **Aliased `node(id:)` rather than `nodes(ids:)`, and that is what keeps its price
1885 /// honest.** The `github-graphql-node-count` model this workspace prices with treats a
1886 /// field that supplies neither `first` nor `last` as free, and `nodes(ids:)` supplies
1887 /// neither — so every connection under it would be priced at nothing and the pin in
1888 /// `tests/point_cost.rs` would understate what GitHub charges. Each alias here is the
1889 /// one-item read the model already prices, so the batch costs what its aliases cost.
1890 ///
1891 /// **Fixed-size, so there is one document to price.** A batch of fewer items binds the
1892 /// slots it has no item for to the last item it does, and reads that item again; the
1893 /// price is the document's, whatever its variables, so a short batch costs what a full
1894 /// one does and nothing more.
1895 pub const ISSUE_DETAILS: &str = concat!(
1896 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!"#,
1897 relationship_events_variable!(),
1898 r#"){"#,
1899 issue_details_alias!(0),
1900 issue_details_alias!(1),
1901 issue_details_alias!(2),
1902 issue_details_alias!(3),
1903 issue_details_alias!(4),
1904 issue_details_alias!(5),
1905 issue_details_alias!(6),
1906 issue_details_alias!(7),
1907 issue_details_alias!(8),
1908 issue_details_alias!(9),
1909 issue_details_alias!(10),
1910 issue_details_alias!(11),
1911 issue_details_alias!(12),
1912 issue_details_alias!(13),
1913 issue_details_alias!(14),
1914 issue_details_alias!(15),
1915 issue_details_alias!(16),
1916 issue_details_alias!(17),
1917 issue_details_alias!(18),
1918 issue_details_alias!(19),
1919 issue_details_alias!(20),
1920 issue_details_alias!(21),
1921 issue_details_alias!(22),
1922 issue_details_alias!(23),
1923 "\n }",
1924 board_issue!()
1925 );
1926
1927 /// One alias of [`ISSUE_COPY_DETAILS`]: the item a batch's `$id<n>` names, with the first
1928 /// page of its comments and the issues it is blocked by.
1929 macro_rules! issue_copy_details_alias {
1930 ($n:literal) => {
1931 concat!(
1932 "\n i",
1933 stringify!($n),
1934 ":node(id:$id",
1935 stringify!($n),
1936 "){__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{",
1937 issue_comment!(),
1938 "}pageInfo{hasNextPage endCursor}} blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}}}"
1939 )
1940 };
1941 }
1942
1943 /// [`ISSUE_DETAILS`] as a copy reads the tasks it carries: each item with the first page of
1944 /// its comments and its `blockedBy` — the edges a copy reads of every task next, which
1945 /// [`ISSUE`] carries and [`ISSUE_DETAILS`] does not — [`COPY_DETAIL_BATCH`](super::COPY_DETAIL_BATCH)
1946 /// at a time, so a copy of a changed task costs this read and nothing else.
1947 ///
1948 /// A document of its own rather than a widening of [`ISSUE_DETAILS`], whose batch size is
1949 /// what `task show-many` is priced by and which reads no edge.
1950 ///
1951 /// `children`, read only while `$children` holds, is which issues the issue `$project` has
1952 /// as sub-issues — each one's id and title, the page `$nestedFirst` allows — so that a
1953 /// project copy, which reads its project and every task at once, knows from this same read
1954 /// which tasks the project holds at the destination, and its orphan check is answered by it
1955 /// rather than by a listing of the project.
1956 pub const ISSUE_COPY_DETAILS: &str = concat!(
1957 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!"#,
1958 relationship_events_variable!(),
1959 r#"){"#,
1960 issue_copy_details_alias!(0),
1961 issue_copy_details_alias!(1),
1962 issue_copy_details_alias!(2),
1963 issue_copy_details_alias!(3),
1964 issue_copy_details_alias!(4),
1965 issue_copy_details_alias!(5),
1966 issue_copy_details_alias!(6),
1967 issue_copy_details_alias!(7),
1968 issue_copy_details_alias!(8),
1969 issue_copy_details_alias!(9),
1970 issue_copy_details_alias!(10),
1971 issue_copy_details_alias!(11),
1972 issue_copy_details_alias!(12),
1973 issue_copy_details_alias!(13),
1974 issue_copy_details_alias!(14),
1975 issue_copy_details_alias!(15),
1976 issue_copy_details_alias!(16),
1977 issue_copy_details_alias!(17),
1978 issue_copy_details_alias!(18),
1979 issue_copy_details_alias!(19),
1980 issue_copy_details_alias!(20),
1981 "\n children:node(id:$project) @include(if:$children){... on Issue{id subIssues(first:$nestedFirst){nodes{id title} pageInfo{hasNextPage}}}}",
1982 "\n }",
1983 board_issue!(),
1984 related_issue!()
1985 );
1986
1987 /// Which issue one comment is on, read before that comment is edited or removed.
1988 ///
1989 /// GitHub's comment mutations take the comment's id and nothing else, so without this a
1990 /// comment id given against the wrong task would change a comment on another issue.
1991 pub const COMMENT_ISSUE: &str =
1992 r#"query($id:ID!){node(id:$id){__typename ... on IssueComment{id body issue{id}}}}"#;
1993 /// Adds one comment to an issue, signed as the account the token belongs to.
1994 pub const ADD_COMMENT: &str = concat!(
1995 r#"mutation($input:AddCommentInput!){addComment(input:$input){subject{id} commentEdge{node{"#,
1996 issue_comment!(),
1997 r#"}}}}"#
1998 );
1999 /// Replaces the body of one issue comment.
2000 pub const UPDATE_COMMENT: &str = concat!(
2001 r#"mutation($input:UpdateIssueCommentInput!){updateIssueComment(input:$input){issueComment{"#,
2002 issue_comment!(),
2003 r#"}}}"#
2004 );
2005 /// Removes one issue comment. Its payload carries nothing about the comment it removed.
2006 pub const DELETE_COMMENT: &str = r#"mutation($input:DeleteIssueCommentInput!){deleteIssueComment(input:$input){clientMutationId}}"#;
2007
2008 /// What a mutation of a write the store's cache expects selects of the issue it wrote:
2009 /// everything [`ISSUE_DETAIL`] reads of it, so its response shows the task whole — its
2010 /// change token's every part and the first page of its comments.
2011 ///
2012 /// Sent **only** while that write runs, in place of the document named beside each
2013 /// below, so every other write's documents, prices and request counts are what they
2014 /// were. GitHub's token waits for a task to settle (`change_token::SETTLED`), and a
2015 /// mutation's response is stamped seconds after the change it made, so against GitHub
2016 /// itself a response rarely determines a version; the cache is then left unserveable
2017 /// for the task, and its next read refreshes it.
2018 macro_rules! whole_issue {
2019 () => {
2020 concat!(
2021 r#"__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{"#,
2022 issue_comment!(),
2023 r#"}pageInfo{hasNextPage endCursor}}}"#
2024 )
2025 };
2026 }
2027
2028 /// The variables every document of `whole_issue!` declares beside its own.
2029 macro_rules! whole_variables {
2030 () => {
2031 concat!(
2032 r#",$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
2033 relationship_events_variable!()
2034 )
2035 };
2036 }
2037
2038 /// [`ADD_COMMENT`], selecting the whole issue commented on.
2039 pub const ADD_COMMENT_WHOLE: &str = concat!(
2040 r#"mutation($input:AddCommentInput!"#,
2041 whole_variables!(),
2042 r#"){addComment(input:$input){subject{id "#,
2043 whole_issue!(),
2044 r#"} commentEdge{node{"#,
2045 issue_comment!(),
2046 r#"}}}}"#,
2047 board_issue!()
2048 );
2049 /// [`UPDATE_COMMENT`], selecting the whole issue the comment is on.
2050 pub const UPDATE_COMMENT_WHOLE: &str = concat!(
2051 r#"mutation($input:UpdateIssueCommentInput!"#,
2052 whole_variables!(),
2053 r#"){updateIssueComment(input:$input){issueComment{"#,
2054 issue_comment!(),
2055 r#" issue{"#,
2056 whole_issue!(),
2057 r#"}}}}"#,
2058 board_issue!()
2059 );
2060 /// [`UPDATE_ISSUE`], selecting the whole issue updated.
2061 pub const UPDATE_ISSUE_WHOLE: &str = concat!(
2062 r#"mutation($input:UpdateIssueInput!"#,
2063 whole_variables!(),
2064 r#"){updateIssue(input:$input){issue{id "#,
2065 whole_issue!(),
2066 r#"}}}"#,
2067 board_issue!()
2068 );
2069 /// [`UPDATE_FIELD`], selecting the whole issue behind the board item written.
2070 pub const UPDATE_FIELD_WHOLE: &str = concat!(
2071 r#"mutation($input:UpdateProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
2072 whole_variables!(),
2073 r#"){updateProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
2074 whole_issue!(),
2075 r#"}}}}"#,
2076 board_issue!()
2077 );
2078 /// [`CLEAR_FIELD`], selecting the whole issue behind the board item cleared.
2079 pub const CLEAR_FIELD_WHOLE: &str = concat!(
2080 r#"mutation($input:ClearProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
2081 whole_variables!(),
2082 r#"){clearProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
2083 whole_issue!(),
2084 r#"}}}}"#,
2085 board_issue!()
2086 );
2087
2088 /// The document sent in `document`'s place while a write the store's cache expects runs,
2089 /// when it has one.
2090 #[must_use]
2091 pub fn whole_twin(document: &str) -> Option<&'static str> {
2092 [
2093 (ADD_COMMENT, ADD_COMMENT_WHOLE),
2094 (UPDATE_COMMENT, UPDATE_COMMENT_WHOLE),
2095 (UPDATE_ISSUE, UPDATE_ISSUE_WHOLE),
2096 (UPDATE_FIELD, UPDATE_FIELD_WHOLE),
2097 (CLEAR_FIELD, CLEAR_FIELD_WHOLE),
2098 ]
2099 .into_iter()
2100 .find(|(plain, _)| *plain == document)
2101 .map(|(_, whole)| whole)
2102 }
2103
2104 /// Every document above, with what this source is doing when it sends one.
2105 ///
2106 /// One list rather than a `match` beside the constants: a rate-limit diagnostic has to
2107 /// name the call that was refused, and a `match` with a catch-all arm would answer a
2108 /// document added later with "talking to GitHub" and never say so.
2109 ///
2110 /// `documents_are_all_inventoried` reads this file back and fails naming any `pub
2111 /// const` here that this list omits, so the two cannot part — which is the same guard
2112 /// `CATEGORIES` carries, in the one shape available to a set of `&str` constants.
2113 pub const DOCUMENTS: [(&str, &str); 55] = [
2114 (SEARCH_ISSUES, "searching this board's issues"),
2115 (ISSUE, "reading one issue"),
2116 (
2117 DOCUMENT_IN_PROJECT,
2118 "reading one document with its project and the project's members",
2119 ),
2120 (
2121 ISSUE_BOARD_ITEMS,
2122 "reading one issue's board memberships past the page it came with",
2123 ),
2124 (SUB_ISSUES, "reading a project's tasks"),
2125 (BOARD, "reading the board"),
2126 (ORIGIN_LOOKUP, "looking up the items copied from one origin"),
2127 (CLOSED_ITEMS, "reading the board's closed items"),
2128 (
2129 ISSUES_BY_REFERENCE,
2130 "reading a batch of issues by their references",
2131 ),
2132 (BOARD_FIELDS, "reading the board's fields"),
2133 (DRAFT, "reading one draft"),
2134 (REPOSITORY, "reading the destination repository"),
2135 (
2136 PROJECT_VISIBILITY,
2137 "reading whether the board's project is public",
2138 ),
2139 (
2140 CREATION_CONTEXT,
2141 "reading the board's fields and the destination repository",
2142 ),
2143 (ISSUE_DEPENDENCIES, "reading an issue's dependencies"),
2144 (ISSUE_DEPENDS_ON, "reading what an item depends on"),
2145 (ISSUE_DEPENDED_ON_BY, "reading what depends on an issue"),
2146 (CREATE_ISSUE, "creating an issue"),
2147 (
2148 ORIGIN_LOOKUPS,
2149 "looking up the items copied from several origins, and what creating one reads",
2150 ),
2151 (
2152 ORIGIN_LOOKUPS_WITH_MEMBERS,
2153 "looking up the items copied from several origins, with a project's members",
2154 ),
2155 (CREATE_ISSUES, "creating several issues together"),
2156 (
2157 CREATED_BOARD_ITEMS,
2158 "reading the board items of the issues a copy created",
2159 ),
2160 (
2161 WRITE_FIELD_VALUES,
2162 "writing board fields of the items a copy created",
2163 ),
2164 (
2165 WRITE_FIELDS_AND_EDGES,
2166 "writing board fields and dependencies of the items a copy created",
2167 ),
2168 (CLOSE_ISSUES, "closing the issues a copy created"),
2169 (UPDATE_ISSUES, "updating several issues together"),
2170 (ADD_BLOCKED_BYS, "recording several dependencies together"),
2171 (ADD_TO_BOARD, "adding an issue to the board"),
2172 (UPDATE_ISSUE, "updating an issue"),
2173 (UPDATE_DRAFT, "updating a draft item"),
2174 (UPDATE_FIELD, "writing a board field"),
2175 (UPDATE_FIELDS, "writing board fields together"),
2176 (CLEAR_FIELD, "clearing a board field"),
2177 (
2178 CREATE_FIELD,
2179 "creating a board single-select field with its options",
2180 ),
2181 (
2182 STATUS_OPTIONS_SNAPSHOT,
2183 "snapshotting board Status options and assignments",
2184 ),
2185 (
2186 STATUS_OPTIONS_UPDATE,
2187 "safely replacing the board Status option list",
2188 ),
2189 (ADD_SUB_ISSUE, "filing an issue under its project"),
2190 (REMOVE_SUB_ISSUE, "taking an issue out of its project"),
2191 (ADD_BLOCKED_BY, "recording a dependency"),
2192 (REMOVE_BLOCKED_BY, "removing a dependency"),
2193 (DELETE_ISSUE, "deleting an issue"),
2194 (DELETE_ISSUES, "deleting several issues together"),
2195 (ISSUE_COMMENTS, "reading a task's comments"),
2196 (ISSUE_DETAIL, "reading one issue with its comments"),
2197 (
2198 ISSUE_DETAILS,
2199 "reading a batch of issues with their comments",
2200 ),
2201 (
2202 ISSUE_COPY_DETAILS,
2203 "reading a batch of issues with their comments and dependencies for a copy",
2204 ),
2205 (COMMENT_ISSUE, "reading which issue a comment is on"),
2206 (ADD_COMMENT, "adding a comment"),
2207 (UPDATE_COMMENT, "editing a comment"),
2208 (DELETE_COMMENT, "deleting a comment"),
2209 (
2210 ADD_COMMENT_WHOLE,
2211 "adding a comment, answered with the whole issue",
2212 ),
2213 (
2214 UPDATE_COMMENT_WHOLE,
2215 "editing a comment, answered with the whole issue",
2216 ),
2217 (
2218 UPDATE_ISSUE_WHOLE,
2219 "updating an issue, answered with it whole",
2220 ),
2221 (
2222 UPDATE_FIELD_WHOLE,
2223 "writing a board field, answered with the whole issue",
2224 ),
2225 (
2226 CLEAR_FIELD_WHOLE,
2227 "clearing a board field, answered with the whole issue",
2228 ),
2229 ];
2230}
2231
2232/// One aliased mutation field of [`graphql::UPDATE_FIELDS`]: the key its answer comes back
2233/// under, the variable carrying its input, and the boolean variable that includes it.
2234#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2235pub struct FieldSlot {
2236 /// The key of the answer's `data` this field's payload is under.
2237 pub alias: &'static str,
2238 /// The variable carrying this field's input.
2239 pub variable: &'static str,
2240 /// The boolean variable whose `true` sends this field.
2241 pub include: &'static str,
2242}
2243
2244/// The value writes [`graphql::UPDATE_FIELDS`] carries, in the order GitHub runs them.
2245pub const FIELD_WRITE_SLOTS: [FieldSlot; 6] = [
2246 FieldSlot {
2247 alias: "updateProjectV2ItemFieldValue",
2248 variable: "input",
2249 include: "writeFirst",
2250 },
2251 FieldSlot {
2252 alias: "second",
2253 variable: "second",
2254 include: "writeSecond",
2255 },
2256 FieldSlot {
2257 alias: "third",
2258 variable: "third",
2259 include: "writeThird",
2260 },
2261 FieldSlot {
2262 alias: "fourth",
2263 variable: "fourth",
2264 include: "writeFourth",
2265 },
2266 FieldSlot {
2267 alias: "fifth",
2268 variable: "fifth",
2269 include: "writeFifth",
2270 },
2271 FieldSlot {
2272 alias: "sixth",
2273 variable: "sixth",
2274 include: "writeSixth",
2275 },
2276];
2277
2278/// The clears [`graphql::UPDATE_FIELDS`] carries, run after every write of the same request.
2279pub const FIELD_CLEAR_SLOTS: [FieldSlot; 3] = [
2280 FieldSlot {
2281 alias: "cleared",
2282 variable: "clear",
2283 include: "writeClear",
2284 },
2285 FieldSlot {
2286 alias: "clearedSecond",
2287 variable: "clearSecond",
2288 include: "writeClearSecond",
2289 },
2290 FieldSlot {
2291 alias: "clearedThird",
2292 variable: "clearThird",
2293 include: "writeClearThird",
2294 },
2295];
2296
2297/// Which of GitHub's two rate limiters refused a request.
2298///
2299/// Waiting is the whole answer to the primary budget, and polling is what *extends* the
2300/// secondary one — so an operator told the wrong one takes the wrong next step, which is
2301/// the whole reason this is carried rather than collapsed into "rate limited".
2302#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2303enum Limiter {
2304 /// The hourly API budget, which `gh api rate_limit` reports and a wait answers.
2305 Primary,
2306 /// The burst limiter over content-generating requests, which nothing reports.
2307 Secondary,
2308}
2309
2310/// The wordings GitHub answers a secondary rate limit with.
2311///
2312/// It sends them under a forbidden status, under a too-many-requests status, and inside
2313/// the `errors` of a *successful* response, which is why the text is what this matches on
2314/// rather than the status. `abuse detection` is the wording GitHub used before the
2315/// limiter was renamed and still returns from some endpoints; `submitted too quickly` is
2316/// what a burst of content creation is refused with.
2317///
2318/// This is GitHub's vocabulary rather than this source's, so it is pinned rather than
2319/// remembered: `tests/fixtures/rate-limits.json` records where each wording was read and
2320/// when, and the drift gate reconciles the two lists both ways. Public for that gate
2321/// alone — a caller has no use for it, and matching on a refusal is this source's job.
2322pub const SECONDARY_WORDINGS: [&str; 5] = [
2323 "secondary rate limit",
2324 "temporarily blocked from content creation",
2325 "abuse detection",
2326 "submitted too quickly",
2327 "exceeded a secondary",
2328];
2329
2330/// The wordings GitHub answers an exhausted primary budget with.
2331///
2332/// `rate_limited` is the `type` its GraphQL error carries, which is read as a field rather
2333/// than looked for in the response text. `api rate limit already exceeded` is what GraphQL
2334/// answers a request made once the hour's budget is spent — "API rate limit already exceeded
2335/// for user ID …" in the `errors` of an HTTP 200, with no `type` — and neither of the other
2336/// two phrases is a substring of it, so without it that answer read as a refusal that will
2337/// never lift. Pinned and gated exactly as [`SECONDARY_WORDINGS`] is, and public for the same
2338/// one reason.
2339pub const PRIMARY_WORDINGS: [&str; 4] = [
2340 "api rate limit exceeded",
2341 "api rate limit already exceeded",
2342 "rate limit exceeded",
2343 "rate_limited",
2344];
2345
2346/// What a response *says about itself*, which is the only place a refusal can be read.
2347///
2348/// Deliberately not the whole response body. A board is a place people write about their
2349/// own work, and a task on it titled "the secondary rate limit" would, matched across the
2350/// raw text, turn a perfectly good answer into a refusal this source then waited out and
2351/// reported. So the item data is never read: what is read is GitHub's own REST-style
2352/// `message` envelope, which is what a forbidden status carries, and the `message` and
2353/// `type` of each GraphQL error, which is where a *successful* response says it.
2354///
2355/// A body that is not JSON at all has nothing structured to read, so only a failing
2356/// response's own text is taken — a successful response that is not JSON is malformed
2357/// rather than refused, and [`GitHubProjectsSource::answer`] says so.
2358fn refusal_wording(status: StatusCode, body: &str) -> String {
2359 let Ok(parsed) = serde_json::from_str::<Value>(body) else {
2360 return if status.is_success() {
2361 String::new()
2362 } else {
2363 body.to_owned()
2364 };
2365 };
2366 let mut said: Vec<&str> = parsed
2367 .get("message")
2368 .and_then(Value::as_str)
2369 .into_iter()
2370 .collect();
2371 if let Some(errors) = parsed.get("errors").and_then(Value::as_array) {
2372 for error in errors {
2373 said.extend(
2374 ["message", "type"]
2375 .into_iter()
2376 .filter_map(|key| error.get(key).and_then(Value::as_str)),
2377 );
2378 }
2379 }
2380 said.join("; ")
2381}
2382
2383impl Limiter {
2384 /// Which limiter refused this response, or `None` when none of them did.
2385 ///
2386 /// The wording is read first and the status only decides what carries none of it,
2387 /// because GitHub answers a secondary limit with a forbidden status far more often
2388 /// than with too-many-requests — while a forbidden status saying nothing about a limit
2389 /// really is a credential this token lacks.
2390 ///
2391 /// A response is a refusal because of its status or its own wording. A spent budget
2392 /// only ever explains one; it never turns an answer into a refusal.
2393 fn classify(status: StatusCode, budget_exhausted: bool, body: &str) -> Option<Self> {
2394 let normalized = refusal_wording(status, body).to_ascii_lowercase();
2395 if SECONDARY_WORDINGS
2396 .iter()
2397 .any(|wording| normalized.contains(wording))
2398 {
2399 return Some(Self::Secondary);
2400 }
2401 if status == StatusCode::TOO_MANY_REQUESTS {
2402 return Some(Self::Primary);
2403 }
2404 // An exhausted budget *explains* a response that failed; it does not make one that
2405 // succeeded into a failure. GitHub sets `x-ratelimit-remaining: 0` on the last
2406 // request the budget allowed as well as on the ones it then refuses, so reading
2407 // the header alone threw away a good answer — and, once refusals were retried,
2408 // replayed a request that had already taken effect.
2409 if !status.is_success() && budget_exhausted {
2410 return Some(Self::Primary);
2411 }
2412 // A successful response saying it: GitHub reports a GraphQL rate limit in the
2413 // `errors` of an HTTP 200, where nothing about the status says so at all.
2414 if status.is_success()
2415 && PRIMARY_WORDINGS
2416 .iter()
2417 .any(|wording| normalized.contains(wording))
2418 {
2419 return Some(Self::Primary);
2420 }
2421 None
2422 }
2423
2424 /// What this limiter is called where an operator can look it up.
2425 const fn name(self) -> &'static str {
2426 match self {
2427 Self::Primary => "GitHub's primary API rate limit",
2428 Self::Secondary => "GitHub's secondary rate limit",
2429 }
2430 }
2431
2432 /// What the endpoint an operator would go and check says about this limiter.
2433 const fn where_to_look(self) -> &'static str {
2434 match self {
2435 Self::Primary => {
2436 "That is the budget `gh api rate_limit` reports, so that endpoint says when it \
2437 comes back."
2438 }
2439 Self::Secondary => {
2440 "That limiter is not the primary API budget: `gh api rate_limit` reports the \
2441 primary budget and does not report this one, so budget showing there says \
2442 nothing about this refusal, and every further attempt extends it."
2443 }
2444 }
2445 }
2446
2447 /// The next step this limiter actually calls for.
2448 const fn what_to_do(self) -> &'static str {
2449 match self {
2450 Self::Primary => {
2451 "wait for the reset `gh api rate_limit` reports, then run the command again."
2452 }
2453 Self::Secondary => {
2454 "leave this board alone for a few minutes, then run the command again — or \
2455 raise pacing.min_mutation_interval_ms on this source so it writes more slowly."
2456 }
2457 }
2458 }
2459}
2460
2461/// One rate-limit refusal, and the wait GitHub asked for if it asked for one.
2462#[derive(Debug, Clone, Copy)]
2463struct Limited {
2464 limiter: Limiter,
2465 hint: Option<u64>,
2466 /// Which header the hint came from: `retry-after`, or an exhausted primary budget's
2467 /// `x-ratelimit-reset`.
2468 hinted_by: Hint,
2469}
2470
2471/// Where a refusal's hint came from, which is what its wait is named after.
2472#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2473enum Hint {
2474 /// `retry-after`, or no hint at all.
2475 RetryAfter,
2476 /// An exhausted primary budget's `x-ratelimit-reset`.
2477 PrimaryReset,
2478}
2479
2480impl Limited {
2481 /// The wait this refusal is answered with, when the backing-off schedule is at `backoff`,
2482 /// and the cause it is named by: the hint, raised to the schedule's own next wait —
2483 /// GitHub really does send `retry-after: 0`, and retrying at once is the one move that
2484 /// extends a secondary limit — or the schedule alone when there is none.
2485 fn wait(self, backoff: Duration) -> (Duration, WaitCause) {
2486 match (self.hint, self.hinted_by) {
2487 (Some(hint), Hint::RetryAfter) => (
2488 Duration::from_secs(hint).max(backoff),
2489 pacing::cause::RETRY_AFTER,
2490 ),
2491 (Some(hint), Hint::PrimaryReset) => (
2492 Duration::from_secs(hint).max(backoff),
2493 pacing::cause::PRIMARY_RESET,
2494 ),
2495 (None, _) => (backoff, pacing::cause::BACKOFF),
2496 }
2497 }
2498
2499 /// What the caller is told once this source has waited as long as it may.
2500 ///
2501 /// Both limiters report as [`SourceError::RateLimited`], because that is what
2502 /// happened: the kind a caller matches on says a rate limit refused this, and nothing
2503 /// about *which* limiter it was makes it a different kind of failure. What differs is
2504 /// the operator's next step, and that is what the message carries — a secondary
2505 /// refusal read as a primary one sends an operator to `gh api rate_limit`, where the
2506 /// budget looks fine, and then back to retry the very burst that was refused.
2507 fn exhausted(
2508 self,
2509 doing: &str,
2510 waits: u32,
2511 waited: Duration,
2512 needed: Duration,
2513 budget: Duration,
2514 ) -> SourceError {
2515 SourceError::RateLimited {
2516 retry_after_seconds: self.hint,
2517 message: Some(format!(
2518 "{} refused this source while {doing}; it waited {} out over {} and was refused \
2519 again, and the next wait of {} would take it past the {} one call may spend \
2520 waiting. {} next: {}",
2521 self.limiter.name(),
2522 plural(waits, "refusal"),
2523 seconds(waited),
2524 seconds(needed),
2525 seconds(budget),
2526 self.limiter.where_to_look(),
2527 self.limiter.what_to_do(),
2528 )),
2529 }
2530 }
2531}
2532
2533/// One HTTP attempt's result, with what its response said about the rate limit.
2534///
2535/// The two travel together so the record and the outcome are written from the same place:
2536/// what a response said about the budget is only readable while that response is in hand,
2537/// and what the attempt *meant* is only decidable once its body has been read.
2538struct Attempted {
2539 result: Result<Value, Attempt>,
2540 limits: accounting::RateLimit,
2541 /// GitHub's own reported cost for this call, for a document that asked for it.
2542 reported_cost: Option<u64>,
2543 response_outcome: accounting::ResponseOutcome,
2544}
2545
2546/// One attempt's outcome: an error to report, or a rate limit to wait out.
2547enum Attempt {
2548 Failed(SourceError),
2549 Limited(Limited),
2550}
2551
2552fn plural(count: u32, thing: &str) -> String {
2553 if count == 1 {
2554 format!("{count} {thing}")
2555 } else {
2556 format!("{count} {thing}s")
2557 }
2558}
2559
2560fn seconds(duration: Duration) -> String {
2561 format!("{:.1}s", duration.as_secs_f64())
2562}
2563
2564/// The member [`stamp_served`] writes onto every node of a response that says when it last
2565/// changed: the `Date` that response was served at. No GitHub type has a field of this name.
2566const SERVED_AT: &str = "onetaskgraphServedAt";
2567
2568/// Write `served` onto every object of `data` carrying an `updatedAt`, so whatever reads a
2569/// node's time later knows the server's clock on the response that read it — which is what
2570/// decides whether that node's change token is given (see [`change_token::SETTLED`]).
2571fn stamp_served(data: &mut Value, served: &str) {
2572 match data {
2573 Value::Object(members) => {
2574 if members.contains_key("updatedAt") {
2575 members.insert(SERVED_AT.to_owned(), Value::String(served.to_owned()));
2576 }
2577 for member in members.values_mut() {
2578 stamp_served(member, served);
2579 }
2580 }
2581 Value::Array(items) => {
2582 for item in items {
2583 stamp_served(item, served);
2584 }
2585 }
2586 _ => {}
2587 }
2588}
2589
2590/// A header GitHub spells as a whole number of seconds, or `None` when this one is not.
2591///
2592/// A value that is present and unreadable is deliberately *not* an error. `retry-after` is
2593/// allowed by HTTP to be a date rather than a count, an intermediary can rewrite either
2594/// header, and neither is what makes a response a refusal — so the whole cost of one this
2595/// cannot read is that the refusal carries no hint and the backing-off schedule answers it
2596/// instead. Refusing the response over the header would turn a readable refusal into an
2597/// unreadable one, and refusing to *wait* would be the one wrong direction to fail in.
2598fn whole_seconds(value: Option<&reqwest::header::HeaderValue>) -> Option<u64> {
2599 value
2600 .and_then(|value| value.to_str().ok())
2601 .and_then(|value| value.trim().parse::<u64>().ok())
2602}
2603
2604/// A transport failure with every cause beneath it, outermost first.
2605///
2606/// The client's own text for a request it could not complete is `error sending request for
2607/// url (…)`, and the socket error that says why — a refused or reset connection, an exhausted
2608/// port range, a connection closed before its answer — is only in the chain of sources below
2609/// it. Those differ in what to do next, so all of them are kept.
2610pub(crate) fn transport_error(error: &(dyn std::error::Error + 'static)) -> String {
2611 let mut text = error.to_string();
2612 let mut cause = error.source();
2613 while let Some(next) = cause {
2614 let said = next.to_string();
2615 if !text.ends_with(&said) {
2616 text = format!("{text}: {said}");
2617 }
2618 cause = next.source();
2619 }
2620 text
2621}
2622
2623/// Every mutation this source sends creates content — an issue, a board item, a field of
2624/// one, a sub-issue link, a dependency, a comment — or edits or removes content of that
2625/// kind, and no query in [`graphql::DOCUMENTS`] does, so what the secondary limiter counts
2626/// and what the keyword says are the same set. That is what makes the keyword a sound test
2627/// rather than a convenient one: counting an edit or a removal the limiter might not have
2628/// counted costs a wait, and not counting one it did count costs the next fifty minutes.
2629pub(crate) fn is_mutation(query: &str) -> bool {
2630 query.trim_start().starts_with("mutation")
2631}
2632
2633/// What this source was doing, for a diagnostic that has to say so.
2634///
2635/// Read out of [`graphql::DOCUMENTS`], which is the inventory rather than a copy of it, so
2636/// a document added without a description is caught by that list's own gate instead of
2637/// falling through to the vague arm below.
2638fn operation_description(query: &str) -> &'static str {
2639 graphql::DOCUMENTS
2640 .iter()
2641 .find(|(document, _)| *document == query)
2642 .map_or("talking to GitHub", |(_, doing)| *doing)
2643}
2644
2645/// GitHub's published ceiling on content-generating requests in any one minute.
2646///
2647/// Pinned in `tests/fixtures/rate-limits.json` and gated against it, because it is
2648/// GitHub's number rather than this source's. This source paces at it over its window: a
2649/// content-creating request goes out at once while the minute's creations leave room for it,
2650/// and waits until they do otherwise — see the `pacing` module.
2651pub const CONTENT_CREATION_PER_MINUTE: u64 = 80;
2652/// The same ceiling as GitHub publishes it for any one hour, paced over its window as the
2653/// per-minute one is. Pinned beside its sibling so the gate would notice either one moving.
2654pub const CONTENT_CREATION_PER_HOUR: u64 = 500;
2655/// GitHub's published ceiling on secondary points in any one minute: what every GraphQL
2656/// request is charged against, a mutation [`SECONDARY_POINTS_PER_MUTATION`] and a query
2657/// [`SECONDARY_POINTS_PER_QUERY`]. Pinned and paced as [`CONTENT_CREATION_PER_MINUTE`] is.
2658pub const SECONDARY_POINTS_PER_MINUTE: u64 = 2_000;
2659/// The secondary points one GraphQL request carrying a mutation is charged.
2660pub const SECONDARY_POINTS_PER_MUTATION: u64 = 5;
2661/// The secondary points one GraphQL request carrying no mutation is charged.
2662pub const SECONDARY_POINTS_PER_QUERY: u64 = 1;
2663/// The fastest *steady* spacing of content-creating mutations that cannot exceed
2664/// [`CONTENT_CREATION_PER_MINUTE`], in milliseconds: 60000/80 is 750.
2665///
2666/// It is no longer the default. This source paces by GitHub's windows instead, so a copy's
2667/// few dozen writes go out as fast as GitHub answers them while the minute has room, rather
2668/// than 750 ms apart — which was most of a plan copy's wall time. An installation that wants
2669/// mutations spaced as well says so through `pacing.min_mutation_interval_ms`, and this is the
2670/// value that holds a long sustained burst under the per-minute bound by spacing alone.
2671pub const MIN_MUTATION_INTERVAL_MS: u64 = 60_000 / CONTENT_CREATION_PER_MINUTE;
2672/// First wait when a rate-limit refusal carries no hint; each further wait doubles it.
2673///
2674/// A doubling schedule from one second reaches a minute in six waits, which is GitHub's
2675/// own advice for a secondary limit — wait, and wait longer each time — without spending
2676/// the first minute of a transient refusal doing nothing.
2677pub const RETRY_BACKOFF_MS: u64 = 1_000;
2678/// Total time one call may spend waiting out rate limits before it reports a failure.
2679///
2680/// Two minutes is long enough to ride out the refusals a paced copy still collects and
2681/// short enough that a command an operator is watching returns. The bound is what makes
2682/// the wait a wait rather than a hang: a call refused past it ends in a diagnostic naming
2683/// the limiter, not in a process nobody can tell from a wedged one.
2684pub const RETRY_BUDGET_MS: u64 = 120_000;
2685
2686fn default_token_env() -> String {
2687 "GH_PROJECTS_TOKEN".to_owned()
2688}
2689fn default_endpoint() -> String {
2690 "https://api.github.com/graphql".to_owned()
2691}
2692
2693/// The name of a `Status` single-select option on the board.
2694///
2695/// Validated on the way in rather than checked later, so a blank option name — which
2696/// nothing on a board can be — is a state this type cannot hold.
2697#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2698#[serde(try_from = "String")]
2699#[schemars(extend("minLength" = 1))]
2700pub struct ColumnName(String);
2701
2702impl ColumnName {
2703 /// The option name, as the board spells it.
2704 fn as_str(&self) -> &str {
2705 &self.0
2706 }
2707}
2708
2709impl TryFrom<String> for ColumnName {
2710 type Error = String;
2711
2712 fn try_from(name: String) -> Result<Self, Self::Error> {
2713 if name.trim().is_empty() {
2714 return Err("a status_mapping option name cannot be blank".to_owned());
2715 }
2716 Ok(Self(name))
2717 }
2718}
2719
2720/// The two closed states this product can mean.
2721///
2722/// GitHub's `IssueClosedStateReason` also spells `DUPLICATE`, which nothing here ever writes:
2723/// an issue closed as one *reads* `cancelled`, because its work is another issue's, but this
2724/// product never decides on a person's behalf that two issues are the same work.
2725#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, schemars::JsonSchema)]
2726#[serde(rename_all = "kebab-case")]
2727pub enum ClosedState {
2728 /// `COMPLETED` — precisely done.
2729 Completed,
2730 /// `NOT_PLANNED` — precisely cancelled.
2731 NotPlanned,
2732}
2733
2734impl ClosedState {
2735 const fn reason(self) -> &'static str {
2736 match self {
2737 Self::Completed => "COMPLETED",
2738 Self::NotPlanned => "NOT_PLANNED",
2739 }
2740 }
2741}
2742
2743/// Configuration for one GitHub Projects v2 board.
2744///
2745/// It serializes to a document this same type reads back as itself, so a caller writing a
2746/// configuration — a consumer filing onto a board it configures — can build one here rather
2747/// than spell the shape again; an empty `metadata_fields` is left out, as one never set.
2748#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2749#[serde(default, deny_unknown_fields)]
2750pub struct GitHubProjectsConfig {
2751 /// Login of the user or organization which owns the board.
2752 pub owner: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates GitHub's owner grammar before private construction.
2753 /// The project number shown in the board's GitHub URL.
2754 pub project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` bounds this to a positive GraphQL Int.
2755 // 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.
2756 /// `owner/name` of the repository this source creates an issue in when the item's own
2757 /// `repositories` field does not decide it.
2758 ///
2759 /// An item naming exactly one repository is created there; a task or a document naming
2760 /// none or several is created in its parent project's repository; and a project, or a
2761 /// task or document with no parent, naming none or several is created here. A board
2762 /// has no repository of its own and `createIssue` requires one, so a write without
2763 /// this is refused naming the field. Reads never need it.
2764 pub repository: Option<String>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the `owner/name` grammar before private construction.
2765 // llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
2766 /// Environment variable containing a fine-grained token with Projects and Issues
2767 /// read/write plus Pull requests read-only access for every repository represented on
2768 /// the board.
2769 #[serde(default = "default_token_env")]
2770 pub token_env: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the environment-variable grammar.
2771 /// GraphQL endpoint. GitHub Enterprise installations may override it.
2772 #[serde(default = "default_endpoint")]
2773 pub endpoint: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` converts it to the private validated `Url`.
2774 /// Per-instance mapping from a status category to the option of the board's one
2775 /// `Status` field it lands on, for a task and for a project.
2776 ///
2777 /// The shared `StatusMapping` grammar: each value is one option name for both kinds,
2778 /// `null` to disable the category for both, or `{task, project}` naming it per kind,
2779 /// where a kind left out leaves the category unmapped for that kind. A category this
2780 /// does not mention keeps its shipped default for both kinds: `backlog` to "Backlog",
2781 /// `todo` to "Todo", `queued` to "Queued", `in-progress` to "In Progress", `done` to
2782 /// "Done" plus closed as completed, `cancelled` to "Cancelled" plus closed as not
2783 /// planned, and `draft` and `unknown` unmapped. A category it does mention gets no
2784 /// shipped default for a kind it leaves out. `done` and `cancelled` close the issue for
2785 /// either kind. No two categories may name one option for the same kind, ignoring case.
2786 /// `unknown` may name one existing option; every unknown word then lands on it and
2787 /// reads back as `unknown` under its name. Unlike `local-md`, this source cannot keep
2788 /// each unknown word because it never creates board options.
2789 #[serde(default)]
2790 pub status_mapping: StatusMapping,
2791 /// Per-instance mapping from a task's priority to an option of this board's
2792 /// single-select field named `Priority`.
2793 ///
2794 /// Absent, this source holds no priority: every task reads as `none`, and a write of any
2795 /// other priority is refused before it reaches this board. Present, each of `urgent`,
2796 /// `high`, `medium` and `low` it does not mention keeps its shipped default — `Urgent`,
2797 /// `High`, `Medium` and `Low` — and an item with no value in the `Priority` field reads
2798 /// as `none`, so writing `none` clears the value. Option names match case-insensitively;
2799 /// no two levels may name one option. Reads and writes never create the field or an
2800 /// option: `onetaskgraph sources fields <source> --apply` does, and a write naming one
2801 /// the board lacks is refused pointing there.
2802 #[serde(default)]
2803 pub priority_mapping: Option<PriorityMappingConfig>,
2804 /// Caller metadata values this source also writes to board text fields, so the board can
2805 /// filter on them.
2806 ///
2807 /// Absent or empty, nothing is projected. Each entry names a board text `field`, a
2808 /// top-level metadata `key` and an optional `path` of object keys walked inside that key's
2809 /// value. On every item write — create, update and copy, of a task, a project or a
2810 /// document — a string found there is written to the field, and nothing is sent when the
2811 /// field already holds it; nothing there, or `null`, clears the field when it holds a
2812 /// value; any other JSON type is refused before any mutation. The metadata comment in the
2813 /// issue body stays the one home of the value, and every read reports metadata from it.
2814 /// Reads and writes never create the field: `onetaskgraph sources fields <source>
2815 /// --apply` does, as a text field, and a write to a board lacking it, or holding a
2816 /// non-text field of that name, is refused pointing there.
2817 #[serde(default, skip_serializing_if = "Vec::is_empty")]
2818 // Kept in the schema as `"default": []` although a serialized configuration leaves an empty
2819 // list out, so both SDKs model an absent list as an empty one rather than as `null`.
2820 #[schemars(!skip_serializing_if)]
2821 pub metadata_fields: Vec<MetadataFieldConfig>,
2822 /// How fast this source writes, and how long it waits out a rate-limit refusal.
2823 ///
2824 /// Every field keeps its shipped default when it is absent, and the defaults are
2825 /// GitHub's own published limits rather than taste. See [`Pacing`].
2826 #[serde(default)]
2827 pub pacing: PacingConfig,
2828}
2829
2830/// Which option of the board's `Priority` field each priority lands on.
2831///
2832/// One member per level rather than a map, so a key that is not a level is refused where
2833/// the configuration is read, naming the levels there are. `none` is not a member: it is no
2834/// value in the field, not an option of it.
2835#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2836#[serde(default, deny_unknown_fields)]
2837pub struct PriorityMappingConfig {
2838 /// The option `urgent` lands on; `Urgent` when absent.
2839 pub urgent: Option<PriorityOptionName>,
2840 /// The option `high` lands on; `High` when absent.
2841 pub high: Option<PriorityOptionName>,
2842 /// The option `medium` lands on; `Medium` when absent.
2843 pub medium: Option<PriorityOptionName>,
2844 /// The option `low` lands on; `Low` when absent.
2845 pub low: Option<PriorityOptionName>,
2846}
2847
2848/// The name of an option of the board's `Priority` single-select field.
2849///
2850/// Validated on the way in, for the reason [`ColumnName`] is: nothing on a board can have a
2851/// blank name.
2852#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2853#[serde(try_from = "String")]
2854#[schemars(extend("minLength" = 1))]
2855pub struct PriorityOptionName(String);
2856
2857impl PriorityOptionName {
2858 /// The option name, as the board spells it.
2859 fn as_str(&self) -> &str {
2860 &self.0
2861 }
2862}
2863
2864impl TryFrom<String> for PriorityOptionName {
2865 type Error = String;
2866
2867 fn try_from(name: String) -> Result<Self, Self::Error> {
2868 if name.trim().is_empty() {
2869 return Err("a priority_mapping option name cannot be blank".to_owned());
2870 }
2871 Ok(Self(name))
2872 }
2873}
2874
2875/// The name of the board field a priority is held in.
2876pub const PRIORITY_FIELD: &str = "Priority";
2877
2878/// The four priorities a board option can hold, in the order a new `Priority` field lists
2879/// them. `none` is not among them: it is the field holding no value.
2880///
2881/// This list mirrors `Priority`, so it carries its own drift gate, in the shape [`CATEGORIES`]
2882/// does: [`level_position`] is a wildcard-free match, so a priority added to the shared
2883/// vocabulary fails to compile until it is placed there, and this crate's suite reconciles
2884/// this list and [`PriorityMappingConfig`]'s members against that enum's own derived schema.
2885pub const PRIORITY_LEVELS: [Priority; 4] = [
2886 Priority::Urgent,
2887 Priority::High,
2888 Priority::Medium,
2889 Priority::Low,
2890];
2891
2892/// Where one priority sits in [`PRIORITY_LEVELS`], or `None` for `none`, which is no option;
2893/// see that list for what this pins.
2894#[must_use]
2895pub const fn level_position(priority: Priority) -> Option<usize> {
2896 match priority {
2897 Priority::None => None,
2898 Priority::Urgent => Some(0),
2899 Priority::High => Some(1),
2900 Priority::Medium => Some(2),
2901 Priority::Low => Some(3),
2902 }
2903}
2904
2905/// This instance's complete priority-to-option mapping, read in both directions.
2906///
2907/// One option per level, held in [`PRIORITY_LEVELS`] order, once it is established that no
2908/// two levels name one option.
2909#[derive(Debug, Clone)]
2910struct PriorityMapping {
2911 options: [PriorityOptionName; 4],
2912}
2913
2914impl PriorityMapping {
2915 fn resolve(config: PriorityMappingConfig, instance: &SourceName) -> Result<Self, SourceError> {
2916 let shipped = |name: &str| PriorityOptionName(name.to_owned());
2917 let mapping = Self {
2918 options: [
2919 config.urgent.unwrap_or_else(|| shipped("Urgent")),
2920 config.high.unwrap_or_else(|| shipped("High")),
2921 config.medium.unwrap_or_else(|| shipped("Medium")),
2922 config.low.unwrap_or_else(|| shipped("Low")),
2923 ],
2924 };
2925 for (index, option) in mapping.options.iter().enumerate() {
2926 if let Some(earlier) = mapping.options[..index]
2927 .iter()
2928 .position(|other| other.as_str().eq_ignore_ascii_case(option.as_str()))
2929 {
2930 return Err(SourceError::Config {
2931 message: format!(
2932 "priority_mapping of source {instance} sends both {} and {} to the board \
2933 option {:?}; one option cannot read back as two priorities",
2934 PRIORITY_LEVELS[earlier],
2935 PRIORITY_LEVELS[index],
2936 option.as_str()
2937 ),
2938 });
2939 }
2940 }
2941 Ok(mapping)
2942 }
2943
2944 /// The option `priority` lands on, or `None` for `none`, which is no option at all.
2945 fn option(&self, priority: Priority) -> Option<&str> {
2946 level_position(priority).map(|index| self.options[index].as_str())
2947 }
2948
2949 /// The priority a board option name reports, or `None` when nothing maps to it.
2950 fn priority_of(&self, option: &str) -> Option<Priority> {
2951 self.options
2952 .iter()
2953 .position(|name| name.as_str().eq_ignore_ascii_case(option))
2954 .map(|index| PRIORITY_LEVELS[index])
2955 }
2956
2957 /// Every mapped option name, in the order a new `Priority` field lists them.
2958 fn names(&self) -> impl Iterator<Item = &str> {
2959 self.options.iter().map(PriorityOptionName::as_str)
2960 }
2961}
2962
2963/// One metadata value this source projects onto a board text field.
2964#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2965#[serde(deny_unknown_fields)]
2966pub struct MetadataFieldConfig {
2967 /// The name of the board text field the value is written to.
2968 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.
2969 /// The top-level metadata key the value is read from. A key under the reserved
2970 /// `onetaskgraph.` prefix is refused.
2971 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.
2972 /// Object keys walked inside that key's value, outermost first. Absent or empty, the
2973 /// key's own value is the one projected. Left out of a serialized entry when empty.
2974 #[serde(default, skip_serializing_if = "Vec::is_empty")]
2975 // Kept in the schema as `"default": []`, for the reason `metadata_fields`' is.
2976 #[schemars(!skip_serializing_if)]
2977 pub path: Vec<String>,
2978}
2979
2980/// The board fields no metadata value may be projected onto, compared ignoring case: the
2981/// three this source writes itself, and those GitHub owns on every board.
2982// 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.
2983const UNPROJECTABLE_FIELDS: [&str; 13] = [
2984 STATUS_FIELD,
2985 PRIORITY_FIELD,
2986 ORIGIN_FIELD,
2987 "Title",
2988 "Assignees",
2989 "Labels",
2990 "Linked pull requests",
2991 "Milestone",
2992 "Repository",
2993 "Reviewers",
2994 "Parent issue",
2995 "Sub-issues progress",
2996 "Type",
2997];
2998
2999/// One validated [`MetadataFieldConfig`] entry.
3000#[derive(Debug, Clone)]
3001struct MetadataField {
3002 field: ProjectedField,
3003 key: ProjectedKey,
3004 path: Vec<String>,
3005}
3006
3007/// The name of a board text field a metadata value is projected onto: never blank, never one
3008/// this source or GitHub writes, and never one another entry of the same source names.
3009#[derive(Debug, Clone, PartialEq, Eq)]
3010struct ProjectedField(String);
3011
3012/// The top-level metadata key a projected value is read from: never blank, and never under
3013/// the namespace this product reserves.
3014#[derive(Debug, Clone, PartialEq, Eq)]
3015struct ProjectedKey(String);
3016
3017impl std::ops::Deref for ProjectedField {
3018 type Target = str;
3019 fn deref(&self) -> &str {
3020 &self.0
3021 }
3022}
3023
3024impl std::ops::Deref for ProjectedKey {
3025 type Target = str;
3026 fn deref(&self) -> &str {
3027 &self.0
3028 }
3029}
3030
3031/// What one item's metadata says one projected field should hold.
3032#[derive(Debug, Clone, PartialEq, Eq)]
3033enum Projected {
3034 /// A string to hold.
3035 Text(String),
3036 /// Nothing: the key or a step of the path is absent, or the value is `null`.
3037 Nothing,
3038}
3039
3040impl MetadataField {
3041 /// Validate every entry of one instance's `metadata_fields`, refusing the first that
3042 /// cannot stand, naming the source and the entry.
3043 fn resolve(
3044 configured: Vec<MetadataFieldConfig>,
3045 instance: &SourceName,
3046 ) -> Result<Vec<Self>, SourceError> {
3047 let mut resolved: Vec<Self> = Vec::with_capacity(configured.len());
3048 for (index, entry) in configured.into_iter().enumerate() {
3049 let refuse = |why: String| SourceError::Config {
3050 message: format!(
3051 "metadata_fields[{index}] of source {instance} (field {:?}, key {:?}) {why}",
3052 entry.field, entry.key
3053 ),
3054 };
3055 if entry.field.trim().is_empty() {
3056 return Err(refuse("names a blank field".to_owned()));
3057 }
3058 if entry.key.trim().is_empty() {
3059 return Err(refuse("names a blank key".to_owned()));
3060 }
3061 if entry.key.split('.').next() == Some(MetadataKey::RESERVED_NAMESPACE) {
3062 return Err(refuse(format!(
3063 "names a key under the reserved \"{}.\" prefix, which this product keeps \
3064 for its own keys; next: project a caller key instead",
3065 MetadataKey::RESERVED_NAMESPACE
3066 )));
3067 }
3068 if let Some(owned) = UNPROJECTABLE_FIELDS
3069 .iter()
3070 .find(|owned| owned.eq_ignore_ascii_case(&entry.field))
3071 {
3072 return Err(refuse(format!(
3073 "names the board field {owned:?}, which this source or GitHub already \
3074 writes; next: name a text field of your own"
3075 )));
3076 }
3077 if let Some(earlier) = resolved
3078 .iter()
3079 .position(|other| other.field.eq_ignore_ascii_case(&entry.field))
3080 {
3081 return Err(refuse(format!(
3082 "names the same board field as metadata_fields[{earlier}]; one field \
3083 cannot hold two values"
3084 )));
3085 }
3086 resolved.push(Self {
3087 field: ProjectedField(entry.field),
3088 key: ProjectedKey(entry.key),
3089 path: entry.path,
3090 });
3091 }
3092 Ok(resolved)
3093 }
3094
3095 /// The path as a refusal spells it: `[]` for the key's own value.
3096 fn spelled_path(&self) -> String {
3097 serde_json::to_string(&self.path).unwrap_or_else(|_| format!("{:?}", self.path))
3098 }
3099
3100 /// What `metadata` says this field should hold, or the refusal for a value no text field
3101 /// can hold.
3102 fn projected(
3103 &self,
3104 metadata: &BTreeMap<String, Value>,
3105 instance: &SourceName,
3106 ) -> Result<Projected, SourceError> {
3107 let mut value = metadata.get(&*self.key);
3108 for step in &self.path {
3109 value = value.and_then(|held| held.get(step.as_str()));
3110 }
3111 let found = match value {
3112 None | Some(Value::Null) => return Ok(Projected::Nothing),
3113 Some(Value::String(text)) => return Ok(Projected::Text(text.clone())),
3114 Some(Value::Bool(_)) => "a boolean",
3115 Some(Value::Number(_)) => "a number",
3116 Some(Value::Array(_)) => "an array",
3117 Some(Value::Object(_)) => "an object",
3118 };
3119 Err(SourceError::Refused {
3120 message: format!(
3121 "source {instance} projects metadata key {:?} at path {} onto its board text \
3122 field {:?}, and this item holds {found} there; only a string, null or nothing \
3123 can be written to a text field; next: store a string there, or remove the \
3124 value",
3125 &*self.key,
3126 self.spelled_path(),
3127 &*self.field
3128 ),
3129 })
3130 }
3131}
3132
3133/// What one item's `Priority` field says, read through this instance's mapping.
3134#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
3135enum HeldPriority {
3136 /// A priority this source reports: an option the mapping names, or no value (`none`).
3137 Read(Priority),
3138 /// An option the mapping does not name, which is never read as a level or as `none`.
3139 Unmapped(String),
3140}
3141
3142/// How fast this source writes, and how long it waits out a rate-limit refusal.
3143///
3144/// Configurable because a GitHub Enterprise installation sets its own limits and an
3145/// operator who has already been refused may want to go slower still — not because the
3146/// defaults are guesses.
3147#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3148#[serde(default, deny_unknown_fields)]
3149pub struct PacingConfig {
3150 /// Shortest interval between two content-creating mutations, in milliseconds.
3151 ///
3152 /// Zero sends them as fast as they are asked for, which is what a fixture server on
3153 /// loopback wants and what no board on github.com does. At most [`MAX_PACING_MS`].
3154 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.
3155 /// First wait when a rate-limit refusal carries no hint, in milliseconds. Each
3156 /// further wait of the same call doubles it. At most [`MAX_PACING_MS`], and never
3157 /// zero while there is a budget to spend, because a schedule of zero-length waits
3158 /// consumes none of it and so never ends.
3159 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.
3160 /// Total time one call may spend waiting out rate limits, in milliseconds.
3161 ///
3162 /// Zero reports the refusal rather than waiting at all. At most [`MAX_PACING_MS`]:
3163 /// the bound is what makes this a wait rather than a hang.
3164 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.
3165}
3166
3167/// The largest any pacing setting may be, in milliseconds.
3168///
3169/// One hour. GitHub's own harshest published bound on content-generating requests works
3170/// out at one every 7.2 seconds, so an hour is already three orders of magnitude past
3171/// anything a real limit asks for, and past it the settings stop describing pacing at all:
3172/// a wait budget beyond it is the unbounded wait this whole mechanism exists to replace,
3173/// and an interval beyond it is a command that never sends its second mutation. It also
3174/// keeps the clock arithmetic in [`GitHubProjectsSource::reserve_mutation_slot`] inside
3175/// what a `Duration` can hold on every platform.
3176pub const MAX_PACING_MS: u64 = 3_600_000;
3177
3178/// [`PacingConfig`] with every default resolved and every value checked, which is what the
3179/// source holds.
3180///
3181/// Unset, `min_mutation_interval_ms` spaces nothing, and every request is paced by GitHub's
3182/// secondary windows alone — see the `pacing` module; set, it spaces content-creating mutations
3183/// on top of those windows, [`MIN_MUTATION_INTERVAL_MS`] being the spacing that holds a sustained
3184/// burst under the per-minute bound by spacing alone; and zero sends every request as fast as it
3185/// is asked for, windows included.
3186#[derive(Debug, Clone, Copy)]
3187struct Pacing {
3188 /// Whether requests are held for GitHub's secondary windows: always, unless
3189 /// `min_mutation_interval_ms` is set to exactly zero, which has always asked for no
3190 /// pacing at all — what a fixture server on loopback wants and no board on github.com does.
3191 windows: bool,
3192 min_mutation_interval: Duration,
3193 retry_backoff: Duration,
3194 retry_budget: Duration,
3195}
3196
3197impl Pacing {
3198 /// Resolve one instance's pacing, refusing a configuration that would not pace at all.
3199 fn resolve(config: PacingConfig, instance: &SourceName) -> Result<Self, SourceError> {
3200 let bounded = |value: Option<u64>, default: u64, field: &str| match value {
3201 Some(value) if value > MAX_PACING_MS => Err(SourceError::Config {
3202 message: format!(
3203 "pacing.{field} of source {instance} is {value} ms, and the most any pacing \
3204 setting may be is {MAX_PACING_MS} ms — an hour, which is already far past \
3205 GitHub's own harshest published limit"
3206 ),
3207 }),
3208 Some(value) => Ok(Duration::from_millis(value)),
3209 None => Ok(Duration::from_millis(default)),
3210 };
3211 let retry_backoff = bounded(
3212 config.retry_backoff_ms,
3213 RETRY_BACKOFF_MS,
3214 "retry_backoff_ms",
3215 )?;
3216 let retry_budget = bounded(config.retry_budget_ms, RETRY_BUDGET_MS, "retry_budget_ms")?;
3217 if retry_backoff.is_zero() && !retry_budget.is_zero() {
3218 return Err(SourceError::Config {
3219 message: format!(
3220 "pacing.retry_backoff_ms of source {instance} is 0 while \
3221 pacing.retry_budget_ms is {} ms; a schedule of zero-length waits spends \
3222 none of that budget, so it would retry a refusal forever. Set a backoff of \
3223 at least 1 ms, or set retry_budget_ms to 0 to report a refusal without \
3224 waiting at all",
3225 retry_budget.as_millis()
3226 ),
3227 });
3228 }
3229 Ok(Self {
3230 windows: config.min_mutation_interval_ms != Some(0),
3231 min_mutation_interval: bounded(
3232 config.min_mutation_interval_ms,
3233 0,
3234 "min_mutation_interval_ms",
3235 )?,
3236 retry_backoff,
3237 retry_budget,
3238 })
3239 }
3240}
3241
3242/// Factory for [`GitHubProjectsSource`].
3243#[derive(Debug, Clone, Copy, Default)]
3244pub struct Plugin;
3245
3246impl SourcePlugin for Plugin {
3247 fn kind(&self) -> &'static str {
3248 KIND
3249 }
3250 /// The endpoint, the owner and the board's number: what names one board.
3251 fn remote_identity(&self, config: &Value) -> Option<String> {
3252 let config: GitHubProjectsConfig = serde_json::from_value(config.clone()).ok()?;
3253 Some(format!(
3254 "{}\n{}\n{}",
3255 config.endpoint, config.owner, config.project_number
3256 ))
3257 }
3258 fn config_schema(&self) -> Schema {
3259 schema_for!(GitHubProjectsConfig)
3260 }
3261 fn build(
3262 &self,
3263 name: &SourceName,
3264 config: &Value,
3265 secrets: &dyn SecretResolver,
3266 ) -> Result<Box<dyn TaskSource>, SourceError> {
3267 self.build_recording_into(name, config, secrets, Arc::new(Accounting::new()))
3268 }
3269
3270 fn build_with_clock(
3271 &self,
3272 name: &SourceName,
3273 config: &Value,
3274 secrets: &dyn SecretResolver,
3275 clock: SharedClock,
3276 ) -> Result<Box<dyn TaskSource>, SourceError> {
3277 self.build_recording_with_clock(name, config, secrets, Arc::new(Accounting::new()), clock)
3278 }
3279}
3280
3281/// The client `cell` holds, building it with `build` the first time it is asked for.
3282///
3283/// A failed build is the request's failure, with the message the source's construction gave it
3284/// before clients were deferred.
3285// 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`.
3286fn built(
3287 cell: &std::sync::OnceLock<Client>,
3288 build: impl FnOnce() -> Result<Client, SourceError>,
3289) -> Result<&Client, SourceError> {
3290 if let Some(client) = cell.get() {
3291 return Ok(client);
3292 }
3293 let client = build()?;
3294 Ok(cell.get_or_init(|| client))
3295}
3296
3297impl Plugin {
3298 /// Build a source recording every request it sends into an accounting the caller holds.
3299 ///
3300 /// [`SourcePlugin::build`] is this with an accounting of its own, which is what the
3301 /// registry gets. This is for a caller that is also calling GitHub itself and wants one
3302 /// session total rather than two — see [`accounting`] and
3303 /// [`GitHubProjectsSource::recording_into`].
3304 ///
3305 /// # Errors
3306 ///
3307 /// Exactly [`SourcePlugin::build`]'s, with the same source name in front of each:
3308 /// [`SourceError::Config`] for configuration this plugin cannot use and
3309 /// [`SourceError::Auth`] for a credential it cannot find.
3310 pub fn build_recording_into(
3311 &self,
3312 name: &SourceName,
3313 config: &Value,
3314 secrets: &dyn SecretResolver,
3315 ledger: Arc<Accounting>,
3316 ) -> Result<Box<dyn TaskSource>, SourceError> {
3317 self.build_recording_with_clock(name, config, secrets, ledger, system_clock())
3318 }
3319
3320 fn build_recording_with_clock(
3321 &self,
3322 name: &SourceName,
3323 config: &Value,
3324 secrets: &dyn SecretResolver,
3325 ledger: Arc<Accounting>,
3326 clock: SharedClock,
3327 ) -> Result<Box<dyn TaskSource>, SourceError> {
3328 let config: GitHubProjectsConfig =
3329 serde_json::from_value(config.clone()).map_err(|e| SourceError::Config {
3330 message: format!("source {name}: {e}"),
3331 })?;
3332 let prefix = format!("source {name}: ");
3333 let mut source = GitHubProjectsSource::recording_into(name, config, secrets, ledger)
3334 .map_err(|error| match error {
3335 // The shared `StatusMapping::distinct` names the source itself.
3336 SourceError::Config { message } if message.starts_with(&prefix) => {
3337 SourceError::Config { message }
3338 }
3339 SourceError::Config { message } => SourceError::Config {
3340 message: format!("{prefix}{message}"),
3341 },
3342 SourceError::Auth { message } => SourceError::Auth {
3343 message: format!("source {name}: {message}"),
3344 },
3345 other => other,
3346 })?;
3347 source.clock = clock;
3348 Ok(Box::new(source))
3349 }
3350}
3351
3352/// Where a status category lands on this board, once configuration is resolved.
3353#[derive(Debug, Clone, PartialEq, Eq)]
3354enum StatusTarget {
3355 /// Not usable against this instance for this kind, and why.
3356 Disabled(UnmappedStatus),
3357 /// The board's `Status` option of this name.
3358 Column(ColumnName),
3359 /// A closed issue, with both its board option and the reason that says which closed it means.
3360 // 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.
3361 Terminal(ColumnName, ClosedState),
3362}
3363
3364/// Every status category, in the order the vocabulary declares them.
3365///
3366/// This list mirrors `StatusCategory`, so it carries its own drift gate rather than a
3367/// reviewer's attention: [`category_position`] is a wildcard-free match, so a variant
3368/// added to the shared vocabulary fails to compile until it is named there, and this
3369/// crate's suite reconciles this list against that enum's own derived schema, which is
3370/// generated from the variants rather than written beside them. The schema is what
3371/// catches a list left one short — a list checking only the positions it already holds
3372/// would pass while every mapping indexed by the new position panicked.
3373pub const CATEGORIES: [StatusCategory; 8] = [
3374 StatusCategory::Draft,
3375 StatusCategory::Backlog,
3376 StatusCategory::Todo,
3377 StatusCategory::Queued,
3378 StatusCategory::InProgress,
3379 StatusCategory::Done,
3380 StatusCategory::Cancelled,
3381 StatusCategory::Unknown,
3382];
3383
3384/// Where one category sits in [`CATEGORIES`]; see that list for what this pins.
3385#[must_use]
3386pub const fn category_position(category: StatusCategory) -> usize {
3387 match category {
3388 StatusCategory::Draft => 0,
3389 StatusCategory::Backlog => 1,
3390 StatusCategory::Todo => 2,
3391 StatusCategory::Queued => 3,
3392 StatusCategory::InProgress => 4,
3393 StatusCategory::Done => 5,
3394 StatusCategory::Cancelled => 6,
3395 StatusCategory::Unknown => 7,
3396 }
3397}
3398
3399/// The spelling a status category is configured and reported under.
3400fn category_name(category: StatusCategory) -> &'static str {
3401 match category {
3402 StatusCategory::Draft => "draft",
3403 StatusCategory::Backlog => "backlog",
3404 StatusCategory::Todo => "todo",
3405 StatusCategory::Queued => "queued",
3406 StatusCategory::InProgress => "in-progress",
3407 StatusCategory::Done => "done",
3408 StatusCategory::Cancelled => "cancelled",
3409 StatusCategory::Unknown => "unknown",
3410 }
3411}
3412
3413/// A shipped default's option name.
3414///
3415/// The literals below are this file's own and non-blank, and they are validated by the
3416/// one constructor a configured name goes through rather than beside it.
3417fn shipped_column(name: &'static str) -> ColumnName {
3418 ColumnName::try_from(name.to_owned()).expect("a shipped default names a board option")
3419}
3420
3421/// The shipped default for one category this instance's `status_mapping` does not mention,
3422/// for either kind.
3423fn shipped_default(category: StatusCategory) -> StatusTarget {
3424 match category {
3425 StatusCategory::Backlog => StatusTarget::Column(shipped_column("Backlog")),
3426 StatusCategory::Todo => StatusTarget::Column(shipped_column("Todo")),
3427 StatusCategory::Queued => StatusTarget::Column(shipped_column("Queued")),
3428 StatusCategory::InProgress => StatusTarget::Column(shipped_column("In Progress")),
3429 StatusCategory::Done => {
3430 StatusTarget::Terminal(shipped_column("Done"), ClosedState::Completed)
3431 }
3432 StatusCategory::Cancelled => {
3433 StatusTarget::Terminal(shipped_column("Cancelled"), ClosedState::NotPlanned)
3434 }
3435 StatusCategory::Draft | StatusCategory::Unknown => {
3436 StatusTarget::Disabled(UnmappedStatus::Unconfigured)
3437 }
3438 }
3439}
3440
3441/// The two kinds a status is written and read for, each with its own half of the mapping.
3442const STATUS_KINDS: [ItemKind; 2] = [ItemKind::Task, ItemKind::Project];
3443
3444/// This instance's complete category-to-target mapping for each kind, read in both
3445/// directions.
3446///
3447/// One target per category per kind, held at that category's own [`category_position`], so
3448/// a category missing from the mapping, named twice in it, or filed out of order is a state
3449/// this type cannot hold rather than one [`Self::target`] has to defend against. Both kinds'
3450/// targets are options of the board's one `Status` field.
3451#[derive(Debug, Clone)]
3452struct BoardStatuses {
3453 tasks: [StatusTarget; CATEGORIES.len()],
3454 projects: [StatusTarget; CATEGORIES.len()],
3455}
3456
3457impl BoardStatuses {
3458 /// Resolve `configured` against the shipped defaults, refusing two categories one kind
3459 /// would read back from one option.
3460 ///
3461 /// A category the mapping does not mention keeps its shipped default for both kinds; one
3462 /// it does mention is exactly what it configures, so a per-kind object leaves the kind it
3463 /// omits unmapped rather than defaulted.
3464 fn resolve(configured: &StatusMapping, instance: &SourceName) -> Result<Self, SourceError> {
3465 let resolve_kind =
3466 |kind: ItemKind| -> Result<[StatusTarget; CATEGORIES.len()], SourceError> {
3467 // `CATEGORIES[position] == category` for every category — the crate's suite
3468 // asserts it — so mapping the list in order fills each category's own slot.
3469 let mut targets = CATEGORIES.map(shipped_default);
3470 for (slot, category) in targets.iter_mut().zip(CATEGORIES) {
3471 if !configured.mentions(category) {
3472 continue;
3473 }
3474 *slot = match configured.name_for(category, kind) {
3475 Err(why) => StatusTarget::Disabled(why),
3476 Ok(name) => {
3477 let option = ColumnName::try_from(name.as_str().to_owned())
3478 .map_err(|message| SourceError::Config { message })?;
3479 match category {
3480 StatusCategory::Done => {
3481 StatusTarget::Terminal(option, ClosedState::Completed)
3482 }
3483 StatusCategory::Cancelled => {
3484 StatusTarget::Terminal(option, ClosedState::NotPlanned)
3485 }
3486 _ => StatusTarget::Column(option),
3487 }
3488 }
3489 };
3490 }
3491 StatusMapping::distinct(
3492 instance,
3493 kind,
3494 CATEGORIES
3495 .iter()
3496 .zip(&targets)
3497 .filter_map(|(category, target)| target.option().map(|o| (*category, o))),
3498 )?;
3499 Ok(targets)
3500 };
3501 Ok(Self {
3502 tasks: resolve_kind(ItemKind::Task)?,
3503 projects: resolve_kind(ItemKind::Project)?,
3504 })
3505 }
3506
3507 /// Every category's target for `kind`, in category order.
3508 const fn targets(&self, kind: ItemKind) -> &[StatusTarget; CATEGORIES.len()] {
3509 match kind {
3510 ItemKind::Task => &self.tasks,
3511 ItemKind::Project => &self.projects,
3512 }
3513 }
3514
3515 fn target(&self, kind: ItemKind, category: StatusCategory) -> &StatusTarget {
3516 &self.targets(kind)[category_position(category)]
3517 }
3518
3519 /// The category a board option name reports for `kind`, or `None` when nothing of that
3520 /// kind maps to it.
3521 fn category_of(&self, kind: ItemKind, option: &str) -> Option<StatusCategory> {
3522 CATEGORIES.into_iter().find(|category| {
3523 self.target(kind, *category)
3524 .option()
3525 .is_some_and(|name| name.eq_ignore_ascii_case(option))
3526 })
3527 }
3528
3529 /// Every option name either kind maps a category to, each once ignoring case, in
3530 /// category order with a task's name before a project's — what the guarded setup asks
3531 /// the `Status` field to hold.
3532 fn wanted(&self) -> Vec<String> {
3533 let mut wanted: Vec<String> = Vec::new();
3534 for category in CATEGORIES {
3535 for kind in STATUS_KINDS {
3536 if let Some(name) = self.target(kind, category).option()
3537 && !wanted.iter().any(|held| held.eq_ignore_ascii_case(name))
3538 {
3539 wanted.push(name.to_owned());
3540 }
3541 }
3542 }
3543 wanted
3544 }
3545
3546 /// The status an item of `kind` reports, from the three things a read of it says: its
3547 /// board `Status` option, whether its issue is closed, and the reason it was closed with.
3548 ///
3549 /// **A closed issue's category follows its closure**, by [`closed_category`]: completed
3550 /// or no reason reads `done`, not planned and duplicate read `cancelled`, and any other
3551 /// reason reads `unknown`. `REOPENED`-while-closed is a state this source can never
3552 /// produce, so it is read permissively rather than refused — reads are faithful, and
3553 /// refusals belong on writes.
3554 ///
3555 /// **Its name follows the closure too, unless the option agrees with it.** The option's
3556 /// own name is kept when this kind's mapping reads that option as the same category, or
3557 /// does not name it at all — so a closed issue in a "Shipped" column reports `done`
3558 /// named `Shipped`. An option the mapping reads as another category contradicts the
3559 /// closure, and so does no option at all: the name is then the option the mapping gives
3560 /// the closure's category, so an issue closed as completed at `Todo` reads `done` named
3561 /// by the board's `done` option, and a destination reading `Todo` as todo is never handed
3562 /// it. An `unknown` closure keeps the option, or `Closed` without one.
3563 ///
3564 /// An open item's option reads through its own kind's mapping, and an option that mapping
3565 /// does not name reads as `Unknown` under its own name.
3566 ///
3567 /// One function of those three rather than of a response, so a narrow status write can
3568 /// answer what a re-read would report by applying it to the state it has just written.
3569 fn status(
3570 &self,
3571 kind: ItemKind,
3572 option: Option<&str>,
3573 closed: bool,
3574 reason: Option<&str>,
3575 ) -> Status {
3576 if closed {
3577 let category = closed_category(reason);
3578 let agrees = option.filter(|option| {
3579 category == StatusCategory::Unknown
3580 || self
3581 .category_of(kind, option)
3582 .is_none_or(|read| read == category)
3583 });
3584 let name = agrees.or_else(|| self.target(kind, category).option());
3585 let fallback = match category {
3586 StatusCategory::Done => "Done",
3587 StatusCategory::Cancelled => "Cancelled",
3588 _ => "Closed",
3589 };
3590 return Status {
3591 category,
3592 name: name.or(option).unwrap_or(fallback).to_owned(),
3593 };
3594 }
3595 let name = option.unwrap_or("Open").to_owned();
3596 Status {
3597 category: self
3598 .category_of(kind, &name)
3599 .unwrap_or(StatusCategory::Unknown),
3600 name,
3601 }
3602 }
3603
3604 /// The option a closed item's `Status` has to hold for its card to agree with its
3605 /// closure, and the category that closure reads as — or `None` when the item is open,
3606 /// its closure reads `unknown`, or its option already agrees.
3607 ///
3608 /// What `task status reconcile` writes: only `done` and `cancelled` closures are brought
3609 /// in line, each to the one option the mapping gives its category — an item with no
3610 /// option, or at one the mapping does not name, included.
3611 fn contradicted(
3612 &self,
3613 kind: ItemKind,
3614 option: Option<&str>,
3615 closed: bool,
3616 reason: Option<&str>,
3617 ) -> Option<(ClosedAs, &str)> {
3618 if !closed {
3619 return None;
3620 }
3621 let closed_as = match closed_category(reason) {
3622 StatusCategory::Done => ClosedAs::Done,
3623 StatusCategory::Cancelled => ClosedAs::Cancelled,
3624 _ => return None,
3625 };
3626 let wanted = self.target(kind, closed_as.category()).option()?;
3627 if option.is_some_and(|option| option.eq_ignore_ascii_case(wanted)) {
3628 return None;
3629 }
3630 Some((closed_as, wanted))
3631 }
3632}
3633
3634/// The category a closed issue reads as, from the reason it was closed with.
3635///
3636/// `COMPLETED`, and no reason at all, is finished work; `NOT_PLANNED` and `DUPLICATE` are both
3637/// work nobody will do here — a duplicate's work is the other issue's — and any other reason
3638/// is one this source cannot read a category from.
3639fn closed_category(reason: Option<&str>) -> StatusCategory {
3640 match reason {
3641 None | Some("COMPLETED") => StatusCategory::Done,
3642 Some("NOT_PLANNED" | "DUPLICATE") => StatusCategory::Cancelled,
3643 Some(_) => StatusCategory::Unknown,
3644 }
3645}
3646
3647impl BoardStatuses {
3648 /// For each kind, the option names it maps a category to that `existing` lacks, ignoring
3649 /// case; a kind lacking none is left out.
3650 fn missing_by_kind(&self, existing: &[StatusOption]) -> Vec<KindMissing> {
3651 STATUS_KINDS
3652 .into_iter()
3653 .filter_map(|kind| {
3654 let missing: Vec<String> = self
3655 .targets(kind)
3656 .iter()
3657 .filter_map(StatusTarget::option)
3658 .filter(|wanted| {
3659 !existing
3660 .iter()
3661 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3662 })
3663 .map(str::to_owned)
3664 .collect();
3665 (!missing.is_empty()).then_some(KindMissing { kind, missing })
3666 })
3667 .collect()
3668 }
3669}
3670
3671impl StatusTarget {
3672 /// The board option this target selects, or `None` for an unmapped one.
3673 fn option(&self) -> Option<&str> {
3674 match self {
3675 Self::Column(name) | Self::Terminal(name, _) => Some(name.as_str()),
3676 Self::Disabled(_) => None,
3677 }
3678 }
3679}
3680
3681// 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.
3682/// One repository this source can create an issue in, as `owner/name`.
3683///
3684/// Every `createIssue` this source sends names one of these: the item's own single
3685/// `repositories` entry, else its parent project issue's repository, else the configured
3686/// [`GitHubProjectsConfig::repository`]. [`GitHubProjectsSource::creation_target`] makes
3687/// that choice and says what it refuses before `createIssue`.
3688// llmlint: ignore-end[comments_earn_their_place, contracts_have_one_source_or_a_drift_gate]
3689#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
3690struct RepositoryTarget {
3691 owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
3692 name: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
3693}
3694
3695impl RepositoryTarget {
3696 fn parse(value: &str) -> Result<Self, SourceError> {
3697 let (owner, name) = value.split_once('/').ok_or_else(|| SourceError::Config {
3698 message: format!(
3699 "repository must be spelled owner/name; {value:?} names no repository"
3700 ),
3701 })?;
3702 if !valid_github_owner(owner) || !valid_github_repository_name(name) {
3703 return Err(SourceError::Config {
3704 message: format!(
3705 "repository must be spelled owner/name with a GitHub login and one \
3706 repository name; {value:?} is not"
3707 ),
3708 });
3709 }
3710 Ok(Self {
3711 owner: owner.to_owned(),
3712 name: name.to_owned(),
3713 })
3714 }
3715
3716 /// The one host whose repositories this source creates issues in, spelled once: it is
3717 /// what [`Self::origin`] renders and what [`Self::from_origin`] accepts.
3718 const HOST: &str = "github.com";
3719
3720 fn origin(&self) -> String {
3721 format!("{}/{}/{}", Self::HOST, self.owner, self.name)
3722 }
3723
3724 /// The repository a normalized origin names, or why it is none this source can create
3725 /// an issue in: another host, or more or fewer than `owner/name` under this one.
3726 fn from_origin(origin: &Repository) -> Result<Self, String> {
3727 let not_here = || {
3728 format!(
3729 "{} is not a {}/owner/name repository",
3730 origin.as_str(),
3731 Self::HOST
3732 )
3733 };
3734 let (host, rest) = origin.as_str().split_once('/').ok_or_else(not_here)?;
3735 if host != Self::HOST {
3736 return Err(not_here());
3737 }
3738 Self::parse(rest).map_err(|_| not_here())
3739 }
3740
3741 fn slug(&self) -> String {
3742 format!("{}/{}", self.owner, self.name)
3743 }
3744}
3745
3746/// A source which reads GitHub afresh for every operation.
3747pub struct GitHubProjectsSource {
3748 /// This source's configured name, used both to tell a far end naming this source
3749 /// from one naming a system it knows nothing about, and to name the instance a
3750 /// status refusal is about.
3751 name: SourceName,
3752 owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after full GitHub-owner validation.
3753 project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after GraphQL-Int validation.
3754 repository: Option<RepositoryTarget>,
3755 endpoint: Url,
3756 token: SecretString,
3757 credential_name: String, // llmlint: ignore[invalid_states_unrepresentable] Private diagnostic value constructed only after environment-name validation.
3758 statuses: BoardStatuses,
3759 /// Where each priority lands on this board, or `None` when this instance holds none.
3760 priorities: Option<PriorityMapping>,
3761 /// The metadata values this instance projects onto board text fields, in configured order.
3762 metadata_fields: Vec<MetadataField>,
3763 /// Built on the first request rather than with the source, as [`Self::client`] says.
3764 client: std::sync::OnceLock<Client>,
3765 /// Built on the first attachment request, as [`Self::asset_client`] says.
3766 asset_client: std::sync::OnceLock<Client>,
3767 /// Every item this source has created in this command, in the order it created them —
3768 /// dropped by [`TaskSource::end_command`].
3769 ///
3770 /// GitHub's `projectV2.items` is eventually consistent: an issue added to a board with
3771 /// `addProjectV2ItemById` is routinely absent from the very next read of that board, so
3772 /// a copy resolving a dependency on an item it had just created refused it as not
3773 /// found. A board read is completed from this — an item remembered here and absent from
3774 /// the read is added back, because the board really does hold it and only the read is
3775 /// behind.
3776 ///
3777 /// It is not a cache of a user's work: nothing is remembered that this process did not
3778 /// itself just write, it lives and dies with the process, and it is never consulted for
3779 /// an item this source did not create.
3780 created: Mutex<Vec<Resolved>>,
3781 /// Every item that already existed and that this source has written in this command, as
3782 /// it wrote it — dropped by [`TaskSource::end_command`].
3783 ///
3784 /// The other half of [`Self::created`], held on the same terms and for the reason a
3785 /// narrowed read needs it: an answer from GitHub's search or from the board's own field
3786 /// filter is an index behind a write this process made moments ago, so a query matching
3787 /// what this process just wrote onto an existing item would otherwise miss it. Nothing
3788 /// is remembered that this process did not itself just write.
3789 updated: Mutex<Vec<Resolved>>,
3790 /// Every issue this source has added a comment to or edited a comment of in this command
3791 /// — dropped by [`TaskSource::end_command`].
3792 ///
3793 /// A comment-activity read is narrowed by GitHub's issue search, whose `updated:` index
3794 /// lags the write that moved an issue's `updatedAt`, and neither [`Self::created`] nor
3795 /// [`Self::updated`] is moved by a comment, so an issue this process had just commented
3796 /// on was missing from such a read — or ruled out by the `updatedAt` its own record held
3797 /// from before — until the index caught up. Each id here is a candidate of every such
3798 /// search-narrowed read, and wherever it is a candidate its comments are read rather than
3799 /// it being ruled out by a stale `updatedAt`; that read is of the issue's own node, so it
3800 /// is current. It holds ids alone: nothing of a comment is remembered. A comment another
3801 /// process wrote is still found only once the index has it.
3802 commented: Mutex<Vec<NativeId>>,
3803 /// How fast this source writes, and how long it waits out a refusal.
3804 pacing: Pacing,
3805 /// When the last content-creating mutation finished, or the moment the furthest-out
3806 /// reserved slot releases the next one, whichever is later — so the one after it can be
3807 /// spaced from that, when `pacing.min_mutation_interval_ms` asks for a spacing. See
3808 /// [`GitHubProjectsSource::paced`] for why completion rather than release is what it is
3809 /// measured from.
3810 last_mutation: Mutex<Option<Duration>>,
3811 /// GitHub's secondary windows as this source has charged them; see the `pacing` module.
3812 windows: Mutex<pacing::Windows>,
3813 /// The tasks a project holds, as a copy's batched read of the project and every one of them
3814 /// found them — see [`TaskSource::read_ahead_for_copy`] — for the length of one command, so
3815 /// a listing of that project's tasks is answered without a read of its own.
3816 task_children: Mutex<BTreeMap<NativeId, Vec<Resolved>>>,
3817 /// The web address of each issue this source created in the current command, as its
3818 /// `createIssue` answered it — what [`TaskSource::written_location`] answers, without a
3819 /// request.
3820 written: Mutex<BTreeMap<NativeId, String>>,
3821 /// What a copy has held back of the items it created, between
3822 /// [`TaskSource::begin_deferred_writes`] and [`TaskSource::finish_writes`] — `None` while
3823 /// nothing is being held back, when every write lands whole as it is made.
3824 deferred: Mutex<Option<Deferred>>,
3825 /// Each existing issue's update held back while writes are, in the order it was made: sent
3826 /// together by [`Self::send_held_updates`] before any other request goes out, so nothing
3827 /// this source sends or reads ever sees the board without them.
3828 held_updates: Mutex<Vec<HeldUpdate>>,
3829 clock: SharedClock,
3830 numeric_repositories: tokio::sync::Mutex<BTreeMap<RepositoryTarget, std::num::NonZeroU64>>,
3831 /// The board as this process last read it, for the length of one command — dropped by
3832 /// [`TaskSource::end_command`].
3833 ///
3834 /// A copy of a project used to re-read the whole board, paged, before writing each of
3835 /// its items, which is by far the largest part of a copy's request count and none of
3836 /// its work. Nothing else changes this board while a command runs — this source's own
3837 /// writes are the only writer — so one read answers them all.
3838 ///
3839 /// It is not a store of a user's work and it is not the cache the no-persistence
3840 /// invariant forbids: it lives and dies with the process exactly as `created` does,
3841 /// nothing is written down, and [`Self::board`] still completes it from `created`, so
3842 /// an item this command created and then depends on resolves whether or not GitHub's
3843 /// own eventually-consistent read has caught up. A write to an item already on the
3844 /// board updates the entry here too, so what this holds is the last read plus this
3845 /// process's own writes rather than a snapshot taken before them.
3846 board_cache: Mutex<Option<Board>>,
3847 /// Every issue this board's own search reported, for the length of one command — dropped
3848 /// by [`TaskSource::end_command`].
3849 ///
3850 /// The second half of a board read, and cached for the same reason and on the same
3851 /// terms as the first: it lives and dies with the process, nothing is written down, and
3852 /// a write this process makes updates the entry here exactly as it updates the one in
3853 /// [`Self::board_cache`]. One read answers every question a command asks, so a command
3854 /// that lists this board's projects and its tasks pays for one search rather than two.
3855 search_cache: Mutex<Option<Vec<Resolved>>>,
3856 /// What each narrowed question GitHub was asked answered, keyed by that question, for
3857 /// the length of one command — dropped by [`TaskSource::end_command`].
3858 ///
3859 /// The narrowed counterpart of [`Self::search_cache`], held on the same terms: it lives
3860 /// and dies with the process, nothing is written down, a write this process makes
3861 /// updates the entry here as it updates the other two, and every answer is completed
3862 /// with this process's own writes each time it is given. A command that asks the same
3863 /// narrowed question twice — a wait polling for its own items, a listing repeated after a
3864 /// write — pays for it once, which is what the whole-board read it replaced gave it.
3865 narrowed_cache: Mutex<BTreeMap<String, Vec<Resolved>>>,
3866 search_next: Mutex<BTreeMap<String, Option<String>>>,
3867 /// Records already resolved in this command, reused by writes and for comment identity.
3868 /// Explicit item reads still reach GitHub. Nothing is persisted, and
3869 /// [`TaskSource::end_command`] drops every record, so a write in the next command reads
3870 /// its item as a person has since left it.
3871 resolved_cache: Mutex<BTreeMap<NativeId, Resolved>>,
3872 /// What [`TaskSource::read_ahead_for_copy`] read, by id — `None` for an id that read
3873 /// answered as holding nothing — each taken by the next read of that item by id and by
3874 /// nothing else, dropped by any mutation naming it, and cleared by
3875 /// [`TaskSource::end_command`]. So a read answered from it is the copy's own
3876 /// read-before-write, made in the same request as the others' rather than on its own.
3877 read_ahead: Mutex<BTreeMap<NativeId, Option<Resolved>>>,
3878 /// The origins [`TaskSource::read_ahead_origins_for_copy`] was asked about and has not sent
3879 /// yet: held for the [`TaskSource::read_ahead_for_copy`] that follows it, which sends them —
3880 /// beside the members of the one project it names, when it names one — or for the next
3881 /// request this source makes, whichever comes first.
3882 pending_origins: Mutex<Vec<String>>,
3883 /// Each project's members as a lookup carrying them read them in this command, trimmed to
3884 /// their classification and repositories, taken by [`TaskSource::member_classifications`]
3885 /// of that project and by nothing else, and cleared by [`TaskSource::end_command`].
3886 member_classes: Mutex<BTreeMap<NativeId, Vec<MemberClass>>>,
3887 /// Every item a listing of one project's members carried whole in this command — the
3888 /// project issue and each of its sub-issues — kept for [`TaskSource::read_ahead_for_copy`]
3889 /// alone, which takes each one it is asked for into [`Self::read_ahead`] instead of reading
3890 /// it again. No other read is answered from it, so a read by id outside a copy's
3891 /// read-before-write reads GitHub as it always did. Dropped by any mutation naming an
3892 /// item, and cleared by [`TaskSource::end_command`].
3893 carried: Mutex<BTreeMap<NativeId, Resolved>>,
3894 /// Items an issue read by reference reached, held for the one status write each was read
3895 /// for and taken out by it alone — see [`Self::take_held`] — because that read selects only
3896 /// what such a write needs. Dropped by [`TaskSource::end_command`].
3897 held_cache: Mutex<BTreeMap<NativeId, Resolved>>,
3898 /// Each project's sub-issues as GitHub answered them, keyed by the selector they were
3899 /// asked for under, with the project that selector named — for the length of one command,
3900 /// dropped by [`TaskSource::end_command`].
3901 ///
3902 /// A caller pages through a project's tasks one engine page at a time, and every page
3903 /// is cut from the whole list of them, so without this each page walked every
3904 /// [`graphql::SUB_ISSUES`] page again and a project of `n` listing pages cost `n²`
3905 /// requests to read once. Held on the terms [`Self::narrowed_cache`] is: it lives and
3906 /// dies with the process, nothing is written down, a write this process makes updates or
3907 /// removes the entry here as it does there, and every answer is completed with this
3908 /// process's own writes each time it is given.
3909 children_cache: Mutex<ProjectChildren>,
3910 /// The board's own id and field definitions as this process last read them on their
3911 /// own, for the length of one command — dropped by [`TaskSource::end_command`].
3912 ///
3913 /// What a write needs of the board and its item does not say, read once per command
3914 /// rather than once per item written, on the terms [`Self::board_cache`] is held on: it
3915 /// lives and dies with the process and nothing is written down. It holds no item and so
3916 /// can answer no question about one — see [`Self::board_fields`].
3917 fields_cache: Mutex<Option<BoardFields>>,
3918 /// Each destination repository's node id, resolved once per repository
3919 /// rather than per issue created.
3920 ///
3921 /// A repository's node id does not change, and re-reading it for every issue of a copy
3922 /// spent one request per item on an answer this source already had. It is a map rather
3923 /// than one entry because a copy files each item in the repository its own
3924 /// `repositories` field names, so a plan across five repositories asks GitHub five
3925 /// times and not once per item.
3926 repository_cache: Mutex<BTreeMap<RepositoryTarget, String>>,
3927 /// What every request this source sends is recorded into.
3928 ///
3929 /// Ordinary code path, not a mode: [`Self::send_once`] records into it at the one place
3930 /// a request leaves this crate, so nothing has to be switched on for a session to be
3931 /// counted. It is shared rather than owned so a caller accounting for a whole session —
3932 /// its own schema verification, board lookups, residue sweep and cleanup beside this
3933 /// source's reads and writes — adds up one accounting instead of two. See
3934 /// [`accounting`] for what a record carries and what a session's spend is and is not.
3935 ledger: Arc<Accounting>,
3936 /// What [`expected`] holds for a write the store's cache expects: the journal while one is
3937 /// open, the issues primed from the cache, and the comments known on each.
3938 expected: expected::Held,
3939}
3940
3941/// GitHub's closed single-select color vocabulary.
3942#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3943#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
3944pub enum StatusOptionColor {
3945 /// Gray.
3946 Gray,
3947 /// Blue.
3948 Blue,
3949 /// Green.
3950 Green,
3951 /// Yellow.
3952 Yellow,
3953 /// Purple.
3954 Purple,
3955 /// Red.
3956 Red,
3957 /// Orange.
3958 Orange,
3959 /// Pink.
3960 Pink,
3961}
3962
3963/// Whether a guarded board setup — of the fields, or of the Status options alone — plans or
3964/// applies its additions.
3965#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3966pub enum SetupMode {
3967 /// Read without mutation.
3968 Plan,
3969 /// Apply and verify.
3970 Apply,
3971}
3972
3973/// The name [`SetupMode`] had when Status was the one field set up, kept so a caller written
3974/// against it goes on compiling.
3975pub type StatusOptionsMode = SetupMode;
3976
3977/// The explicit result of the requested operation.
3978#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3979#[serde(rename_all = "kebab-case")]
3980pub enum StatusOptionsOutcome {
3981 /// A read-only plan.
3982 Planned,
3983 /// Apply found nothing missing.
3984 Unchanged,
3985 /// Additions were applied and verified.
3986 Applied,
3987}
3988
3989/// A GitHub single-select option's opaque GraphQL node identifier.
3990#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3991#[serde(transparent)]
3992pub struct StatusOptionId(#[schemars(length(min = 1))] String);
3993
3994impl TryFrom<String> for StatusOptionId {
3995 type Error = String;
3996
3997 fn try_from(id: String) -> Result<Self, Self::Error> {
3998 if id.trim().is_empty() {
3999 return Err("a GitHub Status option id cannot be blank".to_owned());
4000 }
4001 Ok(Self(id))
4002 }
4003}
4004
4005/// One existing or proposed option in a guarded Status-field update.
4006#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4007pub struct StatusOption {
4008 /// GitHub's stable id.
4009 pub id: StatusOptionId,
4010 /// The visible option name.
4011 pub name: ColumnName,
4012 /// GitHub's single-select color token.
4013 pub color: StatusOptionColor,
4014 /// The option description, including an empty one.
4015 pub description: String,
4016}
4017
4018/// One board item's Status assignment, retained as recovery data.
4019#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4020pub struct StatusAssignment {
4021 /// The project item id whose assignment this is.
4022 // llmlint: ignore[invalid_states_unrepresentable] This opaque GraphQL node ID is
4023 // carried verbatim as operator recovery data; introducing a semantic type would claim
4024 // validation rules GitHub does not publish and no operation here interprets.
4025 pub item_id: String,
4026 /// The selected option, absent when the item has no status.
4027 #[serde(skip_serializing_if = "Option::is_none")]
4028 pub option: Option<AssignedStatusOption>,
4029}
4030
4031/// The inseparable id and name of an assigned option.
4032#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4033pub struct AssignedStatusOption {
4034 /// GitHub's stable id.
4035 pub id: StatusOptionId,
4036 /// The visible name.
4037 pub name: ColumnName,
4038}
4039
4040/// The plan and verified outcome of reconciling configured Status options.
4041#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4042pub struct StatusOptionsReport {
4043 /// The configured source name.
4044 pub source: SourceName,
4045 /// Configured option names absent before the operation.
4046 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a
4047 // `ColumnName` and has therefore already passed its nonblank validation; retaining the
4048 // serialized string here preserves the report's intentionally simple public contract.
4049 pub missing: Vec<String>,
4050 /// What the requested operation did.
4051 pub outcome: StatusOptionsOutcome,
4052 /// The complete option list observed before any mutation.
4053 pub existing: Vec<StatusOption>,
4054}
4055
4056#[derive(Debug, Clone, PartialEq, Eq)]
4057struct StatusSnapshot {
4058 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4059 // passed back as the mutation's project identity; a newtype could enforce no stronger
4060 // invariant because GitHub publishes no grammar for it.
4061 board_id: String,
4062 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4063 // passed back as the mutation's field identity; a newtype could enforce no stronger
4064 // invariant because GitHub publishes no grammar for it.
4065 field_id: String,
4066 options: Vec<StatusOption>,
4067 assignments: Vec<StatusAssignment>,
4068}
4069
4070/// The name of the board field a status is held in.
4071const STATUS_FIELD: &str = "Status";
4072
4073/// Every item's value of each field `report` names, as it stood before the setup wrote
4074/// anything — what a person puts back when the setup is refused part way.
4075fn recovery(report: &FieldsReport, before: &BoardSnapshot) -> Result<String, SourceError> {
4076 let assignments: BTreeMap<&str, Vec<StatusAssignment>> = report
4077 .fields
4078 .iter()
4079 .map(|field| (field.field.name(), before.assignments(field.field)))
4080 .collect();
4081 serde_json::to_string_pretty(&assignments).map_err(|error| SourceError::Malformed {
4082 message: format!("cannot render the pre-write field recovery snapshot: {error}"),
4083 })
4084}
4085
4086/// One board field the guarded setup reads and writes — every one it reads, and the only
4087/// ones it writes.
4088#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, schemars::JsonSchema)]
4089pub enum BoardField {
4090 /// The single-select `Status` field every instance's `status_mapping` resolves into.
4091 Status,
4092 /// The single-select `Priority` field an instance's `priority_mapping` resolves into.
4093 Priority,
4094}
4095
4096impl BoardField {
4097 /// The field's name on the board.
4098 #[must_use]
4099 pub const fn name(self) -> &'static str {
4100 match self {
4101 Self::Status => STATUS_FIELD,
4102 Self::Priority => PRIORITY_FIELD,
4103 }
4104 }
4105
4106 /// The field a board calls `name`, or `None` for one this setup does not own.
4107 fn named(name: &str) -> Option<Self> {
4108 [Self::Status, Self::Priority]
4109 .into_iter()
4110 .find(|field| field.name() == name)
4111 }
4112}
4113
4114/// What the guarded setup did to one field.
4115#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4116#[serde(rename_all = "kebab-case")]
4117pub enum FieldOutcome {
4118 /// A read-only plan.
4119 Planned,
4120 /// Apply found the field there with every configured option.
4121 Unchanged,
4122 /// Missing options were added to the field that was there, and verified.
4123 Applied,
4124 /// The field was not there; it was created holding the configured options, and verified.
4125 Created,
4126}
4127
4128/// One field's plan, or its verified outcome.
4129#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4130pub struct FieldReport {
4131 /// Which field.
4132 pub field: BoardField,
4133 /// Whether the board had the field before the operation.
4134 // llmlint: ignore[invalid_states_unrepresentable] `exists` beside `outcome` is the report's
4135 // wire shape as its consumer's contract fixes it — `{"field", "exists", "missing",
4136 // "outcome", "existing"}` — so folding one into the other would change a published JSON
4137 // shape. The contradictory pairings cannot be built: `GitHubProjectsSource::fields` is the
4138 // one constructor, and it derives `outcome` from `exists` in one match.
4139 pub exists: bool,
4140 /// Configured option names the field lacked before the operation — every one of them,
4141 /// in the order a new field lists them, when the field was not there at all.
4142 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
4143 // mapping name and has therefore already passed its nonblank validation; the serialized
4144 // string is the report's intentionally simple public contract, as `StatusOptionsReport`'s is.
4145 pub missing: Vec<String>,
4146 /// For the `Status` field, which item kind each missing name is configured for: one
4147 /// entry per kind `status_mapping` names a missing option for, task before project, each
4148 /// listing that kind's missing names in category order. A name both kinds use is in
4149 /// both. Empty — and left out of the JSON — when nothing is missing, and always for
4150 /// `Priority`, which only a task holds.
4151 #[serde(default, skip_serializing_if = "Vec::is_empty")]
4152 // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
4153 // both SDKs model an absent `kinds` as an empty list rather than as `null`.
4154 #[schemars(!skip_serializing_if)]
4155 pub kinds: Vec<KindMissing>,
4156 /// What the requested operation did.
4157 pub outcome: FieldOutcome,
4158 /// The field's complete option list observed before any mutation; empty when the field
4159 /// was not there.
4160 pub existing: Vec<StatusOption>,
4161}
4162
4163/// The `Status` option names one item kind's `status_mapping` names that the field lacked.
4164#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4165pub struct KindMissing {
4166 /// The kind these names are configured for.
4167 pub kind: ItemKind,
4168 /// The names that kind maps a category to and the field lacked, in category order.
4169 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
4170 // mapping name, as `FieldReport::missing`'s do, and the serialized string is the report's
4171 // intentionally simple public contract.
4172 pub missing: Vec<String>,
4173}
4174
4175/// The plan and verified outcome of setting up every field a source's configuration names.
4176#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4177pub struct FieldsReport {
4178 /// The configured source name.
4179 pub source: SourceName,
4180 /// `Status`, always, and `Priority` when the source sets `priority_mapping`.
4181 // llmlint: ignore[invalid_states_unrepresentable] A list is the report's wire shape as its
4182 // consumer's contract fixes it — `{"source", "fields": [...]}` — so a struct with one member
4183 // per field would change a published JSON shape. The states the list could hold and the
4184 // contract forbids cannot be built: `GitHubProjectsSource::fields` is the one constructor,
4185 // and it pushes `Status` first and exactly once, then `Priority` exactly when configured.
4186 pub fields: Vec<FieldReport>,
4187 /// Each board text field the source's `metadata_fields` projects a value onto, in
4188 /// configured order. Empty — and left out of the JSON — when it projects none.
4189 #[serde(default, skip_serializing_if = "Vec::is_empty")]
4190 // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
4191 // both SDKs model an absent list as an empty one rather than as `null`.
4192 #[schemars(!skip_serializing_if)]
4193 pub metadata_fields: Vec<MetadataFieldReport>,
4194}
4195
4196/// The type of a board field that is not a text field, as GitHub names it — its `dataType`, or
4197/// its GraphQL type when it has none — which is what a projected field's conflict reports.
4198///
4199/// Never empty, never beginning with whitespace, and never `TEXT`: a text field of that name is
4200/// no conflict.
4201// The schema states that rule as `NON_TEXT_FIELD_TYPE_PATTERN`, so a generated SDK model
4202// refuses exactly what `try_from` below does.
4203#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4204#[serde(transparent)]
4205pub struct NonTextFieldType(#[schemars(regex(pattern = NON_TEXT_FIELD_TYPE_PATTERN))] String);
4206
4207/// What a [`NonTextFieldType`] may be, as the emitted schema states it: a first character that
4208/// is not whitespace, and anything but exactly `TEXT` — spelled without a lookahead, which
4209/// neither SDK's validator can be relied on to support.
4210const NON_TEXT_FIELD_TYPE_PATTERN: &str =
4211 r"^(?:[^T\s]|T(?:[^E]|$)|TE(?:[^X]|$)|TEX(?:[^T]|$)|TEXT[\s\S])";
4212
4213impl TryFrom<String> for NonTextFieldType {
4214 type Error = String;
4215
4216 fn try_from(kind: String) -> Result<Self, Self::Error> {
4217 if kind.is_empty() || kind.starts_with(char::is_whitespace) {
4218 return Err("a board field's type cannot be blank or begin with whitespace".to_owned());
4219 }
4220 if kind == "TEXT" {
4221 return Err("a TEXT field is no conflict for a projected text field".to_owned());
4222 }
4223 Ok(Self(kind))
4224 }
4225}
4226
4227impl std::fmt::Display for NonTextFieldType {
4228 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4229 formatter.write_str(&self.0)
4230 }
4231}
4232
4233/// One projected metadata text field's plan, or its verified outcome.
4234///
4235/// The setup creates a missing one as a text field and never alters a field that is there:
4236/// a same-named field of another type is reported in `conflict` and left as it is.
4237#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4238pub struct MetadataFieldReport {
4239 /// The board field's name.
4240 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.
4241 /// The metadata key the value is read from.
4242 pub key: String, // llmlint: ignore[invalid_states_unrepresentable] As `field`: a validated configuration value reported back as written.
4243 /// The object keys walked inside that key's value; empty for the key's own value.
4244 pub path: Vec<String>,
4245 /// Whether the board had a text field of that name before the operation.
4246 // 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.
4247 pub exists: bool,
4248 /// The type of a field of that name that is not a text field, which the setup leaves as
4249 /// it is; absent — and left out of the JSON — when there is none.
4250 #[serde(default, skip_serializing_if = "Option::is_none")]
4251 pub conflict: Option<NonTextFieldType>,
4252 /// What the requested operation did: `planned`; `created` for a field that was not
4253 /// there; `unchanged` for one that was, and for a conflict, which is never changed.
4254 pub outcome: FieldOutcome,
4255}
4256
4257/// Which options one field is configured with, in the order a new field would list them.
4258struct FieldPlan {
4259 field: BoardField,
4260 wanted: Vec<String>,
4261}
4262
4263/// One single-select field as the guarded setup snapshots it.
4264#[derive(Debug, Clone, PartialEq, Eq)]
4265struct SnapshotField {
4266 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4267 // passed back as the mutation's field identity; a newtype could enforce no stronger
4268 // invariant because GitHub publishes no grammar for it.
4269 field_id: String,
4270 options: Vec<StatusOption>,
4271}
4272
4273/// Every single-select field of a board and every item's value of each.
4274#[derive(Debug, Clone, PartialEq, Eq)]
4275struct BoardSnapshot {
4276 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4277 // passed back as the mutation's project identity; a newtype could enforce no stronger
4278 // invariant because GitHub publishes no grammar for it.
4279 board_id: String,
4280 fields: BTreeMap<BoardField, SnapshotField>,
4281 /// Each board item's id, and its value of each field this setup owns that it holds one of.
4282 items: Vec<(String, BTreeMap<BoardField, AssignedStatusOption>)>,
4283}
4284
4285impl BoardSnapshot {
4286 /// Every item's value of `field`, in board order — the recovery data a drift refusal
4287 /// carries.
4288 fn assignments(&self, field: BoardField) -> Vec<StatusAssignment> {
4289 self.items
4290 .iter()
4291 .map(|(item_id, values)| StatusAssignment {
4292 item_id: item_id.clone(),
4293 option: values.get(&field).cloned(),
4294 })
4295 .collect()
4296 }
4297}
4298
4299impl GitHubProjectsSource {
4300 /// Report missing configured Status options and, when `apply` is true, add them with
4301 /// a whole-list mutation that preserves every existing id and verifies the result.
4302 ///
4303 /// # Errors
4304 ///
4305 /// Refuses a board without a single-select `Status` field. A post-write difference in
4306 /// any pre-existing option id or item assignment is refused with the complete pre-write
4307 /// assignment snapshot in the diagnostic for recovery.
4308 // llmlint: ignore[changed_behavior_has_e2e] The CLI journeys cover plan, no-op apply,
4309 // successful mutation, both drift refusals, source selection, missing Status, casing,
4310 // and paging. Transport errors remain the shared `graphql` boundary's behavior rather
4311 // than a new status-options behavior, and the pinned-schema test prevents valid GitHub
4312 // responses from entering the defensive malformed-response branches below.
4313 pub async fn status_options(
4314 &self,
4315 mode: StatusOptionsMode,
4316 ) -> Result<StatusOptionsReport, SourceError> {
4317 let before = self.status_snapshot().await?;
4318 // A terminal category's option is as configured as an open one's: a terminal
4319 // write validates it before closing and refuses when the board lacks it. Both
4320 // kinds' names are options of the one field, so both are asked for.
4321 let missing = self
4322 .statuses
4323 .wanted()
4324 .into_iter()
4325 .filter(|wanted| {
4326 !before
4327 .options
4328 .iter()
4329 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4330 })
4331 .collect::<Vec<_>>();
4332 let report = StatusOptionsReport {
4333 source: self.name.clone(),
4334 missing: missing.clone(),
4335 outcome: match (mode, missing.is_empty()) {
4336 (StatusOptionsMode::Plan, _) => StatusOptionsOutcome::Planned,
4337 (StatusOptionsMode::Apply, true) => StatusOptionsOutcome::Unchanged,
4338 (StatusOptionsMode::Apply, false) => StatusOptionsOutcome::Applied,
4339 },
4340 existing: before.options.clone(),
4341 };
4342 if mode == StatusOptionsMode::Plan || missing.is_empty() {
4343 return Ok(report);
4344 }
4345 let mut options = before
4346 .options
4347 .iter()
4348 .map(|option| {
4349 json!({
4350 "id": option.id, "name": option.name, "color": option.color,
4351 "description": option.description,
4352 })
4353 })
4354 .collect::<Vec<_>>();
4355 options.extend(missing.iter().map(|name| {
4356 json!({
4357 "name": name, "color": "GRAY", "description": ""
4358 })
4359 }));
4360 self.graphql(
4361 graphql::STATUS_OPTIONS_UPDATE,
4362 json!({"input": {
4363 "projectId": before.board_id, "fieldId": before.field_id,
4364 "singleSelectOptions": options,
4365 }}),
4366 )
4367 .await?;
4368 let after = self.status_snapshot().await?;
4369 let options_preserved = before
4370 .options
4371 .iter()
4372 .all(|old| after.options.iter().any(|new| new == old));
4373 let additions_present = missing.iter().all(|wanted| {
4374 after
4375 .options
4376 .iter()
4377 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4378 });
4379 if !options_preserved || !additions_present || after.assignments != before.assignments {
4380 let recovery = serde_json::to_string_pretty(&before.assignments).map_err(|error| {
4381 SourceError::Malformed {
4382 message: format!("cannot render pre-write Status recovery snapshot: {error}"),
4383 }
4384 })?;
4385 return Err(SourceError::Refused {
4386 message: format!(
4387 "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}"
4388 ),
4389 });
4390 }
4391 Ok(report)
4392 }
4393
4394 /// A fresh snapshot of the Status field and every board item's assignment of it.
4395 ///
4396 /// # Errors
4397 ///
4398 /// Refuses a board without a single-select `Status` field, and one the token cannot see.
4399 async fn status_snapshot(&self) -> Result<StatusSnapshot, SourceError> {
4400 // Status alone, as this operation has always read it: a `Priority` field is another
4401 // operation's, so nothing about it can refuse this one.
4402 let mut board = self.board_snapshot(&[BoardField::Status]).await?;
4403 let field = board
4404 .fields
4405 .remove(&BoardField::Status)
4406 .ok_or_else(|| self.no_status_field())?;
4407 Ok(StatusSnapshot {
4408 assignments: board.assignments(BoardField::Status),
4409 board_id: board.board_id,
4410 field_id: field.field_id,
4411 options: field.options,
4412 })
4413 }
4414
4415 /// The refusal a board with no `Status` field is answered with by the guarded setup.
4416 fn no_status_field(&self) -> SourceError {
4417 SourceError::Refused {
4418 message: format!("source {} board has no Status field", self.name),
4419 }
4420 }
4421
4422 // llmlint: ignore-block[changed_behavior_has_e2e] Valid snapshot shapes are exercised through
4423 // the real CLI loopback journey, including pagination. The individual malformed guards
4424 // are defensive validation of a schema-pinned third-party response, not separate user
4425 // journeys; drift and missing-field failures cover the operation's recovery behavior.
4426 /// A fresh snapshot of each of the `owned` fields on the board, with its options, and of
4427 /// every board item's value of each, walked to the end of the board's items. A field not
4428 /// in `owned` is read past whatever it holds.
4429 async fn board_snapshot(&self, owned: &[BoardField]) -> Result<BoardSnapshot, SourceError> {
4430 let mut after: Option<String> = None;
4431 let mut snapshot: Option<BoardSnapshot> = None;
4432 loop {
4433 let data = self
4434 .graphql(
4435 graphql::STATUS_OPTIONS_SNAPSHOT,
4436 json!({
4437 "owner": self.owner, "number": self.project_number,
4438 "first": MAX_PAGE_SIZE, "after": after, "nestedFirst": MAX_PAGE_SIZE,
4439 }),
4440 )
4441 .await?;
4442 let board = data
4443 .pointer("/owner/projectV2")
4444 .filter(|board| board.is_object())
4445 .ok_or_else(|| SourceError::Refused {
4446 message: format!(
4447 "source {} has no accessible GitHub Projects board",
4448 self.name
4449 ),
4450 })?;
4451 if board
4452 .pointer("/fields/pageInfo/hasNextPage")
4453 .and_then(Value::as_bool)
4454 != Some(false)
4455 {
4456 return Err(SourceError::Malformed {
4457 message:
4458 "GitHub project fields is incomplete or has malformed pageInfo.hasNextPage"
4459 .into(),
4460 });
4461 }
4462 let mut fields = BTreeMap::new();
4463 // Only the fields this setup owns, by name: a node the single-select fragment did not
4464 // match carries no name, and a person's own single-select field — a `Size`, a
4465 // `Team` — is none of this setup's business, so nothing about it can refuse one. A
4466 // `Status` or `Priority` field without its options is malformed, not absent.
4467 // 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.
4468 for (owned, field) in board
4469 .pointer("/fields/nodes")
4470 .and_then(Value::as_array)
4471 .ok_or_else(|| SourceError::Malformed {
4472 message: "GitHub project fields.nodes is not an array".into(),
4473 })?
4474 .iter()
4475 .filter_map(|field| {
4476 let named = BoardField::named(field.get("name")?.as_str()?)?;
4477 owned.contains(&named).then_some((named, field))
4478 })
4479 {
4480 let options = field
4481 .get("options")
4482 .and_then(Value::as_array)
4483 .ok_or_else(|| SourceError::Malformed {
4484 message: "GitHub single-select field options is not an array".into(),
4485 })?
4486 .iter()
4487 .map(|option| {
4488 Ok(StatusOption {
4489 id: StatusOptionId::try_from(required_str(option, "id")?.to_owned())
4490 .map_err(|message| SourceError::Malformed { message })?,
4491 name: ColumnName::try_from(required_str(option, "name")?.to_owned())
4492 .map_err(|message| SourceError::Malformed {
4493 message: format!(
4494 "GitHub single-select option name is invalid: {message}"
4495 ),
4496 })?,
4497 color: serde_json::from_value(
4498 option.get("color").cloned().unwrap_or(Value::Null),
4499 )
4500 .map_err(|error| {
4501 SourceError::Malformed {
4502 message: format!(
4503 "GitHub single-select option color is invalid: {error}"
4504 ),
4505 }
4506 })?,
4507 description: optional_str(option, "description")?
4508 .unwrap_or_default()
4509 .to_owned(),
4510 })
4511 })
4512 .collect::<Result<Vec<_>, SourceError>>()?;
4513 let snapshot = SnapshotField {
4514 field_id: required_nonblank_str(field, "id")?.to_owned(),
4515 options,
4516 };
4517 // A board's field names are unique, so a second one is an answer that cannot
4518 // say which field the setup would act on — refused rather than one chosen.
4519 if fields.insert(owned, snapshot).is_some() {
4520 return Err(SourceError::Malformed {
4521 message: format!(
4522 "GitHub answered two {} fields for this board",
4523 owned.name()
4524 ),
4525 });
4526 }
4527 }
4528 let board_id = required_nonblank_str(board, "id")?.to_owned();
4529 let current = snapshot.get_or_insert_with(|| BoardSnapshot {
4530 board_id,
4531 fields,
4532 items: Vec::new(),
4533 });
4534 let items = board
4535 .pointer("/items/nodes")
4536 .and_then(Value::as_array)
4537 .ok_or_else(|| SourceError::Malformed {
4538 message: "GitHub project items.nodes is not an array".into(),
4539 })?;
4540 for item in items {
4541 let field_values =
4542 item.get("fieldValues")
4543 .ok_or_else(|| SourceError::Malformed {
4544 message: "GitHub project item is missing fieldValues".into(),
4545 })?;
4546 if field_values
4547 .pointer("/pageInfo/hasNextPage")
4548 .and_then(Value::as_bool)
4549 != Some(false)
4550 {
4551 return Err(SourceError::Malformed {
4552 message: "GitHub project item fieldValues is incomplete or has malformed pageInfo.hasNextPage".into(),
4553 });
4554 }
4555 let values = item
4556 .pointer("/fieldValues/nodes")
4557 .and_then(Value::as_array)
4558 .ok_or_else(|| SourceError::Malformed {
4559 message: "GitHub project item fieldValues.nodes is not an array".into(),
4560 })?;
4561 let item_id = required_nonblank_str(item, "id")?;
4562 let mut assigned = BTreeMap::new();
4563 for value in values {
4564 let Some(field) = value
4565 .pointer("/field/name")
4566 .and_then(Value::as_str)
4567 .and_then(BoardField::named)
4568 .filter(|field| owned.contains(field))
4569 else {
4570 continue;
4571 };
4572 let held = assigned.insert(
4573 field,
4574 AssignedStatusOption {
4575 id: StatusOptionId::try_from(
4576 required_str(value, "optionId")?.to_owned(),
4577 )
4578 .map_err(|message| SourceError::Malformed { message })?,
4579 name: ColumnName::try_from(required_str(value, "name")?.to_owned())
4580 .map_err(|message| SourceError::Malformed {
4581 message: format!(
4582 "GitHub assigned {} name is invalid: {message}",
4583 field.name()
4584 ),
4585 })?,
4586 },
4587 );
4588 // An item holds one value of a field, so a second one leaves no way to
4589 // tell which it holds — and a verification or recovery built on either
4590 // could restore the wrong one.
4591 if held.is_some() {
4592 return Err(SourceError::Malformed {
4593 message: format!(
4594 "GitHub answered two {} values for board item {item_id}",
4595 field.name()
4596 ),
4597 });
4598 }
4599 }
4600 current.items.push((item_id.to_owned(), assigned));
4601 }
4602 let page = board.get("items").ok_or_else(|| SourceError::Malformed {
4603 message: "GitHub project is missing items".into(),
4604 })?;
4605 let has_next = page
4606 .pointer("/pageInfo/hasNextPage")
4607 .and_then(Value::as_bool)
4608 .ok_or_else(|| SourceError::Malformed {
4609 message: "GitHub project items.pageInfo.hasNextPage is not a boolean".into(),
4610 })?;
4611 if !has_next {
4612 break;
4613 }
4614 let next =
4615 required_nonblank_str(page.get("pageInfo").unwrap_or(&Value::Null), "endCursor")?;
4616 validate_cursor_progress(after.as_deref(), next)?;
4617 after = Some(next.to_owned());
4618 }
4619 snapshot.ok_or_else(|| SourceError::Malformed {
4620 message: "GitHub returned no board field snapshot".into(),
4621 })
4622 }
4623 // llmlint: ignore-end[changed_behavior_has_e2e]
4624
4625 /// Report every board field this source's configuration names and, with
4626 /// [`SetupMode::Apply`], set each up: add the options a field lacks, and create
4627 /// the `Priority` field when the board has none.
4628 ///
4629 /// The fields are `Status`, always, with the options `status_mapping` resolves to; and
4630 /// `Priority`, when `priority_mapping` is set, with its four mapped options — created in
4631 /// the order urgent, high, medium, low. An option a field already has keeps its id, name,
4632 /// color and description: the whole option list goes back with every existing id, because
4633 /// a re-minted id clears every item's value.
4634 ///
4635 /// # Errors
4636 ///
4637 /// Refuses a board without a single-select `Status` field. After an apply the board is
4638 /// read again, and a pre-existing option or any item's value of either field that moved is
4639 /// refused with the complete pre-write assignments in the diagnostic, for recovery.
4640 // llmlint: ignore[changed_behavior_has_e2e] The `sources fields` journeys drive plan,
4641 // unchanged apply, a created field, an added option to each field, drift refusal, a board
4642 // with no Status field and a non-github-projects source through the compiled CLI against
4643 // the loopback board. Transport errors are the shared `graphql` boundary's behavior.
4644 pub async fn fields(&self, mode: SetupMode) -> Result<FieldsReport, SourceError> {
4645 let owned: Vec<BoardField> = if self.priorities.is_some() {
4646 vec![BoardField::Status, BoardField::Priority]
4647 } else {
4648 vec![BoardField::Status]
4649 };
4650 let before = self.board_snapshot(&owned).await?;
4651 let mut plans = vec![FieldPlan {
4652 field: BoardField::Status,
4653 wanted: self.statuses.wanted(),
4654 }];
4655 if !before.fields.contains_key(&BoardField::Status) {
4656 return Err(self.no_status_field());
4657 }
4658 if let Some(mapping) = &self.priorities {
4659 plans.push(FieldPlan {
4660 field: BoardField::Priority,
4661 wanted: mapping.names().map(str::to_owned).collect(),
4662 });
4663 }
4664 // The snapshot reads single-select fields alone, so a field it did not find may still
4665 // be on the board under the name, of another type: creating one beside it would fail
4666 // part way, or leave two fields of one name. Asked of the board's own field list, and
4667 // only when a field is missing.
4668 if plans
4669 .iter()
4670 .any(|plan| !before.fields.contains_key(&plan.field))
4671 {
4672 let board = self.board_fields().await?;
4673 for plan in plans
4674 .iter()
4675 .filter(|plan| !before.fields.contains_key(&plan.field))
4676 {
4677 if let Some(field) = Board::field(&board.fields, plan.field.name())? {
4678 return Err(SourceError::Refused {
4679 message: format!(
4680 "source {}'s board has a {} field that is not a single-select field \
4681 (it is a {}), so it cannot hold this source's options; next: rename \
4682 or remove that field, then run this again",
4683 self.name,
4684 plan.field.name(),
4685 optional_str(field, "__typename")?.unwrap_or("field of another type")
4686 ),
4687 });
4688 }
4689 }
4690 }
4691 let mut reports = Vec::new();
4692 for plan in &plans {
4693 let held = before.fields.get(&plan.field);
4694 let existing = held.map(|field| field.options.clone()).unwrap_or_default();
4695 let mut missing: Vec<String> = Vec::new();
4696 for wanted in &plan.wanted {
4697 let present = existing
4698 .iter()
4699 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4700 || missing
4701 .iter()
4702 .any(|named| named.eq_ignore_ascii_case(wanted));
4703 if !present {
4704 missing.push(wanted.clone());
4705 }
4706 }
4707 let kinds = match plan.field {
4708 BoardField::Status => self.statuses.missing_by_kind(&existing),
4709 BoardField::Priority => Vec::new(),
4710 };
4711 reports.push(FieldReport {
4712 field: plan.field,
4713 exists: held.is_some(),
4714 kinds,
4715 outcome: match (mode, held.is_some(), missing.is_empty()) {
4716 (SetupMode::Plan, _, _) => FieldOutcome::Planned,
4717 (SetupMode::Apply, true, true) => FieldOutcome::Unchanged,
4718 (SetupMode::Apply, true, false) => FieldOutcome::Applied,
4719 (SetupMode::Apply, false, _) => FieldOutcome::Created,
4720 },
4721 missing,
4722 existing,
4723 });
4724 }
4725 // A projected field is a text field, which the single-select snapshot cannot see, so
4726 // it is asked of the board's own field list — and only when one is configured.
4727 let mut metadata_fields = Vec::new();
4728 if !self.metadata_fields.is_empty() {
4729 let board = self.board_fields().await?;
4730 for projection in &self.metadata_fields {
4731 let conflict = match Board::field(&board.fields, &projection.field)? {
4732 None => None,
4733 Some(field) => {
4734 let typename = optional_str(field, "__typename")?;
4735 let data_type = optional_str(field, "dataType")?;
4736 if typename == Some("ProjectV2Field") && data_type == Some("TEXT") {
4737 None
4738 } else {
4739 let named = data_type
4740 .filter(|kind| *kind != "TEXT")
4741 .or(typename)
4742 .unwrap_or("field of another type");
4743 Some(NonTextFieldType::try_from(named.to_owned()).map_err(
4744 |message| SourceError::Malformed {
4745 message: format!(
4746 "GitHub answered the board's {} field with a type this \
4747 setup cannot read: {message}",
4748 &*projection.field
4749 ),
4750 },
4751 )?)
4752 }
4753 }
4754 };
4755 let exists =
4756 conflict.is_none() && Board::field(&board.fields, &projection.field)?.is_some();
4757 metadata_fields.push(MetadataFieldReport {
4758 field: projection.field.to_string(),
4759 key: projection.key.to_string(),
4760 path: projection.path.clone(),
4761 exists,
4762 outcome: match (mode, exists || conflict.is_some()) {
4763 (SetupMode::Plan, _) => FieldOutcome::Planned,
4764 (SetupMode::Apply, true) => FieldOutcome::Unchanged,
4765 (SetupMode::Apply, false) => FieldOutcome::Created,
4766 },
4767 conflict,
4768 });
4769 }
4770 }
4771 let report = FieldsReport {
4772 source: self.name.clone(),
4773 fields: reports,
4774 metadata_fields,
4775 };
4776 let writes: Vec<&FieldReport> = report
4777 .fields
4778 .iter()
4779 .filter(|field| !field.missing.is_empty() || !field.exists)
4780 .collect();
4781 let creates: Vec<&MetadataFieldReport> = report
4782 .metadata_fields
4783 .iter()
4784 .filter(|field| field.outcome == FieldOutcome::Created)
4785 .collect();
4786 if mode == SetupMode::Plan || (writes.is_empty() && creates.is_empty()) {
4787 return Ok(report);
4788 }
4789 let mut landed: Vec<&str> = Vec::new();
4790 for field in &writes {
4791 let added = field
4792 .missing
4793 .iter()
4794 .map(|name| json!({"name": name, "color": "GRAY", "description": ""}));
4795 let sent = match before.fields.get(&field.field) {
4796 Some(held) => {
4797 let mut options = held
4798 .options
4799 .iter()
4800 .map(|option| {
4801 json!({
4802 "id": option.id, "name": option.name, "color": option.color,
4803 "description": option.description,
4804 })
4805 })
4806 .collect::<Vec<_>>();
4807 options.extend(added);
4808 self.graphql(
4809 graphql::STATUS_OPTIONS_UPDATE,
4810 json!({"input": {
4811 "projectId": before.board_id, "fieldId": held.field_id,
4812 "singleSelectOptions": options,
4813 }}),
4814 )
4815 .await
4816 }
4817 None => {
4818 self.graphql(
4819 graphql::CREATE_FIELD,
4820 json!({"input": {
4821 "projectId": before.board_id, "dataType": "SINGLE_SELECT",
4822 "name": field.field.name(),
4823 "singleSelectOptions": added.collect::<Vec<_>>(),
4824 }}),
4825 )
4826 .await
4827 }
4828 };
4829 // A mutation that failed does not establish that GitHub left its field as it was,
4830 // so every failure from here on carries the recovery data a drift refusal does.
4831 match sent {
4832 Ok(_) => landed.push(field.field.name()),
4833 Err(error) => {
4834 let changed = if landed.is_empty() {
4835 String::new()
4836 } else {
4837 format!("changed the {} field and then ", landed.join(" and "))
4838 };
4839 return Err(SourceError::Refused {
4840 message: format!(
4841 "the guarded field setup {changed}failed on the {} field, which it may \
4842 have changed part way: {error}; the pre-write item assignments \
4843 are:\n{}",
4844 field.field.name(),
4845 recovery(&report, &before)?
4846 ),
4847 });
4848 }
4849 }
4850 }
4851 for field in &creates {
4852 let sent = self
4853 .graphql(
4854 graphql::CREATE_FIELD,
4855 json!({"input": {
4856 "projectId": before.board_id, "dataType": "TEXT", "name": field.field,
4857 }}),
4858 )
4859 .await;
4860 if let Err(error) = sent {
4861 let changed = if landed.is_empty() {
4862 String::new()
4863 } else {
4864 format!("changed the {} field and then ", landed.join(" and "))
4865 };
4866 return Err(SourceError::Refused {
4867 message: format!(
4868 "the guarded field setup {changed}failed creating the {} text field: \
4869 {error}; run it again to create what is still missing",
4870 field.field
4871 ),
4872 });
4873 }
4874 landed.push(&field.field);
4875 }
4876 // The board has been written, so a verification read that fails leaves it unverified
4877 // rather than unchanged, and says what to put back.
4878 let after = match self.board_snapshot(&owned).await {
4879 Ok(after) => after,
4880 Err(error) => {
4881 return Err(SourceError::Refused {
4882 message: format!(
4883 "the guarded field setup changed the {} field and then could not read the \
4884 board back to verify it: {error}; the pre-write item assignments are:\n{}",
4885 landed.join(" and "),
4886 recovery(&report, &before)?
4887 ),
4888 });
4889 }
4890 };
4891 let mut moved = Vec::new();
4892 for field in &report.fields {
4893 let name = field.field.name();
4894 let now = after
4895 .fields
4896 .get(&field.field)
4897 .map(|held| held.options.as_slice())
4898 .unwrap_or_default();
4899 if !field.existing.iter().all(|old| now.contains(old)) {
4900 moved.push(format!(
4901 "a pre-existing {name} option id, name, color or description"
4902 ));
4903 }
4904 if !field.missing.iter().all(|wanted| {
4905 now.iter()
4906 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4907 }) {
4908 moved.push(format!("an added {name} option"));
4909 }
4910 if after.assignments(field.field) != before.assignments(field.field) {
4911 moved.push(format!("an item's {name} value"));
4912 }
4913 }
4914 if !creates.is_empty() {
4915 // Read afresh: the view held for this command is the one from before the creates.
4916 *self.fields_cache()? = None;
4917 let board = match self.board_fields().await {
4918 Ok(board) => board,
4919 Err(error) => {
4920 return Err(SourceError::Refused {
4921 message: format!(
4922 "the guarded field setup changed the {} field and then could not \
4923 read the board back to verify it: {error}; run it again to verify \
4924 it, which creates nothing a second time",
4925 landed.join(" and ")
4926 ),
4927 });
4928 }
4929 };
4930 for field in &creates {
4931 let text = Board::field(&board.fields, &field.field)?.is_some_and(|held| {
4932 held.get("__typename").and_then(Value::as_str) == Some("ProjectV2Field")
4933 && held.get("dataType").and_then(Value::as_str) == Some("TEXT")
4934 });
4935 if !text {
4936 moved.push(format!("the created {} text field", field.field));
4937 }
4938 }
4939 }
4940 if !moved.is_empty() {
4941 return Err(SourceError::Refused {
4942 message: format!(
4943 "GitHub changed {} after the guarded field setup; the pre-write item \
4944 assignments are:\n{}",
4945 moved.join(", "),
4946 recovery(&report, &before)?
4947 ),
4948 });
4949 }
4950 Ok(report)
4951 }
4952
4953 /// Validate configuration and capture the named credential without exposing it.
4954 ///
4955 /// # Errors
4956 ///
4957 /// Returns [`SourceError::Config`] for a configuration this instance cannot use and
4958 /// [`SourceError::Auth`] when the named credential is missing or empty.
4959 pub fn new(
4960 name: &SourceName,
4961 config: GitHubProjectsConfig,
4962 secrets: &dyn SecretResolver,
4963 ) -> Result<Self, SourceError> {
4964 Self::recording_into(name, config, secrets, Arc::new(Accounting::new()))
4965 }
4966
4967 /// The same, recording every request it sends into an accounting the caller holds too.
4968 ///
4969 /// [`Self::new`] is this with an accounting of its own. A caller that is also making
4970 /// its own calls to GitHub — a lane verifying a schema, sweeping residue or cleaning
4971 /// up — passes the one it records those into, so the session total accounts for the
4972 /// whole session rather than for this source's share of it.
4973 ///
4974 /// # Errors
4975 ///
4976 /// Exactly [`Self::new`]'s: [`SourceError::Config`] for a configuration this instance
4977 /// cannot use and [`SourceError::Auth`] when the named credential is missing or empty.
4978 pub fn recording_into(
4979 name: &SourceName,
4980 config: GitHubProjectsConfig,
4981 secrets: &dyn SecretResolver,
4982 ledger: Arc<Accounting>,
4983 ) -> Result<Self, SourceError> {
4984 if !valid_github_owner(&config.owner) {
4985 return Err(SourceError::Config {
4986 message: "owner must be 1-39 ASCII letters, digits, or single hyphens, and cannot start or end with a hyphen".into(),
4987 });
4988 }
4989 if config.project_number == 0 || config.project_number > i32::MAX as u32 {
4990 return Err(SourceError::Config {
4991 message: format!("project_number must be between 1 and {}", i32::MAX),
4992 });
4993 }
4994 if !valid_environment_name(&config.token_env) {
4995 return Err(SourceError::Config {
4996 message: "token_env must be a valid environment-variable name".into(),
4997 });
4998 }
4999 let repository = config
5000 .repository
5001 .as_deref()
5002 .map(RepositoryTarget::parse)
5003 .transpose()?;
5004 let endpoint = Url::parse(&config.endpoint).map_err(|e| SourceError::Config {
5005 message: format!("endpoint is not a valid URL: {e}"),
5006 })?;
5007 if endpoint.scheme() != "https"
5008 && !(endpoint.scheme() == "http"
5009 && endpoint
5010 .host_str()
5011 .is_some_and(|h| h == "127.0.0.1" || h == "localhost" || h == "::1"))
5012 {
5013 return Err(SourceError::Config {
5014 message:
5015 "endpoint must use HTTPS (HTTP is accepted only for a loopback test server)"
5016 .into(),
5017 });
5018 }
5019 let token = secrets.get(&config.token_env).filter(|token| !token.expose_secret().trim().is_empty()).ok_or_else(|| SourceError::Auth {
5020 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),
5021 })?;
5022 Ok(Self {
5023 name: name.clone(),
5024 owner: config.owner,
5025 project_number: config.project_number,
5026 repository,
5027 asset_client: std::sync::OnceLock::new(),
5028 endpoint,
5029 token,
5030 credential_name: config.token_env,
5031 statuses: BoardStatuses::resolve(&config.status_mapping, name)?,
5032 priorities: config
5033 .priority_mapping
5034 .map(|mapping| PriorityMapping::resolve(mapping, name))
5035 .transpose()?,
5036 metadata_fields: MetadataField::resolve(config.metadata_fields, name)?,
5037 client: std::sync::OnceLock::new(),
5038 created: Mutex::new(Vec::new()),
5039 updated: Mutex::new(Vec::new()),
5040 commented: Mutex::new(Vec::new()),
5041 pacing: Pacing::resolve(config.pacing, name)?,
5042 last_mutation: Mutex::new(None),
5043 windows: Mutex::new(pacing::Windows::default()),
5044 task_children: Mutex::new(BTreeMap::new()),
5045 written: Mutex::new(BTreeMap::new()),
5046 deferred: Mutex::new(None),
5047 held_updates: Mutex::new(Vec::new()),
5048 clock: system_clock(),
5049 numeric_repositories: tokio::sync::Mutex::new(BTreeMap::new()),
5050 board_cache: Mutex::new(None),
5051 search_cache: Mutex::new(None),
5052 narrowed_cache: Mutex::new(BTreeMap::new()),
5053 resolved_cache: Mutex::new(BTreeMap::new()),
5054 read_ahead: Mutex::new(BTreeMap::new()),
5055 pending_origins: Mutex::new(Vec::new()),
5056 member_classes: Mutex::new(BTreeMap::new()),
5057 carried: Mutex::new(BTreeMap::new()),
5058 held_cache: Mutex::new(BTreeMap::new()),
5059 children_cache: Mutex::new(BTreeMap::new()),
5060 search_next: Mutex::new(BTreeMap::new()),
5061 fields_cache: Mutex::new(None),
5062 repository_cache: Mutex::new(BTreeMap::new()),
5063 ledger,
5064 expected: expected::Held::default(),
5065 })
5066 }
5067
5068 /// The client every API request goes through, built on the first one.
5069 ///
5070 /// Not with the source: building a client loads the system's certificate store, which
5071 /// costs tens of milliseconds, and a command that configures this board but never sends to
5072 /// it — a read of another source — should not pay that in its process.
5073 fn client(&self) -> Result<&Client, SourceError> {
5074 built(&self.client, || {
5075 Client::builder()
5076 .user_agent("onetaskgraph")
5077 .build()
5078 .map_err(|e| SourceError::Config {
5079 message: format!("cannot build HTTP client: {e}"),
5080 })
5081 })
5082 }
5083
5084 /// The client attachment requests go through, with its own redirect policy, built on the
5085 /// first one for the reason [`Self::client`] gives.
5086 fn asset_client(&self) -> Result<&Client, SourceError> {
5087 built(&self.asset_client, || assets::client(&self.endpoint))
5088 }
5089
5090 /// A snapshot of every request this source has sent, and what each cost.
5091 ///
5092 /// A value to hold and compare rather than a borrow of the accounting itself, so two
5093 /// of them can sit side by side. When this source was built with
5094 /// [`Self::recording_into`] the snapshot is the whole shared session, which is the
5095 /// point of building it that way.
5096 #[must_use]
5097 pub fn accounting(&self) -> accounting::Session {
5098 self.ledger.snapshot()
5099 }
5100
5101 /// Send one GraphQL document, pacing this source's own mutations and waiting out a
5102 /// rate limit rather than handing it straight back as an error.
5103 ///
5104 /// Retrying is safe for every document here, including the mutations, and the reason
5105 /// is that only a *refusal* is retried: [`Limiter::classify`] rules on a response
5106 /// GitHub sent, and a request GitHub refused for a rate limit did not run, so nothing
5107 /// this replays has already taken effect. An outcome this source cannot know — the
5108 /// send failed, or the body could not be read, so the mutation may well have landed —
5109 /// is [`Attempt::Failed`] in [`send_once`] and leaves this loop without a second
5110 /// attempt. A duplicate write would come from replaying one of those, and none is
5111 /// replayed.
5112 async fn graphql(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
5113 // Updates held back go out before any other request, so whatever it reads or writes
5114 // sees the board as though each had been sent when it was made.
5115 Box::pin(self.send_held_updates()).await?;
5116 // Origins held back for a read ahead that never came are sent before anything else.
5117 if query != graphql::ORIGIN_LOOKUPS && query != graphql::ORIGIN_LOOKUPS_WITH_MEMBERS {
5118 Box::pin(self.send_pending_origins(None)).await?;
5119 }
5120 if !self.expected.journaling() {
5121 return self.graphql_sent(query, variables).await;
5122 }
5123 // While a write the store's cache expects runs, its mutations select the whole issue
5124 // they leave behind, so the response vouches for what the cache keeps — and every
5125 // request is recorded, so the write's parts are reported from their own responses.
5126 let (query, variables) = match graphql::whole_twin(query) {
5127 Some(twin) => (twin, expected::whole_variables(variables)),
5128 None => (query, variables),
5129 };
5130 let sent = self.graphql_sent(query, variables.clone()).await;
5131 self.journal(query, &variables, &sent).await;
5132 sent
5133 }
5134
5135 /// One document sent, retried while GitHub asks for a wait, exactly as asked.
5136 async fn graphql_sent(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
5137 if is_mutation(query)
5138 && ![
5139 graphql::ADD_COMMENT,
5140 graphql::UPDATE_COMMENT,
5141 graphql::DELETE_COMMENT,
5142 graphql::ADD_COMMENT_WHOLE,
5143 graphql::UPDATE_COMMENT_WHOLE,
5144 ]
5145 .contains(&query)
5146 {
5147 let mut cache = self.resolved_cache()?;
5148 let mut ahead = self.read_ahead()?;
5149 let mut carried = self.carried()?;
5150 for argument in ["input", "second", "third", "clear"] {
5151 if let Some(input) = variables.get(argument) {
5152 let names = |id: &NativeId, item_id: Option<&str>| {
5153 ["id", "issueId", "subjectId", "itemId"].iter().any(|key| {
5154 input
5155 .get(key)
5156 .and_then(Value::as_str)
5157 .is_some_and(|value| value == id.0 || Some(value) == item_id)
5158 })
5159 };
5160 cache.retain(|id, item| !names(id, Some(&item.item_id)));
5161 ahead.retain(|id, item| {
5162 !names(id, item.as_ref().map(|item| item.item_id.as_str()))
5163 });
5164 carried.retain(|id, item| !names(id, Some(&item.item_id)));
5165 }
5166 }
5167 }
5168 let doing = operation_description(query);
5169 let mutation = is_mutation(query);
5170 let charge = pacing::Charge::graphql(query, &variables);
5171 let mut waited = Duration::ZERO;
5172 let mut waits = 0_u32;
5173 let mut backoff = self.pacing.retry_backoff;
5174 loop {
5175 let ticket = self.pace(charge, mutation).await;
5176 let attempt = self.send_once(query, &variables, waits + 1).await;
5177 self.paced(ticket, mutation);
5178 let limited = match attempt {
5179 Ok(data) => return Ok(data),
5180 Err(Attempt::Failed(error)) => return Err(error),
5181 Err(Attempt::Limited(limited)) => limited,
5182 };
5183 let (wait, cause) = limited.wait(backoff);
5184 let remaining = self.pacing.retry_budget.saturating_sub(waited);
5185 // A wait of nothing spends none of the budget, so it is exhaustion rather
5186 // than a retry. `Pacing::resolve` rules out every way of configuring one
5187 // except a budget of zero, where reporting the first refusal is the ask.
5188 if wait.is_zero() || wait > remaining {
5189 return Err(limited.exhausted(
5190 doing,
5191 waits,
5192 waited,
5193 wait,
5194 self.pacing.retry_budget,
5195 ));
5196 }
5197 self.clock.sleep_because(wait, cause).await;
5198 waited += wait;
5199 waits += 1;
5200 backoff = backoff.saturating_mul(2);
5201 }
5202 }
5203
5204 /// Hold one request charging `charge` until every secondary window it charges has room for
5205 /// the whole of it — and, for a mutation of an instance configured with
5206 /// `pacing.min_mutation_interval_ms`, until that interval has passed since the last one
5207 /// finished — then count it, answering the ticket [`Self::paced`] finishes it with.
5208 ///
5209 /// The moment is decided and counted under the lock and waited out outside it, so two
5210 /// callers take two places rather than the same one, and no lock is held across an await.
5211 /// A request with room goes out at once: nothing here waits that a window does not ask for.
5212 async fn pace(&self, charge: pacing::Charge, mutation: bool) -> pacing::Ticket {
5213 let (wait, cause, ticket) = {
5214 // A poisoned lock here costs pacing, not correctness, and refusing the write over
5215 // it would turn an earlier failure into a second one for no gain.
5216 let mut windows = self
5217 .windows
5218 .lock()
5219 .unwrap_or_else(std::sync::PoisonError::into_inner);
5220 let now = self.clock.now();
5221 let (mut at, mut cause) = if self.pacing.windows {
5222 windows.room(now, charge)
5223 } else {
5224 (now, None)
5225 };
5226 if mutation && !self.pacing.min_mutation_interval.is_zero() {
5227 let mut last = self
5228 .last_mutation
5229 .lock()
5230 .unwrap_or_else(std::sync::PoisonError::into_inner);
5231 // `checked_add` rather than `+`: adding durations can panic on overflow, and
5232 // pacing is not worth a panic even at a bound `MAX_PACING_MS` already rules out.
5233 if let Some(earliest) = last
5234 .and_then(|previous| previous.checked_add(self.pacing.min_mutation_interval))
5235 && earliest > at
5236 {
5237 at = earliest;
5238 cause = Some(pacing::cause::MIN_INTERVAL);
5239 }
5240 *last = Some(at);
5241 }
5242 let ticket = windows.charge(at, charge);
5243 (at.saturating_sub(now), cause, ticket)
5244 };
5245 if let (false, Some(cause)) = (wait.is_zero(), cause) {
5246 self.clock.sleep_because(wait, cause).await;
5247 }
5248 ticket
5249 }
5250
5251 /// Record that the request `ticket` was charged for has finished, so each window — and
5252 /// the configured spacing — counts it from here rather than from the moment it left.
5253 ///
5254 /// This source can only choose when a request *departs*; GitHub's limiter counts when it
5255 /// *arrives*, and the two differ by whatever the request spent in transit. Counting from
5256 /// departure would hand the limiter a window that looked roomier than it is, so a source
5257 /// pacing exactly at a limit could still be seen arriving over it — which is how a copy
5258 /// paced well inside a board's threshold was once refused by it on a slower machine while
5259 /// passing on a quick one. The previous request had arrived before its response came back,
5260 /// so counting from here is never earlier than the limiter does, whatever transit costs
5261 /// and on whatever platform: the safe side of a limit that punishes being wrong by refusing
5262 /// reads for the next fifty minutes.
5263 ///
5264 /// A failed attempt is finished too: a request refused by the limiter still arrived, and
5265 /// one that never left costs only room nobody needed.
5266 fn paced(&self, ticket: pacing::Ticket, mutation: bool) {
5267 let now = self.clock.now();
5268 self.windows
5269 .lock()
5270 .unwrap_or_else(std::sync::PoisonError::into_inner)
5271 .finish(ticket, now);
5272 if mutation && !self.pacing.min_mutation_interval.is_zero() {
5273 let mut last = self
5274 .last_mutation
5275 .lock()
5276 .unwrap_or_else(std::sync::PoisonError::into_inner);
5277 // `max` rather than an assignment: a concurrent caller may already have reserved a
5278 // slot further out, and completing this request must never pull that slot back in.
5279 *last = Some(last.map_or(now, |reserved| reserved.max(now)));
5280 }
5281 }
5282
5283 /// One HTTP attempt, classified into an answer, a rate limit to wait out, or a
5284 /// failure that waiting cannot help — and recorded, whichever of the three it was.
5285 ///
5286 /// This is the one place a request leaves this crate, which is why the accounting is
5287 /// here rather than at each of the callers: a read path added later is counted without
5288 /// anybody remembering to count it, and
5289 /// `the_session_report_counts_every_request_the_board_served_and_what_each_cost` fails
5290 /// when one is not.
5291 async fn send_once(
5292 &self,
5293 query: &str,
5294 variables: &Value,
5295 number: u32,
5296 ) -> Result<Value, Attempt> {
5297 let executed = self.ledger.priced_document(query);
5298 let Attempted {
5299 result,
5300 limits,
5301 reported_cost,
5302 response_outcome,
5303 } = self.attempt(executed.as_ref(), variables).await;
5304 // No `otherwise` name: every document this source sends is one of its own, and the
5305 // inventory gate on `graphql::DOCUMENTS` is what keeps that true.
5306 let sending = accounting::Request::graphql(query, variables, None, reported_cost);
5307 let outcome = match &result {
5308 Ok(_) => accounting::Outcome::Answered,
5309 Err(Attempt::Limited(_)) => accounting::Outcome::RateLimited,
5310 Err(Attempt::Failed(_)) => accounting::Outcome::Refused,
5311 };
5312 self.ledger
5313 .record(sending.finished(outcome, limits).with_execution(
5314 Some(executed.into_owned()),
5315 response_outcome,
5316 number,
5317 ));
5318 result
5319 }
5320
5321 /// The attempt itself, with what its response said about the rate limit alongside.
5322 ///
5323 /// The two are returned together rather than recorded here because every one of the
5324 /// early exits below is a different outcome, and a record written at each of them is a
5325 /// record one of them can be added without.
5326 async fn attempt(&self, query: &str, variables: &Value) -> Attempted {
5327 let mut limits = accounting::RateLimit::default();
5328 let mut reported_cost = None;
5329 let mut response_outcome = accounting::ResponseOutcome::TransportError;
5330 let result = self
5331 .attempted(
5332 query,
5333 variables,
5334 &mut limits,
5335 &mut reported_cost,
5336 &mut response_outcome,
5337 )
5338 .await;
5339 Attempted {
5340 result,
5341 limits,
5342 reported_cost,
5343 response_outcome,
5344 }
5345 }
5346
5347 /// One HTTP attempt, filling in what its response said about the rate limit as it goes.
5348 async fn attempted(
5349 &self,
5350 query: &str,
5351 variables: &Value,
5352 limits: &mut accounting::RateLimit,
5353 reported_cost: &mut Option<u64>,
5354 response_outcome: &mut accounting::ResponseOutcome,
5355 ) -> Result<Value, Attempt> {
5356 let response = self
5357 .client()
5358 .map_err(Attempt::Failed)?
5359 .post(self.endpoint.clone())
5360 .bearer_auth(self.token.expose_secret())
5361 .json(&json!({"query": query, "variables": variables}))
5362 .send()
5363 .await
5364 .map_err(|e| {
5365 Attempt::Failed(SourceError::Unavailable {
5366 message: format!("GitHub GraphQL request failed: {}", transport_error(&e)),
5367 })
5368 })?;
5369 let status = response.status();
5370 if !status.is_success() {
5371 *response_outcome = accounting::ResponseOutcome::HttpError;
5372 }
5373 let header = |name: &str| whole_seconds(response.headers().get(name));
5374 let served = response
5375 .headers()
5376 .get(reqwest::header::DATE)
5377 .and_then(|value| value.to_str().ok())
5378 .and_then(|value| DateTime::parse_from_rfc2822(value).ok())
5379 .map(|at| at.with_timezone(&Utc));
5380 *limits = accounting::RateLimit::read(|name| {
5381 response
5382 .headers()
5383 .get(name)
5384 .and_then(|value| value.to_str().ok())
5385 .map(str::to_owned)
5386 });
5387 // Exactly `0` is exhaustion and everything else — a count, an empty value, bytes
5388 // that are not text at all — is "not known to be exhausted". This never makes a
5389 // response a refusal on its own: it says which limiter a refusal is attributed to
5390 // and where its hint comes from, so a value this cannot read costs a hint rather
5391 // than an answer.
5392 let exhausted = response
5393 .headers()
5394 .get("x-ratelimit-remaining")
5395 .and_then(|value| value.to_str().ok())
5396 == Some("0");
5397 // `retry-after` is what GitHub asks for when it asks; when it does not and the
5398 // primary budget is spent, `x-ratelimit-reset` says when that budget comes back,
5399 // which is the same question answered as an absolute time. Nothing else here is a
5400 // hint, and a schedule is what answers a refusal that carries none.
5401 let retry_after = header("retry-after");
5402 let hinted_by = if retry_after.is_some() || !exhausted {
5403 Hint::RetryAfter
5404 } else {
5405 Hint::PrimaryReset
5406 };
5407 let hint = retry_after.or_else(|| {
5408 exhausted
5409 .then(|| header("x-ratelimit-reset"))
5410 .flatten()
5411 .map(|reset| reset.saturating_sub(Utc::now().timestamp().max(0).unsigned_abs()))
5412 });
5413 // Read before it is parsed, because the evidence which tells a secondary rate
5414 // limit from a rejected credential is in the body of a response whose status says
5415 // only "forbidden" — and a non-success response was never parsed at all.
5416 let body = response.text().await.map_err(|e| {
5417 Attempt::Failed(SourceError::Unavailable {
5418 message: format!(
5419 "GitHub GraphQL response could not be read: {}",
5420 transport_error(&e)
5421 ),
5422 })
5423 })?;
5424 *reported_cost = serde_json::from_str::<Value>(&body)
5425 .ok()
5426 .as_ref()
5427 .and_then(|body| body.pointer("/data/rateLimit/cost"))
5428 .and_then(Value::as_u64);
5429 let parsed = serde_json::from_str::<Value>(&body).ok();
5430 *response_outcome = accounting::ResponseOutcome::of_graphql_reply(status, parsed.as_ref());
5431 if let Some(limiter) = Limiter::classify(status, exhausted, &body) {
5432 return Err(Attempt::Limited(Limited {
5433 limiter,
5434 hint,
5435 hinted_by,
5436 }));
5437 }
5438 if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {
5439 return Err(Attempt::Failed(SourceError::Auth {
5440 message: format!(
5441 "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"
5442 ),
5443 }));
5444 }
5445 if !status.is_success() {
5446 return Err(Attempt::Failed(SourceError::Unavailable {
5447 message: format!("GitHub GraphQL returned HTTP {status}"),
5448 }));
5449 }
5450 let answered = if query == graphql::ISSUES_BY_REFERENCE {
5451 self.answer_unresolved(&body)
5452 } else if query == graphql::CREATE_ISSUES {
5453 answer_with_errors(&body).map_or_else(|| self.answer(&body), Ok)
5454 } else {
5455 self.answer(&body)
5456 };
5457 let mut data = answered.map_err(Attempt::Failed)?;
5458 if let Some(served) = served {
5459 stamp_served(&mut data, &served.to_rfc3339());
5460 }
5461 Ok(data)
5462 }
5463
5464 /// What one successful HTTP response says, once its GraphQL errors are read.
5465 /// [`Self::answer`], for a read of issues by reference: a reference GitHub cannot resolve
5466 /// for this credential — a repository it cannot see, an issue number it does not hold —
5467 /// is answered `null` beside a `NOT_FOUND` error naming it, and that is an answer about
5468 /// that reference rather than a failure of the others. Any other error refuses the read,
5469 /// as there.
5470 fn answer_unresolved(&self, body: &str) -> Result<Value, SourceError> {
5471 let parsed: Option<Value> = serde_json::from_str(body).ok();
5472 // Each error is a `NOT_FOUND` whose path names one slot of the batch, `r<n>` or its
5473 // `issue`, and the answer holds `null` at exactly that path: anything else — another
5474 // kind of error, one naming no slot, or one beside a value it says is not there — is a
5475 // refusal of the whole read.
5476 let unresolved = |data: &Value, error: &Value| {
5477 error.get("type").and_then(Value::as_str) == Some("NOT_FOUND")
5478 && error
5479 .get("path")
5480 .and_then(Value::as_array)
5481 .is_some_and(|path| {
5482 let alias = path.first().and_then(Value::as_str).and_then(|alias| {
5483 alias
5484 .strip_prefix('r')
5485 .and_then(|slot| slot.parse::<usize>().ok())
5486 .filter(|slot| *slot < REFERENCE_BATCH)
5487 .map(|_| alias)
5488 });
5489 match (alias, path.get(1).and_then(Value::as_str), path.len()) {
5490 (Some(alias), None, 1) => data.get(alias).is_some_and(Value::is_null),
5491 (Some(alias), Some("issue"), 2) => data
5492 .get(alias)
5493 .and_then(|repository| repository.get("issue"))
5494 .is_some_and(Value::is_null),
5495 _ => false,
5496 }
5497 })
5498 };
5499 let unresolved_only = parsed.as_ref().is_some_and(|parsed| {
5500 parsed.get("data").is_some_and(|data| {
5501 data.is_object()
5502 && parsed
5503 .get("errors")
5504 .and_then(Value::as_array)
5505 .is_some_and(|errors| errors.iter().all(|error| unresolved(data, error)))
5506 })
5507 });
5508 match parsed {
5509 Some(parsed) if unresolved_only => Ok(parsed["data"].clone()),
5510 _ => self.answer(body),
5511 }
5512 }
5513
5514 fn answer(&self, body: &str) -> Result<Value, SourceError> {
5515 let body: Value = serde_json::from_str(body).map_err(|e| SourceError::Malformed {
5516 message: format!("GitHub returned invalid JSON: {e}"),
5517 })?;
5518 let errors = body
5519 .get("errors")
5520 .map(|value| {
5521 value.as_array().ok_or_else(|| SourceError::Malformed {
5522 message: "GitHub response errors is not an array".into(),
5523 })
5524 })
5525 .transpose()?;
5526 if let Some(errors) = errors.filter(|errors| !errors.is_empty()) {
5527 let messages = errors
5528 .iter()
5529 .filter_map(|e| e.get("message").and_then(Value::as_str))
5530 .collect::<Vec<_>>()
5531 .join("; ");
5532 let message = if messages.is_empty() {
5533 "GitHub returned GraphQL errors".into()
5534 } else {
5535 messages
5536 };
5537 let normalized = message.to_ascii_lowercase();
5538 if normalized.contains("resource not accessible") || normalized.contains("scope") {
5539 return Err(SourceError::Auth {
5540 message: format!(
5541 "{message}; grant {} Projects and Issues read/write plus Pull requests read-only access for every repository represented on the board",
5542 self.credential_name
5543 ),
5544 });
5545 }
5546 return Err(SourceError::Refused { message });
5547 }
5548 body.get("data")
5549 .filter(|data| data.is_object())
5550 .cloned()
5551 .ok_or_else(|| SourceError::Malformed {
5552 message: "GitHub response has no data object".into(),
5553 })
5554 }
5555
5556 // llmlint: ignore[boundary_inputs_validated] GitHub caps nested connections at 100 and
5557 // GraphQL cannot independently page them inside the outer item page. This source page is
5558 // deliberately bounded at that published maximum; the live drift journey exercises it.
5559 async fn board_page(
5560 &self,
5561 items_after: Option<&str>,
5562 items_first: u32,
5563 ) -> Result<Value, SourceError> {
5564 let data = self
5565 .graphql(
5566 graphql::BOARD,
5567 json!({"owner":self.owner,"number":self.project_number,
5568 "first":items_first.min(MAX_PAGE_SIZE),"after":items_after,
5569 "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
5570 )
5571 .await?;
5572 data.pointer("/owner/projectV2")
5573 .filter(|v| !v.is_null())
5574 .cloned()
5575 .ok_or_else(|| SourceError::Refused {
5576 message: format!(
5577 "GitHub project {}/{} was not found or is not visible to the token",
5578 self.owner, self.project_number
5579 ),
5580 })
5581 }
5582
5583 /// The search that finds the issues of this board, narrowed by `also` when it is
5584 /// given.
5585 ///
5586 /// `project:owner/number` is what scopes a search to one board, and `is:issue` is what
5587 /// keeps pull requests out of it: GitHub's `ISSUE` search type covers both, and a pull
5588 /// request is somebody's change rather than a unit of plan. `-has:parent` is *not*
5589 /// here on purpose — GitHub accepts it and silently ignores it, so a project is told
5590 /// from a task by the `parent` field each issue carries rather than by the search.
5591 fn board_search(&self, also: Option<&str>) -> String {
5592 let scope = format!("project:{}/{} is:issue", self.owner, self.project_number);
5593 match also {
5594 Some(also) => format!("{scope} {also}"),
5595 None => scope,
5596 }
5597 }
5598
5599 /// One issue this source reached directly, as the board item a read of the board would
5600 /// have produced — or `None` when this board does not hold it.
5601 ///
5602 /// The board half of an issue rides along on `Issue.projectItems`, so the value handed
5603 /// to [`Self::resolve`] is the very shape a `ProjectV2.items` read gives it: the board
5604 /// item's own id, that item's field values, and the issue as its content. One resolver
5605 /// for both routes is what makes an issue read through a search, through its own node
5606 /// id, or through its project's sub-issues report the same title, the same status, the
5607 /// same labels and the same qualified id.
5608 ///
5609 /// An issue with no entry for *this* board is not this source's to report, which is
5610 /// what keeps an id naming some other repository's issue from being answered as an item
5611 /// of this board. That answer is given about an **exhausted** connection and never
5612 /// about an unread page: the entry is looked for on the page in hand, and only if that
5613 /// page reports more of the connection, in [`Self::board_membership`]'s walk of the
5614 /// rest of it.
5615 async fn resolve_issue(&self, issue: &Value) -> Result<Option<Resolved>, SourceError> {
5616 self.resolve_issue_as(issue, true).await
5617 }
5618
5619 /// [`Self::resolve_issue`], remembering what it resolves for this command's later reads
5620 /// and writes only when `remember` says to.
5621 async fn resolve_issue_as(
5622 &self,
5623 issue: &Value,
5624 remember: bool,
5625 ) -> Result<Option<Resolved>, SourceError> {
5626 if optional_str(issue, "__typename")? != Some("Issue") {
5627 return Ok(None);
5628 }
5629 let memberships = issue
5630 .get("projectItems")
5631 .ok_or_else(|| SourceError::Malformed {
5632 message: "GitHub issue is missing projectItems".into(),
5633 })?;
5634 let nodes = memberships
5635 .get("nodes")
5636 .and_then(Value::as_array)
5637 .ok_or_else(|| SourceError::Malformed {
5638 message: "GitHub issue projectItems.nodes is not an array".into(),
5639 })?;
5640 let held = match self.board_entry(nodes) {
5641 Some(held) => held.clone(),
5642 None => {
5643 let info = memberships
5644 .get("pageInfo")
5645 .ok_or_else(|| SourceError::Malformed {
5646 message: "GitHub issue projectItems has no pageInfo".into(),
5647 })?;
5648 // The page held no entry for this board. Whether that means the issue is
5649 // not on it is a question about the rest of the connection, and only a
5650 // connection with no rest answers it here.
5651 if !required_bool(info, "hasNextPage")? {
5652 return Ok(None);
5653 }
5654 let cursor = required_str(info, "endCursor")?;
5655 validate_cursor_progress(None, cursor)?;
5656 let issue_id = required_str(issue, "id")?;
5657 match self.board_membership(issue_id, cursor).await? {
5658 Some(held) => held,
5659 None => return Ok(None),
5660 }
5661 }
5662 };
5663 // The membership's own `updatedAt` rides along: it is the board half of the item's change
5664 // token, which a field change moves where the issue's own time does not.
5665 let item = json!({
5666 "id": required_str(&held, "id")?,
5667 "updatedAt": held.get("updatedAt"),
5668 "project": held.get("project"),
5669 "fieldValues": held.get("fieldValues"),
5670 "content": issue,
5671 });
5672 if remember {
5673 self.resolve(&item)
5674 } else {
5675 self.resolve_unremembered(&item)
5676 }
5677 }
5678
5679 /// This board's own entry among one page of an issue's `Issue.projectItems`.
5680 ///
5681 /// One spelling of *which membership is this board's*, so the page a read carries and
5682 /// the pages [`Self::board_membership`] walks are searched by the same rule.
5683 fn board_entry<'a>(&self, nodes: &'a [Value]) -> Option<&'a Value> {
5684 nodes.iter().find(|node| {
5685 node.pointer("/project/number").and_then(Value::as_u64)
5686 == Some(u64::from(self.project_number))
5687 })
5688 }
5689
5690 /// The rest of one issue's board memberships, from `after`, for this board's entry.
5691 ///
5692 /// The recovery read: a page of memberships that holds no entry for this board says
5693 /// nothing about the memberships past it, so the connection is walked to exhaustion
5694 /// before an issue is reported as one this board does not hold. `Ok(None)` is that
5695 /// positive answer — the whole connection was read and no entry named this board —
5696 /// rather than a failure, and the walk is held to
5697 /// [`validate_cursor_progress`] like every other page walk here, so a source answering
5698 /// with a cursor that does not advance is refused instead of spun on.
5699 async fn board_membership(
5700 &self,
5701 issue: &str,
5702 after: &str,
5703 ) -> Result<Option<Value>, SourceError> {
5704 let mut after = after.to_owned();
5705 loop {
5706 let data = self
5707 .graphql(
5708 graphql::ISSUE_BOARD_ITEMS,
5709 json!({"id":issue,"first":MAX_PAGE_SIZE,"after":after,
5710 "nestedFirst":NESTED_PAGE_SIZE}),
5711 )
5712 .await?;
5713 let Some(connection) = data
5714 .pointer("/node/projectItems")
5715 .filter(|value| !value.is_null())
5716 else {
5717 // The id resolved to nothing, or to something with no memberships to walk —
5718 // which is the same answer as a connection holding no entry for this board.
5719 return Ok(None);
5720 };
5721 let nodes = connection
5722 .get("nodes")
5723 .and_then(Value::as_array)
5724 .ok_or_else(|| SourceError::Malformed {
5725 message: "GitHub issue projectItems.nodes is not an array".into(),
5726 })?;
5727 if let Some(held) = self.board_entry(nodes) {
5728 return Ok(Some(held.clone()));
5729 }
5730 let info = connection
5731 .get("pageInfo")
5732 .ok_or_else(|| SourceError::Malformed {
5733 message: "GitHub issue projectItems has no pageInfo".into(),
5734 })?;
5735 let next = required_bool(info, "hasNextPage")?
5736 .then(|| required_str(info, "endCursor"))
5737 .transpose()?;
5738 match next {
5739 Some(next) => {
5740 validate_cursor_progress(Some(&after), next)?;
5741 after = next.to_owned();
5742 }
5743 None => return Ok(None),
5744 }
5745 }
5746 }
5747
5748 /// One page of a board-scoped issue search, and where the next page resumes.
5749 async fn search_page(
5750 &self,
5751 search: &str,
5752 first: u32,
5753 after: Option<&str>,
5754 ) -> Result<(Vec<Resolved>, Option<String>), SourceError> {
5755 let data = self
5756 .graphql(
5757 graphql::SEARCH_ISSUES,
5758 json!({"search":search,"type":"ISSUE","first":first.min(MAX_PAGE_SIZE),
5759 "after":after,"nestedFirst":NESTED_PAGE_SIZE,
5760 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
5761 )
5762 .await?;
5763 let connection = data.get("search").ok_or_else(|| SourceError::Malformed {
5764 message: "GitHub search response has no search connection".into(),
5765 })?;
5766 let mut found = Vec::new();
5767 for node in connection
5768 .get("nodes")
5769 .and_then(Value::as_array)
5770 .ok_or_else(|| SourceError::Malformed {
5771 message: "GitHub search nodes is not an array".into(),
5772 })?
5773 {
5774 if let Some(resolved) = self.resolve_issue(node).await? {
5775 found.push(resolved);
5776 }
5777 }
5778 let info = connection
5779 .get("pageInfo")
5780 .ok_or_else(|| SourceError::Malformed {
5781 message: "GitHub search connection has no pageInfo".into(),
5782 })?;
5783 let next = required_bool(info, "hasNextPage")?
5784 .then(|| required_str(info, "endCursor"))
5785 .transpose()?
5786 .map(str::to_owned);
5787 if let Some(next) = &next {
5788 validate_cursor_progress(after, next)?;
5789 }
5790 Ok((found, next))
5791 }
5792
5793 /// Every issue this board holds, completed with what this run wrote.
5794 ///
5795 /// The completion is not an optimisation and it is not a cache: GitHub's issue search
5796 /// is an index and is eventually consistent, so an issue this run created seconds ago
5797 /// can be absent from it, and a project listed straight after being written would
5798 /// otherwise be missing from its own board. What is added back is only what this
5799 /// process itself wrote, out of [`Self::created`], which lives and dies with the
5800 /// process.
5801 async fn board_issues(&self) -> Result<Vec<Resolved>, SourceError> {
5802 let found = self.searched_issues().await?;
5803 self.completed_with_written(found, |_| true)
5804 }
5805
5806 /// Every issue this board's own search reports, walked to exhaustion, read once per
5807 /// source.
5808 ///
5809 /// The uncompleted half of [`Self::board_issues`], separated because [`Self::board`]
5810 /// needs it too and the two would otherwise walk the same search twice in one command.
5811 /// See [`Self::search_cache`] for why holding it is the same bargain holding the board
5812 /// is.
5813 async fn searched_issues(&self) -> Result<Vec<Resolved>, SourceError> {
5814 let cached = self.search_cache()?.clone();
5815 if let Some(held) = cached {
5816 return Ok(held);
5817 }
5818 let mut after: Option<String> = None;
5819 let mut found = Vec::new();
5820 let search = self.board_search(None);
5821 loop {
5822 let (page, next) = self
5823 .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
5824 .await?;
5825 found.extend(page);
5826 match next {
5827 Some(next) => after = Some(next),
5828 None => break,
5829 }
5830 }
5831 *self.search_cache()? = Some(found.clone());
5832 Ok(found)
5833 }
5834
5835 /// Every issue this board's own search reports with `also` beside its scope, walked to
5836 /// exhaustion — held for no later read, which may not be narrowed alike.
5837 async fn searched_narrowly(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
5838 let mut after: Option<String> = None;
5839 let mut found = Vec::new();
5840 let search = self.board_search(Some(also));
5841 loop {
5842 let (page, next) = self
5843 .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
5844 .await?;
5845 found.extend(page);
5846 match next {
5847 Some(next) => after = Some(next),
5848 None => return Ok(found),
5849 }
5850 }
5851 }
5852
5853 /// This process's own view of the board's issues, or the refusal a poisoned lock is.
5854 fn search_cache(
5855 &self,
5856 ) -> Result<std::sync::MutexGuard<'_, Option<Vec<Resolved>>>, SourceError> {
5857 self.search_cache
5858 .lock()
5859 .map_err(|_| SourceError::Unavailable {
5860 message: "this source's view of the board's issues was left inconsistent by an \
5861 earlier failure; next: run the command again"
5862 .into(),
5863 })
5864 }
5865
5866 /// `found`, with everything this run wrote that `keep` accepts and the read did not
5867 /// report.
5868 ///
5869 /// See [`Self::created`] and [`Self::board_issues`] for why a read has to be completed
5870 /// at all: the search index is behind, and a node read of an item filed moments ago can
5871 /// be too.
5872 fn completed_with_written(
5873 &self,
5874 mut found: Vec<Resolved>,
5875 keep: impl Fn(&Resolved) -> bool,
5876 ) -> Result<Vec<Resolved>, SourceError> {
5877 for own in self.created()?.iter().filter(|own| keep(own)) {
5878 if !found.iter().any(|item| item.id == own.id) {
5879 found.push(own.clone());
5880 }
5881 }
5882 Ok(found)
5883 }
5884
5885 /// What resolving one node id reached.
5886 ///
5887 /// Three answers rather than an `Option`, because a board *draft* is none of the other
5888 /// two: it is not an issue, so the issue fragment reads nothing of it, and a read of one
5889 /// is completed by a read of the draft itself rather than reported as nothing.
5890 async fn reach(&self, id: &NativeId) -> Result<Reached, SourceError> {
5891 let asked = self
5892 .graphql(
5893 graphql::ISSUE,
5894 json!({"id":id.0,"first":MAX_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
5895 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
5896 )
5897 .await;
5898 let data = match asked {
5899 Ok(data) => data,
5900 // A string that is not a node id at all is not a failure to report: it is an id
5901 // this board does not hold, which is what every read of one already answers.
5902 Err(error) if unresolvable_node(&error) => return Ok(Reached::Nothing),
5903 Err(error) => return Err(error),
5904 };
5905 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
5906 return Ok(Reached::Nothing);
5907 };
5908 if optional_str(node, "__typename")? == Some("DraftIssue") {
5909 return Ok(Reached::Draft);
5910 }
5911 Ok(match self.resolve_issue(node).await? {
5912 Some(item) => Reached::Held(Box::new(item)),
5913 None => Reached::Nothing,
5914 })
5915 }
5916
5917 /// One item of this board by its own id, whatever kind it is.
5918 ///
5919 /// Resolved from the identifier alone: no search, board-wide or otherwise. What this
5920 /// run wrote is read first, because a node read of an item created moments ago can
5921 /// still be behind the board field values written onto it — see [`Self::created`].
5922 async fn item_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
5923 if let Some(own) = self.created()?.iter().find(|own| own.id == *id) {
5924 return Ok(Some(own.clone()));
5925 }
5926 if let Some(held) = self.read_ahead()?.remove(id) {
5927 return Ok(held);
5928 }
5929 match self.reach(id).await? {
5930 Reached::Held(item) => Ok(Some(*item)),
5931 Reached::Nothing => Ok(None),
5932 Reached::Draft => self.draft_by_id(id).await,
5933 }
5934 }
5935
5936 /// [`Self::item_by_id`] for a document: one [`graphql::DOCUMENT_IN_PROJECT`] read, which
5937 /// answers the project it is filed under and that project's members beside it.
5938 ///
5939 /// Each is held for this command as a read of it would have been: the project for the
5940 /// next read of it by its id, and — when its page of members is the whole of them — the
5941 /// members for the next listing of that project. So a write held to who may read the
5942 /// document, which reads all three, is answered by this one request.
5943 async fn document_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
5944 if self.created()?.iter().any(|own| own.id == *id) || self.read_ahead()?.contains_key(id) {
5945 return self.item_by_id(id).await;
5946 }
5947 let asked = self
5948 .graphql(
5949 graphql::DOCUMENT_IN_PROJECT,
5950 json!({"id":id.0,"first":MAX_PAGE_SIZE,"memberFirst":MEMBER_PAGE_SIZE,
5951 "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
5952 "duplicates":true}),
5953 )
5954 .await;
5955 let data = match asked {
5956 Ok(data) => data,
5957 Err(error) if unresolvable_node(&error) => return Ok(None),
5958 Err(error) => return Err(error),
5959 };
5960 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
5961 return Ok(None);
5962 };
5963 if optional_str(node, "__typename")? == Some("DraftIssue") {
5964 return self.draft_by_id(id).await;
5965 }
5966 let Some(item) = self.resolve_issue(node).await? else {
5967 return Ok(None);
5968 };
5969 // An answer for another issue than the one asked about says nothing of that one.
5970 if item.id != *id {
5971 return Err(SourceError::Malformed {
5972 message: format!(
5973 "GitHub answered issue {} when asked for document {}",
5974 item.id.0, id.0
5975 ),
5976 });
5977 }
5978 if let Some(project) = node.get("project").filter(|value| !value.is_null())
5979 && let Some(parent) = self
5980 .resolve_issue(project)
5981 .await?
5982 .filter(|parent| Some(&parent.id) == item.parent.as_ref())
5983 {
5984 let members = project.get("subIssues").filter(|value| !value.is_null());
5985 let whole = members
5986 .and_then(|members| members.pointer("/pageInfo/hasNextPage"))
5987 .and_then(Value::as_bool)
5988 == Some(false);
5989 if whole {
5990 let nodes = members
5991 .and_then(|members| members.get("nodes"))
5992 .and_then(Value::as_array)
5993 .ok_or_else(|| SourceError::Malformed {
5994 message: "GitHub subIssues.nodes is not an array".into(),
5995 })?;
5996 let mut children = Vec::new();
5997 for member in nodes {
5998 if let Some(resolved) = self.resolve_issue(member).await? {
5999 children.push(resolved);
6000 }
6001 }
6002 // Held as the project's members only when every one resolved and says it is
6003 // filed under it: a page with any other answers nothing of all the members.
6004 if children.len() == nodes.len()
6005 && children
6006 .iter()
6007 .all(|child| child.parent.as_ref() == Some(&parent.id))
6008 {
6009 self.children_cache()?
6010 .insert(parent.id.clone(), (parent.id.clone(), children));
6011 }
6012 }
6013 self.read_ahead()?.insert(parent.id.clone(), Some(parent));
6014 }
6015 Ok(Some(item))
6016 }
6017
6018 /// Several items of this board, each by its own id, in order — what [`Self::item_by_id`]
6019 /// answers for each, read [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] rather
6020 /// than one request per id.
6021 ///
6022 /// What this run wrote answers first, as it does there, and only the rest is read. One id
6023 /// left to read is read by [`Self::item_by_id`] itself, which costs what a batch does. A
6024 /// batch GitHub refuses because one of its ids resolves to no node at all is read again one
6025 /// id at a time, so that id is answered as not held and the others as themselves; a draft
6026 /// is completed by a read of the draft, exactly as there.
6027 async fn items_by_ids(&self, ids: &[NativeId]) -> Result<Vec<Option<Resolved>>, SourceError> {
6028 let mut found: Vec<Option<Option<Resolved>>> = {
6029 let created = self.created()?;
6030 ids.iter()
6031 .map(|id| {
6032 created
6033 .iter()
6034 .find(|own| own.id == *id)
6035 .map(|own| Some(own.clone()))
6036 })
6037 .collect()
6038 };
6039 let unread: Vec<NativeId> = ids
6040 .iter()
6041 .zip(&found)
6042 .filter(|(_, found)| found.is_none())
6043 .map(|(id, _)| id.clone())
6044 .collect();
6045 let mut read = Vec::with_capacity(unread.len());
6046 if let [one] = unread.as_slice() {
6047 read.push(self.item_by_id(one).await?);
6048 } else {
6049 for batch in unread.chunks(DETAIL_BATCH) {
6050 let data = match self
6051 .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, None))
6052 .await
6053 {
6054 Ok(data) => data,
6055 Err(error) if unresolvable_node(&error) => {
6056 for id in batch {
6057 read.push(self.item_by_id(id).await?);
6058 }
6059 continue;
6060 }
6061 Err(error) => return Err(error),
6062 };
6063 for (slot, id) in batch.iter().enumerate() {
6064 let node =
6065 data.get(format!("i{slot}"))
6066 .ok_or_else(|| SourceError::Malformed {
6067 message: format!(
6068 "GitHub answered a batch read with no item for {}",
6069 id.0
6070 ),
6071 })?;
6072 read.push(if node.is_null() {
6073 None
6074 } else if optional_str(node, "__typename")? == Some("DraftIssue") {
6075 self.draft_by_id(id).await?
6076 } else {
6077 if optional_str(node, "__typename")? == Some("Issue")
6078 && required_str(node, "id")? != id.0
6079 {
6080 return Err(SourceError::Malformed {
6081 message: format!(
6082 "GitHub answered the read of {} with issue {}",
6083 id.0,
6084 required_str(node, "id")?
6085 ),
6086 });
6087 }
6088 self.resolve_issue(node).await?
6089 });
6090 }
6091 }
6092 }
6093 let mut read = read.into_iter();
6094 Ok(found
6095 .iter_mut()
6096 .map(|slot| slot.take().unwrap_or_else(|| read.next().flatten()))
6097 .collect())
6098 }
6099
6100 fn read_ahead(
6101 &self,
6102 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Option<Resolved>>>, SourceError> {
6103 self.read_ahead
6104 .lock()
6105 .map_err(|_| SourceError::Unavailable {
6106 message:
6107 "items read ahead for a copy were left inconsistent; run the command again"
6108 .into(),
6109 })
6110 }
6111
6112 fn resolved_cache(
6113 &self,
6114 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
6115 self.resolved_cache
6116 .lock()
6117 .map_err(|_| SourceError::Unavailable {
6118 message: "resolved item records were left inconsistent; run the command again"
6119 .into(),
6120 })
6121 }
6122
6123 /// Reuse a record this invocation already resolved. The mutation sender invalidates
6124 /// it before writing, so a partial failure cannot leave a pre-write binding behind.
6125 async fn bound_item(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6126 let cached = self.resolved_cache()?.get(id).cloned();
6127 if let Some(item) = cached {
6128 return Ok(Some(item));
6129 }
6130 self.item_by_id(id).await
6131 }
6132
6133 /// Take out the item an issue read by reference reached for `id`, when one is held: the
6134 /// status write it was read for is the one caller, so no other read or write is ever
6135 /// answered from a read that selected only what that write needs.
6136 fn take_held(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6137 Ok(self
6138 .held_cache
6139 .lock()
6140 .map_err(|_| SourceError::Unavailable {
6141 message: "items read by issue reference were left inconsistent; run the \
6142 command again"
6143 .into(),
6144 })?
6145 .remove(id))
6146 }
6147
6148 /// One board draft by its own id, with the board item it sits in — or `None` when no
6149 /// item of this board is that draft's.
6150 ///
6151 /// The same decision [`Self::resolve_issue`] makes for an issue, over the draft's own
6152 /// `projectV2Items`: an entry naming this board is what makes it this board's. GitHub
6153 /// links a draft to one board item, so the page this read carries is the whole of that
6154 /// connection, and a page that reports more than it holds is refused rather than read
6155 /// as an answer about memberships nobody read.
6156 async fn draft_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6157 let data = self
6158 .graphql(
6159 graphql::DRAFT,
6160 json!({"id":id.0,"nestedFirst":NESTED_PAGE_SIZE,
6161 "boardItems":BOARD_ITEMS_PAGE_SIZE}),
6162 )
6163 .await?;
6164 // Gone between the two reads is an answer — the draft is no longer there. Anything
6165 // else than the draft [`Self::reach`] was just told this id is, is not one.
6166 let Some(draft) = data.get("node").filter(|node| !node.is_null()) else {
6167 return Ok(None);
6168 };
6169 if optional_str(draft, "__typename")? != Some("DraftIssue") {
6170 return Err(SourceError::Malformed {
6171 message: format!(
6172 "GitHub answered {} as a draft and then as something else",
6173 id.0
6174 ),
6175 });
6176 }
6177 if required_str(draft, "id")? != id.0 {
6178 return Err(SourceError::Malformed {
6179 message: format!("GitHub answered a different draft for {}", id.0),
6180 });
6181 }
6182 let memberships = draft
6183 .get("projectV2Items")
6184 .ok_or_else(|| SourceError::Malformed {
6185 message: format!("GitHub draft {} is missing projectV2Items", id.0),
6186 })?;
6187 let nodes = memberships
6188 .get("nodes")
6189 .and_then(Value::as_array)
6190 .ok_or_else(|| SourceError::Malformed {
6191 message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
6192 })?;
6193 let info = memberships
6194 .get("pageInfo")
6195 .ok_or_else(|| SourceError::Malformed {
6196 message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
6197 })?;
6198 // Read whether or not this board's entry is on the page: a page claiming more than
6199 // the one item GitHub links a draft to is a malformed answer either way.
6200 if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
6201 return Err(SourceError::Malformed {
6202 message: format!(
6203 "GitHub draft {} reports more board items than the one GitHub links a draft \
6204 to",
6205 id.0
6206 ),
6207 });
6208 }
6209 if let Some(node) = nodes.first()
6210 && node
6211 .pointer("/project/number")
6212 .and_then(Value::as_u64)
6213 .is_none()
6214 {
6215 return Err(SourceError::Malformed {
6216 message: format!(
6217 "GitHub draft {} board item has no numeric project number",
6218 id.0
6219 ),
6220 });
6221 }
6222 let Some(held) = self.board_entry(nodes) else {
6223 return Ok(None);
6224 };
6225 if required_str(
6226 held.get("project").ok_or_else(|| SourceError::Malformed {
6227 message: format!("GitHub draft {} board item has no project", id.0),
6228 })?,
6229 "id",
6230 )? != self.board_fields().await?.id.as_str()
6231 {
6232 return Ok(None);
6233 }
6234 let item = json!({
6235 "id": required_str(held, "id")?,
6236 "project": held.get("project"),
6237 "fieldValues": held.get("fieldValues"),
6238 "content": draft,
6239 });
6240 self.resolve(&item)
6241 }
6242
6243 /// The board's own id and field definitions, for a write whose item does not carry
6244 /// them — never its items.
6245 ///
6246 /// A board this command has already listed supplies them, since it read them beside its
6247 /// items; otherwise they come from [`graphql::BOARD_FIELDS`], once per command. Neither
6248 /// is consulted about which items the board holds: see the module documentation for
6249 /// why a question about one known item is answered by reading that item.
6250 async fn board_fields(&self) -> Result<BoardFields, SourceError> {
6251 if let Some(board) = self.board_cache()?.as_ref() {
6252 return Ok(BoardFields {
6253 id: BoardId::parse(&board.id)?,
6254 fields: board.fields.clone(),
6255 });
6256 }
6257 if let Some(held) = self.fields_cache()?.clone() {
6258 return Ok(held);
6259 }
6260 let data = self
6261 .graphql(
6262 graphql::BOARD_FIELDS,
6263 json!({"owner":self.owner,"number":self.project_number,
6264 "nestedFirst":NESTED_PAGE_SIZE}),
6265 )
6266 .await?;
6267 self.fields_read(&data)
6268 }
6269
6270 /// The board's id and fields out of an answer carrying the `boardFields` root, held for
6271 /// the rest of this command.
6272 fn fields_read(&self, data: &Value) -> Result<BoardFields, SourceError> {
6273 let board = data
6274 .pointer("/boardFields/projectV2")
6275 .filter(|value| !value.is_null())
6276 .ok_or_else(|| SourceError::Refused {
6277 message: format!(
6278 "GitHub project {}/{} was not found or is not visible to the token",
6279 self.owner, self.project_number
6280 ),
6281 })?;
6282 let read = BoardFields {
6283 id: BoardId::parse(required_str(board, "id")?)?,
6284 fields: board.get("fields").cloned().unwrap_or(Value::Null),
6285 };
6286 *self.fields_cache()? = Some(read.clone());
6287 Ok(read)
6288 }
6289
6290 /// Read what creating an issue in `repository` needs and this command has not read yet —
6291 /// the board's fields and the repository's node id — in one request when it needs both.
6292 ///
6293 /// When either is already known this sends nothing, and the other is read by its own
6294 /// document where it is asked for, so no create reads anything twice.
6295 async fn creation_context(
6296 &self,
6297 repository: &RepositoryTarget,
6298 incoming: &Incoming<'_>,
6299 ) -> Result<(), SourceError> {
6300 let fields_known = self.board_cache()?.is_some() || self.fields_cache()?.is_some();
6301 if fields_known || self.repository_cache()?.contains_key(repository) {
6302 return Ok(());
6303 }
6304 let data = self
6305 .graphql(
6306 graphql::CREATION_CONTEXT,
6307 json!({"owner":self.owner,"number":self.project_number,
6308 "nestedFirst":NESTED_PAGE_SIZE,"repositoryOwner":repository.owner,
6309 "repositoryName":repository.name}),
6310 )
6311 .await?;
6312 self.fields_read(&data)?;
6313 self.repository_read(&data, repository, incoming)?;
6314 Ok(())
6315 }
6316
6317 /// This process's own view of the board's fields, or the refusal a poisoned lock is.
6318 fn fields_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<BoardFields>>, SourceError> {
6319 self.fields_cache
6320 .lock()
6321 .map_err(|_| SourceError::Unavailable {
6322 message: "this source's view of the board's fields was left inconsistent by an \
6323 earlier failure; next: run the command again"
6324 .into(),
6325 })
6326 }
6327
6328 /// What a write to `item` needs of the board, read off that item when it says enough and
6329 /// off [`Self::board_fields`] when it does not.
6330 ///
6331 /// A node read of an item names its board and carries the definition of every field it
6332 /// holds a value of — so an item naming its board, holding a value of the origin field
6333 /// when the write carries an origin, and, when the write carries a status, holding a
6334 /// `Status` value, needs no read of the board at all. **Nothing the item does not say is
6335 /// guessed:** a field it holds no value of may still be on the board, and a view reading it
6336 /// as absent would refuse a write the board can take or skip a field write the board
6337 /// needs, so such an item — and a create, which has no item yet — takes the board's fields
6338 /// from their own read instead. A write carrying no origin to an item holding none — every
6339 /// write out of a source declared private — writes no origin whatever the board defines,
6340 /// so it needs no definition of that field.
6341 async fn fields_for(
6342 &self,
6343 item: Option<&Resolved>,
6344 writes_origin: bool,
6345 writes_status: bool,
6346 selects_priority: bool,
6347 ) -> Result<BoardFields, SourceError> {
6348 if let Some(board) = item.and_then(Resolved::carried_board) {
6349 return Ok(board);
6350 }
6351 if let Some(item) = item
6352 && let Some(board_id) = item.named_board()
6353 && (!writes_origin || item.defines(ORIGIN_FIELD))
6354 && (!writes_status || item.defines("Status"))
6355 && (!selects_priority || item.defines(PRIORITY_FIELD))
6356 && self
6357 .metadata_fields
6358 .iter()
6359 .all(|projection| item.defines(&projection.field))
6360 {
6361 return Ok(BoardFields {
6362 id: board_id,
6363 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
6364 });
6365 }
6366 self.board_fields().await
6367 }
6368
6369 /// Everything filed under one issue of this board, walked to exhaustion — or `None`
6370 /// when that id names nothing here with a sub-issue relationship to walk.
6371 ///
6372 /// `None` and an empty answer are different: `None` is *this is not an issue of this
6373 /// GitHub*, which is what sends a project selector on to be read as a name, and an
6374 /// empty vector is a project that holds nothing.
6375 async fn sub_issues(&self, id: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
6376 let mut after: Option<String> = None;
6377 let mut children = Vec::new();
6378 // The project issue as the first page carried it, held once the walk has ended.
6379 let mut parent: Option<Resolved> = None;
6380 loop {
6381 let asked = self
6382 .graphql(
6383 graphql::SUB_ISSUES,
6384 json!({"id":id.0,"memberFirst":MEMBER_PAGE_SIZE,"after":after,
6385 "nestedFirst":NESTED_PAGE_SIZE,
6386 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
6387 )
6388 .await;
6389 let data = match asked {
6390 Ok(data) => data,
6391 // A string that is not a node id at all is not a failure to report: it is
6392 // the ordinary answer to a selector naming a project by its name.
6393 Err(error) if unresolvable_node(&error) => return Ok(None),
6394 Err(error) => return Err(error),
6395 };
6396 let Some(connection) = data
6397 .pointer("/node/subIssues")
6398 .filter(|value| !value.is_null())
6399 else {
6400 // No such node, or one with no sub-issue relationship — a board draft is
6401 // the one this board can really hold.
6402 return Ok(None);
6403 };
6404 if after.is_none()
6405 && let Some(node) = data.get("node").filter(|node| node.get("id").is_some())
6406 {
6407 parent = self
6408 .resolve_issue(node)
6409 .await?
6410 .filter(|resolved| resolved.id == *id);
6411 }
6412 for node in connection
6413 .get("nodes")
6414 .and_then(Value::as_array)
6415 .ok_or_else(|| SourceError::Malformed {
6416 message: "GitHub subIssues.nodes is not an array".into(),
6417 })?
6418 {
6419 if let Some(resolved) = self.resolve_issue(node).await? {
6420 children.push(resolved);
6421 }
6422 }
6423 let info = connection
6424 .get("pageInfo")
6425 .ok_or_else(|| SourceError::Malformed {
6426 message: "GitHub subIssues connection has no pageInfo".into(),
6427 })?;
6428 let next = required_bool(info, "hasNextPage")?
6429 .then(|| required_str(info, "endCursor"))
6430 .transpose()?;
6431 match next {
6432 Some(next) => {
6433 validate_cursor_progress(after.as_deref(), next)?;
6434 after = Some(next.to_owned());
6435 }
6436 None => {
6437 // Held only once the walk has ended, so one failing part way holds
6438 // nothing and every read it would have answered is made.
6439 // The project issue the first page carried answers the next read of the
6440 // project by its id in this command, as a read of it made now would: what
6441 // classifying a project for a write reads straight after its members.
6442 if let Some(parent) = &parent {
6443 self.read_ahead()?
6444 .entry(parent.id.clone())
6445 .or_insert_with(|| Some(parent.clone()));
6446 }
6447 let mut carried = self.carried()?;
6448 for item in parent.into_iter().chain(children.iter().cloned()) {
6449 carried.insert(item.id.clone(), item);
6450 }
6451 return Ok(Some(children));
6452 }
6453 }
6454 }
6455 }
6456
6457 fn carried(
6458 &self,
6459 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
6460 self.carried.lock().map_err(|_| SourceError::Unavailable {
6461 message: "items a project's listing carried were left inconsistent; run the \
6462 command again"
6463 .into(),
6464 })
6465 }
6466
6467 /// Which issue of this board a project *name* is, or `None` when none is.
6468 ///
6469 /// One bounded query which filters on that name at the server, rather than a walk of
6470 /// every issue the board holds. The name is compared again here: the qualifier narrows
6471 /// what GitHub sends, and this source decides what it names.
6472 async fn project_by_name(&self, name: &str) -> Result<Option<NativeId>, SourceError> {
6473 let search = self.board_search(Some(&title_qualifier(name)));
6474 let mut after = None;
6475 loop {
6476 let (candidates, next) = self
6477 .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
6478 .await?;
6479 if let Some(item) = candidates.into_iter().find(|item| {
6480 item.kind == BoardKind::Work(ItemKind::Project)
6481 && item.title.eq_ignore_ascii_case(name)
6482 }) {
6483 return Ok(Some(item.id));
6484 }
6485 match next {
6486 Some(next) => after = Some(next),
6487 None => return Ok(None),
6488 }
6489 }
6490 }
6491
6492 /// Everything filed under one project of this board: the sub-issues of the issue that
6493 /// project is.
6494 ///
6495 /// Tasks *and* documents, because a document filed under a project is a sub-issue of it
6496 /// too — the caller keeps the kind it asked for. Nothing about this grows as the board
6497 /// gains projects, or as another project gains tasks.
6498 ///
6499 /// A qualified id names the issue and is asked for its sub-issues directly: one
6500 /// request, no search of any kind. Only a selector GitHub cannot resolve that way is
6501 /// read as a project *name*, which costs the one bounded search
6502 /// [`Self::project_by_name`] makes.
6503 ///
6504 /// What GitHub answered is held for the rest of the command — see [`Self::children_cache`]
6505 /// — and each answer is still cut to the items whose parent is this project, so one this
6506 /// process has since filed elsewhere is not reported here, and completed with what this
6507 /// process filed under it.
6508 async fn project_children(&self, selector: &NativeId) -> Result<Vec<Resolved>, SourceError> {
6509 let held = self.children_cache()?.get(selector).cloned();
6510 let (project, children) = match held {
6511 Some(held) => held,
6512 None => {
6513 let answered = match self.sub_issues(selector).await? {
6514 Some(children) => (selector.clone(), children),
6515 None => match self.project_by_name(&selector.0).await? {
6516 Some(project) => {
6517 let children = self.sub_issues(&project).await?.unwrap_or_default();
6518 (project, children)
6519 }
6520 None => return Ok(Vec::new()),
6521 },
6522 };
6523 self.children_cache()?
6524 .insert(selector.clone(), answered.clone());
6525 answered
6526 }
6527 };
6528 let mut children: Vec<Resolved> = children
6529 .into_iter()
6530 .filter(|child| child.parent.as_ref() == Some(&project))
6531 .collect();
6532 for own in self.updated()?.iter() {
6533 if own.parent.as_ref() == Some(&project) && !children.iter().any(|c| c.id == own.id) {
6534 children.push(own.clone());
6535 }
6536 }
6537 self.completed_with_written(children, |own| own.parent.as_ref() == Some(&project))
6538 }
6539
6540 /// Every issue of this board GitHub's issue search reports updated at or after `since`,
6541 /// completed with what this run wrote — the candidates a comment-activity read confirms.
6542 ///
6543 /// Scoped by the board and by nothing else: `project:<owner>/<number>` reaches every issue
6544 /// on the board whatever repository, and whatever owner, it lives in, so no repository or
6545 /// owner qualifier is added and none is needed. What makes the `updated:` qualifier
6546 /// sufficient is a fact about GitHub rather than about this source: a comment written on an
6547 /// issue **and a comment edited on it** both move that issue's `updatedAt`. The credentialed
6548 /// journey `an_edited_comment_moves_its_issue_and_is_selected_since` in `tests/journey`
6549 /// re-takes that fact on every run of the lane, so a change on GitHub's side fails there
6550 /// rather than silently narrowing a caller's answer.
6551 ///
6552 /// The instant is written to the second, rounded down, which can only widen what the
6553 /// search returns; confirmation against each candidate's own comments is what makes the
6554 /// answer exact. The search is an index that lags a write by a second or two — the module
6555 /// documentation records it — so a caller that asks again from its last instant should
6556 /// overlap the two by more than that.
6557 async fn updated_since(&self, since: DateTime<Utc>) -> Result<Vec<Resolved>, SourceError> {
6558 let found = self.searched(&updated_qualifier(since)).await?;
6559 self.completed_with_written(found, |_| true)
6560 }
6561
6562 /// Every issue of this board GitHub's issue search reports for the board-scoped search
6563 /// narrowed by `also`, in pages of [`SEARCH_PAGE_SIZE`].
6564 ///
6565 /// Uncompleted: what this process wrote is added by the caller, which knows whether its
6566 /// own record is the fresher of the two.
6567 async fn searched(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
6568 let search = self.board_search(Some(also));
6569 let mut after: Option<String> = None;
6570 let mut found = Vec::new();
6571 loop {
6572 let (page, next) = self
6573 .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
6574 .await?;
6575 found.extend(page);
6576 match next {
6577 Some(next) => after = Some(next),
6578 None => return Ok(found),
6579 }
6580 }
6581 }
6582
6583 /// A bounded task answer; the versioned cursor carries the connection position, how
6584 /// many rows of the page starting there were already handed out, and the own-write ids
6585 /// already observed, including across a new source instance.
6586 ///
6587 /// Every page is sent at [`SEARCH_PAGE_SIZE`] whatever the caller's limit, and a limit is
6588 /// sliced from the pages it needs; why is the module documentation's paging contract.
6589 async fn search_tasks(
6590 &self,
6591 query: &TaskQuery,
6592 page: &PageRequest,
6593 also: &str,
6594 ) -> Result<Page<Task>, SourceError> {
6595 let mut position = match &page.cursor {
6596 None => SearchPosition::default(),
6597 Some(cursor) => serde_json::from_str::<SearchPosition>(&cursor.0)
6598 .ok()
6599 .filter(|position| {
6600 position.version == SEARCH_CURSOR_VERSION
6601 && position.connection.valid_resume(position.offset)
6602 })
6603 .ok_or_else(|| SourceError::Config {
6604 message: "page cursor is invalid".into(),
6605 })?,
6606 };
6607 let search = self.board_search(Some(also));
6608 let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
6609 let own = self.with_own_writes(Vec::new())?;
6610 // An issue this process commented on is a candidate of a comment-activity read
6611 // whether or not the search has caught up with the comment; see `Self::commented`.
6612 let commented = match query.commented_since {
6613 Some(_) => self.commented()?.clone(),
6614 None => Vec::new(),
6615 };
6616 for id in own.iter().map(|item| &item.id).chain(&commented) {
6617 if !position.own.contains(id) {
6618 position.own.push(id.clone());
6619 }
6620 }
6621 let mut tasks = Vec::new();
6622 while !position.connection.exhausted() && tasks.len() < limit {
6623 let first = SEARCH_PAGE_SIZE;
6624 // Page size is part of the key: a short cached answer cannot answer a wider ask.
6625 let key =
6626 serde_json::to_string(&("page", &search, &position.connection.after(), first))
6627 .expect("search page key is serializable");
6628 let cached = if query.commented_since.is_none() {
6629 self.narrowed_cache()?.get(&key).cloned()
6630 } else {
6631 None
6632 };
6633 let (found, next) = match cached {
6634 Some(found) => {
6635 let next = self
6636 .search_next
6637 .lock()
6638 .map_err(|_| SourceError::Unavailable {
6639 message:
6640 "search pagination was left inconsistent; run the command again"
6641 .into(),
6642 })?
6643 .get(&key)
6644 .cloned()
6645 .flatten();
6646 (found, next)
6647 }
6648 None => {
6649 let (found, next) = self
6650 .search_page(&search, first, position.connection.after())
6651 .await?;
6652 if query.commented_since.is_none() {
6653 self.search_next
6654 .lock()
6655 .map_err(|_| SourceError::Unavailable {
6656 message:
6657 "search pagination was left inconsistent; run the command again"
6658 .into(),
6659 })?
6660 .insert(key.clone(), next.clone());
6661 self.narrowed_cache()?.insert(key, found.clone());
6662 }
6663 (found, next)
6664 }
6665 };
6666 let rows = found.len();
6667 for mut item in found.into_iter().skip(position.offset) {
6668 if tasks.len() == limit {
6669 break;
6670 }
6671 position.offset += 1;
6672 if position.own.contains(&item.id) {
6673 if position.seen.contains(&item.id) {
6674 continue;
6675 }
6676 position.seen.push(item.id.clone());
6677 // The search's own copy of an issue this process only commented on is as
6678 // good as a node read of it, since its comments are read either way.
6679 let only_commented = commented.contains(&item.id)
6680 && !own.iter().any(|written| written.id == item.id);
6681 if !only_commented {
6682 let updated_at = item.updated_at;
6683 let Some(written) = self.search_written(&own, &item.id).await? else {
6684 continue;
6685 };
6686 item = written;
6687 item.updated_at = item.updated_at.max(updated_at);
6688 self.resolved_cache()?.insert(item.id.clone(), item.clone());
6689 }
6690 }
6691 if item.kind == BoardKind::Work(ItemKind::Task) {
6692 let task = item.task()?;
6693 if task_matches(&task, query, &query.project)
6694 && self.commented_since(&item, query.commented_since).await?
6695 {
6696 tasks.push(task);
6697 }
6698 }
6699 }
6700 if position.offset < rows {
6701 continue;
6702 }
6703 position.offset = 0;
6704 position.connection = match next {
6705 Some(after) => SearchConnection::Continuing {
6706 after: Cursor(after),
6707 },
6708 None => SearchConnection::Exhausted {},
6709 };
6710 }
6711 if position.connection.exhausted() {
6712 for id in position.own.clone() {
6713 if position.seen.contains(&id) {
6714 continue;
6715 }
6716 if tasks.len() == limit {
6717 break;
6718 }
6719 position.seen.push(id.clone());
6720 let Some(item) = self.search_written(&own, &id).await? else {
6721 continue;
6722 };
6723 if item.kind == BoardKind::Work(ItemKind::Task) {
6724 let task = item.task()?;
6725 if task_matches(&task, query, &query.project)
6726 && self.commented_since(&item, query.commented_since).await?
6727 {
6728 tasks.push(task);
6729 }
6730 }
6731 }
6732 }
6733 let more = !position.connection.exhausted()
6734 || position.own.iter().any(|id| !position.seen.contains(id));
6735 Ok(Page {
6736 items: tasks,
6737 next: more.then(|| {
6738 Cursor(serde_json::to_string(&position).expect("search position is serializable"))
6739 }),
6740 })
6741 }
6742
6743 /// A resumed process has the ids but no write records; resolve only a record the
6744 /// current page needs, by its uncached node read rather than the lagging search index.
6745 async fn search_written(
6746 &self,
6747 own: &[Resolved],
6748 id: &NativeId,
6749 ) -> Result<Option<Resolved>, SourceError> {
6750 match own.iter().find(|item| item.id == *id) {
6751 Some(item) => Ok(Some(item.clone())),
6752 None => self.item_by_id(id).await,
6753 }
6754 }
6755
6756 /// The candidates for a task query carrying a text, metadata or origin predicate, read
6757 /// without enumerating the board — or `None` for a query carrying none of the three, which
6758 /// keeps the reads it always had.
6759 ///
6760 /// An origin is answered by [`Self::origin_candidates`], whatever else the query carries,
6761 /// because it names at most a handful of items. Text and metadata are answered by one
6762 /// board-scoped issue search carrying every term — see [`narrowing_qualifiers`] — narrowed
6763 /// further by `updated:>=` when the query also asks for comment activity, since both
6764 /// qualifiers must hold of an issue the answer keeps. Every candidate is confirmed in
6765 /// process afterwards by the same predicates [`task_matches`] applies to every read.
6766 ///
6767 /// Completed with what this process wrote, its own record winning over the index's copy
6768 /// of the same item: see [`Self::with_own_writes`].
6769 async fn narrowed(&self, query: &TaskQuery) -> Result<Option<Vec<Resolved>>, SourceError> {
6770 self.send_pending_origins(None).await?;
6771 let asked = match (&query.origin, narrowing_qualifiers(query)) {
6772 (Some(origin), _) => Narrowing::Origin(origin.clone()),
6773 (None, Some(qualifiers)) => Narrowing::Search(match query.commented_since {
6774 Some(since) => format!("{} {qualifiers}", updated_qualifier(since)),
6775 None => qualifiers,
6776 }),
6777 (None, None) => return Ok(None),
6778 };
6779 // A question about comment activity is asked afresh every time, as it always was: it
6780 // is the one a caller polls from one source while waiting for the index, and an
6781 // answer held from the first poll would be the answer to every later one.
6782 let key = query.commented_since.is_none().then(|| asked.key());
6783 let cached = match &key {
6784 Some(key) => self.narrowed_cache()?.get(key).cloned(),
6785 None => None,
6786 };
6787 let found = match cached {
6788 Some(found) => found,
6789 None => {
6790 let found = match &asked {
6791 Narrowing::Origin(origin) => self.origin_candidates(origin).await?,
6792 Narrowing::Search(also) => self.searched(also).await?,
6793 };
6794 if let Some(key) = key {
6795 self.narrowed_cache()?.insert(key, found.clone());
6796 }
6797 found
6798 }
6799 };
6800 self.with_own_writes(found).map(Some)
6801 }
6802
6803 /// The candidates for a project or unscoped document query carrying a searchable text,
6804 /// read without enumerating the board — or `None` for a query with no text or a blank one,
6805 /// which keeps the read it always had.
6806 ///
6807 /// The text is sent as the very phrase a task query's text is — see [`text_qualifiers`] —
6808 /// in one board-scoped issue search walked to its end at [`SEARCH_PAGE_SIZE`], so what it
6809 /// costs is the issues that match and never the board. Its answer is held for the command
6810 /// under the same key [`Self::narrowed`] holds that search under, so a walk of the caller's
6811 /// pages asks GitHub once. Every candidate is confirmed afterwards by its kind and by the
6812 /// substring rule, exactly as an item of the wider read was, and is completed with what this
6813 /// process wrote: see [`Self::with_own_writes`].
6814 async fn text_searched(
6815 &self,
6816 text: Option<&TextQuery>,
6817 ) -> Result<Option<Vec<Resolved>>, SourceError> {
6818 let Some(also) = text_qualifiers(text) else {
6819 return Ok(None);
6820 };
6821 let key = Narrowing::Search(also.clone()).key();
6822 let cached = self.narrowed_cache()?.get(&key).cloned();
6823 let found = match cached {
6824 Some(found) => found,
6825 None => {
6826 let found = self.searched(&also).await?;
6827 self.narrowed_cache()?.insert(key, found.clone());
6828 found
6829 }
6830 };
6831 self.with_own_writes(found).map(Some)
6832 }
6833
6834 /// Every `kind` item of this board whose own origin is exactly `origin`: the candidates
6835 /// [`Self::narrowed`] reads for a task query carrying that origin — one
6836 /// [`graphql::ORIGIN_LOOKUP`] walk, held for the command under the same key, completed with
6837 /// what this process wrote — each confirmed against its own origin entry.
6838 ///
6839 /// What answers a project or document listing by origin, so finding every copy of one plan
6840 /// costs the items that carry its origin and never a page of the board.
6841 async fn copied_from(
6842 &self,
6843 origin: &str,
6844 kind: BoardKind,
6845 ) -> Result<Vec<Resolved>, SourceError> {
6846 let wanted = TaskQuery {
6847 origin: Some(origin.to_owned()),
6848 ..TaskQuery::default()
6849 };
6850 Ok(self
6851 .narrowed(&wanted)
6852 .await?
6853 .unwrap_or_default()
6854 .into_iter()
6855 .filter(|item| item.kind == kind && wanted.origin_matches(&item.metadata()))
6856 .collect())
6857 }
6858
6859 /// Every item of this board that may carry `origin` — a superset of those that do — found
6860 /// by [`graphql::ORIGIN_LOOKUP`] and never by enumerating the board.
6861 ///
6862 /// The union of the board's own field filter over the `onetaskgraph.origin` text field —
6863 /// which reads the field every carrier holds, whichever release wrote it — and the
6864 /// board-scoped issue search for the same id as a phrase in the body, where this source
6865 /// mirrors it. The caller adds the third read, this process's own writes. Candidates are
6866 /// returned unconfirmed; [`task_matches`] compares each one's own origin field with the
6867 /// query's, exactly.
6868 ///
6869 /// Both connections are walked to exhaustion, each from its own cursor. One that has
6870 /// already ended is sent its last cursor again, which answers an empty page, so the one
6871 /// document serves every page of either. What the two leave is stated in the module
6872 /// documentation: a carrier another process added within the last second or two, before
6873 /// either index has it.
6874 async fn origin_candidates(&self, origin: &str) -> Result<Vec<Resolved>, SourceError> {
6875 let filter = format!("{ORIGIN_FIELD}:{}", quoted(origin));
6876 let search = self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))));
6877 let mut items_after: Option<String> = None;
6878 let mut search_after: Option<String> = None;
6879 let mut found: Vec<Resolved> = Vec::new();
6880 let keep = |resolved: Resolved, found: &mut Vec<Resolved>| {
6881 if !found.iter().any(|held| held.id == resolved.id) {
6882 found.push(resolved);
6883 }
6884 };
6885 loop {
6886 let data = self
6887 .graphql(
6888 graphql::ORIGIN_LOOKUP,
6889 json!({"owner":self.owner,"number":self.project_number,"filter":filter,
6890 "search":search,"type":"ISSUE","originFirst":ORIGIN_PAGE_SIZE,
6891 "itemsAfter":items_after,"searchAfter":search_after,
6892 "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
6893 "duplicates":true}),
6894 )
6895 .await?;
6896 let items = data
6897 .pointer("/originItems/projectV2/items")
6898 .filter(|value| !value.is_null())
6899 .ok_or_else(|| SourceError::Refused {
6900 message: format!(
6901 "GitHub project {}/{} was not found or is not visible to the token",
6902 self.owner, self.project_number
6903 ),
6904 })?;
6905 for item in optional_nodes(Some(items), "project items")?
6906 .into_iter()
6907 .flatten()
6908 {
6909 // The board's own items list its drafts too, and a draft is not an issue: no
6910 // narrowed read answers with one, whatever its origin field holds.
6911 if let Some(resolved) = self.resolve(item)?
6912 && resolved.content_kind == ContentKind::Issue
6913 {
6914 keep(resolved, &mut found);
6915 }
6916 }
6917 let searched = data.get("search").ok_or_else(|| SourceError::Malformed {
6918 message: "GitHub search response has no search connection".into(),
6919 })?;
6920 for node in optional_nodes(Some(searched), "search")?
6921 .into_iter()
6922 .flatten()
6923 {
6924 if let Some(resolved) = self.resolve_issue(node).await? {
6925 keep(resolved, &mut found);
6926 }
6927 }
6928 let items_next = resumed(items, items_after.as_deref())?;
6929 let search_next = resumed(searched, search_after.as_deref())?;
6930 if !items_next.has_more() && !search_next.has_more() {
6931 return Ok(found);
6932 }
6933 items_after = items_next.cursor();
6934 search_after = search_next.cursor();
6935 }
6936 }
6937
6938 /// `found`, with every item this process created or wrote in its place, and every one of
6939 /// them the read did not report added.
6940 ///
6941 /// This process's own record wins over the read's copy of the same item, because a read
6942 /// of an item written moments ago can still be behind what was written onto it — the
6943 /// origin field included, which is the one a narrowed read is confirmed against — and a
6944 /// read that still names an item under a predicate this process's write moved it out of
6945 /// must not return it. The one thing the read knows that the record cannot is when GitHub
6946 /// last saw the item change, which is what a comment-activity read rules a candidate out
6947 /// by, so the read's `updatedAt` is kept when the record has none of its own. See
6948 /// [`Self::created`] and [`Self::updated`](GitHubProjectsSource::updated).
6949 fn with_own_writes(&self, mut found: Vec<Resolved>) -> Result<Vec<Resolved>, SourceError> {
6950 // A board draft is not an issue, so no narrowed read returns one, and this process
6951 // having written one does not make it an answer either.
6952 let own: Vec<Resolved> = self
6953 .created()?
6954 .iter()
6955 .chain(self.updated()?.iter())
6956 .filter(|own| own.content_kind == ContentKind::Issue)
6957 .cloned()
6958 .collect();
6959 for mut own in own {
6960 self.resolved_cache()?.insert(own.id.clone(), own.clone());
6961 match found.iter_mut().find(|read| read.id == own.id) {
6962 Some(read) => {
6963 own.updated_at = own.updated_at.max(read.updated_at);
6964 *read = own;
6965 }
6966 None => found.push(own),
6967 }
6968 }
6969 Ok(found)
6970 }
6971
6972 /// Whether `item` has a comment created or last edited at or after `since` — always, when
6973 /// there is no instant to hold it to.
6974 ///
6975 /// The candidate's own `updatedAt` is read first, because a comment written or edited at
6976 /// or after the instant moved it there: an issue not updated since holds no such comment,
6977 /// and its comments are never asked for — unless this process commented on it in this
6978 /// command, when the `updatedAt` held may predate that comment; see [`Self::commented`]. Otherwise its comments are walked, oldest first,
6979 /// only as far as the first that matches. A board draft is not an issue and has no
6980 /// comments, so it never matches.
6981 async fn commented_since(
6982 &self,
6983 item: &Resolved,
6984 since: Option<DateTime<Utc>>,
6985 ) -> Result<bool, SourceError> {
6986 let Some(since) = since else {
6987 return Ok(true);
6988 };
6989 if item.content_kind == ContentKind::DraftIssue {
6990 return Ok(false);
6991 }
6992 // An `updatedAt` this process's own record or a lagging index holds can predate a
6993 // comment this process wrote since, so only an issue it did not comment on is ruled
6994 // out by one.
6995 if item.updated_at.is_some_and(|updated| updated < since)
6996 && !self.commented()?.contains(&item.id)
6997 {
6998 return Ok(false);
6999 }
7000 let query = TaskQuery {
7001 commented_since: Some(since),
7002 ..TaskQuery::default()
7003 };
7004 let mut after: Option<String> = None;
7005 loop {
7006 let data = self
7007 .graphql(
7008 graphql::ISSUE_COMMENTS,
7009 json!({"id":item.id.0,"first":MAX_PAGE_SIZE,"after":after}),
7010 )
7011 .await?;
7012 let Some(connection) = data
7013 .get("node")
7014 .filter(|value| !value.is_null())
7015 .and_then(|node| node.get("comments"))
7016 .filter(|value| !value.is_null())
7017 else {
7018 // Removed since the search reported it: no longer an issue with comments.
7019 return Ok(false);
7020 };
7021 let comments = optional_nodes(Some(connection), "issue comments")?
7022 .into_iter()
7023 .flatten()
7024 .map(comment_from)
7025 .collect::<Result<Vec<_>, _>>()?;
7026 if query.comments_match(&comments) {
7027 return Ok(true);
7028 }
7029 match next_cursor(connection)? {
7030 Some(next) => {
7031 validate_cursor_progress(after.as_deref(), &next.0)?;
7032 after = Some(next.0);
7033 }
7034 None => return Ok(false),
7035 }
7036 }
7037 }
7038
7039 /// Every item on the board: the union of both enumerations GitHub offers of one.
7040 ///
7041 /// Neither contains the other, so neither is dropped — only `ProjectV2.items` lists a
7042 /// board **draft** and reads the board's own fields beside its items, and only the search
7043 /// reports an item that connection is behind on. The module documentation is where the lag and the
7044 /// measurements behind it are written down.
7045 ///
7046 /// A search result is admitted on the same terms as any other issue this source reaches
7047 /// directly — [`Self::resolve_issue`] keeps it only if that issue's own `projectItems`
7048 /// names *this* board — so an issue the index still believes is here after it was taken
7049 /// off is refused rather than reported.
7050 ///
7051 /// See [`Self::board_cache`]. Both completions happen on every call rather than once,
7052 /// which is what the cache could otherwise have broken.
7053 async fn board(&self) -> Result<Board, SourceError> {
7054 self.board_completed_by(None).await
7055 }
7056
7057 /// [`Self::board`], its search half narrowed by `also` when it is given — which an issue
7058 /// that half could add is then answered only if it holds.
7059 async fn board_completed_by(&self, also: Option<&str>) -> Result<Board, SourceError> {
7060 let cached = self.board_cache()?.clone();
7061 let mut board = match cached {
7062 Some(board) => board,
7063 None => {
7064 let read = self.read_board().await?;
7065 *self.board_cache()? = Some(read.clone());
7066 read
7067 }
7068 };
7069 let searched = match also {
7070 None => self.searched_issues().await?,
7071 Some(also) => self.searched_narrowly(also).await?,
7072 };
7073 for held in searched {
7074 if !board.items.iter().any(|item| item.id == held.id) {
7075 board.items.push(held);
7076 }
7077 }
7078 for own in self.created()?.iter() {
7079 if !board.items.iter().any(|item| item.id == own.id) {
7080 board.items.push(own.clone());
7081 }
7082 }
7083 Ok(board)
7084 }
7085
7086 /// This process's own view of the board, or the refusal a poisoned lock is.
7087 fn board_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<Board>>, SourceError> {
7088 self.board_cache
7089 .lock()
7090 .map_err(|_| SourceError::Unavailable {
7091 message: "this source's view of the board was left inconsistent by an earlier \
7092 failure; next: run the command again"
7093 .into(),
7094 })
7095 }
7096
7097 /// Bring this process's own view of the board up to an item it has just written.
7098 ///
7099 /// A created item goes to `created`, which is what completes a board read GitHub's own
7100 /// eventual consistency has left behind. An item that was already there is replaced
7101 /// where it sits, so a second write of it in the same command reads its real parent
7102 /// rather than the one it had before the first write.
7103 ///
7104 /// "Where it sits" is three places, and missing an earlier one leaves a stale record
7105 /// that wins: an item this same run created is held in `created` and not in the cached
7106 /// board, and `board` completes the cached board *from* `created`, so replacing only
7107 /// the cached copy of such an item replaces nothing and the read still reports the
7108 /// title it was created with. The search is the third, and it is the one an item the
7109 /// board's own projection is behind on sits in *alone* — which is exactly the item this
7110 /// source is least able to re-read, so leaving it out would put the stale title back on
7111 /// the only items the completion in [`Self::board`] exists for.
7112 fn remember_written(&self, item: Resolved, created: bool) -> Result<(), SourceError> {
7113 self.resolved_cache()?.insert(item.id.clone(), item.clone());
7114 if created {
7115 self.created()?.push(item);
7116 return Ok(());
7117 }
7118 {
7119 let mut own = self.created()?;
7120 if let Some(held) = own.iter_mut().find(|held| held.id == item.id) {
7121 *held = item;
7122 return Ok(());
7123 }
7124 }
7125 {
7126 let mut own = self.updated()?;
7127 match own.iter_mut().find(|held| held.id == item.id) {
7128 Some(held) => *held = item.clone(),
7129 None => own.push(item.clone()),
7130 }
7131 }
7132 if let Some(board) = self.board_cache()?.as_mut()
7133 && let Some(held) = board.items.iter_mut().find(|held| held.id == item.id)
7134 {
7135 *held = item.clone();
7136 }
7137 if let Some(found) = self.search_cache()?.as_mut()
7138 && let Some(held) = found.iter_mut().find(|held| held.id == item.id)
7139 {
7140 *held = item.clone();
7141 }
7142 for found in self.narrowed_cache()?.values_mut() {
7143 if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
7144 *held = item.clone();
7145 }
7146 }
7147 for (_, found) in self.children_cache()?.values_mut() {
7148 if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
7149 *held = item.clone();
7150 }
7151 }
7152 Ok(())
7153 }
7154
7155 /// Forget one item this process has just deleted, from every half of its own view.
7156 fn forget(&self, id: &NativeId) -> Result<(), SourceError> {
7157 if let Some(Deferred::Held { created, .. }) = self.deferred()?.as_mut() {
7158 created.retain(|held| held.content != *id);
7159 }
7160 self.resolved_cache()?.remove(id);
7161 self.read_ahead()?.remove(id);
7162 self.carried()?.remove(id);
7163 self.created()?.retain(|own| own.id != *id);
7164 self.updated()?.retain(|own| own.id != *id);
7165 self.commented()?.retain(|own| own != id);
7166 if let Some(board) = self.board_cache()?.as_mut() {
7167 board.items.retain(|item| item.id != *id);
7168 }
7169 if let Some(found) = self.search_cache()?.as_mut() {
7170 found.retain(|item| item.id != *id);
7171 }
7172 for found in self.narrowed_cache()?.values_mut() {
7173 found.retain(|item| item.id != *id);
7174 }
7175 for (_, found) in self.children_cache()?.values_mut() {
7176 found.retain(|item| item.id != *id);
7177 }
7178 Ok(())
7179 }
7180
7181 /// This process's own record of each narrowed answer, or the refusal a poisoned lock is.
7182 fn narrowed_cache(
7183 &self,
7184 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<String, Vec<Resolved>>>, SourceError> {
7185 self.narrowed_cache
7186 .lock()
7187 .map_err(|_| SourceError::Unavailable {
7188 message: "this source's view of a narrowed read was left inconsistent by an \
7189 earlier failure; next: run the command again"
7190 .into(),
7191 })
7192 }
7193
7194 /// This process's own record of each project's sub-issues, or the refusal a poisoned lock
7195 /// is.
7196 /// The tasks `project` holds, as a copy's batched read found them this command, when it did.
7197 fn held_task_children(&self, project: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
7198 Ok(self.task_children()?.get(project).cloned())
7199 }
7200
7201 fn task_children(
7202 &self,
7203 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Vec<Resolved>>>, SourceError> {
7204 self.task_children
7205 .lock()
7206 .map_err(|_| SourceError::Unavailable {
7207 message: "this source's record of a project's tasks was left inconsistent by an \
7208 earlier failure; next: run the command again"
7209 .into(),
7210 })
7211 }
7212
7213 fn children_cache(&self) -> Result<std::sync::MutexGuard<'_, ProjectChildren>, SourceError> {
7214 self.children_cache
7215 .lock()
7216 .map_err(|_| SourceError::Unavailable {
7217 message: "this source's view of a project's tasks was left inconsistent by an \
7218 earlier failure; next: run the command again"
7219 .into(),
7220 })
7221 }
7222
7223 /// Every page of the board, read from GitHub.
7224 async fn read_board(&self) -> Result<Board, SourceError> {
7225 let mut after: Option<String> = None;
7226 let mut items = Vec::new();
7227 let mut board;
7228 loop {
7229 let page = self.board_page(after.as_deref(), MAX_PAGE_SIZE).await?;
7230 for item in page
7231 .pointer("/items/nodes")
7232 .and_then(Value::as_array)
7233 .ok_or_else(|| SourceError::Malformed {
7234 message: "GitHub project items.nodes is not an array".into(),
7235 })?
7236 {
7237 if let Some(resolved) = self.resolve(item)? {
7238 items.push(resolved);
7239 }
7240 }
7241 let info = page
7242 .pointer("/items/pageInfo")
7243 .ok_or_else(|| SourceError::Malformed {
7244 message: "GitHub project items have no pageInfo".into(),
7245 })?;
7246 let has_next = required_bool(info, "hasNextPage")?;
7247 let next = has_next
7248 .then(|| required_str(info, "endCursor"))
7249 .transpose()?;
7250 board = page.clone();
7251 match next {
7252 Some(next) => {
7253 validate_cursor_progress(after.as_deref(), next)?;
7254 after = Some(next.to_owned());
7255 }
7256 None => break,
7257 }
7258 }
7259 Ok(Board {
7260 id: required_str(&board, "id")?.to_owned(),
7261 fields: board.get("fields").cloned().unwrap_or(Value::Null),
7262 items,
7263 })
7264 }
7265
7266 /// The existing items this source has written, for completing a narrowed read that is
7267 /// behind; see [`Self::updated`](GitHubProjectsSource::updated).
7268 fn updated(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
7269 self.updated.lock().map_err(|_| SourceError::Unavailable {
7270 message: "this source's record of what it wrote in this run was left inconsistent \
7271 by an earlier failure; next: run the command again"
7272 .into(),
7273 })
7274 }
7275
7276 /// The issues this source has commented on in this command; see
7277 /// [`Self::commented`](GitHubProjectsSource::commented).
7278 fn commented(&self) -> Result<std::sync::MutexGuard<'_, Vec<NativeId>>, SourceError> {
7279 self.commented.lock().map_err(|_| SourceError::Unavailable {
7280 message: "this source's record of what it commented on in this run was left \
7281 inconsistent by an earlier failure; next: run the command again"
7282 .into(),
7283 })
7284 }
7285
7286 /// Called only once GitHub has answered the comment write, so an issue whose comment
7287 /// failed is never made a candidate a later read would pay a node read for.
7288 fn remember_commented(&self, issue: &NativeId) -> Result<(), SourceError> {
7289 let mut commented = self.commented()?;
7290 if !commented.contains(issue) {
7291 commented.push(issue.clone());
7292 }
7293 Ok(())
7294 }
7295
7296 /// The items this source has created, for completing a board read that is behind.
7297 fn created(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
7298 self.created.lock().map_err(|_| SourceError::Unavailable {
7299 message: "this source's record of what it created in this run was left \
7300 inconsistent by an earlier failure; next: run the command again"
7301 .into(),
7302 })
7303 }
7304
7305 /// One board item as this source reports it, or `None` for content it ignores.
7306 ///
7307 /// A pull request is neither a project nor a task — it is somebody's change, not a
7308 /// unit of plan — and an item whose content the token cannot see has nothing to
7309 /// report at all.
7310 fn resolve(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
7311 let resolved = self.resolve_unremembered(item)?;
7312 if let Some(resolved) = &resolved {
7313 self.resolved_cache()?
7314 .insert(resolved.id.clone(), resolved.clone());
7315 }
7316 Ok(resolved)
7317 }
7318
7319 /// [`Self::resolve`], without remembering the item it resolves: what reading a token off a
7320 /// write's own response needs, which says nothing this process should answer a later read
7321 /// with.
7322 fn resolve_unremembered(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
7323 let content = item.get("content").ok_or_else(|| SourceError::Malformed {
7324 message: "GitHub project item is missing content".into(),
7325 })?;
7326 if content.is_null() {
7327 return Ok(None);
7328 }
7329 let content_kind = match required_str(content, "__typename")? {
7330 "Issue" => ContentKind::Issue,
7331 "DraftIssue" => ContentKind::DraftIssue,
7332 _ => return Ok(None),
7333 };
7334 let field_values = item
7335 .get("fieldValues")
7336 .ok_or_else(|| SourceError::Malformed {
7337 message: "GitHub project item is missing fieldValues".into(),
7338 })?;
7339 complete_connection(field_values, "project item field values", NESTED_PAGE_SIZE)?;
7340 let nodes = field_values
7341 .get("nodes")
7342 .and_then(Value::as_array)
7343 .ok_or_else(|| SourceError::Malformed {
7344 message: "GitHub project item fieldValues.nodes is not an array".into(),
7345 })?;
7346 if let Some(labels) = content.get("labels") {
7347 complete_connection(labels, "content labels", NESTED_PAGE_SIZE)?;
7348 }
7349 let raw_body = optional_str(content, "body")?.map(str::to_owned);
7350 let (body, slot) = metadata_body(raw_body.clone())?;
7351 let parent = optional_str(content.get("parent").unwrap_or(&Value::Null), "id")?
7352 .map(|id| NativeId(id.to_owned()));
7353 // A draft has no sub-issues to summarise, and GitHub's schema gives it no field
7354 // to read one from; it is a task, and never a project.
7355 let sub_issues = match content_kind {
7356 ContentKind::Issue => sub_issue_total(content)?,
7357 ContentKind::DraftIssue => 0,
7358 };
7359 let content_id = required_str(content, "id")?;
7360 let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
7361 message: format!("GitHub issue {content_id}: {message}"),
7362 })?;
7363 let raw_title = required_str(content, "title")?;
7364 let kind = BoardKind::of(raw_title, parent.is_some(), sub_issues, marked);
7365 // The title a person wrote, which for a document is the one without the prefix —
7366 // the same way `content` above is the body without this source's metadata slot.
7367 let title = match kind {
7368 BoardKind::Document => raw_title[DESIGN_TITLE_PREFIX.len()..].to_owned(),
7369 BoardKind::Work(_) => raw_title.to_owned(),
7370 };
7371 let own_repository = content
7372 .pointer("/repository/nameWithOwner")
7373 .and_then(Value::as_str)
7374 .map(|origin| Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST)))
7375 .transpose()
7376 .map_err(|message| SourceError::Malformed { message })?;
7377 let repositories = if slot.contains_key(Repository::METADATA_KEY) {
7378 Repository::from_metadata(&slot)
7379 .map_err(|message| SourceError::Malformed { message })?
7380 } else {
7381 own_repository.clone().into_iter().collect()
7382 };
7383 let classification = Classification::from_metadata(&slot)
7384 .map_err(|message| SourceError::Malformed { message })?;
7385 let id = NativeId(content_id.to_owned());
7386 // Read only for a task, because only a task has either list: a project or a
7387 // document holding one of these keys holds nothing this source reports, and the
7388 // keys are left out of its caller-visible metadata all the same.
7389 let (delivers, delivered_by) = if kind == BoardKind::Work(ItemKind::Task) {
7390 let listed = |key: &str| {
7391 TaskRef::from_value(key, &id, Some(&self.name), slot.get(key))
7392 .map_err(|message| SourceError::Malformed { message })
7393 };
7394 (
7395 listed(TaskRef::DELIVERS_KEY)?,
7396 listed(TaskRef::DELIVERED_BY_KEY)?,
7397 )
7398 } else {
7399 (Vec::new(), Vec::new())
7400 };
7401 let (option, closed, reason) = Self::status_parts(nodes, content)?;
7402 let priority = self.held_priority(nodes)?;
7403 let item_labels = labels(content)?;
7404 // Present when the item was reached through its own issue, whose board entry
7405 // names the board; a read of the board's own items has the board already. An
7406 // empty id names nothing a field write could address, so it is read as absent and
7407 // the write goes back to reading the board.
7408 let board_id = item
7409 .pointer("/project/id")
7410 .and_then(Value::as_str)
7411 .filter(|id| !id.is_empty());
7412 let resolved = Resolved {
7413 item_id: required_str(item, "id")?.to_owned(),
7414 id,
7415 content_kind,
7416 kind,
7417 title,
7418 body: body.filter(|value| !value.is_empty()),
7419 raw_body,
7420 status: self
7421 .statuses
7422 .status(kind.status_kind(), option, closed, reason),
7423 option: option.map(str::to_owned),
7424 priority,
7425 closed,
7426 reason: closed.then(|| reason.map(str::to_owned)).flatten(),
7427 delivers,
7428 delivered_by,
7429 labels: item_labels.clone(),
7430 parent,
7431 origin: text_field(nodes, ORIGIN_FIELD)?.filter(|value| !value.is_empty()),
7432 projected: self.held_projections(nodes)?,
7433 number: match content_kind {
7434 ContentKind::Issue => Some(issue_number(content)?),
7435 // A draft is filed in no repository, so nothing ever numbered it:
7436 // `DraftIssue` declares no `number` at all, exactly as it declares no
7437 // `subIssuesSummary` the branch above reads.
7438 ContentKind::DraftIssue => None,
7439 },
7440 url: optional_str(content, "url")?.map(str::to_owned),
7441 created_at: optional_time(content, "createdAt")?,
7442 updated_at: optional_time(content, "updatedAt")?,
7443 item_updated_at: optional_time(item, "updatedAt")?,
7444 comment_count: content
7445 .pointer("/commentCount/totalCount")
7446 .and_then(Value::as_u64),
7447 relationship_events: content
7448 .pointer("/relationshipEvents/totalCount")
7449 .and_then(Value::as_u64),
7450 // The issue node is stamped wherever it was read; a board item a read of the issue
7451 // put together from its memberships is not.
7452 served_at: match optional_time(content, SERVED_AT)? {
7453 Some(served) => Some(served),
7454 None => optional_time(item, SERVED_AT)?,
7455 },
7456 // Both connections were read to their end above — one reporting a further page is
7457 // refused — so these are every field value and every label the item holds. A
7458 // response that did not select the labels at all says nothing of them, and gives
7459 // no facets.
7460 facets: content.get("labels").is_some().then(|| {
7461 change_token::Facets::new(kind.facet(), facet_fields(nodes), &item_labels).digest()
7462 }),
7463 own_repository,
7464 repositories,
7465 classification,
7466 slot,
7467 board_id: board_id.map(str::to_owned),
7468 fields: field_definitions(nodes),
7469 board_fields: Self::carried_board_fields(content, board_id)?,
7470 blocked_by: carried_blocked_by(content)?,
7471 };
7472 Ok(Some(resolved))
7473 }
7474
7475 /// The field definitions of the board `board_id` names — the project this issue's own
7476 /// board item is on — off the `boards` page a read of an issue by its own id carries, or
7477 /// `None` when the read carried none, carried no entry for that board, or the board item
7478 /// named no board, which a write then answers by reading the board's fields itself.
7479 ///
7480 /// Matched by the board's node id and never by its number alone: a project number is
7481 /// unique only within its owner, so another owner's board numbered alike can sit on the
7482 /// same page, and its field and option ids address nothing on this one.
7483 fn carried_board_fields(
7484 content: &Value,
7485 board_id: Option<&str>,
7486 ) -> Result<Option<Value>, SourceError> {
7487 let (Some(nodes), Some(board_id)) = (
7488 content.pointer("/boards/nodes").and_then(Value::as_array),
7489 board_id,
7490 ) else {
7491 return Ok(None);
7492 };
7493 let Some(board) = nodes.iter().find_map(|node| {
7494 let project = node.get("project")?;
7495 (project.get("id").and_then(Value::as_str) == Some(board_id)).then_some(project)
7496 }) else {
7497 return Ok(None);
7498 };
7499 let Some(fields) = board.get("fields").filter(|fields| !fields.is_null()) else {
7500 return Ok(None);
7501 };
7502 complete_connection(fields, "board fields", NESTED_PAGE_SIZE)?;
7503 Ok(Some(fields.clone()))
7504 }
7505
7506 /// What one board item's `Priority` field says, through this instance's mapping.
7507 ///
7508 /// An instance with no mapping holds no priority, so every item reads as `none` whatever
7509 /// its board holds. With one, no value is `none`, a mapped option is its level, and an
7510 /// option the mapping does not name is kept as itself — never read as a level or as
7511 /// `none` — for a read of the task to report by name.
7512 fn held_priority(&self, field_values: &[Value]) -> Result<HeldPriority, SourceError> {
7513 let Some(mapping) = &self.priorities else {
7514 return Ok(HeldPriority::Read(Priority::None));
7515 };
7516 // A value of the field that names no option — a text field someone called `Priority` —
7517 // is malformed rather than `none`: reading it as no priority would let the next copy
7518 // clear one a person set.
7519 let Some(option) = field_values
7520 .iter()
7521 .find(|value| {
7522 value.pointer("/field/name").and_then(Value::as_str) == Some(PRIORITY_FIELD)
7523 })
7524 .map(|value| required_str(value, "name"))
7525 .transpose()?
7526 else {
7527 return Ok(HeldPriority::Read(Priority::None));
7528 };
7529 Ok(mapping.priority_of(option).map_or_else(
7530 || HeldPriority::Unmapped(option.to_owned()),
7531 HeldPriority::Read,
7532 ))
7533 }
7534
7535 /// What each projected metadata field holds for one board item, by field name — every text
7536 /// value GitHub answers, an empty one included.
7537 fn held_projections(
7538 &self,
7539 field_values: &[Value],
7540 ) -> Result<BTreeMap<String, String>, SourceError> {
7541 let mut held = BTreeMap::new();
7542 for projection in &self.metadata_fields {
7543 if let Some(text) = text_field(field_values, &projection.field)? {
7544 held.insert(projection.field.to_string(), text);
7545 }
7546 }
7547 Ok(held)
7548 }
7549
7550 /// What one board item's status is read from: its `Status` option, whether its issue
7551 /// is closed, and the reason it was closed with. [`BoardStatuses::status`] turns the
7552 /// three into the status it reports.
7553 fn status_parts<'a>(
7554 field_values: &'a [Value],
7555 content: &'a Value,
7556 ) -> Result<(Option<&'a str>, bool, Option<&'a str>), SourceError> {
7557 let option = field_values
7558 .iter()
7559 .find(|value| value.pointer("/field/name").and_then(Value::as_str) == Some("Status"))
7560 .map(|value| required_str(value, "name"))
7561 .transpose()?;
7562 let closed = optional_str(content, "state")? == Some("CLOSED");
7563 Ok((option, closed, optional_str(content, "stateReason")?))
7564 }
7565
7566 /// The board Status option this write selects, or the refusal that says why not.
7567 ///
7568 /// The mapped option is required for both open and terminal targets. A terminal write
7569 /// validates it before changing either representation, so it can never fall back to
7570 /// closing an issue whose board cannot display the matching status.
7571 ///
7572 /// Answers the field's id, the option's id, and the option's name as the board spells
7573 /// it — which is the name a read of the item reports once it sits there.
7574 fn column_for(
7575 &self,
7576 fields: &Value,
7577 kind: ItemKind,
7578 category: StatusCategory,
7579 target: &StatusTarget,
7580 ) -> Result<Option<(String, String, String)>, SourceError> {
7581 let Some(wanted) = target.option() else {
7582 return Ok(None);
7583 };
7584 let missing = |detail: &str| SourceError::Refused {
7585 message: format!(
7586 "{} status {} of source {} needs the board Status option {wanted:?}, and \
7587 {detail}; next: add that option to the board, which `onetaskgraph sources \
7588 fields {} --apply` does, or point status_mapping.{}.{} of this source at one \
7589 it has",
7590 kind.marker(),
7591 category_name(category),
7592 self.name,
7593 self.name,
7594 category_name(category),
7595 kind.marker()
7596 ),
7597 };
7598 let Some(field) = Board::field(fields, "Status")? else {
7599 return Err(missing("this board has no Status field"));
7600 };
7601 if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
7602 return Err(missing(
7603 "this board's Status field is not a single-select field",
7604 ));
7605 }
7606 let option = field
7607 .get("options")
7608 .and_then(Value::as_array)
7609 .and_then(|options| {
7610 options.iter().find(|option| {
7611 option
7612 .get("name")
7613 .and_then(Value::as_str)
7614 .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
7615 })
7616 });
7617 match option {
7618 None => Err(missing("this board does not have it")),
7619 Some(option) => Ok(Some((
7620 required_str(field, "id")?.to_owned(),
7621 required_str(option, "id")?.to_owned(),
7622 required_str(option, "name")?.to_owned(),
7623 ))),
7624 }
7625 }
7626
7627 /// The refusal a status that closes an issue is answered with over a board draft.
7628 fn closes_a_draft(&self, category: StatusCategory) -> SourceError {
7629 SourceError::Refused {
7630 message: format!(
7631 "status {} of source {} closes the item's issue, and GitHub draft items have \
7632 no open or closed state",
7633 category_name(category),
7634 self.name
7635 ),
7636 }
7637 }
7638
7639 /// What a status write to one item needs of the board: the board's id and the
7640 /// definition of its `Status` field, read off the item when the item says both.
7641 ///
7642 /// The same reasoning as [`Self::fields_for`]: a node read of the item names its board,
7643 /// and its `Status` value carries that field's definition, options and all. An item that
7644 /// does not say — no board id, or no `Status` value to read the field off — takes them
7645 /// from [`Self::board_fields`], which reads no item.
7646 async fn status_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
7647 if let Some(board) = item.carried_board() {
7648 return Ok(board);
7649 }
7650 if item.defines("Status")
7651 && let Some(board_id) = item.named_board()
7652 {
7653 return Ok(BoardFields {
7654 id: board_id,
7655 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
7656 });
7657 }
7658 self.board_fields().await
7659 }
7660
7661 /// Set one task's or one project's status and nothing else; see
7662 /// [`TaskSource::set_task_status`] and [`TaskSource::set_project_status`].
7663 async fn set_status(
7664 &self,
7665 id: &NativeId,
7666 kind: ItemKind,
7667 category: StatusCategory,
7668 ) -> Result<Option<Status>, SourceError> {
7669 // Refused before anything is read, in the words a write of the same status is.
7670 let target = self.resolved_target(kind, category)?;
7671 // An item read by issue reference carries only what this write needs, so what it
7672 // becomes is never remembered for a later read to be answered from.
7673 let held = self.take_held(id)?;
7674 let by_reference = held.is_some();
7675 let bound = match held {
7676 Some(item) => Some(item),
7677 None => self.bound_item(id).await?,
7678 };
7679 let Some(mut item) = bound.filter(|item| item.kind == BoardKind::Work(kind)) else {
7680 return Ok(None);
7681 };
7682 let board = self.status_board(&item).await?;
7683 let (field, option, name) = self
7684 .column_for(&board.fields, kind, category, &target)?
7685 .ok_or_else(|| SourceError::Malformed {
7686 message: format!(
7687 "status {} of source {} names no board Status option",
7688 category_name(category),
7689 self.name
7690 ),
7691 })?;
7692 if item.status.category == category && item.option.as_deref() == Some(&name) {
7693 return Ok(Some(item.status));
7694 }
7695 match &target {
7696 StatusTarget::Terminal(_, reason) => {
7697 if item.content_kind == ContentKind::DraftIssue {
7698 return Err(self.closes_a_draft(category));
7699 }
7700 // The option first, so a refused selection leaves an open issue open rather
7701 // than closed behind a card that still reads open.
7702 self.set_item_field(
7703 board.id.as_str(),
7704 &item.item_id,
7705 &field,
7706 json!({"singleSelectOptionId": option}),
7707 )
7708 .await?;
7709 // An issue already closed in a way that reads as this category keeps its state
7710 // and its reason: only its card was out of line, and closing it again would
7711 // overwrite a reason — a duplicate's — that says more than this write does.
7712 let already = item.closed && item.status.category == category;
7713 if !already {
7714 self.update_content(
7715 ContentKind::Issue,
7716 &item.id,
7717 json!({"stateInput": state_input(Some(&target))}),
7718 )
7719 .await?;
7720 }
7721 let reason = if already {
7722 item.reason.clone()
7723 } else {
7724 Some(reason.reason().to_owned())
7725 };
7726 item.closed = true;
7727 item.status = self
7728 .statuses
7729 .status(kind, Some(&name), true, reason.as_deref());
7730 item.option = Some(name);
7731 item.reason = reason;
7732 }
7733 StatusTarget::Column(_) => {
7734 // An option is what an open item's status is, so a closed issue is reopened
7735 // first — sitting closed in the column, it would read back as closed. A draft has
7736 // no state to reopen.
7737 if item.content_kind == ContentKind::Issue && item.closed {
7738 self.update_content(
7739 ContentKind::Issue,
7740 &item.id,
7741 json!({"stateInput": state_input(Some(&target))}),
7742 )
7743 .await?;
7744 item.closed = false;
7745 }
7746 self.set_item_field(
7747 board.id.as_str(),
7748 &item.item_id,
7749 &field,
7750 json!({"singleSelectOptionId": option}),
7751 )
7752 .await?;
7753 item.status = self.statuses.status(kind, Some(&name), false, None);
7754 item.option = Some(name);
7755 }
7756 StatusTarget::Disabled(_) => {
7757 unreachable!("resolved_target refused a disabled status")
7758 }
7759 }
7760 let status = item.status.clone();
7761 if !by_reference {
7762 self.remember_written(item, false)?;
7763 }
7764 Ok(Some(status))
7765 }
7766
7767 /// Replace one task's `delivered_by` and nothing else; see
7768 /// [`TaskSource::set_delivered_by`].
7769 ///
7770 /// One update of the body, which differs from the body GitHub holds only inside the
7771 /// metadata slot — see [`with_slot`]. A body that would not change is not sent at all.
7772 async fn replace_delivered_by(
7773 &self,
7774 id: &NativeId,
7775 delivered_by: &[TaskRef],
7776 ) -> Result<Option<()>, SourceError> {
7777 let entries = TaskRef::listed(
7778 TaskRef::DELIVERED_BY_KEY,
7779 id,
7780 Some(&self.name),
7781 delivered_by.to_vec(),
7782 )
7783 .map_err(|message| SourceError::Refused { message })?;
7784 let Some(mut item) = self
7785 .bound_item(id)
7786 .await?
7787 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
7788 else {
7789 return Ok(None);
7790 };
7791 let mut slot = item.slot.clone();
7792 set_task_list(&mut slot, TaskRef::DELIVERED_BY_KEY, &entries);
7793 self.write_slot(&mut item, &slot).await?;
7794 item.delivered_by = entries;
7795 self.remember_written(item, false)?;
7796 Ok(Some(()))
7797 }
7798
7799 /// Set one caller key of the metadata slot of one issue of `kind`, and nothing else;
7800 /// see [`TaskSource::set_task_metadata`].
7801 ///
7802 /// `None` when this board holds no item by that id, or holds one of another kind. The
7803 /// answer is the item as this source now reads it, so what a caller is told the key
7804 /// holds is what the slot holds.
7805 ///
7806 /// A key already holding the value is answered without a write, compared as JSON rather
7807 /// than as the body's bytes: a slot a person spelled with other whitespace would
7808 /// otherwise be re-encoded, which is a write that changes nothing the caller asked for.
7809 async fn set_slot_key(
7810 &self,
7811 id: &NativeId,
7812 kind: BoardKind,
7813 key: &MetadataKey,
7814 value: &Value,
7815 ) -> Result<Option<Resolved>, SourceError> {
7816 let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
7817 return Ok(None);
7818 };
7819 if item.slot.get(key.as_str()) == Some(value) {
7820 return Ok(Some(item));
7821 }
7822 let mut slot = item.slot.clone();
7823 slot.insert(key.as_str().to_owned(), value.clone());
7824 // A key a board text field projects moves that field first, so the body — the value's
7825 // one authoritative home — is written last, as every write of an existing item is.
7826 if self
7827 .metadata_fields
7828 .iter()
7829 .any(|projection| &*projection.key == key.as_str())
7830 {
7831 let board = self.projection_board(&item).await?;
7832 let (writes, projected) = self.projection_writes(
7833 &board.fields,
7834 Some(&item.projected),
7835 &slot,
7836 Some(&[key.as_str()]),
7837 )?;
7838 let (mut values, mut clears) = (Vec::new(), Vec::new());
7839 for write in &writes {
7840 write.join(&mut values, &mut clears);
7841 }
7842 self.set_item_fields(board.id.as_str(), &item.item_id, &values, &clears)
7843 .await?;
7844 item.projected = projected;
7845 }
7846 self.write_slot(&mut item, &slot).await?;
7847 self.remember_written(item.clone(), false)?;
7848 Ok(Some(item))
7849 }
7850
7851 /// What a projection write to `item` needs of the board, read off the item when it says
7852 /// enough, as [`Self::fields_for`] reads it.
7853 async fn projection_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
7854 if let Some(board) = item.carried_board() {
7855 return Ok(board);
7856 }
7857 if let Some(board_id) = item.named_board()
7858 && self
7859 .metadata_fields
7860 .iter()
7861 .all(|projection| item.defines(&projection.field))
7862 {
7863 return Ok(BoardFields {
7864 id: board_id,
7865 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
7866 });
7867 }
7868 self.board_fields().await
7869 }
7870
7871 /// Put `slot` in one item's metadata slot with a single update of its body, and bring
7872 /// `item` up to what that write left.
7873 ///
7874 /// The body sent differs from the body GitHub holds only inside the slot — see
7875 /// [`with_slot`] — and a body that would not change is not sent at all. It goes through
7876 /// the mutation the item's content takes, so a board draft's body is written with
7877 /// `updateProjectV2DraftIssue` exactly as an issue's is with `updateIssue`.
7878 async fn write_slot(
7879 &self,
7880 item: &mut Resolved,
7881 slot: &BTreeMap<String, Value>,
7882 ) -> Result<(), SourceError> {
7883 let held = item.raw_body.clone().unwrap_or_default();
7884 let body = with_slot(&held, slot)?;
7885 if body != held {
7886 self.update_content(item.content_kind, &item.id, json!({"body": body}))
7887 .await?;
7888 }
7889 let (visible, slot) = metadata_body(Some(body.clone()))?;
7890 item.body = visible.filter(|value| !value.is_empty());
7891 item.raw_body = Some(body);
7892 item.slot = slot;
7893 Ok(())
7894 }
7895
7896 /// This instance's target for a category written to an item of `kind`, refusing one
7897 /// that kind has no option for — before anything is read or written.
7898 ///
7899 /// Nothing here mutates the board's option set to make room for a status. GitHub
7900 /// documents `UpdateProjectV2FieldInput.singleSelectOptions` as *"provided values
7901 /// overwrite existing options"*, so no addition is additive and a mistake destroys the
7902 /// field and every item's status.
7903 fn resolved_target(
7904 &self,
7905 kind: ItemKind,
7906 category: StatusCategory,
7907 ) -> Result<StatusTarget, SourceError> {
7908 let target = self.statuses.target(kind, category).clone();
7909 let StatusTarget::Disabled(why) = target else {
7910 return Ok(target);
7911 };
7912 let refusal = why.refusal(&self.name, category, kind);
7913 // Why there is no shipped default, which is the question a person meeting this
7914 // refusal on a source that never mentioned the category asks.
7915 let shipped_none = match category {
7916 StatusCategory::Draft => Some(
7917 "draft has no shipped default because GitHub draft issues cannot have \
7918 sub-issues, and this source stores a project's tasks as its issue's sub-issues",
7919 ),
7920 StatusCategory::Unknown => Some(
7921 "unknown has no shipped default because this board keeps no open-ended status \
7922 word: every word classified unknown is written to the one board Status option \
7923 status_mapping.unknown names",
7924 ),
7925 _ => None,
7926 };
7927 Err(match (refusal, shipped_none, why) {
7928 (SourceError::Refused { message }, Some(note), UnmappedStatus::Unconfigured) => {
7929 SourceError::Refused {
7930 message: format!("{message}; {note}"),
7931 }
7932 }
7933 (refusal, _, _) => refusal,
7934 })
7935 }
7936
7937 /// What writing `priority` does to one item's `Priority` field on this board, or the
7938 /// refusal naming what the board lacks.
7939 ///
7940 /// `none` is no value, so it clears the field — and asks nothing of an item that holds
7941 /// none already, or of an item not created yet. Every other priority selects the option
7942 /// the mapping names, matched case-insensitively; a board with no `Priority` field, or
7943 /// without that option, is refused rather than given one: reads and writes never create
7944 /// a field or an option.
7945 fn priority_write(
7946 &self,
7947 fields: &Value,
7948 existing: Option<&Resolved>,
7949 priority: Priority,
7950 ) -> Result<Option<PriorityWrite>, SourceError> {
7951 let Some(mapping) = &self.priorities else {
7952 return Err(self.holds_no_priority());
7953 };
7954 let Some(wanted) = mapping.option(priority) else {
7955 if !existing.is_some_and(Resolved::holds_priority) {
7956 return Ok(None);
7957 }
7958 let field =
7959 Board::field(fields, PRIORITY_FIELD)?.ok_or_else(|| SourceError::Malformed {
7960 message: format!(
7961 "an item holding a {PRIORITY_FIELD} value was read without that field"
7962 ),
7963 })?;
7964 return Ok(Some(PriorityWrite::Clear {
7965 field: required_str(field, "id")?.to_owned(),
7966 }));
7967 };
7968 let missing = |detail: &str| SourceError::Refused {
7969 message: format!(
7970 "priority {priority} of source {} needs the board {PRIORITY_FIELD} option \
7971 {wanted:?}, and {detail}; run `onetaskgraph sources fields {} --apply` to add \
7972 it, or point priority_mapping.{priority} of this source at an option the board \
7973 has",
7974 self.name, self.name
7975 ),
7976 };
7977 let Some(field) = Board::field(fields, PRIORITY_FIELD)? else {
7978 return Err(missing(&format!(
7979 "this board has no {PRIORITY_FIELD} field"
7980 )));
7981 };
7982 if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
7983 return Err(missing(&format!(
7984 "this board's {PRIORITY_FIELD} field is not a single-select field"
7985 )));
7986 }
7987 // An options list that is absent or not a list is an answer this source cannot read,
7988 // not a board lacking the option: `sources fields --apply` is no remedy for it.
7989 let option = field
7990 .get("options")
7991 .and_then(Value::as_array)
7992 .ok_or_else(|| SourceError::Malformed {
7993 message: format!("GitHub {PRIORITY_FIELD} field options is not an array"),
7994 })?
7995 .iter()
7996 .find(|option| {
7997 option
7998 .get("name")
7999 .and_then(Value::as_str)
8000 .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
8001 })
8002 .ok_or_else(|| missing("this board does not have it"))?;
8003 Ok(Some(PriorityWrite::Select {
8004 field: required_str(field, "id")?.to_owned(),
8005 option: required_str(option, "id")?.to_owned(),
8006 }))
8007 }
8008
8009 /// What writing `metadata` does to each projected text field of one item, and what those
8010 /// fields hold after it — or the refusal, before anything is sent.
8011 ///
8012 /// `held` is what the item's fields hold now, `None` for an item not created yet. `keys`
8013 /// narrows the work to the entries projecting one of those metadata keys, for a write that
8014 /// touches only them; `None` is every entry. A string the field already holds, and nothing
8015 /// for a field holding nothing, sends nothing. A value no text field can hold is refused,
8016 /// and so is a board without the field, or with a field of that name that is not a text
8017 /// field: reads and writes never create a field.
8018 fn projection_writes(
8019 &self,
8020 fields: &Value,
8021 held: Option<&BTreeMap<String, String>>,
8022 metadata: &BTreeMap<String, Value>,
8023 keys: Option<&[&str]>,
8024 ) -> Result<(Vec<ProjectionWrite>, BTreeMap<String, String>), SourceError> {
8025 let mut projected = held.cloned().unwrap_or_default();
8026 let mut writes = Vec::new();
8027 for projection in self
8028 .metadata_fields
8029 .iter()
8030 .filter(|projection| keys.is_none_or(|keys| keys.contains(&&*projection.key)))
8031 {
8032 let wanted = projection.projected(metadata, &self.name)?;
8033 let field = self.projection_field(fields, projection)?;
8034 let holding = projected.get(&*projection.field);
8035 match wanted {
8036 Projected::Text(text) if holding != Some(&text) => {
8037 writes.push(ProjectionWrite::Set {
8038 field,
8039 text: text.clone(),
8040 });
8041 projected.insert(projection.field.to_string(), text);
8042 }
8043 Projected::Nothing if holding.is_some() => {
8044 writes.push(ProjectionWrite::Clear { field });
8045 projected.remove(&*projection.field);
8046 }
8047 Projected::Text(_) | Projected::Nothing => {}
8048 }
8049 }
8050 Ok((writes, projected))
8051 }
8052
8053 /// The id of the board text field one projection writes, or the refusal naming what the
8054 /// board has instead.
8055 fn projection_field(
8056 &self,
8057 fields: &Value,
8058 projection: &MetadataField,
8059 ) -> Result<String, SourceError> {
8060 let refuse = |detail: String| SourceError::Refused {
8061 message: format!(
8062 "source {} projects metadata key {:?} onto the board text field {:?}, and \
8063 {detail}; next: run `onetaskgraph sources fields {} --apply` to create it as a \
8064 text field, or name another field in metadata_fields",
8065 self.name, &*projection.key, &*projection.field, self.name
8066 ),
8067 };
8068 let Some(field) = Board::field(fields, &projection.field)? else {
8069 return Err(refuse("this board has no field of that name".to_owned()));
8070 };
8071 let typename = optional_str(field, "__typename")?.unwrap_or("field of another type");
8072 let data_type = optional_str(field, "dataType")?;
8073 if typename != "ProjectV2Field" || data_type != Some("TEXT") {
8074 return Err(refuse(format!(
8075 "this board's field of that name is not a text field (it is a {}), so it is \
8076 left as it is; rename or remove that field first",
8077 data_type.unwrap_or(typename)
8078 )));
8079 }
8080 Ok(required_str(field, "id")?.to_owned())
8081 }
8082
8083 /// Apply one priority write to one board item.
8084 async fn write_priority(
8085 &self,
8086 board_id: &str,
8087 item_id: &str,
8088 write: &PriorityWrite,
8089 ) -> Result<(), SourceError> {
8090 match write {
8091 PriorityWrite::Select { field, option } => {
8092 self.set_item_field(
8093 board_id,
8094 item_id,
8095 field,
8096 json!({"singleSelectOptionId": option}),
8097 )
8098 .await
8099 }
8100 PriorityWrite::Clear { field } => {
8101 let data = self
8102 .graphql(
8103 graphql::CLEAR_FIELD,
8104 json!({"input":{"projectId":board_id,"itemId":item_id,"fieldId":field},
8105 "readPriority":false,"priorityName":PRIORITY_FIELD}),
8106 )
8107 .await?;
8108 let returned = data
8109 .pointer("/clearProjectV2ItemFieldValue/projectV2Item")
8110 .ok_or_else(|| SourceError::Malformed {
8111 message: "GitHub field clear returned no project item".into(),
8112 })?;
8113 if required_str(returned, "id")? != item_id {
8114 return Err(SourceError::Malformed {
8115 message: "GitHub field clear returned the wrong project item".into(),
8116 });
8117 }
8118 Ok(())
8119 }
8120 }
8121 }
8122
8123 /// The refusal a priority is answered with by an instance configured with no
8124 /// `priority_mapping`, which holds none.
8125 fn holds_no_priority(&self) -> SourceError {
8126 SourceError::Refused {
8127 message: format!(
8128 "source {} holds no task priority: its configuration sets no priority_mapping; \
8129 next: set priority_mapping on this source, then run `onetaskgraph sources \
8130 fields {} --apply` to set its board up",
8131 self.name, self.name
8132 ),
8133 }
8134 }
8135
8136 /// Set one task's priority and nothing else; see [`TaskSource::set_task_priority`].
8137 ///
8138 /// One field write — a select, or a clear for `none` — and no title, body, label, state
8139 /// or `Status` request. Clearing a priority an item does not hold sends nothing.
8140 async fn set_priority(
8141 &self,
8142 id: &NativeId,
8143 priority: Priority,
8144 ) -> Result<Option<Priority>, SourceError> {
8145 if self.priorities.is_none() {
8146 return Err(self.holds_no_priority());
8147 }
8148 let Some(mut item) = self
8149 .bound_item(id)
8150 .await?
8151 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8152 else {
8153 return Ok(None);
8154 };
8155 if priority == Priority::None && !item.holds_priority() {
8156 return Ok(Some(priority));
8157 }
8158 // The item's own read carries the field's definition whenever it holds a value of
8159 // it, which a clear always does; a select onto an item holding none reads the board.
8160 let board = match (item.carried_board(), item.named_board()) {
8161 (Some(board), _) => board,
8162 (None, Some(id)) if item.defines(PRIORITY_FIELD) => BoardFields {
8163 id,
8164 fields: json!({"nodes": item.fields.clone(), "pageInfo": {"hasNextPage": false}}),
8165 },
8166 _ => self.board_fields().await?,
8167 };
8168 let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? else {
8169 return Ok(Some(priority));
8170 };
8171 let (document, root, input) = match write {
8172 PriorityWrite::Select { field, option } => (
8173 graphql::UPDATE_FIELD,
8174 "updateProjectV2ItemFieldValue",
8175 json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field,"value":{"singleSelectOptionId":option}}),
8176 ),
8177 PriorityWrite::Clear { field } => (
8178 graphql::CLEAR_FIELD,
8179 "clearProjectV2ItemFieldValue",
8180 json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field}),
8181 ),
8182 };
8183 let data = self
8184 .graphql(
8185 document,
8186 json!({"input":input,"readPriority":true,"priorityName":PRIORITY_FIELD}),
8187 )
8188 .await?;
8189 let returned = data
8190 .get(root)
8191 .and_then(|value| value.get("projectV2Item"))
8192 .ok_or_else(|| SourceError::Malformed {
8193 message: "GitHub priority write returned no project item".into(),
8194 })?;
8195 if required_str(returned, "id")? != item.item_id {
8196 return Err(SourceError::Malformed {
8197 message: "GitHub priority write returned the wrong project item".into(),
8198 });
8199 }
8200 let value = returned
8201 .get("fieldValueByName")
8202 .ok_or_else(|| SourceError::Malformed {
8203 message: "GitHub priority write returned no priority read-back".into(),
8204 })?;
8205 if !value.is_null()
8206 && value.pointer("/field/name").and_then(Value::as_str) != Some(PRIORITY_FIELD)
8207 {
8208 return Err(SourceError::Malformed {
8209 message: "GitHub priority read-back is not a Priority field value".into(),
8210 });
8211 }
8212 let values = if value.is_null() {
8213 Vec::new()
8214 } else {
8215 vec![value.clone()]
8216 };
8217 item.priority = self.held_priority(&values)?;
8218 let answer = item.task()?.priority;
8219 self.remember_written(item, false)?;
8220 Ok(Some(answer))
8221 }
8222
8223 /// Replace one task's visible body and nothing else; see
8224 /// [`TaskSource::set_task_content`].
8225 ///
8226 /// One update of the body, which differs from the body GitHub holds only outside the
8227 /// metadata slot — the slot is kept byte for byte, so every caller key and every list
8228 /// this source keeps there reads back as it was. A body that would not change is not
8229 /// sent at all.
8230 async fn replace_content(
8231 &self,
8232 id: &NativeId,
8233 content: &str,
8234 ) -> Result<Option<()>, SourceError> {
8235 let Some(mut item) = self
8236 .bound_item(id)
8237 .await?
8238 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8239 else {
8240 return Ok(None);
8241 };
8242 let held = item.raw_body.clone().unwrap_or_default();
8243 let body = with_content(&held, content)?;
8244 // Checked before anything is sent: content ending in what this source reads as its own
8245 // metadata slot would read back as metadata rather than as the content it was.
8246 let (visible, slot) = metadata_body(Some(body.clone()))?;
8247 if visible.as_deref().unwrap_or_default() != content || slot != item.slot {
8248 return Err(SourceError::Refused {
8249 message: format!(
8250 "this content ends in what source {} reads as its own metadata slot \
8251 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8252 as content; next: remove that trailing block from the content",
8253 self.name
8254 ),
8255 });
8256 }
8257 if body != held {
8258 self.update_content(item.content_kind, &item.id, json!({"body": body}))
8259 .await?;
8260 }
8261 item.body = visible.filter(|value| !value.is_empty());
8262 item.raw_body = Some(body);
8263 item.slot = slot;
8264 self.remember_written(item, false)?;
8265 Ok(Some(()))
8266 }
8267
8268 /// Apply one targeted update to one task; see [`TaskSource::update_task`].
8269 ///
8270 /// One read of the item — which carries the board's field definitions and the issue's
8271 /// `blockedBy`, so neither is read again — and then only what differs from it: the
8272 /// `Status` option and the `Priority` field together in one request, the `blockedBy`
8273 /// additions and removals the named edges differ by, and last one `updateIssue` carrying
8274 /// the title, the body — visible content and metadata slot together — and a state change.
8275 /// So an update naming any of title, body, metadata, status and priority is one read and
8276 /// at most two writes. The body goes last so that a write refused part-way leaves it, and
8277 /// the metadata in it, as it stood. A terminal status selects its option and then closes,
8278 /// as a whole write does; an open one selects its option and then reopens. The origin
8279 /// field is never written: an update is of an item that already exists, whose origin is
8280 /// what it is.
8281 ///
8282 /// The task answered is the item as those writes left it, built from the read and what was
8283 /// sent rather than read again — the same record a later read in this run answers from.
8284 async fn targeted_update(
8285 &self,
8286 id: &NativeId,
8287 update: &TaskUpdate,
8288 ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
8289 // Everything this source can refuse without reading the item is refused first, in the
8290 // words a whole write of the same fields is refused with.
8291 update.consistent()?;
8292 if update
8293 .title
8294 .as_deref()
8295 .is_some_and(|title| title.starts_with(DESIGN_TITLE_PREFIX))
8296 {
8297 return Err(SourceError::Refused {
8298 message: format!(
8299 "the title of this task begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
8300 spells a document, so it would read back as one rather than as a task; \
8301 retitle it",
8302 self.name
8303 ),
8304 });
8305 }
8306 if let Some(delivers) = &update.delivers {
8307 TaskRef::listed(
8308 TaskRef::DELIVERS_KEY,
8309 id,
8310 Some(&self.name),
8311 delivers.clone(),
8312 )
8313 .map_err(|message| SourceError::Refused { message })?;
8314 }
8315 if self.priorities.is_none()
8316 && update
8317 .priority
8318 .is_some_and(|priority| priority != Priority::None)
8319 {
8320 return Err(self.holds_no_priority());
8321 }
8322 let target = update
8323 .status
8324 .as_ref()
8325 .map(|status| self.resolved_target(ItemKind::Task, status.category))
8326 .transpose()?;
8327 let Some(mut item) = self
8328 .bound_item(id)
8329 .await?
8330 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8331 else {
8332 return Ok(None);
8333 };
8334 let before = item.task()?;
8335
8336 let mut status_move = None;
8337 if let (Some(status), Some(target)) = (&update.status, target) {
8338 let board = self.status_board(&item).await?;
8339 let (field, option, name) = self
8340 .column_for(&board.fields, ItemKind::Task, status.category, &target)?
8341 .ok_or_else(|| SourceError::Malformed {
8342 message: format!(
8343 "status {} of source {} names no board Status option",
8344 category_name(status.category),
8345 self.name
8346 ),
8347 })?;
8348 let terminal = matches!(target, StatusTarget::Terminal(_, _));
8349 if terminal && item.content_kind == ContentKind::DraftIssue {
8350 return Err(self.closes_a_draft(status.category));
8351 }
8352 let landed = match &target {
8353 StatusTarget::Terminal(_, reason) => {
8354 self.statuses
8355 .status(ItemKind::Task, Some(&name), true, Some(reason.reason()))
8356 }
8357 _ => self
8358 .statuses
8359 .status(ItemKind::Task, Some(&name), false, None),
8360 };
8361 let option_moves = item
8362 .option
8363 .as_deref()
8364 .is_none_or(|held| !held.eq_ignore_ascii_case(&name));
8365 let state_moves = item.content_kind == ContentKind::Issue
8366 && (item.closed != terminal || (terminal && item.status != landed));
8367 if let Some(moves) = Moves::of(option_moves, state_moves) {
8368 status_move = Some(StatusMove {
8369 board: board.id,
8370 field,
8371 option,
8372 name,
8373 target,
8374 landed,
8375 moves,
8376 });
8377 }
8378 }
8379
8380 let mut priority_move = None;
8381 if let Some(priority) = update.priority
8382 && self.priorities.is_some()
8383 && item.priority != HeldPriority::Read(priority)
8384 {
8385 let board = match (item.carried_board(), item.named_board()) {
8386 (Some(board), _) => board,
8387 (None, Some(board)) if item.defines(PRIORITY_FIELD) => BoardFields {
8388 id: board,
8389 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
8390 },
8391 _ => self.board_fields().await?,
8392 };
8393 if let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? {
8394 priority_move = Some((board.id, write, priority));
8395 }
8396 }
8397
8398 // The metadata keys this update names that a board text field projects, and what
8399 // that does to those fields — refused, as a whole write refuses it, before anything
8400 // is sent.
8401 let touched: Vec<&str> = update
8402 .metadata_set
8403 .keys()
8404 .chain(update.metadata_remove.iter())
8405 .map(MetadataKey::as_str)
8406 .collect();
8407 let mut projection_move = None;
8408 if self
8409 .metadata_fields
8410 .iter()
8411 .any(|projection| touched.contains(&&*projection.key))
8412 {
8413 let mut metadata = item.slot.clone();
8414 for (key, value) in &update.metadata_set {
8415 metadata.insert(key.as_str().to_owned(), value.clone());
8416 }
8417 for key in &update.metadata_remove {
8418 metadata.remove(key.as_str());
8419 }
8420 let board = self.projection_board(&item).await?;
8421 let (writes, projected) = self.projection_writes(
8422 &board.fields,
8423 Some(&item.projected),
8424 &metadata,
8425 Some(&touched),
8426 )?;
8427 projection_move = Some((board.id, writes, projected));
8428 }
8429
8430 // Resolved before the body is composed, because a far end `blockedBy` cannot name is
8431 // recorded in the slot, and the slot travels in the one body update below.
8432 let edges = match &update.depends_on {
8433 Some(edges) => Some(
8434 self.partition_edges(
8435 BoardKind::Work(ItemKind::Task),
8436 item.content_kind,
8437 item.blocked_by.as_deref(),
8438 edges,
8439 )
8440 .await?,
8441 ),
8442 None => None,
8443 };
8444
8445 let mut slot = item.slot.clone();
8446 for (key, value) in &update.metadata_set {
8447 slot.insert(key.as_str().to_owned(), value.clone());
8448 }
8449 for key in &update.metadata_remove {
8450 slot.remove(key.as_str());
8451 }
8452 if let Some(delivers) = &update.delivers {
8453 set_task_list(&mut slot, TaskRef::DELIVERS_KEY, delivers);
8454 }
8455 if let Some((_, recorded)) = &edges {
8456 record_edges(&mut slot, recorded);
8457 }
8458 let held = item.raw_body.clone().unwrap_or_default();
8459 let content = match &update.content {
8460 Some(content) => with_content(&held, content)?,
8461 None => held.clone(),
8462 };
8463 // A slot holding what it held is kept byte for byte, compared as JSON rather than as
8464 // the body's bytes, as a metadata write compares it: a slot a person spelled with
8465 // other whitespace would otherwise be re-encoded, which is a write nobody asked for.
8466 let body = if slot == item.slot {
8467 content
8468 } else {
8469 with_slot(&content, &slot)?
8470 };
8471 // Checked before anything is sent, as a content write checks it: content ending in
8472 // what this source reads as its own slot would read back as metadata.
8473 let (visible, read) = metadata_body(Some(body.clone()))?;
8474 let wanted = update.content.as_deref().or(item.body.as_deref());
8475 if visible.as_deref().unwrap_or_default() != wanted.unwrap_or_default() || read != slot {
8476 return Err(SourceError::Refused {
8477 message: format!(
8478 "this content ends in what source {} reads as its own metadata slot \
8479 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8480 as content; next: remove that trailing block from the content",
8481 self.name
8482 ),
8483 });
8484 }
8485 let recorded_moves =
8486 slot.get(DependencyEdge::RECORDED_KEY) != item.slot.get(DependencyEdge::RECORDED_KEY);
8487
8488 // One `updateIssue` carries all three, because every mutation spends the secondary
8489 // limiter and the title, body and state are one mutation's inputs.
8490 let mut fields = serde_json::Map::new();
8491 if let Some(title) = update.title.as_ref().filter(|title| **title != item.title) {
8492 fields.insert("title".to_owned(), json!(title));
8493 }
8494 if body != held {
8495 fields.insert("body".to_owned(), json!(body));
8496 }
8497 if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.state()) {
8498 fields.insert("stateInput".to_owned(), state_input(Some(&moving.target)));
8499 }
8500 // **The body is written last, and that is the guarantee a refusal part-way keeps.**
8501 // GitHub runs no two requests as one, and runs one document's mutation fields in order
8502 // without undoing an earlier field when a later one fails — so a body written before a
8503 // board field the board then refused would be left changed. Written after every other
8504 // write has landed, a refusal anywhere leaves the item's body, and every metadata key
8505 // it carries, exactly as they stood. So the `Status` option and the `Priority` field go
8506 // first, together in one request — a terminal option selected before the issue
8507 // closes, as a whole write does — then the `blockedBy` difference, then the body.
8508 let mut board_writes: Vec<(&BoardId, (String, Value))> = Vec::new();
8509 let mut clears: Vec<(&BoardId, &str)> = Vec::new();
8510 if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.option()) {
8511 board_writes.push((
8512 &moving.board,
8513 (
8514 moving.field.clone(),
8515 json!({"singleSelectOptionId": moving.option}),
8516 ),
8517 ));
8518 }
8519 match &priority_move {
8520 Some((board, PriorityWrite::Select { field, option }, _)) => board_writes.push((
8521 board,
8522 (field.clone(), json!({"singleSelectOptionId": option})),
8523 )),
8524 Some((board, PriorityWrite::Clear { field }, _)) => clears.push((board, field)),
8525 None => {}
8526 }
8527 if let Some((board, writes, _)) = &projection_move {
8528 for write in writes {
8529 let (mut values, mut cleared) = (Vec::new(), Vec::new());
8530 write.join(&mut values, &mut cleared);
8531 board_writes.extend(values.into_iter().map(|value| (board, value)));
8532 clears.extend(cleared.into_iter().map(|field| (board, field)));
8533 }
8534 }
8535 let mut boards: Vec<&BoardId> = board_writes.iter().map(|(board, _)| *board).collect();
8536 boards.extend(clears.iter().map(|(board, _)| *board));
8537 boards.dedup_by(|one, other| one.as_str() == other.as_str());
8538 for board in boards {
8539 let writes = board_writes
8540 .iter()
8541 .filter(|(on, _)| on.as_str() == board.as_str())
8542 .map(|(_, write)| write.clone())
8543 .collect::<Vec<_>>();
8544 let cleared = clears
8545 .iter()
8546 .filter(|(on, _)| on.as_str() == board.as_str())
8547 .map(|(_, field)| *field)
8548 .collect::<Vec<_>>();
8549 self.set_item_fields(board.as_str(), &item.item_id, &writes, &cleared)
8550 .await?;
8551 }
8552 let mut blocked_by_moved = false;
8553 if let Some((native, _)) = &edges
8554 && item.content_kind == ContentKind::Issue
8555 {
8556 blocked_by_moved = self
8557 .reconcile_blocked_by(
8558 &item.id,
8559 native,
8560 Issue::Existing(item.blocked_by.as_deref()),
8561 )
8562 .await?;
8563 }
8564 if !fields.is_empty() {
8565 self.update_content(item.content_kind, &item.id, Value::Object(fields))
8566 .await?;
8567 }
8568
8569 // The `blockedBy` this item was read with is what the write just changed, so it is no
8570 // answer any more: a later write in this command would reconcile against it — re-adding
8571 // what it kept and never removing what it dropped — and a forward-edge read would
8572 // answer with it. Left unknown, each asks GitHub instead.
8573 if blocked_by_moved {
8574 item.blocked_by = None;
8575 }
8576 if let Some(title) = &update.title {
8577 item.title.clone_from(title);
8578 }
8579 item.body = visible.filter(|value| !value.is_empty());
8580 item.raw_body = (!body.is_empty() || item.raw_body.is_some()).then_some(body);
8581 item.slot = slot;
8582 if let Some(delivers) = &update.delivers {
8583 item.delivers.clone_from(delivers);
8584 }
8585 if let Some(moving) = status_move {
8586 item.closed = matches!(moving.target, StatusTarget::Terminal(_, _))
8587 && item.content_kind == ContentKind::Issue;
8588 item.status = moving.landed;
8589 item.option = Some(moving.name);
8590 }
8591 if let Some((_, _, priority)) = priority_move {
8592 item.priority = HeldPriority::Read(priority);
8593 }
8594 if let Some((_, _, projected)) = projection_move {
8595 item.projected = projected;
8596 }
8597 let task = item.task()?;
8598 let mut written = update.changed(&before, &task);
8599 if blocked_by_moved || recorded_moves {
8600 written.insert(UpdatedField::DependsOn);
8601 }
8602 self.remember_written(item, false)?;
8603 Ok(Some(TaskUpdateOutcome {
8604 task,
8605 written,
8606 delivers_before: before.delivers,
8607 }))
8608 }
8609
8610 /// Replace one issue's visible body and its [`MetadataKey::TEMPLATE_KEY`] slot entry
8611 /// together, and nothing else; see [`TaskSource::set_task_rendering`].
8612 ///
8613 /// One update of the body: the content outside the slot, and inside it that one entry,
8614 /// every other entry kept as it was. This source keeps no template answers — an issue has
8615 /// no room beside itself that is not its body, and answers written there would duplicate
8616 /// what the content already says and count against GitHub's body limit — so `answers`
8617 /// reaches nothing here. A body that would not change is not sent at all.
8618 async fn replace_rendering(
8619 &self,
8620 id: &NativeId,
8621 kind: BoardKind,
8622 content: &str,
8623 provenance: &Value,
8624 assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
8625 ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
8626 let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
8627 return Ok(None);
8628 };
8629 let held = item.raw_body.clone().unwrap_or_default();
8630 let mut slot = item.slot.clone();
8631 slot.insert(MetadataKey::TEMPLATE_KEY.to_owned(), provenance.clone());
8632 let rewritten;
8633 let content = if let Some(assets) = assets {
8634 let uploads = self
8635 .upload_assets(item.own_repository.as_ref(), assets)
8636 .await?;
8637 rewritten =
8638 onetaskgraph_plugin_api::serve_asset_references(content, &mut slot, &uploads);
8639 rewritten.as_str()
8640 } else {
8641 content
8642 };
8643 let body = with_slot(&with_content(&held, content)?, &slot)?;
8644 // Checked before anything is sent, as a content write checks it.
8645 let (visible, read) = metadata_body(Some(body.clone()))?;
8646 if visible.as_deref().unwrap_or_default() != content || read != slot {
8647 return Err(SourceError::Refused {
8648 message: format!(
8649 "this content ends in what source {} reads as its own metadata slot \
8650 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8651 as content; next: remove that trailing block from the template",
8652 self.name
8653 ),
8654 });
8655 }
8656 if body != held {
8657 self.update_content(item.content_kind, &item.id, json!({"body": body}))
8658 .await?;
8659 }
8660 item.body = visible.filter(|value| !value.is_empty());
8661 item.raw_body = Some(body);
8662 item.slot = read;
8663 self.remember_written(item, false)?;
8664 Ok(Some(onetaskgraph_plugin_api::AssetsWritten {
8665 id: id.clone(),
8666 content: Some(content.to_owned()),
8667 }))
8668 }
8669
8670 async fn set_item_field(
8671 &self,
8672 board_id: &str,
8673 item_id: &str,
8674 field_id: &str,
8675 value: Value,
8676 ) -> Result<(), SourceError> {
8677 let data = self
8678 .graphql(
8679 graphql::UPDATE_FIELD,
8680 json!({"input":{
8681 "projectId":board_id,"itemId":item_id,"fieldId":field_id,"value":value
8682 },"readPriority":false,"priorityName":PRIORITY_FIELD}),
8683 )
8684 .await?;
8685 let returned = data
8686 .pointer("/updateProjectV2ItemFieldValue/projectV2Item")
8687 .ok_or_else(|| SourceError::Malformed {
8688 message: "GitHub field update returned no project item".into(),
8689 })?;
8690 if required_str(returned, "id")? != item_id {
8691 return Err(SourceError::Malformed {
8692 message: "GitHub field update returned the wrong project item".into(),
8693 });
8694 }
8695 Ok(())
8696 }
8697
8698 /// GitHub accepts one value per field mutation; aliases combine those mutations in
8699 /// one request — writes in the order given, then clears. A lone write is
8700 /// [`graphql::UPDATE_FIELD`] and a lone clear [`graphql::CLEAR_FIELD`]; more than one
8701 /// request's slots hold go in further requests of the same document, in order. Every
8702 /// returned item id is checked, including optional aliases.
8703 async fn set_item_fields(
8704 &self,
8705 board: &str,
8706 item: &str,
8707 fields: &[(String, Value)],
8708 clears: &[&str],
8709 ) -> Result<(), SourceError> {
8710 match (fields, clears) {
8711 ([], []) => return Ok(()),
8712 ([(field, value)], []) => {
8713 return self.set_item_field(board, item, field, value.clone()).await;
8714 }
8715 ([], [field]) => {
8716 return self
8717 .write_priority(
8718 board,
8719 item,
8720 &PriorityWrite::Clear {
8721 field: (*field).to_owned(),
8722 },
8723 )
8724 .await;
8725 }
8726 _ => {}
8727 }
8728 // Any field of the request stands in for an input a slot leaves out, which GitHub
8729 // still requires to be well formed although it never runs it.
8730 let filler = fields
8731 .first()
8732 .map_or_else(|| clears[0], |(field, _)| field.as_str());
8733 let mut writes = fields.iter();
8734 let mut cleared = clears.iter();
8735 loop {
8736 let batch: Vec<&(String, Value)> =
8737 writes.by_ref().take(FIELD_WRITE_SLOTS.len()).collect();
8738 let batch_clears: Vec<&&str> = cleared.by_ref().take(FIELD_CLEAR_SLOTS.len()).collect();
8739 if batch.is_empty() && batch_clears.is_empty() {
8740 return Ok(());
8741 }
8742 let mut variables = serde_json::Map::new();
8743 for (index, slot) in FIELD_WRITE_SLOTS.iter().enumerate() {
8744 let input = match batch.get(index) {
8745 Some((field, value)) => {
8746 json!({"projectId":board,"itemId":item,"fieldId":field,"value":value})
8747 }
8748 None => json!({"projectId":board,"itemId":item,"fieldId":filler,
8749 "value":{"text":""}}),
8750 };
8751 variables.insert(slot.variable.to_owned(), input);
8752 variables.insert(slot.include.to_owned(), json!(index < batch.len()));
8753 }
8754 for (index, slot) in FIELD_CLEAR_SLOTS.iter().enumerate() {
8755 let field = batch_clears.get(index).map_or(filler, |field| **field);
8756 variables.insert(
8757 slot.variable.to_owned(),
8758 json!({"projectId":board,"itemId":item,"fieldId":field}),
8759 );
8760 variables.insert(slot.include.to_owned(), json!(index < batch_clears.len()));
8761 }
8762 let data = self
8763 .graphql(graphql::UPDATE_FIELDS, Value::Object(variables))
8764 .await?;
8765 let sent = FIELD_WRITE_SLOTS[..batch.len()]
8766 .iter()
8767 .chain(&FIELD_CLEAR_SLOTS[..batch_clears.len()]);
8768 for slot in sent {
8769 let alias = slot.alias;
8770 let returned = data
8771 .get(alias)
8772 .and_then(|value| value.get("projectV2Item"))
8773 .ok_or_else(|| SourceError::Malformed {
8774 message: format!("GitHub field update {alias} returned no project item"),
8775 })?;
8776 if required_str(returned, "id")? != item {
8777 return Err(SourceError::Malformed {
8778 message: format!(
8779 "GitHub field update {alias} returned the wrong project item"
8780 ),
8781 });
8782 }
8783 }
8784 }
8785 }
8786
8787 async fn native_dependency_ids(&self, id: &NativeId) -> Result<Vec<String>, SourceError> {
8788 let mut after: Option<String> = None;
8789 let mut ids = Vec::new();
8790 loop {
8791 let data = self
8792 .graphql(
8793 graphql::ISSUE_DEPENDENCIES,
8794 json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after}),
8795 )
8796 .await?;
8797 let connection =
8798 data.pointer("/node/blockedBy")
8799 .ok_or_else(|| SourceError::Malformed {
8800 message: "GitHub dependency response has no blockedBy connection".into(),
8801 })?;
8802 ids.extend(
8803 connection
8804 .get("nodes")
8805 .and_then(Value::as_array)
8806 .ok_or_else(|| SourceError::Malformed {
8807 message: "GitHub dependency response nodes is not an array".into(),
8808 })?
8809 .iter()
8810 .map(|value| required_str(value, "id").map(str::to_owned))
8811 .collect::<Result<Vec<_>, _>>()?,
8812 );
8813 let next = next_cursor(connection)?;
8814 if let Some(next) = &next {
8815 validate_cursor_progress(after.as_deref(), &next.0)?;
8816 }
8817 after = next.map(|cursor| cursor.0);
8818 if after.is_none() {
8819 return Ok(ids);
8820 }
8821 }
8822 }
8823
8824 async fn dependencies(
8825 &self,
8826 id: &NativeId,
8827 near_kind: ItemKind,
8828 direction: Direction,
8829 page: &PageRequest,
8830 ) -> Result<Page<DependencyEdge>, SourceError> {
8831 validate_page(page)?;
8832 let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
8833 let cursor = page.cursor.as_ref().map(|c| c.0.as_str());
8834 let recorded = recorded_offset(cursor, direction)?;
8835 // What this issue is blocked by, when a read of it by its own id in this command
8836 // already carried the whole connection — a copy reads the item it writes before it
8837 // reads its edges — and the page asked for is the whole of it, or the recorded tail
8838 // after it. Answered from that read, in the shape the dependency read answers in;
8839 // anything else is asked of GitHub.
8840 let carried = match direction {
8841 Direction::DependsOn => self
8842 .resolved_cache()?
8843 .get(id)
8844 .filter(|item| item.content_kind == ContentKind::Issue)
8845 .and_then(|item| Some((item.blocked_by.clone()?, item.raw_body.clone()))),
8846 Direction::DependedOnBy => None,
8847 }
8848 .filter(|(nodes, _)| recorded.is_some() || (cursor.is_none() && nodes.len() <= limit));
8849 // Asked for even in the recorded phase, whose page reads nothing from the
8850 // connection: `__typename` is what says whether this item has a native
8851 // relationship at all, and that is what decides which far ends the reserved key is
8852 // allowed to hold.
8853 let data = match carried {
8854 Some((nodes, body)) => json!({"node":{"__typename":"Issue","body":body,
8855 "blockedBy":{"nodes":nodes,"pageInfo":{"hasNextPage":false,"endCursor":null}}}}),
8856 None => {
8857 let after = if recorded.is_some() { None } else { cursor };
8858 let first = page.limit.min(MAX_PAGE_SIZE);
8859 match direction {
8860 Direction::DependsOn => {
8861 self.graphql(
8862 graphql::ISSUE_DEPENDS_ON,
8863 json!({"id":id.0,"first":first,"after":after,"owner":self.owner,
8864 "number":self.project_number,
8865 "boardItems":BOARD_ITEMS_PAGE_SIZE}),
8866 )
8867 .await?
8868 }
8869 Direction::DependedOnBy => {
8870 self.graphql(
8871 graphql::ISSUE_DEPENDED_ON_BY,
8872 json!({"id":id.0,"first":first,"after":after}),
8873 )
8874 .await?
8875 }
8876 }
8877 }
8878 };
8879 let draft_slot = draft_slot(&data, id, self.project_number)?;
8880 let node =
8881 data.get("node")
8882 .filter(|v| !v.is_null())
8883 .ok_or_else(|| SourceError::Refused {
8884 message: format!(
8885 "GitHub item {} was not found or does not support dependencies",
8886 id.0
8887 ),
8888 })?;
8889 let connection_name = match direction {
8890 Direction::DependsOn => "blockedBy",
8891 Direction::DependedOnBy => "blocking",
8892 };
8893 // A draft has neither `blockedBy` nor `blocking`, so nothing it depends on can be
8894 // named natively and the reserved key may hold any far end. An issue's connections
8895 // hold issues, and this source reads them at the near item's own level.
8896 let natively_names = (required_str(node, "__typename")? == "Issue").then_some(near_kind);
8897 if let Some(offset) = recorded {
8898 return Ok(recorded_page(
8899 self.recorded_edges(
8900 id,
8901 near_kind,
8902 direction,
8903 natively_names,
8904 node,
8905 draft_slot.as_ref(),
8906 )
8907 .await?,
8908 offset,
8909 limit,
8910 ));
8911 }
8912 if natively_names.is_none() {
8913 return Ok(recorded_page(
8914 self.recorded_edges(
8915 id,
8916 near_kind,
8917 direction,
8918 natively_names,
8919 node,
8920 draft_slot.as_ref(),
8921 )
8922 .await?,
8923 0,
8924 limit,
8925 ));
8926 }
8927 let connection = node
8928 .get(connection_name)
8929 .ok_or_else(|| SourceError::Malformed {
8930 message: "GitHub dependency response is missing its connection".into(),
8931 })?;
8932 let nodes = connection
8933 .get("nodes")
8934 .and_then(Value::as_array)
8935 .ok_or_else(|| SourceError::Malformed {
8936 message: "GitHub dependency response nodes is not an array".into(),
8937 })?;
8938 // `from` depends on `to`, always. GitHub spells the same relationship from either
8939 // end — `blockedBy` lists what this item waits on, `blocking` lists what waits on
8940 // it — so the near item is `from` in one direction and `to` in the other.
8941 let items = nodes
8942 .iter()
8943 .map(|value| {
8944 let related = NativeId(required_str(value, "id")?.into());
8945 let related_kind = related_kind(value)?;
8946 let (from, to) = match direction {
8947 Direction::DependsOn => (
8948 DependencyEndpoint::from_native(id.clone(), near_kind),
8949 DependencyEndpoint::from_native(related, related_kind),
8950 ),
8951 Direction::DependedOnBy => (
8952 DependencyEndpoint::from_native(related, related_kind),
8953 DependencyEndpoint::from_native(id.clone(), near_kind),
8954 ),
8955 };
8956 Ok(DependencyEdge {
8957 from,
8958 to,
8959 kind: DependencyKind::Blocks,
8960 })
8961 })
8962 .collect::<Result<Vec<_>, SourceError>>()?;
8963 let mut next = next_cursor(connection)?;
8964 if let Some(next) = &next {
8965 validate_cursor_progress(cursor, &next.0)?;
8966 }
8967 // The recorded far ends ride on the last page of `blockedBy`, whose answer carried the
8968 // body they are recorded in, as far as the page has room — so they cost no request of
8969 // their own — and only what does not fit is resumed by a cursor.
8970 let mut items = items;
8971 if next.is_none() {
8972 let recorded = self
8973 .recorded_edges(
8974 id,
8975 near_kind,
8976 direction,
8977 natively_names,
8978 node,
8979 draft_slot.as_ref(),
8980 )
8981 .await?;
8982 let room = limit.saturating_sub(items.len());
8983 let total = recorded.len();
8984 items.extend(recorded.into_iter().take(room));
8985 if total > room {
8986 next = Some(Cursor(format!("{RECORDED_CURSOR}{room}")));
8987 }
8988 }
8989 Ok(Page { items, next })
8990 }
8991
8992 /// The edges this item records under [`DependencyEdge::RECORDED_KEY`], which is where
8993 /// a far end in another source has to live: no GitHub issue relationship can name one.
8994 ///
8995 /// Only forwards. The reverse of a recorded edge is derived from the far end, and this
8996 /// source never writes one down.
8997 ///
8998 /// The metadata lives in the item's own body slot, and `node` is the dependency read's
8999 /// own answer, which carries an issue's body — so an issue's recorded edges cost no
9000 /// request beyond the read already made, and reading the board for them would be a
9001 /// walk of every item for one field of one. A draft has no body in that answer, because
9002 /// a draft is not an issue, so a draft's are read off its own read by id — never off a
9003 /// listing of the board, which can be behind on the very item asked about.
9004 async fn recorded_edges(
9005 &self,
9006 id: &NativeId,
9007 near_kind: ItemKind,
9008 direction: Direction,
9009 natively_names: Option<ItemKind>,
9010 node: &Value,
9011 draft_slot: Option<&Option<BTreeMap<String, Value>>>,
9012 ) -> Result<Vec<DependencyEdge>, SourceError> {
9013 if direction != Direction::DependsOn {
9014 return Ok(Vec::new());
9015 }
9016 let slot = match (node.get("body"), draft_slot) {
9017 (Some(body), _) if natively_names.is_some() => {
9018 metadata_body(body.as_str().map(str::to_owned))?.1
9019 }
9020 // A draft the dependency read itself answered: this board's, or none of its.
9021 (_, Some(Some(slot))) => slot.clone(),
9022 (_, Some(None)) => return Ok(Vec::new()),
9023 _ => {
9024 let Some(item) = self.bound_item(id).await? else {
9025 return Ok(Vec::new());
9026 };
9027 item.slot
9028 }
9029 };
9030 DependencyEdge::recorded(&slot, id, near_kind, &self.name, natively_names)
9031 .map_err(|message| SourceError::Malformed { message })
9032 }
9033
9034 fn configured_repository(&self) -> Result<&RepositoryTarget, SourceError> {
9035 self.repository
9036 .as_ref()
9037 .ok_or_else(|| SourceError::Refused {
9038 message: format!(
9039 "source {} has no repository configured, and a GitHub Projects board has no \
9040 repository of its own to create an issue in; set repository: owner/name on \
9041 this source",
9042 self.name
9043 ),
9044 })
9045 }
9046
9047 /// The repository one new issue is created in, under the rule [`RepositoryTarget`]
9048 /// states.
9049 ///
9050 /// The fallback is demanded first, whichever arm answers: a write without a configured
9051 /// repository is refused naming the field exactly as it was before the rule existed,
9052 /// so a source that could not write before cannot write now, rather than writing for
9053 /// the one item whose own field happens to decide it.
9054 ///
9055 /// Everything this refuses is refused before `createIssue`, so a refusal leaves no
9056 /// issue behind: an entry that is not a repository on [`RepositoryTarget::HOST`], an
9057 /// entry owned by someone other than the owner of the parent issue's repository —
9058 /// GitHub accepts a sub-issue from another repository of the same owner and from no
9059 /// other, so `addSubIssue` would refuse it after the issue existed — a parent the
9060 /// board does not hold, and a parent that is a draft, which GitHub gives no sub-issues,
9061 /// both of which `addSubIssue` would likewise refuse too late. Whether the entry exists
9062 /// and is visible to the token is checked where its node id is resolved, still before
9063 /// `createIssue`. The parent is read by its own id through [`Self::item_by_id`] — never
9064 /// looked up in a listing of the board, which can be minutes behind an issue its own
9065 /// `projectItems` already places on it — and that read answers first from this process's
9066 /// own record, so a project created moments ago in this command answers though GitHub
9067 /// has not caught up.
9068 async fn creation_target(
9069 &self,
9070 incoming: &Incoming<'_>,
9071 ) -> Result<RepositoryTarget, SourceError> {
9072 let what = format!(
9073 "{} {:?}",
9074 incoming.written.kind().describes(),
9075 incoming.title
9076 );
9077 self.creation_rule(incoming.repositories, incoming.parent, &what)
9078 .await
9079 }
9080
9081 /// [`Self::creation_target`]'s rule over what it reads of the item — its `repositories`
9082 /// and the project issue it is filed under — with `what` naming the item in a refusal: the
9083 /// one place the rule is, so the create and [`TaskSource::creation_repository`] cannot
9084 /// answer differently.
9085 async fn creation_rule(
9086 &self,
9087 repositories: &[Repository],
9088 parent: Option<&NativeId>,
9089 what: &str,
9090 ) -> Result<RepositoryTarget, SourceError> {
9091 let fallback = self.configured_repository()?;
9092 let parent = match parent {
9093 Some(parent) => Some(self.bound_item(parent).await?.ok_or_else(|| {
9094 SourceError::Refused {
9095 message: format!(
9096 "GitHub project issue {} was not found on the board of source {}, so {} \
9097 cannot be filed under it",
9098 parent.0, self.name, what
9099 ),
9100 }
9101 })?),
9102 None => None,
9103 };
9104 let parents_repository = parent
9105 .as_ref()
9106 .map(|parent| {
9107 // A draft is on the board and so is found, but it has no repository to
9108 // place a task in and GitHub gives it no sub-issues, so `addSubIssue`
9109 // would refuse the task only once `createIssue` had made it.
9110 if parent.content_kind == ContentKind::DraftIssue {
9111 return Err(SourceError::Refused {
9112 message: format!(
9113 "GitHub project item {} on the board of source {} is a draft, \
9114 which cannot have sub-issues, so {} cannot be filed under it",
9115 parent.id.0, self.name, what
9116 ),
9117 });
9118 }
9119 // An issue's repository is where a sub-issue is placed and whose owner it
9120 // is compared against, so a parent whose repository this source cannot
9121 // spell as `owner/name` — GitHub's login grammar is wider than this
9122 // source's floor — is one nothing can be filed under.
9123 parent
9124 .own_repository
9125 .as_ref()
9126 .and_then(|origin| RepositoryTarget::from_origin(origin).ok())
9127 .ok_or_else(|| SourceError::Malformed {
9128 message: format!(
9129 "GitHub project issue {} on the board of source {} is in {}, which \
9130 is not a {}/owner/name repository this source can place {} in",
9131 parent.id.0,
9132 self.name,
9133 parent
9134 .own_repository
9135 .as_ref()
9136 .map_or("no repository", Repository::as_str),
9137 RepositoryTarget::HOST,
9138 what
9139 ),
9140 })
9141 })
9142 .transpose()?;
9143 match repositories {
9144 [named] => {
9145 let target =
9146 RepositoryTarget::from_origin(named).map_err(|_| SourceError::Refused {
9147 message: format!(
9148 "{} names repository {}, which is not a {}/owner/name repository \
9149 source {} can create an issue in; name one that is, or name none",
9150 what,
9151 named.as_str(),
9152 RepositoryTarget::HOST,
9153 self.name
9154 ),
9155 })?;
9156 if let Some(parents) = &parents_repository
9157 && parents.owner != target.owner
9158 {
9159 return Err(SourceError::Refused {
9160 message: format!(
9161 "{} names repository {}, owned by {}, but its project's issue is in \
9162 {}, owned by {}, and GitHub files a sub-issue only in a repository \
9163 of the same owner as its parent issue; name a repository of {}, or \
9164 name none",
9165 what,
9166 target.slug(),
9167 target.owner,
9168 parents.slug(),
9169 parents.owner,
9170 parents.owner
9171 ),
9172 });
9173 }
9174 Ok(target)
9175 }
9176 _ => Ok(parents_repository.unwrap_or_else(|| fallback.clone())),
9177 }
9178 }
9179
9180 /// The node id of the repository `incoming` is being created in, or the refusal naming
9181 /// the item and the repository the token cannot see.
9182 ///
9183 /// Resolved once per command per repository; see [`Self::repository_cache`].
9184 async fn repository_id(
9185 &self,
9186 repository: &RepositoryTarget,
9187 incoming: &Incoming<'_>,
9188 ) -> Result<String, SourceError> {
9189 if let Some(id) = self.repository_cache()?.get(repository).cloned() {
9190 return Ok(id);
9191 }
9192 let data = self
9193 .graphql(
9194 graphql::REPOSITORY,
9195 json!({"owner":repository.owner,"name":repository.name}),
9196 )
9197 .await?;
9198 self.repository_read(&data, repository, incoming)
9199 }
9200
9201 /// The repository's node id out of an answer carrying the `repository` root, held for
9202 /// the rest of this command, or the refusal naming the item that cannot be created in it.
9203 fn repository_read(
9204 &self,
9205 data: &Value,
9206 repository: &RepositoryTarget,
9207 incoming: &Incoming<'_>,
9208 ) -> Result<String, SourceError> {
9209 let node = data
9210 .get("repository")
9211 .filter(|value| !value.is_null())
9212 .ok_or_else(|| SourceError::Refused {
9213 message: format!(
9214 "GitHub repository {} was not found or is not visible to the token, so {} \
9215 {:?} cannot be created in it",
9216 repository.slug(),
9217 incoming.written.kind().describes(),
9218 incoming.title
9219 ),
9220 })?;
9221 let id = required_str(node, "id")?.to_owned();
9222 self.repository_cache()?
9223 .insert(repository.clone(), id.clone());
9224 Ok(id)
9225 }
9226
9227 fn repository_cache(
9228 &self,
9229 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<RepositoryTarget, String>>, SourceError> {
9230 self.repository_cache
9231 .lock()
9232 .map_err(|_| SourceError::Unavailable {
9233 message: "this source's record of the destination repository was left \
9234 inconsistent by an earlier failure; next: run the command again"
9235 .into(),
9236 })
9237 }
9238
9239 /// Create or update one board item, whichever kind it is.
9240 async fn write_item(
9241 &self,
9242 incoming: &Incoming<'_>,
9243 target: Option<&NativeId>,
9244 depends_on: &[DependencyEdge],
9245 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
9246 let prepared = self.prepare_write(incoming, target, depends_on).await?;
9247 let landed = match &prepared.destination {
9248 // Its content is written last, by `complete_write`, once everything else has landed.
9249 WriteDestination::Existing(item) => Landed {
9250 content_id: item.id.clone(),
9251 item_id: Some(item.item_id.clone()),
9252 url: item.url.clone(),
9253 number: item.number,
9254 },
9255 WriteDestination::New(target) => {
9256 if self.deferring()? {
9257 self.create_filed_issue(
9258 prepared.board.id.as_str(),
9259 target,
9260 incoming,
9261 &prepared.body,
9262 )
9263 .await?
9264 } else {
9265 self.create_and_file_issue(
9266 prepared.board.id.as_str(),
9267 target,
9268 incoming,
9269 &prepared.body,
9270 )
9271 .await?
9272 }
9273 }
9274 };
9275 self.complete_write(incoming, prepared, landed).await
9276 }
9277
9278 /// Everything one write of `incoming` reads and decides before it sends anything: what is
9279 /// there now, where a new item is created, the board's fields, and every value refused while
9280 /// nothing has been written — the item's body included.
9281 async fn prepare_write(
9282 &self,
9283 incoming: &Incoming<'_>,
9284 target: Option<&NativeId>,
9285 depends_on: &[DependencyEdge],
9286 ) -> Result<Prepared, SourceError> {
9287 // Refused before anything is read or written: a task or a project titled the way
9288 // this board spells a document would land as an issue this same source reads back
9289 // as a document, so the field this destination cannot carry is named rather than
9290 // written and silently reclassified.
9291 if let Written::Work(kind, _) = incoming.written
9292 && incoming.title.starts_with(DESIGN_TITLE_PREFIX)
9293 {
9294 return Err(SourceError::Refused {
9295 message: format!(
9296 "the title of this {} begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
9297 spells a document, so it would read back as one rather than as a {}; \
9298 retitle it, or copy it as a document",
9299 kind.marker(),
9300 self.name,
9301 kind.marker()
9302 ),
9303 });
9304 }
9305 // The destination is read by its own id, and whether this board holds it is decided
9306 // by that read — its own `projectItems` — rather than by whether a listing of the
9307 // board happens to include it yet. See the module documentation.
9308 // An existing issue is never moved; a new one is created where the rule says — and
9309 // knowing where is what lets the board's fields and that repository's id be read
9310 // together, before anything below needs either.
9311 let destination = match target {
9312 Some(target) => {
9313 WriteDestination::Existing(Box::new(self.bound_item(target).await?.ok_or_else(
9314 || SourceError::Refused {
9315 message: format!("GitHub destination item {} was not found", target.0),
9316 },
9317 )?))
9318 }
9319 None => {
9320 let target = self.creation_target(incoming).await?;
9321 self.creation_context(&target, incoming).await?;
9322 WriteDestination::New(target)
9323 }
9324 };
9325 let existing = destination.existing();
9326 let board = self
9327 .fields_for(
9328 existing,
9329 incoming.metadata.contains_key(ORIGIN_KEY),
9330 incoming.written.status().is_some(),
9331 incoming
9332 .priority
9333 .is_some_and(|priority| priority != Priority::None),
9334 )
9335 .await?;
9336 let status_target = incoming
9337 .written
9338 .work_status()
9339 .map(|(kind, status)| self.resolved_target(kind, status.category))
9340 .transpose()?;
9341 let column = match (incoming.written.work_status(), status_target.as_ref()) {
9342 (Some((kind, status)), Some(target)) => {
9343 self.column_for(&board.fields, kind, status.category, target)?
9344 }
9345 _ => None,
9346 };
9347 // Resolved before anything is created, for the reason the column above is: a
9348 // priority this board has no option for is refused while nothing has been written.
9349 let priority_write = match incoming.priority {
9350 Some(priority) => self.priority_write(&board.fields, existing, priority)?,
9351 None => None,
9352 };
9353 // And for the reason the priority is: a value no projected field can hold, or a board
9354 // that cannot hold the field, is refused while nothing has been written.
9355 let (projections, projected) = self.projection_writes(
9356 &board.fields,
9357 existing.map(|item| &item.projected),
9358 incoming.metadata,
9359 None,
9360 )?;
9361 let content_kind = existing.map_or(ContentKind::Issue, |item| item.content_kind);
9362 if content_kind == ContentKind::DraftIssue {
9363 if let (Some(StatusTarget::Terminal(_, _)), Some(status)) =
9364 (status_target.as_ref(), incoming.written.status())
9365 {
9366 return Err(self.closes_a_draft(status.category));
9367 }
9368 if incoming.parent.is_some() {
9369 return Err(SourceError::Refused {
9370 message: "GitHub draft items cannot be a project's sub-issue".into(),
9371 });
9372 }
9373 }
9374 match existing {
9375 Some(item) if content_kind == ContentKind::Issue => {
9376 if item.labels != incoming.labels {
9377 return Err(SourceError::Refused {
9378 message: "GitHub issue labels differ from the labels being written".into(),
9379 });
9380 }
9381 }
9382 _ => {
9383 if !incoming.labels.is_empty() {
9384 return Err(SourceError::Refused {
9385 message: "GitHub items created by this destination carry no labels".into(),
9386 });
9387 }
9388 }
9389 }
9390
9391 // The repository the issue really lives in is what the slot below is written against,
9392 // so a single entry that is where the issue is created travels as no key at all, and
9393 // the read side derives it back from the issue.
9394 let own_repository = match &destination {
9395 WriteDestination::Existing(item) => item.own_repository.clone(),
9396 WriteDestination::New(target) => Some(
9397 Repository::try_from(target.origin())
9398 .map_err(|message| SourceError::Config { message })?,
9399 ),
9400 };
9401 let (native, fallback) = self
9402 .partition_edges(
9403 incoming.written.kind(),
9404 content_kind,
9405 existing.and_then(|item| item.blocked_by.as_deref()),
9406 depends_on,
9407 )
9408 .await?;
9409 let mut slot = slot_metadata(incoming, own_repository.as_ref(), &fallback);
9410 let content = match incoming.assets {
9411 Some(assets) => {
9412 let uploads = self.upload_assets(own_repository.as_ref(), assets).await?;
9413 let rewritten = onetaskgraph_plugin_api::serve_asset_references(
9414 incoming.content.unwrap_or_default(),
9415 &mut slot,
9416 &uploads,
9417 );
9418 incoming.content.map(|_| rewritten)
9419 }
9420 None => incoming.content.map(str::to_owned),
9421 };
9422 let body = compose_body(content.as_deref(), &slot)?;
9423 // Read before anything is created, for the reason the field below is: a value
9424 // this destination cannot store has to refuse, and refusing after `createIssue`
9425 // would leave an issue behind that nothing asked for. The engine writes a
9426 // qualified id here; a caller handing this key anything else is told so rather
9427 // than having it silently stored as no origin at all.
9428 // 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.
9429 let origin = match incoming.metadata.get(ORIGIN_KEY) {
9430 None => "",
9431 Some(Value::String(origin)) => origin.as_str(),
9432 Some(other) => {
9433 return Err(SourceError::Refused {
9434 message: format!(
9435 "{ORIGIN_KEY} holds a qualified id spelled as a string, and this item's \
9436 is {other}"
9437 ),
9438 });
9439 }
9440 };
9441 // Resolved before anything is created: a board that cannot carry the copy origin
9442 // has to refuse the write, and refusing it after `createIssue` would leave an
9443 // issue behind that nothing asked for.
9444 let origin_field = match Board::field(&board.fields, ORIGIN_FIELD)? {
9445 Some(field) => {
9446 if required_str(field, "__typename")? != "ProjectV2Field" {
9447 return Err(SourceError::Refused {
9448 message: format!(
9449 "GitHub board source-owned {ORIGIN_FIELD} field is not a text field"
9450 ),
9451 });
9452 }
9453 Some(required_str(field, "id")?.to_owned())
9454 }
9455 None if incoming.metadata.contains_key(ORIGIN_KEY) => {
9456 return Err(SourceError::Refused {
9457 message: format!(
9458 "GitHub board has no source-owned {ORIGIN_FIELD} text field, and the \
9459 item carries {ORIGIN_KEY}; add a text field named {ORIGIN_FIELD} to \
9460 the board"
9461 ),
9462 });
9463 }
9464 None => None,
9465 };
9466
9467 Ok(Prepared {
9468 origin: origin.to_owned(),
9469 destination,
9470 board,
9471 status_target,
9472 column,
9473 priority_write,
9474 projections,
9475 projected,
9476 content_kind,
9477 own_repository,
9478 native,
9479 slot,
9480 content,
9481 body,
9482 origin_field,
9483 })
9484 }
9485
9486 /// Everything one write does once the item it writes exists: its board fields, its
9487 /// relationships and — for an item that was already there — its content last, then what this
9488 /// process remembers of it.
9489 ///
9490 /// While this source is holding writes back — see [`TaskSource::begin_deferred_writes`] — an
9491 /// item this command created has its fields, its closing and its edges held for
9492 /// [`TaskSource::finish_writes`] rather than sent here.
9493 async fn complete_write(
9494 &self,
9495 incoming: &Incoming<'_>,
9496 prepared: Prepared,
9497 landed: Landed,
9498 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
9499 let Prepared {
9500 destination,
9501 board,
9502 status_target,
9503 column,
9504 priority_write,
9505 projections,
9506 projected,
9507 content_kind,
9508 own_repository,
9509 native,
9510 slot,
9511 content,
9512 body,
9513 origin,
9514 origin_field,
9515 } = prepared;
9516 let existing = destination.existing();
9517 let origin = origin.as_str();
9518 let Landed {
9519 content_id,
9520 item_id,
9521 url,
9522 number,
9523 } = landed;
9524
9525 // Only what differs from the item as this write read it is sent: a priority the item
9526 // already holds is not, so an unchanged re-copy writes no field and a person's edit of
9527 // it since that read is left alone.
9528 let priority_write = priority_write.filter(|_| {
9529 !existing.is_some_and(|item| {
9530 incoming.priority.map(HeldPriority::Read) == Some(item.priority.clone())
9531 })
9532 });
9533 let written_option = column.as_ref().map(|(_, _, name)| name.clone());
9534 let column = column
9535 .filter(|(_, _, name)| existing.is_none_or(|item| item.option.as_ref() != Some(name)))
9536 .map(|(field, option, _)| (field, option));
9537 // Creating an item here is several calls — `createIssue`, which files it on the
9538 // board, then its board fields, the parent and the dependencies — and GitHub can fail
9539 // at any of them. Everything this source can refuse *before* the first of those is
9540 // already checked above, so what is left is GitHub itself failing part way. When it
9541 // does over an item this call created, the issue is taken back: a write that
9542 // refused must not leave an item behind that nobody asked for, and one that does
9543 // makes the retry create a second.
9544 // Whether the board-field write carrying a moved origin was answered as landing whole.
9545 // When it was refused, GitHub does not say which of its fields ran before the one that
9546 // failed, so the origin may or may not have moved.
9547 let mut origin_landed = false;
9548 // An item this command created while writes are held back has every part that can land
9549 // later held for `finish_writes` — its own create filed it on the board and under its
9550 // project already — and so does a later write of it in the same command.
9551 let holding = (existing.is_none() && self.deferring()?) || self.holds_back(&content_id)?;
9552 let landed = if holding {
9553 self.hold_back(
9554 &board.id,
9555 &content_id,
9556 existing,
9557 origin_field.as_deref(),
9558 origin,
9559 column,
9560 status_target.as_ref(),
9561 priority_write.as_ref(),
9562 &projections,
9563 &native,
9564 )
9565 } else if let Some(item_id) = item_id.as_deref() {
9566 self.finish_write(
9567 board.id.as_str(),
9568 incoming,
9569 &content_id,
9570 item_id,
9571 content_kind,
9572 existing,
9573 origin_field.as_deref(),
9574 origin,
9575 column,
9576 status_target.as_ref(),
9577 priority_write.as_ref(),
9578 &projections,
9579 &native,
9580 &mut origin_landed,
9581 )
9582 .await
9583 } else {
9584 // An issue a create filed on the board itself has no board item this process
9585 // knows until `finish_writes` reads it, so nothing but a held-back write may land
9586 // on it before then.
9587 Err(SourceError::Malformed {
9588 message: format!(
9589 "issue {} was written before the board item GitHub filed it as was read",
9590 content_id.0
9591 ),
9592 })
9593 };
9594 // An existing item's title, body and state go last, in one `updateIssue`, once its board
9595 // fields and its relationships have landed: a refusal of any of those then leaves its
9596 // body — and the metadata slot inside it — exactly as it stood.
9597 let landed = match (landed, existing) {
9598 (Ok(()), Some(item)) => {
9599 self.update_existing(item, incoming, &body, status_target.as_ref())
9600 .await
9601 }
9602 (landed, _) => landed,
9603 };
9604 if let Err(error) = landed {
9605 match existing {
9606 // Best effort, and the write's own failure is what the caller is told: a
9607 // refusal naming the tidy-up would hide why the write failed at all.
9608 None => {
9609 let _ = self.delete_issue(&content_id).await;
9610 }
9611 // The origin field is the one piece of an existing item's metadata written
9612 // before its body, so a write refused after it puts it back as it was. When
9613 // that is refused too, the write's own failure is still what the caller is
9614 // told — with what it left behind added, because the item's metadata is then
9615 // not as it stood and a caller retrying has to know which key moved.
9616 Some(item) => {
9617 let before = item.origin.as_deref().unwrap_or("");
9618 if let Some(field) = origin_field.as_deref()
9619 && before != origin
9620 && let Err(restore) = self
9621 .set_item_field(
9622 board.id.as_str(),
9623 &item.item_id,
9624 field,
9625 json!({"text": before}),
9626 )
9627 .await
9628 {
9629 let left = if origin_landed {
9630 format!(
9631 "its {ORIGIN_KEY} was moved to {origin:?} before that and could \
9632 not be put back to {before:?} ({restore}), so item {} still \
9633 holds {origin:?} there",
9634 item.id.0
9635 )
9636 } else {
9637 format!(
9638 "the refused write carried its {ORIGIN_KEY} from {before:?} to \
9639 {origin:?}, GitHub does not say whether that part of it ran, \
9640 and putting it back to {before:?} was refused ({restore}), so \
9641 item {} holds {origin:?} or {before:?} there",
9642 item.id.0
9643 )
9644 };
9645 return Err(noting(
9646 error,
9647 &format!(
9648 "; {left}; next: set {ORIGIN_KEY} on it back to {before:?}, or \
9649 run the write again"
9650 ),
9651 ));
9652 }
9653 }
9654 }
9655 return Err(error);
9656 }
9657
9658 let written_status = match (incoming.written.work_status(), status_target.as_ref()) {
9659 (Some((kind, _)), Some(StatusTarget::Terminal(_, reason))) => {
9660 self.statuses
9661 .status(kind, written_option.as_deref(), true, Some(reason.reason()))
9662 }
9663 (Some((kind, _)), Some(StatusTarget::Column(_))) => {
9664 self.statuses
9665 .status(kind, written_option.as_deref(), false, None)
9666 }
9667 (Some((_, status)), _) => status.clone(),
9668 (None, _) => Status {
9669 category: StatusCategory::Unknown,
9670 name: "Open".to_owned(),
9671 },
9672 };
9673
9674 // So the rest of this command reads what it just did rather than what the board
9675 // said before it. See `remember_written` for which half takes it.
9676 let remembered = Resolved {
9677 // Empty for an issue whose board item is not read yet: `finish_writes` reads it
9678 // and `learn_item` puts it here.
9679 // 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.
9680 item_id: item_id.unwrap_or_default(),
9681 id: content_id.clone(),
9682 content_kind,
9683 kind: incoming.written.kind(),
9684 title: incoming.title.to_owned(),
9685 // The visible half of the body this write composed, split back off it the
9686 // way a read splits it — so what this record reports is what a read of the
9687 // same issue reports, rather than the person's text with the metadata slot
9688 // still on the end of it.
9689 body: metadata_body(body.clone())?.0,
9690 raw_body: body.clone(),
9691 // A document has no status of its own; what it reads back as is whatever
9692 // the issue's own state says, which is what a re-read reports.
9693 status: written_status,
9694 option: written_option.or_else(|| existing.and_then(|item| item.option.clone())),
9695 priority: match incoming.priority {
9696 Some(priority) => HeldPriority::Read(priority),
9697 None => existing.map_or(HeldPriority::Read(Priority::None), |item| {
9698 item.priority.clone()
9699 }),
9700 },
9701 // What `state_input` asked for: closed for a terminal target, open for any other
9702 // status, and the issue's own state left as it was by a document write.
9703 closed: content_kind == ContentKind::Issue
9704 && match status_target.as_ref() {
9705 Some(StatusTarget::Terminal(_, _)) => true,
9706 Some(_) => false,
9707 None => existing.is_some_and(|item| item.closed),
9708 },
9709 reason: match status_target.as_ref() {
9710 Some(StatusTarget::Terminal(_, reason)) if content_kind == ContentKind::Issue => {
9711 Some(reason.reason().to_owned())
9712 }
9713 Some(_) => None,
9714 None => existing.and_then(|item| item.reason.clone()),
9715 },
9716 delivers: incoming.delivers.to_vec(),
9717 delivered_by: incoming.delivered_by.to_vec(),
9718 labels: incoming.labels.to_vec(),
9719 parent: incoming.parent.cloned(),
9720 origin: (!origin.is_empty()).then(|| origin.to_owned()),
9721 projected,
9722 number,
9723 // In the update path this is the item's own url, read off `existing` where the
9724 // record above was bound, so one expression serves both halves.
9725 url,
9726 created_at: existing.and_then(|item| item.created_at),
9727 updated_at: existing.and_then(|item| item.updated_at),
9728 // What this write moved is GitHub's to say, so a record of it gives no token until
9729 // it is read again.
9730 item_updated_at: None,
9731 comment_count: None,
9732 relationship_events: None,
9733 served_at: None,
9734 facets: None,
9735 own_repository,
9736 repositories: incoming.repositories.to_vec(),
9737 classification: incoming.classification,
9738 slot,
9739 board_id: Some(board.id.as_str().to_owned()),
9740 fields: board
9741 .fields
9742 .get("nodes")
9743 .and_then(Value::as_array)
9744 .cloned()
9745 .unwrap_or_default(),
9746 board_fields: Some(board.fields.clone()),
9747 // What this write left the relationship holding is known by id alone, and a
9748 // later read of its edges needs each far end's kind, so it reads them again.
9749 blocked_by: None,
9750 };
9751 self.remember_written(remembered, existing.is_none())?;
9752 Ok(onetaskgraph_plugin_api::AssetsWritten {
9753 id: content_id,
9754 content,
9755 })
9756 }
9757
9758 /// Everything a write does after the item exists: its board fields, its parent, and
9759 /// its dependencies.
9760 ///
9761 /// Split out of `write_item` so there is one place a failure past the point of no
9762 /// return is caught, rather than a tidy-up repeated at each `?` above.
9763 // llmlint: ignore[suppressions_justified] This is the tail of `write_item` lifted out
9764 // so there is one place a failure past the point of no return is caught, and its
9765 // arguments are exactly the values that tail already had in scope. Bundling them into a
9766 // struct would describe no concept — it would be "the arguments of this function" — and
9767 // would put the whole of `write_item`'s locals behind one more indirection.
9768 #[allow(clippy::too_many_arguments)]
9769 async fn finish_write(
9770 &self,
9771 board_id: &str,
9772 incoming: &Incoming<'_>,
9773 content_id: &NativeId,
9774 item_id: &str,
9775 content_kind: ContentKind,
9776 existing: Option<&Resolved>,
9777 origin_field: Option<&str>,
9778 origin: &str,
9779 column: Option<(String, String)>,
9780 status_target: Option<&StatusTarget>,
9781 priority: Option<&PriorityWrite>,
9782 projections: &[ProjectionWrite],
9783 native: &[String],
9784 origin_landed: &mut bool,
9785 ) -> Result<(), SourceError> {
9786 let (fields, clears) = field_writes(
9787 existing,
9788 origin_field,
9789 origin,
9790 column,
9791 priority,
9792 projections,
9793 );
9794 self.set_item_fields(board_id, item_id, &fields, &clears)
9795 .await?;
9796 *origin_landed = true;
9797
9798 // An existing issue closes in the `updateIssue` its write ends with; one created just
9799 // now closes here, once its option is selected.
9800 if existing.is_none()
9801 && content_kind == ContentKind::Issue
9802 && matches!(status_target, Some(StatusTarget::Terminal(_, _)))
9803 {
9804 self.update_content(
9805 ContentKind::Issue,
9806 content_id,
9807 json!({"stateInput":state_input(status_target)}),
9808 )
9809 .await?;
9810 }
9811
9812 if content_kind == ContentKind::Issue {
9813 self.reparent(
9814 existing.and_then(|item| item.parent.clone()),
9815 content_id,
9816 incoming.parent,
9817 )
9818 .await?;
9819 // A document takes part in no dependency graph, so writing one neither reads
9820 // nor changes the issue's own `blockedBy` relationships. Reconciling them
9821 // against the empty list a document write carries would *delete* whatever
9822 // relationships a person had made on that issue, which is a write nobody
9823 // asked for.
9824 if incoming.written.kind() != BoardKind::Document {
9825 let issue = match existing {
9826 Some(item) => Issue::Existing(item.blocked_by.as_deref()),
9827 None => Issue::Created,
9828 };
9829 self.reconcile_blocked_by(content_id, native, issue).await?;
9830 }
9831 }
9832 Ok(())
9833 }
9834
9835 /// Whether this source is holding writes back — see [`TaskSource::begin_deferred_writes`].
9836 fn deferring(&self) -> Result<bool, SourceError> {
9837 Ok(self.deferred()?.is_some())
9838 }
9839
9840 /// Whether `content` is an item this command created whose parts are held back.
9841 fn holds_back(&self, content: &NativeId) -> Result<bool, SourceError> {
9842 Ok(self
9843 .deferred()?
9844 .as_ref()
9845 .is_some_and(|deferred| deferred.holds(content)))
9846 }
9847
9848 fn deferred(&self) -> Result<std::sync::MutexGuard<'_, Option<Deferred>>, SourceError> {
9849 self.deferred.lock().map_err(|_| SourceError::Unavailable {
9850 message: "this source's record of the writes it is holding back was left \
9851 inconsistent by an earlier failure; next: run the command again"
9852 .into(),
9853 })
9854 }
9855
9856 /// Creates one issue filed on the board and under its project by the `createIssue` itself —
9857 /// `projectV2Ids` naming the board and `parentIssueId` the project — which is what a copy
9858 /// creates with: one request where adding it to the board and filing it under its project
9859 /// were two more. GitHub adds the board item after answering, so its id is not known here;
9860 /// [`TaskSource::finish_writes`] reads it.
9861 async fn create_filed_issue(
9862 &self,
9863 board_id: &str,
9864 repository: &RepositoryTarget,
9865 incoming: &Incoming<'_>,
9866 body: &Option<String>,
9867 ) -> Result<Landed, SourceError> {
9868 let repository_id = self.repository_id(repository, incoming).await?;
9869 let data = self
9870 .graphql(
9871 graphql::CREATE_ISSUE,
9872 json!({"input": filed_input(&repository_id, board_id, incoming, body)}),
9873 )
9874 .await?;
9875 let created = data
9876 .pointer("/createIssue/issue")
9877 .filter(|value| !value.is_null())
9878 .ok_or_else(|| SourceError::Malformed {
9879 message: "GitHub issue creation returned no issue".into(),
9880 })?;
9881 self.created_landed(created).await
9882 }
9883
9884 /// Keep the address `createIssue` answered for the issue `content`, for
9885 /// [`TaskSource::written_location`].
9886 fn note_written(&self, content: &NativeId, url: Option<&str>) {
9887 if let Some(url) = url {
9888 self.written
9889 .lock()
9890 .unwrap_or_else(std::sync::PoisonError::into_inner)
9891 .insert(content.clone(), url.to_owned());
9892 }
9893 }
9894
9895 /// What one `createIssue` answered, as the item it created — or, when its number cannot be
9896 /// read, the refusal, after taking the issue back.
9897 async fn created_landed(&self, created: &Value) -> Result<Landed, SourceError> {
9898 let content_id = NativeId(required_nonblank_str(created, "id")?.to_owned());
9899 let url = optional_str(created, "url")?.map(str::to_owned);
9900 let number = match created_issue_number(created) {
9901 Ok(number) => number,
9902 Err(error) => {
9903 let _ = self.delete_issue(&content_id).await;
9904 return Err(error);
9905 }
9906 };
9907 self.note_written(&content_id, url.as_deref());
9908 Ok(Landed {
9909 content_id,
9910 item_id: None,
9911 url,
9912 number,
9913 })
9914 }
9915
9916 /// Hold back everything a write of the created item `content` would send after creating it,
9917 /// for [`TaskSource::finish_writes`]: the same field values, closing and edges
9918 /// `finish_write` sends, the parent excepted, which its create already named. A later write
9919 /// of the same item replaces what it changes.
9920 // 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.
9921 #[allow(clippy::too_many_arguments)]
9922 fn hold_back(
9923 &self,
9924 board_id: &BoardId,
9925 content: &NativeId,
9926 existing: Option<&Resolved>,
9927 origin_field: Option<&str>,
9928 origin: &str,
9929 column: Option<(String, String)>,
9930 status_target: Option<&StatusTarget>,
9931 priority: Option<&PriorityWrite>,
9932 projections: &[ProjectionWrite],
9933 native: &[String],
9934 ) -> Result<(), SourceError> {
9935 let status = column.map(|(field, option)| {
9936 let name = self
9937 .fields_cache()
9938 .ok()
9939 .and_then(|fields| {
9940 fields.as_ref().and_then(|board| {
9941 option_name(&board.fields, &field, &option).map(str::to_owned)
9942 })
9943 })
9944 .unwrap_or_default();
9945 (field, option, name)
9946 });
9947 let (fields, clears) =
9948 field_writes(existing, origin_field, origin, None, priority, projections);
9949 let close = match status_target {
9950 Some(StatusTarget::Terminal(_, reason)) => Some(*reason),
9951 _ => None,
9952 };
9953 let mut deferred = self.deferred()?;
9954 let deferred = deferred.get_or_insert_with(Deferred::default);
9955 if let Deferred::Nothing = deferred {
9956 *deferred = Deferred::Held {
9957 board: board_id.clone(),
9958 created: Vec::new(),
9959 };
9960 }
9961 let Deferred::Held { created, .. } = deferred else {
9962 unreachable!("held back on the line above");
9963 };
9964 let held = match created.iter().position(|held| held.content == *content) {
9965 Some(at) => &mut created[at],
9966 None => {
9967 created.push(HeldBack {
9968 content: content.clone(),
9969 fields: Vec::new(),
9970 status: None,
9971 clears: Vec::new(),
9972 close: None,
9973 edges: Vec::new(),
9974 });
9975 created.last_mut().expect("just held")
9976 }
9977 };
9978 for (field, value) in fields {
9979 held.fields.retain(|(held, _)| *held != field);
9980 held.clears.retain(|held| *held != field);
9981 held.fields.push((field, value));
9982 }
9983 for field in clears {
9984 held.fields.retain(|(held, _)| held != field);
9985 if !held.clears.iter().any(|held| held == field) {
9986 held.clears.push(field.to_owned());
9987 }
9988 }
9989 if status.is_some() {
9990 held.status = status;
9991 }
9992 if close.is_some() {
9993 held.close = close;
9994 }
9995 // Nothing of an item created in this command has been sent but its create, so the edges
9996 // it is to have are exactly the ones its latest write names.
9997 held.edges = native.iter().map(|far| NativeId(far.clone())).collect();
9998 Ok(())
9999 }
10000
10001 /// Create every task of `writes`, together, filed on the board and under each one's
10002 /// project: [`graphql::CREATE_ISSUES`], [`MUTATION_BATCH`] to a request.
10003 ///
10004 /// Each is prepared as [`Self::write_item`] prepares one — every refusal a write owes made
10005 /// before the first request — then created, then completed with its parts held back. A
10006 /// request refused part way leaves the issues it did create, which are taken back, with the
10007 /// rest of this call's, before the refusal is answered.
10008 async fn create_together(
10009 &self,
10010 writes: &[ItemWrite<Task>],
10011 ) -> Result<Vec<NativeId>, SourceError> {
10012 let incomings: Vec<Incoming<'_>> = writes
10013 .iter()
10014 .map(|write| self.task_incoming(write, None))
10015 .collect::<Result<_, _>>()?;
10016 let mut prepared = Vec::with_capacity(writes.len());
10017 for (incoming, write) in incomings.iter().zip(writes) {
10018 if write.target.is_some() {
10019 return Err(SourceError::Malformed {
10020 message: "a write naming its target was handed to a batch of creates".into(),
10021 });
10022 }
10023 prepared.push(
10024 self.prepare_write(incoming, None, &write.depends_on)
10025 .await?,
10026 );
10027 }
10028 let mut inputs = Vec::with_capacity(prepared.len());
10029 for (incoming, prepared) in incomings.iter().zip(&prepared) {
10030 let WriteDestination::New(target) = &prepared.destination else {
10031 return Err(SourceError::Malformed {
10032 message: "an update was decided for a write handed to a batch of creates"
10033 .into(),
10034 });
10035 };
10036 let repository_id = self.repository_id(target, incoming).await?;
10037 inputs.push(filed_input(
10038 &repository_id,
10039 prepared.board.id.as_str(),
10040 incoming,
10041 &prepared.body,
10042 ));
10043 }
10044 let mut landed: Vec<Landed> = Vec::with_capacity(inputs.len());
10045 let refused = 'create: {
10046 for chunk in inputs.chunks(MUTATION_BATCH) {
10047 let mut variables = serde_json::Map::new();
10048 for slot in 0..MUTATION_BATCH {
10049 variables.insert(
10050 format!("create{slot}"),
10051 chunk.get(slot).cloned().unwrap_or_else(|| chunk[0].clone()),
10052 );
10053 variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10054 }
10055 let data = match self
10056 .graphql(graphql::CREATE_ISSUES, Value::Object(variables))
10057 .await
10058 {
10059 Ok(data) => data,
10060 Err(error) => break 'create Some(error),
10061 };
10062 let mut refusal = data.get(ANSWER_ERRORS).map(|errors| {
10063 let said: Vec<&str> = errors
10064 .as_array()
10065 .into_iter()
10066 .flatten()
10067 .filter_map(|error| error.get("message").and_then(Value::as_str))
10068 .collect();
10069 SourceError::Refused {
10070 message: if said.is_empty() {
10071 "GitHub returned GraphQL errors".to_owned()
10072 } else {
10073 said.join("; ")
10074 },
10075 }
10076 });
10077 for slot in 0..chunk.len() {
10078 match data
10079 .pointer(&format!("/create{slot}/issue"))
10080 .filter(|value| !value.is_null())
10081 {
10082 Some(created) => match self.created_landed(created).await {
10083 Ok(created) => landed.push(created),
10084 Err(error) => {
10085 refusal.get_or_insert(error);
10086 }
10087 },
10088 // An alias GitHub refused answers nothing, and the refusal says why.
10089 None => {
10090 refusal.get_or_insert(SourceError::Malformed {
10091 message: "GitHub issue creation returned no issue".into(),
10092 });
10093 }
10094 }
10095 }
10096 if let Some(error) = refusal {
10097 break 'create Some(error);
10098 }
10099 }
10100 None
10101 };
10102 if let Some(error) = refused {
10103 // None of these ids reaches the caller, so an issue GitHub will not take back is
10104 // named here or by nobody.
10105 let ids: Vec<&NativeId> = landed.iter().map(|created| &created.content_id).collect();
10106 let left = self.delete_issues(&ids).await;
10107 let left: Vec<&str> = left.iter().map(|id| id.0.as_str()).collect();
10108 return Err(left_behind(error, &left));
10109 }
10110 let created: Vec<NativeId> = landed
10111 .iter()
10112 .map(|landed| landed.content_id.clone())
10113 .collect();
10114 let mut ids = Vec::with_capacity(landed.len());
10115 for ((incoming, prepared), landed) in incomings.iter().zip(prepared).zip(landed) {
10116 match self.complete_write(incoming, prepared, landed).await {
10117 Ok(written) => ids.push(written.id),
10118 // 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.
10119 Err(error) => {
10120 let ids: Vec<&NativeId> = created.iter().collect();
10121 let left = self.delete_issues(&ids).await;
10122 let left: Vec<&str> = left.iter().map(|id| id.0.as_str()).collect();
10123 return Err(left_behind(error, &left));
10124 }
10125 }
10126 }
10127 Ok(ids)
10128 }
10129
10130 /// Hold back the update of the existing issue `issue` — `members`, what its own
10131 /// [`graphql::UPDATE_ISSUE`] would send beside its id — while writes are being held back — so the updates a copy makes one after
10132 /// another go to GitHub as one request. Whatever this source held of the issue is dropped
10133 /// as the mutation itself would drop it. Answers whether it was held: never while a write
10134 /// the store's cache expects runs, which records each mutation's own answer, and never for
10135 /// an issue an update is already held for, which that one is sent ahead of.
10136 fn hold_update(&self, issue: &NativeId, members: &Value) -> Result<bool, SourceError> {
10137 if self.expected.journaling() || self.deferred()?.is_none() {
10138 return Ok(false);
10139 }
10140 let mut held = self
10141 .held_updates
10142 .lock()
10143 .map_err(|_| SourceError::Unavailable {
10144 message: "issue updates held back were left inconsistent; run the command again"
10145 .into(),
10146 })?;
10147 if held.iter().any(|earlier| earlier.issue == *issue) {
10148 return Ok(false);
10149 }
10150 let named = |value: &str| issue.0 == value;
10151 self.resolved_cache()?
10152 .retain(|id, item| !named(&id.0) && !named(&item.item_id));
10153 self.read_ahead()?.retain(|id, item| {
10154 !named(&id.0) && !item.as_ref().is_some_and(|item| named(&item.item_id))
10155 });
10156 self.carried()?
10157 .retain(|id, item| !named(&id.0) && !named(&item.item_id));
10158 held.push(HeldUpdate::new(issue, members));
10159 Ok(true)
10160 }
10161
10162 /// Send every update [`Self::hold_update`] held back: one alone as [`graphql::UPDATE_ISSUE`]
10163 /// sends it, several as aliased `updateIssue`s of [`graphql::UPDATE_ISSUES`],
10164 /// [`MUTATION_BATCH`] to a request. Each counts one write, as it would alone.
10165 async fn send_held_updates(&self) -> Result<(), SourceError> {
10166 let held = std::mem::take(&mut *self.held_updates.lock().map_err(|_| {
10167 SourceError::Unavailable {
10168 message: "issue updates held back were left inconsistent; run the command again"
10169 .into(),
10170 }
10171 })?);
10172 if let [one] = held.as_slice() {
10173 return self
10174 .send_update(
10175 graphql::UPDATE_ISSUE,
10176 "/updateIssue/issue",
10177 &one.issue,
10178 one.input.clone(),
10179 )
10180 .await;
10181 }
10182 for chunk in held.chunks(MUTATION_BATCH) {
10183 let mut variables = serde_json::Map::new();
10184 for slot in 0..MUTATION_BATCH {
10185 variables.insert(
10186 format!("update{slot}"),
10187 chunk.get(slot).unwrap_or(&chunk[0]).input.clone(),
10188 );
10189 variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10190 }
10191 let data = self
10192 .graphql(graphql::UPDATE_ISSUES, Value::Object(variables))
10193 .await?;
10194 let inputs: Vec<&Value> = chunk.iter().map(|held| &held.input).collect();
10195 check_aliased_answers(
10196 graphql::UPDATE_ISSUES,
10197 &data,
10198 "update",
10199 &inputs,
10200 &[("issue", "id")],
10201 )?;
10202 }
10203 Ok(())
10204 }
10205
10206 /// Send everything held back since [`TaskSource::begin_deferred_writes`]: one read of the
10207 /// created items' board items, then their field values and edges together in requests of
10208 /// [`WRITE_BATCH`], then their closes in requests of [`MUTATION_BATCH`]. Anything refused
10209 /// takes back every issue held, [`DELETE_BATCH`] to a request, and answers the refusal.
10210 async fn send_held_back(&self) -> Result<(), SourceError> {
10211 let Some(Deferred::Held { board, created }) = self.deferred()?.take() else {
10212 return Ok(());
10213 };
10214 if created.is_empty() {
10215 return Ok(());
10216 }
10217 let sent = self.send_deferred(board.as_str(), &created).await;
10218 if let Err(error) = sent {
10219 // Best effort, and the failure is what the caller is told, as a write of one item
10220 // that fails part way takes back what it created: the copy that held these back
10221 // journalled every one, so one this cannot take back its undo tries again, and says
10222 // so when it cannot either.
10223 let ids: Vec<&NativeId> = created.iter().map(|held| &held.content).collect();
10224 let _ = self.delete_issues(&ids).await;
10225 return Err(error);
10226 }
10227 Ok(())
10228 }
10229
10230 /// The requests [`Self::send_held_back`] makes.
10231 async fn send_deferred(&self, board: &str, created: &[HeldBack]) -> Result<(), SourceError> {
10232 let items = self.created_board_items(board, created).await?;
10233 let mut fields: Vec<Value> = Vec::new();
10234 let mut closes: Vec<Value> = Vec::new();
10235 let mut edges: Vec<Value> = Vec::new();
10236 for held in created {
10237 let (item, status) = &items[&held.content];
10238 let mut values = held.fields.clone();
10239 // A status the board already shows — set by a workflow a person turned on to file
10240 // new items at it — is not written again.
10241 if let Some((field, option, name)) = &held.status
10242 && status.as_deref() != Some(name.as_str())
10243 {
10244 values.insert(0, (field.clone(), json!({"singleSelectOptionId": option})));
10245 }
10246 for (field, value) in values {
10247 fields.push(json!({"projectId":board,"itemId":item,"fieldId":field,"value":value}));
10248 }
10249 if let Some(reason) = held.close {
10250 closes.push(json!({"id": held.content.0, "stateInput": closed_input(reason)}));
10251 }
10252 for far in &held.edges {
10253 edges.push(json!({"issueId": held.content.0, "blockingIssueId": far.0}));
10254 }
10255 if !held.clears.is_empty() {
10256 let clears: Vec<&str> = held.clears.iter().map(String::as_str).collect();
10257 self.set_item_fields(board, item, &[], &clears).await?;
10258 }
10259 }
10260 // Each alias answers with what it wrote, which is what its input named: the board item a
10261 // field value is set on, the issue closed, the issue blocked and the one blocking it.
10262 let field_answer: &[(&str, &str)] = &[("projectV2Item", "itemId")];
10263 let close_answer: &[(&str, &str)] = &[("issue", "id")];
10264 let block_answer: &[(&str, &str)] =
10265 &[("issue", "issueId"), ("blockingIssue", "blockingIssueId")];
10266 // Every field value, then every edge, as one ordered list sent `WRITE_BATCH` aliases to a
10267 // request: a request holding both kinds is the one document carrying both, so a plan's
10268 // values and edges cost one request where they fit in one.
10269 let writes: Vec<HeldWrite<'_>> = fields
10270 .iter()
10271 .map(HeldWrite::Field)
10272 .chain(edges.iter().map(HeldWrite::Edge))
10273 .collect();
10274 for chunk in writes.chunks(WRITE_BATCH) {
10275 let values: Vec<&Value> = chunk
10276 .iter()
10277 .filter_map(|write| match write {
10278 HeldWrite::Field(input) => Some(*input),
10279 HeldWrite::Edge(_) => None,
10280 })
10281 .collect();
10282 let blocks: Vec<&Value> = chunk
10283 .iter()
10284 .filter_map(|write| match write {
10285 HeldWrite::Edge(input) => Some(*input),
10286 HeldWrite::Field(_) => None,
10287 })
10288 .collect();
10289 let (document, sends) = match (values.is_empty(), blocks.is_empty()) {
10290 (false, false) => (graphql::WRITE_FIELDS_AND_EDGES, ["sendField", "sendBlock"]),
10291 (false, true) => (graphql::WRITE_FIELD_VALUES, ["send", "send"]),
10292 _ => (graphql::ADD_BLOCKED_BYS, ["send", "send"]),
10293 };
10294 let mut variables = serde_json::Map::new();
10295 let kinds = [
10296 ("field", &values, sends[0], field_answer),
10297 ("block", &blocks, sends[1], block_answer),
10298 ];
10299 for (prefix, inputs, send, _) in &kinds {
10300 if inputs.is_empty() {
10301 continue;
10302 }
10303 for slot in 0..WRITE_BATCH {
10304 variables.insert(
10305 format!("{prefix}{slot}"),
10306 (*inputs.get(slot).unwrap_or(&inputs[0])).clone(),
10307 );
10308 variables.insert(format!("{send}{slot}"), json!(slot < inputs.len()));
10309 }
10310 }
10311 let data = self.graphql(document, Value::Object(variables)).await?;
10312 for (prefix, inputs, _, answers) in kinds {
10313 check_aliased_answers(document, &data, prefix, inputs, answers)?;
10314 }
10315 }
10316 for chunk in closes.chunks(MUTATION_BATCH) {
10317 let mut variables = serde_json::Map::new();
10318 for slot in 0..MUTATION_BATCH {
10319 variables.insert(
10320 format!("close{slot}"),
10321 chunk.get(slot).cloned().unwrap_or_else(|| chunk[0].clone()),
10322 );
10323 variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10324 }
10325 let data = self
10326 .graphql(graphql::CLOSE_ISSUES, Value::Object(variables))
10327 .await?;
10328 let inputs: Vec<&Value> = chunk.iter().collect();
10329 check_aliased_answers(graphql::CLOSE_ISSUES, &data, "close", &inputs, close_answer)?;
10330 }
10331 for held in created {
10332 self.learn_item(&held.content, &items[&held.content].0)?;
10333 }
10334 Ok(())
10335 }
10336
10337 /// The board item each of `created` is on this board, with its `Status` as the board set it:
10338 /// [`graphql::CREATED_BOARD_ITEMS`], read again — after a wait of a second, then two, four,
10339 /// eight and sixteen — for only those GitHub has not filed yet.
10340 ///
10341 /// **Membership is never assumed.** `createIssue`'s `projectV2Ids` files an issue some time
10342 /// after GitHub answered the create — the feasibility spike saw 0.45 to 1.84 seconds, and no
10343 /// create answer carrying the item — so only a read says it is there. One still absent once
10344 /// those reads are spent is filed with [`graphql::ADD_TO_BOARD`], one write each; GitHub
10345 /// filing it meanwhile refuses that "Content already exists in this project", which says it
10346 /// is placed, and one more read finds the item rather than a second one being made.
10347 async fn created_board_items(
10348 &self,
10349 board: &str,
10350 created: &[HeldBack],
10351 ) -> Result<BTreeMap<NativeId, (String, Option<String>)>, SourceError> {
10352 let mut found: BTreeMap<NativeId, (String, Option<String>)> = BTreeMap::new();
10353 let unfiled = |found: &BTreeMap<NativeId, (String, Option<String>)>| -> Vec<NativeId> {
10354 created
10355 .iter()
10356 .map(|held| held.content.clone())
10357 .filter(|id| !found.contains_key(id))
10358 .collect()
10359 };
10360 let mut wait = Duration::from_secs(1);
10361 for attempt in 0..6 {
10362 let absent = unfiled(&found);
10363 if absent.is_empty() {
10364 return Ok(found);
10365 }
10366 if attempt > 0 {
10367 self.clock
10368 .sleep_because(wait, pacing::cause::BOARD_FILING)
10369 .await;
10370 wait = wait.saturating_mul(2);
10371 }
10372 self.read_created_items(board, &absent, &mut found).await?;
10373 }
10374 let mut placed_meanwhile = Vec::new();
10375 for id in unfiled(&found) {
10376 match self
10377 .graphql(
10378 graphql::ADD_TO_BOARD,
10379 json!({"input":{"projectId":board,"contentId":id.0}}),
10380 )
10381 .await
10382 {
10383 Ok(added) => {
10384 let item = added
10385 .pointer("/addProjectV2ItemById/item")
10386 .filter(|value| !value.is_null())
10387 .ok_or_else(|| SourceError::Malformed {
10388 message: "GitHub board addition returned no project item".into(),
10389 })?;
10390 found.insert(id, (required_nonblank_str(item, "id")?.to_owned(), None));
10391 }
10392 Err(error) if already_on_the_board(&error) => placed_meanwhile.push(id),
10393 Err(error) => return Err(error),
10394 }
10395 }
10396 if !placed_meanwhile.is_empty() {
10397 self.read_created_items(board, &placed_meanwhile, &mut found)
10398 .await?;
10399 }
10400 let absent = unfiled(&found);
10401 if absent.is_empty() {
10402 return Ok(found);
10403 }
10404 let absent: Vec<&str> = absent.iter().map(|id| id.0.as_str()).collect();
10405 Err(SourceError::Unavailable {
10406 message: format!(
10407 "GitHub created {} filed on board {board} and, after 31 seconds and adding {} \
10408 to it, still answers {} on no item of it: {}; next: run the copy again once \
10409 GitHub has caught up",
10410 if absent.len() == 1 {
10411 "an issue"
10412 } else {
10413 "issues"
10414 },
10415 if absent.len() == 1 { "it" } else { "them" },
10416 if absent.len() == 1 { "it" } else { "them" },
10417 absent.join(", ")
10418 ),
10419 })
10420 }
10421
10422 /// One [`graphql::CREATED_BOARD_ITEMS`] read of each of `ids`, [`COPY_DETAIL_BATCH`] to a
10423 /// request, adding to `found` the board item of every one GitHub has filed on `board`.
10424 async fn read_created_items(
10425 &self,
10426 board: &str,
10427 ids: &[NativeId],
10428 found: &mut BTreeMap<NativeId, (String, Option<String>)>,
10429 ) -> Result<(), SourceError> {
10430 for chunk in ids.chunks(COPY_DETAIL_BATCH) {
10431 let mut variables = serde_json::Map::new();
10432 for slot in 0..COPY_DETAIL_BATCH {
10433 variables.insert(
10434 format!("id{slot}"),
10435 json!(chunk.get(slot).unwrap_or(&chunk[0]).0),
10436 );
10437 variables.insert(format!("read{slot}"), json!(slot < chunk.len()));
10438 }
10439 variables.insert("boardItems".into(), json!(BOARD_ITEMS_PAGE_SIZE));
10440 variables.insert("statusName".into(), json!(STATUS_FIELD));
10441 let data = self
10442 .graphql(graphql::CREATED_BOARD_ITEMS, Value::Object(variables))
10443 .await?;
10444 for (slot, id) in chunk.iter().enumerate() {
10445 // An answer for another issue than the one asked about says nothing of
10446 // where that one is filed, and its board item is not this one's.
10447 let node = data.get(format!("i{slot}")).filter(|node| !node.is_null());
10448 let named = node.map(|node| node.get("id").and_then(Value::as_str));
10449 if let Some(named) = named
10450 && named != Some(id.0.as_str())
10451 {
10452 return Err(SourceError::Malformed {
10453 message: format!(
10454 "GitHub answered the board items of {} when asked for those of {}",
10455 named.unwrap_or("an issue it did not name"),
10456 id.0
10457 ),
10458 });
10459 }
10460 // An issue GitHub does not answer yet is not filed yet; one it answers is
10461 // read whole, its connection, each board item's board and its status, and
10462 // any of them unreadable is refused rather than read as not filed.
10463 let Some(node) = node else { continue };
10464 let malformed = |what: &str| SourceError::Malformed {
10465 message: format!("GitHub answered the board items of {} with {what}", id.0),
10466 };
10467 let nodes = node
10468 .pointer("/projectItems/nodes")
10469 .and_then(Value::as_array)
10470 .ok_or_else(|| malformed("no list of board items"))?;
10471 let mut item = None;
10472 for node in nodes {
10473 let on = node
10474 .pointer("/project/id")
10475 .and_then(Value::as_str)
10476 .ok_or_else(|| malformed("a board item naming no board"))?;
10477 if on == board {
10478 item = Some(node);
10479 }
10480 }
10481 if let Some(item) = item {
10482 let status = match item.get("status") {
10483 None | Some(Value::Null) => None,
10484 Some(status) => Some(
10485 status
10486 .get("name")
10487 .and_then(Value::as_str)
10488 .ok_or_else(|| malformed("a status that names no option"))?
10489 .to_owned(),
10490 ),
10491 };
10492 found.insert(
10493 id.clone(),
10494 (required_nonblank_str(item, "id")?.to_owned(), status),
10495 );
10496 }
10497 }
10498 }
10499 Ok(())
10500 }
10501
10502 /// The board item id of `content`, which this process created and remembered without one,
10503 /// in every half of its own view.
10504 fn learn_item(&self, content: &NativeId, item: &str) -> Result<(), SourceError> {
10505 if let Some(held) = self.resolved_cache()?.get_mut(content) {
10506 held.item_id = item.to_owned();
10507 }
10508 if let Some(held) = self.created()?.iter_mut().find(|held| held.id == *content) {
10509 held.item_id = item.to_owned();
10510 }
10511 Ok(())
10512 }
10513
10514 /// Ask together which items each of `origins` names, and what creating an item reads; see
10515 /// [`TaskSource::read_ahead_origins_for_copy`]. An origin whose answer runs past its first
10516 /// page is left to its own lookup.
10517 async fn origins_ahead(&self, origins: &[String]) -> Result<(), SourceError> {
10518 if self.repository.is_none() {
10519 return Ok(());
10520 }
10521 let unasked: Vec<&String> = {
10522 let held = self.narrowed_cache()?;
10523 let mut unasked: Vec<&String> = Vec::new();
10524 for origin in origins {
10525 if !held.contains_key(&Narrowing::Origin(origin.clone()).key())
10526 && !unasked.contains(&origin)
10527 {
10528 unasked.push(origin);
10529 }
10530 }
10531 unasked
10532 };
10533 if unasked.is_empty() {
10534 return Ok(());
10535 }
10536 // One batch is held back for the read ahead that follows, which may carry a project's
10537 // members beside it in the same request.
10538 if unasked.len() <= ORIGIN_BATCH {
10539 let mut pending =
10540 self.pending_origins
10541 .lock()
10542 .map_err(|_| SourceError::Unavailable {
10543 message:
10544 "origins held for a read ahead were left inconsistent; run the command \
10545 again"
10546 .into(),
10547 })?;
10548 for origin in unasked {
10549 if !pending.contains(origin) {
10550 pending.push(origin.clone());
10551 }
10552 }
10553 return Ok(());
10554 }
10555 let unasked: Vec<String> = unasked.into_iter().cloned().collect();
10556 self.send_origin_lookups(&unasked, None).await
10557 }
10558
10559 /// Hold, for [`TaskSource::member_classifications`] of `project`, each member's
10560 /// classification and repositories as the members page `data` carries them — when that page
10561 /// is every member, each one names `project` as its parent, and every record it holds is
10562 /// readable. Any other page holds nothing, so the members are read as they always were.
10563 fn hold_member_classes(&self, project: &NativeId, data: &Value) -> Result<(), SourceError> {
10564 let Some(page) = data
10565 .pointer("/members/subIssues")
10566 .filter(|page| !page.is_null())
10567 else {
10568 return Ok(());
10569 };
10570 if page
10571 .pointer("/pageInfo/hasNextPage")
10572 .and_then(Value::as_bool)
10573 != Some(false)
10574 {
10575 return Ok(());
10576 }
10577 let Some(nodes) = page.get("nodes").and_then(Value::as_array) else {
10578 return Ok(());
10579 };
10580 let mut members = Vec::with_capacity(nodes.len());
10581 for node in nodes {
10582 if node.pointer("/parent/id").and_then(Value::as_str) != Some(project.0.as_str()) {
10583 return Ok(());
10584 }
10585 let Some(body) = node.get("body").and_then(Value::as_str) else {
10586 return Ok(());
10587 };
10588 // Every node here is filed under `project`, which `BoardKind::of` reads as one of its
10589 // tasks whatever its marker says, so each is a member the full read would classify.
10590 let Ok((_, slot)) = metadata_body(Some(body.to_owned())) else {
10591 return Ok(());
10592 };
10593 let Ok(classification) = Classification::from_metadata(&slot) else {
10594 return Ok(());
10595 };
10596 let repositories = if slot.contains_key(Repository::METADATA_KEY) {
10597 let Ok(repositories) = Repository::from_metadata(&slot) else {
10598 return Ok(());
10599 };
10600 repositories
10601 } else {
10602 let Some(origin) = node
10603 .pointer("/repository/nameWithOwner")
10604 .and_then(Value::as_str)
10605 else {
10606 return Ok(());
10607 };
10608 let Ok(own) = Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST))
10609 else {
10610 return Ok(());
10611 };
10612 vec![own]
10613 };
10614 members.push((classification, repositories));
10615 }
10616 self.member_classes
10617 .lock()
10618 .map_err(|_| SourceError::Unavailable {
10619 message: "a project's members held for its classification were left \
10620 inconsistent; run the command again"
10621 .into(),
10622 })?
10623 .insert(project.clone(), members);
10624 Ok(())
10625 }
10626
10627 /// Send every origin held back for the read ahead, beside the members of `members` when it
10628 /// names a project — or nothing, when none is held.
10629 async fn send_pending_origins(&self, members: Option<&NativeId>) -> Result<(), SourceError> {
10630 let pending = std::mem::take(&mut *self.pending_origins.lock().map_err(|_| {
10631 SourceError::Unavailable {
10632 message: "origins held for a read ahead were left inconsistent; run the command \
10633 again"
10634 .into(),
10635 }
10636 })?);
10637 if pending.is_empty() {
10638 return Ok(());
10639 }
10640 self.send_origin_lookups(&pending, members).await
10641 }
10642
10643 /// [`graphql::ORIGIN_LOOKUPS`] of `unasked`, [`ORIGIN_BATCH`] to a request — the first one
10644 /// [`graphql::ORIGIN_LOOKUPS_WITH_MEMBERS`], carrying the members of `members`, when it names
10645 /// a project — holding each origin's answer and, from a members page that is the whole of
10646 /// them, each member's classification.
10647 async fn send_origin_lookups(
10648 &self,
10649 unasked: &[String],
10650 members: Option<&NativeId>,
10651 ) -> Result<(), SourceError> {
10652 let Some(repository) = self.repository.clone() else {
10653 return Ok(());
10654 };
10655 let mut members = members;
10656 for chunk in unasked.chunks(ORIGIN_BATCH) {
10657 let mut variables = json!({"owner":self.owner,"number":self.project_number,
10658 "type":"ISSUE","lookupFirst":ORIGIN_LOOKUPS_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
10659 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true,
10660 "repositoryOwner":repository.owner,"repositoryName":repository.name});
10661 for slot in 0..ORIGIN_BATCH {
10662 let origin = chunk.get(slot).unwrap_or(&chunk[0]);
10663 variables[format!("filter{slot}")] =
10664 json!(format!("{ORIGIN_FIELD}:{}", quoted(origin)));
10665 variables[format!("search{slot}")] = json!(
10666 self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))))
10667 );
10668 variables[format!("ask{slot}")] = json!(slot < chunk.len());
10669 }
10670 let project = members.take();
10671 let document = match project {
10672 Some(project) => {
10673 variables["project"] = json!(project.0);
10674 variables["memberFirst"] = json!(MEMBER_PAGE_SIZE);
10675 graphql::ORIGIN_LOOKUPS_WITH_MEMBERS
10676 }
10677 None => graphql::ORIGIN_LOOKUPS,
10678 };
10679 let data = self.graphql(document, variables).await?;
10680 self.fields_read(&data)?;
10681 if let Some(project) = project {
10682 self.hold_member_classes(project, &data)?;
10683 }
10684 if let Some(node) = data.get("repository").filter(|value| !value.is_null()) {
10685 let id = required_nonblank_str(node, "id")?.to_owned();
10686 self.repository_cache()?.insert(repository.clone(), id);
10687 if let Some(numeric) = node
10688 .get("databaseId")
10689 .and_then(Value::as_u64)
10690 .and_then(std::num::NonZeroU64::new)
10691 {
10692 self.numeric_repositories
10693 .lock()
10694 .await
10695 .insert(repository.clone(), numeric);
10696 }
10697 }
10698 for (slot, origin) in chunk.iter().enumerate() {
10699 let items = data
10700 .pointer(&format!("/o{slot}/projectV2/items"))
10701 .filter(|value| !value.is_null())
10702 .ok_or_else(|| SourceError::Refused {
10703 message: format!(
10704 "GitHub project {}/{} was not found or is not visible to the token",
10705 self.owner, self.project_number
10706 ),
10707 })?;
10708 let searched =
10709 data.get(format!("s{slot}"))
10710 .ok_or_else(|| SourceError::Malformed {
10711 message: "GitHub search response has no search connection".into(),
10712 })?;
10713 if resumed(items, None)?.has_more() || resumed(searched, None)?.has_more() {
10714 continue;
10715 }
10716 let mut found: Vec<Resolved> = Vec::new();
10717 for item in optional_nodes(Some(items), "project items")?
10718 .into_iter()
10719 .flatten()
10720 {
10721 if let Some(resolved) = self.resolve(item)?
10722 && resolved.content_kind == ContentKind::Issue
10723 && !found.iter().any(|held| held.id == resolved.id)
10724 {
10725 found.push(resolved);
10726 }
10727 }
10728 for node in optional_nodes(Some(searched), "search")?
10729 .into_iter()
10730 .flatten()
10731 {
10732 if let Some(resolved) = self.resolve_issue(node).await?
10733 && !found.iter().any(|held| held.id == resolved.id)
10734 {
10735 found.push(resolved);
10736 }
10737 }
10738 self.narrowed_cache()?
10739 .insert(Narrowing::Origin((*origin).clone()).key(), found);
10740 }
10741 }
10742 Ok(())
10743 }
10744
10745 /// Delete every issue of `ids`, [`DELETE_BATCH`] to a request with
10746 /// [`graphql::DELETE_ISSUES`], and answer those still there: each a request's answer does not
10747 /// confirm, every one of a request refused whole included.
10748 async fn delete_issues(&self, ids: &[&NativeId]) -> Vec<NativeId> {
10749 let mut left = Vec::new();
10750 for chunk in ids.chunks(DELETE_BATCH) {
10751 let mut variables = serde_json::Map::new();
10752 for slot in 0..DELETE_BATCH {
10753 let id = chunk.get(slot).unwrap_or(&chunk[0]);
10754 variables.insert(format!("delete{slot}"), json!({"issueId": id.0}));
10755 variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10756 }
10757 let data = self
10758 .graphql(graphql::DELETE_ISSUES, Value::Object(variables))
10759 .await
10760 .unwrap_or(Value::Null);
10761 for (slot, id) in chunk.iter().enumerate() {
10762 // Confirmed only by the repository the deleted issue was in, named by a
10763 // non-blank id: anything else says nothing of whether the issue is gone.
10764 let confirmed = data
10765 .pointer(&format!("/delete{slot}/repository/id"))
10766 .and_then(Value::as_str)
10767 .is_some_and(|id| !id.trim().is_empty());
10768 if !confirmed {
10769 left.push((*id).clone());
10770 }
10771 }
10772 }
10773 left
10774 }
10775
10776 /// Delete one issue, which takes its board item with it.
10777 async fn delete_issue(&self, id: &NativeId) -> Result<(), SourceError> {
10778 let data = self
10779 .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
10780 .await?;
10781 data.pointer("/deleteIssue/repository")
10782 .filter(|value| !value.is_null())
10783 .ok_or_else(|| SourceError::Malformed {
10784 message: "GitHub issue deletion returned no repository".into(),
10785 })?;
10786 self.forget(id)?;
10787 Ok(())
10788 }
10789
10790 /// Remove one item this copy created, so a copy that could not finish leaves the board
10791 /// as it found it.
10792 ///
10793 /// Deleting the issue takes its board item with it, so there is no second mutation to
10794 /// keep in step. An id the board does not hold is not an error: the item is already
10795 /// gone, which is the state this asks for. Which that is, is decided by reading the item
10796 /// by its own id — a listing of the board can still be missing an item it holds, and
10797 /// reading that as *already gone* would leave behind the very item this was asked to
10798 /// take back.
10799 async fn delete_item(&self, id: &NativeId) -> Result<(), SourceError> {
10800 let Some(item) = self.bound_item(id).await? else {
10801 return Ok(());
10802 };
10803 if item.content_kind == ContentKind::DraftIssue {
10804 return Err(SourceError::Refused {
10805 message: format!(
10806 "GitHub item {} is a draft, and this source removes an item by deleting \
10807 its issue; next: remove it from the board by hand",
10808 id.0
10809 ),
10810 });
10811 }
10812 let data = self
10813 .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
10814 .await?;
10815 data.pointer("/deleteIssue/repository")
10816 .filter(|value| !value.is_null())
10817 .ok_or_else(|| SourceError::Malformed {
10818 message: "GitHub issue deletion returned no repository".into(),
10819 })?;
10820 self.forget(id)?;
10821 Ok(())
10822 }
10823
10824 /// The issue a comment call on `task` is about, or `None` when this board holds no such
10825 /// task.
10826 ///
10827 /// Resolved exactly as [`TaskSource::get_task`] resolves it, so the comment verbs and a
10828 /// read of the task cannot disagree about which ids name one: a project or a document of
10829 /// this board is not a task here either.
10830 ///
10831 /// A **draft** is a task with nowhere to keep a comment, because GitHub keeps comments on
10832 /// issues and a draft is not one. It is refused rather than answered with an empty page,
10833 /// which would read as a task nobody has commented on yet.
10834 async fn commented_issue(&self, task: &NativeId) -> Result<Option<NativeId>, SourceError> {
10835 let cached = self.resolved_cache()?.get(task).cloned();
10836 let Some(item) = (match cached {
10837 Some(item) => Some(item),
10838 None => self.item_by_id(task).await?,
10839 })
10840 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
10841 return Ok(None);
10842 };
10843 if item.content_kind == ContentKind::DraftIssue {
10844 return Err(self.draft_has_no_comments(task));
10845 }
10846 Ok(Some(item.id))
10847 }
10848
10849 /// The refusal a comment call on a board draft is answered with: GitHub keeps comments on
10850 /// issues, and a draft is not one.
10851 fn draft_has_no_comments(&self, task: &NativeId) -> SourceError {
10852 SourceError::Refused {
10853 message: format!(
10854 "task {} of source {} is a draft item on the board, and GitHub keeps \
10855 comments on issues alone, so a draft has none to read or write; next: \
10856 convert the draft to an issue on the board, then comment on the issue it \
10857 becomes",
10858 task.0, self.name
10859 ),
10860 }
10861 }
10862
10863 /// One task and a page of its comments, read with [`graphql::ISSUE_DETAIL`] in one
10864 /// request — or `None` when this board holds no task by that id.
10865 ///
10866 /// What `task show` and a comment listing read. A draft is a task with no comments, so it
10867 /// is answered with the draft and the refusal, at the price of the draft's own read.
10868 async fn issue_detail(
10869 &self,
10870 id: &NativeId,
10871 page: &PageRequest,
10872 ) -> Result<Option<TaskDetailRead>, SourceError> {
10873 let after = page.cursor.as_ref().map(|cursor| cursor.0.as_str());
10874 let asked = self
10875 .graphql(
10876 graphql::ISSUE_DETAIL,
10877 json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),"after":after,
10878 "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
10879 "duplicates":true}),
10880 )
10881 .await;
10882 let data = match asked {
10883 Ok(data) => data,
10884 Err(error) if unresolvable_node(&error) => return Ok(None),
10885 Err(error) => return Err(error),
10886 };
10887 // `node` is null for an id that names nothing, and absent only from an answer this
10888 // source cannot read — never the same thing.
10889 let node = data.get("node").ok_or_else(|| SourceError::Malformed {
10890 message: format!("GitHub answered the read of {} with no node", id.0),
10891 })?;
10892 self.detail_of(id, node, true, after).await
10893 }
10894
10895 /// Several tasks, each with the first page of its comments when `comments` is set, read
10896 /// [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] — one answer per id, in
10897 /// order.
10898 ///
10899 /// A batch GitHub refuses because one of its ids resolves to no node at all is read again
10900 /// one item at a time, so that id is answered as missing and the others as themselves; any
10901 /// other refusal is every id of that batch's answer.
10902 async fn issue_details(
10903 &self,
10904 ids: &[NativeId],
10905 comments: Option<&PageRequest>,
10906 ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
10907 let mut read = Vec::with_capacity(ids.len());
10908 for batch in ids.chunks(DETAIL_BATCH) {
10909 match self
10910 .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, comments))
10911 .await
10912 {
10913 Ok(data) => {
10914 for (slot, id) in batch.iter().enumerate() {
10915 // Every alias asked for is answered, null for an id naming nothing;
10916 // one missing is an answer this source cannot read.
10917 let read_one = match data.get(format!("i{slot}")) {
10918 Some(node) => self.detail_of(id, node, comments.is_some(), None).await,
10919 None => Err(SourceError::Malformed {
10920 message: format!(
10921 "GitHub answered a batch read with no item for {}",
10922 id.0
10923 ),
10924 }),
10925 };
10926 read.push(read_one);
10927 }
10928 }
10929 Err(error) if unresolvable_node(&error) => {
10930 for id in batch {
10931 read.push(match comments {
10932 Some(page) => self.issue_detail(id, page).await,
10933 None => self.task_read(id).await,
10934 });
10935 }
10936 }
10937 Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
10938 }
10939 }
10940 read
10941 }
10942
10943 /// One task and nothing of its comments, as [`TaskSource::get_task`] reads it.
10944 async fn task_read(&self, id: &NativeId) -> Result<Option<TaskDetailRead>, SourceError> {
10945 Ok(self.get_task(id).await?.map(|task| TaskDetailRead {
10946 task,
10947 comments: None,
10948 }))
10949 }
10950
10951 /// What one node a detail read reached says: the task this board holds by `id`, with the
10952 /// page of comments the node carries when `commented` — or `None` for a node that is no
10953 /// task of this board.
10954 ///
10955 /// Resolved as [`Self::item_by_id`] resolves an item: a draft is read again as a draft,
10956 /// and an item this process created answers from this process's own record, which a node
10957 /// read taken moments after the write can still be behind.
10958 async fn detail_of(
10959 &self,
10960 id: &NativeId,
10961 node: &Value,
10962 commented: bool,
10963 after: Option<&str>,
10964 ) -> Result<Option<TaskDetailRead>, SourceError> {
10965 if node.is_null() {
10966 return Ok(None);
10967 }
10968 let draft = optional_str(node, "__typename")? == Some("DraftIssue");
10969 // An issue answered under one id is that id's, or the answer is not one this source
10970 // can report: reporting another issue's task and comments under the qualified id asked
10971 // for would be the one wrong answer here. A draft's own read checks the same.
10972 if !draft
10973 && optional_str(node, "__typename")? == Some("Issue")
10974 && required_str(node, "id")? != id.0
10975 {
10976 return Err(SourceError::Malformed {
10977 message: format!(
10978 "GitHub answered the read of {} with issue {}",
10979 id.0,
10980 required_str(node, "id")?
10981 ),
10982 });
10983 }
10984 let item = if draft {
10985 self.draft_by_id(id).await?
10986 } else {
10987 self.resolve_issue(node).await?
10988 };
10989 // A read that carried the whole of what the issue is blocked by — a copy's — is kept
10990 // for this command, so the dependency read the copy makes next is answered from it.
10991 if let Some(read) = item.as_ref().filter(|item| item.blocked_by.is_some()) {
10992 self.resolved_cache()?.insert(read.id.clone(), read.clone());
10993 }
10994 let Some(item) = item.filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
10995 return Ok(None);
10996 };
10997 let own = self.created()?.iter().find(|own| own.id == *id).cloned();
10998 let task = own.unwrap_or(item).task()?;
10999 let comments = match (commented, draft) {
11000 (false, _) => None,
11001 (true, true) => Some(Err(self.draft_has_no_comments(id))),
11002 (true, false) => Some(comment_page(node, &id.0, after).map(Some)),
11003 };
11004 if commented && !draft && after.is_none() {
11005 self.expected.learn_comments(id, node);
11006 }
11007 Ok(Some(TaskDetailRead { task, comments }))
11008 }
11009
11010 /// Whether the comment `comment` is one of `issue`'s own.
11011 ///
11012 /// Read before an edit or a removal is sent, because GitHub's comment mutations take the
11013 /// comment's id and nothing else: a comment id given against the wrong task would
11014 /// otherwise change a comment on some other issue entirely. An id that names nothing, or
11015 /// names something that is not an issue comment, is a comment this task does not have —
11016 /// which is what GitHub refusing to resolve it means too.
11017 async fn comment_is_on(
11018 &self,
11019 issue: &NativeId,
11020 comment: &NativeId,
11021 ) -> Result<bool, SourceError> {
11022 Ok(self.comment_on(issue, comment).await?.is_some())
11023 }
11024
11025 /// The body the comment `comment` holds, exactly as GitHub holds it, when it is one of
11026 /// `issue`'s own — the read [`Self::comment_is_on`] makes, answering with what it selected.
11027 async fn comment_on(
11028 &self,
11029 issue: &NativeId,
11030 comment: &NativeId,
11031 ) -> Result<Option<String>, SourceError> {
11032 // A write the store's cache expects knows which comments its issue held, and their
11033 // bodies, at the version it expects: what this read would answer.
11034 if let Some(primed) = self.expected.primed_comment(issue, comment) {
11035 return Ok(primed);
11036 }
11037 let asked = self
11038 .graphql(graphql::COMMENT_ISSUE, json!({"id":comment.0}))
11039 .await;
11040 let data = match asked {
11041 Ok(data) => data,
11042 Err(error) if unresolvable_node(&error) => return Ok(None),
11043 Err(error) => return Err(error),
11044 };
11045 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
11046 return Ok(None);
11047 };
11048 if optional_str(node, "__typename")? != Some("IssueComment") {
11049 return Ok(None);
11050 }
11051 let on = node.get("issue").ok_or_else(|| SourceError::Malformed {
11052 message: format!("GitHub issue comment {} names no issue", comment.0),
11053 })?;
11054 if required_str(on, "id")? != issue.0 {
11055 return Ok(None);
11056 }
11057 Ok(Some(required_str(node, "body")?.to_owned()))
11058 }
11059
11060 /// Which far ends this item's own `blockedBy` relationship holds, and which it cannot.
11061 async fn partition_edges(
11062 &self,
11063 near_kind: BoardKind,
11064 near_content: ContentKind,
11065 carried: Option<&[Value]>,
11066 depends_on: &[DependencyEdge],
11067 ) -> Result<(Vec<String>, Vec<DependencyEdge>), SourceError> {
11068 let mut native = Vec::new();
11069 let mut fallback = Vec::new();
11070 let far_ends: Vec<(&DependencyEdge, &str, bool, Option<&Value>)> = depends_on
11071 .iter()
11072 .map(|edge| {
11073 // An opaque far end names another tracker, so it is never this board's own.
11074 let same_source = edge.to.designator().is_none()
11075 && edge
11076 .to
11077 .source()
11078 .is_none_or(|source| source == self.name.as_str());
11079 // A qualified id's source segment runs to its *first* colon — `GlobalId` and
11080 // `DependencyEndpoint::source` both read it that way — and a native id may hold
11081 // colons of its own, so the far end is everything after that one separator.
11082 // Splitting at the last would truncate `work:urn:task:7` to `7`.
11083 let far_id = if edge.to.is_qualified() {
11084 edge.to
11085 .id()
11086 .split_once(':')
11087 .map_or(edge.to.id(), |(_, native)| native)
11088 } else {
11089 edge.to.id()
11090 };
11091 // One that already blocks the near issue was answered by that issue's own
11092 // read, which carried each of its blockers' kinds — an issue every one — so it
11093 // is not read again.
11094 let blocking = carried.and_then(|nodes| {
11095 nodes
11096 .iter()
11097 .find(|node| node.get("id").and_then(Value::as_str) == Some(far_id))
11098 });
11099 (edge, far_id, same_source, blocking)
11100 })
11101 .collect();
11102 // Every other same-source far end is read by its own id, exactly as the item it is a
11103 // far end of is: whether this board holds it is that read's answer, never a listing's.
11104 // They are read together, [`DETAIL_BATCH`] to a request, rather than one each.
11105 let mut unread: Vec<NativeId> = Vec::new();
11106 for (_, far_id, same_source, blocking) in &far_ends {
11107 let id = NativeId((*far_id).to_owned());
11108 if *same_source && blocking.is_none() && !unread.contains(&id) {
11109 unread.push(id);
11110 }
11111 }
11112 let read: BTreeMap<NativeId, Option<Resolved>> = unread
11113 .iter()
11114 .cloned()
11115 .zip(self.items_by_ids(&unread).await?)
11116 .collect();
11117 for (edge, far_id, same_source, blocking) in far_ends {
11118 let far = match (same_source, blocking) {
11119 (false, _) => None,
11120 (true, Some(node)) => Some(FarEnd {
11121 kind: if required_str(node, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
11122 BoardKind::Document
11123 } else {
11124 BoardKind::Work(related_kind(node)?)
11125 },
11126 content_kind: ContentKind::Issue,
11127 }),
11128 (true, None) => {
11129 let read = read
11130 .get(&NativeId(far_id.to_owned()))
11131 .cloned()
11132 .flatten()
11133 .ok_or_else(|| SourceError::Refused {
11134 message: format!("GitHub dependency item {far_id} was not found"),
11135 })?;
11136 Some(FarEnd {
11137 kind: read.kind,
11138 content_kind: read.content_kind,
11139 })
11140 }
11141 };
11142 let far = far.as_ref();
11143 // The caller says which kind the far end is, and this board holds the far end
11144 // itself, so a disagreement is settled here rather than stored: recorded, the
11145 // wrong kind would read back as a cross-level edge that never existed; written
11146 // natively, it would name a relationship of a different level than the caller
11147 // asked for.
11148 //
11149 // A far end this board holds as a *document* fails the same comparison and is
11150 // refused by the same sentence: `ItemKind` has no document variant because
11151 // nothing may point at one, so no caller can name it correctly and the refusal
11152 // is the only honest answer.
11153 if let Some(disagreeing) = far.filter(|far| far.kind != BoardKind::Work(edge.to.kind)) {
11154 return Err(SourceError::Refused {
11155 message: format!(
11156 "GitHub dependency item {far_id} is a {} of this board, and this item \
11157 names it as a {}; record the kind it is",
11158 disagreeing.kind.describes(),
11159 edge.to.kind.marker()
11160 ),
11161 });
11162 }
11163 // A draft has neither `blockedBy` nor `blocking`, so no edge of one is native
11164 // however the far end is spelled — and one classified native here would be
11165 // written nowhere at all, because a draft's native reconciliation never runs.
11166 let native_here = near_content == ContentKind::Issue
11167 && far.is_some_and(|far| {
11168 far.content_kind == ContentKind::Issue
11169 && BoardKind::Work(edge.to.kind) == near_kind
11170 });
11171 if native_here {
11172 native.push(far_id.to_owned());
11173 } else {
11174 fallback.push(edge.clone());
11175 }
11176 }
11177 Ok((native, fallback))
11178 }
11179
11180 async fn update_existing(
11181 &self,
11182 item: &Resolved,
11183 incoming: &Incoming<'_>,
11184 body: &Option<String>,
11185 status_target: Option<&StatusTarget>,
11186 ) -> Result<(), SourceError> {
11187 let title = incoming.written_title();
11188 // A terminal status closes the issue here, in the same mutation as its body: its board
11189 // option was selected before this, so a close never lands on an item whose board cannot
11190 // show it.
11191 let mut fields = match item.content_kind {
11192 ContentKind::DraftIssue => json!({"title":title,"body":body}),
11193 ContentKind::Issue => json!({"title":title,"body":body,
11194 "stateInput":state_input(status_target)}),
11195 };
11196 // Only the members whose value differs from the item as this write read it are sent,
11197 // and nothing at all when none does — whether or not the store's cache expects a
11198 // version of it. A member left out is one GitHub leaves as it is, so a person's edit
11199 // of it since that read is kept rather than overwritten with what it already held,
11200 // and a re-copy of an unchanged item costs no mutation.
11201 if let Some(members) = fields.as_object_mut() {
11202 if members.get("title").and_then(Value::as_str) == Some(item.title.as_str()) {
11203 members.remove("title");
11204 }
11205 let body_unchanged = match (members.get("body"), item.raw_body.as_deref()) {
11206 (Some(Value::String(sent)), Some(held)) => sent == held,
11207 (Some(Value::Null), held) => held.is_none_or(str::is_empty),
11208 _ => false,
11209 };
11210 if body_unchanged {
11211 members.remove("body");
11212 }
11213 // An issue already in the state the target asks for keeps it: an open one stays
11214 // open, and a closed one whose closure reads as the target's category keeps its
11215 // reason — a duplicate's says more than this write would.
11216 let unchanged_state = match status_target {
11217 Some(StatusTarget::Terminal(_, reason)) => {
11218 item.closed
11219 && closed_category(item.reason.as_deref())
11220 == closed_category(Some(reason.reason()))
11221 }
11222 Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => !item.closed,
11223 None => true,
11224 };
11225 if unchanged_state {
11226 members.remove("stateInput");
11227 }
11228 if members.is_empty() {
11229 return Ok(());
11230 }
11231 }
11232 self.update_content(item.content_kind, &item.id, fields)
11233 .await
11234 }
11235
11236 /// Update one board item's content with exactly `fields` beside its id, through the
11237 /// mutation its kind takes: `updateIssue` for an issue, `updateProjectV2DraftIssue` for
11238 /// a draft.
11239 ///
11240 /// Every input field either mutation leaves out is a field GitHub leaves as it is, which
11241 /// is what lets a narrow write carry the one thing it changes and nothing else.
11242 async fn update_content(
11243 &self,
11244 kind: ContentKind,
11245 id: &NativeId,
11246 fields: Value,
11247 ) -> Result<(), SourceError> {
11248 let (operation, id_key, pointer) = match kind {
11249 ContentKind::DraftIssue => (
11250 graphql::UPDATE_DRAFT,
11251 "draftIssueId",
11252 "/updateProjectV2DraftIssue/draftIssue",
11253 ),
11254 ContentKind::Issue => (graphql::UPDATE_ISSUE, "id", "/updateIssue/issue"),
11255 };
11256 if kind == ContentKind::Issue && self.hold_update(id, &fields)? {
11257 return Ok(());
11258 }
11259 let mut input = fields;
11260 input[id_key] = json!(id.0);
11261 self.send_update(operation, pointer, id, input).await
11262 }
11263
11264 /// Send one item's update, `input`, as `operation`, and hold GitHub's answer at `pointer`
11265 /// to naming that item, `id`.
11266 async fn send_update(
11267 &self,
11268 operation: &str,
11269 pointer: &str,
11270 id: &NativeId,
11271 input: Value,
11272 ) -> Result<(), SourceError> {
11273 let data = self.graphql(operation, json!({"input":input})).await?;
11274 let returned = data
11275 .pointer(pointer)
11276 .ok_or_else(|| SourceError::Malformed {
11277 message: "GitHub item update returned no item".into(),
11278 })?;
11279 if required_str(returned, "id")? != id.0 {
11280 return Err(SourceError::Malformed {
11281 message: "GitHub item update returned the wrong item".into(),
11282 });
11283 }
11284 Ok(())
11285 }
11286
11287 /// Creates one issue, files it on the board, and reports what a read of it would say:
11288 /// its content id, its board item id, and the web address GitHub gave it.
11289 ///
11290 /// Two calls rather than one: `createIssue` answers with an issue that is on no board,
11291 /// and `addProjectV2ItemById` is what puts it there. Filing it at creation through
11292 /// `CreateIssueInput.projectV2Ids` was tried and is not done: GitHub answered with no
11293 /// board item, and the `addProjectV2ItemById` that then had to follow was refused
11294 /// "Content already exists in this project". A terminal status is not written here:
11295 /// `finish_write` selects its option first and closes the issue after, so a close never
11296 /// lands on an item whose board cannot show it.
11297 ///
11298 /// The address and the number come back here because this is the only place either is
11299 /// known before GitHub's own board read catches up — an item this run created answers
11300 /// the reads that follow it out of the record below, and one remembered without them
11301 /// would report no location and no key for the rest of the run.
11302 async fn create_and_file_issue(
11303 &self,
11304 board_id: &str,
11305 repository: &RepositoryTarget,
11306 incoming: &Incoming<'_>,
11307 body: &Option<String>,
11308 ) -> Result<Landed, SourceError> {
11309 let repository_id = self.repository_id(repository, incoming).await?;
11310 let data = self
11311 .graphql(
11312 graphql::CREATE_ISSUE,
11313 json!({"input":{
11314 "repositoryId":repository_id,"title":incoming.written_title(),"body":body
11315 }}),
11316 )
11317 .await?;
11318 let created = data
11319 .pointer("/createIssue/issue")
11320 .filter(|value| !value.is_null())
11321 .ok_or_else(|| SourceError::Malformed {
11322 message: "GitHub issue creation returned no issue".into(),
11323 })?;
11324 let content_id = NativeId(required_str(created, "id")?.to_owned());
11325 // Optional although GitHub's schema makes it non-null: the issue exists by now, so
11326 // a response without it is not worth failing a landed write over — the item simply
11327 // reports no location until the board read catches up, which is what it did before.
11328 let url = optional_str(created, "url")?.map(str::to_owned);
11329 // The issue exists from here on, so an unreadable number and a refused board
11330 // filing below each try, best effort, to take it back: an issue in the repository
11331 // that is on no board is an item nobody asked for and nothing here would find again.
11332 //
11333 // Its number is optional on the same terms its address is — a landed write is not
11334 // worth failing over a member that came back missing, and such an item reports no
11335 // handle until a board read catches up. A number that is *present* and is not an
11336 // unsigned integer is still a response this source cannot read.
11337 let number = match created_issue_number(created) {
11338 Ok(number) => number,
11339 Err(error) => {
11340 let _ = self.delete_issue(&content_id).await;
11341 return Err(error);
11342 }
11343 };
11344 let added = match self
11345 .graphql(
11346 graphql::ADD_TO_BOARD,
11347 json!({"input":{"projectId":board_id,"contentId":content_id.0}}),
11348 )
11349 .await
11350 {
11351 Ok(added) => added,
11352 Err(error) => {
11353 let _ = self.delete_issue(&content_id).await;
11354 return Err(error);
11355 }
11356 };
11357 let item = added
11358 .pointer("/addProjectV2ItemById/item")
11359 .filter(|value| !value.is_null())
11360 .ok_or_else(|| SourceError::Malformed {
11361 message: "GitHub board addition returned no project item".into(),
11362 })?;
11363 self.note_written(&content_id, url.as_deref());
11364 Ok(Landed {
11365 content_id,
11366 item_id: Some(required_str(item, "id")?.to_owned()),
11367 url,
11368 number,
11369 })
11370 }
11371
11372 /// Move one issue under the project it now belongs to, or out of the one it left.
11373 async fn reparent(
11374 &self,
11375 held: Option<NativeId>,
11376 child: &NativeId,
11377 wanted: Option<&NativeId>,
11378 ) -> Result<(), SourceError> {
11379 if held.as_ref() == wanted {
11380 return Ok(());
11381 }
11382 if let Some(held) = &held {
11383 self.sub_issue(graphql::REMOVE_SUB_ISSUE, held, child, "removeSubIssue")
11384 .await?;
11385 }
11386 if let Some(wanted) = wanted {
11387 self.sub_issue(graphql::ADD_SUB_ISSUE, wanted, child, "addSubIssue")
11388 .await?;
11389 }
11390 Ok(())
11391 }
11392
11393 async fn sub_issue(
11394 &self,
11395 operation: &str,
11396 parent: &NativeId,
11397 child: &NativeId,
11398 root: &str,
11399 ) -> Result<(), SourceError> {
11400 let data = self
11401 .graphql(
11402 operation,
11403 json!({"input":{"issueId":parent.0,"subIssueId":child.0}}),
11404 )
11405 .await?;
11406 let issue =
11407 data.pointer(&format!("/{root}/issue"))
11408 .ok_or_else(|| SourceError::Malformed {
11409 message: "GitHub sub-issue update returned no issue".into(),
11410 })?;
11411 let sub =
11412 data.pointer(&format!("/{root}/subIssue"))
11413 .ok_or_else(|| SourceError::Malformed {
11414 message: "GitHub sub-issue update returned no sub-issue".into(),
11415 })?;
11416 if required_str(issue, "id")? != parent.0 || required_str(sub, "id")? != child.0 {
11417 return Err(SourceError::Malformed {
11418 message: "GitHub sub-issue update returned the wrong issues".into(),
11419 });
11420 }
11421 Ok(())
11422 }
11423
11424 /// Bring one issue's `blockedBy` to exactly `native`, sending only the difference, and say
11425 /// whether there was one.
11426 ///
11427 /// An issue [`Issue::Created`] by this very write is blocked by nothing yet, so its
11428 /// relationships are not read: there is nothing a read of them could find.
11429 async fn reconcile_blocked_by(
11430 &self,
11431 content_id: &NativeId,
11432 native: &[String],
11433 issue: Issue<'_>,
11434 ) -> Result<bool, SourceError> {
11435 let current = match issue {
11436 Issue::Created => Vec::new(),
11437 Issue::Existing(Some(held)) => held
11438 .iter()
11439 .map(|far| required_str(far, "id").map(str::to_owned))
11440 .collect::<Result<Vec<_>, _>>()?,
11441 Issue::Existing(None) => self.native_dependency_ids(content_id).await?,
11442 };
11443 let mut changed = false;
11444 for (operation, far_id) in current
11445 .iter()
11446 .filter(|id| !native.contains(id))
11447 .map(|id| (graphql::REMOVE_BLOCKED_BY, id))
11448 .chain(
11449 native
11450 .iter()
11451 .filter(|id| !current.contains(id))
11452 .map(|id| (graphql::ADD_BLOCKED_BY, id)),
11453 )
11454 {
11455 let data = self
11456 .graphql(
11457 operation,
11458 json!({"input":{"issueId":content_id.0,"blockingIssueId":far_id}}),
11459 )
11460 .await?;
11461 let root = if operation == graphql::ADD_BLOCKED_BY {
11462 "addBlockedBy"
11463 } else {
11464 "removeBlockedBy"
11465 };
11466 let issue =
11467 data.pointer(&format!("/{root}/issue"))
11468 .ok_or_else(|| SourceError::Malformed {
11469 message: "GitHub dependency update returned no issue".into(),
11470 })?;
11471 let blocker = data
11472 .pointer(&format!("/{root}/blockingIssue"))
11473 .ok_or_else(|| SourceError::Malformed {
11474 message: "GitHub dependency update returned no blocking issue".into(),
11475 })?;
11476 if required_str(issue, "id")? != content_id.0 || required_str(blocker, "id")? != far_id
11477 {
11478 return Err(SourceError::Malformed {
11479 message: "GitHub dependency update returned the wrong issues".into(),
11480 });
11481 }
11482 changed = true;
11483 }
11484 Ok(changed)
11485 }
11486}
11487
11488/// The board field values one write sends, in the order GitHub runs them, and the fields it
11489/// clears after them: the origin field when it moves, the `Status` option `column` names, the
11490/// priority, then each projected field.
11491fn field_writes<'a>(
11492 existing: Option<&Resolved>,
11493 origin_field: Option<&str>,
11494 origin: &str,
11495 column: Option<(String, String)>,
11496 priority: Option<&'a PriorityWrite>,
11497 projections: &'a [ProjectionWrite],
11498) -> (Vec<(String, Value)>, Vec<&'a str>) {
11499 let mut fields = Vec::new();
11500 if let Some(field_id) = origin_field
11501 && existing.map_or(!origin.is_empty(), |item| {
11502 item.origin.as_deref().unwrap_or("") != origin
11503 })
11504 {
11505 fields.push((field_id.to_owned(), json!({"text":origin})));
11506 }
11507 if let Some((field_id, option_id)) = column {
11508 fields.push((field_id, json!({"singleSelectOptionId":option_id})));
11509 }
11510 let mut clears = Vec::new();
11511 match priority {
11512 Some(PriorityWrite::Select { field, option }) => {
11513 fields.push((field.clone(), json!({"singleSelectOptionId":option})));
11514 }
11515 Some(PriorityWrite::Clear { field }) => clears.push(field.as_str()),
11516 None => {}
11517 }
11518 for projection in projections {
11519 projection.join(&mut fields, &mut clears);
11520 }
11521 (fields, clears)
11522}
11523
11524/// Everything one write decided before sending anything: see
11525/// [`GitHubProjectsSource::prepare_write`].
11526struct Prepared {
11527 /// What the write lands on.
11528 destination: WriteDestination,
11529 board: BoardFields,
11530 status_target: Option<StatusTarget>,
11531 /// The `Status` field, the option and the option's name the status lands on.
11532 // 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.
11533 column: Option<(String, String, String)>,
11534 priority_write: Option<PriorityWrite>,
11535 projections: Vec<ProjectionWrite>,
11536 projected: BTreeMap<String, String>,
11537 content_kind: ContentKind,
11538 own_repository: Option<Repository>,
11539 /// The far ends of the item's native blocked-by relationships.
11540 // 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.
11541 native: Vec<String>,
11542 slot: BTreeMap<String, Value>,
11543 content: Option<String>,
11544 body: Option<String>,
11545 /// The copy origin the item records, empty for none.
11546 // 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.
11547 origin: String,
11548 origin_field: Option<String>,
11549}
11550
11551/// One write a copy held back of the kinds sent together: a board field value, or a blocked-by
11552/// edge, each its input.
11553enum HeldWrite<'a> {
11554 Field(&'a Value),
11555 Edge(&'a Value),
11556}
11557
11558/// Whether `error` is GitHub refusing to file an issue on a board because it is filed there
11559/// already — what `addProjectV2ItemById` answers once `createIssue`'s `projectV2Ids` has placed
11560/// it, rather than answering with the item it holds.
11561fn already_on_the_board(error: &SourceError) -> bool {
11562 error
11563 .to_string()
11564 .contains("Content already exists in this project")
11565}
11566
11567/// Hold every alias `inputs` sent under `prefix` in `data` to answering with what its input
11568/// named — each `(root, named)` of `answers` — and refuse one that answered nothing or another
11569/// item.
11570fn check_aliased_answers(
11571 document: &str,
11572 data: &Value,
11573 prefix: &str,
11574 inputs: &[&Value],
11575 answers: &[(&str, &str)],
11576) -> Result<(), SourceError> {
11577 for (slot, input) in inputs.iter().enumerate() {
11578 for (root, named) in answers {
11579 let answered = data
11580 .pointer(&format!("/{prefix}{slot}/{root}/id"))
11581 .and_then(Value::as_str)
11582 .ok_or_else(|| SourceError::Malformed {
11583 message: format!(
11584 "GitHub answered {} with nothing at {prefix}{slot}",
11585 operation_description(document)
11586 ),
11587 })?;
11588 let asked = input.get(named).and_then(Value::as_str).unwrap_or_default();
11589 if answered != asked {
11590 return Err(SourceError::Malformed {
11591 message: format!(
11592 "GitHub answered {} at {prefix}{slot} with {answered}, which is not \
11593 {asked}, the item it was asked to write",
11594 operation_description(document)
11595 ),
11596 });
11597 }
11598 }
11599 }
11600 Ok(())
11601}
11602
11603/// `error`, which stopped a batch of creates, and — when GitHub refused to take any of them
11604/// back — each issue of `left` it created that is still in the repository, which its caller,
11605/// never handed their ids, could not name.
11606fn left_behind(error: SourceError, left: &[&str]) -> SourceError {
11607 if left.is_empty() {
11608 return error;
11609 }
11610 SourceError::Refused {
11611 message: format!(
11612 "{error}; GitHub then refused to take back {}, which this write created and which \
11613 are still in the repository; next: delete {} before writing again",
11614 left.join(", "),
11615 if left.len() == 1 { "it" } else { "them" }
11616 ),
11617 }
11618}
11619
11620/// What one write lands on: the item it updates, as it stood, or where it creates a new one.
11621enum WriteDestination {
11622 /// The item as it stood, for an update.
11623 Existing(Box<Resolved>),
11624 /// Where a new item is created.
11625 New(RepositoryTarget),
11626}
11627
11628impl WriteDestination {
11629 /// The item an update writes over, as it stood; `None` for a create.
11630 fn existing(&self) -> Option<&Resolved> {
11631 match self {
11632 Self::Existing(item) => Some(item),
11633 Self::New(_) => None,
11634 }
11635 }
11636}
11637
11638/// One existing issue's update held back by [`GitHubProjectsSource::hold_update`]: the issue,
11639/// and the `UpdateIssueInput` its own [`graphql::UPDATE_ISSUE`] would have sent — the members
11640/// it writes beside that issue's id, which only [`Self::new`] sets.
11641struct HeldUpdate {
11642 issue: NativeId,
11643 // 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.
11644 input: Value,
11645}
11646
11647impl HeldUpdate {
11648 /// The update writing `members` — an `UpdateIssueInput` without its id — to `issue`.
11649 fn new(issue: &NativeId, members: &Value) -> Self {
11650 let mut input = members.clone();
11651 input["id"] = json!(issue.0);
11652 Self {
11653 issue: issue.clone(),
11654 input,
11655 }
11656 }
11657}
11658
11659/// What a copy has held back of the items it created; see
11660/// [`GitHubProjectsSource::hold_back`].
11661#[derive(Debug, Default)]
11662enum Deferred {
11663 /// Writes are being held back, and nothing has been held yet.
11664 #[default]
11665 Nothing,
11666 /// The items held back, every one of them on `board`.
11667 Held {
11668 /// The board every one of them is on.
11669 board: BoardId,
11670 /// Each created item, in the order it was created.
11671 created: Vec<HeldBack>,
11672 },
11673}
11674
11675impl Deferred {
11676 /// Whether `content` is one of the items held back.
11677 fn holds(&self, content: &NativeId) -> bool {
11678 match self {
11679 Self::Nothing => false,
11680 Self::Held { created, .. } => created.iter().any(|held| held.content == *content),
11681 }
11682 }
11683}
11684
11685/// One created item's parts that land once [`TaskSource::finish_writes`] is called.
11686#[derive(Debug)]
11687struct HeldBack {
11688 /// The issue.
11689 content: NativeId,
11690 /// Its board field values other than its `Status`, in the order they are written.
11691 fields: Vec<(String, Value)>,
11692 /// The `Status` field, and the option and its name that the item's status lands on.
11693 // llmlint: ignore[invalid_states_unrepresentable] The same triple `column_for` returns and `Prepared::column` carries, held back unchanged until it is sent.
11694 status: Option<(String, String, String)>,
11695 /// The fields it clears after those are written.
11696 clears: Vec<String>,
11697 /// The reason it is closed for, once its option is selected; `None` leaves it open.
11698 close: Option<ClosedState>,
11699 /// The issues it is blocked by.
11700 edges: Vec<NativeId>,
11701}
11702
11703/// The member [`answer_with_errors`] carries a response's GraphQL errors under, beside its data:
11704/// no GitHub type has a field of this name.
11705const ANSWER_ERRORS: &str = "onetaskgraphErrors";
11706
11707/// A response's `data` with its GraphQL errors beside it under [`ANSWER_ERRORS`], when it has
11708/// both — or `None`, for an answer [`GitHubProjectsSource::answer`] reads as it always has.
11709///
11710/// What [`graphql::CREATE_ISSUES`] is answered with: GitHub runs a document's mutation fields in
11711/// order and answers each one that ran, so a refusal of one alias leaves the issues the others
11712/// created named in the data beside it — the ids a copy needs to take them back.
11713fn answer_with_errors(body: &str) -> Option<Value> {
11714 let parsed: Value = serde_json::from_str(body).ok()?;
11715 let errors = parsed
11716 .get("errors")?
11717 .as_array()
11718 .filter(|errors| !errors.is_empty())?;
11719 let mut data = parsed.get("data").filter(|data| data.is_object())?.clone();
11720 data[ANSWER_ERRORS] = Value::Array(errors.clone());
11721 Some(data)
11722}
11723
11724/// The `createIssue` input that files the new issue on `board` and, when `incoming` names one,
11725/// under its project — see [`GitHubProjectsSource::create_filed_issue`].
11726fn filed_input(
11727 repository: &str,
11728 board: &str,
11729 incoming: &Incoming<'_>,
11730 body: &Option<String>,
11731) -> Value {
11732 let mut input = json!({"repositoryId":repository,"title":incoming.written_title(),
11733 "body":body,"projectV2Ids":[board]});
11734 if let Some(parent) = incoming.parent {
11735 input["parentIssueId"] = json!(parent.0);
11736 }
11737 input
11738}
11739
11740/// The name of option `option` of field `field` among a board's `fields`.
11741fn option_name<'a>(fields: &'a Value, field: &str, option: &str) -> Option<&'a str> {
11742 fields
11743 .get("nodes")?
11744 .as_array()?
11745 .iter()
11746 .find(|node| node.get("id").and_then(Value::as_str) == Some(field))?
11747 .get("options")?
11748 .as_array()?
11749 .iter()
11750 .find(|candidate| candidate.get("id").and_then(Value::as_str) == Some(option))?
11751 .get("name")?
11752 .as_str()
11753}
11754
11755/// What a write needs to know of one far end it names: which kind of item it is, and whether
11756/// it is an issue a native relationship can name.
11757struct FarEnd {
11758 kind: BoardKind,
11759 content_kind: ContentKind,
11760}
11761
11762/// Whether the issue one write reconciles was created by that write or was already there.
11763#[derive(Clone, Copy, PartialEq, Eq)]
11764enum Issue<'a> {
11765 /// Created by this write, so it holds no relationships yet.
11766 Created,
11767 /// On the board before this write, holding whatever relationships it holds — the far
11768 /// ends of its whole `blockedBy`, when the read that reached it carried them.
11769 Existing(Option<&'a [Value]>),
11770}
11771
11772/// What resolving one node id reached; see [`GitHubProjectsSource::reach`].
11773enum Reached {
11774 /// An issue this board holds, resolved into everything this source reports about it.
11775 Held(Box<Resolved>),
11776 /// Nothing this board holds: no such node, or a node on some other board.
11777 Nothing,
11778 /// A board draft, which [`graphql::ISSUE`] reaches and reads nothing of, so it is read
11779 /// again by [`GitHubProjectsSource::draft_by_id`].
11780 Draft,
11781}
11782
11783/// What GitHub says when a string is not a node id it can resolve.
11784///
11785/// Matched because it is the ordinary answer to a project selector naming a project by its
11786/// *name*, and reporting that as a failure would make naming one impossible. It is read
11787/// off the refusal GitHub sent, never guessed from the shape of the string: this source
11788/// does not define the syntax of a GitHub node id and would be wrong about it.
11789const UNRESOLVABLE_NODE: &str = "could not resolve to a node";
11790
11791/// `error` with `note` added to the end of what it says, its kind and every other member
11792/// unchanged — so a caller still branches on the failure that happened, and reads beside it
11793/// what that failure left behind.
11794fn noting(error: SourceError, note: &str) -> SourceError {
11795 match error {
11796 SourceError::Config { message } => SourceError::Config {
11797 message: message + note,
11798 },
11799 SourceError::Auth { message } => SourceError::Auth {
11800 message: message + note,
11801 },
11802 SourceError::Refused { message } => SourceError::Refused {
11803 message: message + note,
11804 },
11805 SourceError::RateLimited {
11806 retry_after_seconds,
11807 message,
11808 } => SourceError::RateLimited {
11809 retry_after_seconds,
11810 message: Some(message.unwrap_or_default() + note),
11811 },
11812 SourceError::Unavailable { message } => SourceError::Unavailable {
11813 message: message + note,
11814 },
11815 SourceError::Malformed { message } => SourceError::Malformed {
11816 message: message + note,
11817 },
11818 }
11819}
11820
11821/// The variables of one [`graphql::ISSUE_COPY_DETAILS`] request, as [`detail_batch`] binds its
11822/// own: a slot `batch` has no id for is bound to its last id, read again at no added price.
11823fn copy_detail_batch(batch: &[NativeId], comments: &PageRequest, children: bool) -> Value {
11824 let mut variables = serde_json::Map::new();
11825 for slot in 0..COPY_DETAIL_BATCH {
11826 let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
11827 variables.insert(format!("id{slot}"), json!(id));
11828 }
11829 variables.insert("project".to_owned(), json!(batch.first().map(|id| &id.0)));
11830 variables.insert("children".to_owned(), json!(children));
11831 variables.insert("first".to_owned(), json!(comments.limit.min(MAX_PAGE_SIZE)));
11832 variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
11833 variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
11834 variables.insert("duplicates".to_owned(), json!(true));
11835 Value::Object(variables)
11836}
11837
11838/// The variables of one [`graphql::ISSUE_DETAILS`] request over `batch` — at most
11839/// [`DETAIL_BATCH`] ids — each item with the first page of its comments when `comments` asks
11840/// for them.
11841///
11842/// The document is fixed-size, so a slot `batch` has no id for is bound to its last id, which
11843/// is read again at no added price.
11844fn detail_batch(batch: &[NativeId], comments: Option<&PageRequest>) -> Value {
11845 let mut variables = serde_json::Map::new();
11846 for slot in 0..DETAIL_BATCH {
11847 let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
11848 variables.insert(format!("id{slot}"), json!(id));
11849 }
11850 variables.insert(
11851 "first".to_owned(),
11852 json!(comments.map_or(MAX_PAGE_SIZE, |page| page.limit.min(MAX_PAGE_SIZE))),
11853 );
11854 variables.insert("comments".to_owned(), json!(comments.is_some()));
11855 variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
11856 variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
11857 variables.insert("duplicates".to_owned(), json!(true));
11858 Value::Object(variables)
11859}
11860
11861/// Whether this refusal is GitHub saying the id names no node at all.
11862fn unresolvable_node(error: &SourceError) -> bool {
11863 matches!(error, SourceError::Refused { message }
11864 if message.to_ascii_lowercase().contains(UNRESOLVABLE_NODE))
11865}
11866
11867/// One project name, as a search qualifier which filters on it at the server.
11868///
11869/// Quoted so the whole title is one phrase rather than a bag of words, with the two
11870/// characters GitHub's own quoting grammar gives a meaning inside a quoted phrase escaped
11871/// the way it documents. A title matched here is still compared for equality afterwards:
11872/// the qualifier narrows what the server sends, and this source decides what it names.
11873fn title_qualifier(name: &str) -> String {
11874 format!("in:title {}", quoted(name))
11875}
11876
11877/// `value` as one quoted phrase of a GitHub search or a board filter, with the two
11878/// characters GitHub's quoting grammar gives a meaning inside a quoted phrase escaped the way
11879/// it documents — so a value holding a qualifier's spelling is searched for rather than
11880/// obeyed.
11881fn quoted(value: &str) -> String {
11882 let escaped = value.replace('\\', "\\\\").replace('"', "\\\"");
11883 format!("\"{escaped}\"")
11884}
11885
11886/// The search qualifier for the issues updated at or after `since`.
11887///
11888/// Written to the second, rounded down, which can only widen what the search returns.
11889fn updated_qualifier(since: DateTime<Utc>) -> String {
11890 format!("updated:>={}", since.format("%Y-%m-%dT%H:%M:%S+00:00"))
11891}
11892
11893/// The search terms that narrow a board-scoped issue search to a task query's text and
11894/// metadata predicates, or `None` when it carries neither.
11895///
11896/// The text is one quoted phrase, searched `in:title`, `in:body` or both as its fields say,
11897/// and each metadata value is one more quoted phrase, which GitHub finds in the body because
11898/// its index covers the metadata comment the value is stored in. GitHub ANDs the phrases and
11899/// matches each in any field the `in:` qualifier names, so a query naming a title search and
11900/// a metadata value searches both fields for both — wider than asked, never narrower, and
11901/// every candidate is confirmed in process afterwards.
11902///
11903/// **This narrows a text search, and that is this source's declared semantics.** GitHub
11904/// matches whole tokens where a substring rule would match inside a word, so an item holding
11905/// the text only inside a longer word is not returned. A text of nothing but whitespace
11906/// matches every item, so it narrows nothing and is not sent.
11907fn narrowing_qualifiers(query: &TaskQuery) -> Option<String> {
11908 let text = query
11909 .text
11910 .as_ref()
11911 .filter(|text| !text.terms.trim().is_empty());
11912 if text.is_none() && query.metadata.is_empty() {
11913 return None;
11914 }
11915 let (title, body) = match text.map(|text| text.fields) {
11916 None => (false, true),
11917 Some(TextFields::Title) => (true, !query.metadata.is_empty()),
11918 Some(TextFields::Content) => (false, true),
11919 Some(TextFields::TitleOrContent) => (true, true),
11920 };
11921 let fields = match (title, body) {
11922 (true, true) => "in:title,body",
11923 (true, false) => "in:title",
11924 _ => "in:body",
11925 };
11926 let phrases = text
11927 .map(|text| text.terms.clone())
11928 .into_iter()
11929 .chain(
11930 query
11931 .metadata
11932 .iter()
11933 .map(|wanted| as_stored(wanted.value())),
11934 )
11935 .map(|phrase| quoted(&phrase))
11936 .collect::<Vec<_>>();
11937 Some(format!("{fields} {}", phrases.join(" ")))
11938}
11939
11940/// The search terms that narrow a board-scoped issue search to a project or document query's
11941/// text, or `None` when it has none or a blank one: the phrase, in the fields, a task query
11942/// carrying that text alone is sent as by [`narrowing_qualifiers`].
11943fn text_qualifiers(text: Option<&TextQuery>) -> Option<String> {
11944 narrowing_qualifiers(&TaskQuery {
11945 text: text.cloned(),
11946 ..TaskQuery::default()
11947 })
11948}
11949
11950/// Refuses a project or document query's text GitHub's issue search cannot find, before
11951/// anything is asked of GitHub, on exactly the terms [`refuse_unsearchable`] refuses a task
11952/// query's.
11953fn refuse_unsearchable_text(text: Option<&TextQuery>) -> Result<(), SourceError> {
11954 refuse_unsearchable(&TaskQuery {
11955 text: text.cloned(),
11956 ..TaskQuery::default()
11957 })
11958}
11959
11960/// Refuses a task query naming a text or a metadata value GitHub's issue search cannot find,
11961/// before anything is asked of GitHub.
11962///
11963/// GitHub's index holds words, so a phrase with no letter or digit names none to find, and no
11964/// bounded query answers it: sent, GitHub's answer to it is nothing this source may rely on;
11965/// left out, the search is every issue of the board. So this source says it cannot answer
11966/// rather than reading the board or answering nothing. A blank text is not refused: it narrows
11967/// nothing GitHub could search for, and keeps the board read it always had.
11968fn refuse_unsearchable(query: &TaskQuery) -> Result<(), SourceError> {
11969 const WHY: &str = "GitHub's issue search indexes words, so it cannot answer a value with no \
11970 letter or digit with a bounded query";
11971 if let Some(text) = &query.text
11972 && !text.terms.trim().is_empty()
11973 && !has_words(&text.terms)
11974 {
11975 return Err(SourceError::Refused {
11976 message: format!(
11977 "cannot search for the text {:?}: {WHY}; search for a text holding a letter or a digit",
11978 text.terms
11979 ),
11980 });
11981 }
11982 if let Some(wanted) = query
11983 .metadata
11984 .iter()
11985 .find(|wanted| !has_words(wanted.value()))
11986 {
11987 return Err(SourceError::Refused {
11988 message: format!(
11989 "cannot filter by the metadata value {:?} at {:?}: {WHY}; filter by a value holding a letter or a digit",
11990 wanted.value(),
11991 std::iter::once(wanted.key())
11992 .chain(wanted.path().iter().map(String::as_str))
11993 .collect::<Vec<_>>()
11994 .join("/"),
11995 ),
11996 });
11997 }
11998 Ok(())
11999}
12000
12001/// Whether GitHub's index could hold a word of `phrase`: whether it has a letter or a digit.
12002fn has_words(phrase: &str) -> bool {
12003 phrase.chars().any(char::is_alphanumeric)
12004}
12005
12006/// `value` spelled the way the metadata slot stores it: as the inside of its JSON string.
12007///
12008/// What GitHub indexes is the slot's JSON text, so a value holding a character JSON escapes —
12009/// a newline, a tab, a quote — is found by the escape the body holds and not by the character,
12010/// which GitHub's word match would read as different words.
12011fn as_stored(value: &str) -> String {
12012 let encoded = Value::String(value.to_owned()).to_string();
12013 encoded[1..encoded.len() - 1].to_owned()
12014}
12015
12016/// The one narrower question a task query carrying a text, metadata or origin predicate is
12017/// sent as.
12018enum Narrowing {
12019 /// Every carrier of this origin: [`graphql::ORIGIN_LOOKUP`].
12020 Origin(String),
12021 /// The board-scoped issue search narrowed by these qualifiers.
12022 Search(String),
12023}
12024
12025impl Narrowing {
12026 /// What this question is remembered under for the length of one command.
12027 fn key(&self) -> String {
12028 match self {
12029 Self::Origin(origin) => format!("origin {origin}"),
12030 Self::Search(also) => format!("search {also}"),
12031 }
12032 }
12033}
12034
12035/// Where one connection of [`graphql::ORIGIN_LOOKUP`] resumes.
12036enum Resumed {
12037 /// It reported another page, which starts after this cursor.
12038 More(String),
12039 /// It has ended. Sending this cursor again — the page's own end when it had one, and
12040 /// otherwise the cursor it was reached from — answers an empty page, so the one document
12041 /// can go on walking the other connection.
12042 Ended(Option<String>),
12043}
12044
12045impl Resumed {
12046 /// Whether the connection has another page.
12047 const fn has_more(&self) -> bool {
12048 matches!(self, Self::More(_))
12049 }
12050
12051 /// The cursor to send this connection next.
12052 fn cursor(self) -> Option<String> {
12053 match self {
12054 Self::More(next) => Some(next),
12055 Self::Ended(last) => last,
12056 }
12057 }
12058}
12059
12060/// Where `connection`, reached from `after`, resumes — refused when it reports another page
12061/// with no cursor to it, or from a cursor that does not advance.
12062fn resumed(connection: &Value, after: Option<&str>) -> Result<Resumed, SourceError> {
12063 let info = connection
12064 .get("pageInfo")
12065 .ok_or_else(|| SourceError::Malformed {
12066 message: "GitHub connection has no pageInfo".into(),
12067 })?;
12068 let end = optional_str(info, "endCursor")?;
12069 if required_bool(info, "hasNextPage")? {
12070 let next = end.ok_or_else(|| SourceError::Malformed {
12071 message: "GitHub connection reports another page and no endCursor".into(),
12072 })?;
12073 validate_cursor_progress(after, next)?;
12074 return Ok(Resumed::More(next.to_owned()));
12075 }
12076 Ok(Resumed::Ended(
12077 end.map(str::to_owned).or_else(|| after.map(str::to_owned)),
12078 ))
12079}
12080
12081/// The board, and every item on it this source reports.
12082#[derive(Clone)]
12083struct Board {
12084 id: String,
12085 fields: Value,
12086 items: Vec<Resolved>,
12087}
12088
12089/// What a write needs of the board and nothing more: its node id and its field
12090/// definitions, in the shape a read of the board's own `fields` gives them.
12091///
12092/// Deliberately no items. A write decides which item it writes, which parent it files
12093/// under and which far ends it names by reading each of them by its own id; this is the
12094/// half of the board those reads cannot carry, and holding no item is what keeps it from
12095/// ever being asked whether an item is there.
12096#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
12097struct BoardFields {
12098 id: BoardId,
12099 fields: Value,
12100}
12101
12102/// A board's node id: what a field write and `addProjectV2ItemById` address.
12103///
12104/// Never blank, because a blank one addresses no board — so an id GitHub answers blank is
12105/// refused where it is read, and one an item names blank is read as not named at all.
12106#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
12107#[serde(try_from = "String")]
12108struct BoardId(String);
12109
12110impl TryFrom<String> for BoardId {
12111 type Error = String;
12112
12113 /// The same refusal [`BoardId::parse`] makes, for an id read back from a kept context.
12114 fn try_from(id: String) -> Result<Self, Self::Error> {
12115 Self::parse(&id).map_err(|error| error.to_string())
12116 }
12117}
12118
12119/// Where one write left its item, for the record the rest of the command reads it out of.
12120///
12121/// A named record rather than a tuple because the update arm and the create arm each fill
12122/// all four, and two `Option`s of different meaning side by side in a tuple are two
12123/// positions a reader has to count.
12124struct Landed {
12125 /// The issue's own node id, which is the [`NativeId`] this source reports.
12126 content_id: NativeId,
12127 /// The board item's id, which is what a field write addresses — `None` for an issue its
12128 /// create filed on the board itself, whose board item GitHub adds after answering and
12129 /// [`TaskSource::finish_writes`] reads.
12130 // 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.
12131 item_id: Option<String>,
12132 /// The web address GitHub gave the issue, when it gave one.
12133 // 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.
12134 url: Option<String>,
12135 /// The issue's number on its repository, when GitHub reported one.
12136 number: Option<u64>,
12137}
12138
12139impl BoardId {
12140 fn parse(id: &str) -> Result<Self, SourceError> {
12141 if id.trim().is_empty() {
12142 return Err(SourceError::Malformed {
12143 message: "GitHub named a board with a blank node id".into(),
12144 });
12145 }
12146 Ok(Self(id.to_owned()))
12147 }
12148
12149 fn as_str(&self) -> &str {
12150 &self.0
12151 }
12152}
12153
12154impl Board {
12155 fn field<'a>(fields: &'a Value, name: &str) -> Result<Option<&'a Value>, SourceError> {
12156 complete_connection(fields, "project fields", NESTED_PAGE_SIZE)?;
12157 let nodes = fields
12158 .get("nodes")
12159 .and_then(Value::as_array)
12160 .ok_or_else(|| SourceError::Malformed {
12161 message: "GitHub project fields.nodes is not an array".into(),
12162 })?;
12163 Ok(nodes
12164 .iter()
12165 .find(|field| field.get("name").and_then(Value::as_str) == Some(name)))
12166 }
12167}
12168
12169/// One board item, resolved into everything this source reports about it.
12170///
12171/// Serializable because it is what [`TaskSource::write_context`] hands the store's cache and
12172/// [`TaskSource::prime_write`] takes back — see [`expected`].
12173#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
12174struct Resolved {
12175 item_id: String,
12176 id: NativeId,
12177 content_kind: ContentKind,
12178 kind: BoardKind,
12179 title: String,
12180 body: Option<String>,
12181 /// The body exactly as GitHub holds it, metadata slot and all, which is what a write
12182 /// that changes the slot alone has to keep byte for byte outside it.
12183 raw_body: Option<String>,
12184 status: Status,
12185 /// The name of the board `Status` option this item sits in, as the board spells it.
12186 option: Option<String>,
12187 /// What its `Priority` field says, read through this instance's mapping.
12188 priority: HeldPriority,
12189 /// Whether this item's issue is closed. A draft has no such state and is never closed.
12190 closed: bool,
12191 /// GitHub's reason for a closed issue's closure, as it spells it — `None` for an open
12192 /// issue, a draft, and an issue closed before GitHub recorded reasons.
12193 #[serde(default)]
12194 reason: Option<String>,
12195 /// The tasks this one delivers, read out of its slot. Empty for anything not a task.
12196 delivers: Vec<TaskRef>,
12197 /// Every task that delivers this one, read out of its slot. Empty for anything not a
12198 /// task.
12199 delivered_by: Vec<TaskRef>,
12200 labels: Vec<Label>,
12201 parent: Option<NativeId>,
12202 // 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.
12203 origin: Option<String>,
12204 /// What each board text field this instance projects metadata onto holds for this item,
12205 /// by field name — only the fields holding a value, an empty text included. What a write compares the
12206 /// item's metadata against, so a field already holding its value is not written again;
12207 /// never read back as metadata, which the slot alone reports.
12208 projected: BTreeMap<String, String>,
12209 /// The issue's own number on its repository, as GitHub reports it.
12210 ///
12211 /// `None` in exactly two cases: a draft, which has no number at all — `DraftIssue`
12212 /// declares none, and a draft is not filed in a repository to be numbered by one — and
12213 /// an issue this run created whose creating mutation answered without one, which is a
12214 /// response GitHub's own schema says cannot happen and which a landed write is not
12215 /// worth failing over. An `Issue` read off the board always has one.
12216 number: Option<u64>,
12217 url: Option<String>,
12218 created_at: Option<DateTime<Utc>>,
12219 updated_at: Option<DateTime<Utc>>,
12220 /// When the board item itself last changed — what a board field write moves — when the
12221 /// read that reached it said.
12222 item_updated_at: Option<DateTime<Utc>>,
12223 /// How many comments the issue holds, when the read that reached it said.
12224 comment_count: Option<u64>,
12225 /// How many relationship events the issue's timeline holds — every dependency, as
12226 /// `blockedBy` or `blocking`, and every project membership, as its parent, added or
12227 /// removed — when the read that reached it said.
12228 relationship_events: Option<u64>,
12229 /// The server's clock on the response that read it — its `Date` header — when the
12230 /// response carried one.
12231 served_at: Option<DateTime<Utc>>,
12232 /// The digest of its [`change_token::Facets`] — its own kind, its board field values and
12233 /// its labels — when the read that reached it carried all three whole.
12234 #[serde(default)]
12235 facets: Option<change_token::FacetsDigest>,
12236 own_repository: Option<Repository>,
12237 repositories: Vec<Repository>,
12238 classification: Classification,
12239 slot: BTreeMap<String, Value>,
12240 /// The node id of the board this item sits on, when the read that reached it said.
12241 board_id: Option<String>,
12242 /// The definition of every board field this item holds a value of, in the shape a read
12243 /// of the board's own `fields` gives one.
12244 ///
12245 /// Only the fields this item has a value in: a field it holds nothing of is not here,
12246 /// which says nothing about whether the board has it.
12247 fields: Vec<Value>,
12248 /// Every field the board this item sits on defines, as its own read of the board's
12249 /// `fields` gives them — when the read that reached the item carried them, which a read
12250 /// of it by its own id does. What a write of it needs of the board, then, needs no read
12251 /// of the board.
12252 board_fields: Option<Value>,
12253 /// The far ends of this issue's whole `blockedBy` connection, each as a dependency read
12254 /// selects one — when the read that reached it carried the connection to its end, which a
12255 /// read of it by its own id does for any issue blocked by no more than a page. What a
12256 /// write reconciles that relationship against, and what a read of its forward edges in
12257 /// the same command answers with.
12258 blocked_by: Option<Vec<Value>>,
12259}
12260
12261impl Resolved {
12262 /// The board this item's own read names it on, when that read named one this source can
12263 /// address.
12264 fn named_board(&self) -> Option<BoardId> {
12265 self.board_id
12266 .as_deref()
12267 .and_then(|id| BoardId::parse(id).ok())
12268 }
12269
12270 /// The board's id and every field it defines, when the read that reached this item
12271 /// carried both — which a read of it by its own id does.
12272 fn carried_board(&self) -> Option<BoardFields> {
12273 Some(BoardFields {
12274 id: self.named_board()?,
12275 fields: self.board_fields.clone()?,
12276 })
12277 }
12278
12279 /// Whether this item holds a value of the board field called `name`, and so carries
12280 /// that field's definition. `false` says nothing about whether the board has the field.
12281 fn defines(&self, name: &str) -> bool {
12282 self.fields
12283 .iter()
12284 .any(|field| field.get("name").and_then(Value::as_str) == Some(name))
12285 }
12286
12287 /// The metadata a caller sees: their own keys, plus the copy origin this source keeps
12288 /// in a field of its own, and none of the five keys that are only an encoding.
12289 ///
12290 /// The two delivery keys are left out for every kind, not only for a task: they are
12291 /// the encoding of [`Task::delivers`] and [`Task::delivered_by`], and a project or a
12292 /// document carrying one holds nothing a caller's own metadata could mean by it.
12293 fn metadata(&self) -> BTreeMap<String, Value> {
12294 let mut metadata = self.slot.clone();
12295 metadata.remove(Repository::METADATA_KEY);
12296 metadata.remove(DependencyEdge::RECORDED_KEY);
12297 metadata.remove(ItemKind::METADATA_KEY);
12298 metadata.remove(TaskRef::DELIVERS_KEY);
12299 metadata.remove(TaskRef::DELIVERED_BY_KEY);
12300 metadata.remove(Classification::METADATA_KEY);
12301 // The board field is the origin, and the body's copy of it is only a mirror for the
12302 // issue search to find: an item whose field holds none has none, whatever its body
12303 // says, so no reader ever sees two answers.
12304 metadata.remove(ORIGIN_KEY);
12305 if let Some(origin) = &self.origin {
12306 metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin.clone()));
12307 }
12308 metadata
12309 }
12310
12311 /// Where this item is, as a link a reader can open.
12312 ///
12313 /// A board is a hosted place and every issue on it has a web address, so that address
12314 /// is what "where is this?" means here — and [`Location::Url`] is what says which kind
12315 /// of place it is, so a reader knows to open it rather than to read a file out. It
12316 /// does not replace or derive from `url`: the field goes on reporting exactly what it
12317 /// reported before, and this says what that address *is*.
12318 ///
12319 /// An item GitHub gave no `url` for — a draft has none — reports no location at all
12320 /// rather than a third variant, which is the contract's "the source did not say". An
12321 /// issue this run created is not one of those: its address comes back from the
12322 /// creating mutation, so it is somewhere a reader can open from the moment it exists
12323 /// rather than from whenever the board read catches up.
12324 fn location(&self) -> Option<Location> {
12325 self.url.clone().map(Location::Url)
12326 }
12327
12328 /// The short handle this board's backend shows people for a task: the issue's number
12329 /// alone, as a decimal string.
12330 ///
12331 /// The number alone rather than `owner/repo#1043`, because that is the contract's
12332 /// value for this backend. A draft has no number and so no handle, which is the
12333 /// contract's *absent* rather than a handle of some other shape — and the native
12334 /// [`Task::id`] here is the issue's GraphQL node id, which this neither replaces nor
12335 /// derives from.
12336 fn key(&self) -> Option<String> {
12337 self.number.map(|number| number.to_string())
12338 }
12339
12340 /// This item's change token — see [`change_token`] — or `None` when the read that
12341 /// reached it did not carry all six of its parts, which a draft never does, did not say
12342 /// when GitHub served it, or found it changed too recently to have settled.
12343 fn version(&self) -> Option<String> {
12344 match self.content_kind {
12345 ContentKind::Issue => {
12346 let (issue, item) = (self.updated_at?, self.item_updated_at?);
12347 let parts = change_token::Parts {
12348 issue,
12349 item,
12350 comments: self.comment_count?,
12351 relationships: self.relationship_events?,
12352 facets: self.facets.clone()?,
12353 repository: self.own_repository.clone()?,
12354 };
12355 change_token::settled(issue, item, self.served_at?)
12356 .then(|| change_token::spell(&parts))
12357 }
12358 ContentKind::DraftIssue => None,
12359 }
12360 }
12361
12362 /// Whether its `Priority` field holds a value at all, mapped or not.
12363 fn holds_priority(&self) -> bool {
12364 self.priority != HeldPriority::Read(Priority::None)
12365 }
12366
12367 /// The task this item is.
12368 ///
12369 /// Fails for an item whose `Priority` field holds an option the mapping does not name:
12370 /// reading that as a level would be a guess, and reading it as `none` would let the next
12371 /// copy clear a priority a person set.
12372 fn task(&self) -> Result<Task, SourceError> {
12373 let priority = match &self.priority {
12374 HeldPriority::Read(priority) => *priority,
12375 HeldPriority::Unmapped(option) => {
12376 return Err(SourceError::Malformed {
12377 message: format!(
12378 "task {}{} sits in the board {PRIORITY_FIELD} option {option:?}, which \
12379 this source's priority_mapping does not name, so its priority cannot be \
12380 read; next: name {option:?} under priority_mapping, or move the item to \
12381 a mapped option",
12382 self.id,
12383 self.number
12384 .map(|number| format!(" (#{number})"))
12385 .unwrap_or_default()
12386 ),
12387 });
12388 }
12389 };
12390 Ok(Task {
12391 version: self.version(),
12392 id: self.id.clone(),
12393 key: self.key(),
12394 title: self.title.clone(),
12395 content: self.body.clone(),
12396 status: self.status.clone(),
12397 priority,
12398 labels: self.labels.clone(),
12399 project: self.parent.clone(),
12400 url: self.url.clone(),
12401 location: self.location(),
12402 created_at: self.created_at,
12403 updated_at: self.updated_at,
12404 metadata: self.metadata(),
12405 repositories: self.repositories.clone(),
12406 delivers: self.delivers.clone(),
12407 delivered_by: self.delivered_by.clone(),
12408 classification: self.classification,
12409 })
12410 }
12411
12412 fn project(&self) -> Project {
12413 Project {
12414 version: self.version(),
12415 id: self.id.clone(),
12416 title: self.title.clone(),
12417 content: self.body.clone(),
12418 status: self.status.clone(),
12419 labels: self.labels.clone(),
12420 url: self.url.clone(),
12421 location: self.location(),
12422 created_at: self.created_at,
12423 updated_at: self.updated_at,
12424 metadata: self.metadata(),
12425 repositories: self.repositories.clone(),
12426 classification: self.classification,
12427 }
12428 }
12429
12430 /// The same issue as a document: the project it is filed under, and no status and no
12431 /// dependencies, because a document is not work.
12432 fn document(&self) -> Document {
12433 Document {
12434 version: self.version(),
12435 id: self.id.clone(),
12436 title: self.title.clone(),
12437 content: self.body.clone(),
12438 project: self.parent.clone(),
12439 labels: self.labels.clone(),
12440 url: self.url.clone(),
12441 location: self.location(),
12442 created_at: self.created_at,
12443 updated_at: self.updated_at,
12444 metadata: self.metadata(),
12445 repositories: self.repositories.clone(),
12446 classification: self.classification,
12447 }
12448 }
12449}
12450
12451/// Where one targeted update moves an item's status, and which of its two halves move.
12452struct StatusMove {
12453 /// The board the item's `Status` field is on.
12454 board: BoardId,
12455 /// The `Status` field's id.
12456 field: String,
12457 /// The option's id.
12458 option: String,
12459 /// The option's name, as the board spells it.
12460 name: String,
12461 /// What the status asks of the issue's state.
12462 target: StatusTarget,
12463 /// The status the item reads as once it is there.
12464 landed: Status,
12465 /// Which of the status's two halves differ from what the item holds.
12466 moves: Moves,
12467}
12468
12469/// Which halves of an item's status one targeted update moves: its `Status` option, the open or
12470/// closed state of its issue, or both. A status neither half of which differs is no move at all,
12471/// and is not a value of this type.
12472#[derive(Clone, Copy, PartialEq, Eq)]
12473enum Moves {
12474 /// The option alone.
12475 Option,
12476 /// The issue's state alone: open, closed, or closed with another reason.
12477 State,
12478 /// Both.
12479 Both,
12480}
12481
12482impl Moves {
12483 /// What differs, or `None` when nothing does.
12484 const fn of(option: bool, state: bool) -> Option<Self> {
12485 match (option, state) {
12486 (true, true) => Some(Self::Both),
12487 (true, false) => Some(Self::Option),
12488 (false, true) => Some(Self::State),
12489 (false, false) => None,
12490 }
12491 }
12492
12493 /// Whether the option moves.
12494 const fn option(self) -> bool {
12495 matches!(self, Self::Option | Self::Both)
12496 }
12497
12498 /// Whether the issue's state moves.
12499 const fn state(self) -> bool {
12500 matches!(self, Self::State | Self::Both)
12501 }
12502}
12503
12504/// What one write is, and the status that comes with being it.
12505///
12506/// One value rather than a [`BoardKind`] beside an `Option<Status>`: a document has no
12507/// status and a task or a project always has one, so "a document carrying a status" and
12508/// "a task carrying none" are states a write cannot be in rather than states every use
12509/// site below has to defend against.
12510enum Written<'a> {
12511 /// A document, which is not work and so has no status at all.
12512 Document,
12513 /// A task or a project, and the status it is being written with.
12514 Work(ItemKind, &'a Status),
12515}
12516
12517impl Written<'_> {
12518 /// Which of the board's three kinds this write is.
12519 const fn kind(&self) -> BoardKind {
12520 match self {
12521 Self::Document => BoardKind::Document,
12522 Self::Work(kind, _) => BoardKind::Work(*kind),
12523 }
12524 }
12525
12526 /// The status this write carries. A document carries none, so a write of one says
12527 /// nothing about the issue's open or closed state and selects no board `Status`
12528 /// option.
12529 const fn status(&self) -> Option<&Status> {
12530 match self {
12531 Self::Document => None,
12532 Self::Work(_, status) => Some(status),
12533 }
12534 }
12535
12536 /// The status this write carries with the kind whose half of `status_mapping` it is
12537 /// written through.
12538 const fn work_status(&self) -> Option<(ItemKind, &Status)> {
12539 match self {
12540 Self::Document => None,
12541 Self::Work(kind, status) => Some((*kind, status)),
12542 }
12543 }
12544}
12545
12546/// The item being written, in the one shape all three write methods reach.
12547struct Incoming<'a> {
12548 written: Written<'a>,
12549 /// The title a person wrote. A document's goes onto the issue with
12550 /// [`DESIGN_TITLE_PREFIX`] put back, so a round trip returns the title that went in.
12551 title: &'a str,
12552 content: Option<&'a str>,
12553 assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
12554 labels: &'a [Label],
12555 metadata: &'a BTreeMap<String, Value>,
12556 repositories: &'a [Repository],
12557 /// Recorded in the slot while private, and nowhere while public.
12558 classification: Classification,
12559 parent: Option<&'a NativeId>,
12560 /// [`Task::delivers`], already checked. Empty for a project or a document, which is
12561 /// what keeps either key out of their slot.
12562 delivers: &'a [TaskRef],
12563 /// [`Task::delivered_by`], already checked. Empty for a project or a document.
12564 delivered_by: &'a [TaskRef],
12565 /// [`Task::priority`], for a task written to an instance that holds one; `None` for a
12566 /// project, a document, and every write to an instance with no `priority_mapping` —
12567 /// which is what keeps such a write's requests exactly what they were before.
12568 priority: Option<Priority>,
12569}
12570
12571/// What one write does to one projected metadata text field, by the field's id.
12572#[derive(Debug, Clone)]
12573enum ProjectionWrite {
12574 /// Write this text.
12575 Set {
12576 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.
12577 text: String,
12578 },
12579 /// Clear the field's value.
12580 Clear { field: String }, // llmlint: ignore[invalid_states_unrepresentable] As above.
12581}
12582
12583impl ProjectionWrite {
12584 /// Add this write to a field write's list of values and list of clears.
12585 fn join<'a>(&'a self, values: &mut Vec<(String, Value)>, clears: &mut Vec<&'a str>) {
12586 match self {
12587 Self::Set { field, text } => values.push((field.clone(), json!({"text": text}))),
12588 Self::Clear { field } => clears.push(field),
12589 }
12590 }
12591}
12592
12593/// What one write does to an item's `Priority` field.
12594enum PriorityWrite {
12595 /// Select this option of this field.
12596 Select {
12597 /// The `Priority` field's id.
12598 field: String,
12599 /// The mapped option's id.
12600 option: String,
12601 },
12602 /// Clear the field's value, which is what `none` is.
12603 Clear {
12604 /// The `Priority` field's id.
12605 field: String,
12606 },
12607}
12608
12609impl Incoming<'_> {
12610 /// The title this write puts on the issue.
12611 fn written_title(&self) -> String {
12612 match self.written {
12613 Written::Document => format!("{DESIGN_TITLE_PREFIX}{}", self.title),
12614 Written::Work(..) => self.title.to_owned(),
12615 }
12616 }
12617}
12618
12619#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
12620enum ContentKind {
12621 DraftIssue,
12622 Issue,
12623}
12624
12625/// What one board issue is: a document, or the work an [`ItemKind`] names.
12626///
12627/// A type of this source's own rather than an `ItemKind` with a third variant, because
12628/// `ItemKind` names what a dependency endpoint points at and nothing may point at a
12629/// document — the contract keeps a document out of that enum deliberately. Holding the
12630/// board's three answers in one value is what makes every place that asks "which is this?"
12631/// answer all three, rather than a `document: bool` beside a `kind` that means nothing for
12632/// two thirds of the board.
12633#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
12634enum BoardKind {
12635 /// An issue whose title begins [`DESIGN_TITLE_PREFIX`].
12636 Document,
12637 /// Every other issue, and every draft.
12638 Work(ItemKind),
12639}
12640
12641impl BoardKind {
12642 /// The kind of an item titled `title`, filed under a project or not, holding `sub_issues`
12643 /// sub-issues and carrying the kind marker `marked` in its slot.
12644 ///
12645 /// The design prefix is read *first*, before either of the two rules that separate a
12646 /// project from a task. A document is not work whatever sub-issues it has and whatever
12647 /// marker it carries, and reading the prefix later would make a design issue with none of
12648 /// either an empty project. Being a sub-issue wins outright over the rest, and no marker
12649 /// overrides it: an issue filed under a project is that project's task even when it has
12650 /// sub-issues of its own.
12651 fn of(title: &str, filed: bool, sub_issues: u64, marked: Option<ItemKind>) -> Self {
12652 if title.starts_with(DESIGN_TITLE_PREFIX) {
12653 Self::Document
12654 } else if filed {
12655 Self::Work(ItemKind::Task)
12656 } else if sub_issues > 0 || marked == Some(ItemKind::Project) {
12657 Self::Work(ItemKind::Project)
12658 } else {
12659 Self::Work(ItemKind::Task)
12660 }
12661 }
12662
12663 /// What the change token's facets call this kind.
12664 const fn facet(self) -> change_token::FacetKind {
12665 match self {
12666 Self::Document => change_token::FacetKind::Document,
12667 Self::Work(ItemKind::Project) => change_token::FacetKind::Project,
12668 Self::Work(ItemKind::Task) => change_token::FacetKind::Task,
12669 }
12670 }
12671
12672 /// Whose half of `status_mapping` an item of this kind reads its status through. A
12673 /// document has no status of its own, so the task half stands in for whatever the issue
12674 /// holds; nothing reports it.
12675 const fn status_kind(self) -> ItemKind {
12676 match self {
12677 Self::Document => ItemKind::Task,
12678 Self::Work(kind) => kind,
12679 }
12680 }
12681
12682 /// How a refusal names this kind to the person reading it.
12683 const fn describes(self) -> &'static str {
12684 match self {
12685 Self::Document => "document",
12686 Self::Work(kind) => kind.marker(),
12687 }
12688 }
12689}
12690
12691/// Whether `labels` satisfies `filter`, matching by name, case-insensitively.
12692///
12693/// This is the local Markdown source's `labels_match`, spelled the same way on purpose:
12694/// the shared cross-source journeys assert one answer to one question, so two sources
12695/// that disagree about what "carries the label bug" means fail them.
12696fn labels_match(labels: &[Label], filter: &LabelFilter) -> bool {
12697 let holds = |name: &String| {
12698 labels
12699 .iter()
12700 .any(|label| label.name.eq_ignore_ascii_case(name))
12701 };
12702 (filter.any_of.is_empty() || filter.any_of.iter().any(holds))
12703 && filter.all_of.iter().all(holds)
12704 && !filter.none_of.iter().any(holds)
12705}
12706
12707/// Whether `category` is one of `statuses`. An empty list is unfiltered rather than
12708/// "keeps nothing", which is what lets a `Vec<StatusCategory>` spell no filter at all.
12709fn status_matches(category: StatusCategory, statuses: &[StatusCategory]) -> bool {
12710 statuses.is_empty() || statuses.contains(&category)
12711}
12712
12713/// Whether `title`/`content` satisfies `query`, matching case-insensitively.
12714///
12715/// `content` is the item's own prose — the body with this source's trailing metadata
12716/// comment already taken off — so a search never matches an encoding the author of the
12717/// issue never wrote.
12718fn text_matches(title: &str, content: Option<&str>, query: &TextQuery) -> bool {
12719 let terms = query.terms.to_lowercase();
12720 let in_title = title.to_lowercase().contains(&terms);
12721 let in_content = content.is_some_and(|body| body.to_lowercase().contains(&terms));
12722 match query.fields {
12723 TextFields::Title => in_title,
12724 TextFields::Content => in_content,
12725 TextFields::TitleOrContent => in_title || in_content,
12726 }
12727}
12728
12729/// Whether `task` satisfies `query`, with `project` deciding the project predicate.
12730///
12731/// The project predicate is passed separately because a read narrowed to one project has
12732/// already answered it by asking *that project* for its own items — and re-applying it
12733/// there would compare the caller's selector, which may be a project's **name**, against
12734/// the id of the project that name resolved to, and keep nothing. Every other read passes
12735/// `query.project` and applies it here, which is what keeps `projects` a predicate this
12736/// source really does apply.
12737fn task_matches(task: &Task, query: &TaskQuery, project: &ProjectFilter) -> bool {
12738 labels_match(&task.labels, &query.labels)
12739 && status_matches(task.status.category, &query.statuses)
12740 && (query.priorities.is_empty() || query.priorities.contains(&task.priority))
12741 && match project {
12742 ProjectFilter::Any => true,
12743 ProjectFilter::Orphans => task.project.is_none(),
12744 ProjectFilter::Is(id) => task.project.as_ref() == Some(id),
12745 }
12746 && query
12747 .text
12748 .as_ref()
12749 .is_none_or(|text| text_matches(&task.title, task.content.as_deref(), text))
12750 // Against the parsed metadata slot, and against the origin field, which is where
12751 // `Resolved::metadata` reads each of them from.
12752 && query.metadata_matches(&task.metadata)
12753 && query.origin_matches(&task.metadata)
12754}
12755
12756fn project_matches(project: &Project, query: &ProjectQuery) -> bool {
12757 labels_match(&project.labels, &query.labels)
12758 && status_matches(project.status.category, &query.statuses)
12759 && query
12760 .text
12761 .as_ref()
12762 .is_none_or(|text| text_matches(&project.title, project.content.as_deref(), text))
12763}
12764
12765/// The same three predicates a task query carries, minus the status filter.
12766///
12767/// A document is not work, so it has no status for one to compare against and the query
12768/// type carries none. The project predicate is the same one — a design issue filed under a
12769/// project issue is in that project, and one filed under nothing is in none — so it is
12770/// spelled the same way here rather than answered differently.
12771fn document_matches(document: &Document, query: &DocumentQuery, project: &ProjectFilter) -> bool {
12772 labels_match(&document.labels, &query.labels)
12773 && match project {
12774 ProjectFilter::Any => true,
12775 ProjectFilter::Orphans => document.project.is_none(),
12776 ProjectFilter::Is(id) => document.project.as_ref() == Some(id),
12777 }
12778 && query
12779 .text
12780 .as_ref()
12781 .is_none_or(|text| text_matches(&document.title, document.content.as_deref(), text))
12782}
12783
12784/// Why a GitHub item's change token does not vouch for its forward edges.
12785///
12786/// A forward edge names its far end and that far end's kind, and the near item's token moves
12787/// with the edges it holds but says nothing of the far ends themselves: a far end turning from a
12788/// task into a project when it gains a sub-issue, retitled into a document, or deleted moves
12789/// nothing the near token reads, so an edge kept at the near version could name a far end that
12790/// is no longer what it said.
12791const FAR_ENDS_UNCOVERED: &str = "a github item's change token does not attest a far end's kind, \
12792 whether it became a document, or its deletion, so its forward edges are always read from \
12793 github";
12794
12795/// The issue-search qualifier that keeps open issues alone, which a status filter naming only
12796/// categories an open issue reads as narrows a board-scoped search with.
12797const OPEN_QUALIFIER: &str = "is:open";
12798
12799/// The board filter `task status reconcile` reads the board through: closed items alone.
12800const CLOSED_FILTER: &str = "is:closed";
12801
12802/// The variables of one [`graphql::ISSUES_BY_REFERENCE`] request for `batch`: each slot names
12803/// one reference, and a slot the batch has none for names its last one again.
12804fn reference_batch(batch: &[IssueRef]) -> Value {
12805 let mut variables = serde_json::Map::new();
12806 for slot in 0..REFERENCE_BATCH {
12807 let issue = batch.get(slot).or(batch.last());
12808 variables.insert(format!("owner{slot}"), json!(issue.map(|i| &i.owner)));
12809 variables.insert(format!("name{slot}"), json!(issue.map(|i| &i.name)));
12810 variables.insert(format!("number{slot}"), json!(issue.map(|i| i.number)));
12811 }
12812 variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
12813 variables.insert("statusField".to_owned(), json!(STATUS_FIELD));
12814 variables.insert("duplicates".to_owned(), json!(true));
12815 Value::Object(variables)
12816}
12817
12818/// One issue an issue read by reference answered, in the shape every other read of an issue
12819/// hands its resolver: each board membership's `Status` value, read by name, as the one field
12820/// value that membership carries.
12821///
12822/// A membership's `status` is `null` when its card sits at no option — or on a board with no
12823/// field of that name — and otherwise an option value naming its option; anything else is not
12824/// an answer to this read, and is refused rather than read as a card at no option.
12825fn held_issue(issue: &Value) -> Result<Value, SourceError> {
12826 let malformed = |what: &str| SourceError::Malformed {
12827 message: format!("GitHub answered an issue read by reference with {what}"),
12828 };
12829 let mut issue = issue.clone();
12830 let nodes = issue
12831 .pointer_mut("/projectItems/nodes")
12832 .and_then(Value::as_array_mut)
12833 .ok_or_else(|| malformed("no list of board memberships"))?;
12834 for node in nodes {
12835 let node = node
12836 .as_object_mut()
12837 .ok_or_else(|| malformed("a board membership that is not an object"))?;
12838 let status = match node.get("status") {
12839 None => return Err(malformed("a board membership with no status member")),
12840 Some(Value::Null) => None,
12841 Some(status) if status.get("name").is_some_and(Value::is_string) => {
12842 Some(status.clone())
12843 }
12844 // A single-select value of another field type answers `{}`; only an option value
12845 // carries a name.
12846 Some(status) if status.as_object().is_some_and(serde_json::Map::is_empty) => None,
12847 Some(_) => return Err(malformed("a membership status that names no option")),
12848 };
12849 node.insert(
12850 "fieldValues".to_owned(),
12851 json!({"nodes": status.into_iter().collect::<Vec<_>>(),
12852 "pageInfo": {"hasNextPage": false}}),
12853 );
12854 }
12855 Ok(issue)
12856}
12857
12858/// The issue one slot of a read by reference answered, `None` when GitHub answered that
12859/// slot's repository or issue `null` — which [`GitHubProjectsSource::answer_unresolved`]
12860/// admitted only beside a `NOT_FOUND` naming that very slot — and a refusal when the answer
12861/// leaves the slot, or its `issue` member, out: an answer missing a member is malformed rather
12862/// than a reference nothing holds.
12863fn referenced_issue(data: &Value, slot: usize) -> Result<Option<&Value>, SourceError> {
12864 let alias = format!("r{slot}");
12865 let repository = data.get(&alias).ok_or_else(|| SourceError::Malformed {
12866 message: format!("GitHub answered a read by reference with no {alias}"),
12867 })?;
12868 if repository.is_null() {
12869 return Ok(None);
12870 }
12871 let issue = repository
12872 .get("issue")
12873 .ok_or_else(|| SourceError::Malformed {
12874 message: format!("GitHub answered {alias} of a read by reference with no issue"),
12875 })?;
12876 if issue.is_null() {
12877 return Ok(None);
12878 }
12879 // `Repository.issue` is typed `Issue`, so anything else is not an answer to this read.
12880 if optional_str(issue, "__typename")? != Some("Issue") {
12881 return Err(SourceError::Malformed {
12882 message: format!(
12883 "GitHub answered {alias} of a read by reference with an issue whose __typename \
12884 is not Issue"
12885 ),
12886 });
12887 }
12888 Ok(Some(issue))
12889}
12890
12891/// A closure reason as a person reads it: `NOT_PLANNED` as `not planned`.
12892fn spoken_reason(reason: &str) -> String {
12893 reason.to_ascii_lowercase().replace('_', " ")
12894}
12895
12896impl GitHubProjectsSource {
12897 /// Every task and project of this board whose card contradicts its issue's closure, each
12898 /// judged against its own kind's half of `status_mapping`, read through the board's own
12899 /// `is:closed` filter, page by page — never the board's unfiltered `items`.
12900 ///
12901 /// Each item the filter answers is confirmed from its own node: an open one, one whose
12902 /// closure reads `unknown`, and one whose card is already in line are left out whatever
12903 /// the filter let through. The board's fields ride along and are held for the option
12904 /// writes that follow, so those need no read of the board.
12905 async fn contradictions(&self) -> Result<Vec<StatusContradiction>, SourceError> {
12906 let mut after: Option<String> = None;
12907 let mut found = Vec::new();
12908 loop {
12909 let data = self
12910 .graphql(
12911 graphql::CLOSED_ITEMS,
12912 json!({"owner":self.owner,"number":self.project_number,
12913 "first":MAX_PAGE_SIZE,"after":after,"filter":CLOSED_FILTER,
12914 "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
12915 )
12916 .await?;
12917 let board = data
12918 .pointer("/closedItems/projectV2")
12919 .filter(|value| !value.is_null())
12920 .ok_or_else(|| SourceError::Refused {
12921 message: format!(
12922 "GitHub project {}/{} was not found or is not visible to the token",
12923 self.owner, self.project_number
12924 ),
12925 })?;
12926 *self.fields_cache()? = Some(BoardFields {
12927 id: BoardId::parse(required_str(board, "id")?)?,
12928 fields: board.get("fields").cloned().unwrap_or(Value::Null),
12929 });
12930 let items = board.get("items").ok_or_else(|| SourceError::Malformed {
12931 message: "GitHub project closed items are missing".into(),
12932 })?;
12933 for item in items
12934 .get("nodes")
12935 .and_then(Value::as_array)
12936 .ok_or_else(|| SourceError::Malformed {
12937 message: "GitHub project items.nodes is not an array".into(),
12938 })?
12939 {
12940 let Some(resolved) = self.resolve(item)? else {
12941 continue;
12942 };
12943 let BoardKind::Work(kind) = resolved.kind else {
12944 continue;
12945 };
12946 let Some((closed_as, name)) = self.statuses.contradicted(
12947 kind,
12948 resolved.option.as_deref(),
12949 resolved.closed,
12950 resolved.reason.as_deref(),
12951 ) else {
12952 continue;
12953 };
12954 found.push(StatusContradiction {
12955 closed_as,
12956 to: name.to_owned(),
12957 option: resolved.option.clone(),
12958 item: match kind {
12959 ItemKind::Task => ContradictedItem::Task(resolved.task()?),
12960 ItemKind::Project => ContradictedItem::Project(resolved.project()),
12961 },
12962 });
12963 }
12964 let info = items
12965 .get("pageInfo")
12966 .ok_or_else(|| SourceError::Malformed {
12967 message: "GitHub project items have no pageInfo".into(),
12968 })?;
12969 if !required_bool(info, "hasNextPage")? {
12970 return Ok(found);
12971 }
12972 let next = required_str(info, "endCursor")?;
12973 validate_cursor_progress(after.as_deref(), next)?;
12974 after = Some(next.to_owned());
12975 }
12976 }
12977
12978 /// What this board holds for each issue in `issues`, once each in the order given, read
12979 /// [`REFERENCE_BATCH`] references at a time — and nothing else of the board.
12980 ///
12981 /// An issue GitHub cannot find for this source's credential, and one with no item on this
12982 /// board, are both absent. A closure that already says the work will not be done here —
12983 /// not planned, duplicate — is skipped: a landing does not overrule it. An item closed as
12984 /// completed whose card sits at its kind's `done` option is unchanged, and every other item
12985 /// is due a `done` write. A project item — a plan's home card, its issue holding the plan's
12986 /// tasks as sub-issues — is held under exactly these rules, judged against the project
12987 /// kind's half of `status_mapping`.
12988 async fn holdings(&self, issues: &[IssueRef]) -> Result<Vec<IssueHolding>, SourceError> {
12989 let mut distinct: Vec<IssueRef> = Vec::with_capacity(issues.len());
12990 for issue in issues {
12991 if !distinct.contains(issue) {
12992 distinct.push(issue.clone());
12993 }
12994 }
12995 let done = |kind: ItemKind| {
12996 self.statuses
12997 .target(kind, StatusCategory::Done)
12998 .option()
12999 .unwrap_or("Done")
13000 .to_owned()
13001 };
13002 // GitHub numbers an issue with a GraphQL `Int`, a signed 32-bit value, so a reference past
13003 // its range names no issue any repository can hold: it is answered absent and never sent,
13004 // where binding it would have GitHub refuse the whole batch.
13005 let readable: Vec<IssueRef> = distinct
13006 .iter()
13007 .filter(|issue| i32::try_from(issue.number).is_ok())
13008 .cloned()
13009 .collect();
13010 let mut holdings = Vec::with_capacity(readable.len());
13011 for batch in readable.chunks(REFERENCE_BATCH) {
13012 let data = self
13013 .graphql(graphql::ISSUES_BY_REFERENCE, reference_batch(batch))
13014 .await?;
13015 for (slot, issue) in batch.iter().enumerate() {
13016 let node = referenced_issue(&data, slot)?;
13017 let resolved = match node {
13018 Some(node) => self.resolve_issue_as(&held_issue(node)?, false).await?,
13019 None => None,
13020 };
13021 // A task and a project are both held; a document is not work, so nothing is.
13022 let Some((item, kind)) = resolved.and_then(|item| match item.kind {
13023 BoardKind::Work(kind) => Some((item, kind)),
13024 BoardKind::Document => None,
13025 }) else {
13026 holdings.push(IssueHolding {
13027 issue: issue.clone(),
13028 completion: Completion::Absent,
13029 });
13030 continue;
13031 };
13032 let task = match kind {
13033 ItemKind::Task => {
13034 let held = item.task()?;
13035 HeldTask {
13036 id: held.id,
13037 kind,
13038 option: item.option.clone(),
13039 classification: held.classification,
13040 repositories: held.repositories,
13041 delivers: held.delivers,
13042 }
13043 }
13044 // A project delivers no task, so what its write keeps in step is nothing.
13045 ItemKind::Project => {
13046 let held = item.project();
13047 HeldTask {
13048 id: held.id,
13049 kind,
13050 option: item.option.clone(),
13051 classification: held.classification,
13052 repositories: held.repositories,
13053 delivers: Vec::new(),
13054 }
13055 }
13056 };
13057 let done = done(kind);
13058 let completion = match (item.closed, item.reason.as_deref()) {
13059 (true, Some(reason @ ("NOT_PLANNED" | "DUPLICATE"))) => Completion::Skipped {
13060 task,
13061 reason: format!("closed as {}", spoken_reason(reason)),
13062 },
13063 (true, None | Some("COMPLETED"))
13064 if item
13065 .option
13066 .as_deref()
13067 .is_some_and(|option| option.eq_ignore_ascii_case(&done)) =>
13068 {
13069 Completion::Unchanged { task }
13070 }
13071 _ => {
13072 let kept = item.clone();
13073 self.held_cache
13074 .lock()
13075 .map_err(|_| SourceError::Unavailable {
13076 message: "items read by issue reference were left \
13077 inconsistent; run the command again"
13078 .into(),
13079 })?
13080 .insert(kept.id.clone(), kept);
13081 Completion::Due {
13082 task,
13083 to: done.clone(),
13084 }
13085 }
13086 };
13087 holdings.push(IssueHolding {
13088 issue: issue.clone(),
13089 completion,
13090 });
13091 }
13092 }
13093 let mut read = holdings.into_iter();
13094 let mut answered = Vec::with_capacity(distinct.len());
13095 for issue in distinct {
13096 if i32::try_from(issue.number).is_ok() {
13097 answered.push(read.next().ok_or_else(|| SourceError::Malformed {
13098 message: format!("no holding was read for {issue}"),
13099 })?);
13100 } else {
13101 answered.push(IssueHolding {
13102 issue,
13103 completion: Completion::Absent,
13104 });
13105 }
13106 }
13107 Ok(answered)
13108 }
13109}
13110
13111#[async_trait::async_trait]
13112impl TaskSource for GitHubProjectsSource {
13113 fn kind(&self) -> &'static str {
13114 KIND
13115 }
13116 fn order_versions(&self, a: &str, b: &str) -> Option<std::cmp::Ordering> {
13117 change_token::order(a, b)
13118 }
13119 /// A task's fields and comments: §4.13d of `docs/plugin-protocol.md` states, member by
13120 /// member, the change each part of the token moves for. Not its forward edges — see
13121 /// [`FAR_ENDS_UNCOVERED`].
13122 fn version_coverage(&self) -> VersionCoverage {
13123 VersionCoverage {
13124 forward_edges: Coverage::Uncovered {
13125 reason: FAR_ENDS_UNCOVERED.to_owned(),
13126 },
13127 ..VersionCoverage::whole()
13128 }
13129 }
13130 /// Every part, for a copy's skip: the relationship count moves for an edge added or removed
13131 /// and for a far end deleted, so the token sees every change to a task's edges but one — a
13132 /// far end's own kind flipping, by gaining a sub-issue or being retitled into a document,
13133 /// which `spike-coverage` measured stale 1 of 1. A skipping recopy does not carry that, and
13134 /// the next copy that reads the task does (`docs/plugin-protocol.md` §4.13d).
13135 // 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.
13136 fn copy_version_coverage(&self) -> VersionCoverage {
13137 VersionCoverage::whole()
13138 }
13139 /// A project's fields, and not its forward edges, for the same reason a task's are not.
13140 fn project_version_coverage(&self) -> ProjectVersionCoverage {
13141 ProjectVersionCoverage {
13142 fields: Coverage::Covered,
13143 forward_edges: Coverage::Uncovered {
13144 reason: FAR_ENDS_UNCOVERED.to_owned(),
13145 },
13146 }
13147 }
13148 /// A document's fields and the manifest of its image assets, which its metadata slot
13149 /// records: both are the issue's body, whose every edit moves the token's issue time.
13150 fn document_version_coverage(&self) -> DocumentVersionCoverage {
13151 DocumentVersionCoverage::whole()
13152 }
13153 fn write_context(&self, id: &NativeId) -> Option<Value> {
13154 self.context_of(id)
13155 }
13156 fn board_context(&self) -> Option<Value> {
13157 self.board_context_held()
13158 }
13159 fn prime_write(&self, id: &NativeId, context: &Value, board: Option<&Value>) -> bool {
13160 self.prime(id, context, board)
13161 }
13162 fn unprime(&self, id: &NativeId) {
13163 self.unprime_held(id);
13164 }
13165 fn begin_journal(&self) {
13166 self.expected.begin();
13167 }
13168 fn end_journal(&self) -> Option<WriteJournal> {
13169 Some(self.expected.end())
13170 }
13171 fn capabilities(&self) -> Capabilities {
13172 Capabilities {
13173 projects: Support::Native,
13174 documents: Support::Native,
13175 comments: Support::Native,
13176 assets: Support::Native,
13177 priority: if self.priorities.is_some() {
13178 Support::Native
13179 } else {
13180 Support::Unsupported
13181 },
13182 filter_by_priority: Support::Native,
13183 filter_by_comment_activity: Support::Native,
13184 filter_by_metadata: Support::Native,
13185 filter_by_origin: Support::Native,
13186 reconcile_status: Support::Native,
13187 complete_by_issue: Support::Native,
13188 orphan_tasks: Support::Native,
13189 filter_by_label: Support::Native,
13190 filter_by_status: Support::Native,
13191 search_title: Support::Native,
13192 search_content: Support::Native,
13193 task_dependencies: DependencySupport::BothDirections,
13194 project_dependencies: DependencySupport::BothDirections,
13195 max_page_size: MAX_PAGE_SIZE,
13196 }
13197 }
13198 async fn visibility(
13199 &self,
13200 target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13201 ) -> Result<onetaskgraph_plugin_api::Visibility, SourceError> {
13202 self.write_visibility(target).await
13203 }
13204 /// Where a create of `target` would file its issue, by [`Self::creation_rule`] — the rule
13205 /// the create applies — reading the parent project's issue only when `target` names one.
13206 async fn creation_repository(
13207 &self,
13208 target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13209 ) -> Result<Option<Repository>, SourceError> {
13210 let onetaskgraph_plugin_api::WriteTarget::New {
13211 repositories,
13212 project,
13213 } = target
13214 else {
13215 return Ok(None);
13216 };
13217 let target = self
13218 .creation_rule(repositories, *project, "a new item")
13219 .await?;
13220 Repository::try_from(target.origin())
13221 .map(Some)
13222 .map_err(|message| SourceError::Malformed { message })
13223 }
13224 async fn health(&self) -> Result<Health, SourceError> {
13225 let board = self.board_page(None, 1).await?;
13226 Ok(Health {
13227 reachable: true,
13228 detail: Some(format!(
13229 "reading GitHub project {}/{} ({})",
13230 self.owner,
13231 self.project_number,
13232 required_str(&board, "title")?
13233 )),
13234 })
13235 }
13236 /// The board's own node id, digested: the one request reading its fields, or none when
13237 /// this command has read them already.
13238 async fn designator(&self) -> Result<Option<Designator>, SourceError> {
13239 let board = self.board_fields().await?;
13240 Designator::of_backend(KIND, board.id.as_str())
13241 .map(Some)
13242 .map_err(|message| SourceError::Malformed { message })
13243 }
13244 async fn get_task(&self, id: &NativeId) -> Result<Option<Task>, SourceError> {
13245 self.item_by_id(id)
13246 .await?
13247 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
13248 .map(|item| item.task())
13249 .transpose()
13250 }
13251 async fn task_assets(
13252 &self,
13253 id: &NativeId,
13254 ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
13255 self.held_assets(id, BoardKind::Work(ItemKind::Task)).await
13256 }
13257 async fn task_asset(
13258 &self,
13259 id: &NativeId,
13260 name: &onetaskgraph_plugin_api::AssetName,
13261 ) -> Result<Option<Vec<u8>>, SourceError> {
13262 self.held_asset(id, BoardKind::Work(ItemKind::Task), name)
13263 .await
13264 }
13265 async fn set_task_rendering_with_assets(
13266 &self,
13267 id: &NativeId,
13268 content: &str,
13269 provenance: &Value,
13270 _answers: &BTreeMap<String, Value>,
13271 assets: &onetaskgraph_plugin_api::AssetWrite,
13272 ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
13273 self.replace_rendering(
13274 id,
13275 BoardKind::Work(ItemKind::Task),
13276 content,
13277 provenance,
13278 Some(assets),
13279 )
13280 .await
13281 }
13282 async fn document_assets(
13283 &self,
13284 id: &NativeId,
13285 ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
13286 self.held_assets(id, BoardKind::Document).await
13287 }
13288 async fn document_asset(
13289 &self,
13290 id: &NativeId,
13291 name: &onetaskgraph_plugin_api::AssetName,
13292 ) -> Result<Option<Vec<u8>>, SourceError> {
13293 self.held_asset(id, BoardKind::Document, name).await
13294 }
13295 async fn set_document_rendering_with_assets(
13296 &self,
13297 id: &NativeId,
13298 content: &str,
13299 provenance: &Value,
13300 _answers: &BTreeMap<String, Value>,
13301 assets: &onetaskgraph_plugin_api::AssetWrite,
13302 ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
13303 self.replace_rendering(id, BoardKind::Document, content, provenance, Some(assets))
13304 .await
13305 }
13306 async fn get_project(&self, id: &NativeId) -> Result<Option<Project>, SourceError> {
13307 Ok(self
13308 .item_by_id(id)
13309 .await?
13310 .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
13311 .map(|item| item.project()))
13312 }
13313 async fn query_tasks(
13314 &self,
13315 query: &TaskQuery,
13316 page: &PageRequest,
13317 ) -> Result<Page<Task>, SourceError> {
13318 validate_page(page)?;
13319 refuse_unsearchable(query)?;
13320 // A closed issue reads `done`, `cancelled` or — closed for a reason that implies no
13321 // category — `unknown`; every other category reads only off an open issue or a draft. So
13322 // a status filter naming none of those three asks every issue search it sends for open
13323 // issues alone; each candidate's category is still confirmed from its own item below.
13324 let open_only = !query.statuses.is_empty()
13325 && query.statuses.iter().all(|category| {
13326 !matches!(
13327 category,
13328 StatusCategory::Done | StatusCategory::Cancelled | StatusCategory::Unknown
13329 )
13330 });
13331 if query.origin.is_none() && !matches!(query.project, ProjectFilter::Is(_)) {
13332 let mut qualifiers: Vec<String> = [
13333 query.commented_since.map(updated_qualifier),
13334 narrowing_qualifiers(query),
13335 ]
13336 .into_iter()
13337 .flatten()
13338 .collect();
13339 // A narrowed search never answers a draft, which is no issue, so narrowing it to
13340 // open issues too leaves out nothing such a search could have answered.
13341 if !qualifiers.is_empty() {
13342 if open_only {
13343 qualifiers.push(OPEN_QUALIFIER.to_owned());
13344 }
13345 return self.search_tasks(query, page, &qualifiers.join(" ")).await;
13346 }
13347 }
13348
13349 // A read narrowed to one project asks that project for its own tasks, so nothing
13350 // about it costs what the rest of the board holds. A read carrying a text, metadata
13351 // or origin predicate asks GitHub the narrower question those predicates are, and a
13352 // read narrowed to comment activity alone asks the board's own issue search for the
13353 // issues updated since, which is every issue a comment could have been written or
13354 // edited on since. Every other task read is a question about the whole board and is
13355 // answered by reading it.
13356 let (held, membership) = match (&query.project, query.commented_since) {
13357 (ProjectFilter::Is(project), _) => (
13358 match self.held_task_children(project)? {
13359 // Taken as `project_children` takes the project's own sub-issue read: with
13360 // every item this process wrote under it since, and none it moved away.
13361 Some(held) => self.completed_with_written(
13362 held.into_iter()
13363 .filter(|child| child.parent.as_ref() == Some(project))
13364 .collect(),
13365 |own| own.parent.as_ref() == Some(project),
13366 )?,
13367 None => self.project_children(project).await?,
13368 },
13369 // Answered by where these items came from; see `task_matches`.
13370 &ProjectFilter::Any,
13371 ),
13372 (ProjectFilter::Any | ProjectFilter::Orphans, since) => {
13373 match (self.narrowed(query).await?, since) {
13374 (Some(narrowed), _) => (narrowed, &query.project),
13375 (None, Some(since)) => (self.updated_since(since).await?, &query.project),
13376 // The board's own items list every draft; only the search half, which
13377 // completes them with issues they lag behind on, is narrowed — a closed
13378 // issue that half leaves out is at no category the filter keeps.
13379 (None, None) if open_only => (
13380 self.board_completed_by(Some(OPEN_QUALIFIER)).await?.items,
13381 &query.project,
13382 ),
13383 (None, None) => (self.board().await?.items, &query.project),
13384 }
13385 }
13386 };
13387 // Filtered before paged: a page of a filtered result is a page of the survivors,
13388 // never the survivors of a page.
13389 let mut tasks = Vec::new();
13390 for item in held
13391 .iter()
13392 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
13393 {
13394 let task = item.task()?;
13395 if task_matches(&task, query, membership)
13396 && self.commented_since(item, query.commented_since).await?
13397 {
13398 tasks.push(task);
13399 }
13400 }
13401 Ok(offset_page(
13402 tasks,
13403 numeric_cursor(page.cursor.as_ref())?,
13404 page.limit.min(MAX_PAGE_SIZE) as usize,
13405 ))
13406 }
13407 async fn query_projects(
13408 &self,
13409 query: &ProjectQuery,
13410 page: &PageRequest,
13411 ) -> Result<Page<Project>, SourceError> {
13412 validate_page(page)?;
13413 refuse_unsearchable_text(query.text.as_ref())?;
13414 // The projects a board holds are found by an issue search scoped to that board,
13415 // never by walking the board's own item connection: what tells a project from a
13416 // task is the `parent` each issue carries, which costs nothing to read. A query
13417 // carrying a text asks that search for the text too, so it reads the issues that
13418 // hold it rather than every issue of the board.
13419 let held = match self.text_searched(query.text.as_ref()).await? {
13420 Some(searched) => searched,
13421 None => self.board_issues().await?,
13422 };
13423 let projects = held
13424 .iter()
13425 .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
13426 .map(Resolved::project)
13427 .filter(|project| project_matches(project, query))
13428 .collect();
13429 Ok(offset_page(
13430 projects,
13431 numeric_cursor(page.cursor.as_ref())?,
13432 page.limit.min(MAX_PAGE_SIZE) as usize,
13433 ))
13434 }
13435 async fn query_projects_by_origin(
13436 &self,
13437 origin: &str,
13438 page: &PageRequest,
13439 ) -> Result<Page<Project>, SourceError> {
13440 validate_page(page)?;
13441 let projects = self
13442 .copied_from(origin, BoardKind::Work(ItemKind::Project))
13443 .await?
13444 .iter()
13445 .map(Resolved::project)
13446 .collect();
13447 Ok(offset_page(
13448 projects,
13449 numeric_cursor(page.cursor.as_ref())?,
13450 page.limit.min(MAX_PAGE_SIZE) as usize,
13451 ))
13452 }
13453 async fn get_document(&self, id: &NativeId) -> Result<Option<Document>, SourceError> {
13454 let Some(item) = self
13455 .document_by_id(id)
13456 .await?
13457 .filter(|item| item.kind == BoardKind::Document)
13458 else {
13459 return Ok(None);
13460 };
13461 self.expected.read_document(&item);
13462 Ok(Some(item.document()))
13463 }
13464 async fn query_documents_by_origin(
13465 &self,
13466 origin: &str,
13467 page: &PageRequest,
13468 ) -> Result<Page<Document>, SourceError> {
13469 validate_page(page)?;
13470 let documents = self
13471 .copied_from(origin, BoardKind::Document)
13472 .await?
13473 .iter()
13474 .map(Resolved::document)
13475 .collect();
13476 Ok(offset_page(
13477 documents,
13478 numeric_cursor(page.cursor.as_ref())?,
13479 page.limit.min(MAX_PAGE_SIZE) as usize,
13480 ))
13481 }
13482 async fn query_documents(
13483 &self,
13484 query: &DocumentQuery,
13485 page: &PageRequest,
13486 ) -> Result<Page<Document>, SourceError> {
13487 validate_page(page)?;
13488 // Narrowed to one project, this is the same sub-issue read a task list scoped to
13489 // that project makes — a document filed under a project is a sub-issue of it too,
13490 // and which of them come back is the kind this caller asked for. Unscoped, a query
13491 // carrying a text asks the board-scoped issue search for it, as a task query does,
13492 // and only one carrying none reads the board.
13493 let (held, membership) = match &query.project {
13494 ProjectFilter::Is(project) => (
13495 self.project_children(project).await?,
13496 // Answered by where these items came from; see `task_matches`.
13497 &ProjectFilter::Any,
13498 ),
13499 ProjectFilter::Any | ProjectFilter::Orphans => {
13500 refuse_unsearchable_text(query.text.as_ref())?;
13501 match self.text_searched(query.text.as_ref()).await? {
13502 Some(searched) => (searched, &query.project),
13503 None => (self.board().await?.items, &query.project),
13504 }
13505 }
13506 };
13507 // Filtered before paged, exactly as a task read is: a page of a filtered result is
13508 // a page of the survivors, never the survivors of a page.
13509 let documents = held
13510 .iter()
13511 .filter(|item| item.kind == BoardKind::Document)
13512 .map(Resolved::document)
13513 .filter(|document| document_matches(document, query, membership))
13514 .collect();
13515 Ok(offset_page(
13516 documents,
13517 numeric_cursor(page.cursor.as_ref())?,
13518 page.limit.min(MAX_PAGE_SIZE) as usize,
13519 ))
13520 }
13521 async fn labels(&self, page: &PageRequest) -> Result<Page<Label>, SourceError> {
13522 validate_page(page)?;
13523 let offset = numeric_cursor(page.cursor.as_ref())?;
13524 let mut labels = self
13525 .board()
13526 .await?
13527 .items
13528 .into_iter()
13529 .flat_map(|item| item.labels)
13530 .fold(Vec::new(), |mut all, label| {
13531 if !all.iter().any(|x: &Label| x.id == label.id) {
13532 all.push(label);
13533 }
13534 all
13535 });
13536 labels.sort_by(|a, b| a.name.cmp(&b.name).then(a.id.0.cmp(&b.id.0)));
13537 Ok(offset_page(
13538 labels,
13539 offset,
13540 page.limit.min(MAX_PAGE_SIZE) as usize,
13541 ))
13542 }
13543 async fn task_dependencies(
13544 &self,
13545 id: &NativeId,
13546 direction: Direction,
13547 page: &PageRequest,
13548 ) -> Result<Page<DependencyEdge>, SourceError> {
13549 self.dependencies(id, ItemKind::Task, direction, page).await
13550 }
13551 async fn project_dependencies(
13552 &self,
13553 id: &NativeId,
13554 direction: Direction,
13555 page: &PageRequest,
13556 ) -> Result<Page<DependencyEdge>, SourceError> {
13557 self.dependencies(id, ItemKind::Project, direction, page)
13558 .await
13559 }
13560
13561 fn writes(&self) -> WriteSupport {
13562 WriteSupport::Supported
13563 }
13564
13565 /// Create or update one task.
13566 ///
13567 /// Its `delivers` and `delivered_by` are checked before anything is read or written —
13568 /// neither may name the task itself or name one task twice — and land in the body's
13569 /// metadata slot under their reserved keys, in place of any caller metadata of those
13570 /// names.
13571 async fn write_task(&self, write: &ItemWrite<Task>) -> Result<NativeId, SourceError> {
13572 self.write_task_assets(write, None)
13573 .await
13574 .map(|written| written.id)
13575 }
13576
13577 async fn read_ahead_origins_for_copy(&self, origins: &[String]) -> Result<(), SourceError> {
13578 self.origins_ahead(origins).await
13579 }
13580
13581 async fn member_classifications(
13582 &self,
13583 project: &NativeId,
13584 ) -> Result<Option<Vec<(Classification, Vec<Repository>)>>, SourceError> {
13585 Ok(self
13586 .member_classes
13587 .lock()
13588 .map_err(|_| SourceError::Unavailable {
13589 message: "a project's members held for its classification were left \
13590 inconsistent; run the command again"
13591 .into(),
13592 })?
13593 .remove(project))
13594 }
13595
13596 /// The issue's own web address, as the `createIssue` that made it in this command answered
13597 /// it: a task, a project and a document are each an issue here.
13598 fn written_location(
13599 &self,
13600 _record: onetaskgraph_plugin_api::MetadataRecord,
13601 id: &NativeId,
13602 ) -> Option<Location> {
13603 self.written
13604 .lock()
13605 .unwrap_or_else(std::sync::PoisonError::into_inner)
13606 .get(id)
13607 .cloned()
13608 .map(Location::Url)
13609 }
13610
13611 /// [`MUTATION_BATCH`], while writes are held back: [`graphql::CREATE_ISSUES`] creates that many
13612 /// in one request, and a copy's tasks are created together only then, since their fields
13613 /// and edges are what is held back.
13614 fn create_batch_limit(&self) -> usize {
13615 if self.deferring().unwrap_or(false) {
13616 MUTATION_BATCH
13617 } else {
13618 1
13619 }
13620 }
13621
13622 // 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.
13623 async fn write_tasks(&self, writes: &[ItemWrite<Task>]) -> Result<Vec<NativeId>, SourceError> {
13624 if let Some(write) = writes.iter().find(|write| write.target.is_some()) {
13625 return Err(SourceError::Refused {
13626 message: format!(
13627 "write_tasks creates tasks, and the write of {} names the existing task {}; \
13628 next: update it with write_task",
13629 write.item.id,
13630 write.target.as_ref().map_or("", |target| target.0.as_str())
13631 ),
13632 });
13633 }
13634 if !self.deferring()? {
13635 let mut created = Vec::with_capacity(writes.len());
13636 for write in writes {
13637 match self.write_task(write).await {
13638 Ok(id) => created.push(id),
13639 Err(error) => {
13640 let mut left: Vec<&str> = Vec::new();
13641 for id in &created {
13642 if self.delete_issue(id).await.is_err() {
13643 left.push(id.0.as_str());
13644 }
13645 }
13646 return Err(left_behind(error, &left));
13647 }
13648 }
13649 }
13650 return Ok(created);
13651 }
13652 self.create_together(writes).await
13653 }
13654
13655 fn begin_deferred_writes(&self) {
13656 if let Ok(mut deferred) = self.deferred.lock() {
13657 deferred.get_or_insert_with(Deferred::default);
13658 }
13659 }
13660
13661 async fn finish_writes(&self) -> Result<(), SourceError> {
13662 self.send_held_updates().await?;
13663 self.send_held_back().await
13664 }
13665
13666 async fn write_task_with_assets(
13667 &self,
13668 write: &ItemWrite<Task>,
13669 _answers: Option<&BTreeMap<String, Value>>,
13670 assets: &onetaskgraph_plugin_api::AssetWrite,
13671 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
13672 self.write_task_assets(write, Some(assets)).await
13673 }
13674
13675 async fn write_project(&self, write: &ItemWrite<Project>) -> Result<NativeId, SourceError> {
13676 self.write_item(
13677 &Incoming {
13678 written: Written::Work(ItemKind::Project, &write.item.status),
13679 title: &write.item.title,
13680 content: write.item.content.as_deref(),
13681 assets: None,
13682 labels: &write.item.labels,
13683 metadata: &write.item.metadata,
13684 repositories: &write.item.repositories,
13685 classification: write.item.classification,
13686 parent: None,
13687 delivers: &[],
13688 delivered_by: &[],
13689 priority: None,
13690 },
13691 write.target.as_ref(),
13692 &write.depends_on,
13693 )
13694 .await
13695 .map(|written| written.id)
13696 }
13697
13698 /// Create or update one document, which is one issue titled the way this board spells
13699 /// a document.
13700 ///
13701 /// Everything else is exactly a task write: caller metadata goes to the same canonical
13702 /// JSON slot at the end of the body and comes back with its JSON types intact, a key
13703 /// or a field this board cannot carry is refused by name rather than dropped, a target
13704 /// naming an issue this board does not hold is refused rather than created, and an
13705 /// issue this call created is taken back when the rest of the write fails.
13706 async fn write_document(&self, write: &ItemWrite<Document>) -> Result<NativeId, SourceError> {
13707 self.write_document_assets(write, None)
13708 .await
13709 .map(|written| written.id)
13710 }
13711
13712 async fn write_document_with_assets(
13713 &self,
13714 write: &ItemWrite<Document>,
13715 _answers: Option<&BTreeMap<String, Value>>,
13716 assets: &onetaskgraph_plugin_api::AssetWrite,
13717 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
13718 self.write_document_assets(write, Some(assets)).await
13719 }
13720
13721 /// Refused exactly as the write refuses it, from what the write reads: the mapping first,
13722 /// which reads nothing; then the board's `Status` option. Over an existing item that is
13723 /// read off the item, as the write reads it, and the item is held among this command's
13724 /// resolved records so the write that follows reuses that read rather than repeating it;
13725 /// an item that does not carry the field takes the board's fields, which are held once
13726 /// read. A create is checked against the board's fields only when this command already
13727 /// holds them, because a create reads them together with its repository, in one request,
13728 /// and refuses a missing option before it writes anything.
13729 async fn check_status_write(
13730 &self,
13731 kind: ItemKind,
13732 category: StatusCategory,
13733 target: Option<&NativeId>,
13734 ) -> Result<(), SourceError> {
13735 let status = self.resolved_target(kind, category)?;
13736 if status.option().is_none() {
13737 return Ok(());
13738 }
13739 let fields = match target {
13740 Some(target) => {
13741 // A target this board does not hold is the write's own refusal to make.
13742 let Some(item) = self.bound_item(target).await? else {
13743 return Ok(());
13744 };
13745 self.resolved_cache()?.insert(target.clone(), item.clone());
13746 // Only the `Status` field decides whether the write can land.
13747 self.fields_for(Some(&item), false, true, false)
13748 .await?
13749 .fields
13750 }
13751 None => {
13752 let held = self
13753 .board_cache()?
13754 .as_ref()
13755 .map(|board| board.fields.clone());
13756 match held.or_else(|| {
13757 self.fields_cache()
13758 .ok()
13759 .and_then(|cache| cache.as_ref().map(|board| board.fields.clone()))
13760 }) {
13761 Some(fields) => fields,
13762 None => return Ok(()),
13763 }
13764 }
13765 };
13766 self.column_for(&fields, kind, category, &status)
13767 .map(|_| ())
13768 }
13769
13770 /// What a write of `status` reads back as: the `Status` option its category maps to for
13771 /// `kind`, and for a closing category the closure that option and its fixed reason read
13772 /// as — exactly what [`BoardStatuses::status`] reports of the item once written. A category
13773 /// this instance has no option for is answered as given, since writing it is refused.
13774 fn status_as_written(&self, kind: ItemKind, status: &Status) -> Status {
13775 match self.statuses.target(kind, status.category) {
13776 StatusTarget::Column(name) => {
13777 self.statuses.status(kind, Some(name.as_str()), false, None)
13778 }
13779 StatusTarget::Terminal(name, reason) => {
13780 self.statuses
13781 .status(kind, Some(name.as_str()), true, Some(reason.reason()))
13782 }
13783 StatusTarget::Disabled(_) => status.clone(),
13784 }
13785 }
13786
13787 /// Set one task's status alone.
13788 ///
13789 /// An open target reopens a closed issue with an `updateIssue` carrying only its
13790 /// `stateInput`, then selects the board option with `updateProjectV2ItemFieldValue`; a
13791 /// terminal target selects its mapped option, then closes with its fixed reason. No
13792 /// request carries a title, a body or a label. The status
13793 /// answered is what [`BoardStatuses::status`] reads off the state just written, which is
13794 /// what a re-read reports.
13795 async fn set_task_status(
13796 &self,
13797 id: &NativeId,
13798 category: StatusCategory,
13799 ) -> Result<Option<Status>, SourceError> {
13800 self.set_status(id, ItemKind::Task, category).await
13801 }
13802
13803 /// Set one task's status and answer the task with it, reading the item once: the read the
13804 /// task is answered from is handed to the status write, which would otherwise read the
13805 /// same issue again — two requests on a fresh process, the read and the field write,
13806 /// where the default's `get_task` before `set_task_status` sent three.
13807 async fn set_task_status_reading(
13808 &self,
13809 id: &NativeId,
13810 category: StatusCategory,
13811 ) -> Result<Option<Task>, SourceError> {
13812 // Refused before anything is read, in the words the write itself refuses it.
13813 self.resolved_target(ItemKind::Task, category)?;
13814 let Some(item) = self
13815 .bound_item(id)
13816 .await?
13817 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
13818 else {
13819 return Ok(None);
13820 };
13821 let mut task = item.task()?;
13822 self.held_cache
13823 .lock()
13824 .map_err(|_| SourceError::Unavailable {
13825 message: "items read by issue reference were left inconsistent; run the \
13826 command again"
13827 .into(),
13828 })?
13829 .insert(id.clone(), item);
13830 let Some(status) = self.set_status(id, ItemKind::Task, category).await? else {
13831 return Ok(None);
13832 };
13833 task.status = status;
13834 Ok(Some(task))
13835 }
13836
13837 /// Set one project's status alone, by exactly the paired write a task's status takes, for
13838 /// the project kind: a terminal category selects the project kind's mapped option and then
13839 /// closes the issue with the category's fixed reason — an issue already closed with a
13840 /// reason that reads that category is left as it is — and an open category reopens a closed
13841 /// issue and then selects its option. A board draft is refused a terminal category as a
13842 /// task's is.
13843 async fn set_project_status(
13844 &self,
13845 id: &NativeId,
13846 category: StatusCategory,
13847 ) -> Result<Option<Status>, SourceError> {
13848 self.set_status(id, ItemKind::Project, category).await
13849 }
13850
13851 /// Every task and project whose card contradicts its issue's closure, read through the
13852 /// board's own `is:closed` filter; see [`TaskSource::status_contradictions`].
13853 async fn status_contradictions(&self) -> Result<Vec<StatusContradiction>, SourceError> {
13854 self.contradictions().await
13855 }
13856
13857 /// What this board holds for each issue reference, one request per fifty of them; see
13858 /// [`TaskSource::issue_holdings`].
13859 async fn issue_holdings(&self, issues: &[IssueRef]) -> Result<Vec<IssueHolding>, SourceError> {
13860 self.holdings(issues).await
13861 }
13862
13863 /// Set one task's priority alone: one `updateProjectV2ItemFieldValue` selecting the
13864 /// mapped option of the board's `Priority` field, or one `clearProjectV2ItemFieldValue`
13865 /// for `none`. Refused by an instance with no `priority_mapping`.
13866 async fn set_task_priority(
13867 &self,
13868 id: &NativeId,
13869 priority: Priority,
13870 ) -> Result<Option<Priority>, SourceError> {
13871 self.set_priority(id, priority).await
13872 }
13873
13874 /// Replace one task's content with a single body update that keeps the metadata slot
13875 /// byte for byte.
13876 async fn set_task_content(
13877 &self,
13878 id: &NativeId,
13879 content: &str,
13880 ) -> Result<Option<()>, SourceError> {
13881 self.replace_content(id, content).await
13882 }
13883
13884 /// Replace one task issue's content and its provenance slot entry with a single body
13885 /// update. The answers are not kept: see `replace_rendering`.
13886 async fn set_task_rendering(
13887 &self,
13888 id: &NativeId,
13889 content: &str,
13890 provenance: &Value,
13891 _answers: &BTreeMap<String, Value>,
13892 ) -> Result<Option<()>, SourceError> {
13893 self.replace_rendering(
13894 id,
13895 BoardKind::Work(ItemKind::Task),
13896 content,
13897 provenance,
13898 None,
13899 )
13900 .await
13901 .map(|written| written.map(|_| ()))
13902 }
13903
13904 /// Replace one design-document issue's content and its provenance slot entry, on exactly
13905 /// the terms of [`set_task_rendering`](TaskSource::set_task_rendering).
13906 async fn set_document_rendering(
13907 &self,
13908 id: &NativeId,
13909 content: &str,
13910 provenance: &Value,
13911 _answers: &BTreeMap<String, Value>,
13912 ) -> Result<Option<()>, SourceError> {
13913 self.replace_rendering(id, BoardKind::Document, content, provenance, None)
13914 .await
13915 .map(|written| written.map(|_| ()))
13916 }
13917
13918 /// Replace one project issue's content and its provenance slot entry, on exactly the
13919 /// terms of [`set_task_rendering`](TaskSource::set_task_rendering).
13920 async fn set_project_rendering(
13921 &self,
13922 id: &NativeId,
13923 content: &str,
13924 provenance: &Value,
13925 _answers: &BTreeMap<String, Value>,
13926 ) -> Result<Option<()>, SourceError> {
13927 self.replace_rendering(
13928 id,
13929 BoardKind::Work(ItemKind::Project),
13930 content,
13931 provenance,
13932 None,
13933 )
13934 .await
13935 .map(|written| written.map(|_| ()))
13936 }
13937
13938 /// Apply a targeted update with one read of the item and a write only for what differs:
13939 /// the `Status` and `Priority` field writes in one request, the `blockedBy` difference,
13940 /// and last one `updateIssue` for title, body and state. See `targeted_update`.
13941 async fn update_task(
13942 &self,
13943 id: &NativeId,
13944 update: &TaskUpdate,
13945 ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
13946 self.targeted_update(id, update).await
13947 }
13948
13949 /// Replace one task's `delivered_by` with a single body update that changes the
13950 /// metadata slot and nothing outside it.
13951 async fn set_delivered_by(
13952 &self,
13953 id: &NativeId,
13954 delivered_by: &[TaskRef],
13955 ) -> Result<Option<()>, SourceError> {
13956 self.replace_delivered_by(id, delivered_by).await
13957 }
13958
13959 /// Set one key of one task issue's metadata with a single body update that changes the
13960 /// metadata slot and nothing outside it — no title, label, state or board field request —
13961 /// and sends nothing when the task already holds that value under the key.
13962 async fn set_task_metadata(
13963 &self,
13964 id: &NativeId,
13965 key: &MetadataKey,
13966 value: &Value,
13967 ) -> Result<Option<Task>, SourceError> {
13968 Ok(self
13969 .set_slot_key(id, BoardKind::Work(ItemKind::Task), key, value)
13970 .await?
13971 .map(|item| item.task())
13972 .transpose()?)
13973 }
13974
13975 /// Set one key of one project issue's metadata, on exactly the terms of
13976 /// [`set_task_metadata`](TaskSource::set_task_metadata).
13977 async fn set_project_metadata(
13978 &self,
13979 id: &NativeId,
13980 key: &MetadataKey,
13981 value: &Value,
13982 ) -> Result<Option<Project>, SourceError> {
13983 Ok(self
13984 .set_slot_key(id, BoardKind::Work(ItemKind::Project), key, value)
13985 .await?
13986 .map(|item| item.project()))
13987 }
13988
13989 /// Set one key of one design-document issue's metadata, on exactly the terms of
13990 /// [`set_task_metadata`](TaskSource::set_task_metadata).
13991 async fn set_document_metadata(
13992 &self,
13993 id: &NativeId,
13994 key: &MetadataKey,
13995 value: &Value,
13996 ) -> Result<Option<Document>, SourceError> {
13997 Ok(self
13998 .set_slot_key(id, BoardKind::Document, key, value)
13999 .await?
14000 .map(|item| item.document()))
14001 }
14002
14003 async fn delete_task(&self, id: &NativeId) -> Result<(), SourceError> {
14004 self.delete_item(id).await
14005 }
14006
14007 async fn delete_project(&self, id: &NativeId) -> Result<(), SourceError> {
14008 self.delete_item(id).await
14009 }
14010
14011 async fn delete_document(&self, id: &NativeId) -> Result<(), SourceError> {
14012 self.delete_item(id).await
14013 }
14014
14015 /// One page of the task issue's own comments, walked by GitHub's own cursor.
14016 ///
14017 /// Nothing here filters, so nothing has to be read ahead of the page: the caller's limit is
14018 /// the page GitHub is asked for and GitHub's `endCursor` is the cursor handed back.
14019 ///
14020 /// One request, [`graphql::ISSUE_DETAIL`]: the read that says the id names a task of this
14021 /// board is the read of its comments. A draft this process already resolved is refused
14022 /// without one.
14023 async fn task_comments(
14024 &self,
14025 task: &NativeId,
14026 page: &PageRequest,
14027 ) -> Result<Option<Page<Comment>>, SourceError> {
14028 validate_page(page)?;
14029 let cached = self.resolved_cache()?.get(task).cloned();
14030 if let Some(item) = cached {
14031 if item.kind != BoardKind::Work(ItemKind::Task) {
14032 return Ok(None);
14033 }
14034 if item.content_kind == ContentKind::DraftIssue {
14035 return Err(self.draft_has_no_comments(task));
14036 }
14037 }
14038 match self.issue_detail(task, page).await? {
14039 Some(TaskDetailRead {
14040 comments: Some(comments),
14041 ..
14042 }) => comments,
14043 _ => Ok(None),
14044 }
14045 }
14046
14047 /// Every id's task, with the first page of its comments when `comments` names it:
14048 /// [`DETAIL_BATCH`] items per [`graphql::ISSUE_DETAILS`] request, and one item with its
14049 /// comments in one [`graphql::ISSUE_DETAIL`] request.
14050 async fn get_task_details(
14051 &self,
14052 ids: &[NativeId],
14053 comments: Option<&PageRequest>,
14054 ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
14055 if let Some(page) = comments
14056 && let Err(error) = validate_page(page)
14057 {
14058 return ids.iter().map(|_| Err(error.clone())).collect();
14059 }
14060 match (ids, comments) {
14061 ([id], Some(page)) => vec![self.issue_detail(id, page).await],
14062 ([id], None) => vec![self.task_read(id).await],
14063 _ => self.issue_details(ids, comments).await,
14064 }
14065 }
14066
14067 /// Every id's task with the first page of its comments and its `blockedBy`, as a copy reads
14068 /// the tasks it carries: [`COPY_DETAIL_BATCH`] items per [`graphql::ISSUE_COPY_DETAILS`]
14069 /// request, so the dependency read a copy makes of each next is answered from this one.
14070 async fn get_task_details_for_copy(
14071 &self,
14072 ids: &[NativeId],
14073 comments: &PageRequest,
14074 ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
14075 // A page past the first is a page of one task's comments, which the detail read of it
14076 // alone answers.
14077 if comments.cursor.is_some() {
14078 return self.get_task_details(ids, Some(comments)).await;
14079 }
14080 if let Err(error) = validate_page(comments) {
14081 return ids.iter().map(|_| Err(error.clone())).collect();
14082 }
14083 let mut read = Vec::with_capacity(ids.len());
14084 for batch in ids.chunks(COPY_DETAIL_BATCH) {
14085 match self
14086 .graphql(
14087 graphql::ISSUE_COPY_DETAILS,
14088 copy_detail_batch(batch, comments, false),
14089 )
14090 .await
14091 {
14092 Ok(data) => {
14093 for (slot, id) in batch.iter().enumerate() {
14094 let read_one = match data.get(format!("i{slot}")) {
14095 Some(node) => self.detail_of(id, node, true, None).await,
14096 None => Err(SourceError::Malformed {
14097 message: format!(
14098 "GitHub answered a batch read with no item for {}",
14099 id.0
14100 ),
14101 }),
14102 };
14103 read.push(read_one);
14104 }
14105 }
14106 Err(error) if unresolvable_node(&error) => {
14107 for id in batch {
14108 read.push(self.issue_detail(id, comments).await);
14109 }
14110 }
14111 Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
14112 }
14113 }
14114 read
14115 }
14116
14117 /// Every id's item, whatever kind it is, read [`COPY_DETAIL_BATCH`] at a time with
14118 /// [`graphql::ISSUE_COPY_DETAILS`] — the document a copy reads the tasks it carries with —
14119 /// and held for the next read of each by id; see `read_ahead`.
14120 ///
14121 /// What this run wrote is never read: its own record answers, as it does for a read by id.
14122 /// Nor is an item a listing of its project carried whole earlier in this command — see
14123 /// `carried` — which is held in place of reading it.
14124 /// A batch GitHub refuses because one of its ids resolves to no node at all holds nothing,
14125 /// so each of its ids is read on its own and answered as that read answers; a draft, and an
14126 /// answer naming another issue than the one asked for, are left to their own reads too.
14127 async fn read_ahead_for_copy(&self, ids: &[NativeId]) -> Result<(), SourceError> {
14128 // The origins asked just before go out now, beside the members of the one project this
14129 // names when it names one; a project those origins answered whole is held for its next
14130 // read by id rather than read again here.
14131 let hinted = match ids {
14132 [one] => Some(one),
14133 _ => None,
14134 };
14135 self.send_pending_origins(hinted).await?;
14136 if let Some(project) = hinted {
14137 let answered = self.resolved_cache()?.get(project).cloned();
14138 if let Some(answered) = answered
14139 && answered.kind == BoardKind::Work(ItemKind::Project)
14140 {
14141 self.read_ahead()?.insert(project.clone(), Some(answered));
14142 return Ok(());
14143 }
14144 }
14145 let mut unread: Vec<NativeId> = Vec::new();
14146 // Read whole by a listing of its project in this command, which a mutation naming it
14147 // would have dropped: held as a batch would have held it, once every batch is answered.
14148 let mut listed: Vec<NativeId> = Vec::new();
14149 {
14150 let created = self.created()?;
14151 let held = self.read_ahead()?;
14152 let carried = self.carried()?;
14153 for id in ids {
14154 if unread.contains(id)
14155 || listed.contains(id)
14156 || held.contains_key(id)
14157 || created.iter().any(|own| own.id == *id)
14158 {
14159 continue;
14160 }
14161 // An issue a listing carried without the whole of what blocks it is read in a
14162 // batch too, which carries that, rather than having its dependency read sent
14163 // on its own once the copy reconciles its edges.
14164 let whole = carried.get(id).is_some_and(|item| {
14165 item.content_kind != ContentKind::Issue || item.blocked_by.is_some()
14166 });
14167 if whole {
14168 listed.push(id.clone());
14169 } else {
14170 unread.push(id.clone());
14171 }
14172 }
14173 }
14174 // Nothing of an item's comments is wanted here: the smallest page keeps the read to
14175 // what the item itself costs.
14176 let comments = PageRequest {
14177 cursor: None,
14178 limit: 1,
14179 };
14180 // Held only once every batch has been answered, so a read-ahead that fails part way
14181 // holds nothing — what a listing carried included, which stays carried — and every
14182 // item is read on its own, as the method promises.
14183 let mut read: Vec<(NativeId, Option<Resolved>)> = Vec::new();
14184 // The issues the first id names as sub-issues, as `ISSUE_COPY_DETAILS`'s `children`
14185 // answered them: what a project copy's orphan check asks, when that first id is its
14186 // project.
14187 let mut children: Option<(NativeId, Vec<(String, String)>)> = None;
14188 for (index, batch) in unread.chunks(COPY_DETAIL_BATCH).enumerate() {
14189 let data = match self
14190 .graphql(
14191 graphql::ISSUE_COPY_DETAILS,
14192 copy_detail_batch(batch, &comments, index == 0),
14193 )
14194 .await
14195 {
14196 Ok(data) => data,
14197 Err(error) if unresolvable_node(&error) => continue,
14198 Err(error) => return Err(error),
14199 };
14200 for (slot, id) in batch.iter().enumerate() {
14201 let Some(node) = data.get(format!("i{slot}")) else {
14202 continue;
14203 };
14204 let held = if node.is_null() {
14205 None
14206 } else {
14207 match optional_str(node, "__typename")? {
14208 Some("DraftIssue") => continue,
14209 Some("Issue") if required_str(node, "id")? != id.0 => continue,
14210 _ => {}
14211 }
14212 // Remembered below with the rest, so a later batch refused leaves no
14213 // dependency read answered from this one either.
14214 self.resolve_issue_as(node, false).await?
14215 };
14216 read.push((id.clone(), held));
14217 }
14218 if index == 0
14219 && let Some(node) = data.get("children").filter(|node| !node.is_null())
14220 && node.get("id").and_then(Value::as_str) == batch.first().map(|id| id.0.as_str())
14221 && node.pointer("/subIssues/pageInfo/hasNextPage") == Some(&Value::Bool(false))
14222 {
14223 // A list GitHub did not answer is not an empty one: it would answer the project's
14224 // task listing with nothing, and the orphan check with no orphans.
14225 let listed = node
14226 .pointer("/subIssues/nodes")
14227 .and_then(Value::as_array)
14228 .ok_or_else(|| SourceError::Malformed {
14229 message: format!(
14230 "GitHub answered the sub-issues of project {} with no list of them",
14231 batch[0].0
14232 ),
14233 })?
14234 .iter()
14235 .map(|child| {
14236 Ok((
14237 required_str(child, "id")?.to_owned(),
14238 required_str(child, "title")?.to_owned(),
14239 ))
14240 })
14241 .collect::<Result<Vec<_>, SourceError>>()?;
14242 children = Some((batch[0].clone(), listed));
14243 }
14244 }
14245 // The project's tasks are known from this read alone when every child it named is held
14246 // from it, but for a document, which its title says is one and which a task listing
14247 // never answers: then a listing of the project's tasks is answered from them.
14248 if let Some((project, listed)) = children
14249 && read.iter().any(|(id, held)| {
14250 *id == project
14251 && held
14252 .as_ref()
14253 .is_some_and(|held| held.kind == BoardKind::Work(ItemKind::Project))
14254 })
14255 {
14256 let mut tasks = Vec::new();
14257 let mut whole = true;
14258 for (child, title) in &listed {
14259 match read.iter().find(|(id, _)| id.0 == *child) {
14260 Some((_, Some(held))) if held.parent.as_ref() == Some(&project) => {
14261 if held.kind == BoardKind::Work(ItemKind::Task) {
14262 tasks.push(held.clone());
14263 }
14264 }
14265 _ if title.starts_with(DESIGN_TITLE_PREFIX) => {}
14266 _ => whole = false,
14267 }
14268 }
14269 if whole {
14270 self.task_children()?.insert(project, tasks);
14271 }
14272 }
14273 let mut resolved = self.resolved_cache()?;
14274 let mut ahead = self.read_ahead()?;
14275 let mut carried = self.carried()?;
14276 for item in read.iter().filter_map(|(_, held)| held.as_ref()) {
14277 resolved.insert(item.id.clone(), item.clone());
14278 }
14279 // Taken only if still carried: a mutation naming it while the batches ran dropped it,
14280 // and its next read is then its own.
14281 for id in &listed {
14282 let Some(item) = carried.remove(id) else {
14283 continue;
14284 };
14285 // The whole of what it is blocked by came with it, so the dependency read a copy
14286 // makes next is answered from this one, as a copy's detail read is.
14287 if item.blocked_by.is_some() {
14288 resolved.insert(item.id.clone(), item.clone());
14289 }
14290 read.push((id.clone(), Some(item)));
14291 }
14292 for (id, held) in read {
14293 ahead.insert(id, held);
14294 }
14295 Ok(())
14296 }
14297
14298 /// Add one comment to the task's issue, as the account the token belongs to.
14299 ///
14300 /// The author is refused before anything is sent — not even the task is read — because
14301 /// no answer GitHub could give would make posting under another name than the one asked
14302 /// for the right outcome.
14303 async fn add_comment(
14304 &self,
14305 task: &NativeId,
14306 comment: &NewComment,
14307 ) -> Result<Option<Comment>, SourceError> {
14308 if let Some(author) = &comment.author {
14309 return Err(SourceError::Refused {
14310 message: format!(
14311 "source {} cannot post a comment as {author:?}: GitHub records the account \
14312 the token signs in as the author of every comment; next: leave --author \
14313 out, and the comment is posted as that account",
14314 self.name
14315 ),
14316 });
14317 }
14318 let Some(issue) = self.commented_issue(task).await? else {
14319 return Ok(None);
14320 };
14321 let data = self
14322 .graphql(
14323 graphql::ADD_COMMENT,
14324 json!({"input":{"subjectId":issue.0,"body":comment.body.as_str()}}),
14325 )
14326 .await?;
14327 let subject = data
14328 .pointer("/addComment/subject")
14329 .filter(|value| !value.is_null())
14330 .ok_or_else(|| SourceError::Malformed {
14331 message: "GitHub comment addition returned no subject".into(),
14332 })?;
14333 if required_str(subject, "id")? != issue.0 {
14334 return Err(SourceError::Malformed {
14335 message: "GitHub comment addition answered about another issue".into(),
14336 });
14337 }
14338 let added = data
14339 .pointer("/addComment/commentEdge/node")
14340 .filter(|value| !value.is_null())
14341 .ok_or_else(|| SourceError::Malformed {
14342 message: "GitHub comment addition returned no comment".into(),
14343 })?;
14344 let added = comment_from(added)?;
14345 self.remember_commented(&issue)?;
14346 Ok(Some(added))
14347 }
14348
14349 async fn edit_comment(
14350 &self,
14351 task: &NativeId,
14352 comment: &NativeId,
14353 body: &CommentBody,
14354 ) -> Result<Option<Comment>, SourceError> {
14355 let Some(issue) = self.commented_issue(task).await? else {
14356 return Ok(None);
14357 };
14358 let Some(held) = self.comment_on(&issue, comment).await? else {
14359 return Ok(None);
14360 };
14361 // A comment a copy wrote keeps its origin through an edit: its marker is carried over.
14362 let body = CommentOrigin::kept(&held, body)?;
14363 let data = self
14364 .graphql(
14365 graphql::UPDATE_COMMENT,
14366 json!({"input":{"id":comment.0,"body":body.as_str()}}),
14367 )
14368 .await?;
14369 let edited = data
14370 .pointer("/updateIssueComment/issueComment")
14371 .filter(|value| !value.is_null())
14372 .ok_or_else(|| SourceError::Malformed {
14373 message: "GitHub comment update returned no comment".into(),
14374 })?;
14375 let edited = comment_from(edited)?;
14376 if edited.id != *comment {
14377 return Err(SourceError::Malformed {
14378 message: "GitHub comment update returned the wrong comment".into(),
14379 });
14380 }
14381 self.remember_commented(&issue)?;
14382 Ok(Some(edited))
14383 }
14384
14385 async fn delete_comment(
14386 &self,
14387 task: &NativeId,
14388 comment: &NativeId,
14389 ) -> Result<Option<NativeId>, SourceError> {
14390 let Some(issue) = self.commented_issue(task).await? else {
14391 return Ok(None);
14392 };
14393 if !self.comment_is_on(&issue, comment).await? {
14394 return Ok(None);
14395 }
14396 let data = self
14397 .graphql(graphql::DELETE_COMMENT, json!({"input":{"id":comment.0}}))
14398 .await?;
14399 // The payload says nothing about the comment it removed, so what is checked is that
14400 // GitHub answered the mutation at all rather than leaving it unanswered.
14401 data.get("deleteIssueComment")
14402 .filter(|value| !value.is_null())
14403 .ok_or_else(|| SourceError::Malformed {
14404 message: "GitHub comment deletion returned no payload".into(),
14405 })?;
14406 Ok(Some(comment.clone()))
14407 }
14408
14409 /// Every request this source has recorded, and what each of GitHub's two budgets was
14410 /// attributed — read off the same accounting the session report is rendered from, so
14411 /// the two cannot count one request two ways.
14412 async fn metering(&self) -> Result<Option<Metering>, SourceError> {
14413 Ok(Some(self.ledger.snapshot().metering()))
14414 }
14415
14416 /// Drop every item, search answer and board read this source holds, so the next command
14417 /// reads the board as a person has since left it.
14418 ///
14419 /// Every one of those is held on the assumption that nothing but this source writes the
14420 /// board while a command runs, which stops being true the moment the command is over: a
14421 /// body a person edited would be overwritten from the record held here, and a card they
14422 /// moved would be read as still where this source left it. The board's own field
14423 /// definitions go too, because a person can add or delete a `Status` option and a write
14424 /// resolved against the held list would not re-read on a miss. What stays is what stays
14425 /// valid in normal use: each repository's node id, which a miss re-reads, the pacing of
14426 /// mutations, which is about GitHub's limiter rather than anybody's work, and the running
14427 /// accounting [`metering`](TaskSource::metering) answers from.
14428 ///
14429 /// Infallible in practice: a lock an earlier failure poisoned is cleared rather than
14430 /// refused, because clearing it is what puts it right.
14431 async fn end_command(&self) -> Result<(), SourceError> {
14432 fn clear<T: Default>(held: &Mutex<T>) {
14433 *held
14434 .lock()
14435 .unwrap_or_else(std::sync::PoisonError::into_inner) = T::default();
14436 held.clear_poison();
14437 }
14438 clear(&self.created);
14439 clear(&self.updated);
14440 clear(&self.commented);
14441 clear(&self.board_cache);
14442 clear(&self.search_cache);
14443 clear(&self.narrowed_cache);
14444 clear(&self.search_next);
14445 clear(&self.resolved_cache);
14446 clear(&self.read_ahead);
14447 clear(&self.pending_origins);
14448 clear(&self.member_classes);
14449 clear(&self.carried);
14450 clear(&self.held_cache);
14451 clear(&self.children_cache);
14452 clear(&self.task_children);
14453 clear(&self.written);
14454 clear(&self.fields_cache);
14455 self.expected.end_command();
14456 Ok(())
14457 }
14458}
14459
14460/// One member of a project as a classification-only read answers it: its classification and
14461/// the repositories its work changes.
14462type MemberClass = (Classification, Vec<Repository>);
14463
14464/// Each project's sub-issues as one command read them, keyed by the selector they were asked
14465/// for under, beside the project that selector named; see `children_cache`.
14466type ProjectChildren = BTreeMap<NativeId, (NativeId, Vec<Resolved>)>;
14467
14468/// One issue comment as the contract carries it.
14469///
14470/// `author` is absent both when GitHub answers `null` for an account that no longer exists
14471/// and when it answers an actor with no login, because either way the source did not say who
14472/// wrote it — which is what an absent author means, rather than an author called nothing.
14473///
14474/// A comment a copy wrote here carries its origin as the hidden marker at the end of its body
14475/// — GitHub sets every comment's author and times itself, so there is nowhere else to keep it —
14476/// and is read back with the marker taken out and reported as its origin.
14477fn comment_from(value: &Value) -> Result<Comment, SourceError> {
14478 Ok(Comment {
14479 origin: None,
14480 id: NativeId(required_str(value, "id")?.to_owned()),
14481 author: optional_str(value.get("author").unwrap_or(&Value::Null), "login")?
14482 .map(str::to_owned),
14483 created_at: optional_time(value, "createdAt")?,
14484 updated_at: optional_time(value, "updatedAt")?,
14485 body: required_str(value, "body")?.to_owned(),
14486 url: optional_str(value, "url")?.map(str::to_owned),
14487 }
14488 .unmarked())
14489}
14490
14491/// The page of comments one issue node carries, resumed from `after`.
14492fn comment_page(
14493 node: &Value,
14494 issue: &str,
14495 after: Option<&str>,
14496) -> Result<Page<Comment>, SourceError> {
14497 let connection = node
14498 .get("comments")
14499 .filter(|value| !value.is_null())
14500 .ok_or_else(|| SourceError::Malformed {
14501 message: format!("GitHub issue {issue} answered with no comments connection"),
14502 })?;
14503 let items = optional_nodes(Some(connection), "issue comments")?
14504 .into_iter()
14505 .flatten()
14506 .map(comment_from)
14507 .collect::<Result<Vec<_>, _>>()?;
14508 let next = next_cursor(connection)?;
14509 if let Some(next) = &next {
14510 validate_cursor_progress(after, &next.0)?;
14511 }
14512 Ok(Page { items, next })
14513}
14514
14515/// The far ends of an issue's whole `blockedBy` connection, when the read carried it to its
14516/// end — `None` when it carried none, or a page with more past it.
14517fn carried_blocked_by(content: &Value) -> Result<Option<Vec<Value>>, SourceError> {
14518 let Some(connection) = content.get("blockedBy").filter(|value| !value.is_null()) else {
14519 return Ok(None);
14520 };
14521 if next_cursor(connection)?.is_some() {
14522 return Ok(None);
14523 }
14524 Ok(Some(
14525 optional_nodes(Some(connection), "blocked-by issues")?
14526 .into_iter()
14527 .flatten()
14528 .cloned()
14529 .collect(),
14530 ))
14531}
14532
14533/// Where the recorded tail of a dependency walk resumes; see
14534/// [`GitHubProjectsSource::recorded_edges`].
14535const RECORDED_CURSOR: &str = "onetaskgraph.depends_on:";
14536
14537/// The board text field this source keeps a copy's origin in.
14538///
14539/// Named after the key it holds, and held to that name by the guard below rather than by
14540/// a reader noticing.
14541const ORIGIN_FIELD: &str = "onetaskgraph.origin";
14542
14543/// The metadata key that field holds.
14544///
14545/// The engine owns this key and spells it once as `GlobalId::ORIGIN_KEY`; a plugin never
14546/// constructs or interprets the qualified id it carries. This source names it only to
14547/// route it — a short, typed value belongs in a typed field rather than in the body slot
14548/// a caller's own prose shares.
14549///
14550/// Restated rather than imported, because no plugin crate may depend on the engine. What
14551/// keeps the two spellings one contract is `scripts/check-origin-key-spelling.sh`, a
14552/// target in `check`: it reads the engine's own literal and fails naming the file and the
14553/// line when a plugin's parts from it either way. Drift here has one symptom — a copy
14554/// that creates a second item every run instead of finding the one it wrote — and that is
14555/// too late to learn it.
14556const ORIGIN_KEY: &str = "onetaskgraph.origin";
14557
14558/// Where a recorded tail resumes, refusing a cursor no walk in `direction` reported.
14559///
14560/// The reserved key holds forward edges and nothing else — the reverse of a recorded edge
14561/// is derived from the far end, never written down on the near item — so only a forward
14562/// walk ever reports one of these cursors. A reverse read carrying one is resuming a walk
14563/// it did not come from, and it is told so rather than answered with an empty page that
14564/// reads as a walk which ended.
14565fn recorded_offset(
14566 cursor: Option<&str>,
14567 direction: Direction,
14568) -> Result<Option<usize>, SourceError> {
14569 cursor
14570 .and_then(|cursor| cursor.strip_prefix(RECORDED_CURSOR))
14571 .map(|offset| {
14572 if direction != Direction::DependsOn {
14573 return Err(SourceError::Config {
14574 message: format!(
14575 "{RECORDED_CURSOR}{offset} resumes recorded forward edges, which a \
14576 reverse dependency read never issues; resume it in the direction \
14577 that reported it"
14578 ),
14579 });
14580 }
14581 offset.parse().map_err(|_| SourceError::Config {
14582 message: format!("{RECORDED_CURSOR}{offset} is not a recorded-edge cursor"),
14583 })
14584 })
14585 .transpose()
14586}
14587
14588/// What a dependency read's own answer says of a board draft: `None` when the item is not a
14589/// draft, or the answer did not carry its board item; `Some(None)` for a draft whose one board
14590/// item is not on this board, which this source does not hold; and `Some(Some(slot))`, the
14591/// draft's metadata, for one it does — its board item naming this board by its number and by
14592/// the id the same answer read of the board, as [`GitHubProjectsSource::draft_by_id`] decides.
14593fn draft_slot(
14594 data: &Value,
14595 id: &NativeId,
14596 project_number: u32,
14597) -> Result<Option<Option<BTreeMap<String, Value>>>, SourceError> {
14598 let Some(draft) = data
14599 .get("node")
14600 .filter(|node| node.get("__typename").and_then(Value::as_str) == Some("DraftIssue"))
14601 else {
14602 return Ok(None);
14603 };
14604 let Some(items) = data.pointer("/draft/projectV2Items") else {
14605 return Ok(None);
14606 };
14607 if required_str(draft, "id")? != id.0 {
14608 return Err(SourceError::Malformed {
14609 message: format!("GitHub answered a different draft for {}", id.0),
14610 });
14611 }
14612 let nodes = items
14613 .get("nodes")
14614 .and_then(Value::as_array)
14615 .ok_or_else(|| SourceError::Malformed {
14616 message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
14617 })?;
14618 let info = items
14619 .get("pageInfo")
14620 .ok_or_else(|| SourceError::Malformed {
14621 message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
14622 })?;
14623 if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
14624 return Err(SourceError::Malformed {
14625 message: format!(
14626 "GitHub draft {} reports more board items than the one GitHub links a draft to",
14627 id.0
14628 ),
14629 });
14630 }
14631 let board = data
14632 .pointer("/board/projectV2/id")
14633 .and_then(Value::as_str)
14634 .ok_or_else(|| SourceError::Malformed {
14635 message: format!(
14636 "GitHub answered draft {} without this board's own id beside it",
14637 id.0
14638 ),
14639 })?;
14640 let mut held = false;
14641 for node in nodes {
14642 let project = node.get("project").ok_or_else(|| SourceError::Malformed {
14643 message: format!("GitHub draft {} board item has no project", id.0),
14644 })?;
14645 let number = project
14646 .get("number")
14647 .and_then(Value::as_u64)
14648 .ok_or_else(|| SourceError::Malformed {
14649 message: format!(
14650 "GitHub draft {} board item has no numeric project number",
14651 id.0
14652 ),
14653 })?;
14654 let project_id = required_str(project, "id")?;
14655 held |= number == u64::from(project_number) && project_id == board;
14656 }
14657 if !held {
14658 return Ok(Some(None));
14659 }
14660 Ok(Some(Some(
14661 metadata_body(optional_str(draft, "body")?.map(str::to_owned))?.1,
14662 )))
14663}
14664
14665fn recorded_page(edges: Vec<DependencyEdge>, offset: usize, limit: usize) -> Page<DependencyEdge> {
14666 let mut page = offset_page(edges, offset, limit.max(1));
14667 page.next = page
14668 .next
14669 .map(|cursor| Cursor(format!("{RECORDED_CURSOR}{}", cursor.0)));
14670 page
14671}
14672
14673/// The kind of one issue reached through a dependency connection.
14674///
14675/// The same questions the board scan asks, over the fields the dependency document
14676/// selects, and in the same order: the design prefix first, then a sub-issue is a task,
14677/// then anything with sub-issues or the marker is a project.
14678///
14679/// # Errors
14680///
14681/// A far end this board holds as a document is refused rather than reported. The two
14682/// answers that are not refusals would both be wrong: reporting it as a task names an id
14683/// no task read of this source can find, and reporting it as a project names one no
14684/// project read can. There is no third value to return — `ItemKind` has no document
14685/// variant, because nothing may point at a document — so the relationship itself is what
14686/// the person is told about.
14687fn related_kind(value: &Value) -> Result<ItemKind, SourceError> {
14688 let id = required_str(value, "id")?;
14689 if required_str(value, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
14690 return Err(SourceError::Refused {
14691 message: format!(
14692 "GitHub issue {id} is a document of this board — its title begins \
14693 {DESIGN_TITLE_PREFIX:?} — and nothing may depend on a document or be depended \
14694 on by one; next: remove that issue's blocking relationship on this board"
14695 ),
14696 });
14697 }
14698 let parent = optional_str(value.get("parent").unwrap_or(&Value::Null), "id")?;
14699 if parent.is_some() {
14700 return Ok(ItemKind::Task);
14701 }
14702 let (_, slot) = metadata_body(optional_str(value, "body")?.map(str::to_owned))?;
14703 let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
14704 message: format!("GitHub issue {id}: {message}"),
14705 })?;
14706 let sub_issues = sub_issue_total(value)?;
14707 Ok(if sub_issues > 0 || marked == Some(ItemKind::Project) {
14708 ItemKind::Project
14709 } else {
14710 ItemKind::Task
14711 })
14712}
14713
14714/// The `stateInput` that closes an issue for `reason`.
14715fn closed_input(reason: ClosedState) -> Value {
14716 json!({"value":"CLOSED","stateReason":reason.reason()})
14717}
14718
14719/// The `IssueStateUpdateInput` one status target asks for.
14720///
14721/// `stateInput` and `state` are mutually exclusive on `UpdateIssueInput`, and only this
14722/// one is ever sent. A non-terminal status always asks for `OPEN`, which is what reopens
14723/// a currently-closed issue: without that the item would read back `Unknown` and a copy
14724/// would report a change forever. A document has no status at all, and asks for neither.
14725fn state_input(target: Option<&StatusTarget>) -> Value {
14726 match target {
14727 Some(StatusTarget::Terminal(_, reason)) => closed_input(*reason),
14728 Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => json!({"value":"OPEN"}),
14729 // A document has no status, so a write of one says nothing about the issue's open
14730 // or closed state rather than forcing it open: `stateInput` is what carries that
14731 // instruction, and an explicit null asks for no change to it.
14732 None => Value::Null,
14733 }
14734}
14735
14736/// The metadata one write stores in the item's body slot.
14737///
14738/// The typed fields travel as themselves, so the three reserved keys are rebuilt here
14739/// rather than carried: the kind marker so an empty project stays readable, the
14740/// repository list only when it is not exactly the issue's own repository, and the far
14741/// ends no relationship here can name.
14742///
14743/// The copy origin is the one typed field that is also mirrored here, and only as a
14744/// mirror: it lands in the board's origin field as well, which stays the one every reader
14745/// takes it from, and it is here so that GitHub's issue search — which indexes this comment
14746/// and catches up with a write in seconds rather than minutes — can find the item by it.
14747/// A reader of the release before this one drops the slot's copy and reads the field, so an
14748/// item written here still reads with exactly one origin there.
14749fn slot_metadata(
14750 incoming: &Incoming<'_>,
14751 own_repository: Option<&Repository>,
14752 fallback: &[DependencyEdge],
14753) -> BTreeMap<String, Value> {
14754 let mut metadata = incoming.metadata.clone();
14755 match metadata.remove(ORIGIN_KEY) {
14756 Some(Value::String(origin)) if !origin.is_empty() => {
14757 metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin));
14758 }
14759 _ => {}
14760 }
14761 match incoming.written.kind() {
14762 BoardKind::Work(kind) => metadata.insert(
14763 ItemKind::METADATA_KEY.to_owned(),
14764 Value::String(kind.marker().to_owned()),
14765 ),
14766 // A document is told by its title, so it carries no kind marker: that key names
14767 // what a dependency endpoint points at, and nothing may point at a document.
14768 BoardKind::Document => metadata.remove(ItemKind::METADATA_KEY),
14769 };
14770 incoming.classification.record(&mut metadata);
14771 let derivable = own_repository
14772 .map(|own| incoming.repositories == [own.clone()])
14773 .unwrap_or(incoming.repositories.is_empty());
14774 if derivable {
14775 metadata.remove(Repository::METADATA_KEY);
14776 } else {
14777 metadata.insert(
14778 Repository::METADATA_KEY.to_owned(),
14779 Value::Array(
14780 incoming
14781 .repositories
14782 .iter()
14783 .map(|repository| Value::String(repository.as_str().to_owned()))
14784 .collect(),
14785 ),
14786 );
14787 }
14788 // The typed lists are what land, whatever the caller's own metadata held under their
14789 // keys: a key of either name travelling beside the field would otherwise be a second
14790 // answer to the same question, and the field is the one the contract names.
14791 for (key, entries) in [
14792 (TaskRef::DELIVERS_KEY, incoming.delivers),
14793 (TaskRef::DELIVERED_BY_KEY, incoming.delivered_by),
14794 ] {
14795 set_task_list(&mut metadata, key, entries);
14796 }
14797 record_edges(&mut metadata, fallback);
14798 metadata
14799}
14800
14801/// Hold the far ends no relationship here can name under [`DependencyEdge::RECORDED_KEY`] in
14802/// one slot's metadata, or no such key when there are none.
14803fn record_edges(metadata: &mut BTreeMap<String, Value>, fallback: &[DependencyEdge]) {
14804 if fallback.is_empty() {
14805 metadata.remove(DependencyEdge::RECORDED_KEY);
14806 } else {
14807 metadata.insert(
14808 DependencyEdge::RECORDED_KEY.to_owned(),
14809 Value::Array(
14810 fallback
14811 .iter()
14812 .map(|edge| {
14813 serde_json::to_value(&edge.to).expect("an endpoint renders as JSON")
14814 })
14815 .collect(),
14816 ),
14817 );
14818 }
14819}
14820
14821/// Every label one item carries, from its content's own connection and nowhere else.
14822///
14823/// There is no second place to read one from: no document this source sends selects the
14824/// board's built-in `Labels` field, because GitHub derives it from the content and a draft
14825/// cannot carry one at all. The module documentation records the three schema facts that
14826/// settle it.
14827fn labels(content: &Value) -> Result<Vec<Label>, SourceError> {
14828 optional_nodes(content.get("labels"), "content labels")?
14829 .into_iter()
14830 .flatten()
14831 .map(|v| {
14832 Ok(Label {
14833 id: NativeId(required_str(v, "id")?.to_owned()),
14834 name: required_str(v, "name")?.to_owned(),
14835 color: optional_str(v, "color")?.map(str::to_owned),
14836 })
14837 })
14838 .collect()
14839}
14840
14841/// Each board field value one item holds, as the change token's facets name it: the field's
14842/// name, with the single-select option's name or the text. A value of a type no read here
14843/// selects, and one naming no field, says nothing a person can rename and is left out.
14844fn facet_fields(field_values: &[Value]) -> Vec<(String, String)> {
14845 field_values
14846 .iter()
14847 .filter_map(|value| {
14848 let field = value.pointer("/field/name")?.as_str()?;
14849 let held = value.get("name").or_else(|| value.get("text"))?.as_str()?;
14850 Some((field.to_owned(), held.to_owned()))
14851 })
14852 .collect()
14853}
14854
14855/// The digest of the facets of the issue `content`, whose board item's values are
14856/// `field_values` — or `None` when the response did not carry, whole, every member they are read
14857/// from: the title, the parent, the sub-issue summary and the body its kind is read from, every
14858/// label, and every field value.
14859///
14860/// What a write's own response spells its token from, by exactly the rules a read takes them by.
14861fn facets_of(content: &Value, field_values: &Value) -> Option<change_token::FacetsDigest> {
14862 let ended = |connection: &Value| {
14863 connection.pointer("/pageInfo/hasNextPage") == Some(&Value::Bool(false))
14864 };
14865 let labels = content.get("labels").filter(|labels| ended(labels))?;
14866 if !ended(field_values) {
14867 return None;
14868 }
14869 let parent = content.get("parent")?;
14870 let (_, slot) = metadata_body(content.get("body")?.as_str().map(str::to_owned)).ok()?;
14871 let kind = BoardKind::of(
14872 content.get("title")?.as_str()?,
14873 !parent.is_null(),
14874 sub_issue_total(content).ok()?,
14875 ItemKind::from_metadata(&slot).ok()?,
14876 );
14877 let labels = self::labels(&json!({"labels": labels})).ok()?;
14878 let nodes = field_values.get("nodes")?.as_array()?;
14879 Some(change_token::Facets::new(kind.facet(), facet_fields(nodes), &labels).digest())
14880}
14881
14882/// The definition of each board field one item's values are values of, in the shape a read
14883/// of the board's own `fields` gives one.
14884///
14885/// A value names its field through a fragment on that field's own type, so the type is
14886/// known from which kind of value it is: a single-select value's field is a
14887/// `ProjectV2SingleSelectField`, options and all, and a text value's is a `ProjectV2Field`.
14888/// A value whose field carried no id, or an empty one, says nothing usable and is left out.
14889fn field_definitions(field_values: &[Value]) -> Vec<Value> {
14890 field_values
14891 .iter()
14892 .filter_map(|value| {
14893 let field = value.get("field")?.as_object()?;
14894 field.get("id")?.as_str().filter(|id| !id.is_empty())?;
14895 let typename = if value.get("text").is_some() {
14896 "ProjectV2Field"
14897 } else if value.get("name").is_some() {
14898 "ProjectV2SingleSelectField"
14899 } else {
14900 return None;
14901 };
14902 let mut defined = field.clone();
14903 defined.insert("__typename".to_owned(), json!(typename));
14904 // Only a text field holds a text value, so the value says the field's type too.
14905 if typename == "ProjectV2Field" {
14906 defined.insert("dataType".to_owned(), json!("TEXT"));
14907 }
14908 Some(Value::Object(defined))
14909 })
14910 .collect()
14911}
14912
14913fn text_field(field_values: &[Value], name: &str) -> Result<Option<String>, SourceError> {
14914 let Some(node) = field_values
14915 .iter()
14916 .find(|node| node.pointer("/field/name").and_then(Value::as_str) == Some(name))
14917 else {
14918 return Ok(None);
14919 };
14920 Ok(optional_str(node, "text")?.map(str::to_owned))
14921}
14922
14923fn valid_github_owner(owner: &str) -> bool {
14924 !owner.is_empty()
14925 && owner.len() <= 39
14926 && !owner.starts_with('-')
14927 && !owner.ends_with('-')
14928 && !owner.contains("--")
14929 && owner
14930 .bytes()
14931 .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
14932}
14933
14934/// GitHub's repository-name grammar: 1-100 ASCII letters, digits, `-`, `_` or `.`, and
14935/// neither of the two names a path segment already means.
14936fn valid_github_repository_name(name: &str) -> bool {
14937 !name.is_empty()
14938 && name.len() <= 100
14939 && name != "."
14940 && name != ".."
14941 && name
14942 .bytes()
14943 .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
14944}
14945
14946fn valid_environment_name(name: &str) -> bool {
14947 let mut bytes = name.bytes();
14948 bytes
14949 .next()
14950 .is_some_and(|byte| byte.is_ascii_alphabetic() || byte == b'_')
14951 && bytes.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
14952}
14953
14954/// How many sub-issues one issue has.
14955///
14956/// `Issue.subIssuesSummary` is `SubIssuesSummary!` and its `total` is `Int!`, so an
14957/// absent or non-integer one is a response this source cannot read — and reading it as
14958/// zero would classify a project as a task, which is exactly the mistake the marker
14959/// exists to keep from happening quietly.
14960fn sub_issue_total(issue: &Value) -> Result<u64, SourceError> {
14961 let summary = issue
14962 .get("subIssuesSummary")
14963 .ok_or_else(|| SourceError::Malformed {
14964 message: "GitHub issue is missing subIssuesSummary".into(),
14965 })?;
14966 summary
14967 .get("total")
14968 .and_then(Value::as_u64)
14969 .ok_or_else(|| SourceError::Malformed {
14970 message: "GitHub issue subIssuesSummary.total is not an unsigned integer".into(),
14971 })
14972}
14973
14974/// One issue's own `number`.
14975///
14976/// An issue always has one: GitHub declares `Issue.number` as `Int!` and every selection of
14977/// an issue in this module asks for it. So a read of one that comes back without it, or
14978/// with something that is not an unsigned integer, is a response this source cannot read —
14979/// absence here is **not** "this issue has no number". A draft is the content that has
14980/// none, and a draft never reaches this: the caller decides on `__typename` first, the way
14981/// it does for `subIssuesSummary`, which `DraftIssue` equally declares nothing for.
14982fn issue_number(issue: &Value) -> Result<u64, SourceError> {
14983 issue
14984 .get("number")
14985 .and_then(Value::as_u64)
14986 .ok_or_else(|| SourceError::Malformed {
14987 message: "GitHub issue number is missing or is not an unsigned integer".into(),
14988 })
14989}
14990
14991/// The `number` a creating mutation answered with, and `None` when it answered without one;
14992/// why a missing one is tolerated is at the call in `create_and_file_issue`.
14993fn created_issue_number(created: &Value) -> Result<Option<u64>, SourceError> {
14994 match created.get("number") {
14995 None | Some(Value::Null) => Ok(None),
14996 Some(value) => value
14997 .as_u64()
14998 .map(Some)
14999 .ok_or_else(|| SourceError::Malformed {
15000 message: "GitHub created issue number is not an unsigned integer".into(),
15001 }),
15002 }
15003}
15004
15005fn required_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
15006 value
15007 .get(field)
15008 .and_then(Value::as_str)
15009 .ok_or_else(|| SourceError::Malformed {
15010 message: format!("GitHub response is missing string field {field}"),
15011 })
15012}
15013
15014fn required_nonblank_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
15015 let found = required_str(value, field)?;
15016 if found.trim().is_empty() {
15017 return Err(SourceError::Malformed {
15018 message: format!("GitHub response has blank string field {field}"),
15019 });
15020 }
15021 Ok(found)
15022}
15023
15024/// The slot's delimiters, which `docs/metadata.md` settles once for every source that
15025/// needs one — Linear spells them too, in its own description field.
15026///
15027/// Restated rather than shared, because a plugin crate depends on the contract crate and
15028/// nothing else of this workspace. `scripts/check-metadata-slot-encoding.sh`, a target in
15029/// `check`, is what keeps the two one encoding: drift is otherwise quiet, since each
15030/// source round-trips its own writes perfectly well under its own spelling.
15031const METADATA_OPEN: &str = "<!-- onetaskgraph.metadata\n";
15032const METADATA_CLOSE: &str = "\n-->";
15033
15034/// What the composer puts between a non-empty visible body and the slot, and the one thing
15035/// the parser takes off the visible body when it takes the slot off — exactly once, so every
15036/// other trailing byte of the body comes back as it was written.
15037// 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.
15038const METADATA_SEPARATOR: &str = "\n\n";
15039
15040/// The visible body and the metadata slot at the end of it.
15041///
15042/// The encoding is the one `docs/metadata.md` settles for Linear, which is where its
15043/// reasons are. Only a comment at the very end is a slot; one in the middle is a person's
15044/// own content and is left alone. The visible body is everything before the slot less the
15045/// one [`METADATA_SEPARATOR`] the composer put there, byte for byte.
15046fn metadata_body(
15047 body: Option<String>,
15048) -> Result<(Option<String>, BTreeMap<String, Value>), SourceError> {
15049 let Some(body) = body else {
15050 return Ok((None, BTreeMap::new()));
15051 };
15052 let Some(slot) = slot_span(&body)? else {
15053 return Ok((Some(body), BTreeMap::new()));
15054 };
15055 let metadata =
15056 serde_json::from_str(&body[slot.encoded_start..slot.encoded_end]).map_err(|error| {
15057 SourceError::Malformed {
15058 message: format!(
15059 "invalid canonical JSON in GitHub issue onetaskgraph metadata slot: {error}"
15060 ),
15061 }
15062 })?;
15063 let before = &body[..slot.start];
15064 let visible = before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before);
15065 Ok(((!visible.is_empty()).then(|| visible.to_owned()), metadata))
15066}
15067
15068/// Where the metadata slot sits in one body, as byte offsets into it.
15069struct SlotSpan {
15070 /// Where [`METADATA_OPEN`] begins.
15071 start: usize,
15072 /// Where the encoded JSON begins, just past [`METADATA_OPEN`].
15073 encoded_start: usize,
15074 /// Where the encoded JSON ends, at the start of [`METADATA_CLOSE`].
15075 encoded_end: usize,
15076 /// Just past [`METADATA_CLOSE`].
15077 end: usize,
15078}
15079
15080/// The slot at the very end of `body`, or `None` when it has none.
15081///
15082/// The one reading of *where the slot is*, shared by [`metadata_body`], which reads it, and
15083/// [`with_slot`], which rewrites it — so the two cannot disagree about which comment is the
15084/// slot.
15085fn slot_span(body: &str) -> Result<Option<SlotSpan>, SourceError> {
15086 let Some(start) = body.rfind(METADATA_OPEN) else {
15087 return Ok(None);
15088 };
15089 let encoded_start = start + METADATA_OPEN.len();
15090 let Some(relative_end) = body[encoded_start..].find(METADATA_CLOSE) else {
15091 return Err(SourceError::Malformed {
15092 message: "unterminated onetaskgraph metadata slot in GitHub issue body".into(),
15093 });
15094 };
15095 let encoded_end = encoded_start + relative_end;
15096 let end = encoded_end + METADATA_CLOSE.len();
15097 if !body[end..].trim().is_empty() {
15098 return Ok(None);
15099 }
15100 Ok(Some(SlotSpan {
15101 start,
15102 encoded_start,
15103 encoded_end,
15104 end,
15105 }))
15106}
15107
15108/// `body` with its metadata slot holding exactly `metadata`, and every byte outside the
15109/// slot as it was.
15110///
15111/// A slot that is there has its JSON replaced in place; one that becomes empty is removed
15112/// together with the one [`METADATA_SEPARATOR`] separating it from the prose before it. A
15113/// body with no slot gains one the way [`compose_body`] writes it — after that separator,
15114/// or alone in an empty body — and a body with no slot that is given no metadata is
15115/// returned as it is.
15116fn with_slot(body: &str, metadata: &BTreeMap<String, Value>) -> Result<String, SourceError> {
15117 let encoded = if metadata.is_empty() {
15118 None
15119 } else {
15120 Some(
15121 serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
15122 message: error.to_string(),
15123 })?,
15124 )
15125 };
15126 Ok(match (slot_span(body)?, encoded) {
15127 (Some(slot), Some(encoded)) => format!(
15128 "{}{encoded}{}",
15129 &body[..slot.encoded_start],
15130 &body[slot.encoded_end..]
15131 ),
15132 (Some(slot), None) => {
15133 let before = &body[..slot.start];
15134 format!(
15135 "{}{}",
15136 before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before),
15137 &body[slot.end..]
15138 )
15139 }
15140 (None, None) => body.to_owned(),
15141 (None, Some(encoded)) if body.is_empty() => {
15142 format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15143 }
15144 (None, Some(encoded)) => {
15145 format!("{body}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15146 }
15147 })
15148}
15149
15150/// `body` with everything before its metadata slot replaced by `content`, and the slot
15151/// itself kept byte for byte.
15152///
15153/// The inverse of how [`metadata_body`] splits a body: the slot, when there is one, follows
15154/// `content` after the one [`METADATA_SEPARATOR`] the composer puts there — or alone, when
15155/// `content` is empty — so a read of the result reports `content` as the visible body and
15156/// the slot's metadata exactly as it was.
15157fn with_content(body: &str, content: &str) -> Result<String, SourceError> {
15158 let Some(slot) = slot_span(body)? else {
15159 return Ok(content.to_owned());
15160 };
15161 let kept = &body[slot.start..];
15162 Ok(if content.is_empty() {
15163 kept.to_owned()
15164 } else {
15165 format!("{content}{METADATA_SEPARATOR}{kept}")
15166 })
15167}
15168
15169/// Hold `entries` under `key` in one slot's metadata, or no such key when there are none.
15170fn set_task_list(metadata: &mut BTreeMap<String, Value>, key: &str, entries: &[TaskRef]) {
15171 if entries.is_empty() {
15172 metadata.remove(key);
15173 } else {
15174 metadata.insert(
15175 key.to_owned(),
15176 Value::Array(
15177 entries
15178 .iter()
15179 .map(|entry| Value::String(entry.as_str().to_owned()))
15180 .collect(),
15181 ),
15182 );
15183 }
15184}
15185
15186fn compose_body(
15187 content: Option<&str>,
15188 metadata: &BTreeMap<String, Value>,
15189) -> Result<Option<String>, SourceError> {
15190 let visible = content.unwrap_or_default();
15191 if metadata.is_empty() {
15192 return Ok((!visible.is_empty()).then(|| visible.to_owned()));
15193 }
15194 let encoded = serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
15195 message: error.to_string(),
15196 })?;
15197 Ok(Some(if visible.is_empty() {
15198 format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15199 } else {
15200 format!("{visible}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15201 }))
15202}
15203
15204fn required_bool(value: &Value, field: &str) -> Result<bool, SourceError> {
15205 value
15206 .get(field)
15207 .and_then(Value::as_bool)
15208 .ok_or_else(|| SourceError::Malformed {
15209 message: format!("GitHub response is missing boolean field {field}"),
15210 })
15211}
15212fn optional_str<'a>(value: &'a Value, field: &str) -> Result<Option<&'a str>, SourceError> {
15213 match value.get(field) {
15214 None | Some(Value::Null) => Ok(None),
15215 Some(value) => value
15216 .as_str()
15217 .map(Some)
15218 .ok_or_else(|| SourceError::Malformed {
15219 message: format!("GitHub response field {field} is not a string or null"),
15220 }),
15221 }
15222}
15223fn optional_nodes<'a>(
15224 connection: Option<&'a Value>,
15225 name: &str,
15226) -> Result<Option<&'a Vec<Value>>, SourceError> {
15227 match connection {
15228 None | Some(Value::Null) => Ok(None),
15229 Some(value) => value
15230 .get("nodes")
15231 .and_then(Value::as_array)
15232 .map(Some)
15233 .ok_or_else(|| SourceError::Malformed {
15234 message: format!("GitHub {name}.nodes is not an array"),
15235 }),
15236 }
15237}
15238fn complete_connection(connection: &Value, name: &str, size: u32) -> Result<(), SourceError> {
15239 let page_info = connection
15240 .get("pageInfo")
15241 .ok_or_else(|| SourceError::Malformed {
15242 message: format!("GitHub {name} has no pageInfo"),
15243 })?;
15244 if required_bool(page_info, "hasNextPage")? {
15245 return Err(SourceError::Malformed {
15246 message: format!(
15247 "GitHub {name} exceeds the supported nested connection size of {size}"
15248 ),
15249 });
15250 }
15251 Ok(())
15252}
15253fn optional_time(value: &Value, field: &str) -> Result<Option<DateTime<Utc>>, SourceError> {
15254 optional_str(value, field)?
15255 .map(|timestamp| {
15256 timestamp.parse().map_err(|error| SourceError::Malformed {
15257 message: format!("GitHub response field {field} is not a timestamp: {error}"),
15258 })
15259 })
15260 .transpose()
15261}
15262fn validate_page(page: &PageRequest) -> Result<(), SourceError> {
15263 if page.limit == 0 {
15264 Err(SourceError::Config {
15265 message: "page limit must be at least 1".into(),
15266 })
15267 } else {
15268 Ok(())
15269 }
15270}
15271fn next_cursor(connection: &Value) -> Result<Option<Cursor>, SourceError> {
15272 let page = connection
15273 .get("pageInfo")
15274 .filter(|value| value.is_object())
15275 .ok_or_else(|| SourceError::Malformed {
15276 message: "GitHub connection is missing pageInfo".into(),
15277 })?;
15278 if required_bool(page, "hasNextPage")? {
15279 let cursor = required_str(page, "endCursor")?;
15280 validate_cursor_progress(None, cursor)?;
15281 Ok(Some(Cursor(cursor.into())))
15282 } else {
15283 Ok(None)
15284 }
15285}
15286fn validate_cursor_progress(previous: Option<&str>, next: &str) -> Result<(), SourceError> {
15287 if next.is_empty() || previous == Some(next) {
15288 Err(SourceError::Malformed {
15289 message: "GitHub pagination cursor is empty or did not advance".into(),
15290 })
15291 } else {
15292 Ok(())
15293 }
15294}
15295/// The version of this plugin's opaque narrowing-search cursor.
15296pub const SEARCH_CURSOR_VERSION: u32 = 4;
15297
15298#[derive(Serialize, Deserialize)]
15299#[serde(tag = "state", rename_all = "snake_case", deny_unknown_fields)]
15300enum SearchConnection {
15301 Initial {},
15302 Continuing { after: Cursor },
15303 Exhausted {},
15304}
15305impl SearchConnection {
15306 fn after(&self) -> Option<&str> {
15307 match self {
15308 Self::Continuing { after } => Some(&after.0),
15309 _ => None,
15310 }
15311 }
15312 fn exhausted(&self) -> bool {
15313 matches!(self, Self::Exhausted { .. })
15314 }
15315 /// Whether a cursor naming this position, `offset` rows into its page, is one this
15316 /// plugin could have handed out: a page is resumed only part of the way through it — an
15317 /// offset of a whole page or more would skip rows nobody was given — an initial page
15318 /// only once some of it was handed out, and an exhausted connection has no page to be
15319 /// part of the way through.
15320 fn valid_resume(&self, offset: usize) -> bool {
15321 let within = offset < SEARCH_PAGE_SIZE as usize;
15322 match self {
15323 Self::Initial { .. } => offset > 0 && within,
15324 Self::Continuing { after } => !after.0.is_empty() && within,
15325 Self::Exhausted { .. } => offset == 0,
15326 }
15327 }
15328}
15329
15330/// Versioned source cursor. A zero offset and empty own-write ids are omitted.
15331#[derive(Serialize, Deserialize)]
15332#[serde(deny_unknown_fields)]
15333struct SearchPosition {
15334 version: u32,
15335 connection: SearchConnection,
15336 /// How many rows of the page `connection` starts were already handed out.
15337 #[serde(default, skip_serializing_if = "is_zero")]
15338 offset: usize,
15339 #[serde(default, skip_serializing_if = "Vec::is_empty")]
15340 seen: Vec<NativeId>,
15341 #[serde(default, skip_serializing_if = "Vec::is_empty")]
15342 own: Vec<NativeId>,
15343}
15344impl Default for SearchPosition {
15345 fn default() -> Self {
15346 Self {
15347 version: SEARCH_CURSOR_VERSION,
15348 connection: SearchConnection::Initial {},
15349 offset: 0,
15350 seen: Vec::new(),
15351 own: Vec::new(),
15352 }
15353 }
15354}
15355
15356fn is_zero(offset: &usize) -> bool {
15357 *offset == 0
15358}
15359
15360fn numeric_cursor(cursor: Option<&Cursor>) -> Result<usize, SourceError> {
15361 cursor.map_or(Ok(0), |c| {
15362 c.0.parse().map_err(|_| SourceError::Config {
15363 message: "page cursor is invalid".into(),
15364 })
15365 })
15366}
15367fn offset_page<T>(mut items: Vec<T>, offset: usize, limit: usize) -> Page<T> {
15368 if offset > items.len() {
15369 return Page::last(vec![]);
15370 }
15371 let tail = items.split_off(offset);
15372 let mut selected = tail;
15373 let next = (selected.len() > limit).then(|| Cursor((offset + limit).to_string()));
15374 selected.truncate(limit);
15375 Page {
15376 items: selected,
15377 next,
15378 }
15379}
15380
15381impl GitHubProjectsSource {
15382 async fn write_task_assets(
15383 &self,
15384 write: &ItemWrite<Task>,
15385 assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
15386 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
15387 self.write_item(
15388 &self.task_incoming(write, assets)?,
15389 write.target.as_ref(),
15390 &write.depends_on,
15391 )
15392 .await
15393 }
15394
15395 /// One task write as [`Self::write_item`] takes it, once what it delivers and its priority
15396 /// are known to be ones this source can write.
15397 fn task_incoming<'a>(
15398 &self,
15399 write: &'a ItemWrite<Task>,
15400 assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
15401 ) -> Result<Incoming<'a>, SourceError> {
15402 let near = write.target.as_ref().unwrap_or(&write.item.id);
15403 for (key, entries) in [
15404 (TaskRef::DELIVERS_KEY, &write.item.delivers),
15405 (TaskRef::DELIVERED_BY_KEY, &write.item.delivered_by),
15406 ] {
15407 TaskRef::listed(key, near, Some(&self.name), entries.clone())
15408 .map_err(|message| SourceError::Refused { message })?;
15409 }
15410 if self.priorities.is_none() && write.item.priority != Priority::None {
15411 return Err(self.holds_no_priority());
15412 }
15413 Ok(Incoming {
15414 written: Written::Work(ItemKind::Task, &write.item.status),
15415 title: &write.item.title,
15416 content: write.item.content.as_deref(),
15417 assets,
15418 labels: &write.item.labels,
15419 metadata: &write.item.metadata,
15420 repositories: &write.item.repositories,
15421 classification: write.item.classification,
15422 parent: write.item.project.as_ref(),
15423 delivers: &write.item.delivers,
15424 delivered_by: &write.item.delivered_by,
15425 priority: self.priorities.as_ref().map(|_| write.item.priority),
15426 })
15427 }
15428 async fn write_document_assets(
15429 &self,
15430 write: &ItemWrite<Document>,
15431 assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
15432 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
15433 // A document takes part in no dependency graph, so there is no far end to write
15434 // natively and none to record: a caller naming one is told so rather than having it
15435 // stored under the reserved key, where a later read would report an edge the
15436 // contract says cannot exist.
15437 if !write.depends_on.is_empty() {
15438 return Err(SourceError::Refused {
15439 message: format!(
15440 "this write names {} dependencies for a document, and a document takes \
15441 part in no dependency graph; next: put the dependency on the task or \
15442 project the document is about",
15443 write.depends_on.len()
15444 ),
15445 });
15446 }
15447 self.write_item(
15448 &Incoming {
15449 written: Written::Document,
15450 title: &write.item.title,
15451 content: write.item.content.as_deref(),
15452 assets,
15453 labels: &write.item.labels,
15454 metadata: &write.item.metadata,
15455 repositories: &write.item.repositories,
15456 classification: write.item.classification,
15457 parent: write.item.project.as_ref(),
15458 delivers: &[],
15459 delivered_by: &[],
15460 priority: None,
15461 },
15462 write.target.as_ref(),
15463 &[],
15464 )
15465 .await
15466 }
15467}
15468
15469/// What [`TaskSource::end_command`] leaves of this source's held state, asserted on the state
15470/// itself, for the two things no journey can observe.
15471///
15472/// The journeys in `crates/onetaskgraph-e2e/tests/e2e/end_command.rs` prove through the engine,
15473/// with and without the call, that a settlement, a board listing and a metadata search each
15474/// read afresh after it — the resolved records, the written-item overlay, the board and its
15475/// search, and the narrowed searches. What they cannot reach is the held field definitions,
15476/// because a status write naming an option a person deleted is refused the same whether or
15477/// not the list is held, and a poisoned lock, because nothing outside the source can panic
15478/// while one of its locks is held. So these assert those directly, and every other holder
15479/// beside them so a holder added later without a clear in the call fails here.
15480#[cfg(test)]
15481mod end_command_tests {
15482 use super::*;
15483
15484 struct Token;
15485
15486 impl SecretResolver for Token {
15487 fn get(&self, var: &str) -> Option<SecretString> {
15488 (var == "GH_PROJECTS_TOKEN").then(|| "test-token".into())
15489 }
15490 }
15491
15492 fn source() -> GitHubProjectsSource {
15493 let config = serde_json::from_value(json!({
15494 "owner": "octo-org", "project_number": 7, "repository": "acme/work",
15495 // Nothing here is sent: the source is only built and its state inspected.
15496 "endpoint": "http://127.0.0.1:9/graphql",
15497 }))
15498 .expect("a usable configuration");
15499 GitHubProjectsSource::new(&SourceName::new("work").unwrap(), config, &Token)
15500 .expect("the source builds")
15501 }
15502
15503 /// One issue as a board read answers it.
15504 fn resolved(source: &GitHubProjectsSource) -> Resolved {
15505 source
15506 .resolve(&json!({
15507 "id": "ITEM-1",
15508 "content": {"__typename": "Issue", "id": "I_1", "title": "Held",
15509 "body": "what a person may since have edited", "state": "OPEN",
15510 "stateReason": null, "url": null, "number": 1,
15511 "subIssuesSummary": {"total": 0},
15512 "labels": {"nodes": [], "pageInfo": {"hasNextPage": false}}},
15513 "fieldValues": {"nodes": [], "pageInfo": {"hasNextPage": false}},
15514 }))
15515 .expect("the item reads")
15516 .expect("an issue")
15517 }
15518
15519 /// Hold something in every holder the call clears, and the repository id it keeps.
15520 fn fill(source: &GitHubProjectsSource) {
15521 let item = resolved(source);
15522 source.created.lock().unwrap().push(item.clone());
15523 source.updated.lock().unwrap().push(item.clone());
15524 *source.board_cache.lock().unwrap() = Some(Board {
15525 id: "PVT-board".into(),
15526 fields: json!({"nodes": []}),
15527 items: vec![item.clone()],
15528 });
15529 *source.search_cache.lock().unwrap() = Some(vec![item.clone()]);
15530 source
15531 .narrowed_cache
15532 .lock()
15533 .unwrap()
15534 .insert("status:todo".into(), vec![item.clone()]);
15535 source.children_cache.lock().unwrap().insert(
15536 NativeId("P-1".into()),
15537 (NativeId("P-1".into()), vec![item.clone()]),
15538 );
15539 source
15540 .search_next
15541 .lock()
15542 .unwrap()
15543 .insert("status:todo".into(), Some("cursor".into()));
15544 source
15545 .resolved_cache
15546 .lock()
15547 .unwrap()
15548 .insert(item.id.clone(), item);
15549 *source.fields_cache.lock().unwrap() = Some(BoardFields {
15550 id: BoardId::parse("PVT-board").unwrap(),
15551 fields: json!({"nodes": []}),
15552 });
15553 source
15554 .repository_cache
15555 .lock()
15556 .unwrap()
15557 .insert(RepositoryTarget::parse("acme/work").unwrap(), "R_1".into());
15558 }
15559
15560 fn assert_dropped(source: &GitHubProjectsSource) {
15561 assert!(source.created().unwrap().is_empty(), "created");
15562 assert!(source.updated().unwrap().is_empty(), "updated");
15563 assert!(source.board_cache().unwrap().is_none(), "board");
15564 assert!(source.search_cache.lock().unwrap().is_none(), "search");
15565 assert!(source.narrowed_cache.lock().unwrap().is_empty(), "narrowed");
15566 assert!(
15567 source.children_cache.lock().unwrap().is_empty(),
15568 "project children"
15569 );
15570 assert!(
15571 source.search_next.lock().unwrap().is_empty(),
15572 "search paging"
15573 );
15574 assert!(
15575 source.resolved_cache().unwrap().is_empty(),
15576 "resolved records"
15577 );
15578 assert!(source.fields_cache().unwrap().is_none(), "board fields");
15579 assert_eq!(
15580 source.repository_cache().unwrap().len(),
15581 1,
15582 "a repository's node id stays valid and is kept"
15583 );
15584 }
15585
15586 fn end(source: &GitHubProjectsSource) {
15587 tokio::runtime::Builder::new_current_thread()
15588 .build()
15589 .unwrap()
15590 .block_on(source.end_command())
15591 .expect("the command ends");
15592 }
15593
15594 #[test]
15595 fn the_call_drops_every_item_search_and_board_read_and_keeps_repository_ids() {
15596 let source = source();
15597 fill(&source);
15598 end(&source);
15599 assert_dropped(&source);
15600 }
15601
15602 #[test]
15603 fn the_call_clears_a_lock_an_earlier_failure_poisoned() {
15604 fn poison<T: Send>(held: &Mutex<T>) {
15605 std::thread::scope(|scope| {
15606 let _ = scope
15607 .spawn(|| {
15608 let _guard = held.lock().unwrap();
15609 panic!("a failure while the lock is held");
15610 })
15611 .join();
15612 });
15613 assert!(held.is_poisoned());
15614 }
15615 let source = source();
15616 fill(&source);
15617 poison(&source.created);
15618 poison(&source.updated);
15619 poison(&source.board_cache);
15620 poison(&source.search_cache);
15621 poison(&source.narrowed_cache);
15622 poison(&source.search_next);
15623 poison(&source.resolved_cache);
15624 poison(&source.fields_cache);
15625 assert!(
15626 source.resolved_cache().is_err(),
15627 "a poisoned lock is refused before the call"
15628 );
15629 end(&source);
15630 assert_dropped(&source);
15631 }
15632}
15633
15634#[cfg(test)]
15635mod unprojectable_fields_tests {
15636 use super::*;
15637
15638 /// The field names `docs/metadata.md` says a `metadata_fields` entry may not name.
15639 fn documented() -> std::collections::BTreeSet<String> {
15640 let document = std::fs::read_to_string(
15641 std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../docs/metadata.md"),
15642 )
15643 .expect("docs/metadata.md is readable");
15644 let document = document.split_whitespace().collect::<Vec<_>>().join(" ");
15645 let lead = "a `field` equal, ignoring case, to ";
15646 let start = document.find(lead).expect("the refused-field sentence") + lead.len();
15647 let sentence = &document[start..];
15648 let sentence = &sentence[..sentence
15649 .find("; and two entries naming one field")
15650 .expect("the refused-field sentence ends at the duplicate rule")];
15651 let (own, github) = sentence
15652 .split_once(" or a field GitHub owns on every board (")
15653 .expect("the sentence names GitHub's own fields");
15654 own.split('`')
15655 .skip(1)
15656 .step_by(2)
15657 .chain(
15658 github
15659 .strip_suffix(')')
15660 .expect("GitHub's own fields close the sentence")
15661 .split(", "),
15662 )
15663 .map(str::to_owned)
15664 .collect()
15665 }
15666
15667 #[test]
15668 fn the_documented_refused_fields_are_exactly_the_ones_the_configuration_refuses() {
15669 let refused: std::collections::BTreeSet<String> = UNPROJECTABLE_FIELDS
15670 .iter()
15671 .map(|&name| name.to_owned())
15672 .collect();
15673 assert_eq!(documented(), refused, "the fields docs/metadata.md lists");
15674 let instance = SourceName::new("work").unwrap();
15675 for name in UNPROJECTABLE_FIELDS {
15676 for spelled in [name.to_owned(), name.to_uppercase()] {
15677 let entry = MetadataFieldConfig {
15678 field: spelled.clone(),
15679 key: "team.machine".to_owned(),
15680 path: Vec::new(),
15681 };
15682 let refused = MetadataField::resolve(vec![entry], &instance)
15683 .expect_err("an unprojectable field is refused");
15684 assert!(
15685 refused.to_string().contains(&format!("{name:?}")),
15686 "{spelled}: {refused}"
15687 );
15688 }
15689 }
15690 }
15691}