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//! | one board page with each task's first comments, for `TaskSource::query_task_details` | [`graphql::BOARD_WITH_COMMENTS`] — the board's own `items`, without search completion | tasks and first comment pages in one request; board consistency, with comment-activity filtering refused |
237//! | 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 |
238//! | 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 |
239//! | one project's tasks or documents | [`graphql::SUB_ISSUES`] — that issue's own `subIssues` | that project |
240//! | which projects this board holds | [`graphql::SEARCH_ISSUES`] — an issue search scoped to the board | the board's issues, without their board items |
241//! | 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 |
242//! | 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 |
243//! | 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 |
244//! | 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 |
245//! | 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 |
246//! | 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 |
247//!
248//! The following standalone-ticket requests are pinned by the real CLI fixture journeys
249//! `follow_up_writes_resolve_each_item_once_and_batch_the_copy_fields` and
250//! `a_batched_detail_read_costs_one_request_and_one_point_per_detail_batch`, as request count
251//! equal to declared points equal to the row. They include the origin lookup and the
252//! field/repository discovery a create needs. A bound re-copy changes status, priority,
253//! content and metadata; comment recount means a subsequent detail read. Each request here
254//! costs one declared point. A membership beyond the embedded page can additionally require
255//! the one-point membership recovery described above. A bound re-copy of a task filed under a
256//! project adds one read, the engine confirming that project's link by its own id once per
257//! command; and the same-source far ends a write newly names — those that do not already block
258//! the item, whose own read answered for them — are read together by their own ids,
259//! [`DETAIL_BATCH`] to one [`graphql::ISSUE_DETAILS`] request, each new edge then one
260//! [`graphql::ADD_BLOCKED_BY`]. Both additions are rows of the table below, pinned by
261//! `a_bound_recopy_adds_one_project_read_and_batches_the_dependencies_it_newly_names`.
262//!
263//! **[`DETAIL_BATCH`] is 24**: the largest batch of [`graphql::ISSUE_DETAILS`] the node-count
264//! model prices at one point. Each aliased item is six of GitHub's aggregate, so 24 are 144,
265//! which rounds to one point, and 25 are 150, which rounds to two; `tests/point_cost.rs`
266//! holds both halves.
267//!
268//! **An existing item is written body last.** A bound re-copy and a `task update` send its
269//! board fields first — the `Status` option and the `Priority` together, in one request — then
270//! its parent and its `blockedBy`, and its title, body and state in one `updateIssue` last.
271//! GitHub runs no two requests as one, and runs a document's mutation fields in order without
272//! undoing an earlier field when a later one fails, so that order is what makes a write
273//! refused part-way leave the item's body, and every metadata key in it, exactly as it stood;
274//! the one piece of metadata written before the body, an origin a copy re-points, is put back
275//! when a later write is refused — and when putting it back is refused too, the write's own
276//! refusal names that key, what it now holds and what it held. `crates/onetaskgraph-github-projects-e2e/tests/e2e/write_order.rs` refuses each
277//! of those writes in turn, whole and as one aliased field failing after the one before it.
278//!
279//! **Two facts about GitHub the write rows rest on, each read off GitHub's published schema
280//! artifact <https://docs.github.com/public/fpt/schema.docs.graphql> on 2026-10-01 and pinned
281//! in `tests/fixtures/schema.graphql`, and the first then put to GitHub itself:**
282//!
283//! - **A board is accepted at creation but its item is not answered, so a create still files
284//! the issue itself: a new copy is 5 requests, and 4 with `--create`.**
285//! `CreateIssueInput.projectV2Ids: [ID!]` is declared there — "An array of Node IDs for
286//! Projects V2 associated with this issue", `@possibleTypes(concreteTypes: ["ProjectV2"])`.
287//! The credentialed journey `real_projects_v2_contract_writes_and_leaves_no_residue` was run
288//! against a real board on 2026-10-01 with a create sending the board there and reading the
289//! item off the payload's `Issue.projectItems`: every one of its four creates answered with
290//! no item on the board, so each went on to [`graphql::ADD_TO_BOARD`], and the fourth was
291//! refused "Content already exists in this project" — GitHub had filed the issue after
292//! answering, and refuses a second filing rather than answering with the item it holds. A
293//! create therefore sends no `projectV2Ids` and files the issue with
294//! `addProjectV2ItemById`, the one call whose answer names the board item. The saving that is
295//! real is the read before it: the board's fields and the repository's id together, in
296//! [`graphql::CREATION_CONTEXT`], at the point the repository is known.
297//! - **A comment still reads its target first, so a comment is 2 requests.**
298//! `AddCommentInput.subjectId: ID!` is declared there with
299//! `@possibleTypes(concreteTypes: ["Issue", "PullRequest"], abstractType:
300//! "IssueOrPullRequest")`. A board draft is no such subject and would be refused, but a
301//! project's issue, a document's issue, an issue on no board of this source and a pull
302//! request all are: GitHub writes the comment, so there is no refusal to map into "that is
303//! not a task of this board". [`graphql::ISSUE`] before [`graphql::ADD_COMMENT`] is what
304//! refuses those by name.
305//!
306//! | Verb | Requests / points | Documents |
307//! | --- | --- | --- |
308//! | 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 |
309//! | copy --create | 4 | CREATION_CONTEXT, CREATE_ISSUE, CREATED_BOARD_ITEMS, WRITE_FIELD_VALUES: the new copy with no origin to ask about |
310//! | 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 |
311//! | 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 |
312//! | 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 |
313//! | comment | 2 | ISSUE, ADD_COMMENT: the target is read first, because GitHub accepts a comment on any issue or pull request (see below) |
314//! | detail | 1 | ISSUE_DETAIL: the item and its first page of comments, for `task show` and `task comment list`; `--no-comments` is ISSUE alone |
315//! | batched detail | ceil(n / DETAIL_BATCH) | ISSUE_DETAILS: `task show-many` of `n` items, DETAIL_BATCH (24) at a time, comments included or not |
316//! | 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 |
317//! | recount | 1 | ISSUE_DETAIL |
318//! | status | 2 | ISSUE, UPDATE_FIELD; a terminal status additionally updates issue state |
319//! | priority | 2 | ISSUE, UPDATE_FIELD or CLEAR_FIELD, with stored priority in the mutation response |
320//! | content | 2 | ISSUE, UPDATE_ISSUE |
321//! | metadata | 2 | ISSUE, UPDATE_ISSUE |
322//! | 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 |
323//! | 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 |
324//! | record only | 1 | ISSUE |
325//!
326//! <!-- github-search-paging:start -->
327//! Board-scoped text, metadata, project-name and comment-activity searches send every
328//! page at `first = 20` (SEARCH_PAGE_SIZE), the SEARCH_ISSUES document's one-point
329//! ceiling. A later page is sent only when `hasNextPage` is true and the caller still
330//! needs rows. A page is never resized to the rows still needed: GitHub orders one
331//! search differently at different page sizes, so one fixed size makes a paged walk
332//! send exactly the requests one whole read sends, and the answer's order is the order
333//! those pages arrive in. A page below twenty would cost the same one point, and GitHub
334//! prices this document by rows, so twenty-row pages cost per row what 100-row pages do.
335//! Project-name lookup continues until an exact match or exhaustion. A task limit bounds
336//! returned and fetched pages: a limit is sliced from the pages it needs, and local
337//! confirmation can require more candidates than matching rows. Walking all pages
338//! returns the whole answer. The opaque version-4 source cursor carries GitHub's page
339//! cursor and how far into that page the last answer stopped, and resumes in the same
340//! process or a new one, without duplicates or gaps. It carries no rows: one process
341//! sends each page's search once, and a new process re-reads only the page it resumes
342//! in, then sends a further page once, never as a re-read, only when its limit still
343//! needs rows. Every request either walk sends is the one a whole read sends for that page. Own writes replace stale index
344//! copies and complete missing rows at exhaustion. Cache entries are whole GitHub pages,
345//! so a small answer cannot truncate a wider question. Origin pages remain three; whole-board sizing is unchanged.
346//! Read-after-write is a per-process guarantee. A cursor resumed in a new process is
347//! not required to include the original process's writes still omitted by the index.
348//! <!-- github-search-paging:end -->
349//!
350//! The board half of an issue — its board item's id, its `Status` option and this
351//! source's origin text field — rides along on `Issue.projectItems` in the first three, so
352//! an item reached any of those ways resolves through the same
353//! [`GitHubProjectsSource::resolve`] the board walk uses and reports the same title, the
354//! same status, the same labels and the same qualified id. That connection comes back a
355//! *page* at a time, at `BOARD_ITEMS_PAGE_SIZE`, so the entry for this board is looked for
356//! on the page in hand and — only if that page reports more of the connection — in the
357//! last row's read of that one issue's memberships, resumed from the page's own cursor and
358//! walked to exhaustion. An issue with no entry for *this* board is not this source's to
359//! report, which is what keeps an id naming another repository's issue from being answered
360//! as an item of this board; and because the page is where the search starts rather than
361//! where it ends, that answer is one about a connection read to exhaustion and never about
362//! an unread page. Nothing costs the extra read but an issue on more boards than a page
363//! holds: an issue this board really does not hold reports no next page, so its
364//! memberships are already exhausted where they arrived.
365//!
366//! **No document here selects the board's own `Labels` field, and nothing is lost by
367//! that.** An item's labels are read from its content alone, wherever that content is
368//! reached: the three documents above select `Issue.labels` on the fragment, and
369//! [`graphql::BOARD`] selects the same connection on the `... on Issue` arm of its
370//! `content`. A board's `Labels` field is not one anybody fills in: it is a built-in
371//! `ProjectV2FieldType`, it is absent from `ProjectV2CustomFieldType` so no project can
372//! create one, and `ProjectV2FieldValue` — the whole of what
373//! `updateProjectV2ItemFieldValue` accepts — offers no way to write one. So GitHub derives
374//! it from the content, for every content type it exists on, and there is nothing it can
375//! hold that the content does not already say: for an `Issue` it *is* that issue's own
376//! labels, so selecting it beside them unions a set with itself.
377//!
378//! **A draft loses nothing by that either**, which is the reasoning this paragraph once had
379//! backwards. `DraftIssue` exposes no `labels` field, and by the three schema facts above
380//! it cannot carry a board `Labels` value to be derived from one — so a draft has nothing
381//! to select *and nothing to lose*, and reports no labels at all. A `PullRequest` item is
382//! discarded by [`GitHubProjectsSource::resolve`] before labels are read. Both halves are
383//! held to that by tests in `tests/plugin.rs`: the four ways an item is reached report one
384//! label set, and that set is the fixture issue's own, by
385//! `an_item_reports_the_same_labels_title_status_and_id_however_it_is_reached`; and a board
386//! item whose content is a draft reports an empty set, by
387//! `a_board_item_whose_content_is_a_draft_reports_no_labels_at_all`. The absence of the
388//! selection is held over [`graphql::DOCUMENTS`] by
389//! `no_document_selects_the_boards_own_labels_field`.
390//!
391//! The whole-board row is still the board's own item connection, and deliberately: a
392//! **draft** board item is not an issue, so no search can list one, and the reads that have
393//! to answer for the whole board are the ones whose cost is the board's size anyway.
394//!
395//! **A question about one item this source already names by id never lists the board.**
396//! Whether that item is on this board, and what its board fields are, is answered by reading
397//! that item — its own `Issue.projectItems`, walked to exhaustion by
398//! [`GitHubProjectsSource::resolve_issue`], or a draft's own board item — and never by
399//! looking for it in [`graphql::BOARD`]'s `items` or in a listing this command already
400//! holds. That covers a write's destination, the project a new item is filed under, a
401//! same-source far end a dependency names, a status write, the dependency slot a draft keeps,
402//! and the delete that takes back an item a copy made. What such a write needs of the board
403//! and the item does not carry — the board's id, the `Status` and origin field definitions —
404//! comes from [`graphql::BOARD_FIELDS`], which reads no item at all. The reason is evidence,
405//! not economy alone: `ProjectV2.items` is a projection that lags the membership GitHub
406//! itself reports — an issue added with `addProjectV2ItemById` can be missing from it for
407//! minutes. Scanning this host's 842-item board has refused a document copy and an update
408//! even though the items' own reads named that board. A scan there gives the wrong answer
409//! as well as paying for every page. So a `board.items` lookup does not belong on any of
410//! those paths.
411//!
412//! **What a read may return is capped too, and that cap is on the document rather than on
413//! the board.** GitHub limits the number of nodes **one query may return** to
414//! [`NODE_COUNT_LIMIT`] and refuses a query above that before executing it: the answer is
415//! an error naming the connection the count crossed at, not a slow or a partial result.
416//! Every board this source reads is refused the same way, so no board is too big for these
417//! documents and none is small enough to save one that is over.
418//!
419//! The count is arithmetic over the document's own text: each connection contributes the
420//! `first:` it asks for, counts **multiply** down a nested path and **sum** across sibling
421//! paths. Those are [GitHub's published rules][node-limits] and this workspace does not
422//! restate them — `github-graphql-node-count` implements them, and
423//! [`worst_case_node_count`] under [`largest_page_sizes`] is where every node count here
424//! comes from. `every_document_this_source_sends_stays_under_githubs_node_limit`, in
425//! `tests/node_count.rs`, recomputes every document in [`graphql::DOCUMENTS`] from that
426//! same text on every run and fails naming any that reaches the limit — so a connection
427//! added to a shared fragment is caught there rather than by GitHub.
428//!
429//! What decides those counts is the page sizes: [`MAX_PAGE_SIZE`] on the outer page,
430//! `NESTED_PAGE_SIZE` on the connections hanging off one item, and
431//! `BOARD_ITEMS_PAGE_SIZE` on the page of an issue's board memberships a read carries.
432//! `$nestedFirst` is spent twice down one path of a board read, so that constant is
433//! effectively squared there, which is why it is the one the limit is most sensitive to.
434//! `BOARD_ITEMS_PAGE_SIZE` is small for a reason of its own, recorded beside it: what a
435//! page of memberships misses is recovered by one further read rather than refused, so it
436//! buys a bound every read pays for at the price of a request only a multi-board issue
437//! pays.
438//!
439//! **`nodeCount` and `cost` are two numbers against two limits, and both are computed
440//! offline here — per document, one document at a time.** `nodeCount` is the one above: the
441//! most nodes one query may return, checked per query and bounded by [`NODE_COUNT_LIMIT`].
442//! `cost` is rate-limit points, metered per hour across everything one credential does; it
443//! is what the two limiters [`Limiter`] tells apart meter, and a document under
444//! [`NODE_COUNT_LIMIT`] still says nothing about its price. [`worst_case_point_cost`] is
445//! that second number, and `tests/point_cost.rs` pins every document in
446//! [`graphql::DOCUMENTS`] at what it costs — there being no per-call point ceiling to hold
447//! one under, the pin itself is the check. The credentialed lane reconciles both figures
448//! against GitHub's own, off a probe it already sends.
449//!
450//! **What is pinned that way is a per-document price and never a session's.** The record in
451//! `session-cost.md` measures the two quantities a whole session can be counted in offline —
452//! **requests** and **worst-case nodes** — and neither is points. What one whole session
453//! consumes of the hourly point allowance is observable only from a credentialed run's own
454//! `x-ratelimit-*` headers, which is what [`accounting`] fills its per-budget figures from
455//! and what `tests/live.rs` prints at the end of every run.
456//!
457//! [node-limits]: https://docs.github.com/en/graphql/overview/rate-limits-and-node-limits-for-the-graphql-api
458//!
459//! **Where a read-after-write guarantee comes from, since neither of GitHub's two
460//! enumerations of a board can supply one alone.** Resolving a node id is strongly
461//! consistent, so a read by id and a project's own sub-issues are already current. The
462//! other two are not, and they are behind by different amounts and in different directions:
463//!
464//! - GitHub's **issue search** is an index and answers a write made moments ago with the
465//! value from before it — usually for a second or two.
466//! - **`ProjectV2.items`** is a projection GitHub rebuilds behind the write, and an item put
467//! on a board with `addProjectV2ItemById` can be **absent** from it — not present with its
468//! content withheld, absent, with the connection walked to its own `hasNextPage: false` —
469//! for *minutes*, while `Issue.projectItems` names the same membership at once.
470//!
471//! That second one is a measurement rather than a caution. This repository's own
472//! credentialed journey writes a project and waits for the board to report it, then writes a
473//! task and waits for the same thing seconds later on the same board: the project wait is
474//! answered through the search and converged in two or three attempts in each of three runs,
475//! and the task wait is answered through `ProjectV2.items` and converged in none of them
476//! inside thirty. Separately, an item added to a second and larger board was read back by
477//! `Issue.projectItems` on that board's own id while every one of that connection's nine
478//! pages, walked to exhaustion nine minutes after the add, did not name it. Reading a board
479//! through the lagging one alone is what had a board read deny an issue that had certainly
480//! landed on it.
481//!
482//! So [`GitHubProjectsSource::board`] is the **union** of both — each search result still
483//! admitted only on this board's own strongly-consistent `Issue.projectItems`, and neither
484//! enumeration dropped, because only `ProjectV2.items` lists a board draft and only the
485//! search reports what the projection is behind on. What closes the last
486//! gap, the one where both are behind, is [`GitHubProjectsSource::created`]: every read this
487//! source answers is completed with what this process itself wrote, so an item created
488//! seconds ago is reported whether or not GitHub has caught up. Nothing else is remembered,
489//! nothing is written down, and the record dies with the process. **A wait that has to
490//! observe GitHub's own data cannot be answered from that record** — which is why the
491//! credentialed journey asks through a source built afresh, and why the union above rather
492//! than a longer wait is what makes such a wait converge.
493//!
494//! **A narrowed read is the same bargain, stated for each of the three predicates it
495//! answers.** A read carrying a text, metadata or origin predicate asks GitHub's index rather
496//! than walking the board, and every such answer is completed with what this process wrote —
497//! its [`created`](GitHubProjectsSource::created) record and every existing item it wrote,
498//! each filtered by the same predicates as the rest — so an item this command wrote a moment
499//! ago is returned by a query that matches it whether or not the index has caught up. An item
500//! a caller holds the id of is read by that id, with `node(id:)`, which is strongly
501//! consistent. What is left is stated rather than papered over:
502//!
503//! | Read | Finds | Behind by |
504//! | --- | --- | --- |
505//! | text, metadata | the issue search for the phrases | what another process wrote in the last second or two, until GitHub indexes it |
506//! | 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 |
507//! | 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 |
508//! | origin, third read | this process's own writes | nothing |
509//!
510//! So an origin carrier another process added within the last second or two, before either
511//! index has it, can be missing from an origin query, and one written by the release before
512//! this one — its origin in the field alone — can be missing for as long as the board's own
513//! item connection is behind on it. A copy that must not duplicate its own earlier write
514//! relies on the link it records, not on either index. **A board draft is not an issue**, so
515//! a draft is never returned by a text, metadata or origin query, whatever it holds: no search
516//! lists one, the origin lookup drops any the board's own field filter names, and one this
517//! process wrote is not added back either.
518//!
519//! **The origin lives in the board field, and the body holds a mirror of it.** A write that
520//! carries an origin writes it to the `onetaskgraph.origin` text field and also into the
521//! body's metadata slot, so the issue search can find it in seconds. The field is
522//! authoritative: this source reads an item's origin from the field alone, so a slot that
523//! disagrees with it, or holds one where the field holds none, is never read as a second
524//! origin — and the release before this one reads the slot, drops that key's copy for the
525//! field's, and sees the same one origin.
526//!
527//! Filtering happens before paging, so a page of a filtered result is a page of the
528//! survivors rather than the survivors of a page. Label matching and the substring rule a
529//! text candidate is confirmed by answer the same question the same way the local Markdown
530//! source's do; which candidates a text search has to confirm is GitHub's word match, which
531//! is the one place the two sources can answer the same text differently.
532//!
533//! <!-- llmlint: ignore[contracts_have_one_source_or_a_drift_gate] The declaration itself
534//! has one source, `capabilities`, and the note above is the reasoning behind it rather
535//! than a second copy of it: without the three facts recorded here a reader takes the
536//! uniform `Native` for a lie and reverts it. The drift gate on the declaration is this
537//! crate's own capabilities test, which pins every field of it against a fully spelled-out
538//! `Capabilities` literal — a struct with no `Default`, so a field added to the contract
539//! fails to compile there rather than going unasserted. -->
540//! The fixture-server tests above run wherever this crate is selected; the credentialed
541//! lane runs in the same required check, beside them, and can fail it — it verifies the
542//! 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,
543//! one filed under neither, a label on one of the three and a closed status on another —
544//! because that shape is what tells an honoured predicate from an ignored one: a board
545//! holding a single project answers a project filter the same way whether or not this
546//! source applies it, which is exactly how the defect above went unseen.
547//!
548//! That lane writes only to the board `GH_PROJECTS_OWNER` and `GH_PROJECTS_NUMBER` name,
549//! and the scratch repository `GH_PROJECTS_REPOSITORY` names:
550//! `nickderobertis/onetaskgraph-live-scratch`. It refuses the core repository before any
551//! session or request, independently of the live demand. Fix that variable in the machine's
552//! onetaskgraph `secrets.env` or the environment it pushes from, such as ai-orchestrator's
553//! `.env`. The targets are declared once in `onetaskgraph_github_live`, the lane's own
554//! policy crate; absent credentials or nominations otherwise skip.
555//!
556//! GitHub Actions artifacts carry `ci-<run id>-<attempt>-<micros>`, naming their writing
557//! run and attempt; invalid Actions identity refuses before writing. Other runs retain
558//! `<host>-<process>-<micros>`. Own cleanup matches the whole stamp. Machine residue stays
559//! the owning machine's lock sweep's; Linear always keeps that form and is unaffected.
560//! The hourly janitor is cleanup, never a test lane: scratch CI residue is removed only
561//! after its run reads back as `completed`, immediately before the listed-artifact batch.
562//! Failed or incomplete ownership reads preserve residue. A 24-hour waiting period
563//! is a margin, never the ownership authorisation. Cleanup leaves machine stamps
564//! and the board's `onetaskgraph.origin` field untouched.
565//!
566//! # What a session of requests costs, and where the report is
567//!
568//! This source records **every** request it sends into [`accounting::Accounting`], at
569//! `send_once` — the one place a request leaves this crate, which is why a read path added
570//! later is counted without anybody remembering to count it. That is the whole of what this
571//! crate adds to the arrangement; [`accounting`] is where what a record carries, how a
572//! session's spend is arrived at, and what it deliberately does not know are set out.
573//!
574//! What one whole session of the live journey costs, counted that way against this crate's
575//! loopback fixture board, is written down in `session-cost.md` beside this crate — with the
576//! reduction it came out of, and with what it does and does not say about rate-limit points.
577//!
578//! [`GitHubProjectsSource::accounting`] is the read: a snapshot to hold and compare, which
579//! [`accounting::Session::report`] renders the session report from. It is on the ordinary
580//! code path — no environment variable, no feature, no build configuration — because an
581//! instrument nobody switches on measures nothing, and
582//! [`Plugin::build_recording_into`] is how a caller making its own calls beside this
583//! source's counts the whole session rather than this source's share. The credentialed lane
584//! in `tests/live.rs` does exactly that, and prints the report at the end of every run,
585//! passed or failed.
586//!
587//! **A live session refuses to start unless the account can afford it.** Before it does any
588//! of the work it exists to do, the journey makes one request — `GET /rate_limit`, which
589//! GitHub documents as not counting against the REST rate limit and which answers both of
590//! its budgets at once — and starts only if, for each of them, what remains minus this
591//! session's estimated cost is still at least
592//! `onetaskgraph_live::RETAINED_BUFFER` — twenty per cent — of that budget's whole
593//! allowance. A session that cannot **declines**: it did not run, so it is
594//! neither a pass nor a failing assertion, and it says which budget was short, that budget's
595//! limit, what remained, the estimate, the buffer and when it resets — then stops, without
596//! waiting for the budget to come back. The estimate is derived offline from
597//! `tests/fixtures/session-cost.txt` and a cost model stated in `tests/journey/budget.rs`,
598//! which is also where the published rule that model rests on is cited; the accounting
599//! above records the gate's own read like any other request, and
600//! [`accounting::Session::report`] prints the estimate beside what the session really spent.
601//!
602//! **GitHub is the authority on both of its own numbers, and the credentialed lane goes and
603//! asks it.** Everything above computes `nodeCount` and `cost` offline from a document's own
604//! text, which is what lets it run on every platform and on a pull request from a fork with
605//! no credential — and that is what actually stops a regression merging. But an offline
606//! arithmetic can only ever agree with itself: if GitHub changes its rules, this workspace
607//! goes on computing the old answer and nothing notices. So `tests/live.rs` reconciles them.
608//! GitHub's schema exposes `rateLimit(dryRun: true)`, whose `nodeCount` is *"the maximum
609//! number of nodes this query may return"* and whose `cost` is what that document would
610//! spend, both for a document **without executing it**, and the lane asks it for every query
611//! document this source sends, under the largest bindings this source sends, and fails when
612//! GitHub's figure and [`worst_case_node_count`] or [`worst_case_point_cost`] disagree. A
613//! mutation is skipped, because `rateLimit` is a field of `Query` and cannot be asked about
614//! one; the offline pins still cover it. It records what those calls reported about the
615//! account's own allowance, because whether asking is free is a thing to observe rather than
616//! to assume. Two quantities, not one: [`NODE_COUNT_LIMIT`] bounds `nodeCount` per query,
617//! and `cost` is metered against an hourly allowance the accounting above reads off a
618//! credentialed run's own response headers.
619//!
620//! **GitHub has two rate limiters and this source is refused by both, so nothing here
621//! treats them as one thing.** The primary budget is the hourly allowance `gh api
622//! rate_limit` reports; the secondary limiter is a burst limiter over content-generating
623//! requests, and *nothing* reports it. Which one refused decides the operator's next step,
624//! so [`Limiter`] is a type rather than a detail, and it is what [`MIN_MUTATION_INTERVAL_MS`],
625//! [`GitHubProjectsSource::board_cache`] and [`GitHubProjectsSource::graphql`] each answer
626//! one part of.
627#![deny(missing_docs)]
628
629use std::collections::BTreeMap;
630use std::sync::{Arc, Mutex};
631use std::time::Duration;
632
633use chrono::{DateTime, Utc};
634use onetaskgraph_plugin_api::{
635 Capabilities, Classification, ClosedAs, Comment, CommentBody, CommentOrigin, Completion,
636 ContradictedItem, Coverage, Cursor, DependencyEdge, DependencyEndpoint, DependencyKind,
637 DependencySupport, Designator, Direction, Document, DocumentQuery, DocumentVersionCoverage,
638 Health, HeldTask, IssueHolding, IssueRef, ItemKind, ItemWrite, Label, LabelFilter, Location,
639 MetadataKey, Metering, NativeId, NewComment, Page, PageRequest, Priority, Project,
640 ProjectFilter, ProjectQuery, ProjectVersionCoverage, Repository, SecretResolver, SharedClock,
641 SourceError, SourceName, SourcePlugin, Status, StatusCategory, StatusContradiction,
642 StatusMapping, Support, Task, TaskDetailRead, TaskQuery, TaskRef, TaskSource, TaskUpdate,
643 TaskUpdateOutcome, TextFields, TextQuery, UnmappedStatus, UpdatedField, VersionCoverage,
644 WaitCause, WriteJournal, WriteSupport, system_clock,
645};
646use reqwest::{Client, StatusCode, Url};
647use schemars::{Schema, schema_for};
648use secrecy::{ExposeSecret, SecretString};
649use serde::{Deserialize, Serialize};
650use serde_json::{Value, json};
651
652pub mod accounting;
653mod assets;
654pub mod change_token;
655mod expected;
656pub use expected::{CONTEXT_VERSION, WHOLE_MEMBERS, context_shapes};
657mod pacing;
658mod projection;
659mod visibility;
660
661use accounting::Accounting;
662
663/// The registry name for this plugin.
664pub const KIND: &str = "github-projects";
665/// GitHub's maximum connection page size.
666pub const MAX_PAGE_SIZE: u32 = 100;
667/// Every page of a board-scoped narrowing search: 20 rows, one point of SEARCH_ISSUES, the
668/// most one point buys. GitHub prices that document by rows, so pages of 20 cost what pages
669/// of 100 cost per row, and a page of fewer than 20 costs the same one point.
670pub const SEARCH_PAGE_SIZE: u32 = 20;
671/// How many items one [`graphql::ISSUE_DETAILS`] request reads, each with the first page of
672/// its comments: the largest batch the node-count model prices at one point.
673///
674/// Each aliased item is resolved once, and what GitHub charges for it is the connections
675/// under it — its labels, its page of board memberships, the field values of each of those
676/// three memberships, and its comments: six requests' worth of the aggregate GitHub divides
677/// by a hundred and rounds. Twenty-four items come to 144, which rounds to one point;
678/// twenty-five come to 150, which rounds to two. `tests/point_cost.rs` prices the document at
679/// one point and fails if one item more would still be priced at one.
680pub const DETAIL_BATCH: usize = 24;
681/// How many issue references one [`graphql::ISSUES_BY_REFERENCE`] request reads.
682///
683/// Each reference asks GitHub for one connection — a page of its board memberships, each with
684/// its `Status` value read by name rather than through a connection of field values — so fifty
685/// come to fifty requests' worth of the aggregate GitHub divides by a hundred, which is its
686/// one-point minimum, and the batch a landing's closures are read in costs one point per fifty
687/// issues per source. `tests/point_cost.rs` prices the document at one point.
688pub const REFERENCE_BATCH: usize = 50;
689/// How many items one [`graphql::ISSUE_COPY_DETAILS`] request reads: each with the first page
690/// of its comments and its `blockedBy`, the largest batch the node-count model prices at one
691/// point.
692///
693/// The six requests' worth an item of [`DETAIL_BATCH`] costs, and its `blockedBy` connection
694/// besides: seven. Twenty-one items come to 147, which rounds to one point; twenty-two come to
695/// 154, which rounds to two. `tests/point_cost.rs` prices the document at one point and fails
696/// if one item more would still be priced at one.
697pub const COPY_DETAIL_BATCH: usize = 21;
698/// How many mutations one aliased request of a copy's creates, closes and updates carries:
699/// [`graphql::CREATE_ISSUES`], [`graphql::CLOSE_ISSUES`] and [`graphql::UPDATE_ISSUES`].
700///
701/// Eleven is what GitHub was seen to accept in one request — the timing spike
702/// (`board-copy-speed-2026-10-18-spike-copy-timing-report`) sent eleven aliased mutations of four
703/// kinds, three `createIssue`, three `addProjectV2ItemById`, three field updates, an
704/// `addSubIssue` and an `addBlockedBy`, and every one ran — and no more was established.
705pub const MUTATION_BATCH: usize = 11;
706/// How many aliased writes one request of a copy's board field values and blocked-by edges
707/// carries: [`graphql::WRITE_FIELD_VALUES`], [`graphql::ADD_BLOCKED_BYS`] and, for a request
708/// holding both kinds, [`graphql::WRITE_FIELDS_AND_EDGES`].
709///
710/// Nineteen is the most aliases the feasibility spike
711/// (`board-copy-gaps-2026-10-24-spike-feasibility-report`) sent in one mutation request and
712/// saw GitHub price at the one point every mutation request costs; no larger request was
713/// established, so a copy holding more is split.
714pub const WRITE_BATCH: usize = 19;
715/// How many issues one [`graphql::DELETE_ISSUES`] request deletes: one request of eleven
716/// aliased `deleteIssue` met `RESOURCE_LIMITS_EXCEEDED` from the eighth alias on, and batches
717/// of five succeeded (the feasibility spike's undo-batching run).
718pub const DELETE_BATCH: usize = 5;
719/// How many origins one [`graphql::ORIGIN_LOOKUPS`] request asks about: the nine records of a
720/// plan and the project's document, and no more, since each carries a page of the board's field
721/// filter and one of the issue search.
722pub const ORIGIN_BATCH: usize = 10;
723
724/// The most nodes any one document this source sends may be asked to return.
725///
726/// GitHub's own published per-query ceiling, taken from
727/// [`github_graphql_node_count::NODE_LIMIT`] rather than written out again here, so this
728/// workspace cannot hold a stale copy of somebody else's number. A query above it is
729/// **refused before it is executed**, whoever is asking and whatever board they are
730/// asking about — so this is a bound on the documents rather than a budget that runs out.
731///
732/// This is `nodeCount`, the maximum number of nodes *one query may return*. It is not
733/// `cost`, the rate-limit points a call spends against an hourly allowance shared by
734/// everything the credential does — two numbers against two limits, and this constant
735/// bounds only the first. The second is computed offline too, per document:
736/// [`worst_case_point_cost`], pinned for every document in [`graphql::DOCUMENTS`] by
737/// `tests/point_cost.rs`, and reconciled against GitHub's own `cost` by the credentialed
738/// lane. There is no constant like this one to hold a price under, because points are an
739/// hourly allowance rather than a per-call bound.
740///
741/// Neither is a session's price. What `session-cost.md` records of a whole session is its
742/// **requests** and its **worst-case nodes**; what a whole session spends in points is
743/// reported only by a credentialed run's own `x-ratelimit-*` headers, through
744/// [`accounting`]. The module section on the three ways this source reaches an item says how
745/// the count is arrived at, and which of the page sizes below decide it.
746pub const NODE_COUNT_LIMIT: u64 = github_graphql_node_count::NODE_LIMIT;
747
748/// Nested connection size for the connections that hang off one item.
749///
750/// It multiplies through every document that reaches an item under a page — the count
751/// rules multiply down a nested path — so it is the constant [`NODE_COUNT_LIMIT`] is most
752/// sensitive to. `tests/node_count.rs` is what holds the pair together: it recomputes
753/// every document under these constants and fails naming any that reaches the limit, so
754/// raising this is caught there rather than by GitHub.
755const NESTED_PAGE_SIZE: u32 = 50;
756/// How many of one issue's board memberships are read when an issue is reached directly.
757///
758/// An issue reached through a search or through its own node id carries its board half in
759/// `Issue.projectItems`, and only the entry for *this* board is read. This connection sits
760/// under a page of issues, so every point of it multiplies through the whole document and
761/// is paid for whether or not any issue is on a second board — which is why it is
762/// deliberately far smaller than [`NESTED_PAGE_SIZE`].
763///
764/// **Three, because what a page misses is now recovered rather than refused**, and the
765/// recovery is what the value is chosen against. An issue whose entry for this board sits
766/// past this page costs one further request — [`graphql::ISSUE_BOARD_ITEMS`], resumed from
767/// that page's own cursor — so the value trades a bound every read pays for a request only
768/// a multi-board issue pays. At one, a deployment whose issues commonly sit on two or more
769/// boards would pay that request *per issue*, which is order N against the one page per
770/// hundred issues a read costs today. At three it is only reached by an issue on four or
771/// more boards at once, which keeps the recovery path exceptional rather than routine for
772/// a plausible deployment.
773const BOARD_ITEMS_PAGE_SIZE: u32 = 3;
774/// How many carriers of one copy origin one page of [`graphql::ORIGIN_LOOKUP`] asks each of
775/// its two connections for.
776///
777/// An origin names one item, so the answer an origin lookup expects is one carrier, and a
778/// second is a duplicate a copy already takes the first of. Both connections are walked to
779/// exhaustion whatever this is, so it decides how many requests an unusual answer costs and
780/// never what the answer is. It is small because every point of it is paid on every lookup,
781/// and a copy makes one lookup per item it has no link for: at three, ten lookups cost fewer
782/// worst-case nodes than the one whole-board read they replaced.
783const ORIGIN_PAGE_SIZE: u32 = 3;
784/// How many carriers of each origin the batched [`graphql::ORIGIN_LOOKUPS`] asks each of its two
785/// connections for: one, the one carrier an origin names.
786///
787/// A second carrier still shows: the page then says there is more, and that origin is left to
788/// its own [`graphql::ORIGIN_LOOKUP`], which walks every page at `ORIGIN_PAGE_SIZE` — so a
789/// duplicate is found exactly as before, by the lookup that reads all of them. The feasibility
790/// spike (`board-copy-gaps-2026-10-24-spike-feasibility-report`) priced the batch of ten
791/// origins at two points with a first page of three and at one with a first page of one.
792const ORIGIN_LOOKUPS_PAGE_SIZE: u32 = 1;
793
794/// How many of one project's sub-issues one [`graphql::SUB_ISSUES`] page asks for: the most
795/// one point buys, since every row carries six connections GitHub charges for once per row.
796/// `session-cost.md` derives it; `tests/point_cost.rs` prices the document at one point and
797/// fails if one row more would still be priced at one.
798pub const MEMBER_PAGE_SIZE: u32 = 23;
799
800pub use github_graphql_node_count::{NodeCountError, Variables};
801
802/// The largest value this source can bind to each page-size variable its documents name.
803///
804/// Every `first:` in [`graphql`] reads one of these seven, and each is capped at the
805/// constant above it wherever a caller's own limit could reach it — `$first` at
806/// [`MAX_PAGE_SIZE`], `$commentFirst` at [`MAX_PAGE_SIZE`], `$nestedFirst` at `NESTED_PAGE_SIZE`, `$boardItems` at
807/// `BOARD_ITEMS_PAGE_SIZE`, `$originFirst` at `ORIGIN_PAGE_SIZE`, `$lookupFirst` at
808/// `ORIGIN_LOOKUPS_PAGE_SIZE`, `$memberFirst` at [`MEMBER_PAGE_SIZE`]. So this is the worst case a caller can drive this source to,
809/// not one configuration of it, which is what makes a bound computed under it a bound on
810/// every read.
811pub fn largest_page_sizes() -> Variables {
812 Variables::from([
813 ("first".to_owned(), MAX_PAGE_SIZE),
814 ("commentFirst".to_owned(), MAX_PAGE_SIZE),
815 ("nestedFirst".to_owned(), NESTED_PAGE_SIZE),
816 ("boardItems".to_owned(), BOARD_ITEMS_PAGE_SIZE),
817 ("originFirst".to_owned(), ORIGIN_PAGE_SIZE),
818 ("lookupFirst".to_owned(), ORIGIN_LOOKUPS_PAGE_SIZE),
819 ("memberFirst".to_owned(), MEMBER_PAGE_SIZE),
820 ])
821}
822
823/// The most nodes `document` could be asked to return, by GitHub's published rules.
824///
825/// Computed offline from the document's own text under [`largest_page_sizes`] — no
826/// network, no credential and no schema — by
827/// [`github_graphql_node_count::node_count`], which is where the rules themselves live.
828/// A document at or above [`NODE_COUNT_LIMIT`] is one GitHub refuses before executing, so
829/// this is what a check holds every document in [`graphql::DOCUMENTS`] below.
830///
831/// # Errors
832///
833/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
834/// no single operation, or binds a page size this source does not name — each of which is
835/// a defect in the document rather than a number.
836pub fn worst_case_node_count(document: &str) -> Result<u64, NodeCountError> {
837 node_count(document, &largest_page_sizes())
838}
839
840/// The most rate-limit points one call of `document` could spend, by GitHub's published
841/// rules.
842///
843/// Computed offline from the document's own text under [`largest_page_sizes`] — no
844/// network, no credential and no schema — by
845/// [`github_graphql_node_count::point_cost`], which is where the rules themselves live.
846/// This is `cost`, metered **per hour** against the allowance one credential shares across
847/// everything it does; it is not `nodeCount`, which is [`worst_case_node_count`] and is
848/// bounded per query by [`NODE_COUNT_LIMIT`]. There is no per-call ceiling to hold this
849/// under, so what `tests/point_cost.rs` does with it is pin every document in
850/// [`graphql::DOCUMENTS`] at what it costs, and the credentialed lane reconciles those
851/// figures against GitHub's own reported `cost`.
852///
853/// # Errors
854///
855/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
856/// no single operation, or binds a page size this source does not name — each of which is
857/// a defect in the document rather than a number.
858pub fn worst_case_point_cost(document: &str) -> Result<u64, NodeCountError> {
859 github_graphql_node_count::point_cost(document, &largest_page_sizes())
860}
861
862/// The most nodes `document` could be asked to return under `variables`.
863///
864/// [`worst_case_node_count`] is this under [`largest_page_sizes`], and the accounting in
865/// [`accounting`] is this under the bindings one request really sent — one spelling of the
866/// calculation, so a bound checked offline and a cost recorded at run time cannot come to
867/// disagree. The rules themselves live in [`github_graphql_node_count::node_count`].
868///
869/// # Errors
870///
871/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
872/// no single operation, or binds a page size `variables` does not name.
873pub fn node_count(document: &str, variables: &Variables) -> Result<u64, NodeCountError> {
874 github_graphql_node_count::node_count(document, variables)
875}
876
877/// The issue-title prefix that makes a board issue a document.
878///
879/// A GitHub Projects board has no document type — it holds issues — so the discriminator
880/// is the title, and this is the whole of it: an issue whose title begins with these bytes
881/// is a document and every other issue is the task or project the sub-issue rule makes it.
882///
883/// It is spelled **once**, here, and read rather than restated everywhere else — including
884/// by the shared journeys, which take it from this constant so a board fixture cannot
885/// drift from what this source reads. `docs/metadata.md` records the two consequences that
886/// are not obvious from the bytes: the reported title has this prefix taken off, exactly
887/// as the body's metadata slot is taken off `content`, and this prefix is read *before*
888/// the sub-issue rule, so a design issue with no sub-issues is never an empty project.
889pub const DESIGN_TITLE_PREFIX: &str = "DESIGN: ";
890
891/// Exact GraphQL query documents issued by this plugin.
892///
893/// Keeping the production documents here lets the pinned-schema test validate the same
894/// bytes that are sent to GitHub, rather than a test-only copy which could drift
895/// independently. [`STATUS_OPTIONS_UPDATE`] is the sole document that may rewrite a board
896/// field, and its guarded caller always supplies the complete existing option set with ids.
897pub mod graphql {
898 /// The board half of one item: the field values every document here reads it from.
899 ///
900 /// A macro for the same reason [`board_issue!`] below is one, a level further in. This
901 /// selection is needed by that fragment, by [`BOARD`] under the board's own `items`,
902 /// and by [`ISSUE_BOARD_ITEMS`] under a membership walk — and all three have to produce
903 /// *the same value*, because
904 /// [`GitHubProjectsSource::resolve`](super::GitHubProjectsSource) reads them through
905 /// one path. Three spellings of it is what would drift, so there is one.
906 ///
907 /// The `Status` option and this source's own origin text field are the whole of it. It
908 /// selects no `ProjectV2ItemFieldLabelValue`: GitHub derives that field from the item's
909 /// content, so it holds nothing the content's own `labels` do not already say, and it
910 /// would sit a label connection two page sizes deep.
911 macro_rules! board_item_values {
912 () => {
913 r#"fieldValues(first:$nestedFirst){nodes{
914 ... on ProjectV2ItemFieldSingleSelectValue{name field{
915 ... on ProjectV2SingleSelectField{id name options{id name}}
916 }}
917 ... on ProjectV2ItemFieldTextValue{text field{... on ProjectV2Field{id name}}}
918 }pageInfo{hasNextPage}}"#
919 };
920 }
921
922 /// The variable every document reading an issue declares for the relationship events its
923 /// change token counts, with its value as the default, so no caller binds it.
924 ///
925 /// A variable rather than a literal list because every argument a production document
926 /// passes is a declared variable, which `tests/schema.rs` holds them to, and every member of
927 /// its default to the pinned enum. Its default is the six event types GitHub records a
928 /// relationship added or removed with: a dependency on either end, as `blockedBy` and
929 /// `blocking`, so one a person makes from the far end moves this end's count too; and a
930 /// project membership on the task, as its parent, which GitHub records there whichever
931 /// issue the move was made from. A sub-issue's events on its parent are not counted: a
932 /// project carries no task token, and a copy writes nothing of a task its own sub-issues
933 /// decide. [`super::change_token`] says why the count is in the token.
934 macro_rules! relationship_events_variable {
935 () => {
936 ",$relationshipEvents:[IssueTimelineItemsItemType!]=[BLOCKED_BY_ADDED_EVENT,BLOCKED_BY_REMOVED_EVENT,BLOCKING_ADDED_EVENT,BLOCKING_REMOVED_EVENT,PARENT_ISSUE_ADDED_EVENT,PARENT_ISSUE_REMOVED_EVENT]"
937 };
938 }
939
940 /// Everything this source reads about one issue, wherever it reaches that issue.
941 ///
942 /// A macro rather than a constant so the three documents below can `concat!` it: one
943 /// spelling of these fields is what makes an issue read through the board-scoped
944 /// search, through its own node id, and through its project's sub-issue relationship
945 /// resolve to *the same* item, which is the whole of what
946 /// [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource) relies on.
947 ///
948 /// `projectItems` is what carries the board half of an issue: the board item's own id
949 /// and the [`board_item_values!`] above — the `Status` option and this source's origin
950 /// text field — that a `ProjectV2.items` read used to carry. It is asked for on the
951 /// issue rather than on the board, which is what makes the cost of a read proportional
952 /// to what was asked for instead of to the board's size.
953 ///
954 /// It carries a *page* of that connection, at `BOARD_ITEMS_PAGE_SIZE`, and its
955 /// `endCursor` is what [`ISSUE_BOARD_ITEMS`] resumes from when this board's entry is
956 /// not on that page: a page here is where the search for the entry starts rather than
957 /// where it ends.
958 ///
959 /// It does **not** select the board's `Labels` field value, and that is the whole of
960 /// what keeps the three documents below under [`NODE_COUNT_LIMIT`](super::NODE_COUNT_LIMIT):
961 /// a label connection there sits under `fieldValues` under `projectItems` under a page
962 /// of issues, spending `$nestedFirst` twice down one path, and took
963 /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] to 2,556,100 nodes against a limit of 500,000.
964 /// No label is lost — this is a fragment `on Issue`, whose own `labels` are selected
965 /// above, and that connection is where every label this source reports comes from. No
966 /// document in this module selects the board field any longer, [`BOARD`] included; the
967 /// module documentation records why nothing it could have held is lost.
968 macro_rules! board_issue {
969 () => {
970 concat!(
971 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}
972 labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}
973 projectItems(first:$boardItems){nodes{id updatedAt project{id number}
974 "#,
975 board_item_values!(),
976 r#"}pageInfo{hasNextPage endCursor}}}"#
977 )
978 };
979 }
980
981 /// One page of a repository's issues, newest first, each with when it was created and its
982 /// body: what a retried copy reads to find the issue its earlier attempt created, by the
983 /// create key in that body's metadata slot — never through search, whose index lags.
984 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}}}}";
985 /// Every issue of one board, found by a search scoped to that board.
986 ///
987 /// This is how the projects a board holds are listed, and it selects no `items`
988 /// connection on `ProjectV2`: the board is a *qualifier of the search* rather than a
989 /// container walked page by page, so nothing nested inside a board item is paid for.
990 /// Which of the issues it returns is a project is then read off `parent` — GitHub
991 /// accepts `-has:parent` as a search qualifier and silently ignores it, so the
992 /// discriminator has to be applied to the field, which is a scalar on the issue and
993 /// costs nothing.
994 pub const SEARCH_ISSUES: &str = concat!(
995 r#"query($search:String!,$type:SearchType!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
996 relationship_events_variable!(),
997 r#"){
998 search(query:$search,type:$type,first:$first,after:$after){
999 pageInfo{hasNextPage endCursor}
1000 nodes{__typename ...BoardIssue}
1001 }
1002 }"#,
1003 board_issue!()
1004 );
1005
1006 /// What a dependency read selects of each far end: enough to say which kind of item it
1007 /// is, its body included for the kind marker.
1008 macro_rules! related_issue {
1009 () => {
1010 " fragment Related on Issue{id title body parent{id} subIssuesSummary{total}}"
1011 };
1012 }
1013
1014 /// One issue by its own node id, which is what a qualified id names here — with what a
1015 /// write of it needs and the issue does not carry in `board_issue!`: the field
1016 /// definitions of the boards it sits on, and the far ends of its `blockedBy`.
1017 ///
1018 /// Strongly consistent, unlike the search above: GitHub's issue search is an index and
1019 /// answers a write made moments ago with the value from before it, and resolving a node
1020 /// id does not.
1021 ///
1022 /// **Why those two ride here and not on the fragment.** A copy or an update of an item
1023 /// reads it by its own id, and with them that one read answers everything the write
1024 /// needs: which option ids the board's `Status` and `Priority` fields hold — so no
1025 /// [`BOARD_FIELDS`] — and which issues block it, with each one's kind — so no
1026 /// [`ISSUE_DEPENDENCIES`]. On `board_issue!` they would sit under the hundred-issue
1027 /// pages of [`SEARCH_ISSUES`] and [`SUB_ISSUES`], multiplying both documents' price. Here
1028 /// they sit under one item, and this read is still one point.
1029 pub const ISSUE: &str = concat!(
1030 r#"query($id:ID!,$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1031 relationship_events_variable!(),
1032 r#"){
1033 node(id:$id){__typename ...BoardIssue ... on Issue{
1034 boards:projectItems(first:$boardItems){nodes{project{id number fields(first:$nestedFirst){nodes{
1035 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1036 ... on ProjectV2Field{__typename id name dataType}
1037 }pageInfo{hasNextPage}}}}}
1038 blockedBy(first:$first){nodes{...Related}pageInfo{hasNextPage endCursor}}
1039 }}
1040 }"#,
1041 board_issue!(),
1042 related_issue!()
1043 );
1044
1045 /// One document by its own node id, as [`ISSUE`] reads one, with the project it is filed
1046 /// under and that project's members beside it: the project issue whole, and a page of its
1047 /// sub-issues, each as [`SUB_ISSUES`] carries one but with no page of what blocks it.
1048 ///
1049 /// What a guarded write of a document's metadata reads, and what reading a document by its
1050 /// id is: a write to a board the boundary screens is held to who may read the document,
1051 /// which is the project's and every member's classification as much as its own, and here
1052 /// the one request that holds the version the write is compared against answers all of it.
1053 /// Its members carry no edges, which nothing classifying them reads.
1054 pub const DOCUMENT_IN_PROJECT: &str = concat!(
1055 r#"query($id:ID!,$first:Int!,$memberFirst:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1056 relationship_events_variable!(),
1057 r#"){
1058 node(id:$id){__typename ...BoardIssue ... on Issue{
1059 boards:projectItems(first:$boardItems){nodes{project{id number fields(first:$nestedFirst){nodes{
1060 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1061 ... on ProjectV2Field{__typename id name dataType}
1062 }pageInfo{hasNextPage}}}}}
1063 blockedBy(first:$first){nodes{...Related}pageInfo{hasNextPage endCursor}}
1064 project:parent{__typename ...BoardIssue
1065 subIssues(first:$memberFirst){pageInfo{hasNextPage endCursor} nodes{__typename ...BoardIssue}}
1066 }
1067 }}
1068 }"#,
1069 board_issue!(),
1070 related_issue!()
1071 );
1072
1073 /// One project's tasks: the sub-issues of the issue that project is, each with a page of
1074 /// what blocks it.
1075 ///
1076 /// Paged at `$memberFirst` — [`MEMBER_PAGE_SIZE`](super::MEMBER_PAGE_SIZE), the most rows
1077 /// one point buys — rather than at `$first`, because every row carries six connections and
1078 /// a hundred-row page cost six points to list a project of ten.
1079 ///
1080 /// The work this costs is the project's own size. Nothing about it grows as the board
1081 /// gains projects, or as those projects gain tasks.
1082 ///
1083 /// **Why `blockedBy` rides here and on no other page of issues.** What reads a project's
1084 /// tasks reads their edges next — `project graph` draws them, a copy carries them — and
1085 /// without them here that is one [`ISSUE_DEPENDENCIES`] per task, so the requests a graph
1086 /// costs grow with its tasks rather than with the pages of them. Carried here, a task
1087 /// blocked by no more than `$nestedFirst` issues answers its forward edges from this read,
1088 /// exactly as an [`ISSUE`] read of it does, and only one blocked by more is asked again.
1089 /// It adds a connection under each issue of the page — one rate-limit point per page,
1090 /// and `$nestedFirst` nodes per issue — and is kept off [`SEARCH_ISSUES`], whose pages
1091 /// answer questions that never read an edge.
1092 ///
1093 /// **The project issue itself rides here too**, with the page of what blocks it, because
1094 /// the reader of a project's tasks reads the project next: a copy writing the project reads
1095 /// it and its edges before it writes them. Carried on the request that already names the
1096 /// project, the first page answers that read and the dependency read after it, at the
1097 /// state GitHub answered in this command, rather than spending one more read on an item
1098 /// the cache's token cannot vouch for whole. Each sub-issue is kept the same way, so a
1099 /// copy's read of a task this listing carried is answered by it rather than sent again.
1100 /// Only a copy's read-before-write takes them; every other read by id reads GitHub.
1101 pub const SUB_ISSUES: &str = concat!(
1102 r#"query($id:ID!,$memberFirst:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1103 relationship_events_variable!(),
1104 r#"){
1105 node(id:$id){__typename ...BoardIssue
1106 ... on Issue{blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}
1107 subIssues(first:$memberFirst,after:$after){
1108 pageInfo{hasNextPage endCursor}
1109 nodes{__typename ...BoardIssue ... on Issue{
1110 blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}
1111 }}
1112 }}}
1113 }"#,
1114 board_issue!(),
1115 related_issue!()
1116 );
1117
1118 /// Everything this source reads about one issue comment, wherever it reaches one.
1119 ///
1120 /// A macro for the reason [`board_issue!`] is one: a comment listed, a comment just added
1121 /// and a comment just edited are handed to one mapper, so they are selected by one
1122 /// spelling. `author` is `Actor`, which GitHub answers `null` for an account that no
1123 /// longer exists, and `login` is the one member every kind of actor carries.
1124 macro_rules! issue_comment {
1125 () => {
1126 "id author{login} createdAt updatedAt body url"
1127 };
1128 }
1129
1130 /// What a read of the board's own `items` selects of each item's content.
1131 ///
1132 /// A macro for the reason [`board_item_values!`] is one: [`BOARD`] and [`ORIGIN_LOOKUP`]
1133 /// both walk `ProjectV2.items` and hand each item to one resolver, so they select its
1134 /// content by one spelling.
1135 macro_rules! board_item_content {
1136 () => { board_item_content!("") };
1137 ($comments:expr) => {
1138 concat!(r#" content{
1139 ... 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}}"#, $comments, r#"}
1140 ... on PullRequest{__typename id}
1141 ... on DraftIssue{__typename id title body createdAt updatedAt}
1142 }"#)
1143 };
1144 }
1145
1146 macro_rules! board_listing {
1147 ($variables:expr, $comments:expr) => { concat!(
1148 r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$nestedFirst:Int!,$duplicates:Boolean!"#,
1149 relationship_events_variable!(), $variables,
1150 r#"){
1151 owner:repositoryOwner(login:$owner){
1152 ... on ProjectV2Owner{projectV2(number:$number){...Board}}
1153 }
1154 } fragment Board on ProjectV2 { id title
1155 fields(first:$nestedFirst){nodes{
1156 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1157 ... on ProjectV2Field{__typename id name dataType}
1158 }pageInfo{hasNextPage}}
1159 items(first:$first,after:$after){nodes{id updatedAt "#,
1160 board_item_values!(),
1161 board_item_content!($comments),
1162 r#"} pageInfo{hasNextPage endCursor}}
1163 }"#
1164 ) };
1165 }
1166
1167 /// Reads the board's fields and one page of its items.
1168 pub const BOARD: &str = board_listing!("", "");
1169
1170 /// A board page carrying each issue's first comment page in the same request.
1171 pub const BOARD_WITH_COMMENTS: &str = concat!(
1172 board_listing!(
1173 ",$commentFirst:Int!",
1174 concat!(
1175 "comments(first:$commentFirst){nodes{",
1176 issue_comment!(),
1177 "}pageInfo{hasNextPage endCursor}} blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}"
1178 )
1179 ),
1180 related_issue!()
1181 );
1182
1183 /// Every carrier of one copy origin, by two reads in one request, and nothing else of
1184 /// the board.
1185 ///
1186 /// **`originItems`** is the board's own items narrowed by its own field filter —
1187 /// `ProjectV2.items(query:)`, which GitHub's schema declares as "Search query for
1188 /// filtering items" — to those whose `onetaskgraph.origin` text field holds the
1189 /// qualified id, quoted. It reads the field every carrier already holds, whichever release
1190 /// wrote it, and matches it exactly: measured on 2026-09-29 against a 394-item board,
1191 /// the quoted, the unquoted and the bare-value spellings each returned exactly the one
1192 /// carrier and a prefix of the value returned none. It is `ProjectV2.items`, so it lags a
1193 /// fresh `addProjectV2ItemById` the way that connection does.
1194 ///
1195 /// **`search`** is the board-scoped issue search for the same id as a quoted phrase in
1196 /// the body, which is where this source mirrors the origin into its metadata slot. GitHub
1197 /// indexes that comment, and the index catches up with a write in a second or two rather
1198 /// than in minutes, so it finds a carrier another process wrote that the first read is
1199 /// still behind on.
1200 ///
1201 /// Each connection pages at `$originFirst`, its own small size — see `ORIGIN_PAGE_SIZE`
1202 /// — and resumes from its own cursor; a connection already walked to its end is resumed
1203 /// from its last cursor, which answers an empty page. Every candidate either read returns
1204 /// is confirmed against its own origin field before it is reported, so a token match of
1205 /// the search or anything else the filter admits never is.
1206 ///
1207 /// The root is aliased `originItems` rather than `owner`, so nothing counting the board's
1208 /// own whole reads counts this one among them.
1209 pub const ORIGIN_LOOKUP: &str = concat!(
1210 r#"query($owner:String!,$number:Int!,$filter:String!,$search:String!,$type:SearchType!,$originFirst:Int!,$itemsAfter:String,$searchAfter:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1211 relationship_events_variable!(),
1212 r#"){
1213 originItems:repositoryOwner(login:$owner){
1214 ... on ProjectV2Owner{projectV2(number:$number){
1215 items(first:$originFirst,after:$itemsAfter,query:$filter){nodes{id updatedAt "#,
1216 board_item_values!(),
1217 board_item_content!(),
1218 r#"} pageInfo{hasNextPage endCursor}}
1219 }}
1220 }
1221 search(query:$search,type:$type,first:$originFirst,after:$searchAfter){
1222 pageInfo{hasNextPage endCursor}
1223 nodes{__typename ...BoardIssue}
1224 }
1225 }"#,
1226 board_issue!()
1227 );
1228
1229 /// Every closed item of the board, by the board's own field filter — what
1230 /// `task status reconcile` reads, and nothing else of the board.
1231 ///
1232 /// **`ProjectV2.items(query:)` narrowed by `$filter`**, which this source binds to
1233 /// `is:closed` alone, so a board whose open items are most of it is not read whole to find
1234 /// the closed ones. It is narrowed rather than trusted: every item it answers is confirmed
1235 /// closed, with a reason and a card that contradict each other, from the item node it
1236 /// already carries, so a filter that also let open items through would cost reads and
1237 /// write nothing wrong.
1238 ///
1239 /// The board's own id and field definitions ride along, as [`BOARD`] carries them, so the
1240 /// option writes that follow need no read of the board. The root is aliased `closedItems`
1241 /// rather than `owner`, so nothing counting the board's own whole reads counts this one
1242 /// among them.
1243 pub const CLOSED_ITEMS: &str = concat!(
1244 r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$filter:String!,$nestedFirst:Int!,$duplicates:Boolean!"#,
1245 relationship_events_variable!(),
1246 r#"){
1247 closedItems:repositoryOwner(login:$owner){
1248 ... on ProjectV2Owner{projectV2(number:$number){id
1249 fields(first:$nestedFirst){nodes{
1250 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1251 ... on ProjectV2Field{__typename id name dataType}
1252 }pageInfo{hasNextPage}}
1253 items(first:$first,after:$after,query:$filter){nodes{id updatedAt "#,
1254 board_item_values!(),
1255 board_item_content!(),
1256 r#"} pageInfo{hasNextPage endCursor}}
1257 }}
1258 }
1259 }"#
1260 );
1261
1262 /// One alias of [`ISSUES_BY_REFERENCE`]: the issue `$owner<n>/$name<n>#$number<n>` names.
1263 macro_rules! reference_alias {
1264 ($n:literal) => {
1265 concat!(
1266 "\n r",
1267 stringify!($n),
1268 ":repository(owner:$owner",
1269 stringify!($n),
1270 ",name:$name",
1271 stringify!($n),
1272 "){issue(number:$number",
1273 stringify!($n),
1274 "){...HeldIssue}}"
1275 )
1276 };
1277 }
1278
1279 /// [`REFERENCE_BATCH`](super::REFERENCE_BATCH) issues at once, each by its repository and
1280 /// number — the one read `task status complete` makes of a source, and nothing else of
1281 /// the board: no `items`, no search and no project read.
1282 ///
1283 /// **Its own fragment, lighter than `board_issue!`, and that is what makes fifty
1284 /// references one point.** It selects no labels, and of each board membership only the
1285 /// `Status` value, by name, with `fieldValueByName` — so each alias asks GitHub for one
1286 /// connection, its page of memberships, where the whole fragment asks for five. What a
1287 /// completion writes is a card and, at most, the issue's state, so the labels and the other
1288 /// field values it leaves out are never what it needs; an item read this way is held for
1289 /// that write alone and never answers another read. An issue whose entry for this board is
1290 /// past its first page of memberships is walked with [`ISSUE_BOARD_ITEMS`], exactly as
1291 /// every other read of an issue recovers one.
1292 ///
1293 /// Fixed-size, as [`ISSUE_DETAILS`] is, so there is one document to price: a batch of
1294 /// fewer references binds the slots it has none for to its last one.
1295 pub const ISSUES_BY_REFERENCE: &str = concat!(
1296 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!"#,
1297 relationship_events_variable!(),
1298 r#"){"#,
1299 reference_alias!(0),
1300 reference_alias!(1),
1301 reference_alias!(2),
1302 reference_alias!(3),
1303 reference_alias!(4),
1304 reference_alias!(5),
1305 reference_alias!(6),
1306 reference_alias!(7),
1307 reference_alias!(8),
1308 reference_alias!(9),
1309 reference_alias!(10),
1310 reference_alias!(11),
1311 reference_alias!(12),
1312 reference_alias!(13),
1313 reference_alias!(14),
1314 reference_alias!(15),
1315 reference_alias!(16),
1316 reference_alias!(17),
1317 reference_alias!(18),
1318 reference_alias!(19),
1319 reference_alias!(20),
1320 reference_alias!(21),
1321 reference_alias!(22),
1322 reference_alias!(23),
1323 reference_alias!(24),
1324 reference_alias!(25),
1325 reference_alias!(26),
1326 reference_alias!(27),
1327 reference_alias!(28),
1328 reference_alias!(29),
1329 reference_alias!(30),
1330 reference_alias!(31),
1331 reference_alias!(32),
1332 reference_alias!(33),
1333 reference_alias!(34),
1334 reference_alias!(35),
1335 reference_alias!(36),
1336 reference_alias!(37),
1337 reference_alias!(38),
1338 reference_alias!(39),
1339 reference_alias!(40),
1340 reference_alias!(41),
1341 reference_alias!(42),
1342 reference_alias!(43),
1343 reference_alias!(44),
1344 reference_alias!(45),
1345 reference_alias!(46),
1346 reference_alias!(47),
1347 reference_alias!(48),
1348 reference_alias!(49),
1349 "\n }",
1350 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}
1351 projectItems(first:$boardItems){nodes{id updatedAt project{id number}
1352 status:fieldValueByName(name:$statusField){... on ProjectV2ItemFieldSingleSelectValue{name field{
1353 ... on ProjectV2SingleSelectField{id name options{id name}}
1354 }}}
1355 }pageInfo{hasNextPage endCursor}}}"#
1356 );
1357
1358 /// The board's own id and field definitions, and not one of its items.
1359 ///
1360 /// What a write needs of the board when the item it writes does not say: the id a field
1361 /// write and `addProjectV2ItemById` address, and the definitions of the `Status` and
1362 /// origin fields. It selects no `items`, so what it costs is the board's field list
1363 /// however many items the board holds — and it decides nothing about which items those
1364 /// are, which is the question a read of one item by its own id answers instead.
1365 ///
1366 /// The root is aliased `boardFields` rather than `owner`, so nothing counting the
1367 /// board's item reads by their root counts this one among them.
1368 pub const BOARD_FIELDS: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!){
1369 boardFields:repositoryOwner(login:$owner){
1370 ... on ProjectV2Owner{projectV2(number:$number){id
1371 fields(first:$nestedFirst){nodes{
1372 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1373 ... on ProjectV2Field{__typename id name dataType}
1374 }pageInfo{hasNextPage}}
1375 }}
1376 }
1377 }"#;
1378
1379 /// One board draft by its own node id, with the board item it sits in.
1380 ///
1381 /// A draft is not an issue, so [`ISSUE`] reaches it and reads nothing of it; this is the
1382 /// second read that answers it. `DraftIssue.projectV2Items` names the board item a draft
1383 /// is — GitHub links a draft to one item — with the same [`board_item_values!`] the
1384 /// issue fragment reads, so a draft reached by id resolves through the same resolver a
1385 /// board listing hands it to, and nothing has to list the board to find one.
1386 pub const DRAFT: &str = concat!(
1387 r#"query($id:ID!,$nestedFirst:Int!,$boardItems:Int!){
1388 node(id:$id){__typename ... on DraftIssue{id title body createdAt updatedAt
1389 projectV2Items(first:$boardItems){nodes{id project{id number}
1390 "#,
1391 board_item_values!(),
1392 r#"}pageInfo{hasNextPage endCursor}}}}
1393 }"#
1394 );
1395
1396 /// One issue's board memberships alone, walked past the page a read of it carried.
1397 ///
1398 /// The recovery read behind [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource):
1399 /// every document above carries a *page* of `Issue.projectItems`, and an issue on more
1400 /// boards than that page holds may have this board's entry past its end. This asks that
1401 /// one issue for its memberships and nothing else — the caller already holds the issue —
1402 /// so an answer of "this board does not hold it" is only ever given about a connection
1403 /// read to exhaustion.
1404 ///
1405 /// It selects the board item's id, its project number and the same
1406 /// [`board_item_values!`] the fragment does, because what it produces is handed to the
1407 /// very same resolver: an issue recovered this way reports the same title, the same
1408 /// status, the same labels and the same qualified id as one whose entry was on the
1409 /// page.
1410 ///
1411 /// `$first` rather than `$boardItems`: this document reads one issue, so nothing
1412 /// multiplies through it and the membership connection can be walked at
1413 /// [`MAX_PAGE_SIZE`](super::MAX_PAGE_SIZE) — which is what keeps the recovery to one
1414 /// further request for any issue a person really keeps.
1415 pub const ISSUE_BOARD_ITEMS: &str = concat!(
1416 r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!){
1417 node(id:$id){
1418 ... on Issue{projectItems(first:$first,after:$after){
1419 nodes{id updatedAt project{id number}
1420 "#,
1421 board_item_values!(),
1422 r#"}
1423 pageInfo{hasNextPage endCursor}}}
1424 }
1425 }"#
1426 );
1427 /// Whether the board's Project is public — half of what decides whether a write here can
1428 /// be read by anybody. Needs the `read:project` scope.
1429 pub const PROJECT_VISIBILITY: &str = r#"query($owner:String!,$number:Int!){visibility:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){public}}}}"#;
1430 /// Resolves the configured repository's node id, which creating an issue requires.
1431 pub const REPOSITORY: &str = r#"query($owner:String!,$name:String!){repository(owner:$owner,name:$name){id nameWithOwner}}"#;
1432 /// What creating an issue needs and has not read yet: the board's own id and field
1433 /// definitions, as [`BOARD_FIELDS`] reads them, and the node id of the repository the
1434 /// issue is created in, as [`REPOSITORY`] reads it — in one request.
1435 ///
1436 /// Sent at the point a create knows which repository it is for, when neither half is
1437 /// already known to this process; a create needing only one of them sends that one's own
1438 /// document. Neither half is kept past the process: a field's option ids are re-minted by
1439 /// `sources fields --apply`, so a copy of them held between runs would write the wrong
1440 /// status.
1441 pub const CREATION_CONTEXT: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!,$repositoryOwner:String!,$repositoryName:String!){
1442 boardFields:repositoryOwner(login:$owner){
1443 ... on ProjectV2Owner{projectV2(number:$number){id
1444 fields(first:$nestedFirst){nodes{
1445 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1446 ... on ProjectV2Field{__typename id name dataType}
1447 }pageInfo{hasNextPage}}
1448 }}
1449 }
1450 repository(owner:$repositoryOwner,name:$repositoryName){id nameWithOwner}
1451 }"#;
1452 /// Reads both dependency directions for one issue, with each far end's own kind — and
1453 /// the issue's own body, which is where an edge to another source is recorded, so that
1454 /// half of a dependency read needs no second read of the issue or of the board.
1455 pub const ISSUE_DEPENDENCIES: &str = concat!(
1456 r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
1457 ... on Issue{body
1458 blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1459 blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1460 }}}"#,
1461 related_issue!()
1462 );
1463 /// What one item depends on: [`ISSUE_DEPENDENCIES`] narrowed to `blockedBy`, so a
1464 /// dependency read asks GitHub for the one direction it answers. The issue's `body` rides
1465 /// along because its metadata slot is where an edge to another source is recorded — and a
1466 /// board draft's `body` too, with the one board item GitHub links a draft to and, aliased
1467 /// `board`, this board's own id, so a draft's recorded edges are read off this same answer
1468 /// and only when that item is this board's: no draft read and no board read follow it.
1469 ///
1470 /// The draft's board item is asked for under a root of its own, aliased `draft`, rather
1471 /// than beside its `body` under `node`. GitHub counts the type-conditional fragments of
1472 /// one selection as the largest of them — `blockedBy`'s 100 alone, where this workspace's
1473 /// calculation adds the draft's 3 — while separate roots are summed by both, so this is
1474 /// the spelling whose node count GitHub and the calculation agree on. Both price it at
1475 /// one point; the credentialed lane's reconciliation is what holds the two to that.
1476 pub const ISSUE_DEPENDS_ON: &str = concat!(
1477 r#"query($id:ID!,$first:Int!,$after:String,$owner:String!,$number:Int!,$boardItems:Int!){
1478 node(id:$id){__typename
1479 ... on Issue{body
1480 blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1481 }
1482 ... on DraftIssue{id body}}
1483 draft:node(id:$id){... on DraftIssue{projectV2Items(first:$boardItems){nodes{project{id number}}pageInfo{hasNextPage}}}}
1484 board:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){id}}}}"#,
1485 related_issue!()
1486 );
1487 /// What depends on one issue: [`ISSUE_DEPENDENCIES`] with its `blocking` alone. No `body`:
1488 /// the reverse of a recorded edge is derived from the far end, never written down here.
1489 pub const ISSUE_DEPENDED_ON_BY: &str = concat!(
1490 r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
1491 ... on Issue{
1492 blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1493 }}}"#,
1494 related_issue!()
1495 );
1496 /// Creates one issue. Outside a copy it is created on no board, and [`ADD_TO_BOARD`] files
1497 /// it; inside one, its input names the board in `projectV2Ids` and its project in
1498 /// `parentIssueId`, so this one request files it under both — see
1499 /// [`GitHubProjectsSource::create_filed_issue`](super::GitHubProjectsSource).
1500 pub const CREATE_ISSUE: &str =
1501 r#"mutation($input:CreateIssueInput!){createIssue(input:$input){issue{id number url}}}"#;
1502 /// Puts an existing issue on the configured board.
1503 pub const ADD_TO_BOARD: &str = r#"mutation($input:AddProjectV2ItemByIdInput!){addProjectV2ItemById(input:$input){item{id}}}"#;
1504 /// Updates an issue's visible fields and its open or closed state in one call.
1505 pub const UPDATE_ISSUE: &str =
1506 r#"mutation($input:UpdateIssueInput!){updateIssue(input:$input){issue{id}}}"#;
1507 /// Updates an existing draft's user-visible fields.
1508 pub const UPDATE_DRAFT: &str = r#"mutation($input:UpdateProjectV2DraftIssueInput!){updateProjectV2DraftIssue(input:$input){draftIssue{id}}}"#;
1509 /// Updates a text or single-select value on one project item.
1510 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}}}}}}}}"#;
1511 /// Writes up to six board fields and clears up to three in one ordered mutation: every
1512 /// aliased field is included by its own boolean, as [`FIELD_WRITE_SLOTS`](super::FIELD_WRITE_SLOTS)
1513 /// and [`FIELD_CLEAR_SLOTS`](super::FIELD_CLEAR_SLOTS) name them, writes before clears.
1514 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}}}"#;
1515 /// Clears one project item's value of one field, which is what a `none` priority is.
1516 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}}}}}}}}"#;
1517 /// Creates one field: a single-select one with its options, or a text one for a projected
1518 /// metadata value. Only the guarded field setup may use this document, and only for a
1519 /// field the board lacks.
1520 pub const CREATE_FIELD: &str = r#"mutation($input:CreateProjectV2FieldInput!){createProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id name options{id name color description}}}}}"#;
1521 /// Replaces a single-select field's options. Only the guarded field setup — the
1522 /// `status-options` and `fields` operations — may use this document, because GitHub
1523 /// treats the input as the complete option list.
1524 pub const STATUS_OPTIONS_UPDATE: &str = r#"mutation($input:UpdateProjectV2FieldInput!){updateProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id options{id name color description}}}}}"#;
1525 /// A fresh snapshot of the Status field and every board item's assignment.
1526 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}}}}}}"#;
1527 /// Files one issue under another as a sub-issue, which is what project membership is.
1528 pub const ADD_SUB_ISSUE: &str =
1529 r#"mutation($input:AddSubIssueInput!){addSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1530 /// Takes one issue back out of its parent.
1531 pub const REMOVE_SUB_ISSUE: &str = r#"mutation($input:RemoveSubIssueInput!){removeSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1532 /// Adds GitHub's native issue blocked-by relationship.
1533 pub const ADD_BLOCKED_BY: &str = r#"mutation($input:AddBlockedByInput!){addBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1534 /// One origin of [`ORIGIN_LOOKUPS`]: [`ORIGIN_LOOKUP`]'s two connections for the origin the
1535 /// `$filter<n>` and `$search<n>` of slot `<n>` name, sent only while `$ask<n>` holds.
1536 macro_rules! origin_lookup_alias {
1537 ($n:literal) => {
1538 concat!(
1539 "\n o",
1540 stringify!($n),
1541 ":repositoryOwner(login:$owner) @include(if:$ask",
1542 stringify!($n),
1543 "){... on ProjectV2Owner{projectV2(number:$number){items(first:$lookupFirst,query:$filter",
1544 stringify!($n),
1545 "){nodes{id updatedAt ",
1546 board_item_values!(),
1547 board_item_content!(),
1548 "} pageInfo{hasNextPage endCursor}}}}}\n s",
1549 stringify!($n),
1550 ":search(query:$search",
1551 stringify!($n),
1552 ",type:$type,first:$lookupFirst) @include(if:$ask",
1553 stringify!($n),
1554 "){pageInfo{hasNextPage endCursor} nodes{__typename ...BoardIssue}}"
1555 )
1556 };
1557 }
1558
1559 /// [`ORIGIN_LOOKUPS`], with `$extra_variables` declared after its own and `$extra_fields`
1560 /// selected beside them: one spelling of the batched lookup for the documents that send it.
1561 macro_rules! origin_lookups {
1562 ($extra_variables:literal, $extra_fields:literal) => {
1563 concat!(
1564 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!"#,
1565 relationship_events_variable!(),
1566 $extra_variables,
1567 r#"){
1568 boardFields:repositoryOwner(login:$owner){
1569 ... on ProjectV2Owner{projectV2(number:$number){id
1570 fields(first:$nestedFirst){nodes{
1571 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1572 ... on ProjectV2Field{__typename id name dataType}
1573 }pageInfo{hasNextPage}}
1574 }}
1575 }
1576 repository(owner:$repositoryOwner,name:$repositoryName){id databaseId nameWithOwner}"#,
1577 $extra_fields,
1578 origin_lookup_alias!(0),
1579 origin_lookup_alias!(1),
1580 origin_lookup_alias!(2),
1581 origin_lookup_alias!(3),
1582 origin_lookup_alias!(4),
1583 origin_lookup_alias!(5),
1584 origin_lookup_alias!(6),
1585 origin_lookup_alias!(7),
1586 origin_lookup_alias!(8),
1587 origin_lookup_alias!(9),
1588 "\n }",
1589 board_issue!()
1590 )
1591 };
1592 }
1593
1594 /// The first page of [`ORIGIN_LOOKUP`] for up to [`ORIGIN_BATCH`](super::ORIGIN_BATCH)
1595 /// origins a copy is about to look for, and everything creating an item then reads —
1596 /// the board's own id and field definitions, as [`CREATION_CONTEXT`] reads them, and the node
1597 /// id and the numeric id of the repository an issue is created in — in one request.
1598 ///
1599 /// What a copy reads before it writes anything, so that finding none of its records on the
1600 /// board and creating every one costs this read and the writes. An origin whose page says
1601 /// there is more is left to its own [`ORIGIN_LOOKUP`]. The repository's numeric id is what an
1602 /// image upload names, so a copy carrying assets needs no `GET /repos/{owner}/{repo}` either.
1603 pub const ORIGIN_LOOKUPS: &str = origin_lookups!("", "");
1604
1605 /// [`ORIGIN_LOOKUPS`], with a page of the members of the project `$project` beside it, each
1606 /// trimmed to what classifying it reads — its id, its parent, its body and its repository,
1607 /// and no connection under it.
1608 ///
1609 /// What a document copy sends where the public boundary screens its write: the document is
1610 /// held to who may read its project, which is its members' classification as much as the
1611 /// project's own, and this request carries both the project — among the origins it asks —
1612 /// and its members. GitHub's own dry run prices it at the one point [`ORIGIN_LOOKUPS`]
1613 /// costs alone (`docs/measurements/board-copy-gaps-2026-10-24/coalesced-members-dry-run`).
1614 pub const ORIGIN_LOOKUPS_WITH_MEMBERS: &str = origin_lookups!(
1615 ",$project:ID!,$memberFirst:Int!",
1616 "\n members:node(id:$project){... on Issue{subIssues(first:$memberFirst){pageInfo{hasNextPage endCursor} nodes{id body parent{id} repository{nameWithOwner}}}}}"
1617 );
1618
1619 /// One slot `<n>` of a copy's aliased writes: `$<prefix><n>` is its input and `$send<n>`
1620 /// whether it is sent.
1621 macro_rules! write_slot {
1622 ($prefix:literal, $field:literal, $selection:literal, $n:literal) => {
1623 concat!(
1624 " ",
1625 $prefix,
1626 stringify!($n),
1627 ":",
1628 $field,
1629 "(input:$",
1630 $prefix,
1631 stringify!($n),
1632 ") @include(if:$send",
1633 stringify!($n),
1634 "){",
1635 $selection,
1636 "}"
1637 )
1638 };
1639 }
1640
1641 /// The variables of a copy's aliased writes: eleven inputs of `$type` named `$<prefix><n>`,
1642 /// and eleven `$send<n>`.
1643 macro_rules! write_variables {
1644 ($prefix:literal, $type:literal) => {
1645 concat!(
1646 "mutation(",
1647 "$",
1648 $prefix,
1649 "0:",
1650 $type,
1651 "!,$",
1652 $prefix,
1653 "1:",
1654 $type,
1655 "!,$",
1656 $prefix,
1657 "2:",
1658 $type,
1659 "!,$",
1660 $prefix,
1661 "3:",
1662 $type,
1663 "!,$",
1664 $prefix,
1665 "4:",
1666 $type,
1667 "!,$",
1668 $prefix,
1669 "5:",
1670 $type,
1671 "!,$",
1672 $prefix,
1673 "6:",
1674 $type,
1675 "!,$",
1676 $prefix,
1677 "7:",
1678 $type,
1679 "!,$",
1680 $prefix,
1681 "8:",
1682 $type,
1683 "!,$",
1684 $prefix,
1685 "9:",
1686 $type,
1687 "!,$",
1688 $prefix,
1689 "10:",
1690 $type,
1691 "!,",
1692 "$send0:Boolean!,$send1:Boolean!,$send2:Boolean!,$send3:Boolean!,$send4:Boolean!,",
1693 "$send5:Boolean!,$send6:Boolean!,$send7:Boolean!,$send8:Boolean!,$send9:Boolean!,",
1694 "$send10:Boolean!){"
1695 )
1696 };
1697 }
1698
1699 /// A copy's aliased writes of one kind: [`MUTATION_BATCH`](super::MUTATION_BATCH) slots of
1700 /// `$field`, each sent only while its `$send<n>` holds.
1701 macro_rules! write_batch {
1702 ($prefix:literal, $type:literal, $field:literal, $selection:literal) => {
1703 concat!(
1704 write_variables!($prefix, $type),
1705 write_slot!($prefix, $field, $selection, 0),
1706 write_slot!($prefix, $field, $selection, 1),
1707 write_slot!($prefix, $field, $selection, 2),
1708 write_slot!($prefix, $field, $selection, 3),
1709 write_slot!($prefix, $field, $selection, 4),
1710 write_slot!($prefix, $field, $selection, 5),
1711 write_slot!($prefix, $field, $selection, 6),
1712 write_slot!($prefix, $field, $selection, 7),
1713 write_slot!($prefix, $field, $selection, 8),
1714 write_slot!($prefix, $field, $selection, 9),
1715 write_slot!($prefix, $field, $selection, 10),
1716 "}"
1717 )
1718 };
1719 }
1720
1721 /// Creates up to [`MUTATION_BATCH`](super::MUTATION_BATCH) issues in one request, each filed on
1722 /// the board and under its project by the same `createIssue` — its `projectV2Ids` and
1723 /// `parentIssueId` — as [`CREATE_ISSUE`] creates one.
1724 pub const CREATE_ISSUES: &str = write_batch!(
1725 "create",
1726 "CreateIssueInput",
1727 "createIssue",
1728 "issue{id number url}"
1729 );
1730
1731 /// One issue of [`CREATED_BOARD_ITEMS`]: the board items the issue `$id<n>` names is on, with
1732 /// the `Status` value of each, read only while `$read<n>` holds.
1733 macro_rules! created_item_alias {
1734 ($n:literal) => {
1735 concat!(
1736 "\n i",
1737 stringify!($n),
1738 ":node(id:$id",
1739 stringify!($n),
1740 ") @include(if:$read",
1741 stringify!($n),
1742 "){... on Issue{id projectItems(first:$boardItems){nodes{id project{id} status:fieldValueByName(name:$statusName){... on ProjectV2ItemFieldSingleSelectValue{name}}}}}}"
1743 )
1744 };
1745 }
1746
1747 /// The board item of each issue a copy created, up to
1748 /// [`COPY_DETAIL_BATCH`](super::COPY_DETAIL_BATCH) at a time, with its `Status` as the board
1749 /// set it: what `createIssue`'s `projectV2Ids` files the issue as, which GitHub adds after
1750 /// answering the create, so its id comes from a read made once every create has been sent.
1751 pub const CREATED_BOARD_ITEMS: &str = concat!(
1752 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!){"#,
1753 created_item_alias!(0),
1754 created_item_alias!(1),
1755 created_item_alias!(2),
1756 created_item_alias!(3),
1757 created_item_alias!(4),
1758 created_item_alias!(5),
1759 created_item_alias!(6),
1760 created_item_alias!(7),
1761 created_item_alias!(8),
1762 created_item_alias!(9),
1763 created_item_alias!(10),
1764 created_item_alias!(11),
1765 created_item_alias!(12),
1766 created_item_alias!(13),
1767 created_item_alias!(14),
1768 created_item_alias!(15),
1769 created_item_alias!(16),
1770 created_item_alias!(17),
1771 created_item_alias!(18),
1772 created_item_alias!(19),
1773 created_item_alias!(20),
1774 "\n }"
1775 );
1776
1777 /// The variables and the aliases of [`WRITE_BATCH`](super::WRITE_BATCH) slots: `$<prefix><n>`
1778 /// is a slot's input and `$<send><n>` whether it is sent, for the slot numbers given.
1779 macro_rules! wide_variables {
1780 ($prefix:literal, $type:literal, $send:literal; $($n:literal)+) => {
1781 concat!($("$", $prefix, stringify!($n), ":", $type, "!,$", $send, stringify!($n), ":Boolean!,",)+)
1782 };
1783 }
1784 macro_rules! wide_slots {
1785 ($prefix:literal, $field:literal, $selection:literal, $send:literal; $($n:literal)+) => {
1786 concat!($(
1787 " ", $prefix, stringify!($n), ":", $field, "(input:$", $prefix, stringify!($n),
1788 ") @include(if:$", $send, stringify!($n), "){", $selection, "}",
1789 )+)
1790 };
1791 }
1792
1793 /// Up to [`WRITE_BATCH`](super::WRITE_BATCH) board field values a copy writes on the items it
1794 /// created, each on its own item, in one ordered request.
1795 pub const WRITE_FIELD_VALUES: &str = concat!(
1796 "mutation(",
1797 wide_variables!("field", "UpdateProjectV2ItemFieldValueInput", "send"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1798 "){",
1799 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),
1800 "}"
1801 );
1802
1803 /// A copy's board field values and its blocked-by edges together, up to
1804 /// [`WRITE_BATCH`](super::WRITE_BATCH) of the two kinds between them, in one ordered request:
1805 /// every field value its `$sendField<n>` sends, then every edge its `$sendBlock<n>` sends.
1806 /// What a copy sends when one request of its writes holds both kinds; one holding a single
1807 /// kind is [`WRITE_FIELD_VALUES`] or [`ADD_BLOCKED_BYS`].
1808 pub const WRITE_FIELDS_AND_EDGES: &str = concat!(
1809 "mutation(",
1810 wide_variables!("field", "UpdateProjectV2ItemFieldValueInput", "sendField"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1811 wide_variables!("block", "AddBlockedByInput", "sendBlock"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1812 "){",
1813 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),
1814 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),
1815 "}"
1816 );
1817
1818 /// Closes up to [`MUTATION_BATCH`](super::MUTATION_BATCH) issues a copy created at a closing
1819 /// status, each with its own `stateInput` and nothing else, once its option is selected.
1820 pub const CLOSE_ISSUES: &str =
1821 write_batch!("close", "UpdateIssueInput", "updateIssue", "issue{id}");
1822
1823 /// Updates up to [`MUTATION_BATCH`](super::MUTATION_BATCH) existing issues a copy rewrites,
1824 /// each with the members its own [`UPDATE_ISSUE`] would have sent, in one request.
1825 pub const UPDATE_ISSUES: &str =
1826 write_batch!("update", "UpdateIssueInput", "updateIssue", "issue{id}");
1827
1828 /// Up to [`WRITE_BATCH`](super::WRITE_BATCH) native blocked-by relationships a copy adds to the
1829 /// issues it created, in one request.
1830 pub const ADD_BLOCKED_BYS: &str = concat!(
1831 "mutation(",
1832 wide_variables!("block", "AddBlockedByInput", "send"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1833 "){",
1834 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),
1835 "}"
1836 );
1837
1838 /// Removes one native issue blocked-by relationship.
1839 pub const REMOVE_BLOCKED_BY: &str = r#"mutation($input:RemoveBlockedByInput!){removeBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1840 /// Deletes one issue, which takes its board item with it.
1841 ///
1842 /// The engine sends this in one situation only: undoing a copy that could not finish,
1843 /// over the items that same copy created. Deleting the issue removes the board item
1844 /// too, so there is no second `deleteProjectV2Item` to keep in step with it.
1845 pub const DELETE_ISSUE: &str =
1846 r#"mutation($input:DeleteIssueInput!){deleteIssue(input:$input){repository{id}}}"#;
1847 /// Deletes up to [`DELETE_BATCH`](super::DELETE_BATCH) issues in one request, each as
1848 /// [`DELETE_ISSUE`] deletes one: what a write that created several issues and was refused
1849 /// part way sends to take them back.
1850 pub const DELETE_ISSUES: &str = concat!(
1851 "mutation(",
1852 wide_variables!("delete", "DeleteIssueInput", "send"; 0 1 2 3 4),
1853 "){",
1854 wide_slots!("delete", "deleteIssue", "repository{id}", "send"; 0 1 2 3 4),
1855 "}"
1856 );
1857
1858 /// One task's comments: a page of its issue's own `comments` connection.
1859 ///
1860 /// **No `orderBy`, and that is what makes the page oldest first.** GitHub's only
1861 /// `IssueCommentOrder` field is `UPDATED_AT`, which would move a comment to the end of the
1862 /// list every time somebody edited it; left unordered the connection answers in the order
1863 /// the comments were written, which is the order GitHub documents for the same collection
1864 /// over REST — ascending id. Nothing multiplies through it, so `$first` is the whole of its
1865 /// node count and the caller's own page size is pushed straight down.
1866 pub const ISSUE_COMMENTS: &str = concat!(
1867 r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename ... on Issue{comments(first:$first,after:$after){nodes{"#,
1868 issue_comment!(),
1869 r#"}pageInfo{hasNextPage endCursor}}}}}"#
1870 );
1871 /// One issue by its own node id, with a page of its comments: what `task show` and a
1872 /// comment listing read, in one request.
1873 ///
1874 /// [`ISSUE`] and [`ISSUE_COMMENTS`] in one document, rather than one then the other. The
1875 /// comments are selected here and **not** on the shared `board_issue!` fragment, which
1876 /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] nest under a page of a hundred issues: a comment
1877 /// connection there would multiply through both of those documents' price, and neither
1878 /// needs one.
1879 pub const ISSUE_DETAIL: &str = concat!(
1880 r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1881 relationship_events_variable!(),
1882 r#"){
1883 node(id:$id){__typename ...BoardIssue ... on Issue{comments(first:$first,after:$after){nodes{"#,
1884 issue_comment!(),
1885 r#"}pageInfo{hasNextPage endCursor}}}}
1886 }"#,
1887 board_issue!()
1888 );
1889
1890 /// One alias of [`ISSUE_DETAILS`]: the item a batch's `$id<n>` names, with the first
1891 /// page of its comments when `$comments` asks for them.
1892 macro_rules! issue_details_alias {
1893 ($n:literal) => {
1894 concat!(
1895 "\n i",
1896 stringify!($n),
1897 ":node(id:$id",
1898 stringify!($n),
1899 "){__typename ...BoardIssue ... on Issue{comments(first:$first) @include(if:$comments){nodes{",
1900 issue_comment!(),
1901 "}pageInfo{hasNextPage endCursor}}}}"
1902 )
1903 };
1904 }
1905
1906 /// [`ISSUE_DETAIL`] for [`DETAIL_BATCH`](super::DETAIL_BATCH) items at once, each by its
1907 /// own node id, as one fixed-size document of aliased `node(id:)` fields.
1908 ///
1909 /// **Aliased `node(id:)` rather than `nodes(ids:)`, and that is what keeps its price
1910 /// honest.** The `github-graphql-node-count` model this workspace prices with treats a
1911 /// field that supplies neither `first` nor `last` as free, and `nodes(ids:)` supplies
1912 /// neither — so every connection under it would be priced at nothing and the pin in
1913 /// `tests/point_cost.rs` would understate what GitHub charges. Each alias here is the
1914 /// one-item read the model already prices, so the batch costs what its aliases cost.
1915 ///
1916 /// **Fixed-size, so there is one document to price.** A batch of fewer items binds the
1917 /// slots it has no item for to the last item it does, and reads that item again; the
1918 /// price is the document's, whatever its variables, so a short batch costs what a full
1919 /// one does and nothing more.
1920 pub const ISSUE_DETAILS: &str = concat!(
1921 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!"#,
1922 relationship_events_variable!(),
1923 r#"){"#,
1924 issue_details_alias!(0),
1925 issue_details_alias!(1),
1926 issue_details_alias!(2),
1927 issue_details_alias!(3),
1928 issue_details_alias!(4),
1929 issue_details_alias!(5),
1930 issue_details_alias!(6),
1931 issue_details_alias!(7),
1932 issue_details_alias!(8),
1933 issue_details_alias!(9),
1934 issue_details_alias!(10),
1935 issue_details_alias!(11),
1936 issue_details_alias!(12),
1937 issue_details_alias!(13),
1938 issue_details_alias!(14),
1939 issue_details_alias!(15),
1940 issue_details_alias!(16),
1941 issue_details_alias!(17),
1942 issue_details_alias!(18),
1943 issue_details_alias!(19),
1944 issue_details_alias!(20),
1945 issue_details_alias!(21),
1946 issue_details_alias!(22),
1947 issue_details_alias!(23),
1948 "\n }",
1949 board_issue!()
1950 );
1951
1952 /// One alias of [`ISSUE_COPY_DETAILS`]: the item a batch's `$id<n>` names, with the first
1953 /// page of its comments and the issues it is blocked by.
1954 macro_rules! issue_copy_details_alias {
1955 ($n:literal) => {
1956 concat!(
1957 "\n i",
1958 stringify!($n),
1959 ":node(id:$id",
1960 stringify!($n),
1961 "){__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{",
1962 issue_comment!(),
1963 "}pageInfo{hasNextPage endCursor}} blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}}}"
1964 )
1965 };
1966 }
1967
1968 /// [`ISSUE_DETAILS`] as a copy reads the tasks it carries: each item with the first page of
1969 /// its comments and its `blockedBy` — the edges a copy reads of every task next, which
1970 /// [`ISSUE`] carries and [`ISSUE_DETAILS`] does not — [`COPY_DETAIL_BATCH`](super::COPY_DETAIL_BATCH)
1971 /// at a time, so a copy of a changed task costs this read and nothing else.
1972 ///
1973 /// A document of its own rather than a widening of [`ISSUE_DETAILS`], whose batch size is
1974 /// what `task show-many` is priced by and which reads no edge.
1975 ///
1976 /// `children`, read only while `$children` holds, is which issues the issue `$project` has
1977 /// as sub-issues — each one's id and title, the page `$nestedFirst` allows — so that a
1978 /// project copy, which reads its project and every task at once, knows from this same read
1979 /// which tasks the project holds at the destination, and its orphan check is answered by it
1980 /// rather than by a listing of the project.
1981 pub const ISSUE_COPY_DETAILS: &str = concat!(
1982 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!"#,
1983 relationship_events_variable!(),
1984 r#"){"#,
1985 issue_copy_details_alias!(0),
1986 issue_copy_details_alias!(1),
1987 issue_copy_details_alias!(2),
1988 issue_copy_details_alias!(3),
1989 issue_copy_details_alias!(4),
1990 issue_copy_details_alias!(5),
1991 issue_copy_details_alias!(6),
1992 issue_copy_details_alias!(7),
1993 issue_copy_details_alias!(8),
1994 issue_copy_details_alias!(9),
1995 issue_copy_details_alias!(10),
1996 issue_copy_details_alias!(11),
1997 issue_copy_details_alias!(12),
1998 issue_copy_details_alias!(13),
1999 issue_copy_details_alias!(14),
2000 issue_copy_details_alias!(15),
2001 issue_copy_details_alias!(16),
2002 issue_copy_details_alias!(17),
2003 issue_copy_details_alias!(18),
2004 issue_copy_details_alias!(19),
2005 issue_copy_details_alias!(20),
2006 "\n children:node(id:$project) @include(if:$children){... on Issue{id subIssues(first:$nestedFirst){nodes{id title} pageInfo{hasNextPage}}}}",
2007 "\n }",
2008 board_issue!(),
2009 related_issue!()
2010 );
2011
2012 /// Which issue one comment is on, read before that comment is edited or removed.
2013 ///
2014 /// GitHub's comment mutations take the comment's id and nothing else, so without this a
2015 /// comment id given against the wrong task would change a comment on another issue.
2016 pub const COMMENT_ISSUE: &str =
2017 r#"query($id:ID!){node(id:$id){__typename ... on IssueComment{id body issue{id}}}}"#;
2018 /// Adds one comment to an issue, signed as the account the token belongs to.
2019 pub const ADD_COMMENT: &str = concat!(
2020 r#"mutation($input:AddCommentInput!){addComment(input:$input){subject{id} commentEdge{node{"#,
2021 issue_comment!(),
2022 r#"}}}}"#
2023 );
2024 /// Replaces the body of one issue comment.
2025 pub const UPDATE_COMMENT: &str = concat!(
2026 r#"mutation($input:UpdateIssueCommentInput!){updateIssueComment(input:$input){issueComment{"#,
2027 issue_comment!(),
2028 r#"}}}"#
2029 );
2030 /// Removes one issue comment. Its payload carries nothing about the comment it removed.
2031 pub const DELETE_COMMENT: &str = r#"mutation($input:DeleteIssueCommentInput!){deleteIssueComment(input:$input){clientMutationId}}"#;
2032
2033 /// What a mutation of a write the store's cache expects selects of the issue it wrote:
2034 /// everything [`ISSUE_DETAIL`] reads of it, so its response shows the task whole — its
2035 /// change token's every part and the first page of its comments.
2036 ///
2037 /// Sent **only** while that write runs, in place of the document named beside each
2038 /// below, so every other write's documents, prices and request counts are what they
2039 /// were. GitHub's token waits for a task to settle (`change_token::SETTLED`), and a
2040 /// mutation's response is stamped seconds after the change it made, so against GitHub
2041 /// itself a response rarely determines a version; the cache is then left unserveable
2042 /// for the task, and its next read refreshes it.
2043 macro_rules! whole_issue {
2044 () => {
2045 concat!(
2046 r#"__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{"#,
2047 issue_comment!(),
2048 r#"}pageInfo{hasNextPage endCursor}}}"#
2049 )
2050 };
2051 }
2052
2053 /// The variables every document of `whole_issue!` declares beside its own.
2054 macro_rules! whole_variables {
2055 () => {
2056 concat!(
2057 r#",$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
2058 relationship_events_variable!()
2059 )
2060 };
2061 }
2062
2063 /// [`ADD_COMMENT`], selecting the whole issue commented on.
2064 pub const ADD_COMMENT_WHOLE: &str = concat!(
2065 r#"mutation($input:AddCommentInput!"#,
2066 whole_variables!(),
2067 r#"){addComment(input:$input){subject{id "#,
2068 whole_issue!(),
2069 r#"} commentEdge{node{"#,
2070 issue_comment!(),
2071 r#"}}}}"#,
2072 board_issue!()
2073 );
2074 /// [`UPDATE_COMMENT`], selecting the whole issue the comment is on.
2075 pub const UPDATE_COMMENT_WHOLE: &str = concat!(
2076 r#"mutation($input:UpdateIssueCommentInput!"#,
2077 whole_variables!(),
2078 r#"){updateIssueComment(input:$input){issueComment{"#,
2079 issue_comment!(),
2080 r#" issue{"#,
2081 whole_issue!(),
2082 r#"}}}}"#,
2083 board_issue!()
2084 );
2085 /// [`UPDATE_ISSUE`], selecting the whole issue updated.
2086 pub const UPDATE_ISSUE_WHOLE: &str = concat!(
2087 r#"mutation($input:UpdateIssueInput!"#,
2088 whole_variables!(),
2089 r#"){updateIssue(input:$input){issue{id "#,
2090 whole_issue!(),
2091 r#"}}}"#,
2092 board_issue!()
2093 );
2094 /// [`UPDATE_FIELD`], selecting the whole issue behind the board item written.
2095 pub const UPDATE_FIELD_WHOLE: &str = concat!(
2096 r#"mutation($input:UpdateProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
2097 whole_variables!(),
2098 r#"){updateProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
2099 whole_issue!(),
2100 r#"}}}}"#,
2101 board_issue!()
2102 );
2103 /// [`CLEAR_FIELD`], selecting the whole issue behind the board item cleared.
2104 pub const CLEAR_FIELD_WHOLE: &str = concat!(
2105 r#"mutation($input:ClearProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
2106 whole_variables!(),
2107 r#"){clearProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
2108 whole_issue!(),
2109 r#"}}}}"#,
2110 board_issue!()
2111 );
2112
2113 /// The document sent in `document`'s place while a write the store's cache expects runs,
2114 /// when it has one.
2115 #[must_use]
2116 pub fn whole_twin(document: &str) -> Option<&'static str> {
2117 [
2118 (ADD_COMMENT, ADD_COMMENT_WHOLE),
2119 (UPDATE_COMMENT, UPDATE_COMMENT_WHOLE),
2120 (UPDATE_ISSUE, UPDATE_ISSUE_WHOLE),
2121 (UPDATE_FIELD, UPDATE_FIELD_WHOLE),
2122 (CLEAR_FIELD, CLEAR_FIELD_WHOLE),
2123 ]
2124 .into_iter()
2125 .find(|(plain, _)| *plain == document)
2126 .map(|(_, whole)| whole)
2127 }
2128
2129 /// Every document above, with what this source is doing when it sends one.
2130 ///
2131 /// One list rather than a `match` beside the constants: a rate-limit diagnostic has to
2132 /// name the call that was refused, and a `match` with a catch-all arm would answer a
2133 /// document added later with "talking to GitHub" and never say so.
2134 ///
2135 /// `documents_are_all_inventoried` reads this file back and fails naming any `pub
2136 /// const` here that this list omits, so the two cannot part — which is the same guard
2137 /// `CATEGORIES` carries, in the one shape available to a set of `&str` constants.
2138 pub const DOCUMENTS: [(&str, &str); 57] = [
2139 (
2140 ISSUES_BY_CREATION,
2141 "listing a repository's issues newest first for a create key",
2142 ),
2143 (SEARCH_ISSUES, "searching this board's issues"),
2144 (ISSUE, "reading one issue"),
2145 (
2146 DOCUMENT_IN_PROJECT,
2147 "reading one document with its project and the project's members",
2148 ),
2149 (
2150 ISSUE_BOARD_ITEMS,
2151 "reading one issue's board memberships past the page it came with",
2152 ),
2153 (SUB_ISSUES, "reading a project's tasks"),
2154 (BOARD, "reading the board"),
2155 (
2156 BOARD_WITH_COMMENTS,
2157 "reading a board page with first comments",
2158 ),
2159 (ORIGIN_LOOKUP, "looking up the items copied from one origin"),
2160 (CLOSED_ITEMS, "reading the board's closed items"),
2161 (
2162 ISSUES_BY_REFERENCE,
2163 "reading a batch of issues by their references",
2164 ),
2165 (BOARD_FIELDS, "reading the board's fields"),
2166 (DRAFT, "reading one draft"),
2167 (REPOSITORY, "reading the destination repository"),
2168 (
2169 PROJECT_VISIBILITY,
2170 "reading whether the board's project is public",
2171 ),
2172 (
2173 CREATION_CONTEXT,
2174 "reading the board's fields and the destination repository",
2175 ),
2176 (ISSUE_DEPENDENCIES, "reading an issue's dependencies"),
2177 (ISSUE_DEPENDS_ON, "reading what an item depends on"),
2178 (ISSUE_DEPENDED_ON_BY, "reading what depends on an issue"),
2179 (CREATE_ISSUE, "creating an issue"),
2180 (
2181 ORIGIN_LOOKUPS,
2182 "looking up the items copied from several origins, and what creating one reads",
2183 ),
2184 (
2185 ORIGIN_LOOKUPS_WITH_MEMBERS,
2186 "looking up the items copied from several origins, with a project's members",
2187 ),
2188 (CREATE_ISSUES, "creating several issues together"),
2189 (
2190 CREATED_BOARD_ITEMS,
2191 "reading the board items of the issues a copy created",
2192 ),
2193 (
2194 WRITE_FIELD_VALUES,
2195 "writing board fields of the items a copy created",
2196 ),
2197 (
2198 WRITE_FIELDS_AND_EDGES,
2199 "writing board fields and dependencies of the items a copy created",
2200 ),
2201 (CLOSE_ISSUES, "closing the issues a copy created"),
2202 (UPDATE_ISSUES, "updating several issues together"),
2203 (ADD_BLOCKED_BYS, "recording several dependencies together"),
2204 (ADD_TO_BOARD, "adding an issue to the board"),
2205 (UPDATE_ISSUE, "updating an issue"),
2206 (UPDATE_DRAFT, "updating a draft item"),
2207 (UPDATE_FIELD, "writing a board field"),
2208 (UPDATE_FIELDS, "writing board fields together"),
2209 (CLEAR_FIELD, "clearing a board field"),
2210 (
2211 CREATE_FIELD,
2212 "creating a board single-select field with its options",
2213 ),
2214 (
2215 STATUS_OPTIONS_SNAPSHOT,
2216 "snapshotting board Status options and assignments",
2217 ),
2218 (
2219 STATUS_OPTIONS_UPDATE,
2220 "safely replacing the board Status option list",
2221 ),
2222 (ADD_SUB_ISSUE, "filing an issue under its project"),
2223 (REMOVE_SUB_ISSUE, "taking an issue out of its project"),
2224 (ADD_BLOCKED_BY, "recording a dependency"),
2225 (REMOVE_BLOCKED_BY, "removing a dependency"),
2226 (DELETE_ISSUE, "deleting an issue"),
2227 (DELETE_ISSUES, "deleting several issues together"),
2228 (ISSUE_COMMENTS, "reading a task's comments"),
2229 (ISSUE_DETAIL, "reading one issue with its comments"),
2230 (
2231 ISSUE_DETAILS,
2232 "reading a batch of issues with their comments",
2233 ),
2234 (
2235 ISSUE_COPY_DETAILS,
2236 "reading a batch of issues with their comments and dependencies for a copy",
2237 ),
2238 (COMMENT_ISSUE, "reading which issue a comment is on"),
2239 (ADD_COMMENT, "adding a comment"),
2240 (UPDATE_COMMENT, "editing a comment"),
2241 (DELETE_COMMENT, "deleting a comment"),
2242 (
2243 ADD_COMMENT_WHOLE,
2244 "adding a comment, answered with the whole issue",
2245 ),
2246 (
2247 UPDATE_COMMENT_WHOLE,
2248 "editing a comment, answered with the whole issue",
2249 ),
2250 (
2251 UPDATE_ISSUE_WHOLE,
2252 "updating an issue, answered with it whole",
2253 ),
2254 (
2255 UPDATE_FIELD_WHOLE,
2256 "writing a board field, answered with the whole issue",
2257 ),
2258 (
2259 CLEAR_FIELD_WHOLE,
2260 "clearing a board field, answered with the whole issue",
2261 ),
2262 ];
2263}
2264
2265/// One aliased mutation field of [`graphql::UPDATE_FIELDS`]: the key its answer comes back
2266/// under, the variable carrying its input, and the boolean variable that includes it.
2267#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2268pub struct FieldSlot {
2269 /// The key of the answer's `data` this field's payload is under.
2270 pub alias: &'static str,
2271 /// The variable carrying this field's input.
2272 pub variable: &'static str,
2273 /// The boolean variable whose `true` sends this field.
2274 pub include: &'static str,
2275}
2276
2277/// The value writes [`graphql::UPDATE_FIELDS`] carries, in the order GitHub runs them.
2278pub const FIELD_WRITE_SLOTS: [FieldSlot; 6] = [
2279 FieldSlot {
2280 alias: "updateProjectV2ItemFieldValue",
2281 variable: "input",
2282 include: "writeFirst",
2283 },
2284 FieldSlot {
2285 alias: "second",
2286 variable: "second",
2287 include: "writeSecond",
2288 },
2289 FieldSlot {
2290 alias: "third",
2291 variable: "third",
2292 include: "writeThird",
2293 },
2294 FieldSlot {
2295 alias: "fourth",
2296 variable: "fourth",
2297 include: "writeFourth",
2298 },
2299 FieldSlot {
2300 alias: "fifth",
2301 variable: "fifth",
2302 include: "writeFifth",
2303 },
2304 FieldSlot {
2305 alias: "sixth",
2306 variable: "sixth",
2307 include: "writeSixth",
2308 },
2309];
2310
2311/// The clears [`graphql::UPDATE_FIELDS`] carries, run after every write of the same request.
2312pub const FIELD_CLEAR_SLOTS: [FieldSlot; 3] = [
2313 FieldSlot {
2314 alias: "cleared",
2315 variable: "clear",
2316 include: "writeClear",
2317 },
2318 FieldSlot {
2319 alias: "clearedSecond",
2320 variable: "clearSecond",
2321 include: "writeClearSecond",
2322 },
2323 FieldSlot {
2324 alias: "clearedThird",
2325 variable: "clearThird",
2326 include: "writeClearThird",
2327 },
2328];
2329
2330/// Which of GitHub's two rate limiters refused a request.
2331///
2332/// Waiting is the whole answer to the primary budget, and polling is what *extends* the
2333/// secondary one — so an operator told the wrong one takes the wrong next step, which is
2334/// the whole reason this is carried rather than collapsed into "rate limited".
2335#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2336enum Limiter {
2337 /// The hourly API budget, which `gh api rate_limit` reports and a wait answers.
2338 Primary,
2339 /// The burst limiter over content-generating requests, which nothing reports.
2340 Secondary,
2341}
2342
2343/// The wordings GitHub answers a secondary rate limit with.
2344///
2345/// It sends them under a forbidden status, under a too-many-requests status, and inside
2346/// the `errors` of a *successful* response, which is why the text is what this matches on
2347/// rather than the status. `abuse detection` is the wording GitHub used before the
2348/// limiter was renamed and still returns from some endpoints; `submitted too quickly` is
2349/// what a burst of content creation is refused with.
2350///
2351/// This is GitHub's vocabulary rather than this source's, so it is pinned rather than
2352/// remembered: `tests/fixtures/rate-limits.json` records where each wording was read and
2353/// when, and the drift gate reconciles the two lists both ways. Public for that gate
2354/// alone — a caller has no use for it, and matching on a refusal is this source's job.
2355pub const SECONDARY_WORDINGS: [&str; 5] = [
2356 "secondary rate limit",
2357 "temporarily blocked from content creation",
2358 "abuse detection",
2359 "submitted too quickly",
2360 "exceeded a secondary",
2361];
2362
2363/// The wordings GitHub answers an exhausted primary budget with.
2364///
2365/// `rate_limited` is the `type` its GraphQL error carries, which is read as a field rather
2366/// than looked for in the response text. `api rate limit already exceeded` is what GraphQL
2367/// answers a request made once the hour's budget is spent — "API rate limit already exceeded
2368/// for user ID …" in the `errors` of an HTTP 200, with no `type` — and neither of the other
2369/// two phrases is a substring of it, so without it that answer read as a refusal that will
2370/// never lift. Pinned and gated exactly as [`SECONDARY_WORDINGS`] is, and public for the same
2371/// one reason.
2372pub const PRIMARY_WORDINGS: [&str; 4] = [
2373 "api rate limit exceeded",
2374 "api rate limit already exceeded",
2375 "rate limit exceeded",
2376 "rate_limited",
2377];
2378
2379/// The wordings GitHub answers a write that collided with another change to the same
2380/// ordering with, in the `errors` of an HTTP 200: "Your attempt to move this item created a
2381/// temporary conflict. Please try again." The write did not take effect and GitHub asks for it
2382/// again, so it is retried on the same growing schedule a rate limit without a hint is, within
2383/// the same retry budget — except a `createIssue`, whose answer cannot say the issue was not
2384/// made, and which is therefore reported rather than replayed into a duplicate.
2385///
2386/// Observed rather than documented, so it is pinned exactly as [`SECONDARY_WORDINGS`] is:
2387/// `tests/fixtures/rate-limits.json` records where it was read under `conflict.observed`, and
2388/// the same drift gate reconciles the two both ways. Public for that gate alone.
2389pub const CONFLICT_WORDINGS: [&str; 1] = ["created a temporary conflict"];
2390
2391/// What a response *says about itself*, which is the only place a refusal can be read.
2392///
2393/// Deliberately not the whole response body. A board is a place people write about their
2394/// own work, and a task on it titled "the secondary rate limit" would, matched across the
2395/// raw text, turn a perfectly good answer into a refusal this source then waited out and
2396/// reported. So the item data is never read: what is read is GitHub's own REST-style
2397/// `message` envelope, which is what a forbidden status carries, and the `message` and
2398/// `type` of each GraphQL error, which is where a *successful* response says it.
2399///
2400/// A body that is not JSON at all has nothing structured to read, so only a failing
2401/// response's own text is taken — a successful response that is not JSON is malformed
2402/// rather than refused, and [`GitHubProjectsSource::answer`] says so.
2403fn refusal_wording(status: StatusCode, body: &str) -> String {
2404 let Ok(parsed) = serde_json::from_str::<Value>(body) else {
2405 return if status.is_success() {
2406 String::new()
2407 } else {
2408 body.to_owned()
2409 };
2410 };
2411 let mut said: Vec<&str> = parsed
2412 .get("message")
2413 .and_then(Value::as_str)
2414 .into_iter()
2415 .collect();
2416 if let Some(errors) = parsed.get("errors").and_then(Value::as_array) {
2417 for error in errors {
2418 said.extend(
2419 ["message", "type"]
2420 .into_iter()
2421 .filter_map(|key| error.get(key).and_then(Value::as_str)),
2422 );
2423 }
2424 }
2425 said.join("; ")
2426}
2427
2428impl Limiter {
2429 /// Which limiter refused this response, or `None` when none of them did.
2430 ///
2431 /// The wording is read first and the status only decides what carries none of it,
2432 /// because GitHub answers a secondary limit with a forbidden status far more often
2433 /// than with too-many-requests — while a forbidden status saying nothing about a limit
2434 /// really is a credential this token lacks.
2435 ///
2436 /// A response is a refusal because of its status or its own wording. A spent budget
2437 /// only ever explains one; it never turns an answer into a refusal.
2438 fn classify(status: StatusCode, budget_exhausted: bool, body: &str) -> Option<Self> {
2439 let normalized = refusal_wording(status, body).to_ascii_lowercase();
2440 if SECONDARY_WORDINGS
2441 .iter()
2442 .any(|wording| normalized.contains(wording))
2443 {
2444 return Some(Self::Secondary);
2445 }
2446 if status == StatusCode::TOO_MANY_REQUESTS {
2447 return Some(Self::Primary);
2448 }
2449 // An exhausted budget *explains* a response that failed; it does not make one that
2450 // succeeded into a failure. GitHub sets `x-ratelimit-remaining: 0` on the last
2451 // request the budget allowed as well as on the ones it then refuses, so reading
2452 // the header alone threw away a good answer — and, once refusals were retried,
2453 // replayed a request that had already taken effect.
2454 if !status.is_success() && budget_exhausted {
2455 return Some(Self::Primary);
2456 }
2457 // A successful response saying it: GitHub reports a GraphQL rate limit in the
2458 // `errors` of an HTTP 200, where nothing about the status says so at all.
2459 if status.is_success()
2460 && PRIMARY_WORDINGS
2461 .iter()
2462 .any(|wording| normalized.contains(wording))
2463 {
2464 return Some(Self::Primary);
2465 }
2466 None
2467 }
2468
2469 /// What this limiter is called where an operator can look it up.
2470 const fn name(self) -> &'static str {
2471 match self {
2472 Self::Primary => "GitHub's primary API rate limit",
2473 Self::Secondary => "GitHub's secondary rate limit",
2474 }
2475 }
2476
2477 /// What the endpoint an operator would go and check says about this limiter.
2478 const fn where_to_look(self) -> &'static str {
2479 match self {
2480 Self::Primary => {
2481 "That is the budget `gh api rate_limit` reports, so that endpoint says when it \
2482 comes back."
2483 }
2484 Self::Secondary => {
2485 "That limiter is not the primary API budget: `gh api rate_limit` reports the \
2486 primary budget and does not report this one, so budget showing there says \
2487 nothing about this refusal, and every further attempt extends it."
2488 }
2489 }
2490 }
2491
2492 /// The next step this limiter actually calls for.
2493 const fn what_to_do(self) -> &'static str {
2494 match self {
2495 Self::Primary => {
2496 "wait for the reset `gh api rate_limit` reports, then run the command again."
2497 }
2498 Self::Secondary => {
2499 "leave this board alone for a few minutes, then run the command again — or \
2500 raise pacing.min_mutation_interval_ms on this source so it writes more slowly."
2501 }
2502 }
2503 }
2504}
2505
2506/// One rate-limit refusal, and the wait GitHub asked for if it asked for one.
2507#[derive(Debug, Clone, Copy)]
2508struct Limited {
2509 limiter: Limiter,
2510 hint: Option<u64>,
2511 /// Which header the hint came from: `retry-after`, or an exhausted primary budget's
2512 /// `x-ratelimit-reset`.
2513 hinted_by: Hint,
2514}
2515
2516/// Where a refusal's hint came from, which is what its wait is named after.
2517#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2518enum Hint {
2519 /// `retry-after`, or no hint at all.
2520 RetryAfter,
2521 /// An exhausted primary budget's `x-ratelimit-reset`.
2522 PrimaryReset,
2523}
2524
2525impl Limited {
2526 /// The wait this refusal is answered with, when the backing-off schedule is at `backoff`,
2527 /// and the cause it is named by: the hint, raised to the schedule's own next wait —
2528 /// GitHub really does send `retry-after: 0`, and retrying at once is the one move that
2529 /// extends a secondary limit — or the schedule alone when there is none.
2530 fn wait(self, backoff: Duration) -> (Duration, WaitCause) {
2531 match (self.hint, self.hinted_by) {
2532 (Some(hint), Hint::RetryAfter) => (
2533 Duration::from_secs(hint).max(backoff),
2534 pacing::cause::RETRY_AFTER,
2535 ),
2536 (Some(hint), Hint::PrimaryReset) => (
2537 Duration::from_secs(hint).max(backoff),
2538 pacing::cause::PRIMARY_RESET,
2539 ),
2540 (None, _) => (backoff, pacing::cause::BACKOFF),
2541 }
2542 }
2543
2544 /// What the caller is told once this source has waited as long as it may.
2545 ///
2546 /// Both limiters report as [`SourceError::RateLimited`], because that is what
2547 /// happened: the kind a caller matches on says a rate limit refused this, and nothing
2548 /// about *which* limiter it was makes it a different kind of failure. What differs is
2549 /// the operator's next step, and that is what the message carries — a secondary
2550 /// refusal read as a primary one sends an operator to `gh api rate_limit`, where the
2551 /// budget looks fine, and then back to retry the very burst that was refused.
2552 fn exhausted(
2553 self,
2554 doing: &str,
2555 waits: u32,
2556 waited: Duration,
2557 needed: Duration,
2558 budget: Duration,
2559 ) -> SourceError {
2560 SourceError::RateLimited {
2561 retry_after_seconds: self.hint,
2562 message: Some(format!(
2563 "{} refused this source while {doing}; it waited {} out over {} and was refused \
2564 again, and the next wait of {} would take it past the {} one call may spend \
2565 waiting. {} next: {}",
2566 self.limiter.name(),
2567 plural(waits, "refusal"),
2568 seconds(waited),
2569 seconds(needed),
2570 seconds(budget),
2571 self.limiter.where_to_look(),
2572 self.limiter.what_to_do(),
2573 )),
2574 }
2575 }
2576}
2577
2578/// One HTTP attempt's result, with what its response said about the rate limit.
2579///
2580/// The two travel together so the record and the outcome are written from the same place:
2581/// what a response said about the budget is only readable while that response is in hand,
2582/// and what the attempt *meant* is only decidable once its body has been read.
2583struct Attempted {
2584 result: Result<Value, Attempt>,
2585 limits: accounting::RateLimit,
2586 /// GitHub's own reported cost for this call, for a document that asked for it.
2587 reported_cost: Option<u64>,
2588 response_outcome: accounting::ResponseOutcome,
2589}
2590
2591/// One attempt's outcome: an error to report, or a rate limit to wait out.
2592enum Attempt {
2593 Failed(SourceError),
2594 Limited(Limited),
2595 /// GitHub refused a write as a temporary conflict and asked for it again — see
2596 /// [`CONFLICT_WORDINGS`]. Carries what GitHub said, for the diagnostic once retrying ends.
2597 Conflicted(String),
2598}
2599
2600fn plural(count: u32, thing: &str) -> String {
2601 if count == 1 {
2602 format!("{count} {thing}")
2603 } else {
2604 format!("{count} {thing}s")
2605 }
2606}
2607
2608fn seconds(duration: Duration) -> String {
2609 format!("{:.1}s", duration.as_secs_f64())
2610}
2611
2612/// The member [`stamp_served`] writes onto every node of a response that says when it last
2613/// changed: the `Date` that response was served at. No GitHub type has a field of this name.
2614const SERVED_AT: &str = "onetaskgraphServedAt";
2615
2616/// Write `served` onto every object of `data` carrying an `updatedAt`, so whatever reads a
2617/// node's time later knows the server's clock on the response that read it — which is what
2618/// decides whether that node's change token is given (see [`change_token::SETTLED`]).
2619fn stamp_served(data: &mut Value, served: &str) {
2620 match data {
2621 Value::Object(members) => {
2622 if members.contains_key("updatedAt") {
2623 members.insert(SERVED_AT.to_owned(), Value::String(served.to_owned()));
2624 }
2625 for member in members.values_mut() {
2626 stamp_served(member, served);
2627 }
2628 }
2629 Value::Array(items) => {
2630 for item in items {
2631 stamp_served(item, served);
2632 }
2633 }
2634 _ => {}
2635 }
2636}
2637
2638/// A header GitHub spells as a whole number of seconds, or `None` when this one is not.
2639///
2640/// A value that is present and unreadable is deliberately *not* an error. `retry-after` is
2641/// allowed by HTTP to be a date rather than a count, an intermediary can rewrite either
2642/// header, and neither is what makes a response a refusal — so the whole cost of one this
2643/// cannot read is that the refusal carries no hint and the backing-off schedule answers it
2644/// instead. Refusing the response over the header would turn a readable refusal into an
2645/// unreadable one, and refusing to *wait* would be the one wrong direction to fail in.
2646fn whole_seconds(value: Option<&reqwest::header::HeaderValue>) -> Option<u64> {
2647 value
2648 .and_then(|value| value.to_str().ok())
2649 .and_then(|value| value.trim().parse::<u64>().ok())
2650}
2651
2652/// A transport failure with every cause beneath it, outermost first.
2653///
2654/// The client's own text for a request it could not complete is `error sending request for
2655/// url (…)`, and the socket error that says why — a refused or reset connection, an exhausted
2656/// port range, a connection closed before its answer — is only in the chain of sources below
2657/// it. Those differ in what to do next, so all of them are kept.
2658pub(crate) fn transport_error(error: &(dyn std::error::Error + 'static)) -> String {
2659 let mut text = error.to_string();
2660 let mut cause = error.source();
2661 while let Some(next) = cause {
2662 let said = next.to_string();
2663 if !text.ends_with(&said) {
2664 text = format!("{text}: {said}");
2665 }
2666 cause = next.source();
2667 }
2668 text
2669}
2670
2671/// Every mutation this source sends creates content — an issue, a board item, a field of
2672/// one, a sub-issue link, a dependency, a comment — or edits or removes content of that
2673/// kind, and no query in [`graphql::DOCUMENTS`] does, so what the secondary limiter counts
2674/// and what the keyword says are the same set. That is what makes the keyword a sound test
2675/// rather than a convenient one: counting an edit or a removal the limiter might not have
2676/// counted costs a wait, and not counting one it did count costs the next fifty minutes.
2677pub(crate) fn is_mutation(query: &str) -> bool {
2678 query.trim_start().starts_with("mutation")
2679}
2680
2681/// What this source was doing, for a diagnostic that has to say so.
2682///
2683/// Read out of [`graphql::DOCUMENTS`], which is the inventory rather than a copy of it, so
2684/// a document added without a description is caught by that list's own gate instead of
2685/// falling through to the vague arm below.
2686fn operation_description(query: &str) -> &'static str {
2687 graphql::DOCUMENTS
2688 .iter()
2689 .find(|(document, _)| *document == query)
2690 .map_or("talking to GitHub", |(_, doing)| *doing)
2691}
2692
2693/// GitHub's published ceiling on content-generating requests in any one minute.
2694///
2695/// Pinned in `tests/fixtures/rate-limits.json` and gated against it, because it is
2696/// GitHub's number rather than this source's. This source paces at it over its window: a
2697/// content-creating request goes out at once while the minute's creations leave room for it,
2698/// and waits until they do otherwise — see the `pacing` module.
2699pub const CONTENT_CREATION_PER_MINUTE: u64 = 80;
2700/// The same ceiling as GitHub publishes it for any one hour, paced over its window as the
2701/// per-minute one is. Pinned beside its sibling so the gate would notice either one moving.
2702pub const CONTENT_CREATION_PER_HOUR: u64 = 500;
2703/// GitHub's published ceiling on secondary points in any one minute: what every GraphQL
2704/// request is charged against, a mutation [`SECONDARY_POINTS_PER_MUTATION`] and a query
2705/// [`SECONDARY_POINTS_PER_QUERY`]. Pinned and paced as [`CONTENT_CREATION_PER_MINUTE`] is.
2706pub const SECONDARY_POINTS_PER_MINUTE: u64 = 2_000;
2707/// The secondary points one GraphQL request carrying a mutation is charged.
2708pub const SECONDARY_POINTS_PER_MUTATION: u64 = 5;
2709/// The secondary points one GraphQL request carrying no mutation is charged.
2710pub const SECONDARY_POINTS_PER_QUERY: u64 = 1;
2711/// The fastest *steady* spacing of content-creating mutations that cannot exceed
2712/// [`CONTENT_CREATION_PER_MINUTE`], in milliseconds: 60000/80 is 750.
2713///
2714/// It is no longer the default. This source paces by GitHub's windows instead, so a copy's
2715/// few dozen writes go out as fast as GitHub answers them while the minute has room, rather
2716/// than 750 ms apart — which was most of a plan copy's wall time. An installation that wants
2717/// mutations spaced as well says so through `pacing.min_mutation_interval_ms`, and this is the
2718/// value that holds a long sustained burst under the per-minute bound by spacing alone.
2719pub const MIN_MUTATION_INTERVAL_MS: u64 = 60_000 / CONTENT_CREATION_PER_MINUTE;
2720/// First wait when a rate-limit refusal carries no hint; each further wait doubles it.
2721///
2722/// A doubling schedule from one second reaches a minute in six waits, which is GitHub's
2723/// own advice for a secondary limit — wait, and wait longer each time — without spending
2724/// the first minute of a transient refusal doing nothing.
2725pub const RETRY_BACKOFF_MS: u64 = 1_000;
2726/// Total time one call may spend waiting out rate limits before it reports a failure.
2727///
2728/// Two minutes is long enough to ride out the refusals a paced copy still collects and
2729/// short enough that a command an operator is watching returns. The bound is what makes
2730/// the wait a wait rather than a hang: a call refused past it ends in a diagnostic naming
2731/// the limiter, not in a process nobody can tell from a wedged one.
2732pub const RETRY_BUDGET_MS: u64 = 120_000;
2733
2734fn default_token_env() -> String {
2735 "GH_PROJECTS_TOKEN".to_owned()
2736}
2737fn default_endpoint() -> String {
2738 "https://api.github.com/graphql".to_owned()
2739}
2740
2741/// The name of a `Status` single-select option on the board.
2742///
2743/// Validated on the way in rather than checked later, so a blank option name — which
2744/// nothing on a board can be — is a state this type cannot hold.
2745#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2746#[serde(try_from = "String")]
2747#[schemars(extend("minLength" = 1))]
2748pub struct ColumnName(String);
2749
2750impl ColumnName {
2751 /// The option name, as the board spells it.
2752 fn as_str(&self) -> &str {
2753 &self.0
2754 }
2755}
2756
2757impl TryFrom<String> for ColumnName {
2758 type Error = String;
2759
2760 fn try_from(name: String) -> Result<Self, Self::Error> {
2761 if name.trim().is_empty() {
2762 return Err("a status_mapping option name cannot be blank".to_owned());
2763 }
2764 Ok(Self(name))
2765 }
2766}
2767
2768/// The two closed states this product can mean.
2769///
2770/// GitHub's `IssueClosedStateReason` also spells `DUPLICATE`, which nothing here ever writes:
2771/// an issue closed as one *reads* `cancelled`, because its work is another issue's, but this
2772/// product never decides on a person's behalf that two issues are the same work.
2773#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, schemars::JsonSchema)]
2774#[serde(rename_all = "kebab-case")]
2775pub enum ClosedState {
2776 /// `COMPLETED` — precisely done.
2777 Completed,
2778 /// `NOT_PLANNED` — precisely cancelled.
2779 NotPlanned,
2780}
2781
2782impl ClosedState {
2783 const fn reason(self) -> &'static str {
2784 match self {
2785 Self::Completed => "COMPLETED",
2786 Self::NotPlanned => "NOT_PLANNED",
2787 }
2788 }
2789}
2790
2791/// Configuration for one GitHub Projects v2 board.
2792///
2793/// It serializes to a document this same type reads back as itself, so a caller writing a
2794/// configuration — a consumer filing onto a board it configures — can build one here rather
2795/// than spell the shape again; an empty `metadata_fields` is left out, as one never set.
2796#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2797#[serde(default, deny_unknown_fields)]
2798pub struct GitHubProjectsConfig {
2799 /// Login of the user or organization which owns the board.
2800 pub owner: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates GitHub's owner grammar before private construction.
2801 /// The project number shown in the board's GitHub URL.
2802 pub project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` bounds this to a positive GraphQL Int.
2803 // 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.
2804 /// `owner/name` of the repository this source creates an issue in when the item's own
2805 /// `repositories` field does not decide it.
2806 ///
2807 /// An item naming exactly one repository is created there — on a source declared
2808 /// `visibility: private`, only when that repository reads private, and otherwise as an
2809 /// item naming none is; a task or a document naming none or several is created in its
2810 /// parent project's repository; and a project, or a task or document with no parent,
2811 /// naming none or several is created here. A board
2812 /// has no repository of its own and `createIssue` requires one, so a write without
2813 /// this is refused naming the field. Reads never need it.
2814 pub repository: Option<String>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the `owner/name` grammar before private construction.
2815 // llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
2816 /// GitHub logins whose repositories this source may create an issue in.
2817 ///
2818 /// Absent or empty, the default, restricts nothing. Otherwise a create whose issue the
2819 /// rule `repository` describes would file in a repository owned by anyone else is refused
2820 /// before anything of it is written — by `task copy`, `task copy --create`, a member
2821 /// create of a project copy and a dry run of each — with the error kind `creation-owner`,
2822 /// naming the repository and these owners. Logins compare case-insensitively, as GitHub
2823 /// compares them. An update of an issue that already exists is never refused by it.
2824 /// `task locate` reports such a refusal as `creates_in.refused` rather than failing.
2825 #[serde(default, skip_serializing_if = "Vec::is_empty")]
2826 // Kept in the schema as `"default": []` although a serialized configuration leaves an empty
2827 // list out, so both SDKs model an absent list as an empty one rather than as `null`.
2828 #[schemars(!skip_serializing_if)]
2829 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.
2830 /// Environment variable containing a fine-grained token with Projects and Issues
2831 /// read/write plus Pull requests read-only access for every repository represented on
2832 /// the board.
2833 #[serde(default = "default_token_env")]
2834 pub token_env: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the environment-variable grammar.
2835 /// GraphQL endpoint. GitHub Enterprise installations may override it.
2836 #[serde(default = "default_endpoint")]
2837 pub endpoint: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` converts it to the private validated `Url`.
2838 /// Per-instance mapping from a status category to the option of the board's one
2839 /// `Status` field it lands on, for a task and for a project.
2840 ///
2841 /// The shared `StatusMapping` grammar: each value is one option name for both kinds,
2842 /// `null` to disable the category for both, or `{task, project}` naming it per kind,
2843 /// where a kind left out leaves the category unmapped for that kind. A category this
2844 /// does not mention keeps its shipped default for both kinds: `backlog` to "Backlog",
2845 /// `todo` to "Todo", `queued` to "Queued", `in-progress` to "In Progress", `done` to
2846 /// "Done" plus closed as completed, `cancelled` to "Cancelled" plus closed as not
2847 /// planned, and `draft` and `unknown` unmapped. A category it does mention gets no
2848 /// shipped default for a kind it leaves out. `done` and `cancelled` close the issue for
2849 /// either kind. No two categories may name one option for the same kind, ignoring case.
2850 /// `unknown` may name one existing option; every unknown word then lands on it and
2851 /// reads back as `unknown` under its name. Unlike `local-md`, this source cannot keep
2852 /// each unknown word because it never creates board options.
2853 #[serde(default)]
2854 pub status_mapping: StatusMapping,
2855 /// Per-instance mapping from a task's priority to an option of this board's
2856 /// single-select field named `Priority`.
2857 ///
2858 /// Absent, this source holds no priority: every task reads as `none`, and a write of any
2859 /// other priority is refused before it reaches this board. Present, each of `urgent`,
2860 /// `high`, `medium` and `low` it does not mention keeps its shipped default — `Urgent`,
2861 /// `High`, `Medium` and `Low` — and an item with no value in the `Priority` field reads
2862 /// as `none`, so writing `none` clears the value. Option names match case-insensitively;
2863 /// no two levels may name one option. Reads and writes never create the field or an
2864 /// option: `onetaskgraph sources fields <source> --apply` does, and a write naming one
2865 /// the board lacks is refused pointing there.
2866 #[serde(default)]
2867 pub priority_mapping: Option<PriorityMappingConfig>,
2868 /// Caller metadata values this source also writes to board text fields, so the board can
2869 /// filter on them.
2870 ///
2871 /// Absent or empty, nothing is projected. Each entry names a board text `field`, a
2872 /// top-level metadata `key` and an optional `path` of object keys walked inside that key's
2873 /// value. On every item write — create, update and copy, of a task, a project or a
2874 /// document — a string found there is written to the field, and nothing is sent when the
2875 /// field already holds it; nothing there, or `null`, clears the field when it holds a
2876 /// value; any other JSON type is refused before any mutation. The metadata comment in the
2877 /// issue body stays the one home of the value, and every read reports metadata from it.
2878 /// Reads and writes never create the field: `onetaskgraph sources fields <source>
2879 /// --apply` does, as a text field, and a write to a board lacking it, or holding a
2880 /// non-text field of that name, is refused pointing there.
2881 #[serde(default, skip_serializing_if = "Vec::is_empty")]
2882 // Kept in the schema as `"default": []` although a serialized configuration leaves an empty
2883 // list out, so both SDKs model an absent list as an empty one rather than as `null`.
2884 #[schemars(!skip_serializing_if)]
2885 pub metadata_fields: Vec<MetadataFieldConfig>,
2886 /// How fast this source writes, and how long it waits out a rate-limit refusal.
2887 ///
2888 /// Every field keeps its shipped default when it is absent, and the defaults are
2889 /// GitHub's own published limits rather than taste. See [`Pacing`].
2890 #[serde(default)]
2891 pub pacing: PacingConfig,
2892}
2893
2894/// Which option of the board's `Priority` field each priority lands on.
2895///
2896/// One member per level rather than a map, so a key that is not a level is refused where
2897/// the configuration is read, naming the levels there are. `none` is not a member: it is no
2898/// value in the field, not an option of it.
2899#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2900#[serde(default, deny_unknown_fields)]
2901pub struct PriorityMappingConfig {
2902 /// The option `urgent` lands on; `Urgent` when absent.
2903 pub urgent: Option<PriorityOptionName>,
2904 /// The option `high` lands on; `High` when absent.
2905 pub high: Option<PriorityOptionName>,
2906 /// The option `medium` lands on; `Medium` when absent.
2907 pub medium: Option<PriorityOptionName>,
2908 /// The option `low` lands on; `Low` when absent.
2909 pub low: Option<PriorityOptionName>,
2910}
2911
2912/// The name of an option of the board's `Priority` single-select field.
2913///
2914/// Validated on the way in, for the reason [`ColumnName`] is: nothing on a board can have a
2915/// blank name.
2916#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2917#[serde(try_from = "String")]
2918#[schemars(extend("minLength" = 1))]
2919pub struct PriorityOptionName(String);
2920
2921impl PriorityOptionName {
2922 /// The option name, as the board spells it.
2923 fn as_str(&self) -> &str {
2924 &self.0
2925 }
2926}
2927
2928impl TryFrom<String> for PriorityOptionName {
2929 type Error = String;
2930
2931 fn try_from(name: String) -> Result<Self, Self::Error> {
2932 if name.trim().is_empty() {
2933 return Err("a priority_mapping option name cannot be blank".to_owned());
2934 }
2935 Ok(Self(name))
2936 }
2937}
2938
2939/// The name of the board field a priority is held in.
2940pub const PRIORITY_FIELD: &str = "Priority";
2941
2942/// The four priorities a board option can hold, in the order a new `Priority` field lists
2943/// them. `none` is not among them: it is the field holding no value.
2944///
2945/// This list mirrors `Priority`, so it carries its own drift gate, in the shape [`CATEGORIES`]
2946/// does: [`level_position`] is a wildcard-free match, so a priority added to the shared
2947/// vocabulary fails to compile until it is placed there, and this crate's suite reconciles
2948/// this list and [`PriorityMappingConfig`]'s members against that enum's own derived schema.
2949pub const PRIORITY_LEVELS: [Priority; 4] = [
2950 Priority::Urgent,
2951 Priority::High,
2952 Priority::Medium,
2953 Priority::Low,
2954];
2955
2956/// Where one priority sits in [`PRIORITY_LEVELS`], or `None` for `none`, which is no option;
2957/// see that list for what this pins.
2958#[must_use]
2959pub const fn level_position(priority: Priority) -> Option<usize> {
2960 match priority {
2961 Priority::None => None,
2962 Priority::Urgent => Some(0),
2963 Priority::High => Some(1),
2964 Priority::Medium => Some(2),
2965 Priority::Low => Some(3),
2966 }
2967}
2968
2969/// This instance's complete priority-to-option mapping, read in both directions.
2970///
2971/// One option per level, held in [`PRIORITY_LEVELS`] order, once it is established that no
2972/// two levels name one option.
2973#[derive(Debug, Clone)]
2974struct PriorityMapping {
2975 options: [PriorityOptionName; 4],
2976}
2977
2978impl PriorityMapping {
2979 fn resolve(config: PriorityMappingConfig, instance: &SourceName) -> Result<Self, SourceError> {
2980 let shipped = |name: &str| PriorityOptionName(name.to_owned());
2981 let mapping = Self {
2982 options: [
2983 config.urgent.unwrap_or_else(|| shipped("Urgent")),
2984 config.high.unwrap_or_else(|| shipped("High")),
2985 config.medium.unwrap_or_else(|| shipped("Medium")),
2986 config.low.unwrap_or_else(|| shipped("Low")),
2987 ],
2988 };
2989 for (index, option) in mapping.options.iter().enumerate() {
2990 if let Some(earlier) = mapping.options[..index]
2991 .iter()
2992 .position(|other| other.as_str().eq_ignore_ascii_case(option.as_str()))
2993 {
2994 return Err(SourceError::Config {
2995 message: format!(
2996 "priority_mapping of source {instance} sends both {} and {} to the board \
2997 option {:?}; one option cannot read back as two priorities",
2998 PRIORITY_LEVELS[earlier],
2999 PRIORITY_LEVELS[index],
3000 option.as_str()
3001 ),
3002 });
3003 }
3004 }
3005 Ok(mapping)
3006 }
3007
3008 /// The option `priority` lands on, or `None` for `none`, which is no option at all.
3009 fn option(&self, priority: Priority) -> Option<&str> {
3010 level_position(priority).map(|index| self.options[index].as_str())
3011 }
3012
3013 /// The priority a board option name reports, or `None` when nothing maps to it.
3014 fn priority_of(&self, option: &str) -> Option<Priority> {
3015 self.options
3016 .iter()
3017 .position(|name| name.as_str().eq_ignore_ascii_case(option))
3018 .map(|index| PRIORITY_LEVELS[index])
3019 }
3020
3021 /// Every mapped option name, in the order a new `Priority` field lists them.
3022 fn names(&self) -> impl Iterator<Item = &str> {
3023 self.options.iter().map(PriorityOptionName::as_str)
3024 }
3025}
3026
3027/// One metadata value this source projects onto a board text field.
3028#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3029#[serde(deny_unknown_fields)]
3030pub struct MetadataFieldConfig {
3031 /// The name of the board text field the value is written to.
3032 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.
3033 /// The top-level metadata key the value is read from. A key under the reserved
3034 /// `onetaskgraph.` prefix is refused.
3035 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.
3036 /// Object keys walked inside that key's value, outermost first. Absent or empty, the
3037 /// key's own value is the one projected. Left out of a serialized entry when empty.
3038 #[serde(default, skip_serializing_if = "Vec::is_empty")]
3039 // Kept in the schema as `"default": []`, for the reason `metadata_fields`' is.
3040 #[schemars(!skip_serializing_if)]
3041 pub path: Vec<String>,
3042}
3043
3044/// The board fields no metadata value may be projected onto, compared ignoring case: the
3045/// three this source writes itself, and those GitHub owns on every board.
3046// 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.
3047const UNPROJECTABLE_FIELDS: [&str; 13] = [
3048 STATUS_FIELD,
3049 PRIORITY_FIELD,
3050 ORIGIN_FIELD,
3051 "Title",
3052 "Assignees",
3053 "Labels",
3054 "Linked pull requests",
3055 "Milestone",
3056 "Repository",
3057 "Reviewers",
3058 "Parent issue",
3059 "Sub-issues progress",
3060 "Type",
3061];
3062
3063/// One validated [`MetadataFieldConfig`] entry.
3064#[derive(Debug, Clone)]
3065struct MetadataField {
3066 field: ProjectedField,
3067 key: ProjectedKey,
3068 path: Vec<String>,
3069}
3070
3071/// The name of a board text field a metadata value is projected onto: never blank, never one
3072/// this source or GitHub writes, and never one another entry of the same source names.
3073#[derive(Debug, Clone, PartialEq, Eq)]
3074struct ProjectedField(String);
3075
3076/// The top-level metadata key a projected value is read from: never blank, and never under
3077/// the namespace this product reserves.
3078#[derive(Debug, Clone, PartialEq, Eq)]
3079struct ProjectedKey(String);
3080
3081impl std::ops::Deref for ProjectedField {
3082 type Target = str;
3083 fn deref(&self) -> &str {
3084 &self.0
3085 }
3086}
3087
3088impl std::ops::Deref for ProjectedKey {
3089 type Target = str;
3090 fn deref(&self) -> &str {
3091 &self.0
3092 }
3093}
3094
3095/// What one item's metadata says one projected field should hold.
3096#[derive(Debug, Clone, PartialEq, Eq)]
3097enum Projected {
3098 /// A string to hold.
3099 Text(String),
3100 /// Nothing: the key or a step of the path is absent, or the value is `null`.
3101 Nothing,
3102}
3103
3104impl MetadataField {
3105 /// Validate every entry of one instance's `metadata_fields`, refusing the first that
3106 /// cannot stand, naming the source and the entry.
3107 fn resolve(
3108 configured: Vec<MetadataFieldConfig>,
3109 instance: &SourceName,
3110 ) -> Result<Vec<Self>, SourceError> {
3111 let mut resolved: Vec<Self> = Vec::with_capacity(configured.len());
3112 for (index, entry) in configured.into_iter().enumerate() {
3113 let refuse = |why: String| SourceError::Config {
3114 message: format!(
3115 "metadata_fields[{index}] of source {instance} (field {:?}, key {:?}) {why}",
3116 entry.field, entry.key
3117 ),
3118 };
3119 if entry.field.trim().is_empty() {
3120 return Err(refuse("names a blank field".to_owned()));
3121 }
3122 if entry.key.trim().is_empty() {
3123 return Err(refuse("names a blank key".to_owned()));
3124 }
3125 if entry.key.split('.').next() == Some(MetadataKey::RESERVED_NAMESPACE) {
3126 return Err(refuse(format!(
3127 "names a key under the reserved \"{}.\" prefix, which this product keeps \
3128 for its own keys; next: project a caller key instead",
3129 MetadataKey::RESERVED_NAMESPACE
3130 )));
3131 }
3132 if let Some(owned) = UNPROJECTABLE_FIELDS
3133 .iter()
3134 .find(|owned| owned.eq_ignore_ascii_case(&entry.field))
3135 {
3136 return Err(refuse(format!(
3137 "names the board field {owned:?}, which this source or GitHub already \
3138 writes; next: name a text field of your own"
3139 )));
3140 }
3141 if let Some(earlier) = resolved
3142 .iter()
3143 .position(|other| other.field.eq_ignore_ascii_case(&entry.field))
3144 {
3145 return Err(refuse(format!(
3146 "names the same board field as metadata_fields[{earlier}]; one field \
3147 cannot hold two values"
3148 )));
3149 }
3150 resolved.push(Self {
3151 field: ProjectedField(entry.field),
3152 key: ProjectedKey(entry.key),
3153 path: entry.path,
3154 });
3155 }
3156 Ok(resolved)
3157 }
3158
3159 /// The path as a refusal spells it: `[]` for the key's own value.
3160 fn spelled_path(&self) -> String {
3161 serde_json::to_string(&self.path).unwrap_or_else(|_| format!("{:?}", self.path))
3162 }
3163
3164 /// What `metadata` says this field should hold, or the refusal for a value no text field
3165 /// can hold.
3166 fn projected(
3167 &self,
3168 metadata: &BTreeMap<String, Value>,
3169 instance: &SourceName,
3170 ) -> Result<Projected, SourceError> {
3171 let mut value = metadata.get(&*self.key);
3172 for step in &self.path {
3173 value = value.and_then(|held| held.get(step.as_str()));
3174 }
3175 let found = match value {
3176 None | Some(Value::Null) => return Ok(Projected::Nothing),
3177 Some(Value::String(text)) => return Ok(Projected::Text(text.clone())),
3178 Some(Value::Bool(_)) => "a boolean",
3179 Some(Value::Number(_)) => "a number",
3180 Some(Value::Array(_)) => "an array",
3181 Some(Value::Object(_)) => "an object",
3182 };
3183 Err(SourceError::Refused {
3184 message: format!(
3185 "source {instance} projects metadata key {:?} at path {} onto its board text \
3186 field {:?}, and this item holds {found} there; only a string, null or nothing \
3187 can be written to a text field; next: store a string there, or remove the \
3188 value",
3189 &*self.key,
3190 self.spelled_path(),
3191 &*self.field
3192 ),
3193 })
3194 }
3195}
3196
3197/// What one item's `Priority` field says, read through this instance's mapping.
3198#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
3199enum HeldPriority {
3200 /// A priority this source reports: an option the mapping names, or no value (`none`).
3201 Read(Priority),
3202 /// An option the mapping does not name, which is never read as a level or as `none`.
3203 Unmapped(String),
3204}
3205
3206/// How fast this source writes, and how long it waits out a rate-limit refusal.
3207///
3208/// Configurable because a GitHub Enterprise installation sets its own limits and an
3209/// operator who has already been refused may want to go slower still — not because the
3210/// defaults are guesses.
3211#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3212#[serde(default, deny_unknown_fields)]
3213pub struct PacingConfig {
3214 /// Shortest interval between two content-creating mutations, in milliseconds.
3215 ///
3216 /// Zero sends them as fast as they are asked for, which is what a fixture server on
3217 /// loopback wants and what no board on github.com does. At most [`MAX_PACING_MS`].
3218 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.
3219 /// First wait when a rate-limit refusal carries no hint, in milliseconds. Each
3220 /// further wait of the same call doubles it. At most [`MAX_PACING_MS`], and never
3221 /// zero while there is a budget to spend, because a schedule of zero-length waits
3222 /// consumes none of it and so never ends.
3223 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.
3224 /// Total time one call may spend waiting out rate limits, in milliseconds.
3225 ///
3226 /// Zero reports the refusal rather than waiting at all. At most [`MAX_PACING_MS`]:
3227 /// the bound is what makes this a wait rather than a hang.
3228 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.
3229}
3230
3231/// The largest any pacing setting may be, in milliseconds.
3232///
3233/// One hour. GitHub's own harshest published bound on content-generating requests works
3234/// out at one every 7.2 seconds, so an hour is already three orders of magnitude past
3235/// anything a real limit asks for, and past it the settings stop describing pacing at all:
3236/// a wait budget beyond it is the unbounded wait this whole mechanism exists to replace,
3237/// and an interval beyond it is a command that never sends its second mutation. It also
3238/// keeps the clock arithmetic in [`GitHubProjectsSource::reserve_mutation_slot`] inside
3239/// what a `Duration` can hold on every platform.
3240pub const MAX_PACING_MS: u64 = 3_600_000;
3241
3242/// [`PacingConfig`] with every default resolved and every value checked, which is what the
3243/// source holds.
3244///
3245/// Unset, `min_mutation_interval_ms` spaces nothing, and every request is paced by GitHub's
3246/// secondary windows alone — see the `pacing` module; set, it spaces content-creating mutations
3247/// on top of those windows, [`MIN_MUTATION_INTERVAL_MS`] being the spacing that holds a sustained
3248/// burst under the per-minute bound by spacing alone; and zero sends every request as fast as it
3249/// is asked for, windows included.
3250#[derive(Debug, Clone, Copy)]
3251struct Pacing {
3252 /// Whether requests are held for GitHub's secondary windows: always, unless
3253 /// `min_mutation_interval_ms` is set to exactly zero, which has always asked for no
3254 /// pacing at all — what a fixture server on loopback wants and no board on github.com does.
3255 windows: bool,
3256 min_mutation_interval: Duration,
3257 retry_backoff: Duration,
3258 retry_budget: Duration,
3259}
3260
3261impl Pacing {
3262 /// Resolve one instance's pacing, refusing a configuration that would not pace at all.
3263 fn resolve(config: PacingConfig, instance: &SourceName) -> Result<Self, SourceError> {
3264 let bounded = |value: Option<u64>, default: u64, field: &str| match value {
3265 Some(value) if value > MAX_PACING_MS => Err(SourceError::Config {
3266 message: format!(
3267 "pacing.{field} of source {instance} is {value} ms, and the most any pacing \
3268 setting may be is {MAX_PACING_MS} ms — an hour, which is already far past \
3269 GitHub's own harshest published limit"
3270 ),
3271 }),
3272 Some(value) => Ok(Duration::from_millis(value)),
3273 None => Ok(Duration::from_millis(default)),
3274 };
3275 let retry_backoff = bounded(
3276 config.retry_backoff_ms,
3277 RETRY_BACKOFF_MS,
3278 "retry_backoff_ms",
3279 )?;
3280 let retry_budget = bounded(config.retry_budget_ms, RETRY_BUDGET_MS, "retry_budget_ms")?;
3281 if retry_backoff.is_zero() && !retry_budget.is_zero() {
3282 return Err(SourceError::Config {
3283 message: format!(
3284 "pacing.retry_backoff_ms of source {instance} is 0 while \
3285 pacing.retry_budget_ms is {} ms; a schedule of zero-length waits spends \
3286 none of that budget, so it would retry a refusal forever. Set a backoff of \
3287 at least 1 ms, or set retry_budget_ms to 0 to report a refusal without \
3288 waiting at all",
3289 retry_budget.as_millis()
3290 ),
3291 });
3292 }
3293 Ok(Self {
3294 windows: config.min_mutation_interval_ms != Some(0),
3295 min_mutation_interval: bounded(
3296 config.min_mutation_interval_ms,
3297 0,
3298 "min_mutation_interval_ms",
3299 )?,
3300 retry_backoff,
3301 retry_budget,
3302 })
3303 }
3304}
3305
3306/// Factory for [`GitHubProjectsSource`].
3307#[derive(Debug, Clone, Copy, Default)]
3308pub struct Plugin;
3309
3310impl SourcePlugin for Plugin {
3311 fn kind(&self) -> &'static str {
3312 KIND
3313 }
3314 /// The endpoint, the owner and the board's number: what names one board.
3315 fn remote_identity(&self, config: &Value) -> Option<String> {
3316 let config: GitHubProjectsConfig = serde_json::from_value(config.clone()).ok()?;
3317 Some(format!(
3318 "{}\n{}\n{}",
3319 config.endpoint, config.owner, config.project_number
3320 ))
3321 }
3322 fn config_schema(&self) -> Schema {
3323 schema_for!(GitHubProjectsConfig)
3324 }
3325 fn build(
3326 &self,
3327 name: &SourceName,
3328 config: &Value,
3329 secrets: &dyn SecretResolver,
3330 ) -> Result<Box<dyn TaskSource>, SourceError> {
3331 self.build_recording_into(name, config, secrets, Arc::new(Accounting::new()))
3332 }
3333
3334 fn build_with_clock(
3335 &self,
3336 name: &SourceName,
3337 config: &Value,
3338 secrets: &dyn SecretResolver,
3339 clock: SharedClock,
3340 ) -> Result<Box<dyn TaskSource>, SourceError> {
3341 self.build_recording_with_clock(name, config, secrets, Arc::new(Accounting::new()), clock)
3342 }
3343}
3344
3345/// The client `cell` holds, building it with `build` the first time it is asked for.
3346///
3347/// A failed build is the request's failure, with the message the source's construction gave it
3348/// before clients were deferred.
3349// 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`.
3350fn built(
3351 cell: &std::sync::OnceLock<Client>,
3352 build: impl FnOnce() -> Result<Client, SourceError>,
3353) -> Result<&Client, SourceError> {
3354 if let Some(client) = cell.get() {
3355 return Ok(client);
3356 }
3357 let client = build()?;
3358 Ok(cell.get_or_init(|| client))
3359}
3360
3361impl Plugin {
3362 /// Build a source recording every request it sends into an accounting the caller holds.
3363 ///
3364 /// [`SourcePlugin::build`] is this with an accounting of its own, which is what the
3365 /// registry gets. This is for a caller that is also calling GitHub itself and wants one
3366 /// session total rather than two — see [`accounting`] and
3367 /// [`GitHubProjectsSource::recording_into`].
3368 ///
3369 /// # Errors
3370 ///
3371 /// Exactly [`SourcePlugin::build`]'s, with the same source name in front of each:
3372 /// [`SourceError::Config`] for configuration this plugin cannot use and
3373 /// [`SourceError::Auth`] for a credential it cannot find.
3374 pub fn build_recording_into(
3375 &self,
3376 name: &SourceName,
3377 config: &Value,
3378 secrets: &dyn SecretResolver,
3379 ledger: Arc<Accounting>,
3380 ) -> Result<Box<dyn TaskSource>, SourceError> {
3381 self.build_recording_with_clock(name, config, secrets, ledger, system_clock())
3382 }
3383
3384 fn build_recording_with_clock(
3385 &self,
3386 name: &SourceName,
3387 config: &Value,
3388 secrets: &dyn SecretResolver,
3389 ledger: Arc<Accounting>,
3390 clock: SharedClock,
3391 ) -> Result<Box<dyn TaskSource>, SourceError> {
3392 let config: GitHubProjectsConfig =
3393 serde_json::from_value(config.clone()).map_err(|e| SourceError::Config {
3394 message: format!("source {name}: {e}"),
3395 })?;
3396 let prefix = format!("source {name}: ");
3397 let mut source = GitHubProjectsSource::recording_into(name, config, secrets, ledger)
3398 .map_err(|error| match error {
3399 // The shared `StatusMapping::distinct` names the source itself.
3400 SourceError::Config { message } if message.starts_with(&prefix) => {
3401 SourceError::Config { message }
3402 }
3403 SourceError::Config { message } => SourceError::Config {
3404 message: format!("{prefix}{message}"),
3405 },
3406 SourceError::Auth { message } => SourceError::Auth {
3407 message: format!("source {name}: {message}"),
3408 },
3409 other => other,
3410 })?;
3411 source.clock = clock;
3412 Ok(Box::new(source))
3413 }
3414}
3415
3416/// Where a status category lands on this board, once configuration is resolved.
3417#[derive(Debug, Clone, PartialEq, Eq)]
3418enum StatusTarget {
3419 /// Not usable against this instance for this kind, and why.
3420 Disabled(UnmappedStatus),
3421 /// The board's `Status` option of this name.
3422 Column(ColumnName),
3423 /// A closed issue, with both its board option and the reason that says which closed it means.
3424 // 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.
3425 Terminal(ColumnName, ClosedState),
3426}
3427
3428/// Every status category, in the order the vocabulary declares them.
3429///
3430/// This list mirrors `StatusCategory`, so it carries its own drift gate rather than a
3431/// reviewer's attention: [`category_position`] is a wildcard-free match, so a variant
3432/// added to the shared vocabulary fails to compile until it is named there, and this
3433/// crate's suite reconciles this list against that enum's own derived schema, which is
3434/// generated from the variants rather than written beside them. The schema is what
3435/// catches a list left one short — a list checking only the positions it already holds
3436/// would pass while every mapping indexed by the new position panicked.
3437pub const CATEGORIES: [StatusCategory; 8] = [
3438 StatusCategory::Draft,
3439 StatusCategory::Backlog,
3440 StatusCategory::Todo,
3441 StatusCategory::Queued,
3442 StatusCategory::InProgress,
3443 StatusCategory::Done,
3444 StatusCategory::Cancelled,
3445 StatusCategory::Unknown,
3446];
3447
3448/// Where one category sits in [`CATEGORIES`]; see that list for what this pins.
3449#[must_use]
3450pub const fn category_position(category: StatusCategory) -> usize {
3451 match category {
3452 StatusCategory::Draft => 0,
3453 StatusCategory::Backlog => 1,
3454 StatusCategory::Todo => 2,
3455 StatusCategory::Queued => 3,
3456 StatusCategory::InProgress => 4,
3457 StatusCategory::Done => 5,
3458 StatusCategory::Cancelled => 6,
3459 StatusCategory::Unknown => 7,
3460 }
3461}
3462
3463/// The spelling a status category is configured and reported under.
3464fn category_name(category: StatusCategory) -> &'static str {
3465 match category {
3466 StatusCategory::Draft => "draft",
3467 StatusCategory::Backlog => "backlog",
3468 StatusCategory::Todo => "todo",
3469 StatusCategory::Queued => "queued",
3470 StatusCategory::InProgress => "in-progress",
3471 StatusCategory::Done => "done",
3472 StatusCategory::Cancelled => "cancelled",
3473 StatusCategory::Unknown => "unknown",
3474 }
3475}
3476
3477/// A shipped default's option name.
3478///
3479/// The literals below are this file's own and non-blank, and they are validated by the
3480/// one constructor a configured name goes through rather than beside it.
3481fn shipped_column(name: &'static str) -> ColumnName {
3482 ColumnName::try_from(name.to_owned()).expect("a shipped default names a board option")
3483}
3484
3485/// The shipped default for one category this instance's `status_mapping` does not mention,
3486/// for either kind.
3487fn shipped_default(category: StatusCategory) -> StatusTarget {
3488 match category {
3489 StatusCategory::Backlog => StatusTarget::Column(shipped_column("Backlog")),
3490 StatusCategory::Todo => StatusTarget::Column(shipped_column("Todo")),
3491 StatusCategory::Queued => StatusTarget::Column(shipped_column("Queued")),
3492 StatusCategory::InProgress => StatusTarget::Column(shipped_column("In Progress")),
3493 StatusCategory::Done => {
3494 StatusTarget::Terminal(shipped_column("Done"), ClosedState::Completed)
3495 }
3496 StatusCategory::Cancelled => {
3497 StatusTarget::Terminal(shipped_column("Cancelled"), ClosedState::NotPlanned)
3498 }
3499 StatusCategory::Draft | StatusCategory::Unknown => {
3500 StatusTarget::Disabled(UnmappedStatus::Unconfigured)
3501 }
3502 }
3503}
3504
3505/// The two kinds a status is written and read for, each with its own half of the mapping.
3506const STATUS_KINDS: [ItemKind; 2] = [ItemKind::Task, ItemKind::Project];
3507
3508/// This instance's complete category-to-target mapping for each kind, read in both
3509/// directions.
3510///
3511/// One target per category per kind, held at that category's own [`category_position`], so
3512/// a category missing from the mapping, named twice in it, or filed out of order is a state
3513/// this type cannot hold rather than one [`Self::target`] has to defend against. Both kinds'
3514/// targets are options of the board's one `Status` field.
3515#[derive(Debug, Clone)]
3516struct BoardStatuses {
3517 tasks: [StatusTarget; CATEGORIES.len()],
3518 projects: [StatusTarget; CATEGORIES.len()],
3519}
3520
3521impl BoardStatuses {
3522 /// Resolve `configured` against the shipped defaults, refusing two categories one kind
3523 /// would read back from one option.
3524 ///
3525 /// A category the mapping does not mention keeps its shipped default for both kinds; one
3526 /// it does mention is exactly what it configures, so a per-kind object leaves the kind it
3527 /// omits unmapped rather than defaulted.
3528 fn resolve(configured: &StatusMapping, instance: &SourceName) -> Result<Self, SourceError> {
3529 let resolve_kind =
3530 |kind: ItemKind| -> Result<[StatusTarget; CATEGORIES.len()], SourceError> {
3531 // `CATEGORIES[position] == category` for every category — the crate's suite
3532 // asserts it — so mapping the list in order fills each category's own slot.
3533 let mut targets = CATEGORIES.map(shipped_default);
3534 for (slot, category) in targets.iter_mut().zip(CATEGORIES) {
3535 if !configured.mentions(category) {
3536 continue;
3537 }
3538 *slot = match configured.name_for(category, kind) {
3539 Err(why) => StatusTarget::Disabled(why),
3540 Ok(name) => {
3541 let option = ColumnName::try_from(name.as_str().to_owned())
3542 .map_err(|message| SourceError::Config { message })?;
3543 match category {
3544 StatusCategory::Done => {
3545 StatusTarget::Terminal(option, ClosedState::Completed)
3546 }
3547 StatusCategory::Cancelled => {
3548 StatusTarget::Terminal(option, ClosedState::NotPlanned)
3549 }
3550 _ => StatusTarget::Column(option),
3551 }
3552 }
3553 };
3554 }
3555 StatusMapping::distinct(
3556 instance,
3557 kind,
3558 CATEGORIES
3559 .iter()
3560 .zip(&targets)
3561 .filter_map(|(category, target)| target.option().map(|o| (*category, o))),
3562 )?;
3563 Ok(targets)
3564 };
3565 Ok(Self {
3566 tasks: resolve_kind(ItemKind::Task)?,
3567 projects: resolve_kind(ItemKind::Project)?,
3568 })
3569 }
3570
3571 /// Every category's target for `kind`, in category order.
3572 const fn targets(&self, kind: ItemKind) -> &[StatusTarget; CATEGORIES.len()] {
3573 match kind {
3574 ItemKind::Task => &self.tasks,
3575 ItemKind::Project => &self.projects,
3576 }
3577 }
3578
3579 fn target(&self, kind: ItemKind, category: StatusCategory) -> &StatusTarget {
3580 &self.targets(kind)[category_position(category)]
3581 }
3582
3583 /// The category a board option name reports for `kind`, or `None` when nothing of that
3584 /// kind maps to it.
3585 fn category_of(&self, kind: ItemKind, option: &str) -> Option<StatusCategory> {
3586 CATEGORIES.into_iter().find(|category| {
3587 self.target(kind, *category)
3588 .option()
3589 .is_some_and(|name| name.eq_ignore_ascii_case(option))
3590 })
3591 }
3592
3593 /// Every option name either kind maps a category to, each once ignoring case, in
3594 /// category order with a task's name before a project's — what the guarded setup asks
3595 /// the `Status` field to hold.
3596 fn wanted(&self) -> Vec<String> {
3597 let mut wanted: Vec<String> = Vec::new();
3598 for category in CATEGORIES {
3599 for kind in STATUS_KINDS {
3600 if let Some(name) = self.target(kind, category).option()
3601 && !wanted.iter().any(|held| held.eq_ignore_ascii_case(name))
3602 {
3603 wanted.push(name.to_owned());
3604 }
3605 }
3606 }
3607 wanted
3608 }
3609
3610 /// The status an item of `kind` reports, from the three things a read of it says: its
3611 /// board `Status` option, whether its issue is closed, and the reason it was closed with.
3612 ///
3613 /// **A closed issue's category follows its closure**, by [`closed_category`]: completed
3614 /// or no reason reads `done`, not planned and duplicate read `cancelled`, and any other
3615 /// reason reads `unknown`. `REOPENED`-while-closed is a state this source can never
3616 /// produce, so it is read permissively rather than refused — reads are faithful, and
3617 /// refusals belong on writes.
3618 ///
3619 /// **Its name follows the closure too, unless the option agrees with it.** The option's
3620 /// own name is kept when this kind's mapping reads that option as the same category, or
3621 /// does not name it at all — so a closed issue in a "Shipped" column reports `done`
3622 /// named `Shipped`. An option the mapping reads as another category contradicts the
3623 /// closure, and so does no option at all: the name is then the option the mapping gives
3624 /// the closure's category, so an issue closed as completed at `Todo` reads `done` named
3625 /// by the board's `done` option, and a destination reading `Todo` as todo is never handed
3626 /// it. An `unknown` closure keeps the option, or `Closed` without one.
3627 ///
3628 /// An open item's option reads through its own kind's mapping, and an option that mapping
3629 /// does not name reads as `Unknown` under its own name.
3630 ///
3631 /// One function of those three rather than of a response, so a narrow status write can
3632 /// answer what a re-read would report by applying it to the state it has just written.
3633 fn status(
3634 &self,
3635 kind: ItemKind,
3636 option: Option<&str>,
3637 closed: bool,
3638 reason: Option<&str>,
3639 ) -> Status {
3640 if closed {
3641 let category = closed_category(reason);
3642 let agrees = option.filter(|option| {
3643 category == StatusCategory::Unknown
3644 || self
3645 .category_of(kind, option)
3646 .is_none_or(|read| read == category)
3647 });
3648 let name = agrees.or_else(|| self.target(kind, category).option());
3649 let fallback = match category {
3650 StatusCategory::Done => "Done",
3651 StatusCategory::Cancelled => "Cancelled",
3652 _ => "Closed",
3653 };
3654 return Status {
3655 category,
3656 name: name.or(option).unwrap_or(fallback).to_owned(),
3657 };
3658 }
3659 let name = option.unwrap_or("Open").to_owned();
3660 Status {
3661 category: self
3662 .category_of(kind, &name)
3663 .unwrap_or(StatusCategory::Unknown),
3664 name,
3665 }
3666 }
3667
3668 /// The option a closed item's `Status` has to hold for its card to agree with its
3669 /// closure, and the category that closure reads as — or `None` when the item is open,
3670 /// its closure reads `unknown`, or its option already agrees.
3671 ///
3672 /// What `task status reconcile` writes: only `done` and `cancelled` closures are brought
3673 /// in line, each to the one option the mapping gives its category — an item with no
3674 /// option, or at one the mapping does not name, included.
3675 fn contradicted(
3676 &self,
3677 kind: ItemKind,
3678 option: Option<&str>,
3679 closed: bool,
3680 reason: Option<&str>,
3681 ) -> Option<(ClosedAs, &str)> {
3682 if !closed {
3683 return None;
3684 }
3685 let closed_as = match closed_category(reason) {
3686 StatusCategory::Done => ClosedAs::Done,
3687 StatusCategory::Cancelled => ClosedAs::Cancelled,
3688 _ => return None,
3689 };
3690 let wanted = self.target(kind, closed_as.category()).option()?;
3691 if option.is_some_and(|option| option.eq_ignore_ascii_case(wanted)) {
3692 return None;
3693 }
3694 Some((closed_as, wanted))
3695 }
3696}
3697
3698/// The category a closed issue reads as, from the reason it was closed with.
3699///
3700/// `COMPLETED`, and no reason at all, is finished work; `NOT_PLANNED` and `DUPLICATE` are both
3701/// work nobody will do here — a duplicate's work is the other issue's — and any other reason
3702/// is one this source cannot read a category from.
3703fn closed_category(reason: Option<&str>) -> StatusCategory {
3704 match reason {
3705 None | Some("COMPLETED") => StatusCategory::Done,
3706 Some("NOT_PLANNED" | "DUPLICATE") => StatusCategory::Cancelled,
3707 Some(_) => StatusCategory::Unknown,
3708 }
3709}
3710
3711impl BoardStatuses {
3712 /// For each kind, the option names it maps a category to that `existing` lacks, ignoring
3713 /// case; a kind lacking none is left out.
3714 fn missing_by_kind(&self, existing: &[StatusOption]) -> Vec<KindMissing> {
3715 STATUS_KINDS
3716 .into_iter()
3717 .filter_map(|kind| {
3718 let missing: Vec<String> = self
3719 .targets(kind)
3720 .iter()
3721 .filter_map(StatusTarget::option)
3722 .filter(|wanted| {
3723 !existing
3724 .iter()
3725 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3726 })
3727 .map(str::to_owned)
3728 .collect();
3729 (!missing.is_empty()).then_some(KindMissing { kind, missing })
3730 })
3731 .collect()
3732 }
3733}
3734
3735impl StatusTarget {
3736 /// The board option this target selects, or `None` for an unmapped one.
3737 fn option(&self) -> Option<&str> {
3738 match self {
3739 Self::Column(name) | Self::Terminal(name, _) => Some(name.as_str()),
3740 Self::Disabled(_) => None,
3741 }
3742 }
3743}
3744
3745// 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.
3746/// One repository this source can create an issue in, as `owner/name`.
3747///
3748/// Every `createIssue` this source sends names one of these: the item's own single
3749/// `repositories` entry — on a source declared private, only when it reads private — else
3750/// its parent project issue's repository, else the configured
3751/// [`GitHubProjectsConfig::repository`]. [`GitHubProjectsSource::creation_target`] makes
3752/// that choice and says what it refuses before `createIssue`.
3753// llmlint: ignore-end[comments_earn_their_place, contracts_have_one_source_or_a_drift_gate]
3754#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
3755#[serde(try_from = "String", into = "String")]
3756struct RepositoryTarget {
3757 owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
3758 name: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
3759}
3760
3761impl RepositoryTarget {
3762 fn parse(value: &str) -> Result<Self, SourceError> {
3763 let (owner, name) = value.split_once('/').ok_or_else(|| SourceError::Config {
3764 message: format!(
3765 "repository must be spelled owner/name; {value:?} names no repository"
3766 ),
3767 })?;
3768 if !valid_github_owner(owner) || !valid_github_repository_name(name) {
3769 return Err(SourceError::Config {
3770 message: format!(
3771 "repository must be spelled owner/name with a GitHub login and one \
3772 repository name; {value:?} is not"
3773 ),
3774 });
3775 }
3776 Ok(Self {
3777 owner: owner.to_owned(),
3778 name: name.to_owned(),
3779 })
3780 }
3781
3782 /// The one host whose repositories this source creates issues in, spelled once: it is
3783 /// what [`Self::origin`] renders and what [`Self::from_origin`] accepts.
3784 const HOST: &str = "github.com";
3785
3786 fn origin(&self) -> String {
3787 format!("{}/{}/{}", Self::HOST, self.owner, self.name)
3788 }
3789
3790 /// The repository a normalized origin names, or why it is none this source can create
3791 /// an issue in: another host, or more or fewer than `owner/name` under this one.
3792 fn from_origin(origin: &Repository) -> Result<Self, String> {
3793 let not_here = || {
3794 format!(
3795 "{} is not a {}/owner/name repository",
3796 origin.as_str(),
3797 Self::HOST
3798 )
3799 };
3800 let (host, rest) = origin.as_str().split_once('/').ok_or_else(not_here)?;
3801 if host != Self::HOST {
3802 return Err(not_here());
3803 }
3804 Self::parse(rest).map_err(|_| not_here())
3805 }
3806
3807 fn slug(&self) -> String {
3808 format!("{}/{}", self.owner, self.name)
3809 }
3810}
3811
3812/// Read back from a kept context as the `owner/name` it was written as, refused unless
3813/// [`RepositoryTarget::parse`] takes it.
3814impl TryFrom<String> for RepositoryTarget {
3815 type Error = String;
3816
3817 fn try_from(slug: String) -> Result<Self, Self::Error> {
3818 Self::parse(&slug).map_err(|error| error.to_string())
3819 }
3820}
3821
3822impl From<RepositoryTarget> for String {
3823 fn from(target: RepositoryTarget) -> Self {
3824 target.slug()
3825 }
3826}
3827
3828/// A source which reads GitHub afresh for every operation.
3829pub struct GitHubProjectsSource {
3830 /// This source's configured name, used both to tell a far end naming this source
3831 /// from one naming a system it knows nothing about, and to name the instance a
3832 /// status refusal is about.
3833 name: SourceName,
3834 owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after full GitHub-owner validation.
3835 project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after GraphQL-Int validation.
3836 repository: Option<RepositoryTarget>,
3837 /// The logins whose repositories a create may file in; empty restricts nothing.
3838 creation_owners: Vec<String>, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after each entry passes GitHub's login grammar.
3839 endpoint: Url,
3840 token: SecretString,
3841 credential_name: String, // llmlint: ignore[invalid_states_unrepresentable] Private diagnostic value constructed only after environment-name validation.
3842 statuses: BoardStatuses,
3843 /// Where each priority lands on this board, or `None` when this instance holds none.
3844 priorities: Option<PriorityMapping>,
3845 /// The metadata values this instance projects onto board text fields, in configured order.
3846 metadata_fields: Vec<MetadataField>,
3847 /// Built on the first request rather than with the source, as [`Self::client`] says.
3848 client: std::sync::OnceLock<Client>,
3849 /// Built on the first attachment request, as [`Self::asset_client`] says.
3850 asset_client: std::sync::OnceLock<Client>,
3851 /// The markup the engine handed this source for its bodies' headings, applied to every
3852 /// visible body it composes and stripped from every one it reads back: see
3853 /// [`TaskSource::attach_body_markup`].
3854 body_markup: std::sync::OnceLock<onetaskgraph_plugin_api::SharedBodyMarkup>,
3855 /// Every item this source has created in this command, in the order it created them —
3856 /// dropped by [`TaskSource::end_command`].
3857 ///
3858 /// GitHub's `projectV2.items` is eventually consistent: an issue added to a board with
3859 /// `addProjectV2ItemById` is routinely absent from the very next read of that board, so
3860 /// a copy resolving a dependency on an item it had just created refused it as not
3861 /// found. A board read is completed from this — an item remembered here and absent from
3862 /// the read is added back, because the board really does hold it and only the read is
3863 /// behind.
3864 ///
3865 /// It is not a cache of a user's work: nothing is remembered that this process did not
3866 /// itself just write, it lives and dies with the process, and it is never consulted for
3867 /// an item this source did not create.
3868 created: Mutex<Vec<Resolved>>,
3869 /// Every item that already existed and that this source has written in this command, as
3870 /// it wrote it — dropped by [`TaskSource::end_command`].
3871 ///
3872 /// The other half of [`Self::created`], held on the same terms and for the reason a
3873 /// narrowed read needs it: an answer from GitHub's search or from the board's own field
3874 /// filter is an index behind a write this process made moments ago, so a query matching
3875 /// what this process just wrote onto an existing item would otherwise miss it. Nothing
3876 /// is remembered that this process did not itself just write.
3877 updated: Mutex<Vec<Resolved>>,
3878 /// Every issue this source has added a comment to or edited a comment of in this command
3879 /// — dropped by [`TaskSource::end_command`].
3880 ///
3881 /// A comment-activity read is narrowed by GitHub's issue search, whose `updated:` index
3882 /// lags the write that moved an issue's `updatedAt`, and neither [`Self::created`] nor
3883 /// [`Self::updated`] is moved by a comment, so an issue this process had just commented
3884 /// on was missing from such a read — or ruled out by the `updatedAt` its own record held
3885 /// from before — until the index caught up. Each id here is a candidate of every such
3886 /// search-narrowed read, and wherever it is a candidate its comments are read rather than
3887 /// it being ruled out by a stale `updatedAt`; that read is of the issue's own node, so it
3888 /// is current. It holds ids alone: nothing of a comment is remembered. A comment another
3889 /// process wrote is still found only once the index has it.
3890 commented: Mutex<Vec<NativeId>>,
3891 /// How fast this source writes, and how long it waits out a refusal.
3892 pacing: Pacing,
3893 /// When the last content-creating mutation finished, or the moment the furthest-out
3894 /// reserved slot releases the next one, whichever is later — so the one after it can be
3895 /// spaced from that, when `pacing.min_mutation_interval_ms` asks for a spacing. See
3896 /// [`GitHubProjectsSource::paced`] for why completion rather than release is what it is
3897 /// measured from.
3898 last_mutation: Mutex<Option<Duration>>,
3899 /// GitHub's secondary windows as this source has charged them; see the `pacing` module.
3900 windows: Mutex<pacing::Windows>,
3901 /// The tasks a project holds, as a copy's batched read of the project and every one of them
3902 /// found them — see [`TaskSource::read_ahead_for_copy`] — for the length of one command, so
3903 /// a listing of that project's tasks is answered without a read of its own.
3904 task_children: Mutex<BTreeMap<NativeId, Vec<Resolved>>>,
3905 /// The web address of each issue this source created in the current command, as its
3906 /// `createIssue` answered it — what [`TaskSource::written_location`] answers, without a
3907 /// request.
3908 written: Mutex<BTreeMap<NativeId, String>>,
3909 /// What a copy has held back of the items it created, between
3910 /// [`TaskSource::begin_deferred_writes`] and [`TaskSource::finish_writes`] — `None` while
3911 /// nothing is being held back, when every write lands whole as it is made.
3912 deferred: Mutex<Option<Deferred>>,
3913 /// Each existing issue's update held back while writes are, in the order it was made: sent
3914 /// together by [`Self::send_held_updates`] before any other request goes out, so nothing
3915 /// this source sends or reads ever sees the board without them.
3916 held_updates: Mutex<Vec<HeldUpdate>>,
3917 clock: SharedClock,
3918 /// Whether the configuration declares this source private, as
3919 /// [`TaskSource::declare_visibility`] told it: what decides where a new issue whose one
3920 /// named repository reads public is created — see [`Self::creation_rule`].
3921 declared_private: std::sync::atomic::AtomicBool,
3922 numeric_repositories: tokio::sync::Mutex<BTreeMap<RepositoryTarget, std::num::NonZeroU64>>,
3923 /// The board as this process last read it, for the length of one command — dropped by
3924 /// [`TaskSource::end_command`].
3925 ///
3926 /// A copy of a project used to re-read the whole board, paged, before writing each of
3927 /// its items, which is by far the largest part of a copy's request count and none of
3928 /// its work. Nothing else changes this board while a command runs — this source's own
3929 /// writes are the only writer — so one read answers them all.
3930 ///
3931 /// It is not a store of a user's work and it is not the cache the no-persistence
3932 /// invariant forbids: it lives and dies with the process exactly as `created` does,
3933 /// nothing is written down, and [`Self::board`] still completes it from `created`, so
3934 /// an item this command created and then depends on resolves whether or not GitHub's
3935 /// own eventually-consistent read has caught up. A write to an item already on the
3936 /// board updates the entry here too, so what this holds is the last read plus this
3937 /// process's own writes rather than a snapshot taken before them.
3938 board_cache: Mutex<Option<Board>>,
3939 /// Every issue this board's own search reported, for the length of one command — dropped
3940 /// by [`TaskSource::end_command`].
3941 ///
3942 /// The second half of a board read, and cached for the same reason and on the same
3943 /// terms as the first: it lives and dies with the process, nothing is written down, and
3944 /// a write this process makes updates the entry here exactly as it updates the one in
3945 /// [`Self::board_cache`]. One read answers every question a command asks, so a command
3946 /// that lists this board's projects and its tasks pays for one search rather than two.
3947 search_cache: Mutex<Option<Vec<Resolved>>>,
3948 /// What each narrowed question GitHub was asked answered, keyed by that question, for
3949 /// the length of one command — dropped by [`TaskSource::end_command`].
3950 ///
3951 /// The narrowed counterpart of [`Self::search_cache`], held on the same terms: it lives
3952 /// and dies with the process, nothing is written down, a write this process makes
3953 /// updates the entry here as it updates the other two, and every answer is completed
3954 /// with this process's own writes each time it is given. A command that asks the same
3955 /// narrowed question twice — a wait polling for its own items, a listing repeated after a
3956 /// write — pays for it once, which is what the whole-board read it replaced gave it.
3957 narrowed_cache: Mutex<BTreeMap<String, Vec<Resolved>>>,
3958 search_next: Mutex<BTreeMap<String, Option<String>>>,
3959 /// Records already resolved in this command, reused by writes and for comment identity.
3960 /// Explicit item reads still reach GitHub. Nothing is persisted, and
3961 /// [`TaskSource::end_command`] drops every record, so a write in the next command reads
3962 /// its item as a person has since left it.
3963 resolved_cache: Mutex<BTreeMap<NativeId, Resolved>>,
3964 /// What [`TaskSource::read_ahead_for_copy`] read, by id — `None` for an id that read
3965 /// answered as holding nothing — each taken by the next read of that item by id and by
3966 /// nothing else, dropped by any mutation naming it, and cleared by
3967 /// [`TaskSource::end_command`]. So a read answered from it is the copy's own
3968 /// read-before-write, made in the same request as the others' rather than on its own.
3969 read_ahead: Mutex<BTreeMap<NativeId, Option<Resolved>>>,
3970 /// The origins [`TaskSource::read_ahead_origins_for_copy`] was asked about and has not sent
3971 /// yet: held for the [`TaskSource::read_ahead_for_copy`] that follows it, which sends them —
3972 /// beside the members of the one project it names, when it names one — or for the next
3973 /// request this source makes, whichever comes first.
3974 pending_origins: Mutex<Vec<String>>,
3975 /// Each project's members as a lookup carrying them read them in this command, trimmed to
3976 /// their classification and repositories, taken by [`TaskSource::member_classifications`]
3977 /// of that project and by nothing else, and cleared by [`TaskSource::end_command`].
3978 member_classes: Mutex<BTreeMap<NativeId, Vec<MemberClass>>>,
3979 /// Every item a listing of one project's members carried whole in this command — the
3980 /// project issue and each of its sub-issues — kept for [`TaskSource::read_ahead_for_copy`]
3981 /// alone, which takes each one it is asked for into [`Self::read_ahead`] instead of reading
3982 /// it again. No other read is answered from it, so a read by id outside a copy's
3983 /// read-before-write reads GitHub as it always did. Dropped by any mutation naming an
3984 /// item, and cleared by [`TaskSource::end_command`].
3985 carried: Mutex<BTreeMap<NativeId, Resolved>>,
3986 /// Items an issue read by reference reached, held for the one status write each was read
3987 /// for and taken out by it alone — see [`Self::take_held`] — because that read selects only
3988 /// what such a write needs. Dropped by [`TaskSource::end_command`].
3989 held_cache: Mutex<BTreeMap<NativeId, Resolved>>,
3990 /// Each project's sub-issues as GitHub answered them, keyed by the selector they were
3991 /// asked for under, with the project that selector named — for the length of one command,
3992 /// dropped by [`TaskSource::end_command`].
3993 ///
3994 /// A caller pages through a project's tasks one engine page at a time, and every page
3995 /// is cut from the whole list of them, so without this each page walked every
3996 /// [`graphql::SUB_ISSUES`] page again and a project of `n` listing pages cost `n²`
3997 /// requests to read once. Held on the terms [`Self::narrowed_cache`] is: it lives and
3998 /// dies with the process, nothing is written down, a write this process makes updates or
3999 /// removes the entry here as it does there, and every answer is completed with this
4000 /// process's own writes each time it is given.
4001 children_cache: Mutex<ProjectChildren>,
4002 /// The board's own id and field definitions as this process last read them on their
4003 /// own, for the length of one command — dropped by [`TaskSource::end_command`].
4004 ///
4005 /// What a write needs of the board and its item does not say, read once per command
4006 /// rather than once per item written, on the terms [`Self::board_cache`] is held on: it
4007 /// lives and dies with the process and nothing is written down. It holds no item and so
4008 /// can answer no question about one — see [`Self::board_fields`].
4009 fields_cache: Mutex<Option<BoardFields>>,
4010 /// Each destination repository's node id, resolved once per repository
4011 /// rather than per issue created.
4012 ///
4013 /// A repository's node id does not change, and re-reading it for every issue of a copy
4014 /// spent one request per item on an answer this source already had. It is a map rather
4015 /// than one entry because a copy files each item in the repository its own
4016 /// `repositories` field names, so a plan across five repositories asks GitHub five
4017 /// times and not once per item.
4018 repository_cache: Mutex<BTreeMap<RepositoryTarget, String>>,
4019 /// What every request this source sends is recorded into.
4020 ///
4021 /// Ordinary code path, not a mode: [`Self::send_once`] records into it at the one place
4022 /// a request leaves this crate, so nothing has to be switched on for a session to be
4023 /// counted. It is shared rather than owned so a caller accounting for a whole session —
4024 /// its own schema verification, board lookups, residue sweep and cleanup beside this
4025 /// source's reads and writes — adds up one accounting instead of two. See
4026 /// [`accounting`] for what a record carries and what a session's spend is and is not.
4027 ledger: Arc<Accounting>,
4028 /// What [`expected`] holds for a write the store's cache expects: the journal while one is
4029 /// open, the issues primed from the cache, and the comments known on each.
4030 expected: expected::Held,
4031 /// What [`projection`] holds for a producer's projection: a kept placement awaiting its
4032 /// setup-context token, and where the last create was placed.
4033 projection: projection::Held,
4034}
4035
4036/// The board item a single field update's answer names, refused unless it is `item_id`.
4037fn field_update_item<'a>(data: &'a Value, item_id: &str) -> Result<&'a Value, SourceError> {
4038 let returned = data
4039 .pointer("/updateProjectV2ItemFieldValue/projectV2Item")
4040 .ok_or_else(|| SourceError::Malformed {
4041 message: "GitHub field update returned no project item".into(),
4042 })?;
4043 if required_str(returned, "id")? != item_id {
4044 return Err(SourceError::Malformed {
4045 message: "GitHub field update returned the wrong project item".into(),
4046 });
4047 }
4048 Ok(returned)
4049}
4050
4051/// GitHub's closed single-select color vocabulary.
4052#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
4053#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
4054pub enum StatusOptionColor {
4055 /// Gray.
4056 Gray,
4057 /// Blue.
4058 Blue,
4059 /// Green.
4060 Green,
4061 /// Yellow.
4062 Yellow,
4063 /// Purple.
4064 Purple,
4065 /// Red.
4066 Red,
4067 /// Orange.
4068 Orange,
4069 /// Pink.
4070 Pink,
4071}
4072
4073/// Whether a guarded board setup — of the fields, or of the Status options alone — plans or
4074/// applies its additions.
4075#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4076pub enum SetupMode {
4077 /// Read without mutation.
4078 Plan,
4079 /// Apply and verify.
4080 Apply,
4081}
4082
4083/// The name [`SetupMode`] had when Status was the one field set up, kept so a caller written
4084/// against it goes on compiling.
4085pub type StatusOptionsMode = SetupMode;
4086
4087/// The explicit result of the requested operation.
4088#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4089#[serde(rename_all = "kebab-case")]
4090pub enum StatusOptionsOutcome {
4091 /// A read-only plan.
4092 Planned,
4093 /// Apply found nothing missing.
4094 Unchanged,
4095 /// Additions were applied and verified.
4096 Applied,
4097}
4098
4099/// A GitHub single-select option's opaque GraphQL node identifier.
4100#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4101#[serde(transparent)]
4102pub struct StatusOptionId(#[schemars(length(min = 1))] String);
4103
4104impl TryFrom<String> for StatusOptionId {
4105 type Error = String;
4106
4107 fn try_from(id: String) -> Result<Self, Self::Error> {
4108 if id.trim().is_empty() {
4109 return Err("a GitHub Status option id cannot be blank".to_owned());
4110 }
4111 Ok(Self(id))
4112 }
4113}
4114
4115/// One existing or proposed option in a guarded Status-field update.
4116#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4117pub struct StatusOption {
4118 /// GitHub's stable id.
4119 pub id: StatusOptionId,
4120 /// The visible option name.
4121 pub name: ColumnName,
4122 /// GitHub's single-select color token.
4123 pub color: StatusOptionColor,
4124 /// The option description, including an empty one.
4125 pub description: String,
4126}
4127
4128/// One board item's Status assignment, retained as recovery data.
4129#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4130pub struct StatusAssignment {
4131 /// The project item id whose assignment this is.
4132 // llmlint: ignore[invalid_states_unrepresentable] This opaque GraphQL node ID is
4133 // carried verbatim as operator recovery data; introducing a semantic type would claim
4134 // validation rules GitHub does not publish and no operation here interprets.
4135 pub item_id: String,
4136 /// The selected option, absent when the item has no status.
4137 #[serde(skip_serializing_if = "Option::is_none")]
4138 pub option: Option<AssignedStatusOption>,
4139}
4140
4141/// The inseparable id and name of an assigned option.
4142#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4143pub struct AssignedStatusOption {
4144 /// GitHub's stable id.
4145 pub id: StatusOptionId,
4146 /// The visible name.
4147 pub name: ColumnName,
4148}
4149
4150/// The plan and verified outcome of reconciling configured Status options.
4151#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4152pub struct StatusOptionsReport {
4153 /// The configured source name.
4154 pub source: SourceName,
4155 /// Configured option names absent before the operation.
4156 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a
4157 // `ColumnName` and has therefore already passed its nonblank validation; retaining the
4158 // serialized string here preserves the report's intentionally simple public contract.
4159 pub missing: Vec<String>,
4160 /// What the requested operation did.
4161 pub outcome: StatusOptionsOutcome,
4162 /// The complete option list observed before any mutation.
4163 pub existing: Vec<StatusOption>,
4164}
4165
4166#[derive(Debug, Clone, PartialEq, Eq)]
4167struct StatusSnapshot {
4168 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4169 // passed back as the mutation's project identity; a newtype could enforce no stronger
4170 // invariant because GitHub publishes no grammar for it.
4171 board_id: String,
4172 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4173 // passed back as the mutation's field identity; a newtype could enforce no stronger
4174 // invariant because GitHub publishes no grammar for it.
4175 field_id: String,
4176 options: Vec<StatusOption>,
4177 assignments: Vec<StatusAssignment>,
4178}
4179
4180/// The name of the board field a status is held in.
4181const STATUS_FIELD: &str = "Status";
4182
4183/// Every item's value of each field `report` names, as it stood before the setup wrote
4184/// anything — what a person puts back when the setup is refused part way.
4185fn recovery(report: &FieldsReport, before: &BoardSnapshot) -> Result<String, SourceError> {
4186 let assignments: BTreeMap<&str, Vec<StatusAssignment>> = report
4187 .fields
4188 .iter()
4189 .map(|field| (field.field.name(), before.assignments(field.field)))
4190 .collect();
4191 serde_json::to_string_pretty(&assignments).map_err(|error| SourceError::Malformed {
4192 message: format!("cannot render the pre-write field recovery snapshot: {error}"),
4193 })
4194}
4195
4196/// One board field the guarded setup reads and writes — every one it reads, and the only
4197/// ones it writes.
4198#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, schemars::JsonSchema)]
4199pub enum BoardField {
4200 /// The single-select `Status` field every instance's `status_mapping` resolves into.
4201 Status,
4202 /// The single-select `Priority` field an instance's `priority_mapping` resolves into.
4203 Priority,
4204}
4205
4206impl BoardField {
4207 /// The field's name on the board.
4208 #[must_use]
4209 pub const fn name(self) -> &'static str {
4210 match self {
4211 Self::Status => STATUS_FIELD,
4212 Self::Priority => PRIORITY_FIELD,
4213 }
4214 }
4215
4216 /// The field a board calls `name`, or `None` for one this setup does not own.
4217 fn named(name: &str) -> Option<Self> {
4218 [Self::Status, Self::Priority]
4219 .into_iter()
4220 .find(|field| field.name() == name)
4221 }
4222}
4223
4224/// What the guarded setup did to one field.
4225#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4226#[serde(rename_all = "kebab-case")]
4227pub enum FieldOutcome {
4228 /// A read-only plan.
4229 Planned,
4230 /// Apply found the field there with every configured option.
4231 Unchanged,
4232 /// Missing options were added to the field that was there, and verified.
4233 Applied,
4234 /// The field was not there; it was created holding the configured options, and verified.
4235 Created,
4236}
4237
4238/// One field's plan, or its verified outcome.
4239#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4240pub struct FieldReport {
4241 /// Which field.
4242 pub field: BoardField,
4243 /// Whether the board had the field before the operation.
4244 // llmlint: ignore[invalid_states_unrepresentable] `exists` beside `outcome` is the report's
4245 // wire shape as its consumer's contract fixes it — `{"field", "exists", "missing",
4246 // "outcome", "existing"}` — so folding one into the other would change a published JSON
4247 // shape. The contradictory pairings cannot be built: `GitHubProjectsSource::fields` is the
4248 // one constructor, and it derives `outcome` from `exists` in one match.
4249 pub exists: bool,
4250 /// Configured option names the field lacked before the operation — every one of them,
4251 /// in the order a new field lists them, when the field was not there at all.
4252 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
4253 // mapping name and has therefore already passed its nonblank validation; the serialized
4254 // string is the report's intentionally simple public contract, as `StatusOptionsReport`'s is.
4255 pub missing: Vec<String>,
4256 /// For the `Status` field, which item kind each missing name is configured for: one
4257 /// entry per kind `status_mapping` names a missing option for, task before project, each
4258 /// listing that kind's missing names in category order. A name both kinds use is in
4259 /// both. Empty — and left out of the JSON — when nothing is missing, and always for
4260 /// `Priority`, which only a task holds.
4261 #[serde(default, skip_serializing_if = "Vec::is_empty")]
4262 // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
4263 // both SDKs model an absent `kinds` as an empty list rather than as `null`.
4264 #[schemars(!skip_serializing_if)]
4265 pub kinds: Vec<KindMissing>,
4266 /// What the requested operation did.
4267 pub outcome: FieldOutcome,
4268 /// The field's complete option list observed before any mutation; empty when the field
4269 /// was not there.
4270 pub existing: Vec<StatusOption>,
4271}
4272
4273/// The `Status` option names one item kind's `status_mapping` names that the field lacked.
4274#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4275pub struct KindMissing {
4276 /// The kind these names are configured for.
4277 pub kind: ItemKind,
4278 /// The names that kind maps a category to and the field lacked, in category order.
4279 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
4280 // mapping name, as `FieldReport::missing`'s do, and the serialized string is the report's
4281 // intentionally simple public contract.
4282 pub missing: Vec<String>,
4283}
4284
4285/// The plan and verified outcome of setting up every field a source's configuration names.
4286#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4287pub struct FieldsReport {
4288 /// The configured source name.
4289 pub source: SourceName,
4290 /// `Status`, always, and `Priority` when the source sets `priority_mapping`.
4291 // llmlint: ignore[invalid_states_unrepresentable] A list is the report's wire shape as its
4292 // consumer's contract fixes it — `{"source", "fields": [...]}` — so a struct with one member
4293 // per field would change a published JSON shape. The states the list could hold and the
4294 // contract forbids cannot be built: `GitHubProjectsSource::fields` is the one constructor,
4295 // and it pushes `Status` first and exactly once, then `Priority` exactly when configured.
4296 pub fields: Vec<FieldReport>,
4297 /// Each board text field the source's `metadata_fields` projects a value onto, in
4298 /// configured order. Empty — and left out of the JSON — when it projects none.
4299 #[serde(default, skip_serializing_if = "Vec::is_empty")]
4300 // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
4301 // both SDKs model an absent list as an empty one rather than as `null`.
4302 #[schemars(!skip_serializing_if)]
4303 pub metadata_fields: Vec<MetadataFieldReport>,
4304}
4305
4306/// The type of a board field that is not a text field, as GitHub names it — its `dataType`, or
4307/// its GraphQL type when it has none — which is what a projected field's conflict reports.
4308///
4309/// Never empty, never beginning with whitespace, and never `TEXT`: a text field of that name is
4310/// no conflict.
4311// The schema states that rule as `NON_TEXT_FIELD_TYPE_PATTERN`, so a generated SDK model
4312// refuses exactly what `try_from` below does.
4313#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4314#[serde(transparent)]
4315pub struct NonTextFieldType(#[schemars(regex(pattern = NON_TEXT_FIELD_TYPE_PATTERN))] String);
4316
4317/// What a [`NonTextFieldType`] may be, as the emitted schema states it: a first character that
4318/// is not whitespace, and anything but exactly `TEXT` — spelled without a lookahead, which
4319/// neither SDK's validator can be relied on to support.
4320const NON_TEXT_FIELD_TYPE_PATTERN: &str =
4321 r"^(?:[^T\s]|T(?:[^E]|$)|TE(?:[^X]|$)|TEX(?:[^T]|$)|TEXT[\s\S])";
4322
4323impl TryFrom<String> for NonTextFieldType {
4324 type Error = String;
4325
4326 fn try_from(kind: String) -> Result<Self, Self::Error> {
4327 if kind.is_empty() || kind.starts_with(char::is_whitespace) {
4328 return Err("a board field's type cannot be blank or begin with whitespace".to_owned());
4329 }
4330 if kind == "TEXT" {
4331 return Err("a TEXT field is no conflict for a projected text field".to_owned());
4332 }
4333 Ok(Self(kind))
4334 }
4335}
4336
4337impl std::fmt::Display for NonTextFieldType {
4338 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4339 formatter.write_str(&self.0)
4340 }
4341}
4342
4343/// One projected metadata text field's plan, or its verified outcome.
4344///
4345/// The setup creates a missing one as a text field and never alters a field that is there:
4346/// a same-named field of another type is reported in `conflict` and left as it is.
4347#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4348pub struct MetadataFieldReport {
4349 /// The board field's name.
4350 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.
4351 /// The metadata key the value is read from.
4352 pub key: String, // llmlint: ignore[invalid_states_unrepresentable] As `field`: a validated configuration value reported back as written.
4353 /// The object keys walked inside that key's value; empty for the key's own value.
4354 pub path: Vec<String>,
4355 /// Whether the board had a text field of that name before the operation.
4356 // 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.
4357 pub exists: bool,
4358 /// The type of a field of that name that is not a text field, which the setup leaves as
4359 /// it is; absent — and left out of the JSON — when there is none.
4360 #[serde(default, skip_serializing_if = "Option::is_none")]
4361 pub conflict: Option<NonTextFieldType>,
4362 /// What the requested operation did: `planned`; `created` for a field that was not
4363 /// there; `unchanged` for one that was, and for a conflict, which is never changed.
4364 pub outcome: FieldOutcome,
4365}
4366
4367/// Which options one field is configured with, in the order a new field would list them.
4368struct FieldPlan {
4369 field: BoardField,
4370 wanted: Vec<String>,
4371}
4372
4373/// One single-select field as the guarded setup snapshots it.
4374#[derive(Debug, Clone, PartialEq, Eq)]
4375struct SnapshotField {
4376 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4377 // passed back as the mutation's field identity; a newtype could enforce no stronger
4378 // invariant because GitHub publishes no grammar for it.
4379 field_id: String,
4380 options: Vec<StatusOption>,
4381}
4382
4383/// Every single-select field of a board and every item's value of each.
4384#[derive(Debug, Clone, PartialEq, Eq)]
4385struct BoardSnapshot {
4386 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4387 // passed back as the mutation's project identity; a newtype could enforce no stronger
4388 // invariant because GitHub publishes no grammar for it.
4389 board_id: String,
4390 fields: BTreeMap<BoardField, SnapshotField>,
4391 /// Each board item's id, and its value of each field this setup owns that it holds one of.
4392 items: Vec<(String, BTreeMap<BoardField, AssignedStatusOption>)>,
4393}
4394
4395impl BoardSnapshot {
4396 /// Every item's value of `field`, in board order — the recovery data a drift refusal
4397 /// carries.
4398 fn assignments(&self, field: BoardField) -> Vec<StatusAssignment> {
4399 self.items
4400 .iter()
4401 .map(|(item_id, values)| StatusAssignment {
4402 item_id: item_id.clone(),
4403 option: values.get(&field).cloned(),
4404 })
4405 .collect()
4406 }
4407}
4408
4409impl GitHubProjectsSource {
4410 /// `content` as this source sends it, with the markup the engine handed it applied.
4411 fn marked(&self, content: &str) -> String {
4412 match self.body_markup.get() {
4413 Some(markup) if !content.is_empty() => markup.written(content),
4414 _ => content.to_owned(),
4415 }
4416 }
4417
4418 /// The visible body and the metadata slot of `body` as [`metadata_body`] splits them,
4419 /// the visible body without the markup [`Self::marked`] applies.
4420 fn visible(
4421 &self,
4422 body: Option<String>,
4423 ) -> Result<(Option<String>, BTreeMap<String, Value>), SourceError> {
4424 let (visible, slot) = metadata_body(body)?;
4425 Ok((
4426 match (visible, self.body_markup.get()) {
4427 (Some(visible), Some(markup)) => {
4428 Some(markup.read(&visible)).filter(|read| !read.is_empty())
4429 }
4430 (visible, _) => visible,
4431 },
4432 slot,
4433 ))
4434 }
4435
4436 /// Report missing configured Status options and, when `apply` is true, add them with
4437 /// a whole-list mutation that preserves every existing id and verifies the result.
4438 ///
4439 /// # Errors
4440 ///
4441 /// Refuses a board without a single-select `Status` field. A post-write difference in
4442 /// any pre-existing option id or item assignment is refused with the complete pre-write
4443 /// assignment snapshot in the diagnostic for recovery.
4444 // llmlint: ignore[changed_behavior_has_e2e] The CLI journeys cover plan, no-op apply,
4445 // successful mutation, both drift refusals, source selection, missing Status, casing,
4446 // and paging. Transport errors remain the shared `graphql` boundary's behavior rather
4447 // than a new status-options behavior, and the pinned-schema test prevents valid GitHub
4448 // responses from entering the defensive malformed-response branches below.
4449 pub async fn status_options(
4450 &self,
4451 mode: StatusOptionsMode,
4452 ) -> Result<StatusOptionsReport, SourceError> {
4453 let before = self.status_snapshot().await?;
4454 // A terminal category's option is as configured as an open one's: a terminal
4455 // write validates it before closing and refuses when the board lacks it. Both
4456 // kinds' names are options of the one field, so both are asked for.
4457 let missing = self
4458 .statuses
4459 .wanted()
4460 .into_iter()
4461 .filter(|wanted| {
4462 !before
4463 .options
4464 .iter()
4465 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4466 })
4467 .collect::<Vec<_>>();
4468 let report = StatusOptionsReport {
4469 source: self.name.clone(),
4470 missing: missing.clone(),
4471 outcome: match (mode, missing.is_empty()) {
4472 (StatusOptionsMode::Plan, _) => StatusOptionsOutcome::Planned,
4473 (StatusOptionsMode::Apply, true) => StatusOptionsOutcome::Unchanged,
4474 (StatusOptionsMode::Apply, false) => StatusOptionsOutcome::Applied,
4475 },
4476 existing: before.options.clone(),
4477 };
4478 if mode == StatusOptionsMode::Plan || missing.is_empty() {
4479 return Ok(report);
4480 }
4481 let mut options = before
4482 .options
4483 .iter()
4484 .map(|option| {
4485 json!({
4486 "id": option.id, "name": option.name, "color": option.color,
4487 "description": option.description,
4488 })
4489 })
4490 .collect::<Vec<_>>();
4491 options.extend(missing.iter().map(|name| {
4492 json!({
4493 "name": name, "color": "GRAY", "description": ""
4494 })
4495 }));
4496 self.graphql(
4497 graphql::STATUS_OPTIONS_UPDATE,
4498 json!({"input": {
4499 "fieldId": before.field_id,
4500 "singleSelectOptions": options,
4501 }}),
4502 )
4503 .await?;
4504 let after = self.status_snapshot().await?;
4505 let options_preserved = before
4506 .options
4507 .iter()
4508 .all(|old| after.options.iter().any(|new| new == old));
4509 let additions_present = missing.iter().all(|wanted| {
4510 after
4511 .options
4512 .iter()
4513 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4514 });
4515 if !options_preserved || !additions_present || after.assignments != before.assignments {
4516 let recovery = serde_json::to_string_pretty(&before.assignments).map_err(|error| {
4517 SourceError::Malformed {
4518 message: format!("cannot render pre-write Status recovery snapshot: {error}"),
4519 }
4520 })?;
4521 return Err(SourceError::Refused {
4522 message: format!(
4523 "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}"
4524 ),
4525 });
4526 }
4527 Ok(report)
4528 }
4529
4530 /// A fresh snapshot of the Status field and every board item's assignment of it.
4531 ///
4532 /// # Errors
4533 ///
4534 /// Refuses a board without a single-select `Status` field, and one the token cannot see.
4535 async fn status_snapshot(&self) -> Result<StatusSnapshot, SourceError> {
4536 // Status alone, as this operation has always read it: a `Priority` field is another
4537 // operation's, so nothing about it can refuse this one.
4538 let mut board = self.board_snapshot(&[BoardField::Status]).await?;
4539 let field = board
4540 .fields
4541 .remove(&BoardField::Status)
4542 .ok_or_else(|| self.no_status_field())?;
4543 Ok(StatusSnapshot {
4544 assignments: board.assignments(BoardField::Status),
4545 board_id: board.board_id,
4546 field_id: field.field_id,
4547 options: field.options,
4548 })
4549 }
4550
4551 /// The refusal a board with no `Status` field is answered with by the guarded setup.
4552 fn no_status_field(&self) -> SourceError {
4553 SourceError::Refused {
4554 message: format!("source {} board has no Status field", self.name),
4555 }
4556 }
4557
4558 // llmlint: ignore-block[changed_behavior_has_e2e] Valid snapshot shapes are exercised through
4559 // the real CLI loopback journey, including pagination. The individual malformed guards
4560 // are defensive validation of a schema-pinned third-party response, not separate user
4561 // journeys; drift and missing-field failures cover the operation's recovery behavior.
4562 /// A fresh snapshot of each of the `owned` fields on the board, with its options, and of
4563 /// every board item's value of each, walked to the end of the board's items. A field not
4564 /// in `owned` is read past whatever it holds.
4565 async fn board_snapshot(&self, owned: &[BoardField]) -> Result<BoardSnapshot, SourceError> {
4566 let mut after: Option<String> = None;
4567 let mut snapshot: Option<BoardSnapshot> = None;
4568 loop {
4569 let data = self
4570 .graphql(
4571 graphql::STATUS_OPTIONS_SNAPSHOT,
4572 json!({
4573 "owner": self.owner, "number": self.project_number,
4574 "first": MAX_PAGE_SIZE, "after": after, "nestedFirst": MAX_PAGE_SIZE,
4575 }),
4576 )
4577 .await?;
4578 let board = data
4579 .pointer("/owner/projectV2")
4580 .filter(|board| board.is_object())
4581 .ok_or_else(|| SourceError::Refused {
4582 message: format!(
4583 "source {} has no accessible GitHub Projects board",
4584 self.name
4585 ),
4586 })?;
4587 if board
4588 .pointer("/fields/pageInfo/hasNextPage")
4589 .and_then(Value::as_bool)
4590 != Some(false)
4591 {
4592 return Err(SourceError::Malformed {
4593 message:
4594 "GitHub project fields is incomplete or has malformed pageInfo.hasNextPage"
4595 .into(),
4596 });
4597 }
4598 let mut fields = BTreeMap::new();
4599 // Only the fields this setup owns, by name: a node the single-select fragment did not
4600 // match carries no name, and a person's own single-select field — a `Size`, a
4601 // `Team` — is none of this setup's business, so nothing about it can refuse one. A
4602 // `Status` or `Priority` field without its options is malformed, not absent.
4603 // 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.
4604 for (owned, field) in board
4605 .pointer("/fields/nodes")
4606 .and_then(Value::as_array)
4607 .ok_or_else(|| SourceError::Malformed {
4608 message: "GitHub project fields.nodes is not an array".into(),
4609 })?
4610 .iter()
4611 .filter_map(|field| {
4612 let named = BoardField::named(field.get("name")?.as_str()?)?;
4613 owned.contains(&named).then_some((named, field))
4614 })
4615 {
4616 let options = field
4617 .get("options")
4618 .and_then(Value::as_array)
4619 .ok_or_else(|| SourceError::Malformed {
4620 message: "GitHub single-select field options is not an array".into(),
4621 })?
4622 .iter()
4623 .map(|option| {
4624 Ok(StatusOption {
4625 id: StatusOptionId::try_from(required_str(option, "id")?.to_owned())
4626 .map_err(|message| SourceError::Malformed { message })?,
4627 name: ColumnName::try_from(required_str(option, "name")?.to_owned())
4628 .map_err(|message| SourceError::Malformed {
4629 message: format!(
4630 "GitHub single-select option name is invalid: {message}"
4631 ),
4632 })?,
4633 color: serde_json::from_value(
4634 option.get("color").cloned().unwrap_or(Value::Null),
4635 )
4636 .map_err(|error| {
4637 SourceError::Malformed {
4638 message: format!(
4639 "GitHub single-select option color is invalid: {error}"
4640 ),
4641 }
4642 })?,
4643 description: optional_str(option, "description")?
4644 .unwrap_or_default()
4645 .to_owned(),
4646 })
4647 })
4648 .collect::<Result<Vec<_>, SourceError>>()?;
4649 let snapshot = SnapshotField {
4650 field_id: required_nonblank_str(field, "id")?.to_owned(),
4651 options,
4652 };
4653 // A board's field names are unique, so a second one is an answer that cannot
4654 // say which field the setup would act on — refused rather than one chosen.
4655 if fields.insert(owned, snapshot).is_some() {
4656 return Err(SourceError::Malformed {
4657 message: format!(
4658 "GitHub answered two {} fields for this board",
4659 owned.name()
4660 ),
4661 });
4662 }
4663 }
4664 let board_id = required_nonblank_str(board, "id")?.to_owned();
4665 let current = snapshot.get_or_insert_with(|| BoardSnapshot {
4666 board_id,
4667 fields,
4668 items: Vec::new(),
4669 });
4670 let items = board
4671 .pointer("/items/nodes")
4672 .and_then(Value::as_array)
4673 .ok_or_else(|| SourceError::Malformed {
4674 message: "GitHub project items.nodes is not an array".into(),
4675 })?;
4676 for item in items {
4677 let field_values =
4678 item.get("fieldValues")
4679 .ok_or_else(|| SourceError::Malformed {
4680 message: "GitHub project item is missing fieldValues".into(),
4681 })?;
4682 if field_values
4683 .pointer("/pageInfo/hasNextPage")
4684 .and_then(Value::as_bool)
4685 != Some(false)
4686 {
4687 return Err(SourceError::Malformed {
4688 message: "GitHub project item fieldValues is incomplete or has malformed pageInfo.hasNextPage".into(),
4689 });
4690 }
4691 let values = item
4692 .pointer("/fieldValues/nodes")
4693 .and_then(Value::as_array)
4694 .ok_or_else(|| SourceError::Malformed {
4695 message: "GitHub project item fieldValues.nodes is not an array".into(),
4696 })?;
4697 let item_id = required_nonblank_str(item, "id")?;
4698 let mut assigned = BTreeMap::new();
4699 for value in values {
4700 let Some(field) = value
4701 .pointer("/field/name")
4702 .and_then(Value::as_str)
4703 .and_then(BoardField::named)
4704 .filter(|field| owned.contains(field))
4705 else {
4706 continue;
4707 };
4708 let held = assigned.insert(
4709 field,
4710 AssignedStatusOption {
4711 id: StatusOptionId::try_from(
4712 required_str(value, "optionId")?.to_owned(),
4713 )
4714 .map_err(|message| SourceError::Malformed { message })?,
4715 name: ColumnName::try_from(required_str(value, "name")?.to_owned())
4716 .map_err(|message| SourceError::Malformed {
4717 message: format!(
4718 "GitHub assigned {} name is invalid: {message}",
4719 field.name()
4720 ),
4721 })?,
4722 },
4723 );
4724 // An item holds one value of a field, so a second one leaves no way to
4725 // tell which it holds — and a verification or recovery built on either
4726 // could restore the wrong one.
4727 if held.is_some() {
4728 return Err(SourceError::Malformed {
4729 message: format!(
4730 "GitHub answered two {} values for board item {item_id}",
4731 field.name()
4732 ),
4733 });
4734 }
4735 }
4736 current.items.push((item_id.to_owned(), assigned));
4737 }
4738 let page = board.get("items").ok_or_else(|| SourceError::Malformed {
4739 message: "GitHub project is missing items".into(),
4740 })?;
4741 let has_next = page
4742 .pointer("/pageInfo/hasNextPage")
4743 .and_then(Value::as_bool)
4744 .ok_or_else(|| SourceError::Malformed {
4745 message: "GitHub project items.pageInfo.hasNextPage is not a boolean".into(),
4746 })?;
4747 if !has_next {
4748 break;
4749 }
4750 let next =
4751 required_nonblank_str(page.get("pageInfo").unwrap_or(&Value::Null), "endCursor")?;
4752 validate_cursor_progress(after.as_deref(), next)?;
4753 after = Some(next.to_owned());
4754 }
4755 snapshot.ok_or_else(|| SourceError::Malformed {
4756 message: "GitHub returned no board field snapshot".into(),
4757 })
4758 }
4759 // llmlint: ignore-end[changed_behavior_has_e2e]
4760
4761 /// Report every board field this source's configuration names and, with
4762 /// [`SetupMode::Apply`], set each up: add the options a field lacks, and create
4763 /// the `Priority` field when the board has none.
4764 ///
4765 /// The fields are `Status`, always, with the options `status_mapping` resolves to; and
4766 /// `Priority`, when `priority_mapping` is set, with its four mapped options — created in
4767 /// the order urgent, high, medium, low. An option a field already has keeps its id, name,
4768 /// color and description: the whole option list goes back with every existing id, because
4769 /// a re-minted id clears every item's value.
4770 ///
4771 /// # Errors
4772 ///
4773 /// Refuses a board without a single-select `Status` field. After an apply the board is
4774 /// read again, and a pre-existing option or any item's value of either field that moved is
4775 /// refused with the complete pre-write assignments in the diagnostic, for recovery.
4776 // llmlint: ignore[changed_behavior_has_e2e] The `sources fields` journeys drive plan,
4777 // unchanged apply, a created field, an added option to each field, drift refusal, a board
4778 // with no Status field and a non-github-projects source through the compiled CLI against
4779 // the loopback board. Transport errors are the shared `graphql` boundary's behavior.
4780 pub async fn fields(&self, mode: SetupMode) -> Result<FieldsReport, SourceError> {
4781 let owned: Vec<BoardField> = if self.priorities.is_some() {
4782 vec![BoardField::Status, BoardField::Priority]
4783 } else {
4784 vec![BoardField::Status]
4785 };
4786 let before = self.board_snapshot(&owned).await?;
4787 let mut plans = vec![FieldPlan {
4788 field: BoardField::Status,
4789 wanted: self.statuses.wanted(),
4790 }];
4791 if !before.fields.contains_key(&BoardField::Status) {
4792 return Err(self.no_status_field());
4793 }
4794 if let Some(mapping) = &self.priorities {
4795 plans.push(FieldPlan {
4796 field: BoardField::Priority,
4797 wanted: mapping.names().map(str::to_owned).collect(),
4798 });
4799 }
4800 // The snapshot reads single-select fields alone, so a field it did not find may still
4801 // be on the board under the name, of another type: creating one beside it would fail
4802 // part way, or leave two fields of one name. Asked of the board's own field list, and
4803 // only when a field is missing.
4804 if plans
4805 .iter()
4806 .any(|plan| !before.fields.contains_key(&plan.field))
4807 {
4808 let board = self.board_fields().await?;
4809 for plan in plans
4810 .iter()
4811 .filter(|plan| !before.fields.contains_key(&plan.field))
4812 {
4813 if let Some(field) = Board::field(&board.fields, plan.field.name())? {
4814 return Err(SourceError::Refused {
4815 message: format!(
4816 "source {}'s board has a {} field that is not a single-select field \
4817 (it is a {}), so it cannot hold this source's options; next: rename \
4818 or remove that field, then run this again",
4819 self.name,
4820 plan.field.name(),
4821 optional_str(field, "__typename")?.unwrap_or("field of another type")
4822 ),
4823 });
4824 }
4825 }
4826 }
4827 let mut reports = Vec::new();
4828 for plan in &plans {
4829 let held = before.fields.get(&plan.field);
4830 let existing = held.map(|field| field.options.clone()).unwrap_or_default();
4831 let mut missing: Vec<String> = Vec::new();
4832 for wanted in &plan.wanted {
4833 let present = existing
4834 .iter()
4835 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4836 || missing
4837 .iter()
4838 .any(|named| named.eq_ignore_ascii_case(wanted));
4839 if !present {
4840 missing.push(wanted.clone());
4841 }
4842 }
4843 let kinds = match plan.field {
4844 BoardField::Status => self.statuses.missing_by_kind(&existing),
4845 BoardField::Priority => Vec::new(),
4846 };
4847 reports.push(FieldReport {
4848 field: plan.field,
4849 exists: held.is_some(),
4850 kinds,
4851 outcome: match (mode, held.is_some(), missing.is_empty()) {
4852 (SetupMode::Plan, _, _) => FieldOutcome::Planned,
4853 (SetupMode::Apply, true, true) => FieldOutcome::Unchanged,
4854 (SetupMode::Apply, true, false) => FieldOutcome::Applied,
4855 (SetupMode::Apply, false, _) => FieldOutcome::Created,
4856 },
4857 missing,
4858 existing,
4859 });
4860 }
4861 // A projected field is a text field, which the single-select snapshot cannot see, so
4862 // it is asked of the board's own field list — and only when one is configured.
4863 let mut metadata_fields = Vec::new();
4864 if !self.metadata_fields.is_empty() {
4865 let board = self.board_fields().await?;
4866 for projection in &self.metadata_fields {
4867 let conflict = match Board::field(&board.fields, &projection.field)? {
4868 None => None,
4869 Some(field) => {
4870 let typename = optional_str(field, "__typename")?;
4871 let data_type = optional_str(field, "dataType")?;
4872 if typename == Some("ProjectV2Field") && data_type == Some("TEXT") {
4873 None
4874 } else {
4875 let named = data_type
4876 .filter(|kind| *kind != "TEXT")
4877 .or(typename)
4878 .unwrap_or("field of another type");
4879 Some(NonTextFieldType::try_from(named.to_owned()).map_err(
4880 |message| SourceError::Malformed {
4881 message: format!(
4882 "GitHub answered the board's {} field with a type this \
4883 setup cannot read: {message}",
4884 &*projection.field
4885 ),
4886 },
4887 )?)
4888 }
4889 }
4890 };
4891 let exists =
4892 conflict.is_none() && Board::field(&board.fields, &projection.field)?.is_some();
4893 metadata_fields.push(MetadataFieldReport {
4894 field: projection.field.to_string(),
4895 key: projection.key.to_string(),
4896 path: projection.path.clone(),
4897 exists,
4898 outcome: match (mode, exists || conflict.is_some()) {
4899 (SetupMode::Plan, _) => FieldOutcome::Planned,
4900 (SetupMode::Apply, true) => FieldOutcome::Unchanged,
4901 (SetupMode::Apply, false) => FieldOutcome::Created,
4902 },
4903 conflict,
4904 });
4905 }
4906 }
4907 let report = FieldsReport {
4908 source: self.name.clone(),
4909 fields: reports,
4910 metadata_fields,
4911 };
4912 let writes: Vec<&FieldReport> = report
4913 .fields
4914 .iter()
4915 .filter(|field| !field.missing.is_empty() || !field.exists)
4916 .collect();
4917 let creates: Vec<&MetadataFieldReport> = report
4918 .metadata_fields
4919 .iter()
4920 .filter(|field| field.outcome == FieldOutcome::Created)
4921 .collect();
4922 if mode == SetupMode::Plan || (writes.is_empty() && creates.is_empty()) {
4923 return Ok(report);
4924 }
4925 let mut landed: Vec<&str> = Vec::new();
4926 for field in &writes {
4927 let added = field
4928 .missing
4929 .iter()
4930 .map(|name| json!({"name": name, "color": "GRAY", "description": ""}));
4931 let sent = match before.fields.get(&field.field) {
4932 Some(held) => {
4933 let mut options = held
4934 .options
4935 .iter()
4936 .map(|option| {
4937 json!({
4938 "id": option.id, "name": option.name, "color": option.color,
4939 "description": option.description,
4940 })
4941 })
4942 .collect::<Vec<_>>();
4943 options.extend(added);
4944 self.graphql(
4945 graphql::STATUS_OPTIONS_UPDATE,
4946 json!({"input": {
4947 "fieldId": held.field_id,
4948 "singleSelectOptions": options,
4949 }}),
4950 )
4951 .await
4952 }
4953 None => {
4954 self.graphql(
4955 graphql::CREATE_FIELD,
4956 json!({"input": {
4957 "projectId": before.board_id, "dataType": "SINGLE_SELECT",
4958 "name": field.field.name(),
4959 "singleSelectOptions": added.collect::<Vec<_>>(),
4960 }}),
4961 )
4962 .await
4963 }
4964 };
4965 // A mutation that failed does not establish that GitHub left its field as it was,
4966 // so every failure from here on carries the recovery data a drift refusal does.
4967 match sent {
4968 Ok(_) => landed.push(field.field.name()),
4969 Err(error) => {
4970 let changed = if landed.is_empty() {
4971 String::new()
4972 } else {
4973 format!("changed the {} field and then ", landed.join(" and "))
4974 };
4975 return Err(SourceError::Refused {
4976 message: format!(
4977 "the guarded field setup {changed}failed on the {} field, which it may \
4978 have changed part way: {error}; the pre-write item assignments \
4979 are:\n{}",
4980 field.field.name(),
4981 recovery(&report, &before)?
4982 ),
4983 });
4984 }
4985 }
4986 }
4987 for field in &creates {
4988 let sent = self
4989 .graphql(
4990 graphql::CREATE_FIELD,
4991 json!({"input": {
4992 "projectId": before.board_id, "dataType": "TEXT", "name": field.field,
4993 }}),
4994 )
4995 .await;
4996 if let Err(error) = sent {
4997 let changed = if landed.is_empty() {
4998 String::new()
4999 } else {
5000 format!("changed the {} field and then ", landed.join(" and "))
5001 };
5002 return Err(SourceError::Refused {
5003 message: format!(
5004 "the guarded field setup {changed}failed creating the {} text field: \
5005 {error}; run it again to create what is still missing",
5006 field.field
5007 ),
5008 });
5009 }
5010 landed.push(&field.field);
5011 }
5012 // The board has been written, so a verification read that fails leaves it unverified
5013 // rather than unchanged, and says what to put back.
5014 let after = match self.board_snapshot(&owned).await {
5015 Ok(after) => after,
5016 Err(error) => {
5017 return Err(SourceError::Refused {
5018 message: format!(
5019 "the guarded field setup changed the {} field and then could not read the \
5020 board back to verify it: {error}; the pre-write item assignments are:\n{}",
5021 landed.join(" and "),
5022 recovery(&report, &before)?
5023 ),
5024 });
5025 }
5026 };
5027 let mut moved = Vec::new();
5028 for field in &report.fields {
5029 let name = field.field.name();
5030 let now = after
5031 .fields
5032 .get(&field.field)
5033 .map(|held| held.options.as_slice())
5034 .unwrap_or_default();
5035 if !field.existing.iter().all(|old| now.contains(old)) {
5036 moved.push(format!(
5037 "a pre-existing {name} option id, name, color or description"
5038 ));
5039 }
5040 if !field.missing.iter().all(|wanted| {
5041 now.iter()
5042 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
5043 }) {
5044 moved.push(format!("an added {name} option"));
5045 }
5046 if after.assignments(field.field) != before.assignments(field.field) {
5047 moved.push(format!("an item's {name} value"));
5048 }
5049 }
5050 if !creates.is_empty() {
5051 // Read afresh: the view held for this command is the one from before the creates.
5052 *self.fields_cache()? = None;
5053 let board = match self.board_fields().await {
5054 Ok(board) => board,
5055 Err(error) => {
5056 return Err(SourceError::Refused {
5057 message: format!(
5058 "the guarded field setup changed the {} field and then could not \
5059 read the board back to verify it: {error}; run it again to verify \
5060 it, which creates nothing a second time",
5061 landed.join(" and ")
5062 ),
5063 });
5064 }
5065 };
5066 for field in &creates {
5067 let text = Board::field(&board.fields, &field.field)?.is_some_and(|held| {
5068 held.get("__typename").and_then(Value::as_str) == Some("ProjectV2Field")
5069 && held.get("dataType").and_then(Value::as_str) == Some("TEXT")
5070 });
5071 if !text {
5072 moved.push(format!("the created {} text field", field.field));
5073 }
5074 }
5075 }
5076 if !moved.is_empty() {
5077 return Err(SourceError::Refused {
5078 message: format!(
5079 "GitHub changed {} after the guarded field setup; the pre-write item \
5080 assignments are:\n{}",
5081 moved.join(", "),
5082 recovery(&report, &before)?
5083 ),
5084 });
5085 }
5086 Ok(report)
5087 }
5088
5089 /// Validate configuration and capture the named credential without exposing it.
5090 ///
5091 /// # Errors
5092 ///
5093 /// Returns [`SourceError::Config`] for a configuration this instance cannot use and
5094 /// [`SourceError::Auth`] when the named credential is missing or empty.
5095 pub fn new(
5096 name: &SourceName,
5097 config: GitHubProjectsConfig,
5098 secrets: &dyn SecretResolver,
5099 ) -> Result<Self, SourceError> {
5100 Self::recording_into(name, config, secrets, Arc::new(Accounting::new()))
5101 }
5102
5103 /// The same, recording every request it sends into an accounting the caller holds too.
5104 ///
5105 /// [`Self::new`] is this with an accounting of its own. A caller that is also making
5106 /// its own calls to GitHub — a lane verifying a schema, sweeping residue or cleaning
5107 /// up — passes the one it records those into, so the session total accounts for the
5108 /// whole session rather than for this source's share of it.
5109 ///
5110 /// # Errors
5111 ///
5112 /// Exactly [`Self::new`]'s: [`SourceError::Config`] for a configuration this instance
5113 /// cannot use and [`SourceError::Auth`] when the named credential is missing or empty.
5114 pub fn recording_into(
5115 name: &SourceName,
5116 config: GitHubProjectsConfig,
5117 secrets: &dyn SecretResolver,
5118 ledger: Arc<Accounting>,
5119 ) -> Result<Self, SourceError> {
5120 if !valid_github_owner(&config.owner) {
5121 return Err(SourceError::Config {
5122 message: "owner must be 1-39 ASCII letters, digits, or single hyphens, and cannot start or end with a hyphen".into(),
5123 });
5124 }
5125 if config.project_number == 0 || config.project_number > i32::MAX as u32 {
5126 return Err(SourceError::Config {
5127 message: format!("project_number must be between 1 and {}", i32::MAX),
5128 });
5129 }
5130 if !valid_environment_name(&config.token_env) {
5131 return Err(SourceError::Config {
5132 message: "token_env must be a valid environment-variable name".into(),
5133 });
5134 }
5135 let repository = config
5136 .repository
5137 .as_deref()
5138 .map(RepositoryTarget::parse)
5139 .transpose()?;
5140 if let Some(entry) = config
5141 .creation_owners
5142 .iter()
5143 .find(|entry| !valid_github_owner(entry))
5144 {
5145 return Err(SourceError::Config {
5146 message: format!(
5147 "creation_owners entry {entry:?} is not a GitHub login: a login is 1-39 ASCII \
5148 letters, digits, or single hyphens, and cannot start or end with a hyphen"
5149 ),
5150 });
5151 }
5152 let endpoint = Url::parse(&config.endpoint).map_err(|e| SourceError::Config {
5153 message: format!("endpoint is not a valid URL: {e}"),
5154 })?;
5155 if endpoint.scheme() != "https"
5156 && !(endpoint.scheme() == "http"
5157 && endpoint
5158 .host_str()
5159 .is_some_and(|h| h == "127.0.0.1" || h == "localhost" || h == "::1"))
5160 {
5161 return Err(SourceError::Config {
5162 message:
5163 "endpoint must use HTTPS (HTTP is accepted only for a loopback test server)"
5164 .into(),
5165 });
5166 }
5167 let token = secrets.get(&config.token_env).filter(|token| !token.expose_secret().trim().is_empty()).ok_or_else(|| SourceError::Auth {
5168 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),
5169 })?;
5170 Ok(Self {
5171 name: name.clone(),
5172 owner: config.owner,
5173 project_number: config.project_number,
5174 repository,
5175 creation_owners: config.creation_owners,
5176 asset_client: std::sync::OnceLock::new(),
5177 body_markup: std::sync::OnceLock::new(),
5178 endpoint,
5179 token,
5180 credential_name: config.token_env,
5181 statuses: BoardStatuses::resolve(&config.status_mapping, name)?,
5182 priorities: config
5183 .priority_mapping
5184 .map(|mapping| PriorityMapping::resolve(mapping, name))
5185 .transpose()?,
5186 metadata_fields: MetadataField::resolve(config.metadata_fields, name)?,
5187 client: std::sync::OnceLock::new(),
5188 created: Mutex::new(Vec::new()),
5189 updated: Mutex::new(Vec::new()),
5190 commented: Mutex::new(Vec::new()),
5191 pacing: Pacing::resolve(config.pacing, name)?,
5192 last_mutation: Mutex::new(None),
5193 windows: Mutex::new(pacing::Windows::default()),
5194 task_children: Mutex::new(BTreeMap::new()),
5195 written: Mutex::new(BTreeMap::new()),
5196 deferred: Mutex::new(None),
5197 held_updates: Mutex::new(Vec::new()),
5198 clock: system_clock(),
5199 declared_private: std::sync::atomic::AtomicBool::new(false),
5200 numeric_repositories: tokio::sync::Mutex::new(BTreeMap::new()),
5201 board_cache: Mutex::new(None),
5202 search_cache: Mutex::new(None),
5203 narrowed_cache: Mutex::new(BTreeMap::new()),
5204 resolved_cache: Mutex::new(BTreeMap::new()),
5205 read_ahead: Mutex::new(BTreeMap::new()),
5206 pending_origins: Mutex::new(Vec::new()),
5207 member_classes: Mutex::new(BTreeMap::new()),
5208 carried: Mutex::new(BTreeMap::new()),
5209 held_cache: Mutex::new(BTreeMap::new()),
5210 children_cache: Mutex::new(BTreeMap::new()),
5211 search_next: Mutex::new(BTreeMap::new()),
5212 fields_cache: Mutex::new(None),
5213 repository_cache: Mutex::new(BTreeMap::new()),
5214 ledger,
5215 expected: expected::Held::default(),
5216 projection: projection::Held::default(),
5217 })
5218 }
5219
5220 /// The client every API request goes through, built on the first one.
5221 ///
5222 /// Not with the source: building a client loads the system's certificate store, which
5223 /// costs tens of milliseconds, and a command that configures this board but never sends to
5224 /// it — a read of another source — should not pay that in its process.
5225 fn client(&self) -> Result<&Client, SourceError> {
5226 built(&self.client, || {
5227 Client::builder()
5228 .user_agent("onetaskgraph")
5229 .build()
5230 .map_err(|e| SourceError::Config {
5231 message: format!("cannot build HTTP client: {e}"),
5232 })
5233 })
5234 }
5235
5236 /// The client attachment requests go through, with its own redirect policy, built on the
5237 /// first one for the reason [`Self::client`] gives.
5238 fn asset_client(&self) -> Result<&Client, SourceError> {
5239 built(&self.asset_client, || assets::client(&self.endpoint))
5240 }
5241
5242 /// A snapshot of every request this source has sent, and what each cost.
5243 ///
5244 /// A value to hold and compare rather than a borrow of the accounting itself, so two
5245 /// of them can sit side by side. When this source was built with
5246 /// [`Self::recording_into`] the snapshot is the whole shared session, which is the
5247 /// point of building it that way.
5248 #[must_use]
5249 pub fn accounting(&self) -> accounting::Session {
5250 self.ledger.snapshot()
5251 }
5252
5253 /// Send one GraphQL document, pacing this source's own mutations and waiting out a
5254 /// rate limit rather than handing it straight back as an error.
5255 ///
5256 /// Retrying is safe for every document here, including the mutations, and the reason
5257 /// is that only a *refusal* is retried: [`Limiter::classify`] rules on a response
5258 /// GitHub sent, and a request GitHub refused for a rate limit did not run, so nothing
5259 /// this replays has already taken effect. An outcome this source cannot know — the
5260 /// send failed, or the body could not be read, so the mutation may well have landed —
5261 /// is [`Attempt::Failed`] in [`send_once`] and leaves this loop without a second
5262 /// attempt. A duplicate write would come from replaying one of those, and none is
5263 /// replayed.
5264 async fn graphql(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
5265 // Updates held back go out before any other request, so whatever it reads or writes
5266 // sees the board as though each had been sent when it was made.
5267 Box::pin(self.send_held_updates()).await?;
5268 // Origins held back for a read ahead that never came are sent before anything else.
5269 if query != graphql::ORIGIN_LOOKUPS && query != graphql::ORIGIN_LOOKUPS_WITH_MEMBERS {
5270 Box::pin(self.send_pending_origins(None)).await?;
5271 }
5272 if !self.expected.journaling() {
5273 return self.graphql_sent(query, variables).await;
5274 }
5275 // While a write the store's cache expects runs, its mutations select the whole issue
5276 // they leave behind, so the response vouches for what the cache keeps — and every
5277 // request is recorded, so the write's parts are reported from their own responses.
5278 let (query, variables) = match graphql::whole_twin(query) {
5279 Some(twin) => (twin, expected::whole_variables(variables)),
5280 None => (query, variables),
5281 };
5282 let sent = self.graphql_sent(query, variables.clone()).await;
5283 self.journal(query, &variables, &sent).await;
5284 sent
5285 }
5286
5287 /// One document sent, retried while GitHub asks for a wait, exactly as asked.
5288 async fn graphql_sent(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
5289 if is_mutation(query)
5290 && ![
5291 graphql::ADD_COMMENT,
5292 graphql::UPDATE_COMMENT,
5293 graphql::DELETE_COMMENT,
5294 graphql::ADD_COMMENT_WHOLE,
5295 graphql::UPDATE_COMMENT_WHOLE,
5296 ]
5297 .contains(&query)
5298 {
5299 let mut cache = self.resolved_cache()?;
5300 let mut ahead = self.read_ahead()?;
5301 let mut carried = self.carried()?;
5302 for argument in ["input", "second", "third", "clear"] {
5303 if let Some(input) = variables.get(argument) {
5304 let names = |id: &NativeId, item_id: Option<&str>| {
5305 ["id", "issueId", "subjectId", "itemId"].iter().any(|key| {
5306 input
5307 .get(key)
5308 .and_then(Value::as_str)
5309 .is_some_and(|value| value == id.0 || Some(value) == item_id)
5310 })
5311 };
5312 cache.retain(|id, item| !names(id, Some(&item.item_id)));
5313 ahead.retain(|id, item| {
5314 !names(id, item.as_ref().map(|item| item.item_id.as_str()))
5315 });
5316 carried.retain(|id, item| !names(id, Some(&item.item_id)));
5317 }
5318 }
5319 }
5320 let doing = operation_description(query);
5321 let mutation = is_mutation(query);
5322 let charge = pacing::Charge::graphql(query, &variables);
5323 let mut waited = Duration::ZERO;
5324 let mut waits = 0_u32;
5325 let mut backoff = self.pacing.retry_backoff;
5326 loop {
5327 let ticket = self.pace(charge, mutation).await;
5328 // Every write but a create is asked for immediately before it is sent — a create's
5329 // fields, its relationships, its content and the delete that takes it back included
5330 // — so a copy that owns the create renews its hold first, and one that has lost it
5331 // sends nothing more. Asked again before a retry, which is sent later.
5332 if mutation
5333 && !query.contains("createIssue(")
5334 && let Err(refused) = <dyn onetaskgraph_plugin_api::WriteGuard>::check().await
5335 {
5336 self.paced(ticket, mutation);
5337 return Err(refused);
5338 }
5339 let attempt = self.send_once(query, &variables, waits + 1).await;
5340 self.paced(ticket, mutation);
5341 let remaining = self.pacing.retry_budget.saturating_sub(waited);
5342 let (wait, cause) = match attempt {
5343 Ok(data) => return Ok(data),
5344 Err(Attempt::Failed(error)) => return Err(error),
5345 Err(Attempt::Conflicted(said)) => {
5346 // A create is never replayed: see `CONFLICT_WORDINGS`.
5347 if query.contains("createIssue(") || backoff.is_zero() || backoff > remaining {
5348 return Err(SourceError::Unavailable {
5349 message: format!(
5350 "GitHub refused this source while {doing} as a temporary \
5351 conflict ({said}); it retried {} over {} and the conflict \
5352 did not clear within the {} one call may spend waiting. \
5353 next: run the command again once nothing else is writing to \
5354 the same items",
5355 plural(waits, "time"),
5356 seconds(waited),
5357 seconds(self.pacing.retry_budget),
5358 ),
5359 });
5360 }
5361 (backoff, pacing::cause::BACKOFF)
5362 }
5363 Err(Attempt::Limited(limited)) => {
5364 let (wait, cause) = limited.wait(backoff);
5365 // A wait of nothing spends none of the budget, so it is exhaustion rather
5366 // than a retry. `Pacing::resolve` rules out every way of configuring one
5367 // except a budget of zero, where reporting the first refusal is the ask.
5368 if wait.is_zero() || wait > remaining {
5369 return Err(limited.exhausted(
5370 doing,
5371 waits,
5372 waited,
5373 wait,
5374 self.pacing.retry_budget,
5375 ));
5376 }
5377 (wait, cause)
5378 }
5379 };
5380 self.clock.sleep_because(wait, cause).await;
5381 waited += wait;
5382 waits += 1;
5383 backoff = backoff.saturating_mul(2);
5384 }
5385 }
5386
5387 /// Hold one request charging `charge` until every secondary window it charges has room for
5388 /// the whole of it — and, for a mutation of an instance configured with
5389 /// `pacing.min_mutation_interval_ms`, until that interval has passed since the last one
5390 /// finished — then count it, answering the ticket [`Self::paced`] finishes it with.
5391 ///
5392 /// The moment is decided and counted under the lock and waited out outside it, so two
5393 /// callers take two places rather than the same one, and no lock is held across an await.
5394 /// A request with room goes out at once: nothing here waits that a window does not ask for.
5395 async fn pace(&self, charge: pacing::Charge, mutation: bool) -> pacing::Ticket {
5396 let (wait, cause, ticket) = {
5397 // A poisoned lock here costs pacing, not correctness, and refusing the write over
5398 // it would turn an earlier failure into a second one for no gain.
5399 let mut windows = self
5400 .windows
5401 .lock()
5402 .unwrap_or_else(std::sync::PoisonError::into_inner);
5403 let now = self.clock.now();
5404 let (mut at, mut cause) = if self.pacing.windows {
5405 windows.room(now, charge)
5406 } else {
5407 (now, None)
5408 };
5409 if mutation && !self.pacing.min_mutation_interval.is_zero() {
5410 let mut last = self
5411 .last_mutation
5412 .lock()
5413 .unwrap_or_else(std::sync::PoisonError::into_inner);
5414 // `checked_add` rather than `+`: adding durations can panic on overflow, and
5415 // pacing is not worth a panic even at a bound `MAX_PACING_MS` already rules out.
5416 if let Some(earliest) = last
5417 .and_then(|previous| previous.checked_add(self.pacing.min_mutation_interval))
5418 && earliest > at
5419 {
5420 at = earliest;
5421 cause = Some(pacing::cause::MIN_INTERVAL);
5422 }
5423 *last = Some(at);
5424 }
5425 let ticket = windows.charge(at, charge);
5426 (at.saturating_sub(now), cause, ticket)
5427 };
5428 if let (false, Some(cause)) = (wait.is_zero(), cause) {
5429 self.clock.sleep_because(wait, cause).await;
5430 }
5431 ticket
5432 }
5433
5434 /// Record that the request `ticket` was charged for has finished, so each window — and
5435 /// the configured spacing — counts it from here rather than from the moment it left.
5436 ///
5437 /// This source can only choose when a request *departs*; GitHub's limiter counts when it
5438 /// *arrives*, and the two differ by whatever the request spent in transit. Counting from
5439 /// departure would hand the limiter a window that looked roomier than it is, so a source
5440 /// pacing exactly at a limit could still be seen arriving over it — which is how a copy
5441 /// paced well inside a board's threshold was once refused by it on a slower machine while
5442 /// passing on a quick one. The previous request had arrived before its response came back,
5443 /// so counting from here is never earlier than the limiter does, whatever transit costs
5444 /// and on whatever platform: the safe side of a limit that punishes being wrong by refusing
5445 /// reads for the next fifty minutes.
5446 ///
5447 /// A failed attempt is finished too: a request refused by the limiter still arrived, and
5448 /// one that never left costs only room nobody needed.
5449 fn paced(&self, ticket: pacing::Ticket, mutation: bool) {
5450 let now = self.clock.now();
5451 self.windows
5452 .lock()
5453 .unwrap_or_else(std::sync::PoisonError::into_inner)
5454 .finish(ticket, now);
5455 if mutation && !self.pacing.min_mutation_interval.is_zero() {
5456 let mut last = self
5457 .last_mutation
5458 .lock()
5459 .unwrap_or_else(std::sync::PoisonError::into_inner);
5460 // `max` rather than an assignment: a concurrent caller may already have reserved a
5461 // slot further out, and completing this request must never pull that slot back in.
5462 *last = Some(last.map_or(now, |reserved| reserved.max(now)));
5463 }
5464 }
5465
5466 /// One HTTP attempt, classified into an answer, a rate limit to wait out, or a
5467 /// failure that waiting cannot help — and recorded, whichever of the three it was.
5468 ///
5469 /// This is the one place a request leaves this crate, which is why the accounting is
5470 /// here rather than at each of the callers: a read path added later is counted without
5471 /// anybody remembering to count it, and
5472 /// `the_session_report_counts_every_request_the_board_served_and_what_each_cost` fails
5473 /// when one is not.
5474 async fn send_once(
5475 &self,
5476 query: &str,
5477 variables: &Value,
5478 number: u32,
5479 ) -> Result<Value, Attempt> {
5480 let executed = self.ledger.priced_document(query);
5481 let Attempted {
5482 result,
5483 limits,
5484 reported_cost,
5485 response_outcome,
5486 } = self.attempt(executed.as_ref(), variables).await;
5487 // No `otherwise` name: every document this source sends is one of its own, and the
5488 // inventory gate on `graphql::DOCUMENTS` is what keeps that true.
5489 let sending = accounting::Request::graphql(query, variables, None, reported_cost);
5490 let outcome = match &result {
5491 Ok(_) => accounting::Outcome::Answered,
5492 Err(Attempt::Limited(_)) => accounting::Outcome::RateLimited,
5493 Err(Attempt::Failed(_) | Attempt::Conflicted(_)) => accounting::Outcome::Refused,
5494 };
5495 self.ledger
5496 .record(sending.finished(outcome, limits).with_execution(
5497 Some(executed.into_owned()),
5498 response_outcome,
5499 number,
5500 ));
5501 result
5502 }
5503
5504 /// The attempt itself, with what its response said about the rate limit alongside.
5505 ///
5506 /// The two are returned together rather than recorded here because every one of the
5507 /// early exits below is a different outcome, and a record written at each of them is a
5508 /// record one of them can be added without.
5509 async fn attempt(&self, query: &str, variables: &Value) -> Attempted {
5510 let mut limits = accounting::RateLimit::default();
5511 let mut reported_cost = None;
5512 let mut response_outcome = accounting::ResponseOutcome::TransportError;
5513 let result = self
5514 .attempted(
5515 query,
5516 variables,
5517 &mut limits,
5518 &mut reported_cost,
5519 &mut response_outcome,
5520 )
5521 .await;
5522 Attempted {
5523 result,
5524 limits,
5525 reported_cost,
5526 response_outcome,
5527 }
5528 }
5529
5530 /// One HTTP attempt, filling in what its response said about the rate limit as it goes.
5531 async fn attempted(
5532 &self,
5533 query: &str,
5534 variables: &Value,
5535 limits: &mut accounting::RateLimit,
5536 reported_cost: &mut Option<u64>,
5537 response_outcome: &mut accounting::ResponseOutcome,
5538 ) -> Result<Value, Attempt> {
5539 let response = self
5540 .client()
5541 .map_err(Attempt::Failed)?
5542 .post(self.endpoint.clone())
5543 .bearer_auth(self.token.expose_secret())
5544 .json(&json!({"query": query, "variables": variables}))
5545 .send()
5546 .await
5547 .map_err(|e| {
5548 Attempt::Failed(SourceError::Unavailable {
5549 message: format!("GitHub GraphQL request failed: {}", transport_error(&e)),
5550 })
5551 })?;
5552 let status = response.status();
5553 if !status.is_success() {
5554 *response_outcome = accounting::ResponseOutcome::HttpError;
5555 }
5556 let header = |name: &str| whole_seconds(response.headers().get(name));
5557 let served = response
5558 .headers()
5559 .get(reqwest::header::DATE)
5560 .and_then(|value| value.to_str().ok())
5561 .and_then(|value| DateTime::parse_from_rfc2822(value).ok())
5562 .map(|at| at.with_timezone(&Utc));
5563 *limits = accounting::RateLimit::read(|name| {
5564 response
5565 .headers()
5566 .get(name)
5567 .and_then(|value| value.to_str().ok())
5568 .map(str::to_owned)
5569 });
5570 // Exactly `0` is exhaustion and everything else — a count, an empty value, bytes
5571 // that are not text at all — is "not known to be exhausted". This never makes a
5572 // response a refusal on its own: it says which limiter a refusal is attributed to
5573 // and where its hint comes from, so a value this cannot read costs a hint rather
5574 // than an answer.
5575 let exhausted = response
5576 .headers()
5577 .get("x-ratelimit-remaining")
5578 .and_then(|value| value.to_str().ok())
5579 == Some("0");
5580 // `retry-after` is what GitHub asks for when it asks; when it does not and the
5581 // primary budget is spent, `x-ratelimit-reset` says when that budget comes back,
5582 // which is the same question answered as an absolute time. Nothing else here is a
5583 // hint, and a schedule is what answers a refusal that carries none.
5584 let retry_after = header("retry-after");
5585 let hinted_by = if retry_after.is_some() || !exhausted {
5586 Hint::RetryAfter
5587 } else {
5588 Hint::PrimaryReset
5589 };
5590 let hint = retry_after.or_else(|| {
5591 exhausted
5592 .then(|| header("x-ratelimit-reset"))
5593 .flatten()
5594 .map(|reset| reset.saturating_sub(Utc::now().timestamp().max(0).unsigned_abs()))
5595 });
5596 // Read before it is parsed, because the evidence which tells a secondary rate
5597 // limit from a rejected credential is in the body of a response whose status says
5598 // only "forbidden" — and a non-success response was never parsed at all.
5599 let body = response.text().await.map_err(|e| {
5600 Attempt::Failed(SourceError::Unavailable {
5601 message: format!(
5602 "GitHub GraphQL response could not be read: {}",
5603 transport_error(&e)
5604 ),
5605 })
5606 })?;
5607 *reported_cost = serde_json::from_str::<Value>(&body)
5608 .ok()
5609 .as_ref()
5610 .and_then(|body| body.pointer("/data/rateLimit/cost"))
5611 .and_then(Value::as_u64);
5612 let parsed = serde_json::from_str::<Value>(&body).ok();
5613 *response_outcome = accounting::ResponseOutcome::of_graphql_reply(status, parsed.as_ref());
5614 if let Some(limiter) = Limiter::classify(status, exhausted, &body) {
5615 return Err(Attempt::Limited(Limited {
5616 limiter,
5617 hint,
5618 hinted_by,
5619 }));
5620 }
5621 if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {
5622 return Err(Attempt::Failed(SourceError::Auth {
5623 message: format!(
5624 "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"
5625 ),
5626 }));
5627 }
5628 if !status.is_success() {
5629 return Err(Attempt::Failed(SourceError::Unavailable {
5630 message: format!("GitHub GraphQL returned HTTP {status}"),
5631 }));
5632 }
5633 let said = refusal_wording(status, &body);
5634 let normalized = said.to_ascii_lowercase();
5635 if CONFLICT_WORDINGS
5636 .iter()
5637 .any(|wording| normalized.contains(wording))
5638 {
5639 return Err(Attempt::Conflicted(said));
5640 }
5641 let answered = if query == graphql::ISSUES_BY_REFERENCE {
5642 self.answer_unresolved(&body)
5643 } else if query == graphql::CREATE_ISSUES {
5644 answer_with_errors(&body).map_or_else(|| self.answer(&body), Ok)
5645 } else {
5646 self.answer(&body)
5647 };
5648 let mut data = answered.map_err(Attempt::Failed)?;
5649 if let Some(served) = served {
5650 stamp_served(&mut data, &served.to_rfc3339());
5651 }
5652 Ok(data)
5653 }
5654
5655 /// What one successful HTTP response says, once its GraphQL errors are read.
5656 /// [`Self::answer`], for a read of issues by reference: a reference GitHub cannot resolve
5657 /// for this credential — a repository it cannot see, an issue number it does not hold —
5658 /// is answered `null` beside a `NOT_FOUND` error naming it, and that is an answer about
5659 /// that reference rather than a failure of the others. Any other error refuses the read,
5660 /// as there.
5661 fn answer_unresolved(&self, body: &str) -> Result<Value, SourceError> {
5662 let parsed: Option<Value> = serde_json::from_str(body).ok();
5663 // Each error is a `NOT_FOUND` whose path names one slot of the batch, `r<n>` or its
5664 // `issue`, and the answer holds `null` at exactly that path: anything else — another
5665 // kind of error, one naming no slot, or one beside a value it says is not there — is a
5666 // refusal of the whole read.
5667 let unresolved = |data: &Value, error: &Value| {
5668 error.get("type").and_then(Value::as_str) == Some("NOT_FOUND")
5669 && error
5670 .get("path")
5671 .and_then(Value::as_array)
5672 .is_some_and(|path| {
5673 let alias = path.first().and_then(Value::as_str).and_then(|alias| {
5674 alias
5675 .strip_prefix('r')
5676 .and_then(|slot| slot.parse::<usize>().ok())
5677 .filter(|slot| *slot < REFERENCE_BATCH)
5678 .map(|_| alias)
5679 });
5680 match (alias, path.get(1).and_then(Value::as_str), path.len()) {
5681 (Some(alias), None, 1) => data.get(alias).is_some_and(Value::is_null),
5682 (Some(alias), Some("issue"), 2) => data
5683 .get(alias)
5684 .and_then(|repository| repository.get("issue"))
5685 .is_some_and(Value::is_null),
5686 _ => false,
5687 }
5688 })
5689 };
5690 let unresolved_only = parsed.as_ref().is_some_and(|parsed| {
5691 parsed.get("data").is_some_and(|data| {
5692 data.is_object()
5693 && parsed
5694 .get("errors")
5695 .and_then(Value::as_array)
5696 .is_some_and(|errors| errors.iter().all(|error| unresolved(data, error)))
5697 })
5698 });
5699 match parsed {
5700 Some(parsed) if unresolved_only => Ok(parsed["data"].clone()),
5701 _ => self.answer(body),
5702 }
5703 }
5704
5705 fn answer(&self, body: &str) -> Result<Value, SourceError> {
5706 let body: Value = serde_json::from_str(body).map_err(|e| SourceError::Malformed {
5707 message: format!("GitHub returned invalid JSON: {e}"),
5708 })?;
5709 let errors = body
5710 .get("errors")
5711 .map(|value| {
5712 value.as_array().ok_or_else(|| SourceError::Malformed {
5713 message: "GitHub response errors is not an array".into(),
5714 })
5715 })
5716 .transpose()?;
5717 if let Some(errors) = errors.filter(|errors| !errors.is_empty()) {
5718 let messages = errors
5719 .iter()
5720 .filter_map(|e| e.get("message").and_then(Value::as_str))
5721 .collect::<Vec<_>>()
5722 .join("; ");
5723 let message = if messages.is_empty() {
5724 "GitHub returned GraphQL errors".into()
5725 } else {
5726 messages
5727 };
5728 let normalized = message.to_ascii_lowercase();
5729 if normalized.contains("resource not accessible") || normalized.contains("scope") {
5730 return Err(SourceError::Auth {
5731 message: format!(
5732 "{message}; grant {} Projects and Issues read/write plus Pull requests read-only access for every repository represented on the board",
5733 self.credential_name
5734 ),
5735 });
5736 }
5737 return Err(SourceError::Refused { message });
5738 }
5739 body.get("data")
5740 .filter(|data| data.is_object())
5741 .cloned()
5742 .ok_or_else(|| SourceError::Malformed {
5743 message: "GitHub response has no data object".into(),
5744 })
5745 }
5746
5747 // llmlint: ignore[boundary_inputs_validated] GitHub caps nested connections at 100 and
5748 // GraphQL cannot independently page them inside the outer item page. This source page is
5749 // deliberately bounded at that published maximum; the live drift journey exercises it.
5750 async fn board_page(
5751 &self,
5752 items_after: Option<&str>,
5753 items_first: u32,
5754 ) -> Result<Value, SourceError> {
5755 let data = self
5756 .graphql(
5757 graphql::BOARD,
5758 json!({"owner":self.owner,"number":self.project_number,
5759 "first":items_first.min(MAX_PAGE_SIZE),"after":items_after,
5760 "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
5761 )
5762 .await?;
5763 data.pointer("/owner/projectV2")
5764 .filter(|v| !v.is_null())
5765 .cloned()
5766 .ok_or_else(|| SourceError::Refused {
5767 message: format!(
5768 "GitHub project {}/{} was not found or is not visible to the token",
5769 self.owner, self.project_number
5770 ),
5771 })
5772 }
5773
5774 /// The search that finds the issues of this board, narrowed by `also` when it is
5775 /// given.
5776 ///
5777 /// `project:owner/number` is what scopes a search to one board, and `is:issue` is what
5778 /// keeps pull requests out of it: GitHub's `ISSUE` search type covers both, and a pull
5779 /// request is somebody's change rather than a unit of plan. `-has:parent` is *not*
5780 /// here on purpose — GitHub accepts it and silently ignores it, so a project is told
5781 /// from a task by the `parent` field each issue carries rather than by the search.
5782 fn board_search(&self, also: Option<&str>) -> String {
5783 let scope = format!("project:{}/{} is:issue", self.owner, self.project_number);
5784 match also {
5785 Some(also) => format!("{scope} {also}"),
5786 None => scope,
5787 }
5788 }
5789
5790 /// One issue this source reached directly, as the board item a read of the board would
5791 /// have produced — or `None` when this board does not hold it.
5792 ///
5793 /// The board half of an issue rides along on `Issue.projectItems`, so the value handed
5794 /// to [`Self::resolve`] is the very shape a `ProjectV2.items` read gives it: the board
5795 /// item's own id, that item's field values, and the issue as its content. One resolver
5796 /// for both routes is what makes an issue read through a search, through its own node
5797 /// id, or through its project's sub-issues report the same title, the same status, the
5798 /// same labels and the same qualified id.
5799 ///
5800 /// An issue with no entry for *this* board is not this source's to report, which is
5801 /// what keeps an id naming some other repository's issue from being answered as an item
5802 /// of this board. That answer is given about an **exhausted** connection and never
5803 /// about an unread page: the entry is looked for on the page in hand, and only if that
5804 /// page reports more of the connection, in [`Self::board_membership`]'s walk of the
5805 /// rest of it.
5806 async fn resolve_issue(&self, issue: &Value) -> Result<Option<Resolved>, SourceError> {
5807 self.resolve_issue_as(issue, true).await
5808 }
5809
5810 /// [`Self::resolve_issue`], remembering what it resolves for this command's later reads
5811 /// and writes only when `remember` says to.
5812 async fn resolve_issue_as(
5813 &self,
5814 issue: &Value,
5815 remember: bool,
5816 ) -> Result<Option<Resolved>, SourceError> {
5817 if optional_str(issue, "__typename")? != Some("Issue") {
5818 return Ok(None);
5819 }
5820 let memberships = issue
5821 .get("projectItems")
5822 .ok_or_else(|| SourceError::Malformed {
5823 message: "GitHub issue is missing projectItems".into(),
5824 })?;
5825 let nodes = memberships
5826 .get("nodes")
5827 .and_then(Value::as_array)
5828 .ok_or_else(|| SourceError::Malformed {
5829 message: "GitHub issue projectItems.nodes is not an array".into(),
5830 })?;
5831 let held = match self.board_entry(nodes) {
5832 Some(held) => held.clone(),
5833 None => {
5834 let info = memberships
5835 .get("pageInfo")
5836 .ok_or_else(|| SourceError::Malformed {
5837 message: "GitHub issue projectItems has no pageInfo".into(),
5838 })?;
5839 // The page held no entry for this board. Whether that means the issue is
5840 // not on it is a question about the rest of the connection, and only a
5841 // connection with no rest answers it here.
5842 if !required_bool(info, "hasNextPage")? {
5843 return Ok(None);
5844 }
5845 let cursor = required_str(info, "endCursor")?;
5846 validate_cursor_progress(None, cursor)?;
5847 let issue_id = required_str(issue, "id")?;
5848 match self.board_membership(issue_id, cursor).await? {
5849 Some(held) => held,
5850 None => return Ok(None),
5851 }
5852 }
5853 };
5854 // The membership's own `updatedAt` rides along: it is the board half of the item's change
5855 // token, which a field change moves where the issue's own time does not.
5856 let item = json!({
5857 "id": required_str(&held, "id")?,
5858 "updatedAt": held.get("updatedAt"),
5859 "project": held.get("project"),
5860 "fieldValues": held.get("fieldValues"),
5861 "content": issue,
5862 });
5863 if remember {
5864 self.resolve(&item)
5865 } else {
5866 self.resolve_unremembered(&item)
5867 }
5868 }
5869
5870 /// This board's own entry among one page of an issue's `Issue.projectItems`.
5871 ///
5872 /// One spelling of *which membership is this board's*, so the page a read carries and
5873 /// the pages [`Self::board_membership`] walks are searched by the same rule.
5874 fn board_entry<'a>(&self, nodes: &'a [Value]) -> Option<&'a Value> {
5875 nodes.iter().find(|node| {
5876 node.pointer("/project/number").and_then(Value::as_u64)
5877 == Some(u64::from(self.project_number))
5878 })
5879 }
5880
5881 /// The rest of one issue's board memberships, from `after`, for this board's entry.
5882 ///
5883 /// The recovery read: a page of memberships that holds no entry for this board says
5884 /// nothing about the memberships past it, so the connection is walked to exhaustion
5885 /// before an issue is reported as one this board does not hold. `Ok(None)` is that
5886 /// positive answer — the whole connection was read and no entry named this board —
5887 /// rather than a failure, and the walk is held to
5888 /// [`validate_cursor_progress`] like every other page walk here, so a source answering
5889 /// with a cursor that does not advance is refused instead of spun on.
5890 async fn board_membership(
5891 &self,
5892 issue: &str,
5893 after: &str,
5894 ) -> Result<Option<Value>, SourceError> {
5895 let mut after = after.to_owned();
5896 loop {
5897 let data = self
5898 .graphql(
5899 graphql::ISSUE_BOARD_ITEMS,
5900 json!({"id":issue,"first":MAX_PAGE_SIZE,"after":after,
5901 "nestedFirst":NESTED_PAGE_SIZE}),
5902 )
5903 .await?;
5904 let Some(connection) = data
5905 .pointer("/node/projectItems")
5906 .filter(|value| !value.is_null())
5907 else {
5908 // The id resolved to nothing, or to something with no memberships to walk —
5909 // which is the same answer as a connection holding no entry for this board.
5910 return Ok(None);
5911 };
5912 let nodes = connection
5913 .get("nodes")
5914 .and_then(Value::as_array)
5915 .ok_or_else(|| SourceError::Malformed {
5916 message: "GitHub issue projectItems.nodes is not an array".into(),
5917 })?;
5918 if let Some(held) = self.board_entry(nodes) {
5919 return Ok(Some(held.clone()));
5920 }
5921 let info = connection
5922 .get("pageInfo")
5923 .ok_or_else(|| SourceError::Malformed {
5924 message: "GitHub issue projectItems has no pageInfo".into(),
5925 })?;
5926 let next = required_bool(info, "hasNextPage")?
5927 .then(|| required_str(info, "endCursor"))
5928 .transpose()?;
5929 match next {
5930 Some(next) => {
5931 validate_cursor_progress(Some(&after), next)?;
5932 after = next.to_owned();
5933 }
5934 None => return Ok(None),
5935 }
5936 }
5937 }
5938
5939 /// One page of a board-scoped issue search, and where the next page resumes.
5940 async fn search_page(
5941 &self,
5942 search: &str,
5943 first: u32,
5944 after: Option<&str>,
5945 ) -> Result<(Vec<Resolved>, Option<String>), SourceError> {
5946 let data = self
5947 .graphql(
5948 graphql::SEARCH_ISSUES,
5949 json!({"search":search,"type":"ISSUE","first":first.min(MAX_PAGE_SIZE),
5950 "after":after,"nestedFirst":NESTED_PAGE_SIZE,
5951 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
5952 )
5953 .await?;
5954 let connection = data.get("search").ok_or_else(|| SourceError::Malformed {
5955 message: "GitHub search response has no search connection".into(),
5956 })?;
5957 let mut found = Vec::new();
5958 for node in connection
5959 .get("nodes")
5960 .and_then(Value::as_array)
5961 .ok_or_else(|| SourceError::Malformed {
5962 message: "GitHub search nodes is not an array".into(),
5963 })?
5964 {
5965 if let Some(resolved) = self.resolve_issue(node).await? {
5966 found.push(resolved);
5967 }
5968 }
5969 let info = connection
5970 .get("pageInfo")
5971 .ok_or_else(|| SourceError::Malformed {
5972 message: "GitHub search connection has no pageInfo".into(),
5973 })?;
5974 let next = required_bool(info, "hasNextPage")?
5975 .then(|| required_str(info, "endCursor"))
5976 .transpose()?
5977 .map(str::to_owned);
5978 if let Some(next) = &next {
5979 validate_cursor_progress(after, next)?;
5980 }
5981 Ok((found, next))
5982 }
5983
5984 /// Every issue this board holds, completed with what this run wrote.
5985 ///
5986 /// The completion is not an optimisation and it is not a cache: GitHub's issue search
5987 /// is an index and is eventually consistent, so an issue this run created seconds ago
5988 /// can be absent from it, and a project listed straight after being written would
5989 /// otherwise be missing from its own board. What is added back is only what this
5990 /// process itself wrote, out of [`Self::created`], which lives and dies with the
5991 /// process.
5992 async fn board_issues(&self) -> Result<Vec<Resolved>, SourceError> {
5993 let found = self.searched_issues().await?;
5994 self.completed_with_written(found, |_| true)
5995 }
5996
5997 /// Every issue this board's own search reports, walked to exhaustion, read once per
5998 /// source.
5999 ///
6000 /// The uncompleted half of [`Self::board_issues`], separated because [`Self::board`]
6001 /// needs it too and the two would otherwise walk the same search twice in one command.
6002 /// See [`Self::search_cache`] for why holding it is the same bargain holding the board
6003 /// is.
6004 async fn searched_issues(&self) -> Result<Vec<Resolved>, SourceError> {
6005 let cached = self.search_cache()?.clone();
6006 if let Some(held) = cached {
6007 return Ok(held);
6008 }
6009 let mut after: Option<String> = None;
6010 let mut found = Vec::new();
6011 let search = self.board_search(None);
6012 loop {
6013 let (page, next) = self
6014 .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
6015 .await?;
6016 found.extend(page);
6017 match next {
6018 Some(next) => after = Some(next),
6019 None => break,
6020 }
6021 }
6022 *self.search_cache()? = Some(found.clone());
6023 Ok(found)
6024 }
6025
6026 /// Every issue this board's own search reports with `also` beside its scope, walked to
6027 /// exhaustion — held for no later read, which may not be narrowed alike.
6028 async fn searched_narrowly(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
6029 let mut after: Option<String> = None;
6030 let mut found = Vec::new();
6031 let search = self.board_search(Some(also));
6032 loop {
6033 let (page, next) = self
6034 .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
6035 .await?;
6036 found.extend(page);
6037 match next {
6038 Some(next) => after = Some(next),
6039 None => return Ok(found),
6040 }
6041 }
6042 }
6043
6044 /// This process's own view of the board's issues, or the refusal a poisoned lock is.
6045 fn search_cache(
6046 &self,
6047 ) -> Result<std::sync::MutexGuard<'_, Option<Vec<Resolved>>>, SourceError> {
6048 self.search_cache
6049 .lock()
6050 .map_err(|_| SourceError::Unavailable {
6051 message: "this source's view of the board's issues was left inconsistent by an \
6052 earlier failure; next: run the command again"
6053 .into(),
6054 })
6055 }
6056
6057 /// `found`, with everything this run wrote that `keep` accepts and the read did not
6058 /// report.
6059 ///
6060 /// See [`Self::created`] and [`Self::board_issues`] for why a read has to be completed
6061 /// at all: the search index is behind, and a node read of an item filed moments ago can
6062 /// be too.
6063 fn completed_with_written(
6064 &self,
6065 mut found: Vec<Resolved>,
6066 keep: impl Fn(&Resolved) -> bool,
6067 ) -> Result<Vec<Resolved>, SourceError> {
6068 for own in self.created()?.iter().filter(|own| keep(own)) {
6069 if !found.iter().any(|item| item.id == own.id) {
6070 found.push(own.clone());
6071 }
6072 }
6073 Ok(found)
6074 }
6075
6076 /// What resolving one node id reached.
6077 ///
6078 /// Three answers rather than an `Option`, because a board *draft* is none of the other
6079 /// two: it is not an issue, so the issue fragment reads nothing of it, and a read of one
6080 /// is completed by a read of the draft itself rather than reported as nothing.
6081 async fn reach(&self, id: &NativeId) -> Result<Reached, SourceError> {
6082 let asked = self
6083 .graphql(
6084 graphql::ISSUE,
6085 json!({"id":id.0,"first":MAX_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
6086 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
6087 )
6088 .await;
6089 let data = match asked {
6090 Ok(data) => data,
6091 // A string that is not a node id at all is not a failure to report: it is an id
6092 // this board does not hold, which is what every read of one already answers.
6093 Err(error) if unresolvable_node(&error) => return Ok(Reached::Nothing),
6094 Err(error) => return Err(error),
6095 };
6096 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
6097 return Ok(Reached::Nothing);
6098 };
6099 if optional_str(node, "__typename")? == Some("DraftIssue") {
6100 return Ok(Reached::Draft);
6101 }
6102 Ok(match self.resolve_issue(node).await? {
6103 Some(item) => Reached::Held(Box::new(item)),
6104 None => Reached::Nothing,
6105 })
6106 }
6107
6108 /// One item of this board by its own id, whatever kind it is.
6109 ///
6110 /// Resolved from the identifier alone: no search, board-wide or otherwise. What this
6111 /// run wrote is read first, because a node read of an item created moments ago can
6112 /// still be behind the board field values written onto it — see [`Self::created`].
6113 async fn item_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6114 if let Some(own) = self.created()?.iter().find(|own| own.id == *id) {
6115 return Ok(Some(own.clone()));
6116 }
6117 if let Some(held) = self.read_ahead()?.remove(id) {
6118 return Ok(held);
6119 }
6120 match self.reach(id).await? {
6121 Reached::Held(item) => Ok(Some(*item)),
6122 Reached::Nothing => Ok(None),
6123 Reached::Draft => self.draft_by_id(id).await,
6124 }
6125 }
6126
6127 /// [`Self::item_by_id`] for a document: one [`graphql::DOCUMENT_IN_PROJECT`] read, which
6128 /// answers the project it is filed under and that project's members beside it.
6129 ///
6130 /// Each is held for this command as a read of it would have been: the project for the
6131 /// next read of it by its id, and — when its page of members is the whole of them — the
6132 /// members for the next listing of that project. So a write held to who may read the
6133 /// document, which reads all three, is answered by this one request.
6134 async fn document_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6135 if self.created()?.iter().any(|own| own.id == *id) || self.read_ahead()?.contains_key(id) {
6136 return self.item_by_id(id).await;
6137 }
6138 let asked = self
6139 .graphql(
6140 graphql::DOCUMENT_IN_PROJECT,
6141 json!({"id":id.0,"first":MAX_PAGE_SIZE,"memberFirst":MEMBER_PAGE_SIZE,
6142 "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
6143 "duplicates":true}),
6144 )
6145 .await;
6146 let data = match asked {
6147 Ok(data) => data,
6148 Err(error) if unresolvable_node(&error) => return Ok(None),
6149 Err(error) => return Err(error),
6150 };
6151 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
6152 return Ok(None);
6153 };
6154 if optional_str(node, "__typename")? == Some("DraftIssue") {
6155 return self.draft_by_id(id).await;
6156 }
6157 let Some(item) = self.resolve_issue(node).await? else {
6158 return Ok(None);
6159 };
6160 // An answer for another issue than the one asked about says nothing of that one.
6161 if item.id != *id {
6162 return Err(SourceError::Malformed {
6163 message: format!(
6164 "GitHub answered issue {} when asked for document {}",
6165 item.id.0, id.0
6166 ),
6167 });
6168 }
6169 if let Some(project) = node.get("project").filter(|value| !value.is_null())
6170 && let Some(parent) = self
6171 .resolve_issue(project)
6172 .await?
6173 .filter(|parent| Some(&parent.id) == item.parent.as_ref())
6174 {
6175 let members = project.get("subIssues").filter(|value| !value.is_null());
6176 let whole = members
6177 .and_then(|members| members.pointer("/pageInfo/hasNextPage"))
6178 .and_then(Value::as_bool)
6179 == Some(false);
6180 if whole {
6181 let nodes = members
6182 .and_then(|members| members.get("nodes"))
6183 .and_then(Value::as_array)
6184 .ok_or_else(|| SourceError::Malformed {
6185 message: "GitHub subIssues.nodes is not an array".into(),
6186 })?;
6187 let mut children = Vec::new();
6188 for member in nodes {
6189 if let Some(resolved) = self.resolve_issue(member).await? {
6190 children.push(resolved);
6191 }
6192 }
6193 // Held as the project's members only when every one resolved and says it is
6194 // filed under it: a page with any other answers nothing of all the members.
6195 if children.len() == nodes.len()
6196 && children
6197 .iter()
6198 .all(|child| child.parent.as_ref() == Some(&parent.id))
6199 {
6200 self.children_cache()?
6201 .insert(parent.id.clone(), (parent.id.clone(), children));
6202 }
6203 }
6204 self.read_ahead()?.insert(parent.id.clone(), Some(parent));
6205 }
6206 Ok(Some(item))
6207 }
6208
6209 /// Several items of this board, each by its own id, in order — what [`Self::item_by_id`]
6210 /// answers for each, read [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] rather
6211 /// than one request per id.
6212 ///
6213 /// What this run wrote answers first, as it does there, and only the rest is read. One id
6214 /// left to read is read by [`Self::item_by_id`] itself, which costs what a batch does. A
6215 /// batch GitHub refuses because one of its ids resolves to no node at all is read again one
6216 /// id at a time, so that id is answered as not held and the others as themselves; a draft
6217 /// is completed by a read of the draft, exactly as there.
6218 async fn items_by_ids(&self, ids: &[NativeId]) -> Result<Vec<Option<Resolved>>, SourceError> {
6219 let mut found: Vec<Option<Option<Resolved>>> = {
6220 let created = self.created()?;
6221 ids.iter()
6222 .map(|id| {
6223 created
6224 .iter()
6225 .find(|own| own.id == *id)
6226 .map(|own| Some(own.clone()))
6227 })
6228 .collect()
6229 };
6230 let unread: Vec<NativeId> = ids
6231 .iter()
6232 .zip(&found)
6233 .filter(|(_, found)| found.is_none())
6234 .map(|(id, _)| id.clone())
6235 .collect();
6236 let mut read = Vec::with_capacity(unread.len());
6237 if let [one] = unread.as_slice() {
6238 read.push(self.item_by_id(one).await?);
6239 } else {
6240 for batch in unread.chunks(DETAIL_BATCH) {
6241 let data = match self
6242 .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, None))
6243 .await
6244 {
6245 Ok(data) => data,
6246 Err(error) if unresolvable_node(&error) => {
6247 for id in batch {
6248 read.push(self.item_by_id(id).await?);
6249 }
6250 continue;
6251 }
6252 Err(error) => return Err(error),
6253 };
6254 for (slot, id) in batch.iter().enumerate() {
6255 let node =
6256 data.get(format!("i{slot}"))
6257 .ok_or_else(|| SourceError::Malformed {
6258 message: format!(
6259 "GitHub answered a batch read with no item for {}",
6260 id.0
6261 ),
6262 })?;
6263 read.push(if node.is_null() {
6264 None
6265 } else if optional_str(node, "__typename")? == Some("DraftIssue") {
6266 self.draft_by_id(id).await?
6267 } else {
6268 if optional_str(node, "__typename")? == Some("Issue")
6269 && required_str(node, "id")? != id.0
6270 {
6271 return Err(SourceError::Malformed {
6272 message: format!(
6273 "GitHub answered the read of {} with issue {}",
6274 id.0,
6275 required_str(node, "id")?
6276 ),
6277 });
6278 }
6279 self.resolve_issue(node).await?
6280 });
6281 }
6282 }
6283 }
6284 let mut read = read.into_iter();
6285 Ok(found
6286 .iter_mut()
6287 .map(|slot| slot.take().unwrap_or_else(|| read.next().flatten()))
6288 .collect())
6289 }
6290
6291 fn read_ahead(
6292 &self,
6293 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Option<Resolved>>>, SourceError> {
6294 self.read_ahead
6295 .lock()
6296 .map_err(|_| SourceError::Unavailable {
6297 message:
6298 "items read ahead for a copy were left inconsistent; run the command again"
6299 .into(),
6300 })
6301 }
6302
6303 fn resolved_cache(
6304 &self,
6305 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
6306 self.resolved_cache
6307 .lock()
6308 .map_err(|_| SourceError::Unavailable {
6309 message: "resolved item records were left inconsistent; run the command again"
6310 .into(),
6311 })
6312 }
6313
6314 /// Reuse a record this invocation already resolved. The mutation sender invalidates
6315 /// it before writing, so a partial failure cannot leave a pre-write binding behind.
6316 async fn bound_item(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6317 let cached = self.resolved_cache()?.get(id).cloned();
6318 if let Some(item) = cached {
6319 return Ok(Some(item));
6320 }
6321 self.item_by_id(id).await
6322 }
6323
6324 /// Take out the item an issue read by reference reached for `id`, when one is held: the
6325 /// status write it was read for is the one caller, so no other read or write is ever
6326 /// answered from a read that selected only what that write needs.
6327 fn take_held(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6328 Ok(self
6329 .held_cache
6330 .lock()
6331 .map_err(|_| SourceError::Unavailable {
6332 message: "items read by issue reference were left inconsistent; run the \
6333 command again"
6334 .into(),
6335 })?
6336 .remove(id))
6337 }
6338
6339 /// One board draft by its own id, with the board item it sits in — or `None` when no
6340 /// item of this board is that draft's.
6341 ///
6342 /// The same decision [`Self::resolve_issue`] makes for an issue, over the draft's own
6343 /// `projectV2Items`: an entry naming this board is what makes it this board's. GitHub
6344 /// links a draft to one board item, so the page this read carries is the whole of that
6345 /// connection, and a page that reports more than it holds is refused rather than read
6346 /// as an answer about memberships nobody read.
6347 async fn draft_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6348 let data = self
6349 .graphql(
6350 graphql::DRAFT,
6351 json!({"id":id.0,"nestedFirst":NESTED_PAGE_SIZE,
6352 "boardItems":BOARD_ITEMS_PAGE_SIZE}),
6353 )
6354 .await?;
6355 // Gone between the two reads is an answer — the draft is no longer there. Anything
6356 // else than the draft [`Self::reach`] was just told this id is, is not one.
6357 let Some(draft) = data.get("node").filter(|node| !node.is_null()) else {
6358 return Ok(None);
6359 };
6360 if optional_str(draft, "__typename")? != Some("DraftIssue") {
6361 return Err(SourceError::Malformed {
6362 message: format!(
6363 "GitHub answered {} as a draft and then as something else",
6364 id.0
6365 ),
6366 });
6367 }
6368 if required_str(draft, "id")? != id.0 {
6369 return Err(SourceError::Malformed {
6370 message: format!("GitHub answered a different draft for {}", id.0),
6371 });
6372 }
6373 let memberships = draft
6374 .get("projectV2Items")
6375 .ok_or_else(|| SourceError::Malformed {
6376 message: format!("GitHub draft {} is missing projectV2Items", id.0),
6377 })?;
6378 let nodes = memberships
6379 .get("nodes")
6380 .and_then(Value::as_array)
6381 .ok_or_else(|| SourceError::Malformed {
6382 message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
6383 })?;
6384 let info = memberships
6385 .get("pageInfo")
6386 .ok_or_else(|| SourceError::Malformed {
6387 message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
6388 })?;
6389 // Read whether or not this board's entry is on the page: a page claiming more than
6390 // the one item GitHub links a draft to is a malformed answer either way.
6391 if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
6392 return Err(SourceError::Malformed {
6393 message: format!(
6394 "GitHub draft {} reports more board items than the one GitHub links a draft \
6395 to",
6396 id.0
6397 ),
6398 });
6399 }
6400 if let Some(node) = nodes.first()
6401 && node
6402 .pointer("/project/number")
6403 .and_then(Value::as_u64)
6404 .is_none()
6405 {
6406 return Err(SourceError::Malformed {
6407 message: format!(
6408 "GitHub draft {} board item has no numeric project number",
6409 id.0
6410 ),
6411 });
6412 }
6413 let Some(held) = self.board_entry(nodes) else {
6414 return Ok(None);
6415 };
6416 if required_str(
6417 held.get("project").ok_or_else(|| SourceError::Malformed {
6418 message: format!("GitHub draft {} board item has no project", id.0),
6419 })?,
6420 "id",
6421 )? != self.board_fields().await?.id.as_str()
6422 {
6423 return Ok(None);
6424 }
6425 let item = json!({
6426 "id": required_str(held, "id")?,
6427 "project": held.get("project"),
6428 "fieldValues": held.get("fieldValues"),
6429 "content": draft,
6430 });
6431 self.resolve(&item)
6432 }
6433
6434 /// The board's own id and field definitions, for a write whose item does not carry
6435 /// them — never its items.
6436 ///
6437 /// A board this command has already listed supplies them, since it read them beside its
6438 /// items; otherwise they come from [`graphql::BOARD_FIELDS`], once per command. Neither
6439 /// is consulted about which items the board holds: see the module documentation for
6440 /// why a question about one known item is answered by reading that item.
6441 async fn board_fields(&self) -> Result<BoardFields, SourceError> {
6442 if let Some(board) = self.board_cache()?.as_ref() {
6443 return Ok(BoardFields {
6444 id: BoardId::parse(&board.id)?,
6445 fields: board.fields.clone(),
6446 });
6447 }
6448 if let Some(held) = self.fields_cache()?.clone() {
6449 return Ok(held);
6450 }
6451 let data = self
6452 .graphql(
6453 graphql::BOARD_FIELDS,
6454 json!({"owner":self.owner,"number":self.project_number,
6455 "nestedFirst":NESTED_PAGE_SIZE}),
6456 )
6457 .await?;
6458 self.fields_read(&data)
6459 }
6460
6461 /// The board's id and fields out of an answer carrying the `boardFields` root, held for
6462 /// the rest of this command.
6463 fn fields_read(&self, data: &Value) -> Result<BoardFields, SourceError> {
6464 let board = data
6465 .pointer("/boardFields/projectV2")
6466 .filter(|value| !value.is_null())
6467 .ok_or_else(|| SourceError::Refused {
6468 message: format!(
6469 "GitHub project {}/{} was not found or is not visible to the token",
6470 self.owner, self.project_number
6471 ),
6472 })?;
6473 let read = BoardFields {
6474 id: BoardId::parse(required_str(board, "id")?)?,
6475 fields: board.get("fields").cloned().unwrap_or(Value::Null),
6476 };
6477 *self.fields_cache()? = Some(read.clone());
6478 Ok(read)
6479 }
6480
6481 /// Read what creating an issue in `repository` needs and this command has not read yet —
6482 /// the board's fields and the repository's node id — in one request when it needs both.
6483 ///
6484 /// When either is already known this sends nothing, and the other is read by its own
6485 /// document where it is asked for, so no create reads anything twice.
6486 async fn creation_context(
6487 &self,
6488 repository: &RepositoryTarget,
6489 incoming: &Incoming<'_>,
6490 ) -> Result<(), SourceError> {
6491 let fields_known = self.board_cache()?.is_some() || self.fields_cache()?.is_some();
6492 if fields_known || self.repository_cache()?.contains_key(repository) {
6493 return Ok(());
6494 }
6495 // A placement an earlier process kept is taken only while the project issue this create
6496 // has just read shows its setup-context token unchanged; see `projection`.
6497 if self.placed_by_kept(repository, incoming.parent) {
6498 return Ok(());
6499 }
6500 let data = self
6501 .graphql(
6502 graphql::CREATION_CONTEXT,
6503 json!({"owner":self.owner,"number":self.project_number,
6504 "nestedFirst":NESTED_PAGE_SIZE,"repositoryOwner":repository.owner,
6505 "repositoryName":repository.name}),
6506 )
6507 .await?;
6508 self.fields_read(&data)?;
6509 self.repository_read(&data, repository, incoming)?;
6510 Ok(())
6511 }
6512
6513 /// This process's own view of the board's fields, or the refusal a poisoned lock is.
6514 fn fields_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<BoardFields>>, SourceError> {
6515 self.fields_cache
6516 .lock()
6517 .map_err(|_| SourceError::Unavailable {
6518 message: "this source's view of the board's fields was left inconsistent by an \
6519 earlier failure; next: run the command again"
6520 .into(),
6521 })
6522 }
6523
6524 /// What a write to `item` needs of the board, read off that item when it says enough and
6525 /// off [`Self::board_fields`] when it does not.
6526 ///
6527 /// A node read of an item names its board and carries the definition of every field it
6528 /// holds a value of — so an item naming its board, holding a value of the origin field
6529 /// when the write carries an origin, and, when the write carries a status, holding a
6530 /// `Status` value, needs no read of the board at all. **Nothing the item does not say is
6531 /// guessed:** a field it holds no value of may still be on the board, and a view reading it
6532 /// as absent would refuse a write the board can take or skip a field write the board
6533 /// needs, so such an item — and a create, which has no item yet — takes the board's fields
6534 /// from their own read instead. A write carrying no origin to an item holding none — every
6535 /// write out of a source declared private — writes no origin whatever the board defines,
6536 /// so it needs no definition of that field.
6537 async fn fields_for(
6538 &self,
6539 item: Option<&Resolved>,
6540 writes_origin: bool,
6541 writes_status: bool,
6542 selects_priority: bool,
6543 ) -> Result<BoardFields, SourceError> {
6544 if let Some(board) = item.and_then(Resolved::carried_board) {
6545 return Ok(board);
6546 }
6547 if let Some(item) = item
6548 && let Some(board_id) = item.named_board()
6549 && (!writes_origin || item.defines(ORIGIN_FIELD))
6550 && (!writes_status || item.defines("Status"))
6551 && (!selects_priority || item.defines(PRIORITY_FIELD))
6552 && self
6553 .metadata_fields
6554 .iter()
6555 .all(|projection| item.defines(&projection.field))
6556 {
6557 return Ok(BoardFields {
6558 id: board_id,
6559 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
6560 });
6561 }
6562 self.board_fields().await
6563 }
6564
6565 /// Everything filed under one issue of this board, walked to exhaustion — or `None`
6566 /// when that id names nothing here with a sub-issue relationship to walk.
6567 ///
6568 /// `None` and an empty answer are different: `None` is *this is not an issue of this
6569 /// GitHub*, which is what sends a project selector on to be read as a name, and an
6570 /// empty vector is a project that holds nothing.
6571 async fn sub_issues(&self, id: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
6572 let mut after: Option<String> = None;
6573 let mut children = Vec::new();
6574 // The project issue as the first page carried it, held once the walk has ended.
6575 let mut parent: Option<Resolved> = None;
6576 loop {
6577 let asked = self
6578 .graphql(
6579 graphql::SUB_ISSUES,
6580 json!({"id":id.0,"memberFirst":MEMBER_PAGE_SIZE,"after":after,
6581 "nestedFirst":NESTED_PAGE_SIZE,
6582 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
6583 )
6584 .await;
6585 let data = match asked {
6586 Ok(data) => data,
6587 // A string that is not a node id at all is not a failure to report: it is
6588 // the ordinary answer to a selector naming a project by its name.
6589 Err(error) if unresolvable_node(&error) => return Ok(None),
6590 Err(error) => return Err(error),
6591 };
6592 let Some(connection) = data
6593 .pointer("/node/subIssues")
6594 .filter(|value| !value.is_null())
6595 else {
6596 // No such node, or one with no sub-issue relationship — a board draft is
6597 // the one this board can really hold.
6598 return Ok(None);
6599 };
6600 if after.is_none()
6601 && let Some(node) = data.get("node").filter(|node| node.get("id").is_some())
6602 {
6603 parent = self
6604 .resolve_issue(node)
6605 .await?
6606 .filter(|resolved| resolved.id == *id);
6607 }
6608 for node in connection
6609 .get("nodes")
6610 .and_then(Value::as_array)
6611 .ok_or_else(|| SourceError::Malformed {
6612 message: "GitHub subIssues.nodes is not an array".into(),
6613 })?
6614 {
6615 if let Some(resolved) = self.resolve_issue(node).await? {
6616 children.push(resolved);
6617 }
6618 }
6619 let info = connection
6620 .get("pageInfo")
6621 .ok_or_else(|| SourceError::Malformed {
6622 message: "GitHub subIssues connection has no pageInfo".into(),
6623 })?;
6624 let next = required_bool(info, "hasNextPage")?
6625 .then(|| required_str(info, "endCursor"))
6626 .transpose()?;
6627 match next {
6628 Some(next) => {
6629 validate_cursor_progress(after.as_deref(), next)?;
6630 after = Some(next.to_owned());
6631 }
6632 None => {
6633 // Held only once the walk has ended, so one failing part way holds
6634 // nothing and every read it would have answered is made.
6635 // The project issue the first page carried answers the next read of the
6636 // project by its id in this command, as a read of it made now would: what
6637 // classifying a project for a write reads straight after its members.
6638 if let Some(parent) = &parent {
6639 self.read_ahead()?
6640 .entry(parent.id.clone())
6641 .or_insert_with(|| Some(parent.clone()));
6642 }
6643 let mut carried = self.carried()?;
6644 for item in parent.into_iter().chain(children.iter().cloned()) {
6645 carried.insert(item.id.clone(), item);
6646 }
6647 return Ok(Some(children));
6648 }
6649 }
6650 }
6651 }
6652
6653 fn carried(
6654 &self,
6655 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
6656 self.carried.lock().map_err(|_| SourceError::Unavailable {
6657 message: "items a project's listing carried were left inconsistent; run the \
6658 command again"
6659 .into(),
6660 })
6661 }
6662
6663 /// Which issue of this board a project *name* is, or `None` when none is.
6664 ///
6665 /// One bounded query which filters on that name at the server, rather than a walk of
6666 /// every issue the board holds. The name is compared again here: the qualifier narrows
6667 /// what GitHub sends, and this source decides what it names.
6668 async fn project_by_name(&self, name: &str) -> Result<Option<NativeId>, SourceError> {
6669 let search = self.board_search(Some(&title_qualifier(name)));
6670 let mut after = None;
6671 loop {
6672 let (candidates, next) = self
6673 .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
6674 .await?;
6675 if let Some(item) = candidates.into_iter().find(|item| {
6676 item.kind == BoardKind::Work(ItemKind::Project)
6677 && item.title.eq_ignore_ascii_case(name)
6678 }) {
6679 return Ok(Some(item.id));
6680 }
6681 match next {
6682 Some(next) => after = Some(next),
6683 None => return Ok(None),
6684 }
6685 }
6686 }
6687
6688 /// Everything filed under one project of this board: the sub-issues of the issue that
6689 /// project is.
6690 ///
6691 /// Tasks *and* documents, because a document filed under a project is a sub-issue of it
6692 /// too — the caller keeps the kind it asked for. Nothing about this grows as the board
6693 /// gains projects, or as another project gains tasks.
6694 ///
6695 /// A qualified id names the issue and is asked for its sub-issues directly: one
6696 /// request, no search of any kind. Only a selector GitHub cannot resolve that way is
6697 /// read as a project *name*, which costs the one bounded search
6698 /// [`Self::project_by_name`] makes.
6699 ///
6700 /// What GitHub answered is held for the rest of the command — see [`Self::children_cache`]
6701 /// — and each answer is still cut to the items whose parent is this project, so one this
6702 /// process has since filed elsewhere is not reported here, and completed with what this
6703 /// process filed under it.
6704 async fn project_children(&self, selector: &NativeId) -> Result<Vec<Resolved>, SourceError> {
6705 let held = self.children_cache()?.get(selector).cloned();
6706 let (project, children) = match held {
6707 Some(held) => held,
6708 None => {
6709 let answered = match self.sub_issues(selector).await? {
6710 Some(children) => (selector.clone(), children),
6711 None => match self.project_by_name(&selector.0).await? {
6712 Some(project) => {
6713 let children = self.sub_issues(&project).await?.unwrap_or_default();
6714 (project, children)
6715 }
6716 None => return Ok(Vec::new()),
6717 },
6718 };
6719 self.children_cache()?
6720 .insert(selector.clone(), answered.clone());
6721 answered
6722 }
6723 };
6724 let mut children: Vec<Resolved> = children
6725 .into_iter()
6726 .filter(|child| child.parent.as_ref() == Some(&project))
6727 .collect();
6728 for own in self.updated()?.iter() {
6729 if own.parent.as_ref() == Some(&project) && !children.iter().any(|c| c.id == own.id) {
6730 children.push(own.clone());
6731 }
6732 }
6733 self.completed_with_written(children, |own| own.parent.as_ref() == Some(&project))
6734 }
6735
6736 /// Every issue of this board GitHub's issue search reports updated at or after `since`,
6737 /// completed with what this run wrote — the candidates a comment-activity read confirms.
6738 ///
6739 /// Scoped by the board and by nothing else: `project:<owner>/<number>` reaches every issue
6740 /// on the board whatever repository, and whatever owner, it lives in, so no repository or
6741 /// owner qualifier is added and none is needed. What makes the `updated:` qualifier
6742 /// sufficient is a fact about GitHub rather than about this source: a comment written on an
6743 /// issue **and a comment edited on it** both move that issue's `updatedAt`. The credentialed
6744 /// journey `an_edited_comment_moves_its_issue_and_is_selected_since` in `tests/journey`
6745 /// re-takes that fact on every run of the lane, so a change on GitHub's side fails there
6746 /// rather than silently narrowing a caller's answer.
6747 ///
6748 /// The instant is written to the second, rounded down, which can only widen what the
6749 /// search returns; confirmation against each candidate's own comments is what makes the
6750 /// answer exact. The search is an index that lags a write by a second or two — the module
6751 /// documentation records it — so a caller that asks again from its last instant should
6752 /// overlap the two by more than that.
6753 async fn updated_since(&self, since: DateTime<Utc>) -> Result<Vec<Resolved>, SourceError> {
6754 let found = self.searched(&updated_qualifier(since)).await?;
6755 self.completed_with_written(found, |_| true)
6756 }
6757
6758 /// Every issue of this board GitHub's issue search reports for the board-scoped search
6759 /// narrowed by `also`, in pages of [`SEARCH_PAGE_SIZE`].
6760 ///
6761 /// Uncompleted: what this process wrote is added by the caller, which knows whether its
6762 /// own record is the fresher of the two.
6763 async fn searched(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
6764 let search = self.board_search(Some(also));
6765 let mut after: Option<String> = None;
6766 let mut found = Vec::new();
6767 loop {
6768 let (page, next) = self
6769 .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
6770 .await?;
6771 found.extend(page);
6772 match next {
6773 Some(next) => after = Some(next),
6774 None => return Ok(found),
6775 }
6776 }
6777 }
6778
6779 /// A bounded task answer; the versioned cursor carries the connection position, how
6780 /// many rows of the page starting there were already handed out, and the own-write ids
6781 /// already observed, including across a new source instance.
6782 ///
6783 /// Every page is sent at [`SEARCH_PAGE_SIZE`] whatever the caller's limit, and a limit is
6784 /// sliced from the pages it needs; why is the module documentation's paging contract.
6785 async fn search_tasks(
6786 &self,
6787 query: &TaskQuery,
6788 page: &PageRequest,
6789 also: &str,
6790 ) -> Result<Page<Task>, SourceError> {
6791 let mut position = match &page.cursor {
6792 None => SearchPosition::default(),
6793 Some(cursor) => serde_json::from_str::<SearchPosition>(&cursor.0)
6794 .ok()
6795 .filter(|position| {
6796 position.version == SEARCH_CURSOR_VERSION
6797 && position.connection.valid_resume(position.offset)
6798 })
6799 .ok_or_else(|| SourceError::Config {
6800 message: "page cursor is invalid".into(),
6801 })?,
6802 };
6803 let search = self.board_search(Some(also));
6804 let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
6805 let own = self.with_own_writes(Vec::new())?;
6806 // An issue this process commented on is a candidate of a comment-activity read
6807 // whether or not the search has caught up with the comment; see `Self::commented`.
6808 let commented = match query.commented_since {
6809 Some(_) => self.commented()?.clone(),
6810 None => Vec::new(),
6811 };
6812 for id in own.iter().map(|item| &item.id).chain(&commented) {
6813 if !position.own.contains(id) {
6814 position.own.push(id.clone());
6815 }
6816 }
6817 let mut tasks = Vec::new();
6818 while !position.connection.exhausted() && tasks.len() < limit {
6819 let first = SEARCH_PAGE_SIZE;
6820 // Page size is part of the key: a short cached answer cannot answer a wider ask.
6821 let key =
6822 serde_json::to_string(&("page", &search, &position.connection.after(), first))
6823 .expect("search page key is serializable");
6824 let cached = if query.commented_since.is_none() {
6825 self.narrowed_cache()?.get(&key).cloned()
6826 } else {
6827 None
6828 };
6829 let (found, next) = match cached {
6830 Some(found) => {
6831 let next = self
6832 .search_next
6833 .lock()
6834 .map_err(|_| SourceError::Unavailable {
6835 message:
6836 "search pagination was left inconsistent; run the command again"
6837 .into(),
6838 })?
6839 .get(&key)
6840 .cloned()
6841 .flatten();
6842 (found, next)
6843 }
6844 None => {
6845 let (found, next) = self
6846 .search_page(&search, first, position.connection.after())
6847 .await?;
6848 if query.commented_since.is_none() {
6849 self.search_next
6850 .lock()
6851 .map_err(|_| SourceError::Unavailable {
6852 message:
6853 "search pagination was left inconsistent; run the command again"
6854 .into(),
6855 })?
6856 .insert(key.clone(), next.clone());
6857 self.narrowed_cache()?.insert(key, found.clone());
6858 }
6859 (found, next)
6860 }
6861 };
6862 let rows = found.len();
6863 for mut item in found.into_iter().skip(position.offset) {
6864 if tasks.len() == limit {
6865 break;
6866 }
6867 position.offset += 1;
6868 if position.own.contains(&item.id) {
6869 if position.seen.contains(&item.id) {
6870 continue;
6871 }
6872 position.seen.push(item.id.clone());
6873 // The search's own copy of an issue this process only commented on is as
6874 // good as a node read of it, since its comments are read either way.
6875 let only_commented = commented.contains(&item.id)
6876 && !own.iter().any(|written| written.id == item.id);
6877 if !only_commented {
6878 let updated_at = item.updated_at;
6879 let Some(written) = self.search_written(&own, &item.id).await? else {
6880 continue;
6881 };
6882 item = written;
6883 item.updated_at = item.updated_at.max(updated_at);
6884 self.resolved_cache()?.insert(item.id.clone(), item.clone());
6885 }
6886 }
6887 if item.kind == BoardKind::Work(ItemKind::Task) {
6888 let task = item.task()?;
6889 if task_matches(&task, query, &query.project)
6890 && self.commented_since(&item, query.commented_since).await?
6891 {
6892 tasks.push(task);
6893 }
6894 }
6895 }
6896 if position.offset < rows {
6897 continue;
6898 }
6899 position.offset = 0;
6900 position.connection = match next {
6901 Some(after) => SearchConnection::Continuing {
6902 after: Cursor(after),
6903 },
6904 None => SearchConnection::Exhausted {},
6905 };
6906 }
6907 if position.connection.exhausted() {
6908 for id in position.own.clone() {
6909 if position.seen.contains(&id) {
6910 continue;
6911 }
6912 if tasks.len() == limit {
6913 break;
6914 }
6915 position.seen.push(id.clone());
6916 let Some(item) = self.search_written(&own, &id).await? else {
6917 continue;
6918 };
6919 if item.kind == BoardKind::Work(ItemKind::Task) {
6920 let task = item.task()?;
6921 if task_matches(&task, query, &query.project)
6922 && self.commented_since(&item, query.commented_since).await?
6923 {
6924 tasks.push(task);
6925 }
6926 }
6927 }
6928 }
6929 let more = !position.connection.exhausted()
6930 || position.own.iter().any(|id| !position.seen.contains(id));
6931 Ok(Page {
6932 items: tasks,
6933 next: more.then(|| {
6934 Cursor(serde_json::to_string(&position).expect("search position is serializable"))
6935 }),
6936 })
6937 }
6938
6939 /// A resumed process has the ids but no write records; resolve only a record the
6940 /// current page needs, by its uncached node read rather than the lagging search index.
6941 async fn search_written(
6942 &self,
6943 own: &[Resolved],
6944 id: &NativeId,
6945 ) -> Result<Option<Resolved>, SourceError> {
6946 match own.iter().find(|item| item.id == *id) {
6947 Some(item) => Ok(Some(item.clone())),
6948 None => self.item_by_id(id).await,
6949 }
6950 }
6951
6952 /// The candidates for a task query carrying a text, metadata or origin predicate, read
6953 /// without enumerating the board — or `None` for a query carrying none of the three, which
6954 /// keeps the reads it always had.
6955 ///
6956 /// An origin is answered by [`Self::origin_candidates`], whatever else the query carries,
6957 /// because it names at most a handful of items. Text and metadata are answered by one
6958 /// board-scoped issue search carrying every term — see [`narrowing_qualifiers`] — narrowed
6959 /// further by `updated:>=` when the query also asks for comment activity, since both
6960 /// qualifiers must hold of an issue the answer keeps. Every candidate is confirmed in
6961 /// process afterwards by the same predicates [`task_matches`] applies to every read.
6962 ///
6963 /// Completed with what this process wrote, its own record winning over the index's copy
6964 /// of the same item: see [`Self::with_own_writes`].
6965 async fn narrowed(&self, query: &TaskQuery) -> Result<Option<Vec<Resolved>>, SourceError> {
6966 self.send_pending_origins(None).await?;
6967 let asked = match (&query.origin, narrowing_qualifiers(query)) {
6968 (Some(origin), _) => Narrowing::Origin(origin.clone()),
6969 (None, Some(qualifiers)) => Narrowing::Search(match query.commented_since {
6970 Some(since) => format!("{} {qualifiers}", updated_qualifier(since)),
6971 None => qualifiers,
6972 }),
6973 (None, None) => return Ok(None),
6974 };
6975 // A question about comment activity is asked afresh every time, as it always was: it
6976 // is the one a caller polls from one source while waiting for the index, and an
6977 // answer held from the first poll would be the answer to every later one.
6978 let key = query.commented_since.is_none().then(|| asked.key());
6979 let cached = match &key {
6980 Some(key) => self.narrowed_cache()?.get(key).cloned(),
6981 None => None,
6982 };
6983 let found = match cached {
6984 Some(found) => found,
6985 None => {
6986 let found = match &asked {
6987 Narrowing::Origin(origin) => self.origin_candidates(origin).await?,
6988 Narrowing::Search(also) => self.searched(also).await?,
6989 };
6990 if let Some(key) = key {
6991 self.narrowed_cache()?.insert(key, found.clone());
6992 }
6993 found
6994 }
6995 };
6996 self.with_own_writes(found).map(Some)
6997 }
6998
6999 /// The candidates for a project or unscoped document query carrying a searchable text,
7000 /// read without enumerating the board — or `None` for a query with no text or a blank one,
7001 /// which keeps the read it always had.
7002 ///
7003 /// The text is sent as the very phrase a task query's text is — see [`text_qualifiers`] —
7004 /// in one board-scoped issue search walked to its end at [`SEARCH_PAGE_SIZE`], so what it
7005 /// costs is the issues that match and never the board. Its answer is held for the command
7006 /// under the same key [`Self::narrowed`] holds that search under, so a walk of the caller's
7007 /// pages asks GitHub once. Every candidate is confirmed afterwards by its kind and by the
7008 /// substring rule, exactly as an item of the wider read was, and is completed with what this
7009 /// process wrote: see [`Self::with_own_writes`].
7010 async fn text_searched(
7011 &self,
7012 text: Option<&TextQuery>,
7013 ) -> Result<Option<Vec<Resolved>>, SourceError> {
7014 let Some(also) = text_qualifiers(text) else {
7015 return Ok(None);
7016 };
7017 let key = Narrowing::Search(also.clone()).key();
7018 let cached = self.narrowed_cache()?.get(&key).cloned();
7019 let found = match cached {
7020 Some(found) => found,
7021 None => {
7022 let found = self.searched(&also).await?;
7023 self.narrowed_cache()?.insert(key, found.clone());
7024 found
7025 }
7026 };
7027 self.with_own_writes(found).map(Some)
7028 }
7029
7030 /// Every `kind` item of this board whose own origin is exactly `origin`: the candidates
7031 /// [`Self::narrowed`] reads for a task query carrying that origin — one
7032 /// [`graphql::ORIGIN_LOOKUP`] walk, held for the command under the same key, completed with
7033 /// what this process wrote — each confirmed against its own origin entry.
7034 ///
7035 /// What answers a project or document listing by origin, so finding every copy of one plan
7036 /// costs the items that carry its origin and never a page of the board.
7037 async fn copied_from(
7038 &self,
7039 origin: &str,
7040 kind: BoardKind,
7041 ) -> Result<Vec<Resolved>, SourceError> {
7042 let wanted = TaskQuery {
7043 origin: Some(origin.to_owned()),
7044 ..TaskQuery::default()
7045 };
7046 Ok(self
7047 .narrowed(&wanted)
7048 .await?
7049 .unwrap_or_default()
7050 .into_iter()
7051 .filter(|item| item.kind == kind && wanted.origin_matches(&item.metadata()))
7052 .collect())
7053 }
7054
7055 /// Every item of this board that may carry `origin` — a superset of those that do — found
7056 /// by [`graphql::ORIGIN_LOOKUP`] and never by enumerating the board.
7057 ///
7058 /// The union of the board's own field filter over the `onetaskgraph.origin` text field —
7059 /// which reads the field every carrier holds, whichever release wrote it — and the
7060 /// board-scoped issue search for the same id as a phrase in the body, where this source
7061 /// mirrors it. The caller adds the third read, this process's own writes. Candidates are
7062 /// returned unconfirmed; [`task_matches`] compares each one's own origin field with the
7063 /// query's, exactly.
7064 ///
7065 /// Both connections are walked to exhaustion, each from its own cursor. One that has
7066 /// already ended is sent its last cursor again, which answers an empty page, so the one
7067 /// document serves every page of either. What the two leave is stated in the module
7068 /// documentation: a carrier another process added within the last second or two, before
7069 /// either index has it.
7070 async fn origin_candidates(&self, origin: &str) -> Result<Vec<Resolved>, SourceError> {
7071 let filter = format!("{ORIGIN_FIELD}:{}", quoted(origin));
7072 let search = self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))));
7073 let mut items_after: Option<String> = None;
7074 let mut search_after: Option<String> = None;
7075 let mut found: Vec<Resolved> = Vec::new();
7076 let keep = |resolved: Resolved, found: &mut Vec<Resolved>| {
7077 if !found.iter().any(|held| held.id == resolved.id) {
7078 found.push(resolved);
7079 }
7080 };
7081 loop {
7082 let data = self
7083 .graphql(
7084 graphql::ORIGIN_LOOKUP,
7085 json!({"owner":self.owner,"number":self.project_number,"filter":filter,
7086 "search":search,"type":"ISSUE","originFirst":ORIGIN_PAGE_SIZE,
7087 "itemsAfter":items_after,"searchAfter":search_after,
7088 "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
7089 "duplicates":true}),
7090 )
7091 .await?;
7092 let items = data
7093 .pointer("/originItems/projectV2/items")
7094 .filter(|value| !value.is_null())
7095 .ok_or_else(|| SourceError::Refused {
7096 message: format!(
7097 "GitHub project {}/{} was not found or is not visible to the token",
7098 self.owner, self.project_number
7099 ),
7100 })?;
7101 for item in optional_nodes(Some(items), "project items")?
7102 .into_iter()
7103 .flatten()
7104 {
7105 // The board's own items list its drafts too, and a draft is not an issue: no
7106 // narrowed read answers with one, whatever its origin field holds.
7107 if let Some(resolved) = self.resolve(item)?
7108 && resolved.content_kind == ContentKind::Issue
7109 {
7110 keep(resolved, &mut found);
7111 }
7112 }
7113 let searched = data.get("search").ok_or_else(|| SourceError::Malformed {
7114 message: "GitHub search response has no search connection".into(),
7115 })?;
7116 for node in optional_nodes(Some(searched), "search")?
7117 .into_iter()
7118 .flatten()
7119 {
7120 if let Some(resolved) = self.resolve_issue(node).await? {
7121 keep(resolved, &mut found);
7122 }
7123 }
7124 let items_next = resumed(items, items_after.as_deref())?;
7125 let search_next = resumed(searched, search_after.as_deref())?;
7126 if !items_next.has_more() && !search_next.has_more() {
7127 return Ok(found);
7128 }
7129 items_after = items_next.cursor();
7130 search_after = search_next.cursor();
7131 }
7132 }
7133
7134 /// `found`, with every item this process created or wrote in its place, and every one of
7135 /// them the read did not report added.
7136 ///
7137 /// This process's own record wins over the read's copy of the same item, because a read
7138 /// of an item written moments ago can still be behind what was written onto it — the
7139 /// origin field included, which is the one a narrowed read is confirmed against — and a
7140 /// read that still names an item under a predicate this process's write moved it out of
7141 /// must not return it. The one thing the read knows that the record cannot is when GitHub
7142 /// last saw the item change, which is what a comment-activity read rules a candidate out
7143 /// by, so the read's `updatedAt` is kept when the record has none of its own. See
7144 /// [`Self::created`] and [`Self::updated`](GitHubProjectsSource::updated).
7145 fn with_own_writes(&self, mut found: Vec<Resolved>) -> Result<Vec<Resolved>, SourceError> {
7146 // A board draft is not an issue, so no narrowed read returns one, and this process
7147 // having written one does not make it an answer either.
7148 let own: Vec<Resolved> = self
7149 .created()?
7150 .iter()
7151 .chain(self.updated()?.iter())
7152 .filter(|own| own.content_kind == ContentKind::Issue)
7153 .cloned()
7154 .collect();
7155 for mut own in own {
7156 self.resolved_cache()?.insert(own.id.clone(), own.clone());
7157 match found.iter_mut().find(|read| read.id == own.id) {
7158 Some(read) => {
7159 own.updated_at = own.updated_at.max(read.updated_at);
7160 *read = own;
7161 }
7162 None => found.push(own),
7163 }
7164 }
7165 Ok(found)
7166 }
7167
7168 /// Whether `item` has a comment created or last edited at or after `since` — always, when
7169 /// there is no instant to hold it to.
7170 ///
7171 /// The candidate's own `updatedAt` is read first, because a comment written or edited at
7172 /// or after the instant moved it there: an issue not updated since holds no such comment,
7173 /// and its comments are never asked for — unless this process commented on it in this
7174 /// command, when the `updatedAt` held may predate that comment; see [`Self::commented`]. Otherwise its comments are walked, oldest first,
7175 /// only as far as the first that matches. A board draft is not an issue and has no
7176 /// comments, so it never matches.
7177 async fn commented_since(
7178 &self,
7179 item: &Resolved,
7180 since: Option<DateTime<Utc>>,
7181 ) -> Result<bool, SourceError> {
7182 let Some(since) = since else {
7183 return Ok(true);
7184 };
7185 if item.content_kind == ContentKind::DraftIssue {
7186 return Ok(false);
7187 }
7188 // An `updatedAt` this process's own record or a lagging index holds can predate a
7189 // comment this process wrote since, so only an issue it did not comment on is ruled
7190 // out by one.
7191 if item.updated_at.is_some_and(|updated| updated < since)
7192 && !self.commented()?.contains(&item.id)
7193 {
7194 return Ok(false);
7195 }
7196 let query = TaskQuery {
7197 commented_since: Some(since),
7198 ..TaskQuery::default()
7199 };
7200 let mut after: Option<String> = None;
7201 loop {
7202 let data = self
7203 .graphql(
7204 graphql::ISSUE_COMMENTS,
7205 json!({"id":item.id.0,"first":MAX_PAGE_SIZE,"after":after}),
7206 )
7207 .await?;
7208 let Some(connection) = data
7209 .get("node")
7210 .filter(|value| !value.is_null())
7211 .and_then(|node| node.get("comments"))
7212 .filter(|value| !value.is_null())
7213 else {
7214 // Removed since the search reported it: no longer an issue with comments.
7215 return Ok(false);
7216 };
7217 let comments = optional_nodes(Some(connection), "issue comments")?
7218 .into_iter()
7219 .flatten()
7220 .map(comment_from)
7221 .collect::<Result<Vec<_>, _>>()?;
7222 if query.comments_match(&comments) {
7223 return Ok(true);
7224 }
7225 match next_cursor(connection)? {
7226 Some(next) => {
7227 validate_cursor_progress(after.as_deref(), &next.0)?;
7228 after = Some(next.0);
7229 }
7230 None => return Ok(false),
7231 }
7232 }
7233 }
7234
7235 /// Every item on the board: the union of both enumerations GitHub offers of one.
7236 ///
7237 /// Neither contains the other, so neither is dropped — only `ProjectV2.items` lists a
7238 /// board **draft** and reads the board's own fields beside its items, and only the search
7239 /// reports an item that connection is behind on. The module documentation is where the lag and the
7240 /// measurements behind it are written down.
7241 ///
7242 /// A search result is admitted on the same terms as any other issue this source reaches
7243 /// directly — [`Self::resolve_issue`] keeps it only if that issue's own `projectItems`
7244 /// names *this* board — so an issue the index still believes is here after it was taken
7245 /// off is refused rather than reported.
7246 ///
7247 /// See [`Self::board_cache`]. Both completions happen on every call rather than once,
7248 /// which is what the cache could otherwise have broken.
7249 async fn board(&self) -> Result<Board, SourceError> {
7250 self.board_completed_by(None).await
7251 }
7252
7253 /// [`Self::board`], its search half narrowed by `also` when it is given — which an issue
7254 /// that half could add is then answered only if it holds.
7255 async fn board_completed_by(&self, also: Option<&str>) -> Result<Board, SourceError> {
7256 let cached = self.board_cache()?.clone();
7257 let mut board = match cached {
7258 Some(board) => board,
7259 None => {
7260 let read = self.read_board().await?;
7261 *self.board_cache()? = Some(read.clone());
7262 read
7263 }
7264 };
7265 let searched = match also {
7266 None => self.searched_issues().await?,
7267 Some(also) => self.searched_narrowly(also).await?,
7268 };
7269 // A complete sweep asks for every page of this view. Reconcile ids once per
7270 // view, rather than scanning the board for every search result on every page.
7271 let mut ids: std::collections::HashSet<_> =
7272 board.items.iter().map(|item| item.id.clone()).collect();
7273 for held in searched {
7274 if ids.insert(held.id.clone()) {
7275 board.items.push(held);
7276 }
7277 }
7278 for own in self.created()?.iter() {
7279 if ids.insert(own.id.clone()) {
7280 board.items.push(own.clone());
7281 }
7282 }
7283 Ok(board)
7284 }
7285
7286 /// This process's own view of the board, or the refusal a poisoned lock is.
7287 fn board_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<Board>>, SourceError> {
7288 self.board_cache
7289 .lock()
7290 .map_err(|_| SourceError::Unavailable {
7291 message: "this source's view of the board was left inconsistent by an earlier \
7292 failure; next: run the command again"
7293 .into(),
7294 })
7295 }
7296
7297 /// Bring this process's own view of the board up to an item it has just written.
7298 ///
7299 /// A created item goes to `created`, which is what completes a board read GitHub's own
7300 /// eventual consistency has left behind. An item that was already there is replaced
7301 /// where it sits, so a second write of it in the same command reads its real parent
7302 /// rather than the one it had before the first write.
7303 ///
7304 /// "Where it sits" is three places, and missing an earlier one leaves a stale record
7305 /// that wins: an item this same run created is held in `created` and not in the cached
7306 /// board, and `board` completes the cached board *from* `created`, so replacing only
7307 /// the cached copy of such an item replaces nothing and the read still reports the
7308 /// title it was created with. The search is the third, and it is the one an item the
7309 /// board's own projection is behind on sits in *alone* — which is exactly the item this
7310 /// source is least able to re-read, so leaving it out would put the stale title back on
7311 /// the only items the completion in [`Self::board`] exists for.
7312 fn remember_written(&self, item: Resolved, created: bool) -> Result<(), SourceError> {
7313 self.resolved_cache()?.insert(item.id.clone(), item.clone());
7314 if created {
7315 self.created()?.push(item);
7316 return Ok(());
7317 }
7318 {
7319 let mut own = self.created()?;
7320 if let Some(held) = own.iter_mut().find(|held| held.id == item.id) {
7321 *held = item;
7322 return Ok(());
7323 }
7324 }
7325 {
7326 let mut own = self.updated()?;
7327 match own.iter_mut().find(|held| held.id == item.id) {
7328 Some(held) => *held = item.clone(),
7329 None => own.push(item.clone()),
7330 }
7331 }
7332 if let Some(board) = self.board_cache()?.as_mut()
7333 && let Some(held) = board.items.iter_mut().find(|held| held.id == item.id)
7334 {
7335 *held = item.clone();
7336 }
7337 if let Some(found) = self.search_cache()?.as_mut()
7338 && let Some(held) = found.iter_mut().find(|held| held.id == item.id)
7339 {
7340 *held = item.clone();
7341 }
7342 for found in self.narrowed_cache()?.values_mut() {
7343 if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
7344 *held = item.clone();
7345 }
7346 }
7347 for (_, found) in self.children_cache()?.values_mut() {
7348 if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
7349 *held = item.clone();
7350 }
7351 }
7352 Ok(())
7353 }
7354
7355 /// Forget one item this process has just deleted, from every half of its own view.
7356 fn forget(&self, id: &NativeId) -> Result<(), SourceError> {
7357 if let Some(Deferred::Held { created, .. }) = self.deferred()?.as_mut() {
7358 created.retain(|held| held.content != *id);
7359 }
7360 self.resolved_cache()?.remove(id);
7361 self.read_ahead()?.remove(id);
7362 self.carried()?.remove(id);
7363 self.created()?.retain(|own| own.id != *id);
7364 self.updated()?.retain(|own| own.id != *id);
7365 self.commented()?.retain(|own| own != id);
7366 if let Some(board) = self.board_cache()?.as_mut() {
7367 board.items.retain(|item| item.id != *id);
7368 }
7369 if let Some(found) = self.search_cache()?.as_mut() {
7370 found.retain(|item| item.id != *id);
7371 }
7372 for found in self.narrowed_cache()?.values_mut() {
7373 found.retain(|item| item.id != *id);
7374 }
7375 for (_, found) in self.children_cache()?.values_mut() {
7376 found.retain(|item| item.id != *id);
7377 }
7378 Ok(())
7379 }
7380
7381 /// This process's own record of each narrowed answer, or the refusal a poisoned lock is.
7382 fn narrowed_cache(
7383 &self,
7384 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<String, Vec<Resolved>>>, SourceError> {
7385 self.narrowed_cache
7386 .lock()
7387 .map_err(|_| SourceError::Unavailable {
7388 message: "this source's view of a narrowed read was left inconsistent by an \
7389 earlier failure; next: run the command again"
7390 .into(),
7391 })
7392 }
7393
7394 /// This process's own record of each project's sub-issues, or the refusal a poisoned lock
7395 /// is.
7396 /// The tasks `project` holds, as a copy's batched read found them this command, when it did.
7397 fn held_task_children(&self, project: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
7398 Ok(self.task_children()?.get(project).cloned())
7399 }
7400
7401 fn task_children(
7402 &self,
7403 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Vec<Resolved>>>, SourceError> {
7404 self.task_children
7405 .lock()
7406 .map_err(|_| SourceError::Unavailable {
7407 message: "this source's record of a project's tasks was left inconsistent by an \
7408 earlier failure; next: run the command again"
7409 .into(),
7410 })
7411 }
7412
7413 fn children_cache(&self) -> Result<std::sync::MutexGuard<'_, ProjectChildren>, SourceError> {
7414 self.children_cache
7415 .lock()
7416 .map_err(|_| SourceError::Unavailable {
7417 message: "this source's view of a project's tasks was left inconsistent by an \
7418 earlier failure; next: run the command again"
7419 .into(),
7420 })
7421 }
7422
7423 /// Every page of the board, read from GitHub.
7424 async fn read_board(&self) -> Result<Board, SourceError> {
7425 let mut after: Option<String> = None;
7426 let mut items = Vec::new();
7427 let mut board;
7428 loop {
7429 let page = self.board_page(after.as_deref(), MAX_PAGE_SIZE).await?;
7430 for item in page
7431 .pointer("/items/nodes")
7432 .and_then(Value::as_array)
7433 .ok_or_else(|| SourceError::Malformed {
7434 message: "GitHub project items.nodes is not an array".into(),
7435 })?
7436 {
7437 if let Some(resolved) = self.resolve(item)? {
7438 items.push(resolved);
7439 }
7440 }
7441 let info = page
7442 .pointer("/items/pageInfo")
7443 .ok_or_else(|| SourceError::Malformed {
7444 message: "GitHub project items have no pageInfo".into(),
7445 })?;
7446 let has_next = required_bool(info, "hasNextPage")?;
7447 let next = has_next
7448 .then(|| required_str(info, "endCursor"))
7449 .transpose()?;
7450 board = page.clone();
7451 match next {
7452 Some(next) => {
7453 validate_cursor_progress(after.as_deref(), next)?;
7454 after = Some(next.to_owned());
7455 }
7456 None => break,
7457 }
7458 }
7459 Ok(Board {
7460 id: required_str(&board, "id")?.to_owned(),
7461 fields: board.get("fields").cloned().unwrap_or(Value::Null),
7462 items,
7463 })
7464 }
7465
7466 /// The existing items this source has written, for completing a narrowed read that is
7467 /// behind; see [`Self::updated`](GitHubProjectsSource::updated).
7468 fn updated(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
7469 self.updated.lock().map_err(|_| SourceError::Unavailable {
7470 message: "this source's record of what it wrote in this run was left inconsistent \
7471 by an earlier failure; next: run the command again"
7472 .into(),
7473 })
7474 }
7475
7476 /// The issues this source has commented on in this command; see
7477 /// [`Self::commented`](GitHubProjectsSource::commented).
7478 fn commented(&self) -> Result<std::sync::MutexGuard<'_, Vec<NativeId>>, SourceError> {
7479 self.commented.lock().map_err(|_| SourceError::Unavailable {
7480 message: "this source's record of what it commented on in this run was left \
7481 inconsistent by an earlier failure; next: run the command again"
7482 .into(),
7483 })
7484 }
7485
7486 /// Called only once GitHub has answered the comment write, so an issue whose comment
7487 /// failed is never made a candidate a later read would pay a node read for.
7488 fn remember_commented(&self, issue: &NativeId) -> Result<(), SourceError> {
7489 let mut commented = self.commented()?;
7490 if !commented.contains(issue) {
7491 commented.push(issue.clone());
7492 }
7493 Ok(())
7494 }
7495
7496 /// The items this source has created, for completing a board read that is behind.
7497 fn created(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
7498 self.created.lock().map_err(|_| SourceError::Unavailable {
7499 message: "this source's record of what it created in this run was left \
7500 inconsistent by an earlier failure; next: run the command again"
7501 .into(),
7502 })
7503 }
7504
7505 /// One board item as this source reports it, or `None` for content it ignores.
7506 ///
7507 /// A pull request is neither a project nor a task — it is somebody's change, not a
7508 /// unit of plan — and an item whose content the token cannot see has nothing to
7509 /// report at all.
7510 fn resolve(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
7511 let resolved = self.resolve_unremembered(item)?;
7512 if let Some(resolved) = &resolved {
7513 self.resolved_cache()?
7514 .insert(resolved.id.clone(), resolved.clone());
7515 }
7516 Ok(resolved)
7517 }
7518
7519 /// [`Self::resolve`], without remembering the item it resolves: what reading a token off a
7520 /// write's own response needs, which says nothing this process should answer a later read
7521 /// with.
7522 fn resolve_unremembered(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
7523 let content = item.get("content").ok_or_else(|| SourceError::Malformed {
7524 message: "GitHub project item is missing content".into(),
7525 })?;
7526 if content.is_null() {
7527 return Ok(None);
7528 }
7529 let content_kind = match required_str(content, "__typename")? {
7530 "Issue" => ContentKind::Issue,
7531 "DraftIssue" => ContentKind::DraftIssue,
7532 _ => return Ok(None),
7533 };
7534 let field_values = item
7535 .get("fieldValues")
7536 .ok_or_else(|| SourceError::Malformed {
7537 message: "GitHub project item is missing fieldValues".into(),
7538 })?;
7539 complete_connection(field_values, "project item field values", NESTED_PAGE_SIZE)?;
7540 let nodes = field_values
7541 .get("nodes")
7542 .and_then(Value::as_array)
7543 .ok_or_else(|| SourceError::Malformed {
7544 message: "GitHub project item fieldValues.nodes is not an array".into(),
7545 })?;
7546 if let Some(labels) = content.get("labels") {
7547 complete_connection(labels, "content labels", NESTED_PAGE_SIZE)?;
7548 }
7549 let raw_body = optional_str(content, "body")?.map(str::to_owned);
7550 let (body, slot) = self.visible(raw_body.clone())?;
7551 let parent = optional_str(content.get("parent").unwrap_or(&Value::Null), "id")?
7552 .map(|id| NativeId(id.to_owned()));
7553 // A draft has no sub-issues to summarise, and GitHub's schema gives it no field
7554 // to read one from; it is a task, and never a project.
7555 let sub_issues = match content_kind {
7556 ContentKind::Issue => sub_issue_total(content)?,
7557 ContentKind::DraftIssue => 0,
7558 };
7559 let content_id = required_str(content, "id")?;
7560 let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
7561 message: format!("GitHub issue {content_id}: {message}"),
7562 })?;
7563 let raw_title = required_str(content, "title")?;
7564 let kind = BoardKind::of(raw_title, parent.is_some(), sub_issues, marked);
7565 // The title a person wrote, which for a document is the one without the prefix —
7566 // the same way `content` above is the body without this source's metadata slot.
7567 let title = match kind {
7568 BoardKind::Document => raw_title[DESIGN_TITLE_PREFIX.len()..].to_owned(),
7569 BoardKind::Work(_) => raw_title.to_owned(),
7570 };
7571 let own_repository = content
7572 .pointer("/repository/nameWithOwner")
7573 .and_then(Value::as_str)
7574 .map(|origin| Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST)))
7575 .transpose()
7576 .map_err(|message| SourceError::Malformed { message })?;
7577 let repositories = if slot.contains_key(Repository::METADATA_KEY) {
7578 Repository::from_metadata(&slot)
7579 .map_err(|message| SourceError::Malformed { message })?
7580 } else {
7581 own_repository.clone().into_iter().collect()
7582 };
7583 let classification = Classification::from_metadata(&slot)
7584 .map_err(|message| SourceError::Malformed { message })?;
7585 let id = NativeId(content_id.to_owned());
7586 // Read only for a task, because only a task has either list: a project or a
7587 // document holding one of these keys holds nothing this source reports, and the
7588 // keys are left out of its caller-visible metadata all the same.
7589 let (delivers, delivered_by) = if kind == BoardKind::Work(ItemKind::Task) {
7590 let listed = |key: &str| {
7591 TaskRef::from_value(key, &id, Some(&self.name), slot.get(key))
7592 .map_err(|message| SourceError::Malformed { message })
7593 };
7594 (
7595 listed(TaskRef::DELIVERS_KEY)?,
7596 listed(TaskRef::DELIVERED_BY_KEY)?,
7597 )
7598 } else {
7599 (Vec::new(), Vec::new())
7600 };
7601 let (option, closed, reason) = Self::status_parts(nodes, content)?;
7602 let priority = self.held_priority(nodes)?;
7603 let item_labels = labels(content)?;
7604 // Present when the item was reached through its own issue, whose board entry
7605 // names the board; a read of the board's own items has the board already. An
7606 // empty id names nothing a field write could address, so it is read as absent and
7607 // the write goes back to reading the board.
7608 let board_id = item
7609 .pointer("/project/id")
7610 .and_then(Value::as_str)
7611 .filter(|id| !id.is_empty());
7612 let resolved = Resolved {
7613 item_id: required_str(item, "id")?.to_owned(),
7614 id,
7615 content_kind,
7616 kind,
7617 title,
7618 body: body.filter(|value| !value.is_empty()),
7619 raw_body,
7620 status: self
7621 .statuses
7622 .status(kind.status_kind(), option, closed, reason),
7623 option: option.map(str::to_owned),
7624 priority,
7625 closed,
7626 reason: closed.then(|| reason.map(str::to_owned)).flatten(),
7627 delivers,
7628 delivered_by,
7629 labels: item_labels.clone(),
7630 parent,
7631 origin: text_field(nodes, ORIGIN_FIELD)?.filter(|value| !value.is_empty()),
7632 projected: self.held_projections(nodes)?,
7633 number: match content_kind {
7634 ContentKind::Issue => Some(issue_number(content)?),
7635 // A draft is filed in no repository, so nothing ever numbered it:
7636 // `DraftIssue` declares no `number` at all, exactly as it declares no
7637 // `subIssuesSummary` the branch above reads.
7638 ContentKind::DraftIssue => None,
7639 },
7640 url: optional_str(content, "url")?.map(str::to_owned),
7641 created_at: optional_time(content, "createdAt")?,
7642 updated_at: optional_time(content, "updatedAt")?,
7643 item_updated_at: optional_time(item, "updatedAt")?,
7644 comment_count: content
7645 .pointer("/commentCount/totalCount")
7646 .and_then(Value::as_u64),
7647 relationship_events: content
7648 .pointer("/relationshipEvents/totalCount")
7649 .and_then(Value::as_u64),
7650 // The issue node is stamped wherever it was read; a board item a read of the issue
7651 // put together from its memberships is not.
7652 served_at: match optional_time(content, SERVED_AT)? {
7653 Some(served) => Some(served),
7654 None => optional_time(item, SERVED_AT)?,
7655 },
7656 // Both connections were read to their end above — one reporting a further page is
7657 // refused — so these are every field value and every label the item holds. A
7658 // response that did not select the labels at all says nothing of them, and gives
7659 // no facets.
7660 facets: content.get("labels").is_some().then(|| {
7661 change_token::Facets::new(kind.facet(), facet_fields(nodes), &item_labels).digest()
7662 }),
7663 own_repository,
7664 repositories,
7665 classification,
7666 slot,
7667 board_id: board_id.map(str::to_owned),
7668 fields: field_definitions(nodes),
7669 board_fields: Self::carried_board_fields(content, board_id)?,
7670 blocked_by: carried_blocked_by(content)?,
7671 };
7672 Ok(Some(resolved))
7673 }
7674
7675 /// The field definitions of the board `board_id` names — the project this issue's own
7676 /// board item is on — off the `boards` page a read of an issue by its own id carries, or
7677 /// `None` when the read carried none, carried no entry for that board, or the board item
7678 /// named no board, which a write then answers by reading the board's fields itself.
7679 ///
7680 /// Matched by the board's node id and never by its number alone: a project number is
7681 /// unique only within its owner, so another owner's board numbered alike can sit on the
7682 /// same page, and its field and option ids address nothing on this one.
7683 fn carried_board_fields(
7684 content: &Value,
7685 board_id: Option<&str>,
7686 ) -> Result<Option<Value>, SourceError> {
7687 let (Some(nodes), Some(board_id)) = (
7688 content.pointer("/boards/nodes").and_then(Value::as_array),
7689 board_id,
7690 ) else {
7691 return Ok(None);
7692 };
7693 let Some(board) = nodes.iter().find_map(|node| {
7694 let project = node.get("project")?;
7695 (project.get("id").and_then(Value::as_str) == Some(board_id)).then_some(project)
7696 }) else {
7697 return Ok(None);
7698 };
7699 let Some(fields) = board.get("fields").filter(|fields| !fields.is_null()) else {
7700 return Ok(None);
7701 };
7702 complete_connection(fields, "board fields", NESTED_PAGE_SIZE)?;
7703 Ok(Some(fields.clone()))
7704 }
7705
7706 /// What one board item's `Priority` field says, through this instance's mapping.
7707 ///
7708 /// An instance with no mapping holds no priority, so every item reads as `none` whatever
7709 /// its board holds. With one, no value is `none`, a mapped option is its level, and an
7710 /// option the mapping does not name is kept as itself — never read as a level or as
7711 /// `none` — for a read of the task to report by name.
7712 fn held_priority(&self, field_values: &[Value]) -> Result<HeldPriority, SourceError> {
7713 let Some(mapping) = &self.priorities else {
7714 return Ok(HeldPriority::Read(Priority::None));
7715 };
7716 // A value of the field that names no option — a text field someone called `Priority` —
7717 // is malformed rather than `none`: reading it as no priority would let the next copy
7718 // clear one a person set.
7719 let Some(option) = field_values
7720 .iter()
7721 .find(|value| {
7722 value.pointer("/field/name").and_then(Value::as_str) == Some(PRIORITY_FIELD)
7723 })
7724 .map(|value| required_str(value, "name"))
7725 .transpose()?
7726 else {
7727 return Ok(HeldPriority::Read(Priority::None));
7728 };
7729 Ok(mapping.priority_of(option).map_or_else(
7730 || HeldPriority::Unmapped(option.to_owned()),
7731 HeldPriority::Read,
7732 ))
7733 }
7734
7735 /// What each projected metadata field holds for one board item, by field name — every text
7736 /// value GitHub answers, an empty one included.
7737 fn held_projections(
7738 &self,
7739 field_values: &[Value],
7740 ) -> Result<BTreeMap<String, String>, SourceError> {
7741 let mut held = BTreeMap::new();
7742 for projection in &self.metadata_fields {
7743 if let Some(text) = text_field(field_values, &projection.field)? {
7744 held.insert(projection.field.to_string(), text);
7745 }
7746 }
7747 Ok(held)
7748 }
7749
7750 /// What one board item's status is read from: its `Status` option, whether its issue
7751 /// is closed, and the reason it was closed with. [`BoardStatuses::status`] turns the
7752 /// three into the status it reports.
7753 fn status_parts<'a>(
7754 field_values: &'a [Value],
7755 content: &'a Value,
7756 ) -> Result<(Option<&'a str>, bool, Option<&'a str>), SourceError> {
7757 let option = field_values
7758 .iter()
7759 .find(|value| value.pointer("/field/name").and_then(Value::as_str) == Some("Status"))
7760 .map(|value| required_str(value, "name"))
7761 .transpose()?;
7762 let closed = optional_str(content, "state")? == Some("CLOSED");
7763 Ok((option, closed, optional_str(content, "stateReason")?))
7764 }
7765
7766 /// The board Status option this write selects, or the refusal that says why not.
7767 ///
7768 /// The mapped option is required for both open and terminal targets. A terminal write
7769 /// validates it before changing either representation, so it can never fall back to
7770 /// closing an issue whose board cannot display the matching status.
7771 ///
7772 /// Answers the field's id, the option's id, and the option's name as the board spells
7773 /// it — which is the name a read of the item reports once it sits there.
7774 fn column_for(
7775 &self,
7776 fields: &Value,
7777 kind: ItemKind,
7778 category: StatusCategory,
7779 target: &StatusTarget,
7780 ) -> Result<Option<(String, String, String)>, SourceError> {
7781 let Some(wanted) = target.option() else {
7782 return Ok(None);
7783 };
7784 let missing = |detail: &str| SourceError::Refused {
7785 message: format!(
7786 "{} status {} of source {} needs the board Status option {wanted:?}, and \
7787 {detail}; next: add that option to the board, which `onetaskgraph sources \
7788 fields {} --apply` does, or point status_mapping.{}.{} of this source at one \
7789 it has",
7790 kind.marker(),
7791 category_name(category),
7792 self.name,
7793 self.name,
7794 category_name(category),
7795 kind.marker()
7796 ),
7797 };
7798 let Some(field) = Board::field(fields, "Status")? else {
7799 return Err(missing("this board has no Status field"));
7800 };
7801 if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
7802 return Err(missing(
7803 "this board's Status field is not a single-select field",
7804 ));
7805 }
7806 let option = field
7807 .get("options")
7808 .and_then(Value::as_array)
7809 .and_then(|options| {
7810 options.iter().find(|option| {
7811 option
7812 .get("name")
7813 .and_then(Value::as_str)
7814 .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
7815 })
7816 });
7817 match option {
7818 None => Err(missing("this board does not have it")),
7819 Some(option) => Ok(Some((
7820 required_str(field, "id")?.to_owned(),
7821 required_str(option, "id")?.to_owned(),
7822 required_str(option, "name")?.to_owned(),
7823 ))),
7824 }
7825 }
7826
7827 /// The refusal a status that closes an issue is answered with over a board draft.
7828 fn closes_a_draft(&self, category: StatusCategory) -> SourceError {
7829 SourceError::Refused {
7830 message: format!(
7831 "status {} of source {} closes the item's issue, and GitHub draft items have \
7832 no open or closed state",
7833 category_name(category),
7834 self.name
7835 ),
7836 }
7837 }
7838
7839 /// What a status write to one item needs of the board: the board's id and the
7840 /// definition of its `Status` field, read off the item when the item says both.
7841 ///
7842 /// The same reasoning as [`Self::fields_for`]: a node read of the item names its board,
7843 /// and its `Status` value carries that field's definition, options and all. An item that
7844 /// does not say — no board id, or no `Status` value to read the field off — takes them
7845 /// from [`Self::board_fields`], which reads no item.
7846 async fn status_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
7847 if let Some(board) = item.carried_board() {
7848 return Ok(board);
7849 }
7850 if item.defines("Status")
7851 && let Some(board_id) = item.named_board()
7852 {
7853 return Ok(BoardFields {
7854 id: board_id,
7855 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
7856 });
7857 }
7858 self.board_fields().await
7859 }
7860
7861 /// Set one task's or one project's status and nothing else; see
7862 /// [`TaskSource::set_task_status`] and [`TaskSource::set_project_status`].
7863 async fn set_status(
7864 &self,
7865 id: &NativeId,
7866 kind: ItemKind,
7867 category: StatusCategory,
7868 ) -> Result<Option<Status>, SourceError> {
7869 // Refused before anything is read, in the words a write of the same status is.
7870 let target = self.resolved_target(kind, category)?;
7871 // An item read by issue reference carries only what this write needs, so what it
7872 // becomes is never remembered for a later read to be answered from.
7873 let held = self.take_held(id)?;
7874 let by_reference = held.is_some();
7875 let bound = match held {
7876 Some(item) => Some(item),
7877 None => self.bound_item(id).await?,
7878 };
7879 let Some(mut item) = bound.filter(|item| item.kind == BoardKind::Work(kind)) else {
7880 return Ok(None);
7881 };
7882 let board = self.status_board(&item).await?;
7883 let (field, option, name) = self
7884 .column_for(&board.fields, kind, category, &target)?
7885 .ok_or_else(|| SourceError::Malformed {
7886 message: format!(
7887 "status {} of source {} names no board Status option",
7888 category_name(category),
7889 self.name
7890 ),
7891 })?;
7892 if item.status.category == category && item.option.as_deref() == Some(&name) {
7893 return Ok(Some(item.status));
7894 }
7895 match &target {
7896 StatusTarget::Terminal(_, reason) => {
7897 if item.content_kind == ContentKind::DraftIssue {
7898 return Err(self.closes_a_draft(category));
7899 }
7900 // The option first, so a refused selection leaves an open issue open rather
7901 // than closed behind a card that still reads open.
7902 self.set_item_field(
7903 board.id.as_str(),
7904 &item.item_id,
7905 &field,
7906 json!({"singleSelectOptionId": option}),
7907 )
7908 .await?;
7909 // An issue already closed in a way that reads as this category keeps its state
7910 // and its reason: only its card was out of line, and closing it again would
7911 // overwrite a reason — a duplicate's — that says more than this write does.
7912 let already = item.closed && item.status.category == category;
7913 if !already {
7914 self.update_content(
7915 ContentKind::Issue,
7916 &item.id,
7917 json!({"stateInput": state_input(Some(&target))}),
7918 )
7919 .await?;
7920 }
7921 let reason = if already {
7922 item.reason.clone()
7923 } else {
7924 Some(reason.reason().to_owned())
7925 };
7926 item.closed = true;
7927 item.status = self
7928 .statuses
7929 .status(kind, Some(&name), true, reason.as_deref());
7930 item.option = Some(name);
7931 item.reason = reason;
7932 }
7933 StatusTarget::Column(_) => {
7934 // An option is what an open item's status is, so a closed issue is reopened
7935 // first — sitting closed in the column, it would read back as closed. A draft has
7936 // no state to reopen.
7937 if item.content_kind == ContentKind::Issue && item.closed {
7938 self.update_content(
7939 ContentKind::Issue,
7940 &item.id,
7941 json!({"stateInput": state_input(Some(&target))}),
7942 )
7943 .await?;
7944 item.closed = false;
7945 }
7946 self.set_item_field(
7947 board.id.as_str(),
7948 &item.item_id,
7949 &field,
7950 json!({"singleSelectOptionId": option}),
7951 )
7952 .await?;
7953 item.status = self.statuses.status(kind, Some(&name), false, None);
7954 item.option = Some(name);
7955 }
7956 StatusTarget::Disabled(_) => {
7957 unreachable!("resolved_target refused a disabled status")
7958 }
7959 }
7960 let status = item.status.clone();
7961 if !by_reference {
7962 self.remember_written(item, false)?;
7963 }
7964 Ok(Some(status))
7965 }
7966
7967 /// Replace one task's `delivered_by` and nothing else; see
7968 /// [`TaskSource::set_delivered_by`].
7969 ///
7970 /// One update of the body, which differs from the body GitHub holds only inside the
7971 /// metadata slot — see [`with_slot`]. A body that would not change is not sent at all.
7972 async fn replace_delivered_by(
7973 &self,
7974 id: &NativeId,
7975 delivered_by: &[TaskRef],
7976 ) -> Result<Option<()>, SourceError> {
7977 let entries = TaskRef::listed(
7978 TaskRef::DELIVERED_BY_KEY,
7979 id,
7980 Some(&self.name),
7981 delivered_by.to_vec(),
7982 )
7983 .map_err(|message| SourceError::Refused { message })?;
7984 let Some(mut item) = self
7985 .bound_item(id)
7986 .await?
7987 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
7988 else {
7989 return Ok(None);
7990 };
7991 let mut slot = item.slot.clone();
7992 set_task_list(&mut slot, TaskRef::DELIVERED_BY_KEY, &entries);
7993 self.write_slot(&mut item, &slot).await?;
7994 item.delivered_by = entries;
7995 self.remember_written(item, false)?;
7996 Ok(Some(()))
7997 }
7998
7999 /// Set one caller key of the metadata slot of one issue of `kind`, and nothing else;
8000 /// see [`TaskSource::set_task_metadata`].
8001 ///
8002 /// `None` when this board holds no item by that id, or holds one of another kind. The
8003 /// answer is the item as this source now reads it, so what a caller is told the key
8004 /// holds is what the slot holds.
8005 ///
8006 /// A key already holding the value is answered without a write, compared as JSON rather
8007 /// than as the body's bytes: a slot a person spelled with other whitespace would
8008 /// otherwise be re-encoded, which is a write that changes nothing the caller asked for.
8009 async fn set_slot_key(
8010 &self,
8011 id: &NativeId,
8012 kind: BoardKind,
8013 key: &MetadataKey,
8014 value: &Value,
8015 ) -> Result<Option<Resolved>, SourceError> {
8016 let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
8017 return Ok(None);
8018 };
8019 if item.slot.get(key.as_str()) == Some(value) {
8020 return Ok(Some(item));
8021 }
8022 let mut slot = item.slot.clone();
8023 slot.insert(key.as_str().to_owned(), value.clone());
8024 // A key a board text field projects moves that field first, so the body — the value's
8025 // one authoritative home — is written last, as every write of an existing item is.
8026 if self
8027 .metadata_fields
8028 .iter()
8029 .any(|projection| &*projection.key == key.as_str())
8030 {
8031 let board = self.projection_board(&item).await?;
8032 let (writes, projected) = self.projection_writes(
8033 &board.fields,
8034 Some(&item.projected),
8035 &slot,
8036 Some(&[key.as_str()]),
8037 )?;
8038 let (mut values, mut clears) = (Vec::new(), Vec::new());
8039 for write in &writes {
8040 write.join(&mut values, &mut clears);
8041 }
8042 self.set_item_fields(board.id.as_str(), &item.item_id, &values, &clears)
8043 .await?;
8044 item.projected = projected;
8045 }
8046 self.write_slot(&mut item, &slot).await?;
8047 self.remember_written(item.clone(), false)?;
8048 Ok(Some(item))
8049 }
8050
8051 /// What a projection write to `item` needs of the board, read off the item when it says
8052 /// enough, as [`Self::fields_for`] reads it.
8053 async fn projection_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
8054 if let Some(board) = item.carried_board() {
8055 return Ok(board);
8056 }
8057 if let Some(board_id) = item.named_board()
8058 && self
8059 .metadata_fields
8060 .iter()
8061 .all(|projection| item.defines(&projection.field))
8062 {
8063 return Ok(BoardFields {
8064 id: board_id,
8065 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
8066 });
8067 }
8068 self.board_fields().await
8069 }
8070
8071 /// Put `slot` in one item's metadata slot with a single update of its body, and bring
8072 /// `item` up to what that write left.
8073 ///
8074 /// The body sent differs from the body GitHub holds only inside the slot — see
8075 /// [`with_slot`] — and a body that would not change is not sent at all. It goes through
8076 /// the mutation the item's content takes, so a board draft's body is written with
8077 /// `updateProjectV2DraftIssue` exactly as an issue's is with `updateIssue`.
8078 async fn write_slot(
8079 &self,
8080 item: &mut Resolved,
8081 slot: &BTreeMap<String, Value>,
8082 ) -> Result<(), SourceError> {
8083 let held = item.raw_body.clone().unwrap_or_default();
8084 let body = with_slot(&held, slot)?;
8085 if body != held {
8086 self.update_content(item.content_kind, &item.id, json!({"body": body}))
8087 .await?;
8088 }
8089 let (visible, slot) = self.visible(Some(body.clone()))?;
8090 item.body = visible.filter(|value| !value.is_empty());
8091 item.raw_body = Some(body);
8092 item.slot = slot;
8093 Ok(())
8094 }
8095
8096 /// This instance's target for a category written to an item of `kind`, refusing one
8097 /// that kind has no option for — before anything is read or written.
8098 ///
8099 /// Nothing here mutates the board's option set to make room for a status. GitHub
8100 /// documents `UpdateProjectV2FieldInput.singleSelectOptions` as *"provided values
8101 /// overwrite existing options"*, so no addition is additive and a mistake destroys the
8102 /// field and every item's status.
8103 fn resolved_target(
8104 &self,
8105 kind: ItemKind,
8106 category: StatusCategory,
8107 ) -> Result<StatusTarget, SourceError> {
8108 let target = self.statuses.target(kind, category).clone();
8109 let StatusTarget::Disabled(why) = target else {
8110 return Ok(target);
8111 };
8112 let refusal = why.refusal(&self.name, category, kind);
8113 // Why there is no shipped default, which is the question a person meeting this
8114 // refusal on a source that never mentioned the category asks.
8115 let shipped_none = match category {
8116 StatusCategory::Draft => Some(
8117 "draft has no shipped default because GitHub draft issues cannot have \
8118 sub-issues, and this source stores a project's tasks as its issue's sub-issues",
8119 ),
8120 StatusCategory::Unknown => Some(
8121 "unknown has no shipped default because this board keeps no open-ended status \
8122 word: every word classified unknown is written to the one board Status option \
8123 status_mapping.unknown names",
8124 ),
8125 _ => None,
8126 };
8127 Err(match (refusal, shipped_none, why) {
8128 (SourceError::Refused { message }, Some(note), UnmappedStatus::Unconfigured) => {
8129 SourceError::Refused {
8130 message: format!("{message}; {note}"),
8131 }
8132 }
8133 (refusal, _, _) => refusal,
8134 })
8135 }
8136
8137 /// What writing `priority` does to one item's `Priority` field on this board, or the
8138 /// refusal naming what the board lacks.
8139 ///
8140 /// `none` is no value, so it clears the field — and asks nothing of an item that holds
8141 /// none already, or of an item not created yet. Every other priority selects the option
8142 /// the mapping names, matched case-insensitively; a board with no `Priority` field, or
8143 /// without that option, is refused rather than given one: reads and writes never create
8144 /// a field or an option.
8145 fn priority_write(
8146 &self,
8147 fields: &Value,
8148 existing: Option<&Resolved>,
8149 priority: Priority,
8150 ) -> Result<Option<PriorityWrite>, SourceError> {
8151 let Some(mapping) = &self.priorities else {
8152 return Err(self.holds_no_priority());
8153 };
8154 let Some(wanted) = mapping.option(priority) else {
8155 if !existing.is_some_and(Resolved::holds_priority) {
8156 return Ok(None);
8157 }
8158 let field =
8159 Board::field(fields, PRIORITY_FIELD)?.ok_or_else(|| SourceError::Malformed {
8160 message: format!(
8161 "an item holding a {PRIORITY_FIELD} value was read without that field"
8162 ),
8163 })?;
8164 return Ok(Some(PriorityWrite::Clear {
8165 field: required_str(field, "id")?.to_owned(),
8166 }));
8167 };
8168 let missing = |detail: &str| SourceError::Refused {
8169 message: format!(
8170 "priority {priority} of source {} needs the board {PRIORITY_FIELD} option \
8171 {wanted:?}, and {detail}; run `onetaskgraph sources fields {} --apply` to add \
8172 it, or point priority_mapping.{priority} of this source at an option the board \
8173 has",
8174 self.name, self.name
8175 ),
8176 };
8177 let Some(field) = Board::field(fields, PRIORITY_FIELD)? else {
8178 return Err(missing(&format!(
8179 "this board has no {PRIORITY_FIELD} field"
8180 )));
8181 };
8182 if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
8183 return Err(missing(&format!(
8184 "this board's {PRIORITY_FIELD} field is not a single-select field"
8185 )));
8186 }
8187 // An options list that is absent or not a list is an answer this source cannot read,
8188 // not a board lacking the option: `sources fields --apply` is no remedy for it.
8189 let option = field
8190 .get("options")
8191 .and_then(Value::as_array)
8192 .ok_or_else(|| SourceError::Malformed {
8193 message: format!("GitHub {PRIORITY_FIELD} field options is not an array"),
8194 })?
8195 .iter()
8196 .find(|option| {
8197 option
8198 .get("name")
8199 .and_then(Value::as_str)
8200 .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
8201 })
8202 .ok_or_else(|| missing("this board does not have it"))?;
8203 Ok(Some(PriorityWrite::Select {
8204 field: required_str(field, "id")?.to_owned(),
8205 option: required_str(option, "id")?.to_owned(),
8206 }))
8207 }
8208
8209 /// What writing `metadata` does to each projected text field of one item, and what those
8210 /// fields hold after it — or the refusal, before anything is sent.
8211 ///
8212 /// `held` is what the item's fields hold now, `None` for an item not created yet. `keys`
8213 /// narrows the work to the entries projecting one of those metadata keys, for a write that
8214 /// touches only them; `None` is every entry. A string the field already holds, and nothing
8215 /// for a field holding nothing, sends nothing. A value no text field can hold is refused,
8216 /// and so is a board without the field, or with a field of that name that is not a text
8217 /// field: reads and writes never create a field.
8218 fn projection_writes(
8219 &self,
8220 fields: &Value,
8221 held: Option<&BTreeMap<String, String>>,
8222 metadata: &BTreeMap<String, Value>,
8223 keys: Option<&[&str]>,
8224 ) -> Result<(Vec<ProjectionWrite>, BTreeMap<String, String>), SourceError> {
8225 let mut projected = held.cloned().unwrap_or_default();
8226 let mut writes = Vec::new();
8227 for projection in self
8228 .metadata_fields
8229 .iter()
8230 .filter(|projection| keys.is_none_or(|keys| keys.contains(&&*projection.key)))
8231 {
8232 let wanted = projection.projected(metadata, &self.name)?;
8233 let field = self.projection_field(fields, projection)?;
8234 let holding = projected.get(&*projection.field);
8235 match wanted {
8236 Projected::Text(text) if holding != Some(&text) => {
8237 writes.push(ProjectionWrite::Set {
8238 field,
8239 text: text.clone(),
8240 });
8241 projected.insert(projection.field.to_string(), text);
8242 }
8243 Projected::Nothing if holding.is_some() => {
8244 writes.push(ProjectionWrite::Clear { field });
8245 projected.remove(&*projection.field);
8246 }
8247 Projected::Text(_) | Projected::Nothing => {}
8248 }
8249 }
8250 Ok((writes, projected))
8251 }
8252
8253 /// The id of the board text field one projection writes, or the refusal naming what the
8254 /// board has instead.
8255 fn projection_field(
8256 &self,
8257 fields: &Value,
8258 projection: &MetadataField,
8259 ) -> Result<String, SourceError> {
8260 let refuse = |detail: String| SourceError::Refused {
8261 message: format!(
8262 "source {} projects metadata key {:?} onto the board text field {:?}, and \
8263 {detail}; next: run `onetaskgraph sources fields {} --apply` to create it as a \
8264 text field, or name another field in metadata_fields",
8265 self.name, &*projection.key, &*projection.field, self.name
8266 ),
8267 };
8268 let Some(field) = Board::field(fields, &projection.field)? else {
8269 return Err(refuse("this board has no field of that name".to_owned()));
8270 };
8271 let typename = optional_str(field, "__typename")?.unwrap_or("field of another type");
8272 let data_type = optional_str(field, "dataType")?;
8273 if typename != "ProjectV2Field" || data_type != Some("TEXT") {
8274 return Err(refuse(format!(
8275 "this board's field of that name is not a text field (it is a {}), so it is \
8276 left as it is; rename or remove that field first",
8277 data_type.unwrap_or(typename)
8278 )));
8279 }
8280 Ok(required_str(field, "id")?.to_owned())
8281 }
8282
8283 /// Apply one priority write to one board item.
8284 async fn write_priority(
8285 &self,
8286 board_id: &str,
8287 item_id: &str,
8288 write: &PriorityWrite,
8289 ) -> Result<(), SourceError> {
8290 match write {
8291 PriorityWrite::Select { field, option } => {
8292 self.set_item_field(
8293 board_id,
8294 item_id,
8295 field,
8296 json!({"singleSelectOptionId": option}),
8297 )
8298 .await
8299 }
8300 PriorityWrite::Clear { field } => {
8301 let data = self
8302 .graphql(
8303 graphql::CLEAR_FIELD,
8304 json!({"input":{"projectId":board_id,"itemId":item_id,"fieldId":field},
8305 "readPriority":false,"priorityName":PRIORITY_FIELD}),
8306 )
8307 .await?;
8308 let returned = data
8309 .pointer("/clearProjectV2ItemFieldValue/projectV2Item")
8310 .ok_or_else(|| SourceError::Malformed {
8311 message: "GitHub field clear returned no project item".into(),
8312 })?;
8313 if required_str(returned, "id")? != item_id {
8314 return Err(SourceError::Malformed {
8315 message: "GitHub field clear returned the wrong project item".into(),
8316 });
8317 }
8318 Ok(())
8319 }
8320 }
8321 }
8322
8323 /// The refusal a priority is answered with by an instance configured with no
8324 /// `priority_mapping`, which holds none.
8325 fn holds_no_priority(&self) -> SourceError {
8326 SourceError::Refused {
8327 message: format!(
8328 "source {} holds no task priority: its configuration sets no priority_mapping; \
8329 next: set priority_mapping on this source, then run `onetaskgraph sources \
8330 fields {} --apply` to set its board up",
8331 self.name, self.name
8332 ),
8333 }
8334 }
8335
8336 /// Set one task's priority and nothing else; see [`TaskSource::set_task_priority`].
8337 ///
8338 /// One field write — a select, or a clear for `none` — and no title, body, label, state
8339 /// or `Status` request. Clearing a priority an item does not hold sends nothing.
8340 async fn set_priority(
8341 &self,
8342 id: &NativeId,
8343 priority: Priority,
8344 ) -> Result<Option<Priority>, SourceError> {
8345 if self.priorities.is_none() {
8346 return Err(self.holds_no_priority());
8347 }
8348 let Some(mut item) = self
8349 .bound_item(id)
8350 .await?
8351 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8352 else {
8353 return Ok(None);
8354 };
8355 if priority == Priority::None && !item.holds_priority() {
8356 return Ok(Some(priority));
8357 }
8358 // The item's own read carries the field's definition whenever it holds a value of
8359 // it, which a clear always does; a select onto an item holding none reads the board.
8360 let board = match (item.carried_board(), item.named_board()) {
8361 (Some(board), _) => board,
8362 (None, Some(id)) if item.defines(PRIORITY_FIELD) => BoardFields {
8363 id,
8364 fields: json!({"nodes": item.fields.clone(), "pageInfo": {"hasNextPage": false}}),
8365 },
8366 _ => self.board_fields().await?,
8367 };
8368 let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? else {
8369 return Ok(Some(priority));
8370 };
8371 let (document, root, input) = match write {
8372 PriorityWrite::Select { field, option } => (
8373 graphql::UPDATE_FIELD,
8374 "updateProjectV2ItemFieldValue",
8375 json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field,"value":{"singleSelectOptionId":option}}),
8376 ),
8377 PriorityWrite::Clear { field } => (
8378 graphql::CLEAR_FIELD,
8379 "clearProjectV2ItemFieldValue",
8380 json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field}),
8381 ),
8382 };
8383 let data = self
8384 .graphql(
8385 document,
8386 json!({"input":input,"readPriority":true,"priorityName":PRIORITY_FIELD}),
8387 )
8388 .await?;
8389 let returned = data
8390 .get(root)
8391 .and_then(|value| value.get("projectV2Item"))
8392 .ok_or_else(|| SourceError::Malformed {
8393 message: "GitHub priority write returned no project item".into(),
8394 })?;
8395 if required_str(returned, "id")? != item.item_id {
8396 return Err(SourceError::Malformed {
8397 message: "GitHub priority write returned the wrong project item".into(),
8398 });
8399 }
8400 let value = returned
8401 .get("fieldValueByName")
8402 .ok_or_else(|| SourceError::Malformed {
8403 message: "GitHub priority write returned no priority read-back".into(),
8404 })?;
8405 if !value.is_null()
8406 && value.pointer("/field/name").and_then(Value::as_str) != Some(PRIORITY_FIELD)
8407 {
8408 return Err(SourceError::Malformed {
8409 message: "GitHub priority read-back is not a Priority field value".into(),
8410 });
8411 }
8412 let values = if value.is_null() {
8413 Vec::new()
8414 } else {
8415 vec![value.clone()]
8416 };
8417 item.priority = self.held_priority(&values)?;
8418 let answer = item.task()?.priority;
8419 self.remember_written(item, false)?;
8420 Ok(Some(answer))
8421 }
8422
8423 /// Replace one task's visible body and nothing else; see
8424 /// [`TaskSource::set_task_content`].
8425 ///
8426 /// One update of the body, which differs from the body GitHub holds only outside the
8427 /// metadata slot — the slot is kept byte for byte, so every caller key and every list
8428 /// this source keeps there reads back as it was. A body that would not change is not
8429 /// sent at all.
8430 async fn replace_content(
8431 &self,
8432 id: &NativeId,
8433 content: &str,
8434 ) -> Result<Option<()>, SourceError> {
8435 let Some(mut item) = self
8436 .bound_item(id)
8437 .await?
8438 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8439 else {
8440 return Ok(None);
8441 };
8442 let held = item.raw_body.clone().unwrap_or_default();
8443 let body = with_content(&held, &self.marked(content))?;
8444 // Checked before anything is sent: content ending in what this source reads as its own
8445 // metadata slot would read back as metadata rather than as the content it was.
8446 let (visible, slot) = self.visible(Some(body.clone()))?;
8447 if visible.as_deref().unwrap_or_default() != content || slot != item.slot {
8448 return Err(SourceError::Refused {
8449 message: format!(
8450 "this content ends in what source {} reads as its own metadata slot \
8451 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8452 as content; next: remove that trailing block from the content",
8453 self.name
8454 ),
8455 });
8456 }
8457 if body != held {
8458 self.update_content(item.content_kind, &item.id, json!({"body": body}))
8459 .await?;
8460 }
8461 item.body = visible.filter(|value| !value.is_empty());
8462 item.raw_body = Some(body);
8463 item.slot = slot;
8464 self.remember_written(item, false)?;
8465 Ok(Some(()))
8466 }
8467
8468 /// Apply one targeted update to one task; see [`TaskSource::update_task`].
8469 ///
8470 /// One read of the item — which carries the board's field definitions and the issue's
8471 /// `blockedBy`, so neither is read again — and then only what differs from it: the
8472 /// `Status` option and the `Priority` field together in one request, the `blockedBy`
8473 /// additions and removals the named edges differ by, and last one `updateIssue` carrying
8474 /// the title, the body — visible content and metadata slot together — and a state change.
8475 /// So an update naming any of title, body, metadata, status and priority is one read and
8476 /// at most two writes. The body goes last so that a write refused part-way leaves it, and
8477 /// the metadata in it, as it stood. A terminal status selects its option and then closes,
8478 /// as a whole write does; an open one selects its option and then reopens. The origin
8479 /// field is never written: an update is of an item that already exists, whose origin is
8480 /// what it is.
8481 ///
8482 /// The task answered is the item as those writes left it, built from the read and what was
8483 /// sent rather than read again — the same record a later read in this run answers from.
8484 async fn targeted_update(
8485 &self,
8486 id: &NativeId,
8487 update: &TaskUpdate,
8488 ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
8489 // Everything this source can refuse without reading the item is refused first, in the
8490 // words a whole write of the same fields is refused with.
8491 update.consistent()?;
8492 if update
8493 .title
8494 .as_deref()
8495 .is_some_and(|title| title.starts_with(DESIGN_TITLE_PREFIX))
8496 {
8497 return Err(SourceError::Refused {
8498 message: format!(
8499 "the title of this task begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
8500 spells a document, so it would read back as one rather than as a task; \
8501 retitle it",
8502 self.name
8503 ),
8504 });
8505 }
8506 if let Some(delivers) = &update.delivers {
8507 TaskRef::listed(
8508 TaskRef::DELIVERS_KEY,
8509 id,
8510 Some(&self.name),
8511 delivers.clone(),
8512 )
8513 .map_err(|message| SourceError::Refused { message })?;
8514 }
8515 if self.priorities.is_none()
8516 && update
8517 .priority
8518 .is_some_and(|priority| priority != Priority::None)
8519 {
8520 return Err(self.holds_no_priority());
8521 }
8522 let target = update
8523 .status
8524 .as_ref()
8525 .map(|status| self.resolved_target(ItemKind::Task, status.category))
8526 .transpose()?;
8527 let Some(mut item) = self
8528 .bound_item(id)
8529 .await?
8530 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8531 else {
8532 return Ok(None);
8533 };
8534 let before = item.task()?;
8535
8536 let mut status_move = None;
8537 if let (Some(status), Some(target)) = (&update.status, target) {
8538 let board = self.status_board(&item).await?;
8539 let (field, option, name) = self
8540 .column_for(&board.fields, ItemKind::Task, status.category, &target)?
8541 .ok_or_else(|| SourceError::Malformed {
8542 message: format!(
8543 "status {} of source {} names no board Status option",
8544 category_name(status.category),
8545 self.name
8546 ),
8547 })?;
8548 let terminal = matches!(target, StatusTarget::Terminal(_, _));
8549 if terminal && item.content_kind == ContentKind::DraftIssue {
8550 return Err(self.closes_a_draft(status.category));
8551 }
8552 let landed = match &target {
8553 StatusTarget::Terminal(_, reason) => {
8554 self.statuses
8555 .status(ItemKind::Task, Some(&name), true, Some(reason.reason()))
8556 }
8557 _ => self
8558 .statuses
8559 .status(ItemKind::Task, Some(&name), false, None),
8560 };
8561 let option_moves = item
8562 .option
8563 .as_deref()
8564 .is_none_or(|held| !held.eq_ignore_ascii_case(&name));
8565 let state_moves = item.content_kind == ContentKind::Issue
8566 && (item.closed != terminal || (terminal && item.status != landed));
8567 if let Some(moves) = Moves::of(option_moves, state_moves) {
8568 status_move = Some(StatusMove {
8569 board: board.id,
8570 field,
8571 option,
8572 name,
8573 target,
8574 landed,
8575 moves,
8576 });
8577 }
8578 }
8579
8580 let mut priority_move = None;
8581 if let Some(priority) = update.priority
8582 && self.priorities.is_some()
8583 && item.priority != HeldPriority::Read(priority)
8584 {
8585 let board = match (item.carried_board(), item.named_board()) {
8586 (Some(board), _) => board,
8587 (None, Some(board)) if item.defines(PRIORITY_FIELD) => BoardFields {
8588 id: board,
8589 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
8590 },
8591 _ => self.board_fields().await?,
8592 };
8593 if let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? {
8594 priority_move = Some((board.id, write, priority));
8595 }
8596 }
8597
8598 // The metadata keys this update names that a board text field projects, and what
8599 // that does to those fields — refused, as a whole write refuses it, before anything
8600 // is sent.
8601 let touched: Vec<&str> = update
8602 .metadata_set
8603 .keys()
8604 .chain(update.metadata_remove.iter())
8605 .map(MetadataKey::as_str)
8606 .collect();
8607 let mut projection_move = None;
8608 if self
8609 .metadata_fields
8610 .iter()
8611 .any(|projection| touched.contains(&&*projection.key))
8612 {
8613 let mut metadata = item.slot.clone();
8614 for (key, value) in &update.metadata_set {
8615 metadata.insert(key.as_str().to_owned(), value.clone());
8616 }
8617 for key in &update.metadata_remove {
8618 metadata.remove(key.as_str());
8619 }
8620 let board = self.projection_board(&item).await?;
8621 let (writes, projected) = self.projection_writes(
8622 &board.fields,
8623 Some(&item.projected),
8624 &metadata,
8625 Some(&touched),
8626 )?;
8627 projection_move = Some((board.id, writes, projected));
8628 }
8629
8630 // Resolved before the body is composed, because a far end `blockedBy` cannot name is
8631 // recorded in the slot, and the slot travels in the one body update below.
8632 let edges = match &update.depends_on {
8633 Some(edges) => Some(
8634 self.partition_edges(
8635 BoardKind::Work(ItemKind::Task),
8636 item.content_kind,
8637 item.blocked_by.as_deref(),
8638 edges,
8639 )
8640 .await?,
8641 ),
8642 None => None,
8643 };
8644
8645 // A record primed from what a producer wrote holds no body anybody read, so an update
8646 // changing the body reads it first: by the `Status` write's own answer when the update
8647 // moves the option — the mutation answering with the issue whole — and otherwise by
8648 // reading the issue. The body is then composed against what GitHub holds now.
8649 let mut option_selected = false;
8650 // The mark stays until a read lands, so an update that writes no body, or one whose
8651 // read fails, leaves the next update of it to read the body too.
8652 if (update.content.is_some()
8653 || !update.metadata_set.is_empty()
8654 || !update.metadata_remove.is_empty()
8655 || update.delivers.is_some()
8656 || edges.is_some())
8657 && self.is_unread(id)
8658 {
8659 let live = match status_move.as_ref().filter(|moving| moving.moves.option()) {
8660 Some(moving) => {
8661 option_selected = true;
8662 Some(
8663 self.select_reading_body(
8664 moving.board.as_str(),
8665 &item.item_id,
8666 &moving.field,
8667 &moving.option,
8668 )
8669 .await?,
8670 )
8671 }
8672 None => {
8673 self.forget(id)?;
8674 let Some(read) = self.item_by_id(id).await? else {
8675 return Ok(None);
8676 };
8677 read.raw_body
8678 }
8679 };
8680 let (visible, read_slot) = self.visible(live.clone())?;
8681 self.mark_read(id);
8682 item.raw_body = live;
8683 item.body = visible;
8684 item.slot = read_slot;
8685 }
8686
8687 let mut slot = item.slot.clone();
8688 for (key, value) in &update.metadata_set {
8689 slot.insert(key.as_str().to_owned(), value.clone());
8690 }
8691 for key in &update.metadata_remove {
8692 slot.remove(key.as_str());
8693 }
8694 if let Some(delivers) = &update.delivers {
8695 set_task_list(&mut slot, TaskRef::DELIVERS_KEY, delivers);
8696 }
8697 if let Some((_, recorded)) = &edges {
8698 record_edges(&mut slot, recorded);
8699 }
8700 let held = item.raw_body.clone().unwrap_or_default();
8701 let content = match &update.content {
8702 Some(content) => with_content(&held, &self.marked(content))?,
8703 None => held.clone(),
8704 };
8705 // A slot holding what it held is kept byte for byte, compared as JSON rather than as
8706 // the body's bytes, as a metadata write compares it: a slot a person spelled with
8707 // other whitespace would otherwise be re-encoded, which is a write nobody asked for.
8708 let body = if slot == item.slot {
8709 content
8710 } else {
8711 with_slot(&content, &slot)?
8712 };
8713 // Checked before anything is sent, as a content write checks it: content ending in
8714 // what this source reads as its own slot would read back as metadata.
8715 let (visible, read) = self.visible(Some(body.clone()))?;
8716 let wanted = update.content.as_deref().or(item.body.as_deref());
8717 if visible.as_deref().unwrap_or_default() != wanted.unwrap_or_default() || read != slot {
8718 return Err(SourceError::Refused {
8719 message: format!(
8720 "this content ends in what source {} reads as its own metadata slot \
8721 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8722 as content; next: remove that trailing block from the content",
8723 self.name
8724 ),
8725 });
8726 }
8727 let recorded_moves =
8728 slot.get(DependencyEdge::RECORDED_KEY) != item.slot.get(DependencyEdge::RECORDED_KEY);
8729
8730 // One `updateIssue` carries all three, because every mutation spends the secondary
8731 // limiter and the title, body and state are one mutation's inputs.
8732 let mut fields = serde_json::Map::new();
8733 if let Some(title) = update.title.as_ref().filter(|title| **title != item.title) {
8734 fields.insert("title".to_owned(), json!(title));
8735 }
8736 if body != held {
8737 fields.insert("body".to_owned(), json!(body));
8738 }
8739 if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.state()) {
8740 fields.insert("stateInput".to_owned(), state_input(Some(&moving.target)));
8741 }
8742 // **The body is written last, and that is the guarantee a refusal part-way keeps.**
8743 // GitHub runs no two requests as one, and runs one document's mutation fields in order
8744 // without undoing an earlier field when a later one fails — so a body written before a
8745 // board field the board then refused would be left changed. Written after every other
8746 // write has landed, a refusal anywhere leaves the item's body, and every metadata key
8747 // it carries, exactly as they stood. So the `Status` option and the `Priority` field go
8748 // first, together in one request — a terminal option selected before the issue
8749 // closes, as a whole write does — then the `blockedBy` difference, then the body.
8750 let mut board_writes: Vec<(&BoardId, (String, Value))> = Vec::new();
8751 let mut clears: Vec<(&BoardId, &str)> = Vec::new();
8752 if let Some(moving) = status_move
8753 .as_ref()
8754 .filter(|moving| moving.moves.option() && !option_selected)
8755 {
8756 board_writes.push((
8757 &moving.board,
8758 (
8759 moving.field.clone(),
8760 json!({"singleSelectOptionId": moving.option}),
8761 ),
8762 ));
8763 }
8764 match &priority_move {
8765 Some((board, PriorityWrite::Select { field, option }, _)) => board_writes.push((
8766 board,
8767 (field.clone(), json!({"singleSelectOptionId": option})),
8768 )),
8769 Some((board, PriorityWrite::Clear { field }, _)) => clears.push((board, field)),
8770 None => {}
8771 }
8772 if let Some((board, writes, _)) = &projection_move {
8773 for write in writes {
8774 let (mut values, mut cleared) = (Vec::new(), Vec::new());
8775 write.join(&mut values, &mut cleared);
8776 board_writes.extend(values.into_iter().map(|value| (board, value)));
8777 clears.extend(cleared.into_iter().map(|field| (board, field)));
8778 }
8779 }
8780 let mut boards: Vec<&BoardId> = board_writes.iter().map(|(board, _)| *board).collect();
8781 boards.extend(clears.iter().map(|(board, _)| *board));
8782 boards.dedup_by(|one, other| one.as_str() == other.as_str());
8783 for board in boards {
8784 let writes = board_writes
8785 .iter()
8786 .filter(|(on, _)| on.as_str() == board.as_str())
8787 .map(|(_, write)| write.clone())
8788 .collect::<Vec<_>>();
8789 let cleared = clears
8790 .iter()
8791 .filter(|(on, _)| on.as_str() == board.as_str())
8792 .map(|(_, field)| *field)
8793 .collect::<Vec<_>>();
8794 self.set_item_fields(board.as_str(), &item.item_id, &writes, &cleared)
8795 .await?;
8796 }
8797 let mut blocked_by_moved = false;
8798 if let Some((native, _)) = &edges
8799 && item.content_kind == ContentKind::Issue
8800 {
8801 blocked_by_moved = self
8802 .reconcile_blocked_by(
8803 &item.id,
8804 native,
8805 Issue::Existing(item.blocked_by.as_deref()),
8806 )
8807 .await?;
8808 }
8809 if !fields.is_empty() {
8810 self.update_content(item.content_kind, &item.id, Value::Object(fields))
8811 .await?;
8812 }
8813
8814 // The `blockedBy` this item was read with is what the write just changed, so it is no
8815 // answer any more: a later write in this command would reconcile against it — re-adding
8816 // what it kept and never removing what it dropped — and a forward-edge read would
8817 // answer with it. Left unknown, each asks GitHub instead.
8818 if blocked_by_moved {
8819 item.blocked_by = None;
8820 }
8821 if let Some(title) = &update.title {
8822 item.title.clone_from(title);
8823 }
8824 item.body = visible.filter(|value| !value.is_empty());
8825 item.raw_body = (!body.is_empty() || item.raw_body.is_some()).then_some(body);
8826 item.slot = slot;
8827 if let Some(delivers) = &update.delivers {
8828 item.delivers.clone_from(delivers);
8829 }
8830 if let Some(moving) = status_move {
8831 item.closed = matches!(moving.target, StatusTarget::Terminal(_, _))
8832 && item.content_kind == ContentKind::Issue;
8833 item.status = moving.landed;
8834 item.option = Some(moving.name);
8835 }
8836 if let Some((_, _, priority)) = priority_move {
8837 item.priority = HeldPriority::Read(priority);
8838 }
8839 if let Some((_, _, projected)) = projection_move {
8840 item.projected = projected;
8841 }
8842 let task = item.task()?;
8843 let mut written = update.changed(&before, &task);
8844 if blocked_by_moved || recorded_moves {
8845 written.insert(UpdatedField::DependsOn);
8846 }
8847 self.remember_written(item, false)?;
8848 Ok(Some(TaskUpdateOutcome {
8849 task,
8850 written,
8851 delivers_before: before.delivers,
8852 }))
8853 }
8854
8855 /// Replace one issue's visible body and its [`MetadataKey::TEMPLATE_KEY`] slot entry
8856 /// together, and nothing else; see [`TaskSource::set_task_rendering`].
8857 ///
8858 /// One update of the body: the content outside the slot, and inside it that one entry,
8859 /// every other entry kept as it was. This source keeps no template answers — an issue has
8860 /// no room beside itself that is not its body, and answers written there would duplicate
8861 /// what the content already says and count against GitHub's body limit — so `answers`
8862 /// reaches nothing here. A body that would not change is not sent at all.
8863 async fn replace_rendering(
8864 &self,
8865 id: &NativeId,
8866 kind: BoardKind,
8867 content: &str,
8868 provenance: &Value,
8869 assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
8870 ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
8871 let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
8872 return Ok(None);
8873 };
8874 let held = item.raw_body.clone().unwrap_or_default();
8875 let mut slot = item.slot.clone();
8876 slot.insert(MetadataKey::TEMPLATE_KEY.to_owned(), provenance.clone());
8877 let rewritten;
8878 let content = if let Some(assets) = assets {
8879 let uploads = self
8880 .upload_assets(item.own_repository.as_ref(), assets)
8881 .await?;
8882 rewritten =
8883 onetaskgraph_plugin_api::serve_asset_references(content, &mut slot, &uploads);
8884 rewritten.as_str()
8885 } else {
8886 content
8887 };
8888 let body = with_slot(&with_content(&held, &self.marked(content))?, &slot)?;
8889 // Checked before anything is sent, as a content write checks it.
8890 let (visible, read) = self.visible(Some(body.clone()))?;
8891 if visible.as_deref().unwrap_or_default() != content || read != slot {
8892 return Err(SourceError::Refused {
8893 message: format!(
8894 "this content ends in what source {} reads as its own metadata slot \
8895 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8896 as content; next: remove that trailing block from the template",
8897 self.name
8898 ),
8899 });
8900 }
8901 if body != held {
8902 self.update_content(item.content_kind, &item.id, json!({"body": body}))
8903 .await?;
8904 }
8905 item.body = visible.filter(|value| !value.is_empty());
8906 item.raw_body = Some(body);
8907 item.slot = read;
8908 self.remember_written(item, false)?;
8909 Ok(Some(onetaskgraph_plugin_api::AssetsWritten {
8910 id: id.clone(),
8911 content: Some(content.to_owned()),
8912 }))
8913 }
8914
8915 async fn set_item_field(
8916 &self,
8917 board_id: &str,
8918 item_id: &str,
8919 field_id: &str,
8920 value: Value,
8921 ) -> Result<(), SourceError> {
8922 let data = self
8923 .graphql(
8924 graphql::UPDATE_FIELD,
8925 json!({"input":{
8926 "projectId":board_id,"itemId":item_id,"fieldId":field_id,"value":value
8927 },"readPriority":false,"priorityName":PRIORITY_FIELD}),
8928 )
8929 .await?;
8930 field_update_item(&data, item_id).map(|_| ())
8931 }
8932
8933 /// GitHub accepts one value per field mutation; aliases combine those mutations in
8934 /// one request — writes in the order given, then clears. A lone write is
8935 /// [`graphql::UPDATE_FIELD`] and a lone clear [`graphql::CLEAR_FIELD`]; more than one
8936 /// request's slots hold go in further requests of the same document, in order. Every
8937 /// returned item id is checked, including optional aliases.
8938 async fn set_item_fields(
8939 &self,
8940 board: &str,
8941 item: &str,
8942 fields: &[(String, Value)],
8943 clears: &[&str],
8944 ) -> Result<(), SourceError> {
8945 match (fields, clears) {
8946 ([], []) => return Ok(()),
8947 ([(field, value)], []) => {
8948 return self.set_item_field(board, item, field, value.clone()).await;
8949 }
8950 ([], [field]) => {
8951 return self
8952 .write_priority(
8953 board,
8954 item,
8955 &PriorityWrite::Clear {
8956 field: (*field).to_owned(),
8957 },
8958 )
8959 .await;
8960 }
8961 _ => {}
8962 }
8963 // Any field of the request stands in for an input a slot leaves out, which GitHub
8964 // still requires to be well formed although it never runs it.
8965 let filler = fields
8966 .first()
8967 .map_or_else(|| clears[0], |(field, _)| field.as_str());
8968 let mut writes = fields.iter();
8969 let mut cleared = clears.iter();
8970 loop {
8971 let batch: Vec<&(String, Value)> =
8972 writes.by_ref().take(FIELD_WRITE_SLOTS.len()).collect();
8973 let batch_clears: Vec<&&str> = cleared.by_ref().take(FIELD_CLEAR_SLOTS.len()).collect();
8974 if batch.is_empty() && batch_clears.is_empty() {
8975 return Ok(());
8976 }
8977 let mut variables = serde_json::Map::new();
8978 for (index, slot) in FIELD_WRITE_SLOTS.iter().enumerate() {
8979 let input = match batch.get(index) {
8980 Some((field, value)) => {
8981 json!({"projectId":board,"itemId":item,"fieldId":field,"value":value})
8982 }
8983 None => json!({"projectId":board,"itemId":item,"fieldId":filler,
8984 "value":{"text":""}}),
8985 };
8986 variables.insert(slot.variable.to_owned(), input);
8987 variables.insert(slot.include.to_owned(), json!(index < batch.len()));
8988 }
8989 for (index, slot) in FIELD_CLEAR_SLOTS.iter().enumerate() {
8990 let field = batch_clears.get(index).map_or(filler, |field| **field);
8991 variables.insert(
8992 slot.variable.to_owned(),
8993 json!({"projectId":board,"itemId":item,"fieldId":field}),
8994 );
8995 variables.insert(slot.include.to_owned(), json!(index < batch_clears.len()));
8996 }
8997 let data = self
8998 .graphql(graphql::UPDATE_FIELDS, Value::Object(variables))
8999 .await?;
9000 let sent = FIELD_WRITE_SLOTS[..batch.len()]
9001 .iter()
9002 .chain(&FIELD_CLEAR_SLOTS[..batch_clears.len()]);
9003 for slot in sent {
9004 let alias = slot.alias;
9005 let returned = data
9006 .get(alias)
9007 .and_then(|value| value.get("projectV2Item"))
9008 .ok_or_else(|| SourceError::Malformed {
9009 message: format!("GitHub field update {alias} returned no project item"),
9010 })?;
9011 if required_str(returned, "id")? != item {
9012 return Err(SourceError::Malformed {
9013 message: format!(
9014 "GitHub field update {alias} returned the wrong project item"
9015 ),
9016 });
9017 }
9018 }
9019 }
9020 }
9021
9022 async fn native_dependency_ids(&self, id: &NativeId) -> Result<Vec<String>, SourceError> {
9023 let mut after: Option<String> = None;
9024 let mut ids = Vec::new();
9025 loop {
9026 let data = self
9027 .graphql(
9028 graphql::ISSUE_DEPENDENCIES,
9029 json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after}),
9030 )
9031 .await?;
9032 let connection =
9033 data.pointer("/node/blockedBy")
9034 .ok_or_else(|| SourceError::Malformed {
9035 message: "GitHub dependency response has no blockedBy connection".into(),
9036 })?;
9037 ids.extend(
9038 connection
9039 .get("nodes")
9040 .and_then(Value::as_array)
9041 .ok_or_else(|| SourceError::Malformed {
9042 message: "GitHub dependency response nodes is not an array".into(),
9043 })?
9044 .iter()
9045 .map(|value| required_str(value, "id").map(str::to_owned))
9046 .collect::<Result<Vec<_>, _>>()?,
9047 );
9048 let next = next_cursor(connection)?;
9049 if let Some(next) = &next {
9050 validate_cursor_progress(after.as_deref(), &next.0)?;
9051 }
9052 after = next.map(|cursor| cursor.0);
9053 if after.is_none() {
9054 return Ok(ids);
9055 }
9056 }
9057 }
9058
9059 async fn dependencies(
9060 &self,
9061 id: &NativeId,
9062 near_kind: ItemKind,
9063 direction: Direction,
9064 page: &PageRequest,
9065 ) -> Result<Page<DependencyEdge>, SourceError> {
9066 validate_page(page)?;
9067 let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
9068 let cursor = page.cursor.as_ref().map(|c| c.0.as_str());
9069 let recorded = recorded_offset(cursor, direction)?;
9070 // What this issue is blocked by, when a read of it by its own id in this command
9071 // already carried the whole connection — a copy reads the item it writes before it
9072 // reads its edges — and the page asked for is the whole of it, or the recorded tail
9073 // after it. Answered from that read, in the shape the dependency read answers in;
9074 // anything else is asked of GitHub.
9075 let carried = match direction {
9076 Direction::DependsOn => self
9077 .resolved_cache()?
9078 .get(id)
9079 .filter(|item| item.content_kind == ContentKind::Issue)
9080 .and_then(|item| Some((item.blocked_by.clone()?, item.raw_body.clone()))),
9081 Direction::DependedOnBy => None,
9082 }
9083 .filter(|(nodes, _)| recorded.is_some() || (cursor.is_none() && nodes.len() <= limit));
9084 // Asked for even in the recorded phase, whose page reads nothing from the
9085 // connection: `__typename` is what says whether this item has a native
9086 // relationship at all, and that is what decides which far ends the reserved key is
9087 // allowed to hold.
9088 let data = match carried {
9089 Some((nodes, body)) => json!({"node":{"__typename":"Issue","body":body,
9090 "blockedBy":{"nodes":nodes,"pageInfo":{"hasNextPage":false,"endCursor":null}}}}),
9091 None => {
9092 let after = if recorded.is_some() { None } else { cursor };
9093 let first = page.limit.min(MAX_PAGE_SIZE);
9094 match direction {
9095 Direction::DependsOn => {
9096 self.graphql(
9097 graphql::ISSUE_DEPENDS_ON,
9098 json!({"id":id.0,"first":first,"after":after,"owner":self.owner,
9099 "number":self.project_number,
9100 "boardItems":BOARD_ITEMS_PAGE_SIZE}),
9101 )
9102 .await?
9103 }
9104 Direction::DependedOnBy => {
9105 self.graphql(
9106 graphql::ISSUE_DEPENDED_ON_BY,
9107 json!({"id":id.0,"first":first,"after":after}),
9108 )
9109 .await?
9110 }
9111 }
9112 }
9113 };
9114 let draft_slot = draft_slot(&data, id, self.project_number)?;
9115 let node =
9116 data.get("node")
9117 .filter(|v| !v.is_null())
9118 .ok_or_else(|| SourceError::Refused {
9119 message: format!(
9120 "GitHub item {} was not found or does not support dependencies",
9121 id.0
9122 ),
9123 })?;
9124 let connection_name = match direction {
9125 Direction::DependsOn => "blockedBy",
9126 Direction::DependedOnBy => "blocking",
9127 };
9128 // A draft has neither `blockedBy` nor `blocking`, so nothing it depends on can be
9129 // named natively and the reserved key may hold any far end. An issue's connections
9130 // hold issues, and this source reads them at the near item's own level.
9131 let natively_names = (required_str(node, "__typename")? == "Issue").then_some(near_kind);
9132 if let Some(offset) = recorded {
9133 return Ok(recorded_page(
9134 self.recorded_edges(
9135 id,
9136 near_kind,
9137 direction,
9138 natively_names,
9139 node,
9140 draft_slot.as_ref(),
9141 )
9142 .await?,
9143 offset,
9144 limit,
9145 ));
9146 }
9147 if natively_names.is_none() {
9148 return Ok(recorded_page(
9149 self.recorded_edges(
9150 id,
9151 near_kind,
9152 direction,
9153 natively_names,
9154 node,
9155 draft_slot.as_ref(),
9156 )
9157 .await?,
9158 0,
9159 limit,
9160 ));
9161 }
9162 let connection = node
9163 .get(connection_name)
9164 .ok_or_else(|| SourceError::Malformed {
9165 message: "GitHub dependency response is missing its connection".into(),
9166 })?;
9167 let nodes = connection
9168 .get("nodes")
9169 .and_then(Value::as_array)
9170 .ok_or_else(|| SourceError::Malformed {
9171 message: "GitHub dependency response nodes is not an array".into(),
9172 })?;
9173 // `from` depends on `to`, always. GitHub spells the same relationship from either
9174 // end — `blockedBy` lists what this item waits on, `blocking` lists what waits on
9175 // it — so the near item is `from` in one direction and `to` in the other.
9176 let items = nodes
9177 .iter()
9178 .map(|value| {
9179 let related = NativeId(required_str(value, "id")?.into());
9180 let related_kind = related_kind(value)?;
9181 let (from, to) = match direction {
9182 Direction::DependsOn => (
9183 DependencyEndpoint::from_native(id.clone(), near_kind),
9184 DependencyEndpoint::from_native(related, related_kind),
9185 ),
9186 Direction::DependedOnBy => (
9187 DependencyEndpoint::from_native(related, related_kind),
9188 DependencyEndpoint::from_native(id.clone(), near_kind),
9189 ),
9190 };
9191 Ok(DependencyEdge {
9192 from,
9193 to,
9194 kind: DependencyKind::Blocks,
9195 })
9196 })
9197 .collect::<Result<Vec<_>, SourceError>>()?;
9198 let mut next = next_cursor(connection)?;
9199 if let Some(next) = &next {
9200 validate_cursor_progress(cursor, &next.0)?;
9201 }
9202 // The recorded far ends ride on the last page of `blockedBy`, whose answer carried the
9203 // body they are recorded in, as far as the page has room — so they cost no request of
9204 // their own — and only what does not fit is resumed by a cursor.
9205 let mut items = items;
9206 if next.is_none() {
9207 let recorded = self
9208 .recorded_edges(
9209 id,
9210 near_kind,
9211 direction,
9212 natively_names,
9213 node,
9214 draft_slot.as_ref(),
9215 )
9216 .await?;
9217 let room = limit.saturating_sub(items.len());
9218 let total = recorded.len();
9219 items.extend(recorded.into_iter().take(room));
9220 if total > room {
9221 next = Some(Cursor(format!("{RECORDED_CURSOR}{room}")));
9222 }
9223 }
9224 Ok(Page { items, next })
9225 }
9226
9227 /// The edges this item records under [`DependencyEdge::RECORDED_KEY`], which is where
9228 /// a far end in another source has to live: no GitHub issue relationship can name one.
9229 ///
9230 /// Only forwards. The reverse of a recorded edge is derived from the far end, and this
9231 /// source never writes one down.
9232 ///
9233 /// The metadata lives in the item's own body slot, and `node` is the dependency read's
9234 /// own answer, which carries an issue's body — so an issue's recorded edges cost no
9235 /// request beyond the read already made, and reading the board for them would be a
9236 /// walk of every item for one field of one. A draft has no body in that answer, because
9237 /// a draft is not an issue, so a draft's are read off its own read by id — never off a
9238 /// listing of the board, which can be behind on the very item asked about.
9239 async fn recorded_edges(
9240 &self,
9241 id: &NativeId,
9242 near_kind: ItemKind,
9243 direction: Direction,
9244 natively_names: Option<ItemKind>,
9245 node: &Value,
9246 draft_slot: Option<&Option<BTreeMap<String, Value>>>,
9247 ) -> Result<Vec<DependencyEdge>, SourceError> {
9248 if direction != Direction::DependsOn {
9249 return Ok(Vec::new());
9250 }
9251 let slot = match (node.get("body"), draft_slot) {
9252 (Some(body), _) if natively_names.is_some() => {
9253 metadata_body(body.as_str().map(str::to_owned))?.1
9254 }
9255 // A draft the dependency read itself answered: this board's, or none of its.
9256 (_, Some(Some(slot))) => slot.clone(),
9257 (_, Some(None)) => return Ok(Vec::new()),
9258 _ => {
9259 let Some(item) = self.bound_item(id).await? else {
9260 return Ok(Vec::new());
9261 };
9262 item.slot
9263 }
9264 };
9265 DependencyEdge::recorded(&slot, id, near_kind, &self.name, natively_names)
9266 .map_err(|message| SourceError::Malformed { message })
9267 }
9268
9269 /// Why `creation_owners` refuses a create filed in `target`, or `None` when it allows it —
9270 /// every create when the list is empty.
9271 fn owner_refusal(&self, target: &RepositoryTarget) -> Option<String> {
9272 if self.creation_owners.is_empty()
9273 || self
9274 .creation_owners
9275 .iter()
9276 .any(|owner| owner.eq_ignore_ascii_case(&target.owner))
9277 {
9278 return None;
9279 }
9280 Some(format!(
9281 "source {} would create the issue in repository {}, owned by {}, and its \
9282 creation_owners allows only repositories of {}",
9283 self.name,
9284 target.slug(),
9285 target.owner,
9286 self.creation_owners.join(", ")
9287 ))
9288 }
9289
9290 fn configured_repository(&self) -> Result<&RepositoryTarget, SourceError> {
9291 self.repository
9292 .as_ref()
9293 .ok_or_else(|| SourceError::Refused {
9294 message: format!(
9295 "source {} has no repository configured, and a GitHub Projects board has no \
9296 repository of its own to create an issue in; set repository: owner/name on \
9297 this source",
9298 self.name
9299 ),
9300 })
9301 }
9302
9303 /// The repository one new issue is created in, under the rule [`RepositoryTarget`]
9304 /// states.
9305 ///
9306 /// The fallback is demanded first, whichever arm answers: a write without a configured
9307 /// repository is refused naming the field exactly as it was before the rule existed,
9308 /// so a source that could not write before cannot write now, rather than writing for
9309 /// the one item whose own field happens to decide it.
9310 ///
9311 /// Everything this refuses is refused before `createIssue`, so a refusal leaves no
9312 /// issue behind: an entry that is not a repository on [`RepositoryTarget::HOST`], an
9313 /// entry owned by someone other than the owner of the parent issue's repository —
9314 /// GitHub accepts a sub-issue from another repository of the same owner and from no
9315 /// other, so `addSubIssue` would refuse it after the issue existed — a parent the
9316 /// board does not hold, and a parent that is a draft, which GitHub gives no sub-issues,
9317 /// both of which `addSubIssue` would likewise refuse too late. Whether the entry exists
9318 /// and is visible to the token is checked where its node id is resolved, still before
9319 /// `createIssue`. The parent is read by its own id through [`Self::item_by_id`] — never
9320 /// looked up in a listing of the board, which can be minutes behind an issue its own
9321 /// `projectItems` already places on it — and that read answers first from this process's
9322 /// own record, so a project created moments ago in this command answers though GitHub
9323 /// has not caught up.
9324 async fn creation_target(
9325 &self,
9326 incoming: &Incoming<'_>,
9327 ) -> Result<RepositoryTarget, SourceError> {
9328 let what = format!(
9329 "{} {:?}",
9330 incoming.written.kind().describes(),
9331 incoming.title
9332 );
9333 let target = self
9334 .creation_rule(incoming.repositories, incoming.parent, &what)
9335 .await?;
9336 // The engine asks `creation_refusal` before anything of a copy is written; this holds
9337 // every other create to the same restriction, before `createIssue`.
9338 match self.owner_refusal(&target) {
9339 Some(message) => Err(SourceError::Refused { message }),
9340 None => Ok(target),
9341 }
9342 }
9343
9344 /// [`Self::creation_target`]'s rule over what it reads of the item — its `repositories`
9345 /// and the project issue it is filed under — with `what` naming the item in a refusal: the
9346 /// one place the rule is, so the create, [`TaskSource::creation_repository`] and the
9347 /// write-time read of who can read the issue ([`TaskSource::visibility`]) cannot answer
9348 /// differently.
9349 ///
9350 /// On a source declared private, an item's one named repository is read for its
9351 /// visibility first — once per engine hold, see the `visibility` module — and one that
9352 /// does not read private decides nothing: the issue is created in the parent's
9353 /// repository, else the configured one. A read that cannot be answered refuses.
9354 async fn creation_rule(
9355 &self,
9356 repositories: &[Repository],
9357 parent: Option<&NativeId>,
9358 what: &str,
9359 ) -> Result<RepositoryTarget, SourceError> {
9360 let parent = match parent {
9361 Some(parent) => Some(self.bound_item(parent).await?.ok_or_else(|| {
9362 SourceError::Refused {
9363 message: format!(
9364 "GitHub project issue {} was not found on the board of source {}, so {} \
9365 cannot be filed under it",
9366 parent.0, self.name, what
9367 ),
9368 }
9369 })?),
9370 None => None,
9371 };
9372 let parents_repository = parent
9373 .as_ref()
9374 .map(|parent| {
9375 // A draft is on the board and so is found, but it has no repository to
9376 // place a task in and GitHub gives it no sub-issues, so `addSubIssue`
9377 // would refuse the task only once `createIssue` had made it.
9378 if parent.content_kind == ContentKind::DraftIssue {
9379 return Err(SourceError::Refused {
9380 message: format!(
9381 "GitHub project item {} on the board of source {} is a draft, \
9382 which cannot have sub-issues, so {} cannot be filed under it",
9383 parent.id.0, self.name, what
9384 ),
9385 });
9386 }
9387 // An issue's repository is where a sub-issue is placed and whose owner it
9388 // is compared against, so a parent whose repository this source cannot
9389 // spell as `owner/name` — GitHub's login grammar is wider than this
9390 // source's floor — is one nothing can be filed under.
9391 parent
9392 .own_repository
9393 .as_ref()
9394 .and_then(|origin| RepositoryTarget::from_origin(origin).ok())
9395 .ok_or_else(|| SourceError::Malformed {
9396 message: format!(
9397 "GitHub project issue {} on the board of source {} is in {}, which \
9398 is not a {}/owner/name repository this source can place {} in",
9399 parent.id.0,
9400 self.name,
9401 parent
9402 .own_repository
9403 .as_ref()
9404 .map_or("no repository", Repository::as_str),
9405 RepositoryTarget::HOST,
9406 what
9407 ),
9408 })
9409 })
9410 .transpose()?;
9411 self.creation_rule_in_repository(repositories, parents_repository, what)
9412 .await
9413 }
9414
9415 async fn creation_rule_in_repository(
9416 &self,
9417 repositories: &[Repository],
9418 parents_repository: Option<RepositoryTarget>,
9419 what: &str,
9420 ) -> Result<RepositoryTarget, SourceError> {
9421 let fallback = self.configured_repository()?;
9422 match repositories {
9423 [named] => {
9424 let target =
9425 RepositoryTarget::from_origin(named).map_err(|_| SourceError::Refused {
9426 message: format!(
9427 "{} names repository {}, which is not a {}/owner/name repository \
9428 source {} can create an issue in; name one that is, or name none",
9429 what,
9430 named.as_str(),
9431 RepositoryTarget::HOST,
9432 self.name
9433 ),
9434 })?;
9435 // On a source declared private, an issue whose one named repository does not
9436 // read private is created where an item naming none is: the item still names
9437 // that repository in its metadata slot, and its issue stays private.
9438 if self.is_declared_private()
9439 && self.repository_visibility(&target).await?
9440 != onetaskgraph_plugin_api::Visibility::Private
9441 {
9442 return Ok(parents_repository.unwrap_or_else(|| fallback.clone()));
9443 }
9444 if let Some(parents) = &parents_repository
9445 && parents.owner != target.owner
9446 {
9447 return Err(SourceError::Refused {
9448 message: format!(
9449 "{} names repository {}, owned by {}, but its project's issue is in \
9450 {}, owned by {}, and GitHub files a sub-issue only in a repository \
9451 of the same owner as its parent issue; name a repository of {}, or \
9452 name none",
9453 what,
9454 target.slug(),
9455 target.owner,
9456 parents.slug(),
9457 parents.owner,
9458 parents.owner
9459 ),
9460 });
9461 }
9462 Ok(target)
9463 }
9464 _ => Ok(parents_repository.unwrap_or_else(|| fallback.clone())),
9465 }
9466 }
9467
9468 /// The node id of the repository `incoming` is being created in, or the refusal naming
9469 /// the item and the repository the token cannot see.
9470 ///
9471 /// Resolved once per command per repository; see [`Self::repository_cache`].
9472 async fn repository_id(
9473 &self,
9474 repository: &RepositoryTarget,
9475 incoming: &Incoming<'_>,
9476 ) -> Result<String, SourceError> {
9477 if let Some(id) = self.repository_cache()?.get(repository).cloned() {
9478 return Ok(id);
9479 }
9480 let data = self
9481 .graphql(
9482 graphql::REPOSITORY,
9483 json!({"owner":repository.owner,"name":repository.name}),
9484 )
9485 .await?;
9486 self.repository_read(&data, repository, incoming)
9487 }
9488
9489 /// The repository's node id out of an answer carrying the `repository` root, held for
9490 /// the rest of this command, or the refusal naming the item that cannot be created in it.
9491 fn repository_read(
9492 &self,
9493 data: &Value,
9494 repository: &RepositoryTarget,
9495 incoming: &Incoming<'_>,
9496 ) -> Result<String, SourceError> {
9497 let node = data
9498 .get("repository")
9499 .filter(|value| !value.is_null())
9500 .ok_or_else(|| SourceError::Refused {
9501 message: format!(
9502 "GitHub repository {} was not found or is not visible to the token, so {} \
9503 {:?} cannot be created in it",
9504 repository.slug(),
9505 incoming.written.kind().describes(),
9506 incoming.title
9507 ),
9508 })?;
9509 let id = required_str(node, "id")?.to_owned();
9510 self.repository_cache()?
9511 .insert(repository.clone(), id.clone());
9512 Ok(id)
9513 }
9514
9515 fn repository_cache(
9516 &self,
9517 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<RepositoryTarget, String>>, SourceError> {
9518 self.repository_cache
9519 .lock()
9520 .map_err(|_| SourceError::Unavailable {
9521 message: "this source's record of the destination repository was left \
9522 inconsistent by an earlier failure; next: run the command again"
9523 .into(),
9524 })
9525 }
9526
9527 /// Create or update one board item, whichever kind it is.
9528 async fn write_item(
9529 &self,
9530 incoming: &Incoming<'_>,
9531 target: Option<&NativeId>,
9532 depends_on: &[DependencyEdge],
9533 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
9534 let prepared = self.prepare_write(incoming, target, depends_on).await?;
9535 let landed = match &prepared.destination {
9536 // Its content is written last, by `complete_write`, once everything else has landed.
9537 WriteDestination::Existing(item) => Landed {
9538 content_id: item.id.clone(),
9539 item_id: Some(item.item_id.clone()),
9540 url: item.url.clone(),
9541 number: item.number,
9542 },
9543 WriteDestination::New(target) => {
9544 if self.deferring()? {
9545 self.create_filed_issue(
9546 prepared.board.id.as_str(),
9547 target,
9548 incoming,
9549 &prepared.body,
9550 )
9551 .await?
9552 } else {
9553 self.create_and_file_issue(
9554 prepared.board.id.as_str(),
9555 target,
9556 incoming,
9557 &prepared.body,
9558 )
9559 .await?
9560 }
9561 }
9562 };
9563 self.complete_write(incoming, prepared, landed).await
9564 }
9565
9566 /// Everything one write of `incoming` reads and decides before it sends anything: what is
9567 /// there now, where a new item is created, the board's fields, and every value refused while
9568 /// nothing has been written — the item's body included.
9569 async fn prepare_write(
9570 &self,
9571 incoming: &Incoming<'_>,
9572 target: Option<&NativeId>,
9573 depends_on: &[DependencyEdge],
9574 ) -> Result<Prepared, SourceError> {
9575 // Refused before anything is read or written: a task or a project titled the way
9576 // this board spells a document would land as an issue this same source reads back
9577 // as a document, so the field this destination cannot carry is named rather than
9578 // written and silently reclassified.
9579 if let Written::Work(kind, _) = incoming.written
9580 && incoming.title.starts_with(DESIGN_TITLE_PREFIX)
9581 {
9582 return Err(SourceError::Refused {
9583 message: format!(
9584 "the title of this {} begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
9585 spells a document, so it would read back as one rather than as a {}; \
9586 retitle it, or copy it as a document",
9587 kind.marker(),
9588 self.name,
9589 kind.marker()
9590 ),
9591 });
9592 }
9593 // The destination is read by its own id, and whether this board holds it is decided
9594 // by that read — its own `projectItems` — rather than by whether a listing of the
9595 // board happens to include it yet. See the module documentation.
9596 // An existing issue is never moved; a new one is created where the rule says — and
9597 // knowing where is what lets the board's fields and that repository's id be read
9598 // together, before anything below needs either.
9599 let destination = match target {
9600 Some(target) => {
9601 WriteDestination::Existing(Box::new(self.bound_item(target).await?.ok_or_else(
9602 || SourceError::Refused {
9603 message: format!("GitHub destination item {} was not found", target.0),
9604 },
9605 )?))
9606 }
9607 None => {
9608 let target = self.creation_target(incoming).await?;
9609 self.creation_context(&target, incoming).await?;
9610 WriteDestination::New(target)
9611 }
9612 };
9613 let existing = destination.existing();
9614 let board = self
9615 .fields_for(
9616 existing,
9617 incoming.metadata.contains_key(ORIGIN_KEY),
9618 incoming.written.status().is_some(),
9619 incoming
9620 .priority
9621 .is_some_and(|priority| priority != Priority::None),
9622 )
9623 .await?;
9624 let status_target = incoming
9625 .written
9626 .work_status()
9627 .map(|(kind, status)| self.resolved_target(kind, status.category))
9628 .transpose()?;
9629 let column = match (incoming.written.work_status(), status_target.as_ref()) {
9630 (Some((kind, status)), Some(target)) => {
9631 self.column_for(&board.fields, kind, status.category, target)?
9632 }
9633 _ => None,
9634 };
9635 // Resolved before anything is created, for the reason the column above is: a
9636 // priority this board has no option for is refused while nothing has been written.
9637 let priority_write = match incoming.priority {
9638 Some(priority) => self.priority_write(&board.fields, existing, priority)?,
9639 None => None,
9640 };
9641 // And for the reason the priority is: a value no projected field can hold, or a board
9642 // that cannot hold the field, is refused while nothing has been written.
9643 let (projections, projected) = self.projection_writes(
9644 &board.fields,
9645 existing.map(|item| &item.projected),
9646 incoming.metadata,
9647 None,
9648 )?;
9649 let content_kind = existing.map_or(ContentKind::Issue, |item| item.content_kind);
9650 if content_kind == ContentKind::DraftIssue {
9651 if let (Some(StatusTarget::Terminal(_, _)), Some(status)) =
9652 (status_target.as_ref(), incoming.written.status())
9653 {
9654 return Err(self.closes_a_draft(status.category));
9655 }
9656 if incoming.parent.is_some() {
9657 return Err(SourceError::Refused {
9658 message: "GitHub draft items cannot be a project's sub-issue".into(),
9659 });
9660 }
9661 }
9662 match existing {
9663 Some(item) if content_kind == ContentKind::Issue => {
9664 if item.labels != incoming.labels {
9665 return Err(SourceError::Refused {
9666 message: "GitHub issue labels differ from the labels being written".into(),
9667 });
9668 }
9669 }
9670 _ => {
9671 if !incoming.labels.is_empty() {
9672 return Err(SourceError::Refused {
9673 message: "GitHub items created by this destination carry no labels".into(),
9674 });
9675 }
9676 }
9677 }
9678
9679 // The repository the issue really lives in is what the slot below is written against,
9680 // so a single entry that is where the issue is created travels as no key at all, and
9681 // the read side derives it back from the issue.
9682 let own_repository = match &destination {
9683 WriteDestination::Existing(item) => item.own_repository.clone(),
9684 WriteDestination::New(target) => Some(
9685 Repository::try_from(target.origin())
9686 .map_err(|message| SourceError::Config { message })?,
9687 ),
9688 };
9689 let (native, fallback) = self
9690 .partition_edges(
9691 incoming.written.kind(),
9692 content_kind,
9693 existing.and_then(|item| item.blocked_by.as_deref()),
9694 depends_on,
9695 )
9696 .await?;
9697 let mut slot = slot_metadata(incoming, own_repository.as_ref(), &fallback);
9698 let content = match incoming.assets {
9699 Some(assets) => {
9700 let uploads = self.upload_assets(own_repository.as_ref(), assets).await?;
9701 let rewritten = onetaskgraph_plugin_api::serve_asset_references(
9702 incoming.content.unwrap_or_default(),
9703 &mut slot,
9704 &uploads,
9705 );
9706 incoming.content.map(|_| rewritten)
9707 }
9708 None => incoming.content.map(str::to_owned),
9709 };
9710 let marked = content.as_deref().map(|content| self.marked(content));
9711 let body = compose_body(marked.as_deref(), &slot)?;
9712 // Read before anything is created, for the reason the field below is: a value
9713 // this destination cannot store has to refuse, and refusing after `createIssue`
9714 // would leave an issue behind that nothing asked for. The engine writes a
9715 // qualified id here; a caller handing this key anything else is told so rather
9716 // than having it silently stored as no origin at all.
9717 // 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.
9718 let origin = match incoming.metadata.get(ORIGIN_KEY) {
9719 None => "",
9720 Some(Value::String(origin)) => origin.as_str(),
9721 Some(other) => {
9722 return Err(SourceError::Refused {
9723 message: format!(
9724 "{ORIGIN_KEY} holds a qualified id spelled as a string, and this item's \
9725 is {other}"
9726 ),
9727 });
9728 }
9729 };
9730 // Resolved before anything is created: a board that cannot carry the copy origin
9731 // has to refuse the write, and refusing it after `createIssue` would leave an
9732 // issue behind that nothing asked for.
9733 let origin_field = match Board::field(&board.fields, ORIGIN_FIELD)? {
9734 Some(field) => {
9735 if required_str(field, "__typename")? != "ProjectV2Field" {
9736 return Err(SourceError::Refused {
9737 message: format!(
9738 "GitHub board source-owned {ORIGIN_FIELD} field is not a text field"
9739 ),
9740 });
9741 }
9742 Some(required_str(field, "id")?.to_owned())
9743 }
9744 None if incoming.metadata.contains_key(ORIGIN_KEY) => {
9745 return Err(SourceError::Refused {
9746 message: format!(
9747 "GitHub board has no source-owned {ORIGIN_FIELD} text field, and the \
9748 item carries {ORIGIN_KEY}; add a text field named {ORIGIN_FIELD} to \
9749 the board"
9750 ),
9751 });
9752 }
9753 None => None,
9754 };
9755
9756 Ok(Prepared {
9757 origin: origin.to_owned(),
9758 destination,
9759 board,
9760 status_target,
9761 column,
9762 priority_write,
9763 projections,
9764 projected,
9765 content_kind,
9766 own_repository,
9767 native,
9768 slot,
9769 content,
9770 body,
9771 origin_field,
9772 })
9773 }
9774
9775 /// Everything one write does once the item it writes exists: its board fields, its
9776 /// relationships and — for an item that was already there — its content last, then what this
9777 /// process remembers of it.
9778 ///
9779 /// While this source is holding writes back — see [`TaskSource::begin_deferred_writes`] — an
9780 /// item this command created has its fields, its closing and its edges held for
9781 /// [`TaskSource::finish_writes`] rather than sent here.
9782 async fn complete_write(
9783 &self,
9784 incoming: &Incoming<'_>,
9785 prepared: Prepared,
9786 landed: Landed,
9787 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
9788 let Prepared {
9789 destination,
9790 board,
9791 status_target,
9792 column,
9793 priority_write,
9794 projections,
9795 projected,
9796 content_kind,
9797 own_repository,
9798 native,
9799 slot,
9800 content,
9801 body,
9802 origin,
9803 origin_field,
9804 } = prepared;
9805 let existing = destination.existing();
9806 let origin = origin.as_str();
9807 let Landed {
9808 content_id,
9809 item_id,
9810 url,
9811 number,
9812 } = landed;
9813
9814 // Only what differs from the item as this write read it is sent: a priority the item
9815 // already holds is not, so an unchanged re-copy writes no field and a person's edit of
9816 // it since that read is left alone.
9817 let priority_write = priority_write.filter(|_| {
9818 !existing.is_some_and(|item| {
9819 incoming.priority.map(HeldPriority::Read) == Some(item.priority.clone())
9820 })
9821 });
9822 let written_option = column.as_ref().map(|(_, _, name)| name.clone());
9823 let column = column
9824 .filter(|(_, _, name)| existing.is_none_or(|item| item.option.as_ref() != Some(name)))
9825 .map(|(field, option, _)| (field, option));
9826 // Creating an item here is several calls — `createIssue`, which files it on the
9827 // board, then its board fields, the parent and the dependencies — and GitHub can fail
9828 // at any of them. Everything this source can refuse *before* the first of those is
9829 // already checked above, so what is left is GitHub itself failing part way. When it
9830 // does over an item this call created, the issue is taken back: a write that
9831 // refused must not leave an item behind that nobody asked for, and one that does
9832 // makes the retry create a second.
9833 // Whether the board-field write carrying a moved origin was answered as landing whole.
9834 // When it was refused, GitHub does not say which of its fields ran before the one that
9835 // failed, so the origin may or may not have moved.
9836 let mut origin_landed = false;
9837 // An item this command created while writes are held back has every part that can land
9838 // later held for `finish_writes` — its own create filed it on the board and under its
9839 // project already — and so does a later write of it in the same command.
9840 let holding = (existing.is_none() && self.deferring()?) || self.holds_back(&content_id)?;
9841 let landed = if holding {
9842 self.hold_back(
9843 &board.id,
9844 &content_id,
9845 existing,
9846 origin_field.as_deref(),
9847 origin,
9848 column,
9849 status_target.as_ref(),
9850 priority_write.as_ref(),
9851 &projections,
9852 &native,
9853 )
9854 } else if let Some(item_id) = item_id.as_deref() {
9855 self.finish_write(
9856 board.id.as_str(),
9857 incoming,
9858 &content_id,
9859 item_id,
9860 content_kind,
9861 existing,
9862 origin_field.as_deref(),
9863 origin,
9864 column,
9865 status_target.as_ref(),
9866 priority_write.as_ref(),
9867 &projections,
9868 &native,
9869 &mut origin_landed,
9870 )
9871 .await
9872 } else {
9873 // An issue a create filed on the board itself has no board item this process
9874 // knows until `finish_writes` reads it, so nothing but a held-back write may land
9875 // on it before then.
9876 Err(SourceError::Malformed {
9877 message: format!(
9878 "issue {} was written before the board item GitHub filed it as was read",
9879 content_id.0
9880 ),
9881 })
9882 };
9883 // An existing item's title, body and state go last, in one `updateIssue`, once its board
9884 // fields and its relationships have landed: a refusal of any of those then leaves its
9885 // body — and the metadata slot inside it — exactly as it stood.
9886 let landed = match (landed, existing) {
9887 (Ok(()), Some(item)) => {
9888 self.update_existing(item, incoming, &body, status_target.as_ref())
9889 .await
9890 }
9891 (landed, _) => landed,
9892 };
9893 if let Err(error) = landed {
9894 match existing {
9895 // Best effort, and the write's own failure is what the caller is told: a
9896 // refusal naming the tidy-up would hide why the write failed at all.
9897 None => {
9898 let _ = self.delete_issue(&content_id).await;
9899 }
9900 // The origin field is the one piece of an existing item's metadata written
9901 // before its body, so a write refused after it puts it back as it was. When
9902 // that is refused too, the write's own failure is still what the caller is
9903 // told — with what it left behind added, because the item's metadata is then
9904 // not as it stood and a caller retrying has to know which key moved.
9905 Some(item) => {
9906 let before = item.origin.as_deref().unwrap_or("");
9907 if let Some(field) = origin_field.as_deref()
9908 && before != origin
9909 && let Err(restore) = self
9910 .set_item_field(
9911 board.id.as_str(),
9912 &item.item_id,
9913 field,
9914 json!({"text": before}),
9915 )
9916 .await
9917 {
9918 let left = if origin_landed {
9919 format!(
9920 "its {ORIGIN_KEY} was moved to {origin:?} before that and could \
9921 not be put back to {before:?} ({restore}), so item {} still \
9922 holds {origin:?} there",
9923 item.id.0
9924 )
9925 } else {
9926 format!(
9927 "the refused write carried its {ORIGIN_KEY} from {before:?} to \
9928 {origin:?}, GitHub does not say whether that part of it ran, \
9929 and putting it back to {before:?} was refused ({restore}), so \
9930 item {} holds {origin:?} or {before:?} there",
9931 item.id.0
9932 )
9933 };
9934 return Err(noting(
9935 error,
9936 &format!(
9937 "; {left}; next: set {ORIGIN_KEY} on it back to {before:?}, or \
9938 run the write again"
9939 ),
9940 ));
9941 }
9942 }
9943 }
9944 return Err(error);
9945 }
9946
9947 let written_status = match (incoming.written.work_status(), status_target.as_ref()) {
9948 (Some((kind, _)), Some(StatusTarget::Terminal(_, reason))) => {
9949 self.statuses
9950 .status(kind, written_option.as_deref(), true, Some(reason.reason()))
9951 }
9952 (Some((kind, _)), Some(StatusTarget::Column(_))) => {
9953 self.statuses
9954 .status(kind, written_option.as_deref(), false, None)
9955 }
9956 (Some((_, status)), _) => status.clone(),
9957 (None, _) => Status {
9958 category: StatusCategory::Unknown,
9959 name: "Open".to_owned(),
9960 },
9961 };
9962
9963 // So the rest of this command reads what it just did rather than what the board
9964 // said before it. See `remember_written` for which half takes it.
9965 let remembered = Resolved {
9966 // Empty for an issue whose board item is not read yet: `finish_writes` reads it
9967 // and `learn_item` puts it here.
9968 // 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.
9969 item_id: item_id.unwrap_or_default(),
9970 id: content_id.clone(),
9971 content_kind,
9972 kind: incoming.written.kind(),
9973 title: incoming.title.to_owned(),
9974 // The visible half of the body this write composed, split back off it the
9975 // way a read splits it — so what this record reports is what a read of the
9976 // same issue reports, rather than the person's text with the metadata slot
9977 // still on the end of it.
9978 body: self.visible(body.clone())?.0,
9979 raw_body: body.clone(),
9980 // A document has no status of its own; what it reads back as is whatever
9981 // the issue's own state says, which is what a re-read reports.
9982 status: written_status,
9983 option: written_option.or_else(|| existing.and_then(|item| item.option.clone())),
9984 priority: match incoming.priority {
9985 Some(priority) => HeldPriority::Read(priority),
9986 None => existing.map_or(HeldPriority::Read(Priority::None), |item| {
9987 item.priority.clone()
9988 }),
9989 },
9990 // What `state_input` asked for: closed for a terminal target, open for any other
9991 // status, and the issue's own state left as it was by a document write.
9992 closed: content_kind == ContentKind::Issue
9993 && match status_target.as_ref() {
9994 Some(StatusTarget::Terminal(_, _)) => true,
9995 Some(_) => false,
9996 None => existing.is_some_and(|item| item.closed),
9997 },
9998 reason: match status_target.as_ref() {
9999 Some(StatusTarget::Terminal(_, reason)) if content_kind == ContentKind::Issue => {
10000 Some(reason.reason().to_owned())
10001 }
10002 Some(_) => None,
10003 None => existing.and_then(|item| item.reason.clone()),
10004 },
10005 delivers: incoming.delivers.to_vec(),
10006 delivered_by: incoming.delivered_by.to_vec(),
10007 labels: incoming.labels.to_vec(),
10008 parent: incoming.parent.cloned(),
10009 origin: (!origin.is_empty()).then(|| origin.to_owned()),
10010 projected,
10011 number,
10012 // In the update path this is the item's own url, read off `existing` where the
10013 // record above was bound, so one expression serves both halves.
10014 url,
10015 created_at: existing.and_then(|item| item.created_at),
10016 updated_at: existing.and_then(|item| item.updated_at),
10017 // What this write moved is GitHub's to say, so a record of it gives no token until
10018 // it is read again.
10019 item_updated_at: None,
10020 comment_count: None,
10021 relationship_events: None,
10022 served_at: None,
10023 facets: None,
10024 own_repository,
10025 repositories: incoming.repositories.to_vec(),
10026 classification: incoming.classification,
10027 slot,
10028 board_id: Some(board.id.as_str().to_owned()),
10029 fields: board
10030 .fields
10031 .get("nodes")
10032 .and_then(Value::as_array)
10033 .cloned()
10034 .unwrap_or_default(),
10035 board_fields: Some(board.fields.clone()),
10036 // What this write left the relationship holding is known by id alone, and a
10037 // later read of its edges needs each far end's kind, so it reads them again.
10038 blocked_by: None,
10039 };
10040 self.remember_written(remembered, existing.is_none())?;
10041 Ok(onetaskgraph_plugin_api::AssetsWritten {
10042 id: content_id,
10043 content,
10044 })
10045 }
10046
10047 /// Everything a write does after the item exists: its board fields, its parent, and
10048 /// its dependencies.
10049 ///
10050 /// Split out of `write_item` so there is one place a failure past the point of no
10051 /// return is caught, rather than a tidy-up repeated at each `?` above.
10052 // llmlint: ignore[suppressions_justified] This is the tail of `write_item` lifted out
10053 // so there is one place a failure past the point of no return is caught, and its
10054 // arguments are exactly the values that tail already had in scope. Bundling them into a
10055 // struct would describe no concept — it would be "the arguments of this function" — and
10056 // would put the whole of `write_item`'s locals behind one more indirection.
10057 #[allow(clippy::too_many_arguments)]
10058 async fn finish_write(
10059 &self,
10060 board_id: &str,
10061 incoming: &Incoming<'_>,
10062 content_id: &NativeId,
10063 item_id: &str,
10064 content_kind: ContentKind,
10065 existing: Option<&Resolved>,
10066 origin_field: Option<&str>,
10067 origin: &str,
10068 column: Option<(String, String)>,
10069 status_target: Option<&StatusTarget>,
10070 priority: Option<&PriorityWrite>,
10071 projections: &[ProjectionWrite],
10072 native: &[String],
10073 origin_landed: &mut bool,
10074 ) -> Result<(), SourceError> {
10075 let (fields, clears) = field_writes(
10076 existing,
10077 origin_field,
10078 origin,
10079 column,
10080 priority,
10081 projections,
10082 );
10083 self.set_item_fields(board_id, item_id, &fields, &clears)
10084 .await?;
10085 *origin_landed = true;
10086
10087 // An existing issue closes in the `updateIssue` its write ends with; one created just
10088 // now closes here, once its option is selected.
10089 if existing.is_none()
10090 && content_kind == ContentKind::Issue
10091 && matches!(status_target, Some(StatusTarget::Terminal(_, _)))
10092 {
10093 self.update_content(
10094 ContentKind::Issue,
10095 content_id,
10096 json!({"stateInput":state_input(status_target)}),
10097 )
10098 .await?;
10099 }
10100
10101 if content_kind == ContentKind::Issue {
10102 self.reparent(
10103 existing.and_then(|item| item.parent.clone()),
10104 content_id,
10105 incoming.parent,
10106 )
10107 .await?;
10108 // A document takes part in no dependency graph, so writing one neither reads
10109 // nor changes the issue's own `blockedBy` relationships. Reconciling them
10110 // against the empty list a document write carries would *delete* whatever
10111 // relationships a person had made on that issue, which is a write nobody
10112 // asked for.
10113 if incoming.written.kind() != BoardKind::Document {
10114 let issue = match existing {
10115 Some(item) => Issue::Existing(item.blocked_by.as_deref()),
10116 None => Issue::Created,
10117 };
10118 self.reconcile_blocked_by(content_id, native, issue).await?;
10119 }
10120 }
10121 Ok(())
10122 }
10123
10124 /// Whether this source is holding writes back — see [`TaskSource::begin_deferred_writes`].
10125 fn deferring(&self) -> Result<bool, SourceError> {
10126 Ok(self.deferred()?.is_some())
10127 }
10128
10129 /// Whether `content` is an item this command created whose parts are held back.
10130 fn holds_back(&self, content: &NativeId) -> Result<bool, SourceError> {
10131 Ok(self
10132 .deferred()?
10133 .as_ref()
10134 .is_some_and(|deferred| deferred.holds(content)))
10135 }
10136
10137 fn deferred(&self) -> Result<std::sync::MutexGuard<'_, Option<Deferred>>, SourceError> {
10138 self.deferred.lock().map_err(|_| SourceError::Unavailable {
10139 message: "this source's record of the writes it is holding back was left \
10140 inconsistent by an earlier failure; next: run the command again"
10141 .into(),
10142 })
10143 }
10144
10145 /// Creates one issue filed on the board and under its project by the `createIssue` itself —
10146 /// `projectV2Ids` naming the board and `parentIssueId` the project — which is what a copy
10147 /// creates with: one request where adding it to the board and filing it under its project
10148 /// were two more. GitHub adds the board item after answering, so its id is not known here;
10149 /// [`TaskSource::finish_writes`] reads it.
10150 async fn create_filed_issue(
10151 &self,
10152 board_id: &str,
10153 repository: &RepositoryTarget,
10154 incoming: &Incoming<'_>,
10155 body: &Option<String>,
10156 ) -> Result<Landed, SourceError> {
10157 let repository_id = self.repository_id(repository, incoming).await?;
10158 let data = self
10159 .graphql(
10160 graphql::CREATE_ISSUE,
10161 json!({"input": filed_input(&repository_id, board_id, incoming, body)}),
10162 )
10163 .await?;
10164 let created = data
10165 .pointer("/createIssue/issue")
10166 .filter(|value| !value.is_null())
10167 .ok_or_else(|| SourceError::Malformed {
10168 message: "GitHub issue creation returned no issue".into(),
10169 })?;
10170 self.created_landed(created).await
10171 }
10172
10173 /// Keep the address `createIssue` answered for the issue `content`, for
10174 /// [`TaskSource::written_location`].
10175 fn note_written(&self, content: &NativeId, url: Option<&str>) {
10176 if let Some(url) = url {
10177 self.written
10178 .lock()
10179 .unwrap_or_else(std::sync::PoisonError::into_inner)
10180 .insert(content.clone(), url.to_owned());
10181 }
10182 }
10183
10184 /// What one `createIssue` answered, as the item it created — or, when its number cannot be
10185 /// read, the refusal, after taking the issue back.
10186 async fn created_landed(&self, created: &Value) -> Result<Landed, SourceError> {
10187 let content_id = NativeId(required_nonblank_str(created, "id")?.to_owned());
10188 let url = optional_str(created, "url")?.map(str::to_owned);
10189 let number = match created_issue_number(created) {
10190 Ok(number) => number,
10191 Err(error) => {
10192 let _ = self.delete_issue(&content_id).await;
10193 return Err(error);
10194 }
10195 };
10196 self.note_written(&content_id, url.as_deref());
10197 Ok(Landed {
10198 content_id,
10199 item_id: None,
10200 url,
10201 number,
10202 })
10203 }
10204
10205 /// Hold back everything a write of the created item `content` would send after creating it,
10206 /// for [`TaskSource::finish_writes`]: the same field values, closing and edges
10207 /// `finish_write` sends, the parent excepted, which its create already named. A later write
10208 /// of the same item replaces what it changes.
10209 // 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.
10210 #[allow(clippy::too_many_arguments)]
10211 fn hold_back(
10212 &self,
10213 board_id: &BoardId,
10214 content: &NativeId,
10215 existing: Option<&Resolved>,
10216 origin_field: Option<&str>,
10217 origin: &str,
10218 column: Option<(String, String)>,
10219 status_target: Option<&StatusTarget>,
10220 priority: Option<&PriorityWrite>,
10221 projections: &[ProjectionWrite],
10222 native: &[String],
10223 ) -> Result<(), SourceError> {
10224 let status = column.map(|(field, option)| {
10225 let name = self
10226 .fields_cache()
10227 .ok()
10228 .and_then(|fields| {
10229 fields.as_ref().and_then(|board| {
10230 option_name(&board.fields, &field, &option).map(str::to_owned)
10231 })
10232 })
10233 .unwrap_or_default();
10234 (field, option, name)
10235 });
10236 let (fields, clears) =
10237 field_writes(existing, origin_field, origin, None, priority, projections);
10238 let close = match status_target {
10239 Some(StatusTarget::Terminal(_, reason)) => Some(*reason),
10240 _ => None,
10241 };
10242 let mut deferred = self.deferred()?;
10243 let deferred = deferred.get_or_insert_with(Deferred::default);
10244 if let Deferred::Nothing = deferred {
10245 *deferred = Deferred::Held {
10246 board: board_id.clone(),
10247 created: Vec::new(),
10248 };
10249 }
10250 let Deferred::Held { created, .. } = deferred else {
10251 unreachable!("held back on the line above");
10252 };
10253 let held = match created.iter().position(|held| held.content == *content) {
10254 Some(at) => &mut created[at],
10255 None => {
10256 created.push(HeldBack {
10257 content: content.clone(),
10258 fields: Vec::new(),
10259 status: None,
10260 clears: Vec::new(),
10261 close: None,
10262 edges: Vec::new(),
10263 });
10264 created.last_mut().expect("just held")
10265 }
10266 };
10267 for (field, value) in fields {
10268 held.fields.retain(|(held, _)| *held != field);
10269 held.clears.retain(|held| *held != field);
10270 held.fields.push((field, value));
10271 }
10272 for field in clears {
10273 held.fields.retain(|(held, _)| held != field);
10274 if !held.clears.iter().any(|held| held == field) {
10275 held.clears.push(field.to_owned());
10276 }
10277 }
10278 if status.is_some() {
10279 held.status = status;
10280 }
10281 if close.is_some() {
10282 held.close = close;
10283 }
10284 // Nothing of an item created in this command has been sent but its create, so the edges
10285 // it is to have are exactly the ones its latest write names.
10286 held.edges = native.iter().map(|far| NativeId(far.clone())).collect();
10287 Ok(())
10288 }
10289
10290 /// Create every task of `writes`, together, filed on the board and under each one's
10291 /// project: [`graphql::CREATE_ISSUES`], [`MUTATION_BATCH`] to a request.
10292 ///
10293 /// Each is prepared as [`Self::write_item`] prepares one — every refusal a write owes made
10294 /// before the first request — then created, then completed with its parts held back. A
10295 /// request refused part way leaves the issues it did create, which are taken back, with the
10296 /// rest of this call's, before the refusal is answered.
10297 async fn create_together(
10298 &self,
10299 writes: &[ItemWrite<Task>],
10300 ) -> Result<Vec<NativeId>, SourceError> {
10301 let incomings: Vec<Incoming<'_>> = writes
10302 .iter()
10303 .map(|write| self.task_incoming(write, None))
10304 .collect::<Result<_, _>>()?;
10305 let mut prepared = Vec::with_capacity(writes.len());
10306 for (incoming, write) in incomings.iter().zip(writes) {
10307 if write.target.is_some() {
10308 return Err(SourceError::Malformed {
10309 message: "a write naming its target was handed to a batch of creates".into(),
10310 });
10311 }
10312 prepared.push(
10313 self.prepare_write(incoming, None, &write.depends_on)
10314 .await?,
10315 );
10316 }
10317 let mut inputs = Vec::with_capacity(prepared.len());
10318 for (incoming, prepared) in incomings.iter().zip(&prepared) {
10319 let WriteDestination::New(target) = &prepared.destination else {
10320 return Err(SourceError::Malformed {
10321 message: "an update was decided for a write handed to a batch of creates"
10322 .into(),
10323 });
10324 };
10325 let repository_id = self.repository_id(target, incoming).await?;
10326 inputs.push(filed_input(
10327 &repository_id,
10328 prepared.board.id.as_str(),
10329 incoming,
10330 &prepared.body,
10331 ));
10332 }
10333 let mut landed: Vec<Landed> = Vec::with_capacity(inputs.len());
10334 let refused = 'create: {
10335 for chunk in inputs.chunks(MUTATION_BATCH) {
10336 let mut variables = serde_json::Map::new();
10337 for slot in 0..MUTATION_BATCH {
10338 variables.insert(
10339 format!("create{slot}"),
10340 chunk.get(slot).cloned().unwrap_or_else(|| chunk[0].clone()),
10341 );
10342 variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10343 }
10344 let data = match self
10345 .graphql(graphql::CREATE_ISSUES, Value::Object(variables))
10346 .await
10347 {
10348 Ok(data) => data,
10349 Err(error) => break 'create Some(error),
10350 };
10351 let mut refusal = data.get(ANSWER_ERRORS).map(|errors| {
10352 let said: Vec<&str> = errors
10353 .as_array()
10354 .into_iter()
10355 .flatten()
10356 .filter_map(|error| error.get("message").and_then(Value::as_str))
10357 .collect();
10358 SourceError::Refused {
10359 message: if said.is_empty() {
10360 "GitHub returned GraphQL errors".to_owned()
10361 } else {
10362 said.join("; ")
10363 },
10364 }
10365 });
10366 for slot in 0..chunk.len() {
10367 match data
10368 .pointer(&format!("/create{slot}/issue"))
10369 .filter(|value| !value.is_null())
10370 {
10371 Some(created) => match self.created_landed(created).await {
10372 Ok(created) => landed.push(created),
10373 Err(error) => {
10374 refusal.get_or_insert(error);
10375 }
10376 },
10377 // An alias GitHub refused answers nothing, and the refusal says why.
10378 None => {
10379 refusal.get_or_insert(SourceError::Malformed {
10380 message: "GitHub issue creation returned no issue".into(),
10381 });
10382 }
10383 }
10384 }
10385 if let Some(error) = refusal {
10386 break 'create Some(error);
10387 }
10388 }
10389 None
10390 };
10391 if let Some(error) = refused {
10392 // None of these ids reaches the caller, so an issue GitHub will not take back is
10393 // named here or by nobody.
10394 let ids: Vec<&NativeId> = landed.iter().map(|created| &created.content_id).collect();
10395 let left = self.delete_issues(&ids).await;
10396 let left: Vec<&str> = left.iter().map(|id| id.0.as_str()).collect();
10397 return Err(left_behind(error, &left));
10398 }
10399 let created: Vec<NativeId> = landed
10400 .iter()
10401 .map(|landed| landed.content_id.clone())
10402 .collect();
10403 let mut ids = Vec::with_capacity(landed.len());
10404 for ((incoming, prepared), landed) in incomings.iter().zip(prepared).zip(landed) {
10405 match self.complete_write(incoming, prepared, landed).await {
10406 Ok(written) => ids.push(written.id),
10407 // 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.
10408 Err(error) => {
10409 let ids: Vec<&NativeId> = created.iter().collect();
10410 let left = self.delete_issues(&ids).await;
10411 let left: Vec<&str> = left.iter().map(|id| id.0.as_str()).collect();
10412 return Err(left_behind(error, &left));
10413 }
10414 }
10415 }
10416 Ok(ids)
10417 }
10418
10419 /// Hold back the update of the existing issue `issue` — `members`, what its own
10420 /// [`graphql::UPDATE_ISSUE`] would send beside its id — while writes are being held back — so the updates a copy makes one after
10421 /// another go to GitHub as one request. Whatever this source held of the issue is dropped
10422 /// as the mutation itself would drop it. Answers whether it was held: never while a write
10423 /// the store's cache expects runs, which records each mutation's own answer, and never for
10424 /// an issue an update is already held for, which that one is sent ahead of.
10425 fn hold_update(&self, issue: &NativeId, members: &Value) -> Result<bool, SourceError> {
10426 if self.expected.journaling() || self.deferred()?.is_none() {
10427 return Ok(false);
10428 }
10429 let mut held = self
10430 .held_updates
10431 .lock()
10432 .map_err(|_| SourceError::Unavailable {
10433 message: "issue updates held back were left inconsistent; run the command again"
10434 .into(),
10435 })?;
10436 if held.iter().any(|earlier| earlier.issue == *issue) {
10437 return Ok(false);
10438 }
10439 let named = |value: &str| issue.0 == value;
10440 self.resolved_cache()?
10441 .retain(|id, item| !named(&id.0) && !named(&item.item_id));
10442 self.read_ahead()?.retain(|id, item| {
10443 !named(&id.0) && !item.as_ref().is_some_and(|item| named(&item.item_id))
10444 });
10445 self.carried()?
10446 .retain(|id, item| !named(&id.0) && !named(&item.item_id));
10447 held.push(HeldUpdate::new(issue, members));
10448 Ok(true)
10449 }
10450
10451 /// Send every update [`Self::hold_update`] held back: one alone as [`graphql::UPDATE_ISSUE`]
10452 /// sends it, several as aliased `updateIssue`s of [`graphql::UPDATE_ISSUES`],
10453 /// [`MUTATION_BATCH`] to a request. Each counts one write, as it would alone.
10454 async fn send_held_updates(&self) -> Result<(), SourceError> {
10455 let held = std::mem::take(&mut *self.held_updates.lock().map_err(|_| {
10456 SourceError::Unavailable {
10457 message: "issue updates held back were left inconsistent; run the command again"
10458 .into(),
10459 }
10460 })?);
10461 if let [one] = held.as_slice() {
10462 return self
10463 .send_update(
10464 graphql::UPDATE_ISSUE,
10465 "/updateIssue/issue",
10466 &one.issue,
10467 one.input.clone(),
10468 )
10469 .await;
10470 }
10471 for chunk in held.chunks(MUTATION_BATCH) {
10472 let mut variables = serde_json::Map::new();
10473 for slot in 0..MUTATION_BATCH {
10474 variables.insert(
10475 format!("update{slot}"),
10476 chunk.get(slot).unwrap_or(&chunk[0]).input.clone(),
10477 );
10478 variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10479 }
10480 let data = self
10481 .graphql(graphql::UPDATE_ISSUES, Value::Object(variables))
10482 .await?;
10483 let inputs: Vec<&Value> = chunk.iter().map(|held| &held.input).collect();
10484 check_aliased_answers(
10485 graphql::UPDATE_ISSUES,
10486 &data,
10487 "update",
10488 &inputs,
10489 &[("issue", "id")],
10490 )?;
10491 }
10492 Ok(())
10493 }
10494
10495 /// Send everything held back since [`TaskSource::begin_deferred_writes`]: one read of the
10496 /// created items' board items, then their field values and edges together in requests of
10497 /// [`WRITE_BATCH`], then their closes in requests of [`MUTATION_BATCH`]. Anything refused
10498 /// takes back every issue held, [`DELETE_BATCH`] to a request, and answers the refusal.
10499 async fn send_held_back(&self) -> Result<(), SourceError> {
10500 let Some(Deferred::Held { board, created }) = self.deferred()?.take() else {
10501 return Ok(());
10502 };
10503 if created.is_empty() {
10504 return Ok(());
10505 }
10506 let sent = self.send_deferred(board.as_str(), &created).await;
10507 if let Err(error) = sent {
10508 // Best effort, and the failure is what the caller is told, as a write of one item
10509 // that fails part way takes back what it created: the copy that held these back
10510 // journalled every one, so one this cannot take back its undo tries again, and says
10511 // so when it cannot either.
10512 let ids: Vec<&NativeId> = created.iter().map(|held| &held.content).collect();
10513 let _ = self.delete_issues(&ids).await;
10514 return Err(error);
10515 }
10516 Ok(())
10517 }
10518
10519 /// The requests [`Self::send_held_back`] makes.
10520 async fn send_deferred(&self, board: &str, created: &[HeldBack]) -> Result<(), SourceError> {
10521 let items = self.created_board_items(board, created).await?;
10522 let mut fields: Vec<Value> = Vec::new();
10523 let mut closes: Vec<Value> = Vec::new();
10524 let mut edges: Vec<Value> = Vec::new();
10525 for held in created {
10526 let (item, status) = &items[&held.content];
10527 let mut values = held.fields.clone();
10528 // A status the board already shows — set by a workflow a person turned on to file
10529 // new items at it — is not written again.
10530 if let Some((field, option, name)) = &held.status
10531 && status.as_deref() != Some(name.as_str())
10532 {
10533 values.insert(0, (field.clone(), json!({"singleSelectOptionId": option})));
10534 }
10535 for (field, value) in values {
10536 fields.push(json!({"projectId":board,"itemId":item,"fieldId":field,"value":value}));
10537 }
10538 if let Some(reason) = held.close {
10539 closes.push(json!({"id": held.content.0, "stateInput": closed_input(reason)}));
10540 }
10541 for far in &held.edges {
10542 edges.push(json!({"issueId": held.content.0, "blockingIssueId": far.0}));
10543 }
10544 if !held.clears.is_empty() {
10545 let clears: Vec<&str> = held.clears.iter().map(String::as_str).collect();
10546 self.set_item_fields(board, item, &[], &clears).await?;
10547 }
10548 }
10549 // Each alias answers with what it wrote, which is what its input named: the board item a
10550 // field value is set on, the issue closed, the issue blocked and the one blocking it.
10551 let field_answer: &[(&str, &str)] = &[("projectV2Item", "itemId")];
10552 let close_answer: &[(&str, &str)] = &[("issue", "id")];
10553 let block_answer: &[(&str, &str)] =
10554 &[("issue", "issueId"), ("blockingIssue", "blockingIssueId")];
10555 // Every field value, then every edge, as one ordered list sent `WRITE_BATCH` aliases to a
10556 // request: a request holding both kinds is the one document carrying both, so a plan's
10557 // values and edges cost one request where they fit in one.
10558 let writes: Vec<HeldWrite<'_>> = fields
10559 .iter()
10560 .map(HeldWrite::Field)
10561 .chain(edges.iter().map(HeldWrite::Edge))
10562 .collect();
10563 for chunk in writes.chunks(WRITE_BATCH) {
10564 let values: Vec<&Value> = chunk
10565 .iter()
10566 .filter_map(|write| match write {
10567 HeldWrite::Field(input) => Some(*input),
10568 HeldWrite::Edge(_) => None,
10569 })
10570 .collect();
10571 let blocks: Vec<&Value> = chunk
10572 .iter()
10573 .filter_map(|write| match write {
10574 HeldWrite::Edge(input) => Some(*input),
10575 HeldWrite::Field(_) => None,
10576 })
10577 .collect();
10578 let (document, sends) = match (values.is_empty(), blocks.is_empty()) {
10579 (false, false) => (graphql::WRITE_FIELDS_AND_EDGES, ["sendField", "sendBlock"]),
10580 (false, true) => (graphql::WRITE_FIELD_VALUES, ["send", "send"]),
10581 _ => (graphql::ADD_BLOCKED_BYS, ["send", "send"]),
10582 };
10583 let mut variables = serde_json::Map::new();
10584 let kinds = [
10585 ("field", &values, sends[0], field_answer),
10586 ("block", &blocks, sends[1], block_answer),
10587 ];
10588 for (prefix, inputs, send, _) in &kinds {
10589 if inputs.is_empty() {
10590 continue;
10591 }
10592 for slot in 0..WRITE_BATCH {
10593 variables.insert(
10594 format!("{prefix}{slot}"),
10595 (*inputs.get(slot).unwrap_or(&inputs[0])).clone(),
10596 );
10597 variables.insert(format!("{send}{slot}"), json!(slot < inputs.len()));
10598 }
10599 }
10600 let data = self.graphql(document, Value::Object(variables)).await?;
10601 for (prefix, inputs, _, answers) in kinds {
10602 check_aliased_answers(document, &data, prefix, inputs, answers)?;
10603 }
10604 }
10605 for chunk in closes.chunks(MUTATION_BATCH) {
10606 let mut variables = serde_json::Map::new();
10607 for slot in 0..MUTATION_BATCH {
10608 variables.insert(
10609 format!("close{slot}"),
10610 chunk.get(slot).cloned().unwrap_or_else(|| chunk[0].clone()),
10611 );
10612 variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10613 }
10614 let data = self
10615 .graphql(graphql::CLOSE_ISSUES, Value::Object(variables))
10616 .await?;
10617 let inputs: Vec<&Value> = chunk.iter().collect();
10618 check_aliased_answers(graphql::CLOSE_ISSUES, &data, "close", &inputs, close_answer)?;
10619 }
10620 for held in created {
10621 self.learn_item(&held.content, &items[&held.content].0)?;
10622 }
10623 Ok(())
10624 }
10625
10626 /// The board item each of `created` is on this board, with its `Status` as the board set it:
10627 /// [`graphql::CREATED_BOARD_ITEMS`], read again — after a wait of a second, then two, four,
10628 /// eight and sixteen — for only those GitHub has not filed yet.
10629 ///
10630 /// **Membership is never assumed.** `createIssue`'s `projectV2Ids` files an issue some time
10631 /// after GitHub answered the create — the feasibility spike saw 0.45 to 1.84 seconds, and no
10632 /// create answer carrying the item — so only a read says it is there. One still absent once
10633 /// those reads are spent is filed with [`graphql::ADD_TO_BOARD`], one write each; GitHub
10634 /// filing it meanwhile refuses that "Content already exists in this project", which says it
10635 /// is placed, and one more read finds the item rather than a second one being made.
10636 async fn created_board_items(
10637 &self,
10638 board: &str,
10639 created: &[HeldBack],
10640 ) -> Result<BTreeMap<NativeId, (String, Option<String>)>, SourceError> {
10641 let mut found: BTreeMap<NativeId, (String, Option<String>)> = BTreeMap::new();
10642 let unfiled = |found: &BTreeMap<NativeId, (String, Option<String>)>| -> Vec<NativeId> {
10643 created
10644 .iter()
10645 .map(|held| held.content.clone())
10646 .filter(|id| !found.contains_key(id))
10647 .collect()
10648 };
10649 let mut wait = Duration::from_secs(1);
10650 for attempt in 0..6 {
10651 let absent = unfiled(&found);
10652 if absent.is_empty() {
10653 return Ok(found);
10654 }
10655 if attempt > 0 {
10656 self.clock
10657 .sleep_because(wait, pacing::cause::BOARD_FILING)
10658 .await;
10659 wait = wait.saturating_mul(2);
10660 }
10661 self.read_created_items(board, &absent, &mut found).await?;
10662 }
10663 let mut placed_meanwhile = Vec::new();
10664 for id in unfiled(&found) {
10665 match self
10666 .graphql(
10667 graphql::ADD_TO_BOARD,
10668 json!({"input":{"projectId":board,"contentId":id.0}}),
10669 )
10670 .await
10671 {
10672 Ok(added) => {
10673 let item = added
10674 .pointer("/addProjectV2ItemById/item")
10675 .filter(|value| !value.is_null())
10676 .ok_or_else(|| SourceError::Malformed {
10677 message: "GitHub board addition returned no project item".into(),
10678 })?;
10679 found.insert(id, (required_nonblank_str(item, "id")?.to_owned(), None));
10680 }
10681 Err(error) if already_on_the_board(&error) => placed_meanwhile.push(id),
10682 Err(error) => return Err(error),
10683 }
10684 }
10685 if !placed_meanwhile.is_empty() {
10686 self.read_created_items(board, &placed_meanwhile, &mut found)
10687 .await?;
10688 }
10689 let absent = unfiled(&found);
10690 if absent.is_empty() {
10691 return Ok(found);
10692 }
10693 let absent: Vec<&str> = absent.iter().map(|id| id.0.as_str()).collect();
10694 Err(SourceError::Unavailable {
10695 message: format!(
10696 "GitHub created {} filed on board {board} and, after 31 seconds and adding {} \
10697 to it, still answers {} on no item of it: {}; next: run the copy again once \
10698 GitHub has caught up",
10699 if absent.len() == 1 {
10700 "an issue"
10701 } else {
10702 "issues"
10703 },
10704 if absent.len() == 1 { "it" } else { "them" },
10705 if absent.len() == 1 { "it" } else { "them" },
10706 absent.join(", ")
10707 ),
10708 })
10709 }
10710
10711 /// One [`graphql::CREATED_BOARD_ITEMS`] read of each of `ids`, [`COPY_DETAIL_BATCH`] to a
10712 /// request, adding to `found` the board item of every one GitHub has filed on `board`.
10713 async fn read_created_items(
10714 &self,
10715 board: &str,
10716 ids: &[NativeId],
10717 found: &mut BTreeMap<NativeId, (String, Option<String>)>,
10718 ) -> Result<(), SourceError> {
10719 for chunk in ids.chunks(COPY_DETAIL_BATCH) {
10720 let mut variables = serde_json::Map::new();
10721 for slot in 0..COPY_DETAIL_BATCH {
10722 variables.insert(
10723 format!("id{slot}"),
10724 json!(chunk.get(slot).unwrap_or(&chunk[0]).0),
10725 );
10726 variables.insert(format!("read{slot}"), json!(slot < chunk.len()));
10727 }
10728 variables.insert("boardItems".into(), json!(BOARD_ITEMS_PAGE_SIZE));
10729 variables.insert("statusName".into(), json!(STATUS_FIELD));
10730 let data = self
10731 .graphql(graphql::CREATED_BOARD_ITEMS, Value::Object(variables))
10732 .await?;
10733 for (slot, id) in chunk.iter().enumerate() {
10734 // An answer for another issue than the one asked about says nothing of
10735 // where that one is filed, and its board item is not this one's.
10736 let node = data.get(format!("i{slot}")).filter(|node| !node.is_null());
10737 let named = node.map(|node| node.get("id").and_then(Value::as_str));
10738 if let Some(named) = named
10739 && named != Some(id.0.as_str())
10740 {
10741 return Err(SourceError::Malformed {
10742 message: format!(
10743 "GitHub answered the board items of {} when asked for those of {}",
10744 named.unwrap_or("an issue it did not name"),
10745 id.0
10746 ),
10747 });
10748 }
10749 // An issue GitHub does not answer yet is not filed yet; one it answers is
10750 // read whole, its connection, each board item's board and its status, and
10751 // any of them unreadable is refused rather than read as not filed.
10752 let Some(node) = node else { continue };
10753 let malformed = |what: &str| SourceError::Malformed {
10754 message: format!("GitHub answered the board items of {} with {what}", id.0),
10755 };
10756 let nodes = node
10757 .pointer("/projectItems/nodes")
10758 .and_then(Value::as_array)
10759 .ok_or_else(|| malformed("no list of board items"))?;
10760 let mut item = None;
10761 for node in nodes {
10762 let on = node
10763 .pointer("/project/id")
10764 .and_then(Value::as_str)
10765 .ok_or_else(|| malformed("a board item naming no board"))?;
10766 if on == board {
10767 item = Some(node);
10768 }
10769 }
10770 if let Some(item) = item {
10771 let status = match item.get("status") {
10772 None | Some(Value::Null) => None,
10773 Some(status) => Some(
10774 status
10775 .get("name")
10776 .and_then(Value::as_str)
10777 .ok_or_else(|| malformed("a status that names no option"))?
10778 .to_owned(),
10779 ),
10780 };
10781 found.insert(
10782 id.clone(),
10783 (required_nonblank_str(item, "id")?.to_owned(), status),
10784 );
10785 }
10786 }
10787 }
10788 Ok(())
10789 }
10790
10791 /// The board item id of `content`, which this process created and remembered without one,
10792 /// in every half of its own view.
10793 fn learn_item(&self, content: &NativeId, item: &str) -> Result<(), SourceError> {
10794 if let Some(held) = self.resolved_cache()?.get_mut(content) {
10795 held.item_id = item.to_owned();
10796 }
10797 if let Some(held) = self.created()?.iter_mut().find(|held| held.id == *content) {
10798 held.item_id = item.to_owned();
10799 }
10800 Ok(())
10801 }
10802
10803 /// Ask together which items each of `origins` names, and what creating an item reads; see
10804 /// [`TaskSource::read_ahead_origins_for_copy`]. An origin whose answer runs past its first
10805 /// page is left to its own lookup.
10806 async fn origins_ahead(&self, origins: &[String]) -> Result<(), SourceError> {
10807 if self.repository.is_none() {
10808 return Ok(());
10809 }
10810 let unasked: Vec<&String> = {
10811 let held = self.narrowed_cache()?;
10812 let mut unasked: Vec<&String> = Vec::new();
10813 for origin in origins {
10814 if !held.contains_key(&Narrowing::Origin(origin.clone()).key())
10815 && !unasked.contains(&origin)
10816 {
10817 unasked.push(origin);
10818 }
10819 }
10820 unasked
10821 };
10822 if unasked.is_empty() {
10823 return Ok(());
10824 }
10825 // One batch is held back for the read ahead that follows, which may carry a project's
10826 // members beside it in the same request.
10827 if unasked.len() <= ORIGIN_BATCH {
10828 let mut pending =
10829 self.pending_origins
10830 .lock()
10831 .map_err(|_| SourceError::Unavailable {
10832 message:
10833 "origins held for a read ahead were left inconsistent; run the command \
10834 again"
10835 .into(),
10836 })?;
10837 for origin in unasked {
10838 if !pending.contains(origin) {
10839 pending.push(origin.clone());
10840 }
10841 }
10842 return Ok(());
10843 }
10844 let unasked: Vec<String> = unasked.into_iter().cloned().collect();
10845 self.send_origin_lookups(&unasked, None).await
10846 }
10847
10848 /// Hold, for [`TaskSource::member_classifications`] of `project`, each member's
10849 /// classification and repositories as the members page `data` carries them — when that page
10850 /// is every member, each one names `project` as its parent, and every record it holds is
10851 /// readable. Any other page holds nothing, so the members are read as they always were.
10852 fn hold_member_classes(&self, project: &NativeId, data: &Value) -> Result<(), SourceError> {
10853 let Some(page) = data
10854 .pointer("/members/subIssues")
10855 .filter(|page| !page.is_null())
10856 else {
10857 return Ok(());
10858 };
10859 if page
10860 .pointer("/pageInfo/hasNextPage")
10861 .and_then(Value::as_bool)
10862 != Some(false)
10863 {
10864 return Ok(());
10865 }
10866 let Some(nodes) = page.get("nodes").and_then(Value::as_array) else {
10867 return Ok(());
10868 };
10869 let mut members = Vec::with_capacity(nodes.len());
10870 for node in nodes {
10871 if node.pointer("/parent/id").and_then(Value::as_str) != Some(project.0.as_str()) {
10872 return Ok(());
10873 }
10874 let Some(body) = node.get("body").and_then(Value::as_str) else {
10875 return Ok(());
10876 };
10877 // Every node here is filed under `project`, which `BoardKind::of` reads as one of its
10878 // tasks whatever its marker says, so each is a member the full read would classify.
10879 let Ok((_, slot)) = metadata_body(Some(body.to_owned())) else {
10880 return Ok(());
10881 };
10882 let Ok(classification) = Classification::from_metadata(&slot) else {
10883 return Ok(());
10884 };
10885 let repositories = if slot.contains_key(Repository::METADATA_KEY) {
10886 let Ok(repositories) = Repository::from_metadata(&slot) else {
10887 return Ok(());
10888 };
10889 repositories
10890 } else {
10891 let Some(origin) = node
10892 .pointer("/repository/nameWithOwner")
10893 .and_then(Value::as_str)
10894 else {
10895 return Ok(());
10896 };
10897 let Ok(own) = Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST))
10898 else {
10899 return Ok(());
10900 };
10901 vec![own]
10902 };
10903 members.push((classification, repositories));
10904 }
10905 self.member_classes
10906 .lock()
10907 .map_err(|_| SourceError::Unavailable {
10908 message: "a project's members held for its classification were left \
10909 inconsistent; run the command again"
10910 .into(),
10911 })?
10912 .insert(project.clone(), members);
10913 Ok(())
10914 }
10915
10916 /// Send every origin held back for the read ahead, beside the members of `members` when it
10917 /// names a project — or nothing, when none is held.
10918 async fn send_pending_origins(&self, members: Option<&NativeId>) -> Result<(), SourceError> {
10919 let pending = std::mem::take(&mut *self.pending_origins.lock().map_err(|_| {
10920 SourceError::Unavailable {
10921 message: "origins held for a read ahead were left inconsistent; run the command \
10922 again"
10923 .into(),
10924 }
10925 })?);
10926 if pending.is_empty() {
10927 return Ok(());
10928 }
10929 self.send_origin_lookups(&pending, members).await
10930 }
10931
10932 /// [`graphql::ORIGIN_LOOKUPS`] of `unasked`, [`ORIGIN_BATCH`] to a request — the first one
10933 /// [`graphql::ORIGIN_LOOKUPS_WITH_MEMBERS`], carrying the members of `members`, when it names
10934 /// a project — holding each origin's answer and, from a members page that is the whole of
10935 /// them, each member's classification.
10936 async fn send_origin_lookups(
10937 &self,
10938 unasked: &[String],
10939 members: Option<&NativeId>,
10940 ) -> Result<(), SourceError> {
10941 let Some(repository) = self.repository.clone() else {
10942 return Ok(());
10943 };
10944 let mut members = members;
10945 for chunk in unasked.chunks(ORIGIN_BATCH) {
10946 let mut variables = json!({"owner":self.owner,"number":self.project_number,
10947 "type":"ISSUE","lookupFirst":ORIGIN_LOOKUPS_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
10948 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true,
10949 "repositoryOwner":repository.owner,"repositoryName":repository.name});
10950 for slot in 0..ORIGIN_BATCH {
10951 let origin = chunk.get(slot).unwrap_or(&chunk[0]);
10952 variables[format!("filter{slot}")] =
10953 json!(format!("{ORIGIN_FIELD}:{}", quoted(origin)));
10954 variables[format!("search{slot}")] = json!(
10955 self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))))
10956 );
10957 variables[format!("ask{slot}")] = json!(slot < chunk.len());
10958 }
10959 let project = members.take();
10960 let document = match project {
10961 Some(project) => {
10962 variables["project"] = json!(project.0);
10963 variables["memberFirst"] = json!(MEMBER_PAGE_SIZE);
10964 graphql::ORIGIN_LOOKUPS_WITH_MEMBERS
10965 }
10966 None => graphql::ORIGIN_LOOKUPS,
10967 };
10968 let data = self.graphql(document, variables).await?;
10969 self.fields_read(&data)?;
10970 if let Some(project) = project {
10971 self.hold_member_classes(project, &data)?;
10972 }
10973 if let Some(node) = data.get("repository").filter(|value| !value.is_null()) {
10974 let id = required_nonblank_str(node, "id")?.to_owned();
10975 self.repository_cache()?.insert(repository.clone(), id);
10976 if let Some(numeric) = node
10977 .get("databaseId")
10978 .and_then(Value::as_u64)
10979 .and_then(std::num::NonZeroU64::new)
10980 {
10981 self.numeric_repositories
10982 .lock()
10983 .await
10984 .insert(repository.clone(), numeric);
10985 }
10986 }
10987 for (slot, origin) in chunk.iter().enumerate() {
10988 let items = data
10989 .pointer(&format!("/o{slot}/projectV2/items"))
10990 .filter(|value| !value.is_null())
10991 .ok_or_else(|| SourceError::Refused {
10992 message: format!(
10993 "GitHub project {}/{} was not found or is not visible to the token",
10994 self.owner, self.project_number
10995 ),
10996 })?;
10997 let searched =
10998 data.get(format!("s{slot}"))
10999 .ok_or_else(|| SourceError::Malformed {
11000 message: "GitHub search response has no search connection".into(),
11001 })?;
11002 if resumed(items, None)?.has_more() || resumed(searched, None)?.has_more() {
11003 continue;
11004 }
11005 let mut found: Vec<Resolved> = Vec::new();
11006 for item in optional_nodes(Some(items), "project items")?
11007 .into_iter()
11008 .flatten()
11009 {
11010 if let Some(resolved) = self.resolve(item)?
11011 && resolved.content_kind == ContentKind::Issue
11012 && !found.iter().any(|held| held.id == resolved.id)
11013 {
11014 found.push(resolved);
11015 }
11016 }
11017 for node in optional_nodes(Some(searched), "search")?
11018 .into_iter()
11019 .flatten()
11020 {
11021 if let Some(resolved) = self.resolve_issue(node).await?
11022 && !found.iter().any(|held| held.id == resolved.id)
11023 {
11024 found.push(resolved);
11025 }
11026 }
11027 self.narrowed_cache()?
11028 .insert(Narrowing::Origin((*origin).clone()).key(), found);
11029 }
11030 }
11031 Ok(())
11032 }
11033
11034 /// Delete every issue of `ids`, [`DELETE_BATCH`] to a request with
11035 /// [`graphql::DELETE_ISSUES`], and answer those still there: each a request's answer does not
11036 /// confirm, every one of a request refused whole included.
11037 async fn delete_issues(&self, ids: &[&NativeId]) -> Vec<NativeId> {
11038 let mut left = Vec::new();
11039 for chunk in ids.chunks(DELETE_BATCH) {
11040 let mut variables = serde_json::Map::new();
11041 for slot in 0..DELETE_BATCH {
11042 let id = chunk.get(slot).unwrap_or(&chunk[0]);
11043 variables.insert(format!("delete{slot}"), json!({"issueId": id.0}));
11044 variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
11045 }
11046 let data = self
11047 .graphql(graphql::DELETE_ISSUES, Value::Object(variables))
11048 .await
11049 .unwrap_or(Value::Null);
11050 for (slot, id) in chunk.iter().enumerate() {
11051 // Confirmed only by the repository the deleted issue was in, named by a
11052 // non-blank id: anything else says nothing of whether the issue is gone.
11053 let confirmed = data
11054 .pointer(&format!("/delete{slot}/repository/id"))
11055 .and_then(Value::as_str)
11056 .is_some_and(|id| !id.trim().is_empty());
11057 if !confirmed {
11058 left.push((*id).clone());
11059 }
11060 }
11061 }
11062 left
11063 }
11064
11065 /// Delete one issue, which takes its board item with it.
11066 async fn delete_issue(&self, id: &NativeId) -> Result<(), SourceError> {
11067 let data = self
11068 .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
11069 .await?;
11070 data.pointer("/deleteIssue/repository")
11071 .filter(|value| !value.is_null())
11072 .ok_or_else(|| SourceError::Malformed {
11073 message: "GitHub issue deletion returned no repository".into(),
11074 })?;
11075 self.forget(id)?;
11076 Ok(())
11077 }
11078
11079 /// Remove one item this copy created, so a copy that could not finish leaves the board
11080 /// as it found it.
11081 ///
11082 /// Deleting the issue takes its board item with it, so there is no second mutation to
11083 /// keep in step. An id the board does not hold is not an error: the item is already
11084 /// gone, which is the state this asks for. Which that is, is decided by reading the item
11085 /// by its own id — a listing of the board can still be missing an item it holds, and
11086 /// reading that as *already gone* would leave behind the very item this was asked to
11087 /// take back.
11088 async fn delete_item(&self, id: &NativeId) -> Result<(), SourceError> {
11089 let Some(item) = self.bound_item(id).await? else {
11090 return Ok(());
11091 };
11092 if item.content_kind == ContentKind::DraftIssue {
11093 return Err(SourceError::Refused {
11094 message: format!(
11095 "GitHub item {} is a draft, and this source removes an item by deleting \
11096 its issue; next: remove it from the board by hand",
11097 id.0
11098 ),
11099 });
11100 }
11101 let data = self
11102 .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
11103 .await?;
11104 data.pointer("/deleteIssue/repository")
11105 .filter(|value| !value.is_null())
11106 .ok_or_else(|| SourceError::Malformed {
11107 message: "GitHub issue deletion returned no repository".into(),
11108 })?;
11109 self.forget(id)?;
11110 Ok(())
11111 }
11112
11113 /// The issue a comment call on `task` is about, or `None` when this board holds no such
11114 /// task.
11115 ///
11116 /// Resolved exactly as [`TaskSource::get_task`] resolves it, so the comment verbs and a
11117 /// read of the task cannot disagree about which ids name one: a project or a document of
11118 /// this board is not a task here either.
11119 ///
11120 /// A **draft** is a task with nowhere to keep a comment, because GitHub keeps comments on
11121 /// issues and a draft is not one. It is refused rather than answered with an empty page,
11122 /// which would read as a task nobody has commented on yet.
11123 async fn commented_issue(&self, task: &NativeId) -> Result<Option<NativeId>, SourceError> {
11124 let cached = self.resolved_cache()?.get(task).cloned();
11125 let Some(item) = (match cached {
11126 Some(item) => Some(item),
11127 None => self.item_by_id(task).await?,
11128 })
11129 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
11130 return Ok(None);
11131 };
11132 if item.content_kind == ContentKind::DraftIssue {
11133 return Err(self.draft_has_no_comments(task));
11134 }
11135 Ok(Some(item.id))
11136 }
11137
11138 /// The refusal a comment call on a board draft is answered with: GitHub keeps comments on
11139 /// issues, and a draft is not one.
11140 fn draft_has_no_comments(&self, task: &NativeId) -> SourceError {
11141 SourceError::Refused {
11142 message: format!(
11143 "task {} of source {} is a draft item on the board, and GitHub keeps \
11144 comments on issues alone, so a draft has none to read or write; next: \
11145 convert the draft to an issue on the board, then comment on the issue it \
11146 becomes",
11147 task.0, self.name
11148 ),
11149 }
11150 }
11151
11152 /// One task and a page of its comments, read with [`graphql::ISSUE_DETAIL`] in one
11153 /// request — or `None` when this board holds no task by that id.
11154 ///
11155 /// What `task show` and a comment listing read. A draft is a task with no comments, so it
11156 /// is answered with the draft and the refusal, at the price of the draft's own read.
11157 async fn issue_detail(
11158 &self,
11159 id: &NativeId,
11160 page: &PageRequest,
11161 ) -> Result<Option<TaskDetailRead>, SourceError> {
11162 let after = page.cursor.as_ref().map(|cursor| cursor.0.as_str());
11163 let asked = self
11164 .graphql(
11165 graphql::ISSUE_DETAIL,
11166 json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),"after":after,
11167 "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
11168 "duplicates":true}),
11169 )
11170 .await;
11171 let data = match asked {
11172 Ok(data) => data,
11173 Err(error) if unresolvable_node(&error) => return Ok(None),
11174 Err(error) => return Err(error),
11175 };
11176 // `node` is null for an id that names nothing, and absent only from an answer this
11177 // source cannot read — never the same thing.
11178 let node = data.get("node").ok_or_else(|| SourceError::Malformed {
11179 message: format!("GitHub answered the read of {} with no node", id.0),
11180 })?;
11181 self.detail_of(id, node, true, after).await
11182 }
11183
11184 /// Several tasks, each with the first page of its comments when `comments` is set, read
11185 /// [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] — one answer per id, in
11186 /// order.
11187 ///
11188 /// A batch GitHub refuses because one of its ids resolves to no node at all is read again
11189 /// one item at a time, so that id is answered as missing and the others as themselves; any
11190 /// other refusal is every id of that batch's answer.
11191 async fn issue_details(
11192 &self,
11193 ids: &[NativeId],
11194 comments: Option<&PageRequest>,
11195 ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
11196 let mut read = Vec::with_capacity(ids.len());
11197 for batch in ids.chunks(DETAIL_BATCH) {
11198 match self
11199 .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, comments))
11200 .await
11201 {
11202 Ok(data) => {
11203 for (slot, id) in batch.iter().enumerate() {
11204 // Every alias asked for is answered, null for an id naming nothing;
11205 // one missing is an answer this source cannot read.
11206 let read_one = match data.get(format!("i{slot}")) {
11207 Some(node) => self.detail_of(id, node, comments.is_some(), None).await,
11208 None => Err(SourceError::Malformed {
11209 message: format!(
11210 "GitHub answered a batch read with no item for {}",
11211 id.0
11212 ),
11213 }),
11214 };
11215 read.push(read_one);
11216 }
11217 }
11218 Err(error) if unresolvable_node(&error) => {
11219 for id in batch {
11220 read.push(match comments {
11221 Some(page) => self.issue_detail(id, page).await,
11222 None => self.task_read(id).await,
11223 });
11224 }
11225 }
11226 Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
11227 }
11228 }
11229 read
11230 }
11231
11232 /// One task and nothing of its comments, as [`TaskSource::get_task`] reads it.
11233 async fn task_read(&self, id: &NativeId) -> Result<Option<TaskDetailRead>, SourceError> {
11234 Ok(self.get_task(id).await?.map(|task| TaskDetailRead {
11235 task,
11236 comments: None,
11237 }))
11238 }
11239
11240 /// What one node a detail read reached says: the task this board holds by `id`, with the
11241 /// page of comments the node carries when `commented` — or `None` for a node that is no
11242 /// task of this board.
11243 ///
11244 /// Resolved as [`Self::item_by_id`] resolves an item: a draft is read again as a draft,
11245 /// and an item this process created answers from this process's own record, which a node
11246 /// read taken moments after the write can still be behind.
11247 async fn detail_of(
11248 &self,
11249 id: &NativeId,
11250 node: &Value,
11251 commented: bool,
11252 after: Option<&str>,
11253 ) -> Result<Option<TaskDetailRead>, SourceError> {
11254 if node.is_null() {
11255 return Ok(None);
11256 }
11257 let draft = optional_str(node, "__typename")? == Some("DraftIssue");
11258 // An issue answered under one id is that id's, or the answer is not one this source
11259 // can report: reporting another issue's task and comments under the qualified id asked
11260 // for would be the one wrong answer here. A draft's own read checks the same.
11261 if !draft
11262 && optional_str(node, "__typename")? == Some("Issue")
11263 && required_str(node, "id")? != id.0
11264 {
11265 return Err(SourceError::Malformed {
11266 message: format!(
11267 "GitHub answered the read of {} with issue {}",
11268 id.0,
11269 required_str(node, "id")?
11270 ),
11271 });
11272 }
11273 let item = if draft {
11274 self.draft_by_id(id).await?
11275 } else {
11276 self.resolve_issue(node).await?
11277 };
11278 // A read that carried the whole of what the issue is blocked by — a copy's — is kept
11279 // for this command, so the dependency read the copy makes next is answered from it.
11280 if let Some(read) = item.as_ref().filter(|item| item.blocked_by.is_some()) {
11281 self.resolved_cache()?.insert(read.id.clone(), read.clone());
11282 }
11283 let Some(item) = item.filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
11284 return Ok(None);
11285 };
11286 let own = self.created()?.iter().find(|own| own.id == *id).cloned();
11287 let task = own.unwrap_or(item).task()?;
11288 let comments = match (commented, draft) {
11289 (false, _) => None,
11290 (true, true) => Some(Err(self.draft_has_no_comments(id))),
11291 (true, false) => Some(comment_page(node, &id.0, after).map(Some)),
11292 };
11293 if commented && !draft && after.is_none() {
11294 self.expected.learn_comments(id, node);
11295 }
11296 Ok(Some(TaskDetailRead { task, comments }))
11297 }
11298
11299 /// Whether the comment `comment` is one of `issue`'s own.
11300 ///
11301 /// Read before an edit or a removal is sent, because GitHub's comment mutations take the
11302 /// comment's id and nothing else: a comment id given against the wrong task would
11303 /// otherwise change a comment on some other issue entirely. An id that names nothing, or
11304 /// names something that is not an issue comment, is a comment this task does not have —
11305 /// which is what GitHub refusing to resolve it means too.
11306 async fn comment_is_on(
11307 &self,
11308 issue: &NativeId,
11309 comment: &NativeId,
11310 ) -> Result<bool, SourceError> {
11311 Ok(self.comment_on(issue, comment).await?.is_some())
11312 }
11313
11314 /// The body the comment `comment` holds, exactly as GitHub holds it, when it is one of
11315 /// `issue`'s own — the read [`Self::comment_is_on`] makes, answering with what it selected.
11316 async fn comment_on(
11317 &self,
11318 issue: &NativeId,
11319 comment: &NativeId,
11320 ) -> Result<Option<String>, SourceError> {
11321 // A write the store's cache expects knows which comments its issue held, and their
11322 // bodies, at the version it expects: what this read would answer.
11323 if let Some(primed) = self.expected.primed_comment(issue, comment) {
11324 return Ok(primed);
11325 }
11326 let asked = self
11327 .graphql(graphql::COMMENT_ISSUE, json!({"id":comment.0}))
11328 .await;
11329 let data = match asked {
11330 Ok(data) => data,
11331 Err(error) if unresolvable_node(&error) => return Ok(None),
11332 Err(error) => return Err(error),
11333 };
11334 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
11335 return Ok(None);
11336 };
11337 if optional_str(node, "__typename")? != Some("IssueComment") {
11338 return Ok(None);
11339 }
11340 let on = node.get("issue").ok_or_else(|| SourceError::Malformed {
11341 message: format!("GitHub issue comment {} names no issue", comment.0),
11342 })?;
11343 if required_str(on, "id")? != issue.0 {
11344 return Ok(None);
11345 }
11346 Ok(Some(required_str(node, "body")?.to_owned()))
11347 }
11348
11349 /// Which far ends this item's own `blockedBy` relationship holds, and which it cannot.
11350 async fn partition_edges(
11351 &self,
11352 near_kind: BoardKind,
11353 near_content: ContentKind,
11354 carried: Option<&[Value]>,
11355 depends_on: &[DependencyEdge],
11356 ) -> Result<(Vec<String>, Vec<DependencyEdge>), SourceError> {
11357 let mut native = Vec::new();
11358 let mut fallback = Vec::new();
11359 let far_ends: Vec<(&DependencyEdge, &str, bool, Option<&Value>)> = depends_on
11360 .iter()
11361 .map(|edge| {
11362 // An opaque far end names another tracker, so it is never this board's own.
11363 let same_source = edge.to.designator().is_none()
11364 && edge
11365 .to
11366 .source()
11367 .is_none_or(|source| source == self.name.as_str());
11368 // A qualified id's source segment runs to its *first* colon — `GlobalId` and
11369 // `DependencyEndpoint::source` both read it that way — and a native id may hold
11370 // colons of its own, so the far end is everything after that one separator.
11371 // Splitting at the last would truncate `work:urn:task:7` to `7`.
11372 let far_id = if edge.to.is_qualified() {
11373 edge.to
11374 .id()
11375 .split_once(':')
11376 .map_or(edge.to.id(), |(_, native)| native)
11377 } else {
11378 edge.to.id()
11379 };
11380 // One that already blocks the near issue was answered by that issue's own
11381 // read, which carried each of its blockers' kinds — an issue every one — so it
11382 // is not read again.
11383 let blocking = carried.and_then(|nodes| {
11384 nodes
11385 .iter()
11386 .find(|node| node.get("id").and_then(Value::as_str) == Some(far_id))
11387 });
11388 (edge, far_id, same_source, blocking)
11389 })
11390 .collect();
11391 // Every other same-source far end is read by its own id, exactly as the item it is a
11392 // far end of is: whether this board holds it is that read's answer, never a listing's.
11393 // They are read together, [`DETAIL_BATCH`] to a request, rather than one each.
11394 let mut unread: Vec<NativeId> = Vec::new();
11395 for (_, far_id, same_source, blocking) in &far_ends {
11396 let id = NativeId((*far_id).to_owned());
11397 if *same_source && blocking.is_none() && !unread.contains(&id) {
11398 unread.push(id);
11399 }
11400 }
11401 let read: BTreeMap<NativeId, Option<Resolved>> = unread
11402 .iter()
11403 .cloned()
11404 .zip(self.items_by_ids(&unread).await?)
11405 .collect();
11406 for (edge, far_id, same_source, blocking) in far_ends {
11407 let far = match (same_source, blocking) {
11408 (false, _) => None,
11409 (true, Some(node)) => Some(FarEnd {
11410 kind: if required_str(node, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
11411 BoardKind::Document
11412 } else {
11413 BoardKind::Work(related_kind(node)?)
11414 },
11415 content_kind: ContentKind::Issue,
11416 }),
11417 (true, None) => {
11418 let read = read
11419 .get(&NativeId(far_id.to_owned()))
11420 .cloned()
11421 .flatten()
11422 .ok_or_else(|| SourceError::Refused {
11423 message: format!("GitHub dependency item {far_id} was not found"),
11424 })?;
11425 Some(FarEnd {
11426 kind: read.kind,
11427 content_kind: read.content_kind,
11428 })
11429 }
11430 };
11431 let far = far.as_ref();
11432 // The caller says which kind the far end is, and this board holds the far end
11433 // itself, so a disagreement is settled here rather than stored: recorded, the
11434 // wrong kind would read back as a cross-level edge that never existed; written
11435 // natively, it would name a relationship of a different level than the caller
11436 // asked for.
11437 //
11438 // A far end this board holds as a *document* fails the same comparison and is
11439 // refused by the same sentence: `ItemKind` has no document variant because
11440 // nothing may point at one, so no caller can name it correctly and the refusal
11441 // is the only honest answer.
11442 if let Some(disagreeing) = far.filter(|far| far.kind != BoardKind::Work(edge.to.kind)) {
11443 return Err(SourceError::Refused {
11444 message: format!(
11445 "GitHub dependency item {far_id} is a {} of this board, and this item \
11446 names it as a {}; record the kind it is",
11447 disagreeing.kind.describes(),
11448 edge.to.kind.marker()
11449 ),
11450 });
11451 }
11452 // A draft has neither `blockedBy` nor `blocking`, so no edge of one is native
11453 // however the far end is spelled — and one classified native here would be
11454 // written nowhere at all, because a draft's native reconciliation never runs.
11455 let native_here = near_content == ContentKind::Issue
11456 && far.is_some_and(|far| {
11457 far.content_kind == ContentKind::Issue
11458 && BoardKind::Work(edge.to.kind) == near_kind
11459 });
11460 if native_here {
11461 native.push(far_id.to_owned());
11462 } else {
11463 fallback.push(edge.clone());
11464 }
11465 }
11466 Ok((native, fallback))
11467 }
11468
11469 async fn update_existing(
11470 &self,
11471 item: &Resolved,
11472 incoming: &Incoming<'_>,
11473 body: &Option<String>,
11474 status_target: Option<&StatusTarget>,
11475 ) -> Result<(), SourceError> {
11476 let title = incoming.written_title();
11477 // A terminal status closes the issue here, in the same mutation as its body: its board
11478 // option was selected before this, so a close never lands on an item whose board cannot
11479 // show it.
11480 let mut fields = match item.content_kind {
11481 ContentKind::DraftIssue => json!({"title":title,"body":body}),
11482 ContentKind::Issue => json!({"title":title,"body":body,
11483 "stateInput":state_input(status_target)}),
11484 };
11485 // Only the members whose value differs from the item as this write read it are sent,
11486 // and nothing at all when none does — whether or not the store's cache expects a
11487 // version of it. A member left out is one GitHub leaves as it is, so a person's edit
11488 // of it since that read is kept rather than overwritten with what it already held,
11489 // and a re-copy of an unchanged item costs no mutation.
11490 if let Some(members) = fields.as_object_mut() {
11491 if members.get("title").and_then(Value::as_str) == Some(item.title.as_str()) {
11492 members.remove("title");
11493 }
11494 let body_unchanged = match (members.get("body"), item.raw_body.as_deref()) {
11495 (Some(Value::String(sent)), Some(held)) => sent == held,
11496 (Some(Value::Null), held) => held.is_none_or(str::is_empty),
11497 _ => false,
11498 };
11499 if body_unchanged {
11500 members.remove("body");
11501 }
11502 // An issue already in the state the target asks for keeps it: an open one stays
11503 // open, and a closed one whose closure reads as the target's category keeps its
11504 // reason — a duplicate's says more than this write would.
11505 let unchanged_state = match status_target {
11506 Some(StatusTarget::Terminal(_, reason)) => {
11507 item.closed
11508 && closed_category(item.reason.as_deref())
11509 == closed_category(Some(reason.reason()))
11510 }
11511 Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => !item.closed,
11512 None => true,
11513 };
11514 if unchanged_state {
11515 members.remove("stateInput");
11516 }
11517 if members.is_empty() {
11518 return Ok(());
11519 }
11520 }
11521 self.update_content(item.content_kind, &item.id, fields)
11522 .await
11523 }
11524
11525 /// Update one board item's content with exactly `fields` beside its id, through the
11526 /// mutation its kind takes: `updateIssue` for an issue, `updateProjectV2DraftIssue` for
11527 /// a draft.
11528 ///
11529 /// Every input field either mutation leaves out is a field GitHub leaves as it is, which
11530 /// is what lets a narrow write carry the one thing it changes and nothing else.
11531 async fn update_content(
11532 &self,
11533 kind: ContentKind,
11534 id: &NativeId,
11535 fields: Value,
11536 ) -> Result<(), SourceError> {
11537 let (operation, id_key, pointer) = match kind {
11538 ContentKind::DraftIssue => (
11539 graphql::UPDATE_DRAFT,
11540 "draftIssueId",
11541 "/updateProjectV2DraftIssue/draftIssue",
11542 ),
11543 ContentKind::Issue => (graphql::UPDATE_ISSUE, "id", "/updateIssue/issue"),
11544 };
11545 if kind == ContentKind::Issue && self.hold_update(id, &fields)? {
11546 return Ok(());
11547 }
11548 let mut input = fields;
11549 input[id_key] = json!(id.0);
11550 self.send_update(operation, pointer, id, input).await
11551 }
11552
11553 /// Send one item's update, `input`, as `operation`, and hold GitHub's answer at `pointer`
11554 /// to naming that item, `id`.
11555 async fn send_update(
11556 &self,
11557 operation: &str,
11558 pointer: &str,
11559 id: &NativeId,
11560 input: Value,
11561 ) -> Result<(), SourceError> {
11562 let data = self.graphql(operation, json!({"input":input})).await?;
11563 let returned = data
11564 .pointer(pointer)
11565 .ok_or_else(|| SourceError::Malformed {
11566 message: "GitHub item update returned no item".into(),
11567 })?;
11568 if required_str(returned, "id")? != id.0 {
11569 return Err(SourceError::Malformed {
11570 message: "GitHub item update returned the wrong item".into(),
11571 });
11572 }
11573 Ok(())
11574 }
11575
11576 /// Creates one issue, files it on the board, and reports what a read of it would say:
11577 /// its content id, its board item id, and the web address GitHub gave it.
11578 ///
11579 /// Two calls rather than one: `createIssue` answers with an issue that is on no board,
11580 /// and `addProjectV2ItemById` is what puts it there. Filing it at creation through
11581 /// `CreateIssueInput.projectV2Ids` was tried and is not done: GitHub answered with no
11582 /// board item, and the `addProjectV2ItemById` that then had to follow was refused
11583 /// "Content already exists in this project". A terminal status is not written here:
11584 /// `finish_write` selects its option first and closes the issue after, so a close never
11585 /// lands on an item whose board cannot show it.
11586 ///
11587 /// The address and the number come back here because this is the only place either is
11588 /// known before GitHub's own board read catches up — an item this run created answers
11589 /// the reads that follow it out of the record below, and one remembered without them
11590 /// would report no location and no key for the rest of the run.
11591 async fn create_and_file_issue(
11592 &self,
11593 board_id: &str,
11594 repository: &RepositoryTarget,
11595 incoming: &Incoming<'_>,
11596 body: &Option<String>,
11597 ) -> Result<Landed, SourceError> {
11598 let repository_id = self.repository_id(repository, incoming).await?;
11599 let data = self
11600 .graphql(
11601 graphql::CREATE_ISSUE,
11602 json!({"input":{
11603 "repositoryId":repository_id,"title":incoming.written_title(),"body":body
11604 }}),
11605 )
11606 .await?;
11607 let created = data
11608 .pointer("/createIssue/issue")
11609 .filter(|value| !value.is_null())
11610 .ok_or_else(|| SourceError::Malformed {
11611 message: "GitHub issue creation returned no issue".into(),
11612 })?;
11613 let content_id = NativeId(required_str(created, "id")?.to_owned());
11614 // Optional although GitHub's schema makes it non-null: the issue exists by now, so
11615 // a response without it is not worth failing a landed write over — the item simply
11616 // reports no location until the board read catches up, which is what it did before.
11617 let url = optional_str(created, "url")?.map(str::to_owned);
11618 // The issue exists from here on, so an unreadable number and a refused board
11619 // filing below each try, best effort, to take it back: an issue in the repository
11620 // that is on no board is an item nobody asked for and nothing here would find again.
11621 //
11622 // Its number is optional on the same terms its address is — a landed write is not
11623 // worth failing over a member that came back missing, and such an item reports no
11624 // handle until a board read catches up. A number that is *present* and is not an
11625 // unsigned integer is still a response this source cannot read.
11626 let number = match created_issue_number(created) {
11627 Ok(number) => number,
11628 Err(error) => {
11629 let _ = self.delete_issue(&content_id).await;
11630 return Err(error);
11631 }
11632 };
11633 let added = match self
11634 .graphql(
11635 graphql::ADD_TO_BOARD,
11636 json!({"input":{"projectId":board_id,"contentId":content_id.0}}),
11637 )
11638 .await
11639 {
11640 Ok(added) => added,
11641 Err(error) => {
11642 let _ = self.delete_issue(&content_id).await;
11643 return Err(error);
11644 }
11645 };
11646 let item = added
11647 .pointer("/addProjectV2ItemById/item")
11648 .filter(|value| !value.is_null())
11649 .ok_or_else(|| SourceError::Malformed {
11650 message: "GitHub board addition returned no project item".into(),
11651 })?;
11652 self.note_written(&content_id, url.as_deref());
11653 Ok(Landed {
11654 content_id,
11655 item_id: Some(required_str(item, "id")?.to_owned()),
11656 url,
11657 number,
11658 })
11659 }
11660
11661 /// Move one issue under the project it now belongs to, or out of the one it left.
11662 async fn reparent(
11663 &self,
11664 held: Option<NativeId>,
11665 child: &NativeId,
11666 wanted: Option<&NativeId>,
11667 ) -> Result<(), SourceError> {
11668 if held.as_ref() == wanted {
11669 return Ok(());
11670 }
11671 if let Some(held) = &held {
11672 self.sub_issue(graphql::REMOVE_SUB_ISSUE, held, child, "removeSubIssue")
11673 .await?;
11674 }
11675 if let Some(wanted) = wanted {
11676 self.sub_issue(graphql::ADD_SUB_ISSUE, wanted, child, "addSubIssue")
11677 .await?;
11678 }
11679 Ok(())
11680 }
11681
11682 async fn sub_issue(
11683 &self,
11684 operation: &str,
11685 parent: &NativeId,
11686 child: &NativeId,
11687 root: &str,
11688 ) -> Result<(), SourceError> {
11689 let data = self
11690 .graphql(
11691 operation,
11692 json!({"input":{"issueId":parent.0,"subIssueId":child.0}}),
11693 )
11694 .await?;
11695 let issue =
11696 data.pointer(&format!("/{root}/issue"))
11697 .ok_or_else(|| SourceError::Malformed {
11698 message: "GitHub sub-issue update returned no issue".into(),
11699 })?;
11700 let sub =
11701 data.pointer(&format!("/{root}/subIssue"))
11702 .ok_or_else(|| SourceError::Malformed {
11703 message: "GitHub sub-issue update returned no sub-issue".into(),
11704 })?;
11705 if required_str(issue, "id")? != parent.0 || required_str(sub, "id")? != child.0 {
11706 return Err(SourceError::Malformed {
11707 message: "GitHub sub-issue update returned the wrong issues".into(),
11708 });
11709 }
11710 Ok(())
11711 }
11712
11713 /// Bring one issue's `blockedBy` to exactly `native`, sending only the difference, and say
11714 /// whether there was one.
11715 ///
11716 /// An issue [`Issue::Created`] by this very write is blocked by nothing yet, so its
11717 /// relationships are not read: there is nothing a read of them could find.
11718 async fn reconcile_blocked_by(
11719 &self,
11720 content_id: &NativeId,
11721 native: &[String],
11722 issue: Issue<'_>,
11723 ) -> Result<bool, SourceError> {
11724 let current = match issue {
11725 Issue::Created => Vec::new(),
11726 Issue::Existing(Some(held)) => held
11727 .iter()
11728 .map(|far| required_str(far, "id").map(str::to_owned))
11729 .collect::<Result<Vec<_>, _>>()?,
11730 Issue::Existing(None) => self.native_dependency_ids(content_id).await?,
11731 };
11732 let mut changed = false;
11733 for (operation, far_id) in current
11734 .iter()
11735 .filter(|id| !native.contains(id))
11736 .map(|id| (graphql::REMOVE_BLOCKED_BY, id))
11737 .chain(
11738 native
11739 .iter()
11740 .filter(|id| !current.contains(id))
11741 .map(|id| (graphql::ADD_BLOCKED_BY, id)),
11742 )
11743 {
11744 let data = self
11745 .graphql(
11746 operation,
11747 json!({"input":{"issueId":content_id.0,"blockingIssueId":far_id}}),
11748 )
11749 .await?;
11750 let root = if operation == graphql::ADD_BLOCKED_BY {
11751 "addBlockedBy"
11752 } else {
11753 "removeBlockedBy"
11754 };
11755 let issue =
11756 data.pointer(&format!("/{root}/issue"))
11757 .ok_or_else(|| SourceError::Malformed {
11758 message: "GitHub dependency update returned no issue".into(),
11759 })?;
11760 let blocker = data
11761 .pointer(&format!("/{root}/blockingIssue"))
11762 .ok_or_else(|| SourceError::Malformed {
11763 message: "GitHub dependency update returned no blocking issue".into(),
11764 })?;
11765 if required_str(issue, "id")? != content_id.0 || required_str(blocker, "id")? != far_id
11766 {
11767 return Err(SourceError::Malformed {
11768 message: "GitHub dependency update returned the wrong issues".into(),
11769 });
11770 }
11771 changed = true;
11772 }
11773 Ok(changed)
11774 }
11775}
11776
11777/// The board field values one write sends, in the order GitHub runs them, and the fields it
11778/// clears after them: the origin field when it moves, the `Status` option `column` names, the
11779/// priority, then each projected field.
11780fn field_writes<'a>(
11781 existing: Option<&Resolved>,
11782 origin_field: Option<&str>,
11783 origin: &str,
11784 column: Option<(String, String)>,
11785 priority: Option<&'a PriorityWrite>,
11786 projections: &'a [ProjectionWrite],
11787) -> (Vec<(String, Value)>, Vec<&'a str>) {
11788 let mut fields = Vec::new();
11789 if let Some(field_id) = origin_field
11790 && existing.map_or(!origin.is_empty(), |item| {
11791 item.origin.as_deref().unwrap_or("") != origin
11792 })
11793 {
11794 fields.push((field_id.to_owned(), json!({"text":origin})));
11795 }
11796 if let Some((field_id, option_id)) = column {
11797 fields.push((field_id, json!({"singleSelectOptionId":option_id})));
11798 }
11799 let mut clears = Vec::new();
11800 match priority {
11801 Some(PriorityWrite::Select { field, option }) => {
11802 fields.push((field.clone(), json!({"singleSelectOptionId":option})));
11803 }
11804 Some(PriorityWrite::Clear { field }) => clears.push(field.as_str()),
11805 None => {}
11806 }
11807 for projection in projections {
11808 projection.join(&mut fields, &mut clears);
11809 }
11810 (fields, clears)
11811}
11812
11813/// Everything one write decided before sending anything: see
11814/// [`GitHubProjectsSource::prepare_write`].
11815struct Prepared {
11816 /// What the write lands on.
11817 destination: WriteDestination,
11818 board: BoardFields,
11819 status_target: Option<StatusTarget>,
11820 /// The `Status` field, the option and the option's name the status lands on.
11821 // 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.
11822 column: Option<(String, String, String)>,
11823 priority_write: Option<PriorityWrite>,
11824 projections: Vec<ProjectionWrite>,
11825 projected: BTreeMap<String, String>,
11826 content_kind: ContentKind,
11827 own_repository: Option<Repository>,
11828 /// The far ends of the item's native blocked-by relationships.
11829 // 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.
11830 native: Vec<String>,
11831 slot: BTreeMap<String, Value>,
11832 content: Option<String>,
11833 body: Option<String>,
11834 /// The copy origin the item records, empty for none.
11835 // 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.
11836 origin: String,
11837 origin_field: Option<String>,
11838}
11839
11840/// One write a copy held back of the kinds sent together: a board field value, or a blocked-by
11841/// edge, each its input.
11842enum HeldWrite<'a> {
11843 Field(&'a Value),
11844 Edge(&'a Value),
11845}
11846
11847/// Whether `error` is GitHub refusing to file an issue on a board because it is filed there
11848/// already — what `addProjectV2ItemById` answers once `createIssue`'s `projectV2Ids` has placed
11849/// it, rather than answering with the item it holds.
11850fn already_on_the_board(error: &SourceError) -> bool {
11851 error
11852 .to_string()
11853 .contains("Content already exists in this project")
11854}
11855
11856/// Hold every alias `inputs` sent under `prefix` in `data` to answering with what its input
11857/// named — each `(root, named)` of `answers` — and refuse one that answered nothing or another
11858/// item.
11859fn check_aliased_answers(
11860 document: &str,
11861 data: &Value,
11862 prefix: &str,
11863 inputs: &[&Value],
11864 answers: &[(&str, &str)],
11865) -> Result<(), SourceError> {
11866 for (slot, input) in inputs.iter().enumerate() {
11867 for (root, named) in answers {
11868 let answered = data
11869 .pointer(&format!("/{prefix}{slot}/{root}/id"))
11870 .and_then(Value::as_str)
11871 .ok_or_else(|| SourceError::Malformed {
11872 message: format!(
11873 "GitHub answered {} with nothing at {prefix}{slot}",
11874 operation_description(document)
11875 ),
11876 })?;
11877 let asked = input.get(named).and_then(Value::as_str).unwrap_or_default();
11878 if answered != asked {
11879 return Err(SourceError::Malformed {
11880 message: format!(
11881 "GitHub answered {} at {prefix}{slot} with {answered}, which is not \
11882 {asked}, the item it was asked to write",
11883 operation_description(document)
11884 ),
11885 });
11886 }
11887 }
11888 }
11889 Ok(())
11890}
11891
11892/// `error`, which stopped a batch of creates, and — when GitHub refused to take any of them
11893/// back — each issue of `left` it created that is still in the repository, which its caller,
11894/// never handed their ids, could not name.
11895fn left_behind(error: SourceError, left: &[&str]) -> SourceError {
11896 if left.is_empty() {
11897 return error;
11898 }
11899 SourceError::Refused {
11900 message: format!(
11901 "{error}; GitHub then refused to take back {}, which this write created and which \
11902 are still in the repository; next: delete {} before writing again",
11903 left.join(", "),
11904 if left.len() == 1 { "it" } else { "them" }
11905 ),
11906 }
11907}
11908
11909/// What one write lands on: the item it updates, as it stood, or where it creates a new one.
11910enum WriteDestination {
11911 /// The item as it stood, for an update.
11912 Existing(Box<Resolved>),
11913 /// Where a new item is created.
11914 New(RepositoryTarget),
11915}
11916
11917impl WriteDestination {
11918 /// The item an update writes over, as it stood; `None` for a create.
11919 fn existing(&self) -> Option<&Resolved> {
11920 match self {
11921 Self::Existing(item) => Some(item),
11922 Self::New(_) => None,
11923 }
11924 }
11925}
11926
11927/// One existing issue's update held back by [`GitHubProjectsSource::hold_update`]: the issue,
11928/// and the `UpdateIssueInput` its own [`graphql::UPDATE_ISSUE`] would have sent — the members
11929/// it writes beside that issue's id, which only [`Self::new`] sets.
11930struct HeldUpdate {
11931 issue: NativeId,
11932 // 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.
11933 input: Value,
11934}
11935
11936impl HeldUpdate {
11937 /// The update writing `members` — an `UpdateIssueInput` without its id — to `issue`.
11938 fn new(issue: &NativeId, members: &Value) -> Self {
11939 let mut input = members.clone();
11940 input["id"] = json!(issue.0);
11941 Self {
11942 issue: issue.clone(),
11943 input,
11944 }
11945 }
11946}
11947
11948/// What a copy has held back of the items it created; see
11949/// [`GitHubProjectsSource::hold_back`].
11950#[derive(Debug, Default)]
11951enum Deferred {
11952 /// Writes are being held back, and nothing has been held yet.
11953 #[default]
11954 Nothing,
11955 /// The items held back, every one of them on `board`.
11956 Held {
11957 /// The board every one of them is on.
11958 board: BoardId,
11959 /// Each created item, in the order it was created.
11960 created: Vec<HeldBack>,
11961 },
11962}
11963
11964impl Deferred {
11965 /// Whether `content` is one of the items held back.
11966 fn holds(&self, content: &NativeId) -> bool {
11967 match self {
11968 Self::Nothing => false,
11969 Self::Held { created, .. } => created.iter().any(|held| held.content == *content),
11970 }
11971 }
11972}
11973
11974/// One created item's parts that land once [`TaskSource::finish_writes`] is called.
11975#[derive(Debug)]
11976struct HeldBack {
11977 /// The issue.
11978 content: NativeId,
11979 /// Its board field values other than its `Status`, in the order they are written.
11980 fields: Vec<(String, Value)>,
11981 /// The `Status` field, and the option and its name that the item's status lands on.
11982 // llmlint: ignore[invalid_states_unrepresentable] The same triple `column_for` returns and `Prepared::column` carries, held back unchanged until it is sent.
11983 status: Option<(String, String, String)>,
11984 /// The fields it clears after those are written.
11985 clears: Vec<String>,
11986 /// The reason it is closed for, once its option is selected; `None` leaves it open.
11987 close: Option<ClosedState>,
11988 /// The issues it is blocked by.
11989 edges: Vec<NativeId>,
11990}
11991
11992/// The member [`answer_with_errors`] carries a response's GraphQL errors under, beside its data:
11993/// no GitHub type has a field of this name.
11994const ANSWER_ERRORS: &str = "onetaskgraphErrors";
11995
11996/// A response's `data` with its GraphQL errors beside it under [`ANSWER_ERRORS`], when it has
11997/// both — or `None`, for an answer [`GitHubProjectsSource::answer`] reads as it always has.
11998///
11999/// What [`graphql::CREATE_ISSUES`] is answered with: GitHub runs a document's mutation fields in
12000/// order and answers each one that ran, so a refusal of one alias leaves the issues the others
12001/// created named in the data beside it — the ids a copy needs to take them back.
12002fn answer_with_errors(body: &str) -> Option<Value> {
12003 let parsed: Value = serde_json::from_str(body).ok()?;
12004 let errors = parsed
12005 .get("errors")?
12006 .as_array()
12007 .filter(|errors| !errors.is_empty())?;
12008 let mut data = parsed.get("data").filter(|data| data.is_object())?.clone();
12009 data[ANSWER_ERRORS] = Value::Array(errors.clone());
12010 Some(data)
12011}
12012
12013/// The `createIssue` input that files the new issue on `board` and, when `incoming` names one,
12014/// under its project — see [`GitHubProjectsSource::create_filed_issue`].
12015fn filed_input(
12016 repository: &str,
12017 board: &str,
12018 incoming: &Incoming<'_>,
12019 body: &Option<String>,
12020) -> Value {
12021 let mut input = json!({"repositoryId":repository,"title":incoming.written_title(),
12022 "body":body,"projectV2Ids":[board]});
12023 if let Some(parent) = incoming.parent {
12024 input["parentIssueId"] = json!(parent.0);
12025 }
12026 input
12027}
12028
12029/// The name of option `option` of field `field` among a board's `fields`.
12030fn option_name<'a>(fields: &'a Value, field: &str, option: &str) -> Option<&'a str> {
12031 fields
12032 .get("nodes")?
12033 .as_array()?
12034 .iter()
12035 .find(|node| node.get("id").and_then(Value::as_str) == Some(field))?
12036 .get("options")?
12037 .as_array()?
12038 .iter()
12039 .find(|candidate| candidate.get("id").and_then(Value::as_str) == Some(option))?
12040 .get("name")?
12041 .as_str()
12042}
12043
12044/// What a write needs to know of one far end it names: which kind of item it is, and whether
12045/// it is an issue a native relationship can name.
12046struct FarEnd {
12047 kind: BoardKind,
12048 content_kind: ContentKind,
12049}
12050
12051/// Whether the issue one write reconciles was created by that write or was already there.
12052#[derive(Clone, Copy, PartialEq, Eq)]
12053enum Issue<'a> {
12054 /// Created by this write, so it holds no relationships yet.
12055 Created,
12056 /// On the board before this write, holding whatever relationships it holds — the far
12057 /// ends of its whole `blockedBy`, when the read that reached it carried them.
12058 Existing(Option<&'a [Value]>),
12059}
12060
12061/// What resolving one node id reached; see [`GitHubProjectsSource::reach`].
12062enum Reached {
12063 /// An issue this board holds, resolved into everything this source reports about it.
12064 Held(Box<Resolved>),
12065 /// Nothing this board holds: no such node, or a node on some other board.
12066 Nothing,
12067 /// A board draft, which [`graphql::ISSUE`] reaches and reads nothing of, so it is read
12068 /// again by [`GitHubProjectsSource::draft_by_id`].
12069 Draft,
12070}
12071
12072/// What GitHub says when a string is not a node id it can resolve.
12073///
12074/// Matched because it is the ordinary answer to a project selector naming a project by its
12075/// *name*, and reporting that as a failure would make naming one impossible. It is read
12076/// off the refusal GitHub sent, never guessed from the shape of the string: this source
12077/// does not define the syntax of a GitHub node id and would be wrong about it.
12078const UNRESOLVABLE_NODE: &str = "could not resolve to a node";
12079
12080/// `error` with `note` added to the end of what it says, its kind and every other member
12081/// unchanged — so a caller still branches on the failure that happened, and reads beside it
12082/// what that failure left behind.
12083fn noting(error: SourceError, note: &str) -> SourceError {
12084 match error {
12085 SourceError::Config { message } => SourceError::Config {
12086 message: message + note,
12087 },
12088 SourceError::Auth { message } => SourceError::Auth {
12089 message: message + note,
12090 },
12091 SourceError::Refused { message } => SourceError::Refused {
12092 message: message + note,
12093 },
12094 SourceError::RateLimited {
12095 retry_after_seconds,
12096 message,
12097 } => SourceError::RateLimited {
12098 retry_after_seconds,
12099 message: Some(message.unwrap_or_default() + note),
12100 },
12101 SourceError::Unavailable { message } => SourceError::Unavailable {
12102 message: message + note,
12103 },
12104 SourceError::Malformed { message } => SourceError::Malformed {
12105 message: message + note,
12106 },
12107 }
12108}
12109
12110/// The variables of one [`graphql::ISSUE_COPY_DETAILS`] request, as [`detail_batch`] binds its
12111/// own: a slot `batch` has no id for is bound to its last id, read again at no added price.
12112fn copy_detail_batch(batch: &[NativeId], comments: &PageRequest, children: bool) -> Value {
12113 let mut variables = serde_json::Map::new();
12114 for slot in 0..COPY_DETAIL_BATCH {
12115 let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
12116 variables.insert(format!("id{slot}"), json!(id));
12117 }
12118 variables.insert("project".to_owned(), json!(batch.first().map(|id| &id.0)));
12119 variables.insert("children".to_owned(), json!(children));
12120 variables.insert("first".to_owned(), json!(comments.limit.min(MAX_PAGE_SIZE)));
12121 variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
12122 variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
12123 variables.insert("duplicates".to_owned(), json!(true));
12124 Value::Object(variables)
12125}
12126
12127/// The variables of one [`graphql::ISSUE_DETAILS`] request over `batch` — at most
12128/// [`DETAIL_BATCH`] ids — each item with the first page of its comments when `comments` asks
12129/// for them.
12130///
12131/// The document is fixed-size, so a slot `batch` has no id for is bound to its last id, which
12132/// is read again at no added price.
12133fn detail_batch(batch: &[NativeId], comments: Option<&PageRequest>) -> Value {
12134 let mut variables = serde_json::Map::new();
12135 for slot in 0..DETAIL_BATCH {
12136 let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
12137 variables.insert(format!("id{slot}"), json!(id));
12138 }
12139 variables.insert(
12140 "first".to_owned(),
12141 json!(comments.map_or(MAX_PAGE_SIZE, |page| page.limit.min(MAX_PAGE_SIZE))),
12142 );
12143 variables.insert("comments".to_owned(), json!(comments.is_some()));
12144 variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
12145 variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
12146 variables.insert("duplicates".to_owned(), json!(true));
12147 Value::Object(variables)
12148}
12149
12150/// Whether this refusal is GitHub saying the id names no node at all.
12151fn unresolvable_node(error: &SourceError) -> bool {
12152 matches!(error, SourceError::Refused { message }
12153 if message.to_ascii_lowercase().contains(UNRESOLVABLE_NODE))
12154}
12155
12156/// One project name, as a search qualifier which filters on it at the server.
12157///
12158/// Quoted so the whole title is one phrase rather than a bag of words, with the two
12159/// characters GitHub's own quoting grammar gives a meaning inside a quoted phrase escaped
12160/// the way it documents. A title matched here is still compared for equality afterwards:
12161/// the qualifier narrows what the server sends, and this source decides what it names.
12162fn title_qualifier(name: &str) -> String {
12163 format!("in:title {}", quoted(name))
12164}
12165
12166/// `value` as one quoted phrase of a GitHub search or a board filter, with the two
12167/// characters GitHub's quoting grammar gives a meaning inside a quoted phrase escaped the way
12168/// it documents — so a value holding a qualifier's spelling is searched for rather than
12169/// obeyed.
12170fn quoted(value: &str) -> String {
12171 let escaped = value.replace('\\', "\\\\").replace('"', "\\\"");
12172 format!("\"{escaped}\"")
12173}
12174
12175/// The search qualifier for the issues updated at or after `since`.
12176///
12177/// Written to the second, rounded down, which can only widen what the search returns.
12178fn updated_qualifier(since: DateTime<Utc>) -> String {
12179 format!("updated:>={}", since.format("%Y-%m-%dT%H:%M:%S+00:00"))
12180}
12181
12182/// The search terms that narrow a board-scoped issue search to a task query's text and
12183/// metadata predicates, or `None` when it carries neither.
12184///
12185/// The text is one quoted phrase, searched `in:title`, `in:body` or both as its fields say,
12186/// and each metadata value is one more quoted phrase, which GitHub finds in the body because
12187/// its index covers the metadata comment the value is stored in. GitHub ANDs the phrases and
12188/// matches each in any field the `in:` qualifier names, so a query naming a title search and
12189/// a metadata value searches both fields for both — wider than asked, never narrower, and
12190/// every candidate is confirmed in process afterwards.
12191///
12192/// **This narrows a text search, and that is this source's declared semantics.** GitHub
12193/// matches whole tokens where a substring rule would match inside a word, so an item holding
12194/// the text only inside a longer word is not returned. A text of nothing but whitespace
12195/// matches every item, so it narrows nothing and is not sent.
12196fn narrowing_qualifiers(query: &TaskQuery) -> Option<String> {
12197 let text = query
12198 .text
12199 .as_ref()
12200 .filter(|text| !text.terms.trim().is_empty());
12201 if text.is_none() && query.metadata.is_empty() {
12202 return None;
12203 }
12204 let (title, body) = match text.map(|text| text.fields) {
12205 None => (false, true),
12206 Some(TextFields::Title) => (true, !query.metadata.is_empty()),
12207 Some(TextFields::Content) => (false, true),
12208 Some(TextFields::TitleOrContent) => (true, true),
12209 };
12210 let fields = match (title, body) {
12211 (true, true) => "in:title,body",
12212 (true, false) => "in:title",
12213 _ => "in:body",
12214 };
12215 let phrases = text
12216 .map(|text| text.terms.clone())
12217 .into_iter()
12218 .chain(
12219 query
12220 .metadata
12221 .iter()
12222 .map(|wanted| as_stored(wanted.value())),
12223 )
12224 .map(|phrase| quoted(&phrase))
12225 .collect::<Vec<_>>();
12226 Some(format!("{fields} {}", phrases.join(" ")))
12227}
12228
12229/// The search terms that narrow a board-scoped issue search to a project or document query's
12230/// text, or `None` when it has none or a blank one: the phrase, in the fields, a task query
12231/// carrying that text alone is sent as by [`narrowing_qualifiers`].
12232fn text_qualifiers(text: Option<&TextQuery>) -> Option<String> {
12233 narrowing_qualifiers(&TaskQuery {
12234 text: text.cloned(),
12235 ..TaskQuery::default()
12236 })
12237}
12238
12239/// Refuses a project or document query's text GitHub's issue search cannot find, before
12240/// anything is asked of GitHub, on exactly the terms [`refuse_unsearchable`] refuses a task
12241/// query's.
12242fn refuse_unsearchable_text(text: Option<&TextQuery>) -> Result<(), SourceError> {
12243 refuse_unsearchable(&TaskQuery {
12244 text: text.cloned(),
12245 ..TaskQuery::default()
12246 })
12247}
12248
12249/// Refuses a task query naming a text or a metadata value GitHub's issue search cannot find,
12250/// before anything is asked of GitHub.
12251///
12252/// GitHub's index holds words, so a phrase with no letter or digit names none to find, and no
12253/// bounded query answers it: sent, GitHub's answer to it is nothing this source may rely on;
12254/// left out, the search is every issue of the board. So this source says it cannot answer
12255/// rather than reading the board or answering nothing. A blank text is not refused: it narrows
12256/// nothing GitHub could search for, and keeps the board read it always had.
12257fn refuse_unsearchable(query: &TaskQuery) -> Result<(), SourceError> {
12258 const WHY: &str = "GitHub's issue search indexes words, so it cannot answer a value with no \
12259 letter or digit with a bounded query";
12260 if let Some(text) = &query.text
12261 && !text.terms.trim().is_empty()
12262 && !has_words(&text.terms)
12263 {
12264 return Err(SourceError::Refused {
12265 message: format!(
12266 "cannot search for the text {:?}: {WHY}; search for a text holding a letter or a digit",
12267 text.terms
12268 ),
12269 });
12270 }
12271 if let Some(wanted) = query
12272 .metadata
12273 .iter()
12274 .find(|wanted| !has_words(wanted.value()))
12275 {
12276 return Err(SourceError::Refused {
12277 message: format!(
12278 "cannot filter by the metadata value {:?} at {:?}: {WHY}; filter by a value holding a letter or a digit",
12279 wanted.value(),
12280 std::iter::once(wanted.key())
12281 .chain(wanted.path().iter().map(String::as_str))
12282 .collect::<Vec<_>>()
12283 .join("/"),
12284 ),
12285 });
12286 }
12287 Ok(())
12288}
12289
12290/// Whether GitHub's index could hold a word of `phrase`: whether it has a letter or a digit.
12291fn has_words(phrase: &str) -> bool {
12292 phrase.chars().any(char::is_alphanumeric)
12293}
12294
12295/// `value` spelled the way the metadata slot stores it: as the inside of its JSON string.
12296///
12297/// What GitHub indexes is the slot's JSON text, so a value holding a character JSON escapes —
12298/// a newline, a tab, a quote — is found by the escape the body holds and not by the character,
12299/// which GitHub's word match would read as different words.
12300fn as_stored(value: &str) -> String {
12301 let encoded = Value::String(value.to_owned()).to_string();
12302 encoded[1..encoded.len() - 1].to_owned()
12303}
12304
12305/// The one narrower question a task query carrying a text, metadata or origin predicate is
12306/// sent as.
12307enum Narrowing {
12308 /// Every carrier of this origin: [`graphql::ORIGIN_LOOKUP`].
12309 Origin(String),
12310 /// The board-scoped issue search narrowed by these qualifiers.
12311 Search(String),
12312}
12313
12314impl Narrowing {
12315 /// What this question is remembered under for the length of one command.
12316 fn key(&self) -> String {
12317 match self {
12318 Self::Origin(origin) => format!("origin {origin}"),
12319 Self::Search(also) => format!("search {also}"),
12320 }
12321 }
12322}
12323
12324/// Where one connection of [`graphql::ORIGIN_LOOKUP`] resumes.
12325enum Resumed {
12326 /// It reported another page, which starts after this cursor.
12327 More(String),
12328 /// It has ended. Sending this cursor again — the page's own end when it had one, and
12329 /// otherwise the cursor it was reached from — answers an empty page, so the one document
12330 /// can go on walking the other connection.
12331 Ended(Option<String>),
12332}
12333
12334impl Resumed {
12335 /// Whether the connection has another page.
12336 const fn has_more(&self) -> bool {
12337 matches!(self, Self::More(_))
12338 }
12339
12340 /// The cursor to send this connection next.
12341 fn cursor(self) -> Option<String> {
12342 match self {
12343 Self::More(next) => Some(next),
12344 Self::Ended(last) => last,
12345 }
12346 }
12347}
12348
12349/// Where `connection`, reached from `after`, resumes — refused when it reports another page
12350/// with no cursor to it, or from a cursor that does not advance.
12351fn resumed(connection: &Value, after: Option<&str>) -> Result<Resumed, SourceError> {
12352 let info = connection
12353 .get("pageInfo")
12354 .ok_or_else(|| SourceError::Malformed {
12355 message: "GitHub connection has no pageInfo".into(),
12356 })?;
12357 let end = optional_str(info, "endCursor")?;
12358 if required_bool(info, "hasNextPage")? {
12359 let next = end.ok_or_else(|| SourceError::Malformed {
12360 message: "GitHub connection reports another page and no endCursor".into(),
12361 })?;
12362 validate_cursor_progress(after, next)?;
12363 return Ok(Resumed::More(next.to_owned()));
12364 }
12365 Ok(Resumed::Ended(
12366 end.map(str::to_owned).or_else(|| after.map(str::to_owned)),
12367 ))
12368}
12369
12370/// The board, and every item on it this source reports.
12371#[derive(Clone)]
12372struct Board {
12373 id: String,
12374 fields: Value,
12375 items: Vec<Resolved>,
12376}
12377
12378/// What a write needs of the board and nothing more: its node id and its field
12379/// definitions, in the shape a read of the board's own `fields` gives them.
12380///
12381/// Deliberately no items. A write decides which item it writes, which parent it files
12382/// under and which far ends it names by reading each of them by its own id; this is the
12383/// half of the board those reads cannot carry, and holding no item is what keeps it from
12384/// ever being asked whether an item is there.
12385#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
12386struct BoardFields {
12387 id: BoardId,
12388 fields: Value,
12389}
12390
12391/// A board's node id: what a field write and `addProjectV2ItemById` address.
12392///
12393/// Never blank, because a blank one addresses no board — so an id GitHub answers blank is
12394/// refused where it is read, and one an item names blank is read as not named at all.
12395#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
12396#[serde(try_from = "String")]
12397struct BoardId(String);
12398
12399impl TryFrom<String> for BoardId {
12400 type Error = String;
12401
12402 /// The same refusal [`BoardId::parse`] makes, for an id read back from a kept context.
12403 fn try_from(id: String) -> Result<Self, Self::Error> {
12404 Self::parse(&id).map_err(|error| error.to_string())
12405 }
12406}
12407
12408/// Where one write left its item, for the record the rest of the command reads it out of.
12409///
12410/// A named record rather than a tuple because the update arm and the create arm each fill
12411/// all four, and two `Option`s of different meaning side by side in a tuple are two
12412/// positions a reader has to count.
12413struct Landed {
12414 /// The issue's own node id, which is the [`NativeId`] this source reports.
12415 content_id: NativeId,
12416 /// The board item's id, which is what a field write addresses — `None` for an issue its
12417 /// create filed on the board itself, whose board item GitHub adds after answering and
12418 /// [`TaskSource::finish_writes`] reads.
12419 // 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.
12420 item_id: Option<String>,
12421 /// The web address GitHub gave the issue, when it gave one.
12422 // 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.
12423 url: Option<String>,
12424 /// The issue's number on its repository, when GitHub reported one.
12425 number: Option<u64>,
12426}
12427
12428impl BoardId {
12429 fn parse(id: &str) -> Result<Self, SourceError> {
12430 if id.trim().is_empty() {
12431 return Err(SourceError::Malformed {
12432 message: "GitHub named a board with a blank node id".into(),
12433 });
12434 }
12435 Ok(Self(id.to_owned()))
12436 }
12437
12438 fn as_str(&self) -> &str {
12439 &self.0
12440 }
12441}
12442
12443impl Board {
12444 fn field<'a>(fields: &'a Value, name: &str) -> Result<Option<&'a Value>, SourceError> {
12445 complete_connection(fields, "project fields", NESTED_PAGE_SIZE)?;
12446 let nodes = fields
12447 .get("nodes")
12448 .and_then(Value::as_array)
12449 .ok_or_else(|| SourceError::Malformed {
12450 message: "GitHub project fields.nodes is not an array".into(),
12451 })?;
12452 Ok(nodes
12453 .iter()
12454 .find(|field| field.get("name").and_then(Value::as_str) == Some(name)))
12455 }
12456}
12457
12458/// One board item, resolved into everything this source reports about it.
12459///
12460/// Serializable because it is what [`TaskSource::write_context`] hands the store's cache and
12461/// [`TaskSource::prime_write`] takes back — see [`expected`].
12462#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
12463struct Resolved {
12464 item_id: String,
12465 id: NativeId,
12466 content_kind: ContentKind,
12467 kind: BoardKind,
12468 title: String,
12469 body: Option<String>,
12470 /// The body exactly as GitHub holds it, metadata slot and all, which is what a write
12471 /// that changes the slot alone has to keep byte for byte outside it.
12472 raw_body: Option<String>,
12473 status: Status,
12474 /// The name of the board `Status` option this item sits in, as the board spells it.
12475 option: Option<String>,
12476 /// What its `Priority` field says, read through this instance's mapping.
12477 priority: HeldPriority,
12478 /// Whether this item's issue is closed. A draft has no such state and is never closed.
12479 closed: bool,
12480 /// GitHub's reason for a closed issue's closure, as it spells it — `None` for an open
12481 /// issue, a draft, and an issue closed before GitHub recorded reasons.
12482 #[serde(default)]
12483 reason: Option<String>,
12484 /// The tasks this one delivers, read out of its slot. Empty for anything not a task.
12485 delivers: Vec<TaskRef>,
12486 /// Every task that delivers this one, read out of its slot. Empty for anything not a
12487 /// task.
12488 delivered_by: Vec<TaskRef>,
12489 labels: Vec<Label>,
12490 parent: Option<NativeId>,
12491 // 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.
12492 origin: Option<String>,
12493 /// What each board text field this instance projects metadata onto holds for this item,
12494 /// by field name — only the fields holding a value, an empty text included. What a write compares the
12495 /// item's metadata against, so a field already holding its value is not written again;
12496 /// never read back as metadata, which the slot alone reports.
12497 projected: BTreeMap<String, String>,
12498 /// The issue's own number on its repository, as GitHub reports it.
12499 ///
12500 /// `None` in exactly two cases: a draft, which has no number at all — `DraftIssue`
12501 /// declares none, and a draft is not filed in a repository to be numbered by one — and
12502 /// an issue this run created whose creating mutation answered without one, which is a
12503 /// response GitHub's own schema says cannot happen and which a landed write is not
12504 /// worth failing over. An `Issue` read off the board always has one.
12505 number: Option<u64>,
12506 url: Option<String>,
12507 created_at: Option<DateTime<Utc>>,
12508 updated_at: Option<DateTime<Utc>>,
12509 /// When the board item itself last changed — what a board field write moves — when the
12510 /// read that reached it said.
12511 item_updated_at: Option<DateTime<Utc>>,
12512 /// How many comments the issue holds, when the read that reached it said.
12513 comment_count: Option<u64>,
12514 /// How many relationship events the issue's timeline holds — every dependency, as
12515 /// `blockedBy` or `blocking`, and every project membership, as its parent, added or
12516 /// removed — when the read that reached it said.
12517 relationship_events: Option<u64>,
12518 /// The server's clock on the response that read it — its `Date` header — when the
12519 /// response carried one.
12520 served_at: Option<DateTime<Utc>>,
12521 /// The digest of its [`change_token::Facets`] — its own kind, its board field values and
12522 /// its labels — when the read that reached it carried all three whole.
12523 #[serde(default)]
12524 facets: Option<change_token::FacetsDigest>,
12525 own_repository: Option<Repository>,
12526 repositories: Vec<Repository>,
12527 classification: Classification,
12528 slot: BTreeMap<String, Value>,
12529 /// The node id of the board this item sits on, when the read that reached it said.
12530 board_id: Option<String>,
12531 /// The definition of every board field this item holds a value of, in the shape a read
12532 /// of the board's own `fields` gives one.
12533 ///
12534 /// Only the fields this item has a value in: a field it holds nothing of is not here,
12535 /// which says nothing about whether the board has it.
12536 fields: Vec<Value>,
12537 /// Every field the board this item sits on defines, as its own read of the board's
12538 /// `fields` gives them — when the read that reached the item carried them, which a read
12539 /// of it by its own id does. What a write of it needs of the board, then, needs no read
12540 /// of the board.
12541 board_fields: Option<Value>,
12542 /// The far ends of this issue's whole `blockedBy` connection, each as a dependency read
12543 /// selects one — when the read that reached it carried the connection to its end, which a
12544 /// read of it by its own id does for any issue blocked by no more than a page. What a
12545 /// write reconciles that relationship against, and what a read of its forward edges in
12546 /// the same command answers with.
12547 blocked_by: Option<Vec<Value>>,
12548}
12549
12550impl Resolved {
12551 /// The board this item's own read names it on, when that read named one this source can
12552 /// address.
12553 fn named_board(&self) -> Option<BoardId> {
12554 self.board_id
12555 .as_deref()
12556 .and_then(|id| BoardId::parse(id).ok())
12557 }
12558
12559 /// The board's id and every field it defines, when the read that reached this item
12560 /// carried both — which a read of it by its own id does.
12561 fn carried_board(&self) -> Option<BoardFields> {
12562 Some(BoardFields {
12563 id: self.named_board()?,
12564 fields: self.board_fields.clone()?,
12565 })
12566 }
12567
12568 /// Whether this item holds a value of the board field called `name`, and so carries
12569 /// that field's definition. `false` says nothing about whether the board has the field.
12570 fn defines(&self, name: &str) -> bool {
12571 self.fields
12572 .iter()
12573 .any(|field| field.get("name").and_then(Value::as_str) == Some(name))
12574 }
12575
12576 /// The metadata a caller sees: their own keys, plus the copy origin this source keeps
12577 /// in a field of its own, and none of the five keys that are only an encoding.
12578 ///
12579 /// The two delivery keys are left out for every kind, not only for a task: they are
12580 /// the encoding of [`Task::delivers`] and [`Task::delivered_by`], and a project or a
12581 /// document carrying one holds nothing a caller's own metadata could mean by it.
12582 fn metadata(&self) -> BTreeMap<String, Value> {
12583 let mut metadata = self.slot.clone();
12584 metadata.remove(Repository::METADATA_KEY);
12585 metadata.remove(DependencyEdge::RECORDED_KEY);
12586 metadata.remove(ItemKind::METADATA_KEY);
12587 metadata.remove(TaskRef::DELIVERS_KEY);
12588 metadata.remove(TaskRef::DELIVERED_BY_KEY);
12589 metadata.remove(Classification::METADATA_KEY);
12590 // The board field is the origin, and the body's copy of it is only a mirror for the
12591 // issue search to find: an item whose field holds none has none, whatever its body
12592 // says, so no reader ever sees two answers.
12593 metadata.remove(ORIGIN_KEY);
12594 if let Some(origin) = &self.origin {
12595 metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin.clone()));
12596 }
12597 metadata
12598 }
12599
12600 /// Where this item is, as a link a reader can open.
12601 ///
12602 /// A board is a hosted place and every issue on it has a web address, so that address
12603 /// is what "where is this?" means here — and [`Location::Url`] is what says which kind
12604 /// of place it is, so a reader knows to open it rather than to read a file out. It
12605 /// does not replace or derive from `url`: the field goes on reporting exactly what it
12606 /// reported before, and this says what that address *is*.
12607 ///
12608 /// An item GitHub gave no `url` for — a draft has none — reports no location at all
12609 /// rather than a third variant, which is the contract's "the source did not say". An
12610 /// issue this run created is not one of those: its address comes back from the
12611 /// creating mutation, so it is somewhere a reader can open from the moment it exists
12612 /// rather than from whenever the board read catches up.
12613 fn location(&self) -> Option<Location> {
12614 self.url.clone().map(Location::Url)
12615 }
12616
12617 /// The short handle this board's backend shows people for a task: the issue's number
12618 /// alone, as a decimal string.
12619 ///
12620 /// The number alone rather than `owner/repo#1043`, because that is the contract's
12621 /// value for this backend. A draft has no number and so no handle, which is the
12622 /// contract's *absent* rather than a handle of some other shape — and the native
12623 /// [`Task::id`] here is the issue's GraphQL node id, which this neither replaces nor
12624 /// derives from.
12625 fn key(&self) -> Option<String> {
12626 self.number.map(|number| number.to_string())
12627 }
12628
12629 /// This item's change token — see [`change_token`] — or `None` when the read that
12630 /// reached it did not carry all six of its parts, which a draft never does, did not say
12631 /// when GitHub served it, or found it changed too recently to have settled.
12632 fn version(&self) -> Option<String> {
12633 match self.content_kind {
12634 ContentKind::Issue => {
12635 let (issue, item) = (self.updated_at?, self.item_updated_at?);
12636 let parts = change_token::Parts {
12637 issue,
12638 item,
12639 comments: self.comment_count?,
12640 relationships: self.relationship_events?,
12641 facets: self.facets.clone()?,
12642 repository: self.own_repository.clone()?,
12643 };
12644 change_token::settled(issue, item, self.served_at?)
12645 .then(|| change_token::spell(&parts))
12646 }
12647 ContentKind::DraftIssue => None,
12648 }
12649 }
12650
12651 /// Whether its `Priority` field holds a value at all, mapped or not.
12652 fn holds_priority(&self) -> bool {
12653 self.priority != HeldPriority::Read(Priority::None)
12654 }
12655
12656 /// The task this item is.
12657 ///
12658 /// Fails for an item whose `Priority` field holds an option the mapping does not name:
12659 /// reading that as a level would be a guess, and reading it as `none` would let the next
12660 /// copy clear a priority a person set.
12661 fn task(&self) -> Result<Task, SourceError> {
12662 let priority = match &self.priority {
12663 HeldPriority::Read(priority) => *priority,
12664 HeldPriority::Unmapped(option) => {
12665 return Err(SourceError::Malformed {
12666 message: format!(
12667 "task {}{} sits in the board {PRIORITY_FIELD} option {option:?}, which \
12668 this source's priority_mapping does not name, so its priority cannot be \
12669 read; next: name {option:?} under priority_mapping, or move the item to \
12670 a mapped option",
12671 self.id,
12672 self.number
12673 .map(|number| format!(" (#{number})"))
12674 .unwrap_or_default()
12675 ),
12676 });
12677 }
12678 };
12679 Ok(Task {
12680 version: self.version(),
12681 id: self.id.clone(),
12682 key: self.key(),
12683 title: self.title.clone(),
12684 content: self.body.clone(),
12685 status: self.status.clone(),
12686 priority,
12687 labels: self.labels.clone(),
12688 project: self.parent.clone(),
12689 url: self.url.clone(),
12690 location: self.location(),
12691 created_at: self.created_at,
12692 updated_at: self.updated_at,
12693 metadata: self.metadata(),
12694 repositories: self.repositories.clone(),
12695 delivers: self.delivers.clone(),
12696 delivered_by: self.delivered_by.clone(),
12697 classification: self.classification,
12698 })
12699 }
12700
12701 fn project(&self) -> Project {
12702 Project {
12703 version: self.version(),
12704 id: self.id.clone(),
12705 title: self.title.clone(),
12706 content: self.body.clone(),
12707 status: self.status.clone(),
12708 labels: self.labels.clone(),
12709 url: self.url.clone(),
12710 location: self.location(),
12711 created_at: self.created_at,
12712 updated_at: self.updated_at,
12713 metadata: self.metadata(),
12714 repositories: self.repositories.clone(),
12715 classification: self.classification,
12716 }
12717 }
12718
12719 /// The same issue as a document: the project it is filed under, and no status and no
12720 /// dependencies, because a document is not work.
12721 fn document(&self) -> Document {
12722 Document {
12723 version: self.version(),
12724 id: self.id.clone(),
12725 title: self.title.clone(),
12726 content: self.body.clone(),
12727 project: self.parent.clone(),
12728 labels: self.labels.clone(),
12729 url: self.url.clone(),
12730 location: self.location(),
12731 created_at: self.created_at,
12732 updated_at: self.updated_at,
12733 metadata: self.metadata(),
12734 repositories: self.repositories.clone(),
12735 classification: self.classification,
12736 }
12737 }
12738}
12739
12740/// Where one targeted update moves an item's status, and which of its two halves move.
12741struct StatusMove {
12742 /// The board the item's `Status` field is on.
12743 board: BoardId,
12744 /// The `Status` field's id.
12745 field: String,
12746 /// The option's id.
12747 option: String,
12748 /// The option's name, as the board spells it.
12749 name: String,
12750 /// What the status asks of the issue's state.
12751 target: StatusTarget,
12752 /// The status the item reads as once it is there.
12753 landed: Status,
12754 /// Which of the status's two halves differ from what the item holds.
12755 moves: Moves,
12756}
12757
12758/// Which halves of an item's status one targeted update moves: its `Status` option, the open or
12759/// closed state of its issue, or both. A status neither half of which differs is no move at all,
12760/// and is not a value of this type.
12761#[derive(Clone, Copy, PartialEq, Eq)]
12762enum Moves {
12763 /// The option alone.
12764 Option,
12765 /// The issue's state alone: open, closed, or closed with another reason.
12766 State,
12767 /// Both.
12768 Both,
12769}
12770
12771impl Moves {
12772 /// What differs, or `None` when nothing does.
12773 const fn of(option: bool, state: bool) -> Option<Self> {
12774 match (option, state) {
12775 (true, true) => Some(Self::Both),
12776 (true, false) => Some(Self::Option),
12777 (false, true) => Some(Self::State),
12778 (false, false) => None,
12779 }
12780 }
12781
12782 /// Whether the option moves.
12783 const fn option(self) -> bool {
12784 matches!(self, Self::Option | Self::Both)
12785 }
12786
12787 /// Whether the issue's state moves.
12788 const fn state(self) -> bool {
12789 matches!(self, Self::State | Self::Both)
12790 }
12791}
12792
12793/// What one write is, and the status that comes with being it.
12794///
12795/// One value rather than a [`BoardKind`] beside an `Option<Status>`: a document has no
12796/// status and a task or a project always has one, so "a document carrying a status" and
12797/// "a task carrying none" are states a write cannot be in rather than states every use
12798/// site below has to defend against.
12799enum Written<'a> {
12800 /// A document, which is not work and so has no status at all.
12801 Document,
12802 /// A task or a project, and the status it is being written with.
12803 Work(ItemKind, &'a Status),
12804}
12805
12806impl Written<'_> {
12807 /// Which of the board's three kinds this write is.
12808 const fn kind(&self) -> BoardKind {
12809 match self {
12810 Self::Document => BoardKind::Document,
12811 Self::Work(kind, _) => BoardKind::Work(*kind),
12812 }
12813 }
12814
12815 /// The status this write carries. A document carries none, so a write of one says
12816 /// nothing about the issue's open or closed state and selects no board `Status`
12817 /// option.
12818 const fn status(&self) -> Option<&Status> {
12819 match self {
12820 Self::Document => None,
12821 Self::Work(_, status) => Some(status),
12822 }
12823 }
12824
12825 /// The status this write carries with the kind whose half of `status_mapping` it is
12826 /// written through.
12827 const fn work_status(&self) -> Option<(ItemKind, &Status)> {
12828 match self {
12829 Self::Document => None,
12830 Self::Work(kind, status) => Some((*kind, status)),
12831 }
12832 }
12833}
12834
12835/// The item being written, in the one shape all three write methods reach.
12836struct Incoming<'a> {
12837 written: Written<'a>,
12838 /// The title a person wrote. A document's goes onto the issue with
12839 /// [`DESIGN_TITLE_PREFIX`] put back, so a round trip returns the title that went in.
12840 title: &'a str,
12841 content: Option<&'a str>,
12842 assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
12843 labels: &'a [Label],
12844 metadata: &'a BTreeMap<String, Value>,
12845 repositories: &'a [Repository],
12846 /// Recorded in the slot while private, and nowhere while public.
12847 classification: Classification,
12848 parent: Option<&'a NativeId>,
12849 /// [`Task::delivers`], already checked. Empty for a project or a document, which is
12850 /// what keeps either key out of their slot.
12851 delivers: &'a [TaskRef],
12852 /// [`Task::delivered_by`], already checked. Empty for a project or a document.
12853 delivered_by: &'a [TaskRef],
12854 /// [`Task::priority`], for a task written to an instance that holds one; `None` for a
12855 /// project, a document, and every write to an instance with no `priority_mapping` —
12856 /// which is what keeps such a write's requests exactly what they were before.
12857 priority: Option<Priority>,
12858}
12859
12860/// What one write does to one projected metadata text field, by the field's id.
12861#[derive(Debug, Clone)]
12862enum ProjectionWrite {
12863 /// Write this text.
12864 Set {
12865 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.
12866 text: String,
12867 },
12868 /// Clear the field's value.
12869 Clear { field: String }, // llmlint: ignore[invalid_states_unrepresentable] As above.
12870}
12871
12872impl ProjectionWrite {
12873 /// Add this write to a field write's list of values and list of clears.
12874 fn join<'a>(&'a self, values: &mut Vec<(String, Value)>, clears: &mut Vec<&'a str>) {
12875 match self {
12876 Self::Set { field, text } => values.push((field.clone(), json!({"text": text}))),
12877 Self::Clear { field } => clears.push(field),
12878 }
12879 }
12880}
12881
12882/// What one write does to an item's `Priority` field.
12883enum PriorityWrite {
12884 /// Select this option of this field.
12885 Select {
12886 /// The `Priority` field's id.
12887 field: String,
12888 /// The mapped option's id.
12889 option: String,
12890 },
12891 /// Clear the field's value, which is what `none` is.
12892 Clear {
12893 /// The `Priority` field's id.
12894 field: String,
12895 },
12896}
12897
12898impl Incoming<'_> {
12899 /// The title this write puts on the issue.
12900 fn written_title(&self) -> String {
12901 match self.written {
12902 Written::Document => format!("{DESIGN_TITLE_PREFIX}{}", self.title),
12903 Written::Work(..) => self.title.to_owned(),
12904 }
12905 }
12906}
12907
12908#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
12909enum ContentKind {
12910 DraftIssue,
12911 Issue,
12912}
12913
12914/// What one board issue is: a document, or the work an [`ItemKind`] names.
12915///
12916/// A type of this source's own rather than an `ItemKind` with a third variant, because
12917/// `ItemKind` names what a dependency endpoint points at and nothing may point at a
12918/// document — the contract keeps a document out of that enum deliberately. Holding the
12919/// board's three answers in one value is what makes every place that asks "which is this?"
12920/// answer all three, rather than a `document: bool` beside a `kind` that means nothing for
12921/// two thirds of the board.
12922#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
12923enum BoardKind {
12924 /// An issue whose title begins [`DESIGN_TITLE_PREFIX`].
12925 Document,
12926 /// Every other issue, and every draft.
12927 Work(ItemKind),
12928}
12929
12930impl BoardKind {
12931 /// The kind of an item titled `title`, filed under a project or not, holding `sub_issues`
12932 /// sub-issues and carrying the kind marker `marked` in its slot.
12933 ///
12934 /// The design prefix is read *first*, before either of the two rules that separate a
12935 /// project from a task. A document is not work whatever sub-issues it has and whatever
12936 /// marker it carries, and reading the prefix later would make a design issue with none of
12937 /// either an empty project. Being a sub-issue wins outright over the rest, and no marker
12938 /// overrides it: an issue filed under a project is that project's task even when it has
12939 /// sub-issues of its own.
12940 fn of(title: &str, filed: bool, sub_issues: u64, marked: Option<ItemKind>) -> Self {
12941 if title.starts_with(DESIGN_TITLE_PREFIX) {
12942 Self::Document
12943 } else if filed {
12944 Self::Work(ItemKind::Task)
12945 } else if sub_issues > 0 || marked == Some(ItemKind::Project) {
12946 Self::Work(ItemKind::Project)
12947 } else {
12948 Self::Work(ItemKind::Task)
12949 }
12950 }
12951
12952 /// What the change token's facets call this kind.
12953 const fn facet(self) -> change_token::FacetKind {
12954 match self {
12955 Self::Document => change_token::FacetKind::Document,
12956 Self::Work(ItemKind::Project) => change_token::FacetKind::Project,
12957 Self::Work(ItemKind::Task) => change_token::FacetKind::Task,
12958 }
12959 }
12960
12961 /// Whose half of `status_mapping` an item of this kind reads its status through. A
12962 /// document has no status of its own, so the task half stands in for whatever the issue
12963 /// holds; nothing reports it.
12964 const fn status_kind(self) -> ItemKind {
12965 match self {
12966 Self::Document => ItemKind::Task,
12967 Self::Work(kind) => kind,
12968 }
12969 }
12970
12971 /// How a refusal names this kind to the person reading it.
12972 const fn describes(self) -> &'static str {
12973 match self {
12974 Self::Document => "document",
12975 Self::Work(kind) => kind.marker(),
12976 }
12977 }
12978}
12979
12980/// Whether `labels` satisfies `filter`, matching by name, case-insensitively.
12981///
12982/// This is the local Markdown source's `labels_match`, spelled the same way on purpose:
12983/// the shared cross-source journeys assert one answer to one question, so two sources
12984/// that disagree about what "carries the label bug" means fail them.
12985fn labels_match(labels: &[Label], filter: &LabelFilter) -> bool {
12986 let holds = |name: &String| {
12987 labels
12988 .iter()
12989 .any(|label| label.name.eq_ignore_ascii_case(name))
12990 };
12991 (filter.any_of.is_empty() || filter.any_of.iter().any(holds))
12992 && filter.all_of.iter().all(holds)
12993 && !filter.none_of.iter().any(holds)
12994}
12995
12996/// Whether `category` is one of `statuses`. An empty list is unfiltered rather than
12997/// "keeps nothing", which is what lets a `Vec<StatusCategory>` spell no filter at all.
12998fn status_matches(category: StatusCategory, statuses: &[StatusCategory]) -> bool {
12999 statuses.is_empty() || statuses.contains(&category)
13000}
13001
13002/// Whether `title`/`content` satisfies `query`, matching case-insensitively.
13003///
13004/// `content` is the item's own prose — the body with this source's trailing metadata
13005/// comment already taken off — so a search never matches an encoding the author of the
13006/// issue never wrote.
13007fn text_matches(title: &str, content: Option<&str>, query: &TextQuery) -> bool {
13008 let terms = query.terms.to_lowercase();
13009 let in_title = title.to_lowercase().contains(&terms);
13010 let in_content = content.is_some_and(|body| body.to_lowercase().contains(&terms));
13011 match query.fields {
13012 TextFields::Title => in_title,
13013 TextFields::Content => in_content,
13014 TextFields::TitleOrContent => in_title || in_content,
13015 }
13016}
13017
13018/// Whether `task` satisfies `query`, with `project` deciding the project predicate.
13019///
13020/// The project predicate is passed separately because a read narrowed to one project has
13021/// already answered it by asking *that project* for its own items — and re-applying it
13022/// there would compare the caller's selector, which may be a project's **name**, against
13023/// the id of the project that name resolved to, and keep nothing. Every other read passes
13024/// `query.project` and applies it here, which is what keeps `projects` a predicate this
13025/// source really does apply.
13026fn task_matches(task: &Task, query: &TaskQuery, project: &ProjectFilter) -> bool {
13027 labels_match(&task.labels, &query.labels)
13028 && status_matches(task.status.category, &query.statuses)
13029 && (query.priorities.is_empty() || query.priorities.contains(&task.priority))
13030 && match project {
13031 ProjectFilter::Any => true,
13032 ProjectFilter::Orphans => task.project.is_none(),
13033 ProjectFilter::Is(id) => task.project.as_ref() == Some(id),
13034 }
13035 && query
13036 .text
13037 .as_ref()
13038 .is_none_or(|text| text_matches(&task.title, task.content.as_deref(), text))
13039 // Against the parsed metadata slot, and against the origin field, which is where
13040 // `Resolved::metadata` reads each of them from.
13041 && query.metadata_matches(&task.metadata)
13042 && query.origin_matches(&task.metadata)
13043}
13044
13045fn project_matches(project: &Project, query: &ProjectQuery) -> bool {
13046 labels_match(&project.labels, &query.labels)
13047 && status_matches(project.status.category, &query.statuses)
13048 && query
13049 .text
13050 .as_ref()
13051 .is_none_or(|text| text_matches(&project.title, project.content.as_deref(), text))
13052}
13053
13054/// The same three predicates a task query carries, minus the status filter.
13055///
13056/// A document is not work, so it has no status for one to compare against and the query
13057/// type carries none. The project predicate is the same one — a design issue filed under a
13058/// project issue is in that project, and one filed under nothing is in none — so it is
13059/// spelled the same way here rather than answered differently.
13060fn document_matches(document: &Document, query: &DocumentQuery, project: &ProjectFilter) -> bool {
13061 labels_match(&document.labels, &query.labels)
13062 && match project {
13063 ProjectFilter::Any => true,
13064 ProjectFilter::Orphans => document.project.is_none(),
13065 ProjectFilter::Is(id) => document.project.as_ref() == Some(id),
13066 }
13067 && query
13068 .text
13069 .as_ref()
13070 .is_none_or(|text| text_matches(&document.title, document.content.as_deref(), text))
13071}
13072
13073/// Why a GitHub item's change token does not vouch for its forward edges.
13074///
13075/// A forward edge names its far end and that far end's kind, and the near item's token moves
13076/// with the edges it holds but says nothing of the far ends themselves: a far end turning from a
13077/// task into a project when it gains a sub-issue, retitled into a document, or deleted moves
13078/// nothing the near token reads, so an edge kept at the near version could name a far end that
13079/// is no longer what it said.
13080const FAR_ENDS_UNCOVERED: &str = "a github item's change token does not attest a far end's kind, \
13081 whether it became a document, or its deletion, so its forward edges are always read from \
13082 github";
13083
13084/// The issue-search qualifier that keeps open issues alone, which a status filter naming only
13085/// categories an open issue reads as narrows a board-scoped search with.
13086const OPEN_QUALIFIER: &str = "is:open";
13087
13088/// The board filter `task status reconcile` reads the board through: closed items alone.
13089const CLOSED_FILTER: &str = "is:closed";
13090
13091/// The variables of one [`graphql::ISSUES_BY_REFERENCE`] request for `batch`: each slot names
13092/// one reference, and a slot the batch has none for names its last one again.
13093fn reference_batch(batch: &[IssueRef]) -> Value {
13094 let mut variables = serde_json::Map::new();
13095 for slot in 0..REFERENCE_BATCH {
13096 let issue = batch.get(slot).or(batch.last());
13097 variables.insert(format!("owner{slot}"), json!(issue.map(|i| &i.owner)));
13098 variables.insert(format!("name{slot}"), json!(issue.map(|i| &i.name)));
13099 variables.insert(format!("number{slot}"), json!(issue.map(|i| i.number)));
13100 }
13101 variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
13102 variables.insert("statusField".to_owned(), json!(STATUS_FIELD));
13103 variables.insert("duplicates".to_owned(), json!(true));
13104 Value::Object(variables)
13105}
13106
13107/// One issue an issue read by reference answered, in the shape every other read of an issue
13108/// hands its resolver: each board membership's `Status` value, read by name, as the one field
13109/// value that membership carries.
13110///
13111/// A membership's `status` is `null` when its card sits at no option — or on a board with no
13112/// field of that name — and otherwise an option value naming its option; anything else is not
13113/// an answer to this read, and is refused rather than read as a card at no option.
13114fn held_issue(issue: &Value) -> Result<Value, SourceError> {
13115 let malformed = |what: &str| SourceError::Malformed {
13116 message: format!("GitHub answered an issue read by reference with {what}"),
13117 };
13118 let mut issue = issue.clone();
13119 let nodes = issue
13120 .pointer_mut("/projectItems/nodes")
13121 .and_then(Value::as_array_mut)
13122 .ok_or_else(|| malformed("no list of board memberships"))?;
13123 for node in nodes {
13124 let node = node
13125 .as_object_mut()
13126 .ok_or_else(|| malformed("a board membership that is not an object"))?;
13127 let status = match node.get("status") {
13128 None => return Err(malformed("a board membership with no status member")),
13129 Some(Value::Null) => None,
13130 Some(status) if status.get("name").is_some_and(Value::is_string) => {
13131 Some(status.clone())
13132 }
13133 // A single-select value of another field type answers `{}`; only an option value
13134 // carries a name.
13135 Some(status) if status.as_object().is_some_and(serde_json::Map::is_empty) => None,
13136 Some(_) => return Err(malformed("a membership status that names no option")),
13137 };
13138 node.insert(
13139 "fieldValues".to_owned(),
13140 json!({"nodes": status.into_iter().collect::<Vec<_>>(),
13141 "pageInfo": {"hasNextPage": false}}),
13142 );
13143 }
13144 Ok(issue)
13145}
13146
13147/// The issue one slot of a read by reference answered, `None` when GitHub answered that
13148/// slot's repository or issue `null` — which [`GitHubProjectsSource::answer_unresolved`]
13149/// admitted only beside a `NOT_FOUND` naming that very slot — and a refusal when the answer
13150/// leaves the slot, or its `issue` member, out: an answer missing a member is malformed rather
13151/// than a reference nothing holds.
13152fn referenced_issue(data: &Value, slot: usize) -> Result<Option<&Value>, SourceError> {
13153 let alias = format!("r{slot}");
13154 let repository = data.get(&alias).ok_or_else(|| SourceError::Malformed {
13155 message: format!("GitHub answered a read by reference with no {alias}"),
13156 })?;
13157 if repository.is_null() {
13158 return Ok(None);
13159 }
13160 let issue = repository
13161 .get("issue")
13162 .ok_or_else(|| SourceError::Malformed {
13163 message: format!("GitHub answered {alias} of a read by reference with no issue"),
13164 })?;
13165 if issue.is_null() {
13166 return Ok(None);
13167 }
13168 // `Repository.issue` is typed `Issue`, so anything else is not an answer to this read.
13169 if optional_str(issue, "__typename")? != Some("Issue") {
13170 return Err(SourceError::Malformed {
13171 message: format!(
13172 "GitHub answered {alias} of a read by reference with an issue whose __typename \
13173 is not Issue"
13174 ),
13175 });
13176 }
13177 Ok(Some(issue))
13178}
13179
13180/// A closure reason as a person reads it: `NOT_PLANNED` as `not planned`.
13181fn spoken_reason(reason: &str) -> String {
13182 reason.to_ascii_lowercase().replace('_', " ")
13183}
13184
13185impl GitHubProjectsSource {
13186 /// Every task and project of this board whose card contradicts its issue's closure, each
13187 /// judged against its own kind's half of `status_mapping`, read through the board's own
13188 /// `is:closed` filter, page by page — never the board's unfiltered `items`.
13189 ///
13190 /// Each item the filter answers is confirmed from its own node: an open one, one whose
13191 /// closure reads `unknown`, and one whose card is already in line are left out whatever
13192 /// the filter let through. The board's fields ride along and are held for the option
13193 /// writes that follow, so those need no read of the board.
13194 async fn contradictions(&self) -> Result<Vec<StatusContradiction>, SourceError> {
13195 let mut after: Option<String> = None;
13196 let mut found = Vec::new();
13197 loop {
13198 let data = self
13199 .graphql(
13200 graphql::CLOSED_ITEMS,
13201 json!({"owner":self.owner,"number":self.project_number,
13202 "first":MAX_PAGE_SIZE,"after":after,"filter":CLOSED_FILTER,
13203 "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
13204 )
13205 .await?;
13206 let board = data
13207 .pointer("/closedItems/projectV2")
13208 .filter(|value| !value.is_null())
13209 .ok_or_else(|| SourceError::Refused {
13210 message: format!(
13211 "GitHub project {}/{} was not found or is not visible to the token",
13212 self.owner, self.project_number
13213 ),
13214 })?;
13215 *self.fields_cache()? = Some(BoardFields {
13216 id: BoardId::parse(required_str(board, "id")?)?,
13217 fields: board.get("fields").cloned().unwrap_or(Value::Null),
13218 });
13219 let items = board.get("items").ok_or_else(|| SourceError::Malformed {
13220 message: "GitHub project closed items are missing".into(),
13221 })?;
13222 for item in items
13223 .get("nodes")
13224 .and_then(Value::as_array)
13225 .ok_or_else(|| SourceError::Malformed {
13226 message: "GitHub project items.nodes is not an array".into(),
13227 })?
13228 {
13229 let Some(resolved) = self.resolve(item)? else {
13230 continue;
13231 };
13232 let BoardKind::Work(kind) = resolved.kind else {
13233 continue;
13234 };
13235 let Some((closed_as, name)) = self.statuses.contradicted(
13236 kind,
13237 resolved.option.as_deref(),
13238 resolved.closed,
13239 resolved.reason.as_deref(),
13240 ) else {
13241 continue;
13242 };
13243 found.push(StatusContradiction {
13244 closed_as,
13245 to: name.to_owned(),
13246 option: resolved.option.clone(),
13247 item: match kind {
13248 ItemKind::Task => ContradictedItem::Task(resolved.task()?),
13249 ItemKind::Project => ContradictedItem::Project(resolved.project()),
13250 },
13251 });
13252 }
13253 let info = items
13254 .get("pageInfo")
13255 .ok_or_else(|| SourceError::Malformed {
13256 message: "GitHub project items have no pageInfo".into(),
13257 })?;
13258 if !required_bool(info, "hasNextPage")? {
13259 return Ok(found);
13260 }
13261 let next = required_str(info, "endCursor")?;
13262 validate_cursor_progress(after.as_deref(), next)?;
13263 after = Some(next.to_owned());
13264 }
13265 }
13266
13267 /// What this board holds for each issue in `issues`, once each in the order given, read
13268 /// [`REFERENCE_BATCH`] references at a time — and nothing else of the board.
13269 ///
13270 /// An issue GitHub cannot find for this source's credential, and one with no item on this
13271 /// board, are both absent. A closure that already says the work will not be done here —
13272 /// not planned, duplicate — is skipped: a landing does not overrule it. An item closed as
13273 /// completed whose card sits at its kind's `done` option is unchanged, and every other item
13274 /// is due a `done` write. A project item — a plan's home card, its issue holding the plan's
13275 /// tasks as sub-issues — is held under exactly these rules, judged against the project
13276 /// kind's half of `status_mapping`.
13277 async fn holdings(&self, issues: &[IssueRef]) -> Result<Vec<IssueHolding>, SourceError> {
13278 let mut distinct: Vec<IssueRef> = Vec::with_capacity(issues.len());
13279 for issue in issues {
13280 if !distinct.contains(issue) {
13281 distinct.push(issue.clone());
13282 }
13283 }
13284 let done = |kind: ItemKind| {
13285 self.statuses
13286 .target(kind, StatusCategory::Done)
13287 .option()
13288 .unwrap_or("Done")
13289 .to_owned()
13290 };
13291 // GitHub numbers an issue with a GraphQL `Int`, a signed 32-bit value, so a reference past
13292 // its range names no issue any repository can hold: it is answered absent and never sent,
13293 // where binding it would have GitHub refuse the whole batch.
13294 let readable: Vec<IssueRef> = distinct
13295 .iter()
13296 .filter(|issue| i32::try_from(issue.number).is_ok())
13297 .cloned()
13298 .collect();
13299 let mut holdings = Vec::with_capacity(readable.len());
13300 for batch in readable.chunks(REFERENCE_BATCH) {
13301 let data = self
13302 .graphql(graphql::ISSUES_BY_REFERENCE, reference_batch(batch))
13303 .await?;
13304 for (slot, issue) in batch.iter().enumerate() {
13305 let node = referenced_issue(&data, slot)?;
13306 let resolved = match node {
13307 Some(node) => self.resolve_issue_as(&held_issue(node)?, false).await?,
13308 None => None,
13309 };
13310 // A task and a project are both held; a document is not work, so nothing is.
13311 let Some((item, kind)) = resolved.and_then(|item| match item.kind {
13312 BoardKind::Work(kind) => Some((item, kind)),
13313 BoardKind::Document => None,
13314 }) else {
13315 holdings.push(IssueHolding {
13316 issue: issue.clone(),
13317 completion: Completion::Absent,
13318 });
13319 continue;
13320 };
13321 let task = match kind {
13322 ItemKind::Task => {
13323 let held = item.task()?;
13324 HeldTask {
13325 id: held.id,
13326 kind,
13327 option: item.option.clone(),
13328 classification: held.classification,
13329 repositories: held.repositories,
13330 delivers: held.delivers,
13331 }
13332 }
13333 // A project delivers no task, so what its write keeps in step is nothing.
13334 ItemKind::Project => {
13335 let held = item.project();
13336 HeldTask {
13337 id: held.id,
13338 kind,
13339 option: item.option.clone(),
13340 classification: held.classification,
13341 repositories: held.repositories,
13342 delivers: Vec::new(),
13343 }
13344 }
13345 };
13346 let done = done(kind);
13347 let completion = match (item.closed, item.reason.as_deref()) {
13348 (true, Some(reason @ ("NOT_PLANNED" | "DUPLICATE"))) => Completion::Skipped {
13349 task,
13350 reason: format!("closed as {}", spoken_reason(reason)),
13351 },
13352 (true, None | Some("COMPLETED"))
13353 if item
13354 .option
13355 .as_deref()
13356 .is_some_and(|option| option.eq_ignore_ascii_case(&done)) =>
13357 {
13358 Completion::Unchanged { task }
13359 }
13360 _ => {
13361 let kept = item.clone();
13362 self.held_cache
13363 .lock()
13364 .map_err(|_| SourceError::Unavailable {
13365 message: "items read by issue reference were left \
13366 inconsistent; run the command again"
13367 .into(),
13368 })?
13369 .insert(kept.id.clone(), kept);
13370 Completion::Due {
13371 task,
13372 to: done.clone(),
13373 }
13374 }
13375 };
13376 holdings.push(IssueHolding {
13377 issue: issue.clone(),
13378 completion,
13379 });
13380 }
13381 }
13382 let mut read = holdings.into_iter();
13383 let mut answered = Vec::with_capacity(distinct.len());
13384 for issue in distinct {
13385 if i32::try_from(issue.number).is_ok() {
13386 answered.push(read.next().ok_or_else(|| SourceError::Malformed {
13387 message: format!("no holding was read for {issue}"),
13388 })?);
13389 } else {
13390 answered.push(IssueHolding {
13391 issue,
13392 completion: Completion::Absent,
13393 });
13394 }
13395 }
13396 Ok(answered)
13397 }
13398}
13399
13400#[async_trait::async_trait]
13401impl TaskSource for GitHubProjectsSource {
13402 fn kind(&self) -> &'static str {
13403 KIND
13404 }
13405 fn attach_body_markup(&self, markup: onetaskgraph_plugin_api::SharedBodyMarkup) {
13406 let _ = self.body_markup.set(markup);
13407 }
13408 fn order_versions(&self, a: &str, b: &str) -> Option<std::cmp::Ordering> {
13409 change_token::order(a, b)
13410 }
13411 /// A task's fields and comments: §4.13d of `docs/plugin-protocol.md` states, member by
13412 /// member, the change each part of the token moves for. Not its forward edges — see
13413 /// [`FAR_ENDS_UNCOVERED`].
13414 fn version_coverage(&self) -> VersionCoverage {
13415 VersionCoverage {
13416 forward_edges: Coverage::Uncovered {
13417 reason: FAR_ENDS_UNCOVERED.to_owned(),
13418 },
13419 ..VersionCoverage::whole()
13420 }
13421 }
13422 /// Every part, for a copy's skip: the relationship count moves for an edge added or removed
13423 /// and for a far end deleted, so the token sees every change to a task's edges but one — a
13424 /// far end's own kind flipping, by gaining a sub-issue or being retitled into a document,
13425 /// which `spike-coverage` measured stale 1 of 1. A skipping recopy does not carry that, and
13426 /// the next copy that reads the task does (`docs/plugin-protocol.md` §4.13d).
13427 // 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.
13428 fn copy_version_coverage(&self) -> VersionCoverage {
13429 VersionCoverage::whole()
13430 }
13431 /// A project's fields, and not its forward edges, for the same reason a task's are not.
13432 fn project_version_coverage(&self) -> ProjectVersionCoverage {
13433 ProjectVersionCoverage {
13434 fields: Coverage::Covered,
13435 forward_edges: Coverage::Uncovered {
13436 reason: FAR_ENDS_UNCOVERED.to_owned(),
13437 },
13438 }
13439 }
13440 /// A document's fields and the manifest of its image assets, which its metadata slot
13441 /// records: both are the issue's body, whose every edit moves the token's issue time.
13442 fn document_version_coverage(&self) -> DocumentVersionCoverage {
13443 DocumentVersionCoverage::whole()
13444 }
13445 fn write_context(&self, id: &NativeId) -> Option<Value> {
13446 self.context_of(id)
13447 }
13448 fn board_context(&self) -> Option<Value> {
13449 self.board_context_held()
13450 }
13451 fn prime_write(&self, id: &NativeId, context: &Value, board: Option<&Value>) -> bool {
13452 self.prime(id, context, board)
13453 }
13454 fn unprime(&self, id: &NativeId) {
13455 self.unprime_held(id);
13456 }
13457 fn placement_context(&self) -> Option<Value> {
13458 self.placement_held()
13459 }
13460 fn prime_placement(&self, context: &Value) -> bool {
13461 self.prime_placement_held(context)
13462 }
13463 fn written_context(&self, id: &NativeId) -> Option<Value> {
13464 self.written_held(id)
13465 }
13466 fn prime_written(&self, id: &NativeId, context: &Value, task: &Task) -> bool {
13467 self.prime_written_held(id, context, task)
13468 }
13469 fn begin_journal(&self) {
13470 self.expected.begin();
13471 }
13472 fn end_journal(&self) -> Option<WriteJournal> {
13473 Some(self.expected.end())
13474 }
13475 fn capabilities(&self) -> Capabilities {
13476 Capabilities {
13477 projects: Support::Native,
13478 documents: Support::Native,
13479 comments: Support::Native,
13480 assets: Support::Native,
13481 priority: if self.priorities.is_some() {
13482 Support::Native
13483 } else {
13484 Support::Unsupported
13485 },
13486 filter_by_priority: Support::Native,
13487 filter_by_comment_activity: Support::Native,
13488 filter_by_metadata: Support::Native,
13489 filter_by_origin: Support::Native,
13490 reconcile_status: Support::Native,
13491 complete_by_issue: Support::Native,
13492 orphan_tasks: Support::Native,
13493 filter_by_label: Support::Native,
13494 filter_by_status: Support::Native,
13495 search_title: Support::Native,
13496 search_content: Support::Native,
13497 task_dependencies: DependencySupport::BothDirections,
13498 project_dependencies: DependencySupport::BothDirections,
13499 max_page_size: MAX_PAGE_SIZE,
13500 }
13501 }
13502 async fn visibility(
13503 &self,
13504 target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13505 ) -> Result<onetaskgraph_plugin_api::Visibility, SourceError> {
13506 self.write_visibility(target).await
13507 }
13508 async fn visibility_with_project(
13509 &self,
13510 target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13511 project: &onetaskgraph_plugin_api::WriteTarget<'_>,
13512 ) -> Result<onetaskgraph_plugin_api::Visibility, SourceError> {
13513 self.write_visibility_with_project(target, project).await
13514 }
13515 fn declare_visibility(&self, declared: onetaskgraph_plugin_api::Visibility) {
13516 self.declared_private.store(
13517 declared == onetaskgraph_plugin_api::Visibility::Private,
13518 std::sync::atomic::Ordering::SeqCst,
13519 );
13520 }
13521 fn scope_visibility<'a>(
13522 &'a self,
13523 operation: std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send + 'a>>,
13524 ) -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send + 'a>> {
13525 self.scope_visibility_reads(operation)
13526 }
13527
13528 /// Where a create of `target` would file its issue, by [`Self::creation_rule`] — the rule
13529 /// the create applies — reading the parent project's issue only when `target` names one.
13530 async fn creation_repository(
13531 &self,
13532 target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13533 ) -> Result<Option<Repository>, SourceError> {
13534 let onetaskgraph_plugin_api::WriteTarget::New {
13535 repositories,
13536 project,
13537 } = target
13538 else {
13539 return Ok(None);
13540 };
13541 let target = self
13542 .creation_rule(repositories, *project, "a new item")
13543 .await?;
13544 Repository::try_from(target.origin())
13545 .map(Some)
13546 .map_err(|message| SourceError::Malformed { message })
13547 }
13548 /// Every issue of the repository a create of `target` files in whose body carries `key` in
13549 /// its metadata slot, read off that repository's issues newest first back to `since` —
13550 /// never through search, whose index lags a fresh issue by seconds.
13551 async fn created_with_key(
13552 &self,
13553 record: onetaskgraph_plugin_api::MetadataRecord,
13554 key: &str,
13555 since: Option<DateTime<Utc>>,
13556 target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13557 ) -> Result<Vec<NativeId>, SourceError> {
13558 let _ = record;
13559 let onetaskgraph_plugin_api::WriteTarget::New {
13560 repositories,
13561 project,
13562 } = target
13563 else {
13564 return Ok(Vec::new());
13565 };
13566 let repository = self
13567 .creation_rule(repositories, *project, "a new item")
13568 .await?;
13569 let wanted = Value::String(key.to_owned());
13570 let mut found = Vec::new();
13571 let mut after: Option<String> = None;
13572 let mut followed = std::collections::HashSet::new();
13573 loop {
13574 let data = self
13575 .graphql(
13576 graphql::ISSUES_BY_CREATION,
13577 json!({"owner": repository.owner, "name": repository.name,
13578 "first": MAX_PAGE_SIZE, "after": after}),
13579 )
13580 .await?;
13581 let issues =
13582 data.pointer("/repository/issues")
13583 .ok_or_else(|| SourceError::Malformed {
13584 message: format!(
13585 "GitHub answered no issues for repository {}",
13586 repository.slug()
13587 ),
13588 })?;
13589 let malformed = |what: &str| SourceError::Malformed {
13590 message: format!(
13591 "GitHub answered a page of repository {}'s issues {what}",
13592 repository.slug()
13593 ),
13594 };
13595 let nodes = issues
13596 .get("nodes")
13597 .and_then(Value::as_array)
13598 .ok_or_else(|| malformed("with no list of issues"))?;
13599 let mut older = false;
13600 for node in nodes {
13601 let id = node
13602 .get("id")
13603 .and_then(Value::as_str)
13604 .filter(|id| !id.is_empty())
13605 .ok_or_else(|| malformed("holding an issue with no id"))?;
13606 let created = optional_time(node, "createdAt")?
13607 .ok_or_else(|| malformed("holding an issue with no creation time"))?;
13608 if since.is_some_and(|since| created < since) {
13609 older = true;
13610 break;
13611 }
13612 let body = match node.get("body") {
13613 Some(Value::String(body)) => Some(body.clone()),
13614 Some(Value::Null) => None,
13615 _ => return Err(malformed("holding an issue with no body member")),
13616 };
13617 let (_, metadata) = metadata_body(body)?;
13618 if metadata.get(MetadataKey::CREATE_KEY) == Some(&wanted) {
13619 found.push(NativeId(id.to_owned()));
13620 }
13621 }
13622 let next = issues
13623 .pointer("/pageInfo/hasNextPage")
13624 .and_then(Value::as_bool)
13625 .ok_or_else(|| malformed("that does not say whether another follows"))?;
13626 if older || !next {
13627 return Ok(found);
13628 }
13629 // A page that says another follows names where it starts, and never a page already
13630 // read: anything else would ask for the same pages again for ever.
13631 let cursor = issues
13632 .pointer("/pageInfo/endCursor")
13633 .and_then(Value::as_str)
13634 .filter(|cursor| !cursor.is_empty())
13635 .map(str::to_owned);
13636 if cursor
13637 .as_ref()
13638 .is_none_or(|cursor| !followed.insert(cursor.clone()))
13639 {
13640 return Err(SourceError::Malformed {
13641 message: format!(
13642 "GitHub answered a page of repository {}'s issues that says another \
13643 follows without a new cursor to it",
13644 repository.slug()
13645 ),
13646 });
13647 }
13648 after = cursor;
13649 }
13650 }
13651
13652 /// `Repository.issues` lists an issue GitHub committed only once its listing catches up.
13653 fn create_key_lags(&self) -> bool {
13654 true
13655 }
13656
13657 /// The task as this process's own latest write of it left it, from the item it recorded
13658 /// that write as — never a request. Its `version` is always `None`: a board's change token
13659 /// is given only once an item has gone unchanged for [`change_token::SETTLED`], which an
13660 /// item this command has just written has not, so no write's own answer can vouch for one.
13661 async fn written_task(&self, id: &NativeId) -> Option<Task> {
13662 let held = self
13663 .created()
13664 .ok()?
13665 .iter()
13666 .find(|held| held.id == *id)
13667 .cloned()
13668 .or_else(|| {
13669 self.updated()
13670 .ok()?
13671 .iter()
13672 .find(|held| held.id == *id)
13673 .cloned()
13674 })?;
13675 let mut task = held.task().ok()?;
13676 task.version = None;
13677 Some(task)
13678 }
13679
13680 /// Why `creation_owners` refuses a create of `target`, by [`Self::creation_rule`] — read
13681 /// only when the list is not empty, so a source restricting nothing reads nothing.
13682 async fn creation_refusal(
13683 &self,
13684 target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13685 ) -> Result<Option<String>, SourceError> {
13686 let onetaskgraph_plugin_api::WriteTarget::New {
13687 repositories,
13688 project,
13689 } = target
13690 else {
13691 return Ok(None);
13692 };
13693 if self.creation_owners.is_empty() {
13694 return Ok(None);
13695 }
13696 let target = self
13697 .creation_rule(repositories, *project, "a new item")
13698 .await?;
13699 Ok(self.owner_refusal(&target))
13700 }
13701
13702 async fn health(&self) -> Result<Health, SourceError> {
13703 let board = self.board_page(None, 1).await?;
13704 Ok(Health {
13705 reachable: true,
13706 detail: Some(format!(
13707 "reading GitHub project {}/{} ({})",
13708 self.owner,
13709 self.project_number,
13710 required_str(&board, "title")?
13711 )),
13712 })
13713 }
13714 /// The board's own node id, digested: the one request reading its fields, or none when
13715 /// this command has read them already.
13716 async fn designator(&self) -> Result<Option<Designator>, SourceError> {
13717 let board = self.board_fields().await?;
13718 Designator::of_backend(KIND, board.id.as_str())
13719 .map(Some)
13720 .map_err(|message| SourceError::Malformed { message })
13721 }
13722 async fn get_task(&self, id: &NativeId) -> Result<Option<Task>, SourceError> {
13723 self.item_by_id(id)
13724 .await?
13725 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
13726 .map(|item| item.task())
13727 .transpose()
13728 }
13729 async fn task_assets(
13730 &self,
13731 id: &NativeId,
13732 ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
13733 self.held_assets(id, BoardKind::Work(ItemKind::Task)).await
13734 }
13735 async fn task_asset(
13736 &self,
13737 id: &NativeId,
13738 name: &onetaskgraph_plugin_api::AssetName,
13739 ) -> Result<Option<Vec<u8>>, SourceError> {
13740 self.held_asset(id, BoardKind::Work(ItemKind::Task), name)
13741 .await
13742 }
13743 async fn set_task_rendering_with_assets(
13744 &self,
13745 id: &NativeId,
13746 content: &str,
13747 provenance: &Value,
13748 _answers: &BTreeMap<String, Value>,
13749 assets: &onetaskgraph_plugin_api::AssetWrite,
13750 ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
13751 self.replace_rendering(
13752 id,
13753 BoardKind::Work(ItemKind::Task),
13754 content,
13755 provenance,
13756 Some(assets),
13757 )
13758 .await
13759 }
13760 async fn document_assets(
13761 &self,
13762 id: &NativeId,
13763 ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
13764 self.held_assets(id, BoardKind::Document).await
13765 }
13766 async fn document_asset(
13767 &self,
13768 id: &NativeId,
13769 name: &onetaskgraph_plugin_api::AssetName,
13770 ) -> Result<Option<Vec<u8>>, SourceError> {
13771 self.held_asset(id, BoardKind::Document, name).await
13772 }
13773 async fn set_document_rendering_with_assets(
13774 &self,
13775 id: &NativeId,
13776 content: &str,
13777 provenance: &Value,
13778 _answers: &BTreeMap<String, Value>,
13779 assets: &onetaskgraph_plugin_api::AssetWrite,
13780 ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
13781 self.replace_rendering(id, BoardKind::Document, content, provenance, Some(assets))
13782 .await
13783 }
13784 async fn get_project(&self, id: &NativeId) -> Result<Option<Project>, SourceError> {
13785 Ok(self
13786 .item_by_id(id)
13787 .await?
13788 .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
13789 .map(|item| item.project()))
13790 }
13791 async fn query_tasks(
13792 &self,
13793 query: &TaskQuery,
13794 page: &PageRequest,
13795 ) -> Result<Page<Task>, SourceError> {
13796 validate_page(page)?;
13797 refuse_unsearchable(query)?;
13798 // A closed issue reads `done`, `cancelled` or — closed for a reason that implies no
13799 // category — `unknown`; every other category reads only off an open issue or a draft. So
13800 // a status filter naming none of those three asks every issue search it sends for open
13801 // issues alone; each candidate's category is still confirmed from its own item below.
13802 let open_only = !query.statuses.is_empty()
13803 && query.statuses.iter().all(|category| {
13804 !matches!(
13805 category,
13806 StatusCategory::Done | StatusCategory::Cancelled | StatusCategory::Unknown
13807 )
13808 });
13809 if query.origin.is_none() && !matches!(query.project, ProjectFilter::Is(_)) {
13810 let mut qualifiers: Vec<String> = [
13811 query.commented_since.map(updated_qualifier),
13812 narrowing_qualifiers(query),
13813 ]
13814 .into_iter()
13815 .flatten()
13816 .collect();
13817 // A narrowed search never answers a draft, which is no issue, so narrowing it to
13818 // open issues too leaves out nothing such a search could have answered.
13819 if !qualifiers.is_empty() {
13820 if open_only {
13821 qualifiers.push(OPEN_QUALIFIER.to_owned());
13822 }
13823 return self.search_tasks(query, page, &qualifiers.join(" ")).await;
13824 }
13825 }
13826
13827 // A read narrowed to one project asks that project for its own tasks, so nothing
13828 // about it costs what the rest of the board holds. A read carrying a text, metadata
13829 // or origin predicate asks GitHub the narrower question those predicates are, and a
13830 // read narrowed to comment activity alone asks the board's own issue search for the
13831 // issues updated since, which is every issue a comment could have been written or
13832 // edited on since. Every other task read is a question about the whole board and is
13833 // answered by reading it.
13834 let (held, membership) = match (&query.project, query.commented_since) {
13835 (ProjectFilter::Is(project), _) => (
13836 match self.held_task_children(project)? {
13837 // Taken as `project_children` takes the project's own sub-issue read: with
13838 // every item this process wrote under it since, and none it moved away.
13839 Some(held) => self.completed_with_written(
13840 held.into_iter()
13841 .filter(|child| child.parent.as_ref() == Some(project))
13842 .collect(),
13843 |own| own.parent.as_ref() == Some(project),
13844 )?,
13845 None => self.project_children(project).await?,
13846 },
13847 // Answered by where these items came from; see `task_matches`.
13848 &ProjectFilter::Any,
13849 ),
13850 (ProjectFilter::Any | ProjectFilter::Orphans, since) => {
13851 match (self.narrowed(query).await?, since) {
13852 (Some(narrowed), _) => (narrowed, &query.project),
13853 (None, Some(since)) => (self.updated_since(since).await?, &query.project),
13854 // The board's own items list every draft; only the search half, which
13855 // completes them with issues they lag behind on, is narrowed — a closed
13856 // issue that half leaves out is at no category the filter keeps.
13857 (None, None) if open_only => (
13858 self.board_completed_by(Some(OPEN_QUALIFIER)).await?.items,
13859 &query.project,
13860 ),
13861 (None, None) => (self.board().await?.items, &query.project),
13862 }
13863 }
13864 };
13865 // Filtered before paged: a page of a filtered result is a page of the survivors,
13866 // never the survivors of a page.
13867 let mut tasks = Vec::new();
13868 for item in held
13869 .iter()
13870 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
13871 {
13872 let task = item.task()?;
13873 if task_matches(&task, query, membership)
13874 && self.commented_since(item, query.commented_since).await?
13875 {
13876 tasks.push(task);
13877 }
13878 }
13879 Ok(offset_page(
13880 tasks,
13881 numeric_cursor(page.cursor.as_ref())?,
13882 page.limit.min(MAX_PAGE_SIZE) as usize,
13883 ))
13884 }
13885 /// Read board pages directly, at the board connection's consistency. Unlike
13886 /// `query_tasks`, this does not complete a lagging board with issue search: a complete
13887 /// sweep needs one request per listing page, including the first comments.
13888 async fn query_task_details(
13889 &self,
13890 query: &TaskQuery,
13891 page: &PageRequest,
13892 comments: Option<&PageRequest>,
13893 ) -> Result<Page<TaskDetailRead>, SourceError> {
13894 let Some(comments) = comments else {
13895 let tasks = self.query_tasks(query, page).await?;
13896 return Ok(Page {
13897 items: tasks
13898 .items
13899 .into_iter()
13900 .map(|task| TaskDetailRead {
13901 task,
13902 comments: None,
13903 })
13904 .collect(),
13905 next: tasks.next,
13906 });
13907 };
13908 validate_page(page)?;
13909 validate_page(comments)?;
13910 refuse_unsearchable(query)?;
13911 if comments.cursor.is_some() || query.commented_since.is_some() {
13912 return Err(SourceError::Refused {
13913 message: "a board listing with comments requires a first comment page and cannot filter by comment activity".into(),
13914 });
13915 }
13916 let after = page.cursor.as_ref().map(|cursor| cursor.0.as_str());
13917 let data = self
13918 .graphql(
13919 graphql::BOARD_WITH_COMMENTS,
13920 json!({
13921 "owner":self.owner,"number":self.project_number,
13922 "first":page.limit.min(MAX_PAGE_SIZE),"after":after,
13923 "commentFirst":comments.limit.min(MAX_PAGE_SIZE),
13924 "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true,
13925 }),
13926 )
13927 .await?;
13928 let connection = data
13929 .pointer("/owner/projectV2/items")
13930 .filter(|value| !value.is_null())
13931 .ok_or_else(|| SourceError::Refused {
13932 message: "GitHub project was not found or is not visible to the token".into(),
13933 })?;
13934 let mut items = Vec::new();
13935 for node in optional_nodes(Some(connection), "board items")?
13936 .into_iter()
13937 .flatten()
13938 {
13939 let Some(item) = self.resolve(node)? else {
13940 continue;
13941 };
13942 if item.kind != BoardKind::Work(ItemKind::Task) {
13943 continue;
13944 }
13945 let task = item.task()?;
13946 if !task_matches(&task, query, &query.project) {
13947 continue;
13948 }
13949 let read = if item.content_kind == ContentKind::DraftIssue {
13950 Err(self.draft_has_no_comments(&task.id))
13951 } else {
13952 self.expected.learn_comments(&task.id, &node["content"]);
13953 comment_page(&node["content"], &task.id.0, None).map(Some)
13954 };
13955 items.push(TaskDetailRead {
13956 task,
13957 comments: Some(read),
13958 });
13959 }
13960 let next = next_cursor(connection)?;
13961 if let Some(next) = &next {
13962 validate_cursor_progress(after, &next.0)?;
13963 }
13964 Ok(Page { items, next })
13965 }
13966
13967 async fn query_projects(
13968 &self,
13969 query: &ProjectQuery,
13970 page: &PageRequest,
13971 ) -> Result<Page<Project>, SourceError> {
13972 validate_page(page)?;
13973 refuse_unsearchable_text(query.text.as_ref())?;
13974 // The projects a board holds are found by an issue search scoped to that board,
13975 // never by walking the board's own item connection: what tells a project from a
13976 // task is the `parent` each issue carries, which costs nothing to read. A query
13977 // carrying a text asks that search for the text too, so it reads the issues that
13978 // hold it rather than every issue of the board.
13979 let held = match self.text_searched(query.text.as_ref()).await? {
13980 Some(searched) => searched,
13981 None => self.board_issues().await?,
13982 };
13983 let projects = held
13984 .iter()
13985 .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
13986 .map(Resolved::project)
13987 .filter(|project| project_matches(project, query))
13988 .collect();
13989 Ok(offset_page(
13990 projects,
13991 numeric_cursor(page.cursor.as_ref())?,
13992 page.limit.min(MAX_PAGE_SIZE) as usize,
13993 ))
13994 }
13995 async fn query_projects_by_origin(
13996 &self,
13997 origin: &str,
13998 page: &PageRequest,
13999 ) -> Result<Page<Project>, SourceError> {
14000 validate_page(page)?;
14001 let projects = self
14002 .copied_from(origin, BoardKind::Work(ItemKind::Project))
14003 .await?
14004 .iter()
14005 .map(Resolved::project)
14006 .collect();
14007 Ok(offset_page(
14008 projects,
14009 numeric_cursor(page.cursor.as_ref())?,
14010 page.limit.min(MAX_PAGE_SIZE) as usize,
14011 ))
14012 }
14013 async fn get_document(&self, id: &NativeId) -> Result<Option<Document>, SourceError> {
14014 let Some(item) = self
14015 .document_by_id(id)
14016 .await?
14017 .filter(|item| item.kind == BoardKind::Document)
14018 else {
14019 return Ok(None);
14020 };
14021 self.expected.read_document(&item);
14022 Ok(Some(item.document()))
14023 }
14024 async fn query_documents_by_origin(
14025 &self,
14026 origin: &str,
14027 page: &PageRequest,
14028 ) -> Result<Page<Document>, SourceError> {
14029 validate_page(page)?;
14030 let documents = self
14031 .copied_from(origin, BoardKind::Document)
14032 .await?
14033 .iter()
14034 .map(Resolved::document)
14035 .collect();
14036 Ok(offset_page(
14037 documents,
14038 numeric_cursor(page.cursor.as_ref())?,
14039 page.limit.min(MAX_PAGE_SIZE) as usize,
14040 ))
14041 }
14042 async fn query_documents(
14043 &self,
14044 query: &DocumentQuery,
14045 page: &PageRequest,
14046 ) -> Result<Page<Document>, SourceError> {
14047 validate_page(page)?;
14048 // Narrowed to one project, this is the same sub-issue read a task list scoped to
14049 // that project makes — a document filed under a project is a sub-issue of it too,
14050 // and which of them come back is the kind this caller asked for. Unscoped, a query
14051 // carrying a text asks the board-scoped issue search for it, as a task query does,
14052 // and only one carrying none reads the board.
14053 let (held, membership) = match &query.project {
14054 ProjectFilter::Is(project) => (
14055 self.project_children(project).await?,
14056 // Answered by where these items came from; see `task_matches`.
14057 &ProjectFilter::Any,
14058 ),
14059 ProjectFilter::Any | ProjectFilter::Orphans => {
14060 refuse_unsearchable_text(query.text.as_ref())?;
14061 match self.text_searched(query.text.as_ref()).await? {
14062 Some(searched) => (searched, &query.project),
14063 None => (self.board().await?.items, &query.project),
14064 }
14065 }
14066 };
14067 // Filtered before paged, exactly as a task read is: a page of a filtered result is
14068 // a page of the survivors, never the survivors of a page.
14069 let documents = held
14070 .iter()
14071 .filter(|item| item.kind == BoardKind::Document)
14072 .map(Resolved::document)
14073 .filter(|document| document_matches(document, query, membership))
14074 .collect();
14075 Ok(offset_page(
14076 documents,
14077 numeric_cursor(page.cursor.as_ref())?,
14078 page.limit.min(MAX_PAGE_SIZE) as usize,
14079 ))
14080 }
14081 async fn labels(&self, page: &PageRequest) -> Result<Page<Label>, SourceError> {
14082 validate_page(page)?;
14083 let offset = numeric_cursor(page.cursor.as_ref())?;
14084 let mut labels = self
14085 .board()
14086 .await?
14087 .items
14088 .into_iter()
14089 .flat_map(|item| item.labels)
14090 .fold(Vec::new(), |mut all, label| {
14091 if !all.iter().any(|x: &Label| x.id == label.id) {
14092 all.push(label);
14093 }
14094 all
14095 });
14096 labels.sort_by(|a, b| a.name.cmp(&b.name).then(a.id.0.cmp(&b.id.0)));
14097 Ok(offset_page(
14098 labels,
14099 offset,
14100 page.limit.min(MAX_PAGE_SIZE) as usize,
14101 ))
14102 }
14103 async fn task_dependencies(
14104 &self,
14105 id: &NativeId,
14106 direction: Direction,
14107 page: &PageRequest,
14108 ) -> Result<Page<DependencyEdge>, SourceError> {
14109 self.dependencies(id, ItemKind::Task, direction, page).await
14110 }
14111 async fn project_dependencies(
14112 &self,
14113 id: &NativeId,
14114 direction: Direction,
14115 page: &PageRequest,
14116 ) -> Result<Page<DependencyEdge>, SourceError> {
14117 self.dependencies(id, ItemKind::Project, direction, page)
14118 .await
14119 }
14120
14121 fn writes(&self) -> WriteSupport {
14122 WriteSupport::Supported
14123 }
14124
14125 /// Create or update one task.
14126 ///
14127 /// Its `delivers` and `delivered_by` are checked before anything is read or written —
14128 /// neither may name the task itself or name one task twice — and land in the body's
14129 /// metadata slot under their reserved keys, in place of any caller metadata of those
14130 /// names.
14131 async fn write_task(&self, write: &ItemWrite<Task>) -> Result<NativeId, SourceError> {
14132 self.write_task_assets(write, None)
14133 .await
14134 .map(|written| written.id)
14135 }
14136
14137 async fn read_ahead_origins_for_copy(&self, origins: &[String]) -> Result<(), SourceError> {
14138 self.origins_ahead(origins).await
14139 }
14140
14141 async fn member_classifications(
14142 &self,
14143 project: &NativeId,
14144 ) -> Result<Option<Vec<(Classification, Vec<Repository>)>>, SourceError> {
14145 Ok(self
14146 .member_classes
14147 .lock()
14148 .map_err(|_| SourceError::Unavailable {
14149 message: "a project's members held for its classification were left \
14150 inconsistent; run the command again"
14151 .into(),
14152 })?
14153 .remove(project))
14154 }
14155
14156 /// The issue's own web address, as the `createIssue` that made it in this command answered
14157 /// it: a task, a project and a document are each an issue here.
14158 fn written_location(
14159 &self,
14160 _record: onetaskgraph_plugin_api::MetadataRecord,
14161 id: &NativeId,
14162 ) -> Option<Location> {
14163 self.written
14164 .lock()
14165 .unwrap_or_else(std::sync::PoisonError::into_inner)
14166 .get(id)
14167 .cloned()
14168 .map(Location::Url)
14169 }
14170
14171 /// [`MUTATION_BATCH`], while writes are held back: [`graphql::CREATE_ISSUES`] creates that many
14172 /// in one request, and a copy's tasks are created together only then, since their fields
14173 /// and edges are what is held back.
14174 fn create_batch_limit(&self) -> usize {
14175 if self.deferring().unwrap_or(false) {
14176 MUTATION_BATCH
14177 } else {
14178 1
14179 }
14180 }
14181
14182 // 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.
14183 async fn write_tasks(&self, writes: &[ItemWrite<Task>]) -> Result<Vec<NativeId>, SourceError> {
14184 if let Some(write) = writes.iter().find(|write| write.target.is_some()) {
14185 return Err(SourceError::Refused {
14186 message: format!(
14187 "write_tasks creates tasks, and the write of {} names the existing task {}; \
14188 next: update it with write_task",
14189 write.item.id,
14190 write.target.as_ref().map_or("", |target| target.0.as_str())
14191 ),
14192 });
14193 }
14194 if !self.deferring()? {
14195 let mut created = Vec::with_capacity(writes.len());
14196 for write in writes {
14197 match self.write_task(write).await {
14198 Ok(id) => created.push(id),
14199 Err(error) => {
14200 let mut left: Vec<&str> = Vec::new();
14201 for id in &created {
14202 if self.delete_issue(id).await.is_err() {
14203 left.push(id.0.as_str());
14204 }
14205 }
14206 return Err(left_behind(error, &left));
14207 }
14208 }
14209 }
14210 return Ok(created);
14211 }
14212 self.create_together(writes).await
14213 }
14214
14215 fn begin_deferred_writes(&self) {
14216 if let Ok(mut deferred) = self.deferred.lock() {
14217 deferred.get_or_insert_with(Deferred::default);
14218 }
14219 }
14220
14221 async fn finish_writes(&self) -> Result<(), SourceError> {
14222 self.send_held_updates().await?;
14223 self.send_held_back().await
14224 }
14225
14226 async fn write_task_with_assets(
14227 &self,
14228 write: &ItemWrite<Task>,
14229 _answers: Option<&BTreeMap<String, Value>>,
14230 assets: &onetaskgraph_plugin_api::AssetWrite,
14231 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
14232 self.write_task_assets(write, Some(assets)).await
14233 }
14234
14235 async fn write_project(&self, write: &ItemWrite<Project>) -> Result<NativeId, SourceError> {
14236 self.write_item(
14237 &Incoming {
14238 written: Written::Work(ItemKind::Project, &write.item.status),
14239 title: &write.item.title,
14240 content: write.item.content.as_deref(),
14241 assets: None,
14242 labels: &write.item.labels,
14243 metadata: &write.item.metadata,
14244 repositories: &write.item.repositories,
14245 classification: write.item.classification,
14246 parent: None,
14247 delivers: &[],
14248 delivered_by: &[],
14249 priority: None,
14250 },
14251 write.target.as_ref(),
14252 &write.depends_on,
14253 )
14254 .await
14255 .map(|written| written.id)
14256 }
14257
14258 /// Create or update one document, which is one issue titled the way this board spells
14259 /// a document.
14260 ///
14261 /// Everything else is exactly a task write: caller metadata goes to the same canonical
14262 /// JSON slot at the end of the body and comes back with its JSON types intact, a key
14263 /// or a field this board cannot carry is refused by name rather than dropped, a target
14264 /// naming an issue this board does not hold is refused rather than created, and an
14265 /// issue this call created is taken back when the rest of the write fails.
14266 async fn write_document(&self, write: &ItemWrite<Document>) -> Result<NativeId, SourceError> {
14267 self.write_document_assets(write, None)
14268 .await
14269 .map(|written| written.id)
14270 }
14271
14272 async fn write_document_with_assets(
14273 &self,
14274 write: &ItemWrite<Document>,
14275 _answers: Option<&BTreeMap<String, Value>>,
14276 assets: &onetaskgraph_plugin_api::AssetWrite,
14277 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
14278 self.write_document_assets(write, Some(assets)).await
14279 }
14280
14281 /// Refused exactly as the write refuses it, from what the write reads: the mapping first,
14282 /// which reads nothing; then the board's `Status` option. Over an existing item that is
14283 /// read off the item, as the write reads it, and the item is held among this command's
14284 /// resolved records so the write that follows reuses that read rather than repeating it;
14285 /// an item that does not carry the field takes the board's fields, which are held once
14286 /// read. A create is checked against the board's fields only when this command already
14287 /// holds them, because a create reads them together with its repository, in one request,
14288 /// and refuses a missing option before it writes anything.
14289 async fn check_status_write(
14290 &self,
14291 kind: ItemKind,
14292 category: StatusCategory,
14293 target: Option<&NativeId>,
14294 ) -> Result<(), SourceError> {
14295 let status = self.resolved_target(kind, category)?;
14296 if status.option().is_none() {
14297 return Ok(());
14298 }
14299 let fields = match target {
14300 Some(target) => {
14301 // A target this board does not hold is the write's own refusal to make.
14302 let Some(item) = self.bound_item(target).await? else {
14303 return Ok(());
14304 };
14305 self.resolved_cache()?.insert(target.clone(), item.clone());
14306 // Only the `Status` field decides whether the write can land.
14307 self.fields_for(Some(&item), false, true, false)
14308 .await?
14309 .fields
14310 }
14311 None => {
14312 let held = self
14313 .board_cache()?
14314 .as_ref()
14315 .map(|board| board.fields.clone());
14316 match held.or_else(|| {
14317 self.fields_cache()
14318 .ok()
14319 .and_then(|cache| cache.as_ref().map(|board| board.fields.clone()))
14320 }) {
14321 Some(fields) => fields,
14322 None => return Ok(()),
14323 }
14324 }
14325 };
14326 self.column_for(&fields, kind, category, &status)
14327 .map(|_| ())
14328 }
14329
14330 /// What a write of `status` reads back as: the `Status` option its category maps to for
14331 /// `kind`, and for a closing category the closure that option and its fixed reason read
14332 /// as — exactly what [`BoardStatuses::status`] reports of the item once written. A category
14333 /// this instance has no option for is answered as given, since writing it is refused.
14334 fn status_as_written(&self, kind: ItemKind, status: &Status) -> Status {
14335 match self.statuses.target(kind, status.category) {
14336 StatusTarget::Column(name) => {
14337 self.statuses.status(kind, Some(name.as_str()), false, None)
14338 }
14339 StatusTarget::Terminal(name, reason) => {
14340 self.statuses
14341 .status(kind, Some(name.as_str()), true, Some(reason.reason()))
14342 }
14343 StatusTarget::Disabled(_) => status.clone(),
14344 }
14345 }
14346
14347 /// Set one task's status alone.
14348 ///
14349 /// An open target reopens a closed issue with an `updateIssue` carrying only its
14350 /// `stateInput`, then selects the board option with `updateProjectV2ItemFieldValue`; a
14351 /// terminal target selects its mapped option, then closes with its fixed reason. No
14352 /// request carries a title, a body or a label. The status
14353 /// answered is what [`BoardStatuses::status`] reads off the state just written, which is
14354 /// what a re-read reports.
14355 async fn set_task_status(
14356 &self,
14357 id: &NativeId,
14358 category: StatusCategory,
14359 ) -> Result<Option<Status>, SourceError> {
14360 self.set_status(id, ItemKind::Task, category).await
14361 }
14362
14363 /// Set one task's status and answer the task with it, reading the item once: the read the
14364 /// task is answered from is handed to the status write, which would otherwise read the
14365 /// same issue again — two requests on a fresh process, the read and the field write,
14366 /// where the default's `get_task` before `set_task_status` sent three.
14367 async fn set_task_status_reading(
14368 &self,
14369 id: &NativeId,
14370 category: StatusCategory,
14371 ) -> Result<Option<Task>, SourceError> {
14372 // Refused before anything is read, in the words the write itself refuses it.
14373 self.resolved_target(ItemKind::Task, category)?;
14374 let Some(item) = self
14375 .bound_item(id)
14376 .await?
14377 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
14378 else {
14379 return Ok(None);
14380 };
14381 let mut task = item.task()?;
14382 self.held_cache
14383 .lock()
14384 .map_err(|_| SourceError::Unavailable {
14385 message: "items read by issue reference were left inconsistent; run the \
14386 command again"
14387 .into(),
14388 })?
14389 .insert(id.clone(), item);
14390 let Some(status) = self.set_status(id, ItemKind::Task, category).await? else {
14391 return Ok(None);
14392 };
14393 task.status = status;
14394 Ok(Some(task))
14395 }
14396
14397 /// Set one project's status alone, by exactly the paired write a task's status takes, for
14398 /// the project kind: a terminal category selects the project kind's mapped option and then
14399 /// closes the issue with the category's fixed reason — an issue already closed with a
14400 /// reason that reads that category is left as it is — and an open category reopens a closed
14401 /// issue and then selects its option. A board draft is refused a terminal category as a
14402 /// task's is.
14403 async fn set_project_status(
14404 &self,
14405 id: &NativeId,
14406 category: StatusCategory,
14407 ) -> Result<Option<Status>, SourceError> {
14408 self.set_status(id, ItemKind::Project, category).await
14409 }
14410
14411 /// Every task and project whose card contradicts its issue's closure, read through the
14412 /// board's own `is:closed` filter; see [`TaskSource::status_contradictions`].
14413 async fn status_contradictions(&self) -> Result<Vec<StatusContradiction>, SourceError> {
14414 self.contradictions().await
14415 }
14416
14417 /// What this board holds for each issue reference, one request per fifty of them; see
14418 /// [`TaskSource::issue_holdings`].
14419 async fn issue_holdings(&self, issues: &[IssueRef]) -> Result<Vec<IssueHolding>, SourceError> {
14420 self.holdings(issues).await
14421 }
14422
14423 /// Set one task's priority alone: one `updateProjectV2ItemFieldValue` selecting the
14424 /// mapped option of the board's `Priority` field, or one `clearProjectV2ItemFieldValue`
14425 /// for `none`. Refused by an instance with no `priority_mapping`.
14426 async fn set_task_priority(
14427 &self,
14428 id: &NativeId,
14429 priority: Priority,
14430 ) -> Result<Option<Priority>, SourceError> {
14431 self.set_priority(id, priority).await
14432 }
14433
14434 /// Replace one task's content with a single body update that keeps the metadata slot
14435 /// byte for byte.
14436 async fn set_task_content(
14437 &self,
14438 id: &NativeId,
14439 content: &str,
14440 ) -> Result<Option<()>, SourceError> {
14441 self.replace_content(id, content).await
14442 }
14443
14444 /// Replace one task issue's content and its provenance slot entry with a single body
14445 /// update. The answers are not kept: see `replace_rendering`.
14446 async fn set_task_rendering(
14447 &self,
14448 id: &NativeId,
14449 content: &str,
14450 provenance: &Value,
14451 _answers: &BTreeMap<String, Value>,
14452 ) -> Result<Option<()>, SourceError> {
14453 self.replace_rendering(
14454 id,
14455 BoardKind::Work(ItemKind::Task),
14456 content,
14457 provenance,
14458 None,
14459 )
14460 .await
14461 .map(|written| written.map(|_| ()))
14462 }
14463
14464 /// Replace one design-document issue's content and its provenance slot entry, on exactly
14465 /// the terms of [`set_task_rendering`](TaskSource::set_task_rendering).
14466 async fn set_document_rendering(
14467 &self,
14468 id: &NativeId,
14469 content: &str,
14470 provenance: &Value,
14471 _answers: &BTreeMap<String, Value>,
14472 ) -> Result<Option<()>, SourceError> {
14473 self.replace_rendering(id, BoardKind::Document, content, provenance, None)
14474 .await
14475 .map(|written| written.map(|_| ()))
14476 }
14477
14478 /// Replace one project issue's content and its provenance slot entry, on exactly the
14479 /// terms of [`set_task_rendering`](TaskSource::set_task_rendering).
14480 async fn set_project_rendering(
14481 &self,
14482 id: &NativeId,
14483 content: &str,
14484 provenance: &Value,
14485 _answers: &BTreeMap<String, Value>,
14486 ) -> Result<Option<()>, SourceError> {
14487 self.replace_rendering(
14488 id,
14489 BoardKind::Work(ItemKind::Project),
14490 content,
14491 provenance,
14492 None,
14493 )
14494 .await
14495 .map(|written| written.map(|_| ()))
14496 }
14497
14498 /// Apply a targeted update with one read of the item and a write only for what differs:
14499 /// the `Status` and `Priority` field writes in one request, the `blockedBy` difference,
14500 /// and last one `updateIssue` for title, body and state. See `targeted_update`.
14501 async fn update_task(
14502 &self,
14503 id: &NativeId,
14504 update: &TaskUpdate,
14505 ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
14506 self.targeted_update(id, update).await
14507 }
14508
14509 /// Replace one task's `delivered_by` with a single body update that changes the
14510 /// metadata slot and nothing outside it.
14511 async fn set_delivered_by(
14512 &self,
14513 id: &NativeId,
14514 delivered_by: &[TaskRef],
14515 ) -> Result<Option<()>, SourceError> {
14516 self.replace_delivered_by(id, delivered_by).await
14517 }
14518
14519 /// Set one key of one task issue's metadata with a single body update that changes the
14520 /// metadata slot and nothing outside it — no title, label, state or board field request —
14521 /// and sends nothing when the task already holds that value under the key.
14522 async fn set_task_metadata(
14523 &self,
14524 id: &NativeId,
14525 key: &MetadataKey,
14526 value: &Value,
14527 ) -> Result<Option<Task>, SourceError> {
14528 Ok(self
14529 .set_slot_key(id, BoardKind::Work(ItemKind::Task), key, value)
14530 .await?
14531 .map(|item| item.task())
14532 .transpose()?)
14533 }
14534
14535 /// Set one key of one project issue's metadata, on exactly the terms of
14536 /// [`set_task_metadata`](TaskSource::set_task_metadata).
14537 async fn set_project_metadata(
14538 &self,
14539 id: &NativeId,
14540 key: &MetadataKey,
14541 value: &Value,
14542 ) -> Result<Option<Project>, SourceError> {
14543 Ok(self
14544 .set_slot_key(id, BoardKind::Work(ItemKind::Project), key, value)
14545 .await?
14546 .map(|item| item.project()))
14547 }
14548
14549 /// Set one key of one design-document issue's metadata, on exactly the terms of
14550 /// [`set_task_metadata`](TaskSource::set_task_metadata).
14551 async fn set_document_metadata(
14552 &self,
14553 id: &NativeId,
14554 key: &MetadataKey,
14555 value: &Value,
14556 ) -> Result<Option<Document>, SourceError> {
14557 Ok(self
14558 .set_slot_key(id, BoardKind::Document, key, value)
14559 .await?
14560 .map(|item| item.document()))
14561 }
14562
14563 /// Take one key out of one issue's metadata slot with a single body update that changes the
14564 /// slot and nothing outside it, sending nothing when the slot does not hold the key.
14565 async fn remove_record_metadata(
14566 &self,
14567 record: onetaskgraph_plugin_api::MetadataRecord,
14568 id: &NativeId,
14569 key: &MetadataKey,
14570 ) -> Result<Option<Option<String>>, SourceError> {
14571 let kind = match record {
14572 onetaskgraph_plugin_api::MetadataRecord::Task => BoardKind::Work(ItemKind::Task),
14573 onetaskgraph_plugin_api::MetadataRecord::Project => BoardKind::Work(ItemKind::Project),
14574 onetaskgraph_plugin_api::MetadataRecord::Document => BoardKind::Document,
14575 };
14576 let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
14577 return Ok(None);
14578 };
14579 if item.slot.contains_key(key.as_str()) {
14580 let mut slot = item.slot.clone();
14581 slot.remove(key.as_str());
14582 self.write_slot(&mut item, &slot).await?;
14583 self.remember_written(item.clone(), false)?;
14584 }
14585 Ok(Some(match record {
14586 onetaskgraph_plugin_api::MetadataRecord::Task => item.task()?.version,
14587 onetaskgraph_plugin_api::MetadataRecord::Project => item.project().version,
14588 onetaskgraph_plugin_api::MetadataRecord::Document => item.document().version,
14589 }))
14590 }
14591
14592 async fn delete_task(&self, id: &NativeId) -> Result<(), SourceError> {
14593 self.delete_item(id).await
14594 }
14595
14596 async fn delete_project(&self, id: &NativeId) -> Result<(), SourceError> {
14597 self.delete_item(id).await
14598 }
14599
14600 async fn delete_document(&self, id: &NativeId) -> Result<(), SourceError> {
14601 self.delete_item(id).await
14602 }
14603
14604 /// One page of the task issue's own comments, walked by GitHub's own cursor.
14605 ///
14606 /// Nothing here filters, so nothing has to be read ahead of the page: the caller's limit is
14607 /// the page GitHub is asked for and GitHub's `endCursor` is the cursor handed back.
14608 ///
14609 /// One request, [`graphql::ISSUE_DETAIL`]: the read that says the id names a task of this
14610 /// board is the read of its comments. A draft this process already resolved is refused
14611 /// without one.
14612 async fn task_comments(
14613 &self,
14614 task: &NativeId,
14615 page: &PageRequest,
14616 ) -> Result<Option<Page<Comment>>, SourceError> {
14617 validate_page(page)?;
14618 let cached = self.resolved_cache()?.get(task).cloned();
14619 if let Some(item) = cached {
14620 if item.kind != BoardKind::Work(ItemKind::Task) {
14621 return Ok(None);
14622 }
14623 if item.content_kind == ContentKind::DraftIssue {
14624 return Err(self.draft_has_no_comments(task));
14625 }
14626 }
14627 match self.issue_detail(task, page).await? {
14628 Some(TaskDetailRead {
14629 comments: Some(comments),
14630 ..
14631 }) => comments,
14632 _ => Ok(None),
14633 }
14634 }
14635
14636 /// Every id's task, with the first page of its comments when `comments` names it:
14637 /// [`DETAIL_BATCH`] items per [`graphql::ISSUE_DETAILS`] request, and one item with its
14638 /// comments in one [`graphql::ISSUE_DETAIL`] request.
14639 async fn get_task_details(
14640 &self,
14641 ids: &[NativeId],
14642 comments: Option<&PageRequest>,
14643 ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
14644 if let Some(page) = comments
14645 && let Err(error) = validate_page(page)
14646 {
14647 return ids.iter().map(|_| Err(error.clone())).collect();
14648 }
14649 match (ids, comments) {
14650 ([id], Some(page)) => vec![self.issue_detail(id, page).await],
14651 ([id], None) => vec![self.task_read(id).await],
14652 _ => self.issue_details(ids, comments).await,
14653 }
14654 }
14655
14656 /// Every id's task with the first page of its comments and its `blockedBy`, as a copy reads
14657 /// the tasks it carries: [`COPY_DETAIL_BATCH`] items per [`graphql::ISSUE_COPY_DETAILS`]
14658 /// request, so the dependency read a copy makes of each next is answered from this one.
14659 async fn get_task_details_for_copy(
14660 &self,
14661 ids: &[NativeId],
14662 comments: &PageRequest,
14663 ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
14664 // A page past the first is a page of one task's comments, which the detail read of it
14665 // alone answers.
14666 if comments.cursor.is_some() {
14667 return self.get_task_details(ids, Some(comments)).await;
14668 }
14669 if let Err(error) = validate_page(comments) {
14670 return ids.iter().map(|_| Err(error.clone())).collect();
14671 }
14672 let mut read = Vec::with_capacity(ids.len());
14673 for batch in ids.chunks(COPY_DETAIL_BATCH) {
14674 match self
14675 .graphql(
14676 graphql::ISSUE_COPY_DETAILS,
14677 copy_detail_batch(batch, comments, false),
14678 )
14679 .await
14680 {
14681 Ok(data) => {
14682 for (slot, id) in batch.iter().enumerate() {
14683 let read_one = match data.get(format!("i{slot}")) {
14684 Some(node) => self.detail_of(id, node, true, None).await,
14685 None => Err(SourceError::Malformed {
14686 message: format!(
14687 "GitHub answered a batch read with no item for {}",
14688 id.0
14689 ),
14690 }),
14691 };
14692 read.push(read_one);
14693 }
14694 }
14695 Err(error) if unresolvable_node(&error) => {
14696 for id in batch {
14697 read.push(self.issue_detail(id, comments).await);
14698 }
14699 }
14700 Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
14701 }
14702 }
14703 read
14704 }
14705
14706 /// Every id's item, whatever kind it is, read [`COPY_DETAIL_BATCH`] at a time with
14707 /// [`graphql::ISSUE_COPY_DETAILS`] — the document a copy reads the tasks it carries with —
14708 /// and held for the next read of each by id; see `read_ahead`.
14709 ///
14710 /// What this run wrote is never read: its own record answers, as it does for a read by id.
14711 /// Nor is an item a listing of its project carried whole earlier in this command — see
14712 /// `carried` — which is held in place of reading it.
14713 /// A batch GitHub refuses because one of its ids resolves to no node at all holds nothing,
14714 /// so each of its ids is read on its own and answered as that read answers; a draft, and an
14715 /// answer naming another issue than the one asked for, are left to their own reads too.
14716 async fn read_ahead_for_copy(&self, ids: &[NativeId]) -> Result<(), SourceError> {
14717 // The origins asked just before go out now, beside the members of the one project this
14718 // names when it names one; a project those origins answered whole is held for its next
14719 // read by id rather than read again here.
14720 let hinted = match ids {
14721 [one] => Some(one),
14722 _ => None,
14723 };
14724 self.send_pending_origins(hinted).await?;
14725 if let Some(project) = hinted {
14726 let answered = self.resolved_cache()?.get(project).cloned();
14727 if let Some(answered) = answered
14728 && answered.kind == BoardKind::Work(ItemKind::Project)
14729 {
14730 self.read_ahead()?.insert(project.clone(), Some(answered));
14731 return Ok(());
14732 }
14733 }
14734 let mut unread: Vec<NativeId> = Vec::new();
14735 // Read whole by a listing of its project in this command, which a mutation naming it
14736 // would have dropped: held as a batch would have held it, once every batch is answered.
14737 let mut listed: Vec<NativeId> = Vec::new();
14738 {
14739 let created = self.created()?;
14740 let held = self.read_ahead()?;
14741 let carried = self.carried()?;
14742 for id in ids {
14743 if unread.contains(id)
14744 || listed.contains(id)
14745 || held.contains_key(id)
14746 || created.iter().any(|own| own.id == *id)
14747 {
14748 continue;
14749 }
14750 // An issue a listing carried without the whole of what blocks it is read in a
14751 // batch too, which carries that, rather than having its dependency read sent
14752 // on its own once the copy reconciles its edges.
14753 let whole = carried.get(id).is_some_and(|item| {
14754 item.content_kind != ContentKind::Issue || item.blocked_by.is_some()
14755 });
14756 if whole {
14757 listed.push(id.clone());
14758 } else {
14759 unread.push(id.clone());
14760 }
14761 }
14762 }
14763 // Nothing of an item's comments is wanted here: the smallest page keeps the read to
14764 // what the item itself costs.
14765 let comments = PageRequest {
14766 cursor: None,
14767 limit: 1,
14768 };
14769 // Held only once every batch has been answered, so a read-ahead that fails part way
14770 // holds nothing — what a listing carried included, which stays carried — and every
14771 // item is read on its own, as the method promises.
14772 let mut read: Vec<(NativeId, Option<Resolved>)> = Vec::new();
14773 // The issues the first id names as sub-issues, as `ISSUE_COPY_DETAILS`'s `children`
14774 // answered them: what a project copy's orphan check asks, when that first id is its
14775 // project.
14776 let mut children: Option<(NativeId, Vec<(String, String)>)> = None;
14777 for (index, batch) in unread.chunks(COPY_DETAIL_BATCH).enumerate() {
14778 let data = match self
14779 .graphql(
14780 graphql::ISSUE_COPY_DETAILS,
14781 copy_detail_batch(batch, &comments, index == 0),
14782 )
14783 .await
14784 {
14785 Ok(data) => data,
14786 Err(error) if unresolvable_node(&error) => continue,
14787 Err(error) => return Err(error),
14788 };
14789 for (slot, id) in batch.iter().enumerate() {
14790 let Some(node) = data.get(format!("i{slot}")) else {
14791 continue;
14792 };
14793 let held = if node.is_null() {
14794 None
14795 } else {
14796 match optional_str(node, "__typename")? {
14797 Some("DraftIssue") => continue,
14798 Some("Issue") if required_str(node, "id")? != id.0 => continue,
14799 _ => {}
14800 }
14801 // Remembered below with the rest, so a later batch refused leaves no
14802 // dependency read answered from this one either.
14803 self.resolve_issue_as(node, false).await?
14804 };
14805 read.push((id.clone(), held));
14806 }
14807 if index == 0
14808 && let Some(node) = data.get("children").filter(|node| !node.is_null())
14809 && node.get("id").and_then(Value::as_str) == batch.first().map(|id| id.0.as_str())
14810 && node.pointer("/subIssues/pageInfo/hasNextPage") == Some(&Value::Bool(false))
14811 {
14812 // A list GitHub did not answer is not an empty one: it would answer the project's
14813 // task listing with nothing, and the orphan check with no orphans.
14814 let listed = node
14815 .pointer("/subIssues/nodes")
14816 .and_then(Value::as_array)
14817 .ok_or_else(|| SourceError::Malformed {
14818 message: format!(
14819 "GitHub answered the sub-issues of project {} with no list of them",
14820 batch[0].0
14821 ),
14822 })?
14823 .iter()
14824 .map(|child| {
14825 Ok((
14826 required_str(child, "id")?.to_owned(),
14827 required_str(child, "title")?.to_owned(),
14828 ))
14829 })
14830 .collect::<Result<Vec<_>, SourceError>>()?;
14831 children = Some((batch[0].clone(), listed));
14832 }
14833 }
14834 // The project's tasks are known from this read alone when every child it named is held
14835 // from it, but for a document, which its title says is one and which a task listing
14836 // never answers: then a listing of the project's tasks is answered from them.
14837 if let Some((project, listed)) = children
14838 && read.iter().any(|(id, held)| {
14839 *id == project
14840 && held
14841 .as_ref()
14842 .is_some_and(|held| held.kind == BoardKind::Work(ItemKind::Project))
14843 })
14844 {
14845 let mut tasks = Vec::new();
14846 let mut whole = true;
14847 for (child, title) in &listed {
14848 match read.iter().find(|(id, _)| id.0 == *child) {
14849 Some((_, Some(held))) if held.parent.as_ref() == Some(&project) => {
14850 if held.kind == BoardKind::Work(ItemKind::Task) {
14851 tasks.push(held.clone());
14852 }
14853 }
14854 _ if title.starts_with(DESIGN_TITLE_PREFIX) => {}
14855 _ => whole = false,
14856 }
14857 }
14858 if whole {
14859 self.task_children()?.insert(project, tasks);
14860 }
14861 }
14862 let mut resolved = self.resolved_cache()?;
14863 let mut ahead = self.read_ahead()?;
14864 let mut carried = self.carried()?;
14865 for item in read.iter().filter_map(|(_, held)| held.as_ref()) {
14866 resolved.insert(item.id.clone(), item.clone());
14867 }
14868 // Taken only if still carried: a mutation naming it while the batches ran dropped it,
14869 // and its next read is then its own.
14870 for id in &listed {
14871 let Some(item) = carried.remove(id) else {
14872 continue;
14873 };
14874 // The whole of what it is blocked by came with it, so the dependency read a copy
14875 // makes next is answered from this one, as a copy's detail read is.
14876 if item.blocked_by.is_some() {
14877 resolved.insert(item.id.clone(), item.clone());
14878 }
14879 read.push((id.clone(), Some(item)));
14880 }
14881 for (id, held) in read {
14882 ahead.insert(id, held);
14883 }
14884 Ok(())
14885 }
14886
14887 /// Add one comment to the task's issue, as the account the token belongs to.
14888 ///
14889 /// The author is refused before anything is sent — not even the task is read — because
14890 /// no answer GitHub could give would make posting under another name than the one asked
14891 /// for the right outcome.
14892 async fn add_comment(
14893 &self,
14894 task: &NativeId,
14895 comment: &NewComment,
14896 ) -> Result<Option<Comment>, SourceError> {
14897 if let Some(author) = &comment.author {
14898 return Err(SourceError::Refused {
14899 message: format!(
14900 "source {} cannot post a comment as {author:?}: GitHub records the account \
14901 the token signs in as the author of every comment; next: leave --author \
14902 out, and the comment is posted as that account",
14903 self.name
14904 ),
14905 });
14906 }
14907 let Some(issue) = self.commented_issue(task).await? else {
14908 return Ok(None);
14909 };
14910 let data = self
14911 .graphql(
14912 graphql::ADD_COMMENT,
14913 json!({"input":{"subjectId":issue.0,"body":comment.body.as_str()}}),
14914 )
14915 .await?;
14916 let subject = data
14917 .pointer("/addComment/subject")
14918 .filter(|value| !value.is_null())
14919 .ok_or_else(|| SourceError::Malformed {
14920 message: "GitHub comment addition returned no subject".into(),
14921 })?;
14922 if required_str(subject, "id")? != issue.0 {
14923 return Err(SourceError::Malformed {
14924 message: "GitHub comment addition answered about another issue".into(),
14925 });
14926 }
14927 let added = data
14928 .pointer("/addComment/commentEdge/node")
14929 .filter(|value| !value.is_null())
14930 .ok_or_else(|| SourceError::Malformed {
14931 message: "GitHub comment addition returned no comment".into(),
14932 })?;
14933 let added = comment_from(added)?;
14934 self.remember_commented(&issue)?;
14935 Ok(Some(added))
14936 }
14937
14938 async fn edit_comment(
14939 &self,
14940 task: &NativeId,
14941 comment: &NativeId,
14942 body: &CommentBody,
14943 ) -> Result<Option<Comment>, SourceError> {
14944 let Some(issue) = self.commented_issue(task).await? else {
14945 return Ok(None);
14946 };
14947 let Some(held) = self.comment_on(&issue, comment).await? else {
14948 return Ok(None);
14949 };
14950 // A comment a copy wrote keeps its origin through an edit: its marker is carried over.
14951 let body = CommentOrigin::kept(&held, body)?;
14952 let data = self
14953 .graphql(
14954 graphql::UPDATE_COMMENT,
14955 json!({"input":{"id":comment.0,"body":body.as_str()}}),
14956 )
14957 .await?;
14958 let edited = data
14959 .pointer("/updateIssueComment/issueComment")
14960 .filter(|value| !value.is_null())
14961 .ok_or_else(|| SourceError::Malformed {
14962 message: "GitHub comment update returned no comment".into(),
14963 })?;
14964 let edited = comment_from(edited)?;
14965 if edited.id != *comment {
14966 return Err(SourceError::Malformed {
14967 message: "GitHub comment update returned the wrong comment".into(),
14968 });
14969 }
14970 self.remember_commented(&issue)?;
14971 Ok(Some(edited))
14972 }
14973
14974 async fn delete_comment(
14975 &self,
14976 task: &NativeId,
14977 comment: &NativeId,
14978 ) -> Result<Option<NativeId>, SourceError> {
14979 let Some(issue) = self.commented_issue(task).await? else {
14980 return Ok(None);
14981 };
14982 if !self.comment_is_on(&issue, comment).await? {
14983 return Ok(None);
14984 }
14985 let data = self
14986 .graphql(graphql::DELETE_COMMENT, json!({"input":{"id":comment.0}}))
14987 .await?;
14988 // The payload says nothing about the comment it removed, so what is checked is that
14989 // GitHub answered the mutation at all rather than leaving it unanswered.
14990 data.get("deleteIssueComment")
14991 .filter(|value| !value.is_null())
14992 .ok_or_else(|| SourceError::Malformed {
14993 message: "GitHub comment deletion returned no payload".into(),
14994 })?;
14995 Ok(Some(comment.clone()))
14996 }
14997
14998 /// Every request this source has recorded, and what each of GitHub's two budgets was
14999 /// attributed — read off the same accounting the session report is rendered from, so
15000 /// the two cannot count one request two ways.
15001 async fn metering(&self) -> Result<Option<Metering>, SourceError> {
15002 Ok(Some(self.ledger.snapshot().metering()))
15003 }
15004
15005 /// Drop every item, search answer and board read this source holds, so the next command
15006 /// reads the board as a person has since left it.
15007 ///
15008 /// Every one of those is held on the assumption that nothing but this source writes the
15009 /// board while a command runs, which stops being true the moment the command is over: a
15010 /// body a person edited would be overwritten from the record held here, and a card they
15011 /// moved would be read as still where this source left it. The board's own field
15012 /// definitions go too, because a person can add or delete a `Status` option and a write
15013 /// resolved against the held list would not re-read on a miss. What stays is what stays
15014 /// valid in normal use: each repository's node id, which a miss re-reads, the pacing of
15015 /// mutations, which is about GitHub's limiter rather than anybody's work, and the running
15016 /// accounting [`metering`](TaskSource::metering) answers from.
15017 ///
15018 /// Infallible in practice: a lock an earlier failure poisoned is cleared rather than
15019 /// refused, because clearing it is what puts it right.
15020 async fn end_command(&self) -> Result<(), SourceError> {
15021 fn clear<T: Default>(held: &Mutex<T>) {
15022 *held
15023 .lock()
15024 .unwrap_or_else(std::sync::PoisonError::into_inner) = T::default();
15025 held.clear_poison();
15026 }
15027 clear(&self.created);
15028 clear(&self.updated);
15029 clear(&self.commented);
15030 clear(&self.board_cache);
15031 clear(&self.search_cache);
15032 clear(&self.narrowed_cache);
15033 clear(&self.search_next);
15034 clear(&self.resolved_cache);
15035 clear(&self.read_ahead);
15036 clear(&self.pending_origins);
15037 clear(&self.member_classes);
15038 clear(&self.carried);
15039 clear(&self.held_cache);
15040 clear(&self.children_cache);
15041 clear(&self.task_children);
15042 clear(&self.written);
15043 clear(&self.fields_cache);
15044 self.expected.end_command();
15045 Ok(())
15046 }
15047}
15048
15049/// One member of a project as a classification-only read answers it: its classification and
15050/// the repositories its work changes.
15051type MemberClass = (Classification, Vec<Repository>);
15052
15053/// Each project's sub-issues as one command read them, keyed by the selector they were asked
15054/// for under, beside the project that selector named; see `children_cache`.
15055type ProjectChildren = BTreeMap<NativeId, (NativeId, Vec<Resolved>)>;
15056
15057/// One issue comment as the contract carries it.
15058///
15059/// `author` is absent both when GitHub answers `null` for an account that no longer exists
15060/// and when it answers an actor with no login, because either way the source did not say who
15061/// wrote it — which is what an absent author means, rather than an author called nothing.
15062///
15063/// A comment a copy wrote here carries its origin as the hidden marker at the end of its body
15064/// — GitHub sets every comment's author and times itself, so there is nowhere else to keep it —
15065/// and is read back with the marker taken out and reported as its origin.
15066fn comment_from(value: &Value) -> Result<Comment, SourceError> {
15067 Ok(Comment {
15068 origin: None,
15069 id: NativeId(required_str(value, "id")?.to_owned()),
15070 author: optional_str(value.get("author").unwrap_or(&Value::Null), "login")?
15071 .map(str::to_owned),
15072 created_at: optional_time(value, "createdAt")?,
15073 updated_at: optional_time(value, "updatedAt")?,
15074 body: required_str(value, "body")?.to_owned(),
15075 url: optional_str(value, "url")?.map(str::to_owned),
15076 }
15077 .unmarked())
15078}
15079
15080/// The page of comments one issue node carries, resumed from `after`.
15081fn comment_page(
15082 node: &Value,
15083 issue: &str,
15084 after: Option<&str>,
15085) -> Result<Page<Comment>, SourceError> {
15086 let connection = node
15087 .get("comments")
15088 .filter(|value| !value.is_null())
15089 .ok_or_else(|| SourceError::Malformed {
15090 message: format!("GitHub issue {issue} answered with no comments connection"),
15091 })?;
15092 let items = optional_nodes(Some(connection), "issue comments")?
15093 .into_iter()
15094 .flatten()
15095 .map(comment_from)
15096 .collect::<Result<Vec<_>, _>>()?;
15097 let next = next_cursor(connection)?;
15098 if let Some(next) = &next {
15099 validate_cursor_progress(after, &next.0)?;
15100 }
15101 Ok(Page { items, next })
15102}
15103
15104/// The far ends of an issue's whole `blockedBy` connection, when the read carried it to its
15105/// end — `None` when it carried none, or a page with more past it.
15106fn carried_blocked_by(content: &Value) -> Result<Option<Vec<Value>>, SourceError> {
15107 let Some(connection) = content.get("blockedBy").filter(|value| !value.is_null()) else {
15108 return Ok(None);
15109 };
15110 if next_cursor(connection)?.is_some() {
15111 return Ok(None);
15112 }
15113 Ok(Some(
15114 optional_nodes(Some(connection), "blocked-by issues")?
15115 .into_iter()
15116 .flatten()
15117 .cloned()
15118 .collect(),
15119 ))
15120}
15121
15122/// Where the recorded tail of a dependency walk resumes; see
15123/// [`GitHubProjectsSource::recorded_edges`].
15124const RECORDED_CURSOR: &str = "onetaskgraph.depends_on:";
15125
15126/// The board text field this source keeps a copy's origin in.
15127///
15128/// Named after the key it holds, and held to that name by the guard below rather than by
15129/// a reader noticing.
15130const ORIGIN_FIELD: &str = "onetaskgraph.origin";
15131
15132/// The metadata key that field holds.
15133///
15134/// The engine owns this key and spells it once as `GlobalId::ORIGIN_KEY`; a plugin never
15135/// constructs or interprets the qualified id it carries. This source names it only to
15136/// route it — a short, typed value belongs in a typed field rather than in the body slot
15137/// a caller's own prose shares.
15138///
15139/// Restated rather than imported, because no plugin crate may depend on the engine. What
15140/// keeps the two spellings one contract is `scripts/check-origin-key-spelling.sh`, a
15141/// target in `check`: it reads the engine's own literal and fails naming the file and the
15142/// line when a plugin's parts from it either way. Drift here has one symptom — a copy
15143/// that creates a second item every run instead of finding the one it wrote — and that is
15144/// too late to learn it.
15145const ORIGIN_KEY: &str = "onetaskgraph.origin";
15146
15147/// Where a recorded tail resumes, refusing a cursor no walk in `direction` reported.
15148///
15149/// The reserved key holds forward edges and nothing else — the reverse of a recorded edge
15150/// is derived from the far end, never written down on the near item — so only a forward
15151/// walk ever reports one of these cursors. A reverse read carrying one is resuming a walk
15152/// it did not come from, and it is told so rather than answered with an empty page that
15153/// reads as a walk which ended.
15154fn recorded_offset(
15155 cursor: Option<&str>,
15156 direction: Direction,
15157) -> Result<Option<usize>, SourceError> {
15158 cursor
15159 .and_then(|cursor| cursor.strip_prefix(RECORDED_CURSOR))
15160 .map(|offset| {
15161 if direction != Direction::DependsOn {
15162 return Err(SourceError::Config {
15163 message: format!(
15164 "{RECORDED_CURSOR}{offset} resumes recorded forward edges, which a \
15165 reverse dependency read never issues; resume it in the direction \
15166 that reported it"
15167 ),
15168 });
15169 }
15170 offset.parse().map_err(|_| SourceError::Config {
15171 message: format!("{RECORDED_CURSOR}{offset} is not a recorded-edge cursor"),
15172 })
15173 })
15174 .transpose()
15175}
15176
15177/// What a dependency read's own answer says of a board draft: `None` when the item is not a
15178/// draft, or the answer did not carry its board item; `Some(None)` for a draft whose one board
15179/// item is not on this board, which this source does not hold; and `Some(Some(slot))`, the
15180/// draft's metadata, for one it does — its board item naming this board by its number and by
15181/// the id the same answer read of the board, as [`GitHubProjectsSource::draft_by_id`] decides.
15182fn draft_slot(
15183 data: &Value,
15184 id: &NativeId,
15185 project_number: u32,
15186) -> Result<Option<Option<BTreeMap<String, Value>>>, SourceError> {
15187 let Some(draft) = data
15188 .get("node")
15189 .filter(|node| node.get("__typename").and_then(Value::as_str) == Some("DraftIssue"))
15190 else {
15191 return Ok(None);
15192 };
15193 let Some(items) = data.pointer("/draft/projectV2Items") else {
15194 return Ok(None);
15195 };
15196 if required_str(draft, "id")? != id.0 {
15197 return Err(SourceError::Malformed {
15198 message: format!("GitHub answered a different draft for {}", id.0),
15199 });
15200 }
15201 let nodes = items
15202 .get("nodes")
15203 .and_then(Value::as_array)
15204 .ok_or_else(|| SourceError::Malformed {
15205 message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
15206 })?;
15207 let info = items
15208 .get("pageInfo")
15209 .ok_or_else(|| SourceError::Malformed {
15210 message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
15211 })?;
15212 if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
15213 return Err(SourceError::Malformed {
15214 message: format!(
15215 "GitHub draft {} reports more board items than the one GitHub links a draft to",
15216 id.0
15217 ),
15218 });
15219 }
15220 let board = data
15221 .pointer("/board/projectV2/id")
15222 .and_then(Value::as_str)
15223 .ok_or_else(|| SourceError::Malformed {
15224 message: format!(
15225 "GitHub answered draft {} without this board's own id beside it",
15226 id.0
15227 ),
15228 })?;
15229 let mut held = false;
15230 for node in nodes {
15231 let project = node.get("project").ok_or_else(|| SourceError::Malformed {
15232 message: format!("GitHub draft {} board item has no project", id.0),
15233 })?;
15234 let number = project
15235 .get("number")
15236 .and_then(Value::as_u64)
15237 .ok_or_else(|| SourceError::Malformed {
15238 message: format!(
15239 "GitHub draft {} board item has no numeric project number",
15240 id.0
15241 ),
15242 })?;
15243 let project_id = required_str(project, "id")?;
15244 held |= number == u64::from(project_number) && project_id == board;
15245 }
15246 if !held {
15247 return Ok(Some(None));
15248 }
15249 Ok(Some(Some(
15250 metadata_body(optional_str(draft, "body")?.map(str::to_owned))?.1,
15251 )))
15252}
15253
15254fn recorded_page(edges: Vec<DependencyEdge>, offset: usize, limit: usize) -> Page<DependencyEdge> {
15255 let mut page = offset_page(edges, offset, limit.max(1));
15256 page.next = page
15257 .next
15258 .map(|cursor| Cursor(format!("{RECORDED_CURSOR}{}", cursor.0)));
15259 page
15260}
15261
15262/// The kind of one issue reached through a dependency connection.
15263///
15264/// The same questions the board scan asks, over the fields the dependency document
15265/// selects, and in the same order: the design prefix first, then a sub-issue is a task,
15266/// then anything with sub-issues or the marker is a project.
15267///
15268/// # Errors
15269///
15270/// A far end this board holds as a document is refused rather than reported. The two
15271/// answers that are not refusals would both be wrong: reporting it as a task names an id
15272/// no task read of this source can find, and reporting it as a project names one no
15273/// project read can. There is no third value to return — `ItemKind` has no document
15274/// variant, because nothing may point at a document — so the relationship itself is what
15275/// the person is told about.
15276fn related_kind(value: &Value) -> Result<ItemKind, SourceError> {
15277 let id = required_str(value, "id")?;
15278 if required_str(value, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
15279 return Err(SourceError::Refused {
15280 message: format!(
15281 "GitHub issue {id} is a document of this board — its title begins \
15282 {DESIGN_TITLE_PREFIX:?} — and nothing may depend on a document or be depended \
15283 on by one; next: remove that issue's blocking relationship on this board"
15284 ),
15285 });
15286 }
15287 let parent = optional_str(value.get("parent").unwrap_or(&Value::Null), "id")?;
15288 if parent.is_some() {
15289 return Ok(ItemKind::Task);
15290 }
15291 let (_, slot) = metadata_body(optional_str(value, "body")?.map(str::to_owned))?;
15292 let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
15293 message: format!("GitHub issue {id}: {message}"),
15294 })?;
15295 let sub_issues = sub_issue_total(value)?;
15296 Ok(if sub_issues > 0 || marked == Some(ItemKind::Project) {
15297 ItemKind::Project
15298 } else {
15299 ItemKind::Task
15300 })
15301}
15302
15303/// The `stateInput` that closes an issue for `reason`.
15304fn closed_input(reason: ClosedState) -> Value {
15305 json!({"value":"CLOSED","stateReason":reason.reason()})
15306}
15307
15308/// The `IssueStateUpdateInput` one status target asks for.
15309///
15310/// `stateInput` and `state` are mutually exclusive on `UpdateIssueInput`, and only this
15311/// one is ever sent. A non-terminal status always asks for `OPEN`, which is what reopens
15312/// a currently-closed issue: without that the item would read back `Unknown` and a copy
15313/// would report a change forever. A document has no status at all, and asks for neither.
15314fn state_input(target: Option<&StatusTarget>) -> Value {
15315 match target {
15316 Some(StatusTarget::Terminal(_, reason)) => closed_input(*reason),
15317 Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => json!({"value":"OPEN"}),
15318 // A document has no status, so a write of one says nothing about the issue's open
15319 // or closed state rather than forcing it open: `stateInput` is what carries that
15320 // instruction, and an explicit null asks for no change to it.
15321 None => Value::Null,
15322 }
15323}
15324
15325/// The metadata one write stores in the item's body slot.
15326///
15327/// The typed fields travel as themselves, so the three reserved keys are rebuilt here
15328/// rather than carried: the kind marker so an empty project stays readable, the
15329/// repository list only when it is not exactly the issue's own repository, and the far
15330/// ends no relationship here can name.
15331///
15332/// The copy origin is the one typed field that is also mirrored here, and only as a
15333/// mirror: it lands in the board's origin field as well, which stays the one every reader
15334/// takes it from, and it is here so that GitHub's issue search — which indexes this comment
15335/// and catches up with a write in seconds rather than minutes — can find the item by it.
15336/// A reader of the release before this one drops the slot's copy and reads the field, so an
15337/// item written here still reads with exactly one origin there.
15338fn slot_metadata(
15339 incoming: &Incoming<'_>,
15340 own_repository: Option<&Repository>,
15341 fallback: &[DependencyEdge],
15342) -> BTreeMap<String, Value> {
15343 let mut metadata = incoming.metadata.clone();
15344 match metadata.remove(ORIGIN_KEY) {
15345 Some(Value::String(origin)) if !origin.is_empty() => {
15346 metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin));
15347 }
15348 _ => {}
15349 }
15350 match incoming.written.kind() {
15351 BoardKind::Work(kind) => metadata.insert(
15352 ItemKind::METADATA_KEY.to_owned(),
15353 Value::String(kind.marker().to_owned()),
15354 ),
15355 // A document is told by its title, so it carries no kind marker: that key names
15356 // what a dependency endpoint points at, and nothing may point at a document.
15357 BoardKind::Document => metadata.remove(ItemKind::METADATA_KEY),
15358 };
15359 incoming.classification.record(&mut metadata);
15360 let derivable = own_repository
15361 .map(|own| incoming.repositories == [own.clone()])
15362 .unwrap_or(incoming.repositories.is_empty());
15363 if derivable {
15364 metadata.remove(Repository::METADATA_KEY);
15365 } else {
15366 metadata.insert(
15367 Repository::METADATA_KEY.to_owned(),
15368 Value::Array(
15369 incoming
15370 .repositories
15371 .iter()
15372 .map(|repository| Value::String(repository.as_str().to_owned()))
15373 .collect(),
15374 ),
15375 );
15376 }
15377 // The typed lists are what land, whatever the caller's own metadata held under their
15378 // keys: a key of either name travelling beside the field would otherwise be a second
15379 // answer to the same question, and the field is the one the contract names.
15380 for (key, entries) in [
15381 (TaskRef::DELIVERS_KEY, incoming.delivers),
15382 (TaskRef::DELIVERED_BY_KEY, incoming.delivered_by),
15383 ] {
15384 set_task_list(&mut metadata, key, entries);
15385 }
15386 record_edges(&mut metadata, fallback);
15387 metadata
15388}
15389
15390/// Hold the far ends no relationship here can name under [`DependencyEdge::RECORDED_KEY`] in
15391/// one slot's metadata, or no such key when there are none.
15392fn record_edges(metadata: &mut BTreeMap<String, Value>, fallback: &[DependencyEdge]) {
15393 if fallback.is_empty() {
15394 metadata.remove(DependencyEdge::RECORDED_KEY);
15395 } else {
15396 metadata.insert(
15397 DependencyEdge::RECORDED_KEY.to_owned(),
15398 Value::Array(
15399 fallback
15400 .iter()
15401 .map(|edge| {
15402 serde_json::to_value(&edge.to).expect("an endpoint renders as JSON")
15403 })
15404 .collect(),
15405 ),
15406 );
15407 }
15408}
15409
15410/// Every label one item carries, from its content's own connection and nowhere else.
15411///
15412/// There is no second place to read one from: no document this source sends selects the
15413/// board's built-in `Labels` field, because GitHub derives it from the content and a draft
15414/// cannot carry one at all. The module documentation records the three schema facts that
15415/// settle it.
15416fn labels(content: &Value) -> Result<Vec<Label>, SourceError> {
15417 optional_nodes(content.get("labels"), "content labels")?
15418 .into_iter()
15419 .flatten()
15420 .map(|v| {
15421 Ok(Label {
15422 id: NativeId(required_str(v, "id")?.to_owned()),
15423 name: required_str(v, "name")?.to_owned(),
15424 color: optional_str(v, "color")?.map(str::to_owned),
15425 })
15426 })
15427 .collect()
15428}
15429
15430/// Each board field value one item holds, as the change token's facets name it: the field's
15431/// name, with the single-select option's name or the text. A value of a type no read here
15432/// selects, and one naming no field, says nothing a person can rename and is left out.
15433fn facet_fields(field_values: &[Value]) -> Vec<(String, String)> {
15434 field_values
15435 .iter()
15436 .filter_map(|value| {
15437 let field = value.pointer("/field/name")?.as_str()?;
15438 let held = value.get("name").or_else(|| value.get("text"))?.as_str()?;
15439 Some((field.to_owned(), held.to_owned()))
15440 })
15441 .collect()
15442}
15443
15444/// The digest of the facets of the issue `content`, whose board item's values are
15445/// `field_values` — or `None` when the response did not carry, whole, every member they are read
15446/// from: the title, the parent, the sub-issue summary and the body its kind is read from, every
15447/// label, and every field value.
15448///
15449/// What a write's own response spells its token from, by exactly the rules a read takes them by.
15450fn facets_of(content: &Value, field_values: &Value) -> Option<change_token::FacetsDigest> {
15451 let ended = |connection: &Value| {
15452 connection.pointer("/pageInfo/hasNextPage") == Some(&Value::Bool(false))
15453 };
15454 let labels = content.get("labels").filter(|labels| ended(labels))?;
15455 if !ended(field_values) {
15456 return None;
15457 }
15458 let parent = content.get("parent")?;
15459 let (_, slot) = metadata_body(content.get("body")?.as_str().map(str::to_owned)).ok()?;
15460 let kind = BoardKind::of(
15461 content.get("title")?.as_str()?,
15462 !parent.is_null(),
15463 sub_issue_total(content).ok()?,
15464 ItemKind::from_metadata(&slot).ok()?,
15465 );
15466 let labels = self::labels(&json!({"labels": labels})).ok()?;
15467 let nodes = field_values.get("nodes")?.as_array()?;
15468 Some(change_token::Facets::new(kind.facet(), facet_fields(nodes), &labels).digest())
15469}
15470
15471/// The definition of each board field one item's values are values of, in the shape a read
15472/// of the board's own `fields` gives one.
15473///
15474/// A value names its field through a fragment on that field's own type, so the type is
15475/// known from which kind of value it is: a single-select value's field is a
15476/// `ProjectV2SingleSelectField`, options and all, and a text value's is a `ProjectV2Field`.
15477/// A value whose field carried no id, or an empty one, says nothing usable and is left out.
15478fn field_definitions(field_values: &[Value]) -> Vec<Value> {
15479 field_values
15480 .iter()
15481 .filter_map(|value| {
15482 let field = value.get("field")?.as_object()?;
15483 field.get("id")?.as_str().filter(|id| !id.is_empty())?;
15484 let typename = if value.get("text").is_some() {
15485 "ProjectV2Field"
15486 } else if value.get("name").is_some() {
15487 "ProjectV2SingleSelectField"
15488 } else {
15489 return None;
15490 };
15491 let mut defined = field.clone();
15492 defined.insert("__typename".to_owned(), json!(typename));
15493 // Only a text field holds a text value, so the value says the field's type too.
15494 if typename == "ProjectV2Field" {
15495 defined.insert("dataType".to_owned(), json!("TEXT"));
15496 }
15497 Some(Value::Object(defined))
15498 })
15499 .collect()
15500}
15501
15502fn text_field(field_values: &[Value], name: &str) -> Result<Option<String>, SourceError> {
15503 let Some(node) = field_values
15504 .iter()
15505 .find(|node| node.pointer("/field/name").and_then(Value::as_str) == Some(name))
15506 else {
15507 return Ok(None);
15508 };
15509 Ok(optional_str(node, "text")?.map(str::to_owned))
15510}
15511
15512fn valid_github_owner(owner: &str) -> bool {
15513 !owner.is_empty()
15514 && owner.len() <= 39
15515 && !owner.starts_with('-')
15516 && !owner.ends_with('-')
15517 && !owner.contains("--")
15518 && owner
15519 .bytes()
15520 .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
15521}
15522
15523/// GitHub's repository-name grammar: 1-100 ASCII letters, digits, `-`, `_` or `.`, and
15524/// neither of the two names a path segment already means.
15525fn valid_github_repository_name(name: &str) -> bool {
15526 !name.is_empty()
15527 && name.len() <= 100
15528 && name != "."
15529 && name != ".."
15530 && name
15531 .bytes()
15532 .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
15533}
15534
15535fn valid_environment_name(name: &str) -> bool {
15536 let mut bytes = name.bytes();
15537 bytes
15538 .next()
15539 .is_some_and(|byte| byte.is_ascii_alphabetic() || byte == b'_')
15540 && bytes.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
15541}
15542
15543/// How many sub-issues one issue has.
15544///
15545/// `Issue.subIssuesSummary` is `SubIssuesSummary!` and its `total` is `Int!`, so an
15546/// absent or non-integer one is a response this source cannot read — and reading it as
15547/// zero would classify a project as a task, which is exactly the mistake the marker
15548/// exists to keep from happening quietly.
15549fn sub_issue_total(issue: &Value) -> Result<u64, SourceError> {
15550 let summary = issue
15551 .get("subIssuesSummary")
15552 .ok_or_else(|| SourceError::Malformed {
15553 message: "GitHub issue is missing subIssuesSummary".into(),
15554 })?;
15555 summary
15556 .get("total")
15557 .and_then(Value::as_u64)
15558 .ok_or_else(|| SourceError::Malformed {
15559 message: "GitHub issue subIssuesSummary.total is not an unsigned integer".into(),
15560 })
15561}
15562
15563/// One issue's own `number`.
15564///
15565/// An issue always has one: GitHub declares `Issue.number` as `Int!` and every selection of
15566/// an issue in this module asks for it. So a read of one that comes back without it, or
15567/// with something that is not an unsigned integer, is a response this source cannot read —
15568/// absence here is **not** "this issue has no number". A draft is the content that has
15569/// none, and a draft never reaches this: the caller decides on `__typename` first, the way
15570/// it does for `subIssuesSummary`, which `DraftIssue` equally declares nothing for.
15571fn issue_number(issue: &Value) -> Result<u64, SourceError> {
15572 issue
15573 .get("number")
15574 .and_then(Value::as_u64)
15575 .ok_or_else(|| SourceError::Malformed {
15576 message: "GitHub issue number is missing or is not an unsigned integer".into(),
15577 })
15578}
15579
15580/// The `number` a creating mutation answered with, and `None` when it answered without one;
15581/// why a missing one is tolerated is at the call in `create_and_file_issue`.
15582fn created_issue_number(created: &Value) -> Result<Option<u64>, SourceError> {
15583 match created.get("number") {
15584 None | Some(Value::Null) => Ok(None),
15585 Some(value) => value
15586 .as_u64()
15587 .map(Some)
15588 .ok_or_else(|| SourceError::Malformed {
15589 message: "GitHub created issue number is not an unsigned integer".into(),
15590 }),
15591 }
15592}
15593
15594fn required_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
15595 value
15596 .get(field)
15597 .and_then(Value::as_str)
15598 .ok_or_else(|| SourceError::Malformed {
15599 message: format!("GitHub response is missing string field {field}"),
15600 })
15601}
15602
15603fn required_nonblank_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
15604 let found = required_str(value, field)?;
15605 if found.trim().is_empty() {
15606 return Err(SourceError::Malformed {
15607 message: format!("GitHub response has blank string field {field}"),
15608 });
15609 }
15610 Ok(found)
15611}
15612
15613/// The slot's delimiters, which `docs/metadata.md` settles once for every source that
15614/// needs one — Linear spells them too, in its own description field.
15615///
15616/// Restated rather than shared, because a plugin crate depends on the contract crate and
15617/// nothing else of this workspace. `scripts/check-metadata-slot-encoding.sh`, a target in
15618/// `check`, is what keeps the two one encoding: drift is otherwise quiet, since each
15619/// source round-trips its own writes perfectly well under its own spelling.
15620const METADATA_OPEN: &str = "<!-- onetaskgraph.metadata\n";
15621const METADATA_CLOSE: &str = "\n-->";
15622
15623/// What the composer puts between a non-empty visible body and the slot, and the one thing
15624/// the parser takes off the visible body when it takes the slot off — exactly once, so every
15625/// other trailing byte of the body comes back as it was written.
15626// 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.
15627const METADATA_SEPARATOR: &str = "\n\n";
15628
15629/// The visible body and the metadata slot at the end of it.
15630///
15631/// The encoding is the one `docs/metadata.md` settles for Linear, which is where its
15632/// reasons are. Only a comment at the very end is a slot; one in the middle is a person's
15633/// own content and is left alone. The visible body is everything before the slot less the
15634/// one [`METADATA_SEPARATOR`] the composer put there, byte for byte.
15635fn metadata_body(
15636 body: Option<String>,
15637) -> Result<(Option<String>, BTreeMap<String, Value>), SourceError> {
15638 let Some(body) = body else {
15639 return Ok((None, BTreeMap::new()));
15640 };
15641 let Some(slot) = slot_span(&body)? else {
15642 return Ok((Some(body), BTreeMap::new()));
15643 };
15644 let metadata =
15645 serde_json::from_str(&body[slot.encoded_start..slot.encoded_end]).map_err(|error| {
15646 SourceError::Malformed {
15647 message: format!(
15648 "invalid canonical JSON in GitHub issue onetaskgraph metadata slot: {error}"
15649 ),
15650 }
15651 })?;
15652 let before = &body[..slot.start];
15653 let visible = before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before);
15654 Ok(((!visible.is_empty()).then(|| visible.to_owned()), metadata))
15655}
15656
15657/// Where the metadata slot sits in one body, as byte offsets into it.
15658struct SlotSpan {
15659 /// Where [`METADATA_OPEN`] begins.
15660 start: usize,
15661 /// Where the encoded JSON begins, just past [`METADATA_OPEN`].
15662 encoded_start: usize,
15663 /// Where the encoded JSON ends, at the start of [`METADATA_CLOSE`].
15664 encoded_end: usize,
15665 /// Just past [`METADATA_CLOSE`].
15666 end: usize,
15667}
15668
15669/// The slot at the very end of `body`, or `None` when it has none.
15670///
15671/// The one reading of *where the slot is*, shared by [`metadata_body`], which reads it, and
15672/// [`with_slot`], which rewrites it — so the two cannot disagree about which comment is the
15673/// slot.
15674fn slot_span(body: &str) -> Result<Option<SlotSpan>, SourceError> {
15675 let Some(start) = body.rfind(METADATA_OPEN) else {
15676 return Ok(None);
15677 };
15678 let encoded_start = start + METADATA_OPEN.len();
15679 let Some(relative_end) = body[encoded_start..].find(METADATA_CLOSE) else {
15680 return Err(SourceError::Malformed {
15681 message: "unterminated onetaskgraph metadata slot in GitHub issue body".into(),
15682 });
15683 };
15684 let encoded_end = encoded_start + relative_end;
15685 let end = encoded_end + METADATA_CLOSE.len();
15686 if !body[end..].trim().is_empty() {
15687 return Ok(None);
15688 }
15689 Ok(Some(SlotSpan {
15690 start,
15691 encoded_start,
15692 encoded_end,
15693 end,
15694 }))
15695}
15696
15697/// `body` with its metadata slot holding exactly `metadata`, and every byte outside the
15698/// slot as it was.
15699///
15700/// A slot that is there has its JSON replaced in place; one that becomes empty is removed
15701/// together with the one [`METADATA_SEPARATOR`] separating it from the prose before it. A
15702/// body with no slot gains one the way [`compose_body`] writes it — after that separator,
15703/// or alone in an empty body — and a body with no slot that is given no metadata is
15704/// returned as it is.
15705fn with_slot(body: &str, metadata: &BTreeMap<String, Value>) -> Result<String, SourceError> {
15706 let encoded = if metadata.is_empty() {
15707 None
15708 } else {
15709 Some(
15710 serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
15711 message: error.to_string(),
15712 })?,
15713 )
15714 };
15715 Ok(match (slot_span(body)?, encoded) {
15716 (Some(slot), Some(encoded)) => format!(
15717 "{}{encoded}{}",
15718 &body[..slot.encoded_start],
15719 &body[slot.encoded_end..]
15720 ),
15721 (Some(slot), None) => {
15722 let before = &body[..slot.start];
15723 format!(
15724 "{}{}",
15725 before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before),
15726 &body[slot.end..]
15727 )
15728 }
15729 (None, None) => body.to_owned(),
15730 (None, Some(encoded)) if body.is_empty() => {
15731 format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15732 }
15733 (None, Some(encoded)) => {
15734 format!("{body}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15735 }
15736 })
15737}
15738
15739/// `body` with everything before its metadata slot replaced by `content`, and the slot
15740/// itself kept byte for byte.
15741///
15742/// The inverse of how [`metadata_body`] splits a body: the slot, when there is one, follows
15743/// `content` after the one [`METADATA_SEPARATOR`] the composer puts there — or alone, when
15744/// `content` is empty — so a read of the result reports `content` as the visible body and
15745/// the slot's metadata exactly as it was.
15746fn with_content(body: &str, content: &str) -> Result<String, SourceError> {
15747 let Some(slot) = slot_span(body)? else {
15748 return Ok(content.to_owned());
15749 };
15750 let kept = &body[slot.start..];
15751 Ok(if content.is_empty() {
15752 kept.to_owned()
15753 } else {
15754 format!("{content}{METADATA_SEPARATOR}{kept}")
15755 })
15756}
15757
15758/// Hold `entries` under `key` in one slot's metadata, or no such key when there are none.
15759fn set_task_list(metadata: &mut BTreeMap<String, Value>, key: &str, entries: &[TaskRef]) {
15760 if entries.is_empty() {
15761 metadata.remove(key);
15762 } else {
15763 metadata.insert(
15764 key.to_owned(),
15765 Value::Array(
15766 entries
15767 .iter()
15768 .map(|entry| Value::String(entry.as_str().to_owned()))
15769 .collect(),
15770 ),
15771 );
15772 }
15773}
15774
15775fn compose_body(
15776 content: Option<&str>,
15777 metadata: &BTreeMap<String, Value>,
15778) -> Result<Option<String>, SourceError> {
15779 let visible = content.unwrap_or_default();
15780 if metadata.is_empty() {
15781 return Ok((!visible.is_empty()).then(|| visible.to_owned()));
15782 }
15783 let encoded = serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
15784 message: error.to_string(),
15785 })?;
15786 Ok(Some(if visible.is_empty() {
15787 format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15788 } else {
15789 format!("{visible}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15790 }))
15791}
15792
15793fn required_bool(value: &Value, field: &str) -> Result<bool, SourceError> {
15794 value
15795 .get(field)
15796 .and_then(Value::as_bool)
15797 .ok_or_else(|| SourceError::Malformed {
15798 message: format!("GitHub response is missing boolean field {field}"),
15799 })
15800}
15801fn optional_str<'a>(value: &'a Value, field: &str) -> Result<Option<&'a str>, SourceError> {
15802 match value.get(field) {
15803 None | Some(Value::Null) => Ok(None),
15804 Some(value) => value
15805 .as_str()
15806 .map(Some)
15807 .ok_or_else(|| SourceError::Malformed {
15808 message: format!("GitHub response field {field} is not a string or null"),
15809 }),
15810 }
15811}
15812fn optional_nodes<'a>(
15813 connection: Option<&'a Value>,
15814 name: &str,
15815) -> Result<Option<&'a Vec<Value>>, SourceError> {
15816 match connection {
15817 None | Some(Value::Null) => Ok(None),
15818 Some(value) => value
15819 .get("nodes")
15820 .and_then(Value::as_array)
15821 .map(Some)
15822 .ok_or_else(|| SourceError::Malformed {
15823 message: format!("GitHub {name}.nodes is not an array"),
15824 }),
15825 }
15826}
15827fn complete_connection(connection: &Value, name: &str, size: u32) -> Result<(), SourceError> {
15828 let page_info = connection
15829 .get("pageInfo")
15830 .ok_or_else(|| SourceError::Malformed {
15831 message: format!("GitHub {name} has no pageInfo"),
15832 })?;
15833 if required_bool(page_info, "hasNextPage")? {
15834 return Err(SourceError::Malformed {
15835 message: format!(
15836 "GitHub {name} exceeds the supported nested connection size of {size}"
15837 ),
15838 });
15839 }
15840 Ok(())
15841}
15842fn optional_time(value: &Value, field: &str) -> Result<Option<DateTime<Utc>>, SourceError> {
15843 optional_str(value, field)?
15844 .map(|timestamp| {
15845 timestamp.parse().map_err(|error| SourceError::Malformed {
15846 message: format!("GitHub response field {field} is not a timestamp: {error}"),
15847 })
15848 })
15849 .transpose()
15850}
15851fn validate_page(page: &PageRequest) -> Result<(), SourceError> {
15852 if page.limit == 0 {
15853 Err(SourceError::Config {
15854 message: "page limit must be at least 1".into(),
15855 })
15856 } else {
15857 Ok(())
15858 }
15859}
15860fn next_cursor(connection: &Value) -> Result<Option<Cursor>, SourceError> {
15861 let page = connection
15862 .get("pageInfo")
15863 .filter(|value| value.is_object())
15864 .ok_or_else(|| SourceError::Malformed {
15865 message: "GitHub connection is missing pageInfo".into(),
15866 })?;
15867 if required_bool(page, "hasNextPage")? {
15868 let cursor = required_str(page, "endCursor")?;
15869 validate_cursor_progress(None, cursor)?;
15870 Ok(Some(Cursor(cursor.into())))
15871 } else {
15872 Ok(None)
15873 }
15874}
15875fn validate_cursor_progress(previous: Option<&str>, next: &str) -> Result<(), SourceError> {
15876 if next.is_empty() || previous == Some(next) {
15877 Err(SourceError::Malformed {
15878 message: "GitHub pagination cursor is empty or did not advance".into(),
15879 })
15880 } else {
15881 Ok(())
15882 }
15883}
15884/// The version of this plugin's opaque narrowing-search cursor.
15885pub const SEARCH_CURSOR_VERSION: u32 = 4;
15886
15887#[derive(Serialize, Deserialize)]
15888#[serde(tag = "state", rename_all = "snake_case", deny_unknown_fields)]
15889enum SearchConnection {
15890 Initial {},
15891 Continuing { after: Cursor },
15892 Exhausted {},
15893}
15894impl SearchConnection {
15895 fn after(&self) -> Option<&str> {
15896 match self {
15897 Self::Continuing { after } => Some(&after.0),
15898 _ => None,
15899 }
15900 }
15901 fn exhausted(&self) -> bool {
15902 matches!(self, Self::Exhausted { .. })
15903 }
15904 /// Whether a cursor naming this position, `offset` rows into its page, is one this
15905 /// plugin could have handed out: a page is resumed only part of the way through it — an
15906 /// offset of a whole page or more would skip rows nobody was given — an initial page
15907 /// only once some of it was handed out, and an exhausted connection has no page to be
15908 /// part of the way through.
15909 fn valid_resume(&self, offset: usize) -> bool {
15910 let within = offset < SEARCH_PAGE_SIZE as usize;
15911 match self {
15912 Self::Initial { .. } => offset > 0 && within,
15913 Self::Continuing { after } => !after.0.is_empty() && within,
15914 Self::Exhausted { .. } => offset == 0,
15915 }
15916 }
15917}
15918
15919/// Versioned source cursor. A zero offset and empty own-write ids are omitted.
15920#[derive(Serialize, Deserialize)]
15921#[serde(deny_unknown_fields)]
15922struct SearchPosition {
15923 version: u32,
15924 connection: SearchConnection,
15925 /// How many rows of the page `connection` starts were already handed out.
15926 #[serde(default, skip_serializing_if = "is_zero")]
15927 offset: usize,
15928 #[serde(default, skip_serializing_if = "Vec::is_empty")]
15929 seen: Vec<NativeId>,
15930 #[serde(default, skip_serializing_if = "Vec::is_empty")]
15931 own: Vec<NativeId>,
15932}
15933impl Default for SearchPosition {
15934 fn default() -> Self {
15935 Self {
15936 version: SEARCH_CURSOR_VERSION,
15937 connection: SearchConnection::Initial {},
15938 offset: 0,
15939 seen: Vec::new(),
15940 own: Vec::new(),
15941 }
15942 }
15943}
15944
15945fn is_zero(offset: &usize) -> bool {
15946 *offset == 0
15947}
15948
15949fn numeric_cursor(cursor: Option<&Cursor>) -> Result<usize, SourceError> {
15950 cursor.map_or(Ok(0), |c| {
15951 c.0.parse().map_err(|_| SourceError::Config {
15952 message: "page cursor is invalid".into(),
15953 })
15954 })
15955}
15956fn offset_page<T>(mut items: Vec<T>, offset: usize, limit: usize) -> Page<T> {
15957 if offset > items.len() {
15958 return Page::last(vec![]);
15959 }
15960 let tail = items.split_off(offset);
15961 let mut selected = tail;
15962 let next = (selected.len() > limit).then(|| Cursor((offset + limit).to_string()));
15963 selected.truncate(limit);
15964 Page {
15965 items: selected,
15966 next,
15967 }
15968}
15969
15970impl GitHubProjectsSource {
15971 async fn write_task_assets(
15972 &self,
15973 write: &ItemWrite<Task>,
15974 assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
15975 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
15976 self.write_item(
15977 &self.task_incoming(write, assets)?,
15978 write.target.as_ref(),
15979 &write.depends_on,
15980 )
15981 .await
15982 }
15983
15984 /// One task write as [`Self::write_item`] takes it, once what it delivers and its priority
15985 /// are known to be ones this source can write.
15986 fn task_incoming<'a>(
15987 &self,
15988 write: &'a ItemWrite<Task>,
15989 assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
15990 ) -> Result<Incoming<'a>, SourceError> {
15991 let near = write.target.as_ref().unwrap_or(&write.item.id);
15992 for (key, entries) in [
15993 (TaskRef::DELIVERS_KEY, &write.item.delivers),
15994 (TaskRef::DELIVERED_BY_KEY, &write.item.delivered_by),
15995 ] {
15996 TaskRef::listed(key, near, Some(&self.name), entries.clone())
15997 .map_err(|message| SourceError::Refused { message })?;
15998 }
15999 if self.priorities.is_none() && write.item.priority != Priority::None {
16000 return Err(self.holds_no_priority());
16001 }
16002 Ok(Incoming {
16003 written: Written::Work(ItemKind::Task, &write.item.status),
16004 title: &write.item.title,
16005 content: write.item.content.as_deref(),
16006 assets,
16007 labels: &write.item.labels,
16008 metadata: &write.item.metadata,
16009 repositories: &write.item.repositories,
16010 classification: write.item.classification,
16011 parent: write.item.project.as_ref(),
16012 delivers: &write.item.delivers,
16013 delivered_by: &write.item.delivered_by,
16014 priority: self.priorities.as_ref().map(|_| write.item.priority),
16015 })
16016 }
16017 async fn write_document_assets(
16018 &self,
16019 write: &ItemWrite<Document>,
16020 assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
16021 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
16022 // A document takes part in no dependency graph, so there is no far end to write
16023 // natively and none to record: a caller naming one is told so rather than having it
16024 // stored under the reserved key, where a later read would report an edge the
16025 // contract says cannot exist.
16026 if !write.depends_on.is_empty() {
16027 return Err(SourceError::Refused {
16028 message: format!(
16029 "this write names {} dependencies for a document, and a document takes \
16030 part in no dependency graph; next: put the dependency on the task or \
16031 project the document is about",
16032 write.depends_on.len()
16033 ),
16034 });
16035 }
16036 self.write_item(
16037 &Incoming {
16038 written: Written::Document,
16039 title: &write.item.title,
16040 content: write.item.content.as_deref(),
16041 assets,
16042 labels: &write.item.labels,
16043 metadata: &write.item.metadata,
16044 repositories: &write.item.repositories,
16045 classification: write.item.classification,
16046 parent: write.item.project.as_ref(),
16047 delivers: &[],
16048 delivered_by: &[],
16049 priority: None,
16050 },
16051 write.target.as_ref(),
16052 &[],
16053 )
16054 .await
16055 }
16056}
16057
16058/// What [`TaskSource::end_command`] leaves of this source's held state, asserted on the state
16059/// itself, for the two things no journey can observe.
16060///
16061/// The journeys in `crates/onetaskgraph-e2e/tests/e2e/end_command.rs` prove through the engine,
16062/// with and without the call, that a settlement, a board listing and a metadata search each
16063/// read afresh after it — the resolved records, the written-item overlay, the board and its
16064/// search, and the narrowed searches. What they cannot reach is the held field definitions,
16065/// because a status write naming an option a person deleted is refused the same whether or
16066/// not the list is held, and a poisoned lock, because nothing outside the source can panic
16067/// while one of its locks is held. So these assert those directly, and every other holder
16068/// beside them so a holder added later without a clear in the call fails here.
16069#[cfg(test)]
16070mod end_command_tests {
16071 use super::*;
16072
16073 struct Token;
16074
16075 impl SecretResolver for Token {
16076 fn get(&self, var: &str) -> Option<SecretString> {
16077 (var == "GH_PROJECTS_TOKEN").then(|| "test-token".into())
16078 }
16079 }
16080
16081 fn source() -> GitHubProjectsSource {
16082 let config = serde_json::from_value(json!({
16083 "owner": "octo-org", "project_number": 7, "repository": "acme/work",
16084 // Nothing here is sent: the source is only built and its state inspected.
16085 "endpoint": "http://127.0.0.1:9/graphql",
16086 }))
16087 .expect("a usable configuration");
16088 GitHubProjectsSource::new(&SourceName::new("work").unwrap(), config, &Token)
16089 .expect("the source builds")
16090 }
16091
16092 /// One issue as a board read answers it.
16093 fn resolved(source: &GitHubProjectsSource) -> Resolved {
16094 source
16095 .resolve(&json!({
16096 "id": "ITEM-1",
16097 "content": {"__typename": "Issue", "id": "I_1", "title": "Held",
16098 "body": "what a person may since have edited", "state": "OPEN",
16099 "stateReason": null, "url": null, "number": 1,
16100 "subIssuesSummary": {"total": 0},
16101 "labels": {"nodes": [], "pageInfo": {"hasNextPage": false}}},
16102 "fieldValues": {"nodes": [], "pageInfo": {"hasNextPage": false}},
16103 }))
16104 .expect("the item reads")
16105 .expect("an issue")
16106 }
16107
16108 /// Hold something in every holder the call clears, and the repository id it keeps.
16109 fn fill(source: &GitHubProjectsSource) {
16110 let item = resolved(source);
16111 source.created.lock().unwrap().push(item.clone());
16112 source.updated.lock().unwrap().push(item.clone());
16113 *source.board_cache.lock().unwrap() = Some(Board {
16114 id: "PVT-board".into(),
16115 fields: json!({"nodes": []}),
16116 items: vec![item.clone()],
16117 });
16118 *source.search_cache.lock().unwrap() = Some(vec![item.clone()]);
16119 source
16120 .narrowed_cache
16121 .lock()
16122 .unwrap()
16123 .insert("status:todo".into(), vec![item.clone()]);
16124 source.children_cache.lock().unwrap().insert(
16125 NativeId("P-1".into()),
16126 (NativeId("P-1".into()), vec![item.clone()]),
16127 );
16128 source
16129 .search_next
16130 .lock()
16131 .unwrap()
16132 .insert("status:todo".into(), Some("cursor".into()));
16133 source
16134 .resolved_cache
16135 .lock()
16136 .unwrap()
16137 .insert(item.id.clone(), item);
16138 *source.fields_cache.lock().unwrap() = Some(BoardFields {
16139 id: BoardId::parse("PVT-board").unwrap(),
16140 fields: json!({"nodes": []}),
16141 });
16142 source
16143 .repository_cache
16144 .lock()
16145 .unwrap()
16146 .insert(RepositoryTarget::parse("acme/work").unwrap(), "R_1".into());
16147 }
16148
16149 fn assert_dropped(source: &GitHubProjectsSource) {
16150 assert!(source.created().unwrap().is_empty(), "created");
16151 assert!(source.updated().unwrap().is_empty(), "updated");
16152 assert!(source.board_cache().unwrap().is_none(), "board");
16153 assert!(source.search_cache.lock().unwrap().is_none(), "search");
16154 assert!(source.narrowed_cache.lock().unwrap().is_empty(), "narrowed");
16155 assert!(
16156 source.children_cache.lock().unwrap().is_empty(),
16157 "project children"
16158 );
16159 assert!(
16160 source.search_next.lock().unwrap().is_empty(),
16161 "search paging"
16162 );
16163 assert!(
16164 source.resolved_cache().unwrap().is_empty(),
16165 "resolved records"
16166 );
16167 assert!(source.fields_cache().unwrap().is_none(), "board fields");
16168 assert_eq!(
16169 source.repository_cache().unwrap().len(),
16170 1,
16171 "a repository's node id stays valid and is kept"
16172 );
16173 }
16174
16175 fn end(source: &GitHubProjectsSource) {
16176 tokio::runtime::Builder::new_current_thread()
16177 .build()
16178 .unwrap()
16179 .block_on(source.end_command())
16180 .expect("the command ends");
16181 }
16182
16183 #[test]
16184 fn the_call_drops_every_item_search_and_board_read_and_keeps_repository_ids() {
16185 let source = source();
16186 fill(&source);
16187 end(&source);
16188 assert_dropped(&source);
16189 }
16190
16191 #[test]
16192 fn the_call_clears_a_lock_an_earlier_failure_poisoned() {
16193 fn poison<T: Send>(held: &Mutex<T>) {
16194 std::thread::scope(|scope| {
16195 let _ = scope
16196 .spawn(|| {
16197 let _guard = held.lock().unwrap();
16198 panic!("a failure while the lock is held");
16199 })
16200 .join();
16201 });
16202 assert!(held.is_poisoned());
16203 }
16204 let source = source();
16205 fill(&source);
16206 poison(&source.created);
16207 poison(&source.updated);
16208 poison(&source.board_cache);
16209 poison(&source.search_cache);
16210 poison(&source.narrowed_cache);
16211 poison(&source.search_next);
16212 poison(&source.resolved_cache);
16213 poison(&source.fields_cache);
16214 assert!(
16215 source.resolved_cache().is_err(),
16216 "a poisoned lock is refused before the call"
16217 );
16218 end(&source);
16219 assert_dropped(&source);
16220 }
16221}
16222
16223#[cfg(test)]
16224mod unprojectable_fields_tests {
16225 use super::*;
16226
16227 /// The field names `docs/metadata.md` says a `metadata_fields` entry may not name.
16228 fn documented() -> std::collections::BTreeSet<String> {
16229 let document = std::fs::read_to_string(
16230 std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../docs/metadata.md"),
16231 )
16232 .expect("docs/metadata.md is readable");
16233 let document = document.split_whitespace().collect::<Vec<_>>().join(" ");
16234 let lead = "a `field` equal, ignoring case, to ";
16235 let start = document.find(lead).expect("the refused-field sentence") + lead.len();
16236 let sentence = &document[start..];
16237 let sentence = &sentence[..sentence
16238 .find("; and two entries naming one field")
16239 .expect("the refused-field sentence ends at the duplicate rule")];
16240 let (own, github) = sentence
16241 .split_once(" or a field GitHub owns on every board (")
16242 .expect("the sentence names GitHub's own fields");
16243 own.split('`')
16244 .skip(1)
16245 .step_by(2)
16246 .chain(
16247 github
16248 .strip_suffix(')')
16249 .expect("GitHub's own fields close the sentence")
16250 .split(", "),
16251 )
16252 .map(str::to_owned)
16253 .collect()
16254 }
16255
16256 #[test]
16257 fn the_documented_refused_fields_are_exactly_the_ones_the_configuration_refuses() {
16258 let refused: std::collections::BTreeSet<String> = UNPROJECTABLE_FIELDS
16259 .iter()
16260 .map(|&name| name.to_owned())
16261 .collect();
16262 assert_eq!(documented(), refused, "the fields docs/metadata.md lists");
16263 let instance = SourceName::new("work").unwrap();
16264 for name in UNPROJECTABLE_FIELDS {
16265 for spelled in [name.to_owned(), name.to_uppercase()] {
16266 let entry = MetadataFieldConfig {
16267 field: spelled.clone(),
16268 key: "team.machine".to_owned(),
16269 path: Vec::new(),
16270 };
16271 let refused = MetadataField::resolve(vec![entry], &instance)
16272 .expect_err("an unprojectable field is refused");
16273 assert!(
16274 refused.to_string().contains(&format!("{name:?}")),
16275 "{spelled}: {refused}"
16276 );
16277 }
16278 }
16279 }
16280}