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/// The wordings GitHub answers a write that collided with another change to the same
2356/// ordering with, in the `errors` of an HTTP 200: "Your attempt to move this item created a
2357/// temporary conflict. Please try again." The write did not take effect and GitHub asks for it
2358/// again, so it is retried on the same growing schedule a rate limit without a hint is, within
2359/// the same retry budget — except a `createIssue`, whose answer cannot say the issue was not
2360/// made, and which is therefore reported rather than replayed into a duplicate.
2361///
2362/// Observed rather than documented, so it is pinned exactly as [`SECONDARY_WORDINGS`] is:
2363/// `tests/fixtures/rate-limits.json` records where it was read under `conflict.observed`, and
2364/// the same drift gate reconciles the two both ways. Public for that gate alone.
2365pub const CONFLICT_WORDINGS: [&str; 1] = ["created a temporary conflict"];
2366
2367/// What a response *says about itself*, which is the only place a refusal can be read.
2368///
2369/// Deliberately not the whole response body. A board is a place people write about their
2370/// own work, and a task on it titled "the secondary rate limit" would, matched across the
2371/// raw text, turn a perfectly good answer into a refusal this source then waited out and
2372/// reported. So the item data is never read: what is read is GitHub's own REST-style
2373/// `message` envelope, which is what a forbidden status carries, and the `message` and
2374/// `type` of each GraphQL error, which is where a *successful* response says it.
2375///
2376/// A body that is not JSON at all has nothing structured to read, so only a failing
2377/// response's own text is taken — a successful response that is not JSON is malformed
2378/// rather than refused, and [`GitHubProjectsSource::answer`] says so.
2379fn refusal_wording(status: StatusCode, body: &str) -> String {
2380 let Ok(parsed) = serde_json::from_str::<Value>(body) else {
2381 return if status.is_success() {
2382 String::new()
2383 } else {
2384 body.to_owned()
2385 };
2386 };
2387 let mut said: Vec<&str> = parsed
2388 .get("message")
2389 .and_then(Value::as_str)
2390 .into_iter()
2391 .collect();
2392 if let Some(errors) = parsed.get("errors").and_then(Value::as_array) {
2393 for error in errors {
2394 said.extend(
2395 ["message", "type"]
2396 .into_iter()
2397 .filter_map(|key| error.get(key).and_then(Value::as_str)),
2398 );
2399 }
2400 }
2401 said.join("; ")
2402}
2403
2404impl Limiter {
2405 /// Which limiter refused this response, or `None` when none of them did.
2406 ///
2407 /// The wording is read first and the status only decides what carries none of it,
2408 /// because GitHub answers a secondary limit with a forbidden status far more often
2409 /// than with too-many-requests — while a forbidden status saying nothing about a limit
2410 /// really is a credential this token lacks.
2411 ///
2412 /// A response is a refusal because of its status or its own wording. A spent budget
2413 /// only ever explains one; it never turns an answer into a refusal.
2414 fn classify(status: StatusCode, budget_exhausted: bool, body: &str) -> Option<Self> {
2415 let normalized = refusal_wording(status, body).to_ascii_lowercase();
2416 if SECONDARY_WORDINGS
2417 .iter()
2418 .any(|wording| normalized.contains(wording))
2419 {
2420 return Some(Self::Secondary);
2421 }
2422 if status == StatusCode::TOO_MANY_REQUESTS {
2423 return Some(Self::Primary);
2424 }
2425 // An exhausted budget *explains* a response that failed; it does not make one that
2426 // succeeded into a failure. GitHub sets `x-ratelimit-remaining: 0` on the last
2427 // request the budget allowed as well as on the ones it then refuses, so reading
2428 // the header alone threw away a good answer — and, once refusals were retried,
2429 // replayed a request that had already taken effect.
2430 if !status.is_success() && budget_exhausted {
2431 return Some(Self::Primary);
2432 }
2433 // A successful response saying it: GitHub reports a GraphQL rate limit in the
2434 // `errors` of an HTTP 200, where nothing about the status says so at all.
2435 if status.is_success()
2436 && PRIMARY_WORDINGS
2437 .iter()
2438 .any(|wording| normalized.contains(wording))
2439 {
2440 return Some(Self::Primary);
2441 }
2442 None
2443 }
2444
2445 /// What this limiter is called where an operator can look it up.
2446 const fn name(self) -> &'static str {
2447 match self {
2448 Self::Primary => "GitHub's primary API rate limit",
2449 Self::Secondary => "GitHub's secondary rate limit",
2450 }
2451 }
2452
2453 /// What the endpoint an operator would go and check says about this limiter.
2454 const fn where_to_look(self) -> &'static str {
2455 match self {
2456 Self::Primary => {
2457 "That is the budget `gh api rate_limit` reports, so that endpoint says when it \
2458 comes back."
2459 }
2460 Self::Secondary => {
2461 "That limiter is not the primary API budget: `gh api rate_limit` reports the \
2462 primary budget and does not report this one, so budget showing there says \
2463 nothing about this refusal, and every further attempt extends it."
2464 }
2465 }
2466 }
2467
2468 /// The next step this limiter actually calls for.
2469 const fn what_to_do(self) -> &'static str {
2470 match self {
2471 Self::Primary => {
2472 "wait for the reset `gh api rate_limit` reports, then run the command again."
2473 }
2474 Self::Secondary => {
2475 "leave this board alone for a few minutes, then run the command again — or \
2476 raise pacing.min_mutation_interval_ms on this source so it writes more slowly."
2477 }
2478 }
2479 }
2480}
2481
2482/// One rate-limit refusal, and the wait GitHub asked for if it asked for one.
2483#[derive(Debug, Clone, Copy)]
2484struct Limited {
2485 limiter: Limiter,
2486 hint: Option<u64>,
2487 /// Which header the hint came from: `retry-after`, or an exhausted primary budget's
2488 /// `x-ratelimit-reset`.
2489 hinted_by: Hint,
2490}
2491
2492/// Where a refusal's hint came from, which is what its wait is named after.
2493#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2494enum Hint {
2495 /// `retry-after`, or no hint at all.
2496 RetryAfter,
2497 /// An exhausted primary budget's `x-ratelimit-reset`.
2498 PrimaryReset,
2499}
2500
2501impl Limited {
2502 /// The wait this refusal is answered with, when the backing-off schedule is at `backoff`,
2503 /// and the cause it is named by: the hint, raised to the schedule's own next wait —
2504 /// GitHub really does send `retry-after: 0`, and retrying at once is the one move that
2505 /// extends a secondary limit — or the schedule alone when there is none.
2506 fn wait(self, backoff: Duration) -> (Duration, WaitCause) {
2507 match (self.hint, self.hinted_by) {
2508 (Some(hint), Hint::RetryAfter) => (
2509 Duration::from_secs(hint).max(backoff),
2510 pacing::cause::RETRY_AFTER,
2511 ),
2512 (Some(hint), Hint::PrimaryReset) => (
2513 Duration::from_secs(hint).max(backoff),
2514 pacing::cause::PRIMARY_RESET,
2515 ),
2516 (None, _) => (backoff, pacing::cause::BACKOFF),
2517 }
2518 }
2519
2520 /// What the caller is told once this source has waited as long as it may.
2521 ///
2522 /// Both limiters report as [`SourceError::RateLimited`], because that is what
2523 /// happened: the kind a caller matches on says a rate limit refused this, and nothing
2524 /// about *which* limiter it was makes it a different kind of failure. What differs is
2525 /// the operator's next step, and that is what the message carries — a secondary
2526 /// refusal read as a primary one sends an operator to `gh api rate_limit`, where the
2527 /// budget looks fine, and then back to retry the very burst that was refused.
2528 fn exhausted(
2529 self,
2530 doing: &str,
2531 waits: u32,
2532 waited: Duration,
2533 needed: Duration,
2534 budget: Duration,
2535 ) -> SourceError {
2536 SourceError::RateLimited {
2537 retry_after_seconds: self.hint,
2538 message: Some(format!(
2539 "{} refused this source while {doing}; it waited {} out over {} and was refused \
2540 again, and the next wait of {} would take it past the {} one call may spend \
2541 waiting. {} next: {}",
2542 self.limiter.name(),
2543 plural(waits, "refusal"),
2544 seconds(waited),
2545 seconds(needed),
2546 seconds(budget),
2547 self.limiter.where_to_look(),
2548 self.limiter.what_to_do(),
2549 )),
2550 }
2551 }
2552}
2553
2554/// One HTTP attempt's result, with what its response said about the rate limit.
2555///
2556/// The two travel together so the record and the outcome are written from the same place:
2557/// what a response said about the budget is only readable while that response is in hand,
2558/// and what the attempt *meant* is only decidable once its body has been read.
2559struct Attempted {
2560 result: Result<Value, Attempt>,
2561 limits: accounting::RateLimit,
2562 /// GitHub's own reported cost for this call, for a document that asked for it.
2563 reported_cost: Option<u64>,
2564 response_outcome: accounting::ResponseOutcome,
2565}
2566
2567/// One attempt's outcome: an error to report, or a rate limit to wait out.
2568enum Attempt {
2569 Failed(SourceError),
2570 Limited(Limited),
2571 /// GitHub refused a write as a temporary conflict and asked for it again — see
2572 /// [`CONFLICT_WORDINGS`]. Carries what GitHub said, for the diagnostic once retrying ends.
2573 Conflicted(String),
2574}
2575
2576fn plural(count: u32, thing: &str) -> String {
2577 if count == 1 {
2578 format!("{count} {thing}")
2579 } else {
2580 format!("{count} {thing}s")
2581 }
2582}
2583
2584fn seconds(duration: Duration) -> String {
2585 format!("{:.1}s", duration.as_secs_f64())
2586}
2587
2588/// The member [`stamp_served`] writes onto every node of a response that says when it last
2589/// changed: the `Date` that response was served at. No GitHub type has a field of this name.
2590const SERVED_AT: &str = "onetaskgraphServedAt";
2591
2592/// Write `served` onto every object of `data` carrying an `updatedAt`, so whatever reads a
2593/// node's time later knows the server's clock on the response that read it — which is what
2594/// decides whether that node's change token is given (see [`change_token::SETTLED`]).
2595fn stamp_served(data: &mut Value, served: &str) {
2596 match data {
2597 Value::Object(members) => {
2598 if members.contains_key("updatedAt") {
2599 members.insert(SERVED_AT.to_owned(), Value::String(served.to_owned()));
2600 }
2601 for member in members.values_mut() {
2602 stamp_served(member, served);
2603 }
2604 }
2605 Value::Array(items) => {
2606 for item in items {
2607 stamp_served(item, served);
2608 }
2609 }
2610 _ => {}
2611 }
2612}
2613
2614/// A header GitHub spells as a whole number of seconds, or `None` when this one is not.
2615///
2616/// A value that is present and unreadable is deliberately *not* an error. `retry-after` is
2617/// allowed by HTTP to be a date rather than a count, an intermediary can rewrite either
2618/// header, and neither is what makes a response a refusal — so the whole cost of one this
2619/// cannot read is that the refusal carries no hint and the backing-off schedule answers it
2620/// instead. Refusing the response over the header would turn a readable refusal into an
2621/// unreadable one, and refusing to *wait* would be the one wrong direction to fail in.
2622fn whole_seconds(value: Option<&reqwest::header::HeaderValue>) -> Option<u64> {
2623 value
2624 .and_then(|value| value.to_str().ok())
2625 .and_then(|value| value.trim().parse::<u64>().ok())
2626}
2627
2628/// A transport failure with every cause beneath it, outermost first.
2629///
2630/// The client's own text for a request it could not complete is `error sending request for
2631/// url (…)`, and the socket error that says why — a refused or reset connection, an exhausted
2632/// port range, a connection closed before its answer — is only in the chain of sources below
2633/// it. Those differ in what to do next, so all of them are kept.
2634pub(crate) fn transport_error(error: &(dyn std::error::Error + 'static)) -> String {
2635 let mut text = error.to_string();
2636 let mut cause = error.source();
2637 while let Some(next) = cause {
2638 let said = next.to_string();
2639 if !text.ends_with(&said) {
2640 text = format!("{text}: {said}");
2641 }
2642 cause = next.source();
2643 }
2644 text
2645}
2646
2647/// Every mutation this source sends creates content — an issue, a board item, a field of
2648/// one, a sub-issue link, a dependency, a comment — or edits or removes content of that
2649/// kind, and no query in [`graphql::DOCUMENTS`] does, so what the secondary limiter counts
2650/// and what the keyword says are the same set. That is what makes the keyword a sound test
2651/// rather than a convenient one: counting an edit or a removal the limiter might not have
2652/// counted costs a wait, and not counting one it did count costs the next fifty minutes.
2653pub(crate) fn is_mutation(query: &str) -> bool {
2654 query.trim_start().starts_with("mutation")
2655}
2656
2657/// What this source was doing, for a diagnostic that has to say so.
2658///
2659/// Read out of [`graphql::DOCUMENTS`], which is the inventory rather than a copy of it, so
2660/// a document added without a description is caught by that list's own gate instead of
2661/// falling through to the vague arm below.
2662fn operation_description(query: &str) -> &'static str {
2663 graphql::DOCUMENTS
2664 .iter()
2665 .find(|(document, _)| *document == query)
2666 .map_or("talking to GitHub", |(_, doing)| *doing)
2667}
2668
2669/// GitHub's published ceiling on content-generating requests in any one minute.
2670///
2671/// Pinned in `tests/fixtures/rate-limits.json` and gated against it, because it is
2672/// GitHub's number rather than this source's. This source paces at it over its window: a
2673/// content-creating request goes out at once while the minute's creations leave room for it,
2674/// and waits until they do otherwise — see the `pacing` module.
2675pub const CONTENT_CREATION_PER_MINUTE: u64 = 80;
2676/// The same ceiling as GitHub publishes it for any one hour, paced over its window as the
2677/// per-minute one is. Pinned beside its sibling so the gate would notice either one moving.
2678pub const CONTENT_CREATION_PER_HOUR: u64 = 500;
2679/// GitHub's published ceiling on secondary points in any one minute: what every GraphQL
2680/// request is charged against, a mutation [`SECONDARY_POINTS_PER_MUTATION`] and a query
2681/// [`SECONDARY_POINTS_PER_QUERY`]. Pinned and paced as [`CONTENT_CREATION_PER_MINUTE`] is.
2682pub const SECONDARY_POINTS_PER_MINUTE: u64 = 2_000;
2683/// The secondary points one GraphQL request carrying a mutation is charged.
2684pub const SECONDARY_POINTS_PER_MUTATION: u64 = 5;
2685/// The secondary points one GraphQL request carrying no mutation is charged.
2686pub const SECONDARY_POINTS_PER_QUERY: u64 = 1;
2687/// The fastest *steady* spacing of content-creating mutations that cannot exceed
2688/// [`CONTENT_CREATION_PER_MINUTE`], in milliseconds: 60000/80 is 750.
2689///
2690/// It is no longer the default. This source paces by GitHub's windows instead, so a copy's
2691/// few dozen writes go out as fast as GitHub answers them while the minute has room, rather
2692/// than 750 ms apart — which was most of a plan copy's wall time. An installation that wants
2693/// mutations spaced as well says so through `pacing.min_mutation_interval_ms`, and this is the
2694/// value that holds a long sustained burst under the per-minute bound by spacing alone.
2695pub const MIN_MUTATION_INTERVAL_MS: u64 = 60_000 / CONTENT_CREATION_PER_MINUTE;
2696/// First wait when a rate-limit refusal carries no hint; each further wait doubles it.
2697///
2698/// A doubling schedule from one second reaches a minute in six waits, which is GitHub's
2699/// own advice for a secondary limit — wait, and wait longer each time — without spending
2700/// the first minute of a transient refusal doing nothing.
2701pub const RETRY_BACKOFF_MS: u64 = 1_000;
2702/// Total time one call may spend waiting out rate limits before it reports a failure.
2703///
2704/// Two minutes is long enough to ride out the refusals a paced copy still collects and
2705/// short enough that a command an operator is watching returns. The bound is what makes
2706/// the wait a wait rather than a hang: a call refused past it ends in a diagnostic naming
2707/// the limiter, not in a process nobody can tell from a wedged one.
2708pub const RETRY_BUDGET_MS: u64 = 120_000;
2709
2710fn default_token_env() -> String {
2711 "GH_PROJECTS_TOKEN".to_owned()
2712}
2713fn default_endpoint() -> String {
2714 "https://api.github.com/graphql".to_owned()
2715}
2716
2717/// The name of a `Status` single-select option on the board.
2718///
2719/// Validated on the way in rather than checked later, so a blank option name — which
2720/// nothing on a board can be — is a state this type cannot hold.
2721#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2722#[serde(try_from = "String")]
2723#[schemars(extend("minLength" = 1))]
2724pub struct ColumnName(String);
2725
2726impl ColumnName {
2727 /// The option name, as the board spells it.
2728 fn as_str(&self) -> &str {
2729 &self.0
2730 }
2731}
2732
2733impl TryFrom<String> for ColumnName {
2734 type Error = String;
2735
2736 fn try_from(name: String) -> Result<Self, Self::Error> {
2737 if name.trim().is_empty() {
2738 return Err("a status_mapping option name cannot be blank".to_owned());
2739 }
2740 Ok(Self(name))
2741 }
2742}
2743
2744/// The two closed states this product can mean.
2745///
2746/// GitHub's `IssueClosedStateReason` also spells `DUPLICATE`, which nothing here ever writes:
2747/// an issue closed as one *reads* `cancelled`, because its work is another issue's, but this
2748/// product never decides on a person's behalf that two issues are the same work.
2749#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, schemars::JsonSchema)]
2750#[serde(rename_all = "kebab-case")]
2751pub enum ClosedState {
2752 /// `COMPLETED` — precisely done.
2753 Completed,
2754 /// `NOT_PLANNED` — precisely cancelled.
2755 NotPlanned,
2756}
2757
2758impl ClosedState {
2759 const fn reason(self) -> &'static str {
2760 match self {
2761 Self::Completed => "COMPLETED",
2762 Self::NotPlanned => "NOT_PLANNED",
2763 }
2764 }
2765}
2766
2767/// Configuration for one GitHub Projects v2 board.
2768///
2769/// It serializes to a document this same type reads back as itself, so a caller writing a
2770/// configuration — a consumer filing onto a board it configures — can build one here rather
2771/// than spell the shape again; an empty `metadata_fields` is left out, as one never set.
2772#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2773#[serde(default, deny_unknown_fields)]
2774pub struct GitHubProjectsConfig {
2775 /// Login of the user or organization which owns the board.
2776 pub owner: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates GitHub's owner grammar before private construction.
2777 /// The project number shown in the board's GitHub URL.
2778 pub project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` bounds this to a positive GraphQL Int.
2779 // 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.
2780 /// `owner/name` of the repository this source creates an issue in when the item's own
2781 /// `repositories` field does not decide it.
2782 ///
2783 /// An item naming exactly one repository is created there — on a source declared
2784 /// `visibility: private`, only when that repository reads private, and otherwise as an
2785 /// item naming none is; a task or a document naming none or several is created in its
2786 /// parent project's repository; and a project, or a task or document with no parent,
2787 /// naming none or several is created here. A board
2788 /// has no repository of its own and `createIssue` requires one, so a write without
2789 /// this is refused naming the field. Reads never need it.
2790 pub repository: Option<String>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the `owner/name` grammar before private construction.
2791 // llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
2792 /// GitHub logins whose repositories this source may create an issue in.
2793 ///
2794 /// Absent or empty, the default, restricts nothing. Otherwise a create whose issue the
2795 /// rule `repository` describes would file in a repository owned by anyone else is refused
2796 /// before anything of it is written — by `task copy`, `task copy --create`, a member
2797 /// create of a project copy and a dry run of each — with the error kind `creation-owner`,
2798 /// naming the repository and these owners. Logins compare case-insensitively, as GitHub
2799 /// compares them. An update of an issue that already exists is never refused by it.
2800 /// `task locate` reports such a refusal as `creates_in.refused` rather than failing.
2801 #[serde(default, skip_serializing_if = "Vec::is_empty")]
2802 // Kept in the schema as `"default": []` although a serialized configuration leaves an empty
2803 // list out, so both SDKs model an absent list as an empty one rather than as `null`.
2804 #[schemars(!skip_serializing_if)]
2805 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.
2806 /// Environment variable containing a fine-grained token with Projects and Issues
2807 /// read/write plus Pull requests read-only access for every repository represented on
2808 /// the board.
2809 #[serde(default = "default_token_env")]
2810 pub token_env: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the environment-variable grammar.
2811 /// GraphQL endpoint. GitHub Enterprise installations may override it.
2812 #[serde(default = "default_endpoint")]
2813 pub endpoint: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` converts it to the private validated `Url`.
2814 /// Per-instance mapping from a status category to the option of the board's one
2815 /// `Status` field it lands on, for a task and for a project.
2816 ///
2817 /// The shared `StatusMapping` grammar: each value is one option name for both kinds,
2818 /// `null` to disable the category for both, or `{task, project}` naming it per kind,
2819 /// where a kind left out leaves the category unmapped for that kind. A category this
2820 /// does not mention keeps its shipped default for both kinds: `backlog` to "Backlog",
2821 /// `todo` to "Todo", `queued` to "Queued", `in-progress` to "In Progress", `done` to
2822 /// "Done" plus closed as completed, `cancelled` to "Cancelled" plus closed as not
2823 /// planned, and `draft` and `unknown` unmapped. A category it does mention gets no
2824 /// shipped default for a kind it leaves out. `done` and `cancelled` close the issue for
2825 /// either kind. No two categories may name one option for the same kind, ignoring case.
2826 /// `unknown` may name one existing option; every unknown word then lands on it and
2827 /// reads back as `unknown` under its name. Unlike `local-md`, this source cannot keep
2828 /// each unknown word because it never creates board options.
2829 #[serde(default)]
2830 pub status_mapping: StatusMapping,
2831 /// Per-instance mapping from a task's priority to an option of this board's
2832 /// single-select field named `Priority`.
2833 ///
2834 /// Absent, this source holds no priority: every task reads as `none`, and a write of any
2835 /// other priority is refused before it reaches this board. Present, each of `urgent`,
2836 /// `high`, `medium` and `low` it does not mention keeps its shipped default — `Urgent`,
2837 /// `High`, `Medium` and `Low` — and an item with no value in the `Priority` field reads
2838 /// as `none`, so writing `none` clears the value. Option names match case-insensitively;
2839 /// no two levels may name one option. Reads and writes never create the field or an
2840 /// option: `onetaskgraph sources fields <source> --apply` does, and a write naming one
2841 /// the board lacks is refused pointing there.
2842 #[serde(default)]
2843 pub priority_mapping: Option<PriorityMappingConfig>,
2844 /// Caller metadata values this source also writes to board text fields, so the board can
2845 /// filter on them.
2846 ///
2847 /// Absent or empty, nothing is projected. Each entry names a board text `field`, a
2848 /// top-level metadata `key` and an optional `path` of object keys walked inside that key's
2849 /// value. On every item write — create, update and copy, of a task, a project or a
2850 /// document — a string found there is written to the field, and nothing is sent when the
2851 /// field already holds it; nothing there, or `null`, clears the field when it holds a
2852 /// value; any other JSON type is refused before any mutation. The metadata comment in the
2853 /// issue body stays the one home of the value, and every read reports metadata from it.
2854 /// Reads and writes never create the field: `onetaskgraph sources fields <source>
2855 /// --apply` does, as a text field, and a write to a board lacking it, or holding a
2856 /// non-text field of that name, is refused pointing there.
2857 #[serde(default, skip_serializing_if = "Vec::is_empty")]
2858 // Kept in the schema as `"default": []` although a serialized configuration leaves an empty
2859 // list out, so both SDKs model an absent list as an empty one rather than as `null`.
2860 #[schemars(!skip_serializing_if)]
2861 pub metadata_fields: Vec<MetadataFieldConfig>,
2862 /// How fast this source writes, and how long it waits out a rate-limit refusal.
2863 ///
2864 /// Every field keeps its shipped default when it is absent, and the defaults are
2865 /// GitHub's own published limits rather than taste. See [`Pacing`].
2866 #[serde(default)]
2867 pub pacing: PacingConfig,
2868}
2869
2870/// Which option of the board's `Priority` field each priority lands on.
2871///
2872/// One member per level rather than a map, so a key that is not a level is refused where
2873/// the configuration is read, naming the levels there are. `none` is not a member: it is no
2874/// value in the field, not an option of it.
2875#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2876#[serde(default, deny_unknown_fields)]
2877pub struct PriorityMappingConfig {
2878 /// The option `urgent` lands on; `Urgent` when absent.
2879 pub urgent: Option<PriorityOptionName>,
2880 /// The option `high` lands on; `High` when absent.
2881 pub high: Option<PriorityOptionName>,
2882 /// The option `medium` lands on; `Medium` when absent.
2883 pub medium: Option<PriorityOptionName>,
2884 /// The option `low` lands on; `Low` when absent.
2885 pub low: Option<PriorityOptionName>,
2886}
2887
2888/// The name of an option of the board's `Priority` single-select field.
2889///
2890/// Validated on the way in, for the reason [`ColumnName`] is: nothing on a board can have a
2891/// blank name.
2892#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2893#[serde(try_from = "String")]
2894#[schemars(extend("minLength" = 1))]
2895pub struct PriorityOptionName(String);
2896
2897impl PriorityOptionName {
2898 /// The option name, as the board spells it.
2899 fn as_str(&self) -> &str {
2900 &self.0
2901 }
2902}
2903
2904impl TryFrom<String> for PriorityOptionName {
2905 type Error = String;
2906
2907 fn try_from(name: String) -> Result<Self, Self::Error> {
2908 if name.trim().is_empty() {
2909 return Err("a priority_mapping option name cannot be blank".to_owned());
2910 }
2911 Ok(Self(name))
2912 }
2913}
2914
2915/// The name of the board field a priority is held in.
2916pub const PRIORITY_FIELD: &str = "Priority";
2917
2918/// The four priorities a board option can hold, in the order a new `Priority` field lists
2919/// them. `none` is not among them: it is the field holding no value.
2920///
2921/// This list mirrors `Priority`, so it carries its own drift gate, in the shape [`CATEGORIES`]
2922/// does: [`level_position`] is a wildcard-free match, so a priority added to the shared
2923/// vocabulary fails to compile until it is placed there, and this crate's suite reconciles
2924/// this list and [`PriorityMappingConfig`]'s members against that enum's own derived schema.
2925pub const PRIORITY_LEVELS: [Priority; 4] = [
2926 Priority::Urgent,
2927 Priority::High,
2928 Priority::Medium,
2929 Priority::Low,
2930];
2931
2932/// Where one priority sits in [`PRIORITY_LEVELS`], or `None` for `none`, which is no option;
2933/// see that list for what this pins.
2934#[must_use]
2935pub const fn level_position(priority: Priority) -> Option<usize> {
2936 match priority {
2937 Priority::None => None,
2938 Priority::Urgent => Some(0),
2939 Priority::High => Some(1),
2940 Priority::Medium => Some(2),
2941 Priority::Low => Some(3),
2942 }
2943}
2944
2945/// This instance's complete priority-to-option mapping, read in both directions.
2946///
2947/// One option per level, held in [`PRIORITY_LEVELS`] order, once it is established that no
2948/// two levels name one option.
2949#[derive(Debug, Clone)]
2950struct PriorityMapping {
2951 options: [PriorityOptionName; 4],
2952}
2953
2954impl PriorityMapping {
2955 fn resolve(config: PriorityMappingConfig, instance: &SourceName) -> Result<Self, SourceError> {
2956 let shipped = |name: &str| PriorityOptionName(name.to_owned());
2957 let mapping = Self {
2958 options: [
2959 config.urgent.unwrap_or_else(|| shipped("Urgent")),
2960 config.high.unwrap_or_else(|| shipped("High")),
2961 config.medium.unwrap_or_else(|| shipped("Medium")),
2962 config.low.unwrap_or_else(|| shipped("Low")),
2963 ],
2964 };
2965 for (index, option) in mapping.options.iter().enumerate() {
2966 if let Some(earlier) = mapping.options[..index]
2967 .iter()
2968 .position(|other| other.as_str().eq_ignore_ascii_case(option.as_str()))
2969 {
2970 return Err(SourceError::Config {
2971 message: format!(
2972 "priority_mapping of source {instance} sends both {} and {} to the board \
2973 option {:?}; one option cannot read back as two priorities",
2974 PRIORITY_LEVELS[earlier],
2975 PRIORITY_LEVELS[index],
2976 option.as_str()
2977 ),
2978 });
2979 }
2980 }
2981 Ok(mapping)
2982 }
2983
2984 /// The option `priority` lands on, or `None` for `none`, which is no option at all.
2985 fn option(&self, priority: Priority) -> Option<&str> {
2986 level_position(priority).map(|index| self.options[index].as_str())
2987 }
2988
2989 /// The priority a board option name reports, or `None` when nothing maps to it.
2990 fn priority_of(&self, option: &str) -> Option<Priority> {
2991 self.options
2992 .iter()
2993 .position(|name| name.as_str().eq_ignore_ascii_case(option))
2994 .map(|index| PRIORITY_LEVELS[index])
2995 }
2996
2997 /// Every mapped option name, in the order a new `Priority` field lists them.
2998 fn names(&self) -> impl Iterator<Item = &str> {
2999 self.options.iter().map(PriorityOptionName::as_str)
3000 }
3001}
3002
3003/// One metadata value this source projects onto a board text field.
3004#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3005#[serde(deny_unknown_fields)]
3006pub struct MetadataFieldConfig {
3007 /// The name of the board text field the value is written to.
3008 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.
3009 /// The top-level metadata key the value is read from. A key under the reserved
3010 /// `onetaskgraph.` prefix is refused.
3011 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.
3012 /// Object keys walked inside that key's value, outermost first. Absent or empty, the
3013 /// key's own value is the one projected. Left out of a serialized entry when empty.
3014 #[serde(default, skip_serializing_if = "Vec::is_empty")]
3015 // Kept in the schema as `"default": []`, for the reason `metadata_fields`' is.
3016 #[schemars(!skip_serializing_if)]
3017 pub path: Vec<String>,
3018}
3019
3020/// The board fields no metadata value may be projected onto, compared ignoring case: the
3021/// three this source writes itself, and those GitHub owns on every board.
3022// 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.
3023const UNPROJECTABLE_FIELDS: [&str; 13] = [
3024 STATUS_FIELD,
3025 PRIORITY_FIELD,
3026 ORIGIN_FIELD,
3027 "Title",
3028 "Assignees",
3029 "Labels",
3030 "Linked pull requests",
3031 "Milestone",
3032 "Repository",
3033 "Reviewers",
3034 "Parent issue",
3035 "Sub-issues progress",
3036 "Type",
3037];
3038
3039/// One validated [`MetadataFieldConfig`] entry.
3040#[derive(Debug, Clone)]
3041struct MetadataField {
3042 field: ProjectedField,
3043 key: ProjectedKey,
3044 path: Vec<String>,
3045}
3046
3047/// The name of a board text field a metadata value is projected onto: never blank, never one
3048/// this source or GitHub writes, and never one another entry of the same source names.
3049#[derive(Debug, Clone, PartialEq, Eq)]
3050struct ProjectedField(String);
3051
3052/// The top-level metadata key a projected value is read from: never blank, and never under
3053/// the namespace this product reserves.
3054#[derive(Debug, Clone, PartialEq, Eq)]
3055struct ProjectedKey(String);
3056
3057impl std::ops::Deref for ProjectedField {
3058 type Target = str;
3059 fn deref(&self) -> &str {
3060 &self.0
3061 }
3062}
3063
3064impl std::ops::Deref for ProjectedKey {
3065 type Target = str;
3066 fn deref(&self) -> &str {
3067 &self.0
3068 }
3069}
3070
3071/// What one item's metadata says one projected field should hold.
3072#[derive(Debug, Clone, PartialEq, Eq)]
3073enum Projected {
3074 /// A string to hold.
3075 Text(String),
3076 /// Nothing: the key or a step of the path is absent, or the value is `null`.
3077 Nothing,
3078}
3079
3080impl MetadataField {
3081 /// Validate every entry of one instance's `metadata_fields`, refusing the first that
3082 /// cannot stand, naming the source and the entry.
3083 fn resolve(
3084 configured: Vec<MetadataFieldConfig>,
3085 instance: &SourceName,
3086 ) -> Result<Vec<Self>, SourceError> {
3087 let mut resolved: Vec<Self> = Vec::with_capacity(configured.len());
3088 for (index, entry) in configured.into_iter().enumerate() {
3089 let refuse = |why: String| SourceError::Config {
3090 message: format!(
3091 "metadata_fields[{index}] of source {instance} (field {:?}, key {:?}) {why}",
3092 entry.field, entry.key
3093 ),
3094 };
3095 if entry.field.trim().is_empty() {
3096 return Err(refuse("names a blank field".to_owned()));
3097 }
3098 if entry.key.trim().is_empty() {
3099 return Err(refuse("names a blank key".to_owned()));
3100 }
3101 if entry.key.split('.').next() == Some(MetadataKey::RESERVED_NAMESPACE) {
3102 return Err(refuse(format!(
3103 "names a key under the reserved \"{}.\" prefix, which this product keeps \
3104 for its own keys; next: project a caller key instead",
3105 MetadataKey::RESERVED_NAMESPACE
3106 )));
3107 }
3108 if let Some(owned) = UNPROJECTABLE_FIELDS
3109 .iter()
3110 .find(|owned| owned.eq_ignore_ascii_case(&entry.field))
3111 {
3112 return Err(refuse(format!(
3113 "names the board field {owned:?}, which this source or GitHub already \
3114 writes; next: name a text field of your own"
3115 )));
3116 }
3117 if let Some(earlier) = resolved
3118 .iter()
3119 .position(|other| other.field.eq_ignore_ascii_case(&entry.field))
3120 {
3121 return Err(refuse(format!(
3122 "names the same board field as metadata_fields[{earlier}]; one field \
3123 cannot hold two values"
3124 )));
3125 }
3126 resolved.push(Self {
3127 field: ProjectedField(entry.field),
3128 key: ProjectedKey(entry.key),
3129 path: entry.path,
3130 });
3131 }
3132 Ok(resolved)
3133 }
3134
3135 /// The path as a refusal spells it: `[]` for the key's own value.
3136 fn spelled_path(&self) -> String {
3137 serde_json::to_string(&self.path).unwrap_or_else(|_| format!("{:?}", self.path))
3138 }
3139
3140 /// What `metadata` says this field should hold, or the refusal for a value no text field
3141 /// can hold.
3142 fn projected(
3143 &self,
3144 metadata: &BTreeMap<String, Value>,
3145 instance: &SourceName,
3146 ) -> Result<Projected, SourceError> {
3147 let mut value = metadata.get(&*self.key);
3148 for step in &self.path {
3149 value = value.and_then(|held| held.get(step.as_str()));
3150 }
3151 let found = match value {
3152 None | Some(Value::Null) => return Ok(Projected::Nothing),
3153 Some(Value::String(text)) => return Ok(Projected::Text(text.clone())),
3154 Some(Value::Bool(_)) => "a boolean",
3155 Some(Value::Number(_)) => "a number",
3156 Some(Value::Array(_)) => "an array",
3157 Some(Value::Object(_)) => "an object",
3158 };
3159 Err(SourceError::Refused {
3160 message: format!(
3161 "source {instance} projects metadata key {:?} at path {} onto its board text \
3162 field {:?}, and this item holds {found} there; only a string, null or nothing \
3163 can be written to a text field; next: store a string there, or remove the \
3164 value",
3165 &*self.key,
3166 self.spelled_path(),
3167 &*self.field
3168 ),
3169 })
3170 }
3171}
3172
3173/// What one item's `Priority` field says, read through this instance's mapping.
3174#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
3175enum HeldPriority {
3176 /// A priority this source reports: an option the mapping names, or no value (`none`).
3177 Read(Priority),
3178 /// An option the mapping does not name, which is never read as a level or as `none`.
3179 Unmapped(String),
3180}
3181
3182/// How fast this source writes, and how long it waits out a rate-limit refusal.
3183///
3184/// Configurable because a GitHub Enterprise installation sets its own limits and an
3185/// operator who has already been refused may want to go slower still — not because the
3186/// defaults are guesses.
3187#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3188#[serde(default, deny_unknown_fields)]
3189pub struct PacingConfig {
3190 /// Shortest interval between two content-creating mutations, in milliseconds.
3191 ///
3192 /// Zero sends them as fast as they are asked for, which is what a fixture server on
3193 /// loopback wants and what no board on github.com does. At most [`MAX_PACING_MS`].
3194 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.
3195 /// First wait when a rate-limit refusal carries no hint, in milliseconds. Each
3196 /// further wait of the same call doubles it. At most [`MAX_PACING_MS`], and never
3197 /// zero while there is a budget to spend, because a schedule of zero-length waits
3198 /// consumes none of it and so never ends.
3199 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.
3200 /// Total time one call may spend waiting out rate limits, in milliseconds.
3201 ///
3202 /// Zero reports the refusal rather than waiting at all. At most [`MAX_PACING_MS`]:
3203 /// the bound is what makes this a wait rather than a hang.
3204 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.
3205}
3206
3207/// The largest any pacing setting may be, in milliseconds.
3208///
3209/// One hour. GitHub's own harshest published bound on content-generating requests works
3210/// out at one every 7.2 seconds, so an hour is already three orders of magnitude past
3211/// anything a real limit asks for, and past it the settings stop describing pacing at all:
3212/// a wait budget beyond it is the unbounded wait this whole mechanism exists to replace,
3213/// and an interval beyond it is a command that never sends its second mutation. It also
3214/// keeps the clock arithmetic in [`GitHubProjectsSource::reserve_mutation_slot`] inside
3215/// what a `Duration` can hold on every platform.
3216pub const MAX_PACING_MS: u64 = 3_600_000;
3217
3218/// [`PacingConfig`] with every default resolved and every value checked, which is what the
3219/// source holds.
3220///
3221/// Unset, `min_mutation_interval_ms` spaces nothing, and every request is paced by GitHub's
3222/// secondary windows alone — see the `pacing` module; set, it spaces content-creating mutations
3223/// on top of those windows, [`MIN_MUTATION_INTERVAL_MS`] being the spacing that holds a sustained
3224/// burst under the per-minute bound by spacing alone; and zero sends every request as fast as it
3225/// is asked for, windows included.
3226#[derive(Debug, Clone, Copy)]
3227struct Pacing {
3228 /// Whether requests are held for GitHub's secondary windows: always, unless
3229 /// `min_mutation_interval_ms` is set to exactly zero, which has always asked for no
3230 /// pacing at all — what a fixture server on loopback wants and no board on github.com does.
3231 windows: bool,
3232 min_mutation_interval: Duration,
3233 retry_backoff: Duration,
3234 retry_budget: Duration,
3235}
3236
3237impl Pacing {
3238 /// Resolve one instance's pacing, refusing a configuration that would not pace at all.
3239 fn resolve(config: PacingConfig, instance: &SourceName) -> Result<Self, SourceError> {
3240 let bounded = |value: Option<u64>, default: u64, field: &str| match value {
3241 Some(value) if value > MAX_PACING_MS => Err(SourceError::Config {
3242 message: format!(
3243 "pacing.{field} of source {instance} is {value} ms, and the most any pacing \
3244 setting may be is {MAX_PACING_MS} ms — an hour, which is already far past \
3245 GitHub's own harshest published limit"
3246 ),
3247 }),
3248 Some(value) => Ok(Duration::from_millis(value)),
3249 None => Ok(Duration::from_millis(default)),
3250 };
3251 let retry_backoff = bounded(
3252 config.retry_backoff_ms,
3253 RETRY_BACKOFF_MS,
3254 "retry_backoff_ms",
3255 )?;
3256 let retry_budget = bounded(config.retry_budget_ms, RETRY_BUDGET_MS, "retry_budget_ms")?;
3257 if retry_backoff.is_zero() && !retry_budget.is_zero() {
3258 return Err(SourceError::Config {
3259 message: format!(
3260 "pacing.retry_backoff_ms of source {instance} is 0 while \
3261 pacing.retry_budget_ms is {} ms; a schedule of zero-length waits spends \
3262 none of that budget, so it would retry a refusal forever. Set a backoff of \
3263 at least 1 ms, or set retry_budget_ms to 0 to report a refusal without \
3264 waiting at all",
3265 retry_budget.as_millis()
3266 ),
3267 });
3268 }
3269 Ok(Self {
3270 windows: config.min_mutation_interval_ms != Some(0),
3271 min_mutation_interval: bounded(
3272 config.min_mutation_interval_ms,
3273 0,
3274 "min_mutation_interval_ms",
3275 )?,
3276 retry_backoff,
3277 retry_budget,
3278 })
3279 }
3280}
3281
3282/// Factory for [`GitHubProjectsSource`].
3283#[derive(Debug, Clone, Copy, Default)]
3284pub struct Plugin;
3285
3286impl SourcePlugin for Plugin {
3287 fn kind(&self) -> &'static str {
3288 KIND
3289 }
3290 /// The endpoint, the owner and the board's number: what names one board.
3291 fn remote_identity(&self, config: &Value) -> Option<String> {
3292 let config: GitHubProjectsConfig = serde_json::from_value(config.clone()).ok()?;
3293 Some(format!(
3294 "{}\n{}\n{}",
3295 config.endpoint, config.owner, config.project_number
3296 ))
3297 }
3298 fn config_schema(&self) -> Schema {
3299 schema_for!(GitHubProjectsConfig)
3300 }
3301 fn build(
3302 &self,
3303 name: &SourceName,
3304 config: &Value,
3305 secrets: &dyn SecretResolver,
3306 ) -> Result<Box<dyn TaskSource>, SourceError> {
3307 self.build_recording_into(name, config, secrets, Arc::new(Accounting::new()))
3308 }
3309
3310 fn build_with_clock(
3311 &self,
3312 name: &SourceName,
3313 config: &Value,
3314 secrets: &dyn SecretResolver,
3315 clock: SharedClock,
3316 ) -> Result<Box<dyn TaskSource>, SourceError> {
3317 self.build_recording_with_clock(name, config, secrets, Arc::new(Accounting::new()), clock)
3318 }
3319}
3320
3321/// The client `cell` holds, building it with `build` the first time it is asked for.
3322///
3323/// A failed build is the request's failure, with the message the source's construction gave it
3324/// before clients were deferred.
3325// 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`.
3326fn built(
3327 cell: &std::sync::OnceLock<Client>,
3328 build: impl FnOnce() -> Result<Client, SourceError>,
3329) -> Result<&Client, SourceError> {
3330 if let Some(client) = cell.get() {
3331 return Ok(client);
3332 }
3333 let client = build()?;
3334 Ok(cell.get_or_init(|| client))
3335}
3336
3337impl Plugin {
3338 /// Build a source recording every request it sends into an accounting the caller holds.
3339 ///
3340 /// [`SourcePlugin::build`] is this with an accounting of its own, which is what the
3341 /// registry gets. This is for a caller that is also calling GitHub itself and wants one
3342 /// session total rather than two — see [`accounting`] and
3343 /// [`GitHubProjectsSource::recording_into`].
3344 ///
3345 /// # Errors
3346 ///
3347 /// Exactly [`SourcePlugin::build`]'s, with the same source name in front of each:
3348 /// [`SourceError::Config`] for configuration this plugin cannot use and
3349 /// [`SourceError::Auth`] for a credential it cannot find.
3350 pub fn build_recording_into(
3351 &self,
3352 name: &SourceName,
3353 config: &Value,
3354 secrets: &dyn SecretResolver,
3355 ledger: Arc<Accounting>,
3356 ) -> Result<Box<dyn TaskSource>, SourceError> {
3357 self.build_recording_with_clock(name, config, secrets, ledger, system_clock())
3358 }
3359
3360 fn build_recording_with_clock(
3361 &self,
3362 name: &SourceName,
3363 config: &Value,
3364 secrets: &dyn SecretResolver,
3365 ledger: Arc<Accounting>,
3366 clock: SharedClock,
3367 ) -> Result<Box<dyn TaskSource>, SourceError> {
3368 let config: GitHubProjectsConfig =
3369 serde_json::from_value(config.clone()).map_err(|e| SourceError::Config {
3370 message: format!("source {name}: {e}"),
3371 })?;
3372 let prefix = format!("source {name}: ");
3373 let mut source = GitHubProjectsSource::recording_into(name, config, secrets, ledger)
3374 .map_err(|error| match error {
3375 // The shared `StatusMapping::distinct` names the source itself.
3376 SourceError::Config { message } if message.starts_with(&prefix) => {
3377 SourceError::Config { message }
3378 }
3379 SourceError::Config { message } => SourceError::Config {
3380 message: format!("{prefix}{message}"),
3381 },
3382 SourceError::Auth { message } => SourceError::Auth {
3383 message: format!("source {name}: {message}"),
3384 },
3385 other => other,
3386 })?;
3387 source.clock = clock;
3388 Ok(Box::new(source))
3389 }
3390}
3391
3392/// Where a status category lands on this board, once configuration is resolved.
3393#[derive(Debug, Clone, PartialEq, Eq)]
3394enum StatusTarget {
3395 /// Not usable against this instance for this kind, and why.
3396 Disabled(UnmappedStatus),
3397 /// The board's `Status` option of this name.
3398 Column(ColumnName),
3399 /// A closed issue, with both its board option and the reason that says which closed it means.
3400 // 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.
3401 Terminal(ColumnName, ClosedState),
3402}
3403
3404/// Every status category, in the order the vocabulary declares them.
3405///
3406/// This list mirrors `StatusCategory`, so it carries its own drift gate rather than a
3407/// reviewer's attention: [`category_position`] is a wildcard-free match, so a variant
3408/// added to the shared vocabulary fails to compile until it is named there, and this
3409/// crate's suite reconciles this list against that enum's own derived schema, which is
3410/// generated from the variants rather than written beside them. The schema is what
3411/// catches a list left one short — a list checking only the positions it already holds
3412/// would pass while every mapping indexed by the new position panicked.
3413pub const CATEGORIES: [StatusCategory; 8] = [
3414 StatusCategory::Draft,
3415 StatusCategory::Backlog,
3416 StatusCategory::Todo,
3417 StatusCategory::Queued,
3418 StatusCategory::InProgress,
3419 StatusCategory::Done,
3420 StatusCategory::Cancelled,
3421 StatusCategory::Unknown,
3422];
3423
3424/// Where one category sits in [`CATEGORIES`]; see that list for what this pins.
3425#[must_use]
3426pub const fn category_position(category: StatusCategory) -> usize {
3427 match category {
3428 StatusCategory::Draft => 0,
3429 StatusCategory::Backlog => 1,
3430 StatusCategory::Todo => 2,
3431 StatusCategory::Queued => 3,
3432 StatusCategory::InProgress => 4,
3433 StatusCategory::Done => 5,
3434 StatusCategory::Cancelled => 6,
3435 StatusCategory::Unknown => 7,
3436 }
3437}
3438
3439/// The spelling a status category is configured and reported under.
3440fn category_name(category: StatusCategory) -> &'static str {
3441 match category {
3442 StatusCategory::Draft => "draft",
3443 StatusCategory::Backlog => "backlog",
3444 StatusCategory::Todo => "todo",
3445 StatusCategory::Queued => "queued",
3446 StatusCategory::InProgress => "in-progress",
3447 StatusCategory::Done => "done",
3448 StatusCategory::Cancelled => "cancelled",
3449 StatusCategory::Unknown => "unknown",
3450 }
3451}
3452
3453/// A shipped default's option name.
3454///
3455/// The literals below are this file's own and non-blank, and they are validated by the
3456/// one constructor a configured name goes through rather than beside it.
3457fn shipped_column(name: &'static str) -> ColumnName {
3458 ColumnName::try_from(name.to_owned()).expect("a shipped default names a board option")
3459}
3460
3461/// The shipped default for one category this instance's `status_mapping` does not mention,
3462/// for either kind.
3463fn shipped_default(category: StatusCategory) -> StatusTarget {
3464 match category {
3465 StatusCategory::Backlog => StatusTarget::Column(shipped_column("Backlog")),
3466 StatusCategory::Todo => StatusTarget::Column(shipped_column("Todo")),
3467 StatusCategory::Queued => StatusTarget::Column(shipped_column("Queued")),
3468 StatusCategory::InProgress => StatusTarget::Column(shipped_column("In Progress")),
3469 StatusCategory::Done => {
3470 StatusTarget::Terminal(shipped_column("Done"), ClosedState::Completed)
3471 }
3472 StatusCategory::Cancelled => {
3473 StatusTarget::Terminal(shipped_column("Cancelled"), ClosedState::NotPlanned)
3474 }
3475 StatusCategory::Draft | StatusCategory::Unknown => {
3476 StatusTarget::Disabled(UnmappedStatus::Unconfigured)
3477 }
3478 }
3479}
3480
3481/// The two kinds a status is written and read for, each with its own half of the mapping.
3482const STATUS_KINDS: [ItemKind; 2] = [ItemKind::Task, ItemKind::Project];
3483
3484/// This instance's complete category-to-target mapping for each kind, read in both
3485/// directions.
3486///
3487/// One target per category per kind, held at that category's own [`category_position`], so
3488/// a category missing from the mapping, named twice in it, or filed out of order is a state
3489/// this type cannot hold rather than one [`Self::target`] has to defend against. Both kinds'
3490/// targets are options of the board's one `Status` field.
3491#[derive(Debug, Clone)]
3492struct BoardStatuses {
3493 tasks: [StatusTarget; CATEGORIES.len()],
3494 projects: [StatusTarget; CATEGORIES.len()],
3495}
3496
3497impl BoardStatuses {
3498 /// Resolve `configured` against the shipped defaults, refusing two categories one kind
3499 /// would read back from one option.
3500 ///
3501 /// A category the mapping does not mention keeps its shipped default for both kinds; one
3502 /// it does mention is exactly what it configures, so a per-kind object leaves the kind it
3503 /// omits unmapped rather than defaulted.
3504 fn resolve(configured: &StatusMapping, instance: &SourceName) -> Result<Self, SourceError> {
3505 let resolve_kind =
3506 |kind: ItemKind| -> Result<[StatusTarget; CATEGORIES.len()], SourceError> {
3507 // `CATEGORIES[position] == category` for every category — the crate's suite
3508 // asserts it — so mapping the list in order fills each category's own slot.
3509 let mut targets = CATEGORIES.map(shipped_default);
3510 for (slot, category) in targets.iter_mut().zip(CATEGORIES) {
3511 if !configured.mentions(category) {
3512 continue;
3513 }
3514 *slot = match configured.name_for(category, kind) {
3515 Err(why) => StatusTarget::Disabled(why),
3516 Ok(name) => {
3517 let option = ColumnName::try_from(name.as_str().to_owned())
3518 .map_err(|message| SourceError::Config { message })?;
3519 match category {
3520 StatusCategory::Done => {
3521 StatusTarget::Terminal(option, ClosedState::Completed)
3522 }
3523 StatusCategory::Cancelled => {
3524 StatusTarget::Terminal(option, ClosedState::NotPlanned)
3525 }
3526 _ => StatusTarget::Column(option),
3527 }
3528 }
3529 };
3530 }
3531 StatusMapping::distinct(
3532 instance,
3533 kind,
3534 CATEGORIES
3535 .iter()
3536 .zip(&targets)
3537 .filter_map(|(category, target)| target.option().map(|o| (*category, o))),
3538 )?;
3539 Ok(targets)
3540 };
3541 Ok(Self {
3542 tasks: resolve_kind(ItemKind::Task)?,
3543 projects: resolve_kind(ItemKind::Project)?,
3544 })
3545 }
3546
3547 /// Every category's target for `kind`, in category order.
3548 const fn targets(&self, kind: ItemKind) -> &[StatusTarget; CATEGORIES.len()] {
3549 match kind {
3550 ItemKind::Task => &self.tasks,
3551 ItemKind::Project => &self.projects,
3552 }
3553 }
3554
3555 fn target(&self, kind: ItemKind, category: StatusCategory) -> &StatusTarget {
3556 &self.targets(kind)[category_position(category)]
3557 }
3558
3559 /// The category a board option name reports for `kind`, or `None` when nothing of that
3560 /// kind maps to it.
3561 fn category_of(&self, kind: ItemKind, option: &str) -> Option<StatusCategory> {
3562 CATEGORIES.into_iter().find(|category| {
3563 self.target(kind, *category)
3564 .option()
3565 .is_some_and(|name| name.eq_ignore_ascii_case(option))
3566 })
3567 }
3568
3569 /// Every option name either kind maps a category to, each once ignoring case, in
3570 /// category order with a task's name before a project's — what the guarded setup asks
3571 /// the `Status` field to hold.
3572 fn wanted(&self) -> Vec<String> {
3573 let mut wanted: Vec<String> = Vec::new();
3574 for category in CATEGORIES {
3575 for kind in STATUS_KINDS {
3576 if let Some(name) = self.target(kind, category).option()
3577 && !wanted.iter().any(|held| held.eq_ignore_ascii_case(name))
3578 {
3579 wanted.push(name.to_owned());
3580 }
3581 }
3582 }
3583 wanted
3584 }
3585
3586 /// The status an item of `kind` reports, from the three things a read of it says: its
3587 /// board `Status` option, whether its issue is closed, and the reason it was closed with.
3588 ///
3589 /// **A closed issue's category follows its closure**, by [`closed_category`]: completed
3590 /// or no reason reads `done`, not planned and duplicate read `cancelled`, and any other
3591 /// reason reads `unknown`. `REOPENED`-while-closed is a state this source can never
3592 /// produce, so it is read permissively rather than refused — reads are faithful, and
3593 /// refusals belong on writes.
3594 ///
3595 /// **Its name follows the closure too, unless the option agrees with it.** The option's
3596 /// own name is kept when this kind's mapping reads that option as the same category, or
3597 /// does not name it at all — so a closed issue in a "Shipped" column reports `done`
3598 /// named `Shipped`. An option the mapping reads as another category contradicts the
3599 /// closure, and so does no option at all: the name is then the option the mapping gives
3600 /// the closure's category, so an issue closed as completed at `Todo` reads `done` named
3601 /// by the board's `done` option, and a destination reading `Todo` as todo is never handed
3602 /// it. An `unknown` closure keeps the option, or `Closed` without one.
3603 ///
3604 /// An open item's option reads through its own kind's mapping, and an option that mapping
3605 /// does not name reads as `Unknown` under its own name.
3606 ///
3607 /// One function of those three rather than of a response, so a narrow status write can
3608 /// answer what a re-read would report by applying it to the state it has just written.
3609 fn status(
3610 &self,
3611 kind: ItemKind,
3612 option: Option<&str>,
3613 closed: bool,
3614 reason: Option<&str>,
3615 ) -> Status {
3616 if closed {
3617 let category = closed_category(reason);
3618 let agrees = option.filter(|option| {
3619 category == StatusCategory::Unknown
3620 || self
3621 .category_of(kind, option)
3622 .is_none_or(|read| read == category)
3623 });
3624 let name = agrees.or_else(|| self.target(kind, category).option());
3625 let fallback = match category {
3626 StatusCategory::Done => "Done",
3627 StatusCategory::Cancelled => "Cancelled",
3628 _ => "Closed",
3629 };
3630 return Status {
3631 category,
3632 name: name.or(option).unwrap_or(fallback).to_owned(),
3633 };
3634 }
3635 let name = option.unwrap_or("Open").to_owned();
3636 Status {
3637 category: self
3638 .category_of(kind, &name)
3639 .unwrap_or(StatusCategory::Unknown),
3640 name,
3641 }
3642 }
3643
3644 /// The option a closed item's `Status` has to hold for its card to agree with its
3645 /// closure, and the category that closure reads as — or `None` when the item is open,
3646 /// its closure reads `unknown`, or its option already agrees.
3647 ///
3648 /// What `task status reconcile` writes: only `done` and `cancelled` closures are brought
3649 /// in line, each to the one option the mapping gives its category — an item with no
3650 /// option, or at one the mapping does not name, included.
3651 fn contradicted(
3652 &self,
3653 kind: ItemKind,
3654 option: Option<&str>,
3655 closed: bool,
3656 reason: Option<&str>,
3657 ) -> Option<(ClosedAs, &str)> {
3658 if !closed {
3659 return None;
3660 }
3661 let closed_as = match closed_category(reason) {
3662 StatusCategory::Done => ClosedAs::Done,
3663 StatusCategory::Cancelled => ClosedAs::Cancelled,
3664 _ => return None,
3665 };
3666 let wanted = self.target(kind, closed_as.category()).option()?;
3667 if option.is_some_and(|option| option.eq_ignore_ascii_case(wanted)) {
3668 return None;
3669 }
3670 Some((closed_as, wanted))
3671 }
3672}
3673
3674/// The category a closed issue reads as, from the reason it was closed with.
3675///
3676/// `COMPLETED`, and no reason at all, is finished work; `NOT_PLANNED` and `DUPLICATE` are both
3677/// work nobody will do here — a duplicate's work is the other issue's — and any other reason
3678/// is one this source cannot read a category from.
3679fn closed_category(reason: Option<&str>) -> StatusCategory {
3680 match reason {
3681 None | Some("COMPLETED") => StatusCategory::Done,
3682 Some("NOT_PLANNED" | "DUPLICATE") => StatusCategory::Cancelled,
3683 Some(_) => StatusCategory::Unknown,
3684 }
3685}
3686
3687impl BoardStatuses {
3688 /// For each kind, the option names it maps a category to that `existing` lacks, ignoring
3689 /// case; a kind lacking none is left out.
3690 fn missing_by_kind(&self, existing: &[StatusOption]) -> Vec<KindMissing> {
3691 STATUS_KINDS
3692 .into_iter()
3693 .filter_map(|kind| {
3694 let missing: Vec<String> = self
3695 .targets(kind)
3696 .iter()
3697 .filter_map(StatusTarget::option)
3698 .filter(|wanted| {
3699 !existing
3700 .iter()
3701 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3702 })
3703 .map(str::to_owned)
3704 .collect();
3705 (!missing.is_empty()).then_some(KindMissing { kind, missing })
3706 })
3707 .collect()
3708 }
3709}
3710
3711impl StatusTarget {
3712 /// The board option this target selects, or `None` for an unmapped one.
3713 fn option(&self) -> Option<&str> {
3714 match self {
3715 Self::Column(name) | Self::Terminal(name, _) => Some(name.as_str()),
3716 Self::Disabled(_) => None,
3717 }
3718 }
3719}
3720
3721// 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.
3722/// One repository this source can create an issue in, as `owner/name`.
3723///
3724/// Every `createIssue` this source sends names one of these: the item's own single
3725/// `repositories` entry — on a source declared private, only when it reads private — else
3726/// its parent project issue's repository, else the configured
3727/// [`GitHubProjectsConfig::repository`]. [`GitHubProjectsSource::creation_target`] makes
3728/// that choice and says what it refuses before `createIssue`.
3729// llmlint: ignore-end[comments_earn_their_place, contracts_have_one_source_or_a_drift_gate]
3730#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
3731#[serde(try_from = "String", into = "String")]
3732struct RepositoryTarget {
3733 owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
3734 name: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
3735}
3736
3737impl RepositoryTarget {
3738 fn parse(value: &str) -> Result<Self, SourceError> {
3739 let (owner, name) = value.split_once('/').ok_or_else(|| SourceError::Config {
3740 message: format!(
3741 "repository must be spelled owner/name; {value:?} names no repository"
3742 ),
3743 })?;
3744 if !valid_github_owner(owner) || !valid_github_repository_name(name) {
3745 return Err(SourceError::Config {
3746 message: format!(
3747 "repository must be spelled owner/name with a GitHub login and one \
3748 repository name; {value:?} is not"
3749 ),
3750 });
3751 }
3752 Ok(Self {
3753 owner: owner.to_owned(),
3754 name: name.to_owned(),
3755 })
3756 }
3757
3758 /// The one host whose repositories this source creates issues in, spelled once: it is
3759 /// what [`Self::origin`] renders and what [`Self::from_origin`] accepts.
3760 const HOST: &str = "github.com";
3761
3762 fn origin(&self) -> String {
3763 format!("{}/{}/{}", Self::HOST, self.owner, self.name)
3764 }
3765
3766 /// The repository a normalized origin names, or why it is none this source can create
3767 /// an issue in: another host, or more or fewer than `owner/name` under this one.
3768 fn from_origin(origin: &Repository) -> Result<Self, String> {
3769 let not_here = || {
3770 format!(
3771 "{} is not a {}/owner/name repository",
3772 origin.as_str(),
3773 Self::HOST
3774 )
3775 };
3776 let (host, rest) = origin.as_str().split_once('/').ok_or_else(not_here)?;
3777 if host != Self::HOST {
3778 return Err(not_here());
3779 }
3780 Self::parse(rest).map_err(|_| not_here())
3781 }
3782
3783 fn slug(&self) -> String {
3784 format!("{}/{}", self.owner, self.name)
3785 }
3786}
3787
3788/// Read back from a kept context as the `owner/name` it was written as, refused unless
3789/// [`RepositoryTarget::parse`] takes it.
3790impl TryFrom<String> for RepositoryTarget {
3791 type Error = String;
3792
3793 fn try_from(slug: String) -> Result<Self, Self::Error> {
3794 Self::parse(&slug).map_err(|error| error.to_string())
3795 }
3796}
3797
3798impl From<RepositoryTarget> for String {
3799 fn from(target: RepositoryTarget) -> Self {
3800 target.slug()
3801 }
3802}
3803
3804/// A source which reads GitHub afresh for every operation.
3805pub struct GitHubProjectsSource {
3806 /// This source's configured name, used both to tell a far end naming this source
3807 /// from one naming a system it knows nothing about, and to name the instance a
3808 /// status refusal is about.
3809 name: SourceName,
3810 owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after full GitHub-owner validation.
3811 project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after GraphQL-Int validation.
3812 repository: Option<RepositoryTarget>,
3813 /// The logins whose repositories a create may file in; empty restricts nothing.
3814 creation_owners: Vec<String>, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after each entry passes GitHub's login grammar.
3815 endpoint: Url,
3816 token: SecretString,
3817 credential_name: String, // llmlint: ignore[invalid_states_unrepresentable] Private diagnostic value constructed only after environment-name validation.
3818 statuses: BoardStatuses,
3819 /// Where each priority lands on this board, or `None` when this instance holds none.
3820 priorities: Option<PriorityMapping>,
3821 /// The metadata values this instance projects onto board text fields, in configured order.
3822 metadata_fields: Vec<MetadataField>,
3823 /// Built on the first request rather than with the source, as [`Self::client`] says.
3824 client: std::sync::OnceLock<Client>,
3825 /// Built on the first attachment request, as [`Self::asset_client`] says.
3826 asset_client: std::sync::OnceLock<Client>,
3827 /// The markup the engine handed this source for its bodies' headings, applied to every
3828 /// visible body it composes and stripped from every one it reads back: see
3829 /// [`TaskSource::attach_body_markup`].
3830 body_markup: std::sync::OnceLock<onetaskgraph_plugin_api::SharedBodyMarkup>,
3831 /// Every item this source has created in this command, in the order it created them —
3832 /// dropped by [`TaskSource::end_command`].
3833 ///
3834 /// GitHub's `projectV2.items` is eventually consistent: an issue added to a board with
3835 /// `addProjectV2ItemById` is routinely absent from the very next read of that board, so
3836 /// a copy resolving a dependency on an item it had just created refused it as not
3837 /// found. A board read is completed from this — an item remembered here and absent from
3838 /// the read is added back, because the board really does hold it and only the read is
3839 /// behind.
3840 ///
3841 /// It is not a cache of a user's work: nothing is remembered that this process did not
3842 /// itself just write, it lives and dies with the process, and it is never consulted for
3843 /// an item this source did not create.
3844 created: Mutex<Vec<Resolved>>,
3845 /// Every item that already existed and that this source has written in this command, as
3846 /// it wrote it — dropped by [`TaskSource::end_command`].
3847 ///
3848 /// The other half of [`Self::created`], held on the same terms and for the reason a
3849 /// narrowed read needs it: an answer from GitHub's search or from the board's own field
3850 /// filter is an index behind a write this process made moments ago, so a query matching
3851 /// what this process just wrote onto an existing item would otherwise miss it. Nothing
3852 /// is remembered that this process did not itself just write.
3853 updated: Mutex<Vec<Resolved>>,
3854 /// Every issue this source has added a comment to or edited a comment of in this command
3855 /// — dropped by [`TaskSource::end_command`].
3856 ///
3857 /// A comment-activity read is narrowed by GitHub's issue search, whose `updated:` index
3858 /// lags the write that moved an issue's `updatedAt`, and neither [`Self::created`] nor
3859 /// [`Self::updated`] is moved by a comment, so an issue this process had just commented
3860 /// on was missing from such a read — or ruled out by the `updatedAt` its own record held
3861 /// from before — until the index caught up. Each id here is a candidate of every such
3862 /// search-narrowed read, and wherever it is a candidate its comments are read rather than
3863 /// it being ruled out by a stale `updatedAt`; that read is of the issue's own node, so it
3864 /// is current. It holds ids alone: nothing of a comment is remembered. A comment another
3865 /// process wrote is still found only once the index has it.
3866 commented: Mutex<Vec<NativeId>>,
3867 /// How fast this source writes, and how long it waits out a refusal.
3868 pacing: Pacing,
3869 /// When the last content-creating mutation finished, or the moment the furthest-out
3870 /// reserved slot releases the next one, whichever is later — so the one after it can be
3871 /// spaced from that, when `pacing.min_mutation_interval_ms` asks for a spacing. See
3872 /// [`GitHubProjectsSource::paced`] for why completion rather than release is what it is
3873 /// measured from.
3874 last_mutation: Mutex<Option<Duration>>,
3875 /// GitHub's secondary windows as this source has charged them; see the `pacing` module.
3876 windows: Mutex<pacing::Windows>,
3877 /// The tasks a project holds, as a copy's batched read of the project and every one of them
3878 /// found them — see [`TaskSource::read_ahead_for_copy`] — for the length of one command, so
3879 /// a listing of that project's tasks is answered without a read of its own.
3880 task_children: Mutex<BTreeMap<NativeId, Vec<Resolved>>>,
3881 /// The web address of each issue this source created in the current command, as its
3882 /// `createIssue` answered it — what [`TaskSource::written_location`] answers, without a
3883 /// request.
3884 written: Mutex<BTreeMap<NativeId, String>>,
3885 /// What a copy has held back of the items it created, between
3886 /// [`TaskSource::begin_deferred_writes`] and [`TaskSource::finish_writes`] — `None` while
3887 /// nothing is being held back, when every write lands whole as it is made.
3888 deferred: Mutex<Option<Deferred>>,
3889 /// Each existing issue's update held back while writes are, in the order it was made: sent
3890 /// together by [`Self::send_held_updates`] before any other request goes out, so nothing
3891 /// this source sends or reads ever sees the board without them.
3892 held_updates: Mutex<Vec<HeldUpdate>>,
3893 clock: SharedClock,
3894 /// Whether the configuration declares this source private, as
3895 /// [`TaskSource::declare_visibility`] told it: what decides where a new issue whose one
3896 /// named repository reads public is created — see [`Self::creation_rule`].
3897 declared_private: std::sync::atomic::AtomicBool,
3898 numeric_repositories: tokio::sync::Mutex<BTreeMap<RepositoryTarget, std::num::NonZeroU64>>,
3899 /// The board as this process last read it, for the length of one command — dropped by
3900 /// [`TaskSource::end_command`].
3901 ///
3902 /// A copy of a project used to re-read the whole board, paged, before writing each of
3903 /// its items, which is by far the largest part of a copy's request count and none of
3904 /// its work. Nothing else changes this board while a command runs — this source's own
3905 /// writes are the only writer — so one read answers them all.
3906 ///
3907 /// It is not a store of a user's work and it is not the cache the no-persistence
3908 /// invariant forbids: it lives and dies with the process exactly as `created` does,
3909 /// nothing is written down, and [`Self::board`] still completes it from `created`, so
3910 /// an item this command created and then depends on resolves whether or not GitHub's
3911 /// own eventually-consistent read has caught up. A write to an item already on the
3912 /// board updates the entry here too, so what this holds is the last read plus this
3913 /// process's own writes rather than a snapshot taken before them.
3914 board_cache: Mutex<Option<Board>>,
3915 /// Every issue this board's own search reported, for the length of one command — dropped
3916 /// by [`TaskSource::end_command`].
3917 ///
3918 /// The second half of a board read, and cached for the same reason and on the same
3919 /// terms as the first: it lives and dies with the process, nothing is written down, and
3920 /// a write this process makes updates the entry here exactly as it updates the one in
3921 /// [`Self::board_cache`]. One read answers every question a command asks, so a command
3922 /// that lists this board's projects and its tasks pays for one search rather than two.
3923 search_cache: Mutex<Option<Vec<Resolved>>>,
3924 /// What each narrowed question GitHub was asked answered, keyed by that question, for
3925 /// the length of one command — dropped by [`TaskSource::end_command`].
3926 ///
3927 /// The narrowed counterpart of [`Self::search_cache`], held on the same terms: it lives
3928 /// and dies with the process, nothing is written down, a write this process makes
3929 /// updates the entry here as it updates the other two, and every answer is completed
3930 /// with this process's own writes each time it is given. A command that asks the same
3931 /// narrowed question twice — a wait polling for its own items, a listing repeated after a
3932 /// write — pays for it once, which is what the whole-board read it replaced gave it.
3933 narrowed_cache: Mutex<BTreeMap<String, Vec<Resolved>>>,
3934 search_next: Mutex<BTreeMap<String, Option<String>>>,
3935 /// Records already resolved in this command, reused by writes and for comment identity.
3936 /// Explicit item reads still reach GitHub. Nothing is persisted, and
3937 /// [`TaskSource::end_command`] drops every record, so a write in the next command reads
3938 /// its item as a person has since left it.
3939 resolved_cache: Mutex<BTreeMap<NativeId, Resolved>>,
3940 /// What [`TaskSource::read_ahead_for_copy`] read, by id — `None` for an id that read
3941 /// answered as holding nothing — each taken by the next read of that item by id and by
3942 /// nothing else, dropped by any mutation naming it, and cleared by
3943 /// [`TaskSource::end_command`]. So a read answered from it is the copy's own
3944 /// read-before-write, made in the same request as the others' rather than on its own.
3945 read_ahead: Mutex<BTreeMap<NativeId, Option<Resolved>>>,
3946 /// The origins [`TaskSource::read_ahead_origins_for_copy`] was asked about and has not sent
3947 /// yet: held for the [`TaskSource::read_ahead_for_copy`] that follows it, which sends them —
3948 /// beside the members of the one project it names, when it names one — or for the next
3949 /// request this source makes, whichever comes first.
3950 pending_origins: Mutex<Vec<String>>,
3951 /// Each project's members as a lookup carrying them read them in this command, trimmed to
3952 /// their classification and repositories, taken by [`TaskSource::member_classifications`]
3953 /// of that project and by nothing else, and cleared by [`TaskSource::end_command`].
3954 member_classes: Mutex<BTreeMap<NativeId, Vec<MemberClass>>>,
3955 /// Every item a listing of one project's members carried whole in this command — the
3956 /// project issue and each of its sub-issues — kept for [`TaskSource::read_ahead_for_copy`]
3957 /// alone, which takes each one it is asked for into [`Self::read_ahead`] instead of reading
3958 /// it again. No other read is answered from it, so a read by id outside a copy's
3959 /// read-before-write reads GitHub as it always did. Dropped by any mutation naming an
3960 /// item, and cleared by [`TaskSource::end_command`].
3961 carried: Mutex<BTreeMap<NativeId, Resolved>>,
3962 /// Items an issue read by reference reached, held for the one status write each was read
3963 /// for and taken out by it alone — see [`Self::take_held`] — because that read selects only
3964 /// what such a write needs. Dropped by [`TaskSource::end_command`].
3965 held_cache: Mutex<BTreeMap<NativeId, Resolved>>,
3966 /// Each project's sub-issues as GitHub answered them, keyed by the selector they were
3967 /// asked for under, with the project that selector named — for the length of one command,
3968 /// dropped by [`TaskSource::end_command`].
3969 ///
3970 /// A caller pages through a project's tasks one engine page at a time, and every page
3971 /// is cut from the whole list of them, so without this each page walked every
3972 /// [`graphql::SUB_ISSUES`] page again and a project of `n` listing pages cost `n²`
3973 /// requests to read once. Held on the terms [`Self::narrowed_cache`] is: it lives and
3974 /// dies with the process, nothing is written down, a write this process makes updates or
3975 /// removes the entry here as it does there, and every answer is completed with this
3976 /// process's own writes each time it is given.
3977 children_cache: Mutex<ProjectChildren>,
3978 /// The board's own id and field definitions as this process last read them on their
3979 /// own, for the length of one command — dropped by [`TaskSource::end_command`].
3980 ///
3981 /// What a write needs of the board and its item does not say, read once per command
3982 /// rather than once per item written, on the terms [`Self::board_cache`] is held on: it
3983 /// lives and dies with the process and nothing is written down. It holds no item and so
3984 /// can answer no question about one — see [`Self::board_fields`].
3985 fields_cache: Mutex<Option<BoardFields>>,
3986 /// Each destination repository's node id, resolved once per repository
3987 /// rather than per issue created.
3988 ///
3989 /// A repository's node id does not change, and re-reading it for every issue of a copy
3990 /// spent one request per item on an answer this source already had. It is a map rather
3991 /// than one entry because a copy files each item in the repository its own
3992 /// `repositories` field names, so a plan across five repositories asks GitHub five
3993 /// times and not once per item.
3994 repository_cache: Mutex<BTreeMap<RepositoryTarget, String>>,
3995 /// What every request this source sends is recorded into.
3996 ///
3997 /// Ordinary code path, not a mode: [`Self::send_once`] records into it at the one place
3998 /// a request leaves this crate, so nothing has to be switched on for a session to be
3999 /// counted. It is shared rather than owned so a caller accounting for a whole session —
4000 /// its own schema verification, board lookups, residue sweep and cleanup beside this
4001 /// source's reads and writes — adds up one accounting instead of two. See
4002 /// [`accounting`] for what a record carries and what a session's spend is and is not.
4003 ledger: Arc<Accounting>,
4004 /// What [`expected`] holds for a write the store's cache expects: the journal while one is
4005 /// open, the issues primed from the cache, and the comments known on each.
4006 expected: expected::Held,
4007 /// What [`projection`] holds for a producer's projection: a kept placement awaiting its
4008 /// setup-context token, and where the last create was placed.
4009 projection: projection::Held,
4010}
4011
4012/// The board item a single field update's answer names, refused unless it is `item_id`.
4013fn field_update_item<'a>(data: &'a Value, item_id: &str) -> Result<&'a Value, SourceError> {
4014 let returned = data
4015 .pointer("/updateProjectV2ItemFieldValue/projectV2Item")
4016 .ok_or_else(|| SourceError::Malformed {
4017 message: "GitHub field update returned no project item".into(),
4018 })?;
4019 if required_str(returned, "id")? != item_id {
4020 return Err(SourceError::Malformed {
4021 message: "GitHub field update returned the wrong project item".into(),
4022 });
4023 }
4024 Ok(returned)
4025}
4026
4027/// GitHub's closed single-select color vocabulary.
4028#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
4029#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
4030pub enum StatusOptionColor {
4031 /// Gray.
4032 Gray,
4033 /// Blue.
4034 Blue,
4035 /// Green.
4036 Green,
4037 /// Yellow.
4038 Yellow,
4039 /// Purple.
4040 Purple,
4041 /// Red.
4042 Red,
4043 /// Orange.
4044 Orange,
4045 /// Pink.
4046 Pink,
4047}
4048
4049/// Whether a guarded board setup — of the fields, or of the Status options alone — plans or
4050/// applies its additions.
4051#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4052pub enum SetupMode {
4053 /// Read without mutation.
4054 Plan,
4055 /// Apply and verify.
4056 Apply,
4057}
4058
4059/// The name [`SetupMode`] had when Status was the one field set up, kept so a caller written
4060/// against it goes on compiling.
4061pub type StatusOptionsMode = SetupMode;
4062
4063/// The explicit result of the requested operation.
4064#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4065#[serde(rename_all = "kebab-case")]
4066pub enum StatusOptionsOutcome {
4067 /// A read-only plan.
4068 Planned,
4069 /// Apply found nothing missing.
4070 Unchanged,
4071 /// Additions were applied and verified.
4072 Applied,
4073}
4074
4075/// A GitHub single-select option's opaque GraphQL node identifier.
4076#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4077#[serde(transparent)]
4078pub struct StatusOptionId(#[schemars(length(min = 1))] String);
4079
4080impl TryFrom<String> for StatusOptionId {
4081 type Error = String;
4082
4083 fn try_from(id: String) -> Result<Self, Self::Error> {
4084 if id.trim().is_empty() {
4085 return Err("a GitHub Status option id cannot be blank".to_owned());
4086 }
4087 Ok(Self(id))
4088 }
4089}
4090
4091/// One existing or proposed option in a guarded Status-field update.
4092#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4093pub struct StatusOption {
4094 /// GitHub's stable id.
4095 pub id: StatusOptionId,
4096 /// The visible option name.
4097 pub name: ColumnName,
4098 /// GitHub's single-select color token.
4099 pub color: StatusOptionColor,
4100 /// The option description, including an empty one.
4101 pub description: String,
4102}
4103
4104/// One board item's Status assignment, retained as recovery data.
4105#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4106pub struct StatusAssignment {
4107 /// The project item id whose assignment this is.
4108 // llmlint: ignore[invalid_states_unrepresentable] This opaque GraphQL node ID is
4109 // carried verbatim as operator recovery data; introducing a semantic type would claim
4110 // validation rules GitHub does not publish and no operation here interprets.
4111 pub item_id: String,
4112 /// The selected option, absent when the item has no status.
4113 #[serde(skip_serializing_if = "Option::is_none")]
4114 pub option: Option<AssignedStatusOption>,
4115}
4116
4117/// The inseparable id and name of an assigned option.
4118#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4119pub struct AssignedStatusOption {
4120 /// GitHub's stable id.
4121 pub id: StatusOptionId,
4122 /// The visible name.
4123 pub name: ColumnName,
4124}
4125
4126/// The plan and verified outcome of reconciling configured Status options.
4127#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4128pub struct StatusOptionsReport {
4129 /// The configured source name.
4130 pub source: SourceName,
4131 /// Configured option names absent before the operation.
4132 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a
4133 // `ColumnName` and has therefore already passed its nonblank validation; retaining the
4134 // serialized string here preserves the report's intentionally simple public contract.
4135 pub missing: Vec<String>,
4136 /// What the requested operation did.
4137 pub outcome: StatusOptionsOutcome,
4138 /// The complete option list observed before any mutation.
4139 pub existing: Vec<StatusOption>,
4140}
4141
4142#[derive(Debug, Clone, PartialEq, Eq)]
4143struct StatusSnapshot {
4144 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4145 // passed back as the mutation's project identity; a newtype could enforce no stronger
4146 // invariant because GitHub publishes no grammar for it.
4147 board_id: String,
4148 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4149 // passed back as the mutation's field identity; a newtype could enforce no stronger
4150 // invariant because GitHub publishes no grammar for it.
4151 field_id: String,
4152 options: Vec<StatusOption>,
4153 assignments: Vec<StatusAssignment>,
4154}
4155
4156/// The name of the board field a status is held in.
4157const STATUS_FIELD: &str = "Status";
4158
4159/// Every item's value of each field `report` names, as it stood before the setup wrote
4160/// anything — what a person puts back when the setup is refused part way.
4161fn recovery(report: &FieldsReport, before: &BoardSnapshot) -> Result<String, SourceError> {
4162 let assignments: BTreeMap<&str, Vec<StatusAssignment>> = report
4163 .fields
4164 .iter()
4165 .map(|field| (field.field.name(), before.assignments(field.field)))
4166 .collect();
4167 serde_json::to_string_pretty(&assignments).map_err(|error| SourceError::Malformed {
4168 message: format!("cannot render the pre-write field recovery snapshot: {error}"),
4169 })
4170}
4171
4172/// One board field the guarded setup reads and writes — every one it reads, and the only
4173/// ones it writes.
4174#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, schemars::JsonSchema)]
4175pub enum BoardField {
4176 /// The single-select `Status` field every instance's `status_mapping` resolves into.
4177 Status,
4178 /// The single-select `Priority` field an instance's `priority_mapping` resolves into.
4179 Priority,
4180}
4181
4182impl BoardField {
4183 /// The field's name on the board.
4184 #[must_use]
4185 pub const fn name(self) -> &'static str {
4186 match self {
4187 Self::Status => STATUS_FIELD,
4188 Self::Priority => PRIORITY_FIELD,
4189 }
4190 }
4191
4192 /// The field a board calls `name`, or `None` for one this setup does not own.
4193 fn named(name: &str) -> Option<Self> {
4194 [Self::Status, Self::Priority]
4195 .into_iter()
4196 .find(|field| field.name() == name)
4197 }
4198}
4199
4200/// What the guarded setup did to one field.
4201#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4202#[serde(rename_all = "kebab-case")]
4203pub enum FieldOutcome {
4204 /// A read-only plan.
4205 Planned,
4206 /// Apply found the field there with every configured option.
4207 Unchanged,
4208 /// Missing options were added to the field that was there, and verified.
4209 Applied,
4210 /// The field was not there; it was created holding the configured options, and verified.
4211 Created,
4212}
4213
4214/// One field's plan, or its verified outcome.
4215#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4216pub struct FieldReport {
4217 /// Which field.
4218 pub field: BoardField,
4219 /// Whether the board had the field before the operation.
4220 // llmlint: ignore[invalid_states_unrepresentable] `exists` beside `outcome` is the report's
4221 // wire shape as its consumer's contract fixes it — `{"field", "exists", "missing",
4222 // "outcome", "existing"}` — so folding one into the other would change a published JSON
4223 // shape. The contradictory pairings cannot be built: `GitHubProjectsSource::fields` is the
4224 // one constructor, and it derives `outcome` from `exists` in one match.
4225 pub exists: bool,
4226 /// Configured option names the field lacked before the operation — every one of them,
4227 /// in the order a new field lists them, when the field was not there at all.
4228 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
4229 // mapping name and has therefore already passed its nonblank validation; the serialized
4230 // string is the report's intentionally simple public contract, as `StatusOptionsReport`'s is.
4231 pub missing: Vec<String>,
4232 /// For the `Status` field, which item kind each missing name is configured for: one
4233 /// entry per kind `status_mapping` names a missing option for, task before project, each
4234 /// listing that kind's missing names in category order. A name both kinds use is in
4235 /// both. Empty — and left out of the JSON — when nothing is missing, and always for
4236 /// `Priority`, which only a task holds.
4237 #[serde(default, skip_serializing_if = "Vec::is_empty")]
4238 // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
4239 // both SDKs model an absent `kinds` as an empty list rather than as `null`.
4240 #[schemars(!skip_serializing_if)]
4241 pub kinds: Vec<KindMissing>,
4242 /// What the requested operation did.
4243 pub outcome: FieldOutcome,
4244 /// The field's complete option list observed before any mutation; empty when the field
4245 /// was not there.
4246 pub existing: Vec<StatusOption>,
4247}
4248
4249/// The `Status` option names one item kind's `status_mapping` names that the field lacked.
4250#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4251pub struct KindMissing {
4252 /// The kind these names are configured for.
4253 pub kind: ItemKind,
4254 /// The names that kind maps a category to and the field lacked, in category order.
4255 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
4256 // mapping name, as `FieldReport::missing`'s do, and the serialized string is the report's
4257 // intentionally simple public contract.
4258 pub missing: Vec<String>,
4259}
4260
4261/// The plan and verified outcome of setting up every field a source's configuration names.
4262#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4263pub struct FieldsReport {
4264 /// The configured source name.
4265 pub source: SourceName,
4266 /// `Status`, always, and `Priority` when the source sets `priority_mapping`.
4267 // llmlint: ignore[invalid_states_unrepresentable] A list is the report's wire shape as its
4268 // consumer's contract fixes it — `{"source", "fields": [...]}` — so a struct with one member
4269 // per field would change a published JSON shape. The states the list could hold and the
4270 // contract forbids cannot be built: `GitHubProjectsSource::fields` is the one constructor,
4271 // and it pushes `Status` first and exactly once, then `Priority` exactly when configured.
4272 pub fields: Vec<FieldReport>,
4273 /// Each board text field the source's `metadata_fields` projects a value onto, in
4274 /// configured order. Empty — and left out of the JSON — when it projects none.
4275 #[serde(default, skip_serializing_if = "Vec::is_empty")]
4276 // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
4277 // both SDKs model an absent list as an empty one rather than as `null`.
4278 #[schemars(!skip_serializing_if)]
4279 pub metadata_fields: Vec<MetadataFieldReport>,
4280}
4281
4282/// The type of a board field that is not a text field, as GitHub names it — its `dataType`, or
4283/// its GraphQL type when it has none — which is what a projected field's conflict reports.
4284///
4285/// Never empty, never beginning with whitespace, and never `TEXT`: a text field of that name is
4286/// no conflict.
4287// The schema states that rule as `NON_TEXT_FIELD_TYPE_PATTERN`, so a generated SDK model
4288// refuses exactly what `try_from` below does.
4289#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4290#[serde(transparent)]
4291pub struct NonTextFieldType(#[schemars(regex(pattern = NON_TEXT_FIELD_TYPE_PATTERN))] String);
4292
4293/// What a [`NonTextFieldType`] may be, as the emitted schema states it: a first character that
4294/// is not whitespace, and anything but exactly `TEXT` — spelled without a lookahead, which
4295/// neither SDK's validator can be relied on to support.
4296const NON_TEXT_FIELD_TYPE_PATTERN: &str =
4297 r"^(?:[^T\s]|T(?:[^E]|$)|TE(?:[^X]|$)|TEX(?:[^T]|$)|TEXT[\s\S])";
4298
4299impl TryFrom<String> for NonTextFieldType {
4300 type Error = String;
4301
4302 fn try_from(kind: String) -> Result<Self, Self::Error> {
4303 if kind.is_empty() || kind.starts_with(char::is_whitespace) {
4304 return Err("a board field's type cannot be blank or begin with whitespace".to_owned());
4305 }
4306 if kind == "TEXT" {
4307 return Err("a TEXT field is no conflict for a projected text field".to_owned());
4308 }
4309 Ok(Self(kind))
4310 }
4311}
4312
4313impl std::fmt::Display for NonTextFieldType {
4314 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4315 formatter.write_str(&self.0)
4316 }
4317}
4318
4319/// One projected metadata text field's plan, or its verified outcome.
4320///
4321/// The setup creates a missing one as a text field and never alters a field that is there:
4322/// a same-named field of another type is reported in `conflict` and left as it is.
4323#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4324pub struct MetadataFieldReport {
4325 /// The board field's name.
4326 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.
4327 /// The metadata key the value is read from.
4328 pub key: String, // llmlint: ignore[invalid_states_unrepresentable] As `field`: a validated configuration value reported back as written.
4329 /// The object keys walked inside that key's value; empty for the key's own value.
4330 pub path: Vec<String>,
4331 /// Whether the board had a text field of that name before the operation.
4332 // 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.
4333 pub exists: bool,
4334 /// The type of a field of that name that is not a text field, which the setup leaves as
4335 /// it is; absent — and left out of the JSON — when there is none.
4336 #[serde(default, skip_serializing_if = "Option::is_none")]
4337 pub conflict: Option<NonTextFieldType>,
4338 /// What the requested operation did: `planned`; `created` for a field that was not
4339 /// there; `unchanged` for one that was, and for a conflict, which is never changed.
4340 pub outcome: FieldOutcome,
4341}
4342
4343/// Which options one field is configured with, in the order a new field would list them.
4344struct FieldPlan {
4345 field: BoardField,
4346 wanted: Vec<String>,
4347}
4348
4349/// One single-select field as the guarded setup snapshots it.
4350#[derive(Debug, Clone, PartialEq, Eq)]
4351struct SnapshotField {
4352 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4353 // passed back as the mutation's field identity; a newtype could enforce no stronger
4354 // invariant because GitHub publishes no grammar for it.
4355 field_id: String,
4356 options: Vec<StatusOption>,
4357}
4358
4359/// Every single-select field of a board and every item's value of each.
4360#[derive(Debug, Clone, PartialEq, Eq)]
4361struct BoardSnapshot {
4362 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4363 // passed back as the mutation's project identity; a newtype could enforce no stronger
4364 // invariant because GitHub publishes no grammar for it.
4365 board_id: String,
4366 fields: BTreeMap<BoardField, SnapshotField>,
4367 /// Each board item's id, and its value of each field this setup owns that it holds one of.
4368 items: Vec<(String, BTreeMap<BoardField, AssignedStatusOption>)>,
4369}
4370
4371impl BoardSnapshot {
4372 /// Every item's value of `field`, in board order — the recovery data a drift refusal
4373 /// carries.
4374 fn assignments(&self, field: BoardField) -> Vec<StatusAssignment> {
4375 self.items
4376 .iter()
4377 .map(|(item_id, values)| StatusAssignment {
4378 item_id: item_id.clone(),
4379 option: values.get(&field).cloned(),
4380 })
4381 .collect()
4382 }
4383}
4384
4385impl GitHubProjectsSource {
4386 /// `content` as this source sends it, with the markup the engine handed it applied.
4387 fn marked(&self, content: &str) -> String {
4388 match self.body_markup.get() {
4389 Some(markup) if !content.is_empty() => markup.written(content),
4390 _ => content.to_owned(),
4391 }
4392 }
4393
4394 /// The visible body and the metadata slot of `body` as [`metadata_body`] splits them,
4395 /// the visible body without the markup [`Self::marked`] applies.
4396 fn visible(
4397 &self,
4398 body: Option<String>,
4399 ) -> Result<(Option<String>, BTreeMap<String, Value>), SourceError> {
4400 let (visible, slot) = metadata_body(body)?;
4401 Ok((
4402 match (visible, self.body_markup.get()) {
4403 (Some(visible), Some(markup)) => {
4404 Some(markup.read(&visible)).filter(|read| !read.is_empty())
4405 }
4406 (visible, _) => visible,
4407 },
4408 slot,
4409 ))
4410 }
4411
4412 /// Report missing configured Status options and, when `apply` is true, add them with
4413 /// a whole-list mutation that preserves every existing id and verifies the result.
4414 ///
4415 /// # Errors
4416 ///
4417 /// Refuses a board without a single-select `Status` field. A post-write difference in
4418 /// any pre-existing option id or item assignment is refused with the complete pre-write
4419 /// assignment snapshot in the diagnostic for recovery.
4420 // llmlint: ignore[changed_behavior_has_e2e] The CLI journeys cover plan, no-op apply,
4421 // successful mutation, both drift refusals, source selection, missing Status, casing,
4422 // and paging. Transport errors remain the shared `graphql` boundary's behavior rather
4423 // than a new status-options behavior, and the pinned-schema test prevents valid GitHub
4424 // responses from entering the defensive malformed-response branches below.
4425 pub async fn status_options(
4426 &self,
4427 mode: StatusOptionsMode,
4428 ) -> Result<StatusOptionsReport, SourceError> {
4429 let before = self.status_snapshot().await?;
4430 // A terminal category's option is as configured as an open one's: a terminal
4431 // write validates it before closing and refuses when the board lacks it. Both
4432 // kinds' names are options of the one field, so both are asked for.
4433 let missing = self
4434 .statuses
4435 .wanted()
4436 .into_iter()
4437 .filter(|wanted| {
4438 !before
4439 .options
4440 .iter()
4441 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4442 })
4443 .collect::<Vec<_>>();
4444 let report = StatusOptionsReport {
4445 source: self.name.clone(),
4446 missing: missing.clone(),
4447 outcome: match (mode, missing.is_empty()) {
4448 (StatusOptionsMode::Plan, _) => StatusOptionsOutcome::Planned,
4449 (StatusOptionsMode::Apply, true) => StatusOptionsOutcome::Unchanged,
4450 (StatusOptionsMode::Apply, false) => StatusOptionsOutcome::Applied,
4451 },
4452 existing: before.options.clone(),
4453 };
4454 if mode == StatusOptionsMode::Plan || missing.is_empty() {
4455 return Ok(report);
4456 }
4457 let mut options = before
4458 .options
4459 .iter()
4460 .map(|option| {
4461 json!({
4462 "id": option.id, "name": option.name, "color": option.color,
4463 "description": option.description,
4464 })
4465 })
4466 .collect::<Vec<_>>();
4467 options.extend(missing.iter().map(|name| {
4468 json!({
4469 "name": name, "color": "GRAY", "description": ""
4470 })
4471 }));
4472 self.graphql(
4473 graphql::STATUS_OPTIONS_UPDATE,
4474 json!({"input": {
4475 "fieldId": before.field_id,
4476 "singleSelectOptions": options,
4477 }}),
4478 )
4479 .await?;
4480 let after = self.status_snapshot().await?;
4481 let options_preserved = before
4482 .options
4483 .iter()
4484 .all(|old| after.options.iter().any(|new| new == old));
4485 let additions_present = missing.iter().all(|wanted| {
4486 after
4487 .options
4488 .iter()
4489 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4490 });
4491 if !options_preserved || !additions_present || after.assignments != before.assignments {
4492 let recovery = serde_json::to_string_pretty(&before.assignments).map_err(|error| {
4493 SourceError::Malformed {
4494 message: format!("cannot render pre-write Status recovery snapshot: {error}"),
4495 }
4496 })?;
4497 return Err(SourceError::Refused {
4498 message: format!(
4499 "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}"
4500 ),
4501 });
4502 }
4503 Ok(report)
4504 }
4505
4506 /// A fresh snapshot of the Status field and every board item's assignment of it.
4507 ///
4508 /// # Errors
4509 ///
4510 /// Refuses a board without a single-select `Status` field, and one the token cannot see.
4511 async fn status_snapshot(&self) -> Result<StatusSnapshot, SourceError> {
4512 // Status alone, as this operation has always read it: a `Priority` field is another
4513 // operation's, so nothing about it can refuse this one.
4514 let mut board = self.board_snapshot(&[BoardField::Status]).await?;
4515 let field = board
4516 .fields
4517 .remove(&BoardField::Status)
4518 .ok_or_else(|| self.no_status_field())?;
4519 Ok(StatusSnapshot {
4520 assignments: board.assignments(BoardField::Status),
4521 board_id: board.board_id,
4522 field_id: field.field_id,
4523 options: field.options,
4524 })
4525 }
4526
4527 /// The refusal a board with no `Status` field is answered with by the guarded setup.
4528 fn no_status_field(&self) -> SourceError {
4529 SourceError::Refused {
4530 message: format!("source {} board has no Status field", self.name),
4531 }
4532 }
4533
4534 // llmlint: ignore-block[changed_behavior_has_e2e] Valid snapshot shapes are exercised through
4535 // the real CLI loopback journey, including pagination. The individual malformed guards
4536 // are defensive validation of a schema-pinned third-party response, not separate user
4537 // journeys; drift and missing-field failures cover the operation's recovery behavior.
4538 /// A fresh snapshot of each of the `owned` fields on the board, with its options, and of
4539 /// every board item's value of each, walked to the end of the board's items. A field not
4540 /// in `owned` is read past whatever it holds.
4541 async fn board_snapshot(&self, owned: &[BoardField]) -> Result<BoardSnapshot, SourceError> {
4542 let mut after: Option<String> = None;
4543 let mut snapshot: Option<BoardSnapshot> = None;
4544 loop {
4545 let data = self
4546 .graphql(
4547 graphql::STATUS_OPTIONS_SNAPSHOT,
4548 json!({
4549 "owner": self.owner, "number": self.project_number,
4550 "first": MAX_PAGE_SIZE, "after": after, "nestedFirst": MAX_PAGE_SIZE,
4551 }),
4552 )
4553 .await?;
4554 let board = data
4555 .pointer("/owner/projectV2")
4556 .filter(|board| board.is_object())
4557 .ok_or_else(|| SourceError::Refused {
4558 message: format!(
4559 "source {} has no accessible GitHub Projects board",
4560 self.name
4561 ),
4562 })?;
4563 if board
4564 .pointer("/fields/pageInfo/hasNextPage")
4565 .and_then(Value::as_bool)
4566 != Some(false)
4567 {
4568 return Err(SourceError::Malformed {
4569 message:
4570 "GitHub project fields is incomplete or has malformed pageInfo.hasNextPage"
4571 .into(),
4572 });
4573 }
4574 let mut fields = BTreeMap::new();
4575 // Only the fields this setup owns, by name: a node the single-select fragment did not
4576 // match carries no name, and a person's own single-select field — a `Size`, a
4577 // `Team` — is none of this setup's business, so nothing about it can refuse one. A
4578 // `Status` or `Priority` field without its options is malformed, not absent.
4579 // 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.
4580 for (owned, field) in board
4581 .pointer("/fields/nodes")
4582 .and_then(Value::as_array)
4583 .ok_or_else(|| SourceError::Malformed {
4584 message: "GitHub project fields.nodes is not an array".into(),
4585 })?
4586 .iter()
4587 .filter_map(|field| {
4588 let named = BoardField::named(field.get("name")?.as_str()?)?;
4589 owned.contains(&named).then_some((named, field))
4590 })
4591 {
4592 let options = field
4593 .get("options")
4594 .and_then(Value::as_array)
4595 .ok_or_else(|| SourceError::Malformed {
4596 message: "GitHub single-select field options is not an array".into(),
4597 })?
4598 .iter()
4599 .map(|option| {
4600 Ok(StatusOption {
4601 id: StatusOptionId::try_from(required_str(option, "id")?.to_owned())
4602 .map_err(|message| SourceError::Malformed { message })?,
4603 name: ColumnName::try_from(required_str(option, "name")?.to_owned())
4604 .map_err(|message| SourceError::Malformed {
4605 message: format!(
4606 "GitHub single-select option name is invalid: {message}"
4607 ),
4608 })?,
4609 color: serde_json::from_value(
4610 option.get("color").cloned().unwrap_or(Value::Null),
4611 )
4612 .map_err(|error| {
4613 SourceError::Malformed {
4614 message: format!(
4615 "GitHub single-select option color is invalid: {error}"
4616 ),
4617 }
4618 })?,
4619 description: optional_str(option, "description")?
4620 .unwrap_or_default()
4621 .to_owned(),
4622 })
4623 })
4624 .collect::<Result<Vec<_>, SourceError>>()?;
4625 let snapshot = SnapshotField {
4626 field_id: required_nonblank_str(field, "id")?.to_owned(),
4627 options,
4628 };
4629 // A board's field names are unique, so a second one is an answer that cannot
4630 // say which field the setup would act on — refused rather than one chosen.
4631 if fields.insert(owned, snapshot).is_some() {
4632 return Err(SourceError::Malformed {
4633 message: format!(
4634 "GitHub answered two {} fields for this board",
4635 owned.name()
4636 ),
4637 });
4638 }
4639 }
4640 let board_id = required_nonblank_str(board, "id")?.to_owned();
4641 let current = snapshot.get_or_insert_with(|| BoardSnapshot {
4642 board_id,
4643 fields,
4644 items: Vec::new(),
4645 });
4646 let items = board
4647 .pointer("/items/nodes")
4648 .and_then(Value::as_array)
4649 .ok_or_else(|| SourceError::Malformed {
4650 message: "GitHub project items.nodes is not an array".into(),
4651 })?;
4652 for item in items {
4653 let field_values =
4654 item.get("fieldValues")
4655 .ok_or_else(|| SourceError::Malformed {
4656 message: "GitHub project item is missing fieldValues".into(),
4657 })?;
4658 if field_values
4659 .pointer("/pageInfo/hasNextPage")
4660 .and_then(Value::as_bool)
4661 != Some(false)
4662 {
4663 return Err(SourceError::Malformed {
4664 message: "GitHub project item fieldValues is incomplete or has malformed pageInfo.hasNextPage".into(),
4665 });
4666 }
4667 let values = item
4668 .pointer("/fieldValues/nodes")
4669 .and_then(Value::as_array)
4670 .ok_or_else(|| SourceError::Malformed {
4671 message: "GitHub project item fieldValues.nodes is not an array".into(),
4672 })?;
4673 let item_id = required_nonblank_str(item, "id")?;
4674 let mut assigned = BTreeMap::new();
4675 for value in values {
4676 let Some(field) = value
4677 .pointer("/field/name")
4678 .and_then(Value::as_str)
4679 .and_then(BoardField::named)
4680 .filter(|field| owned.contains(field))
4681 else {
4682 continue;
4683 };
4684 let held = assigned.insert(
4685 field,
4686 AssignedStatusOption {
4687 id: StatusOptionId::try_from(
4688 required_str(value, "optionId")?.to_owned(),
4689 )
4690 .map_err(|message| SourceError::Malformed { message })?,
4691 name: ColumnName::try_from(required_str(value, "name")?.to_owned())
4692 .map_err(|message| SourceError::Malformed {
4693 message: format!(
4694 "GitHub assigned {} name is invalid: {message}",
4695 field.name()
4696 ),
4697 })?,
4698 },
4699 );
4700 // An item holds one value of a field, so a second one leaves no way to
4701 // tell which it holds — and a verification or recovery built on either
4702 // could restore the wrong one.
4703 if held.is_some() {
4704 return Err(SourceError::Malformed {
4705 message: format!(
4706 "GitHub answered two {} values for board item {item_id}",
4707 field.name()
4708 ),
4709 });
4710 }
4711 }
4712 current.items.push((item_id.to_owned(), assigned));
4713 }
4714 let page = board.get("items").ok_or_else(|| SourceError::Malformed {
4715 message: "GitHub project is missing items".into(),
4716 })?;
4717 let has_next = page
4718 .pointer("/pageInfo/hasNextPage")
4719 .and_then(Value::as_bool)
4720 .ok_or_else(|| SourceError::Malformed {
4721 message: "GitHub project items.pageInfo.hasNextPage is not a boolean".into(),
4722 })?;
4723 if !has_next {
4724 break;
4725 }
4726 let next =
4727 required_nonblank_str(page.get("pageInfo").unwrap_or(&Value::Null), "endCursor")?;
4728 validate_cursor_progress(after.as_deref(), next)?;
4729 after = Some(next.to_owned());
4730 }
4731 snapshot.ok_or_else(|| SourceError::Malformed {
4732 message: "GitHub returned no board field snapshot".into(),
4733 })
4734 }
4735 // llmlint: ignore-end[changed_behavior_has_e2e]
4736
4737 /// Report every board field this source's configuration names and, with
4738 /// [`SetupMode::Apply`], set each up: add the options a field lacks, and create
4739 /// the `Priority` field when the board has none.
4740 ///
4741 /// The fields are `Status`, always, with the options `status_mapping` resolves to; and
4742 /// `Priority`, when `priority_mapping` is set, with its four mapped options — created in
4743 /// the order urgent, high, medium, low. An option a field already has keeps its id, name,
4744 /// color and description: the whole option list goes back with every existing id, because
4745 /// a re-minted id clears every item's value.
4746 ///
4747 /// # Errors
4748 ///
4749 /// Refuses a board without a single-select `Status` field. After an apply the board is
4750 /// read again, and a pre-existing option or any item's value of either field that moved is
4751 /// refused with the complete pre-write assignments in the diagnostic, for recovery.
4752 // llmlint: ignore[changed_behavior_has_e2e] The `sources fields` journeys drive plan,
4753 // unchanged apply, a created field, an added option to each field, drift refusal, a board
4754 // with no Status field and a non-github-projects source through the compiled CLI against
4755 // the loopback board. Transport errors are the shared `graphql` boundary's behavior.
4756 pub async fn fields(&self, mode: SetupMode) -> Result<FieldsReport, SourceError> {
4757 let owned: Vec<BoardField> = if self.priorities.is_some() {
4758 vec![BoardField::Status, BoardField::Priority]
4759 } else {
4760 vec![BoardField::Status]
4761 };
4762 let before = self.board_snapshot(&owned).await?;
4763 let mut plans = vec![FieldPlan {
4764 field: BoardField::Status,
4765 wanted: self.statuses.wanted(),
4766 }];
4767 if !before.fields.contains_key(&BoardField::Status) {
4768 return Err(self.no_status_field());
4769 }
4770 if let Some(mapping) = &self.priorities {
4771 plans.push(FieldPlan {
4772 field: BoardField::Priority,
4773 wanted: mapping.names().map(str::to_owned).collect(),
4774 });
4775 }
4776 // The snapshot reads single-select fields alone, so a field it did not find may still
4777 // be on the board under the name, of another type: creating one beside it would fail
4778 // part way, or leave two fields of one name. Asked of the board's own field list, and
4779 // only when a field is missing.
4780 if plans
4781 .iter()
4782 .any(|plan| !before.fields.contains_key(&plan.field))
4783 {
4784 let board = self.board_fields().await?;
4785 for plan in plans
4786 .iter()
4787 .filter(|plan| !before.fields.contains_key(&plan.field))
4788 {
4789 if let Some(field) = Board::field(&board.fields, plan.field.name())? {
4790 return Err(SourceError::Refused {
4791 message: format!(
4792 "source {}'s board has a {} field that is not a single-select field \
4793 (it is a {}), so it cannot hold this source's options; next: rename \
4794 or remove that field, then run this again",
4795 self.name,
4796 plan.field.name(),
4797 optional_str(field, "__typename")?.unwrap_or("field of another type")
4798 ),
4799 });
4800 }
4801 }
4802 }
4803 let mut reports = Vec::new();
4804 for plan in &plans {
4805 let held = before.fields.get(&plan.field);
4806 let existing = held.map(|field| field.options.clone()).unwrap_or_default();
4807 let mut missing: Vec<String> = Vec::new();
4808 for wanted in &plan.wanted {
4809 let present = existing
4810 .iter()
4811 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4812 || missing
4813 .iter()
4814 .any(|named| named.eq_ignore_ascii_case(wanted));
4815 if !present {
4816 missing.push(wanted.clone());
4817 }
4818 }
4819 let kinds = match plan.field {
4820 BoardField::Status => self.statuses.missing_by_kind(&existing),
4821 BoardField::Priority => Vec::new(),
4822 };
4823 reports.push(FieldReport {
4824 field: plan.field,
4825 exists: held.is_some(),
4826 kinds,
4827 outcome: match (mode, held.is_some(), missing.is_empty()) {
4828 (SetupMode::Plan, _, _) => FieldOutcome::Planned,
4829 (SetupMode::Apply, true, true) => FieldOutcome::Unchanged,
4830 (SetupMode::Apply, true, false) => FieldOutcome::Applied,
4831 (SetupMode::Apply, false, _) => FieldOutcome::Created,
4832 },
4833 missing,
4834 existing,
4835 });
4836 }
4837 // A projected field is a text field, which the single-select snapshot cannot see, so
4838 // it is asked of the board's own field list — and only when one is configured.
4839 let mut metadata_fields = Vec::new();
4840 if !self.metadata_fields.is_empty() {
4841 let board = self.board_fields().await?;
4842 for projection in &self.metadata_fields {
4843 let conflict = match Board::field(&board.fields, &projection.field)? {
4844 None => None,
4845 Some(field) => {
4846 let typename = optional_str(field, "__typename")?;
4847 let data_type = optional_str(field, "dataType")?;
4848 if typename == Some("ProjectV2Field") && data_type == Some("TEXT") {
4849 None
4850 } else {
4851 let named = data_type
4852 .filter(|kind| *kind != "TEXT")
4853 .or(typename)
4854 .unwrap_or("field of another type");
4855 Some(NonTextFieldType::try_from(named.to_owned()).map_err(
4856 |message| SourceError::Malformed {
4857 message: format!(
4858 "GitHub answered the board's {} field with a type this \
4859 setup cannot read: {message}",
4860 &*projection.field
4861 ),
4862 },
4863 )?)
4864 }
4865 }
4866 };
4867 let exists =
4868 conflict.is_none() && Board::field(&board.fields, &projection.field)?.is_some();
4869 metadata_fields.push(MetadataFieldReport {
4870 field: projection.field.to_string(),
4871 key: projection.key.to_string(),
4872 path: projection.path.clone(),
4873 exists,
4874 outcome: match (mode, exists || conflict.is_some()) {
4875 (SetupMode::Plan, _) => FieldOutcome::Planned,
4876 (SetupMode::Apply, true) => FieldOutcome::Unchanged,
4877 (SetupMode::Apply, false) => FieldOutcome::Created,
4878 },
4879 conflict,
4880 });
4881 }
4882 }
4883 let report = FieldsReport {
4884 source: self.name.clone(),
4885 fields: reports,
4886 metadata_fields,
4887 };
4888 let writes: Vec<&FieldReport> = report
4889 .fields
4890 .iter()
4891 .filter(|field| !field.missing.is_empty() || !field.exists)
4892 .collect();
4893 let creates: Vec<&MetadataFieldReport> = report
4894 .metadata_fields
4895 .iter()
4896 .filter(|field| field.outcome == FieldOutcome::Created)
4897 .collect();
4898 if mode == SetupMode::Plan || (writes.is_empty() && creates.is_empty()) {
4899 return Ok(report);
4900 }
4901 let mut landed: Vec<&str> = Vec::new();
4902 for field in &writes {
4903 let added = field
4904 .missing
4905 .iter()
4906 .map(|name| json!({"name": name, "color": "GRAY", "description": ""}));
4907 let sent = match before.fields.get(&field.field) {
4908 Some(held) => {
4909 let mut options = held
4910 .options
4911 .iter()
4912 .map(|option| {
4913 json!({
4914 "id": option.id, "name": option.name, "color": option.color,
4915 "description": option.description,
4916 })
4917 })
4918 .collect::<Vec<_>>();
4919 options.extend(added);
4920 self.graphql(
4921 graphql::STATUS_OPTIONS_UPDATE,
4922 json!({"input": {
4923 "fieldId": held.field_id,
4924 "singleSelectOptions": options,
4925 }}),
4926 )
4927 .await
4928 }
4929 None => {
4930 self.graphql(
4931 graphql::CREATE_FIELD,
4932 json!({"input": {
4933 "projectId": before.board_id, "dataType": "SINGLE_SELECT",
4934 "name": field.field.name(),
4935 "singleSelectOptions": added.collect::<Vec<_>>(),
4936 }}),
4937 )
4938 .await
4939 }
4940 };
4941 // A mutation that failed does not establish that GitHub left its field as it was,
4942 // so every failure from here on carries the recovery data a drift refusal does.
4943 match sent {
4944 Ok(_) => landed.push(field.field.name()),
4945 Err(error) => {
4946 let changed = if landed.is_empty() {
4947 String::new()
4948 } else {
4949 format!("changed the {} field and then ", landed.join(" and "))
4950 };
4951 return Err(SourceError::Refused {
4952 message: format!(
4953 "the guarded field setup {changed}failed on the {} field, which it may \
4954 have changed part way: {error}; the pre-write item assignments \
4955 are:\n{}",
4956 field.field.name(),
4957 recovery(&report, &before)?
4958 ),
4959 });
4960 }
4961 }
4962 }
4963 for field in &creates {
4964 let sent = self
4965 .graphql(
4966 graphql::CREATE_FIELD,
4967 json!({"input": {
4968 "projectId": before.board_id, "dataType": "TEXT", "name": field.field,
4969 }}),
4970 )
4971 .await;
4972 if let Err(error) = sent {
4973 let changed = if landed.is_empty() {
4974 String::new()
4975 } else {
4976 format!("changed the {} field and then ", landed.join(" and "))
4977 };
4978 return Err(SourceError::Refused {
4979 message: format!(
4980 "the guarded field setup {changed}failed creating the {} text field: \
4981 {error}; run it again to create what is still missing",
4982 field.field
4983 ),
4984 });
4985 }
4986 landed.push(&field.field);
4987 }
4988 // The board has been written, so a verification read that fails leaves it unverified
4989 // rather than unchanged, and says what to put back.
4990 let after = match self.board_snapshot(&owned).await {
4991 Ok(after) => after,
4992 Err(error) => {
4993 return Err(SourceError::Refused {
4994 message: format!(
4995 "the guarded field setup changed the {} field and then could not read the \
4996 board back to verify it: {error}; the pre-write item assignments are:\n{}",
4997 landed.join(" and "),
4998 recovery(&report, &before)?
4999 ),
5000 });
5001 }
5002 };
5003 let mut moved = Vec::new();
5004 for field in &report.fields {
5005 let name = field.field.name();
5006 let now = after
5007 .fields
5008 .get(&field.field)
5009 .map(|held| held.options.as_slice())
5010 .unwrap_or_default();
5011 if !field.existing.iter().all(|old| now.contains(old)) {
5012 moved.push(format!(
5013 "a pre-existing {name} option id, name, color or description"
5014 ));
5015 }
5016 if !field.missing.iter().all(|wanted| {
5017 now.iter()
5018 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
5019 }) {
5020 moved.push(format!("an added {name} option"));
5021 }
5022 if after.assignments(field.field) != before.assignments(field.field) {
5023 moved.push(format!("an item's {name} value"));
5024 }
5025 }
5026 if !creates.is_empty() {
5027 // Read afresh: the view held for this command is the one from before the creates.
5028 *self.fields_cache()? = None;
5029 let board = match self.board_fields().await {
5030 Ok(board) => board,
5031 Err(error) => {
5032 return Err(SourceError::Refused {
5033 message: format!(
5034 "the guarded field setup changed the {} field and then could not \
5035 read the board back to verify it: {error}; run it again to verify \
5036 it, which creates nothing a second time",
5037 landed.join(" and ")
5038 ),
5039 });
5040 }
5041 };
5042 for field in &creates {
5043 let text = Board::field(&board.fields, &field.field)?.is_some_and(|held| {
5044 held.get("__typename").and_then(Value::as_str) == Some("ProjectV2Field")
5045 && held.get("dataType").and_then(Value::as_str) == Some("TEXT")
5046 });
5047 if !text {
5048 moved.push(format!("the created {} text field", field.field));
5049 }
5050 }
5051 }
5052 if !moved.is_empty() {
5053 return Err(SourceError::Refused {
5054 message: format!(
5055 "GitHub changed {} after the guarded field setup; the pre-write item \
5056 assignments are:\n{}",
5057 moved.join(", "),
5058 recovery(&report, &before)?
5059 ),
5060 });
5061 }
5062 Ok(report)
5063 }
5064
5065 /// Validate configuration and capture the named credential without exposing it.
5066 ///
5067 /// # Errors
5068 ///
5069 /// Returns [`SourceError::Config`] for a configuration this instance cannot use and
5070 /// [`SourceError::Auth`] when the named credential is missing or empty.
5071 pub fn new(
5072 name: &SourceName,
5073 config: GitHubProjectsConfig,
5074 secrets: &dyn SecretResolver,
5075 ) -> Result<Self, SourceError> {
5076 Self::recording_into(name, config, secrets, Arc::new(Accounting::new()))
5077 }
5078
5079 /// The same, recording every request it sends into an accounting the caller holds too.
5080 ///
5081 /// [`Self::new`] is this with an accounting of its own. A caller that is also making
5082 /// its own calls to GitHub — a lane verifying a schema, sweeping residue or cleaning
5083 /// up — passes the one it records those into, so the session total accounts for the
5084 /// whole session rather than for this source's share of it.
5085 ///
5086 /// # Errors
5087 ///
5088 /// Exactly [`Self::new`]'s: [`SourceError::Config`] for a configuration this instance
5089 /// cannot use and [`SourceError::Auth`] when the named credential is missing or empty.
5090 pub fn recording_into(
5091 name: &SourceName,
5092 config: GitHubProjectsConfig,
5093 secrets: &dyn SecretResolver,
5094 ledger: Arc<Accounting>,
5095 ) -> Result<Self, SourceError> {
5096 if !valid_github_owner(&config.owner) {
5097 return Err(SourceError::Config {
5098 message: "owner must be 1-39 ASCII letters, digits, or single hyphens, and cannot start or end with a hyphen".into(),
5099 });
5100 }
5101 if config.project_number == 0 || config.project_number > i32::MAX as u32 {
5102 return Err(SourceError::Config {
5103 message: format!("project_number must be between 1 and {}", i32::MAX),
5104 });
5105 }
5106 if !valid_environment_name(&config.token_env) {
5107 return Err(SourceError::Config {
5108 message: "token_env must be a valid environment-variable name".into(),
5109 });
5110 }
5111 let repository = config
5112 .repository
5113 .as_deref()
5114 .map(RepositoryTarget::parse)
5115 .transpose()?;
5116 if let Some(entry) = config
5117 .creation_owners
5118 .iter()
5119 .find(|entry| !valid_github_owner(entry))
5120 {
5121 return Err(SourceError::Config {
5122 message: format!(
5123 "creation_owners entry {entry:?} is not a GitHub login: a login is 1-39 ASCII \
5124 letters, digits, or single hyphens, and cannot start or end with a hyphen"
5125 ),
5126 });
5127 }
5128 let endpoint = Url::parse(&config.endpoint).map_err(|e| SourceError::Config {
5129 message: format!("endpoint is not a valid URL: {e}"),
5130 })?;
5131 if endpoint.scheme() != "https"
5132 && !(endpoint.scheme() == "http"
5133 && endpoint
5134 .host_str()
5135 .is_some_and(|h| h == "127.0.0.1" || h == "localhost" || h == "::1"))
5136 {
5137 return Err(SourceError::Config {
5138 message:
5139 "endpoint must use HTTPS (HTTP is accepted only for a loopback test server)"
5140 .into(),
5141 });
5142 }
5143 let token = secrets.get(&config.token_env).filter(|token| !token.expose_secret().trim().is_empty()).ok_or_else(|| SourceError::Auth {
5144 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),
5145 })?;
5146 Ok(Self {
5147 name: name.clone(),
5148 owner: config.owner,
5149 project_number: config.project_number,
5150 repository,
5151 creation_owners: config.creation_owners,
5152 asset_client: std::sync::OnceLock::new(),
5153 body_markup: std::sync::OnceLock::new(),
5154 endpoint,
5155 token,
5156 credential_name: config.token_env,
5157 statuses: BoardStatuses::resolve(&config.status_mapping, name)?,
5158 priorities: config
5159 .priority_mapping
5160 .map(|mapping| PriorityMapping::resolve(mapping, name))
5161 .transpose()?,
5162 metadata_fields: MetadataField::resolve(config.metadata_fields, name)?,
5163 client: std::sync::OnceLock::new(),
5164 created: Mutex::new(Vec::new()),
5165 updated: Mutex::new(Vec::new()),
5166 commented: Mutex::new(Vec::new()),
5167 pacing: Pacing::resolve(config.pacing, name)?,
5168 last_mutation: Mutex::new(None),
5169 windows: Mutex::new(pacing::Windows::default()),
5170 task_children: Mutex::new(BTreeMap::new()),
5171 written: Mutex::new(BTreeMap::new()),
5172 deferred: Mutex::new(None),
5173 held_updates: Mutex::new(Vec::new()),
5174 clock: system_clock(),
5175 declared_private: std::sync::atomic::AtomicBool::new(false),
5176 numeric_repositories: tokio::sync::Mutex::new(BTreeMap::new()),
5177 board_cache: Mutex::new(None),
5178 search_cache: Mutex::new(None),
5179 narrowed_cache: Mutex::new(BTreeMap::new()),
5180 resolved_cache: Mutex::new(BTreeMap::new()),
5181 read_ahead: Mutex::new(BTreeMap::new()),
5182 pending_origins: Mutex::new(Vec::new()),
5183 member_classes: Mutex::new(BTreeMap::new()),
5184 carried: Mutex::new(BTreeMap::new()),
5185 held_cache: Mutex::new(BTreeMap::new()),
5186 children_cache: Mutex::new(BTreeMap::new()),
5187 search_next: Mutex::new(BTreeMap::new()),
5188 fields_cache: Mutex::new(None),
5189 repository_cache: Mutex::new(BTreeMap::new()),
5190 ledger,
5191 expected: expected::Held::default(),
5192 projection: projection::Held::default(),
5193 })
5194 }
5195
5196 /// The client every API request goes through, built on the first one.
5197 ///
5198 /// Not with the source: building a client loads the system's certificate store, which
5199 /// costs tens of milliseconds, and a command that configures this board but never sends to
5200 /// it — a read of another source — should not pay that in its process.
5201 fn client(&self) -> Result<&Client, SourceError> {
5202 built(&self.client, || {
5203 Client::builder()
5204 .user_agent("onetaskgraph")
5205 .build()
5206 .map_err(|e| SourceError::Config {
5207 message: format!("cannot build HTTP client: {e}"),
5208 })
5209 })
5210 }
5211
5212 /// The client attachment requests go through, with its own redirect policy, built on the
5213 /// first one for the reason [`Self::client`] gives.
5214 fn asset_client(&self) -> Result<&Client, SourceError> {
5215 built(&self.asset_client, || assets::client(&self.endpoint))
5216 }
5217
5218 /// A snapshot of every request this source has sent, and what each cost.
5219 ///
5220 /// A value to hold and compare rather than a borrow of the accounting itself, so two
5221 /// of them can sit side by side. When this source was built with
5222 /// [`Self::recording_into`] the snapshot is the whole shared session, which is the
5223 /// point of building it that way.
5224 #[must_use]
5225 pub fn accounting(&self) -> accounting::Session {
5226 self.ledger.snapshot()
5227 }
5228
5229 /// Send one GraphQL document, pacing this source's own mutations and waiting out a
5230 /// rate limit rather than handing it straight back as an error.
5231 ///
5232 /// Retrying is safe for every document here, including the mutations, and the reason
5233 /// is that only a *refusal* is retried: [`Limiter::classify`] rules on a response
5234 /// GitHub sent, and a request GitHub refused for a rate limit did not run, so nothing
5235 /// this replays has already taken effect. An outcome this source cannot know — the
5236 /// send failed, or the body could not be read, so the mutation may well have landed —
5237 /// is [`Attempt::Failed`] in [`send_once`] and leaves this loop without a second
5238 /// attempt. A duplicate write would come from replaying one of those, and none is
5239 /// replayed.
5240 async fn graphql(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
5241 // Updates held back go out before any other request, so whatever it reads or writes
5242 // sees the board as though each had been sent when it was made.
5243 Box::pin(self.send_held_updates()).await?;
5244 // Origins held back for a read ahead that never came are sent before anything else.
5245 if query != graphql::ORIGIN_LOOKUPS && query != graphql::ORIGIN_LOOKUPS_WITH_MEMBERS {
5246 Box::pin(self.send_pending_origins(None)).await?;
5247 }
5248 if !self.expected.journaling() {
5249 return self.graphql_sent(query, variables).await;
5250 }
5251 // While a write the store's cache expects runs, its mutations select the whole issue
5252 // they leave behind, so the response vouches for what the cache keeps — and every
5253 // request is recorded, so the write's parts are reported from their own responses.
5254 let (query, variables) = match graphql::whole_twin(query) {
5255 Some(twin) => (twin, expected::whole_variables(variables)),
5256 None => (query, variables),
5257 };
5258 let sent = self.graphql_sent(query, variables.clone()).await;
5259 self.journal(query, &variables, &sent).await;
5260 sent
5261 }
5262
5263 /// One document sent, retried while GitHub asks for a wait, exactly as asked.
5264 async fn graphql_sent(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
5265 if is_mutation(query)
5266 && ![
5267 graphql::ADD_COMMENT,
5268 graphql::UPDATE_COMMENT,
5269 graphql::DELETE_COMMENT,
5270 graphql::ADD_COMMENT_WHOLE,
5271 graphql::UPDATE_COMMENT_WHOLE,
5272 ]
5273 .contains(&query)
5274 {
5275 let mut cache = self.resolved_cache()?;
5276 let mut ahead = self.read_ahead()?;
5277 let mut carried = self.carried()?;
5278 for argument in ["input", "second", "third", "clear"] {
5279 if let Some(input) = variables.get(argument) {
5280 let names = |id: &NativeId, item_id: Option<&str>| {
5281 ["id", "issueId", "subjectId", "itemId"].iter().any(|key| {
5282 input
5283 .get(key)
5284 .and_then(Value::as_str)
5285 .is_some_and(|value| value == id.0 || Some(value) == item_id)
5286 })
5287 };
5288 cache.retain(|id, item| !names(id, Some(&item.item_id)));
5289 ahead.retain(|id, item| {
5290 !names(id, item.as_ref().map(|item| item.item_id.as_str()))
5291 });
5292 carried.retain(|id, item| !names(id, Some(&item.item_id)));
5293 }
5294 }
5295 }
5296 let doing = operation_description(query);
5297 let mutation = is_mutation(query);
5298 let charge = pacing::Charge::graphql(query, &variables);
5299 let mut waited = Duration::ZERO;
5300 let mut waits = 0_u32;
5301 let mut backoff = self.pacing.retry_backoff;
5302 loop {
5303 let ticket = self.pace(charge, mutation).await;
5304 // Every write but a create is asked for immediately before it is sent — a create's
5305 // fields, its relationships, its content and the delete that takes it back included
5306 // — so a copy that owns the create renews its hold first, and one that has lost it
5307 // sends nothing more. Asked again before a retry, which is sent later.
5308 if mutation
5309 && !query.contains("createIssue(")
5310 && let Err(refused) = <dyn onetaskgraph_plugin_api::WriteGuard>::check().await
5311 {
5312 self.paced(ticket, mutation);
5313 return Err(refused);
5314 }
5315 let attempt = self.send_once(query, &variables, waits + 1).await;
5316 self.paced(ticket, mutation);
5317 let remaining = self.pacing.retry_budget.saturating_sub(waited);
5318 let (wait, cause) = match attempt {
5319 Ok(data) => return Ok(data),
5320 Err(Attempt::Failed(error)) => return Err(error),
5321 Err(Attempt::Conflicted(said)) => {
5322 // A create is never replayed: see `CONFLICT_WORDINGS`.
5323 if query.contains("createIssue(") || backoff.is_zero() || backoff > remaining {
5324 return Err(SourceError::Unavailable {
5325 message: format!(
5326 "GitHub refused this source while {doing} as a temporary \
5327 conflict ({said}); it retried {} over {} and the conflict \
5328 did not clear within the {} one call may spend waiting. \
5329 next: run the command again once nothing else is writing to \
5330 the same items",
5331 plural(waits, "time"),
5332 seconds(waited),
5333 seconds(self.pacing.retry_budget),
5334 ),
5335 });
5336 }
5337 (backoff, pacing::cause::BACKOFF)
5338 }
5339 Err(Attempt::Limited(limited)) => {
5340 let (wait, cause) = limited.wait(backoff);
5341 // A wait of nothing spends none of the budget, so it is exhaustion rather
5342 // than a retry. `Pacing::resolve` rules out every way of configuring one
5343 // except a budget of zero, where reporting the first refusal is the ask.
5344 if wait.is_zero() || wait > remaining {
5345 return Err(limited.exhausted(
5346 doing,
5347 waits,
5348 waited,
5349 wait,
5350 self.pacing.retry_budget,
5351 ));
5352 }
5353 (wait, cause)
5354 }
5355 };
5356 self.clock.sleep_because(wait, cause).await;
5357 waited += wait;
5358 waits += 1;
5359 backoff = backoff.saturating_mul(2);
5360 }
5361 }
5362
5363 /// Hold one request charging `charge` until every secondary window it charges has room for
5364 /// the whole of it — and, for a mutation of an instance configured with
5365 /// `pacing.min_mutation_interval_ms`, until that interval has passed since the last one
5366 /// finished — then count it, answering the ticket [`Self::paced`] finishes it with.
5367 ///
5368 /// The moment is decided and counted under the lock and waited out outside it, so two
5369 /// callers take two places rather than the same one, and no lock is held across an await.
5370 /// A request with room goes out at once: nothing here waits that a window does not ask for.
5371 async fn pace(&self, charge: pacing::Charge, mutation: bool) -> pacing::Ticket {
5372 let (wait, cause, ticket) = {
5373 // A poisoned lock here costs pacing, not correctness, and refusing the write over
5374 // it would turn an earlier failure into a second one for no gain.
5375 let mut windows = self
5376 .windows
5377 .lock()
5378 .unwrap_or_else(std::sync::PoisonError::into_inner);
5379 let now = self.clock.now();
5380 let (mut at, mut cause) = if self.pacing.windows {
5381 windows.room(now, charge)
5382 } else {
5383 (now, None)
5384 };
5385 if mutation && !self.pacing.min_mutation_interval.is_zero() {
5386 let mut last = self
5387 .last_mutation
5388 .lock()
5389 .unwrap_or_else(std::sync::PoisonError::into_inner);
5390 // `checked_add` rather than `+`: adding durations can panic on overflow, and
5391 // pacing is not worth a panic even at a bound `MAX_PACING_MS` already rules out.
5392 if let Some(earliest) = last
5393 .and_then(|previous| previous.checked_add(self.pacing.min_mutation_interval))
5394 && earliest > at
5395 {
5396 at = earliest;
5397 cause = Some(pacing::cause::MIN_INTERVAL);
5398 }
5399 *last = Some(at);
5400 }
5401 let ticket = windows.charge(at, charge);
5402 (at.saturating_sub(now), cause, ticket)
5403 };
5404 if let (false, Some(cause)) = (wait.is_zero(), cause) {
5405 self.clock.sleep_because(wait, cause).await;
5406 }
5407 ticket
5408 }
5409
5410 /// Record that the request `ticket` was charged for has finished, so each window — and
5411 /// the configured spacing — counts it from here rather than from the moment it left.
5412 ///
5413 /// This source can only choose when a request *departs*; GitHub's limiter counts when it
5414 /// *arrives*, and the two differ by whatever the request spent in transit. Counting from
5415 /// departure would hand the limiter a window that looked roomier than it is, so a source
5416 /// pacing exactly at a limit could still be seen arriving over it — which is how a copy
5417 /// paced well inside a board's threshold was once refused by it on a slower machine while
5418 /// passing on a quick one. The previous request had arrived before its response came back,
5419 /// so counting from here is never earlier than the limiter does, whatever transit costs
5420 /// and on whatever platform: the safe side of a limit that punishes being wrong by refusing
5421 /// reads for the next fifty minutes.
5422 ///
5423 /// A failed attempt is finished too: a request refused by the limiter still arrived, and
5424 /// one that never left costs only room nobody needed.
5425 fn paced(&self, ticket: pacing::Ticket, mutation: bool) {
5426 let now = self.clock.now();
5427 self.windows
5428 .lock()
5429 .unwrap_or_else(std::sync::PoisonError::into_inner)
5430 .finish(ticket, now);
5431 if mutation && !self.pacing.min_mutation_interval.is_zero() {
5432 let mut last = self
5433 .last_mutation
5434 .lock()
5435 .unwrap_or_else(std::sync::PoisonError::into_inner);
5436 // `max` rather than an assignment: a concurrent caller may already have reserved a
5437 // slot further out, and completing this request must never pull that slot back in.
5438 *last = Some(last.map_or(now, |reserved| reserved.max(now)));
5439 }
5440 }
5441
5442 /// One HTTP attempt, classified into an answer, a rate limit to wait out, or a
5443 /// failure that waiting cannot help — and recorded, whichever of the three it was.
5444 ///
5445 /// This is the one place a request leaves this crate, which is why the accounting is
5446 /// here rather than at each of the callers: a read path added later is counted without
5447 /// anybody remembering to count it, and
5448 /// `the_session_report_counts_every_request_the_board_served_and_what_each_cost` fails
5449 /// when one is not.
5450 async fn send_once(
5451 &self,
5452 query: &str,
5453 variables: &Value,
5454 number: u32,
5455 ) -> Result<Value, Attempt> {
5456 let executed = self.ledger.priced_document(query);
5457 let Attempted {
5458 result,
5459 limits,
5460 reported_cost,
5461 response_outcome,
5462 } = self.attempt(executed.as_ref(), variables).await;
5463 // No `otherwise` name: every document this source sends is one of its own, and the
5464 // inventory gate on `graphql::DOCUMENTS` is what keeps that true.
5465 let sending = accounting::Request::graphql(query, variables, None, reported_cost);
5466 let outcome = match &result {
5467 Ok(_) => accounting::Outcome::Answered,
5468 Err(Attempt::Limited(_)) => accounting::Outcome::RateLimited,
5469 Err(Attempt::Failed(_) | Attempt::Conflicted(_)) => accounting::Outcome::Refused,
5470 };
5471 self.ledger
5472 .record(sending.finished(outcome, limits).with_execution(
5473 Some(executed.into_owned()),
5474 response_outcome,
5475 number,
5476 ));
5477 result
5478 }
5479
5480 /// The attempt itself, with what its response said about the rate limit alongside.
5481 ///
5482 /// The two are returned together rather than recorded here because every one of the
5483 /// early exits below is a different outcome, and a record written at each of them is a
5484 /// record one of them can be added without.
5485 async fn attempt(&self, query: &str, variables: &Value) -> Attempted {
5486 let mut limits = accounting::RateLimit::default();
5487 let mut reported_cost = None;
5488 let mut response_outcome = accounting::ResponseOutcome::TransportError;
5489 let result = self
5490 .attempted(
5491 query,
5492 variables,
5493 &mut limits,
5494 &mut reported_cost,
5495 &mut response_outcome,
5496 )
5497 .await;
5498 Attempted {
5499 result,
5500 limits,
5501 reported_cost,
5502 response_outcome,
5503 }
5504 }
5505
5506 /// One HTTP attempt, filling in what its response said about the rate limit as it goes.
5507 async fn attempted(
5508 &self,
5509 query: &str,
5510 variables: &Value,
5511 limits: &mut accounting::RateLimit,
5512 reported_cost: &mut Option<u64>,
5513 response_outcome: &mut accounting::ResponseOutcome,
5514 ) -> Result<Value, Attempt> {
5515 let response = self
5516 .client()
5517 .map_err(Attempt::Failed)?
5518 .post(self.endpoint.clone())
5519 .bearer_auth(self.token.expose_secret())
5520 .json(&json!({"query": query, "variables": variables}))
5521 .send()
5522 .await
5523 .map_err(|e| {
5524 Attempt::Failed(SourceError::Unavailable {
5525 message: format!("GitHub GraphQL request failed: {}", transport_error(&e)),
5526 })
5527 })?;
5528 let status = response.status();
5529 if !status.is_success() {
5530 *response_outcome = accounting::ResponseOutcome::HttpError;
5531 }
5532 let header = |name: &str| whole_seconds(response.headers().get(name));
5533 let served = response
5534 .headers()
5535 .get(reqwest::header::DATE)
5536 .and_then(|value| value.to_str().ok())
5537 .and_then(|value| DateTime::parse_from_rfc2822(value).ok())
5538 .map(|at| at.with_timezone(&Utc));
5539 *limits = accounting::RateLimit::read(|name| {
5540 response
5541 .headers()
5542 .get(name)
5543 .and_then(|value| value.to_str().ok())
5544 .map(str::to_owned)
5545 });
5546 // Exactly `0` is exhaustion and everything else — a count, an empty value, bytes
5547 // that are not text at all — is "not known to be exhausted". This never makes a
5548 // response a refusal on its own: it says which limiter a refusal is attributed to
5549 // and where its hint comes from, so a value this cannot read costs a hint rather
5550 // than an answer.
5551 let exhausted = response
5552 .headers()
5553 .get("x-ratelimit-remaining")
5554 .and_then(|value| value.to_str().ok())
5555 == Some("0");
5556 // `retry-after` is what GitHub asks for when it asks; when it does not and the
5557 // primary budget is spent, `x-ratelimit-reset` says when that budget comes back,
5558 // which is the same question answered as an absolute time. Nothing else here is a
5559 // hint, and a schedule is what answers a refusal that carries none.
5560 let retry_after = header("retry-after");
5561 let hinted_by = if retry_after.is_some() || !exhausted {
5562 Hint::RetryAfter
5563 } else {
5564 Hint::PrimaryReset
5565 };
5566 let hint = retry_after.or_else(|| {
5567 exhausted
5568 .then(|| header("x-ratelimit-reset"))
5569 .flatten()
5570 .map(|reset| reset.saturating_sub(Utc::now().timestamp().max(0).unsigned_abs()))
5571 });
5572 // Read before it is parsed, because the evidence which tells a secondary rate
5573 // limit from a rejected credential is in the body of a response whose status says
5574 // only "forbidden" — and a non-success response was never parsed at all.
5575 let body = response.text().await.map_err(|e| {
5576 Attempt::Failed(SourceError::Unavailable {
5577 message: format!(
5578 "GitHub GraphQL response could not be read: {}",
5579 transport_error(&e)
5580 ),
5581 })
5582 })?;
5583 *reported_cost = serde_json::from_str::<Value>(&body)
5584 .ok()
5585 .as_ref()
5586 .and_then(|body| body.pointer("/data/rateLimit/cost"))
5587 .and_then(Value::as_u64);
5588 let parsed = serde_json::from_str::<Value>(&body).ok();
5589 *response_outcome = accounting::ResponseOutcome::of_graphql_reply(status, parsed.as_ref());
5590 if let Some(limiter) = Limiter::classify(status, exhausted, &body) {
5591 return Err(Attempt::Limited(Limited {
5592 limiter,
5593 hint,
5594 hinted_by,
5595 }));
5596 }
5597 if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {
5598 return Err(Attempt::Failed(SourceError::Auth {
5599 message: format!(
5600 "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"
5601 ),
5602 }));
5603 }
5604 if !status.is_success() {
5605 return Err(Attempt::Failed(SourceError::Unavailable {
5606 message: format!("GitHub GraphQL returned HTTP {status}"),
5607 }));
5608 }
5609 let said = refusal_wording(status, &body);
5610 let normalized = said.to_ascii_lowercase();
5611 if CONFLICT_WORDINGS
5612 .iter()
5613 .any(|wording| normalized.contains(wording))
5614 {
5615 return Err(Attempt::Conflicted(said));
5616 }
5617 let answered = if query == graphql::ISSUES_BY_REFERENCE {
5618 self.answer_unresolved(&body)
5619 } else if query == graphql::CREATE_ISSUES {
5620 answer_with_errors(&body).map_or_else(|| self.answer(&body), Ok)
5621 } else {
5622 self.answer(&body)
5623 };
5624 let mut data = answered.map_err(Attempt::Failed)?;
5625 if let Some(served) = served {
5626 stamp_served(&mut data, &served.to_rfc3339());
5627 }
5628 Ok(data)
5629 }
5630
5631 /// What one successful HTTP response says, once its GraphQL errors are read.
5632 /// [`Self::answer`], for a read of issues by reference: a reference GitHub cannot resolve
5633 /// for this credential — a repository it cannot see, an issue number it does not hold —
5634 /// is answered `null` beside a `NOT_FOUND` error naming it, and that is an answer about
5635 /// that reference rather than a failure of the others. Any other error refuses the read,
5636 /// as there.
5637 fn answer_unresolved(&self, body: &str) -> Result<Value, SourceError> {
5638 let parsed: Option<Value> = serde_json::from_str(body).ok();
5639 // Each error is a `NOT_FOUND` whose path names one slot of the batch, `r<n>` or its
5640 // `issue`, and the answer holds `null` at exactly that path: anything else — another
5641 // kind of error, one naming no slot, or one beside a value it says is not there — is a
5642 // refusal of the whole read.
5643 let unresolved = |data: &Value, error: &Value| {
5644 error.get("type").and_then(Value::as_str) == Some("NOT_FOUND")
5645 && error
5646 .get("path")
5647 .and_then(Value::as_array)
5648 .is_some_and(|path| {
5649 let alias = path.first().and_then(Value::as_str).and_then(|alias| {
5650 alias
5651 .strip_prefix('r')
5652 .and_then(|slot| slot.parse::<usize>().ok())
5653 .filter(|slot| *slot < REFERENCE_BATCH)
5654 .map(|_| alias)
5655 });
5656 match (alias, path.get(1).and_then(Value::as_str), path.len()) {
5657 (Some(alias), None, 1) => data.get(alias).is_some_and(Value::is_null),
5658 (Some(alias), Some("issue"), 2) => data
5659 .get(alias)
5660 .and_then(|repository| repository.get("issue"))
5661 .is_some_and(Value::is_null),
5662 _ => false,
5663 }
5664 })
5665 };
5666 let unresolved_only = parsed.as_ref().is_some_and(|parsed| {
5667 parsed.get("data").is_some_and(|data| {
5668 data.is_object()
5669 && parsed
5670 .get("errors")
5671 .and_then(Value::as_array)
5672 .is_some_and(|errors| errors.iter().all(|error| unresolved(data, error)))
5673 })
5674 });
5675 match parsed {
5676 Some(parsed) if unresolved_only => Ok(parsed["data"].clone()),
5677 _ => self.answer(body),
5678 }
5679 }
5680
5681 fn answer(&self, body: &str) -> Result<Value, SourceError> {
5682 let body: Value = serde_json::from_str(body).map_err(|e| SourceError::Malformed {
5683 message: format!("GitHub returned invalid JSON: {e}"),
5684 })?;
5685 let errors = body
5686 .get("errors")
5687 .map(|value| {
5688 value.as_array().ok_or_else(|| SourceError::Malformed {
5689 message: "GitHub response errors is not an array".into(),
5690 })
5691 })
5692 .transpose()?;
5693 if let Some(errors) = errors.filter(|errors| !errors.is_empty()) {
5694 let messages = errors
5695 .iter()
5696 .filter_map(|e| e.get("message").and_then(Value::as_str))
5697 .collect::<Vec<_>>()
5698 .join("; ");
5699 let message = if messages.is_empty() {
5700 "GitHub returned GraphQL errors".into()
5701 } else {
5702 messages
5703 };
5704 let normalized = message.to_ascii_lowercase();
5705 if normalized.contains("resource not accessible") || normalized.contains("scope") {
5706 return Err(SourceError::Auth {
5707 message: format!(
5708 "{message}; grant {} Projects and Issues read/write plus Pull requests read-only access for every repository represented on the board",
5709 self.credential_name
5710 ),
5711 });
5712 }
5713 return Err(SourceError::Refused { message });
5714 }
5715 body.get("data")
5716 .filter(|data| data.is_object())
5717 .cloned()
5718 .ok_or_else(|| SourceError::Malformed {
5719 message: "GitHub response has no data object".into(),
5720 })
5721 }
5722
5723 // llmlint: ignore[boundary_inputs_validated] GitHub caps nested connections at 100 and
5724 // GraphQL cannot independently page them inside the outer item page. This source page is
5725 // deliberately bounded at that published maximum; the live drift journey exercises it.
5726 async fn board_page(
5727 &self,
5728 items_after: Option<&str>,
5729 items_first: u32,
5730 ) -> Result<Value, SourceError> {
5731 let data = self
5732 .graphql(
5733 graphql::BOARD,
5734 json!({"owner":self.owner,"number":self.project_number,
5735 "first":items_first.min(MAX_PAGE_SIZE),"after":items_after,
5736 "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
5737 )
5738 .await?;
5739 data.pointer("/owner/projectV2")
5740 .filter(|v| !v.is_null())
5741 .cloned()
5742 .ok_or_else(|| SourceError::Refused {
5743 message: format!(
5744 "GitHub project {}/{} was not found or is not visible to the token",
5745 self.owner, self.project_number
5746 ),
5747 })
5748 }
5749
5750 /// The search that finds the issues of this board, narrowed by `also` when it is
5751 /// given.
5752 ///
5753 /// `project:owner/number` is what scopes a search to one board, and `is:issue` is what
5754 /// keeps pull requests out of it: GitHub's `ISSUE` search type covers both, and a pull
5755 /// request is somebody's change rather than a unit of plan. `-has:parent` is *not*
5756 /// here on purpose — GitHub accepts it and silently ignores it, so a project is told
5757 /// from a task by the `parent` field each issue carries rather than by the search.
5758 fn board_search(&self, also: Option<&str>) -> String {
5759 let scope = format!("project:{}/{} is:issue", self.owner, self.project_number);
5760 match also {
5761 Some(also) => format!("{scope} {also}"),
5762 None => scope,
5763 }
5764 }
5765
5766 /// One issue this source reached directly, as the board item a read of the board would
5767 /// have produced — or `None` when this board does not hold it.
5768 ///
5769 /// The board half of an issue rides along on `Issue.projectItems`, so the value handed
5770 /// to [`Self::resolve`] is the very shape a `ProjectV2.items` read gives it: the board
5771 /// item's own id, that item's field values, and the issue as its content. One resolver
5772 /// for both routes is what makes an issue read through a search, through its own node
5773 /// id, or through its project's sub-issues report the same title, the same status, the
5774 /// same labels and the same qualified id.
5775 ///
5776 /// An issue with no entry for *this* board is not this source's to report, which is
5777 /// what keeps an id naming some other repository's issue from being answered as an item
5778 /// of this board. That answer is given about an **exhausted** connection and never
5779 /// about an unread page: the entry is looked for on the page in hand, and only if that
5780 /// page reports more of the connection, in [`Self::board_membership`]'s walk of the
5781 /// rest of it.
5782 async fn resolve_issue(&self, issue: &Value) -> Result<Option<Resolved>, SourceError> {
5783 self.resolve_issue_as(issue, true).await
5784 }
5785
5786 /// [`Self::resolve_issue`], remembering what it resolves for this command's later reads
5787 /// and writes only when `remember` says to.
5788 async fn resolve_issue_as(
5789 &self,
5790 issue: &Value,
5791 remember: bool,
5792 ) -> Result<Option<Resolved>, SourceError> {
5793 if optional_str(issue, "__typename")? != Some("Issue") {
5794 return Ok(None);
5795 }
5796 let memberships = issue
5797 .get("projectItems")
5798 .ok_or_else(|| SourceError::Malformed {
5799 message: "GitHub issue is missing projectItems".into(),
5800 })?;
5801 let nodes = memberships
5802 .get("nodes")
5803 .and_then(Value::as_array)
5804 .ok_or_else(|| SourceError::Malformed {
5805 message: "GitHub issue projectItems.nodes is not an array".into(),
5806 })?;
5807 let held = match self.board_entry(nodes) {
5808 Some(held) => held.clone(),
5809 None => {
5810 let info = memberships
5811 .get("pageInfo")
5812 .ok_or_else(|| SourceError::Malformed {
5813 message: "GitHub issue projectItems has no pageInfo".into(),
5814 })?;
5815 // The page held no entry for this board. Whether that means the issue is
5816 // not on it is a question about the rest of the connection, and only a
5817 // connection with no rest answers it here.
5818 if !required_bool(info, "hasNextPage")? {
5819 return Ok(None);
5820 }
5821 let cursor = required_str(info, "endCursor")?;
5822 validate_cursor_progress(None, cursor)?;
5823 let issue_id = required_str(issue, "id")?;
5824 match self.board_membership(issue_id, cursor).await? {
5825 Some(held) => held,
5826 None => return Ok(None),
5827 }
5828 }
5829 };
5830 // The membership's own `updatedAt` rides along: it is the board half of the item's change
5831 // token, which a field change moves where the issue's own time does not.
5832 let item = json!({
5833 "id": required_str(&held, "id")?,
5834 "updatedAt": held.get("updatedAt"),
5835 "project": held.get("project"),
5836 "fieldValues": held.get("fieldValues"),
5837 "content": issue,
5838 });
5839 if remember {
5840 self.resolve(&item)
5841 } else {
5842 self.resolve_unremembered(&item)
5843 }
5844 }
5845
5846 /// This board's own entry among one page of an issue's `Issue.projectItems`.
5847 ///
5848 /// One spelling of *which membership is this board's*, so the page a read carries and
5849 /// the pages [`Self::board_membership`] walks are searched by the same rule.
5850 fn board_entry<'a>(&self, nodes: &'a [Value]) -> Option<&'a Value> {
5851 nodes.iter().find(|node| {
5852 node.pointer("/project/number").and_then(Value::as_u64)
5853 == Some(u64::from(self.project_number))
5854 })
5855 }
5856
5857 /// The rest of one issue's board memberships, from `after`, for this board's entry.
5858 ///
5859 /// The recovery read: a page of memberships that holds no entry for this board says
5860 /// nothing about the memberships past it, so the connection is walked to exhaustion
5861 /// before an issue is reported as one this board does not hold. `Ok(None)` is that
5862 /// positive answer — the whole connection was read and no entry named this board —
5863 /// rather than a failure, and the walk is held to
5864 /// [`validate_cursor_progress`] like every other page walk here, so a source answering
5865 /// with a cursor that does not advance is refused instead of spun on.
5866 async fn board_membership(
5867 &self,
5868 issue: &str,
5869 after: &str,
5870 ) -> Result<Option<Value>, SourceError> {
5871 let mut after = after.to_owned();
5872 loop {
5873 let data = self
5874 .graphql(
5875 graphql::ISSUE_BOARD_ITEMS,
5876 json!({"id":issue,"first":MAX_PAGE_SIZE,"after":after,
5877 "nestedFirst":NESTED_PAGE_SIZE}),
5878 )
5879 .await?;
5880 let Some(connection) = data
5881 .pointer("/node/projectItems")
5882 .filter(|value| !value.is_null())
5883 else {
5884 // The id resolved to nothing, or to something with no memberships to walk —
5885 // which is the same answer as a connection holding no entry for this board.
5886 return Ok(None);
5887 };
5888 let nodes = connection
5889 .get("nodes")
5890 .and_then(Value::as_array)
5891 .ok_or_else(|| SourceError::Malformed {
5892 message: "GitHub issue projectItems.nodes is not an array".into(),
5893 })?;
5894 if let Some(held) = self.board_entry(nodes) {
5895 return Ok(Some(held.clone()));
5896 }
5897 let info = connection
5898 .get("pageInfo")
5899 .ok_or_else(|| SourceError::Malformed {
5900 message: "GitHub issue projectItems has no pageInfo".into(),
5901 })?;
5902 let next = required_bool(info, "hasNextPage")?
5903 .then(|| required_str(info, "endCursor"))
5904 .transpose()?;
5905 match next {
5906 Some(next) => {
5907 validate_cursor_progress(Some(&after), next)?;
5908 after = next.to_owned();
5909 }
5910 None => return Ok(None),
5911 }
5912 }
5913 }
5914
5915 /// One page of a board-scoped issue search, and where the next page resumes.
5916 async fn search_page(
5917 &self,
5918 search: &str,
5919 first: u32,
5920 after: Option<&str>,
5921 ) -> Result<(Vec<Resolved>, Option<String>), SourceError> {
5922 let data = self
5923 .graphql(
5924 graphql::SEARCH_ISSUES,
5925 json!({"search":search,"type":"ISSUE","first":first.min(MAX_PAGE_SIZE),
5926 "after":after,"nestedFirst":NESTED_PAGE_SIZE,
5927 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
5928 )
5929 .await?;
5930 let connection = data.get("search").ok_or_else(|| SourceError::Malformed {
5931 message: "GitHub search response has no search connection".into(),
5932 })?;
5933 let mut found = Vec::new();
5934 for node in connection
5935 .get("nodes")
5936 .and_then(Value::as_array)
5937 .ok_or_else(|| SourceError::Malformed {
5938 message: "GitHub search nodes is not an array".into(),
5939 })?
5940 {
5941 if let Some(resolved) = self.resolve_issue(node).await? {
5942 found.push(resolved);
5943 }
5944 }
5945 let info = connection
5946 .get("pageInfo")
5947 .ok_or_else(|| SourceError::Malformed {
5948 message: "GitHub search connection has no pageInfo".into(),
5949 })?;
5950 let next = required_bool(info, "hasNextPage")?
5951 .then(|| required_str(info, "endCursor"))
5952 .transpose()?
5953 .map(str::to_owned);
5954 if let Some(next) = &next {
5955 validate_cursor_progress(after, next)?;
5956 }
5957 Ok((found, next))
5958 }
5959
5960 /// Every issue this board holds, completed with what this run wrote.
5961 ///
5962 /// The completion is not an optimisation and it is not a cache: GitHub's issue search
5963 /// is an index and is eventually consistent, so an issue this run created seconds ago
5964 /// can be absent from it, and a project listed straight after being written would
5965 /// otherwise be missing from its own board. What is added back is only what this
5966 /// process itself wrote, out of [`Self::created`], which lives and dies with the
5967 /// process.
5968 async fn board_issues(&self) -> Result<Vec<Resolved>, SourceError> {
5969 let found = self.searched_issues().await?;
5970 self.completed_with_written(found, |_| true)
5971 }
5972
5973 /// Every issue this board's own search reports, walked to exhaustion, read once per
5974 /// source.
5975 ///
5976 /// The uncompleted half of [`Self::board_issues`], separated because [`Self::board`]
5977 /// needs it too and the two would otherwise walk the same search twice in one command.
5978 /// See [`Self::search_cache`] for why holding it is the same bargain holding the board
5979 /// is.
5980 async fn searched_issues(&self) -> Result<Vec<Resolved>, SourceError> {
5981 let cached = self.search_cache()?.clone();
5982 if let Some(held) = cached {
5983 return Ok(held);
5984 }
5985 let mut after: Option<String> = None;
5986 let mut found = Vec::new();
5987 let search = self.board_search(None);
5988 loop {
5989 let (page, next) = self
5990 .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
5991 .await?;
5992 found.extend(page);
5993 match next {
5994 Some(next) => after = Some(next),
5995 None => break,
5996 }
5997 }
5998 *self.search_cache()? = Some(found.clone());
5999 Ok(found)
6000 }
6001
6002 /// Every issue this board's own search reports with `also` beside its scope, walked to
6003 /// exhaustion — held for no later read, which may not be narrowed alike.
6004 async fn searched_narrowly(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
6005 let mut after: Option<String> = None;
6006 let mut found = Vec::new();
6007 let search = self.board_search(Some(also));
6008 loop {
6009 let (page, next) = self
6010 .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
6011 .await?;
6012 found.extend(page);
6013 match next {
6014 Some(next) => after = Some(next),
6015 None => return Ok(found),
6016 }
6017 }
6018 }
6019
6020 /// This process's own view of the board's issues, or the refusal a poisoned lock is.
6021 fn search_cache(
6022 &self,
6023 ) -> Result<std::sync::MutexGuard<'_, Option<Vec<Resolved>>>, SourceError> {
6024 self.search_cache
6025 .lock()
6026 .map_err(|_| SourceError::Unavailable {
6027 message: "this source's view of the board's issues was left inconsistent by an \
6028 earlier failure; next: run the command again"
6029 .into(),
6030 })
6031 }
6032
6033 /// `found`, with everything this run wrote that `keep` accepts and the read did not
6034 /// report.
6035 ///
6036 /// See [`Self::created`] and [`Self::board_issues`] for why a read has to be completed
6037 /// at all: the search index is behind, and a node read of an item filed moments ago can
6038 /// be too.
6039 fn completed_with_written(
6040 &self,
6041 mut found: Vec<Resolved>,
6042 keep: impl Fn(&Resolved) -> bool,
6043 ) -> Result<Vec<Resolved>, SourceError> {
6044 for own in self.created()?.iter().filter(|own| keep(own)) {
6045 if !found.iter().any(|item| item.id == own.id) {
6046 found.push(own.clone());
6047 }
6048 }
6049 Ok(found)
6050 }
6051
6052 /// What resolving one node id reached.
6053 ///
6054 /// Three answers rather than an `Option`, because a board *draft* is none of the other
6055 /// two: it is not an issue, so the issue fragment reads nothing of it, and a read of one
6056 /// is completed by a read of the draft itself rather than reported as nothing.
6057 async fn reach(&self, id: &NativeId) -> Result<Reached, SourceError> {
6058 let asked = self
6059 .graphql(
6060 graphql::ISSUE,
6061 json!({"id":id.0,"first":MAX_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
6062 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
6063 )
6064 .await;
6065 let data = match asked {
6066 Ok(data) => data,
6067 // A string that is not a node id at all is not a failure to report: it is an id
6068 // this board does not hold, which is what every read of one already answers.
6069 Err(error) if unresolvable_node(&error) => return Ok(Reached::Nothing),
6070 Err(error) => return Err(error),
6071 };
6072 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
6073 return Ok(Reached::Nothing);
6074 };
6075 if optional_str(node, "__typename")? == Some("DraftIssue") {
6076 return Ok(Reached::Draft);
6077 }
6078 Ok(match self.resolve_issue(node).await? {
6079 Some(item) => Reached::Held(Box::new(item)),
6080 None => Reached::Nothing,
6081 })
6082 }
6083
6084 /// One item of this board by its own id, whatever kind it is.
6085 ///
6086 /// Resolved from the identifier alone: no search, board-wide or otherwise. What this
6087 /// run wrote is read first, because a node read of an item created moments ago can
6088 /// still be behind the board field values written onto it — see [`Self::created`].
6089 async fn item_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6090 if let Some(own) = self.created()?.iter().find(|own| own.id == *id) {
6091 return Ok(Some(own.clone()));
6092 }
6093 if let Some(held) = self.read_ahead()?.remove(id) {
6094 return Ok(held);
6095 }
6096 match self.reach(id).await? {
6097 Reached::Held(item) => Ok(Some(*item)),
6098 Reached::Nothing => Ok(None),
6099 Reached::Draft => self.draft_by_id(id).await,
6100 }
6101 }
6102
6103 /// [`Self::item_by_id`] for a document: one [`graphql::DOCUMENT_IN_PROJECT`] read, which
6104 /// answers the project it is filed under and that project's members beside it.
6105 ///
6106 /// Each is held for this command as a read of it would have been: the project for the
6107 /// next read of it by its id, and — when its page of members is the whole of them — the
6108 /// members for the next listing of that project. So a write held to who may read the
6109 /// document, which reads all three, is answered by this one request.
6110 async fn document_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6111 if self.created()?.iter().any(|own| own.id == *id) || self.read_ahead()?.contains_key(id) {
6112 return self.item_by_id(id).await;
6113 }
6114 let asked = self
6115 .graphql(
6116 graphql::DOCUMENT_IN_PROJECT,
6117 json!({"id":id.0,"first":MAX_PAGE_SIZE,"memberFirst":MEMBER_PAGE_SIZE,
6118 "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
6119 "duplicates":true}),
6120 )
6121 .await;
6122 let data = match asked {
6123 Ok(data) => data,
6124 Err(error) if unresolvable_node(&error) => return Ok(None),
6125 Err(error) => return Err(error),
6126 };
6127 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
6128 return Ok(None);
6129 };
6130 if optional_str(node, "__typename")? == Some("DraftIssue") {
6131 return self.draft_by_id(id).await;
6132 }
6133 let Some(item) = self.resolve_issue(node).await? else {
6134 return Ok(None);
6135 };
6136 // An answer for another issue than the one asked about says nothing of that one.
6137 if item.id != *id {
6138 return Err(SourceError::Malformed {
6139 message: format!(
6140 "GitHub answered issue {} when asked for document {}",
6141 item.id.0, id.0
6142 ),
6143 });
6144 }
6145 if let Some(project) = node.get("project").filter(|value| !value.is_null())
6146 && let Some(parent) = self
6147 .resolve_issue(project)
6148 .await?
6149 .filter(|parent| Some(&parent.id) == item.parent.as_ref())
6150 {
6151 let members = project.get("subIssues").filter(|value| !value.is_null());
6152 let whole = members
6153 .and_then(|members| members.pointer("/pageInfo/hasNextPage"))
6154 .and_then(Value::as_bool)
6155 == Some(false);
6156 if whole {
6157 let nodes = members
6158 .and_then(|members| members.get("nodes"))
6159 .and_then(Value::as_array)
6160 .ok_or_else(|| SourceError::Malformed {
6161 message: "GitHub subIssues.nodes is not an array".into(),
6162 })?;
6163 let mut children = Vec::new();
6164 for member in nodes {
6165 if let Some(resolved) = self.resolve_issue(member).await? {
6166 children.push(resolved);
6167 }
6168 }
6169 // Held as the project's members only when every one resolved and says it is
6170 // filed under it: a page with any other answers nothing of all the members.
6171 if children.len() == nodes.len()
6172 && children
6173 .iter()
6174 .all(|child| child.parent.as_ref() == Some(&parent.id))
6175 {
6176 self.children_cache()?
6177 .insert(parent.id.clone(), (parent.id.clone(), children));
6178 }
6179 }
6180 self.read_ahead()?.insert(parent.id.clone(), Some(parent));
6181 }
6182 Ok(Some(item))
6183 }
6184
6185 /// Several items of this board, each by its own id, in order — what [`Self::item_by_id`]
6186 /// answers for each, read [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] rather
6187 /// than one request per id.
6188 ///
6189 /// What this run wrote answers first, as it does there, and only the rest is read. One id
6190 /// left to read is read by [`Self::item_by_id`] itself, which costs what a batch does. A
6191 /// batch GitHub refuses because one of its ids resolves to no node at all is read again one
6192 /// id at a time, so that id is answered as not held and the others as themselves; a draft
6193 /// is completed by a read of the draft, exactly as there.
6194 async fn items_by_ids(&self, ids: &[NativeId]) -> Result<Vec<Option<Resolved>>, SourceError> {
6195 let mut found: Vec<Option<Option<Resolved>>> = {
6196 let created = self.created()?;
6197 ids.iter()
6198 .map(|id| {
6199 created
6200 .iter()
6201 .find(|own| own.id == *id)
6202 .map(|own| Some(own.clone()))
6203 })
6204 .collect()
6205 };
6206 let unread: Vec<NativeId> = ids
6207 .iter()
6208 .zip(&found)
6209 .filter(|(_, found)| found.is_none())
6210 .map(|(id, _)| id.clone())
6211 .collect();
6212 let mut read = Vec::with_capacity(unread.len());
6213 if let [one] = unread.as_slice() {
6214 read.push(self.item_by_id(one).await?);
6215 } else {
6216 for batch in unread.chunks(DETAIL_BATCH) {
6217 let data = match self
6218 .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, None))
6219 .await
6220 {
6221 Ok(data) => data,
6222 Err(error) if unresolvable_node(&error) => {
6223 for id in batch {
6224 read.push(self.item_by_id(id).await?);
6225 }
6226 continue;
6227 }
6228 Err(error) => return Err(error),
6229 };
6230 for (slot, id) in batch.iter().enumerate() {
6231 let node =
6232 data.get(format!("i{slot}"))
6233 .ok_or_else(|| SourceError::Malformed {
6234 message: format!(
6235 "GitHub answered a batch read with no item for {}",
6236 id.0
6237 ),
6238 })?;
6239 read.push(if node.is_null() {
6240 None
6241 } else if optional_str(node, "__typename")? == Some("DraftIssue") {
6242 self.draft_by_id(id).await?
6243 } else {
6244 if optional_str(node, "__typename")? == Some("Issue")
6245 && required_str(node, "id")? != id.0
6246 {
6247 return Err(SourceError::Malformed {
6248 message: format!(
6249 "GitHub answered the read of {} with issue {}",
6250 id.0,
6251 required_str(node, "id")?
6252 ),
6253 });
6254 }
6255 self.resolve_issue(node).await?
6256 });
6257 }
6258 }
6259 }
6260 let mut read = read.into_iter();
6261 Ok(found
6262 .iter_mut()
6263 .map(|slot| slot.take().unwrap_or_else(|| read.next().flatten()))
6264 .collect())
6265 }
6266
6267 fn read_ahead(
6268 &self,
6269 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Option<Resolved>>>, SourceError> {
6270 self.read_ahead
6271 .lock()
6272 .map_err(|_| SourceError::Unavailable {
6273 message:
6274 "items read ahead for a copy were left inconsistent; run the command again"
6275 .into(),
6276 })
6277 }
6278
6279 fn resolved_cache(
6280 &self,
6281 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
6282 self.resolved_cache
6283 .lock()
6284 .map_err(|_| SourceError::Unavailable {
6285 message: "resolved item records were left inconsistent; run the command again"
6286 .into(),
6287 })
6288 }
6289
6290 /// Reuse a record this invocation already resolved. The mutation sender invalidates
6291 /// it before writing, so a partial failure cannot leave a pre-write binding behind.
6292 async fn bound_item(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6293 let cached = self.resolved_cache()?.get(id).cloned();
6294 if let Some(item) = cached {
6295 return Ok(Some(item));
6296 }
6297 self.item_by_id(id).await
6298 }
6299
6300 /// Take out the item an issue read by reference reached for `id`, when one is held: the
6301 /// status write it was read for is the one caller, so no other read or write is ever
6302 /// answered from a read that selected only what that write needs.
6303 fn take_held(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6304 Ok(self
6305 .held_cache
6306 .lock()
6307 .map_err(|_| SourceError::Unavailable {
6308 message: "items read by issue reference were left inconsistent; run the \
6309 command again"
6310 .into(),
6311 })?
6312 .remove(id))
6313 }
6314
6315 /// One board draft by its own id, with the board item it sits in — or `None` when no
6316 /// item of this board is that draft's.
6317 ///
6318 /// The same decision [`Self::resolve_issue`] makes for an issue, over the draft's own
6319 /// `projectV2Items`: an entry naming this board is what makes it this board's. GitHub
6320 /// links a draft to one board item, so the page this read carries is the whole of that
6321 /// connection, and a page that reports more than it holds is refused rather than read
6322 /// as an answer about memberships nobody read.
6323 async fn draft_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6324 let data = self
6325 .graphql(
6326 graphql::DRAFT,
6327 json!({"id":id.0,"nestedFirst":NESTED_PAGE_SIZE,
6328 "boardItems":BOARD_ITEMS_PAGE_SIZE}),
6329 )
6330 .await?;
6331 // Gone between the two reads is an answer — the draft is no longer there. Anything
6332 // else than the draft [`Self::reach`] was just told this id is, is not one.
6333 let Some(draft) = data.get("node").filter(|node| !node.is_null()) else {
6334 return Ok(None);
6335 };
6336 if optional_str(draft, "__typename")? != Some("DraftIssue") {
6337 return Err(SourceError::Malformed {
6338 message: format!(
6339 "GitHub answered {} as a draft and then as something else",
6340 id.0
6341 ),
6342 });
6343 }
6344 if required_str(draft, "id")? != id.0 {
6345 return Err(SourceError::Malformed {
6346 message: format!("GitHub answered a different draft for {}", id.0),
6347 });
6348 }
6349 let memberships = draft
6350 .get("projectV2Items")
6351 .ok_or_else(|| SourceError::Malformed {
6352 message: format!("GitHub draft {} is missing projectV2Items", id.0),
6353 })?;
6354 let nodes = memberships
6355 .get("nodes")
6356 .and_then(Value::as_array)
6357 .ok_or_else(|| SourceError::Malformed {
6358 message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
6359 })?;
6360 let info = memberships
6361 .get("pageInfo")
6362 .ok_or_else(|| SourceError::Malformed {
6363 message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
6364 })?;
6365 // Read whether or not this board's entry is on the page: a page claiming more than
6366 // the one item GitHub links a draft to is a malformed answer either way.
6367 if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
6368 return Err(SourceError::Malformed {
6369 message: format!(
6370 "GitHub draft {} reports more board items than the one GitHub links a draft \
6371 to",
6372 id.0
6373 ),
6374 });
6375 }
6376 if let Some(node) = nodes.first()
6377 && node
6378 .pointer("/project/number")
6379 .and_then(Value::as_u64)
6380 .is_none()
6381 {
6382 return Err(SourceError::Malformed {
6383 message: format!(
6384 "GitHub draft {} board item has no numeric project number",
6385 id.0
6386 ),
6387 });
6388 }
6389 let Some(held) = self.board_entry(nodes) else {
6390 return Ok(None);
6391 };
6392 if required_str(
6393 held.get("project").ok_or_else(|| SourceError::Malformed {
6394 message: format!("GitHub draft {} board item has no project", id.0),
6395 })?,
6396 "id",
6397 )? != self.board_fields().await?.id.as_str()
6398 {
6399 return Ok(None);
6400 }
6401 let item = json!({
6402 "id": required_str(held, "id")?,
6403 "project": held.get("project"),
6404 "fieldValues": held.get("fieldValues"),
6405 "content": draft,
6406 });
6407 self.resolve(&item)
6408 }
6409
6410 /// The board's own id and field definitions, for a write whose item does not carry
6411 /// them — never its items.
6412 ///
6413 /// A board this command has already listed supplies them, since it read them beside its
6414 /// items; otherwise they come from [`graphql::BOARD_FIELDS`], once per command. Neither
6415 /// is consulted about which items the board holds: see the module documentation for
6416 /// why a question about one known item is answered by reading that item.
6417 async fn board_fields(&self) -> Result<BoardFields, SourceError> {
6418 if let Some(board) = self.board_cache()?.as_ref() {
6419 return Ok(BoardFields {
6420 id: BoardId::parse(&board.id)?,
6421 fields: board.fields.clone(),
6422 });
6423 }
6424 if let Some(held) = self.fields_cache()?.clone() {
6425 return Ok(held);
6426 }
6427 let data = self
6428 .graphql(
6429 graphql::BOARD_FIELDS,
6430 json!({"owner":self.owner,"number":self.project_number,
6431 "nestedFirst":NESTED_PAGE_SIZE}),
6432 )
6433 .await?;
6434 self.fields_read(&data)
6435 }
6436
6437 /// The board's id and fields out of an answer carrying the `boardFields` root, held for
6438 /// the rest of this command.
6439 fn fields_read(&self, data: &Value) -> Result<BoardFields, SourceError> {
6440 let board = data
6441 .pointer("/boardFields/projectV2")
6442 .filter(|value| !value.is_null())
6443 .ok_or_else(|| SourceError::Refused {
6444 message: format!(
6445 "GitHub project {}/{} was not found or is not visible to the token",
6446 self.owner, self.project_number
6447 ),
6448 })?;
6449 let read = BoardFields {
6450 id: BoardId::parse(required_str(board, "id")?)?,
6451 fields: board.get("fields").cloned().unwrap_or(Value::Null),
6452 };
6453 *self.fields_cache()? = Some(read.clone());
6454 Ok(read)
6455 }
6456
6457 /// Read what creating an issue in `repository` needs and this command has not read yet —
6458 /// the board's fields and the repository's node id — in one request when it needs both.
6459 ///
6460 /// When either is already known this sends nothing, and the other is read by its own
6461 /// document where it is asked for, so no create reads anything twice.
6462 async fn creation_context(
6463 &self,
6464 repository: &RepositoryTarget,
6465 incoming: &Incoming<'_>,
6466 ) -> Result<(), SourceError> {
6467 let fields_known = self.board_cache()?.is_some() || self.fields_cache()?.is_some();
6468 if fields_known || self.repository_cache()?.contains_key(repository) {
6469 return Ok(());
6470 }
6471 // A placement an earlier process kept is taken only while the project issue this create
6472 // has just read shows its setup-context token unchanged; see `projection`.
6473 if self.placed_by_kept(repository, incoming.parent) {
6474 return Ok(());
6475 }
6476 let data = self
6477 .graphql(
6478 graphql::CREATION_CONTEXT,
6479 json!({"owner":self.owner,"number":self.project_number,
6480 "nestedFirst":NESTED_PAGE_SIZE,"repositoryOwner":repository.owner,
6481 "repositoryName":repository.name}),
6482 )
6483 .await?;
6484 self.fields_read(&data)?;
6485 self.repository_read(&data, repository, incoming)?;
6486 Ok(())
6487 }
6488
6489 /// This process's own view of the board's fields, or the refusal a poisoned lock is.
6490 fn fields_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<BoardFields>>, SourceError> {
6491 self.fields_cache
6492 .lock()
6493 .map_err(|_| SourceError::Unavailable {
6494 message: "this source's view of the board's fields was left inconsistent by an \
6495 earlier failure; next: run the command again"
6496 .into(),
6497 })
6498 }
6499
6500 /// What a write to `item` needs of the board, read off that item when it says enough and
6501 /// off [`Self::board_fields`] when it does not.
6502 ///
6503 /// A node read of an item names its board and carries the definition of every field it
6504 /// holds a value of — so an item naming its board, holding a value of the origin field
6505 /// when the write carries an origin, and, when the write carries a status, holding a
6506 /// `Status` value, needs no read of the board at all. **Nothing the item does not say is
6507 /// guessed:** a field it holds no value of may still be on the board, and a view reading it
6508 /// as absent would refuse a write the board can take or skip a field write the board
6509 /// needs, so such an item — and a create, which has no item yet — takes the board's fields
6510 /// from their own read instead. A write carrying no origin to an item holding none — every
6511 /// write out of a source declared private — writes no origin whatever the board defines,
6512 /// so it needs no definition of that field.
6513 async fn fields_for(
6514 &self,
6515 item: Option<&Resolved>,
6516 writes_origin: bool,
6517 writes_status: bool,
6518 selects_priority: bool,
6519 ) -> Result<BoardFields, SourceError> {
6520 if let Some(board) = item.and_then(Resolved::carried_board) {
6521 return Ok(board);
6522 }
6523 if let Some(item) = item
6524 && let Some(board_id) = item.named_board()
6525 && (!writes_origin || item.defines(ORIGIN_FIELD))
6526 && (!writes_status || item.defines("Status"))
6527 && (!selects_priority || item.defines(PRIORITY_FIELD))
6528 && self
6529 .metadata_fields
6530 .iter()
6531 .all(|projection| item.defines(&projection.field))
6532 {
6533 return Ok(BoardFields {
6534 id: board_id,
6535 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
6536 });
6537 }
6538 self.board_fields().await
6539 }
6540
6541 /// Everything filed under one issue of this board, walked to exhaustion — or `None`
6542 /// when that id names nothing here with a sub-issue relationship to walk.
6543 ///
6544 /// `None` and an empty answer are different: `None` is *this is not an issue of this
6545 /// GitHub*, which is what sends a project selector on to be read as a name, and an
6546 /// empty vector is a project that holds nothing.
6547 async fn sub_issues(&self, id: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
6548 let mut after: Option<String> = None;
6549 let mut children = Vec::new();
6550 // The project issue as the first page carried it, held once the walk has ended.
6551 let mut parent: Option<Resolved> = None;
6552 loop {
6553 let asked = self
6554 .graphql(
6555 graphql::SUB_ISSUES,
6556 json!({"id":id.0,"memberFirst":MEMBER_PAGE_SIZE,"after":after,
6557 "nestedFirst":NESTED_PAGE_SIZE,
6558 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
6559 )
6560 .await;
6561 let data = match asked {
6562 Ok(data) => data,
6563 // A string that is not a node id at all is not a failure to report: it is
6564 // the ordinary answer to a selector naming a project by its name.
6565 Err(error) if unresolvable_node(&error) => return Ok(None),
6566 Err(error) => return Err(error),
6567 };
6568 let Some(connection) = data
6569 .pointer("/node/subIssues")
6570 .filter(|value| !value.is_null())
6571 else {
6572 // No such node, or one with no sub-issue relationship — a board draft is
6573 // the one this board can really hold.
6574 return Ok(None);
6575 };
6576 if after.is_none()
6577 && let Some(node) = data.get("node").filter(|node| node.get("id").is_some())
6578 {
6579 parent = self
6580 .resolve_issue(node)
6581 .await?
6582 .filter(|resolved| resolved.id == *id);
6583 }
6584 for node in connection
6585 .get("nodes")
6586 .and_then(Value::as_array)
6587 .ok_or_else(|| SourceError::Malformed {
6588 message: "GitHub subIssues.nodes is not an array".into(),
6589 })?
6590 {
6591 if let Some(resolved) = self.resolve_issue(node).await? {
6592 children.push(resolved);
6593 }
6594 }
6595 let info = connection
6596 .get("pageInfo")
6597 .ok_or_else(|| SourceError::Malformed {
6598 message: "GitHub subIssues connection has no pageInfo".into(),
6599 })?;
6600 let next = required_bool(info, "hasNextPage")?
6601 .then(|| required_str(info, "endCursor"))
6602 .transpose()?;
6603 match next {
6604 Some(next) => {
6605 validate_cursor_progress(after.as_deref(), next)?;
6606 after = Some(next.to_owned());
6607 }
6608 None => {
6609 // Held only once the walk has ended, so one failing part way holds
6610 // nothing and every read it would have answered is made.
6611 // The project issue the first page carried answers the next read of the
6612 // project by its id in this command, as a read of it made now would: what
6613 // classifying a project for a write reads straight after its members.
6614 if let Some(parent) = &parent {
6615 self.read_ahead()?
6616 .entry(parent.id.clone())
6617 .or_insert_with(|| Some(parent.clone()));
6618 }
6619 let mut carried = self.carried()?;
6620 for item in parent.into_iter().chain(children.iter().cloned()) {
6621 carried.insert(item.id.clone(), item);
6622 }
6623 return Ok(Some(children));
6624 }
6625 }
6626 }
6627 }
6628
6629 fn carried(
6630 &self,
6631 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
6632 self.carried.lock().map_err(|_| SourceError::Unavailable {
6633 message: "items a project's listing carried were left inconsistent; run the \
6634 command again"
6635 .into(),
6636 })
6637 }
6638
6639 /// Which issue of this board a project *name* is, or `None` when none is.
6640 ///
6641 /// One bounded query which filters on that name at the server, rather than a walk of
6642 /// every issue the board holds. The name is compared again here: the qualifier narrows
6643 /// what GitHub sends, and this source decides what it names.
6644 async fn project_by_name(&self, name: &str) -> Result<Option<NativeId>, SourceError> {
6645 let search = self.board_search(Some(&title_qualifier(name)));
6646 let mut after = None;
6647 loop {
6648 let (candidates, next) = self
6649 .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
6650 .await?;
6651 if let Some(item) = candidates.into_iter().find(|item| {
6652 item.kind == BoardKind::Work(ItemKind::Project)
6653 && item.title.eq_ignore_ascii_case(name)
6654 }) {
6655 return Ok(Some(item.id));
6656 }
6657 match next {
6658 Some(next) => after = Some(next),
6659 None => return Ok(None),
6660 }
6661 }
6662 }
6663
6664 /// Everything filed under one project of this board: the sub-issues of the issue that
6665 /// project is.
6666 ///
6667 /// Tasks *and* documents, because a document filed under a project is a sub-issue of it
6668 /// too — the caller keeps the kind it asked for. Nothing about this grows as the board
6669 /// gains projects, or as another project gains tasks.
6670 ///
6671 /// A qualified id names the issue and is asked for its sub-issues directly: one
6672 /// request, no search of any kind. Only a selector GitHub cannot resolve that way is
6673 /// read as a project *name*, which costs the one bounded search
6674 /// [`Self::project_by_name`] makes.
6675 ///
6676 /// What GitHub answered is held for the rest of the command — see [`Self::children_cache`]
6677 /// — and each answer is still cut to the items whose parent is this project, so one this
6678 /// process has since filed elsewhere is not reported here, and completed with what this
6679 /// process filed under it.
6680 async fn project_children(&self, selector: &NativeId) -> Result<Vec<Resolved>, SourceError> {
6681 let held = self.children_cache()?.get(selector).cloned();
6682 let (project, children) = match held {
6683 Some(held) => held,
6684 None => {
6685 let answered = match self.sub_issues(selector).await? {
6686 Some(children) => (selector.clone(), children),
6687 None => match self.project_by_name(&selector.0).await? {
6688 Some(project) => {
6689 let children = self.sub_issues(&project).await?.unwrap_or_default();
6690 (project, children)
6691 }
6692 None => return Ok(Vec::new()),
6693 },
6694 };
6695 self.children_cache()?
6696 .insert(selector.clone(), answered.clone());
6697 answered
6698 }
6699 };
6700 let mut children: Vec<Resolved> = children
6701 .into_iter()
6702 .filter(|child| child.parent.as_ref() == Some(&project))
6703 .collect();
6704 for own in self.updated()?.iter() {
6705 if own.parent.as_ref() == Some(&project) && !children.iter().any(|c| c.id == own.id) {
6706 children.push(own.clone());
6707 }
6708 }
6709 self.completed_with_written(children, |own| own.parent.as_ref() == Some(&project))
6710 }
6711
6712 /// Every issue of this board GitHub's issue search reports updated at or after `since`,
6713 /// completed with what this run wrote — the candidates a comment-activity read confirms.
6714 ///
6715 /// Scoped by the board and by nothing else: `project:<owner>/<number>` reaches every issue
6716 /// on the board whatever repository, and whatever owner, it lives in, so no repository or
6717 /// owner qualifier is added and none is needed. What makes the `updated:` qualifier
6718 /// sufficient is a fact about GitHub rather than about this source: a comment written on an
6719 /// issue **and a comment edited on it** both move that issue's `updatedAt`. The credentialed
6720 /// journey `an_edited_comment_moves_its_issue_and_is_selected_since` in `tests/journey`
6721 /// re-takes that fact on every run of the lane, so a change on GitHub's side fails there
6722 /// rather than silently narrowing a caller's answer.
6723 ///
6724 /// The instant is written to the second, rounded down, which can only widen what the
6725 /// search returns; confirmation against each candidate's own comments is what makes the
6726 /// answer exact. The search is an index that lags a write by a second or two — the module
6727 /// documentation records it — so a caller that asks again from its last instant should
6728 /// overlap the two by more than that.
6729 async fn updated_since(&self, since: DateTime<Utc>) -> Result<Vec<Resolved>, SourceError> {
6730 let found = self.searched(&updated_qualifier(since)).await?;
6731 self.completed_with_written(found, |_| true)
6732 }
6733
6734 /// Every issue of this board GitHub's issue search reports for the board-scoped search
6735 /// narrowed by `also`, in pages of [`SEARCH_PAGE_SIZE`].
6736 ///
6737 /// Uncompleted: what this process wrote is added by the caller, which knows whether its
6738 /// own record is the fresher of the two.
6739 async fn searched(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
6740 let search = self.board_search(Some(also));
6741 let mut after: Option<String> = None;
6742 let mut found = Vec::new();
6743 loop {
6744 let (page, next) = self
6745 .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
6746 .await?;
6747 found.extend(page);
6748 match next {
6749 Some(next) => after = Some(next),
6750 None => return Ok(found),
6751 }
6752 }
6753 }
6754
6755 /// A bounded task answer; the versioned cursor carries the connection position, how
6756 /// many rows of the page starting there were already handed out, and the own-write ids
6757 /// already observed, including across a new source instance.
6758 ///
6759 /// Every page is sent at [`SEARCH_PAGE_SIZE`] whatever the caller's limit, and a limit is
6760 /// sliced from the pages it needs; why is the module documentation's paging contract.
6761 async fn search_tasks(
6762 &self,
6763 query: &TaskQuery,
6764 page: &PageRequest,
6765 also: &str,
6766 ) -> Result<Page<Task>, SourceError> {
6767 let mut position = match &page.cursor {
6768 None => SearchPosition::default(),
6769 Some(cursor) => serde_json::from_str::<SearchPosition>(&cursor.0)
6770 .ok()
6771 .filter(|position| {
6772 position.version == SEARCH_CURSOR_VERSION
6773 && position.connection.valid_resume(position.offset)
6774 })
6775 .ok_or_else(|| SourceError::Config {
6776 message: "page cursor is invalid".into(),
6777 })?,
6778 };
6779 let search = self.board_search(Some(also));
6780 let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
6781 let own = self.with_own_writes(Vec::new())?;
6782 // An issue this process commented on is a candidate of a comment-activity read
6783 // whether or not the search has caught up with the comment; see `Self::commented`.
6784 let commented = match query.commented_since {
6785 Some(_) => self.commented()?.clone(),
6786 None => Vec::new(),
6787 };
6788 for id in own.iter().map(|item| &item.id).chain(&commented) {
6789 if !position.own.contains(id) {
6790 position.own.push(id.clone());
6791 }
6792 }
6793 let mut tasks = Vec::new();
6794 while !position.connection.exhausted() && tasks.len() < limit {
6795 let first = SEARCH_PAGE_SIZE;
6796 // Page size is part of the key: a short cached answer cannot answer a wider ask.
6797 let key =
6798 serde_json::to_string(&("page", &search, &position.connection.after(), first))
6799 .expect("search page key is serializable");
6800 let cached = if query.commented_since.is_none() {
6801 self.narrowed_cache()?.get(&key).cloned()
6802 } else {
6803 None
6804 };
6805 let (found, next) = match cached {
6806 Some(found) => {
6807 let next = self
6808 .search_next
6809 .lock()
6810 .map_err(|_| SourceError::Unavailable {
6811 message:
6812 "search pagination was left inconsistent; run the command again"
6813 .into(),
6814 })?
6815 .get(&key)
6816 .cloned()
6817 .flatten();
6818 (found, next)
6819 }
6820 None => {
6821 let (found, next) = self
6822 .search_page(&search, first, position.connection.after())
6823 .await?;
6824 if query.commented_since.is_none() {
6825 self.search_next
6826 .lock()
6827 .map_err(|_| SourceError::Unavailable {
6828 message:
6829 "search pagination was left inconsistent; run the command again"
6830 .into(),
6831 })?
6832 .insert(key.clone(), next.clone());
6833 self.narrowed_cache()?.insert(key, found.clone());
6834 }
6835 (found, next)
6836 }
6837 };
6838 let rows = found.len();
6839 for mut item in found.into_iter().skip(position.offset) {
6840 if tasks.len() == limit {
6841 break;
6842 }
6843 position.offset += 1;
6844 if position.own.contains(&item.id) {
6845 if position.seen.contains(&item.id) {
6846 continue;
6847 }
6848 position.seen.push(item.id.clone());
6849 // The search's own copy of an issue this process only commented on is as
6850 // good as a node read of it, since its comments are read either way.
6851 let only_commented = commented.contains(&item.id)
6852 && !own.iter().any(|written| written.id == item.id);
6853 if !only_commented {
6854 let updated_at = item.updated_at;
6855 let Some(written) = self.search_written(&own, &item.id).await? else {
6856 continue;
6857 };
6858 item = written;
6859 item.updated_at = item.updated_at.max(updated_at);
6860 self.resolved_cache()?.insert(item.id.clone(), item.clone());
6861 }
6862 }
6863 if item.kind == BoardKind::Work(ItemKind::Task) {
6864 let task = item.task()?;
6865 if task_matches(&task, query, &query.project)
6866 && self.commented_since(&item, query.commented_since).await?
6867 {
6868 tasks.push(task);
6869 }
6870 }
6871 }
6872 if position.offset < rows {
6873 continue;
6874 }
6875 position.offset = 0;
6876 position.connection = match next {
6877 Some(after) => SearchConnection::Continuing {
6878 after: Cursor(after),
6879 },
6880 None => SearchConnection::Exhausted {},
6881 };
6882 }
6883 if position.connection.exhausted() {
6884 for id in position.own.clone() {
6885 if position.seen.contains(&id) {
6886 continue;
6887 }
6888 if tasks.len() == limit {
6889 break;
6890 }
6891 position.seen.push(id.clone());
6892 let Some(item) = self.search_written(&own, &id).await? else {
6893 continue;
6894 };
6895 if item.kind == BoardKind::Work(ItemKind::Task) {
6896 let task = item.task()?;
6897 if task_matches(&task, query, &query.project)
6898 && self.commented_since(&item, query.commented_since).await?
6899 {
6900 tasks.push(task);
6901 }
6902 }
6903 }
6904 }
6905 let more = !position.connection.exhausted()
6906 || position.own.iter().any(|id| !position.seen.contains(id));
6907 Ok(Page {
6908 items: tasks,
6909 next: more.then(|| {
6910 Cursor(serde_json::to_string(&position).expect("search position is serializable"))
6911 }),
6912 })
6913 }
6914
6915 /// A resumed process has the ids but no write records; resolve only a record the
6916 /// current page needs, by its uncached node read rather than the lagging search index.
6917 async fn search_written(
6918 &self,
6919 own: &[Resolved],
6920 id: &NativeId,
6921 ) -> Result<Option<Resolved>, SourceError> {
6922 match own.iter().find(|item| item.id == *id) {
6923 Some(item) => Ok(Some(item.clone())),
6924 None => self.item_by_id(id).await,
6925 }
6926 }
6927
6928 /// The candidates for a task query carrying a text, metadata or origin predicate, read
6929 /// without enumerating the board — or `None` for a query carrying none of the three, which
6930 /// keeps the reads it always had.
6931 ///
6932 /// An origin is answered by [`Self::origin_candidates`], whatever else the query carries,
6933 /// because it names at most a handful of items. Text and metadata are answered by one
6934 /// board-scoped issue search carrying every term — see [`narrowing_qualifiers`] — narrowed
6935 /// further by `updated:>=` when the query also asks for comment activity, since both
6936 /// qualifiers must hold of an issue the answer keeps. Every candidate is confirmed in
6937 /// process afterwards by the same predicates [`task_matches`] applies to every read.
6938 ///
6939 /// Completed with what this process wrote, its own record winning over the index's copy
6940 /// of the same item: see [`Self::with_own_writes`].
6941 async fn narrowed(&self, query: &TaskQuery) -> Result<Option<Vec<Resolved>>, SourceError> {
6942 self.send_pending_origins(None).await?;
6943 let asked = match (&query.origin, narrowing_qualifiers(query)) {
6944 (Some(origin), _) => Narrowing::Origin(origin.clone()),
6945 (None, Some(qualifiers)) => Narrowing::Search(match query.commented_since {
6946 Some(since) => format!("{} {qualifiers}", updated_qualifier(since)),
6947 None => qualifiers,
6948 }),
6949 (None, None) => return Ok(None),
6950 };
6951 // A question about comment activity is asked afresh every time, as it always was: it
6952 // is the one a caller polls from one source while waiting for the index, and an
6953 // answer held from the first poll would be the answer to every later one.
6954 let key = query.commented_since.is_none().then(|| asked.key());
6955 let cached = match &key {
6956 Some(key) => self.narrowed_cache()?.get(key).cloned(),
6957 None => None,
6958 };
6959 let found = match cached {
6960 Some(found) => found,
6961 None => {
6962 let found = match &asked {
6963 Narrowing::Origin(origin) => self.origin_candidates(origin).await?,
6964 Narrowing::Search(also) => self.searched(also).await?,
6965 };
6966 if let Some(key) = key {
6967 self.narrowed_cache()?.insert(key, found.clone());
6968 }
6969 found
6970 }
6971 };
6972 self.with_own_writes(found).map(Some)
6973 }
6974
6975 /// The candidates for a project or unscoped document query carrying a searchable text,
6976 /// read without enumerating the board — or `None` for a query with no text or a blank one,
6977 /// which keeps the read it always had.
6978 ///
6979 /// The text is sent as the very phrase a task query's text is — see [`text_qualifiers`] —
6980 /// in one board-scoped issue search walked to its end at [`SEARCH_PAGE_SIZE`], so what it
6981 /// costs is the issues that match and never the board. Its answer is held for the command
6982 /// under the same key [`Self::narrowed`] holds that search under, so a walk of the caller's
6983 /// pages asks GitHub once. Every candidate is confirmed afterwards by its kind and by the
6984 /// substring rule, exactly as an item of the wider read was, and is completed with what this
6985 /// process wrote: see [`Self::with_own_writes`].
6986 async fn text_searched(
6987 &self,
6988 text: Option<&TextQuery>,
6989 ) -> Result<Option<Vec<Resolved>>, SourceError> {
6990 let Some(also) = text_qualifiers(text) else {
6991 return Ok(None);
6992 };
6993 let key = Narrowing::Search(also.clone()).key();
6994 let cached = self.narrowed_cache()?.get(&key).cloned();
6995 let found = match cached {
6996 Some(found) => found,
6997 None => {
6998 let found = self.searched(&also).await?;
6999 self.narrowed_cache()?.insert(key, found.clone());
7000 found
7001 }
7002 };
7003 self.with_own_writes(found).map(Some)
7004 }
7005
7006 /// Every `kind` item of this board whose own origin is exactly `origin`: the candidates
7007 /// [`Self::narrowed`] reads for a task query carrying that origin — one
7008 /// [`graphql::ORIGIN_LOOKUP`] walk, held for the command under the same key, completed with
7009 /// what this process wrote — each confirmed against its own origin entry.
7010 ///
7011 /// What answers a project or document listing by origin, so finding every copy of one plan
7012 /// costs the items that carry its origin and never a page of the board.
7013 async fn copied_from(
7014 &self,
7015 origin: &str,
7016 kind: BoardKind,
7017 ) -> Result<Vec<Resolved>, SourceError> {
7018 let wanted = TaskQuery {
7019 origin: Some(origin.to_owned()),
7020 ..TaskQuery::default()
7021 };
7022 Ok(self
7023 .narrowed(&wanted)
7024 .await?
7025 .unwrap_or_default()
7026 .into_iter()
7027 .filter(|item| item.kind == kind && wanted.origin_matches(&item.metadata()))
7028 .collect())
7029 }
7030
7031 /// Every item of this board that may carry `origin` — a superset of those that do — found
7032 /// by [`graphql::ORIGIN_LOOKUP`] and never by enumerating the board.
7033 ///
7034 /// The union of the board's own field filter over the `onetaskgraph.origin` text field —
7035 /// which reads the field every carrier holds, whichever release wrote it — and the
7036 /// board-scoped issue search for the same id as a phrase in the body, where this source
7037 /// mirrors it. The caller adds the third read, this process's own writes. Candidates are
7038 /// returned unconfirmed; [`task_matches`] compares each one's own origin field with the
7039 /// query's, exactly.
7040 ///
7041 /// Both connections are walked to exhaustion, each from its own cursor. One that has
7042 /// already ended is sent its last cursor again, which answers an empty page, so the one
7043 /// document serves every page of either. What the two leave is stated in the module
7044 /// documentation: a carrier another process added within the last second or two, before
7045 /// either index has it.
7046 async fn origin_candidates(&self, origin: &str) -> Result<Vec<Resolved>, SourceError> {
7047 let filter = format!("{ORIGIN_FIELD}:{}", quoted(origin));
7048 let search = self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))));
7049 let mut items_after: Option<String> = None;
7050 let mut search_after: Option<String> = None;
7051 let mut found: Vec<Resolved> = Vec::new();
7052 let keep = |resolved: Resolved, found: &mut Vec<Resolved>| {
7053 if !found.iter().any(|held| held.id == resolved.id) {
7054 found.push(resolved);
7055 }
7056 };
7057 loop {
7058 let data = self
7059 .graphql(
7060 graphql::ORIGIN_LOOKUP,
7061 json!({"owner":self.owner,"number":self.project_number,"filter":filter,
7062 "search":search,"type":"ISSUE","originFirst":ORIGIN_PAGE_SIZE,
7063 "itemsAfter":items_after,"searchAfter":search_after,
7064 "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
7065 "duplicates":true}),
7066 )
7067 .await?;
7068 let items = data
7069 .pointer("/originItems/projectV2/items")
7070 .filter(|value| !value.is_null())
7071 .ok_or_else(|| SourceError::Refused {
7072 message: format!(
7073 "GitHub project {}/{} was not found or is not visible to the token",
7074 self.owner, self.project_number
7075 ),
7076 })?;
7077 for item in optional_nodes(Some(items), "project items")?
7078 .into_iter()
7079 .flatten()
7080 {
7081 // The board's own items list its drafts too, and a draft is not an issue: no
7082 // narrowed read answers with one, whatever its origin field holds.
7083 if let Some(resolved) = self.resolve(item)?
7084 && resolved.content_kind == ContentKind::Issue
7085 {
7086 keep(resolved, &mut found);
7087 }
7088 }
7089 let searched = data.get("search").ok_or_else(|| SourceError::Malformed {
7090 message: "GitHub search response has no search connection".into(),
7091 })?;
7092 for node in optional_nodes(Some(searched), "search")?
7093 .into_iter()
7094 .flatten()
7095 {
7096 if let Some(resolved) = self.resolve_issue(node).await? {
7097 keep(resolved, &mut found);
7098 }
7099 }
7100 let items_next = resumed(items, items_after.as_deref())?;
7101 let search_next = resumed(searched, search_after.as_deref())?;
7102 if !items_next.has_more() && !search_next.has_more() {
7103 return Ok(found);
7104 }
7105 items_after = items_next.cursor();
7106 search_after = search_next.cursor();
7107 }
7108 }
7109
7110 /// `found`, with every item this process created or wrote in its place, and every one of
7111 /// them the read did not report added.
7112 ///
7113 /// This process's own record wins over the read's copy of the same item, because a read
7114 /// of an item written moments ago can still be behind what was written onto it — the
7115 /// origin field included, which is the one a narrowed read is confirmed against — and a
7116 /// read that still names an item under a predicate this process's write moved it out of
7117 /// must not return it. The one thing the read knows that the record cannot is when GitHub
7118 /// last saw the item change, which is what a comment-activity read rules a candidate out
7119 /// by, so the read's `updatedAt` is kept when the record has none of its own. See
7120 /// [`Self::created`] and [`Self::updated`](GitHubProjectsSource::updated).
7121 fn with_own_writes(&self, mut found: Vec<Resolved>) -> Result<Vec<Resolved>, SourceError> {
7122 // A board draft is not an issue, so no narrowed read returns one, and this process
7123 // having written one does not make it an answer either.
7124 let own: Vec<Resolved> = self
7125 .created()?
7126 .iter()
7127 .chain(self.updated()?.iter())
7128 .filter(|own| own.content_kind == ContentKind::Issue)
7129 .cloned()
7130 .collect();
7131 for mut own in own {
7132 self.resolved_cache()?.insert(own.id.clone(), own.clone());
7133 match found.iter_mut().find(|read| read.id == own.id) {
7134 Some(read) => {
7135 own.updated_at = own.updated_at.max(read.updated_at);
7136 *read = own;
7137 }
7138 None => found.push(own),
7139 }
7140 }
7141 Ok(found)
7142 }
7143
7144 /// Whether `item` has a comment created or last edited at or after `since` — always, when
7145 /// there is no instant to hold it to.
7146 ///
7147 /// The candidate's own `updatedAt` is read first, because a comment written or edited at
7148 /// or after the instant moved it there: an issue not updated since holds no such comment,
7149 /// and its comments are never asked for — unless this process commented on it in this
7150 /// command, when the `updatedAt` held may predate that comment; see [`Self::commented`]. Otherwise its comments are walked, oldest first,
7151 /// only as far as the first that matches. A board draft is not an issue and has no
7152 /// comments, so it never matches.
7153 async fn commented_since(
7154 &self,
7155 item: &Resolved,
7156 since: Option<DateTime<Utc>>,
7157 ) -> Result<bool, SourceError> {
7158 let Some(since) = since else {
7159 return Ok(true);
7160 };
7161 if item.content_kind == ContentKind::DraftIssue {
7162 return Ok(false);
7163 }
7164 // An `updatedAt` this process's own record or a lagging index holds can predate a
7165 // comment this process wrote since, so only an issue it did not comment on is ruled
7166 // out by one.
7167 if item.updated_at.is_some_and(|updated| updated < since)
7168 && !self.commented()?.contains(&item.id)
7169 {
7170 return Ok(false);
7171 }
7172 let query = TaskQuery {
7173 commented_since: Some(since),
7174 ..TaskQuery::default()
7175 };
7176 let mut after: Option<String> = None;
7177 loop {
7178 let data = self
7179 .graphql(
7180 graphql::ISSUE_COMMENTS,
7181 json!({"id":item.id.0,"first":MAX_PAGE_SIZE,"after":after}),
7182 )
7183 .await?;
7184 let Some(connection) = data
7185 .get("node")
7186 .filter(|value| !value.is_null())
7187 .and_then(|node| node.get("comments"))
7188 .filter(|value| !value.is_null())
7189 else {
7190 // Removed since the search reported it: no longer an issue with comments.
7191 return Ok(false);
7192 };
7193 let comments = optional_nodes(Some(connection), "issue comments")?
7194 .into_iter()
7195 .flatten()
7196 .map(comment_from)
7197 .collect::<Result<Vec<_>, _>>()?;
7198 if query.comments_match(&comments) {
7199 return Ok(true);
7200 }
7201 match next_cursor(connection)? {
7202 Some(next) => {
7203 validate_cursor_progress(after.as_deref(), &next.0)?;
7204 after = Some(next.0);
7205 }
7206 None => return Ok(false),
7207 }
7208 }
7209 }
7210
7211 /// Every item on the board: the union of both enumerations GitHub offers of one.
7212 ///
7213 /// Neither contains the other, so neither is dropped — only `ProjectV2.items` lists a
7214 /// board **draft** and reads the board's own fields beside its items, and only the search
7215 /// reports an item that connection is behind on. The module documentation is where the lag and the
7216 /// measurements behind it are written down.
7217 ///
7218 /// A search result is admitted on the same terms as any other issue this source reaches
7219 /// directly — [`Self::resolve_issue`] keeps it only if that issue's own `projectItems`
7220 /// names *this* board — so an issue the index still believes is here after it was taken
7221 /// off is refused rather than reported.
7222 ///
7223 /// See [`Self::board_cache`]. Both completions happen on every call rather than once,
7224 /// which is what the cache could otherwise have broken.
7225 async fn board(&self) -> Result<Board, SourceError> {
7226 self.board_completed_by(None).await
7227 }
7228
7229 /// [`Self::board`], its search half narrowed by `also` when it is given — which an issue
7230 /// that half could add is then answered only if it holds.
7231 async fn board_completed_by(&self, also: Option<&str>) -> Result<Board, SourceError> {
7232 let cached = self.board_cache()?.clone();
7233 let mut board = match cached {
7234 Some(board) => board,
7235 None => {
7236 let read = self.read_board().await?;
7237 *self.board_cache()? = Some(read.clone());
7238 read
7239 }
7240 };
7241 let searched = match also {
7242 None => self.searched_issues().await?,
7243 Some(also) => self.searched_narrowly(also).await?,
7244 };
7245 for held in searched {
7246 if !board.items.iter().any(|item| item.id == held.id) {
7247 board.items.push(held);
7248 }
7249 }
7250 for own in self.created()?.iter() {
7251 if !board.items.iter().any(|item| item.id == own.id) {
7252 board.items.push(own.clone());
7253 }
7254 }
7255 Ok(board)
7256 }
7257
7258 /// This process's own view of the board, or the refusal a poisoned lock is.
7259 fn board_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<Board>>, SourceError> {
7260 self.board_cache
7261 .lock()
7262 .map_err(|_| SourceError::Unavailable {
7263 message: "this source's view of the board was left inconsistent by an earlier \
7264 failure; next: run the command again"
7265 .into(),
7266 })
7267 }
7268
7269 /// Bring this process's own view of the board up to an item it has just written.
7270 ///
7271 /// A created item goes to `created`, which is what completes a board read GitHub's own
7272 /// eventual consistency has left behind. An item that was already there is replaced
7273 /// where it sits, so a second write of it in the same command reads its real parent
7274 /// rather than the one it had before the first write.
7275 ///
7276 /// "Where it sits" is three places, and missing an earlier one leaves a stale record
7277 /// that wins: an item this same run created is held in `created` and not in the cached
7278 /// board, and `board` completes the cached board *from* `created`, so replacing only
7279 /// the cached copy of such an item replaces nothing and the read still reports the
7280 /// title it was created with. The search is the third, and it is the one an item the
7281 /// board's own projection is behind on sits in *alone* — which is exactly the item this
7282 /// source is least able to re-read, so leaving it out would put the stale title back on
7283 /// the only items the completion in [`Self::board`] exists for.
7284 fn remember_written(&self, item: Resolved, created: bool) -> Result<(), SourceError> {
7285 self.resolved_cache()?.insert(item.id.clone(), item.clone());
7286 if created {
7287 self.created()?.push(item);
7288 return Ok(());
7289 }
7290 {
7291 let mut own = self.created()?;
7292 if let Some(held) = own.iter_mut().find(|held| held.id == item.id) {
7293 *held = item;
7294 return Ok(());
7295 }
7296 }
7297 {
7298 let mut own = self.updated()?;
7299 match own.iter_mut().find(|held| held.id == item.id) {
7300 Some(held) => *held = item.clone(),
7301 None => own.push(item.clone()),
7302 }
7303 }
7304 if let Some(board) = self.board_cache()?.as_mut()
7305 && let Some(held) = board.items.iter_mut().find(|held| held.id == item.id)
7306 {
7307 *held = item.clone();
7308 }
7309 if let Some(found) = self.search_cache()?.as_mut()
7310 && let Some(held) = found.iter_mut().find(|held| held.id == item.id)
7311 {
7312 *held = item.clone();
7313 }
7314 for found in self.narrowed_cache()?.values_mut() {
7315 if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
7316 *held = item.clone();
7317 }
7318 }
7319 for (_, found) in self.children_cache()?.values_mut() {
7320 if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
7321 *held = item.clone();
7322 }
7323 }
7324 Ok(())
7325 }
7326
7327 /// Forget one item this process has just deleted, from every half of its own view.
7328 fn forget(&self, id: &NativeId) -> Result<(), SourceError> {
7329 if let Some(Deferred::Held { created, .. }) = self.deferred()?.as_mut() {
7330 created.retain(|held| held.content != *id);
7331 }
7332 self.resolved_cache()?.remove(id);
7333 self.read_ahead()?.remove(id);
7334 self.carried()?.remove(id);
7335 self.created()?.retain(|own| own.id != *id);
7336 self.updated()?.retain(|own| own.id != *id);
7337 self.commented()?.retain(|own| own != id);
7338 if let Some(board) = self.board_cache()?.as_mut() {
7339 board.items.retain(|item| item.id != *id);
7340 }
7341 if let Some(found) = self.search_cache()?.as_mut() {
7342 found.retain(|item| item.id != *id);
7343 }
7344 for found in self.narrowed_cache()?.values_mut() {
7345 found.retain(|item| item.id != *id);
7346 }
7347 for (_, found) in self.children_cache()?.values_mut() {
7348 found.retain(|item| item.id != *id);
7349 }
7350 Ok(())
7351 }
7352
7353 /// This process's own record of each narrowed answer, or the refusal a poisoned lock is.
7354 fn narrowed_cache(
7355 &self,
7356 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<String, Vec<Resolved>>>, SourceError> {
7357 self.narrowed_cache
7358 .lock()
7359 .map_err(|_| SourceError::Unavailable {
7360 message: "this source's view of a narrowed read was left inconsistent by an \
7361 earlier failure; next: run the command again"
7362 .into(),
7363 })
7364 }
7365
7366 /// This process's own record of each project's sub-issues, or the refusal a poisoned lock
7367 /// is.
7368 /// The tasks `project` holds, as a copy's batched read found them this command, when it did.
7369 fn held_task_children(&self, project: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
7370 Ok(self.task_children()?.get(project).cloned())
7371 }
7372
7373 fn task_children(
7374 &self,
7375 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Vec<Resolved>>>, SourceError> {
7376 self.task_children
7377 .lock()
7378 .map_err(|_| SourceError::Unavailable {
7379 message: "this source's record of a project's tasks was left inconsistent by an \
7380 earlier failure; next: run the command again"
7381 .into(),
7382 })
7383 }
7384
7385 fn children_cache(&self) -> Result<std::sync::MutexGuard<'_, ProjectChildren>, SourceError> {
7386 self.children_cache
7387 .lock()
7388 .map_err(|_| SourceError::Unavailable {
7389 message: "this source's view of a project's tasks was left inconsistent by an \
7390 earlier failure; next: run the command again"
7391 .into(),
7392 })
7393 }
7394
7395 /// Every page of the board, read from GitHub.
7396 async fn read_board(&self) -> Result<Board, SourceError> {
7397 let mut after: Option<String> = None;
7398 let mut items = Vec::new();
7399 let mut board;
7400 loop {
7401 let page = self.board_page(after.as_deref(), MAX_PAGE_SIZE).await?;
7402 for item in page
7403 .pointer("/items/nodes")
7404 .and_then(Value::as_array)
7405 .ok_or_else(|| SourceError::Malformed {
7406 message: "GitHub project items.nodes is not an array".into(),
7407 })?
7408 {
7409 if let Some(resolved) = self.resolve(item)? {
7410 items.push(resolved);
7411 }
7412 }
7413 let info = page
7414 .pointer("/items/pageInfo")
7415 .ok_or_else(|| SourceError::Malformed {
7416 message: "GitHub project items have no pageInfo".into(),
7417 })?;
7418 let has_next = required_bool(info, "hasNextPage")?;
7419 let next = has_next
7420 .then(|| required_str(info, "endCursor"))
7421 .transpose()?;
7422 board = page.clone();
7423 match next {
7424 Some(next) => {
7425 validate_cursor_progress(after.as_deref(), next)?;
7426 after = Some(next.to_owned());
7427 }
7428 None => break,
7429 }
7430 }
7431 Ok(Board {
7432 id: required_str(&board, "id")?.to_owned(),
7433 fields: board.get("fields").cloned().unwrap_or(Value::Null),
7434 items,
7435 })
7436 }
7437
7438 /// The existing items this source has written, for completing a narrowed read that is
7439 /// behind; see [`Self::updated`](GitHubProjectsSource::updated).
7440 fn updated(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
7441 self.updated.lock().map_err(|_| SourceError::Unavailable {
7442 message: "this source's record of what it wrote in this run was left inconsistent \
7443 by an earlier failure; next: run the command again"
7444 .into(),
7445 })
7446 }
7447
7448 /// The issues this source has commented on in this command; see
7449 /// [`Self::commented`](GitHubProjectsSource::commented).
7450 fn commented(&self) -> Result<std::sync::MutexGuard<'_, Vec<NativeId>>, SourceError> {
7451 self.commented.lock().map_err(|_| SourceError::Unavailable {
7452 message: "this source's record of what it commented on in this run was left \
7453 inconsistent by an earlier failure; next: run the command again"
7454 .into(),
7455 })
7456 }
7457
7458 /// Called only once GitHub has answered the comment write, so an issue whose comment
7459 /// failed is never made a candidate a later read would pay a node read for.
7460 fn remember_commented(&self, issue: &NativeId) -> Result<(), SourceError> {
7461 let mut commented = self.commented()?;
7462 if !commented.contains(issue) {
7463 commented.push(issue.clone());
7464 }
7465 Ok(())
7466 }
7467
7468 /// The items this source has created, for completing a board read that is behind.
7469 fn created(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
7470 self.created.lock().map_err(|_| SourceError::Unavailable {
7471 message: "this source's record of what it created in this run was left \
7472 inconsistent by an earlier failure; next: run the command again"
7473 .into(),
7474 })
7475 }
7476
7477 /// One board item as this source reports it, or `None` for content it ignores.
7478 ///
7479 /// A pull request is neither a project nor a task — it is somebody's change, not a
7480 /// unit of plan — and an item whose content the token cannot see has nothing to
7481 /// report at all.
7482 fn resolve(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
7483 let resolved = self.resolve_unremembered(item)?;
7484 if let Some(resolved) = &resolved {
7485 self.resolved_cache()?
7486 .insert(resolved.id.clone(), resolved.clone());
7487 }
7488 Ok(resolved)
7489 }
7490
7491 /// [`Self::resolve`], without remembering the item it resolves: what reading a token off a
7492 /// write's own response needs, which says nothing this process should answer a later read
7493 /// with.
7494 fn resolve_unremembered(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
7495 let content = item.get("content").ok_or_else(|| SourceError::Malformed {
7496 message: "GitHub project item is missing content".into(),
7497 })?;
7498 if content.is_null() {
7499 return Ok(None);
7500 }
7501 let content_kind = match required_str(content, "__typename")? {
7502 "Issue" => ContentKind::Issue,
7503 "DraftIssue" => ContentKind::DraftIssue,
7504 _ => return Ok(None),
7505 };
7506 let field_values = item
7507 .get("fieldValues")
7508 .ok_or_else(|| SourceError::Malformed {
7509 message: "GitHub project item is missing fieldValues".into(),
7510 })?;
7511 complete_connection(field_values, "project item field values", NESTED_PAGE_SIZE)?;
7512 let nodes = field_values
7513 .get("nodes")
7514 .and_then(Value::as_array)
7515 .ok_or_else(|| SourceError::Malformed {
7516 message: "GitHub project item fieldValues.nodes is not an array".into(),
7517 })?;
7518 if let Some(labels) = content.get("labels") {
7519 complete_connection(labels, "content labels", NESTED_PAGE_SIZE)?;
7520 }
7521 let raw_body = optional_str(content, "body")?.map(str::to_owned);
7522 let (body, slot) = self.visible(raw_body.clone())?;
7523 let parent = optional_str(content.get("parent").unwrap_or(&Value::Null), "id")?
7524 .map(|id| NativeId(id.to_owned()));
7525 // A draft has no sub-issues to summarise, and GitHub's schema gives it no field
7526 // to read one from; it is a task, and never a project.
7527 let sub_issues = match content_kind {
7528 ContentKind::Issue => sub_issue_total(content)?,
7529 ContentKind::DraftIssue => 0,
7530 };
7531 let content_id = required_str(content, "id")?;
7532 let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
7533 message: format!("GitHub issue {content_id}: {message}"),
7534 })?;
7535 let raw_title = required_str(content, "title")?;
7536 let kind = BoardKind::of(raw_title, parent.is_some(), sub_issues, marked);
7537 // The title a person wrote, which for a document is the one without the prefix —
7538 // the same way `content` above is the body without this source's metadata slot.
7539 let title = match kind {
7540 BoardKind::Document => raw_title[DESIGN_TITLE_PREFIX.len()..].to_owned(),
7541 BoardKind::Work(_) => raw_title.to_owned(),
7542 };
7543 let own_repository = content
7544 .pointer("/repository/nameWithOwner")
7545 .and_then(Value::as_str)
7546 .map(|origin| Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST)))
7547 .transpose()
7548 .map_err(|message| SourceError::Malformed { message })?;
7549 let repositories = if slot.contains_key(Repository::METADATA_KEY) {
7550 Repository::from_metadata(&slot)
7551 .map_err(|message| SourceError::Malformed { message })?
7552 } else {
7553 own_repository.clone().into_iter().collect()
7554 };
7555 let classification = Classification::from_metadata(&slot)
7556 .map_err(|message| SourceError::Malformed { message })?;
7557 let id = NativeId(content_id.to_owned());
7558 // Read only for a task, because only a task has either list: a project or a
7559 // document holding one of these keys holds nothing this source reports, and the
7560 // keys are left out of its caller-visible metadata all the same.
7561 let (delivers, delivered_by) = if kind == BoardKind::Work(ItemKind::Task) {
7562 let listed = |key: &str| {
7563 TaskRef::from_value(key, &id, Some(&self.name), slot.get(key))
7564 .map_err(|message| SourceError::Malformed { message })
7565 };
7566 (
7567 listed(TaskRef::DELIVERS_KEY)?,
7568 listed(TaskRef::DELIVERED_BY_KEY)?,
7569 )
7570 } else {
7571 (Vec::new(), Vec::new())
7572 };
7573 let (option, closed, reason) = Self::status_parts(nodes, content)?;
7574 let priority = self.held_priority(nodes)?;
7575 let item_labels = labels(content)?;
7576 // Present when the item was reached through its own issue, whose board entry
7577 // names the board; a read of the board's own items has the board already. An
7578 // empty id names nothing a field write could address, so it is read as absent and
7579 // the write goes back to reading the board.
7580 let board_id = item
7581 .pointer("/project/id")
7582 .and_then(Value::as_str)
7583 .filter(|id| !id.is_empty());
7584 let resolved = Resolved {
7585 item_id: required_str(item, "id")?.to_owned(),
7586 id,
7587 content_kind,
7588 kind,
7589 title,
7590 body: body.filter(|value| !value.is_empty()),
7591 raw_body,
7592 status: self
7593 .statuses
7594 .status(kind.status_kind(), option, closed, reason),
7595 option: option.map(str::to_owned),
7596 priority,
7597 closed,
7598 reason: closed.then(|| reason.map(str::to_owned)).flatten(),
7599 delivers,
7600 delivered_by,
7601 labels: item_labels.clone(),
7602 parent,
7603 origin: text_field(nodes, ORIGIN_FIELD)?.filter(|value| !value.is_empty()),
7604 projected: self.held_projections(nodes)?,
7605 number: match content_kind {
7606 ContentKind::Issue => Some(issue_number(content)?),
7607 // A draft is filed in no repository, so nothing ever numbered it:
7608 // `DraftIssue` declares no `number` at all, exactly as it declares no
7609 // `subIssuesSummary` the branch above reads.
7610 ContentKind::DraftIssue => None,
7611 },
7612 url: optional_str(content, "url")?.map(str::to_owned),
7613 created_at: optional_time(content, "createdAt")?,
7614 updated_at: optional_time(content, "updatedAt")?,
7615 item_updated_at: optional_time(item, "updatedAt")?,
7616 comment_count: content
7617 .pointer("/commentCount/totalCount")
7618 .and_then(Value::as_u64),
7619 relationship_events: content
7620 .pointer("/relationshipEvents/totalCount")
7621 .and_then(Value::as_u64),
7622 // The issue node is stamped wherever it was read; a board item a read of the issue
7623 // put together from its memberships is not.
7624 served_at: match optional_time(content, SERVED_AT)? {
7625 Some(served) => Some(served),
7626 None => optional_time(item, SERVED_AT)?,
7627 },
7628 // Both connections were read to their end above — one reporting a further page is
7629 // refused — so these are every field value and every label the item holds. A
7630 // response that did not select the labels at all says nothing of them, and gives
7631 // no facets.
7632 facets: content.get("labels").is_some().then(|| {
7633 change_token::Facets::new(kind.facet(), facet_fields(nodes), &item_labels).digest()
7634 }),
7635 own_repository,
7636 repositories,
7637 classification,
7638 slot,
7639 board_id: board_id.map(str::to_owned),
7640 fields: field_definitions(nodes),
7641 board_fields: Self::carried_board_fields(content, board_id)?,
7642 blocked_by: carried_blocked_by(content)?,
7643 };
7644 Ok(Some(resolved))
7645 }
7646
7647 /// The field definitions of the board `board_id` names — the project this issue's own
7648 /// board item is on — off the `boards` page a read of an issue by its own id carries, or
7649 /// `None` when the read carried none, carried no entry for that board, or the board item
7650 /// named no board, which a write then answers by reading the board's fields itself.
7651 ///
7652 /// Matched by the board's node id and never by its number alone: a project number is
7653 /// unique only within its owner, so another owner's board numbered alike can sit on the
7654 /// same page, and its field and option ids address nothing on this one.
7655 fn carried_board_fields(
7656 content: &Value,
7657 board_id: Option<&str>,
7658 ) -> Result<Option<Value>, SourceError> {
7659 let (Some(nodes), Some(board_id)) = (
7660 content.pointer("/boards/nodes").and_then(Value::as_array),
7661 board_id,
7662 ) else {
7663 return Ok(None);
7664 };
7665 let Some(board) = nodes.iter().find_map(|node| {
7666 let project = node.get("project")?;
7667 (project.get("id").and_then(Value::as_str) == Some(board_id)).then_some(project)
7668 }) else {
7669 return Ok(None);
7670 };
7671 let Some(fields) = board.get("fields").filter(|fields| !fields.is_null()) else {
7672 return Ok(None);
7673 };
7674 complete_connection(fields, "board fields", NESTED_PAGE_SIZE)?;
7675 Ok(Some(fields.clone()))
7676 }
7677
7678 /// What one board item's `Priority` field says, through this instance's mapping.
7679 ///
7680 /// An instance with no mapping holds no priority, so every item reads as `none` whatever
7681 /// its board holds. With one, no value is `none`, a mapped option is its level, and an
7682 /// option the mapping does not name is kept as itself — never read as a level or as
7683 /// `none` — for a read of the task to report by name.
7684 fn held_priority(&self, field_values: &[Value]) -> Result<HeldPriority, SourceError> {
7685 let Some(mapping) = &self.priorities else {
7686 return Ok(HeldPriority::Read(Priority::None));
7687 };
7688 // A value of the field that names no option — a text field someone called `Priority` —
7689 // is malformed rather than `none`: reading it as no priority would let the next copy
7690 // clear one a person set.
7691 let Some(option) = field_values
7692 .iter()
7693 .find(|value| {
7694 value.pointer("/field/name").and_then(Value::as_str) == Some(PRIORITY_FIELD)
7695 })
7696 .map(|value| required_str(value, "name"))
7697 .transpose()?
7698 else {
7699 return Ok(HeldPriority::Read(Priority::None));
7700 };
7701 Ok(mapping.priority_of(option).map_or_else(
7702 || HeldPriority::Unmapped(option.to_owned()),
7703 HeldPriority::Read,
7704 ))
7705 }
7706
7707 /// What each projected metadata field holds for one board item, by field name — every text
7708 /// value GitHub answers, an empty one included.
7709 fn held_projections(
7710 &self,
7711 field_values: &[Value],
7712 ) -> Result<BTreeMap<String, String>, SourceError> {
7713 let mut held = BTreeMap::new();
7714 for projection in &self.metadata_fields {
7715 if let Some(text) = text_field(field_values, &projection.field)? {
7716 held.insert(projection.field.to_string(), text);
7717 }
7718 }
7719 Ok(held)
7720 }
7721
7722 /// What one board item's status is read from: its `Status` option, whether its issue
7723 /// is closed, and the reason it was closed with. [`BoardStatuses::status`] turns the
7724 /// three into the status it reports.
7725 fn status_parts<'a>(
7726 field_values: &'a [Value],
7727 content: &'a Value,
7728 ) -> Result<(Option<&'a str>, bool, Option<&'a str>), SourceError> {
7729 let option = field_values
7730 .iter()
7731 .find(|value| value.pointer("/field/name").and_then(Value::as_str) == Some("Status"))
7732 .map(|value| required_str(value, "name"))
7733 .transpose()?;
7734 let closed = optional_str(content, "state")? == Some("CLOSED");
7735 Ok((option, closed, optional_str(content, "stateReason")?))
7736 }
7737
7738 /// The board Status option this write selects, or the refusal that says why not.
7739 ///
7740 /// The mapped option is required for both open and terminal targets. A terminal write
7741 /// validates it before changing either representation, so it can never fall back to
7742 /// closing an issue whose board cannot display the matching status.
7743 ///
7744 /// Answers the field's id, the option's id, and the option's name as the board spells
7745 /// it — which is the name a read of the item reports once it sits there.
7746 fn column_for(
7747 &self,
7748 fields: &Value,
7749 kind: ItemKind,
7750 category: StatusCategory,
7751 target: &StatusTarget,
7752 ) -> Result<Option<(String, String, String)>, SourceError> {
7753 let Some(wanted) = target.option() else {
7754 return Ok(None);
7755 };
7756 let missing = |detail: &str| SourceError::Refused {
7757 message: format!(
7758 "{} status {} of source {} needs the board Status option {wanted:?}, and \
7759 {detail}; next: add that option to the board, which `onetaskgraph sources \
7760 fields {} --apply` does, or point status_mapping.{}.{} of this source at one \
7761 it has",
7762 kind.marker(),
7763 category_name(category),
7764 self.name,
7765 self.name,
7766 category_name(category),
7767 kind.marker()
7768 ),
7769 };
7770 let Some(field) = Board::field(fields, "Status")? else {
7771 return Err(missing("this board has no Status field"));
7772 };
7773 if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
7774 return Err(missing(
7775 "this board's Status field is not a single-select field",
7776 ));
7777 }
7778 let option = field
7779 .get("options")
7780 .and_then(Value::as_array)
7781 .and_then(|options| {
7782 options.iter().find(|option| {
7783 option
7784 .get("name")
7785 .and_then(Value::as_str)
7786 .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
7787 })
7788 });
7789 match option {
7790 None => Err(missing("this board does not have it")),
7791 Some(option) => Ok(Some((
7792 required_str(field, "id")?.to_owned(),
7793 required_str(option, "id")?.to_owned(),
7794 required_str(option, "name")?.to_owned(),
7795 ))),
7796 }
7797 }
7798
7799 /// The refusal a status that closes an issue is answered with over a board draft.
7800 fn closes_a_draft(&self, category: StatusCategory) -> SourceError {
7801 SourceError::Refused {
7802 message: format!(
7803 "status {} of source {} closes the item's issue, and GitHub draft items have \
7804 no open or closed state",
7805 category_name(category),
7806 self.name
7807 ),
7808 }
7809 }
7810
7811 /// What a status write to one item needs of the board: the board's id and the
7812 /// definition of its `Status` field, read off the item when the item says both.
7813 ///
7814 /// The same reasoning as [`Self::fields_for`]: a node read of the item names its board,
7815 /// and its `Status` value carries that field's definition, options and all. An item that
7816 /// does not say — no board id, or no `Status` value to read the field off — takes them
7817 /// from [`Self::board_fields`], which reads no item.
7818 async fn status_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
7819 if let Some(board) = item.carried_board() {
7820 return Ok(board);
7821 }
7822 if item.defines("Status")
7823 && let Some(board_id) = item.named_board()
7824 {
7825 return Ok(BoardFields {
7826 id: board_id,
7827 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
7828 });
7829 }
7830 self.board_fields().await
7831 }
7832
7833 /// Set one task's or one project's status and nothing else; see
7834 /// [`TaskSource::set_task_status`] and [`TaskSource::set_project_status`].
7835 async fn set_status(
7836 &self,
7837 id: &NativeId,
7838 kind: ItemKind,
7839 category: StatusCategory,
7840 ) -> Result<Option<Status>, SourceError> {
7841 // Refused before anything is read, in the words a write of the same status is.
7842 let target = self.resolved_target(kind, category)?;
7843 // An item read by issue reference carries only what this write needs, so what it
7844 // becomes is never remembered for a later read to be answered from.
7845 let held = self.take_held(id)?;
7846 let by_reference = held.is_some();
7847 let bound = match held {
7848 Some(item) => Some(item),
7849 None => self.bound_item(id).await?,
7850 };
7851 let Some(mut item) = bound.filter(|item| item.kind == BoardKind::Work(kind)) else {
7852 return Ok(None);
7853 };
7854 let board = self.status_board(&item).await?;
7855 let (field, option, name) = self
7856 .column_for(&board.fields, kind, category, &target)?
7857 .ok_or_else(|| SourceError::Malformed {
7858 message: format!(
7859 "status {} of source {} names no board Status option",
7860 category_name(category),
7861 self.name
7862 ),
7863 })?;
7864 if item.status.category == category && item.option.as_deref() == Some(&name) {
7865 return Ok(Some(item.status));
7866 }
7867 match &target {
7868 StatusTarget::Terminal(_, reason) => {
7869 if item.content_kind == ContentKind::DraftIssue {
7870 return Err(self.closes_a_draft(category));
7871 }
7872 // The option first, so a refused selection leaves an open issue open rather
7873 // than closed behind a card that still reads open.
7874 self.set_item_field(
7875 board.id.as_str(),
7876 &item.item_id,
7877 &field,
7878 json!({"singleSelectOptionId": option}),
7879 )
7880 .await?;
7881 // An issue already closed in a way that reads as this category keeps its state
7882 // and its reason: only its card was out of line, and closing it again would
7883 // overwrite a reason — a duplicate's — that says more than this write does.
7884 let already = item.closed && item.status.category == category;
7885 if !already {
7886 self.update_content(
7887 ContentKind::Issue,
7888 &item.id,
7889 json!({"stateInput": state_input(Some(&target))}),
7890 )
7891 .await?;
7892 }
7893 let reason = if already {
7894 item.reason.clone()
7895 } else {
7896 Some(reason.reason().to_owned())
7897 };
7898 item.closed = true;
7899 item.status = self
7900 .statuses
7901 .status(kind, Some(&name), true, reason.as_deref());
7902 item.option = Some(name);
7903 item.reason = reason;
7904 }
7905 StatusTarget::Column(_) => {
7906 // An option is what an open item's status is, so a closed issue is reopened
7907 // first — sitting closed in the column, it would read back as closed. A draft has
7908 // no state to reopen.
7909 if item.content_kind == ContentKind::Issue && item.closed {
7910 self.update_content(
7911 ContentKind::Issue,
7912 &item.id,
7913 json!({"stateInput": state_input(Some(&target))}),
7914 )
7915 .await?;
7916 item.closed = false;
7917 }
7918 self.set_item_field(
7919 board.id.as_str(),
7920 &item.item_id,
7921 &field,
7922 json!({"singleSelectOptionId": option}),
7923 )
7924 .await?;
7925 item.status = self.statuses.status(kind, Some(&name), false, None);
7926 item.option = Some(name);
7927 }
7928 StatusTarget::Disabled(_) => {
7929 unreachable!("resolved_target refused a disabled status")
7930 }
7931 }
7932 let status = item.status.clone();
7933 if !by_reference {
7934 self.remember_written(item, false)?;
7935 }
7936 Ok(Some(status))
7937 }
7938
7939 /// Replace one task's `delivered_by` and nothing else; see
7940 /// [`TaskSource::set_delivered_by`].
7941 ///
7942 /// One update of the body, which differs from the body GitHub holds only inside the
7943 /// metadata slot — see [`with_slot`]. A body that would not change is not sent at all.
7944 async fn replace_delivered_by(
7945 &self,
7946 id: &NativeId,
7947 delivered_by: &[TaskRef],
7948 ) -> Result<Option<()>, SourceError> {
7949 let entries = TaskRef::listed(
7950 TaskRef::DELIVERED_BY_KEY,
7951 id,
7952 Some(&self.name),
7953 delivered_by.to_vec(),
7954 )
7955 .map_err(|message| SourceError::Refused { message })?;
7956 let Some(mut item) = self
7957 .bound_item(id)
7958 .await?
7959 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
7960 else {
7961 return Ok(None);
7962 };
7963 let mut slot = item.slot.clone();
7964 set_task_list(&mut slot, TaskRef::DELIVERED_BY_KEY, &entries);
7965 self.write_slot(&mut item, &slot).await?;
7966 item.delivered_by = entries;
7967 self.remember_written(item, false)?;
7968 Ok(Some(()))
7969 }
7970
7971 /// Set one caller key of the metadata slot of one issue of `kind`, and nothing else;
7972 /// see [`TaskSource::set_task_metadata`].
7973 ///
7974 /// `None` when this board holds no item by that id, or holds one of another kind. The
7975 /// answer is the item as this source now reads it, so what a caller is told the key
7976 /// holds is what the slot holds.
7977 ///
7978 /// A key already holding the value is answered without a write, compared as JSON rather
7979 /// than as the body's bytes: a slot a person spelled with other whitespace would
7980 /// otherwise be re-encoded, which is a write that changes nothing the caller asked for.
7981 async fn set_slot_key(
7982 &self,
7983 id: &NativeId,
7984 kind: BoardKind,
7985 key: &MetadataKey,
7986 value: &Value,
7987 ) -> Result<Option<Resolved>, SourceError> {
7988 let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
7989 return Ok(None);
7990 };
7991 if item.slot.get(key.as_str()) == Some(value) {
7992 return Ok(Some(item));
7993 }
7994 let mut slot = item.slot.clone();
7995 slot.insert(key.as_str().to_owned(), value.clone());
7996 // A key a board text field projects moves that field first, so the body — the value's
7997 // one authoritative home — is written last, as every write of an existing item is.
7998 if self
7999 .metadata_fields
8000 .iter()
8001 .any(|projection| &*projection.key == key.as_str())
8002 {
8003 let board = self.projection_board(&item).await?;
8004 let (writes, projected) = self.projection_writes(
8005 &board.fields,
8006 Some(&item.projected),
8007 &slot,
8008 Some(&[key.as_str()]),
8009 )?;
8010 let (mut values, mut clears) = (Vec::new(), Vec::new());
8011 for write in &writes {
8012 write.join(&mut values, &mut clears);
8013 }
8014 self.set_item_fields(board.id.as_str(), &item.item_id, &values, &clears)
8015 .await?;
8016 item.projected = projected;
8017 }
8018 self.write_slot(&mut item, &slot).await?;
8019 self.remember_written(item.clone(), false)?;
8020 Ok(Some(item))
8021 }
8022
8023 /// What a projection write to `item` needs of the board, read off the item when it says
8024 /// enough, as [`Self::fields_for`] reads it.
8025 async fn projection_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
8026 if let Some(board) = item.carried_board() {
8027 return Ok(board);
8028 }
8029 if let Some(board_id) = item.named_board()
8030 && self
8031 .metadata_fields
8032 .iter()
8033 .all(|projection| item.defines(&projection.field))
8034 {
8035 return Ok(BoardFields {
8036 id: board_id,
8037 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
8038 });
8039 }
8040 self.board_fields().await
8041 }
8042
8043 /// Put `slot` in one item's metadata slot with a single update of its body, and bring
8044 /// `item` up to what that write left.
8045 ///
8046 /// The body sent differs from the body GitHub holds only inside the slot — see
8047 /// [`with_slot`] — and a body that would not change is not sent at all. It goes through
8048 /// the mutation the item's content takes, so a board draft's body is written with
8049 /// `updateProjectV2DraftIssue` exactly as an issue's is with `updateIssue`.
8050 async fn write_slot(
8051 &self,
8052 item: &mut Resolved,
8053 slot: &BTreeMap<String, Value>,
8054 ) -> Result<(), SourceError> {
8055 let held = item.raw_body.clone().unwrap_or_default();
8056 let body = with_slot(&held, slot)?;
8057 if body != held {
8058 self.update_content(item.content_kind, &item.id, json!({"body": body}))
8059 .await?;
8060 }
8061 let (visible, slot) = self.visible(Some(body.clone()))?;
8062 item.body = visible.filter(|value| !value.is_empty());
8063 item.raw_body = Some(body);
8064 item.slot = slot;
8065 Ok(())
8066 }
8067
8068 /// This instance's target for a category written to an item of `kind`, refusing one
8069 /// that kind has no option for — before anything is read or written.
8070 ///
8071 /// Nothing here mutates the board's option set to make room for a status. GitHub
8072 /// documents `UpdateProjectV2FieldInput.singleSelectOptions` as *"provided values
8073 /// overwrite existing options"*, so no addition is additive and a mistake destroys the
8074 /// field and every item's status.
8075 fn resolved_target(
8076 &self,
8077 kind: ItemKind,
8078 category: StatusCategory,
8079 ) -> Result<StatusTarget, SourceError> {
8080 let target = self.statuses.target(kind, category).clone();
8081 let StatusTarget::Disabled(why) = target else {
8082 return Ok(target);
8083 };
8084 let refusal = why.refusal(&self.name, category, kind);
8085 // Why there is no shipped default, which is the question a person meeting this
8086 // refusal on a source that never mentioned the category asks.
8087 let shipped_none = match category {
8088 StatusCategory::Draft => Some(
8089 "draft has no shipped default because GitHub draft issues cannot have \
8090 sub-issues, and this source stores a project's tasks as its issue's sub-issues",
8091 ),
8092 StatusCategory::Unknown => Some(
8093 "unknown has no shipped default because this board keeps no open-ended status \
8094 word: every word classified unknown is written to the one board Status option \
8095 status_mapping.unknown names",
8096 ),
8097 _ => None,
8098 };
8099 Err(match (refusal, shipped_none, why) {
8100 (SourceError::Refused { message }, Some(note), UnmappedStatus::Unconfigured) => {
8101 SourceError::Refused {
8102 message: format!("{message}; {note}"),
8103 }
8104 }
8105 (refusal, _, _) => refusal,
8106 })
8107 }
8108
8109 /// What writing `priority` does to one item's `Priority` field on this board, or the
8110 /// refusal naming what the board lacks.
8111 ///
8112 /// `none` is no value, so it clears the field — and asks nothing of an item that holds
8113 /// none already, or of an item not created yet. Every other priority selects the option
8114 /// the mapping names, matched case-insensitively; a board with no `Priority` field, or
8115 /// without that option, is refused rather than given one: reads and writes never create
8116 /// a field or an option.
8117 fn priority_write(
8118 &self,
8119 fields: &Value,
8120 existing: Option<&Resolved>,
8121 priority: Priority,
8122 ) -> Result<Option<PriorityWrite>, SourceError> {
8123 let Some(mapping) = &self.priorities else {
8124 return Err(self.holds_no_priority());
8125 };
8126 let Some(wanted) = mapping.option(priority) else {
8127 if !existing.is_some_and(Resolved::holds_priority) {
8128 return Ok(None);
8129 }
8130 let field =
8131 Board::field(fields, PRIORITY_FIELD)?.ok_or_else(|| SourceError::Malformed {
8132 message: format!(
8133 "an item holding a {PRIORITY_FIELD} value was read without that field"
8134 ),
8135 })?;
8136 return Ok(Some(PriorityWrite::Clear {
8137 field: required_str(field, "id")?.to_owned(),
8138 }));
8139 };
8140 let missing = |detail: &str| SourceError::Refused {
8141 message: format!(
8142 "priority {priority} of source {} needs the board {PRIORITY_FIELD} option \
8143 {wanted:?}, and {detail}; run `onetaskgraph sources fields {} --apply` to add \
8144 it, or point priority_mapping.{priority} of this source at an option the board \
8145 has",
8146 self.name, self.name
8147 ),
8148 };
8149 let Some(field) = Board::field(fields, PRIORITY_FIELD)? else {
8150 return Err(missing(&format!(
8151 "this board has no {PRIORITY_FIELD} field"
8152 )));
8153 };
8154 if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
8155 return Err(missing(&format!(
8156 "this board's {PRIORITY_FIELD} field is not a single-select field"
8157 )));
8158 }
8159 // An options list that is absent or not a list is an answer this source cannot read,
8160 // not a board lacking the option: `sources fields --apply` is no remedy for it.
8161 let option = field
8162 .get("options")
8163 .and_then(Value::as_array)
8164 .ok_or_else(|| SourceError::Malformed {
8165 message: format!("GitHub {PRIORITY_FIELD} field options is not an array"),
8166 })?
8167 .iter()
8168 .find(|option| {
8169 option
8170 .get("name")
8171 .and_then(Value::as_str)
8172 .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
8173 })
8174 .ok_or_else(|| missing("this board does not have it"))?;
8175 Ok(Some(PriorityWrite::Select {
8176 field: required_str(field, "id")?.to_owned(),
8177 option: required_str(option, "id")?.to_owned(),
8178 }))
8179 }
8180
8181 /// What writing `metadata` does to each projected text field of one item, and what those
8182 /// fields hold after it — or the refusal, before anything is sent.
8183 ///
8184 /// `held` is what the item's fields hold now, `None` for an item not created yet. `keys`
8185 /// narrows the work to the entries projecting one of those metadata keys, for a write that
8186 /// touches only them; `None` is every entry. A string the field already holds, and nothing
8187 /// for a field holding nothing, sends nothing. A value no text field can hold is refused,
8188 /// and so is a board without the field, or with a field of that name that is not a text
8189 /// field: reads and writes never create a field.
8190 fn projection_writes(
8191 &self,
8192 fields: &Value,
8193 held: Option<&BTreeMap<String, String>>,
8194 metadata: &BTreeMap<String, Value>,
8195 keys: Option<&[&str]>,
8196 ) -> Result<(Vec<ProjectionWrite>, BTreeMap<String, String>), SourceError> {
8197 let mut projected = held.cloned().unwrap_or_default();
8198 let mut writes = Vec::new();
8199 for projection in self
8200 .metadata_fields
8201 .iter()
8202 .filter(|projection| keys.is_none_or(|keys| keys.contains(&&*projection.key)))
8203 {
8204 let wanted = projection.projected(metadata, &self.name)?;
8205 let field = self.projection_field(fields, projection)?;
8206 let holding = projected.get(&*projection.field);
8207 match wanted {
8208 Projected::Text(text) if holding != Some(&text) => {
8209 writes.push(ProjectionWrite::Set {
8210 field,
8211 text: text.clone(),
8212 });
8213 projected.insert(projection.field.to_string(), text);
8214 }
8215 Projected::Nothing if holding.is_some() => {
8216 writes.push(ProjectionWrite::Clear { field });
8217 projected.remove(&*projection.field);
8218 }
8219 Projected::Text(_) | Projected::Nothing => {}
8220 }
8221 }
8222 Ok((writes, projected))
8223 }
8224
8225 /// The id of the board text field one projection writes, or the refusal naming what the
8226 /// board has instead.
8227 fn projection_field(
8228 &self,
8229 fields: &Value,
8230 projection: &MetadataField,
8231 ) -> Result<String, SourceError> {
8232 let refuse = |detail: String| SourceError::Refused {
8233 message: format!(
8234 "source {} projects metadata key {:?} onto the board text field {:?}, and \
8235 {detail}; next: run `onetaskgraph sources fields {} --apply` to create it as a \
8236 text field, or name another field in metadata_fields",
8237 self.name, &*projection.key, &*projection.field, self.name
8238 ),
8239 };
8240 let Some(field) = Board::field(fields, &projection.field)? else {
8241 return Err(refuse("this board has no field of that name".to_owned()));
8242 };
8243 let typename = optional_str(field, "__typename")?.unwrap_or("field of another type");
8244 let data_type = optional_str(field, "dataType")?;
8245 if typename != "ProjectV2Field" || data_type != Some("TEXT") {
8246 return Err(refuse(format!(
8247 "this board's field of that name is not a text field (it is a {}), so it is \
8248 left as it is; rename or remove that field first",
8249 data_type.unwrap_or(typename)
8250 )));
8251 }
8252 Ok(required_str(field, "id")?.to_owned())
8253 }
8254
8255 /// Apply one priority write to one board item.
8256 async fn write_priority(
8257 &self,
8258 board_id: &str,
8259 item_id: &str,
8260 write: &PriorityWrite,
8261 ) -> Result<(), SourceError> {
8262 match write {
8263 PriorityWrite::Select { field, option } => {
8264 self.set_item_field(
8265 board_id,
8266 item_id,
8267 field,
8268 json!({"singleSelectOptionId": option}),
8269 )
8270 .await
8271 }
8272 PriorityWrite::Clear { field } => {
8273 let data = self
8274 .graphql(
8275 graphql::CLEAR_FIELD,
8276 json!({"input":{"projectId":board_id,"itemId":item_id,"fieldId":field},
8277 "readPriority":false,"priorityName":PRIORITY_FIELD}),
8278 )
8279 .await?;
8280 let returned = data
8281 .pointer("/clearProjectV2ItemFieldValue/projectV2Item")
8282 .ok_or_else(|| SourceError::Malformed {
8283 message: "GitHub field clear returned no project item".into(),
8284 })?;
8285 if required_str(returned, "id")? != item_id {
8286 return Err(SourceError::Malformed {
8287 message: "GitHub field clear returned the wrong project item".into(),
8288 });
8289 }
8290 Ok(())
8291 }
8292 }
8293 }
8294
8295 /// The refusal a priority is answered with by an instance configured with no
8296 /// `priority_mapping`, which holds none.
8297 fn holds_no_priority(&self) -> SourceError {
8298 SourceError::Refused {
8299 message: format!(
8300 "source {} holds no task priority: its configuration sets no priority_mapping; \
8301 next: set priority_mapping on this source, then run `onetaskgraph sources \
8302 fields {} --apply` to set its board up",
8303 self.name, self.name
8304 ),
8305 }
8306 }
8307
8308 /// Set one task's priority and nothing else; see [`TaskSource::set_task_priority`].
8309 ///
8310 /// One field write — a select, or a clear for `none` — and no title, body, label, state
8311 /// or `Status` request. Clearing a priority an item does not hold sends nothing.
8312 async fn set_priority(
8313 &self,
8314 id: &NativeId,
8315 priority: Priority,
8316 ) -> Result<Option<Priority>, SourceError> {
8317 if self.priorities.is_none() {
8318 return Err(self.holds_no_priority());
8319 }
8320 let Some(mut item) = self
8321 .bound_item(id)
8322 .await?
8323 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8324 else {
8325 return Ok(None);
8326 };
8327 if priority == Priority::None && !item.holds_priority() {
8328 return Ok(Some(priority));
8329 }
8330 // The item's own read carries the field's definition whenever it holds a value of
8331 // it, which a clear always does; a select onto an item holding none reads the board.
8332 let board = match (item.carried_board(), item.named_board()) {
8333 (Some(board), _) => board,
8334 (None, Some(id)) if item.defines(PRIORITY_FIELD) => BoardFields {
8335 id,
8336 fields: json!({"nodes": item.fields.clone(), "pageInfo": {"hasNextPage": false}}),
8337 },
8338 _ => self.board_fields().await?,
8339 };
8340 let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? else {
8341 return Ok(Some(priority));
8342 };
8343 let (document, root, input) = match write {
8344 PriorityWrite::Select { field, option } => (
8345 graphql::UPDATE_FIELD,
8346 "updateProjectV2ItemFieldValue",
8347 json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field,"value":{"singleSelectOptionId":option}}),
8348 ),
8349 PriorityWrite::Clear { field } => (
8350 graphql::CLEAR_FIELD,
8351 "clearProjectV2ItemFieldValue",
8352 json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field}),
8353 ),
8354 };
8355 let data = self
8356 .graphql(
8357 document,
8358 json!({"input":input,"readPriority":true,"priorityName":PRIORITY_FIELD}),
8359 )
8360 .await?;
8361 let returned = data
8362 .get(root)
8363 .and_then(|value| value.get("projectV2Item"))
8364 .ok_or_else(|| SourceError::Malformed {
8365 message: "GitHub priority write returned no project item".into(),
8366 })?;
8367 if required_str(returned, "id")? != item.item_id {
8368 return Err(SourceError::Malformed {
8369 message: "GitHub priority write returned the wrong project item".into(),
8370 });
8371 }
8372 let value = returned
8373 .get("fieldValueByName")
8374 .ok_or_else(|| SourceError::Malformed {
8375 message: "GitHub priority write returned no priority read-back".into(),
8376 })?;
8377 if !value.is_null()
8378 && value.pointer("/field/name").and_then(Value::as_str) != Some(PRIORITY_FIELD)
8379 {
8380 return Err(SourceError::Malformed {
8381 message: "GitHub priority read-back is not a Priority field value".into(),
8382 });
8383 }
8384 let values = if value.is_null() {
8385 Vec::new()
8386 } else {
8387 vec![value.clone()]
8388 };
8389 item.priority = self.held_priority(&values)?;
8390 let answer = item.task()?.priority;
8391 self.remember_written(item, false)?;
8392 Ok(Some(answer))
8393 }
8394
8395 /// Replace one task's visible body and nothing else; see
8396 /// [`TaskSource::set_task_content`].
8397 ///
8398 /// One update of the body, which differs from the body GitHub holds only outside the
8399 /// metadata slot — the slot is kept byte for byte, so every caller key and every list
8400 /// this source keeps there reads back as it was. A body that would not change is not
8401 /// sent at all.
8402 async fn replace_content(
8403 &self,
8404 id: &NativeId,
8405 content: &str,
8406 ) -> Result<Option<()>, SourceError> {
8407 let Some(mut item) = self
8408 .bound_item(id)
8409 .await?
8410 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8411 else {
8412 return Ok(None);
8413 };
8414 let held = item.raw_body.clone().unwrap_or_default();
8415 let body = with_content(&held, &self.marked(content))?;
8416 // Checked before anything is sent: content ending in what this source reads as its own
8417 // metadata slot would read back as metadata rather than as the content it was.
8418 let (visible, slot) = self.visible(Some(body.clone()))?;
8419 if visible.as_deref().unwrap_or_default() != content || slot != item.slot {
8420 return Err(SourceError::Refused {
8421 message: format!(
8422 "this content ends in what source {} reads as its own metadata slot \
8423 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8424 as content; next: remove that trailing block from the content",
8425 self.name
8426 ),
8427 });
8428 }
8429 if body != held {
8430 self.update_content(item.content_kind, &item.id, json!({"body": body}))
8431 .await?;
8432 }
8433 item.body = visible.filter(|value| !value.is_empty());
8434 item.raw_body = Some(body);
8435 item.slot = slot;
8436 self.remember_written(item, false)?;
8437 Ok(Some(()))
8438 }
8439
8440 /// Apply one targeted update to one task; see [`TaskSource::update_task`].
8441 ///
8442 /// One read of the item — which carries the board's field definitions and the issue's
8443 /// `blockedBy`, so neither is read again — and then only what differs from it: the
8444 /// `Status` option and the `Priority` field together in one request, the `blockedBy`
8445 /// additions and removals the named edges differ by, and last one `updateIssue` carrying
8446 /// the title, the body — visible content and metadata slot together — and a state change.
8447 /// So an update naming any of title, body, metadata, status and priority is one read and
8448 /// at most two writes. The body goes last so that a write refused part-way leaves it, and
8449 /// the metadata in it, as it stood. A terminal status selects its option and then closes,
8450 /// as a whole write does; an open one selects its option and then reopens. The origin
8451 /// field is never written: an update is of an item that already exists, whose origin is
8452 /// what it is.
8453 ///
8454 /// The task answered is the item as those writes left it, built from the read and what was
8455 /// sent rather than read again — the same record a later read in this run answers from.
8456 async fn targeted_update(
8457 &self,
8458 id: &NativeId,
8459 update: &TaskUpdate,
8460 ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
8461 // Everything this source can refuse without reading the item is refused first, in the
8462 // words a whole write of the same fields is refused with.
8463 update.consistent()?;
8464 if update
8465 .title
8466 .as_deref()
8467 .is_some_and(|title| title.starts_with(DESIGN_TITLE_PREFIX))
8468 {
8469 return Err(SourceError::Refused {
8470 message: format!(
8471 "the title of this task begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
8472 spells a document, so it would read back as one rather than as a task; \
8473 retitle it",
8474 self.name
8475 ),
8476 });
8477 }
8478 if let Some(delivers) = &update.delivers {
8479 TaskRef::listed(
8480 TaskRef::DELIVERS_KEY,
8481 id,
8482 Some(&self.name),
8483 delivers.clone(),
8484 )
8485 .map_err(|message| SourceError::Refused { message })?;
8486 }
8487 if self.priorities.is_none()
8488 && update
8489 .priority
8490 .is_some_and(|priority| priority != Priority::None)
8491 {
8492 return Err(self.holds_no_priority());
8493 }
8494 let target = update
8495 .status
8496 .as_ref()
8497 .map(|status| self.resolved_target(ItemKind::Task, status.category))
8498 .transpose()?;
8499 let Some(mut item) = self
8500 .bound_item(id)
8501 .await?
8502 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8503 else {
8504 return Ok(None);
8505 };
8506 let before = item.task()?;
8507
8508 let mut status_move = None;
8509 if let (Some(status), Some(target)) = (&update.status, target) {
8510 let board = self.status_board(&item).await?;
8511 let (field, option, name) = self
8512 .column_for(&board.fields, ItemKind::Task, status.category, &target)?
8513 .ok_or_else(|| SourceError::Malformed {
8514 message: format!(
8515 "status {} of source {} names no board Status option",
8516 category_name(status.category),
8517 self.name
8518 ),
8519 })?;
8520 let terminal = matches!(target, StatusTarget::Terminal(_, _));
8521 if terminal && item.content_kind == ContentKind::DraftIssue {
8522 return Err(self.closes_a_draft(status.category));
8523 }
8524 let landed = match &target {
8525 StatusTarget::Terminal(_, reason) => {
8526 self.statuses
8527 .status(ItemKind::Task, Some(&name), true, Some(reason.reason()))
8528 }
8529 _ => self
8530 .statuses
8531 .status(ItemKind::Task, Some(&name), false, None),
8532 };
8533 let option_moves = item
8534 .option
8535 .as_deref()
8536 .is_none_or(|held| !held.eq_ignore_ascii_case(&name));
8537 let state_moves = item.content_kind == ContentKind::Issue
8538 && (item.closed != terminal || (terminal && item.status != landed));
8539 if let Some(moves) = Moves::of(option_moves, state_moves) {
8540 status_move = Some(StatusMove {
8541 board: board.id,
8542 field,
8543 option,
8544 name,
8545 target,
8546 landed,
8547 moves,
8548 });
8549 }
8550 }
8551
8552 let mut priority_move = None;
8553 if let Some(priority) = update.priority
8554 && self.priorities.is_some()
8555 && item.priority != HeldPriority::Read(priority)
8556 {
8557 let board = match (item.carried_board(), item.named_board()) {
8558 (Some(board), _) => board,
8559 (None, Some(board)) if item.defines(PRIORITY_FIELD) => BoardFields {
8560 id: board,
8561 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
8562 },
8563 _ => self.board_fields().await?,
8564 };
8565 if let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? {
8566 priority_move = Some((board.id, write, priority));
8567 }
8568 }
8569
8570 // The metadata keys this update names that a board text field projects, and what
8571 // that does to those fields — refused, as a whole write refuses it, before anything
8572 // is sent.
8573 let touched: Vec<&str> = update
8574 .metadata_set
8575 .keys()
8576 .chain(update.metadata_remove.iter())
8577 .map(MetadataKey::as_str)
8578 .collect();
8579 let mut projection_move = None;
8580 if self
8581 .metadata_fields
8582 .iter()
8583 .any(|projection| touched.contains(&&*projection.key))
8584 {
8585 let mut metadata = item.slot.clone();
8586 for (key, value) in &update.metadata_set {
8587 metadata.insert(key.as_str().to_owned(), value.clone());
8588 }
8589 for key in &update.metadata_remove {
8590 metadata.remove(key.as_str());
8591 }
8592 let board = self.projection_board(&item).await?;
8593 let (writes, projected) = self.projection_writes(
8594 &board.fields,
8595 Some(&item.projected),
8596 &metadata,
8597 Some(&touched),
8598 )?;
8599 projection_move = Some((board.id, writes, projected));
8600 }
8601
8602 // Resolved before the body is composed, because a far end `blockedBy` cannot name is
8603 // recorded in the slot, and the slot travels in the one body update below.
8604 let edges = match &update.depends_on {
8605 Some(edges) => Some(
8606 self.partition_edges(
8607 BoardKind::Work(ItemKind::Task),
8608 item.content_kind,
8609 item.blocked_by.as_deref(),
8610 edges,
8611 )
8612 .await?,
8613 ),
8614 None => None,
8615 };
8616
8617 // A record primed from what a producer wrote holds no body anybody read, so an update
8618 // changing the body reads it first: by the `Status` write's own answer when the update
8619 // moves the option — the mutation answering with the issue whole — and otherwise by
8620 // reading the issue. The body is then composed against what GitHub holds now.
8621 let mut option_selected = false;
8622 // The mark stays until a read lands, so an update that writes no body, or one whose
8623 // read fails, leaves the next update of it to read the body too.
8624 if (update.content.is_some()
8625 || !update.metadata_set.is_empty()
8626 || !update.metadata_remove.is_empty()
8627 || update.delivers.is_some()
8628 || edges.is_some())
8629 && self.is_unread(id)
8630 {
8631 let live = match status_move.as_ref().filter(|moving| moving.moves.option()) {
8632 Some(moving) => {
8633 option_selected = true;
8634 Some(
8635 self.select_reading_body(
8636 moving.board.as_str(),
8637 &item.item_id,
8638 &moving.field,
8639 &moving.option,
8640 )
8641 .await?,
8642 )
8643 }
8644 None => {
8645 self.forget(id)?;
8646 let Some(read) = self.item_by_id(id).await? else {
8647 return Ok(None);
8648 };
8649 read.raw_body
8650 }
8651 };
8652 let (visible, read_slot) = self.visible(live.clone())?;
8653 self.mark_read(id);
8654 item.raw_body = live;
8655 item.body = visible;
8656 item.slot = read_slot;
8657 }
8658
8659 let mut slot = item.slot.clone();
8660 for (key, value) in &update.metadata_set {
8661 slot.insert(key.as_str().to_owned(), value.clone());
8662 }
8663 for key in &update.metadata_remove {
8664 slot.remove(key.as_str());
8665 }
8666 if let Some(delivers) = &update.delivers {
8667 set_task_list(&mut slot, TaskRef::DELIVERS_KEY, delivers);
8668 }
8669 if let Some((_, recorded)) = &edges {
8670 record_edges(&mut slot, recorded);
8671 }
8672 let held = item.raw_body.clone().unwrap_or_default();
8673 let content = match &update.content {
8674 Some(content) => with_content(&held, &self.marked(content))?,
8675 None => held.clone(),
8676 };
8677 // A slot holding what it held is kept byte for byte, compared as JSON rather than as
8678 // the body's bytes, as a metadata write compares it: a slot a person spelled with
8679 // other whitespace would otherwise be re-encoded, which is a write nobody asked for.
8680 let body = if slot == item.slot {
8681 content
8682 } else {
8683 with_slot(&content, &slot)?
8684 };
8685 // Checked before anything is sent, as a content write checks it: content ending in
8686 // what this source reads as its own slot would read back as metadata.
8687 let (visible, read) = self.visible(Some(body.clone()))?;
8688 let wanted = update.content.as_deref().or(item.body.as_deref());
8689 if visible.as_deref().unwrap_or_default() != wanted.unwrap_or_default() || read != slot {
8690 return Err(SourceError::Refused {
8691 message: format!(
8692 "this content ends in what source {} reads as its own metadata slot \
8693 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8694 as content; next: remove that trailing block from the content",
8695 self.name
8696 ),
8697 });
8698 }
8699 let recorded_moves =
8700 slot.get(DependencyEdge::RECORDED_KEY) != item.slot.get(DependencyEdge::RECORDED_KEY);
8701
8702 // One `updateIssue` carries all three, because every mutation spends the secondary
8703 // limiter and the title, body and state are one mutation's inputs.
8704 let mut fields = serde_json::Map::new();
8705 if let Some(title) = update.title.as_ref().filter(|title| **title != item.title) {
8706 fields.insert("title".to_owned(), json!(title));
8707 }
8708 if body != held {
8709 fields.insert("body".to_owned(), json!(body));
8710 }
8711 if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.state()) {
8712 fields.insert("stateInput".to_owned(), state_input(Some(&moving.target)));
8713 }
8714 // **The body is written last, and that is the guarantee a refusal part-way keeps.**
8715 // GitHub runs no two requests as one, and runs one document's mutation fields in order
8716 // without undoing an earlier field when a later one fails — so a body written before a
8717 // board field the board then refused would be left changed. Written after every other
8718 // write has landed, a refusal anywhere leaves the item's body, and every metadata key
8719 // it carries, exactly as they stood. So the `Status` option and the `Priority` field go
8720 // first, together in one request — a terminal option selected before the issue
8721 // closes, as a whole write does — then the `blockedBy` difference, then the body.
8722 let mut board_writes: Vec<(&BoardId, (String, Value))> = Vec::new();
8723 let mut clears: Vec<(&BoardId, &str)> = Vec::new();
8724 if let Some(moving) = status_move
8725 .as_ref()
8726 .filter(|moving| moving.moves.option() && !option_selected)
8727 {
8728 board_writes.push((
8729 &moving.board,
8730 (
8731 moving.field.clone(),
8732 json!({"singleSelectOptionId": moving.option}),
8733 ),
8734 ));
8735 }
8736 match &priority_move {
8737 Some((board, PriorityWrite::Select { field, option }, _)) => board_writes.push((
8738 board,
8739 (field.clone(), json!({"singleSelectOptionId": option})),
8740 )),
8741 Some((board, PriorityWrite::Clear { field }, _)) => clears.push((board, field)),
8742 None => {}
8743 }
8744 if let Some((board, writes, _)) = &projection_move {
8745 for write in writes {
8746 let (mut values, mut cleared) = (Vec::new(), Vec::new());
8747 write.join(&mut values, &mut cleared);
8748 board_writes.extend(values.into_iter().map(|value| (board, value)));
8749 clears.extend(cleared.into_iter().map(|field| (board, field)));
8750 }
8751 }
8752 let mut boards: Vec<&BoardId> = board_writes.iter().map(|(board, _)| *board).collect();
8753 boards.extend(clears.iter().map(|(board, _)| *board));
8754 boards.dedup_by(|one, other| one.as_str() == other.as_str());
8755 for board in boards {
8756 let writes = board_writes
8757 .iter()
8758 .filter(|(on, _)| on.as_str() == board.as_str())
8759 .map(|(_, write)| write.clone())
8760 .collect::<Vec<_>>();
8761 let cleared = clears
8762 .iter()
8763 .filter(|(on, _)| on.as_str() == board.as_str())
8764 .map(|(_, field)| *field)
8765 .collect::<Vec<_>>();
8766 self.set_item_fields(board.as_str(), &item.item_id, &writes, &cleared)
8767 .await?;
8768 }
8769 let mut blocked_by_moved = false;
8770 if let Some((native, _)) = &edges
8771 && item.content_kind == ContentKind::Issue
8772 {
8773 blocked_by_moved = self
8774 .reconcile_blocked_by(
8775 &item.id,
8776 native,
8777 Issue::Existing(item.blocked_by.as_deref()),
8778 )
8779 .await?;
8780 }
8781 if !fields.is_empty() {
8782 self.update_content(item.content_kind, &item.id, Value::Object(fields))
8783 .await?;
8784 }
8785
8786 // The `blockedBy` this item was read with is what the write just changed, so it is no
8787 // answer any more: a later write in this command would reconcile against it — re-adding
8788 // what it kept and never removing what it dropped — and a forward-edge read would
8789 // answer with it. Left unknown, each asks GitHub instead.
8790 if blocked_by_moved {
8791 item.blocked_by = None;
8792 }
8793 if let Some(title) = &update.title {
8794 item.title.clone_from(title);
8795 }
8796 item.body = visible.filter(|value| !value.is_empty());
8797 item.raw_body = (!body.is_empty() || item.raw_body.is_some()).then_some(body);
8798 item.slot = slot;
8799 if let Some(delivers) = &update.delivers {
8800 item.delivers.clone_from(delivers);
8801 }
8802 if let Some(moving) = status_move {
8803 item.closed = matches!(moving.target, StatusTarget::Terminal(_, _))
8804 && item.content_kind == ContentKind::Issue;
8805 item.status = moving.landed;
8806 item.option = Some(moving.name);
8807 }
8808 if let Some((_, _, priority)) = priority_move {
8809 item.priority = HeldPriority::Read(priority);
8810 }
8811 if let Some((_, _, projected)) = projection_move {
8812 item.projected = projected;
8813 }
8814 let task = item.task()?;
8815 let mut written = update.changed(&before, &task);
8816 if blocked_by_moved || recorded_moves {
8817 written.insert(UpdatedField::DependsOn);
8818 }
8819 self.remember_written(item, false)?;
8820 Ok(Some(TaskUpdateOutcome {
8821 task,
8822 written,
8823 delivers_before: before.delivers,
8824 }))
8825 }
8826
8827 /// Replace one issue's visible body and its [`MetadataKey::TEMPLATE_KEY`] slot entry
8828 /// together, and nothing else; see [`TaskSource::set_task_rendering`].
8829 ///
8830 /// One update of the body: the content outside the slot, and inside it that one entry,
8831 /// every other entry kept as it was. This source keeps no template answers — an issue has
8832 /// no room beside itself that is not its body, and answers written there would duplicate
8833 /// what the content already says and count against GitHub's body limit — so `answers`
8834 /// reaches nothing here. A body that would not change is not sent at all.
8835 async fn replace_rendering(
8836 &self,
8837 id: &NativeId,
8838 kind: BoardKind,
8839 content: &str,
8840 provenance: &Value,
8841 assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
8842 ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
8843 let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
8844 return Ok(None);
8845 };
8846 let held = item.raw_body.clone().unwrap_or_default();
8847 let mut slot = item.slot.clone();
8848 slot.insert(MetadataKey::TEMPLATE_KEY.to_owned(), provenance.clone());
8849 let rewritten;
8850 let content = if let Some(assets) = assets {
8851 let uploads = self
8852 .upload_assets(item.own_repository.as_ref(), assets)
8853 .await?;
8854 rewritten =
8855 onetaskgraph_plugin_api::serve_asset_references(content, &mut slot, &uploads);
8856 rewritten.as_str()
8857 } else {
8858 content
8859 };
8860 let body = with_slot(&with_content(&held, &self.marked(content))?, &slot)?;
8861 // Checked before anything is sent, as a content write checks it.
8862 let (visible, read) = self.visible(Some(body.clone()))?;
8863 if visible.as_deref().unwrap_or_default() != content || read != slot {
8864 return Err(SourceError::Refused {
8865 message: format!(
8866 "this content ends in what source {} reads as its own metadata slot \
8867 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8868 as content; next: remove that trailing block from the template",
8869 self.name
8870 ),
8871 });
8872 }
8873 if body != held {
8874 self.update_content(item.content_kind, &item.id, json!({"body": body}))
8875 .await?;
8876 }
8877 item.body = visible.filter(|value| !value.is_empty());
8878 item.raw_body = Some(body);
8879 item.slot = read;
8880 self.remember_written(item, false)?;
8881 Ok(Some(onetaskgraph_plugin_api::AssetsWritten {
8882 id: id.clone(),
8883 content: Some(content.to_owned()),
8884 }))
8885 }
8886
8887 async fn set_item_field(
8888 &self,
8889 board_id: &str,
8890 item_id: &str,
8891 field_id: &str,
8892 value: Value,
8893 ) -> Result<(), SourceError> {
8894 let data = self
8895 .graphql(
8896 graphql::UPDATE_FIELD,
8897 json!({"input":{
8898 "projectId":board_id,"itemId":item_id,"fieldId":field_id,"value":value
8899 },"readPriority":false,"priorityName":PRIORITY_FIELD}),
8900 )
8901 .await?;
8902 field_update_item(&data, item_id).map(|_| ())
8903 }
8904
8905 /// GitHub accepts one value per field mutation; aliases combine those mutations in
8906 /// one request — writes in the order given, then clears. A lone write is
8907 /// [`graphql::UPDATE_FIELD`] and a lone clear [`graphql::CLEAR_FIELD`]; more than one
8908 /// request's slots hold go in further requests of the same document, in order. Every
8909 /// returned item id is checked, including optional aliases.
8910 async fn set_item_fields(
8911 &self,
8912 board: &str,
8913 item: &str,
8914 fields: &[(String, Value)],
8915 clears: &[&str],
8916 ) -> Result<(), SourceError> {
8917 match (fields, clears) {
8918 ([], []) => return Ok(()),
8919 ([(field, value)], []) => {
8920 return self.set_item_field(board, item, field, value.clone()).await;
8921 }
8922 ([], [field]) => {
8923 return self
8924 .write_priority(
8925 board,
8926 item,
8927 &PriorityWrite::Clear {
8928 field: (*field).to_owned(),
8929 },
8930 )
8931 .await;
8932 }
8933 _ => {}
8934 }
8935 // Any field of the request stands in for an input a slot leaves out, which GitHub
8936 // still requires to be well formed although it never runs it.
8937 let filler = fields
8938 .first()
8939 .map_or_else(|| clears[0], |(field, _)| field.as_str());
8940 let mut writes = fields.iter();
8941 let mut cleared = clears.iter();
8942 loop {
8943 let batch: Vec<&(String, Value)> =
8944 writes.by_ref().take(FIELD_WRITE_SLOTS.len()).collect();
8945 let batch_clears: Vec<&&str> = cleared.by_ref().take(FIELD_CLEAR_SLOTS.len()).collect();
8946 if batch.is_empty() && batch_clears.is_empty() {
8947 return Ok(());
8948 }
8949 let mut variables = serde_json::Map::new();
8950 for (index, slot) in FIELD_WRITE_SLOTS.iter().enumerate() {
8951 let input = match batch.get(index) {
8952 Some((field, value)) => {
8953 json!({"projectId":board,"itemId":item,"fieldId":field,"value":value})
8954 }
8955 None => json!({"projectId":board,"itemId":item,"fieldId":filler,
8956 "value":{"text":""}}),
8957 };
8958 variables.insert(slot.variable.to_owned(), input);
8959 variables.insert(slot.include.to_owned(), json!(index < batch.len()));
8960 }
8961 for (index, slot) in FIELD_CLEAR_SLOTS.iter().enumerate() {
8962 let field = batch_clears.get(index).map_or(filler, |field| **field);
8963 variables.insert(
8964 slot.variable.to_owned(),
8965 json!({"projectId":board,"itemId":item,"fieldId":field}),
8966 );
8967 variables.insert(slot.include.to_owned(), json!(index < batch_clears.len()));
8968 }
8969 let data = self
8970 .graphql(graphql::UPDATE_FIELDS, Value::Object(variables))
8971 .await?;
8972 let sent = FIELD_WRITE_SLOTS[..batch.len()]
8973 .iter()
8974 .chain(&FIELD_CLEAR_SLOTS[..batch_clears.len()]);
8975 for slot in sent {
8976 let alias = slot.alias;
8977 let returned = data
8978 .get(alias)
8979 .and_then(|value| value.get("projectV2Item"))
8980 .ok_or_else(|| SourceError::Malformed {
8981 message: format!("GitHub field update {alias} returned no project item"),
8982 })?;
8983 if required_str(returned, "id")? != item {
8984 return Err(SourceError::Malformed {
8985 message: format!(
8986 "GitHub field update {alias} returned the wrong project item"
8987 ),
8988 });
8989 }
8990 }
8991 }
8992 }
8993
8994 async fn native_dependency_ids(&self, id: &NativeId) -> Result<Vec<String>, SourceError> {
8995 let mut after: Option<String> = None;
8996 let mut ids = Vec::new();
8997 loop {
8998 let data = self
8999 .graphql(
9000 graphql::ISSUE_DEPENDENCIES,
9001 json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after}),
9002 )
9003 .await?;
9004 let connection =
9005 data.pointer("/node/blockedBy")
9006 .ok_or_else(|| SourceError::Malformed {
9007 message: "GitHub dependency response has no blockedBy connection".into(),
9008 })?;
9009 ids.extend(
9010 connection
9011 .get("nodes")
9012 .and_then(Value::as_array)
9013 .ok_or_else(|| SourceError::Malformed {
9014 message: "GitHub dependency response nodes is not an array".into(),
9015 })?
9016 .iter()
9017 .map(|value| required_str(value, "id").map(str::to_owned))
9018 .collect::<Result<Vec<_>, _>>()?,
9019 );
9020 let next = next_cursor(connection)?;
9021 if let Some(next) = &next {
9022 validate_cursor_progress(after.as_deref(), &next.0)?;
9023 }
9024 after = next.map(|cursor| cursor.0);
9025 if after.is_none() {
9026 return Ok(ids);
9027 }
9028 }
9029 }
9030
9031 async fn dependencies(
9032 &self,
9033 id: &NativeId,
9034 near_kind: ItemKind,
9035 direction: Direction,
9036 page: &PageRequest,
9037 ) -> Result<Page<DependencyEdge>, SourceError> {
9038 validate_page(page)?;
9039 let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
9040 let cursor = page.cursor.as_ref().map(|c| c.0.as_str());
9041 let recorded = recorded_offset(cursor, direction)?;
9042 // What this issue is blocked by, when a read of it by its own id in this command
9043 // already carried the whole connection — a copy reads the item it writes before it
9044 // reads its edges — and the page asked for is the whole of it, or the recorded tail
9045 // after it. Answered from that read, in the shape the dependency read answers in;
9046 // anything else is asked of GitHub.
9047 let carried = match direction {
9048 Direction::DependsOn => self
9049 .resolved_cache()?
9050 .get(id)
9051 .filter(|item| item.content_kind == ContentKind::Issue)
9052 .and_then(|item| Some((item.blocked_by.clone()?, item.raw_body.clone()))),
9053 Direction::DependedOnBy => None,
9054 }
9055 .filter(|(nodes, _)| recorded.is_some() || (cursor.is_none() && nodes.len() <= limit));
9056 // Asked for even in the recorded phase, whose page reads nothing from the
9057 // connection: `__typename` is what says whether this item has a native
9058 // relationship at all, and that is what decides which far ends the reserved key is
9059 // allowed to hold.
9060 let data = match carried {
9061 Some((nodes, body)) => json!({"node":{"__typename":"Issue","body":body,
9062 "blockedBy":{"nodes":nodes,"pageInfo":{"hasNextPage":false,"endCursor":null}}}}),
9063 None => {
9064 let after = if recorded.is_some() { None } else { cursor };
9065 let first = page.limit.min(MAX_PAGE_SIZE);
9066 match direction {
9067 Direction::DependsOn => {
9068 self.graphql(
9069 graphql::ISSUE_DEPENDS_ON,
9070 json!({"id":id.0,"first":first,"after":after,"owner":self.owner,
9071 "number":self.project_number,
9072 "boardItems":BOARD_ITEMS_PAGE_SIZE}),
9073 )
9074 .await?
9075 }
9076 Direction::DependedOnBy => {
9077 self.graphql(
9078 graphql::ISSUE_DEPENDED_ON_BY,
9079 json!({"id":id.0,"first":first,"after":after}),
9080 )
9081 .await?
9082 }
9083 }
9084 }
9085 };
9086 let draft_slot = draft_slot(&data, id, self.project_number)?;
9087 let node =
9088 data.get("node")
9089 .filter(|v| !v.is_null())
9090 .ok_or_else(|| SourceError::Refused {
9091 message: format!(
9092 "GitHub item {} was not found or does not support dependencies",
9093 id.0
9094 ),
9095 })?;
9096 let connection_name = match direction {
9097 Direction::DependsOn => "blockedBy",
9098 Direction::DependedOnBy => "blocking",
9099 };
9100 // A draft has neither `blockedBy` nor `blocking`, so nothing it depends on can be
9101 // named natively and the reserved key may hold any far end. An issue's connections
9102 // hold issues, and this source reads them at the near item's own level.
9103 let natively_names = (required_str(node, "__typename")? == "Issue").then_some(near_kind);
9104 if let Some(offset) = recorded {
9105 return Ok(recorded_page(
9106 self.recorded_edges(
9107 id,
9108 near_kind,
9109 direction,
9110 natively_names,
9111 node,
9112 draft_slot.as_ref(),
9113 )
9114 .await?,
9115 offset,
9116 limit,
9117 ));
9118 }
9119 if natively_names.is_none() {
9120 return Ok(recorded_page(
9121 self.recorded_edges(
9122 id,
9123 near_kind,
9124 direction,
9125 natively_names,
9126 node,
9127 draft_slot.as_ref(),
9128 )
9129 .await?,
9130 0,
9131 limit,
9132 ));
9133 }
9134 let connection = node
9135 .get(connection_name)
9136 .ok_or_else(|| SourceError::Malformed {
9137 message: "GitHub dependency response is missing its connection".into(),
9138 })?;
9139 let nodes = connection
9140 .get("nodes")
9141 .and_then(Value::as_array)
9142 .ok_or_else(|| SourceError::Malformed {
9143 message: "GitHub dependency response nodes is not an array".into(),
9144 })?;
9145 // `from` depends on `to`, always. GitHub spells the same relationship from either
9146 // end — `blockedBy` lists what this item waits on, `blocking` lists what waits on
9147 // it — so the near item is `from` in one direction and `to` in the other.
9148 let items = nodes
9149 .iter()
9150 .map(|value| {
9151 let related = NativeId(required_str(value, "id")?.into());
9152 let related_kind = related_kind(value)?;
9153 let (from, to) = match direction {
9154 Direction::DependsOn => (
9155 DependencyEndpoint::from_native(id.clone(), near_kind),
9156 DependencyEndpoint::from_native(related, related_kind),
9157 ),
9158 Direction::DependedOnBy => (
9159 DependencyEndpoint::from_native(related, related_kind),
9160 DependencyEndpoint::from_native(id.clone(), near_kind),
9161 ),
9162 };
9163 Ok(DependencyEdge {
9164 from,
9165 to,
9166 kind: DependencyKind::Blocks,
9167 })
9168 })
9169 .collect::<Result<Vec<_>, SourceError>>()?;
9170 let mut next = next_cursor(connection)?;
9171 if let Some(next) = &next {
9172 validate_cursor_progress(cursor, &next.0)?;
9173 }
9174 // The recorded far ends ride on the last page of `blockedBy`, whose answer carried the
9175 // body they are recorded in, as far as the page has room — so they cost no request of
9176 // their own — and only what does not fit is resumed by a cursor.
9177 let mut items = items;
9178 if next.is_none() {
9179 let recorded = self
9180 .recorded_edges(
9181 id,
9182 near_kind,
9183 direction,
9184 natively_names,
9185 node,
9186 draft_slot.as_ref(),
9187 )
9188 .await?;
9189 let room = limit.saturating_sub(items.len());
9190 let total = recorded.len();
9191 items.extend(recorded.into_iter().take(room));
9192 if total > room {
9193 next = Some(Cursor(format!("{RECORDED_CURSOR}{room}")));
9194 }
9195 }
9196 Ok(Page { items, next })
9197 }
9198
9199 /// The edges this item records under [`DependencyEdge::RECORDED_KEY`], which is where
9200 /// a far end in another source has to live: no GitHub issue relationship can name one.
9201 ///
9202 /// Only forwards. The reverse of a recorded edge is derived from the far end, and this
9203 /// source never writes one down.
9204 ///
9205 /// The metadata lives in the item's own body slot, and `node` is the dependency read's
9206 /// own answer, which carries an issue's body — so an issue's recorded edges cost no
9207 /// request beyond the read already made, and reading the board for them would be a
9208 /// walk of every item for one field of one. A draft has no body in that answer, because
9209 /// a draft is not an issue, so a draft's are read off its own read by id — never off a
9210 /// listing of the board, which can be behind on the very item asked about.
9211 async fn recorded_edges(
9212 &self,
9213 id: &NativeId,
9214 near_kind: ItemKind,
9215 direction: Direction,
9216 natively_names: Option<ItemKind>,
9217 node: &Value,
9218 draft_slot: Option<&Option<BTreeMap<String, Value>>>,
9219 ) -> Result<Vec<DependencyEdge>, SourceError> {
9220 if direction != Direction::DependsOn {
9221 return Ok(Vec::new());
9222 }
9223 let slot = match (node.get("body"), draft_slot) {
9224 (Some(body), _) if natively_names.is_some() => {
9225 metadata_body(body.as_str().map(str::to_owned))?.1
9226 }
9227 // A draft the dependency read itself answered: this board's, or none of its.
9228 (_, Some(Some(slot))) => slot.clone(),
9229 (_, Some(None)) => return Ok(Vec::new()),
9230 _ => {
9231 let Some(item) = self.bound_item(id).await? else {
9232 return Ok(Vec::new());
9233 };
9234 item.slot
9235 }
9236 };
9237 DependencyEdge::recorded(&slot, id, near_kind, &self.name, natively_names)
9238 .map_err(|message| SourceError::Malformed { message })
9239 }
9240
9241 /// Why `creation_owners` refuses a create filed in `target`, or `None` when it allows it —
9242 /// every create when the list is empty.
9243 fn owner_refusal(&self, target: &RepositoryTarget) -> Option<String> {
9244 if self.creation_owners.is_empty()
9245 || self
9246 .creation_owners
9247 .iter()
9248 .any(|owner| owner.eq_ignore_ascii_case(&target.owner))
9249 {
9250 return None;
9251 }
9252 Some(format!(
9253 "source {} would create the issue in repository {}, owned by {}, and its \
9254 creation_owners allows only repositories of {}",
9255 self.name,
9256 target.slug(),
9257 target.owner,
9258 self.creation_owners.join(", ")
9259 ))
9260 }
9261
9262 fn configured_repository(&self) -> Result<&RepositoryTarget, SourceError> {
9263 self.repository
9264 .as_ref()
9265 .ok_or_else(|| SourceError::Refused {
9266 message: format!(
9267 "source {} has no repository configured, and a GitHub Projects board has no \
9268 repository of its own to create an issue in; set repository: owner/name on \
9269 this source",
9270 self.name
9271 ),
9272 })
9273 }
9274
9275 /// The repository one new issue is created in, under the rule [`RepositoryTarget`]
9276 /// states.
9277 ///
9278 /// The fallback is demanded first, whichever arm answers: a write without a configured
9279 /// repository is refused naming the field exactly as it was before the rule existed,
9280 /// so a source that could not write before cannot write now, rather than writing for
9281 /// the one item whose own field happens to decide it.
9282 ///
9283 /// Everything this refuses is refused before `createIssue`, so a refusal leaves no
9284 /// issue behind: an entry that is not a repository on [`RepositoryTarget::HOST`], an
9285 /// entry owned by someone other than the owner of the parent issue's repository —
9286 /// GitHub accepts a sub-issue from another repository of the same owner and from no
9287 /// other, so `addSubIssue` would refuse it after the issue existed — a parent the
9288 /// board does not hold, and a parent that is a draft, which GitHub gives no sub-issues,
9289 /// both of which `addSubIssue` would likewise refuse too late. Whether the entry exists
9290 /// and is visible to the token is checked where its node id is resolved, still before
9291 /// `createIssue`. The parent is read by its own id through [`Self::item_by_id`] — never
9292 /// looked up in a listing of the board, which can be minutes behind an issue its own
9293 /// `projectItems` already places on it — and that read answers first from this process's
9294 /// own record, so a project created moments ago in this command answers though GitHub
9295 /// has not caught up.
9296 async fn creation_target(
9297 &self,
9298 incoming: &Incoming<'_>,
9299 ) -> Result<RepositoryTarget, SourceError> {
9300 let what = format!(
9301 "{} {:?}",
9302 incoming.written.kind().describes(),
9303 incoming.title
9304 );
9305 let target = self
9306 .creation_rule(incoming.repositories, incoming.parent, &what)
9307 .await?;
9308 // The engine asks `creation_refusal` before anything of a copy is written; this holds
9309 // every other create to the same restriction, before `createIssue`.
9310 match self.owner_refusal(&target) {
9311 Some(message) => Err(SourceError::Refused { message }),
9312 None => Ok(target),
9313 }
9314 }
9315
9316 /// [`Self::creation_target`]'s rule over what it reads of the item — its `repositories`
9317 /// and the project issue it is filed under — with `what` naming the item in a refusal: the
9318 /// one place the rule is, so the create, [`TaskSource::creation_repository`] and the
9319 /// write-time read of who can read the issue ([`TaskSource::visibility`]) cannot answer
9320 /// differently.
9321 ///
9322 /// On a source declared private, an item's one named repository is read for its
9323 /// visibility first — once per engine hold, see the `visibility` module — and one that
9324 /// does not read private decides nothing: the issue is created in the parent's
9325 /// repository, else the configured one. A read that cannot be answered refuses.
9326 async fn creation_rule(
9327 &self,
9328 repositories: &[Repository],
9329 parent: Option<&NativeId>,
9330 what: &str,
9331 ) -> Result<RepositoryTarget, SourceError> {
9332 let parent = match parent {
9333 Some(parent) => Some(self.bound_item(parent).await?.ok_or_else(|| {
9334 SourceError::Refused {
9335 message: format!(
9336 "GitHub project issue {} was not found on the board of source {}, so {} \
9337 cannot be filed under it",
9338 parent.0, self.name, what
9339 ),
9340 }
9341 })?),
9342 None => None,
9343 };
9344 let parents_repository = parent
9345 .as_ref()
9346 .map(|parent| {
9347 // A draft is on the board and so is found, but it has no repository to
9348 // place a task in and GitHub gives it no sub-issues, so `addSubIssue`
9349 // would refuse the task only once `createIssue` had made it.
9350 if parent.content_kind == ContentKind::DraftIssue {
9351 return Err(SourceError::Refused {
9352 message: format!(
9353 "GitHub project item {} on the board of source {} is a draft, \
9354 which cannot have sub-issues, so {} cannot be filed under it",
9355 parent.id.0, self.name, what
9356 ),
9357 });
9358 }
9359 // An issue's repository is where a sub-issue is placed and whose owner it
9360 // is compared against, so a parent whose repository this source cannot
9361 // spell as `owner/name` — GitHub's login grammar is wider than this
9362 // source's floor — is one nothing can be filed under.
9363 parent
9364 .own_repository
9365 .as_ref()
9366 .and_then(|origin| RepositoryTarget::from_origin(origin).ok())
9367 .ok_or_else(|| SourceError::Malformed {
9368 message: format!(
9369 "GitHub project issue {} on the board of source {} is in {}, which \
9370 is not a {}/owner/name repository this source can place {} in",
9371 parent.id.0,
9372 self.name,
9373 parent
9374 .own_repository
9375 .as_ref()
9376 .map_or("no repository", Repository::as_str),
9377 RepositoryTarget::HOST,
9378 what
9379 ),
9380 })
9381 })
9382 .transpose()?;
9383 self.creation_rule_in_repository(repositories, parents_repository, what)
9384 .await
9385 }
9386
9387 async fn creation_rule_in_repository(
9388 &self,
9389 repositories: &[Repository],
9390 parents_repository: Option<RepositoryTarget>,
9391 what: &str,
9392 ) -> Result<RepositoryTarget, SourceError> {
9393 let fallback = self.configured_repository()?;
9394 match repositories {
9395 [named] => {
9396 let target =
9397 RepositoryTarget::from_origin(named).map_err(|_| SourceError::Refused {
9398 message: format!(
9399 "{} names repository {}, which is not a {}/owner/name repository \
9400 source {} can create an issue in; name one that is, or name none",
9401 what,
9402 named.as_str(),
9403 RepositoryTarget::HOST,
9404 self.name
9405 ),
9406 })?;
9407 // On a source declared private, an issue whose one named repository does not
9408 // read private is created where an item naming none is: the item still names
9409 // that repository in its metadata slot, and its issue stays private.
9410 if self.is_declared_private()
9411 && self.repository_visibility(&target).await?
9412 != onetaskgraph_plugin_api::Visibility::Private
9413 {
9414 return Ok(parents_repository.unwrap_or_else(|| fallback.clone()));
9415 }
9416 if let Some(parents) = &parents_repository
9417 && parents.owner != target.owner
9418 {
9419 return Err(SourceError::Refused {
9420 message: format!(
9421 "{} names repository {}, owned by {}, but its project's issue is in \
9422 {}, owned by {}, and GitHub files a sub-issue only in a repository \
9423 of the same owner as its parent issue; name a repository of {}, or \
9424 name none",
9425 what,
9426 target.slug(),
9427 target.owner,
9428 parents.slug(),
9429 parents.owner,
9430 parents.owner
9431 ),
9432 });
9433 }
9434 Ok(target)
9435 }
9436 _ => Ok(parents_repository.unwrap_or_else(|| fallback.clone())),
9437 }
9438 }
9439
9440 /// The node id of the repository `incoming` is being created in, or the refusal naming
9441 /// the item and the repository the token cannot see.
9442 ///
9443 /// Resolved once per command per repository; see [`Self::repository_cache`].
9444 async fn repository_id(
9445 &self,
9446 repository: &RepositoryTarget,
9447 incoming: &Incoming<'_>,
9448 ) -> Result<String, SourceError> {
9449 if let Some(id) = self.repository_cache()?.get(repository).cloned() {
9450 return Ok(id);
9451 }
9452 let data = self
9453 .graphql(
9454 graphql::REPOSITORY,
9455 json!({"owner":repository.owner,"name":repository.name}),
9456 )
9457 .await?;
9458 self.repository_read(&data, repository, incoming)
9459 }
9460
9461 /// The repository's node id out of an answer carrying the `repository` root, held for
9462 /// the rest of this command, or the refusal naming the item that cannot be created in it.
9463 fn repository_read(
9464 &self,
9465 data: &Value,
9466 repository: &RepositoryTarget,
9467 incoming: &Incoming<'_>,
9468 ) -> Result<String, SourceError> {
9469 let node = data
9470 .get("repository")
9471 .filter(|value| !value.is_null())
9472 .ok_or_else(|| SourceError::Refused {
9473 message: format!(
9474 "GitHub repository {} was not found or is not visible to the token, so {} \
9475 {:?} cannot be created in it",
9476 repository.slug(),
9477 incoming.written.kind().describes(),
9478 incoming.title
9479 ),
9480 })?;
9481 let id = required_str(node, "id")?.to_owned();
9482 self.repository_cache()?
9483 .insert(repository.clone(), id.clone());
9484 Ok(id)
9485 }
9486
9487 fn repository_cache(
9488 &self,
9489 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<RepositoryTarget, String>>, SourceError> {
9490 self.repository_cache
9491 .lock()
9492 .map_err(|_| SourceError::Unavailable {
9493 message: "this source's record of the destination repository was left \
9494 inconsistent by an earlier failure; next: run the command again"
9495 .into(),
9496 })
9497 }
9498
9499 /// Create or update one board item, whichever kind it is.
9500 async fn write_item(
9501 &self,
9502 incoming: &Incoming<'_>,
9503 target: Option<&NativeId>,
9504 depends_on: &[DependencyEdge],
9505 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
9506 let prepared = self.prepare_write(incoming, target, depends_on).await?;
9507 let landed = match &prepared.destination {
9508 // Its content is written last, by `complete_write`, once everything else has landed.
9509 WriteDestination::Existing(item) => Landed {
9510 content_id: item.id.clone(),
9511 item_id: Some(item.item_id.clone()),
9512 url: item.url.clone(),
9513 number: item.number,
9514 },
9515 WriteDestination::New(target) => {
9516 if self.deferring()? {
9517 self.create_filed_issue(
9518 prepared.board.id.as_str(),
9519 target,
9520 incoming,
9521 &prepared.body,
9522 )
9523 .await?
9524 } else {
9525 self.create_and_file_issue(
9526 prepared.board.id.as_str(),
9527 target,
9528 incoming,
9529 &prepared.body,
9530 )
9531 .await?
9532 }
9533 }
9534 };
9535 self.complete_write(incoming, prepared, landed).await
9536 }
9537
9538 /// Everything one write of `incoming` reads and decides before it sends anything: what is
9539 /// there now, where a new item is created, the board's fields, and every value refused while
9540 /// nothing has been written — the item's body included.
9541 async fn prepare_write(
9542 &self,
9543 incoming: &Incoming<'_>,
9544 target: Option<&NativeId>,
9545 depends_on: &[DependencyEdge],
9546 ) -> Result<Prepared, SourceError> {
9547 // Refused before anything is read or written: a task or a project titled the way
9548 // this board spells a document would land as an issue this same source reads back
9549 // as a document, so the field this destination cannot carry is named rather than
9550 // written and silently reclassified.
9551 if let Written::Work(kind, _) = incoming.written
9552 && incoming.title.starts_with(DESIGN_TITLE_PREFIX)
9553 {
9554 return Err(SourceError::Refused {
9555 message: format!(
9556 "the title of this {} begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
9557 spells a document, so it would read back as one rather than as a {}; \
9558 retitle it, or copy it as a document",
9559 kind.marker(),
9560 self.name,
9561 kind.marker()
9562 ),
9563 });
9564 }
9565 // The destination is read by its own id, and whether this board holds it is decided
9566 // by that read — its own `projectItems` — rather than by whether a listing of the
9567 // board happens to include it yet. See the module documentation.
9568 // An existing issue is never moved; a new one is created where the rule says — and
9569 // knowing where is what lets the board's fields and that repository's id be read
9570 // together, before anything below needs either.
9571 let destination = match target {
9572 Some(target) => {
9573 WriteDestination::Existing(Box::new(self.bound_item(target).await?.ok_or_else(
9574 || SourceError::Refused {
9575 message: format!("GitHub destination item {} was not found", target.0),
9576 },
9577 )?))
9578 }
9579 None => {
9580 let target = self.creation_target(incoming).await?;
9581 self.creation_context(&target, incoming).await?;
9582 WriteDestination::New(target)
9583 }
9584 };
9585 let existing = destination.existing();
9586 let board = self
9587 .fields_for(
9588 existing,
9589 incoming.metadata.contains_key(ORIGIN_KEY),
9590 incoming.written.status().is_some(),
9591 incoming
9592 .priority
9593 .is_some_and(|priority| priority != Priority::None),
9594 )
9595 .await?;
9596 let status_target = incoming
9597 .written
9598 .work_status()
9599 .map(|(kind, status)| self.resolved_target(kind, status.category))
9600 .transpose()?;
9601 let column = match (incoming.written.work_status(), status_target.as_ref()) {
9602 (Some((kind, status)), Some(target)) => {
9603 self.column_for(&board.fields, kind, status.category, target)?
9604 }
9605 _ => None,
9606 };
9607 // Resolved before anything is created, for the reason the column above is: a
9608 // priority this board has no option for is refused while nothing has been written.
9609 let priority_write = match incoming.priority {
9610 Some(priority) => self.priority_write(&board.fields, existing, priority)?,
9611 None => None,
9612 };
9613 // And for the reason the priority is: a value no projected field can hold, or a board
9614 // that cannot hold the field, is refused while nothing has been written.
9615 let (projections, projected) = self.projection_writes(
9616 &board.fields,
9617 existing.map(|item| &item.projected),
9618 incoming.metadata,
9619 None,
9620 )?;
9621 let content_kind = existing.map_or(ContentKind::Issue, |item| item.content_kind);
9622 if content_kind == ContentKind::DraftIssue {
9623 if let (Some(StatusTarget::Terminal(_, _)), Some(status)) =
9624 (status_target.as_ref(), incoming.written.status())
9625 {
9626 return Err(self.closes_a_draft(status.category));
9627 }
9628 if incoming.parent.is_some() {
9629 return Err(SourceError::Refused {
9630 message: "GitHub draft items cannot be a project's sub-issue".into(),
9631 });
9632 }
9633 }
9634 match existing {
9635 Some(item) if content_kind == ContentKind::Issue => {
9636 if item.labels != incoming.labels {
9637 return Err(SourceError::Refused {
9638 message: "GitHub issue labels differ from the labels being written".into(),
9639 });
9640 }
9641 }
9642 _ => {
9643 if !incoming.labels.is_empty() {
9644 return Err(SourceError::Refused {
9645 message: "GitHub items created by this destination carry no labels".into(),
9646 });
9647 }
9648 }
9649 }
9650
9651 // The repository the issue really lives in is what the slot below is written against,
9652 // so a single entry that is where the issue is created travels as no key at all, and
9653 // the read side derives it back from the issue.
9654 let own_repository = match &destination {
9655 WriteDestination::Existing(item) => item.own_repository.clone(),
9656 WriteDestination::New(target) => Some(
9657 Repository::try_from(target.origin())
9658 .map_err(|message| SourceError::Config { message })?,
9659 ),
9660 };
9661 let (native, fallback) = self
9662 .partition_edges(
9663 incoming.written.kind(),
9664 content_kind,
9665 existing.and_then(|item| item.blocked_by.as_deref()),
9666 depends_on,
9667 )
9668 .await?;
9669 let mut slot = slot_metadata(incoming, own_repository.as_ref(), &fallback);
9670 let content = match incoming.assets {
9671 Some(assets) => {
9672 let uploads = self.upload_assets(own_repository.as_ref(), assets).await?;
9673 let rewritten = onetaskgraph_plugin_api::serve_asset_references(
9674 incoming.content.unwrap_or_default(),
9675 &mut slot,
9676 &uploads,
9677 );
9678 incoming.content.map(|_| rewritten)
9679 }
9680 None => incoming.content.map(str::to_owned),
9681 };
9682 let marked = content.as_deref().map(|content| self.marked(content));
9683 let body = compose_body(marked.as_deref(), &slot)?;
9684 // Read before anything is created, for the reason the field below is: a value
9685 // this destination cannot store has to refuse, and refusing after `createIssue`
9686 // would leave an issue behind that nothing asked for. The engine writes a
9687 // qualified id here; a caller handing this key anything else is told so rather
9688 // than having it silently stored as no origin at all.
9689 // 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.
9690 let origin = match incoming.metadata.get(ORIGIN_KEY) {
9691 None => "",
9692 Some(Value::String(origin)) => origin.as_str(),
9693 Some(other) => {
9694 return Err(SourceError::Refused {
9695 message: format!(
9696 "{ORIGIN_KEY} holds a qualified id spelled as a string, and this item's \
9697 is {other}"
9698 ),
9699 });
9700 }
9701 };
9702 // Resolved before anything is created: a board that cannot carry the copy origin
9703 // has to refuse the write, and refusing it after `createIssue` would leave an
9704 // issue behind that nothing asked for.
9705 let origin_field = match Board::field(&board.fields, ORIGIN_FIELD)? {
9706 Some(field) => {
9707 if required_str(field, "__typename")? != "ProjectV2Field" {
9708 return Err(SourceError::Refused {
9709 message: format!(
9710 "GitHub board source-owned {ORIGIN_FIELD} field is not a text field"
9711 ),
9712 });
9713 }
9714 Some(required_str(field, "id")?.to_owned())
9715 }
9716 None if incoming.metadata.contains_key(ORIGIN_KEY) => {
9717 return Err(SourceError::Refused {
9718 message: format!(
9719 "GitHub board has no source-owned {ORIGIN_FIELD} text field, and the \
9720 item carries {ORIGIN_KEY}; add a text field named {ORIGIN_FIELD} to \
9721 the board"
9722 ),
9723 });
9724 }
9725 None => None,
9726 };
9727
9728 Ok(Prepared {
9729 origin: origin.to_owned(),
9730 destination,
9731 board,
9732 status_target,
9733 column,
9734 priority_write,
9735 projections,
9736 projected,
9737 content_kind,
9738 own_repository,
9739 native,
9740 slot,
9741 content,
9742 body,
9743 origin_field,
9744 })
9745 }
9746
9747 /// Everything one write does once the item it writes exists: its board fields, its
9748 /// relationships and — for an item that was already there — its content last, then what this
9749 /// process remembers of it.
9750 ///
9751 /// While this source is holding writes back — see [`TaskSource::begin_deferred_writes`] — an
9752 /// item this command created has its fields, its closing and its edges held for
9753 /// [`TaskSource::finish_writes`] rather than sent here.
9754 async fn complete_write(
9755 &self,
9756 incoming: &Incoming<'_>,
9757 prepared: Prepared,
9758 landed: Landed,
9759 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
9760 let Prepared {
9761 destination,
9762 board,
9763 status_target,
9764 column,
9765 priority_write,
9766 projections,
9767 projected,
9768 content_kind,
9769 own_repository,
9770 native,
9771 slot,
9772 content,
9773 body,
9774 origin,
9775 origin_field,
9776 } = prepared;
9777 let existing = destination.existing();
9778 let origin = origin.as_str();
9779 let Landed {
9780 content_id,
9781 item_id,
9782 url,
9783 number,
9784 } = landed;
9785
9786 // Only what differs from the item as this write read it is sent: a priority the item
9787 // already holds is not, so an unchanged re-copy writes no field and a person's edit of
9788 // it since that read is left alone.
9789 let priority_write = priority_write.filter(|_| {
9790 !existing.is_some_and(|item| {
9791 incoming.priority.map(HeldPriority::Read) == Some(item.priority.clone())
9792 })
9793 });
9794 let written_option = column.as_ref().map(|(_, _, name)| name.clone());
9795 let column = column
9796 .filter(|(_, _, name)| existing.is_none_or(|item| item.option.as_ref() != Some(name)))
9797 .map(|(field, option, _)| (field, option));
9798 // Creating an item here is several calls — `createIssue`, which files it on the
9799 // board, then its board fields, the parent and the dependencies — and GitHub can fail
9800 // at any of them. Everything this source can refuse *before* the first of those is
9801 // already checked above, so what is left is GitHub itself failing part way. When it
9802 // does over an item this call created, the issue is taken back: a write that
9803 // refused must not leave an item behind that nobody asked for, and one that does
9804 // makes the retry create a second.
9805 // Whether the board-field write carrying a moved origin was answered as landing whole.
9806 // When it was refused, GitHub does not say which of its fields ran before the one that
9807 // failed, so the origin may or may not have moved.
9808 let mut origin_landed = false;
9809 // An item this command created while writes are held back has every part that can land
9810 // later held for `finish_writes` — its own create filed it on the board and under its
9811 // project already — and so does a later write of it in the same command.
9812 let holding = (existing.is_none() && self.deferring()?) || self.holds_back(&content_id)?;
9813 let landed = if holding {
9814 self.hold_back(
9815 &board.id,
9816 &content_id,
9817 existing,
9818 origin_field.as_deref(),
9819 origin,
9820 column,
9821 status_target.as_ref(),
9822 priority_write.as_ref(),
9823 &projections,
9824 &native,
9825 )
9826 } else if let Some(item_id) = item_id.as_deref() {
9827 self.finish_write(
9828 board.id.as_str(),
9829 incoming,
9830 &content_id,
9831 item_id,
9832 content_kind,
9833 existing,
9834 origin_field.as_deref(),
9835 origin,
9836 column,
9837 status_target.as_ref(),
9838 priority_write.as_ref(),
9839 &projections,
9840 &native,
9841 &mut origin_landed,
9842 )
9843 .await
9844 } else {
9845 // An issue a create filed on the board itself has no board item this process
9846 // knows until `finish_writes` reads it, so nothing but a held-back write may land
9847 // on it before then.
9848 Err(SourceError::Malformed {
9849 message: format!(
9850 "issue {} was written before the board item GitHub filed it as was read",
9851 content_id.0
9852 ),
9853 })
9854 };
9855 // An existing item's title, body and state go last, in one `updateIssue`, once its board
9856 // fields and its relationships have landed: a refusal of any of those then leaves its
9857 // body — and the metadata slot inside it — exactly as it stood.
9858 let landed = match (landed, existing) {
9859 (Ok(()), Some(item)) => {
9860 self.update_existing(item, incoming, &body, status_target.as_ref())
9861 .await
9862 }
9863 (landed, _) => landed,
9864 };
9865 if let Err(error) = landed {
9866 match existing {
9867 // Best effort, and the write's own failure is what the caller is told: a
9868 // refusal naming the tidy-up would hide why the write failed at all.
9869 None => {
9870 let _ = self.delete_issue(&content_id).await;
9871 }
9872 // The origin field is the one piece of an existing item's metadata written
9873 // before its body, so a write refused after it puts it back as it was. When
9874 // that is refused too, the write's own failure is still what the caller is
9875 // told — with what it left behind added, because the item's metadata is then
9876 // not as it stood and a caller retrying has to know which key moved.
9877 Some(item) => {
9878 let before = item.origin.as_deref().unwrap_or("");
9879 if let Some(field) = origin_field.as_deref()
9880 && before != origin
9881 && let Err(restore) = self
9882 .set_item_field(
9883 board.id.as_str(),
9884 &item.item_id,
9885 field,
9886 json!({"text": before}),
9887 )
9888 .await
9889 {
9890 let left = if origin_landed {
9891 format!(
9892 "its {ORIGIN_KEY} was moved to {origin:?} before that and could \
9893 not be put back to {before:?} ({restore}), so item {} still \
9894 holds {origin:?} there",
9895 item.id.0
9896 )
9897 } else {
9898 format!(
9899 "the refused write carried its {ORIGIN_KEY} from {before:?} to \
9900 {origin:?}, GitHub does not say whether that part of it ran, \
9901 and putting it back to {before:?} was refused ({restore}), so \
9902 item {} holds {origin:?} or {before:?} there",
9903 item.id.0
9904 )
9905 };
9906 return Err(noting(
9907 error,
9908 &format!(
9909 "; {left}; next: set {ORIGIN_KEY} on it back to {before:?}, or \
9910 run the write again"
9911 ),
9912 ));
9913 }
9914 }
9915 }
9916 return Err(error);
9917 }
9918
9919 let written_status = match (incoming.written.work_status(), status_target.as_ref()) {
9920 (Some((kind, _)), Some(StatusTarget::Terminal(_, reason))) => {
9921 self.statuses
9922 .status(kind, written_option.as_deref(), true, Some(reason.reason()))
9923 }
9924 (Some((kind, _)), Some(StatusTarget::Column(_))) => {
9925 self.statuses
9926 .status(kind, written_option.as_deref(), false, None)
9927 }
9928 (Some((_, status)), _) => status.clone(),
9929 (None, _) => Status {
9930 category: StatusCategory::Unknown,
9931 name: "Open".to_owned(),
9932 },
9933 };
9934
9935 // So the rest of this command reads what it just did rather than what the board
9936 // said before it. See `remember_written` for which half takes it.
9937 let remembered = Resolved {
9938 // Empty for an issue whose board item is not read yet: `finish_writes` reads it
9939 // and `learn_item` puts it here.
9940 // 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.
9941 item_id: item_id.unwrap_or_default(),
9942 id: content_id.clone(),
9943 content_kind,
9944 kind: incoming.written.kind(),
9945 title: incoming.title.to_owned(),
9946 // The visible half of the body this write composed, split back off it the
9947 // way a read splits it — so what this record reports is what a read of the
9948 // same issue reports, rather than the person's text with the metadata slot
9949 // still on the end of it.
9950 body: self.visible(body.clone())?.0,
9951 raw_body: body.clone(),
9952 // A document has no status of its own; what it reads back as is whatever
9953 // the issue's own state says, which is what a re-read reports.
9954 status: written_status,
9955 option: written_option.or_else(|| existing.and_then(|item| item.option.clone())),
9956 priority: match incoming.priority {
9957 Some(priority) => HeldPriority::Read(priority),
9958 None => existing.map_or(HeldPriority::Read(Priority::None), |item| {
9959 item.priority.clone()
9960 }),
9961 },
9962 // What `state_input` asked for: closed for a terminal target, open for any other
9963 // status, and the issue's own state left as it was by a document write.
9964 closed: content_kind == ContentKind::Issue
9965 && match status_target.as_ref() {
9966 Some(StatusTarget::Terminal(_, _)) => true,
9967 Some(_) => false,
9968 None => existing.is_some_and(|item| item.closed),
9969 },
9970 reason: match status_target.as_ref() {
9971 Some(StatusTarget::Terminal(_, reason)) if content_kind == ContentKind::Issue => {
9972 Some(reason.reason().to_owned())
9973 }
9974 Some(_) => None,
9975 None => existing.and_then(|item| item.reason.clone()),
9976 },
9977 delivers: incoming.delivers.to_vec(),
9978 delivered_by: incoming.delivered_by.to_vec(),
9979 labels: incoming.labels.to_vec(),
9980 parent: incoming.parent.cloned(),
9981 origin: (!origin.is_empty()).then(|| origin.to_owned()),
9982 projected,
9983 number,
9984 // In the update path this is the item's own url, read off `existing` where the
9985 // record above was bound, so one expression serves both halves.
9986 url,
9987 created_at: existing.and_then(|item| item.created_at),
9988 updated_at: existing.and_then(|item| item.updated_at),
9989 // What this write moved is GitHub's to say, so a record of it gives no token until
9990 // it is read again.
9991 item_updated_at: None,
9992 comment_count: None,
9993 relationship_events: None,
9994 served_at: None,
9995 facets: None,
9996 own_repository,
9997 repositories: incoming.repositories.to_vec(),
9998 classification: incoming.classification,
9999 slot,
10000 board_id: Some(board.id.as_str().to_owned()),
10001 fields: board
10002 .fields
10003 .get("nodes")
10004 .and_then(Value::as_array)
10005 .cloned()
10006 .unwrap_or_default(),
10007 board_fields: Some(board.fields.clone()),
10008 // What this write left the relationship holding is known by id alone, and a
10009 // later read of its edges needs each far end's kind, so it reads them again.
10010 blocked_by: None,
10011 };
10012 self.remember_written(remembered, existing.is_none())?;
10013 Ok(onetaskgraph_plugin_api::AssetsWritten {
10014 id: content_id,
10015 content,
10016 })
10017 }
10018
10019 /// Everything a write does after the item exists: its board fields, its parent, and
10020 /// its dependencies.
10021 ///
10022 /// Split out of `write_item` so there is one place a failure past the point of no
10023 /// return is caught, rather than a tidy-up repeated at each `?` above.
10024 // llmlint: ignore[suppressions_justified] This is the tail of `write_item` lifted out
10025 // so there is one place a failure past the point of no return is caught, and its
10026 // arguments are exactly the values that tail already had in scope. Bundling them into a
10027 // struct would describe no concept — it would be "the arguments of this function" — and
10028 // would put the whole of `write_item`'s locals behind one more indirection.
10029 #[allow(clippy::too_many_arguments)]
10030 async fn finish_write(
10031 &self,
10032 board_id: &str,
10033 incoming: &Incoming<'_>,
10034 content_id: &NativeId,
10035 item_id: &str,
10036 content_kind: ContentKind,
10037 existing: Option<&Resolved>,
10038 origin_field: Option<&str>,
10039 origin: &str,
10040 column: Option<(String, String)>,
10041 status_target: Option<&StatusTarget>,
10042 priority: Option<&PriorityWrite>,
10043 projections: &[ProjectionWrite],
10044 native: &[String],
10045 origin_landed: &mut bool,
10046 ) -> Result<(), SourceError> {
10047 let (fields, clears) = field_writes(
10048 existing,
10049 origin_field,
10050 origin,
10051 column,
10052 priority,
10053 projections,
10054 );
10055 self.set_item_fields(board_id, item_id, &fields, &clears)
10056 .await?;
10057 *origin_landed = true;
10058
10059 // An existing issue closes in the `updateIssue` its write ends with; one created just
10060 // now closes here, once its option is selected.
10061 if existing.is_none()
10062 && content_kind == ContentKind::Issue
10063 && matches!(status_target, Some(StatusTarget::Terminal(_, _)))
10064 {
10065 self.update_content(
10066 ContentKind::Issue,
10067 content_id,
10068 json!({"stateInput":state_input(status_target)}),
10069 )
10070 .await?;
10071 }
10072
10073 if content_kind == ContentKind::Issue {
10074 self.reparent(
10075 existing.and_then(|item| item.parent.clone()),
10076 content_id,
10077 incoming.parent,
10078 )
10079 .await?;
10080 // A document takes part in no dependency graph, so writing one neither reads
10081 // nor changes the issue's own `blockedBy` relationships. Reconciling them
10082 // against the empty list a document write carries would *delete* whatever
10083 // relationships a person had made on that issue, which is a write nobody
10084 // asked for.
10085 if incoming.written.kind() != BoardKind::Document {
10086 let issue = match existing {
10087 Some(item) => Issue::Existing(item.blocked_by.as_deref()),
10088 None => Issue::Created,
10089 };
10090 self.reconcile_blocked_by(content_id, native, issue).await?;
10091 }
10092 }
10093 Ok(())
10094 }
10095
10096 /// Whether this source is holding writes back — see [`TaskSource::begin_deferred_writes`].
10097 fn deferring(&self) -> Result<bool, SourceError> {
10098 Ok(self.deferred()?.is_some())
10099 }
10100
10101 /// Whether `content` is an item this command created whose parts are held back.
10102 fn holds_back(&self, content: &NativeId) -> Result<bool, SourceError> {
10103 Ok(self
10104 .deferred()?
10105 .as_ref()
10106 .is_some_and(|deferred| deferred.holds(content)))
10107 }
10108
10109 fn deferred(&self) -> Result<std::sync::MutexGuard<'_, Option<Deferred>>, SourceError> {
10110 self.deferred.lock().map_err(|_| SourceError::Unavailable {
10111 message: "this source's record of the writes it is holding back was left \
10112 inconsistent by an earlier failure; next: run the command again"
10113 .into(),
10114 })
10115 }
10116
10117 /// Creates one issue filed on the board and under its project by the `createIssue` itself —
10118 /// `projectV2Ids` naming the board and `parentIssueId` the project — which is what a copy
10119 /// creates with: one request where adding it to the board and filing it under its project
10120 /// were two more. GitHub adds the board item after answering, so its id is not known here;
10121 /// [`TaskSource::finish_writes`] reads it.
10122 async fn create_filed_issue(
10123 &self,
10124 board_id: &str,
10125 repository: &RepositoryTarget,
10126 incoming: &Incoming<'_>,
10127 body: &Option<String>,
10128 ) -> Result<Landed, SourceError> {
10129 let repository_id = self.repository_id(repository, incoming).await?;
10130 let data = self
10131 .graphql(
10132 graphql::CREATE_ISSUE,
10133 json!({"input": filed_input(&repository_id, board_id, incoming, body)}),
10134 )
10135 .await?;
10136 let created = data
10137 .pointer("/createIssue/issue")
10138 .filter(|value| !value.is_null())
10139 .ok_or_else(|| SourceError::Malformed {
10140 message: "GitHub issue creation returned no issue".into(),
10141 })?;
10142 self.created_landed(created).await
10143 }
10144
10145 /// Keep the address `createIssue` answered for the issue `content`, for
10146 /// [`TaskSource::written_location`].
10147 fn note_written(&self, content: &NativeId, url: Option<&str>) {
10148 if let Some(url) = url {
10149 self.written
10150 .lock()
10151 .unwrap_or_else(std::sync::PoisonError::into_inner)
10152 .insert(content.clone(), url.to_owned());
10153 }
10154 }
10155
10156 /// What one `createIssue` answered, as the item it created — or, when its number cannot be
10157 /// read, the refusal, after taking the issue back.
10158 async fn created_landed(&self, created: &Value) -> Result<Landed, SourceError> {
10159 let content_id = NativeId(required_nonblank_str(created, "id")?.to_owned());
10160 let url = optional_str(created, "url")?.map(str::to_owned);
10161 let number = match created_issue_number(created) {
10162 Ok(number) => number,
10163 Err(error) => {
10164 let _ = self.delete_issue(&content_id).await;
10165 return Err(error);
10166 }
10167 };
10168 self.note_written(&content_id, url.as_deref());
10169 Ok(Landed {
10170 content_id,
10171 item_id: None,
10172 url,
10173 number,
10174 })
10175 }
10176
10177 /// Hold back everything a write of the created item `content` would send after creating it,
10178 /// for [`TaskSource::finish_writes`]: the same field values, closing and edges
10179 /// `finish_write` sends, the parent excepted, which its create already named. A later write
10180 /// of the same item replaces what it changes.
10181 // 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.
10182 #[allow(clippy::too_many_arguments)]
10183 fn hold_back(
10184 &self,
10185 board_id: &BoardId,
10186 content: &NativeId,
10187 existing: Option<&Resolved>,
10188 origin_field: Option<&str>,
10189 origin: &str,
10190 column: Option<(String, String)>,
10191 status_target: Option<&StatusTarget>,
10192 priority: Option<&PriorityWrite>,
10193 projections: &[ProjectionWrite],
10194 native: &[String],
10195 ) -> Result<(), SourceError> {
10196 let status = column.map(|(field, option)| {
10197 let name = self
10198 .fields_cache()
10199 .ok()
10200 .and_then(|fields| {
10201 fields.as_ref().and_then(|board| {
10202 option_name(&board.fields, &field, &option).map(str::to_owned)
10203 })
10204 })
10205 .unwrap_or_default();
10206 (field, option, name)
10207 });
10208 let (fields, clears) =
10209 field_writes(existing, origin_field, origin, None, priority, projections);
10210 let close = match status_target {
10211 Some(StatusTarget::Terminal(_, reason)) => Some(*reason),
10212 _ => None,
10213 };
10214 let mut deferred = self.deferred()?;
10215 let deferred = deferred.get_or_insert_with(Deferred::default);
10216 if let Deferred::Nothing = deferred {
10217 *deferred = Deferred::Held {
10218 board: board_id.clone(),
10219 created: Vec::new(),
10220 };
10221 }
10222 let Deferred::Held { created, .. } = deferred else {
10223 unreachable!("held back on the line above");
10224 };
10225 let held = match created.iter().position(|held| held.content == *content) {
10226 Some(at) => &mut created[at],
10227 None => {
10228 created.push(HeldBack {
10229 content: content.clone(),
10230 fields: Vec::new(),
10231 status: None,
10232 clears: Vec::new(),
10233 close: None,
10234 edges: Vec::new(),
10235 });
10236 created.last_mut().expect("just held")
10237 }
10238 };
10239 for (field, value) in fields {
10240 held.fields.retain(|(held, _)| *held != field);
10241 held.clears.retain(|held| *held != field);
10242 held.fields.push((field, value));
10243 }
10244 for field in clears {
10245 held.fields.retain(|(held, _)| held != field);
10246 if !held.clears.iter().any(|held| held == field) {
10247 held.clears.push(field.to_owned());
10248 }
10249 }
10250 if status.is_some() {
10251 held.status = status;
10252 }
10253 if close.is_some() {
10254 held.close = close;
10255 }
10256 // Nothing of an item created in this command has been sent but its create, so the edges
10257 // it is to have are exactly the ones its latest write names.
10258 held.edges = native.iter().map(|far| NativeId(far.clone())).collect();
10259 Ok(())
10260 }
10261
10262 /// Create every task of `writes`, together, filed on the board and under each one's
10263 /// project: [`graphql::CREATE_ISSUES`], [`MUTATION_BATCH`] to a request.
10264 ///
10265 /// Each is prepared as [`Self::write_item`] prepares one — every refusal a write owes made
10266 /// before the first request — then created, then completed with its parts held back. A
10267 /// request refused part way leaves the issues it did create, which are taken back, with the
10268 /// rest of this call's, before the refusal is answered.
10269 async fn create_together(
10270 &self,
10271 writes: &[ItemWrite<Task>],
10272 ) -> Result<Vec<NativeId>, SourceError> {
10273 let incomings: Vec<Incoming<'_>> = writes
10274 .iter()
10275 .map(|write| self.task_incoming(write, None))
10276 .collect::<Result<_, _>>()?;
10277 let mut prepared = Vec::with_capacity(writes.len());
10278 for (incoming, write) in incomings.iter().zip(writes) {
10279 if write.target.is_some() {
10280 return Err(SourceError::Malformed {
10281 message: "a write naming its target was handed to a batch of creates".into(),
10282 });
10283 }
10284 prepared.push(
10285 self.prepare_write(incoming, None, &write.depends_on)
10286 .await?,
10287 );
10288 }
10289 let mut inputs = Vec::with_capacity(prepared.len());
10290 for (incoming, prepared) in incomings.iter().zip(&prepared) {
10291 let WriteDestination::New(target) = &prepared.destination else {
10292 return Err(SourceError::Malformed {
10293 message: "an update was decided for a write handed to a batch of creates"
10294 .into(),
10295 });
10296 };
10297 let repository_id = self.repository_id(target, incoming).await?;
10298 inputs.push(filed_input(
10299 &repository_id,
10300 prepared.board.id.as_str(),
10301 incoming,
10302 &prepared.body,
10303 ));
10304 }
10305 let mut landed: Vec<Landed> = Vec::with_capacity(inputs.len());
10306 let refused = 'create: {
10307 for chunk in inputs.chunks(MUTATION_BATCH) {
10308 let mut variables = serde_json::Map::new();
10309 for slot in 0..MUTATION_BATCH {
10310 variables.insert(
10311 format!("create{slot}"),
10312 chunk.get(slot).cloned().unwrap_or_else(|| chunk[0].clone()),
10313 );
10314 variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10315 }
10316 let data = match self
10317 .graphql(graphql::CREATE_ISSUES, Value::Object(variables))
10318 .await
10319 {
10320 Ok(data) => data,
10321 Err(error) => break 'create Some(error),
10322 };
10323 let mut refusal = data.get(ANSWER_ERRORS).map(|errors| {
10324 let said: Vec<&str> = errors
10325 .as_array()
10326 .into_iter()
10327 .flatten()
10328 .filter_map(|error| error.get("message").and_then(Value::as_str))
10329 .collect();
10330 SourceError::Refused {
10331 message: if said.is_empty() {
10332 "GitHub returned GraphQL errors".to_owned()
10333 } else {
10334 said.join("; ")
10335 },
10336 }
10337 });
10338 for slot in 0..chunk.len() {
10339 match data
10340 .pointer(&format!("/create{slot}/issue"))
10341 .filter(|value| !value.is_null())
10342 {
10343 Some(created) => match self.created_landed(created).await {
10344 Ok(created) => landed.push(created),
10345 Err(error) => {
10346 refusal.get_or_insert(error);
10347 }
10348 },
10349 // An alias GitHub refused answers nothing, and the refusal says why.
10350 None => {
10351 refusal.get_or_insert(SourceError::Malformed {
10352 message: "GitHub issue creation returned no issue".into(),
10353 });
10354 }
10355 }
10356 }
10357 if let Some(error) = refusal {
10358 break 'create Some(error);
10359 }
10360 }
10361 None
10362 };
10363 if let Some(error) = refused {
10364 // None of these ids reaches the caller, so an issue GitHub will not take back is
10365 // named here or by nobody.
10366 let ids: Vec<&NativeId> = landed.iter().map(|created| &created.content_id).collect();
10367 let left = self.delete_issues(&ids).await;
10368 let left: Vec<&str> = left.iter().map(|id| id.0.as_str()).collect();
10369 return Err(left_behind(error, &left));
10370 }
10371 let created: Vec<NativeId> = landed
10372 .iter()
10373 .map(|landed| landed.content_id.clone())
10374 .collect();
10375 let mut ids = Vec::with_capacity(landed.len());
10376 for ((incoming, prepared), landed) in incomings.iter().zip(prepared).zip(landed) {
10377 match self.complete_write(incoming, prepared, landed).await {
10378 Ok(written) => ids.push(written.id),
10379 // 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.
10380 Err(error) => {
10381 let ids: Vec<&NativeId> = created.iter().collect();
10382 let left = self.delete_issues(&ids).await;
10383 let left: Vec<&str> = left.iter().map(|id| id.0.as_str()).collect();
10384 return Err(left_behind(error, &left));
10385 }
10386 }
10387 }
10388 Ok(ids)
10389 }
10390
10391 /// Hold back the update of the existing issue `issue` — `members`, what its own
10392 /// [`graphql::UPDATE_ISSUE`] would send beside its id — while writes are being held back — so the updates a copy makes one after
10393 /// another go to GitHub as one request. Whatever this source held of the issue is dropped
10394 /// as the mutation itself would drop it. Answers whether it was held: never while a write
10395 /// the store's cache expects runs, which records each mutation's own answer, and never for
10396 /// an issue an update is already held for, which that one is sent ahead of.
10397 fn hold_update(&self, issue: &NativeId, members: &Value) -> Result<bool, SourceError> {
10398 if self.expected.journaling() || self.deferred()?.is_none() {
10399 return Ok(false);
10400 }
10401 let mut held = self
10402 .held_updates
10403 .lock()
10404 .map_err(|_| SourceError::Unavailable {
10405 message: "issue updates held back were left inconsistent; run the command again"
10406 .into(),
10407 })?;
10408 if held.iter().any(|earlier| earlier.issue == *issue) {
10409 return Ok(false);
10410 }
10411 let named = |value: &str| issue.0 == value;
10412 self.resolved_cache()?
10413 .retain(|id, item| !named(&id.0) && !named(&item.item_id));
10414 self.read_ahead()?.retain(|id, item| {
10415 !named(&id.0) && !item.as_ref().is_some_and(|item| named(&item.item_id))
10416 });
10417 self.carried()?
10418 .retain(|id, item| !named(&id.0) && !named(&item.item_id));
10419 held.push(HeldUpdate::new(issue, members));
10420 Ok(true)
10421 }
10422
10423 /// Send every update [`Self::hold_update`] held back: one alone as [`graphql::UPDATE_ISSUE`]
10424 /// sends it, several as aliased `updateIssue`s of [`graphql::UPDATE_ISSUES`],
10425 /// [`MUTATION_BATCH`] to a request. Each counts one write, as it would alone.
10426 async fn send_held_updates(&self) -> Result<(), SourceError> {
10427 let held = std::mem::take(&mut *self.held_updates.lock().map_err(|_| {
10428 SourceError::Unavailable {
10429 message: "issue updates held back were left inconsistent; run the command again"
10430 .into(),
10431 }
10432 })?);
10433 if let [one] = held.as_slice() {
10434 return self
10435 .send_update(
10436 graphql::UPDATE_ISSUE,
10437 "/updateIssue/issue",
10438 &one.issue,
10439 one.input.clone(),
10440 )
10441 .await;
10442 }
10443 for chunk in held.chunks(MUTATION_BATCH) {
10444 let mut variables = serde_json::Map::new();
10445 for slot in 0..MUTATION_BATCH {
10446 variables.insert(
10447 format!("update{slot}"),
10448 chunk.get(slot).unwrap_or(&chunk[0]).input.clone(),
10449 );
10450 variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10451 }
10452 let data = self
10453 .graphql(graphql::UPDATE_ISSUES, Value::Object(variables))
10454 .await?;
10455 let inputs: Vec<&Value> = chunk.iter().map(|held| &held.input).collect();
10456 check_aliased_answers(
10457 graphql::UPDATE_ISSUES,
10458 &data,
10459 "update",
10460 &inputs,
10461 &[("issue", "id")],
10462 )?;
10463 }
10464 Ok(())
10465 }
10466
10467 /// Send everything held back since [`TaskSource::begin_deferred_writes`]: one read of the
10468 /// created items' board items, then their field values and edges together in requests of
10469 /// [`WRITE_BATCH`], then their closes in requests of [`MUTATION_BATCH`]. Anything refused
10470 /// takes back every issue held, [`DELETE_BATCH`] to a request, and answers the refusal.
10471 async fn send_held_back(&self) -> Result<(), SourceError> {
10472 let Some(Deferred::Held { board, created }) = self.deferred()?.take() else {
10473 return Ok(());
10474 };
10475 if created.is_empty() {
10476 return Ok(());
10477 }
10478 let sent = self.send_deferred(board.as_str(), &created).await;
10479 if let Err(error) = sent {
10480 // Best effort, and the failure is what the caller is told, as a write of one item
10481 // that fails part way takes back what it created: the copy that held these back
10482 // journalled every one, so one this cannot take back its undo tries again, and says
10483 // so when it cannot either.
10484 let ids: Vec<&NativeId> = created.iter().map(|held| &held.content).collect();
10485 let _ = self.delete_issues(&ids).await;
10486 return Err(error);
10487 }
10488 Ok(())
10489 }
10490
10491 /// The requests [`Self::send_held_back`] makes.
10492 async fn send_deferred(&self, board: &str, created: &[HeldBack]) -> Result<(), SourceError> {
10493 let items = self.created_board_items(board, created).await?;
10494 let mut fields: Vec<Value> = Vec::new();
10495 let mut closes: Vec<Value> = Vec::new();
10496 let mut edges: Vec<Value> = Vec::new();
10497 for held in created {
10498 let (item, status) = &items[&held.content];
10499 let mut values = held.fields.clone();
10500 // A status the board already shows — set by a workflow a person turned on to file
10501 // new items at it — is not written again.
10502 if let Some((field, option, name)) = &held.status
10503 && status.as_deref() != Some(name.as_str())
10504 {
10505 values.insert(0, (field.clone(), json!({"singleSelectOptionId": option})));
10506 }
10507 for (field, value) in values {
10508 fields.push(json!({"projectId":board,"itemId":item,"fieldId":field,"value":value}));
10509 }
10510 if let Some(reason) = held.close {
10511 closes.push(json!({"id": held.content.0, "stateInput": closed_input(reason)}));
10512 }
10513 for far in &held.edges {
10514 edges.push(json!({"issueId": held.content.0, "blockingIssueId": far.0}));
10515 }
10516 if !held.clears.is_empty() {
10517 let clears: Vec<&str> = held.clears.iter().map(String::as_str).collect();
10518 self.set_item_fields(board, item, &[], &clears).await?;
10519 }
10520 }
10521 // Each alias answers with what it wrote, which is what its input named: the board item a
10522 // field value is set on, the issue closed, the issue blocked and the one blocking it.
10523 let field_answer: &[(&str, &str)] = &[("projectV2Item", "itemId")];
10524 let close_answer: &[(&str, &str)] = &[("issue", "id")];
10525 let block_answer: &[(&str, &str)] =
10526 &[("issue", "issueId"), ("blockingIssue", "blockingIssueId")];
10527 // Every field value, then every edge, as one ordered list sent `WRITE_BATCH` aliases to a
10528 // request: a request holding both kinds is the one document carrying both, so a plan's
10529 // values and edges cost one request where they fit in one.
10530 let writes: Vec<HeldWrite<'_>> = fields
10531 .iter()
10532 .map(HeldWrite::Field)
10533 .chain(edges.iter().map(HeldWrite::Edge))
10534 .collect();
10535 for chunk in writes.chunks(WRITE_BATCH) {
10536 let values: Vec<&Value> = chunk
10537 .iter()
10538 .filter_map(|write| match write {
10539 HeldWrite::Field(input) => Some(*input),
10540 HeldWrite::Edge(_) => None,
10541 })
10542 .collect();
10543 let blocks: Vec<&Value> = chunk
10544 .iter()
10545 .filter_map(|write| match write {
10546 HeldWrite::Edge(input) => Some(*input),
10547 HeldWrite::Field(_) => None,
10548 })
10549 .collect();
10550 let (document, sends) = match (values.is_empty(), blocks.is_empty()) {
10551 (false, false) => (graphql::WRITE_FIELDS_AND_EDGES, ["sendField", "sendBlock"]),
10552 (false, true) => (graphql::WRITE_FIELD_VALUES, ["send", "send"]),
10553 _ => (graphql::ADD_BLOCKED_BYS, ["send", "send"]),
10554 };
10555 let mut variables = serde_json::Map::new();
10556 let kinds = [
10557 ("field", &values, sends[0], field_answer),
10558 ("block", &blocks, sends[1], block_answer),
10559 ];
10560 for (prefix, inputs, send, _) in &kinds {
10561 if inputs.is_empty() {
10562 continue;
10563 }
10564 for slot in 0..WRITE_BATCH {
10565 variables.insert(
10566 format!("{prefix}{slot}"),
10567 (*inputs.get(slot).unwrap_or(&inputs[0])).clone(),
10568 );
10569 variables.insert(format!("{send}{slot}"), json!(slot < inputs.len()));
10570 }
10571 }
10572 let data = self.graphql(document, Value::Object(variables)).await?;
10573 for (prefix, inputs, _, answers) in kinds {
10574 check_aliased_answers(document, &data, prefix, inputs, answers)?;
10575 }
10576 }
10577 for chunk in closes.chunks(MUTATION_BATCH) {
10578 let mut variables = serde_json::Map::new();
10579 for slot in 0..MUTATION_BATCH {
10580 variables.insert(
10581 format!("close{slot}"),
10582 chunk.get(slot).cloned().unwrap_or_else(|| chunk[0].clone()),
10583 );
10584 variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10585 }
10586 let data = self
10587 .graphql(graphql::CLOSE_ISSUES, Value::Object(variables))
10588 .await?;
10589 let inputs: Vec<&Value> = chunk.iter().collect();
10590 check_aliased_answers(graphql::CLOSE_ISSUES, &data, "close", &inputs, close_answer)?;
10591 }
10592 for held in created {
10593 self.learn_item(&held.content, &items[&held.content].0)?;
10594 }
10595 Ok(())
10596 }
10597
10598 /// The board item each of `created` is on this board, with its `Status` as the board set it:
10599 /// [`graphql::CREATED_BOARD_ITEMS`], read again — after a wait of a second, then two, four,
10600 /// eight and sixteen — for only those GitHub has not filed yet.
10601 ///
10602 /// **Membership is never assumed.** `createIssue`'s `projectV2Ids` files an issue some time
10603 /// after GitHub answered the create — the feasibility spike saw 0.45 to 1.84 seconds, and no
10604 /// create answer carrying the item — so only a read says it is there. One still absent once
10605 /// those reads are spent is filed with [`graphql::ADD_TO_BOARD`], one write each; GitHub
10606 /// filing it meanwhile refuses that "Content already exists in this project", which says it
10607 /// is placed, and one more read finds the item rather than a second one being made.
10608 async fn created_board_items(
10609 &self,
10610 board: &str,
10611 created: &[HeldBack],
10612 ) -> Result<BTreeMap<NativeId, (String, Option<String>)>, SourceError> {
10613 let mut found: BTreeMap<NativeId, (String, Option<String>)> = BTreeMap::new();
10614 let unfiled = |found: &BTreeMap<NativeId, (String, Option<String>)>| -> Vec<NativeId> {
10615 created
10616 .iter()
10617 .map(|held| held.content.clone())
10618 .filter(|id| !found.contains_key(id))
10619 .collect()
10620 };
10621 let mut wait = Duration::from_secs(1);
10622 for attempt in 0..6 {
10623 let absent = unfiled(&found);
10624 if absent.is_empty() {
10625 return Ok(found);
10626 }
10627 if attempt > 0 {
10628 self.clock
10629 .sleep_because(wait, pacing::cause::BOARD_FILING)
10630 .await;
10631 wait = wait.saturating_mul(2);
10632 }
10633 self.read_created_items(board, &absent, &mut found).await?;
10634 }
10635 let mut placed_meanwhile = Vec::new();
10636 for id in unfiled(&found) {
10637 match self
10638 .graphql(
10639 graphql::ADD_TO_BOARD,
10640 json!({"input":{"projectId":board,"contentId":id.0}}),
10641 )
10642 .await
10643 {
10644 Ok(added) => {
10645 let item = added
10646 .pointer("/addProjectV2ItemById/item")
10647 .filter(|value| !value.is_null())
10648 .ok_or_else(|| SourceError::Malformed {
10649 message: "GitHub board addition returned no project item".into(),
10650 })?;
10651 found.insert(id, (required_nonblank_str(item, "id")?.to_owned(), None));
10652 }
10653 Err(error) if already_on_the_board(&error) => placed_meanwhile.push(id),
10654 Err(error) => return Err(error),
10655 }
10656 }
10657 if !placed_meanwhile.is_empty() {
10658 self.read_created_items(board, &placed_meanwhile, &mut found)
10659 .await?;
10660 }
10661 let absent = unfiled(&found);
10662 if absent.is_empty() {
10663 return Ok(found);
10664 }
10665 let absent: Vec<&str> = absent.iter().map(|id| id.0.as_str()).collect();
10666 Err(SourceError::Unavailable {
10667 message: format!(
10668 "GitHub created {} filed on board {board} and, after 31 seconds and adding {} \
10669 to it, still answers {} on no item of it: {}; next: run the copy again once \
10670 GitHub has caught up",
10671 if absent.len() == 1 {
10672 "an issue"
10673 } else {
10674 "issues"
10675 },
10676 if absent.len() == 1 { "it" } else { "them" },
10677 if absent.len() == 1 { "it" } else { "them" },
10678 absent.join(", ")
10679 ),
10680 })
10681 }
10682
10683 /// One [`graphql::CREATED_BOARD_ITEMS`] read of each of `ids`, [`COPY_DETAIL_BATCH`] to a
10684 /// request, adding to `found` the board item of every one GitHub has filed on `board`.
10685 async fn read_created_items(
10686 &self,
10687 board: &str,
10688 ids: &[NativeId],
10689 found: &mut BTreeMap<NativeId, (String, Option<String>)>,
10690 ) -> Result<(), SourceError> {
10691 for chunk in ids.chunks(COPY_DETAIL_BATCH) {
10692 let mut variables = serde_json::Map::new();
10693 for slot in 0..COPY_DETAIL_BATCH {
10694 variables.insert(
10695 format!("id{slot}"),
10696 json!(chunk.get(slot).unwrap_or(&chunk[0]).0),
10697 );
10698 variables.insert(format!("read{slot}"), json!(slot < chunk.len()));
10699 }
10700 variables.insert("boardItems".into(), json!(BOARD_ITEMS_PAGE_SIZE));
10701 variables.insert("statusName".into(), json!(STATUS_FIELD));
10702 let data = self
10703 .graphql(graphql::CREATED_BOARD_ITEMS, Value::Object(variables))
10704 .await?;
10705 for (slot, id) in chunk.iter().enumerate() {
10706 // An answer for another issue than the one asked about says nothing of
10707 // where that one is filed, and its board item is not this one's.
10708 let node = data.get(format!("i{slot}")).filter(|node| !node.is_null());
10709 let named = node.map(|node| node.get("id").and_then(Value::as_str));
10710 if let Some(named) = named
10711 && named != Some(id.0.as_str())
10712 {
10713 return Err(SourceError::Malformed {
10714 message: format!(
10715 "GitHub answered the board items of {} when asked for those of {}",
10716 named.unwrap_or("an issue it did not name"),
10717 id.0
10718 ),
10719 });
10720 }
10721 // An issue GitHub does not answer yet is not filed yet; one it answers is
10722 // read whole, its connection, each board item's board and its status, and
10723 // any of them unreadable is refused rather than read as not filed.
10724 let Some(node) = node else { continue };
10725 let malformed = |what: &str| SourceError::Malformed {
10726 message: format!("GitHub answered the board items of {} with {what}", id.0),
10727 };
10728 let nodes = node
10729 .pointer("/projectItems/nodes")
10730 .and_then(Value::as_array)
10731 .ok_or_else(|| malformed("no list of board items"))?;
10732 let mut item = None;
10733 for node in nodes {
10734 let on = node
10735 .pointer("/project/id")
10736 .and_then(Value::as_str)
10737 .ok_or_else(|| malformed("a board item naming no board"))?;
10738 if on == board {
10739 item = Some(node);
10740 }
10741 }
10742 if let Some(item) = item {
10743 let status = match item.get("status") {
10744 None | Some(Value::Null) => None,
10745 Some(status) => Some(
10746 status
10747 .get("name")
10748 .and_then(Value::as_str)
10749 .ok_or_else(|| malformed("a status that names no option"))?
10750 .to_owned(),
10751 ),
10752 };
10753 found.insert(
10754 id.clone(),
10755 (required_nonblank_str(item, "id")?.to_owned(), status),
10756 );
10757 }
10758 }
10759 }
10760 Ok(())
10761 }
10762
10763 /// The board item id of `content`, which this process created and remembered without one,
10764 /// in every half of its own view.
10765 fn learn_item(&self, content: &NativeId, item: &str) -> Result<(), SourceError> {
10766 if let Some(held) = self.resolved_cache()?.get_mut(content) {
10767 held.item_id = item.to_owned();
10768 }
10769 if let Some(held) = self.created()?.iter_mut().find(|held| held.id == *content) {
10770 held.item_id = item.to_owned();
10771 }
10772 Ok(())
10773 }
10774
10775 /// Ask together which items each of `origins` names, and what creating an item reads; see
10776 /// [`TaskSource::read_ahead_origins_for_copy`]. An origin whose answer runs past its first
10777 /// page is left to its own lookup.
10778 async fn origins_ahead(&self, origins: &[String]) -> Result<(), SourceError> {
10779 if self.repository.is_none() {
10780 return Ok(());
10781 }
10782 let unasked: Vec<&String> = {
10783 let held = self.narrowed_cache()?;
10784 let mut unasked: Vec<&String> = Vec::new();
10785 for origin in origins {
10786 if !held.contains_key(&Narrowing::Origin(origin.clone()).key())
10787 && !unasked.contains(&origin)
10788 {
10789 unasked.push(origin);
10790 }
10791 }
10792 unasked
10793 };
10794 if unasked.is_empty() {
10795 return Ok(());
10796 }
10797 // One batch is held back for the read ahead that follows, which may carry a project's
10798 // members beside it in the same request.
10799 if unasked.len() <= ORIGIN_BATCH {
10800 let mut pending =
10801 self.pending_origins
10802 .lock()
10803 .map_err(|_| SourceError::Unavailable {
10804 message:
10805 "origins held for a read ahead were left inconsistent; run the command \
10806 again"
10807 .into(),
10808 })?;
10809 for origin in unasked {
10810 if !pending.contains(origin) {
10811 pending.push(origin.clone());
10812 }
10813 }
10814 return Ok(());
10815 }
10816 let unasked: Vec<String> = unasked.into_iter().cloned().collect();
10817 self.send_origin_lookups(&unasked, None).await
10818 }
10819
10820 /// Hold, for [`TaskSource::member_classifications`] of `project`, each member's
10821 /// classification and repositories as the members page `data` carries them — when that page
10822 /// is every member, each one names `project` as its parent, and every record it holds is
10823 /// readable. Any other page holds nothing, so the members are read as they always were.
10824 fn hold_member_classes(&self, project: &NativeId, data: &Value) -> Result<(), SourceError> {
10825 let Some(page) = data
10826 .pointer("/members/subIssues")
10827 .filter(|page| !page.is_null())
10828 else {
10829 return Ok(());
10830 };
10831 if page
10832 .pointer("/pageInfo/hasNextPage")
10833 .and_then(Value::as_bool)
10834 != Some(false)
10835 {
10836 return Ok(());
10837 }
10838 let Some(nodes) = page.get("nodes").and_then(Value::as_array) else {
10839 return Ok(());
10840 };
10841 let mut members = Vec::with_capacity(nodes.len());
10842 for node in nodes {
10843 if node.pointer("/parent/id").and_then(Value::as_str) != Some(project.0.as_str()) {
10844 return Ok(());
10845 }
10846 let Some(body) = node.get("body").and_then(Value::as_str) else {
10847 return Ok(());
10848 };
10849 // Every node here is filed under `project`, which `BoardKind::of` reads as one of its
10850 // tasks whatever its marker says, so each is a member the full read would classify.
10851 let Ok((_, slot)) = metadata_body(Some(body.to_owned())) else {
10852 return Ok(());
10853 };
10854 let Ok(classification) = Classification::from_metadata(&slot) else {
10855 return Ok(());
10856 };
10857 let repositories = if slot.contains_key(Repository::METADATA_KEY) {
10858 let Ok(repositories) = Repository::from_metadata(&slot) else {
10859 return Ok(());
10860 };
10861 repositories
10862 } else {
10863 let Some(origin) = node
10864 .pointer("/repository/nameWithOwner")
10865 .and_then(Value::as_str)
10866 else {
10867 return Ok(());
10868 };
10869 let Ok(own) = Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST))
10870 else {
10871 return Ok(());
10872 };
10873 vec![own]
10874 };
10875 members.push((classification, repositories));
10876 }
10877 self.member_classes
10878 .lock()
10879 .map_err(|_| SourceError::Unavailable {
10880 message: "a project's members held for its classification were left \
10881 inconsistent; run the command again"
10882 .into(),
10883 })?
10884 .insert(project.clone(), members);
10885 Ok(())
10886 }
10887
10888 /// Send every origin held back for the read ahead, beside the members of `members` when it
10889 /// names a project — or nothing, when none is held.
10890 async fn send_pending_origins(&self, members: Option<&NativeId>) -> Result<(), SourceError> {
10891 let pending = std::mem::take(&mut *self.pending_origins.lock().map_err(|_| {
10892 SourceError::Unavailable {
10893 message: "origins held for a read ahead were left inconsistent; run the command \
10894 again"
10895 .into(),
10896 }
10897 })?);
10898 if pending.is_empty() {
10899 return Ok(());
10900 }
10901 self.send_origin_lookups(&pending, members).await
10902 }
10903
10904 /// [`graphql::ORIGIN_LOOKUPS`] of `unasked`, [`ORIGIN_BATCH`] to a request — the first one
10905 /// [`graphql::ORIGIN_LOOKUPS_WITH_MEMBERS`], carrying the members of `members`, when it names
10906 /// a project — holding each origin's answer and, from a members page that is the whole of
10907 /// them, each member's classification.
10908 async fn send_origin_lookups(
10909 &self,
10910 unasked: &[String],
10911 members: Option<&NativeId>,
10912 ) -> Result<(), SourceError> {
10913 let Some(repository) = self.repository.clone() else {
10914 return Ok(());
10915 };
10916 let mut members = members;
10917 for chunk in unasked.chunks(ORIGIN_BATCH) {
10918 let mut variables = json!({"owner":self.owner,"number":self.project_number,
10919 "type":"ISSUE","lookupFirst":ORIGIN_LOOKUPS_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
10920 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true,
10921 "repositoryOwner":repository.owner,"repositoryName":repository.name});
10922 for slot in 0..ORIGIN_BATCH {
10923 let origin = chunk.get(slot).unwrap_or(&chunk[0]);
10924 variables[format!("filter{slot}")] =
10925 json!(format!("{ORIGIN_FIELD}:{}", quoted(origin)));
10926 variables[format!("search{slot}")] = json!(
10927 self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))))
10928 );
10929 variables[format!("ask{slot}")] = json!(slot < chunk.len());
10930 }
10931 let project = members.take();
10932 let document = match project {
10933 Some(project) => {
10934 variables["project"] = json!(project.0);
10935 variables["memberFirst"] = json!(MEMBER_PAGE_SIZE);
10936 graphql::ORIGIN_LOOKUPS_WITH_MEMBERS
10937 }
10938 None => graphql::ORIGIN_LOOKUPS,
10939 };
10940 let data = self.graphql(document, variables).await?;
10941 self.fields_read(&data)?;
10942 if let Some(project) = project {
10943 self.hold_member_classes(project, &data)?;
10944 }
10945 if let Some(node) = data.get("repository").filter(|value| !value.is_null()) {
10946 let id = required_nonblank_str(node, "id")?.to_owned();
10947 self.repository_cache()?.insert(repository.clone(), id);
10948 if let Some(numeric) = node
10949 .get("databaseId")
10950 .and_then(Value::as_u64)
10951 .and_then(std::num::NonZeroU64::new)
10952 {
10953 self.numeric_repositories
10954 .lock()
10955 .await
10956 .insert(repository.clone(), numeric);
10957 }
10958 }
10959 for (slot, origin) in chunk.iter().enumerate() {
10960 let items = data
10961 .pointer(&format!("/o{slot}/projectV2/items"))
10962 .filter(|value| !value.is_null())
10963 .ok_or_else(|| SourceError::Refused {
10964 message: format!(
10965 "GitHub project {}/{} was not found or is not visible to the token",
10966 self.owner, self.project_number
10967 ),
10968 })?;
10969 let searched =
10970 data.get(format!("s{slot}"))
10971 .ok_or_else(|| SourceError::Malformed {
10972 message: "GitHub search response has no search connection".into(),
10973 })?;
10974 if resumed(items, None)?.has_more() || resumed(searched, None)?.has_more() {
10975 continue;
10976 }
10977 let mut found: Vec<Resolved> = Vec::new();
10978 for item in optional_nodes(Some(items), "project items")?
10979 .into_iter()
10980 .flatten()
10981 {
10982 if let Some(resolved) = self.resolve(item)?
10983 && resolved.content_kind == ContentKind::Issue
10984 && !found.iter().any(|held| held.id == resolved.id)
10985 {
10986 found.push(resolved);
10987 }
10988 }
10989 for node in optional_nodes(Some(searched), "search")?
10990 .into_iter()
10991 .flatten()
10992 {
10993 if let Some(resolved) = self.resolve_issue(node).await?
10994 && !found.iter().any(|held| held.id == resolved.id)
10995 {
10996 found.push(resolved);
10997 }
10998 }
10999 self.narrowed_cache()?
11000 .insert(Narrowing::Origin((*origin).clone()).key(), found);
11001 }
11002 }
11003 Ok(())
11004 }
11005
11006 /// Delete every issue of `ids`, [`DELETE_BATCH`] to a request with
11007 /// [`graphql::DELETE_ISSUES`], and answer those still there: each a request's answer does not
11008 /// confirm, every one of a request refused whole included.
11009 async fn delete_issues(&self, ids: &[&NativeId]) -> Vec<NativeId> {
11010 let mut left = Vec::new();
11011 for chunk in ids.chunks(DELETE_BATCH) {
11012 let mut variables = serde_json::Map::new();
11013 for slot in 0..DELETE_BATCH {
11014 let id = chunk.get(slot).unwrap_or(&chunk[0]);
11015 variables.insert(format!("delete{slot}"), json!({"issueId": id.0}));
11016 variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
11017 }
11018 let data = self
11019 .graphql(graphql::DELETE_ISSUES, Value::Object(variables))
11020 .await
11021 .unwrap_or(Value::Null);
11022 for (slot, id) in chunk.iter().enumerate() {
11023 // Confirmed only by the repository the deleted issue was in, named by a
11024 // non-blank id: anything else says nothing of whether the issue is gone.
11025 let confirmed = data
11026 .pointer(&format!("/delete{slot}/repository/id"))
11027 .and_then(Value::as_str)
11028 .is_some_and(|id| !id.trim().is_empty());
11029 if !confirmed {
11030 left.push((*id).clone());
11031 }
11032 }
11033 }
11034 left
11035 }
11036
11037 /// Delete one issue, which takes its board item with it.
11038 async fn delete_issue(&self, id: &NativeId) -> Result<(), SourceError> {
11039 let data = self
11040 .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
11041 .await?;
11042 data.pointer("/deleteIssue/repository")
11043 .filter(|value| !value.is_null())
11044 .ok_or_else(|| SourceError::Malformed {
11045 message: "GitHub issue deletion returned no repository".into(),
11046 })?;
11047 self.forget(id)?;
11048 Ok(())
11049 }
11050
11051 /// Remove one item this copy created, so a copy that could not finish leaves the board
11052 /// as it found it.
11053 ///
11054 /// Deleting the issue takes its board item with it, so there is no second mutation to
11055 /// keep in step. An id the board does not hold is not an error: the item is already
11056 /// gone, which is the state this asks for. Which that is, is decided by reading the item
11057 /// by its own id — a listing of the board can still be missing an item it holds, and
11058 /// reading that as *already gone* would leave behind the very item this was asked to
11059 /// take back.
11060 async fn delete_item(&self, id: &NativeId) -> Result<(), SourceError> {
11061 let Some(item) = self.bound_item(id).await? else {
11062 return Ok(());
11063 };
11064 if item.content_kind == ContentKind::DraftIssue {
11065 return Err(SourceError::Refused {
11066 message: format!(
11067 "GitHub item {} is a draft, and this source removes an item by deleting \
11068 its issue; next: remove it from the board by hand",
11069 id.0
11070 ),
11071 });
11072 }
11073 let data = self
11074 .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
11075 .await?;
11076 data.pointer("/deleteIssue/repository")
11077 .filter(|value| !value.is_null())
11078 .ok_or_else(|| SourceError::Malformed {
11079 message: "GitHub issue deletion returned no repository".into(),
11080 })?;
11081 self.forget(id)?;
11082 Ok(())
11083 }
11084
11085 /// The issue a comment call on `task` is about, or `None` when this board holds no such
11086 /// task.
11087 ///
11088 /// Resolved exactly as [`TaskSource::get_task`] resolves it, so the comment verbs and a
11089 /// read of the task cannot disagree about which ids name one: a project or a document of
11090 /// this board is not a task here either.
11091 ///
11092 /// A **draft** is a task with nowhere to keep a comment, because GitHub keeps comments on
11093 /// issues and a draft is not one. It is refused rather than answered with an empty page,
11094 /// which would read as a task nobody has commented on yet.
11095 async fn commented_issue(&self, task: &NativeId) -> Result<Option<NativeId>, SourceError> {
11096 let cached = self.resolved_cache()?.get(task).cloned();
11097 let Some(item) = (match cached {
11098 Some(item) => Some(item),
11099 None => self.item_by_id(task).await?,
11100 })
11101 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
11102 return Ok(None);
11103 };
11104 if item.content_kind == ContentKind::DraftIssue {
11105 return Err(self.draft_has_no_comments(task));
11106 }
11107 Ok(Some(item.id))
11108 }
11109
11110 /// The refusal a comment call on a board draft is answered with: GitHub keeps comments on
11111 /// issues, and a draft is not one.
11112 fn draft_has_no_comments(&self, task: &NativeId) -> SourceError {
11113 SourceError::Refused {
11114 message: format!(
11115 "task {} of source {} is a draft item on the board, and GitHub keeps \
11116 comments on issues alone, so a draft has none to read or write; next: \
11117 convert the draft to an issue on the board, then comment on the issue it \
11118 becomes",
11119 task.0, self.name
11120 ),
11121 }
11122 }
11123
11124 /// One task and a page of its comments, read with [`graphql::ISSUE_DETAIL`] in one
11125 /// request — or `None` when this board holds no task by that id.
11126 ///
11127 /// What `task show` and a comment listing read. A draft is a task with no comments, so it
11128 /// is answered with the draft and the refusal, at the price of the draft's own read.
11129 async fn issue_detail(
11130 &self,
11131 id: &NativeId,
11132 page: &PageRequest,
11133 ) -> Result<Option<TaskDetailRead>, SourceError> {
11134 let after = page.cursor.as_ref().map(|cursor| cursor.0.as_str());
11135 let asked = self
11136 .graphql(
11137 graphql::ISSUE_DETAIL,
11138 json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),"after":after,
11139 "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
11140 "duplicates":true}),
11141 )
11142 .await;
11143 let data = match asked {
11144 Ok(data) => data,
11145 Err(error) if unresolvable_node(&error) => return Ok(None),
11146 Err(error) => return Err(error),
11147 };
11148 // `node` is null for an id that names nothing, and absent only from an answer this
11149 // source cannot read — never the same thing.
11150 let node = data.get("node").ok_or_else(|| SourceError::Malformed {
11151 message: format!("GitHub answered the read of {} with no node", id.0),
11152 })?;
11153 self.detail_of(id, node, true, after).await
11154 }
11155
11156 /// Several tasks, each with the first page of its comments when `comments` is set, read
11157 /// [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] — one answer per id, in
11158 /// order.
11159 ///
11160 /// A batch GitHub refuses because one of its ids resolves to no node at all is read again
11161 /// one item at a time, so that id is answered as missing and the others as themselves; any
11162 /// other refusal is every id of that batch's answer.
11163 async fn issue_details(
11164 &self,
11165 ids: &[NativeId],
11166 comments: Option<&PageRequest>,
11167 ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
11168 let mut read = Vec::with_capacity(ids.len());
11169 for batch in ids.chunks(DETAIL_BATCH) {
11170 match self
11171 .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, comments))
11172 .await
11173 {
11174 Ok(data) => {
11175 for (slot, id) in batch.iter().enumerate() {
11176 // Every alias asked for is answered, null for an id naming nothing;
11177 // one missing is an answer this source cannot read.
11178 let read_one = match data.get(format!("i{slot}")) {
11179 Some(node) => self.detail_of(id, node, comments.is_some(), None).await,
11180 None => Err(SourceError::Malformed {
11181 message: format!(
11182 "GitHub answered a batch read with no item for {}",
11183 id.0
11184 ),
11185 }),
11186 };
11187 read.push(read_one);
11188 }
11189 }
11190 Err(error) if unresolvable_node(&error) => {
11191 for id in batch {
11192 read.push(match comments {
11193 Some(page) => self.issue_detail(id, page).await,
11194 None => self.task_read(id).await,
11195 });
11196 }
11197 }
11198 Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
11199 }
11200 }
11201 read
11202 }
11203
11204 /// One task and nothing of its comments, as [`TaskSource::get_task`] reads it.
11205 async fn task_read(&self, id: &NativeId) -> Result<Option<TaskDetailRead>, SourceError> {
11206 Ok(self.get_task(id).await?.map(|task| TaskDetailRead {
11207 task,
11208 comments: None,
11209 }))
11210 }
11211
11212 /// What one node a detail read reached says: the task this board holds by `id`, with the
11213 /// page of comments the node carries when `commented` — or `None` for a node that is no
11214 /// task of this board.
11215 ///
11216 /// Resolved as [`Self::item_by_id`] resolves an item: a draft is read again as a draft,
11217 /// and an item this process created answers from this process's own record, which a node
11218 /// read taken moments after the write can still be behind.
11219 async fn detail_of(
11220 &self,
11221 id: &NativeId,
11222 node: &Value,
11223 commented: bool,
11224 after: Option<&str>,
11225 ) -> Result<Option<TaskDetailRead>, SourceError> {
11226 if node.is_null() {
11227 return Ok(None);
11228 }
11229 let draft = optional_str(node, "__typename")? == Some("DraftIssue");
11230 // An issue answered under one id is that id's, or the answer is not one this source
11231 // can report: reporting another issue's task and comments under the qualified id asked
11232 // for would be the one wrong answer here. A draft's own read checks the same.
11233 if !draft
11234 && optional_str(node, "__typename")? == Some("Issue")
11235 && required_str(node, "id")? != id.0
11236 {
11237 return Err(SourceError::Malformed {
11238 message: format!(
11239 "GitHub answered the read of {} with issue {}",
11240 id.0,
11241 required_str(node, "id")?
11242 ),
11243 });
11244 }
11245 let item = if draft {
11246 self.draft_by_id(id).await?
11247 } else {
11248 self.resolve_issue(node).await?
11249 };
11250 // A read that carried the whole of what the issue is blocked by — a copy's — is kept
11251 // for this command, so the dependency read the copy makes next is answered from it.
11252 if let Some(read) = item.as_ref().filter(|item| item.blocked_by.is_some()) {
11253 self.resolved_cache()?.insert(read.id.clone(), read.clone());
11254 }
11255 let Some(item) = item.filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
11256 return Ok(None);
11257 };
11258 let own = self.created()?.iter().find(|own| own.id == *id).cloned();
11259 let task = own.unwrap_or(item).task()?;
11260 let comments = match (commented, draft) {
11261 (false, _) => None,
11262 (true, true) => Some(Err(self.draft_has_no_comments(id))),
11263 (true, false) => Some(comment_page(node, &id.0, after).map(Some)),
11264 };
11265 if commented && !draft && after.is_none() {
11266 self.expected.learn_comments(id, node);
11267 }
11268 Ok(Some(TaskDetailRead { task, comments }))
11269 }
11270
11271 /// Whether the comment `comment` is one of `issue`'s own.
11272 ///
11273 /// Read before an edit or a removal is sent, because GitHub's comment mutations take the
11274 /// comment's id and nothing else: a comment id given against the wrong task would
11275 /// otherwise change a comment on some other issue entirely. An id that names nothing, or
11276 /// names something that is not an issue comment, is a comment this task does not have —
11277 /// which is what GitHub refusing to resolve it means too.
11278 async fn comment_is_on(
11279 &self,
11280 issue: &NativeId,
11281 comment: &NativeId,
11282 ) -> Result<bool, SourceError> {
11283 Ok(self.comment_on(issue, comment).await?.is_some())
11284 }
11285
11286 /// The body the comment `comment` holds, exactly as GitHub holds it, when it is one of
11287 /// `issue`'s own — the read [`Self::comment_is_on`] makes, answering with what it selected.
11288 async fn comment_on(
11289 &self,
11290 issue: &NativeId,
11291 comment: &NativeId,
11292 ) -> Result<Option<String>, SourceError> {
11293 // A write the store's cache expects knows which comments its issue held, and their
11294 // bodies, at the version it expects: what this read would answer.
11295 if let Some(primed) = self.expected.primed_comment(issue, comment) {
11296 return Ok(primed);
11297 }
11298 let asked = self
11299 .graphql(graphql::COMMENT_ISSUE, json!({"id":comment.0}))
11300 .await;
11301 let data = match asked {
11302 Ok(data) => data,
11303 Err(error) if unresolvable_node(&error) => return Ok(None),
11304 Err(error) => return Err(error),
11305 };
11306 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
11307 return Ok(None);
11308 };
11309 if optional_str(node, "__typename")? != Some("IssueComment") {
11310 return Ok(None);
11311 }
11312 let on = node.get("issue").ok_or_else(|| SourceError::Malformed {
11313 message: format!("GitHub issue comment {} names no issue", comment.0),
11314 })?;
11315 if required_str(on, "id")? != issue.0 {
11316 return Ok(None);
11317 }
11318 Ok(Some(required_str(node, "body")?.to_owned()))
11319 }
11320
11321 /// Which far ends this item's own `blockedBy` relationship holds, and which it cannot.
11322 async fn partition_edges(
11323 &self,
11324 near_kind: BoardKind,
11325 near_content: ContentKind,
11326 carried: Option<&[Value]>,
11327 depends_on: &[DependencyEdge],
11328 ) -> Result<(Vec<String>, Vec<DependencyEdge>), SourceError> {
11329 let mut native = Vec::new();
11330 let mut fallback = Vec::new();
11331 let far_ends: Vec<(&DependencyEdge, &str, bool, Option<&Value>)> = depends_on
11332 .iter()
11333 .map(|edge| {
11334 // An opaque far end names another tracker, so it is never this board's own.
11335 let same_source = edge.to.designator().is_none()
11336 && edge
11337 .to
11338 .source()
11339 .is_none_or(|source| source == self.name.as_str());
11340 // A qualified id's source segment runs to its *first* colon — `GlobalId` and
11341 // `DependencyEndpoint::source` both read it that way — and a native id may hold
11342 // colons of its own, so the far end is everything after that one separator.
11343 // Splitting at the last would truncate `work:urn:task:7` to `7`.
11344 let far_id = if edge.to.is_qualified() {
11345 edge.to
11346 .id()
11347 .split_once(':')
11348 .map_or(edge.to.id(), |(_, native)| native)
11349 } else {
11350 edge.to.id()
11351 };
11352 // One that already blocks the near issue was answered by that issue's own
11353 // read, which carried each of its blockers' kinds — an issue every one — so it
11354 // is not read again.
11355 let blocking = carried.and_then(|nodes| {
11356 nodes
11357 .iter()
11358 .find(|node| node.get("id").and_then(Value::as_str) == Some(far_id))
11359 });
11360 (edge, far_id, same_source, blocking)
11361 })
11362 .collect();
11363 // Every other same-source far end is read by its own id, exactly as the item it is a
11364 // far end of is: whether this board holds it is that read's answer, never a listing's.
11365 // They are read together, [`DETAIL_BATCH`] to a request, rather than one each.
11366 let mut unread: Vec<NativeId> = Vec::new();
11367 for (_, far_id, same_source, blocking) in &far_ends {
11368 let id = NativeId((*far_id).to_owned());
11369 if *same_source && blocking.is_none() && !unread.contains(&id) {
11370 unread.push(id);
11371 }
11372 }
11373 let read: BTreeMap<NativeId, Option<Resolved>> = unread
11374 .iter()
11375 .cloned()
11376 .zip(self.items_by_ids(&unread).await?)
11377 .collect();
11378 for (edge, far_id, same_source, blocking) in far_ends {
11379 let far = match (same_source, blocking) {
11380 (false, _) => None,
11381 (true, Some(node)) => Some(FarEnd {
11382 kind: if required_str(node, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
11383 BoardKind::Document
11384 } else {
11385 BoardKind::Work(related_kind(node)?)
11386 },
11387 content_kind: ContentKind::Issue,
11388 }),
11389 (true, None) => {
11390 let read = read
11391 .get(&NativeId(far_id.to_owned()))
11392 .cloned()
11393 .flatten()
11394 .ok_or_else(|| SourceError::Refused {
11395 message: format!("GitHub dependency item {far_id} was not found"),
11396 })?;
11397 Some(FarEnd {
11398 kind: read.kind,
11399 content_kind: read.content_kind,
11400 })
11401 }
11402 };
11403 let far = far.as_ref();
11404 // The caller says which kind the far end is, and this board holds the far end
11405 // itself, so a disagreement is settled here rather than stored: recorded, the
11406 // wrong kind would read back as a cross-level edge that never existed; written
11407 // natively, it would name a relationship of a different level than the caller
11408 // asked for.
11409 //
11410 // A far end this board holds as a *document* fails the same comparison and is
11411 // refused by the same sentence: `ItemKind` has no document variant because
11412 // nothing may point at one, so no caller can name it correctly and the refusal
11413 // is the only honest answer.
11414 if let Some(disagreeing) = far.filter(|far| far.kind != BoardKind::Work(edge.to.kind)) {
11415 return Err(SourceError::Refused {
11416 message: format!(
11417 "GitHub dependency item {far_id} is a {} of this board, and this item \
11418 names it as a {}; record the kind it is",
11419 disagreeing.kind.describes(),
11420 edge.to.kind.marker()
11421 ),
11422 });
11423 }
11424 // A draft has neither `blockedBy` nor `blocking`, so no edge of one is native
11425 // however the far end is spelled — and one classified native here would be
11426 // written nowhere at all, because a draft's native reconciliation never runs.
11427 let native_here = near_content == ContentKind::Issue
11428 && far.is_some_and(|far| {
11429 far.content_kind == ContentKind::Issue
11430 && BoardKind::Work(edge.to.kind) == near_kind
11431 });
11432 if native_here {
11433 native.push(far_id.to_owned());
11434 } else {
11435 fallback.push(edge.clone());
11436 }
11437 }
11438 Ok((native, fallback))
11439 }
11440
11441 async fn update_existing(
11442 &self,
11443 item: &Resolved,
11444 incoming: &Incoming<'_>,
11445 body: &Option<String>,
11446 status_target: Option<&StatusTarget>,
11447 ) -> Result<(), SourceError> {
11448 let title = incoming.written_title();
11449 // A terminal status closes the issue here, in the same mutation as its body: its board
11450 // option was selected before this, so a close never lands on an item whose board cannot
11451 // show it.
11452 let mut fields = match item.content_kind {
11453 ContentKind::DraftIssue => json!({"title":title,"body":body}),
11454 ContentKind::Issue => json!({"title":title,"body":body,
11455 "stateInput":state_input(status_target)}),
11456 };
11457 // Only the members whose value differs from the item as this write read it are sent,
11458 // and nothing at all when none does — whether or not the store's cache expects a
11459 // version of it. A member left out is one GitHub leaves as it is, so a person's edit
11460 // of it since that read is kept rather than overwritten with what it already held,
11461 // and a re-copy of an unchanged item costs no mutation.
11462 if let Some(members) = fields.as_object_mut() {
11463 if members.get("title").and_then(Value::as_str) == Some(item.title.as_str()) {
11464 members.remove("title");
11465 }
11466 let body_unchanged = match (members.get("body"), item.raw_body.as_deref()) {
11467 (Some(Value::String(sent)), Some(held)) => sent == held,
11468 (Some(Value::Null), held) => held.is_none_or(str::is_empty),
11469 _ => false,
11470 };
11471 if body_unchanged {
11472 members.remove("body");
11473 }
11474 // An issue already in the state the target asks for keeps it: an open one stays
11475 // open, and a closed one whose closure reads as the target's category keeps its
11476 // reason — a duplicate's says more than this write would.
11477 let unchanged_state = match status_target {
11478 Some(StatusTarget::Terminal(_, reason)) => {
11479 item.closed
11480 && closed_category(item.reason.as_deref())
11481 == closed_category(Some(reason.reason()))
11482 }
11483 Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => !item.closed,
11484 None => true,
11485 };
11486 if unchanged_state {
11487 members.remove("stateInput");
11488 }
11489 if members.is_empty() {
11490 return Ok(());
11491 }
11492 }
11493 self.update_content(item.content_kind, &item.id, fields)
11494 .await
11495 }
11496
11497 /// Update one board item's content with exactly `fields` beside its id, through the
11498 /// mutation its kind takes: `updateIssue` for an issue, `updateProjectV2DraftIssue` for
11499 /// a draft.
11500 ///
11501 /// Every input field either mutation leaves out is a field GitHub leaves as it is, which
11502 /// is what lets a narrow write carry the one thing it changes and nothing else.
11503 async fn update_content(
11504 &self,
11505 kind: ContentKind,
11506 id: &NativeId,
11507 fields: Value,
11508 ) -> Result<(), SourceError> {
11509 let (operation, id_key, pointer) = match kind {
11510 ContentKind::DraftIssue => (
11511 graphql::UPDATE_DRAFT,
11512 "draftIssueId",
11513 "/updateProjectV2DraftIssue/draftIssue",
11514 ),
11515 ContentKind::Issue => (graphql::UPDATE_ISSUE, "id", "/updateIssue/issue"),
11516 };
11517 if kind == ContentKind::Issue && self.hold_update(id, &fields)? {
11518 return Ok(());
11519 }
11520 let mut input = fields;
11521 input[id_key] = json!(id.0);
11522 self.send_update(operation, pointer, id, input).await
11523 }
11524
11525 /// Send one item's update, `input`, as `operation`, and hold GitHub's answer at `pointer`
11526 /// to naming that item, `id`.
11527 async fn send_update(
11528 &self,
11529 operation: &str,
11530 pointer: &str,
11531 id: &NativeId,
11532 input: Value,
11533 ) -> Result<(), SourceError> {
11534 let data = self.graphql(operation, json!({"input":input})).await?;
11535 let returned = data
11536 .pointer(pointer)
11537 .ok_or_else(|| SourceError::Malformed {
11538 message: "GitHub item update returned no item".into(),
11539 })?;
11540 if required_str(returned, "id")? != id.0 {
11541 return Err(SourceError::Malformed {
11542 message: "GitHub item update returned the wrong item".into(),
11543 });
11544 }
11545 Ok(())
11546 }
11547
11548 /// Creates one issue, files it on the board, and reports what a read of it would say:
11549 /// its content id, its board item id, and the web address GitHub gave it.
11550 ///
11551 /// Two calls rather than one: `createIssue` answers with an issue that is on no board,
11552 /// and `addProjectV2ItemById` is what puts it there. Filing it at creation through
11553 /// `CreateIssueInput.projectV2Ids` was tried and is not done: GitHub answered with no
11554 /// board item, and the `addProjectV2ItemById` that then had to follow was refused
11555 /// "Content already exists in this project". A terminal status is not written here:
11556 /// `finish_write` selects its option first and closes the issue after, so a close never
11557 /// lands on an item whose board cannot show it.
11558 ///
11559 /// The address and the number come back here because this is the only place either is
11560 /// known before GitHub's own board read catches up — an item this run created answers
11561 /// the reads that follow it out of the record below, and one remembered without them
11562 /// would report no location and no key for the rest of the run.
11563 async fn create_and_file_issue(
11564 &self,
11565 board_id: &str,
11566 repository: &RepositoryTarget,
11567 incoming: &Incoming<'_>,
11568 body: &Option<String>,
11569 ) -> Result<Landed, SourceError> {
11570 let repository_id = self.repository_id(repository, incoming).await?;
11571 let data = self
11572 .graphql(
11573 graphql::CREATE_ISSUE,
11574 json!({"input":{
11575 "repositoryId":repository_id,"title":incoming.written_title(),"body":body
11576 }}),
11577 )
11578 .await?;
11579 let created = data
11580 .pointer("/createIssue/issue")
11581 .filter(|value| !value.is_null())
11582 .ok_or_else(|| SourceError::Malformed {
11583 message: "GitHub issue creation returned no issue".into(),
11584 })?;
11585 let content_id = NativeId(required_str(created, "id")?.to_owned());
11586 // Optional although GitHub's schema makes it non-null: the issue exists by now, so
11587 // a response without it is not worth failing a landed write over — the item simply
11588 // reports no location until the board read catches up, which is what it did before.
11589 let url = optional_str(created, "url")?.map(str::to_owned);
11590 // The issue exists from here on, so an unreadable number and a refused board
11591 // filing below each try, best effort, to take it back: an issue in the repository
11592 // that is on no board is an item nobody asked for and nothing here would find again.
11593 //
11594 // Its number is optional on the same terms its address is — a landed write is not
11595 // worth failing over a member that came back missing, and such an item reports no
11596 // handle until a board read catches up. A number that is *present* and is not an
11597 // unsigned integer is still a response this source cannot read.
11598 let number = match created_issue_number(created) {
11599 Ok(number) => number,
11600 Err(error) => {
11601 let _ = self.delete_issue(&content_id).await;
11602 return Err(error);
11603 }
11604 };
11605 let added = match self
11606 .graphql(
11607 graphql::ADD_TO_BOARD,
11608 json!({"input":{"projectId":board_id,"contentId":content_id.0}}),
11609 )
11610 .await
11611 {
11612 Ok(added) => added,
11613 Err(error) => {
11614 let _ = self.delete_issue(&content_id).await;
11615 return Err(error);
11616 }
11617 };
11618 let item = added
11619 .pointer("/addProjectV2ItemById/item")
11620 .filter(|value| !value.is_null())
11621 .ok_or_else(|| SourceError::Malformed {
11622 message: "GitHub board addition returned no project item".into(),
11623 })?;
11624 self.note_written(&content_id, url.as_deref());
11625 Ok(Landed {
11626 content_id,
11627 item_id: Some(required_str(item, "id")?.to_owned()),
11628 url,
11629 number,
11630 })
11631 }
11632
11633 /// Move one issue under the project it now belongs to, or out of the one it left.
11634 async fn reparent(
11635 &self,
11636 held: Option<NativeId>,
11637 child: &NativeId,
11638 wanted: Option<&NativeId>,
11639 ) -> Result<(), SourceError> {
11640 if held.as_ref() == wanted {
11641 return Ok(());
11642 }
11643 if let Some(held) = &held {
11644 self.sub_issue(graphql::REMOVE_SUB_ISSUE, held, child, "removeSubIssue")
11645 .await?;
11646 }
11647 if let Some(wanted) = wanted {
11648 self.sub_issue(graphql::ADD_SUB_ISSUE, wanted, child, "addSubIssue")
11649 .await?;
11650 }
11651 Ok(())
11652 }
11653
11654 async fn sub_issue(
11655 &self,
11656 operation: &str,
11657 parent: &NativeId,
11658 child: &NativeId,
11659 root: &str,
11660 ) -> Result<(), SourceError> {
11661 let data = self
11662 .graphql(
11663 operation,
11664 json!({"input":{"issueId":parent.0,"subIssueId":child.0}}),
11665 )
11666 .await?;
11667 let issue =
11668 data.pointer(&format!("/{root}/issue"))
11669 .ok_or_else(|| SourceError::Malformed {
11670 message: "GitHub sub-issue update returned no issue".into(),
11671 })?;
11672 let sub =
11673 data.pointer(&format!("/{root}/subIssue"))
11674 .ok_or_else(|| SourceError::Malformed {
11675 message: "GitHub sub-issue update returned no sub-issue".into(),
11676 })?;
11677 if required_str(issue, "id")? != parent.0 || required_str(sub, "id")? != child.0 {
11678 return Err(SourceError::Malformed {
11679 message: "GitHub sub-issue update returned the wrong issues".into(),
11680 });
11681 }
11682 Ok(())
11683 }
11684
11685 /// Bring one issue's `blockedBy` to exactly `native`, sending only the difference, and say
11686 /// whether there was one.
11687 ///
11688 /// An issue [`Issue::Created`] by this very write is blocked by nothing yet, so its
11689 /// relationships are not read: there is nothing a read of them could find.
11690 async fn reconcile_blocked_by(
11691 &self,
11692 content_id: &NativeId,
11693 native: &[String],
11694 issue: Issue<'_>,
11695 ) -> Result<bool, SourceError> {
11696 let current = match issue {
11697 Issue::Created => Vec::new(),
11698 Issue::Existing(Some(held)) => held
11699 .iter()
11700 .map(|far| required_str(far, "id").map(str::to_owned))
11701 .collect::<Result<Vec<_>, _>>()?,
11702 Issue::Existing(None) => self.native_dependency_ids(content_id).await?,
11703 };
11704 let mut changed = false;
11705 for (operation, far_id) in current
11706 .iter()
11707 .filter(|id| !native.contains(id))
11708 .map(|id| (graphql::REMOVE_BLOCKED_BY, id))
11709 .chain(
11710 native
11711 .iter()
11712 .filter(|id| !current.contains(id))
11713 .map(|id| (graphql::ADD_BLOCKED_BY, id)),
11714 )
11715 {
11716 let data = self
11717 .graphql(
11718 operation,
11719 json!({"input":{"issueId":content_id.0,"blockingIssueId":far_id}}),
11720 )
11721 .await?;
11722 let root = if operation == graphql::ADD_BLOCKED_BY {
11723 "addBlockedBy"
11724 } else {
11725 "removeBlockedBy"
11726 };
11727 let issue =
11728 data.pointer(&format!("/{root}/issue"))
11729 .ok_or_else(|| SourceError::Malformed {
11730 message: "GitHub dependency update returned no issue".into(),
11731 })?;
11732 let blocker = data
11733 .pointer(&format!("/{root}/blockingIssue"))
11734 .ok_or_else(|| SourceError::Malformed {
11735 message: "GitHub dependency update returned no blocking issue".into(),
11736 })?;
11737 if required_str(issue, "id")? != content_id.0 || required_str(blocker, "id")? != far_id
11738 {
11739 return Err(SourceError::Malformed {
11740 message: "GitHub dependency update returned the wrong issues".into(),
11741 });
11742 }
11743 changed = true;
11744 }
11745 Ok(changed)
11746 }
11747}
11748
11749/// The board field values one write sends, in the order GitHub runs them, and the fields it
11750/// clears after them: the origin field when it moves, the `Status` option `column` names, the
11751/// priority, then each projected field.
11752fn field_writes<'a>(
11753 existing: Option<&Resolved>,
11754 origin_field: Option<&str>,
11755 origin: &str,
11756 column: Option<(String, String)>,
11757 priority: Option<&'a PriorityWrite>,
11758 projections: &'a [ProjectionWrite],
11759) -> (Vec<(String, Value)>, Vec<&'a str>) {
11760 let mut fields = Vec::new();
11761 if let Some(field_id) = origin_field
11762 && existing.map_or(!origin.is_empty(), |item| {
11763 item.origin.as_deref().unwrap_or("") != origin
11764 })
11765 {
11766 fields.push((field_id.to_owned(), json!({"text":origin})));
11767 }
11768 if let Some((field_id, option_id)) = column {
11769 fields.push((field_id, json!({"singleSelectOptionId":option_id})));
11770 }
11771 let mut clears = Vec::new();
11772 match priority {
11773 Some(PriorityWrite::Select { field, option }) => {
11774 fields.push((field.clone(), json!({"singleSelectOptionId":option})));
11775 }
11776 Some(PriorityWrite::Clear { field }) => clears.push(field.as_str()),
11777 None => {}
11778 }
11779 for projection in projections {
11780 projection.join(&mut fields, &mut clears);
11781 }
11782 (fields, clears)
11783}
11784
11785/// Everything one write decided before sending anything: see
11786/// [`GitHubProjectsSource::prepare_write`].
11787struct Prepared {
11788 /// What the write lands on.
11789 destination: WriteDestination,
11790 board: BoardFields,
11791 status_target: Option<StatusTarget>,
11792 /// The `Status` field, the option and the option's name the status lands on.
11793 // 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.
11794 column: Option<(String, String, String)>,
11795 priority_write: Option<PriorityWrite>,
11796 projections: Vec<ProjectionWrite>,
11797 projected: BTreeMap<String, String>,
11798 content_kind: ContentKind,
11799 own_repository: Option<Repository>,
11800 /// The far ends of the item's native blocked-by relationships.
11801 // 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.
11802 native: Vec<String>,
11803 slot: BTreeMap<String, Value>,
11804 content: Option<String>,
11805 body: Option<String>,
11806 /// The copy origin the item records, empty for none.
11807 // 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.
11808 origin: String,
11809 origin_field: Option<String>,
11810}
11811
11812/// One write a copy held back of the kinds sent together: a board field value, or a blocked-by
11813/// edge, each its input.
11814enum HeldWrite<'a> {
11815 Field(&'a Value),
11816 Edge(&'a Value),
11817}
11818
11819/// Whether `error` is GitHub refusing to file an issue on a board because it is filed there
11820/// already — what `addProjectV2ItemById` answers once `createIssue`'s `projectV2Ids` has placed
11821/// it, rather than answering with the item it holds.
11822fn already_on_the_board(error: &SourceError) -> bool {
11823 error
11824 .to_string()
11825 .contains("Content already exists in this project")
11826}
11827
11828/// Hold every alias `inputs` sent under `prefix` in `data` to answering with what its input
11829/// named — each `(root, named)` of `answers` — and refuse one that answered nothing or another
11830/// item.
11831fn check_aliased_answers(
11832 document: &str,
11833 data: &Value,
11834 prefix: &str,
11835 inputs: &[&Value],
11836 answers: &[(&str, &str)],
11837) -> Result<(), SourceError> {
11838 for (slot, input) in inputs.iter().enumerate() {
11839 for (root, named) in answers {
11840 let answered = data
11841 .pointer(&format!("/{prefix}{slot}/{root}/id"))
11842 .and_then(Value::as_str)
11843 .ok_or_else(|| SourceError::Malformed {
11844 message: format!(
11845 "GitHub answered {} with nothing at {prefix}{slot}",
11846 operation_description(document)
11847 ),
11848 })?;
11849 let asked = input.get(named).and_then(Value::as_str).unwrap_or_default();
11850 if answered != asked {
11851 return Err(SourceError::Malformed {
11852 message: format!(
11853 "GitHub answered {} at {prefix}{slot} with {answered}, which is not \
11854 {asked}, the item it was asked to write",
11855 operation_description(document)
11856 ),
11857 });
11858 }
11859 }
11860 }
11861 Ok(())
11862}
11863
11864/// `error`, which stopped a batch of creates, and — when GitHub refused to take any of them
11865/// back — each issue of `left` it created that is still in the repository, which its caller,
11866/// never handed their ids, could not name.
11867fn left_behind(error: SourceError, left: &[&str]) -> SourceError {
11868 if left.is_empty() {
11869 return error;
11870 }
11871 SourceError::Refused {
11872 message: format!(
11873 "{error}; GitHub then refused to take back {}, which this write created and which \
11874 are still in the repository; next: delete {} before writing again",
11875 left.join(", "),
11876 if left.len() == 1 { "it" } else { "them" }
11877 ),
11878 }
11879}
11880
11881/// What one write lands on: the item it updates, as it stood, or where it creates a new one.
11882enum WriteDestination {
11883 /// The item as it stood, for an update.
11884 Existing(Box<Resolved>),
11885 /// Where a new item is created.
11886 New(RepositoryTarget),
11887}
11888
11889impl WriteDestination {
11890 /// The item an update writes over, as it stood; `None` for a create.
11891 fn existing(&self) -> Option<&Resolved> {
11892 match self {
11893 Self::Existing(item) => Some(item),
11894 Self::New(_) => None,
11895 }
11896 }
11897}
11898
11899/// One existing issue's update held back by [`GitHubProjectsSource::hold_update`]: the issue,
11900/// and the `UpdateIssueInput` its own [`graphql::UPDATE_ISSUE`] would have sent — the members
11901/// it writes beside that issue's id, which only [`Self::new`] sets.
11902struct HeldUpdate {
11903 issue: NativeId,
11904 // 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.
11905 input: Value,
11906}
11907
11908impl HeldUpdate {
11909 /// The update writing `members` — an `UpdateIssueInput` without its id — to `issue`.
11910 fn new(issue: &NativeId, members: &Value) -> Self {
11911 let mut input = members.clone();
11912 input["id"] = json!(issue.0);
11913 Self {
11914 issue: issue.clone(),
11915 input,
11916 }
11917 }
11918}
11919
11920/// What a copy has held back of the items it created; see
11921/// [`GitHubProjectsSource::hold_back`].
11922#[derive(Debug, Default)]
11923enum Deferred {
11924 /// Writes are being held back, and nothing has been held yet.
11925 #[default]
11926 Nothing,
11927 /// The items held back, every one of them on `board`.
11928 Held {
11929 /// The board every one of them is on.
11930 board: BoardId,
11931 /// Each created item, in the order it was created.
11932 created: Vec<HeldBack>,
11933 },
11934}
11935
11936impl Deferred {
11937 /// Whether `content` is one of the items held back.
11938 fn holds(&self, content: &NativeId) -> bool {
11939 match self {
11940 Self::Nothing => false,
11941 Self::Held { created, .. } => created.iter().any(|held| held.content == *content),
11942 }
11943 }
11944}
11945
11946/// One created item's parts that land once [`TaskSource::finish_writes`] is called.
11947#[derive(Debug)]
11948struct HeldBack {
11949 /// The issue.
11950 content: NativeId,
11951 /// Its board field values other than its `Status`, in the order they are written.
11952 fields: Vec<(String, Value)>,
11953 /// The `Status` field, and the option and its name that the item's status lands on.
11954 // llmlint: ignore[invalid_states_unrepresentable] The same triple `column_for` returns and `Prepared::column` carries, held back unchanged until it is sent.
11955 status: Option<(String, String, String)>,
11956 /// The fields it clears after those are written.
11957 clears: Vec<String>,
11958 /// The reason it is closed for, once its option is selected; `None` leaves it open.
11959 close: Option<ClosedState>,
11960 /// The issues it is blocked by.
11961 edges: Vec<NativeId>,
11962}
11963
11964/// The member [`answer_with_errors`] carries a response's GraphQL errors under, beside its data:
11965/// no GitHub type has a field of this name.
11966const ANSWER_ERRORS: &str = "onetaskgraphErrors";
11967
11968/// A response's `data` with its GraphQL errors beside it under [`ANSWER_ERRORS`], when it has
11969/// both — or `None`, for an answer [`GitHubProjectsSource::answer`] reads as it always has.
11970///
11971/// What [`graphql::CREATE_ISSUES`] is answered with: GitHub runs a document's mutation fields in
11972/// order and answers each one that ran, so a refusal of one alias leaves the issues the others
11973/// created named in the data beside it — the ids a copy needs to take them back.
11974fn answer_with_errors(body: &str) -> Option<Value> {
11975 let parsed: Value = serde_json::from_str(body).ok()?;
11976 let errors = parsed
11977 .get("errors")?
11978 .as_array()
11979 .filter(|errors| !errors.is_empty())?;
11980 let mut data = parsed.get("data").filter(|data| data.is_object())?.clone();
11981 data[ANSWER_ERRORS] = Value::Array(errors.clone());
11982 Some(data)
11983}
11984
11985/// The `createIssue` input that files the new issue on `board` and, when `incoming` names one,
11986/// under its project — see [`GitHubProjectsSource::create_filed_issue`].
11987fn filed_input(
11988 repository: &str,
11989 board: &str,
11990 incoming: &Incoming<'_>,
11991 body: &Option<String>,
11992) -> Value {
11993 let mut input = json!({"repositoryId":repository,"title":incoming.written_title(),
11994 "body":body,"projectV2Ids":[board]});
11995 if let Some(parent) = incoming.parent {
11996 input["parentIssueId"] = json!(parent.0);
11997 }
11998 input
11999}
12000
12001/// The name of option `option` of field `field` among a board's `fields`.
12002fn option_name<'a>(fields: &'a Value, field: &str, option: &str) -> Option<&'a str> {
12003 fields
12004 .get("nodes")?
12005 .as_array()?
12006 .iter()
12007 .find(|node| node.get("id").and_then(Value::as_str) == Some(field))?
12008 .get("options")?
12009 .as_array()?
12010 .iter()
12011 .find(|candidate| candidate.get("id").and_then(Value::as_str) == Some(option))?
12012 .get("name")?
12013 .as_str()
12014}
12015
12016/// What a write needs to know of one far end it names: which kind of item it is, and whether
12017/// it is an issue a native relationship can name.
12018struct FarEnd {
12019 kind: BoardKind,
12020 content_kind: ContentKind,
12021}
12022
12023/// Whether the issue one write reconciles was created by that write or was already there.
12024#[derive(Clone, Copy, PartialEq, Eq)]
12025enum Issue<'a> {
12026 /// Created by this write, so it holds no relationships yet.
12027 Created,
12028 /// On the board before this write, holding whatever relationships it holds — the far
12029 /// ends of its whole `blockedBy`, when the read that reached it carried them.
12030 Existing(Option<&'a [Value]>),
12031}
12032
12033/// What resolving one node id reached; see [`GitHubProjectsSource::reach`].
12034enum Reached {
12035 /// An issue this board holds, resolved into everything this source reports about it.
12036 Held(Box<Resolved>),
12037 /// Nothing this board holds: no such node, or a node on some other board.
12038 Nothing,
12039 /// A board draft, which [`graphql::ISSUE`] reaches and reads nothing of, so it is read
12040 /// again by [`GitHubProjectsSource::draft_by_id`].
12041 Draft,
12042}
12043
12044/// What GitHub says when a string is not a node id it can resolve.
12045///
12046/// Matched because it is the ordinary answer to a project selector naming a project by its
12047/// *name*, and reporting that as a failure would make naming one impossible. It is read
12048/// off the refusal GitHub sent, never guessed from the shape of the string: this source
12049/// does not define the syntax of a GitHub node id and would be wrong about it.
12050const UNRESOLVABLE_NODE: &str = "could not resolve to a node";
12051
12052/// `error` with `note` added to the end of what it says, its kind and every other member
12053/// unchanged — so a caller still branches on the failure that happened, and reads beside it
12054/// what that failure left behind.
12055fn noting(error: SourceError, note: &str) -> SourceError {
12056 match error {
12057 SourceError::Config { message } => SourceError::Config {
12058 message: message + note,
12059 },
12060 SourceError::Auth { message } => SourceError::Auth {
12061 message: message + note,
12062 },
12063 SourceError::Refused { message } => SourceError::Refused {
12064 message: message + note,
12065 },
12066 SourceError::RateLimited {
12067 retry_after_seconds,
12068 message,
12069 } => SourceError::RateLimited {
12070 retry_after_seconds,
12071 message: Some(message.unwrap_or_default() + note),
12072 },
12073 SourceError::Unavailable { message } => SourceError::Unavailable {
12074 message: message + note,
12075 },
12076 SourceError::Malformed { message } => SourceError::Malformed {
12077 message: message + note,
12078 },
12079 }
12080}
12081
12082/// The variables of one [`graphql::ISSUE_COPY_DETAILS`] request, as [`detail_batch`] binds its
12083/// own: a slot `batch` has no id for is bound to its last id, read again at no added price.
12084fn copy_detail_batch(batch: &[NativeId], comments: &PageRequest, children: bool) -> Value {
12085 let mut variables = serde_json::Map::new();
12086 for slot in 0..COPY_DETAIL_BATCH {
12087 let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
12088 variables.insert(format!("id{slot}"), json!(id));
12089 }
12090 variables.insert("project".to_owned(), json!(batch.first().map(|id| &id.0)));
12091 variables.insert("children".to_owned(), json!(children));
12092 variables.insert("first".to_owned(), json!(comments.limit.min(MAX_PAGE_SIZE)));
12093 variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
12094 variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
12095 variables.insert("duplicates".to_owned(), json!(true));
12096 Value::Object(variables)
12097}
12098
12099/// The variables of one [`graphql::ISSUE_DETAILS`] request over `batch` — at most
12100/// [`DETAIL_BATCH`] ids — each item with the first page of its comments when `comments` asks
12101/// for them.
12102///
12103/// The document is fixed-size, so a slot `batch` has no id for is bound to its last id, which
12104/// is read again at no added price.
12105fn detail_batch(batch: &[NativeId], comments: Option<&PageRequest>) -> Value {
12106 let mut variables = serde_json::Map::new();
12107 for slot in 0..DETAIL_BATCH {
12108 let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
12109 variables.insert(format!("id{slot}"), json!(id));
12110 }
12111 variables.insert(
12112 "first".to_owned(),
12113 json!(comments.map_or(MAX_PAGE_SIZE, |page| page.limit.min(MAX_PAGE_SIZE))),
12114 );
12115 variables.insert("comments".to_owned(), json!(comments.is_some()));
12116 variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
12117 variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
12118 variables.insert("duplicates".to_owned(), json!(true));
12119 Value::Object(variables)
12120}
12121
12122/// Whether this refusal is GitHub saying the id names no node at all.
12123fn unresolvable_node(error: &SourceError) -> bool {
12124 matches!(error, SourceError::Refused { message }
12125 if message.to_ascii_lowercase().contains(UNRESOLVABLE_NODE))
12126}
12127
12128/// One project name, as a search qualifier which filters on it at the server.
12129///
12130/// Quoted so the whole title is one phrase rather than a bag of words, with the two
12131/// characters GitHub's own quoting grammar gives a meaning inside a quoted phrase escaped
12132/// the way it documents. A title matched here is still compared for equality afterwards:
12133/// the qualifier narrows what the server sends, and this source decides what it names.
12134fn title_qualifier(name: &str) -> String {
12135 format!("in:title {}", quoted(name))
12136}
12137
12138/// `value` as one quoted phrase of a GitHub search or a board filter, with the two
12139/// characters GitHub's quoting grammar gives a meaning inside a quoted phrase escaped the way
12140/// it documents — so a value holding a qualifier's spelling is searched for rather than
12141/// obeyed.
12142fn quoted(value: &str) -> String {
12143 let escaped = value.replace('\\', "\\\\").replace('"', "\\\"");
12144 format!("\"{escaped}\"")
12145}
12146
12147/// The search qualifier for the issues updated at or after `since`.
12148///
12149/// Written to the second, rounded down, which can only widen what the search returns.
12150fn updated_qualifier(since: DateTime<Utc>) -> String {
12151 format!("updated:>={}", since.format("%Y-%m-%dT%H:%M:%S+00:00"))
12152}
12153
12154/// The search terms that narrow a board-scoped issue search to a task query's text and
12155/// metadata predicates, or `None` when it carries neither.
12156///
12157/// The text is one quoted phrase, searched `in:title`, `in:body` or both as its fields say,
12158/// and each metadata value is one more quoted phrase, which GitHub finds in the body because
12159/// its index covers the metadata comment the value is stored in. GitHub ANDs the phrases and
12160/// matches each in any field the `in:` qualifier names, so a query naming a title search and
12161/// a metadata value searches both fields for both — wider than asked, never narrower, and
12162/// every candidate is confirmed in process afterwards.
12163///
12164/// **This narrows a text search, and that is this source's declared semantics.** GitHub
12165/// matches whole tokens where a substring rule would match inside a word, so an item holding
12166/// the text only inside a longer word is not returned. A text of nothing but whitespace
12167/// matches every item, so it narrows nothing and is not sent.
12168fn narrowing_qualifiers(query: &TaskQuery) -> Option<String> {
12169 let text = query
12170 .text
12171 .as_ref()
12172 .filter(|text| !text.terms.trim().is_empty());
12173 if text.is_none() && query.metadata.is_empty() {
12174 return None;
12175 }
12176 let (title, body) = match text.map(|text| text.fields) {
12177 None => (false, true),
12178 Some(TextFields::Title) => (true, !query.metadata.is_empty()),
12179 Some(TextFields::Content) => (false, true),
12180 Some(TextFields::TitleOrContent) => (true, true),
12181 };
12182 let fields = match (title, body) {
12183 (true, true) => "in:title,body",
12184 (true, false) => "in:title",
12185 _ => "in:body",
12186 };
12187 let phrases = text
12188 .map(|text| text.terms.clone())
12189 .into_iter()
12190 .chain(
12191 query
12192 .metadata
12193 .iter()
12194 .map(|wanted| as_stored(wanted.value())),
12195 )
12196 .map(|phrase| quoted(&phrase))
12197 .collect::<Vec<_>>();
12198 Some(format!("{fields} {}", phrases.join(" ")))
12199}
12200
12201/// The search terms that narrow a board-scoped issue search to a project or document query's
12202/// text, or `None` when it has none or a blank one: the phrase, in the fields, a task query
12203/// carrying that text alone is sent as by [`narrowing_qualifiers`].
12204fn text_qualifiers(text: Option<&TextQuery>) -> Option<String> {
12205 narrowing_qualifiers(&TaskQuery {
12206 text: text.cloned(),
12207 ..TaskQuery::default()
12208 })
12209}
12210
12211/// Refuses a project or document query's text GitHub's issue search cannot find, before
12212/// anything is asked of GitHub, on exactly the terms [`refuse_unsearchable`] refuses a task
12213/// query's.
12214fn refuse_unsearchable_text(text: Option<&TextQuery>) -> Result<(), SourceError> {
12215 refuse_unsearchable(&TaskQuery {
12216 text: text.cloned(),
12217 ..TaskQuery::default()
12218 })
12219}
12220
12221/// Refuses a task query naming a text or a metadata value GitHub's issue search cannot find,
12222/// before anything is asked of GitHub.
12223///
12224/// GitHub's index holds words, so a phrase with no letter or digit names none to find, and no
12225/// bounded query answers it: sent, GitHub's answer to it is nothing this source may rely on;
12226/// left out, the search is every issue of the board. So this source says it cannot answer
12227/// rather than reading the board or answering nothing. A blank text is not refused: it narrows
12228/// nothing GitHub could search for, and keeps the board read it always had.
12229fn refuse_unsearchable(query: &TaskQuery) -> Result<(), SourceError> {
12230 const WHY: &str = "GitHub's issue search indexes words, so it cannot answer a value with no \
12231 letter or digit with a bounded query";
12232 if let Some(text) = &query.text
12233 && !text.terms.trim().is_empty()
12234 && !has_words(&text.terms)
12235 {
12236 return Err(SourceError::Refused {
12237 message: format!(
12238 "cannot search for the text {:?}: {WHY}; search for a text holding a letter or a digit",
12239 text.terms
12240 ),
12241 });
12242 }
12243 if let Some(wanted) = query
12244 .metadata
12245 .iter()
12246 .find(|wanted| !has_words(wanted.value()))
12247 {
12248 return Err(SourceError::Refused {
12249 message: format!(
12250 "cannot filter by the metadata value {:?} at {:?}: {WHY}; filter by a value holding a letter or a digit",
12251 wanted.value(),
12252 std::iter::once(wanted.key())
12253 .chain(wanted.path().iter().map(String::as_str))
12254 .collect::<Vec<_>>()
12255 .join("/"),
12256 ),
12257 });
12258 }
12259 Ok(())
12260}
12261
12262/// Whether GitHub's index could hold a word of `phrase`: whether it has a letter or a digit.
12263fn has_words(phrase: &str) -> bool {
12264 phrase.chars().any(char::is_alphanumeric)
12265}
12266
12267/// `value` spelled the way the metadata slot stores it: as the inside of its JSON string.
12268///
12269/// What GitHub indexes is the slot's JSON text, so a value holding a character JSON escapes —
12270/// a newline, a tab, a quote — is found by the escape the body holds and not by the character,
12271/// which GitHub's word match would read as different words.
12272fn as_stored(value: &str) -> String {
12273 let encoded = Value::String(value.to_owned()).to_string();
12274 encoded[1..encoded.len() - 1].to_owned()
12275}
12276
12277/// The one narrower question a task query carrying a text, metadata or origin predicate is
12278/// sent as.
12279enum Narrowing {
12280 /// Every carrier of this origin: [`graphql::ORIGIN_LOOKUP`].
12281 Origin(String),
12282 /// The board-scoped issue search narrowed by these qualifiers.
12283 Search(String),
12284}
12285
12286impl Narrowing {
12287 /// What this question is remembered under for the length of one command.
12288 fn key(&self) -> String {
12289 match self {
12290 Self::Origin(origin) => format!("origin {origin}"),
12291 Self::Search(also) => format!("search {also}"),
12292 }
12293 }
12294}
12295
12296/// Where one connection of [`graphql::ORIGIN_LOOKUP`] resumes.
12297enum Resumed {
12298 /// It reported another page, which starts after this cursor.
12299 More(String),
12300 /// It has ended. Sending this cursor again — the page's own end when it had one, and
12301 /// otherwise the cursor it was reached from — answers an empty page, so the one document
12302 /// can go on walking the other connection.
12303 Ended(Option<String>),
12304}
12305
12306impl Resumed {
12307 /// Whether the connection has another page.
12308 const fn has_more(&self) -> bool {
12309 matches!(self, Self::More(_))
12310 }
12311
12312 /// The cursor to send this connection next.
12313 fn cursor(self) -> Option<String> {
12314 match self {
12315 Self::More(next) => Some(next),
12316 Self::Ended(last) => last,
12317 }
12318 }
12319}
12320
12321/// Where `connection`, reached from `after`, resumes — refused when it reports another page
12322/// with no cursor to it, or from a cursor that does not advance.
12323fn resumed(connection: &Value, after: Option<&str>) -> Result<Resumed, SourceError> {
12324 let info = connection
12325 .get("pageInfo")
12326 .ok_or_else(|| SourceError::Malformed {
12327 message: "GitHub connection has no pageInfo".into(),
12328 })?;
12329 let end = optional_str(info, "endCursor")?;
12330 if required_bool(info, "hasNextPage")? {
12331 let next = end.ok_or_else(|| SourceError::Malformed {
12332 message: "GitHub connection reports another page and no endCursor".into(),
12333 })?;
12334 validate_cursor_progress(after, next)?;
12335 return Ok(Resumed::More(next.to_owned()));
12336 }
12337 Ok(Resumed::Ended(
12338 end.map(str::to_owned).or_else(|| after.map(str::to_owned)),
12339 ))
12340}
12341
12342/// The board, and every item on it this source reports.
12343#[derive(Clone)]
12344struct Board {
12345 id: String,
12346 fields: Value,
12347 items: Vec<Resolved>,
12348}
12349
12350/// What a write needs of the board and nothing more: its node id and its field
12351/// definitions, in the shape a read of the board's own `fields` gives them.
12352///
12353/// Deliberately no items. A write decides which item it writes, which parent it files
12354/// under and which far ends it names by reading each of them by its own id; this is the
12355/// half of the board those reads cannot carry, and holding no item is what keeps it from
12356/// ever being asked whether an item is there.
12357#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
12358struct BoardFields {
12359 id: BoardId,
12360 fields: Value,
12361}
12362
12363/// A board's node id: what a field write and `addProjectV2ItemById` address.
12364///
12365/// Never blank, because a blank one addresses no board — so an id GitHub answers blank is
12366/// refused where it is read, and one an item names blank is read as not named at all.
12367#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
12368#[serde(try_from = "String")]
12369struct BoardId(String);
12370
12371impl TryFrom<String> for BoardId {
12372 type Error = String;
12373
12374 /// The same refusal [`BoardId::parse`] makes, for an id read back from a kept context.
12375 fn try_from(id: String) -> Result<Self, Self::Error> {
12376 Self::parse(&id).map_err(|error| error.to_string())
12377 }
12378}
12379
12380/// Where one write left its item, for the record the rest of the command reads it out of.
12381///
12382/// A named record rather than a tuple because the update arm and the create arm each fill
12383/// all four, and two `Option`s of different meaning side by side in a tuple are two
12384/// positions a reader has to count.
12385struct Landed {
12386 /// The issue's own node id, which is the [`NativeId`] this source reports.
12387 content_id: NativeId,
12388 /// The board item's id, which is what a field write addresses — `None` for an issue its
12389 /// create filed on the board itself, whose board item GitHub adds after answering and
12390 /// [`TaskSource::finish_writes`] reads.
12391 // 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.
12392 item_id: Option<String>,
12393 /// The web address GitHub gave the issue, when it gave one.
12394 // 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.
12395 url: Option<String>,
12396 /// The issue's number on its repository, when GitHub reported one.
12397 number: Option<u64>,
12398}
12399
12400impl BoardId {
12401 fn parse(id: &str) -> Result<Self, SourceError> {
12402 if id.trim().is_empty() {
12403 return Err(SourceError::Malformed {
12404 message: "GitHub named a board with a blank node id".into(),
12405 });
12406 }
12407 Ok(Self(id.to_owned()))
12408 }
12409
12410 fn as_str(&self) -> &str {
12411 &self.0
12412 }
12413}
12414
12415impl Board {
12416 fn field<'a>(fields: &'a Value, name: &str) -> Result<Option<&'a Value>, SourceError> {
12417 complete_connection(fields, "project fields", NESTED_PAGE_SIZE)?;
12418 let nodes = fields
12419 .get("nodes")
12420 .and_then(Value::as_array)
12421 .ok_or_else(|| SourceError::Malformed {
12422 message: "GitHub project fields.nodes is not an array".into(),
12423 })?;
12424 Ok(nodes
12425 .iter()
12426 .find(|field| field.get("name").and_then(Value::as_str) == Some(name)))
12427 }
12428}
12429
12430/// One board item, resolved into everything this source reports about it.
12431///
12432/// Serializable because it is what [`TaskSource::write_context`] hands the store's cache and
12433/// [`TaskSource::prime_write`] takes back — see [`expected`].
12434#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
12435struct Resolved {
12436 item_id: String,
12437 id: NativeId,
12438 content_kind: ContentKind,
12439 kind: BoardKind,
12440 title: String,
12441 body: Option<String>,
12442 /// The body exactly as GitHub holds it, metadata slot and all, which is what a write
12443 /// that changes the slot alone has to keep byte for byte outside it.
12444 raw_body: Option<String>,
12445 status: Status,
12446 /// The name of the board `Status` option this item sits in, as the board spells it.
12447 option: Option<String>,
12448 /// What its `Priority` field says, read through this instance's mapping.
12449 priority: HeldPriority,
12450 /// Whether this item's issue is closed. A draft has no such state and is never closed.
12451 closed: bool,
12452 /// GitHub's reason for a closed issue's closure, as it spells it — `None` for an open
12453 /// issue, a draft, and an issue closed before GitHub recorded reasons.
12454 #[serde(default)]
12455 reason: Option<String>,
12456 /// The tasks this one delivers, read out of its slot. Empty for anything not a task.
12457 delivers: Vec<TaskRef>,
12458 /// Every task that delivers this one, read out of its slot. Empty for anything not a
12459 /// task.
12460 delivered_by: Vec<TaskRef>,
12461 labels: Vec<Label>,
12462 parent: Option<NativeId>,
12463 // 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.
12464 origin: Option<String>,
12465 /// What each board text field this instance projects metadata onto holds for this item,
12466 /// by field name — only the fields holding a value, an empty text included. What a write compares the
12467 /// item's metadata against, so a field already holding its value is not written again;
12468 /// never read back as metadata, which the slot alone reports.
12469 projected: BTreeMap<String, String>,
12470 /// The issue's own number on its repository, as GitHub reports it.
12471 ///
12472 /// `None` in exactly two cases: a draft, which has no number at all — `DraftIssue`
12473 /// declares none, and a draft is not filed in a repository to be numbered by one — and
12474 /// an issue this run created whose creating mutation answered without one, which is a
12475 /// response GitHub's own schema says cannot happen and which a landed write is not
12476 /// worth failing over. An `Issue` read off the board always has one.
12477 number: Option<u64>,
12478 url: Option<String>,
12479 created_at: Option<DateTime<Utc>>,
12480 updated_at: Option<DateTime<Utc>>,
12481 /// When the board item itself last changed — what a board field write moves — when the
12482 /// read that reached it said.
12483 item_updated_at: Option<DateTime<Utc>>,
12484 /// How many comments the issue holds, when the read that reached it said.
12485 comment_count: Option<u64>,
12486 /// How many relationship events the issue's timeline holds — every dependency, as
12487 /// `blockedBy` or `blocking`, and every project membership, as its parent, added or
12488 /// removed — when the read that reached it said.
12489 relationship_events: Option<u64>,
12490 /// The server's clock on the response that read it — its `Date` header — when the
12491 /// response carried one.
12492 served_at: Option<DateTime<Utc>>,
12493 /// The digest of its [`change_token::Facets`] — its own kind, its board field values and
12494 /// its labels — when the read that reached it carried all three whole.
12495 #[serde(default)]
12496 facets: Option<change_token::FacetsDigest>,
12497 own_repository: Option<Repository>,
12498 repositories: Vec<Repository>,
12499 classification: Classification,
12500 slot: BTreeMap<String, Value>,
12501 /// The node id of the board this item sits on, when the read that reached it said.
12502 board_id: Option<String>,
12503 /// The definition of every board field this item holds a value of, in the shape a read
12504 /// of the board's own `fields` gives one.
12505 ///
12506 /// Only the fields this item has a value in: a field it holds nothing of is not here,
12507 /// which says nothing about whether the board has it.
12508 fields: Vec<Value>,
12509 /// Every field the board this item sits on defines, as its own read of the board's
12510 /// `fields` gives them — when the read that reached the item carried them, which a read
12511 /// of it by its own id does. What a write of it needs of the board, then, needs no read
12512 /// of the board.
12513 board_fields: Option<Value>,
12514 /// The far ends of this issue's whole `blockedBy` connection, each as a dependency read
12515 /// selects one — when the read that reached it carried the connection to its end, which a
12516 /// read of it by its own id does for any issue blocked by no more than a page. What a
12517 /// write reconciles that relationship against, and what a read of its forward edges in
12518 /// the same command answers with.
12519 blocked_by: Option<Vec<Value>>,
12520}
12521
12522impl Resolved {
12523 /// The board this item's own read names it on, when that read named one this source can
12524 /// address.
12525 fn named_board(&self) -> Option<BoardId> {
12526 self.board_id
12527 .as_deref()
12528 .and_then(|id| BoardId::parse(id).ok())
12529 }
12530
12531 /// The board's id and every field it defines, when the read that reached this item
12532 /// carried both — which a read of it by its own id does.
12533 fn carried_board(&self) -> Option<BoardFields> {
12534 Some(BoardFields {
12535 id: self.named_board()?,
12536 fields: self.board_fields.clone()?,
12537 })
12538 }
12539
12540 /// Whether this item holds a value of the board field called `name`, and so carries
12541 /// that field's definition. `false` says nothing about whether the board has the field.
12542 fn defines(&self, name: &str) -> bool {
12543 self.fields
12544 .iter()
12545 .any(|field| field.get("name").and_then(Value::as_str) == Some(name))
12546 }
12547
12548 /// The metadata a caller sees: their own keys, plus the copy origin this source keeps
12549 /// in a field of its own, and none of the five keys that are only an encoding.
12550 ///
12551 /// The two delivery keys are left out for every kind, not only for a task: they are
12552 /// the encoding of [`Task::delivers`] and [`Task::delivered_by`], and a project or a
12553 /// document carrying one holds nothing a caller's own metadata could mean by it.
12554 fn metadata(&self) -> BTreeMap<String, Value> {
12555 let mut metadata = self.slot.clone();
12556 metadata.remove(Repository::METADATA_KEY);
12557 metadata.remove(DependencyEdge::RECORDED_KEY);
12558 metadata.remove(ItemKind::METADATA_KEY);
12559 metadata.remove(TaskRef::DELIVERS_KEY);
12560 metadata.remove(TaskRef::DELIVERED_BY_KEY);
12561 metadata.remove(Classification::METADATA_KEY);
12562 // The board field is the origin, and the body's copy of it is only a mirror for the
12563 // issue search to find: an item whose field holds none has none, whatever its body
12564 // says, so no reader ever sees two answers.
12565 metadata.remove(ORIGIN_KEY);
12566 if let Some(origin) = &self.origin {
12567 metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin.clone()));
12568 }
12569 metadata
12570 }
12571
12572 /// Where this item is, as a link a reader can open.
12573 ///
12574 /// A board is a hosted place and every issue on it has a web address, so that address
12575 /// is what "where is this?" means here — and [`Location::Url`] is what says which kind
12576 /// of place it is, so a reader knows to open it rather than to read a file out. It
12577 /// does not replace or derive from `url`: the field goes on reporting exactly what it
12578 /// reported before, and this says what that address *is*.
12579 ///
12580 /// An item GitHub gave no `url` for — a draft has none — reports no location at all
12581 /// rather than a third variant, which is the contract's "the source did not say". An
12582 /// issue this run created is not one of those: its address comes back from the
12583 /// creating mutation, so it is somewhere a reader can open from the moment it exists
12584 /// rather than from whenever the board read catches up.
12585 fn location(&self) -> Option<Location> {
12586 self.url.clone().map(Location::Url)
12587 }
12588
12589 /// The short handle this board's backend shows people for a task: the issue's number
12590 /// alone, as a decimal string.
12591 ///
12592 /// The number alone rather than `owner/repo#1043`, because that is the contract's
12593 /// value for this backend. A draft has no number and so no handle, which is the
12594 /// contract's *absent* rather than a handle of some other shape — and the native
12595 /// [`Task::id`] here is the issue's GraphQL node id, which this neither replaces nor
12596 /// derives from.
12597 fn key(&self) -> Option<String> {
12598 self.number.map(|number| number.to_string())
12599 }
12600
12601 /// This item's change token — see [`change_token`] — or `None` when the read that
12602 /// reached it did not carry all six of its parts, which a draft never does, did not say
12603 /// when GitHub served it, or found it changed too recently to have settled.
12604 fn version(&self) -> Option<String> {
12605 match self.content_kind {
12606 ContentKind::Issue => {
12607 let (issue, item) = (self.updated_at?, self.item_updated_at?);
12608 let parts = change_token::Parts {
12609 issue,
12610 item,
12611 comments: self.comment_count?,
12612 relationships: self.relationship_events?,
12613 facets: self.facets.clone()?,
12614 repository: self.own_repository.clone()?,
12615 };
12616 change_token::settled(issue, item, self.served_at?)
12617 .then(|| change_token::spell(&parts))
12618 }
12619 ContentKind::DraftIssue => None,
12620 }
12621 }
12622
12623 /// Whether its `Priority` field holds a value at all, mapped or not.
12624 fn holds_priority(&self) -> bool {
12625 self.priority != HeldPriority::Read(Priority::None)
12626 }
12627
12628 /// The task this item is.
12629 ///
12630 /// Fails for an item whose `Priority` field holds an option the mapping does not name:
12631 /// reading that as a level would be a guess, and reading it as `none` would let the next
12632 /// copy clear a priority a person set.
12633 fn task(&self) -> Result<Task, SourceError> {
12634 let priority = match &self.priority {
12635 HeldPriority::Read(priority) => *priority,
12636 HeldPriority::Unmapped(option) => {
12637 return Err(SourceError::Malformed {
12638 message: format!(
12639 "task {}{} sits in the board {PRIORITY_FIELD} option {option:?}, which \
12640 this source's priority_mapping does not name, so its priority cannot be \
12641 read; next: name {option:?} under priority_mapping, or move the item to \
12642 a mapped option",
12643 self.id,
12644 self.number
12645 .map(|number| format!(" (#{number})"))
12646 .unwrap_or_default()
12647 ),
12648 });
12649 }
12650 };
12651 Ok(Task {
12652 version: self.version(),
12653 id: self.id.clone(),
12654 key: self.key(),
12655 title: self.title.clone(),
12656 content: self.body.clone(),
12657 status: self.status.clone(),
12658 priority,
12659 labels: self.labels.clone(),
12660 project: self.parent.clone(),
12661 url: self.url.clone(),
12662 location: self.location(),
12663 created_at: self.created_at,
12664 updated_at: self.updated_at,
12665 metadata: self.metadata(),
12666 repositories: self.repositories.clone(),
12667 delivers: self.delivers.clone(),
12668 delivered_by: self.delivered_by.clone(),
12669 classification: self.classification,
12670 })
12671 }
12672
12673 fn project(&self) -> Project {
12674 Project {
12675 version: self.version(),
12676 id: self.id.clone(),
12677 title: self.title.clone(),
12678 content: self.body.clone(),
12679 status: self.status.clone(),
12680 labels: self.labels.clone(),
12681 url: self.url.clone(),
12682 location: self.location(),
12683 created_at: self.created_at,
12684 updated_at: self.updated_at,
12685 metadata: self.metadata(),
12686 repositories: self.repositories.clone(),
12687 classification: self.classification,
12688 }
12689 }
12690
12691 /// The same issue as a document: the project it is filed under, and no status and no
12692 /// dependencies, because a document is not work.
12693 fn document(&self) -> Document {
12694 Document {
12695 version: self.version(),
12696 id: self.id.clone(),
12697 title: self.title.clone(),
12698 content: self.body.clone(),
12699 project: self.parent.clone(),
12700 labels: self.labels.clone(),
12701 url: self.url.clone(),
12702 location: self.location(),
12703 created_at: self.created_at,
12704 updated_at: self.updated_at,
12705 metadata: self.metadata(),
12706 repositories: self.repositories.clone(),
12707 classification: self.classification,
12708 }
12709 }
12710}
12711
12712/// Where one targeted update moves an item's status, and which of its two halves move.
12713struct StatusMove {
12714 /// The board the item's `Status` field is on.
12715 board: BoardId,
12716 /// The `Status` field's id.
12717 field: String,
12718 /// The option's id.
12719 option: String,
12720 /// The option's name, as the board spells it.
12721 name: String,
12722 /// What the status asks of the issue's state.
12723 target: StatusTarget,
12724 /// The status the item reads as once it is there.
12725 landed: Status,
12726 /// Which of the status's two halves differ from what the item holds.
12727 moves: Moves,
12728}
12729
12730/// Which halves of an item's status one targeted update moves: its `Status` option, the open or
12731/// closed state of its issue, or both. A status neither half of which differs is no move at all,
12732/// and is not a value of this type.
12733#[derive(Clone, Copy, PartialEq, Eq)]
12734enum Moves {
12735 /// The option alone.
12736 Option,
12737 /// The issue's state alone: open, closed, or closed with another reason.
12738 State,
12739 /// Both.
12740 Both,
12741}
12742
12743impl Moves {
12744 /// What differs, or `None` when nothing does.
12745 const fn of(option: bool, state: bool) -> Option<Self> {
12746 match (option, state) {
12747 (true, true) => Some(Self::Both),
12748 (true, false) => Some(Self::Option),
12749 (false, true) => Some(Self::State),
12750 (false, false) => None,
12751 }
12752 }
12753
12754 /// Whether the option moves.
12755 const fn option(self) -> bool {
12756 matches!(self, Self::Option | Self::Both)
12757 }
12758
12759 /// Whether the issue's state moves.
12760 const fn state(self) -> bool {
12761 matches!(self, Self::State | Self::Both)
12762 }
12763}
12764
12765/// What one write is, and the status that comes with being it.
12766///
12767/// One value rather than a [`BoardKind`] beside an `Option<Status>`: a document has no
12768/// status and a task or a project always has one, so "a document carrying a status" and
12769/// "a task carrying none" are states a write cannot be in rather than states every use
12770/// site below has to defend against.
12771enum Written<'a> {
12772 /// A document, which is not work and so has no status at all.
12773 Document,
12774 /// A task or a project, and the status it is being written with.
12775 Work(ItemKind, &'a Status),
12776}
12777
12778impl Written<'_> {
12779 /// Which of the board's three kinds this write is.
12780 const fn kind(&self) -> BoardKind {
12781 match self {
12782 Self::Document => BoardKind::Document,
12783 Self::Work(kind, _) => BoardKind::Work(*kind),
12784 }
12785 }
12786
12787 /// The status this write carries. A document carries none, so a write of one says
12788 /// nothing about the issue's open or closed state and selects no board `Status`
12789 /// option.
12790 const fn status(&self) -> Option<&Status> {
12791 match self {
12792 Self::Document => None,
12793 Self::Work(_, status) => Some(status),
12794 }
12795 }
12796
12797 /// The status this write carries with the kind whose half of `status_mapping` it is
12798 /// written through.
12799 const fn work_status(&self) -> Option<(ItemKind, &Status)> {
12800 match self {
12801 Self::Document => None,
12802 Self::Work(kind, status) => Some((*kind, status)),
12803 }
12804 }
12805}
12806
12807/// The item being written, in the one shape all three write methods reach.
12808struct Incoming<'a> {
12809 written: Written<'a>,
12810 /// The title a person wrote. A document's goes onto the issue with
12811 /// [`DESIGN_TITLE_PREFIX`] put back, so a round trip returns the title that went in.
12812 title: &'a str,
12813 content: Option<&'a str>,
12814 assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
12815 labels: &'a [Label],
12816 metadata: &'a BTreeMap<String, Value>,
12817 repositories: &'a [Repository],
12818 /// Recorded in the slot while private, and nowhere while public.
12819 classification: Classification,
12820 parent: Option<&'a NativeId>,
12821 /// [`Task::delivers`], already checked. Empty for a project or a document, which is
12822 /// what keeps either key out of their slot.
12823 delivers: &'a [TaskRef],
12824 /// [`Task::delivered_by`], already checked. Empty for a project or a document.
12825 delivered_by: &'a [TaskRef],
12826 /// [`Task::priority`], for a task written to an instance that holds one; `None` for a
12827 /// project, a document, and every write to an instance with no `priority_mapping` —
12828 /// which is what keeps such a write's requests exactly what they were before.
12829 priority: Option<Priority>,
12830}
12831
12832/// What one write does to one projected metadata text field, by the field's id.
12833#[derive(Debug, Clone)]
12834enum ProjectionWrite {
12835 /// Write this text.
12836 Set {
12837 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.
12838 text: String,
12839 },
12840 /// Clear the field's value.
12841 Clear { field: String }, // llmlint: ignore[invalid_states_unrepresentable] As above.
12842}
12843
12844impl ProjectionWrite {
12845 /// Add this write to a field write's list of values and list of clears.
12846 fn join<'a>(&'a self, values: &mut Vec<(String, Value)>, clears: &mut Vec<&'a str>) {
12847 match self {
12848 Self::Set { field, text } => values.push((field.clone(), json!({"text": text}))),
12849 Self::Clear { field } => clears.push(field),
12850 }
12851 }
12852}
12853
12854/// What one write does to an item's `Priority` field.
12855enum PriorityWrite {
12856 /// Select this option of this field.
12857 Select {
12858 /// The `Priority` field's id.
12859 field: String,
12860 /// The mapped option's id.
12861 option: String,
12862 },
12863 /// Clear the field's value, which is what `none` is.
12864 Clear {
12865 /// The `Priority` field's id.
12866 field: String,
12867 },
12868}
12869
12870impl Incoming<'_> {
12871 /// The title this write puts on the issue.
12872 fn written_title(&self) -> String {
12873 match self.written {
12874 Written::Document => format!("{DESIGN_TITLE_PREFIX}{}", self.title),
12875 Written::Work(..) => self.title.to_owned(),
12876 }
12877 }
12878}
12879
12880#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
12881enum ContentKind {
12882 DraftIssue,
12883 Issue,
12884}
12885
12886/// What one board issue is: a document, or the work an [`ItemKind`] names.
12887///
12888/// A type of this source's own rather than an `ItemKind` with a third variant, because
12889/// `ItemKind` names what a dependency endpoint points at and nothing may point at a
12890/// document — the contract keeps a document out of that enum deliberately. Holding the
12891/// board's three answers in one value is what makes every place that asks "which is this?"
12892/// answer all three, rather than a `document: bool` beside a `kind` that means nothing for
12893/// two thirds of the board.
12894#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
12895enum BoardKind {
12896 /// An issue whose title begins [`DESIGN_TITLE_PREFIX`].
12897 Document,
12898 /// Every other issue, and every draft.
12899 Work(ItemKind),
12900}
12901
12902impl BoardKind {
12903 /// The kind of an item titled `title`, filed under a project or not, holding `sub_issues`
12904 /// sub-issues and carrying the kind marker `marked` in its slot.
12905 ///
12906 /// The design prefix is read *first*, before either of the two rules that separate a
12907 /// project from a task. A document is not work whatever sub-issues it has and whatever
12908 /// marker it carries, and reading the prefix later would make a design issue with none of
12909 /// either an empty project. Being a sub-issue wins outright over the rest, and no marker
12910 /// overrides it: an issue filed under a project is that project's task even when it has
12911 /// sub-issues of its own.
12912 fn of(title: &str, filed: bool, sub_issues: u64, marked: Option<ItemKind>) -> Self {
12913 if title.starts_with(DESIGN_TITLE_PREFIX) {
12914 Self::Document
12915 } else if filed {
12916 Self::Work(ItemKind::Task)
12917 } else if sub_issues > 0 || marked == Some(ItemKind::Project) {
12918 Self::Work(ItemKind::Project)
12919 } else {
12920 Self::Work(ItemKind::Task)
12921 }
12922 }
12923
12924 /// What the change token's facets call this kind.
12925 const fn facet(self) -> change_token::FacetKind {
12926 match self {
12927 Self::Document => change_token::FacetKind::Document,
12928 Self::Work(ItemKind::Project) => change_token::FacetKind::Project,
12929 Self::Work(ItemKind::Task) => change_token::FacetKind::Task,
12930 }
12931 }
12932
12933 /// Whose half of `status_mapping` an item of this kind reads its status through. A
12934 /// document has no status of its own, so the task half stands in for whatever the issue
12935 /// holds; nothing reports it.
12936 const fn status_kind(self) -> ItemKind {
12937 match self {
12938 Self::Document => ItemKind::Task,
12939 Self::Work(kind) => kind,
12940 }
12941 }
12942
12943 /// How a refusal names this kind to the person reading it.
12944 const fn describes(self) -> &'static str {
12945 match self {
12946 Self::Document => "document",
12947 Self::Work(kind) => kind.marker(),
12948 }
12949 }
12950}
12951
12952/// Whether `labels` satisfies `filter`, matching by name, case-insensitively.
12953///
12954/// This is the local Markdown source's `labels_match`, spelled the same way on purpose:
12955/// the shared cross-source journeys assert one answer to one question, so two sources
12956/// that disagree about what "carries the label bug" means fail them.
12957fn labels_match(labels: &[Label], filter: &LabelFilter) -> bool {
12958 let holds = |name: &String| {
12959 labels
12960 .iter()
12961 .any(|label| label.name.eq_ignore_ascii_case(name))
12962 };
12963 (filter.any_of.is_empty() || filter.any_of.iter().any(holds))
12964 && filter.all_of.iter().all(holds)
12965 && !filter.none_of.iter().any(holds)
12966}
12967
12968/// Whether `category` is one of `statuses`. An empty list is unfiltered rather than
12969/// "keeps nothing", which is what lets a `Vec<StatusCategory>` spell no filter at all.
12970fn status_matches(category: StatusCategory, statuses: &[StatusCategory]) -> bool {
12971 statuses.is_empty() || statuses.contains(&category)
12972}
12973
12974/// Whether `title`/`content` satisfies `query`, matching case-insensitively.
12975///
12976/// `content` is the item's own prose — the body with this source's trailing metadata
12977/// comment already taken off — so a search never matches an encoding the author of the
12978/// issue never wrote.
12979fn text_matches(title: &str, content: Option<&str>, query: &TextQuery) -> bool {
12980 let terms = query.terms.to_lowercase();
12981 let in_title = title.to_lowercase().contains(&terms);
12982 let in_content = content.is_some_and(|body| body.to_lowercase().contains(&terms));
12983 match query.fields {
12984 TextFields::Title => in_title,
12985 TextFields::Content => in_content,
12986 TextFields::TitleOrContent => in_title || in_content,
12987 }
12988}
12989
12990/// Whether `task` satisfies `query`, with `project` deciding the project predicate.
12991///
12992/// The project predicate is passed separately because a read narrowed to one project has
12993/// already answered it by asking *that project* for its own items — and re-applying it
12994/// there would compare the caller's selector, which may be a project's **name**, against
12995/// the id of the project that name resolved to, and keep nothing. Every other read passes
12996/// `query.project` and applies it here, which is what keeps `projects` a predicate this
12997/// source really does apply.
12998fn task_matches(task: &Task, query: &TaskQuery, project: &ProjectFilter) -> bool {
12999 labels_match(&task.labels, &query.labels)
13000 && status_matches(task.status.category, &query.statuses)
13001 && (query.priorities.is_empty() || query.priorities.contains(&task.priority))
13002 && match project {
13003 ProjectFilter::Any => true,
13004 ProjectFilter::Orphans => task.project.is_none(),
13005 ProjectFilter::Is(id) => task.project.as_ref() == Some(id),
13006 }
13007 && query
13008 .text
13009 .as_ref()
13010 .is_none_or(|text| text_matches(&task.title, task.content.as_deref(), text))
13011 // Against the parsed metadata slot, and against the origin field, which is where
13012 // `Resolved::metadata` reads each of them from.
13013 && query.metadata_matches(&task.metadata)
13014 && query.origin_matches(&task.metadata)
13015}
13016
13017fn project_matches(project: &Project, query: &ProjectQuery) -> bool {
13018 labels_match(&project.labels, &query.labels)
13019 && status_matches(project.status.category, &query.statuses)
13020 && query
13021 .text
13022 .as_ref()
13023 .is_none_or(|text| text_matches(&project.title, project.content.as_deref(), text))
13024}
13025
13026/// The same three predicates a task query carries, minus the status filter.
13027///
13028/// A document is not work, so it has no status for one to compare against and the query
13029/// type carries none. The project predicate is the same one — a design issue filed under a
13030/// project issue is in that project, and one filed under nothing is in none — so it is
13031/// spelled the same way here rather than answered differently.
13032fn document_matches(document: &Document, query: &DocumentQuery, project: &ProjectFilter) -> bool {
13033 labels_match(&document.labels, &query.labels)
13034 && match project {
13035 ProjectFilter::Any => true,
13036 ProjectFilter::Orphans => document.project.is_none(),
13037 ProjectFilter::Is(id) => document.project.as_ref() == Some(id),
13038 }
13039 && query
13040 .text
13041 .as_ref()
13042 .is_none_or(|text| text_matches(&document.title, document.content.as_deref(), text))
13043}
13044
13045/// Why a GitHub item's change token does not vouch for its forward edges.
13046///
13047/// A forward edge names its far end and that far end's kind, and the near item's token moves
13048/// with the edges it holds but says nothing of the far ends themselves: a far end turning from a
13049/// task into a project when it gains a sub-issue, retitled into a document, or deleted moves
13050/// nothing the near token reads, so an edge kept at the near version could name a far end that
13051/// is no longer what it said.
13052const FAR_ENDS_UNCOVERED: &str = "a github item's change token does not attest a far end's kind, \
13053 whether it became a document, or its deletion, so its forward edges are always read from \
13054 github";
13055
13056/// The issue-search qualifier that keeps open issues alone, which a status filter naming only
13057/// categories an open issue reads as narrows a board-scoped search with.
13058const OPEN_QUALIFIER: &str = "is:open";
13059
13060/// The board filter `task status reconcile` reads the board through: closed items alone.
13061const CLOSED_FILTER: &str = "is:closed";
13062
13063/// The variables of one [`graphql::ISSUES_BY_REFERENCE`] request for `batch`: each slot names
13064/// one reference, and a slot the batch has none for names its last one again.
13065fn reference_batch(batch: &[IssueRef]) -> Value {
13066 let mut variables = serde_json::Map::new();
13067 for slot in 0..REFERENCE_BATCH {
13068 let issue = batch.get(slot).or(batch.last());
13069 variables.insert(format!("owner{slot}"), json!(issue.map(|i| &i.owner)));
13070 variables.insert(format!("name{slot}"), json!(issue.map(|i| &i.name)));
13071 variables.insert(format!("number{slot}"), json!(issue.map(|i| i.number)));
13072 }
13073 variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
13074 variables.insert("statusField".to_owned(), json!(STATUS_FIELD));
13075 variables.insert("duplicates".to_owned(), json!(true));
13076 Value::Object(variables)
13077}
13078
13079/// One issue an issue read by reference answered, in the shape every other read of an issue
13080/// hands its resolver: each board membership's `Status` value, read by name, as the one field
13081/// value that membership carries.
13082///
13083/// A membership's `status` is `null` when its card sits at no option — or on a board with no
13084/// field of that name — and otherwise an option value naming its option; anything else is not
13085/// an answer to this read, and is refused rather than read as a card at no option.
13086fn held_issue(issue: &Value) -> Result<Value, SourceError> {
13087 let malformed = |what: &str| SourceError::Malformed {
13088 message: format!("GitHub answered an issue read by reference with {what}"),
13089 };
13090 let mut issue = issue.clone();
13091 let nodes = issue
13092 .pointer_mut("/projectItems/nodes")
13093 .and_then(Value::as_array_mut)
13094 .ok_or_else(|| malformed("no list of board memberships"))?;
13095 for node in nodes {
13096 let node = node
13097 .as_object_mut()
13098 .ok_or_else(|| malformed("a board membership that is not an object"))?;
13099 let status = match node.get("status") {
13100 None => return Err(malformed("a board membership with no status member")),
13101 Some(Value::Null) => None,
13102 Some(status) if status.get("name").is_some_and(Value::is_string) => {
13103 Some(status.clone())
13104 }
13105 // A single-select value of another field type answers `{}`; only an option value
13106 // carries a name.
13107 Some(status) if status.as_object().is_some_and(serde_json::Map::is_empty) => None,
13108 Some(_) => return Err(malformed("a membership status that names no option")),
13109 };
13110 node.insert(
13111 "fieldValues".to_owned(),
13112 json!({"nodes": status.into_iter().collect::<Vec<_>>(),
13113 "pageInfo": {"hasNextPage": false}}),
13114 );
13115 }
13116 Ok(issue)
13117}
13118
13119/// The issue one slot of a read by reference answered, `None` when GitHub answered that
13120/// slot's repository or issue `null` — which [`GitHubProjectsSource::answer_unresolved`]
13121/// admitted only beside a `NOT_FOUND` naming that very slot — and a refusal when the answer
13122/// leaves the slot, or its `issue` member, out: an answer missing a member is malformed rather
13123/// than a reference nothing holds.
13124fn referenced_issue(data: &Value, slot: usize) -> Result<Option<&Value>, SourceError> {
13125 let alias = format!("r{slot}");
13126 let repository = data.get(&alias).ok_or_else(|| SourceError::Malformed {
13127 message: format!("GitHub answered a read by reference with no {alias}"),
13128 })?;
13129 if repository.is_null() {
13130 return Ok(None);
13131 }
13132 let issue = repository
13133 .get("issue")
13134 .ok_or_else(|| SourceError::Malformed {
13135 message: format!("GitHub answered {alias} of a read by reference with no issue"),
13136 })?;
13137 if issue.is_null() {
13138 return Ok(None);
13139 }
13140 // `Repository.issue` is typed `Issue`, so anything else is not an answer to this read.
13141 if optional_str(issue, "__typename")? != Some("Issue") {
13142 return Err(SourceError::Malformed {
13143 message: format!(
13144 "GitHub answered {alias} of a read by reference with an issue whose __typename \
13145 is not Issue"
13146 ),
13147 });
13148 }
13149 Ok(Some(issue))
13150}
13151
13152/// A closure reason as a person reads it: `NOT_PLANNED` as `not planned`.
13153fn spoken_reason(reason: &str) -> String {
13154 reason.to_ascii_lowercase().replace('_', " ")
13155}
13156
13157impl GitHubProjectsSource {
13158 /// Every task and project of this board whose card contradicts its issue's closure, each
13159 /// judged against its own kind's half of `status_mapping`, read through the board's own
13160 /// `is:closed` filter, page by page — never the board's unfiltered `items`.
13161 ///
13162 /// Each item the filter answers is confirmed from its own node: an open one, one whose
13163 /// closure reads `unknown`, and one whose card is already in line are left out whatever
13164 /// the filter let through. The board's fields ride along and are held for the option
13165 /// writes that follow, so those need no read of the board.
13166 async fn contradictions(&self) -> Result<Vec<StatusContradiction>, SourceError> {
13167 let mut after: Option<String> = None;
13168 let mut found = Vec::new();
13169 loop {
13170 let data = self
13171 .graphql(
13172 graphql::CLOSED_ITEMS,
13173 json!({"owner":self.owner,"number":self.project_number,
13174 "first":MAX_PAGE_SIZE,"after":after,"filter":CLOSED_FILTER,
13175 "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
13176 )
13177 .await?;
13178 let board = data
13179 .pointer("/closedItems/projectV2")
13180 .filter(|value| !value.is_null())
13181 .ok_or_else(|| SourceError::Refused {
13182 message: format!(
13183 "GitHub project {}/{} was not found or is not visible to the token",
13184 self.owner, self.project_number
13185 ),
13186 })?;
13187 *self.fields_cache()? = Some(BoardFields {
13188 id: BoardId::parse(required_str(board, "id")?)?,
13189 fields: board.get("fields").cloned().unwrap_or(Value::Null),
13190 });
13191 let items = board.get("items").ok_or_else(|| SourceError::Malformed {
13192 message: "GitHub project closed items are missing".into(),
13193 })?;
13194 for item in items
13195 .get("nodes")
13196 .and_then(Value::as_array)
13197 .ok_or_else(|| SourceError::Malformed {
13198 message: "GitHub project items.nodes is not an array".into(),
13199 })?
13200 {
13201 let Some(resolved) = self.resolve(item)? else {
13202 continue;
13203 };
13204 let BoardKind::Work(kind) = resolved.kind else {
13205 continue;
13206 };
13207 let Some((closed_as, name)) = self.statuses.contradicted(
13208 kind,
13209 resolved.option.as_deref(),
13210 resolved.closed,
13211 resolved.reason.as_deref(),
13212 ) else {
13213 continue;
13214 };
13215 found.push(StatusContradiction {
13216 closed_as,
13217 to: name.to_owned(),
13218 option: resolved.option.clone(),
13219 item: match kind {
13220 ItemKind::Task => ContradictedItem::Task(resolved.task()?),
13221 ItemKind::Project => ContradictedItem::Project(resolved.project()),
13222 },
13223 });
13224 }
13225 let info = items
13226 .get("pageInfo")
13227 .ok_or_else(|| SourceError::Malformed {
13228 message: "GitHub project items have no pageInfo".into(),
13229 })?;
13230 if !required_bool(info, "hasNextPage")? {
13231 return Ok(found);
13232 }
13233 let next = required_str(info, "endCursor")?;
13234 validate_cursor_progress(after.as_deref(), next)?;
13235 after = Some(next.to_owned());
13236 }
13237 }
13238
13239 /// What this board holds for each issue in `issues`, once each in the order given, read
13240 /// [`REFERENCE_BATCH`] references at a time — and nothing else of the board.
13241 ///
13242 /// An issue GitHub cannot find for this source's credential, and one with no item on this
13243 /// board, are both absent. A closure that already says the work will not be done here —
13244 /// not planned, duplicate — is skipped: a landing does not overrule it. An item closed as
13245 /// completed whose card sits at its kind's `done` option is unchanged, and every other item
13246 /// is due a `done` write. A project item — a plan's home card, its issue holding the plan's
13247 /// tasks as sub-issues — is held under exactly these rules, judged against the project
13248 /// kind's half of `status_mapping`.
13249 async fn holdings(&self, issues: &[IssueRef]) -> Result<Vec<IssueHolding>, SourceError> {
13250 let mut distinct: Vec<IssueRef> = Vec::with_capacity(issues.len());
13251 for issue in issues {
13252 if !distinct.contains(issue) {
13253 distinct.push(issue.clone());
13254 }
13255 }
13256 let done = |kind: ItemKind| {
13257 self.statuses
13258 .target(kind, StatusCategory::Done)
13259 .option()
13260 .unwrap_or("Done")
13261 .to_owned()
13262 };
13263 // GitHub numbers an issue with a GraphQL `Int`, a signed 32-bit value, so a reference past
13264 // its range names no issue any repository can hold: it is answered absent and never sent,
13265 // where binding it would have GitHub refuse the whole batch.
13266 let readable: Vec<IssueRef> = distinct
13267 .iter()
13268 .filter(|issue| i32::try_from(issue.number).is_ok())
13269 .cloned()
13270 .collect();
13271 let mut holdings = Vec::with_capacity(readable.len());
13272 for batch in readable.chunks(REFERENCE_BATCH) {
13273 let data = self
13274 .graphql(graphql::ISSUES_BY_REFERENCE, reference_batch(batch))
13275 .await?;
13276 for (slot, issue) in batch.iter().enumerate() {
13277 let node = referenced_issue(&data, slot)?;
13278 let resolved = match node {
13279 Some(node) => self.resolve_issue_as(&held_issue(node)?, false).await?,
13280 None => None,
13281 };
13282 // A task and a project are both held; a document is not work, so nothing is.
13283 let Some((item, kind)) = resolved.and_then(|item| match item.kind {
13284 BoardKind::Work(kind) => Some((item, kind)),
13285 BoardKind::Document => None,
13286 }) else {
13287 holdings.push(IssueHolding {
13288 issue: issue.clone(),
13289 completion: Completion::Absent,
13290 });
13291 continue;
13292 };
13293 let task = match kind {
13294 ItemKind::Task => {
13295 let held = item.task()?;
13296 HeldTask {
13297 id: held.id,
13298 kind,
13299 option: item.option.clone(),
13300 classification: held.classification,
13301 repositories: held.repositories,
13302 delivers: held.delivers,
13303 }
13304 }
13305 // A project delivers no task, so what its write keeps in step is nothing.
13306 ItemKind::Project => {
13307 let held = item.project();
13308 HeldTask {
13309 id: held.id,
13310 kind,
13311 option: item.option.clone(),
13312 classification: held.classification,
13313 repositories: held.repositories,
13314 delivers: Vec::new(),
13315 }
13316 }
13317 };
13318 let done = done(kind);
13319 let completion = match (item.closed, item.reason.as_deref()) {
13320 (true, Some(reason @ ("NOT_PLANNED" | "DUPLICATE"))) => Completion::Skipped {
13321 task,
13322 reason: format!("closed as {}", spoken_reason(reason)),
13323 },
13324 (true, None | Some("COMPLETED"))
13325 if item
13326 .option
13327 .as_deref()
13328 .is_some_and(|option| option.eq_ignore_ascii_case(&done)) =>
13329 {
13330 Completion::Unchanged { task }
13331 }
13332 _ => {
13333 let kept = item.clone();
13334 self.held_cache
13335 .lock()
13336 .map_err(|_| SourceError::Unavailable {
13337 message: "items read by issue reference were left \
13338 inconsistent; run the command again"
13339 .into(),
13340 })?
13341 .insert(kept.id.clone(), kept);
13342 Completion::Due {
13343 task,
13344 to: done.clone(),
13345 }
13346 }
13347 };
13348 holdings.push(IssueHolding {
13349 issue: issue.clone(),
13350 completion,
13351 });
13352 }
13353 }
13354 let mut read = holdings.into_iter();
13355 let mut answered = Vec::with_capacity(distinct.len());
13356 for issue in distinct {
13357 if i32::try_from(issue.number).is_ok() {
13358 answered.push(read.next().ok_or_else(|| SourceError::Malformed {
13359 message: format!("no holding was read for {issue}"),
13360 })?);
13361 } else {
13362 answered.push(IssueHolding {
13363 issue,
13364 completion: Completion::Absent,
13365 });
13366 }
13367 }
13368 Ok(answered)
13369 }
13370}
13371
13372#[async_trait::async_trait]
13373impl TaskSource for GitHubProjectsSource {
13374 fn kind(&self) -> &'static str {
13375 KIND
13376 }
13377 fn attach_body_markup(&self, markup: onetaskgraph_plugin_api::SharedBodyMarkup) {
13378 let _ = self.body_markup.set(markup);
13379 }
13380 fn order_versions(&self, a: &str, b: &str) -> Option<std::cmp::Ordering> {
13381 change_token::order(a, b)
13382 }
13383 /// A task's fields and comments: §4.13d of `docs/plugin-protocol.md` states, member by
13384 /// member, the change each part of the token moves for. Not its forward edges — see
13385 /// [`FAR_ENDS_UNCOVERED`].
13386 fn version_coverage(&self) -> VersionCoverage {
13387 VersionCoverage {
13388 forward_edges: Coverage::Uncovered {
13389 reason: FAR_ENDS_UNCOVERED.to_owned(),
13390 },
13391 ..VersionCoverage::whole()
13392 }
13393 }
13394 /// Every part, for a copy's skip: the relationship count moves for an edge added or removed
13395 /// and for a far end deleted, so the token sees every change to a task's edges but one — a
13396 /// far end's own kind flipping, by gaining a sub-issue or being retitled into a document,
13397 /// which `spike-coverage` measured stale 1 of 1. A skipping recopy does not carry that, and
13398 /// the next copy that reads the task does (`docs/plugin-protocol.md` §4.13d).
13399 // 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.
13400 fn copy_version_coverage(&self) -> VersionCoverage {
13401 VersionCoverage::whole()
13402 }
13403 /// A project's fields, and not its forward edges, for the same reason a task's are not.
13404 fn project_version_coverage(&self) -> ProjectVersionCoverage {
13405 ProjectVersionCoverage {
13406 fields: Coverage::Covered,
13407 forward_edges: Coverage::Uncovered {
13408 reason: FAR_ENDS_UNCOVERED.to_owned(),
13409 },
13410 }
13411 }
13412 /// A document's fields and the manifest of its image assets, which its metadata slot
13413 /// records: both are the issue's body, whose every edit moves the token's issue time.
13414 fn document_version_coverage(&self) -> DocumentVersionCoverage {
13415 DocumentVersionCoverage::whole()
13416 }
13417 fn write_context(&self, id: &NativeId) -> Option<Value> {
13418 self.context_of(id)
13419 }
13420 fn board_context(&self) -> Option<Value> {
13421 self.board_context_held()
13422 }
13423 fn prime_write(&self, id: &NativeId, context: &Value, board: Option<&Value>) -> bool {
13424 self.prime(id, context, board)
13425 }
13426 fn unprime(&self, id: &NativeId) {
13427 self.unprime_held(id);
13428 }
13429 fn placement_context(&self) -> Option<Value> {
13430 self.placement_held()
13431 }
13432 fn prime_placement(&self, context: &Value) -> bool {
13433 self.prime_placement_held(context)
13434 }
13435 fn written_context(&self, id: &NativeId) -> Option<Value> {
13436 self.written_held(id)
13437 }
13438 fn prime_written(&self, id: &NativeId, context: &Value, task: &Task) -> bool {
13439 self.prime_written_held(id, context, task)
13440 }
13441 fn begin_journal(&self) {
13442 self.expected.begin();
13443 }
13444 fn end_journal(&self) -> Option<WriteJournal> {
13445 Some(self.expected.end())
13446 }
13447 fn capabilities(&self) -> Capabilities {
13448 Capabilities {
13449 projects: Support::Native,
13450 documents: Support::Native,
13451 comments: Support::Native,
13452 assets: Support::Native,
13453 priority: if self.priorities.is_some() {
13454 Support::Native
13455 } else {
13456 Support::Unsupported
13457 },
13458 filter_by_priority: Support::Native,
13459 filter_by_comment_activity: Support::Native,
13460 filter_by_metadata: Support::Native,
13461 filter_by_origin: Support::Native,
13462 reconcile_status: Support::Native,
13463 complete_by_issue: Support::Native,
13464 orphan_tasks: Support::Native,
13465 filter_by_label: Support::Native,
13466 filter_by_status: Support::Native,
13467 search_title: Support::Native,
13468 search_content: Support::Native,
13469 task_dependencies: DependencySupport::BothDirections,
13470 project_dependencies: DependencySupport::BothDirections,
13471 max_page_size: MAX_PAGE_SIZE,
13472 }
13473 }
13474 async fn visibility(
13475 &self,
13476 target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13477 ) -> Result<onetaskgraph_plugin_api::Visibility, SourceError> {
13478 self.write_visibility(target).await
13479 }
13480 async fn visibility_with_project(
13481 &self,
13482 target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13483 project: &onetaskgraph_plugin_api::WriteTarget<'_>,
13484 ) -> Result<onetaskgraph_plugin_api::Visibility, SourceError> {
13485 self.write_visibility_with_project(target, project).await
13486 }
13487 fn declare_visibility(&self, declared: onetaskgraph_plugin_api::Visibility) {
13488 self.declared_private.store(
13489 declared == onetaskgraph_plugin_api::Visibility::Private,
13490 std::sync::atomic::Ordering::SeqCst,
13491 );
13492 }
13493 fn scope_visibility<'a>(
13494 &'a self,
13495 operation: std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send + 'a>>,
13496 ) -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send + 'a>> {
13497 self.scope_visibility_reads(operation)
13498 }
13499
13500 /// Where a create of `target` would file its issue, by [`Self::creation_rule`] — the rule
13501 /// the create applies — reading the parent project's issue only when `target` names one.
13502 async fn creation_repository(
13503 &self,
13504 target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13505 ) -> Result<Option<Repository>, SourceError> {
13506 let onetaskgraph_plugin_api::WriteTarget::New {
13507 repositories,
13508 project,
13509 } = target
13510 else {
13511 return Ok(None);
13512 };
13513 let target = self
13514 .creation_rule(repositories, *project, "a new item")
13515 .await?;
13516 Repository::try_from(target.origin())
13517 .map(Some)
13518 .map_err(|message| SourceError::Malformed { message })
13519 }
13520 /// Every issue of the repository a create of `target` files in whose body carries `key` in
13521 /// its metadata slot, read off that repository's issues newest first back to `since` —
13522 /// never through search, whose index lags a fresh issue by seconds.
13523 async fn created_with_key(
13524 &self,
13525 record: onetaskgraph_plugin_api::MetadataRecord,
13526 key: &str,
13527 since: Option<DateTime<Utc>>,
13528 target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13529 ) -> Result<Vec<NativeId>, SourceError> {
13530 let _ = record;
13531 let onetaskgraph_plugin_api::WriteTarget::New {
13532 repositories,
13533 project,
13534 } = target
13535 else {
13536 return Ok(Vec::new());
13537 };
13538 let repository = self
13539 .creation_rule(repositories, *project, "a new item")
13540 .await?;
13541 let wanted = Value::String(key.to_owned());
13542 let mut found = Vec::new();
13543 let mut after: Option<String> = None;
13544 let mut followed = std::collections::HashSet::new();
13545 loop {
13546 let data = self
13547 .graphql(
13548 graphql::ISSUES_BY_CREATION,
13549 json!({"owner": repository.owner, "name": repository.name,
13550 "first": MAX_PAGE_SIZE, "after": after}),
13551 )
13552 .await?;
13553 let issues =
13554 data.pointer("/repository/issues")
13555 .ok_or_else(|| SourceError::Malformed {
13556 message: format!(
13557 "GitHub answered no issues for repository {}",
13558 repository.slug()
13559 ),
13560 })?;
13561 let malformed = |what: &str| SourceError::Malformed {
13562 message: format!(
13563 "GitHub answered a page of repository {}'s issues {what}",
13564 repository.slug()
13565 ),
13566 };
13567 let nodes = issues
13568 .get("nodes")
13569 .and_then(Value::as_array)
13570 .ok_or_else(|| malformed("with no list of issues"))?;
13571 let mut older = false;
13572 for node in nodes {
13573 let id = node
13574 .get("id")
13575 .and_then(Value::as_str)
13576 .filter(|id| !id.is_empty())
13577 .ok_or_else(|| malformed("holding an issue with no id"))?;
13578 let created = optional_time(node, "createdAt")?
13579 .ok_or_else(|| malformed("holding an issue with no creation time"))?;
13580 if since.is_some_and(|since| created < since) {
13581 older = true;
13582 break;
13583 }
13584 let body = match node.get("body") {
13585 Some(Value::String(body)) => Some(body.clone()),
13586 Some(Value::Null) => None,
13587 _ => return Err(malformed("holding an issue with no body member")),
13588 };
13589 let (_, metadata) = metadata_body(body)?;
13590 if metadata.get(MetadataKey::CREATE_KEY) == Some(&wanted) {
13591 found.push(NativeId(id.to_owned()));
13592 }
13593 }
13594 let next = issues
13595 .pointer("/pageInfo/hasNextPage")
13596 .and_then(Value::as_bool)
13597 .ok_or_else(|| malformed("that does not say whether another follows"))?;
13598 if older || !next {
13599 return Ok(found);
13600 }
13601 // A page that says another follows names where it starts, and never a page already
13602 // read: anything else would ask for the same pages again for ever.
13603 let cursor = issues
13604 .pointer("/pageInfo/endCursor")
13605 .and_then(Value::as_str)
13606 .filter(|cursor| !cursor.is_empty())
13607 .map(str::to_owned);
13608 if cursor
13609 .as_ref()
13610 .is_none_or(|cursor| !followed.insert(cursor.clone()))
13611 {
13612 return Err(SourceError::Malformed {
13613 message: format!(
13614 "GitHub answered a page of repository {}'s issues that says another \
13615 follows without a new cursor to it",
13616 repository.slug()
13617 ),
13618 });
13619 }
13620 after = cursor;
13621 }
13622 }
13623
13624 /// `Repository.issues` lists an issue GitHub committed only once its listing catches up.
13625 fn create_key_lags(&self) -> bool {
13626 true
13627 }
13628
13629 /// The task as this process's own latest write of it left it, from the item it recorded
13630 /// that write as — never a request. Its `version` is always `None`: a board's change token
13631 /// is given only once an item has gone unchanged for [`change_token::SETTLED`], which an
13632 /// item this command has just written has not, so no write's own answer can vouch for one.
13633 async fn written_task(&self, id: &NativeId) -> Option<Task> {
13634 let held = self
13635 .created()
13636 .ok()?
13637 .iter()
13638 .find(|held| held.id == *id)
13639 .cloned()
13640 .or_else(|| {
13641 self.updated()
13642 .ok()?
13643 .iter()
13644 .find(|held| held.id == *id)
13645 .cloned()
13646 })?;
13647 let mut task = held.task().ok()?;
13648 task.version = None;
13649 Some(task)
13650 }
13651
13652 /// Why `creation_owners` refuses a create of `target`, by [`Self::creation_rule`] — read
13653 /// only when the list is not empty, so a source restricting nothing reads nothing.
13654 async fn creation_refusal(
13655 &self,
13656 target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13657 ) -> Result<Option<String>, SourceError> {
13658 let onetaskgraph_plugin_api::WriteTarget::New {
13659 repositories,
13660 project,
13661 } = target
13662 else {
13663 return Ok(None);
13664 };
13665 if self.creation_owners.is_empty() {
13666 return Ok(None);
13667 }
13668 let target = self
13669 .creation_rule(repositories, *project, "a new item")
13670 .await?;
13671 Ok(self.owner_refusal(&target))
13672 }
13673
13674 async fn health(&self) -> Result<Health, SourceError> {
13675 let board = self.board_page(None, 1).await?;
13676 Ok(Health {
13677 reachable: true,
13678 detail: Some(format!(
13679 "reading GitHub project {}/{} ({})",
13680 self.owner,
13681 self.project_number,
13682 required_str(&board, "title")?
13683 )),
13684 })
13685 }
13686 /// The board's own node id, digested: the one request reading its fields, or none when
13687 /// this command has read them already.
13688 async fn designator(&self) -> Result<Option<Designator>, SourceError> {
13689 let board = self.board_fields().await?;
13690 Designator::of_backend(KIND, board.id.as_str())
13691 .map(Some)
13692 .map_err(|message| SourceError::Malformed { message })
13693 }
13694 async fn get_task(&self, id: &NativeId) -> Result<Option<Task>, SourceError> {
13695 self.item_by_id(id)
13696 .await?
13697 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
13698 .map(|item| item.task())
13699 .transpose()
13700 }
13701 async fn task_assets(
13702 &self,
13703 id: &NativeId,
13704 ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
13705 self.held_assets(id, BoardKind::Work(ItemKind::Task)).await
13706 }
13707 async fn task_asset(
13708 &self,
13709 id: &NativeId,
13710 name: &onetaskgraph_plugin_api::AssetName,
13711 ) -> Result<Option<Vec<u8>>, SourceError> {
13712 self.held_asset(id, BoardKind::Work(ItemKind::Task), name)
13713 .await
13714 }
13715 async fn set_task_rendering_with_assets(
13716 &self,
13717 id: &NativeId,
13718 content: &str,
13719 provenance: &Value,
13720 _answers: &BTreeMap<String, Value>,
13721 assets: &onetaskgraph_plugin_api::AssetWrite,
13722 ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
13723 self.replace_rendering(
13724 id,
13725 BoardKind::Work(ItemKind::Task),
13726 content,
13727 provenance,
13728 Some(assets),
13729 )
13730 .await
13731 }
13732 async fn document_assets(
13733 &self,
13734 id: &NativeId,
13735 ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
13736 self.held_assets(id, BoardKind::Document).await
13737 }
13738 async fn document_asset(
13739 &self,
13740 id: &NativeId,
13741 name: &onetaskgraph_plugin_api::AssetName,
13742 ) -> Result<Option<Vec<u8>>, SourceError> {
13743 self.held_asset(id, BoardKind::Document, name).await
13744 }
13745 async fn set_document_rendering_with_assets(
13746 &self,
13747 id: &NativeId,
13748 content: &str,
13749 provenance: &Value,
13750 _answers: &BTreeMap<String, Value>,
13751 assets: &onetaskgraph_plugin_api::AssetWrite,
13752 ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
13753 self.replace_rendering(id, BoardKind::Document, content, provenance, Some(assets))
13754 .await
13755 }
13756 async fn get_project(&self, id: &NativeId) -> Result<Option<Project>, SourceError> {
13757 Ok(self
13758 .item_by_id(id)
13759 .await?
13760 .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
13761 .map(|item| item.project()))
13762 }
13763 async fn query_tasks(
13764 &self,
13765 query: &TaskQuery,
13766 page: &PageRequest,
13767 ) -> Result<Page<Task>, SourceError> {
13768 validate_page(page)?;
13769 refuse_unsearchable(query)?;
13770 // A closed issue reads `done`, `cancelled` or — closed for a reason that implies no
13771 // category — `unknown`; every other category reads only off an open issue or a draft. So
13772 // a status filter naming none of those three asks every issue search it sends for open
13773 // issues alone; each candidate's category is still confirmed from its own item below.
13774 let open_only = !query.statuses.is_empty()
13775 && query.statuses.iter().all(|category| {
13776 !matches!(
13777 category,
13778 StatusCategory::Done | StatusCategory::Cancelled | StatusCategory::Unknown
13779 )
13780 });
13781 if query.origin.is_none() && !matches!(query.project, ProjectFilter::Is(_)) {
13782 let mut qualifiers: Vec<String> = [
13783 query.commented_since.map(updated_qualifier),
13784 narrowing_qualifiers(query),
13785 ]
13786 .into_iter()
13787 .flatten()
13788 .collect();
13789 // A narrowed search never answers a draft, which is no issue, so narrowing it to
13790 // open issues too leaves out nothing such a search could have answered.
13791 if !qualifiers.is_empty() {
13792 if open_only {
13793 qualifiers.push(OPEN_QUALIFIER.to_owned());
13794 }
13795 return self.search_tasks(query, page, &qualifiers.join(" ")).await;
13796 }
13797 }
13798
13799 // A read narrowed to one project asks that project for its own tasks, so nothing
13800 // about it costs what the rest of the board holds. A read carrying a text, metadata
13801 // or origin predicate asks GitHub the narrower question those predicates are, and a
13802 // read narrowed to comment activity alone asks the board's own issue search for the
13803 // issues updated since, which is every issue a comment could have been written or
13804 // edited on since. Every other task read is a question about the whole board and is
13805 // answered by reading it.
13806 let (held, membership) = match (&query.project, query.commented_since) {
13807 (ProjectFilter::Is(project), _) => (
13808 match self.held_task_children(project)? {
13809 // Taken as `project_children` takes the project's own sub-issue read: with
13810 // every item this process wrote under it since, and none it moved away.
13811 Some(held) => self.completed_with_written(
13812 held.into_iter()
13813 .filter(|child| child.parent.as_ref() == Some(project))
13814 .collect(),
13815 |own| own.parent.as_ref() == Some(project),
13816 )?,
13817 None => self.project_children(project).await?,
13818 },
13819 // Answered by where these items came from; see `task_matches`.
13820 &ProjectFilter::Any,
13821 ),
13822 (ProjectFilter::Any | ProjectFilter::Orphans, since) => {
13823 match (self.narrowed(query).await?, since) {
13824 (Some(narrowed), _) => (narrowed, &query.project),
13825 (None, Some(since)) => (self.updated_since(since).await?, &query.project),
13826 // The board's own items list every draft; only the search half, which
13827 // completes them with issues they lag behind on, is narrowed — a closed
13828 // issue that half leaves out is at no category the filter keeps.
13829 (None, None) if open_only => (
13830 self.board_completed_by(Some(OPEN_QUALIFIER)).await?.items,
13831 &query.project,
13832 ),
13833 (None, None) => (self.board().await?.items, &query.project),
13834 }
13835 }
13836 };
13837 // Filtered before paged: a page of a filtered result is a page of the survivors,
13838 // never the survivors of a page.
13839 let mut tasks = Vec::new();
13840 for item in held
13841 .iter()
13842 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
13843 {
13844 let task = item.task()?;
13845 if task_matches(&task, query, membership)
13846 && self.commented_since(item, query.commented_since).await?
13847 {
13848 tasks.push(task);
13849 }
13850 }
13851 Ok(offset_page(
13852 tasks,
13853 numeric_cursor(page.cursor.as_ref())?,
13854 page.limit.min(MAX_PAGE_SIZE) as usize,
13855 ))
13856 }
13857 async fn query_projects(
13858 &self,
13859 query: &ProjectQuery,
13860 page: &PageRequest,
13861 ) -> Result<Page<Project>, SourceError> {
13862 validate_page(page)?;
13863 refuse_unsearchable_text(query.text.as_ref())?;
13864 // The projects a board holds are found by an issue search scoped to that board,
13865 // never by walking the board's own item connection: what tells a project from a
13866 // task is the `parent` each issue carries, which costs nothing to read. A query
13867 // carrying a text asks that search for the text too, so it reads the issues that
13868 // hold it rather than every issue of the board.
13869 let held = match self.text_searched(query.text.as_ref()).await? {
13870 Some(searched) => searched,
13871 None => self.board_issues().await?,
13872 };
13873 let projects = held
13874 .iter()
13875 .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
13876 .map(Resolved::project)
13877 .filter(|project| project_matches(project, query))
13878 .collect();
13879 Ok(offset_page(
13880 projects,
13881 numeric_cursor(page.cursor.as_ref())?,
13882 page.limit.min(MAX_PAGE_SIZE) as usize,
13883 ))
13884 }
13885 async fn query_projects_by_origin(
13886 &self,
13887 origin: &str,
13888 page: &PageRequest,
13889 ) -> Result<Page<Project>, SourceError> {
13890 validate_page(page)?;
13891 let projects = self
13892 .copied_from(origin, BoardKind::Work(ItemKind::Project))
13893 .await?
13894 .iter()
13895 .map(Resolved::project)
13896 .collect();
13897 Ok(offset_page(
13898 projects,
13899 numeric_cursor(page.cursor.as_ref())?,
13900 page.limit.min(MAX_PAGE_SIZE) as usize,
13901 ))
13902 }
13903 async fn get_document(&self, id: &NativeId) -> Result<Option<Document>, SourceError> {
13904 let Some(item) = self
13905 .document_by_id(id)
13906 .await?
13907 .filter(|item| item.kind == BoardKind::Document)
13908 else {
13909 return Ok(None);
13910 };
13911 self.expected.read_document(&item);
13912 Ok(Some(item.document()))
13913 }
13914 async fn query_documents_by_origin(
13915 &self,
13916 origin: &str,
13917 page: &PageRequest,
13918 ) -> Result<Page<Document>, SourceError> {
13919 validate_page(page)?;
13920 let documents = self
13921 .copied_from(origin, BoardKind::Document)
13922 .await?
13923 .iter()
13924 .map(Resolved::document)
13925 .collect();
13926 Ok(offset_page(
13927 documents,
13928 numeric_cursor(page.cursor.as_ref())?,
13929 page.limit.min(MAX_PAGE_SIZE) as usize,
13930 ))
13931 }
13932 async fn query_documents(
13933 &self,
13934 query: &DocumentQuery,
13935 page: &PageRequest,
13936 ) -> Result<Page<Document>, SourceError> {
13937 validate_page(page)?;
13938 // Narrowed to one project, this is the same sub-issue read a task list scoped to
13939 // that project makes — a document filed under a project is a sub-issue of it too,
13940 // and which of them come back is the kind this caller asked for. Unscoped, a query
13941 // carrying a text asks the board-scoped issue search for it, as a task query does,
13942 // and only one carrying none reads the board.
13943 let (held, membership) = match &query.project {
13944 ProjectFilter::Is(project) => (
13945 self.project_children(project).await?,
13946 // Answered by where these items came from; see `task_matches`.
13947 &ProjectFilter::Any,
13948 ),
13949 ProjectFilter::Any | ProjectFilter::Orphans => {
13950 refuse_unsearchable_text(query.text.as_ref())?;
13951 match self.text_searched(query.text.as_ref()).await? {
13952 Some(searched) => (searched, &query.project),
13953 None => (self.board().await?.items, &query.project),
13954 }
13955 }
13956 };
13957 // Filtered before paged, exactly as a task read is: a page of a filtered result is
13958 // a page of the survivors, never the survivors of a page.
13959 let documents = held
13960 .iter()
13961 .filter(|item| item.kind == BoardKind::Document)
13962 .map(Resolved::document)
13963 .filter(|document| document_matches(document, query, membership))
13964 .collect();
13965 Ok(offset_page(
13966 documents,
13967 numeric_cursor(page.cursor.as_ref())?,
13968 page.limit.min(MAX_PAGE_SIZE) as usize,
13969 ))
13970 }
13971 async fn labels(&self, page: &PageRequest) -> Result<Page<Label>, SourceError> {
13972 validate_page(page)?;
13973 let offset = numeric_cursor(page.cursor.as_ref())?;
13974 let mut labels = self
13975 .board()
13976 .await?
13977 .items
13978 .into_iter()
13979 .flat_map(|item| item.labels)
13980 .fold(Vec::new(), |mut all, label| {
13981 if !all.iter().any(|x: &Label| x.id == label.id) {
13982 all.push(label);
13983 }
13984 all
13985 });
13986 labels.sort_by(|a, b| a.name.cmp(&b.name).then(a.id.0.cmp(&b.id.0)));
13987 Ok(offset_page(
13988 labels,
13989 offset,
13990 page.limit.min(MAX_PAGE_SIZE) as usize,
13991 ))
13992 }
13993 async fn task_dependencies(
13994 &self,
13995 id: &NativeId,
13996 direction: Direction,
13997 page: &PageRequest,
13998 ) -> Result<Page<DependencyEdge>, SourceError> {
13999 self.dependencies(id, ItemKind::Task, direction, page).await
14000 }
14001 async fn project_dependencies(
14002 &self,
14003 id: &NativeId,
14004 direction: Direction,
14005 page: &PageRequest,
14006 ) -> Result<Page<DependencyEdge>, SourceError> {
14007 self.dependencies(id, ItemKind::Project, direction, page)
14008 .await
14009 }
14010
14011 fn writes(&self) -> WriteSupport {
14012 WriteSupport::Supported
14013 }
14014
14015 /// Create or update one task.
14016 ///
14017 /// Its `delivers` and `delivered_by` are checked before anything is read or written —
14018 /// neither may name the task itself or name one task twice — and land in the body's
14019 /// metadata slot under their reserved keys, in place of any caller metadata of those
14020 /// names.
14021 async fn write_task(&self, write: &ItemWrite<Task>) -> Result<NativeId, SourceError> {
14022 self.write_task_assets(write, None)
14023 .await
14024 .map(|written| written.id)
14025 }
14026
14027 async fn read_ahead_origins_for_copy(&self, origins: &[String]) -> Result<(), SourceError> {
14028 self.origins_ahead(origins).await
14029 }
14030
14031 async fn member_classifications(
14032 &self,
14033 project: &NativeId,
14034 ) -> Result<Option<Vec<(Classification, Vec<Repository>)>>, SourceError> {
14035 Ok(self
14036 .member_classes
14037 .lock()
14038 .map_err(|_| SourceError::Unavailable {
14039 message: "a project's members held for its classification were left \
14040 inconsistent; run the command again"
14041 .into(),
14042 })?
14043 .remove(project))
14044 }
14045
14046 /// The issue's own web address, as the `createIssue` that made it in this command answered
14047 /// it: a task, a project and a document are each an issue here.
14048 fn written_location(
14049 &self,
14050 _record: onetaskgraph_plugin_api::MetadataRecord,
14051 id: &NativeId,
14052 ) -> Option<Location> {
14053 self.written
14054 .lock()
14055 .unwrap_or_else(std::sync::PoisonError::into_inner)
14056 .get(id)
14057 .cloned()
14058 .map(Location::Url)
14059 }
14060
14061 /// [`MUTATION_BATCH`], while writes are held back: [`graphql::CREATE_ISSUES`] creates that many
14062 /// in one request, and a copy's tasks are created together only then, since their fields
14063 /// and edges are what is held back.
14064 fn create_batch_limit(&self) -> usize {
14065 if self.deferring().unwrap_or(false) {
14066 MUTATION_BATCH
14067 } else {
14068 1
14069 }
14070 }
14071
14072 // 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.
14073 async fn write_tasks(&self, writes: &[ItemWrite<Task>]) -> Result<Vec<NativeId>, SourceError> {
14074 if let Some(write) = writes.iter().find(|write| write.target.is_some()) {
14075 return Err(SourceError::Refused {
14076 message: format!(
14077 "write_tasks creates tasks, and the write of {} names the existing task {}; \
14078 next: update it with write_task",
14079 write.item.id,
14080 write.target.as_ref().map_or("", |target| target.0.as_str())
14081 ),
14082 });
14083 }
14084 if !self.deferring()? {
14085 let mut created = Vec::with_capacity(writes.len());
14086 for write in writes {
14087 match self.write_task(write).await {
14088 Ok(id) => created.push(id),
14089 Err(error) => {
14090 let mut left: Vec<&str> = Vec::new();
14091 for id in &created {
14092 if self.delete_issue(id).await.is_err() {
14093 left.push(id.0.as_str());
14094 }
14095 }
14096 return Err(left_behind(error, &left));
14097 }
14098 }
14099 }
14100 return Ok(created);
14101 }
14102 self.create_together(writes).await
14103 }
14104
14105 fn begin_deferred_writes(&self) {
14106 if let Ok(mut deferred) = self.deferred.lock() {
14107 deferred.get_or_insert_with(Deferred::default);
14108 }
14109 }
14110
14111 async fn finish_writes(&self) -> Result<(), SourceError> {
14112 self.send_held_updates().await?;
14113 self.send_held_back().await
14114 }
14115
14116 async fn write_task_with_assets(
14117 &self,
14118 write: &ItemWrite<Task>,
14119 _answers: Option<&BTreeMap<String, Value>>,
14120 assets: &onetaskgraph_plugin_api::AssetWrite,
14121 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
14122 self.write_task_assets(write, Some(assets)).await
14123 }
14124
14125 async fn write_project(&self, write: &ItemWrite<Project>) -> Result<NativeId, SourceError> {
14126 self.write_item(
14127 &Incoming {
14128 written: Written::Work(ItemKind::Project, &write.item.status),
14129 title: &write.item.title,
14130 content: write.item.content.as_deref(),
14131 assets: None,
14132 labels: &write.item.labels,
14133 metadata: &write.item.metadata,
14134 repositories: &write.item.repositories,
14135 classification: write.item.classification,
14136 parent: None,
14137 delivers: &[],
14138 delivered_by: &[],
14139 priority: None,
14140 },
14141 write.target.as_ref(),
14142 &write.depends_on,
14143 )
14144 .await
14145 .map(|written| written.id)
14146 }
14147
14148 /// Create or update one document, which is one issue titled the way this board spells
14149 /// a document.
14150 ///
14151 /// Everything else is exactly a task write: caller metadata goes to the same canonical
14152 /// JSON slot at the end of the body and comes back with its JSON types intact, a key
14153 /// or a field this board cannot carry is refused by name rather than dropped, a target
14154 /// naming an issue this board does not hold is refused rather than created, and an
14155 /// issue this call created is taken back when the rest of the write fails.
14156 async fn write_document(&self, write: &ItemWrite<Document>) -> Result<NativeId, SourceError> {
14157 self.write_document_assets(write, None)
14158 .await
14159 .map(|written| written.id)
14160 }
14161
14162 async fn write_document_with_assets(
14163 &self,
14164 write: &ItemWrite<Document>,
14165 _answers: Option<&BTreeMap<String, Value>>,
14166 assets: &onetaskgraph_plugin_api::AssetWrite,
14167 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
14168 self.write_document_assets(write, Some(assets)).await
14169 }
14170
14171 /// Refused exactly as the write refuses it, from what the write reads: the mapping first,
14172 /// which reads nothing; then the board's `Status` option. Over an existing item that is
14173 /// read off the item, as the write reads it, and the item is held among this command's
14174 /// resolved records so the write that follows reuses that read rather than repeating it;
14175 /// an item that does not carry the field takes the board's fields, which are held once
14176 /// read. A create is checked against the board's fields only when this command already
14177 /// holds them, because a create reads them together with its repository, in one request,
14178 /// and refuses a missing option before it writes anything.
14179 async fn check_status_write(
14180 &self,
14181 kind: ItemKind,
14182 category: StatusCategory,
14183 target: Option<&NativeId>,
14184 ) -> Result<(), SourceError> {
14185 let status = self.resolved_target(kind, category)?;
14186 if status.option().is_none() {
14187 return Ok(());
14188 }
14189 let fields = match target {
14190 Some(target) => {
14191 // A target this board does not hold is the write's own refusal to make.
14192 let Some(item) = self.bound_item(target).await? else {
14193 return Ok(());
14194 };
14195 self.resolved_cache()?.insert(target.clone(), item.clone());
14196 // Only the `Status` field decides whether the write can land.
14197 self.fields_for(Some(&item), false, true, false)
14198 .await?
14199 .fields
14200 }
14201 None => {
14202 let held = self
14203 .board_cache()?
14204 .as_ref()
14205 .map(|board| board.fields.clone());
14206 match held.or_else(|| {
14207 self.fields_cache()
14208 .ok()
14209 .and_then(|cache| cache.as_ref().map(|board| board.fields.clone()))
14210 }) {
14211 Some(fields) => fields,
14212 None => return Ok(()),
14213 }
14214 }
14215 };
14216 self.column_for(&fields, kind, category, &status)
14217 .map(|_| ())
14218 }
14219
14220 /// What a write of `status` reads back as: the `Status` option its category maps to for
14221 /// `kind`, and for a closing category the closure that option and its fixed reason read
14222 /// as — exactly what [`BoardStatuses::status`] reports of the item once written. A category
14223 /// this instance has no option for is answered as given, since writing it is refused.
14224 fn status_as_written(&self, kind: ItemKind, status: &Status) -> Status {
14225 match self.statuses.target(kind, status.category) {
14226 StatusTarget::Column(name) => {
14227 self.statuses.status(kind, Some(name.as_str()), false, None)
14228 }
14229 StatusTarget::Terminal(name, reason) => {
14230 self.statuses
14231 .status(kind, Some(name.as_str()), true, Some(reason.reason()))
14232 }
14233 StatusTarget::Disabled(_) => status.clone(),
14234 }
14235 }
14236
14237 /// Set one task's status alone.
14238 ///
14239 /// An open target reopens a closed issue with an `updateIssue` carrying only its
14240 /// `stateInput`, then selects the board option with `updateProjectV2ItemFieldValue`; a
14241 /// terminal target selects its mapped option, then closes with its fixed reason. No
14242 /// request carries a title, a body or a label. The status
14243 /// answered is what [`BoardStatuses::status`] reads off the state just written, which is
14244 /// what a re-read reports.
14245 async fn set_task_status(
14246 &self,
14247 id: &NativeId,
14248 category: StatusCategory,
14249 ) -> Result<Option<Status>, SourceError> {
14250 self.set_status(id, ItemKind::Task, category).await
14251 }
14252
14253 /// Set one task's status and answer the task with it, reading the item once: the read the
14254 /// task is answered from is handed to the status write, which would otherwise read the
14255 /// same issue again — two requests on a fresh process, the read and the field write,
14256 /// where the default's `get_task` before `set_task_status` sent three.
14257 async fn set_task_status_reading(
14258 &self,
14259 id: &NativeId,
14260 category: StatusCategory,
14261 ) -> Result<Option<Task>, SourceError> {
14262 // Refused before anything is read, in the words the write itself refuses it.
14263 self.resolved_target(ItemKind::Task, category)?;
14264 let Some(item) = self
14265 .bound_item(id)
14266 .await?
14267 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
14268 else {
14269 return Ok(None);
14270 };
14271 let mut task = item.task()?;
14272 self.held_cache
14273 .lock()
14274 .map_err(|_| SourceError::Unavailable {
14275 message: "items read by issue reference were left inconsistent; run the \
14276 command again"
14277 .into(),
14278 })?
14279 .insert(id.clone(), item);
14280 let Some(status) = self.set_status(id, ItemKind::Task, category).await? else {
14281 return Ok(None);
14282 };
14283 task.status = status;
14284 Ok(Some(task))
14285 }
14286
14287 /// Set one project's status alone, by exactly the paired write a task's status takes, for
14288 /// the project kind: a terminal category selects the project kind's mapped option and then
14289 /// closes the issue with the category's fixed reason — an issue already closed with a
14290 /// reason that reads that category is left as it is — and an open category reopens a closed
14291 /// issue and then selects its option. A board draft is refused a terminal category as a
14292 /// task's is.
14293 async fn set_project_status(
14294 &self,
14295 id: &NativeId,
14296 category: StatusCategory,
14297 ) -> Result<Option<Status>, SourceError> {
14298 self.set_status(id, ItemKind::Project, category).await
14299 }
14300
14301 /// Every task and project whose card contradicts its issue's closure, read through the
14302 /// board's own `is:closed` filter; see [`TaskSource::status_contradictions`].
14303 async fn status_contradictions(&self) -> Result<Vec<StatusContradiction>, SourceError> {
14304 self.contradictions().await
14305 }
14306
14307 /// What this board holds for each issue reference, one request per fifty of them; see
14308 /// [`TaskSource::issue_holdings`].
14309 async fn issue_holdings(&self, issues: &[IssueRef]) -> Result<Vec<IssueHolding>, SourceError> {
14310 self.holdings(issues).await
14311 }
14312
14313 /// Set one task's priority alone: one `updateProjectV2ItemFieldValue` selecting the
14314 /// mapped option of the board's `Priority` field, or one `clearProjectV2ItemFieldValue`
14315 /// for `none`. Refused by an instance with no `priority_mapping`.
14316 async fn set_task_priority(
14317 &self,
14318 id: &NativeId,
14319 priority: Priority,
14320 ) -> Result<Option<Priority>, SourceError> {
14321 self.set_priority(id, priority).await
14322 }
14323
14324 /// Replace one task's content with a single body update that keeps the metadata slot
14325 /// byte for byte.
14326 async fn set_task_content(
14327 &self,
14328 id: &NativeId,
14329 content: &str,
14330 ) -> Result<Option<()>, SourceError> {
14331 self.replace_content(id, content).await
14332 }
14333
14334 /// Replace one task issue's content and its provenance slot entry with a single body
14335 /// update. The answers are not kept: see `replace_rendering`.
14336 async fn set_task_rendering(
14337 &self,
14338 id: &NativeId,
14339 content: &str,
14340 provenance: &Value,
14341 _answers: &BTreeMap<String, Value>,
14342 ) -> Result<Option<()>, SourceError> {
14343 self.replace_rendering(
14344 id,
14345 BoardKind::Work(ItemKind::Task),
14346 content,
14347 provenance,
14348 None,
14349 )
14350 .await
14351 .map(|written| written.map(|_| ()))
14352 }
14353
14354 /// Replace one design-document issue's content and its provenance slot entry, on exactly
14355 /// the terms of [`set_task_rendering`](TaskSource::set_task_rendering).
14356 async fn set_document_rendering(
14357 &self,
14358 id: &NativeId,
14359 content: &str,
14360 provenance: &Value,
14361 _answers: &BTreeMap<String, Value>,
14362 ) -> Result<Option<()>, SourceError> {
14363 self.replace_rendering(id, BoardKind::Document, content, provenance, None)
14364 .await
14365 .map(|written| written.map(|_| ()))
14366 }
14367
14368 /// Replace one project issue's content and its provenance slot entry, on exactly the
14369 /// terms of [`set_task_rendering`](TaskSource::set_task_rendering).
14370 async fn set_project_rendering(
14371 &self,
14372 id: &NativeId,
14373 content: &str,
14374 provenance: &Value,
14375 _answers: &BTreeMap<String, Value>,
14376 ) -> Result<Option<()>, SourceError> {
14377 self.replace_rendering(
14378 id,
14379 BoardKind::Work(ItemKind::Project),
14380 content,
14381 provenance,
14382 None,
14383 )
14384 .await
14385 .map(|written| written.map(|_| ()))
14386 }
14387
14388 /// Apply a targeted update with one read of the item and a write only for what differs:
14389 /// the `Status` and `Priority` field writes in one request, the `blockedBy` difference,
14390 /// and last one `updateIssue` for title, body and state. See `targeted_update`.
14391 async fn update_task(
14392 &self,
14393 id: &NativeId,
14394 update: &TaskUpdate,
14395 ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
14396 self.targeted_update(id, update).await
14397 }
14398
14399 /// Replace one task's `delivered_by` with a single body update that changes the
14400 /// metadata slot and nothing outside it.
14401 async fn set_delivered_by(
14402 &self,
14403 id: &NativeId,
14404 delivered_by: &[TaskRef],
14405 ) -> Result<Option<()>, SourceError> {
14406 self.replace_delivered_by(id, delivered_by).await
14407 }
14408
14409 /// Set one key of one task issue's metadata with a single body update that changes the
14410 /// metadata slot and nothing outside it — no title, label, state or board field request —
14411 /// and sends nothing when the task already holds that value under the key.
14412 async fn set_task_metadata(
14413 &self,
14414 id: &NativeId,
14415 key: &MetadataKey,
14416 value: &Value,
14417 ) -> Result<Option<Task>, SourceError> {
14418 Ok(self
14419 .set_slot_key(id, BoardKind::Work(ItemKind::Task), key, value)
14420 .await?
14421 .map(|item| item.task())
14422 .transpose()?)
14423 }
14424
14425 /// Set one key of one project issue's metadata, on exactly the terms of
14426 /// [`set_task_metadata`](TaskSource::set_task_metadata).
14427 async fn set_project_metadata(
14428 &self,
14429 id: &NativeId,
14430 key: &MetadataKey,
14431 value: &Value,
14432 ) -> Result<Option<Project>, SourceError> {
14433 Ok(self
14434 .set_slot_key(id, BoardKind::Work(ItemKind::Project), key, value)
14435 .await?
14436 .map(|item| item.project()))
14437 }
14438
14439 /// Set one key of one design-document issue's metadata, on exactly the terms of
14440 /// [`set_task_metadata`](TaskSource::set_task_metadata).
14441 async fn set_document_metadata(
14442 &self,
14443 id: &NativeId,
14444 key: &MetadataKey,
14445 value: &Value,
14446 ) -> Result<Option<Document>, SourceError> {
14447 Ok(self
14448 .set_slot_key(id, BoardKind::Document, key, value)
14449 .await?
14450 .map(|item| item.document()))
14451 }
14452
14453 /// Take one key out of one issue's metadata slot with a single body update that changes the
14454 /// slot and nothing outside it, sending nothing when the slot does not hold the key.
14455 async fn remove_record_metadata(
14456 &self,
14457 record: onetaskgraph_plugin_api::MetadataRecord,
14458 id: &NativeId,
14459 key: &MetadataKey,
14460 ) -> Result<Option<Option<String>>, SourceError> {
14461 let kind = match record {
14462 onetaskgraph_plugin_api::MetadataRecord::Task => BoardKind::Work(ItemKind::Task),
14463 onetaskgraph_plugin_api::MetadataRecord::Project => BoardKind::Work(ItemKind::Project),
14464 onetaskgraph_plugin_api::MetadataRecord::Document => BoardKind::Document,
14465 };
14466 let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
14467 return Ok(None);
14468 };
14469 if item.slot.contains_key(key.as_str()) {
14470 let mut slot = item.slot.clone();
14471 slot.remove(key.as_str());
14472 self.write_slot(&mut item, &slot).await?;
14473 self.remember_written(item.clone(), false)?;
14474 }
14475 Ok(Some(match record {
14476 onetaskgraph_plugin_api::MetadataRecord::Task => item.task()?.version,
14477 onetaskgraph_plugin_api::MetadataRecord::Project => item.project().version,
14478 onetaskgraph_plugin_api::MetadataRecord::Document => item.document().version,
14479 }))
14480 }
14481
14482 async fn delete_task(&self, id: &NativeId) -> Result<(), SourceError> {
14483 self.delete_item(id).await
14484 }
14485
14486 async fn delete_project(&self, id: &NativeId) -> Result<(), SourceError> {
14487 self.delete_item(id).await
14488 }
14489
14490 async fn delete_document(&self, id: &NativeId) -> Result<(), SourceError> {
14491 self.delete_item(id).await
14492 }
14493
14494 /// One page of the task issue's own comments, walked by GitHub's own cursor.
14495 ///
14496 /// Nothing here filters, so nothing has to be read ahead of the page: the caller's limit is
14497 /// the page GitHub is asked for and GitHub's `endCursor` is the cursor handed back.
14498 ///
14499 /// One request, [`graphql::ISSUE_DETAIL`]: the read that says the id names a task of this
14500 /// board is the read of its comments. A draft this process already resolved is refused
14501 /// without one.
14502 async fn task_comments(
14503 &self,
14504 task: &NativeId,
14505 page: &PageRequest,
14506 ) -> Result<Option<Page<Comment>>, SourceError> {
14507 validate_page(page)?;
14508 let cached = self.resolved_cache()?.get(task).cloned();
14509 if let Some(item) = cached {
14510 if item.kind != BoardKind::Work(ItemKind::Task) {
14511 return Ok(None);
14512 }
14513 if item.content_kind == ContentKind::DraftIssue {
14514 return Err(self.draft_has_no_comments(task));
14515 }
14516 }
14517 match self.issue_detail(task, page).await? {
14518 Some(TaskDetailRead {
14519 comments: Some(comments),
14520 ..
14521 }) => comments,
14522 _ => Ok(None),
14523 }
14524 }
14525
14526 /// Every id's task, with the first page of its comments when `comments` names it:
14527 /// [`DETAIL_BATCH`] items per [`graphql::ISSUE_DETAILS`] request, and one item with its
14528 /// comments in one [`graphql::ISSUE_DETAIL`] request.
14529 async fn get_task_details(
14530 &self,
14531 ids: &[NativeId],
14532 comments: Option<&PageRequest>,
14533 ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
14534 if let Some(page) = comments
14535 && let Err(error) = validate_page(page)
14536 {
14537 return ids.iter().map(|_| Err(error.clone())).collect();
14538 }
14539 match (ids, comments) {
14540 ([id], Some(page)) => vec![self.issue_detail(id, page).await],
14541 ([id], None) => vec![self.task_read(id).await],
14542 _ => self.issue_details(ids, comments).await,
14543 }
14544 }
14545
14546 /// Every id's task with the first page of its comments and its `blockedBy`, as a copy reads
14547 /// the tasks it carries: [`COPY_DETAIL_BATCH`] items per [`graphql::ISSUE_COPY_DETAILS`]
14548 /// request, so the dependency read a copy makes of each next is answered from this one.
14549 async fn get_task_details_for_copy(
14550 &self,
14551 ids: &[NativeId],
14552 comments: &PageRequest,
14553 ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
14554 // A page past the first is a page of one task's comments, which the detail read of it
14555 // alone answers.
14556 if comments.cursor.is_some() {
14557 return self.get_task_details(ids, Some(comments)).await;
14558 }
14559 if let Err(error) = validate_page(comments) {
14560 return ids.iter().map(|_| Err(error.clone())).collect();
14561 }
14562 let mut read = Vec::with_capacity(ids.len());
14563 for batch in ids.chunks(COPY_DETAIL_BATCH) {
14564 match self
14565 .graphql(
14566 graphql::ISSUE_COPY_DETAILS,
14567 copy_detail_batch(batch, comments, false),
14568 )
14569 .await
14570 {
14571 Ok(data) => {
14572 for (slot, id) in batch.iter().enumerate() {
14573 let read_one = match data.get(format!("i{slot}")) {
14574 Some(node) => self.detail_of(id, node, true, None).await,
14575 None => Err(SourceError::Malformed {
14576 message: format!(
14577 "GitHub answered a batch read with no item for {}",
14578 id.0
14579 ),
14580 }),
14581 };
14582 read.push(read_one);
14583 }
14584 }
14585 Err(error) if unresolvable_node(&error) => {
14586 for id in batch {
14587 read.push(self.issue_detail(id, comments).await);
14588 }
14589 }
14590 Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
14591 }
14592 }
14593 read
14594 }
14595
14596 /// Every id's item, whatever kind it is, read [`COPY_DETAIL_BATCH`] at a time with
14597 /// [`graphql::ISSUE_COPY_DETAILS`] — the document a copy reads the tasks it carries with —
14598 /// and held for the next read of each by id; see `read_ahead`.
14599 ///
14600 /// What this run wrote is never read: its own record answers, as it does for a read by id.
14601 /// Nor is an item a listing of its project carried whole earlier in this command — see
14602 /// `carried` — which is held in place of reading it.
14603 /// A batch GitHub refuses because one of its ids resolves to no node at all holds nothing,
14604 /// so each of its ids is read on its own and answered as that read answers; a draft, and an
14605 /// answer naming another issue than the one asked for, are left to their own reads too.
14606 async fn read_ahead_for_copy(&self, ids: &[NativeId]) -> Result<(), SourceError> {
14607 // The origins asked just before go out now, beside the members of the one project this
14608 // names when it names one; a project those origins answered whole is held for its next
14609 // read by id rather than read again here.
14610 let hinted = match ids {
14611 [one] => Some(one),
14612 _ => None,
14613 };
14614 self.send_pending_origins(hinted).await?;
14615 if let Some(project) = hinted {
14616 let answered = self.resolved_cache()?.get(project).cloned();
14617 if let Some(answered) = answered
14618 && answered.kind == BoardKind::Work(ItemKind::Project)
14619 {
14620 self.read_ahead()?.insert(project.clone(), Some(answered));
14621 return Ok(());
14622 }
14623 }
14624 let mut unread: Vec<NativeId> = Vec::new();
14625 // Read whole by a listing of its project in this command, which a mutation naming it
14626 // would have dropped: held as a batch would have held it, once every batch is answered.
14627 let mut listed: Vec<NativeId> = Vec::new();
14628 {
14629 let created = self.created()?;
14630 let held = self.read_ahead()?;
14631 let carried = self.carried()?;
14632 for id in ids {
14633 if unread.contains(id)
14634 || listed.contains(id)
14635 || held.contains_key(id)
14636 || created.iter().any(|own| own.id == *id)
14637 {
14638 continue;
14639 }
14640 // An issue a listing carried without the whole of what blocks it is read in a
14641 // batch too, which carries that, rather than having its dependency read sent
14642 // on its own once the copy reconciles its edges.
14643 let whole = carried.get(id).is_some_and(|item| {
14644 item.content_kind != ContentKind::Issue || item.blocked_by.is_some()
14645 });
14646 if whole {
14647 listed.push(id.clone());
14648 } else {
14649 unread.push(id.clone());
14650 }
14651 }
14652 }
14653 // Nothing of an item's comments is wanted here: the smallest page keeps the read to
14654 // what the item itself costs.
14655 let comments = PageRequest {
14656 cursor: None,
14657 limit: 1,
14658 };
14659 // Held only once every batch has been answered, so a read-ahead that fails part way
14660 // holds nothing — what a listing carried included, which stays carried — and every
14661 // item is read on its own, as the method promises.
14662 let mut read: Vec<(NativeId, Option<Resolved>)> = Vec::new();
14663 // The issues the first id names as sub-issues, as `ISSUE_COPY_DETAILS`'s `children`
14664 // answered them: what a project copy's orphan check asks, when that first id is its
14665 // project.
14666 let mut children: Option<(NativeId, Vec<(String, String)>)> = None;
14667 for (index, batch) in unread.chunks(COPY_DETAIL_BATCH).enumerate() {
14668 let data = match self
14669 .graphql(
14670 graphql::ISSUE_COPY_DETAILS,
14671 copy_detail_batch(batch, &comments, index == 0),
14672 )
14673 .await
14674 {
14675 Ok(data) => data,
14676 Err(error) if unresolvable_node(&error) => continue,
14677 Err(error) => return Err(error),
14678 };
14679 for (slot, id) in batch.iter().enumerate() {
14680 let Some(node) = data.get(format!("i{slot}")) else {
14681 continue;
14682 };
14683 let held = if node.is_null() {
14684 None
14685 } else {
14686 match optional_str(node, "__typename")? {
14687 Some("DraftIssue") => continue,
14688 Some("Issue") if required_str(node, "id")? != id.0 => continue,
14689 _ => {}
14690 }
14691 // Remembered below with the rest, so a later batch refused leaves no
14692 // dependency read answered from this one either.
14693 self.resolve_issue_as(node, false).await?
14694 };
14695 read.push((id.clone(), held));
14696 }
14697 if index == 0
14698 && let Some(node) = data.get("children").filter(|node| !node.is_null())
14699 && node.get("id").and_then(Value::as_str) == batch.first().map(|id| id.0.as_str())
14700 && node.pointer("/subIssues/pageInfo/hasNextPage") == Some(&Value::Bool(false))
14701 {
14702 // A list GitHub did not answer is not an empty one: it would answer the project's
14703 // task listing with nothing, and the orphan check with no orphans.
14704 let listed = node
14705 .pointer("/subIssues/nodes")
14706 .and_then(Value::as_array)
14707 .ok_or_else(|| SourceError::Malformed {
14708 message: format!(
14709 "GitHub answered the sub-issues of project {} with no list of them",
14710 batch[0].0
14711 ),
14712 })?
14713 .iter()
14714 .map(|child| {
14715 Ok((
14716 required_str(child, "id")?.to_owned(),
14717 required_str(child, "title")?.to_owned(),
14718 ))
14719 })
14720 .collect::<Result<Vec<_>, SourceError>>()?;
14721 children = Some((batch[0].clone(), listed));
14722 }
14723 }
14724 // The project's tasks are known from this read alone when every child it named is held
14725 // from it, but for a document, which its title says is one and which a task listing
14726 // never answers: then a listing of the project's tasks is answered from them.
14727 if let Some((project, listed)) = children
14728 && read.iter().any(|(id, held)| {
14729 *id == project
14730 && held
14731 .as_ref()
14732 .is_some_and(|held| held.kind == BoardKind::Work(ItemKind::Project))
14733 })
14734 {
14735 let mut tasks = Vec::new();
14736 let mut whole = true;
14737 for (child, title) in &listed {
14738 match read.iter().find(|(id, _)| id.0 == *child) {
14739 Some((_, Some(held))) if held.parent.as_ref() == Some(&project) => {
14740 if held.kind == BoardKind::Work(ItemKind::Task) {
14741 tasks.push(held.clone());
14742 }
14743 }
14744 _ if title.starts_with(DESIGN_TITLE_PREFIX) => {}
14745 _ => whole = false,
14746 }
14747 }
14748 if whole {
14749 self.task_children()?.insert(project, tasks);
14750 }
14751 }
14752 let mut resolved = self.resolved_cache()?;
14753 let mut ahead = self.read_ahead()?;
14754 let mut carried = self.carried()?;
14755 for item in read.iter().filter_map(|(_, held)| held.as_ref()) {
14756 resolved.insert(item.id.clone(), item.clone());
14757 }
14758 // Taken only if still carried: a mutation naming it while the batches ran dropped it,
14759 // and its next read is then its own.
14760 for id in &listed {
14761 let Some(item) = carried.remove(id) else {
14762 continue;
14763 };
14764 // The whole of what it is blocked by came with it, so the dependency read a copy
14765 // makes next is answered from this one, as a copy's detail read is.
14766 if item.blocked_by.is_some() {
14767 resolved.insert(item.id.clone(), item.clone());
14768 }
14769 read.push((id.clone(), Some(item)));
14770 }
14771 for (id, held) in read {
14772 ahead.insert(id, held);
14773 }
14774 Ok(())
14775 }
14776
14777 /// Add one comment to the task's issue, as the account the token belongs to.
14778 ///
14779 /// The author is refused before anything is sent — not even the task is read — because
14780 /// no answer GitHub could give would make posting under another name than the one asked
14781 /// for the right outcome.
14782 async fn add_comment(
14783 &self,
14784 task: &NativeId,
14785 comment: &NewComment,
14786 ) -> Result<Option<Comment>, SourceError> {
14787 if let Some(author) = &comment.author {
14788 return Err(SourceError::Refused {
14789 message: format!(
14790 "source {} cannot post a comment as {author:?}: GitHub records the account \
14791 the token signs in as the author of every comment; next: leave --author \
14792 out, and the comment is posted as that account",
14793 self.name
14794 ),
14795 });
14796 }
14797 let Some(issue) = self.commented_issue(task).await? else {
14798 return Ok(None);
14799 };
14800 let data = self
14801 .graphql(
14802 graphql::ADD_COMMENT,
14803 json!({"input":{"subjectId":issue.0,"body":comment.body.as_str()}}),
14804 )
14805 .await?;
14806 let subject = data
14807 .pointer("/addComment/subject")
14808 .filter(|value| !value.is_null())
14809 .ok_or_else(|| SourceError::Malformed {
14810 message: "GitHub comment addition returned no subject".into(),
14811 })?;
14812 if required_str(subject, "id")? != issue.0 {
14813 return Err(SourceError::Malformed {
14814 message: "GitHub comment addition answered about another issue".into(),
14815 });
14816 }
14817 let added = data
14818 .pointer("/addComment/commentEdge/node")
14819 .filter(|value| !value.is_null())
14820 .ok_or_else(|| SourceError::Malformed {
14821 message: "GitHub comment addition returned no comment".into(),
14822 })?;
14823 let added = comment_from(added)?;
14824 self.remember_commented(&issue)?;
14825 Ok(Some(added))
14826 }
14827
14828 async fn edit_comment(
14829 &self,
14830 task: &NativeId,
14831 comment: &NativeId,
14832 body: &CommentBody,
14833 ) -> Result<Option<Comment>, SourceError> {
14834 let Some(issue) = self.commented_issue(task).await? else {
14835 return Ok(None);
14836 };
14837 let Some(held) = self.comment_on(&issue, comment).await? else {
14838 return Ok(None);
14839 };
14840 // A comment a copy wrote keeps its origin through an edit: its marker is carried over.
14841 let body = CommentOrigin::kept(&held, body)?;
14842 let data = self
14843 .graphql(
14844 graphql::UPDATE_COMMENT,
14845 json!({"input":{"id":comment.0,"body":body.as_str()}}),
14846 )
14847 .await?;
14848 let edited = data
14849 .pointer("/updateIssueComment/issueComment")
14850 .filter(|value| !value.is_null())
14851 .ok_or_else(|| SourceError::Malformed {
14852 message: "GitHub comment update returned no comment".into(),
14853 })?;
14854 let edited = comment_from(edited)?;
14855 if edited.id != *comment {
14856 return Err(SourceError::Malformed {
14857 message: "GitHub comment update returned the wrong comment".into(),
14858 });
14859 }
14860 self.remember_commented(&issue)?;
14861 Ok(Some(edited))
14862 }
14863
14864 async fn delete_comment(
14865 &self,
14866 task: &NativeId,
14867 comment: &NativeId,
14868 ) -> Result<Option<NativeId>, SourceError> {
14869 let Some(issue) = self.commented_issue(task).await? else {
14870 return Ok(None);
14871 };
14872 if !self.comment_is_on(&issue, comment).await? {
14873 return Ok(None);
14874 }
14875 let data = self
14876 .graphql(graphql::DELETE_COMMENT, json!({"input":{"id":comment.0}}))
14877 .await?;
14878 // The payload says nothing about the comment it removed, so what is checked is that
14879 // GitHub answered the mutation at all rather than leaving it unanswered.
14880 data.get("deleteIssueComment")
14881 .filter(|value| !value.is_null())
14882 .ok_or_else(|| SourceError::Malformed {
14883 message: "GitHub comment deletion returned no payload".into(),
14884 })?;
14885 Ok(Some(comment.clone()))
14886 }
14887
14888 /// Every request this source has recorded, and what each of GitHub's two budgets was
14889 /// attributed — read off the same accounting the session report is rendered from, so
14890 /// the two cannot count one request two ways.
14891 async fn metering(&self) -> Result<Option<Metering>, SourceError> {
14892 Ok(Some(self.ledger.snapshot().metering()))
14893 }
14894
14895 /// Drop every item, search answer and board read this source holds, so the next command
14896 /// reads the board as a person has since left it.
14897 ///
14898 /// Every one of those is held on the assumption that nothing but this source writes the
14899 /// board while a command runs, which stops being true the moment the command is over: a
14900 /// body a person edited would be overwritten from the record held here, and a card they
14901 /// moved would be read as still where this source left it. The board's own field
14902 /// definitions go too, because a person can add or delete a `Status` option and a write
14903 /// resolved against the held list would not re-read on a miss. What stays is what stays
14904 /// valid in normal use: each repository's node id, which a miss re-reads, the pacing of
14905 /// mutations, which is about GitHub's limiter rather than anybody's work, and the running
14906 /// accounting [`metering`](TaskSource::metering) answers from.
14907 ///
14908 /// Infallible in practice: a lock an earlier failure poisoned is cleared rather than
14909 /// refused, because clearing it is what puts it right.
14910 async fn end_command(&self) -> Result<(), SourceError> {
14911 fn clear<T: Default>(held: &Mutex<T>) {
14912 *held
14913 .lock()
14914 .unwrap_or_else(std::sync::PoisonError::into_inner) = T::default();
14915 held.clear_poison();
14916 }
14917 clear(&self.created);
14918 clear(&self.updated);
14919 clear(&self.commented);
14920 clear(&self.board_cache);
14921 clear(&self.search_cache);
14922 clear(&self.narrowed_cache);
14923 clear(&self.search_next);
14924 clear(&self.resolved_cache);
14925 clear(&self.read_ahead);
14926 clear(&self.pending_origins);
14927 clear(&self.member_classes);
14928 clear(&self.carried);
14929 clear(&self.held_cache);
14930 clear(&self.children_cache);
14931 clear(&self.task_children);
14932 clear(&self.written);
14933 clear(&self.fields_cache);
14934 self.expected.end_command();
14935 Ok(())
14936 }
14937}
14938
14939/// One member of a project as a classification-only read answers it: its classification and
14940/// the repositories its work changes.
14941type MemberClass = (Classification, Vec<Repository>);
14942
14943/// Each project's sub-issues as one command read them, keyed by the selector they were asked
14944/// for under, beside the project that selector named; see `children_cache`.
14945type ProjectChildren = BTreeMap<NativeId, (NativeId, Vec<Resolved>)>;
14946
14947/// One issue comment as the contract carries it.
14948///
14949/// `author` is absent both when GitHub answers `null` for an account that no longer exists
14950/// and when it answers an actor with no login, because either way the source did not say who
14951/// wrote it — which is what an absent author means, rather than an author called nothing.
14952///
14953/// A comment a copy wrote here carries its origin as the hidden marker at the end of its body
14954/// — GitHub sets every comment's author and times itself, so there is nowhere else to keep it —
14955/// and is read back with the marker taken out and reported as its origin.
14956fn comment_from(value: &Value) -> Result<Comment, SourceError> {
14957 Ok(Comment {
14958 origin: None,
14959 id: NativeId(required_str(value, "id")?.to_owned()),
14960 author: optional_str(value.get("author").unwrap_or(&Value::Null), "login")?
14961 .map(str::to_owned),
14962 created_at: optional_time(value, "createdAt")?,
14963 updated_at: optional_time(value, "updatedAt")?,
14964 body: required_str(value, "body")?.to_owned(),
14965 url: optional_str(value, "url")?.map(str::to_owned),
14966 }
14967 .unmarked())
14968}
14969
14970/// The page of comments one issue node carries, resumed from `after`.
14971fn comment_page(
14972 node: &Value,
14973 issue: &str,
14974 after: Option<&str>,
14975) -> Result<Page<Comment>, SourceError> {
14976 let connection = node
14977 .get("comments")
14978 .filter(|value| !value.is_null())
14979 .ok_or_else(|| SourceError::Malformed {
14980 message: format!("GitHub issue {issue} answered with no comments connection"),
14981 })?;
14982 let items = optional_nodes(Some(connection), "issue comments")?
14983 .into_iter()
14984 .flatten()
14985 .map(comment_from)
14986 .collect::<Result<Vec<_>, _>>()?;
14987 let next = next_cursor(connection)?;
14988 if let Some(next) = &next {
14989 validate_cursor_progress(after, &next.0)?;
14990 }
14991 Ok(Page { items, next })
14992}
14993
14994/// The far ends of an issue's whole `blockedBy` connection, when the read carried it to its
14995/// end — `None` when it carried none, or a page with more past it.
14996fn carried_blocked_by(content: &Value) -> Result<Option<Vec<Value>>, SourceError> {
14997 let Some(connection) = content.get("blockedBy").filter(|value| !value.is_null()) else {
14998 return Ok(None);
14999 };
15000 if next_cursor(connection)?.is_some() {
15001 return Ok(None);
15002 }
15003 Ok(Some(
15004 optional_nodes(Some(connection), "blocked-by issues")?
15005 .into_iter()
15006 .flatten()
15007 .cloned()
15008 .collect(),
15009 ))
15010}
15011
15012/// Where the recorded tail of a dependency walk resumes; see
15013/// [`GitHubProjectsSource::recorded_edges`].
15014const RECORDED_CURSOR: &str = "onetaskgraph.depends_on:";
15015
15016/// The board text field this source keeps a copy's origin in.
15017///
15018/// Named after the key it holds, and held to that name by the guard below rather than by
15019/// a reader noticing.
15020const ORIGIN_FIELD: &str = "onetaskgraph.origin";
15021
15022/// The metadata key that field holds.
15023///
15024/// The engine owns this key and spells it once as `GlobalId::ORIGIN_KEY`; a plugin never
15025/// constructs or interprets the qualified id it carries. This source names it only to
15026/// route it — a short, typed value belongs in a typed field rather than in the body slot
15027/// a caller's own prose shares.
15028///
15029/// Restated rather than imported, because no plugin crate may depend on the engine. What
15030/// keeps the two spellings one contract is `scripts/check-origin-key-spelling.sh`, a
15031/// target in `check`: it reads the engine's own literal and fails naming the file and the
15032/// line when a plugin's parts from it either way. Drift here has one symptom — a copy
15033/// that creates a second item every run instead of finding the one it wrote — and that is
15034/// too late to learn it.
15035const ORIGIN_KEY: &str = "onetaskgraph.origin";
15036
15037/// Where a recorded tail resumes, refusing a cursor no walk in `direction` reported.
15038///
15039/// The reserved key holds forward edges and nothing else — the reverse of a recorded edge
15040/// is derived from the far end, never written down on the near item — so only a forward
15041/// walk ever reports one of these cursors. A reverse read carrying one is resuming a walk
15042/// it did not come from, and it is told so rather than answered with an empty page that
15043/// reads as a walk which ended.
15044fn recorded_offset(
15045 cursor: Option<&str>,
15046 direction: Direction,
15047) -> Result<Option<usize>, SourceError> {
15048 cursor
15049 .and_then(|cursor| cursor.strip_prefix(RECORDED_CURSOR))
15050 .map(|offset| {
15051 if direction != Direction::DependsOn {
15052 return Err(SourceError::Config {
15053 message: format!(
15054 "{RECORDED_CURSOR}{offset} resumes recorded forward edges, which a \
15055 reverse dependency read never issues; resume it in the direction \
15056 that reported it"
15057 ),
15058 });
15059 }
15060 offset.parse().map_err(|_| SourceError::Config {
15061 message: format!("{RECORDED_CURSOR}{offset} is not a recorded-edge cursor"),
15062 })
15063 })
15064 .transpose()
15065}
15066
15067/// What a dependency read's own answer says of a board draft: `None` when the item is not a
15068/// draft, or the answer did not carry its board item; `Some(None)` for a draft whose one board
15069/// item is not on this board, which this source does not hold; and `Some(Some(slot))`, the
15070/// draft's metadata, for one it does — its board item naming this board by its number and by
15071/// the id the same answer read of the board, as [`GitHubProjectsSource::draft_by_id`] decides.
15072fn draft_slot(
15073 data: &Value,
15074 id: &NativeId,
15075 project_number: u32,
15076) -> Result<Option<Option<BTreeMap<String, Value>>>, SourceError> {
15077 let Some(draft) = data
15078 .get("node")
15079 .filter(|node| node.get("__typename").and_then(Value::as_str) == Some("DraftIssue"))
15080 else {
15081 return Ok(None);
15082 };
15083 let Some(items) = data.pointer("/draft/projectV2Items") else {
15084 return Ok(None);
15085 };
15086 if required_str(draft, "id")? != id.0 {
15087 return Err(SourceError::Malformed {
15088 message: format!("GitHub answered a different draft for {}", id.0),
15089 });
15090 }
15091 let nodes = items
15092 .get("nodes")
15093 .and_then(Value::as_array)
15094 .ok_or_else(|| SourceError::Malformed {
15095 message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
15096 })?;
15097 let info = items
15098 .get("pageInfo")
15099 .ok_or_else(|| SourceError::Malformed {
15100 message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
15101 })?;
15102 if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
15103 return Err(SourceError::Malformed {
15104 message: format!(
15105 "GitHub draft {} reports more board items than the one GitHub links a draft to",
15106 id.0
15107 ),
15108 });
15109 }
15110 let board = data
15111 .pointer("/board/projectV2/id")
15112 .and_then(Value::as_str)
15113 .ok_or_else(|| SourceError::Malformed {
15114 message: format!(
15115 "GitHub answered draft {} without this board's own id beside it",
15116 id.0
15117 ),
15118 })?;
15119 let mut held = false;
15120 for node in nodes {
15121 let project = node.get("project").ok_or_else(|| SourceError::Malformed {
15122 message: format!("GitHub draft {} board item has no project", id.0),
15123 })?;
15124 let number = project
15125 .get("number")
15126 .and_then(Value::as_u64)
15127 .ok_or_else(|| SourceError::Malformed {
15128 message: format!(
15129 "GitHub draft {} board item has no numeric project number",
15130 id.0
15131 ),
15132 })?;
15133 let project_id = required_str(project, "id")?;
15134 held |= number == u64::from(project_number) && project_id == board;
15135 }
15136 if !held {
15137 return Ok(Some(None));
15138 }
15139 Ok(Some(Some(
15140 metadata_body(optional_str(draft, "body")?.map(str::to_owned))?.1,
15141 )))
15142}
15143
15144fn recorded_page(edges: Vec<DependencyEdge>, offset: usize, limit: usize) -> Page<DependencyEdge> {
15145 let mut page = offset_page(edges, offset, limit.max(1));
15146 page.next = page
15147 .next
15148 .map(|cursor| Cursor(format!("{RECORDED_CURSOR}{}", cursor.0)));
15149 page
15150}
15151
15152/// The kind of one issue reached through a dependency connection.
15153///
15154/// The same questions the board scan asks, over the fields the dependency document
15155/// selects, and in the same order: the design prefix first, then a sub-issue is a task,
15156/// then anything with sub-issues or the marker is a project.
15157///
15158/// # Errors
15159///
15160/// A far end this board holds as a document is refused rather than reported. The two
15161/// answers that are not refusals would both be wrong: reporting it as a task names an id
15162/// no task read of this source can find, and reporting it as a project names one no
15163/// project read can. There is no third value to return — `ItemKind` has no document
15164/// variant, because nothing may point at a document — so the relationship itself is what
15165/// the person is told about.
15166fn related_kind(value: &Value) -> Result<ItemKind, SourceError> {
15167 let id = required_str(value, "id")?;
15168 if required_str(value, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
15169 return Err(SourceError::Refused {
15170 message: format!(
15171 "GitHub issue {id} is a document of this board — its title begins \
15172 {DESIGN_TITLE_PREFIX:?} — and nothing may depend on a document or be depended \
15173 on by one; next: remove that issue's blocking relationship on this board"
15174 ),
15175 });
15176 }
15177 let parent = optional_str(value.get("parent").unwrap_or(&Value::Null), "id")?;
15178 if parent.is_some() {
15179 return Ok(ItemKind::Task);
15180 }
15181 let (_, slot) = metadata_body(optional_str(value, "body")?.map(str::to_owned))?;
15182 let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
15183 message: format!("GitHub issue {id}: {message}"),
15184 })?;
15185 let sub_issues = sub_issue_total(value)?;
15186 Ok(if sub_issues > 0 || marked == Some(ItemKind::Project) {
15187 ItemKind::Project
15188 } else {
15189 ItemKind::Task
15190 })
15191}
15192
15193/// The `stateInput` that closes an issue for `reason`.
15194fn closed_input(reason: ClosedState) -> Value {
15195 json!({"value":"CLOSED","stateReason":reason.reason()})
15196}
15197
15198/// The `IssueStateUpdateInput` one status target asks for.
15199///
15200/// `stateInput` and `state` are mutually exclusive on `UpdateIssueInput`, and only this
15201/// one is ever sent. A non-terminal status always asks for `OPEN`, which is what reopens
15202/// a currently-closed issue: without that the item would read back `Unknown` and a copy
15203/// would report a change forever. A document has no status at all, and asks for neither.
15204fn state_input(target: Option<&StatusTarget>) -> Value {
15205 match target {
15206 Some(StatusTarget::Terminal(_, reason)) => closed_input(*reason),
15207 Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => json!({"value":"OPEN"}),
15208 // A document has no status, so a write of one says nothing about the issue's open
15209 // or closed state rather than forcing it open: `stateInput` is what carries that
15210 // instruction, and an explicit null asks for no change to it.
15211 None => Value::Null,
15212 }
15213}
15214
15215/// The metadata one write stores in the item's body slot.
15216///
15217/// The typed fields travel as themselves, so the three reserved keys are rebuilt here
15218/// rather than carried: the kind marker so an empty project stays readable, the
15219/// repository list only when it is not exactly the issue's own repository, and the far
15220/// ends no relationship here can name.
15221///
15222/// The copy origin is the one typed field that is also mirrored here, and only as a
15223/// mirror: it lands in the board's origin field as well, which stays the one every reader
15224/// takes it from, and it is here so that GitHub's issue search — which indexes this comment
15225/// and catches up with a write in seconds rather than minutes — can find the item by it.
15226/// A reader of the release before this one drops the slot's copy and reads the field, so an
15227/// item written here still reads with exactly one origin there.
15228fn slot_metadata(
15229 incoming: &Incoming<'_>,
15230 own_repository: Option<&Repository>,
15231 fallback: &[DependencyEdge],
15232) -> BTreeMap<String, Value> {
15233 let mut metadata = incoming.metadata.clone();
15234 match metadata.remove(ORIGIN_KEY) {
15235 Some(Value::String(origin)) if !origin.is_empty() => {
15236 metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin));
15237 }
15238 _ => {}
15239 }
15240 match incoming.written.kind() {
15241 BoardKind::Work(kind) => metadata.insert(
15242 ItemKind::METADATA_KEY.to_owned(),
15243 Value::String(kind.marker().to_owned()),
15244 ),
15245 // A document is told by its title, so it carries no kind marker: that key names
15246 // what a dependency endpoint points at, and nothing may point at a document.
15247 BoardKind::Document => metadata.remove(ItemKind::METADATA_KEY),
15248 };
15249 incoming.classification.record(&mut metadata);
15250 let derivable = own_repository
15251 .map(|own| incoming.repositories == [own.clone()])
15252 .unwrap_or(incoming.repositories.is_empty());
15253 if derivable {
15254 metadata.remove(Repository::METADATA_KEY);
15255 } else {
15256 metadata.insert(
15257 Repository::METADATA_KEY.to_owned(),
15258 Value::Array(
15259 incoming
15260 .repositories
15261 .iter()
15262 .map(|repository| Value::String(repository.as_str().to_owned()))
15263 .collect(),
15264 ),
15265 );
15266 }
15267 // The typed lists are what land, whatever the caller's own metadata held under their
15268 // keys: a key of either name travelling beside the field would otherwise be a second
15269 // answer to the same question, and the field is the one the contract names.
15270 for (key, entries) in [
15271 (TaskRef::DELIVERS_KEY, incoming.delivers),
15272 (TaskRef::DELIVERED_BY_KEY, incoming.delivered_by),
15273 ] {
15274 set_task_list(&mut metadata, key, entries);
15275 }
15276 record_edges(&mut metadata, fallback);
15277 metadata
15278}
15279
15280/// Hold the far ends no relationship here can name under [`DependencyEdge::RECORDED_KEY`] in
15281/// one slot's metadata, or no such key when there are none.
15282fn record_edges(metadata: &mut BTreeMap<String, Value>, fallback: &[DependencyEdge]) {
15283 if fallback.is_empty() {
15284 metadata.remove(DependencyEdge::RECORDED_KEY);
15285 } else {
15286 metadata.insert(
15287 DependencyEdge::RECORDED_KEY.to_owned(),
15288 Value::Array(
15289 fallback
15290 .iter()
15291 .map(|edge| {
15292 serde_json::to_value(&edge.to).expect("an endpoint renders as JSON")
15293 })
15294 .collect(),
15295 ),
15296 );
15297 }
15298}
15299
15300/// Every label one item carries, from its content's own connection and nowhere else.
15301///
15302/// There is no second place to read one from: no document this source sends selects the
15303/// board's built-in `Labels` field, because GitHub derives it from the content and a draft
15304/// cannot carry one at all. The module documentation records the three schema facts that
15305/// settle it.
15306fn labels(content: &Value) -> Result<Vec<Label>, SourceError> {
15307 optional_nodes(content.get("labels"), "content labels")?
15308 .into_iter()
15309 .flatten()
15310 .map(|v| {
15311 Ok(Label {
15312 id: NativeId(required_str(v, "id")?.to_owned()),
15313 name: required_str(v, "name")?.to_owned(),
15314 color: optional_str(v, "color")?.map(str::to_owned),
15315 })
15316 })
15317 .collect()
15318}
15319
15320/// Each board field value one item holds, as the change token's facets name it: the field's
15321/// name, with the single-select option's name or the text. A value of a type no read here
15322/// selects, and one naming no field, says nothing a person can rename and is left out.
15323fn facet_fields(field_values: &[Value]) -> Vec<(String, String)> {
15324 field_values
15325 .iter()
15326 .filter_map(|value| {
15327 let field = value.pointer("/field/name")?.as_str()?;
15328 let held = value.get("name").or_else(|| value.get("text"))?.as_str()?;
15329 Some((field.to_owned(), held.to_owned()))
15330 })
15331 .collect()
15332}
15333
15334/// The digest of the facets of the issue `content`, whose board item's values are
15335/// `field_values` — or `None` when the response did not carry, whole, every member they are read
15336/// from: the title, the parent, the sub-issue summary and the body its kind is read from, every
15337/// label, and every field value.
15338///
15339/// What a write's own response spells its token from, by exactly the rules a read takes them by.
15340fn facets_of(content: &Value, field_values: &Value) -> Option<change_token::FacetsDigest> {
15341 let ended = |connection: &Value| {
15342 connection.pointer("/pageInfo/hasNextPage") == Some(&Value::Bool(false))
15343 };
15344 let labels = content.get("labels").filter(|labels| ended(labels))?;
15345 if !ended(field_values) {
15346 return None;
15347 }
15348 let parent = content.get("parent")?;
15349 let (_, slot) = metadata_body(content.get("body")?.as_str().map(str::to_owned)).ok()?;
15350 let kind = BoardKind::of(
15351 content.get("title")?.as_str()?,
15352 !parent.is_null(),
15353 sub_issue_total(content).ok()?,
15354 ItemKind::from_metadata(&slot).ok()?,
15355 );
15356 let labels = self::labels(&json!({"labels": labels})).ok()?;
15357 let nodes = field_values.get("nodes")?.as_array()?;
15358 Some(change_token::Facets::new(kind.facet(), facet_fields(nodes), &labels).digest())
15359}
15360
15361/// The definition of each board field one item's values are values of, in the shape a read
15362/// of the board's own `fields` gives one.
15363///
15364/// A value names its field through a fragment on that field's own type, so the type is
15365/// known from which kind of value it is: a single-select value's field is a
15366/// `ProjectV2SingleSelectField`, options and all, and a text value's is a `ProjectV2Field`.
15367/// A value whose field carried no id, or an empty one, says nothing usable and is left out.
15368fn field_definitions(field_values: &[Value]) -> Vec<Value> {
15369 field_values
15370 .iter()
15371 .filter_map(|value| {
15372 let field = value.get("field")?.as_object()?;
15373 field.get("id")?.as_str().filter(|id| !id.is_empty())?;
15374 let typename = if value.get("text").is_some() {
15375 "ProjectV2Field"
15376 } else if value.get("name").is_some() {
15377 "ProjectV2SingleSelectField"
15378 } else {
15379 return None;
15380 };
15381 let mut defined = field.clone();
15382 defined.insert("__typename".to_owned(), json!(typename));
15383 // Only a text field holds a text value, so the value says the field's type too.
15384 if typename == "ProjectV2Field" {
15385 defined.insert("dataType".to_owned(), json!("TEXT"));
15386 }
15387 Some(Value::Object(defined))
15388 })
15389 .collect()
15390}
15391
15392fn text_field(field_values: &[Value], name: &str) -> Result<Option<String>, SourceError> {
15393 let Some(node) = field_values
15394 .iter()
15395 .find(|node| node.pointer("/field/name").and_then(Value::as_str) == Some(name))
15396 else {
15397 return Ok(None);
15398 };
15399 Ok(optional_str(node, "text")?.map(str::to_owned))
15400}
15401
15402fn valid_github_owner(owner: &str) -> bool {
15403 !owner.is_empty()
15404 && owner.len() <= 39
15405 && !owner.starts_with('-')
15406 && !owner.ends_with('-')
15407 && !owner.contains("--")
15408 && owner
15409 .bytes()
15410 .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
15411}
15412
15413/// GitHub's repository-name grammar: 1-100 ASCII letters, digits, `-`, `_` or `.`, and
15414/// neither of the two names a path segment already means.
15415fn valid_github_repository_name(name: &str) -> bool {
15416 !name.is_empty()
15417 && name.len() <= 100
15418 && name != "."
15419 && name != ".."
15420 && name
15421 .bytes()
15422 .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
15423}
15424
15425fn valid_environment_name(name: &str) -> bool {
15426 let mut bytes = name.bytes();
15427 bytes
15428 .next()
15429 .is_some_and(|byte| byte.is_ascii_alphabetic() || byte == b'_')
15430 && bytes.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
15431}
15432
15433/// How many sub-issues one issue has.
15434///
15435/// `Issue.subIssuesSummary` is `SubIssuesSummary!` and its `total` is `Int!`, so an
15436/// absent or non-integer one is a response this source cannot read — and reading it as
15437/// zero would classify a project as a task, which is exactly the mistake the marker
15438/// exists to keep from happening quietly.
15439fn sub_issue_total(issue: &Value) -> Result<u64, SourceError> {
15440 let summary = issue
15441 .get("subIssuesSummary")
15442 .ok_or_else(|| SourceError::Malformed {
15443 message: "GitHub issue is missing subIssuesSummary".into(),
15444 })?;
15445 summary
15446 .get("total")
15447 .and_then(Value::as_u64)
15448 .ok_or_else(|| SourceError::Malformed {
15449 message: "GitHub issue subIssuesSummary.total is not an unsigned integer".into(),
15450 })
15451}
15452
15453/// One issue's own `number`.
15454///
15455/// An issue always has one: GitHub declares `Issue.number` as `Int!` and every selection of
15456/// an issue in this module asks for it. So a read of one that comes back without it, or
15457/// with something that is not an unsigned integer, is a response this source cannot read —
15458/// absence here is **not** "this issue has no number". A draft is the content that has
15459/// none, and a draft never reaches this: the caller decides on `__typename` first, the way
15460/// it does for `subIssuesSummary`, which `DraftIssue` equally declares nothing for.
15461fn issue_number(issue: &Value) -> Result<u64, SourceError> {
15462 issue
15463 .get("number")
15464 .and_then(Value::as_u64)
15465 .ok_or_else(|| SourceError::Malformed {
15466 message: "GitHub issue number is missing or is not an unsigned integer".into(),
15467 })
15468}
15469
15470/// The `number` a creating mutation answered with, and `None` when it answered without one;
15471/// why a missing one is tolerated is at the call in `create_and_file_issue`.
15472fn created_issue_number(created: &Value) -> Result<Option<u64>, SourceError> {
15473 match created.get("number") {
15474 None | Some(Value::Null) => Ok(None),
15475 Some(value) => value
15476 .as_u64()
15477 .map(Some)
15478 .ok_or_else(|| SourceError::Malformed {
15479 message: "GitHub created issue number is not an unsigned integer".into(),
15480 }),
15481 }
15482}
15483
15484fn required_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
15485 value
15486 .get(field)
15487 .and_then(Value::as_str)
15488 .ok_or_else(|| SourceError::Malformed {
15489 message: format!("GitHub response is missing string field {field}"),
15490 })
15491}
15492
15493fn required_nonblank_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
15494 let found = required_str(value, field)?;
15495 if found.trim().is_empty() {
15496 return Err(SourceError::Malformed {
15497 message: format!("GitHub response has blank string field {field}"),
15498 });
15499 }
15500 Ok(found)
15501}
15502
15503/// The slot's delimiters, which `docs/metadata.md` settles once for every source that
15504/// needs one — Linear spells them too, in its own description field.
15505///
15506/// Restated rather than shared, because a plugin crate depends on the contract crate and
15507/// nothing else of this workspace. `scripts/check-metadata-slot-encoding.sh`, a target in
15508/// `check`, is what keeps the two one encoding: drift is otherwise quiet, since each
15509/// source round-trips its own writes perfectly well under its own spelling.
15510const METADATA_OPEN: &str = "<!-- onetaskgraph.metadata\n";
15511const METADATA_CLOSE: &str = "\n-->";
15512
15513/// What the composer puts between a non-empty visible body and the slot, and the one thing
15514/// the parser takes off the visible body when it takes the slot off — exactly once, so every
15515/// other trailing byte of the body comes back as it was written.
15516// 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.
15517const METADATA_SEPARATOR: &str = "\n\n";
15518
15519/// The visible body and the metadata slot at the end of it.
15520///
15521/// The encoding is the one `docs/metadata.md` settles for Linear, which is where its
15522/// reasons are. Only a comment at the very end is a slot; one in the middle is a person's
15523/// own content and is left alone. The visible body is everything before the slot less the
15524/// one [`METADATA_SEPARATOR`] the composer put there, byte for byte.
15525fn metadata_body(
15526 body: Option<String>,
15527) -> Result<(Option<String>, BTreeMap<String, Value>), SourceError> {
15528 let Some(body) = body else {
15529 return Ok((None, BTreeMap::new()));
15530 };
15531 let Some(slot) = slot_span(&body)? else {
15532 return Ok((Some(body), BTreeMap::new()));
15533 };
15534 let metadata =
15535 serde_json::from_str(&body[slot.encoded_start..slot.encoded_end]).map_err(|error| {
15536 SourceError::Malformed {
15537 message: format!(
15538 "invalid canonical JSON in GitHub issue onetaskgraph metadata slot: {error}"
15539 ),
15540 }
15541 })?;
15542 let before = &body[..slot.start];
15543 let visible = before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before);
15544 Ok(((!visible.is_empty()).then(|| visible.to_owned()), metadata))
15545}
15546
15547/// Where the metadata slot sits in one body, as byte offsets into it.
15548struct SlotSpan {
15549 /// Where [`METADATA_OPEN`] begins.
15550 start: usize,
15551 /// Where the encoded JSON begins, just past [`METADATA_OPEN`].
15552 encoded_start: usize,
15553 /// Where the encoded JSON ends, at the start of [`METADATA_CLOSE`].
15554 encoded_end: usize,
15555 /// Just past [`METADATA_CLOSE`].
15556 end: usize,
15557}
15558
15559/// The slot at the very end of `body`, or `None` when it has none.
15560///
15561/// The one reading of *where the slot is*, shared by [`metadata_body`], which reads it, and
15562/// [`with_slot`], which rewrites it — so the two cannot disagree about which comment is the
15563/// slot.
15564fn slot_span(body: &str) -> Result<Option<SlotSpan>, SourceError> {
15565 let Some(start) = body.rfind(METADATA_OPEN) else {
15566 return Ok(None);
15567 };
15568 let encoded_start = start + METADATA_OPEN.len();
15569 let Some(relative_end) = body[encoded_start..].find(METADATA_CLOSE) else {
15570 return Err(SourceError::Malformed {
15571 message: "unterminated onetaskgraph metadata slot in GitHub issue body".into(),
15572 });
15573 };
15574 let encoded_end = encoded_start + relative_end;
15575 let end = encoded_end + METADATA_CLOSE.len();
15576 if !body[end..].trim().is_empty() {
15577 return Ok(None);
15578 }
15579 Ok(Some(SlotSpan {
15580 start,
15581 encoded_start,
15582 encoded_end,
15583 end,
15584 }))
15585}
15586
15587/// `body` with its metadata slot holding exactly `metadata`, and every byte outside the
15588/// slot as it was.
15589///
15590/// A slot that is there has its JSON replaced in place; one that becomes empty is removed
15591/// together with the one [`METADATA_SEPARATOR`] separating it from the prose before it. A
15592/// body with no slot gains one the way [`compose_body`] writes it — after that separator,
15593/// or alone in an empty body — and a body with no slot that is given no metadata is
15594/// returned as it is.
15595fn with_slot(body: &str, metadata: &BTreeMap<String, Value>) -> Result<String, SourceError> {
15596 let encoded = if metadata.is_empty() {
15597 None
15598 } else {
15599 Some(
15600 serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
15601 message: error.to_string(),
15602 })?,
15603 )
15604 };
15605 Ok(match (slot_span(body)?, encoded) {
15606 (Some(slot), Some(encoded)) => format!(
15607 "{}{encoded}{}",
15608 &body[..slot.encoded_start],
15609 &body[slot.encoded_end..]
15610 ),
15611 (Some(slot), None) => {
15612 let before = &body[..slot.start];
15613 format!(
15614 "{}{}",
15615 before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before),
15616 &body[slot.end..]
15617 )
15618 }
15619 (None, None) => body.to_owned(),
15620 (None, Some(encoded)) if body.is_empty() => {
15621 format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15622 }
15623 (None, Some(encoded)) => {
15624 format!("{body}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15625 }
15626 })
15627}
15628
15629/// `body` with everything before its metadata slot replaced by `content`, and the slot
15630/// itself kept byte for byte.
15631///
15632/// The inverse of how [`metadata_body`] splits a body: the slot, when there is one, follows
15633/// `content` after the one [`METADATA_SEPARATOR`] the composer puts there — or alone, when
15634/// `content` is empty — so a read of the result reports `content` as the visible body and
15635/// the slot's metadata exactly as it was.
15636fn with_content(body: &str, content: &str) -> Result<String, SourceError> {
15637 let Some(slot) = slot_span(body)? else {
15638 return Ok(content.to_owned());
15639 };
15640 let kept = &body[slot.start..];
15641 Ok(if content.is_empty() {
15642 kept.to_owned()
15643 } else {
15644 format!("{content}{METADATA_SEPARATOR}{kept}")
15645 })
15646}
15647
15648/// Hold `entries` under `key` in one slot's metadata, or no such key when there are none.
15649fn set_task_list(metadata: &mut BTreeMap<String, Value>, key: &str, entries: &[TaskRef]) {
15650 if entries.is_empty() {
15651 metadata.remove(key);
15652 } else {
15653 metadata.insert(
15654 key.to_owned(),
15655 Value::Array(
15656 entries
15657 .iter()
15658 .map(|entry| Value::String(entry.as_str().to_owned()))
15659 .collect(),
15660 ),
15661 );
15662 }
15663}
15664
15665fn compose_body(
15666 content: Option<&str>,
15667 metadata: &BTreeMap<String, Value>,
15668) -> Result<Option<String>, SourceError> {
15669 let visible = content.unwrap_or_default();
15670 if metadata.is_empty() {
15671 return Ok((!visible.is_empty()).then(|| visible.to_owned()));
15672 }
15673 let encoded = serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
15674 message: error.to_string(),
15675 })?;
15676 Ok(Some(if visible.is_empty() {
15677 format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15678 } else {
15679 format!("{visible}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15680 }))
15681}
15682
15683fn required_bool(value: &Value, field: &str) -> Result<bool, SourceError> {
15684 value
15685 .get(field)
15686 .and_then(Value::as_bool)
15687 .ok_or_else(|| SourceError::Malformed {
15688 message: format!("GitHub response is missing boolean field {field}"),
15689 })
15690}
15691fn optional_str<'a>(value: &'a Value, field: &str) -> Result<Option<&'a str>, SourceError> {
15692 match value.get(field) {
15693 None | Some(Value::Null) => Ok(None),
15694 Some(value) => value
15695 .as_str()
15696 .map(Some)
15697 .ok_or_else(|| SourceError::Malformed {
15698 message: format!("GitHub response field {field} is not a string or null"),
15699 }),
15700 }
15701}
15702fn optional_nodes<'a>(
15703 connection: Option<&'a Value>,
15704 name: &str,
15705) -> Result<Option<&'a Vec<Value>>, SourceError> {
15706 match connection {
15707 None | Some(Value::Null) => Ok(None),
15708 Some(value) => value
15709 .get("nodes")
15710 .and_then(Value::as_array)
15711 .map(Some)
15712 .ok_or_else(|| SourceError::Malformed {
15713 message: format!("GitHub {name}.nodes is not an array"),
15714 }),
15715 }
15716}
15717fn complete_connection(connection: &Value, name: &str, size: u32) -> Result<(), SourceError> {
15718 let page_info = connection
15719 .get("pageInfo")
15720 .ok_or_else(|| SourceError::Malformed {
15721 message: format!("GitHub {name} has no pageInfo"),
15722 })?;
15723 if required_bool(page_info, "hasNextPage")? {
15724 return Err(SourceError::Malformed {
15725 message: format!(
15726 "GitHub {name} exceeds the supported nested connection size of {size}"
15727 ),
15728 });
15729 }
15730 Ok(())
15731}
15732fn optional_time(value: &Value, field: &str) -> Result<Option<DateTime<Utc>>, SourceError> {
15733 optional_str(value, field)?
15734 .map(|timestamp| {
15735 timestamp.parse().map_err(|error| SourceError::Malformed {
15736 message: format!("GitHub response field {field} is not a timestamp: {error}"),
15737 })
15738 })
15739 .transpose()
15740}
15741fn validate_page(page: &PageRequest) -> Result<(), SourceError> {
15742 if page.limit == 0 {
15743 Err(SourceError::Config {
15744 message: "page limit must be at least 1".into(),
15745 })
15746 } else {
15747 Ok(())
15748 }
15749}
15750fn next_cursor(connection: &Value) -> Result<Option<Cursor>, SourceError> {
15751 let page = connection
15752 .get("pageInfo")
15753 .filter(|value| value.is_object())
15754 .ok_or_else(|| SourceError::Malformed {
15755 message: "GitHub connection is missing pageInfo".into(),
15756 })?;
15757 if required_bool(page, "hasNextPage")? {
15758 let cursor = required_str(page, "endCursor")?;
15759 validate_cursor_progress(None, cursor)?;
15760 Ok(Some(Cursor(cursor.into())))
15761 } else {
15762 Ok(None)
15763 }
15764}
15765fn validate_cursor_progress(previous: Option<&str>, next: &str) -> Result<(), SourceError> {
15766 if next.is_empty() || previous == Some(next) {
15767 Err(SourceError::Malformed {
15768 message: "GitHub pagination cursor is empty or did not advance".into(),
15769 })
15770 } else {
15771 Ok(())
15772 }
15773}
15774/// The version of this plugin's opaque narrowing-search cursor.
15775pub const SEARCH_CURSOR_VERSION: u32 = 4;
15776
15777#[derive(Serialize, Deserialize)]
15778#[serde(tag = "state", rename_all = "snake_case", deny_unknown_fields)]
15779enum SearchConnection {
15780 Initial {},
15781 Continuing { after: Cursor },
15782 Exhausted {},
15783}
15784impl SearchConnection {
15785 fn after(&self) -> Option<&str> {
15786 match self {
15787 Self::Continuing { after } => Some(&after.0),
15788 _ => None,
15789 }
15790 }
15791 fn exhausted(&self) -> bool {
15792 matches!(self, Self::Exhausted { .. })
15793 }
15794 /// Whether a cursor naming this position, `offset` rows into its page, is one this
15795 /// plugin could have handed out: a page is resumed only part of the way through it — an
15796 /// offset of a whole page or more would skip rows nobody was given — an initial page
15797 /// only once some of it was handed out, and an exhausted connection has no page to be
15798 /// part of the way through.
15799 fn valid_resume(&self, offset: usize) -> bool {
15800 let within = offset < SEARCH_PAGE_SIZE as usize;
15801 match self {
15802 Self::Initial { .. } => offset > 0 && within,
15803 Self::Continuing { after } => !after.0.is_empty() && within,
15804 Self::Exhausted { .. } => offset == 0,
15805 }
15806 }
15807}
15808
15809/// Versioned source cursor. A zero offset and empty own-write ids are omitted.
15810#[derive(Serialize, Deserialize)]
15811#[serde(deny_unknown_fields)]
15812struct SearchPosition {
15813 version: u32,
15814 connection: SearchConnection,
15815 /// How many rows of the page `connection` starts were already handed out.
15816 #[serde(default, skip_serializing_if = "is_zero")]
15817 offset: usize,
15818 #[serde(default, skip_serializing_if = "Vec::is_empty")]
15819 seen: Vec<NativeId>,
15820 #[serde(default, skip_serializing_if = "Vec::is_empty")]
15821 own: Vec<NativeId>,
15822}
15823impl Default for SearchPosition {
15824 fn default() -> Self {
15825 Self {
15826 version: SEARCH_CURSOR_VERSION,
15827 connection: SearchConnection::Initial {},
15828 offset: 0,
15829 seen: Vec::new(),
15830 own: Vec::new(),
15831 }
15832 }
15833}
15834
15835fn is_zero(offset: &usize) -> bool {
15836 *offset == 0
15837}
15838
15839fn numeric_cursor(cursor: Option<&Cursor>) -> Result<usize, SourceError> {
15840 cursor.map_or(Ok(0), |c| {
15841 c.0.parse().map_err(|_| SourceError::Config {
15842 message: "page cursor is invalid".into(),
15843 })
15844 })
15845}
15846fn offset_page<T>(mut items: Vec<T>, offset: usize, limit: usize) -> Page<T> {
15847 if offset > items.len() {
15848 return Page::last(vec![]);
15849 }
15850 let tail = items.split_off(offset);
15851 let mut selected = tail;
15852 let next = (selected.len() > limit).then(|| Cursor((offset + limit).to_string()));
15853 selected.truncate(limit);
15854 Page {
15855 items: selected,
15856 next,
15857 }
15858}
15859
15860impl GitHubProjectsSource {
15861 async fn write_task_assets(
15862 &self,
15863 write: &ItemWrite<Task>,
15864 assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
15865 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
15866 self.write_item(
15867 &self.task_incoming(write, assets)?,
15868 write.target.as_ref(),
15869 &write.depends_on,
15870 )
15871 .await
15872 }
15873
15874 /// One task write as [`Self::write_item`] takes it, once what it delivers and its priority
15875 /// are known to be ones this source can write.
15876 fn task_incoming<'a>(
15877 &self,
15878 write: &'a ItemWrite<Task>,
15879 assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
15880 ) -> Result<Incoming<'a>, SourceError> {
15881 let near = write.target.as_ref().unwrap_or(&write.item.id);
15882 for (key, entries) in [
15883 (TaskRef::DELIVERS_KEY, &write.item.delivers),
15884 (TaskRef::DELIVERED_BY_KEY, &write.item.delivered_by),
15885 ] {
15886 TaskRef::listed(key, near, Some(&self.name), entries.clone())
15887 .map_err(|message| SourceError::Refused { message })?;
15888 }
15889 if self.priorities.is_none() && write.item.priority != Priority::None {
15890 return Err(self.holds_no_priority());
15891 }
15892 Ok(Incoming {
15893 written: Written::Work(ItemKind::Task, &write.item.status),
15894 title: &write.item.title,
15895 content: write.item.content.as_deref(),
15896 assets,
15897 labels: &write.item.labels,
15898 metadata: &write.item.metadata,
15899 repositories: &write.item.repositories,
15900 classification: write.item.classification,
15901 parent: write.item.project.as_ref(),
15902 delivers: &write.item.delivers,
15903 delivered_by: &write.item.delivered_by,
15904 priority: self.priorities.as_ref().map(|_| write.item.priority),
15905 })
15906 }
15907 async fn write_document_assets(
15908 &self,
15909 write: &ItemWrite<Document>,
15910 assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
15911 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
15912 // A document takes part in no dependency graph, so there is no far end to write
15913 // natively and none to record: a caller naming one is told so rather than having it
15914 // stored under the reserved key, where a later read would report an edge the
15915 // contract says cannot exist.
15916 if !write.depends_on.is_empty() {
15917 return Err(SourceError::Refused {
15918 message: format!(
15919 "this write names {} dependencies for a document, and a document takes \
15920 part in no dependency graph; next: put the dependency on the task or \
15921 project the document is about",
15922 write.depends_on.len()
15923 ),
15924 });
15925 }
15926 self.write_item(
15927 &Incoming {
15928 written: Written::Document,
15929 title: &write.item.title,
15930 content: write.item.content.as_deref(),
15931 assets,
15932 labels: &write.item.labels,
15933 metadata: &write.item.metadata,
15934 repositories: &write.item.repositories,
15935 classification: write.item.classification,
15936 parent: write.item.project.as_ref(),
15937 delivers: &[],
15938 delivered_by: &[],
15939 priority: None,
15940 },
15941 write.target.as_ref(),
15942 &[],
15943 )
15944 .await
15945 }
15946}
15947
15948/// What [`TaskSource::end_command`] leaves of this source's held state, asserted on the state
15949/// itself, for the two things no journey can observe.
15950///
15951/// The journeys in `crates/onetaskgraph-e2e/tests/e2e/end_command.rs` prove through the engine,
15952/// with and without the call, that a settlement, a board listing and a metadata search each
15953/// read afresh after it — the resolved records, the written-item overlay, the board and its
15954/// search, and the narrowed searches. What they cannot reach is the held field definitions,
15955/// because a status write naming an option a person deleted is refused the same whether or
15956/// not the list is held, and a poisoned lock, because nothing outside the source can panic
15957/// while one of its locks is held. So these assert those directly, and every other holder
15958/// beside them so a holder added later without a clear in the call fails here.
15959#[cfg(test)]
15960mod end_command_tests {
15961 use super::*;
15962
15963 struct Token;
15964
15965 impl SecretResolver for Token {
15966 fn get(&self, var: &str) -> Option<SecretString> {
15967 (var == "GH_PROJECTS_TOKEN").then(|| "test-token".into())
15968 }
15969 }
15970
15971 fn source() -> GitHubProjectsSource {
15972 let config = serde_json::from_value(json!({
15973 "owner": "octo-org", "project_number": 7, "repository": "acme/work",
15974 // Nothing here is sent: the source is only built and its state inspected.
15975 "endpoint": "http://127.0.0.1:9/graphql",
15976 }))
15977 .expect("a usable configuration");
15978 GitHubProjectsSource::new(&SourceName::new("work").unwrap(), config, &Token)
15979 .expect("the source builds")
15980 }
15981
15982 /// One issue as a board read answers it.
15983 fn resolved(source: &GitHubProjectsSource) -> Resolved {
15984 source
15985 .resolve(&json!({
15986 "id": "ITEM-1",
15987 "content": {"__typename": "Issue", "id": "I_1", "title": "Held",
15988 "body": "what a person may since have edited", "state": "OPEN",
15989 "stateReason": null, "url": null, "number": 1,
15990 "subIssuesSummary": {"total": 0},
15991 "labels": {"nodes": [], "pageInfo": {"hasNextPage": false}}},
15992 "fieldValues": {"nodes": [], "pageInfo": {"hasNextPage": false}},
15993 }))
15994 .expect("the item reads")
15995 .expect("an issue")
15996 }
15997
15998 /// Hold something in every holder the call clears, and the repository id it keeps.
15999 fn fill(source: &GitHubProjectsSource) {
16000 let item = resolved(source);
16001 source.created.lock().unwrap().push(item.clone());
16002 source.updated.lock().unwrap().push(item.clone());
16003 *source.board_cache.lock().unwrap() = Some(Board {
16004 id: "PVT-board".into(),
16005 fields: json!({"nodes": []}),
16006 items: vec![item.clone()],
16007 });
16008 *source.search_cache.lock().unwrap() = Some(vec![item.clone()]);
16009 source
16010 .narrowed_cache
16011 .lock()
16012 .unwrap()
16013 .insert("status:todo".into(), vec![item.clone()]);
16014 source.children_cache.lock().unwrap().insert(
16015 NativeId("P-1".into()),
16016 (NativeId("P-1".into()), vec![item.clone()]),
16017 );
16018 source
16019 .search_next
16020 .lock()
16021 .unwrap()
16022 .insert("status:todo".into(), Some("cursor".into()));
16023 source
16024 .resolved_cache
16025 .lock()
16026 .unwrap()
16027 .insert(item.id.clone(), item);
16028 *source.fields_cache.lock().unwrap() = Some(BoardFields {
16029 id: BoardId::parse("PVT-board").unwrap(),
16030 fields: json!({"nodes": []}),
16031 });
16032 source
16033 .repository_cache
16034 .lock()
16035 .unwrap()
16036 .insert(RepositoryTarget::parse("acme/work").unwrap(), "R_1".into());
16037 }
16038
16039 fn assert_dropped(source: &GitHubProjectsSource) {
16040 assert!(source.created().unwrap().is_empty(), "created");
16041 assert!(source.updated().unwrap().is_empty(), "updated");
16042 assert!(source.board_cache().unwrap().is_none(), "board");
16043 assert!(source.search_cache.lock().unwrap().is_none(), "search");
16044 assert!(source.narrowed_cache.lock().unwrap().is_empty(), "narrowed");
16045 assert!(
16046 source.children_cache.lock().unwrap().is_empty(),
16047 "project children"
16048 );
16049 assert!(
16050 source.search_next.lock().unwrap().is_empty(),
16051 "search paging"
16052 );
16053 assert!(
16054 source.resolved_cache().unwrap().is_empty(),
16055 "resolved records"
16056 );
16057 assert!(source.fields_cache().unwrap().is_none(), "board fields");
16058 assert_eq!(
16059 source.repository_cache().unwrap().len(),
16060 1,
16061 "a repository's node id stays valid and is kept"
16062 );
16063 }
16064
16065 fn end(source: &GitHubProjectsSource) {
16066 tokio::runtime::Builder::new_current_thread()
16067 .build()
16068 .unwrap()
16069 .block_on(source.end_command())
16070 .expect("the command ends");
16071 }
16072
16073 #[test]
16074 fn the_call_drops_every_item_search_and_board_read_and_keeps_repository_ids() {
16075 let source = source();
16076 fill(&source);
16077 end(&source);
16078 assert_dropped(&source);
16079 }
16080
16081 #[test]
16082 fn the_call_clears_a_lock_an_earlier_failure_poisoned() {
16083 fn poison<T: Send>(held: &Mutex<T>) {
16084 std::thread::scope(|scope| {
16085 let _ = scope
16086 .spawn(|| {
16087 let _guard = held.lock().unwrap();
16088 panic!("a failure while the lock is held");
16089 })
16090 .join();
16091 });
16092 assert!(held.is_poisoned());
16093 }
16094 let source = source();
16095 fill(&source);
16096 poison(&source.created);
16097 poison(&source.updated);
16098 poison(&source.board_cache);
16099 poison(&source.search_cache);
16100 poison(&source.narrowed_cache);
16101 poison(&source.search_next);
16102 poison(&source.resolved_cache);
16103 poison(&source.fields_cache);
16104 assert!(
16105 source.resolved_cache().is_err(),
16106 "a poisoned lock is refused before the call"
16107 );
16108 end(&source);
16109 assert_dropped(&source);
16110 }
16111}
16112
16113#[cfg(test)]
16114mod unprojectable_fields_tests {
16115 use super::*;
16116
16117 /// The field names `docs/metadata.md` says a `metadata_fields` entry may not name.
16118 fn documented() -> std::collections::BTreeSet<String> {
16119 let document = std::fs::read_to_string(
16120 std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../docs/metadata.md"),
16121 )
16122 .expect("docs/metadata.md is readable");
16123 let document = document.split_whitespace().collect::<Vec<_>>().join(" ");
16124 let lead = "a `field` equal, ignoring case, to ";
16125 let start = document.find(lead).expect("the refused-field sentence") + lead.len();
16126 let sentence = &document[start..];
16127 let sentence = &sentence[..sentence
16128 .find("; and two entries naming one field")
16129 .expect("the refused-field sentence ends at the duplicate rule")];
16130 let (own, github) = sentence
16131 .split_once(" or a field GitHub owns on every board (")
16132 .expect("the sentence names GitHub's own fields");
16133 own.split('`')
16134 .skip(1)
16135 .step_by(2)
16136 .chain(
16137 github
16138 .strip_suffix(')')
16139 .expect("GitHub's own fields close the sentence")
16140 .split(", "),
16141 )
16142 .map(str::to_owned)
16143 .collect()
16144 }
16145
16146 #[test]
16147 fn the_documented_refused_fields_are_exactly_the_ones_the_configuration_refuses() {
16148 let refused: std::collections::BTreeSet<String> = UNPROJECTABLE_FIELDS
16149 .iter()
16150 .map(|&name| name.to_owned())
16151 .collect();
16152 assert_eq!(documented(), refused, "the fields docs/metadata.md lists");
16153 let instance = SourceName::new("work").unwrap();
16154 for name in UNPROJECTABLE_FIELDS {
16155 for spelled in [name.to_owned(), name.to_uppercase()] {
16156 let entry = MetadataFieldConfig {
16157 field: spelled.clone(),
16158 key: "team.machine".to_owned(),
16159 path: Vec::new(),
16160 };
16161 let refused = MetadataField::resolve(vec![entry], &instance)
16162 .expect_err("an unprojectable field is refused");
16163 assert!(
16164 refused.to_string().contains(&format!("{name:?}")),
16165 "{spelled}: {refused}"
16166 );
16167 }
16168 }
16169 }
16170}