onetaskgraph_github_projects/lib.rs
1//! A stateless onetaskgraph source over one GitHub Projects v2 board.
2//!
3//! **A board is a container of projects, not a project.** Its own `title`,
4//! `shortDescription` and `readme` are never read as an item's fields and are never
5//! written: nothing in this source can rename the board a user configured.
6//!
7//! **A project is an issue and its tasks are that issue's sub-issues.** GitHub's schema
8//! decides that: `Issue` exposes `parent`, `subIssues` and `subIssuesSummary`, and
9//! `DraftIssue` exposes none of them. Creating an issue needs a `repositoryId`, and a
10//! board has none, so a write without [`GitHubProjectsConfig::repository`] is refused
11//! naming the field — but that repository is the *fallback*, not the home of every item.
12//!
13//! <!-- llmlint: ignore-block[contracts_have_one_source_or_a_drift_gate] The rule's one
14//! executable source is `GitHubProjectsSource::creation_target`; this is where a reader of
15//! the module meets it, and `tests/plugin.rs` drives every arm below against the loopback
16//! board and asserts on `createIssue`'s own `repositoryId`, so the prose cannot outlive a
17//! change to the rule. -->
18//! **Which repository an issue is created in is decided by the item's own `repositories`
19//! field, under one rule.** Exactly one entry names the repository the issue is created in:
20//! a task issue is where a person finds the work from the repository it changes, and one
21//! filed in a board's nominated repository is invisible from every other. Zero entries, or
22//! two or more, name none, so a task's or a document's issue is created in the repository
23//! its parent project's issue lives in — read from the board, or from this process's own
24//! record of a project it created earlier in the same command — and a project's issue, or
25//! a task or document written with no parent, is created in the configured `repository:`.
26//! What that rule refuses, it refuses before `createIssue`, so no issue is half-created. An
27//! existing issue is never moved: the update path leaves the issue where it is and records
28//! the list in the metadata slot when it differs, so the read side's derivation and the
29//! creation rule agree by construction.
30//! <!-- llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate] -->
31//!
32//! **A document is an ordinary issue whose title begins [`DESIGN_TITLE_PREFIX`].** A
33//! board has no document type and nothing but issues to hold one in, so the title is the
34//! discriminator and it is the whole of it. The title this source *reports* is the one a
35//! person wrote, with the prefix taken off — the same way the metadata slot is taken off
36//! the body so `content` is what the person wrote — and writing a document puts the prefix
37//! back, so a round trip returns the title that went in.
38//!
39//! **Telling a document from a project from a task.** The design prefix is read **first**:
40//! a document is never a project and never a task, whatever sub-issues it has or does not
41//! have. Only then does the rest apply — a board issue is a project when *either* it has
42//! sub-issues *or* it carries [`ItemKind::METADATA_KEY`]; otherwise it is a task. A
43//! sub-issue is always a task, whatever it carries. The marker is sufficient and never
44//! necessary: it is what makes an *empty* project — the state a project copy passes
45//! through between creating the project and filing its first task — readable as a
46//! project, while the sub-issue arm lets a person author a project on the board by hand
47//! with no knowledge of this product's metadata at all. Reading the prefix later than the
48//! sub-issue rule would make a design issue with no sub-issues an empty project, which is
49//! exactly the state that rule exists to catch. Pull requests are neither a project nor a
50//! task nor a document and are ignored.
51//!
52//! **A task's comments are its issue's comments.** They are read off `Issue.comments` and
53//! written with `addComment`, `updateIssueComment` and `deleteIssueComment`, and a comment's
54//! id is GitHub's own node id for the `IssueComment`. Two things GitHub decides are refused
55//! rather than papered over: a board **draft** is not an issue and has no comments at all, so
56//! a comment call on one is refused rather than answered with an empty page; and GitHub signs
57//! every comment as the account the token belongs to, so a comment handed an author of its
58//! own is refused rather than posted under another name. GitHub's comment mutations take the
59//! comment's id and nothing else, so an edit or a delete first reads which issue that comment
60//! is on, and a comment on some other issue is one this task does not have.
61//!
62//! **Where an entity is, is a link.** Every project, task and document this source reports
63//! carries a [`Location::Url`] naming the issue's own web address — the same address the
64//! `url` field already reports, in the shape that says a reader can open it. That is the
65//! contrast the location contract exists for: a reader holding an entity from this source
66//! is handed something to link to and one holding an entity from a folder of Markdown is
67//! handed a path, and neither has to know which plugin answered. It does not replace or
68//! derive from `url`; that field goes on reporting what it always reported.
69//!
70//! **Where metadata lives.** Short typed things go to typed fields and native relations:
71//! status to the board's `Status` single-select and the issue's own state, the copy
72//! origin to a source-owned `onetaskgraph.origin` text field, and dependencies to
73//! `blockedBy` and to sub-issue links. Unbounded caller JSON goes in a trailing
74//! `<!-- onetaskgraph.metadata ... -->` comment at the end of the issue body — the same
75//! encoding `docs/metadata.md` settles for Linear, not a second one. A ProjectV2 text
76//! field is length-bounded and `shortDescription` is capped at 300 characters, which is
77//! why neither can hold a caller's own prose. Setting one caller key on its own — on a task,
78//! a project or a document alike — is one update of the issue body that changes that slot
79//! and not one byte outside it, and it is not sent at all when the key already holds the
80//! value. The link a copy records on an item it copied, `onetaskgraph.copies`, is small and
81//! is kept in that same slot, written by that same update.
82//!
83//! **A caller's value can also be projected onto a board text field**, so the board can filter
84//! on it: each [`GitHubProjectsConfig::metadata_fields`] entry names a text field, a top-level
85//! metadata key and a path inside its value. The slot stays the value's one home — every read
86//! reports metadata from it alone — and the field follows it on every write of an item, a
87//! create, an update and a copy alike: a string is written, and not sent again while the field
88//! holds it — an empty string included, which is written as itself; nothing there or `null`
89//! clears a field holding a value; any other
90//! JSON type is refused before any mutation. It rides the ordered [`graphql::UPDATE_FIELDS`]
91//! write beside `Status`, `Priority` and the origin, so it adds no request; a key set on its
92//! own moves its field first and the body last, as every write of an existing item does. A
93//! board without the field, or with a non-text field of that name, is refused before any
94//! mutation pointing at `sources fields`, whose [`GitHubProjectsSource::fields`] creates it as
95//! a text field and never changes a field that is there.
96//!
97// llmlint: ignore-block[contracts_have_one_source_or_a_drift_gate] This public module documentation is a required user-facing description; the loopback plugin tests and shared live journey drive StatusMapping resolution, both mutations, and observed read-back together.
98//! **Status.** `status_mapping` is per-instance configuration, in the shared grammar
99//! [`onetaskgraph_plugin_api::StatusMapping`] documents, from a status category to an
100//! option of the board's one `Status` field for a task and for a project: a bare name is
101//! the option for both kinds, `null` disables the category for both, and `{task, project}`
102//! names it per kind. A category the mapping does not mention keeps its shipped default for
103//! both kinds; one it mentions is exactly what it configures, so a per-kind object no longer
104//! gets the shipped default for the kind it leaves out. Two categories one kind would read
105//! back from one option are refused as the configuration is read, ignoring case, while one
106//! option may stand for different categories of the two kinds. Writes go by the kind of the
107//! item written: a status that kind has no option for, or whose option the board lacks, is
108//! refused before any mutation, naming the source, the kind, the category and the key
109//! `status_mapping.<category>.<kind>` — there is no fallback. `done` selects its mapped
110//! option and closes the issue as `COMPLETED`; `cancelled` selects its mapped option and
111//! closes it as `NOT_PLANNED`, for either kind. Every open category reopens a closed issue
112//! before selecting its option. Reads give a closed issue's reason precedence over its
113//! option, while an open issue's option decides its category through its own kind's
114//! mapping, and an option that mapping does not name reads as `unknown` under its own name.
115//! The guarded [`GitHubProjectsSource::status_options`] and
116//! [`GitHubProjectsSource::fields`] operations are the one path here that calls
117//! `updateProjectV2Field`: GitHub replaces the whole option list, so they preserve every
118//! existing option id and verify the field and item assignments immediately afterwards.
119//! They ask for both kinds' options, counting a terminal category's mapped option as
120//! configured because a terminal write refuses without it. No ordinary source read or
121//! write calls that mutation, whose
122//! `singleSelectOptions` *overwrites* a field's option set, so no addition is additive
123//! and a mistake destroys every item's status. A status this board cannot represent is a
124//! refusal naming the status and the instance instead.
125//!
126//! `unknown` has no shipped option because this source cannot preserve an open-ended
127//! status word: it writes an existing board option and never
128//! creates an option. An operator may map `unknown` to one existing option, in which case
129//! every unknown word lands on that option and reads back as `unknown` under the option's
130//! name. This differs from `local-md`, which writes and reads the original word itself.
131//!
132//! The shipped terminal options are exactly `done: Done` and `cancelled: Cancelled`.
133//! `done` also closes the issue because GitHub derives `subIssuesSummary.completed`
134//! and the board's own `Sub-issues progress` field from closed sub-issues: a plan whose
135//! finished tasks were only moved to a "Done" column would read 0% complete forever.
136// llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
137//!
138//! # What this source declares, field by field
139//!
140//! One verdict per field of [`Capabilities`], and what `Native` means when this source
141//! says it. *Proven* means a shared journey drives it against the real
142//! binary over this source's own row in `crates/onetaskgraph-e2e-support/src/fixtures.rs`, and
143//! `every_row_declares_exactly_what_its_plugin_reports` is what keeps this list and
144//! [`capabilities`](TaskSource::capabilities) from parting.
145//!
146//! | Field | Verdict |
147//! | --- | --- |
148//! | `projects` | **Supported and proven,** and the one predicate here that is pushed down rather than applied in process: a task's project is the issue it is a sub-issue of, so a listing scoped to one *asks that issue* for its own sub-issues. This is the field that was declared and then not applied, which silently returned another project's tasks. |
149//! | `documents` | **Supported and proven.** A board holds issues, so a document is one: the issue whose title begins [`DESIGN_TITLE_PREFIX`]. Reads, filters and paging answer on exactly the terms a task read does, and a write puts the prefix back. |
150//! | `comments` | **Supported and proven,** over the task issue's own comment connection, oldest first and paged by GitHub's own cursor; added, edited and removed through GitHub's comment mutations, paced as every other mutation is. A draft item has no comments on GitHub and is refused, and so is an author, because GitHub records the signed-in account as every comment's author. |
151//! | `assets` | **Supported and native.** Image references travel as GitHub user attachments in the repository the task or document issue lives in. Uploads are authenticated and verified before the issue body is written. |
152//! | `priority` | **Supported and proven** by an instance configured with `priority_mapping`, and declared unsupported by one without it, which reports every task's priority as `none` and sends exactly the requests it sent before priorities existed. The priority is the board's single-select `Priority` field: no value is `none`, a mapped option is its level, matched case-insensitively, and an option the mapping does not name fails the read of that task, naming the option. A write selects the mapped option, or clears the value for `none`; a board without the field or the option is refused, pointing at `sources fields`, which is the one thing that creates either. |
153//! | `filter_by_priority` | **Supported and proven,** over the priority each task reads as — `none` for every task of an instance without `priority_mapping`. |
154//! | `filter_by_comment_activity` | **Supported, and exact** for comments created and for comments edited at or after `commented_since`, in every repository — of any owner — the board's items live in. Applied by asking a narrower question rather than by reading the board: GitHub's issue search scoped by `project:<owner>/<number>` alone, with an `updated:>=` qualifier, names the candidates, and each candidate's own comments confirm it, so `ProjectV2.items` is never read. That rests on GitHub moving an issue's `updatedAt` when a comment on it is added **or edited**, which the credentialed journey `an_edited_comment_moves_its_issue_and_is_selected_since` re-takes on every run of this lane. The search is an index that lags a write — the credentialed lane has watched it miss a newly commented issue for thirty seconds — so an issue this process itself commented on in the same command is a candidate whatever the search says, read by its own node if the search did not name it, and has its comments read rather than being ruled out by an `updatedAt` from before the comment. A comment another process wrote is found only once the index has it, so a caller asking again from its last instant should overlap the two generously. |
155//! | `orphan_tasks` | **Supported and proven.** A task issue with no `parent` is in no project. |
156//! | `filter_by_label` | **Supported and proven,** over the issue's own labels. |
157//! | `filter_by_status` | **Supported and proven,** over the board's `Status` option and the issue's open or closed state, through this instance's own `status_mapping` for the item's kind — a task query by the task half, a project query by the project half, `unknown` included. |
158//! | `filter_by_metadata` | **Supported, and asked of GitHub** — through the body and never through a projected `metadata_fields` text field, which is the board's to filter on and not this source's to read. A query naming metadata values is one board-scoped issue search with each value a quoted phrase `in:body` — GitHub's index covers the metadata comment at the end of the body, which is where caller metadata lives — and every candidate is confirmed against its own parsed metadata comment, so only an item holding that string at that key and path is returned. **A value with no letter or digit is refused** — the empty string, whitespace or punctuation alone — before any request, as a `SourceError::Refused` (wire kind `refused`) naming the value: GitHub's index holds words, so no bounded query can find such a value, and this source neither reads the whole board for it nor answers it as empty. |
159//! | `filter_by_origin` | **Supported, and asked of GitHub without enumerating the board.** The union of three reads, each confirmed by an exact match against the item's own origin field: the board's field filter over the `onetaskgraph.origin` text field, the issue search for the id as a phrase in the body where a write of this release mirrors it, and this process's own writes. See *Where a read-after-write guarantee comes from* for the window the three leave. |
160//! | `reconcile_status` | **Supported,** and proven by `onetaskgraph-github-projects-e2e`'s `status_reconcile.rs`: the board's own `ProjectV2.items(query:)` filter, `is:closed`, read page by page and never the unfiltered items, each candidate confirmed from its own item node — a closed task whose card is not at the option the mapping gives its closure's category. |
161//! | `complete_by_issue` | **Supported,** and proven by `status_complete.rs` there and in the engine's suite: each issue a reference names read in one aliased request per fifty — its repository, its number, its first page of board memberships with each card's `Status` read by name — and nothing else of the board. |
162//! | `search_title` | **Supported, and asked of GitHub for a task,** over `Issue.title`: a task query's text is one board-scoped issue search for it as a phrase `in:title`, every candidate confirmed by the case-insensitive substring rule. GitHub matches whole words, so a task holding the text only inside a longer word is not returned — a narrowing this source declares rather than hides. **A text with no letter or digit that is not blank is refused** — `--` for one — before any request, as the same `refused` error naming the text, for the reason a metadata value like it is; a blank text is not refused, and keeps the board read it always had, confirmed by the same substring rule. A project query's text, and a document query's text when the query is scoped to no project, is that same board-scoped search for the same phrase in the same fields, refused on the same terms, every candidate confirmed by its kind and by the same substring rule, so it narrows exactly as a task's does; a document query scoped to one project sends no search, reads that project's sub-issues and confirms its text over them by the substring rule alone, so it is neither narrowed to whole words nor refused for a text with no letter or digit. A board draft is not an issue, so no text search lists one, a draft titled as a document included. |
163//! | `search_content` | **Supported,** on the same terms, `in:body`, over the visible body — the trailing metadata comment is not part of what the substring rule confirms. |
164//! | `task_dependencies` | **Supported and proven,** in both directions: `blockedBy` and `blocking`. |
165//! | `project_dependencies` | **Supported and proven,** in both directions, over the same two connections, because a project here is an issue. |
166//! | `max_page_size` | **Supported and proven.** [`MAX_PAGE_SIZE`], GitHub's own connection maximum. |
167//!
168//! Image writes use `POST https://uploads.github.com/user-attachments/assets` with `name`,
169//! `content_type` and the issue repository's numeric `repository_id` as query parameters,
170//! the source token as `Authorization: Bearer`, the image content type as `Content-Type`,
171//! and the raw bytes as the request body. The numeric repository id is read at most once
172//! per repository per source instance. A loopback API endpoint also selects the loopback
173//! uploads host; production needs no assets repository, commits or extra configuration.
174//! Every returned JSON `url` is read with the same token before a body is written: only
175//! a 2xx verification succeeds. A refusal names the asset, URL and HTTP status; an
176//! anonymous 404 does not invalidate an authenticated 200. An upload refusal names the
177//! asset and HTTP status and says the token type may not be accepted by the upload
178//! endpoint. A classic PAT was measured accepted; no acceptance claim is made about
179//! fine-grained PATs or OAuth tokens.
180//! The body keeps the authored alt text while `./<name>` becomes the attachment URL,
181//! and `onetaskgraph.assets` records `{sha256, url}` by name. A matching destination
182//! SHA-256 reuses its URL without an upload or verifying read; changed bytes are uploaded
183//! and verified afresh, in the repository the issue already lives in on an update.
184//! An uploaded image renders for the viewers GitHub lets read it, like the issue text beside it.
185//! Uploads take the same mutation spacing as content writes. Both uploads and verification
186//! reads wait out classified rate limits within the configured per-call retry budget,
187//! on the supplied clock; verifying reads take no mutation slot.
188//!
189//! Nothing here is unsupported. `documents` and `comments` are not predicates — they say this
190//! source has documents and that its tasks have comments, both of which hold — and the three
191//! facts behind the uniform `Native` on the
192//! predicates beside it are recorded below rather than re-derived, because a reader who
193//! takes `Native` to mean *the remote service filters* will read that uniformity as a
194//! lie.
195//!
196//! First, the plugin contract defines `Support::Native` as *the source applies this
197//! predicate itself*, and says nothing about where it applies it. What the declaration
198//! promises the engine is capability rule 1 — a predicate declared `Native` **is** applied
199//! — so that the engine may push it down and apply nothing of its own.
200//!
201//! Second, this source can keep that promise for every predicate at no additional API
202//! cost, because whichever of the reads below answers a query has already read every
203//! candidate that query will return before it filters anything. Filtering those items is
204//! in-process work over data already in hand.
205//!
206//! Third, six task predicates are asked of GitHub as a narrower question and the rest are
207//! applied in process over what that question returned. A project filter has a relationship — a
208//! project's tasks are that issue's sub-issues, and asking the issue for them is both cheaper
209//! and exact. Comment activity is the issue search's `updated:` qualifier. A text search, and
210//! a search for metadata values, is the board-scoped issue search carrying the text and each
211//! value as quoted phrases; an origin is the board's own field filter over its origin field
212//! beside the same search for the id. **The text search narrows, and that is this source's
213//! declared semantics:** GitHub matches whole words where the substring rule this source and
214//! the local Markdown source confirm with would match inside one, so an item holding the text
215//! only inside a longer word is never a candidate. Every item returned does contain the text.
216//! A project query's text, and a document query's scoped to no project, is that same search
217//! and narrows on the same terms, its candidates confirmed by their kind as well.
218//! GitHub's issue search offers no qualifier for a label set, a status column or a priority,
219//! so those three are applied in process over the candidates, and a query carrying none of
220//! the six narrowing predicates reads the board. Declaring one `Unsupported` would make the
221//! engine compensate for work this source has already done, and declaring `projects` native
222//! while ignoring the filter (which this source once did) silently returns another project's
223//! tasks, because the engine trusts the declaration and applies nothing locally.
224//!
225//! # The three ways this source reaches an item, and what each costs
226//!
227//! A board read is charged for what its *nested* connections could return rather than for
228//! what was asked, so one whole-board read costs the same whether the question was about
229//! one project or about all of them. That is why a question about one project is never
230//! answered by reading the board:
231//!
232//! | The question | What is sent | What it costs |
233//! | --- | --- | --- |
234//! | one item, by its own id | [`graphql::ISSUE`] — `node(id:)`, carrying the field definitions of the boards it sits on and the far ends of its `blockedBy`, which is what a write of it needs — and, when that node is a board draft, [`graphql::DRAFT`] — the draft and the one board item it is | the item |
235//! | one task with its first page of comments, for `task show` and a comment listing | [`graphql::ISSUE_DETAIL`] — the same `node(id:)` read with the issue's `comments` | the item and a page of its comments |
236//! | several tasks with their comments, for `task show-many` | [`graphql::ISSUE_DETAILS`] — [`DETAIL_BATCH`] aliased `node(id:)` fields per request | each item and a page of its comments |
237//! | the board's own id and field definitions, for a write whose item does not carry them | [`graphql::BOARD_FIELDS`] — the board's `id` and `fields`, and no `items` — or, for a create that needs the repository's id too, [`graphql::CREATION_CONTEXT`], both in one request | the board's fields |
238//! | one project's tasks or documents | [`graphql::SUB_ISSUES`] — that issue's own `subIssues` | that project |
239//! | which projects this board holds | [`graphql::SEARCH_ISSUES`] — an issue search scoped to the board | the board's issues, without their board items |
240//! | which projects hold a text, or which documents do when no project narrows the question | [`graphql::SEARCH_ISSUES`] — the board-scoped search with the text as one quoted phrase, `in:title`, `in:body` or both, as a task's text is sent — walked to its end in pages of twenty | the issues that match |
241//! | which tasks were commented on since an instant | [`graphql::SEARCH_ISSUES`] — the same board-scoped search with an `updated:>=` qualifier — then [`graphql::ISSUE_COMMENTS`] for each candidate it names | the issues updated since, and their comments |
242//! | which tasks hold a text, or a metadata value | [`graphql::SEARCH_ISSUES`] — the board-scoped search with the text and each value as quoted phrases, `in:title`, `in:body` or both, and an `updated:>=` qualifier too when comment activity is asked for — in pages of twenty, only as many as the caller's rows need | the issues that match |
243//! | which tasks were copied from one origin | [`graphql::ORIGIN_LOOKUP`] — the board's own `items` under its field filter on the origin field, and the same board-scoped search for the id `in:body`, in one request, each paged at three | the carriers of that origin, which is one item |
244//! | every task, every document, every label, when nothing above narrows the question | [`graphql::BOARD`] — the board's own `items` — **and** [`graphql::SEARCH_ISSUES`], because neither enumeration of a board is complete alone; see [`GitHubProjectsSource::board`] | the board, twice over |
245//! | which board item one issue is, past the page that came with it | [`graphql::ISSUE_BOARD_ITEMS`] — that issue's own `projectItems` | one issue's memberships |
246//!
247//! The following standalone-ticket requests are pinned by the real CLI fixture journeys
248//! `follow_up_writes_resolve_each_item_once_and_batch_the_copy_fields` and
249//! `a_batched_detail_read_costs_one_request_and_one_point_per_detail_batch`, as request count
250//! equal to declared points equal to the row. They include the origin lookup and the
251//! field/repository discovery a create needs. A bound re-copy changes status, priority,
252//! content and metadata; comment recount means a subsequent detail read. Each request here
253//! costs one declared point. A membership beyond the embedded page can additionally require
254//! the one-point membership recovery described above. A bound re-copy of a task filed under a
255//! project adds one read, the engine confirming that project's link by its own id once per
256//! command; and the same-source far ends a write newly names — those that do not already block
257//! the item, whose own read answered for them — are read together by their own ids,
258//! [`DETAIL_BATCH`] to one [`graphql::ISSUE_DETAILS`] request, each new edge then one
259//! [`graphql::ADD_BLOCKED_BY`]. Both additions are rows of the table below, pinned by
260//! `a_bound_recopy_adds_one_project_read_and_batches_the_dependencies_it_newly_names`.
261//!
262//! **[`DETAIL_BATCH`] is 24**: the largest batch of [`graphql::ISSUE_DETAILS`] the node-count
263//! model prices at one point. Each aliased item is six of GitHub's aggregate, so 24 are 144,
264//! which rounds to one point, and 25 are 150, which rounds to two; `tests/point_cost.rs`
265//! holds both halves.
266//!
267//! **An existing item is written body last.** A bound re-copy and a `task update` send its
268//! board fields first — the `Status` option and the `Priority` together, in one request — then
269//! its parent and its `blockedBy`, and its title, body and state in one `updateIssue` last.
270//! GitHub runs no two requests as one, and runs a document's mutation fields in order without
271//! undoing an earlier field when a later one fails, so that order is what makes a write
272//! refused part-way leave the item's body, and every metadata key in it, exactly as it stood;
273//! the one piece of metadata written before the body, an origin a copy re-points, is put back
274//! when a later write is refused — and when putting it back is refused too, the write's own
275//! refusal names that key, what it now holds and what it held. `crates/onetaskgraph-github-projects-e2e/tests/e2e/write_order.rs` refuses each
276//! of those writes in turn, whole and as one aliased field failing after the one before it.
277//!
278//! **Two facts about GitHub the write rows rest on, each read off GitHub's published schema
279//! artifact <https://docs.github.com/public/fpt/schema.docs.graphql> on 2026-10-01 and pinned
280//! in `tests/fixtures/schema.graphql`, and the first then put to GitHub itself:**
281//!
282//! - **A board is accepted at creation but its item is not answered, so a create still files
283//! the issue itself: a new copy is 5 requests, and 4 with `--create`.**
284//! `CreateIssueInput.projectV2Ids: [ID!]` is declared there — "An array of Node IDs for
285//! Projects V2 associated with this issue", `@possibleTypes(concreteTypes: ["ProjectV2"])`.
286//! The credentialed journey `real_projects_v2_contract_writes_and_leaves_no_residue` was run
287//! against a real board on 2026-10-01 with a create sending the board there and reading the
288//! item off the payload's `Issue.projectItems`: every one of its four creates answered with
289//! no item on the board, so each went on to [`graphql::ADD_TO_BOARD`], and the fourth was
290//! refused "Content already exists in this project" — GitHub had filed the issue after
291//! answering, and refuses a second filing rather than answering with the item it holds. A
292//! create therefore sends no `projectV2Ids` and files the issue with
293//! `addProjectV2ItemById`, the one call whose answer names the board item. The saving that is
294//! real is the read before it: the board's fields and the repository's id together, in
295//! [`graphql::CREATION_CONTEXT`], at the point the repository is known.
296//! - **A comment still reads its target first, so a comment is 2 requests.**
297//! `AddCommentInput.subjectId: ID!` is declared there with
298//! `@possibleTypes(concreteTypes: ["Issue", "PullRequest"], abstractType:
299//! "IssueOrPullRequest")`. A board draft is no such subject and would be refused, but a
300//! project's issue, a document's issue, an issue on no board of this source and a pull
301//! request all are: GitHub writes the comment, so there is no refusal to map into "that is
302//! not a task of this board". [`graphql::ISSUE`] before [`graphql::ADD_COMMENT`] is what
303//! refuses those by name.
304//!
305//! | Verb | Requests / points | Documents |
306//! | --- | --- | --- |
307//! | new copy | 4 | ORIGIN_LOOKUPS (the origin, the board's fields and the repository's node and numeric ids together, a page of one carrier for each origin), CREATE_ISSUE filing the issue on the board and under its project, CREATED_BOARD_ITEMS once GitHub has added it, WRITE_FIELD_VALUES |
308//! | copy --create | 4 | CREATION_CONTEXT, CREATE_ISSUE, CREATED_BOARD_ITEMS, WRITE_FIELD_VALUES: the new copy with no origin to ask about |
309//! | bound copy | 3 | ISSUE (with the board's fields and the issue's `blockedBy`, so no BOARD_FIELDS or ISSUE_DEPENDENCIES), UPDATE_FIELDS when a board field differs, then UPDATE_ISSUE last carrying only the title, body or state that differs — none, when nothing does |
310//! | bound copy, filed under a project | 4 | the bound copy's three, and one ISSUE of the destination project its link names, read once per command |
311//! | bound copy, newly naming n dependencies | + ceil(n / DETAIL_BATCH) + n | ISSUE_DETAILS for the far ends that do not already block the item, DETAIL_BATCH (24) to a request (one alone is ISSUE), then one ADD_BLOCKED_BY each; a far end already blocking it is answered by its own read and costs nothing |
312//! | comment | 2 | ISSUE, ADD_COMMENT: the target is read first, because GitHub accepts a comment on any issue or pull request (see below) |
313//! | detail | 1 | ISSUE_DETAIL: the item and its first page of comments, for `task show` and `task comment list`; `--no-comments` is ISSUE alone |
314//! | batched detail | ceil(n / DETAIL_BATCH) | ISSUE_DETAILS: `task show-many` of `n` items, DETAIL_BATCH (24) at a time, comments included or not |
315//! | copy --comments, n changed tasks | ceil(n / COPY_DETAIL_BATCH) | ISSUE_COPY_DETAILS: each task with its first page of comments and its `blockedBy`, COPY_DETAIL_BATCH (21) at a time; a task whose comments run past that page adds one ISSUE_DETAIL per further page, and a task skipped from its listing costs nothing beyond it |
316//! | recount | 1 | ISSUE_DETAIL |
317//! | status | 2 | ISSUE, UPDATE_FIELD; a terminal status additionally updates issue state |
318//! | priority | 2 | ISSUE, UPDATE_FIELD or CLEAR_FIELD, with stored priority in the mutation response |
319//! | content | 2 | ISSUE, UPDATE_ISSUE |
320//! | metadata | 2 | ISSUE, UPDATE_ISSUE |
321//! | document metadata, `--expect` a version | 2 | ISSUE, then UPDATE_ISSUE only when the issue is still at that version — the precondition GitHub's own update cannot take — primed with what the read answered |
322//! | update | 3 | `task update` naming any of title, body, metadata, status and priority — all five included: ISSUE, UPDATE_FIELDS (the status option and the priority together), UPDATE_ISSUE (title, body with its metadata slot, and state) last |
323//! | record only | 1 | ISSUE |
324//!
325//! <!-- github-search-paging:start -->
326//! Board-scoped text, metadata, project-name and comment-activity searches send every
327//! page at `first = 20` (SEARCH_PAGE_SIZE), the SEARCH_ISSUES document's one-point
328//! ceiling. A later page is sent only when `hasNextPage` is true and the caller still
329//! needs rows. A page is never resized to the rows still needed: GitHub orders one
330//! search differently at different page sizes, so one fixed size makes a paged walk
331//! send exactly the requests one whole read sends, and the answer's order is the order
332//! those pages arrive in. A page below twenty would cost the same one point, and GitHub
333//! prices this document by rows, so twenty-row pages cost per row what 100-row pages do.
334//! Project-name lookup continues until an exact match or exhaustion. A task limit bounds
335//! returned and fetched pages: a limit is sliced from the pages it needs, and local
336//! confirmation can require more candidates than matching rows. Walking all pages
337//! returns the whole answer. The opaque version-4 source cursor carries GitHub's page
338//! cursor and how far into that page the last answer stopped, and resumes in the same
339//! process or a new one, without duplicates or gaps. It carries no rows: one process
340//! sends each page's search once, and a new process re-reads only the page it resumes
341//! in, then sends a further page once, never as a re-read, only when its limit still
342//! needs rows. Every request either walk sends is the one a whole read sends for that page. Own writes replace stale index
343//! copies and complete missing rows at exhaustion. Cache entries are whole GitHub pages,
344//! so a small answer cannot truncate a wider question. Origin pages remain three; whole-board sizing is unchanged.
345//! Read-after-write is a per-process guarantee. A cursor resumed in a new process is
346//! not required to include the original process's writes still omitted by the index.
347//! <!-- github-search-paging:end -->
348//!
349//! The board half of an issue — its board item's id, its `Status` option and this
350//! source's origin text field — rides along on `Issue.projectItems` in the first three, so
351//! an item reached any of those ways resolves through the same
352//! [`GitHubProjectsSource::resolve`] the board walk uses and reports the same title, the
353//! same status, the same labels and the same qualified id. That connection comes back a
354//! *page* at a time, at `BOARD_ITEMS_PAGE_SIZE`, so the entry for this board is looked for
355//! on the page in hand and — only if that page reports more of the connection — in the
356//! last row's read of that one issue's memberships, resumed from the page's own cursor and
357//! walked to exhaustion. An issue with no entry for *this* board is not this source's to
358//! report, which is what keeps an id naming another repository's issue from being answered
359//! as an item of this board; and because the page is where the search starts rather than
360//! where it ends, that answer is one about a connection read to exhaustion and never about
361//! an unread page. Nothing costs the extra read but an issue on more boards than a page
362//! holds: an issue this board really does not hold reports no next page, so its
363//! memberships are already exhausted where they arrived.
364//!
365//! **No document here selects the board's own `Labels` field, and nothing is lost by
366//! that.** An item's labels are read from its content alone, wherever that content is
367//! reached: the three documents above select `Issue.labels` on the fragment, and
368//! [`graphql::BOARD`] selects the same connection on the `... on Issue` arm of its
369//! `content`. A board's `Labels` field is not one anybody fills in: it is a built-in
370//! `ProjectV2FieldType`, it is absent from `ProjectV2CustomFieldType` so no project can
371//! create one, and `ProjectV2FieldValue` — the whole of what
372//! `updateProjectV2ItemFieldValue` accepts — offers no way to write one. So GitHub derives
373//! it from the content, for every content type it exists on, and there is nothing it can
374//! hold that the content does not already say: for an `Issue` it *is* that issue's own
375//! labels, so selecting it beside them unions a set with itself.
376//!
377//! **A draft loses nothing by that either**, which is the reasoning this paragraph once had
378//! backwards. `DraftIssue` exposes no `labels` field, and by the three schema facts above
379//! it cannot carry a board `Labels` value to be derived from one — so a draft has nothing
380//! to select *and nothing to lose*, and reports no labels at all. A `PullRequest` item is
381//! discarded by [`GitHubProjectsSource::resolve`] before labels are read. Both halves are
382//! held to that by tests in `tests/plugin.rs`: the four ways an item is reached report one
383//! label set, and that set is the fixture issue's own, by
384//! `an_item_reports_the_same_labels_title_status_and_id_however_it_is_reached`; and a board
385//! item whose content is a draft reports an empty set, by
386//! `a_board_item_whose_content_is_a_draft_reports_no_labels_at_all`. The absence of the
387//! selection is held over [`graphql::DOCUMENTS`] by
388//! `no_document_selects_the_boards_own_labels_field`.
389//!
390//! The whole-board row is still the board's own item connection, and deliberately: a
391//! **draft** board item is not an issue, so no search can list one, and the reads that have
392//! to answer for the whole board are the ones whose cost is the board's size anyway.
393//!
394//! **A question about one item this source already names by id never lists the board.**
395//! Whether that item is on this board, and what its board fields are, is answered by reading
396//! that item — its own `Issue.projectItems`, walked to exhaustion by
397//! [`GitHubProjectsSource::resolve_issue`], or a draft's own board item — and never by
398//! looking for it in [`graphql::BOARD`]'s `items` or in a listing this command already
399//! holds. That covers a write's destination, the project a new item is filed under, a
400//! same-source far end a dependency names, a status write, the dependency slot a draft keeps,
401//! and the delete that takes back an item a copy made. What such a write needs of the board
402//! and the item does not carry — the board's id, the `Status` and origin field definitions —
403//! comes from [`graphql::BOARD_FIELDS`], which reads no item at all. The reason is evidence,
404//! not economy alone: `ProjectV2.items` is a projection that lags the membership GitHub
405//! itself reports — an issue added with `addProjectV2ItemById` can be missing from it for
406//! minutes. Scanning this host's 842-item board has refused a document copy and an update
407//! even though the items' own reads named that board. A scan there gives the wrong answer
408//! as well as paying for every page. So a `board.items` lookup does not belong on any of
409//! those paths.
410//!
411//! **What a read may return is capped too, and that cap is on the document rather than on
412//! the board.** GitHub limits the number of nodes **one query may return** to
413//! [`NODE_COUNT_LIMIT`] and refuses a query above that before executing it: the answer is
414//! an error naming the connection the count crossed at, not a slow or a partial result.
415//! Every board this source reads is refused the same way, so no board is too big for these
416//! documents and none is small enough to save one that is over.
417//!
418//! The count is arithmetic over the document's own text: each connection contributes the
419//! `first:` it asks for, counts **multiply** down a nested path and **sum** across sibling
420//! paths. Those are [GitHub's published rules][node-limits] and this workspace does not
421//! restate them — `github-graphql-node-count` implements them, and
422//! [`worst_case_node_count`] under [`largest_page_sizes`] is where every node count here
423//! comes from. `every_document_this_source_sends_stays_under_githubs_node_limit`, in
424//! `tests/node_count.rs`, recomputes every document in [`graphql::DOCUMENTS`] from that
425//! same text on every run and fails naming any that reaches the limit — so a connection
426//! added to a shared fragment is caught there rather than by GitHub.
427//!
428//! What decides those counts is the page sizes: [`MAX_PAGE_SIZE`] on the outer page,
429//! `NESTED_PAGE_SIZE` on the connections hanging off one item, and
430//! `BOARD_ITEMS_PAGE_SIZE` on the page of an issue's board memberships a read carries.
431//! `$nestedFirst` is spent twice down one path of a board read, so that constant is
432//! effectively squared there, which is why it is the one the limit is most sensitive to.
433//! `BOARD_ITEMS_PAGE_SIZE` is small for a reason of its own, recorded beside it: what a
434//! page of memberships misses is recovered by one further read rather than refused, so it
435//! buys a bound every read pays for at the price of a request only a multi-board issue
436//! pays.
437//!
438//! **`nodeCount` and `cost` are two numbers against two limits, and both are computed
439//! offline here — per document, one document at a time.** `nodeCount` is the one above: the
440//! most nodes one query may return, checked per query and bounded by [`NODE_COUNT_LIMIT`].
441//! `cost` is rate-limit points, metered per hour across everything one credential does; it
442//! is what the two limiters [`Limiter`] tells apart meter, and a document under
443//! [`NODE_COUNT_LIMIT`] still says nothing about its price. [`worst_case_point_cost`] is
444//! that second number, and `tests/point_cost.rs` pins every document in
445//! [`graphql::DOCUMENTS`] at what it costs — there being no per-call point ceiling to hold
446//! one under, the pin itself is the check. The credentialed lane reconciles both figures
447//! against GitHub's own, off a probe it already sends.
448//!
449//! **What is pinned that way is a per-document price and never a session's.** The record in
450//! `session-cost.md` measures the two quantities a whole session can be counted in offline —
451//! **requests** and **worst-case nodes** — and neither is points. What one whole session
452//! consumes of the hourly point allowance is observable only from a credentialed run's own
453//! `x-ratelimit-*` headers, which is what [`accounting`] fills its per-budget figures from
454//! and what `tests/live.rs` prints at the end of every run.
455//!
456//! [node-limits]: https://docs.github.com/en/graphql/overview/rate-limits-and-node-limits-for-the-graphql-api
457//!
458//! **Where a read-after-write guarantee comes from, since neither of GitHub's two
459//! enumerations of a board can supply one alone.** Resolving a node id is strongly
460//! consistent, so a read by id and a project's own sub-issues are already current. The
461//! other two are not, and they are behind by different amounts and in different directions:
462//!
463//! - GitHub's **issue search** is an index and answers a write made moments ago with the
464//! value from before it — usually for a second or two.
465//! - **`ProjectV2.items`** is a projection GitHub rebuilds behind the write, and an item put
466//! on a board with `addProjectV2ItemById` can be **absent** from it — not present with its
467//! content withheld, absent, with the connection walked to its own `hasNextPage: false` —
468//! for *minutes*, while `Issue.projectItems` names the same membership at once.
469//!
470//! That second one is a measurement rather than a caution. This repository's own
471//! credentialed journey writes a project and waits for the board to report it, then writes a
472//! task and waits for the same thing seconds later on the same board: the project wait is
473//! answered through the search and converged in two or three attempts in each of three runs,
474//! and the task wait is answered through `ProjectV2.items` and converged in none of them
475//! inside thirty. Separately, an item added to a second and larger board was read back by
476//! `Issue.projectItems` on that board's own id while every one of that connection's nine
477//! pages, walked to exhaustion nine minutes after the add, did not name it. Reading a board
478//! through the lagging one alone is what had a board read deny an issue that had certainly
479//! landed on it.
480//!
481//! So [`GitHubProjectsSource::board`] is the **union** of both — each search result still
482//! admitted only on this board's own strongly-consistent `Issue.projectItems`, and neither
483//! enumeration dropped, because only `ProjectV2.items` lists a board draft and only the
484//! search reports what the projection is behind on. What closes the last
485//! gap, the one where both are behind, is [`GitHubProjectsSource::created`]: every read this
486//! source answers is completed with what this process itself wrote, so an item created
487//! seconds ago is reported whether or not GitHub has caught up. Nothing else is remembered,
488//! nothing is written down, and the record dies with the process. **A wait that has to
489//! observe GitHub's own data cannot be answered from that record** — which is why the
490//! credentialed journey asks through a source built afresh, and why the union above rather
491//! than a longer wait is what makes such a wait converge.
492//!
493//! **A narrowed read is the same bargain, stated for each of the three predicates it
494//! answers.** A read carrying a text, metadata or origin predicate asks GitHub's index rather
495//! than walking the board, and every such answer is completed with what this process wrote —
496//! its [`created`](GitHubProjectsSource::created) record and every existing item it wrote,
497//! each filtered by the same predicates as the rest — so an item this command wrote a moment
498//! ago is returned by a query that matches it whether or not the index has caught up. An item
499//! a caller holds the id of is read by that id, with `node(id:)`, which is strongly
500//! consistent. What is left is stated rather than papered over:
501//!
502//! | Read | Finds | Behind by |
503//! | --- | --- | --- |
504//! | text, metadata | the issue search for the phrases | what another process wrote in the last second or two, until GitHub indexes it |
505//! | origin, first read | the board's field filter over the origin field — every carrier, whichever release wrote it | what `ProjectV2.items` is behind on, which the measurements above put in minutes |
506//! | origin, second read | the issue search for the id in the body, where a write of this release mirrors it | a second or two, as any search |
507//! | origin, third read | this process's own writes | nothing |
508//!
509//! So an origin carrier another process added within the last second or two, before either
510//! index has it, can be missing from an origin query, and one written by the release before
511//! this one — its origin in the field alone — can be missing for as long as the board's own
512//! item connection is behind on it. A copy that must not duplicate its own earlier write
513//! relies on the link it records, not on either index. **A board draft is not an issue**, so
514//! a draft is never returned by a text, metadata or origin query, whatever it holds: no search
515//! lists one, the origin lookup drops any the board's own field filter names, and one this
516//! process wrote is not added back either.
517//!
518//! **The origin lives in the board field, and the body holds a mirror of it.** A write that
519//! carries an origin writes it to the `onetaskgraph.origin` text field and also into the
520//! body's metadata slot, so the issue search can find it in seconds. The field is
521//! authoritative: this source reads an item's origin from the field alone, so a slot that
522//! disagrees with it, or holds one where the field holds none, is never read as a second
523//! origin — and the release before this one reads the slot, drops that key's copy for the
524//! field's, and sees the same one origin.
525//!
526//! Filtering happens before paging, so a page of a filtered result is a page of the
527//! survivors rather than the survivors of a page. Label matching and the substring rule a
528//! text candidate is confirmed by answer the same question the same way the local Markdown
529//! source's do; which candidates a text search has to confirm is GitHub's word match, which
530//! is the one place the two sources can answer the same text differently.
531//!
532//! <!-- llmlint: ignore[contracts_have_one_source_or_a_drift_gate] The declaration itself
533//! has one source, `capabilities`, and the note above is the reasoning behind it rather
534//! than a second copy of it: without the three facts recorded here a reader takes the
535//! uniform `Native` for a lie and reverts it. The drift gate on the declaration is this
536//! crate's own capabilities test, which pins every field of it against a fully spelled-out
537//! `Capabilities` literal — a struct with no `Default`, so a field added to the contract
538//! fails to compile there rather than going unasserted. -->
539//! The fixture-server tests above run wherever this crate is selected; the credentialed
540//! lane runs in the same required check, beside them, and can fail it — it verifies the
541//! current schema, then drives every field of the table above against the real board. It builds its own fixture there — two projects, one task filed under each,
542//! one filed under neither, a label on one of the three and a closed status on another —
543//! because that shape is what tells an honoured predicate from an ignored one: a board
544//! holding a single project answers a project filter the same way whether or not this
545//! source applies it, which is exactly how the defect above went unseen.
546//!
547//! That lane writes only to the board `GH_PROJECTS_OWNER` and `GH_PROJECTS_NUMBER` name,
548//! and the scratch repository `GH_PROJECTS_REPOSITORY` names:
549//! `nickderobertis/onetaskgraph-live-scratch`. It refuses the core repository before any
550//! session or request, independently of the live demand. Fix that variable in the machine's
551//! onetaskgraph `secrets.env` or the environment it pushes from, such as ai-orchestrator's
552//! `.env`. The targets are declared once in `onetaskgraph_github_live`, the lane's own
553//! policy crate; absent credentials or nominations otherwise skip.
554//!
555//! GitHub Actions artifacts carry `ci-<run id>-<attempt>-<micros>`, naming their writing
556//! run and attempt; invalid Actions identity refuses before writing. Other runs retain
557//! `<host>-<process>-<micros>`. Own cleanup matches the whole stamp. Machine residue stays
558//! the owning machine's lock sweep's; Linear always keeps that form and is unaffected.
559//! The hourly janitor is cleanup, never a test lane: scratch CI residue is removed only
560//! after its run reads back as `completed`, immediately before the listed-artifact batch.
561//! Failed or incomplete ownership reads preserve residue. A 24-hour waiting period
562//! is a margin, never the ownership authorisation. Cleanup leaves machine stamps
563//! and the board's `onetaskgraph.origin` field untouched.
564//!
565//! # What a session of requests costs, and where the report is
566//!
567//! This source records **every** request it sends into [`accounting::Accounting`], at
568//! `send_once` — the one place a request leaves this crate, which is why a read path added
569//! later is counted without anybody remembering to count it. That is the whole of what this
570//! crate adds to the arrangement; [`accounting`] is where what a record carries, how a
571//! session's spend is arrived at, and what it deliberately does not know are set out.
572//!
573//! What one whole session of the live journey costs, counted that way against this crate's
574//! loopback fixture board, is written down in `session-cost.md` beside this crate — with the
575//! reduction it came out of, and with what it does and does not say about rate-limit points.
576//!
577//! [`GitHubProjectsSource::accounting`] is the read: a snapshot to hold and compare, which
578//! [`accounting::Session::report`] renders the session report from. It is on the ordinary
579//! code path — no environment variable, no feature, no build configuration — because an
580//! instrument nobody switches on measures nothing, and
581//! [`Plugin::build_recording_into`] is how a caller making its own calls beside this
582//! source's counts the whole session rather than this source's share. The credentialed lane
583//! in `tests/live.rs` does exactly that, and prints the report at the end of every run,
584//! passed or failed.
585//!
586//! **A live session refuses to start unless the account can afford it.** Before it does any
587//! of the work it exists to do, the journey makes one request — `GET /rate_limit`, which
588//! GitHub documents as not counting against the REST rate limit and which answers both of
589//! its budgets at once — and starts only if, for each of them, what remains minus this
590//! session's estimated cost is still at least
591//! `onetaskgraph_live::RETAINED_BUFFER` — twenty per cent — of that budget's whole
592//! allowance. A session that cannot **declines**: it did not run, so it is
593//! neither a pass nor a failing assertion, and it says which budget was short, that budget's
594//! limit, what remained, the estimate, the buffer and when it resets — then stops, without
595//! waiting for the budget to come back. The estimate is derived offline from
596//! `tests/fixtures/session-cost.txt` and a cost model stated in `tests/journey/budget.rs`,
597//! which is also where the published rule that model rests on is cited; the accounting
598//! above records the gate's own read like any other request, and
599//! [`accounting::Session::report`] prints the estimate beside what the session really spent.
600//!
601//! **GitHub is the authority on both of its own numbers, and the credentialed lane goes and
602//! asks it.** Everything above computes `nodeCount` and `cost` offline from a document's own
603//! text, which is what lets it run on every platform and on a pull request from a fork with
604//! no credential — and that is what actually stops a regression merging. But an offline
605//! arithmetic can only ever agree with itself: if GitHub changes its rules, this workspace
606//! goes on computing the old answer and nothing notices. So `tests/live.rs` reconciles them.
607//! GitHub's schema exposes `rateLimit(dryRun: true)`, whose `nodeCount` is *"the maximum
608//! number of nodes this query may return"* and whose `cost` is what that document would
609//! spend, both for a document **without executing it**, and the lane asks it for every query
610//! document this source sends, under the largest bindings this source sends, and fails when
611//! GitHub's figure and [`worst_case_node_count`] or [`worst_case_point_cost`] disagree. A
612//! mutation is skipped, because `rateLimit` is a field of `Query` and cannot be asked about
613//! one; the offline pins still cover it. It records what those calls reported about the
614//! account's own allowance, because whether asking is free is a thing to observe rather than
615//! to assume. Two quantities, not one: [`NODE_COUNT_LIMIT`] bounds `nodeCount` per query,
616//! and `cost` is metered against an hourly allowance the accounting above reads off a
617//! credentialed run's own response headers.
618//!
619//! **GitHub has two rate limiters and this source is refused by both, so nothing here
620//! treats them as one thing.** The primary budget is the hourly allowance `gh api
621//! rate_limit` reports; the secondary limiter is a burst limiter over content-generating
622//! requests, and *nothing* reports it. Which one refused decides the operator's next step,
623//! so [`Limiter`] is a type rather than a detail, and it is what [`MIN_MUTATION_INTERVAL_MS`],
624//! [`GitHubProjectsSource::board_cache`] and [`GitHubProjectsSource::graphql`] each answer
625//! one part of.
626#![deny(missing_docs)]
627
628use std::collections::BTreeMap;
629use std::sync::{Arc, Mutex};
630use std::time::Duration;
631
632use chrono::{DateTime, Utc};
633use onetaskgraph_plugin_api::{
634 Capabilities, Classification, ClosedAs, Comment, CommentBody, CommentOrigin, Completion,
635 ContradictedItem, Coverage, Cursor, DependencyEdge, DependencyEndpoint, DependencyKind,
636 DependencySupport, Designator, Direction, Document, DocumentQuery, DocumentVersionCoverage,
637 Health, HeldTask, IssueHolding, IssueRef, ItemKind, ItemWrite, Label, LabelFilter, Location,
638 MetadataKey, Metering, NativeId, NewComment, Page, PageRequest, Priority, Project,
639 ProjectFilter, ProjectQuery, ProjectVersionCoverage, Repository, SecretResolver, SharedClock,
640 SourceError, SourceName, SourcePlugin, Status, StatusCategory, StatusContradiction,
641 StatusMapping, Support, Task, TaskDetailRead, TaskQuery, TaskRef, TaskSource, TaskUpdate,
642 TaskUpdateOutcome, TextFields, TextQuery, UnmappedStatus, UpdatedField, VersionCoverage,
643 WaitCause, WriteJournal, WriteSupport, system_clock,
644};
645use reqwest::{Client, StatusCode, Url};
646use schemars::{Schema, schema_for};
647use secrecy::{ExposeSecret, SecretString};
648use serde::{Deserialize, Serialize};
649use serde_json::{Value, json};
650
651pub mod accounting;
652mod assets;
653pub mod change_token;
654mod expected;
655pub use expected::{CONTEXT_VERSION, WHOLE_MEMBERS, context_shapes};
656mod pacing;
657mod projection;
658mod visibility;
659
660use accounting::Accounting;
661
662/// The registry name for this plugin.
663pub const KIND: &str = "github-projects";
664/// GitHub's maximum connection page size.
665pub const MAX_PAGE_SIZE: u32 = 100;
666/// Every page of a board-scoped narrowing search: 20 rows, one point of SEARCH_ISSUES, the
667/// most one point buys. GitHub prices that document by rows, so pages of 20 cost what pages
668/// of 100 cost per row, and a page of fewer than 20 costs the same one point.
669pub const SEARCH_PAGE_SIZE: u32 = 20;
670/// How many items one [`graphql::ISSUE_DETAILS`] request reads, each with the first page of
671/// its comments: the largest batch the node-count model prices at one point.
672///
673/// Each aliased item is resolved once, and what GitHub charges for it is the connections
674/// under it — its labels, its page of board memberships, the field values of each of those
675/// three memberships, and its comments: six requests' worth of the aggregate GitHub divides
676/// by a hundred and rounds. Twenty-four items come to 144, which rounds to one point;
677/// twenty-five come to 150, which rounds to two. `tests/point_cost.rs` prices the document at
678/// one point and fails if one item more would still be priced at one.
679pub const DETAIL_BATCH: usize = 24;
680/// How many issue references one [`graphql::ISSUES_BY_REFERENCE`] request reads.
681///
682/// Each reference asks GitHub for one connection — a page of its board memberships, each with
683/// its `Status` value read by name rather than through a connection of field values — so fifty
684/// come to fifty requests' worth of the aggregate GitHub divides by a hundred, which is its
685/// one-point minimum, and the batch a landing's closures are read in costs one point per fifty
686/// issues per source. `tests/point_cost.rs` prices the document at one point.
687pub const REFERENCE_BATCH: usize = 50;
688/// How many items one [`graphql::ISSUE_COPY_DETAILS`] request reads: each with the first page
689/// of its comments and its `blockedBy`, the largest batch the node-count model prices at one
690/// point.
691///
692/// The six requests' worth an item of [`DETAIL_BATCH`] costs, and its `blockedBy` connection
693/// besides: seven. Twenty-one items come to 147, which rounds to one point; twenty-two come to
694/// 154, which rounds to two. `tests/point_cost.rs` prices the document at one point and fails
695/// if one item more would still be priced at one.
696pub const COPY_DETAIL_BATCH: usize = 21;
697/// How many mutations one aliased request of a copy's creates, closes and updates carries:
698/// [`graphql::CREATE_ISSUES`], [`graphql::CLOSE_ISSUES`] and [`graphql::UPDATE_ISSUES`].
699///
700/// Eleven is what GitHub was seen to accept in one request — the timing spike
701/// (`board-copy-speed-2026-10-18-spike-copy-timing-report`) sent eleven aliased mutations of four
702/// kinds, three `createIssue`, three `addProjectV2ItemById`, three field updates, an
703/// `addSubIssue` and an `addBlockedBy`, and every one ran — and no more was established.
704pub const MUTATION_BATCH: usize = 11;
705/// How many aliased writes one request of a copy's board field values and blocked-by edges
706/// carries: [`graphql::WRITE_FIELD_VALUES`], [`graphql::ADD_BLOCKED_BYS`] and, for a request
707/// holding both kinds, [`graphql::WRITE_FIELDS_AND_EDGES`].
708///
709/// Nineteen is the most aliases the feasibility spike
710/// (`board-copy-gaps-2026-10-24-spike-feasibility-report`) sent in one mutation request and
711/// saw GitHub price at the one point every mutation request costs; no larger request was
712/// established, so a copy holding more is split.
713pub const WRITE_BATCH: usize = 19;
714/// How many issues one [`graphql::DELETE_ISSUES`] request deletes: one request of eleven
715/// aliased `deleteIssue` met `RESOURCE_LIMITS_EXCEEDED` from the eighth alias on, and batches
716/// of five succeeded (the feasibility spike's undo-batching run).
717pub const DELETE_BATCH: usize = 5;
718/// How many origins one [`graphql::ORIGIN_LOOKUPS`] request asks about: the nine records of a
719/// plan and the project's document, and no more, since each carries a page of the board's field
720/// filter and one of the issue search.
721pub const ORIGIN_BATCH: usize = 10;
722
723/// The most nodes any one document this source sends may be asked to return.
724///
725/// GitHub's own published per-query ceiling, taken from
726/// [`github_graphql_node_count::NODE_LIMIT`] rather than written out again here, so this
727/// workspace cannot hold a stale copy of somebody else's number. A query above it is
728/// **refused before it is executed**, whoever is asking and whatever board they are
729/// asking about — so this is a bound on the documents rather than a budget that runs out.
730///
731/// This is `nodeCount`, the maximum number of nodes *one query may return*. It is not
732/// `cost`, the rate-limit points a call spends against an hourly allowance shared by
733/// everything the credential does — two numbers against two limits, and this constant
734/// bounds only the first. The second is computed offline too, per document:
735/// [`worst_case_point_cost`], pinned for every document in [`graphql::DOCUMENTS`] by
736/// `tests/point_cost.rs`, and reconciled against GitHub's own `cost` by the credentialed
737/// lane. There is no constant like this one to hold a price under, because points are an
738/// hourly allowance rather than a per-call bound.
739///
740/// Neither is a session's price. What `session-cost.md` records of a whole session is its
741/// **requests** and its **worst-case nodes**; what a whole session spends in points is
742/// reported only by a credentialed run's own `x-ratelimit-*` headers, through
743/// [`accounting`]. The module section on the three ways this source reaches an item says how
744/// the count is arrived at, and which of the page sizes below decide it.
745pub const NODE_COUNT_LIMIT: u64 = github_graphql_node_count::NODE_LIMIT;
746
747/// Nested connection size for the connections that hang off one item.
748///
749/// It multiplies through every document that reaches an item under a page — the count
750/// rules multiply down a nested path — so it is the constant [`NODE_COUNT_LIMIT`] is most
751/// sensitive to. `tests/node_count.rs` is what holds the pair together: it recomputes
752/// every document under these constants and fails naming any that reaches the limit, so
753/// raising this is caught there rather than by GitHub.
754const NESTED_PAGE_SIZE: u32 = 50;
755/// How many of one issue's board memberships are read when an issue is reached directly.
756///
757/// An issue reached through a search or through its own node id carries its board half in
758/// `Issue.projectItems`, and only the entry for *this* board is read. This connection sits
759/// under a page of issues, so every point of it multiplies through the whole document and
760/// is paid for whether or not any issue is on a second board — which is why it is
761/// deliberately far smaller than [`NESTED_PAGE_SIZE`].
762///
763/// **Three, because what a page misses is now recovered rather than refused**, and the
764/// recovery is what the value is chosen against. An issue whose entry for this board sits
765/// past this page costs one further request — [`graphql::ISSUE_BOARD_ITEMS`], resumed from
766/// that page's own cursor — so the value trades a bound every read pays for a request only
767/// a multi-board issue pays. At one, a deployment whose issues commonly sit on two or more
768/// boards would pay that request *per issue*, which is order N against the one page per
769/// hundred issues a read costs today. At three it is only reached by an issue on four or
770/// more boards at once, which keeps the recovery path exceptional rather than routine for
771/// a plausible deployment.
772const BOARD_ITEMS_PAGE_SIZE: u32 = 3;
773/// How many carriers of one copy origin one page of [`graphql::ORIGIN_LOOKUP`] asks each of
774/// its two connections for.
775///
776/// An origin names one item, so the answer an origin lookup expects is one carrier, and a
777/// second is a duplicate a copy already takes the first of. Both connections are walked to
778/// exhaustion whatever this is, so it decides how many requests an unusual answer costs and
779/// never what the answer is. It is small because every point of it is paid on every lookup,
780/// and a copy makes one lookup per item it has no link for: at three, ten lookups cost fewer
781/// worst-case nodes than the one whole-board read they replaced.
782const ORIGIN_PAGE_SIZE: u32 = 3;
783/// How many carriers of each origin the batched [`graphql::ORIGIN_LOOKUPS`] asks each of its two
784/// connections for: one, the one carrier an origin names.
785///
786/// A second carrier still shows: the page then says there is more, and that origin is left to
787/// its own [`graphql::ORIGIN_LOOKUP`], which walks every page at `ORIGIN_PAGE_SIZE` — so a
788/// duplicate is found exactly as before, by the lookup that reads all of them. The feasibility
789/// spike (`board-copy-gaps-2026-10-24-spike-feasibility-report`) priced the batch of ten
790/// origins at two points with a first page of three and at one with a first page of one.
791const ORIGIN_LOOKUPS_PAGE_SIZE: u32 = 1;
792
793/// How many of one project's sub-issues one [`graphql::SUB_ISSUES`] page asks for: the most
794/// one point buys, since every row carries six connections GitHub charges for once per row.
795/// `session-cost.md` derives it; `tests/point_cost.rs` prices the document at one point and
796/// fails if one row more would still be priced at one.
797pub const MEMBER_PAGE_SIZE: u32 = 23;
798
799pub use github_graphql_node_count::{NodeCountError, Variables};
800
801/// The largest value this source can bind to each page-size variable its documents name.
802///
803/// Every `first:` in [`graphql`] reads one of these six, and each is capped at the
804/// constant above it wherever a caller's own limit could reach it — `$first` at
805/// [`MAX_PAGE_SIZE`], `$nestedFirst` at `NESTED_PAGE_SIZE`, `$boardItems` at
806/// `BOARD_ITEMS_PAGE_SIZE`, `$originFirst` at `ORIGIN_PAGE_SIZE`, `$lookupFirst` at
807/// `ORIGIN_LOOKUPS_PAGE_SIZE`, `$memberFirst` at [`MEMBER_PAGE_SIZE`]. So this is the worst case a caller can drive this source to,
808/// not one configuration of it, which is what makes a bound computed under it a bound on
809/// every read.
810pub fn largest_page_sizes() -> Variables {
811 Variables::from([
812 ("first".to_owned(), MAX_PAGE_SIZE),
813 ("nestedFirst".to_owned(), NESTED_PAGE_SIZE),
814 ("boardItems".to_owned(), BOARD_ITEMS_PAGE_SIZE),
815 ("originFirst".to_owned(), ORIGIN_PAGE_SIZE),
816 ("lookupFirst".to_owned(), ORIGIN_LOOKUPS_PAGE_SIZE),
817 ("memberFirst".to_owned(), MEMBER_PAGE_SIZE),
818 ])
819}
820
821/// The most nodes `document` could be asked to return, by GitHub's published rules.
822///
823/// Computed offline from the document's own text under [`largest_page_sizes`] — no
824/// network, no credential and no schema — by
825/// [`github_graphql_node_count::node_count`], which is where the rules themselves live.
826/// A document at or above [`NODE_COUNT_LIMIT`] is one GitHub refuses before executing, so
827/// this is what a check holds every document in [`graphql::DOCUMENTS`] below.
828///
829/// # Errors
830///
831/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
832/// no single operation, or binds a page size this source does not name — each of which is
833/// a defect in the document rather than a number.
834pub fn worst_case_node_count(document: &str) -> Result<u64, NodeCountError> {
835 node_count(document, &largest_page_sizes())
836}
837
838/// The most rate-limit points one call of `document` could spend, by GitHub's published
839/// rules.
840///
841/// Computed offline from the document's own text under [`largest_page_sizes`] — no
842/// network, no credential and no schema — by
843/// [`github_graphql_node_count::point_cost`], which is where the rules themselves live.
844/// This is `cost`, metered **per hour** against the allowance one credential shares across
845/// everything it does; it is not `nodeCount`, which is [`worst_case_node_count`] and is
846/// bounded per query by [`NODE_COUNT_LIMIT`]. There is no per-call ceiling to hold this
847/// under, so what `tests/point_cost.rs` does with it is pin every document in
848/// [`graphql::DOCUMENTS`] at what it costs, and the credentialed lane reconciles those
849/// figures against GitHub's own reported `cost`.
850///
851/// # Errors
852///
853/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
854/// no single operation, or binds a page size this source does not name — each of which is
855/// a defect in the document rather than a number.
856pub fn worst_case_point_cost(document: &str) -> Result<u64, NodeCountError> {
857 github_graphql_node_count::point_cost(document, &largest_page_sizes())
858}
859
860/// The most nodes `document` could be asked to return under `variables`.
861///
862/// [`worst_case_node_count`] is this under [`largest_page_sizes`], and the accounting in
863/// [`accounting`] is this under the bindings one request really sent — one spelling of the
864/// calculation, so a bound checked offline and a cost recorded at run time cannot come to
865/// disagree. The rules themselves live in [`github_graphql_node_count::node_count`].
866///
867/// # Errors
868///
869/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
870/// no single operation, or binds a page size `variables` does not name.
871pub fn node_count(document: &str, variables: &Variables) -> Result<u64, NodeCountError> {
872 github_graphql_node_count::node_count(document, variables)
873}
874
875/// The issue-title prefix that makes a board issue a document.
876///
877/// A GitHub Projects board has no document type — it holds issues — so the discriminator
878/// is the title, and this is the whole of it: an issue whose title begins with these bytes
879/// is a document and every other issue is the task or project the sub-issue rule makes it.
880///
881/// It is spelled **once**, here, and read rather than restated everywhere else — including
882/// by the shared journeys, which take it from this constant so a board fixture cannot
883/// drift from what this source reads. `docs/metadata.md` records the two consequences that
884/// are not obvious from the bytes: the reported title has this prefix taken off, exactly
885/// as the body's metadata slot is taken off `content`, and this prefix is read *before*
886/// the sub-issue rule, so a design issue with no sub-issues is never an empty project.
887pub const DESIGN_TITLE_PREFIX: &str = "DESIGN: ";
888
889/// Exact GraphQL query documents issued by this plugin.
890///
891/// Keeping the production documents here lets the pinned-schema test validate the same
892/// bytes that are sent to GitHub, rather than a test-only copy which could drift
893/// independently. [`STATUS_OPTIONS_UPDATE`] is the sole document that may rewrite a board
894/// field, and its guarded caller always supplies the complete existing option set with ids.
895pub mod graphql {
896 /// The board half of one item: the field values every document here reads it from.
897 ///
898 /// A macro for the same reason [`board_issue!`] below is one, a level further in. This
899 /// selection is needed by that fragment, by [`BOARD`] under the board's own `items`,
900 /// and by [`ISSUE_BOARD_ITEMS`] under a membership walk — and all three have to produce
901 /// *the same value*, because
902 /// [`GitHubProjectsSource::resolve`](super::GitHubProjectsSource) reads them through
903 /// one path. Three spellings of it is what would drift, so there is one.
904 ///
905 /// The `Status` option and this source's own origin text field are the whole of it. It
906 /// selects no `ProjectV2ItemFieldLabelValue`: GitHub derives that field from the item's
907 /// content, so it holds nothing the content's own `labels` do not already say, and it
908 /// would sit a label connection two page sizes deep.
909 macro_rules! board_item_values {
910 () => {
911 r#"fieldValues(first:$nestedFirst){nodes{
912 ... on ProjectV2ItemFieldSingleSelectValue{name field{
913 ... on ProjectV2SingleSelectField{id name options{id name}}
914 }}
915 ... on ProjectV2ItemFieldTextValue{text field{... on ProjectV2Field{id name}}}
916 }pageInfo{hasNextPage}}"#
917 };
918 }
919
920 /// The variable every document reading an issue declares for the relationship events its
921 /// change token counts, with its value as the default, so no caller binds it.
922 ///
923 /// A variable rather than a literal list because every argument a production document
924 /// passes is a declared variable, which `tests/schema.rs` holds them to, and every member of
925 /// its default to the pinned enum. Its default is the six event types GitHub records a
926 /// relationship added or removed with: a dependency on either end, as `blockedBy` and
927 /// `blocking`, so one a person makes from the far end moves this end's count too; and a
928 /// project membership on the task, as its parent, which GitHub records there whichever
929 /// issue the move was made from. A sub-issue's events on its parent are not counted: a
930 /// project carries no task token, and a copy writes nothing of a task its own sub-issues
931 /// decide. [`super::change_token`] says why the count is in the token.
932 macro_rules! relationship_events_variable {
933 () => {
934 ",$relationshipEvents:[IssueTimelineItemsItemType!]=[BLOCKED_BY_ADDED_EVENT,BLOCKED_BY_REMOVED_EVENT,BLOCKING_ADDED_EVENT,BLOCKING_REMOVED_EVENT,PARENT_ISSUE_ADDED_EVENT,PARENT_ISSUE_REMOVED_EVENT]"
935 };
936 }
937
938 /// Everything this source reads about one issue, wherever it reaches that issue.
939 ///
940 /// A macro rather than a constant so the three documents below can `concat!` it: one
941 /// spelling of these fields is what makes an issue read through the board-scoped
942 /// search, through its own node id, and through its project's sub-issue relationship
943 /// resolve to *the same* item, which is the whole of what
944 /// [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource) relies on.
945 ///
946 /// `projectItems` is what carries the board half of an issue: the board item's own id
947 /// and the [`board_item_values!`] above — the `Status` option and this source's origin
948 /// text field — that a `ProjectV2.items` read used to carry. It is asked for on the
949 /// issue rather than on the board, which is what makes the cost of a read proportional
950 /// to what was asked for instead of to the board's size.
951 ///
952 /// It carries a *page* of that connection, at `BOARD_ITEMS_PAGE_SIZE`, and its
953 /// `endCursor` is what [`ISSUE_BOARD_ITEMS`] resumes from when this board's entry is
954 /// not on that page: a page here is where the search for the entry starts rather than
955 /// where it ends.
956 ///
957 /// It does **not** select the board's `Labels` field value, and that is the whole of
958 /// what keeps the three documents below under [`NODE_COUNT_LIMIT`](super::NODE_COUNT_LIMIT):
959 /// a label connection there sits under `fieldValues` under `projectItems` under a page
960 /// of issues, spending `$nestedFirst` twice down one path, and took
961 /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] to 2,556,100 nodes against a limit of 500,000.
962 /// No label is lost — this is a fragment `on Issue`, whose own `labels` are selected
963 /// above, and that connection is where every label this source reports comes from. No
964 /// document in this module selects the board field any longer, [`BOARD`] included; the
965 /// module documentation records why nothing it could have held is lost.
966 macro_rules! board_issue {
967 () => {
968 concat!(
969 r#" fragment BoardIssue on Issue{__typename id number title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total} commentCount:comments{totalCount} relationshipEvents:timelineItems(itemTypes:$relationshipEvents){totalCount}
970 labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}
971 projectItems(first:$boardItems){nodes{id updatedAt project{id number}
972 "#,
973 board_item_values!(),
974 r#"}pageInfo{hasNextPage endCursor}}}"#
975 )
976 };
977 }
978
979 /// One page of a repository's issues, newest first, each with when it was created and its
980 /// body: what a retried copy reads to find the issue its earlier attempt created, by the
981 /// create key in that body's metadata slot — never through search, whose index lags.
982 pub const ISSUES_BY_CREATION: &str = "query($owner:String!,$name:String!,$first:Int!,$after:String){repository(owner:$owner,name:$name){issues(first:$first,after:$after,orderBy:{field:CREATED_AT,direction:DESC}){nodes{id createdAt body}pageInfo{hasNextPage endCursor}}}}";
983 /// Every issue of one board, found by a search scoped to that board.
984 ///
985 /// This is how the projects a board holds are listed, and it selects no `items`
986 /// connection on `ProjectV2`: the board is a *qualifier of the search* rather than a
987 /// container walked page by page, so nothing nested inside a board item is paid for.
988 /// Which of the issues it returns is a project is then read off `parent` — GitHub
989 /// accepts `-has:parent` as a search qualifier and silently ignores it, so the
990 /// discriminator has to be applied to the field, which is a scalar on the issue and
991 /// costs nothing.
992 pub const SEARCH_ISSUES: &str = concat!(
993 r#"query($search:String!,$type:SearchType!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
994 relationship_events_variable!(),
995 r#"){
996 search(query:$search,type:$type,first:$first,after:$after){
997 pageInfo{hasNextPage endCursor}
998 nodes{__typename ...BoardIssue}
999 }
1000 }"#,
1001 board_issue!()
1002 );
1003
1004 /// What a dependency read selects of each far end: enough to say which kind of item it
1005 /// is, its body included for the kind marker.
1006 macro_rules! related_issue {
1007 () => {
1008 " fragment Related on Issue{id title body parent{id} subIssuesSummary{total}}"
1009 };
1010 }
1011
1012 /// One issue by its own node id, which is what a qualified id names here — with what a
1013 /// write of it needs and the issue does not carry in `board_issue!`: the field
1014 /// definitions of the boards it sits on, and the far ends of its `blockedBy`.
1015 ///
1016 /// Strongly consistent, unlike the search above: GitHub's issue search is an index and
1017 /// answers a write made moments ago with the value from before it, and resolving a node
1018 /// id does not.
1019 ///
1020 /// **Why those two ride here and not on the fragment.** A copy or an update of an item
1021 /// reads it by its own id, and with them that one read answers everything the write
1022 /// needs: which option ids the board's `Status` and `Priority` fields hold — so no
1023 /// [`BOARD_FIELDS`] — and which issues block it, with each one's kind — so no
1024 /// [`ISSUE_DEPENDENCIES`]. On `board_issue!` they would sit under the hundred-issue
1025 /// pages of [`SEARCH_ISSUES`] and [`SUB_ISSUES`], multiplying both documents' price. Here
1026 /// they sit under one item, and this read is still one point.
1027 pub const ISSUE: &str = concat!(
1028 r#"query($id:ID!,$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1029 relationship_events_variable!(),
1030 r#"){
1031 node(id:$id){__typename ...BoardIssue ... on Issue{
1032 boards:projectItems(first:$boardItems){nodes{project{id number fields(first:$nestedFirst){nodes{
1033 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1034 ... on ProjectV2Field{__typename id name dataType}
1035 }pageInfo{hasNextPage}}}}}
1036 blockedBy(first:$first){nodes{...Related}pageInfo{hasNextPage endCursor}}
1037 }}
1038 }"#,
1039 board_issue!(),
1040 related_issue!()
1041 );
1042
1043 /// One document by its own node id, as [`ISSUE`] reads one, with the project it is filed
1044 /// under and that project's members beside it: the project issue whole, and a page of its
1045 /// sub-issues, each as [`SUB_ISSUES`] carries one but with no page of what blocks it.
1046 ///
1047 /// What a guarded write of a document's metadata reads, and what reading a document by its
1048 /// id is: a write to a board the boundary screens is held to who may read the document,
1049 /// which is the project's and every member's classification as much as its own, and here
1050 /// the one request that holds the version the write is compared against answers all of it.
1051 /// Its members carry no edges, which nothing classifying them reads.
1052 pub const DOCUMENT_IN_PROJECT: &str = concat!(
1053 r#"query($id:ID!,$first:Int!,$memberFirst:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1054 relationship_events_variable!(),
1055 r#"){
1056 node(id:$id){__typename ...BoardIssue ... on Issue{
1057 boards:projectItems(first:$boardItems){nodes{project{id number fields(first:$nestedFirst){nodes{
1058 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1059 ... on ProjectV2Field{__typename id name dataType}
1060 }pageInfo{hasNextPage}}}}}
1061 blockedBy(first:$first){nodes{...Related}pageInfo{hasNextPage endCursor}}
1062 project:parent{__typename ...BoardIssue
1063 subIssues(first:$memberFirst){pageInfo{hasNextPage endCursor} nodes{__typename ...BoardIssue}}
1064 }
1065 }}
1066 }"#,
1067 board_issue!(),
1068 related_issue!()
1069 );
1070
1071 /// One project's tasks: the sub-issues of the issue that project is, each with a page of
1072 /// what blocks it.
1073 ///
1074 /// Paged at `$memberFirst` — [`MEMBER_PAGE_SIZE`](super::MEMBER_PAGE_SIZE), the most rows
1075 /// one point buys — rather than at `$first`, because every row carries six connections and
1076 /// a hundred-row page cost six points to list a project of ten.
1077 ///
1078 /// The work this costs is the project's own size. Nothing about it grows as the board
1079 /// gains projects, or as those projects gain tasks.
1080 ///
1081 /// **Why `blockedBy` rides here and on no other page of issues.** What reads a project's
1082 /// tasks reads their edges next — `project graph` draws them, a copy carries them — and
1083 /// without them here that is one [`ISSUE_DEPENDENCIES`] per task, so the requests a graph
1084 /// costs grow with its tasks rather than with the pages of them. Carried here, a task
1085 /// blocked by no more than `$nestedFirst` issues answers its forward edges from this read,
1086 /// exactly as an [`ISSUE`] read of it does, and only one blocked by more is asked again.
1087 /// It adds a connection under each issue of the page — one rate-limit point per page,
1088 /// and `$nestedFirst` nodes per issue — and is kept off [`SEARCH_ISSUES`], whose pages
1089 /// answer questions that never read an edge.
1090 ///
1091 /// **The project issue itself rides here too**, with the page of what blocks it, because
1092 /// the reader of a project's tasks reads the project next: a copy writing the project reads
1093 /// it and its edges before it writes them. Carried on the request that already names the
1094 /// project, the first page answers that read and the dependency read after it, at the
1095 /// state GitHub answered in this command, rather than spending one more read on an item
1096 /// the cache's token cannot vouch for whole. Each sub-issue is kept the same way, so a
1097 /// copy's read of a task this listing carried is answered by it rather than sent again.
1098 /// Only a copy's read-before-write takes them; every other read by id reads GitHub.
1099 pub const SUB_ISSUES: &str = concat!(
1100 r#"query($id:ID!,$memberFirst:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1101 relationship_events_variable!(),
1102 r#"){
1103 node(id:$id){__typename ...BoardIssue
1104 ... on Issue{blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}
1105 subIssues(first:$memberFirst,after:$after){
1106 pageInfo{hasNextPage endCursor}
1107 nodes{__typename ...BoardIssue ... on Issue{
1108 blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}
1109 }}
1110 }}}
1111 }"#,
1112 board_issue!(),
1113 related_issue!()
1114 );
1115
1116 /// What a read of the board's own `items` selects of each item's content.
1117 ///
1118 /// A macro for the reason [`board_item_values!`] is one: [`BOARD`] and [`ORIGIN_LOOKUP`]
1119 /// both walk `ProjectV2.items` and hand each item to one resolver, so they select its
1120 /// content by one spelling.
1121 macro_rules! board_item_content {
1122 () => {
1123 r#" content{
1124 ... 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}}}
1125 ... on PullRequest{__typename id}
1126 ... on DraftIssue{__typename id title body createdAt updatedAt}
1127 }"#
1128 };
1129 }
1130
1131 /// Reads the board's fields and one page of its items.
1132 pub const BOARD: &str = concat!(
1133 r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$nestedFirst:Int!,$duplicates:Boolean!"#,
1134 relationship_events_variable!(),
1135 r#"){
1136 owner:repositoryOwner(login:$owner){
1137 ... on ProjectV2Owner{projectV2(number:$number){...Board}}
1138 }
1139 } fragment Board on ProjectV2 { id title
1140 fields(first:$nestedFirst){nodes{
1141 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1142 ... on ProjectV2Field{__typename id name dataType}
1143 }pageInfo{hasNextPage}}
1144 items(first:$first,after:$after){nodes{id updatedAt "#,
1145 board_item_values!(),
1146 board_item_content!(),
1147 r#"} pageInfo{hasNextPage endCursor}}
1148 }"#
1149 );
1150
1151 /// Every carrier of one copy origin, by two reads in one request, and nothing else of
1152 /// the board.
1153 ///
1154 /// **`originItems`** is the board's own items narrowed by its own field filter —
1155 /// `ProjectV2.items(query:)`, which GitHub's schema declares as "Search query for
1156 /// filtering items" — to those whose `onetaskgraph.origin` text field holds the
1157 /// qualified id, quoted. It reads the field every carrier already holds, whichever release
1158 /// wrote it, and matches it exactly: measured on 2026-09-29 against a 394-item board,
1159 /// the quoted, the unquoted and the bare-value spellings each returned exactly the one
1160 /// carrier and a prefix of the value returned none. It is `ProjectV2.items`, so it lags a
1161 /// fresh `addProjectV2ItemById` the way that connection does.
1162 ///
1163 /// **`search`** is the board-scoped issue search for the same id as a quoted phrase in
1164 /// the body, which is where this source mirrors the origin into its metadata slot. GitHub
1165 /// indexes that comment, and the index catches up with a write in a second or two rather
1166 /// than in minutes, so it finds a carrier another process wrote that the first read is
1167 /// still behind on.
1168 ///
1169 /// Each connection pages at `$originFirst`, its own small size — see `ORIGIN_PAGE_SIZE`
1170 /// — and resumes from its own cursor; a connection already walked to its end is resumed
1171 /// from its last cursor, which answers an empty page. Every candidate either read returns
1172 /// is confirmed against its own origin field before it is reported, so a token match of
1173 /// the search or anything else the filter admits never is.
1174 ///
1175 /// The root is aliased `originItems` rather than `owner`, so nothing counting the board's
1176 /// own whole reads counts this one among them.
1177 pub const ORIGIN_LOOKUP: &str = concat!(
1178 r#"query($owner:String!,$number:Int!,$filter:String!,$search:String!,$type:SearchType!,$originFirst:Int!,$itemsAfter:String,$searchAfter:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1179 relationship_events_variable!(),
1180 r#"){
1181 originItems:repositoryOwner(login:$owner){
1182 ... on ProjectV2Owner{projectV2(number:$number){
1183 items(first:$originFirst,after:$itemsAfter,query:$filter){nodes{id updatedAt "#,
1184 board_item_values!(),
1185 board_item_content!(),
1186 r#"} pageInfo{hasNextPage endCursor}}
1187 }}
1188 }
1189 search(query:$search,type:$type,first:$originFirst,after:$searchAfter){
1190 pageInfo{hasNextPage endCursor}
1191 nodes{__typename ...BoardIssue}
1192 }
1193 }"#,
1194 board_issue!()
1195 );
1196
1197 /// Every closed item of the board, by the board's own field filter — what
1198 /// `task status reconcile` reads, and nothing else of the board.
1199 ///
1200 /// **`ProjectV2.items(query:)` narrowed by `$filter`**, which this source binds to
1201 /// `is:closed` alone, so a board whose open items are most of it is not read whole to find
1202 /// the closed ones. It is narrowed rather than trusted: every item it answers is confirmed
1203 /// closed, with a reason and a card that contradict each other, from the item node it
1204 /// already carries, so a filter that also let open items through would cost reads and
1205 /// write nothing wrong.
1206 ///
1207 /// The board's own id and field definitions ride along, as [`BOARD`] carries them, so the
1208 /// option writes that follow need no read of the board. The root is aliased `closedItems`
1209 /// rather than `owner`, so nothing counting the board's own whole reads counts this one
1210 /// among them.
1211 pub const CLOSED_ITEMS: &str = concat!(
1212 r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$filter:String!,$nestedFirst:Int!,$duplicates:Boolean!"#,
1213 relationship_events_variable!(),
1214 r#"){
1215 closedItems:repositoryOwner(login:$owner){
1216 ... on ProjectV2Owner{projectV2(number:$number){id
1217 fields(first:$nestedFirst){nodes{
1218 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1219 ... on ProjectV2Field{__typename id name dataType}
1220 }pageInfo{hasNextPage}}
1221 items(first:$first,after:$after,query:$filter){nodes{id updatedAt "#,
1222 board_item_values!(),
1223 board_item_content!(),
1224 r#"} pageInfo{hasNextPage endCursor}}
1225 }}
1226 }
1227 }"#
1228 );
1229
1230 /// One alias of [`ISSUES_BY_REFERENCE`]: the issue `$owner<n>/$name<n>#$number<n>` names.
1231 macro_rules! reference_alias {
1232 ($n:literal) => {
1233 concat!(
1234 "\n r",
1235 stringify!($n),
1236 ":repository(owner:$owner",
1237 stringify!($n),
1238 ",name:$name",
1239 stringify!($n),
1240 "){issue(number:$number",
1241 stringify!($n),
1242 "){...HeldIssue}}"
1243 )
1244 };
1245 }
1246
1247 /// [`REFERENCE_BATCH`](super::REFERENCE_BATCH) issues at once, each by its repository and
1248 /// number — the one read `task status complete` makes of a source, and nothing else of
1249 /// the board: no `items`, no search and no project read.
1250 ///
1251 /// **Its own fragment, lighter than `board_issue!`, and that is what makes fifty
1252 /// references one point.** It selects no labels, and of each board membership only the
1253 /// `Status` value, by name, with `fieldValueByName` — so each alias asks GitHub for one
1254 /// connection, its page of memberships, where the whole fragment asks for five. What a
1255 /// completion writes is a card and, at most, the issue's state, so the labels and the other
1256 /// field values it leaves out are never what it needs; an item read this way is held for
1257 /// that write alone and never answers another read. An issue whose entry for this board is
1258 /// past its first page of memberships is walked with [`ISSUE_BOARD_ITEMS`], exactly as
1259 /// every other read of an issue recovers one.
1260 ///
1261 /// Fixed-size, as [`ISSUE_DETAILS`] is, so there is one document to price: a batch of
1262 /// fewer references binds the slots it has none for to its last one.
1263 pub const ISSUES_BY_REFERENCE: &str = concat!(
1264 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!"#,
1265 relationship_events_variable!(),
1266 r#"){"#,
1267 reference_alias!(0),
1268 reference_alias!(1),
1269 reference_alias!(2),
1270 reference_alias!(3),
1271 reference_alias!(4),
1272 reference_alias!(5),
1273 reference_alias!(6),
1274 reference_alias!(7),
1275 reference_alias!(8),
1276 reference_alias!(9),
1277 reference_alias!(10),
1278 reference_alias!(11),
1279 reference_alias!(12),
1280 reference_alias!(13),
1281 reference_alias!(14),
1282 reference_alias!(15),
1283 reference_alias!(16),
1284 reference_alias!(17),
1285 reference_alias!(18),
1286 reference_alias!(19),
1287 reference_alias!(20),
1288 reference_alias!(21),
1289 reference_alias!(22),
1290 reference_alias!(23),
1291 reference_alias!(24),
1292 reference_alias!(25),
1293 reference_alias!(26),
1294 reference_alias!(27),
1295 reference_alias!(28),
1296 reference_alias!(29),
1297 reference_alias!(30),
1298 reference_alias!(31),
1299 reference_alias!(32),
1300 reference_alias!(33),
1301 reference_alias!(34),
1302 reference_alias!(35),
1303 reference_alias!(36),
1304 reference_alias!(37),
1305 reference_alias!(38),
1306 reference_alias!(39),
1307 reference_alias!(40),
1308 reference_alias!(41),
1309 reference_alias!(42),
1310 reference_alias!(43),
1311 reference_alias!(44),
1312 reference_alias!(45),
1313 reference_alias!(46),
1314 reference_alias!(47),
1315 reference_alias!(48),
1316 reference_alias!(49),
1317 "\n }",
1318 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}
1319 projectItems(first:$boardItems){nodes{id updatedAt project{id number}
1320 status:fieldValueByName(name:$statusField){... on ProjectV2ItemFieldSingleSelectValue{name field{
1321 ... on ProjectV2SingleSelectField{id name options{id name}}
1322 }}}
1323 }pageInfo{hasNextPage endCursor}}}"#
1324 );
1325
1326 /// The board's own id and field definitions, and not one of its items.
1327 ///
1328 /// What a write needs of the board when the item it writes does not say: the id a field
1329 /// write and `addProjectV2ItemById` address, and the definitions of the `Status` and
1330 /// origin fields. It selects no `items`, so what it costs is the board's field list
1331 /// however many items the board holds — and it decides nothing about which items those
1332 /// are, which is the question a read of one item by its own id answers instead.
1333 ///
1334 /// The root is aliased `boardFields` rather than `owner`, so nothing counting the
1335 /// board's item reads by their root counts this one among them.
1336 pub const BOARD_FIELDS: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!){
1337 boardFields:repositoryOwner(login:$owner){
1338 ... on ProjectV2Owner{projectV2(number:$number){id
1339 fields(first:$nestedFirst){nodes{
1340 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1341 ... on ProjectV2Field{__typename id name dataType}
1342 }pageInfo{hasNextPage}}
1343 }}
1344 }
1345 }"#;
1346
1347 /// One board draft by its own node id, with the board item it sits in.
1348 ///
1349 /// A draft is not an issue, so [`ISSUE`] reaches it and reads nothing of it; this is the
1350 /// second read that answers it. `DraftIssue.projectV2Items` names the board item a draft
1351 /// is — GitHub links a draft to one item — with the same [`board_item_values!`] the
1352 /// issue fragment reads, so a draft reached by id resolves through the same resolver a
1353 /// board listing hands it to, and nothing has to list the board to find one.
1354 pub const DRAFT: &str = concat!(
1355 r#"query($id:ID!,$nestedFirst:Int!,$boardItems:Int!){
1356 node(id:$id){__typename ... on DraftIssue{id title body createdAt updatedAt
1357 projectV2Items(first:$boardItems){nodes{id project{id number}
1358 "#,
1359 board_item_values!(),
1360 r#"}pageInfo{hasNextPage endCursor}}}}
1361 }"#
1362 );
1363
1364 /// One issue's board memberships alone, walked past the page a read of it carried.
1365 ///
1366 /// The recovery read behind [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource):
1367 /// every document above carries a *page* of `Issue.projectItems`, and an issue on more
1368 /// boards than that page holds may have this board's entry past its end. This asks that
1369 /// one issue for its memberships and nothing else — the caller already holds the issue —
1370 /// so an answer of "this board does not hold it" is only ever given about a connection
1371 /// read to exhaustion.
1372 ///
1373 /// It selects the board item's id, its project number and the same
1374 /// [`board_item_values!`] the fragment does, because what it produces is handed to the
1375 /// very same resolver: an issue recovered this way reports the same title, the same
1376 /// status, the same labels and the same qualified id as one whose entry was on the
1377 /// page.
1378 ///
1379 /// `$first` rather than `$boardItems`: this document reads one issue, so nothing
1380 /// multiplies through it and the membership connection can be walked at
1381 /// [`MAX_PAGE_SIZE`](super::MAX_PAGE_SIZE) — which is what keeps the recovery to one
1382 /// further request for any issue a person really keeps.
1383 pub const ISSUE_BOARD_ITEMS: &str = concat!(
1384 r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!){
1385 node(id:$id){
1386 ... on Issue{projectItems(first:$first,after:$after){
1387 nodes{id updatedAt project{id number}
1388 "#,
1389 board_item_values!(),
1390 r#"}
1391 pageInfo{hasNextPage endCursor}}}
1392 }
1393 }"#
1394 );
1395 /// Whether the board's Project is public — half of what decides whether a write here can
1396 /// be read by anybody. Needs the `read:project` scope.
1397 pub const PROJECT_VISIBILITY: &str = r#"query($owner:String!,$number:Int!){visibility:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){public}}}}"#;
1398 /// Resolves the configured repository's node id, which creating an issue requires.
1399 pub const REPOSITORY: &str = r#"query($owner:String!,$name:String!){repository(owner:$owner,name:$name){id nameWithOwner}}"#;
1400 /// What creating an issue needs and has not read yet: the board's own id and field
1401 /// definitions, as [`BOARD_FIELDS`] reads them, and the node id of the repository the
1402 /// issue is created in, as [`REPOSITORY`] reads it — in one request.
1403 ///
1404 /// Sent at the point a create knows which repository it is for, when neither half is
1405 /// already known to this process; a create needing only one of them sends that one's own
1406 /// document. Neither half is kept past the process: a field's option ids are re-minted by
1407 /// `sources fields --apply`, so a copy of them held between runs would write the wrong
1408 /// status.
1409 pub const CREATION_CONTEXT: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!,$repositoryOwner:String!,$repositoryName:String!){
1410 boardFields:repositoryOwner(login:$owner){
1411 ... on ProjectV2Owner{projectV2(number:$number){id
1412 fields(first:$nestedFirst){nodes{
1413 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1414 ... on ProjectV2Field{__typename id name dataType}
1415 }pageInfo{hasNextPage}}
1416 }}
1417 }
1418 repository(owner:$repositoryOwner,name:$repositoryName){id nameWithOwner}
1419 }"#;
1420 /// Reads both dependency directions for one issue, with each far end's own kind — and
1421 /// the issue's own body, which is where an edge to another source is recorded, so that
1422 /// half of a dependency read needs no second read of the issue or of the board.
1423 pub const ISSUE_DEPENDENCIES: &str = concat!(
1424 r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
1425 ... on Issue{body
1426 blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1427 blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1428 }}}"#,
1429 related_issue!()
1430 );
1431 /// What one item depends on: [`ISSUE_DEPENDENCIES`] narrowed to `blockedBy`, so a
1432 /// dependency read asks GitHub for the one direction it answers. The issue's `body` rides
1433 /// along because its metadata slot is where an edge to another source is recorded — and a
1434 /// board draft's `body` too, with the one board item GitHub links a draft to and, aliased
1435 /// `board`, this board's own id, so a draft's recorded edges are read off this same answer
1436 /// and only when that item is this board's: no draft read and no board read follow it.
1437 ///
1438 /// The draft's board item is asked for under a root of its own, aliased `draft`, rather
1439 /// than beside its `body` under `node`. GitHub counts the type-conditional fragments of
1440 /// one selection as the largest of them — `blockedBy`'s 100 alone, where this workspace's
1441 /// calculation adds the draft's 3 — while separate roots are summed by both, so this is
1442 /// the spelling whose node count GitHub and the calculation agree on. Both price it at
1443 /// one point; the credentialed lane's reconciliation is what holds the two to that.
1444 pub const ISSUE_DEPENDS_ON: &str = concat!(
1445 r#"query($id:ID!,$first:Int!,$after:String,$owner:String!,$number:Int!,$boardItems:Int!){
1446 node(id:$id){__typename
1447 ... on Issue{body
1448 blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1449 }
1450 ... on DraftIssue{id body}}
1451 draft:node(id:$id){... on DraftIssue{projectV2Items(first:$boardItems){nodes{project{id number}}pageInfo{hasNextPage}}}}
1452 board:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){id}}}}"#,
1453 related_issue!()
1454 );
1455 /// What depends on one issue: [`ISSUE_DEPENDENCIES`] with its `blocking` alone. No `body`:
1456 /// the reverse of a recorded edge is derived from the far end, never written down here.
1457 pub const ISSUE_DEPENDED_ON_BY: &str = concat!(
1458 r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
1459 ... on Issue{
1460 blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1461 }}}"#,
1462 related_issue!()
1463 );
1464 /// Creates one issue. Outside a copy it is created on no board, and [`ADD_TO_BOARD`] files
1465 /// it; inside one, its input names the board in `projectV2Ids` and its project in
1466 /// `parentIssueId`, so this one request files it under both — see
1467 /// [`GitHubProjectsSource::create_filed_issue`](super::GitHubProjectsSource).
1468 pub const CREATE_ISSUE: &str =
1469 r#"mutation($input:CreateIssueInput!){createIssue(input:$input){issue{id number url}}}"#;
1470 /// Puts an existing issue on the configured board.
1471 pub const ADD_TO_BOARD: &str = r#"mutation($input:AddProjectV2ItemByIdInput!){addProjectV2ItemById(input:$input){item{id}}}"#;
1472 /// Updates an issue's visible fields and its open or closed state in one call.
1473 pub const UPDATE_ISSUE: &str =
1474 r#"mutation($input:UpdateIssueInput!){updateIssue(input:$input){issue{id}}}"#;
1475 /// Updates an existing draft's user-visible fields.
1476 pub const UPDATE_DRAFT: &str = r#"mutation($input:UpdateProjectV2DraftIssueInput!){updateProjectV2DraftIssue(input:$input){draftIssue{id}}}"#;
1477 /// Updates a text or single-select value on one project item.
1478 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}}}}}}}}"#;
1479 /// Writes up to six board fields and clears up to three in one ordered mutation: every
1480 /// aliased field is included by its own boolean, as [`FIELD_WRITE_SLOTS`](super::FIELD_WRITE_SLOTS)
1481 /// and [`FIELD_CLEAR_SLOTS`](super::FIELD_CLEAR_SLOTS) name them, writes before clears.
1482 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}}}"#;
1483 /// Clears one project item's value of one field, which is what a `none` priority is.
1484 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}}}}}}}}"#;
1485 /// Creates one field: a single-select one with its options, or a text one for a projected
1486 /// metadata value. Only the guarded field setup may use this document, and only for a
1487 /// field the board lacks.
1488 pub const CREATE_FIELD: &str = r#"mutation($input:CreateProjectV2FieldInput!){createProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id name options{id name color description}}}}}"#;
1489 /// Replaces a single-select field's options. Only the guarded field setup — the
1490 /// `status-options` and `fields` operations — may use this document, because GitHub
1491 /// treats the input as the complete option list.
1492 pub const STATUS_OPTIONS_UPDATE: &str = r#"mutation($input:UpdateProjectV2FieldInput!){updateProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id options{id name color description}}}}}"#;
1493 /// A fresh snapshot of the Status field and every board item's assignment.
1494 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}}}}}}"#;
1495 /// Files one issue under another as a sub-issue, which is what project membership is.
1496 pub const ADD_SUB_ISSUE: &str =
1497 r#"mutation($input:AddSubIssueInput!){addSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1498 /// Takes one issue back out of its parent.
1499 pub const REMOVE_SUB_ISSUE: &str = r#"mutation($input:RemoveSubIssueInput!){removeSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1500 /// Adds GitHub's native issue blocked-by relationship.
1501 pub const ADD_BLOCKED_BY: &str = r#"mutation($input:AddBlockedByInput!){addBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1502 /// One origin of [`ORIGIN_LOOKUPS`]: [`ORIGIN_LOOKUP`]'s two connections for the origin the
1503 /// `$filter<n>` and `$search<n>` of slot `<n>` name, sent only while `$ask<n>` holds.
1504 macro_rules! origin_lookup_alias {
1505 ($n:literal) => {
1506 concat!(
1507 "\n o",
1508 stringify!($n),
1509 ":repositoryOwner(login:$owner) @include(if:$ask",
1510 stringify!($n),
1511 "){... on ProjectV2Owner{projectV2(number:$number){items(first:$lookupFirst,query:$filter",
1512 stringify!($n),
1513 "){nodes{id updatedAt ",
1514 board_item_values!(),
1515 board_item_content!(),
1516 "} pageInfo{hasNextPage endCursor}}}}}\n s",
1517 stringify!($n),
1518 ":search(query:$search",
1519 stringify!($n),
1520 ",type:$type,first:$lookupFirst) @include(if:$ask",
1521 stringify!($n),
1522 "){pageInfo{hasNextPage endCursor} nodes{__typename ...BoardIssue}}"
1523 )
1524 };
1525 }
1526
1527 /// [`ORIGIN_LOOKUPS`], with `$extra_variables` declared after its own and `$extra_fields`
1528 /// selected beside them: one spelling of the batched lookup for the documents that send it.
1529 macro_rules! origin_lookups {
1530 ($extra_variables:literal, $extra_fields:literal) => {
1531 concat!(
1532 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!"#,
1533 relationship_events_variable!(),
1534 $extra_variables,
1535 r#"){
1536 boardFields:repositoryOwner(login:$owner){
1537 ... on ProjectV2Owner{projectV2(number:$number){id
1538 fields(first:$nestedFirst){nodes{
1539 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1540 ... on ProjectV2Field{__typename id name dataType}
1541 }pageInfo{hasNextPage}}
1542 }}
1543 }
1544 repository(owner:$repositoryOwner,name:$repositoryName){id databaseId nameWithOwner}"#,
1545 $extra_fields,
1546 origin_lookup_alias!(0),
1547 origin_lookup_alias!(1),
1548 origin_lookup_alias!(2),
1549 origin_lookup_alias!(3),
1550 origin_lookup_alias!(4),
1551 origin_lookup_alias!(5),
1552 origin_lookup_alias!(6),
1553 origin_lookup_alias!(7),
1554 origin_lookup_alias!(8),
1555 origin_lookup_alias!(9),
1556 "\n }",
1557 board_issue!()
1558 )
1559 };
1560 }
1561
1562 /// The first page of [`ORIGIN_LOOKUP`] for up to [`ORIGIN_BATCH`](super::ORIGIN_BATCH)
1563 /// origins a copy is about to look for, and everything creating an item then reads —
1564 /// the board's own id and field definitions, as [`CREATION_CONTEXT`] reads them, and the node
1565 /// id and the numeric id of the repository an issue is created in — in one request.
1566 ///
1567 /// What a copy reads before it writes anything, so that finding none of its records on the
1568 /// board and creating every one costs this read and the writes. An origin whose page says
1569 /// there is more is left to its own [`ORIGIN_LOOKUP`]. The repository's numeric id is what an
1570 /// image upload names, so a copy carrying assets needs no `GET /repos/{owner}/{repo}` either.
1571 pub const ORIGIN_LOOKUPS: &str = origin_lookups!("", "");
1572
1573 /// [`ORIGIN_LOOKUPS`], with a page of the members of the project `$project` beside it, each
1574 /// trimmed to what classifying it reads — its id, its parent, its body and its repository,
1575 /// and no connection under it.
1576 ///
1577 /// What a document copy sends where the public boundary screens its write: the document is
1578 /// held to who may read its project, which is its members' classification as much as the
1579 /// project's own, and this request carries both the project — among the origins it asks —
1580 /// and its members. GitHub's own dry run prices it at the one point [`ORIGIN_LOOKUPS`]
1581 /// costs alone (`docs/measurements/board-copy-gaps-2026-10-24/coalesced-members-dry-run`).
1582 pub const ORIGIN_LOOKUPS_WITH_MEMBERS: &str = origin_lookups!(
1583 ",$project:ID!,$memberFirst:Int!",
1584 "\n members:node(id:$project){... on Issue{subIssues(first:$memberFirst){pageInfo{hasNextPage endCursor} nodes{id body parent{id} repository{nameWithOwner}}}}}"
1585 );
1586
1587 /// One slot `<n>` of a copy's aliased writes: `$<prefix><n>` is its input and `$send<n>`
1588 /// whether it is sent.
1589 macro_rules! write_slot {
1590 ($prefix:literal, $field:literal, $selection:literal, $n:literal) => {
1591 concat!(
1592 " ",
1593 $prefix,
1594 stringify!($n),
1595 ":",
1596 $field,
1597 "(input:$",
1598 $prefix,
1599 stringify!($n),
1600 ") @include(if:$send",
1601 stringify!($n),
1602 "){",
1603 $selection,
1604 "}"
1605 )
1606 };
1607 }
1608
1609 /// The variables of a copy's aliased writes: eleven inputs of `$type` named `$<prefix><n>`,
1610 /// and eleven `$send<n>`.
1611 macro_rules! write_variables {
1612 ($prefix:literal, $type:literal) => {
1613 concat!(
1614 "mutation(",
1615 "$",
1616 $prefix,
1617 "0:",
1618 $type,
1619 "!,$",
1620 $prefix,
1621 "1:",
1622 $type,
1623 "!,$",
1624 $prefix,
1625 "2:",
1626 $type,
1627 "!,$",
1628 $prefix,
1629 "3:",
1630 $type,
1631 "!,$",
1632 $prefix,
1633 "4:",
1634 $type,
1635 "!,$",
1636 $prefix,
1637 "5:",
1638 $type,
1639 "!,$",
1640 $prefix,
1641 "6:",
1642 $type,
1643 "!,$",
1644 $prefix,
1645 "7:",
1646 $type,
1647 "!,$",
1648 $prefix,
1649 "8:",
1650 $type,
1651 "!,$",
1652 $prefix,
1653 "9:",
1654 $type,
1655 "!,$",
1656 $prefix,
1657 "10:",
1658 $type,
1659 "!,",
1660 "$send0:Boolean!,$send1:Boolean!,$send2:Boolean!,$send3:Boolean!,$send4:Boolean!,",
1661 "$send5:Boolean!,$send6:Boolean!,$send7:Boolean!,$send8:Boolean!,$send9:Boolean!,",
1662 "$send10:Boolean!){"
1663 )
1664 };
1665 }
1666
1667 /// A copy's aliased writes of one kind: [`MUTATION_BATCH`](super::MUTATION_BATCH) slots of
1668 /// `$field`, each sent only while its `$send<n>` holds.
1669 macro_rules! write_batch {
1670 ($prefix:literal, $type:literal, $field:literal, $selection:literal) => {
1671 concat!(
1672 write_variables!($prefix, $type),
1673 write_slot!($prefix, $field, $selection, 0),
1674 write_slot!($prefix, $field, $selection, 1),
1675 write_slot!($prefix, $field, $selection, 2),
1676 write_slot!($prefix, $field, $selection, 3),
1677 write_slot!($prefix, $field, $selection, 4),
1678 write_slot!($prefix, $field, $selection, 5),
1679 write_slot!($prefix, $field, $selection, 6),
1680 write_slot!($prefix, $field, $selection, 7),
1681 write_slot!($prefix, $field, $selection, 8),
1682 write_slot!($prefix, $field, $selection, 9),
1683 write_slot!($prefix, $field, $selection, 10),
1684 "}"
1685 )
1686 };
1687 }
1688
1689 /// Creates up to [`MUTATION_BATCH`](super::MUTATION_BATCH) issues in one request, each filed on
1690 /// the board and under its project by the same `createIssue` — its `projectV2Ids` and
1691 /// `parentIssueId` — as [`CREATE_ISSUE`] creates one.
1692 pub const CREATE_ISSUES: &str = write_batch!(
1693 "create",
1694 "CreateIssueInput",
1695 "createIssue",
1696 "issue{id number url}"
1697 );
1698
1699 /// One issue of [`CREATED_BOARD_ITEMS`]: the board items the issue `$id<n>` names is on, with
1700 /// the `Status` value of each, read only while `$read<n>` holds.
1701 macro_rules! created_item_alias {
1702 ($n:literal) => {
1703 concat!(
1704 "\n i",
1705 stringify!($n),
1706 ":node(id:$id",
1707 stringify!($n),
1708 ") @include(if:$read",
1709 stringify!($n),
1710 "){... on Issue{id projectItems(first:$boardItems){nodes{id project{id} status:fieldValueByName(name:$statusName){... on ProjectV2ItemFieldSingleSelectValue{name}}}}}}"
1711 )
1712 };
1713 }
1714
1715 /// The board item of each issue a copy created, up to
1716 /// [`COPY_DETAIL_BATCH`](super::COPY_DETAIL_BATCH) at a time, with its `Status` as the board
1717 /// set it: what `createIssue`'s `projectV2Ids` files the issue as, which GitHub adds after
1718 /// answering the create, so its id comes from a read made once every create has been sent.
1719 pub const CREATED_BOARD_ITEMS: &str = concat!(
1720 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!){"#,
1721 created_item_alias!(0),
1722 created_item_alias!(1),
1723 created_item_alias!(2),
1724 created_item_alias!(3),
1725 created_item_alias!(4),
1726 created_item_alias!(5),
1727 created_item_alias!(6),
1728 created_item_alias!(7),
1729 created_item_alias!(8),
1730 created_item_alias!(9),
1731 created_item_alias!(10),
1732 created_item_alias!(11),
1733 created_item_alias!(12),
1734 created_item_alias!(13),
1735 created_item_alias!(14),
1736 created_item_alias!(15),
1737 created_item_alias!(16),
1738 created_item_alias!(17),
1739 created_item_alias!(18),
1740 created_item_alias!(19),
1741 created_item_alias!(20),
1742 "\n }"
1743 );
1744
1745 /// The variables and the aliases of [`WRITE_BATCH`](super::WRITE_BATCH) slots: `$<prefix><n>`
1746 /// is a slot's input and `$<send><n>` whether it is sent, for the slot numbers given.
1747 macro_rules! wide_variables {
1748 ($prefix:literal, $type:literal, $send:literal; $($n:literal)+) => {
1749 concat!($("$", $prefix, stringify!($n), ":", $type, "!,$", $send, stringify!($n), ":Boolean!,",)+)
1750 };
1751 }
1752 macro_rules! wide_slots {
1753 ($prefix:literal, $field:literal, $selection:literal, $send:literal; $($n:literal)+) => {
1754 concat!($(
1755 " ", $prefix, stringify!($n), ":", $field, "(input:$", $prefix, stringify!($n),
1756 ") @include(if:$", $send, stringify!($n), "){", $selection, "}",
1757 )+)
1758 };
1759 }
1760
1761 /// Up to [`WRITE_BATCH`](super::WRITE_BATCH) board field values a copy writes on the items it
1762 /// created, each on its own item, in one ordered request.
1763 pub const WRITE_FIELD_VALUES: &str = concat!(
1764 "mutation(",
1765 wide_variables!("field", "UpdateProjectV2ItemFieldValueInput", "send"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1766 "){",
1767 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),
1768 "}"
1769 );
1770
1771 /// A copy's board field values and its blocked-by edges together, up to
1772 /// [`WRITE_BATCH`](super::WRITE_BATCH) of the two kinds between them, in one ordered request:
1773 /// every field value its `$sendField<n>` sends, then every edge its `$sendBlock<n>` sends.
1774 /// What a copy sends when one request of its writes holds both kinds; one holding a single
1775 /// kind is [`WRITE_FIELD_VALUES`] or [`ADD_BLOCKED_BYS`].
1776 pub const WRITE_FIELDS_AND_EDGES: &str = concat!(
1777 "mutation(",
1778 wide_variables!("field", "UpdateProjectV2ItemFieldValueInput", "sendField"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1779 wide_variables!("block", "AddBlockedByInput", "sendBlock"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1780 "){",
1781 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),
1782 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),
1783 "}"
1784 );
1785
1786 /// Closes up to [`MUTATION_BATCH`](super::MUTATION_BATCH) issues a copy created at a closing
1787 /// status, each with its own `stateInput` and nothing else, once its option is selected.
1788 pub const CLOSE_ISSUES: &str =
1789 write_batch!("close", "UpdateIssueInput", "updateIssue", "issue{id}");
1790
1791 /// Updates up to [`MUTATION_BATCH`](super::MUTATION_BATCH) existing issues a copy rewrites,
1792 /// each with the members its own [`UPDATE_ISSUE`] would have sent, in one request.
1793 pub const UPDATE_ISSUES: &str =
1794 write_batch!("update", "UpdateIssueInput", "updateIssue", "issue{id}");
1795
1796 /// Up to [`WRITE_BATCH`](super::WRITE_BATCH) native blocked-by relationships a copy adds to the
1797 /// issues it created, in one request.
1798 pub const ADD_BLOCKED_BYS: &str = concat!(
1799 "mutation(",
1800 wide_variables!("block", "AddBlockedByInput", "send"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1801 "){",
1802 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),
1803 "}"
1804 );
1805
1806 /// Removes one native issue blocked-by relationship.
1807 pub const REMOVE_BLOCKED_BY: &str = r#"mutation($input:RemoveBlockedByInput!){removeBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1808 /// Deletes one issue, which takes its board item with it.
1809 ///
1810 /// The engine sends this in one situation only: undoing a copy that could not finish,
1811 /// over the items that same copy created. Deleting the issue removes the board item
1812 /// too, so there is no second `deleteProjectV2Item` to keep in step with it.
1813 pub const DELETE_ISSUE: &str =
1814 r#"mutation($input:DeleteIssueInput!){deleteIssue(input:$input){repository{id}}}"#;
1815 /// Deletes up to [`DELETE_BATCH`](super::DELETE_BATCH) issues in one request, each as
1816 /// [`DELETE_ISSUE`] deletes one: what a write that created several issues and was refused
1817 /// part way sends to take them back.
1818 pub const DELETE_ISSUES: &str = concat!(
1819 "mutation(",
1820 wide_variables!("delete", "DeleteIssueInput", "send"; 0 1 2 3 4),
1821 "){",
1822 wide_slots!("delete", "deleteIssue", "repository{id}", "send"; 0 1 2 3 4),
1823 "}"
1824 );
1825
1826 /// Everything this source reads about one issue comment, wherever it reaches one.
1827 ///
1828 /// A macro for the reason [`board_issue!`] is one: a comment listed, a comment just added
1829 /// and a comment just edited are handed to one mapper, so they are selected by one
1830 /// spelling. `author` is `Actor`, which GitHub answers `null` for an account that no
1831 /// longer exists, and `login` is the one member every kind of actor carries.
1832 macro_rules! issue_comment {
1833 () => {
1834 "id author{login} createdAt updatedAt body url"
1835 };
1836 }
1837
1838 /// One task's comments: a page of its issue's own `comments` connection.
1839 ///
1840 /// **No `orderBy`, and that is what makes the page oldest first.** GitHub's only
1841 /// `IssueCommentOrder` field is `UPDATED_AT`, which would move a comment to the end of the
1842 /// list every time somebody edited it; left unordered the connection answers in the order
1843 /// the comments were written, which is the order GitHub documents for the same collection
1844 /// over REST — ascending id. Nothing multiplies through it, so `$first` is the whole of its
1845 /// node count and the caller's own page size is pushed straight down.
1846 pub const ISSUE_COMMENTS: &str = concat!(
1847 r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename ... on Issue{comments(first:$first,after:$after){nodes{"#,
1848 issue_comment!(),
1849 r#"}pageInfo{hasNextPage endCursor}}}}}"#
1850 );
1851 /// One issue by its own node id, with a page of its comments: what `task show` and a
1852 /// comment listing read, in one request.
1853 ///
1854 /// [`ISSUE`] and [`ISSUE_COMMENTS`] in one document, rather than one then the other. The
1855 /// comments are selected here and **not** on the shared `board_issue!` fragment, which
1856 /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] nest under a page of a hundred issues: a comment
1857 /// connection there would multiply through both of those documents' price, and neither
1858 /// needs one.
1859 pub const ISSUE_DETAIL: &str = concat!(
1860 r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1861 relationship_events_variable!(),
1862 r#"){
1863 node(id:$id){__typename ...BoardIssue ... on Issue{comments(first:$first,after:$after){nodes{"#,
1864 issue_comment!(),
1865 r#"}pageInfo{hasNextPage endCursor}}}}
1866 }"#,
1867 board_issue!()
1868 );
1869
1870 /// One alias of [`ISSUE_DETAILS`]: the item a batch's `$id<n>` names, with the first
1871 /// page of its comments when `$comments` asks for them.
1872 macro_rules! issue_details_alias {
1873 ($n:literal) => {
1874 concat!(
1875 "\n i",
1876 stringify!($n),
1877 ":node(id:$id",
1878 stringify!($n),
1879 "){__typename ...BoardIssue ... on Issue{comments(first:$first) @include(if:$comments){nodes{",
1880 issue_comment!(),
1881 "}pageInfo{hasNextPage endCursor}}}}"
1882 )
1883 };
1884 }
1885
1886 /// [`ISSUE_DETAIL`] for [`DETAIL_BATCH`](super::DETAIL_BATCH) items at once, each by its
1887 /// own node id, as one fixed-size document of aliased `node(id:)` fields.
1888 ///
1889 /// **Aliased `node(id:)` rather than `nodes(ids:)`, and that is what keeps its price
1890 /// honest.** The `github-graphql-node-count` model this workspace prices with treats a
1891 /// field that supplies neither `first` nor `last` as free, and `nodes(ids:)` supplies
1892 /// neither — so every connection under it would be priced at nothing and the pin in
1893 /// `tests/point_cost.rs` would understate what GitHub charges. Each alias here is the
1894 /// one-item read the model already prices, so the batch costs what its aliases cost.
1895 ///
1896 /// **Fixed-size, so there is one document to price.** A batch of fewer items binds the
1897 /// slots it has no item for to the last item it does, and reads that item again; the
1898 /// price is the document's, whatever its variables, so a short batch costs what a full
1899 /// one does and nothing more.
1900 pub const ISSUE_DETAILS: &str = concat!(
1901 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!"#,
1902 relationship_events_variable!(),
1903 r#"){"#,
1904 issue_details_alias!(0),
1905 issue_details_alias!(1),
1906 issue_details_alias!(2),
1907 issue_details_alias!(3),
1908 issue_details_alias!(4),
1909 issue_details_alias!(5),
1910 issue_details_alias!(6),
1911 issue_details_alias!(7),
1912 issue_details_alias!(8),
1913 issue_details_alias!(9),
1914 issue_details_alias!(10),
1915 issue_details_alias!(11),
1916 issue_details_alias!(12),
1917 issue_details_alias!(13),
1918 issue_details_alias!(14),
1919 issue_details_alias!(15),
1920 issue_details_alias!(16),
1921 issue_details_alias!(17),
1922 issue_details_alias!(18),
1923 issue_details_alias!(19),
1924 issue_details_alias!(20),
1925 issue_details_alias!(21),
1926 issue_details_alias!(22),
1927 issue_details_alias!(23),
1928 "\n }",
1929 board_issue!()
1930 );
1931
1932 /// One alias of [`ISSUE_COPY_DETAILS`]: the item a batch's `$id<n>` names, with the first
1933 /// page of its comments and the issues it is blocked by.
1934 macro_rules! issue_copy_details_alias {
1935 ($n:literal) => {
1936 concat!(
1937 "\n i",
1938 stringify!($n),
1939 ":node(id:$id",
1940 stringify!($n),
1941 "){__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{",
1942 issue_comment!(),
1943 "}pageInfo{hasNextPage endCursor}} blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}}}"
1944 )
1945 };
1946 }
1947
1948 /// [`ISSUE_DETAILS`] as a copy reads the tasks it carries: each item with the first page of
1949 /// its comments and its `blockedBy` — the edges a copy reads of every task next, which
1950 /// [`ISSUE`] carries and [`ISSUE_DETAILS`] does not — [`COPY_DETAIL_BATCH`](super::COPY_DETAIL_BATCH)
1951 /// at a time, so a copy of a changed task costs this read and nothing else.
1952 ///
1953 /// A document of its own rather than a widening of [`ISSUE_DETAILS`], whose batch size is
1954 /// what `task show-many` is priced by and which reads no edge.
1955 ///
1956 /// `children`, read only while `$children` holds, is which issues the issue `$project` has
1957 /// as sub-issues — each one's id and title, the page `$nestedFirst` allows — so that a
1958 /// project copy, which reads its project and every task at once, knows from this same read
1959 /// which tasks the project holds at the destination, and its orphan check is answered by it
1960 /// rather than by a listing of the project.
1961 pub const ISSUE_COPY_DETAILS: &str = concat!(
1962 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!"#,
1963 relationship_events_variable!(),
1964 r#"){"#,
1965 issue_copy_details_alias!(0),
1966 issue_copy_details_alias!(1),
1967 issue_copy_details_alias!(2),
1968 issue_copy_details_alias!(3),
1969 issue_copy_details_alias!(4),
1970 issue_copy_details_alias!(5),
1971 issue_copy_details_alias!(6),
1972 issue_copy_details_alias!(7),
1973 issue_copy_details_alias!(8),
1974 issue_copy_details_alias!(9),
1975 issue_copy_details_alias!(10),
1976 issue_copy_details_alias!(11),
1977 issue_copy_details_alias!(12),
1978 issue_copy_details_alias!(13),
1979 issue_copy_details_alias!(14),
1980 issue_copy_details_alias!(15),
1981 issue_copy_details_alias!(16),
1982 issue_copy_details_alias!(17),
1983 issue_copy_details_alias!(18),
1984 issue_copy_details_alias!(19),
1985 issue_copy_details_alias!(20),
1986 "\n children:node(id:$project) @include(if:$children){... on Issue{id subIssues(first:$nestedFirst){nodes{id title} pageInfo{hasNextPage}}}}",
1987 "\n }",
1988 board_issue!(),
1989 related_issue!()
1990 );
1991
1992 /// Which issue one comment is on, read before that comment is edited or removed.
1993 ///
1994 /// GitHub's comment mutations take the comment's id and nothing else, so without this a
1995 /// comment id given against the wrong task would change a comment on another issue.
1996 pub const COMMENT_ISSUE: &str =
1997 r#"query($id:ID!){node(id:$id){__typename ... on IssueComment{id body issue{id}}}}"#;
1998 /// Adds one comment to an issue, signed as the account the token belongs to.
1999 pub const ADD_COMMENT: &str = concat!(
2000 r#"mutation($input:AddCommentInput!){addComment(input:$input){subject{id} commentEdge{node{"#,
2001 issue_comment!(),
2002 r#"}}}}"#
2003 );
2004 /// Replaces the body of one issue comment.
2005 pub const UPDATE_COMMENT: &str = concat!(
2006 r#"mutation($input:UpdateIssueCommentInput!){updateIssueComment(input:$input){issueComment{"#,
2007 issue_comment!(),
2008 r#"}}}"#
2009 );
2010 /// Removes one issue comment. Its payload carries nothing about the comment it removed.
2011 pub const DELETE_COMMENT: &str = r#"mutation($input:DeleteIssueCommentInput!){deleteIssueComment(input:$input){clientMutationId}}"#;
2012
2013 /// What a mutation of a write the store's cache expects selects of the issue it wrote:
2014 /// everything [`ISSUE_DETAIL`] reads of it, so its response shows the task whole — its
2015 /// change token's every part and the first page of its comments.
2016 ///
2017 /// Sent **only** while that write runs, in place of the document named beside each
2018 /// below, so every other write's documents, prices and request counts are what they
2019 /// were. GitHub's token waits for a task to settle (`change_token::SETTLED`), and a
2020 /// mutation's response is stamped seconds after the change it made, so against GitHub
2021 /// itself a response rarely determines a version; the cache is then left unserveable
2022 /// for the task, and its next read refreshes it.
2023 macro_rules! whole_issue {
2024 () => {
2025 concat!(
2026 r#"__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{"#,
2027 issue_comment!(),
2028 r#"}pageInfo{hasNextPage endCursor}}}"#
2029 )
2030 };
2031 }
2032
2033 /// The variables every document of `whole_issue!` declares beside its own.
2034 macro_rules! whole_variables {
2035 () => {
2036 concat!(
2037 r#",$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
2038 relationship_events_variable!()
2039 )
2040 };
2041 }
2042
2043 /// [`ADD_COMMENT`], selecting the whole issue commented on.
2044 pub const ADD_COMMENT_WHOLE: &str = concat!(
2045 r#"mutation($input:AddCommentInput!"#,
2046 whole_variables!(),
2047 r#"){addComment(input:$input){subject{id "#,
2048 whole_issue!(),
2049 r#"} commentEdge{node{"#,
2050 issue_comment!(),
2051 r#"}}}}"#,
2052 board_issue!()
2053 );
2054 /// [`UPDATE_COMMENT`], selecting the whole issue the comment is on.
2055 pub const UPDATE_COMMENT_WHOLE: &str = concat!(
2056 r#"mutation($input:UpdateIssueCommentInput!"#,
2057 whole_variables!(),
2058 r#"){updateIssueComment(input:$input){issueComment{"#,
2059 issue_comment!(),
2060 r#" issue{"#,
2061 whole_issue!(),
2062 r#"}}}}"#,
2063 board_issue!()
2064 );
2065 /// [`UPDATE_ISSUE`], selecting the whole issue updated.
2066 pub const UPDATE_ISSUE_WHOLE: &str = concat!(
2067 r#"mutation($input:UpdateIssueInput!"#,
2068 whole_variables!(),
2069 r#"){updateIssue(input:$input){issue{id "#,
2070 whole_issue!(),
2071 r#"}}}"#,
2072 board_issue!()
2073 );
2074 /// [`UPDATE_FIELD`], selecting the whole issue behind the board item written.
2075 pub const UPDATE_FIELD_WHOLE: &str = concat!(
2076 r#"mutation($input:UpdateProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
2077 whole_variables!(),
2078 r#"){updateProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
2079 whole_issue!(),
2080 r#"}}}}"#,
2081 board_issue!()
2082 );
2083 /// [`CLEAR_FIELD`], selecting the whole issue behind the board item cleared.
2084 pub const CLEAR_FIELD_WHOLE: &str = concat!(
2085 r#"mutation($input:ClearProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
2086 whole_variables!(),
2087 r#"){clearProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
2088 whole_issue!(),
2089 r#"}}}}"#,
2090 board_issue!()
2091 );
2092
2093 /// The document sent in `document`'s place while a write the store's cache expects runs,
2094 /// when it has one.
2095 #[must_use]
2096 pub fn whole_twin(document: &str) -> Option<&'static str> {
2097 [
2098 (ADD_COMMENT, ADD_COMMENT_WHOLE),
2099 (UPDATE_COMMENT, UPDATE_COMMENT_WHOLE),
2100 (UPDATE_ISSUE, UPDATE_ISSUE_WHOLE),
2101 (UPDATE_FIELD, UPDATE_FIELD_WHOLE),
2102 (CLEAR_FIELD, CLEAR_FIELD_WHOLE),
2103 ]
2104 .into_iter()
2105 .find(|(plain, _)| *plain == document)
2106 .map(|(_, whole)| whole)
2107 }
2108
2109 /// Every document above, with what this source is doing when it sends one.
2110 ///
2111 /// One list rather than a `match` beside the constants: a rate-limit diagnostic has to
2112 /// name the call that was refused, and a `match` with a catch-all arm would answer a
2113 /// document added later with "talking to GitHub" and never say so.
2114 ///
2115 /// `documents_are_all_inventoried` reads this file back and fails naming any `pub
2116 /// const` here that this list omits, so the two cannot part — which is the same guard
2117 /// `CATEGORIES` carries, in the one shape available to a set of `&str` constants.
2118 pub const DOCUMENTS: [(&str, &str); 56] = [
2119 (
2120 ISSUES_BY_CREATION,
2121 "listing a repository's issues newest first for a create key",
2122 ),
2123 (SEARCH_ISSUES, "searching this board's issues"),
2124 (ISSUE, "reading one issue"),
2125 (
2126 DOCUMENT_IN_PROJECT,
2127 "reading one document with its project and the project's members",
2128 ),
2129 (
2130 ISSUE_BOARD_ITEMS,
2131 "reading one issue's board memberships past the page it came with",
2132 ),
2133 (SUB_ISSUES, "reading a project's tasks"),
2134 (BOARD, "reading the board"),
2135 (ORIGIN_LOOKUP, "looking up the items copied from one origin"),
2136 (CLOSED_ITEMS, "reading the board's closed items"),
2137 (
2138 ISSUES_BY_REFERENCE,
2139 "reading a batch of issues by their references",
2140 ),
2141 (BOARD_FIELDS, "reading the board's fields"),
2142 (DRAFT, "reading one draft"),
2143 (REPOSITORY, "reading the destination repository"),
2144 (
2145 PROJECT_VISIBILITY,
2146 "reading whether the board's project is public",
2147 ),
2148 (
2149 CREATION_CONTEXT,
2150 "reading the board's fields and the destination repository",
2151 ),
2152 (ISSUE_DEPENDENCIES, "reading an issue's dependencies"),
2153 (ISSUE_DEPENDS_ON, "reading what an item depends on"),
2154 (ISSUE_DEPENDED_ON_BY, "reading what depends on an issue"),
2155 (CREATE_ISSUE, "creating an issue"),
2156 (
2157 ORIGIN_LOOKUPS,
2158 "looking up the items copied from several origins, and what creating one reads",
2159 ),
2160 (
2161 ORIGIN_LOOKUPS_WITH_MEMBERS,
2162 "looking up the items copied from several origins, with a project's members",
2163 ),
2164 (CREATE_ISSUES, "creating several issues together"),
2165 (
2166 CREATED_BOARD_ITEMS,
2167 "reading the board items of the issues a copy created",
2168 ),
2169 (
2170 WRITE_FIELD_VALUES,
2171 "writing board fields of the items a copy created",
2172 ),
2173 (
2174 WRITE_FIELDS_AND_EDGES,
2175 "writing board fields and dependencies of the items a copy created",
2176 ),
2177 (CLOSE_ISSUES, "closing the issues a copy created"),
2178 (UPDATE_ISSUES, "updating several issues together"),
2179 (ADD_BLOCKED_BYS, "recording several dependencies together"),
2180 (ADD_TO_BOARD, "adding an issue to the board"),
2181 (UPDATE_ISSUE, "updating an issue"),
2182 (UPDATE_DRAFT, "updating a draft item"),
2183 (UPDATE_FIELD, "writing a board field"),
2184 (UPDATE_FIELDS, "writing board fields together"),
2185 (CLEAR_FIELD, "clearing a board field"),
2186 (
2187 CREATE_FIELD,
2188 "creating a board single-select field with its options",
2189 ),
2190 (
2191 STATUS_OPTIONS_SNAPSHOT,
2192 "snapshotting board Status options and assignments",
2193 ),
2194 (
2195 STATUS_OPTIONS_UPDATE,
2196 "safely replacing the board Status option list",
2197 ),
2198 (ADD_SUB_ISSUE, "filing an issue under its project"),
2199 (REMOVE_SUB_ISSUE, "taking an issue out of its project"),
2200 (ADD_BLOCKED_BY, "recording a dependency"),
2201 (REMOVE_BLOCKED_BY, "removing a dependency"),
2202 (DELETE_ISSUE, "deleting an issue"),
2203 (DELETE_ISSUES, "deleting several issues together"),
2204 (ISSUE_COMMENTS, "reading a task's comments"),
2205 (ISSUE_DETAIL, "reading one issue with its comments"),
2206 (
2207 ISSUE_DETAILS,
2208 "reading a batch of issues with their comments",
2209 ),
2210 (
2211 ISSUE_COPY_DETAILS,
2212 "reading a batch of issues with their comments and dependencies for a copy",
2213 ),
2214 (COMMENT_ISSUE, "reading which issue a comment is on"),
2215 (ADD_COMMENT, "adding a comment"),
2216 (UPDATE_COMMENT, "editing a comment"),
2217 (DELETE_COMMENT, "deleting a comment"),
2218 (
2219 ADD_COMMENT_WHOLE,
2220 "adding a comment, answered with the whole issue",
2221 ),
2222 (
2223 UPDATE_COMMENT_WHOLE,
2224 "editing a comment, answered with the whole issue",
2225 ),
2226 (
2227 UPDATE_ISSUE_WHOLE,
2228 "updating an issue, answered with it whole",
2229 ),
2230 (
2231 UPDATE_FIELD_WHOLE,
2232 "writing a board field, answered with the whole issue",
2233 ),
2234 (
2235 CLEAR_FIELD_WHOLE,
2236 "clearing a board field, answered with the whole issue",
2237 ),
2238 ];
2239}
2240
2241/// One aliased mutation field of [`graphql::UPDATE_FIELDS`]: the key its answer comes back
2242/// under, the variable carrying its input, and the boolean variable that includes it.
2243#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2244pub struct FieldSlot {
2245 /// The key of the answer's `data` this field's payload is under.
2246 pub alias: &'static str,
2247 /// The variable carrying this field's input.
2248 pub variable: &'static str,
2249 /// The boolean variable whose `true` sends this field.
2250 pub include: &'static str,
2251}
2252
2253/// The value writes [`graphql::UPDATE_FIELDS`] carries, in the order GitHub runs them.
2254pub const FIELD_WRITE_SLOTS: [FieldSlot; 6] = [
2255 FieldSlot {
2256 alias: "updateProjectV2ItemFieldValue",
2257 variable: "input",
2258 include: "writeFirst",
2259 },
2260 FieldSlot {
2261 alias: "second",
2262 variable: "second",
2263 include: "writeSecond",
2264 },
2265 FieldSlot {
2266 alias: "third",
2267 variable: "third",
2268 include: "writeThird",
2269 },
2270 FieldSlot {
2271 alias: "fourth",
2272 variable: "fourth",
2273 include: "writeFourth",
2274 },
2275 FieldSlot {
2276 alias: "fifth",
2277 variable: "fifth",
2278 include: "writeFifth",
2279 },
2280 FieldSlot {
2281 alias: "sixth",
2282 variable: "sixth",
2283 include: "writeSixth",
2284 },
2285];
2286
2287/// The clears [`graphql::UPDATE_FIELDS`] carries, run after every write of the same request.
2288pub const FIELD_CLEAR_SLOTS: [FieldSlot; 3] = [
2289 FieldSlot {
2290 alias: "cleared",
2291 variable: "clear",
2292 include: "writeClear",
2293 },
2294 FieldSlot {
2295 alias: "clearedSecond",
2296 variable: "clearSecond",
2297 include: "writeClearSecond",
2298 },
2299 FieldSlot {
2300 alias: "clearedThird",
2301 variable: "clearThird",
2302 include: "writeClearThird",
2303 },
2304];
2305
2306/// Which of GitHub's two rate limiters refused a request.
2307///
2308/// Waiting is the whole answer to the primary budget, and polling is what *extends* the
2309/// secondary one — so an operator told the wrong one takes the wrong next step, which is
2310/// the whole reason this is carried rather than collapsed into "rate limited".
2311#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2312enum Limiter {
2313 /// The hourly API budget, which `gh api rate_limit` reports and a wait answers.
2314 Primary,
2315 /// The burst limiter over content-generating requests, which nothing reports.
2316 Secondary,
2317}
2318
2319/// The wordings GitHub answers a secondary rate limit with.
2320///
2321/// It sends them under a forbidden status, under a too-many-requests status, and inside
2322/// the `errors` of a *successful* response, which is why the text is what this matches on
2323/// rather than the status. `abuse detection` is the wording GitHub used before the
2324/// limiter was renamed and still returns from some endpoints; `submitted too quickly` is
2325/// what a burst of content creation is refused with.
2326///
2327/// This is GitHub's vocabulary rather than this source's, so it is pinned rather than
2328/// remembered: `tests/fixtures/rate-limits.json` records where each wording was read and
2329/// when, and the drift gate reconciles the two lists both ways. Public for that gate
2330/// alone — a caller has no use for it, and matching on a refusal is this source's job.
2331pub const SECONDARY_WORDINGS: [&str; 5] = [
2332 "secondary rate limit",
2333 "temporarily blocked from content creation",
2334 "abuse detection",
2335 "submitted too quickly",
2336 "exceeded a secondary",
2337];
2338
2339/// The wordings GitHub answers an exhausted primary budget with.
2340///
2341/// `rate_limited` is the `type` its GraphQL error carries, which is read as a field rather
2342/// than looked for in the response text. `api rate limit already exceeded` is what GraphQL
2343/// answers a request made once the hour's budget is spent — "API rate limit already exceeded
2344/// for user ID …" in the `errors` of an HTTP 200, with no `type` — and neither of the other
2345/// two phrases is a substring of it, so without it that answer read as a refusal that will
2346/// never lift. Pinned and gated exactly as [`SECONDARY_WORDINGS`] is, and public for the same
2347/// one reason.
2348pub const PRIMARY_WORDINGS: [&str; 4] = [
2349 "api rate limit exceeded",
2350 "api rate limit already exceeded",
2351 "rate limit exceeded",
2352 "rate_limited",
2353];
2354
2355/// What a response *says about itself*, which is the only place a refusal can be read.
2356///
2357/// Deliberately not the whole response body. A board is a place people write about their
2358/// own work, and a task on it titled "the secondary rate limit" would, matched across the
2359/// raw text, turn a perfectly good answer into a refusal this source then waited out and
2360/// reported. So the item data is never read: what is read is GitHub's own REST-style
2361/// `message` envelope, which is what a forbidden status carries, and the `message` and
2362/// `type` of each GraphQL error, which is where a *successful* response says it.
2363///
2364/// A body that is not JSON at all has nothing structured to read, so only a failing
2365/// response's own text is taken — a successful response that is not JSON is malformed
2366/// rather than refused, and [`GitHubProjectsSource::answer`] says so.
2367fn refusal_wording(status: StatusCode, body: &str) -> String {
2368 let Ok(parsed) = serde_json::from_str::<Value>(body) else {
2369 return if status.is_success() {
2370 String::new()
2371 } else {
2372 body.to_owned()
2373 };
2374 };
2375 let mut said: Vec<&str> = parsed
2376 .get("message")
2377 .and_then(Value::as_str)
2378 .into_iter()
2379 .collect();
2380 if let Some(errors) = parsed.get("errors").and_then(Value::as_array) {
2381 for error in errors {
2382 said.extend(
2383 ["message", "type"]
2384 .into_iter()
2385 .filter_map(|key| error.get(key).and_then(Value::as_str)),
2386 );
2387 }
2388 }
2389 said.join("; ")
2390}
2391
2392impl Limiter {
2393 /// Which limiter refused this response, or `None` when none of them did.
2394 ///
2395 /// The wording is read first and the status only decides what carries none of it,
2396 /// because GitHub answers a secondary limit with a forbidden status far more often
2397 /// than with too-many-requests — while a forbidden status saying nothing about a limit
2398 /// really is a credential this token lacks.
2399 ///
2400 /// A response is a refusal because of its status or its own wording. A spent budget
2401 /// only ever explains one; it never turns an answer into a refusal.
2402 fn classify(status: StatusCode, budget_exhausted: bool, body: &str) -> Option<Self> {
2403 let normalized = refusal_wording(status, body).to_ascii_lowercase();
2404 if SECONDARY_WORDINGS
2405 .iter()
2406 .any(|wording| normalized.contains(wording))
2407 {
2408 return Some(Self::Secondary);
2409 }
2410 if status == StatusCode::TOO_MANY_REQUESTS {
2411 return Some(Self::Primary);
2412 }
2413 // An exhausted budget *explains* a response that failed; it does not make one that
2414 // succeeded into a failure. GitHub sets `x-ratelimit-remaining: 0` on the last
2415 // request the budget allowed as well as on the ones it then refuses, so reading
2416 // the header alone threw away a good answer — and, once refusals were retried,
2417 // replayed a request that had already taken effect.
2418 if !status.is_success() && budget_exhausted {
2419 return Some(Self::Primary);
2420 }
2421 // A successful response saying it: GitHub reports a GraphQL rate limit in the
2422 // `errors` of an HTTP 200, where nothing about the status says so at all.
2423 if status.is_success()
2424 && PRIMARY_WORDINGS
2425 .iter()
2426 .any(|wording| normalized.contains(wording))
2427 {
2428 return Some(Self::Primary);
2429 }
2430 None
2431 }
2432
2433 /// What this limiter is called where an operator can look it up.
2434 const fn name(self) -> &'static str {
2435 match self {
2436 Self::Primary => "GitHub's primary API rate limit",
2437 Self::Secondary => "GitHub's secondary rate limit",
2438 }
2439 }
2440
2441 /// What the endpoint an operator would go and check says about this limiter.
2442 const fn where_to_look(self) -> &'static str {
2443 match self {
2444 Self::Primary => {
2445 "That is the budget `gh api rate_limit` reports, so that endpoint says when it \
2446 comes back."
2447 }
2448 Self::Secondary => {
2449 "That limiter is not the primary API budget: `gh api rate_limit` reports the \
2450 primary budget and does not report this one, so budget showing there says \
2451 nothing about this refusal, and every further attempt extends it."
2452 }
2453 }
2454 }
2455
2456 /// The next step this limiter actually calls for.
2457 const fn what_to_do(self) -> &'static str {
2458 match self {
2459 Self::Primary => {
2460 "wait for the reset `gh api rate_limit` reports, then run the command again."
2461 }
2462 Self::Secondary => {
2463 "leave this board alone for a few minutes, then run the command again — or \
2464 raise pacing.min_mutation_interval_ms on this source so it writes more slowly."
2465 }
2466 }
2467 }
2468}
2469
2470/// One rate-limit refusal, and the wait GitHub asked for if it asked for one.
2471#[derive(Debug, Clone, Copy)]
2472struct Limited {
2473 limiter: Limiter,
2474 hint: Option<u64>,
2475 /// Which header the hint came from: `retry-after`, or an exhausted primary budget's
2476 /// `x-ratelimit-reset`.
2477 hinted_by: Hint,
2478}
2479
2480/// Where a refusal's hint came from, which is what its wait is named after.
2481#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2482enum Hint {
2483 /// `retry-after`, or no hint at all.
2484 RetryAfter,
2485 /// An exhausted primary budget's `x-ratelimit-reset`.
2486 PrimaryReset,
2487}
2488
2489impl Limited {
2490 /// The wait this refusal is answered with, when the backing-off schedule is at `backoff`,
2491 /// and the cause it is named by: the hint, raised to the schedule's own next wait —
2492 /// GitHub really does send `retry-after: 0`, and retrying at once is the one move that
2493 /// extends a secondary limit — or the schedule alone when there is none.
2494 fn wait(self, backoff: Duration) -> (Duration, WaitCause) {
2495 match (self.hint, self.hinted_by) {
2496 (Some(hint), Hint::RetryAfter) => (
2497 Duration::from_secs(hint).max(backoff),
2498 pacing::cause::RETRY_AFTER,
2499 ),
2500 (Some(hint), Hint::PrimaryReset) => (
2501 Duration::from_secs(hint).max(backoff),
2502 pacing::cause::PRIMARY_RESET,
2503 ),
2504 (None, _) => (backoff, pacing::cause::BACKOFF),
2505 }
2506 }
2507
2508 /// What the caller is told once this source has waited as long as it may.
2509 ///
2510 /// Both limiters report as [`SourceError::RateLimited`], because that is what
2511 /// happened: the kind a caller matches on says a rate limit refused this, and nothing
2512 /// about *which* limiter it was makes it a different kind of failure. What differs is
2513 /// the operator's next step, and that is what the message carries — a secondary
2514 /// refusal read as a primary one sends an operator to `gh api rate_limit`, where the
2515 /// budget looks fine, and then back to retry the very burst that was refused.
2516 fn exhausted(
2517 self,
2518 doing: &str,
2519 waits: u32,
2520 waited: Duration,
2521 needed: Duration,
2522 budget: Duration,
2523 ) -> SourceError {
2524 SourceError::RateLimited {
2525 retry_after_seconds: self.hint,
2526 message: Some(format!(
2527 "{} refused this source while {doing}; it waited {} out over {} and was refused \
2528 again, and the next wait of {} would take it past the {} one call may spend \
2529 waiting. {} next: {}",
2530 self.limiter.name(),
2531 plural(waits, "refusal"),
2532 seconds(waited),
2533 seconds(needed),
2534 seconds(budget),
2535 self.limiter.where_to_look(),
2536 self.limiter.what_to_do(),
2537 )),
2538 }
2539 }
2540}
2541
2542/// One HTTP attempt's result, with what its response said about the rate limit.
2543///
2544/// The two travel together so the record and the outcome are written from the same place:
2545/// what a response said about the budget is only readable while that response is in hand,
2546/// and what the attempt *meant* is only decidable once its body has been read.
2547struct Attempted {
2548 result: Result<Value, Attempt>,
2549 limits: accounting::RateLimit,
2550 /// GitHub's own reported cost for this call, for a document that asked for it.
2551 reported_cost: Option<u64>,
2552 response_outcome: accounting::ResponseOutcome,
2553}
2554
2555/// One attempt's outcome: an error to report, or a rate limit to wait out.
2556enum Attempt {
2557 Failed(SourceError),
2558 Limited(Limited),
2559}
2560
2561fn plural(count: u32, thing: &str) -> String {
2562 if count == 1 {
2563 format!("{count} {thing}")
2564 } else {
2565 format!("{count} {thing}s")
2566 }
2567}
2568
2569fn seconds(duration: Duration) -> String {
2570 format!("{:.1}s", duration.as_secs_f64())
2571}
2572
2573/// The member [`stamp_served`] writes onto every node of a response that says when it last
2574/// changed: the `Date` that response was served at. No GitHub type has a field of this name.
2575const SERVED_AT: &str = "onetaskgraphServedAt";
2576
2577/// Write `served` onto every object of `data` carrying an `updatedAt`, so whatever reads a
2578/// node's time later knows the server's clock on the response that read it — which is what
2579/// decides whether that node's change token is given (see [`change_token::SETTLED`]).
2580fn stamp_served(data: &mut Value, served: &str) {
2581 match data {
2582 Value::Object(members) => {
2583 if members.contains_key("updatedAt") {
2584 members.insert(SERVED_AT.to_owned(), Value::String(served.to_owned()));
2585 }
2586 for member in members.values_mut() {
2587 stamp_served(member, served);
2588 }
2589 }
2590 Value::Array(items) => {
2591 for item in items {
2592 stamp_served(item, served);
2593 }
2594 }
2595 _ => {}
2596 }
2597}
2598
2599/// A header GitHub spells as a whole number of seconds, or `None` when this one is not.
2600///
2601/// A value that is present and unreadable is deliberately *not* an error. `retry-after` is
2602/// allowed by HTTP to be a date rather than a count, an intermediary can rewrite either
2603/// header, and neither is what makes a response a refusal — so the whole cost of one this
2604/// cannot read is that the refusal carries no hint and the backing-off schedule answers it
2605/// instead. Refusing the response over the header would turn a readable refusal into an
2606/// unreadable one, and refusing to *wait* would be the one wrong direction to fail in.
2607fn whole_seconds(value: Option<&reqwest::header::HeaderValue>) -> Option<u64> {
2608 value
2609 .and_then(|value| value.to_str().ok())
2610 .and_then(|value| value.trim().parse::<u64>().ok())
2611}
2612
2613/// A transport failure with every cause beneath it, outermost first.
2614///
2615/// The client's own text for a request it could not complete is `error sending request for
2616/// url (…)`, and the socket error that says why — a refused or reset connection, an exhausted
2617/// port range, a connection closed before its answer — is only in the chain of sources below
2618/// it. Those differ in what to do next, so all of them are kept.
2619pub(crate) fn transport_error(error: &(dyn std::error::Error + 'static)) -> String {
2620 let mut text = error.to_string();
2621 let mut cause = error.source();
2622 while let Some(next) = cause {
2623 let said = next.to_string();
2624 if !text.ends_with(&said) {
2625 text = format!("{text}: {said}");
2626 }
2627 cause = next.source();
2628 }
2629 text
2630}
2631
2632/// Every mutation this source sends creates content — an issue, a board item, a field of
2633/// one, a sub-issue link, a dependency, a comment — or edits or removes content of that
2634/// kind, and no query in [`graphql::DOCUMENTS`] does, so what the secondary limiter counts
2635/// and what the keyword says are the same set. That is what makes the keyword a sound test
2636/// rather than a convenient one: counting an edit or a removal the limiter might not have
2637/// counted costs a wait, and not counting one it did count costs the next fifty minutes.
2638pub(crate) fn is_mutation(query: &str) -> bool {
2639 query.trim_start().starts_with("mutation")
2640}
2641
2642/// What this source was doing, for a diagnostic that has to say so.
2643///
2644/// Read out of [`graphql::DOCUMENTS`], which is the inventory rather than a copy of it, so
2645/// a document added without a description is caught by that list's own gate instead of
2646/// falling through to the vague arm below.
2647fn operation_description(query: &str) -> &'static str {
2648 graphql::DOCUMENTS
2649 .iter()
2650 .find(|(document, _)| *document == query)
2651 .map_or("talking to GitHub", |(_, doing)| *doing)
2652}
2653
2654/// GitHub's published ceiling on content-generating requests in any one minute.
2655///
2656/// Pinned in `tests/fixtures/rate-limits.json` and gated against it, because it is
2657/// GitHub's number rather than this source's. This source paces at it over its window: a
2658/// content-creating request goes out at once while the minute's creations leave room for it,
2659/// and waits until they do otherwise — see the `pacing` module.
2660pub const CONTENT_CREATION_PER_MINUTE: u64 = 80;
2661/// The same ceiling as GitHub publishes it for any one hour, paced over its window as the
2662/// per-minute one is. Pinned beside its sibling so the gate would notice either one moving.
2663pub const CONTENT_CREATION_PER_HOUR: u64 = 500;
2664/// GitHub's published ceiling on secondary points in any one minute: what every GraphQL
2665/// request is charged against, a mutation [`SECONDARY_POINTS_PER_MUTATION`] and a query
2666/// [`SECONDARY_POINTS_PER_QUERY`]. Pinned and paced as [`CONTENT_CREATION_PER_MINUTE`] is.
2667pub const SECONDARY_POINTS_PER_MINUTE: u64 = 2_000;
2668/// The secondary points one GraphQL request carrying a mutation is charged.
2669pub const SECONDARY_POINTS_PER_MUTATION: u64 = 5;
2670/// The secondary points one GraphQL request carrying no mutation is charged.
2671pub const SECONDARY_POINTS_PER_QUERY: u64 = 1;
2672/// The fastest *steady* spacing of content-creating mutations that cannot exceed
2673/// [`CONTENT_CREATION_PER_MINUTE`], in milliseconds: 60000/80 is 750.
2674///
2675/// It is no longer the default. This source paces by GitHub's windows instead, so a copy's
2676/// few dozen writes go out as fast as GitHub answers them while the minute has room, rather
2677/// than 750 ms apart — which was most of a plan copy's wall time. An installation that wants
2678/// mutations spaced as well says so through `pacing.min_mutation_interval_ms`, and this is the
2679/// value that holds a long sustained burst under the per-minute bound by spacing alone.
2680pub const MIN_MUTATION_INTERVAL_MS: u64 = 60_000 / CONTENT_CREATION_PER_MINUTE;
2681/// First wait when a rate-limit refusal carries no hint; each further wait doubles it.
2682///
2683/// A doubling schedule from one second reaches a minute in six waits, which is GitHub's
2684/// own advice for a secondary limit — wait, and wait longer each time — without spending
2685/// the first minute of a transient refusal doing nothing.
2686pub const RETRY_BACKOFF_MS: u64 = 1_000;
2687/// Total time one call may spend waiting out rate limits before it reports a failure.
2688///
2689/// Two minutes is long enough to ride out the refusals a paced copy still collects and
2690/// short enough that a command an operator is watching returns. The bound is what makes
2691/// the wait a wait rather than a hang: a call refused past it ends in a diagnostic naming
2692/// the limiter, not in a process nobody can tell from a wedged one.
2693pub const RETRY_BUDGET_MS: u64 = 120_000;
2694
2695fn default_token_env() -> String {
2696 "GH_PROJECTS_TOKEN".to_owned()
2697}
2698fn default_endpoint() -> String {
2699 "https://api.github.com/graphql".to_owned()
2700}
2701
2702/// The name of a `Status` single-select option on the board.
2703///
2704/// Validated on the way in rather than checked later, so a blank option name — which
2705/// nothing on a board can be — is a state this type cannot hold.
2706#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2707#[serde(try_from = "String")]
2708#[schemars(extend("minLength" = 1))]
2709pub struct ColumnName(String);
2710
2711impl ColumnName {
2712 /// The option name, as the board spells it.
2713 fn as_str(&self) -> &str {
2714 &self.0
2715 }
2716}
2717
2718impl TryFrom<String> for ColumnName {
2719 type Error = String;
2720
2721 fn try_from(name: String) -> Result<Self, Self::Error> {
2722 if name.trim().is_empty() {
2723 return Err("a status_mapping option name cannot be blank".to_owned());
2724 }
2725 Ok(Self(name))
2726 }
2727}
2728
2729/// The two closed states this product can mean.
2730///
2731/// GitHub's `IssueClosedStateReason` also spells `DUPLICATE`, which nothing here ever writes:
2732/// an issue closed as one *reads* `cancelled`, because its work is another issue's, but this
2733/// product never decides on a person's behalf that two issues are the same work.
2734#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, schemars::JsonSchema)]
2735#[serde(rename_all = "kebab-case")]
2736pub enum ClosedState {
2737 /// `COMPLETED` — precisely done.
2738 Completed,
2739 /// `NOT_PLANNED` — precisely cancelled.
2740 NotPlanned,
2741}
2742
2743impl ClosedState {
2744 const fn reason(self) -> &'static str {
2745 match self {
2746 Self::Completed => "COMPLETED",
2747 Self::NotPlanned => "NOT_PLANNED",
2748 }
2749 }
2750}
2751
2752/// Configuration for one GitHub Projects v2 board.
2753///
2754/// It serializes to a document this same type reads back as itself, so a caller writing a
2755/// configuration — a consumer filing onto a board it configures — can build one here rather
2756/// than spell the shape again; an empty `metadata_fields` is left out, as one never set.
2757#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2758#[serde(default, deny_unknown_fields)]
2759pub struct GitHubProjectsConfig {
2760 /// Login of the user or organization which owns the board.
2761 pub owner: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates GitHub's owner grammar before private construction.
2762 /// The project number shown in the board's GitHub URL.
2763 pub project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` bounds this to a positive GraphQL Int.
2764 // 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.
2765 /// `owner/name` of the repository this source creates an issue in when the item's own
2766 /// `repositories` field does not decide it.
2767 ///
2768 /// An item naming exactly one repository is created there — on a source declared
2769 /// `visibility: private`, only when that repository reads private, and otherwise as an
2770 /// item naming none is; a task or a document naming none or several is created in its
2771 /// parent project's repository; and a project, or a task or document with no parent,
2772 /// naming none or several is created here. A board
2773 /// has no repository of its own and `createIssue` requires one, so a write without
2774 /// this is refused naming the field. Reads never need it.
2775 pub repository: Option<String>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the `owner/name` grammar before private construction.
2776 // llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
2777 /// GitHub logins whose repositories this source may create an issue in.
2778 ///
2779 /// Absent or empty, the default, restricts nothing. Otherwise a create whose issue the
2780 /// rule `repository` describes would file in a repository owned by anyone else is refused
2781 /// before anything of it is written — by `task copy`, `task copy --create`, a member
2782 /// create of a project copy and a dry run of each — with the error kind `creation-owner`,
2783 /// naming the repository and these owners. Logins compare case-insensitively, as GitHub
2784 /// compares them. An update of an issue that already exists is never refused by it.
2785 /// `task locate` reports such a refusal as `creates_in.refused` rather than failing.
2786 #[serde(default, skip_serializing_if = "Vec::is_empty")]
2787 // Kept in the schema as `"default": []` although a serialized configuration leaves an empty
2788 // list out, so both SDKs model an absent list as an empty one rather than as `null`.
2789 #[schemars(!skip_serializing_if)]
2790 pub creation_owners: Vec<String>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates each entry against GitHub's login grammar, naming the one it refuses.
2791 /// Environment variable containing a fine-grained token with Projects and Issues
2792 /// read/write plus Pull requests read-only access for every repository represented on
2793 /// the board.
2794 #[serde(default = "default_token_env")]
2795 pub token_env: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the environment-variable grammar.
2796 /// GraphQL endpoint. GitHub Enterprise installations may override it.
2797 #[serde(default = "default_endpoint")]
2798 pub endpoint: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` converts it to the private validated `Url`.
2799 /// Per-instance mapping from a status category to the option of the board's one
2800 /// `Status` field it lands on, for a task and for a project.
2801 ///
2802 /// The shared `StatusMapping` grammar: each value is one option name for both kinds,
2803 /// `null` to disable the category for both, or `{task, project}` naming it per kind,
2804 /// where a kind left out leaves the category unmapped for that kind. A category this
2805 /// does not mention keeps its shipped default for both kinds: `backlog` to "Backlog",
2806 /// `todo` to "Todo", `queued` to "Queued", `in-progress` to "In Progress", `done` to
2807 /// "Done" plus closed as completed, `cancelled` to "Cancelled" plus closed as not
2808 /// planned, and `draft` and `unknown` unmapped. A category it does mention gets no
2809 /// shipped default for a kind it leaves out. `done` and `cancelled` close the issue for
2810 /// either kind. No two categories may name one option for the same kind, ignoring case.
2811 /// `unknown` may name one existing option; every unknown word then lands on it and
2812 /// reads back as `unknown` under its name. Unlike `local-md`, this source cannot keep
2813 /// each unknown word because it never creates board options.
2814 #[serde(default)]
2815 pub status_mapping: StatusMapping,
2816 /// Per-instance mapping from a task's priority to an option of this board's
2817 /// single-select field named `Priority`.
2818 ///
2819 /// Absent, this source holds no priority: every task reads as `none`, and a write of any
2820 /// other priority is refused before it reaches this board. Present, each of `urgent`,
2821 /// `high`, `medium` and `low` it does not mention keeps its shipped default — `Urgent`,
2822 /// `High`, `Medium` and `Low` — and an item with no value in the `Priority` field reads
2823 /// as `none`, so writing `none` clears the value. Option names match case-insensitively;
2824 /// no two levels may name one option. Reads and writes never create the field or an
2825 /// option: `onetaskgraph sources fields <source> --apply` does, and a write naming one
2826 /// the board lacks is refused pointing there.
2827 #[serde(default)]
2828 pub priority_mapping: Option<PriorityMappingConfig>,
2829 /// Caller metadata values this source also writes to board text fields, so the board can
2830 /// filter on them.
2831 ///
2832 /// Absent or empty, nothing is projected. Each entry names a board text `field`, a
2833 /// top-level metadata `key` and an optional `path` of object keys walked inside that key's
2834 /// value. On every item write — create, update and copy, of a task, a project or a
2835 /// document — a string found there is written to the field, and nothing is sent when the
2836 /// field already holds it; nothing there, or `null`, clears the field when it holds a
2837 /// value; any other JSON type is refused before any mutation. The metadata comment in the
2838 /// issue body stays the one home of the value, and every read reports metadata from it.
2839 /// Reads and writes never create the field: `onetaskgraph sources fields <source>
2840 /// --apply` does, as a text field, and a write to a board lacking it, or holding a
2841 /// non-text field of that name, is refused pointing there.
2842 #[serde(default, skip_serializing_if = "Vec::is_empty")]
2843 // Kept in the schema as `"default": []` although a serialized configuration leaves an empty
2844 // list out, so both SDKs model an absent list as an empty one rather than as `null`.
2845 #[schemars(!skip_serializing_if)]
2846 pub metadata_fields: Vec<MetadataFieldConfig>,
2847 /// How fast this source writes, and how long it waits out a rate-limit refusal.
2848 ///
2849 /// Every field keeps its shipped default when it is absent, and the defaults are
2850 /// GitHub's own published limits rather than taste. See [`Pacing`].
2851 #[serde(default)]
2852 pub pacing: PacingConfig,
2853}
2854
2855/// Which option of the board's `Priority` field each priority lands on.
2856///
2857/// One member per level rather than a map, so a key that is not a level is refused where
2858/// the configuration is read, naming the levels there are. `none` is not a member: it is no
2859/// value in the field, not an option of it.
2860#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2861#[serde(default, deny_unknown_fields)]
2862pub struct PriorityMappingConfig {
2863 /// The option `urgent` lands on; `Urgent` when absent.
2864 pub urgent: Option<PriorityOptionName>,
2865 /// The option `high` lands on; `High` when absent.
2866 pub high: Option<PriorityOptionName>,
2867 /// The option `medium` lands on; `Medium` when absent.
2868 pub medium: Option<PriorityOptionName>,
2869 /// The option `low` lands on; `Low` when absent.
2870 pub low: Option<PriorityOptionName>,
2871}
2872
2873/// The name of an option of the board's `Priority` single-select field.
2874///
2875/// Validated on the way in, for the reason [`ColumnName`] is: nothing on a board can have a
2876/// blank name.
2877#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2878#[serde(try_from = "String")]
2879#[schemars(extend("minLength" = 1))]
2880pub struct PriorityOptionName(String);
2881
2882impl PriorityOptionName {
2883 /// The option name, as the board spells it.
2884 fn as_str(&self) -> &str {
2885 &self.0
2886 }
2887}
2888
2889impl TryFrom<String> for PriorityOptionName {
2890 type Error = String;
2891
2892 fn try_from(name: String) -> Result<Self, Self::Error> {
2893 if name.trim().is_empty() {
2894 return Err("a priority_mapping option name cannot be blank".to_owned());
2895 }
2896 Ok(Self(name))
2897 }
2898}
2899
2900/// The name of the board field a priority is held in.
2901pub const PRIORITY_FIELD: &str = "Priority";
2902
2903/// The four priorities a board option can hold, in the order a new `Priority` field lists
2904/// them. `none` is not among them: it is the field holding no value.
2905///
2906/// This list mirrors `Priority`, so it carries its own drift gate, in the shape [`CATEGORIES`]
2907/// does: [`level_position`] is a wildcard-free match, so a priority added to the shared
2908/// vocabulary fails to compile until it is placed there, and this crate's suite reconciles
2909/// this list and [`PriorityMappingConfig`]'s members against that enum's own derived schema.
2910pub const PRIORITY_LEVELS: [Priority; 4] = [
2911 Priority::Urgent,
2912 Priority::High,
2913 Priority::Medium,
2914 Priority::Low,
2915];
2916
2917/// Where one priority sits in [`PRIORITY_LEVELS`], or `None` for `none`, which is no option;
2918/// see that list for what this pins.
2919#[must_use]
2920pub const fn level_position(priority: Priority) -> Option<usize> {
2921 match priority {
2922 Priority::None => None,
2923 Priority::Urgent => Some(0),
2924 Priority::High => Some(1),
2925 Priority::Medium => Some(2),
2926 Priority::Low => Some(3),
2927 }
2928}
2929
2930/// This instance's complete priority-to-option mapping, read in both directions.
2931///
2932/// One option per level, held in [`PRIORITY_LEVELS`] order, once it is established that no
2933/// two levels name one option.
2934#[derive(Debug, Clone)]
2935struct PriorityMapping {
2936 options: [PriorityOptionName; 4],
2937}
2938
2939impl PriorityMapping {
2940 fn resolve(config: PriorityMappingConfig, instance: &SourceName) -> Result<Self, SourceError> {
2941 let shipped = |name: &str| PriorityOptionName(name.to_owned());
2942 let mapping = Self {
2943 options: [
2944 config.urgent.unwrap_or_else(|| shipped("Urgent")),
2945 config.high.unwrap_or_else(|| shipped("High")),
2946 config.medium.unwrap_or_else(|| shipped("Medium")),
2947 config.low.unwrap_or_else(|| shipped("Low")),
2948 ],
2949 };
2950 for (index, option) in mapping.options.iter().enumerate() {
2951 if let Some(earlier) = mapping.options[..index]
2952 .iter()
2953 .position(|other| other.as_str().eq_ignore_ascii_case(option.as_str()))
2954 {
2955 return Err(SourceError::Config {
2956 message: format!(
2957 "priority_mapping of source {instance} sends both {} and {} to the board \
2958 option {:?}; one option cannot read back as two priorities",
2959 PRIORITY_LEVELS[earlier],
2960 PRIORITY_LEVELS[index],
2961 option.as_str()
2962 ),
2963 });
2964 }
2965 }
2966 Ok(mapping)
2967 }
2968
2969 /// The option `priority` lands on, or `None` for `none`, which is no option at all.
2970 fn option(&self, priority: Priority) -> Option<&str> {
2971 level_position(priority).map(|index| self.options[index].as_str())
2972 }
2973
2974 /// The priority a board option name reports, or `None` when nothing maps to it.
2975 fn priority_of(&self, option: &str) -> Option<Priority> {
2976 self.options
2977 .iter()
2978 .position(|name| name.as_str().eq_ignore_ascii_case(option))
2979 .map(|index| PRIORITY_LEVELS[index])
2980 }
2981
2982 /// Every mapped option name, in the order a new `Priority` field lists them.
2983 fn names(&self) -> impl Iterator<Item = &str> {
2984 self.options.iter().map(PriorityOptionName::as_str)
2985 }
2986}
2987
2988/// One metadata value this source projects onto a board text field.
2989#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2990#[serde(deny_unknown_fields)]
2991pub struct MetadataFieldConfig {
2992 /// The name of the board text field the value is written to.
2993 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.
2994 /// The top-level metadata key the value is read from. A key under the reserved
2995 /// `onetaskgraph.` prefix is refused.
2996 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.
2997 /// Object keys walked inside that key's value, outermost first. Absent or empty, the
2998 /// key's own value is the one projected. Left out of a serialized entry when empty.
2999 #[serde(default, skip_serializing_if = "Vec::is_empty")]
3000 // Kept in the schema as `"default": []`, for the reason `metadata_fields`' is.
3001 #[schemars(!skip_serializing_if)]
3002 pub path: Vec<String>,
3003}
3004
3005/// The board fields no metadata value may be projected onto, compared ignoring case: the
3006/// three this source writes itself, and those GitHub owns on every board.
3007// 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.
3008const UNPROJECTABLE_FIELDS: [&str; 13] = [
3009 STATUS_FIELD,
3010 PRIORITY_FIELD,
3011 ORIGIN_FIELD,
3012 "Title",
3013 "Assignees",
3014 "Labels",
3015 "Linked pull requests",
3016 "Milestone",
3017 "Repository",
3018 "Reviewers",
3019 "Parent issue",
3020 "Sub-issues progress",
3021 "Type",
3022];
3023
3024/// One validated [`MetadataFieldConfig`] entry.
3025#[derive(Debug, Clone)]
3026struct MetadataField {
3027 field: ProjectedField,
3028 key: ProjectedKey,
3029 path: Vec<String>,
3030}
3031
3032/// The name of a board text field a metadata value is projected onto: never blank, never one
3033/// this source or GitHub writes, and never one another entry of the same source names.
3034#[derive(Debug, Clone, PartialEq, Eq)]
3035struct ProjectedField(String);
3036
3037/// The top-level metadata key a projected value is read from: never blank, and never under
3038/// the namespace this product reserves.
3039#[derive(Debug, Clone, PartialEq, Eq)]
3040struct ProjectedKey(String);
3041
3042impl std::ops::Deref for ProjectedField {
3043 type Target = str;
3044 fn deref(&self) -> &str {
3045 &self.0
3046 }
3047}
3048
3049impl std::ops::Deref for ProjectedKey {
3050 type Target = str;
3051 fn deref(&self) -> &str {
3052 &self.0
3053 }
3054}
3055
3056/// What one item's metadata says one projected field should hold.
3057#[derive(Debug, Clone, PartialEq, Eq)]
3058enum Projected {
3059 /// A string to hold.
3060 Text(String),
3061 /// Nothing: the key or a step of the path is absent, or the value is `null`.
3062 Nothing,
3063}
3064
3065impl MetadataField {
3066 /// Validate every entry of one instance's `metadata_fields`, refusing the first that
3067 /// cannot stand, naming the source and the entry.
3068 fn resolve(
3069 configured: Vec<MetadataFieldConfig>,
3070 instance: &SourceName,
3071 ) -> Result<Vec<Self>, SourceError> {
3072 let mut resolved: Vec<Self> = Vec::with_capacity(configured.len());
3073 for (index, entry) in configured.into_iter().enumerate() {
3074 let refuse = |why: String| SourceError::Config {
3075 message: format!(
3076 "metadata_fields[{index}] of source {instance} (field {:?}, key {:?}) {why}",
3077 entry.field, entry.key
3078 ),
3079 };
3080 if entry.field.trim().is_empty() {
3081 return Err(refuse("names a blank field".to_owned()));
3082 }
3083 if entry.key.trim().is_empty() {
3084 return Err(refuse("names a blank key".to_owned()));
3085 }
3086 if entry.key.split('.').next() == Some(MetadataKey::RESERVED_NAMESPACE) {
3087 return Err(refuse(format!(
3088 "names a key under the reserved \"{}.\" prefix, which this product keeps \
3089 for its own keys; next: project a caller key instead",
3090 MetadataKey::RESERVED_NAMESPACE
3091 )));
3092 }
3093 if let Some(owned) = UNPROJECTABLE_FIELDS
3094 .iter()
3095 .find(|owned| owned.eq_ignore_ascii_case(&entry.field))
3096 {
3097 return Err(refuse(format!(
3098 "names the board field {owned:?}, which this source or GitHub already \
3099 writes; next: name a text field of your own"
3100 )));
3101 }
3102 if let Some(earlier) = resolved
3103 .iter()
3104 .position(|other| other.field.eq_ignore_ascii_case(&entry.field))
3105 {
3106 return Err(refuse(format!(
3107 "names the same board field as metadata_fields[{earlier}]; one field \
3108 cannot hold two values"
3109 )));
3110 }
3111 resolved.push(Self {
3112 field: ProjectedField(entry.field),
3113 key: ProjectedKey(entry.key),
3114 path: entry.path,
3115 });
3116 }
3117 Ok(resolved)
3118 }
3119
3120 /// The path as a refusal spells it: `[]` for the key's own value.
3121 fn spelled_path(&self) -> String {
3122 serde_json::to_string(&self.path).unwrap_or_else(|_| format!("{:?}", self.path))
3123 }
3124
3125 /// What `metadata` says this field should hold, or the refusal for a value no text field
3126 /// can hold.
3127 fn projected(
3128 &self,
3129 metadata: &BTreeMap<String, Value>,
3130 instance: &SourceName,
3131 ) -> Result<Projected, SourceError> {
3132 let mut value = metadata.get(&*self.key);
3133 for step in &self.path {
3134 value = value.and_then(|held| held.get(step.as_str()));
3135 }
3136 let found = match value {
3137 None | Some(Value::Null) => return Ok(Projected::Nothing),
3138 Some(Value::String(text)) => return Ok(Projected::Text(text.clone())),
3139 Some(Value::Bool(_)) => "a boolean",
3140 Some(Value::Number(_)) => "a number",
3141 Some(Value::Array(_)) => "an array",
3142 Some(Value::Object(_)) => "an object",
3143 };
3144 Err(SourceError::Refused {
3145 message: format!(
3146 "source {instance} projects metadata key {:?} at path {} onto its board text \
3147 field {:?}, and this item holds {found} there; only a string, null or nothing \
3148 can be written to a text field; next: store a string there, or remove the \
3149 value",
3150 &*self.key,
3151 self.spelled_path(),
3152 &*self.field
3153 ),
3154 })
3155 }
3156}
3157
3158/// What one item's `Priority` field says, read through this instance's mapping.
3159#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
3160enum HeldPriority {
3161 /// A priority this source reports: an option the mapping names, or no value (`none`).
3162 Read(Priority),
3163 /// An option the mapping does not name, which is never read as a level or as `none`.
3164 Unmapped(String),
3165}
3166
3167/// How fast this source writes, and how long it waits out a rate-limit refusal.
3168///
3169/// Configurable because a GitHub Enterprise installation sets its own limits and an
3170/// operator who has already been refused may want to go slower still — not because the
3171/// defaults are guesses.
3172#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3173#[serde(default, deny_unknown_fields)]
3174pub struct PacingConfig {
3175 /// Shortest interval between two content-creating mutations, in milliseconds.
3176 ///
3177 /// Zero sends them as fast as they are asked for, which is what a fixture server on
3178 /// loopback wants and what no board on github.com does. At most [`MAX_PACING_MS`].
3179 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.
3180 /// First wait when a rate-limit refusal carries no hint, in milliseconds. Each
3181 /// further wait of the same call doubles it. At most [`MAX_PACING_MS`], and never
3182 /// zero while there is a budget to spend, because a schedule of zero-length waits
3183 /// consumes none of it and so never ends.
3184 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.
3185 /// Total time one call may spend waiting out rate limits, in milliseconds.
3186 ///
3187 /// Zero reports the refusal rather than waiting at all. At most [`MAX_PACING_MS`]:
3188 /// the bound is what makes this a wait rather than a hang.
3189 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.
3190}
3191
3192/// The largest any pacing setting may be, in milliseconds.
3193///
3194/// One hour. GitHub's own harshest published bound on content-generating requests works
3195/// out at one every 7.2 seconds, so an hour is already three orders of magnitude past
3196/// anything a real limit asks for, and past it the settings stop describing pacing at all:
3197/// a wait budget beyond it is the unbounded wait this whole mechanism exists to replace,
3198/// and an interval beyond it is a command that never sends its second mutation. It also
3199/// keeps the clock arithmetic in [`GitHubProjectsSource::reserve_mutation_slot`] inside
3200/// what a `Duration` can hold on every platform.
3201pub const MAX_PACING_MS: u64 = 3_600_000;
3202
3203/// [`PacingConfig`] with every default resolved and every value checked, which is what the
3204/// source holds.
3205///
3206/// Unset, `min_mutation_interval_ms` spaces nothing, and every request is paced by GitHub's
3207/// secondary windows alone — see the `pacing` module; set, it spaces content-creating mutations
3208/// on top of those windows, [`MIN_MUTATION_INTERVAL_MS`] being the spacing that holds a sustained
3209/// burst under the per-minute bound by spacing alone; and zero sends every request as fast as it
3210/// is asked for, windows included.
3211#[derive(Debug, Clone, Copy)]
3212struct Pacing {
3213 /// Whether requests are held for GitHub's secondary windows: always, unless
3214 /// `min_mutation_interval_ms` is set to exactly zero, which has always asked for no
3215 /// pacing at all — what a fixture server on loopback wants and no board on github.com does.
3216 windows: bool,
3217 min_mutation_interval: Duration,
3218 retry_backoff: Duration,
3219 retry_budget: Duration,
3220}
3221
3222impl Pacing {
3223 /// Resolve one instance's pacing, refusing a configuration that would not pace at all.
3224 fn resolve(config: PacingConfig, instance: &SourceName) -> Result<Self, SourceError> {
3225 let bounded = |value: Option<u64>, default: u64, field: &str| match value {
3226 Some(value) if value > MAX_PACING_MS => Err(SourceError::Config {
3227 message: format!(
3228 "pacing.{field} of source {instance} is {value} ms, and the most any pacing \
3229 setting may be is {MAX_PACING_MS} ms — an hour, which is already far past \
3230 GitHub's own harshest published limit"
3231 ),
3232 }),
3233 Some(value) => Ok(Duration::from_millis(value)),
3234 None => Ok(Duration::from_millis(default)),
3235 };
3236 let retry_backoff = bounded(
3237 config.retry_backoff_ms,
3238 RETRY_BACKOFF_MS,
3239 "retry_backoff_ms",
3240 )?;
3241 let retry_budget = bounded(config.retry_budget_ms, RETRY_BUDGET_MS, "retry_budget_ms")?;
3242 if retry_backoff.is_zero() && !retry_budget.is_zero() {
3243 return Err(SourceError::Config {
3244 message: format!(
3245 "pacing.retry_backoff_ms of source {instance} is 0 while \
3246 pacing.retry_budget_ms is {} ms; a schedule of zero-length waits spends \
3247 none of that budget, so it would retry a refusal forever. Set a backoff of \
3248 at least 1 ms, or set retry_budget_ms to 0 to report a refusal without \
3249 waiting at all",
3250 retry_budget.as_millis()
3251 ),
3252 });
3253 }
3254 Ok(Self {
3255 windows: config.min_mutation_interval_ms != Some(0),
3256 min_mutation_interval: bounded(
3257 config.min_mutation_interval_ms,
3258 0,
3259 "min_mutation_interval_ms",
3260 )?,
3261 retry_backoff,
3262 retry_budget,
3263 })
3264 }
3265}
3266
3267/// Factory for [`GitHubProjectsSource`].
3268#[derive(Debug, Clone, Copy, Default)]
3269pub struct Plugin;
3270
3271impl SourcePlugin for Plugin {
3272 fn kind(&self) -> &'static str {
3273 KIND
3274 }
3275 /// The endpoint, the owner and the board's number: what names one board.
3276 fn remote_identity(&self, config: &Value) -> Option<String> {
3277 let config: GitHubProjectsConfig = serde_json::from_value(config.clone()).ok()?;
3278 Some(format!(
3279 "{}\n{}\n{}",
3280 config.endpoint, config.owner, config.project_number
3281 ))
3282 }
3283 fn config_schema(&self) -> Schema {
3284 schema_for!(GitHubProjectsConfig)
3285 }
3286 fn build(
3287 &self,
3288 name: &SourceName,
3289 config: &Value,
3290 secrets: &dyn SecretResolver,
3291 ) -> Result<Box<dyn TaskSource>, SourceError> {
3292 self.build_recording_into(name, config, secrets, Arc::new(Accounting::new()))
3293 }
3294
3295 fn build_with_clock(
3296 &self,
3297 name: &SourceName,
3298 config: &Value,
3299 secrets: &dyn SecretResolver,
3300 clock: SharedClock,
3301 ) -> Result<Box<dyn TaskSource>, SourceError> {
3302 self.build_recording_with_clock(name, config, secrets, Arc::new(Accounting::new()), clock)
3303 }
3304}
3305
3306/// The client `cell` holds, building it with `build` the first time it is asked for.
3307///
3308/// A failed build is the request's failure, with the message the source's construction gave it
3309/// before clients were deferred.
3310// 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`.
3311fn built(
3312 cell: &std::sync::OnceLock<Client>,
3313 build: impl FnOnce() -> Result<Client, SourceError>,
3314) -> Result<&Client, SourceError> {
3315 if let Some(client) = cell.get() {
3316 return Ok(client);
3317 }
3318 let client = build()?;
3319 Ok(cell.get_or_init(|| client))
3320}
3321
3322impl Plugin {
3323 /// Build a source recording every request it sends into an accounting the caller holds.
3324 ///
3325 /// [`SourcePlugin::build`] is this with an accounting of its own, which is what the
3326 /// registry gets. This is for a caller that is also calling GitHub itself and wants one
3327 /// session total rather than two — see [`accounting`] and
3328 /// [`GitHubProjectsSource::recording_into`].
3329 ///
3330 /// # Errors
3331 ///
3332 /// Exactly [`SourcePlugin::build`]'s, with the same source name in front of each:
3333 /// [`SourceError::Config`] for configuration this plugin cannot use and
3334 /// [`SourceError::Auth`] for a credential it cannot find.
3335 pub fn build_recording_into(
3336 &self,
3337 name: &SourceName,
3338 config: &Value,
3339 secrets: &dyn SecretResolver,
3340 ledger: Arc<Accounting>,
3341 ) -> Result<Box<dyn TaskSource>, SourceError> {
3342 self.build_recording_with_clock(name, config, secrets, ledger, system_clock())
3343 }
3344
3345 fn build_recording_with_clock(
3346 &self,
3347 name: &SourceName,
3348 config: &Value,
3349 secrets: &dyn SecretResolver,
3350 ledger: Arc<Accounting>,
3351 clock: SharedClock,
3352 ) -> Result<Box<dyn TaskSource>, SourceError> {
3353 let config: GitHubProjectsConfig =
3354 serde_json::from_value(config.clone()).map_err(|e| SourceError::Config {
3355 message: format!("source {name}: {e}"),
3356 })?;
3357 let prefix = format!("source {name}: ");
3358 let mut source = GitHubProjectsSource::recording_into(name, config, secrets, ledger)
3359 .map_err(|error| match error {
3360 // The shared `StatusMapping::distinct` names the source itself.
3361 SourceError::Config { message } if message.starts_with(&prefix) => {
3362 SourceError::Config { message }
3363 }
3364 SourceError::Config { message } => SourceError::Config {
3365 message: format!("{prefix}{message}"),
3366 },
3367 SourceError::Auth { message } => SourceError::Auth {
3368 message: format!("source {name}: {message}"),
3369 },
3370 other => other,
3371 })?;
3372 source.clock = clock;
3373 Ok(Box::new(source))
3374 }
3375}
3376
3377/// Where a status category lands on this board, once configuration is resolved.
3378#[derive(Debug, Clone, PartialEq, Eq)]
3379enum StatusTarget {
3380 /// Not usable against this instance for this kind, and why.
3381 Disabled(UnmappedStatus),
3382 /// The board's `Status` option of this name.
3383 Column(ColumnName),
3384 /// A closed issue, with both its board option and the reason that says which closed it means.
3385 // 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.
3386 Terminal(ColumnName, ClosedState),
3387}
3388
3389/// Every status category, in the order the vocabulary declares them.
3390///
3391/// This list mirrors `StatusCategory`, so it carries its own drift gate rather than a
3392/// reviewer's attention: [`category_position`] is a wildcard-free match, so a variant
3393/// added to the shared vocabulary fails to compile until it is named there, and this
3394/// crate's suite reconciles this list against that enum's own derived schema, which is
3395/// generated from the variants rather than written beside them. The schema is what
3396/// catches a list left one short — a list checking only the positions it already holds
3397/// would pass while every mapping indexed by the new position panicked.
3398pub const CATEGORIES: [StatusCategory; 8] = [
3399 StatusCategory::Draft,
3400 StatusCategory::Backlog,
3401 StatusCategory::Todo,
3402 StatusCategory::Queued,
3403 StatusCategory::InProgress,
3404 StatusCategory::Done,
3405 StatusCategory::Cancelled,
3406 StatusCategory::Unknown,
3407];
3408
3409/// Where one category sits in [`CATEGORIES`]; see that list for what this pins.
3410#[must_use]
3411pub const fn category_position(category: StatusCategory) -> usize {
3412 match category {
3413 StatusCategory::Draft => 0,
3414 StatusCategory::Backlog => 1,
3415 StatusCategory::Todo => 2,
3416 StatusCategory::Queued => 3,
3417 StatusCategory::InProgress => 4,
3418 StatusCategory::Done => 5,
3419 StatusCategory::Cancelled => 6,
3420 StatusCategory::Unknown => 7,
3421 }
3422}
3423
3424/// The spelling a status category is configured and reported under.
3425fn category_name(category: StatusCategory) -> &'static str {
3426 match category {
3427 StatusCategory::Draft => "draft",
3428 StatusCategory::Backlog => "backlog",
3429 StatusCategory::Todo => "todo",
3430 StatusCategory::Queued => "queued",
3431 StatusCategory::InProgress => "in-progress",
3432 StatusCategory::Done => "done",
3433 StatusCategory::Cancelled => "cancelled",
3434 StatusCategory::Unknown => "unknown",
3435 }
3436}
3437
3438/// A shipped default's option name.
3439///
3440/// The literals below are this file's own and non-blank, and they are validated by the
3441/// one constructor a configured name goes through rather than beside it.
3442fn shipped_column(name: &'static str) -> ColumnName {
3443 ColumnName::try_from(name.to_owned()).expect("a shipped default names a board option")
3444}
3445
3446/// The shipped default for one category this instance's `status_mapping` does not mention,
3447/// for either kind.
3448fn shipped_default(category: StatusCategory) -> StatusTarget {
3449 match category {
3450 StatusCategory::Backlog => StatusTarget::Column(shipped_column("Backlog")),
3451 StatusCategory::Todo => StatusTarget::Column(shipped_column("Todo")),
3452 StatusCategory::Queued => StatusTarget::Column(shipped_column("Queued")),
3453 StatusCategory::InProgress => StatusTarget::Column(shipped_column("In Progress")),
3454 StatusCategory::Done => {
3455 StatusTarget::Terminal(shipped_column("Done"), ClosedState::Completed)
3456 }
3457 StatusCategory::Cancelled => {
3458 StatusTarget::Terminal(shipped_column("Cancelled"), ClosedState::NotPlanned)
3459 }
3460 StatusCategory::Draft | StatusCategory::Unknown => {
3461 StatusTarget::Disabled(UnmappedStatus::Unconfigured)
3462 }
3463 }
3464}
3465
3466/// The two kinds a status is written and read for, each with its own half of the mapping.
3467const STATUS_KINDS: [ItemKind; 2] = [ItemKind::Task, ItemKind::Project];
3468
3469/// This instance's complete category-to-target mapping for each kind, read in both
3470/// directions.
3471///
3472/// One target per category per kind, held at that category's own [`category_position`], so
3473/// a category missing from the mapping, named twice in it, or filed out of order is a state
3474/// this type cannot hold rather than one [`Self::target`] has to defend against. Both kinds'
3475/// targets are options of the board's one `Status` field.
3476#[derive(Debug, Clone)]
3477struct BoardStatuses {
3478 tasks: [StatusTarget; CATEGORIES.len()],
3479 projects: [StatusTarget; CATEGORIES.len()],
3480}
3481
3482impl BoardStatuses {
3483 /// Resolve `configured` against the shipped defaults, refusing two categories one kind
3484 /// would read back from one option.
3485 ///
3486 /// A category the mapping does not mention keeps its shipped default for both kinds; one
3487 /// it does mention is exactly what it configures, so a per-kind object leaves the kind it
3488 /// omits unmapped rather than defaulted.
3489 fn resolve(configured: &StatusMapping, instance: &SourceName) -> Result<Self, SourceError> {
3490 let resolve_kind =
3491 |kind: ItemKind| -> Result<[StatusTarget; CATEGORIES.len()], SourceError> {
3492 // `CATEGORIES[position] == category` for every category — the crate's suite
3493 // asserts it — so mapping the list in order fills each category's own slot.
3494 let mut targets = CATEGORIES.map(shipped_default);
3495 for (slot, category) in targets.iter_mut().zip(CATEGORIES) {
3496 if !configured.mentions(category) {
3497 continue;
3498 }
3499 *slot = match configured.name_for(category, kind) {
3500 Err(why) => StatusTarget::Disabled(why),
3501 Ok(name) => {
3502 let option = ColumnName::try_from(name.as_str().to_owned())
3503 .map_err(|message| SourceError::Config { message })?;
3504 match category {
3505 StatusCategory::Done => {
3506 StatusTarget::Terminal(option, ClosedState::Completed)
3507 }
3508 StatusCategory::Cancelled => {
3509 StatusTarget::Terminal(option, ClosedState::NotPlanned)
3510 }
3511 _ => StatusTarget::Column(option),
3512 }
3513 }
3514 };
3515 }
3516 StatusMapping::distinct(
3517 instance,
3518 kind,
3519 CATEGORIES
3520 .iter()
3521 .zip(&targets)
3522 .filter_map(|(category, target)| target.option().map(|o| (*category, o))),
3523 )?;
3524 Ok(targets)
3525 };
3526 Ok(Self {
3527 tasks: resolve_kind(ItemKind::Task)?,
3528 projects: resolve_kind(ItemKind::Project)?,
3529 })
3530 }
3531
3532 /// Every category's target for `kind`, in category order.
3533 const fn targets(&self, kind: ItemKind) -> &[StatusTarget; CATEGORIES.len()] {
3534 match kind {
3535 ItemKind::Task => &self.tasks,
3536 ItemKind::Project => &self.projects,
3537 }
3538 }
3539
3540 fn target(&self, kind: ItemKind, category: StatusCategory) -> &StatusTarget {
3541 &self.targets(kind)[category_position(category)]
3542 }
3543
3544 /// The category a board option name reports for `kind`, or `None` when nothing of that
3545 /// kind maps to it.
3546 fn category_of(&self, kind: ItemKind, option: &str) -> Option<StatusCategory> {
3547 CATEGORIES.into_iter().find(|category| {
3548 self.target(kind, *category)
3549 .option()
3550 .is_some_and(|name| name.eq_ignore_ascii_case(option))
3551 })
3552 }
3553
3554 /// Every option name either kind maps a category to, each once ignoring case, in
3555 /// category order with a task's name before a project's — what the guarded setup asks
3556 /// the `Status` field to hold.
3557 fn wanted(&self) -> Vec<String> {
3558 let mut wanted: Vec<String> = Vec::new();
3559 for category in CATEGORIES {
3560 for kind in STATUS_KINDS {
3561 if let Some(name) = self.target(kind, category).option()
3562 && !wanted.iter().any(|held| held.eq_ignore_ascii_case(name))
3563 {
3564 wanted.push(name.to_owned());
3565 }
3566 }
3567 }
3568 wanted
3569 }
3570
3571 /// The status an item of `kind` reports, from the three things a read of it says: its
3572 /// board `Status` option, whether its issue is closed, and the reason it was closed with.
3573 ///
3574 /// **A closed issue's category follows its closure**, by [`closed_category`]: completed
3575 /// or no reason reads `done`, not planned and duplicate read `cancelled`, and any other
3576 /// reason reads `unknown`. `REOPENED`-while-closed is a state this source can never
3577 /// produce, so it is read permissively rather than refused — reads are faithful, and
3578 /// refusals belong on writes.
3579 ///
3580 /// **Its name follows the closure too, unless the option agrees with it.** The option's
3581 /// own name is kept when this kind's mapping reads that option as the same category, or
3582 /// does not name it at all — so a closed issue in a "Shipped" column reports `done`
3583 /// named `Shipped`. An option the mapping reads as another category contradicts the
3584 /// closure, and so does no option at all: the name is then the option the mapping gives
3585 /// the closure's category, so an issue closed as completed at `Todo` reads `done` named
3586 /// by the board's `done` option, and a destination reading `Todo` as todo is never handed
3587 /// it. An `unknown` closure keeps the option, or `Closed` without one.
3588 ///
3589 /// An open item's option reads through its own kind's mapping, and an option that mapping
3590 /// does not name reads as `Unknown` under its own name.
3591 ///
3592 /// One function of those three rather than of a response, so a narrow status write can
3593 /// answer what a re-read would report by applying it to the state it has just written.
3594 fn status(
3595 &self,
3596 kind: ItemKind,
3597 option: Option<&str>,
3598 closed: bool,
3599 reason: Option<&str>,
3600 ) -> Status {
3601 if closed {
3602 let category = closed_category(reason);
3603 let agrees = option.filter(|option| {
3604 category == StatusCategory::Unknown
3605 || self
3606 .category_of(kind, option)
3607 .is_none_or(|read| read == category)
3608 });
3609 let name = agrees.or_else(|| self.target(kind, category).option());
3610 let fallback = match category {
3611 StatusCategory::Done => "Done",
3612 StatusCategory::Cancelled => "Cancelled",
3613 _ => "Closed",
3614 };
3615 return Status {
3616 category,
3617 name: name.or(option).unwrap_or(fallback).to_owned(),
3618 };
3619 }
3620 let name = option.unwrap_or("Open").to_owned();
3621 Status {
3622 category: self
3623 .category_of(kind, &name)
3624 .unwrap_or(StatusCategory::Unknown),
3625 name,
3626 }
3627 }
3628
3629 /// The option a closed item's `Status` has to hold for its card to agree with its
3630 /// closure, and the category that closure reads as — or `None` when the item is open,
3631 /// its closure reads `unknown`, or its option already agrees.
3632 ///
3633 /// What `task status reconcile` writes: only `done` and `cancelled` closures are brought
3634 /// in line, each to the one option the mapping gives its category — an item with no
3635 /// option, or at one the mapping does not name, included.
3636 fn contradicted(
3637 &self,
3638 kind: ItemKind,
3639 option: Option<&str>,
3640 closed: bool,
3641 reason: Option<&str>,
3642 ) -> Option<(ClosedAs, &str)> {
3643 if !closed {
3644 return None;
3645 }
3646 let closed_as = match closed_category(reason) {
3647 StatusCategory::Done => ClosedAs::Done,
3648 StatusCategory::Cancelled => ClosedAs::Cancelled,
3649 _ => return None,
3650 };
3651 let wanted = self.target(kind, closed_as.category()).option()?;
3652 if option.is_some_and(|option| option.eq_ignore_ascii_case(wanted)) {
3653 return None;
3654 }
3655 Some((closed_as, wanted))
3656 }
3657}
3658
3659/// The category a closed issue reads as, from the reason it was closed with.
3660///
3661/// `COMPLETED`, and no reason at all, is finished work; `NOT_PLANNED` and `DUPLICATE` are both
3662/// work nobody will do here — a duplicate's work is the other issue's — and any other reason
3663/// is one this source cannot read a category from.
3664fn closed_category(reason: Option<&str>) -> StatusCategory {
3665 match reason {
3666 None | Some("COMPLETED") => StatusCategory::Done,
3667 Some("NOT_PLANNED" | "DUPLICATE") => StatusCategory::Cancelled,
3668 Some(_) => StatusCategory::Unknown,
3669 }
3670}
3671
3672impl BoardStatuses {
3673 /// For each kind, the option names it maps a category to that `existing` lacks, ignoring
3674 /// case; a kind lacking none is left out.
3675 fn missing_by_kind(&self, existing: &[StatusOption]) -> Vec<KindMissing> {
3676 STATUS_KINDS
3677 .into_iter()
3678 .filter_map(|kind| {
3679 let missing: Vec<String> = self
3680 .targets(kind)
3681 .iter()
3682 .filter_map(StatusTarget::option)
3683 .filter(|wanted| {
3684 !existing
3685 .iter()
3686 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3687 })
3688 .map(str::to_owned)
3689 .collect();
3690 (!missing.is_empty()).then_some(KindMissing { kind, missing })
3691 })
3692 .collect()
3693 }
3694}
3695
3696impl StatusTarget {
3697 /// The board option this target selects, or `None` for an unmapped one.
3698 fn option(&self) -> Option<&str> {
3699 match self {
3700 Self::Column(name) | Self::Terminal(name, _) => Some(name.as_str()),
3701 Self::Disabled(_) => None,
3702 }
3703 }
3704}
3705
3706// 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.
3707/// One repository this source can create an issue in, as `owner/name`.
3708///
3709/// Every `createIssue` this source sends names one of these: the item's own single
3710/// `repositories` entry — on a source declared private, only when it reads private — else
3711/// its parent project issue's repository, else the configured
3712/// [`GitHubProjectsConfig::repository`]. [`GitHubProjectsSource::creation_target`] makes
3713/// that choice and says what it refuses before `createIssue`.
3714// llmlint: ignore-end[comments_earn_their_place, contracts_have_one_source_or_a_drift_gate]
3715#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
3716#[serde(try_from = "String", into = "String")]
3717struct RepositoryTarget {
3718 owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
3719 name: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
3720}
3721
3722impl RepositoryTarget {
3723 fn parse(value: &str) -> Result<Self, SourceError> {
3724 let (owner, name) = value.split_once('/').ok_or_else(|| SourceError::Config {
3725 message: format!(
3726 "repository must be spelled owner/name; {value:?} names no repository"
3727 ),
3728 })?;
3729 if !valid_github_owner(owner) || !valid_github_repository_name(name) {
3730 return Err(SourceError::Config {
3731 message: format!(
3732 "repository must be spelled owner/name with a GitHub login and one \
3733 repository name; {value:?} is not"
3734 ),
3735 });
3736 }
3737 Ok(Self {
3738 owner: owner.to_owned(),
3739 name: name.to_owned(),
3740 })
3741 }
3742
3743 /// The one host whose repositories this source creates issues in, spelled once: it is
3744 /// what [`Self::origin`] renders and what [`Self::from_origin`] accepts.
3745 const HOST: &str = "github.com";
3746
3747 fn origin(&self) -> String {
3748 format!("{}/{}/{}", Self::HOST, self.owner, self.name)
3749 }
3750
3751 /// The repository a normalized origin names, or why it is none this source can create
3752 /// an issue in: another host, or more or fewer than `owner/name` under this one.
3753 fn from_origin(origin: &Repository) -> Result<Self, String> {
3754 let not_here = || {
3755 format!(
3756 "{} is not a {}/owner/name repository",
3757 origin.as_str(),
3758 Self::HOST
3759 )
3760 };
3761 let (host, rest) = origin.as_str().split_once('/').ok_or_else(not_here)?;
3762 if host != Self::HOST {
3763 return Err(not_here());
3764 }
3765 Self::parse(rest).map_err(|_| not_here())
3766 }
3767
3768 fn slug(&self) -> String {
3769 format!("{}/{}", self.owner, self.name)
3770 }
3771}
3772
3773/// Read back from a kept context as the `owner/name` it was written as, refused unless
3774/// [`RepositoryTarget::parse`] takes it.
3775impl TryFrom<String> for RepositoryTarget {
3776 type Error = String;
3777
3778 fn try_from(slug: String) -> Result<Self, Self::Error> {
3779 Self::parse(&slug).map_err(|error| error.to_string())
3780 }
3781}
3782
3783impl From<RepositoryTarget> for String {
3784 fn from(target: RepositoryTarget) -> Self {
3785 target.slug()
3786 }
3787}
3788
3789/// A source which reads GitHub afresh for every operation.
3790pub struct GitHubProjectsSource {
3791 /// This source's configured name, used both to tell a far end naming this source
3792 /// from one naming a system it knows nothing about, and to name the instance a
3793 /// status refusal is about.
3794 name: SourceName,
3795 owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after full GitHub-owner validation.
3796 project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after GraphQL-Int validation.
3797 repository: Option<RepositoryTarget>,
3798 /// The logins whose repositories a create may file in; empty restricts nothing.
3799 creation_owners: Vec<String>, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after each entry passes GitHub's login grammar.
3800 endpoint: Url,
3801 token: SecretString,
3802 credential_name: String, // llmlint: ignore[invalid_states_unrepresentable] Private diagnostic value constructed only after environment-name validation.
3803 statuses: BoardStatuses,
3804 /// Where each priority lands on this board, or `None` when this instance holds none.
3805 priorities: Option<PriorityMapping>,
3806 /// The metadata values this instance projects onto board text fields, in configured order.
3807 metadata_fields: Vec<MetadataField>,
3808 /// Built on the first request rather than with the source, as [`Self::client`] says.
3809 client: std::sync::OnceLock<Client>,
3810 /// Built on the first attachment request, as [`Self::asset_client`] says.
3811 asset_client: std::sync::OnceLock<Client>,
3812 /// Every item this source has created in this command, in the order it created them —
3813 /// dropped by [`TaskSource::end_command`].
3814 ///
3815 /// GitHub's `projectV2.items` is eventually consistent: an issue added to a board with
3816 /// `addProjectV2ItemById` is routinely absent from the very next read of that board, so
3817 /// a copy resolving a dependency on an item it had just created refused it as not
3818 /// found. A board read is completed from this — an item remembered here and absent from
3819 /// the read is added back, because the board really does hold it and only the read is
3820 /// behind.
3821 ///
3822 /// It is not a cache of a user's work: nothing is remembered that this process did not
3823 /// itself just write, it lives and dies with the process, and it is never consulted for
3824 /// an item this source did not create.
3825 created: Mutex<Vec<Resolved>>,
3826 /// Every item that already existed and that this source has written in this command, as
3827 /// it wrote it — dropped by [`TaskSource::end_command`].
3828 ///
3829 /// The other half of [`Self::created`], held on the same terms and for the reason a
3830 /// narrowed read needs it: an answer from GitHub's search or from the board's own field
3831 /// filter is an index behind a write this process made moments ago, so a query matching
3832 /// what this process just wrote onto an existing item would otherwise miss it. Nothing
3833 /// is remembered that this process did not itself just write.
3834 updated: Mutex<Vec<Resolved>>,
3835 /// Every issue this source has added a comment to or edited a comment of in this command
3836 /// — dropped by [`TaskSource::end_command`].
3837 ///
3838 /// A comment-activity read is narrowed by GitHub's issue search, whose `updated:` index
3839 /// lags the write that moved an issue's `updatedAt`, and neither [`Self::created`] nor
3840 /// [`Self::updated`] is moved by a comment, so an issue this process had just commented
3841 /// on was missing from such a read — or ruled out by the `updatedAt` its own record held
3842 /// from before — until the index caught up. Each id here is a candidate of every such
3843 /// search-narrowed read, and wherever it is a candidate its comments are read rather than
3844 /// it being ruled out by a stale `updatedAt`; that read is of the issue's own node, so it
3845 /// is current. It holds ids alone: nothing of a comment is remembered. A comment another
3846 /// process wrote is still found only once the index has it.
3847 commented: Mutex<Vec<NativeId>>,
3848 /// How fast this source writes, and how long it waits out a refusal.
3849 pacing: Pacing,
3850 /// When the last content-creating mutation finished, or the moment the furthest-out
3851 /// reserved slot releases the next one, whichever is later — so the one after it can be
3852 /// spaced from that, when `pacing.min_mutation_interval_ms` asks for a spacing. See
3853 /// [`GitHubProjectsSource::paced`] for why completion rather than release is what it is
3854 /// measured from.
3855 last_mutation: Mutex<Option<Duration>>,
3856 /// GitHub's secondary windows as this source has charged them; see the `pacing` module.
3857 windows: Mutex<pacing::Windows>,
3858 /// The tasks a project holds, as a copy's batched read of the project and every one of them
3859 /// found them — see [`TaskSource::read_ahead_for_copy`] — for the length of one command, so
3860 /// a listing of that project's tasks is answered without a read of its own.
3861 task_children: Mutex<BTreeMap<NativeId, Vec<Resolved>>>,
3862 /// The web address of each issue this source created in the current command, as its
3863 /// `createIssue` answered it — what [`TaskSource::written_location`] answers, without a
3864 /// request.
3865 written: Mutex<BTreeMap<NativeId, String>>,
3866 /// What a copy has held back of the items it created, between
3867 /// [`TaskSource::begin_deferred_writes`] and [`TaskSource::finish_writes`] — `None` while
3868 /// nothing is being held back, when every write lands whole as it is made.
3869 deferred: Mutex<Option<Deferred>>,
3870 /// Each existing issue's update held back while writes are, in the order it was made: sent
3871 /// together by [`Self::send_held_updates`] before any other request goes out, so nothing
3872 /// this source sends or reads ever sees the board without them.
3873 held_updates: Mutex<Vec<HeldUpdate>>,
3874 clock: SharedClock,
3875 /// Whether the configuration declares this source private, as
3876 /// [`TaskSource::declare_visibility`] told it: what decides where a new issue whose one
3877 /// named repository reads public is created — see [`Self::creation_rule`].
3878 declared_private: std::sync::atomic::AtomicBool,
3879 numeric_repositories: tokio::sync::Mutex<BTreeMap<RepositoryTarget, std::num::NonZeroU64>>,
3880 /// The board as this process last read it, for the length of one command — dropped by
3881 /// [`TaskSource::end_command`].
3882 ///
3883 /// A copy of a project used to re-read the whole board, paged, before writing each of
3884 /// its items, which is by far the largest part of a copy's request count and none of
3885 /// its work. Nothing else changes this board while a command runs — this source's own
3886 /// writes are the only writer — so one read answers them all.
3887 ///
3888 /// It is not a store of a user's work and it is not the cache the no-persistence
3889 /// invariant forbids: it lives and dies with the process exactly as `created` does,
3890 /// nothing is written down, and [`Self::board`] still completes it from `created`, so
3891 /// an item this command created and then depends on resolves whether or not GitHub's
3892 /// own eventually-consistent read has caught up. A write to an item already on the
3893 /// board updates the entry here too, so what this holds is the last read plus this
3894 /// process's own writes rather than a snapshot taken before them.
3895 board_cache: Mutex<Option<Board>>,
3896 /// Every issue this board's own search reported, for the length of one command — dropped
3897 /// by [`TaskSource::end_command`].
3898 ///
3899 /// The second half of a board read, and cached for the same reason and on the same
3900 /// terms as the first: it lives and dies with the process, nothing is written down, and
3901 /// a write this process makes updates the entry here exactly as it updates the one in
3902 /// [`Self::board_cache`]. One read answers every question a command asks, so a command
3903 /// that lists this board's projects and its tasks pays for one search rather than two.
3904 search_cache: Mutex<Option<Vec<Resolved>>>,
3905 /// What each narrowed question GitHub was asked answered, keyed by that question, for
3906 /// the length of one command — dropped by [`TaskSource::end_command`].
3907 ///
3908 /// The narrowed counterpart of [`Self::search_cache`], held on the same terms: it lives
3909 /// and dies with the process, nothing is written down, a write this process makes
3910 /// updates the entry here as it updates the other two, and every answer is completed
3911 /// with this process's own writes each time it is given. A command that asks the same
3912 /// narrowed question twice — a wait polling for its own items, a listing repeated after a
3913 /// write — pays for it once, which is what the whole-board read it replaced gave it.
3914 narrowed_cache: Mutex<BTreeMap<String, Vec<Resolved>>>,
3915 search_next: Mutex<BTreeMap<String, Option<String>>>,
3916 /// Records already resolved in this command, reused by writes and for comment identity.
3917 /// Explicit item reads still reach GitHub. Nothing is persisted, and
3918 /// [`TaskSource::end_command`] drops every record, so a write in the next command reads
3919 /// its item as a person has since left it.
3920 resolved_cache: Mutex<BTreeMap<NativeId, Resolved>>,
3921 /// What [`TaskSource::read_ahead_for_copy`] read, by id — `None` for an id that read
3922 /// answered as holding nothing — each taken by the next read of that item by id and by
3923 /// nothing else, dropped by any mutation naming it, and cleared by
3924 /// [`TaskSource::end_command`]. So a read answered from it is the copy's own
3925 /// read-before-write, made in the same request as the others' rather than on its own.
3926 read_ahead: Mutex<BTreeMap<NativeId, Option<Resolved>>>,
3927 /// The origins [`TaskSource::read_ahead_origins_for_copy`] was asked about and has not sent
3928 /// yet: held for the [`TaskSource::read_ahead_for_copy`] that follows it, which sends them —
3929 /// beside the members of the one project it names, when it names one — or for the next
3930 /// request this source makes, whichever comes first.
3931 pending_origins: Mutex<Vec<String>>,
3932 /// Each project's members as a lookup carrying them read them in this command, trimmed to
3933 /// their classification and repositories, taken by [`TaskSource::member_classifications`]
3934 /// of that project and by nothing else, and cleared by [`TaskSource::end_command`].
3935 member_classes: Mutex<BTreeMap<NativeId, Vec<MemberClass>>>,
3936 /// Every item a listing of one project's members carried whole in this command — the
3937 /// project issue and each of its sub-issues — kept for [`TaskSource::read_ahead_for_copy`]
3938 /// alone, which takes each one it is asked for into [`Self::read_ahead`] instead of reading
3939 /// it again. No other read is answered from it, so a read by id outside a copy's
3940 /// read-before-write reads GitHub as it always did. Dropped by any mutation naming an
3941 /// item, and cleared by [`TaskSource::end_command`].
3942 carried: Mutex<BTreeMap<NativeId, Resolved>>,
3943 /// Items an issue read by reference reached, held for the one status write each was read
3944 /// for and taken out by it alone — see [`Self::take_held`] — because that read selects only
3945 /// what such a write needs. Dropped by [`TaskSource::end_command`].
3946 held_cache: Mutex<BTreeMap<NativeId, Resolved>>,
3947 /// Each project's sub-issues as GitHub answered them, keyed by the selector they were
3948 /// asked for under, with the project that selector named — for the length of one command,
3949 /// dropped by [`TaskSource::end_command`].
3950 ///
3951 /// A caller pages through a project's tasks one engine page at a time, and every page
3952 /// is cut from the whole list of them, so without this each page walked every
3953 /// [`graphql::SUB_ISSUES`] page again and a project of `n` listing pages cost `n²`
3954 /// requests to read once. Held on the terms [`Self::narrowed_cache`] is: it lives and
3955 /// dies with the process, nothing is written down, a write this process makes updates or
3956 /// removes the entry here as it does there, and every answer is completed with this
3957 /// process's own writes each time it is given.
3958 children_cache: Mutex<ProjectChildren>,
3959 /// The board's own id and field definitions as this process last read them on their
3960 /// own, for the length of one command — dropped by [`TaskSource::end_command`].
3961 ///
3962 /// What a write needs of the board and its item does not say, read once per command
3963 /// rather than once per item written, on the terms [`Self::board_cache`] is held on: it
3964 /// lives and dies with the process and nothing is written down. It holds no item and so
3965 /// can answer no question about one — see [`Self::board_fields`].
3966 fields_cache: Mutex<Option<BoardFields>>,
3967 /// Each destination repository's node id, resolved once per repository
3968 /// rather than per issue created.
3969 ///
3970 /// A repository's node id does not change, and re-reading it for every issue of a copy
3971 /// spent one request per item on an answer this source already had. It is a map rather
3972 /// than one entry because a copy files each item in the repository its own
3973 /// `repositories` field names, so a plan across five repositories asks GitHub five
3974 /// times and not once per item.
3975 repository_cache: Mutex<BTreeMap<RepositoryTarget, String>>,
3976 /// What every request this source sends is recorded into.
3977 ///
3978 /// Ordinary code path, not a mode: [`Self::send_once`] records into it at the one place
3979 /// a request leaves this crate, so nothing has to be switched on for a session to be
3980 /// counted. It is shared rather than owned so a caller accounting for a whole session —
3981 /// its own schema verification, board lookups, residue sweep and cleanup beside this
3982 /// source's reads and writes — adds up one accounting instead of two. See
3983 /// [`accounting`] for what a record carries and what a session's spend is and is not.
3984 ledger: Arc<Accounting>,
3985 /// What [`expected`] holds for a write the store's cache expects: the journal while one is
3986 /// open, the issues primed from the cache, and the comments known on each.
3987 expected: expected::Held,
3988 /// What [`projection`] holds for a producer's projection: a kept placement awaiting its
3989 /// setup-context token, and where the last create was placed.
3990 projection: projection::Held,
3991}
3992
3993/// The board item a single field update's answer names, refused unless it is `item_id`.
3994fn field_update_item<'a>(data: &'a Value, item_id: &str) -> Result<&'a Value, SourceError> {
3995 let returned = data
3996 .pointer("/updateProjectV2ItemFieldValue/projectV2Item")
3997 .ok_or_else(|| SourceError::Malformed {
3998 message: "GitHub field update returned no project item".into(),
3999 })?;
4000 if required_str(returned, "id")? != item_id {
4001 return Err(SourceError::Malformed {
4002 message: "GitHub field update returned the wrong project item".into(),
4003 });
4004 }
4005 Ok(returned)
4006}
4007
4008/// GitHub's closed single-select color vocabulary.
4009#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
4010#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
4011pub enum StatusOptionColor {
4012 /// Gray.
4013 Gray,
4014 /// Blue.
4015 Blue,
4016 /// Green.
4017 Green,
4018 /// Yellow.
4019 Yellow,
4020 /// Purple.
4021 Purple,
4022 /// Red.
4023 Red,
4024 /// Orange.
4025 Orange,
4026 /// Pink.
4027 Pink,
4028}
4029
4030/// Whether a guarded board setup — of the fields, or of the Status options alone — plans or
4031/// applies its additions.
4032#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4033pub enum SetupMode {
4034 /// Read without mutation.
4035 Plan,
4036 /// Apply and verify.
4037 Apply,
4038}
4039
4040/// The name [`SetupMode`] had when Status was the one field set up, kept so a caller written
4041/// against it goes on compiling.
4042pub type StatusOptionsMode = SetupMode;
4043
4044/// The explicit result of the requested operation.
4045#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4046#[serde(rename_all = "kebab-case")]
4047pub enum StatusOptionsOutcome {
4048 /// A read-only plan.
4049 Planned,
4050 /// Apply found nothing missing.
4051 Unchanged,
4052 /// Additions were applied and verified.
4053 Applied,
4054}
4055
4056/// A GitHub single-select option's opaque GraphQL node identifier.
4057#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4058#[serde(transparent)]
4059pub struct StatusOptionId(#[schemars(length(min = 1))] String);
4060
4061impl TryFrom<String> for StatusOptionId {
4062 type Error = String;
4063
4064 fn try_from(id: String) -> Result<Self, Self::Error> {
4065 if id.trim().is_empty() {
4066 return Err("a GitHub Status option id cannot be blank".to_owned());
4067 }
4068 Ok(Self(id))
4069 }
4070}
4071
4072/// One existing or proposed option in a guarded Status-field update.
4073#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4074pub struct StatusOption {
4075 /// GitHub's stable id.
4076 pub id: StatusOptionId,
4077 /// The visible option name.
4078 pub name: ColumnName,
4079 /// GitHub's single-select color token.
4080 pub color: StatusOptionColor,
4081 /// The option description, including an empty one.
4082 pub description: String,
4083}
4084
4085/// One board item's Status assignment, retained as recovery data.
4086#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4087pub struct StatusAssignment {
4088 /// The project item id whose assignment this is.
4089 // llmlint: ignore[invalid_states_unrepresentable] This opaque GraphQL node ID is
4090 // carried verbatim as operator recovery data; introducing a semantic type would claim
4091 // validation rules GitHub does not publish and no operation here interprets.
4092 pub item_id: String,
4093 /// The selected option, absent when the item has no status.
4094 #[serde(skip_serializing_if = "Option::is_none")]
4095 pub option: Option<AssignedStatusOption>,
4096}
4097
4098/// The inseparable id and name of an assigned option.
4099#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4100pub struct AssignedStatusOption {
4101 /// GitHub's stable id.
4102 pub id: StatusOptionId,
4103 /// The visible name.
4104 pub name: ColumnName,
4105}
4106
4107/// The plan and verified outcome of reconciling configured Status options.
4108#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4109pub struct StatusOptionsReport {
4110 /// The configured source name.
4111 pub source: SourceName,
4112 /// Configured option names absent before the operation.
4113 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a
4114 // `ColumnName` and has therefore already passed its nonblank validation; retaining the
4115 // serialized string here preserves the report's intentionally simple public contract.
4116 pub missing: Vec<String>,
4117 /// What the requested operation did.
4118 pub outcome: StatusOptionsOutcome,
4119 /// The complete option list observed before any mutation.
4120 pub existing: Vec<StatusOption>,
4121}
4122
4123#[derive(Debug, Clone, PartialEq, Eq)]
4124struct StatusSnapshot {
4125 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4126 // passed back as the mutation's project identity; a newtype could enforce no stronger
4127 // invariant because GitHub publishes no grammar for it.
4128 board_id: String,
4129 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4130 // passed back as the mutation's field identity; a newtype could enforce no stronger
4131 // invariant because GitHub publishes no grammar for it.
4132 field_id: String,
4133 options: Vec<StatusOption>,
4134 assignments: Vec<StatusAssignment>,
4135}
4136
4137/// The name of the board field a status is held in.
4138const STATUS_FIELD: &str = "Status";
4139
4140/// Every item's value of each field `report` names, as it stood before the setup wrote
4141/// anything — what a person puts back when the setup is refused part way.
4142fn recovery(report: &FieldsReport, before: &BoardSnapshot) -> Result<String, SourceError> {
4143 let assignments: BTreeMap<&str, Vec<StatusAssignment>> = report
4144 .fields
4145 .iter()
4146 .map(|field| (field.field.name(), before.assignments(field.field)))
4147 .collect();
4148 serde_json::to_string_pretty(&assignments).map_err(|error| SourceError::Malformed {
4149 message: format!("cannot render the pre-write field recovery snapshot: {error}"),
4150 })
4151}
4152
4153/// One board field the guarded setup reads and writes — every one it reads, and the only
4154/// ones it writes.
4155#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, schemars::JsonSchema)]
4156pub enum BoardField {
4157 /// The single-select `Status` field every instance's `status_mapping` resolves into.
4158 Status,
4159 /// The single-select `Priority` field an instance's `priority_mapping` resolves into.
4160 Priority,
4161}
4162
4163impl BoardField {
4164 /// The field's name on the board.
4165 #[must_use]
4166 pub const fn name(self) -> &'static str {
4167 match self {
4168 Self::Status => STATUS_FIELD,
4169 Self::Priority => PRIORITY_FIELD,
4170 }
4171 }
4172
4173 /// The field a board calls `name`, or `None` for one this setup does not own.
4174 fn named(name: &str) -> Option<Self> {
4175 [Self::Status, Self::Priority]
4176 .into_iter()
4177 .find(|field| field.name() == name)
4178 }
4179}
4180
4181/// What the guarded setup did to one field.
4182#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4183#[serde(rename_all = "kebab-case")]
4184pub enum FieldOutcome {
4185 /// A read-only plan.
4186 Planned,
4187 /// Apply found the field there with every configured option.
4188 Unchanged,
4189 /// Missing options were added to the field that was there, and verified.
4190 Applied,
4191 /// The field was not there; it was created holding the configured options, and verified.
4192 Created,
4193}
4194
4195/// One field's plan, or its verified outcome.
4196#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4197pub struct FieldReport {
4198 /// Which field.
4199 pub field: BoardField,
4200 /// Whether the board had the field before the operation.
4201 // llmlint: ignore[invalid_states_unrepresentable] `exists` beside `outcome` is the report's
4202 // wire shape as its consumer's contract fixes it — `{"field", "exists", "missing",
4203 // "outcome", "existing"}` — so folding one into the other would change a published JSON
4204 // shape. The contradictory pairings cannot be built: `GitHubProjectsSource::fields` is the
4205 // one constructor, and it derives `outcome` from `exists` in one match.
4206 pub exists: bool,
4207 /// Configured option names the field lacked before the operation — every one of them,
4208 /// in the order a new field lists them, when the field was not there at all.
4209 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
4210 // mapping name and has therefore already passed its nonblank validation; the serialized
4211 // string is the report's intentionally simple public contract, as `StatusOptionsReport`'s is.
4212 pub missing: Vec<String>,
4213 /// For the `Status` field, which item kind each missing name is configured for: one
4214 /// entry per kind `status_mapping` names a missing option for, task before project, each
4215 /// listing that kind's missing names in category order. A name both kinds use is in
4216 /// both. Empty — and left out of the JSON — when nothing is missing, and always for
4217 /// `Priority`, which only a task holds.
4218 #[serde(default, skip_serializing_if = "Vec::is_empty")]
4219 // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
4220 // both SDKs model an absent `kinds` as an empty list rather than as `null`.
4221 #[schemars(!skip_serializing_if)]
4222 pub kinds: Vec<KindMissing>,
4223 /// What the requested operation did.
4224 pub outcome: FieldOutcome,
4225 /// The field's complete option list observed before any mutation; empty when the field
4226 /// was not there.
4227 pub existing: Vec<StatusOption>,
4228}
4229
4230/// The `Status` option names one item kind's `status_mapping` names that the field lacked.
4231#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4232pub struct KindMissing {
4233 /// The kind these names are configured for.
4234 pub kind: ItemKind,
4235 /// The names that kind maps a category to and the field lacked, in category order.
4236 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
4237 // mapping name, as `FieldReport::missing`'s do, and the serialized string is the report's
4238 // intentionally simple public contract.
4239 pub missing: Vec<String>,
4240}
4241
4242/// The plan and verified outcome of setting up every field a source's configuration names.
4243#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4244pub struct FieldsReport {
4245 /// The configured source name.
4246 pub source: SourceName,
4247 /// `Status`, always, and `Priority` when the source sets `priority_mapping`.
4248 // llmlint: ignore[invalid_states_unrepresentable] A list is the report's wire shape as its
4249 // consumer's contract fixes it — `{"source", "fields": [...]}` — so a struct with one member
4250 // per field would change a published JSON shape. The states the list could hold and the
4251 // contract forbids cannot be built: `GitHubProjectsSource::fields` is the one constructor,
4252 // and it pushes `Status` first and exactly once, then `Priority` exactly when configured.
4253 pub fields: Vec<FieldReport>,
4254 /// Each board text field the source's `metadata_fields` projects a value onto, in
4255 /// configured order. Empty — and left out of the JSON — when it projects none.
4256 #[serde(default, skip_serializing_if = "Vec::is_empty")]
4257 // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
4258 // both SDKs model an absent list as an empty one rather than as `null`.
4259 #[schemars(!skip_serializing_if)]
4260 pub metadata_fields: Vec<MetadataFieldReport>,
4261}
4262
4263/// The type of a board field that is not a text field, as GitHub names it — its `dataType`, or
4264/// its GraphQL type when it has none — which is what a projected field's conflict reports.
4265///
4266/// Never empty, never beginning with whitespace, and never `TEXT`: a text field of that name is
4267/// no conflict.
4268// The schema states that rule as `NON_TEXT_FIELD_TYPE_PATTERN`, so a generated SDK model
4269// refuses exactly what `try_from` below does.
4270#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4271#[serde(transparent)]
4272pub struct NonTextFieldType(#[schemars(regex(pattern = NON_TEXT_FIELD_TYPE_PATTERN))] String);
4273
4274/// What a [`NonTextFieldType`] may be, as the emitted schema states it: a first character that
4275/// is not whitespace, and anything but exactly `TEXT` — spelled without a lookahead, which
4276/// neither SDK's validator can be relied on to support.
4277const NON_TEXT_FIELD_TYPE_PATTERN: &str =
4278 r"^(?:[^T\s]|T(?:[^E]|$)|TE(?:[^X]|$)|TEX(?:[^T]|$)|TEXT[\s\S])";
4279
4280impl TryFrom<String> for NonTextFieldType {
4281 type Error = String;
4282
4283 fn try_from(kind: String) -> Result<Self, Self::Error> {
4284 if kind.is_empty() || kind.starts_with(char::is_whitespace) {
4285 return Err("a board field's type cannot be blank or begin with whitespace".to_owned());
4286 }
4287 if kind == "TEXT" {
4288 return Err("a TEXT field is no conflict for a projected text field".to_owned());
4289 }
4290 Ok(Self(kind))
4291 }
4292}
4293
4294impl std::fmt::Display for NonTextFieldType {
4295 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4296 formatter.write_str(&self.0)
4297 }
4298}
4299
4300/// One projected metadata text field's plan, or its verified outcome.
4301///
4302/// The setup creates a missing one as a text field and never alters a field that is there:
4303/// a same-named field of another type is reported in `conflict` and left as it is.
4304#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4305pub struct MetadataFieldReport {
4306 /// The board field's name.
4307 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.
4308 /// The metadata key the value is read from.
4309 pub key: String, // llmlint: ignore[invalid_states_unrepresentable] As `field`: a validated configuration value reported back as written.
4310 /// The object keys walked inside that key's value; empty for the key's own value.
4311 pub path: Vec<String>,
4312 /// Whether the board had a text field of that name before the operation.
4313 // 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.
4314 pub exists: bool,
4315 /// The type of a field of that name that is not a text field, which the setup leaves as
4316 /// it is; absent — and left out of the JSON — when there is none.
4317 #[serde(default, skip_serializing_if = "Option::is_none")]
4318 pub conflict: Option<NonTextFieldType>,
4319 /// What the requested operation did: `planned`; `created` for a field that was not
4320 /// there; `unchanged` for one that was, and for a conflict, which is never changed.
4321 pub outcome: FieldOutcome,
4322}
4323
4324/// Which options one field is configured with, in the order a new field would list them.
4325struct FieldPlan {
4326 field: BoardField,
4327 wanted: Vec<String>,
4328}
4329
4330/// One single-select field as the guarded setup snapshots it.
4331#[derive(Debug, Clone, PartialEq, Eq)]
4332struct SnapshotField {
4333 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4334 // passed back as the mutation's field identity; a newtype could enforce no stronger
4335 // invariant because GitHub publishes no grammar for it.
4336 field_id: String,
4337 options: Vec<StatusOption>,
4338}
4339
4340/// Every single-select field of a board and every item's value of each.
4341#[derive(Debug, Clone, PartialEq, Eq)]
4342struct BoardSnapshot {
4343 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4344 // passed back as the mutation's project identity; a newtype could enforce no stronger
4345 // invariant because GitHub publishes no grammar for it.
4346 board_id: String,
4347 fields: BTreeMap<BoardField, SnapshotField>,
4348 /// Each board item's id, and its value of each field this setup owns that it holds one of.
4349 items: Vec<(String, BTreeMap<BoardField, AssignedStatusOption>)>,
4350}
4351
4352impl BoardSnapshot {
4353 /// Every item's value of `field`, in board order — the recovery data a drift refusal
4354 /// carries.
4355 fn assignments(&self, field: BoardField) -> Vec<StatusAssignment> {
4356 self.items
4357 .iter()
4358 .map(|(item_id, values)| StatusAssignment {
4359 item_id: item_id.clone(),
4360 option: values.get(&field).cloned(),
4361 })
4362 .collect()
4363 }
4364}
4365
4366impl GitHubProjectsSource {
4367 /// Report missing configured Status options and, when `apply` is true, add them with
4368 /// a whole-list mutation that preserves every existing id and verifies the result.
4369 ///
4370 /// # Errors
4371 ///
4372 /// Refuses a board without a single-select `Status` field. A post-write difference in
4373 /// any pre-existing option id or item assignment is refused with the complete pre-write
4374 /// assignment snapshot in the diagnostic for recovery.
4375 // llmlint: ignore[changed_behavior_has_e2e] The CLI journeys cover plan, no-op apply,
4376 // successful mutation, both drift refusals, source selection, missing Status, casing,
4377 // and paging. Transport errors remain the shared `graphql` boundary's behavior rather
4378 // than a new status-options behavior, and the pinned-schema test prevents valid GitHub
4379 // responses from entering the defensive malformed-response branches below.
4380 pub async fn status_options(
4381 &self,
4382 mode: StatusOptionsMode,
4383 ) -> Result<StatusOptionsReport, SourceError> {
4384 let before = self.status_snapshot().await?;
4385 // A terminal category's option is as configured as an open one's: a terminal
4386 // write validates it before closing and refuses when the board lacks it. Both
4387 // kinds' names are options of the one field, so both are asked for.
4388 let missing = self
4389 .statuses
4390 .wanted()
4391 .into_iter()
4392 .filter(|wanted| {
4393 !before
4394 .options
4395 .iter()
4396 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4397 })
4398 .collect::<Vec<_>>();
4399 let report = StatusOptionsReport {
4400 source: self.name.clone(),
4401 missing: missing.clone(),
4402 outcome: match (mode, missing.is_empty()) {
4403 (StatusOptionsMode::Plan, _) => StatusOptionsOutcome::Planned,
4404 (StatusOptionsMode::Apply, true) => StatusOptionsOutcome::Unchanged,
4405 (StatusOptionsMode::Apply, false) => StatusOptionsOutcome::Applied,
4406 },
4407 existing: before.options.clone(),
4408 };
4409 if mode == StatusOptionsMode::Plan || missing.is_empty() {
4410 return Ok(report);
4411 }
4412 let mut options = before
4413 .options
4414 .iter()
4415 .map(|option| {
4416 json!({
4417 "id": option.id, "name": option.name, "color": option.color,
4418 "description": option.description,
4419 })
4420 })
4421 .collect::<Vec<_>>();
4422 options.extend(missing.iter().map(|name| {
4423 json!({
4424 "name": name, "color": "GRAY", "description": ""
4425 })
4426 }));
4427 self.graphql(
4428 graphql::STATUS_OPTIONS_UPDATE,
4429 json!({"input": {
4430 "projectId": before.board_id, "fieldId": before.field_id,
4431 "singleSelectOptions": options,
4432 }}),
4433 )
4434 .await?;
4435 let after = self.status_snapshot().await?;
4436 let options_preserved = before
4437 .options
4438 .iter()
4439 .all(|old| after.options.iter().any(|new| new == old));
4440 let additions_present = missing.iter().all(|wanted| {
4441 after
4442 .options
4443 .iter()
4444 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4445 });
4446 if !options_preserved || !additions_present || after.assignments != before.assignments {
4447 let recovery = serde_json::to_string_pretty(&before.assignments).map_err(|error| {
4448 SourceError::Malformed {
4449 message: format!("cannot render pre-write Status recovery snapshot: {error}"),
4450 }
4451 })?;
4452 return Err(SourceError::Refused {
4453 message: format!(
4454 "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}"
4455 ),
4456 });
4457 }
4458 Ok(report)
4459 }
4460
4461 /// A fresh snapshot of the Status field and every board item's assignment of it.
4462 ///
4463 /// # Errors
4464 ///
4465 /// Refuses a board without a single-select `Status` field, and one the token cannot see.
4466 async fn status_snapshot(&self) -> Result<StatusSnapshot, SourceError> {
4467 // Status alone, as this operation has always read it: a `Priority` field is another
4468 // operation's, so nothing about it can refuse this one.
4469 let mut board = self.board_snapshot(&[BoardField::Status]).await?;
4470 let field = board
4471 .fields
4472 .remove(&BoardField::Status)
4473 .ok_or_else(|| self.no_status_field())?;
4474 Ok(StatusSnapshot {
4475 assignments: board.assignments(BoardField::Status),
4476 board_id: board.board_id,
4477 field_id: field.field_id,
4478 options: field.options,
4479 })
4480 }
4481
4482 /// The refusal a board with no `Status` field is answered with by the guarded setup.
4483 fn no_status_field(&self) -> SourceError {
4484 SourceError::Refused {
4485 message: format!("source {} board has no Status field", self.name),
4486 }
4487 }
4488
4489 // llmlint: ignore-block[changed_behavior_has_e2e] Valid snapshot shapes are exercised through
4490 // the real CLI loopback journey, including pagination. The individual malformed guards
4491 // are defensive validation of a schema-pinned third-party response, not separate user
4492 // journeys; drift and missing-field failures cover the operation's recovery behavior.
4493 /// A fresh snapshot of each of the `owned` fields on the board, with its options, and of
4494 /// every board item's value of each, walked to the end of the board's items. A field not
4495 /// in `owned` is read past whatever it holds.
4496 async fn board_snapshot(&self, owned: &[BoardField]) -> Result<BoardSnapshot, SourceError> {
4497 let mut after: Option<String> = None;
4498 let mut snapshot: Option<BoardSnapshot> = None;
4499 loop {
4500 let data = self
4501 .graphql(
4502 graphql::STATUS_OPTIONS_SNAPSHOT,
4503 json!({
4504 "owner": self.owner, "number": self.project_number,
4505 "first": MAX_PAGE_SIZE, "after": after, "nestedFirst": MAX_PAGE_SIZE,
4506 }),
4507 )
4508 .await?;
4509 let board = data
4510 .pointer("/owner/projectV2")
4511 .filter(|board| board.is_object())
4512 .ok_or_else(|| SourceError::Refused {
4513 message: format!(
4514 "source {} has no accessible GitHub Projects board",
4515 self.name
4516 ),
4517 })?;
4518 if board
4519 .pointer("/fields/pageInfo/hasNextPage")
4520 .and_then(Value::as_bool)
4521 != Some(false)
4522 {
4523 return Err(SourceError::Malformed {
4524 message:
4525 "GitHub project fields is incomplete or has malformed pageInfo.hasNextPage"
4526 .into(),
4527 });
4528 }
4529 let mut fields = BTreeMap::new();
4530 // Only the fields this setup owns, by name: a node the single-select fragment did not
4531 // match carries no name, and a person's own single-select field — a `Size`, a
4532 // `Team` — is none of this setup's business, so nothing about it can refuse one. A
4533 // `Status` or `Priority` field without its options is malformed, not absent.
4534 // 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.
4535 for (owned, field) in board
4536 .pointer("/fields/nodes")
4537 .and_then(Value::as_array)
4538 .ok_or_else(|| SourceError::Malformed {
4539 message: "GitHub project fields.nodes is not an array".into(),
4540 })?
4541 .iter()
4542 .filter_map(|field| {
4543 let named = BoardField::named(field.get("name")?.as_str()?)?;
4544 owned.contains(&named).then_some((named, field))
4545 })
4546 {
4547 let options = field
4548 .get("options")
4549 .and_then(Value::as_array)
4550 .ok_or_else(|| SourceError::Malformed {
4551 message: "GitHub single-select field options is not an array".into(),
4552 })?
4553 .iter()
4554 .map(|option| {
4555 Ok(StatusOption {
4556 id: StatusOptionId::try_from(required_str(option, "id")?.to_owned())
4557 .map_err(|message| SourceError::Malformed { message })?,
4558 name: ColumnName::try_from(required_str(option, "name")?.to_owned())
4559 .map_err(|message| SourceError::Malformed {
4560 message: format!(
4561 "GitHub single-select option name is invalid: {message}"
4562 ),
4563 })?,
4564 color: serde_json::from_value(
4565 option.get("color").cloned().unwrap_or(Value::Null),
4566 )
4567 .map_err(|error| {
4568 SourceError::Malformed {
4569 message: format!(
4570 "GitHub single-select option color is invalid: {error}"
4571 ),
4572 }
4573 })?,
4574 description: optional_str(option, "description")?
4575 .unwrap_or_default()
4576 .to_owned(),
4577 })
4578 })
4579 .collect::<Result<Vec<_>, SourceError>>()?;
4580 let snapshot = SnapshotField {
4581 field_id: required_nonblank_str(field, "id")?.to_owned(),
4582 options,
4583 };
4584 // A board's field names are unique, so a second one is an answer that cannot
4585 // say which field the setup would act on — refused rather than one chosen.
4586 if fields.insert(owned, snapshot).is_some() {
4587 return Err(SourceError::Malformed {
4588 message: format!(
4589 "GitHub answered two {} fields for this board",
4590 owned.name()
4591 ),
4592 });
4593 }
4594 }
4595 let board_id = required_nonblank_str(board, "id")?.to_owned();
4596 let current = snapshot.get_or_insert_with(|| BoardSnapshot {
4597 board_id,
4598 fields,
4599 items: Vec::new(),
4600 });
4601 let items = board
4602 .pointer("/items/nodes")
4603 .and_then(Value::as_array)
4604 .ok_or_else(|| SourceError::Malformed {
4605 message: "GitHub project items.nodes is not an array".into(),
4606 })?;
4607 for item in items {
4608 let field_values =
4609 item.get("fieldValues")
4610 .ok_or_else(|| SourceError::Malformed {
4611 message: "GitHub project item is missing fieldValues".into(),
4612 })?;
4613 if field_values
4614 .pointer("/pageInfo/hasNextPage")
4615 .and_then(Value::as_bool)
4616 != Some(false)
4617 {
4618 return Err(SourceError::Malformed {
4619 message: "GitHub project item fieldValues is incomplete or has malformed pageInfo.hasNextPage".into(),
4620 });
4621 }
4622 let values = item
4623 .pointer("/fieldValues/nodes")
4624 .and_then(Value::as_array)
4625 .ok_or_else(|| SourceError::Malformed {
4626 message: "GitHub project item fieldValues.nodes is not an array".into(),
4627 })?;
4628 let item_id = required_nonblank_str(item, "id")?;
4629 let mut assigned = BTreeMap::new();
4630 for value in values {
4631 let Some(field) = value
4632 .pointer("/field/name")
4633 .and_then(Value::as_str)
4634 .and_then(BoardField::named)
4635 .filter(|field| owned.contains(field))
4636 else {
4637 continue;
4638 };
4639 let held = assigned.insert(
4640 field,
4641 AssignedStatusOption {
4642 id: StatusOptionId::try_from(
4643 required_str(value, "optionId")?.to_owned(),
4644 )
4645 .map_err(|message| SourceError::Malformed { message })?,
4646 name: ColumnName::try_from(required_str(value, "name")?.to_owned())
4647 .map_err(|message| SourceError::Malformed {
4648 message: format!(
4649 "GitHub assigned {} name is invalid: {message}",
4650 field.name()
4651 ),
4652 })?,
4653 },
4654 );
4655 // An item holds one value of a field, so a second one leaves no way to
4656 // tell which it holds — and a verification or recovery built on either
4657 // could restore the wrong one.
4658 if held.is_some() {
4659 return Err(SourceError::Malformed {
4660 message: format!(
4661 "GitHub answered two {} values for board item {item_id}",
4662 field.name()
4663 ),
4664 });
4665 }
4666 }
4667 current.items.push((item_id.to_owned(), assigned));
4668 }
4669 let page = board.get("items").ok_or_else(|| SourceError::Malformed {
4670 message: "GitHub project is missing items".into(),
4671 })?;
4672 let has_next = page
4673 .pointer("/pageInfo/hasNextPage")
4674 .and_then(Value::as_bool)
4675 .ok_or_else(|| SourceError::Malformed {
4676 message: "GitHub project items.pageInfo.hasNextPage is not a boolean".into(),
4677 })?;
4678 if !has_next {
4679 break;
4680 }
4681 let next =
4682 required_nonblank_str(page.get("pageInfo").unwrap_or(&Value::Null), "endCursor")?;
4683 validate_cursor_progress(after.as_deref(), next)?;
4684 after = Some(next.to_owned());
4685 }
4686 snapshot.ok_or_else(|| SourceError::Malformed {
4687 message: "GitHub returned no board field snapshot".into(),
4688 })
4689 }
4690 // llmlint: ignore-end[changed_behavior_has_e2e]
4691
4692 /// Report every board field this source's configuration names and, with
4693 /// [`SetupMode::Apply`], set each up: add the options a field lacks, and create
4694 /// the `Priority` field when the board has none.
4695 ///
4696 /// The fields are `Status`, always, with the options `status_mapping` resolves to; and
4697 /// `Priority`, when `priority_mapping` is set, with its four mapped options — created in
4698 /// the order urgent, high, medium, low. An option a field already has keeps its id, name,
4699 /// color and description: the whole option list goes back with every existing id, because
4700 /// a re-minted id clears every item's value.
4701 ///
4702 /// # Errors
4703 ///
4704 /// Refuses a board without a single-select `Status` field. After an apply the board is
4705 /// read again, and a pre-existing option or any item's value of either field that moved is
4706 /// refused with the complete pre-write assignments in the diagnostic, for recovery.
4707 // llmlint: ignore[changed_behavior_has_e2e] The `sources fields` journeys drive plan,
4708 // unchanged apply, a created field, an added option to each field, drift refusal, a board
4709 // with no Status field and a non-github-projects source through the compiled CLI against
4710 // the loopback board. Transport errors are the shared `graphql` boundary's behavior.
4711 pub async fn fields(&self, mode: SetupMode) -> Result<FieldsReport, SourceError> {
4712 let owned: Vec<BoardField> = if self.priorities.is_some() {
4713 vec![BoardField::Status, BoardField::Priority]
4714 } else {
4715 vec![BoardField::Status]
4716 };
4717 let before = self.board_snapshot(&owned).await?;
4718 let mut plans = vec![FieldPlan {
4719 field: BoardField::Status,
4720 wanted: self.statuses.wanted(),
4721 }];
4722 if !before.fields.contains_key(&BoardField::Status) {
4723 return Err(self.no_status_field());
4724 }
4725 if let Some(mapping) = &self.priorities {
4726 plans.push(FieldPlan {
4727 field: BoardField::Priority,
4728 wanted: mapping.names().map(str::to_owned).collect(),
4729 });
4730 }
4731 // The snapshot reads single-select fields alone, so a field it did not find may still
4732 // be on the board under the name, of another type: creating one beside it would fail
4733 // part way, or leave two fields of one name. Asked of the board's own field list, and
4734 // only when a field is missing.
4735 if plans
4736 .iter()
4737 .any(|plan| !before.fields.contains_key(&plan.field))
4738 {
4739 let board = self.board_fields().await?;
4740 for plan in plans
4741 .iter()
4742 .filter(|plan| !before.fields.contains_key(&plan.field))
4743 {
4744 if let Some(field) = Board::field(&board.fields, plan.field.name())? {
4745 return Err(SourceError::Refused {
4746 message: format!(
4747 "source {}'s board has a {} field that is not a single-select field \
4748 (it is a {}), so it cannot hold this source's options; next: rename \
4749 or remove that field, then run this again",
4750 self.name,
4751 plan.field.name(),
4752 optional_str(field, "__typename")?.unwrap_or("field of another type")
4753 ),
4754 });
4755 }
4756 }
4757 }
4758 let mut reports = Vec::new();
4759 for plan in &plans {
4760 let held = before.fields.get(&plan.field);
4761 let existing = held.map(|field| field.options.clone()).unwrap_or_default();
4762 let mut missing: Vec<String> = Vec::new();
4763 for wanted in &plan.wanted {
4764 let present = existing
4765 .iter()
4766 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4767 || missing
4768 .iter()
4769 .any(|named| named.eq_ignore_ascii_case(wanted));
4770 if !present {
4771 missing.push(wanted.clone());
4772 }
4773 }
4774 let kinds = match plan.field {
4775 BoardField::Status => self.statuses.missing_by_kind(&existing),
4776 BoardField::Priority => Vec::new(),
4777 };
4778 reports.push(FieldReport {
4779 field: plan.field,
4780 exists: held.is_some(),
4781 kinds,
4782 outcome: match (mode, held.is_some(), missing.is_empty()) {
4783 (SetupMode::Plan, _, _) => FieldOutcome::Planned,
4784 (SetupMode::Apply, true, true) => FieldOutcome::Unchanged,
4785 (SetupMode::Apply, true, false) => FieldOutcome::Applied,
4786 (SetupMode::Apply, false, _) => FieldOutcome::Created,
4787 },
4788 missing,
4789 existing,
4790 });
4791 }
4792 // A projected field is a text field, which the single-select snapshot cannot see, so
4793 // it is asked of the board's own field list — and only when one is configured.
4794 let mut metadata_fields = Vec::new();
4795 if !self.metadata_fields.is_empty() {
4796 let board = self.board_fields().await?;
4797 for projection in &self.metadata_fields {
4798 let conflict = match Board::field(&board.fields, &projection.field)? {
4799 None => None,
4800 Some(field) => {
4801 let typename = optional_str(field, "__typename")?;
4802 let data_type = optional_str(field, "dataType")?;
4803 if typename == Some("ProjectV2Field") && data_type == Some("TEXT") {
4804 None
4805 } else {
4806 let named = data_type
4807 .filter(|kind| *kind != "TEXT")
4808 .or(typename)
4809 .unwrap_or("field of another type");
4810 Some(NonTextFieldType::try_from(named.to_owned()).map_err(
4811 |message| SourceError::Malformed {
4812 message: format!(
4813 "GitHub answered the board's {} field with a type this \
4814 setup cannot read: {message}",
4815 &*projection.field
4816 ),
4817 },
4818 )?)
4819 }
4820 }
4821 };
4822 let exists =
4823 conflict.is_none() && Board::field(&board.fields, &projection.field)?.is_some();
4824 metadata_fields.push(MetadataFieldReport {
4825 field: projection.field.to_string(),
4826 key: projection.key.to_string(),
4827 path: projection.path.clone(),
4828 exists,
4829 outcome: match (mode, exists || conflict.is_some()) {
4830 (SetupMode::Plan, _) => FieldOutcome::Planned,
4831 (SetupMode::Apply, true) => FieldOutcome::Unchanged,
4832 (SetupMode::Apply, false) => FieldOutcome::Created,
4833 },
4834 conflict,
4835 });
4836 }
4837 }
4838 let report = FieldsReport {
4839 source: self.name.clone(),
4840 fields: reports,
4841 metadata_fields,
4842 };
4843 let writes: Vec<&FieldReport> = report
4844 .fields
4845 .iter()
4846 .filter(|field| !field.missing.is_empty() || !field.exists)
4847 .collect();
4848 let creates: Vec<&MetadataFieldReport> = report
4849 .metadata_fields
4850 .iter()
4851 .filter(|field| field.outcome == FieldOutcome::Created)
4852 .collect();
4853 if mode == SetupMode::Plan || (writes.is_empty() && creates.is_empty()) {
4854 return Ok(report);
4855 }
4856 let mut landed: Vec<&str> = Vec::new();
4857 for field in &writes {
4858 let added = field
4859 .missing
4860 .iter()
4861 .map(|name| json!({"name": name, "color": "GRAY", "description": ""}));
4862 let sent = match before.fields.get(&field.field) {
4863 Some(held) => {
4864 let mut options = held
4865 .options
4866 .iter()
4867 .map(|option| {
4868 json!({
4869 "id": option.id, "name": option.name, "color": option.color,
4870 "description": option.description,
4871 })
4872 })
4873 .collect::<Vec<_>>();
4874 options.extend(added);
4875 self.graphql(
4876 graphql::STATUS_OPTIONS_UPDATE,
4877 json!({"input": {
4878 "projectId": before.board_id, "fieldId": held.field_id,
4879 "singleSelectOptions": options,
4880 }}),
4881 )
4882 .await
4883 }
4884 None => {
4885 self.graphql(
4886 graphql::CREATE_FIELD,
4887 json!({"input": {
4888 "projectId": before.board_id, "dataType": "SINGLE_SELECT",
4889 "name": field.field.name(),
4890 "singleSelectOptions": added.collect::<Vec<_>>(),
4891 }}),
4892 )
4893 .await
4894 }
4895 };
4896 // A mutation that failed does not establish that GitHub left its field as it was,
4897 // so every failure from here on carries the recovery data a drift refusal does.
4898 match sent {
4899 Ok(_) => landed.push(field.field.name()),
4900 Err(error) => {
4901 let changed = if landed.is_empty() {
4902 String::new()
4903 } else {
4904 format!("changed the {} field and then ", landed.join(" and "))
4905 };
4906 return Err(SourceError::Refused {
4907 message: format!(
4908 "the guarded field setup {changed}failed on the {} field, which it may \
4909 have changed part way: {error}; the pre-write item assignments \
4910 are:\n{}",
4911 field.field.name(),
4912 recovery(&report, &before)?
4913 ),
4914 });
4915 }
4916 }
4917 }
4918 for field in &creates {
4919 let sent = self
4920 .graphql(
4921 graphql::CREATE_FIELD,
4922 json!({"input": {
4923 "projectId": before.board_id, "dataType": "TEXT", "name": field.field,
4924 }}),
4925 )
4926 .await;
4927 if let Err(error) = sent {
4928 let changed = if landed.is_empty() {
4929 String::new()
4930 } else {
4931 format!("changed the {} field and then ", landed.join(" and "))
4932 };
4933 return Err(SourceError::Refused {
4934 message: format!(
4935 "the guarded field setup {changed}failed creating the {} text field: \
4936 {error}; run it again to create what is still missing",
4937 field.field
4938 ),
4939 });
4940 }
4941 landed.push(&field.field);
4942 }
4943 // The board has been written, so a verification read that fails leaves it unverified
4944 // rather than unchanged, and says what to put back.
4945 let after = match self.board_snapshot(&owned).await {
4946 Ok(after) => after,
4947 Err(error) => {
4948 return Err(SourceError::Refused {
4949 message: format!(
4950 "the guarded field setup changed the {} field and then could not read the \
4951 board back to verify it: {error}; the pre-write item assignments are:\n{}",
4952 landed.join(" and "),
4953 recovery(&report, &before)?
4954 ),
4955 });
4956 }
4957 };
4958 let mut moved = Vec::new();
4959 for field in &report.fields {
4960 let name = field.field.name();
4961 let now = after
4962 .fields
4963 .get(&field.field)
4964 .map(|held| held.options.as_slice())
4965 .unwrap_or_default();
4966 if !field.existing.iter().all(|old| now.contains(old)) {
4967 moved.push(format!(
4968 "a pre-existing {name} option id, name, color or description"
4969 ));
4970 }
4971 if !field.missing.iter().all(|wanted| {
4972 now.iter()
4973 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4974 }) {
4975 moved.push(format!("an added {name} option"));
4976 }
4977 if after.assignments(field.field) != before.assignments(field.field) {
4978 moved.push(format!("an item's {name} value"));
4979 }
4980 }
4981 if !creates.is_empty() {
4982 // Read afresh: the view held for this command is the one from before the creates.
4983 *self.fields_cache()? = None;
4984 let board = match self.board_fields().await {
4985 Ok(board) => board,
4986 Err(error) => {
4987 return Err(SourceError::Refused {
4988 message: format!(
4989 "the guarded field setup changed the {} field and then could not \
4990 read the board back to verify it: {error}; run it again to verify \
4991 it, which creates nothing a second time",
4992 landed.join(" and ")
4993 ),
4994 });
4995 }
4996 };
4997 for field in &creates {
4998 let text = Board::field(&board.fields, &field.field)?.is_some_and(|held| {
4999 held.get("__typename").and_then(Value::as_str) == Some("ProjectV2Field")
5000 && held.get("dataType").and_then(Value::as_str) == Some("TEXT")
5001 });
5002 if !text {
5003 moved.push(format!("the created {} text field", field.field));
5004 }
5005 }
5006 }
5007 if !moved.is_empty() {
5008 return Err(SourceError::Refused {
5009 message: format!(
5010 "GitHub changed {} after the guarded field setup; the pre-write item \
5011 assignments are:\n{}",
5012 moved.join(", "),
5013 recovery(&report, &before)?
5014 ),
5015 });
5016 }
5017 Ok(report)
5018 }
5019
5020 /// Validate configuration and capture the named credential without exposing it.
5021 ///
5022 /// # Errors
5023 ///
5024 /// Returns [`SourceError::Config`] for a configuration this instance cannot use and
5025 /// [`SourceError::Auth`] when the named credential is missing or empty.
5026 pub fn new(
5027 name: &SourceName,
5028 config: GitHubProjectsConfig,
5029 secrets: &dyn SecretResolver,
5030 ) -> Result<Self, SourceError> {
5031 Self::recording_into(name, config, secrets, Arc::new(Accounting::new()))
5032 }
5033
5034 /// The same, recording every request it sends into an accounting the caller holds too.
5035 ///
5036 /// [`Self::new`] is this with an accounting of its own. A caller that is also making
5037 /// its own calls to GitHub — a lane verifying a schema, sweeping residue or cleaning
5038 /// up — passes the one it records those into, so the session total accounts for the
5039 /// whole session rather than for this source's share of it.
5040 ///
5041 /// # Errors
5042 ///
5043 /// Exactly [`Self::new`]'s: [`SourceError::Config`] for a configuration this instance
5044 /// cannot use and [`SourceError::Auth`] when the named credential is missing or empty.
5045 pub fn recording_into(
5046 name: &SourceName,
5047 config: GitHubProjectsConfig,
5048 secrets: &dyn SecretResolver,
5049 ledger: Arc<Accounting>,
5050 ) -> Result<Self, SourceError> {
5051 if !valid_github_owner(&config.owner) {
5052 return Err(SourceError::Config {
5053 message: "owner must be 1-39 ASCII letters, digits, or single hyphens, and cannot start or end with a hyphen".into(),
5054 });
5055 }
5056 if config.project_number == 0 || config.project_number > i32::MAX as u32 {
5057 return Err(SourceError::Config {
5058 message: format!("project_number must be between 1 and {}", i32::MAX),
5059 });
5060 }
5061 if !valid_environment_name(&config.token_env) {
5062 return Err(SourceError::Config {
5063 message: "token_env must be a valid environment-variable name".into(),
5064 });
5065 }
5066 let repository = config
5067 .repository
5068 .as_deref()
5069 .map(RepositoryTarget::parse)
5070 .transpose()?;
5071 if let Some(entry) = config
5072 .creation_owners
5073 .iter()
5074 .find(|entry| !valid_github_owner(entry))
5075 {
5076 return Err(SourceError::Config {
5077 message: format!(
5078 "creation_owners entry {entry:?} is not a GitHub login: a login is 1-39 ASCII \
5079 letters, digits, or single hyphens, and cannot start or end with a hyphen"
5080 ),
5081 });
5082 }
5083 let endpoint = Url::parse(&config.endpoint).map_err(|e| SourceError::Config {
5084 message: format!("endpoint is not a valid URL: {e}"),
5085 })?;
5086 if endpoint.scheme() != "https"
5087 && !(endpoint.scheme() == "http"
5088 && endpoint
5089 .host_str()
5090 .is_some_and(|h| h == "127.0.0.1" || h == "localhost" || h == "::1"))
5091 {
5092 return Err(SourceError::Config {
5093 message:
5094 "endpoint must use HTTPS (HTTP is accepted only for a loopback test server)"
5095 .into(),
5096 });
5097 }
5098 let token = secrets.get(&config.token_env).filter(|token| !token.expose_secret().trim().is_empty()).ok_or_else(|| SourceError::Auth {
5099 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),
5100 })?;
5101 Ok(Self {
5102 name: name.clone(),
5103 owner: config.owner,
5104 project_number: config.project_number,
5105 repository,
5106 creation_owners: config.creation_owners,
5107 asset_client: std::sync::OnceLock::new(),
5108 endpoint,
5109 token,
5110 credential_name: config.token_env,
5111 statuses: BoardStatuses::resolve(&config.status_mapping, name)?,
5112 priorities: config
5113 .priority_mapping
5114 .map(|mapping| PriorityMapping::resolve(mapping, name))
5115 .transpose()?,
5116 metadata_fields: MetadataField::resolve(config.metadata_fields, name)?,
5117 client: std::sync::OnceLock::new(),
5118 created: Mutex::new(Vec::new()),
5119 updated: Mutex::new(Vec::new()),
5120 commented: Mutex::new(Vec::new()),
5121 pacing: Pacing::resolve(config.pacing, name)?,
5122 last_mutation: Mutex::new(None),
5123 windows: Mutex::new(pacing::Windows::default()),
5124 task_children: Mutex::new(BTreeMap::new()),
5125 written: Mutex::new(BTreeMap::new()),
5126 deferred: Mutex::new(None),
5127 held_updates: Mutex::new(Vec::new()),
5128 clock: system_clock(),
5129 declared_private: std::sync::atomic::AtomicBool::new(false),
5130 numeric_repositories: tokio::sync::Mutex::new(BTreeMap::new()),
5131 board_cache: Mutex::new(None),
5132 search_cache: Mutex::new(None),
5133 narrowed_cache: Mutex::new(BTreeMap::new()),
5134 resolved_cache: Mutex::new(BTreeMap::new()),
5135 read_ahead: Mutex::new(BTreeMap::new()),
5136 pending_origins: Mutex::new(Vec::new()),
5137 member_classes: Mutex::new(BTreeMap::new()),
5138 carried: Mutex::new(BTreeMap::new()),
5139 held_cache: Mutex::new(BTreeMap::new()),
5140 children_cache: Mutex::new(BTreeMap::new()),
5141 search_next: Mutex::new(BTreeMap::new()),
5142 fields_cache: Mutex::new(None),
5143 repository_cache: Mutex::new(BTreeMap::new()),
5144 ledger,
5145 expected: expected::Held::default(),
5146 projection: projection::Held::default(),
5147 })
5148 }
5149
5150 /// The client every API request goes through, built on the first one.
5151 ///
5152 /// Not with the source: building a client loads the system's certificate store, which
5153 /// costs tens of milliseconds, and a command that configures this board but never sends to
5154 /// it — a read of another source — should not pay that in its process.
5155 fn client(&self) -> Result<&Client, SourceError> {
5156 built(&self.client, || {
5157 Client::builder()
5158 .user_agent("onetaskgraph")
5159 .build()
5160 .map_err(|e| SourceError::Config {
5161 message: format!("cannot build HTTP client: {e}"),
5162 })
5163 })
5164 }
5165
5166 /// The client attachment requests go through, with its own redirect policy, built on the
5167 /// first one for the reason [`Self::client`] gives.
5168 fn asset_client(&self) -> Result<&Client, SourceError> {
5169 built(&self.asset_client, || assets::client(&self.endpoint))
5170 }
5171
5172 /// A snapshot of every request this source has sent, and what each cost.
5173 ///
5174 /// A value to hold and compare rather than a borrow of the accounting itself, so two
5175 /// of them can sit side by side. When this source was built with
5176 /// [`Self::recording_into`] the snapshot is the whole shared session, which is the
5177 /// point of building it that way.
5178 #[must_use]
5179 pub fn accounting(&self) -> accounting::Session {
5180 self.ledger.snapshot()
5181 }
5182
5183 /// Send one GraphQL document, pacing this source's own mutations and waiting out a
5184 /// rate limit rather than handing it straight back as an error.
5185 ///
5186 /// Retrying is safe for every document here, including the mutations, and the reason
5187 /// is that only a *refusal* is retried: [`Limiter::classify`] rules on a response
5188 /// GitHub sent, and a request GitHub refused for a rate limit did not run, so nothing
5189 /// this replays has already taken effect. An outcome this source cannot know — the
5190 /// send failed, or the body could not be read, so the mutation may well have landed —
5191 /// is [`Attempt::Failed`] in [`send_once`] and leaves this loop without a second
5192 /// attempt. A duplicate write would come from replaying one of those, and none is
5193 /// replayed.
5194 async fn graphql(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
5195 // Updates held back go out before any other request, so whatever it reads or writes
5196 // sees the board as though each had been sent when it was made.
5197 Box::pin(self.send_held_updates()).await?;
5198 // Origins held back for a read ahead that never came are sent before anything else.
5199 if query != graphql::ORIGIN_LOOKUPS && query != graphql::ORIGIN_LOOKUPS_WITH_MEMBERS {
5200 Box::pin(self.send_pending_origins(None)).await?;
5201 }
5202 if !self.expected.journaling() {
5203 return self.graphql_sent(query, variables).await;
5204 }
5205 // While a write the store's cache expects runs, its mutations select the whole issue
5206 // they leave behind, so the response vouches for what the cache keeps — and every
5207 // request is recorded, so the write's parts are reported from their own responses.
5208 let (query, variables) = match graphql::whole_twin(query) {
5209 Some(twin) => (twin, expected::whole_variables(variables)),
5210 None => (query, variables),
5211 };
5212 let sent = self.graphql_sent(query, variables.clone()).await;
5213 self.journal(query, &variables, &sent).await;
5214 sent
5215 }
5216
5217 /// One document sent, retried while GitHub asks for a wait, exactly as asked.
5218 async fn graphql_sent(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
5219 if is_mutation(query)
5220 && ![
5221 graphql::ADD_COMMENT,
5222 graphql::UPDATE_COMMENT,
5223 graphql::DELETE_COMMENT,
5224 graphql::ADD_COMMENT_WHOLE,
5225 graphql::UPDATE_COMMENT_WHOLE,
5226 ]
5227 .contains(&query)
5228 {
5229 let mut cache = self.resolved_cache()?;
5230 let mut ahead = self.read_ahead()?;
5231 let mut carried = self.carried()?;
5232 for argument in ["input", "second", "third", "clear"] {
5233 if let Some(input) = variables.get(argument) {
5234 let names = |id: &NativeId, item_id: Option<&str>| {
5235 ["id", "issueId", "subjectId", "itemId"].iter().any(|key| {
5236 input
5237 .get(key)
5238 .and_then(Value::as_str)
5239 .is_some_and(|value| value == id.0 || Some(value) == item_id)
5240 })
5241 };
5242 cache.retain(|id, item| !names(id, Some(&item.item_id)));
5243 ahead.retain(|id, item| {
5244 !names(id, item.as_ref().map(|item| item.item_id.as_str()))
5245 });
5246 carried.retain(|id, item| !names(id, Some(&item.item_id)));
5247 }
5248 }
5249 }
5250 let doing = operation_description(query);
5251 let mutation = is_mutation(query);
5252 let charge = pacing::Charge::graphql(query, &variables);
5253 let mut waited = Duration::ZERO;
5254 let mut waits = 0_u32;
5255 let mut backoff = self.pacing.retry_backoff;
5256 loop {
5257 let ticket = self.pace(charge, mutation).await;
5258 // Every write but a create is asked for immediately before it is sent — a create's
5259 // fields, its relationships, its content and the delete that takes it back included
5260 // — so a copy that owns the create renews its hold first, and one that has lost it
5261 // sends nothing more. Asked again before a retry, which is sent later.
5262 if mutation
5263 && !query.contains("createIssue(")
5264 && let Err(refused) = <dyn onetaskgraph_plugin_api::WriteGuard>::check().await
5265 {
5266 self.paced(ticket, mutation);
5267 return Err(refused);
5268 }
5269 let attempt = self.send_once(query, &variables, waits + 1).await;
5270 self.paced(ticket, mutation);
5271 let limited = match attempt {
5272 Ok(data) => return Ok(data),
5273 Err(Attempt::Failed(error)) => return Err(error),
5274 Err(Attempt::Limited(limited)) => limited,
5275 };
5276 let (wait, cause) = limited.wait(backoff);
5277 let remaining = self.pacing.retry_budget.saturating_sub(waited);
5278 // A wait of nothing spends none of the budget, so it is exhaustion rather
5279 // than a retry. `Pacing::resolve` rules out every way of configuring one
5280 // except a budget of zero, where reporting the first refusal is the ask.
5281 if wait.is_zero() || wait > remaining {
5282 return Err(limited.exhausted(
5283 doing,
5284 waits,
5285 waited,
5286 wait,
5287 self.pacing.retry_budget,
5288 ));
5289 }
5290 self.clock.sleep_because(wait, cause).await;
5291 waited += wait;
5292 waits += 1;
5293 backoff = backoff.saturating_mul(2);
5294 }
5295 }
5296
5297 /// Hold one request charging `charge` until every secondary window it charges has room for
5298 /// the whole of it — and, for a mutation of an instance configured with
5299 /// `pacing.min_mutation_interval_ms`, until that interval has passed since the last one
5300 /// finished — then count it, answering the ticket [`Self::paced`] finishes it with.
5301 ///
5302 /// The moment is decided and counted under the lock and waited out outside it, so two
5303 /// callers take two places rather than the same one, and no lock is held across an await.
5304 /// A request with room goes out at once: nothing here waits that a window does not ask for.
5305 async fn pace(&self, charge: pacing::Charge, mutation: bool) -> pacing::Ticket {
5306 let (wait, cause, ticket) = {
5307 // A poisoned lock here costs pacing, not correctness, and refusing the write over
5308 // it would turn an earlier failure into a second one for no gain.
5309 let mut windows = self
5310 .windows
5311 .lock()
5312 .unwrap_or_else(std::sync::PoisonError::into_inner);
5313 let now = self.clock.now();
5314 let (mut at, mut cause) = if self.pacing.windows {
5315 windows.room(now, charge)
5316 } else {
5317 (now, None)
5318 };
5319 if mutation && !self.pacing.min_mutation_interval.is_zero() {
5320 let mut last = self
5321 .last_mutation
5322 .lock()
5323 .unwrap_or_else(std::sync::PoisonError::into_inner);
5324 // `checked_add` rather than `+`: adding durations can panic on overflow, and
5325 // pacing is not worth a panic even at a bound `MAX_PACING_MS` already rules out.
5326 if let Some(earliest) = last
5327 .and_then(|previous| previous.checked_add(self.pacing.min_mutation_interval))
5328 && earliest > at
5329 {
5330 at = earliest;
5331 cause = Some(pacing::cause::MIN_INTERVAL);
5332 }
5333 *last = Some(at);
5334 }
5335 let ticket = windows.charge(at, charge);
5336 (at.saturating_sub(now), cause, ticket)
5337 };
5338 if let (false, Some(cause)) = (wait.is_zero(), cause) {
5339 self.clock.sleep_because(wait, cause).await;
5340 }
5341 ticket
5342 }
5343
5344 /// Record that the request `ticket` was charged for has finished, so each window — and
5345 /// the configured spacing — counts it from here rather than from the moment it left.
5346 ///
5347 /// This source can only choose when a request *departs*; GitHub's limiter counts when it
5348 /// *arrives*, and the two differ by whatever the request spent in transit. Counting from
5349 /// departure would hand the limiter a window that looked roomier than it is, so a source
5350 /// pacing exactly at a limit could still be seen arriving over it — which is how a copy
5351 /// paced well inside a board's threshold was once refused by it on a slower machine while
5352 /// passing on a quick one. The previous request had arrived before its response came back,
5353 /// so counting from here is never earlier than the limiter does, whatever transit costs
5354 /// and on whatever platform: the safe side of a limit that punishes being wrong by refusing
5355 /// reads for the next fifty minutes.
5356 ///
5357 /// A failed attempt is finished too: a request refused by the limiter still arrived, and
5358 /// one that never left costs only room nobody needed.
5359 fn paced(&self, ticket: pacing::Ticket, mutation: bool) {
5360 let now = self.clock.now();
5361 self.windows
5362 .lock()
5363 .unwrap_or_else(std::sync::PoisonError::into_inner)
5364 .finish(ticket, now);
5365 if mutation && !self.pacing.min_mutation_interval.is_zero() {
5366 let mut last = self
5367 .last_mutation
5368 .lock()
5369 .unwrap_or_else(std::sync::PoisonError::into_inner);
5370 // `max` rather than an assignment: a concurrent caller may already have reserved a
5371 // slot further out, and completing this request must never pull that slot back in.
5372 *last = Some(last.map_or(now, |reserved| reserved.max(now)));
5373 }
5374 }
5375
5376 /// One HTTP attempt, classified into an answer, a rate limit to wait out, or a
5377 /// failure that waiting cannot help — and recorded, whichever of the three it was.
5378 ///
5379 /// This is the one place a request leaves this crate, which is why the accounting is
5380 /// here rather than at each of the callers: a read path added later is counted without
5381 /// anybody remembering to count it, and
5382 /// `the_session_report_counts_every_request_the_board_served_and_what_each_cost` fails
5383 /// when one is not.
5384 async fn send_once(
5385 &self,
5386 query: &str,
5387 variables: &Value,
5388 number: u32,
5389 ) -> Result<Value, Attempt> {
5390 let executed = self.ledger.priced_document(query);
5391 let Attempted {
5392 result,
5393 limits,
5394 reported_cost,
5395 response_outcome,
5396 } = self.attempt(executed.as_ref(), variables).await;
5397 // No `otherwise` name: every document this source sends is one of its own, and the
5398 // inventory gate on `graphql::DOCUMENTS` is what keeps that true.
5399 let sending = accounting::Request::graphql(query, variables, None, reported_cost);
5400 let outcome = match &result {
5401 Ok(_) => accounting::Outcome::Answered,
5402 Err(Attempt::Limited(_)) => accounting::Outcome::RateLimited,
5403 Err(Attempt::Failed(_)) => accounting::Outcome::Refused,
5404 };
5405 self.ledger
5406 .record(sending.finished(outcome, limits).with_execution(
5407 Some(executed.into_owned()),
5408 response_outcome,
5409 number,
5410 ));
5411 result
5412 }
5413
5414 /// The attempt itself, with what its response said about the rate limit alongside.
5415 ///
5416 /// The two are returned together rather than recorded here because every one of the
5417 /// early exits below is a different outcome, and a record written at each of them is a
5418 /// record one of them can be added without.
5419 async fn attempt(&self, query: &str, variables: &Value) -> Attempted {
5420 let mut limits = accounting::RateLimit::default();
5421 let mut reported_cost = None;
5422 let mut response_outcome = accounting::ResponseOutcome::TransportError;
5423 let result = self
5424 .attempted(
5425 query,
5426 variables,
5427 &mut limits,
5428 &mut reported_cost,
5429 &mut response_outcome,
5430 )
5431 .await;
5432 Attempted {
5433 result,
5434 limits,
5435 reported_cost,
5436 response_outcome,
5437 }
5438 }
5439
5440 /// One HTTP attempt, filling in what its response said about the rate limit as it goes.
5441 async fn attempted(
5442 &self,
5443 query: &str,
5444 variables: &Value,
5445 limits: &mut accounting::RateLimit,
5446 reported_cost: &mut Option<u64>,
5447 response_outcome: &mut accounting::ResponseOutcome,
5448 ) -> Result<Value, Attempt> {
5449 let response = self
5450 .client()
5451 .map_err(Attempt::Failed)?
5452 .post(self.endpoint.clone())
5453 .bearer_auth(self.token.expose_secret())
5454 .json(&json!({"query": query, "variables": variables}))
5455 .send()
5456 .await
5457 .map_err(|e| {
5458 Attempt::Failed(SourceError::Unavailable {
5459 message: format!("GitHub GraphQL request failed: {}", transport_error(&e)),
5460 })
5461 })?;
5462 let status = response.status();
5463 if !status.is_success() {
5464 *response_outcome = accounting::ResponseOutcome::HttpError;
5465 }
5466 let header = |name: &str| whole_seconds(response.headers().get(name));
5467 let served = response
5468 .headers()
5469 .get(reqwest::header::DATE)
5470 .and_then(|value| value.to_str().ok())
5471 .and_then(|value| DateTime::parse_from_rfc2822(value).ok())
5472 .map(|at| at.with_timezone(&Utc));
5473 *limits = accounting::RateLimit::read(|name| {
5474 response
5475 .headers()
5476 .get(name)
5477 .and_then(|value| value.to_str().ok())
5478 .map(str::to_owned)
5479 });
5480 // Exactly `0` is exhaustion and everything else — a count, an empty value, bytes
5481 // that are not text at all — is "not known to be exhausted". This never makes a
5482 // response a refusal on its own: it says which limiter a refusal is attributed to
5483 // and where its hint comes from, so a value this cannot read costs a hint rather
5484 // than an answer.
5485 let exhausted = response
5486 .headers()
5487 .get("x-ratelimit-remaining")
5488 .and_then(|value| value.to_str().ok())
5489 == Some("0");
5490 // `retry-after` is what GitHub asks for when it asks; when it does not and the
5491 // primary budget is spent, `x-ratelimit-reset` says when that budget comes back,
5492 // which is the same question answered as an absolute time. Nothing else here is a
5493 // hint, and a schedule is what answers a refusal that carries none.
5494 let retry_after = header("retry-after");
5495 let hinted_by = if retry_after.is_some() || !exhausted {
5496 Hint::RetryAfter
5497 } else {
5498 Hint::PrimaryReset
5499 };
5500 let hint = retry_after.or_else(|| {
5501 exhausted
5502 .then(|| header("x-ratelimit-reset"))
5503 .flatten()
5504 .map(|reset| reset.saturating_sub(Utc::now().timestamp().max(0).unsigned_abs()))
5505 });
5506 // Read before it is parsed, because the evidence which tells a secondary rate
5507 // limit from a rejected credential is in the body of a response whose status says
5508 // only "forbidden" — and a non-success response was never parsed at all.
5509 let body = response.text().await.map_err(|e| {
5510 Attempt::Failed(SourceError::Unavailable {
5511 message: format!(
5512 "GitHub GraphQL response could not be read: {}",
5513 transport_error(&e)
5514 ),
5515 })
5516 })?;
5517 *reported_cost = serde_json::from_str::<Value>(&body)
5518 .ok()
5519 .as_ref()
5520 .and_then(|body| body.pointer("/data/rateLimit/cost"))
5521 .and_then(Value::as_u64);
5522 let parsed = serde_json::from_str::<Value>(&body).ok();
5523 *response_outcome = accounting::ResponseOutcome::of_graphql_reply(status, parsed.as_ref());
5524 if let Some(limiter) = Limiter::classify(status, exhausted, &body) {
5525 return Err(Attempt::Limited(Limited {
5526 limiter,
5527 hint,
5528 hinted_by,
5529 }));
5530 }
5531 if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {
5532 return Err(Attempt::Failed(SourceError::Auth {
5533 message: format!(
5534 "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"
5535 ),
5536 }));
5537 }
5538 if !status.is_success() {
5539 return Err(Attempt::Failed(SourceError::Unavailable {
5540 message: format!("GitHub GraphQL returned HTTP {status}"),
5541 }));
5542 }
5543 let answered = if query == graphql::ISSUES_BY_REFERENCE {
5544 self.answer_unresolved(&body)
5545 } else if query == graphql::CREATE_ISSUES {
5546 answer_with_errors(&body).map_or_else(|| self.answer(&body), Ok)
5547 } else {
5548 self.answer(&body)
5549 };
5550 let mut data = answered.map_err(Attempt::Failed)?;
5551 if let Some(served) = served {
5552 stamp_served(&mut data, &served.to_rfc3339());
5553 }
5554 Ok(data)
5555 }
5556
5557 /// What one successful HTTP response says, once its GraphQL errors are read.
5558 /// [`Self::answer`], for a read of issues by reference: a reference GitHub cannot resolve
5559 /// for this credential — a repository it cannot see, an issue number it does not hold —
5560 /// is answered `null` beside a `NOT_FOUND` error naming it, and that is an answer about
5561 /// that reference rather than a failure of the others. Any other error refuses the read,
5562 /// as there.
5563 fn answer_unresolved(&self, body: &str) -> Result<Value, SourceError> {
5564 let parsed: Option<Value> = serde_json::from_str(body).ok();
5565 // Each error is a `NOT_FOUND` whose path names one slot of the batch, `r<n>` or its
5566 // `issue`, and the answer holds `null` at exactly that path: anything else — another
5567 // kind of error, one naming no slot, or one beside a value it says is not there — is a
5568 // refusal of the whole read.
5569 let unresolved = |data: &Value, error: &Value| {
5570 error.get("type").and_then(Value::as_str) == Some("NOT_FOUND")
5571 && error
5572 .get("path")
5573 .and_then(Value::as_array)
5574 .is_some_and(|path| {
5575 let alias = path.first().and_then(Value::as_str).and_then(|alias| {
5576 alias
5577 .strip_prefix('r')
5578 .and_then(|slot| slot.parse::<usize>().ok())
5579 .filter(|slot| *slot < REFERENCE_BATCH)
5580 .map(|_| alias)
5581 });
5582 match (alias, path.get(1).and_then(Value::as_str), path.len()) {
5583 (Some(alias), None, 1) => data.get(alias).is_some_and(Value::is_null),
5584 (Some(alias), Some("issue"), 2) => data
5585 .get(alias)
5586 .and_then(|repository| repository.get("issue"))
5587 .is_some_and(Value::is_null),
5588 _ => false,
5589 }
5590 })
5591 };
5592 let unresolved_only = parsed.as_ref().is_some_and(|parsed| {
5593 parsed.get("data").is_some_and(|data| {
5594 data.is_object()
5595 && parsed
5596 .get("errors")
5597 .and_then(Value::as_array)
5598 .is_some_and(|errors| errors.iter().all(|error| unresolved(data, error)))
5599 })
5600 });
5601 match parsed {
5602 Some(parsed) if unresolved_only => Ok(parsed["data"].clone()),
5603 _ => self.answer(body),
5604 }
5605 }
5606
5607 fn answer(&self, body: &str) -> Result<Value, SourceError> {
5608 let body: Value = serde_json::from_str(body).map_err(|e| SourceError::Malformed {
5609 message: format!("GitHub returned invalid JSON: {e}"),
5610 })?;
5611 let errors = body
5612 .get("errors")
5613 .map(|value| {
5614 value.as_array().ok_or_else(|| SourceError::Malformed {
5615 message: "GitHub response errors is not an array".into(),
5616 })
5617 })
5618 .transpose()?;
5619 if let Some(errors) = errors.filter(|errors| !errors.is_empty()) {
5620 let messages = errors
5621 .iter()
5622 .filter_map(|e| e.get("message").and_then(Value::as_str))
5623 .collect::<Vec<_>>()
5624 .join("; ");
5625 let message = if messages.is_empty() {
5626 "GitHub returned GraphQL errors".into()
5627 } else {
5628 messages
5629 };
5630 let normalized = message.to_ascii_lowercase();
5631 if normalized.contains("resource not accessible") || normalized.contains("scope") {
5632 return Err(SourceError::Auth {
5633 message: format!(
5634 "{message}; grant {} Projects and Issues read/write plus Pull requests read-only access for every repository represented on the board",
5635 self.credential_name
5636 ),
5637 });
5638 }
5639 return Err(SourceError::Refused { message });
5640 }
5641 body.get("data")
5642 .filter(|data| data.is_object())
5643 .cloned()
5644 .ok_or_else(|| SourceError::Malformed {
5645 message: "GitHub response has no data object".into(),
5646 })
5647 }
5648
5649 // llmlint: ignore[boundary_inputs_validated] GitHub caps nested connections at 100 and
5650 // GraphQL cannot independently page them inside the outer item page. This source page is
5651 // deliberately bounded at that published maximum; the live drift journey exercises it.
5652 async fn board_page(
5653 &self,
5654 items_after: Option<&str>,
5655 items_first: u32,
5656 ) -> Result<Value, SourceError> {
5657 let data = self
5658 .graphql(
5659 graphql::BOARD,
5660 json!({"owner":self.owner,"number":self.project_number,
5661 "first":items_first.min(MAX_PAGE_SIZE),"after":items_after,
5662 "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
5663 )
5664 .await?;
5665 data.pointer("/owner/projectV2")
5666 .filter(|v| !v.is_null())
5667 .cloned()
5668 .ok_or_else(|| SourceError::Refused {
5669 message: format!(
5670 "GitHub project {}/{} was not found or is not visible to the token",
5671 self.owner, self.project_number
5672 ),
5673 })
5674 }
5675
5676 /// The search that finds the issues of this board, narrowed by `also` when it is
5677 /// given.
5678 ///
5679 /// `project:owner/number` is what scopes a search to one board, and `is:issue` is what
5680 /// keeps pull requests out of it: GitHub's `ISSUE` search type covers both, and a pull
5681 /// request is somebody's change rather than a unit of plan. `-has:parent` is *not*
5682 /// here on purpose — GitHub accepts it and silently ignores it, so a project is told
5683 /// from a task by the `parent` field each issue carries rather than by the search.
5684 fn board_search(&self, also: Option<&str>) -> String {
5685 let scope = format!("project:{}/{} is:issue", self.owner, self.project_number);
5686 match also {
5687 Some(also) => format!("{scope} {also}"),
5688 None => scope,
5689 }
5690 }
5691
5692 /// One issue this source reached directly, as the board item a read of the board would
5693 /// have produced — or `None` when this board does not hold it.
5694 ///
5695 /// The board half of an issue rides along on `Issue.projectItems`, so the value handed
5696 /// to [`Self::resolve`] is the very shape a `ProjectV2.items` read gives it: the board
5697 /// item's own id, that item's field values, and the issue as its content. One resolver
5698 /// for both routes is what makes an issue read through a search, through its own node
5699 /// id, or through its project's sub-issues report the same title, the same status, the
5700 /// same labels and the same qualified id.
5701 ///
5702 /// An issue with no entry for *this* board is not this source's to report, which is
5703 /// what keeps an id naming some other repository's issue from being answered as an item
5704 /// of this board. That answer is given about an **exhausted** connection and never
5705 /// about an unread page: the entry is looked for on the page in hand, and only if that
5706 /// page reports more of the connection, in [`Self::board_membership`]'s walk of the
5707 /// rest of it.
5708 async fn resolve_issue(&self, issue: &Value) -> Result<Option<Resolved>, SourceError> {
5709 self.resolve_issue_as(issue, true).await
5710 }
5711
5712 /// [`Self::resolve_issue`], remembering what it resolves for this command's later reads
5713 /// and writes only when `remember` says to.
5714 async fn resolve_issue_as(
5715 &self,
5716 issue: &Value,
5717 remember: bool,
5718 ) -> Result<Option<Resolved>, SourceError> {
5719 if optional_str(issue, "__typename")? != Some("Issue") {
5720 return Ok(None);
5721 }
5722 let memberships = issue
5723 .get("projectItems")
5724 .ok_or_else(|| SourceError::Malformed {
5725 message: "GitHub issue is missing projectItems".into(),
5726 })?;
5727 let nodes = memberships
5728 .get("nodes")
5729 .and_then(Value::as_array)
5730 .ok_or_else(|| SourceError::Malformed {
5731 message: "GitHub issue projectItems.nodes is not an array".into(),
5732 })?;
5733 let held = match self.board_entry(nodes) {
5734 Some(held) => held.clone(),
5735 None => {
5736 let info = memberships
5737 .get("pageInfo")
5738 .ok_or_else(|| SourceError::Malformed {
5739 message: "GitHub issue projectItems has no pageInfo".into(),
5740 })?;
5741 // The page held no entry for this board. Whether that means the issue is
5742 // not on it is a question about the rest of the connection, and only a
5743 // connection with no rest answers it here.
5744 if !required_bool(info, "hasNextPage")? {
5745 return Ok(None);
5746 }
5747 let cursor = required_str(info, "endCursor")?;
5748 validate_cursor_progress(None, cursor)?;
5749 let issue_id = required_str(issue, "id")?;
5750 match self.board_membership(issue_id, cursor).await? {
5751 Some(held) => held,
5752 None => return Ok(None),
5753 }
5754 }
5755 };
5756 // The membership's own `updatedAt` rides along: it is the board half of the item's change
5757 // token, which a field change moves where the issue's own time does not.
5758 let item = json!({
5759 "id": required_str(&held, "id")?,
5760 "updatedAt": held.get("updatedAt"),
5761 "project": held.get("project"),
5762 "fieldValues": held.get("fieldValues"),
5763 "content": issue,
5764 });
5765 if remember {
5766 self.resolve(&item)
5767 } else {
5768 self.resolve_unremembered(&item)
5769 }
5770 }
5771
5772 /// This board's own entry among one page of an issue's `Issue.projectItems`.
5773 ///
5774 /// One spelling of *which membership is this board's*, so the page a read carries and
5775 /// the pages [`Self::board_membership`] walks are searched by the same rule.
5776 fn board_entry<'a>(&self, nodes: &'a [Value]) -> Option<&'a Value> {
5777 nodes.iter().find(|node| {
5778 node.pointer("/project/number").and_then(Value::as_u64)
5779 == Some(u64::from(self.project_number))
5780 })
5781 }
5782
5783 /// The rest of one issue's board memberships, from `after`, for this board's entry.
5784 ///
5785 /// The recovery read: a page of memberships that holds no entry for this board says
5786 /// nothing about the memberships past it, so the connection is walked to exhaustion
5787 /// before an issue is reported as one this board does not hold. `Ok(None)` is that
5788 /// positive answer — the whole connection was read and no entry named this board —
5789 /// rather than a failure, and the walk is held to
5790 /// [`validate_cursor_progress`] like every other page walk here, so a source answering
5791 /// with a cursor that does not advance is refused instead of spun on.
5792 async fn board_membership(
5793 &self,
5794 issue: &str,
5795 after: &str,
5796 ) -> Result<Option<Value>, SourceError> {
5797 let mut after = after.to_owned();
5798 loop {
5799 let data = self
5800 .graphql(
5801 graphql::ISSUE_BOARD_ITEMS,
5802 json!({"id":issue,"first":MAX_PAGE_SIZE,"after":after,
5803 "nestedFirst":NESTED_PAGE_SIZE}),
5804 )
5805 .await?;
5806 let Some(connection) = data
5807 .pointer("/node/projectItems")
5808 .filter(|value| !value.is_null())
5809 else {
5810 // The id resolved to nothing, or to something with no memberships to walk —
5811 // which is the same answer as a connection holding no entry for this board.
5812 return Ok(None);
5813 };
5814 let nodes = connection
5815 .get("nodes")
5816 .and_then(Value::as_array)
5817 .ok_or_else(|| SourceError::Malformed {
5818 message: "GitHub issue projectItems.nodes is not an array".into(),
5819 })?;
5820 if let Some(held) = self.board_entry(nodes) {
5821 return Ok(Some(held.clone()));
5822 }
5823 let info = connection
5824 .get("pageInfo")
5825 .ok_or_else(|| SourceError::Malformed {
5826 message: "GitHub issue projectItems has no pageInfo".into(),
5827 })?;
5828 let next = required_bool(info, "hasNextPage")?
5829 .then(|| required_str(info, "endCursor"))
5830 .transpose()?;
5831 match next {
5832 Some(next) => {
5833 validate_cursor_progress(Some(&after), next)?;
5834 after = next.to_owned();
5835 }
5836 None => return Ok(None),
5837 }
5838 }
5839 }
5840
5841 /// One page of a board-scoped issue search, and where the next page resumes.
5842 async fn search_page(
5843 &self,
5844 search: &str,
5845 first: u32,
5846 after: Option<&str>,
5847 ) -> Result<(Vec<Resolved>, Option<String>), SourceError> {
5848 let data = self
5849 .graphql(
5850 graphql::SEARCH_ISSUES,
5851 json!({"search":search,"type":"ISSUE","first":first.min(MAX_PAGE_SIZE),
5852 "after":after,"nestedFirst":NESTED_PAGE_SIZE,
5853 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
5854 )
5855 .await?;
5856 let connection = data.get("search").ok_or_else(|| SourceError::Malformed {
5857 message: "GitHub search response has no search connection".into(),
5858 })?;
5859 let mut found = Vec::new();
5860 for node in connection
5861 .get("nodes")
5862 .and_then(Value::as_array)
5863 .ok_or_else(|| SourceError::Malformed {
5864 message: "GitHub search nodes is not an array".into(),
5865 })?
5866 {
5867 if let Some(resolved) = self.resolve_issue(node).await? {
5868 found.push(resolved);
5869 }
5870 }
5871 let info = connection
5872 .get("pageInfo")
5873 .ok_or_else(|| SourceError::Malformed {
5874 message: "GitHub search connection has no pageInfo".into(),
5875 })?;
5876 let next = required_bool(info, "hasNextPage")?
5877 .then(|| required_str(info, "endCursor"))
5878 .transpose()?
5879 .map(str::to_owned);
5880 if let Some(next) = &next {
5881 validate_cursor_progress(after, next)?;
5882 }
5883 Ok((found, next))
5884 }
5885
5886 /// Every issue this board holds, completed with what this run wrote.
5887 ///
5888 /// The completion is not an optimisation and it is not a cache: GitHub's issue search
5889 /// is an index and is eventually consistent, so an issue this run created seconds ago
5890 /// can be absent from it, and a project listed straight after being written would
5891 /// otherwise be missing from its own board. What is added back is only what this
5892 /// process itself wrote, out of [`Self::created`], which lives and dies with the
5893 /// process.
5894 async fn board_issues(&self) -> Result<Vec<Resolved>, SourceError> {
5895 let found = self.searched_issues().await?;
5896 self.completed_with_written(found, |_| true)
5897 }
5898
5899 /// Every issue this board's own search reports, walked to exhaustion, read once per
5900 /// source.
5901 ///
5902 /// The uncompleted half of [`Self::board_issues`], separated because [`Self::board`]
5903 /// needs it too and the two would otherwise walk the same search twice in one command.
5904 /// See [`Self::search_cache`] for why holding it is the same bargain holding the board
5905 /// is.
5906 async fn searched_issues(&self) -> Result<Vec<Resolved>, SourceError> {
5907 let cached = self.search_cache()?.clone();
5908 if let Some(held) = cached {
5909 return Ok(held);
5910 }
5911 let mut after: Option<String> = None;
5912 let mut found = Vec::new();
5913 let search = self.board_search(None);
5914 loop {
5915 let (page, next) = self
5916 .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
5917 .await?;
5918 found.extend(page);
5919 match next {
5920 Some(next) => after = Some(next),
5921 None => break,
5922 }
5923 }
5924 *self.search_cache()? = Some(found.clone());
5925 Ok(found)
5926 }
5927
5928 /// Every issue this board's own search reports with `also` beside its scope, walked to
5929 /// exhaustion — held for no later read, which may not be narrowed alike.
5930 async fn searched_narrowly(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
5931 let mut after: Option<String> = None;
5932 let mut found = Vec::new();
5933 let search = self.board_search(Some(also));
5934 loop {
5935 let (page, next) = self
5936 .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
5937 .await?;
5938 found.extend(page);
5939 match next {
5940 Some(next) => after = Some(next),
5941 None => return Ok(found),
5942 }
5943 }
5944 }
5945
5946 /// This process's own view of the board's issues, or the refusal a poisoned lock is.
5947 fn search_cache(
5948 &self,
5949 ) -> Result<std::sync::MutexGuard<'_, Option<Vec<Resolved>>>, SourceError> {
5950 self.search_cache
5951 .lock()
5952 .map_err(|_| SourceError::Unavailable {
5953 message: "this source's view of the board's issues was left inconsistent by an \
5954 earlier failure; next: run the command again"
5955 .into(),
5956 })
5957 }
5958
5959 /// `found`, with everything this run wrote that `keep` accepts and the read did not
5960 /// report.
5961 ///
5962 /// See [`Self::created`] and [`Self::board_issues`] for why a read has to be completed
5963 /// at all: the search index is behind, and a node read of an item filed moments ago can
5964 /// be too.
5965 fn completed_with_written(
5966 &self,
5967 mut found: Vec<Resolved>,
5968 keep: impl Fn(&Resolved) -> bool,
5969 ) -> Result<Vec<Resolved>, SourceError> {
5970 for own in self.created()?.iter().filter(|own| keep(own)) {
5971 if !found.iter().any(|item| item.id == own.id) {
5972 found.push(own.clone());
5973 }
5974 }
5975 Ok(found)
5976 }
5977
5978 /// What resolving one node id reached.
5979 ///
5980 /// Three answers rather than an `Option`, because a board *draft* is none of the other
5981 /// two: it is not an issue, so the issue fragment reads nothing of it, and a read of one
5982 /// is completed by a read of the draft itself rather than reported as nothing.
5983 async fn reach(&self, id: &NativeId) -> Result<Reached, SourceError> {
5984 let asked = self
5985 .graphql(
5986 graphql::ISSUE,
5987 json!({"id":id.0,"first":MAX_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
5988 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
5989 )
5990 .await;
5991 let data = match asked {
5992 Ok(data) => data,
5993 // A string that is not a node id at all is not a failure to report: it is an id
5994 // this board does not hold, which is what every read of one already answers.
5995 Err(error) if unresolvable_node(&error) => return Ok(Reached::Nothing),
5996 Err(error) => return Err(error),
5997 };
5998 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
5999 return Ok(Reached::Nothing);
6000 };
6001 if optional_str(node, "__typename")? == Some("DraftIssue") {
6002 return Ok(Reached::Draft);
6003 }
6004 Ok(match self.resolve_issue(node).await? {
6005 Some(item) => Reached::Held(Box::new(item)),
6006 None => Reached::Nothing,
6007 })
6008 }
6009
6010 /// One item of this board by its own id, whatever kind it is.
6011 ///
6012 /// Resolved from the identifier alone: no search, board-wide or otherwise. What this
6013 /// run wrote is read first, because a node read of an item created moments ago can
6014 /// still be behind the board field values written onto it — see [`Self::created`].
6015 async fn item_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6016 if let Some(own) = self.created()?.iter().find(|own| own.id == *id) {
6017 return Ok(Some(own.clone()));
6018 }
6019 if let Some(held) = self.read_ahead()?.remove(id) {
6020 return Ok(held);
6021 }
6022 match self.reach(id).await? {
6023 Reached::Held(item) => Ok(Some(*item)),
6024 Reached::Nothing => Ok(None),
6025 Reached::Draft => self.draft_by_id(id).await,
6026 }
6027 }
6028
6029 /// [`Self::item_by_id`] for a document: one [`graphql::DOCUMENT_IN_PROJECT`] read, which
6030 /// answers the project it is filed under and that project's members beside it.
6031 ///
6032 /// Each is held for this command as a read of it would have been: the project for the
6033 /// next read of it by its id, and — when its page of members is the whole of them — the
6034 /// members for the next listing of that project. So a write held to who may read the
6035 /// document, which reads all three, is answered by this one request.
6036 async fn document_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6037 if self.created()?.iter().any(|own| own.id == *id) || self.read_ahead()?.contains_key(id) {
6038 return self.item_by_id(id).await;
6039 }
6040 let asked = self
6041 .graphql(
6042 graphql::DOCUMENT_IN_PROJECT,
6043 json!({"id":id.0,"first":MAX_PAGE_SIZE,"memberFirst":MEMBER_PAGE_SIZE,
6044 "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
6045 "duplicates":true}),
6046 )
6047 .await;
6048 let data = match asked {
6049 Ok(data) => data,
6050 Err(error) if unresolvable_node(&error) => return Ok(None),
6051 Err(error) => return Err(error),
6052 };
6053 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
6054 return Ok(None);
6055 };
6056 if optional_str(node, "__typename")? == Some("DraftIssue") {
6057 return self.draft_by_id(id).await;
6058 }
6059 let Some(item) = self.resolve_issue(node).await? else {
6060 return Ok(None);
6061 };
6062 // An answer for another issue than the one asked about says nothing of that one.
6063 if item.id != *id {
6064 return Err(SourceError::Malformed {
6065 message: format!(
6066 "GitHub answered issue {} when asked for document {}",
6067 item.id.0, id.0
6068 ),
6069 });
6070 }
6071 if let Some(project) = node.get("project").filter(|value| !value.is_null())
6072 && let Some(parent) = self
6073 .resolve_issue(project)
6074 .await?
6075 .filter(|parent| Some(&parent.id) == item.parent.as_ref())
6076 {
6077 let members = project.get("subIssues").filter(|value| !value.is_null());
6078 let whole = members
6079 .and_then(|members| members.pointer("/pageInfo/hasNextPage"))
6080 .and_then(Value::as_bool)
6081 == Some(false);
6082 if whole {
6083 let nodes = members
6084 .and_then(|members| members.get("nodes"))
6085 .and_then(Value::as_array)
6086 .ok_or_else(|| SourceError::Malformed {
6087 message: "GitHub subIssues.nodes is not an array".into(),
6088 })?;
6089 let mut children = Vec::new();
6090 for member in nodes {
6091 if let Some(resolved) = self.resolve_issue(member).await? {
6092 children.push(resolved);
6093 }
6094 }
6095 // Held as the project's members only when every one resolved and says it is
6096 // filed under it: a page with any other answers nothing of all the members.
6097 if children.len() == nodes.len()
6098 && children
6099 .iter()
6100 .all(|child| child.parent.as_ref() == Some(&parent.id))
6101 {
6102 self.children_cache()?
6103 .insert(parent.id.clone(), (parent.id.clone(), children));
6104 }
6105 }
6106 self.read_ahead()?.insert(parent.id.clone(), Some(parent));
6107 }
6108 Ok(Some(item))
6109 }
6110
6111 /// Several items of this board, each by its own id, in order — what [`Self::item_by_id`]
6112 /// answers for each, read [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] rather
6113 /// than one request per id.
6114 ///
6115 /// What this run wrote answers first, as it does there, and only the rest is read. One id
6116 /// left to read is read by [`Self::item_by_id`] itself, which costs what a batch does. A
6117 /// batch GitHub refuses because one of its ids resolves to no node at all is read again one
6118 /// id at a time, so that id is answered as not held and the others as themselves; a draft
6119 /// is completed by a read of the draft, exactly as there.
6120 async fn items_by_ids(&self, ids: &[NativeId]) -> Result<Vec<Option<Resolved>>, SourceError> {
6121 let mut found: Vec<Option<Option<Resolved>>> = {
6122 let created = self.created()?;
6123 ids.iter()
6124 .map(|id| {
6125 created
6126 .iter()
6127 .find(|own| own.id == *id)
6128 .map(|own| Some(own.clone()))
6129 })
6130 .collect()
6131 };
6132 let unread: Vec<NativeId> = ids
6133 .iter()
6134 .zip(&found)
6135 .filter(|(_, found)| found.is_none())
6136 .map(|(id, _)| id.clone())
6137 .collect();
6138 let mut read = Vec::with_capacity(unread.len());
6139 if let [one] = unread.as_slice() {
6140 read.push(self.item_by_id(one).await?);
6141 } else {
6142 for batch in unread.chunks(DETAIL_BATCH) {
6143 let data = match self
6144 .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, None))
6145 .await
6146 {
6147 Ok(data) => data,
6148 Err(error) if unresolvable_node(&error) => {
6149 for id in batch {
6150 read.push(self.item_by_id(id).await?);
6151 }
6152 continue;
6153 }
6154 Err(error) => return Err(error),
6155 };
6156 for (slot, id) in batch.iter().enumerate() {
6157 let node =
6158 data.get(format!("i{slot}"))
6159 .ok_or_else(|| SourceError::Malformed {
6160 message: format!(
6161 "GitHub answered a batch read with no item for {}",
6162 id.0
6163 ),
6164 })?;
6165 read.push(if node.is_null() {
6166 None
6167 } else if optional_str(node, "__typename")? == Some("DraftIssue") {
6168 self.draft_by_id(id).await?
6169 } else {
6170 if optional_str(node, "__typename")? == Some("Issue")
6171 && required_str(node, "id")? != id.0
6172 {
6173 return Err(SourceError::Malformed {
6174 message: format!(
6175 "GitHub answered the read of {} with issue {}",
6176 id.0,
6177 required_str(node, "id")?
6178 ),
6179 });
6180 }
6181 self.resolve_issue(node).await?
6182 });
6183 }
6184 }
6185 }
6186 let mut read = read.into_iter();
6187 Ok(found
6188 .iter_mut()
6189 .map(|slot| slot.take().unwrap_or_else(|| read.next().flatten()))
6190 .collect())
6191 }
6192
6193 fn read_ahead(
6194 &self,
6195 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Option<Resolved>>>, SourceError> {
6196 self.read_ahead
6197 .lock()
6198 .map_err(|_| SourceError::Unavailable {
6199 message:
6200 "items read ahead for a copy were left inconsistent; run the command again"
6201 .into(),
6202 })
6203 }
6204
6205 fn resolved_cache(
6206 &self,
6207 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
6208 self.resolved_cache
6209 .lock()
6210 .map_err(|_| SourceError::Unavailable {
6211 message: "resolved item records were left inconsistent; run the command again"
6212 .into(),
6213 })
6214 }
6215
6216 /// Reuse a record this invocation already resolved. The mutation sender invalidates
6217 /// it before writing, so a partial failure cannot leave a pre-write binding behind.
6218 async fn bound_item(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6219 let cached = self.resolved_cache()?.get(id).cloned();
6220 if let Some(item) = cached {
6221 return Ok(Some(item));
6222 }
6223 self.item_by_id(id).await
6224 }
6225
6226 /// Take out the item an issue read by reference reached for `id`, when one is held: the
6227 /// status write it was read for is the one caller, so no other read or write is ever
6228 /// answered from a read that selected only what that write needs.
6229 fn take_held(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6230 Ok(self
6231 .held_cache
6232 .lock()
6233 .map_err(|_| SourceError::Unavailable {
6234 message: "items read by issue reference were left inconsistent; run the \
6235 command again"
6236 .into(),
6237 })?
6238 .remove(id))
6239 }
6240
6241 /// One board draft by its own id, with the board item it sits in — or `None` when no
6242 /// item of this board is that draft's.
6243 ///
6244 /// The same decision [`Self::resolve_issue`] makes for an issue, over the draft's own
6245 /// `projectV2Items`: an entry naming this board is what makes it this board's. GitHub
6246 /// links a draft to one board item, so the page this read carries is the whole of that
6247 /// connection, and a page that reports more than it holds is refused rather than read
6248 /// as an answer about memberships nobody read.
6249 async fn draft_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6250 let data = self
6251 .graphql(
6252 graphql::DRAFT,
6253 json!({"id":id.0,"nestedFirst":NESTED_PAGE_SIZE,
6254 "boardItems":BOARD_ITEMS_PAGE_SIZE}),
6255 )
6256 .await?;
6257 // Gone between the two reads is an answer — the draft is no longer there. Anything
6258 // else than the draft [`Self::reach`] was just told this id is, is not one.
6259 let Some(draft) = data.get("node").filter(|node| !node.is_null()) else {
6260 return Ok(None);
6261 };
6262 if optional_str(draft, "__typename")? != Some("DraftIssue") {
6263 return Err(SourceError::Malformed {
6264 message: format!(
6265 "GitHub answered {} as a draft and then as something else",
6266 id.0
6267 ),
6268 });
6269 }
6270 if required_str(draft, "id")? != id.0 {
6271 return Err(SourceError::Malformed {
6272 message: format!("GitHub answered a different draft for {}", id.0),
6273 });
6274 }
6275 let memberships = draft
6276 .get("projectV2Items")
6277 .ok_or_else(|| SourceError::Malformed {
6278 message: format!("GitHub draft {} is missing projectV2Items", id.0),
6279 })?;
6280 let nodes = memberships
6281 .get("nodes")
6282 .and_then(Value::as_array)
6283 .ok_or_else(|| SourceError::Malformed {
6284 message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
6285 })?;
6286 let info = memberships
6287 .get("pageInfo")
6288 .ok_or_else(|| SourceError::Malformed {
6289 message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
6290 })?;
6291 // Read whether or not this board's entry is on the page: a page claiming more than
6292 // the one item GitHub links a draft to is a malformed answer either way.
6293 if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
6294 return Err(SourceError::Malformed {
6295 message: format!(
6296 "GitHub draft {} reports more board items than the one GitHub links a draft \
6297 to",
6298 id.0
6299 ),
6300 });
6301 }
6302 if let Some(node) = nodes.first()
6303 && node
6304 .pointer("/project/number")
6305 .and_then(Value::as_u64)
6306 .is_none()
6307 {
6308 return Err(SourceError::Malformed {
6309 message: format!(
6310 "GitHub draft {} board item has no numeric project number",
6311 id.0
6312 ),
6313 });
6314 }
6315 let Some(held) = self.board_entry(nodes) else {
6316 return Ok(None);
6317 };
6318 if required_str(
6319 held.get("project").ok_or_else(|| SourceError::Malformed {
6320 message: format!("GitHub draft {} board item has no project", id.0),
6321 })?,
6322 "id",
6323 )? != self.board_fields().await?.id.as_str()
6324 {
6325 return Ok(None);
6326 }
6327 let item = json!({
6328 "id": required_str(held, "id")?,
6329 "project": held.get("project"),
6330 "fieldValues": held.get("fieldValues"),
6331 "content": draft,
6332 });
6333 self.resolve(&item)
6334 }
6335
6336 /// The board's own id and field definitions, for a write whose item does not carry
6337 /// them — never its items.
6338 ///
6339 /// A board this command has already listed supplies them, since it read them beside its
6340 /// items; otherwise they come from [`graphql::BOARD_FIELDS`], once per command. Neither
6341 /// is consulted about which items the board holds: see the module documentation for
6342 /// why a question about one known item is answered by reading that item.
6343 async fn board_fields(&self) -> Result<BoardFields, SourceError> {
6344 if let Some(board) = self.board_cache()?.as_ref() {
6345 return Ok(BoardFields {
6346 id: BoardId::parse(&board.id)?,
6347 fields: board.fields.clone(),
6348 });
6349 }
6350 if let Some(held) = self.fields_cache()?.clone() {
6351 return Ok(held);
6352 }
6353 let data = self
6354 .graphql(
6355 graphql::BOARD_FIELDS,
6356 json!({"owner":self.owner,"number":self.project_number,
6357 "nestedFirst":NESTED_PAGE_SIZE}),
6358 )
6359 .await?;
6360 self.fields_read(&data)
6361 }
6362
6363 /// The board's id and fields out of an answer carrying the `boardFields` root, held for
6364 /// the rest of this command.
6365 fn fields_read(&self, data: &Value) -> Result<BoardFields, SourceError> {
6366 let board = data
6367 .pointer("/boardFields/projectV2")
6368 .filter(|value| !value.is_null())
6369 .ok_or_else(|| SourceError::Refused {
6370 message: format!(
6371 "GitHub project {}/{} was not found or is not visible to the token",
6372 self.owner, self.project_number
6373 ),
6374 })?;
6375 let read = BoardFields {
6376 id: BoardId::parse(required_str(board, "id")?)?,
6377 fields: board.get("fields").cloned().unwrap_or(Value::Null),
6378 };
6379 *self.fields_cache()? = Some(read.clone());
6380 Ok(read)
6381 }
6382
6383 /// Read what creating an issue in `repository` needs and this command has not read yet —
6384 /// the board's fields and the repository's node id — in one request when it needs both.
6385 ///
6386 /// When either is already known this sends nothing, and the other is read by its own
6387 /// document where it is asked for, so no create reads anything twice.
6388 async fn creation_context(
6389 &self,
6390 repository: &RepositoryTarget,
6391 incoming: &Incoming<'_>,
6392 ) -> Result<(), SourceError> {
6393 let fields_known = self.board_cache()?.is_some() || self.fields_cache()?.is_some();
6394 if fields_known || self.repository_cache()?.contains_key(repository) {
6395 return Ok(());
6396 }
6397 // A placement an earlier process kept is taken only while the project issue this create
6398 // has just read shows its setup-context token unchanged; see `projection`.
6399 if self.placed_by_kept(repository, incoming.parent) {
6400 return Ok(());
6401 }
6402 let data = self
6403 .graphql(
6404 graphql::CREATION_CONTEXT,
6405 json!({"owner":self.owner,"number":self.project_number,
6406 "nestedFirst":NESTED_PAGE_SIZE,"repositoryOwner":repository.owner,
6407 "repositoryName":repository.name}),
6408 )
6409 .await?;
6410 self.fields_read(&data)?;
6411 self.repository_read(&data, repository, incoming)?;
6412 Ok(())
6413 }
6414
6415 /// This process's own view of the board's fields, or the refusal a poisoned lock is.
6416 fn fields_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<BoardFields>>, SourceError> {
6417 self.fields_cache
6418 .lock()
6419 .map_err(|_| SourceError::Unavailable {
6420 message: "this source's view of the board's fields was left inconsistent by an \
6421 earlier failure; next: run the command again"
6422 .into(),
6423 })
6424 }
6425
6426 /// What a write to `item` needs of the board, read off that item when it says enough and
6427 /// off [`Self::board_fields`] when it does not.
6428 ///
6429 /// A node read of an item names its board and carries the definition of every field it
6430 /// holds a value of — so an item naming its board, holding a value of the origin field
6431 /// when the write carries an origin, and, when the write carries a status, holding a
6432 /// `Status` value, needs no read of the board at all. **Nothing the item does not say is
6433 /// guessed:** a field it holds no value of may still be on the board, and a view reading it
6434 /// as absent would refuse a write the board can take or skip a field write the board
6435 /// needs, so such an item — and a create, which has no item yet — takes the board's fields
6436 /// from their own read instead. A write carrying no origin to an item holding none — every
6437 /// write out of a source declared private — writes no origin whatever the board defines,
6438 /// so it needs no definition of that field.
6439 async fn fields_for(
6440 &self,
6441 item: Option<&Resolved>,
6442 writes_origin: bool,
6443 writes_status: bool,
6444 selects_priority: bool,
6445 ) -> Result<BoardFields, SourceError> {
6446 if let Some(board) = item.and_then(Resolved::carried_board) {
6447 return Ok(board);
6448 }
6449 if let Some(item) = item
6450 && let Some(board_id) = item.named_board()
6451 && (!writes_origin || item.defines(ORIGIN_FIELD))
6452 && (!writes_status || item.defines("Status"))
6453 && (!selects_priority || item.defines(PRIORITY_FIELD))
6454 && self
6455 .metadata_fields
6456 .iter()
6457 .all(|projection| item.defines(&projection.field))
6458 {
6459 return Ok(BoardFields {
6460 id: board_id,
6461 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
6462 });
6463 }
6464 self.board_fields().await
6465 }
6466
6467 /// Everything filed under one issue of this board, walked to exhaustion — or `None`
6468 /// when that id names nothing here with a sub-issue relationship to walk.
6469 ///
6470 /// `None` and an empty answer are different: `None` is *this is not an issue of this
6471 /// GitHub*, which is what sends a project selector on to be read as a name, and an
6472 /// empty vector is a project that holds nothing.
6473 async fn sub_issues(&self, id: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
6474 let mut after: Option<String> = None;
6475 let mut children = Vec::new();
6476 // The project issue as the first page carried it, held once the walk has ended.
6477 let mut parent: Option<Resolved> = None;
6478 loop {
6479 let asked = self
6480 .graphql(
6481 graphql::SUB_ISSUES,
6482 json!({"id":id.0,"memberFirst":MEMBER_PAGE_SIZE,"after":after,
6483 "nestedFirst":NESTED_PAGE_SIZE,
6484 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
6485 )
6486 .await;
6487 let data = match asked {
6488 Ok(data) => data,
6489 // A string that is not a node id at all is not a failure to report: it is
6490 // the ordinary answer to a selector naming a project by its name.
6491 Err(error) if unresolvable_node(&error) => return Ok(None),
6492 Err(error) => return Err(error),
6493 };
6494 let Some(connection) = data
6495 .pointer("/node/subIssues")
6496 .filter(|value| !value.is_null())
6497 else {
6498 // No such node, or one with no sub-issue relationship — a board draft is
6499 // the one this board can really hold.
6500 return Ok(None);
6501 };
6502 if after.is_none()
6503 && let Some(node) = data.get("node").filter(|node| node.get("id").is_some())
6504 {
6505 parent = self
6506 .resolve_issue(node)
6507 .await?
6508 .filter(|resolved| resolved.id == *id);
6509 }
6510 for node in connection
6511 .get("nodes")
6512 .and_then(Value::as_array)
6513 .ok_or_else(|| SourceError::Malformed {
6514 message: "GitHub subIssues.nodes is not an array".into(),
6515 })?
6516 {
6517 if let Some(resolved) = self.resolve_issue(node).await? {
6518 children.push(resolved);
6519 }
6520 }
6521 let info = connection
6522 .get("pageInfo")
6523 .ok_or_else(|| SourceError::Malformed {
6524 message: "GitHub subIssues connection has no pageInfo".into(),
6525 })?;
6526 let next = required_bool(info, "hasNextPage")?
6527 .then(|| required_str(info, "endCursor"))
6528 .transpose()?;
6529 match next {
6530 Some(next) => {
6531 validate_cursor_progress(after.as_deref(), next)?;
6532 after = Some(next.to_owned());
6533 }
6534 None => {
6535 // Held only once the walk has ended, so one failing part way holds
6536 // nothing and every read it would have answered is made.
6537 // The project issue the first page carried answers the next read of the
6538 // project by its id in this command, as a read of it made now would: what
6539 // classifying a project for a write reads straight after its members.
6540 if let Some(parent) = &parent {
6541 self.read_ahead()?
6542 .entry(parent.id.clone())
6543 .or_insert_with(|| Some(parent.clone()));
6544 }
6545 let mut carried = self.carried()?;
6546 for item in parent.into_iter().chain(children.iter().cloned()) {
6547 carried.insert(item.id.clone(), item);
6548 }
6549 return Ok(Some(children));
6550 }
6551 }
6552 }
6553 }
6554
6555 fn carried(
6556 &self,
6557 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
6558 self.carried.lock().map_err(|_| SourceError::Unavailable {
6559 message: "items a project's listing carried were left inconsistent; run the \
6560 command again"
6561 .into(),
6562 })
6563 }
6564
6565 /// Which issue of this board a project *name* is, or `None` when none is.
6566 ///
6567 /// One bounded query which filters on that name at the server, rather than a walk of
6568 /// every issue the board holds. The name is compared again here: the qualifier narrows
6569 /// what GitHub sends, and this source decides what it names.
6570 async fn project_by_name(&self, name: &str) -> Result<Option<NativeId>, SourceError> {
6571 let search = self.board_search(Some(&title_qualifier(name)));
6572 let mut after = None;
6573 loop {
6574 let (candidates, next) = self
6575 .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
6576 .await?;
6577 if let Some(item) = candidates.into_iter().find(|item| {
6578 item.kind == BoardKind::Work(ItemKind::Project)
6579 && item.title.eq_ignore_ascii_case(name)
6580 }) {
6581 return Ok(Some(item.id));
6582 }
6583 match next {
6584 Some(next) => after = Some(next),
6585 None => return Ok(None),
6586 }
6587 }
6588 }
6589
6590 /// Everything filed under one project of this board: the sub-issues of the issue that
6591 /// project is.
6592 ///
6593 /// Tasks *and* documents, because a document filed under a project is a sub-issue of it
6594 /// too — the caller keeps the kind it asked for. Nothing about this grows as the board
6595 /// gains projects, or as another project gains tasks.
6596 ///
6597 /// A qualified id names the issue and is asked for its sub-issues directly: one
6598 /// request, no search of any kind. Only a selector GitHub cannot resolve that way is
6599 /// read as a project *name*, which costs the one bounded search
6600 /// [`Self::project_by_name`] makes.
6601 ///
6602 /// What GitHub answered is held for the rest of the command — see [`Self::children_cache`]
6603 /// — and each answer is still cut to the items whose parent is this project, so one this
6604 /// process has since filed elsewhere is not reported here, and completed with what this
6605 /// process filed under it.
6606 async fn project_children(&self, selector: &NativeId) -> Result<Vec<Resolved>, SourceError> {
6607 let held = self.children_cache()?.get(selector).cloned();
6608 let (project, children) = match held {
6609 Some(held) => held,
6610 None => {
6611 let answered = match self.sub_issues(selector).await? {
6612 Some(children) => (selector.clone(), children),
6613 None => match self.project_by_name(&selector.0).await? {
6614 Some(project) => {
6615 let children = self.sub_issues(&project).await?.unwrap_or_default();
6616 (project, children)
6617 }
6618 None => return Ok(Vec::new()),
6619 },
6620 };
6621 self.children_cache()?
6622 .insert(selector.clone(), answered.clone());
6623 answered
6624 }
6625 };
6626 let mut children: Vec<Resolved> = children
6627 .into_iter()
6628 .filter(|child| child.parent.as_ref() == Some(&project))
6629 .collect();
6630 for own in self.updated()?.iter() {
6631 if own.parent.as_ref() == Some(&project) && !children.iter().any(|c| c.id == own.id) {
6632 children.push(own.clone());
6633 }
6634 }
6635 self.completed_with_written(children, |own| own.parent.as_ref() == Some(&project))
6636 }
6637
6638 /// Every issue of this board GitHub's issue search reports updated at or after `since`,
6639 /// completed with what this run wrote — the candidates a comment-activity read confirms.
6640 ///
6641 /// Scoped by the board and by nothing else: `project:<owner>/<number>` reaches every issue
6642 /// on the board whatever repository, and whatever owner, it lives in, so no repository or
6643 /// owner qualifier is added and none is needed. What makes the `updated:` qualifier
6644 /// sufficient is a fact about GitHub rather than about this source: a comment written on an
6645 /// issue **and a comment edited on it** both move that issue's `updatedAt`. The credentialed
6646 /// journey `an_edited_comment_moves_its_issue_and_is_selected_since` in `tests/journey`
6647 /// re-takes that fact on every run of the lane, so a change on GitHub's side fails there
6648 /// rather than silently narrowing a caller's answer.
6649 ///
6650 /// The instant is written to the second, rounded down, which can only widen what the
6651 /// search returns; confirmation against each candidate's own comments is what makes the
6652 /// answer exact. The search is an index that lags a write by a second or two — the module
6653 /// documentation records it — so a caller that asks again from its last instant should
6654 /// overlap the two by more than that.
6655 async fn updated_since(&self, since: DateTime<Utc>) -> Result<Vec<Resolved>, SourceError> {
6656 let found = self.searched(&updated_qualifier(since)).await?;
6657 self.completed_with_written(found, |_| true)
6658 }
6659
6660 /// Every issue of this board GitHub's issue search reports for the board-scoped search
6661 /// narrowed by `also`, in pages of [`SEARCH_PAGE_SIZE`].
6662 ///
6663 /// Uncompleted: what this process wrote is added by the caller, which knows whether its
6664 /// own record is the fresher of the two.
6665 async fn searched(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
6666 let search = self.board_search(Some(also));
6667 let mut after: Option<String> = None;
6668 let mut found = Vec::new();
6669 loop {
6670 let (page, next) = self
6671 .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
6672 .await?;
6673 found.extend(page);
6674 match next {
6675 Some(next) => after = Some(next),
6676 None => return Ok(found),
6677 }
6678 }
6679 }
6680
6681 /// A bounded task answer; the versioned cursor carries the connection position, how
6682 /// many rows of the page starting there were already handed out, and the own-write ids
6683 /// already observed, including across a new source instance.
6684 ///
6685 /// Every page is sent at [`SEARCH_PAGE_SIZE`] whatever the caller's limit, and a limit is
6686 /// sliced from the pages it needs; why is the module documentation's paging contract.
6687 async fn search_tasks(
6688 &self,
6689 query: &TaskQuery,
6690 page: &PageRequest,
6691 also: &str,
6692 ) -> Result<Page<Task>, SourceError> {
6693 let mut position = match &page.cursor {
6694 None => SearchPosition::default(),
6695 Some(cursor) => serde_json::from_str::<SearchPosition>(&cursor.0)
6696 .ok()
6697 .filter(|position| {
6698 position.version == SEARCH_CURSOR_VERSION
6699 && position.connection.valid_resume(position.offset)
6700 })
6701 .ok_or_else(|| SourceError::Config {
6702 message: "page cursor is invalid".into(),
6703 })?,
6704 };
6705 let search = self.board_search(Some(also));
6706 let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
6707 let own = self.with_own_writes(Vec::new())?;
6708 // An issue this process commented on is a candidate of a comment-activity read
6709 // whether or not the search has caught up with the comment; see `Self::commented`.
6710 let commented = match query.commented_since {
6711 Some(_) => self.commented()?.clone(),
6712 None => Vec::new(),
6713 };
6714 for id in own.iter().map(|item| &item.id).chain(&commented) {
6715 if !position.own.contains(id) {
6716 position.own.push(id.clone());
6717 }
6718 }
6719 let mut tasks = Vec::new();
6720 while !position.connection.exhausted() && tasks.len() < limit {
6721 let first = SEARCH_PAGE_SIZE;
6722 // Page size is part of the key: a short cached answer cannot answer a wider ask.
6723 let key =
6724 serde_json::to_string(&("page", &search, &position.connection.after(), first))
6725 .expect("search page key is serializable");
6726 let cached = if query.commented_since.is_none() {
6727 self.narrowed_cache()?.get(&key).cloned()
6728 } else {
6729 None
6730 };
6731 let (found, next) = match cached {
6732 Some(found) => {
6733 let next = self
6734 .search_next
6735 .lock()
6736 .map_err(|_| SourceError::Unavailable {
6737 message:
6738 "search pagination was left inconsistent; run the command again"
6739 .into(),
6740 })?
6741 .get(&key)
6742 .cloned()
6743 .flatten();
6744 (found, next)
6745 }
6746 None => {
6747 let (found, next) = self
6748 .search_page(&search, first, position.connection.after())
6749 .await?;
6750 if query.commented_since.is_none() {
6751 self.search_next
6752 .lock()
6753 .map_err(|_| SourceError::Unavailable {
6754 message:
6755 "search pagination was left inconsistent; run the command again"
6756 .into(),
6757 })?
6758 .insert(key.clone(), next.clone());
6759 self.narrowed_cache()?.insert(key, found.clone());
6760 }
6761 (found, next)
6762 }
6763 };
6764 let rows = found.len();
6765 for mut item in found.into_iter().skip(position.offset) {
6766 if tasks.len() == limit {
6767 break;
6768 }
6769 position.offset += 1;
6770 if position.own.contains(&item.id) {
6771 if position.seen.contains(&item.id) {
6772 continue;
6773 }
6774 position.seen.push(item.id.clone());
6775 // The search's own copy of an issue this process only commented on is as
6776 // good as a node read of it, since its comments are read either way.
6777 let only_commented = commented.contains(&item.id)
6778 && !own.iter().any(|written| written.id == item.id);
6779 if !only_commented {
6780 let updated_at = item.updated_at;
6781 let Some(written) = self.search_written(&own, &item.id).await? else {
6782 continue;
6783 };
6784 item = written;
6785 item.updated_at = item.updated_at.max(updated_at);
6786 self.resolved_cache()?.insert(item.id.clone(), item.clone());
6787 }
6788 }
6789 if item.kind == BoardKind::Work(ItemKind::Task) {
6790 let task = item.task()?;
6791 if task_matches(&task, query, &query.project)
6792 && self.commented_since(&item, query.commented_since).await?
6793 {
6794 tasks.push(task);
6795 }
6796 }
6797 }
6798 if position.offset < rows {
6799 continue;
6800 }
6801 position.offset = 0;
6802 position.connection = match next {
6803 Some(after) => SearchConnection::Continuing {
6804 after: Cursor(after),
6805 },
6806 None => SearchConnection::Exhausted {},
6807 };
6808 }
6809 if position.connection.exhausted() {
6810 for id in position.own.clone() {
6811 if position.seen.contains(&id) {
6812 continue;
6813 }
6814 if tasks.len() == limit {
6815 break;
6816 }
6817 position.seen.push(id.clone());
6818 let Some(item) = self.search_written(&own, &id).await? else {
6819 continue;
6820 };
6821 if item.kind == BoardKind::Work(ItemKind::Task) {
6822 let task = item.task()?;
6823 if task_matches(&task, query, &query.project)
6824 && self.commented_since(&item, query.commented_since).await?
6825 {
6826 tasks.push(task);
6827 }
6828 }
6829 }
6830 }
6831 let more = !position.connection.exhausted()
6832 || position.own.iter().any(|id| !position.seen.contains(id));
6833 Ok(Page {
6834 items: tasks,
6835 next: more.then(|| {
6836 Cursor(serde_json::to_string(&position).expect("search position is serializable"))
6837 }),
6838 })
6839 }
6840
6841 /// A resumed process has the ids but no write records; resolve only a record the
6842 /// current page needs, by its uncached node read rather than the lagging search index.
6843 async fn search_written(
6844 &self,
6845 own: &[Resolved],
6846 id: &NativeId,
6847 ) -> Result<Option<Resolved>, SourceError> {
6848 match own.iter().find(|item| item.id == *id) {
6849 Some(item) => Ok(Some(item.clone())),
6850 None => self.item_by_id(id).await,
6851 }
6852 }
6853
6854 /// The candidates for a task query carrying a text, metadata or origin predicate, read
6855 /// without enumerating the board — or `None` for a query carrying none of the three, which
6856 /// keeps the reads it always had.
6857 ///
6858 /// An origin is answered by [`Self::origin_candidates`], whatever else the query carries,
6859 /// because it names at most a handful of items. Text and metadata are answered by one
6860 /// board-scoped issue search carrying every term — see [`narrowing_qualifiers`] — narrowed
6861 /// further by `updated:>=` when the query also asks for comment activity, since both
6862 /// qualifiers must hold of an issue the answer keeps. Every candidate is confirmed in
6863 /// process afterwards by the same predicates [`task_matches`] applies to every read.
6864 ///
6865 /// Completed with what this process wrote, its own record winning over the index's copy
6866 /// of the same item: see [`Self::with_own_writes`].
6867 async fn narrowed(&self, query: &TaskQuery) -> Result<Option<Vec<Resolved>>, SourceError> {
6868 self.send_pending_origins(None).await?;
6869 let asked = match (&query.origin, narrowing_qualifiers(query)) {
6870 (Some(origin), _) => Narrowing::Origin(origin.clone()),
6871 (None, Some(qualifiers)) => Narrowing::Search(match query.commented_since {
6872 Some(since) => format!("{} {qualifiers}", updated_qualifier(since)),
6873 None => qualifiers,
6874 }),
6875 (None, None) => return Ok(None),
6876 };
6877 // A question about comment activity is asked afresh every time, as it always was: it
6878 // is the one a caller polls from one source while waiting for the index, and an
6879 // answer held from the first poll would be the answer to every later one.
6880 let key = query.commented_since.is_none().then(|| asked.key());
6881 let cached = match &key {
6882 Some(key) => self.narrowed_cache()?.get(key).cloned(),
6883 None => None,
6884 };
6885 let found = match cached {
6886 Some(found) => found,
6887 None => {
6888 let found = match &asked {
6889 Narrowing::Origin(origin) => self.origin_candidates(origin).await?,
6890 Narrowing::Search(also) => self.searched(also).await?,
6891 };
6892 if let Some(key) = key {
6893 self.narrowed_cache()?.insert(key, found.clone());
6894 }
6895 found
6896 }
6897 };
6898 self.with_own_writes(found).map(Some)
6899 }
6900
6901 /// The candidates for a project or unscoped document query carrying a searchable text,
6902 /// read without enumerating the board — or `None` for a query with no text or a blank one,
6903 /// which keeps the read it always had.
6904 ///
6905 /// The text is sent as the very phrase a task query's text is — see [`text_qualifiers`] —
6906 /// in one board-scoped issue search walked to its end at [`SEARCH_PAGE_SIZE`], so what it
6907 /// costs is the issues that match and never the board. Its answer is held for the command
6908 /// under the same key [`Self::narrowed`] holds that search under, so a walk of the caller's
6909 /// pages asks GitHub once. Every candidate is confirmed afterwards by its kind and by the
6910 /// substring rule, exactly as an item of the wider read was, and is completed with what this
6911 /// process wrote: see [`Self::with_own_writes`].
6912 async fn text_searched(
6913 &self,
6914 text: Option<&TextQuery>,
6915 ) -> Result<Option<Vec<Resolved>>, SourceError> {
6916 let Some(also) = text_qualifiers(text) else {
6917 return Ok(None);
6918 };
6919 let key = Narrowing::Search(also.clone()).key();
6920 let cached = self.narrowed_cache()?.get(&key).cloned();
6921 let found = match cached {
6922 Some(found) => found,
6923 None => {
6924 let found = self.searched(&also).await?;
6925 self.narrowed_cache()?.insert(key, found.clone());
6926 found
6927 }
6928 };
6929 self.with_own_writes(found).map(Some)
6930 }
6931
6932 /// Every `kind` item of this board whose own origin is exactly `origin`: the candidates
6933 /// [`Self::narrowed`] reads for a task query carrying that origin — one
6934 /// [`graphql::ORIGIN_LOOKUP`] walk, held for the command under the same key, completed with
6935 /// what this process wrote — each confirmed against its own origin entry.
6936 ///
6937 /// What answers a project or document listing by origin, so finding every copy of one plan
6938 /// costs the items that carry its origin and never a page of the board.
6939 async fn copied_from(
6940 &self,
6941 origin: &str,
6942 kind: BoardKind,
6943 ) -> Result<Vec<Resolved>, SourceError> {
6944 let wanted = TaskQuery {
6945 origin: Some(origin.to_owned()),
6946 ..TaskQuery::default()
6947 };
6948 Ok(self
6949 .narrowed(&wanted)
6950 .await?
6951 .unwrap_or_default()
6952 .into_iter()
6953 .filter(|item| item.kind == kind && wanted.origin_matches(&item.metadata()))
6954 .collect())
6955 }
6956
6957 /// Every item of this board that may carry `origin` — a superset of those that do — found
6958 /// by [`graphql::ORIGIN_LOOKUP`] and never by enumerating the board.
6959 ///
6960 /// The union of the board's own field filter over the `onetaskgraph.origin` text field —
6961 /// which reads the field every carrier holds, whichever release wrote it — and the
6962 /// board-scoped issue search for the same id as a phrase in the body, where this source
6963 /// mirrors it. The caller adds the third read, this process's own writes. Candidates are
6964 /// returned unconfirmed; [`task_matches`] compares each one's own origin field with the
6965 /// query's, exactly.
6966 ///
6967 /// Both connections are walked to exhaustion, each from its own cursor. One that has
6968 /// already ended is sent its last cursor again, which answers an empty page, so the one
6969 /// document serves every page of either. What the two leave is stated in the module
6970 /// documentation: a carrier another process added within the last second or two, before
6971 /// either index has it.
6972 async fn origin_candidates(&self, origin: &str) -> Result<Vec<Resolved>, SourceError> {
6973 let filter = format!("{ORIGIN_FIELD}:{}", quoted(origin));
6974 let search = self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))));
6975 let mut items_after: Option<String> = None;
6976 let mut search_after: Option<String> = None;
6977 let mut found: Vec<Resolved> = Vec::new();
6978 let keep = |resolved: Resolved, found: &mut Vec<Resolved>| {
6979 if !found.iter().any(|held| held.id == resolved.id) {
6980 found.push(resolved);
6981 }
6982 };
6983 loop {
6984 let data = self
6985 .graphql(
6986 graphql::ORIGIN_LOOKUP,
6987 json!({"owner":self.owner,"number":self.project_number,"filter":filter,
6988 "search":search,"type":"ISSUE","originFirst":ORIGIN_PAGE_SIZE,
6989 "itemsAfter":items_after,"searchAfter":search_after,
6990 "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
6991 "duplicates":true}),
6992 )
6993 .await?;
6994 let items = data
6995 .pointer("/originItems/projectV2/items")
6996 .filter(|value| !value.is_null())
6997 .ok_or_else(|| SourceError::Refused {
6998 message: format!(
6999 "GitHub project {}/{} was not found or is not visible to the token",
7000 self.owner, self.project_number
7001 ),
7002 })?;
7003 for item in optional_nodes(Some(items), "project items")?
7004 .into_iter()
7005 .flatten()
7006 {
7007 // The board's own items list its drafts too, and a draft is not an issue: no
7008 // narrowed read answers with one, whatever its origin field holds.
7009 if let Some(resolved) = self.resolve(item)?
7010 && resolved.content_kind == ContentKind::Issue
7011 {
7012 keep(resolved, &mut found);
7013 }
7014 }
7015 let searched = data.get("search").ok_or_else(|| SourceError::Malformed {
7016 message: "GitHub search response has no search connection".into(),
7017 })?;
7018 for node in optional_nodes(Some(searched), "search")?
7019 .into_iter()
7020 .flatten()
7021 {
7022 if let Some(resolved) = self.resolve_issue(node).await? {
7023 keep(resolved, &mut found);
7024 }
7025 }
7026 let items_next = resumed(items, items_after.as_deref())?;
7027 let search_next = resumed(searched, search_after.as_deref())?;
7028 if !items_next.has_more() && !search_next.has_more() {
7029 return Ok(found);
7030 }
7031 items_after = items_next.cursor();
7032 search_after = search_next.cursor();
7033 }
7034 }
7035
7036 /// `found`, with every item this process created or wrote in its place, and every one of
7037 /// them the read did not report added.
7038 ///
7039 /// This process's own record wins over the read's copy of the same item, because a read
7040 /// of an item written moments ago can still be behind what was written onto it — the
7041 /// origin field included, which is the one a narrowed read is confirmed against — and a
7042 /// read that still names an item under a predicate this process's write moved it out of
7043 /// must not return it. The one thing the read knows that the record cannot is when GitHub
7044 /// last saw the item change, which is what a comment-activity read rules a candidate out
7045 /// by, so the read's `updatedAt` is kept when the record has none of its own. See
7046 /// [`Self::created`] and [`Self::updated`](GitHubProjectsSource::updated).
7047 fn with_own_writes(&self, mut found: Vec<Resolved>) -> Result<Vec<Resolved>, SourceError> {
7048 // A board draft is not an issue, so no narrowed read returns one, and this process
7049 // having written one does not make it an answer either.
7050 let own: Vec<Resolved> = self
7051 .created()?
7052 .iter()
7053 .chain(self.updated()?.iter())
7054 .filter(|own| own.content_kind == ContentKind::Issue)
7055 .cloned()
7056 .collect();
7057 for mut own in own {
7058 self.resolved_cache()?.insert(own.id.clone(), own.clone());
7059 match found.iter_mut().find(|read| read.id == own.id) {
7060 Some(read) => {
7061 own.updated_at = own.updated_at.max(read.updated_at);
7062 *read = own;
7063 }
7064 None => found.push(own),
7065 }
7066 }
7067 Ok(found)
7068 }
7069
7070 /// Whether `item` has a comment created or last edited at or after `since` — always, when
7071 /// there is no instant to hold it to.
7072 ///
7073 /// The candidate's own `updatedAt` is read first, because a comment written or edited at
7074 /// or after the instant moved it there: an issue not updated since holds no such comment,
7075 /// and its comments are never asked for — unless this process commented on it in this
7076 /// command, when the `updatedAt` held may predate that comment; see [`Self::commented`]. Otherwise its comments are walked, oldest first,
7077 /// only as far as the first that matches. A board draft is not an issue and has no
7078 /// comments, so it never matches.
7079 async fn commented_since(
7080 &self,
7081 item: &Resolved,
7082 since: Option<DateTime<Utc>>,
7083 ) -> Result<bool, SourceError> {
7084 let Some(since) = since else {
7085 return Ok(true);
7086 };
7087 if item.content_kind == ContentKind::DraftIssue {
7088 return Ok(false);
7089 }
7090 // An `updatedAt` this process's own record or a lagging index holds can predate a
7091 // comment this process wrote since, so only an issue it did not comment on is ruled
7092 // out by one.
7093 if item.updated_at.is_some_and(|updated| updated < since)
7094 && !self.commented()?.contains(&item.id)
7095 {
7096 return Ok(false);
7097 }
7098 let query = TaskQuery {
7099 commented_since: Some(since),
7100 ..TaskQuery::default()
7101 };
7102 let mut after: Option<String> = None;
7103 loop {
7104 let data = self
7105 .graphql(
7106 graphql::ISSUE_COMMENTS,
7107 json!({"id":item.id.0,"first":MAX_PAGE_SIZE,"after":after}),
7108 )
7109 .await?;
7110 let Some(connection) = data
7111 .get("node")
7112 .filter(|value| !value.is_null())
7113 .and_then(|node| node.get("comments"))
7114 .filter(|value| !value.is_null())
7115 else {
7116 // Removed since the search reported it: no longer an issue with comments.
7117 return Ok(false);
7118 };
7119 let comments = optional_nodes(Some(connection), "issue comments")?
7120 .into_iter()
7121 .flatten()
7122 .map(comment_from)
7123 .collect::<Result<Vec<_>, _>>()?;
7124 if query.comments_match(&comments) {
7125 return Ok(true);
7126 }
7127 match next_cursor(connection)? {
7128 Some(next) => {
7129 validate_cursor_progress(after.as_deref(), &next.0)?;
7130 after = Some(next.0);
7131 }
7132 None => return Ok(false),
7133 }
7134 }
7135 }
7136
7137 /// Every item on the board: the union of both enumerations GitHub offers of one.
7138 ///
7139 /// Neither contains the other, so neither is dropped — only `ProjectV2.items` lists a
7140 /// board **draft** and reads the board's own fields beside its items, and only the search
7141 /// reports an item that connection is behind on. The module documentation is where the lag and the
7142 /// measurements behind it are written down.
7143 ///
7144 /// A search result is admitted on the same terms as any other issue this source reaches
7145 /// directly — [`Self::resolve_issue`] keeps it only if that issue's own `projectItems`
7146 /// names *this* board — so an issue the index still believes is here after it was taken
7147 /// off is refused rather than reported.
7148 ///
7149 /// See [`Self::board_cache`]. Both completions happen on every call rather than once,
7150 /// which is what the cache could otherwise have broken.
7151 async fn board(&self) -> Result<Board, SourceError> {
7152 self.board_completed_by(None).await
7153 }
7154
7155 /// [`Self::board`], its search half narrowed by `also` when it is given — which an issue
7156 /// that half could add is then answered only if it holds.
7157 async fn board_completed_by(&self, also: Option<&str>) -> Result<Board, SourceError> {
7158 let cached = self.board_cache()?.clone();
7159 let mut board = match cached {
7160 Some(board) => board,
7161 None => {
7162 let read = self.read_board().await?;
7163 *self.board_cache()? = Some(read.clone());
7164 read
7165 }
7166 };
7167 let searched = match also {
7168 None => self.searched_issues().await?,
7169 Some(also) => self.searched_narrowly(also).await?,
7170 };
7171 for held in searched {
7172 if !board.items.iter().any(|item| item.id == held.id) {
7173 board.items.push(held);
7174 }
7175 }
7176 for own in self.created()?.iter() {
7177 if !board.items.iter().any(|item| item.id == own.id) {
7178 board.items.push(own.clone());
7179 }
7180 }
7181 Ok(board)
7182 }
7183
7184 /// This process's own view of the board, or the refusal a poisoned lock is.
7185 fn board_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<Board>>, SourceError> {
7186 self.board_cache
7187 .lock()
7188 .map_err(|_| SourceError::Unavailable {
7189 message: "this source's view of the board was left inconsistent by an earlier \
7190 failure; next: run the command again"
7191 .into(),
7192 })
7193 }
7194
7195 /// Bring this process's own view of the board up to an item it has just written.
7196 ///
7197 /// A created item goes to `created`, which is what completes a board read GitHub's own
7198 /// eventual consistency has left behind. An item that was already there is replaced
7199 /// where it sits, so a second write of it in the same command reads its real parent
7200 /// rather than the one it had before the first write.
7201 ///
7202 /// "Where it sits" is three places, and missing an earlier one leaves a stale record
7203 /// that wins: an item this same run created is held in `created` and not in the cached
7204 /// board, and `board` completes the cached board *from* `created`, so replacing only
7205 /// the cached copy of such an item replaces nothing and the read still reports the
7206 /// title it was created with. The search is the third, and it is the one an item the
7207 /// board's own projection is behind on sits in *alone* — which is exactly the item this
7208 /// source is least able to re-read, so leaving it out would put the stale title back on
7209 /// the only items the completion in [`Self::board`] exists for.
7210 fn remember_written(&self, item: Resolved, created: bool) -> Result<(), SourceError> {
7211 self.resolved_cache()?.insert(item.id.clone(), item.clone());
7212 if created {
7213 self.created()?.push(item);
7214 return Ok(());
7215 }
7216 {
7217 let mut own = self.created()?;
7218 if let Some(held) = own.iter_mut().find(|held| held.id == item.id) {
7219 *held = item;
7220 return Ok(());
7221 }
7222 }
7223 {
7224 let mut own = self.updated()?;
7225 match own.iter_mut().find(|held| held.id == item.id) {
7226 Some(held) => *held = item.clone(),
7227 None => own.push(item.clone()),
7228 }
7229 }
7230 if let Some(board) = self.board_cache()?.as_mut()
7231 && let Some(held) = board.items.iter_mut().find(|held| held.id == item.id)
7232 {
7233 *held = item.clone();
7234 }
7235 if let Some(found) = self.search_cache()?.as_mut()
7236 && let Some(held) = found.iter_mut().find(|held| held.id == item.id)
7237 {
7238 *held = item.clone();
7239 }
7240 for found in self.narrowed_cache()?.values_mut() {
7241 if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
7242 *held = item.clone();
7243 }
7244 }
7245 for (_, found) in self.children_cache()?.values_mut() {
7246 if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
7247 *held = item.clone();
7248 }
7249 }
7250 Ok(())
7251 }
7252
7253 /// Forget one item this process has just deleted, from every half of its own view.
7254 fn forget(&self, id: &NativeId) -> Result<(), SourceError> {
7255 if let Some(Deferred::Held { created, .. }) = self.deferred()?.as_mut() {
7256 created.retain(|held| held.content != *id);
7257 }
7258 self.resolved_cache()?.remove(id);
7259 self.read_ahead()?.remove(id);
7260 self.carried()?.remove(id);
7261 self.created()?.retain(|own| own.id != *id);
7262 self.updated()?.retain(|own| own.id != *id);
7263 self.commented()?.retain(|own| own != id);
7264 if let Some(board) = self.board_cache()?.as_mut() {
7265 board.items.retain(|item| item.id != *id);
7266 }
7267 if let Some(found) = self.search_cache()?.as_mut() {
7268 found.retain(|item| item.id != *id);
7269 }
7270 for found in self.narrowed_cache()?.values_mut() {
7271 found.retain(|item| item.id != *id);
7272 }
7273 for (_, found) in self.children_cache()?.values_mut() {
7274 found.retain(|item| item.id != *id);
7275 }
7276 Ok(())
7277 }
7278
7279 /// This process's own record of each narrowed answer, or the refusal a poisoned lock is.
7280 fn narrowed_cache(
7281 &self,
7282 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<String, Vec<Resolved>>>, SourceError> {
7283 self.narrowed_cache
7284 .lock()
7285 .map_err(|_| SourceError::Unavailable {
7286 message: "this source's view of a narrowed read was left inconsistent by an \
7287 earlier failure; next: run the command again"
7288 .into(),
7289 })
7290 }
7291
7292 /// This process's own record of each project's sub-issues, or the refusal a poisoned lock
7293 /// is.
7294 /// The tasks `project` holds, as a copy's batched read found them this command, when it did.
7295 fn held_task_children(&self, project: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
7296 Ok(self.task_children()?.get(project).cloned())
7297 }
7298
7299 fn task_children(
7300 &self,
7301 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Vec<Resolved>>>, SourceError> {
7302 self.task_children
7303 .lock()
7304 .map_err(|_| SourceError::Unavailable {
7305 message: "this source's record of a project's tasks was left inconsistent by an \
7306 earlier failure; next: run the command again"
7307 .into(),
7308 })
7309 }
7310
7311 fn children_cache(&self) -> Result<std::sync::MutexGuard<'_, ProjectChildren>, SourceError> {
7312 self.children_cache
7313 .lock()
7314 .map_err(|_| SourceError::Unavailable {
7315 message: "this source's view of a project's tasks was left inconsistent by an \
7316 earlier failure; next: run the command again"
7317 .into(),
7318 })
7319 }
7320
7321 /// Every page of the board, read from GitHub.
7322 async fn read_board(&self) -> Result<Board, SourceError> {
7323 let mut after: Option<String> = None;
7324 let mut items = Vec::new();
7325 let mut board;
7326 loop {
7327 let page = self.board_page(after.as_deref(), MAX_PAGE_SIZE).await?;
7328 for item in page
7329 .pointer("/items/nodes")
7330 .and_then(Value::as_array)
7331 .ok_or_else(|| SourceError::Malformed {
7332 message: "GitHub project items.nodes is not an array".into(),
7333 })?
7334 {
7335 if let Some(resolved) = self.resolve(item)? {
7336 items.push(resolved);
7337 }
7338 }
7339 let info = page
7340 .pointer("/items/pageInfo")
7341 .ok_or_else(|| SourceError::Malformed {
7342 message: "GitHub project items have no pageInfo".into(),
7343 })?;
7344 let has_next = required_bool(info, "hasNextPage")?;
7345 let next = has_next
7346 .then(|| required_str(info, "endCursor"))
7347 .transpose()?;
7348 board = page.clone();
7349 match next {
7350 Some(next) => {
7351 validate_cursor_progress(after.as_deref(), next)?;
7352 after = Some(next.to_owned());
7353 }
7354 None => break,
7355 }
7356 }
7357 Ok(Board {
7358 id: required_str(&board, "id")?.to_owned(),
7359 fields: board.get("fields").cloned().unwrap_or(Value::Null),
7360 items,
7361 })
7362 }
7363
7364 /// The existing items this source has written, for completing a narrowed read that is
7365 /// behind; see [`Self::updated`](GitHubProjectsSource::updated).
7366 fn updated(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
7367 self.updated.lock().map_err(|_| SourceError::Unavailable {
7368 message: "this source's record of what it wrote in this run was left inconsistent \
7369 by an earlier failure; next: run the command again"
7370 .into(),
7371 })
7372 }
7373
7374 /// The issues this source has commented on in this command; see
7375 /// [`Self::commented`](GitHubProjectsSource::commented).
7376 fn commented(&self) -> Result<std::sync::MutexGuard<'_, Vec<NativeId>>, SourceError> {
7377 self.commented.lock().map_err(|_| SourceError::Unavailable {
7378 message: "this source's record of what it commented on in this run was left \
7379 inconsistent by an earlier failure; next: run the command again"
7380 .into(),
7381 })
7382 }
7383
7384 /// Called only once GitHub has answered the comment write, so an issue whose comment
7385 /// failed is never made a candidate a later read would pay a node read for.
7386 fn remember_commented(&self, issue: &NativeId) -> Result<(), SourceError> {
7387 let mut commented = self.commented()?;
7388 if !commented.contains(issue) {
7389 commented.push(issue.clone());
7390 }
7391 Ok(())
7392 }
7393
7394 /// The items this source has created, for completing a board read that is behind.
7395 fn created(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
7396 self.created.lock().map_err(|_| SourceError::Unavailable {
7397 message: "this source's record of what it created in this run was left \
7398 inconsistent by an earlier failure; next: run the command again"
7399 .into(),
7400 })
7401 }
7402
7403 /// One board item as this source reports it, or `None` for content it ignores.
7404 ///
7405 /// A pull request is neither a project nor a task — it is somebody's change, not a
7406 /// unit of plan — and an item whose content the token cannot see has nothing to
7407 /// report at all.
7408 fn resolve(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
7409 let resolved = self.resolve_unremembered(item)?;
7410 if let Some(resolved) = &resolved {
7411 self.resolved_cache()?
7412 .insert(resolved.id.clone(), resolved.clone());
7413 }
7414 Ok(resolved)
7415 }
7416
7417 /// [`Self::resolve`], without remembering the item it resolves: what reading a token off a
7418 /// write's own response needs, which says nothing this process should answer a later read
7419 /// with.
7420 fn resolve_unremembered(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
7421 let content = item.get("content").ok_or_else(|| SourceError::Malformed {
7422 message: "GitHub project item is missing content".into(),
7423 })?;
7424 if content.is_null() {
7425 return Ok(None);
7426 }
7427 let content_kind = match required_str(content, "__typename")? {
7428 "Issue" => ContentKind::Issue,
7429 "DraftIssue" => ContentKind::DraftIssue,
7430 _ => return Ok(None),
7431 };
7432 let field_values = item
7433 .get("fieldValues")
7434 .ok_or_else(|| SourceError::Malformed {
7435 message: "GitHub project item is missing fieldValues".into(),
7436 })?;
7437 complete_connection(field_values, "project item field values", NESTED_PAGE_SIZE)?;
7438 let nodes = field_values
7439 .get("nodes")
7440 .and_then(Value::as_array)
7441 .ok_or_else(|| SourceError::Malformed {
7442 message: "GitHub project item fieldValues.nodes is not an array".into(),
7443 })?;
7444 if let Some(labels) = content.get("labels") {
7445 complete_connection(labels, "content labels", NESTED_PAGE_SIZE)?;
7446 }
7447 let raw_body = optional_str(content, "body")?.map(str::to_owned);
7448 let (body, slot) = metadata_body(raw_body.clone())?;
7449 let parent = optional_str(content.get("parent").unwrap_or(&Value::Null), "id")?
7450 .map(|id| NativeId(id.to_owned()));
7451 // A draft has no sub-issues to summarise, and GitHub's schema gives it no field
7452 // to read one from; it is a task, and never a project.
7453 let sub_issues = match content_kind {
7454 ContentKind::Issue => sub_issue_total(content)?,
7455 ContentKind::DraftIssue => 0,
7456 };
7457 let content_id = required_str(content, "id")?;
7458 let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
7459 message: format!("GitHub issue {content_id}: {message}"),
7460 })?;
7461 let raw_title = required_str(content, "title")?;
7462 let kind = BoardKind::of(raw_title, parent.is_some(), sub_issues, marked);
7463 // The title a person wrote, which for a document is the one without the prefix —
7464 // the same way `content` above is the body without this source's metadata slot.
7465 let title = match kind {
7466 BoardKind::Document => raw_title[DESIGN_TITLE_PREFIX.len()..].to_owned(),
7467 BoardKind::Work(_) => raw_title.to_owned(),
7468 };
7469 let own_repository = content
7470 .pointer("/repository/nameWithOwner")
7471 .and_then(Value::as_str)
7472 .map(|origin| Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST)))
7473 .transpose()
7474 .map_err(|message| SourceError::Malformed { message })?;
7475 let repositories = if slot.contains_key(Repository::METADATA_KEY) {
7476 Repository::from_metadata(&slot)
7477 .map_err(|message| SourceError::Malformed { message })?
7478 } else {
7479 own_repository.clone().into_iter().collect()
7480 };
7481 let classification = Classification::from_metadata(&slot)
7482 .map_err(|message| SourceError::Malformed { message })?;
7483 let id = NativeId(content_id.to_owned());
7484 // Read only for a task, because only a task has either list: a project or a
7485 // document holding one of these keys holds nothing this source reports, and the
7486 // keys are left out of its caller-visible metadata all the same.
7487 let (delivers, delivered_by) = if kind == BoardKind::Work(ItemKind::Task) {
7488 let listed = |key: &str| {
7489 TaskRef::from_value(key, &id, Some(&self.name), slot.get(key))
7490 .map_err(|message| SourceError::Malformed { message })
7491 };
7492 (
7493 listed(TaskRef::DELIVERS_KEY)?,
7494 listed(TaskRef::DELIVERED_BY_KEY)?,
7495 )
7496 } else {
7497 (Vec::new(), Vec::new())
7498 };
7499 let (option, closed, reason) = Self::status_parts(nodes, content)?;
7500 let priority = self.held_priority(nodes)?;
7501 let item_labels = labels(content)?;
7502 // Present when the item was reached through its own issue, whose board entry
7503 // names the board; a read of the board's own items has the board already. An
7504 // empty id names nothing a field write could address, so it is read as absent and
7505 // the write goes back to reading the board.
7506 let board_id = item
7507 .pointer("/project/id")
7508 .and_then(Value::as_str)
7509 .filter(|id| !id.is_empty());
7510 let resolved = Resolved {
7511 item_id: required_str(item, "id")?.to_owned(),
7512 id,
7513 content_kind,
7514 kind,
7515 title,
7516 body: body.filter(|value| !value.is_empty()),
7517 raw_body,
7518 status: self
7519 .statuses
7520 .status(kind.status_kind(), option, closed, reason),
7521 option: option.map(str::to_owned),
7522 priority,
7523 closed,
7524 reason: closed.then(|| reason.map(str::to_owned)).flatten(),
7525 delivers,
7526 delivered_by,
7527 labels: item_labels.clone(),
7528 parent,
7529 origin: text_field(nodes, ORIGIN_FIELD)?.filter(|value| !value.is_empty()),
7530 projected: self.held_projections(nodes)?,
7531 number: match content_kind {
7532 ContentKind::Issue => Some(issue_number(content)?),
7533 // A draft is filed in no repository, so nothing ever numbered it:
7534 // `DraftIssue` declares no `number` at all, exactly as it declares no
7535 // `subIssuesSummary` the branch above reads.
7536 ContentKind::DraftIssue => None,
7537 },
7538 url: optional_str(content, "url")?.map(str::to_owned),
7539 created_at: optional_time(content, "createdAt")?,
7540 updated_at: optional_time(content, "updatedAt")?,
7541 item_updated_at: optional_time(item, "updatedAt")?,
7542 comment_count: content
7543 .pointer("/commentCount/totalCount")
7544 .and_then(Value::as_u64),
7545 relationship_events: content
7546 .pointer("/relationshipEvents/totalCount")
7547 .and_then(Value::as_u64),
7548 // The issue node is stamped wherever it was read; a board item a read of the issue
7549 // put together from its memberships is not.
7550 served_at: match optional_time(content, SERVED_AT)? {
7551 Some(served) => Some(served),
7552 None => optional_time(item, SERVED_AT)?,
7553 },
7554 // Both connections were read to their end above — one reporting a further page is
7555 // refused — so these are every field value and every label the item holds. A
7556 // response that did not select the labels at all says nothing of them, and gives
7557 // no facets.
7558 facets: content.get("labels").is_some().then(|| {
7559 change_token::Facets::new(kind.facet(), facet_fields(nodes), &item_labels).digest()
7560 }),
7561 own_repository,
7562 repositories,
7563 classification,
7564 slot,
7565 board_id: board_id.map(str::to_owned),
7566 fields: field_definitions(nodes),
7567 board_fields: Self::carried_board_fields(content, board_id)?,
7568 blocked_by: carried_blocked_by(content)?,
7569 };
7570 Ok(Some(resolved))
7571 }
7572
7573 /// The field definitions of the board `board_id` names — the project this issue's own
7574 /// board item is on — off the `boards` page a read of an issue by its own id carries, or
7575 /// `None` when the read carried none, carried no entry for that board, or the board item
7576 /// named no board, which a write then answers by reading the board's fields itself.
7577 ///
7578 /// Matched by the board's node id and never by its number alone: a project number is
7579 /// unique only within its owner, so another owner's board numbered alike can sit on the
7580 /// same page, and its field and option ids address nothing on this one.
7581 fn carried_board_fields(
7582 content: &Value,
7583 board_id: Option<&str>,
7584 ) -> Result<Option<Value>, SourceError> {
7585 let (Some(nodes), Some(board_id)) = (
7586 content.pointer("/boards/nodes").and_then(Value::as_array),
7587 board_id,
7588 ) else {
7589 return Ok(None);
7590 };
7591 let Some(board) = nodes.iter().find_map(|node| {
7592 let project = node.get("project")?;
7593 (project.get("id").and_then(Value::as_str) == Some(board_id)).then_some(project)
7594 }) else {
7595 return Ok(None);
7596 };
7597 let Some(fields) = board.get("fields").filter(|fields| !fields.is_null()) else {
7598 return Ok(None);
7599 };
7600 complete_connection(fields, "board fields", NESTED_PAGE_SIZE)?;
7601 Ok(Some(fields.clone()))
7602 }
7603
7604 /// What one board item's `Priority` field says, through this instance's mapping.
7605 ///
7606 /// An instance with no mapping holds no priority, so every item reads as `none` whatever
7607 /// its board holds. With one, no value is `none`, a mapped option is its level, and an
7608 /// option the mapping does not name is kept as itself — never read as a level or as
7609 /// `none` — for a read of the task to report by name.
7610 fn held_priority(&self, field_values: &[Value]) -> Result<HeldPriority, SourceError> {
7611 let Some(mapping) = &self.priorities else {
7612 return Ok(HeldPriority::Read(Priority::None));
7613 };
7614 // A value of the field that names no option — a text field someone called `Priority` —
7615 // is malformed rather than `none`: reading it as no priority would let the next copy
7616 // clear one a person set.
7617 let Some(option) = field_values
7618 .iter()
7619 .find(|value| {
7620 value.pointer("/field/name").and_then(Value::as_str) == Some(PRIORITY_FIELD)
7621 })
7622 .map(|value| required_str(value, "name"))
7623 .transpose()?
7624 else {
7625 return Ok(HeldPriority::Read(Priority::None));
7626 };
7627 Ok(mapping.priority_of(option).map_or_else(
7628 || HeldPriority::Unmapped(option.to_owned()),
7629 HeldPriority::Read,
7630 ))
7631 }
7632
7633 /// What each projected metadata field holds for one board item, by field name — every text
7634 /// value GitHub answers, an empty one included.
7635 fn held_projections(
7636 &self,
7637 field_values: &[Value],
7638 ) -> Result<BTreeMap<String, String>, SourceError> {
7639 let mut held = BTreeMap::new();
7640 for projection in &self.metadata_fields {
7641 if let Some(text) = text_field(field_values, &projection.field)? {
7642 held.insert(projection.field.to_string(), text);
7643 }
7644 }
7645 Ok(held)
7646 }
7647
7648 /// What one board item's status is read from: its `Status` option, whether its issue
7649 /// is closed, and the reason it was closed with. [`BoardStatuses::status`] turns the
7650 /// three into the status it reports.
7651 fn status_parts<'a>(
7652 field_values: &'a [Value],
7653 content: &'a Value,
7654 ) -> Result<(Option<&'a str>, bool, Option<&'a str>), SourceError> {
7655 let option = field_values
7656 .iter()
7657 .find(|value| value.pointer("/field/name").and_then(Value::as_str) == Some("Status"))
7658 .map(|value| required_str(value, "name"))
7659 .transpose()?;
7660 let closed = optional_str(content, "state")? == Some("CLOSED");
7661 Ok((option, closed, optional_str(content, "stateReason")?))
7662 }
7663
7664 /// The board Status option this write selects, or the refusal that says why not.
7665 ///
7666 /// The mapped option is required for both open and terminal targets. A terminal write
7667 /// validates it before changing either representation, so it can never fall back to
7668 /// closing an issue whose board cannot display the matching status.
7669 ///
7670 /// Answers the field's id, the option's id, and the option's name as the board spells
7671 /// it — which is the name a read of the item reports once it sits there.
7672 fn column_for(
7673 &self,
7674 fields: &Value,
7675 kind: ItemKind,
7676 category: StatusCategory,
7677 target: &StatusTarget,
7678 ) -> Result<Option<(String, String, String)>, SourceError> {
7679 let Some(wanted) = target.option() else {
7680 return Ok(None);
7681 };
7682 let missing = |detail: &str| SourceError::Refused {
7683 message: format!(
7684 "{} status {} of source {} needs the board Status option {wanted:?}, and \
7685 {detail}; next: add that option to the board, which `onetaskgraph sources \
7686 fields {} --apply` does, or point status_mapping.{}.{} of this source at one \
7687 it has",
7688 kind.marker(),
7689 category_name(category),
7690 self.name,
7691 self.name,
7692 category_name(category),
7693 kind.marker()
7694 ),
7695 };
7696 let Some(field) = Board::field(fields, "Status")? else {
7697 return Err(missing("this board has no Status field"));
7698 };
7699 if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
7700 return Err(missing(
7701 "this board's Status field is not a single-select field",
7702 ));
7703 }
7704 let option = field
7705 .get("options")
7706 .and_then(Value::as_array)
7707 .and_then(|options| {
7708 options.iter().find(|option| {
7709 option
7710 .get("name")
7711 .and_then(Value::as_str)
7712 .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
7713 })
7714 });
7715 match option {
7716 None => Err(missing("this board does not have it")),
7717 Some(option) => Ok(Some((
7718 required_str(field, "id")?.to_owned(),
7719 required_str(option, "id")?.to_owned(),
7720 required_str(option, "name")?.to_owned(),
7721 ))),
7722 }
7723 }
7724
7725 /// The refusal a status that closes an issue is answered with over a board draft.
7726 fn closes_a_draft(&self, category: StatusCategory) -> SourceError {
7727 SourceError::Refused {
7728 message: format!(
7729 "status {} of source {} closes the item's issue, and GitHub draft items have \
7730 no open or closed state",
7731 category_name(category),
7732 self.name
7733 ),
7734 }
7735 }
7736
7737 /// What a status write to one item needs of the board: the board's id and the
7738 /// definition of its `Status` field, read off the item when the item says both.
7739 ///
7740 /// The same reasoning as [`Self::fields_for`]: a node read of the item names its board,
7741 /// and its `Status` value carries that field's definition, options and all. An item that
7742 /// does not say — no board id, or no `Status` value to read the field off — takes them
7743 /// from [`Self::board_fields`], which reads no item.
7744 async fn status_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
7745 if let Some(board) = item.carried_board() {
7746 return Ok(board);
7747 }
7748 if item.defines("Status")
7749 && let Some(board_id) = item.named_board()
7750 {
7751 return Ok(BoardFields {
7752 id: board_id,
7753 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
7754 });
7755 }
7756 self.board_fields().await
7757 }
7758
7759 /// Set one task's or one project's status and nothing else; see
7760 /// [`TaskSource::set_task_status`] and [`TaskSource::set_project_status`].
7761 async fn set_status(
7762 &self,
7763 id: &NativeId,
7764 kind: ItemKind,
7765 category: StatusCategory,
7766 ) -> Result<Option<Status>, SourceError> {
7767 // Refused before anything is read, in the words a write of the same status is.
7768 let target = self.resolved_target(kind, category)?;
7769 // An item read by issue reference carries only what this write needs, so what it
7770 // becomes is never remembered for a later read to be answered from.
7771 let held = self.take_held(id)?;
7772 let by_reference = held.is_some();
7773 let bound = match held {
7774 Some(item) => Some(item),
7775 None => self.bound_item(id).await?,
7776 };
7777 let Some(mut item) = bound.filter(|item| item.kind == BoardKind::Work(kind)) else {
7778 return Ok(None);
7779 };
7780 let board = self.status_board(&item).await?;
7781 let (field, option, name) = self
7782 .column_for(&board.fields, kind, category, &target)?
7783 .ok_or_else(|| SourceError::Malformed {
7784 message: format!(
7785 "status {} of source {} names no board Status option",
7786 category_name(category),
7787 self.name
7788 ),
7789 })?;
7790 if item.status.category == category && item.option.as_deref() == Some(&name) {
7791 return Ok(Some(item.status));
7792 }
7793 match &target {
7794 StatusTarget::Terminal(_, reason) => {
7795 if item.content_kind == ContentKind::DraftIssue {
7796 return Err(self.closes_a_draft(category));
7797 }
7798 // The option first, so a refused selection leaves an open issue open rather
7799 // than closed behind a card that still reads open.
7800 self.set_item_field(
7801 board.id.as_str(),
7802 &item.item_id,
7803 &field,
7804 json!({"singleSelectOptionId": option}),
7805 )
7806 .await?;
7807 // An issue already closed in a way that reads as this category keeps its state
7808 // and its reason: only its card was out of line, and closing it again would
7809 // overwrite a reason — a duplicate's — that says more than this write does.
7810 let already = item.closed && item.status.category == category;
7811 if !already {
7812 self.update_content(
7813 ContentKind::Issue,
7814 &item.id,
7815 json!({"stateInput": state_input(Some(&target))}),
7816 )
7817 .await?;
7818 }
7819 let reason = if already {
7820 item.reason.clone()
7821 } else {
7822 Some(reason.reason().to_owned())
7823 };
7824 item.closed = true;
7825 item.status = self
7826 .statuses
7827 .status(kind, Some(&name), true, reason.as_deref());
7828 item.option = Some(name);
7829 item.reason = reason;
7830 }
7831 StatusTarget::Column(_) => {
7832 // An option is what an open item's status is, so a closed issue is reopened
7833 // first — sitting closed in the column, it would read back as closed. A draft has
7834 // no state to reopen.
7835 if item.content_kind == ContentKind::Issue && item.closed {
7836 self.update_content(
7837 ContentKind::Issue,
7838 &item.id,
7839 json!({"stateInput": state_input(Some(&target))}),
7840 )
7841 .await?;
7842 item.closed = false;
7843 }
7844 self.set_item_field(
7845 board.id.as_str(),
7846 &item.item_id,
7847 &field,
7848 json!({"singleSelectOptionId": option}),
7849 )
7850 .await?;
7851 item.status = self.statuses.status(kind, Some(&name), false, None);
7852 item.option = Some(name);
7853 }
7854 StatusTarget::Disabled(_) => {
7855 unreachable!("resolved_target refused a disabled status")
7856 }
7857 }
7858 let status = item.status.clone();
7859 if !by_reference {
7860 self.remember_written(item, false)?;
7861 }
7862 Ok(Some(status))
7863 }
7864
7865 /// Replace one task's `delivered_by` and nothing else; see
7866 /// [`TaskSource::set_delivered_by`].
7867 ///
7868 /// One update of the body, which differs from the body GitHub holds only inside the
7869 /// metadata slot — see [`with_slot`]. A body that would not change is not sent at all.
7870 async fn replace_delivered_by(
7871 &self,
7872 id: &NativeId,
7873 delivered_by: &[TaskRef],
7874 ) -> Result<Option<()>, SourceError> {
7875 let entries = TaskRef::listed(
7876 TaskRef::DELIVERED_BY_KEY,
7877 id,
7878 Some(&self.name),
7879 delivered_by.to_vec(),
7880 )
7881 .map_err(|message| SourceError::Refused { message })?;
7882 let Some(mut item) = self
7883 .bound_item(id)
7884 .await?
7885 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
7886 else {
7887 return Ok(None);
7888 };
7889 let mut slot = item.slot.clone();
7890 set_task_list(&mut slot, TaskRef::DELIVERED_BY_KEY, &entries);
7891 self.write_slot(&mut item, &slot).await?;
7892 item.delivered_by = entries;
7893 self.remember_written(item, false)?;
7894 Ok(Some(()))
7895 }
7896
7897 /// Set one caller key of the metadata slot of one issue of `kind`, and nothing else;
7898 /// see [`TaskSource::set_task_metadata`].
7899 ///
7900 /// `None` when this board holds no item by that id, or holds one of another kind. The
7901 /// answer is the item as this source now reads it, so what a caller is told the key
7902 /// holds is what the slot holds.
7903 ///
7904 /// A key already holding the value is answered without a write, compared as JSON rather
7905 /// than as the body's bytes: a slot a person spelled with other whitespace would
7906 /// otherwise be re-encoded, which is a write that changes nothing the caller asked for.
7907 async fn set_slot_key(
7908 &self,
7909 id: &NativeId,
7910 kind: BoardKind,
7911 key: &MetadataKey,
7912 value: &Value,
7913 ) -> Result<Option<Resolved>, SourceError> {
7914 let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
7915 return Ok(None);
7916 };
7917 if item.slot.get(key.as_str()) == Some(value) {
7918 return Ok(Some(item));
7919 }
7920 let mut slot = item.slot.clone();
7921 slot.insert(key.as_str().to_owned(), value.clone());
7922 // A key a board text field projects moves that field first, so the body — the value's
7923 // one authoritative home — is written last, as every write of an existing item is.
7924 if self
7925 .metadata_fields
7926 .iter()
7927 .any(|projection| &*projection.key == key.as_str())
7928 {
7929 let board = self.projection_board(&item).await?;
7930 let (writes, projected) = self.projection_writes(
7931 &board.fields,
7932 Some(&item.projected),
7933 &slot,
7934 Some(&[key.as_str()]),
7935 )?;
7936 let (mut values, mut clears) = (Vec::new(), Vec::new());
7937 for write in &writes {
7938 write.join(&mut values, &mut clears);
7939 }
7940 self.set_item_fields(board.id.as_str(), &item.item_id, &values, &clears)
7941 .await?;
7942 item.projected = projected;
7943 }
7944 self.write_slot(&mut item, &slot).await?;
7945 self.remember_written(item.clone(), false)?;
7946 Ok(Some(item))
7947 }
7948
7949 /// What a projection write to `item` needs of the board, read off the item when it says
7950 /// enough, as [`Self::fields_for`] reads it.
7951 async fn projection_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
7952 if let Some(board) = item.carried_board() {
7953 return Ok(board);
7954 }
7955 if let Some(board_id) = item.named_board()
7956 && self
7957 .metadata_fields
7958 .iter()
7959 .all(|projection| item.defines(&projection.field))
7960 {
7961 return Ok(BoardFields {
7962 id: board_id,
7963 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
7964 });
7965 }
7966 self.board_fields().await
7967 }
7968
7969 /// Put `slot` in one item's metadata slot with a single update of its body, and bring
7970 /// `item` up to what that write left.
7971 ///
7972 /// The body sent differs from the body GitHub holds only inside the slot — see
7973 /// [`with_slot`] — and a body that would not change is not sent at all. It goes through
7974 /// the mutation the item's content takes, so a board draft's body is written with
7975 /// `updateProjectV2DraftIssue` exactly as an issue's is with `updateIssue`.
7976 async fn write_slot(
7977 &self,
7978 item: &mut Resolved,
7979 slot: &BTreeMap<String, Value>,
7980 ) -> Result<(), SourceError> {
7981 let held = item.raw_body.clone().unwrap_or_default();
7982 let body = with_slot(&held, slot)?;
7983 if body != held {
7984 self.update_content(item.content_kind, &item.id, json!({"body": body}))
7985 .await?;
7986 }
7987 let (visible, slot) = metadata_body(Some(body.clone()))?;
7988 item.body = visible.filter(|value| !value.is_empty());
7989 item.raw_body = Some(body);
7990 item.slot = slot;
7991 Ok(())
7992 }
7993
7994 /// This instance's target for a category written to an item of `kind`, refusing one
7995 /// that kind has no option for — before anything is read or written.
7996 ///
7997 /// Nothing here mutates the board's option set to make room for a status. GitHub
7998 /// documents `UpdateProjectV2FieldInput.singleSelectOptions` as *"provided values
7999 /// overwrite existing options"*, so no addition is additive and a mistake destroys the
8000 /// field and every item's status.
8001 fn resolved_target(
8002 &self,
8003 kind: ItemKind,
8004 category: StatusCategory,
8005 ) -> Result<StatusTarget, SourceError> {
8006 let target = self.statuses.target(kind, category).clone();
8007 let StatusTarget::Disabled(why) = target else {
8008 return Ok(target);
8009 };
8010 let refusal = why.refusal(&self.name, category, kind);
8011 // Why there is no shipped default, which is the question a person meeting this
8012 // refusal on a source that never mentioned the category asks.
8013 let shipped_none = match category {
8014 StatusCategory::Draft => Some(
8015 "draft has no shipped default because GitHub draft issues cannot have \
8016 sub-issues, and this source stores a project's tasks as its issue's sub-issues",
8017 ),
8018 StatusCategory::Unknown => Some(
8019 "unknown has no shipped default because this board keeps no open-ended status \
8020 word: every word classified unknown is written to the one board Status option \
8021 status_mapping.unknown names",
8022 ),
8023 _ => None,
8024 };
8025 Err(match (refusal, shipped_none, why) {
8026 (SourceError::Refused { message }, Some(note), UnmappedStatus::Unconfigured) => {
8027 SourceError::Refused {
8028 message: format!("{message}; {note}"),
8029 }
8030 }
8031 (refusal, _, _) => refusal,
8032 })
8033 }
8034
8035 /// What writing `priority` does to one item's `Priority` field on this board, or the
8036 /// refusal naming what the board lacks.
8037 ///
8038 /// `none` is no value, so it clears the field — and asks nothing of an item that holds
8039 /// none already, or of an item not created yet. Every other priority selects the option
8040 /// the mapping names, matched case-insensitively; a board with no `Priority` field, or
8041 /// without that option, is refused rather than given one: reads and writes never create
8042 /// a field or an option.
8043 fn priority_write(
8044 &self,
8045 fields: &Value,
8046 existing: Option<&Resolved>,
8047 priority: Priority,
8048 ) -> Result<Option<PriorityWrite>, SourceError> {
8049 let Some(mapping) = &self.priorities else {
8050 return Err(self.holds_no_priority());
8051 };
8052 let Some(wanted) = mapping.option(priority) else {
8053 if !existing.is_some_and(Resolved::holds_priority) {
8054 return Ok(None);
8055 }
8056 let field =
8057 Board::field(fields, PRIORITY_FIELD)?.ok_or_else(|| SourceError::Malformed {
8058 message: format!(
8059 "an item holding a {PRIORITY_FIELD} value was read without that field"
8060 ),
8061 })?;
8062 return Ok(Some(PriorityWrite::Clear {
8063 field: required_str(field, "id")?.to_owned(),
8064 }));
8065 };
8066 let missing = |detail: &str| SourceError::Refused {
8067 message: format!(
8068 "priority {priority} of source {} needs the board {PRIORITY_FIELD} option \
8069 {wanted:?}, and {detail}; run `onetaskgraph sources fields {} --apply` to add \
8070 it, or point priority_mapping.{priority} of this source at an option the board \
8071 has",
8072 self.name, self.name
8073 ),
8074 };
8075 let Some(field) = Board::field(fields, PRIORITY_FIELD)? else {
8076 return Err(missing(&format!(
8077 "this board has no {PRIORITY_FIELD} field"
8078 )));
8079 };
8080 if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
8081 return Err(missing(&format!(
8082 "this board's {PRIORITY_FIELD} field is not a single-select field"
8083 )));
8084 }
8085 // An options list that is absent or not a list is an answer this source cannot read,
8086 // not a board lacking the option: `sources fields --apply` is no remedy for it.
8087 let option = field
8088 .get("options")
8089 .and_then(Value::as_array)
8090 .ok_or_else(|| SourceError::Malformed {
8091 message: format!("GitHub {PRIORITY_FIELD} field options is not an array"),
8092 })?
8093 .iter()
8094 .find(|option| {
8095 option
8096 .get("name")
8097 .and_then(Value::as_str)
8098 .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
8099 })
8100 .ok_or_else(|| missing("this board does not have it"))?;
8101 Ok(Some(PriorityWrite::Select {
8102 field: required_str(field, "id")?.to_owned(),
8103 option: required_str(option, "id")?.to_owned(),
8104 }))
8105 }
8106
8107 /// What writing `metadata` does to each projected text field of one item, and what those
8108 /// fields hold after it — or the refusal, before anything is sent.
8109 ///
8110 /// `held` is what the item's fields hold now, `None` for an item not created yet. `keys`
8111 /// narrows the work to the entries projecting one of those metadata keys, for a write that
8112 /// touches only them; `None` is every entry. A string the field already holds, and nothing
8113 /// for a field holding nothing, sends nothing. A value no text field can hold is refused,
8114 /// and so is a board without the field, or with a field of that name that is not a text
8115 /// field: reads and writes never create a field.
8116 fn projection_writes(
8117 &self,
8118 fields: &Value,
8119 held: Option<&BTreeMap<String, String>>,
8120 metadata: &BTreeMap<String, Value>,
8121 keys: Option<&[&str]>,
8122 ) -> Result<(Vec<ProjectionWrite>, BTreeMap<String, String>), SourceError> {
8123 let mut projected = held.cloned().unwrap_or_default();
8124 let mut writes = Vec::new();
8125 for projection in self
8126 .metadata_fields
8127 .iter()
8128 .filter(|projection| keys.is_none_or(|keys| keys.contains(&&*projection.key)))
8129 {
8130 let wanted = projection.projected(metadata, &self.name)?;
8131 let field = self.projection_field(fields, projection)?;
8132 let holding = projected.get(&*projection.field);
8133 match wanted {
8134 Projected::Text(text) if holding != Some(&text) => {
8135 writes.push(ProjectionWrite::Set {
8136 field,
8137 text: text.clone(),
8138 });
8139 projected.insert(projection.field.to_string(), text);
8140 }
8141 Projected::Nothing if holding.is_some() => {
8142 writes.push(ProjectionWrite::Clear { field });
8143 projected.remove(&*projection.field);
8144 }
8145 Projected::Text(_) | Projected::Nothing => {}
8146 }
8147 }
8148 Ok((writes, projected))
8149 }
8150
8151 /// The id of the board text field one projection writes, or the refusal naming what the
8152 /// board has instead.
8153 fn projection_field(
8154 &self,
8155 fields: &Value,
8156 projection: &MetadataField,
8157 ) -> Result<String, SourceError> {
8158 let refuse = |detail: String| SourceError::Refused {
8159 message: format!(
8160 "source {} projects metadata key {:?} onto the board text field {:?}, and \
8161 {detail}; next: run `onetaskgraph sources fields {} --apply` to create it as a \
8162 text field, or name another field in metadata_fields",
8163 self.name, &*projection.key, &*projection.field, self.name
8164 ),
8165 };
8166 let Some(field) = Board::field(fields, &projection.field)? else {
8167 return Err(refuse("this board has no field of that name".to_owned()));
8168 };
8169 let typename = optional_str(field, "__typename")?.unwrap_or("field of another type");
8170 let data_type = optional_str(field, "dataType")?;
8171 if typename != "ProjectV2Field" || data_type != Some("TEXT") {
8172 return Err(refuse(format!(
8173 "this board's field of that name is not a text field (it is a {}), so it is \
8174 left as it is; rename or remove that field first",
8175 data_type.unwrap_or(typename)
8176 )));
8177 }
8178 Ok(required_str(field, "id")?.to_owned())
8179 }
8180
8181 /// Apply one priority write to one board item.
8182 async fn write_priority(
8183 &self,
8184 board_id: &str,
8185 item_id: &str,
8186 write: &PriorityWrite,
8187 ) -> Result<(), SourceError> {
8188 match write {
8189 PriorityWrite::Select { field, option } => {
8190 self.set_item_field(
8191 board_id,
8192 item_id,
8193 field,
8194 json!({"singleSelectOptionId": option}),
8195 )
8196 .await
8197 }
8198 PriorityWrite::Clear { field } => {
8199 let data = self
8200 .graphql(
8201 graphql::CLEAR_FIELD,
8202 json!({"input":{"projectId":board_id,"itemId":item_id,"fieldId":field},
8203 "readPriority":false,"priorityName":PRIORITY_FIELD}),
8204 )
8205 .await?;
8206 let returned = data
8207 .pointer("/clearProjectV2ItemFieldValue/projectV2Item")
8208 .ok_or_else(|| SourceError::Malformed {
8209 message: "GitHub field clear returned no project item".into(),
8210 })?;
8211 if required_str(returned, "id")? != item_id {
8212 return Err(SourceError::Malformed {
8213 message: "GitHub field clear returned the wrong project item".into(),
8214 });
8215 }
8216 Ok(())
8217 }
8218 }
8219 }
8220
8221 /// The refusal a priority is answered with by an instance configured with no
8222 /// `priority_mapping`, which holds none.
8223 fn holds_no_priority(&self) -> SourceError {
8224 SourceError::Refused {
8225 message: format!(
8226 "source {} holds no task priority: its configuration sets no priority_mapping; \
8227 next: set priority_mapping on this source, then run `onetaskgraph sources \
8228 fields {} --apply` to set its board up",
8229 self.name, self.name
8230 ),
8231 }
8232 }
8233
8234 /// Set one task's priority and nothing else; see [`TaskSource::set_task_priority`].
8235 ///
8236 /// One field write — a select, or a clear for `none` — and no title, body, label, state
8237 /// or `Status` request. Clearing a priority an item does not hold sends nothing.
8238 async fn set_priority(
8239 &self,
8240 id: &NativeId,
8241 priority: Priority,
8242 ) -> Result<Option<Priority>, SourceError> {
8243 if self.priorities.is_none() {
8244 return Err(self.holds_no_priority());
8245 }
8246 let Some(mut item) = self
8247 .bound_item(id)
8248 .await?
8249 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8250 else {
8251 return Ok(None);
8252 };
8253 if priority == Priority::None && !item.holds_priority() {
8254 return Ok(Some(priority));
8255 }
8256 // The item's own read carries the field's definition whenever it holds a value of
8257 // it, which a clear always does; a select onto an item holding none reads the board.
8258 let board = match (item.carried_board(), item.named_board()) {
8259 (Some(board), _) => board,
8260 (None, Some(id)) if item.defines(PRIORITY_FIELD) => BoardFields {
8261 id,
8262 fields: json!({"nodes": item.fields.clone(), "pageInfo": {"hasNextPage": false}}),
8263 },
8264 _ => self.board_fields().await?,
8265 };
8266 let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? else {
8267 return Ok(Some(priority));
8268 };
8269 let (document, root, input) = match write {
8270 PriorityWrite::Select { field, option } => (
8271 graphql::UPDATE_FIELD,
8272 "updateProjectV2ItemFieldValue",
8273 json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field,"value":{"singleSelectOptionId":option}}),
8274 ),
8275 PriorityWrite::Clear { field } => (
8276 graphql::CLEAR_FIELD,
8277 "clearProjectV2ItemFieldValue",
8278 json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field}),
8279 ),
8280 };
8281 let data = self
8282 .graphql(
8283 document,
8284 json!({"input":input,"readPriority":true,"priorityName":PRIORITY_FIELD}),
8285 )
8286 .await?;
8287 let returned = data
8288 .get(root)
8289 .and_then(|value| value.get("projectV2Item"))
8290 .ok_or_else(|| SourceError::Malformed {
8291 message: "GitHub priority write returned no project item".into(),
8292 })?;
8293 if required_str(returned, "id")? != item.item_id {
8294 return Err(SourceError::Malformed {
8295 message: "GitHub priority write returned the wrong project item".into(),
8296 });
8297 }
8298 let value = returned
8299 .get("fieldValueByName")
8300 .ok_or_else(|| SourceError::Malformed {
8301 message: "GitHub priority write returned no priority read-back".into(),
8302 })?;
8303 if !value.is_null()
8304 && value.pointer("/field/name").and_then(Value::as_str) != Some(PRIORITY_FIELD)
8305 {
8306 return Err(SourceError::Malformed {
8307 message: "GitHub priority read-back is not a Priority field value".into(),
8308 });
8309 }
8310 let values = if value.is_null() {
8311 Vec::new()
8312 } else {
8313 vec![value.clone()]
8314 };
8315 item.priority = self.held_priority(&values)?;
8316 let answer = item.task()?.priority;
8317 self.remember_written(item, false)?;
8318 Ok(Some(answer))
8319 }
8320
8321 /// Replace one task's visible body and nothing else; see
8322 /// [`TaskSource::set_task_content`].
8323 ///
8324 /// One update of the body, which differs from the body GitHub holds only outside the
8325 /// metadata slot — the slot is kept byte for byte, so every caller key and every list
8326 /// this source keeps there reads back as it was. A body that would not change is not
8327 /// sent at all.
8328 async fn replace_content(
8329 &self,
8330 id: &NativeId,
8331 content: &str,
8332 ) -> Result<Option<()>, SourceError> {
8333 let Some(mut item) = self
8334 .bound_item(id)
8335 .await?
8336 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8337 else {
8338 return Ok(None);
8339 };
8340 let held = item.raw_body.clone().unwrap_or_default();
8341 let body = with_content(&held, content)?;
8342 // Checked before anything is sent: content ending in what this source reads as its own
8343 // metadata slot would read back as metadata rather than as the content it was.
8344 let (visible, slot) = metadata_body(Some(body.clone()))?;
8345 if visible.as_deref().unwrap_or_default() != content || slot != item.slot {
8346 return Err(SourceError::Refused {
8347 message: format!(
8348 "this content ends in what source {} reads as its own metadata slot \
8349 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8350 as content; next: remove that trailing block from the content",
8351 self.name
8352 ),
8353 });
8354 }
8355 if body != held {
8356 self.update_content(item.content_kind, &item.id, json!({"body": body}))
8357 .await?;
8358 }
8359 item.body = visible.filter(|value| !value.is_empty());
8360 item.raw_body = Some(body);
8361 item.slot = slot;
8362 self.remember_written(item, false)?;
8363 Ok(Some(()))
8364 }
8365
8366 /// Apply one targeted update to one task; see [`TaskSource::update_task`].
8367 ///
8368 /// One read of the item — which carries the board's field definitions and the issue's
8369 /// `blockedBy`, so neither is read again — and then only what differs from it: the
8370 /// `Status` option and the `Priority` field together in one request, the `blockedBy`
8371 /// additions and removals the named edges differ by, and last one `updateIssue` carrying
8372 /// the title, the body — visible content and metadata slot together — and a state change.
8373 /// So an update naming any of title, body, metadata, status and priority is one read and
8374 /// at most two writes. The body goes last so that a write refused part-way leaves it, and
8375 /// the metadata in it, as it stood. A terminal status selects its option and then closes,
8376 /// as a whole write does; an open one selects its option and then reopens. The origin
8377 /// field is never written: an update is of an item that already exists, whose origin is
8378 /// what it is.
8379 ///
8380 /// The task answered is the item as those writes left it, built from the read and what was
8381 /// sent rather than read again — the same record a later read in this run answers from.
8382 async fn targeted_update(
8383 &self,
8384 id: &NativeId,
8385 update: &TaskUpdate,
8386 ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
8387 // Everything this source can refuse without reading the item is refused first, in the
8388 // words a whole write of the same fields is refused with.
8389 update.consistent()?;
8390 if update
8391 .title
8392 .as_deref()
8393 .is_some_and(|title| title.starts_with(DESIGN_TITLE_PREFIX))
8394 {
8395 return Err(SourceError::Refused {
8396 message: format!(
8397 "the title of this task begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
8398 spells a document, so it would read back as one rather than as a task; \
8399 retitle it",
8400 self.name
8401 ),
8402 });
8403 }
8404 if let Some(delivers) = &update.delivers {
8405 TaskRef::listed(
8406 TaskRef::DELIVERS_KEY,
8407 id,
8408 Some(&self.name),
8409 delivers.clone(),
8410 )
8411 .map_err(|message| SourceError::Refused { message })?;
8412 }
8413 if self.priorities.is_none()
8414 && update
8415 .priority
8416 .is_some_and(|priority| priority != Priority::None)
8417 {
8418 return Err(self.holds_no_priority());
8419 }
8420 let target = update
8421 .status
8422 .as_ref()
8423 .map(|status| self.resolved_target(ItemKind::Task, status.category))
8424 .transpose()?;
8425 let Some(mut item) = self
8426 .bound_item(id)
8427 .await?
8428 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8429 else {
8430 return Ok(None);
8431 };
8432 let before = item.task()?;
8433
8434 let mut status_move = None;
8435 if let (Some(status), Some(target)) = (&update.status, target) {
8436 let board = self.status_board(&item).await?;
8437 let (field, option, name) = self
8438 .column_for(&board.fields, ItemKind::Task, status.category, &target)?
8439 .ok_or_else(|| SourceError::Malformed {
8440 message: format!(
8441 "status {} of source {} names no board Status option",
8442 category_name(status.category),
8443 self.name
8444 ),
8445 })?;
8446 let terminal = matches!(target, StatusTarget::Terminal(_, _));
8447 if terminal && item.content_kind == ContentKind::DraftIssue {
8448 return Err(self.closes_a_draft(status.category));
8449 }
8450 let landed = match &target {
8451 StatusTarget::Terminal(_, reason) => {
8452 self.statuses
8453 .status(ItemKind::Task, Some(&name), true, Some(reason.reason()))
8454 }
8455 _ => self
8456 .statuses
8457 .status(ItemKind::Task, Some(&name), false, None),
8458 };
8459 let option_moves = item
8460 .option
8461 .as_deref()
8462 .is_none_or(|held| !held.eq_ignore_ascii_case(&name));
8463 let state_moves = item.content_kind == ContentKind::Issue
8464 && (item.closed != terminal || (terminal && item.status != landed));
8465 if let Some(moves) = Moves::of(option_moves, state_moves) {
8466 status_move = Some(StatusMove {
8467 board: board.id,
8468 field,
8469 option,
8470 name,
8471 target,
8472 landed,
8473 moves,
8474 });
8475 }
8476 }
8477
8478 let mut priority_move = None;
8479 if let Some(priority) = update.priority
8480 && self.priorities.is_some()
8481 && item.priority != HeldPriority::Read(priority)
8482 {
8483 let board = match (item.carried_board(), item.named_board()) {
8484 (Some(board), _) => board,
8485 (None, Some(board)) if item.defines(PRIORITY_FIELD) => BoardFields {
8486 id: board,
8487 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
8488 },
8489 _ => self.board_fields().await?,
8490 };
8491 if let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? {
8492 priority_move = Some((board.id, write, priority));
8493 }
8494 }
8495
8496 // The metadata keys this update names that a board text field projects, and what
8497 // that does to those fields — refused, as a whole write refuses it, before anything
8498 // is sent.
8499 let touched: Vec<&str> = update
8500 .metadata_set
8501 .keys()
8502 .chain(update.metadata_remove.iter())
8503 .map(MetadataKey::as_str)
8504 .collect();
8505 let mut projection_move = None;
8506 if self
8507 .metadata_fields
8508 .iter()
8509 .any(|projection| touched.contains(&&*projection.key))
8510 {
8511 let mut metadata = item.slot.clone();
8512 for (key, value) in &update.metadata_set {
8513 metadata.insert(key.as_str().to_owned(), value.clone());
8514 }
8515 for key in &update.metadata_remove {
8516 metadata.remove(key.as_str());
8517 }
8518 let board = self.projection_board(&item).await?;
8519 let (writes, projected) = self.projection_writes(
8520 &board.fields,
8521 Some(&item.projected),
8522 &metadata,
8523 Some(&touched),
8524 )?;
8525 projection_move = Some((board.id, writes, projected));
8526 }
8527
8528 // Resolved before the body is composed, because a far end `blockedBy` cannot name is
8529 // recorded in the slot, and the slot travels in the one body update below.
8530 let edges = match &update.depends_on {
8531 Some(edges) => Some(
8532 self.partition_edges(
8533 BoardKind::Work(ItemKind::Task),
8534 item.content_kind,
8535 item.blocked_by.as_deref(),
8536 edges,
8537 )
8538 .await?,
8539 ),
8540 None => None,
8541 };
8542
8543 // A record primed from what a producer wrote holds no body anybody read, so an update
8544 // changing the body reads it first: by the `Status` write's own answer when the update
8545 // moves the option — the mutation answering with the issue whole — and otherwise by
8546 // reading the issue. The body is then composed against what GitHub holds now.
8547 let mut option_selected = false;
8548 // The mark stays until a read lands, so an update that writes no body, or one whose
8549 // read fails, leaves the next update of it to read the body too.
8550 if (update.content.is_some()
8551 || !update.metadata_set.is_empty()
8552 || !update.metadata_remove.is_empty()
8553 || update.delivers.is_some()
8554 || edges.is_some())
8555 && self.is_unread(id)
8556 {
8557 let live = match status_move.as_ref().filter(|moving| moving.moves.option()) {
8558 Some(moving) => {
8559 option_selected = true;
8560 Some(
8561 self.select_reading_body(
8562 moving.board.as_str(),
8563 &item.item_id,
8564 &moving.field,
8565 &moving.option,
8566 )
8567 .await?,
8568 )
8569 }
8570 None => {
8571 self.forget(id)?;
8572 let Some(read) = self.item_by_id(id).await? else {
8573 return Ok(None);
8574 };
8575 read.raw_body
8576 }
8577 };
8578 let (visible, read_slot) = metadata_body(live.clone())?;
8579 self.mark_read(id);
8580 item.raw_body = live;
8581 item.body = visible;
8582 item.slot = read_slot;
8583 }
8584
8585 let mut slot = item.slot.clone();
8586 for (key, value) in &update.metadata_set {
8587 slot.insert(key.as_str().to_owned(), value.clone());
8588 }
8589 for key in &update.metadata_remove {
8590 slot.remove(key.as_str());
8591 }
8592 if let Some(delivers) = &update.delivers {
8593 set_task_list(&mut slot, TaskRef::DELIVERS_KEY, delivers);
8594 }
8595 if let Some((_, recorded)) = &edges {
8596 record_edges(&mut slot, recorded);
8597 }
8598 let held = item.raw_body.clone().unwrap_or_default();
8599 let content = match &update.content {
8600 Some(content) => with_content(&held, content)?,
8601 None => held.clone(),
8602 };
8603 // A slot holding what it held is kept byte for byte, compared as JSON rather than as
8604 // the body's bytes, as a metadata write compares it: a slot a person spelled with
8605 // other whitespace would otherwise be re-encoded, which is a write nobody asked for.
8606 let body = if slot == item.slot {
8607 content
8608 } else {
8609 with_slot(&content, &slot)?
8610 };
8611 // Checked before anything is sent, as a content write checks it: content ending in
8612 // what this source reads as its own slot would read back as metadata.
8613 let (visible, read) = metadata_body(Some(body.clone()))?;
8614 let wanted = update.content.as_deref().or(item.body.as_deref());
8615 if visible.as_deref().unwrap_or_default() != wanted.unwrap_or_default() || read != slot {
8616 return Err(SourceError::Refused {
8617 message: format!(
8618 "this content ends in what source {} reads as its own metadata slot \
8619 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8620 as content; next: remove that trailing block from the content",
8621 self.name
8622 ),
8623 });
8624 }
8625 let recorded_moves =
8626 slot.get(DependencyEdge::RECORDED_KEY) != item.slot.get(DependencyEdge::RECORDED_KEY);
8627
8628 // One `updateIssue` carries all three, because every mutation spends the secondary
8629 // limiter and the title, body and state are one mutation's inputs.
8630 let mut fields = serde_json::Map::new();
8631 if let Some(title) = update.title.as_ref().filter(|title| **title != item.title) {
8632 fields.insert("title".to_owned(), json!(title));
8633 }
8634 if body != held {
8635 fields.insert("body".to_owned(), json!(body));
8636 }
8637 if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.state()) {
8638 fields.insert("stateInput".to_owned(), state_input(Some(&moving.target)));
8639 }
8640 // **The body is written last, and that is the guarantee a refusal part-way keeps.**
8641 // GitHub runs no two requests as one, and runs one document's mutation fields in order
8642 // without undoing an earlier field when a later one fails — so a body written before a
8643 // board field the board then refused would be left changed. Written after every other
8644 // write has landed, a refusal anywhere leaves the item's body, and every metadata key
8645 // it carries, exactly as they stood. So the `Status` option and the `Priority` field go
8646 // first, together in one request — a terminal option selected before the issue
8647 // closes, as a whole write does — then the `blockedBy` difference, then the body.
8648 let mut board_writes: Vec<(&BoardId, (String, Value))> = Vec::new();
8649 let mut clears: Vec<(&BoardId, &str)> = Vec::new();
8650 if let Some(moving) = status_move
8651 .as_ref()
8652 .filter(|moving| moving.moves.option() && !option_selected)
8653 {
8654 board_writes.push((
8655 &moving.board,
8656 (
8657 moving.field.clone(),
8658 json!({"singleSelectOptionId": moving.option}),
8659 ),
8660 ));
8661 }
8662 match &priority_move {
8663 Some((board, PriorityWrite::Select { field, option }, _)) => board_writes.push((
8664 board,
8665 (field.clone(), json!({"singleSelectOptionId": option})),
8666 )),
8667 Some((board, PriorityWrite::Clear { field }, _)) => clears.push((board, field)),
8668 None => {}
8669 }
8670 if let Some((board, writes, _)) = &projection_move {
8671 for write in writes {
8672 let (mut values, mut cleared) = (Vec::new(), Vec::new());
8673 write.join(&mut values, &mut cleared);
8674 board_writes.extend(values.into_iter().map(|value| (board, value)));
8675 clears.extend(cleared.into_iter().map(|field| (board, field)));
8676 }
8677 }
8678 let mut boards: Vec<&BoardId> = board_writes.iter().map(|(board, _)| *board).collect();
8679 boards.extend(clears.iter().map(|(board, _)| *board));
8680 boards.dedup_by(|one, other| one.as_str() == other.as_str());
8681 for board in boards {
8682 let writes = board_writes
8683 .iter()
8684 .filter(|(on, _)| on.as_str() == board.as_str())
8685 .map(|(_, write)| write.clone())
8686 .collect::<Vec<_>>();
8687 let cleared = clears
8688 .iter()
8689 .filter(|(on, _)| on.as_str() == board.as_str())
8690 .map(|(_, field)| *field)
8691 .collect::<Vec<_>>();
8692 self.set_item_fields(board.as_str(), &item.item_id, &writes, &cleared)
8693 .await?;
8694 }
8695 let mut blocked_by_moved = false;
8696 if let Some((native, _)) = &edges
8697 && item.content_kind == ContentKind::Issue
8698 {
8699 blocked_by_moved = self
8700 .reconcile_blocked_by(
8701 &item.id,
8702 native,
8703 Issue::Existing(item.blocked_by.as_deref()),
8704 )
8705 .await?;
8706 }
8707 if !fields.is_empty() {
8708 self.update_content(item.content_kind, &item.id, Value::Object(fields))
8709 .await?;
8710 }
8711
8712 // The `blockedBy` this item was read with is what the write just changed, so it is no
8713 // answer any more: a later write in this command would reconcile against it — re-adding
8714 // what it kept and never removing what it dropped — and a forward-edge read would
8715 // answer with it. Left unknown, each asks GitHub instead.
8716 if blocked_by_moved {
8717 item.blocked_by = None;
8718 }
8719 if let Some(title) = &update.title {
8720 item.title.clone_from(title);
8721 }
8722 item.body = visible.filter(|value| !value.is_empty());
8723 item.raw_body = (!body.is_empty() || item.raw_body.is_some()).then_some(body);
8724 item.slot = slot;
8725 if let Some(delivers) = &update.delivers {
8726 item.delivers.clone_from(delivers);
8727 }
8728 if let Some(moving) = status_move {
8729 item.closed = matches!(moving.target, StatusTarget::Terminal(_, _))
8730 && item.content_kind == ContentKind::Issue;
8731 item.status = moving.landed;
8732 item.option = Some(moving.name);
8733 }
8734 if let Some((_, _, priority)) = priority_move {
8735 item.priority = HeldPriority::Read(priority);
8736 }
8737 if let Some((_, _, projected)) = projection_move {
8738 item.projected = projected;
8739 }
8740 let task = item.task()?;
8741 let mut written = update.changed(&before, &task);
8742 if blocked_by_moved || recorded_moves {
8743 written.insert(UpdatedField::DependsOn);
8744 }
8745 self.remember_written(item, false)?;
8746 Ok(Some(TaskUpdateOutcome {
8747 task,
8748 written,
8749 delivers_before: before.delivers,
8750 }))
8751 }
8752
8753 /// Replace one issue's visible body and its [`MetadataKey::TEMPLATE_KEY`] slot entry
8754 /// together, and nothing else; see [`TaskSource::set_task_rendering`].
8755 ///
8756 /// One update of the body: the content outside the slot, and inside it that one entry,
8757 /// every other entry kept as it was. This source keeps no template answers — an issue has
8758 /// no room beside itself that is not its body, and answers written there would duplicate
8759 /// what the content already says and count against GitHub's body limit — so `answers`
8760 /// reaches nothing here. A body that would not change is not sent at all.
8761 async fn replace_rendering(
8762 &self,
8763 id: &NativeId,
8764 kind: BoardKind,
8765 content: &str,
8766 provenance: &Value,
8767 assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
8768 ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
8769 let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
8770 return Ok(None);
8771 };
8772 let held = item.raw_body.clone().unwrap_or_default();
8773 let mut slot = item.slot.clone();
8774 slot.insert(MetadataKey::TEMPLATE_KEY.to_owned(), provenance.clone());
8775 let rewritten;
8776 let content = if let Some(assets) = assets {
8777 let uploads = self
8778 .upload_assets(item.own_repository.as_ref(), assets)
8779 .await?;
8780 rewritten =
8781 onetaskgraph_plugin_api::serve_asset_references(content, &mut slot, &uploads);
8782 rewritten.as_str()
8783 } else {
8784 content
8785 };
8786 let body = with_slot(&with_content(&held, content)?, &slot)?;
8787 // Checked before anything is sent, as a content write checks it.
8788 let (visible, read) = metadata_body(Some(body.clone()))?;
8789 if visible.as_deref().unwrap_or_default() != content || read != slot {
8790 return Err(SourceError::Refused {
8791 message: format!(
8792 "this content ends in what source {} reads as its own metadata slot \
8793 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8794 as content; next: remove that trailing block from the template",
8795 self.name
8796 ),
8797 });
8798 }
8799 if body != held {
8800 self.update_content(item.content_kind, &item.id, json!({"body": body}))
8801 .await?;
8802 }
8803 item.body = visible.filter(|value| !value.is_empty());
8804 item.raw_body = Some(body);
8805 item.slot = read;
8806 self.remember_written(item, false)?;
8807 Ok(Some(onetaskgraph_plugin_api::AssetsWritten {
8808 id: id.clone(),
8809 content: Some(content.to_owned()),
8810 }))
8811 }
8812
8813 async fn set_item_field(
8814 &self,
8815 board_id: &str,
8816 item_id: &str,
8817 field_id: &str,
8818 value: Value,
8819 ) -> Result<(), SourceError> {
8820 let data = self
8821 .graphql(
8822 graphql::UPDATE_FIELD,
8823 json!({"input":{
8824 "projectId":board_id,"itemId":item_id,"fieldId":field_id,"value":value
8825 },"readPriority":false,"priorityName":PRIORITY_FIELD}),
8826 )
8827 .await?;
8828 field_update_item(&data, item_id).map(|_| ())
8829 }
8830
8831 /// GitHub accepts one value per field mutation; aliases combine those mutations in
8832 /// one request — writes in the order given, then clears. A lone write is
8833 /// [`graphql::UPDATE_FIELD`] and a lone clear [`graphql::CLEAR_FIELD`]; more than one
8834 /// request's slots hold go in further requests of the same document, in order. Every
8835 /// returned item id is checked, including optional aliases.
8836 async fn set_item_fields(
8837 &self,
8838 board: &str,
8839 item: &str,
8840 fields: &[(String, Value)],
8841 clears: &[&str],
8842 ) -> Result<(), SourceError> {
8843 match (fields, clears) {
8844 ([], []) => return Ok(()),
8845 ([(field, value)], []) => {
8846 return self.set_item_field(board, item, field, value.clone()).await;
8847 }
8848 ([], [field]) => {
8849 return self
8850 .write_priority(
8851 board,
8852 item,
8853 &PriorityWrite::Clear {
8854 field: (*field).to_owned(),
8855 },
8856 )
8857 .await;
8858 }
8859 _ => {}
8860 }
8861 // Any field of the request stands in for an input a slot leaves out, which GitHub
8862 // still requires to be well formed although it never runs it.
8863 let filler = fields
8864 .first()
8865 .map_or_else(|| clears[0], |(field, _)| field.as_str());
8866 let mut writes = fields.iter();
8867 let mut cleared = clears.iter();
8868 loop {
8869 let batch: Vec<&(String, Value)> =
8870 writes.by_ref().take(FIELD_WRITE_SLOTS.len()).collect();
8871 let batch_clears: Vec<&&str> = cleared.by_ref().take(FIELD_CLEAR_SLOTS.len()).collect();
8872 if batch.is_empty() && batch_clears.is_empty() {
8873 return Ok(());
8874 }
8875 let mut variables = serde_json::Map::new();
8876 for (index, slot) in FIELD_WRITE_SLOTS.iter().enumerate() {
8877 let input = match batch.get(index) {
8878 Some((field, value)) => {
8879 json!({"projectId":board,"itemId":item,"fieldId":field,"value":value})
8880 }
8881 None => json!({"projectId":board,"itemId":item,"fieldId":filler,
8882 "value":{"text":""}}),
8883 };
8884 variables.insert(slot.variable.to_owned(), input);
8885 variables.insert(slot.include.to_owned(), json!(index < batch.len()));
8886 }
8887 for (index, slot) in FIELD_CLEAR_SLOTS.iter().enumerate() {
8888 let field = batch_clears.get(index).map_or(filler, |field| **field);
8889 variables.insert(
8890 slot.variable.to_owned(),
8891 json!({"projectId":board,"itemId":item,"fieldId":field}),
8892 );
8893 variables.insert(slot.include.to_owned(), json!(index < batch_clears.len()));
8894 }
8895 let data = self
8896 .graphql(graphql::UPDATE_FIELDS, Value::Object(variables))
8897 .await?;
8898 let sent = FIELD_WRITE_SLOTS[..batch.len()]
8899 .iter()
8900 .chain(&FIELD_CLEAR_SLOTS[..batch_clears.len()]);
8901 for slot in sent {
8902 let alias = slot.alias;
8903 let returned = data
8904 .get(alias)
8905 .and_then(|value| value.get("projectV2Item"))
8906 .ok_or_else(|| SourceError::Malformed {
8907 message: format!("GitHub field update {alias} returned no project item"),
8908 })?;
8909 if required_str(returned, "id")? != item {
8910 return Err(SourceError::Malformed {
8911 message: format!(
8912 "GitHub field update {alias} returned the wrong project item"
8913 ),
8914 });
8915 }
8916 }
8917 }
8918 }
8919
8920 async fn native_dependency_ids(&self, id: &NativeId) -> Result<Vec<String>, SourceError> {
8921 let mut after: Option<String> = None;
8922 let mut ids = Vec::new();
8923 loop {
8924 let data = self
8925 .graphql(
8926 graphql::ISSUE_DEPENDENCIES,
8927 json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after}),
8928 )
8929 .await?;
8930 let connection =
8931 data.pointer("/node/blockedBy")
8932 .ok_or_else(|| SourceError::Malformed {
8933 message: "GitHub dependency response has no blockedBy connection".into(),
8934 })?;
8935 ids.extend(
8936 connection
8937 .get("nodes")
8938 .and_then(Value::as_array)
8939 .ok_or_else(|| SourceError::Malformed {
8940 message: "GitHub dependency response nodes is not an array".into(),
8941 })?
8942 .iter()
8943 .map(|value| required_str(value, "id").map(str::to_owned))
8944 .collect::<Result<Vec<_>, _>>()?,
8945 );
8946 let next = next_cursor(connection)?;
8947 if let Some(next) = &next {
8948 validate_cursor_progress(after.as_deref(), &next.0)?;
8949 }
8950 after = next.map(|cursor| cursor.0);
8951 if after.is_none() {
8952 return Ok(ids);
8953 }
8954 }
8955 }
8956
8957 async fn dependencies(
8958 &self,
8959 id: &NativeId,
8960 near_kind: ItemKind,
8961 direction: Direction,
8962 page: &PageRequest,
8963 ) -> Result<Page<DependencyEdge>, SourceError> {
8964 validate_page(page)?;
8965 let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
8966 let cursor = page.cursor.as_ref().map(|c| c.0.as_str());
8967 let recorded = recorded_offset(cursor, direction)?;
8968 // What this issue is blocked by, when a read of it by its own id in this command
8969 // already carried the whole connection — a copy reads the item it writes before it
8970 // reads its edges — and the page asked for is the whole of it, or the recorded tail
8971 // after it. Answered from that read, in the shape the dependency read answers in;
8972 // anything else is asked of GitHub.
8973 let carried = match direction {
8974 Direction::DependsOn => self
8975 .resolved_cache()?
8976 .get(id)
8977 .filter(|item| item.content_kind == ContentKind::Issue)
8978 .and_then(|item| Some((item.blocked_by.clone()?, item.raw_body.clone()))),
8979 Direction::DependedOnBy => None,
8980 }
8981 .filter(|(nodes, _)| recorded.is_some() || (cursor.is_none() && nodes.len() <= limit));
8982 // Asked for even in the recorded phase, whose page reads nothing from the
8983 // connection: `__typename` is what says whether this item has a native
8984 // relationship at all, and that is what decides which far ends the reserved key is
8985 // allowed to hold.
8986 let data = match carried {
8987 Some((nodes, body)) => json!({"node":{"__typename":"Issue","body":body,
8988 "blockedBy":{"nodes":nodes,"pageInfo":{"hasNextPage":false,"endCursor":null}}}}),
8989 None => {
8990 let after = if recorded.is_some() { None } else { cursor };
8991 let first = page.limit.min(MAX_PAGE_SIZE);
8992 match direction {
8993 Direction::DependsOn => {
8994 self.graphql(
8995 graphql::ISSUE_DEPENDS_ON,
8996 json!({"id":id.0,"first":first,"after":after,"owner":self.owner,
8997 "number":self.project_number,
8998 "boardItems":BOARD_ITEMS_PAGE_SIZE}),
8999 )
9000 .await?
9001 }
9002 Direction::DependedOnBy => {
9003 self.graphql(
9004 graphql::ISSUE_DEPENDED_ON_BY,
9005 json!({"id":id.0,"first":first,"after":after}),
9006 )
9007 .await?
9008 }
9009 }
9010 }
9011 };
9012 let draft_slot = draft_slot(&data, id, self.project_number)?;
9013 let node =
9014 data.get("node")
9015 .filter(|v| !v.is_null())
9016 .ok_or_else(|| SourceError::Refused {
9017 message: format!(
9018 "GitHub item {} was not found or does not support dependencies",
9019 id.0
9020 ),
9021 })?;
9022 let connection_name = match direction {
9023 Direction::DependsOn => "blockedBy",
9024 Direction::DependedOnBy => "blocking",
9025 };
9026 // A draft has neither `blockedBy` nor `blocking`, so nothing it depends on can be
9027 // named natively and the reserved key may hold any far end. An issue's connections
9028 // hold issues, and this source reads them at the near item's own level.
9029 let natively_names = (required_str(node, "__typename")? == "Issue").then_some(near_kind);
9030 if let Some(offset) = recorded {
9031 return Ok(recorded_page(
9032 self.recorded_edges(
9033 id,
9034 near_kind,
9035 direction,
9036 natively_names,
9037 node,
9038 draft_slot.as_ref(),
9039 )
9040 .await?,
9041 offset,
9042 limit,
9043 ));
9044 }
9045 if natively_names.is_none() {
9046 return Ok(recorded_page(
9047 self.recorded_edges(
9048 id,
9049 near_kind,
9050 direction,
9051 natively_names,
9052 node,
9053 draft_slot.as_ref(),
9054 )
9055 .await?,
9056 0,
9057 limit,
9058 ));
9059 }
9060 let connection = node
9061 .get(connection_name)
9062 .ok_or_else(|| SourceError::Malformed {
9063 message: "GitHub dependency response is missing its connection".into(),
9064 })?;
9065 let nodes = connection
9066 .get("nodes")
9067 .and_then(Value::as_array)
9068 .ok_or_else(|| SourceError::Malformed {
9069 message: "GitHub dependency response nodes is not an array".into(),
9070 })?;
9071 // `from` depends on `to`, always. GitHub spells the same relationship from either
9072 // end — `blockedBy` lists what this item waits on, `blocking` lists what waits on
9073 // it — so the near item is `from` in one direction and `to` in the other.
9074 let items = nodes
9075 .iter()
9076 .map(|value| {
9077 let related = NativeId(required_str(value, "id")?.into());
9078 let related_kind = related_kind(value)?;
9079 let (from, to) = match direction {
9080 Direction::DependsOn => (
9081 DependencyEndpoint::from_native(id.clone(), near_kind),
9082 DependencyEndpoint::from_native(related, related_kind),
9083 ),
9084 Direction::DependedOnBy => (
9085 DependencyEndpoint::from_native(related, related_kind),
9086 DependencyEndpoint::from_native(id.clone(), near_kind),
9087 ),
9088 };
9089 Ok(DependencyEdge {
9090 from,
9091 to,
9092 kind: DependencyKind::Blocks,
9093 })
9094 })
9095 .collect::<Result<Vec<_>, SourceError>>()?;
9096 let mut next = next_cursor(connection)?;
9097 if let Some(next) = &next {
9098 validate_cursor_progress(cursor, &next.0)?;
9099 }
9100 // The recorded far ends ride on the last page of `blockedBy`, whose answer carried the
9101 // body they are recorded in, as far as the page has room — so they cost no request of
9102 // their own — and only what does not fit is resumed by a cursor.
9103 let mut items = items;
9104 if next.is_none() {
9105 let recorded = self
9106 .recorded_edges(
9107 id,
9108 near_kind,
9109 direction,
9110 natively_names,
9111 node,
9112 draft_slot.as_ref(),
9113 )
9114 .await?;
9115 let room = limit.saturating_sub(items.len());
9116 let total = recorded.len();
9117 items.extend(recorded.into_iter().take(room));
9118 if total > room {
9119 next = Some(Cursor(format!("{RECORDED_CURSOR}{room}")));
9120 }
9121 }
9122 Ok(Page { items, next })
9123 }
9124
9125 /// The edges this item records under [`DependencyEdge::RECORDED_KEY`], which is where
9126 /// a far end in another source has to live: no GitHub issue relationship can name one.
9127 ///
9128 /// Only forwards. The reverse of a recorded edge is derived from the far end, and this
9129 /// source never writes one down.
9130 ///
9131 /// The metadata lives in the item's own body slot, and `node` is the dependency read's
9132 /// own answer, which carries an issue's body — so an issue's recorded edges cost no
9133 /// request beyond the read already made, and reading the board for them would be a
9134 /// walk of every item for one field of one. A draft has no body in that answer, because
9135 /// a draft is not an issue, so a draft's are read off its own read by id — never off a
9136 /// listing of the board, which can be behind on the very item asked about.
9137 async fn recorded_edges(
9138 &self,
9139 id: &NativeId,
9140 near_kind: ItemKind,
9141 direction: Direction,
9142 natively_names: Option<ItemKind>,
9143 node: &Value,
9144 draft_slot: Option<&Option<BTreeMap<String, Value>>>,
9145 ) -> Result<Vec<DependencyEdge>, SourceError> {
9146 if direction != Direction::DependsOn {
9147 return Ok(Vec::new());
9148 }
9149 let slot = match (node.get("body"), draft_slot) {
9150 (Some(body), _) if natively_names.is_some() => {
9151 metadata_body(body.as_str().map(str::to_owned))?.1
9152 }
9153 // A draft the dependency read itself answered: this board's, or none of its.
9154 (_, Some(Some(slot))) => slot.clone(),
9155 (_, Some(None)) => return Ok(Vec::new()),
9156 _ => {
9157 let Some(item) = self.bound_item(id).await? else {
9158 return Ok(Vec::new());
9159 };
9160 item.slot
9161 }
9162 };
9163 DependencyEdge::recorded(&slot, id, near_kind, &self.name, natively_names)
9164 .map_err(|message| SourceError::Malformed { message })
9165 }
9166
9167 /// Why `creation_owners` refuses a create filed in `target`, or `None` when it allows it —
9168 /// every create when the list is empty.
9169 fn owner_refusal(&self, target: &RepositoryTarget) -> Option<String> {
9170 if self.creation_owners.is_empty()
9171 || self
9172 .creation_owners
9173 .iter()
9174 .any(|owner| owner.eq_ignore_ascii_case(&target.owner))
9175 {
9176 return None;
9177 }
9178 Some(format!(
9179 "source {} would create the issue in repository {}, owned by {}, and its \
9180 creation_owners allows only repositories of {}",
9181 self.name,
9182 target.slug(),
9183 target.owner,
9184 self.creation_owners.join(", ")
9185 ))
9186 }
9187
9188 fn configured_repository(&self) -> Result<&RepositoryTarget, SourceError> {
9189 self.repository
9190 .as_ref()
9191 .ok_or_else(|| SourceError::Refused {
9192 message: format!(
9193 "source {} has no repository configured, and a GitHub Projects board has no \
9194 repository of its own to create an issue in; set repository: owner/name on \
9195 this source",
9196 self.name
9197 ),
9198 })
9199 }
9200
9201 /// The repository one new issue is created in, under the rule [`RepositoryTarget`]
9202 /// states.
9203 ///
9204 /// The fallback is demanded first, whichever arm answers: a write without a configured
9205 /// repository is refused naming the field exactly as it was before the rule existed,
9206 /// so a source that could not write before cannot write now, rather than writing for
9207 /// the one item whose own field happens to decide it.
9208 ///
9209 /// Everything this refuses is refused before `createIssue`, so a refusal leaves no
9210 /// issue behind: an entry that is not a repository on [`RepositoryTarget::HOST`], an
9211 /// entry owned by someone other than the owner of the parent issue's repository —
9212 /// GitHub accepts a sub-issue from another repository of the same owner and from no
9213 /// other, so `addSubIssue` would refuse it after the issue existed — a parent the
9214 /// board does not hold, and a parent that is a draft, which GitHub gives no sub-issues,
9215 /// both of which `addSubIssue` would likewise refuse too late. Whether the entry exists
9216 /// and is visible to the token is checked where its node id is resolved, still before
9217 /// `createIssue`. The parent is read by its own id through [`Self::item_by_id`] — never
9218 /// looked up in a listing of the board, which can be minutes behind an issue its own
9219 /// `projectItems` already places on it — and that read answers first from this process's
9220 /// own record, so a project created moments ago in this command answers though GitHub
9221 /// has not caught up.
9222 async fn creation_target(
9223 &self,
9224 incoming: &Incoming<'_>,
9225 ) -> Result<RepositoryTarget, SourceError> {
9226 let what = format!(
9227 "{} {:?}",
9228 incoming.written.kind().describes(),
9229 incoming.title
9230 );
9231 let target = self
9232 .creation_rule(incoming.repositories, incoming.parent, &what)
9233 .await?;
9234 // The engine asks `creation_refusal` before anything of a copy is written; this holds
9235 // every other create to the same restriction, before `createIssue`.
9236 match self.owner_refusal(&target) {
9237 Some(message) => Err(SourceError::Refused { message }),
9238 None => Ok(target),
9239 }
9240 }
9241
9242 /// [`Self::creation_target`]'s rule over what it reads of the item — its `repositories`
9243 /// and the project issue it is filed under — with `what` naming the item in a refusal: the
9244 /// one place the rule is, so the create, [`TaskSource::creation_repository`] and the
9245 /// write-time read of who can read the issue ([`TaskSource::visibility`]) cannot answer
9246 /// differently.
9247 ///
9248 /// On a source declared private, an item's one named repository is read for its
9249 /// visibility first — once per engine hold, see the `visibility` module — and one that
9250 /// does not read private decides nothing: the issue is created in the parent's
9251 /// repository, else the configured one. A read that cannot be answered refuses.
9252 async fn creation_rule(
9253 &self,
9254 repositories: &[Repository],
9255 parent: Option<&NativeId>,
9256 what: &str,
9257 ) -> Result<RepositoryTarget, SourceError> {
9258 let parent = match parent {
9259 Some(parent) => Some(self.bound_item(parent).await?.ok_or_else(|| {
9260 SourceError::Refused {
9261 message: format!(
9262 "GitHub project issue {} was not found on the board of source {}, so {} \
9263 cannot be filed under it",
9264 parent.0, self.name, what
9265 ),
9266 }
9267 })?),
9268 None => None,
9269 };
9270 let parents_repository = parent
9271 .as_ref()
9272 .map(|parent| {
9273 // A draft is on the board and so is found, but it has no repository to
9274 // place a task in and GitHub gives it no sub-issues, so `addSubIssue`
9275 // would refuse the task only once `createIssue` had made it.
9276 if parent.content_kind == ContentKind::DraftIssue {
9277 return Err(SourceError::Refused {
9278 message: format!(
9279 "GitHub project item {} on the board of source {} is a draft, \
9280 which cannot have sub-issues, so {} cannot be filed under it",
9281 parent.id.0, self.name, what
9282 ),
9283 });
9284 }
9285 // An issue's repository is where a sub-issue is placed and whose owner it
9286 // is compared against, so a parent whose repository this source cannot
9287 // spell as `owner/name` — GitHub's login grammar is wider than this
9288 // source's floor — is one nothing can be filed under.
9289 parent
9290 .own_repository
9291 .as_ref()
9292 .and_then(|origin| RepositoryTarget::from_origin(origin).ok())
9293 .ok_or_else(|| SourceError::Malformed {
9294 message: format!(
9295 "GitHub project issue {} on the board of source {} is in {}, which \
9296 is not a {}/owner/name repository this source can place {} in",
9297 parent.id.0,
9298 self.name,
9299 parent
9300 .own_repository
9301 .as_ref()
9302 .map_or("no repository", Repository::as_str),
9303 RepositoryTarget::HOST,
9304 what
9305 ),
9306 })
9307 })
9308 .transpose()?;
9309 self.creation_rule_in_repository(repositories, parents_repository, what)
9310 .await
9311 }
9312
9313 async fn creation_rule_in_repository(
9314 &self,
9315 repositories: &[Repository],
9316 parents_repository: Option<RepositoryTarget>,
9317 what: &str,
9318 ) -> Result<RepositoryTarget, SourceError> {
9319 let fallback = self.configured_repository()?;
9320 match repositories {
9321 [named] => {
9322 let target =
9323 RepositoryTarget::from_origin(named).map_err(|_| SourceError::Refused {
9324 message: format!(
9325 "{} names repository {}, which is not a {}/owner/name repository \
9326 source {} can create an issue in; name one that is, or name none",
9327 what,
9328 named.as_str(),
9329 RepositoryTarget::HOST,
9330 self.name
9331 ),
9332 })?;
9333 // On a source declared private, an issue whose one named repository does not
9334 // read private is created where an item naming none is: the item still names
9335 // that repository in its metadata slot, and its issue stays private.
9336 if self.is_declared_private()
9337 && self.repository_visibility(&target).await?
9338 != onetaskgraph_plugin_api::Visibility::Private
9339 {
9340 return Ok(parents_repository.unwrap_or_else(|| fallback.clone()));
9341 }
9342 if let Some(parents) = &parents_repository
9343 && parents.owner != target.owner
9344 {
9345 return Err(SourceError::Refused {
9346 message: format!(
9347 "{} names repository {}, owned by {}, but its project's issue is in \
9348 {}, owned by {}, and GitHub files a sub-issue only in a repository \
9349 of the same owner as its parent issue; name a repository of {}, or \
9350 name none",
9351 what,
9352 target.slug(),
9353 target.owner,
9354 parents.slug(),
9355 parents.owner,
9356 parents.owner
9357 ),
9358 });
9359 }
9360 Ok(target)
9361 }
9362 _ => Ok(parents_repository.unwrap_or_else(|| fallback.clone())),
9363 }
9364 }
9365
9366 /// The node id of the repository `incoming` is being created in, or the refusal naming
9367 /// the item and the repository the token cannot see.
9368 ///
9369 /// Resolved once per command per repository; see [`Self::repository_cache`].
9370 async fn repository_id(
9371 &self,
9372 repository: &RepositoryTarget,
9373 incoming: &Incoming<'_>,
9374 ) -> Result<String, SourceError> {
9375 if let Some(id) = self.repository_cache()?.get(repository).cloned() {
9376 return Ok(id);
9377 }
9378 let data = self
9379 .graphql(
9380 graphql::REPOSITORY,
9381 json!({"owner":repository.owner,"name":repository.name}),
9382 )
9383 .await?;
9384 self.repository_read(&data, repository, incoming)
9385 }
9386
9387 /// The repository's node id out of an answer carrying the `repository` root, held for
9388 /// the rest of this command, or the refusal naming the item that cannot be created in it.
9389 fn repository_read(
9390 &self,
9391 data: &Value,
9392 repository: &RepositoryTarget,
9393 incoming: &Incoming<'_>,
9394 ) -> Result<String, SourceError> {
9395 let node = data
9396 .get("repository")
9397 .filter(|value| !value.is_null())
9398 .ok_or_else(|| SourceError::Refused {
9399 message: format!(
9400 "GitHub repository {} was not found or is not visible to the token, so {} \
9401 {:?} cannot be created in it",
9402 repository.slug(),
9403 incoming.written.kind().describes(),
9404 incoming.title
9405 ),
9406 })?;
9407 let id = required_str(node, "id")?.to_owned();
9408 self.repository_cache()?
9409 .insert(repository.clone(), id.clone());
9410 Ok(id)
9411 }
9412
9413 fn repository_cache(
9414 &self,
9415 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<RepositoryTarget, String>>, SourceError> {
9416 self.repository_cache
9417 .lock()
9418 .map_err(|_| SourceError::Unavailable {
9419 message: "this source's record of the destination repository was left \
9420 inconsistent by an earlier failure; next: run the command again"
9421 .into(),
9422 })
9423 }
9424
9425 /// Create or update one board item, whichever kind it is.
9426 async fn write_item(
9427 &self,
9428 incoming: &Incoming<'_>,
9429 target: Option<&NativeId>,
9430 depends_on: &[DependencyEdge],
9431 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
9432 let prepared = self.prepare_write(incoming, target, depends_on).await?;
9433 let landed = match &prepared.destination {
9434 // Its content is written last, by `complete_write`, once everything else has landed.
9435 WriteDestination::Existing(item) => Landed {
9436 content_id: item.id.clone(),
9437 item_id: Some(item.item_id.clone()),
9438 url: item.url.clone(),
9439 number: item.number,
9440 },
9441 WriteDestination::New(target) => {
9442 if self.deferring()? {
9443 self.create_filed_issue(
9444 prepared.board.id.as_str(),
9445 target,
9446 incoming,
9447 &prepared.body,
9448 )
9449 .await?
9450 } else {
9451 self.create_and_file_issue(
9452 prepared.board.id.as_str(),
9453 target,
9454 incoming,
9455 &prepared.body,
9456 )
9457 .await?
9458 }
9459 }
9460 };
9461 self.complete_write(incoming, prepared, landed).await
9462 }
9463
9464 /// Everything one write of `incoming` reads and decides before it sends anything: what is
9465 /// there now, where a new item is created, the board's fields, and every value refused while
9466 /// nothing has been written — the item's body included.
9467 async fn prepare_write(
9468 &self,
9469 incoming: &Incoming<'_>,
9470 target: Option<&NativeId>,
9471 depends_on: &[DependencyEdge],
9472 ) -> Result<Prepared, SourceError> {
9473 // Refused before anything is read or written: a task or a project titled the way
9474 // this board spells a document would land as an issue this same source reads back
9475 // as a document, so the field this destination cannot carry is named rather than
9476 // written and silently reclassified.
9477 if let Written::Work(kind, _) = incoming.written
9478 && incoming.title.starts_with(DESIGN_TITLE_PREFIX)
9479 {
9480 return Err(SourceError::Refused {
9481 message: format!(
9482 "the title of this {} begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
9483 spells a document, so it would read back as one rather than as a {}; \
9484 retitle it, or copy it as a document",
9485 kind.marker(),
9486 self.name,
9487 kind.marker()
9488 ),
9489 });
9490 }
9491 // The destination is read by its own id, and whether this board holds it is decided
9492 // by that read — its own `projectItems` — rather than by whether a listing of the
9493 // board happens to include it yet. See the module documentation.
9494 // An existing issue is never moved; a new one is created where the rule says — and
9495 // knowing where is what lets the board's fields and that repository's id be read
9496 // together, before anything below needs either.
9497 let destination = match target {
9498 Some(target) => {
9499 WriteDestination::Existing(Box::new(self.bound_item(target).await?.ok_or_else(
9500 || SourceError::Refused {
9501 message: format!("GitHub destination item {} was not found", target.0),
9502 },
9503 )?))
9504 }
9505 None => {
9506 let target = self.creation_target(incoming).await?;
9507 self.creation_context(&target, incoming).await?;
9508 WriteDestination::New(target)
9509 }
9510 };
9511 let existing = destination.existing();
9512 let board = self
9513 .fields_for(
9514 existing,
9515 incoming.metadata.contains_key(ORIGIN_KEY),
9516 incoming.written.status().is_some(),
9517 incoming
9518 .priority
9519 .is_some_and(|priority| priority != Priority::None),
9520 )
9521 .await?;
9522 let status_target = incoming
9523 .written
9524 .work_status()
9525 .map(|(kind, status)| self.resolved_target(kind, status.category))
9526 .transpose()?;
9527 let column = match (incoming.written.work_status(), status_target.as_ref()) {
9528 (Some((kind, status)), Some(target)) => {
9529 self.column_for(&board.fields, kind, status.category, target)?
9530 }
9531 _ => None,
9532 };
9533 // Resolved before anything is created, for the reason the column above is: a
9534 // priority this board has no option for is refused while nothing has been written.
9535 let priority_write = match incoming.priority {
9536 Some(priority) => self.priority_write(&board.fields, existing, priority)?,
9537 None => None,
9538 };
9539 // And for the reason the priority is: a value no projected field can hold, or a board
9540 // that cannot hold the field, is refused while nothing has been written.
9541 let (projections, projected) = self.projection_writes(
9542 &board.fields,
9543 existing.map(|item| &item.projected),
9544 incoming.metadata,
9545 None,
9546 )?;
9547 let content_kind = existing.map_or(ContentKind::Issue, |item| item.content_kind);
9548 if content_kind == ContentKind::DraftIssue {
9549 if let (Some(StatusTarget::Terminal(_, _)), Some(status)) =
9550 (status_target.as_ref(), incoming.written.status())
9551 {
9552 return Err(self.closes_a_draft(status.category));
9553 }
9554 if incoming.parent.is_some() {
9555 return Err(SourceError::Refused {
9556 message: "GitHub draft items cannot be a project's sub-issue".into(),
9557 });
9558 }
9559 }
9560 match existing {
9561 Some(item) if content_kind == ContentKind::Issue => {
9562 if item.labels != incoming.labels {
9563 return Err(SourceError::Refused {
9564 message: "GitHub issue labels differ from the labels being written".into(),
9565 });
9566 }
9567 }
9568 _ => {
9569 if !incoming.labels.is_empty() {
9570 return Err(SourceError::Refused {
9571 message: "GitHub items created by this destination carry no labels".into(),
9572 });
9573 }
9574 }
9575 }
9576
9577 // The repository the issue really lives in is what the slot below is written against,
9578 // so a single entry that is where the issue is created travels as no key at all, and
9579 // the read side derives it back from the issue.
9580 let own_repository = match &destination {
9581 WriteDestination::Existing(item) => item.own_repository.clone(),
9582 WriteDestination::New(target) => Some(
9583 Repository::try_from(target.origin())
9584 .map_err(|message| SourceError::Config { message })?,
9585 ),
9586 };
9587 let (native, fallback) = self
9588 .partition_edges(
9589 incoming.written.kind(),
9590 content_kind,
9591 existing.and_then(|item| item.blocked_by.as_deref()),
9592 depends_on,
9593 )
9594 .await?;
9595 let mut slot = slot_metadata(incoming, own_repository.as_ref(), &fallback);
9596 let content = match incoming.assets {
9597 Some(assets) => {
9598 let uploads = self.upload_assets(own_repository.as_ref(), assets).await?;
9599 let rewritten = onetaskgraph_plugin_api::serve_asset_references(
9600 incoming.content.unwrap_or_default(),
9601 &mut slot,
9602 &uploads,
9603 );
9604 incoming.content.map(|_| rewritten)
9605 }
9606 None => incoming.content.map(str::to_owned),
9607 };
9608 let body = compose_body(content.as_deref(), &slot)?;
9609 // Read before anything is created, for the reason the field below is: a value
9610 // this destination cannot store has to refuse, and refusing after `createIssue`
9611 // would leave an issue behind that nothing asked for. The engine writes a
9612 // qualified id here; a caller handing this key anything else is told so rather
9613 // than having it silently stored as no origin at all.
9614 // 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.
9615 let origin = match incoming.metadata.get(ORIGIN_KEY) {
9616 None => "",
9617 Some(Value::String(origin)) => origin.as_str(),
9618 Some(other) => {
9619 return Err(SourceError::Refused {
9620 message: format!(
9621 "{ORIGIN_KEY} holds a qualified id spelled as a string, and this item's \
9622 is {other}"
9623 ),
9624 });
9625 }
9626 };
9627 // Resolved before anything is created: a board that cannot carry the copy origin
9628 // has to refuse the write, and refusing it after `createIssue` would leave an
9629 // issue behind that nothing asked for.
9630 let origin_field = match Board::field(&board.fields, ORIGIN_FIELD)? {
9631 Some(field) => {
9632 if required_str(field, "__typename")? != "ProjectV2Field" {
9633 return Err(SourceError::Refused {
9634 message: format!(
9635 "GitHub board source-owned {ORIGIN_FIELD} field is not a text field"
9636 ),
9637 });
9638 }
9639 Some(required_str(field, "id")?.to_owned())
9640 }
9641 None if incoming.metadata.contains_key(ORIGIN_KEY) => {
9642 return Err(SourceError::Refused {
9643 message: format!(
9644 "GitHub board has no source-owned {ORIGIN_FIELD} text field, and the \
9645 item carries {ORIGIN_KEY}; add a text field named {ORIGIN_FIELD} to \
9646 the board"
9647 ),
9648 });
9649 }
9650 None => None,
9651 };
9652
9653 Ok(Prepared {
9654 origin: origin.to_owned(),
9655 destination,
9656 board,
9657 status_target,
9658 column,
9659 priority_write,
9660 projections,
9661 projected,
9662 content_kind,
9663 own_repository,
9664 native,
9665 slot,
9666 content,
9667 body,
9668 origin_field,
9669 })
9670 }
9671
9672 /// Everything one write does once the item it writes exists: its board fields, its
9673 /// relationships and — for an item that was already there — its content last, then what this
9674 /// process remembers of it.
9675 ///
9676 /// While this source is holding writes back — see [`TaskSource::begin_deferred_writes`] — an
9677 /// item this command created has its fields, its closing and its edges held for
9678 /// [`TaskSource::finish_writes`] rather than sent here.
9679 async fn complete_write(
9680 &self,
9681 incoming: &Incoming<'_>,
9682 prepared: Prepared,
9683 landed: Landed,
9684 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
9685 let Prepared {
9686 destination,
9687 board,
9688 status_target,
9689 column,
9690 priority_write,
9691 projections,
9692 projected,
9693 content_kind,
9694 own_repository,
9695 native,
9696 slot,
9697 content,
9698 body,
9699 origin,
9700 origin_field,
9701 } = prepared;
9702 let existing = destination.existing();
9703 let origin = origin.as_str();
9704 let Landed {
9705 content_id,
9706 item_id,
9707 url,
9708 number,
9709 } = landed;
9710
9711 // Only what differs from the item as this write read it is sent: a priority the item
9712 // already holds is not, so an unchanged re-copy writes no field and a person's edit of
9713 // it since that read is left alone.
9714 let priority_write = priority_write.filter(|_| {
9715 !existing.is_some_and(|item| {
9716 incoming.priority.map(HeldPriority::Read) == Some(item.priority.clone())
9717 })
9718 });
9719 let written_option = column.as_ref().map(|(_, _, name)| name.clone());
9720 let column = column
9721 .filter(|(_, _, name)| existing.is_none_or(|item| item.option.as_ref() != Some(name)))
9722 .map(|(field, option, _)| (field, option));
9723 // Creating an item here is several calls — `createIssue`, which files it on the
9724 // board, then its board fields, the parent and the dependencies — and GitHub can fail
9725 // at any of them. Everything this source can refuse *before* the first of those is
9726 // already checked above, so what is left is GitHub itself failing part way. When it
9727 // does over an item this call created, the issue is taken back: a write that
9728 // refused must not leave an item behind that nobody asked for, and one that does
9729 // makes the retry create a second.
9730 // Whether the board-field write carrying a moved origin was answered as landing whole.
9731 // When it was refused, GitHub does not say which of its fields ran before the one that
9732 // failed, so the origin may or may not have moved.
9733 let mut origin_landed = false;
9734 // An item this command created while writes are held back has every part that can land
9735 // later held for `finish_writes` — its own create filed it on the board and under its
9736 // project already — and so does a later write of it in the same command.
9737 let holding = (existing.is_none() && self.deferring()?) || self.holds_back(&content_id)?;
9738 let landed = if holding {
9739 self.hold_back(
9740 &board.id,
9741 &content_id,
9742 existing,
9743 origin_field.as_deref(),
9744 origin,
9745 column,
9746 status_target.as_ref(),
9747 priority_write.as_ref(),
9748 &projections,
9749 &native,
9750 )
9751 } else if let Some(item_id) = item_id.as_deref() {
9752 self.finish_write(
9753 board.id.as_str(),
9754 incoming,
9755 &content_id,
9756 item_id,
9757 content_kind,
9758 existing,
9759 origin_field.as_deref(),
9760 origin,
9761 column,
9762 status_target.as_ref(),
9763 priority_write.as_ref(),
9764 &projections,
9765 &native,
9766 &mut origin_landed,
9767 )
9768 .await
9769 } else {
9770 // An issue a create filed on the board itself has no board item this process
9771 // knows until `finish_writes` reads it, so nothing but a held-back write may land
9772 // on it before then.
9773 Err(SourceError::Malformed {
9774 message: format!(
9775 "issue {} was written before the board item GitHub filed it as was read",
9776 content_id.0
9777 ),
9778 })
9779 };
9780 // An existing item's title, body and state go last, in one `updateIssue`, once its board
9781 // fields and its relationships have landed: a refusal of any of those then leaves its
9782 // body — and the metadata slot inside it — exactly as it stood.
9783 let landed = match (landed, existing) {
9784 (Ok(()), Some(item)) => {
9785 self.update_existing(item, incoming, &body, status_target.as_ref())
9786 .await
9787 }
9788 (landed, _) => landed,
9789 };
9790 if let Err(error) = landed {
9791 match existing {
9792 // Best effort, and the write's own failure is what the caller is told: a
9793 // refusal naming the tidy-up would hide why the write failed at all.
9794 None => {
9795 let _ = self.delete_issue(&content_id).await;
9796 }
9797 // The origin field is the one piece of an existing item's metadata written
9798 // before its body, so a write refused after it puts it back as it was. When
9799 // that is refused too, the write's own failure is still what the caller is
9800 // told — with what it left behind added, because the item's metadata is then
9801 // not as it stood and a caller retrying has to know which key moved.
9802 Some(item) => {
9803 let before = item.origin.as_deref().unwrap_or("");
9804 if let Some(field) = origin_field.as_deref()
9805 && before != origin
9806 && let Err(restore) = self
9807 .set_item_field(
9808 board.id.as_str(),
9809 &item.item_id,
9810 field,
9811 json!({"text": before}),
9812 )
9813 .await
9814 {
9815 let left = if origin_landed {
9816 format!(
9817 "its {ORIGIN_KEY} was moved to {origin:?} before that and could \
9818 not be put back to {before:?} ({restore}), so item {} still \
9819 holds {origin:?} there",
9820 item.id.0
9821 )
9822 } else {
9823 format!(
9824 "the refused write carried its {ORIGIN_KEY} from {before:?} to \
9825 {origin:?}, GitHub does not say whether that part of it ran, \
9826 and putting it back to {before:?} was refused ({restore}), so \
9827 item {} holds {origin:?} or {before:?} there",
9828 item.id.0
9829 )
9830 };
9831 return Err(noting(
9832 error,
9833 &format!(
9834 "; {left}; next: set {ORIGIN_KEY} on it back to {before:?}, or \
9835 run the write again"
9836 ),
9837 ));
9838 }
9839 }
9840 }
9841 return Err(error);
9842 }
9843
9844 let written_status = match (incoming.written.work_status(), status_target.as_ref()) {
9845 (Some((kind, _)), Some(StatusTarget::Terminal(_, reason))) => {
9846 self.statuses
9847 .status(kind, written_option.as_deref(), true, Some(reason.reason()))
9848 }
9849 (Some((kind, _)), Some(StatusTarget::Column(_))) => {
9850 self.statuses
9851 .status(kind, written_option.as_deref(), false, None)
9852 }
9853 (Some((_, status)), _) => status.clone(),
9854 (None, _) => Status {
9855 category: StatusCategory::Unknown,
9856 name: "Open".to_owned(),
9857 },
9858 };
9859
9860 // So the rest of this command reads what it just did rather than what the board
9861 // said before it. See `remember_written` for which half takes it.
9862 let remembered = Resolved {
9863 // Empty for an issue whose board item is not read yet: `finish_writes` reads it
9864 // and `learn_item` puts it here.
9865 // 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.
9866 item_id: item_id.unwrap_or_default(),
9867 id: content_id.clone(),
9868 content_kind,
9869 kind: incoming.written.kind(),
9870 title: incoming.title.to_owned(),
9871 // The visible half of the body this write composed, split back off it the
9872 // way a read splits it — so what this record reports is what a read of the
9873 // same issue reports, rather than the person's text with the metadata slot
9874 // still on the end of it.
9875 body: metadata_body(body.clone())?.0,
9876 raw_body: body.clone(),
9877 // A document has no status of its own; what it reads back as is whatever
9878 // the issue's own state says, which is what a re-read reports.
9879 status: written_status,
9880 option: written_option.or_else(|| existing.and_then(|item| item.option.clone())),
9881 priority: match incoming.priority {
9882 Some(priority) => HeldPriority::Read(priority),
9883 None => existing.map_or(HeldPriority::Read(Priority::None), |item| {
9884 item.priority.clone()
9885 }),
9886 },
9887 // What `state_input` asked for: closed for a terminal target, open for any other
9888 // status, and the issue's own state left as it was by a document write.
9889 closed: content_kind == ContentKind::Issue
9890 && match status_target.as_ref() {
9891 Some(StatusTarget::Terminal(_, _)) => true,
9892 Some(_) => false,
9893 None => existing.is_some_and(|item| item.closed),
9894 },
9895 reason: match status_target.as_ref() {
9896 Some(StatusTarget::Terminal(_, reason)) if content_kind == ContentKind::Issue => {
9897 Some(reason.reason().to_owned())
9898 }
9899 Some(_) => None,
9900 None => existing.and_then(|item| item.reason.clone()),
9901 },
9902 delivers: incoming.delivers.to_vec(),
9903 delivered_by: incoming.delivered_by.to_vec(),
9904 labels: incoming.labels.to_vec(),
9905 parent: incoming.parent.cloned(),
9906 origin: (!origin.is_empty()).then(|| origin.to_owned()),
9907 projected,
9908 number,
9909 // In the update path this is the item's own url, read off `existing` where the
9910 // record above was bound, so one expression serves both halves.
9911 url,
9912 created_at: existing.and_then(|item| item.created_at),
9913 updated_at: existing.and_then(|item| item.updated_at),
9914 // What this write moved is GitHub's to say, so a record of it gives no token until
9915 // it is read again.
9916 item_updated_at: None,
9917 comment_count: None,
9918 relationship_events: None,
9919 served_at: None,
9920 facets: None,
9921 own_repository,
9922 repositories: incoming.repositories.to_vec(),
9923 classification: incoming.classification,
9924 slot,
9925 board_id: Some(board.id.as_str().to_owned()),
9926 fields: board
9927 .fields
9928 .get("nodes")
9929 .and_then(Value::as_array)
9930 .cloned()
9931 .unwrap_or_default(),
9932 board_fields: Some(board.fields.clone()),
9933 // What this write left the relationship holding is known by id alone, and a
9934 // later read of its edges needs each far end's kind, so it reads them again.
9935 blocked_by: None,
9936 };
9937 self.remember_written(remembered, existing.is_none())?;
9938 Ok(onetaskgraph_plugin_api::AssetsWritten {
9939 id: content_id,
9940 content,
9941 })
9942 }
9943
9944 /// Everything a write does after the item exists: its board fields, its parent, and
9945 /// its dependencies.
9946 ///
9947 /// Split out of `write_item` so there is one place a failure past the point of no
9948 /// return is caught, rather than a tidy-up repeated at each `?` above.
9949 // llmlint: ignore[suppressions_justified] This is the tail of `write_item` lifted out
9950 // so there is one place a failure past the point of no return is caught, and its
9951 // arguments are exactly the values that tail already had in scope. Bundling them into a
9952 // struct would describe no concept — it would be "the arguments of this function" — and
9953 // would put the whole of `write_item`'s locals behind one more indirection.
9954 #[allow(clippy::too_many_arguments)]
9955 async fn finish_write(
9956 &self,
9957 board_id: &str,
9958 incoming: &Incoming<'_>,
9959 content_id: &NativeId,
9960 item_id: &str,
9961 content_kind: ContentKind,
9962 existing: Option<&Resolved>,
9963 origin_field: Option<&str>,
9964 origin: &str,
9965 column: Option<(String, String)>,
9966 status_target: Option<&StatusTarget>,
9967 priority: Option<&PriorityWrite>,
9968 projections: &[ProjectionWrite],
9969 native: &[String],
9970 origin_landed: &mut bool,
9971 ) -> Result<(), SourceError> {
9972 let (fields, clears) = field_writes(
9973 existing,
9974 origin_field,
9975 origin,
9976 column,
9977 priority,
9978 projections,
9979 );
9980 self.set_item_fields(board_id, item_id, &fields, &clears)
9981 .await?;
9982 *origin_landed = true;
9983
9984 // An existing issue closes in the `updateIssue` its write ends with; one created just
9985 // now closes here, once its option is selected.
9986 if existing.is_none()
9987 && content_kind == ContentKind::Issue
9988 && matches!(status_target, Some(StatusTarget::Terminal(_, _)))
9989 {
9990 self.update_content(
9991 ContentKind::Issue,
9992 content_id,
9993 json!({"stateInput":state_input(status_target)}),
9994 )
9995 .await?;
9996 }
9997
9998 if content_kind == ContentKind::Issue {
9999 self.reparent(
10000 existing.and_then(|item| item.parent.clone()),
10001 content_id,
10002 incoming.parent,
10003 )
10004 .await?;
10005 // A document takes part in no dependency graph, so writing one neither reads
10006 // nor changes the issue's own `blockedBy` relationships. Reconciling them
10007 // against the empty list a document write carries would *delete* whatever
10008 // relationships a person had made on that issue, which is a write nobody
10009 // asked for.
10010 if incoming.written.kind() != BoardKind::Document {
10011 let issue = match existing {
10012 Some(item) => Issue::Existing(item.blocked_by.as_deref()),
10013 None => Issue::Created,
10014 };
10015 self.reconcile_blocked_by(content_id, native, issue).await?;
10016 }
10017 }
10018 Ok(())
10019 }
10020
10021 /// Whether this source is holding writes back — see [`TaskSource::begin_deferred_writes`].
10022 fn deferring(&self) -> Result<bool, SourceError> {
10023 Ok(self.deferred()?.is_some())
10024 }
10025
10026 /// Whether `content` is an item this command created whose parts are held back.
10027 fn holds_back(&self, content: &NativeId) -> Result<bool, SourceError> {
10028 Ok(self
10029 .deferred()?
10030 .as_ref()
10031 .is_some_and(|deferred| deferred.holds(content)))
10032 }
10033
10034 fn deferred(&self) -> Result<std::sync::MutexGuard<'_, Option<Deferred>>, SourceError> {
10035 self.deferred.lock().map_err(|_| SourceError::Unavailable {
10036 message: "this source's record of the writes it is holding back was left \
10037 inconsistent by an earlier failure; next: run the command again"
10038 .into(),
10039 })
10040 }
10041
10042 /// Creates one issue filed on the board and under its project by the `createIssue` itself —
10043 /// `projectV2Ids` naming the board and `parentIssueId` the project — which is what a copy
10044 /// creates with: one request where adding it to the board and filing it under its project
10045 /// were two more. GitHub adds the board item after answering, so its id is not known here;
10046 /// [`TaskSource::finish_writes`] reads it.
10047 async fn create_filed_issue(
10048 &self,
10049 board_id: &str,
10050 repository: &RepositoryTarget,
10051 incoming: &Incoming<'_>,
10052 body: &Option<String>,
10053 ) -> Result<Landed, SourceError> {
10054 let repository_id = self.repository_id(repository, incoming).await?;
10055 let data = self
10056 .graphql(
10057 graphql::CREATE_ISSUE,
10058 json!({"input": filed_input(&repository_id, board_id, incoming, body)}),
10059 )
10060 .await?;
10061 let created = data
10062 .pointer("/createIssue/issue")
10063 .filter(|value| !value.is_null())
10064 .ok_or_else(|| SourceError::Malformed {
10065 message: "GitHub issue creation returned no issue".into(),
10066 })?;
10067 self.created_landed(created).await
10068 }
10069
10070 /// Keep the address `createIssue` answered for the issue `content`, for
10071 /// [`TaskSource::written_location`].
10072 fn note_written(&self, content: &NativeId, url: Option<&str>) {
10073 if let Some(url) = url {
10074 self.written
10075 .lock()
10076 .unwrap_or_else(std::sync::PoisonError::into_inner)
10077 .insert(content.clone(), url.to_owned());
10078 }
10079 }
10080
10081 /// What one `createIssue` answered, as the item it created — or, when its number cannot be
10082 /// read, the refusal, after taking the issue back.
10083 async fn created_landed(&self, created: &Value) -> Result<Landed, SourceError> {
10084 let content_id = NativeId(required_nonblank_str(created, "id")?.to_owned());
10085 let url = optional_str(created, "url")?.map(str::to_owned);
10086 let number = match created_issue_number(created) {
10087 Ok(number) => number,
10088 Err(error) => {
10089 let _ = self.delete_issue(&content_id).await;
10090 return Err(error);
10091 }
10092 };
10093 self.note_written(&content_id, url.as_deref());
10094 Ok(Landed {
10095 content_id,
10096 item_id: None,
10097 url,
10098 number,
10099 })
10100 }
10101
10102 /// Hold back everything a write of the created item `content` would send after creating it,
10103 /// for [`TaskSource::finish_writes`]: the same field values, closing and edges
10104 /// `finish_write` sends, the parent excepted, which its create already named. A later write
10105 /// of the same item replaces what it changes.
10106 // 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.
10107 #[allow(clippy::too_many_arguments)]
10108 fn hold_back(
10109 &self,
10110 board_id: &BoardId,
10111 content: &NativeId,
10112 existing: Option<&Resolved>,
10113 origin_field: Option<&str>,
10114 origin: &str,
10115 column: Option<(String, String)>,
10116 status_target: Option<&StatusTarget>,
10117 priority: Option<&PriorityWrite>,
10118 projections: &[ProjectionWrite],
10119 native: &[String],
10120 ) -> Result<(), SourceError> {
10121 let status = column.map(|(field, option)| {
10122 let name = self
10123 .fields_cache()
10124 .ok()
10125 .and_then(|fields| {
10126 fields.as_ref().and_then(|board| {
10127 option_name(&board.fields, &field, &option).map(str::to_owned)
10128 })
10129 })
10130 .unwrap_or_default();
10131 (field, option, name)
10132 });
10133 let (fields, clears) =
10134 field_writes(existing, origin_field, origin, None, priority, projections);
10135 let close = match status_target {
10136 Some(StatusTarget::Terminal(_, reason)) => Some(*reason),
10137 _ => None,
10138 };
10139 let mut deferred = self.deferred()?;
10140 let deferred = deferred.get_or_insert_with(Deferred::default);
10141 if let Deferred::Nothing = deferred {
10142 *deferred = Deferred::Held {
10143 board: board_id.clone(),
10144 created: Vec::new(),
10145 };
10146 }
10147 let Deferred::Held { created, .. } = deferred else {
10148 unreachable!("held back on the line above");
10149 };
10150 let held = match created.iter().position(|held| held.content == *content) {
10151 Some(at) => &mut created[at],
10152 None => {
10153 created.push(HeldBack {
10154 content: content.clone(),
10155 fields: Vec::new(),
10156 status: None,
10157 clears: Vec::new(),
10158 close: None,
10159 edges: Vec::new(),
10160 });
10161 created.last_mut().expect("just held")
10162 }
10163 };
10164 for (field, value) in fields {
10165 held.fields.retain(|(held, _)| *held != field);
10166 held.clears.retain(|held| *held != field);
10167 held.fields.push((field, value));
10168 }
10169 for field in clears {
10170 held.fields.retain(|(held, _)| held != field);
10171 if !held.clears.iter().any(|held| held == field) {
10172 held.clears.push(field.to_owned());
10173 }
10174 }
10175 if status.is_some() {
10176 held.status = status;
10177 }
10178 if close.is_some() {
10179 held.close = close;
10180 }
10181 // Nothing of an item created in this command has been sent but its create, so the edges
10182 // it is to have are exactly the ones its latest write names.
10183 held.edges = native.iter().map(|far| NativeId(far.clone())).collect();
10184 Ok(())
10185 }
10186
10187 /// Create every task of `writes`, together, filed on the board and under each one's
10188 /// project: [`graphql::CREATE_ISSUES`], [`MUTATION_BATCH`] to a request.
10189 ///
10190 /// Each is prepared as [`Self::write_item`] prepares one — every refusal a write owes made
10191 /// before the first request — then created, then completed with its parts held back. A
10192 /// request refused part way leaves the issues it did create, which are taken back, with the
10193 /// rest of this call's, before the refusal is answered.
10194 async fn create_together(
10195 &self,
10196 writes: &[ItemWrite<Task>],
10197 ) -> Result<Vec<NativeId>, SourceError> {
10198 let incomings: Vec<Incoming<'_>> = writes
10199 .iter()
10200 .map(|write| self.task_incoming(write, None))
10201 .collect::<Result<_, _>>()?;
10202 let mut prepared = Vec::with_capacity(writes.len());
10203 for (incoming, write) in incomings.iter().zip(writes) {
10204 if write.target.is_some() {
10205 return Err(SourceError::Malformed {
10206 message: "a write naming its target was handed to a batch of creates".into(),
10207 });
10208 }
10209 prepared.push(
10210 self.prepare_write(incoming, None, &write.depends_on)
10211 .await?,
10212 );
10213 }
10214 let mut inputs = Vec::with_capacity(prepared.len());
10215 for (incoming, prepared) in incomings.iter().zip(&prepared) {
10216 let WriteDestination::New(target) = &prepared.destination else {
10217 return Err(SourceError::Malformed {
10218 message: "an update was decided for a write handed to a batch of creates"
10219 .into(),
10220 });
10221 };
10222 let repository_id = self.repository_id(target, incoming).await?;
10223 inputs.push(filed_input(
10224 &repository_id,
10225 prepared.board.id.as_str(),
10226 incoming,
10227 &prepared.body,
10228 ));
10229 }
10230 let mut landed: Vec<Landed> = Vec::with_capacity(inputs.len());
10231 let refused = 'create: {
10232 for chunk in inputs.chunks(MUTATION_BATCH) {
10233 let mut variables = serde_json::Map::new();
10234 for slot in 0..MUTATION_BATCH {
10235 variables.insert(
10236 format!("create{slot}"),
10237 chunk.get(slot).cloned().unwrap_or_else(|| chunk[0].clone()),
10238 );
10239 variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10240 }
10241 let data = match self
10242 .graphql(graphql::CREATE_ISSUES, Value::Object(variables))
10243 .await
10244 {
10245 Ok(data) => data,
10246 Err(error) => break 'create Some(error),
10247 };
10248 let mut refusal = data.get(ANSWER_ERRORS).map(|errors| {
10249 let said: Vec<&str> = errors
10250 .as_array()
10251 .into_iter()
10252 .flatten()
10253 .filter_map(|error| error.get("message").and_then(Value::as_str))
10254 .collect();
10255 SourceError::Refused {
10256 message: if said.is_empty() {
10257 "GitHub returned GraphQL errors".to_owned()
10258 } else {
10259 said.join("; ")
10260 },
10261 }
10262 });
10263 for slot in 0..chunk.len() {
10264 match data
10265 .pointer(&format!("/create{slot}/issue"))
10266 .filter(|value| !value.is_null())
10267 {
10268 Some(created) => match self.created_landed(created).await {
10269 Ok(created) => landed.push(created),
10270 Err(error) => {
10271 refusal.get_or_insert(error);
10272 }
10273 },
10274 // An alias GitHub refused answers nothing, and the refusal says why.
10275 None => {
10276 refusal.get_or_insert(SourceError::Malformed {
10277 message: "GitHub issue creation returned no issue".into(),
10278 });
10279 }
10280 }
10281 }
10282 if let Some(error) = refusal {
10283 break 'create Some(error);
10284 }
10285 }
10286 None
10287 };
10288 if let Some(error) = refused {
10289 // None of these ids reaches the caller, so an issue GitHub will not take back is
10290 // named here or by nobody.
10291 let ids: Vec<&NativeId> = landed.iter().map(|created| &created.content_id).collect();
10292 let left = self.delete_issues(&ids).await;
10293 let left: Vec<&str> = left.iter().map(|id| id.0.as_str()).collect();
10294 return Err(left_behind(error, &left));
10295 }
10296 let created: Vec<NativeId> = landed
10297 .iter()
10298 .map(|landed| landed.content_id.clone())
10299 .collect();
10300 let mut ids = Vec::with_capacity(landed.len());
10301 for ((incoming, prepared), landed) in incomings.iter().zip(prepared).zip(landed) {
10302 match self.complete_write(incoming, prepared, landed).await {
10303 Ok(written) => ids.push(written.id),
10304 // 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.
10305 Err(error) => {
10306 let ids: Vec<&NativeId> = created.iter().collect();
10307 let left = self.delete_issues(&ids).await;
10308 let left: Vec<&str> = left.iter().map(|id| id.0.as_str()).collect();
10309 return Err(left_behind(error, &left));
10310 }
10311 }
10312 }
10313 Ok(ids)
10314 }
10315
10316 /// Hold back the update of the existing issue `issue` — `members`, what its own
10317 /// [`graphql::UPDATE_ISSUE`] would send beside its id — while writes are being held back — so the updates a copy makes one after
10318 /// another go to GitHub as one request. Whatever this source held of the issue is dropped
10319 /// as the mutation itself would drop it. Answers whether it was held: never while a write
10320 /// the store's cache expects runs, which records each mutation's own answer, and never for
10321 /// an issue an update is already held for, which that one is sent ahead of.
10322 fn hold_update(&self, issue: &NativeId, members: &Value) -> Result<bool, SourceError> {
10323 if self.expected.journaling() || self.deferred()?.is_none() {
10324 return Ok(false);
10325 }
10326 let mut held = self
10327 .held_updates
10328 .lock()
10329 .map_err(|_| SourceError::Unavailable {
10330 message: "issue updates held back were left inconsistent; run the command again"
10331 .into(),
10332 })?;
10333 if held.iter().any(|earlier| earlier.issue == *issue) {
10334 return Ok(false);
10335 }
10336 let named = |value: &str| issue.0 == value;
10337 self.resolved_cache()?
10338 .retain(|id, item| !named(&id.0) && !named(&item.item_id));
10339 self.read_ahead()?.retain(|id, item| {
10340 !named(&id.0) && !item.as_ref().is_some_and(|item| named(&item.item_id))
10341 });
10342 self.carried()?
10343 .retain(|id, item| !named(&id.0) && !named(&item.item_id));
10344 held.push(HeldUpdate::new(issue, members));
10345 Ok(true)
10346 }
10347
10348 /// Send every update [`Self::hold_update`] held back: one alone as [`graphql::UPDATE_ISSUE`]
10349 /// sends it, several as aliased `updateIssue`s of [`graphql::UPDATE_ISSUES`],
10350 /// [`MUTATION_BATCH`] to a request. Each counts one write, as it would alone.
10351 async fn send_held_updates(&self) -> Result<(), SourceError> {
10352 let held = std::mem::take(&mut *self.held_updates.lock().map_err(|_| {
10353 SourceError::Unavailable {
10354 message: "issue updates held back were left inconsistent; run the command again"
10355 .into(),
10356 }
10357 })?);
10358 if let [one] = held.as_slice() {
10359 return self
10360 .send_update(
10361 graphql::UPDATE_ISSUE,
10362 "/updateIssue/issue",
10363 &one.issue,
10364 one.input.clone(),
10365 )
10366 .await;
10367 }
10368 for chunk in held.chunks(MUTATION_BATCH) {
10369 let mut variables = serde_json::Map::new();
10370 for slot in 0..MUTATION_BATCH {
10371 variables.insert(
10372 format!("update{slot}"),
10373 chunk.get(slot).unwrap_or(&chunk[0]).input.clone(),
10374 );
10375 variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10376 }
10377 let data = self
10378 .graphql(graphql::UPDATE_ISSUES, Value::Object(variables))
10379 .await?;
10380 let inputs: Vec<&Value> = chunk.iter().map(|held| &held.input).collect();
10381 check_aliased_answers(
10382 graphql::UPDATE_ISSUES,
10383 &data,
10384 "update",
10385 &inputs,
10386 &[("issue", "id")],
10387 )?;
10388 }
10389 Ok(())
10390 }
10391
10392 /// Send everything held back since [`TaskSource::begin_deferred_writes`]: one read of the
10393 /// created items' board items, then their field values and edges together in requests of
10394 /// [`WRITE_BATCH`], then their closes in requests of [`MUTATION_BATCH`]. Anything refused
10395 /// takes back every issue held, [`DELETE_BATCH`] to a request, and answers the refusal.
10396 async fn send_held_back(&self) -> Result<(), SourceError> {
10397 let Some(Deferred::Held { board, created }) = self.deferred()?.take() else {
10398 return Ok(());
10399 };
10400 if created.is_empty() {
10401 return Ok(());
10402 }
10403 let sent = self.send_deferred(board.as_str(), &created).await;
10404 if let Err(error) = sent {
10405 // Best effort, and the failure is what the caller is told, as a write of one item
10406 // that fails part way takes back what it created: the copy that held these back
10407 // journalled every one, so one this cannot take back its undo tries again, and says
10408 // so when it cannot either.
10409 let ids: Vec<&NativeId> = created.iter().map(|held| &held.content).collect();
10410 let _ = self.delete_issues(&ids).await;
10411 return Err(error);
10412 }
10413 Ok(())
10414 }
10415
10416 /// The requests [`Self::send_held_back`] makes.
10417 async fn send_deferred(&self, board: &str, created: &[HeldBack]) -> Result<(), SourceError> {
10418 let items = self.created_board_items(board, created).await?;
10419 let mut fields: Vec<Value> = Vec::new();
10420 let mut closes: Vec<Value> = Vec::new();
10421 let mut edges: Vec<Value> = Vec::new();
10422 for held in created {
10423 let (item, status) = &items[&held.content];
10424 let mut values = held.fields.clone();
10425 // A status the board already shows — set by a workflow a person turned on to file
10426 // new items at it — is not written again.
10427 if let Some((field, option, name)) = &held.status
10428 && status.as_deref() != Some(name.as_str())
10429 {
10430 values.insert(0, (field.clone(), json!({"singleSelectOptionId": option})));
10431 }
10432 for (field, value) in values {
10433 fields.push(json!({"projectId":board,"itemId":item,"fieldId":field,"value":value}));
10434 }
10435 if let Some(reason) = held.close {
10436 closes.push(json!({"id": held.content.0, "stateInput": closed_input(reason)}));
10437 }
10438 for far in &held.edges {
10439 edges.push(json!({"issueId": held.content.0, "blockingIssueId": far.0}));
10440 }
10441 if !held.clears.is_empty() {
10442 let clears: Vec<&str> = held.clears.iter().map(String::as_str).collect();
10443 self.set_item_fields(board, item, &[], &clears).await?;
10444 }
10445 }
10446 // Each alias answers with what it wrote, which is what its input named: the board item a
10447 // field value is set on, the issue closed, the issue blocked and the one blocking it.
10448 let field_answer: &[(&str, &str)] = &[("projectV2Item", "itemId")];
10449 let close_answer: &[(&str, &str)] = &[("issue", "id")];
10450 let block_answer: &[(&str, &str)] =
10451 &[("issue", "issueId"), ("blockingIssue", "blockingIssueId")];
10452 // Every field value, then every edge, as one ordered list sent `WRITE_BATCH` aliases to a
10453 // request: a request holding both kinds is the one document carrying both, so a plan's
10454 // values and edges cost one request where they fit in one.
10455 let writes: Vec<HeldWrite<'_>> = fields
10456 .iter()
10457 .map(HeldWrite::Field)
10458 .chain(edges.iter().map(HeldWrite::Edge))
10459 .collect();
10460 for chunk in writes.chunks(WRITE_BATCH) {
10461 let values: Vec<&Value> = chunk
10462 .iter()
10463 .filter_map(|write| match write {
10464 HeldWrite::Field(input) => Some(*input),
10465 HeldWrite::Edge(_) => None,
10466 })
10467 .collect();
10468 let blocks: Vec<&Value> = chunk
10469 .iter()
10470 .filter_map(|write| match write {
10471 HeldWrite::Edge(input) => Some(*input),
10472 HeldWrite::Field(_) => None,
10473 })
10474 .collect();
10475 let (document, sends) = match (values.is_empty(), blocks.is_empty()) {
10476 (false, false) => (graphql::WRITE_FIELDS_AND_EDGES, ["sendField", "sendBlock"]),
10477 (false, true) => (graphql::WRITE_FIELD_VALUES, ["send", "send"]),
10478 _ => (graphql::ADD_BLOCKED_BYS, ["send", "send"]),
10479 };
10480 let mut variables = serde_json::Map::new();
10481 let kinds = [
10482 ("field", &values, sends[0], field_answer),
10483 ("block", &blocks, sends[1], block_answer),
10484 ];
10485 for (prefix, inputs, send, _) in &kinds {
10486 if inputs.is_empty() {
10487 continue;
10488 }
10489 for slot in 0..WRITE_BATCH {
10490 variables.insert(
10491 format!("{prefix}{slot}"),
10492 (*inputs.get(slot).unwrap_or(&inputs[0])).clone(),
10493 );
10494 variables.insert(format!("{send}{slot}"), json!(slot < inputs.len()));
10495 }
10496 }
10497 let data = self.graphql(document, Value::Object(variables)).await?;
10498 for (prefix, inputs, _, answers) in kinds {
10499 check_aliased_answers(document, &data, prefix, inputs, answers)?;
10500 }
10501 }
10502 for chunk in closes.chunks(MUTATION_BATCH) {
10503 let mut variables = serde_json::Map::new();
10504 for slot in 0..MUTATION_BATCH {
10505 variables.insert(
10506 format!("close{slot}"),
10507 chunk.get(slot).cloned().unwrap_or_else(|| chunk[0].clone()),
10508 );
10509 variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10510 }
10511 let data = self
10512 .graphql(graphql::CLOSE_ISSUES, Value::Object(variables))
10513 .await?;
10514 let inputs: Vec<&Value> = chunk.iter().collect();
10515 check_aliased_answers(graphql::CLOSE_ISSUES, &data, "close", &inputs, close_answer)?;
10516 }
10517 for held in created {
10518 self.learn_item(&held.content, &items[&held.content].0)?;
10519 }
10520 Ok(())
10521 }
10522
10523 /// The board item each of `created` is on this board, with its `Status` as the board set it:
10524 /// [`graphql::CREATED_BOARD_ITEMS`], read again — after a wait of a second, then two, four,
10525 /// eight and sixteen — for only those GitHub has not filed yet.
10526 ///
10527 /// **Membership is never assumed.** `createIssue`'s `projectV2Ids` files an issue some time
10528 /// after GitHub answered the create — the feasibility spike saw 0.45 to 1.84 seconds, and no
10529 /// create answer carrying the item — so only a read says it is there. One still absent once
10530 /// those reads are spent is filed with [`graphql::ADD_TO_BOARD`], one write each; GitHub
10531 /// filing it meanwhile refuses that "Content already exists in this project", which says it
10532 /// is placed, and one more read finds the item rather than a second one being made.
10533 async fn created_board_items(
10534 &self,
10535 board: &str,
10536 created: &[HeldBack],
10537 ) -> Result<BTreeMap<NativeId, (String, Option<String>)>, SourceError> {
10538 let mut found: BTreeMap<NativeId, (String, Option<String>)> = BTreeMap::new();
10539 let unfiled = |found: &BTreeMap<NativeId, (String, Option<String>)>| -> Vec<NativeId> {
10540 created
10541 .iter()
10542 .map(|held| held.content.clone())
10543 .filter(|id| !found.contains_key(id))
10544 .collect()
10545 };
10546 let mut wait = Duration::from_secs(1);
10547 for attempt in 0..6 {
10548 let absent = unfiled(&found);
10549 if absent.is_empty() {
10550 return Ok(found);
10551 }
10552 if attempt > 0 {
10553 self.clock
10554 .sleep_because(wait, pacing::cause::BOARD_FILING)
10555 .await;
10556 wait = wait.saturating_mul(2);
10557 }
10558 self.read_created_items(board, &absent, &mut found).await?;
10559 }
10560 let mut placed_meanwhile = Vec::new();
10561 for id in unfiled(&found) {
10562 match self
10563 .graphql(
10564 graphql::ADD_TO_BOARD,
10565 json!({"input":{"projectId":board,"contentId":id.0}}),
10566 )
10567 .await
10568 {
10569 Ok(added) => {
10570 let item = added
10571 .pointer("/addProjectV2ItemById/item")
10572 .filter(|value| !value.is_null())
10573 .ok_or_else(|| SourceError::Malformed {
10574 message: "GitHub board addition returned no project item".into(),
10575 })?;
10576 found.insert(id, (required_nonblank_str(item, "id")?.to_owned(), None));
10577 }
10578 Err(error) if already_on_the_board(&error) => placed_meanwhile.push(id),
10579 Err(error) => return Err(error),
10580 }
10581 }
10582 if !placed_meanwhile.is_empty() {
10583 self.read_created_items(board, &placed_meanwhile, &mut found)
10584 .await?;
10585 }
10586 let absent = unfiled(&found);
10587 if absent.is_empty() {
10588 return Ok(found);
10589 }
10590 let absent: Vec<&str> = absent.iter().map(|id| id.0.as_str()).collect();
10591 Err(SourceError::Unavailable {
10592 message: format!(
10593 "GitHub created {} filed on board {board} and, after 31 seconds and adding {} \
10594 to it, still answers {} on no item of it: {}; next: run the copy again once \
10595 GitHub has caught up",
10596 if absent.len() == 1 {
10597 "an issue"
10598 } else {
10599 "issues"
10600 },
10601 if absent.len() == 1 { "it" } else { "them" },
10602 if absent.len() == 1 { "it" } else { "them" },
10603 absent.join(", ")
10604 ),
10605 })
10606 }
10607
10608 /// One [`graphql::CREATED_BOARD_ITEMS`] read of each of `ids`, [`COPY_DETAIL_BATCH`] to a
10609 /// request, adding to `found` the board item of every one GitHub has filed on `board`.
10610 async fn read_created_items(
10611 &self,
10612 board: &str,
10613 ids: &[NativeId],
10614 found: &mut BTreeMap<NativeId, (String, Option<String>)>,
10615 ) -> Result<(), SourceError> {
10616 for chunk in ids.chunks(COPY_DETAIL_BATCH) {
10617 let mut variables = serde_json::Map::new();
10618 for slot in 0..COPY_DETAIL_BATCH {
10619 variables.insert(
10620 format!("id{slot}"),
10621 json!(chunk.get(slot).unwrap_or(&chunk[0]).0),
10622 );
10623 variables.insert(format!("read{slot}"), json!(slot < chunk.len()));
10624 }
10625 variables.insert("boardItems".into(), json!(BOARD_ITEMS_PAGE_SIZE));
10626 variables.insert("statusName".into(), json!(STATUS_FIELD));
10627 let data = self
10628 .graphql(graphql::CREATED_BOARD_ITEMS, Value::Object(variables))
10629 .await?;
10630 for (slot, id) in chunk.iter().enumerate() {
10631 // An answer for another issue than the one asked about says nothing of
10632 // where that one is filed, and its board item is not this one's.
10633 let node = data.get(format!("i{slot}")).filter(|node| !node.is_null());
10634 let named = node.map(|node| node.get("id").and_then(Value::as_str));
10635 if let Some(named) = named
10636 && named != Some(id.0.as_str())
10637 {
10638 return Err(SourceError::Malformed {
10639 message: format!(
10640 "GitHub answered the board items of {} when asked for those of {}",
10641 named.unwrap_or("an issue it did not name"),
10642 id.0
10643 ),
10644 });
10645 }
10646 // An issue GitHub does not answer yet is not filed yet; one it answers is
10647 // read whole, its connection, each board item's board and its status, and
10648 // any of them unreadable is refused rather than read as not filed.
10649 let Some(node) = node else { continue };
10650 let malformed = |what: &str| SourceError::Malformed {
10651 message: format!("GitHub answered the board items of {} with {what}", id.0),
10652 };
10653 let nodes = node
10654 .pointer("/projectItems/nodes")
10655 .and_then(Value::as_array)
10656 .ok_or_else(|| malformed("no list of board items"))?;
10657 let mut item = None;
10658 for node in nodes {
10659 let on = node
10660 .pointer("/project/id")
10661 .and_then(Value::as_str)
10662 .ok_or_else(|| malformed("a board item naming no board"))?;
10663 if on == board {
10664 item = Some(node);
10665 }
10666 }
10667 if let Some(item) = item {
10668 let status = match item.get("status") {
10669 None | Some(Value::Null) => None,
10670 Some(status) => Some(
10671 status
10672 .get("name")
10673 .and_then(Value::as_str)
10674 .ok_or_else(|| malformed("a status that names no option"))?
10675 .to_owned(),
10676 ),
10677 };
10678 found.insert(
10679 id.clone(),
10680 (required_nonblank_str(item, "id")?.to_owned(), status),
10681 );
10682 }
10683 }
10684 }
10685 Ok(())
10686 }
10687
10688 /// The board item id of `content`, which this process created and remembered without one,
10689 /// in every half of its own view.
10690 fn learn_item(&self, content: &NativeId, item: &str) -> Result<(), SourceError> {
10691 if let Some(held) = self.resolved_cache()?.get_mut(content) {
10692 held.item_id = item.to_owned();
10693 }
10694 if let Some(held) = self.created()?.iter_mut().find(|held| held.id == *content) {
10695 held.item_id = item.to_owned();
10696 }
10697 Ok(())
10698 }
10699
10700 /// Ask together which items each of `origins` names, and what creating an item reads; see
10701 /// [`TaskSource::read_ahead_origins_for_copy`]. An origin whose answer runs past its first
10702 /// page is left to its own lookup.
10703 async fn origins_ahead(&self, origins: &[String]) -> Result<(), SourceError> {
10704 if self.repository.is_none() {
10705 return Ok(());
10706 }
10707 let unasked: Vec<&String> = {
10708 let held = self.narrowed_cache()?;
10709 let mut unasked: Vec<&String> = Vec::new();
10710 for origin in origins {
10711 if !held.contains_key(&Narrowing::Origin(origin.clone()).key())
10712 && !unasked.contains(&origin)
10713 {
10714 unasked.push(origin);
10715 }
10716 }
10717 unasked
10718 };
10719 if unasked.is_empty() {
10720 return Ok(());
10721 }
10722 // One batch is held back for the read ahead that follows, which may carry a project's
10723 // members beside it in the same request.
10724 if unasked.len() <= ORIGIN_BATCH {
10725 let mut pending =
10726 self.pending_origins
10727 .lock()
10728 .map_err(|_| SourceError::Unavailable {
10729 message:
10730 "origins held for a read ahead were left inconsistent; run the command \
10731 again"
10732 .into(),
10733 })?;
10734 for origin in unasked {
10735 if !pending.contains(origin) {
10736 pending.push(origin.clone());
10737 }
10738 }
10739 return Ok(());
10740 }
10741 let unasked: Vec<String> = unasked.into_iter().cloned().collect();
10742 self.send_origin_lookups(&unasked, None).await
10743 }
10744
10745 /// Hold, for [`TaskSource::member_classifications`] of `project`, each member's
10746 /// classification and repositories as the members page `data` carries them — when that page
10747 /// is every member, each one names `project` as its parent, and every record it holds is
10748 /// readable. Any other page holds nothing, so the members are read as they always were.
10749 fn hold_member_classes(&self, project: &NativeId, data: &Value) -> Result<(), SourceError> {
10750 let Some(page) = data
10751 .pointer("/members/subIssues")
10752 .filter(|page| !page.is_null())
10753 else {
10754 return Ok(());
10755 };
10756 if page
10757 .pointer("/pageInfo/hasNextPage")
10758 .and_then(Value::as_bool)
10759 != Some(false)
10760 {
10761 return Ok(());
10762 }
10763 let Some(nodes) = page.get("nodes").and_then(Value::as_array) else {
10764 return Ok(());
10765 };
10766 let mut members = Vec::with_capacity(nodes.len());
10767 for node in nodes {
10768 if node.pointer("/parent/id").and_then(Value::as_str) != Some(project.0.as_str()) {
10769 return Ok(());
10770 }
10771 let Some(body) = node.get("body").and_then(Value::as_str) else {
10772 return Ok(());
10773 };
10774 // Every node here is filed under `project`, which `BoardKind::of` reads as one of its
10775 // tasks whatever its marker says, so each is a member the full read would classify.
10776 let Ok((_, slot)) = metadata_body(Some(body.to_owned())) else {
10777 return Ok(());
10778 };
10779 let Ok(classification) = Classification::from_metadata(&slot) else {
10780 return Ok(());
10781 };
10782 let repositories = if slot.contains_key(Repository::METADATA_KEY) {
10783 let Ok(repositories) = Repository::from_metadata(&slot) else {
10784 return Ok(());
10785 };
10786 repositories
10787 } else {
10788 let Some(origin) = node
10789 .pointer("/repository/nameWithOwner")
10790 .and_then(Value::as_str)
10791 else {
10792 return Ok(());
10793 };
10794 let Ok(own) = Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST))
10795 else {
10796 return Ok(());
10797 };
10798 vec![own]
10799 };
10800 members.push((classification, repositories));
10801 }
10802 self.member_classes
10803 .lock()
10804 .map_err(|_| SourceError::Unavailable {
10805 message: "a project's members held for its classification were left \
10806 inconsistent; run the command again"
10807 .into(),
10808 })?
10809 .insert(project.clone(), members);
10810 Ok(())
10811 }
10812
10813 /// Send every origin held back for the read ahead, beside the members of `members` when it
10814 /// names a project — or nothing, when none is held.
10815 async fn send_pending_origins(&self, members: Option<&NativeId>) -> Result<(), SourceError> {
10816 let pending = std::mem::take(&mut *self.pending_origins.lock().map_err(|_| {
10817 SourceError::Unavailable {
10818 message: "origins held for a read ahead were left inconsistent; run the command \
10819 again"
10820 .into(),
10821 }
10822 })?);
10823 if pending.is_empty() {
10824 return Ok(());
10825 }
10826 self.send_origin_lookups(&pending, members).await
10827 }
10828
10829 /// [`graphql::ORIGIN_LOOKUPS`] of `unasked`, [`ORIGIN_BATCH`] to a request — the first one
10830 /// [`graphql::ORIGIN_LOOKUPS_WITH_MEMBERS`], carrying the members of `members`, when it names
10831 /// a project — holding each origin's answer and, from a members page that is the whole of
10832 /// them, each member's classification.
10833 async fn send_origin_lookups(
10834 &self,
10835 unasked: &[String],
10836 members: Option<&NativeId>,
10837 ) -> Result<(), SourceError> {
10838 let Some(repository) = self.repository.clone() else {
10839 return Ok(());
10840 };
10841 let mut members = members;
10842 for chunk in unasked.chunks(ORIGIN_BATCH) {
10843 let mut variables = json!({"owner":self.owner,"number":self.project_number,
10844 "type":"ISSUE","lookupFirst":ORIGIN_LOOKUPS_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
10845 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true,
10846 "repositoryOwner":repository.owner,"repositoryName":repository.name});
10847 for slot in 0..ORIGIN_BATCH {
10848 let origin = chunk.get(slot).unwrap_or(&chunk[0]);
10849 variables[format!("filter{slot}")] =
10850 json!(format!("{ORIGIN_FIELD}:{}", quoted(origin)));
10851 variables[format!("search{slot}")] = json!(
10852 self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))))
10853 );
10854 variables[format!("ask{slot}")] = json!(slot < chunk.len());
10855 }
10856 let project = members.take();
10857 let document = match project {
10858 Some(project) => {
10859 variables["project"] = json!(project.0);
10860 variables["memberFirst"] = json!(MEMBER_PAGE_SIZE);
10861 graphql::ORIGIN_LOOKUPS_WITH_MEMBERS
10862 }
10863 None => graphql::ORIGIN_LOOKUPS,
10864 };
10865 let data = self.graphql(document, variables).await?;
10866 self.fields_read(&data)?;
10867 if let Some(project) = project {
10868 self.hold_member_classes(project, &data)?;
10869 }
10870 if let Some(node) = data.get("repository").filter(|value| !value.is_null()) {
10871 let id = required_nonblank_str(node, "id")?.to_owned();
10872 self.repository_cache()?.insert(repository.clone(), id);
10873 if let Some(numeric) = node
10874 .get("databaseId")
10875 .and_then(Value::as_u64)
10876 .and_then(std::num::NonZeroU64::new)
10877 {
10878 self.numeric_repositories
10879 .lock()
10880 .await
10881 .insert(repository.clone(), numeric);
10882 }
10883 }
10884 for (slot, origin) in chunk.iter().enumerate() {
10885 let items = data
10886 .pointer(&format!("/o{slot}/projectV2/items"))
10887 .filter(|value| !value.is_null())
10888 .ok_or_else(|| SourceError::Refused {
10889 message: format!(
10890 "GitHub project {}/{} was not found or is not visible to the token",
10891 self.owner, self.project_number
10892 ),
10893 })?;
10894 let searched =
10895 data.get(format!("s{slot}"))
10896 .ok_or_else(|| SourceError::Malformed {
10897 message: "GitHub search response has no search connection".into(),
10898 })?;
10899 if resumed(items, None)?.has_more() || resumed(searched, None)?.has_more() {
10900 continue;
10901 }
10902 let mut found: Vec<Resolved> = Vec::new();
10903 for item in optional_nodes(Some(items), "project items")?
10904 .into_iter()
10905 .flatten()
10906 {
10907 if let Some(resolved) = self.resolve(item)?
10908 && resolved.content_kind == ContentKind::Issue
10909 && !found.iter().any(|held| held.id == resolved.id)
10910 {
10911 found.push(resolved);
10912 }
10913 }
10914 for node in optional_nodes(Some(searched), "search")?
10915 .into_iter()
10916 .flatten()
10917 {
10918 if let Some(resolved) = self.resolve_issue(node).await?
10919 && !found.iter().any(|held| held.id == resolved.id)
10920 {
10921 found.push(resolved);
10922 }
10923 }
10924 self.narrowed_cache()?
10925 .insert(Narrowing::Origin((*origin).clone()).key(), found);
10926 }
10927 }
10928 Ok(())
10929 }
10930
10931 /// Delete every issue of `ids`, [`DELETE_BATCH`] to a request with
10932 /// [`graphql::DELETE_ISSUES`], and answer those still there: each a request's answer does not
10933 /// confirm, every one of a request refused whole included.
10934 async fn delete_issues(&self, ids: &[&NativeId]) -> Vec<NativeId> {
10935 let mut left = Vec::new();
10936 for chunk in ids.chunks(DELETE_BATCH) {
10937 let mut variables = serde_json::Map::new();
10938 for slot in 0..DELETE_BATCH {
10939 let id = chunk.get(slot).unwrap_or(&chunk[0]);
10940 variables.insert(format!("delete{slot}"), json!({"issueId": id.0}));
10941 variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10942 }
10943 let data = self
10944 .graphql(graphql::DELETE_ISSUES, Value::Object(variables))
10945 .await
10946 .unwrap_or(Value::Null);
10947 for (slot, id) in chunk.iter().enumerate() {
10948 // Confirmed only by the repository the deleted issue was in, named by a
10949 // non-blank id: anything else says nothing of whether the issue is gone.
10950 let confirmed = data
10951 .pointer(&format!("/delete{slot}/repository/id"))
10952 .and_then(Value::as_str)
10953 .is_some_and(|id| !id.trim().is_empty());
10954 if !confirmed {
10955 left.push((*id).clone());
10956 }
10957 }
10958 }
10959 left
10960 }
10961
10962 /// Delete one issue, which takes its board item with it.
10963 async fn delete_issue(&self, id: &NativeId) -> Result<(), SourceError> {
10964 let data = self
10965 .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
10966 .await?;
10967 data.pointer("/deleteIssue/repository")
10968 .filter(|value| !value.is_null())
10969 .ok_or_else(|| SourceError::Malformed {
10970 message: "GitHub issue deletion returned no repository".into(),
10971 })?;
10972 self.forget(id)?;
10973 Ok(())
10974 }
10975
10976 /// Remove one item this copy created, so a copy that could not finish leaves the board
10977 /// as it found it.
10978 ///
10979 /// Deleting the issue takes its board item with it, so there is no second mutation to
10980 /// keep in step. An id the board does not hold is not an error: the item is already
10981 /// gone, which is the state this asks for. Which that is, is decided by reading the item
10982 /// by its own id — a listing of the board can still be missing an item it holds, and
10983 /// reading that as *already gone* would leave behind the very item this was asked to
10984 /// take back.
10985 async fn delete_item(&self, id: &NativeId) -> Result<(), SourceError> {
10986 let Some(item) = self.bound_item(id).await? else {
10987 return Ok(());
10988 };
10989 if item.content_kind == ContentKind::DraftIssue {
10990 return Err(SourceError::Refused {
10991 message: format!(
10992 "GitHub item {} is a draft, and this source removes an item by deleting \
10993 its issue; next: remove it from the board by hand",
10994 id.0
10995 ),
10996 });
10997 }
10998 let data = self
10999 .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
11000 .await?;
11001 data.pointer("/deleteIssue/repository")
11002 .filter(|value| !value.is_null())
11003 .ok_or_else(|| SourceError::Malformed {
11004 message: "GitHub issue deletion returned no repository".into(),
11005 })?;
11006 self.forget(id)?;
11007 Ok(())
11008 }
11009
11010 /// The issue a comment call on `task` is about, or `None` when this board holds no such
11011 /// task.
11012 ///
11013 /// Resolved exactly as [`TaskSource::get_task`] resolves it, so the comment verbs and a
11014 /// read of the task cannot disagree about which ids name one: a project or a document of
11015 /// this board is not a task here either.
11016 ///
11017 /// A **draft** is a task with nowhere to keep a comment, because GitHub keeps comments on
11018 /// issues and a draft is not one. It is refused rather than answered with an empty page,
11019 /// which would read as a task nobody has commented on yet.
11020 async fn commented_issue(&self, task: &NativeId) -> Result<Option<NativeId>, SourceError> {
11021 let cached = self.resolved_cache()?.get(task).cloned();
11022 let Some(item) = (match cached {
11023 Some(item) => Some(item),
11024 None => self.item_by_id(task).await?,
11025 })
11026 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
11027 return Ok(None);
11028 };
11029 if item.content_kind == ContentKind::DraftIssue {
11030 return Err(self.draft_has_no_comments(task));
11031 }
11032 Ok(Some(item.id))
11033 }
11034
11035 /// The refusal a comment call on a board draft is answered with: GitHub keeps comments on
11036 /// issues, and a draft is not one.
11037 fn draft_has_no_comments(&self, task: &NativeId) -> SourceError {
11038 SourceError::Refused {
11039 message: format!(
11040 "task {} of source {} is a draft item on the board, and GitHub keeps \
11041 comments on issues alone, so a draft has none to read or write; next: \
11042 convert the draft to an issue on the board, then comment on the issue it \
11043 becomes",
11044 task.0, self.name
11045 ),
11046 }
11047 }
11048
11049 /// One task and a page of its comments, read with [`graphql::ISSUE_DETAIL`] in one
11050 /// request — or `None` when this board holds no task by that id.
11051 ///
11052 /// What `task show` and a comment listing read. A draft is a task with no comments, so it
11053 /// is answered with the draft and the refusal, at the price of the draft's own read.
11054 async fn issue_detail(
11055 &self,
11056 id: &NativeId,
11057 page: &PageRequest,
11058 ) -> Result<Option<TaskDetailRead>, SourceError> {
11059 let after = page.cursor.as_ref().map(|cursor| cursor.0.as_str());
11060 let asked = self
11061 .graphql(
11062 graphql::ISSUE_DETAIL,
11063 json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),"after":after,
11064 "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
11065 "duplicates":true}),
11066 )
11067 .await;
11068 let data = match asked {
11069 Ok(data) => data,
11070 Err(error) if unresolvable_node(&error) => return Ok(None),
11071 Err(error) => return Err(error),
11072 };
11073 // `node` is null for an id that names nothing, and absent only from an answer this
11074 // source cannot read — never the same thing.
11075 let node = data.get("node").ok_or_else(|| SourceError::Malformed {
11076 message: format!("GitHub answered the read of {} with no node", id.0),
11077 })?;
11078 self.detail_of(id, node, true, after).await
11079 }
11080
11081 /// Several tasks, each with the first page of its comments when `comments` is set, read
11082 /// [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] — one answer per id, in
11083 /// order.
11084 ///
11085 /// A batch GitHub refuses because one of its ids resolves to no node at all is read again
11086 /// one item at a time, so that id is answered as missing and the others as themselves; any
11087 /// other refusal is every id of that batch's answer.
11088 async fn issue_details(
11089 &self,
11090 ids: &[NativeId],
11091 comments: Option<&PageRequest>,
11092 ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
11093 let mut read = Vec::with_capacity(ids.len());
11094 for batch in ids.chunks(DETAIL_BATCH) {
11095 match self
11096 .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, comments))
11097 .await
11098 {
11099 Ok(data) => {
11100 for (slot, id) in batch.iter().enumerate() {
11101 // Every alias asked for is answered, null for an id naming nothing;
11102 // one missing is an answer this source cannot read.
11103 let read_one = match data.get(format!("i{slot}")) {
11104 Some(node) => self.detail_of(id, node, comments.is_some(), None).await,
11105 None => Err(SourceError::Malformed {
11106 message: format!(
11107 "GitHub answered a batch read with no item for {}",
11108 id.0
11109 ),
11110 }),
11111 };
11112 read.push(read_one);
11113 }
11114 }
11115 Err(error) if unresolvable_node(&error) => {
11116 for id in batch {
11117 read.push(match comments {
11118 Some(page) => self.issue_detail(id, page).await,
11119 None => self.task_read(id).await,
11120 });
11121 }
11122 }
11123 Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
11124 }
11125 }
11126 read
11127 }
11128
11129 /// One task and nothing of its comments, as [`TaskSource::get_task`] reads it.
11130 async fn task_read(&self, id: &NativeId) -> Result<Option<TaskDetailRead>, SourceError> {
11131 Ok(self.get_task(id).await?.map(|task| TaskDetailRead {
11132 task,
11133 comments: None,
11134 }))
11135 }
11136
11137 /// What one node a detail read reached says: the task this board holds by `id`, with the
11138 /// page of comments the node carries when `commented` — or `None` for a node that is no
11139 /// task of this board.
11140 ///
11141 /// Resolved as [`Self::item_by_id`] resolves an item: a draft is read again as a draft,
11142 /// and an item this process created answers from this process's own record, which a node
11143 /// read taken moments after the write can still be behind.
11144 async fn detail_of(
11145 &self,
11146 id: &NativeId,
11147 node: &Value,
11148 commented: bool,
11149 after: Option<&str>,
11150 ) -> Result<Option<TaskDetailRead>, SourceError> {
11151 if node.is_null() {
11152 return Ok(None);
11153 }
11154 let draft = optional_str(node, "__typename")? == Some("DraftIssue");
11155 // An issue answered under one id is that id's, or the answer is not one this source
11156 // can report: reporting another issue's task and comments under the qualified id asked
11157 // for would be the one wrong answer here. A draft's own read checks the same.
11158 if !draft
11159 && optional_str(node, "__typename")? == Some("Issue")
11160 && required_str(node, "id")? != id.0
11161 {
11162 return Err(SourceError::Malformed {
11163 message: format!(
11164 "GitHub answered the read of {} with issue {}",
11165 id.0,
11166 required_str(node, "id")?
11167 ),
11168 });
11169 }
11170 let item = if draft {
11171 self.draft_by_id(id).await?
11172 } else {
11173 self.resolve_issue(node).await?
11174 };
11175 // A read that carried the whole of what the issue is blocked by — a copy's — is kept
11176 // for this command, so the dependency read the copy makes next is answered from it.
11177 if let Some(read) = item.as_ref().filter(|item| item.blocked_by.is_some()) {
11178 self.resolved_cache()?.insert(read.id.clone(), read.clone());
11179 }
11180 let Some(item) = item.filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
11181 return Ok(None);
11182 };
11183 let own = self.created()?.iter().find(|own| own.id == *id).cloned();
11184 let task = own.unwrap_or(item).task()?;
11185 let comments = match (commented, draft) {
11186 (false, _) => None,
11187 (true, true) => Some(Err(self.draft_has_no_comments(id))),
11188 (true, false) => Some(comment_page(node, &id.0, after).map(Some)),
11189 };
11190 if commented && !draft && after.is_none() {
11191 self.expected.learn_comments(id, node);
11192 }
11193 Ok(Some(TaskDetailRead { task, comments }))
11194 }
11195
11196 /// Whether the comment `comment` is one of `issue`'s own.
11197 ///
11198 /// Read before an edit or a removal is sent, because GitHub's comment mutations take the
11199 /// comment's id and nothing else: a comment id given against the wrong task would
11200 /// otherwise change a comment on some other issue entirely. An id that names nothing, or
11201 /// names something that is not an issue comment, is a comment this task does not have —
11202 /// which is what GitHub refusing to resolve it means too.
11203 async fn comment_is_on(
11204 &self,
11205 issue: &NativeId,
11206 comment: &NativeId,
11207 ) -> Result<bool, SourceError> {
11208 Ok(self.comment_on(issue, comment).await?.is_some())
11209 }
11210
11211 /// The body the comment `comment` holds, exactly as GitHub holds it, when it is one of
11212 /// `issue`'s own — the read [`Self::comment_is_on`] makes, answering with what it selected.
11213 async fn comment_on(
11214 &self,
11215 issue: &NativeId,
11216 comment: &NativeId,
11217 ) -> Result<Option<String>, SourceError> {
11218 // A write the store's cache expects knows which comments its issue held, and their
11219 // bodies, at the version it expects: what this read would answer.
11220 if let Some(primed) = self.expected.primed_comment(issue, comment) {
11221 return Ok(primed);
11222 }
11223 let asked = self
11224 .graphql(graphql::COMMENT_ISSUE, json!({"id":comment.0}))
11225 .await;
11226 let data = match asked {
11227 Ok(data) => data,
11228 Err(error) if unresolvable_node(&error) => return Ok(None),
11229 Err(error) => return Err(error),
11230 };
11231 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
11232 return Ok(None);
11233 };
11234 if optional_str(node, "__typename")? != Some("IssueComment") {
11235 return Ok(None);
11236 }
11237 let on = node.get("issue").ok_or_else(|| SourceError::Malformed {
11238 message: format!("GitHub issue comment {} names no issue", comment.0),
11239 })?;
11240 if required_str(on, "id")? != issue.0 {
11241 return Ok(None);
11242 }
11243 Ok(Some(required_str(node, "body")?.to_owned()))
11244 }
11245
11246 /// Which far ends this item's own `blockedBy` relationship holds, and which it cannot.
11247 async fn partition_edges(
11248 &self,
11249 near_kind: BoardKind,
11250 near_content: ContentKind,
11251 carried: Option<&[Value]>,
11252 depends_on: &[DependencyEdge],
11253 ) -> Result<(Vec<String>, Vec<DependencyEdge>), SourceError> {
11254 let mut native = Vec::new();
11255 let mut fallback = Vec::new();
11256 let far_ends: Vec<(&DependencyEdge, &str, bool, Option<&Value>)> = depends_on
11257 .iter()
11258 .map(|edge| {
11259 // An opaque far end names another tracker, so it is never this board's own.
11260 let same_source = edge.to.designator().is_none()
11261 && edge
11262 .to
11263 .source()
11264 .is_none_or(|source| source == self.name.as_str());
11265 // A qualified id's source segment runs to its *first* colon — `GlobalId` and
11266 // `DependencyEndpoint::source` both read it that way — and a native id may hold
11267 // colons of its own, so the far end is everything after that one separator.
11268 // Splitting at the last would truncate `work:urn:task:7` to `7`.
11269 let far_id = if edge.to.is_qualified() {
11270 edge.to
11271 .id()
11272 .split_once(':')
11273 .map_or(edge.to.id(), |(_, native)| native)
11274 } else {
11275 edge.to.id()
11276 };
11277 // One that already blocks the near issue was answered by that issue's own
11278 // read, which carried each of its blockers' kinds — an issue every one — so it
11279 // is not read again.
11280 let blocking = carried.and_then(|nodes| {
11281 nodes
11282 .iter()
11283 .find(|node| node.get("id").and_then(Value::as_str) == Some(far_id))
11284 });
11285 (edge, far_id, same_source, blocking)
11286 })
11287 .collect();
11288 // Every other same-source far end is read by its own id, exactly as the item it is a
11289 // far end of is: whether this board holds it is that read's answer, never a listing's.
11290 // They are read together, [`DETAIL_BATCH`] to a request, rather than one each.
11291 let mut unread: Vec<NativeId> = Vec::new();
11292 for (_, far_id, same_source, blocking) in &far_ends {
11293 let id = NativeId((*far_id).to_owned());
11294 if *same_source && blocking.is_none() && !unread.contains(&id) {
11295 unread.push(id);
11296 }
11297 }
11298 let read: BTreeMap<NativeId, Option<Resolved>> = unread
11299 .iter()
11300 .cloned()
11301 .zip(self.items_by_ids(&unread).await?)
11302 .collect();
11303 for (edge, far_id, same_source, blocking) in far_ends {
11304 let far = match (same_source, blocking) {
11305 (false, _) => None,
11306 (true, Some(node)) => Some(FarEnd {
11307 kind: if required_str(node, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
11308 BoardKind::Document
11309 } else {
11310 BoardKind::Work(related_kind(node)?)
11311 },
11312 content_kind: ContentKind::Issue,
11313 }),
11314 (true, None) => {
11315 let read = read
11316 .get(&NativeId(far_id.to_owned()))
11317 .cloned()
11318 .flatten()
11319 .ok_or_else(|| SourceError::Refused {
11320 message: format!("GitHub dependency item {far_id} was not found"),
11321 })?;
11322 Some(FarEnd {
11323 kind: read.kind,
11324 content_kind: read.content_kind,
11325 })
11326 }
11327 };
11328 let far = far.as_ref();
11329 // The caller says which kind the far end is, and this board holds the far end
11330 // itself, so a disagreement is settled here rather than stored: recorded, the
11331 // wrong kind would read back as a cross-level edge that never existed; written
11332 // natively, it would name a relationship of a different level than the caller
11333 // asked for.
11334 //
11335 // A far end this board holds as a *document* fails the same comparison and is
11336 // refused by the same sentence: `ItemKind` has no document variant because
11337 // nothing may point at one, so no caller can name it correctly and the refusal
11338 // is the only honest answer.
11339 if let Some(disagreeing) = far.filter(|far| far.kind != BoardKind::Work(edge.to.kind)) {
11340 return Err(SourceError::Refused {
11341 message: format!(
11342 "GitHub dependency item {far_id} is a {} of this board, and this item \
11343 names it as a {}; record the kind it is",
11344 disagreeing.kind.describes(),
11345 edge.to.kind.marker()
11346 ),
11347 });
11348 }
11349 // A draft has neither `blockedBy` nor `blocking`, so no edge of one is native
11350 // however the far end is spelled — and one classified native here would be
11351 // written nowhere at all, because a draft's native reconciliation never runs.
11352 let native_here = near_content == ContentKind::Issue
11353 && far.is_some_and(|far| {
11354 far.content_kind == ContentKind::Issue
11355 && BoardKind::Work(edge.to.kind) == near_kind
11356 });
11357 if native_here {
11358 native.push(far_id.to_owned());
11359 } else {
11360 fallback.push(edge.clone());
11361 }
11362 }
11363 Ok((native, fallback))
11364 }
11365
11366 async fn update_existing(
11367 &self,
11368 item: &Resolved,
11369 incoming: &Incoming<'_>,
11370 body: &Option<String>,
11371 status_target: Option<&StatusTarget>,
11372 ) -> Result<(), SourceError> {
11373 let title = incoming.written_title();
11374 // A terminal status closes the issue here, in the same mutation as its body: its board
11375 // option was selected before this, so a close never lands on an item whose board cannot
11376 // show it.
11377 let mut fields = match item.content_kind {
11378 ContentKind::DraftIssue => json!({"title":title,"body":body}),
11379 ContentKind::Issue => json!({"title":title,"body":body,
11380 "stateInput":state_input(status_target)}),
11381 };
11382 // Only the members whose value differs from the item as this write read it are sent,
11383 // and nothing at all when none does — whether or not the store's cache expects a
11384 // version of it. A member left out is one GitHub leaves as it is, so a person's edit
11385 // of it since that read is kept rather than overwritten with what it already held,
11386 // and a re-copy of an unchanged item costs no mutation.
11387 if let Some(members) = fields.as_object_mut() {
11388 if members.get("title").and_then(Value::as_str) == Some(item.title.as_str()) {
11389 members.remove("title");
11390 }
11391 let body_unchanged = match (members.get("body"), item.raw_body.as_deref()) {
11392 (Some(Value::String(sent)), Some(held)) => sent == held,
11393 (Some(Value::Null), held) => held.is_none_or(str::is_empty),
11394 _ => false,
11395 };
11396 if body_unchanged {
11397 members.remove("body");
11398 }
11399 // An issue already in the state the target asks for keeps it: an open one stays
11400 // open, and a closed one whose closure reads as the target's category keeps its
11401 // reason — a duplicate's says more than this write would.
11402 let unchanged_state = match status_target {
11403 Some(StatusTarget::Terminal(_, reason)) => {
11404 item.closed
11405 && closed_category(item.reason.as_deref())
11406 == closed_category(Some(reason.reason()))
11407 }
11408 Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => !item.closed,
11409 None => true,
11410 };
11411 if unchanged_state {
11412 members.remove("stateInput");
11413 }
11414 if members.is_empty() {
11415 return Ok(());
11416 }
11417 }
11418 self.update_content(item.content_kind, &item.id, fields)
11419 .await
11420 }
11421
11422 /// Update one board item's content with exactly `fields` beside its id, through the
11423 /// mutation its kind takes: `updateIssue` for an issue, `updateProjectV2DraftIssue` for
11424 /// a draft.
11425 ///
11426 /// Every input field either mutation leaves out is a field GitHub leaves as it is, which
11427 /// is what lets a narrow write carry the one thing it changes and nothing else.
11428 async fn update_content(
11429 &self,
11430 kind: ContentKind,
11431 id: &NativeId,
11432 fields: Value,
11433 ) -> Result<(), SourceError> {
11434 let (operation, id_key, pointer) = match kind {
11435 ContentKind::DraftIssue => (
11436 graphql::UPDATE_DRAFT,
11437 "draftIssueId",
11438 "/updateProjectV2DraftIssue/draftIssue",
11439 ),
11440 ContentKind::Issue => (graphql::UPDATE_ISSUE, "id", "/updateIssue/issue"),
11441 };
11442 if kind == ContentKind::Issue && self.hold_update(id, &fields)? {
11443 return Ok(());
11444 }
11445 let mut input = fields;
11446 input[id_key] = json!(id.0);
11447 self.send_update(operation, pointer, id, input).await
11448 }
11449
11450 /// Send one item's update, `input`, as `operation`, and hold GitHub's answer at `pointer`
11451 /// to naming that item, `id`.
11452 async fn send_update(
11453 &self,
11454 operation: &str,
11455 pointer: &str,
11456 id: &NativeId,
11457 input: Value,
11458 ) -> Result<(), SourceError> {
11459 let data = self.graphql(operation, json!({"input":input})).await?;
11460 let returned = data
11461 .pointer(pointer)
11462 .ok_or_else(|| SourceError::Malformed {
11463 message: "GitHub item update returned no item".into(),
11464 })?;
11465 if required_str(returned, "id")? != id.0 {
11466 return Err(SourceError::Malformed {
11467 message: "GitHub item update returned the wrong item".into(),
11468 });
11469 }
11470 Ok(())
11471 }
11472
11473 /// Creates one issue, files it on the board, and reports what a read of it would say:
11474 /// its content id, its board item id, and the web address GitHub gave it.
11475 ///
11476 /// Two calls rather than one: `createIssue` answers with an issue that is on no board,
11477 /// and `addProjectV2ItemById` is what puts it there. Filing it at creation through
11478 /// `CreateIssueInput.projectV2Ids` was tried and is not done: GitHub answered with no
11479 /// board item, and the `addProjectV2ItemById` that then had to follow was refused
11480 /// "Content already exists in this project". A terminal status is not written here:
11481 /// `finish_write` selects its option first and closes the issue after, so a close never
11482 /// lands on an item whose board cannot show it.
11483 ///
11484 /// The address and the number come back here because this is the only place either is
11485 /// known before GitHub's own board read catches up — an item this run created answers
11486 /// the reads that follow it out of the record below, and one remembered without them
11487 /// would report no location and no key for the rest of the run.
11488 async fn create_and_file_issue(
11489 &self,
11490 board_id: &str,
11491 repository: &RepositoryTarget,
11492 incoming: &Incoming<'_>,
11493 body: &Option<String>,
11494 ) -> Result<Landed, SourceError> {
11495 let repository_id = self.repository_id(repository, incoming).await?;
11496 let data = self
11497 .graphql(
11498 graphql::CREATE_ISSUE,
11499 json!({"input":{
11500 "repositoryId":repository_id,"title":incoming.written_title(),"body":body
11501 }}),
11502 )
11503 .await?;
11504 let created = data
11505 .pointer("/createIssue/issue")
11506 .filter(|value| !value.is_null())
11507 .ok_or_else(|| SourceError::Malformed {
11508 message: "GitHub issue creation returned no issue".into(),
11509 })?;
11510 let content_id = NativeId(required_str(created, "id")?.to_owned());
11511 // Optional although GitHub's schema makes it non-null: the issue exists by now, so
11512 // a response without it is not worth failing a landed write over — the item simply
11513 // reports no location until the board read catches up, which is what it did before.
11514 let url = optional_str(created, "url")?.map(str::to_owned);
11515 // The issue exists from here on, so an unreadable number and a refused board
11516 // filing below each try, best effort, to take it back: an issue in the repository
11517 // that is on no board is an item nobody asked for and nothing here would find again.
11518 //
11519 // Its number is optional on the same terms its address is — a landed write is not
11520 // worth failing over a member that came back missing, and such an item reports no
11521 // handle until a board read catches up. A number that is *present* and is not an
11522 // unsigned integer is still a response this source cannot read.
11523 let number = match created_issue_number(created) {
11524 Ok(number) => number,
11525 Err(error) => {
11526 let _ = self.delete_issue(&content_id).await;
11527 return Err(error);
11528 }
11529 };
11530 let added = match self
11531 .graphql(
11532 graphql::ADD_TO_BOARD,
11533 json!({"input":{"projectId":board_id,"contentId":content_id.0}}),
11534 )
11535 .await
11536 {
11537 Ok(added) => added,
11538 Err(error) => {
11539 let _ = self.delete_issue(&content_id).await;
11540 return Err(error);
11541 }
11542 };
11543 let item = added
11544 .pointer("/addProjectV2ItemById/item")
11545 .filter(|value| !value.is_null())
11546 .ok_or_else(|| SourceError::Malformed {
11547 message: "GitHub board addition returned no project item".into(),
11548 })?;
11549 self.note_written(&content_id, url.as_deref());
11550 Ok(Landed {
11551 content_id,
11552 item_id: Some(required_str(item, "id")?.to_owned()),
11553 url,
11554 number,
11555 })
11556 }
11557
11558 /// Move one issue under the project it now belongs to, or out of the one it left.
11559 async fn reparent(
11560 &self,
11561 held: Option<NativeId>,
11562 child: &NativeId,
11563 wanted: Option<&NativeId>,
11564 ) -> Result<(), SourceError> {
11565 if held.as_ref() == wanted {
11566 return Ok(());
11567 }
11568 if let Some(held) = &held {
11569 self.sub_issue(graphql::REMOVE_SUB_ISSUE, held, child, "removeSubIssue")
11570 .await?;
11571 }
11572 if let Some(wanted) = wanted {
11573 self.sub_issue(graphql::ADD_SUB_ISSUE, wanted, child, "addSubIssue")
11574 .await?;
11575 }
11576 Ok(())
11577 }
11578
11579 async fn sub_issue(
11580 &self,
11581 operation: &str,
11582 parent: &NativeId,
11583 child: &NativeId,
11584 root: &str,
11585 ) -> Result<(), SourceError> {
11586 let data = self
11587 .graphql(
11588 operation,
11589 json!({"input":{"issueId":parent.0,"subIssueId":child.0}}),
11590 )
11591 .await?;
11592 let issue =
11593 data.pointer(&format!("/{root}/issue"))
11594 .ok_or_else(|| SourceError::Malformed {
11595 message: "GitHub sub-issue update returned no issue".into(),
11596 })?;
11597 let sub =
11598 data.pointer(&format!("/{root}/subIssue"))
11599 .ok_or_else(|| SourceError::Malformed {
11600 message: "GitHub sub-issue update returned no sub-issue".into(),
11601 })?;
11602 if required_str(issue, "id")? != parent.0 || required_str(sub, "id")? != child.0 {
11603 return Err(SourceError::Malformed {
11604 message: "GitHub sub-issue update returned the wrong issues".into(),
11605 });
11606 }
11607 Ok(())
11608 }
11609
11610 /// Bring one issue's `blockedBy` to exactly `native`, sending only the difference, and say
11611 /// whether there was one.
11612 ///
11613 /// An issue [`Issue::Created`] by this very write is blocked by nothing yet, so its
11614 /// relationships are not read: there is nothing a read of them could find.
11615 async fn reconcile_blocked_by(
11616 &self,
11617 content_id: &NativeId,
11618 native: &[String],
11619 issue: Issue<'_>,
11620 ) -> Result<bool, SourceError> {
11621 let current = match issue {
11622 Issue::Created => Vec::new(),
11623 Issue::Existing(Some(held)) => held
11624 .iter()
11625 .map(|far| required_str(far, "id").map(str::to_owned))
11626 .collect::<Result<Vec<_>, _>>()?,
11627 Issue::Existing(None) => self.native_dependency_ids(content_id).await?,
11628 };
11629 let mut changed = false;
11630 for (operation, far_id) in current
11631 .iter()
11632 .filter(|id| !native.contains(id))
11633 .map(|id| (graphql::REMOVE_BLOCKED_BY, id))
11634 .chain(
11635 native
11636 .iter()
11637 .filter(|id| !current.contains(id))
11638 .map(|id| (graphql::ADD_BLOCKED_BY, id)),
11639 )
11640 {
11641 let data = self
11642 .graphql(
11643 operation,
11644 json!({"input":{"issueId":content_id.0,"blockingIssueId":far_id}}),
11645 )
11646 .await?;
11647 let root = if operation == graphql::ADD_BLOCKED_BY {
11648 "addBlockedBy"
11649 } else {
11650 "removeBlockedBy"
11651 };
11652 let issue =
11653 data.pointer(&format!("/{root}/issue"))
11654 .ok_or_else(|| SourceError::Malformed {
11655 message: "GitHub dependency update returned no issue".into(),
11656 })?;
11657 let blocker = data
11658 .pointer(&format!("/{root}/blockingIssue"))
11659 .ok_or_else(|| SourceError::Malformed {
11660 message: "GitHub dependency update returned no blocking issue".into(),
11661 })?;
11662 if required_str(issue, "id")? != content_id.0 || required_str(blocker, "id")? != far_id
11663 {
11664 return Err(SourceError::Malformed {
11665 message: "GitHub dependency update returned the wrong issues".into(),
11666 });
11667 }
11668 changed = true;
11669 }
11670 Ok(changed)
11671 }
11672}
11673
11674/// The board field values one write sends, in the order GitHub runs them, and the fields it
11675/// clears after them: the origin field when it moves, the `Status` option `column` names, the
11676/// priority, then each projected field.
11677fn field_writes<'a>(
11678 existing: Option<&Resolved>,
11679 origin_field: Option<&str>,
11680 origin: &str,
11681 column: Option<(String, String)>,
11682 priority: Option<&'a PriorityWrite>,
11683 projections: &'a [ProjectionWrite],
11684) -> (Vec<(String, Value)>, Vec<&'a str>) {
11685 let mut fields = Vec::new();
11686 if let Some(field_id) = origin_field
11687 && existing.map_or(!origin.is_empty(), |item| {
11688 item.origin.as_deref().unwrap_or("") != origin
11689 })
11690 {
11691 fields.push((field_id.to_owned(), json!({"text":origin})));
11692 }
11693 if let Some((field_id, option_id)) = column {
11694 fields.push((field_id, json!({"singleSelectOptionId":option_id})));
11695 }
11696 let mut clears = Vec::new();
11697 match priority {
11698 Some(PriorityWrite::Select { field, option }) => {
11699 fields.push((field.clone(), json!({"singleSelectOptionId":option})));
11700 }
11701 Some(PriorityWrite::Clear { field }) => clears.push(field.as_str()),
11702 None => {}
11703 }
11704 for projection in projections {
11705 projection.join(&mut fields, &mut clears);
11706 }
11707 (fields, clears)
11708}
11709
11710/// Everything one write decided before sending anything: see
11711/// [`GitHubProjectsSource::prepare_write`].
11712struct Prepared {
11713 /// What the write lands on.
11714 destination: WriteDestination,
11715 board: BoardFields,
11716 status_target: Option<StatusTarget>,
11717 /// The `Status` field, the option and the option's name the status lands on.
11718 // 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.
11719 column: Option<(String, String, String)>,
11720 priority_write: Option<PriorityWrite>,
11721 projections: Vec<ProjectionWrite>,
11722 projected: BTreeMap<String, String>,
11723 content_kind: ContentKind,
11724 own_repository: Option<Repository>,
11725 /// The far ends of the item's native blocked-by relationships.
11726 // 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.
11727 native: Vec<String>,
11728 slot: BTreeMap<String, Value>,
11729 content: Option<String>,
11730 body: Option<String>,
11731 /// The copy origin the item records, empty for none.
11732 // 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.
11733 origin: String,
11734 origin_field: Option<String>,
11735}
11736
11737/// One write a copy held back of the kinds sent together: a board field value, or a blocked-by
11738/// edge, each its input.
11739enum HeldWrite<'a> {
11740 Field(&'a Value),
11741 Edge(&'a Value),
11742}
11743
11744/// Whether `error` is GitHub refusing to file an issue on a board because it is filed there
11745/// already — what `addProjectV2ItemById` answers once `createIssue`'s `projectV2Ids` has placed
11746/// it, rather than answering with the item it holds.
11747fn already_on_the_board(error: &SourceError) -> bool {
11748 error
11749 .to_string()
11750 .contains("Content already exists in this project")
11751}
11752
11753/// Hold every alias `inputs` sent under `prefix` in `data` to answering with what its input
11754/// named — each `(root, named)` of `answers` — and refuse one that answered nothing or another
11755/// item.
11756fn check_aliased_answers(
11757 document: &str,
11758 data: &Value,
11759 prefix: &str,
11760 inputs: &[&Value],
11761 answers: &[(&str, &str)],
11762) -> Result<(), SourceError> {
11763 for (slot, input) in inputs.iter().enumerate() {
11764 for (root, named) in answers {
11765 let answered = data
11766 .pointer(&format!("/{prefix}{slot}/{root}/id"))
11767 .and_then(Value::as_str)
11768 .ok_or_else(|| SourceError::Malformed {
11769 message: format!(
11770 "GitHub answered {} with nothing at {prefix}{slot}",
11771 operation_description(document)
11772 ),
11773 })?;
11774 let asked = input.get(named).and_then(Value::as_str).unwrap_or_default();
11775 if answered != asked {
11776 return Err(SourceError::Malformed {
11777 message: format!(
11778 "GitHub answered {} at {prefix}{slot} with {answered}, which is not \
11779 {asked}, the item it was asked to write",
11780 operation_description(document)
11781 ),
11782 });
11783 }
11784 }
11785 }
11786 Ok(())
11787}
11788
11789/// `error`, which stopped a batch of creates, and — when GitHub refused to take any of them
11790/// back — each issue of `left` it created that is still in the repository, which its caller,
11791/// never handed their ids, could not name.
11792fn left_behind(error: SourceError, left: &[&str]) -> SourceError {
11793 if left.is_empty() {
11794 return error;
11795 }
11796 SourceError::Refused {
11797 message: format!(
11798 "{error}; GitHub then refused to take back {}, which this write created and which \
11799 are still in the repository; next: delete {} before writing again",
11800 left.join(", "),
11801 if left.len() == 1 { "it" } else { "them" }
11802 ),
11803 }
11804}
11805
11806/// What one write lands on: the item it updates, as it stood, or where it creates a new one.
11807enum WriteDestination {
11808 /// The item as it stood, for an update.
11809 Existing(Box<Resolved>),
11810 /// Where a new item is created.
11811 New(RepositoryTarget),
11812}
11813
11814impl WriteDestination {
11815 /// The item an update writes over, as it stood; `None` for a create.
11816 fn existing(&self) -> Option<&Resolved> {
11817 match self {
11818 Self::Existing(item) => Some(item),
11819 Self::New(_) => None,
11820 }
11821 }
11822}
11823
11824/// One existing issue's update held back by [`GitHubProjectsSource::hold_update`]: the issue,
11825/// and the `UpdateIssueInput` its own [`graphql::UPDATE_ISSUE`] would have sent — the members
11826/// it writes beside that issue's id, which only [`Self::new`] sets.
11827struct HeldUpdate {
11828 issue: NativeId,
11829 // 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.
11830 input: Value,
11831}
11832
11833impl HeldUpdate {
11834 /// The update writing `members` — an `UpdateIssueInput` without its id — to `issue`.
11835 fn new(issue: &NativeId, members: &Value) -> Self {
11836 let mut input = members.clone();
11837 input["id"] = json!(issue.0);
11838 Self {
11839 issue: issue.clone(),
11840 input,
11841 }
11842 }
11843}
11844
11845/// What a copy has held back of the items it created; see
11846/// [`GitHubProjectsSource::hold_back`].
11847#[derive(Debug, Default)]
11848enum Deferred {
11849 /// Writes are being held back, and nothing has been held yet.
11850 #[default]
11851 Nothing,
11852 /// The items held back, every one of them on `board`.
11853 Held {
11854 /// The board every one of them is on.
11855 board: BoardId,
11856 /// Each created item, in the order it was created.
11857 created: Vec<HeldBack>,
11858 },
11859}
11860
11861impl Deferred {
11862 /// Whether `content` is one of the items held back.
11863 fn holds(&self, content: &NativeId) -> bool {
11864 match self {
11865 Self::Nothing => false,
11866 Self::Held { created, .. } => created.iter().any(|held| held.content == *content),
11867 }
11868 }
11869}
11870
11871/// One created item's parts that land once [`TaskSource::finish_writes`] is called.
11872#[derive(Debug)]
11873struct HeldBack {
11874 /// The issue.
11875 content: NativeId,
11876 /// Its board field values other than its `Status`, in the order they are written.
11877 fields: Vec<(String, Value)>,
11878 /// The `Status` field, and the option and its name that the item's status lands on.
11879 // llmlint: ignore[invalid_states_unrepresentable] The same triple `column_for` returns and `Prepared::column` carries, held back unchanged until it is sent.
11880 status: Option<(String, String, String)>,
11881 /// The fields it clears after those are written.
11882 clears: Vec<String>,
11883 /// The reason it is closed for, once its option is selected; `None` leaves it open.
11884 close: Option<ClosedState>,
11885 /// The issues it is blocked by.
11886 edges: Vec<NativeId>,
11887}
11888
11889/// The member [`answer_with_errors`] carries a response's GraphQL errors under, beside its data:
11890/// no GitHub type has a field of this name.
11891const ANSWER_ERRORS: &str = "onetaskgraphErrors";
11892
11893/// A response's `data` with its GraphQL errors beside it under [`ANSWER_ERRORS`], when it has
11894/// both — or `None`, for an answer [`GitHubProjectsSource::answer`] reads as it always has.
11895///
11896/// What [`graphql::CREATE_ISSUES`] is answered with: GitHub runs a document's mutation fields in
11897/// order and answers each one that ran, so a refusal of one alias leaves the issues the others
11898/// created named in the data beside it — the ids a copy needs to take them back.
11899fn answer_with_errors(body: &str) -> Option<Value> {
11900 let parsed: Value = serde_json::from_str(body).ok()?;
11901 let errors = parsed
11902 .get("errors")?
11903 .as_array()
11904 .filter(|errors| !errors.is_empty())?;
11905 let mut data = parsed.get("data").filter(|data| data.is_object())?.clone();
11906 data[ANSWER_ERRORS] = Value::Array(errors.clone());
11907 Some(data)
11908}
11909
11910/// The `createIssue` input that files the new issue on `board` and, when `incoming` names one,
11911/// under its project — see [`GitHubProjectsSource::create_filed_issue`].
11912fn filed_input(
11913 repository: &str,
11914 board: &str,
11915 incoming: &Incoming<'_>,
11916 body: &Option<String>,
11917) -> Value {
11918 let mut input = json!({"repositoryId":repository,"title":incoming.written_title(),
11919 "body":body,"projectV2Ids":[board]});
11920 if let Some(parent) = incoming.parent {
11921 input["parentIssueId"] = json!(parent.0);
11922 }
11923 input
11924}
11925
11926/// The name of option `option` of field `field` among a board's `fields`.
11927fn option_name<'a>(fields: &'a Value, field: &str, option: &str) -> Option<&'a str> {
11928 fields
11929 .get("nodes")?
11930 .as_array()?
11931 .iter()
11932 .find(|node| node.get("id").and_then(Value::as_str) == Some(field))?
11933 .get("options")?
11934 .as_array()?
11935 .iter()
11936 .find(|candidate| candidate.get("id").and_then(Value::as_str) == Some(option))?
11937 .get("name")?
11938 .as_str()
11939}
11940
11941/// What a write needs to know of one far end it names: which kind of item it is, and whether
11942/// it is an issue a native relationship can name.
11943struct FarEnd {
11944 kind: BoardKind,
11945 content_kind: ContentKind,
11946}
11947
11948/// Whether the issue one write reconciles was created by that write or was already there.
11949#[derive(Clone, Copy, PartialEq, Eq)]
11950enum Issue<'a> {
11951 /// Created by this write, so it holds no relationships yet.
11952 Created,
11953 /// On the board before this write, holding whatever relationships it holds — the far
11954 /// ends of its whole `blockedBy`, when the read that reached it carried them.
11955 Existing(Option<&'a [Value]>),
11956}
11957
11958/// What resolving one node id reached; see [`GitHubProjectsSource::reach`].
11959enum Reached {
11960 /// An issue this board holds, resolved into everything this source reports about it.
11961 Held(Box<Resolved>),
11962 /// Nothing this board holds: no such node, or a node on some other board.
11963 Nothing,
11964 /// A board draft, which [`graphql::ISSUE`] reaches and reads nothing of, so it is read
11965 /// again by [`GitHubProjectsSource::draft_by_id`].
11966 Draft,
11967}
11968
11969/// What GitHub says when a string is not a node id it can resolve.
11970///
11971/// Matched because it is the ordinary answer to a project selector naming a project by its
11972/// *name*, and reporting that as a failure would make naming one impossible. It is read
11973/// off the refusal GitHub sent, never guessed from the shape of the string: this source
11974/// does not define the syntax of a GitHub node id and would be wrong about it.
11975const UNRESOLVABLE_NODE: &str = "could not resolve to a node";
11976
11977/// `error` with `note` added to the end of what it says, its kind and every other member
11978/// unchanged — so a caller still branches on the failure that happened, and reads beside it
11979/// what that failure left behind.
11980fn noting(error: SourceError, note: &str) -> SourceError {
11981 match error {
11982 SourceError::Config { message } => SourceError::Config {
11983 message: message + note,
11984 },
11985 SourceError::Auth { message } => SourceError::Auth {
11986 message: message + note,
11987 },
11988 SourceError::Refused { message } => SourceError::Refused {
11989 message: message + note,
11990 },
11991 SourceError::RateLimited {
11992 retry_after_seconds,
11993 message,
11994 } => SourceError::RateLimited {
11995 retry_after_seconds,
11996 message: Some(message.unwrap_or_default() + note),
11997 },
11998 SourceError::Unavailable { message } => SourceError::Unavailable {
11999 message: message + note,
12000 },
12001 SourceError::Malformed { message } => SourceError::Malformed {
12002 message: message + note,
12003 },
12004 }
12005}
12006
12007/// The variables of one [`graphql::ISSUE_COPY_DETAILS`] request, as [`detail_batch`] binds its
12008/// own: a slot `batch` has no id for is bound to its last id, read again at no added price.
12009fn copy_detail_batch(batch: &[NativeId], comments: &PageRequest, children: bool) -> Value {
12010 let mut variables = serde_json::Map::new();
12011 for slot in 0..COPY_DETAIL_BATCH {
12012 let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
12013 variables.insert(format!("id{slot}"), json!(id));
12014 }
12015 variables.insert("project".to_owned(), json!(batch.first().map(|id| &id.0)));
12016 variables.insert("children".to_owned(), json!(children));
12017 variables.insert("first".to_owned(), json!(comments.limit.min(MAX_PAGE_SIZE)));
12018 variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
12019 variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
12020 variables.insert("duplicates".to_owned(), json!(true));
12021 Value::Object(variables)
12022}
12023
12024/// The variables of one [`graphql::ISSUE_DETAILS`] request over `batch` — at most
12025/// [`DETAIL_BATCH`] ids — each item with the first page of its comments when `comments` asks
12026/// for them.
12027///
12028/// The document is fixed-size, so a slot `batch` has no id for is bound to its last id, which
12029/// is read again at no added price.
12030fn detail_batch(batch: &[NativeId], comments: Option<&PageRequest>) -> Value {
12031 let mut variables = serde_json::Map::new();
12032 for slot in 0..DETAIL_BATCH {
12033 let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
12034 variables.insert(format!("id{slot}"), json!(id));
12035 }
12036 variables.insert(
12037 "first".to_owned(),
12038 json!(comments.map_or(MAX_PAGE_SIZE, |page| page.limit.min(MAX_PAGE_SIZE))),
12039 );
12040 variables.insert("comments".to_owned(), json!(comments.is_some()));
12041 variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
12042 variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
12043 variables.insert("duplicates".to_owned(), json!(true));
12044 Value::Object(variables)
12045}
12046
12047/// Whether this refusal is GitHub saying the id names no node at all.
12048fn unresolvable_node(error: &SourceError) -> bool {
12049 matches!(error, SourceError::Refused { message }
12050 if message.to_ascii_lowercase().contains(UNRESOLVABLE_NODE))
12051}
12052
12053/// One project name, as a search qualifier which filters on it at the server.
12054///
12055/// Quoted so the whole title is one phrase rather than a bag of words, with the two
12056/// characters GitHub's own quoting grammar gives a meaning inside a quoted phrase escaped
12057/// the way it documents. A title matched here is still compared for equality afterwards:
12058/// the qualifier narrows what the server sends, and this source decides what it names.
12059fn title_qualifier(name: &str) -> String {
12060 format!("in:title {}", quoted(name))
12061}
12062
12063/// `value` as one quoted phrase of a GitHub search or a board filter, with the two
12064/// characters GitHub's quoting grammar gives a meaning inside a quoted phrase escaped the way
12065/// it documents — so a value holding a qualifier's spelling is searched for rather than
12066/// obeyed.
12067fn quoted(value: &str) -> String {
12068 let escaped = value.replace('\\', "\\\\").replace('"', "\\\"");
12069 format!("\"{escaped}\"")
12070}
12071
12072/// The search qualifier for the issues updated at or after `since`.
12073///
12074/// Written to the second, rounded down, which can only widen what the search returns.
12075fn updated_qualifier(since: DateTime<Utc>) -> String {
12076 format!("updated:>={}", since.format("%Y-%m-%dT%H:%M:%S+00:00"))
12077}
12078
12079/// The search terms that narrow a board-scoped issue search to a task query's text and
12080/// metadata predicates, or `None` when it carries neither.
12081///
12082/// The text is one quoted phrase, searched `in:title`, `in:body` or both as its fields say,
12083/// and each metadata value is one more quoted phrase, which GitHub finds in the body because
12084/// its index covers the metadata comment the value is stored in. GitHub ANDs the phrases and
12085/// matches each in any field the `in:` qualifier names, so a query naming a title search and
12086/// a metadata value searches both fields for both — wider than asked, never narrower, and
12087/// every candidate is confirmed in process afterwards.
12088///
12089/// **This narrows a text search, and that is this source's declared semantics.** GitHub
12090/// matches whole tokens where a substring rule would match inside a word, so an item holding
12091/// the text only inside a longer word is not returned. A text of nothing but whitespace
12092/// matches every item, so it narrows nothing and is not sent.
12093fn narrowing_qualifiers(query: &TaskQuery) -> Option<String> {
12094 let text = query
12095 .text
12096 .as_ref()
12097 .filter(|text| !text.terms.trim().is_empty());
12098 if text.is_none() && query.metadata.is_empty() {
12099 return None;
12100 }
12101 let (title, body) = match text.map(|text| text.fields) {
12102 None => (false, true),
12103 Some(TextFields::Title) => (true, !query.metadata.is_empty()),
12104 Some(TextFields::Content) => (false, true),
12105 Some(TextFields::TitleOrContent) => (true, true),
12106 };
12107 let fields = match (title, body) {
12108 (true, true) => "in:title,body",
12109 (true, false) => "in:title",
12110 _ => "in:body",
12111 };
12112 let phrases = text
12113 .map(|text| text.terms.clone())
12114 .into_iter()
12115 .chain(
12116 query
12117 .metadata
12118 .iter()
12119 .map(|wanted| as_stored(wanted.value())),
12120 )
12121 .map(|phrase| quoted(&phrase))
12122 .collect::<Vec<_>>();
12123 Some(format!("{fields} {}", phrases.join(" ")))
12124}
12125
12126/// The search terms that narrow a board-scoped issue search to a project or document query's
12127/// text, or `None` when it has none or a blank one: the phrase, in the fields, a task query
12128/// carrying that text alone is sent as by [`narrowing_qualifiers`].
12129fn text_qualifiers(text: Option<&TextQuery>) -> Option<String> {
12130 narrowing_qualifiers(&TaskQuery {
12131 text: text.cloned(),
12132 ..TaskQuery::default()
12133 })
12134}
12135
12136/// Refuses a project or document query's text GitHub's issue search cannot find, before
12137/// anything is asked of GitHub, on exactly the terms [`refuse_unsearchable`] refuses a task
12138/// query's.
12139fn refuse_unsearchable_text(text: Option<&TextQuery>) -> Result<(), SourceError> {
12140 refuse_unsearchable(&TaskQuery {
12141 text: text.cloned(),
12142 ..TaskQuery::default()
12143 })
12144}
12145
12146/// Refuses a task query naming a text or a metadata value GitHub's issue search cannot find,
12147/// before anything is asked of GitHub.
12148///
12149/// GitHub's index holds words, so a phrase with no letter or digit names none to find, and no
12150/// bounded query answers it: sent, GitHub's answer to it is nothing this source may rely on;
12151/// left out, the search is every issue of the board. So this source says it cannot answer
12152/// rather than reading the board or answering nothing. A blank text is not refused: it narrows
12153/// nothing GitHub could search for, and keeps the board read it always had.
12154fn refuse_unsearchable(query: &TaskQuery) -> Result<(), SourceError> {
12155 const WHY: &str = "GitHub's issue search indexes words, so it cannot answer a value with no \
12156 letter or digit with a bounded query";
12157 if let Some(text) = &query.text
12158 && !text.terms.trim().is_empty()
12159 && !has_words(&text.terms)
12160 {
12161 return Err(SourceError::Refused {
12162 message: format!(
12163 "cannot search for the text {:?}: {WHY}; search for a text holding a letter or a digit",
12164 text.terms
12165 ),
12166 });
12167 }
12168 if let Some(wanted) = query
12169 .metadata
12170 .iter()
12171 .find(|wanted| !has_words(wanted.value()))
12172 {
12173 return Err(SourceError::Refused {
12174 message: format!(
12175 "cannot filter by the metadata value {:?} at {:?}: {WHY}; filter by a value holding a letter or a digit",
12176 wanted.value(),
12177 std::iter::once(wanted.key())
12178 .chain(wanted.path().iter().map(String::as_str))
12179 .collect::<Vec<_>>()
12180 .join("/"),
12181 ),
12182 });
12183 }
12184 Ok(())
12185}
12186
12187/// Whether GitHub's index could hold a word of `phrase`: whether it has a letter or a digit.
12188fn has_words(phrase: &str) -> bool {
12189 phrase.chars().any(char::is_alphanumeric)
12190}
12191
12192/// `value` spelled the way the metadata slot stores it: as the inside of its JSON string.
12193///
12194/// What GitHub indexes is the slot's JSON text, so a value holding a character JSON escapes —
12195/// a newline, a tab, a quote — is found by the escape the body holds and not by the character,
12196/// which GitHub's word match would read as different words.
12197fn as_stored(value: &str) -> String {
12198 let encoded = Value::String(value.to_owned()).to_string();
12199 encoded[1..encoded.len() - 1].to_owned()
12200}
12201
12202/// The one narrower question a task query carrying a text, metadata or origin predicate is
12203/// sent as.
12204enum Narrowing {
12205 /// Every carrier of this origin: [`graphql::ORIGIN_LOOKUP`].
12206 Origin(String),
12207 /// The board-scoped issue search narrowed by these qualifiers.
12208 Search(String),
12209}
12210
12211impl Narrowing {
12212 /// What this question is remembered under for the length of one command.
12213 fn key(&self) -> String {
12214 match self {
12215 Self::Origin(origin) => format!("origin {origin}"),
12216 Self::Search(also) => format!("search {also}"),
12217 }
12218 }
12219}
12220
12221/// Where one connection of [`graphql::ORIGIN_LOOKUP`] resumes.
12222enum Resumed {
12223 /// It reported another page, which starts after this cursor.
12224 More(String),
12225 /// It has ended. Sending this cursor again — the page's own end when it had one, and
12226 /// otherwise the cursor it was reached from — answers an empty page, so the one document
12227 /// can go on walking the other connection.
12228 Ended(Option<String>),
12229}
12230
12231impl Resumed {
12232 /// Whether the connection has another page.
12233 const fn has_more(&self) -> bool {
12234 matches!(self, Self::More(_))
12235 }
12236
12237 /// The cursor to send this connection next.
12238 fn cursor(self) -> Option<String> {
12239 match self {
12240 Self::More(next) => Some(next),
12241 Self::Ended(last) => last,
12242 }
12243 }
12244}
12245
12246/// Where `connection`, reached from `after`, resumes — refused when it reports another page
12247/// with no cursor to it, or from a cursor that does not advance.
12248fn resumed(connection: &Value, after: Option<&str>) -> Result<Resumed, SourceError> {
12249 let info = connection
12250 .get("pageInfo")
12251 .ok_or_else(|| SourceError::Malformed {
12252 message: "GitHub connection has no pageInfo".into(),
12253 })?;
12254 let end = optional_str(info, "endCursor")?;
12255 if required_bool(info, "hasNextPage")? {
12256 let next = end.ok_or_else(|| SourceError::Malformed {
12257 message: "GitHub connection reports another page and no endCursor".into(),
12258 })?;
12259 validate_cursor_progress(after, next)?;
12260 return Ok(Resumed::More(next.to_owned()));
12261 }
12262 Ok(Resumed::Ended(
12263 end.map(str::to_owned).or_else(|| after.map(str::to_owned)),
12264 ))
12265}
12266
12267/// The board, and every item on it this source reports.
12268#[derive(Clone)]
12269struct Board {
12270 id: String,
12271 fields: Value,
12272 items: Vec<Resolved>,
12273}
12274
12275/// What a write needs of the board and nothing more: its node id and its field
12276/// definitions, in the shape a read of the board's own `fields` gives them.
12277///
12278/// Deliberately no items. A write decides which item it writes, which parent it files
12279/// under and which far ends it names by reading each of them by its own id; this is the
12280/// half of the board those reads cannot carry, and holding no item is what keeps it from
12281/// ever being asked whether an item is there.
12282#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
12283struct BoardFields {
12284 id: BoardId,
12285 fields: Value,
12286}
12287
12288/// A board's node id: what a field write and `addProjectV2ItemById` address.
12289///
12290/// Never blank, because a blank one addresses no board — so an id GitHub answers blank is
12291/// refused where it is read, and one an item names blank is read as not named at all.
12292#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
12293#[serde(try_from = "String")]
12294struct BoardId(String);
12295
12296impl TryFrom<String> for BoardId {
12297 type Error = String;
12298
12299 /// The same refusal [`BoardId::parse`] makes, for an id read back from a kept context.
12300 fn try_from(id: String) -> Result<Self, Self::Error> {
12301 Self::parse(&id).map_err(|error| error.to_string())
12302 }
12303}
12304
12305/// Where one write left its item, for the record the rest of the command reads it out of.
12306///
12307/// A named record rather than a tuple because the update arm and the create arm each fill
12308/// all four, and two `Option`s of different meaning side by side in a tuple are two
12309/// positions a reader has to count.
12310struct Landed {
12311 /// The issue's own node id, which is the [`NativeId`] this source reports.
12312 content_id: NativeId,
12313 /// The board item's id, which is what a field write addresses — `None` for an issue its
12314 /// create filed on the board itself, whose board item GitHub adds after answering and
12315 /// [`TaskSource::finish_writes`] reads.
12316 // 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.
12317 item_id: Option<String>,
12318 /// The web address GitHub gave the issue, when it gave one.
12319 // 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.
12320 url: Option<String>,
12321 /// The issue's number on its repository, when GitHub reported one.
12322 number: Option<u64>,
12323}
12324
12325impl BoardId {
12326 fn parse(id: &str) -> Result<Self, SourceError> {
12327 if id.trim().is_empty() {
12328 return Err(SourceError::Malformed {
12329 message: "GitHub named a board with a blank node id".into(),
12330 });
12331 }
12332 Ok(Self(id.to_owned()))
12333 }
12334
12335 fn as_str(&self) -> &str {
12336 &self.0
12337 }
12338}
12339
12340impl Board {
12341 fn field<'a>(fields: &'a Value, name: &str) -> Result<Option<&'a Value>, SourceError> {
12342 complete_connection(fields, "project fields", NESTED_PAGE_SIZE)?;
12343 let nodes = fields
12344 .get("nodes")
12345 .and_then(Value::as_array)
12346 .ok_or_else(|| SourceError::Malformed {
12347 message: "GitHub project fields.nodes is not an array".into(),
12348 })?;
12349 Ok(nodes
12350 .iter()
12351 .find(|field| field.get("name").and_then(Value::as_str) == Some(name)))
12352 }
12353}
12354
12355/// One board item, resolved into everything this source reports about it.
12356///
12357/// Serializable because it is what [`TaskSource::write_context`] hands the store's cache and
12358/// [`TaskSource::prime_write`] takes back — see [`expected`].
12359#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
12360struct Resolved {
12361 item_id: String,
12362 id: NativeId,
12363 content_kind: ContentKind,
12364 kind: BoardKind,
12365 title: String,
12366 body: Option<String>,
12367 /// The body exactly as GitHub holds it, metadata slot and all, which is what a write
12368 /// that changes the slot alone has to keep byte for byte outside it.
12369 raw_body: Option<String>,
12370 status: Status,
12371 /// The name of the board `Status` option this item sits in, as the board spells it.
12372 option: Option<String>,
12373 /// What its `Priority` field says, read through this instance's mapping.
12374 priority: HeldPriority,
12375 /// Whether this item's issue is closed. A draft has no such state and is never closed.
12376 closed: bool,
12377 /// GitHub's reason for a closed issue's closure, as it spells it — `None` for an open
12378 /// issue, a draft, and an issue closed before GitHub recorded reasons.
12379 #[serde(default)]
12380 reason: Option<String>,
12381 /// The tasks this one delivers, read out of its slot. Empty for anything not a task.
12382 delivers: Vec<TaskRef>,
12383 /// Every task that delivers this one, read out of its slot. Empty for anything not a
12384 /// task.
12385 delivered_by: Vec<TaskRef>,
12386 labels: Vec<Label>,
12387 parent: Option<NativeId>,
12388 // 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.
12389 origin: Option<String>,
12390 /// What each board text field this instance projects metadata onto holds for this item,
12391 /// by field name — only the fields holding a value, an empty text included. What a write compares the
12392 /// item's metadata against, so a field already holding its value is not written again;
12393 /// never read back as metadata, which the slot alone reports.
12394 projected: BTreeMap<String, String>,
12395 /// The issue's own number on its repository, as GitHub reports it.
12396 ///
12397 /// `None` in exactly two cases: a draft, which has no number at all — `DraftIssue`
12398 /// declares none, and a draft is not filed in a repository to be numbered by one — and
12399 /// an issue this run created whose creating mutation answered without one, which is a
12400 /// response GitHub's own schema says cannot happen and which a landed write is not
12401 /// worth failing over. An `Issue` read off the board always has one.
12402 number: Option<u64>,
12403 url: Option<String>,
12404 created_at: Option<DateTime<Utc>>,
12405 updated_at: Option<DateTime<Utc>>,
12406 /// When the board item itself last changed — what a board field write moves — when the
12407 /// read that reached it said.
12408 item_updated_at: Option<DateTime<Utc>>,
12409 /// How many comments the issue holds, when the read that reached it said.
12410 comment_count: Option<u64>,
12411 /// How many relationship events the issue's timeline holds — every dependency, as
12412 /// `blockedBy` or `blocking`, and every project membership, as its parent, added or
12413 /// removed — when the read that reached it said.
12414 relationship_events: Option<u64>,
12415 /// The server's clock on the response that read it — its `Date` header — when the
12416 /// response carried one.
12417 served_at: Option<DateTime<Utc>>,
12418 /// The digest of its [`change_token::Facets`] — its own kind, its board field values and
12419 /// its labels — when the read that reached it carried all three whole.
12420 #[serde(default)]
12421 facets: Option<change_token::FacetsDigest>,
12422 own_repository: Option<Repository>,
12423 repositories: Vec<Repository>,
12424 classification: Classification,
12425 slot: BTreeMap<String, Value>,
12426 /// The node id of the board this item sits on, when the read that reached it said.
12427 board_id: Option<String>,
12428 /// The definition of every board field this item holds a value of, in the shape a read
12429 /// of the board's own `fields` gives one.
12430 ///
12431 /// Only the fields this item has a value in: a field it holds nothing of is not here,
12432 /// which says nothing about whether the board has it.
12433 fields: Vec<Value>,
12434 /// Every field the board this item sits on defines, as its own read of the board's
12435 /// `fields` gives them — when the read that reached the item carried them, which a read
12436 /// of it by its own id does. What a write of it needs of the board, then, needs no read
12437 /// of the board.
12438 board_fields: Option<Value>,
12439 /// The far ends of this issue's whole `blockedBy` connection, each as a dependency read
12440 /// selects one — when the read that reached it carried the connection to its end, which a
12441 /// read of it by its own id does for any issue blocked by no more than a page. What a
12442 /// write reconciles that relationship against, and what a read of its forward edges in
12443 /// the same command answers with.
12444 blocked_by: Option<Vec<Value>>,
12445}
12446
12447impl Resolved {
12448 /// The board this item's own read names it on, when that read named one this source can
12449 /// address.
12450 fn named_board(&self) -> Option<BoardId> {
12451 self.board_id
12452 .as_deref()
12453 .and_then(|id| BoardId::parse(id).ok())
12454 }
12455
12456 /// The board's id and every field it defines, when the read that reached this item
12457 /// carried both — which a read of it by its own id does.
12458 fn carried_board(&self) -> Option<BoardFields> {
12459 Some(BoardFields {
12460 id: self.named_board()?,
12461 fields: self.board_fields.clone()?,
12462 })
12463 }
12464
12465 /// Whether this item holds a value of the board field called `name`, and so carries
12466 /// that field's definition. `false` says nothing about whether the board has the field.
12467 fn defines(&self, name: &str) -> bool {
12468 self.fields
12469 .iter()
12470 .any(|field| field.get("name").and_then(Value::as_str) == Some(name))
12471 }
12472
12473 /// The metadata a caller sees: their own keys, plus the copy origin this source keeps
12474 /// in a field of its own, and none of the five keys that are only an encoding.
12475 ///
12476 /// The two delivery keys are left out for every kind, not only for a task: they are
12477 /// the encoding of [`Task::delivers`] and [`Task::delivered_by`], and a project or a
12478 /// document carrying one holds nothing a caller's own metadata could mean by it.
12479 fn metadata(&self) -> BTreeMap<String, Value> {
12480 let mut metadata = self.slot.clone();
12481 metadata.remove(Repository::METADATA_KEY);
12482 metadata.remove(DependencyEdge::RECORDED_KEY);
12483 metadata.remove(ItemKind::METADATA_KEY);
12484 metadata.remove(TaskRef::DELIVERS_KEY);
12485 metadata.remove(TaskRef::DELIVERED_BY_KEY);
12486 metadata.remove(Classification::METADATA_KEY);
12487 // The board field is the origin, and the body's copy of it is only a mirror for the
12488 // issue search to find: an item whose field holds none has none, whatever its body
12489 // says, so no reader ever sees two answers.
12490 metadata.remove(ORIGIN_KEY);
12491 if let Some(origin) = &self.origin {
12492 metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin.clone()));
12493 }
12494 metadata
12495 }
12496
12497 /// Where this item is, as a link a reader can open.
12498 ///
12499 /// A board is a hosted place and every issue on it has a web address, so that address
12500 /// is what "where is this?" means here — and [`Location::Url`] is what says which kind
12501 /// of place it is, so a reader knows to open it rather than to read a file out. It
12502 /// does not replace or derive from `url`: the field goes on reporting exactly what it
12503 /// reported before, and this says what that address *is*.
12504 ///
12505 /// An item GitHub gave no `url` for — a draft has none — reports no location at all
12506 /// rather than a third variant, which is the contract's "the source did not say". An
12507 /// issue this run created is not one of those: its address comes back from the
12508 /// creating mutation, so it is somewhere a reader can open from the moment it exists
12509 /// rather than from whenever the board read catches up.
12510 fn location(&self) -> Option<Location> {
12511 self.url.clone().map(Location::Url)
12512 }
12513
12514 /// The short handle this board's backend shows people for a task: the issue's number
12515 /// alone, as a decimal string.
12516 ///
12517 /// The number alone rather than `owner/repo#1043`, because that is the contract's
12518 /// value for this backend. A draft has no number and so no handle, which is the
12519 /// contract's *absent* rather than a handle of some other shape — and the native
12520 /// [`Task::id`] here is the issue's GraphQL node id, which this neither replaces nor
12521 /// derives from.
12522 fn key(&self) -> Option<String> {
12523 self.number.map(|number| number.to_string())
12524 }
12525
12526 /// This item's change token — see [`change_token`] — or `None` when the read that
12527 /// reached it did not carry all six of its parts, which a draft never does, did not say
12528 /// when GitHub served it, or found it changed too recently to have settled.
12529 fn version(&self) -> Option<String> {
12530 match self.content_kind {
12531 ContentKind::Issue => {
12532 let (issue, item) = (self.updated_at?, self.item_updated_at?);
12533 let parts = change_token::Parts {
12534 issue,
12535 item,
12536 comments: self.comment_count?,
12537 relationships: self.relationship_events?,
12538 facets: self.facets.clone()?,
12539 repository: self.own_repository.clone()?,
12540 };
12541 change_token::settled(issue, item, self.served_at?)
12542 .then(|| change_token::spell(&parts))
12543 }
12544 ContentKind::DraftIssue => None,
12545 }
12546 }
12547
12548 /// Whether its `Priority` field holds a value at all, mapped or not.
12549 fn holds_priority(&self) -> bool {
12550 self.priority != HeldPriority::Read(Priority::None)
12551 }
12552
12553 /// The task this item is.
12554 ///
12555 /// Fails for an item whose `Priority` field holds an option the mapping does not name:
12556 /// reading that as a level would be a guess, and reading it as `none` would let the next
12557 /// copy clear a priority a person set.
12558 fn task(&self) -> Result<Task, SourceError> {
12559 let priority = match &self.priority {
12560 HeldPriority::Read(priority) => *priority,
12561 HeldPriority::Unmapped(option) => {
12562 return Err(SourceError::Malformed {
12563 message: format!(
12564 "task {}{} sits in the board {PRIORITY_FIELD} option {option:?}, which \
12565 this source's priority_mapping does not name, so its priority cannot be \
12566 read; next: name {option:?} under priority_mapping, or move the item to \
12567 a mapped option",
12568 self.id,
12569 self.number
12570 .map(|number| format!(" (#{number})"))
12571 .unwrap_or_default()
12572 ),
12573 });
12574 }
12575 };
12576 Ok(Task {
12577 version: self.version(),
12578 id: self.id.clone(),
12579 key: self.key(),
12580 title: self.title.clone(),
12581 content: self.body.clone(),
12582 status: self.status.clone(),
12583 priority,
12584 labels: self.labels.clone(),
12585 project: self.parent.clone(),
12586 url: self.url.clone(),
12587 location: self.location(),
12588 created_at: self.created_at,
12589 updated_at: self.updated_at,
12590 metadata: self.metadata(),
12591 repositories: self.repositories.clone(),
12592 delivers: self.delivers.clone(),
12593 delivered_by: self.delivered_by.clone(),
12594 classification: self.classification,
12595 })
12596 }
12597
12598 fn project(&self) -> Project {
12599 Project {
12600 version: self.version(),
12601 id: self.id.clone(),
12602 title: self.title.clone(),
12603 content: self.body.clone(),
12604 status: self.status.clone(),
12605 labels: self.labels.clone(),
12606 url: self.url.clone(),
12607 location: self.location(),
12608 created_at: self.created_at,
12609 updated_at: self.updated_at,
12610 metadata: self.metadata(),
12611 repositories: self.repositories.clone(),
12612 classification: self.classification,
12613 }
12614 }
12615
12616 /// The same issue as a document: the project it is filed under, and no status and no
12617 /// dependencies, because a document is not work.
12618 fn document(&self) -> Document {
12619 Document {
12620 version: self.version(),
12621 id: self.id.clone(),
12622 title: self.title.clone(),
12623 content: self.body.clone(),
12624 project: self.parent.clone(),
12625 labels: self.labels.clone(),
12626 url: self.url.clone(),
12627 location: self.location(),
12628 created_at: self.created_at,
12629 updated_at: self.updated_at,
12630 metadata: self.metadata(),
12631 repositories: self.repositories.clone(),
12632 classification: self.classification,
12633 }
12634 }
12635}
12636
12637/// Where one targeted update moves an item's status, and which of its two halves move.
12638struct StatusMove {
12639 /// The board the item's `Status` field is on.
12640 board: BoardId,
12641 /// The `Status` field's id.
12642 field: String,
12643 /// The option's id.
12644 option: String,
12645 /// The option's name, as the board spells it.
12646 name: String,
12647 /// What the status asks of the issue's state.
12648 target: StatusTarget,
12649 /// The status the item reads as once it is there.
12650 landed: Status,
12651 /// Which of the status's two halves differ from what the item holds.
12652 moves: Moves,
12653}
12654
12655/// Which halves of an item's status one targeted update moves: its `Status` option, the open or
12656/// closed state of its issue, or both. A status neither half of which differs is no move at all,
12657/// and is not a value of this type.
12658#[derive(Clone, Copy, PartialEq, Eq)]
12659enum Moves {
12660 /// The option alone.
12661 Option,
12662 /// The issue's state alone: open, closed, or closed with another reason.
12663 State,
12664 /// Both.
12665 Both,
12666}
12667
12668impl Moves {
12669 /// What differs, or `None` when nothing does.
12670 const fn of(option: bool, state: bool) -> Option<Self> {
12671 match (option, state) {
12672 (true, true) => Some(Self::Both),
12673 (true, false) => Some(Self::Option),
12674 (false, true) => Some(Self::State),
12675 (false, false) => None,
12676 }
12677 }
12678
12679 /// Whether the option moves.
12680 const fn option(self) -> bool {
12681 matches!(self, Self::Option | Self::Both)
12682 }
12683
12684 /// Whether the issue's state moves.
12685 const fn state(self) -> bool {
12686 matches!(self, Self::State | Self::Both)
12687 }
12688}
12689
12690/// What one write is, and the status that comes with being it.
12691///
12692/// One value rather than a [`BoardKind`] beside an `Option<Status>`: a document has no
12693/// status and a task or a project always has one, so "a document carrying a status" and
12694/// "a task carrying none" are states a write cannot be in rather than states every use
12695/// site below has to defend against.
12696enum Written<'a> {
12697 /// A document, which is not work and so has no status at all.
12698 Document,
12699 /// A task or a project, and the status it is being written with.
12700 Work(ItemKind, &'a Status),
12701}
12702
12703impl Written<'_> {
12704 /// Which of the board's three kinds this write is.
12705 const fn kind(&self) -> BoardKind {
12706 match self {
12707 Self::Document => BoardKind::Document,
12708 Self::Work(kind, _) => BoardKind::Work(*kind),
12709 }
12710 }
12711
12712 /// The status this write carries. A document carries none, so a write of one says
12713 /// nothing about the issue's open or closed state and selects no board `Status`
12714 /// option.
12715 const fn status(&self) -> Option<&Status> {
12716 match self {
12717 Self::Document => None,
12718 Self::Work(_, status) => Some(status),
12719 }
12720 }
12721
12722 /// The status this write carries with the kind whose half of `status_mapping` it is
12723 /// written through.
12724 const fn work_status(&self) -> Option<(ItemKind, &Status)> {
12725 match self {
12726 Self::Document => None,
12727 Self::Work(kind, status) => Some((*kind, status)),
12728 }
12729 }
12730}
12731
12732/// The item being written, in the one shape all three write methods reach.
12733struct Incoming<'a> {
12734 written: Written<'a>,
12735 /// The title a person wrote. A document's goes onto the issue with
12736 /// [`DESIGN_TITLE_PREFIX`] put back, so a round trip returns the title that went in.
12737 title: &'a str,
12738 content: Option<&'a str>,
12739 assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
12740 labels: &'a [Label],
12741 metadata: &'a BTreeMap<String, Value>,
12742 repositories: &'a [Repository],
12743 /// Recorded in the slot while private, and nowhere while public.
12744 classification: Classification,
12745 parent: Option<&'a NativeId>,
12746 /// [`Task::delivers`], already checked. Empty for a project or a document, which is
12747 /// what keeps either key out of their slot.
12748 delivers: &'a [TaskRef],
12749 /// [`Task::delivered_by`], already checked. Empty for a project or a document.
12750 delivered_by: &'a [TaskRef],
12751 /// [`Task::priority`], for a task written to an instance that holds one; `None` for a
12752 /// project, a document, and every write to an instance with no `priority_mapping` —
12753 /// which is what keeps such a write's requests exactly what they were before.
12754 priority: Option<Priority>,
12755}
12756
12757/// What one write does to one projected metadata text field, by the field's id.
12758#[derive(Debug, Clone)]
12759enum ProjectionWrite {
12760 /// Write this text.
12761 Set {
12762 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.
12763 text: String,
12764 },
12765 /// Clear the field's value.
12766 Clear { field: String }, // llmlint: ignore[invalid_states_unrepresentable] As above.
12767}
12768
12769impl ProjectionWrite {
12770 /// Add this write to a field write's list of values and list of clears.
12771 fn join<'a>(&'a self, values: &mut Vec<(String, Value)>, clears: &mut Vec<&'a str>) {
12772 match self {
12773 Self::Set { field, text } => values.push((field.clone(), json!({"text": text}))),
12774 Self::Clear { field } => clears.push(field),
12775 }
12776 }
12777}
12778
12779/// What one write does to an item's `Priority` field.
12780enum PriorityWrite {
12781 /// Select this option of this field.
12782 Select {
12783 /// The `Priority` field's id.
12784 field: String,
12785 /// The mapped option's id.
12786 option: String,
12787 },
12788 /// Clear the field's value, which is what `none` is.
12789 Clear {
12790 /// The `Priority` field's id.
12791 field: String,
12792 },
12793}
12794
12795impl Incoming<'_> {
12796 /// The title this write puts on the issue.
12797 fn written_title(&self) -> String {
12798 match self.written {
12799 Written::Document => format!("{DESIGN_TITLE_PREFIX}{}", self.title),
12800 Written::Work(..) => self.title.to_owned(),
12801 }
12802 }
12803}
12804
12805#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
12806enum ContentKind {
12807 DraftIssue,
12808 Issue,
12809}
12810
12811/// What one board issue is: a document, or the work an [`ItemKind`] names.
12812///
12813/// A type of this source's own rather than an `ItemKind` with a third variant, because
12814/// `ItemKind` names what a dependency endpoint points at and nothing may point at a
12815/// document — the contract keeps a document out of that enum deliberately. Holding the
12816/// board's three answers in one value is what makes every place that asks "which is this?"
12817/// answer all three, rather than a `document: bool` beside a `kind` that means nothing for
12818/// two thirds of the board.
12819#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
12820enum BoardKind {
12821 /// An issue whose title begins [`DESIGN_TITLE_PREFIX`].
12822 Document,
12823 /// Every other issue, and every draft.
12824 Work(ItemKind),
12825}
12826
12827impl BoardKind {
12828 /// The kind of an item titled `title`, filed under a project or not, holding `sub_issues`
12829 /// sub-issues and carrying the kind marker `marked` in its slot.
12830 ///
12831 /// The design prefix is read *first*, before either of the two rules that separate a
12832 /// project from a task. A document is not work whatever sub-issues it has and whatever
12833 /// marker it carries, and reading the prefix later would make a design issue with none of
12834 /// either an empty project. Being a sub-issue wins outright over the rest, and no marker
12835 /// overrides it: an issue filed under a project is that project's task even when it has
12836 /// sub-issues of its own.
12837 fn of(title: &str, filed: bool, sub_issues: u64, marked: Option<ItemKind>) -> Self {
12838 if title.starts_with(DESIGN_TITLE_PREFIX) {
12839 Self::Document
12840 } else if filed {
12841 Self::Work(ItemKind::Task)
12842 } else if sub_issues > 0 || marked == Some(ItemKind::Project) {
12843 Self::Work(ItemKind::Project)
12844 } else {
12845 Self::Work(ItemKind::Task)
12846 }
12847 }
12848
12849 /// What the change token's facets call this kind.
12850 const fn facet(self) -> change_token::FacetKind {
12851 match self {
12852 Self::Document => change_token::FacetKind::Document,
12853 Self::Work(ItemKind::Project) => change_token::FacetKind::Project,
12854 Self::Work(ItemKind::Task) => change_token::FacetKind::Task,
12855 }
12856 }
12857
12858 /// Whose half of `status_mapping` an item of this kind reads its status through. A
12859 /// document has no status of its own, so the task half stands in for whatever the issue
12860 /// holds; nothing reports it.
12861 const fn status_kind(self) -> ItemKind {
12862 match self {
12863 Self::Document => ItemKind::Task,
12864 Self::Work(kind) => kind,
12865 }
12866 }
12867
12868 /// How a refusal names this kind to the person reading it.
12869 const fn describes(self) -> &'static str {
12870 match self {
12871 Self::Document => "document",
12872 Self::Work(kind) => kind.marker(),
12873 }
12874 }
12875}
12876
12877/// Whether `labels` satisfies `filter`, matching by name, case-insensitively.
12878///
12879/// This is the local Markdown source's `labels_match`, spelled the same way on purpose:
12880/// the shared cross-source journeys assert one answer to one question, so two sources
12881/// that disagree about what "carries the label bug" means fail them.
12882fn labels_match(labels: &[Label], filter: &LabelFilter) -> bool {
12883 let holds = |name: &String| {
12884 labels
12885 .iter()
12886 .any(|label| label.name.eq_ignore_ascii_case(name))
12887 };
12888 (filter.any_of.is_empty() || filter.any_of.iter().any(holds))
12889 && filter.all_of.iter().all(holds)
12890 && !filter.none_of.iter().any(holds)
12891}
12892
12893/// Whether `category` is one of `statuses`. An empty list is unfiltered rather than
12894/// "keeps nothing", which is what lets a `Vec<StatusCategory>` spell no filter at all.
12895fn status_matches(category: StatusCategory, statuses: &[StatusCategory]) -> bool {
12896 statuses.is_empty() || statuses.contains(&category)
12897}
12898
12899/// Whether `title`/`content` satisfies `query`, matching case-insensitively.
12900///
12901/// `content` is the item's own prose — the body with this source's trailing metadata
12902/// comment already taken off — so a search never matches an encoding the author of the
12903/// issue never wrote.
12904fn text_matches(title: &str, content: Option<&str>, query: &TextQuery) -> bool {
12905 let terms = query.terms.to_lowercase();
12906 let in_title = title.to_lowercase().contains(&terms);
12907 let in_content = content.is_some_and(|body| body.to_lowercase().contains(&terms));
12908 match query.fields {
12909 TextFields::Title => in_title,
12910 TextFields::Content => in_content,
12911 TextFields::TitleOrContent => in_title || in_content,
12912 }
12913}
12914
12915/// Whether `task` satisfies `query`, with `project` deciding the project predicate.
12916///
12917/// The project predicate is passed separately because a read narrowed to one project has
12918/// already answered it by asking *that project* for its own items — and re-applying it
12919/// there would compare the caller's selector, which may be a project's **name**, against
12920/// the id of the project that name resolved to, and keep nothing. Every other read passes
12921/// `query.project` and applies it here, which is what keeps `projects` a predicate this
12922/// source really does apply.
12923fn task_matches(task: &Task, query: &TaskQuery, project: &ProjectFilter) -> bool {
12924 labels_match(&task.labels, &query.labels)
12925 && status_matches(task.status.category, &query.statuses)
12926 && (query.priorities.is_empty() || query.priorities.contains(&task.priority))
12927 && match project {
12928 ProjectFilter::Any => true,
12929 ProjectFilter::Orphans => task.project.is_none(),
12930 ProjectFilter::Is(id) => task.project.as_ref() == Some(id),
12931 }
12932 && query
12933 .text
12934 .as_ref()
12935 .is_none_or(|text| text_matches(&task.title, task.content.as_deref(), text))
12936 // Against the parsed metadata slot, and against the origin field, which is where
12937 // `Resolved::metadata` reads each of them from.
12938 && query.metadata_matches(&task.metadata)
12939 && query.origin_matches(&task.metadata)
12940}
12941
12942fn project_matches(project: &Project, query: &ProjectQuery) -> bool {
12943 labels_match(&project.labels, &query.labels)
12944 && status_matches(project.status.category, &query.statuses)
12945 && query
12946 .text
12947 .as_ref()
12948 .is_none_or(|text| text_matches(&project.title, project.content.as_deref(), text))
12949}
12950
12951/// The same three predicates a task query carries, minus the status filter.
12952///
12953/// A document is not work, so it has no status for one to compare against and the query
12954/// type carries none. The project predicate is the same one — a design issue filed under a
12955/// project issue is in that project, and one filed under nothing is in none — so it is
12956/// spelled the same way here rather than answered differently.
12957fn document_matches(document: &Document, query: &DocumentQuery, project: &ProjectFilter) -> bool {
12958 labels_match(&document.labels, &query.labels)
12959 && match project {
12960 ProjectFilter::Any => true,
12961 ProjectFilter::Orphans => document.project.is_none(),
12962 ProjectFilter::Is(id) => document.project.as_ref() == Some(id),
12963 }
12964 && query
12965 .text
12966 .as_ref()
12967 .is_none_or(|text| text_matches(&document.title, document.content.as_deref(), text))
12968}
12969
12970/// Why a GitHub item's change token does not vouch for its forward edges.
12971///
12972/// A forward edge names its far end and that far end's kind, and the near item's token moves
12973/// with the edges it holds but says nothing of the far ends themselves: a far end turning from a
12974/// task into a project when it gains a sub-issue, retitled into a document, or deleted moves
12975/// nothing the near token reads, so an edge kept at the near version could name a far end that
12976/// is no longer what it said.
12977const FAR_ENDS_UNCOVERED: &str = "a github item's change token does not attest a far end's kind, \
12978 whether it became a document, or its deletion, so its forward edges are always read from \
12979 github";
12980
12981/// The issue-search qualifier that keeps open issues alone, which a status filter naming only
12982/// categories an open issue reads as narrows a board-scoped search with.
12983const OPEN_QUALIFIER: &str = "is:open";
12984
12985/// The board filter `task status reconcile` reads the board through: closed items alone.
12986const CLOSED_FILTER: &str = "is:closed";
12987
12988/// The variables of one [`graphql::ISSUES_BY_REFERENCE`] request for `batch`: each slot names
12989/// one reference, and a slot the batch has none for names its last one again.
12990fn reference_batch(batch: &[IssueRef]) -> Value {
12991 let mut variables = serde_json::Map::new();
12992 for slot in 0..REFERENCE_BATCH {
12993 let issue = batch.get(slot).or(batch.last());
12994 variables.insert(format!("owner{slot}"), json!(issue.map(|i| &i.owner)));
12995 variables.insert(format!("name{slot}"), json!(issue.map(|i| &i.name)));
12996 variables.insert(format!("number{slot}"), json!(issue.map(|i| i.number)));
12997 }
12998 variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
12999 variables.insert("statusField".to_owned(), json!(STATUS_FIELD));
13000 variables.insert("duplicates".to_owned(), json!(true));
13001 Value::Object(variables)
13002}
13003
13004/// One issue an issue read by reference answered, in the shape every other read of an issue
13005/// hands its resolver: each board membership's `Status` value, read by name, as the one field
13006/// value that membership carries.
13007///
13008/// A membership's `status` is `null` when its card sits at no option — or on a board with no
13009/// field of that name — and otherwise an option value naming its option; anything else is not
13010/// an answer to this read, and is refused rather than read as a card at no option.
13011fn held_issue(issue: &Value) -> Result<Value, SourceError> {
13012 let malformed = |what: &str| SourceError::Malformed {
13013 message: format!("GitHub answered an issue read by reference with {what}"),
13014 };
13015 let mut issue = issue.clone();
13016 let nodes = issue
13017 .pointer_mut("/projectItems/nodes")
13018 .and_then(Value::as_array_mut)
13019 .ok_or_else(|| malformed("no list of board memberships"))?;
13020 for node in nodes {
13021 let node = node
13022 .as_object_mut()
13023 .ok_or_else(|| malformed("a board membership that is not an object"))?;
13024 let status = match node.get("status") {
13025 None => return Err(malformed("a board membership with no status member")),
13026 Some(Value::Null) => None,
13027 Some(status) if status.get("name").is_some_and(Value::is_string) => {
13028 Some(status.clone())
13029 }
13030 // A single-select value of another field type answers `{}`; only an option value
13031 // carries a name.
13032 Some(status) if status.as_object().is_some_and(serde_json::Map::is_empty) => None,
13033 Some(_) => return Err(malformed("a membership status that names no option")),
13034 };
13035 node.insert(
13036 "fieldValues".to_owned(),
13037 json!({"nodes": status.into_iter().collect::<Vec<_>>(),
13038 "pageInfo": {"hasNextPage": false}}),
13039 );
13040 }
13041 Ok(issue)
13042}
13043
13044/// The issue one slot of a read by reference answered, `None` when GitHub answered that
13045/// slot's repository or issue `null` — which [`GitHubProjectsSource::answer_unresolved`]
13046/// admitted only beside a `NOT_FOUND` naming that very slot — and a refusal when the answer
13047/// leaves the slot, or its `issue` member, out: an answer missing a member is malformed rather
13048/// than a reference nothing holds.
13049fn referenced_issue(data: &Value, slot: usize) -> Result<Option<&Value>, SourceError> {
13050 let alias = format!("r{slot}");
13051 let repository = data.get(&alias).ok_or_else(|| SourceError::Malformed {
13052 message: format!("GitHub answered a read by reference with no {alias}"),
13053 })?;
13054 if repository.is_null() {
13055 return Ok(None);
13056 }
13057 let issue = repository
13058 .get("issue")
13059 .ok_or_else(|| SourceError::Malformed {
13060 message: format!("GitHub answered {alias} of a read by reference with no issue"),
13061 })?;
13062 if issue.is_null() {
13063 return Ok(None);
13064 }
13065 // `Repository.issue` is typed `Issue`, so anything else is not an answer to this read.
13066 if optional_str(issue, "__typename")? != Some("Issue") {
13067 return Err(SourceError::Malformed {
13068 message: format!(
13069 "GitHub answered {alias} of a read by reference with an issue whose __typename \
13070 is not Issue"
13071 ),
13072 });
13073 }
13074 Ok(Some(issue))
13075}
13076
13077/// A closure reason as a person reads it: `NOT_PLANNED` as `not planned`.
13078fn spoken_reason(reason: &str) -> String {
13079 reason.to_ascii_lowercase().replace('_', " ")
13080}
13081
13082impl GitHubProjectsSource {
13083 /// Every task and project of this board whose card contradicts its issue's closure, each
13084 /// judged against its own kind's half of `status_mapping`, read through the board's own
13085 /// `is:closed` filter, page by page — never the board's unfiltered `items`.
13086 ///
13087 /// Each item the filter answers is confirmed from its own node: an open one, one whose
13088 /// closure reads `unknown`, and one whose card is already in line are left out whatever
13089 /// the filter let through. The board's fields ride along and are held for the option
13090 /// writes that follow, so those need no read of the board.
13091 async fn contradictions(&self) -> Result<Vec<StatusContradiction>, SourceError> {
13092 let mut after: Option<String> = None;
13093 let mut found = Vec::new();
13094 loop {
13095 let data = self
13096 .graphql(
13097 graphql::CLOSED_ITEMS,
13098 json!({"owner":self.owner,"number":self.project_number,
13099 "first":MAX_PAGE_SIZE,"after":after,"filter":CLOSED_FILTER,
13100 "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
13101 )
13102 .await?;
13103 let board = data
13104 .pointer("/closedItems/projectV2")
13105 .filter(|value| !value.is_null())
13106 .ok_or_else(|| SourceError::Refused {
13107 message: format!(
13108 "GitHub project {}/{} was not found or is not visible to the token",
13109 self.owner, self.project_number
13110 ),
13111 })?;
13112 *self.fields_cache()? = Some(BoardFields {
13113 id: BoardId::parse(required_str(board, "id")?)?,
13114 fields: board.get("fields").cloned().unwrap_or(Value::Null),
13115 });
13116 let items = board.get("items").ok_or_else(|| SourceError::Malformed {
13117 message: "GitHub project closed items are missing".into(),
13118 })?;
13119 for item in items
13120 .get("nodes")
13121 .and_then(Value::as_array)
13122 .ok_or_else(|| SourceError::Malformed {
13123 message: "GitHub project items.nodes is not an array".into(),
13124 })?
13125 {
13126 let Some(resolved) = self.resolve(item)? else {
13127 continue;
13128 };
13129 let BoardKind::Work(kind) = resolved.kind else {
13130 continue;
13131 };
13132 let Some((closed_as, name)) = self.statuses.contradicted(
13133 kind,
13134 resolved.option.as_deref(),
13135 resolved.closed,
13136 resolved.reason.as_deref(),
13137 ) else {
13138 continue;
13139 };
13140 found.push(StatusContradiction {
13141 closed_as,
13142 to: name.to_owned(),
13143 option: resolved.option.clone(),
13144 item: match kind {
13145 ItemKind::Task => ContradictedItem::Task(resolved.task()?),
13146 ItemKind::Project => ContradictedItem::Project(resolved.project()),
13147 },
13148 });
13149 }
13150 let info = items
13151 .get("pageInfo")
13152 .ok_or_else(|| SourceError::Malformed {
13153 message: "GitHub project items have no pageInfo".into(),
13154 })?;
13155 if !required_bool(info, "hasNextPage")? {
13156 return Ok(found);
13157 }
13158 let next = required_str(info, "endCursor")?;
13159 validate_cursor_progress(after.as_deref(), next)?;
13160 after = Some(next.to_owned());
13161 }
13162 }
13163
13164 /// What this board holds for each issue in `issues`, once each in the order given, read
13165 /// [`REFERENCE_BATCH`] references at a time — and nothing else of the board.
13166 ///
13167 /// An issue GitHub cannot find for this source's credential, and one with no item on this
13168 /// board, are both absent. A closure that already says the work will not be done here —
13169 /// not planned, duplicate — is skipped: a landing does not overrule it. An item closed as
13170 /// completed whose card sits at its kind's `done` option is unchanged, and every other item
13171 /// is due a `done` write. A project item — a plan's home card, its issue holding the plan's
13172 /// tasks as sub-issues — is held under exactly these rules, judged against the project
13173 /// kind's half of `status_mapping`.
13174 async fn holdings(&self, issues: &[IssueRef]) -> Result<Vec<IssueHolding>, SourceError> {
13175 let mut distinct: Vec<IssueRef> = Vec::with_capacity(issues.len());
13176 for issue in issues {
13177 if !distinct.contains(issue) {
13178 distinct.push(issue.clone());
13179 }
13180 }
13181 let done = |kind: ItemKind| {
13182 self.statuses
13183 .target(kind, StatusCategory::Done)
13184 .option()
13185 .unwrap_or("Done")
13186 .to_owned()
13187 };
13188 // GitHub numbers an issue with a GraphQL `Int`, a signed 32-bit value, so a reference past
13189 // its range names no issue any repository can hold: it is answered absent and never sent,
13190 // where binding it would have GitHub refuse the whole batch.
13191 let readable: Vec<IssueRef> = distinct
13192 .iter()
13193 .filter(|issue| i32::try_from(issue.number).is_ok())
13194 .cloned()
13195 .collect();
13196 let mut holdings = Vec::with_capacity(readable.len());
13197 for batch in readable.chunks(REFERENCE_BATCH) {
13198 let data = self
13199 .graphql(graphql::ISSUES_BY_REFERENCE, reference_batch(batch))
13200 .await?;
13201 for (slot, issue) in batch.iter().enumerate() {
13202 let node = referenced_issue(&data, slot)?;
13203 let resolved = match node {
13204 Some(node) => self.resolve_issue_as(&held_issue(node)?, false).await?,
13205 None => None,
13206 };
13207 // A task and a project are both held; a document is not work, so nothing is.
13208 let Some((item, kind)) = resolved.and_then(|item| match item.kind {
13209 BoardKind::Work(kind) => Some((item, kind)),
13210 BoardKind::Document => None,
13211 }) else {
13212 holdings.push(IssueHolding {
13213 issue: issue.clone(),
13214 completion: Completion::Absent,
13215 });
13216 continue;
13217 };
13218 let task = match kind {
13219 ItemKind::Task => {
13220 let held = item.task()?;
13221 HeldTask {
13222 id: held.id,
13223 kind,
13224 option: item.option.clone(),
13225 classification: held.classification,
13226 repositories: held.repositories,
13227 delivers: held.delivers,
13228 }
13229 }
13230 // A project delivers no task, so what its write keeps in step is nothing.
13231 ItemKind::Project => {
13232 let held = item.project();
13233 HeldTask {
13234 id: held.id,
13235 kind,
13236 option: item.option.clone(),
13237 classification: held.classification,
13238 repositories: held.repositories,
13239 delivers: Vec::new(),
13240 }
13241 }
13242 };
13243 let done = done(kind);
13244 let completion = match (item.closed, item.reason.as_deref()) {
13245 (true, Some(reason @ ("NOT_PLANNED" | "DUPLICATE"))) => Completion::Skipped {
13246 task,
13247 reason: format!("closed as {}", spoken_reason(reason)),
13248 },
13249 (true, None | Some("COMPLETED"))
13250 if item
13251 .option
13252 .as_deref()
13253 .is_some_and(|option| option.eq_ignore_ascii_case(&done)) =>
13254 {
13255 Completion::Unchanged { task }
13256 }
13257 _ => {
13258 let kept = item.clone();
13259 self.held_cache
13260 .lock()
13261 .map_err(|_| SourceError::Unavailable {
13262 message: "items read by issue reference were left \
13263 inconsistent; run the command again"
13264 .into(),
13265 })?
13266 .insert(kept.id.clone(), kept);
13267 Completion::Due {
13268 task,
13269 to: done.clone(),
13270 }
13271 }
13272 };
13273 holdings.push(IssueHolding {
13274 issue: issue.clone(),
13275 completion,
13276 });
13277 }
13278 }
13279 let mut read = holdings.into_iter();
13280 let mut answered = Vec::with_capacity(distinct.len());
13281 for issue in distinct {
13282 if i32::try_from(issue.number).is_ok() {
13283 answered.push(read.next().ok_or_else(|| SourceError::Malformed {
13284 message: format!("no holding was read for {issue}"),
13285 })?);
13286 } else {
13287 answered.push(IssueHolding {
13288 issue,
13289 completion: Completion::Absent,
13290 });
13291 }
13292 }
13293 Ok(answered)
13294 }
13295}
13296
13297#[async_trait::async_trait]
13298impl TaskSource for GitHubProjectsSource {
13299 fn kind(&self) -> &'static str {
13300 KIND
13301 }
13302 fn order_versions(&self, a: &str, b: &str) -> Option<std::cmp::Ordering> {
13303 change_token::order(a, b)
13304 }
13305 /// A task's fields and comments: §4.13d of `docs/plugin-protocol.md` states, member by
13306 /// member, the change each part of the token moves for. Not its forward edges — see
13307 /// [`FAR_ENDS_UNCOVERED`].
13308 fn version_coverage(&self) -> VersionCoverage {
13309 VersionCoverage {
13310 forward_edges: Coverage::Uncovered {
13311 reason: FAR_ENDS_UNCOVERED.to_owned(),
13312 },
13313 ..VersionCoverage::whole()
13314 }
13315 }
13316 /// Every part, for a copy's skip: the relationship count moves for an edge added or removed
13317 /// and for a far end deleted, so the token sees every change to a task's edges but one — a
13318 /// far end's own kind flipping, by gaining a sub-issue or being retitled into a document,
13319 /// which `spike-coverage` measured stale 1 of 1. A skipping recopy does not carry that, and
13320 /// the next copy that reads the task does (`docs/plugin-protocol.md` §4.13d).
13321 // 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.
13322 fn copy_version_coverage(&self) -> VersionCoverage {
13323 VersionCoverage::whole()
13324 }
13325 /// A project's fields, and not its forward edges, for the same reason a task's are not.
13326 fn project_version_coverage(&self) -> ProjectVersionCoverage {
13327 ProjectVersionCoverage {
13328 fields: Coverage::Covered,
13329 forward_edges: Coverage::Uncovered {
13330 reason: FAR_ENDS_UNCOVERED.to_owned(),
13331 },
13332 }
13333 }
13334 /// A document's fields and the manifest of its image assets, which its metadata slot
13335 /// records: both are the issue's body, whose every edit moves the token's issue time.
13336 fn document_version_coverage(&self) -> DocumentVersionCoverage {
13337 DocumentVersionCoverage::whole()
13338 }
13339 fn write_context(&self, id: &NativeId) -> Option<Value> {
13340 self.context_of(id)
13341 }
13342 fn board_context(&self) -> Option<Value> {
13343 self.board_context_held()
13344 }
13345 fn prime_write(&self, id: &NativeId, context: &Value, board: Option<&Value>) -> bool {
13346 self.prime(id, context, board)
13347 }
13348 fn unprime(&self, id: &NativeId) {
13349 self.unprime_held(id);
13350 }
13351 fn placement_context(&self) -> Option<Value> {
13352 self.placement_held()
13353 }
13354 fn prime_placement(&self, context: &Value) -> bool {
13355 self.prime_placement_held(context)
13356 }
13357 fn written_context(&self, id: &NativeId) -> Option<Value> {
13358 self.written_held(id)
13359 }
13360 fn prime_written(&self, id: &NativeId, context: &Value, task: &Task) -> bool {
13361 self.prime_written_held(id, context, task)
13362 }
13363 fn begin_journal(&self) {
13364 self.expected.begin();
13365 }
13366 fn end_journal(&self) -> Option<WriteJournal> {
13367 Some(self.expected.end())
13368 }
13369 fn capabilities(&self) -> Capabilities {
13370 Capabilities {
13371 projects: Support::Native,
13372 documents: Support::Native,
13373 comments: Support::Native,
13374 assets: Support::Native,
13375 priority: if self.priorities.is_some() {
13376 Support::Native
13377 } else {
13378 Support::Unsupported
13379 },
13380 filter_by_priority: Support::Native,
13381 filter_by_comment_activity: Support::Native,
13382 filter_by_metadata: Support::Native,
13383 filter_by_origin: Support::Native,
13384 reconcile_status: Support::Native,
13385 complete_by_issue: Support::Native,
13386 orphan_tasks: Support::Native,
13387 filter_by_label: Support::Native,
13388 filter_by_status: Support::Native,
13389 search_title: Support::Native,
13390 search_content: Support::Native,
13391 task_dependencies: DependencySupport::BothDirections,
13392 project_dependencies: DependencySupport::BothDirections,
13393 max_page_size: MAX_PAGE_SIZE,
13394 }
13395 }
13396 async fn visibility(
13397 &self,
13398 target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13399 ) -> Result<onetaskgraph_plugin_api::Visibility, SourceError> {
13400 self.write_visibility(target).await
13401 }
13402 async fn visibility_with_project(
13403 &self,
13404 target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13405 project: &onetaskgraph_plugin_api::WriteTarget<'_>,
13406 ) -> Result<onetaskgraph_plugin_api::Visibility, SourceError> {
13407 self.write_visibility_with_project(target, project).await
13408 }
13409 fn declare_visibility(&self, declared: onetaskgraph_plugin_api::Visibility) {
13410 self.declared_private.store(
13411 declared == onetaskgraph_plugin_api::Visibility::Private,
13412 std::sync::atomic::Ordering::SeqCst,
13413 );
13414 }
13415 fn scope_visibility<'a>(
13416 &'a self,
13417 operation: std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send + 'a>>,
13418 ) -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send + 'a>> {
13419 self.scope_visibility_reads(operation)
13420 }
13421
13422 /// Where a create of `target` would file its issue, by [`Self::creation_rule`] — the rule
13423 /// the create applies — reading the parent project's issue only when `target` names one.
13424 async fn creation_repository(
13425 &self,
13426 target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13427 ) -> Result<Option<Repository>, SourceError> {
13428 let onetaskgraph_plugin_api::WriteTarget::New {
13429 repositories,
13430 project,
13431 } = target
13432 else {
13433 return Ok(None);
13434 };
13435 let target = self
13436 .creation_rule(repositories, *project, "a new item")
13437 .await?;
13438 Repository::try_from(target.origin())
13439 .map(Some)
13440 .map_err(|message| SourceError::Malformed { message })
13441 }
13442 /// Every issue of the repository a create of `target` files in whose body carries `key` in
13443 /// its metadata slot, read off that repository's issues newest first back to `since` —
13444 /// never through search, whose index lags a fresh issue by seconds.
13445 async fn created_with_key(
13446 &self,
13447 record: onetaskgraph_plugin_api::MetadataRecord,
13448 key: &str,
13449 since: Option<DateTime<Utc>>,
13450 target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13451 ) -> Result<Vec<NativeId>, SourceError> {
13452 let _ = record;
13453 let onetaskgraph_plugin_api::WriteTarget::New {
13454 repositories,
13455 project,
13456 } = target
13457 else {
13458 return Ok(Vec::new());
13459 };
13460 let repository = self
13461 .creation_rule(repositories, *project, "a new item")
13462 .await?;
13463 let wanted = Value::String(key.to_owned());
13464 let mut found = Vec::new();
13465 let mut after: Option<String> = None;
13466 let mut followed = std::collections::HashSet::new();
13467 loop {
13468 let data = self
13469 .graphql(
13470 graphql::ISSUES_BY_CREATION,
13471 json!({"owner": repository.owner, "name": repository.name,
13472 "first": MAX_PAGE_SIZE, "after": after}),
13473 )
13474 .await?;
13475 let issues =
13476 data.pointer("/repository/issues")
13477 .ok_or_else(|| SourceError::Malformed {
13478 message: format!(
13479 "GitHub answered no issues for repository {}",
13480 repository.slug()
13481 ),
13482 })?;
13483 let malformed = |what: &str| SourceError::Malformed {
13484 message: format!(
13485 "GitHub answered a page of repository {}'s issues {what}",
13486 repository.slug()
13487 ),
13488 };
13489 let nodes = issues
13490 .get("nodes")
13491 .and_then(Value::as_array)
13492 .ok_or_else(|| malformed("with no list of issues"))?;
13493 let mut older = false;
13494 for node in nodes {
13495 let id = node
13496 .get("id")
13497 .and_then(Value::as_str)
13498 .filter(|id| !id.is_empty())
13499 .ok_or_else(|| malformed("holding an issue with no id"))?;
13500 let created = optional_time(node, "createdAt")?
13501 .ok_or_else(|| malformed("holding an issue with no creation time"))?;
13502 if since.is_some_and(|since| created < since) {
13503 older = true;
13504 break;
13505 }
13506 let body = match node.get("body") {
13507 Some(Value::String(body)) => Some(body.clone()),
13508 Some(Value::Null) => None,
13509 _ => return Err(malformed("holding an issue with no body member")),
13510 };
13511 let (_, metadata) = metadata_body(body)?;
13512 if metadata.get(MetadataKey::CREATE_KEY) == Some(&wanted) {
13513 found.push(NativeId(id.to_owned()));
13514 }
13515 }
13516 let next = issues
13517 .pointer("/pageInfo/hasNextPage")
13518 .and_then(Value::as_bool)
13519 .ok_or_else(|| malformed("that does not say whether another follows"))?;
13520 if older || !next {
13521 return Ok(found);
13522 }
13523 // A page that says another follows names where it starts, and never a page already
13524 // read: anything else would ask for the same pages again for ever.
13525 let cursor = issues
13526 .pointer("/pageInfo/endCursor")
13527 .and_then(Value::as_str)
13528 .filter(|cursor| !cursor.is_empty())
13529 .map(str::to_owned);
13530 if cursor
13531 .as_ref()
13532 .is_none_or(|cursor| !followed.insert(cursor.clone()))
13533 {
13534 return Err(SourceError::Malformed {
13535 message: format!(
13536 "GitHub answered a page of repository {}'s issues that says another \
13537 follows without a new cursor to it",
13538 repository.slug()
13539 ),
13540 });
13541 }
13542 after = cursor;
13543 }
13544 }
13545
13546 /// `Repository.issues` lists an issue GitHub committed only once its listing catches up.
13547 fn create_key_lags(&self) -> bool {
13548 true
13549 }
13550
13551 /// The task as this process's own latest write of it left it, from the item it recorded
13552 /// that write as — never a request. Its `version` is always `None`: a board's change token
13553 /// is given only once an item has gone unchanged for [`change_token::SETTLED`], which an
13554 /// item this command has just written has not, so no write's own answer can vouch for one.
13555 async fn written_task(&self, id: &NativeId) -> Option<Task> {
13556 let held = self
13557 .created()
13558 .ok()?
13559 .iter()
13560 .find(|held| held.id == *id)
13561 .cloned()
13562 .or_else(|| {
13563 self.updated()
13564 .ok()?
13565 .iter()
13566 .find(|held| held.id == *id)
13567 .cloned()
13568 })?;
13569 let mut task = held.task().ok()?;
13570 task.version = None;
13571 Some(task)
13572 }
13573
13574 /// Why `creation_owners` refuses a create of `target`, by [`Self::creation_rule`] — read
13575 /// only when the list is not empty, so a source restricting nothing reads nothing.
13576 async fn creation_refusal(
13577 &self,
13578 target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13579 ) -> Result<Option<String>, SourceError> {
13580 let onetaskgraph_plugin_api::WriteTarget::New {
13581 repositories,
13582 project,
13583 } = target
13584 else {
13585 return Ok(None);
13586 };
13587 if self.creation_owners.is_empty() {
13588 return Ok(None);
13589 }
13590 let target = self
13591 .creation_rule(repositories, *project, "a new item")
13592 .await?;
13593 Ok(self.owner_refusal(&target))
13594 }
13595
13596 async fn health(&self) -> Result<Health, SourceError> {
13597 let board = self.board_page(None, 1).await?;
13598 Ok(Health {
13599 reachable: true,
13600 detail: Some(format!(
13601 "reading GitHub project {}/{} ({})",
13602 self.owner,
13603 self.project_number,
13604 required_str(&board, "title")?
13605 )),
13606 })
13607 }
13608 /// The board's own node id, digested: the one request reading its fields, or none when
13609 /// this command has read them already.
13610 async fn designator(&self) -> Result<Option<Designator>, SourceError> {
13611 let board = self.board_fields().await?;
13612 Designator::of_backend(KIND, board.id.as_str())
13613 .map(Some)
13614 .map_err(|message| SourceError::Malformed { message })
13615 }
13616 async fn get_task(&self, id: &NativeId) -> Result<Option<Task>, SourceError> {
13617 self.item_by_id(id)
13618 .await?
13619 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
13620 .map(|item| item.task())
13621 .transpose()
13622 }
13623 async fn task_assets(
13624 &self,
13625 id: &NativeId,
13626 ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
13627 self.held_assets(id, BoardKind::Work(ItemKind::Task)).await
13628 }
13629 async fn task_asset(
13630 &self,
13631 id: &NativeId,
13632 name: &onetaskgraph_plugin_api::AssetName,
13633 ) -> Result<Option<Vec<u8>>, SourceError> {
13634 self.held_asset(id, BoardKind::Work(ItemKind::Task), name)
13635 .await
13636 }
13637 async fn set_task_rendering_with_assets(
13638 &self,
13639 id: &NativeId,
13640 content: &str,
13641 provenance: &Value,
13642 _answers: &BTreeMap<String, Value>,
13643 assets: &onetaskgraph_plugin_api::AssetWrite,
13644 ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
13645 self.replace_rendering(
13646 id,
13647 BoardKind::Work(ItemKind::Task),
13648 content,
13649 provenance,
13650 Some(assets),
13651 )
13652 .await
13653 }
13654 async fn document_assets(
13655 &self,
13656 id: &NativeId,
13657 ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
13658 self.held_assets(id, BoardKind::Document).await
13659 }
13660 async fn document_asset(
13661 &self,
13662 id: &NativeId,
13663 name: &onetaskgraph_plugin_api::AssetName,
13664 ) -> Result<Option<Vec<u8>>, SourceError> {
13665 self.held_asset(id, BoardKind::Document, name).await
13666 }
13667 async fn set_document_rendering_with_assets(
13668 &self,
13669 id: &NativeId,
13670 content: &str,
13671 provenance: &Value,
13672 _answers: &BTreeMap<String, Value>,
13673 assets: &onetaskgraph_plugin_api::AssetWrite,
13674 ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
13675 self.replace_rendering(id, BoardKind::Document, content, provenance, Some(assets))
13676 .await
13677 }
13678 async fn get_project(&self, id: &NativeId) -> Result<Option<Project>, SourceError> {
13679 Ok(self
13680 .item_by_id(id)
13681 .await?
13682 .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
13683 .map(|item| item.project()))
13684 }
13685 async fn query_tasks(
13686 &self,
13687 query: &TaskQuery,
13688 page: &PageRequest,
13689 ) -> Result<Page<Task>, SourceError> {
13690 validate_page(page)?;
13691 refuse_unsearchable(query)?;
13692 // A closed issue reads `done`, `cancelled` or — closed for a reason that implies no
13693 // category — `unknown`; every other category reads only off an open issue or a draft. So
13694 // a status filter naming none of those three asks every issue search it sends for open
13695 // issues alone; each candidate's category is still confirmed from its own item below.
13696 let open_only = !query.statuses.is_empty()
13697 && query.statuses.iter().all(|category| {
13698 !matches!(
13699 category,
13700 StatusCategory::Done | StatusCategory::Cancelled | StatusCategory::Unknown
13701 )
13702 });
13703 if query.origin.is_none() && !matches!(query.project, ProjectFilter::Is(_)) {
13704 let mut qualifiers: Vec<String> = [
13705 query.commented_since.map(updated_qualifier),
13706 narrowing_qualifiers(query),
13707 ]
13708 .into_iter()
13709 .flatten()
13710 .collect();
13711 // A narrowed search never answers a draft, which is no issue, so narrowing it to
13712 // open issues too leaves out nothing such a search could have answered.
13713 if !qualifiers.is_empty() {
13714 if open_only {
13715 qualifiers.push(OPEN_QUALIFIER.to_owned());
13716 }
13717 return self.search_tasks(query, page, &qualifiers.join(" ")).await;
13718 }
13719 }
13720
13721 // A read narrowed to one project asks that project for its own tasks, so nothing
13722 // about it costs what the rest of the board holds. A read carrying a text, metadata
13723 // or origin predicate asks GitHub the narrower question those predicates are, and a
13724 // read narrowed to comment activity alone asks the board's own issue search for the
13725 // issues updated since, which is every issue a comment could have been written or
13726 // edited on since. Every other task read is a question about the whole board and is
13727 // answered by reading it.
13728 let (held, membership) = match (&query.project, query.commented_since) {
13729 (ProjectFilter::Is(project), _) => (
13730 match self.held_task_children(project)? {
13731 // Taken as `project_children` takes the project's own sub-issue read: with
13732 // every item this process wrote under it since, and none it moved away.
13733 Some(held) => self.completed_with_written(
13734 held.into_iter()
13735 .filter(|child| child.parent.as_ref() == Some(project))
13736 .collect(),
13737 |own| own.parent.as_ref() == Some(project),
13738 )?,
13739 None => self.project_children(project).await?,
13740 },
13741 // Answered by where these items came from; see `task_matches`.
13742 &ProjectFilter::Any,
13743 ),
13744 (ProjectFilter::Any | ProjectFilter::Orphans, since) => {
13745 match (self.narrowed(query).await?, since) {
13746 (Some(narrowed), _) => (narrowed, &query.project),
13747 (None, Some(since)) => (self.updated_since(since).await?, &query.project),
13748 // The board's own items list every draft; only the search half, which
13749 // completes them with issues they lag behind on, is narrowed — a closed
13750 // issue that half leaves out is at no category the filter keeps.
13751 (None, None) if open_only => (
13752 self.board_completed_by(Some(OPEN_QUALIFIER)).await?.items,
13753 &query.project,
13754 ),
13755 (None, None) => (self.board().await?.items, &query.project),
13756 }
13757 }
13758 };
13759 // Filtered before paged: a page of a filtered result is a page of the survivors,
13760 // never the survivors of a page.
13761 let mut tasks = Vec::new();
13762 for item in held
13763 .iter()
13764 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
13765 {
13766 let task = item.task()?;
13767 if task_matches(&task, query, membership)
13768 && self.commented_since(item, query.commented_since).await?
13769 {
13770 tasks.push(task);
13771 }
13772 }
13773 Ok(offset_page(
13774 tasks,
13775 numeric_cursor(page.cursor.as_ref())?,
13776 page.limit.min(MAX_PAGE_SIZE) as usize,
13777 ))
13778 }
13779 async fn query_projects(
13780 &self,
13781 query: &ProjectQuery,
13782 page: &PageRequest,
13783 ) -> Result<Page<Project>, SourceError> {
13784 validate_page(page)?;
13785 refuse_unsearchable_text(query.text.as_ref())?;
13786 // The projects a board holds are found by an issue search scoped to that board,
13787 // never by walking the board's own item connection: what tells a project from a
13788 // task is the `parent` each issue carries, which costs nothing to read. A query
13789 // carrying a text asks that search for the text too, so it reads the issues that
13790 // hold it rather than every issue of the board.
13791 let held = match self.text_searched(query.text.as_ref()).await? {
13792 Some(searched) => searched,
13793 None => self.board_issues().await?,
13794 };
13795 let projects = held
13796 .iter()
13797 .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
13798 .map(Resolved::project)
13799 .filter(|project| project_matches(project, query))
13800 .collect();
13801 Ok(offset_page(
13802 projects,
13803 numeric_cursor(page.cursor.as_ref())?,
13804 page.limit.min(MAX_PAGE_SIZE) as usize,
13805 ))
13806 }
13807 async fn query_projects_by_origin(
13808 &self,
13809 origin: &str,
13810 page: &PageRequest,
13811 ) -> Result<Page<Project>, SourceError> {
13812 validate_page(page)?;
13813 let projects = self
13814 .copied_from(origin, BoardKind::Work(ItemKind::Project))
13815 .await?
13816 .iter()
13817 .map(Resolved::project)
13818 .collect();
13819 Ok(offset_page(
13820 projects,
13821 numeric_cursor(page.cursor.as_ref())?,
13822 page.limit.min(MAX_PAGE_SIZE) as usize,
13823 ))
13824 }
13825 async fn get_document(&self, id: &NativeId) -> Result<Option<Document>, SourceError> {
13826 let Some(item) = self
13827 .document_by_id(id)
13828 .await?
13829 .filter(|item| item.kind == BoardKind::Document)
13830 else {
13831 return Ok(None);
13832 };
13833 self.expected.read_document(&item);
13834 Ok(Some(item.document()))
13835 }
13836 async fn query_documents_by_origin(
13837 &self,
13838 origin: &str,
13839 page: &PageRequest,
13840 ) -> Result<Page<Document>, SourceError> {
13841 validate_page(page)?;
13842 let documents = self
13843 .copied_from(origin, BoardKind::Document)
13844 .await?
13845 .iter()
13846 .map(Resolved::document)
13847 .collect();
13848 Ok(offset_page(
13849 documents,
13850 numeric_cursor(page.cursor.as_ref())?,
13851 page.limit.min(MAX_PAGE_SIZE) as usize,
13852 ))
13853 }
13854 async fn query_documents(
13855 &self,
13856 query: &DocumentQuery,
13857 page: &PageRequest,
13858 ) -> Result<Page<Document>, SourceError> {
13859 validate_page(page)?;
13860 // Narrowed to one project, this is the same sub-issue read a task list scoped to
13861 // that project makes — a document filed under a project is a sub-issue of it too,
13862 // and which of them come back is the kind this caller asked for. Unscoped, a query
13863 // carrying a text asks the board-scoped issue search for it, as a task query does,
13864 // and only one carrying none reads the board.
13865 let (held, membership) = match &query.project {
13866 ProjectFilter::Is(project) => (
13867 self.project_children(project).await?,
13868 // Answered by where these items came from; see `task_matches`.
13869 &ProjectFilter::Any,
13870 ),
13871 ProjectFilter::Any | ProjectFilter::Orphans => {
13872 refuse_unsearchable_text(query.text.as_ref())?;
13873 match self.text_searched(query.text.as_ref()).await? {
13874 Some(searched) => (searched, &query.project),
13875 None => (self.board().await?.items, &query.project),
13876 }
13877 }
13878 };
13879 // Filtered before paged, exactly as a task read is: a page of a filtered result is
13880 // a page of the survivors, never the survivors of a page.
13881 let documents = held
13882 .iter()
13883 .filter(|item| item.kind == BoardKind::Document)
13884 .map(Resolved::document)
13885 .filter(|document| document_matches(document, query, membership))
13886 .collect();
13887 Ok(offset_page(
13888 documents,
13889 numeric_cursor(page.cursor.as_ref())?,
13890 page.limit.min(MAX_PAGE_SIZE) as usize,
13891 ))
13892 }
13893 async fn labels(&self, page: &PageRequest) -> Result<Page<Label>, SourceError> {
13894 validate_page(page)?;
13895 let offset = numeric_cursor(page.cursor.as_ref())?;
13896 let mut labels = self
13897 .board()
13898 .await?
13899 .items
13900 .into_iter()
13901 .flat_map(|item| item.labels)
13902 .fold(Vec::new(), |mut all, label| {
13903 if !all.iter().any(|x: &Label| x.id == label.id) {
13904 all.push(label);
13905 }
13906 all
13907 });
13908 labels.sort_by(|a, b| a.name.cmp(&b.name).then(a.id.0.cmp(&b.id.0)));
13909 Ok(offset_page(
13910 labels,
13911 offset,
13912 page.limit.min(MAX_PAGE_SIZE) as usize,
13913 ))
13914 }
13915 async fn task_dependencies(
13916 &self,
13917 id: &NativeId,
13918 direction: Direction,
13919 page: &PageRequest,
13920 ) -> Result<Page<DependencyEdge>, SourceError> {
13921 self.dependencies(id, ItemKind::Task, direction, page).await
13922 }
13923 async fn project_dependencies(
13924 &self,
13925 id: &NativeId,
13926 direction: Direction,
13927 page: &PageRequest,
13928 ) -> Result<Page<DependencyEdge>, SourceError> {
13929 self.dependencies(id, ItemKind::Project, direction, page)
13930 .await
13931 }
13932
13933 fn writes(&self) -> WriteSupport {
13934 WriteSupport::Supported
13935 }
13936
13937 /// Create or update one task.
13938 ///
13939 /// Its `delivers` and `delivered_by` are checked before anything is read or written —
13940 /// neither may name the task itself or name one task twice — and land in the body's
13941 /// metadata slot under their reserved keys, in place of any caller metadata of those
13942 /// names.
13943 async fn write_task(&self, write: &ItemWrite<Task>) -> Result<NativeId, SourceError> {
13944 self.write_task_assets(write, None)
13945 .await
13946 .map(|written| written.id)
13947 }
13948
13949 async fn read_ahead_origins_for_copy(&self, origins: &[String]) -> Result<(), SourceError> {
13950 self.origins_ahead(origins).await
13951 }
13952
13953 async fn member_classifications(
13954 &self,
13955 project: &NativeId,
13956 ) -> Result<Option<Vec<(Classification, Vec<Repository>)>>, SourceError> {
13957 Ok(self
13958 .member_classes
13959 .lock()
13960 .map_err(|_| SourceError::Unavailable {
13961 message: "a project's members held for its classification were left \
13962 inconsistent; run the command again"
13963 .into(),
13964 })?
13965 .remove(project))
13966 }
13967
13968 /// The issue's own web address, as the `createIssue` that made it in this command answered
13969 /// it: a task, a project and a document are each an issue here.
13970 fn written_location(
13971 &self,
13972 _record: onetaskgraph_plugin_api::MetadataRecord,
13973 id: &NativeId,
13974 ) -> Option<Location> {
13975 self.written
13976 .lock()
13977 .unwrap_or_else(std::sync::PoisonError::into_inner)
13978 .get(id)
13979 .cloned()
13980 .map(Location::Url)
13981 }
13982
13983 /// [`MUTATION_BATCH`], while writes are held back: [`graphql::CREATE_ISSUES`] creates that many
13984 /// in one request, and a copy's tasks are created together only then, since their fields
13985 /// and edges are what is held back.
13986 fn create_batch_limit(&self) -> usize {
13987 if self.deferring().unwrap_or(false) {
13988 MUTATION_BATCH
13989 } else {
13990 1
13991 }
13992 }
13993
13994 // 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.
13995 async fn write_tasks(&self, writes: &[ItemWrite<Task>]) -> Result<Vec<NativeId>, SourceError> {
13996 if let Some(write) = writes.iter().find(|write| write.target.is_some()) {
13997 return Err(SourceError::Refused {
13998 message: format!(
13999 "write_tasks creates tasks, and the write of {} names the existing task {}; \
14000 next: update it with write_task",
14001 write.item.id,
14002 write.target.as_ref().map_or("", |target| target.0.as_str())
14003 ),
14004 });
14005 }
14006 if !self.deferring()? {
14007 let mut created = Vec::with_capacity(writes.len());
14008 for write in writes {
14009 match self.write_task(write).await {
14010 Ok(id) => created.push(id),
14011 Err(error) => {
14012 let mut left: Vec<&str> = Vec::new();
14013 for id in &created {
14014 if self.delete_issue(id).await.is_err() {
14015 left.push(id.0.as_str());
14016 }
14017 }
14018 return Err(left_behind(error, &left));
14019 }
14020 }
14021 }
14022 return Ok(created);
14023 }
14024 self.create_together(writes).await
14025 }
14026
14027 fn begin_deferred_writes(&self) {
14028 if let Ok(mut deferred) = self.deferred.lock() {
14029 deferred.get_or_insert_with(Deferred::default);
14030 }
14031 }
14032
14033 async fn finish_writes(&self) -> Result<(), SourceError> {
14034 self.send_held_updates().await?;
14035 self.send_held_back().await
14036 }
14037
14038 async fn write_task_with_assets(
14039 &self,
14040 write: &ItemWrite<Task>,
14041 _answers: Option<&BTreeMap<String, Value>>,
14042 assets: &onetaskgraph_plugin_api::AssetWrite,
14043 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
14044 self.write_task_assets(write, Some(assets)).await
14045 }
14046
14047 async fn write_project(&self, write: &ItemWrite<Project>) -> Result<NativeId, SourceError> {
14048 self.write_item(
14049 &Incoming {
14050 written: Written::Work(ItemKind::Project, &write.item.status),
14051 title: &write.item.title,
14052 content: write.item.content.as_deref(),
14053 assets: None,
14054 labels: &write.item.labels,
14055 metadata: &write.item.metadata,
14056 repositories: &write.item.repositories,
14057 classification: write.item.classification,
14058 parent: None,
14059 delivers: &[],
14060 delivered_by: &[],
14061 priority: None,
14062 },
14063 write.target.as_ref(),
14064 &write.depends_on,
14065 )
14066 .await
14067 .map(|written| written.id)
14068 }
14069
14070 /// Create or update one document, which is one issue titled the way this board spells
14071 /// a document.
14072 ///
14073 /// Everything else is exactly a task write: caller metadata goes to the same canonical
14074 /// JSON slot at the end of the body and comes back with its JSON types intact, a key
14075 /// or a field this board cannot carry is refused by name rather than dropped, a target
14076 /// naming an issue this board does not hold is refused rather than created, and an
14077 /// issue this call created is taken back when the rest of the write fails.
14078 async fn write_document(&self, write: &ItemWrite<Document>) -> Result<NativeId, SourceError> {
14079 self.write_document_assets(write, None)
14080 .await
14081 .map(|written| written.id)
14082 }
14083
14084 async fn write_document_with_assets(
14085 &self,
14086 write: &ItemWrite<Document>,
14087 _answers: Option<&BTreeMap<String, Value>>,
14088 assets: &onetaskgraph_plugin_api::AssetWrite,
14089 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
14090 self.write_document_assets(write, Some(assets)).await
14091 }
14092
14093 /// Refused exactly as the write refuses it, from what the write reads: the mapping first,
14094 /// which reads nothing; then the board's `Status` option. Over an existing item that is
14095 /// read off the item, as the write reads it, and the item is held among this command's
14096 /// resolved records so the write that follows reuses that read rather than repeating it;
14097 /// an item that does not carry the field takes the board's fields, which are held once
14098 /// read. A create is checked against the board's fields only when this command already
14099 /// holds them, because a create reads them together with its repository, in one request,
14100 /// and refuses a missing option before it writes anything.
14101 async fn check_status_write(
14102 &self,
14103 kind: ItemKind,
14104 category: StatusCategory,
14105 target: Option<&NativeId>,
14106 ) -> Result<(), SourceError> {
14107 let status = self.resolved_target(kind, category)?;
14108 if status.option().is_none() {
14109 return Ok(());
14110 }
14111 let fields = match target {
14112 Some(target) => {
14113 // A target this board does not hold is the write's own refusal to make.
14114 let Some(item) = self.bound_item(target).await? else {
14115 return Ok(());
14116 };
14117 self.resolved_cache()?.insert(target.clone(), item.clone());
14118 // Only the `Status` field decides whether the write can land.
14119 self.fields_for(Some(&item), false, true, false)
14120 .await?
14121 .fields
14122 }
14123 None => {
14124 let held = self
14125 .board_cache()?
14126 .as_ref()
14127 .map(|board| board.fields.clone());
14128 match held.or_else(|| {
14129 self.fields_cache()
14130 .ok()
14131 .and_then(|cache| cache.as_ref().map(|board| board.fields.clone()))
14132 }) {
14133 Some(fields) => fields,
14134 None => return Ok(()),
14135 }
14136 }
14137 };
14138 self.column_for(&fields, kind, category, &status)
14139 .map(|_| ())
14140 }
14141
14142 /// What a write of `status` reads back as: the `Status` option its category maps to for
14143 /// `kind`, and for a closing category the closure that option and its fixed reason read
14144 /// as — exactly what [`BoardStatuses::status`] reports of the item once written. A category
14145 /// this instance has no option for is answered as given, since writing it is refused.
14146 fn status_as_written(&self, kind: ItemKind, status: &Status) -> Status {
14147 match self.statuses.target(kind, status.category) {
14148 StatusTarget::Column(name) => {
14149 self.statuses.status(kind, Some(name.as_str()), false, None)
14150 }
14151 StatusTarget::Terminal(name, reason) => {
14152 self.statuses
14153 .status(kind, Some(name.as_str()), true, Some(reason.reason()))
14154 }
14155 StatusTarget::Disabled(_) => status.clone(),
14156 }
14157 }
14158
14159 /// Set one task's status alone.
14160 ///
14161 /// An open target reopens a closed issue with an `updateIssue` carrying only its
14162 /// `stateInput`, then selects the board option with `updateProjectV2ItemFieldValue`; a
14163 /// terminal target selects its mapped option, then closes with its fixed reason. No
14164 /// request carries a title, a body or a label. The status
14165 /// answered is what [`BoardStatuses::status`] reads off the state just written, which is
14166 /// what a re-read reports.
14167 async fn set_task_status(
14168 &self,
14169 id: &NativeId,
14170 category: StatusCategory,
14171 ) -> Result<Option<Status>, SourceError> {
14172 self.set_status(id, ItemKind::Task, category).await
14173 }
14174
14175 /// Set one task's status and answer the task with it, reading the item once: the read the
14176 /// task is answered from is handed to the status write, which would otherwise read the
14177 /// same issue again — two requests on a fresh process, the read and the field write,
14178 /// where the default's `get_task` before `set_task_status` sent three.
14179 async fn set_task_status_reading(
14180 &self,
14181 id: &NativeId,
14182 category: StatusCategory,
14183 ) -> Result<Option<Task>, SourceError> {
14184 // Refused before anything is read, in the words the write itself refuses it.
14185 self.resolved_target(ItemKind::Task, category)?;
14186 let Some(item) = self
14187 .bound_item(id)
14188 .await?
14189 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
14190 else {
14191 return Ok(None);
14192 };
14193 let mut task = item.task()?;
14194 self.held_cache
14195 .lock()
14196 .map_err(|_| SourceError::Unavailable {
14197 message: "items read by issue reference were left inconsistent; run the \
14198 command again"
14199 .into(),
14200 })?
14201 .insert(id.clone(), item);
14202 let Some(status) = self.set_status(id, ItemKind::Task, category).await? else {
14203 return Ok(None);
14204 };
14205 task.status = status;
14206 Ok(Some(task))
14207 }
14208
14209 /// Set one project's status alone, by exactly the paired write a task's status takes, for
14210 /// the project kind: a terminal category selects the project kind's mapped option and then
14211 /// closes the issue with the category's fixed reason — an issue already closed with a
14212 /// reason that reads that category is left as it is — and an open category reopens a closed
14213 /// issue and then selects its option. A board draft is refused a terminal category as a
14214 /// task's is.
14215 async fn set_project_status(
14216 &self,
14217 id: &NativeId,
14218 category: StatusCategory,
14219 ) -> Result<Option<Status>, SourceError> {
14220 self.set_status(id, ItemKind::Project, category).await
14221 }
14222
14223 /// Every task and project whose card contradicts its issue's closure, read through the
14224 /// board's own `is:closed` filter; see [`TaskSource::status_contradictions`].
14225 async fn status_contradictions(&self) -> Result<Vec<StatusContradiction>, SourceError> {
14226 self.contradictions().await
14227 }
14228
14229 /// What this board holds for each issue reference, one request per fifty of them; see
14230 /// [`TaskSource::issue_holdings`].
14231 async fn issue_holdings(&self, issues: &[IssueRef]) -> Result<Vec<IssueHolding>, SourceError> {
14232 self.holdings(issues).await
14233 }
14234
14235 /// Set one task's priority alone: one `updateProjectV2ItemFieldValue` selecting the
14236 /// mapped option of the board's `Priority` field, or one `clearProjectV2ItemFieldValue`
14237 /// for `none`. Refused by an instance with no `priority_mapping`.
14238 async fn set_task_priority(
14239 &self,
14240 id: &NativeId,
14241 priority: Priority,
14242 ) -> Result<Option<Priority>, SourceError> {
14243 self.set_priority(id, priority).await
14244 }
14245
14246 /// Replace one task's content with a single body update that keeps the metadata slot
14247 /// byte for byte.
14248 async fn set_task_content(
14249 &self,
14250 id: &NativeId,
14251 content: &str,
14252 ) -> Result<Option<()>, SourceError> {
14253 self.replace_content(id, content).await
14254 }
14255
14256 /// Replace one task issue's content and its provenance slot entry with a single body
14257 /// update. The answers are not kept: see `replace_rendering`.
14258 async fn set_task_rendering(
14259 &self,
14260 id: &NativeId,
14261 content: &str,
14262 provenance: &Value,
14263 _answers: &BTreeMap<String, Value>,
14264 ) -> Result<Option<()>, SourceError> {
14265 self.replace_rendering(
14266 id,
14267 BoardKind::Work(ItemKind::Task),
14268 content,
14269 provenance,
14270 None,
14271 )
14272 .await
14273 .map(|written| written.map(|_| ()))
14274 }
14275
14276 /// Replace one design-document issue's content and its provenance slot entry, on exactly
14277 /// the terms of [`set_task_rendering`](TaskSource::set_task_rendering).
14278 async fn set_document_rendering(
14279 &self,
14280 id: &NativeId,
14281 content: &str,
14282 provenance: &Value,
14283 _answers: &BTreeMap<String, Value>,
14284 ) -> Result<Option<()>, SourceError> {
14285 self.replace_rendering(id, BoardKind::Document, content, provenance, None)
14286 .await
14287 .map(|written| written.map(|_| ()))
14288 }
14289
14290 /// Replace one project issue's content and its provenance slot entry, on exactly the
14291 /// terms of [`set_task_rendering`](TaskSource::set_task_rendering).
14292 async fn set_project_rendering(
14293 &self,
14294 id: &NativeId,
14295 content: &str,
14296 provenance: &Value,
14297 _answers: &BTreeMap<String, Value>,
14298 ) -> Result<Option<()>, SourceError> {
14299 self.replace_rendering(
14300 id,
14301 BoardKind::Work(ItemKind::Project),
14302 content,
14303 provenance,
14304 None,
14305 )
14306 .await
14307 .map(|written| written.map(|_| ()))
14308 }
14309
14310 /// Apply a targeted update with one read of the item and a write only for what differs:
14311 /// the `Status` and `Priority` field writes in one request, the `blockedBy` difference,
14312 /// and last one `updateIssue` for title, body and state. See `targeted_update`.
14313 async fn update_task(
14314 &self,
14315 id: &NativeId,
14316 update: &TaskUpdate,
14317 ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
14318 self.targeted_update(id, update).await
14319 }
14320
14321 /// Replace one task's `delivered_by` with a single body update that changes the
14322 /// metadata slot and nothing outside it.
14323 async fn set_delivered_by(
14324 &self,
14325 id: &NativeId,
14326 delivered_by: &[TaskRef],
14327 ) -> Result<Option<()>, SourceError> {
14328 self.replace_delivered_by(id, delivered_by).await
14329 }
14330
14331 /// Set one key of one task issue's metadata with a single body update that changes the
14332 /// metadata slot and nothing outside it — no title, label, state or board field request —
14333 /// and sends nothing when the task already holds that value under the key.
14334 async fn set_task_metadata(
14335 &self,
14336 id: &NativeId,
14337 key: &MetadataKey,
14338 value: &Value,
14339 ) -> Result<Option<Task>, SourceError> {
14340 Ok(self
14341 .set_slot_key(id, BoardKind::Work(ItemKind::Task), key, value)
14342 .await?
14343 .map(|item| item.task())
14344 .transpose()?)
14345 }
14346
14347 /// Set one key of one project issue's metadata, on exactly the terms of
14348 /// [`set_task_metadata`](TaskSource::set_task_metadata).
14349 async fn set_project_metadata(
14350 &self,
14351 id: &NativeId,
14352 key: &MetadataKey,
14353 value: &Value,
14354 ) -> Result<Option<Project>, SourceError> {
14355 Ok(self
14356 .set_slot_key(id, BoardKind::Work(ItemKind::Project), key, value)
14357 .await?
14358 .map(|item| item.project()))
14359 }
14360
14361 /// Set one key of one design-document issue's metadata, on exactly the terms of
14362 /// [`set_task_metadata`](TaskSource::set_task_metadata).
14363 async fn set_document_metadata(
14364 &self,
14365 id: &NativeId,
14366 key: &MetadataKey,
14367 value: &Value,
14368 ) -> Result<Option<Document>, SourceError> {
14369 Ok(self
14370 .set_slot_key(id, BoardKind::Document, key, value)
14371 .await?
14372 .map(|item| item.document()))
14373 }
14374
14375 /// Take one key out of one issue's metadata slot with a single body update that changes the
14376 /// slot and nothing outside it, sending nothing when the slot does not hold the key.
14377 async fn remove_record_metadata(
14378 &self,
14379 record: onetaskgraph_plugin_api::MetadataRecord,
14380 id: &NativeId,
14381 key: &MetadataKey,
14382 ) -> Result<Option<Option<String>>, SourceError> {
14383 let kind = match record {
14384 onetaskgraph_plugin_api::MetadataRecord::Task => BoardKind::Work(ItemKind::Task),
14385 onetaskgraph_plugin_api::MetadataRecord::Project => BoardKind::Work(ItemKind::Project),
14386 onetaskgraph_plugin_api::MetadataRecord::Document => BoardKind::Document,
14387 };
14388 let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
14389 return Ok(None);
14390 };
14391 if item.slot.contains_key(key.as_str()) {
14392 let mut slot = item.slot.clone();
14393 slot.remove(key.as_str());
14394 self.write_slot(&mut item, &slot).await?;
14395 self.remember_written(item.clone(), false)?;
14396 }
14397 Ok(Some(match record {
14398 onetaskgraph_plugin_api::MetadataRecord::Task => item.task()?.version,
14399 onetaskgraph_plugin_api::MetadataRecord::Project => item.project().version,
14400 onetaskgraph_plugin_api::MetadataRecord::Document => item.document().version,
14401 }))
14402 }
14403
14404 async fn delete_task(&self, id: &NativeId) -> Result<(), SourceError> {
14405 self.delete_item(id).await
14406 }
14407
14408 async fn delete_project(&self, id: &NativeId) -> Result<(), SourceError> {
14409 self.delete_item(id).await
14410 }
14411
14412 async fn delete_document(&self, id: &NativeId) -> Result<(), SourceError> {
14413 self.delete_item(id).await
14414 }
14415
14416 /// One page of the task issue's own comments, walked by GitHub's own cursor.
14417 ///
14418 /// Nothing here filters, so nothing has to be read ahead of the page: the caller's limit is
14419 /// the page GitHub is asked for and GitHub's `endCursor` is the cursor handed back.
14420 ///
14421 /// One request, [`graphql::ISSUE_DETAIL`]: the read that says the id names a task of this
14422 /// board is the read of its comments. A draft this process already resolved is refused
14423 /// without one.
14424 async fn task_comments(
14425 &self,
14426 task: &NativeId,
14427 page: &PageRequest,
14428 ) -> Result<Option<Page<Comment>>, SourceError> {
14429 validate_page(page)?;
14430 let cached = self.resolved_cache()?.get(task).cloned();
14431 if let Some(item) = cached {
14432 if item.kind != BoardKind::Work(ItemKind::Task) {
14433 return Ok(None);
14434 }
14435 if item.content_kind == ContentKind::DraftIssue {
14436 return Err(self.draft_has_no_comments(task));
14437 }
14438 }
14439 match self.issue_detail(task, page).await? {
14440 Some(TaskDetailRead {
14441 comments: Some(comments),
14442 ..
14443 }) => comments,
14444 _ => Ok(None),
14445 }
14446 }
14447
14448 /// Every id's task, with the first page of its comments when `comments` names it:
14449 /// [`DETAIL_BATCH`] items per [`graphql::ISSUE_DETAILS`] request, and one item with its
14450 /// comments in one [`graphql::ISSUE_DETAIL`] request.
14451 async fn get_task_details(
14452 &self,
14453 ids: &[NativeId],
14454 comments: Option<&PageRequest>,
14455 ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
14456 if let Some(page) = comments
14457 && let Err(error) = validate_page(page)
14458 {
14459 return ids.iter().map(|_| Err(error.clone())).collect();
14460 }
14461 match (ids, comments) {
14462 ([id], Some(page)) => vec![self.issue_detail(id, page).await],
14463 ([id], None) => vec![self.task_read(id).await],
14464 _ => self.issue_details(ids, comments).await,
14465 }
14466 }
14467
14468 /// Every id's task with the first page of its comments and its `blockedBy`, as a copy reads
14469 /// the tasks it carries: [`COPY_DETAIL_BATCH`] items per [`graphql::ISSUE_COPY_DETAILS`]
14470 /// request, so the dependency read a copy makes of each next is answered from this one.
14471 async fn get_task_details_for_copy(
14472 &self,
14473 ids: &[NativeId],
14474 comments: &PageRequest,
14475 ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
14476 // A page past the first is a page of one task's comments, which the detail read of it
14477 // alone answers.
14478 if comments.cursor.is_some() {
14479 return self.get_task_details(ids, Some(comments)).await;
14480 }
14481 if let Err(error) = validate_page(comments) {
14482 return ids.iter().map(|_| Err(error.clone())).collect();
14483 }
14484 let mut read = Vec::with_capacity(ids.len());
14485 for batch in ids.chunks(COPY_DETAIL_BATCH) {
14486 match self
14487 .graphql(
14488 graphql::ISSUE_COPY_DETAILS,
14489 copy_detail_batch(batch, comments, false),
14490 )
14491 .await
14492 {
14493 Ok(data) => {
14494 for (slot, id) in batch.iter().enumerate() {
14495 let read_one = match data.get(format!("i{slot}")) {
14496 Some(node) => self.detail_of(id, node, true, None).await,
14497 None => Err(SourceError::Malformed {
14498 message: format!(
14499 "GitHub answered a batch read with no item for {}",
14500 id.0
14501 ),
14502 }),
14503 };
14504 read.push(read_one);
14505 }
14506 }
14507 Err(error) if unresolvable_node(&error) => {
14508 for id in batch {
14509 read.push(self.issue_detail(id, comments).await);
14510 }
14511 }
14512 Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
14513 }
14514 }
14515 read
14516 }
14517
14518 /// Every id's item, whatever kind it is, read [`COPY_DETAIL_BATCH`] at a time with
14519 /// [`graphql::ISSUE_COPY_DETAILS`] — the document a copy reads the tasks it carries with —
14520 /// and held for the next read of each by id; see `read_ahead`.
14521 ///
14522 /// What this run wrote is never read: its own record answers, as it does for a read by id.
14523 /// Nor is an item a listing of its project carried whole earlier in this command — see
14524 /// `carried` — which is held in place of reading it.
14525 /// A batch GitHub refuses because one of its ids resolves to no node at all holds nothing,
14526 /// so each of its ids is read on its own and answered as that read answers; a draft, and an
14527 /// answer naming another issue than the one asked for, are left to their own reads too.
14528 async fn read_ahead_for_copy(&self, ids: &[NativeId]) -> Result<(), SourceError> {
14529 // The origins asked just before go out now, beside the members of the one project this
14530 // names when it names one; a project those origins answered whole is held for its next
14531 // read by id rather than read again here.
14532 let hinted = match ids {
14533 [one] => Some(one),
14534 _ => None,
14535 };
14536 self.send_pending_origins(hinted).await?;
14537 if let Some(project) = hinted {
14538 let answered = self.resolved_cache()?.get(project).cloned();
14539 if let Some(answered) = answered
14540 && answered.kind == BoardKind::Work(ItemKind::Project)
14541 {
14542 self.read_ahead()?.insert(project.clone(), Some(answered));
14543 return Ok(());
14544 }
14545 }
14546 let mut unread: Vec<NativeId> = Vec::new();
14547 // Read whole by a listing of its project in this command, which a mutation naming it
14548 // would have dropped: held as a batch would have held it, once every batch is answered.
14549 let mut listed: Vec<NativeId> = Vec::new();
14550 {
14551 let created = self.created()?;
14552 let held = self.read_ahead()?;
14553 let carried = self.carried()?;
14554 for id in ids {
14555 if unread.contains(id)
14556 || listed.contains(id)
14557 || held.contains_key(id)
14558 || created.iter().any(|own| own.id == *id)
14559 {
14560 continue;
14561 }
14562 // An issue a listing carried without the whole of what blocks it is read in a
14563 // batch too, which carries that, rather than having its dependency read sent
14564 // on its own once the copy reconciles its edges.
14565 let whole = carried.get(id).is_some_and(|item| {
14566 item.content_kind != ContentKind::Issue || item.blocked_by.is_some()
14567 });
14568 if whole {
14569 listed.push(id.clone());
14570 } else {
14571 unread.push(id.clone());
14572 }
14573 }
14574 }
14575 // Nothing of an item's comments is wanted here: the smallest page keeps the read to
14576 // what the item itself costs.
14577 let comments = PageRequest {
14578 cursor: None,
14579 limit: 1,
14580 };
14581 // Held only once every batch has been answered, so a read-ahead that fails part way
14582 // holds nothing — what a listing carried included, which stays carried — and every
14583 // item is read on its own, as the method promises.
14584 let mut read: Vec<(NativeId, Option<Resolved>)> = Vec::new();
14585 // The issues the first id names as sub-issues, as `ISSUE_COPY_DETAILS`'s `children`
14586 // answered them: what a project copy's orphan check asks, when that first id is its
14587 // project.
14588 let mut children: Option<(NativeId, Vec<(String, String)>)> = None;
14589 for (index, batch) in unread.chunks(COPY_DETAIL_BATCH).enumerate() {
14590 let data = match self
14591 .graphql(
14592 graphql::ISSUE_COPY_DETAILS,
14593 copy_detail_batch(batch, &comments, index == 0),
14594 )
14595 .await
14596 {
14597 Ok(data) => data,
14598 Err(error) if unresolvable_node(&error) => continue,
14599 Err(error) => return Err(error),
14600 };
14601 for (slot, id) in batch.iter().enumerate() {
14602 let Some(node) = data.get(format!("i{slot}")) else {
14603 continue;
14604 };
14605 let held = if node.is_null() {
14606 None
14607 } else {
14608 match optional_str(node, "__typename")? {
14609 Some("DraftIssue") => continue,
14610 Some("Issue") if required_str(node, "id")? != id.0 => continue,
14611 _ => {}
14612 }
14613 // Remembered below with the rest, so a later batch refused leaves no
14614 // dependency read answered from this one either.
14615 self.resolve_issue_as(node, false).await?
14616 };
14617 read.push((id.clone(), held));
14618 }
14619 if index == 0
14620 && let Some(node) = data.get("children").filter(|node| !node.is_null())
14621 && node.get("id").and_then(Value::as_str) == batch.first().map(|id| id.0.as_str())
14622 && node.pointer("/subIssues/pageInfo/hasNextPage") == Some(&Value::Bool(false))
14623 {
14624 // A list GitHub did not answer is not an empty one: it would answer the project's
14625 // task listing with nothing, and the orphan check with no orphans.
14626 let listed = node
14627 .pointer("/subIssues/nodes")
14628 .and_then(Value::as_array)
14629 .ok_or_else(|| SourceError::Malformed {
14630 message: format!(
14631 "GitHub answered the sub-issues of project {} with no list of them",
14632 batch[0].0
14633 ),
14634 })?
14635 .iter()
14636 .map(|child| {
14637 Ok((
14638 required_str(child, "id")?.to_owned(),
14639 required_str(child, "title")?.to_owned(),
14640 ))
14641 })
14642 .collect::<Result<Vec<_>, SourceError>>()?;
14643 children = Some((batch[0].clone(), listed));
14644 }
14645 }
14646 // The project's tasks are known from this read alone when every child it named is held
14647 // from it, but for a document, which its title says is one and which a task listing
14648 // never answers: then a listing of the project's tasks is answered from them.
14649 if let Some((project, listed)) = children
14650 && read.iter().any(|(id, held)| {
14651 *id == project
14652 && held
14653 .as_ref()
14654 .is_some_and(|held| held.kind == BoardKind::Work(ItemKind::Project))
14655 })
14656 {
14657 let mut tasks = Vec::new();
14658 let mut whole = true;
14659 for (child, title) in &listed {
14660 match read.iter().find(|(id, _)| id.0 == *child) {
14661 Some((_, Some(held))) if held.parent.as_ref() == Some(&project) => {
14662 if held.kind == BoardKind::Work(ItemKind::Task) {
14663 tasks.push(held.clone());
14664 }
14665 }
14666 _ if title.starts_with(DESIGN_TITLE_PREFIX) => {}
14667 _ => whole = false,
14668 }
14669 }
14670 if whole {
14671 self.task_children()?.insert(project, tasks);
14672 }
14673 }
14674 let mut resolved = self.resolved_cache()?;
14675 let mut ahead = self.read_ahead()?;
14676 let mut carried = self.carried()?;
14677 for item in read.iter().filter_map(|(_, held)| held.as_ref()) {
14678 resolved.insert(item.id.clone(), item.clone());
14679 }
14680 // Taken only if still carried: a mutation naming it while the batches ran dropped it,
14681 // and its next read is then its own.
14682 for id in &listed {
14683 let Some(item) = carried.remove(id) else {
14684 continue;
14685 };
14686 // The whole of what it is blocked by came with it, so the dependency read a copy
14687 // makes next is answered from this one, as a copy's detail read is.
14688 if item.blocked_by.is_some() {
14689 resolved.insert(item.id.clone(), item.clone());
14690 }
14691 read.push((id.clone(), Some(item)));
14692 }
14693 for (id, held) in read {
14694 ahead.insert(id, held);
14695 }
14696 Ok(())
14697 }
14698
14699 /// Add one comment to the task's issue, as the account the token belongs to.
14700 ///
14701 /// The author is refused before anything is sent — not even the task is read — because
14702 /// no answer GitHub could give would make posting under another name than the one asked
14703 /// for the right outcome.
14704 async fn add_comment(
14705 &self,
14706 task: &NativeId,
14707 comment: &NewComment,
14708 ) -> Result<Option<Comment>, SourceError> {
14709 if let Some(author) = &comment.author {
14710 return Err(SourceError::Refused {
14711 message: format!(
14712 "source {} cannot post a comment as {author:?}: GitHub records the account \
14713 the token signs in as the author of every comment; next: leave --author \
14714 out, and the comment is posted as that account",
14715 self.name
14716 ),
14717 });
14718 }
14719 let Some(issue) = self.commented_issue(task).await? else {
14720 return Ok(None);
14721 };
14722 let data = self
14723 .graphql(
14724 graphql::ADD_COMMENT,
14725 json!({"input":{"subjectId":issue.0,"body":comment.body.as_str()}}),
14726 )
14727 .await?;
14728 let subject = data
14729 .pointer("/addComment/subject")
14730 .filter(|value| !value.is_null())
14731 .ok_or_else(|| SourceError::Malformed {
14732 message: "GitHub comment addition returned no subject".into(),
14733 })?;
14734 if required_str(subject, "id")? != issue.0 {
14735 return Err(SourceError::Malformed {
14736 message: "GitHub comment addition answered about another issue".into(),
14737 });
14738 }
14739 let added = data
14740 .pointer("/addComment/commentEdge/node")
14741 .filter(|value| !value.is_null())
14742 .ok_or_else(|| SourceError::Malformed {
14743 message: "GitHub comment addition returned no comment".into(),
14744 })?;
14745 let added = comment_from(added)?;
14746 self.remember_commented(&issue)?;
14747 Ok(Some(added))
14748 }
14749
14750 async fn edit_comment(
14751 &self,
14752 task: &NativeId,
14753 comment: &NativeId,
14754 body: &CommentBody,
14755 ) -> Result<Option<Comment>, SourceError> {
14756 let Some(issue) = self.commented_issue(task).await? else {
14757 return Ok(None);
14758 };
14759 let Some(held) = self.comment_on(&issue, comment).await? else {
14760 return Ok(None);
14761 };
14762 // A comment a copy wrote keeps its origin through an edit: its marker is carried over.
14763 let body = CommentOrigin::kept(&held, body)?;
14764 let data = self
14765 .graphql(
14766 graphql::UPDATE_COMMENT,
14767 json!({"input":{"id":comment.0,"body":body.as_str()}}),
14768 )
14769 .await?;
14770 let edited = data
14771 .pointer("/updateIssueComment/issueComment")
14772 .filter(|value| !value.is_null())
14773 .ok_or_else(|| SourceError::Malformed {
14774 message: "GitHub comment update returned no comment".into(),
14775 })?;
14776 let edited = comment_from(edited)?;
14777 if edited.id != *comment {
14778 return Err(SourceError::Malformed {
14779 message: "GitHub comment update returned the wrong comment".into(),
14780 });
14781 }
14782 self.remember_commented(&issue)?;
14783 Ok(Some(edited))
14784 }
14785
14786 async fn delete_comment(
14787 &self,
14788 task: &NativeId,
14789 comment: &NativeId,
14790 ) -> Result<Option<NativeId>, SourceError> {
14791 let Some(issue) = self.commented_issue(task).await? else {
14792 return Ok(None);
14793 };
14794 if !self.comment_is_on(&issue, comment).await? {
14795 return Ok(None);
14796 }
14797 let data = self
14798 .graphql(graphql::DELETE_COMMENT, json!({"input":{"id":comment.0}}))
14799 .await?;
14800 // The payload says nothing about the comment it removed, so what is checked is that
14801 // GitHub answered the mutation at all rather than leaving it unanswered.
14802 data.get("deleteIssueComment")
14803 .filter(|value| !value.is_null())
14804 .ok_or_else(|| SourceError::Malformed {
14805 message: "GitHub comment deletion returned no payload".into(),
14806 })?;
14807 Ok(Some(comment.clone()))
14808 }
14809
14810 /// Every request this source has recorded, and what each of GitHub's two budgets was
14811 /// attributed — read off the same accounting the session report is rendered from, so
14812 /// the two cannot count one request two ways.
14813 async fn metering(&self) -> Result<Option<Metering>, SourceError> {
14814 Ok(Some(self.ledger.snapshot().metering()))
14815 }
14816
14817 /// Drop every item, search answer and board read this source holds, so the next command
14818 /// reads the board as a person has since left it.
14819 ///
14820 /// Every one of those is held on the assumption that nothing but this source writes the
14821 /// board while a command runs, which stops being true the moment the command is over: a
14822 /// body a person edited would be overwritten from the record held here, and a card they
14823 /// moved would be read as still where this source left it. The board's own field
14824 /// definitions go too, because a person can add or delete a `Status` option and a write
14825 /// resolved against the held list would not re-read on a miss. What stays is what stays
14826 /// valid in normal use: each repository's node id, which a miss re-reads, the pacing of
14827 /// mutations, which is about GitHub's limiter rather than anybody's work, and the running
14828 /// accounting [`metering`](TaskSource::metering) answers from.
14829 ///
14830 /// Infallible in practice: a lock an earlier failure poisoned is cleared rather than
14831 /// refused, because clearing it is what puts it right.
14832 async fn end_command(&self) -> Result<(), SourceError> {
14833 fn clear<T: Default>(held: &Mutex<T>) {
14834 *held
14835 .lock()
14836 .unwrap_or_else(std::sync::PoisonError::into_inner) = T::default();
14837 held.clear_poison();
14838 }
14839 clear(&self.created);
14840 clear(&self.updated);
14841 clear(&self.commented);
14842 clear(&self.board_cache);
14843 clear(&self.search_cache);
14844 clear(&self.narrowed_cache);
14845 clear(&self.search_next);
14846 clear(&self.resolved_cache);
14847 clear(&self.read_ahead);
14848 clear(&self.pending_origins);
14849 clear(&self.member_classes);
14850 clear(&self.carried);
14851 clear(&self.held_cache);
14852 clear(&self.children_cache);
14853 clear(&self.task_children);
14854 clear(&self.written);
14855 clear(&self.fields_cache);
14856 self.expected.end_command();
14857 Ok(())
14858 }
14859}
14860
14861/// One member of a project as a classification-only read answers it: its classification and
14862/// the repositories its work changes.
14863type MemberClass = (Classification, Vec<Repository>);
14864
14865/// Each project's sub-issues as one command read them, keyed by the selector they were asked
14866/// for under, beside the project that selector named; see `children_cache`.
14867type ProjectChildren = BTreeMap<NativeId, (NativeId, Vec<Resolved>)>;
14868
14869/// One issue comment as the contract carries it.
14870///
14871/// `author` is absent both when GitHub answers `null` for an account that no longer exists
14872/// and when it answers an actor with no login, because either way the source did not say who
14873/// wrote it — which is what an absent author means, rather than an author called nothing.
14874///
14875/// A comment a copy wrote here carries its origin as the hidden marker at the end of its body
14876/// — GitHub sets every comment's author and times itself, so there is nowhere else to keep it —
14877/// and is read back with the marker taken out and reported as its origin.
14878fn comment_from(value: &Value) -> Result<Comment, SourceError> {
14879 Ok(Comment {
14880 origin: None,
14881 id: NativeId(required_str(value, "id")?.to_owned()),
14882 author: optional_str(value.get("author").unwrap_or(&Value::Null), "login")?
14883 .map(str::to_owned),
14884 created_at: optional_time(value, "createdAt")?,
14885 updated_at: optional_time(value, "updatedAt")?,
14886 body: required_str(value, "body")?.to_owned(),
14887 url: optional_str(value, "url")?.map(str::to_owned),
14888 }
14889 .unmarked())
14890}
14891
14892/// The page of comments one issue node carries, resumed from `after`.
14893fn comment_page(
14894 node: &Value,
14895 issue: &str,
14896 after: Option<&str>,
14897) -> Result<Page<Comment>, SourceError> {
14898 let connection = node
14899 .get("comments")
14900 .filter(|value| !value.is_null())
14901 .ok_or_else(|| SourceError::Malformed {
14902 message: format!("GitHub issue {issue} answered with no comments connection"),
14903 })?;
14904 let items = optional_nodes(Some(connection), "issue comments")?
14905 .into_iter()
14906 .flatten()
14907 .map(comment_from)
14908 .collect::<Result<Vec<_>, _>>()?;
14909 let next = next_cursor(connection)?;
14910 if let Some(next) = &next {
14911 validate_cursor_progress(after, &next.0)?;
14912 }
14913 Ok(Page { items, next })
14914}
14915
14916/// The far ends of an issue's whole `blockedBy` connection, when the read carried it to its
14917/// end — `None` when it carried none, or a page with more past it.
14918fn carried_blocked_by(content: &Value) -> Result<Option<Vec<Value>>, SourceError> {
14919 let Some(connection) = content.get("blockedBy").filter(|value| !value.is_null()) else {
14920 return Ok(None);
14921 };
14922 if next_cursor(connection)?.is_some() {
14923 return Ok(None);
14924 }
14925 Ok(Some(
14926 optional_nodes(Some(connection), "blocked-by issues")?
14927 .into_iter()
14928 .flatten()
14929 .cloned()
14930 .collect(),
14931 ))
14932}
14933
14934/// Where the recorded tail of a dependency walk resumes; see
14935/// [`GitHubProjectsSource::recorded_edges`].
14936const RECORDED_CURSOR: &str = "onetaskgraph.depends_on:";
14937
14938/// The board text field this source keeps a copy's origin in.
14939///
14940/// Named after the key it holds, and held to that name by the guard below rather than by
14941/// a reader noticing.
14942const ORIGIN_FIELD: &str = "onetaskgraph.origin";
14943
14944/// The metadata key that field holds.
14945///
14946/// The engine owns this key and spells it once as `GlobalId::ORIGIN_KEY`; a plugin never
14947/// constructs or interprets the qualified id it carries. This source names it only to
14948/// route it — a short, typed value belongs in a typed field rather than in the body slot
14949/// a caller's own prose shares.
14950///
14951/// Restated rather than imported, because no plugin crate may depend on the engine. What
14952/// keeps the two spellings one contract is `scripts/check-origin-key-spelling.sh`, a
14953/// target in `check`: it reads the engine's own literal and fails naming the file and the
14954/// line when a plugin's parts from it either way. Drift here has one symptom — a copy
14955/// that creates a second item every run instead of finding the one it wrote — and that is
14956/// too late to learn it.
14957const ORIGIN_KEY: &str = "onetaskgraph.origin";
14958
14959/// Where a recorded tail resumes, refusing a cursor no walk in `direction` reported.
14960///
14961/// The reserved key holds forward edges and nothing else — the reverse of a recorded edge
14962/// is derived from the far end, never written down on the near item — so only a forward
14963/// walk ever reports one of these cursors. A reverse read carrying one is resuming a walk
14964/// it did not come from, and it is told so rather than answered with an empty page that
14965/// reads as a walk which ended.
14966fn recorded_offset(
14967 cursor: Option<&str>,
14968 direction: Direction,
14969) -> Result<Option<usize>, SourceError> {
14970 cursor
14971 .and_then(|cursor| cursor.strip_prefix(RECORDED_CURSOR))
14972 .map(|offset| {
14973 if direction != Direction::DependsOn {
14974 return Err(SourceError::Config {
14975 message: format!(
14976 "{RECORDED_CURSOR}{offset} resumes recorded forward edges, which a \
14977 reverse dependency read never issues; resume it in the direction \
14978 that reported it"
14979 ),
14980 });
14981 }
14982 offset.parse().map_err(|_| SourceError::Config {
14983 message: format!("{RECORDED_CURSOR}{offset} is not a recorded-edge cursor"),
14984 })
14985 })
14986 .transpose()
14987}
14988
14989/// What a dependency read's own answer says of a board draft: `None` when the item is not a
14990/// draft, or the answer did not carry its board item; `Some(None)` for a draft whose one board
14991/// item is not on this board, which this source does not hold; and `Some(Some(slot))`, the
14992/// draft's metadata, for one it does — its board item naming this board by its number and by
14993/// the id the same answer read of the board, as [`GitHubProjectsSource::draft_by_id`] decides.
14994fn draft_slot(
14995 data: &Value,
14996 id: &NativeId,
14997 project_number: u32,
14998) -> Result<Option<Option<BTreeMap<String, Value>>>, SourceError> {
14999 let Some(draft) = data
15000 .get("node")
15001 .filter(|node| node.get("__typename").and_then(Value::as_str) == Some("DraftIssue"))
15002 else {
15003 return Ok(None);
15004 };
15005 let Some(items) = data.pointer("/draft/projectV2Items") else {
15006 return Ok(None);
15007 };
15008 if required_str(draft, "id")? != id.0 {
15009 return Err(SourceError::Malformed {
15010 message: format!("GitHub answered a different draft for {}", id.0),
15011 });
15012 }
15013 let nodes = items
15014 .get("nodes")
15015 .and_then(Value::as_array)
15016 .ok_or_else(|| SourceError::Malformed {
15017 message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
15018 })?;
15019 let info = items
15020 .get("pageInfo")
15021 .ok_or_else(|| SourceError::Malformed {
15022 message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
15023 })?;
15024 if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
15025 return Err(SourceError::Malformed {
15026 message: format!(
15027 "GitHub draft {} reports more board items than the one GitHub links a draft to",
15028 id.0
15029 ),
15030 });
15031 }
15032 let board = data
15033 .pointer("/board/projectV2/id")
15034 .and_then(Value::as_str)
15035 .ok_or_else(|| SourceError::Malformed {
15036 message: format!(
15037 "GitHub answered draft {} without this board's own id beside it",
15038 id.0
15039 ),
15040 })?;
15041 let mut held = false;
15042 for node in nodes {
15043 let project = node.get("project").ok_or_else(|| SourceError::Malformed {
15044 message: format!("GitHub draft {} board item has no project", id.0),
15045 })?;
15046 let number = project
15047 .get("number")
15048 .and_then(Value::as_u64)
15049 .ok_or_else(|| SourceError::Malformed {
15050 message: format!(
15051 "GitHub draft {} board item has no numeric project number",
15052 id.0
15053 ),
15054 })?;
15055 let project_id = required_str(project, "id")?;
15056 held |= number == u64::from(project_number) && project_id == board;
15057 }
15058 if !held {
15059 return Ok(Some(None));
15060 }
15061 Ok(Some(Some(
15062 metadata_body(optional_str(draft, "body")?.map(str::to_owned))?.1,
15063 )))
15064}
15065
15066fn recorded_page(edges: Vec<DependencyEdge>, offset: usize, limit: usize) -> Page<DependencyEdge> {
15067 let mut page = offset_page(edges, offset, limit.max(1));
15068 page.next = page
15069 .next
15070 .map(|cursor| Cursor(format!("{RECORDED_CURSOR}{}", cursor.0)));
15071 page
15072}
15073
15074/// The kind of one issue reached through a dependency connection.
15075///
15076/// The same questions the board scan asks, over the fields the dependency document
15077/// selects, and in the same order: the design prefix first, then a sub-issue is a task,
15078/// then anything with sub-issues or the marker is a project.
15079///
15080/// # Errors
15081///
15082/// A far end this board holds as a document is refused rather than reported. The two
15083/// answers that are not refusals would both be wrong: reporting it as a task names an id
15084/// no task read of this source can find, and reporting it as a project names one no
15085/// project read can. There is no third value to return — `ItemKind` has no document
15086/// variant, because nothing may point at a document — so the relationship itself is what
15087/// the person is told about.
15088fn related_kind(value: &Value) -> Result<ItemKind, SourceError> {
15089 let id = required_str(value, "id")?;
15090 if required_str(value, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
15091 return Err(SourceError::Refused {
15092 message: format!(
15093 "GitHub issue {id} is a document of this board — its title begins \
15094 {DESIGN_TITLE_PREFIX:?} — and nothing may depend on a document or be depended \
15095 on by one; next: remove that issue's blocking relationship on this board"
15096 ),
15097 });
15098 }
15099 let parent = optional_str(value.get("parent").unwrap_or(&Value::Null), "id")?;
15100 if parent.is_some() {
15101 return Ok(ItemKind::Task);
15102 }
15103 let (_, slot) = metadata_body(optional_str(value, "body")?.map(str::to_owned))?;
15104 let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
15105 message: format!("GitHub issue {id}: {message}"),
15106 })?;
15107 let sub_issues = sub_issue_total(value)?;
15108 Ok(if sub_issues > 0 || marked == Some(ItemKind::Project) {
15109 ItemKind::Project
15110 } else {
15111 ItemKind::Task
15112 })
15113}
15114
15115/// The `stateInput` that closes an issue for `reason`.
15116fn closed_input(reason: ClosedState) -> Value {
15117 json!({"value":"CLOSED","stateReason":reason.reason()})
15118}
15119
15120/// The `IssueStateUpdateInput` one status target asks for.
15121///
15122/// `stateInput` and `state` are mutually exclusive on `UpdateIssueInput`, and only this
15123/// one is ever sent. A non-terminal status always asks for `OPEN`, which is what reopens
15124/// a currently-closed issue: without that the item would read back `Unknown` and a copy
15125/// would report a change forever. A document has no status at all, and asks for neither.
15126fn state_input(target: Option<&StatusTarget>) -> Value {
15127 match target {
15128 Some(StatusTarget::Terminal(_, reason)) => closed_input(*reason),
15129 Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => json!({"value":"OPEN"}),
15130 // A document has no status, so a write of one says nothing about the issue's open
15131 // or closed state rather than forcing it open: `stateInput` is what carries that
15132 // instruction, and an explicit null asks for no change to it.
15133 None => Value::Null,
15134 }
15135}
15136
15137/// The metadata one write stores in the item's body slot.
15138///
15139/// The typed fields travel as themselves, so the three reserved keys are rebuilt here
15140/// rather than carried: the kind marker so an empty project stays readable, the
15141/// repository list only when it is not exactly the issue's own repository, and the far
15142/// ends no relationship here can name.
15143///
15144/// The copy origin is the one typed field that is also mirrored here, and only as a
15145/// mirror: it lands in the board's origin field as well, which stays the one every reader
15146/// takes it from, and it is here so that GitHub's issue search — which indexes this comment
15147/// and catches up with a write in seconds rather than minutes — can find the item by it.
15148/// A reader of the release before this one drops the slot's copy and reads the field, so an
15149/// item written here still reads with exactly one origin there.
15150fn slot_metadata(
15151 incoming: &Incoming<'_>,
15152 own_repository: Option<&Repository>,
15153 fallback: &[DependencyEdge],
15154) -> BTreeMap<String, Value> {
15155 let mut metadata = incoming.metadata.clone();
15156 match metadata.remove(ORIGIN_KEY) {
15157 Some(Value::String(origin)) if !origin.is_empty() => {
15158 metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin));
15159 }
15160 _ => {}
15161 }
15162 match incoming.written.kind() {
15163 BoardKind::Work(kind) => metadata.insert(
15164 ItemKind::METADATA_KEY.to_owned(),
15165 Value::String(kind.marker().to_owned()),
15166 ),
15167 // A document is told by its title, so it carries no kind marker: that key names
15168 // what a dependency endpoint points at, and nothing may point at a document.
15169 BoardKind::Document => metadata.remove(ItemKind::METADATA_KEY),
15170 };
15171 incoming.classification.record(&mut metadata);
15172 let derivable = own_repository
15173 .map(|own| incoming.repositories == [own.clone()])
15174 .unwrap_or(incoming.repositories.is_empty());
15175 if derivable {
15176 metadata.remove(Repository::METADATA_KEY);
15177 } else {
15178 metadata.insert(
15179 Repository::METADATA_KEY.to_owned(),
15180 Value::Array(
15181 incoming
15182 .repositories
15183 .iter()
15184 .map(|repository| Value::String(repository.as_str().to_owned()))
15185 .collect(),
15186 ),
15187 );
15188 }
15189 // The typed lists are what land, whatever the caller's own metadata held under their
15190 // keys: a key of either name travelling beside the field would otherwise be a second
15191 // answer to the same question, and the field is the one the contract names.
15192 for (key, entries) in [
15193 (TaskRef::DELIVERS_KEY, incoming.delivers),
15194 (TaskRef::DELIVERED_BY_KEY, incoming.delivered_by),
15195 ] {
15196 set_task_list(&mut metadata, key, entries);
15197 }
15198 record_edges(&mut metadata, fallback);
15199 metadata
15200}
15201
15202/// Hold the far ends no relationship here can name under [`DependencyEdge::RECORDED_KEY`] in
15203/// one slot's metadata, or no such key when there are none.
15204fn record_edges(metadata: &mut BTreeMap<String, Value>, fallback: &[DependencyEdge]) {
15205 if fallback.is_empty() {
15206 metadata.remove(DependencyEdge::RECORDED_KEY);
15207 } else {
15208 metadata.insert(
15209 DependencyEdge::RECORDED_KEY.to_owned(),
15210 Value::Array(
15211 fallback
15212 .iter()
15213 .map(|edge| {
15214 serde_json::to_value(&edge.to).expect("an endpoint renders as JSON")
15215 })
15216 .collect(),
15217 ),
15218 );
15219 }
15220}
15221
15222/// Every label one item carries, from its content's own connection and nowhere else.
15223///
15224/// There is no second place to read one from: no document this source sends selects the
15225/// board's built-in `Labels` field, because GitHub derives it from the content and a draft
15226/// cannot carry one at all. The module documentation records the three schema facts that
15227/// settle it.
15228fn labels(content: &Value) -> Result<Vec<Label>, SourceError> {
15229 optional_nodes(content.get("labels"), "content labels")?
15230 .into_iter()
15231 .flatten()
15232 .map(|v| {
15233 Ok(Label {
15234 id: NativeId(required_str(v, "id")?.to_owned()),
15235 name: required_str(v, "name")?.to_owned(),
15236 color: optional_str(v, "color")?.map(str::to_owned),
15237 })
15238 })
15239 .collect()
15240}
15241
15242/// Each board field value one item holds, as the change token's facets name it: the field's
15243/// name, with the single-select option's name or the text. A value of a type no read here
15244/// selects, and one naming no field, says nothing a person can rename and is left out.
15245fn facet_fields(field_values: &[Value]) -> Vec<(String, String)> {
15246 field_values
15247 .iter()
15248 .filter_map(|value| {
15249 let field = value.pointer("/field/name")?.as_str()?;
15250 let held = value.get("name").or_else(|| value.get("text"))?.as_str()?;
15251 Some((field.to_owned(), held.to_owned()))
15252 })
15253 .collect()
15254}
15255
15256/// The digest of the facets of the issue `content`, whose board item's values are
15257/// `field_values` — or `None` when the response did not carry, whole, every member they are read
15258/// from: the title, the parent, the sub-issue summary and the body its kind is read from, every
15259/// label, and every field value.
15260///
15261/// What a write's own response spells its token from, by exactly the rules a read takes them by.
15262fn facets_of(content: &Value, field_values: &Value) -> Option<change_token::FacetsDigest> {
15263 let ended = |connection: &Value| {
15264 connection.pointer("/pageInfo/hasNextPage") == Some(&Value::Bool(false))
15265 };
15266 let labels = content.get("labels").filter(|labels| ended(labels))?;
15267 if !ended(field_values) {
15268 return None;
15269 }
15270 let parent = content.get("parent")?;
15271 let (_, slot) = metadata_body(content.get("body")?.as_str().map(str::to_owned)).ok()?;
15272 let kind = BoardKind::of(
15273 content.get("title")?.as_str()?,
15274 !parent.is_null(),
15275 sub_issue_total(content).ok()?,
15276 ItemKind::from_metadata(&slot).ok()?,
15277 );
15278 let labels = self::labels(&json!({"labels": labels})).ok()?;
15279 let nodes = field_values.get("nodes")?.as_array()?;
15280 Some(change_token::Facets::new(kind.facet(), facet_fields(nodes), &labels).digest())
15281}
15282
15283/// The definition of each board field one item's values are values of, in the shape a read
15284/// of the board's own `fields` gives one.
15285///
15286/// A value names its field through a fragment on that field's own type, so the type is
15287/// known from which kind of value it is: a single-select value's field is a
15288/// `ProjectV2SingleSelectField`, options and all, and a text value's is a `ProjectV2Field`.
15289/// A value whose field carried no id, or an empty one, says nothing usable and is left out.
15290fn field_definitions(field_values: &[Value]) -> Vec<Value> {
15291 field_values
15292 .iter()
15293 .filter_map(|value| {
15294 let field = value.get("field")?.as_object()?;
15295 field.get("id")?.as_str().filter(|id| !id.is_empty())?;
15296 let typename = if value.get("text").is_some() {
15297 "ProjectV2Field"
15298 } else if value.get("name").is_some() {
15299 "ProjectV2SingleSelectField"
15300 } else {
15301 return None;
15302 };
15303 let mut defined = field.clone();
15304 defined.insert("__typename".to_owned(), json!(typename));
15305 // Only a text field holds a text value, so the value says the field's type too.
15306 if typename == "ProjectV2Field" {
15307 defined.insert("dataType".to_owned(), json!("TEXT"));
15308 }
15309 Some(Value::Object(defined))
15310 })
15311 .collect()
15312}
15313
15314fn text_field(field_values: &[Value], name: &str) -> Result<Option<String>, SourceError> {
15315 let Some(node) = field_values
15316 .iter()
15317 .find(|node| node.pointer("/field/name").and_then(Value::as_str) == Some(name))
15318 else {
15319 return Ok(None);
15320 };
15321 Ok(optional_str(node, "text")?.map(str::to_owned))
15322}
15323
15324fn valid_github_owner(owner: &str) -> bool {
15325 !owner.is_empty()
15326 && owner.len() <= 39
15327 && !owner.starts_with('-')
15328 && !owner.ends_with('-')
15329 && !owner.contains("--")
15330 && owner
15331 .bytes()
15332 .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
15333}
15334
15335/// GitHub's repository-name grammar: 1-100 ASCII letters, digits, `-`, `_` or `.`, and
15336/// neither of the two names a path segment already means.
15337fn valid_github_repository_name(name: &str) -> bool {
15338 !name.is_empty()
15339 && name.len() <= 100
15340 && name != "."
15341 && name != ".."
15342 && name
15343 .bytes()
15344 .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
15345}
15346
15347fn valid_environment_name(name: &str) -> bool {
15348 let mut bytes = name.bytes();
15349 bytes
15350 .next()
15351 .is_some_and(|byte| byte.is_ascii_alphabetic() || byte == b'_')
15352 && bytes.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
15353}
15354
15355/// How many sub-issues one issue has.
15356///
15357/// `Issue.subIssuesSummary` is `SubIssuesSummary!` and its `total` is `Int!`, so an
15358/// absent or non-integer one is a response this source cannot read — and reading it as
15359/// zero would classify a project as a task, which is exactly the mistake the marker
15360/// exists to keep from happening quietly.
15361fn sub_issue_total(issue: &Value) -> Result<u64, SourceError> {
15362 let summary = issue
15363 .get("subIssuesSummary")
15364 .ok_or_else(|| SourceError::Malformed {
15365 message: "GitHub issue is missing subIssuesSummary".into(),
15366 })?;
15367 summary
15368 .get("total")
15369 .and_then(Value::as_u64)
15370 .ok_or_else(|| SourceError::Malformed {
15371 message: "GitHub issue subIssuesSummary.total is not an unsigned integer".into(),
15372 })
15373}
15374
15375/// One issue's own `number`.
15376///
15377/// An issue always has one: GitHub declares `Issue.number` as `Int!` and every selection of
15378/// an issue in this module asks for it. So a read of one that comes back without it, or
15379/// with something that is not an unsigned integer, is a response this source cannot read —
15380/// absence here is **not** "this issue has no number". A draft is the content that has
15381/// none, and a draft never reaches this: the caller decides on `__typename` first, the way
15382/// it does for `subIssuesSummary`, which `DraftIssue` equally declares nothing for.
15383fn issue_number(issue: &Value) -> Result<u64, SourceError> {
15384 issue
15385 .get("number")
15386 .and_then(Value::as_u64)
15387 .ok_or_else(|| SourceError::Malformed {
15388 message: "GitHub issue number is missing or is not an unsigned integer".into(),
15389 })
15390}
15391
15392/// The `number` a creating mutation answered with, and `None` when it answered without one;
15393/// why a missing one is tolerated is at the call in `create_and_file_issue`.
15394fn created_issue_number(created: &Value) -> Result<Option<u64>, SourceError> {
15395 match created.get("number") {
15396 None | Some(Value::Null) => Ok(None),
15397 Some(value) => value
15398 .as_u64()
15399 .map(Some)
15400 .ok_or_else(|| SourceError::Malformed {
15401 message: "GitHub created issue number is not an unsigned integer".into(),
15402 }),
15403 }
15404}
15405
15406fn required_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
15407 value
15408 .get(field)
15409 .and_then(Value::as_str)
15410 .ok_or_else(|| SourceError::Malformed {
15411 message: format!("GitHub response is missing string field {field}"),
15412 })
15413}
15414
15415fn required_nonblank_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
15416 let found = required_str(value, field)?;
15417 if found.trim().is_empty() {
15418 return Err(SourceError::Malformed {
15419 message: format!("GitHub response has blank string field {field}"),
15420 });
15421 }
15422 Ok(found)
15423}
15424
15425/// The slot's delimiters, which `docs/metadata.md` settles once for every source that
15426/// needs one — Linear spells them too, in its own description field.
15427///
15428/// Restated rather than shared, because a plugin crate depends on the contract crate and
15429/// nothing else of this workspace. `scripts/check-metadata-slot-encoding.sh`, a target in
15430/// `check`, is what keeps the two one encoding: drift is otherwise quiet, since each
15431/// source round-trips its own writes perfectly well under its own spelling.
15432const METADATA_OPEN: &str = "<!-- onetaskgraph.metadata\n";
15433const METADATA_CLOSE: &str = "\n-->";
15434
15435/// What the composer puts between a non-empty visible body and the slot, and the one thing
15436/// the parser takes off the visible body when it takes the slot off — exactly once, so every
15437/// other trailing byte of the body comes back as it was written.
15438// 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.
15439const METADATA_SEPARATOR: &str = "\n\n";
15440
15441/// The visible body and the metadata slot at the end of it.
15442///
15443/// The encoding is the one `docs/metadata.md` settles for Linear, which is where its
15444/// reasons are. Only a comment at the very end is a slot; one in the middle is a person's
15445/// own content and is left alone. The visible body is everything before the slot less the
15446/// one [`METADATA_SEPARATOR`] the composer put there, byte for byte.
15447fn metadata_body(
15448 body: Option<String>,
15449) -> Result<(Option<String>, BTreeMap<String, Value>), SourceError> {
15450 let Some(body) = body else {
15451 return Ok((None, BTreeMap::new()));
15452 };
15453 let Some(slot) = slot_span(&body)? else {
15454 return Ok((Some(body), BTreeMap::new()));
15455 };
15456 let metadata =
15457 serde_json::from_str(&body[slot.encoded_start..slot.encoded_end]).map_err(|error| {
15458 SourceError::Malformed {
15459 message: format!(
15460 "invalid canonical JSON in GitHub issue onetaskgraph metadata slot: {error}"
15461 ),
15462 }
15463 })?;
15464 let before = &body[..slot.start];
15465 let visible = before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before);
15466 Ok(((!visible.is_empty()).then(|| visible.to_owned()), metadata))
15467}
15468
15469/// Where the metadata slot sits in one body, as byte offsets into it.
15470struct SlotSpan {
15471 /// Where [`METADATA_OPEN`] begins.
15472 start: usize,
15473 /// Where the encoded JSON begins, just past [`METADATA_OPEN`].
15474 encoded_start: usize,
15475 /// Where the encoded JSON ends, at the start of [`METADATA_CLOSE`].
15476 encoded_end: usize,
15477 /// Just past [`METADATA_CLOSE`].
15478 end: usize,
15479}
15480
15481/// The slot at the very end of `body`, or `None` when it has none.
15482///
15483/// The one reading of *where the slot is*, shared by [`metadata_body`], which reads it, and
15484/// [`with_slot`], which rewrites it — so the two cannot disagree about which comment is the
15485/// slot.
15486fn slot_span(body: &str) -> Result<Option<SlotSpan>, SourceError> {
15487 let Some(start) = body.rfind(METADATA_OPEN) else {
15488 return Ok(None);
15489 };
15490 let encoded_start = start + METADATA_OPEN.len();
15491 let Some(relative_end) = body[encoded_start..].find(METADATA_CLOSE) else {
15492 return Err(SourceError::Malformed {
15493 message: "unterminated onetaskgraph metadata slot in GitHub issue body".into(),
15494 });
15495 };
15496 let encoded_end = encoded_start + relative_end;
15497 let end = encoded_end + METADATA_CLOSE.len();
15498 if !body[end..].trim().is_empty() {
15499 return Ok(None);
15500 }
15501 Ok(Some(SlotSpan {
15502 start,
15503 encoded_start,
15504 encoded_end,
15505 end,
15506 }))
15507}
15508
15509/// `body` with its metadata slot holding exactly `metadata`, and every byte outside the
15510/// slot as it was.
15511///
15512/// A slot that is there has its JSON replaced in place; one that becomes empty is removed
15513/// together with the one [`METADATA_SEPARATOR`] separating it from the prose before it. A
15514/// body with no slot gains one the way [`compose_body`] writes it — after that separator,
15515/// or alone in an empty body — and a body with no slot that is given no metadata is
15516/// returned as it is.
15517fn with_slot(body: &str, metadata: &BTreeMap<String, Value>) -> Result<String, SourceError> {
15518 let encoded = if metadata.is_empty() {
15519 None
15520 } else {
15521 Some(
15522 serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
15523 message: error.to_string(),
15524 })?,
15525 )
15526 };
15527 Ok(match (slot_span(body)?, encoded) {
15528 (Some(slot), Some(encoded)) => format!(
15529 "{}{encoded}{}",
15530 &body[..slot.encoded_start],
15531 &body[slot.encoded_end..]
15532 ),
15533 (Some(slot), None) => {
15534 let before = &body[..slot.start];
15535 format!(
15536 "{}{}",
15537 before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before),
15538 &body[slot.end..]
15539 )
15540 }
15541 (None, None) => body.to_owned(),
15542 (None, Some(encoded)) if body.is_empty() => {
15543 format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15544 }
15545 (None, Some(encoded)) => {
15546 format!("{body}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15547 }
15548 })
15549}
15550
15551/// `body` with everything before its metadata slot replaced by `content`, and the slot
15552/// itself kept byte for byte.
15553///
15554/// The inverse of how [`metadata_body`] splits a body: the slot, when there is one, follows
15555/// `content` after the one [`METADATA_SEPARATOR`] the composer puts there — or alone, when
15556/// `content` is empty — so a read of the result reports `content` as the visible body and
15557/// the slot's metadata exactly as it was.
15558fn with_content(body: &str, content: &str) -> Result<String, SourceError> {
15559 let Some(slot) = slot_span(body)? else {
15560 return Ok(content.to_owned());
15561 };
15562 let kept = &body[slot.start..];
15563 Ok(if content.is_empty() {
15564 kept.to_owned()
15565 } else {
15566 format!("{content}{METADATA_SEPARATOR}{kept}")
15567 })
15568}
15569
15570/// Hold `entries` under `key` in one slot's metadata, or no such key when there are none.
15571fn set_task_list(metadata: &mut BTreeMap<String, Value>, key: &str, entries: &[TaskRef]) {
15572 if entries.is_empty() {
15573 metadata.remove(key);
15574 } else {
15575 metadata.insert(
15576 key.to_owned(),
15577 Value::Array(
15578 entries
15579 .iter()
15580 .map(|entry| Value::String(entry.as_str().to_owned()))
15581 .collect(),
15582 ),
15583 );
15584 }
15585}
15586
15587fn compose_body(
15588 content: Option<&str>,
15589 metadata: &BTreeMap<String, Value>,
15590) -> Result<Option<String>, SourceError> {
15591 let visible = content.unwrap_or_default();
15592 if metadata.is_empty() {
15593 return Ok((!visible.is_empty()).then(|| visible.to_owned()));
15594 }
15595 let encoded = serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
15596 message: error.to_string(),
15597 })?;
15598 Ok(Some(if visible.is_empty() {
15599 format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15600 } else {
15601 format!("{visible}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15602 }))
15603}
15604
15605fn required_bool(value: &Value, field: &str) -> Result<bool, SourceError> {
15606 value
15607 .get(field)
15608 .and_then(Value::as_bool)
15609 .ok_or_else(|| SourceError::Malformed {
15610 message: format!("GitHub response is missing boolean field {field}"),
15611 })
15612}
15613fn optional_str<'a>(value: &'a Value, field: &str) -> Result<Option<&'a str>, SourceError> {
15614 match value.get(field) {
15615 None | Some(Value::Null) => Ok(None),
15616 Some(value) => value
15617 .as_str()
15618 .map(Some)
15619 .ok_or_else(|| SourceError::Malformed {
15620 message: format!("GitHub response field {field} is not a string or null"),
15621 }),
15622 }
15623}
15624fn optional_nodes<'a>(
15625 connection: Option<&'a Value>,
15626 name: &str,
15627) -> Result<Option<&'a Vec<Value>>, SourceError> {
15628 match connection {
15629 None | Some(Value::Null) => Ok(None),
15630 Some(value) => value
15631 .get("nodes")
15632 .and_then(Value::as_array)
15633 .map(Some)
15634 .ok_or_else(|| SourceError::Malformed {
15635 message: format!("GitHub {name}.nodes is not an array"),
15636 }),
15637 }
15638}
15639fn complete_connection(connection: &Value, name: &str, size: u32) -> Result<(), SourceError> {
15640 let page_info = connection
15641 .get("pageInfo")
15642 .ok_or_else(|| SourceError::Malformed {
15643 message: format!("GitHub {name} has no pageInfo"),
15644 })?;
15645 if required_bool(page_info, "hasNextPage")? {
15646 return Err(SourceError::Malformed {
15647 message: format!(
15648 "GitHub {name} exceeds the supported nested connection size of {size}"
15649 ),
15650 });
15651 }
15652 Ok(())
15653}
15654fn optional_time(value: &Value, field: &str) -> Result<Option<DateTime<Utc>>, SourceError> {
15655 optional_str(value, field)?
15656 .map(|timestamp| {
15657 timestamp.parse().map_err(|error| SourceError::Malformed {
15658 message: format!("GitHub response field {field} is not a timestamp: {error}"),
15659 })
15660 })
15661 .transpose()
15662}
15663fn validate_page(page: &PageRequest) -> Result<(), SourceError> {
15664 if page.limit == 0 {
15665 Err(SourceError::Config {
15666 message: "page limit must be at least 1".into(),
15667 })
15668 } else {
15669 Ok(())
15670 }
15671}
15672fn next_cursor(connection: &Value) -> Result<Option<Cursor>, SourceError> {
15673 let page = connection
15674 .get("pageInfo")
15675 .filter(|value| value.is_object())
15676 .ok_or_else(|| SourceError::Malformed {
15677 message: "GitHub connection is missing pageInfo".into(),
15678 })?;
15679 if required_bool(page, "hasNextPage")? {
15680 let cursor = required_str(page, "endCursor")?;
15681 validate_cursor_progress(None, cursor)?;
15682 Ok(Some(Cursor(cursor.into())))
15683 } else {
15684 Ok(None)
15685 }
15686}
15687fn validate_cursor_progress(previous: Option<&str>, next: &str) -> Result<(), SourceError> {
15688 if next.is_empty() || previous == Some(next) {
15689 Err(SourceError::Malformed {
15690 message: "GitHub pagination cursor is empty or did not advance".into(),
15691 })
15692 } else {
15693 Ok(())
15694 }
15695}
15696/// The version of this plugin's opaque narrowing-search cursor.
15697pub const SEARCH_CURSOR_VERSION: u32 = 4;
15698
15699#[derive(Serialize, Deserialize)]
15700#[serde(tag = "state", rename_all = "snake_case", deny_unknown_fields)]
15701enum SearchConnection {
15702 Initial {},
15703 Continuing { after: Cursor },
15704 Exhausted {},
15705}
15706impl SearchConnection {
15707 fn after(&self) -> Option<&str> {
15708 match self {
15709 Self::Continuing { after } => Some(&after.0),
15710 _ => None,
15711 }
15712 }
15713 fn exhausted(&self) -> bool {
15714 matches!(self, Self::Exhausted { .. })
15715 }
15716 /// Whether a cursor naming this position, `offset` rows into its page, is one this
15717 /// plugin could have handed out: a page is resumed only part of the way through it — an
15718 /// offset of a whole page or more would skip rows nobody was given — an initial page
15719 /// only once some of it was handed out, and an exhausted connection has no page to be
15720 /// part of the way through.
15721 fn valid_resume(&self, offset: usize) -> bool {
15722 let within = offset < SEARCH_PAGE_SIZE as usize;
15723 match self {
15724 Self::Initial { .. } => offset > 0 && within,
15725 Self::Continuing { after } => !after.0.is_empty() && within,
15726 Self::Exhausted { .. } => offset == 0,
15727 }
15728 }
15729}
15730
15731/// Versioned source cursor. A zero offset and empty own-write ids are omitted.
15732#[derive(Serialize, Deserialize)]
15733#[serde(deny_unknown_fields)]
15734struct SearchPosition {
15735 version: u32,
15736 connection: SearchConnection,
15737 /// How many rows of the page `connection` starts were already handed out.
15738 #[serde(default, skip_serializing_if = "is_zero")]
15739 offset: usize,
15740 #[serde(default, skip_serializing_if = "Vec::is_empty")]
15741 seen: Vec<NativeId>,
15742 #[serde(default, skip_serializing_if = "Vec::is_empty")]
15743 own: Vec<NativeId>,
15744}
15745impl Default for SearchPosition {
15746 fn default() -> Self {
15747 Self {
15748 version: SEARCH_CURSOR_VERSION,
15749 connection: SearchConnection::Initial {},
15750 offset: 0,
15751 seen: Vec::new(),
15752 own: Vec::new(),
15753 }
15754 }
15755}
15756
15757fn is_zero(offset: &usize) -> bool {
15758 *offset == 0
15759}
15760
15761fn numeric_cursor(cursor: Option<&Cursor>) -> Result<usize, SourceError> {
15762 cursor.map_or(Ok(0), |c| {
15763 c.0.parse().map_err(|_| SourceError::Config {
15764 message: "page cursor is invalid".into(),
15765 })
15766 })
15767}
15768fn offset_page<T>(mut items: Vec<T>, offset: usize, limit: usize) -> Page<T> {
15769 if offset > items.len() {
15770 return Page::last(vec![]);
15771 }
15772 let tail = items.split_off(offset);
15773 let mut selected = tail;
15774 let next = (selected.len() > limit).then(|| Cursor((offset + limit).to_string()));
15775 selected.truncate(limit);
15776 Page {
15777 items: selected,
15778 next,
15779 }
15780}
15781
15782impl GitHubProjectsSource {
15783 async fn write_task_assets(
15784 &self,
15785 write: &ItemWrite<Task>,
15786 assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
15787 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
15788 self.write_item(
15789 &self.task_incoming(write, assets)?,
15790 write.target.as_ref(),
15791 &write.depends_on,
15792 )
15793 .await
15794 }
15795
15796 /// One task write as [`Self::write_item`] takes it, once what it delivers and its priority
15797 /// are known to be ones this source can write.
15798 fn task_incoming<'a>(
15799 &self,
15800 write: &'a ItemWrite<Task>,
15801 assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
15802 ) -> Result<Incoming<'a>, SourceError> {
15803 let near = write.target.as_ref().unwrap_or(&write.item.id);
15804 for (key, entries) in [
15805 (TaskRef::DELIVERS_KEY, &write.item.delivers),
15806 (TaskRef::DELIVERED_BY_KEY, &write.item.delivered_by),
15807 ] {
15808 TaskRef::listed(key, near, Some(&self.name), entries.clone())
15809 .map_err(|message| SourceError::Refused { message })?;
15810 }
15811 if self.priorities.is_none() && write.item.priority != Priority::None {
15812 return Err(self.holds_no_priority());
15813 }
15814 Ok(Incoming {
15815 written: Written::Work(ItemKind::Task, &write.item.status),
15816 title: &write.item.title,
15817 content: write.item.content.as_deref(),
15818 assets,
15819 labels: &write.item.labels,
15820 metadata: &write.item.metadata,
15821 repositories: &write.item.repositories,
15822 classification: write.item.classification,
15823 parent: write.item.project.as_ref(),
15824 delivers: &write.item.delivers,
15825 delivered_by: &write.item.delivered_by,
15826 priority: self.priorities.as_ref().map(|_| write.item.priority),
15827 })
15828 }
15829 async fn write_document_assets(
15830 &self,
15831 write: &ItemWrite<Document>,
15832 assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
15833 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
15834 // A document takes part in no dependency graph, so there is no far end to write
15835 // natively and none to record: a caller naming one is told so rather than having it
15836 // stored under the reserved key, where a later read would report an edge the
15837 // contract says cannot exist.
15838 if !write.depends_on.is_empty() {
15839 return Err(SourceError::Refused {
15840 message: format!(
15841 "this write names {} dependencies for a document, and a document takes \
15842 part in no dependency graph; next: put the dependency on the task or \
15843 project the document is about",
15844 write.depends_on.len()
15845 ),
15846 });
15847 }
15848 self.write_item(
15849 &Incoming {
15850 written: Written::Document,
15851 title: &write.item.title,
15852 content: write.item.content.as_deref(),
15853 assets,
15854 labels: &write.item.labels,
15855 metadata: &write.item.metadata,
15856 repositories: &write.item.repositories,
15857 classification: write.item.classification,
15858 parent: write.item.project.as_ref(),
15859 delivers: &[],
15860 delivered_by: &[],
15861 priority: None,
15862 },
15863 write.target.as_ref(),
15864 &[],
15865 )
15866 .await
15867 }
15868}
15869
15870/// What [`TaskSource::end_command`] leaves of this source's held state, asserted on the state
15871/// itself, for the two things no journey can observe.
15872///
15873/// The journeys in `crates/onetaskgraph-e2e/tests/e2e/end_command.rs` prove through the engine,
15874/// with and without the call, that a settlement, a board listing and a metadata search each
15875/// read afresh after it — the resolved records, the written-item overlay, the board and its
15876/// search, and the narrowed searches. What they cannot reach is the held field definitions,
15877/// because a status write naming an option a person deleted is refused the same whether or
15878/// not the list is held, and a poisoned lock, because nothing outside the source can panic
15879/// while one of its locks is held. So these assert those directly, and every other holder
15880/// beside them so a holder added later without a clear in the call fails here.
15881#[cfg(test)]
15882mod end_command_tests {
15883 use super::*;
15884
15885 struct Token;
15886
15887 impl SecretResolver for Token {
15888 fn get(&self, var: &str) -> Option<SecretString> {
15889 (var == "GH_PROJECTS_TOKEN").then(|| "test-token".into())
15890 }
15891 }
15892
15893 fn source() -> GitHubProjectsSource {
15894 let config = serde_json::from_value(json!({
15895 "owner": "octo-org", "project_number": 7, "repository": "acme/work",
15896 // Nothing here is sent: the source is only built and its state inspected.
15897 "endpoint": "http://127.0.0.1:9/graphql",
15898 }))
15899 .expect("a usable configuration");
15900 GitHubProjectsSource::new(&SourceName::new("work").unwrap(), config, &Token)
15901 .expect("the source builds")
15902 }
15903
15904 /// One issue as a board read answers it.
15905 fn resolved(source: &GitHubProjectsSource) -> Resolved {
15906 source
15907 .resolve(&json!({
15908 "id": "ITEM-1",
15909 "content": {"__typename": "Issue", "id": "I_1", "title": "Held",
15910 "body": "what a person may since have edited", "state": "OPEN",
15911 "stateReason": null, "url": null, "number": 1,
15912 "subIssuesSummary": {"total": 0},
15913 "labels": {"nodes": [], "pageInfo": {"hasNextPage": false}}},
15914 "fieldValues": {"nodes": [], "pageInfo": {"hasNextPage": false}},
15915 }))
15916 .expect("the item reads")
15917 .expect("an issue")
15918 }
15919
15920 /// Hold something in every holder the call clears, and the repository id it keeps.
15921 fn fill(source: &GitHubProjectsSource) {
15922 let item = resolved(source);
15923 source.created.lock().unwrap().push(item.clone());
15924 source.updated.lock().unwrap().push(item.clone());
15925 *source.board_cache.lock().unwrap() = Some(Board {
15926 id: "PVT-board".into(),
15927 fields: json!({"nodes": []}),
15928 items: vec![item.clone()],
15929 });
15930 *source.search_cache.lock().unwrap() = Some(vec![item.clone()]);
15931 source
15932 .narrowed_cache
15933 .lock()
15934 .unwrap()
15935 .insert("status:todo".into(), vec![item.clone()]);
15936 source.children_cache.lock().unwrap().insert(
15937 NativeId("P-1".into()),
15938 (NativeId("P-1".into()), vec![item.clone()]),
15939 );
15940 source
15941 .search_next
15942 .lock()
15943 .unwrap()
15944 .insert("status:todo".into(), Some("cursor".into()));
15945 source
15946 .resolved_cache
15947 .lock()
15948 .unwrap()
15949 .insert(item.id.clone(), item);
15950 *source.fields_cache.lock().unwrap() = Some(BoardFields {
15951 id: BoardId::parse("PVT-board").unwrap(),
15952 fields: json!({"nodes": []}),
15953 });
15954 source
15955 .repository_cache
15956 .lock()
15957 .unwrap()
15958 .insert(RepositoryTarget::parse("acme/work").unwrap(), "R_1".into());
15959 }
15960
15961 fn assert_dropped(source: &GitHubProjectsSource) {
15962 assert!(source.created().unwrap().is_empty(), "created");
15963 assert!(source.updated().unwrap().is_empty(), "updated");
15964 assert!(source.board_cache().unwrap().is_none(), "board");
15965 assert!(source.search_cache.lock().unwrap().is_none(), "search");
15966 assert!(source.narrowed_cache.lock().unwrap().is_empty(), "narrowed");
15967 assert!(
15968 source.children_cache.lock().unwrap().is_empty(),
15969 "project children"
15970 );
15971 assert!(
15972 source.search_next.lock().unwrap().is_empty(),
15973 "search paging"
15974 );
15975 assert!(
15976 source.resolved_cache().unwrap().is_empty(),
15977 "resolved records"
15978 );
15979 assert!(source.fields_cache().unwrap().is_none(), "board fields");
15980 assert_eq!(
15981 source.repository_cache().unwrap().len(),
15982 1,
15983 "a repository's node id stays valid and is kept"
15984 );
15985 }
15986
15987 fn end(source: &GitHubProjectsSource) {
15988 tokio::runtime::Builder::new_current_thread()
15989 .build()
15990 .unwrap()
15991 .block_on(source.end_command())
15992 .expect("the command ends");
15993 }
15994
15995 #[test]
15996 fn the_call_drops_every_item_search_and_board_read_and_keeps_repository_ids() {
15997 let source = source();
15998 fill(&source);
15999 end(&source);
16000 assert_dropped(&source);
16001 }
16002
16003 #[test]
16004 fn the_call_clears_a_lock_an_earlier_failure_poisoned() {
16005 fn poison<T: Send>(held: &Mutex<T>) {
16006 std::thread::scope(|scope| {
16007 let _ = scope
16008 .spawn(|| {
16009 let _guard = held.lock().unwrap();
16010 panic!("a failure while the lock is held");
16011 })
16012 .join();
16013 });
16014 assert!(held.is_poisoned());
16015 }
16016 let source = source();
16017 fill(&source);
16018 poison(&source.created);
16019 poison(&source.updated);
16020 poison(&source.board_cache);
16021 poison(&source.search_cache);
16022 poison(&source.narrowed_cache);
16023 poison(&source.search_next);
16024 poison(&source.resolved_cache);
16025 poison(&source.fields_cache);
16026 assert!(
16027 source.resolved_cache().is_err(),
16028 "a poisoned lock is refused before the call"
16029 );
16030 end(&source);
16031 assert_dropped(&source);
16032 }
16033}
16034
16035#[cfg(test)]
16036mod unprojectable_fields_tests {
16037 use super::*;
16038
16039 /// The field names `docs/metadata.md` says a `metadata_fields` entry may not name.
16040 fn documented() -> std::collections::BTreeSet<String> {
16041 let document = std::fs::read_to_string(
16042 std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../docs/metadata.md"),
16043 )
16044 .expect("docs/metadata.md is readable");
16045 let document = document.split_whitespace().collect::<Vec<_>>().join(" ");
16046 let lead = "a `field` equal, ignoring case, to ";
16047 let start = document.find(lead).expect("the refused-field sentence") + lead.len();
16048 let sentence = &document[start..];
16049 let sentence = &sentence[..sentence
16050 .find("; and two entries naming one field")
16051 .expect("the refused-field sentence ends at the duplicate rule")];
16052 let (own, github) = sentence
16053 .split_once(" or a field GitHub owns on every board (")
16054 .expect("the sentence names GitHub's own fields");
16055 own.split('`')
16056 .skip(1)
16057 .step_by(2)
16058 .chain(
16059 github
16060 .strip_suffix(')')
16061 .expect("GitHub's own fields close the sentence")
16062 .split(", "),
16063 )
16064 .map(str::to_owned)
16065 .collect()
16066 }
16067
16068 #[test]
16069 fn the_documented_refused_fields_are_exactly_the_ones_the_configuration_refuses() {
16070 let refused: std::collections::BTreeSet<String> = UNPROJECTABLE_FIELDS
16071 .iter()
16072 .map(|&name| name.to_owned())
16073 .collect();
16074 assert_eq!(documented(), refused, "the fields docs/metadata.md lists");
16075 let instance = SourceName::new("work").unwrap();
16076 for name in UNPROJECTABLE_FIELDS {
16077 for spelled in [name.to_owned(), name.to_uppercase()] {
16078 let entry = MetadataFieldConfig {
16079 field: spelled.clone(),
16080 key: "team.machine".to_owned(),
16081 path: Vec::new(),
16082 };
16083 let refused = MetadataField::resolve(vec![entry], &instance)
16084 .expect_err("an unprojectable field is refused");
16085 assert!(
16086 refused.to_string().contains(&format!("{name:?}")),
16087 "{spelled}: {refused}"
16088 );
16089 }
16090 }
16091 }
16092}