onetaskgraph_github_projects/lib.rs
1//! A stateless onetaskgraph source over one GitHub Projects v2 board.
2//!
3//! **A board is a container of projects, not a project.** Its own `title`,
4//! `shortDescription` and `readme` are never read as an item's fields and are never
5//! written: nothing in this source can rename the board a user configured.
6//!
7//! **A project is an issue and its tasks are that issue's sub-issues.** GitHub's schema
8//! decides that: `Issue` exposes `parent`, `subIssues` and `subIssuesSummary`, and
9//! `DraftIssue` exposes none of them. Creating an issue needs a `repositoryId`, and a
10//! board has none, so a write without [`GitHubProjectsConfig::repository`] is refused
11//! naming the field — but that repository is the *fallback*, not the home of every item.
12//!
13//! <!-- llmlint: ignore-block[contracts_have_one_source_or_a_drift_gate] The rule's one
14//! executable source is `GitHubProjectsSource::creation_target`; this is where a reader of
15//! the module meets it, and `tests/plugin.rs` drives every arm below against the loopback
16//! board and asserts on `createIssue`'s own `repositoryId`, so the prose cannot outlive a
17//! change to the rule. -->
18//! **Which repository an issue is created in is decided by the item's own `repositories`
19//! field, under one rule.** Exactly one entry names the repository the issue is created in:
20//! a task issue is where a person finds the work from the repository it changes, and one
21//! filed in a board's nominated repository is invisible from every other. Zero entries, or
22//! two or more, name none, so a task's or a document's issue is created in the repository
23//! its parent project's issue lives in — read from the board, or from this process's own
24//! record of a project it created earlier in the same command — and a project's issue, or
25//! a task or document written with no parent, is created in the configured `repository:`.
26//! What that rule refuses, it refuses before `createIssue`, so no issue is half-created. An
27//! existing issue is never moved: the update path leaves the issue where it is and records
28//! the list in the metadata slot when it differs, so the read side's derivation and the
29//! creation rule agree by construction.
30//! <!-- llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate] -->
31//!
32//! **A document is an ordinary issue whose title begins [`DESIGN_TITLE_PREFIX`].** A
33//! board has no document type and nothing but issues to hold one in, so the title is the
34//! discriminator and it is the whole of it. The title this source *reports* is the one a
35//! person wrote, with the prefix taken off — the same way the metadata slot is taken off
36//! the body so `content` is what the person wrote — and writing a document puts the prefix
37//! back, so a round trip returns the title that went in.
38//!
39//! **Telling a document from a project from a task.** The design prefix is read **first**:
40//! a document is never a project and never a task, whatever sub-issues it has or does not
41//! have. Only then does the rest apply — a board issue is a project when *either* it has
42//! sub-issues *or* it carries [`ItemKind::METADATA_KEY`]; otherwise it is a task. A
43//! sub-issue is always a task, whatever it carries. The marker is sufficient and never
44//! necessary: it is what makes an *empty* project — the state a project copy passes
45//! through between creating the project and filing its first task — readable as a
46//! project, while the sub-issue arm lets a person author a project on the board by hand
47//! with no knowledge of this product's metadata at all. Reading the prefix later than the
48//! sub-issue rule would make a design issue with no sub-issues an empty project, which is
49//! exactly the state that rule exists to catch. Pull requests are neither a project nor a
50//! task nor a document and are ignored.
51//!
52//! **A task's comments are its issue's comments.** They are read off `Issue.comments` and
53//! written with `addComment`, `updateIssueComment` and `deleteIssueComment`, and a comment's
54//! id is GitHub's own node id for the `IssueComment`. Two things GitHub decides are refused
55//! rather than papered over: a board **draft** is not an issue and has no comments at all, so
56//! a comment call on one is refused rather than answered with an empty page; and GitHub signs
57//! every comment as the account the token belongs to, so a comment handed an author of its
58//! own is refused rather than posted under another name. GitHub's comment mutations take the
59//! comment's id and nothing else, so an edit or a delete first reads which issue that comment
60//! is on, and a comment on some other issue is one this task does not have.
61//!
62//! **Where an entity is, is a link.** Every project, task and document this source reports
63//! carries a [`Location::Url`] naming the issue's own web address — the same address the
64//! `url` field already reports, in the shape that says a reader can open it. That is the
65//! contrast the location contract exists for: a reader holding an entity from this source
66//! is handed something to link to and one holding an entity from a folder of Markdown is
67//! handed a path, and neither has to know which plugin answered. It does not replace or
68//! derive from `url`; that field goes on reporting what it always reported.
69//!
70//! **Where metadata lives.** Short typed things go to typed fields and native relations:
71//! status to the board's `Status` single-select and the issue's own state, the copy
72//! origin to a source-owned `onetaskgraph.origin` text field, and dependencies to
73//! `blockedBy` and to sub-issue links. Unbounded caller JSON goes in a trailing
74//! `<!-- onetaskgraph.metadata ... -->` comment at the end of the issue body — the same
75//! encoding `docs/metadata.md` settles for Linear, not a second one. A ProjectV2 text
76//! field is length-bounded and `shortDescription` is capped at 300 characters, which is
77//! why neither can hold a caller's own prose. Setting one caller key on its own — on a task,
78//! a project or a document alike — is one update of the issue body that changes that slot
79//! and not one byte outside it, and it is not sent at all when the key already holds the
80//! value. The link a copy records on an item it copied, `onetaskgraph.copies`, is small and
81//! is kept in that same slot, written by that same update.
82//!
83//! **A caller's value can also be projected onto a board text field**, so the board can filter
84//! on it: each [`GitHubProjectsConfig::metadata_fields`] entry names a text field, a top-level
85//! metadata key and a path inside its value. The slot stays the value's one home — every read
86//! reports metadata from it alone — and the field follows it on every write of an item, a
87//! create, an update and a copy alike: a string is written, and not sent again while the field
88//! holds it — an empty string included, which is written as itself; nothing there or `null`
89//! clears a field holding a value; any other
90//! JSON type is refused before any mutation. It rides the ordered [`graphql::UPDATE_FIELDS`]
91//! write beside `Status`, `Priority` and the origin, so it adds no request; a key set on its
92//! own moves its field first and the body last, as every write of an existing item does. A
93//! board without the field, or with a non-text field of that name, is refused before any
94//! mutation pointing at `sources fields`, whose [`GitHubProjectsSource::fields`] creates it as
95//! a text field and never changes a field that is there.
96//!
97// llmlint: ignore-block[contracts_have_one_source_or_a_drift_gate] This public module documentation is a required user-facing description; the loopback plugin tests and shared live journey drive StatusMapping resolution, both mutations, and observed read-back together.
98//! **Status.** `status_mapping` is per-instance configuration, in the shared grammar
99//! [`onetaskgraph_plugin_api::StatusMapping`] documents, from a status category to an
100//! option of the board's one `Status` field for a task and for a project: a bare name is
101//! the option for both kinds, `null` disables the category for both, and `{task, project}`
102//! names it per kind. A category the mapping does not mention keeps its shipped default for
103//! both kinds; one it mentions is exactly what it configures, so a per-kind object no longer
104//! gets the shipped default for the kind it leaves out. Two categories one kind would read
105//! back from one option are refused as the configuration is read, ignoring case, while one
106//! option may stand for different categories of the two kinds. Writes go by the kind of the
107//! item written: a status that kind has no option for, or whose option the board lacks, is
108//! refused before any mutation, naming the source, the kind, the category and the key
109//! `status_mapping.<category>.<kind>` — there is no fallback. `done` selects its mapped
110//! option and closes the issue as `COMPLETED`; `cancelled` selects its mapped option and
111//! closes it as `NOT_PLANNED`, for either kind. Every open category reopens a closed issue
112//! before selecting its option. Reads give a closed issue's reason precedence over its
113//! option, while an open issue's option decides its category through its own kind's
114//! mapping, and an option that mapping does not name reads as `unknown` under its own name.
115//! The guarded [`GitHubProjectsSource::status_options`] and
116//! [`GitHubProjectsSource::fields`] operations are the one path here that calls
117//! `updateProjectV2Field`: GitHub replaces the whole option list, so they preserve every
118//! existing option id and verify the field and item assignments immediately afterwards.
119//! They ask for both kinds' options, counting a terminal category's mapped option as
120//! configured because a terminal write refuses without it. No ordinary source read or
121//! write calls that mutation, whose
122//! `singleSelectOptions` *overwrites* a field's option set, so no addition is additive
123//! and a mistake destroys every item's status. A status this board cannot represent is a
124//! refusal naming the status and the instance instead.
125//!
126//! `unknown` has no shipped option because this source cannot preserve an open-ended
127//! status word: it writes an existing board option and never
128//! creates an option. An operator may map `unknown` to one existing option, in which case
129//! every unknown word lands on that option and reads back as `unknown` under the option's
130//! name. This differs from `local-md`, which writes and reads the original word itself.
131//!
132//! The shipped terminal options are exactly `done: Done` and `cancelled: Cancelled`.
133//! `done` also closes the issue because GitHub derives `subIssuesSummary.completed`
134//! and the board's own `Sub-issues progress` field from closed sub-issues: a plan whose
135//! finished tasks were only moved to a "Done" column would read 0% complete forever.
136// llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
137//!
138//! # What this source declares, field by field
139//!
140//! One verdict per field of [`Capabilities`], and what `Native` means when this source
141//! says it. *Proven* means a shared journey drives it against the real
142//! binary over this source's own row in `crates/onetaskgraph-e2e-support/src/fixtures.rs`, and
143//! `every_row_declares_exactly_what_its_plugin_reports` is what keeps this list and
144//! [`capabilities`](TaskSource::capabilities) from parting.
145//!
146//! | Field | Verdict |
147//! | --- | --- |
148//! | `projects` | **Supported and proven,** and the one predicate here that is pushed down rather than applied in process: a task's project is the issue it is a sub-issue of, so a listing scoped to one *asks that issue* for its own sub-issues. This is the field that was declared and then not applied, which silently returned another project's tasks. |
149//! | `documents` | **Supported and proven.** A board holds issues, so a document is one: the issue whose title begins [`DESIGN_TITLE_PREFIX`]. Reads, filters and paging answer on exactly the terms a task read does, and a write puts the prefix back. |
150//! | `comments` | **Supported and proven,** over the task issue's own comment connection, oldest first and paged by GitHub's own cursor; added, edited and removed through GitHub's comment mutations, paced as every other mutation is. A draft item has no comments on GitHub and is refused, and so is an author, because GitHub records the signed-in account as every comment's author. |
151//! | `assets` | **Supported and native.** Image references travel as GitHub user attachments in the repository the task or document issue lives in. Uploads are authenticated and verified before the issue body is written. |
152//! | `priority` | **Supported and proven** by an instance configured with `priority_mapping`, and declared unsupported by one without it, which reports every task's priority as `none` and sends exactly the requests it sent before priorities existed. The priority is the board's single-select `Priority` field: no value is `none`, a mapped option is its level, matched case-insensitively, and an option the mapping does not name fails the read of that task, naming the option. A write selects the mapped option, or clears the value for `none`; a board without the field or the option is refused, pointing at `sources fields`, which is the one thing that creates either. |
153//! | `filter_by_priority` | **Supported and proven,** over the priority each task reads as — `none` for every task of an instance without `priority_mapping`. |
154//! | `filter_by_comment_activity` | **Supported, and exact** for comments created and for comments edited at or after `commented_since`, in every repository — of any owner — the board's items live in. Applied by asking a narrower question rather than by reading the board: GitHub's issue search scoped by `project:<owner>/<number>` alone, with an `updated:>=` qualifier, names the candidates, and each candidate's own comments confirm it, so `ProjectV2.items` is never read. That rests on GitHub moving an issue's `updatedAt` when a comment on it is added **or edited**, which the credentialed journey `an_edited_comment_moves_its_issue_and_is_selected_since` re-takes on every run of this lane. The search is an index that lags a write — the credentialed lane has watched it miss a newly commented issue for thirty seconds — so an issue this process itself commented on in the same command is a candidate whatever the search says, read by its own node if the search did not name it, and has its comments read rather than being ruled out by an `updatedAt` from before the comment. A comment another process wrote is found only once the index has it, so a caller asking again from its last instant should overlap the two generously. |
155//! | `orphan_tasks` | **Supported and proven.** A task issue with no `parent` is in no project. |
156//! | `filter_by_label` | **Supported and proven,** over the issue's own labels. |
157//! | `filter_by_status` | **Supported and proven,** over the board's `Status` option and the issue's open or closed state, through this instance's own `status_mapping` for the item's kind — a task query by the task half, a project query by the project half, `unknown` included. |
158//! | `filter_by_metadata` | **Supported, and asked of GitHub** — through the body and never through a projected `metadata_fields` text field, which is the board's to filter on and not this source's to read. A query naming metadata values is one board-scoped issue search with each value a quoted phrase `in:body` — GitHub's index covers the metadata comment at the end of the body, which is where caller metadata lives — and every candidate is confirmed against its own parsed metadata comment, so only an item holding that string at that key and path is returned. **A value with no letter or digit is refused** — the empty string, whitespace or punctuation alone — before any request, as a `SourceError::Refused` (wire kind `refused`) naming the value: GitHub's index holds words, so no bounded query can find such a value, and this source neither reads the whole board for it nor answers it as empty. |
159//! | `filter_by_origin` | **Supported, and asked of GitHub without enumerating the board.** The union of three reads, each confirmed by an exact match against the item's own origin field: the board's field filter over the `onetaskgraph.origin` text field, the issue search for the id as a phrase in the body where a write of this release mirrors it, and this process's own writes. See *Where a read-after-write guarantee comes from* for the window the three leave. |
160//! | `reconcile_status` | **Supported,** and proven by `onetaskgraph-github-projects-e2e`'s `status_reconcile.rs`: the board's own `ProjectV2.items(query:)` filter, `is:closed`, read page by page and never the unfiltered items, each candidate confirmed from its own item node — a closed task whose card is not at the option the mapping gives its closure's category. |
161//! | `complete_by_issue` | **Supported,** and proven by `status_complete.rs` there and in the engine's suite: each issue a reference names read in one aliased request per fifty — its repository, its number, its first page of board memberships with each card's `Status` read by name — and nothing else of the board. |
162//! | `search_title` | **Supported, and asked of GitHub for a task,** over `Issue.title`: a task query's text is one board-scoped issue search for it as a phrase `in:title`, every candidate confirmed by the case-insensitive substring rule. GitHub matches whole words, so a task holding the text only inside a longer word is not returned — a narrowing this source declares rather than hides. **A text with no letter or digit that is not blank is refused** — `--` for one — before any request, as the same `refused` error naming the text, for the reason a metadata value like it is; a blank text is not refused, and keeps the board read it always had, confirmed by the same substring rule. A project query's text, and a document query's text when the query is scoped to no project, is that same board-scoped search for the same phrase in the same fields, refused on the same terms, every candidate confirmed by its kind and by the same substring rule, so it narrows exactly as a task's does; a document query scoped to one project sends no search, reads that project's sub-issues and confirms its text over them by the substring rule alone, so it is neither narrowed to whole words nor refused for a text with no letter or digit. A board draft is not an issue, so no text search lists one, a draft titled as a document included. |
163//! | `search_content` | **Supported,** on the same terms, `in:body`, over the visible body — the trailing metadata comment is not part of what the substring rule confirms. |
164//! | `task_dependencies` | **Supported and proven,** in both directions: `blockedBy` and `blocking`. |
165//! | `project_dependencies` | **Supported and proven,** in both directions, over the same two connections, because a project here is an issue. |
166//! | `max_page_size` | **Supported and proven.** [`MAX_PAGE_SIZE`], GitHub's own connection maximum. |
167//!
168//! Image writes use `POST https://uploads.github.com/user-attachments/assets` with `name`,
169//! `content_type` and the issue repository's numeric `repository_id` as query parameters,
170//! the source token as `Authorization: Bearer`, the image content type as `Content-Type`,
171//! and the raw bytes as the request body. The numeric repository id is read at most once
172//! per repository per source instance. A loopback API endpoint also selects the loopback
173//! uploads host; production needs no assets repository, commits or extra configuration.
174//! Every returned JSON `url` is read with the same token before a body is written: only
175//! a 2xx verification succeeds. A refusal names the asset, URL and HTTP status; an
176//! anonymous 404 does not invalidate an authenticated 200. An upload refusal names the
177//! asset and HTTP status and says the token type may not be accepted by the upload
178//! endpoint. A classic PAT was measured accepted; no acceptance claim is made about
179//! fine-grained PATs or OAuth tokens.
180//! The body keeps the authored alt text while `./<name>` becomes the attachment URL,
181//! and `onetaskgraph.assets` records `{sha256, url}` by name. A matching destination
182//! SHA-256 reuses its URL without an upload or verifying read; changed bytes are uploaded
183//! and verified afresh, in the repository the issue already lives in on an update.
184//! An uploaded image renders for the viewers GitHub lets read it, like the issue text beside it.
185//! Uploads take the same mutation spacing as content writes. Both uploads and verification
186//! reads wait out classified rate limits within the configured per-call retry budget,
187//! on the supplied clock; verifying reads take no mutation slot.
188//!
189//! Nothing here is unsupported. `documents` and `comments` are not predicates — they say this
190//! source has documents and that its tasks have comments, both of which hold — and the three
191//! facts behind the uniform `Native` on the
192//! predicates beside it are recorded below rather than re-derived, because a reader who
193//! takes `Native` to mean *the remote service filters* will read that uniformity as a
194//! lie.
195//!
196//! First, the plugin contract defines `Support::Native` as *the source applies this
197//! predicate itself*, and says nothing about where it applies it. What the declaration
198//! promises the engine is capability rule 1 — a predicate declared `Native` **is** applied
199//! — so that the engine may push it down and apply nothing of its own.
200//!
201//! Second, this source can keep that promise for every predicate at no additional API
202//! cost, because whichever of the reads below answers a query has already read every
203//! candidate that query will return before it filters anything. Filtering those items is
204//! in-process work over data already in hand.
205//!
206//! Third, six task predicates are asked of GitHub as a narrower question and the rest are
207//! applied in process over what that question returned. A project filter has a relationship — a
208//! project's tasks are that issue's sub-issues, and asking the issue for them is both cheaper
209//! and exact. Comment activity is the issue search's `updated:` qualifier. A text search, and
210//! a search for metadata values, is the board-scoped issue search carrying the text and each
211//! value as quoted phrases; an origin is the board's own field filter over its origin field
212//! beside the same search for the id. **The text search narrows, and that is this source's
213//! declared semantics:** GitHub matches whole words where the substring rule this source and
214//! the local Markdown source confirm with would match inside one, so an item holding the text
215//! only inside a longer word is never a candidate. Every item returned does contain the text.
216//! A project query's text, and a document query's scoped to no project, is that same search
217//! and narrows on the same terms, its candidates confirmed by their kind as well.
218//! GitHub's issue search offers no qualifier for a label set, a status column or a priority,
219//! so those three are applied in process over the candidates, and a query carrying none of
220//! the six narrowing predicates reads the board. Declaring one `Unsupported` would make the
221//! engine compensate for work this source has already done, and declaring `projects` native
222//! while ignoring the filter (which this source once did) silently returns another project's
223//! tasks, because the engine trusts the declaration and applies nothing locally.
224//!
225//! # The three ways this source reaches an item, and what each costs
226//!
227//! A board read is charged for what its *nested* connections could return rather than for
228//! what was asked, so one whole-board read costs the same whether the question was about
229//! one project or about all of them. That is why a question about one project is never
230//! answered by reading the board:
231//!
232//! | The question | What is sent | What it costs |
233//! | --- | --- | --- |
234//! | one item, by its own id | [`graphql::ISSUE`] — `node(id:)`, carrying the field definitions of the boards it sits on and the far ends of its `blockedBy`, which is what a write of it needs — and, when that node is a board draft, [`graphql::DRAFT`] — the draft and the one board item it is | the item |
235//! | one task with its first page of comments, for `task show` and a comment listing | [`graphql::ISSUE_DETAIL`] — the same `node(id:)` read with the issue's `comments` | the item and a page of its comments |
236//! | several tasks with their comments, for `task show-many` | [`graphql::ISSUE_DETAILS`] — [`DETAIL_BATCH`] aliased `node(id:)` fields per request | each item and a page of its comments |
237//! | the board's own id and field definitions, for a write whose item does not carry them | [`graphql::BOARD_FIELDS`] — the board's `id` and `fields`, and no `items` — or, for a create that needs the repository's id too, [`graphql::CREATION_CONTEXT`], both in one request | the board's fields |
238//! | one project's tasks or documents | [`graphql::SUB_ISSUES`] — that issue's own `subIssues` | that project |
239//! | which projects this board holds | [`graphql::SEARCH_ISSUES`] — an issue search scoped to the board | the board's issues, without their board items |
240//! | which projects hold a text, or which documents do when no project narrows the question | [`graphql::SEARCH_ISSUES`] — the board-scoped search with the text as one quoted phrase, `in:title`, `in:body` or both, as a task's text is sent — walked to its end in pages of twenty | the issues that match |
241//! | which tasks were commented on since an instant | [`graphql::SEARCH_ISSUES`] — the same board-scoped search with an `updated:>=` qualifier — then [`graphql::ISSUE_COMMENTS`] for each candidate it names | the issues updated since, and their comments |
242//! | which tasks hold a text, or a metadata value | [`graphql::SEARCH_ISSUES`] — the board-scoped search with the text and each value as quoted phrases, `in:title`, `in:body` or both, and an `updated:>=` qualifier too when comment activity is asked for — in pages of twenty, only as many as the caller's rows need | the issues that match |
243//! | which tasks were copied from one origin | [`graphql::ORIGIN_LOOKUP`] — the board's own `items` under its field filter on the origin field, and the same board-scoped search for the id `in:body`, in one request, each paged at three | the carriers of that origin, which is one item |
244//! | every task, every document, every label, when nothing above narrows the question | [`graphql::BOARD`] — the board's own `items` — **and** [`graphql::SEARCH_ISSUES`], because neither enumeration of a board is complete alone; see [`GitHubProjectsSource::board`] | the board, twice over |
245//! | which board item one issue is, past the page that came with it | [`graphql::ISSUE_BOARD_ITEMS`] — that issue's own `projectItems` | one issue's memberships |
246//!
247//! The following standalone-ticket requests are pinned by the real CLI fixture journeys
248//! `follow_up_writes_resolve_each_item_once_and_batch_the_copy_fields` and
249//! `a_batched_detail_read_costs_one_request_and_one_point_per_detail_batch`, as request count
250//! equal to declared points equal to the row. They include the origin lookup and the
251//! field/repository discovery a create needs. A bound re-copy changes status, priority,
252//! content and metadata; comment recount means a subsequent detail read. Each request here
253//! costs one declared point. A membership beyond the embedded page can additionally require
254//! the one-point membership recovery described above. A bound re-copy of a task filed under a
255//! project adds one read, the engine confirming that project's link by its own id once per
256//! command; and the same-source far ends a write newly names — those that do not already block
257//! the item, whose own read answered for them — are read together by their own ids,
258//! [`DETAIL_BATCH`] to one [`graphql::ISSUE_DETAILS`] request, each new edge then one
259//! [`graphql::ADD_BLOCKED_BY`]. Both additions are rows of the table below, pinned by
260//! `a_bound_recopy_adds_one_project_read_and_batches_the_dependencies_it_newly_names`.
261//!
262//! **[`DETAIL_BATCH`] is 24**: the largest batch of [`graphql::ISSUE_DETAILS`] the node-count
263//! model prices at one point. Each aliased item is six of GitHub's aggregate, so 24 are 144,
264//! which rounds to one point, and 25 are 150, which rounds to two; `tests/point_cost.rs`
265//! holds both halves.
266//!
267//! **An existing item is written body last.** A bound re-copy and a `task update` send its
268//! board fields first — the `Status` option and the `Priority` together, in one request — then
269//! its parent and its `blockedBy`, and its title, body and state in one `updateIssue` last.
270//! GitHub runs no two requests as one, and runs a document's mutation fields in order without
271//! undoing an earlier field when a later one fails, so that order is what makes a write
272//! refused part-way leave the item's body, and every metadata key in it, exactly as it stood;
273//! the one piece of metadata written before the body, an origin a copy re-points, is put back
274//! when a later write is refused — and when putting it back is refused too, the write's own
275//! refusal names that key, what it now holds and what it held. `crates/onetaskgraph-github-projects-e2e/tests/e2e/write_order.rs` refuses each
276//! of those writes in turn, whole and as one aliased field failing after the one before it.
277//!
278//! **Two facts about GitHub the write rows rest on, each read off GitHub's published schema
279//! artifact <https://docs.github.com/public/fpt/schema.docs.graphql> on 2026-10-01 and pinned
280//! in `tests/fixtures/schema.graphql`, and the first then put to GitHub itself:**
281//!
282//! - **A board is accepted at creation but its item is not answered, so a create still files
283//! the issue itself: a new copy is 5 requests, and 4 with `--create`.**
284//! `CreateIssueInput.projectV2Ids: [ID!]` is declared there — "An array of Node IDs for
285//! Projects V2 associated with this issue", `@possibleTypes(concreteTypes: ["ProjectV2"])`.
286//! The credentialed journey `real_projects_v2_contract_writes_and_leaves_no_residue` was run
287//! against a real board on 2026-10-01 with a create sending the board there and reading the
288//! item off the payload's `Issue.projectItems`: every one of its four creates answered with
289//! no item on the board, so each went on to [`graphql::ADD_TO_BOARD`], and the fourth was
290//! refused "Content already exists in this project" — GitHub had filed the issue after
291//! answering, and refuses a second filing rather than answering with the item it holds. A
292//! create therefore sends no `projectV2Ids` and files the issue with
293//! `addProjectV2ItemById`, the one call whose answer names the board item. The saving that is
294//! real is the read before it: the board's fields and the repository's id together, in
295//! [`graphql::CREATION_CONTEXT`], at the point the repository is known.
296//! - **A comment still reads its target first, so a comment is 2 requests.**
297//! `AddCommentInput.subjectId: ID!` is declared there with
298//! `@possibleTypes(concreteTypes: ["Issue", "PullRequest"], abstractType:
299//! "IssueOrPullRequest")`. A board draft is no such subject and would be refused, but a
300//! project's issue, a document's issue, an issue on no board of this source and a pull
301//! request all are: GitHub writes the comment, so there is no refusal to map into "that is
302//! not a task of this board". [`graphql::ISSUE`] before [`graphql::ADD_COMMENT`] is what
303//! refuses those by name.
304//!
305//! | Verb | Requests / points | Documents |
306//! | --- | --- | --- |
307//! | new copy | 5 | ORIGIN_LOOKUP, CREATION_CONTEXT (the board's fields and the repository's id together), CREATE_ISSUE, ADD_TO_BOARD, UPDATE_FIELDS |
308//! | copy --create | 4 | CREATION_CONTEXT, CREATE_ISSUE, ADD_TO_BOARD, UPDATE_FIELDS: the new copy without its ORIGIN_LOOKUP |
309//! | bound copy | 3 | ISSUE (with the board's fields and the issue's `blockedBy`, so no BOARD_FIELDS or ISSUE_DEPENDENCIES), UPDATE_FIELDS, then UPDATE_ISSUE last |
310//! | bound copy, filed under a project | 4 | the bound copy's three, and one ISSUE of the destination project its link names, read once per command |
311//! | bound copy, newly naming n dependencies | + ceil(n / DETAIL_BATCH) + n | ISSUE_DETAILS for the far ends that do not already block the item, DETAIL_BATCH (24) to a request (one alone is ISSUE), then one ADD_BLOCKED_BY each; a far end already blocking it is answered by its own read and costs nothing |
312//! | comment | 2 | ISSUE, ADD_COMMENT: the target is read first, because GitHub accepts a comment on any issue or pull request (see below) |
313//! | detail | 1 | ISSUE_DETAIL: the item and its first page of comments, for `task show` and `task comment list`; `--no-comments` is ISSUE alone |
314//! | batched detail | ceil(n / DETAIL_BATCH) | ISSUE_DETAILS: `task show-many` of `n` items, DETAIL_BATCH (24) at a time, comments included or not |
315//! | copy --comments, n changed tasks | ceil(n / COPY_DETAIL_BATCH) | ISSUE_COPY_DETAILS: each task with its first page of comments and its `blockedBy`, COPY_DETAIL_BATCH (21) at a time; a task whose comments run past that page adds one ISSUE_DETAIL per further page, and a task skipped from its listing costs nothing beyond it |
316//! | recount | 1 | ISSUE_DETAIL |
317//! | status | 2 | ISSUE, UPDATE_FIELD; a terminal status additionally updates issue state |
318//! | priority | 2 | ISSUE, UPDATE_FIELD or CLEAR_FIELD, with stored priority in the mutation response |
319//! | content | 2 | ISSUE, UPDATE_ISSUE |
320//! | metadata | 2 | ISSUE, UPDATE_ISSUE |
321//! | 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 |
322//! | record only | 1 | ISSUE |
323//!
324//! <!-- github-search-paging:start -->
325//! Board-scoped text, metadata, project-name and comment-activity searches send every
326//! page at `first = 20` (SEARCH_PAGE_SIZE), the SEARCH_ISSUES document's one-point
327//! ceiling. A later page is sent only when `hasNextPage` is true and the caller still
328//! needs rows. A page is never resized to the rows still needed: GitHub orders one
329//! search differently at different page sizes, so one fixed size makes a paged walk
330//! send exactly the requests one whole read sends, and the answer's order is the order
331//! those pages arrive in. A page below twenty would cost the same one point, and GitHub
332//! prices this document by rows, so twenty-row pages cost per row what 100-row pages do.
333//! Project-name lookup continues until an exact match or exhaustion. A task limit bounds
334//! returned and fetched pages: a limit is sliced from the pages it needs, and local
335//! confirmation can require more candidates than matching rows. Walking all pages
336//! returns the whole answer. The opaque version-4 source cursor carries GitHub's page
337//! cursor and how far into that page the last answer stopped, and resumes in the same
338//! process or a new one, without duplicates or gaps. It carries no rows: one process
339//! sends each page's search once, and a new process re-reads only the page it resumes
340//! in, then sends a further page once, never as a re-read, only when its limit still
341//! needs rows. Every request either walk sends is the one a whole read sends for that page. Own writes replace stale index
342//! copies and complete missing rows at exhaustion. Cache entries are whole GitHub pages,
343//! so a small answer cannot truncate a wider question. Origin pages remain three; whole-board sizing is unchanged.
344//! Read-after-write is a per-process guarantee. A cursor resumed in a new process is
345//! not required to include the original process's writes still omitted by the index.
346//! <!-- github-search-paging:end -->
347//!
348//! The board half of an issue — its board item's id, its `Status` option and this
349//! source's origin text field — rides along on `Issue.projectItems` in the first three, so
350//! an item reached any of those ways resolves through the same
351//! [`GitHubProjectsSource::resolve`] the board walk uses and reports the same title, the
352//! same status, the same labels and the same qualified id. That connection comes back a
353//! *page* at a time, at `BOARD_ITEMS_PAGE_SIZE`, so the entry for this board is looked for
354//! on the page in hand and — only if that page reports more of the connection — in the
355//! last row's read of that one issue's memberships, resumed from the page's own cursor and
356//! walked to exhaustion. An issue with no entry for *this* board is not this source's to
357//! report, which is what keeps an id naming another repository's issue from being answered
358//! as an item of this board; and because the page is where the search starts rather than
359//! where it ends, that answer is one about a connection read to exhaustion and never about
360//! an unread page. Nothing costs the extra read but an issue on more boards than a page
361//! holds: an issue this board really does not hold reports no next page, so its
362//! memberships are already exhausted where they arrived.
363//!
364//! **No document here selects the board's own `Labels` field, and nothing is lost by
365//! that.** An item's labels are read from its content alone, wherever that content is
366//! reached: the three documents above select `Issue.labels` on the fragment, and
367//! [`graphql::BOARD`] selects the same connection on the `... on Issue` arm of its
368//! `content`. A board's `Labels` field is not one anybody fills in: it is a built-in
369//! `ProjectV2FieldType`, it is absent from `ProjectV2CustomFieldType` so no project can
370//! create one, and `ProjectV2FieldValue` — the whole of what
371//! `updateProjectV2ItemFieldValue` accepts — offers no way to write one. So GitHub derives
372//! it from the content, for every content type it exists on, and there is nothing it can
373//! hold that the content does not already say: for an `Issue` it *is* that issue's own
374//! labels, so selecting it beside them unions a set with itself.
375//!
376//! **A draft loses nothing by that either**, which is the reasoning this paragraph once had
377//! backwards. `DraftIssue` exposes no `labels` field, and by the three schema facts above
378//! it cannot carry a board `Labels` value to be derived from one — so a draft has nothing
379//! to select *and nothing to lose*, and reports no labels at all. A `PullRequest` item is
380//! discarded by [`GitHubProjectsSource::resolve`] before labels are read. Both halves are
381//! held to that by tests in `tests/plugin.rs`: the four ways an item is reached report one
382//! label set, and that set is the fixture issue's own, by
383//! `an_item_reports_the_same_labels_title_status_and_id_however_it_is_reached`; and a board
384//! item whose content is a draft reports an empty set, by
385//! `a_board_item_whose_content_is_a_draft_reports_no_labels_at_all`. The absence of the
386//! selection is held over [`graphql::DOCUMENTS`] by
387//! `no_document_selects_the_boards_own_labels_field`.
388//!
389//! The whole-board row is still the board's own item connection, and deliberately: a
390//! **draft** board item is not an issue, so no search can list one, and the reads that have
391//! to answer for the whole board are the ones whose cost is the board's size anyway.
392//!
393//! **A question about one item this source already names by id never lists the board.**
394//! Whether that item is on this board, and what its board fields are, is answered by reading
395//! that item — its own `Issue.projectItems`, walked to exhaustion by
396//! [`GitHubProjectsSource::resolve_issue`], or a draft's own board item — and never by
397//! looking for it in [`graphql::BOARD`]'s `items` or in a listing this command already
398//! holds. That covers a write's destination, the project a new item is filed under, a
399//! same-source far end a dependency names, a status write, the dependency slot a draft keeps,
400//! and the delete that takes back an item a copy made. What such a write needs of the board
401//! and the item does not carry — the board's id, the `Status` and origin field definitions —
402//! comes from [`graphql::BOARD_FIELDS`], which reads no item at all. The reason is evidence,
403//! not economy alone: `ProjectV2.items` is a projection that lags the membership GitHub
404//! itself reports — an issue added with `addProjectV2ItemById` can be missing from it for
405//! minutes. Scanning this host's 842-item board has refused a document copy and an update
406//! even though the items' own reads named that board. A scan there gives the wrong answer
407//! as well as paying for every page. So a `board.items` lookup does not belong on any of
408//! those paths.
409//!
410//! **What a read may return is capped too, and that cap is on the document rather than on
411//! the board.** GitHub limits the number of nodes **one query may return** to
412//! [`NODE_COUNT_LIMIT`] and refuses a query above that before executing it: the answer is
413//! an error naming the connection the count crossed at, not a slow or a partial result.
414//! Every board this source reads is refused the same way, so no board is too big for these
415//! documents and none is small enough to save one that is over.
416//!
417//! The count is arithmetic over the document's own text: each connection contributes the
418//! `first:` it asks for, counts **multiply** down a nested path and **sum** across sibling
419//! paths. Those are [GitHub's published rules][node-limits] and this workspace does not
420//! restate them — `github-graphql-node-count` implements them, and
421//! [`worst_case_node_count`] under [`largest_page_sizes`] is where every node count here
422//! comes from. `every_document_this_source_sends_stays_under_githubs_node_limit`, in
423//! `tests/node_count.rs`, recomputes every document in [`graphql::DOCUMENTS`] from that
424//! same text on every run and fails naming any that reaches the limit — so a connection
425//! added to a shared fragment is caught there rather than by GitHub.
426//!
427//! What decides those counts is the page sizes: [`MAX_PAGE_SIZE`] on the outer page,
428//! `NESTED_PAGE_SIZE` on the connections hanging off one item, and
429//! `BOARD_ITEMS_PAGE_SIZE` on the page of an issue's board memberships a read carries.
430//! `$nestedFirst` is spent twice down one path of a board read, so that constant is
431//! effectively squared there, which is why it is the one the limit is most sensitive to.
432//! `BOARD_ITEMS_PAGE_SIZE` is small for a reason of its own, recorded beside it: what a
433//! page of memberships misses is recovered by one further read rather than refused, so it
434//! buys a bound every read pays for at the price of a request only a multi-board issue
435//! pays.
436//!
437//! **`nodeCount` and `cost` are two numbers against two limits, and both are computed
438//! offline here — per document, one document at a time.** `nodeCount` is the one above: the
439//! most nodes one query may return, checked per query and bounded by [`NODE_COUNT_LIMIT`].
440//! `cost` is rate-limit points, metered per hour across everything one credential does; it
441//! is what the two limiters [`Limiter`] tells apart meter, and a document under
442//! [`NODE_COUNT_LIMIT`] still says nothing about its price. [`worst_case_point_cost`] is
443//! that second number, and `tests/point_cost.rs` pins every document in
444//! [`graphql::DOCUMENTS`] at what it costs — there being no per-call point ceiling to hold
445//! one under, the pin itself is the check. The credentialed lane reconciles both figures
446//! against GitHub's own, off a probe it already sends.
447//!
448//! **What is pinned that way is a per-document price and never a session's.** The record in
449//! `session-cost.md` measures the two quantities a whole session can be counted in offline —
450//! **requests** and **worst-case nodes** — and neither is points. What one whole session
451//! consumes of the hourly point allowance is observable only from a credentialed run's own
452//! `x-ratelimit-*` headers, which is what [`accounting`] fills its per-budget figures from
453//! and what `tests/live.rs` prints at the end of every run.
454//!
455//! [node-limits]: https://docs.github.com/en/graphql/overview/rate-limits-and-node-limits-for-the-graphql-api
456//!
457//! **Where a read-after-write guarantee comes from, since neither of GitHub's two
458//! enumerations of a board can supply one alone.** Resolving a node id is strongly
459//! consistent, so a read by id and a project's own sub-issues are already current. The
460//! other two are not, and they are behind by different amounts and in different directions:
461//!
462//! - GitHub's **issue search** is an index and answers a write made moments ago with the
463//! value from before it — usually for a second or two.
464//! - **`ProjectV2.items`** is a projection GitHub rebuilds behind the write, and an item put
465//! on a board with `addProjectV2ItemById` can be **absent** from it — not present with its
466//! content withheld, absent, with the connection walked to its own `hasNextPage: false` —
467//! for *minutes*, while `Issue.projectItems` names the same membership at once.
468//!
469//! That second one is a measurement rather than a caution. This repository's own
470//! credentialed journey writes a project and waits for the board to report it, then writes a
471//! task and waits for the same thing seconds later on the same board: the project wait is
472//! answered through the search and converged in two or three attempts in each of three runs,
473//! and the task wait is answered through `ProjectV2.items` and converged in none of them
474//! inside thirty. Separately, an item added to a second and larger board was read back by
475//! `Issue.projectItems` on that board's own id while every one of that connection's nine
476//! pages, walked to exhaustion nine minutes after the add, did not name it. Reading a board
477//! through the lagging one alone is what had a board read deny an issue that had certainly
478//! landed on it.
479//!
480//! So [`GitHubProjectsSource::board`] is the **union** of both — each search result still
481//! admitted only on this board's own strongly-consistent `Issue.projectItems`, and neither
482//! enumeration dropped, because only `ProjectV2.items` lists a board draft and only the
483//! search reports what the projection is behind on. What closes the last
484//! gap, the one where both are behind, is [`GitHubProjectsSource::created`]: every read this
485//! source answers is completed with what this process itself wrote, so an item created
486//! seconds ago is reported whether or not GitHub has caught up. Nothing else is remembered,
487//! nothing is written down, and the record dies with the process. **A wait that has to
488//! observe GitHub's own data cannot be answered from that record** — which is why the
489//! credentialed journey asks through a source built afresh, and why the union above rather
490//! than a longer wait is what makes such a wait converge.
491//!
492//! **A narrowed read is the same bargain, stated for each of the three predicates it
493//! answers.** A read carrying a text, metadata or origin predicate asks GitHub's index rather
494//! than walking the board, and every such answer is completed with what this process wrote —
495//! its [`created`](GitHubProjectsSource::created) record and every existing item it wrote,
496//! each filtered by the same predicates as the rest — so an item this command wrote a moment
497//! ago is returned by a query that matches it whether or not the index has caught up. An item
498//! a caller holds the id of is read by that id, with `node(id:)`, which is strongly
499//! consistent. What is left is stated rather than papered over:
500//!
501//! | Read | Finds | Behind by |
502//! | --- | --- | --- |
503//! | text, metadata | the issue search for the phrases | what another process wrote in the last second or two, until GitHub indexes it |
504//! | 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 |
505//! | 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 |
506//! | origin, third read | this process's own writes | nothing |
507//!
508//! So an origin carrier another process added within the last second or two, before either
509//! index has it, can be missing from an origin query, and one written by the release before
510//! this one — its origin in the field alone — can be missing for as long as the board's own
511//! item connection is behind on it. A copy that must not duplicate its own earlier write
512//! relies on the link it records, not on either index. **A board draft is not an issue**, so
513//! a draft is never returned by a text, metadata or origin query, whatever it holds: no search
514//! lists one, the origin lookup drops any the board's own field filter names, and one this
515//! process wrote is not added back either.
516//!
517//! **The origin lives in the board field, and the body holds a mirror of it.** A write that
518//! carries an origin writes it to the `onetaskgraph.origin` text field and also into the
519//! body's metadata slot, so the issue search can find it in seconds. The field is
520//! authoritative: this source reads an item's origin from the field alone, so a slot that
521//! disagrees with it, or holds one where the field holds none, is never read as a second
522//! origin — and the release before this one reads the slot, drops that key's copy for the
523//! field's, and sees the same one origin.
524//!
525//! Filtering happens before paging, so a page of a filtered result is a page of the
526//! survivors rather than the survivors of a page. Label matching and the substring rule a
527//! text candidate is confirmed by answer the same question the same way the local Markdown
528//! source's do; which candidates a text search has to confirm is GitHub's word match, which
529//! is the one place the two sources can answer the same text differently.
530//!
531//! <!-- llmlint: ignore[contracts_have_one_source_or_a_drift_gate] The declaration itself
532//! has one source, `capabilities`, and the note above is the reasoning behind it rather
533//! than a second copy of it: without the three facts recorded here a reader takes the
534//! uniform `Native` for a lie and reverts it. The drift gate on the declaration is this
535//! crate's own capabilities test, which pins every field of it against a fully spelled-out
536//! `Capabilities` literal — a struct with no `Default`, so a field added to the contract
537//! fails to compile there rather than going unasserted. -->
538//! The fixture-server tests above run wherever this crate is selected; the credentialed
539//! lane runs in the same required check, beside them, and can fail it — it verifies the
540//! 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,
541//! one filed under neither, a label on one of the three and a closed status on another —
542//! because that shape is what tells an honoured predicate from an ignored one: a board
543//! holding a single project answers a project filter the same way whether or not this
544//! source applies it, which is exactly how the defect above went unseen.
545//!
546//! That lane writes only to the board `GH_PROJECTS_OWNER` and `GH_PROJECTS_NUMBER` name,
547//! and the scratch repository `GH_PROJECTS_REPOSITORY` names:
548//! `nickderobertis/onetaskgraph-live-scratch`. It refuses the core repository before any
549//! session or request, independently of the live demand. Fix that variable in the machine's
550//! onetaskgraph `secrets.env` or the environment it pushes from, such as ai-orchestrator's
551//! `.env`. The targets are declared once in `onetaskgraph_github_live`, the lane's own
552//! policy crate; absent credentials or nominations otherwise skip.
553//!
554//! GitHub Actions artifacts carry `ci-<run id>-<attempt>-<micros>`, naming their writing
555//! run and attempt; invalid Actions identity refuses before writing. Other runs retain
556//! `<host>-<process>-<micros>`. Own cleanup matches the whole stamp. Machine residue stays
557//! the owning machine's lock sweep's; Linear always keeps that form and is unaffected.
558//! The hourly janitor is cleanup, never a test lane: scratch CI residue is removed only
559//! after its run reads back as `completed`, immediately before the listed-artifact batch.
560//! Failed or incomplete ownership reads preserve residue. A 24-hour waiting period
561//! is a margin, never the ownership authorisation. Cleanup leaves machine stamps
562//! and the board's `onetaskgraph.origin` field untouched.
563//!
564//! # What a session of requests costs, and where the report is
565//!
566//! This source records **every** request it sends into [`accounting::Accounting`], at
567//! `send_once` — the one place a request leaves this crate, which is why a read path added
568//! later is counted without anybody remembering to count it. That is the whole of what this
569//! crate adds to the arrangement; [`accounting`] is where what a record carries, how a
570//! session's spend is arrived at, and what it deliberately does not know are set out.
571//!
572//! What one whole session of the live journey costs, counted that way against this crate's
573//! loopback fixture board, is written down in `session-cost.md` beside this crate — with the
574//! reduction it came out of, and with what it does and does not say about rate-limit points.
575//!
576//! [`GitHubProjectsSource::accounting`] is the read: a snapshot to hold and compare, which
577//! [`accounting::Session::report`] renders the session report from. It is on the ordinary
578//! code path — no environment variable, no feature, no build configuration — because an
579//! instrument nobody switches on measures nothing, and
580//! [`Plugin::build_recording_into`] is how a caller making its own calls beside this
581//! source's counts the whole session rather than this source's share. The credentialed lane
582//! in `tests/live.rs` does exactly that, and prints the report at the end of every run,
583//! passed or failed.
584//!
585//! **A live session refuses to start unless the account can afford it.** Before it does any
586//! of the work it exists to do, the journey makes one request — `GET /rate_limit`, which
587//! GitHub documents as not counting against the REST rate limit and which answers both of
588//! its budgets at once — and starts only if, for each of them, what remains minus this
589//! session's estimated cost is still at least
590//! `onetaskgraph_live::RETAINED_BUFFER` — twenty per cent — of that budget's whole
591//! allowance. A session that cannot **declines**: it did not run, so it is
592//! neither a pass nor a failing assertion, and it says which budget was short, that budget's
593//! limit, what remained, the estimate, the buffer and when it resets — then stops, without
594//! waiting for the budget to come back. The estimate is derived offline from
595//! `tests/fixtures/session-cost.txt` and a cost model stated in `tests/journey/budget.rs`,
596//! which is also where the published rule that model rests on is cited; the accounting
597//! above records the gate's own read like any other request, and
598//! [`accounting::Session::report`] prints the estimate beside what the session really spent.
599//!
600//! **GitHub is the authority on both of its own numbers, and the credentialed lane goes and
601//! asks it.** Everything above computes `nodeCount` and `cost` offline from a document's own
602//! text, which is what lets it run on every platform and on a pull request from a fork with
603//! no credential — and that is what actually stops a regression merging. But an offline
604//! arithmetic can only ever agree with itself: if GitHub changes its rules, this workspace
605//! goes on computing the old answer and nothing notices. So `tests/live.rs` reconciles them.
606//! GitHub's schema exposes `rateLimit(dryRun: true)`, whose `nodeCount` is *"the maximum
607//! number of nodes this query may return"* and whose `cost` is what that document would
608//! spend, both for a document **without executing it**, and the lane asks it for every query
609//! document this source sends, under the largest bindings this source sends, and fails when
610//! GitHub's figure and [`worst_case_node_count`] or [`worst_case_point_cost`] disagree. A
611//! mutation is skipped, because `rateLimit` is a field of `Query` and cannot be asked about
612//! one; the offline pins still cover it. It records what those calls reported about the
613//! account's own allowance, because whether asking is free is a thing to observe rather than
614//! to assume. Two quantities, not one: [`NODE_COUNT_LIMIT`] bounds `nodeCount` per query,
615//! and `cost` is metered against an hourly allowance the accounting above reads off a
616//! credentialed run's own response headers.
617//!
618//! **GitHub has two rate limiters and this source is refused by both, so nothing here
619//! treats them as one thing.** The primary budget is the hourly allowance `gh api
620//! rate_limit` reports; the secondary limiter is a burst limiter over content-generating
621//! requests, and *nothing* reports it. Which one refused decides the operator's next step,
622//! so [`Limiter`] is a type rather than a detail, and it is what [`MIN_MUTATION_INTERVAL_MS`],
623//! [`GitHubProjectsSource::board_cache`] and [`GitHubProjectsSource::graphql`] each answer
624//! one part of.
625#![deny(missing_docs)]
626
627use std::collections::BTreeMap;
628use std::sync::{Arc, Mutex};
629use std::time::Duration;
630
631use chrono::{DateTime, Utc};
632use onetaskgraph_plugin_api::{
633 Capabilities, Classification, ClosedAs, Comment, CommentBody, CommentOrigin, Completion,
634 ContradictedItem, Coverage, Cursor, DependencyEdge, DependencyEndpoint, DependencyKind,
635 DependencySupport, Direction, Document, DocumentQuery, DocumentVersionCoverage, Health,
636 HeldTask, IssueHolding, IssueRef, ItemKind, ItemWrite, Label, LabelFilter, Location,
637 MetadataKey, Metering, NativeId, NewComment, Page, PageRequest, Priority, Project,
638 ProjectFilter, ProjectQuery, ProjectVersionCoverage, Repository, SecretResolver, SharedClock,
639 SourceError, SourceName, SourcePlugin, Status, StatusCategory, StatusContradiction,
640 StatusMapping, Support, Task, TaskDetailRead, TaskQuery, TaskRef, TaskSource, TaskUpdate,
641 TaskUpdateOutcome, TextFields, TextQuery, UnmappedStatus, UpdatedField, VersionCoverage,
642 WriteJournal, WriteSupport, system_clock,
643};
644use reqwest::{Client, StatusCode, Url};
645use schemars::{Schema, schema_for};
646use secrecy::{ExposeSecret, SecretString};
647use serde::{Deserialize, Serialize};
648use serde_json::{Value, json};
649
650pub mod accounting;
651mod assets;
652pub mod change_token;
653mod expected;
654pub use expected::{CONTEXT_VERSION, WHOLE_MEMBERS, context_shapes};
655mod visibility;
656
657use accounting::Accounting;
658
659/// The registry name for this plugin.
660pub const KIND: &str = "github-projects";
661/// GitHub's maximum connection page size.
662pub const MAX_PAGE_SIZE: u32 = 100;
663/// Every page of a board-scoped narrowing search: 20 rows, one point of SEARCH_ISSUES, the
664/// most one point buys. GitHub prices that document by rows, so pages of 20 cost what pages
665/// of 100 cost per row, and a page of fewer than 20 costs the same one point.
666pub const SEARCH_PAGE_SIZE: u32 = 20;
667/// How many items one [`graphql::ISSUE_DETAILS`] request reads, each with the first page of
668/// its comments: the largest batch the node-count model prices at one point.
669///
670/// Each aliased item is resolved once, and what GitHub charges for it is the connections
671/// under it — its labels, its page of board memberships, the field values of each of those
672/// three memberships, and its comments: six requests' worth of the aggregate GitHub divides
673/// by a hundred and rounds. Twenty-four items come to 144, which rounds to one point;
674/// twenty-five come to 150, which rounds to two. `tests/point_cost.rs` prices the document at
675/// one point and fails if one item more would still be priced at one.
676pub const DETAIL_BATCH: usize = 24;
677/// How many issue references one [`graphql::ISSUES_BY_REFERENCE`] request reads.
678///
679/// Each reference asks GitHub for one connection — a page of its board memberships, each with
680/// its `Status` value read by name rather than through a connection of field values — so fifty
681/// come to fifty requests' worth of the aggregate GitHub divides by a hundred, which is its
682/// one-point minimum, and the batch a landing's closures are read in costs one point per fifty
683/// issues per source. `tests/point_cost.rs` prices the document at one point.
684pub const REFERENCE_BATCH: usize = 50;
685/// How many items one [`graphql::ISSUE_COPY_DETAILS`] request reads: each with the first page
686/// of its comments and its `blockedBy`, the largest batch the node-count model prices at one
687/// point.
688///
689/// The six requests' worth an item of [`DETAIL_BATCH`] costs, and its `blockedBy` connection
690/// besides: seven. Twenty-one items come to 147, which rounds to one point; twenty-two come to
691/// 154, which rounds to two. `tests/point_cost.rs` prices the document at one point and fails
692/// if one item more would still be priced at one.
693pub const COPY_DETAIL_BATCH: usize = 21;
694
695/// The most nodes any one document this source sends may be asked to return.
696///
697/// GitHub's own published per-query ceiling, taken from
698/// [`github_graphql_node_count::NODE_LIMIT`] rather than written out again here, so this
699/// workspace cannot hold a stale copy of somebody else's number. A query above it is
700/// **refused before it is executed**, whoever is asking and whatever board they are
701/// asking about — so this is a bound on the documents rather than a budget that runs out.
702///
703/// This is `nodeCount`, the maximum number of nodes *one query may return*. It is not
704/// `cost`, the rate-limit points a call spends against an hourly allowance shared by
705/// everything the credential does — two numbers against two limits, and this constant
706/// bounds only the first. The second is computed offline too, per document:
707/// [`worst_case_point_cost`], pinned for every document in [`graphql::DOCUMENTS`] by
708/// `tests/point_cost.rs`, and reconciled against GitHub's own `cost` by the credentialed
709/// lane. There is no constant like this one to hold a price under, because points are an
710/// hourly allowance rather than a per-call bound.
711///
712/// Neither is a session's price. What `session-cost.md` records of a whole session is its
713/// **requests** and its **worst-case nodes**; what a whole session spends in points is
714/// reported only by a credentialed run's own `x-ratelimit-*` headers, through
715/// [`accounting`]. The module section on the three ways this source reaches an item says how
716/// the count is arrived at, and which of the page sizes below decide it.
717pub const NODE_COUNT_LIMIT: u64 = github_graphql_node_count::NODE_LIMIT;
718
719/// Nested connection size for the connections that hang off one item.
720///
721/// It multiplies through every document that reaches an item under a page — the count
722/// rules multiply down a nested path — so it is the constant [`NODE_COUNT_LIMIT`] is most
723/// sensitive to. `tests/node_count.rs` is what holds the pair together: it recomputes
724/// every document under these constants and fails naming any that reaches the limit, so
725/// raising this is caught there rather than by GitHub.
726const NESTED_PAGE_SIZE: u32 = 50;
727/// How many of one issue's board memberships are read when an issue is reached directly.
728///
729/// An issue reached through a search or through its own node id carries its board half in
730/// `Issue.projectItems`, and only the entry for *this* board is read. This connection sits
731/// under a page of issues, so every point of it multiplies through the whole document and
732/// is paid for whether or not any issue is on a second board — which is why it is
733/// deliberately far smaller than [`NESTED_PAGE_SIZE`].
734///
735/// **Three, because what a page misses is now recovered rather than refused**, and the
736/// recovery is what the value is chosen against. An issue whose entry for this board sits
737/// past this page costs one further request — [`graphql::ISSUE_BOARD_ITEMS`], resumed from
738/// that page's own cursor — so the value trades a bound every read pays for a request only
739/// a multi-board issue pays. At one, a deployment whose issues commonly sit on two or more
740/// boards would pay that request *per issue*, which is order N against the one page per
741/// hundred issues a read costs today. At three it is only reached by an issue on four or
742/// more boards at once, which keeps the recovery path exceptional rather than routine for
743/// a plausible deployment.
744const BOARD_ITEMS_PAGE_SIZE: u32 = 3;
745/// How many carriers of one copy origin one page of [`graphql::ORIGIN_LOOKUP`] asks each of
746/// its two connections for.
747///
748/// An origin names one item, so the answer an origin lookup expects is one carrier, and a
749/// second is a duplicate a copy already takes the first of. Both connections are walked to
750/// exhaustion whatever this is, so it decides how many requests an unusual answer costs and
751/// never what the answer is. It is small because every point of it is paid on every lookup,
752/// and a copy makes one lookup per item it has no link for: at three, ten lookups cost fewer
753/// worst-case nodes than the one whole-board read they replaced.
754const ORIGIN_PAGE_SIZE: u32 = 3;
755
756pub use github_graphql_node_count::{NodeCountError, Variables};
757
758/// The largest value this source can bind to each page-size variable its documents name.
759///
760/// Every `first:` in [`graphql`] reads one of these four, and each is capped at the
761/// constant above it wherever a caller's own limit could reach it — `$first` at
762/// [`MAX_PAGE_SIZE`], `$nestedFirst` at `NESTED_PAGE_SIZE`, `$boardItems` at
763/// `BOARD_ITEMS_PAGE_SIZE`, `$originFirst` at `ORIGIN_PAGE_SIZE`. So this is the worst case a caller can drive this source to,
764/// not one configuration of it, which is what makes a bound computed under it a bound on
765/// every read.
766pub fn largest_page_sizes() -> Variables {
767 Variables::from([
768 ("first".to_owned(), MAX_PAGE_SIZE),
769 ("nestedFirst".to_owned(), NESTED_PAGE_SIZE),
770 ("boardItems".to_owned(), BOARD_ITEMS_PAGE_SIZE),
771 ("originFirst".to_owned(), ORIGIN_PAGE_SIZE),
772 ])
773}
774
775/// The most nodes `document` could be asked to return, by GitHub's published rules.
776///
777/// Computed offline from the document's own text under [`largest_page_sizes`] — no
778/// network, no credential and no schema — by
779/// [`github_graphql_node_count::node_count`], which is where the rules themselves live.
780/// A document at or above [`NODE_COUNT_LIMIT`] is one GitHub refuses before executing, so
781/// this is what a check holds every document in [`graphql::DOCUMENTS`] below.
782///
783/// # Errors
784///
785/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
786/// no single operation, or binds a page size this source does not name — each of which is
787/// a defect in the document rather than a number.
788pub fn worst_case_node_count(document: &str) -> Result<u64, NodeCountError> {
789 node_count(document, &largest_page_sizes())
790}
791
792/// The most rate-limit points one call of `document` could spend, by GitHub's published
793/// rules.
794///
795/// Computed offline from the document's own text under [`largest_page_sizes`] — no
796/// network, no credential and no schema — by
797/// [`github_graphql_node_count::point_cost`], which is where the rules themselves live.
798/// This is `cost`, metered **per hour** against the allowance one credential shares across
799/// everything it does; it is not `nodeCount`, which is [`worst_case_node_count`] and is
800/// bounded per query by [`NODE_COUNT_LIMIT`]. There is no per-call ceiling to hold this
801/// under, so what `tests/point_cost.rs` does with it is pin every document in
802/// [`graphql::DOCUMENTS`] at what it costs, and the credentialed lane reconciles those
803/// figures against GitHub's own reported `cost`.
804///
805/// # Errors
806///
807/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
808/// no single operation, or binds a page size this source does not name — each of which is
809/// a defect in the document rather than a number.
810pub fn worst_case_point_cost(document: &str) -> Result<u64, NodeCountError> {
811 github_graphql_node_count::point_cost(document, &largest_page_sizes())
812}
813
814/// The most nodes `document` could be asked to return under `variables`.
815///
816/// [`worst_case_node_count`] is this under [`largest_page_sizes`], and the accounting in
817/// [`accounting`] is this under the bindings one request really sent — one spelling of the
818/// calculation, so a bound checked offline and a cost recorded at run time cannot come to
819/// disagree. The rules themselves live in [`github_graphql_node_count::node_count`].
820///
821/// # Errors
822///
823/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
824/// no single operation, or binds a page size `variables` does not name.
825pub fn node_count(document: &str, variables: &Variables) -> Result<u64, NodeCountError> {
826 github_graphql_node_count::node_count(document, variables)
827}
828
829/// The issue-title prefix that makes a board issue a document.
830///
831/// A GitHub Projects board has no document type — it holds issues — so the discriminator
832/// is the title, and this is the whole of it: an issue whose title begins with these bytes
833/// is a document and every other issue is the task or project the sub-issue rule makes it.
834///
835/// It is spelled **once**, here, and read rather than restated everywhere else — including
836/// by the shared journeys, which take it from this constant so a board fixture cannot
837/// drift from what this source reads. `docs/metadata.md` records the two consequences that
838/// are not obvious from the bytes: the reported title has this prefix taken off, exactly
839/// as the body's metadata slot is taken off `content`, and this prefix is read *before*
840/// the sub-issue rule, so a design issue with no sub-issues is never an empty project.
841pub const DESIGN_TITLE_PREFIX: &str = "DESIGN: ";
842
843/// Exact GraphQL query documents issued by this plugin.
844///
845/// Keeping the production documents here lets the pinned-schema test validate the same
846/// bytes that are sent to GitHub, rather than a test-only copy which could drift
847/// independently. [`STATUS_OPTIONS_UPDATE`] is the sole document that may rewrite a board
848/// field, and its guarded caller always supplies the complete existing option set with ids.
849pub mod graphql {
850 /// The board half of one item: the field values every document here reads it from.
851 ///
852 /// A macro for the same reason [`board_issue!`] below is one, a level further in. This
853 /// selection is needed by that fragment, by [`BOARD`] under the board's own `items`,
854 /// and by [`ISSUE_BOARD_ITEMS`] under a membership walk — and all three have to produce
855 /// *the same value*, because
856 /// [`GitHubProjectsSource::resolve`](super::GitHubProjectsSource) reads them through
857 /// one path. Three spellings of it is what would drift, so there is one.
858 ///
859 /// The `Status` option and this source's own origin text field are the whole of it. It
860 /// selects no `ProjectV2ItemFieldLabelValue`: GitHub derives that field from the item's
861 /// content, so it holds nothing the content's own `labels` do not already say, and it
862 /// would sit a label connection two page sizes deep.
863 macro_rules! board_item_values {
864 () => {
865 r#"fieldValues(first:$nestedFirst){nodes{
866 ... on ProjectV2ItemFieldSingleSelectValue{name field{
867 ... on ProjectV2SingleSelectField{id name options{id name}}
868 }}
869 ... on ProjectV2ItemFieldTextValue{text field{... on ProjectV2Field{id name}}}
870 }pageInfo{hasNextPage}}"#
871 };
872 }
873
874 /// The variable every document reading an issue declares for the relationship events its
875 /// change token counts, with its value as the default, so no caller binds it.
876 ///
877 /// A variable rather than a literal list because every argument a production document
878 /// passes is a declared variable, which `tests/schema.rs` holds them to, and every member of
879 /// its default to the pinned enum. Its default is the six event types GitHub records a
880 /// relationship added or removed with: a dependency on either end, as `blockedBy` and
881 /// `blocking`, so one a person makes from the far end moves this end's count too; and a
882 /// project membership on the task, as its parent, which GitHub records there whichever
883 /// issue the move was made from. A sub-issue's events on its parent are not counted: a
884 /// project carries no task token, and a copy writes nothing of a task its own sub-issues
885 /// decide. [`super::change_token`] says why the count is in the token.
886 macro_rules! relationship_events_variable {
887 () => {
888 ",$relationshipEvents:[IssueTimelineItemsItemType!]=[BLOCKED_BY_ADDED_EVENT,BLOCKED_BY_REMOVED_EVENT,BLOCKING_ADDED_EVENT,BLOCKING_REMOVED_EVENT,PARENT_ISSUE_ADDED_EVENT,PARENT_ISSUE_REMOVED_EVENT]"
889 };
890 }
891
892 /// Everything this source reads about one issue, wherever it reaches that issue.
893 ///
894 /// A macro rather than a constant so the three documents below can `concat!` it: one
895 /// spelling of these fields is what makes an issue read through the board-scoped
896 /// search, through its own node id, and through its project's sub-issue relationship
897 /// resolve to *the same* item, which is the whole of what
898 /// [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource) relies on.
899 ///
900 /// `projectItems` is what carries the board half of an issue: the board item's own id
901 /// and the [`board_item_values!`] above — the `Status` option and this source's origin
902 /// text field — that a `ProjectV2.items` read used to carry. It is asked for on the
903 /// issue rather than on the board, which is what makes the cost of a read proportional
904 /// to what was asked for instead of to the board's size.
905 ///
906 /// It carries a *page* of that connection, at `BOARD_ITEMS_PAGE_SIZE`, and its
907 /// `endCursor` is what [`ISSUE_BOARD_ITEMS`] resumes from when this board's entry is
908 /// not on that page: a page here is where the search for the entry starts rather than
909 /// where it ends.
910 ///
911 /// It does **not** select the board's `Labels` field value, and that is the whole of
912 /// what keeps the three documents below under [`NODE_COUNT_LIMIT`](super::NODE_COUNT_LIMIT):
913 /// a label connection there sits under `fieldValues` under `projectItems` under a page
914 /// of issues, spending `$nestedFirst` twice down one path, and took
915 /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] to 2,556,100 nodes against a limit of 500,000.
916 /// No label is lost — this is a fragment `on Issue`, whose own `labels` are selected
917 /// above, and that connection is where every label this source reports comes from. No
918 /// document in this module selects the board field any longer, [`BOARD`] included; the
919 /// module documentation records why nothing it could have held is lost.
920 macro_rules! board_issue {
921 () => {
922 concat!(
923 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}
924 labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}
925 projectItems(first:$boardItems){nodes{id updatedAt project{id number}
926 "#,
927 board_item_values!(),
928 r#"}pageInfo{hasNextPage endCursor}}}"#
929 )
930 };
931 }
932
933 /// Every issue of one board, found by a search scoped to that board.
934 ///
935 /// This is how the projects a board holds are listed, and it selects no `items`
936 /// connection on `ProjectV2`: the board is a *qualifier of the search* rather than a
937 /// container walked page by page, so nothing nested inside a board item is paid for.
938 /// Which of the issues it returns is a project is then read off `parent` — GitHub
939 /// accepts `-has:parent` as a search qualifier and silently ignores it, so the
940 /// discriminator has to be applied to the field, which is a scalar on the issue and
941 /// costs nothing.
942 pub const SEARCH_ISSUES: &str = concat!(
943 r#"query($search:String!,$type:SearchType!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
944 relationship_events_variable!(),
945 r#"){
946 search(query:$search,type:$type,first:$first,after:$after){
947 pageInfo{hasNextPage endCursor}
948 nodes{__typename ...BoardIssue}
949 }
950 }"#,
951 board_issue!()
952 );
953
954 /// What a dependency read selects of each far end: enough to say which kind of item it
955 /// is, its body included for the kind marker.
956 macro_rules! related_issue {
957 () => {
958 " fragment Related on Issue{id title body parent{id} subIssuesSummary{total}}"
959 };
960 }
961
962 /// One issue by its own node id, which is what a qualified id names here — with what a
963 /// write of it needs and the issue does not carry in `board_issue!`: the field
964 /// definitions of the boards it sits on, and the far ends of its `blockedBy`.
965 ///
966 /// Strongly consistent, unlike the search above: GitHub's issue search is an index and
967 /// answers a write made moments ago with the value from before it, and resolving a node
968 /// id does not.
969 ///
970 /// **Why those two ride here and not on the fragment.** A copy or an update of an item
971 /// reads it by its own id, and with them that one read answers everything the write
972 /// needs: which option ids the board's `Status` and `Priority` fields hold — so no
973 /// [`BOARD_FIELDS`] — and which issues block it, with each one's kind — so no
974 /// [`ISSUE_DEPENDENCIES`]. On `board_issue!` they would sit under the hundred-issue
975 /// pages of [`SEARCH_ISSUES`] and [`SUB_ISSUES`], multiplying both documents' price. Here
976 /// they sit under one item, and this read is still one point.
977 pub const ISSUE: &str = concat!(
978 r#"query($id:ID!,$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
979 relationship_events_variable!(),
980 r#"){
981 node(id:$id){__typename ...BoardIssue ... on Issue{
982 boards:projectItems(first:$boardItems){nodes{project{id number fields(first:$nestedFirst){nodes{
983 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
984 ... on ProjectV2Field{__typename id name dataType}
985 }pageInfo{hasNextPage}}}}}
986 blockedBy(first:$first){nodes{...Related}pageInfo{hasNextPage endCursor}}
987 }}
988 }"#,
989 board_issue!(),
990 related_issue!()
991 );
992
993 /// One project's tasks: the sub-issues of the issue that project is, each with a page of
994 /// what blocks it.
995 ///
996 /// The work this costs is the project's own size. Nothing about it grows as the board
997 /// gains projects, or as those projects gain tasks.
998 ///
999 /// **Why `blockedBy` rides here and on no other page of issues.** What reads a project's
1000 /// tasks reads their edges next — `project graph` draws them, a copy carries them — and
1001 /// without them here that is one [`ISSUE_DEPENDENCIES`] per task, so the requests a graph
1002 /// costs grow with its tasks rather than with the pages of them. Carried here, a task
1003 /// blocked by no more than `$nestedFirst` issues answers its forward edges from this read,
1004 /// exactly as an [`ISSUE`] read of it does, and only one blocked by more is asked again.
1005 /// It adds a connection under each issue of the page — one rate-limit point per page,
1006 /// and `$nestedFirst` nodes per issue — and is kept off [`SEARCH_ISSUES`], whose pages
1007 /// answer questions that never read an edge.
1008 pub const SUB_ISSUES: &str = concat!(
1009 r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1010 relationship_events_variable!(),
1011 r#"){
1012 node(id:$id){__typename
1013 ... on Issue{subIssues(first:$first,after:$after){
1014 pageInfo{hasNextPage endCursor}
1015 nodes{__typename ...BoardIssue ... on Issue{
1016 blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}
1017 }}
1018 }}}
1019 }"#,
1020 board_issue!(),
1021 related_issue!()
1022 );
1023
1024 /// What a read of the board's own `items` selects of each item's content.
1025 ///
1026 /// A macro for the reason [`board_item_values!`] is one: [`BOARD`] and [`ORIGIN_LOOKUP`]
1027 /// both walk `ProjectV2.items` and hand each item to one resolver, so they select its
1028 /// content by one spelling.
1029 macro_rules! board_item_content {
1030 () => {
1031 r#" content{
1032 ... 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}}}
1033 ... on PullRequest{__typename id}
1034 ... on DraftIssue{__typename id title body createdAt updatedAt}
1035 }"#
1036 };
1037 }
1038
1039 /// Reads the board's fields and one page of its items.
1040 pub const BOARD: &str = concat!(
1041 r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$nestedFirst:Int!,$duplicates:Boolean!"#,
1042 relationship_events_variable!(),
1043 r#"){
1044 owner:repositoryOwner(login:$owner){
1045 ... on ProjectV2Owner{projectV2(number:$number){...Board}}
1046 }
1047 } fragment Board on ProjectV2 { id title
1048 fields(first:$nestedFirst){nodes{
1049 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1050 ... on ProjectV2Field{__typename id name dataType}
1051 }pageInfo{hasNextPage}}
1052 items(first:$first,after:$after){nodes{id updatedAt "#,
1053 board_item_values!(),
1054 board_item_content!(),
1055 r#"} pageInfo{hasNextPage endCursor}}
1056 }"#
1057 );
1058
1059 /// Every carrier of one copy origin, by two reads in one request, and nothing else of
1060 /// the board.
1061 ///
1062 /// **`originItems`** is the board's own items narrowed by its own field filter —
1063 /// `ProjectV2.items(query:)`, which GitHub's schema declares as "Search query for
1064 /// filtering items" — to those whose `onetaskgraph.origin` text field holds the
1065 /// qualified id, quoted. It reads the field every carrier already holds, whichever release
1066 /// wrote it, and matches it exactly: measured on 2026-09-29 against a 394-item board,
1067 /// the quoted, the unquoted and the bare-value spellings each returned exactly the one
1068 /// carrier and a prefix of the value returned none. It is `ProjectV2.items`, so it lags a
1069 /// fresh `addProjectV2ItemById` the way that connection does.
1070 ///
1071 /// **`search`** is the board-scoped issue search for the same id as a quoted phrase in
1072 /// the body, which is where this source mirrors the origin into its metadata slot. GitHub
1073 /// indexes that comment, and the index catches up with a write in a second or two rather
1074 /// than in minutes, so it finds a carrier another process wrote that the first read is
1075 /// still behind on.
1076 ///
1077 /// Each connection pages at `$originFirst`, its own small size — see `ORIGIN_PAGE_SIZE`
1078 /// — and resumes from its own cursor; a connection already walked to its end is resumed
1079 /// from its last cursor, which answers an empty page. Every candidate either read returns
1080 /// is confirmed against its own origin field before it is reported, so a token match of
1081 /// the search or anything else the filter admits never is.
1082 ///
1083 /// The root is aliased `originItems` rather than `owner`, so nothing counting the board's
1084 /// own whole reads counts this one among them.
1085 pub const ORIGIN_LOOKUP: &str = concat!(
1086 r#"query($owner:String!,$number:Int!,$filter:String!,$search:String!,$type:SearchType!,$originFirst:Int!,$itemsAfter:String,$searchAfter:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1087 relationship_events_variable!(),
1088 r#"){
1089 originItems:repositoryOwner(login:$owner){
1090 ... on ProjectV2Owner{projectV2(number:$number){
1091 items(first:$originFirst,after:$itemsAfter,query:$filter){nodes{id updatedAt "#,
1092 board_item_values!(),
1093 board_item_content!(),
1094 r#"} pageInfo{hasNextPage endCursor}}
1095 }}
1096 }
1097 search(query:$search,type:$type,first:$originFirst,after:$searchAfter){
1098 pageInfo{hasNextPage endCursor}
1099 nodes{__typename ...BoardIssue}
1100 }
1101 }"#,
1102 board_issue!()
1103 );
1104
1105 /// Every closed item of the board, by the board's own field filter — what
1106 /// `task status reconcile` reads, and nothing else of the board.
1107 ///
1108 /// **`ProjectV2.items(query:)` narrowed by `$filter`**, which this source binds to
1109 /// `is:closed` alone, so a board whose open items are most of it is not read whole to find
1110 /// the closed ones. It is narrowed rather than trusted: every item it answers is confirmed
1111 /// closed, with a reason and a card that contradict each other, from the item node it
1112 /// already carries, so a filter that also let open items through would cost reads and
1113 /// write nothing wrong.
1114 ///
1115 /// The board's own id and field definitions ride along, as [`BOARD`] carries them, so the
1116 /// option writes that follow need no read of the board. The root is aliased `closedItems`
1117 /// rather than `owner`, so nothing counting the board's own whole reads counts this one
1118 /// among them.
1119 pub const CLOSED_ITEMS: &str = concat!(
1120 r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$filter:String!,$nestedFirst:Int!,$duplicates:Boolean!"#,
1121 relationship_events_variable!(),
1122 r#"){
1123 closedItems:repositoryOwner(login:$owner){
1124 ... on ProjectV2Owner{projectV2(number:$number){id
1125 fields(first:$nestedFirst){nodes{
1126 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1127 ... on ProjectV2Field{__typename id name dataType}
1128 }pageInfo{hasNextPage}}
1129 items(first:$first,after:$after,query:$filter){nodes{id updatedAt "#,
1130 board_item_values!(),
1131 board_item_content!(),
1132 r#"} pageInfo{hasNextPage endCursor}}
1133 }}
1134 }
1135 }"#
1136 );
1137
1138 /// One alias of [`ISSUES_BY_REFERENCE`]: the issue `$owner<n>/$name<n>#$number<n>` names.
1139 macro_rules! reference_alias {
1140 ($n:literal) => {
1141 concat!(
1142 "\n r",
1143 stringify!($n),
1144 ":repository(owner:$owner",
1145 stringify!($n),
1146 ",name:$name",
1147 stringify!($n),
1148 "){issue(number:$number",
1149 stringify!($n),
1150 "){...HeldIssue}}"
1151 )
1152 };
1153 }
1154
1155 /// [`REFERENCE_BATCH`](super::REFERENCE_BATCH) issues at once, each by its repository and
1156 /// number — the one read `task status complete` makes of a source, and nothing else of
1157 /// the board: no `items`, no search and no project read.
1158 ///
1159 /// **Its own fragment, lighter than `board_issue!`, and that is what makes fifty
1160 /// references one point.** It selects no labels, and of each board membership only the
1161 /// `Status` value, by name, with `fieldValueByName` — so each alias asks GitHub for one
1162 /// connection, its page of memberships, where the whole fragment asks for five. What a
1163 /// completion writes is a card and, at most, the issue's state, so the labels and the other
1164 /// field values it leaves out are never what it needs; an item read this way is held for
1165 /// that write alone and never answers another read. An issue whose entry for this board is
1166 /// past its first page of memberships is walked with [`ISSUE_BOARD_ITEMS`], exactly as
1167 /// every other read of an issue recovers one.
1168 ///
1169 /// Fixed-size, as [`ISSUE_DETAILS`] is, so there is one document to price: a batch of
1170 /// fewer references binds the slots it has none for to its last one.
1171 pub const ISSUES_BY_REFERENCE: &str = concat!(
1172 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!"#,
1173 relationship_events_variable!(),
1174 r#"){"#,
1175 reference_alias!(0),
1176 reference_alias!(1),
1177 reference_alias!(2),
1178 reference_alias!(3),
1179 reference_alias!(4),
1180 reference_alias!(5),
1181 reference_alias!(6),
1182 reference_alias!(7),
1183 reference_alias!(8),
1184 reference_alias!(9),
1185 reference_alias!(10),
1186 reference_alias!(11),
1187 reference_alias!(12),
1188 reference_alias!(13),
1189 reference_alias!(14),
1190 reference_alias!(15),
1191 reference_alias!(16),
1192 reference_alias!(17),
1193 reference_alias!(18),
1194 reference_alias!(19),
1195 reference_alias!(20),
1196 reference_alias!(21),
1197 reference_alias!(22),
1198 reference_alias!(23),
1199 reference_alias!(24),
1200 reference_alias!(25),
1201 reference_alias!(26),
1202 reference_alias!(27),
1203 reference_alias!(28),
1204 reference_alias!(29),
1205 reference_alias!(30),
1206 reference_alias!(31),
1207 reference_alias!(32),
1208 reference_alias!(33),
1209 reference_alias!(34),
1210 reference_alias!(35),
1211 reference_alias!(36),
1212 reference_alias!(37),
1213 reference_alias!(38),
1214 reference_alias!(39),
1215 reference_alias!(40),
1216 reference_alias!(41),
1217 reference_alias!(42),
1218 reference_alias!(43),
1219 reference_alias!(44),
1220 reference_alias!(45),
1221 reference_alias!(46),
1222 reference_alias!(47),
1223 reference_alias!(48),
1224 reference_alias!(49),
1225 "\n }",
1226 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}
1227 projectItems(first:$boardItems){nodes{id updatedAt project{id number}
1228 status:fieldValueByName(name:$statusField){... on ProjectV2ItemFieldSingleSelectValue{name field{
1229 ... on ProjectV2SingleSelectField{id name options{id name}}
1230 }}}
1231 }pageInfo{hasNextPage endCursor}}}"#
1232 );
1233
1234 /// The board's own id and field definitions, and not one of its items.
1235 ///
1236 /// What a write needs of the board when the item it writes does not say: the id a field
1237 /// write and `addProjectV2ItemById` address, and the definitions of the `Status` and
1238 /// origin fields. It selects no `items`, so what it costs is the board's field list
1239 /// however many items the board holds — and it decides nothing about which items those
1240 /// are, which is the question a read of one item by its own id answers instead.
1241 ///
1242 /// The root is aliased `boardFields` rather than `owner`, so nothing counting the
1243 /// board's item reads by their root counts this one among them.
1244 pub const BOARD_FIELDS: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!){
1245 boardFields:repositoryOwner(login:$owner){
1246 ... on ProjectV2Owner{projectV2(number:$number){id
1247 fields(first:$nestedFirst){nodes{
1248 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1249 ... on ProjectV2Field{__typename id name dataType}
1250 }pageInfo{hasNextPage}}
1251 }}
1252 }
1253 }"#;
1254
1255 /// One board draft by its own node id, with the board item it sits in.
1256 ///
1257 /// A draft is not an issue, so [`ISSUE`] reaches it and reads nothing of it; this is the
1258 /// second read that answers it. `DraftIssue.projectV2Items` names the board item a draft
1259 /// is — GitHub links a draft to one item — with the same [`board_item_values!`] the
1260 /// issue fragment reads, so a draft reached by id resolves through the same resolver a
1261 /// board listing hands it to, and nothing has to list the board to find one.
1262 pub const DRAFT: &str = concat!(
1263 r#"query($id:ID!,$nestedFirst:Int!,$boardItems:Int!){
1264 node(id:$id){__typename ... on DraftIssue{id title body createdAt updatedAt
1265 projectV2Items(first:$boardItems){nodes{id project{id number}
1266 "#,
1267 board_item_values!(),
1268 r#"}pageInfo{hasNextPage endCursor}}}}
1269 }"#
1270 );
1271
1272 /// One issue's board memberships alone, walked past the page a read of it carried.
1273 ///
1274 /// The recovery read behind [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource):
1275 /// every document above carries a *page* of `Issue.projectItems`, and an issue on more
1276 /// boards than that page holds may have this board's entry past its end. This asks that
1277 /// one issue for its memberships and nothing else — the caller already holds the issue —
1278 /// so an answer of "this board does not hold it" is only ever given about a connection
1279 /// read to exhaustion.
1280 ///
1281 /// It selects the board item's id, its project number and the same
1282 /// [`board_item_values!`] the fragment does, because what it produces is handed to the
1283 /// very same resolver: an issue recovered this way reports the same title, the same
1284 /// status, the same labels and the same qualified id as one whose entry was on the
1285 /// page.
1286 ///
1287 /// `$first` rather than `$boardItems`: this document reads one issue, so nothing
1288 /// multiplies through it and the membership connection can be walked at
1289 /// [`MAX_PAGE_SIZE`](super::MAX_PAGE_SIZE) — which is what keeps the recovery to one
1290 /// further request for any issue a person really keeps.
1291 pub const ISSUE_BOARD_ITEMS: &str = concat!(
1292 r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!){
1293 node(id:$id){
1294 ... on Issue{projectItems(first:$first,after:$after){
1295 nodes{id updatedAt project{id number}
1296 "#,
1297 board_item_values!(),
1298 r#"}
1299 pageInfo{hasNextPage endCursor}}}
1300 }
1301 }"#
1302 );
1303 /// Whether the board's Project is public — half of what decides whether a write here can
1304 /// be read by anybody. Needs the `read:project` scope.
1305 pub const PROJECT_VISIBILITY: &str = r#"query($owner:String!,$number:Int!){visibility:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){public}}}}"#;
1306 /// Resolves the configured repository's node id, which creating an issue requires.
1307 pub const REPOSITORY: &str = r#"query($owner:String!,$name:String!){repository(owner:$owner,name:$name){id nameWithOwner}}"#;
1308 /// What creating an issue needs and has not read yet: the board's own id and field
1309 /// definitions, as [`BOARD_FIELDS`] reads them, and the node id of the repository the
1310 /// issue is created in, as [`REPOSITORY`] reads it — in one request.
1311 ///
1312 /// Sent at the point a create knows which repository it is for, when neither half is
1313 /// already known to this process; a create needing only one of them sends that one's own
1314 /// document. Neither half is kept past the process: a field's option ids are re-minted by
1315 /// `sources fields --apply`, so a copy of them held between runs would write the wrong
1316 /// status.
1317 pub const CREATION_CONTEXT: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!,$repositoryOwner:String!,$repositoryName:String!){
1318 boardFields:repositoryOwner(login:$owner){
1319 ... on ProjectV2Owner{projectV2(number:$number){id
1320 fields(first:$nestedFirst){nodes{
1321 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1322 ... on ProjectV2Field{__typename id name dataType}
1323 }pageInfo{hasNextPage}}
1324 }}
1325 }
1326 repository(owner:$repositoryOwner,name:$repositoryName){id nameWithOwner}
1327 }"#;
1328 /// Reads both dependency directions for one issue, with each far end's own kind — and
1329 /// the issue's own body, which is where an edge to another source is recorded, so that
1330 /// half of a dependency read needs no second read of the issue or of the board.
1331 pub const ISSUE_DEPENDENCIES: &str = concat!(
1332 r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
1333 ... on Issue{body
1334 blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1335 blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1336 }}}"#,
1337 related_issue!()
1338 );
1339 /// Creates one issue in the configured repository, on no board: [`ADD_TO_BOARD`] files
1340 /// it. `CreateIssueInput.projectV2Ids` is not sent — see the crate's notes on what GitHub
1341 /// answered when it was.
1342 pub const CREATE_ISSUE: &str =
1343 r#"mutation($input:CreateIssueInput!){createIssue(input:$input){issue{id number url}}}"#;
1344 /// Puts an existing issue on the configured board.
1345 pub const ADD_TO_BOARD: &str = r#"mutation($input:AddProjectV2ItemByIdInput!){addProjectV2ItemById(input:$input){item{id}}}"#;
1346 /// Updates an issue's visible fields and its open or closed state in one call.
1347 pub const UPDATE_ISSUE: &str =
1348 r#"mutation($input:UpdateIssueInput!){updateIssue(input:$input){issue{id}}}"#;
1349 /// Updates an existing draft's user-visible fields.
1350 pub const UPDATE_DRAFT: &str = r#"mutation($input:UpdateProjectV2DraftIssueInput!){updateProjectV2DraftIssue(input:$input){draftIssue{id}}}"#;
1351 /// Updates a text or single-select value on one project item.
1352 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}}}}}}}}"#;
1353 /// Writes up to six board fields and clears up to three in one ordered mutation: every
1354 /// aliased field is included by its own boolean, as [`FIELD_WRITE_SLOTS`](super::FIELD_WRITE_SLOTS)
1355 /// and [`FIELD_CLEAR_SLOTS`](super::FIELD_CLEAR_SLOTS) name them, writes before clears.
1356 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}}}"#;
1357 /// Clears one project item's value of one field, which is what a `none` priority is.
1358 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}}}}}}}}"#;
1359 /// Creates one field: a single-select one with its options, or a text one for a projected
1360 /// metadata value. Only the guarded field setup may use this document, and only for a
1361 /// field the board lacks.
1362 pub const CREATE_FIELD: &str = r#"mutation($input:CreateProjectV2FieldInput!){createProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id name options{id name color description}}}}}"#;
1363 /// Replaces a single-select field's options. Only the guarded field setup — the
1364 /// `status-options` and `fields` operations — may use this document, because GitHub
1365 /// treats the input as the complete option list.
1366 pub const STATUS_OPTIONS_UPDATE: &str = r#"mutation($input:UpdateProjectV2FieldInput!){updateProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id options{id name color description}}}}}"#;
1367 /// A fresh snapshot of the Status field and every board item's assignment.
1368 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}}}}}}"#;
1369 /// Files one issue under another as a sub-issue, which is what project membership is.
1370 pub const ADD_SUB_ISSUE: &str =
1371 r#"mutation($input:AddSubIssueInput!){addSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1372 /// Takes one issue back out of its parent.
1373 pub const REMOVE_SUB_ISSUE: &str = r#"mutation($input:RemoveSubIssueInput!){removeSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1374 /// Adds GitHub's native issue blocked-by relationship.
1375 pub const ADD_BLOCKED_BY: &str = r#"mutation($input:AddBlockedByInput!){addBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1376 /// Removes one native issue blocked-by relationship.
1377 pub const REMOVE_BLOCKED_BY: &str = r#"mutation($input:RemoveBlockedByInput!){removeBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1378 /// Deletes one issue, which takes its board item with it.
1379 ///
1380 /// The engine sends this in one situation only: undoing a copy that could not finish,
1381 /// over the items that same copy created. Deleting the issue removes the board item
1382 /// too, so there is no second `deleteProjectV2Item` to keep in step with it.
1383 pub const DELETE_ISSUE: &str =
1384 r#"mutation($input:DeleteIssueInput!){deleteIssue(input:$input){repository{id}}}"#;
1385
1386 /// Everything this source reads about one issue comment, wherever it reaches one.
1387 ///
1388 /// A macro for the reason [`board_issue!`] is one: a comment listed, a comment just added
1389 /// and a comment just edited are handed to one mapper, so they are selected by one
1390 /// spelling. `author` is `Actor`, which GitHub answers `null` for an account that no
1391 /// longer exists, and `login` is the one member every kind of actor carries.
1392 macro_rules! issue_comment {
1393 () => {
1394 "id author{login} createdAt updatedAt body url"
1395 };
1396 }
1397
1398 /// One task's comments: a page of its issue's own `comments` connection.
1399 ///
1400 /// **No `orderBy`, and that is what makes the page oldest first.** GitHub's only
1401 /// `IssueCommentOrder` field is `UPDATED_AT`, which would move a comment to the end of the
1402 /// list every time somebody edited it; left unordered the connection answers in the order
1403 /// the comments were written, which is the order GitHub documents for the same collection
1404 /// over REST — ascending id. Nothing multiplies through it, so `$first` is the whole of its
1405 /// node count and the caller's own page size is pushed straight down.
1406 pub const ISSUE_COMMENTS: &str = concat!(
1407 r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename ... on Issue{comments(first:$first,after:$after){nodes{"#,
1408 issue_comment!(),
1409 r#"}pageInfo{hasNextPage endCursor}}}}}"#
1410 );
1411 /// One issue by its own node id, with a page of its comments: what `task show` and a
1412 /// comment listing read, in one request.
1413 ///
1414 /// [`ISSUE`] and [`ISSUE_COMMENTS`] in one document, rather than one then the other. The
1415 /// comments are selected here and **not** on the shared `board_issue!` fragment, which
1416 /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] nest under a page of a hundred issues: a comment
1417 /// connection there would multiply through both of those documents' price, and neither
1418 /// needs one.
1419 pub const ISSUE_DETAIL: &str = concat!(
1420 r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1421 relationship_events_variable!(),
1422 r#"){
1423 node(id:$id){__typename ...BoardIssue ... on Issue{comments(first:$first,after:$after){nodes{"#,
1424 issue_comment!(),
1425 r#"}pageInfo{hasNextPage endCursor}}}}
1426 }"#,
1427 board_issue!()
1428 );
1429
1430 /// One alias of [`ISSUE_DETAILS`]: the item a batch's `$id<n>` names, with the first
1431 /// page of its comments when `$comments` asks for them.
1432 macro_rules! issue_details_alias {
1433 ($n:literal) => {
1434 concat!(
1435 "\n i",
1436 stringify!($n),
1437 ":node(id:$id",
1438 stringify!($n),
1439 "){__typename ...BoardIssue ... on Issue{comments(first:$first) @include(if:$comments){nodes{",
1440 issue_comment!(),
1441 "}pageInfo{hasNextPage endCursor}}}}"
1442 )
1443 };
1444 }
1445
1446 /// [`ISSUE_DETAIL`] for [`DETAIL_BATCH`](super::DETAIL_BATCH) items at once, each by its
1447 /// own node id, as one fixed-size document of aliased `node(id:)` fields.
1448 ///
1449 /// **Aliased `node(id:)` rather than `nodes(ids:)`, and that is what keeps its price
1450 /// honest.** The `github-graphql-node-count` model this workspace prices with treats a
1451 /// field that supplies neither `first` nor `last` as free, and `nodes(ids:)` supplies
1452 /// neither — so every connection under it would be priced at nothing and the pin in
1453 /// `tests/point_cost.rs` would understate what GitHub charges. Each alias here is the
1454 /// one-item read the model already prices, so the batch costs what its aliases cost.
1455 ///
1456 /// **Fixed-size, so there is one document to price.** A batch of fewer items binds the
1457 /// slots it has no item for to the last item it does, and reads that item again; the
1458 /// price is the document's, whatever its variables, so a short batch costs what a full
1459 /// one does and nothing more.
1460 pub const ISSUE_DETAILS: &str = concat!(
1461 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!"#,
1462 relationship_events_variable!(),
1463 r#"){"#,
1464 issue_details_alias!(0),
1465 issue_details_alias!(1),
1466 issue_details_alias!(2),
1467 issue_details_alias!(3),
1468 issue_details_alias!(4),
1469 issue_details_alias!(5),
1470 issue_details_alias!(6),
1471 issue_details_alias!(7),
1472 issue_details_alias!(8),
1473 issue_details_alias!(9),
1474 issue_details_alias!(10),
1475 issue_details_alias!(11),
1476 issue_details_alias!(12),
1477 issue_details_alias!(13),
1478 issue_details_alias!(14),
1479 issue_details_alias!(15),
1480 issue_details_alias!(16),
1481 issue_details_alias!(17),
1482 issue_details_alias!(18),
1483 issue_details_alias!(19),
1484 issue_details_alias!(20),
1485 issue_details_alias!(21),
1486 issue_details_alias!(22),
1487 issue_details_alias!(23),
1488 "\n }",
1489 board_issue!()
1490 );
1491
1492 /// One alias of [`ISSUE_COPY_DETAILS`]: the item a batch's `$id<n>` names, with the first
1493 /// page of its comments and the issues it is blocked by.
1494 macro_rules! issue_copy_details_alias {
1495 ($n:literal) => {
1496 concat!(
1497 "\n i",
1498 stringify!($n),
1499 ":node(id:$id",
1500 stringify!($n),
1501 "){__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{",
1502 issue_comment!(),
1503 "}pageInfo{hasNextPage endCursor}} blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}}}"
1504 )
1505 };
1506 }
1507
1508 /// [`ISSUE_DETAILS`] as a copy reads the tasks it carries: each item with the first page of
1509 /// its comments and its `blockedBy` — the edges a copy reads of every task next, which
1510 /// [`ISSUE`] carries and [`ISSUE_DETAILS`] does not — [`COPY_DETAIL_BATCH`](super::COPY_DETAIL_BATCH)
1511 /// at a time, so a copy of a changed task costs this read and nothing else.
1512 ///
1513 /// A document of its own rather than a widening of [`ISSUE_DETAILS`], whose batch size is
1514 /// what `task show-many` is priced by and which reads no edge.
1515 pub const ISSUE_COPY_DETAILS: &str = concat!(
1516 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!,$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1517 relationship_events_variable!(),
1518 r#"){"#,
1519 issue_copy_details_alias!(0),
1520 issue_copy_details_alias!(1),
1521 issue_copy_details_alias!(2),
1522 issue_copy_details_alias!(3),
1523 issue_copy_details_alias!(4),
1524 issue_copy_details_alias!(5),
1525 issue_copy_details_alias!(6),
1526 issue_copy_details_alias!(7),
1527 issue_copy_details_alias!(8),
1528 issue_copy_details_alias!(9),
1529 issue_copy_details_alias!(10),
1530 issue_copy_details_alias!(11),
1531 issue_copy_details_alias!(12),
1532 issue_copy_details_alias!(13),
1533 issue_copy_details_alias!(14),
1534 issue_copy_details_alias!(15),
1535 issue_copy_details_alias!(16),
1536 issue_copy_details_alias!(17),
1537 issue_copy_details_alias!(18),
1538 issue_copy_details_alias!(19),
1539 issue_copy_details_alias!(20),
1540 "\n }",
1541 board_issue!(),
1542 related_issue!()
1543 );
1544
1545 /// Which issue one comment is on, read before that comment is edited or removed.
1546 ///
1547 /// GitHub's comment mutations take the comment's id and nothing else, so without this a
1548 /// comment id given against the wrong task would change a comment on another issue.
1549 pub const COMMENT_ISSUE: &str =
1550 r#"query($id:ID!){node(id:$id){__typename ... on IssueComment{id body issue{id}}}}"#;
1551 /// Adds one comment to an issue, signed as the account the token belongs to.
1552 pub const ADD_COMMENT: &str = concat!(
1553 r#"mutation($input:AddCommentInput!){addComment(input:$input){subject{id} commentEdge{node{"#,
1554 issue_comment!(),
1555 r#"}}}}"#
1556 );
1557 /// Replaces the body of one issue comment.
1558 pub const UPDATE_COMMENT: &str = concat!(
1559 r#"mutation($input:UpdateIssueCommentInput!){updateIssueComment(input:$input){issueComment{"#,
1560 issue_comment!(),
1561 r#"}}}"#
1562 );
1563 /// Removes one issue comment. Its payload carries nothing about the comment it removed.
1564 pub const DELETE_COMMENT: &str = r#"mutation($input:DeleteIssueCommentInput!){deleteIssueComment(input:$input){clientMutationId}}"#;
1565
1566 /// What a mutation of a write the store's cache expects selects of the issue it wrote:
1567 /// everything [`ISSUE_DETAIL`] reads of it, so its response shows the task whole — its
1568 /// change token's every part and the first page of its comments.
1569 ///
1570 /// Sent **only** while that write runs, in place of the document named beside each
1571 /// below, so every other write's documents, prices and request counts are what they
1572 /// were. GitHub's token waits for a task to settle (`change_token::SETTLED`), and a
1573 /// mutation's response is stamped seconds after the change it made, so against GitHub
1574 /// itself a response rarely determines a version; the cache is then left unserveable
1575 /// for the task, and its next read refreshes it.
1576 macro_rules! whole_issue {
1577 () => {
1578 concat!(
1579 r#"__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{"#,
1580 issue_comment!(),
1581 r#"}pageInfo{hasNextPage endCursor}}}"#
1582 )
1583 };
1584 }
1585
1586 /// The variables every document of `whole_issue!` declares beside its own.
1587 macro_rules! whole_variables {
1588 () => {
1589 concat!(
1590 r#",$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1591 relationship_events_variable!()
1592 )
1593 };
1594 }
1595
1596 /// [`ADD_COMMENT`], selecting the whole issue commented on.
1597 pub const ADD_COMMENT_WHOLE: &str = concat!(
1598 r#"mutation($input:AddCommentInput!"#,
1599 whole_variables!(),
1600 r#"){addComment(input:$input){subject{id "#,
1601 whole_issue!(),
1602 r#"} commentEdge{node{"#,
1603 issue_comment!(),
1604 r#"}}}}"#,
1605 board_issue!()
1606 );
1607 /// [`UPDATE_COMMENT`], selecting the whole issue the comment is on.
1608 pub const UPDATE_COMMENT_WHOLE: &str = concat!(
1609 r#"mutation($input:UpdateIssueCommentInput!"#,
1610 whole_variables!(),
1611 r#"){updateIssueComment(input:$input){issueComment{"#,
1612 issue_comment!(),
1613 r#" issue{"#,
1614 whole_issue!(),
1615 r#"}}}}"#,
1616 board_issue!()
1617 );
1618 /// [`UPDATE_ISSUE`], selecting the whole issue updated.
1619 pub const UPDATE_ISSUE_WHOLE: &str = concat!(
1620 r#"mutation($input:UpdateIssueInput!"#,
1621 whole_variables!(),
1622 r#"){updateIssue(input:$input){issue{id "#,
1623 whole_issue!(),
1624 r#"}}}"#,
1625 board_issue!()
1626 );
1627 /// [`UPDATE_FIELD`], selecting the whole issue behind the board item written.
1628 pub const UPDATE_FIELD_WHOLE: &str = concat!(
1629 r#"mutation($input:UpdateProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
1630 whole_variables!(),
1631 r#"){updateProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
1632 whole_issue!(),
1633 r#"}}}}"#,
1634 board_issue!()
1635 );
1636 /// [`CLEAR_FIELD`], selecting the whole issue behind the board item cleared.
1637 pub const CLEAR_FIELD_WHOLE: &str = concat!(
1638 r#"mutation($input:ClearProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
1639 whole_variables!(),
1640 r#"){clearProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
1641 whole_issue!(),
1642 r#"}}}}"#,
1643 board_issue!()
1644 );
1645
1646 /// The document sent in `document`'s place while a write the store's cache expects runs,
1647 /// when it has one.
1648 #[must_use]
1649 pub fn whole_twin(document: &str) -> Option<&'static str> {
1650 [
1651 (ADD_COMMENT, ADD_COMMENT_WHOLE),
1652 (UPDATE_COMMENT, UPDATE_COMMENT_WHOLE),
1653 (UPDATE_ISSUE, UPDATE_ISSUE_WHOLE),
1654 (UPDATE_FIELD, UPDATE_FIELD_WHOLE),
1655 (CLEAR_FIELD, CLEAR_FIELD_WHOLE),
1656 ]
1657 .into_iter()
1658 .find(|(plain, _)| *plain == document)
1659 .map(|(_, whole)| whole)
1660 }
1661
1662 /// Every document above, with what this source is doing when it sends one.
1663 ///
1664 /// One list rather than a `match` beside the constants: a rate-limit diagnostic has to
1665 /// name the call that was refused, and a `match` with a catch-all arm would answer a
1666 /// document added later with "talking to GitHub" and never say so.
1667 ///
1668 /// `documents_are_all_inventoried` reads this file back and fails naming any `pub
1669 /// const` here that this list omits, so the two cannot part — which is the same guard
1670 /// `CATEGORIES` carries, in the one shape available to a set of `&str` constants.
1671 pub const DOCUMENTS: [(&str, &str); 42] = [
1672 (SEARCH_ISSUES, "searching this board's issues"),
1673 (ISSUE, "reading one issue"),
1674 (
1675 ISSUE_BOARD_ITEMS,
1676 "reading one issue's board memberships past the page it came with",
1677 ),
1678 (SUB_ISSUES, "reading a project's tasks"),
1679 (BOARD, "reading the board"),
1680 (ORIGIN_LOOKUP, "looking up the items copied from one origin"),
1681 (CLOSED_ITEMS, "reading the board's closed items"),
1682 (
1683 ISSUES_BY_REFERENCE,
1684 "reading a batch of issues by their references",
1685 ),
1686 (BOARD_FIELDS, "reading the board's fields"),
1687 (DRAFT, "reading one draft"),
1688 (REPOSITORY, "reading the destination repository"),
1689 (
1690 PROJECT_VISIBILITY,
1691 "reading whether the board's project is public",
1692 ),
1693 (
1694 CREATION_CONTEXT,
1695 "reading the board's fields and the destination repository",
1696 ),
1697 (ISSUE_DEPENDENCIES, "reading an issue's dependencies"),
1698 (CREATE_ISSUE, "creating an issue"),
1699 (ADD_TO_BOARD, "adding an issue to the board"),
1700 (UPDATE_ISSUE, "updating an issue"),
1701 (UPDATE_DRAFT, "updating a draft item"),
1702 (UPDATE_FIELD, "writing a board field"),
1703 (UPDATE_FIELDS, "writing board fields together"),
1704 (CLEAR_FIELD, "clearing a board field"),
1705 (
1706 CREATE_FIELD,
1707 "creating a board single-select field with its options",
1708 ),
1709 (
1710 STATUS_OPTIONS_SNAPSHOT,
1711 "snapshotting board Status options and assignments",
1712 ),
1713 (
1714 STATUS_OPTIONS_UPDATE,
1715 "safely replacing the board Status option list",
1716 ),
1717 (ADD_SUB_ISSUE, "filing an issue under its project"),
1718 (REMOVE_SUB_ISSUE, "taking an issue out of its project"),
1719 (ADD_BLOCKED_BY, "recording a dependency"),
1720 (REMOVE_BLOCKED_BY, "removing a dependency"),
1721 (DELETE_ISSUE, "deleting an issue"),
1722 (ISSUE_COMMENTS, "reading a task's comments"),
1723 (ISSUE_DETAIL, "reading one issue with its comments"),
1724 (
1725 ISSUE_DETAILS,
1726 "reading a batch of issues with their comments",
1727 ),
1728 (
1729 ISSUE_COPY_DETAILS,
1730 "reading a batch of issues with their comments and dependencies for a copy",
1731 ),
1732 (COMMENT_ISSUE, "reading which issue a comment is on"),
1733 (ADD_COMMENT, "adding a comment"),
1734 (UPDATE_COMMENT, "editing a comment"),
1735 (DELETE_COMMENT, "deleting a comment"),
1736 (
1737 ADD_COMMENT_WHOLE,
1738 "adding a comment, answered with the whole issue",
1739 ),
1740 (
1741 UPDATE_COMMENT_WHOLE,
1742 "editing a comment, answered with the whole issue",
1743 ),
1744 (
1745 UPDATE_ISSUE_WHOLE,
1746 "updating an issue, answered with it whole",
1747 ),
1748 (
1749 UPDATE_FIELD_WHOLE,
1750 "writing a board field, answered with the whole issue",
1751 ),
1752 (
1753 CLEAR_FIELD_WHOLE,
1754 "clearing a board field, answered with the whole issue",
1755 ),
1756 ];
1757}
1758
1759/// One aliased mutation field of [`graphql::UPDATE_FIELDS`]: the key its answer comes back
1760/// under, the variable carrying its input, and the boolean variable that includes it.
1761#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1762pub struct FieldSlot {
1763 /// The key of the answer's `data` this field's payload is under.
1764 pub alias: &'static str,
1765 /// The variable carrying this field's input.
1766 pub variable: &'static str,
1767 /// The boolean variable whose `true` sends this field.
1768 pub include: &'static str,
1769}
1770
1771/// The value writes [`graphql::UPDATE_FIELDS`] carries, in the order GitHub runs them.
1772pub const FIELD_WRITE_SLOTS: [FieldSlot; 6] = [
1773 FieldSlot {
1774 alias: "updateProjectV2ItemFieldValue",
1775 variable: "input",
1776 include: "writeFirst",
1777 },
1778 FieldSlot {
1779 alias: "second",
1780 variable: "second",
1781 include: "writeSecond",
1782 },
1783 FieldSlot {
1784 alias: "third",
1785 variable: "third",
1786 include: "writeThird",
1787 },
1788 FieldSlot {
1789 alias: "fourth",
1790 variable: "fourth",
1791 include: "writeFourth",
1792 },
1793 FieldSlot {
1794 alias: "fifth",
1795 variable: "fifth",
1796 include: "writeFifth",
1797 },
1798 FieldSlot {
1799 alias: "sixth",
1800 variable: "sixth",
1801 include: "writeSixth",
1802 },
1803];
1804
1805/// The clears [`graphql::UPDATE_FIELDS`] carries, run after every write of the same request.
1806pub const FIELD_CLEAR_SLOTS: [FieldSlot; 3] = [
1807 FieldSlot {
1808 alias: "cleared",
1809 variable: "clear",
1810 include: "writeClear",
1811 },
1812 FieldSlot {
1813 alias: "clearedSecond",
1814 variable: "clearSecond",
1815 include: "writeClearSecond",
1816 },
1817 FieldSlot {
1818 alias: "clearedThird",
1819 variable: "clearThird",
1820 include: "writeClearThird",
1821 },
1822];
1823
1824/// Which of GitHub's two rate limiters refused a request.
1825///
1826/// Waiting is the whole answer to the primary budget, and polling is what *extends* the
1827/// secondary one — so an operator told the wrong one takes the wrong next step, which is
1828/// the whole reason this is carried rather than collapsed into "rate limited".
1829#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1830enum Limiter {
1831 /// The hourly API budget, which `gh api rate_limit` reports and a wait answers.
1832 Primary,
1833 /// The burst limiter over content-generating requests, which nothing reports.
1834 Secondary,
1835}
1836
1837/// The wordings GitHub answers a secondary rate limit with.
1838///
1839/// It sends them under a forbidden status, under a too-many-requests status, and inside
1840/// the `errors` of a *successful* response, which is why the text is what this matches on
1841/// rather than the status. `abuse detection` is the wording GitHub used before the
1842/// limiter was renamed and still returns from some endpoints; `submitted too quickly` is
1843/// what a burst of content creation is refused with.
1844///
1845/// This is GitHub's vocabulary rather than this source's, so it is pinned rather than
1846/// remembered: `tests/fixtures/rate-limits.json` records where each wording was read and
1847/// when, and the drift gate reconciles the two lists both ways. Public for that gate
1848/// alone — a caller has no use for it, and matching on a refusal is this source's job.
1849pub const SECONDARY_WORDINGS: [&str; 5] = [
1850 "secondary rate limit",
1851 "temporarily blocked from content creation",
1852 "abuse detection",
1853 "submitted too quickly",
1854 "exceeded a secondary",
1855];
1856
1857/// The wordings GitHub answers an exhausted primary budget with.
1858///
1859/// `rate_limited` is the `type` its GraphQL error carries, which is read as a field rather
1860/// than looked for in the response text. `api rate limit already exceeded` is what GraphQL
1861/// answers a request made once the hour's budget is spent — "API rate limit already exceeded
1862/// for user ID …" in the `errors` of an HTTP 200, with no `type` — and neither of the other
1863/// two phrases is a substring of it, so without it that answer read as a refusal that will
1864/// never lift. Pinned and gated exactly as [`SECONDARY_WORDINGS`] is, and public for the same
1865/// one reason.
1866pub const PRIMARY_WORDINGS: [&str; 4] = [
1867 "api rate limit exceeded",
1868 "api rate limit already exceeded",
1869 "rate limit exceeded",
1870 "rate_limited",
1871];
1872
1873/// What a response *says about itself*, which is the only place a refusal can be read.
1874///
1875/// Deliberately not the whole response body. A board is a place people write about their
1876/// own work, and a task on it titled "the secondary rate limit" would, matched across the
1877/// raw text, turn a perfectly good answer into a refusal this source then waited out and
1878/// reported. So the item data is never read: what is read is GitHub's own REST-style
1879/// `message` envelope, which is what a forbidden status carries, and the `message` and
1880/// `type` of each GraphQL error, which is where a *successful* response says it.
1881///
1882/// A body that is not JSON at all has nothing structured to read, so only a failing
1883/// response's own text is taken — a successful response that is not JSON is malformed
1884/// rather than refused, and [`GitHubProjectsSource::answer`] says so.
1885fn refusal_wording(status: StatusCode, body: &str) -> String {
1886 let Ok(parsed) = serde_json::from_str::<Value>(body) else {
1887 return if status.is_success() {
1888 String::new()
1889 } else {
1890 body.to_owned()
1891 };
1892 };
1893 let mut said: Vec<&str> = parsed
1894 .get("message")
1895 .and_then(Value::as_str)
1896 .into_iter()
1897 .collect();
1898 if let Some(errors) = parsed.get("errors").and_then(Value::as_array) {
1899 for error in errors {
1900 said.extend(
1901 ["message", "type"]
1902 .into_iter()
1903 .filter_map(|key| error.get(key).and_then(Value::as_str)),
1904 );
1905 }
1906 }
1907 said.join("; ")
1908}
1909
1910impl Limiter {
1911 /// Which limiter refused this response, or `None` when none of them did.
1912 ///
1913 /// The wording is read first and the status only decides what carries none of it,
1914 /// because GitHub answers a secondary limit with a forbidden status far more often
1915 /// than with too-many-requests — while a forbidden status saying nothing about a limit
1916 /// really is a credential this token lacks.
1917 ///
1918 /// A response is a refusal because of its status or its own wording. A spent budget
1919 /// only ever explains one; it never turns an answer into a refusal.
1920 fn classify(status: StatusCode, budget_exhausted: bool, body: &str) -> Option<Self> {
1921 let normalized = refusal_wording(status, body).to_ascii_lowercase();
1922 if SECONDARY_WORDINGS
1923 .iter()
1924 .any(|wording| normalized.contains(wording))
1925 {
1926 return Some(Self::Secondary);
1927 }
1928 if status == StatusCode::TOO_MANY_REQUESTS {
1929 return Some(Self::Primary);
1930 }
1931 // An exhausted budget *explains* a response that failed; it does not make one that
1932 // succeeded into a failure. GitHub sets `x-ratelimit-remaining: 0` on the last
1933 // request the budget allowed as well as on the ones it then refuses, so reading
1934 // the header alone threw away a good answer — and, once refusals were retried,
1935 // replayed a request that had already taken effect.
1936 if !status.is_success() && budget_exhausted {
1937 return Some(Self::Primary);
1938 }
1939 // A successful response saying it: GitHub reports a GraphQL rate limit in the
1940 // `errors` of an HTTP 200, where nothing about the status says so at all.
1941 if status.is_success()
1942 && PRIMARY_WORDINGS
1943 .iter()
1944 .any(|wording| normalized.contains(wording))
1945 {
1946 return Some(Self::Primary);
1947 }
1948 None
1949 }
1950
1951 /// What this limiter is called where an operator can look it up.
1952 const fn name(self) -> &'static str {
1953 match self {
1954 Self::Primary => "GitHub's primary API rate limit",
1955 Self::Secondary => "GitHub's secondary rate limit",
1956 }
1957 }
1958
1959 /// What the endpoint an operator would go and check says about this limiter.
1960 const fn where_to_look(self) -> &'static str {
1961 match self {
1962 Self::Primary => {
1963 "That is the budget `gh api rate_limit` reports, so that endpoint says when it \
1964 comes back."
1965 }
1966 Self::Secondary => {
1967 "That limiter is not the primary API budget: `gh api rate_limit` reports the \
1968 primary budget and does not report this one, so budget showing there says \
1969 nothing about this refusal, and every further attempt extends it."
1970 }
1971 }
1972 }
1973
1974 /// The next step this limiter actually calls for.
1975 const fn what_to_do(self) -> &'static str {
1976 match self {
1977 Self::Primary => {
1978 "wait for the reset `gh api rate_limit` reports, then run the command again."
1979 }
1980 Self::Secondary => {
1981 "leave this board alone for a few minutes, then run the command again — or \
1982 raise pacing.min_mutation_interval_ms on this source so it writes more slowly."
1983 }
1984 }
1985 }
1986}
1987
1988/// One rate-limit refusal, and the wait GitHub asked for if it asked for one.
1989#[derive(Debug, Clone, Copy)]
1990struct Limited {
1991 limiter: Limiter,
1992 hint: Option<u64>,
1993}
1994
1995impl Limited {
1996 /// What the caller is told once this source has waited as long as it may.
1997 ///
1998 /// Both limiters report as [`SourceError::RateLimited`], because that is what
1999 /// happened: the kind a caller matches on says a rate limit refused this, and nothing
2000 /// about *which* limiter it was makes it a different kind of failure. What differs is
2001 /// the operator's next step, and that is what the message carries — a secondary
2002 /// refusal read as a primary one sends an operator to `gh api rate_limit`, where the
2003 /// budget looks fine, and then back to retry the very burst that was refused.
2004 fn exhausted(
2005 self,
2006 doing: &str,
2007 waits: u32,
2008 waited: Duration,
2009 needed: Duration,
2010 budget: Duration,
2011 ) -> SourceError {
2012 SourceError::RateLimited {
2013 retry_after_seconds: self.hint,
2014 message: Some(format!(
2015 "{} refused this source while {doing}; it waited {} out over {} and was refused \
2016 again, and the next wait of {} would take it past the {} one call may spend \
2017 waiting. {} next: {}",
2018 self.limiter.name(),
2019 plural(waits, "refusal"),
2020 seconds(waited),
2021 seconds(needed),
2022 seconds(budget),
2023 self.limiter.where_to_look(),
2024 self.limiter.what_to_do(),
2025 )),
2026 }
2027 }
2028}
2029
2030/// One HTTP attempt's result, with what its response said about the rate limit.
2031///
2032/// The two travel together so the record and the outcome are written from the same place:
2033/// what a response said about the budget is only readable while that response is in hand,
2034/// and what the attempt *meant* is only decidable once its body has been read.
2035struct Attempted {
2036 result: Result<Value, Attempt>,
2037 limits: accounting::RateLimit,
2038 /// GitHub's own reported cost for this call, for a document that asked for it.
2039 reported_cost: Option<u64>,
2040 response_outcome: accounting::ResponseOutcome,
2041}
2042
2043/// One attempt's outcome: an error to report, or a rate limit to wait out.
2044enum Attempt {
2045 Failed(SourceError),
2046 Limited(Limited),
2047}
2048
2049fn plural(count: u32, thing: &str) -> String {
2050 if count == 1 {
2051 format!("{count} {thing}")
2052 } else {
2053 format!("{count} {thing}s")
2054 }
2055}
2056
2057fn seconds(duration: Duration) -> String {
2058 format!("{:.1}s", duration.as_secs_f64())
2059}
2060
2061/// The member [`stamp_served`] writes onto every node of a response that says when it last
2062/// changed: the `Date` that response was served at. No GitHub type has a field of this name.
2063const SERVED_AT: &str = "onetaskgraphServedAt";
2064
2065/// Write `served` onto every object of `data` carrying an `updatedAt`, so whatever reads a
2066/// node's time later knows the server's clock on the response that read it — which is what
2067/// decides whether that node's change token is given (see [`change_token::SETTLED`]).
2068fn stamp_served(data: &mut Value, served: &str) {
2069 match data {
2070 Value::Object(members) => {
2071 if members.contains_key("updatedAt") {
2072 members.insert(SERVED_AT.to_owned(), Value::String(served.to_owned()));
2073 }
2074 for member in members.values_mut() {
2075 stamp_served(member, served);
2076 }
2077 }
2078 Value::Array(items) => {
2079 for item in items {
2080 stamp_served(item, served);
2081 }
2082 }
2083 _ => {}
2084 }
2085}
2086
2087/// A header GitHub spells as a whole number of seconds, or `None` when this one is not.
2088///
2089/// A value that is present and unreadable is deliberately *not* an error. `retry-after` is
2090/// allowed by HTTP to be a date rather than a count, an intermediary can rewrite either
2091/// header, and neither is what makes a response a refusal — so the whole cost of one this
2092/// cannot read is that the refusal carries no hint and the backing-off schedule answers it
2093/// instead. Refusing the response over the header would turn a readable refusal into an
2094/// unreadable one, and refusing to *wait* would be the one wrong direction to fail in.
2095fn whole_seconds(value: Option<&reqwest::header::HeaderValue>) -> Option<u64> {
2096 value
2097 .and_then(|value| value.to_str().ok())
2098 .and_then(|value| value.trim().parse::<u64>().ok())
2099}
2100
2101/// Every mutation this source sends creates content — an issue, a board item, a field of
2102/// one, a sub-issue link, a dependency, a comment — or edits or removes content of that
2103/// kind, and no query in [`graphql::DOCUMENTS`] does, so what the secondary limiter counts
2104/// and what the keyword says are the same set. That is what makes the keyword a sound test
2105/// rather than a convenient one: pacing an edit or a removal the limiter might not have
2106/// counted costs a wait, and not pacing one it did count costs the next fifty minutes.
2107fn is_mutation(query: &str) -> bool {
2108 query.trim_start().starts_with("mutation")
2109}
2110
2111/// What this source was doing, for a diagnostic that has to say so.
2112///
2113/// Read out of [`graphql::DOCUMENTS`], which is the inventory rather than a copy of it, so
2114/// a document added without a description is caught by that list's own gate instead of
2115/// falling through to the vague arm below.
2116fn operation_description(query: &str) -> &'static str {
2117 graphql::DOCUMENTS
2118 .iter()
2119 .find(|(document, _)| *document == query)
2120 .map_or("talking to GitHub", |(_, doing)| *doing)
2121}
2122
2123/// GitHub's published ceiling on content-generating requests, per minute.
2124///
2125/// Pinned in `tests/fixtures/rate-limits.json` and gated against it, because it is
2126/// GitHub's number rather than this source's: [`MIN_MUTATION_INTERVAL_MS`] is *derived*
2127/// from it, so a pacing value checked only against itself cannot go stale here.
2128pub const CONTENT_CREATION_PER_MINUTE: u64 = 80;
2129/// The same ceiling as GitHub publishes it per hour, which this source does **not** pace
2130/// at. See [`MIN_MUTATION_INTERVAL_MS`] for why the per-minute bound is the one that
2131/// governs; it is pinned beside its sibling so the gate would notice either one moving.
2132pub const CONTENT_CREATION_PER_HOUR: u64 = 500;
2133/// Shortest interval between two content-creating mutations, in milliseconds.
2134///
2135/// GitHub documents two secondary limits on content-generating requests:
2136/// [`CONTENT_CREATION_PER_MINUTE`] and [`CONTENT_CREATION_PER_HOUR`]. 60000/80 is 750, so
2137/// a mutation every 750 ms is the fastest rate that cannot exceed the per-minute bound,
2138/// and that is the bound a copy actually trips: a copy of one plan-sized project is a
2139/// burst of a few dozen mutations inside a few seconds. The hourly bound works out at one
2140/// every 7.2 seconds sustained, which no single copy reaches and which, used as the
2141/// spacing here, would turn an ordinary copy into an hour of waiting — so it is
2142/// deliberately *not* what this paces at. An installation that wants the hourly bound
2143/// honoured for a long sequence of copies says so through
2144/// `pacing.min_mutation_interval_ms`.
2145pub const MIN_MUTATION_INTERVAL_MS: u64 = 60_000 / CONTENT_CREATION_PER_MINUTE;
2146/// First wait when a rate-limit refusal carries no hint; each further wait doubles it.
2147///
2148/// A doubling schedule from one second reaches a minute in six waits, which is GitHub's
2149/// own advice for a secondary limit — wait, and wait longer each time — without spending
2150/// the first minute of a transient refusal doing nothing.
2151pub const RETRY_BACKOFF_MS: u64 = 1_000;
2152/// Total time one call may spend waiting out rate limits before it reports a failure.
2153///
2154/// Two minutes is long enough to ride out the refusals a paced copy still collects and
2155/// short enough that a command an operator is watching returns. The bound is what makes
2156/// the wait a wait rather than a hang: a call refused past it ends in a diagnostic naming
2157/// the limiter, not in a process nobody can tell from a wedged one.
2158pub const RETRY_BUDGET_MS: u64 = 120_000;
2159
2160fn default_token_env() -> String {
2161 "GH_PROJECTS_TOKEN".to_owned()
2162}
2163fn default_endpoint() -> String {
2164 "https://api.github.com/graphql".to_owned()
2165}
2166
2167/// The name of a `Status` single-select option on the board.
2168///
2169/// Validated on the way in rather than checked later, so a blank option name — which
2170/// nothing on a board can be — is a state this type cannot hold.
2171#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2172#[serde(try_from = "String")]
2173#[schemars(extend("minLength" = 1))]
2174pub struct ColumnName(String);
2175
2176impl ColumnName {
2177 /// The option name, as the board spells it.
2178 fn as_str(&self) -> &str {
2179 &self.0
2180 }
2181}
2182
2183impl TryFrom<String> for ColumnName {
2184 type Error = String;
2185
2186 fn try_from(name: String) -> Result<Self, Self::Error> {
2187 if name.trim().is_empty() {
2188 return Err("a status_mapping option name cannot be blank".to_owned());
2189 }
2190 Ok(Self(name))
2191 }
2192}
2193
2194/// The two closed states this product can mean.
2195///
2196/// GitHub's `IssueClosedStateReason` also spells `DUPLICATE`, which nothing here ever writes:
2197/// an issue closed as one *reads* `cancelled`, because its work is another issue's, but this
2198/// product never decides on a person's behalf that two issues are the same work.
2199#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, schemars::JsonSchema)]
2200#[serde(rename_all = "kebab-case")]
2201pub enum ClosedState {
2202 /// `COMPLETED` — precisely done.
2203 Completed,
2204 /// `NOT_PLANNED` — precisely cancelled.
2205 NotPlanned,
2206}
2207
2208impl ClosedState {
2209 const fn reason(self) -> &'static str {
2210 match self {
2211 Self::Completed => "COMPLETED",
2212 Self::NotPlanned => "NOT_PLANNED",
2213 }
2214 }
2215}
2216
2217/// Configuration for one GitHub Projects v2 board.
2218///
2219/// It serializes to a document this same type reads back as itself, so a caller writing a
2220/// configuration — a consumer filing onto a board it configures — can build one here rather
2221/// than spell the shape again; an empty `metadata_fields` is left out, as one never set.
2222#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2223#[serde(default, deny_unknown_fields)]
2224pub struct GitHubProjectsConfig {
2225 /// Login of the user or organization which owns the board.
2226 pub owner: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates GitHub's owner grammar before private construction.
2227 /// The project number shown in the board's GitHub URL.
2228 pub project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` bounds this to a positive GraphQL Int.
2229 // 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.
2230 /// `owner/name` of the repository this source creates an issue in when the item's own
2231 /// `repositories` field does not decide it.
2232 ///
2233 /// An item naming exactly one repository is created there; a task or a document naming
2234 /// none or several is created in its parent project's repository; and a project, or a
2235 /// task or document with no parent, naming none or several is created here. A board
2236 /// has no repository of its own and `createIssue` requires one, so a write without
2237 /// this is refused naming the field. Reads never need it.
2238 pub repository: Option<String>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the `owner/name` grammar before private construction.
2239 // llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
2240 /// Environment variable containing a fine-grained token with Projects and Issues
2241 /// read/write plus Pull requests read-only access for every repository represented on
2242 /// the board.
2243 #[serde(default = "default_token_env")]
2244 pub token_env: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the environment-variable grammar.
2245 /// GraphQL endpoint. GitHub Enterprise installations may override it.
2246 #[serde(default = "default_endpoint")]
2247 pub endpoint: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` converts it to the private validated `Url`.
2248 /// Per-instance mapping from a status category to the option of the board's one
2249 /// `Status` field it lands on, for a task and for a project.
2250 ///
2251 /// The shared `StatusMapping` grammar: each value is one option name for both kinds,
2252 /// `null` to disable the category for both, or `{task, project}` naming it per kind,
2253 /// where a kind left out leaves the category unmapped for that kind. A category this
2254 /// does not mention keeps its shipped default for both kinds: `backlog` to "Backlog",
2255 /// `todo` to "Todo", `queued` to "Queued", `in-progress` to "In Progress", `done` to
2256 /// "Done" plus closed as completed, `cancelled` to "Cancelled" plus closed as not
2257 /// planned, and `draft` and `unknown` unmapped. A category it does mention gets no
2258 /// shipped default for a kind it leaves out. `done` and `cancelled` close the issue for
2259 /// either kind. No two categories may name one option for the same kind, ignoring case.
2260 /// `unknown` may name one existing option; every unknown word then lands on it and
2261 /// reads back as `unknown` under its name. Unlike `local-md`, this source cannot keep
2262 /// each unknown word because it never creates board options.
2263 #[serde(default)]
2264 pub status_mapping: StatusMapping,
2265 /// Per-instance mapping from a task's priority to an option of this board's
2266 /// single-select field named `Priority`.
2267 ///
2268 /// Absent, this source holds no priority: every task reads as `none`, and a write of any
2269 /// other priority is refused before it reaches this board. Present, each of `urgent`,
2270 /// `high`, `medium` and `low` it does not mention keeps its shipped default — `Urgent`,
2271 /// `High`, `Medium` and `Low` — and an item with no value in the `Priority` field reads
2272 /// as `none`, so writing `none` clears the value. Option names match case-insensitively;
2273 /// no two levels may name one option. Reads and writes never create the field or an
2274 /// option: `onetaskgraph sources fields <source> --apply` does, and a write naming one
2275 /// the board lacks is refused pointing there.
2276 #[serde(default)]
2277 pub priority_mapping: Option<PriorityMappingConfig>,
2278 /// Caller metadata values this source also writes to board text fields, so the board can
2279 /// filter on them.
2280 ///
2281 /// Absent or empty, nothing is projected. Each entry names a board text `field`, a
2282 /// top-level metadata `key` and an optional `path` of object keys walked inside that key's
2283 /// value. On every item write — create, update and copy, of a task, a project or a
2284 /// document — a string found there is written to the field, and nothing is sent when the
2285 /// field already holds it; nothing there, or `null`, clears the field when it holds a
2286 /// value; any other JSON type is refused before any mutation. The metadata comment in the
2287 /// issue body stays the one home of the value, and every read reports metadata from it.
2288 /// Reads and writes never create the field: `onetaskgraph sources fields <source>
2289 /// --apply` does, as a text field, and a write to a board lacking it, or holding a
2290 /// non-text field of that name, is refused pointing there.
2291 #[serde(default, skip_serializing_if = "Vec::is_empty")]
2292 // Kept in the schema as `"default": []` although a serialized configuration leaves an empty
2293 // list out, so both SDKs model an absent list as an empty one rather than as `null`.
2294 #[schemars(!skip_serializing_if)]
2295 pub metadata_fields: Vec<MetadataFieldConfig>,
2296 /// How fast this source writes, and how long it waits out a rate-limit refusal.
2297 ///
2298 /// Every field keeps its shipped default when it is absent, and the defaults are
2299 /// GitHub's own published limits rather than taste. See [`Pacing`].
2300 #[serde(default)]
2301 pub pacing: PacingConfig,
2302}
2303
2304/// Which option of the board's `Priority` field each priority lands on.
2305///
2306/// One member per level rather than a map, so a key that is not a level is refused where
2307/// the configuration is read, naming the levels there are. `none` is not a member: it is no
2308/// value in the field, not an option of it.
2309#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2310#[serde(default, deny_unknown_fields)]
2311pub struct PriorityMappingConfig {
2312 /// The option `urgent` lands on; `Urgent` when absent.
2313 pub urgent: Option<PriorityOptionName>,
2314 /// The option `high` lands on; `High` when absent.
2315 pub high: Option<PriorityOptionName>,
2316 /// The option `medium` lands on; `Medium` when absent.
2317 pub medium: Option<PriorityOptionName>,
2318 /// The option `low` lands on; `Low` when absent.
2319 pub low: Option<PriorityOptionName>,
2320}
2321
2322/// The name of an option of the board's `Priority` single-select field.
2323///
2324/// Validated on the way in, for the reason [`ColumnName`] is: nothing on a board can have a
2325/// blank name.
2326#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2327#[serde(try_from = "String")]
2328#[schemars(extend("minLength" = 1))]
2329pub struct PriorityOptionName(String);
2330
2331impl PriorityOptionName {
2332 /// The option name, as the board spells it.
2333 fn as_str(&self) -> &str {
2334 &self.0
2335 }
2336}
2337
2338impl TryFrom<String> for PriorityOptionName {
2339 type Error = String;
2340
2341 fn try_from(name: String) -> Result<Self, Self::Error> {
2342 if name.trim().is_empty() {
2343 return Err("a priority_mapping option name cannot be blank".to_owned());
2344 }
2345 Ok(Self(name))
2346 }
2347}
2348
2349/// The name of the board field a priority is held in.
2350pub const PRIORITY_FIELD: &str = "Priority";
2351
2352/// The four priorities a board option can hold, in the order a new `Priority` field lists
2353/// them. `none` is not among them: it is the field holding no value.
2354///
2355/// This list mirrors `Priority`, so it carries its own drift gate, in the shape [`CATEGORIES`]
2356/// does: [`level_position`] is a wildcard-free match, so a priority added to the shared
2357/// vocabulary fails to compile until it is placed there, and this crate's suite reconciles
2358/// this list and [`PriorityMappingConfig`]'s members against that enum's own derived schema.
2359pub const PRIORITY_LEVELS: [Priority; 4] = [
2360 Priority::Urgent,
2361 Priority::High,
2362 Priority::Medium,
2363 Priority::Low,
2364];
2365
2366/// Where one priority sits in [`PRIORITY_LEVELS`], or `None` for `none`, which is no option;
2367/// see that list for what this pins.
2368#[must_use]
2369pub const fn level_position(priority: Priority) -> Option<usize> {
2370 match priority {
2371 Priority::None => None,
2372 Priority::Urgent => Some(0),
2373 Priority::High => Some(1),
2374 Priority::Medium => Some(2),
2375 Priority::Low => Some(3),
2376 }
2377}
2378
2379/// This instance's complete priority-to-option mapping, read in both directions.
2380///
2381/// One option per level, held in [`PRIORITY_LEVELS`] order, once it is established that no
2382/// two levels name one option.
2383#[derive(Debug, Clone)]
2384struct PriorityMapping {
2385 options: [PriorityOptionName; 4],
2386}
2387
2388impl PriorityMapping {
2389 fn resolve(config: PriorityMappingConfig, instance: &SourceName) -> Result<Self, SourceError> {
2390 let shipped = |name: &str| PriorityOptionName(name.to_owned());
2391 let mapping = Self {
2392 options: [
2393 config.urgent.unwrap_or_else(|| shipped("Urgent")),
2394 config.high.unwrap_or_else(|| shipped("High")),
2395 config.medium.unwrap_or_else(|| shipped("Medium")),
2396 config.low.unwrap_or_else(|| shipped("Low")),
2397 ],
2398 };
2399 for (index, option) in mapping.options.iter().enumerate() {
2400 if let Some(earlier) = mapping.options[..index]
2401 .iter()
2402 .position(|other| other.as_str().eq_ignore_ascii_case(option.as_str()))
2403 {
2404 return Err(SourceError::Config {
2405 message: format!(
2406 "priority_mapping of source {instance} sends both {} and {} to the board \
2407 option {:?}; one option cannot read back as two priorities",
2408 PRIORITY_LEVELS[earlier],
2409 PRIORITY_LEVELS[index],
2410 option.as_str()
2411 ),
2412 });
2413 }
2414 }
2415 Ok(mapping)
2416 }
2417
2418 /// The option `priority` lands on, or `None` for `none`, which is no option at all.
2419 fn option(&self, priority: Priority) -> Option<&str> {
2420 level_position(priority).map(|index| self.options[index].as_str())
2421 }
2422
2423 /// The priority a board option name reports, or `None` when nothing maps to it.
2424 fn priority_of(&self, option: &str) -> Option<Priority> {
2425 self.options
2426 .iter()
2427 .position(|name| name.as_str().eq_ignore_ascii_case(option))
2428 .map(|index| PRIORITY_LEVELS[index])
2429 }
2430
2431 /// Every mapped option name, in the order a new `Priority` field lists them.
2432 fn names(&self) -> impl Iterator<Item = &str> {
2433 self.options.iter().map(PriorityOptionName::as_str)
2434 }
2435}
2436
2437/// One metadata value this source projects onto a board text field.
2438#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2439#[serde(deny_unknown_fields)]
2440pub struct MetadataFieldConfig {
2441 /// The name of the board text field the value is written to.
2442 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.
2443 /// The top-level metadata key the value is read from. A key under the reserved
2444 /// `onetaskgraph.` prefix is refused.
2445 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.
2446 /// Object keys walked inside that key's value, outermost first. Absent or empty, the
2447 /// key's own value is the one projected. Left out of a serialized entry when empty.
2448 #[serde(default, skip_serializing_if = "Vec::is_empty")]
2449 // Kept in the schema as `"default": []`, for the reason `metadata_fields`' is.
2450 #[schemars(!skip_serializing_if)]
2451 pub path: Vec<String>,
2452}
2453
2454/// The board fields no metadata value may be projected onto, compared ignoring case: the
2455/// three this source writes itself, and those GitHub owns on every board.
2456// 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.
2457const UNPROJECTABLE_FIELDS: [&str; 13] = [
2458 STATUS_FIELD,
2459 PRIORITY_FIELD,
2460 ORIGIN_FIELD,
2461 "Title",
2462 "Assignees",
2463 "Labels",
2464 "Linked pull requests",
2465 "Milestone",
2466 "Repository",
2467 "Reviewers",
2468 "Parent issue",
2469 "Sub-issues progress",
2470 "Type",
2471];
2472
2473/// One validated [`MetadataFieldConfig`] entry.
2474#[derive(Debug, Clone)]
2475struct MetadataField {
2476 field: ProjectedField,
2477 key: ProjectedKey,
2478 path: Vec<String>,
2479}
2480
2481/// The name of a board text field a metadata value is projected onto: never blank, never one
2482/// this source or GitHub writes, and never one another entry of the same source names.
2483#[derive(Debug, Clone, PartialEq, Eq)]
2484struct ProjectedField(String);
2485
2486/// The top-level metadata key a projected value is read from: never blank, and never under
2487/// the namespace this product reserves.
2488#[derive(Debug, Clone, PartialEq, Eq)]
2489struct ProjectedKey(String);
2490
2491impl std::ops::Deref for ProjectedField {
2492 type Target = str;
2493 fn deref(&self) -> &str {
2494 &self.0
2495 }
2496}
2497
2498impl std::ops::Deref for ProjectedKey {
2499 type Target = str;
2500 fn deref(&self) -> &str {
2501 &self.0
2502 }
2503}
2504
2505/// What one item's metadata says one projected field should hold.
2506#[derive(Debug, Clone, PartialEq, Eq)]
2507enum Projected {
2508 /// A string to hold.
2509 Text(String),
2510 /// Nothing: the key or a step of the path is absent, or the value is `null`.
2511 Nothing,
2512}
2513
2514impl MetadataField {
2515 /// Validate every entry of one instance's `metadata_fields`, refusing the first that
2516 /// cannot stand, naming the source and the entry.
2517 fn resolve(
2518 configured: Vec<MetadataFieldConfig>,
2519 instance: &SourceName,
2520 ) -> Result<Vec<Self>, SourceError> {
2521 let mut resolved: Vec<Self> = Vec::with_capacity(configured.len());
2522 for (index, entry) in configured.into_iter().enumerate() {
2523 let refuse = |why: String| SourceError::Config {
2524 message: format!(
2525 "metadata_fields[{index}] of source {instance} (field {:?}, key {:?}) {why}",
2526 entry.field, entry.key
2527 ),
2528 };
2529 if entry.field.trim().is_empty() {
2530 return Err(refuse("names a blank field".to_owned()));
2531 }
2532 if entry.key.trim().is_empty() {
2533 return Err(refuse("names a blank key".to_owned()));
2534 }
2535 if entry.key.split('.').next() == Some(MetadataKey::RESERVED_NAMESPACE) {
2536 return Err(refuse(format!(
2537 "names a key under the reserved \"{}.\" prefix, which this product keeps \
2538 for its own keys; next: project a caller key instead",
2539 MetadataKey::RESERVED_NAMESPACE
2540 )));
2541 }
2542 if let Some(owned) = UNPROJECTABLE_FIELDS
2543 .iter()
2544 .find(|owned| owned.eq_ignore_ascii_case(&entry.field))
2545 {
2546 return Err(refuse(format!(
2547 "names the board field {owned:?}, which this source or GitHub already \
2548 writes; next: name a text field of your own"
2549 )));
2550 }
2551 if let Some(earlier) = resolved
2552 .iter()
2553 .position(|other| other.field.eq_ignore_ascii_case(&entry.field))
2554 {
2555 return Err(refuse(format!(
2556 "names the same board field as metadata_fields[{earlier}]; one field \
2557 cannot hold two values"
2558 )));
2559 }
2560 resolved.push(Self {
2561 field: ProjectedField(entry.field),
2562 key: ProjectedKey(entry.key),
2563 path: entry.path,
2564 });
2565 }
2566 Ok(resolved)
2567 }
2568
2569 /// The path as a refusal spells it: `[]` for the key's own value.
2570 fn spelled_path(&self) -> String {
2571 serde_json::to_string(&self.path).unwrap_or_else(|_| format!("{:?}", self.path))
2572 }
2573
2574 /// What `metadata` says this field should hold, or the refusal for a value no text field
2575 /// can hold.
2576 fn projected(
2577 &self,
2578 metadata: &BTreeMap<String, Value>,
2579 instance: &SourceName,
2580 ) -> Result<Projected, SourceError> {
2581 let mut value = metadata.get(&*self.key);
2582 for step in &self.path {
2583 value = value.and_then(|held| held.get(step.as_str()));
2584 }
2585 let found = match value {
2586 None | Some(Value::Null) => return Ok(Projected::Nothing),
2587 Some(Value::String(text)) => return Ok(Projected::Text(text.clone())),
2588 Some(Value::Bool(_)) => "a boolean",
2589 Some(Value::Number(_)) => "a number",
2590 Some(Value::Array(_)) => "an array",
2591 Some(Value::Object(_)) => "an object",
2592 };
2593 Err(SourceError::Refused {
2594 message: format!(
2595 "source {instance} projects metadata key {:?} at path {} onto its board text \
2596 field {:?}, and this item holds {found} there; only a string, null or nothing \
2597 can be written to a text field; next: store a string there, or remove the \
2598 value",
2599 &*self.key,
2600 self.spelled_path(),
2601 &*self.field
2602 ),
2603 })
2604 }
2605}
2606
2607/// What one item's `Priority` field says, read through this instance's mapping.
2608#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
2609enum HeldPriority {
2610 /// A priority this source reports: an option the mapping names, or no value (`none`).
2611 Read(Priority),
2612 /// An option the mapping does not name, which is never read as a level or as `none`.
2613 Unmapped(String),
2614}
2615
2616/// How fast this source writes, and how long it waits out a rate-limit refusal.
2617///
2618/// Configurable because a GitHub Enterprise installation sets its own limits and an
2619/// operator who has already been refused may want to go slower still — not because the
2620/// defaults are guesses.
2621#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2622#[serde(default, deny_unknown_fields)]
2623pub struct PacingConfig {
2624 /// Shortest interval between two content-creating mutations, in milliseconds.
2625 ///
2626 /// Zero sends them as fast as they are asked for, which is what a fixture server on
2627 /// loopback wants and what no board on github.com does. At most [`MAX_PACING_MS`].
2628 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.
2629 /// First wait when a rate-limit refusal carries no hint, in milliseconds. Each
2630 /// further wait of the same call doubles it. At most [`MAX_PACING_MS`], and never
2631 /// zero while there is a budget to spend, because a schedule of zero-length waits
2632 /// consumes none of it and so never ends.
2633 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.
2634 /// Total time one call may spend waiting out rate limits, in milliseconds.
2635 ///
2636 /// Zero reports the refusal rather than waiting at all. At most [`MAX_PACING_MS`]:
2637 /// the bound is what makes this a wait rather than a hang.
2638 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.
2639}
2640
2641/// The largest any pacing setting may be, in milliseconds.
2642///
2643/// One hour. GitHub's own harshest published bound on content-generating requests works
2644/// out at one every 7.2 seconds, so an hour is already three orders of magnitude past
2645/// anything a real limit asks for, and past it the settings stop describing pacing at all:
2646/// a wait budget beyond it is the unbounded wait this whole mechanism exists to replace,
2647/// and an interval beyond it is a command that never sends its second mutation. It also
2648/// keeps the clock arithmetic in [`GitHubProjectsSource::reserve_mutation_slot`] inside
2649/// what a `Duration` can hold on every platform.
2650pub const MAX_PACING_MS: u64 = 3_600_000;
2651
2652/// [`PacingConfig`] with every default resolved and every value checked, which is what the
2653/// source holds.
2654#[derive(Debug, Clone, Copy)]
2655struct Pacing {
2656 min_mutation_interval: Duration,
2657 retry_backoff: Duration,
2658 retry_budget: Duration,
2659}
2660
2661impl Pacing {
2662 /// Resolve one instance's pacing, refusing a configuration that would not pace at all.
2663 fn resolve(config: PacingConfig, instance: &SourceName) -> Result<Self, SourceError> {
2664 let bounded = |value: Option<u64>, default: u64, field: &str| match value {
2665 Some(value) if value > MAX_PACING_MS => Err(SourceError::Config {
2666 message: format!(
2667 "pacing.{field} of source {instance} is {value} ms, and the most any pacing \
2668 setting may be is {MAX_PACING_MS} ms — an hour, which is already far past \
2669 GitHub's own harshest published limit"
2670 ),
2671 }),
2672 Some(value) => Ok(Duration::from_millis(value)),
2673 None => Ok(Duration::from_millis(default)),
2674 };
2675 let retry_backoff = bounded(
2676 config.retry_backoff_ms,
2677 RETRY_BACKOFF_MS,
2678 "retry_backoff_ms",
2679 )?;
2680 let retry_budget = bounded(config.retry_budget_ms, RETRY_BUDGET_MS, "retry_budget_ms")?;
2681 if retry_backoff.is_zero() && !retry_budget.is_zero() {
2682 return Err(SourceError::Config {
2683 message: format!(
2684 "pacing.retry_backoff_ms of source {instance} is 0 while \
2685 pacing.retry_budget_ms is {} ms; a schedule of zero-length waits spends \
2686 none of that budget, so it would retry a refusal forever. Set a backoff of \
2687 at least 1 ms, or set retry_budget_ms to 0 to report a refusal without \
2688 waiting at all",
2689 retry_budget.as_millis()
2690 ),
2691 });
2692 }
2693 Ok(Self {
2694 min_mutation_interval: bounded(
2695 config.min_mutation_interval_ms,
2696 MIN_MUTATION_INTERVAL_MS,
2697 "min_mutation_interval_ms",
2698 )?,
2699 retry_backoff,
2700 retry_budget,
2701 })
2702 }
2703}
2704
2705/// Factory for [`GitHubProjectsSource`].
2706#[derive(Debug, Clone, Copy, Default)]
2707pub struct Plugin;
2708
2709impl SourcePlugin for Plugin {
2710 fn kind(&self) -> &'static str {
2711 KIND
2712 }
2713 /// The endpoint, the owner and the board's number: what names one board.
2714 fn remote_identity(&self, config: &Value) -> Option<String> {
2715 let config: GitHubProjectsConfig = serde_json::from_value(config.clone()).ok()?;
2716 Some(format!(
2717 "{}\n{}\n{}",
2718 config.endpoint, config.owner, config.project_number
2719 ))
2720 }
2721 fn config_schema(&self) -> Schema {
2722 schema_for!(GitHubProjectsConfig)
2723 }
2724 fn build(
2725 &self,
2726 name: &SourceName,
2727 config: &Value,
2728 secrets: &dyn SecretResolver,
2729 ) -> Result<Box<dyn TaskSource>, SourceError> {
2730 self.build_recording_into(name, config, secrets, Arc::new(Accounting::new()))
2731 }
2732
2733 fn build_with_clock(
2734 &self,
2735 name: &SourceName,
2736 config: &Value,
2737 secrets: &dyn SecretResolver,
2738 clock: SharedClock,
2739 ) -> Result<Box<dyn TaskSource>, SourceError> {
2740 self.build_recording_with_clock(name, config, secrets, Arc::new(Accounting::new()), clock)
2741 }
2742}
2743
2744impl Plugin {
2745 /// Build a source recording every request it sends into an accounting the caller holds.
2746 ///
2747 /// [`SourcePlugin::build`] is this with an accounting of its own, which is what the
2748 /// registry gets. This is for a caller that is also calling GitHub itself and wants one
2749 /// session total rather than two — see [`accounting`] and
2750 /// [`GitHubProjectsSource::recording_into`].
2751 ///
2752 /// # Errors
2753 ///
2754 /// Exactly [`SourcePlugin::build`]'s, with the same source name in front of each:
2755 /// [`SourceError::Config`] for configuration this plugin cannot use and
2756 /// [`SourceError::Auth`] for a credential it cannot find.
2757 pub fn build_recording_into(
2758 &self,
2759 name: &SourceName,
2760 config: &Value,
2761 secrets: &dyn SecretResolver,
2762 ledger: Arc<Accounting>,
2763 ) -> Result<Box<dyn TaskSource>, SourceError> {
2764 self.build_recording_with_clock(name, config, secrets, ledger, system_clock())
2765 }
2766
2767 fn build_recording_with_clock(
2768 &self,
2769 name: &SourceName,
2770 config: &Value,
2771 secrets: &dyn SecretResolver,
2772 ledger: Arc<Accounting>,
2773 clock: SharedClock,
2774 ) -> Result<Box<dyn TaskSource>, SourceError> {
2775 let config: GitHubProjectsConfig =
2776 serde_json::from_value(config.clone()).map_err(|e| SourceError::Config {
2777 message: format!("source {name}: {e}"),
2778 })?;
2779 let prefix = format!("source {name}: ");
2780 let mut source = GitHubProjectsSource::recording_into(name, config, secrets, ledger)
2781 .map_err(|error| match error {
2782 // The shared `StatusMapping::distinct` names the source itself.
2783 SourceError::Config { message } if message.starts_with(&prefix) => {
2784 SourceError::Config { message }
2785 }
2786 SourceError::Config { message } => SourceError::Config {
2787 message: format!("{prefix}{message}"),
2788 },
2789 SourceError::Auth { message } => SourceError::Auth {
2790 message: format!("source {name}: {message}"),
2791 },
2792 other => other,
2793 })?;
2794 source.clock = clock;
2795 Ok(Box::new(source))
2796 }
2797}
2798
2799/// Where a status category lands on this board, once configuration is resolved.
2800#[derive(Debug, Clone, PartialEq, Eq)]
2801enum StatusTarget {
2802 /// Not usable against this instance for this kind, and why.
2803 Disabled(UnmappedStatus),
2804 /// The board's `Status` option of this name.
2805 Column(ColumnName),
2806 /// A closed issue, with both its board option and the reason that says which closed it means.
2807 // 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.
2808 Terminal(ColumnName, ClosedState),
2809}
2810
2811/// Every status category, in the order the vocabulary declares them.
2812///
2813/// This list mirrors `StatusCategory`, so it carries its own drift gate rather than a
2814/// reviewer's attention: [`category_position`] is a wildcard-free match, so a variant
2815/// added to the shared vocabulary fails to compile until it is named there, and this
2816/// crate's suite reconciles this list against that enum's own derived schema, which is
2817/// generated from the variants rather than written beside them. The schema is what
2818/// catches a list left one short — a list checking only the positions it already holds
2819/// would pass while every mapping indexed by the new position panicked.
2820pub const CATEGORIES: [StatusCategory; 8] = [
2821 StatusCategory::Draft,
2822 StatusCategory::Backlog,
2823 StatusCategory::Todo,
2824 StatusCategory::Queued,
2825 StatusCategory::InProgress,
2826 StatusCategory::Done,
2827 StatusCategory::Cancelled,
2828 StatusCategory::Unknown,
2829];
2830
2831/// Where one category sits in [`CATEGORIES`]; see that list for what this pins.
2832#[must_use]
2833pub const fn category_position(category: StatusCategory) -> usize {
2834 match category {
2835 StatusCategory::Draft => 0,
2836 StatusCategory::Backlog => 1,
2837 StatusCategory::Todo => 2,
2838 StatusCategory::Queued => 3,
2839 StatusCategory::InProgress => 4,
2840 StatusCategory::Done => 5,
2841 StatusCategory::Cancelled => 6,
2842 StatusCategory::Unknown => 7,
2843 }
2844}
2845
2846/// The spelling a status category is configured and reported under.
2847fn category_name(category: StatusCategory) -> &'static str {
2848 match category {
2849 StatusCategory::Draft => "draft",
2850 StatusCategory::Backlog => "backlog",
2851 StatusCategory::Todo => "todo",
2852 StatusCategory::Queued => "queued",
2853 StatusCategory::InProgress => "in-progress",
2854 StatusCategory::Done => "done",
2855 StatusCategory::Cancelled => "cancelled",
2856 StatusCategory::Unknown => "unknown",
2857 }
2858}
2859
2860/// A shipped default's option name.
2861///
2862/// The literals below are this file's own and non-blank, and they are validated by the
2863/// one constructor a configured name goes through rather than beside it.
2864fn shipped_column(name: &'static str) -> ColumnName {
2865 ColumnName::try_from(name.to_owned()).expect("a shipped default names a board option")
2866}
2867
2868/// The shipped default for one category this instance's `status_mapping` does not mention,
2869/// for either kind.
2870fn shipped_default(category: StatusCategory) -> StatusTarget {
2871 match category {
2872 StatusCategory::Backlog => StatusTarget::Column(shipped_column("Backlog")),
2873 StatusCategory::Todo => StatusTarget::Column(shipped_column("Todo")),
2874 StatusCategory::Queued => StatusTarget::Column(shipped_column("Queued")),
2875 StatusCategory::InProgress => StatusTarget::Column(shipped_column("In Progress")),
2876 StatusCategory::Done => {
2877 StatusTarget::Terminal(shipped_column("Done"), ClosedState::Completed)
2878 }
2879 StatusCategory::Cancelled => {
2880 StatusTarget::Terminal(shipped_column("Cancelled"), ClosedState::NotPlanned)
2881 }
2882 StatusCategory::Draft | StatusCategory::Unknown => {
2883 StatusTarget::Disabled(UnmappedStatus::Unconfigured)
2884 }
2885 }
2886}
2887
2888/// The two kinds a status is written and read for, each with its own half of the mapping.
2889const STATUS_KINDS: [ItemKind; 2] = [ItemKind::Task, ItemKind::Project];
2890
2891/// This instance's complete category-to-target mapping for each kind, read in both
2892/// directions.
2893///
2894/// One target per category per kind, held at that category's own [`category_position`], so
2895/// a category missing from the mapping, named twice in it, or filed out of order is a state
2896/// this type cannot hold rather than one [`Self::target`] has to defend against. Both kinds'
2897/// targets are options of the board's one `Status` field.
2898#[derive(Debug, Clone)]
2899struct BoardStatuses {
2900 tasks: [StatusTarget; CATEGORIES.len()],
2901 projects: [StatusTarget; CATEGORIES.len()],
2902}
2903
2904impl BoardStatuses {
2905 /// Resolve `configured` against the shipped defaults, refusing two categories one kind
2906 /// would read back from one option.
2907 ///
2908 /// A category the mapping does not mention keeps its shipped default for both kinds; one
2909 /// it does mention is exactly what it configures, so a per-kind object leaves the kind it
2910 /// omits unmapped rather than defaulted.
2911 fn resolve(configured: &StatusMapping, instance: &SourceName) -> Result<Self, SourceError> {
2912 let resolve_kind =
2913 |kind: ItemKind| -> Result<[StatusTarget; CATEGORIES.len()], SourceError> {
2914 // `CATEGORIES[position] == category` for every category — the crate's suite
2915 // asserts it — so mapping the list in order fills each category's own slot.
2916 let mut targets = CATEGORIES.map(shipped_default);
2917 for (slot, category) in targets.iter_mut().zip(CATEGORIES) {
2918 if !configured.mentions(category) {
2919 continue;
2920 }
2921 *slot = match configured.name_for(category, kind) {
2922 Err(why) => StatusTarget::Disabled(why),
2923 Ok(name) => {
2924 let option = ColumnName::try_from(name.as_str().to_owned())
2925 .map_err(|message| SourceError::Config { message })?;
2926 match category {
2927 StatusCategory::Done => {
2928 StatusTarget::Terminal(option, ClosedState::Completed)
2929 }
2930 StatusCategory::Cancelled => {
2931 StatusTarget::Terminal(option, ClosedState::NotPlanned)
2932 }
2933 _ => StatusTarget::Column(option),
2934 }
2935 }
2936 };
2937 }
2938 StatusMapping::distinct(
2939 instance,
2940 kind,
2941 CATEGORIES
2942 .iter()
2943 .zip(&targets)
2944 .filter_map(|(category, target)| target.option().map(|o| (*category, o))),
2945 )?;
2946 Ok(targets)
2947 };
2948 Ok(Self {
2949 tasks: resolve_kind(ItemKind::Task)?,
2950 projects: resolve_kind(ItemKind::Project)?,
2951 })
2952 }
2953
2954 /// Every category's target for `kind`, in category order.
2955 const fn targets(&self, kind: ItemKind) -> &[StatusTarget; CATEGORIES.len()] {
2956 match kind {
2957 ItemKind::Task => &self.tasks,
2958 ItemKind::Project => &self.projects,
2959 }
2960 }
2961
2962 fn target(&self, kind: ItemKind, category: StatusCategory) -> &StatusTarget {
2963 &self.targets(kind)[category_position(category)]
2964 }
2965
2966 /// The category a board option name reports for `kind`, or `None` when nothing of that
2967 /// kind maps to it.
2968 fn category_of(&self, kind: ItemKind, option: &str) -> Option<StatusCategory> {
2969 CATEGORIES.into_iter().find(|category| {
2970 self.target(kind, *category)
2971 .option()
2972 .is_some_and(|name| name.eq_ignore_ascii_case(option))
2973 })
2974 }
2975
2976 /// Every option name either kind maps a category to, each once ignoring case, in
2977 /// category order with a task's name before a project's — what the guarded setup asks
2978 /// the `Status` field to hold.
2979 fn wanted(&self) -> Vec<String> {
2980 let mut wanted: Vec<String> = Vec::new();
2981 for category in CATEGORIES {
2982 for kind in STATUS_KINDS {
2983 if let Some(name) = self.target(kind, category).option()
2984 && !wanted.iter().any(|held| held.eq_ignore_ascii_case(name))
2985 {
2986 wanted.push(name.to_owned());
2987 }
2988 }
2989 }
2990 wanted
2991 }
2992
2993 /// The status an item of `kind` reports, from the three things a read of it says: its
2994 /// board `Status` option, whether its issue is closed, and the reason it was closed with.
2995 ///
2996 /// **A closed issue's category follows its closure**, by [`closed_category`]: completed
2997 /// or no reason reads `done`, not planned and duplicate read `cancelled`, and any other
2998 /// reason reads `unknown`. `REOPENED`-while-closed is a state this source can never
2999 /// produce, so it is read permissively rather than refused — reads are faithful, and
3000 /// refusals belong on writes.
3001 ///
3002 /// **Its name follows the closure too, unless the option agrees with it.** The option's
3003 /// own name is kept when this kind's mapping reads that option as the same category, or
3004 /// does not name it at all — so a closed issue in a "Shipped" column reports `done`
3005 /// named `Shipped`. An option the mapping reads as another category contradicts the
3006 /// closure, and so does no option at all: the name is then the option the mapping gives
3007 /// the closure's category, so an issue closed as completed at `Todo` reads `done` named
3008 /// by the board's `done` option, and a destination reading `Todo` as todo is never handed
3009 /// it. An `unknown` closure keeps the option, or `Closed` without one.
3010 ///
3011 /// An open item's option reads through its own kind's mapping, and an option that mapping
3012 /// does not name reads as `Unknown` under its own name.
3013 ///
3014 /// One function of those three rather than of a response, so a narrow status write can
3015 /// answer what a re-read would report by applying it to the state it has just written.
3016 fn status(
3017 &self,
3018 kind: ItemKind,
3019 option: Option<&str>,
3020 closed: bool,
3021 reason: Option<&str>,
3022 ) -> Status {
3023 if closed {
3024 let category = closed_category(reason);
3025 let agrees = option.filter(|option| {
3026 category == StatusCategory::Unknown
3027 || self
3028 .category_of(kind, option)
3029 .is_none_or(|read| read == category)
3030 });
3031 let name = agrees.or_else(|| self.target(kind, category).option());
3032 let fallback = match category {
3033 StatusCategory::Done => "Done",
3034 StatusCategory::Cancelled => "Cancelled",
3035 _ => "Closed",
3036 };
3037 return Status {
3038 category,
3039 name: name.or(option).unwrap_or(fallback).to_owned(),
3040 };
3041 }
3042 let name = option.unwrap_or("Open").to_owned();
3043 Status {
3044 category: self
3045 .category_of(kind, &name)
3046 .unwrap_or(StatusCategory::Unknown),
3047 name,
3048 }
3049 }
3050
3051 /// The option a closed item's `Status` has to hold for its card to agree with its
3052 /// closure, and the category that closure reads as — or `None` when the item is open,
3053 /// its closure reads `unknown`, or its option already agrees.
3054 ///
3055 /// What `task status reconcile` writes: only `done` and `cancelled` closures are brought
3056 /// in line, each to the one option the mapping gives its category — an item with no
3057 /// option, or at one the mapping does not name, included.
3058 fn contradicted(
3059 &self,
3060 kind: ItemKind,
3061 option: Option<&str>,
3062 closed: bool,
3063 reason: Option<&str>,
3064 ) -> Option<(ClosedAs, &str)> {
3065 if !closed {
3066 return None;
3067 }
3068 let closed_as = match closed_category(reason) {
3069 StatusCategory::Done => ClosedAs::Done,
3070 StatusCategory::Cancelled => ClosedAs::Cancelled,
3071 _ => return None,
3072 };
3073 let wanted = self.target(kind, closed_as.category()).option()?;
3074 if option.is_some_and(|option| option.eq_ignore_ascii_case(wanted)) {
3075 return None;
3076 }
3077 Some((closed_as, wanted))
3078 }
3079}
3080
3081/// The category a closed issue reads as, from the reason it was closed with.
3082///
3083/// `COMPLETED`, and no reason at all, is finished work; `NOT_PLANNED` and `DUPLICATE` are both
3084/// work nobody will do here — a duplicate's work is the other issue's — and any other reason
3085/// is one this source cannot read a category from.
3086fn closed_category(reason: Option<&str>) -> StatusCategory {
3087 match reason {
3088 None | Some("COMPLETED") => StatusCategory::Done,
3089 Some("NOT_PLANNED" | "DUPLICATE") => StatusCategory::Cancelled,
3090 Some(_) => StatusCategory::Unknown,
3091 }
3092}
3093
3094impl BoardStatuses {
3095 /// For each kind, the option names it maps a category to that `existing` lacks, ignoring
3096 /// case; a kind lacking none is left out.
3097 fn missing_by_kind(&self, existing: &[StatusOption]) -> Vec<KindMissing> {
3098 STATUS_KINDS
3099 .into_iter()
3100 .filter_map(|kind| {
3101 let missing: Vec<String> = self
3102 .targets(kind)
3103 .iter()
3104 .filter_map(StatusTarget::option)
3105 .filter(|wanted| {
3106 !existing
3107 .iter()
3108 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3109 })
3110 .map(str::to_owned)
3111 .collect();
3112 (!missing.is_empty()).then_some(KindMissing { kind, missing })
3113 })
3114 .collect()
3115 }
3116}
3117
3118impl StatusTarget {
3119 /// The board option this target selects, or `None` for an unmapped one.
3120 fn option(&self) -> Option<&str> {
3121 match self {
3122 Self::Column(name) | Self::Terminal(name, _) => Some(name.as_str()),
3123 Self::Disabled(_) => None,
3124 }
3125 }
3126}
3127
3128// 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.
3129/// One repository this source can create an issue in, as `owner/name`.
3130///
3131/// Every `createIssue` this source sends names one of these: the item's own single
3132/// `repositories` entry, else its parent project issue's repository, else the configured
3133/// [`GitHubProjectsConfig::repository`]. [`GitHubProjectsSource::creation_target`] makes
3134/// that choice and says what it refuses before `createIssue`.
3135// llmlint: ignore-end[comments_earn_their_place, contracts_have_one_source_or_a_drift_gate]
3136#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
3137struct RepositoryTarget {
3138 owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
3139 name: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
3140}
3141
3142impl RepositoryTarget {
3143 fn parse(value: &str) -> Result<Self, SourceError> {
3144 let (owner, name) = value.split_once('/').ok_or_else(|| SourceError::Config {
3145 message: format!(
3146 "repository must be spelled owner/name; {value:?} names no repository"
3147 ),
3148 })?;
3149 if !valid_github_owner(owner) || !valid_github_repository_name(name) {
3150 return Err(SourceError::Config {
3151 message: format!(
3152 "repository must be spelled owner/name with a GitHub login and one \
3153 repository name; {value:?} is not"
3154 ),
3155 });
3156 }
3157 Ok(Self {
3158 owner: owner.to_owned(),
3159 name: name.to_owned(),
3160 })
3161 }
3162
3163 /// The one host whose repositories this source creates issues in, spelled once: it is
3164 /// what [`Self::origin`] renders and what [`Self::from_origin`] accepts.
3165 const HOST: &str = "github.com";
3166
3167 fn origin(&self) -> String {
3168 format!("{}/{}/{}", Self::HOST, self.owner, self.name)
3169 }
3170
3171 /// The repository a normalized origin names, or why it is none this source can create
3172 /// an issue in: another host, or more or fewer than `owner/name` under this one.
3173 fn from_origin(origin: &Repository) -> Result<Self, String> {
3174 let not_here = || {
3175 format!(
3176 "{} is not a {}/owner/name repository",
3177 origin.as_str(),
3178 Self::HOST
3179 )
3180 };
3181 let (host, rest) = origin.as_str().split_once('/').ok_or_else(not_here)?;
3182 if host != Self::HOST {
3183 return Err(not_here());
3184 }
3185 Self::parse(rest).map_err(|_| not_here())
3186 }
3187
3188 fn slug(&self) -> String {
3189 format!("{}/{}", self.owner, self.name)
3190 }
3191}
3192
3193/// A source which reads GitHub afresh for every operation.
3194pub struct GitHubProjectsSource {
3195 /// This source's configured name, used both to tell a far end naming this source
3196 /// from one naming a system it knows nothing about, and to name the instance a
3197 /// status refusal is about.
3198 name: SourceName,
3199 owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after full GitHub-owner validation.
3200 project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after GraphQL-Int validation.
3201 repository: Option<RepositoryTarget>,
3202 endpoint: Url,
3203 token: SecretString,
3204 credential_name: String, // llmlint: ignore[invalid_states_unrepresentable] Private diagnostic value constructed only after environment-name validation.
3205 statuses: BoardStatuses,
3206 /// Where each priority lands on this board, or `None` when this instance holds none.
3207 priorities: Option<PriorityMapping>,
3208 /// The metadata values this instance projects onto board text fields, in configured order.
3209 metadata_fields: Vec<MetadataField>,
3210 client: Client,
3211 asset_client: Client,
3212 /// Every item this source has created in this command, in the order it created them —
3213 /// dropped by [`TaskSource::end_command`].
3214 ///
3215 /// GitHub's `projectV2.items` is eventually consistent: an issue added to a board with
3216 /// `addProjectV2ItemById` is routinely absent from the very next read of that board, so
3217 /// a copy resolving a dependency on an item it had just created refused it as not
3218 /// found. A board read is completed from this — an item remembered here and absent from
3219 /// the read is added back, because the board really does hold it and only the read is
3220 /// behind.
3221 ///
3222 /// It is not a cache of a user's work: nothing is remembered that this process did not
3223 /// itself just write, it lives and dies with the process, and it is never consulted for
3224 /// an item this source did not create.
3225 created: Mutex<Vec<Resolved>>,
3226 /// Every item that already existed and that this source has written in this command, as
3227 /// it wrote it — dropped by [`TaskSource::end_command`].
3228 ///
3229 /// The other half of [`Self::created`], held on the same terms and for the reason a
3230 /// narrowed read needs it: an answer from GitHub's search or from the board's own field
3231 /// filter is an index behind a write this process made moments ago, so a query matching
3232 /// what this process just wrote onto an existing item would otherwise miss it. Nothing
3233 /// is remembered that this process did not itself just write.
3234 updated: Mutex<Vec<Resolved>>,
3235 /// Every issue this source has added a comment to or edited a comment of in this command
3236 /// — dropped by [`TaskSource::end_command`].
3237 ///
3238 /// A comment-activity read is narrowed by GitHub's issue search, whose `updated:` index
3239 /// lags the write that moved an issue's `updatedAt`, and neither [`Self::created`] nor
3240 /// [`Self::updated`] is moved by a comment, so an issue this process had just commented
3241 /// on was missing from such a read — or ruled out by the `updatedAt` its own record held
3242 /// from before — until the index caught up. Each id here is a candidate of every such
3243 /// search-narrowed read, and wherever it is a candidate its comments are read rather than
3244 /// it being ruled out by a stale `updatedAt`; that read is of the issue's own node, so it
3245 /// is current. It holds ids alone: nothing of a comment is remembered. A comment another
3246 /// process wrote is still found only once the index has it.
3247 commented: Mutex<Vec<NativeId>>,
3248 /// How fast this source writes, and how long it waits out a refusal.
3249 pacing: Pacing,
3250 /// When the last content-creating mutation finished, or the moment the furthest-out
3251 /// reserved slot releases the next one, whichever is later — so the one after it can be
3252 /// spaced from that. See [`MIN_MUTATION_INTERVAL_MS`] for the interval and
3253 /// [`GitHubProjectsSource::finish_mutation`] for why completion rather than release is
3254 /// what it is measured from.
3255 last_mutation: Mutex<Option<Duration>>,
3256 clock: SharedClock,
3257 numeric_repositories: tokio::sync::Mutex<BTreeMap<RepositoryTarget, std::num::NonZeroU64>>,
3258 /// The board as this process last read it, for the length of one command — dropped by
3259 /// [`TaskSource::end_command`].
3260 ///
3261 /// A copy of a project used to re-read the whole board, paged, before writing each of
3262 /// its items, which is by far the largest part of a copy's request count and none of
3263 /// its work. Nothing else changes this board while a command runs — this source's own
3264 /// writes are the only writer — so one read answers them all.
3265 ///
3266 /// It is not a store of a user's work and it is not the cache the no-persistence
3267 /// invariant forbids: it lives and dies with the process exactly as `created` does,
3268 /// nothing is written down, and [`Self::board`] still completes it from `created`, so
3269 /// an item this command created and then depends on resolves whether or not GitHub's
3270 /// own eventually-consistent read has caught up. A write to an item already on the
3271 /// board updates the entry here too, so what this holds is the last read plus this
3272 /// process's own writes rather than a snapshot taken before them.
3273 board_cache: Mutex<Option<Board>>,
3274 /// Every issue this board's own search reported, for the length of one command — dropped
3275 /// by [`TaskSource::end_command`].
3276 ///
3277 /// The second half of a board read, and cached for the same reason and on the same
3278 /// terms as the first: it lives and dies with the process, nothing is written down, and
3279 /// a write this process makes updates the entry here exactly as it updates the one in
3280 /// [`Self::board_cache`]. One read answers every question a command asks, so a command
3281 /// that lists this board's projects and its tasks pays for one search rather than two.
3282 search_cache: Mutex<Option<Vec<Resolved>>>,
3283 /// What each narrowed question GitHub was asked answered, keyed by that question, for
3284 /// the length of one command — dropped by [`TaskSource::end_command`].
3285 ///
3286 /// The narrowed counterpart of [`Self::search_cache`], held on the same terms: it lives
3287 /// and dies with the process, nothing is written down, a write this process makes
3288 /// updates the entry here as it updates the other two, and every answer is completed
3289 /// with this process's own writes each time it is given. A command that asks the same
3290 /// narrowed question twice — a wait polling for its own items, a listing repeated after a
3291 /// write — pays for it once, which is what the whole-board read it replaced gave it.
3292 narrowed_cache: Mutex<BTreeMap<String, Vec<Resolved>>>,
3293 search_next: Mutex<BTreeMap<String, Option<String>>>,
3294 /// Records already resolved in this command, reused by writes and for comment identity.
3295 /// Explicit item reads still reach GitHub. Nothing is persisted, and
3296 /// [`TaskSource::end_command`] drops every record, so a write in the next command reads
3297 /// its item as a person has since left it.
3298 resolved_cache: Mutex<BTreeMap<NativeId, Resolved>>,
3299 /// Items an issue read by reference reached, held for the one status write each was read
3300 /// for and taken out by it alone — see [`Self::take_held`] — because that read selects only
3301 /// what such a write needs. Dropped by [`TaskSource::end_command`].
3302 held_cache: Mutex<BTreeMap<NativeId, Resolved>>,
3303 /// Each project's sub-issues as GitHub answered them, keyed by the selector they were
3304 /// asked for under, with the project that selector named — for the length of one command,
3305 /// dropped by [`TaskSource::end_command`].
3306 ///
3307 /// A caller pages through a project's tasks one engine page at a time, and every page
3308 /// is cut from the whole list of them, so without this each page walked every
3309 /// [`graphql::SUB_ISSUES`] page again and a project of `n` listing pages cost `n²`
3310 /// requests to read once. Held on the terms [`Self::narrowed_cache`] is: it lives and
3311 /// dies with the process, nothing is written down, a write this process makes updates or
3312 /// removes the entry here as it does there, and every answer is completed with this
3313 /// process's own writes each time it is given.
3314 children_cache: Mutex<ProjectChildren>,
3315 /// The board's own id and field definitions as this process last read them on their
3316 /// own, for the length of one command — dropped by [`TaskSource::end_command`].
3317 ///
3318 /// What a write needs of the board and its item does not say, read once per command
3319 /// rather than once per item written, on the terms [`Self::board_cache`] is held on: it
3320 /// lives and dies with the process and nothing is written down. It holds no item and so
3321 /// can answer no question about one — see [`Self::board_fields`].
3322 fields_cache: Mutex<Option<BoardFields>>,
3323 /// Each destination repository's node id, resolved once per repository
3324 /// rather than per issue created.
3325 ///
3326 /// A repository's node id does not change, and re-reading it for every issue of a copy
3327 /// spent one request per item on an answer this source already had. It is a map rather
3328 /// than one entry because a copy files each item in the repository its own
3329 /// `repositories` field names, so a plan across five repositories asks GitHub five
3330 /// times and not once per item.
3331 repository_cache: Mutex<BTreeMap<RepositoryTarget, String>>,
3332 /// What every request this source sends is recorded into.
3333 ///
3334 /// Ordinary code path, not a mode: [`Self::send_once`] records into it at the one place
3335 /// a request leaves this crate, so nothing has to be switched on for a session to be
3336 /// counted. It is shared rather than owned so a caller accounting for a whole session —
3337 /// its own schema verification, board lookups, residue sweep and cleanup beside this
3338 /// source's reads and writes — adds up one accounting instead of two. See
3339 /// [`accounting`] for what a record carries and what a session's spend is and is not.
3340 ledger: Arc<Accounting>,
3341 /// What [`expected`] holds for a write the store's cache expects: the journal while one is
3342 /// open, the issues primed from the cache, and the comments known on each.
3343 expected: expected::Held,
3344}
3345
3346/// GitHub's closed single-select color vocabulary.
3347#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3348#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
3349pub enum StatusOptionColor {
3350 /// Gray.
3351 Gray,
3352 /// Blue.
3353 Blue,
3354 /// Green.
3355 Green,
3356 /// Yellow.
3357 Yellow,
3358 /// Purple.
3359 Purple,
3360 /// Red.
3361 Red,
3362 /// Orange.
3363 Orange,
3364 /// Pink.
3365 Pink,
3366}
3367
3368/// Whether a guarded board setup — of the fields, or of the Status options alone — plans or
3369/// applies its additions.
3370#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3371pub enum SetupMode {
3372 /// Read without mutation.
3373 Plan,
3374 /// Apply and verify.
3375 Apply,
3376}
3377
3378/// The name [`SetupMode`] had when Status was the one field set up, kept so a caller written
3379/// against it goes on compiling.
3380pub type StatusOptionsMode = SetupMode;
3381
3382/// The explicit result of the requested operation.
3383#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3384#[serde(rename_all = "kebab-case")]
3385pub enum StatusOptionsOutcome {
3386 /// A read-only plan.
3387 Planned,
3388 /// Apply found nothing missing.
3389 Unchanged,
3390 /// Additions were applied and verified.
3391 Applied,
3392}
3393
3394/// A GitHub single-select option's opaque GraphQL node identifier.
3395#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3396#[serde(transparent)]
3397pub struct StatusOptionId(#[schemars(length(min = 1))] String);
3398
3399impl TryFrom<String> for StatusOptionId {
3400 type Error = String;
3401
3402 fn try_from(id: String) -> Result<Self, Self::Error> {
3403 if id.trim().is_empty() {
3404 return Err("a GitHub Status option id cannot be blank".to_owned());
3405 }
3406 Ok(Self(id))
3407 }
3408}
3409
3410/// One existing or proposed option in a guarded Status-field update.
3411#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3412pub struct StatusOption {
3413 /// GitHub's stable id.
3414 pub id: StatusOptionId,
3415 /// The visible option name.
3416 pub name: ColumnName,
3417 /// GitHub's single-select color token.
3418 pub color: StatusOptionColor,
3419 /// The option description, including an empty one.
3420 pub description: String,
3421}
3422
3423/// One board item's Status assignment, retained as recovery data.
3424#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3425pub struct StatusAssignment {
3426 /// The project item id whose assignment this is.
3427 // llmlint: ignore[invalid_states_unrepresentable] This opaque GraphQL node ID is
3428 // carried verbatim as operator recovery data; introducing a semantic type would claim
3429 // validation rules GitHub does not publish and no operation here interprets.
3430 pub item_id: String,
3431 /// The selected option, absent when the item has no status.
3432 #[serde(skip_serializing_if = "Option::is_none")]
3433 pub option: Option<AssignedStatusOption>,
3434}
3435
3436/// The inseparable id and name of an assigned option.
3437#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3438pub struct AssignedStatusOption {
3439 /// GitHub's stable id.
3440 pub id: StatusOptionId,
3441 /// The visible name.
3442 pub name: ColumnName,
3443}
3444
3445/// The plan and verified outcome of reconciling configured Status options.
3446#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3447pub struct StatusOptionsReport {
3448 /// The configured source name.
3449 pub source: SourceName,
3450 /// Configured option names absent before the operation.
3451 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a
3452 // `ColumnName` and has therefore already passed its nonblank validation; retaining the
3453 // serialized string here preserves the report's intentionally simple public contract.
3454 pub missing: Vec<String>,
3455 /// What the requested operation did.
3456 pub outcome: StatusOptionsOutcome,
3457 /// The complete option list observed before any mutation.
3458 pub existing: Vec<StatusOption>,
3459}
3460
3461#[derive(Debug, Clone, PartialEq, Eq)]
3462struct StatusSnapshot {
3463 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
3464 // passed back as the mutation's project identity; a newtype could enforce no stronger
3465 // invariant because GitHub publishes no grammar for it.
3466 board_id: String,
3467 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
3468 // passed back as the mutation's field identity; a newtype could enforce no stronger
3469 // invariant because GitHub publishes no grammar for it.
3470 field_id: String,
3471 options: Vec<StatusOption>,
3472 assignments: Vec<StatusAssignment>,
3473}
3474
3475/// The name of the board field a status is held in.
3476const STATUS_FIELD: &str = "Status";
3477
3478/// Every item's value of each field `report` names, as it stood before the setup wrote
3479/// anything — what a person puts back when the setup is refused part way.
3480fn recovery(report: &FieldsReport, before: &BoardSnapshot) -> Result<String, SourceError> {
3481 let assignments: BTreeMap<&str, Vec<StatusAssignment>> = report
3482 .fields
3483 .iter()
3484 .map(|field| (field.field.name(), before.assignments(field.field)))
3485 .collect();
3486 serde_json::to_string_pretty(&assignments).map_err(|error| SourceError::Malformed {
3487 message: format!("cannot render the pre-write field recovery snapshot: {error}"),
3488 })
3489}
3490
3491/// One board field the guarded setup reads and writes — every one it reads, and the only
3492/// ones it writes.
3493#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, schemars::JsonSchema)]
3494pub enum BoardField {
3495 /// The single-select `Status` field every instance's `status_mapping` resolves into.
3496 Status,
3497 /// The single-select `Priority` field an instance's `priority_mapping` resolves into.
3498 Priority,
3499}
3500
3501impl BoardField {
3502 /// The field's name on the board.
3503 #[must_use]
3504 pub const fn name(self) -> &'static str {
3505 match self {
3506 Self::Status => STATUS_FIELD,
3507 Self::Priority => PRIORITY_FIELD,
3508 }
3509 }
3510
3511 /// The field a board calls `name`, or `None` for one this setup does not own.
3512 fn named(name: &str) -> Option<Self> {
3513 [Self::Status, Self::Priority]
3514 .into_iter()
3515 .find(|field| field.name() == name)
3516 }
3517}
3518
3519/// What the guarded setup did to one field.
3520#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3521#[serde(rename_all = "kebab-case")]
3522pub enum FieldOutcome {
3523 /// A read-only plan.
3524 Planned,
3525 /// Apply found the field there with every configured option.
3526 Unchanged,
3527 /// Missing options were added to the field that was there, and verified.
3528 Applied,
3529 /// The field was not there; it was created holding the configured options, and verified.
3530 Created,
3531}
3532
3533/// One field's plan, or its verified outcome.
3534#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3535pub struct FieldReport {
3536 /// Which field.
3537 pub field: BoardField,
3538 /// Whether the board had the field before the operation.
3539 // llmlint: ignore[invalid_states_unrepresentable] `exists` beside `outcome` is the report's
3540 // wire shape as its consumer's contract fixes it — `{"field", "exists", "missing",
3541 // "outcome", "existing"}` — so folding one into the other would change a published JSON
3542 // shape. The contradictory pairings cannot be built: `GitHubProjectsSource::fields` is the
3543 // one constructor, and it derives `outcome` from `exists` in one match.
3544 pub exists: bool,
3545 /// Configured option names the field lacked before the operation — every one of them,
3546 /// in the order a new field lists them, when the field was not there at all.
3547 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
3548 // mapping name and has therefore already passed its nonblank validation; the serialized
3549 // string is the report's intentionally simple public contract, as `StatusOptionsReport`'s is.
3550 pub missing: Vec<String>,
3551 /// For the `Status` field, which item kind each missing name is configured for: one
3552 /// entry per kind `status_mapping` names a missing option for, task before project, each
3553 /// listing that kind's missing names in category order. A name both kinds use is in
3554 /// both. Empty — and left out of the JSON — when nothing is missing, and always for
3555 /// `Priority`, which only a task holds.
3556 #[serde(default, skip_serializing_if = "Vec::is_empty")]
3557 // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
3558 // both SDKs model an absent `kinds` as an empty list rather than as `null`.
3559 #[schemars(!skip_serializing_if)]
3560 pub kinds: Vec<KindMissing>,
3561 /// What the requested operation did.
3562 pub outcome: FieldOutcome,
3563 /// The field's complete option list observed before any mutation; empty when the field
3564 /// was not there.
3565 pub existing: Vec<StatusOption>,
3566}
3567
3568/// The `Status` option names one item kind's `status_mapping` names that the field lacked.
3569#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3570pub struct KindMissing {
3571 /// The kind these names are configured for.
3572 pub kind: ItemKind,
3573 /// The names that kind maps a category to and the field lacked, in category order.
3574 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
3575 // mapping name, as `FieldReport::missing`'s do, and the serialized string is the report's
3576 // intentionally simple public contract.
3577 pub missing: Vec<String>,
3578}
3579
3580/// The plan and verified outcome of setting up every field a source's configuration names.
3581#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3582pub struct FieldsReport {
3583 /// The configured source name.
3584 pub source: SourceName,
3585 /// `Status`, always, and `Priority` when the source sets `priority_mapping`.
3586 // llmlint: ignore[invalid_states_unrepresentable] A list is the report's wire shape as its
3587 // consumer's contract fixes it — `{"source", "fields": [...]}` — so a struct with one member
3588 // per field would change a published JSON shape. The states the list could hold and the
3589 // contract forbids cannot be built: `GitHubProjectsSource::fields` is the one constructor,
3590 // and it pushes `Status` first and exactly once, then `Priority` exactly when configured.
3591 pub fields: Vec<FieldReport>,
3592 /// Each board text field the source's `metadata_fields` projects a value onto, in
3593 /// configured order. Empty — and left out of the JSON — when it projects none.
3594 #[serde(default, skip_serializing_if = "Vec::is_empty")]
3595 // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
3596 // both SDKs model an absent list as an empty one rather than as `null`.
3597 #[schemars(!skip_serializing_if)]
3598 pub metadata_fields: Vec<MetadataFieldReport>,
3599}
3600
3601/// The type of a board field that is not a text field, as GitHub names it — its `dataType`, or
3602/// its GraphQL type when it has none — which is what a projected field's conflict reports.
3603///
3604/// Never empty, never beginning with whitespace, and never `TEXT`: a text field of that name is
3605/// no conflict.
3606// The schema states that rule as `NON_TEXT_FIELD_TYPE_PATTERN`, so a generated SDK model
3607// refuses exactly what `try_from` below does.
3608#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3609#[serde(transparent)]
3610pub struct NonTextFieldType(#[schemars(regex(pattern = NON_TEXT_FIELD_TYPE_PATTERN))] String);
3611
3612/// What a [`NonTextFieldType`] may be, as the emitted schema states it: a first character that
3613/// is not whitespace, and anything but exactly `TEXT` — spelled without a lookahead, which
3614/// neither SDK's validator can be relied on to support.
3615const NON_TEXT_FIELD_TYPE_PATTERN: &str =
3616 r"^(?:[^T\s]|T(?:[^E]|$)|TE(?:[^X]|$)|TEX(?:[^T]|$)|TEXT[\s\S])";
3617
3618impl TryFrom<String> for NonTextFieldType {
3619 type Error = String;
3620
3621 fn try_from(kind: String) -> Result<Self, Self::Error> {
3622 if kind.is_empty() || kind.starts_with(char::is_whitespace) {
3623 return Err("a board field's type cannot be blank or begin with whitespace".to_owned());
3624 }
3625 if kind == "TEXT" {
3626 return Err("a TEXT field is no conflict for a projected text field".to_owned());
3627 }
3628 Ok(Self(kind))
3629 }
3630}
3631
3632impl std::fmt::Display for NonTextFieldType {
3633 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3634 formatter.write_str(&self.0)
3635 }
3636}
3637
3638/// One projected metadata text field's plan, or its verified outcome.
3639///
3640/// The setup creates a missing one as a text field and never alters a field that is there:
3641/// a same-named field of another type is reported in `conflict` and left as it is.
3642#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3643pub struct MetadataFieldReport {
3644 /// The board field's name.
3645 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.
3646 /// The metadata key the value is read from.
3647 pub key: String, // llmlint: ignore[invalid_states_unrepresentable] As `field`: a validated configuration value reported back as written.
3648 /// The object keys walked inside that key's value; empty for the key's own value.
3649 pub path: Vec<String>,
3650 /// Whether the board had a text field of that name before the operation.
3651 // 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.
3652 pub exists: bool,
3653 /// The type of a field of that name that is not a text field, which the setup leaves as
3654 /// it is; absent — and left out of the JSON — when there is none.
3655 #[serde(default, skip_serializing_if = "Option::is_none")]
3656 pub conflict: Option<NonTextFieldType>,
3657 /// What the requested operation did: `planned`; `created` for a field that was not
3658 /// there; `unchanged` for one that was, and for a conflict, which is never changed.
3659 pub outcome: FieldOutcome,
3660}
3661
3662/// Which options one field is configured with, in the order a new field would list them.
3663struct FieldPlan {
3664 field: BoardField,
3665 wanted: Vec<String>,
3666}
3667
3668/// One single-select field as the guarded setup snapshots it.
3669#[derive(Debug, Clone, PartialEq, Eq)]
3670struct SnapshotField {
3671 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
3672 // passed back as the mutation's field identity; a newtype could enforce no stronger
3673 // invariant because GitHub publishes no grammar for it.
3674 field_id: String,
3675 options: Vec<StatusOption>,
3676}
3677
3678/// Every single-select field of a board and every item's value of each.
3679#[derive(Debug, Clone, PartialEq, Eq)]
3680struct BoardSnapshot {
3681 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
3682 // passed back as the mutation's project identity; a newtype could enforce no stronger
3683 // invariant because GitHub publishes no grammar for it.
3684 board_id: String,
3685 fields: BTreeMap<BoardField, SnapshotField>,
3686 /// Each board item's id, and its value of each field this setup owns that it holds one of.
3687 items: Vec<(String, BTreeMap<BoardField, AssignedStatusOption>)>,
3688}
3689
3690impl BoardSnapshot {
3691 /// Every item's value of `field`, in board order — the recovery data a drift refusal
3692 /// carries.
3693 fn assignments(&self, field: BoardField) -> Vec<StatusAssignment> {
3694 self.items
3695 .iter()
3696 .map(|(item_id, values)| StatusAssignment {
3697 item_id: item_id.clone(),
3698 option: values.get(&field).cloned(),
3699 })
3700 .collect()
3701 }
3702}
3703
3704impl GitHubProjectsSource {
3705 /// Report missing configured Status options and, when `apply` is true, add them with
3706 /// a whole-list mutation that preserves every existing id and verifies the result.
3707 ///
3708 /// # Errors
3709 ///
3710 /// Refuses a board without a single-select `Status` field. A post-write difference in
3711 /// any pre-existing option id or item assignment is refused with the complete pre-write
3712 /// assignment snapshot in the diagnostic for recovery.
3713 // llmlint: ignore[changed_behavior_has_e2e] The CLI journeys cover plan, no-op apply,
3714 // successful mutation, both drift refusals, source selection, missing Status, casing,
3715 // and paging. Transport errors remain the shared `graphql` boundary's behavior rather
3716 // than a new status-options behavior, and the pinned-schema test prevents valid GitHub
3717 // responses from entering the defensive malformed-response branches below.
3718 pub async fn status_options(
3719 &self,
3720 mode: StatusOptionsMode,
3721 ) -> Result<StatusOptionsReport, SourceError> {
3722 let before = self.status_snapshot().await?;
3723 // A terminal category's option is as configured as an open one's: a terminal
3724 // write validates it before closing and refuses when the board lacks it. Both
3725 // kinds' names are options of the one field, so both are asked for.
3726 let missing = self
3727 .statuses
3728 .wanted()
3729 .into_iter()
3730 .filter(|wanted| {
3731 !before
3732 .options
3733 .iter()
3734 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3735 })
3736 .collect::<Vec<_>>();
3737 let report = StatusOptionsReport {
3738 source: self.name.clone(),
3739 missing: missing.clone(),
3740 outcome: match (mode, missing.is_empty()) {
3741 (StatusOptionsMode::Plan, _) => StatusOptionsOutcome::Planned,
3742 (StatusOptionsMode::Apply, true) => StatusOptionsOutcome::Unchanged,
3743 (StatusOptionsMode::Apply, false) => StatusOptionsOutcome::Applied,
3744 },
3745 existing: before.options.clone(),
3746 };
3747 if mode == StatusOptionsMode::Plan || missing.is_empty() {
3748 return Ok(report);
3749 }
3750 let mut options = before
3751 .options
3752 .iter()
3753 .map(|option| {
3754 json!({
3755 "id": option.id, "name": option.name, "color": option.color,
3756 "description": option.description,
3757 })
3758 })
3759 .collect::<Vec<_>>();
3760 options.extend(missing.iter().map(|name| {
3761 json!({
3762 "name": name, "color": "GRAY", "description": ""
3763 })
3764 }));
3765 self.graphql(
3766 graphql::STATUS_OPTIONS_UPDATE,
3767 json!({"input": {
3768 "projectId": before.board_id, "fieldId": before.field_id,
3769 "singleSelectOptions": options,
3770 }}),
3771 )
3772 .await?;
3773 let after = self.status_snapshot().await?;
3774 let options_preserved = before
3775 .options
3776 .iter()
3777 .all(|old| after.options.iter().any(|new| new == old));
3778 let additions_present = missing.iter().all(|wanted| {
3779 after
3780 .options
3781 .iter()
3782 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3783 });
3784 if !options_preserved || !additions_present || after.assignments != before.assignments {
3785 let recovery = serde_json::to_string_pretty(&before.assignments).map_err(|error| {
3786 SourceError::Malformed {
3787 message: format!("cannot render pre-write Status recovery snapshot: {error}"),
3788 }
3789 })?;
3790 return Err(SourceError::Refused {
3791 message: format!(
3792 "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}"
3793 ),
3794 });
3795 }
3796 Ok(report)
3797 }
3798
3799 /// A fresh snapshot of the Status field and every board item's assignment of it.
3800 ///
3801 /// # Errors
3802 ///
3803 /// Refuses a board without a single-select `Status` field, and one the token cannot see.
3804 async fn status_snapshot(&self) -> Result<StatusSnapshot, SourceError> {
3805 // Status alone, as this operation has always read it: a `Priority` field is another
3806 // operation's, so nothing about it can refuse this one.
3807 let mut board = self.board_snapshot(&[BoardField::Status]).await?;
3808 let field = board
3809 .fields
3810 .remove(&BoardField::Status)
3811 .ok_or_else(|| self.no_status_field())?;
3812 Ok(StatusSnapshot {
3813 assignments: board.assignments(BoardField::Status),
3814 board_id: board.board_id,
3815 field_id: field.field_id,
3816 options: field.options,
3817 })
3818 }
3819
3820 /// The refusal a board with no `Status` field is answered with by the guarded setup.
3821 fn no_status_field(&self) -> SourceError {
3822 SourceError::Refused {
3823 message: format!("source {} board has no Status field", self.name),
3824 }
3825 }
3826
3827 // llmlint: ignore-block[changed_behavior_has_e2e] Valid snapshot shapes are exercised through
3828 // the real CLI loopback journey, including pagination. The individual malformed guards
3829 // are defensive validation of a schema-pinned third-party response, not separate user
3830 // journeys; drift and missing-field failures cover the operation's recovery behavior.
3831 /// A fresh snapshot of each of the `owned` fields on the board, with its options, and of
3832 /// every board item's value of each, walked to the end of the board's items. A field not
3833 /// in `owned` is read past whatever it holds.
3834 async fn board_snapshot(&self, owned: &[BoardField]) -> Result<BoardSnapshot, SourceError> {
3835 let mut after: Option<String> = None;
3836 let mut snapshot: Option<BoardSnapshot> = None;
3837 loop {
3838 let data = self
3839 .graphql(
3840 graphql::STATUS_OPTIONS_SNAPSHOT,
3841 json!({
3842 "owner": self.owner, "number": self.project_number,
3843 "first": MAX_PAGE_SIZE, "after": after, "nestedFirst": MAX_PAGE_SIZE,
3844 }),
3845 )
3846 .await?;
3847 let board = data
3848 .pointer("/owner/projectV2")
3849 .filter(|board| board.is_object())
3850 .ok_or_else(|| SourceError::Refused {
3851 message: format!(
3852 "source {} has no accessible GitHub Projects board",
3853 self.name
3854 ),
3855 })?;
3856 if board
3857 .pointer("/fields/pageInfo/hasNextPage")
3858 .and_then(Value::as_bool)
3859 != Some(false)
3860 {
3861 return Err(SourceError::Malformed {
3862 message:
3863 "GitHub project fields is incomplete or has malformed pageInfo.hasNextPage"
3864 .into(),
3865 });
3866 }
3867 let mut fields = BTreeMap::new();
3868 // Only the fields this setup owns, by name: a node the single-select fragment did not
3869 // match carries no name, and a person's own single-select field — a `Size`, a
3870 // `Team` — is none of this setup's business, so nothing about it can refuse one. A
3871 // `Status` or `Priority` field without its options is malformed, not absent.
3872 // 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.
3873 for (owned, field) in board
3874 .pointer("/fields/nodes")
3875 .and_then(Value::as_array)
3876 .ok_or_else(|| SourceError::Malformed {
3877 message: "GitHub project fields.nodes is not an array".into(),
3878 })?
3879 .iter()
3880 .filter_map(|field| {
3881 let named = BoardField::named(field.get("name")?.as_str()?)?;
3882 owned.contains(&named).then_some((named, field))
3883 })
3884 {
3885 let options = field
3886 .get("options")
3887 .and_then(Value::as_array)
3888 .ok_or_else(|| SourceError::Malformed {
3889 message: "GitHub single-select field options is not an array".into(),
3890 })?
3891 .iter()
3892 .map(|option| {
3893 Ok(StatusOption {
3894 id: StatusOptionId::try_from(required_str(option, "id")?.to_owned())
3895 .map_err(|message| SourceError::Malformed { message })?,
3896 name: ColumnName::try_from(required_str(option, "name")?.to_owned())
3897 .map_err(|message| SourceError::Malformed {
3898 message: format!(
3899 "GitHub single-select option name is invalid: {message}"
3900 ),
3901 })?,
3902 color: serde_json::from_value(
3903 option.get("color").cloned().unwrap_or(Value::Null),
3904 )
3905 .map_err(|error| {
3906 SourceError::Malformed {
3907 message: format!(
3908 "GitHub single-select option color is invalid: {error}"
3909 ),
3910 }
3911 })?,
3912 description: optional_str(option, "description")?
3913 .unwrap_or_default()
3914 .to_owned(),
3915 })
3916 })
3917 .collect::<Result<Vec<_>, SourceError>>()?;
3918 let snapshot = SnapshotField {
3919 field_id: required_nonblank_str(field, "id")?.to_owned(),
3920 options,
3921 };
3922 // A board's field names are unique, so a second one is an answer that cannot
3923 // say which field the setup would act on — refused rather than one chosen.
3924 if fields.insert(owned, snapshot).is_some() {
3925 return Err(SourceError::Malformed {
3926 message: format!(
3927 "GitHub answered two {} fields for this board",
3928 owned.name()
3929 ),
3930 });
3931 }
3932 }
3933 let board_id = required_nonblank_str(board, "id")?.to_owned();
3934 let current = snapshot.get_or_insert_with(|| BoardSnapshot {
3935 board_id,
3936 fields,
3937 items: Vec::new(),
3938 });
3939 let items = board
3940 .pointer("/items/nodes")
3941 .and_then(Value::as_array)
3942 .ok_or_else(|| SourceError::Malformed {
3943 message: "GitHub project items.nodes is not an array".into(),
3944 })?;
3945 for item in items {
3946 let field_values =
3947 item.get("fieldValues")
3948 .ok_or_else(|| SourceError::Malformed {
3949 message: "GitHub project item is missing fieldValues".into(),
3950 })?;
3951 if field_values
3952 .pointer("/pageInfo/hasNextPage")
3953 .and_then(Value::as_bool)
3954 != Some(false)
3955 {
3956 return Err(SourceError::Malformed {
3957 message: "GitHub project item fieldValues is incomplete or has malformed pageInfo.hasNextPage".into(),
3958 });
3959 }
3960 let values = item
3961 .pointer("/fieldValues/nodes")
3962 .and_then(Value::as_array)
3963 .ok_or_else(|| SourceError::Malformed {
3964 message: "GitHub project item fieldValues.nodes is not an array".into(),
3965 })?;
3966 let item_id = required_nonblank_str(item, "id")?;
3967 let mut assigned = BTreeMap::new();
3968 for value in values {
3969 let Some(field) = value
3970 .pointer("/field/name")
3971 .and_then(Value::as_str)
3972 .and_then(BoardField::named)
3973 .filter(|field| owned.contains(field))
3974 else {
3975 continue;
3976 };
3977 let held = assigned.insert(
3978 field,
3979 AssignedStatusOption {
3980 id: StatusOptionId::try_from(
3981 required_str(value, "optionId")?.to_owned(),
3982 )
3983 .map_err(|message| SourceError::Malformed { message })?,
3984 name: ColumnName::try_from(required_str(value, "name")?.to_owned())
3985 .map_err(|message| SourceError::Malformed {
3986 message: format!(
3987 "GitHub assigned {} name is invalid: {message}",
3988 field.name()
3989 ),
3990 })?,
3991 },
3992 );
3993 // An item holds one value of a field, so a second one leaves no way to
3994 // tell which it holds — and a verification or recovery built on either
3995 // could restore the wrong one.
3996 if held.is_some() {
3997 return Err(SourceError::Malformed {
3998 message: format!(
3999 "GitHub answered two {} values for board item {item_id}",
4000 field.name()
4001 ),
4002 });
4003 }
4004 }
4005 current.items.push((item_id.to_owned(), assigned));
4006 }
4007 let page = board.get("items").ok_or_else(|| SourceError::Malformed {
4008 message: "GitHub project is missing items".into(),
4009 })?;
4010 let has_next = page
4011 .pointer("/pageInfo/hasNextPage")
4012 .and_then(Value::as_bool)
4013 .ok_or_else(|| SourceError::Malformed {
4014 message: "GitHub project items.pageInfo.hasNextPage is not a boolean".into(),
4015 })?;
4016 if !has_next {
4017 break;
4018 }
4019 let next =
4020 required_nonblank_str(page.get("pageInfo").unwrap_or(&Value::Null), "endCursor")?;
4021 validate_cursor_progress(after.as_deref(), next)?;
4022 after = Some(next.to_owned());
4023 }
4024 snapshot.ok_or_else(|| SourceError::Malformed {
4025 message: "GitHub returned no board field snapshot".into(),
4026 })
4027 }
4028 // llmlint: ignore-end[changed_behavior_has_e2e]
4029
4030 /// Report every board field this source's configuration names and, with
4031 /// [`SetupMode::Apply`], set each up: add the options a field lacks, and create
4032 /// the `Priority` field when the board has none.
4033 ///
4034 /// The fields are `Status`, always, with the options `status_mapping` resolves to; and
4035 /// `Priority`, when `priority_mapping` is set, with its four mapped options — created in
4036 /// the order urgent, high, medium, low. An option a field already has keeps its id, name,
4037 /// color and description: the whole option list goes back with every existing id, because
4038 /// a re-minted id clears every item's value.
4039 ///
4040 /// # Errors
4041 ///
4042 /// Refuses a board without a single-select `Status` field. After an apply the board is
4043 /// read again, and a pre-existing option or any item's value of either field that moved is
4044 /// refused with the complete pre-write assignments in the diagnostic, for recovery.
4045 // llmlint: ignore[changed_behavior_has_e2e] The `sources fields` journeys drive plan,
4046 // unchanged apply, a created field, an added option to each field, drift refusal, a board
4047 // with no Status field and a non-github-projects source through the compiled CLI against
4048 // the loopback board. Transport errors are the shared `graphql` boundary's behavior.
4049 pub async fn fields(&self, mode: SetupMode) -> Result<FieldsReport, SourceError> {
4050 let owned: Vec<BoardField> = if self.priorities.is_some() {
4051 vec![BoardField::Status, BoardField::Priority]
4052 } else {
4053 vec![BoardField::Status]
4054 };
4055 let before = self.board_snapshot(&owned).await?;
4056 let mut plans = vec![FieldPlan {
4057 field: BoardField::Status,
4058 wanted: self.statuses.wanted(),
4059 }];
4060 if !before.fields.contains_key(&BoardField::Status) {
4061 return Err(self.no_status_field());
4062 }
4063 if let Some(mapping) = &self.priorities {
4064 plans.push(FieldPlan {
4065 field: BoardField::Priority,
4066 wanted: mapping.names().map(str::to_owned).collect(),
4067 });
4068 }
4069 // The snapshot reads single-select fields alone, so a field it did not find may still
4070 // be on the board under the name, of another type: creating one beside it would fail
4071 // part way, or leave two fields of one name. Asked of the board's own field list, and
4072 // only when a field is missing.
4073 if plans
4074 .iter()
4075 .any(|plan| !before.fields.contains_key(&plan.field))
4076 {
4077 let board = self.board_fields().await?;
4078 for plan in plans
4079 .iter()
4080 .filter(|plan| !before.fields.contains_key(&plan.field))
4081 {
4082 if let Some(field) = Board::field(&board.fields, plan.field.name())? {
4083 return Err(SourceError::Refused {
4084 message: format!(
4085 "source {}'s board has a {} field that is not a single-select field \
4086 (it is a {}), so it cannot hold this source's options; next: rename \
4087 or remove that field, then run this again",
4088 self.name,
4089 plan.field.name(),
4090 optional_str(field, "__typename")?.unwrap_or("field of another type")
4091 ),
4092 });
4093 }
4094 }
4095 }
4096 let mut reports = Vec::new();
4097 for plan in &plans {
4098 let held = before.fields.get(&plan.field);
4099 let existing = held.map(|field| field.options.clone()).unwrap_or_default();
4100 let mut missing: Vec<String> = Vec::new();
4101 for wanted in &plan.wanted {
4102 let present = existing
4103 .iter()
4104 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4105 || missing
4106 .iter()
4107 .any(|named| named.eq_ignore_ascii_case(wanted));
4108 if !present {
4109 missing.push(wanted.clone());
4110 }
4111 }
4112 let kinds = match plan.field {
4113 BoardField::Status => self.statuses.missing_by_kind(&existing),
4114 BoardField::Priority => Vec::new(),
4115 };
4116 reports.push(FieldReport {
4117 field: plan.field,
4118 exists: held.is_some(),
4119 kinds,
4120 outcome: match (mode, held.is_some(), missing.is_empty()) {
4121 (SetupMode::Plan, _, _) => FieldOutcome::Planned,
4122 (SetupMode::Apply, true, true) => FieldOutcome::Unchanged,
4123 (SetupMode::Apply, true, false) => FieldOutcome::Applied,
4124 (SetupMode::Apply, false, _) => FieldOutcome::Created,
4125 },
4126 missing,
4127 existing,
4128 });
4129 }
4130 // A projected field is a text field, which the single-select snapshot cannot see, so
4131 // it is asked of the board's own field list — and only when one is configured.
4132 let mut metadata_fields = Vec::new();
4133 if !self.metadata_fields.is_empty() {
4134 let board = self.board_fields().await?;
4135 for projection in &self.metadata_fields {
4136 let conflict = match Board::field(&board.fields, &projection.field)? {
4137 None => None,
4138 Some(field) => {
4139 let typename = optional_str(field, "__typename")?;
4140 let data_type = optional_str(field, "dataType")?;
4141 if typename == Some("ProjectV2Field") && data_type == Some("TEXT") {
4142 None
4143 } else {
4144 let named = data_type
4145 .filter(|kind| *kind != "TEXT")
4146 .or(typename)
4147 .unwrap_or("field of another type");
4148 Some(NonTextFieldType::try_from(named.to_owned()).map_err(
4149 |message| SourceError::Malformed {
4150 message: format!(
4151 "GitHub answered the board's {} field with a type this \
4152 setup cannot read: {message}",
4153 &*projection.field
4154 ),
4155 },
4156 )?)
4157 }
4158 }
4159 };
4160 let exists =
4161 conflict.is_none() && Board::field(&board.fields, &projection.field)?.is_some();
4162 metadata_fields.push(MetadataFieldReport {
4163 field: projection.field.to_string(),
4164 key: projection.key.to_string(),
4165 path: projection.path.clone(),
4166 exists,
4167 outcome: match (mode, exists || conflict.is_some()) {
4168 (SetupMode::Plan, _) => FieldOutcome::Planned,
4169 (SetupMode::Apply, true) => FieldOutcome::Unchanged,
4170 (SetupMode::Apply, false) => FieldOutcome::Created,
4171 },
4172 conflict,
4173 });
4174 }
4175 }
4176 let report = FieldsReport {
4177 source: self.name.clone(),
4178 fields: reports,
4179 metadata_fields,
4180 };
4181 let writes: Vec<&FieldReport> = report
4182 .fields
4183 .iter()
4184 .filter(|field| !field.missing.is_empty() || !field.exists)
4185 .collect();
4186 let creates: Vec<&MetadataFieldReport> = report
4187 .metadata_fields
4188 .iter()
4189 .filter(|field| field.outcome == FieldOutcome::Created)
4190 .collect();
4191 if mode == SetupMode::Plan || (writes.is_empty() && creates.is_empty()) {
4192 return Ok(report);
4193 }
4194 let mut landed: Vec<&str> = Vec::new();
4195 for field in &writes {
4196 let added = field
4197 .missing
4198 .iter()
4199 .map(|name| json!({"name": name, "color": "GRAY", "description": ""}));
4200 let sent = match before.fields.get(&field.field) {
4201 Some(held) => {
4202 let mut options = held
4203 .options
4204 .iter()
4205 .map(|option| {
4206 json!({
4207 "id": option.id, "name": option.name, "color": option.color,
4208 "description": option.description,
4209 })
4210 })
4211 .collect::<Vec<_>>();
4212 options.extend(added);
4213 self.graphql(
4214 graphql::STATUS_OPTIONS_UPDATE,
4215 json!({"input": {
4216 "projectId": before.board_id, "fieldId": held.field_id,
4217 "singleSelectOptions": options,
4218 }}),
4219 )
4220 .await
4221 }
4222 None => {
4223 self.graphql(
4224 graphql::CREATE_FIELD,
4225 json!({"input": {
4226 "projectId": before.board_id, "dataType": "SINGLE_SELECT",
4227 "name": field.field.name(),
4228 "singleSelectOptions": added.collect::<Vec<_>>(),
4229 }}),
4230 )
4231 .await
4232 }
4233 };
4234 // A mutation that failed does not establish that GitHub left its field as it was,
4235 // so every failure from here on carries the recovery data a drift refusal does.
4236 match sent {
4237 Ok(_) => landed.push(field.field.name()),
4238 Err(error) => {
4239 let changed = if landed.is_empty() {
4240 String::new()
4241 } else {
4242 format!("changed the {} field and then ", landed.join(" and "))
4243 };
4244 return Err(SourceError::Refused {
4245 message: format!(
4246 "the guarded field setup {changed}failed on the {} field, which it may \
4247 have changed part way: {error}; the pre-write item assignments \
4248 are:\n{}",
4249 field.field.name(),
4250 recovery(&report, &before)?
4251 ),
4252 });
4253 }
4254 }
4255 }
4256 for field in &creates {
4257 let sent = self
4258 .graphql(
4259 graphql::CREATE_FIELD,
4260 json!({"input": {
4261 "projectId": before.board_id, "dataType": "TEXT", "name": field.field,
4262 }}),
4263 )
4264 .await;
4265 if let Err(error) = sent {
4266 let changed = if landed.is_empty() {
4267 String::new()
4268 } else {
4269 format!("changed the {} field and then ", landed.join(" and "))
4270 };
4271 return Err(SourceError::Refused {
4272 message: format!(
4273 "the guarded field setup {changed}failed creating the {} text field: \
4274 {error}; run it again to create what is still missing",
4275 field.field
4276 ),
4277 });
4278 }
4279 landed.push(&field.field);
4280 }
4281 // The board has been written, so a verification read that fails leaves it unverified
4282 // rather than unchanged, and says what to put back.
4283 let after = match self.board_snapshot(&owned).await {
4284 Ok(after) => after,
4285 Err(error) => {
4286 return Err(SourceError::Refused {
4287 message: format!(
4288 "the guarded field setup changed the {} field and then could not read the \
4289 board back to verify it: {error}; the pre-write item assignments are:\n{}",
4290 landed.join(" and "),
4291 recovery(&report, &before)?
4292 ),
4293 });
4294 }
4295 };
4296 let mut moved = Vec::new();
4297 for field in &report.fields {
4298 let name = field.field.name();
4299 let now = after
4300 .fields
4301 .get(&field.field)
4302 .map(|held| held.options.as_slice())
4303 .unwrap_or_default();
4304 if !field.existing.iter().all(|old| now.contains(old)) {
4305 moved.push(format!(
4306 "a pre-existing {name} option id, name, color or description"
4307 ));
4308 }
4309 if !field.missing.iter().all(|wanted| {
4310 now.iter()
4311 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4312 }) {
4313 moved.push(format!("an added {name} option"));
4314 }
4315 if after.assignments(field.field) != before.assignments(field.field) {
4316 moved.push(format!("an item's {name} value"));
4317 }
4318 }
4319 if !creates.is_empty() {
4320 // Read afresh: the view held for this command is the one from before the creates.
4321 *self.fields_cache()? = None;
4322 let board = match self.board_fields().await {
4323 Ok(board) => board,
4324 Err(error) => {
4325 return Err(SourceError::Refused {
4326 message: format!(
4327 "the guarded field setup changed the {} field and then could not \
4328 read the board back to verify it: {error}; run it again to verify \
4329 it, which creates nothing a second time",
4330 landed.join(" and ")
4331 ),
4332 });
4333 }
4334 };
4335 for field in &creates {
4336 let text = Board::field(&board.fields, &field.field)?.is_some_and(|held| {
4337 held.get("__typename").and_then(Value::as_str) == Some("ProjectV2Field")
4338 && held.get("dataType").and_then(Value::as_str) == Some("TEXT")
4339 });
4340 if !text {
4341 moved.push(format!("the created {} text field", field.field));
4342 }
4343 }
4344 }
4345 if !moved.is_empty() {
4346 return Err(SourceError::Refused {
4347 message: format!(
4348 "GitHub changed {} after the guarded field setup; the pre-write item \
4349 assignments are:\n{}",
4350 moved.join(", "),
4351 recovery(&report, &before)?
4352 ),
4353 });
4354 }
4355 Ok(report)
4356 }
4357
4358 /// Validate configuration and capture the named credential without exposing it.
4359 ///
4360 /// # Errors
4361 ///
4362 /// Returns [`SourceError::Config`] for a configuration this instance cannot use and
4363 /// [`SourceError::Auth`] when the named credential is missing or empty.
4364 pub fn new(
4365 name: &SourceName,
4366 config: GitHubProjectsConfig,
4367 secrets: &dyn SecretResolver,
4368 ) -> Result<Self, SourceError> {
4369 Self::recording_into(name, config, secrets, Arc::new(Accounting::new()))
4370 }
4371
4372 /// The same, recording every request it sends into an accounting the caller holds too.
4373 ///
4374 /// [`Self::new`] is this with an accounting of its own. A caller that is also making
4375 /// its own calls to GitHub — a lane verifying a schema, sweeping residue or cleaning
4376 /// up — passes the one it records those into, so the session total accounts for the
4377 /// whole session rather than for this source's share of it.
4378 ///
4379 /// # Errors
4380 ///
4381 /// Exactly [`Self::new`]'s: [`SourceError::Config`] for a configuration this instance
4382 /// cannot use and [`SourceError::Auth`] when the named credential is missing or empty.
4383 pub fn recording_into(
4384 name: &SourceName,
4385 config: GitHubProjectsConfig,
4386 secrets: &dyn SecretResolver,
4387 ledger: Arc<Accounting>,
4388 ) -> Result<Self, SourceError> {
4389 if !valid_github_owner(&config.owner) {
4390 return Err(SourceError::Config {
4391 message: "owner must be 1-39 ASCII letters, digits, or single hyphens, and cannot start or end with a hyphen".into(),
4392 });
4393 }
4394 if config.project_number == 0 || config.project_number > i32::MAX as u32 {
4395 return Err(SourceError::Config {
4396 message: format!("project_number must be between 1 and {}", i32::MAX),
4397 });
4398 }
4399 if !valid_environment_name(&config.token_env) {
4400 return Err(SourceError::Config {
4401 message: "token_env must be a valid environment-variable name".into(),
4402 });
4403 }
4404 let repository = config
4405 .repository
4406 .as_deref()
4407 .map(RepositoryTarget::parse)
4408 .transpose()?;
4409 let endpoint = Url::parse(&config.endpoint).map_err(|e| SourceError::Config {
4410 message: format!("endpoint is not a valid URL: {e}"),
4411 })?;
4412 if endpoint.scheme() != "https"
4413 && !(endpoint.scheme() == "http"
4414 && endpoint
4415 .host_str()
4416 .is_some_and(|h| h == "127.0.0.1" || h == "localhost" || h == "::1"))
4417 {
4418 return Err(SourceError::Config {
4419 message:
4420 "endpoint must use HTTPS (HTTP is accepted only for a loopback test server)"
4421 .into(),
4422 });
4423 }
4424 let token = secrets.get(&config.token_env).filter(|token| !token.expose_secret().trim().is_empty()).ok_or_else(|| SourceError::Auth {
4425 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),
4426 })?;
4427 Ok(Self {
4428 name: name.clone(),
4429 owner: config.owner,
4430 project_number: config.project_number,
4431 repository,
4432 asset_client: assets::client(&endpoint)?,
4433 endpoint,
4434 token,
4435 credential_name: config.token_env,
4436 statuses: BoardStatuses::resolve(&config.status_mapping, name)?,
4437 priorities: config
4438 .priority_mapping
4439 .map(|mapping| PriorityMapping::resolve(mapping, name))
4440 .transpose()?,
4441 metadata_fields: MetadataField::resolve(config.metadata_fields, name)?,
4442 client: Client::builder()
4443 .user_agent("onetaskgraph")
4444 .build()
4445 .map_err(|e| SourceError::Config {
4446 message: format!("cannot build HTTP client: {e}"),
4447 })?,
4448 created: Mutex::new(Vec::new()),
4449 updated: Mutex::new(Vec::new()),
4450 commented: Mutex::new(Vec::new()),
4451 pacing: Pacing::resolve(config.pacing, name)?,
4452 last_mutation: Mutex::new(None),
4453 clock: system_clock(),
4454 numeric_repositories: tokio::sync::Mutex::new(BTreeMap::new()),
4455 board_cache: Mutex::new(None),
4456 search_cache: Mutex::new(None),
4457 narrowed_cache: Mutex::new(BTreeMap::new()),
4458 resolved_cache: Mutex::new(BTreeMap::new()),
4459 held_cache: Mutex::new(BTreeMap::new()),
4460 children_cache: Mutex::new(BTreeMap::new()),
4461 search_next: Mutex::new(BTreeMap::new()),
4462 fields_cache: Mutex::new(None),
4463 repository_cache: Mutex::new(BTreeMap::new()),
4464 ledger,
4465 expected: expected::Held::default(),
4466 })
4467 }
4468
4469 /// A snapshot of every request this source has sent, and what each cost.
4470 ///
4471 /// A value to hold and compare rather than a borrow of the accounting itself, so two
4472 /// of them can sit side by side. When this source was built with
4473 /// [`Self::recording_into`] the snapshot is the whole shared session, which is the
4474 /// point of building it that way.
4475 #[must_use]
4476 pub fn accounting(&self) -> accounting::Session {
4477 self.ledger.snapshot()
4478 }
4479
4480 /// Send one GraphQL document, pacing this source's own mutations and waiting out a
4481 /// rate limit rather than handing it straight back as an error.
4482 ///
4483 /// Retrying is safe for every document here, including the mutations, and the reason
4484 /// is that only a *refusal* is retried: [`Limiter::classify`] rules on a response
4485 /// GitHub sent, and a request GitHub refused for a rate limit did not run, so nothing
4486 /// this replays has already taken effect. An outcome this source cannot know — the
4487 /// send failed, or the body could not be read, so the mutation may well have landed —
4488 /// is [`Attempt::Failed`] in [`send_once`] and leaves this loop without a second
4489 /// attempt. A duplicate write would come from replaying one of those, and none is
4490 /// replayed.
4491 async fn graphql(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
4492 if !self.expected.journaling() {
4493 return self.graphql_sent(query, variables).await;
4494 }
4495 // While a write the store's cache expects runs, its mutations select the whole issue
4496 // they leave behind, so the response vouches for what the cache keeps — and every
4497 // request is recorded, so the write's parts are reported from their own responses.
4498 let (query, variables) = match graphql::whole_twin(query) {
4499 Some(twin) => (twin, expected::whole_variables(variables)),
4500 None => (query, variables),
4501 };
4502 let sent = self.graphql_sent(query, variables.clone()).await;
4503 self.journal(query, &variables, &sent).await;
4504 sent
4505 }
4506
4507 /// One document sent, retried while GitHub asks for a wait, exactly as asked.
4508 async fn graphql_sent(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
4509 if is_mutation(query)
4510 && ![
4511 graphql::ADD_COMMENT,
4512 graphql::UPDATE_COMMENT,
4513 graphql::DELETE_COMMENT,
4514 graphql::ADD_COMMENT_WHOLE,
4515 graphql::UPDATE_COMMENT_WHOLE,
4516 ]
4517 .contains(&query)
4518 {
4519 let mut cache = self.resolved_cache()?;
4520 for argument in ["input", "second", "third", "clear"] {
4521 if let Some(input) = variables.get(argument) {
4522 cache.retain(|id, item| {
4523 !["id", "issueId", "subjectId", "itemId"].iter().any(|key| {
4524 input
4525 .get(key)
4526 .and_then(Value::as_str)
4527 .is_some_and(|value| value == id.0 || value == item.item_id)
4528 })
4529 });
4530 }
4531 }
4532 }
4533 let doing = operation_description(query);
4534 let mut waited = Duration::ZERO;
4535 let mut waits = 0_u32;
4536 let mut backoff = self.pacing.retry_backoff;
4537 loop {
4538 if is_mutation(query) {
4539 let spacing = self.reserve_mutation_slot();
4540 if !spacing.is_zero() {
4541 self.clock.sleep(spacing).await;
4542 }
4543 }
4544 let attempt = self.send_once(query, &variables, waits + 1).await;
4545 if is_mutation(query) {
4546 self.finish_mutation();
4547 }
4548 let limited = match attempt {
4549 Ok(data) => return Ok(data),
4550 Err(Attempt::Failed(error)) => return Err(error),
4551 Err(Attempt::Limited(limited)) => limited,
4552 };
4553 // GitHub really does send `retry-after: 0`, and retrying at once is the one
4554 // move that extends a secondary limit, so a hint below the schedule's own next
4555 // wait is raised to it.
4556 let wait = match limited.hint {
4557 Some(hint) => Duration::from_secs(hint).max(backoff),
4558 None => backoff,
4559 };
4560 let remaining = self.pacing.retry_budget.saturating_sub(waited);
4561 // A wait of nothing spends none of the budget, so it is exhaustion rather
4562 // than a retry. `Pacing::resolve` rules out every way of configuring one
4563 // except a budget of zero, where reporting the first refusal is the ask.
4564 if wait.is_zero() || wait > remaining {
4565 return Err(limited.exhausted(
4566 doing,
4567 waits,
4568 waited,
4569 wait,
4570 self.pacing.retry_budget,
4571 ));
4572 }
4573 self.clock.sleep(wait).await;
4574 waited += wait;
4575 waits += 1;
4576 backoff = backoff.saturating_mul(2);
4577 }
4578 }
4579
4580 /// The next moment a content-creating mutation may leave this source, as a wait from
4581 /// now.
4582 ///
4583 /// The slot is reserved under the lock and the waiting happens outside it, so two
4584 /// callers take two slots rather than the same one — and no lock is held across an
4585 /// await.
4586 ///
4587 /// The moment it is spaced from is the previous mutation's *completion*, which
4588 /// [`Self::finish_mutation`] records. See that method for why the release moment on its
4589 /// own is the wrong thing to measure from.
4590 fn reserve_mutation_slot(&self) -> Duration {
4591 if self.pacing.min_mutation_interval.is_zero() {
4592 return Duration::ZERO;
4593 }
4594 // A poisoned lock here costs pacing, not correctness, and refusing the write over
4595 // it would turn an earlier failure into a second one for no gain.
4596 let mut last = self
4597 .last_mutation
4598 .lock()
4599 .unwrap_or_else(std::sync::PoisonError::into_inner);
4600 let now = self.clock.now();
4601 // `checked_add` rather than `+`: adding durations can panic on overflow, and
4602 // pacing is not worth a panic even at a bound `MAX_PACING_MS` already rules out.
4603 let at = last.map_or(now, |previous| {
4604 previous
4605 .checked_add(self.pacing.min_mutation_interval)
4606 .map_or(now, |earliest| earliest.max(now))
4607 });
4608 *last = Some(at);
4609 at.saturating_sub(now)
4610 }
4611
4612 /// Record that a content-creating mutation has finished, so the next one is spaced
4613 /// from here rather than from the moment this one was released.
4614 ///
4615 /// This source can only choose when a request *departs*; the limiter counts when it
4616 /// *arrives*, and the two differ by whatever the request spent in transit. Spacing one
4617 /// departure from the last therefore hands the limiter a gap of the interval less that
4618 /// transit, so a source pacing at 750 ms can still be seen arriving faster — which is
4619 /// exactly how a copy paced well inside a board's threshold was refused by it on a
4620 /// slower machine while passing on a quick one.
4621 ///
4622 /// Spacing from completion removes the subtraction rather than budgeting for it. The
4623 /// previous request had already arrived before its response came back, so its arrival
4624 /// is no later than this moment, and the next mutation is released at least the
4625 /// interval after this moment and arrives no earlier than it is released: the gap the
4626 /// limiter measures is therefore at least the interval, whatever transit costs and on
4627 /// whatever platform. The price is that a mutation's own round trip no longer counts
4628 /// towards its spacing, which makes this source slightly slower than the configured
4629 /// rate rather than slightly faster — the safe side of a limit that punishes being
4630 /// wrong by refusing reads for the next fifty minutes.
4631 ///
4632 /// A failed attempt is recorded too: a request refused by the limiter still arrived,
4633 /// and one that never left costs only a wait nobody needed.
4634 fn finish_mutation(&self) {
4635 if self.pacing.min_mutation_interval.is_zero() {
4636 return;
4637 }
4638 // A poisoned lock here costs pacing, not correctness, exactly as in the reservation.
4639 let mut last = self
4640 .last_mutation
4641 .lock()
4642 .unwrap_or_else(std::sync::PoisonError::into_inner);
4643 let now = self.clock.now();
4644 // `max` rather than an assignment: a concurrent caller may already have reserved a
4645 // slot further out, and completing this request must never pull that slot back in.
4646 *last = Some(last.map_or(now, |reserved| reserved.max(now)));
4647 }
4648
4649 /// One HTTP attempt, classified into an answer, a rate limit to wait out, or a
4650 /// failure that waiting cannot help — and recorded, whichever of the three it was.
4651 ///
4652 /// This is the one place a request leaves this crate, which is why the accounting is
4653 /// here rather than at each of the callers: a read path added later is counted without
4654 /// anybody remembering to count it, and
4655 /// `the_session_report_counts_every_request_the_board_served_and_what_each_cost` fails
4656 /// when one is not.
4657 async fn send_once(
4658 &self,
4659 query: &str,
4660 variables: &Value,
4661 number: u32,
4662 ) -> Result<Value, Attempt> {
4663 let executed = self.ledger.priced_document(query);
4664 let Attempted {
4665 result,
4666 limits,
4667 reported_cost,
4668 response_outcome,
4669 } = self.attempt(executed.as_ref(), variables).await;
4670 // No `otherwise` name: every document this source sends is one of its own, and the
4671 // inventory gate on `graphql::DOCUMENTS` is what keeps that true.
4672 let sending = accounting::Request::graphql(query, variables, None, reported_cost);
4673 let outcome = match &result {
4674 Ok(_) => accounting::Outcome::Answered,
4675 Err(Attempt::Limited(_)) => accounting::Outcome::RateLimited,
4676 Err(Attempt::Failed(_)) => accounting::Outcome::Refused,
4677 };
4678 self.ledger
4679 .record(sending.finished(outcome, limits).with_execution(
4680 Some(executed.into_owned()),
4681 response_outcome,
4682 number,
4683 ));
4684 result
4685 }
4686
4687 /// The attempt itself, with what its response said about the rate limit alongside.
4688 ///
4689 /// The two are returned together rather than recorded here because every one of the
4690 /// early exits below is a different outcome, and a record written at each of them is a
4691 /// record one of them can be added without.
4692 async fn attempt(&self, query: &str, variables: &Value) -> Attempted {
4693 let mut limits = accounting::RateLimit::default();
4694 let mut reported_cost = None;
4695 let mut response_outcome = accounting::ResponseOutcome::TransportError;
4696 let result = self
4697 .attempted(
4698 query,
4699 variables,
4700 &mut limits,
4701 &mut reported_cost,
4702 &mut response_outcome,
4703 )
4704 .await;
4705 Attempted {
4706 result,
4707 limits,
4708 reported_cost,
4709 response_outcome,
4710 }
4711 }
4712
4713 /// One HTTP attempt, filling in what its response said about the rate limit as it goes.
4714 async fn attempted(
4715 &self,
4716 query: &str,
4717 variables: &Value,
4718 limits: &mut accounting::RateLimit,
4719 reported_cost: &mut Option<u64>,
4720 response_outcome: &mut accounting::ResponseOutcome,
4721 ) -> Result<Value, Attempt> {
4722 let response = self
4723 .client
4724 .post(self.endpoint.clone())
4725 .bearer_auth(self.token.expose_secret())
4726 .json(&json!({"query": query, "variables": variables}))
4727 .send()
4728 .await
4729 .map_err(|e| {
4730 Attempt::Failed(SourceError::Unavailable {
4731 message: format!("GitHub GraphQL request failed: {e}"),
4732 })
4733 })?;
4734 let status = response.status();
4735 if !status.is_success() {
4736 *response_outcome = accounting::ResponseOutcome::HttpError;
4737 }
4738 let header = |name: &str| whole_seconds(response.headers().get(name));
4739 let served = response
4740 .headers()
4741 .get(reqwest::header::DATE)
4742 .and_then(|value| value.to_str().ok())
4743 .and_then(|value| DateTime::parse_from_rfc2822(value).ok())
4744 .map(|at| at.with_timezone(&Utc));
4745 *limits = accounting::RateLimit::read(|name| {
4746 response
4747 .headers()
4748 .get(name)
4749 .and_then(|value| value.to_str().ok())
4750 .map(str::to_owned)
4751 });
4752 // Exactly `0` is exhaustion and everything else — a count, an empty value, bytes
4753 // that are not text at all — is "not known to be exhausted". This never makes a
4754 // response a refusal on its own: it says which limiter a refusal is attributed to
4755 // and where its hint comes from, so a value this cannot read costs a hint rather
4756 // than an answer.
4757 let exhausted = response
4758 .headers()
4759 .get("x-ratelimit-remaining")
4760 .and_then(|value| value.to_str().ok())
4761 == Some("0");
4762 // `retry-after` is what GitHub asks for when it asks; when it does not and the
4763 // primary budget is spent, `x-ratelimit-reset` says when that budget comes back,
4764 // which is the same question answered as an absolute time. Nothing else here is a
4765 // hint, and a schedule is what answers a refusal that carries none.
4766 let hint = header("retry-after").or_else(|| {
4767 exhausted
4768 .then(|| header("x-ratelimit-reset"))
4769 .flatten()
4770 .map(|reset| reset.saturating_sub(Utc::now().timestamp().max(0).unsigned_abs()))
4771 });
4772 // Read before it is parsed, because the evidence which tells a secondary rate
4773 // limit from a rejected credential is in the body of a response whose status says
4774 // only "forbidden" — and a non-success response was never parsed at all.
4775 let body = response.text().await.map_err(|e| {
4776 Attempt::Failed(SourceError::Unavailable {
4777 message: format!("GitHub GraphQL response could not be read: {e}"),
4778 })
4779 })?;
4780 *reported_cost = serde_json::from_str::<Value>(&body)
4781 .ok()
4782 .as_ref()
4783 .and_then(|body| body.pointer("/data/rateLimit/cost"))
4784 .and_then(Value::as_u64);
4785 let parsed = serde_json::from_str::<Value>(&body).ok();
4786 *response_outcome = accounting::ResponseOutcome::of_graphql_reply(status, parsed.as_ref());
4787 if let Some(limiter) = Limiter::classify(status, exhausted, &body) {
4788 return Err(Attempt::Limited(Limited { limiter, hint }));
4789 }
4790 if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {
4791 return Err(Attempt::Failed(SourceError::Auth {
4792 message: format!(
4793 "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"
4794 ),
4795 }));
4796 }
4797 if !status.is_success() {
4798 return Err(Attempt::Failed(SourceError::Unavailable {
4799 message: format!("GitHub GraphQL returned HTTP {status}"),
4800 }));
4801 }
4802 let answered = if query == graphql::ISSUES_BY_REFERENCE {
4803 self.answer_unresolved(&body)
4804 } else {
4805 self.answer(&body)
4806 };
4807 let mut data = answered.map_err(Attempt::Failed)?;
4808 if let Some(served) = served {
4809 stamp_served(&mut data, &served.to_rfc3339());
4810 }
4811 Ok(data)
4812 }
4813
4814 /// What one successful HTTP response says, once its GraphQL errors are read.
4815 /// [`Self::answer`], for a read of issues by reference: a reference GitHub cannot resolve
4816 /// for this credential — a repository it cannot see, an issue number it does not hold —
4817 /// is answered `null` beside a `NOT_FOUND` error naming it, and that is an answer about
4818 /// that reference rather than a failure of the others. Any other error refuses the read,
4819 /// as there.
4820 fn answer_unresolved(&self, body: &str) -> Result<Value, SourceError> {
4821 let parsed: Option<Value> = serde_json::from_str(body).ok();
4822 // Each error is a `NOT_FOUND` whose path names one slot of the batch, `r<n>` or its
4823 // `issue`, and the answer holds `null` at exactly that path: anything else — another
4824 // kind of error, one naming no slot, or one beside a value it says is not there — is a
4825 // refusal of the whole read.
4826 let unresolved = |data: &Value, error: &Value| {
4827 error.get("type").and_then(Value::as_str) == Some("NOT_FOUND")
4828 && error
4829 .get("path")
4830 .and_then(Value::as_array)
4831 .is_some_and(|path| {
4832 let alias = path.first().and_then(Value::as_str).and_then(|alias| {
4833 alias
4834 .strip_prefix('r')
4835 .and_then(|slot| slot.parse::<usize>().ok())
4836 .filter(|slot| *slot < REFERENCE_BATCH)
4837 .map(|_| alias)
4838 });
4839 match (alias, path.get(1).and_then(Value::as_str), path.len()) {
4840 (Some(alias), None, 1) => data.get(alias).is_some_and(Value::is_null),
4841 (Some(alias), Some("issue"), 2) => data
4842 .get(alias)
4843 .and_then(|repository| repository.get("issue"))
4844 .is_some_and(Value::is_null),
4845 _ => false,
4846 }
4847 })
4848 };
4849 let unresolved_only = parsed.as_ref().is_some_and(|parsed| {
4850 parsed.get("data").is_some_and(|data| {
4851 data.is_object()
4852 && parsed
4853 .get("errors")
4854 .and_then(Value::as_array)
4855 .is_some_and(|errors| errors.iter().all(|error| unresolved(data, error)))
4856 })
4857 });
4858 match parsed {
4859 Some(parsed) if unresolved_only => Ok(parsed["data"].clone()),
4860 _ => self.answer(body),
4861 }
4862 }
4863
4864 fn answer(&self, body: &str) -> Result<Value, SourceError> {
4865 let body: Value = serde_json::from_str(body).map_err(|e| SourceError::Malformed {
4866 message: format!("GitHub returned invalid JSON: {e}"),
4867 })?;
4868 let errors = body
4869 .get("errors")
4870 .map(|value| {
4871 value.as_array().ok_or_else(|| SourceError::Malformed {
4872 message: "GitHub response errors is not an array".into(),
4873 })
4874 })
4875 .transpose()?;
4876 if let Some(errors) = errors.filter(|errors| !errors.is_empty()) {
4877 let messages = errors
4878 .iter()
4879 .filter_map(|e| e.get("message").and_then(Value::as_str))
4880 .collect::<Vec<_>>()
4881 .join("; ");
4882 let message = if messages.is_empty() {
4883 "GitHub returned GraphQL errors".into()
4884 } else {
4885 messages
4886 };
4887 let normalized = message.to_ascii_lowercase();
4888 if normalized.contains("resource not accessible") || normalized.contains("scope") {
4889 return Err(SourceError::Auth {
4890 message: format!(
4891 "{message}; grant {} Projects and Issues read/write plus Pull requests read-only access for every repository represented on the board",
4892 self.credential_name
4893 ),
4894 });
4895 }
4896 return Err(SourceError::Refused { message });
4897 }
4898 body.get("data")
4899 .filter(|data| data.is_object())
4900 .cloned()
4901 .ok_or_else(|| SourceError::Malformed {
4902 message: "GitHub response has no data object".into(),
4903 })
4904 }
4905
4906 // llmlint: ignore[boundary_inputs_validated] GitHub caps nested connections at 100 and
4907 // GraphQL cannot independently page them inside the outer item page. This source page is
4908 // deliberately bounded at that published maximum; the live drift journey exercises it.
4909 async fn board_page(
4910 &self,
4911 items_after: Option<&str>,
4912 items_first: u32,
4913 ) -> Result<Value, SourceError> {
4914 let data = self
4915 .graphql(
4916 graphql::BOARD,
4917 json!({"owner":self.owner,"number":self.project_number,
4918 "first":items_first.min(MAX_PAGE_SIZE),"after":items_after,
4919 "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
4920 )
4921 .await?;
4922 data.pointer("/owner/projectV2")
4923 .filter(|v| !v.is_null())
4924 .cloned()
4925 .ok_or_else(|| SourceError::Refused {
4926 message: format!(
4927 "GitHub project {}/{} was not found or is not visible to the token",
4928 self.owner, self.project_number
4929 ),
4930 })
4931 }
4932
4933 /// The search that finds the issues of this board, narrowed by `also` when it is
4934 /// given.
4935 ///
4936 /// `project:owner/number` is what scopes a search to one board, and `is:issue` is what
4937 /// keeps pull requests out of it: GitHub's `ISSUE` search type covers both, and a pull
4938 /// request is somebody's change rather than a unit of plan. `-has:parent` is *not*
4939 /// here on purpose — GitHub accepts it and silently ignores it, so a project is told
4940 /// from a task by the `parent` field each issue carries rather than by the search.
4941 fn board_search(&self, also: Option<&str>) -> String {
4942 let scope = format!("project:{}/{} is:issue", self.owner, self.project_number);
4943 match also {
4944 Some(also) => format!("{scope} {also}"),
4945 None => scope,
4946 }
4947 }
4948
4949 /// One issue this source reached directly, as the board item a read of the board would
4950 /// have produced — or `None` when this board does not hold it.
4951 ///
4952 /// The board half of an issue rides along on `Issue.projectItems`, so the value handed
4953 /// to [`Self::resolve`] is the very shape a `ProjectV2.items` read gives it: the board
4954 /// item's own id, that item's field values, and the issue as its content. One resolver
4955 /// for both routes is what makes an issue read through a search, through its own node
4956 /// id, or through its project's sub-issues report the same title, the same status, the
4957 /// same labels and the same qualified id.
4958 ///
4959 /// An issue with no entry for *this* board is not this source's to report, which is
4960 /// what keeps an id naming some other repository's issue from being answered as an item
4961 /// of this board. That answer is given about an **exhausted** connection and never
4962 /// about an unread page: the entry is looked for on the page in hand, and only if that
4963 /// page reports more of the connection, in [`Self::board_membership`]'s walk of the
4964 /// rest of it.
4965 async fn resolve_issue(&self, issue: &Value) -> Result<Option<Resolved>, SourceError> {
4966 self.resolve_issue_as(issue, true).await
4967 }
4968
4969 /// [`Self::resolve_issue`], remembering what it resolves for this command's later reads
4970 /// and writes only when `remember` says to.
4971 async fn resolve_issue_as(
4972 &self,
4973 issue: &Value,
4974 remember: bool,
4975 ) -> Result<Option<Resolved>, SourceError> {
4976 if optional_str(issue, "__typename")? != Some("Issue") {
4977 return Ok(None);
4978 }
4979 let memberships = issue
4980 .get("projectItems")
4981 .ok_or_else(|| SourceError::Malformed {
4982 message: "GitHub issue is missing projectItems".into(),
4983 })?;
4984 let nodes = memberships
4985 .get("nodes")
4986 .and_then(Value::as_array)
4987 .ok_or_else(|| SourceError::Malformed {
4988 message: "GitHub issue projectItems.nodes is not an array".into(),
4989 })?;
4990 let held = match self.board_entry(nodes) {
4991 Some(held) => held.clone(),
4992 None => {
4993 let info = memberships
4994 .get("pageInfo")
4995 .ok_or_else(|| SourceError::Malformed {
4996 message: "GitHub issue projectItems has no pageInfo".into(),
4997 })?;
4998 // The page held no entry for this board. Whether that means the issue is
4999 // not on it is a question about the rest of the connection, and only a
5000 // connection with no rest answers it here.
5001 if !required_bool(info, "hasNextPage")? {
5002 return Ok(None);
5003 }
5004 let cursor = required_str(info, "endCursor")?;
5005 validate_cursor_progress(None, cursor)?;
5006 let issue_id = required_str(issue, "id")?;
5007 match self.board_membership(issue_id, cursor).await? {
5008 Some(held) => held,
5009 None => return Ok(None),
5010 }
5011 }
5012 };
5013 // The membership's own `updatedAt` rides along: it is the board half of the item's change
5014 // token, which a field change moves where the issue's own time does not.
5015 let item = json!({
5016 "id": required_str(&held, "id")?,
5017 "updatedAt": held.get("updatedAt"),
5018 "project": held.get("project"),
5019 "fieldValues": held.get("fieldValues"),
5020 "content": issue,
5021 });
5022 if remember {
5023 self.resolve(&item)
5024 } else {
5025 self.resolve_unremembered(&item)
5026 }
5027 }
5028
5029 /// This board's own entry among one page of an issue's `Issue.projectItems`.
5030 ///
5031 /// One spelling of *which membership is this board's*, so the page a read carries and
5032 /// the pages [`Self::board_membership`] walks are searched by the same rule.
5033 fn board_entry<'a>(&self, nodes: &'a [Value]) -> Option<&'a Value> {
5034 nodes.iter().find(|node| {
5035 node.pointer("/project/number").and_then(Value::as_u64)
5036 == Some(u64::from(self.project_number))
5037 })
5038 }
5039
5040 /// The rest of one issue's board memberships, from `after`, for this board's entry.
5041 ///
5042 /// The recovery read: a page of memberships that holds no entry for this board says
5043 /// nothing about the memberships past it, so the connection is walked to exhaustion
5044 /// before an issue is reported as one this board does not hold. `Ok(None)` is that
5045 /// positive answer — the whole connection was read and no entry named this board —
5046 /// rather than a failure, and the walk is held to
5047 /// [`validate_cursor_progress`] like every other page walk here, so a source answering
5048 /// with a cursor that does not advance is refused instead of spun on.
5049 async fn board_membership(
5050 &self,
5051 issue: &str,
5052 after: &str,
5053 ) -> Result<Option<Value>, SourceError> {
5054 let mut after = after.to_owned();
5055 loop {
5056 let data = self
5057 .graphql(
5058 graphql::ISSUE_BOARD_ITEMS,
5059 json!({"id":issue,"first":MAX_PAGE_SIZE,"after":after,
5060 "nestedFirst":NESTED_PAGE_SIZE}),
5061 )
5062 .await?;
5063 let Some(connection) = data
5064 .pointer("/node/projectItems")
5065 .filter(|value| !value.is_null())
5066 else {
5067 // The id resolved to nothing, or to something with no memberships to walk —
5068 // which is the same answer as a connection holding no entry for this board.
5069 return Ok(None);
5070 };
5071 let nodes = connection
5072 .get("nodes")
5073 .and_then(Value::as_array)
5074 .ok_or_else(|| SourceError::Malformed {
5075 message: "GitHub issue projectItems.nodes is not an array".into(),
5076 })?;
5077 if let Some(held) = self.board_entry(nodes) {
5078 return Ok(Some(held.clone()));
5079 }
5080 let info = connection
5081 .get("pageInfo")
5082 .ok_or_else(|| SourceError::Malformed {
5083 message: "GitHub issue projectItems has no pageInfo".into(),
5084 })?;
5085 let next = required_bool(info, "hasNextPage")?
5086 .then(|| required_str(info, "endCursor"))
5087 .transpose()?;
5088 match next {
5089 Some(next) => {
5090 validate_cursor_progress(Some(&after), next)?;
5091 after = next.to_owned();
5092 }
5093 None => return Ok(None),
5094 }
5095 }
5096 }
5097
5098 /// One page of a board-scoped issue search, and where the next page resumes.
5099 async fn search_page(
5100 &self,
5101 search: &str,
5102 first: u32,
5103 after: Option<&str>,
5104 ) -> Result<(Vec<Resolved>, Option<String>), SourceError> {
5105 let data = self
5106 .graphql(
5107 graphql::SEARCH_ISSUES,
5108 json!({"search":search,"type":"ISSUE","first":first.min(MAX_PAGE_SIZE),
5109 "after":after,"nestedFirst":NESTED_PAGE_SIZE,
5110 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
5111 )
5112 .await?;
5113 let connection = data.get("search").ok_or_else(|| SourceError::Malformed {
5114 message: "GitHub search response has no search connection".into(),
5115 })?;
5116 let mut found = Vec::new();
5117 for node in connection
5118 .get("nodes")
5119 .and_then(Value::as_array)
5120 .ok_or_else(|| SourceError::Malformed {
5121 message: "GitHub search nodes is not an array".into(),
5122 })?
5123 {
5124 if let Some(resolved) = self.resolve_issue(node).await? {
5125 found.push(resolved);
5126 }
5127 }
5128 let info = connection
5129 .get("pageInfo")
5130 .ok_or_else(|| SourceError::Malformed {
5131 message: "GitHub search connection has no pageInfo".into(),
5132 })?;
5133 let next = required_bool(info, "hasNextPage")?
5134 .then(|| required_str(info, "endCursor"))
5135 .transpose()?
5136 .map(str::to_owned);
5137 if let Some(next) = &next {
5138 validate_cursor_progress(after, next)?;
5139 }
5140 Ok((found, next))
5141 }
5142
5143 /// Every issue this board holds, completed with what this run wrote.
5144 ///
5145 /// The completion is not an optimisation and it is not a cache: GitHub's issue search
5146 /// is an index and is eventually consistent, so an issue this run created seconds ago
5147 /// can be absent from it, and a project listed straight after being written would
5148 /// otherwise be missing from its own board. What is added back is only what this
5149 /// process itself wrote, out of [`Self::created`], which lives and dies with the
5150 /// process.
5151 async fn board_issues(&self) -> Result<Vec<Resolved>, SourceError> {
5152 let found = self.searched_issues().await?;
5153 self.completed_with_written(found, |_| true)
5154 }
5155
5156 /// Every issue this board's own search reports, walked to exhaustion, read once per
5157 /// source.
5158 ///
5159 /// The uncompleted half of [`Self::board_issues`], separated because [`Self::board`]
5160 /// needs it too and the two would otherwise walk the same search twice in one command.
5161 /// See [`Self::search_cache`] for why holding it is the same bargain holding the board
5162 /// is.
5163 async fn searched_issues(&self) -> Result<Vec<Resolved>, SourceError> {
5164 let cached = self.search_cache()?.clone();
5165 if let Some(held) = cached {
5166 return Ok(held);
5167 }
5168 let mut after: Option<String> = None;
5169 let mut found = Vec::new();
5170 let search = self.board_search(None);
5171 loop {
5172 let (page, next) = self
5173 .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
5174 .await?;
5175 found.extend(page);
5176 match next {
5177 Some(next) => after = Some(next),
5178 None => break,
5179 }
5180 }
5181 *self.search_cache()? = Some(found.clone());
5182 Ok(found)
5183 }
5184
5185 /// Every issue this board's own search reports with `also` beside its scope, walked to
5186 /// exhaustion — held for no later read, which may not be narrowed alike.
5187 async fn searched_narrowly(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
5188 let mut after: Option<String> = None;
5189 let mut found = Vec::new();
5190 let search = self.board_search(Some(also));
5191 loop {
5192 let (page, next) = self
5193 .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
5194 .await?;
5195 found.extend(page);
5196 match next {
5197 Some(next) => after = Some(next),
5198 None => return Ok(found),
5199 }
5200 }
5201 }
5202
5203 /// This process's own view of the board's issues, or the refusal a poisoned lock is.
5204 fn search_cache(
5205 &self,
5206 ) -> Result<std::sync::MutexGuard<'_, Option<Vec<Resolved>>>, SourceError> {
5207 self.search_cache
5208 .lock()
5209 .map_err(|_| SourceError::Unavailable {
5210 message: "this source's view of the board's issues was left inconsistent by an \
5211 earlier failure; next: run the command again"
5212 .into(),
5213 })
5214 }
5215
5216 /// `found`, with everything this run wrote that `keep` accepts and the read did not
5217 /// report.
5218 ///
5219 /// See [`Self::created`] and [`Self::board_issues`] for why a read has to be completed
5220 /// at all: the search index is behind, and a node read of an item filed moments ago can
5221 /// be too.
5222 fn completed_with_written(
5223 &self,
5224 mut found: Vec<Resolved>,
5225 keep: impl Fn(&Resolved) -> bool,
5226 ) -> Result<Vec<Resolved>, SourceError> {
5227 for own in self.created()?.iter().filter(|own| keep(own)) {
5228 if !found.iter().any(|item| item.id == own.id) {
5229 found.push(own.clone());
5230 }
5231 }
5232 Ok(found)
5233 }
5234
5235 /// What resolving one node id reached.
5236 ///
5237 /// Three answers rather than an `Option`, because a board *draft* is none of the other
5238 /// two: it is not an issue, so the issue fragment reads nothing of it, and a read of one
5239 /// is completed by a read of the draft itself rather than reported as nothing.
5240 async fn reach(&self, id: &NativeId) -> Result<Reached, SourceError> {
5241 let asked = self
5242 .graphql(
5243 graphql::ISSUE,
5244 json!({"id":id.0,"first":MAX_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
5245 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
5246 )
5247 .await;
5248 let data = match asked {
5249 Ok(data) => data,
5250 // A string that is not a node id at all is not a failure to report: it is an id
5251 // this board does not hold, which is what every read of one already answers.
5252 Err(error) if unresolvable_node(&error) => return Ok(Reached::Nothing),
5253 Err(error) => return Err(error),
5254 };
5255 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
5256 return Ok(Reached::Nothing);
5257 };
5258 if optional_str(node, "__typename")? == Some("DraftIssue") {
5259 return Ok(Reached::Draft);
5260 }
5261 Ok(match self.resolve_issue(node).await? {
5262 Some(item) => Reached::Held(Box::new(item)),
5263 None => Reached::Nothing,
5264 })
5265 }
5266
5267 /// One item of this board by its own id, whatever kind it is.
5268 ///
5269 /// Resolved from the identifier alone: no search, board-wide or otherwise. What this
5270 /// run wrote is read first, because a node read of an item created moments ago can
5271 /// still be behind the board field values written onto it — see [`Self::created`].
5272 async fn item_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
5273 if let Some(own) = self.created()?.iter().find(|own| own.id == *id) {
5274 return Ok(Some(own.clone()));
5275 }
5276 match self.reach(id).await? {
5277 Reached::Held(item) => Ok(Some(*item)),
5278 Reached::Nothing => Ok(None),
5279 Reached::Draft => self.draft_by_id(id).await,
5280 }
5281 }
5282
5283 /// Several items of this board, each by its own id, in order — what [`Self::item_by_id`]
5284 /// answers for each, read [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] rather
5285 /// than one request per id.
5286 ///
5287 /// What this run wrote answers first, as it does there, and only the rest is read. One id
5288 /// left to read is read by [`Self::item_by_id`] itself, which costs what a batch does. A
5289 /// batch GitHub refuses because one of its ids resolves to no node at all is read again one
5290 /// id at a time, so that id is answered as not held and the others as themselves; a draft
5291 /// is completed by a read of the draft, exactly as there.
5292 async fn items_by_ids(&self, ids: &[NativeId]) -> Result<Vec<Option<Resolved>>, SourceError> {
5293 let mut found: Vec<Option<Option<Resolved>>> = {
5294 let created = self.created()?;
5295 ids.iter()
5296 .map(|id| {
5297 created
5298 .iter()
5299 .find(|own| own.id == *id)
5300 .map(|own| Some(own.clone()))
5301 })
5302 .collect()
5303 };
5304 let unread: Vec<NativeId> = ids
5305 .iter()
5306 .zip(&found)
5307 .filter(|(_, found)| found.is_none())
5308 .map(|(id, _)| id.clone())
5309 .collect();
5310 let mut read = Vec::with_capacity(unread.len());
5311 if let [one] = unread.as_slice() {
5312 read.push(self.item_by_id(one).await?);
5313 } else {
5314 for batch in unread.chunks(DETAIL_BATCH) {
5315 let data = match self
5316 .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, None))
5317 .await
5318 {
5319 Ok(data) => data,
5320 Err(error) if unresolvable_node(&error) => {
5321 for id in batch {
5322 read.push(self.item_by_id(id).await?);
5323 }
5324 continue;
5325 }
5326 Err(error) => return Err(error),
5327 };
5328 for (slot, id) in batch.iter().enumerate() {
5329 let node =
5330 data.get(format!("i{slot}"))
5331 .ok_or_else(|| SourceError::Malformed {
5332 message: format!(
5333 "GitHub answered a batch read with no item for {}",
5334 id.0
5335 ),
5336 })?;
5337 read.push(if node.is_null() {
5338 None
5339 } else if optional_str(node, "__typename")? == Some("DraftIssue") {
5340 self.draft_by_id(id).await?
5341 } else {
5342 if optional_str(node, "__typename")? == Some("Issue")
5343 && required_str(node, "id")? != id.0
5344 {
5345 return Err(SourceError::Malformed {
5346 message: format!(
5347 "GitHub answered the read of {} with issue {}",
5348 id.0,
5349 required_str(node, "id")?
5350 ),
5351 });
5352 }
5353 self.resolve_issue(node).await?
5354 });
5355 }
5356 }
5357 }
5358 let mut read = read.into_iter();
5359 Ok(found
5360 .iter_mut()
5361 .map(|slot| slot.take().unwrap_or_else(|| read.next().flatten()))
5362 .collect())
5363 }
5364
5365 fn resolved_cache(
5366 &self,
5367 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
5368 self.resolved_cache
5369 .lock()
5370 .map_err(|_| SourceError::Unavailable {
5371 message: "resolved item records were left inconsistent; run the command again"
5372 .into(),
5373 })
5374 }
5375
5376 /// Reuse a record this invocation already resolved. The mutation sender invalidates
5377 /// it before writing, so a partial failure cannot leave a pre-write binding behind.
5378 async fn bound_item(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
5379 let cached = self.resolved_cache()?.get(id).cloned();
5380 if let Some(item) = cached {
5381 return Ok(Some(item));
5382 }
5383 self.item_by_id(id).await
5384 }
5385
5386 /// Take out the item an issue read by reference reached for `id`, when one is held: the
5387 /// status write it was read for is the one caller, so no other read or write is ever
5388 /// answered from a read that selected only what that write needs.
5389 fn take_held(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
5390 Ok(self
5391 .held_cache
5392 .lock()
5393 .map_err(|_| SourceError::Unavailable {
5394 message: "items read by issue reference were left inconsistent; run the \
5395 command again"
5396 .into(),
5397 })?
5398 .remove(id))
5399 }
5400
5401 /// One board draft by its own id, with the board item it sits in — or `None` when no
5402 /// item of this board is that draft's.
5403 ///
5404 /// The same decision [`Self::resolve_issue`] makes for an issue, over the draft's own
5405 /// `projectV2Items`: an entry naming this board is what makes it this board's. GitHub
5406 /// links a draft to one board item, so the page this read carries is the whole of that
5407 /// connection, and a page that reports more than it holds is refused rather than read
5408 /// as an answer about memberships nobody read.
5409 async fn draft_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
5410 let data = self
5411 .graphql(
5412 graphql::DRAFT,
5413 json!({"id":id.0,"nestedFirst":NESTED_PAGE_SIZE,
5414 "boardItems":BOARD_ITEMS_PAGE_SIZE}),
5415 )
5416 .await?;
5417 // Gone between the two reads is an answer — the draft is no longer there. Anything
5418 // else than the draft [`Self::reach`] was just told this id is, is not one.
5419 let Some(draft) = data.get("node").filter(|node| !node.is_null()) else {
5420 return Ok(None);
5421 };
5422 if optional_str(draft, "__typename")? != Some("DraftIssue") {
5423 return Err(SourceError::Malformed {
5424 message: format!(
5425 "GitHub answered {} as a draft and then as something else",
5426 id.0
5427 ),
5428 });
5429 }
5430 if required_str(draft, "id")? != id.0 {
5431 return Err(SourceError::Malformed {
5432 message: format!("GitHub answered a different draft for {}", id.0),
5433 });
5434 }
5435 let memberships = draft
5436 .get("projectV2Items")
5437 .ok_or_else(|| SourceError::Malformed {
5438 message: format!("GitHub draft {} is missing projectV2Items", id.0),
5439 })?;
5440 let nodes = memberships
5441 .get("nodes")
5442 .and_then(Value::as_array)
5443 .ok_or_else(|| SourceError::Malformed {
5444 message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
5445 })?;
5446 let info = memberships
5447 .get("pageInfo")
5448 .ok_or_else(|| SourceError::Malformed {
5449 message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
5450 })?;
5451 // Read whether or not this board's entry is on the page: a page claiming more than
5452 // the one item GitHub links a draft to is a malformed answer either way.
5453 if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
5454 return Err(SourceError::Malformed {
5455 message: format!(
5456 "GitHub draft {} reports more board items than the one GitHub links a draft \
5457 to",
5458 id.0
5459 ),
5460 });
5461 }
5462 if let Some(node) = nodes.first()
5463 && node
5464 .pointer("/project/number")
5465 .and_then(Value::as_u64)
5466 .is_none()
5467 {
5468 return Err(SourceError::Malformed {
5469 message: format!(
5470 "GitHub draft {} board item has no numeric project number",
5471 id.0
5472 ),
5473 });
5474 }
5475 let Some(held) = self.board_entry(nodes) else {
5476 return Ok(None);
5477 };
5478 if required_str(
5479 held.get("project").ok_or_else(|| SourceError::Malformed {
5480 message: format!("GitHub draft {} board item has no project", id.0),
5481 })?,
5482 "id",
5483 )? != self.board_fields().await?.id.as_str()
5484 {
5485 return Ok(None);
5486 }
5487 let item = json!({
5488 "id": required_str(held, "id")?,
5489 "project": held.get("project"),
5490 "fieldValues": held.get("fieldValues"),
5491 "content": draft,
5492 });
5493 self.resolve(&item)
5494 }
5495
5496 /// The board's own id and field definitions, for a write whose item does not carry
5497 /// them — never its items.
5498 ///
5499 /// A board this command has already listed supplies them, since it read them beside its
5500 /// items; otherwise they come from [`graphql::BOARD_FIELDS`], once per command. Neither
5501 /// is consulted about which items the board holds: see the module documentation for
5502 /// why a question about one known item is answered by reading that item.
5503 async fn board_fields(&self) -> Result<BoardFields, SourceError> {
5504 if let Some(board) = self.board_cache()?.as_ref() {
5505 return Ok(BoardFields {
5506 id: BoardId::parse(&board.id)?,
5507 fields: board.fields.clone(),
5508 });
5509 }
5510 if let Some(held) = self.fields_cache()?.clone() {
5511 return Ok(held);
5512 }
5513 let data = self
5514 .graphql(
5515 graphql::BOARD_FIELDS,
5516 json!({"owner":self.owner,"number":self.project_number,
5517 "nestedFirst":NESTED_PAGE_SIZE}),
5518 )
5519 .await?;
5520 self.fields_read(&data)
5521 }
5522
5523 /// The board's id and fields out of an answer carrying the `boardFields` root, held for
5524 /// the rest of this command.
5525 fn fields_read(&self, data: &Value) -> Result<BoardFields, SourceError> {
5526 let board = data
5527 .pointer("/boardFields/projectV2")
5528 .filter(|value| !value.is_null())
5529 .ok_or_else(|| SourceError::Refused {
5530 message: format!(
5531 "GitHub project {}/{} was not found or is not visible to the token",
5532 self.owner, self.project_number
5533 ),
5534 })?;
5535 let read = BoardFields {
5536 id: BoardId::parse(required_str(board, "id")?)?,
5537 fields: board.get("fields").cloned().unwrap_or(Value::Null),
5538 };
5539 *self.fields_cache()? = Some(read.clone());
5540 Ok(read)
5541 }
5542
5543 /// Read what creating an issue in `repository` needs and this command has not read yet —
5544 /// the board's fields and the repository's node id — in one request when it needs both.
5545 ///
5546 /// When either is already known this sends nothing, and the other is read by its own
5547 /// document where it is asked for, so no create reads anything twice.
5548 async fn creation_context(
5549 &self,
5550 repository: &RepositoryTarget,
5551 incoming: &Incoming<'_>,
5552 ) -> Result<(), SourceError> {
5553 let fields_known = self.board_cache()?.is_some() || self.fields_cache()?.is_some();
5554 if fields_known || self.repository_cache()?.contains_key(repository) {
5555 return Ok(());
5556 }
5557 let data = self
5558 .graphql(
5559 graphql::CREATION_CONTEXT,
5560 json!({"owner":self.owner,"number":self.project_number,
5561 "nestedFirst":NESTED_PAGE_SIZE,"repositoryOwner":repository.owner,
5562 "repositoryName":repository.name}),
5563 )
5564 .await?;
5565 self.fields_read(&data)?;
5566 self.repository_read(&data, repository, incoming)?;
5567 Ok(())
5568 }
5569
5570 /// This process's own view of the board's fields, or the refusal a poisoned lock is.
5571 fn fields_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<BoardFields>>, SourceError> {
5572 self.fields_cache
5573 .lock()
5574 .map_err(|_| SourceError::Unavailable {
5575 message: "this source's view of the board's fields was left inconsistent by an \
5576 earlier failure; next: run the command again"
5577 .into(),
5578 })
5579 }
5580
5581 /// What a write to `item` needs of the board, read off that item when it says enough and
5582 /// off [`Self::board_fields`] when it does not.
5583 ///
5584 /// A node read of an item names its board and carries the definition of every field it
5585 /// holds a value of — so an item naming its board, holding a value of the origin field,
5586 /// and, when the write carries a status, holding a `Status` value, needs no read of the
5587 /// board at all. **Nothing the item does not say is guessed:** a field it holds no value
5588 /// of may still be on the board, and a view reading it as absent would refuse a write the
5589 /// board can take or skip a field write the board needs, so such an item — and a create,
5590 /// which has no item yet — takes the board's fields from their own read instead.
5591 async fn fields_for(
5592 &self,
5593 item: Option<&Resolved>,
5594 writes_status: bool,
5595 selects_priority: bool,
5596 ) -> Result<BoardFields, SourceError> {
5597 if let Some(board) = item.and_then(Resolved::carried_board) {
5598 return Ok(board);
5599 }
5600 if let Some(item) = item
5601 && let Some(board_id) = item.named_board()
5602 && item.defines(ORIGIN_FIELD)
5603 && (!writes_status || item.defines("Status"))
5604 && (!selects_priority || item.defines(PRIORITY_FIELD))
5605 && self
5606 .metadata_fields
5607 .iter()
5608 .all(|projection| item.defines(&projection.field))
5609 {
5610 return Ok(BoardFields {
5611 id: board_id,
5612 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
5613 });
5614 }
5615 self.board_fields().await
5616 }
5617
5618 /// Everything filed under one issue of this board, walked to exhaustion — or `None`
5619 /// when that id names nothing here with a sub-issue relationship to walk.
5620 ///
5621 /// `None` and an empty answer are different: `None` is *this is not an issue of this
5622 /// GitHub*, which is what sends a project selector on to be read as a name, and an
5623 /// empty vector is a project that holds nothing.
5624 async fn sub_issues(&self, id: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
5625 let mut after: Option<String> = None;
5626 let mut children = Vec::new();
5627 loop {
5628 let asked = self
5629 .graphql(
5630 graphql::SUB_ISSUES,
5631 json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after,
5632 "nestedFirst":NESTED_PAGE_SIZE,
5633 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
5634 )
5635 .await;
5636 let data = match asked {
5637 Ok(data) => data,
5638 // A string that is not a node id at all is not a failure to report: it is
5639 // the ordinary answer to a selector naming a project by its name.
5640 Err(error) if unresolvable_node(&error) => return Ok(None),
5641 Err(error) => return Err(error),
5642 };
5643 let Some(connection) = data
5644 .pointer("/node/subIssues")
5645 .filter(|value| !value.is_null())
5646 else {
5647 // No such node, or one with no sub-issue relationship — a board draft is
5648 // the one this board can really hold.
5649 return Ok(None);
5650 };
5651 for node in connection
5652 .get("nodes")
5653 .and_then(Value::as_array)
5654 .ok_or_else(|| SourceError::Malformed {
5655 message: "GitHub subIssues.nodes is not an array".into(),
5656 })?
5657 {
5658 if let Some(resolved) = self.resolve_issue(node).await? {
5659 children.push(resolved);
5660 }
5661 }
5662 let info = connection
5663 .get("pageInfo")
5664 .ok_or_else(|| SourceError::Malformed {
5665 message: "GitHub subIssues connection has no pageInfo".into(),
5666 })?;
5667 let next = required_bool(info, "hasNextPage")?
5668 .then(|| required_str(info, "endCursor"))
5669 .transpose()?;
5670 match next {
5671 Some(next) => {
5672 validate_cursor_progress(after.as_deref(), next)?;
5673 after = Some(next.to_owned());
5674 }
5675 None => return Ok(Some(children)),
5676 }
5677 }
5678 }
5679
5680 /// Which issue of this board a project *name* is, or `None` when none is.
5681 ///
5682 /// One bounded query which filters on that name at the server, rather than a walk of
5683 /// every issue the board holds. The name is compared again here: the qualifier narrows
5684 /// what GitHub sends, and this source decides what it names.
5685 async fn project_by_name(&self, name: &str) -> Result<Option<NativeId>, SourceError> {
5686 let search = self.board_search(Some(&title_qualifier(name)));
5687 let mut after = None;
5688 loop {
5689 let (candidates, next) = self
5690 .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
5691 .await?;
5692 if let Some(item) = candidates.into_iter().find(|item| {
5693 item.kind == BoardKind::Work(ItemKind::Project)
5694 && item.title.eq_ignore_ascii_case(name)
5695 }) {
5696 return Ok(Some(item.id));
5697 }
5698 match next {
5699 Some(next) => after = Some(next),
5700 None => return Ok(None),
5701 }
5702 }
5703 }
5704
5705 /// Everything filed under one project of this board: the sub-issues of the issue that
5706 /// project is.
5707 ///
5708 /// Tasks *and* documents, because a document filed under a project is a sub-issue of it
5709 /// too — the caller keeps the kind it asked for. Nothing about this grows as the board
5710 /// gains projects, or as another project gains tasks.
5711 ///
5712 /// A qualified id names the issue and is asked for its sub-issues directly: one
5713 /// request, no search of any kind. Only a selector GitHub cannot resolve that way is
5714 /// read as a project *name*, which costs the one bounded search
5715 /// [`Self::project_by_name`] makes.
5716 ///
5717 /// What GitHub answered is held for the rest of the command — see [`Self::children_cache`]
5718 /// — and each answer is still cut to the items whose parent is this project, so one this
5719 /// process has since filed elsewhere is not reported here, and completed with what this
5720 /// process filed under it.
5721 async fn project_children(&self, selector: &NativeId) -> Result<Vec<Resolved>, SourceError> {
5722 let held = self.children_cache()?.get(selector).cloned();
5723 let (project, children) = match held {
5724 Some(held) => held,
5725 None => {
5726 let answered = match self.sub_issues(selector).await? {
5727 Some(children) => (selector.clone(), children),
5728 None => match self.project_by_name(&selector.0).await? {
5729 Some(project) => {
5730 let children = self.sub_issues(&project).await?.unwrap_or_default();
5731 (project, children)
5732 }
5733 None => return Ok(Vec::new()),
5734 },
5735 };
5736 self.children_cache()?
5737 .insert(selector.clone(), answered.clone());
5738 answered
5739 }
5740 };
5741 let mut children: Vec<Resolved> = children
5742 .into_iter()
5743 .filter(|child| child.parent.as_ref() == Some(&project))
5744 .collect();
5745 for own in self.updated()?.iter() {
5746 if own.parent.as_ref() == Some(&project) && !children.iter().any(|c| c.id == own.id) {
5747 children.push(own.clone());
5748 }
5749 }
5750 self.completed_with_written(children, |own| own.parent.as_ref() == Some(&project))
5751 }
5752
5753 /// Every issue of this board GitHub's issue search reports updated at or after `since`,
5754 /// completed with what this run wrote — the candidates a comment-activity read confirms.
5755 ///
5756 /// Scoped by the board and by nothing else: `project:<owner>/<number>` reaches every issue
5757 /// on the board whatever repository, and whatever owner, it lives in, so no repository or
5758 /// owner qualifier is added and none is needed. What makes the `updated:` qualifier
5759 /// sufficient is a fact about GitHub rather than about this source: a comment written on an
5760 /// issue **and a comment edited on it** both move that issue's `updatedAt`. The credentialed
5761 /// journey `an_edited_comment_moves_its_issue_and_is_selected_since` in `tests/journey`
5762 /// re-takes that fact on every run of the lane, so a change on GitHub's side fails there
5763 /// rather than silently narrowing a caller's answer.
5764 ///
5765 /// The instant is written to the second, rounded down, which can only widen what the
5766 /// search returns; confirmation against each candidate's own comments is what makes the
5767 /// answer exact. The search is an index that lags a write by a second or two — the module
5768 /// documentation records it — so a caller that asks again from its last instant should
5769 /// overlap the two by more than that.
5770 async fn updated_since(&self, since: DateTime<Utc>) -> Result<Vec<Resolved>, SourceError> {
5771 let found = self.searched(&updated_qualifier(since)).await?;
5772 self.completed_with_written(found, |_| true)
5773 }
5774
5775 /// Every issue of this board GitHub's issue search reports for the board-scoped search
5776 /// narrowed by `also`, in pages of [`SEARCH_PAGE_SIZE`].
5777 ///
5778 /// Uncompleted: what this process wrote is added by the caller, which knows whether its
5779 /// own record is the fresher of the two.
5780 async fn searched(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
5781 let search = self.board_search(Some(also));
5782 let mut after: Option<String> = None;
5783 let mut found = Vec::new();
5784 loop {
5785 let (page, next) = self
5786 .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
5787 .await?;
5788 found.extend(page);
5789 match next {
5790 Some(next) => after = Some(next),
5791 None => return Ok(found),
5792 }
5793 }
5794 }
5795
5796 /// A bounded task answer; the versioned cursor carries the connection position, how
5797 /// many rows of the page starting there were already handed out, and the own-write ids
5798 /// already observed, including across a new source instance.
5799 ///
5800 /// Every page is sent at [`SEARCH_PAGE_SIZE`] whatever the caller's limit, and a limit is
5801 /// sliced from the pages it needs; why is the module documentation's paging contract.
5802 async fn search_tasks(
5803 &self,
5804 query: &TaskQuery,
5805 page: &PageRequest,
5806 also: &str,
5807 ) -> Result<Page<Task>, SourceError> {
5808 let mut position = match &page.cursor {
5809 None => SearchPosition::default(),
5810 Some(cursor) => serde_json::from_str::<SearchPosition>(&cursor.0)
5811 .ok()
5812 .filter(|position| {
5813 position.version == SEARCH_CURSOR_VERSION
5814 && position.connection.valid_resume(position.offset)
5815 })
5816 .ok_or_else(|| SourceError::Config {
5817 message: "page cursor is invalid".into(),
5818 })?,
5819 };
5820 let search = self.board_search(Some(also));
5821 let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
5822 let own = self.with_own_writes(Vec::new())?;
5823 // An issue this process commented on is a candidate of a comment-activity read
5824 // whether or not the search has caught up with the comment; see `Self::commented`.
5825 let commented = match query.commented_since {
5826 Some(_) => self.commented()?.clone(),
5827 None => Vec::new(),
5828 };
5829 for id in own.iter().map(|item| &item.id).chain(&commented) {
5830 if !position.own.contains(id) {
5831 position.own.push(id.clone());
5832 }
5833 }
5834 let mut tasks = Vec::new();
5835 while !position.connection.exhausted() && tasks.len() < limit {
5836 let first = SEARCH_PAGE_SIZE;
5837 // Page size is part of the key: a short cached answer cannot answer a wider ask.
5838 let key =
5839 serde_json::to_string(&("page", &search, &position.connection.after(), first))
5840 .expect("search page key is serializable");
5841 let cached = if query.commented_since.is_none() {
5842 self.narrowed_cache()?.get(&key).cloned()
5843 } else {
5844 None
5845 };
5846 let (found, next) = match cached {
5847 Some(found) => {
5848 let next = self
5849 .search_next
5850 .lock()
5851 .map_err(|_| SourceError::Unavailable {
5852 message:
5853 "search pagination was left inconsistent; run the command again"
5854 .into(),
5855 })?
5856 .get(&key)
5857 .cloned()
5858 .flatten();
5859 (found, next)
5860 }
5861 None => {
5862 let (found, next) = self
5863 .search_page(&search, first, position.connection.after())
5864 .await?;
5865 if query.commented_since.is_none() {
5866 self.search_next
5867 .lock()
5868 .map_err(|_| SourceError::Unavailable {
5869 message:
5870 "search pagination was left inconsistent; run the command again"
5871 .into(),
5872 })?
5873 .insert(key.clone(), next.clone());
5874 self.narrowed_cache()?.insert(key, found.clone());
5875 }
5876 (found, next)
5877 }
5878 };
5879 let rows = found.len();
5880 for mut item in found.into_iter().skip(position.offset) {
5881 if tasks.len() == limit {
5882 break;
5883 }
5884 position.offset += 1;
5885 if position.own.contains(&item.id) {
5886 if position.seen.contains(&item.id) {
5887 continue;
5888 }
5889 position.seen.push(item.id.clone());
5890 // The search's own copy of an issue this process only commented on is as
5891 // good as a node read of it, since its comments are read either way.
5892 let only_commented = commented.contains(&item.id)
5893 && !own.iter().any(|written| written.id == item.id);
5894 if !only_commented {
5895 let updated_at = item.updated_at;
5896 let Some(written) = self.search_written(&own, &item.id).await? else {
5897 continue;
5898 };
5899 item = written;
5900 item.updated_at = item.updated_at.max(updated_at);
5901 self.resolved_cache()?.insert(item.id.clone(), item.clone());
5902 }
5903 }
5904 if item.kind == BoardKind::Work(ItemKind::Task) {
5905 let task = item.task()?;
5906 if task_matches(&task, query, &query.project)
5907 && self.commented_since(&item, query.commented_since).await?
5908 {
5909 tasks.push(task);
5910 }
5911 }
5912 }
5913 if position.offset < rows {
5914 continue;
5915 }
5916 position.offset = 0;
5917 position.connection = match next {
5918 Some(after) => SearchConnection::Continuing {
5919 after: Cursor(after),
5920 },
5921 None => SearchConnection::Exhausted {},
5922 };
5923 }
5924 if position.connection.exhausted() {
5925 for id in position.own.clone() {
5926 if position.seen.contains(&id) {
5927 continue;
5928 }
5929 if tasks.len() == limit {
5930 break;
5931 }
5932 position.seen.push(id.clone());
5933 let Some(item) = self.search_written(&own, &id).await? else {
5934 continue;
5935 };
5936 if item.kind == BoardKind::Work(ItemKind::Task) {
5937 let task = item.task()?;
5938 if task_matches(&task, query, &query.project)
5939 && self.commented_since(&item, query.commented_since).await?
5940 {
5941 tasks.push(task);
5942 }
5943 }
5944 }
5945 }
5946 let more = !position.connection.exhausted()
5947 || position.own.iter().any(|id| !position.seen.contains(id));
5948 Ok(Page {
5949 items: tasks,
5950 next: more.then(|| {
5951 Cursor(serde_json::to_string(&position).expect("search position is serializable"))
5952 }),
5953 })
5954 }
5955
5956 /// A resumed process has the ids but no write records; resolve only a record the
5957 /// current page needs, by its uncached node read rather than the lagging search index.
5958 async fn search_written(
5959 &self,
5960 own: &[Resolved],
5961 id: &NativeId,
5962 ) -> Result<Option<Resolved>, SourceError> {
5963 match own.iter().find(|item| item.id == *id) {
5964 Some(item) => Ok(Some(item.clone())),
5965 None => self.item_by_id(id).await,
5966 }
5967 }
5968
5969 /// The candidates for a task query carrying a text, metadata or origin predicate, read
5970 /// without enumerating the board — or `None` for a query carrying none of the three, which
5971 /// keeps the reads it always had.
5972 ///
5973 /// An origin is answered by [`Self::origin_candidates`], whatever else the query carries,
5974 /// because it names at most a handful of items. Text and metadata are answered by one
5975 /// board-scoped issue search carrying every term — see [`narrowing_qualifiers`] — narrowed
5976 /// further by `updated:>=` when the query also asks for comment activity, since both
5977 /// qualifiers must hold of an issue the answer keeps. Every candidate is confirmed in
5978 /// process afterwards by the same predicates [`task_matches`] applies to every read.
5979 ///
5980 /// Completed with what this process wrote, its own record winning over the index's copy
5981 /// of the same item: see [`Self::with_own_writes`].
5982 async fn narrowed(&self, query: &TaskQuery) -> Result<Option<Vec<Resolved>>, SourceError> {
5983 let asked = match (&query.origin, narrowing_qualifiers(query)) {
5984 (Some(origin), _) => Narrowing::Origin(origin.clone()),
5985 (None, Some(qualifiers)) => Narrowing::Search(match query.commented_since {
5986 Some(since) => format!("{} {qualifiers}", updated_qualifier(since)),
5987 None => qualifiers,
5988 }),
5989 (None, None) => return Ok(None),
5990 };
5991 // A question about comment activity is asked afresh every time, as it always was: it
5992 // is the one a caller polls from one source while waiting for the index, and an
5993 // answer held from the first poll would be the answer to every later one.
5994 let key = query.commented_since.is_none().then(|| asked.key());
5995 let cached = match &key {
5996 Some(key) => self.narrowed_cache()?.get(key).cloned(),
5997 None => None,
5998 };
5999 let found = match cached {
6000 Some(found) => found,
6001 None => {
6002 let found = match &asked {
6003 Narrowing::Origin(origin) => self.origin_candidates(origin).await?,
6004 Narrowing::Search(also) => self.searched(also).await?,
6005 };
6006 if let Some(key) = key {
6007 self.narrowed_cache()?.insert(key, found.clone());
6008 }
6009 found
6010 }
6011 };
6012 self.with_own_writes(found).map(Some)
6013 }
6014
6015 /// The candidates for a project or unscoped document query carrying a searchable text,
6016 /// read without enumerating the board — or `None` for a query with no text or a blank one,
6017 /// which keeps the read it always had.
6018 ///
6019 /// The text is sent as the very phrase a task query's text is — see [`text_qualifiers`] —
6020 /// in one board-scoped issue search walked to its end at [`SEARCH_PAGE_SIZE`], so what it
6021 /// costs is the issues that match and never the board. Its answer is held for the command
6022 /// under the same key [`Self::narrowed`] holds that search under, so a walk of the caller's
6023 /// pages asks GitHub once. Every candidate is confirmed afterwards by its kind and by the
6024 /// substring rule, exactly as an item of the wider read was, and is completed with what this
6025 /// process wrote: see [`Self::with_own_writes`].
6026 async fn text_searched(
6027 &self,
6028 text: Option<&TextQuery>,
6029 ) -> Result<Option<Vec<Resolved>>, SourceError> {
6030 let Some(also) = text_qualifiers(text) else {
6031 return Ok(None);
6032 };
6033 let key = Narrowing::Search(also.clone()).key();
6034 let cached = self.narrowed_cache()?.get(&key).cloned();
6035 let found = match cached {
6036 Some(found) => found,
6037 None => {
6038 let found = self.searched(&also).await?;
6039 self.narrowed_cache()?.insert(key, found.clone());
6040 found
6041 }
6042 };
6043 self.with_own_writes(found).map(Some)
6044 }
6045
6046 /// Every item of this board that may carry `origin` — a superset of those that do — found
6047 /// by [`graphql::ORIGIN_LOOKUP`] and never by enumerating the board.
6048 ///
6049 /// The union of the board's own field filter over the `onetaskgraph.origin` text field —
6050 /// which reads the field every carrier holds, whichever release wrote it — and the
6051 /// board-scoped issue search for the same id as a phrase in the body, where this source
6052 /// mirrors it. The caller adds the third read, this process's own writes. Candidates are
6053 /// returned unconfirmed; [`task_matches`] compares each one's own origin field with the
6054 /// query's, exactly.
6055 ///
6056 /// Both connections are walked to exhaustion, each from its own cursor. One that has
6057 /// already ended is sent its last cursor again, which answers an empty page, so the one
6058 /// document serves every page of either. What the two leave is stated in the module
6059 /// documentation: a carrier another process added within the last second or two, before
6060 /// either index has it.
6061 async fn origin_candidates(&self, origin: &str) -> Result<Vec<Resolved>, SourceError> {
6062 let filter = format!("{ORIGIN_FIELD}:{}", quoted(origin));
6063 let search = self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))));
6064 let mut items_after: Option<String> = None;
6065 let mut search_after: Option<String> = None;
6066 let mut found: Vec<Resolved> = Vec::new();
6067 let keep = |resolved: Resolved, found: &mut Vec<Resolved>| {
6068 if !found.iter().any(|held| held.id == resolved.id) {
6069 found.push(resolved);
6070 }
6071 };
6072 loop {
6073 let data = self
6074 .graphql(
6075 graphql::ORIGIN_LOOKUP,
6076 json!({"owner":self.owner,"number":self.project_number,"filter":filter,
6077 "search":search,"type":"ISSUE","originFirst":ORIGIN_PAGE_SIZE,
6078 "itemsAfter":items_after,"searchAfter":search_after,
6079 "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
6080 "duplicates":true}),
6081 )
6082 .await?;
6083 let items = data
6084 .pointer("/originItems/projectV2/items")
6085 .filter(|value| !value.is_null())
6086 .ok_or_else(|| SourceError::Refused {
6087 message: format!(
6088 "GitHub project {}/{} was not found or is not visible to the token",
6089 self.owner, self.project_number
6090 ),
6091 })?;
6092 for item in optional_nodes(Some(items), "project items")?
6093 .into_iter()
6094 .flatten()
6095 {
6096 // The board's own items list its drafts too, and a draft is not an issue: no
6097 // narrowed read answers with one, whatever its origin field holds.
6098 if let Some(resolved) = self.resolve(item)?
6099 && resolved.content_kind == ContentKind::Issue
6100 {
6101 keep(resolved, &mut found);
6102 }
6103 }
6104 let searched = data.get("search").ok_or_else(|| SourceError::Malformed {
6105 message: "GitHub search response has no search connection".into(),
6106 })?;
6107 for node in optional_nodes(Some(searched), "search")?
6108 .into_iter()
6109 .flatten()
6110 {
6111 if let Some(resolved) = self.resolve_issue(node).await? {
6112 keep(resolved, &mut found);
6113 }
6114 }
6115 let items_next = resumed(items, items_after.as_deref())?;
6116 let search_next = resumed(searched, search_after.as_deref())?;
6117 if !items_next.has_more() && !search_next.has_more() {
6118 return Ok(found);
6119 }
6120 items_after = items_next.cursor();
6121 search_after = search_next.cursor();
6122 }
6123 }
6124
6125 /// `found`, with every item this process created or wrote in its place, and every one of
6126 /// them the read did not report added.
6127 ///
6128 /// This process's own record wins over the read's copy of the same item, because a read
6129 /// of an item written moments ago can still be behind what was written onto it — the
6130 /// origin field included, which is the one a narrowed read is confirmed against — and a
6131 /// read that still names an item under a predicate this process's write moved it out of
6132 /// must not return it. The one thing the read knows that the record cannot is when GitHub
6133 /// last saw the item change, which is what a comment-activity read rules a candidate out
6134 /// by, so the read's `updatedAt` is kept when the record has none of its own. See
6135 /// [`Self::created`] and [`Self::updated`](GitHubProjectsSource::updated).
6136 fn with_own_writes(&self, mut found: Vec<Resolved>) -> Result<Vec<Resolved>, SourceError> {
6137 // A board draft is not an issue, so no narrowed read returns one, and this process
6138 // having written one does not make it an answer either.
6139 let own: Vec<Resolved> = self
6140 .created()?
6141 .iter()
6142 .chain(self.updated()?.iter())
6143 .filter(|own| own.content_kind == ContentKind::Issue)
6144 .cloned()
6145 .collect();
6146 for mut own in own {
6147 self.resolved_cache()?.insert(own.id.clone(), own.clone());
6148 match found.iter_mut().find(|read| read.id == own.id) {
6149 Some(read) => {
6150 own.updated_at = own.updated_at.max(read.updated_at);
6151 *read = own;
6152 }
6153 None => found.push(own),
6154 }
6155 }
6156 Ok(found)
6157 }
6158
6159 /// Whether `item` has a comment created or last edited at or after `since` — always, when
6160 /// there is no instant to hold it to.
6161 ///
6162 /// The candidate's own `updatedAt` is read first, because a comment written or edited at
6163 /// or after the instant moved it there: an issue not updated since holds no such comment,
6164 /// and its comments are never asked for — unless this process commented on it in this
6165 /// command, when the `updatedAt` held may predate that comment; see [`Self::commented`]. Otherwise its comments are walked, oldest first,
6166 /// only as far as the first that matches. A board draft is not an issue and has no
6167 /// comments, so it never matches.
6168 async fn commented_since(
6169 &self,
6170 item: &Resolved,
6171 since: Option<DateTime<Utc>>,
6172 ) -> Result<bool, SourceError> {
6173 let Some(since) = since else {
6174 return Ok(true);
6175 };
6176 if item.content_kind == ContentKind::DraftIssue {
6177 return Ok(false);
6178 }
6179 // An `updatedAt` this process's own record or a lagging index holds can predate a
6180 // comment this process wrote since, so only an issue it did not comment on is ruled
6181 // out by one.
6182 if item.updated_at.is_some_and(|updated| updated < since)
6183 && !self.commented()?.contains(&item.id)
6184 {
6185 return Ok(false);
6186 }
6187 let query = TaskQuery {
6188 commented_since: Some(since),
6189 ..TaskQuery::default()
6190 };
6191 let mut after: Option<String> = None;
6192 loop {
6193 let data = self
6194 .graphql(
6195 graphql::ISSUE_COMMENTS,
6196 json!({"id":item.id.0,"first":MAX_PAGE_SIZE,"after":after}),
6197 )
6198 .await?;
6199 let Some(connection) = data
6200 .get("node")
6201 .filter(|value| !value.is_null())
6202 .and_then(|node| node.get("comments"))
6203 .filter(|value| !value.is_null())
6204 else {
6205 // Removed since the search reported it: no longer an issue with comments.
6206 return Ok(false);
6207 };
6208 let comments = optional_nodes(Some(connection), "issue comments")?
6209 .into_iter()
6210 .flatten()
6211 .map(comment_from)
6212 .collect::<Result<Vec<_>, _>>()?;
6213 if query.comments_match(&comments) {
6214 return Ok(true);
6215 }
6216 match next_cursor(connection)? {
6217 Some(next) => {
6218 validate_cursor_progress(after.as_deref(), &next.0)?;
6219 after = Some(next.0);
6220 }
6221 None => return Ok(false),
6222 }
6223 }
6224 }
6225
6226 /// Every item on the board: the union of both enumerations GitHub offers of one.
6227 ///
6228 /// Neither contains the other, so neither is dropped — only `ProjectV2.items` lists a
6229 /// board **draft** and reads the board's own fields beside its items, and only the search
6230 /// reports an item that connection is behind on. The module documentation is where the lag and the
6231 /// measurements behind it are written down.
6232 ///
6233 /// A search result is admitted on the same terms as any other issue this source reaches
6234 /// directly — [`Self::resolve_issue`] keeps it only if that issue's own `projectItems`
6235 /// names *this* board — so an issue the index still believes is here after it was taken
6236 /// off is refused rather than reported.
6237 ///
6238 /// See [`Self::board_cache`]. Both completions happen on every call rather than once,
6239 /// which is what the cache could otherwise have broken.
6240 async fn board(&self) -> Result<Board, SourceError> {
6241 self.board_completed_by(None).await
6242 }
6243
6244 /// [`Self::board`], its search half narrowed by `also` when it is given — which an issue
6245 /// that half could add is then answered only if it holds.
6246 async fn board_completed_by(&self, also: Option<&str>) -> Result<Board, SourceError> {
6247 let cached = self.board_cache()?.clone();
6248 let mut board = match cached {
6249 Some(board) => board,
6250 None => {
6251 let read = self.read_board().await?;
6252 *self.board_cache()? = Some(read.clone());
6253 read
6254 }
6255 };
6256 let searched = match also {
6257 None => self.searched_issues().await?,
6258 Some(also) => self.searched_narrowly(also).await?,
6259 };
6260 for held in searched {
6261 if !board.items.iter().any(|item| item.id == held.id) {
6262 board.items.push(held);
6263 }
6264 }
6265 for own in self.created()?.iter() {
6266 if !board.items.iter().any(|item| item.id == own.id) {
6267 board.items.push(own.clone());
6268 }
6269 }
6270 Ok(board)
6271 }
6272
6273 /// This process's own view of the board, or the refusal a poisoned lock is.
6274 fn board_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<Board>>, SourceError> {
6275 self.board_cache
6276 .lock()
6277 .map_err(|_| SourceError::Unavailable {
6278 message: "this source's view of the board was left inconsistent by an earlier \
6279 failure; next: run the command again"
6280 .into(),
6281 })
6282 }
6283
6284 /// Bring this process's own view of the board up to an item it has just written.
6285 ///
6286 /// A created item goes to `created`, which is what completes a board read GitHub's own
6287 /// eventual consistency has left behind. An item that was already there is replaced
6288 /// where it sits, so a second write of it in the same command reads its real parent
6289 /// rather than the one it had before the first write.
6290 ///
6291 /// "Where it sits" is three places, and missing an earlier one leaves a stale record
6292 /// that wins: an item this same run created is held in `created` and not in the cached
6293 /// board, and `board` completes the cached board *from* `created`, so replacing only
6294 /// the cached copy of such an item replaces nothing and the read still reports the
6295 /// title it was created with. The search is the third, and it is the one an item the
6296 /// board's own projection is behind on sits in *alone* — which is exactly the item this
6297 /// source is least able to re-read, so leaving it out would put the stale title back on
6298 /// the only items the completion in [`Self::board`] exists for.
6299 fn remember_written(&self, item: Resolved, created: bool) -> Result<(), SourceError> {
6300 self.resolved_cache()?.insert(item.id.clone(), item.clone());
6301 if created {
6302 self.created()?.push(item);
6303 return Ok(());
6304 }
6305 {
6306 let mut own = self.created()?;
6307 if let Some(held) = own.iter_mut().find(|held| held.id == item.id) {
6308 *held = item;
6309 return Ok(());
6310 }
6311 }
6312 {
6313 let mut own = self.updated()?;
6314 match own.iter_mut().find(|held| held.id == item.id) {
6315 Some(held) => *held = item.clone(),
6316 None => own.push(item.clone()),
6317 }
6318 }
6319 if let Some(board) = self.board_cache()?.as_mut()
6320 && let Some(held) = board.items.iter_mut().find(|held| held.id == item.id)
6321 {
6322 *held = item.clone();
6323 }
6324 if let Some(found) = self.search_cache()?.as_mut()
6325 && let Some(held) = found.iter_mut().find(|held| held.id == item.id)
6326 {
6327 *held = item.clone();
6328 }
6329 for found in self.narrowed_cache()?.values_mut() {
6330 if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
6331 *held = item.clone();
6332 }
6333 }
6334 for (_, found) in self.children_cache()?.values_mut() {
6335 if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
6336 *held = item.clone();
6337 }
6338 }
6339 Ok(())
6340 }
6341
6342 /// Forget one item this process has just deleted, from every half of its own view.
6343 fn forget(&self, id: &NativeId) -> Result<(), SourceError> {
6344 self.resolved_cache()?.remove(id);
6345 self.created()?.retain(|own| own.id != *id);
6346 self.updated()?.retain(|own| own.id != *id);
6347 self.commented()?.retain(|own| own != id);
6348 if let Some(board) = self.board_cache()?.as_mut() {
6349 board.items.retain(|item| item.id != *id);
6350 }
6351 if let Some(found) = self.search_cache()?.as_mut() {
6352 found.retain(|item| item.id != *id);
6353 }
6354 for found in self.narrowed_cache()?.values_mut() {
6355 found.retain(|item| item.id != *id);
6356 }
6357 for (_, found) in self.children_cache()?.values_mut() {
6358 found.retain(|item| item.id != *id);
6359 }
6360 Ok(())
6361 }
6362
6363 /// This process's own record of each narrowed answer, or the refusal a poisoned lock is.
6364 fn narrowed_cache(
6365 &self,
6366 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<String, Vec<Resolved>>>, SourceError> {
6367 self.narrowed_cache
6368 .lock()
6369 .map_err(|_| SourceError::Unavailable {
6370 message: "this source's view of a narrowed read was left inconsistent by an \
6371 earlier failure; next: run the command again"
6372 .into(),
6373 })
6374 }
6375
6376 /// This process's own record of each project's sub-issues, or the refusal a poisoned lock
6377 /// is.
6378 fn children_cache(&self) -> Result<std::sync::MutexGuard<'_, ProjectChildren>, SourceError> {
6379 self.children_cache
6380 .lock()
6381 .map_err(|_| SourceError::Unavailable {
6382 message: "this source's view of a project's tasks was left inconsistent by an \
6383 earlier failure; next: run the command again"
6384 .into(),
6385 })
6386 }
6387
6388 /// Every page of the board, read from GitHub.
6389 async fn read_board(&self) -> Result<Board, SourceError> {
6390 let mut after: Option<String> = None;
6391 let mut items = Vec::new();
6392 let mut board;
6393 loop {
6394 let page = self.board_page(after.as_deref(), MAX_PAGE_SIZE).await?;
6395 for item in page
6396 .pointer("/items/nodes")
6397 .and_then(Value::as_array)
6398 .ok_or_else(|| SourceError::Malformed {
6399 message: "GitHub project items.nodes is not an array".into(),
6400 })?
6401 {
6402 if let Some(resolved) = self.resolve(item)? {
6403 items.push(resolved);
6404 }
6405 }
6406 let info = page
6407 .pointer("/items/pageInfo")
6408 .ok_or_else(|| SourceError::Malformed {
6409 message: "GitHub project items have no pageInfo".into(),
6410 })?;
6411 let has_next = required_bool(info, "hasNextPage")?;
6412 let next = has_next
6413 .then(|| required_str(info, "endCursor"))
6414 .transpose()?;
6415 board = page.clone();
6416 match next {
6417 Some(next) => {
6418 validate_cursor_progress(after.as_deref(), next)?;
6419 after = Some(next.to_owned());
6420 }
6421 None => break,
6422 }
6423 }
6424 Ok(Board {
6425 id: required_str(&board, "id")?.to_owned(),
6426 fields: board.get("fields").cloned().unwrap_or(Value::Null),
6427 items,
6428 })
6429 }
6430
6431 /// The existing items this source has written, for completing a narrowed read that is
6432 /// behind; see [`Self::updated`](GitHubProjectsSource::updated).
6433 fn updated(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
6434 self.updated.lock().map_err(|_| SourceError::Unavailable {
6435 message: "this source's record of what it wrote in this run was left inconsistent \
6436 by an earlier failure; next: run the command again"
6437 .into(),
6438 })
6439 }
6440
6441 /// The issues this source has commented on in this command; see
6442 /// [`Self::commented`](GitHubProjectsSource::commented).
6443 fn commented(&self) -> Result<std::sync::MutexGuard<'_, Vec<NativeId>>, SourceError> {
6444 self.commented.lock().map_err(|_| SourceError::Unavailable {
6445 message: "this source's record of what it commented on in this run was left \
6446 inconsistent by an earlier failure; next: run the command again"
6447 .into(),
6448 })
6449 }
6450
6451 /// Called only once GitHub has answered the comment write, so an issue whose comment
6452 /// failed is never made a candidate a later read would pay a node read for.
6453 fn remember_commented(&self, issue: &NativeId) -> Result<(), SourceError> {
6454 let mut commented = self.commented()?;
6455 if !commented.contains(issue) {
6456 commented.push(issue.clone());
6457 }
6458 Ok(())
6459 }
6460
6461 /// The items this source has created, for completing a board read that is behind.
6462 fn created(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
6463 self.created.lock().map_err(|_| SourceError::Unavailable {
6464 message: "this source's record of what it created in this run was left \
6465 inconsistent by an earlier failure; next: run the command again"
6466 .into(),
6467 })
6468 }
6469
6470 /// One board item as this source reports it, or `None` for content it ignores.
6471 ///
6472 /// A pull request is neither a project nor a task — it is somebody's change, not a
6473 /// unit of plan — and an item whose content the token cannot see has nothing to
6474 /// report at all.
6475 fn resolve(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
6476 let resolved = self.resolve_unremembered(item)?;
6477 if let Some(resolved) = &resolved {
6478 self.resolved_cache()?
6479 .insert(resolved.id.clone(), resolved.clone());
6480 }
6481 Ok(resolved)
6482 }
6483
6484 /// [`Self::resolve`], without remembering the item it resolves: what reading a token off a
6485 /// write's own response needs, which says nothing this process should answer a later read
6486 /// with.
6487 fn resolve_unremembered(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
6488 let content = item.get("content").ok_or_else(|| SourceError::Malformed {
6489 message: "GitHub project item is missing content".into(),
6490 })?;
6491 if content.is_null() {
6492 return Ok(None);
6493 }
6494 let content_kind = match required_str(content, "__typename")? {
6495 "Issue" => ContentKind::Issue,
6496 "DraftIssue" => ContentKind::DraftIssue,
6497 _ => return Ok(None),
6498 };
6499 let field_values = item
6500 .get("fieldValues")
6501 .ok_or_else(|| SourceError::Malformed {
6502 message: "GitHub project item is missing fieldValues".into(),
6503 })?;
6504 complete_connection(field_values, "project item field values", NESTED_PAGE_SIZE)?;
6505 let nodes = field_values
6506 .get("nodes")
6507 .and_then(Value::as_array)
6508 .ok_or_else(|| SourceError::Malformed {
6509 message: "GitHub project item fieldValues.nodes is not an array".into(),
6510 })?;
6511 if let Some(labels) = content.get("labels") {
6512 complete_connection(labels, "content labels", NESTED_PAGE_SIZE)?;
6513 }
6514 let raw_body = optional_str(content, "body")?.map(str::to_owned);
6515 let (body, slot) = metadata_body(raw_body.clone())?;
6516 let parent = optional_str(content.get("parent").unwrap_or(&Value::Null), "id")?
6517 .map(|id| NativeId(id.to_owned()));
6518 // A draft has no sub-issues to summarise, and GitHub's schema gives it no field
6519 // to read one from; it is a task, and never a project.
6520 let sub_issues = match content_kind {
6521 ContentKind::Issue => sub_issue_total(content)?,
6522 ContentKind::DraftIssue => 0,
6523 };
6524 let content_id = required_str(content, "id")?;
6525 let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
6526 message: format!("GitHub issue {content_id}: {message}"),
6527 })?;
6528 let raw_title = required_str(content, "title")?;
6529 let kind = BoardKind::of(raw_title, parent.is_some(), sub_issues, marked);
6530 // The title a person wrote, which for a document is the one without the prefix —
6531 // the same way `content` above is the body without this source's metadata slot.
6532 let title = match kind {
6533 BoardKind::Document => raw_title[DESIGN_TITLE_PREFIX.len()..].to_owned(),
6534 BoardKind::Work(_) => raw_title.to_owned(),
6535 };
6536 let own_repository = content
6537 .pointer("/repository/nameWithOwner")
6538 .and_then(Value::as_str)
6539 .map(|origin| Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST)))
6540 .transpose()
6541 .map_err(|message| SourceError::Malformed { message })?;
6542 let repositories = if slot.contains_key(Repository::METADATA_KEY) {
6543 Repository::from_metadata(&slot)
6544 .map_err(|message| SourceError::Malformed { message })?
6545 } else {
6546 own_repository.clone().into_iter().collect()
6547 };
6548 let classification = Classification::from_metadata(&slot)
6549 .map_err(|message| SourceError::Malformed { message })?;
6550 let id = NativeId(content_id.to_owned());
6551 // Read only for a task, because only a task has either list: a project or a
6552 // document holding one of these keys holds nothing this source reports, and the
6553 // keys are left out of its caller-visible metadata all the same.
6554 let (delivers, delivered_by) = if kind == BoardKind::Work(ItemKind::Task) {
6555 let listed = |key: &str| {
6556 TaskRef::from_value(key, &id, Some(&self.name), slot.get(key))
6557 .map_err(|message| SourceError::Malformed { message })
6558 };
6559 (
6560 listed(TaskRef::DELIVERS_KEY)?,
6561 listed(TaskRef::DELIVERED_BY_KEY)?,
6562 )
6563 } else {
6564 (Vec::new(), Vec::new())
6565 };
6566 let (option, closed, reason) = Self::status_parts(nodes, content)?;
6567 let priority = self.held_priority(nodes)?;
6568 let item_labels = labels(content)?;
6569 // Present when the item was reached through its own issue, whose board entry
6570 // names the board; a read of the board's own items has the board already. An
6571 // empty id names nothing a field write could address, so it is read as absent and
6572 // the write goes back to reading the board.
6573 let board_id = item
6574 .pointer("/project/id")
6575 .and_then(Value::as_str)
6576 .filter(|id| !id.is_empty());
6577 let resolved = Resolved {
6578 item_id: required_str(item, "id")?.to_owned(),
6579 id,
6580 content_kind,
6581 kind,
6582 title,
6583 body: body.filter(|value| !value.is_empty()),
6584 raw_body,
6585 status: self
6586 .statuses
6587 .status(kind.status_kind(), option, closed, reason),
6588 option: option.map(str::to_owned),
6589 priority,
6590 closed,
6591 reason: closed.then(|| reason.map(str::to_owned)).flatten(),
6592 delivers,
6593 delivered_by,
6594 labels: item_labels.clone(),
6595 parent,
6596 origin: text_field(nodes, ORIGIN_FIELD)?.filter(|value| !value.is_empty()),
6597 projected: self.held_projections(nodes)?,
6598 number: match content_kind {
6599 ContentKind::Issue => Some(issue_number(content)?),
6600 // A draft is filed in no repository, so nothing ever numbered it:
6601 // `DraftIssue` declares no `number` at all, exactly as it declares no
6602 // `subIssuesSummary` the branch above reads.
6603 ContentKind::DraftIssue => None,
6604 },
6605 url: optional_str(content, "url")?.map(str::to_owned),
6606 created_at: optional_time(content, "createdAt")?,
6607 updated_at: optional_time(content, "updatedAt")?,
6608 item_updated_at: optional_time(item, "updatedAt")?,
6609 comment_count: content
6610 .pointer("/commentCount/totalCount")
6611 .and_then(Value::as_u64),
6612 relationship_events: content
6613 .pointer("/relationshipEvents/totalCount")
6614 .and_then(Value::as_u64),
6615 // The issue node is stamped wherever it was read; a board item a read of the issue
6616 // put together from its memberships is not.
6617 served_at: match optional_time(content, SERVED_AT)? {
6618 Some(served) => Some(served),
6619 None => optional_time(item, SERVED_AT)?,
6620 },
6621 // Both connections were read to their end above — one reporting a further page is
6622 // refused — so these are every field value and every label the item holds. A
6623 // response that did not select the labels at all says nothing of them, and gives
6624 // no facets.
6625 facets: content.get("labels").is_some().then(|| {
6626 change_token::Facets::new(kind.facet(), facet_fields(nodes), &item_labels).digest()
6627 }),
6628 own_repository,
6629 repositories,
6630 classification,
6631 slot,
6632 board_id: board_id.map(str::to_owned),
6633 fields: field_definitions(nodes),
6634 board_fields: Self::carried_board_fields(content, board_id)?,
6635 blocked_by: carried_blocked_by(content)?,
6636 };
6637 Ok(Some(resolved))
6638 }
6639
6640 /// The field definitions of the board `board_id` names — the project this issue's own
6641 /// board item is on — off the `boards` page a read of an issue by its own id carries, or
6642 /// `None` when the read carried none, carried no entry for that board, or the board item
6643 /// named no board, which a write then answers by reading the board's fields itself.
6644 ///
6645 /// Matched by the board's node id and never by its number alone: a project number is
6646 /// unique only within its owner, so another owner's board numbered alike can sit on the
6647 /// same page, and its field and option ids address nothing on this one.
6648 fn carried_board_fields(
6649 content: &Value,
6650 board_id: Option<&str>,
6651 ) -> Result<Option<Value>, SourceError> {
6652 let (Some(nodes), Some(board_id)) = (
6653 content.pointer("/boards/nodes").and_then(Value::as_array),
6654 board_id,
6655 ) else {
6656 return Ok(None);
6657 };
6658 let Some(board) = nodes.iter().find_map(|node| {
6659 let project = node.get("project")?;
6660 (project.get("id").and_then(Value::as_str) == Some(board_id)).then_some(project)
6661 }) else {
6662 return Ok(None);
6663 };
6664 let Some(fields) = board.get("fields").filter(|fields| !fields.is_null()) else {
6665 return Ok(None);
6666 };
6667 complete_connection(fields, "board fields", NESTED_PAGE_SIZE)?;
6668 Ok(Some(fields.clone()))
6669 }
6670
6671 /// What one board item's `Priority` field says, through this instance's mapping.
6672 ///
6673 /// An instance with no mapping holds no priority, so every item reads as `none` whatever
6674 /// its board holds. With one, no value is `none`, a mapped option is its level, and an
6675 /// option the mapping does not name is kept as itself — never read as a level or as
6676 /// `none` — for a read of the task to report by name.
6677 fn held_priority(&self, field_values: &[Value]) -> Result<HeldPriority, SourceError> {
6678 let Some(mapping) = &self.priorities else {
6679 return Ok(HeldPriority::Read(Priority::None));
6680 };
6681 // A value of the field that names no option — a text field someone called `Priority` —
6682 // is malformed rather than `none`: reading it as no priority would let the next copy
6683 // clear one a person set.
6684 let Some(option) = field_values
6685 .iter()
6686 .find(|value| {
6687 value.pointer("/field/name").and_then(Value::as_str) == Some(PRIORITY_FIELD)
6688 })
6689 .map(|value| required_str(value, "name"))
6690 .transpose()?
6691 else {
6692 return Ok(HeldPriority::Read(Priority::None));
6693 };
6694 Ok(mapping.priority_of(option).map_or_else(
6695 || HeldPriority::Unmapped(option.to_owned()),
6696 HeldPriority::Read,
6697 ))
6698 }
6699
6700 /// What each projected metadata field holds for one board item, by field name — every text
6701 /// value GitHub answers, an empty one included.
6702 fn held_projections(
6703 &self,
6704 field_values: &[Value],
6705 ) -> Result<BTreeMap<String, String>, SourceError> {
6706 let mut held = BTreeMap::new();
6707 for projection in &self.metadata_fields {
6708 if let Some(text) = text_field(field_values, &projection.field)? {
6709 held.insert(projection.field.to_string(), text);
6710 }
6711 }
6712 Ok(held)
6713 }
6714
6715 /// What one board item's status is read from: its `Status` option, whether its issue
6716 /// is closed, and the reason it was closed with. [`BoardStatuses::status`] turns the
6717 /// three into the status it reports.
6718 fn status_parts<'a>(
6719 field_values: &'a [Value],
6720 content: &'a Value,
6721 ) -> Result<(Option<&'a str>, bool, Option<&'a str>), SourceError> {
6722 let option = field_values
6723 .iter()
6724 .find(|value| value.pointer("/field/name").and_then(Value::as_str) == Some("Status"))
6725 .map(|value| required_str(value, "name"))
6726 .transpose()?;
6727 let closed = optional_str(content, "state")? == Some("CLOSED");
6728 Ok((option, closed, optional_str(content, "stateReason")?))
6729 }
6730
6731 /// The board Status option this write selects, or the refusal that says why not.
6732 ///
6733 /// The mapped option is required for both open and terminal targets. A terminal write
6734 /// validates it before changing either representation, so it can never fall back to
6735 /// closing an issue whose board cannot display the matching status.
6736 ///
6737 /// Answers the field's id, the option's id, and the option's name as the board spells
6738 /// it — which is the name a read of the item reports once it sits there.
6739 fn column_for(
6740 &self,
6741 fields: &Value,
6742 kind: ItemKind,
6743 category: StatusCategory,
6744 target: &StatusTarget,
6745 ) -> Result<Option<(String, String, String)>, SourceError> {
6746 let Some(wanted) = target.option() else {
6747 return Ok(None);
6748 };
6749 let missing = |detail: &str| SourceError::Refused {
6750 message: format!(
6751 "{} status {} of source {} needs the board Status option {wanted:?}, and \
6752 {detail}; next: add that option to the board, which `onetaskgraph sources \
6753 fields {} --apply` does, or point status_mapping.{}.{} of this source at one \
6754 it has",
6755 kind.marker(),
6756 category_name(category),
6757 self.name,
6758 self.name,
6759 category_name(category),
6760 kind.marker()
6761 ),
6762 };
6763 let Some(field) = Board::field(fields, "Status")? else {
6764 return Err(missing("this board has no Status field"));
6765 };
6766 if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
6767 return Err(missing(
6768 "this board's Status field is not a single-select field",
6769 ));
6770 }
6771 let option = field
6772 .get("options")
6773 .and_then(Value::as_array)
6774 .and_then(|options| {
6775 options.iter().find(|option| {
6776 option
6777 .get("name")
6778 .and_then(Value::as_str)
6779 .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
6780 })
6781 });
6782 match option {
6783 None => Err(missing("this board does not have it")),
6784 Some(option) => Ok(Some((
6785 required_str(field, "id")?.to_owned(),
6786 required_str(option, "id")?.to_owned(),
6787 required_str(option, "name")?.to_owned(),
6788 ))),
6789 }
6790 }
6791
6792 /// The refusal a status that closes an issue is answered with over a board draft.
6793 fn closes_a_draft(&self, category: StatusCategory) -> SourceError {
6794 SourceError::Refused {
6795 message: format!(
6796 "status {} of source {} closes the item's issue, and GitHub draft items have \
6797 no open or closed state",
6798 category_name(category),
6799 self.name
6800 ),
6801 }
6802 }
6803
6804 /// What a status write to one item needs of the board: the board's id and the
6805 /// definition of its `Status` field, read off the item when the item says both.
6806 ///
6807 /// The same reasoning as [`Self::fields_for`]: a node read of the item names its board,
6808 /// and its `Status` value carries that field's definition, options and all. An item that
6809 /// does not say — no board id, or no `Status` value to read the field off — takes them
6810 /// from [`Self::board_fields`], which reads no item.
6811 async fn status_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
6812 if let Some(board) = item.carried_board() {
6813 return Ok(board);
6814 }
6815 if item.defines("Status")
6816 && let Some(board_id) = item.named_board()
6817 {
6818 return Ok(BoardFields {
6819 id: board_id,
6820 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
6821 });
6822 }
6823 self.board_fields().await
6824 }
6825
6826 /// Set one task's or one project's status and nothing else; see
6827 /// [`TaskSource::set_task_status`] and [`TaskSource::set_project_status`].
6828 async fn set_status(
6829 &self,
6830 id: &NativeId,
6831 kind: ItemKind,
6832 category: StatusCategory,
6833 ) -> Result<Option<Status>, SourceError> {
6834 // Refused before anything is read, in the words a write of the same status is.
6835 let target = self.resolved_target(kind, category)?;
6836 // An item read by issue reference carries only what this write needs, so what it
6837 // becomes is never remembered for a later read to be answered from.
6838 let held = self.take_held(id)?;
6839 let by_reference = held.is_some();
6840 let bound = match held {
6841 Some(item) => Some(item),
6842 None => self.bound_item(id).await?,
6843 };
6844 let Some(mut item) = bound.filter(|item| item.kind == BoardKind::Work(kind)) else {
6845 return Ok(None);
6846 };
6847 let board = self.status_board(&item).await?;
6848 let (field, option, name) = self
6849 .column_for(&board.fields, kind, category, &target)?
6850 .ok_or_else(|| SourceError::Malformed {
6851 message: format!(
6852 "status {} of source {} names no board Status option",
6853 category_name(category),
6854 self.name
6855 ),
6856 })?;
6857 if item.status.category == category && item.option.as_deref() == Some(&name) {
6858 return Ok(Some(item.status));
6859 }
6860 match &target {
6861 StatusTarget::Terminal(_, reason) => {
6862 if item.content_kind == ContentKind::DraftIssue {
6863 return Err(self.closes_a_draft(category));
6864 }
6865 // The option first, so a refused selection leaves an open issue open rather
6866 // than closed behind a card that still reads open.
6867 self.set_item_field(
6868 board.id.as_str(),
6869 &item.item_id,
6870 &field,
6871 json!({"singleSelectOptionId": option}),
6872 )
6873 .await?;
6874 // An issue already closed in a way that reads as this category keeps its state
6875 // and its reason: only its card was out of line, and closing it again would
6876 // overwrite a reason — a duplicate's — that says more than this write does.
6877 let already = item.closed && item.status.category == category;
6878 if !already {
6879 self.update_content(
6880 ContentKind::Issue,
6881 &item.id,
6882 json!({"stateInput": state_input(Some(&target))}),
6883 )
6884 .await?;
6885 }
6886 let reason = if already {
6887 item.reason.clone()
6888 } else {
6889 Some(reason.reason().to_owned())
6890 };
6891 item.closed = true;
6892 item.status = self
6893 .statuses
6894 .status(kind, Some(&name), true, reason.as_deref());
6895 item.option = Some(name);
6896 item.reason = reason;
6897 }
6898 StatusTarget::Column(_) => {
6899 // An option is what an open item's status is, so a closed issue is reopened
6900 // first — sitting closed in the column, it would read back as closed. A draft has
6901 // no state to reopen.
6902 if item.content_kind == ContentKind::Issue && item.closed {
6903 self.update_content(
6904 ContentKind::Issue,
6905 &item.id,
6906 json!({"stateInput": state_input(Some(&target))}),
6907 )
6908 .await?;
6909 item.closed = false;
6910 }
6911 self.set_item_field(
6912 board.id.as_str(),
6913 &item.item_id,
6914 &field,
6915 json!({"singleSelectOptionId": option}),
6916 )
6917 .await?;
6918 item.status = self.statuses.status(kind, Some(&name), false, None);
6919 item.option = Some(name);
6920 }
6921 StatusTarget::Disabled(_) => {
6922 unreachable!("resolved_target refused a disabled status")
6923 }
6924 }
6925 let status = item.status.clone();
6926 if !by_reference {
6927 self.remember_written(item, false)?;
6928 }
6929 Ok(Some(status))
6930 }
6931
6932 /// Replace one task's `delivered_by` and nothing else; see
6933 /// [`TaskSource::set_delivered_by`].
6934 ///
6935 /// One update of the body, which differs from the body GitHub holds only inside the
6936 /// metadata slot — see [`with_slot`]. A body that would not change is not sent at all.
6937 async fn replace_delivered_by(
6938 &self,
6939 id: &NativeId,
6940 delivered_by: &[TaskRef],
6941 ) -> Result<Option<()>, SourceError> {
6942 let entries = TaskRef::listed(
6943 TaskRef::DELIVERED_BY_KEY,
6944 id,
6945 Some(&self.name),
6946 delivered_by.to_vec(),
6947 )
6948 .map_err(|message| SourceError::Refused { message })?;
6949 let Some(mut item) = self
6950 .bound_item(id)
6951 .await?
6952 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
6953 else {
6954 return Ok(None);
6955 };
6956 let mut slot = item.slot.clone();
6957 set_task_list(&mut slot, TaskRef::DELIVERED_BY_KEY, &entries);
6958 self.write_slot(&mut item, &slot).await?;
6959 item.delivered_by = entries;
6960 self.remember_written(item, false)?;
6961 Ok(Some(()))
6962 }
6963
6964 /// Set one caller key of the metadata slot of one issue of `kind`, and nothing else;
6965 /// see [`TaskSource::set_task_metadata`].
6966 ///
6967 /// `None` when this board holds no item by that id, or holds one of another kind. The
6968 /// answer is the item as this source now reads it, so what a caller is told the key
6969 /// holds is what the slot holds.
6970 ///
6971 /// A key already holding the value is answered without a write, compared as JSON rather
6972 /// than as the body's bytes: a slot a person spelled with other whitespace would
6973 /// otherwise be re-encoded, which is a write that changes nothing the caller asked for.
6974 async fn set_slot_key(
6975 &self,
6976 id: &NativeId,
6977 kind: BoardKind,
6978 key: &MetadataKey,
6979 value: &Value,
6980 ) -> Result<Option<Resolved>, SourceError> {
6981 let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
6982 return Ok(None);
6983 };
6984 if item.slot.get(key.as_str()) == Some(value) {
6985 return Ok(Some(item));
6986 }
6987 let mut slot = item.slot.clone();
6988 slot.insert(key.as_str().to_owned(), value.clone());
6989 // A key a board text field projects moves that field first, so the body — the value's
6990 // one authoritative home — is written last, as every write of an existing item is.
6991 if self
6992 .metadata_fields
6993 .iter()
6994 .any(|projection| &*projection.key == key.as_str())
6995 {
6996 let board = self.projection_board(&item).await?;
6997 let (writes, projected) = self.projection_writes(
6998 &board.fields,
6999 Some(&item.projected),
7000 &slot,
7001 Some(&[key.as_str()]),
7002 )?;
7003 let (mut values, mut clears) = (Vec::new(), Vec::new());
7004 for write in &writes {
7005 write.join(&mut values, &mut clears);
7006 }
7007 self.set_item_fields(board.id.as_str(), &item.item_id, &values, &clears)
7008 .await?;
7009 item.projected = projected;
7010 }
7011 self.write_slot(&mut item, &slot).await?;
7012 self.remember_written(item.clone(), false)?;
7013 Ok(Some(item))
7014 }
7015
7016 /// What a projection write to `item` needs of the board, read off the item when it says
7017 /// enough, as [`Self::fields_for`] reads it.
7018 async fn projection_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
7019 if let Some(board) = item.carried_board() {
7020 return Ok(board);
7021 }
7022 if let Some(board_id) = item.named_board()
7023 && self
7024 .metadata_fields
7025 .iter()
7026 .all(|projection| item.defines(&projection.field))
7027 {
7028 return Ok(BoardFields {
7029 id: board_id,
7030 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
7031 });
7032 }
7033 self.board_fields().await
7034 }
7035
7036 /// Put `slot` in one item's metadata slot with a single update of its body, and bring
7037 /// `item` up to what that write left.
7038 ///
7039 /// The body sent differs from the body GitHub holds only inside the slot — see
7040 /// [`with_slot`] — and a body that would not change is not sent at all. It goes through
7041 /// the mutation the item's content takes, so a board draft's body is written with
7042 /// `updateProjectV2DraftIssue` exactly as an issue's is with `updateIssue`.
7043 async fn write_slot(
7044 &self,
7045 item: &mut Resolved,
7046 slot: &BTreeMap<String, Value>,
7047 ) -> Result<(), SourceError> {
7048 let held = item.raw_body.clone().unwrap_or_default();
7049 let body = with_slot(&held, slot)?;
7050 if body != held {
7051 self.update_content(item.content_kind, &item.id, json!({"body": body}))
7052 .await?;
7053 }
7054 let (visible, slot) = metadata_body(Some(body.clone()))?;
7055 item.body = visible.filter(|value| !value.is_empty());
7056 item.raw_body = Some(body);
7057 item.slot = slot;
7058 Ok(())
7059 }
7060
7061 /// This instance's target for a category written to an item of `kind`, refusing one
7062 /// that kind has no option for — before anything is read or written.
7063 ///
7064 /// Nothing here mutates the board's option set to make room for a status. GitHub
7065 /// documents `UpdateProjectV2FieldInput.singleSelectOptions` as *"provided values
7066 /// overwrite existing options"*, so no addition is additive and a mistake destroys the
7067 /// field and every item's status.
7068 fn resolved_target(
7069 &self,
7070 kind: ItemKind,
7071 category: StatusCategory,
7072 ) -> Result<StatusTarget, SourceError> {
7073 let target = self.statuses.target(kind, category).clone();
7074 let StatusTarget::Disabled(why) = target else {
7075 return Ok(target);
7076 };
7077 let refusal = why.refusal(&self.name, category, kind);
7078 // Why there is no shipped default, which is the question a person meeting this
7079 // refusal on a source that never mentioned the category asks.
7080 let shipped_none = match category {
7081 StatusCategory::Draft => Some(
7082 "draft has no shipped default because GitHub draft issues cannot have \
7083 sub-issues, and this source stores a project's tasks as its issue's sub-issues",
7084 ),
7085 StatusCategory::Unknown => Some(
7086 "unknown has no shipped default because this board keeps no open-ended status \
7087 word: every word classified unknown is written to the one board Status option \
7088 status_mapping.unknown names",
7089 ),
7090 _ => None,
7091 };
7092 Err(match (refusal, shipped_none, why) {
7093 (SourceError::Refused { message }, Some(note), UnmappedStatus::Unconfigured) => {
7094 SourceError::Refused {
7095 message: format!("{message}; {note}"),
7096 }
7097 }
7098 (refusal, _, _) => refusal,
7099 })
7100 }
7101
7102 /// What writing `priority` does to one item's `Priority` field on this board, or the
7103 /// refusal naming what the board lacks.
7104 ///
7105 /// `none` is no value, so it clears the field — and asks nothing of an item that holds
7106 /// none already, or of an item not created yet. Every other priority selects the option
7107 /// the mapping names, matched case-insensitively; a board with no `Priority` field, or
7108 /// without that option, is refused rather than given one: reads and writes never create
7109 /// a field or an option.
7110 fn priority_write(
7111 &self,
7112 fields: &Value,
7113 existing: Option<&Resolved>,
7114 priority: Priority,
7115 ) -> Result<Option<PriorityWrite>, SourceError> {
7116 let Some(mapping) = &self.priorities else {
7117 return Err(self.holds_no_priority());
7118 };
7119 let Some(wanted) = mapping.option(priority) else {
7120 if !existing.is_some_and(Resolved::holds_priority) {
7121 return Ok(None);
7122 }
7123 let field =
7124 Board::field(fields, PRIORITY_FIELD)?.ok_or_else(|| SourceError::Malformed {
7125 message: format!(
7126 "an item holding a {PRIORITY_FIELD} value was read without that field"
7127 ),
7128 })?;
7129 return Ok(Some(PriorityWrite::Clear {
7130 field: required_str(field, "id")?.to_owned(),
7131 }));
7132 };
7133 let missing = |detail: &str| SourceError::Refused {
7134 message: format!(
7135 "priority {priority} of source {} needs the board {PRIORITY_FIELD} option \
7136 {wanted:?}, and {detail}; run `onetaskgraph sources fields {} --apply` to add \
7137 it, or point priority_mapping.{priority} of this source at an option the board \
7138 has",
7139 self.name, self.name
7140 ),
7141 };
7142 let Some(field) = Board::field(fields, PRIORITY_FIELD)? else {
7143 return Err(missing(&format!(
7144 "this board has no {PRIORITY_FIELD} field"
7145 )));
7146 };
7147 if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
7148 return Err(missing(&format!(
7149 "this board's {PRIORITY_FIELD} field is not a single-select field"
7150 )));
7151 }
7152 // An options list that is absent or not a list is an answer this source cannot read,
7153 // not a board lacking the option: `sources fields --apply` is no remedy for it.
7154 let option = field
7155 .get("options")
7156 .and_then(Value::as_array)
7157 .ok_or_else(|| SourceError::Malformed {
7158 message: format!("GitHub {PRIORITY_FIELD} field options is not an array"),
7159 })?
7160 .iter()
7161 .find(|option| {
7162 option
7163 .get("name")
7164 .and_then(Value::as_str)
7165 .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
7166 })
7167 .ok_or_else(|| missing("this board does not have it"))?;
7168 Ok(Some(PriorityWrite::Select {
7169 field: required_str(field, "id")?.to_owned(),
7170 option: required_str(option, "id")?.to_owned(),
7171 }))
7172 }
7173
7174 /// What writing `metadata` does to each projected text field of one item, and what those
7175 /// fields hold after it — or the refusal, before anything is sent.
7176 ///
7177 /// `held` is what the item's fields hold now, `None` for an item not created yet. `keys`
7178 /// narrows the work to the entries projecting one of those metadata keys, for a write that
7179 /// touches only them; `None` is every entry. A string the field already holds, and nothing
7180 /// for a field holding nothing, sends nothing. A value no text field can hold is refused,
7181 /// and so is a board without the field, or with a field of that name that is not a text
7182 /// field: reads and writes never create a field.
7183 fn projection_writes(
7184 &self,
7185 fields: &Value,
7186 held: Option<&BTreeMap<String, String>>,
7187 metadata: &BTreeMap<String, Value>,
7188 keys: Option<&[&str]>,
7189 ) -> Result<(Vec<ProjectionWrite>, BTreeMap<String, String>), SourceError> {
7190 let mut projected = held.cloned().unwrap_or_default();
7191 let mut writes = Vec::new();
7192 for projection in self
7193 .metadata_fields
7194 .iter()
7195 .filter(|projection| keys.is_none_or(|keys| keys.contains(&&*projection.key)))
7196 {
7197 let wanted = projection.projected(metadata, &self.name)?;
7198 let field = self.projection_field(fields, projection)?;
7199 let holding = projected.get(&*projection.field);
7200 match wanted {
7201 Projected::Text(text) if holding != Some(&text) => {
7202 writes.push(ProjectionWrite::Set {
7203 field,
7204 text: text.clone(),
7205 });
7206 projected.insert(projection.field.to_string(), text);
7207 }
7208 Projected::Nothing if holding.is_some() => {
7209 writes.push(ProjectionWrite::Clear { field });
7210 projected.remove(&*projection.field);
7211 }
7212 Projected::Text(_) | Projected::Nothing => {}
7213 }
7214 }
7215 Ok((writes, projected))
7216 }
7217
7218 /// The id of the board text field one projection writes, or the refusal naming what the
7219 /// board has instead.
7220 fn projection_field(
7221 &self,
7222 fields: &Value,
7223 projection: &MetadataField,
7224 ) -> Result<String, SourceError> {
7225 let refuse = |detail: String| SourceError::Refused {
7226 message: format!(
7227 "source {} projects metadata key {:?} onto the board text field {:?}, and \
7228 {detail}; next: run `onetaskgraph sources fields {} --apply` to create it as a \
7229 text field, or name another field in metadata_fields",
7230 self.name, &*projection.key, &*projection.field, self.name
7231 ),
7232 };
7233 let Some(field) = Board::field(fields, &projection.field)? else {
7234 return Err(refuse("this board has no field of that name".to_owned()));
7235 };
7236 let typename = optional_str(field, "__typename")?.unwrap_or("field of another type");
7237 let data_type = optional_str(field, "dataType")?;
7238 if typename != "ProjectV2Field" || data_type != Some("TEXT") {
7239 return Err(refuse(format!(
7240 "this board's field of that name is not a text field (it is a {}), so it is \
7241 left as it is; rename or remove that field first",
7242 data_type.unwrap_or(typename)
7243 )));
7244 }
7245 Ok(required_str(field, "id")?.to_owned())
7246 }
7247
7248 /// Apply one priority write to one board item.
7249 async fn write_priority(
7250 &self,
7251 board_id: &str,
7252 item_id: &str,
7253 write: &PriorityWrite,
7254 ) -> Result<(), SourceError> {
7255 match write {
7256 PriorityWrite::Select { field, option } => {
7257 self.set_item_field(
7258 board_id,
7259 item_id,
7260 field,
7261 json!({"singleSelectOptionId": option}),
7262 )
7263 .await
7264 }
7265 PriorityWrite::Clear { field } => {
7266 let data = self
7267 .graphql(
7268 graphql::CLEAR_FIELD,
7269 json!({"input":{"projectId":board_id,"itemId":item_id,"fieldId":field},
7270 "readPriority":false,"priorityName":PRIORITY_FIELD}),
7271 )
7272 .await?;
7273 let returned = data
7274 .pointer("/clearProjectV2ItemFieldValue/projectV2Item")
7275 .ok_or_else(|| SourceError::Malformed {
7276 message: "GitHub field clear returned no project item".into(),
7277 })?;
7278 if required_str(returned, "id")? != item_id {
7279 return Err(SourceError::Malformed {
7280 message: "GitHub field clear returned the wrong project item".into(),
7281 });
7282 }
7283 Ok(())
7284 }
7285 }
7286 }
7287
7288 /// The refusal a priority is answered with by an instance configured with no
7289 /// `priority_mapping`, which holds none.
7290 fn holds_no_priority(&self) -> SourceError {
7291 SourceError::Refused {
7292 message: format!(
7293 "source {} holds no task priority: its configuration sets no priority_mapping; \
7294 next: set priority_mapping on this source, then run `onetaskgraph sources \
7295 fields {} --apply` to set its board up",
7296 self.name, self.name
7297 ),
7298 }
7299 }
7300
7301 /// Set one task's priority and nothing else; see [`TaskSource::set_task_priority`].
7302 ///
7303 /// One field write — a select, or a clear for `none` — and no title, body, label, state
7304 /// or `Status` request. Clearing a priority an item does not hold sends nothing.
7305 async fn set_priority(
7306 &self,
7307 id: &NativeId,
7308 priority: Priority,
7309 ) -> Result<Option<Priority>, SourceError> {
7310 if self.priorities.is_none() {
7311 return Err(self.holds_no_priority());
7312 }
7313 let Some(mut item) = self
7314 .bound_item(id)
7315 .await?
7316 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
7317 else {
7318 return Ok(None);
7319 };
7320 if priority == Priority::None && !item.holds_priority() {
7321 return Ok(Some(priority));
7322 }
7323 // The item's own read carries the field's definition whenever it holds a value of
7324 // it, which a clear always does; a select onto an item holding none reads the board.
7325 let board = match (item.carried_board(), item.named_board()) {
7326 (Some(board), _) => board,
7327 (None, Some(id)) if item.defines(PRIORITY_FIELD) => BoardFields {
7328 id,
7329 fields: json!({"nodes": item.fields.clone(), "pageInfo": {"hasNextPage": false}}),
7330 },
7331 _ => self.board_fields().await?,
7332 };
7333 let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? else {
7334 return Ok(Some(priority));
7335 };
7336 let (document, root, input) = match write {
7337 PriorityWrite::Select { field, option } => (
7338 graphql::UPDATE_FIELD,
7339 "updateProjectV2ItemFieldValue",
7340 json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field,"value":{"singleSelectOptionId":option}}),
7341 ),
7342 PriorityWrite::Clear { field } => (
7343 graphql::CLEAR_FIELD,
7344 "clearProjectV2ItemFieldValue",
7345 json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field}),
7346 ),
7347 };
7348 let data = self
7349 .graphql(
7350 document,
7351 json!({"input":input,"readPriority":true,"priorityName":PRIORITY_FIELD}),
7352 )
7353 .await?;
7354 let returned = data
7355 .get(root)
7356 .and_then(|value| value.get("projectV2Item"))
7357 .ok_or_else(|| SourceError::Malformed {
7358 message: "GitHub priority write returned no project item".into(),
7359 })?;
7360 if required_str(returned, "id")? != item.item_id {
7361 return Err(SourceError::Malformed {
7362 message: "GitHub priority write returned the wrong project item".into(),
7363 });
7364 }
7365 let value = returned
7366 .get("fieldValueByName")
7367 .ok_or_else(|| SourceError::Malformed {
7368 message: "GitHub priority write returned no priority read-back".into(),
7369 })?;
7370 if !value.is_null()
7371 && value.pointer("/field/name").and_then(Value::as_str) != Some(PRIORITY_FIELD)
7372 {
7373 return Err(SourceError::Malformed {
7374 message: "GitHub priority read-back is not a Priority field value".into(),
7375 });
7376 }
7377 let values = if value.is_null() {
7378 Vec::new()
7379 } else {
7380 vec![value.clone()]
7381 };
7382 item.priority = self.held_priority(&values)?;
7383 let answer = item.task()?.priority;
7384 self.remember_written(item, false)?;
7385 Ok(Some(answer))
7386 }
7387
7388 /// Replace one task's visible body and nothing else; see
7389 /// [`TaskSource::set_task_content`].
7390 ///
7391 /// One update of the body, which differs from the body GitHub holds only outside the
7392 /// metadata slot — the slot is kept byte for byte, so every caller key and every list
7393 /// this source keeps there reads back as it was. A body that would not change is not
7394 /// sent at all.
7395 async fn replace_content(
7396 &self,
7397 id: &NativeId,
7398 content: &str,
7399 ) -> Result<Option<()>, SourceError> {
7400 let Some(mut item) = self
7401 .bound_item(id)
7402 .await?
7403 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
7404 else {
7405 return Ok(None);
7406 };
7407 let held = item.raw_body.clone().unwrap_or_default();
7408 let body = with_content(&held, content)?;
7409 // Checked before anything is sent: content ending in what this source reads as its own
7410 // metadata slot would read back as metadata rather than as the content it was.
7411 let (visible, slot) = metadata_body(Some(body.clone()))?;
7412 if visible.as_deref().unwrap_or_default() != content || slot != item.slot {
7413 return Err(SourceError::Refused {
7414 message: format!(
7415 "this content ends in what source {} reads as its own metadata slot \
7416 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
7417 as content; next: remove that trailing block from the content",
7418 self.name
7419 ),
7420 });
7421 }
7422 if body != held {
7423 self.update_content(item.content_kind, &item.id, json!({"body": body}))
7424 .await?;
7425 }
7426 item.body = visible.filter(|value| !value.is_empty());
7427 item.raw_body = Some(body);
7428 item.slot = slot;
7429 self.remember_written(item, false)?;
7430 Ok(Some(()))
7431 }
7432
7433 /// Apply one targeted update to one task; see [`TaskSource::update_task`].
7434 ///
7435 /// One read of the item — which carries the board's field definitions and the issue's
7436 /// `blockedBy`, so neither is read again — and then only what differs from it: the
7437 /// `Status` option and the `Priority` field together in one request, the `blockedBy`
7438 /// additions and removals the named edges differ by, and last one `updateIssue` carrying
7439 /// the title, the body — visible content and metadata slot together — and a state change.
7440 /// So an update naming any of title, body, metadata, status and priority is one read and
7441 /// at most two writes. The body goes last so that a write refused part-way leaves it, and
7442 /// the metadata in it, as it stood. A terminal status selects its option and then closes,
7443 /// as a whole write does; an open one selects its option and then reopens. The origin
7444 /// field is never written: an update is of an item that already exists, whose origin is
7445 /// what it is.
7446 ///
7447 /// The task answered is the item as those writes left it, built from the read and what was
7448 /// sent rather than read again — the same record a later read in this run answers from.
7449 async fn targeted_update(
7450 &self,
7451 id: &NativeId,
7452 update: &TaskUpdate,
7453 ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
7454 // Everything this source can refuse without reading the item is refused first, in the
7455 // words a whole write of the same fields is refused with.
7456 update.consistent()?;
7457 if update
7458 .title
7459 .as_deref()
7460 .is_some_and(|title| title.starts_with(DESIGN_TITLE_PREFIX))
7461 {
7462 return Err(SourceError::Refused {
7463 message: format!(
7464 "the title of this task begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
7465 spells a document, so it would read back as one rather than as a task; \
7466 retitle it",
7467 self.name
7468 ),
7469 });
7470 }
7471 if let Some(delivers) = &update.delivers {
7472 TaskRef::listed(
7473 TaskRef::DELIVERS_KEY,
7474 id,
7475 Some(&self.name),
7476 delivers.clone(),
7477 )
7478 .map_err(|message| SourceError::Refused { message })?;
7479 }
7480 if self.priorities.is_none()
7481 && update
7482 .priority
7483 .is_some_and(|priority| priority != Priority::None)
7484 {
7485 return Err(self.holds_no_priority());
7486 }
7487 let target = update
7488 .status
7489 .as_ref()
7490 .map(|status| self.resolved_target(ItemKind::Task, status.category))
7491 .transpose()?;
7492 let Some(mut item) = self
7493 .bound_item(id)
7494 .await?
7495 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
7496 else {
7497 return Ok(None);
7498 };
7499 let before = item.task()?;
7500
7501 let mut status_move = None;
7502 if let (Some(status), Some(target)) = (&update.status, target) {
7503 let board = self.status_board(&item).await?;
7504 let (field, option, name) = self
7505 .column_for(&board.fields, ItemKind::Task, status.category, &target)?
7506 .ok_or_else(|| SourceError::Malformed {
7507 message: format!(
7508 "status {} of source {} names no board Status option",
7509 category_name(status.category),
7510 self.name
7511 ),
7512 })?;
7513 let terminal = matches!(target, StatusTarget::Terminal(_, _));
7514 if terminal && item.content_kind == ContentKind::DraftIssue {
7515 return Err(self.closes_a_draft(status.category));
7516 }
7517 let landed = match &target {
7518 StatusTarget::Terminal(_, reason) => {
7519 self.statuses
7520 .status(ItemKind::Task, Some(&name), true, Some(reason.reason()))
7521 }
7522 _ => self
7523 .statuses
7524 .status(ItemKind::Task, Some(&name), false, None),
7525 };
7526 let option_moves = item
7527 .option
7528 .as_deref()
7529 .is_none_or(|held| !held.eq_ignore_ascii_case(&name));
7530 let state_moves = item.content_kind == ContentKind::Issue
7531 && (item.closed != terminal || (terminal && item.status != landed));
7532 if let Some(moves) = Moves::of(option_moves, state_moves) {
7533 status_move = Some(StatusMove {
7534 board: board.id,
7535 field,
7536 option,
7537 name,
7538 target,
7539 landed,
7540 moves,
7541 });
7542 }
7543 }
7544
7545 let mut priority_move = None;
7546 if let Some(priority) = update.priority
7547 && self.priorities.is_some()
7548 && item.priority != HeldPriority::Read(priority)
7549 {
7550 let board = match (item.carried_board(), item.named_board()) {
7551 (Some(board), _) => board,
7552 (None, Some(board)) if item.defines(PRIORITY_FIELD) => BoardFields {
7553 id: board,
7554 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
7555 },
7556 _ => self.board_fields().await?,
7557 };
7558 if let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? {
7559 priority_move = Some((board.id, write, priority));
7560 }
7561 }
7562
7563 // The metadata keys this update names that a board text field projects, and what
7564 // that does to those fields — refused, as a whole write refuses it, before anything
7565 // is sent.
7566 let touched: Vec<&str> = update
7567 .metadata_set
7568 .keys()
7569 .chain(update.metadata_remove.iter())
7570 .map(MetadataKey::as_str)
7571 .collect();
7572 let mut projection_move = None;
7573 if self
7574 .metadata_fields
7575 .iter()
7576 .any(|projection| touched.contains(&&*projection.key))
7577 {
7578 let mut metadata = item.slot.clone();
7579 for (key, value) in &update.metadata_set {
7580 metadata.insert(key.as_str().to_owned(), value.clone());
7581 }
7582 for key in &update.metadata_remove {
7583 metadata.remove(key.as_str());
7584 }
7585 let board = self.projection_board(&item).await?;
7586 let (writes, projected) = self.projection_writes(
7587 &board.fields,
7588 Some(&item.projected),
7589 &metadata,
7590 Some(&touched),
7591 )?;
7592 projection_move = Some((board.id, writes, projected));
7593 }
7594
7595 // Resolved before the body is composed, because a far end `blockedBy` cannot name is
7596 // recorded in the slot, and the slot travels in the one body update below.
7597 let edges = match &update.depends_on {
7598 Some(edges) => Some(
7599 self.partition_edges(
7600 BoardKind::Work(ItemKind::Task),
7601 item.content_kind,
7602 item.blocked_by.as_deref(),
7603 edges,
7604 )
7605 .await?,
7606 ),
7607 None => None,
7608 };
7609
7610 let mut slot = item.slot.clone();
7611 for (key, value) in &update.metadata_set {
7612 slot.insert(key.as_str().to_owned(), value.clone());
7613 }
7614 for key in &update.metadata_remove {
7615 slot.remove(key.as_str());
7616 }
7617 if let Some(delivers) = &update.delivers {
7618 set_task_list(&mut slot, TaskRef::DELIVERS_KEY, delivers);
7619 }
7620 if let Some((_, recorded)) = &edges {
7621 record_edges(&mut slot, recorded);
7622 }
7623 let held = item.raw_body.clone().unwrap_or_default();
7624 let content = match &update.content {
7625 Some(content) => with_content(&held, content)?,
7626 None => held.clone(),
7627 };
7628 // A slot holding what it held is kept byte for byte, compared as JSON rather than as
7629 // the body's bytes, as a metadata write compares it: a slot a person spelled with
7630 // other whitespace would otherwise be re-encoded, which is a write nobody asked for.
7631 let body = if slot == item.slot {
7632 content
7633 } else {
7634 with_slot(&content, &slot)?
7635 };
7636 // Checked before anything is sent, as a content write checks it: content ending in
7637 // what this source reads as its own slot would read back as metadata.
7638 let (visible, read) = metadata_body(Some(body.clone()))?;
7639 let wanted = update.content.as_deref().or(item.body.as_deref());
7640 if visible.as_deref().unwrap_or_default() != wanted.unwrap_or_default() || read != slot {
7641 return Err(SourceError::Refused {
7642 message: format!(
7643 "this content ends in what source {} reads as its own metadata slot \
7644 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
7645 as content; next: remove that trailing block from the content",
7646 self.name
7647 ),
7648 });
7649 }
7650 let recorded_moves =
7651 slot.get(DependencyEdge::RECORDED_KEY) != item.slot.get(DependencyEdge::RECORDED_KEY);
7652
7653 // One `updateIssue` carries all three, because every mutation spends the secondary
7654 // limiter and the title, body and state are one mutation's inputs.
7655 let mut fields = serde_json::Map::new();
7656 if let Some(title) = update.title.as_ref().filter(|title| **title != item.title) {
7657 fields.insert("title".to_owned(), json!(title));
7658 }
7659 if body != held {
7660 fields.insert("body".to_owned(), json!(body));
7661 }
7662 if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.state()) {
7663 fields.insert("stateInput".to_owned(), state_input(Some(&moving.target)));
7664 }
7665 // **The body is written last, and that is the guarantee a refusal part-way keeps.**
7666 // GitHub runs no two requests as one, and runs one document's mutation fields in order
7667 // without undoing an earlier field when a later one fails — so a body written before a
7668 // board field the board then refused would be left changed. Written after every other
7669 // write has landed, a refusal anywhere leaves the item's body, and every metadata key
7670 // it carries, exactly as they stood. So the `Status` option and the `Priority` field go
7671 // first, together in one request — a terminal option selected before the issue
7672 // closes, as a whole write does — then the `blockedBy` difference, then the body.
7673 let mut board_writes: Vec<(&BoardId, (String, Value))> = Vec::new();
7674 let mut clears: Vec<(&BoardId, &str)> = Vec::new();
7675 if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.option()) {
7676 board_writes.push((
7677 &moving.board,
7678 (
7679 moving.field.clone(),
7680 json!({"singleSelectOptionId": moving.option}),
7681 ),
7682 ));
7683 }
7684 match &priority_move {
7685 Some((board, PriorityWrite::Select { field, option }, _)) => board_writes.push((
7686 board,
7687 (field.clone(), json!({"singleSelectOptionId": option})),
7688 )),
7689 Some((board, PriorityWrite::Clear { field }, _)) => clears.push((board, field)),
7690 None => {}
7691 }
7692 if let Some((board, writes, _)) = &projection_move {
7693 for write in writes {
7694 let (mut values, mut cleared) = (Vec::new(), Vec::new());
7695 write.join(&mut values, &mut cleared);
7696 board_writes.extend(values.into_iter().map(|value| (board, value)));
7697 clears.extend(cleared.into_iter().map(|field| (board, field)));
7698 }
7699 }
7700 let mut boards: Vec<&BoardId> = board_writes.iter().map(|(board, _)| *board).collect();
7701 boards.extend(clears.iter().map(|(board, _)| *board));
7702 boards.dedup_by(|one, other| one.as_str() == other.as_str());
7703 for board in boards {
7704 let writes = board_writes
7705 .iter()
7706 .filter(|(on, _)| on.as_str() == board.as_str())
7707 .map(|(_, write)| write.clone())
7708 .collect::<Vec<_>>();
7709 let cleared = clears
7710 .iter()
7711 .filter(|(on, _)| on.as_str() == board.as_str())
7712 .map(|(_, field)| *field)
7713 .collect::<Vec<_>>();
7714 self.set_item_fields(board.as_str(), &item.item_id, &writes, &cleared)
7715 .await?;
7716 }
7717 let mut blocked_by_moved = false;
7718 if let Some((native, _)) = &edges
7719 && item.content_kind == ContentKind::Issue
7720 {
7721 blocked_by_moved = self
7722 .reconcile_blocked_by(
7723 &item.id,
7724 native,
7725 Issue::Existing(item.blocked_by.as_deref()),
7726 )
7727 .await?;
7728 }
7729 if !fields.is_empty() {
7730 self.update_content(item.content_kind, &item.id, Value::Object(fields))
7731 .await?;
7732 }
7733
7734 // The `blockedBy` this item was read with is what the write just changed, so it is no
7735 // answer any more: a later write in this command would reconcile against it — re-adding
7736 // what it kept and never removing what it dropped — and a forward-edge read would
7737 // answer with it. Left unknown, each asks GitHub instead.
7738 if blocked_by_moved {
7739 item.blocked_by = None;
7740 }
7741 if let Some(title) = &update.title {
7742 item.title.clone_from(title);
7743 }
7744 item.body = visible.filter(|value| !value.is_empty());
7745 item.raw_body = (!body.is_empty() || item.raw_body.is_some()).then_some(body);
7746 item.slot = slot;
7747 if let Some(delivers) = &update.delivers {
7748 item.delivers.clone_from(delivers);
7749 }
7750 if let Some(moving) = status_move {
7751 item.closed = matches!(moving.target, StatusTarget::Terminal(_, _))
7752 && item.content_kind == ContentKind::Issue;
7753 item.status = moving.landed;
7754 item.option = Some(moving.name);
7755 }
7756 if let Some((_, _, priority)) = priority_move {
7757 item.priority = HeldPriority::Read(priority);
7758 }
7759 if let Some((_, _, projected)) = projection_move {
7760 item.projected = projected;
7761 }
7762 let task = item.task()?;
7763 let mut written = update.changed(&before, &task);
7764 if blocked_by_moved || recorded_moves {
7765 written.insert(UpdatedField::DependsOn);
7766 }
7767 self.remember_written(item, false)?;
7768 Ok(Some(TaskUpdateOutcome {
7769 task,
7770 written,
7771 delivers_before: before.delivers,
7772 }))
7773 }
7774
7775 /// Replace one issue's visible body and its [`MetadataKey::TEMPLATE_KEY`] slot entry
7776 /// together, and nothing else; see [`TaskSource::set_task_rendering`].
7777 ///
7778 /// One update of the body: the content outside the slot, and inside it that one entry,
7779 /// every other entry kept as it was. This source keeps no template answers — an issue has
7780 /// no room beside itself that is not its body, and answers written there would duplicate
7781 /// what the content already says and count against GitHub's body limit — so `answers`
7782 /// reaches nothing here. A body that would not change is not sent at all.
7783 async fn replace_rendering(
7784 &self,
7785 id: &NativeId,
7786 kind: BoardKind,
7787 content: &str,
7788 provenance: &Value,
7789 assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
7790 ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
7791 let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
7792 return Ok(None);
7793 };
7794 let held = item.raw_body.clone().unwrap_or_default();
7795 let mut slot = item.slot.clone();
7796 slot.insert(MetadataKey::TEMPLATE_KEY.to_owned(), provenance.clone());
7797 let rewritten;
7798 let content = if let Some(assets) = assets {
7799 let uploads = self
7800 .upload_assets(item.own_repository.as_ref(), assets)
7801 .await?;
7802 rewritten =
7803 onetaskgraph_plugin_api::serve_asset_references(content, &mut slot, &uploads);
7804 rewritten.as_str()
7805 } else {
7806 content
7807 };
7808 let body = with_slot(&with_content(&held, content)?, &slot)?;
7809 // Checked before anything is sent, as a content write checks it.
7810 let (visible, read) = metadata_body(Some(body.clone()))?;
7811 if visible.as_deref().unwrap_or_default() != content || read != slot {
7812 return Err(SourceError::Refused {
7813 message: format!(
7814 "this content ends in what source {} reads as its own metadata slot \
7815 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
7816 as content; next: remove that trailing block from the template",
7817 self.name
7818 ),
7819 });
7820 }
7821 if body != held {
7822 self.update_content(item.content_kind, &item.id, json!({"body": body}))
7823 .await?;
7824 }
7825 item.body = visible.filter(|value| !value.is_empty());
7826 item.raw_body = Some(body);
7827 item.slot = read;
7828 self.remember_written(item, false)?;
7829 Ok(Some(onetaskgraph_plugin_api::AssetsWritten {
7830 id: id.clone(),
7831 content: Some(content.to_owned()),
7832 }))
7833 }
7834
7835 async fn set_item_field(
7836 &self,
7837 board_id: &str,
7838 item_id: &str,
7839 field_id: &str,
7840 value: Value,
7841 ) -> Result<(), SourceError> {
7842 let data = self
7843 .graphql(
7844 graphql::UPDATE_FIELD,
7845 json!({"input":{
7846 "projectId":board_id,"itemId":item_id,"fieldId":field_id,"value":value
7847 },"readPriority":false,"priorityName":PRIORITY_FIELD}),
7848 )
7849 .await?;
7850 let returned = data
7851 .pointer("/updateProjectV2ItemFieldValue/projectV2Item")
7852 .ok_or_else(|| SourceError::Malformed {
7853 message: "GitHub field update returned no project item".into(),
7854 })?;
7855 if required_str(returned, "id")? != item_id {
7856 return Err(SourceError::Malformed {
7857 message: "GitHub field update returned the wrong project item".into(),
7858 });
7859 }
7860 Ok(())
7861 }
7862
7863 /// GitHub accepts one value per field mutation; aliases combine those mutations in
7864 /// one request — writes in the order given, then clears. A lone write is
7865 /// [`graphql::UPDATE_FIELD`] and a lone clear [`graphql::CLEAR_FIELD`]; more than one
7866 /// request's slots hold go in further requests of the same document, in order. Every
7867 /// returned item id is checked, including optional aliases.
7868 async fn set_item_fields(
7869 &self,
7870 board: &str,
7871 item: &str,
7872 fields: &[(String, Value)],
7873 clears: &[&str],
7874 ) -> Result<(), SourceError> {
7875 match (fields, clears) {
7876 ([], []) => return Ok(()),
7877 ([(field, value)], []) => {
7878 return self.set_item_field(board, item, field, value.clone()).await;
7879 }
7880 ([], [field]) => {
7881 return self
7882 .write_priority(
7883 board,
7884 item,
7885 &PriorityWrite::Clear {
7886 field: (*field).to_owned(),
7887 },
7888 )
7889 .await;
7890 }
7891 _ => {}
7892 }
7893 // Any field of the request stands in for an input a slot leaves out, which GitHub
7894 // still requires to be well formed although it never runs it.
7895 let filler = fields
7896 .first()
7897 .map_or_else(|| clears[0], |(field, _)| field.as_str());
7898 let mut writes = fields.iter();
7899 let mut cleared = clears.iter();
7900 loop {
7901 let batch: Vec<&(String, Value)> =
7902 writes.by_ref().take(FIELD_WRITE_SLOTS.len()).collect();
7903 let batch_clears: Vec<&&str> = cleared.by_ref().take(FIELD_CLEAR_SLOTS.len()).collect();
7904 if batch.is_empty() && batch_clears.is_empty() {
7905 return Ok(());
7906 }
7907 let mut variables = serde_json::Map::new();
7908 for (index, slot) in FIELD_WRITE_SLOTS.iter().enumerate() {
7909 let input = match batch.get(index) {
7910 Some((field, value)) => {
7911 json!({"projectId":board,"itemId":item,"fieldId":field,"value":value})
7912 }
7913 None => json!({"projectId":board,"itemId":item,"fieldId":filler,
7914 "value":{"text":""}}),
7915 };
7916 variables.insert(slot.variable.to_owned(), input);
7917 variables.insert(slot.include.to_owned(), json!(index < batch.len()));
7918 }
7919 for (index, slot) in FIELD_CLEAR_SLOTS.iter().enumerate() {
7920 let field = batch_clears.get(index).map_or(filler, |field| **field);
7921 variables.insert(
7922 slot.variable.to_owned(),
7923 json!({"projectId":board,"itemId":item,"fieldId":field}),
7924 );
7925 variables.insert(slot.include.to_owned(), json!(index < batch_clears.len()));
7926 }
7927 let data = self
7928 .graphql(graphql::UPDATE_FIELDS, Value::Object(variables))
7929 .await?;
7930 let sent = FIELD_WRITE_SLOTS[..batch.len()]
7931 .iter()
7932 .chain(&FIELD_CLEAR_SLOTS[..batch_clears.len()]);
7933 for slot in sent {
7934 let alias = slot.alias;
7935 let returned = data
7936 .get(alias)
7937 .and_then(|value| value.get("projectV2Item"))
7938 .ok_or_else(|| SourceError::Malformed {
7939 message: format!("GitHub field update {alias} returned no project item"),
7940 })?;
7941 if required_str(returned, "id")? != item {
7942 return Err(SourceError::Malformed {
7943 message: format!(
7944 "GitHub field update {alias} returned the wrong project item"
7945 ),
7946 });
7947 }
7948 }
7949 }
7950 }
7951
7952 async fn native_dependency_ids(&self, id: &NativeId) -> Result<Vec<String>, SourceError> {
7953 let mut after: Option<String> = None;
7954 let mut ids = Vec::new();
7955 loop {
7956 let data = self
7957 .graphql(
7958 graphql::ISSUE_DEPENDENCIES,
7959 json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after}),
7960 )
7961 .await?;
7962 let connection =
7963 data.pointer("/node/blockedBy")
7964 .ok_or_else(|| SourceError::Malformed {
7965 message: "GitHub dependency response has no blockedBy connection".into(),
7966 })?;
7967 ids.extend(
7968 connection
7969 .get("nodes")
7970 .and_then(Value::as_array)
7971 .ok_or_else(|| SourceError::Malformed {
7972 message: "GitHub dependency response nodes is not an array".into(),
7973 })?
7974 .iter()
7975 .map(|value| required_str(value, "id").map(str::to_owned))
7976 .collect::<Result<Vec<_>, _>>()?,
7977 );
7978 let next = next_cursor(connection)?;
7979 if let Some(next) = &next {
7980 validate_cursor_progress(after.as_deref(), &next.0)?;
7981 }
7982 after = next.map(|cursor| cursor.0);
7983 if after.is_none() {
7984 return Ok(ids);
7985 }
7986 }
7987 }
7988
7989 async fn dependencies(
7990 &self,
7991 id: &NativeId,
7992 near_kind: ItemKind,
7993 direction: Direction,
7994 page: &PageRequest,
7995 ) -> Result<Page<DependencyEdge>, SourceError> {
7996 validate_page(page)?;
7997 let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
7998 let cursor = page.cursor.as_ref().map(|c| c.0.as_str());
7999 let recorded = recorded_offset(cursor, direction)?;
8000 // What this issue is blocked by, when a read of it by its own id in this command
8001 // already carried the whole connection — a copy reads the item it writes before it
8002 // reads its edges — and the page asked for is the whole of it, or the recorded tail
8003 // after it. Answered from that read, in the shape the dependency read answers in;
8004 // anything else is asked of GitHub.
8005 let carried = match direction {
8006 Direction::DependsOn => self
8007 .resolved_cache()?
8008 .get(id)
8009 .filter(|item| item.content_kind == ContentKind::Issue)
8010 .and_then(|item| Some((item.blocked_by.clone()?, item.raw_body.clone()))),
8011 Direction::DependedOnBy => None,
8012 }
8013 .filter(|(nodes, _)| recorded.is_some() || (cursor.is_none() && nodes.len() <= limit));
8014 // Asked for even in the recorded phase, whose page reads nothing from the
8015 // connection: `__typename` is what says whether this item has a native
8016 // relationship at all, and that is what decides which far ends the reserved key is
8017 // allowed to hold.
8018 let data = match carried {
8019 Some((nodes, body)) => json!({"node":{"__typename":"Issue","body":body,
8020 "blockedBy":{"nodes":nodes,"pageInfo":{"hasNextPage":false,"endCursor":null}}}}),
8021 None => {
8022 self.graphql(
8023 graphql::ISSUE_DEPENDENCIES,
8024 json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),
8025 "after":if recorded.is_some() {None} else {cursor}}),
8026 )
8027 .await?
8028 }
8029 };
8030 let node =
8031 data.get("node")
8032 .filter(|v| !v.is_null())
8033 .ok_or_else(|| SourceError::Refused {
8034 message: format!(
8035 "GitHub item {} was not found or does not support dependencies",
8036 id.0
8037 ),
8038 })?;
8039 let connection_name = match direction {
8040 Direction::DependsOn => "blockedBy",
8041 Direction::DependedOnBy => "blocking",
8042 };
8043 // A draft has neither `blockedBy` nor `blocking`, so nothing it depends on can be
8044 // named natively and the reserved key may hold any far end. An issue's connections
8045 // hold issues, and this source reads them at the near item's own level.
8046 let natively_names = (required_str(node, "__typename")? == "Issue").then_some(near_kind);
8047 if let Some(offset) = recorded {
8048 return Ok(recorded_page(
8049 self.recorded_edges(id, near_kind, direction, natively_names, node)
8050 .await?,
8051 offset,
8052 limit,
8053 ));
8054 }
8055 if natively_names.is_none() {
8056 return Ok(recorded_page(
8057 self.recorded_edges(id, near_kind, direction, natively_names, node)
8058 .await?,
8059 0,
8060 limit,
8061 ));
8062 }
8063 let connection = node
8064 .get(connection_name)
8065 .ok_or_else(|| SourceError::Malformed {
8066 message: "GitHub dependency response is missing its connection".into(),
8067 })?;
8068 let nodes = connection
8069 .get("nodes")
8070 .and_then(Value::as_array)
8071 .ok_or_else(|| SourceError::Malformed {
8072 message: "GitHub dependency response nodes is not an array".into(),
8073 })?;
8074 // `from` depends on `to`, always. GitHub spells the same relationship from either
8075 // end — `blockedBy` lists what this item waits on, `blocking` lists what waits on
8076 // it — so the near item is `from` in one direction and `to` in the other.
8077 let items = nodes
8078 .iter()
8079 .map(|value| {
8080 let related = NativeId(required_str(value, "id")?.into());
8081 let related_kind = related_kind(value)?;
8082 let (from, to) = match direction {
8083 Direction::DependsOn => (
8084 DependencyEndpoint::from_native(id.clone(), near_kind),
8085 DependencyEndpoint::from_native(related, related_kind),
8086 ),
8087 Direction::DependedOnBy => (
8088 DependencyEndpoint::from_native(related, related_kind),
8089 DependencyEndpoint::from_native(id.clone(), near_kind),
8090 ),
8091 };
8092 Ok(DependencyEdge {
8093 from,
8094 to,
8095 kind: DependencyKind::Blocks,
8096 })
8097 })
8098 .collect::<Result<Vec<_>, SourceError>>()?;
8099 let mut next = next_cursor(connection)?;
8100 if let Some(next) = &next {
8101 validate_cursor_progress(cursor, &next.0)?;
8102 }
8103 if next.is_none()
8104 && !self
8105 .recorded_edges(id, near_kind, direction, natively_names, node)
8106 .await?
8107 .is_empty()
8108 {
8109 next = Some(Cursor(format!("{RECORDED_CURSOR}0")));
8110 }
8111 Ok(Page { items, next })
8112 }
8113
8114 /// The edges this item records under [`DependencyEdge::RECORDED_KEY`], which is where
8115 /// a far end in another source has to live: no GitHub issue relationship can name one.
8116 ///
8117 /// Only forwards. The reverse of a recorded edge is derived from the far end, and this
8118 /// source never writes one down.
8119 ///
8120 /// The metadata lives in the item's own body slot, and `node` is the dependency read's
8121 /// own answer, which carries an issue's body — so an issue's recorded edges cost no
8122 /// request beyond the read already made, and reading the board for them would be a
8123 /// walk of every item for one field of one. A draft has no body in that answer, because
8124 /// a draft is not an issue, so a draft's are read off its own read by id — never off a
8125 /// listing of the board, which can be behind on the very item asked about.
8126 async fn recorded_edges(
8127 &self,
8128 id: &NativeId,
8129 near_kind: ItemKind,
8130 direction: Direction,
8131 natively_names: Option<ItemKind>,
8132 node: &Value,
8133 ) -> Result<Vec<DependencyEdge>, SourceError> {
8134 if direction != Direction::DependsOn {
8135 return Ok(Vec::new());
8136 }
8137 let slot = match node.get("body") {
8138 Some(body) if natively_names.is_some() => {
8139 metadata_body(body.as_str().map(str::to_owned))?.1
8140 }
8141 _ => {
8142 let Some(item) = self.bound_item(id).await? else {
8143 return Ok(Vec::new());
8144 };
8145 item.slot
8146 }
8147 };
8148 DependencyEdge::recorded(&slot, id, near_kind, &self.name, natively_names)
8149 .map_err(|message| SourceError::Malformed { message })
8150 }
8151
8152 fn configured_repository(&self) -> Result<&RepositoryTarget, SourceError> {
8153 self.repository
8154 .as_ref()
8155 .ok_or_else(|| SourceError::Refused {
8156 message: format!(
8157 "source {} has no repository configured, and a GitHub Projects board has no \
8158 repository of its own to create an issue in; set repository: owner/name on \
8159 this source",
8160 self.name
8161 ),
8162 })
8163 }
8164
8165 /// The repository one new issue is created in, under the rule [`RepositoryTarget`]
8166 /// states.
8167 ///
8168 /// The fallback is demanded first, whichever arm answers: a write without a configured
8169 /// repository is refused naming the field exactly as it was before the rule existed,
8170 /// so a source that could not write before cannot write now, rather than writing for
8171 /// the one item whose own field happens to decide it.
8172 ///
8173 /// Everything this refuses is refused before `createIssue`, so a refusal leaves no
8174 /// issue behind: an entry that is not a repository on [`RepositoryTarget::HOST`], an
8175 /// entry owned by someone other than the owner of the parent issue's repository —
8176 /// GitHub accepts a sub-issue from another repository of the same owner and from no
8177 /// other, so `addSubIssue` would refuse it after the issue existed — a parent the
8178 /// board does not hold, and a parent that is a draft, which GitHub gives no sub-issues,
8179 /// both of which `addSubIssue` would likewise refuse too late. Whether the entry exists
8180 /// and is visible to the token is checked where its node id is resolved, still before
8181 /// `createIssue`. The parent is read by its own id through [`Self::item_by_id`] — never
8182 /// looked up in a listing of the board, which can be minutes behind an issue its own
8183 /// `projectItems` already places on it — and that read answers first from this process's
8184 /// own record, so a project created moments ago in this command answers though GitHub
8185 /// has not caught up.
8186 async fn creation_target(
8187 &self,
8188 incoming: &Incoming<'_>,
8189 ) -> Result<RepositoryTarget, SourceError> {
8190 let fallback = self.configured_repository()?;
8191 let what = |incoming: &Incoming<'_>| {
8192 format!(
8193 "{} {:?}",
8194 incoming.written.kind().describes(),
8195 incoming.title
8196 )
8197 };
8198 let parent = match incoming.parent {
8199 Some(parent) => Some(self.bound_item(parent).await?.ok_or_else(|| {
8200 SourceError::Refused {
8201 message: format!(
8202 "GitHub project issue {} was not found on the board of source {}, so {} \
8203 cannot be filed under it",
8204 parent.0,
8205 self.name,
8206 what(incoming)
8207 ),
8208 }
8209 })?),
8210 None => None,
8211 };
8212 let parents_repository = parent
8213 .as_ref()
8214 .map(|parent| {
8215 // A draft is on the board and so is found, but it has no repository to
8216 // place a task in and GitHub gives it no sub-issues, so `addSubIssue`
8217 // would refuse the task only once `createIssue` had made it.
8218 if parent.content_kind == ContentKind::DraftIssue {
8219 return Err(SourceError::Refused {
8220 message: format!(
8221 "GitHub project item {} on the board of source {} is a draft, \
8222 which cannot have sub-issues, so {} cannot be filed under it",
8223 parent.id.0,
8224 self.name,
8225 what(incoming)
8226 ),
8227 });
8228 }
8229 // An issue's repository is where a sub-issue is placed and whose owner it
8230 // is compared against, so a parent whose repository this source cannot
8231 // spell as `owner/name` — GitHub's login grammar is wider than this
8232 // source's floor — is one nothing can be filed under.
8233 parent
8234 .own_repository
8235 .as_ref()
8236 .and_then(|origin| RepositoryTarget::from_origin(origin).ok())
8237 .ok_or_else(|| SourceError::Malformed {
8238 message: format!(
8239 "GitHub project issue {} on the board of source {} is in {}, which \
8240 is not a {}/owner/name repository this source can place {} in",
8241 parent.id.0,
8242 self.name,
8243 parent
8244 .own_repository
8245 .as_ref()
8246 .map_or("no repository", Repository::as_str),
8247 RepositoryTarget::HOST,
8248 what(incoming)
8249 ),
8250 })
8251 })
8252 .transpose()?;
8253 match incoming.repositories {
8254 [named] => {
8255 let target =
8256 RepositoryTarget::from_origin(named).map_err(|_| SourceError::Refused {
8257 message: format!(
8258 "{} names repository {}, which is not a {}/owner/name repository \
8259 source {} can create an issue in; name one that is, or name none",
8260 what(incoming),
8261 named.as_str(),
8262 RepositoryTarget::HOST,
8263 self.name
8264 ),
8265 })?;
8266 if let Some(parents) = &parents_repository
8267 && parents.owner != target.owner
8268 {
8269 return Err(SourceError::Refused {
8270 message: format!(
8271 "{} names repository {}, owned by {}, but its project's issue is in \
8272 {}, owned by {}, and GitHub files a sub-issue only in a repository \
8273 of the same owner as its parent issue; name a repository of {}, or \
8274 name none",
8275 what(incoming),
8276 target.slug(),
8277 target.owner,
8278 parents.slug(),
8279 parents.owner,
8280 parents.owner
8281 ),
8282 });
8283 }
8284 Ok(target)
8285 }
8286 _ => Ok(parents_repository.unwrap_or_else(|| fallback.clone())),
8287 }
8288 }
8289
8290 /// The node id of the repository `incoming` is being created in, or the refusal naming
8291 /// the item and the repository the token cannot see.
8292 ///
8293 /// Resolved once per command per repository; see [`Self::repository_cache`].
8294 async fn repository_id(
8295 &self,
8296 repository: &RepositoryTarget,
8297 incoming: &Incoming<'_>,
8298 ) -> Result<String, SourceError> {
8299 if let Some(id) = self.repository_cache()?.get(repository).cloned() {
8300 return Ok(id);
8301 }
8302 let data = self
8303 .graphql(
8304 graphql::REPOSITORY,
8305 json!({"owner":repository.owner,"name":repository.name}),
8306 )
8307 .await?;
8308 self.repository_read(&data, repository, incoming)
8309 }
8310
8311 /// The repository's node id out of an answer carrying the `repository` root, held for
8312 /// the rest of this command, or the refusal naming the item that cannot be created in it.
8313 fn repository_read(
8314 &self,
8315 data: &Value,
8316 repository: &RepositoryTarget,
8317 incoming: &Incoming<'_>,
8318 ) -> Result<String, SourceError> {
8319 let node = data
8320 .get("repository")
8321 .filter(|value| !value.is_null())
8322 .ok_or_else(|| SourceError::Refused {
8323 message: format!(
8324 "GitHub repository {} was not found or is not visible to the token, so {} \
8325 {:?} cannot be created in it",
8326 repository.slug(),
8327 incoming.written.kind().describes(),
8328 incoming.title
8329 ),
8330 })?;
8331 let id = required_str(node, "id")?.to_owned();
8332 self.repository_cache()?
8333 .insert(repository.clone(), id.clone());
8334 Ok(id)
8335 }
8336
8337 fn repository_cache(
8338 &self,
8339 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<RepositoryTarget, String>>, SourceError> {
8340 self.repository_cache
8341 .lock()
8342 .map_err(|_| SourceError::Unavailable {
8343 message: "this source's record of the destination repository was left \
8344 inconsistent by an earlier failure; next: run the command again"
8345 .into(),
8346 })
8347 }
8348
8349 /// Create or update one board item, whichever kind it is.
8350 async fn write_item(
8351 &self,
8352 incoming: &Incoming<'_>,
8353 target: Option<&NativeId>,
8354 depends_on: &[DependencyEdge],
8355 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
8356 // Refused before anything is read or written: a task or a project titled the way
8357 // this board spells a document would land as an issue this same source reads back
8358 // as a document, so the field this destination cannot carry is named rather than
8359 // written and silently reclassified.
8360 if let Written::Work(kind, _) = incoming.written
8361 && incoming.title.starts_with(DESIGN_TITLE_PREFIX)
8362 {
8363 return Err(SourceError::Refused {
8364 message: format!(
8365 "the title of this {} begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
8366 spells a document, so it would read back as one rather than as a {}; \
8367 retitle it, or copy it as a document",
8368 kind.marker(),
8369 self.name,
8370 kind.marker()
8371 ),
8372 });
8373 }
8374 // The destination is read by its own id, and whether this board holds it is decided
8375 // by that read — its own `projectItems` — rather than by whether a listing of the
8376 // board happens to include it yet. See the module documentation.
8377 let existing = match target {
8378 Some(target) => {
8379 Some(
8380 self.bound_item(target)
8381 .await?
8382 .ok_or_else(|| SourceError::Refused {
8383 message: format!("GitHub destination item {} was not found", target.0),
8384 })?,
8385 )
8386 }
8387 None => None,
8388 };
8389 let existing = existing.as_ref();
8390 // An existing issue is never moved; a new one is created where the rule says — and
8391 // knowing where is what lets the board's fields and that repository's id be read
8392 // together, before anything below needs either.
8393 let creation_target = match existing {
8394 Some(_) => None,
8395 None => {
8396 let target = self.creation_target(incoming).await?;
8397 self.creation_context(&target, incoming).await?;
8398 Some(target)
8399 }
8400 };
8401 let board = self
8402 .fields_for(
8403 existing,
8404 incoming.written.status().is_some(),
8405 incoming
8406 .priority
8407 .is_some_and(|priority| priority != Priority::None),
8408 )
8409 .await?;
8410 let status_target = incoming
8411 .written
8412 .work_status()
8413 .map(|(kind, status)| self.resolved_target(kind, status.category))
8414 .transpose()?;
8415 let column = match (incoming.written.work_status(), status_target.as_ref()) {
8416 (Some((kind, status)), Some(target)) => {
8417 self.column_for(&board.fields, kind, status.category, target)?
8418 }
8419 _ => None,
8420 };
8421 // Resolved before anything is created, for the reason the column above is: a
8422 // priority this board has no option for is refused while nothing has been written.
8423 let priority_write = match incoming.priority {
8424 Some(priority) => self.priority_write(&board.fields, existing, priority)?,
8425 None => None,
8426 };
8427 // And for the reason the priority is: a value no projected field can hold, or a board
8428 // that cannot hold the field, is refused while nothing has been written.
8429 let (projections, projected) = self.projection_writes(
8430 &board.fields,
8431 existing.map(|item| &item.projected),
8432 incoming.metadata,
8433 None,
8434 )?;
8435 let content_kind = existing.map_or(ContentKind::Issue, |item| item.content_kind);
8436 if content_kind == ContentKind::DraftIssue {
8437 if let (Some(StatusTarget::Terminal(_, _)), Some(status)) =
8438 (status_target.as_ref(), incoming.written.status())
8439 {
8440 return Err(self.closes_a_draft(status.category));
8441 }
8442 if incoming.parent.is_some() {
8443 return Err(SourceError::Refused {
8444 message: "GitHub draft items cannot be a project's sub-issue".into(),
8445 });
8446 }
8447 }
8448 match existing {
8449 Some(item) if content_kind == ContentKind::Issue => {
8450 if item.labels != incoming.labels {
8451 return Err(SourceError::Refused {
8452 message: "GitHub issue labels differ from the labels being written".into(),
8453 });
8454 }
8455 }
8456 _ => {
8457 if !incoming.labels.is_empty() {
8458 return Err(SourceError::Refused {
8459 message: "GitHub items created by this destination carry no labels".into(),
8460 });
8461 }
8462 }
8463 }
8464
8465 // The repository the issue really lives in is what the slot below is written against,
8466 // so a single entry that is where the issue is created travels as no key at all, and
8467 // the read side derives it back from the issue.
8468 let own_repository = match (existing, &creation_target) {
8469 (Some(item), _) => item.own_repository.clone(),
8470 (None, Some(target)) => Some(
8471 Repository::try_from(target.origin())
8472 .map_err(|message| SourceError::Config { message })?,
8473 ),
8474 (None, None) => None,
8475 };
8476 let (native, fallback) = self
8477 .partition_edges(
8478 incoming.written.kind(),
8479 content_kind,
8480 existing.and_then(|item| item.blocked_by.as_deref()),
8481 depends_on,
8482 )
8483 .await?;
8484 let mut slot = slot_metadata(incoming, own_repository.as_ref(), &fallback);
8485 let content = match incoming.assets {
8486 Some(assets) => {
8487 let uploads = self.upload_assets(own_repository.as_ref(), assets).await?;
8488 let rewritten = onetaskgraph_plugin_api::serve_asset_references(
8489 incoming.content.unwrap_or_default(),
8490 &mut slot,
8491 &uploads,
8492 );
8493 incoming.content.map(|_| rewritten)
8494 }
8495 None => incoming.content.map(str::to_owned),
8496 };
8497 let body = compose_body(content.as_deref(), &slot)?;
8498 // Read before anything is created, for the reason the field below is: a value
8499 // this destination cannot store has to refuse, and refusing after `createIssue`
8500 // would leave an issue behind that nothing asked for. The engine writes a
8501 // qualified id here; a caller handing this key anything else is told so rather
8502 // than having it silently stored as no origin at all.
8503 // 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.
8504 let origin = match incoming.metadata.get(ORIGIN_KEY) {
8505 None => "",
8506 Some(Value::String(origin)) => origin.as_str(),
8507 Some(other) => {
8508 return Err(SourceError::Refused {
8509 message: format!(
8510 "{ORIGIN_KEY} holds a qualified id spelled as a string, and this item's \
8511 is {other}"
8512 ),
8513 });
8514 }
8515 };
8516 // Resolved before anything is created: a board that cannot carry the copy origin
8517 // has to refuse the write, and refusing it after `createIssue` would leave an
8518 // issue behind that nothing asked for.
8519 let origin_field = match Board::field(&board.fields, ORIGIN_FIELD)? {
8520 Some(field) => {
8521 if required_str(field, "__typename")? != "ProjectV2Field" {
8522 return Err(SourceError::Refused {
8523 message: format!(
8524 "GitHub board source-owned {ORIGIN_FIELD} field is not a text field"
8525 ),
8526 });
8527 }
8528 Some(required_str(field, "id")?.to_owned())
8529 }
8530 None if incoming.metadata.contains_key(ORIGIN_KEY) => {
8531 return Err(SourceError::Refused {
8532 message: format!(
8533 "GitHub board has no source-owned {ORIGIN_FIELD} text field, and the \
8534 item carries {ORIGIN_KEY}; add a text field named {ORIGIN_FIELD} to \
8535 the board"
8536 ),
8537 });
8538 }
8539 None => None,
8540 };
8541
8542 let Landed {
8543 content_id,
8544 item_id,
8545 url,
8546 number,
8547 } = match existing {
8548 // Its content is written last, below, once everything else has landed.
8549 Some(item) => Landed {
8550 content_id: item.id.clone(),
8551 item_id: item.item_id.clone(),
8552 url: item.url.clone(),
8553 number: item.number,
8554 },
8555 None => {
8556 let target = creation_target
8557 .as_ref()
8558 .ok_or_else(|| SourceError::Malformed {
8559 message: "a new item was decided without a repository to create it in"
8560 .into(),
8561 })?;
8562 self.create_and_file_issue(board.id.as_str(), target, incoming, &body)
8563 .await?
8564 }
8565 };
8566
8567 // A write the store's cache expects sends only what differs from the version it expects:
8568 // a priority the item already holds there is not sent, so a person's edit of it since is
8569 // left alone. Every other write sends it, as its pinned cost says.
8570 let priority_write = priority_write.filter(|_| {
8571 !existing.is_some_and(|item| {
8572 self.expected.is_primed(&item.id)
8573 && incoming.priority.map(HeldPriority::Read) == Some(item.priority.clone())
8574 })
8575 });
8576 let written_option = column.as_ref().map(|(_, _, name)| name.clone());
8577 let column = column
8578 .filter(|(_, _, name)| existing.is_none_or(|item| item.option.as_ref() != Some(name)))
8579 .map(|(field, option, _)| (field, option));
8580 // Creating an item here is several calls — `createIssue`, which files it on the
8581 // board, then its board fields, the parent and the dependencies — and GitHub can fail
8582 // at any of them. Everything this source can refuse *before* the first of those is
8583 // already checked above, so what is left is GitHub itself failing part way. When it
8584 // does over an item this call created, the issue is taken back: a write that
8585 // refused must not leave an item behind that nobody asked for, and one that does
8586 // makes the retry create a second.
8587 // Whether the board-field write carrying a moved origin was answered as landing whole.
8588 // When it was refused, GitHub does not say which of its fields ran before the one that
8589 // failed, so the origin may or may not have moved.
8590 let mut origin_landed = false;
8591 let landed = self
8592 .finish_write(
8593 board.id.as_str(),
8594 incoming,
8595 &content_id,
8596 &item_id,
8597 content_kind,
8598 existing,
8599 origin_field.as_deref(),
8600 origin,
8601 column,
8602 status_target.as_ref(),
8603 priority_write.as_ref(),
8604 &projections,
8605 &native,
8606 &mut origin_landed,
8607 )
8608 .await;
8609 // An existing item's title, body and state go last, in one `updateIssue`, once its board
8610 // fields and its relationships have landed: a refusal of any of those then leaves its
8611 // body — and the metadata slot inside it — exactly as it stood.
8612 let landed = match (landed, existing) {
8613 (Ok(()), Some(item)) => {
8614 self.update_existing(item, incoming, &body, status_target.as_ref())
8615 .await
8616 }
8617 (landed, _) => landed,
8618 };
8619 if let Err(error) = landed {
8620 match existing {
8621 // Best effort, and the write's own failure is what the caller is told: a
8622 // refusal naming the tidy-up would hide why the write failed at all.
8623 None => {
8624 let _ = self.delete_issue(&content_id).await;
8625 }
8626 // The origin field is the one piece of an existing item's metadata written
8627 // before its body, so a write refused after it puts it back as it was. When
8628 // that is refused too, the write's own failure is still what the caller is
8629 // told — with what it left behind added, because the item's metadata is then
8630 // not as it stood and a caller retrying has to know which key moved.
8631 Some(item) => {
8632 let before = item.origin.as_deref().unwrap_or("");
8633 if let Some(field) = origin_field.as_deref()
8634 && before != origin
8635 && let Err(restore) = self
8636 .set_item_field(
8637 board.id.as_str(),
8638 &item.item_id,
8639 field,
8640 json!({"text": before}),
8641 )
8642 .await
8643 {
8644 let left = if origin_landed {
8645 format!(
8646 "its {ORIGIN_KEY} was moved to {origin:?} before that and could \
8647 not be put back to {before:?} ({restore}), so item {} still \
8648 holds {origin:?} there",
8649 item.id.0
8650 )
8651 } else {
8652 format!(
8653 "the refused write carried its {ORIGIN_KEY} from {before:?} to \
8654 {origin:?}, GitHub does not say whether that part of it ran, \
8655 and putting it back to {before:?} was refused ({restore}), so \
8656 item {} holds {origin:?} or {before:?} there",
8657 item.id.0
8658 )
8659 };
8660 return Err(noting(
8661 error,
8662 &format!(
8663 "; {left}; next: set {ORIGIN_KEY} on it back to {before:?}, or \
8664 run the write again"
8665 ),
8666 ));
8667 }
8668 }
8669 }
8670 return Err(error);
8671 }
8672
8673 let written_status = match (incoming.written.work_status(), status_target.as_ref()) {
8674 (Some((kind, _)), Some(StatusTarget::Terminal(_, reason))) => {
8675 self.statuses
8676 .status(kind, written_option.as_deref(), true, Some(reason.reason()))
8677 }
8678 (Some((kind, _)), Some(StatusTarget::Column(_))) => {
8679 self.statuses
8680 .status(kind, written_option.as_deref(), false, None)
8681 }
8682 (Some((_, status)), _) => status.clone(),
8683 (None, _) => Status {
8684 category: StatusCategory::Unknown,
8685 name: "Open".to_owned(),
8686 },
8687 };
8688
8689 // So the rest of this command reads what it just did rather than what the board
8690 // said before it. See `remember_written` for which half takes it.
8691 let remembered = Resolved {
8692 item_id,
8693 id: content_id.clone(),
8694 content_kind,
8695 kind: incoming.written.kind(),
8696 title: incoming.title.to_owned(),
8697 // The visible half of the body this write composed, split back off it the
8698 // way a read splits it — so what this record reports is what a read of the
8699 // same issue reports, rather than the person's text with the metadata slot
8700 // still on the end of it.
8701 body: metadata_body(body.clone())?.0,
8702 raw_body: body.clone(),
8703 // A document has no status of its own; what it reads back as is whatever
8704 // the issue's own state says, which is what a re-read reports.
8705 status: written_status,
8706 option: written_option.or_else(|| existing.and_then(|item| item.option.clone())),
8707 priority: match incoming.priority {
8708 Some(priority) => HeldPriority::Read(priority),
8709 None => existing.map_or(HeldPriority::Read(Priority::None), |item| {
8710 item.priority.clone()
8711 }),
8712 },
8713 // What `state_input` asked for: closed for a terminal target, open for any other
8714 // status, and the issue's own state left as it was by a document write.
8715 closed: content_kind == ContentKind::Issue
8716 && match status_target.as_ref() {
8717 Some(StatusTarget::Terminal(_, _)) => true,
8718 Some(_) => false,
8719 None => existing.is_some_and(|item| item.closed),
8720 },
8721 reason: match status_target.as_ref() {
8722 Some(StatusTarget::Terminal(_, reason)) if content_kind == ContentKind::Issue => {
8723 Some(reason.reason().to_owned())
8724 }
8725 Some(_) => None,
8726 None => existing.and_then(|item| item.reason.clone()),
8727 },
8728 delivers: incoming.delivers.to_vec(),
8729 delivered_by: incoming.delivered_by.to_vec(),
8730 labels: incoming.labels.to_vec(),
8731 parent: incoming.parent.cloned(),
8732 origin: (!origin.is_empty()).then(|| origin.to_owned()),
8733 projected,
8734 number,
8735 // In the update path this is the item's own url, read off `existing` where the
8736 // record above was bound, so one expression serves both halves.
8737 url,
8738 created_at: existing.and_then(|item| item.created_at),
8739 updated_at: existing.and_then(|item| item.updated_at),
8740 // What this write moved is GitHub's to say, so a record of it gives no token until
8741 // it is read again.
8742 item_updated_at: None,
8743 comment_count: None,
8744 relationship_events: None,
8745 served_at: None,
8746 facets: None,
8747 own_repository,
8748 repositories: incoming.repositories.to_vec(),
8749 classification: incoming.classification,
8750 slot,
8751 board_id: Some(board.id.as_str().to_owned()),
8752 fields: board
8753 .fields
8754 .get("nodes")
8755 .and_then(Value::as_array)
8756 .cloned()
8757 .unwrap_or_default(),
8758 board_fields: Some(board.fields.clone()),
8759 // What this write left the relationship holding is known by id alone, and a
8760 // later read of its edges needs each far end's kind, so it reads them again.
8761 blocked_by: None,
8762 };
8763 self.remember_written(remembered, existing.is_none())?;
8764 Ok(onetaskgraph_plugin_api::AssetsWritten {
8765 id: content_id,
8766 content,
8767 })
8768 }
8769
8770 /// Everything a write does after the item exists: its board fields, its parent, and
8771 /// its dependencies.
8772 ///
8773 /// Split out of `write_item` so there is one place a failure past the point of no
8774 /// return is caught, rather than a tidy-up repeated at each `?` above.
8775 // llmlint: ignore[suppressions_justified] This is the tail of `write_item` lifted out
8776 // so there is one place a failure past the point of no return is caught, and its
8777 // arguments are exactly the values that tail already had in scope. Bundling them into a
8778 // struct would describe no concept — it would be "the arguments of this function" — and
8779 // would put the whole of `write_item`'s locals behind one more indirection.
8780 #[allow(clippy::too_many_arguments)]
8781 async fn finish_write(
8782 &self,
8783 board_id: &str,
8784 incoming: &Incoming<'_>,
8785 content_id: &NativeId,
8786 item_id: &str,
8787 content_kind: ContentKind,
8788 existing: Option<&Resolved>,
8789 origin_field: Option<&str>,
8790 origin: &str,
8791 column: Option<(String, String)>,
8792 status_target: Option<&StatusTarget>,
8793 priority: Option<&PriorityWrite>,
8794 projections: &[ProjectionWrite],
8795 native: &[String],
8796 origin_landed: &mut bool,
8797 ) -> Result<(), SourceError> {
8798 let mut fields = Vec::new();
8799 if let Some(field_id) = origin_field
8800 && existing.map_or(!origin.is_empty(), |item| {
8801 item.origin.as_deref().unwrap_or("") != origin
8802 })
8803 {
8804 fields.push((field_id.to_owned(), json!({"text":origin})));
8805 }
8806 if let Some((field_id, option_id)) = column {
8807 fields.push((field_id, json!({"singleSelectOptionId":option_id})));
8808 }
8809 let mut clears = Vec::new();
8810 match priority {
8811 Some(PriorityWrite::Select { field, option }) => {
8812 fields.push((field.clone(), json!({"singleSelectOptionId":option})));
8813 }
8814 Some(PriorityWrite::Clear { field }) => clears.push(field.as_str()),
8815 None => {}
8816 }
8817 for projection in projections {
8818 projection.join(&mut fields, &mut clears);
8819 }
8820 self.set_item_fields(board_id, item_id, &fields, &clears)
8821 .await?;
8822 *origin_landed = true;
8823
8824 // An existing issue closes in the `updateIssue` its write ends with; one created just
8825 // now closes here, once its option is selected.
8826 if existing.is_none()
8827 && content_kind == ContentKind::Issue
8828 && matches!(status_target, Some(StatusTarget::Terminal(_, _)))
8829 {
8830 self.update_content(
8831 ContentKind::Issue,
8832 content_id,
8833 json!({"stateInput":state_input(status_target)}),
8834 )
8835 .await?;
8836 }
8837
8838 if content_kind == ContentKind::Issue {
8839 self.reparent(
8840 existing.and_then(|item| item.parent.clone()),
8841 content_id,
8842 incoming.parent,
8843 )
8844 .await?;
8845 // A document takes part in no dependency graph, so writing one neither reads
8846 // nor changes the issue's own `blockedBy` relationships. Reconciling them
8847 // against the empty list a document write carries would *delete* whatever
8848 // relationships a person had made on that issue, which is a write nobody
8849 // asked for.
8850 if incoming.written.kind() != BoardKind::Document {
8851 let issue = match existing {
8852 Some(item) => Issue::Existing(item.blocked_by.as_deref()),
8853 None => Issue::Created,
8854 };
8855 self.reconcile_blocked_by(content_id, native, issue).await?;
8856 }
8857 }
8858 Ok(())
8859 }
8860
8861 /// Delete one issue, which takes its board item with it.
8862 async fn delete_issue(&self, id: &NativeId) -> Result<(), SourceError> {
8863 let data = self
8864 .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
8865 .await?;
8866 data.pointer("/deleteIssue/repository")
8867 .filter(|value| !value.is_null())
8868 .ok_or_else(|| SourceError::Malformed {
8869 message: "GitHub issue deletion returned no repository".into(),
8870 })?;
8871 self.forget(id)?;
8872 Ok(())
8873 }
8874
8875 /// Remove one item this copy created, so a copy that could not finish leaves the board
8876 /// as it found it.
8877 ///
8878 /// Deleting the issue takes its board item with it, so there is no second mutation to
8879 /// keep in step. An id the board does not hold is not an error: the item is already
8880 /// gone, which is the state this asks for. Which that is, is decided by reading the item
8881 /// by its own id — a listing of the board can still be missing an item it holds, and
8882 /// reading that as *already gone* would leave behind the very item this was asked to
8883 /// take back.
8884 async fn delete_item(&self, id: &NativeId) -> Result<(), SourceError> {
8885 let Some(item) = self.bound_item(id).await? else {
8886 return Ok(());
8887 };
8888 if item.content_kind == ContentKind::DraftIssue {
8889 return Err(SourceError::Refused {
8890 message: format!(
8891 "GitHub item {} is a draft, and this source removes an item by deleting \
8892 its issue; next: remove it from the board by hand",
8893 id.0
8894 ),
8895 });
8896 }
8897 let data = self
8898 .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
8899 .await?;
8900 data.pointer("/deleteIssue/repository")
8901 .filter(|value| !value.is_null())
8902 .ok_or_else(|| SourceError::Malformed {
8903 message: "GitHub issue deletion returned no repository".into(),
8904 })?;
8905 self.forget(id)?;
8906 Ok(())
8907 }
8908
8909 /// The issue a comment call on `task` is about, or `None` when this board holds no such
8910 /// task.
8911 ///
8912 /// Resolved exactly as [`TaskSource::get_task`] resolves it, so the comment verbs and a
8913 /// read of the task cannot disagree about which ids name one: a project or a document of
8914 /// this board is not a task here either.
8915 ///
8916 /// A **draft** is a task with nowhere to keep a comment, because GitHub keeps comments on
8917 /// issues and a draft is not one. It is refused rather than answered with an empty page,
8918 /// which would read as a task nobody has commented on yet.
8919 async fn commented_issue(&self, task: &NativeId) -> Result<Option<NativeId>, SourceError> {
8920 let cached = self.resolved_cache()?.get(task).cloned();
8921 let Some(item) = (match cached {
8922 Some(item) => Some(item),
8923 None => self.item_by_id(task).await?,
8924 })
8925 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
8926 return Ok(None);
8927 };
8928 if item.content_kind == ContentKind::DraftIssue {
8929 return Err(self.draft_has_no_comments(task));
8930 }
8931 Ok(Some(item.id))
8932 }
8933
8934 /// The refusal a comment call on a board draft is answered with: GitHub keeps comments on
8935 /// issues, and a draft is not one.
8936 fn draft_has_no_comments(&self, task: &NativeId) -> SourceError {
8937 SourceError::Refused {
8938 message: format!(
8939 "task {} of source {} is a draft item on the board, and GitHub keeps \
8940 comments on issues alone, so a draft has none to read or write; next: \
8941 convert the draft to an issue on the board, then comment on the issue it \
8942 becomes",
8943 task.0, self.name
8944 ),
8945 }
8946 }
8947
8948 /// One task and a page of its comments, read with [`graphql::ISSUE_DETAIL`] in one
8949 /// request — or `None` when this board holds no task by that id.
8950 ///
8951 /// What `task show` and a comment listing read. A draft is a task with no comments, so it
8952 /// is answered with the draft and the refusal, at the price of the draft's own read.
8953 async fn issue_detail(
8954 &self,
8955 id: &NativeId,
8956 page: &PageRequest,
8957 ) -> Result<Option<TaskDetailRead>, SourceError> {
8958 let after = page.cursor.as_ref().map(|cursor| cursor.0.as_str());
8959 let asked = self
8960 .graphql(
8961 graphql::ISSUE_DETAIL,
8962 json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),"after":after,
8963 "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
8964 "duplicates":true}),
8965 )
8966 .await;
8967 let data = match asked {
8968 Ok(data) => data,
8969 Err(error) if unresolvable_node(&error) => return Ok(None),
8970 Err(error) => return Err(error),
8971 };
8972 // `node` is null for an id that names nothing, and absent only from an answer this
8973 // source cannot read — never the same thing.
8974 let node = data.get("node").ok_or_else(|| SourceError::Malformed {
8975 message: format!("GitHub answered the read of {} with no node", id.0),
8976 })?;
8977 self.detail_of(id, node, true, after).await
8978 }
8979
8980 /// Several tasks, each with the first page of its comments when `comments` is set, read
8981 /// [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] — one answer per id, in
8982 /// order.
8983 ///
8984 /// A batch GitHub refuses because one of its ids resolves to no node at all is read again
8985 /// one item at a time, so that id is answered as missing and the others as themselves; any
8986 /// other refusal is every id of that batch's answer.
8987 async fn issue_details(
8988 &self,
8989 ids: &[NativeId],
8990 comments: Option<&PageRequest>,
8991 ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
8992 let mut read = Vec::with_capacity(ids.len());
8993 for batch in ids.chunks(DETAIL_BATCH) {
8994 match self
8995 .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, comments))
8996 .await
8997 {
8998 Ok(data) => {
8999 for (slot, id) in batch.iter().enumerate() {
9000 // Every alias asked for is answered, null for an id naming nothing;
9001 // one missing is an answer this source cannot read.
9002 let read_one = match data.get(format!("i{slot}")) {
9003 Some(node) => self.detail_of(id, node, comments.is_some(), None).await,
9004 None => Err(SourceError::Malformed {
9005 message: format!(
9006 "GitHub answered a batch read with no item for {}",
9007 id.0
9008 ),
9009 }),
9010 };
9011 read.push(read_one);
9012 }
9013 }
9014 Err(error) if unresolvable_node(&error) => {
9015 for id in batch {
9016 read.push(match comments {
9017 Some(page) => self.issue_detail(id, page).await,
9018 None => self.task_read(id).await,
9019 });
9020 }
9021 }
9022 Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
9023 }
9024 }
9025 read
9026 }
9027
9028 /// One task and nothing of its comments, as [`TaskSource::get_task`] reads it.
9029 async fn task_read(&self, id: &NativeId) -> Result<Option<TaskDetailRead>, SourceError> {
9030 Ok(self.get_task(id).await?.map(|task| TaskDetailRead {
9031 task,
9032 comments: None,
9033 }))
9034 }
9035
9036 /// What one node a detail read reached says: the task this board holds by `id`, with the
9037 /// page of comments the node carries when `commented` — or `None` for a node that is no
9038 /// task of this board.
9039 ///
9040 /// Resolved as [`Self::item_by_id`] resolves an item: a draft is read again as a draft,
9041 /// and an item this process created answers from this process's own record, which a node
9042 /// read taken moments after the write can still be behind.
9043 async fn detail_of(
9044 &self,
9045 id: &NativeId,
9046 node: &Value,
9047 commented: bool,
9048 after: Option<&str>,
9049 ) -> Result<Option<TaskDetailRead>, SourceError> {
9050 if node.is_null() {
9051 return Ok(None);
9052 }
9053 let draft = optional_str(node, "__typename")? == Some("DraftIssue");
9054 // An issue answered under one id is that id's, or the answer is not one this source
9055 // can report: reporting another issue's task and comments under the qualified id asked
9056 // for would be the one wrong answer here. A draft's own read checks the same.
9057 if !draft
9058 && optional_str(node, "__typename")? == Some("Issue")
9059 && required_str(node, "id")? != id.0
9060 {
9061 return Err(SourceError::Malformed {
9062 message: format!(
9063 "GitHub answered the read of {} with issue {}",
9064 id.0,
9065 required_str(node, "id")?
9066 ),
9067 });
9068 }
9069 let item = if draft {
9070 self.draft_by_id(id).await?
9071 } else {
9072 self.resolve_issue(node).await?
9073 };
9074 // A read that carried the whole of what the issue is blocked by — a copy's — is kept
9075 // for this command, so the dependency read the copy makes next is answered from it.
9076 if let Some(read) = item.as_ref().filter(|item| item.blocked_by.is_some()) {
9077 self.resolved_cache()?.insert(read.id.clone(), read.clone());
9078 }
9079 let Some(item) = item.filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
9080 return Ok(None);
9081 };
9082 let own = self.created()?.iter().find(|own| own.id == *id).cloned();
9083 let task = own.unwrap_or(item).task()?;
9084 let comments = match (commented, draft) {
9085 (false, _) => None,
9086 (true, true) => Some(Err(self.draft_has_no_comments(id))),
9087 (true, false) => Some(comment_page(node, &id.0, after).map(Some)),
9088 };
9089 if commented && !draft && after.is_none() {
9090 self.expected.learn_comments(id, node);
9091 }
9092 Ok(Some(TaskDetailRead { task, comments }))
9093 }
9094
9095 /// Whether the comment `comment` is one of `issue`'s own.
9096 ///
9097 /// Read before an edit or a removal is sent, because GitHub's comment mutations take the
9098 /// comment's id and nothing else: a comment id given against the wrong task would
9099 /// otherwise change a comment on some other issue entirely. An id that names nothing, or
9100 /// names something that is not an issue comment, is a comment this task does not have —
9101 /// which is what GitHub refusing to resolve it means too.
9102 async fn comment_is_on(
9103 &self,
9104 issue: &NativeId,
9105 comment: &NativeId,
9106 ) -> Result<bool, SourceError> {
9107 Ok(self.comment_on(issue, comment).await?.is_some())
9108 }
9109
9110 /// The body the comment `comment` holds, exactly as GitHub holds it, when it is one of
9111 /// `issue`'s own — the read [`Self::comment_is_on`] makes, answering with what it selected.
9112 async fn comment_on(
9113 &self,
9114 issue: &NativeId,
9115 comment: &NativeId,
9116 ) -> Result<Option<String>, SourceError> {
9117 // A write the store's cache expects knows which comments its issue held, and their
9118 // bodies, at the version it expects: what this read would answer.
9119 if let Some(primed) = self.expected.primed_comment(issue, comment) {
9120 return Ok(primed);
9121 }
9122 let asked = self
9123 .graphql(graphql::COMMENT_ISSUE, json!({"id":comment.0}))
9124 .await;
9125 let data = match asked {
9126 Ok(data) => data,
9127 Err(error) if unresolvable_node(&error) => return Ok(None),
9128 Err(error) => return Err(error),
9129 };
9130 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
9131 return Ok(None);
9132 };
9133 if optional_str(node, "__typename")? != Some("IssueComment") {
9134 return Ok(None);
9135 }
9136 let on = node.get("issue").ok_or_else(|| SourceError::Malformed {
9137 message: format!("GitHub issue comment {} names no issue", comment.0),
9138 })?;
9139 if required_str(on, "id")? != issue.0 {
9140 return Ok(None);
9141 }
9142 Ok(Some(required_str(node, "body")?.to_owned()))
9143 }
9144
9145 /// Which far ends this item's own `blockedBy` relationship holds, and which it cannot.
9146 async fn partition_edges(
9147 &self,
9148 near_kind: BoardKind,
9149 near_content: ContentKind,
9150 carried: Option<&[Value]>,
9151 depends_on: &[DependencyEdge],
9152 ) -> Result<(Vec<String>, Vec<DependencyEdge>), SourceError> {
9153 let mut native = Vec::new();
9154 let mut fallback = Vec::new();
9155 let far_ends: Vec<(&DependencyEdge, &str, bool, Option<&Value>)> = depends_on
9156 .iter()
9157 .map(|edge| {
9158 let same_source = edge
9159 .to
9160 .source()
9161 .is_none_or(|source| source == self.name.as_str());
9162 // A qualified id's source segment runs to its *first* colon — `GlobalId` and
9163 // `DependencyEndpoint::source` both read it that way — and a native id may hold
9164 // colons of its own, so the far end is everything after that one separator.
9165 // Splitting at the last would truncate `work:urn:task:7` to `7`.
9166 let far_id = if edge.to.is_qualified() {
9167 edge.to
9168 .id()
9169 .split_once(':')
9170 .map_or(edge.to.id(), |(_, native)| native)
9171 } else {
9172 edge.to.id()
9173 };
9174 // One that already blocks the near issue was answered by that issue's own
9175 // read, which carried each of its blockers' kinds — an issue every one — so it
9176 // is not read again.
9177 let blocking = carried.and_then(|nodes| {
9178 nodes
9179 .iter()
9180 .find(|node| node.get("id").and_then(Value::as_str) == Some(far_id))
9181 });
9182 (edge, far_id, same_source, blocking)
9183 })
9184 .collect();
9185 // Every other same-source far end is read by its own id, exactly as the item it is a
9186 // far end of is: whether this board holds it is that read's answer, never a listing's.
9187 // They are read together, [`DETAIL_BATCH`] to a request, rather than one each.
9188 let mut unread: Vec<NativeId> = Vec::new();
9189 for (_, far_id, same_source, blocking) in &far_ends {
9190 let id = NativeId((*far_id).to_owned());
9191 if *same_source && blocking.is_none() && !unread.contains(&id) {
9192 unread.push(id);
9193 }
9194 }
9195 let read: BTreeMap<NativeId, Option<Resolved>> = unread
9196 .iter()
9197 .cloned()
9198 .zip(self.items_by_ids(&unread).await?)
9199 .collect();
9200 for (edge, far_id, same_source, blocking) in far_ends {
9201 let far = match (same_source, blocking) {
9202 (false, _) => None,
9203 (true, Some(node)) => Some(FarEnd {
9204 kind: if required_str(node, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
9205 BoardKind::Document
9206 } else {
9207 BoardKind::Work(related_kind(node)?)
9208 },
9209 content_kind: ContentKind::Issue,
9210 }),
9211 (true, None) => {
9212 let read = read
9213 .get(&NativeId(far_id.to_owned()))
9214 .cloned()
9215 .flatten()
9216 .ok_or_else(|| SourceError::Refused {
9217 message: format!("GitHub dependency item {far_id} was not found"),
9218 })?;
9219 Some(FarEnd {
9220 kind: read.kind,
9221 content_kind: read.content_kind,
9222 })
9223 }
9224 };
9225 let far = far.as_ref();
9226 // The caller says which kind the far end is, and this board holds the far end
9227 // itself, so a disagreement is settled here rather than stored: recorded, the
9228 // wrong kind would read back as a cross-level edge that never existed; written
9229 // natively, it would name a relationship of a different level than the caller
9230 // asked for.
9231 //
9232 // A far end this board holds as a *document* fails the same comparison and is
9233 // refused by the same sentence: `ItemKind` has no document variant because
9234 // nothing may point at one, so no caller can name it correctly and the refusal
9235 // is the only honest answer.
9236 if let Some(disagreeing) = far.filter(|far| far.kind != BoardKind::Work(edge.to.kind)) {
9237 return Err(SourceError::Refused {
9238 message: format!(
9239 "GitHub dependency item {far_id} is a {} of this board, and this item \
9240 names it as a {}; record the kind it is",
9241 disagreeing.kind.describes(),
9242 edge.to.kind.marker()
9243 ),
9244 });
9245 }
9246 // A draft has neither `blockedBy` nor `blocking`, so no edge of one is native
9247 // however the far end is spelled — and one classified native here would be
9248 // written nowhere at all, because a draft's native reconciliation never runs.
9249 let native_here = near_content == ContentKind::Issue
9250 && far.is_some_and(|far| {
9251 far.content_kind == ContentKind::Issue
9252 && BoardKind::Work(edge.to.kind) == near_kind
9253 });
9254 if native_here {
9255 native.push(far_id.to_owned());
9256 } else {
9257 fallback.push(edge.clone());
9258 }
9259 }
9260 Ok((native, fallback))
9261 }
9262
9263 async fn update_existing(
9264 &self,
9265 item: &Resolved,
9266 incoming: &Incoming<'_>,
9267 body: &Option<String>,
9268 status_target: Option<&StatusTarget>,
9269 ) -> Result<(), SourceError> {
9270 let title = incoming.written_title();
9271 // A terminal status closes the issue here, in the same mutation as its body: its board
9272 // option was selected before this, so a close never lands on an item whose board cannot
9273 // show it.
9274 let mut fields = match item.content_kind {
9275 ContentKind::DraftIssue => json!({"title":title,"body":body}),
9276 ContentKind::Issue => json!({"title":title,"body":body,
9277 "stateInput":state_input(status_target)}),
9278 };
9279 // A write the store's cache expects sends only the members whose value differs from the
9280 // version it expects, and nothing at all when none does.
9281 if self.expected.is_primed(&item.id)
9282 && let Some(members) = fields.as_object_mut()
9283 {
9284 if members.get("title").and_then(Value::as_str) == Some(item.title.as_str()) {
9285 members.remove("title");
9286 }
9287 if members.get("body").and_then(Value::as_str)
9288 == Some(item.raw_body.as_deref().unwrap_or_default())
9289 {
9290 members.remove("body");
9291 }
9292 let terminal = matches!(status_target, Some(StatusTarget::Terminal(_, _)));
9293 if members.contains_key("stateInput") && terminal == item.closed {
9294 members.remove("stateInput");
9295 }
9296 if members.is_empty() {
9297 return Ok(());
9298 }
9299 }
9300 self.update_content(item.content_kind, &item.id, fields)
9301 .await
9302 }
9303
9304 /// Update one board item's content with exactly `fields` beside its id, through the
9305 /// mutation its kind takes: `updateIssue` for an issue, `updateProjectV2DraftIssue` for
9306 /// a draft.
9307 ///
9308 /// Every input field either mutation leaves out is a field GitHub leaves as it is, which
9309 /// is what lets a narrow write carry the one thing it changes and nothing else.
9310 async fn update_content(
9311 &self,
9312 kind: ContentKind,
9313 id: &NativeId,
9314 fields: Value,
9315 ) -> Result<(), SourceError> {
9316 let (operation, id_key, pointer) = match kind {
9317 ContentKind::DraftIssue => (
9318 graphql::UPDATE_DRAFT,
9319 "draftIssueId",
9320 "/updateProjectV2DraftIssue/draftIssue",
9321 ),
9322 ContentKind::Issue => (graphql::UPDATE_ISSUE, "id", "/updateIssue/issue"),
9323 };
9324 let mut input = fields;
9325 input[id_key] = json!(id.0);
9326 let data = self.graphql(operation, json!({"input":input})).await?;
9327 let returned = data
9328 .pointer(pointer)
9329 .ok_or_else(|| SourceError::Malformed {
9330 message: "GitHub item update returned no item".into(),
9331 })?;
9332 if required_str(returned, "id")? != id.0 {
9333 return Err(SourceError::Malformed {
9334 message: "GitHub item update returned the wrong item".into(),
9335 });
9336 }
9337 Ok(())
9338 }
9339
9340 /// Creates one issue, files it on the board, and reports what a read of it would say:
9341 /// its content id, its board item id, and the web address GitHub gave it.
9342 ///
9343 /// Two calls rather than one: `createIssue` answers with an issue that is on no board,
9344 /// and `addProjectV2ItemById` is what puts it there. Filing it at creation through
9345 /// `CreateIssueInput.projectV2Ids` was tried and is not done: GitHub answered with no
9346 /// board item, and the `addProjectV2ItemById` that then had to follow was refused
9347 /// "Content already exists in this project". A terminal status is not written here:
9348 /// `finish_write` selects its option first and closes the issue after, so a close never
9349 /// lands on an item whose board cannot show it.
9350 ///
9351 /// The address and the number come back here because this is the only place either is
9352 /// known before GitHub's own board read catches up — an item this run created answers
9353 /// the reads that follow it out of the record below, and one remembered without them
9354 /// would report no location and no key for the rest of the run.
9355 async fn create_and_file_issue(
9356 &self,
9357 board_id: &str,
9358 repository: &RepositoryTarget,
9359 incoming: &Incoming<'_>,
9360 body: &Option<String>,
9361 ) -> Result<Landed, SourceError> {
9362 let repository_id = self.repository_id(repository, incoming).await?;
9363 let data = self
9364 .graphql(
9365 graphql::CREATE_ISSUE,
9366 json!({"input":{
9367 "repositoryId":repository_id,"title":incoming.written_title(),"body":body
9368 }}),
9369 )
9370 .await?;
9371 let created = data
9372 .pointer("/createIssue/issue")
9373 .filter(|value| !value.is_null())
9374 .ok_or_else(|| SourceError::Malformed {
9375 message: "GitHub issue creation returned no issue".into(),
9376 })?;
9377 let content_id = NativeId(required_str(created, "id")?.to_owned());
9378 // Optional although GitHub's schema makes it non-null: the issue exists by now, so
9379 // a response without it is not worth failing a landed write over — the item simply
9380 // reports no location until the board read catches up, which is what it did before.
9381 let url = optional_str(created, "url")?.map(str::to_owned);
9382 // The issue exists from here on, so an unreadable number and a refused board
9383 // filing below each try, best effort, to take it back: an issue in the repository
9384 // that is on no board is an item nobody asked for and nothing here would find again.
9385 //
9386 // Its number is optional on the same terms its address is — a landed write is not
9387 // worth failing over a member that came back missing, and such an item reports no
9388 // handle until a board read catches up. A number that is *present* and is not an
9389 // unsigned integer is still a response this source cannot read.
9390 let number = match created_issue_number(created) {
9391 Ok(number) => number,
9392 Err(error) => {
9393 let _ = self.delete_issue(&content_id).await;
9394 return Err(error);
9395 }
9396 };
9397 let added = match self
9398 .graphql(
9399 graphql::ADD_TO_BOARD,
9400 json!({"input":{"projectId":board_id,"contentId":content_id.0}}),
9401 )
9402 .await
9403 {
9404 Ok(added) => added,
9405 Err(error) => {
9406 let _ = self.delete_issue(&content_id).await;
9407 return Err(error);
9408 }
9409 };
9410 let item = added
9411 .pointer("/addProjectV2ItemById/item")
9412 .filter(|value| !value.is_null())
9413 .ok_or_else(|| SourceError::Malformed {
9414 message: "GitHub board addition returned no project item".into(),
9415 })?;
9416 Ok(Landed {
9417 content_id,
9418 item_id: required_str(item, "id")?.to_owned(),
9419 url,
9420 number,
9421 })
9422 }
9423
9424 /// Move one issue under the project it now belongs to, or out of the one it left.
9425 async fn reparent(
9426 &self,
9427 held: Option<NativeId>,
9428 child: &NativeId,
9429 wanted: Option<&NativeId>,
9430 ) -> Result<(), SourceError> {
9431 if held.as_ref() == wanted {
9432 return Ok(());
9433 }
9434 if let Some(held) = &held {
9435 self.sub_issue(graphql::REMOVE_SUB_ISSUE, held, child, "removeSubIssue")
9436 .await?;
9437 }
9438 if let Some(wanted) = wanted {
9439 self.sub_issue(graphql::ADD_SUB_ISSUE, wanted, child, "addSubIssue")
9440 .await?;
9441 }
9442 Ok(())
9443 }
9444
9445 async fn sub_issue(
9446 &self,
9447 operation: &str,
9448 parent: &NativeId,
9449 child: &NativeId,
9450 root: &str,
9451 ) -> Result<(), SourceError> {
9452 let data = self
9453 .graphql(
9454 operation,
9455 json!({"input":{"issueId":parent.0,"subIssueId":child.0}}),
9456 )
9457 .await?;
9458 let issue =
9459 data.pointer(&format!("/{root}/issue"))
9460 .ok_or_else(|| SourceError::Malformed {
9461 message: "GitHub sub-issue update returned no issue".into(),
9462 })?;
9463 let sub =
9464 data.pointer(&format!("/{root}/subIssue"))
9465 .ok_or_else(|| SourceError::Malformed {
9466 message: "GitHub sub-issue update returned no sub-issue".into(),
9467 })?;
9468 if required_str(issue, "id")? != parent.0 || required_str(sub, "id")? != child.0 {
9469 return Err(SourceError::Malformed {
9470 message: "GitHub sub-issue update returned the wrong issues".into(),
9471 });
9472 }
9473 Ok(())
9474 }
9475
9476 /// Bring one issue's `blockedBy` to exactly `native`, sending only the difference, and say
9477 /// whether there was one.
9478 ///
9479 /// An issue [`Issue::Created`] by this very write is blocked by nothing yet, so its
9480 /// relationships are not read: there is nothing a read of them could find.
9481 async fn reconcile_blocked_by(
9482 &self,
9483 content_id: &NativeId,
9484 native: &[String],
9485 issue: Issue<'_>,
9486 ) -> Result<bool, SourceError> {
9487 let current = match issue {
9488 Issue::Created => Vec::new(),
9489 Issue::Existing(Some(held)) => held
9490 .iter()
9491 .map(|far| required_str(far, "id").map(str::to_owned))
9492 .collect::<Result<Vec<_>, _>>()?,
9493 Issue::Existing(None) => self.native_dependency_ids(content_id).await?,
9494 };
9495 let mut changed = false;
9496 for (operation, far_id) in current
9497 .iter()
9498 .filter(|id| !native.contains(id))
9499 .map(|id| (graphql::REMOVE_BLOCKED_BY, id))
9500 .chain(
9501 native
9502 .iter()
9503 .filter(|id| !current.contains(id))
9504 .map(|id| (graphql::ADD_BLOCKED_BY, id)),
9505 )
9506 {
9507 let data = self
9508 .graphql(
9509 operation,
9510 json!({"input":{"issueId":content_id.0,"blockingIssueId":far_id}}),
9511 )
9512 .await?;
9513 let root = if operation == graphql::ADD_BLOCKED_BY {
9514 "addBlockedBy"
9515 } else {
9516 "removeBlockedBy"
9517 };
9518 let issue =
9519 data.pointer(&format!("/{root}/issue"))
9520 .ok_or_else(|| SourceError::Malformed {
9521 message: "GitHub dependency update returned no issue".into(),
9522 })?;
9523 let blocker = data
9524 .pointer(&format!("/{root}/blockingIssue"))
9525 .ok_or_else(|| SourceError::Malformed {
9526 message: "GitHub dependency update returned no blocking issue".into(),
9527 })?;
9528 if required_str(issue, "id")? != content_id.0 || required_str(blocker, "id")? != far_id
9529 {
9530 return Err(SourceError::Malformed {
9531 message: "GitHub dependency update returned the wrong issues".into(),
9532 });
9533 }
9534 changed = true;
9535 }
9536 Ok(changed)
9537 }
9538}
9539
9540/// What a write needs to know of one far end it names: which kind of item it is, and whether
9541/// it is an issue a native relationship can name.
9542struct FarEnd {
9543 kind: BoardKind,
9544 content_kind: ContentKind,
9545}
9546
9547/// Whether the issue one write reconciles was created by that write or was already there.
9548#[derive(Clone, Copy, PartialEq, Eq)]
9549enum Issue<'a> {
9550 /// Created by this write, so it holds no relationships yet.
9551 Created,
9552 /// On the board before this write, holding whatever relationships it holds — the far
9553 /// ends of its whole `blockedBy`, when the read that reached it carried them.
9554 Existing(Option<&'a [Value]>),
9555}
9556
9557/// What resolving one node id reached; see [`GitHubProjectsSource::reach`].
9558enum Reached {
9559 /// An issue this board holds, resolved into everything this source reports about it.
9560 Held(Box<Resolved>),
9561 /// Nothing this board holds: no such node, or a node on some other board.
9562 Nothing,
9563 /// A board draft, which [`graphql::ISSUE`] reaches and reads nothing of, so it is read
9564 /// again by [`GitHubProjectsSource::draft_by_id`].
9565 Draft,
9566}
9567
9568/// What GitHub says when a string is not a node id it can resolve.
9569///
9570/// Matched because it is the ordinary answer to a project selector naming a project by its
9571/// *name*, and reporting that as a failure would make naming one impossible. It is read
9572/// off the refusal GitHub sent, never guessed from the shape of the string: this source
9573/// does not define the syntax of a GitHub node id and would be wrong about it.
9574const UNRESOLVABLE_NODE: &str = "could not resolve to a node";
9575
9576/// `error` with `note` added to the end of what it says, its kind and every other member
9577/// unchanged — so a caller still branches on the failure that happened, and reads beside it
9578/// what that failure left behind.
9579fn noting(error: SourceError, note: &str) -> SourceError {
9580 match error {
9581 SourceError::Config { message } => SourceError::Config {
9582 message: message + note,
9583 },
9584 SourceError::Auth { message } => SourceError::Auth {
9585 message: message + note,
9586 },
9587 SourceError::Refused { message } => SourceError::Refused {
9588 message: message + note,
9589 },
9590 SourceError::RateLimited {
9591 retry_after_seconds,
9592 message,
9593 } => SourceError::RateLimited {
9594 retry_after_seconds,
9595 message: Some(message.unwrap_or_default() + note),
9596 },
9597 SourceError::Unavailable { message } => SourceError::Unavailable {
9598 message: message + note,
9599 },
9600 SourceError::Malformed { message } => SourceError::Malformed {
9601 message: message + note,
9602 },
9603 }
9604}
9605
9606/// The variables of one [`graphql::ISSUE_COPY_DETAILS`] request, as [`detail_batch`] binds its
9607/// own: a slot `batch` has no id for is bound to its last id, read again at no added price.
9608fn copy_detail_batch(batch: &[NativeId], comments: &PageRequest) -> Value {
9609 let mut variables = serde_json::Map::new();
9610 for slot in 0..COPY_DETAIL_BATCH {
9611 let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
9612 variables.insert(format!("id{slot}"), json!(id));
9613 }
9614 variables.insert("first".to_owned(), json!(comments.limit.min(MAX_PAGE_SIZE)));
9615 variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
9616 variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
9617 variables.insert("duplicates".to_owned(), json!(true));
9618 Value::Object(variables)
9619}
9620
9621/// The variables of one [`graphql::ISSUE_DETAILS`] request over `batch` — at most
9622/// [`DETAIL_BATCH`] ids — each item with the first page of its comments when `comments` asks
9623/// for them.
9624///
9625/// The document is fixed-size, so a slot `batch` has no id for is bound to its last id, which
9626/// is read again at no added price.
9627fn detail_batch(batch: &[NativeId], comments: Option<&PageRequest>) -> Value {
9628 let mut variables = serde_json::Map::new();
9629 for slot in 0..DETAIL_BATCH {
9630 let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
9631 variables.insert(format!("id{slot}"), json!(id));
9632 }
9633 variables.insert(
9634 "first".to_owned(),
9635 json!(comments.map_or(MAX_PAGE_SIZE, |page| page.limit.min(MAX_PAGE_SIZE))),
9636 );
9637 variables.insert("comments".to_owned(), json!(comments.is_some()));
9638 variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
9639 variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
9640 variables.insert("duplicates".to_owned(), json!(true));
9641 Value::Object(variables)
9642}
9643
9644/// Whether this refusal is GitHub saying the id names no node at all.
9645fn unresolvable_node(error: &SourceError) -> bool {
9646 matches!(error, SourceError::Refused { message }
9647 if message.to_ascii_lowercase().contains(UNRESOLVABLE_NODE))
9648}
9649
9650/// One project name, as a search qualifier which filters on it at the server.
9651///
9652/// Quoted so the whole title is one phrase rather than a bag of words, with the two
9653/// characters GitHub's own quoting grammar gives a meaning inside a quoted phrase escaped
9654/// the way it documents. A title matched here is still compared for equality afterwards:
9655/// the qualifier narrows what the server sends, and this source decides what it names.
9656fn title_qualifier(name: &str) -> String {
9657 format!("in:title {}", quoted(name))
9658}
9659
9660/// `value` as one quoted phrase of a GitHub search or a board filter, with the two
9661/// characters GitHub's quoting grammar gives a meaning inside a quoted phrase escaped the way
9662/// it documents — so a value holding a qualifier's spelling is searched for rather than
9663/// obeyed.
9664fn quoted(value: &str) -> String {
9665 let escaped = value.replace('\\', "\\\\").replace('"', "\\\"");
9666 format!("\"{escaped}\"")
9667}
9668
9669/// The search qualifier for the issues updated at or after `since`.
9670///
9671/// Written to the second, rounded down, which can only widen what the search returns.
9672fn updated_qualifier(since: DateTime<Utc>) -> String {
9673 format!("updated:>={}", since.format("%Y-%m-%dT%H:%M:%S+00:00"))
9674}
9675
9676/// The search terms that narrow a board-scoped issue search to a task query's text and
9677/// metadata predicates, or `None` when it carries neither.
9678///
9679/// The text is one quoted phrase, searched `in:title`, `in:body` or both as its fields say,
9680/// and each metadata value is one more quoted phrase, which GitHub finds in the body because
9681/// its index covers the metadata comment the value is stored in. GitHub ANDs the phrases and
9682/// matches each in any field the `in:` qualifier names, so a query naming a title search and
9683/// a metadata value searches both fields for both — wider than asked, never narrower, and
9684/// every candidate is confirmed in process afterwards.
9685///
9686/// **This narrows a text search, and that is this source's declared semantics.** GitHub
9687/// matches whole tokens where a substring rule would match inside a word, so an item holding
9688/// the text only inside a longer word is not returned. A text of nothing but whitespace
9689/// matches every item, so it narrows nothing and is not sent.
9690fn narrowing_qualifiers(query: &TaskQuery) -> Option<String> {
9691 let text = query
9692 .text
9693 .as_ref()
9694 .filter(|text| !text.terms.trim().is_empty());
9695 if text.is_none() && query.metadata.is_empty() {
9696 return None;
9697 }
9698 let (title, body) = match text.map(|text| text.fields) {
9699 None => (false, true),
9700 Some(TextFields::Title) => (true, !query.metadata.is_empty()),
9701 Some(TextFields::Content) => (false, true),
9702 Some(TextFields::TitleOrContent) => (true, true),
9703 };
9704 let fields = match (title, body) {
9705 (true, true) => "in:title,body",
9706 (true, false) => "in:title",
9707 _ => "in:body",
9708 };
9709 let phrases = text
9710 .map(|text| text.terms.clone())
9711 .into_iter()
9712 .chain(
9713 query
9714 .metadata
9715 .iter()
9716 .map(|wanted| as_stored(wanted.value())),
9717 )
9718 .map(|phrase| quoted(&phrase))
9719 .collect::<Vec<_>>();
9720 Some(format!("{fields} {}", phrases.join(" ")))
9721}
9722
9723/// The search terms that narrow a board-scoped issue search to a project or document query's
9724/// text, or `None` when it has none or a blank one: the phrase, in the fields, a task query
9725/// carrying that text alone is sent as by [`narrowing_qualifiers`].
9726fn text_qualifiers(text: Option<&TextQuery>) -> Option<String> {
9727 narrowing_qualifiers(&TaskQuery {
9728 text: text.cloned(),
9729 ..TaskQuery::default()
9730 })
9731}
9732
9733/// Refuses a project or document query's text GitHub's issue search cannot find, before
9734/// anything is asked of GitHub, on exactly the terms [`refuse_unsearchable`] refuses a task
9735/// query's.
9736fn refuse_unsearchable_text(text: Option<&TextQuery>) -> Result<(), SourceError> {
9737 refuse_unsearchable(&TaskQuery {
9738 text: text.cloned(),
9739 ..TaskQuery::default()
9740 })
9741}
9742
9743/// Refuses a task query naming a text or a metadata value GitHub's issue search cannot find,
9744/// before anything is asked of GitHub.
9745///
9746/// GitHub's index holds words, so a phrase with no letter or digit names none to find, and no
9747/// bounded query answers it: sent, GitHub's answer to it is nothing this source may rely on;
9748/// left out, the search is every issue of the board. So this source says it cannot answer
9749/// rather than reading the board or answering nothing. A blank text is not refused: it narrows
9750/// nothing GitHub could search for, and keeps the board read it always had.
9751fn refuse_unsearchable(query: &TaskQuery) -> Result<(), SourceError> {
9752 const WHY: &str = "GitHub's issue search indexes words, so it cannot answer a value with no \
9753 letter or digit with a bounded query";
9754 if let Some(text) = &query.text
9755 && !text.terms.trim().is_empty()
9756 && !has_words(&text.terms)
9757 {
9758 return Err(SourceError::Refused {
9759 message: format!(
9760 "cannot search for the text {:?}: {WHY}; search for a text holding a letter or a digit",
9761 text.terms
9762 ),
9763 });
9764 }
9765 if let Some(wanted) = query
9766 .metadata
9767 .iter()
9768 .find(|wanted| !has_words(wanted.value()))
9769 {
9770 return Err(SourceError::Refused {
9771 message: format!(
9772 "cannot filter by the metadata value {:?} at {:?}: {WHY}; filter by a value holding a letter or a digit",
9773 wanted.value(),
9774 std::iter::once(wanted.key())
9775 .chain(wanted.path().iter().map(String::as_str))
9776 .collect::<Vec<_>>()
9777 .join("/"),
9778 ),
9779 });
9780 }
9781 Ok(())
9782}
9783
9784/// Whether GitHub's index could hold a word of `phrase`: whether it has a letter or a digit.
9785fn has_words(phrase: &str) -> bool {
9786 phrase.chars().any(char::is_alphanumeric)
9787}
9788
9789/// `value` spelled the way the metadata slot stores it: as the inside of its JSON string.
9790///
9791/// What GitHub indexes is the slot's JSON text, so a value holding a character JSON escapes —
9792/// a newline, a tab, a quote — is found by the escape the body holds and not by the character,
9793/// which GitHub's word match would read as different words.
9794fn as_stored(value: &str) -> String {
9795 let encoded = Value::String(value.to_owned()).to_string();
9796 encoded[1..encoded.len() - 1].to_owned()
9797}
9798
9799/// The one narrower question a task query carrying a text, metadata or origin predicate is
9800/// sent as.
9801enum Narrowing {
9802 /// Every carrier of this origin: [`graphql::ORIGIN_LOOKUP`].
9803 Origin(String),
9804 /// The board-scoped issue search narrowed by these qualifiers.
9805 Search(String),
9806}
9807
9808impl Narrowing {
9809 /// What this question is remembered under for the length of one command.
9810 fn key(&self) -> String {
9811 match self {
9812 Self::Origin(origin) => format!("origin {origin}"),
9813 Self::Search(also) => format!("search {also}"),
9814 }
9815 }
9816}
9817
9818/// Where one connection of [`graphql::ORIGIN_LOOKUP`] resumes.
9819enum Resumed {
9820 /// It reported another page, which starts after this cursor.
9821 More(String),
9822 /// It has ended. Sending this cursor again — the page's own end when it had one, and
9823 /// otherwise the cursor it was reached from — answers an empty page, so the one document
9824 /// can go on walking the other connection.
9825 Ended(Option<String>),
9826}
9827
9828impl Resumed {
9829 /// Whether the connection has another page.
9830 const fn has_more(&self) -> bool {
9831 matches!(self, Self::More(_))
9832 }
9833
9834 /// The cursor to send this connection next.
9835 fn cursor(self) -> Option<String> {
9836 match self {
9837 Self::More(next) => Some(next),
9838 Self::Ended(last) => last,
9839 }
9840 }
9841}
9842
9843/// Where `connection`, reached from `after`, resumes — refused when it reports another page
9844/// with no cursor to it, or from a cursor that does not advance.
9845fn resumed(connection: &Value, after: Option<&str>) -> Result<Resumed, SourceError> {
9846 let info = connection
9847 .get("pageInfo")
9848 .ok_or_else(|| SourceError::Malformed {
9849 message: "GitHub connection has no pageInfo".into(),
9850 })?;
9851 let end = optional_str(info, "endCursor")?;
9852 if required_bool(info, "hasNextPage")? {
9853 let next = end.ok_or_else(|| SourceError::Malformed {
9854 message: "GitHub connection reports another page and no endCursor".into(),
9855 })?;
9856 validate_cursor_progress(after, next)?;
9857 return Ok(Resumed::More(next.to_owned()));
9858 }
9859 Ok(Resumed::Ended(
9860 end.map(str::to_owned).or_else(|| after.map(str::to_owned)),
9861 ))
9862}
9863
9864/// The board, and every item on it this source reports.
9865#[derive(Clone)]
9866struct Board {
9867 id: String,
9868 fields: Value,
9869 items: Vec<Resolved>,
9870}
9871
9872/// What a write needs of the board and nothing more: its node id and its field
9873/// definitions, in the shape a read of the board's own `fields` gives them.
9874///
9875/// Deliberately no items. A write decides which item it writes, which parent it files
9876/// under and which far ends it names by reading each of them by its own id; this is the
9877/// half of the board those reads cannot carry, and holding no item is what keeps it from
9878/// ever being asked whether an item is there.
9879#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
9880struct BoardFields {
9881 id: BoardId,
9882 fields: Value,
9883}
9884
9885/// A board's node id: what a field write and `addProjectV2ItemById` address.
9886///
9887/// Never blank, because a blank one addresses no board — so an id GitHub answers blank is
9888/// refused where it is read, and one an item names blank is read as not named at all.
9889#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
9890#[serde(try_from = "String")]
9891struct BoardId(String);
9892
9893impl TryFrom<String> for BoardId {
9894 type Error = String;
9895
9896 /// The same refusal [`BoardId::parse`] makes, for an id read back from a kept context.
9897 fn try_from(id: String) -> Result<Self, Self::Error> {
9898 Self::parse(&id).map_err(|error| error.to_string())
9899 }
9900}
9901
9902/// Where one write left its item, for the record the rest of the command reads it out of.
9903///
9904/// A named record rather than a tuple because the update arm and the create arm each fill
9905/// all four, and two `Option`s of different meaning side by side in a tuple are two
9906/// positions a reader has to count.
9907struct Landed {
9908 /// The issue's own node id, which is the [`NativeId`] this source reports.
9909 content_id: NativeId,
9910 /// The board item's id, which is what a field write addresses.
9911 // 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.
9912 item_id: String,
9913 /// The web address GitHub gave the issue, when it gave one.
9914 // 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.
9915 url: Option<String>,
9916 /// The issue's number on its repository, when GitHub reported one.
9917 number: Option<u64>,
9918}
9919
9920impl BoardId {
9921 fn parse(id: &str) -> Result<Self, SourceError> {
9922 if id.trim().is_empty() {
9923 return Err(SourceError::Malformed {
9924 message: "GitHub named a board with a blank node id".into(),
9925 });
9926 }
9927 Ok(Self(id.to_owned()))
9928 }
9929
9930 fn as_str(&self) -> &str {
9931 &self.0
9932 }
9933}
9934
9935impl Board {
9936 fn field<'a>(fields: &'a Value, name: &str) -> Result<Option<&'a Value>, SourceError> {
9937 complete_connection(fields, "project fields", NESTED_PAGE_SIZE)?;
9938 let nodes = fields
9939 .get("nodes")
9940 .and_then(Value::as_array)
9941 .ok_or_else(|| SourceError::Malformed {
9942 message: "GitHub project fields.nodes is not an array".into(),
9943 })?;
9944 Ok(nodes
9945 .iter()
9946 .find(|field| field.get("name").and_then(Value::as_str) == Some(name)))
9947 }
9948}
9949
9950/// One board item, resolved into everything this source reports about it.
9951///
9952/// Serializable because it is what [`TaskSource::write_context`] hands the store's cache and
9953/// [`TaskSource::prime_write`] takes back — see [`expected`].
9954#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
9955struct Resolved {
9956 item_id: String,
9957 id: NativeId,
9958 content_kind: ContentKind,
9959 kind: BoardKind,
9960 title: String,
9961 body: Option<String>,
9962 /// The body exactly as GitHub holds it, metadata slot and all, which is what a write
9963 /// that changes the slot alone has to keep byte for byte outside it.
9964 raw_body: Option<String>,
9965 status: Status,
9966 /// The name of the board `Status` option this item sits in, as the board spells it.
9967 option: Option<String>,
9968 /// What its `Priority` field says, read through this instance's mapping.
9969 priority: HeldPriority,
9970 /// Whether this item's issue is closed. A draft has no such state and is never closed.
9971 closed: bool,
9972 /// GitHub's reason for a closed issue's closure, as it spells it — `None` for an open
9973 /// issue, a draft, and an issue closed before GitHub recorded reasons.
9974 #[serde(default)]
9975 reason: Option<String>,
9976 /// The tasks this one delivers, read out of its slot. Empty for anything not a task.
9977 delivers: Vec<TaskRef>,
9978 /// Every task that delivers this one, read out of its slot. Empty for anything not a
9979 /// task.
9980 delivered_by: Vec<TaskRef>,
9981 labels: Vec<Label>,
9982 parent: Option<NativeId>,
9983 // 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.
9984 origin: Option<String>,
9985 /// What each board text field this instance projects metadata onto holds for this item,
9986 /// by field name — only the fields holding a value, an empty text included. What a write compares the
9987 /// item's metadata against, so a field already holding its value is not written again;
9988 /// never read back as metadata, which the slot alone reports.
9989 projected: BTreeMap<String, String>,
9990 /// The issue's own number on its repository, as GitHub reports it.
9991 ///
9992 /// `None` in exactly two cases: a draft, which has no number at all — `DraftIssue`
9993 /// declares none, and a draft is not filed in a repository to be numbered by one — and
9994 /// an issue this run created whose creating mutation answered without one, which is a
9995 /// response GitHub's own schema says cannot happen and which a landed write is not
9996 /// worth failing over. An `Issue` read off the board always has one.
9997 number: Option<u64>,
9998 url: Option<String>,
9999 created_at: Option<DateTime<Utc>>,
10000 updated_at: Option<DateTime<Utc>>,
10001 /// When the board item itself last changed — what a board field write moves — when the
10002 /// read that reached it said.
10003 item_updated_at: Option<DateTime<Utc>>,
10004 /// How many comments the issue holds, when the read that reached it said.
10005 comment_count: Option<u64>,
10006 /// How many relationship events the issue's timeline holds — every dependency, as
10007 /// `blockedBy` or `blocking`, and every project membership, as its parent, added or
10008 /// removed — when the read that reached it said.
10009 relationship_events: Option<u64>,
10010 /// The server's clock on the response that read it — its `Date` header — when the
10011 /// response carried one.
10012 served_at: Option<DateTime<Utc>>,
10013 /// The digest of its [`change_token::Facets`] — its own kind, its board field values and
10014 /// its labels — when the read that reached it carried all three whole.
10015 #[serde(default)]
10016 facets: Option<change_token::FacetsDigest>,
10017 own_repository: Option<Repository>,
10018 repositories: Vec<Repository>,
10019 classification: Classification,
10020 slot: BTreeMap<String, Value>,
10021 /// The node id of the board this item sits on, when the read that reached it said.
10022 board_id: Option<String>,
10023 /// The definition of every board field this item holds a value of, in the shape a read
10024 /// of the board's own `fields` gives one.
10025 ///
10026 /// Only the fields this item has a value in: a field it holds nothing of is not here,
10027 /// which says nothing about whether the board has it.
10028 fields: Vec<Value>,
10029 /// Every field the board this item sits on defines, as its own read of the board's
10030 /// `fields` gives them — when the read that reached the item carried them, which a read
10031 /// of it by its own id does. What a write of it needs of the board, then, needs no read
10032 /// of the board.
10033 board_fields: Option<Value>,
10034 /// The far ends of this issue's whole `blockedBy` connection, each as a dependency read
10035 /// selects one — when the read that reached it carried the connection to its end, which a
10036 /// read of it by its own id does for any issue blocked by no more than a page. What a
10037 /// write reconciles that relationship against, and what a read of its forward edges in
10038 /// the same command answers with.
10039 blocked_by: Option<Vec<Value>>,
10040}
10041
10042impl Resolved {
10043 /// The board this item's own read names it on, when that read named one this source can
10044 /// address.
10045 fn named_board(&self) -> Option<BoardId> {
10046 self.board_id
10047 .as_deref()
10048 .and_then(|id| BoardId::parse(id).ok())
10049 }
10050
10051 /// The board's id and every field it defines, when the read that reached this item
10052 /// carried both — which a read of it by its own id does.
10053 fn carried_board(&self) -> Option<BoardFields> {
10054 Some(BoardFields {
10055 id: self.named_board()?,
10056 fields: self.board_fields.clone()?,
10057 })
10058 }
10059
10060 /// Whether this item holds a value of the board field called `name`, and so carries
10061 /// that field's definition. `false` says nothing about whether the board has the field.
10062 fn defines(&self, name: &str) -> bool {
10063 self.fields
10064 .iter()
10065 .any(|field| field.get("name").and_then(Value::as_str) == Some(name))
10066 }
10067
10068 /// The metadata a caller sees: their own keys, plus the copy origin this source keeps
10069 /// in a field of its own, and none of the five keys that are only an encoding.
10070 ///
10071 /// The two delivery keys are left out for every kind, not only for a task: they are
10072 /// the encoding of [`Task::delivers`] and [`Task::delivered_by`], and a project or a
10073 /// document carrying one holds nothing a caller's own metadata could mean by it.
10074 fn metadata(&self) -> BTreeMap<String, Value> {
10075 let mut metadata = self.slot.clone();
10076 metadata.remove(Repository::METADATA_KEY);
10077 metadata.remove(DependencyEdge::RECORDED_KEY);
10078 metadata.remove(ItemKind::METADATA_KEY);
10079 metadata.remove(TaskRef::DELIVERS_KEY);
10080 metadata.remove(TaskRef::DELIVERED_BY_KEY);
10081 metadata.remove(Classification::METADATA_KEY);
10082 // The board field is the origin, and the body's copy of it is only a mirror for the
10083 // issue search to find: an item whose field holds none has none, whatever its body
10084 // says, so no reader ever sees two answers.
10085 metadata.remove(ORIGIN_KEY);
10086 if let Some(origin) = &self.origin {
10087 metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin.clone()));
10088 }
10089 metadata
10090 }
10091
10092 /// Where this item is, as a link a reader can open.
10093 ///
10094 /// A board is a hosted place and every issue on it has a web address, so that address
10095 /// is what "where is this?" means here — and [`Location::Url`] is what says which kind
10096 /// of place it is, so a reader knows to open it rather than to read a file out. It
10097 /// does not replace or derive from `url`: the field goes on reporting exactly what it
10098 /// reported before, and this says what that address *is*.
10099 ///
10100 /// An item GitHub gave no `url` for — a draft has none — reports no location at all
10101 /// rather than a third variant, which is the contract's "the source did not say". An
10102 /// issue this run created is not one of those: its address comes back from the
10103 /// creating mutation, so it is somewhere a reader can open from the moment it exists
10104 /// rather than from whenever the board read catches up.
10105 fn location(&self) -> Option<Location> {
10106 self.url.clone().map(Location::Url)
10107 }
10108
10109 /// The short handle this board's backend shows people for a task: the issue's number
10110 /// alone, as a decimal string.
10111 ///
10112 /// The number alone rather than `owner/repo#1043`, because that is the contract's
10113 /// value for this backend. A draft has no number and so no handle, which is the
10114 /// contract's *absent* rather than a handle of some other shape — and the native
10115 /// [`Task::id`] here is the issue's GraphQL node id, which this neither replaces nor
10116 /// derives from.
10117 fn key(&self) -> Option<String> {
10118 self.number.map(|number| number.to_string())
10119 }
10120
10121 /// This item's change token — see [`change_token`] — or `None` when the read that
10122 /// reached it did not carry all six of its parts, which a draft never does, did not say
10123 /// when GitHub served it, or found it changed too recently to have settled.
10124 fn version(&self) -> Option<String> {
10125 match self.content_kind {
10126 ContentKind::Issue => {
10127 let (issue, item) = (self.updated_at?, self.item_updated_at?);
10128 let parts = change_token::Parts {
10129 issue,
10130 item,
10131 comments: self.comment_count?,
10132 relationships: self.relationship_events?,
10133 facets: self.facets.clone()?,
10134 repository: self.own_repository.clone()?,
10135 };
10136 change_token::settled(issue, item, self.served_at?)
10137 .then(|| change_token::spell(&parts))
10138 }
10139 ContentKind::DraftIssue => None,
10140 }
10141 }
10142
10143 /// Whether its `Priority` field holds a value at all, mapped or not.
10144 fn holds_priority(&self) -> bool {
10145 self.priority != HeldPriority::Read(Priority::None)
10146 }
10147
10148 /// The task this item is.
10149 ///
10150 /// Fails for an item whose `Priority` field holds an option the mapping does not name:
10151 /// reading that as a level would be a guess, and reading it as `none` would let the next
10152 /// copy clear a priority a person set.
10153 fn task(&self) -> Result<Task, SourceError> {
10154 let priority = match &self.priority {
10155 HeldPriority::Read(priority) => *priority,
10156 HeldPriority::Unmapped(option) => {
10157 return Err(SourceError::Malformed {
10158 message: format!(
10159 "task {}{} sits in the board {PRIORITY_FIELD} option {option:?}, which \
10160 this source's priority_mapping does not name, so its priority cannot be \
10161 read; next: name {option:?} under priority_mapping, or move the item to \
10162 a mapped option",
10163 self.id,
10164 self.number
10165 .map(|number| format!(" (#{number})"))
10166 .unwrap_or_default()
10167 ),
10168 });
10169 }
10170 };
10171 Ok(Task {
10172 version: self.version(),
10173 id: self.id.clone(),
10174 key: self.key(),
10175 title: self.title.clone(),
10176 content: self.body.clone(),
10177 status: self.status.clone(),
10178 priority,
10179 labels: self.labels.clone(),
10180 project: self.parent.clone(),
10181 url: self.url.clone(),
10182 location: self.location(),
10183 created_at: self.created_at,
10184 updated_at: self.updated_at,
10185 metadata: self.metadata(),
10186 repositories: self.repositories.clone(),
10187 delivers: self.delivers.clone(),
10188 delivered_by: self.delivered_by.clone(),
10189 classification: self.classification,
10190 })
10191 }
10192
10193 fn project(&self) -> Project {
10194 Project {
10195 version: self.version(),
10196 id: self.id.clone(),
10197 title: self.title.clone(),
10198 content: self.body.clone(),
10199 status: self.status.clone(),
10200 labels: self.labels.clone(),
10201 url: self.url.clone(),
10202 location: self.location(),
10203 created_at: self.created_at,
10204 updated_at: self.updated_at,
10205 metadata: self.metadata(),
10206 repositories: self.repositories.clone(),
10207 classification: self.classification,
10208 }
10209 }
10210
10211 /// The same issue as a document: the project it is filed under, and no status and no
10212 /// dependencies, because a document is not work.
10213 fn document(&self) -> Document {
10214 Document {
10215 version: self.version(),
10216 id: self.id.clone(),
10217 title: self.title.clone(),
10218 content: self.body.clone(),
10219 project: self.parent.clone(),
10220 labels: self.labels.clone(),
10221 url: self.url.clone(),
10222 location: self.location(),
10223 created_at: self.created_at,
10224 updated_at: self.updated_at,
10225 metadata: self.metadata(),
10226 repositories: self.repositories.clone(),
10227 classification: self.classification,
10228 }
10229 }
10230}
10231
10232/// Where one targeted update moves an item's status, and which of its two halves move.
10233struct StatusMove {
10234 /// The board the item's `Status` field is on.
10235 board: BoardId,
10236 /// The `Status` field's id.
10237 field: String,
10238 /// The option's id.
10239 option: String,
10240 /// The option's name, as the board spells it.
10241 name: String,
10242 /// What the status asks of the issue's state.
10243 target: StatusTarget,
10244 /// The status the item reads as once it is there.
10245 landed: Status,
10246 /// Which of the status's two halves differ from what the item holds.
10247 moves: Moves,
10248}
10249
10250/// Which halves of an item's status one targeted update moves: its `Status` option, the open or
10251/// closed state of its issue, or both. A status neither half of which differs is no move at all,
10252/// and is not a value of this type.
10253#[derive(Clone, Copy, PartialEq, Eq)]
10254enum Moves {
10255 /// The option alone.
10256 Option,
10257 /// The issue's state alone: open, closed, or closed with another reason.
10258 State,
10259 /// Both.
10260 Both,
10261}
10262
10263impl Moves {
10264 /// What differs, or `None` when nothing does.
10265 const fn of(option: bool, state: bool) -> Option<Self> {
10266 match (option, state) {
10267 (true, true) => Some(Self::Both),
10268 (true, false) => Some(Self::Option),
10269 (false, true) => Some(Self::State),
10270 (false, false) => None,
10271 }
10272 }
10273
10274 /// Whether the option moves.
10275 const fn option(self) -> bool {
10276 matches!(self, Self::Option | Self::Both)
10277 }
10278
10279 /// Whether the issue's state moves.
10280 const fn state(self) -> bool {
10281 matches!(self, Self::State | Self::Both)
10282 }
10283}
10284
10285/// What one write is, and the status that comes with being it.
10286///
10287/// One value rather than a [`BoardKind`] beside an `Option<Status>`: a document has no
10288/// status and a task or a project always has one, so "a document carrying a status" and
10289/// "a task carrying none" are states a write cannot be in rather than states every use
10290/// site below has to defend against.
10291enum Written<'a> {
10292 /// A document, which is not work and so has no status at all.
10293 Document,
10294 /// A task or a project, and the status it is being written with.
10295 Work(ItemKind, &'a Status),
10296}
10297
10298impl Written<'_> {
10299 /// Which of the board's three kinds this write is.
10300 const fn kind(&self) -> BoardKind {
10301 match self {
10302 Self::Document => BoardKind::Document,
10303 Self::Work(kind, _) => BoardKind::Work(*kind),
10304 }
10305 }
10306
10307 /// The status this write carries. A document carries none, so a write of one says
10308 /// nothing about the issue's open or closed state and selects no board `Status`
10309 /// option.
10310 const fn status(&self) -> Option<&Status> {
10311 match self {
10312 Self::Document => None,
10313 Self::Work(_, status) => Some(status),
10314 }
10315 }
10316
10317 /// The status this write carries with the kind whose half of `status_mapping` it is
10318 /// written through.
10319 const fn work_status(&self) -> Option<(ItemKind, &Status)> {
10320 match self {
10321 Self::Document => None,
10322 Self::Work(kind, status) => Some((*kind, status)),
10323 }
10324 }
10325}
10326
10327/// The item being written, in the one shape all three write methods reach.
10328struct Incoming<'a> {
10329 written: Written<'a>,
10330 /// The title a person wrote. A document's goes onto the issue with
10331 /// [`DESIGN_TITLE_PREFIX`] put back, so a round trip returns the title that went in.
10332 title: &'a str,
10333 content: Option<&'a str>,
10334 assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
10335 labels: &'a [Label],
10336 metadata: &'a BTreeMap<String, Value>,
10337 repositories: &'a [Repository],
10338 /// Recorded in the slot while private, and nowhere while public.
10339 classification: Classification,
10340 parent: Option<&'a NativeId>,
10341 /// [`Task::delivers`], already checked. Empty for a project or a document, which is
10342 /// what keeps either key out of their slot.
10343 delivers: &'a [TaskRef],
10344 /// [`Task::delivered_by`], already checked. Empty for a project or a document.
10345 delivered_by: &'a [TaskRef],
10346 /// [`Task::priority`], for a task written to an instance that holds one; `None` for a
10347 /// project, a document, and every write to an instance with no `priority_mapping` —
10348 /// which is what keeps such a write's requests exactly what they were before.
10349 priority: Option<Priority>,
10350}
10351
10352/// What one write does to one projected metadata text field, by the field's id.
10353#[derive(Debug, Clone)]
10354enum ProjectionWrite {
10355 /// Write this text.
10356 Set {
10357 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.
10358 text: String,
10359 },
10360 /// Clear the field's value.
10361 Clear { field: String }, // llmlint: ignore[invalid_states_unrepresentable] As above.
10362}
10363
10364impl ProjectionWrite {
10365 /// Add this write to a field write's list of values and list of clears.
10366 fn join<'a>(&'a self, values: &mut Vec<(String, Value)>, clears: &mut Vec<&'a str>) {
10367 match self {
10368 Self::Set { field, text } => values.push((field.clone(), json!({"text": text}))),
10369 Self::Clear { field } => clears.push(field),
10370 }
10371 }
10372}
10373
10374/// What one write does to an item's `Priority` field.
10375enum PriorityWrite {
10376 /// Select this option of this field.
10377 Select {
10378 /// The `Priority` field's id.
10379 field: String,
10380 /// The mapped option's id.
10381 option: String,
10382 },
10383 /// Clear the field's value, which is what `none` is.
10384 Clear {
10385 /// The `Priority` field's id.
10386 field: String,
10387 },
10388}
10389
10390impl Incoming<'_> {
10391 /// The title this write puts on the issue.
10392 fn written_title(&self) -> String {
10393 match self.written {
10394 Written::Document => format!("{DESIGN_TITLE_PREFIX}{}", self.title),
10395 Written::Work(..) => self.title.to_owned(),
10396 }
10397 }
10398}
10399
10400#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
10401enum ContentKind {
10402 DraftIssue,
10403 Issue,
10404}
10405
10406/// What one board issue is: a document, or the work an [`ItemKind`] names.
10407///
10408/// A type of this source's own rather than an `ItemKind` with a third variant, because
10409/// `ItemKind` names what a dependency endpoint points at and nothing may point at a
10410/// document — the contract keeps a document out of that enum deliberately. Holding the
10411/// board's three answers in one value is what makes every place that asks "which is this?"
10412/// answer all three, rather than a `document: bool` beside a `kind` that means nothing for
10413/// two thirds of the board.
10414#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
10415enum BoardKind {
10416 /// An issue whose title begins [`DESIGN_TITLE_PREFIX`].
10417 Document,
10418 /// Every other issue, and every draft.
10419 Work(ItemKind),
10420}
10421
10422impl BoardKind {
10423 /// The kind of an item titled `title`, filed under a project or not, holding `sub_issues`
10424 /// sub-issues and carrying the kind marker `marked` in its slot.
10425 ///
10426 /// The design prefix is read *first*, before either of the two rules that separate a
10427 /// project from a task. A document is not work whatever sub-issues it has and whatever
10428 /// marker it carries, and reading the prefix later would make a design issue with none of
10429 /// either an empty project. Being a sub-issue wins outright over the rest, and no marker
10430 /// overrides it: an issue filed under a project is that project's task even when it has
10431 /// sub-issues of its own.
10432 fn of(title: &str, filed: bool, sub_issues: u64, marked: Option<ItemKind>) -> Self {
10433 if title.starts_with(DESIGN_TITLE_PREFIX) {
10434 Self::Document
10435 } else if filed {
10436 Self::Work(ItemKind::Task)
10437 } else if sub_issues > 0 || marked == Some(ItemKind::Project) {
10438 Self::Work(ItemKind::Project)
10439 } else {
10440 Self::Work(ItemKind::Task)
10441 }
10442 }
10443
10444 /// What the change token's facets call this kind.
10445 const fn facet(self) -> change_token::FacetKind {
10446 match self {
10447 Self::Document => change_token::FacetKind::Document,
10448 Self::Work(ItemKind::Project) => change_token::FacetKind::Project,
10449 Self::Work(ItemKind::Task) => change_token::FacetKind::Task,
10450 }
10451 }
10452
10453 /// Whose half of `status_mapping` an item of this kind reads its status through. A
10454 /// document has no status of its own, so the task half stands in for whatever the issue
10455 /// holds; nothing reports it.
10456 const fn status_kind(self) -> ItemKind {
10457 match self {
10458 Self::Document => ItemKind::Task,
10459 Self::Work(kind) => kind,
10460 }
10461 }
10462
10463 /// How a refusal names this kind to the person reading it.
10464 const fn describes(self) -> &'static str {
10465 match self {
10466 Self::Document => "document",
10467 Self::Work(kind) => kind.marker(),
10468 }
10469 }
10470}
10471
10472/// Whether `labels` satisfies `filter`, matching by name, case-insensitively.
10473///
10474/// This is the local Markdown source's `labels_match`, spelled the same way on purpose:
10475/// the shared cross-source journeys assert one answer to one question, so two sources
10476/// that disagree about what "carries the label bug" means fail them.
10477fn labels_match(labels: &[Label], filter: &LabelFilter) -> bool {
10478 let holds = |name: &String| {
10479 labels
10480 .iter()
10481 .any(|label| label.name.eq_ignore_ascii_case(name))
10482 };
10483 (filter.any_of.is_empty() || filter.any_of.iter().any(holds))
10484 && filter.all_of.iter().all(holds)
10485 && !filter.none_of.iter().any(holds)
10486}
10487
10488/// Whether `category` is one of `statuses`. An empty list is unfiltered rather than
10489/// "keeps nothing", which is what lets a `Vec<StatusCategory>` spell no filter at all.
10490fn status_matches(category: StatusCategory, statuses: &[StatusCategory]) -> bool {
10491 statuses.is_empty() || statuses.contains(&category)
10492}
10493
10494/// Whether `title`/`content` satisfies `query`, matching case-insensitively.
10495///
10496/// `content` is the item's own prose — the body with this source's trailing metadata
10497/// comment already taken off — so a search never matches an encoding the author of the
10498/// issue never wrote.
10499fn text_matches(title: &str, content: Option<&str>, query: &TextQuery) -> bool {
10500 let terms = query.terms.to_lowercase();
10501 let in_title = title.to_lowercase().contains(&terms);
10502 let in_content = content.is_some_and(|body| body.to_lowercase().contains(&terms));
10503 match query.fields {
10504 TextFields::Title => in_title,
10505 TextFields::Content => in_content,
10506 TextFields::TitleOrContent => in_title || in_content,
10507 }
10508}
10509
10510/// Whether `task` satisfies `query`, with `project` deciding the project predicate.
10511///
10512/// The project predicate is passed separately because a read narrowed to one project has
10513/// already answered it by asking *that project* for its own items — and re-applying it
10514/// there would compare the caller's selector, which may be a project's **name**, against
10515/// the id of the project that name resolved to, and keep nothing. Every other read passes
10516/// `query.project` and applies it here, which is what keeps `projects` a predicate this
10517/// source really does apply.
10518fn task_matches(task: &Task, query: &TaskQuery, project: &ProjectFilter) -> bool {
10519 labels_match(&task.labels, &query.labels)
10520 && status_matches(task.status.category, &query.statuses)
10521 && (query.priorities.is_empty() || query.priorities.contains(&task.priority))
10522 && match project {
10523 ProjectFilter::Any => true,
10524 ProjectFilter::Orphans => task.project.is_none(),
10525 ProjectFilter::Is(id) => task.project.as_ref() == Some(id),
10526 }
10527 && query
10528 .text
10529 .as_ref()
10530 .is_none_or(|text| text_matches(&task.title, task.content.as_deref(), text))
10531 // Against the parsed metadata slot, and against the origin field, which is where
10532 // `Resolved::metadata` reads each of them from.
10533 && query.metadata_matches(&task.metadata)
10534 && query.origin_matches(&task.metadata)
10535}
10536
10537fn project_matches(project: &Project, query: &ProjectQuery) -> bool {
10538 labels_match(&project.labels, &query.labels)
10539 && status_matches(project.status.category, &query.statuses)
10540 && query
10541 .text
10542 .as_ref()
10543 .is_none_or(|text| text_matches(&project.title, project.content.as_deref(), text))
10544}
10545
10546/// The same three predicates a task query carries, minus the status filter.
10547///
10548/// A document is not work, so it has no status for one to compare against and the query
10549/// type carries none. The project predicate is the same one — a design issue filed under a
10550/// project issue is in that project, and one filed under nothing is in none — so it is
10551/// spelled the same way here rather than answered differently.
10552fn document_matches(document: &Document, query: &DocumentQuery, project: &ProjectFilter) -> bool {
10553 labels_match(&document.labels, &query.labels)
10554 && match project {
10555 ProjectFilter::Any => true,
10556 ProjectFilter::Orphans => document.project.is_none(),
10557 ProjectFilter::Is(id) => document.project.as_ref() == Some(id),
10558 }
10559 && query
10560 .text
10561 .as_ref()
10562 .is_none_or(|text| text_matches(&document.title, document.content.as_deref(), text))
10563}
10564
10565/// Why a GitHub item's change token does not vouch for its forward edges.
10566///
10567/// A forward edge names its far end and that far end's kind, and the near item's token moves
10568/// with the edges it holds but says nothing of the far ends themselves: a far end turning from a
10569/// task into a project when it gains a sub-issue, retitled into a document, or deleted moves
10570/// nothing the near token reads, so an edge kept at the near version could name a far end that
10571/// is no longer what it said.
10572const FAR_ENDS_UNCOVERED: &str = "a github item's change token does not attest a far end's kind, \
10573 whether it became a document, or its deletion, so its forward edges are always read from \
10574 github";
10575
10576/// The issue-search qualifier that keeps open issues alone, which a status filter naming only
10577/// categories an open issue reads as narrows a board-scoped search with.
10578const OPEN_QUALIFIER: &str = "is:open";
10579
10580/// The board filter `task status reconcile` reads the board through: closed items alone.
10581const CLOSED_FILTER: &str = "is:closed";
10582
10583/// The variables of one [`graphql::ISSUES_BY_REFERENCE`] request for `batch`: each slot names
10584/// one reference, and a slot the batch has none for names its last one again.
10585fn reference_batch(batch: &[IssueRef]) -> Value {
10586 let mut variables = serde_json::Map::new();
10587 for slot in 0..REFERENCE_BATCH {
10588 let issue = batch.get(slot).or(batch.last());
10589 variables.insert(format!("owner{slot}"), json!(issue.map(|i| &i.owner)));
10590 variables.insert(format!("name{slot}"), json!(issue.map(|i| &i.name)));
10591 variables.insert(format!("number{slot}"), json!(issue.map(|i| i.number)));
10592 }
10593 variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
10594 variables.insert("statusField".to_owned(), json!(STATUS_FIELD));
10595 variables.insert("duplicates".to_owned(), json!(true));
10596 Value::Object(variables)
10597}
10598
10599/// One issue an issue read by reference answered, in the shape every other read of an issue
10600/// hands its resolver: each board membership's `Status` value, read by name, as the one field
10601/// value that membership carries.
10602///
10603/// A membership's `status` is `null` when its card sits at no option — or on a board with no
10604/// field of that name — and otherwise an option value naming its option; anything else is not
10605/// an answer to this read, and is refused rather than read as a card at no option.
10606fn held_issue(issue: &Value) -> Result<Value, SourceError> {
10607 let malformed = |what: &str| SourceError::Malformed {
10608 message: format!("GitHub answered an issue read by reference with {what}"),
10609 };
10610 let mut issue = issue.clone();
10611 let nodes = issue
10612 .pointer_mut("/projectItems/nodes")
10613 .and_then(Value::as_array_mut)
10614 .ok_or_else(|| malformed("no list of board memberships"))?;
10615 for node in nodes {
10616 let node = node
10617 .as_object_mut()
10618 .ok_or_else(|| malformed("a board membership that is not an object"))?;
10619 let status = match node.get("status") {
10620 None => return Err(malformed("a board membership with no status member")),
10621 Some(Value::Null) => None,
10622 Some(status) if status.get("name").is_some_and(Value::is_string) => {
10623 Some(status.clone())
10624 }
10625 // A single-select value of another field type answers `{}`; only an option value
10626 // carries a name.
10627 Some(status) if status.as_object().is_some_and(serde_json::Map::is_empty) => None,
10628 Some(_) => return Err(malformed("a membership status that names no option")),
10629 };
10630 node.insert(
10631 "fieldValues".to_owned(),
10632 json!({"nodes": status.into_iter().collect::<Vec<_>>(),
10633 "pageInfo": {"hasNextPage": false}}),
10634 );
10635 }
10636 Ok(issue)
10637}
10638
10639/// The issue one slot of a read by reference answered, `None` when GitHub answered that
10640/// slot's repository or issue `null` — which [`GitHubProjectsSource::answer_unresolved`]
10641/// admitted only beside a `NOT_FOUND` naming that very slot — and a refusal when the answer
10642/// leaves the slot, or its `issue` member, out: an answer missing a member is malformed rather
10643/// than a reference nothing holds.
10644fn referenced_issue(data: &Value, slot: usize) -> Result<Option<&Value>, SourceError> {
10645 let alias = format!("r{slot}");
10646 let repository = data.get(&alias).ok_or_else(|| SourceError::Malformed {
10647 message: format!("GitHub answered a read by reference with no {alias}"),
10648 })?;
10649 if repository.is_null() {
10650 return Ok(None);
10651 }
10652 let issue = repository
10653 .get("issue")
10654 .ok_or_else(|| SourceError::Malformed {
10655 message: format!("GitHub answered {alias} of a read by reference with no issue"),
10656 })?;
10657 if issue.is_null() {
10658 return Ok(None);
10659 }
10660 // `Repository.issue` is typed `Issue`, so anything else is not an answer to this read.
10661 if optional_str(issue, "__typename")? != Some("Issue") {
10662 return Err(SourceError::Malformed {
10663 message: format!(
10664 "GitHub answered {alias} of a read by reference with an issue whose __typename \
10665 is not Issue"
10666 ),
10667 });
10668 }
10669 Ok(Some(issue))
10670}
10671
10672/// A closure reason as a person reads it: `NOT_PLANNED` as `not planned`.
10673fn spoken_reason(reason: &str) -> String {
10674 reason.to_ascii_lowercase().replace('_', " ")
10675}
10676
10677impl GitHubProjectsSource {
10678 /// Every task and project of this board whose card contradicts its issue's closure, each
10679 /// judged against its own kind's half of `status_mapping`, read through the board's own
10680 /// `is:closed` filter, page by page — never the board's unfiltered `items`.
10681 ///
10682 /// Each item the filter answers is confirmed from its own node: an open one, one whose
10683 /// closure reads `unknown`, and one whose card is already in line are left out whatever
10684 /// the filter let through. The board's fields ride along and are held for the option
10685 /// writes that follow, so those need no read of the board.
10686 async fn contradictions(&self) -> Result<Vec<StatusContradiction>, SourceError> {
10687 let mut after: Option<String> = None;
10688 let mut found = Vec::new();
10689 loop {
10690 let data = self
10691 .graphql(
10692 graphql::CLOSED_ITEMS,
10693 json!({"owner":self.owner,"number":self.project_number,
10694 "first":MAX_PAGE_SIZE,"after":after,"filter":CLOSED_FILTER,
10695 "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
10696 )
10697 .await?;
10698 let board = data
10699 .pointer("/closedItems/projectV2")
10700 .filter(|value| !value.is_null())
10701 .ok_or_else(|| SourceError::Refused {
10702 message: format!(
10703 "GitHub project {}/{} was not found or is not visible to the token",
10704 self.owner, self.project_number
10705 ),
10706 })?;
10707 *self.fields_cache()? = Some(BoardFields {
10708 id: BoardId::parse(required_str(board, "id")?)?,
10709 fields: board.get("fields").cloned().unwrap_or(Value::Null),
10710 });
10711 let items = board.get("items").ok_or_else(|| SourceError::Malformed {
10712 message: "GitHub project closed items are missing".into(),
10713 })?;
10714 for item in items
10715 .get("nodes")
10716 .and_then(Value::as_array)
10717 .ok_or_else(|| SourceError::Malformed {
10718 message: "GitHub project items.nodes is not an array".into(),
10719 })?
10720 {
10721 let Some(resolved) = self.resolve(item)? else {
10722 continue;
10723 };
10724 let BoardKind::Work(kind) = resolved.kind else {
10725 continue;
10726 };
10727 let Some((closed_as, name)) = self.statuses.contradicted(
10728 kind,
10729 resolved.option.as_deref(),
10730 resolved.closed,
10731 resolved.reason.as_deref(),
10732 ) else {
10733 continue;
10734 };
10735 found.push(StatusContradiction {
10736 closed_as,
10737 to: name.to_owned(),
10738 option: resolved.option.clone(),
10739 item: match kind {
10740 ItemKind::Task => ContradictedItem::Task(resolved.task()?),
10741 ItemKind::Project => ContradictedItem::Project(resolved.project()),
10742 },
10743 });
10744 }
10745 let info = items
10746 .get("pageInfo")
10747 .ok_or_else(|| SourceError::Malformed {
10748 message: "GitHub project items have no pageInfo".into(),
10749 })?;
10750 if !required_bool(info, "hasNextPage")? {
10751 return Ok(found);
10752 }
10753 let next = required_str(info, "endCursor")?;
10754 validate_cursor_progress(after.as_deref(), next)?;
10755 after = Some(next.to_owned());
10756 }
10757 }
10758
10759 /// What this board holds for each issue in `issues`, once each in the order given, read
10760 /// [`REFERENCE_BATCH`] references at a time — and nothing else of the board.
10761 ///
10762 /// An issue GitHub cannot find for this source's credential, and one with no item on this
10763 /// board, are both absent. A closure that already says the work will not be done here —
10764 /// not planned, duplicate — is skipped: a landing does not overrule it. An item closed as
10765 /// completed whose card sits at its kind's `done` option is unchanged, and every other item
10766 /// is due a `done` write. A project item — a plan's home card, its issue holding the plan's
10767 /// tasks as sub-issues — is held under exactly these rules, judged against the project
10768 /// kind's half of `status_mapping`.
10769 async fn holdings(&self, issues: &[IssueRef]) -> Result<Vec<IssueHolding>, SourceError> {
10770 let mut distinct: Vec<IssueRef> = Vec::with_capacity(issues.len());
10771 for issue in issues {
10772 if !distinct.contains(issue) {
10773 distinct.push(issue.clone());
10774 }
10775 }
10776 let done = |kind: ItemKind| {
10777 self.statuses
10778 .target(kind, StatusCategory::Done)
10779 .option()
10780 .unwrap_or("Done")
10781 .to_owned()
10782 };
10783 // GitHub numbers an issue with a GraphQL `Int`, a signed 32-bit value, so a reference past
10784 // its range names no issue any repository can hold: it is answered absent and never sent,
10785 // where binding it would have GitHub refuse the whole batch.
10786 let readable: Vec<IssueRef> = distinct
10787 .iter()
10788 .filter(|issue| i32::try_from(issue.number).is_ok())
10789 .cloned()
10790 .collect();
10791 let mut holdings = Vec::with_capacity(readable.len());
10792 for batch in readable.chunks(REFERENCE_BATCH) {
10793 let data = self
10794 .graphql(graphql::ISSUES_BY_REFERENCE, reference_batch(batch))
10795 .await?;
10796 for (slot, issue) in batch.iter().enumerate() {
10797 let node = referenced_issue(&data, slot)?;
10798 let resolved = match node {
10799 Some(node) => self.resolve_issue_as(&held_issue(node)?, false).await?,
10800 None => None,
10801 };
10802 // A task and a project are both held; a document is not work, so nothing is.
10803 let Some((item, kind)) = resolved.and_then(|item| match item.kind {
10804 BoardKind::Work(kind) => Some((item, kind)),
10805 BoardKind::Document => None,
10806 }) else {
10807 holdings.push(IssueHolding {
10808 issue: issue.clone(),
10809 completion: Completion::Absent,
10810 });
10811 continue;
10812 };
10813 let task = match kind {
10814 ItemKind::Task => {
10815 let held = item.task()?;
10816 HeldTask {
10817 id: held.id,
10818 kind,
10819 option: item.option.clone(),
10820 classification: held.classification,
10821 repositories: held.repositories,
10822 delivers: held.delivers,
10823 }
10824 }
10825 // A project delivers no task, so what its write keeps in step is nothing.
10826 ItemKind::Project => {
10827 let held = item.project();
10828 HeldTask {
10829 id: held.id,
10830 kind,
10831 option: item.option.clone(),
10832 classification: held.classification,
10833 repositories: held.repositories,
10834 delivers: Vec::new(),
10835 }
10836 }
10837 };
10838 let done = done(kind);
10839 let completion = match (item.closed, item.reason.as_deref()) {
10840 (true, Some(reason @ ("NOT_PLANNED" | "DUPLICATE"))) => Completion::Skipped {
10841 task,
10842 reason: format!("closed as {}", spoken_reason(reason)),
10843 },
10844 (true, None | Some("COMPLETED"))
10845 if item
10846 .option
10847 .as_deref()
10848 .is_some_and(|option| option.eq_ignore_ascii_case(&done)) =>
10849 {
10850 Completion::Unchanged { task }
10851 }
10852 _ => {
10853 let kept = item.clone();
10854 self.held_cache
10855 .lock()
10856 .map_err(|_| SourceError::Unavailable {
10857 message: "items read by issue reference were left \
10858 inconsistent; run the command again"
10859 .into(),
10860 })?
10861 .insert(kept.id.clone(), kept);
10862 Completion::Due {
10863 task,
10864 to: done.clone(),
10865 }
10866 }
10867 };
10868 holdings.push(IssueHolding {
10869 issue: issue.clone(),
10870 completion,
10871 });
10872 }
10873 }
10874 let mut read = holdings.into_iter();
10875 let mut answered = Vec::with_capacity(distinct.len());
10876 for issue in distinct {
10877 if i32::try_from(issue.number).is_ok() {
10878 answered.push(read.next().ok_or_else(|| SourceError::Malformed {
10879 message: format!("no holding was read for {issue}"),
10880 })?);
10881 } else {
10882 answered.push(IssueHolding {
10883 issue,
10884 completion: Completion::Absent,
10885 });
10886 }
10887 }
10888 Ok(answered)
10889 }
10890}
10891
10892#[async_trait::async_trait]
10893impl TaskSource for GitHubProjectsSource {
10894 fn kind(&self) -> &'static str {
10895 KIND
10896 }
10897 fn order_versions(&self, a: &str, b: &str) -> Option<std::cmp::Ordering> {
10898 change_token::order(a, b)
10899 }
10900 /// A task's fields and comments: §4.13d of `docs/plugin-protocol.md` states, member by
10901 /// member, the change each part of the token moves for. Not its forward edges — see
10902 /// [`FAR_ENDS_UNCOVERED`].
10903 fn version_coverage(&self) -> VersionCoverage {
10904 VersionCoverage {
10905 forward_edges: Coverage::Uncovered {
10906 reason: FAR_ENDS_UNCOVERED.to_owned(),
10907 },
10908 ..VersionCoverage::whole()
10909 }
10910 }
10911 /// Every part, for a copy's skip: the relationship count moves for an edge added or removed
10912 /// and for a far end deleted, so the token sees every change to a task's edges but one — a
10913 /// far end's own kind flipping, by gaining a sub-issue or being retitled into a document,
10914 /// which `spike-coverage` measured stale 1 of 1. A skipping recopy does not carry that, and
10915 /// the next copy that reads the task does (`docs/plugin-protocol.md` §4.13d).
10916 // 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.
10917 fn copy_version_coverage(&self) -> VersionCoverage {
10918 VersionCoverage::whole()
10919 }
10920 /// A project's fields, and not its forward edges, for the same reason a task's are not.
10921 fn project_version_coverage(&self) -> ProjectVersionCoverage {
10922 ProjectVersionCoverage {
10923 fields: Coverage::Covered,
10924 forward_edges: Coverage::Uncovered {
10925 reason: FAR_ENDS_UNCOVERED.to_owned(),
10926 },
10927 }
10928 }
10929 /// A document's fields and the manifest of its image assets, which its metadata slot
10930 /// records: both are the issue's body, whose every edit moves the token's issue time.
10931 fn document_version_coverage(&self) -> DocumentVersionCoverage {
10932 DocumentVersionCoverage::whole()
10933 }
10934 fn write_context(&self, id: &NativeId) -> Option<Value> {
10935 self.context_of(id)
10936 }
10937 fn board_context(&self) -> Option<Value> {
10938 self.board_context_held()
10939 }
10940 fn prime_write(&self, id: &NativeId, context: &Value, board: Option<&Value>) -> bool {
10941 self.prime(id, context, board)
10942 }
10943 fn unprime(&self, id: &NativeId) {
10944 self.unprime_held(id);
10945 }
10946 fn begin_journal(&self) {
10947 self.expected.begin();
10948 }
10949 fn end_journal(&self) -> Option<WriteJournal> {
10950 Some(self.expected.end())
10951 }
10952 fn capabilities(&self) -> Capabilities {
10953 Capabilities {
10954 projects: Support::Native,
10955 documents: Support::Native,
10956 comments: Support::Native,
10957 assets: Support::Native,
10958 priority: if self.priorities.is_some() {
10959 Support::Native
10960 } else {
10961 Support::Unsupported
10962 },
10963 filter_by_priority: Support::Native,
10964 filter_by_comment_activity: Support::Native,
10965 filter_by_metadata: Support::Native,
10966 filter_by_origin: Support::Native,
10967 reconcile_status: Support::Native,
10968 complete_by_issue: Support::Native,
10969 orphan_tasks: Support::Native,
10970 filter_by_label: Support::Native,
10971 filter_by_status: Support::Native,
10972 search_title: Support::Native,
10973 search_content: Support::Native,
10974 task_dependencies: DependencySupport::BothDirections,
10975 project_dependencies: DependencySupport::BothDirections,
10976 max_page_size: MAX_PAGE_SIZE,
10977 }
10978 }
10979 async fn visibility(
10980 &self,
10981 target: &onetaskgraph_plugin_api::WriteTarget<'_>,
10982 ) -> Result<onetaskgraph_plugin_api::Visibility, SourceError> {
10983 self.write_visibility(target).await
10984 }
10985 async fn health(&self) -> Result<Health, SourceError> {
10986 let board = self.board_page(None, 1).await?;
10987 Ok(Health {
10988 reachable: true,
10989 detail: Some(format!(
10990 "reading GitHub project {}/{} ({})",
10991 self.owner,
10992 self.project_number,
10993 required_str(&board, "title")?
10994 )),
10995 })
10996 }
10997 async fn get_task(&self, id: &NativeId) -> Result<Option<Task>, SourceError> {
10998 self.item_by_id(id)
10999 .await?
11000 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
11001 .map(|item| item.task())
11002 .transpose()
11003 }
11004 async fn task_assets(
11005 &self,
11006 id: &NativeId,
11007 ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
11008 self.held_assets(id, BoardKind::Work(ItemKind::Task)).await
11009 }
11010 async fn task_asset(
11011 &self,
11012 id: &NativeId,
11013 name: &onetaskgraph_plugin_api::AssetName,
11014 ) -> Result<Option<Vec<u8>>, SourceError> {
11015 self.held_asset(id, BoardKind::Work(ItemKind::Task), name)
11016 .await
11017 }
11018 async fn set_task_rendering_with_assets(
11019 &self,
11020 id: &NativeId,
11021 content: &str,
11022 provenance: &Value,
11023 _answers: &BTreeMap<String, Value>,
11024 assets: &onetaskgraph_plugin_api::AssetWrite,
11025 ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
11026 self.replace_rendering(
11027 id,
11028 BoardKind::Work(ItemKind::Task),
11029 content,
11030 provenance,
11031 Some(assets),
11032 )
11033 .await
11034 }
11035 async fn document_assets(
11036 &self,
11037 id: &NativeId,
11038 ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
11039 self.held_assets(id, BoardKind::Document).await
11040 }
11041 async fn document_asset(
11042 &self,
11043 id: &NativeId,
11044 name: &onetaskgraph_plugin_api::AssetName,
11045 ) -> Result<Option<Vec<u8>>, SourceError> {
11046 self.held_asset(id, BoardKind::Document, name).await
11047 }
11048 async fn set_document_rendering_with_assets(
11049 &self,
11050 id: &NativeId,
11051 content: &str,
11052 provenance: &Value,
11053 _answers: &BTreeMap<String, Value>,
11054 assets: &onetaskgraph_plugin_api::AssetWrite,
11055 ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
11056 self.replace_rendering(id, BoardKind::Document, content, provenance, Some(assets))
11057 .await
11058 }
11059 async fn get_project(&self, id: &NativeId) -> Result<Option<Project>, SourceError> {
11060 Ok(self
11061 .item_by_id(id)
11062 .await?
11063 .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
11064 .map(|item| item.project()))
11065 }
11066 async fn query_tasks(
11067 &self,
11068 query: &TaskQuery,
11069 page: &PageRequest,
11070 ) -> Result<Page<Task>, SourceError> {
11071 validate_page(page)?;
11072 refuse_unsearchable(query)?;
11073 // A closed issue reads `done`, `cancelled` or — closed for a reason that implies no
11074 // category — `unknown`; every other category reads only off an open issue or a draft. So
11075 // a status filter naming none of those three asks every issue search it sends for open
11076 // issues alone; each candidate's category is still confirmed from its own item below.
11077 let open_only = !query.statuses.is_empty()
11078 && query.statuses.iter().all(|category| {
11079 !matches!(
11080 category,
11081 StatusCategory::Done | StatusCategory::Cancelled | StatusCategory::Unknown
11082 )
11083 });
11084 if query.origin.is_none() && !matches!(query.project, ProjectFilter::Is(_)) {
11085 let mut qualifiers: Vec<String> = [
11086 query.commented_since.map(updated_qualifier),
11087 narrowing_qualifiers(query),
11088 ]
11089 .into_iter()
11090 .flatten()
11091 .collect();
11092 // A narrowed search never answers a draft, which is no issue, so narrowing it to
11093 // open issues too leaves out nothing such a search could have answered.
11094 if !qualifiers.is_empty() {
11095 if open_only {
11096 qualifiers.push(OPEN_QUALIFIER.to_owned());
11097 }
11098 return self.search_tasks(query, page, &qualifiers.join(" ")).await;
11099 }
11100 }
11101
11102 // A read narrowed to one project asks that project for its own tasks, so nothing
11103 // about it costs what the rest of the board holds. A read carrying a text, metadata
11104 // or origin predicate asks GitHub the narrower question those predicates are, and a
11105 // read narrowed to comment activity alone asks the board's own issue search for the
11106 // issues updated since, which is every issue a comment could have been written or
11107 // edited on since. Every other task read is a question about the whole board and is
11108 // answered by reading it.
11109 let (held, membership) = match (&query.project, query.commented_since) {
11110 (ProjectFilter::Is(project), _) => (
11111 self.project_children(project).await?,
11112 // Answered by where these items came from; see `task_matches`.
11113 &ProjectFilter::Any,
11114 ),
11115 (ProjectFilter::Any | ProjectFilter::Orphans, since) => {
11116 match (self.narrowed(query).await?, since) {
11117 (Some(narrowed), _) => (narrowed, &query.project),
11118 (None, Some(since)) => (self.updated_since(since).await?, &query.project),
11119 // The board's own items list every draft; only the search half, which
11120 // completes them with issues they lag behind on, is narrowed — a closed
11121 // issue that half leaves out is at no category the filter keeps.
11122 (None, None) if open_only => (
11123 self.board_completed_by(Some(OPEN_QUALIFIER)).await?.items,
11124 &query.project,
11125 ),
11126 (None, None) => (self.board().await?.items, &query.project),
11127 }
11128 }
11129 };
11130 // Filtered before paged: a page of a filtered result is a page of the survivors,
11131 // never the survivors of a page.
11132 let mut tasks = Vec::new();
11133 for item in held
11134 .iter()
11135 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
11136 {
11137 let task = item.task()?;
11138 if task_matches(&task, query, membership)
11139 && self.commented_since(item, query.commented_since).await?
11140 {
11141 tasks.push(task);
11142 }
11143 }
11144 Ok(offset_page(
11145 tasks,
11146 numeric_cursor(page.cursor.as_ref())?,
11147 page.limit.min(MAX_PAGE_SIZE) as usize,
11148 ))
11149 }
11150 async fn query_projects(
11151 &self,
11152 query: &ProjectQuery,
11153 page: &PageRequest,
11154 ) -> Result<Page<Project>, SourceError> {
11155 validate_page(page)?;
11156 refuse_unsearchable_text(query.text.as_ref())?;
11157 // The projects a board holds are found by an issue search scoped to that board,
11158 // never by walking the board's own item connection: what tells a project from a
11159 // task is the `parent` each issue carries, which costs nothing to read. A query
11160 // carrying a text asks that search for the text too, so it reads the issues that
11161 // hold it rather than every issue of the board.
11162 let held = match self.text_searched(query.text.as_ref()).await? {
11163 Some(searched) => searched,
11164 None => self.board_issues().await?,
11165 };
11166 let projects = held
11167 .iter()
11168 .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
11169 .map(Resolved::project)
11170 .filter(|project| project_matches(project, query))
11171 .collect();
11172 Ok(offset_page(
11173 projects,
11174 numeric_cursor(page.cursor.as_ref())?,
11175 page.limit.min(MAX_PAGE_SIZE) as usize,
11176 ))
11177 }
11178 async fn get_document(&self, id: &NativeId) -> Result<Option<Document>, SourceError> {
11179 Ok(self
11180 .item_by_id(id)
11181 .await?
11182 .filter(|item| item.kind == BoardKind::Document)
11183 .map(|item| item.document()))
11184 }
11185 async fn query_documents(
11186 &self,
11187 query: &DocumentQuery,
11188 page: &PageRequest,
11189 ) -> Result<Page<Document>, SourceError> {
11190 validate_page(page)?;
11191 // Narrowed to one project, this is the same sub-issue read a task list scoped to
11192 // that project makes — a document filed under a project is a sub-issue of it too,
11193 // and which of them come back is the kind this caller asked for. Unscoped, a query
11194 // carrying a text asks the board-scoped issue search for it, as a task query does,
11195 // and only one carrying none reads the board.
11196 let (held, membership) = match &query.project {
11197 ProjectFilter::Is(project) => (
11198 self.project_children(project).await?,
11199 // Answered by where these items came from; see `task_matches`.
11200 &ProjectFilter::Any,
11201 ),
11202 ProjectFilter::Any | ProjectFilter::Orphans => {
11203 refuse_unsearchable_text(query.text.as_ref())?;
11204 match self.text_searched(query.text.as_ref()).await? {
11205 Some(searched) => (searched, &query.project),
11206 None => (self.board().await?.items, &query.project),
11207 }
11208 }
11209 };
11210 // Filtered before paged, exactly as a task read is: a page of a filtered result is
11211 // a page of the survivors, never the survivors of a page.
11212 let documents = held
11213 .iter()
11214 .filter(|item| item.kind == BoardKind::Document)
11215 .map(Resolved::document)
11216 .filter(|document| document_matches(document, query, membership))
11217 .collect();
11218 Ok(offset_page(
11219 documents,
11220 numeric_cursor(page.cursor.as_ref())?,
11221 page.limit.min(MAX_PAGE_SIZE) as usize,
11222 ))
11223 }
11224 async fn labels(&self, page: &PageRequest) -> Result<Page<Label>, SourceError> {
11225 validate_page(page)?;
11226 let offset = numeric_cursor(page.cursor.as_ref())?;
11227 let mut labels = self
11228 .board()
11229 .await?
11230 .items
11231 .into_iter()
11232 .flat_map(|item| item.labels)
11233 .fold(Vec::new(), |mut all, label| {
11234 if !all.iter().any(|x: &Label| x.id == label.id) {
11235 all.push(label);
11236 }
11237 all
11238 });
11239 labels.sort_by(|a, b| a.name.cmp(&b.name).then(a.id.0.cmp(&b.id.0)));
11240 Ok(offset_page(
11241 labels,
11242 offset,
11243 page.limit.min(MAX_PAGE_SIZE) as usize,
11244 ))
11245 }
11246 async fn task_dependencies(
11247 &self,
11248 id: &NativeId,
11249 direction: Direction,
11250 page: &PageRequest,
11251 ) -> Result<Page<DependencyEdge>, SourceError> {
11252 self.dependencies(id, ItemKind::Task, direction, page).await
11253 }
11254 async fn project_dependencies(
11255 &self,
11256 id: &NativeId,
11257 direction: Direction,
11258 page: &PageRequest,
11259 ) -> Result<Page<DependencyEdge>, SourceError> {
11260 self.dependencies(id, ItemKind::Project, direction, page)
11261 .await
11262 }
11263
11264 fn writes(&self) -> WriteSupport {
11265 WriteSupport::Supported
11266 }
11267
11268 /// Create or update one task.
11269 ///
11270 /// Its `delivers` and `delivered_by` are checked before anything is read or written —
11271 /// neither may name the task itself or name one task twice — and land in the body's
11272 /// metadata slot under their reserved keys, in place of any caller metadata of those
11273 /// names.
11274 async fn write_task(&self, write: &ItemWrite<Task>) -> Result<NativeId, SourceError> {
11275 self.write_task_assets(write, None)
11276 .await
11277 .map(|written| written.id)
11278 }
11279
11280 async fn write_task_with_assets(
11281 &self,
11282 write: &ItemWrite<Task>,
11283 _answers: Option<&BTreeMap<String, Value>>,
11284 assets: &onetaskgraph_plugin_api::AssetWrite,
11285 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
11286 self.write_task_assets(write, Some(assets)).await
11287 }
11288
11289 async fn write_project(&self, write: &ItemWrite<Project>) -> Result<NativeId, SourceError> {
11290 self.write_item(
11291 &Incoming {
11292 written: Written::Work(ItemKind::Project, &write.item.status),
11293 title: &write.item.title,
11294 content: write.item.content.as_deref(),
11295 assets: None,
11296 labels: &write.item.labels,
11297 metadata: &write.item.metadata,
11298 repositories: &write.item.repositories,
11299 classification: write.item.classification,
11300 parent: None,
11301 delivers: &[],
11302 delivered_by: &[],
11303 priority: None,
11304 },
11305 write.target.as_ref(),
11306 &write.depends_on,
11307 )
11308 .await
11309 .map(|written| written.id)
11310 }
11311
11312 /// Create or update one document, which is one issue titled the way this board spells
11313 /// a document.
11314 ///
11315 /// Everything else is exactly a task write: caller metadata goes to the same canonical
11316 /// JSON slot at the end of the body and comes back with its JSON types intact, a key
11317 /// or a field this board cannot carry is refused by name rather than dropped, a target
11318 /// naming an issue this board does not hold is refused rather than created, and an
11319 /// issue this call created is taken back when the rest of the write fails.
11320 async fn write_document(&self, write: &ItemWrite<Document>) -> Result<NativeId, SourceError> {
11321 self.write_document_assets(write, None)
11322 .await
11323 .map(|written| written.id)
11324 }
11325
11326 async fn write_document_with_assets(
11327 &self,
11328 write: &ItemWrite<Document>,
11329 _answers: Option<&BTreeMap<String, Value>>,
11330 assets: &onetaskgraph_plugin_api::AssetWrite,
11331 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
11332 self.write_document_assets(write, Some(assets)).await
11333 }
11334
11335 /// Refused exactly as the write refuses it, from what the write reads: the mapping first,
11336 /// which reads nothing; then the board's `Status` option. Over an existing item that is
11337 /// read off the item, as the write reads it, and the item is held among this command's
11338 /// resolved records so the write that follows reuses that read rather than repeating it;
11339 /// an item that does not carry the field takes the board's fields, which are held once
11340 /// read. A create is checked against the board's fields only when this command already
11341 /// holds them, because a create reads them together with its repository, in one request,
11342 /// and refuses a missing option before it writes anything.
11343 async fn check_status_write(
11344 &self,
11345 kind: ItemKind,
11346 category: StatusCategory,
11347 target: Option<&NativeId>,
11348 ) -> Result<(), SourceError> {
11349 let status = self.resolved_target(kind, category)?;
11350 if status.option().is_none() {
11351 return Ok(());
11352 }
11353 let fields = match target {
11354 Some(target) => {
11355 // A target this board does not hold is the write's own refusal to make.
11356 let Some(item) = self.bound_item(target).await? else {
11357 return Ok(());
11358 };
11359 self.resolved_cache()?.insert(target.clone(), item.clone());
11360 self.fields_for(Some(&item), true, false).await?.fields
11361 }
11362 None => {
11363 let held = self
11364 .board_cache()?
11365 .as_ref()
11366 .map(|board| board.fields.clone());
11367 match held.or_else(|| {
11368 self.fields_cache()
11369 .ok()
11370 .and_then(|cache| cache.as_ref().map(|board| board.fields.clone()))
11371 }) {
11372 Some(fields) => fields,
11373 None => return Ok(()),
11374 }
11375 }
11376 };
11377 self.column_for(&fields, kind, category, &status)
11378 .map(|_| ())
11379 }
11380
11381 /// Set one task's status alone.
11382 ///
11383 /// An open target reopens a closed issue with an `updateIssue` carrying only its
11384 /// `stateInput`, then selects the board option with `updateProjectV2ItemFieldValue`; a
11385 /// terminal target selects its mapped option, then closes with its fixed reason. No
11386 /// request carries a title, a body or a label. The status
11387 /// answered is what [`BoardStatuses::status`] reads off the state just written, which is
11388 /// what a re-read reports.
11389 async fn set_task_status(
11390 &self,
11391 id: &NativeId,
11392 category: StatusCategory,
11393 ) -> Result<Option<Status>, SourceError> {
11394 self.set_status(id, ItemKind::Task, category).await
11395 }
11396
11397 /// Set one project's status alone, by exactly the paired write a task's status takes, for
11398 /// the project kind: a terminal category selects the project kind's mapped option and then
11399 /// closes the issue with the category's fixed reason — an issue already closed with a
11400 /// reason that reads that category is left as it is — and an open category reopens a closed
11401 /// issue and then selects its option. A board draft is refused a terminal category as a
11402 /// task's is.
11403 async fn set_project_status(
11404 &self,
11405 id: &NativeId,
11406 category: StatusCategory,
11407 ) -> Result<Option<Status>, SourceError> {
11408 self.set_status(id, ItemKind::Project, category).await
11409 }
11410
11411 /// Every task and project whose card contradicts its issue's closure, read through the
11412 /// board's own `is:closed` filter; see [`TaskSource::status_contradictions`].
11413 async fn status_contradictions(&self) -> Result<Vec<StatusContradiction>, SourceError> {
11414 self.contradictions().await
11415 }
11416
11417 /// What this board holds for each issue reference, one request per fifty of them; see
11418 /// [`TaskSource::issue_holdings`].
11419 async fn issue_holdings(&self, issues: &[IssueRef]) -> Result<Vec<IssueHolding>, SourceError> {
11420 self.holdings(issues).await
11421 }
11422
11423 /// Set one task's priority alone: one `updateProjectV2ItemFieldValue` selecting the
11424 /// mapped option of the board's `Priority` field, or one `clearProjectV2ItemFieldValue`
11425 /// for `none`. Refused by an instance with no `priority_mapping`.
11426 async fn set_task_priority(
11427 &self,
11428 id: &NativeId,
11429 priority: Priority,
11430 ) -> Result<Option<Priority>, SourceError> {
11431 self.set_priority(id, priority).await
11432 }
11433
11434 /// Replace one task's content with a single body update that keeps the metadata slot
11435 /// byte for byte.
11436 async fn set_task_content(
11437 &self,
11438 id: &NativeId,
11439 content: &str,
11440 ) -> Result<Option<()>, SourceError> {
11441 self.replace_content(id, content).await
11442 }
11443
11444 /// Replace one task issue's content and its provenance slot entry with a single body
11445 /// update. The answers are not kept: see `replace_rendering`.
11446 async fn set_task_rendering(
11447 &self,
11448 id: &NativeId,
11449 content: &str,
11450 provenance: &Value,
11451 _answers: &BTreeMap<String, Value>,
11452 ) -> Result<Option<()>, SourceError> {
11453 self.replace_rendering(
11454 id,
11455 BoardKind::Work(ItemKind::Task),
11456 content,
11457 provenance,
11458 None,
11459 )
11460 .await
11461 .map(|written| written.map(|_| ()))
11462 }
11463
11464 /// Replace one design-document issue's content and its provenance slot entry, on exactly
11465 /// the terms of [`set_task_rendering`](TaskSource::set_task_rendering).
11466 async fn set_document_rendering(
11467 &self,
11468 id: &NativeId,
11469 content: &str,
11470 provenance: &Value,
11471 _answers: &BTreeMap<String, Value>,
11472 ) -> Result<Option<()>, SourceError> {
11473 self.replace_rendering(id, BoardKind::Document, content, provenance, None)
11474 .await
11475 .map(|written| written.map(|_| ()))
11476 }
11477
11478 /// Replace one project issue's content and its provenance slot entry, on exactly the
11479 /// terms of [`set_task_rendering`](TaskSource::set_task_rendering).
11480 async fn set_project_rendering(
11481 &self,
11482 id: &NativeId,
11483 content: &str,
11484 provenance: &Value,
11485 _answers: &BTreeMap<String, Value>,
11486 ) -> Result<Option<()>, SourceError> {
11487 self.replace_rendering(
11488 id,
11489 BoardKind::Work(ItemKind::Project),
11490 content,
11491 provenance,
11492 None,
11493 )
11494 .await
11495 .map(|written| written.map(|_| ()))
11496 }
11497
11498 /// Apply a targeted update with one read of the item and a write only for what differs:
11499 /// the `Status` and `Priority` field writes in one request, the `blockedBy` difference,
11500 /// and last one `updateIssue` for title, body and state. See `targeted_update`.
11501 async fn update_task(
11502 &self,
11503 id: &NativeId,
11504 update: &TaskUpdate,
11505 ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
11506 self.targeted_update(id, update).await
11507 }
11508
11509 /// Replace one task's `delivered_by` with a single body update that changes the
11510 /// metadata slot and nothing outside it.
11511 async fn set_delivered_by(
11512 &self,
11513 id: &NativeId,
11514 delivered_by: &[TaskRef],
11515 ) -> Result<Option<()>, SourceError> {
11516 self.replace_delivered_by(id, delivered_by).await
11517 }
11518
11519 /// Set one key of one task issue's metadata with a single body update that changes the
11520 /// metadata slot and nothing outside it — no title, label, state or board field request —
11521 /// and sends nothing when the task already holds that value under the key.
11522 async fn set_task_metadata(
11523 &self,
11524 id: &NativeId,
11525 key: &MetadataKey,
11526 value: &Value,
11527 ) -> Result<Option<Task>, SourceError> {
11528 Ok(self
11529 .set_slot_key(id, BoardKind::Work(ItemKind::Task), key, value)
11530 .await?
11531 .map(|item| item.task())
11532 .transpose()?)
11533 }
11534
11535 /// Set one key of one project issue's metadata, on exactly the terms of
11536 /// [`set_task_metadata`](TaskSource::set_task_metadata).
11537 async fn set_project_metadata(
11538 &self,
11539 id: &NativeId,
11540 key: &MetadataKey,
11541 value: &Value,
11542 ) -> Result<Option<Project>, SourceError> {
11543 Ok(self
11544 .set_slot_key(id, BoardKind::Work(ItemKind::Project), key, value)
11545 .await?
11546 .map(|item| item.project()))
11547 }
11548
11549 /// Set one key of one design-document issue's metadata, on exactly the terms of
11550 /// [`set_task_metadata`](TaskSource::set_task_metadata).
11551 async fn set_document_metadata(
11552 &self,
11553 id: &NativeId,
11554 key: &MetadataKey,
11555 value: &Value,
11556 ) -> Result<Option<Document>, SourceError> {
11557 Ok(self
11558 .set_slot_key(id, BoardKind::Document, key, value)
11559 .await?
11560 .map(|item| item.document()))
11561 }
11562
11563 async fn delete_task(&self, id: &NativeId) -> Result<(), SourceError> {
11564 self.delete_item(id).await
11565 }
11566
11567 async fn delete_project(&self, id: &NativeId) -> Result<(), SourceError> {
11568 self.delete_item(id).await
11569 }
11570
11571 async fn delete_document(&self, id: &NativeId) -> Result<(), SourceError> {
11572 self.delete_item(id).await
11573 }
11574
11575 /// One page of the task issue's own comments, walked by GitHub's own cursor.
11576 ///
11577 /// Nothing here filters, so nothing has to be read ahead of the page: the caller's limit is
11578 /// the page GitHub is asked for and GitHub's `endCursor` is the cursor handed back.
11579 ///
11580 /// One request, [`graphql::ISSUE_DETAIL`]: the read that says the id names a task of this
11581 /// board is the read of its comments. A draft this process already resolved is refused
11582 /// without one.
11583 async fn task_comments(
11584 &self,
11585 task: &NativeId,
11586 page: &PageRequest,
11587 ) -> Result<Option<Page<Comment>>, SourceError> {
11588 validate_page(page)?;
11589 let cached = self.resolved_cache()?.get(task).cloned();
11590 if let Some(item) = cached {
11591 if item.kind != BoardKind::Work(ItemKind::Task) {
11592 return Ok(None);
11593 }
11594 if item.content_kind == ContentKind::DraftIssue {
11595 return Err(self.draft_has_no_comments(task));
11596 }
11597 }
11598 match self.issue_detail(task, page).await? {
11599 Some(TaskDetailRead {
11600 comments: Some(comments),
11601 ..
11602 }) => comments,
11603 _ => Ok(None),
11604 }
11605 }
11606
11607 /// Every id's task, with the first page of its comments when `comments` names it:
11608 /// [`DETAIL_BATCH`] items per [`graphql::ISSUE_DETAILS`] request, and one item with its
11609 /// comments in one [`graphql::ISSUE_DETAIL`] request.
11610 async fn get_task_details(
11611 &self,
11612 ids: &[NativeId],
11613 comments: Option<&PageRequest>,
11614 ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
11615 if let Some(page) = comments
11616 && let Err(error) = validate_page(page)
11617 {
11618 return ids.iter().map(|_| Err(error.clone())).collect();
11619 }
11620 match (ids, comments) {
11621 ([id], Some(page)) => vec![self.issue_detail(id, page).await],
11622 ([id], None) => vec![self.task_read(id).await],
11623 _ => self.issue_details(ids, comments).await,
11624 }
11625 }
11626
11627 /// Every id's task with the first page of its comments and its `blockedBy`, as a copy reads
11628 /// the tasks it carries: [`COPY_DETAIL_BATCH`] items per [`graphql::ISSUE_COPY_DETAILS`]
11629 /// request, so the dependency read a copy makes of each next is answered from this one.
11630 async fn get_task_details_for_copy(
11631 &self,
11632 ids: &[NativeId],
11633 comments: &PageRequest,
11634 ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
11635 // A page past the first is a page of one task's comments, which the detail read of it
11636 // alone answers.
11637 if comments.cursor.is_some() {
11638 return self.get_task_details(ids, Some(comments)).await;
11639 }
11640 if let Err(error) = validate_page(comments) {
11641 return ids.iter().map(|_| Err(error.clone())).collect();
11642 }
11643 let mut read = Vec::with_capacity(ids.len());
11644 for batch in ids.chunks(COPY_DETAIL_BATCH) {
11645 match self
11646 .graphql(
11647 graphql::ISSUE_COPY_DETAILS,
11648 copy_detail_batch(batch, comments),
11649 )
11650 .await
11651 {
11652 Ok(data) => {
11653 for (slot, id) in batch.iter().enumerate() {
11654 let read_one = match data.get(format!("i{slot}")) {
11655 Some(node) => self.detail_of(id, node, true, None).await,
11656 None => Err(SourceError::Malformed {
11657 message: format!(
11658 "GitHub answered a batch read with no item for {}",
11659 id.0
11660 ),
11661 }),
11662 };
11663 read.push(read_one);
11664 }
11665 }
11666 Err(error) if unresolvable_node(&error) => {
11667 for id in batch {
11668 read.push(self.issue_detail(id, comments).await);
11669 }
11670 }
11671 Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
11672 }
11673 }
11674 read
11675 }
11676
11677 /// Add one comment to the task's issue, as the account the token belongs to.
11678 ///
11679 /// The author is refused before anything is sent — not even the task is read — because
11680 /// no answer GitHub could give would make posting under another name than the one asked
11681 /// for the right outcome.
11682 async fn add_comment(
11683 &self,
11684 task: &NativeId,
11685 comment: &NewComment,
11686 ) -> Result<Option<Comment>, SourceError> {
11687 if let Some(author) = &comment.author {
11688 return Err(SourceError::Refused {
11689 message: format!(
11690 "source {} cannot post a comment as {author:?}: GitHub records the account \
11691 the token signs in as the author of every comment; next: leave --author \
11692 out, and the comment is posted as that account",
11693 self.name
11694 ),
11695 });
11696 }
11697 let Some(issue) = self.commented_issue(task).await? else {
11698 return Ok(None);
11699 };
11700 let data = self
11701 .graphql(
11702 graphql::ADD_COMMENT,
11703 json!({"input":{"subjectId":issue.0,"body":comment.body.as_str()}}),
11704 )
11705 .await?;
11706 let subject = data
11707 .pointer("/addComment/subject")
11708 .filter(|value| !value.is_null())
11709 .ok_or_else(|| SourceError::Malformed {
11710 message: "GitHub comment addition returned no subject".into(),
11711 })?;
11712 if required_str(subject, "id")? != issue.0 {
11713 return Err(SourceError::Malformed {
11714 message: "GitHub comment addition answered about another issue".into(),
11715 });
11716 }
11717 let added = data
11718 .pointer("/addComment/commentEdge/node")
11719 .filter(|value| !value.is_null())
11720 .ok_or_else(|| SourceError::Malformed {
11721 message: "GitHub comment addition returned no comment".into(),
11722 })?;
11723 let added = comment_from(added)?;
11724 self.remember_commented(&issue)?;
11725 Ok(Some(added))
11726 }
11727
11728 async fn edit_comment(
11729 &self,
11730 task: &NativeId,
11731 comment: &NativeId,
11732 body: &CommentBody,
11733 ) -> Result<Option<Comment>, SourceError> {
11734 let Some(issue) = self.commented_issue(task).await? else {
11735 return Ok(None);
11736 };
11737 let Some(held) = self.comment_on(&issue, comment).await? else {
11738 return Ok(None);
11739 };
11740 // A comment a copy wrote keeps its origin through an edit: its marker is carried over.
11741 let body = CommentOrigin::kept(&held, body)?;
11742 let data = self
11743 .graphql(
11744 graphql::UPDATE_COMMENT,
11745 json!({"input":{"id":comment.0,"body":body.as_str()}}),
11746 )
11747 .await?;
11748 let edited = data
11749 .pointer("/updateIssueComment/issueComment")
11750 .filter(|value| !value.is_null())
11751 .ok_or_else(|| SourceError::Malformed {
11752 message: "GitHub comment update returned no comment".into(),
11753 })?;
11754 let edited = comment_from(edited)?;
11755 if edited.id != *comment {
11756 return Err(SourceError::Malformed {
11757 message: "GitHub comment update returned the wrong comment".into(),
11758 });
11759 }
11760 self.remember_commented(&issue)?;
11761 Ok(Some(edited))
11762 }
11763
11764 async fn delete_comment(
11765 &self,
11766 task: &NativeId,
11767 comment: &NativeId,
11768 ) -> Result<Option<NativeId>, SourceError> {
11769 let Some(issue) = self.commented_issue(task).await? else {
11770 return Ok(None);
11771 };
11772 if !self.comment_is_on(&issue, comment).await? {
11773 return Ok(None);
11774 }
11775 let data = self
11776 .graphql(graphql::DELETE_COMMENT, json!({"input":{"id":comment.0}}))
11777 .await?;
11778 // The payload says nothing about the comment it removed, so what is checked is that
11779 // GitHub answered the mutation at all rather than leaving it unanswered.
11780 data.get("deleteIssueComment")
11781 .filter(|value| !value.is_null())
11782 .ok_or_else(|| SourceError::Malformed {
11783 message: "GitHub comment deletion returned no payload".into(),
11784 })?;
11785 Ok(Some(comment.clone()))
11786 }
11787
11788 /// Every request this source has recorded, and what each of GitHub's two budgets was
11789 /// attributed — read off the same accounting the session report is rendered from, so
11790 /// the two cannot count one request two ways.
11791 async fn metering(&self) -> Result<Option<Metering>, SourceError> {
11792 Ok(Some(self.ledger.snapshot().metering()))
11793 }
11794
11795 /// Drop every item, search answer and board read this source holds, so the next command
11796 /// reads the board as a person has since left it.
11797 ///
11798 /// Every one of those is held on the assumption that nothing but this source writes the
11799 /// board while a command runs, which stops being true the moment the command is over: a
11800 /// body a person edited would be overwritten from the record held here, and a card they
11801 /// moved would be read as still where this source left it. The board's own field
11802 /// definitions go too, because a person can add or delete a `Status` option and a write
11803 /// resolved against the held list would not re-read on a miss. What stays is what stays
11804 /// valid in normal use: each repository's node id, which a miss re-reads, the pacing of
11805 /// mutations, which is about GitHub's limiter rather than anybody's work, and the running
11806 /// accounting [`metering`](TaskSource::metering) answers from.
11807 ///
11808 /// Infallible in practice: a lock an earlier failure poisoned is cleared rather than
11809 /// refused, because clearing it is what puts it right.
11810 async fn end_command(&self) -> Result<(), SourceError> {
11811 fn clear<T: Default>(held: &Mutex<T>) {
11812 *held
11813 .lock()
11814 .unwrap_or_else(std::sync::PoisonError::into_inner) = T::default();
11815 held.clear_poison();
11816 }
11817 clear(&self.created);
11818 clear(&self.updated);
11819 clear(&self.commented);
11820 clear(&self.board_cache);
11821 clear(&self.search_cache);
11822 clear(&self.narrowed_cache);
11823 clear(&self.search_next);
11824 clear(&self.resolved_cache);
11825 clear(&self.held_cache);
11826 clear(&self.children_cache);
11827 clear(&self.fields_cache);
11828 self.expected.end_command();
11829 Ok(())
11830 }
11831}
11832
11833/// Each project's sub-issues as one command read them, keyed by the selector they were asked
11834/// for under, beside the project that selector named; see `children_cache`.
11835type ProjectChildren = BTreeMap<NativeId, (NativeId, Vec<Resolved>)>;
11836
11837/// One issue comment as the contract carries it.
11838///
11839/// `author` is absent both when GitHub answers `null` for an account that no longer exists
11840/// and when it answers an actor with no login, because either way the source did not say who
11841/// wrote it — which is what an absent author means, rather than an author called nothing.
11842///
11843/// A comment a copy wrote here carries its origin as the hidden marker at the end of its body
11844/// — GitHub sets every comment's author and times itself, so there is nowhere else to keep it —
11845/// and is read back with the marker taken out and reported as its origin.
11846fn comment_from(value: &Value) -> Result<Comment, SourceError> {
11847 Ok(Comment {
11848 origin: None,
11849 id: NativeId(required_str(value, "id")?.to_owned()),
11850 author: optional_str(value.get("author").unwrap_or(&Value::Null), "login")?
11851 .map(str::to_owned),
11852 created_at: optional_time(value, "createdAt")?,
11853 updated_at: optional_time(value, "updatedAt")?,
11854 body: required_str(value, "body")?.to_owned(),
11855 url: optional_str(value, "url")?.map(str::to_owned),
11856 }
11857 .unmarked())
11858}
11859
11860/// The page of comments one issue node carries, resumed from `after`.
11861fn comment_page(
11862 node: &Value,
11863 issue: &str,
11864 after: Option<&str>,
11865) -> Result<Page<Comment>, SourceError> {
11866 let connection = node
11867 .get("comments")
11868 .filter(|value| !value.is_null())
11869 .ok_or_else(|| SourceError::Malformed {
11870 message: format!("GitHub issue {issue} answered with no comments connection"),
11871 })?;
11872 let items = optional_nodes(Some(connection), "issue comments")?
11873 .into_iter()
11874 .flatten()
11875 .map(comment_from)
11876 .collect::<Result<Vec<_>, _>>()?;
11877 let next = next_cursor(connection)?;
11878 if let Some(next) = &next {
11879 validate_cursor_progress(after, &next.0)?;
11880 }
11881 Ok(Page { items, next })
11882}
11883
11884/// The far ends of an issue's whole `blockedBy` connection, when the read carried it to its
11885/// end — `None` when it carried none, or a page with more past it.
11886fn carried_blocked_by(content: &Value) -> Result<Option<Vec<Value>>, SourceError> {
11887 let Some(connection) = content.get("blockedBy").filter(|value| !value.is_null()) else {
11888 return Ok(None);
11889 };
11890 if next_cursor(connection)?.is_some() {
11891 return Ok(None);
11892 }
11893 Ok(Some(
11894 optional_nodes(Some(connection), "blocked-by issues")?
11895 .into_iter()
11896 .flatten()
11897 .cloned()
11898 .collect(),
11899 ))
11900}
11901
11902/// Where the recorded tail of a dependency walk resumes; see
11903/// [`GitHubProjectsSource::recorded_edges`].
11904const RECORDED_CURSOR: &str = "onetaskgraph.depends_on:";
11905
11906/// The board text field this source keeps a copy's origin in.
11907///
11908/// Named after the key it holds, and held to that name by the guard below rather than by
11909/// a reader noticing.
11910const ORIGIN_FIELD: &str = "onetaskgraph.origin";
11911
11912/// The metadata key that field holds.
11913///
11914/// The engine owns this key and spells it once as `GlobalId::ORIGIN_KEY`; a plugin never
11915/// constructs or interprets the qualified id it carries. This source names it only to
11916/// route it — a short, typed value belongs in a typed field rather than in the body slot
11917/// a caller's own prose shares.
11918///
11919/// Restated rather than imported, because no plugin crate may depend on the engine. What
11920/// keeps the two spellings one contract is `scripts/check-origin-key-spelling.sh`, a
11921/// target in `check`: it reads the engine's own literal and fails naming the file and the
11922/// line when a plugin's parts from it either way. Drift here has one symptom — a copy
11923/// that creates a second item every run instead of finding the one it wrote — and that is
11924/// too late to learn it.
11925const ORIGIN_KEY: &str = "onetaskgraph.origin";
11926
11927/// Where a recorded tail resumes, refusing a cursor no walk in `direction` reported.
11928///
11929/// The reserved key holds forward edges and nothing else — the reverse of a recorded edge
11930/// is derived from the far end, never written down on the near item — so only a forward
11931/// walk ever reports one of these cursors. A reverse read carrying one is resuming a walk
11932/// it did not come from, and it is told so rather than answered with an empty page that
11933/// reads as a walk which ended.
11934fn recorded_offset(
11935 cursor: Option<&str>,
11936 direction: Direction,
11937) -> Result<Option<usize>, SourceError> {
11938 cursor
11939 .and_then(|cursor| cursor.strip_prefix(RECORDED_CURSOR))
11940 .map(|offset| {
11941 if direction != Direction::DependsOn {
11942 return Err(SourceError::Config {
11943 message: format!(
11944 "{RECORDED_CURSOR}{offset} resumes recorded forward edges, which a \
11945 reverse dependency read never issues; resume it in the direction \
11946 that reported it"
11947 ),
11948 });
11949 }
11950 offset.parse().map_err(|_| SourceError::Config {
11951 message: format!("{RECORDED_CURSOR}{offset} is not a recorded-edge cursor"),
11952 })
11953 })
11954 .transpose()
11955}
11956
11957fn recorded_page(edges: Vec<DependencyEdge>, offset: usize, limit: usize) -> Page<DependencyEdge> {
11958 let mut page = offset_page(edges, offset, limit.max(1));
11959 page.next = page
11960 .next
11961 .map(|cursor| Cursor(format!("{RECORDED_CURSOR}{}", cursor.0)));
11962 page
11963}
11964
11965/// The kind of one issue reached through a dependency connection.
11966///
11967/// The same questions the board scan asks, over the fields the dependency document
11968/// selects, and in the same order: the design prefix first, then a sub-issue is a task,
11969/// then anything with sub-issues or the marker is a project.
11970///
11971/// # Errors
11972///
11973/// A far end this board holds as a document is refused rather than reported. The two
11974/// answers that are not refusals would both be wrong: reporting it as a task names an id
11975/// no task read of this source can find, and reporting it as a project names one no
11976/// project read can. There is no third value to return — `ItemKind` has no document
11977/// variant, because nothing may point at a document — so the relationship itself is what
11978/// the person is told about.
11979fn related_kind(value: &Value) -> Result<ItemKind, SourceError> {
11980 let id = required_str(value, "id")?;
11981 if required_str(value, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
11982 return Err(SourceError::Refused {
11983 message: format!(
11984 "GitHub issue {id} is a document of this board — its title begins \
11985 {DESIGN_TITLE_PREFIX:?} — and nothing may depend on a document or be depended \
11986 on by one; next: remove that issue's blocking relationship on this board"
11987 ),
11988 });
11989 }
11990 let parent = optional_str(value.get("parent").unwrap_or(&Value::Null), "id")?;
11991 if parent.is_some() {
11992 return Ok(ItemKind::Task);
11993 }
11994 let (_, slot) = metadata_body(optional_str(value, "body")?.map(str::to_owned))?;
11995 let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
11996 message: format!("GitHub issue {id}: {message}"),
11997 })?;
11998 let sub_issues = sub_issue_total(value)?;
11999 Ok(if sub_issues > 0 || marked == Some(ItemKind::Project) {
12000 ItemKind::Project
12001 } else {
12002 ItemKind::Task
12003 })
12004}
12005
12006/// The `IssueStateUpdateInput` one status target asks for.
12007///
12008/// `stateInput` and `state` are mutually exclusive on `UpdateIssueInput`, and only this
12009/// one is ever sent. A non-terminal status always asks for `OPEN`, which is what reopens
12010/// a currently-closed issue: without that the item would read back `Unknown` and a copy
12011/// would report a change forever. A document has no status at all, and asks for neither.
12012fn state_input(target: Option<&StatusTarget>) -> Value {
12013 match target {
12014 Some(StatusTarget::Terminal(_, reason)) => {
12015 json!({"value":"CLOSED","stateReason":reason.reason()})
12016 }
12017 Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => json!({"value":"OPEN"}),
12018 // A document has no status, so a write of one says nothing about the issue's open
12019 // or closed state rather than forcing it open: `stateInput` is what carries that
12020 // instruction, and an explicit null asks for no change to it.
12021 None => Value::Null,
12022 }
12023}
12024
12025/// The metadata one write stores in the item's body slot.
12026///
12027/// The typed fields travel as themselves, so the three reserved keys are rebuilt here
12028/// rather than carried: the kind marker so an empty project stays readable, the
12029/// repository list only when it is not exactly the issue's own repository, and the far
12030/// ends no relationship here can name.
12031///
12032/// The copy origin is the one typed field that is also mirrored here, and only as a
12033/// mirror: it lands in the board's origin field as well, which stays the one every reader
12034/// takes it from, and it is here so that GitHub's issue search — which indexes this comment
12035/// and catches up with a write in seconds rather than minutes — can find the item by it.
12036/// A reader of the release before this one drops the slot's copy and reads the field, so an
12037/// item written here still reads with exactly one origin there.
12038fn slot_metadata(
12039 incoming: &Incoming<'_>,
12040 own_repository: Option<&Repository>,
12041 fallback: &[DependencyEdge],
12042) -> BTreeMap<String, Value> {
12043 let mut metadata = incoming.metadata.clone();
12044 match metadata.remove(ORIGIN_KEY) {
12045 Some(Value::String(origin)) if !origin.is_empty() => {
12046 metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin));
12047 }
12048 _ => {}
12049 }
12050 match incoming.written.kind() {
12051 BoardKind::Work(kind) => metadata.insert(
12052 ItemKind::METADATA_KEY.to_owned(),
12053 Value::String(kind.marker().to_owned()),
12054 ),
12055 // A document is told by its title, so it carries no kind marker: that key names
12056 // what a dependency endpoint points at, and nothing may point at a document.
12057 BoardKind::Document => metadata.remove(ItemKind::METADATA_KEY),
12058 };
12059 incoming.classification.record(&mut metadata);
12060 let derivable = own_repository
12061 .map(|own| incoming.repositories == [own.clone()])
12062 .unwrap_or(incoming.repositories.is_empty());
12063 if derivable {
12064 metadata.remove(Repository::METADATA_KEY);
12065 } else {
12066 metadata.insert(
12067 Repository::METADATA_KEY.to_owned(),
12068 Value::Array(
12069 incoming
12070 .repositories
12071 .iter()
12072 .map(|repository| Value::String(repository.as_str().to_owned()))
12073 .collect(),
12074 ),
12075 );
12076 }
12077 // The typed lists are what land, whatever the caller's own metadata held under their
12078 // keys: a key of either name travelling beside the field would otherwise be a second
12079 // answer to the same question, and the field is the one the contract names.
12080 for (key, entries) in [
12081 (TaskRef::DELIVERS_KEY, incoming.delivers),
12082 (TaskRef::DELIVERED_BY_KEY, incoming.delivered_by),
12083 ] {
12084 set_task_list(&mut metadata, key, entries);
12085 }
12086 record_edges(&mut metadata, fallback);
12087 metadata
12088}
12089
12090/// Hold the far ends no relationship here can name under [`DependencyEdge::RECORDED_KEY`] in
12091/// one slot's metadata, or no such key when there are none.
12092fn record_edges(metadata: &mut BTreeMap<String, Value>, fallback: &[DependencyEdge]) {
12093 if fallback.is_empty() {
12094 metadata.remove(DependencyEdge::RECORDED_KEY);
12095 } else {
12096 metadata.insert(
12097 DependencyEdge::RECORDED_KEY.to_owned(),
12098 Value::Array(
12099 fallback
12100 .iter()
12101 .map(|edge| json!({"id":edge.to.id(),"kind":edge.to.kind}))
12102 .collect(),
12103 ),
12104 );
12105 }
12106}
12107
12108/// Every label one item carries, from its content's own connection and nowhere else.
12109///
12110/// There is no second place to read one from: no document this source sends selects the
12111/// board's built-in `Labels` field, because GitHub derives it from the content and a draft
12112/// cannot carry one at all. The module documentation records the three schema facts that
12113/// settle it.
12114fn labels(content: &Value) -> Result<Vec<Label>, SourceError> {
12115 optional_nodes(content.get("labels"), "content labels")?
12116 .into_iter()
12117 .flatten()
12118 .map(|v| {
12119 Ok(Label {
12120 id: NativeId(required_str(v, "id")?.to_owned()),
12121 name: required_str(v, "name")?.to_owned(),
12122 color: optional_str(v, "color")?.map(str::to_owned),
12123 })
12124 })
12125 .collect()
12126}
12127
12128/// Each board field value one item holds, as the change token's facets name it: the field's
12129/// name, with the single-select option's name or the text. A value of a type no read here
12130/// selects, and one naming no field, says nothing a person can rename and is left out.
12131fn facet_fields(field_values: &[Value]) -> Vec<(String, String)> {
12132 field_values
12133 .iter()
12134 .filter_map(|value| {
12135 let field = value.pointer("/field/name")?.as_str()?;
12136 let held = value.get("name").or_else(|| value.get("text"))?.as_str()?;
12137 Some((field.to_owned(), held.to_owned()))
12138 })
12139 .collect()
12140}
12141
12142/// The digest of the facets of the issue `content`, whose board item's values are
12143/// `field_values` — or `None` when the response did not carry, whole, every member they are read
12144/// from: the title, the parent, the sub-issue summary and the body its kind is read from, every
12145/// label, and every field value.
12146///
12147/// What a write's own response spells its token from, by exactly the rules a read takes them by.
12148fn facets_of(content: &Value, field_values: &Value) -> Option<change_token::FacetsDigest> {
12149 let ended = |connection: &Value| {
12150 connection.pointer("/pageInfo/hasNextPage") == Some(&Value::Bool(false))
12151 };
12152 let labels = content.get("labels").filter(|labels| ended(labels))?;
12153 if !ended(field_values) {
12154 return None;
12155 }
12156 let parent = content.get("parent")?;
12157 let (_, slot) = metadata_body(content.get("body")?.as_str().map(str::to_owned)).ok()?;
12158 let kind = BoardKind::of(
12159 content.get("title")?.as_str()?,
12160 !parent.is_null(),
12161 sub_issue_total(content).ok()?,
12162 ItemKind::from_metadata(&slot).ok()?,
12163 );
12164 let labels = self::labels(&json!({"labels": labels})).ok()?;
12165 let nodes = field_values.get("nodes")?.as_array()?;
12166 Some(change_token::Facets::new(kind.facet(), facet_fields(nodes), &labels).digest())
12167}
12168
12169/// The definition of each board field one item's values are values of, in the shape a read
12170/// of the board's own `fields` gives one.
12171///
12172/// A value names its field through a fragment on that field's own type, so the type is
12173/// known from which kind of value it is: a single-select value's field is a
12174/// `ProjectV2SingleSelectField`, options and all, and a text value's is a `ProjectV2Field`.
12175/// A value whose field carried no id, or an empty one, says nothing usable and is left out.
12176fn field_definitions(field_values: &[Value]) -> Vec<Value> {
12177 field_values
12178 .iter()
12179 .filter_map(|value| {
12180 let field = value.get("field")?.as_object()?;
12181 field.get("id")?.as_str().filter(|id| !id.is_empty())?;
12182 let typename = if value.get("text").is_some() {
12183 "ProjectV2Field"
12184 } else if value.get("name").is_some() {
12185 "ProjectV2SingleSelectField"
12186 } else {
12187 return None;
12188 };
12189 let mut defined = field.clone();
12190 defined.insert("__typename".to_owned(), json!(typename));
12191 // Only a text field holds a text value, so the value says the field's type too.
12192 if typename == "ProjectV2Field" {
12193 defined.insert("dataType".to_owned(), json!("TEXT"));
12194 }
12195 Some(Value::Object(defined))
12196 })
12197 .collect()
12198}
12199
12200fn text_field(field_values: &[Value], name: &str) -> Result<Option<String>, SourceError> {
12201 let Some(node) = field_values
12202 .iter()
12203 .find(|node| node.pointer("/field/name").and_then(Value::as_str) == Some(name))
12204 else {
12205 return Ok(None);
12206 };
12207 Ok(optional_str(node, "text")?.map(str::to_owned))
12208}
12209
12210fn valid_github_owner(owner: &str) -> bool {
12211 !owner.is_empty()
12212 && owner.len() <= 39
12213 && !owner.starts_with('-')
12214 && !owner.ends_with('-')
12215 && !owner.contains("--")
12216 && owner
12217 .bytes()
12218 .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
12219}
12220
12221/// GitHub's repository-name grammar: 1-100 ASCII letters, digits, `-`, `_` or `.`, and
12222/// neither of the two names a path segment already means.
12223fn valid_github_repository_name(name: &str) -> bool {
12224 !name.is_empty()
12225 && name.len() <= 100
12226 && name != "."
12227 && name != ".."
12228 && name
12229 .bytes()
12230 .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
12231}
12232
12233fn valid_environment_name(name: &str) -> bool {
12234 let mut bytes = name.bytes();
12235 bytes
12236 .next()
12237 .is_some_and(|byte| byte.is_ascii_alphabetic() || byte == b'_')
12238 && bytes.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
12239}
12240
12241/// How many sub-issues one issue has.
12242///
12243/// `Issue.subIssuesSummary` is `SubIssuesSummary!` and its `total` is `Int!`, so an
12244/// absent or non-integer one is a response this source cannot read — and reading it as
12245/// zero would classify a project as a task, which is exactly the mistake the marker
12246/// exists to keep from happening quietly.
12247fn sub_issue_total(issue: &Value) -> Result<u64, SourceError> {
12248 let summary = issue
12249 .get("subIssuesSummary")
12250 .ok_or_else(|| SourceError::Malformed {
12251 message: "GitHub issue is missing subIssuesSummary".into(),
12252 })?;
12253 summary
12254 .get("total")
12255 .and_then(Value::as_u64)
12256 .ok_or_else(|| SourceError::Malformed {
12257 message: "GitHub issue subIssuesSummary.total is not an unsigned integer".into(),
12258 })
12259}
12260
12261/// One issue's own `number`.
12262///
12263/// An issue always has one: GitHub declares `Issue.number` as `Int!` and every selection of
12264/// an issue in this module asks for it. So a read of one that comes back without it, or
12265/// with something that is not an unsigned integer, is a response this source cannot read —
12266/// absence here is **not** "this issue has no number". A draft is the content that has
12267/// none, and a draft never reaches this: the caller decides on `__typename` first, the way
12268/// it does for `subIssuesSummary`, which `DraftIssue` equally declares nothing for.
12269fn issue_number(issue: &Value) -> Result<u64, SourceError> {
12270 issue
12271 .get("number")
12272 .and_then(Value::as_u64)
12273 .ok_or_else(|| SourceError::Malformed {
12274 message: "GitHub issue number is missing or is not an unsigned integer".into(),
12275 })
12276}
12277
12278/// The `number` a creating mutation answered with, and `None` when it answered without one;
12279/// why a missing one is tolerated is at the call in `create_and_file_issue`.
12280fn created_issue_number(created: &Value) -> Result<Option<u64>, SourceError> {
12281 match created.get("number") {
12282 None | Some(Value::Null) => Ok(None),
12283 Some(value) => value
12284 .as_u64()
12285 .map(Some)
12286 .ok_or_else(|| SourceError::Malformed {
12287 message: "GitHub created issue number is not an unsigned integer".into(),
12288 }),
12289 }
12290}
12291
12292fn required_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
12293 value
12294 .get(field)
12295 .and_then(Value::as_str)
12296 .ok_or_else(|| SourceError::Malformed {
12297 message: format!("GitHub response is missing string field {field}"),
12298 })
12299}
12300
12301fn required_nonblank_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
12302 let found = required_str(value, field)?;
12303 if found.trim().is_empty() {
12304 return Err(SourceError::Malformed {
12305 message: format!("GitHub response has blank string field {field}"),
12306 });
12307 }
12308 Ok(found)
12309}
12310
12311/// The slot's delimiters, which `docs/metadata.md` settles once for every source that
12312/// needs one — Linear spells them too, in its own description field.
12313///
12314/// Restated rather than shared, because a plugin crate depends on the contract crate and
12315/// nothing else of this workspace. `scripts/check-metadata-slot-encoding.sh`, a target in
12316/// `check`, is what keeps the two one encoding: drift is otherwise quiet, since each
12317/// source round-trips its own writes perfectly well under its own spelling.
12318const METADATA_OPEN: &str = "<!-- onetaskgraph.metadata\n";
12319const METADATA_CLOSE: &str = "\n-->";
12320
12321/// What the composer puts between a non-empty visible body and the slot, and the one thing
12322/// the parser takes off the visible body when it takes the slot off — exactly once, so every
12323/// other trailing byte of the body comes back as it was written.
12324// 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.
12325const METADATA_SEPARATOR: &str = "\n\n";
12326
12327/// The visible body and the metadata slot at the end of it.
12328///
12329/// The encoding is the one `docs/metadata.md` settles for Linear, which is where its
12330/// reasons are. Only a comment at the very end is a slot; one in the middle is a person's
12331/// own content and is left alone. The visible body is everything before the slot less the
12332/// one [`METADATA_SEPARATOR`] the composer put there, byte for byte.
12333fn metadata_body(
12334 body: Option<String>,
12335) -> Result<(Option<String>, BTreeMap<String, Value>), SourceError> {
12336 let Some(body) = body else {
12337 return Ok((None, BTreeMap::new()));
12338 };
12339 let Some(slot) = slot_span(&body)? else {
12340 return Ok((Some(body), BTreeMap::new()));
12341 };
12342 let metadata =
12343 serde_json::from_str(&body[slot.encoded_start..slot.encoded_end]).map_err(|error| {
12344 SourceError::Malformed {
12345 message: format!(
12346 "invalid canonical JSON in GitHub issue onetaskgraph metadata slot: {error}"
12347 ),
12348 }
12349 })?;
12350 let before = &body[..slot.start];
12351 let visible = before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before);
12352 Ok(((!visible.is_empty()).then(|| visible.to_owned()), metadata))
12353}
12354
12355/// Where the metadata slot sits in one body, as byte offsets into it.
12356struct SlotSpan {
12357 /// Where [`METADATA_OPEN`] begins.
12358 start: usize,
12359 /// Where the encoded JSON begins, just past [`METADATA_OPEN`].
12360 encoded_start: usize,
12361 /// Where the encoded JSON ends, at the start of [`METADATA_CLOSE`].
12362 encoded_end: usize,
12363 /// Just past [`METADATA_CLOSE`].
12364 end: usize,
12365}
12366
12367/// The slot at the very end of `body`, or `None` when it has none.
12368///
12369/// The one reading of *where the slot is*, shared by [`metadata_body`], which reads it, and
12370/// [`with_slot`], which rewrites it — so the two cannot disagree about which comment is the
12371/// slot.
12372fn slot_span(body: &str) -> Result<Option<SlotSpan>, SourceError> {
12373 let Some(start) = body.rfind(METADATA_OPEN) else {
12374 return Ok(None);
12375 };
12376 let encoded_start = start + METADATA_OPEN.len();
12377 let Some(relative_end) = body[encoded_start..].find(METADATA_CLOSE) else {
12378 return Err(SourceError::Malformed {
12379 message: "unterminated onetaskgraph metadata slot in GitHub issue body".into(),
12380 });
12381 };
12382 let encoded_end = encoded_start + relative_end;
12383 let end = encoded_end + METADATA_CLOSE.len();
12384 if !body[end..].trim().is_empty() {
12385 return Ok(None);
12386 }
12387 Ok(Some(SlotSpan {
12388 start,
12389 encoded_start,
12390 encoded_end,
12391 end,
12392 }))
12393}
12394
12395/// `body` with its metadata slot holding exactly `metadata`, and every byte outside the
12396/// slot as it was.
12397///
12398/// A slot that is there has its JSON replaced in place; one that becomes empty is removed
12399/// together with the one [`METADATA_SEPARATOR`] separating it from the prose before it. A
12400/// body with no slot gains one the way [`compose_body`] writes it — after that separator,
12401/// or alone in an empty body — and a body with no slot that is given no metadata is
12402/// returned as it is.
12403fn with_slot(body: &str, metadata: &BTreeMap<String, Value>) -> Result<String, SourceError> {
12404 let encoded = if metadata.is_empty() {
12405 None
12406 } else {
12407 Some(
12408 serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
12409 message: error.to_string(),
12410 })?,
12411 )
12412 };
12413 Ok(match (slot_span(body)?, encoded) {
12414 (Some(slot), Some(encoded)) => format!(
12415 "{}{encoded}{}",
12416 &body[..slot.encoded_start],
12417 &body[slot.encoded_end..]
12418 ),
12419 (Some(slot), None) => {
12420 let before = &body[..slot.start];
12421 format!(
12422 "{}{}",
12423 before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before),
12424 &body[slot.end..]
12425 )
12426 }
12427 (None, None) => body.to_owned(),
12428 (None, Some(encoded)) if body.is_empty() => {
12429 format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
12430 }
12431 (None, Some(encoded)) => {
12432 format!("{body}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
12433 }
12434 })
12435}
12436
12437/// `body` with everything before its metadata slot replaced by `content`, and the slot
12438/// itself kept byte for byte.
12439///
12440/// The inverse of how [`metadata_body`] splits a body: the slot, when there is one, follows
12441/// `content` after the one [`METADATA_SEPARATOR`] the composer puts there — or alone, when
12442/// `content` is empty — so a read of the result reports `content` as the visible body and
12443/// the slot's metadata exactly as it was.
12444fn with_content(body: &str, content: &str) -> Result<String, SourceError> {
12445 let Some(slot) = slot_span(body)? else {
12446 return Ok(content.to_owned());
12447 };
12448 let kept = &body[slot.start..];
12449 Ok(if content.is_empty() {
12450 kept.to_owned()
12451 } else {
12452 format!("{content}{METADATA_SEPARATOR}{kept}")
12453 })
12454}
12455
12456/// Hold `entries` under `key` in one slot's metadata, or no such key when there are none.
12457fn set_task_list(metadata: &mut BTreeMap<String, Value>, key: &str, entries: &[TaskRef]) {
12458 if entries.is_empty() {
12459 metadata.remove(key);
12460 } else {
12461 metadata.insert(
12462 key.to_owned(),
12463 Value::Array(
12464 entries
12465 .iter()
12466 .map(|entry| Value::String(entry.as_str().to_owned()))
12467 .collect(),
12468 ),
12469 );
12470 }
12471}
12472
12473fn compose_body(
12474 content: Option<&str>,
12475 metadata: &BTreeMap<String, Value>,
12476) -> Result<Option<String>, SourceError> {
12477 let visible = content.unwrap_or_default();
12478 if metadata.is_empty() {
12479 return Ok((!visible.is_empty()).then(|| visible.to_owned()));
12480 }
12481 let encoded = serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
12482 message: error.to_string(),
12483 })?;
12484 Ok(Some(if visible.is_empty() {
12485 format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
12486 } else {
12487 format!("{visible}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
12488 }))
12489}
12490
12491fn required_bool(value: &Value, field: &str) -> Result<bool, SourceError> {
12492 value
12493 .get(field)
12494 .and_then(Value::as_bool)
12495 .ok_or_else(|| SourceError::Malformed {
12496 message: format!("GitHub response is missing boolean field {field}"),
12497 })
12498}
12499fn optional_str<'a>(value: &'a Value, field: &str) -> Result<Option<&'a str>, SourceError> {
12500 match value.get(field) {
12501 None | Some(Value::Null) => Ok(None),
12502 Some(value) => value
12503 .as_str()
12504 .map(Some)
12505 .ok_or_else(|| SourceError::Malformed {
12506 message: format!("GitHub response field {field} is not a string or null"),
12507 }),
12508 }
12509}
12510fn optional_nodes<'a>(
12511 connection: Option<&'a Value>,
12512 name: &str,
12513) -> Result<Option<&'a Vec<Value>>, SourceError> {
12514 match connection {
12515 None | Some(Value::Null) => Ok(None),
12516 Some(value) => value
12517 .get("nodes")
12518 .and_then(Value::as_array)
12519 .map(Some)
12520 .ok_or_else(|| SourceError::Malformed {
12521 message: format!("GitHub {name}.nodes is not an array"),
12522 }),
12523 }
12524}
12525fn complete_connection(connection: &Value, name: &str, size: u32) -> Result<(), SourceError> {
12526 let page_info = connection
12527 .get("pageInfo")
12528 .ok_or_else(|| SourceError::Malformed {
12529 message: format!("GitHub {name} has no pageInfo"),
12530 })?;
12531 if required_bool(page_info, "hasNextPage")? {
12532 return Err(SourceError::Malformed {
12533 message: format!(
12534 "GitHub {name} exceeds the supported nested connection size of {size}"
12535 ),
12536 });
12537 }
12538 Ok(())
12539}
12540fn optional_time(value: &Value, field: &str) -> Result<Option<DateTime<Utc>>, SourceError> {
12541 optional_str(value, field)?
12542 .map(|timestamp| {
12543 timestamp.parse().map_err(|error| SourceError::Malformed {
12544 message: format!("GitHub response field {field} is not a timestamp: {error}"),
12545 })
12546 })
12547 .transpose()
12548}
12549fn validate_page(page: &PageRequest) -> Result<(), SourceError> {
12550 if page.limit == 0 {
12551 Err(SourceError::Config {
12552 message: "page limit must be at least 1".into(),
12553 })
12554 } else {
12555 Ok(())
12556 }
12557}
12558fn next_cursor(connection: &Value) -> Result<Option<Cursor>, SourceError> {
12559 let page = connection
12560 .get("pageInfo")
12561 .filter(|value| value.is_object())
12562 .ok_or_else(|| SourceError::Malformed {
12563 message: "GitHub connection is missing pageInfo".into(),
12564 })?;
12565 if required_bool(page, "hasNextPage")? {
12566 let cursor = required_str(page, "endCursor")?;
12567 validate_cursor_progress(None, cursor)?;
12568 Ok(Some(Cursor(cursor.into())))
12569 } else {
12570 Ok(None)
12571 }
12572}
12573fn validate_cursor_progress(previous: Option<&str>, next: &str) -> Result<(), SourceError> {
12574 if next.is_empty() || previous == Some(next) {
12575 Err(SourceError::Malformed {
12576 message: "GitHub pagination cursor is empty or did not advance".into(),
12577 })
12578 } else {
12579 Ok(())
12580 }
12581}
12582/// The version of this plugin's opaque narrowing-search cursor.
12583pub const SEARCH_CURSOR_VERSION: u32 = 4;
12584
12585#[derive(Serialize, Deserialize)]
12586#[serde(tag = "state", rename_all = "snake_case", deny_unknown_fields)]
12587enum SearchConnection {
12588 Initial {},
12589 Continuing { after: Cursor },
12590 Exhausted {},
12591}
12592impl SearchConnection {
12593 fn after(&self) -> Option<&str> {
12594 match self {
12595 Self::Continuing { after } => Some(&after.0),
12596 _ => None,
12597 }
12598 }
12599 fn exhausted(&self) -> bool {
12600 matches!(self, Self::Exhausted { .. })
12601 }
12602 /// Whether a cursor naming this position, `offset` rows into its page, is one this
12603 /// plugin could have handed out: a page is resumed only part of the way through it — an
12604 /// offset of a whole page or more would skip rows nobody was given — an initial page
12605 /// only once some of it was handed out, and an exhausted connection has no page to be
12606 /// part of the way through.
12607 fn valid_resume(&self, offset: usize) -> bool {
12608 let within = offset < SEARCH_PAGE_SIZE as usize;
12609 match self {
12610 Self::Initial { .. } => offset > 0 && within,
12611 Self::Continuing { after } => !after.0.is_empty() && within,
12612 Self::Exhausted { .. } => offset == 0,
12613 }
12614 }
12615}
12616
12617/// Versioned source cursor. A zero offset and empty own-write ids are omitted.
12618#[derive(Serialize, Deserialize)]
12619#[serde(deny_unknown_fields)]
12620struct SearchPosition {
12621 version: u32,
12622 connection: SearchConnection,
12623 /// How many rows of the page `connection` starts were already handed out.
12624 #[serde(default, skip_serializing_if = "is_zero")]
12625 offset: usize,
12626 #[serde(default, skip_serializing_if = "Vec::is_empty")]
12627 seen: Vec<NativeId>,
12628 #[serde(default, skip_serializing_if = "Vec::is_empty")]
12629 own: Vec<NativeId>,
12630}
12631impl Default for SearchPosition {
12632 fn default() -> Self {
12633 Self {
12634 version: SEARCH_CURSOR_VERSION,
12635 connection: SearchConnection::Initial {},
12636 offset: 0,
12637 seen: Vec::new(),
12638 own: Vec::new(),
12639 }
12640 }
12641}
12642
12643fn is_zero(offset: &usize) -> bool {
12644 *offset == 0
12645}
12646
12647fn numeric_cursor(cursor: Option<&Cursor>) -> Result<usize, SourceError> {
12648 cursor.map_or(Ok(0), |c| {
12649 c.0.parse().map_err(|_| SourceError::Config {
12650 message: "page cursor is invalid".into(),
12651 })
12652 })
12653}
12654fn offset_page<T>(mut items: Vec<T>, offset: usize, limit: usize) -> Page<T> {
12655 if offset > items.len() {
12656 return Page::last(vec![]);
12657 }
12658 let tail = items.split_off(offset);
12659 let mut selected = tail;
12660 let next = (selected.len() > limit).then(|| Cursor((offset + limit).to_string()));
12661 selected.truncate(limit);
12662 Page {
12663 items: selected,
12664 next,
12665 }
12666}
12667
12668impl GitHubProjectsSource {
12669 async fn write_task_assets(
12670 &self,
12671 write: &ItemWrite<Task>,
12672 assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
12673 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
12674 let near = write.target.as_ref().unwrap_or(&write.item.id);
12675 for (key, entries) in [
12676 (TaskRef::DELIVERS_KEY, &write.item.delivers),
12677 (TaskRef::DELIVERED_BY_KEY, &write.item.delivered_by),
12678 ] {
12679 TaskRef::listed(key, near, Some(&self.name), entries.clone())
12680 .map_err(|message| SourceError::Refused { message })?;
12681 }
12682 if self.priorities.is_none() && write.item.priority != Priority::None {
12683 return Err(self.holds_no_priority());
12684 }
12685 self.write_item(
12686 &Incoming {
12687 written: Written::Work(ItemKind::Task, &write.item.status),
12688 title: &write.item.title,
12689 content: write.item.content.as_deref(),
12690 assets,
12691 labels: &write.item.labels,
12692 metadata: &write.item.metadata,
12693 repositories: &write.item.repositories,
12694 classification: write.item.classification,
12695 parent: write.item.project.as_ref(),
12696 delivers: &write.item.delivers,
12697 delivered_by: &write.item.delivered_by,
12698 priority: self.priorities.as_ref().map(|_| write.item.priority),
12699 },
12700 write.target.as_ref(),
12701 &write.depends_on,
12702 )
12703 .await
12704 }
12705 async fn write_document_assets(
12706 &self,
12707 write: &ItemWrite<Document>,
12708 assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
12709 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
12710 // A document takes part in no dependency graph, so there is no far end to write
12711 // natively and none to record: a caller naming one is told so rather than having it
12712 // stored under the reserved key, where a later read would report an edge the
12713 // contract says cannot exist.
12714 if !write.depends_on.is_empty() {
12715 return Err(SourceError::Refused {
12716 message: format!(
12717 "this write names {} dependencies for a document, and a document takes \
12718 part in no dependency graph; next: put the dependency on the task or \
12719 project the document is about",
12720 write.depends_on.len()
12721 ),
12722 });
12723 }
12724 self.write_item(
12725 &Incoming {
12726 written: Written::Document,
12727 title: &write.item.title,
12728 content: write.item.content.as_deref(),
12729 assets,
12730 labels: &write.item.labels,
12731 metadata: &write.item.metadata,
12732 repositories: &write.item.repositories,
12733 classification: write.item.classification,
12734 parent: write.item.project.as_ref(),
12735 delivers: &[],
12736 delivered_by: &[],
12737 priority: None,
12738 },
12739 write.target.as_ref(),
12740 &[],
12741 )
12742 .await
12743 }
12744}
12745
12746/// What [`TaskSource::end_command`] leaves of this source's held state, asserted on the state
12747/// itself, for the two things no journey can observe.
12748///
12749/// The journeys in `crates/onetaskgraph-e2e/tests/e2e/end_command.rs` prove through the engine,
12750/// with and without the call, that a settlement, a board listing and a metadata search each
12751/// read afresh after it — the resolved records, the written-item overlay, the board and its
12752/// search, and the narrowed searches. What they cannot reach is the held field definitions,
12753/// because a status write naming an option a person deleted is refused the same whether or
12754/// not the list is held, and a poisoned lock, because nothing outside the source can panic
12755/// while one of its locks is held. So these assert those directly, and every other holder
12756/// beside them so a holder added later without a clear in the call fails here.
12757#[cfg(test)]
12758mod end_command_tests {
12759 use super::*;
12760
12761 struct Token;
12762
12763 impl SecretResolver for Token {
12764 fn get(&self, var: &str) -> Option<SecretString> {
12765 (var == "GH_PROJECTS_TOKEN").then(|| "test-token".into())
12766 }
12767 }
12768
12769 fn source() -> GitHubProjectsSource {
12770 let config = serde_json::from_value(json!({
12771 "owner": "octo-org", "project_number": 7, "repository": "acme/work",
12772 // Nothing here is sent: the source is only built and its state inspected.
12773 "endpoint": "http://127.0.0.1:9/graphql",
12774 }))
12775 .expect("a usable configuration");
12776 GitHubProjectsSource::new(&SourceName::new("work").unwrap(), config, &Token)
12777 .expect("the source builds")
12778 }
12779
12780 /// One issue as a board read answers it.
12781 fn resolved(source: &GitHubProjectsSource) -> Resolved {
12782 source
12783 .resolve(&json!({
12784 "id": "ITEM-1",
12785 "content": {"__typename": "Issue", "id": "I_1", "title": "Held",
12786 "body": "what a person may since have edited", "state": "OPEN",
12787 "stateReason": null, "url": null, "number": 1,
12788 "subIssuesSummary": {"total": 0},
12789 "labels": {"nodes": [], "pageInfo": {"hasNextPage": false}}},
12790 "fieldValues": {"nodes": [], "pageInfo": {"hasNextPage": false}},
12791 }))
12792 .expect("the item reads")
12793 .expect("an issue")
12794 }
12795
12796 /// Hold something in every holder the call clears, and the repository id it keeps.
12797 fn fill(source: &GitHubProjectsSource) {
12798 let item = resolved(source);
12799 source.created.lock().unwrap().push(item.clone());
12800 source.updated.lock().unwrap().push(item.clone());
12801 *source.board_cache.lock().unwrap() = Some(Board {
12802 id: "PVT-board".into(),
12803 fields: json!({"nodes": []}),
12804 items: vec![item.clone()],
12805 });
12806 *source.search_cache.lock().unwrap() = Some(vec![item.clone()]);
12807 source
12808 .narrowed_cache
12809 .lock()
12810 .unwrap()
12811 .insert("status:todo".into(), vec![item.clone()]);
12812 source.children_cache.lock().unwrap().insert(
12813 NativeId("P-1".into()),
12814 (NativeId("P-1".into()), vec![item.clone()]),
12815 );
12816 source
12817 .search_next
12818 .lock()
12819 .unwrap()
12820 .insert("status:todo".into(), Some("cursor".into()));
12821 source
12822 .resolved_cache
12823 .lock()
12824 .unwrap()
12825 .insert(item.id.clone(), item);
12826 *source.fields_cache.lock().unwrap() = Some(BoardFields {
12827 id: BoardId::parse("PVT-board").unwrap(),
12828 fields: json!({"nodes": []}),
12829 });
12830 source
12831 .repository_cache
12832 .lock()
12833 .unwrap()
12834 .insert(RepositoryTarget::parse("acme/work").unwrap(), "R_1".into());
12835 }
12836
12837 fn assert_dropped(source: &GitHubProjectsSource) {
12838 assert!(source.created().unwrap().is_empty(), "created");
12839 assert!(source.updated().unwrap().is_empty(), "updated");
12840 assert!(source.board_cache().unwrap().is_none(), "board");
12841 assert!(source.search_cache.lock().unwrap().is_none(), "search");
12842 assert!(source.narrowed_cache.lock().unwrap().is_empty(), "narrowed");
12843 assert!(
12844 source.children_cache.lock().unwrap().is_empty(),
12845 "project children"
12846 );
12847 assert!(
12848 source.search_next.lock().unwrap().is_empty(),
12849 "search paging"
12850 );
12851 assert!(
12852 source.resolved_cache().unwrap().is_empty(),
12853 "resolved records"
12854 );
12855 assert!(source.fields_cache().unwrap().is_none(), "board fields");
12856 assert_eq!(
12857 source.repository_cache().unwrap().len(),
12858 1,
12859 "a repository's node id stays valid and is kept"
12860 );
12861 }
12862
12863 fn end(source: &GitHubProjectsSource) {
12864 tokio::runtime::Builder::new_current_thread()
12865 .build()
12866 .unwrap()
12867 .block_on(source.end_command())
12868 .expect("the command ends");
12869 }
12870
12871 #[test]
12872 fn the_call_drops_every_item_search_and_board_read_and_keeps_repository_ids() {
12873 let source = source();
12874 fill(&source);
12875 end(&source);
12876 assert_dropped(&source);
12877 }
12878
12879 #[test]
12880 fn the_call_clears_a_lock_an_earlier_failure_poisoned() {
12881 fn poison<T: Send>(held: &Mutex<T>) {
12882 std::thread::scope(|scope| {
12883 let _ = scope
12884 .spawn(|| {
12885 let _guard = held.lock().unwrap();
12886 panic!("a failure while the lock is held");
12887 })
12888 .join();
12889 });
12890 assert!(held.is_poisoned());
12891 }
12892 let source = source();
12893 fill(&source);
12894 poison(&source.created);
12895 poison(&source.updated);
12896 poison(&source.board_cache);
12897 poison(&source.search_cache);
12898 poison(&source.narrowed_cache);
12899 poison(&source.search_next);
12900 poison(&source.resolved_cache);
12901 poison(&source.fields_cache);
12902 assert!(
12903 source.resolved_cache().is_err(),
12904 "a poisoned lock is refused before the call"
12905 );
12906 end(&source);
12907 assert_dropped(&source);
12908 }
12909}
12910
12911#[cfg(test)]
12912mod unprojectable_fields_tests {
12913 use super::*;
12914
12915 /// The field names `docs/metadata.md` says a `metadata_fields` entry may not name.
12916 fn documented() -> std::collections::BTreeSet<String> {
12917 let document = std::fs::read_to_string(
12918 std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../docs/metadata.md"),
12919 )
12920 .expect("docs/metadata.md is readable");
12921 let document = document.split_whitespace().collect::<Vec<_>>().join(" ");
12922 let lead = "a `field` equal, ignoring case, to ";
12923 let start = document.find(lead).expect("the refused-field sentence") + lead.len();
12924 let sentence = &document[start..];
12925 let sentence = &sentence[..sentence
12926 .find("; and two entries naming one field")
12927 .expect("the refused-field sentence ends at the duplicate rule")];
12928 let (own, github) = sentence
12929 .split_once(" or a field GitHub owns on every board (")
12930 .expect("the sentence names GitHub's own fields");
12931 own.split('`')
12932 .skip(1)
12933 .step_by(2)
12934 .chain(
12935 github
12936 .strip_suffix(')')
12937 .expect("GitHub's own fields close the sentence")
12938 .split(", "),
12939 )
12940 .map(str::to_owned)
12941 .collect()
12942 }
12943
12944 #[test]
12945 fn the_documented_refused_fields_are_exactly_the_ones_the_configuration_refuses() {
12946 let refused: std::collections::BTreeSet<String> = UNPROJECTABLE_FIELDS
12947 .iter()
12948 .map(|&name| name.to_owned())
12949 .collect();
12950 assert_eq!(documented(), refused, "the fields docs/metadata.md lists");
12951 let instance = SourceName::new("work").unwrap();
12952 for name in UNPROJECTABLE_FIELDS {
12953 for spelled in [name.to_owned(), name.to_uppercase()] {
12954 let entry = MetadataFieldConfig {
12955 field: spelled.clone(),
12956 key: "team.machine".to_owned(),
12957 path: Vec::new(),
12958 };
12959 let refused = MetadataField::resolve(vec![entry], &instance)
12960 .expect_err("an unprojectable field is refused");
12961 assert!(
12962 refused.to_string().contains(&format!("{name:?}")),
12963 "{spelled}: {refused}"
12964 );
12965 }
12966 }
12967 }
12968}