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//! | `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. |
161//! | `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. |
162//! | `task_dependencies` | **Supported and proven,** in both directions: `blockedBy` and `blocking`. |
163//! | `project_dependencies` | **Supported and proven,** in both directions, over the same two connections, because a project here is an issue. |
164//! | `max_page_size` | **Supported and proven.** [`MAX_PAGE_SIZE`], GitHub's own connection maximum. |
165//!
166//! Image writes use `POST https://uploads.github.com/user-attachments/assets` with `name`,
167//! `content_type` and the issue repository's numeric `repository_id` as query parameters,
168//! the source token as `Authorization: Bearer`, the image content type as `Content-Type`,
169//! and the raw bytes as the request body. The numeric repository id is read at most once
170//! per repository per source instance. A loopback API endpoint also selects the loopback
171//! uploads host; production needs no assets repository, commits or extra configuration.
172//! Every returned JSON `url` is read with the same token before a body is written: only
173//! a 2xx verification succeeds. A refusal names the asset, URL and HTTP status; an
174//! anonymous 404 does not invalidate an authenticated 200. An upload refusal names the
175//! asset and HTTP status and says the token type may not be accepted by the upload
176//! endpoint. A classic PAT was measured accepted; no acceptance claim is made about
177//! fine-grained PATs or OAuth tokens.
178//! The body keeps the authored alt text while `./<name>` becomes the attachment URL,
179//! and `onetaskgraph.assets` records `{sha256, url}` by name. A matching destination
180//! SHA-256 reuses its URL without an upload or verifying read; changed bytes are uploaded
181//! and verified afresh, in the repository the issue already lives in on an update.
182//! An uploaded image renders for the viewers GitHub lets read it, like the issue text beside it.
183//! Uploads take the same mutation spacing as content writes. Both uploads and verification
184//! reads wait out classified rate limits within the configured per-call retry budget,
185//! on the supplied clock; verifying reads take no mutation slot.
186//!
187//! Nothing here is unsupported. `documents` and `comments` are not predicates — they say this
188//! source has documents and that its tasks have comments, both of which hold — and the three
189//! facts behind the uniform `Native` on the
190//! predicates beside it are recorded below rather than re-derived, because a reader who
191//! takes `Native` to mean *the remote service filters* will read that uniformity as a
192//! lie.
193//!
194//! First, the plugin contract defines `Support::Native` as *the source applies this
195//! predicate itself*, and says nothing about where it applies it. What the declaration
196//! promises the engine is capability rule 1 — a predicate declared `Native` **is** applied
197//! — so that the engine may push it down and apply nothing of its own.
198//!
199//! Second, this source can keep that promise for every predicate at no additional API
200//! cost, because whichever of the reads below answers a query has already read every
201//! candidate that query will return before it filters anything. Filtering those items is
202//! in-process work over data already in hand.
203//!
204//! Third, six task predicates are asked of GitHub as a narrower question and the rest are
205//! applied in process over what that question returned. A project filter has a relationship — a
206//! project's tasks are that issue's sub-issues, and asking the issue for them is both cheaper
207//! and exact. Comment activity is the issue search's `updated:` qualifier. A text search, and
208//! a search for metadata values, is the board-scoped issue search carrying the text and each
209//! value as quoted phrases; an origin is the board's own field filter over its origin field
210//! beside the same search for the id. **The text search narrows, and that is this source's
211//! declared semantics:** GitHub matches whole words where the substring rule this source and
212//! the local Markdown source confirm with would match inside one, so an item holding the text
213//! only inside a longer word is never a candidate. Every item returned does contain the text.
214//! A project query's text, and a document query's scoped to no project, is that same search
215//! and narrows on the same terms, its candidates confirmed by their kind as well.
216//! GitHub's issue search offers no qualifier for a label set, a status column or a priority,
217//! so those three are applied in process over the candidates, and a query carrying none of
218//! the six narrowing predicates reads the board. Declaring one `Unsupported` would make the
219//! engine compensate for work this source has already done, and declaring `projects` native
220//! while ignoring the filter (which this source once did) silently returns another project's
221//! tasks, because the engine trusts the declaration and applies nothing locally.
222//!
223//! # The three ways this source reaches an item, and what each costs
224//!
225//! A board read is charged for what its *nested* connections could return rather than for
226//! what was asked, so one whole-board read costs the same whether the question was about
227//! one project or about all of them. That is why a question about one project is never
228//! answered by reading the board:
229//!
230//! | The question | What is sent | What it costs |
231//! | --- | --- | --- |
232//! | 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 |
233//! | 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 |
234//! | 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 |
235//! | 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 |
236//! | one project's tasks or documents | [`graphql::SUB_ISSUES`] — that issue's own `subIssues` | that project |
237//! | which projects this board holds | [`graphql::SEARCH_ISSUES`] — an issue search scoped to the board | the board's issues, without their board items |
238//! | 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 |
239//! | 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 |
240//! | 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 |
241//! | 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 |
242//! | 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 |
243//! | 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 |
244//!
245//! The following standalone-ticket requests are pinned by the real CLI fixture journeys
246//! `follow_up_writes_resolve_each_item_once_and_batch_the_copy_fields` and
247//! `a_batched_detail_read_costs_one_request_and_one_point_per_detail_batch`, as request count
248//! equal to declared points equal to the row. They include the origin lookup and the
249//! field/repository discovery a create needs. A bound re-copy changes status, priority,
250//! content and metadata; comment recount means a subsequent detail read. Each request here
251//! costs one declared point. A membership beyond the embedded page can additionally require
252//! the one-point membership recovery described above. A bound re-copy of a task filed under a
253//! project adds one read, the engine confirming that project's link by its own id once per
254//! command; and the same-source far ends a write newly names — those that do not already block
255//! the item, whose own read answered for them — are read together by their own ids,
256//! [`DETAIL_BATCH`] to one [`graphql::ISSUE_DETAILS`] request, each new edge then one
257//! [`graphql::ADD_BLOCKED_BY`]. Both additions are rows of the table below, pinned by
258//! `a_bound_recopy_adds_one_project_read_and_batches_the_dependencies_it_newly_names`.
259//!
260//! **[`DETAIL_BATCH`] is 24**: the largest batch of [`graphql::ISSUE_DETAILS`] the node-count
261//! model prices at one point. Each aliased item is six of GitHub's aggregate, so 24 are 144,
262//! which rounds to one point, and 25 are 150, which rounds to two; `tests/point_cost.rs`
263//! holds both halves.
264//!
265//! **An existing item is written body last.** A bound re-copy and a `task update` send its
266//! board fields first — the `Status` option and the `Priority` together, in one request — then
267//! its parent and its `blockedBy`, and its title, body and state in one `updateIssue` last.
268//! GitHub runs no two requests as one, and runs a document's mutation fields in order without
269//! undoing an earlier field when a later one fails, so that order is what makes a write
270//! refused part-way leave the item's body, and every metadata key in it, exactly as it stood;
271//! the one piece of metadata written before the body, an origin a copy re-points, is put back
272//! when a later write is refused — and when putting it back is refused too, the write's own
273//! refusal names that key, what it now holds and what it held. `crates/onetaskgraph-github-projects-e2e/tests/e2e/write_order.rs` refuses each
274//! of those writes in turn, whole and as one aliased field failing after the one before it.
275//!
276//! **Two facts about GitHub the write rows rest on, each read off GitHub's published schema
277//! artifact <https://docs.github.com/public/fpt/schema.docs.graphql> on 2026-10-01 and pinned
278//! in `tests/fixtures/schema.graphql`, and the first then put to GitHub itself:**
279//!
280//! - **A board is accepted at creation but its item is not answered, so a create still files
281//! the issue itself: a new copy is 5 requests, and 4 with `--create`.**
282//! `CreateIssueInput.projectV2Ids: [ID!]` is declared there — "An array of Node IDs for
283//! Projects V2 associated with this issue", `@possibleTypes(concreteTypes: ["ProjectV2"])`.
284//! The credentialed journey `real_projects_v2_contract_writes_and_leaves_no_residue` was run
285//! against a real board on 2026-10-01 with a create sending the board there and reading the
286//! item off the payload's `Issue.projectItems`: every one of its four creates answered with
287//! no item on the board, so each went on to [`graphql::ADD_TO_BOARD`], and the fourth was
288//! refused "Content already exists in this project" — GitHub had filed the issue after
289//! answering, and refuses a second filing rather than answering with the item it holds. A
290//! create therefore sends no `projectV2Ids` and files the issue with
291//! `addProjectV2ItemById`, the one call whose answer names the board item. The saving that is
292//! real is the read before it: the board's fields and the repository's id together, in
293//! [`graphql::CREATION_CONTEXT`], at the point the repository is known.
294//! - **A comment still reads its target first, so a comment is 2 requests.**
295//! `AddCommentInput.subjectId: ID!` is declared there with
296//! `@possibleTypes(concreteTypes: ["Issue", "PullRequest"], abstractType:
297//! "IssueOrPullRequest")`. A board draft is no such subject and would be refused, but a
298//! project's issue, a document's issue, an issue on no board of this source and a pull
299//! request all are: GitHub writes the comment, so there is no refusal to map into "that is
300//! not a task of this board". [`graphql::ISSUE`] before [`graphql::ADD_COMMENT`] is what
301//! refuses those by name.
302//!
303//! | Verb | Requests / points | Documents |
304//! | --- | --- | --- |
305//! | new copy | 5 | ORIGIN_LOOKUP, CREATION_CONTEXT (the board's fields and the repository's id together), CREATE_ISSUE, ADD_TO_BOARD, UPDATE_FIELDS |
306//! | copy --create | 4 | CREATION_CONTEXT, CREATE_ISSUE, ADD_TO_BOARD, UPDATE_FIELDS: the new copy without its ORIGIN_LOOKUP |
307//! | 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 |
308//! | 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 |
309//! | 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 |
310//! | comment | 2 | ISSUE, ADD_COMMENT: the target is read first, because GitHub accepts a comment on any issue or pull request (see below) |
311//! | detail | 1 | ISSUE_DETAIL: the item and its first page of comments, for `task show` and `task comment list`; `--no-comments` is ISSUE alone |
312//! | batched detail | ceil(n / DETAIL_BATCH) | ISSUE_DETAILS: `task show-many` of `n` items, DETAIL_BATCH (24) at a time, comments included or not |
313//! | recount | 1 | ISSUE_DETAIL |
314//! | status | 2 | ISSUE, UPDATE_FIELD; a terminal status additionally updates issue state |
315//! | priority | 2 | ISSUE, UPDATE_FIELD or CLEAR_FIELD, with stored priority in the mutation response |
316//! | content | 2 | ISSUE, UPDATE_ISSUE |
317//! | metadata | 2 | ISSUE, UPDATE_ISSUE |
318//! | 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 |
319//! | record only | 1 | ISSUE |
320//!
321//! <!-- github-search-paging:start -->
322//! Board-scoped text, metadata, project-name and comment-activity searches send every
323//! page at `first = 20` (SEARCH_PAGE_SIZE), the SEARCH_ISSUES document's one-point
324//! ceiling. A later page is sent only when `hasNextPage` is true and the caller still
325//! needs rows. A page is never resized to the rows still needed: GitHub orders one
326//! search differently at different page sizes, so one fixed size makes a paged walk
327//! send exactly the requests one whole read sends, and the answer's order is the order
328//! those pages arrive in. A page below twenty would cost the same one point, and GitHub
329//! prices this document by rows, so twenty-row pages cost per row what 100-row pages do.
330//! Project-name lookup continues until an exact match or exhaustion. A task limit bounds
331//! returned and fetched pages: a limit is sliced from the pages it needs, and local
332//! confirmation can require more candidates than matching rows. Walking all pages
333//! returns the whole answer. The opaque version-4 source cursor carries GitHub's page
334//! cursor and how far into that page the last answer stopped, and resumes in the same
335//! process or a new one, without duplicates or gaps. It carries no rows: one process
336//! sends each page's search once, and a new process re-reads only the page it resumes
337//! in, then sends a further page once, never as a re-read, only when its limit still
338//! needs rows. Every request either walk sends is the one a whole read sends for that page. Own writes replace stale index
339//! copies and complete missing rows at exhaustion. Cache entries are whole GitHub pages,
340//! so a small answer cannot truncate a wider question. Origin pages remain three; whole-board sizing is unchanged.
341//! Read-after-write is a per-process guarantee. A cursor resumed in a new process is
342//! not required to include the original process's writes still omitted by the index.
343//! <!-- github-search-paging:end -->
344//!
345//! The board half of an issue — its board item's id, its `Status` option and this
346//! source's origin text field — rides along on `Issue.projectItems` in the first three, so
347//! an item reached any of those ways resolves through the same
348//! [`GitHubProjectsSource::resolve`] the board walk uses and reports the same title, the
349//! same status, the same labels and the same qualified id. That connection comes back a
350//! *page* at a time, at `BOARD_ITEMS_PAGE_SIZE`, so the entry for this board is looked for
351//! on the page in hand and — only if that page reports more of the connection — in the
352//! last row's read of that one issue's memberships, resumed from the page's own cursor and
353//! walked to exhaustion. An issue with no entry for *this* board is not this source's to
354//! report, which is what keeps an id naming another repository's issue from being answered
355//! as an item of this board; and because the page is where the search starts rather than
356//! where it ends, that answer is one about a connection read to exhaustion and never about
357//! an unread page. Nothing costs the extra read but an issue on more boards than a page
358//! holds: an issue this board really does not hold reports no next page, so its
359//! memberships are already exhausted where they arrived.
360//!
361//! **No document here selects the board's own `Labels` field, and nothing is lost by
362//! that.** An item's labels are read from its content alone, wherever that content is
363//! reached: the three documents above select `Issue.labels` on the fragment, and
364//! [`graphql::BOARD`] selects the same connection on the `... on Issue` arm of its
365//! `content`. A board's `Labels` field is not one anybody fills in: it is a built-in
366//! `ProjectV2FieldType`, it is absent from `ProjectV2CustomFieldType` so no project can
367//! create one, and `ProjectV2FieldValue` — the whole of what
368//! `updateProjectV2ItemFieldValue` accepts — offers no way to write one. So GitHub derives
369//! it from the content, for every content type it exists on, and there is nothing it can
370//! hold that the content does not already say: for an `Issue` it *is* that issue's own
371//! labels, so selecting it beside them unions a set with itself.
372//!
373//! **A draft loses nothing by that either**, which is the reasoning this paragraph once had
374//! backwards. `DraftIssue` exposes no `labels` field, and by the three schema facts above
375//! it cannot carry a board `Labels` value to be derived from one — so a draft has nothing
376//! to select *and nothing to lose*, and reports no labels at all. A `PullRequest` item is
377//! discarded by [`GitHubProjectsSource::resolve`] before labels are read. Both halves are
378//! held to that by tests in `tests/plugin.rs`: the four ways an item is reached report one
379//! label set, and that set is the fixture issue's own, by
380//! `an_item_reports_the_same_labels_title_status_and_id_however_it_is_reached`; and a board
381//! item whose content is a draft reports an empty set, by
382//! `a_board_item_whose_content_is_a_draft_reports_no_labels_at_all`. The absence of the
383//! selection is held over [`graphql::DOCUMENTS`] by
384//! `no_document_selects_the_boards_own_labels_field`.
385//!
386//! The whole-board row is still the board's own item connection, and deliberately: a
387//! **draft** board item is not an issue, so no search can list one, and the reads that have
388//! to answer for the whole board are the ones whose cost is the board's size anyway.
389//!
390//! **A question about one item this source already names by id never lists the board.**
391//! Whether that item is on this board, and what its board fields are, is answered by reading
392//! that item — its own `Issue.projectItems`, walked to exhaustion by
393//! [`GitHubProjectsSource::resolve_issue`], or a draft's own board item — and never by
394//! looking for it in [`graphql::BOARD`]'s `items` or in a listing this command already
395//! holds. That covers a write's destination, the project a new item is filed under, a
396//! same-source far end a dependency names, a status write, the dependency slot a draft keeps,
397//! and the delete that takes back an item a copy made. What such a write needs of the board
398//! and the item does not carry — the board's id, the `Status` and origin field definitions —
399//! comes from [`graphql::BOARD_FIELDS`], which reads no item at all. The reason is evidence,
400//! not economy alone: `ProjectV2.items` is a projection that lags the membership GitHub
401//! itself reports — an issue added with `addProjectV2ItemById` can be missing from it for
402//! minutes. Scanning this host's 842-item board has refused a document copy and an update
403//! even though the items' own reads named that board. A scan there gives the wrong answer
404//! as well as paying for every page. So a `board.items` lookup does not belong on any of
405//! those paths.
406//!
407//! **What a read may return is capped too, and that cap is on the document rather than on
408//! the board.** GitHub limits the number of nodes **one query may return** to
409//! [`NODE_COUNT_LIMIT`] and refuses a query above that before executing it: the answer is
410//! an error naming the connection the count crossed at, not a slow or a partial result.
411//! Every board this source reads is refused the same way, so no board is too big for these
412//! documents and none is small enough to save one that is over.
413//!
414//! The count is arithmetic over the document's own text: each connection contributes the
415//! `first:` it asks for, counts **multiply** down a nested path and **sum** across sibling
416//! paths. Those are [GitHub's published rules][node-limits] and this workspace does not
417//! restate them — `github-graphql-node-count` implements them, and
418//! [`worst_case_node_count`] under [`largest_page_sizes`] is where every node count here
419//! comes from. `every_document_this_source_sends_stays_under_githubs_node_limit`, in
420//! `tests/node_count.rs`, recomputes every document in [`graphql::DOCUMENTS`] from that
421//! same text on every run and fails naming any that reaches the limit — so a connection
422//! added to a shared fragment is caught there rather than by GitHub.
423//!
424//! What decides those counts is the page sizes: [`MAX_PAGE_SIZE`] on the outer page,
425//! `NESTED_PAGE_SIZE` on the connections hanging off one item, and
426//! `BOARD_ITEMS_PAGE_SIZE` on the page of an issue's board memberships a read carries.
427//! `$nestedFirst` is spent twice down one path of a board read, so that constant is
428//! effectively squared there, which is why it is the one the limit is most sensitive to.
429//! `BOARD_ITEMS_PAGE_SIZE` is small for a reason of its own, recorded beside it: what a
430//! page of memberships misses is recovered by one further read rather than refused, so it
431//! buys a bound every read pays for at the price of a request only a multi-board issue
432//! pays.
433//!
434//! **`nodeCount` and `cost` are two numbers against two limits, and both are computed
435//! offline here — per document, one document at a time.** `nodeCount` is the one above: the
436//! most nodes one query may return, checked per query and bounded by [`NODE_COUNT_LIMIT`].
437//! `cost` is rate-limit points, metered per hour across everything one credential does; it
438//! is what the two limiters [`Limiter`] tells apart meter, and a document under
439//! [`NODE_COUNT_LIMIT`] still says nothing about its price. [`worst_case_point_cost`] is
440//! that second number, and `tests/point_cost.rs` pins every document in
441//! [`graphql::DOCUMENTS`] at what it costs — there being no per-call point ceiling to hold
442//! one under, the pin itself is the check. The credentialed lane reconciles both figures
443//! against GitHub's own, off a probe it already sends.
444//!
445//! **What is pinned that way is a per-document price and never a session's.** The record in
446//! `session-cost.md` measures the two quantities a whole session can be counted in offline —
447//! **requests** and **worst-case nodes** — and neither is points. What one whole session
448//! consumes of the hourly point allowance is observable only from a credentialed run's own
449//! `x-ratelimit-*` headers, which is what [`accounting`] fills its per-budget figures from
450//! and what `tests/live.rs` prints at the end of every run.
451//!
452//! [node-limits]: https://docs.github.com/en/graphql/overview/rate-limits-and-node-limits-for-the-graphql-api
453//!
454//! **Where a read-after-write guarantee comes from, since neither of GitHub's two
455//! enumerations of a board can supply one alone.** Resolving a node id is strongly
456//! consistent, so a read by id and a project's own sub-issues are already current. The
457//! other two are not, and they are behind by different amounts and in different directions:
458//!
459//! - GitHub's **issue search** is an index and answers a write made moments ago with the
460//! value from before it — usually for a second or two.
461//! - **`ProjectV2.items`** is a projection GitHub rebuilds behind the write, and an item put
462//! on a board with `addProjectV2ItemById` can be **absent** from it — not present with its
463//! content withheld, absent, with the connection walked to its own `hasNextPage: false` —
464//! for *minutes*, while `Issue.projectItems` names the same membership at once.
465//!
466//! That second one is a measurement rather than a caution. This repository's own
467//! credentialed journey writes a project and waits for the board to report it, then writes a
468//! task and waits for the same thing seconds later on the same board: the project wait is
469//! answered through the search and converged in two or three attempts in each of three runs,
470//! and the task wait is answered through `ProjectV2.items` and converged in none of them
471//! inside thirty. Separately, an item added to a second and larger board was read back by
472//! `Issue.projectItems` on that board's own id while every one of that connection's nine
473//! pages, walked to exhaustion nine minutes after the add, did not name it. Reading a board
474//! through the lagging one alone is what had a board read deny an issue that had certainly
475//! landed on it.
476//!
477//! So [`GitHubProjectsSource::board`] is the **union** of both — each search result still
478//! admitted only on this board's own strongly-consistent `Issue.projectItems`, and neither
479//! enumeration dropped, because only `ProjectV2.items` lists a board draft and only the
480//! search reports what the projection is behind on. What closes the last
481//! gap, the one where both are behind, is [`GitHubProjectsSource::created`]: every read this
482//! source answers is completed with what this process itself wrote, so an item created
483//! seconds ago is reported whether or not GitHub has caught up. Nothing else is remembered,
484//! nothing is written down, and the record dies with the process. **A wait that has to
485//! observe GitHub's own data cannot be answered from that record** — which is why the
486//! credentialed journey asks through a source built afresh, and why the union above rather
487//! than a longer wait is what makes such a wait converge.
488//!
489//! **A narrowed read is the same bargain, stated for each of the three predicates it
490//! answers.** A read carrying a text, metadata or origin predicate asks GitHub's index rather
491//! than walking the board, and every such answer is completed with what this process wrote —
492//! its [`created`](GitHubProjectsSource::created) record and every existing item it wrote,
493//! each filtered by the same predicates as the rest — so an item this command wrote a moment
494//! ago is returned by a query that matches it whether or not the index has caught up. An item
495//! a caller holds the id of is read by that id, with `node(id:)`, which is strongly
496//! consistent. What is left is stated rather than papered over:
497//!
498//! | Read | Finds | Behind by |
499//! | --- | --- | --- |
500//! | text, metadata | the issue search for the phrases | what another process wrote in the last second or two, until GitHub indexes it |
501//! | 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 |
502//! | 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 |
503//! | origin, third read | this process's own writes | nothing |
504//!
505//! So an origin carrier another process added within the last second or two, before either
506//! index has it, can be missing from an origin query, and one written by the release before
507//! this one — its origin in the field alone — can be missing for as long as the board's own
508//! item connection is behind on it. A copy that must not duplicate its own earlier write
509//! relies on the link it records, not on either index. **A board draft is not an issue**, so
510//! a draft is never returned by a text, metadata or origin query, whatever it holds: no search
511//! lists one, the origin lookup drops any the board's own field filter names, and one this
512//! process wrote is not added back either.
513//!
514//! **The origin lives in the board field, and the body holds a mirror of it.** A write that
515//! carries an origin writes it to the `onetaskgraph.origin` text field and also into the
516//! body's metadata slot, so the issue search can find it in seconds. The field is
517//! authoritative: this source reads an item's origin from the field alone, so a slot that
518//! disagrees with it, or holds one where the field holds none, is never read as a second
519//! origin — and the release before this one reads the slot, drops that key's copy for the
520//! field's, and sees the same one origin.
521//!
522//! Filtering happens before paging, so a page of a filtered result is a page of the
523//! survivors rather than the survivors of a page. Label matching and the substring rule a
524//! text candidate is confirmed by answer the same question the same way the local Markdown
525//! source's do; which candidates a text search has to confirm is GitHub's word match, which
526//! is the one place the two sources can answer the same text differently.
527//!
528//! <!-- llmlint: ignore[contracts_have_one_source_or_a_drift_gate] The declaration itself
529//! has one source, `capabilities`, and the note above is the reasoning behind it rather
530//! than a second copy of it: without the three facts recorded here a reader takes the
531//! uniform `Native` for a lie and reverts it. The drift gate on the declaration is this
532//! crate's own capabilities test, which pins every field of it against a fully spelled-out
533//! `Capabilities` literal — a struct with no `Default`, so a field added to the contract
534//! fails to compile there rather than going unasserted. -->
535//! The fixture-server tests above run wherever this crate is selected; the credentialed
536//! lane runs in the same required check, beside them, and can fail it — it verifies the
537//! 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,
538//! one filed under neither, a label on one of the three and a closed status on another —
539//! because that shape is what tells an honoured predicate from an ignored one: a board
540//! holding a single project answers a project filter the same way whether or not this
541//! source applies it, which is exactly how the defect above went unseen.
542//!
543//! That lane writes only to the board `GH_PROJECTS_OWNER` and `GH_PROJECTS_NUMBER` name,
544//! and the scratch repository `GH_PROJECTS_REPOSITORY` names:
545//! `nickderobertis/onetaskgraph-live-scratch`. It refuses the core repository before any
546//! session or request, independently of the live demand. Fix that variable in the machine's
547//! onetaskgraph `secrets.env` or the environment it pushes from, such as ai-orchestrator's
548//! `.env`. The targets are declared once in `onetaskgraph_github_live`, the lane's own
549//! policy crate; absent credentials or nominations otherwise skip.
550//!
551//! GitHub Actions artifacts carry `ci-<run id>-<attempt>-<micros>`, naming their writing
552//! run and attempt; invalid Actions identity refuses before writing. Other runs retain
553//! `<host>-<process>-<micros>`. Own cleanup matches the whole stamp. Machine residue stays
554//! the owning machine's lock sweep's; Linear always keeps that form and is unaffected.
555//! The hourly janitor is cleanup, never a test lane: scratch CI residue is removed only
556//! after its run reads back as `completed`, immediately before the listed-artifact batch.
557//! Failed or incomplete ownership reads preserve residue. A 24-hour waiting period
558//! is a margin, never the ownership authorisation. Cleanup leaves machine stamps
559//! and the board's `onetaskgraph.origin` field untouched.
560//!
561//! # What a session of requests costs, and where the report is
562//!
563//! This source records **every** request it sends into [`accounting::Accounting`], at
564//! `send_once` — the one place a request leaves this crate, which is why a read path added
565//! later is counted without anybody remembering to count it. That is the whole of what this
566//! crate adds to the arrangement; [`accounting`] is where what a record carries, how a
567//! session's spend is arrived at, and what it deliberately does not know are set out.
568//!
569//! What one whole session of the live journey costs, counted that way against this crate's
570//! loopback fixture board, is written down in `session-cost.md` beside this crate — with the
571//! reduction it came out of, and with what it does and does not say about rate-limit points.
572//!
573//! [`GitHubProjectsSource::accounting`] is the read: a snapshot to hold and compare, which
574//! [`accounting::Session::report`] renders the session report from. It is on the ordinary
575//! code path — no environment variable, no feature, no build configuration — because an
576//! instrument nobody switches on measures nothing, and
577//! [`Plugin::build_recording_into`] is how a caller making its own calls beside this
578//! source's counts the whole session rather than this source's share. The credentialed lane
579//! in `tests/live.rs` does exactly that, and prints the report at the end of every run,
580//! passed or failed.
581//!
582//! **A live session refuses to start unless the account can afford it.** Before it does any
583//! of the work it exists to do, the journey makes one request — `GET /rate_limit`, which
584//! GitHub documents as not counting against the REST rate limit and which answers both of
585//! its budgets at once — and starts only if, for each of them, what remains minus this
586//! session's estimated cost is still at least
587//! `onetaskgraph_live::RETAINED_BUFFER` — twenty per cent — of that budget's whole
588//! allowance. A session that cannot **declines**: it did not run, so it is
589//! neither a pass nor a failing assertion, and it says which budget was short, that budget's
590//! limit, what remained, the estimate, the buffer and when it resets — then stops, without
591//! waiting for the budget to come back. The estimate is derived offline from
592//! `tests/fixtures/session-cost.txt` and a cost model stated in `tests/journey/budget.rs`,
593//! which is also where the published rule that model rests on is cited; the accounting
594//! above records the gate's own read like any other request, and
595//! [`accounting::Session::report`] prints the estimate beside what the session really spent.
596//!
597//! **GitHub is the authority on both of its own numbers, and the credentialed lane goes and
598//! asks it.** Everything above computes `nodeCount` and `cost` offline from a document's own
599//! text, which is what lets it run on every platform and on a pull request from a fork with
600//! no credential — and that is what actually stops a regression merging. But an offline
601//! arithmetic can only ever agree with itself: if GitHub changes its rules, this workspace
602//! goes on computing the old answer and nothing notices. So `tests/live.rs` reconciles them.
603//! GitHub's schema exposes `rateLimit(dryRun: true)`, whose `nodeCount` is *"the maximum
604//! number of nodes this query may return"* and whose `cost` is what that document would
605//! spend, both for a document **without executing it**, and the lane asks it for every query
606//! document this source sends, under the largest bindings this source sends, and fails when
607//! GitHub's figure and [`worst_case_node_count`] or [`worst_case_point_cost`] disagree. A
608//! mutation is skipped, because `rateLimit` is a field of `Query` and cannot be asked about
609//! one; the offline pins still cover it. It records what those calls reported about the
610//! account's own allowance, because whether asking is free is a thing to observe rather than
611//! to assume. Two quantities, not one: [`NODE_COUNT_LIMIT`] bounds `nodeCount` per query,
612//! and `cost` is metered against an hourly allowance the accounting above reads off a
613//! credentialed run's own response headers.
614//!
615//! **GitHub has two rate limiters and this source is refused by both, so nothing here
616//! treats them as one thing.** The primary budget is the hourly allowance `gh api
617//! rate_limit` reports; the secondary limiter is a burst limiter over content-generating
618//! requests, and *nothing* reports it. Which one refused decides the operator's next step,
619//! so [`Limiter`] is a type rather than a detail, and it is what [`MIN_MUTATION_INTERVAL_MS`],
620//! [`GitHubProjectsSource::board_cache`] and [`GitHubProjectsSource::graphql`] each answer
621//! one part of.
622#![deny(missing_docs)]
623
624use std::collections::BTreeMap;
625use std::sync::{Arc, Mutex};
626use std::time::Duration;
627
628use chrono::{DateTime, Utc};
629use onetaskgraph_plugin_api::{
630 Capabilities, Classification, Comment, CommentBody, Cursor, DependencyEdge, DependencyEndpoint,
631 DependencyKind, DependencySupport, Direction, Document, DocumentQuery, Health, ItemKind,
632 ItemWrite, Label, LabelFilter, Location, MetadataKey, Metering, NativeId, NewComment, Page,
633 PageRequest, Priority, Project, ProjectFilter, ProjectQuery, Repository, SecretResolver,
634 SharedClock, SourceError, SourceName, SourcePlugin, Status, StatusCategory, StatusMapping,
635 Support, Task, TaskDetailRead, TaskQuery, TaskRef, TaskSource, TaskUpdate, TaskUpdateOutcome,
636 TextFields, TextQuery, UnmappedStatus, UpdatedField, WriteSupport, system_clock,
637};
638use reqwest::{Client, StatusCode, Url};
639use schemars::{Schema, schema_for};
640use secrecy::{ExposeSecret, SecretString};
641use serde::{Deserialize, Serialize};
642use serde_json::{Value, json};
643
644pub mod accounting;
645mod assets;
646mod visibility;
647
648use accounting::Accounting;
649
650/// The registry name for this plugin.
651pub const KIND: &str = "github-projects";
652/// GitHub's maximum connection page size.
653pub const MAX_PAGE_SIZE: u32 = 100;
654/// Every page of a board-scoped narrowing search: 20 rows, one point of SEARCH_ISSUES, the
655/// most one point buys. GitHub prices that document by rows, so pages of 20 cost what pages
656/// of 100 cost per row, and a page of fewer than 20 costs the same one point.
657pub const SEARCH_PAGE_SIZE: u32 = 20;
658/// How many items one [`graphql::ISSUE_DETAILS`] request reads, each with the first page of
659/// its comments: the largest batch the node-count model prices at one point.
660///
661/// Each aliased item is resolved once, and what GitHub charges for it is the connections
662/// under it — its labels, its page of board memberships, the field values of each of those
663/// three memberships, and its comments: six requests' worth of the aggregate GitHub divides
664/// by a hundred and rounds. Twenty-four items come to 144, which rounds to one point;
665/// twenty-five come to 150, which rounds to two. `tests/point_cost.rs` prices the document at
666/// one point and fails if one item more would still be priced at one.
667pub const DETAIL_BATCH: usize = 24;
668
669/// The most nodes any one document this source sends may be asked to return.
670///
671/// GitHub's own published per-query ceiling, taken from
672/// [`github_graphql_node_count::NODE_LIMIT`] rather than written out again here, so this
673/// workspace cannot hold a stale copy of somebody else's number. A query above it is
674/// **refused before it is executed**, whoever is asking and whatever board they are
675/// asking about — so this is a bound on the documents rather than a budget that runs out.
676///
677/// This is `nodeCount`, the maximum number of nodes *one query may return*. It is not
678/// `cost`, the rate-limit points a call spends against an hourly allowance shared by
679/// everything the credential does — two numbers against two limits, and this constant
680/// bounds only the first. The second is computed offline too, per document:
681/// [`worst_case_point_cost`], pinned for every document in [`graphql::DOCUMENTS`] by
682/// `tests/point_cost.rs`, and reconciled against GitHub's own `cost` by the credentialed
683/// lane. There is no constant like this one to hold a price under, because points are an
684/// hourly allowance rather than a per-call bound.
685///
686/// Neither is a session's price. What `session-cost.md` records of a whole session is its
687/// **requests** and its **worst-case nodes**; what a whole session spends in points is
688/// reported only by a credentialed run's own `x-ratelimit-*` headers, through
689/// [`accounting`]. The module section on the three ways this source reaches an item says how
690/// the count is arrived at, and which of the page sizes below decide it.
691pub const NODE_COUNT_LIMIT: u64 = github_graphql_node_count::NODE_LIMIT;
692
693/// Nested connection size for the connections that hang off one item.
694///
695/// It multiplies through every document that reaches an item under a page — the count
696/// rules multiply down a nested path — so it is the constant [`NODE_COUNT_LIMIT`] is most
697/// sensitive to. `tests/node_count.rs` is what holds the pair together: it recomputes
698/// every document under these constants and fails naming any that reaches the limit, so
699/// raising this is caught there rather than by GitHub.
700const NESTED_PAGE_SIZE: u32 = 50;
701/// How many of one issue's board memberships are read when an issue is reached directly.
702///
703/// An issue reached through a search or through its own node id carries its board half in
704/// `Issue.projectItems`, and only the entry for *this* board is read. This connection sits
705/// under a page of issues, so every point of it multiplies through the whole document and
706/// is paid for whether or not any issue is on a second board — which is why it is
707/// deliberately far smaller than [`NESTED_PAGE_SIZE`].
708///
709/// **Three, because what a page misses is now recovered rather than refused**, and the
710/// recovery is what the value is chosen against. An issue whose entry for this board sits
711/// past this page costs one further request — [`graphql::ISSUE_BOARD_ITEMS`], resumed from
712/// that page's own cursor — so the value trades a bound every read pays for a request only
713/// a multi-board issue pays. At one, a deployment whose issues commonly sit on two or more
714/// boards would pay that request *per issue*, which is order N against the one page per
715/// hundred issues a read costs today. At three it is only reached by an issue on four or
716/// more boards at once, which keeps the recovery path exceptional rather than routine for
717/// a plausible deployment.
718const BOARD_ITEMS_PAGE_SIZE: u32 = 3;
719/// How many carriers of one copy origin one page of [`graphql::ORIGIN_LOOKUP`] asks each of
720/// its two connections for.
721///
722/// An origin names one item, so the answer an origin lookup expects is one carrier, and a
723/// second is a duplicate a copy already takes the first of. Both connections are walked to
724/// exhaustion whatever this is, so it decides how many requests an unusual answer costs and
725/// never what the answer is. It is small because every point of it is paid on every lookup,
726/// and a copy makes one lookup per item it has no link for: at three, ten lookups cost fewer
727/// worst-case nodes than the one whole-board read they replaced.
728const ORIGIN_PAGE_SIZE: u32 = 3;
729
730pub use github_graphql_node_count::{NodeCountError, Variables};
731
732/// The largest value this source can bind to each page-size variable its documents name.
733///
734/// Every `first:` in [`graphql`] reads one of these four, and each is capped at the
735/// constant above it wherever a caller's own limit could reach it — `$first` at
736/// [`MAX_PAGE_SIZE`], `$nestedFirst` at `NESTED_PAGE_SIZE`, `$boardItems` at
737/// `BOARD_ITEMS_PAGE_SIZE`, `$originFirst` at `ORIGIN_PAGE_SIZE`. So this is the worst case a caller can drive this source to,
738/// not one configuration of it, which is what makes a bound computed under it a bound on
739/// every read.
740pub fn largest_page_sizes() -> Variables {
741 Variables::from([
742 ("first".to_owned(), MAX_PAGE_SIZE),
743 ("nestedFirst".to_owned(), NESTED_PAGE_SIZE),
744 ("boardItems".to_owned(), BOARD_ITEMS_PAGE_SIZE),
745 ("originFirst".to_owned(), ORIGIN_PAGE_SIZE),
746 ])
747}
748
749/// The most nodes `document` could be asked to return, by GitHub's published rules.
750///
751/// Computed offline from the document's own text under [`largest_page_sizes`] — no
752/// network, no credential and no schema — by
753/// [`github_graphql_node_count::node_count`], which is where the rules themselves live.
754/// A document at or above [`NODE_COUNT_LIMIT`] is one GitHub refuses before executing, so
755/// this is what a check holds every document in [`graphql::DOCUMENTS`] below.
756///
757/// # Errors
758///
759/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
760/// no single operation, or binds a page size this source does not name — each of which is
761/// a defect in the document rather than a number.
762pub fn worst_case_node_count(document: &str) -> Result<u64, NodeCountError> {
763 node_count(document, &largest_page_sizes())
764}
765
766/// The most rate-limit points one call of `document` could spend, by GitHub's published
767/// rules.
768///
769/// Computed offline from the document's own text under [`largest_page_sizes`] — no
770/// network, no credential and no schema — by
771/// [`github_graphql_node_count::point_cost`], which is where the rules themselves live.
772/// This is `cost`, metered **per hour** against the allowance one credential shares across
773/// everything it does; it is not `nodeCount`, which is [`worst_case_node_count`] and is
774/// bounded per query by [`NODE_COUNT_LIMIT`]. There is no per-call ceiling to hold this
775/// under, so what `tests/point_cost.rs` does with it is pin every document in
776/// [`graphql::DOCUMENTS`] at what it costs, and the credentialed lane reconciles those
777/// figures against GitHub's own reported `cost`.
778///
779/// # Errors
780///
781/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
782/// no single operation, or binds a page size this source does not name — each of which is
783/// a defect in the document rather than a number.
784pub fn worst_case_point_cost(document: &str) -> Result<u64, NodeCountError> {
785 github_graphql_node_count::point_cost(document, &largest_page_sizes())
786}
787
788/// The most nodes `document` could be asked to return under `variables`.
789///
790/// [`worst_case_node_count`] is this under [`largest_page_sizes`], and the accounting in
791/// [`accounting`] is this under the bindings one request really sent — one spelling of the
792/// calculation, so a bound checked offline and a cost recorded at run time cannot come to
793/// disagree. The rules themselves live in [`github_graphql_node_count::node_count`].
794///
795/// # Errors
796///
797/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
798/// no single operation, or binds a page size `variables` does not name.
799pub fn node_count(document: &str, variables: &Variables) -> Result<u64, NodeCountError> {
800 github_graphql_node_count::node_count(document, variables)
801}
802
803/// The issue-title prefix that makes a board issue a document.
804///
805/// A GitHub Projects board has no document type — it holds issues — so the discriminator
806/// is the title, and this is the whole of it: an issue whose title begins with these bytes
807/// is a document and every other issue is the task or project the sub-issue rule makes it.
808///
809/// It is spelled **once**, here, and read rather than restated everywhere else — including
810/// by the shared journeys, which take it from this constant so a board fixture cannot
811/// drift from what this source reads. `docs/metadata.md` records the two consequences that
812/// are not obvious from the bytes: the reported title has this prefix taken off, exactly
813/// as the body's metadata slot is taken off `content`, and this prefix is read *before*
814/// the sub-issue rule, so a design issue with no sub-issues is never an empty project.
815pub const DESIGN_TITLE_PREFIX: &str = "DESIGN: ";
816
817/// Exact GraphQL query documents issued by this plugin.
818///
819/// Keeping the production documents here lets the pinned-schema test validate the same
820/// bytes that are sent to GitHub, rather than a test-only copy which could drift
821/// independently. [`STATUS_OPTIONS_UPDATE`] is the sole document that may rewrite a board
822/// field, and its guarded caller always supplies the complete existing option set with ids.
823pub mod graphql {
824 /// The board half of one item: the field values every document here reads it from.
825 ///
826 /// A macro for the same reason [`board_issue!`] below is one, a level further in. This
827 /// selection is needed by that fragment, by [`BOARD`] under the board's own `items`,
828 /// and by [`ISSUE_BOARD_ITEMS`] under a membership walk — and all three have to produce
829 /// *the same value*, because
830 /// [`GitHubProjectsSource::resolve`](super::GitHubProjectsSource) reads them through
831 /// one path. Three spellings of it is what would drift, so there is one.
832 ///
833 /// The `Status` option and this source's own origin text field are the whole of it. It
834 /// selects no `ProjectV2ItemFieldLabelValue`: GitHub derives that field from the item's
835 /// content, so it holds nothing the content's own `labels` do not already say, and it
836 /// would sit a label connection two page sizes deep.
837 macro_rules! board_item_values {
838 () => {
839 r#"fieldValues(first:$nestedFirst){nodes{
840 ... on ProjectV2ItemFieldSingleSelectValue{name field{
841 ... on ProjectV2SingleSelectField{id name options{id name}}
842 }}
843 ... on ProjectV2ItemFieldTextValue{text field{... on ProjectV2Field{id name}}}
844 }pageInfo{hasNextPage}}"#
845 };
846 }
847
848 /// Everything this source reads about one issue, wherever it reaches that issue.
849 ///
850 /// A macro rather than a constant so the three documents below can `concat!` it: one
851 /// spelling of these fields is what makes an issue read through the board-scoped
852 /// search, through its own node id, and through its project's sub-issue relationship
853 /// resolve to *the same* item, which is the whole of what
854 /// [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource) relies on.
855 ///
856 /// `projectItems` is what carries the board half of an issue: the board item's own id
857 /// and the [`board_item_values!`] above — the `Status` option and this source's origin
858 /// text field — that a `ProjectV2.items` read used to carry. It is asked for on the
859 /// issue rather than on the board, which is what makes the cost of a read proportional
860 /// to what was asked for instead of to the board's size.
861 ///
862 /// It carries a *page* of that connection, at `BOARD_ITEMS_PAGE_SIZE`, and its
863 /// `endCursor` is what [`ISSUE_BOARD_ITEMS`] resumes from when this board's entry is
864 /// not on that page: a page here is where the search for the entry starts rather than
865 /// where it ends.
866 ///
867 /// It does **not** select the board's `Labels` field value, and that is the whole of
868 /// what keeps the three documents below under [`NODE_COUNT_LIMIT`](super::NODE_COUNT_LIMIT):
869 /// a label connection there sits under `fieldValues` under `projectItems` under a page
870 /// of issues, spending `$nestedFirst` twice down one path, and took
871 /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] to 2,556,100 nodes against a limit of 500,000.
872 /// No label is lost — this is a fragment `on Issue`, whose own `labels` are selected
873 /// above, and that connection is where every label this source reports comes from. No
874 /// document in this module selects the board field any longer, [`BOARD`] included; the
875 /// module documentation records why nothing it could have held is lost.
876 macro_rules! board_issue {
877 () => {
878 concat!(
879 r#" fragment BoardIssue on Issue{__typename id number title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total}
880 labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}
881 projectItems(first:$boardItems){nodes{id project{id number}
882 "#,
883 board_item_values!(),
884 r#"}pageInfo{hasNextPage endCursor}}}"#
885 )
886 };
887 }
888
889 /// Every issue of one board, found by a search scoped to that board.
890 ///
891 /// This is how the projects a board holds are listed, and it selects no `items`
892 /// connection on `ProjectV2`: the board is a *qualifier of the search* rather than a
893 /// container walked page by page, so nothing nested inside a board item is paid for.
894 /// Which of the issues it returns is a project is then read off `parent` — GitHub
895 /// accepts `-has:parent` as a search qualifier and silently ignores it, so the
896 /// discriminator has to be applied to the field, which is a scalar on the issue and
897 /// costs nothing.
898 pub const SEARCH_ISSUES: &str = concat!(
899 r#"query($search:String!,$type:SearchType!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!){
900 search(query:$search,type:$type,first:$first,after:$after){
901 pageInfo{hasNextPage endCursor}
902 nodes{__typename ...BoardIssue}
903 }
904 }"#,
905 board_issue!()
906 );
907
908 /// What a dependency read selects of each far end: enough to say which kind of item it
909 /// is, its body included for the kind marker.
910 macro_rules! related_issue {
911 () => {
912 " fragment Related on Issue{id title body parent{id} subIssuesSummary{total}}"
913 };
914 }
915
916 /// One issue by its own node id, which is what a qualified id names here — with what a
917 /// write of it needs and the issue does not carry in `board_issue!`: the field
918 /// definitions of the boards it sits on, and the far ends of its `blockedBy`.
919 ///
920 /// Strongly consistent, unlike the search above: GitHub's issue search is an index and
921 /// answers a write made moments ago with the value from before it, and resolving a node
922 /// id does not.
923 ///
924 /// **Why those two ride here and not on the fragment.** A copy or an update of an item
925 /// reads it by its own id, and with them that one read answers everything the write
926 /// needs: which option ids the board's `Status` and `Priority` fields hold — so no
927 /// [`BOARD_FIELDS`] — and which issues block it, with each one's kind — so no
928 /// [`ISSUE_DEPENDENCIES`]. On `board_issue!` they would sit under the hundred-issue
929 /// pages of [`SEARCH_ISSUES`] and [`SUB_ISSUES`], multiplying both documents' price. Here
930 /// they sit under one item, and this read is still one point.
931 pub const ISSUE: &str = concat!(
932 r#"query($id:ID!,$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!){
933 node(id:$id){__typename ...BoardIssue ... on Issue{
934 boards:projectItems(first:$boardItems){nodes{project{id number fields(first:$nestedFirst){nodes{
935 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
936 ... on ProjectV2Field{__typename id name dataType}
937 }pageInfo{hasNextPage}}}}}
938 blockedBy(first:$first){nodes{...Related}pageInfo{hasNextPage endCursor}}
939 }}
940 }"#,
941 board_issue!(),
942 related_issue!()
943 );
944
945 /// One project's tasks: the sub-issues of the issue that project is, each with a page of
946 /// what blocks it.
947 ///
948 /// The work this costs is the project's own size. Nothing about it grows as the board
949 /// gains projects, or as those projects gain tasks.
950 ///
951 /// **Why `blockedBy` rides here and on no other page of issues.** What reads a project's
952 /// tasks reads their edges next — `project graph` draws them, a copy carries them — and
953 /// without them here that is one [`ISSUE_DEPENDENCIES`] per task, so the requests a graph
954 /// costs grow with its tasks rather than with the pages of them. Carried here, a task
955 /// blocked by no more than `$nestedFirst` issues answers its forward edges from this read,
956 /// exactly as an [`ISSUE`] read of it does, and only one blocked by more is asked again.
957 /// It adds a connection under each issue of the page — one rate-limit point per page,
958 /// and `$nestedFirst` nodes per issue — and is kept off [`SEARCH_ISSUES`], whose pages
959 /// answer questions that never read an edge.
960 pub const SUB_ISSUES: &str = concat!(
961 r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!){
962 node(id:$id){__typename
963 ... on Issue{subIssues(first:$first,after:$after){
964 pageInfo{hasNextPage endCursor}
965 nodes{__typename ...BoardIssue ... on Issue{
966 blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}
967 }}
968 }}}
969 }"#,
970 board_issue!(),
971 related_issue!()
972 );
973
974 /// What a read of the board's own `items` selects of each item's content.
975 ///
976 /// A macro for the reason [`board_item_values!`] is one: [`BOARD`] and [`ORIGIN_LOOKUP`]
977 /// both walk `ProjectV2.items` and hand each item to one resolver, so they select its
978 /// content by one spelling.
979 macro_rules! board_item_content {
980 () => {
981 r#" content{
982 ... on Issue{__typename id number title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total} labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}}
983 ... on PullRequest{__typename id}
984 ... on DraftIssue{__typename id title body createdAt updatedAt}
985 }"#
986 };
987 }
988
989 /// Reads the board's fields and one page of its items.
990 pub const BOARD: &str = concat!(
991 r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$nestedFirst:Int!,$duplicates:Boolean!){
992 owner:repositoryOwner(login:$owner){
993 ... on ProjectV2Owner{projectV2(number:$number){...Board}}
994 }
995 } fragment Board on ProjectV2 { id title
996 fields(first:$nestedFirst){nodes{
997 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
998 ... on ProjectV2Field{__typename id name dataType}
999 }pageInfo{hasNextPage}}
1000 items(first:$first,after:$after){nodes{id "#,
1001 board_item_values!(),
1002 board_item_content!(),
1003 r#"} pageInfo{hasNextPage endCursor}}
1004 }"#
1005 );
1006
1007 /// Every carrier of one copy origin, by two reads in one request, and nothing else of
1008 /// the board.
1009 ///
1010 /// **`originItems`** is the board's own items narrowed by its own field filter —
1011 /// `ProjectV2.items(query:)`, which GitHub's schema declares as "Search query for
1012 /// filtering items" — to those whose `onetaskgraph.origin` text field holds the
1013 /// qualified id, quoted. It reads the field every carrier already holds, whichever release
1014 /// wrote it, and matches it exactly: measured on 2026-09-29 against a 394-item board,
1015 /// the quoted, the unquoted and the bare-value spellings each returned exactly the one
1016 /// carrier and a prefix of the value returned none. It is `ProjectV2.items`, so it lags a
1017 /// fresh `addProjectV2ItemById` the way that connection does.
1018 ///
1019 /// **`search`** is the board-scoped issue search for the same id as a quoted phrase in
1020 /// the body, which is where this source mirrors the origin into its metadata slot. GitHub
1021 /// indexes that comment, and the index catches up with a write in a second or two rather
1022 /// than in minutes, so it finds a carrier another process wrote that the first read is
1023 /// still behind on.
1024 ///
1025 /// Each connection pages at `$originFirst`, its own small size — see `ORIGIN_PAGE_SIZE`
1026 /// — and resumes from its own cursor; a connection already walked to its end is resumed
1027 /// from its last cursor, which answers an empty page. Every candidate either read returns
1028 /// is confirmed against its own origin field before it is reported, so a token match of
1029 /// the search or anything else the filter admits never is.
1030 ///
1031 /// The root is aliased `originItems` rather than `owner`, so nothing counting the board's
1032 /// own whole reads counts this one among them.
1033 pub const ORIGIN_LOOKUP: &str = concat!(
1034 r#"query($owner:String!,$number:Int!,$filter:String!,$search:String!,$type:SearchType!,$originFirst:Int!,$itemsAfter:String,$searchAfter:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!){
1035 originItems:repositoryOwner(login:$owner){
1036 ... on ProjectV2Owner{projectV2(number:$number){
1037 items(first:$originFirst,after:$itemsAfter,query:$filter){nodes{id "#,
1038 board_item_values!(),
1039 board_item_content!(),
1040 r#"} pageInfo{hasNextPage endCursor}}
1041 }}
1042 }
1043 search(query:$search,type:$type,first:$originFirst,after:$searchAfter){
1044 pageInfo{hasNextPage endCursor}
1045 nodes{__typename ...BoardIssue}
1046 }
1047 }"#,
1048 board_issue!()
1049 );
1050
1051 /// The board's own id and field definitions, and not one of its items.
1052 ///
1053 /// What a write needs of the board when the item it writes does not say: the id a field
1054 /// write and `addProjectV2ItemById` address, and the definitions of the `Status` and
1055 /// origin fields. It selects no `items`, so what it costs is the board's field list
1056 /// however many items the board holds — and it decides nothing about which items those
1057 /// are, which is the question a read of one item by its own id answers instead.
1058 ///
1059 /// The root is aliased `boardFields` rather than `owner`, so nothing counting the
1060 /// board's item reads by their root counts this one among them.
1061 pub const BOARD_FIELDS: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!){
1062 boardFields:repositoryOwner(login:$owner){
1063 ... on ProjectV2Owner{projectV2(number:$number){id
1064 fields(first:$nestedFirst){nodes{
1065 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1066 ... on ProjectV2Field{__typename id name dataType}
1067 }pageInfo{hasNextPage}}
1068 }}
1069 }
1070 }"#;
1071
1072 /// One board draft by its own node id, with the board item it sits in.
1073 ///
1074 /// A draft is not an issue, so [`ISSUE`] reaches it and reads nothing of it; this is the
1075 /// second read that answers it. `DraftIssue.projectV2Items` names the board item a draft
1076 /// is — GitHub links a draft to one item — with the same [`board_item_values!`] the
1077 /// issue fragment reads, so a draft reached by id resolves through the same resolver a
1078 /// board listing hands it to, and nothing has to list the board to find one.
1079 pub const DRAFT: &str = concat!(
1080 r#"query($id:ID!,$nestedFirst:Int!,$boardItems:Int!){
1081 node(id:$id){__typename ... on DraftIssue{id title body createdAt updatedAt
1082 projectV2Items(first:$boardItems){nodes{id project{id number}
1083 "#,
1084 board_item_values!(),
1085 r#"}pageInfo{hasNextPage endCursor}}}}
1086 }"#
1087 );
1088
1089 /// One issue's board memberships alone, walked past the page a read of it carried.
1090 ///
1091 /// The recovery read behind [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource):
1092 /// every document above carries a *page* of `Issue.projectItems`, and an issue on more
1093 /// boards than that page holds may have this board's entry past its end. This asks that
1094 /// one issue for its memberships and nothing else — the caller already holds the issue —
1095 /// so an answer of "this board does not hold it" is only ever given about a connection
1096 /// read to exhaustion.
1097 ///
1098 /// It selects the board item's id, its project number and the same
1099 /// [`board_item_values!`] the fragment does, because what it produces is handed to the
1100 /// very same resolver: an issue recovered this way reports the same title, the same
1101 /// status, the same labels and the same qualified id as one whose entry was on the
1102 /// page.
1103 ///
1104 /// `$first` rather than `$boardItems`: this document reads one issue, so nothing
1105 /// multiplies through it and the membership connection can be walked at
1106 /// [`MAX_PAGE_SIZE`](super::MAX_PAGE_SIZE) — which is what keeps the recovery to one
1107 /// further request for any issue a person really keeps.
1108 pub const ISSUE_BOARD_ITEMS: &str = concat!(
1109 r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!){
1110 node(id:$id){
1111 ... on Issue{projectItems(first:$first,after:$after){
1112 nodes{id project{id number}
1113 "#,
1114 board_item_values!(),
1115 r#"}
1116 pageInfo{hasNextPage endCursor}}}
1117 }
1118 }"#
1119 );
1120 /// Whether the board's Project is public — half of what decides whether a write here can
1121 /// be read by anybody. Needs the `read:project` scope.
1122 pub const PROJECT_VISIBILITY: &str = r#"query($owner:String!,$number:Int!){visibility:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){public}}}}"#;
1123 /// Resolves the configured repository's node id, which creating an issue requires.
1124 pub const REPOSITORY: &str = r#"query($owner:String!,$name:String!){repository(owner:$owner,name:$name){id nameWithOwner}}"#;
1125 /// What creating an issue needs and has not read yet: the board's own id and field
1126 /// definitions, as [`BOARD_FIELDS`] reads them, and the node id of the repository the
1127 /// issue is created in, as [`REPOSITORY`] reads it — in one request.
1128 ///
1129 /// Sent at the point a create knows which repository it is for, when neither half is
1130 /// already known to this process; a create needing only one of them sends that one's own
1131 /// document. Neither half is kept past the process: a field's option ids are re-minted by
1132 /// `sources fields --apply`, so a copy of them held between runs would write the wrong
1133 /// status.
1134 pub const CREATION_CONTEXT: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!,$repositoryOwner:String!,$repositoryName:String!){
1135 boardFields:repositoryOwner(login:$owner){
1136 ... on ProjectV2Owner{projectV2(number:$number){id
1137 fields(first:$nestedFirst){nodes{
1138 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1139 ... on ProjectV2Field{__typename id name dataType}
1140 }pageInfo{hasNextPage}}
1141 }}
1142 }
1143 repository(owner:$repositoryOwner,name:$repositoryName){id nameWithOwner}
1144 }"#;
1145 /// Reads both dependency directions for one issue, with each far end's own kind — and
1146 /// the issue's own body, which is where an edge to another source is recorded, so that
1147 /// half of a dependency read needs no second read of the issue or of the board.
1148 pub const ISSUE_DEPENDENCIES: &str = concat!(
1149 r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
1150 ... on Issue{body
1151 blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1152 blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1153 }}}"#,
1154 related_issue!()
1155 );
1156 /// Creates one issue in the configured repository, on no board: [`ADD_TO_BOARD`] files
1157 /// it. `CreateIssueInput.projectV2Ids` is not sent — see the crate's notes on what GitHub
1158 /// answered when it was.
1159 pub const CREATE_ISSUE: &str =
1160 r#"mutation($input:CreateIssueInput!){createIssue(input:$input){issue{id number url}}}"#;
1161 /// Puts an existing issue on the configured board.
1162 pub const ADD_TO_BOARD: &str = r#"mutation($input:AddProjectV2ItemByIdInput!){addProjectV2ItemById(input:$input){item{id}}}"#;
1163 /// Updates an issue's visible fields and its open or closed state in one call.
1164 pub const UPDATE_ISSUE: &str =
1165 r#"mutation($input:UpdateIssueInput!){updateIssue(input:$input){issue{id}}}"#;
1166 /// Updates an existing draft's user-visible fields.
1167 pub const UPDATE_DRAFT: &str = r#"mutation($input:UpdateProjectV2DraftIssueInput!){updateProjectV2DraftIssue(input:$input){draftIssue{id}}}"#;
1168 /// Updates a text or single-select value on one project item.
1169 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}}}}}}}}"#;
1170 /// Writes up to six board fields and clears up to three in one ordered mutation: every
1171 /// aliased field is included by its own boolean, as [`FIELD_WRITE_SLOTS`](super::FIELD_WRITE_SLOTS)
1172 /// and [`FIELD_CLEAR_SLOTS`](super::FIELD_CLEAR_SLOTS) name them, writes before clears.
1173 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}}}"#;
1174 /// Clears one project item's value of one field, which is what a `none` priority is.
1175 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}}}}}}}}"#;
1176 /// Creates one field: a single-select one with its options, or a text one for a projected
1177 /// metadata value. Only the guarded field setup may use this document, and only for a
1178 /// field the board lacks.
1179 pub const CREATE_FIELD: &str = r#"mutation($input:CreateProjectV2FieldInput!){createProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id name options{id name color description}}}}}"#;
1180 /// Replaces a single-select field's options. Only the guarded field setup — the
1181 /// `status-options` and `fields` operations — may use this document, because GitHub
1182 /// treats the input as the complete option list.
1183 pub const STATUS_OPTIONS_UPDATE: &str = r#"mutation($input:UpdateProjectV2FieldInput!){updateProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id options{id name color description}}}}}"#;
1184 /// A fresh snapshot of the Status field and every board item's assignment.
1185 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}}}}}}"#;
1186 /// Files one issue under another as a sub-issue, which is what project membership is.
1187 pub const ADD_SUB_ISSUE: &str =
1188 r#"mutation($input:AddSubIssueInput!){addSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1189 /// Takes one issue back out of its parent.
1190 pub const REMOVE_SUB_ISSUE: &str = r#"mutation($input:RemoveSubIssueInput!){removeSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1191 /// Adds GitHub's native issue blocked-by relationship.
1192 pub const ADD_BLOCKED_BY: &str = r#"mutation($input:AddBlockedByInput!){addBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1193 /// Removes one native issue blocked-by relationship.
1194 pub const REMOVE_BLOCKED_BY: &str = r#"mutation($input:RemoveBlockedByInput!){removeBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1195 /// Deletes one issue, which takes its board item with it.
1196 ///
1197 /// The engine sends this in one situation only: undoing a copy that could not finish,
1198 /// over the items that same copy created. Deleting the issue removes the board item
1199 /// too, so there is no second `deleteProjectV2Item` to keep in step with it.
1200 pub const DELETE_ISSUE: &str =
1201 r#"mutation($input:DeleteIssueInput!){deleteIssue(input:$input){repository{id}}}"#;
1202
1203 /// Everything this source reads about one issue comment, wherever it reaches one.
1204 ///
1205 /// A macro for the reason [`board_issue!`] is one: a comment listed, a comment just added
1206 /// and a comment just edited are handed to one mapper, so they are selected by one
1207 /// spelling. `author` is `Actor`, which GitHub answers `null` for an account that no
1208 /// longer exists, and `login` is the one member every kind of actor carries.
1209 macro_rules! issue_comment {
1210 () => {
1211 "id author{login} createdAt updatedAt body url"
1212 };
1213 }
1214
1215 /// One task's comments: a page of its issue's own `comments` connection.
1216 ///
1217 /// **No `orderBy`, and that is what makes the page oldest first.** GitHub's only
1218 /// `IssueCommentOrder` field is `UPDATED_AT`, which would move a comment to the end of the
1219 /// list every time somebody edited it; left unordered the connection answers in the order
1220 /// the comments were written, which is the order GitHub documents for the same collection
1221 /// over REST — ascending id. Nothing multiplies through it, so `$first` is the whole of its
1222 /// node count and the caller's own page size is pushed straight down.
1223 pub const ISSUE_COMMENTS: &str = concat!(
1224 r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename ... on Issue{comments(first:$first,after:$after){nodes{"#,
1225 issue_comment!(),
1226 r#"}pageInfo{hasNextPage endCursor}}}}}"#
1227 );
1228 /// One issue by its own node id, with a page of its comments: what `task show` and a
1229 /// comment listing read, in one request.
1230 ///
1231 /// [`ISSUE`] and [`ISSUE_COMMENTS`] in one document, rather than one then the other. The
1232 /// comments are selected here and **not** on the shared `board_issue!` fragment, which
1233 /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] nest under a page of a hundred issues: a comment
1234 /// connection there would multiply through both of those documents' price, and neither
1235 /// needs one.
1236 pub const ISSUE_DETAIL: &str = concat!(
1237 r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!){
1238 node(id:$id){__typename ...BoardIssue ... on Issue{comments(first:$first,after:$after){nodes{"#,
1239 issue_comment!(),
1240 r#"}pageInfo{hasNextPage endCursor}}}}
1241 }"#,
1242 board_issue!()
1243 );
1244
1245 /// One alias of [`ISSUE_DETAILS`]: the item a batch's `$id<n>` names, with the first
1246 /// page of its comments when `$comments` asks for them.
1247 macro_rules! issue_details_alias {
1248 ($n:literal) => {
1249 concat!(
1250 "\n i",
1251 stringify!($n),
1252 ":node(id:$id",
1253 stringify!($n),
1254 "){__typename ...BoardIssue ... on Issue{comments(first:$first) @include(if:$comments){nodes{",
1255 issue_comment!(),
1256 "}pageInfo{hasNextPage endCursor}}}}"
1257 )
1258 };
1259 }
1260
1261 /// [`ISSUE_DETAIL`] for [`DETAIL_BATCH`](super::DETAIL_BATCH) items at once, each by its
1262 /// own node id, as one fixed-size document of aliased `node(id:)` fields.
1263 ///
1264 /// **Aliased `node(id:)` rather than `nodes(ids:)`, and that is what keeps its price
1265 /// honest.** The `github-graphql-node-count` model this workspace prices with treats a
1266 /// field that supplies neither `first` nor `last` as free, and `nodes(ids:)` supplies
1267 /// neither — so every connection under it would be priced at nothing and the pin in
1268 /// `tests/point_cost.rs` would understate what GitHub charges. Each alias here is the
1269 /// one-item read the model already prices, so the batch costs what its aliases cost.
1270 ///
1271 /// **Fixed-size, so there is one document to price.** A batch of fewer items binds the
1272 /// slots it has no item for to the last item it does, and reads that item again; the
1273 /// price is the document's, whatever its variables, so a short batch costs what a full
1274 /// one does and nothing more.
1275 pub const ISSUE_DETAILS: &str = concat!(
1276 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!){"#,
1277 issue_details_alias!(0),
1278 issue_details_alias!(1),
1279 issue_details_alias!(2),
1280 issue_details_alias!(3),
1281 issue_details_alias!(4),
1282 issue_details_alias!(5),
1283 issue_details_alias!(6),
1284 issue_details_alias!(7),
1285 issue_details_alias!(8),
1286 issue_details_alias!(9),
1287 issue_details_alias!(10),
1288 issue_details_alias!(11),
1289 issue_details_alias!(12),
1290 issue_details_alias!(13),
1291 issue_details_alias!(14),
1292 issue_details_alias!(15),
1293 issue_details_alias!(16),
1294 issue_details_alias!(17),
1295 issue_details_alias!(18),
1296 issue_details_alias!(19),
1297 issue_details_alias!(20),
1298 issue_details_alias!(21),
1299 issue_details_alias!(22),
1300 issue_details_alias!(23),
1301 "\n }",
1302 board_issue!()
1303 );
1304
1305 /// Which issue one comment is on, read before that comment is edited or removed.
1306 ///
1307 /// GitHub's comment mutations take the comment's id and nothing else, so without this a
1308 /// comment id given against the wrong task would change a comment on another issue.
1309 pub const COMMENT_ISSUE: &str =
1310 r#"query($id:ID!){node(id:$id){__typename ... on IssueComment{id issue{id}}}}"#;
1311 /// Adds one comment to an issue, signed as the account the token belongs to.
1312 pub const ADD_COMMENT: &str = concat!(
1313 r#"mutation($input:AddCommentInput!){addComment(input:$input){subject{id} commentEdge{node{"#,
1314 issue_comment!(),
1315 r#"}}}}"#
1316 );
1317 /// Replaces the body of one issue comment.
1318 pub const UPDATE_COMMENT: &str = concat!(
1319 r#"mutation($input:UpdateIssueCommentInput!){updateIssueComment(input:$input){issueComment{"#,
1320 issue_comment!(),
1321 r#"}}}"#
1322 );
1323 /// Removes one issue comment. Its payload carries nothing about the comment it removed.
1324 pub const DELETE_COMMENT: &str = r#"mutation($input:DeleteIssueCommentInput!){deleteIssueComment(input:$input){clientMutationId}}"#;
1325
1326 /// Every document above, with what this source is doing when it sends one.
1327 ///
1328 /// One list rather than a `match` beside the constants: a rate-limit diagnostic has to
1329 /// name the call that was refused, and a `match` with a catch-all arm would answer a
1330 /// document added later with "talking to GitHub" and never say so.
1331 ///
1332 /// `documents_are_all_inventoried` reads this file back and fails naming any `pub
1333 /// const` here that this list omits, so the two cannot part — which is the same guard
1334 /// `CATEGORIES` carries, in the one shape available to a set of `&str` constants.
1335 pub const DOCUMENTS: [(&str, &str); 34] = [
1336 (SEARCH_ISSUES, "searching this board's issues"),
1337 (ISSUE, "reading one issue"),
1338 (
1339 ISSUE_BOARD_ITEMS,
1340 "reading one issue's board memberships past the page it came with",
1341 ),
1342 (SUB_ISSUES, "reading a project's tasks"),
1343 (BOARD, "reading the board"),
1344 (ORIGIN_LOOKUP, "looking up the items copied from one origin"),
1345 (BOARD_FIELDS, "reading the board's fields"),
1346 (DRAFT, "reading one draft"),
1347 (REPOSITORY, "reading the destination repository"),
1348 (
1349 PROJECT_VISIBILITY,
1350 "reading whether the board's project is public",
1351 ),
1352 (
1353 CREATION_CONTEXT,
1354 "reading the board's fields and the destination repository",
1355 ),
1356 (ISSUE_DEPENDENCIES, "reading an issue's dependencies"),
1357 (CREATE_ISSUE, "creating an issue"),
1358 (ADD_TO_BOARD, "adding an issue to the board"),
1359 (UPDATE_ISSUE, "updating an issue"),
1360 (UPDATE_DRAFT, "updating a draft item"),
1361 (UPDATE_FIELD, "writing a board field"),
1362 (UPDATE_FIELDS, "writing board fields together"),
1363 (CLEAR_FIELD, "clearing a board field"),
1364 (
1365 CREATE_FIELD,
1366 "creating a board single-select field with its options",
1367 ),
1368 (
1369 STATUS_OPTIONS_SNAPSHOT,
1370 "snapshotting board Status options and assignments",
1371 ),
1372 (
1373 STATUS_OPTIONS_UPDATE,
1374 "safely replacing the board Status option list",
1375 ),
1376 (ADD_SUB_ISSUE, "filing an issue under its project"),
1377 (REMOVE_SUB_ISSUE, "taking an issue out of its project"),
1378 (ADD_BLOCKED_BY, "recording a dependency"),
1379 (REMOVE_BLOCKED_BY, "removing a dependency"),
1380 (DELETE_ISSUE, "deleting an issue"),
1381 (ISSUE_COMMENTS, "reading a task's comments"),
1382 (ISSUE_DETAIL, "reading one issue with its comments"),
1383 (
1384 ISSUE_DETAILS,
1385 "reading a batch of issues with their comments",
1386 ),
1387 (COMMENT_ISSUE, "reading which issue a comment is on"),
1388 (ADD_COMMENT, "adding a comment"),
1389 (UPDATE_COMMENT, "editing a comment"),
1390 (DELETE_COMMENT, "deleting a comment"),
1391 ];
1392}
1393
1394/// One aliased mutation field of [`graphql::UPDATE_FIELDS`]: the key its answer comes back
1395/// under, the variable carrying its input, and the boolean variable that includes it.
1396#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1397pub struct FieldSlot {
1398 /// The key of the answer's `data` this field's payload is under.
1399 pub alias: &'static str,
1400 /// The variable carrying this field's input.
1401 pub variable: &'static str,
1402 /// The boolean variable whose `true` sends this field.
1403 pub include: &'static str,
1404}
1405
1406/// The value writes [`graphql::UPDATE_FIELDS`] carries, in the order GitHub runs them.
1407pub const FIELD_WRITE_SLOTS: [FieldSlot; 6] = [
1408 FieldSlot {
1409 alias: "updateProjectV2ItemFieldValue",
1410 variable: "input",
1411 include: "writeFirst",
1412 },
1413 FieldSlot {
1414 alias: "second",
1415 variable: "second",
1416 include: "writeSecond",
1417 },
1418 FieldSlot {
1419 alias: "third",
1420 variable: "third",
1421 include: "writeThird",
1422 },
1423 FieldSlot {
1424 alias: "fourth",
1425 variable: "fourth",
1426 include: "writeFourth",
1427 },
1428 FieldSlot {
1429 alias: "fifth",
1430 variable: "fifth",
1431 include: "writeFifth",
1432 },
1433 FieldSlot {
1434 alias: "sixth",
1435 variable: "sixth",
1436 include: "writeSixth",
1437 },
1438];
1439
1440/// The clears [`graphql::UPDATE_FIELDS`] carries, run after every write of the same request.
1441pub const FIELD_CLEAR_SLOTS: [FieldSlot; 3] = [
1442 FieldSlot {
1443 alias: "cleared",
1444 variable: "clear",
1445 include: "writeClear",
1446 },
1447 FieldSlot {
1448 alias: "clearedSecond",
1449 variable: "clearSecond",
1450 include: "writeClearSecond",
1451 },
1452 FieldSlot {
1453 alias: "clearedThird",
1454 variable: "clearThird",
1455 include: "writeClearThird",
1456 },
1457];
1458
1459/// Which of GitHub's two rate limiters refused a request.
1460///
1461/// Waiting is the whole answer to the primary budget, and polling is what *extends* the
1462/// secondary one — so an operator told the wrong one takes the wrong next step, which is
1463/// the whole reason this is carried rather than collapsed into "rate limited".
1464#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1465enum Limiter {
1466 /// The hourly API budget, which `gh api rate_limit` reports and a wait answers.
1467 Primary,
1468 /// The burst limiter over content-generating requests, which nothing reports.
1469 Secondary,
1470}
1471
1472/// The wordings GitHub answers a secondary rate limit with.
1473///
1474/// It sends them under a forbidden status, under a too-many-requests status, and inside
1475/// the `errors` of a *successful* response, which is why the text is what this matches on
1476/// rather than the status. `abuse detection` is the wording GitHub used before the
1477/// limiter was renamed and still returns from some endpoints; `submitted too quickly` is
1478/// what a burst of content creation is refused with.
1479///
1480/// This is GitHub's vocabulary rather than this source's, so it is pinned rather than
1481/// remembered: `tests/fixtures/rate-limits.json` records where each wording was read and
1482/// when, and the drift gate reconciles the two lists both ways. Public for that gate
1483/// alone — a caller has no use for it, and matching on a refusal is this source's job.
1484pub const SECONDARY_WORDINGS: [&str; 5] = [
1485 "secondary rate limit",
1486 "temporarily blocked from content creation",
1487 "abuse detection",
1488 "submitted too quickly",
1489 "exceeded a secondary",
1490];
1491
1492/// The wordings GitHub answers an exhausted primary budget with.
1493///
1494/// `rate_limited` is the `type` its GraphQL error carries, which is read as a field rather
1495/// than looked for in the response text. `api rate limit already exceeded` is what GraphQL
1496/// answers a request made once the hour's budget is spent — "API rate limit already exceeded
1497/// for user ID …" in the `errors` of an HTTP 200, with no `type` — and neither of the other
1498/// two phrases is a substring of it, so without it that answer read as a refusal that will
1499/// never lift. Pinned and gated exactly as [`SECONDARY_WORDINGS`] is, and public for the same
1500/// one reason.
1501pub const PRIMARY_WORDINGS: [&str; 4] = [
1502 "api rate limit exceeded",
1503 "api rate limit already exceeded",
1504 "rate limit exceeded",
1505 "rate_limited",
1506];
1507
1508/// What a response *says about itself*, which is the only place a refusal can be read.
1509///
1510/// Deliberately not the whole response body. A board is a place people write about their
1511/// own work, and a task on it titled "the secondary rate limit" would, matched across the
1512/// raw text, turn a perfectly good answer into a refusal this source then waited out and
1513/// reported. So the item data is never read: what is read is GitHub's own REST-style
1514/// `message` envelope, which is what a forbidden status carries, and the `message` and
1515/// `type` of each GraphQL error, which is where a *successful* response says it.
1516///
1517/// A body that is not JSON at all has nothing structured to read, so only a failing
1518/// response's own text is taken — a successful response that is not JSON is malformed
1519/// rather than refused, and [`GitHubProjectsSource::answer`] says so.
1520fn refusal_wording(status: StatusCode, body: &str) -> String {
1521 let Ok(parsed) = serde_json::from_str::<Value>(body) else {
1522 return if status.is_success() {
1523 String::new()
1524 } else {
1525 body.to_owned()
1526 };
1527 };
1528 let mut said: Vec<&str> = parsed
1529 .get("message")
1530 .and_then(Value::as_str)
1531 .into_iter()
1532 .collect();
1533 if let Some(errors) = parsed.get("errors").and_then(Value::as_array) {
1534 for error in errors {
1535 said.extend(
1536 ["message", "type"]
1537 .into_iter()
1538 .filter_map(|key| error.get(key).and_then(Value::as_str)),
1539 );
1540 }
1541 }
1542 said.join("; ")
1543}
1544
1545impl Limiter {
1546 /// Which limiter refused this response, or `None` when none of them did.
1547 ///
1548 /// The wording is read first and the status only decides what carries none of it,
1549 /// because GitHub answers a secondary limit with a forbidden status far more often
1550 /// than with too-many-requests — while a forbidden status saying nothing about a limit
1551 /// really is a credential this token lacks.
1552 ///
1553 /// A response is a refusal because of its status or its own wording. A spent budget
1554 /// only ever explains one; it never turns an answer into a refusal.
1555 fn classify(status: StatusCode, budget_exhausted: bool, body: &str) -> Option<Self> {
1556 let normalized = refusal_wording(status, body).to_ascii_lowercase();
1557 if SECONDARY_WORDINGS
1558 .iter()
1559 .any(|wording| normalized.contains(wording))
1560 {
1561 return Some(Self::Secondary);
1562 }
1563 if status == StatusCode::TOO_MANY_REQUESTS {
1564 return Some(Self::Primary);
1565 }
1566 // An exhausted budget *explains* a response that failed; it does not make one that
1567 // succeeded into a failure. GitHub sets `x-ratelimit-remaining: 0` on the last
1568 // request the budget allowed as well as on the ones it then refuses, so reading
1569 // the header alone threw away a good answer — and, once refusals were retried,
1570 // replayed a request that had already taken effect.
1571 if !status.is_success() && budget_exhausted {
1572 return Some(Self::Primary);
1573 }
1574 // A successful response saying it: GitHub reports a GraphQL rate limit in the
1575 // `errors` of an HTTP 200, where nothing about the status says so at all.
1576 if status.is_success()
1577 && PRIMARY_WORDINGS
1578 .iter()
1579 .any(|wording| normalized.contains(wording))
1580 {
1581 return Some(Self::Primary);
1582 }
1583 None
1584 }
1585
1586 /// What this limiter is called where an operator can look it up.
1587 const fn name(self) -> &'static str {
1588 match self {
1589 Self::Primary => "GitHub's primary API rate limit",
1590 Self::Secondary => "GitHub's secondary rate limit",
1591 }
1592 }
1593
1594 /// What the endpoint an operator would go and check says about this limiter.
1595 const fn where_to_look(self) -> &'static str {
1596 match self {
1597 Self::Primary => {
1598 "That is the budget `gh api rate_limit` reports, so that endpoint says when it \
1599 comes back."
1600 }
1601 Self::Secondary => {
1602 "That limiter is not the primary API budget: `gh api rate_limit` reports the \
1603 primary budget and does not report this one, so budget showing there says \
1604 nothing about this refusal, and every further attempt extends it."
1605 }
1606 }
1607 }
1608
1609 /// The next step this limiter actually calls for.
1610 const fn what_to_do(self) -> &'static str {
1611 match self {
1612 Self::Primary => {
1613 "wait for the reset `gh api rate_limit` reports, then run the command again."
1614 }
1615 Self::Secondary => {
1616 "leave this board alone for a few minutes, then run the command again — or \
1617 raise pacing.min_mutation_interval_ms on this source so it writes more slowly."
1618 }
1619 }
1620 }
1621}
1622
1623/// One rate-limit refusal, and the wait GitHub asked for if it asked for one.
1624#[derive(Debug, Clone, Copy)]
1625struct Limited {
1626 limiter: Limiter,
1627 hint: Option<u64>,
1628}
1629
1630impl Limited {
1631 /// What the caller is told once this source has waited as long as it may.
1632 ///
1633 /// Both limiters report as [`SourceError::RateLimited`], because that is what
1634 /// happened: the kind a caller matches on says a rate limit refused this, and nothing
1635 /// about *which* limiter it was makes it a different kind of failure. What differs is
1636 /// the operator's next step, and that is what the message carries — a secondary
1637 /// refusal read as a primary one sends an operator to `gh api rate_limit`, where the
1638 /// budget looks fine, and then back to retry the very burst that was refused.
1639 fn exhausted(
1640 self,
1641 doing: &str,
1642 waits: u32,
1643 waited: Duration,
1644 needed: Duration,
1645 budget: Duration,
1646 ) -> SourceError {
1647 SourceError::RateLimited {
1648 retry_after_seconds: self.hint,
1649 message: Some(format!(
1650 "{} refused this source while {doing}; it waited {} out over {} and was refused \
1651 again, and the next wait of {} would take it past the {} one call may spend \
1652 waiting. {} next: {}",
1653 self.limiter.name(),
1654 plural(waits, "refusal"),
1655 seconds(waited),
1656 seconds(needed),
1657 seconds(budget),
1658 self.limiter.where_to_look(),
1659 self.limiter.what_to_do(),
1660 )),
1661 }
1662 }
1663}
1664
1665/// One HTTP attempt's result, with what its response said about the rate limit.
1666///
1667/// The two travel together so the record and the outcome are written from the same place:
1668/// what a response said about the budget is only readable while that response is in hand,
1669/// and what the attempt *meant* is only decidable once its body has been read.
1670struct Attempted {
1671 result: Result<Value, Attempt>,
1672 limits: accounting::RateLimit,
1673 /// GitHub's own reported cost for this call, for a document that asked for it.
1674 reported_cost: Option<u64>,
1675}
1676
1677/// One attempt's outcome: an error to report, or a rate limit to wait out.
1678enum Attempt {
1679 Failed(SourceError),
1680 Limited(Limited),
1681}
1682
1683fn plural(count: u32, thing: &str) -> String {
1684 if count == 1 {
1685 format!("{count} {thing}")
1686 } else {
1687 format!("{count} {thing}s")
1688 }
1689}
1690
1691fn seconds(duration: Duration) -> String {
1692 format!("{:.1}s", duration.as_secs_f64())
1693}
1694
1695/// A header GitHub spells as a whole number of seconds, or `None` when this one is not.
1696///
1697/// A value that is present and unreadable is deliberately *not* an error. `retry-after` is
1698/// allowed by HTTP to be a date rather than a count, an intermediary can rewrite either
1699/// header, and neither is what makes a response a refusal — so the whole cost of one this
1700/// cannot read is that the refusal carries no hint and the backing-off schedule answers it
1701/// instead. Refusing the response over the header would turn a readable refusal into an
1702/// unreadable one, and refusing to *wait* would be the one wrong direction to fail in.
1703fn whole_seconds(value: Option<&reqwest::header::HeaderValue>) -> Option<u64> {
1704 value
1705 .and_then(|value| value.to_str().ok())
1706 .and_then(|value| value.trim().parse::<u64>().ok())
1707}
1708
1709/// Every mutation this source sends creates content — an issue, a board item, a field of
1710/// one, a sub-issue link, a dependency, a comment — or edits or removes content of that
1711/// kind, and no query in [`graphql::DOCUMENTS`] does, so what the secondary limiter counts
1712/// and what the keyword says are the same set. That is what makes the keyword a sound test
1713/// rather than a convenient one: pacing an edit or a removal the limiter might not have
1714/// counted costs a wait, and not pacing one it did count costs the next fifty minutes.
1715fn is_mutation(query: &str) -> bool {
1716 query.trim_start().starts_with("mutation")
1717}
1718
1719/// What this source was doing, for a diagnostic that has to say so.
1720///
1721/// Read out of [`graphql::DOCUMENTS`], which is the inventory rather than a copy of it, so
1722/// a document added without a description is caught by that list's own gate instead of
1723/// falling through to the vague arm below.
1724fn operation_description(query: &str) -> &'static str {
1725 graphql::DOCUMENTS
1726 .iter()
1727 .find(|(document, _)| *document == query)
1728 .map_or("talking to GitHub", |(_, doing)| *doing)
1729}
1730
1731/// GitHub's published ceiling on content-generating requests, per minute.
1732///
1733/// Pinned in `tests/fixtures/rate-limits.json` and gated against it, because it is
1734/// GitHub's number rather than this source's: [`MIN_MUTATION_INTERVAL_MS`] is *derived*
1735/// from it, so a pacing value checked only against itself cannot go stale here.
1736pub const CONTENT_CREATION_PER_MINUTE: u64 = 80;
1737/// The same ceiling as GitHub publishes it per hour, which this source does **not** pace
1738/// at. See [`MIN_MUTATION_INTERVAL_MS`] for why the per-minute bound is the one that
1739/// governs; it is pinned beside its sibling so the gate would notice either one moving.
1740pub const CONTENT_CREATION_PER_HOUR: u64 = 500;
1741/// Shortest interval between two content-creating mutations, in milliseconds.
1742///
1743/// GitHub documents two secondary limits on content-generating requests:
1744/// [`CONTENT_CREATION_PER_MINUTE`] and [`CONTENT_CREATION_PER_HOUR`]. 60000/80 is 750, so
1745/// a mutation every 750 ms is the fastest rate that cannot exceed the per-minute bound,
1746/// and that is the bound a copy actually trips: a copy of one plan-sized project is a
1747/// burst of a few dozen mutations inside a few seconds. The hourly bound works out at one
1748/// every 7.2 seconds sustained, which no single copy reaches and which, used as the
1749/// spacing here, would turn an ordinary copy into an hour of waiting — so it is
1750/// deliberately *not* what this paces at. An installation that wants the hourly bound
1751/// honoured for a long sequence of copies says so through
1752/// `pacing.min_mutation_interval_ms`.
1753pub const MIN_MUTATION_INTERVAL_MS: u64 = 60_000 / CONTENT_CREATION_PER_MINUTE;
1754/// First wait when a rate-limit refusal carries no hint; each further wait doubles it.
1755///
1756/// A doubling schedule from one second reaches a minute in six waits, which is GitHub's
1757/// own advice for a secondary limit — wait, and wait longer each time — without spending
1758/// the first minute of a transient refusal doing nothing.
1759pub const RETRY_BACKOFF_MS: u64 = 1_000;
1760/// Total time one call may spend waiting out rate limits before it reports a failure.
1761///
1762/// Two minutes is long enough to ride out the refusals a paced copy still collects and
1763/// short enough that a command an operator is watching returns. The bound is what makes
1764/// the wait a wait rather than a hang: a call refused past it ends in a diagnostic naming
1765/// the limiter, not in a process nobody can tell from a wedged one.
1766pub const RETRY_BUDGET_MS: u64 = 120_000;
1767
1768fn default_token_env() -> String {
1769 "GH_PROJECTS_TOKEN".to_owned()
1770}
1771fn default_endpoint() -> String {
1772 "https://api.github.com/graphql".to_owned()
1773}
1774
1775/// The name of a `Status` single-select option on the board.
1776///
1777/// Validated on the way in rather than checked later, so a blank option name — which
1778/// nothing on a board can be — is a state this type cannot hold.
1779#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
1780#[serde(try_from = "String")]
1781#[schemars(extend("minLength" = 1))]
1782pub struct ColumnName(String);
1783
1784impl ColumnName {
1785 /// The option name, as the board spells it.
1786 fn as_str(&self) -> &str {
1787 &self.0
1788 }
1789}
1790
1791impl TryFrom<String> for ColumnName {
1792 type Error = String;
1793
1794 fn try_from(name: String) -> Result<Self, Self::Error> {
1795 if name.trim().is_empty() {
1796 return Err("a status_mapping option name cannot be blank".to_owned());
1797 }
1798 Ok(Self(name))
1799 }
1800}
1801
1802/// The two closed states this product can mean.
1803///
1804/// GitHub's `IssueClosedStateReason` also spells `DUPLICATE`, which is neither finished
1805/// work nor abandoned work, so nothing here ever writes it.
1806#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, schemars::JsonSchema)]
1807#[serde(rename_all = "kebab-case")]
1808pub enum ClosedState {
1809 /// `COMPLETED` — precisely done.
1810 Completed,
1811 /// `NOT_PLANNED` — precisely cancelled.
1812 NotPlanned,
1813}
1814
1815impl ClosedState {
1816 const fn reason(self) -> &'static str {
1817 match self {
1818 Self::Completed => "COMPLETED",
1819 Self::NotPlanned => "NOT_PLANNED",
1820 }
1821 }
1822}
1823
1824/// Configuration for one GitHub Projects v2 board.
1825///
1826/// It serializes to a document this same type reads back as itself, so a caller writing a
1827/// configuration — a consumer filing onto a board it configures — can build one here rather
1828/// than spell the shape again; an empty `metadata_fields` is left out, as one never set.
1829#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
1830#[serde(default, deny_unknown_fields)]
1831pub struct GitHubProjectsConfig {
1832 /// Login of the user or organization which owns the board.
1833 pub owner: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates GitHub's owner grammar before private construction.
1834 /// The project number shown in the board's GitHub URL.
1835 pub project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` bounds this to a positive GraphQL Int.
1836 // 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.
1837 /// `owner/name` of the repository this source creates an issue in when the item's own
1838 /// `repositories` field does not decide it.
1839 ///
1840 /// An item naming exactly one repository is created there; a task or a document naming
1841 /// none or several is created in its parent project's repository; and a project, or a
1842 /// task or document with no parent, naming none or several is created here. A board
1843 /// has no repository of its own and `createIssue` requires one, so a write without
1844 /// this is refused naming the field. Reads never need it.
1845 pub repository: Option<String>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the `owner/name` grammar before private construction.
1846 // llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
1847 /// Environment variable containing a fine-grained token with Projects and Issues
1848 /// read/write plus Pull requests read-only access for every repository represented on
1849 /// the board.
1850 #[serde(default = "default_token_env")]
1851 pub token_env: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the environment-variable grammar.
1852 /// GraphQL endpoint. GitHub Enterprise installations may override it.
1853 #[serde(default = "default_endpoint")]
1854 pub endpoint: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` converts it to the private validated `Url`.
1855 /// Per-instance mapping from a status category to the option of the board's one
1856 /// `Status` field it lands on, for a task and for a project.
1857 ///
1858 /// The shared `StatusMapping` grammar: each value is one option name for both kinds,
1859 /// `null` to disable the category for both, or `{task, project}` naming it per kind,
1860 /// where a kind left out leaves the category unmapped for that kind. A category this
1861 /// does not mention keeps its shipped default for both kinds: `backlog` to "Backlog",
1862 /// `todo` to "Todo", `queued` to "Queued", `in-progress` to "In Progress", `done` to
1863 /// "Done" plus closed as completed, `cancelled` to "Cancelled" plus closed as not
1864 /// planned, and `draft` and `unknown` unmapped. A category it does mention gets no
1865 /// shipped default for a kind it leaves out. `done` and `cancelled` close the issue for
1866 /// either kind. No two categories may name one option for the same kind, ignoring case.
1867 /// `unknown` may name one existing option; every unknown word then lands on it and
1868 /// reads back as `unknown` under its name. Unlike `local-md`, this source cannot keep
1869 /// each unknown word because it never creates board options.
1870 #[serde(default)]
1871 pub status_mapping: StatusMapping,
1872 /// Per-instance mapping from a task's priority to an option of this board's
1873 /// single-select field named `Priority`.
1874 ///
1875 /// Absent, this source holds no priority: every task reads as `none`, and a write of any
1876 /// other priority is refused before it reaches this board. Present, each of `urgent`,
1877 /// `high`, `medium` and `low` it does not mention keeps its shipped default — `Urgent`,
1878 /// `High`, `Medium` and `Low` — and an item with no value in the `Priority` field reads
1879 /// as `none`, so writing `none` clears the value. Option names match case-insensitively;
1880 /// no two levels may name one option. Reads and writes never create the field or an
1881 /// option: `onetaskgraph sources fields <source> --apply` does, and a write naming one
1882 /// the board lacks is refused pointing there.
1883 #[serde(default)]
1884 pub priority_mapping: Option<PriorityMappingConfig>,
1885 /// Caller metadata values this source also writes to board text fields, so the board can
1886 /// filter on them.
1887 ///
1888 /// Absent or empty, nothing is projected. Each entry names a board text `field`, a
1889 /// top-level metadata `key` and an optional `path` of object keys walked inside that key's
1890 /// value. On every item write — create, update and copy, of a task, a project or a
1891 /// document — a string found there is written to the field, and nothing is sent when the
1892 /// field already holds it; nothing there, or `null`, clears the field when it holds a
1893 /// value; any other JSON type is refused before any mutation. The metadata comment in the
1894 /// issue body stays the one home of the value, and every read reports metadata from it.
1895 /// Reads and writes never create the field: `onetaskgraph sources fields <source>
1896 /// --apply` does, as a text field, and a write to a board lacking it, or holding a
1897 /// non-text field of that name, is refused pointing there.
1898 #[serde(default, skip_serializing_if = "Vec::is_empty")]
1899 // Kept in the schema as `"default": []` although a serialized configuration leaves an empty
1900 // list out, so both SDKs model an absent list as an empty one rather than as `null`.
1901 #[schemars(!skip_serializing_if)]
1902 pub metadata_fields: Vec<MetadataFieldConfig>,
1903 /// How fast this source writes, and how long it waits out a rate-limit refusal.
1904 ///
1905 /// Every field keeps its shipped default when it is absent, and the defaults are
1906 /// GitHub's own published limits rather than taste. See [`Pacing`].
1907 #[serde(default)]
1908 pub pacing: PacingConfig,
1909}
1910
1911/// Which option of the board's `Priority` field each priority lands on.
1912///
1913/// One member per level rather than a map, so a key that is not a level is refused where
1914/// the configuration is read, naming the levels there are. `none` is not a member: it is no
1915/// value in the field, not an option of it.
1916#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
1917#[serde(default, deny_unknown_fields)]
1918pub struct PriorityMappingConfig {
1919 /// The option `urgent` lands on; `Urgent` when absent.
1920 pub urgent: Option<PriorityOptionName>,
1921 /// The option `high` lands on; `High` when absent.
1922 pub high: Option<PriorityOptionName>,
1923 /// The option `medium` lands on; `Medium` when absent.
1924 pub medium: Option<PriorityOptionName>,
1925 /// The option `low` lands on; `Low` when absent.
1926 pub low: Option<PriorityOptionName>,
1927}
1928
1929/// The name of an option of the board's `Priority` single-select field.
1930///
1931/// Validated on the way in, for the reason [`ColumnName`] is: nothing on a board can have a
1932/// blank name.
1933#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
1934#[serde(try_from = "String")]
1935#[schemars(extend("minLength" = 1))]
1936pub struct PriorityOptionName(String);
1937
1938impl PriorityOptionName {
1939 /// The option name, as the board spells it.
1940 fn as_str(&self) -> &str {
1941 &self.0
1942 }
1943}
1944
1945impl TryFrom<String> for PriorityOptionName {
1946 type Error = String;
1947
1948 fn try_from(name: String) -> Result<Self, Self::Error> {
1949 if name.trim().is_empty() {
1950 return Err("a priority_mapping option name cannot be blank".to_owned());
1951 }
1952 Ok(Self(name))
1953 }
1954}
1955
1956/// The name of the board field a priority is held in.
1957pub const PRIORITY_FIELD: &str = "Priority";
1958
1959/// The four priorities a board option can hold, in the order a new `Priority` field lists
1960/// them. `none` is not among them: it is the field holding no value.
1961///
1962/// This list mirrors `Priority`, so it carries its own drift gate, in the shape [`CATEGORIES`]
1963/// does: [`level_position`] is a wildcard-free match, so a priority added to the shared
1964/// vocabulary fails to compile until it is placed there, and this crate's suite reconciles
1965/// this list and [`PriorityMappingConfig`]'s members against that enum's own derived schema.
1966pub const PRIORITY_LEVELS: [Priority; 4] = [
1967 Priority::Urgent,
1968 Priority::High,
1969 Priority::Medium,
1970 Priority::Low,
1971];
1972
1973/// Where one priority sits in [`PRIORITY_LEVELS`], or `None` for `none`, which is no option;
1974/// see that list for what this pins.
1975#[must_use]
1976pub const fn level_position(priority: Priority) -> Option<usize> {
1977 match priority {
1978 Priority::None => None,
1979 Priority::Urgent => Some(0),
1980 Priority::High => Some(1),
1981 Priority::Medium => Some(2),
1982 Priority::Low => Some(3),
1983 }
1984}
1985
1986/// This instance's complete priority-to-option mapping, read in both directions.
1987///
1988/// One option per level, held in [`PRIORITY_LEVELS`] order, once it is established that no
1989/// two levels name one option.
1990#[derive(Debug, Clone)]
1991struct PriorityMapping {
1992 options: [PriorityOptionName; 4],
1993}
1994
1995impl PriorityMapping {
1996 fn resolve(config: PriorityMappingConfig, instance: &SourceName) -> Result<Self, SourceError> {
1997 let shipped = |name: &str| PriorityOptionName(name.to_owned());
1998 let mapping = Self {
1999 options: [
2000 config.urgent.unwrap_or_else(|| shipped("Urgent")),
2001 config.high.unwrap_or_else(|| shipped("High")),
2002 config.medium.unwrap_or_else(|| shipped("Medium")),
2003 config.low.unwrap_or_else(|| shipped("Low")),
2004 ],
2005 };
2006 for (index, option) in mapping.options.iter().enumerate() {
2007 if let Some(earlier) = mapping.options[..index]
2008 .iter()
2009 .position(|other| other.as_str().eq_ignore_ascii_case(option.as_str()))
2010 {
2011 return Err(SourceError::Config {
2012 message: format!(
2013 "priority_mapping of source {instance} sends both {} and {} to the board \
2014 option {:?}; one option cannot read back as two priorities",
2015 PRIORITY_LEVELS[earlier],
2016 PRIORITY_LEVELS[index],
2017 option.as_str()
2018 ),
2019 });
2020 }
2021 }
2022 Ok(mapping)
2023 }
2024
2025 /// The option `priority` lands on, or `None` for `none`, which is no option at all.
2026 fn option(&self, priority: Priority) -> Option<&str> {
2027 level_position(priority).map(|index| self.options[index].as_str())
2028 }
2029
2030 /// The priority a board option name reports, or `None` when nothing maps to it.
2031 fn priority_of(&self, option: &str) -> Option<Priority> {
2032 self.options
2033 .iter()
2034 .position(|name| name.as_str().eq_ignore_ascii_case(option))
2035 .map(|index| PRIORITY_LEVELS[index])
2036 }
2037
2038 /// Every mapped option name, in the order a new `Priority` field lists them.
2039 fn names(&self) -> impl Iterator<Item = &str> {
2040 self.options.iter().map(PriorityOptionName::as_str)
2041 }
2042}
2043
2044/// One metadata value this source projects onto a board text field.
2045#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2046#[serde(deny_unknown_fields)]
2047pub struct MetadataFieldConfig {
2048 /// The name of the board text field the value is written to.
2049 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.
2050 /// The top-level metadata key the value is read from. A key under the reserved
2051 /// `onetaskgraph.` prefix is refused.
2052 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.
2053 /// Object keys walked inside that key's value, outermost first. Absent or empty, the
2054 /// key's own value is the one projected. Left out of a serialized entry when empty.
2055 #[serde(default, skip_serializing_if = "Vec::is_empty")]
2056 // Kept in the schema as `"default": []`, for the reason `metadata_fields`' is.
2057 #[schemars(!skip_serializing_if)]
2058 pub path: Vec<String>,
2059}
2060
2061/// The board fields no metadata value may be projected onto, compared ignoring case: the
2062/// three this source writes itself, and those GitHub owns on every board.
2063// 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.
2064const UNPROJECTABLE_FIELDS: [&str; 13] = [
2065 STATUS_FIELD,
2066 PRIORITY_FIELD,
2067 ORIGIN_FIELD,
2068 "Title",
2069 "Assignees",
2070 "Labels",
2071 "Linked pull requests",
2072 "Milestone",
2073 "Repository",
2074 "Reviewers",
2075 "Parent issue",
2076 "Sub-issues progress",
2077 "Type",
2078];
2079
2080/// One validated [`MetadataFieldConfig`] entry.
2081#[derive(Debug, Clone)]
2082struct MetadataField {
2083 field: ProjectedField,
2084 key: ProjectedKey,
2085 path: Vec<String>,
2086}
2087
2088/// The name of a board text field a metadata value is projected onto: never blank, never one
2089/// this source or GitHub writes, and never one another entry of the same source names.
2090#[derive(Debug, Clone, PartialEq, Eq)]
2091struct ProjectedField(String);
2092
2093/// The top-level metadata key a projected value is read from: never blank, and never under
2094/// the namespace this product reserves.
2095#[derive(Debug, Clone, PartialEq, Eq)]
2096struct ProjectedKey(String);
2097
2098impl std::ops::Deref for ProjectedField {
2099 type Target = str;
2100 fn deref(&self) -> &str {
2101 &self.0
2102 }
2103}
2104
2105impl std::ops::Deref for ProjectedKey {
2106 type Target = str;
2107 fn deref(&self) -> &str {
2108 &self.0
2109 }
2110}
2111
2112/// What one item's metadata says one projected field should hold.
2113#[derive(Debug, Clone, PartialEq, Eq)]
2114enum Projected {
2115 /// A string to hold.
2116 Text(String),
2117 /// Nothing: the key or a step of the path is absent, or the value is `null`.
2118 Nothing,
2119}
2120
2121impl MetadataField {
2122 /// Validate every entry of one instance's `metadata_fields`, refusing the first that
2123 /// cannot stand, naming the source and the entry.
2124 fn resolve(
2125 configured: Vec<MetadataFieldConfig>,
2126 instance: &SourceName,
2127 ) -> Result<Vec<Self>, SourceError> {
2128 let mut resolved: Vec<Self> = Vec::with_capacity(configured.len());
2129 for (index, entry) in configured.into_iter().enumerate() {
2130 let refuse = |why: String| SourceError::Config {
2131 message: format!(
2132 "metadata_fields[{index}] of source {instance} (field {:?}, key {:?}) {why}",
2133 entry.field, entry.key
2134 ),
2135 };
2136 if entry.field.trim().is_empty() {
2137 return Err(refuse("names a blank field".to_owned()));
2138 }
2139 if entry.key.trim().is_empty() {
2140 return Err(refuse("names a blank key".to_owned()));
2141 }
2142 if entry.key.split('.').next() == Some(MetadataKey::RESERVED_NAMESPACE) {
2143 return Err(refuse(format!(
2144 "names a key under the reserved \"{}.\" prefix, which this product keeps \
2145 for its own keys; next: project a caller key instead",
2146 MetadataKey::RESERVED_NAMESPACE
2147 )));
2148 }
2149 if let Some(owned) = UNPROJECTABLE_FIELDS
2150 .iter()
2151 .find(|owned| owned.eq_ignore_ascii_case(&entry.field))
2152 {
2153 return Err(refuse(format!(
2154 "names the board field {owned:?}, which this source or GitHub already \
2155 writes; next: name a text field of your own"
2156 )));
2157 }
2158 if let Some(earlier) = resolved
2159 .iter()
2160 .position(|other| other.field.eq_ignore_ascii_case(&entry.field))
2161 {
2162 return Err(refuse(format!(
2163 "names the same board field as metadata_fields[{earlier}]; one field \
2164 cannot hold two values"
2165 )));
2166 }
2167 resolved.push(Self {
2168 field: ProjectedField(entry.field),
2169 key: ProjectedKey(entry.key),
2170 path: entry.path,
2171 });
2172 }
2173 Ok(resolved)
2174 }
2175
2176 /// The path as a refusal spells it: `[]` for the key's own value.
2177 fn spelled_path(&self) -> String {
2178 serde_json::to_string(&self.path).unwrap_or_else(|_| format!("{:?}", self.path))
2179 }
2180
2181 /// What `metadata` says this field should hold, or the refusal for a value no text field
2182 /// can hold.
2183 fn projected(
2184 &self,
2185 metadata: &BTreeMap<String, Value>,
2186 instance: &SourceName,
2187 ) -> Result<Projected, SourceError> {
2188 let mut value = metadata.get(&*self.key);
2189 for step in &self.path {
2190 value = value.and_then(|held| held.get(step.as_str()));
2191 }
2192 let found = match value {
2193 None | Some(Value::Null) => return Ok(Projected::Nothing),
2194 Some(Value::String(text)) => return Ok(Projected::Text(text.clone())),
2195 Some(Value::Bool(_)) => "a boolean",
2196 Some(Value::Number(_)) => "a number",
2197 Some(Value::Array(_)) => "an array",
2198 Some(Value::Object(_)) => "an object",
2199 };
2200 Err(SourceError::Refused {
2201 message: format!(
2202 "source {instance} projects metadata key {:?} at path {} onto its board text \
2203 field {:?}, and this item holds {found} there; only a string, null or nothing \
2204 can be written to a text field; next: store a string there, or remove the \
2205 value",
2206 &*self.key,
2207 self.spelled_path(),
2208 &*self.field
2209 ),
2210 })
2211 }
2212}
2213
2214/// What one item's `Priority` field says, read through this instance's mapping.
2215#[derive(Debug, Clone, PartialEq, Eq)]
2216enum HeldPriority {
2217 /// A priority this source reports: an option the mapping names, or no value (`none`).
2218 Read(Priority),
2219 /// An option the mapping does not name, which is never read as a level or as `none`.
2220 Unmapped(String),
2221}
2222
2223/// How fast this source writes, and how long it waits out a rate-limit refusal.
2224///
2225/// Configurable because a GitHub Enterprise installation sets its own limits and an
2226/// operator who has already been refused may want to go slower still — not because the
2227/// defaults are guesses.
2228#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2229#[serde(default, deny_unknown_fields)]
2230pub struct PacingConfig {
2231 /// Shortest interval between two content-creating mutations, in milliseconds.
2232 ///
2233 /// Zero sends them as fast as they are asked for, which is what a fixture server on
2234 /// loopback wants and what no board on github.com does. At most [`MAX_PACING_MS`].
2235 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.
2236 /// First wait when a rate-limit refusal carries no hint, in milliseconds. Each
2237 /// further wait of the same call doubles it. At most [`MAX_PACING_MS`], and never
2238 /// zero while there is a budget to spend, because a schedule of zero-length waits
2239 /// consumes none of it and so never ends.
2240 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.
2241 /// Total time one call may spend waiting out rate limits, in milliseconds.
2242 ///
2243 /// Zero reports the refusal rather than waiting at all. At most [`MAX_PACING_MS`]:
2244 /// the bound is what makes this a wait rather than a hang.
2245 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.
2246}
2247
2248/// The largest any pacing setting may be, in milliseconds.
2249///
2250/// One hour. GitHub's own harshest published bound on content-generating requests works
2251/// out at one every 7.2 seconds, so an hour is already three orders of magnitude past
2252/// anything a real limit asks for, and past it the settings stop describing pacing at all:
2253/// a wait budget beyond it is the unbounded wait this whole mechanism exists to replace,
2254/// and an interval beyond it is a command that never sends its second mutation. It also
2255/// keeps the clock arithmetic in [`GitHubProjectsSource::reserve_mutation_slot`] inside
2256/// what a `Duration` can hold on every platform.
2257pub const MAX_PACING_MS: u64 = 3_600_000;
2258
2259/// [`PacingConfig`] with every default resolved and every value checked, which is what the
2260/// source holds.
2261#[derive(Debug, Clone, Copy)]
2262struct Pacing {
2263 min_mutation_interval: Duration,
2264 retry_backoff: Duration,
2265 retry_budget: Duration,
2266}
2267
2268impl Pacing {
2269 /// Resolve one instance's pacing, refusing a configuration that would not pace at all.
2270 fn resolve(config: PacingConfig, instance: &SourceName) -> Result<Self, SourceError> {
2271 let bounded = |value: Option<u64>, default: u64, field: &str| match value {
2272 Some(value) if value > MAX_PACING_MS => Err(SourceError::Config {
2273 message: format!(
2274 "pacing.{field} of source {instance} is {value} ms, and the most any pacing \
2275 setting may be is {MAX_PACING_MS} ms — an hour, which is already far past \
2276 GitHub's own harshest published limit"
2277 ),
2278 }),
2279 Some(value) => Ok(Duration::from_millis(value)),
2280 None => Ok(Duration::from_millis(default)),
2281 };
2282 let retry_backoff = bounded(
2283 config.retry_backoff_ms,
2284 RETRY_BACKOFF_MS,
2285 "retry_backoff_ms",
2286 )?;
2287 let retry_budget = bounded(config.retry_budget_ms, RETRY_BUDGET_MS, "retry_budget_ms")?;
2288 if retry_backoff.is_zero() && !retry_budget.is_zero() {
2289 return Err(SourceError::Config {
2290 message: format!(
2291 "pacing.retry_backoff_ms of source {instance} is 0 while \
2292 pacing.retry_budget_ms is {} ms; a schedule of zero-length waits spends \
2293 none of that budget, so it would retry a refusal forever. Set a backoff of \
2294 at least 1 ms, or set retry_budget_ms to 0 to report a refusal without \
2295 waiting at all",
2296 retry_budget.as_millis()
2297 ),
2298 });
2299 }
2300 Ok(Self {
2301 min_mutation_interval: bounded(
2302 config.min_mutation_interval_ms,
2303 MIN_MUTATION_INTERVAL_MS,
2304 "min_mutation_interval_ms",
2305 )?,
2306 retry_backoff,
2307 retry_budget,
2308 })
2309 }
2310}
2311
2312/// Factory for [`GitHubProjectsSource`].
2313#[derive(Debug, Clone, Copy, Default)]
2314pub struct Plugin;
2315
2316impl SourcePlugin for Plugin {
2317 fn kind(&self) -> &'static str {
2318 KIND
2319 }
2320 fn config_schema(&self) -> Schema {
2321 schema_for!(GitHubProjectsConfig)
2322 }
2323 fn build(
2324 &self,
2325 name: &SourceName,
2326 config: &Value,
2327 secrets: &dyn SecretResolver,
2328 ) -> Result<Box<dyn TaskSource>, SourceError> {
2329 self.build_recording_into(name, config, secrets, Arc::new(Accounting::new()))
2330 }
2331
2332 fn build_with_clock(
2333 &self,
2334 name: &SourceName,
2335 config: &Value,
2336 secrets: &dyn SecretResolver,
2337 clock: SharedClock,
2338 ) -> Result<Box<dyn TaskSource>, SourceError> {
2339 self.build_recording_with_clock(name, config, secrets, Arc::new(Accounting::new()), clock)
2340 }
2341}
2342
2343impl Plugin {
2344 /// Build a source recording every request it sends into an accounting the caller holds.
2345 ///
2346 /// [`SourcePlugin::build`] is this with an accounting of its own, which is what the
2347 /// registry gets. This is for a caller that is also calling GitHub itself and wants one
2348 /// session total rather than two — see [`accounting`] and
2349 /// [`GitHubProjectsSource::recording_into`].
2350 ///
2351 /// # Errors
2352 ///
2353 /// Exactly [`SourcePlugin::build`]'s, with the same source name in front of each:
2354 /// [`SourceError::Config`] for configuration this plugin cannot use and
2355 /// [`SourceError::Auth`] for a credential it cannot find.
2356 pub fn build_recording_into(
2357 &self,
2358 name: &SourceName,
2359 config: &Value,
2360 secrets: &dyn SecretResolver,
2361 ledger: Arc<Accounting>,
2362 ) -> Result<Box<dyn TaskSource>, SourceError> {
2363 self.build_recording_with_clock(name, config, secrets, ledger, system_clock())
2364 }
2365
2366 fn build_recording_with_clock(
2367 &self,
2368 name: &SourceName,
2369 config: &Value,
2370 secrets: &dyn SecretResolver,
2371 ledger: Arc<Accounting>,
2372 clock: SharedClock,
2373 ) -> Result<Box<dyn TaskSource>, SourceError> {
2374 let config: GitHubProjectsConfig =
2375 serde_json::from_value(config.clone()).map_err(|e| SourceError::Config {
2376 message: format!("source {name}: {e}"),
2377 })?;
2378 let prefix = format!("source {name}: ");
2379 let mut source = GitHubProjectsSource::recording_into(name, config, secrets, ledger)
2380 .map_err(|error| match error {
2381 // The shared `StatusMapping::distinct` names the source itself.
2382 SourceError::Config { message } if message.starts_with(&prefix) => {
2383 SourceError::Config { message }
2384 }
2385 SourceError::Config { message } => SourceError::Config {
2386 message: format!("{prefix}{message}"),
2387 },
2388 SourceError::Auth { message } => SourceError::Auth {
2389 message: format!("source {name}: {message}"),
2390 },
2391 other => other,
2392 })?;
2393 source.clock = clock;
2394 Ok(Box::new(source))
2395 }
2396}
2397
2398/// Where a status category lands on this board, once configuration is resolved.
2399#[derive(Debug, Clone, PartialEq, Eq)]
2400enum StatusTarget {
2401 /// Not usable against this instance for this kind, and why.
2402 Disabled(UnmappedStatus),
2403 /// The board's `Status` option of this name.
2404 Column(ColumnName),
2405 /// A closed issue, with both its board option and the reason that says which closed it means.
2406 // 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.
2407 Terminal(ColumnName, ClosedState),
2408}
2409
2410/// Every status category, in the order the vocabulary declares them.
2411///
2412/// This list mirrors `StatusCategory`, so it carries its own drift gate rather than a
2413/// reviewer's attention: [`category_position`] is a wildcard-free match, so a variant
2414/// added to the shared vocabulary fails to compile until it is named there, and this
2415/// crate's suite reconciles this list against that enum's own derived schema, which is
2416/// generated from the variants rather than written beside them. The schema is what
2417/// catches a list left one short — a list checking only the positions it already holds
2418/// would pass while every mapping indexed by the new position panicked.
2419pub const CATEGORIES: [StatusCategory; 8] = [
2420 StatusCategory::Draft,
2421 StatusCategory::Backlog,
2422 StatusCategory::Todo,
2423 StatusCategory::Queued,
2424 StatusCategory::InProgress,
2425 StatusCategory::Done,
2426 StatusCategory::Cancelled,
2427 StatusCategory::Unknown,
2428];
2429
2430/// Where one category sits in [`CATEGORIES`]; see that list for what this pins.
2431#[must_use]
2432pub const fn category_position(category: StatusCategory) -> usize {
2433 match category {
2434 StatusCategory::Draft => 0,
2435 StatusCategory::Backlog => 1,
2436 StatusCategory::Todo => 2,
2437 StatusCategory::Queued => 3,
2438 StatusCategory::InProgress => 4,
2439 StatusCategory::Done => 5,
2440 StatusCategory::Cancelled => 6,
2441 StatusCategory::Unknown => 7,
2442 }
2443}
2444
2445/// The spelling a status category is configured and reported under.
2446fn category_name(category: StatusCategory) -> &'static str {
2447 match category {
2448 StatusCategory::Draft => "draft",
2449 StatusCategory::Backlog => "backlog",
2450 StatusCategory::Todo => "todo",
2451 StatusCategory::Queued => "queued",
2452 StatusCategory::InProgress => "in-progress",
2453 StatusCategory::Done => "done",
2454 StatusCategory::Cancelled => "cancelled",
2455 StatusCategory::Unknown => "unknown",
2456 }
2457}
2458
2459/// A shipped default's option name.
2460///
2461/// The literals below are this file's own and non-blank, and they are validated by the
2462/// one constructor a configured name goes through rather than beside it.
2463fn shipped_column(name: &'static str) -> ColumnName {
2464 ColumnName::try_from(name.to_owned()).expect("a shipped default names a board option")
2465}
2466
2467/// The shipped default for one category this instance's `status_mapping` does not mention,
2468/// for either kind.
2469fn shipped_default(category: StatusCategory) -> StatusTarget {
2470 match category {
2471 StatusCategory::Backlog => StatusTarget::Column(shipped_column("Backlog")),
2472 StatusCategory::Todo => StatusTarget::Column(shipped_column("Todo")),
2473 StatusCategory::Queued => StatusTarget::Column(shipped_column("Queued")),
2474 StatusCategory::InProgress => StatusTarget::Column(shipped_column("In Progress")),
2475 StatusCategory::Done => {
2476 StatusTarget::Terminal(shipped_column("Done"), ClosedState::Completed)
2477 }
2478 StatusCategory::Cancelled => {
2479 StatusTarget::Terminal(shipped_column("Cancelled"), ClosedState::NotPlanned)
2480 }
2481 StatusCategory::Draft | StatusCategory::Unknown => {
2482 StatusTarget::Disabled(UnmappedStatus::Unconfigured)
2483 }
2484 }
2485}
2486
2487/// The two kinds a status is written and read for, each with its own half of the mapping.
2488const STATUS_KINDS: [ItemKind; 2] = [ItemKind::Task, ItemKind::Project];
2489
2490/// This instance's complete category-to-target mapping for each kind, read in both
2491/// directions.
2492///
2493/// One target per category per kind, held at that category's own [`category_position`], so
2494/// a category missing from the mapping, named twice in it, or filed out of order is a state
2495/// this type cannot hold rather than one [`Self::target`] has to defend against. Both kinds'
2496/// targets are options of the board's one `Status` field.
2497#[derive(Debug, Clone)]
2498struct BoardStatuses {
2499 tasks: [StatusTarget; CATEGORIES.len()],
2500 projects: [StatusTarget; CATEGORIES.len()],
2501}
2502
2503impl BoardStatuses {
2504 /// Resolve `configured` against the shipped defaults, refusing two categories one kind
2505 /// would read back from one option.
2506 ///
2507 /// A category the mapping does not mention keeps its shipped default for both kinds; one
2508 /// it does mention is exactly what it configures, so a per-kind object leaves the kind it
2509 /// omits unmapped rather than defaulted.
2510 fn resolve(configured: &StatusMapping, instance: &SourceName) -> Result<Self, SourceError> {
2511 let resolve_kind =
2512 |kind: ItemKind| -> Result<[StatusTarget; CATEGORIES.len()], SourceError> {
2513 // `CATEGORIES[position] == category` for every category — the crate's suite
2514 // asserts it — so mapping the list in order fills each category's own slot.
2515 let mut targets = CATEGORIES.map(shipped_default);
2516 for (slot, category) in targets.iter_mut().zip(CATEGORIES) {
2517 if !configured.mentions(category) {
2518 continue;
2519 }
2520 *slot = match configured.name_for(category, kind) {
2521 Err(why) => StatusTarget::Disabled(why),
2522 Ok(name) => {
2523 let option = ColumnName::try_from(name.as_str().to_owned())
2524 .map_err(|message| SourceError::Config { message })?;
2525 match category {
2526 StatusCategory::Done => {
2527 StatusTarget::Terminal(option, ClosedState::Completed)
2528 }
2529 StatusCategory::Cancelled => {
2530 StatusTarget::Terminal(option, ClosedState::NotPlanned)
2531 }
2532 _ => StatusTarget::Column(option),
2533 }
2534 }
2535 };
2536 }
2537 StatusMapping::distinct(
2538 instance,
2539 kind,
2540 CATEGORIES
2541 .iter()
2542 .zip(&targets)
2543 .filter_map(|(category, target)| target.option().map(|o| (*category, o))),
2544 )?;
2545 Ok(targets)
2546 };
2547 Ok(Self {
2548 tasks: resolve_kind(ItemKind::Task)?,
2549 projects: resolve_kind(ItemKind::Project)?,
2550 })
2551 }
2552
2553 /// Every category's target for `kind`, in category order.
2554 const fn targets(&self, kind: ItemKind) -> &[StatusTarget; CATEGORIES.len()] {
2555 match kind {
2556 ItemKind::Task => &self.tasks,
2557 ItemKind::Project => &self.projects,
2558 }
2559 }
2560
2561 fn target(&self, kind: ItemKind, category: StatusCategory) -> &StatusTarget {
2562 &self.targets(kind)[category_position(category)]
2563 }
2564
2565 /// The category a board option name reports for `kind`, or `None` when nothing of that
2566 /// kind maps to it.
2567 fn category_of(&self, kind: ItemKind, option: &str) -> Option<StatusCategory> {
2568 CATEGORIES.into_iter().find(|category| {
2569 self.target(kind, *category)
2570 .option()
2571 .is_some_and(|name| name.eq_ignore_ascii_case(option))
2572 })
2573 }
2574
2575 /// Every option name either kind maps a category to, each once ignoring case, in
2576 /// category order with a task's name before a project's — what the guarded setup asks
2577 /// the `Status` field to hold.
2578 fn wanted(&self) -> Vec<String> {
2579 let mut wanted: Vec<String> = Vec::new();
2580 for category in CATEGORIES {
2581 for kind in STATUS_KINDS {
2582 if let Some(name) = self.target(kind, category).option()
2583 && !wanted.iter().any(|held| held.eq_ignore_ascii_case(name))
2584 {
2585 wanted.push(name.to_owned());
2586 }
2587 }
2588 }
2589 wanted
2590 }
2591
2592 /// The status an item of `kind` reports, from the three things a read of it says: its
2593 /// board `Status` option, whether its issue is closed, and the reason it was closed with.
2594 ///
2595 /// The closed state decides the category and the `Status` option decides the name, so
2596 /// a closed issue sitting in a "Shipped" column reports `done` named `Shipped`, whatever
2597 /// its kind. A closed issue whose reason is `DUPLICATE` or `REOPENED` reports `Unknown`:
2598 /// a duplicate is not finished work, and calling it done is a lie the next copy would
2599 /// write back. `REOPENED`-while-closed is a state this source can never produce, so
2600 /// it is read permissively rather than refused — reads are faithful, and refusals
2601 /// belong on writes. An open item's option reads through its own kind's mapping, and an
2602 /// option that mapping does not name reads as `Unknown` under its own name.
2603 ///
2604 /// One function of those three rather than of a response, so a narrow status write can
2605 /// answer what a re-read would report by applying it to the state it has just written.
2606 fn status(
2607 &self,
2608 kind: ItemKind,
2609 option: Option<&str>,
2610 closed: bool,
2611 reason: Option<&str>,
2612 ) -> Status {
2613 if closed {
2614 let category = match reason {
2615 None | Some("COMPLETED") => StatusCategory::Done,
2616 Some("NOT_PLANNED") => StatusCategory::Cancelled,
2617 Some(_) => StatusCategory::Unknown,
2618 };
2619 let fallback = match category {
2620 StatusCategory::Done => "Done",
2621 StatusCategory::Cancelled => "Cancelled",
2622 _ => "Closed",
2623 };
2624 return Status {
2625 category,
2626 name: option.unwrap_or(fallback).to_owned(),
2627 };
2628 }
2629 let name = option.unwrap_or("Open").to_owned();
2630 Status {
2631 category: self
2632 .category_of(kind, &name)
2633 .unwrap_or(StatusCategory::Unknown),
2634 name,
2635 }
2636 }
2637}
2638
2639impl BoardStatuses {
2640 /// For each kind, the option names it maps a category to that `existing` lacks, ignoring
2641 /// case; a kind lacking none is left out.
2642 fn missing_by_kind(&self, existing: &[StatusOption]) -> Vec<KindMissing> {
2643 STATUS_KINDS
2644 .into_iter()
2645 .filter_map(|kind| {
2646 let missing: Vec<String> = self
2647 .targets(kind)
2648 .iter()
2649 .filter_map(StatusTarget::option)
2650 .filter(|wanted| {
2651 !existing
2652 .iter()
2653 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
2654 })
2655 .map(str::to_owned)
2656 .collect();
2657 (!missing.is_empty()).then_some(KindMissing { kind, missing })
2658 })
2659 .collect()
2660 }
2661}
2662
2663impl StatusTarget {
2664 /// The board option this target selects, or `None` for an unmapped one.
2665 fn option(&self) -> Option<&str> {
2666 match self {
2667 Self::Column(name) | Self::Terminal(name, _) => Some(name.as_str()),
2668 Self::Disabled(_) => None,
2669 }
2670 }
2671}
2672
2673// 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.
2674/// One repository this source can create an issue in, as `owner/name`.
2675///
2676/// Every `createIssue` this source sends names one of these: the item's own single
2677/// `repositories` entry, else its parent project issue's repository, else the configured
2678/// [`GitHubProjectsConfig::repository`]. [`GitHubProjectsSource::creation_target`] makes
2679/// that choice and says what it refuses before `createIssue`.
2680// llmlint: ignore-end[comments_earn_their_place, contracts_have_one_source_or_a_drift_gate]
2681#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
2682struct RepositoryTarget {
2683 owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
2684 name: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
2685}
2686
2687impl RepositoryTarget {
2688 fn parse(value: &str) -> Result<Self, SourceError> {
2689 let (owner, name) = value.split_once('/').ok_or_else(|| SourceError::Config {
2690 message: format!(
2691 "repository must be spelled owner/name; {value:?} names no repository"
2692 ),
2693 })?;
2694 if !valid_github_owner(owner) || !valid_github_repository_name(name) {
2695 return Err(SourceError::Config {
2696 message: format!(
2697 "repository must be spelled owner/name with a GitHub login and one \
2698 repository name; {value:?} is not"
2699 ),
2700 });
2701 }
2702 Ok(Self {
2703 owner: owner.to_owned(),
2704 name: name.to_owned(),
2705 })
2706 }
2707
2708 /// The one host whose repositories this source creates issues in, spelled once: it is
2709 /// what [`Self::origin`] renders and what [`Self::from_origin`] accepts.
2710 const HOST: &str = "github.com";
2711
2712 fn origin(&self) -> String {
2713 format!("{}/{}/{}", Self::HOST, self.owner, self.name)
2714 }
2715
2716 /// The repository a normalized origin names, or why it is none this source can create
2717 /// an issue in: another host, or more or fewer than `owner/name` under this one.
2718 fn from_origin(origin: &Repository) -> Result<Self, String> {
2719 let not_here = || {
2720 format!(
2721 "{} is not a {}/owner/name repository",
2722 origin.as_str(),
2723 Self::HOST
2724 )
2725 };
2726 let (host, rest) = origin.as_str().split_once('/').ok_or_else(not_here)?;
2727 if host != Self::HOST {
2728 return Err(not_here());
2729 }
2730 Self::parse(rest).map_err(|_| not_here())
2731 }
2732
2733 fn slug(&self) -> String {
2734 format!("{}/{}", self.owner, self.name)
2735 }
2736}
2737
2738/// A source which reads GitHub afresh for every operation.
2739pub struct GitHubProjectsSource {
2740 /// This source's configured name, used both to tell a far end naming this source
2741 /// from one naming a system it knows nothing about, and to name the instance a
2742 /// status refusal is about.
2743 name: SourceName,
2744 owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after full GitHub-owner validation.
2745 project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after GraphQL-Int validation.
2746 repository: Option<RepositoryTarget>,
2747 endpoint: Url,
2748 token: SecretString,
2749 credential_name: String, // llmlint: ignore[invalid_states_unrepresentable] Private diagnostic value constructed only after environment-name validation.
2750 statuses: BoardStatuses,
2751 /// Where each priority lands on this board, or `None` when this instance holds none.
2752 priorities: Option<PriorityMapping>,
2753 /// The metadata values this instance projects onto board text fields, in configured order.
2754 metadata_fields: Vec<MetadataField>,
2755 client: Client,
2756 asset_client: Client,
2757 /// Every item this source has created in this command, in the order it created them —
2758 /// dropped by [`TaskSource::end_command`].
2759 ///
2760 /// GitHub's `projectV2.items` is eventually consistent: an issue added to a board with
2761 /// `addProjectV2ItemById` is routinely absent from the very next read of that board, so
2762 /// a copy resolving a dependency on an item it had just created refused it as not
2763 /// found. A board read is completed from this — an item remembered here and absent from
2764 /// the read is added back, because the board really does hold it and only the read is
2765 /// behind.
2766 ///
2767 /// It is not a cache of a user's work: nothing is remembered that this process did not
2768 /// itself just write, it lives and dies with the process, and it is never consulted for
2769 /// an item this source did not create.
2770 created: Mutex<Vec<Resolved>>,
2771 /// Every item that already existed and that this source has written in this command, as
2772 /// it wrote it — dropped by [`TaskSource::end_command`].
2773 ///
2774 /// The other half of [`Self::created`], held on the same terms and for the reason a
2775 /// narrowed read needs it: an answer from GitHub's search or from the board's own field
2776 /// filter is an index behind a write this process made moments ago, so a query matching
2777 /// what this process just wrote onto an existing item would otherwise miss it. Nothing
2778 /// is remembered that this process did not itself just write.
2779 updated: Mutex<Vec<Resolved>>,
2780 /// Every issue this source has added a comment to or edited a comment of in this command
2781 /// — dropped by [`TaskSource::end_command`].
2782 ///
2783 /// A comment-activity read is narrowed by GitHub's issue search, whose `updated:` index
2784 /// lags the write that moved an issue's `updatedAt`, and neither [`Self::created`] nor
2785 /// [`Self::updated`] is moved by a comment, so an issue this process had just commented
2786 /// on was missing from such a read — or ruled out by the `updatedAt` its own record held
2787 /// from before — until the index caught up. Each id here is a candidate of every such
2788 /// search-narrowed read, and wherever it is a candidate its comments are read rather than
2789 /// it being ruled out by a stale `updatedAt`; that read is of the issue's own node, so it
2790 /// is current. It holds ids alone: nothing of a comment is remembered. A comment another
2791 /// process wrote is still found only once the index has it.
2792 commented: Mutex<Vec<NativeId>>,
2793 /// How fast this source writes, and how long it waits out a refusal.
2794 pacing: Pacing,
2795 /// When the last content-creating mutation finished, or the moment the furthest-out
2796 /// reserved slot releases the next one, whichever is later — so the one after it can be
2797 /// spaced from that. See [`MIN_MUTATION_INTERVAL_MS`] for the interval and
2798 /// [`GitHubProjectsSource::finish_mutation`] for why completion rather than release is
2799 /// what it is measured from.
2800 last_mutation: Mutex<Option<Duration>>,
2801 clock: SharedClock,
2802 numeric_repositories: tokio::sync::Mutex<BTreeMap<RepositoryTarget, std::num::NonZeroU64>>,
2803 /// The board as this process last read it, for the length of one command — dropped by
2804 /// [`TaskSource::end_command`].
2805 ///
2806 /// A copy of a project used to re-read the whole board, paged, before writing each of
2807 /// its items, which is by far the largest part of a copy's request count and none of
2808 /// its work. Nothing else changes this board while a command runs — this source's own
2809 /// writes are the only writer — so one read answers them all.
2810 ///
2811 /// It is not a store of a user's work and it is not the cache the no-persistence
2812 /// invariant forbids: it lives and dies with the process exactly as `created` does,
2813 /// nothing is written down, and [`Self::board`] still completes it from `created`, so
2814 /// an item this command created and then depends on resolves whether or not GitHub's
2815 /// own eventually-consistent read has caught up. A write to an item already on the
2816 /// board updates the entry here too, so what this holds is the last read plus this
2817 /// process's own writes rather than a snapshot taken before them.
2818 board_cache: Mutex<Option<Board>>,
2819 /// Every issue this board's own search reported, for the length of one command — dropped
2820 /// by [`TaskSource::end_command`].
2821 ///
2822 /// The second half of a board read, and cached for the same reason and on the same
2823 /// terms as the first: it lives and dies with the process, nothing is written down, and
2824 /// a write this process makes updates the entry here exactly as it updates the one in
2825 /// [`Self::board_cache`]. One read answers every question a command asks, so a command
2826 /// that lists this board's projects and its tasks pays for one search rather than two.
2827 search_cache: Mutex<Option<Vec<Resolved>>>,
2828 /// What each narrowed question GitHub was asked answered, keyed by that question, for
2829 /// the length of one command — dropped by [`TaskSource::end_command`].
2830 ///
2831 /// The narrowed counterpart of [`Self::search_cache`], held on the same terms: it lives
2832 /// and dies with the process, nothing is written down, a write this process makes
2833 /// updates the entry here as it updates the other two, and every answer is completed
2834 /// with this process's own writes each time it is given. A command that asks the same
2835 /// narrowed question twice — a wait polling for its own items, a listing repeated after a
2836 /// write — pays for it once, which is what the whole-board read it replaced gave it.
2837 narrowed_cache: Mutex<BTreeMap<String, Vec<Resolved>>>,
2838 search_next: Mutex<BTreeMap<String, Option<String>>>,
2839 /// Records already resolved in this command, reused by writes and for comment identity.
2840 /// Explicit item reads still reach GitHub. Nothing is persisted, and
2841 /// [`TaskSource::end_command`] drops every record, so a write in the next command reads
2842 /// its item as a person has since left it.
2843 resolved_cache: Mutex<BTreeMap<NativeId, Resolved>>,
2844 /// Each project's sub-issues as GitHub answered them, keyed by the selector they were
2845 /// asked for under, with the project that selector named — for the length of one command,
2846 /// dropped by [`TaskSource::end_command`].
2847 ///
2848 /// A caller pages through a project's tasks one engine page at a time, and every page
2849 /// is cut from the whole list of them, so without this each page walked every
2850 /// [`graphql::SUB_ISSUES`] page again and a project of `n` listing pages cost `n²`
2851 /// requests to read once. Held on the terms [`Self::narrowed_cache`] is: it lives and
2852 /// dies with the process, nothing is written down, a write this process makes updates or
2853 /// removes the entry here as it does there, and every answer is completed with this
2854 /// process's own writes each time it is given.
2855 children_cache: Mutex<ProjectChildren>,
2856 /// The board's own id and field definitions as this process last read them on their
2857 /// own, for the length of one command — dropped by [`TaskSource::end_command`].
2858 ///
2859 /// What a write needs of the board and its item does not say, read once per command
2860 /// rather than once per item written, on the terms [`Self::board_cache`] is held on: it
2861 /// lives and dies with the process and nothing is written down. It holds no item and so
2862 /// can answer no question about one — see [`Self::board_fields`].
2863 fields_cache: Mutex<Option<BoardFields>>,
2864 /// Each destination repository's node id, resolved once per repository
2865 /// rather than per issue created.
2866 ///
2867 /// A repository's node id does not change, and re-reading it for every issue of a copy
2868 /// spent one request per item on an answer this source already had. It is a map rather
2869 /// than one entry because a copy files each item in the repository its own
2870 /// `repositories` field names, so a plan across five repositories asks GitHub five
2871 /// times and not once per item.
2872 repository_cache: Mutex<BTreeMap<RepositoryTarget, String>>,
2873 /// What every request this source sends is recorded into.
2874 ///
2875 /// Ordinary code path, not a mode: [`Self::send_once`] records into it at the one place
2876 /// a request leaves this crate, so nothing has to be switched on for a session to be
2877 /// counted. It is shared rather than owned so a caller accounting for a whole session —
2878 /// its own schema verification, board lookups, residue sweep and cleanup beside this
2879 /// source's reads and writes — adds up one accounting instead of two. See
2880 /// [`accounting`] for what a record carries and what a session's spend is and is not.
2881 ledger: Arc<Accounting>,
2882}
2883
2884/// GitHub's closed single-select color vocabulary.
2885#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2886#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
2887pub enum StatusOptionColor {
2888 /// Gray.
2889 Gray,
2890 /// Blue.
2891 Blue,
2892 /// Green.
2893 Green,
2894 /// Yellow.
2895 Yellow,
2896 /// Purple.
2897 Purple,
2898 /// Red.
2899 Red,
2900 /// Orange.
2901 Orange,
2902 /// Pink.
2903 Pink,
2904}
2905
2906/// Whether a guarded board setup — of the fields, or of the Status options alone — plans or
2907/// applies its additions.
2908#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2909pub enum SetupMode {
2910 /// Read without mutation.
2911 Plan,
2912 /// Apply and verify.
2913 Apply,
2914}
2915
2916/// The name [`SetupMode`] had when Status was the one field set up, kept so a caller written
2917/// against it goes on compiling.
2918pub type StatusOptionsMode = SetupMode;
2919
2920/// The explicit result of the requested operation.
2921#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2922#[serde(rename_all = "kebab-case")]
2923pub enum StatusOptionsOutcome {
2924 /// A read-only plan.
2925 Planned,
2926 /// Apply found nothing missing.
2927 Unchanged,
2928 /// Additions were applied and verified.
2929 Applied,
2930}
2931
2932/// A GitHub single-select option's opaque GraphQL node identifier.
2933#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2934#[serde(transparent)]
2935pub struct StatusOptionId(#[schemars(length(min = 1))] String);
2936
2937impl TryFrom<String> for StatusOptionId {
2938 type Error = String;
2939
2940 fn try_from(id: String) -> Result<Self, Self::Error> {
2941 if id.trim().is_empty() {
2942 return Err("a GitHub Status option id cannot be blank".to_owned());
2943 }
2944 Ok(Self(id))
2945 }
2946}
2947
2948/// One existing or proposed option in a guarded Status-field update.
2949#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2950pub struct StatusOption {
2951 /// GitHub's stable id.
2952 pub id: StatusOptionId,
2953 /// The visible option name.
2954 pub name: ColumnName,
2955 /// GitHub's single-select color token.
2956 pub color: StatusOptionColor,
2957 /// The option description, including an empty one.
2958 pub description: String,
2959}
2960
2961/// One board item's Status assignment, retained as recovery data.
2962#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2963pub struct StatusAssignment {
2964 /// The project item id whose assignment this is.
2965 // llmlint: ignore[invalid_states_unrepresentable] This opaque GraphQL node ID is
2966 // carried verbatim as operator recovery data; introducing a semantic type would claim
2967 // validation rules GitHub does not publish and no operation here interprets.
2968 pub item_id: String,
2969 /// The selected option, absent when the item has no status.
2970 #[serde(skip_serializing_if = "Option::is_none")]
2971 pub option: Option<AssignedStatusOption>,
2972}
2973
2974/// The inseparable id and name of an assigned option.
2975#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2976pub struct AssignedStatusOption {
2977 /// GitHub's stable id.
2978 pub id: StatusOptionId,
2979 /// The visible name.
2980 pub name: ColumnName,
2981}
2982
2983/// The plan and verified outcome of reconciling configured Status options.
2984#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2985pub struct StatusOptionsReport {
2986 /// The configured source name.
2987 pub source: SourceName,
2988 /// Configured option names absent before the operation.
2989 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a
2990 // `ColumnName` and has therefore already passed its nonblank validation; retaining the
2991 // serialized string here preserves the report's intentionally simple public contract.
2992 pub missing: Vec<String>,
2993 /// What the requested operation did.
2994 pub outcome: StatusOptionsOutcome,
2995 /// The complete option list observed before any mutation.
2996 pub existing: Vec<StatusOption>,
2997}
2998
2999#[derive(Debug, Clone, PartialEq, Eq)]
3000struct StatusSnapshot {
3001 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
3002 // passed back as the mutation's project identity; a newtype could enforce no stronger
3003 // invariant because GitHub publishes no grammar for it.
3004 board_id: String,
3005 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
3006 // passed back as the mutation's field identity; a newtype could enforce no stronger
3007 // invariant because GitHub publishes no grammar for it.
3008 field_id: String,
3009 options: Vec<StatusOption>,
3010 assignments: Vec<StatusAssignment>,
3011}
3012
3013/// The name of the board field a status is held in.
3014const STATUS_FIELD: &str = "Status";
3015
3016/// Every item's value of each field `report` names, as it stood before the setup wrote
3017/// anything — what a person puts back when the setup is refused part way.
3018fn recovery(report: &FieldsReport, before: &BoardSnapshot) -> Result<String, SourceError> {
3019 let assignments: BTreeMap<&str, Vec<StatusAssignment>> = report
3020 .fields
3021 .iter()
3022 .map(|field| (field.field.name(), before.assignments(field.field)))
3023 .collect();
3024 serde_json::to_string_pretty(&assignments).map_err(|error| SourceError::Malformed {
3025 message: format!("cannot render the pre-write field recovery snapshot: {error}"),
3026 })
3027}
3028
3029/// One board field the guarded setup reads and writes — every one it reads, and the only
3030/// ones it writes.
3031#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, schemars::JsonSchema)]
3032pub enum BoardField {
3033 /// The single-select `Status` field every instance's `status_mapping` resolves into.
3034 Status,
3035 /// The single-select `Priority` field an instance's `priority_mapping` resolves into.
3036 Priority,
3037}
3038
3039impl BoardField {
3040 /// The field's name on the board.
3041 #[must_use]
3042 pub const fn name(self) -> &'static str {
3043 match self {
3044 Self::Status => STATUS_FIELD,
3045 Self::Priority => PRIORITY_FIELD,
3046 }
3047 }
3048
3049 /// The field a board calls `name`, or `None` for one this setup does not own.
3050 fn named(name: &str) -> Option<Self> {
3051 [Self::Status, Self::Priority]
3052 .into_iter()
3053 .find(|field| field.name() == name)
3054 }
3055}
3056
3057/// What the guarded setup did to one field.
3058#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3059#[serde(rename_all = "kebab-case")]
3060pub enum FieldOutcome {
3061 /// A read-only plan.
3062 Planned,
3063 /// Apply found the field there with every configured option.
3064 Unchanged,
3065 /// Missing options were added to the field that was there, and verified.
3066 Applied,
3067 /// The field was not there; it was created holding the configured options, and verified.
3068 Created,
3069}
3070
3071/// One field's plan, or its verified outcome.
3072#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3073pub struct FieldReport {
3074 /// Which field.
3075 pub field: BoardField,
3076 /// Whether the board had the field before the operation.
3077 // llmlint: ignore[invalid_states_unrepresentable] `exists` beside `outcome` is the report's
3078 // wire shape as its consumer's contract fixes it — `{"field", "exists", "missing",
3079 // "outcome", "existing"}` — so folding one into the other would change a published JSON
3080 // shape. The contradictory pairings cannot be built: `GitHubProjectsSource::fields` is the
3081 // one constructor, and it derives `outcome` from `exists` in one match.
3082 pub exists: bool,
3083 /// Configured option names the field lacked before the operation — every one of them,
3084 /// in the order a new field lists them, when the field was not there at all.
3085 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
3086 // mapping name and has therefore already passed its nonblank validation; the serialized
3087 // string is the report's intentionally simple public contract, as `StatusOptionsReport`'s is.
3088 pub missing: Vec<String>,
3089 /// For the `Status` field, which item kind each missing name is configured for: one
3090 /// entry per kind `status_mapping` names a missing option for, task before project, each
3091 /// listing that kind's missing names in category order. A name both kinds use is in
3092 /// both. Empty — and left out of the JSON — when nothing is missing, and always for
3093 /// `Priority`, which only a task holds.
3094 #[serde(default, skip_serializing_if = "Vec::is_empty")]
3095 // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
3096 // both SDKs model an absent `kinds` as an empty list rather than as `null`.
3097 #[schemars(!skip_serializing_if)]
3098 pub kinds: Vec<KindMissing>,
3099 /// What the requested operation did.
3100 pub outcome: FieldOutcome,
3101 /// The field's complete option list observed before any mutation; empty when the field
3102 /// was not there.
3103 pub existing: Vec<StatusOption>,
3104}
3105
3106/// The `Status` option names one item kind's `status_mapping` names that the field lacked.
3107#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3108pub struct KindMissing {
3109 /// The kind these names are configured for.
3110 pub kind: ItemKind,
3111 /// The names that kind maps a category to and the field lacked, in category order.
3112 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
3113 // mapping name, as `FieldReport::missing`'s do, and the serialized string is the report's
3114 // intentionally simple public contract.
3115 pub missing: Vec<String>,
3116}
3117
3118/// The plan and verified outcome of setting up every field a source's configuration names.
3119#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3120pub struct FieldsReport {
3121 /// The configured source name.
3122 pub source: SourceName,
3123 /// `Status`, always, and `Priority` when the source sets `priority_mapping`.
3124 // llmlint: ignore[invalid_states_unrepresentable] A list is the report's wire shape as its
3125 // consumer's contract fixes it — `{"source", "fields": [...]}` — so a struct with one member
3126 // per field would change a published JSON shape. The states the list could hold and the
3127 // contract forbids cannot be built: `GitHubProjectsSource::fields` is the one constructor,
3128 // and it pushes `Status` first and exactly once, then `Priority` exactly when configured.
3129 pub fields: Vec<FieldReport>,
3130 /// Each board text field the source's `metadata_fields` projects a value onto, in
3131 /// configured order. Empty — and left out of the JSON — when it projects none.
3132 #[serde(default, skip_serializing_if = "Vec::is_empty")]
3133 // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
3134 // both SDKs model an absent list as an empty one rather than as `null`.
3135 #[schemars(!skip_serializing_if)]
3136 pub metadata_fields: Vec<MetadataFieldReport>,
3137}
3138
3139/// The type of a board field that is not a text field, as GitHub names it — its `dataType`, or
3140/// its GraphQL type when it has none — which is what a projected field's conflict reports.
3141///
3142/// Never empty, never beginning with whitespace, and never `TEXT`: a text field of that name is
3143/// no conflict.
3144// The schema states that rule as `NON_TEXT_FIELD_TYPE_PATTERN`, so a generated SDK model
3145// refuses exactly what `try_from` below does.
3146#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3147#[serde(transparent)]
3148pub struct NonTextFieldType(#[schemars(regex(pattern = NON_TEXT_FIELD_TYPE_PATTERN))] String);
3149
3150/// What a [`NonTextFieldType`] may be, as the emitted schema states it: a first character that
3151/// is not whitespace, and anything but exactly `TEXT` — spelled without a lookahead, which
3152/// neither SDK's validator can be relied on to support.
3153const NON_TEXT_FIELD_TYPE_PATTERN: &str =
3154 r"^(?:[^T\s]|T(?:[^E]|$)|TE(?:[^X]|$)|TEX(?:[^T]|$)|TEXT[\s\S])";
3155
3156impl TryFrom<String> for NonTextFieldType {
3157 type Error = String;
3158
3159 fn try_from(kind: String) -> Result<Self, Self::Error> {
3160 if kind.is_empty() || kind.starts_with(char::is_whitespace) {
3161 return Err("a board field's type cannot be blank or begin with whitespace".to_owned());
3162 }
3163 if kind == "TEXT" {
3164 return Err("a TEXT field is no conflict for a projected text field".to_owned());
3165 }
3166 Ok(Self(kind))
3167 }
3168}
3169
3170impl std::fmt::Display for NonTextFieldType {
3171 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3172 formatter.write_str(&self.0)
3173 }
3174}
3175
3176/// One projected metadata text field's plan, or its verified outcome.
3177///
3178/// The setup creates a missing one as a text field and never alters a field that is there:
3179/// a same-named field of another type is reported in `conflict` and left as it is.
3180#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3181pub struct MetadataFieldReport {
3182 /// The board field's name.
3183 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.
3184 /// The metadata key the value is read from.
3185 pub key: String, // llmlint: ignore[invalid_states_unrepresentable] As `field`: a validated configuration value reported back as written.
3186 /// The object keys walked inside that key's value; empty for the key's own value.
3187 pub path: Vec<String>,
3188 /// Whether the board had a text field of that name before the operation.
3189 // 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.
3190 pub exists: bool,
3191 /// The type of a field of that name that is not a text field, which the setup leaves as
3192 /// it is; absent — and left out of the JSON — when there is none.
3193 #[serde(default, skip_serializing_if = "Option::is_none")]
3194 pub conflict: Option<NonTextFieldType>,
3195 /// What the requested operation did: `planned`; `created` for a field that was not
3196 /// there; `unchanged` for one that was, and for a conflict, which is never changed.
3197 pub outcome: FieldOutcome,
3198}
3199
3200/// Which options one field is configured with, in the order a new field would list them.
3201struct FieldPlan {
3202 field: BoardField,
3203 wanted: Vec<String>,
3204}
3205
3206/// One single-select field as the guarded setup snapshots it.
3207#[derive(Debug, Clone, PartialEq, Eq)]
3208struct SnapshotField {
3209 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
3210 // passed back as the mutation's field identity; a newtype could enforce no stronger
3211 // invariant because GitHub publishes no grammar for it.
3212 field_id: String,
3213 options: Vec<StatusOption>,
3214}
3215
3216/// Every single-select field of a board and every item's value of each.
3217#[derive(Debug, Clone, PartialEq, Eq)]
3218struct BoardSnapshot {
3219 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
3220 // passed back as the mutation's project identity; a newtype could enforce no stronger
3221 // invariant because GitHub publishes no grammar for it.
3222 board_id: String,
3223 fields: BTreeMap<BoardField, SnapshotField>,
3224 /// Each board item's id, and its value of each field this setup owns that it holds one of.
3225 items: Vec<(String, BTreeMap<BoardField, AssignedStatusOption>)>,
3226}
3227
3228impl BoardSnapshot {
3229 /// Every item's value of `field`, in board order — the recovery data a drift refusal
3230 /// carries.
3231 fn assignments(&self, field: BoardField) -> Vec<StatusAssignment> {
3232 self.items
3233 .iter()
3234 .map(|(item_id, values)| StatusAssignment {
3235 item_id: item_id.clone(),
3236 option: values.get(&field).cloned(),
3237 })
3238 .collect()
3239 }
3240}
3241
3242impl GitHubProjectsSource {
3243 /// Report missing configured Status options and, when `apply` is true, add them with
3244 /// a whole-list mutation that preserves every existing id and verifies the result.
3245 ///
3246 /// # Errors
3247 ///
3248 /// Refuses a board without a single-select `Status` field. A post-write difference in
3249 /// any pre-existing option id or item assignment is refused with the complete pre-write
3250 /// assignment snapshot in the diagnostic for recovery.
3251 // llmlint: ignore[changed_behavior_has_e2e] The CLI journeys cover plan, no-op apply,
3252 // successful mutation, both drift refusals, source selection, missing Status, casing,
3253 // and paging. Transport errors remain the shared `graphql` boundary's behavior rather
3254 // than a new status-options behavior, and the pinned-schema test prevents valid GitHub
3255 // responses from entering the defensive malformed-response branches below.
3256 pub async fn status_options(
3257 &self,
3258 mode: StatusOptionsMode,
3259 ) -> Result<StatusOptionsReport, SourceError> {
3260 let before = self.status_snapshot().await?;
3261 // A terminal category's option is as configured as an open one's: a terminal
3262 // write validates it before closing and refuses when the board lacks it. Both
3263 // kinds' names are options of the one field, so both are asked for.
3264 let missing = self
3265 .statuses
3266 .wanted()
3267 .into_iter()
3268 .filter(|wanted| {
3269 !before
3270 .options
3271 .iter()
3272 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3273 })
3274 .collect::<Vec<_>>();
3275 let report = StatusOptionsReport {
3276 source: self.name.clone(),
3277 missing: missing.clone(),
3278 outcome: match (mode, missing.is_empty()) {
3279 (StatusOptionsMode::Plan, _) => StatusOptionsOutcome::Planned,
3280 (StatusOptionsMode::Apply, true) => StatusOptionsOutcome::Unchanged,
3281 (StatusOptionsMode::Apply, false) => StatusOptionsOutcome::Applied,
3282 },
3283 existing: before.options.clone(),
3284 };
3285 if mode == StatusOptionsMode::Plan || missing.is_empty() {
3286 return Ok(report);
3287 }
3288 let mut options = before
3289 .options
3290 .iter()
3291 .map(|option| {
3292 json!({
3293 "id": option.id, "name": option.name, "color": option.color,
3294 "description": option.description,
3295 })
3296 })
3297 .collect::<Vec<_>>();
3298 options.extend(missing.iter().map(|name| {
3299 json!({
3300 "name": name, "color": "GRAY", "description": ""
3301 })
3302 }));
3303 self.graphql(
3304 graphql::STATUS_OPTIONS_UPDATE,
3305 json!({"input": {
3306 "projectId": before.board_id, "fieldId": before.field_id,
3307 "singleSelectOptions": options,
3308 }}),
3309 )
3310 .await?;
3311 let after = self.status_snapshot().await?;
3312 let options_preserved = before
3313 .options
3314 .iter()
3315 .all(|old| after.options.iter().any(|new| new == old));
3316 let additions_present = missing.iter().all(|wanted| {
3317 after
3318 .options
3319 .iter()
3320 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3321 });
3322 if !options_preserved || !additions_present || after.assignments != before.assignments {
3323 let recovery = serde_json::to_string_pretty(&before.assignments).map_err(|error| {
3324 SourceError::Malformed {
3325 message: format!("cannot render pre-write Status recovery snapshot: {error}"),
3326 }
3327 })?;
3328 return Err(SourceError::Refused {
3329 message: format!(
3330 "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}"
3331 ),
3332 });
3333 }
3334 Ok(report)
3335 }
3336
3337 /// A fresh snapshot of the Status field and every board item's assignment of it.
3338 ///
3339 /// # Errors
3340 ///
3341 /// Refuses a board without a single-select `Status` field, and one the token cannot see.
3342 async fn status_snapshot(&self) -> Result<StatusSnapshot, SourceError> {
3343 // Status alone, as this operation has always read it: a `Priority` field is another
3344 // operation's, so nothing about it can refuse this one.
3345 let mut board = self.board_snapshot(&[BoardField::Status]).await?;
3346 let field = board
3347 .fields
3348 .remove(&BoardField::Status)
3349 .ok_or_else(|| self.no_status_field())?;
3350 Ok(StatusSnapshot {
3351 assignments: board.assignments(BoardField::Status),
3352 board_id: board.board_id,
3353 field_id: field.field_id,
3354 options: field.options,
3355 })
3356 }
3357
3358 /// The refusal a board with no `Status` field is answered with by the guarded setup.
3359 fn no_status_field(&self) -> SourceError {
3360 SourceError::Refused {
3361 message: format!("source {} board has no Status field", self.name),
3362 }
3363 }
3364
3365 // llmlint: ignore-block[changed_behavior_has_e2e] Valid snapshot shapes are exercised through
3366 // the real CLI loopback journey, including pagination. The individual malformed guards
3367 // are defensive validation of a schema-pinned third-party response, not separate user
3368 // journeys; drift and missing-field failures cover the operation's recovery behavior.
3369 /// A fresh snapshot of each of the `owned` fields on the board, with its options, and of
3370 /// every board item's value of each, walked to the end of the board's items. A field not
3371 /// in `owned` is read past whatever it holds.
3372 async fn board_snapshot(&self, owned: &[BoardField]) -> Result<BoardSnapshot, SourceError> {
3373 let mut after: Option<String> = None;
3374 let mut snapshot: Option<BoardSnapshot> = None;
3375 loop {
3376 let data = self
3377 .graphql(
3378 graphql::STATUS_OPTIONS_SNAPSHOT,
3379 json!({
3380 "owner": self.owner, "number": self.project_number,
3381 "first": MAX_PAGE_SIZE, "after": after, "nestedFirst": MAX_PAGE_SIZE,
3382 }),
3383 )
3384 .await?;
3385 let board = data
3386 .pointer("/owner/projectV2")
3387 .filter(|board| board.is_object())
3388 .ok_or_else(|| SourceError::Refused {
3389 message: format!(
3390 "source {} has no accessible GitHub Projects board",
3391 self.name
3392 ),
3393 })?;
3394 if board
3395 .pointer("/fields/pageInfo/hasNextPage")
3396 .and_then(Value::as_bool)
3397 != Some(false)
3398 {
3399 return Err(SourceError::Malformed {
3400 message:
3401 "GitHub project fields is incomplete or has malformed pageInfo.hasNextPage"
3402 .into(),
3403 });
3404 }
3405 let mut fields = BTreeMap::new();
3406 // Only the fields this setup owns, by name: a node the single-select fragment did not
3407 // match carries no name, and a person's own single-select field — a `Size`, a
3408 // `Team` — is none of this setup's business, so nothing about it can refuse one. A
3409 // `Status` or `Priority` field without its options is malformed, not absent.
3410 // 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.
3411 for (owned, field) in board
3412 .pointer("/fields/nodes")
3413 .and_then(Value::as_array)
3414 .ok_or_else(|| SourceError::Malformed {
3415 message: "GitHub project fields.nodes is not an array".into(),
3416 })?
3417 .iter()
3418 .filter_map(|field| {
3419 let named = BoardField::named(field.get("name")?.as_str()?)?;
3420 owned.contains(&named).then_some((named, field))
3421 })
3422 {
3423 let options = field
3424 .get("options")
3425 .and_then(Value::as_array)
3426 .ok_or_else(|| SourceError::Malformed {
3427 message: "GitHub single-select field options is not an array".into(),
3428 })?
3429 .iter()
3430 .map(|option| {
3431 Ok(StatusOption {
3432 id: StatusOptionId::try_from(required_str(option, "id")?.to_owned())
3433 .map_err(|message| SourceError::Malformed { message })?,
3434 name: ColumnName::try_from(required_str(option, "name")?.to_owned())
3435 .map_err(|message| SourceError::Malformed {
3436 message: format!(
3437 "GitHub single-select option name is invalid: {message}"
3438 ),
3439 })?,
3440 color: serde_json::from_value(
3441 option.get("color").cloned().unwrap_or(Value::Null),
3442 )
3443 .map_err(|error| {
3444 SourceError::Malformed {
3445 message: format!(
3446 "GitHub single-select option color is invalid: {error}"
3447 ),
3448 }
3449 })?,
3450 description: optional_str(option, "description")?
3451 .unwrap_or_default()
3452 .to_owned(),
3453 })
3454 })
3455 .collect::<Result<Vec<_>, SourceError>>()?;
3456 let snapshot = SnapshotField {
3457 field_id: required_nonblank_str(field, "id")?.to_owned(),
3458 options,
3459 };
3460 // A board's field names are unique, so a second one is an answer that cannot
3461 // say which field the setup would act on — refused rather than one chosen.
3462 if fields.insert(owned, snapshot).is_some() {
3463 return Err(SourceError::Malformed {
3464 message: format!(
3465 "GitHub answered two {} fields for this board",
3466 owned.name()
3467 ),
3468 });
3469 }
3470 }
3471 let board_id = required_nonblank_str(board, "id")?.to_owned();
3472 let current = snapshot.get_or_insert_with(|| BoardSnapshot {
3473 board_id,
3474 fields,
3475 items: Vec::new(),
3476 });
3477 let items = board
3478 .pointer("/items/nodes")
3479 .and_then(Value::as_array)
3480 .ok_or_else(|| SourceError::Malformed {
3481 message: "GitHub project items.nodes is not an array".into(),
3482 })?;
3483 for item in items {
3484 let field_values =
3485 item.get("fieldValues")
3486 .ok_or_else(|| SourceError::Malformed {
3487 message: "GitHub project item is missing fieldValues".into(),
3488 })?;
3489 if field_values
3490 .pointer("/pageInfo/hasNextPage")
3491 .and_then(Value::as_bool)
3492 != Some(false)
3493 {
3494 return Err(SourceError::Malformed {
3495 message: "GitHub project item fieldValues is incomplete or has malformed pageInfo.hasNextPage".into(),
3496 });
3497 }
3498 let values = item
3499 .pointer("/fieldValues/nodes")
3500 .and_then(Value::as_array)
3501 .ok_or_else(|| SourceError::Malformed {
3502 message: "GitHub project item fieldValues.nodes is not an array".into(),
3503 })?;
3504 let item_id = required_nonblank_str(item, "id")?;
3505 let mut assigned = BTreeMap::new();
3506 for value in values {
3507 let Some(field) = value
3508 .pointer("/field/name")
3509 .and_then(Value::as_str)
3510 .and_then(BoardField::named)
3511 .filter(|field| owned.contains(field))
3512 else {
3513 continue;
3514 };
3515 let held = assigned.insert(
3516 field,
3517 AssignedStatusOption {
3518 id: StatusOptionId::try_from(
3519 required_str(value, "optionId")?.to_owned(),
3520 )
3521 .map_err(|message| SourceError::Malformed { message })?,
3522 name: ColumnName::try_from(required_str(value, "name")?.to_owned())
3523 .map_err(|message| SourceError::Malformed {
3524 message: format!(
3525 "GitHub assigned {} name is invalid: {message}",
3526 field.name()
3527 ),
3528 })?,
3529 },
3530 );
3531 // An item holds one value of a field, so a second one leaves no way to
3532 // tell which it holds — and a verification or recovery built on either
3533 // could restore the wrong one.
3534 if held.is_some() {
3535 return Err(SourceError::Malformed {
3536 message: format!(
3537 "GitHub answered two {} values for board item {item_id}",
3538 field.name()
3539 ),
3540 });
3541 }
3542 }
3543 current.items.push((item_id.to_owned(), assigned));
3544 }
3545 let page = board.get("items").ok_or_else(|| SourceError::Malformed {
3546 message: "GitHub project is missing items".into(),
3547 })?;
3548 let has_next = page
3549 .pointer("/pageInfo/hasNextPage")
3550 .and_then(Value::as_bool)
3551 .ok_or_else(|| SourceError::Malformed {
3552 message: "GitHub project items.pageInfo.hasNextPage is not a boolean".into(),
3553 })?;
3554 if !has_next {
3555 break;
3556 }
3557 let next =
3558 required_nonblank_str(page.get("pageInfo").unwrap_or(&Value::Null), "endCursor")?;
3559 validate_cursor_progress(after.as_deref(), next)?;
3560 after = Some(next.to_owned());
3561 }
3562 snapshot.ok_or_else(|| SourceError::Malformed {
3563 message: "GitHub returned no board field snapshot".into(),
3564 })
3565 }
3566 // llmlint: ignore-end[changed_behavior_has_e2e]
3567
3568 /// Report every board field this source's configuration names and, with
3569 /// [`SetupMode::Apply`], set each up: add the options a field lacks, and create
3570 /// the `Priority` field when the board has none.
3571 ///
3572 /// The fields are `Status`, always, with the options `status_mapping` resolves to; and
3573 /// `Priority`, when `priority_mapping` is set, with its four mapped options — created in
3574 /// the order urgent, high, medium, low. An option a field already has keeps its id, name,
3575 /// color and description: the whole option list goes back with every existing id, because
3576 /// a re-minted id clears every item's value.
3577 ///
3578 /// # Errors
3579 ///
3580 /// Refuses a board without a single-select `Status` field. After an apply the board is
3581 /// read again, and a pre-existing option or any item's value of either field that moved is
3582 /// refused with the complete pre-write assignments in the diagnostic, for recovery.
3583 // llmlint: ignore[changed_behavior_has_e2e] The `sources fields` journeys drive plan,
3584 // unchanged apply, a created field, an added option to each field, drift refusal, a board
3585 // with no Status field and a non-github-projects source through the compiled CLI against
3586 // the loopback board. Transport errors are the shared `graphql` boundary's behavior.
3587 pub async fn fields(&self, mode: SetupMode) -> Result<FieldsReport, SourceError> {
3588 let owned: Vec<BoardField> = if self.priorities.is_some() {
3589 vec![BoardField::Status, BoardField::Priority]
3590 } else {
3591 vec![BoardField::Status]
3592 };
3593 let before = self.board_snapshot(&owned).await?;
3594 let mut plans = vec![FieldPlan {
3595 field: BoardField::Status,
3596 wanted: self.statuses.wanted(),
3597 }];
3598 if !before.fields.contains_key(&BoardField::Status) {
3599 return Err(self.no_status_field());
3600 }
3601 if let Some(mapping) = &self.priorities {
3602 plans.push(FieldPlan {
3603 field: BoardField::Priority,
3604 wanted: mapping.names().map(str::to_owned).collect(),
3605 });
3606 }
3607 // The snapshot reads single-select fields alone, so a field it did not find may still
3608 // be on the board under the name, of another type: creating one beside it would fail
3609 // part way, or leave two fields of one name. Asked of the board's own field list, and
3610 // only when a field is missing.
3611 if plans
3612 .iter()
3613 .any(|plan| !before.fields.contains_key(&plan.field))
3614 {
3615 let board = self.board_fields().await?;
3616 for plan in plans
3617 .iter()
3618 .filter(|plan| !before.fields.contains_key(&plan.field))
3619 {
3620 if let Some(field) = Board::field(&board.fields, plan.field.name())? {
3621 return Err(SourceError::Refused {
3622 message: format!(
3623 "source {}'s board has a {} field that is not a single-select field \
3624 (it is a {}), so it cannot hold this source's options; next: rename \
3625 or remove that field, then run this again",
3626 self.name,
3627 plan.field.name(),
3628 optional_str(field, "__typename")?.unwrap_or("field of another type")
3629 ),
3630 });
3631 }
3632 }
3633 }
3634 let mut reports = Vec::new();
3635 for plan in &plans {
3636 let held = before.fields.get(&plan.field);
3637 let existing = held.map(|field| field.options.clone()).unwrap_or_default();
3638 let mut missing: Vec<String> = Vec::new();
3639 for wanted in &plan.wanted {
3640 let present = existing
3641 .iter()
3642 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3643 || missing
3644 .iter()
3645 .any(|named| named.eq_ignore_ascii_case(wanted));
3646 if !present {
3647 missing.push(wanted.clone());
3648 }
3649 }
3650 let kinds = match plan.field {
3651 BoardField::Status => self.statuses.missing_by_kind(&existing),
3652 BoardField::Priority => Vec::new(),
3653 };
3654 reports.push(FieldReport {
3655 field: plan.field,
3656 exists: held.is_some(),
3657 kinds,
3658 outcome: match (mode, held.is_some(), missing.is_empty()) {
3659 (SetupMode::Plan, _, _) => FieldOutcome::Planned,
3660 (SetupMode::Apply, true, true) => FieldOutcome::Unchanged,
3661 (SetupMode::Apply, true, false) => FieldOutcome::Applied,
3662 (SetupMode::Apply, false, _) => FieldOutcome::Created,
3663 },
3664 missing,
3665 existing,
3666 });
3667 }
3668 // A projected field is a text field, which the single-select snapshot cannot see, so
3669 // it is asked of the board's own field list — and only when one is configured.
3670 let mut metadata_fields = Vec::new();
3671 if !self.metadata_fields.is_empty() {
3672 let board = self.board_fields().await?;
3673 for projection in &self.metadata_fields {
3674 let conflict = match Board::field(&board.fields, &projection.field)? {
3675 None => None,
3676 Some(field) => {
3677 let typename = optional_str(field, "__typename")?;
3678 let data_type = optional_str(field, "dataType")?;
3679 if typename == Some("ProjectV2Field") && data_type == Some("TEXT") {
3680 None
3681 } else {
3682 let named = data_type
3683 .filter(|kind| *kind != "TEXT")
3684 .or(typename)
3685 .unwrap_or("field of another type");
3686 Some(NonTextFieldType::try_from(named.to_owned()).map_err(
3687 |message| SourceError::Malformed {
3688 message: format!(
3689 "GitHub answered the board's {} field with a type this \
3690 setup cannot read: {message}",
3691 &*projection.field
3692 ),
3693 },
3694 )?)
3695 }
3696 }
3697 };
3698 let exists =
3699 conflict.is_none() && Board::field(&board.fields, &projection.field)?.is_some();
3700 metadata_fields.push(MetadataFieldReport {
3701 field: projection.field.to_string(),
3702 key: projection.key.to_string(),
3703 path: projection.path.clone(),
3704 exists,
3705 outcome: match (mode, exists || conflict.is_some()) {
3706 (SetupMode::Plan, _) => FieldOutcome::Planned,
3707 (SetupMode::Apply, true) => FieldOutcome::Unchanged,
3708 (SetupMode::Apply, false) => FieldOutcome::Created,
3709 },
3710 conflict,
3711 });
3712 }
3713 }
3714 let report = FieldsReport {
3715 source: self.name.clone(),
3716 fields: reports,
3717 metadata_fields,
3718 };
3719 let writes: Vec<&FieldReport> = report
3720 .fields
3721 .iter()
3722 .filter(|field| !field.missing.is_empty() || !field.exists)
3723 .collect();
3724 let creates: Vec<&MetadataFieldReport> = report
3725 .metadata_fields
3726 .iter()
3727 .filter(|field| field.outcome == FieldOutcome::Created)
3728 .collect();
3729 if mode == SetupMode::Plan || (writes.is_empty() && creates.is_empty()) {
3730 return Ok(report);
3731 }
3732 let mut landed: Vec<&str> = Vec::new();
3733 for field in &writes {
3734 let added = field
3735 .missing
3736 .iter()
3737 .map(|name| json!({"name": name, "color": "GRAY", "description": ""}));
3738 let sent = match before.fields.get(&field.field) {
3739 Some(held) => {
3740 let mut options = held
3741 .options
3742 .iter()
3743 .map(|option| {
3744 json!({
3745 "id": option.id, "name": option.name, "color": option.color,
3746 "description": option.description,
3747 })
3748 })
3749 .collect::<Vec<_>>();
3750 options.extend(added);
3751 self.graphql(
3752 graphql::STATUS_OPTIONS_UPDATE,
3753 json!({"input": {
3754 "projectId": before.board_id, "fieldId": held.field_id,
3755 "singleSelectOptions": options,
3756 }}),
3757 )
3758 .await
3759 }
3760 None => {
3761 self.graphql(
3762 graphql::CREATE_FIELD,
3763 json!({"input": {
3764 "projectId": before.board_id, "dataType": "SINGLE_SELECT",
3765 "name": field.field.name(),
3766 "singleSelectOptions": added.collect::<Vec<_>>(),
3767 }}),
3768 )
3769 .await
3770 }
3771 };
3772 // A mutation that failed does not establish that GitHub left its field as it was,
3773 // so every failure from here on carries the recovery data a drift refusal does.
3774 match sent {
3775 Ok(_) => landed.push(field.field.name()),
3776 Err(error) => {
3777 let changed = if landed.is_empty() {
3778 String::new()
3779 } else {
3780 format!("changed the {} field and then ", landed.join(" and "))
3781 };
3782 return Err(SourceError::Refused {
3783 message: format!(
3784 "the guarded field setup {changed}failed on the {} field, which it may \
3785 have changed part way: {error}; the pre-write item assignments \
3786 are:\n{}",
3787 field.field.name(),
3788 recovery(&report, &before)?
3789 ),
3790 });
3791 }
3792 }
3793 }
3794 for field in &creates {
3795 let sent = self
3796 .graphql(
3797 graphql::CREATE_FIELD,
3798 json!({"input": {
3799 "projectId": before.board_id, "dataType": "TEXT", "name": field.field,
3800 }}),
3801 )
3802 .await;
3803 if let Err(error) = sent {
3804 let changed = if landed.is_empty() {
3805 String::new()
3806 } else {
3807 format!("changed the {} field and then ", landed.join(" and "))
3808 };
3809 return Err(SourceError::Refused {
3810 message: format!(
3811 "the guarded field setup {changed}failed creating the {} text field: \
3812 {error}; run it again to create what is still missing",
3813 field.field
3814 ),
3815 });
3816 }
3817 landed.push(&field.field);
3818 }
3819 // The board has been written, so a verification read that fails leaves it unverified
3820 // rather than unchanged, and says what to put back.
3821 let after = match self.board_snapshot(&owned).await {
3822 Ok(after) => after,
3823 Err(error) => {
3824 return Err(SourceError::Refused {
3825 message: format!(
3826 "the guarded field setup changed the {} field and then could not read the \
3827 board back to verify it: {error}; the pre-write item assignments are:\n{}",
3828 landed.join(" and "),
3829 recovery(&report, &before)?
3830 ),
3831 });
3832 }
3833 };
3834 let mut moved = Vec::new();
3835 for field in &report.fields {
3836 let name = field.field.name();
3837 let now = after
3838 .fields
3839 .get(&field.field)
3840 .map(|held| held.options.as_slice())
3841 .unwrap_or_default();
3842 if !field.existing.iter().all(|old| now.contains(old)) {
3843 moved.push(format!(
3844 "a pre-existing {name} option id, name, color or description"
3845 ));
3846 }
3847 if !field.missing.iter().all(|wanted| {
3848 now.iter()
3849 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3850 }) {
3851 moved.push(format!("an added {name} option"));
3852 }
3853 if after.assignments(field.field) != before.assignments(field.field) {
3854 moved.push(format!("an item's {name} value"));
3855 }
3856 }
3857 if !creates.is_empty() {
3858 // Read afresh: the view held for this command is the one from before the creates.
3859 *self.fields_cache()? = None;
3860 let board = match self.board_fields().await {
3861 Ok(board) => board,
3862 Err(error) => {
3863 return Err(SourceError::Refused {
3864 message: format!(
3865 "the guarded field setup changed the {} field and then could not \
3866 read the board back to verify it: {error}; run it again to verify \
3867 it, which creates nothing a second time",
3868 landed.join(" and ")
3869 ),
3870 });
3871 }
3872 };
3873 for field in &creates {
3874 let text = Board::field(&board.fields, &field.field)?.is_some_and(|held| {
3875 held.get("__typename").and_then(Value::as_str) == Some("ProjectV2Field")
3876 && held.get("dataType").and_then(Value::as_str) == Some("TEXT")
3877 });
3878 if !text {
3879 moved.push(format!("the created {} text field", field.field));
3880 }
3881 }
3882 }
3883 if !moved.is_empty() {
3884 return Err(SourceError::Refused {
3885 message: format!(
3886 "GitHub changed {} after the guarded field setup; the pre-write item \
3887 assignments are:\n{}",
3888 moved.join(", "),
3889 recovery(&report, &before)?
3890 ),
3891 });
3892 }
3893 Ok(report)
3894 }
3895
3896 /// Validate configuration and capture the named credential without exposing it.
3897 ///
3898 /// # Errors
3899 ///
3900 /// Returns [`SourceError::Config`] for a configuration this instance cannot use and
3901 /// [`SourceError::Auth`] when the named credential is missing or empty.
3902 pub fn new(
3903 name: &SourceName,
3904 config: GitHubProjectsConfig,
3905 secrets: &dyn SecretResolver,
3906 ) -> Result<Self, SourceError> {
3907 Self::recording_into(name, config, secrets, Arc::new(Accounting::new()))
3908 }
3909
3910 /// The same, recording every request it sends into an accounting the caller holds too.
3911 ///
3912 /// [`Self::new`] is this with an accounting of its own. A caller that is also making
3913 /// its own calls to GitHub — a lane verifying a schema, sweeping residue or cleaning
3914 /// up — passes the one it records those into, so the session total accounts for the
3915 /// whole session rather than for this source's share of it.
3916 ///
3917 /// # Errors
3918 ///
3919 /// Exactly [`Self::new`]'s: [`SourceError::Config`] for a configuration this instance
3920 /// cannot use and [`SourceError::Auth`] when the named credential is missing or empty.
3921 pub fn recording_into(
3922 name: &SourceName,
3923 config: GitHubProjectsConfig,
3924 secrets: &dyn SecretResolver,
3925 ledger: Arc<Accounting>,
3926 ) -> Result<Self, SourceError> {
3927 if !valid_github_owner(&config.owner) {
3928 return Err(SourceError::Config {
3929 message: "owner must be 1-39 ASCII letters, digits, or single hyphens, and cannot start or end with a hyphen".into(),
3930 });
3931 }
3932 if config.project_number == 0 || config.project_number > i32::MAX as u32 {
3933 return Err(SourceError::Config {
3934 message: format!("project_number must be between 1 and {}", i32::MAX),
3935 });
3936 }
3937 if !valid_environment_name(&config.token_env) {
3938 return Err(SourceError::Config {
3939 message: "token_env must be a valid environment-variable name".into(),
3940 });
3941 }
3942 let repository = config
3943 .repository
3944 .as_deref()
3945 .map(RepositoryTarget::parse)
3946 .transpose()?;
3947 let endpoint = Url::parse(&config.endpoint).map_err(|e| SourceError::Config {
3948 message: format!("endpoint is not a valid URL: {e}"),
3949 })?;
3950 if endpoint.scheme() != "https"
3951 && !(endpoint.scheme() == "http"
3952 && endpoint
3953 .host_str()
3954 .is_some_and(|h| h == "127.0.0.1" || h == "localhost" || h == "::1"))
3955 {
3956 return Err(SourceError::Config {
3957 message:
3958 "endpoint must use HTTPS (HTTP is accepted only for a loopback test server)"
3959 .into(),
3960 });
3961 }
3962 let token = secrets.get(&config.token_env).filter(|token| !token.expose_secret().trim().is_empty()).ok_or_else(|| SourceError::Auth {
3963 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),
3964 })?;
3965 Ok(Self {
3966 name: name.clone(),
3967 owner: config.owner,
3968 project_number: config.project_number,
3969 repository,
3970 asset_client: assets::client(&endpoint)?,
3971 endpoint,
3972 token,
3973 credential_name: config.token_env,
3974 statuses: BoardStatuses::resolve(&config.status_mapping, name)?,
3975 priorities: config
3976 .priority_mapping
3977 .map(|mapping| PriorityMapping::resolve(mapping, name))
3978 .transpose()?,
3979 metadata_fields: MetadataField::resolve(config.metadata_fields, name)?,
3980 client: Client::builder()
3981 .user_agent("onetaskgraph")
3982 .build()
3983 .map_err(|e| SourceError::Config {
3984 message: format!("cannot build HTTP client: {e}"),
3985 })?,
3986 created: Mutex::new(Vec::new()),
3987 updated: Mutex::new(Vec::new()),
3988 commented: Mutex::new(Vec::new()),
3989 pacing: Pacing::resolve(config.pacing, name)?,
3990 last_mutation: Mutex::new(None),
3991 clock: system_clock(),
3992 numeric_repositories: tokio::sync::Mutex::new(BTreeMap::new()),
3993 board_cache: Mutex::new(None),
3994 search_cache: Mutex::new(None),
3995 narrowed_cache: Mutex::new(BTreeMap::new()),
3996 resolved_cache: Mutex::new(BTreeMap::new()),
3997 children_cache: Mutex::new(BTreeMap::new()),
3998 search_next: Mutex::new(BTreeMap::new()),
3999 fields_cache: Mutex::new(None),
4000 repository_cache: Mutex::new(BTreeMap::new()),
4001 ledger,
4002 })
4003 }
4004
4005 /// A snapshot of every request this source has sent, and what each cost.
4006 ///
4007 /// A value to hold and compare rather than a borrow of the accounting itself, so two
4008 /// of them can sit side by side. When this source was built with
4009 /// [`Self::recording_into`] the snapshot is the whole shared session, which is the
4010 /// point of building it that way.
4011 #[must_use]
4012 pub fn accounting(&self) -> accounting::Session {
4013 self.ledger.snapshot()
4014 }
4015
4016 /// Send one GraphQL document, pacing this source's own mutations and waiting out a
4017 /// rate limit rather than handing it straight back as an error.
4018 ///
4019 /// Retrying is safe for every document here, including the mutations, and the reason
4020 /// is that only a *refusal* is retried: [`Limiter::classify`] rules on a response
4021 /// GitHub sent, and a request GitHub refused for a rate limit did not run, so nothing
4022 /// this replays has already taken effect. An outcome this source cannot know — the
4023 /// send failed, or the body could not be read, so the mutation may well have landed —
4024 /// is [`Attempt::Failed`] in [`send_once`] and leaves this loop without a second
4025 /// attempt. A duplicate write would come from replaying one of those, and none is
4026 /// replayed.
4027 async fn graphql(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
4028 if is_mutation(query)
4029 && ![
4030 graphql::ADD_COMMENT,
4031 graphql::UPDATE_COMMENT,
4032 graphql::DELETE_COMMENT,
4033 ]
4034 .contains(&query)
4035 {
4036 let mut cache = self.resolved_cache()?;
4037 for argument in ["input", "second", "third", "clear"] {
4038 if let Some(input) = variables.get(argument) {
4039 cache.retain(|id, item| {
4040 !["id", "issueId", "subjectId", "itemId"].iter().any(|key| {
4041 input
4042 .get(key)
4043 .and_then(Value::as_str)
4044 .is_some_and(|value| value == id.0 || value == item.item_id)
4045 })
4046 });
4047 }
4048 }
4049 }
4050 let doing = operation_description(query);
4051 let mut waited = Duration::ZERO;
4052 let mut waits = 0_u32;
4053 let mut backoff = self.pacing.retry_backoff;
4054 loop {
4055 if is_mutation(query) {
4056 let spacing = self.reserve_mutation_slot();
4057 if !spacing.is_zero() {
4058 self.clock.sleep(spacing).await;
4059 }
4060 }
4061 let attempt = self.send_once(query, &variables).await;
4062 if is_mutation(query) {
4063 self.finish_mutation();
4064 }
4065 let limited = match attempt {
4066 Ok(data) => return Ok(data),
4067 Err(Attempt::Failed(error)) => return Err(error),
4068 Err(Attempt::Limited(limited)) => limited,
4069 };
4070 // GitHub really does send `retry-after: 0`, and retrying at once is the one
4071 // move that extends a secondary limit, so a hint below the schedule's own next
4072 // wait is raised to it.
4073 let wait = match limited.hint {
4074 Some(hint) => Duration::from_secs(hint).max(backoff),
4075 None => backoff,
4076 };
4077 let remaining = self.pacing.retry_budget.saturating_sub(waited);
4078 // A wait of nothing spends none of the budget, so it is exhaustion rather
4079 // than a retry. `Pacing::resolve` rules out every way of configuring one
4080 // except a budget of zero, where reporting the first refusal is the ask.
4081 if wait.is_zero() || wait > remaining {
4082 return Err(limited.exhausted(
4083 doing,
4084 waits,
4085 waited,
4086 wait,
4087 self.pacing.retry_budget,
4088 ));
4089 }
4090 self.clock.sleep(wait).await;
4091 waited += wait;
4092 waits += 1;
4093 backoff = backoff.saturating_mul(2);
4094 }
4095 }
4096
4097 /// The next moment a content-creating mutation may leave this source, as a wait from
4098 /// now.
4099 ///
4100 /// The slot is reserved under the lock and the waiting happens outside it, so two
4101 /// callers take two slots rather than the same one — and no lock is held across an
4102 /// await.
4103 ///
4104 /// The moment it is spaced from is the previous mutation's *completion*, which
4105 /// [`Self::finish_mutation`] records. See that method for why the release moment on its
4106 /// own is the wrong thing to measure from.
4107 fn reserve_mutation_slot(&self) -> Duration {
4108 if self.pacing.min_mutation_interval.is_zero() {
4109 return Duration::ZERO;
4110 }
4111 // A poisoned lock here costs pacing, not correctness, and refusing the write over
4112 // it would turn an earlier failure into a second one for no gain.
4113 let mut last = self
4114 .last_mutation
4115 .lock()
4116 .unwrap_or_else(std::sync::PoisonError::into_inner);
4117 let now = self.clock.now();
4118 // `checked_add` rather than `+`: adding durations can panic on overflow, and
4119 // pacing is not worth a panic even at a bound `MAX_PACING_MS` already rules out.
4120 let at = last.map_or(now, |previous| {
4121 previous
4122 .checked_add(self.pacing.min_mutation_interval)
4123 .map_or(now, |earliest| earliest.max(now))
4124 });
4125 *last = Some(at);
4126 at.saturating_sub(now)
4127 }
4128
4129 /// Record that a content-creating mutation has finished, so the next one is spaced
4130 /// from here rather than from the moment this one was released.
4131 ///
4132 /// This source can only choose when a request *departs*; the limiter counts when it
4133 /// *arrives*, and the two differ by whatever the request spent in transit. Spacing one
4134 /// departure from the last therefore hands the limiter a gap of the interval less that
4135 /// transit, so a source pacing at 750 ms can still be seen arriving faster — which is
4136 /// exactly how a copy paced well inside a board's threshold was refused by it on a
4137 /// slower machine while passing on a quick one.
4138 ///
4139 /// Spacing from completion removes the subtraction rather than budgeting for it. The
4140 /// previous request had already arrived before its response came back, so its arrival
4141 /// is no later than this moment, and the next mutation is released at least the
4142 /// interval after this moment and arrives no earlier than it is released: the gap the
4143 /// limiter measures is therefore at least the interval, whatever transit costs and on
4144 /// whatever platform. The price is that a mutation's own round trip no longer counts
4145 /// towards its spacing, which makes this source slightly slower than the configured
4146 /// rate rather than slightly faster — the safe side of a limit that punishes being
4147 /// wrong by refusing reads for the next fifty minutes.
4148 ///
4149 /// A failed attempt is recorded too: a request refused by the limiter still arrived,
4150 /// and one that never left costs only a wait nobody needed.
4151 fn finish_mutation(&self) {
4152 if self.pacing.min_mutation_interval.is_zero() {
4153 return;
4154 }
4155 // A poisoned lock here costs pacing, not correctness, exactly as in the reservation.
4156 let mut last = self
4157 .last_mutation
4158 .lock()
4159 .unwrap_or_else(std::sync::PoisonError::into_inner);
4160 let now = self.clock.now();
4161 // `max` rather than an assignment: a concurrent caller may already have reserved a
4162 // slot further out, and completing this request must never pull that slot back in.
4163 *last = Some(last.map_or(now, |reserved| reserved.max(now)));
4164 }
4165
4166 /// One HTTP attempt, classified into an answer, a rate limit to wait out, or a
4167 /// failure that waiting cannot help — and recorded, whichever of the three it was.
4168 ///
4169 /// This is the one place a request leaves this crate, which is why the accounting is
4170 /// here rather than at each of the callers: a read path added later is counted without
4171 /// anybody remembering to count it, and
4172 /// `the_session_report_counts_every_request_the_board_served_and_what_each_cost` fails
4173 /// when one is not.
4174 async fn send_once(&self, query: &str, variables: &Value) -> Result<Value, Attempt> {
4175 let Attempted {
4176 result,
4177 limits,
4178 reported_cost,
4179 } = self.attempt(query, variables).await;
4180 // No `otherwise` name: every document this source sends is one of its own, and the
4181 // inventory gate on `graphql::DOCUMENTS` is what keeps that true.
4182 let sending = accounting::Request::graphql(query, variables, None, reported_cost);
4183 let outcome = match &result {
4184 Ok(_) => accounting::Outcome::Answered,
4185 Err(Attempt::Limited(_)) => accounting::Outcome::RateLimited,
4186 Err(Attempt::Failed(_)) => accounting::Outcome::Refused,
4187 };
4188 self.ledger.record(sending.finished(outcome, limits));
4189 result
4190 }
4191
4192 /// The attempt itself, with what its response said about the rate limit alongside.
4193 ///
4194 /// The two are returned together rather than recorded here because every one of the
4195 /// early exits below is a different outcome, and a record written at each of them is a
4196 /// record one of them can be added without.
4197 async fn attempt(&self, query: &str, variables: &Value) -> Attempted {
4198 let mut limits = accounting::RateLimit::default();
4199 let mut reported_cost = None;
4200 let result = self
4201 .attempted(query, variables, &mut limits, &mut reported_cost)
4202 .await;
4203 Attempted {
4204 result,
4205 limits,
4206 reported_cost,
4207 }
4208 }
4209
4210 /// One HTTP attempt, filling in what its response said about the rate limit as it goes.
4211 async fn attempted(
4212 &self,
4213 query: &str,
4214 variables: &Value,
4215 limits: &mut accounting::RateLimit,
4216 reported_cost: &mut Option<u64>,
4217 ) -> Result<Value, Attempt> {
4218 let response = self
4219 .client
4220 .post(self.endpoint.clone())
4221 .bearer_auth(self.token.expose_secret())
4222 .json(&json!({"query": query, "variables": variables}))
4223 .send()
4224 .await
4225 .map_err(|e| {
4226 Attempt::Failed(SourceError::Unavailable {
4227 message: format!("GitHub GraphQL request failed: {e}"),
4228 })
4229 })?;
4230 let status = response.status();
4231 let header = |name: &str| whole_seconds(response.headers().get(name));
4232 *limits = accounting::RateLimit::read(|name| {
4233 response
4234 .headers()
4235 .get(name)
4236 .and_then(|value| value.to_str().ok())
4237 .map(str::to_owned)
4238 });
4239 // Exactly `0` is exhaustion and everything else — a count, an empty value, bytes
4240 // that are not text at all — is "not known to be exhausted". This never makes a
4241 // response a refusal on its own: it says which limiter a refusal is attributed to
4242 // and where its hint comes from, so a value this cannot read costs a hint rather
4243 // than an answer.
4244 let exhausted = response
4245 .headers()
4246 .get("x-ratelimit-remaining")
4247 .and_then(|value| value.to_str().ok())
4248 == Some("0");
4249 // `retry-after` is what GitHub asks for when it asks; when it does not and the
4250 // primary budget is spent, `x-ratelimit-reset` says when that budget comes back,
4251 // which is the same question answered as an absolute time. Nothing else here is a
4252 // hint, and a schedule is what answers a refusal that carries none.
4253 let hint = header("retry-after").or_else(|| {
4254 exhausted
4255 .then(|| header("x-ratelimit-reset"))
4256 .flatten()
4257 .map(|reset| reset.saturating_sub(Utc::now().timestamp().max(0).unsigned_abs()))
4258 });
4259 // Read before it is parsed, because the evidence which tells a secondary rate
4260 // limit from a rejected credential is in the body of a response whose status says
4261 // only "forbidden" — and a non-success response was never parsed at all.
4262 let body = response.text().await.map_err(|e| {
4263 Attempt::Failed(SourceError::Unavailable {
4264 message: format!("GitHub GraphQL response could not be read: {e}"),
4265 })
4266 })?;
4267 if let Some(limiter) = Limiter::classify(status, exhausted, &body) {
4268 return Err(Attempt::Limited(Limited { limiter, hint }));
4269 }
4270 if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {
4271 return Err(Attempt::Failed(SourceError::Auth {
4272 message: format!(
4273 "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"
4274 ),
4275 }));
4276 }
4277 if !status.is_success() {
4278 return Err(Attempt::Failed(SourceError::Unavailable {
4279 message: format!("GitHub GraphQL returned HTTP {status}"),
4280 }));
4281 }
4282 // GitHub reports what a call cost only when the document asked it to, and no
4283 // document this source sends does — so this is `None` here and carries the figure
4284 // for a caller whose own document selects `rateLimit { cost }`. What it must never
4285 // pick up is a `dryRun` probe's cost, which is some other document's.
4286 *reported_cost = serde_json::from_str::<Value>(&body)
4287 .ok()
4288 .as_ref()
4289 .and_then(|body| body.pointer("/data/rateLimit/cost"))
4290 .and_then(Value::as_u64);
4291 self.answer(&body).map_err(Attempt::Failed)
4292 }
4293
4294 /// What one successful HTTP response says, once its GraphQL errors are read.
4295 fn answer(&self, body: &str) -> Result<Value, SourceError> {
4296 let body: Value = serde_json::from_str(body).map_err(|e| SourceError::Malformed {
4297 message: format!("GitHub returned invalid JSON: {e}"),
4298 })?;
4299 let errors = body
4300 .get("errors")
4301 .map(|value| {
4302 value.as_array().ok_or_else(|| SourceError::Malformed {
4303 message: "GitHub response errors is not an array".into(),
4304 })
4305 })
4306 .transpose()?;
4307 if let Some(errors) = errors.filter(|errors| !errors.is_empty()) {
4308 let messages = errors
4309 .iter()
4310 .filter_map(|e| e.get("message").and_then(Value::as_str))
4311 .collect::<Vec<_>>()
4312 .join("; ");
4313 let message = if messages.is_empty() {
4314 "GitHub returned GraphQL errors".into()
4315 } else {
4316 messages
4317 };
4318 let normalized = message.to_ascii_lowercase();
4319 if normalized.contains("resource not accessible") || normalized.contains("scope") {
4320 return Err(SourceError::Auth {
4321 message: format!(
4322 "{message}; grant {} Projects and Issues read/write plus Pull requests read-only access for every repository represented on the board",
4323 self.credential_name
4324 ),
4325 });
4326 }
4327 return Err(SourceError::Refused { message });
4328 }
4329 body.get("data")
4330 .filter(|data| data.is_object())
4331 .cloned()
4332 .ok_or_else(|| SourceError::Malformed {
4333 message: "GitHub response has no data object".into(),
4334 })
4335 }
4336
4337 // llmlint: ignore[boundary_inputs_validated] GitHub caps nested connections at 100 and
4338 // GraphQL cannot independently page them inside the outer item page. This source page is
4339 // deliberately bounded at that published maximum; the live drift journey exercises it.
4340 async fn board_page(
4341 &self,
4342 items_after: Option<&str>,
4343 items_first: u32,
4344 ) -> Result<Value, SourceError> {
4345 let data = self
4346 .graphql(
4347 graphql::BOARD,
4348 json!({"owner":self.owner,"number":self.project_number,
4349 "first":items_first.min(MAX_PAGE_SIZE),"after":items_after,
4350 "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
4351 )
4352 .await?;
4353 data.pointer("/owner/projectV2")
4354 .filter(|v| !v.is_null())
4355 .cloned()
4356 .ok_or_else(|| SourceError::Refused {
4357 message: format!(
4358 "GitHub project {}/{} was not found or is not visible to the token",
4359 self.owner, self.project_number
4360 ),
4361 })
4362 }
4363
4364 /// The search that finds the issues of this board, narrowed by `also` when it is
4365 /// given.
4366 ///
4367 /// `project:owner/number` is what scopes a search to one board, and `is:issue` is what
4368 /// keeps pull requests out of it: GitHub's `ISSUE` search type covers both, and a pull
4369 /// request is somebody's change rather than a unit of plan. `-has:parent` is *not*
4370 /// here on purpose — GitHub accepts it and silently ignores it, so a project is told
4371 /// from a task by the `parent` field each issue carries rather than by the search.
4372 fn board_search(&self, also: Option<&str>) -> String {
4373 let scope = format!("project:{}/{} is:issue", self.owner, self.project_number);
4374 match also {
4375 Some(also) => format!("{scope} {also}"),
4376 None => scope,
4377 }
4378 }
4379
4380 /// One issue this source reached directly, as the board item a read of the board would
4381 /// have produced — or `None` when this board does not hold it.
4382 ///
4383 /// The board half of an issue rides along on `Issue.projectItems`, so the value handed
4384 /// to [`Self::resolve`] is the very shape a `ProjectV2.items` read gives it: the board
4385 /// item's own id, that item's field values, and the issue as its content. One resolver
4386 /// for both routes is what makes an issue read through a search, through its own node
4387 /// id, or through its project's sub-issues report the same title, the same status, the
4388 /// same labels and the same qualified id.
4389 ///
4390 /// An issue with no entry for *this* board is not this source's to report, which is
4391 /// what keeps an id naming some other repository's issue from being answered as an item
4392 /// of this board. That answer is given about an **exhausted** connection and never
4393 /// about an unread page: the entry is looked for on the page in hand, and only if that
4394 /// page reports more of the connection, in [`Self::board_membership`]'s walk of the
4395 /// rest of it.
4396 async fn resolve_issue(&self, issue: &Value) -> Result<Option<Resolved>, SourceError> {
4397 if optional_str(issue, "__typename")? != Some("Issue") {
4398 return Ok(None);
4399 }
4400 let memberships = issue
4401 .get("projectItems")
4402 .ok_or_else(|| SourceError::Malformed {
4403 message: "GitHub issue is missing projectItems".into(),
4404 })?;
4405 let nodes = memberships
4406 .get("nodes")
4407 .and_then(Value::as_array)
4408 .ok_or_else(|| SourceError::Malformed {
4409 message: "GitHub issue projectItems.nodes is not an array".into(),
4410 })?;
4411 let held = match self.board_entry(nodes) {
4412 Some(held) => held.clone(),
4413 None => {
4414 let info = memberships
4415 .get("pageInfo")
4416 .ok_or_else(|| SourceError::Malformed {
4417 message: "GitHub issue projectItems has no pageInfo".into(),
4418 })?;
4419 // The page held no entry for this board. Whether that means the issue is
4420 // not on it is a question about the rest of the connection, and only a
4421 // connection with no rest answers it here.
4422 if !required_bool(info, "hasNextPage")? {
4423 return Ok(None);
4424 }
4425 let cursor = required_str(info, "endCursor")?;
4426 validate_cursor_progress(None, cursor)?;
4427 let issue_id = required_str(issue, "id")?;
4428 match self.board_membership(issue_id, cursor).await? {
4429 Some(held) => held,
4430 None => return Ok(None),
4431 }
4432 }
4433 };
4434 let item = json!({
4435 "id": required_str(&held, "id")?,
4436 "project": held.get("project"),
4437 "fieldValues": held.get("fieldValues"),
4438 "content": issue,
4439 });
4440 self.resolve(&item)
4441 }
4442
4443 /// This board's own entry among one page of an issue's `Issue.projectItems`.
4444 ///
4445 /// One spelling of *which membership is this board's*, so the page a read carries and
4446 /// the pages [`Self::board_membership`] walks are searched by the same rule.
4447 fn board_entry<'a>(&self, nodes: &'a [Value]) -> Option<&'a Value> {
4448 nodes.iter().find(|node| {
4449 node.pointer("/project/number").and_then(Value::as_u64)
4450 == Some(u64::from(self.project_number))
4451 })
4452 }
4453
4454 /// The rest of one issue's board memberships, from `after`, for this board's entry.
4455 ///
4456 /// The recovery read: a page of memberships that holds no entry for this board says
4457 /// nothing about the memberships past it, so the connection is walked to exhaustion
4458 /// before an issue is reported as one this board does not hold. `Ok(None)` is that
4459 /// positive answer — the whole connection was read and no entry named this board —
4460 /// rather than a failure, and the walk is held to
4461 /// [`validate_cursor_progress`] like every other page walk here, so a source answering
4462 /// with a cursor that does not advance is refused instead of spun on.
4463 async fn board_membership(
4464 &self,
4465 issue: &str,
4466 after: &str,
4467 ) -> Result<Option<Value>, SourceError> {
4468 let mut after = after.to_owned();
4469 loop {
4470 let data = self
4471 .graphql(
4472 graphql::ISSUE_BOARD_ITEMS,
4473 json!({"id":issue,"first":MAX_PAGE_SIZE,"after":after,
4474 "nestedFirst":NESTED_PAGE_SIZE}),
4475 )
4476 .await?;
4477 let Some(connection) = data
4478 .pointer("/node/projectItems")
4479 .filter(|value| !value.is_null())
4480 else {
4481 // The id resolved to nothing, or to something with no memberships to walk —
4482 // which is the same answer as a connection holding no entry for this board.
4483 return Ok(None);
4484 };
4485 let nodes = connection
4486 .get("nodes")
4487 .and_then(Value::as_array)
4488 .ok_or_else(|| SourceError::Malformed {
4489 message: "GitHub issue projectItems.nodes is not an array".into(),
4490 })?;
4491 if let Some(held) = self.board_entry(nodes) {
4492 return Ok(Some(held.clone()));
4493 }
4494 let info = connection
4495 .get("pageInfo")
4496 .ok_or_else(|| SourceError::Malformed {
4497 message: "GitHub issue projectItems has no pageInfo".into(),
4498 })?;
4499 let next = required_bool(info, "hasNextPage")?
4500 .then(|| required_str(info, "endCursor"))
4501 .transpose()?;
4502 match next {
4503 Some(next) => {
4504 validate_cursor_progress(Some(&after), next)?;
4505 after = next.to_owned();
4506 }
4507 None => return Ok(None),
4508 }
4509 }
4510 }
4511
4512 /// One page of a board-scoped issue search, and where the next page resumes.
4513 async fn search_page(
4514 &self,
4515 search: &str,
4516 first: u32,
4517 after: Option<&str>,
4518 ) -> Result<(Vec<Resolved>, Option<String>), SourceError> {
4519 let data = self
4520 .graphql(
4521 graphql::SEARCH_ISSUES,
4522 json!({"search":search,"type":"ISSUE","first":first.min(MAX_PAGE_SIZE),
4523 "after":after,"nestedFirst":NESTED_PAGE_SIZE,
4524 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
4525 )
4526 .await?;
4527 let connection = data.get("search").ok_or_else(|| SourceError::Malformed {
4528 message: "GitHub search response has no search connection".into(),
4529 })?;
4530 let mut found = Vec::new();
4531 for node in connection
4532 .get("nodes")
4533 .and_then(Value::as_array)
4534 .ok_or_else(|| SourceError::Malformed {
4535 message: "GitHub search nodes is not an array".into(),
4536 })?
4537 {
4538 if let Some(resolved) = self.resolve_issue(node).await? {
4539 found.push(resolved);
4540 }
4541 }
4542 let info = connection
4543 .get("pageInfo")
4544 .ok_or_else(|| SourceError::Malformed {
4545 message: "GitHub search connection has no pageInfo".into(),
4546 })?;
4547 let next = required_bool(info, "hasNextPage")?
4548 .then(|| required_str(info, "endCursor"))
4549 .transpose()?
4550 .map(str::to_owned);
4551 if let Some(next) = &next {
4552 validate_cursor_progress(after, next)?;
4553 }
4554 Ok((found, next))
4555 }
4556
4557 /// Every issue this board holds, completed with what this run wrote.
4558 ///
4559 /// The completion is not an optimisation and it is not a cache: GitHub's issue search
4560 /// is an index and is eventually consistent, so an issue this run created seconds ago
4561 /// can be absent from it, and a project listed straight after being written would
4562 /// otherwise be missing from its own board. What is added back is only what this
4563 /// process itself wrote, out of [`Self::created`], which lives and dies with the
4564 /// process.
4565 async fn board_issues(&self) -> Result<Vec<Resolved>, SourceError> {
4566 let found = self.searched_issues().await?;
4567 self.completed_with_written(found, |_| true)
4568 }
4569
4570 /// Every issue this board's own search reports, walked to exhaustion, read once per
4571 /// source.
4572 ///
4573 /// The uncompleted half of [`Self::board_issues`], separated because [`Self::board`]
4574 /// needs it too and the two would otherwise walk the same search twice in one command.
4575 /// See [`Self::search_cache`] for why holding it is the same bargain holding the board
4576 /// is.
4577 async fn searched_issues(&self) -> Result<Vec<Resolved>, SourceError> {
4578 let cached = self.search_cache()?.clone();
4579 if let Some(held) = cached {
4580 return Ok(held);
4581 }
4582 let mut after: Option<String> = None;
4583 let mut found = Vec::new();
4584 let search = self.board_search(None);
4585 loop {
4586 let (page, next) = self
4587 .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
4588 .await?;
4589 found.extend(page);
4590 match next {
4591 Some(next) => after = Some(next),
4592 None => break,
4593 }
4594 }
4595 *self.search_cache()? = Some(found.clone());
4596 Ok(found)
4597 }
4598
4599 /// This process's own view of the board's issues, or the refusal a poisoned lock is.
4600 fn search_cache(
4601 &self,
4602 ) -> Result<std::sync::MutexGuard<'_, Option<Vec<Resolved>>>, SourceError> {
4603 self.search_cache
4604 .lock()
4605 .map_err(|_| SourceError::Unavailable {
4606 message: "this source's view of the board's issues was left inconsistent by an \
4607 earlier failure; next: run the command again"
4608 .into(),
4609 })
4610 }
4611
4612 /// `found`, with everything this run wrote that `keep` accepts and the read did not
4613 /// report.
4614 ///
4615 /// See [`Self::created`] and [`Self::board_issues`] for why a read has to be completed
4616 /// at all: the search index is behind, and a node read of an item filed moments ago can
4617 /// be too.
4618 fn completed_with_written(
4619 &self,
4620 mut found: Vec<Resolved>,
4621 keep: impl Fn(&Resolved) -> bool,
4622 ) -> Result<Vec<Resolved>, SourceError> {
4623 for own in self.created()?.iter().filter(|own| keep(own)) {
4624 if !found.iter().any(|item| item.id == own.id) {
4625 found.push(own.clone());
4626 }
4627 }
4628 Ok(found)
4629 }
4630
4631 /// What resolving one node id reached.
4632 ///
4633 /// Three answers rather than an `Option`, because a board *draft* is none of the other
4634 /// two: it is not an issue, so the issue fragment reads nothing of it, and a read of one
4635 /// is completed by a read of the draft itself rather than reported as nothing.
4636 async fn reach(&self, id: &NativeId) -> Result<Reached, SourceError> {
4637 let asked = self
4638 .graphql(
4639 graphql::ISSUE,
4640 json!({"id":id.0,"first":MAX_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
4641 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
4642 )
4643 .await;
4644 let data = match asked {
4645 Ok(data) => data,
4646 // A string that is not a node id at all is not a failure to report: it is an id
4647 // this board does not hold, which is what every read of one already answers.
4648 Err(error) if unresolvable_node(&error) => return Ok(Reached::Nothing),
4649 Err(error) => return Err(error),
4650 };
4651 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
4652 return Ok(Reached::Nothing);
4653 };
4654 if optional_str(node, "__typename")? == Some("DraftIssue") {
4655 return Ok(Reached::Draft);
4656 }
4657 Ok(match self.resolve_issue(node).await? {
4658 Some(item) => Reached::Held(Box::new(item)),
4659 None => Reached::Nothing,
4660 })
4661 }
4662
4663 /// One item of this board by its own id, whatever kind it is.
4664 ///
4665 /// Resolved from the identifier alone: no search, board-wide or otherwise. What this
4666 /// run wrote is read first, because a node read of an item created moments ago can
4667 /// still be behind the board field values written onto it — see [`Self::created`].
4668 async fn item_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
4669 if let Some(own) = self.created()?.iter().find(|own| own.id == *id) {
4670 return Ok(Some(own.clone()));
4671 }
4672 match self.reach(id).await? {
4673 Reached::Held(item) => Ok(Some(*item)),
4674 Reached::Nothing => Ok(None),
4675 Reached::Draft => self.draft_by_id(id).await,
4676 }
4677 }
4678
4679 /// Several items of this board, each by its own id, in order — what [`Self::item_by_id`]
4680 /// answers for each, read [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] rather
4681 /// than one request per id.
4682 ///
4683 /// What this run wrote answers first, as it does there, and only the rest is read. One id
4684 /// left to read is read by [`Self::item_by_id`] itself, which costs what a batch does. A
4685 /// batch GitHub refuses because one of its ids resolves to no node at all is read again one
4686 /// id at a time, so that id is answered as not held and the others as themselves; a draft
4687 /// is completed by a read of the draft, exactly as there.
4688 async fn items_by_ids(&self, ids: &[NativeId]) -> Result<Vec<Option<Resolved>>, SourceError> {
4689 let mut found: Vec<Option<Option<Resolved>>> = {
4690 let created = self.created()?;
4691 ids.iter()
4692 .map(|id| {
4693 created
4694 .iter()
4695 .find(|own| own.id == *id)
4696 .map(|own| Some(own.clone()))
4697 })
4698 .collect()
4699 };
4700 let unread: Vec<NativeId> = ids
4701 .iter()
4702 .zip(&found)
4703 .filter(|(_, found)| found.is_none())
4704 .map(|(id, _)| id.clone())
4705 .collect();
4706 let mut read = Vec::with_capacity(unread.len());
4707 if let [one] = unread.as_slice() {
4708 read.push(self.item_by_id(one).await?);
4709 } else {
4710 for batch in unread.chunks(DETAIL_BATCH) {
4711 let data = match self
4712 .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, None))
4713 .await
4714 {
4715 Ok(data) => data,
4716 Err(error) if unresolvable_node(&error) => {
4717 for id in batch {
4718 read.push(self.item_by_id(id).await?);
4719 }
4720 continue;
4721 }
4722 Err(error) => return Err(error),
4723 };
4724 for (slot, id) in batch.iter().enumerate() {
4725 let node =
4726 data.get(format!("i{slot}"))
4727 .ok_or_else(|| SourceError::Malformed {
4728 message: format!(
4729 "GitHub answered a batch read with no item for {}",
4730 id.0
4731 ),
4732 })?;
4733 read.push(if node.is_null() {
4734 None
4735 } else if optional_str(node, "__typename")? == Some("DraftIssue") {
4736 self.draft_by_id(id).await?
4737 } else {
4738 if optional_str(node, "__typename")? == Some("Issue")
4739 && required_str(node, "id")? != id.0
4740 {
4741 return Err(SourceError::Malformed {
4742 message: format!(
4743 "GitHub answered the read of {} with issue {}",
4744 id.0,
4745 required_str(node, "id")?
4746 ),
4747 });
4748 }
4749 self.resolve_issue(node).await?
4750 });
4751 }
4752 }
4753 }
4754 let mut read = read.into_iter();
4755 Ok(found
4756 .iter_mut()
4757 .map(|slot| slot.take().unwrap_or_else(|| read.next().flatten()))
4758 .collect())
4759 }
4760
4761 fn resolved_cache(
4762 &self,
4763 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
4764 self.resolved_cache
4765 .lock()
4766 .map_err(|_| SourceError::Unavailable {
4767 message: "resolved item records were left inconsistent; run the command again"
4768 .into(),
4769 })
4770 }
4771
4772 /// Reuse a record this invocation already resolved. The mutation sender invalidates
4773 /// it before writing, so a partial failure cannot leave a pre-write binding behind.
4774 async fn bound_item(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
4775 let cached = self.resolved_cache()?.get(id).cloned();
4776 match cached {
4777 Some(item) => Ok(Some(item)),
4778 None => self.item_by_id(id).await,
4779 }
4780 }
4781
4782 /// One board draft by its own id, with the board item it sits in — or `None` when no
4783 /// item of this board is that draft's.
4784 ///
4785 /// The same decision [`Self::resolve_issue`] makes for an issue, over the draft's own
4786 /// `projectV2Items`: an entry naming this board is what makes it this board's. GitHub
4787 /// links a draft to one board item, so the page this read carries is the whole of that
4788 /// connection, and a page that reports more than it holds is refused rather than read
4789 /// as an answer about memberships nobody read.
4790 async fn draft_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
4791 let data = self
4792 .graphql(
4793 graphql::DRAFT,
4794 json!({"id":id.0,"nestedFirst":NESTED_PAGE_SIZE,
4795 "boardItems":BOARD_ITEMS_PAGE_SIZE}),
4796 )
4797 .await?;
4798 // Gone between the two reads is an answer — the draft is no longer there. Anything
4799 // else than the draft [`Self::reach`] was just told this id is, is not one.
4800 let Some(draft) = data.get("node").filter(|node| !node.is_null()) else {
4801 return Ok(None);
4802 };
4803 if optional_str(draft, "__typename")? != Some("DraftIssue") {
4804 return Err(SourceError::Malformed {
4805 message: format!(
4806 "GitHub answered {} as a draft and then as something else",
4807 id.0
4808 ),
4809 });
4810 }
4811 if required_str(draft, "id")? != id.0 {
4812 return Err(SourceError::Malformed {
4813 message: format!("GitHub answered a different draft for {}", id.0),
4814 });
4815 }
4816 let memberships = draft
4817 .get("projectV2Items")
4818 .ok_or_else(|| SourceError::Malformed {
4819 message: format!("GitHub draft {} is missing projectV2Items", id.0),
4820 })?;
4821 let nodes = memberships
4822 .get("nodes")
4823 .and_then(Value::as_array)
4824 .ok_or_else(|| SourceError::Malformed {
4825 message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
4826 })?;
4827 let info = memberships
4828 .get("pageInfo")
4829 .ok_or_else(|| SourceError::Malformed {
4830 message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
4831 })?;
4832 // Read whether or not this board's entry is on the page: a page claiming more than
4833 // the one item GitHub links a draft to is a malformed answer either way.
4834 if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
4835 return Err(SourceError::Malformed {
4836 message: format!(
4837 "GitHub draft {} reports more board items than the one GitHub links a draft \
4838 to",
4839 id.0
4840 ),
4841 });
4842 }
4843 if let Some(node) = nodes.first()
4844 && node
4845 .pointer("/project/number")
4846 .and_then(Value::as_u64)
4847 .is_none()
4848 {
4849 return Err(SourceError::Malformed {
4850 message: format!(
4851 "GitHub draft {} board item has no numeric project number",
4852 id.0
4853 ),
4854 });
4855 }
4856 let Some(held) = self.board_entry(nodes) else {
4857 return Ok(None);
4858 };
4859 if required_str(
4860 held.get("project").ok_or_else(|| SourceError::Malformed {
4861 message: format!("GitHub draft {} board item has no project", id.0),
4862 })?,
4863 "id",
4864 )? != self.board_fields().await?.id.as_str()
4865 {
4866 return Ok(None);
4867 }
4868 let item = json!({
4869 "id": required_str(held, "id")?,
4870 "project": held.get("project"),
4871 "fieldValues": held.get("fieldValues"),
4872 "content": draft,
4873 });
4874 self.resolve(&item)
4875 }
4876
4877 /// The board's own id and field definitions, for a write whose item does not carry
4878 /// them — never its items.
4879 ///
4880 /// A board this command has already listed supplies them, since it read them beside its
4881 /// items; otherwise they come from [`graphql::BOARD_FIELDS`], once per command. Neither
4882 /// is consulted about which items the board holds: see the module documentation for
4883 /// why a question about one known item is answered by reading that item.
4884 async fn board_fields(&self) -> Result<BoardFields, SourceError> {
4885 if let Some(board) = self.board_cache()?.as_ref() {
4886 return Ok(BoardFields {
4887 id: BoardId::parse(&board.id)?,
4888 fields: board.fields.clone(),
4889 });
4890 }
4891 if let Some(held) = self.fields_cache()?.clone() {
4892 return Ok(held);
4893 }
4894 let data = self
4895 .graphql(
4896 graphql::BOARD_FIELDS,
4897 json!({"owner":self.owner,"number":self.project_number,
4898 "nestedFirst":NESTED_PAGE_SIZE}),
4899 )
4900 .await?;
4901 self.fields_read(&data)
4902 }
4903
4904 /// The board's id and fields out of an answer carrying the `boardFields` root, held for
4905 /// the rest of this command.
4906 fn fields_read(&self, data: &Value) -> Result<BoardFields, SourceError> {
4907 let board = data
4908 .pointer("/boardFields/projectV2")
4909 .filter(|value| !value.is_null())
4910 .ok_or_else(|| SourceError::Refused {
4911 message: format!(
4912 "GitHub project {}/{} was not found or is not visible to the token",
4913 self.owner, self.project_number
4914 ),
4915 })?;
4916 let read = BoardFields {
4917 id: BoardId::parse(required_str(board, "id")?)?,
4918 fields: board.get("fields").cloned().unwrap_or(Value::Null),
4919 };
4920 *self.fields_cache()? = Some(read.clone());
4921 Ok(read)
4922 }
4923
4924 /// Read what creating an issue in `repository` needs and this command has not read yet —
4925 /// the board's fields and the repository's node id — in one request when it needs both.
4926 ///
4927 /// When either is already known this sends nothing, and the other is read by its own
4928 /// document where it is asked for, so no create reads anything twice.
4929 async fn creation_context(
4930 &self,
4931 repository: &RepositoryTarget,
4932 incoming: &Incoming<'_>,
4933 ) -> Result<(), SourceError> {
4934 let fields_known = self.board_cache()?.is_some() || self.fields_cache()?.is_some();
4935 if fields_known || self.repository_cache()?.contains_key(repository) {
4936 return Ok(());
4937 }
4938 let data = self
4939 .graphql(
4940 graphql::CREATION_CONTEXT,
4941 json!({"owner":self.owner,"number":self.project_number,
4942 "nestedFirst":NESTED_PAGE_SIZE,"repositoryOwner":repository.owner,
4943 "repositoryName":repository.name}),
4944 )
4945 .await?;
4946 self.fields_read(&data)?;
4947 self.repository_read(&data, repository, incoming)?;
4948 Ok(())
4949 }
4950
4951 /// This process's own view of the board's fields, or the refusal a poisoned lock is.
4952 fn fields_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<BoardFields>>, SourceError> {
4953 self.fields_cache
4954 .lock()
4955 .map_err(|_| SourceError::Unavailable {
4956 message: "this source's view of the board's fields was left inconsistent by an \
4957 earlier failure; next: run the command again"
4958 .into(),
4959 })
4960 }
4961
4962 /// What a write to `item` needs of the board, read off that item when it says enough and
4963 /// off [`Self::board_fields`] when it does not.
4964 ///
4965 /// A node read of an item names its board and carries the definition of every field it
4966 /// holds a value of — so an item naming its board, holding a value of the origin field,
4967 /// and, when the write carries a status, holding a `Status` value, needs no read of the
4968 /// board at all. **Nothing the item does not say is guessed:** a field it holds no value
4969 /// of may still be on the board, and a view reading it as absent would refuse a write the
4970 /// board can take or skip a field write the board needs, so such an item — and a create,
4971 /// which has no item yet — takes the board's fields from their own read instead.
4972 async fn fields_for(
4973 &self,
4974 item: Option<&Resolved>,
4975 writes_status: bool,
4976 selects_priority: bool,
4977 ) -> Result<BoardFields, SourceError> {
4978 if let Some(board) = item.and_then(Resolved::carried_board) {
4979 return Ok(board);
4980 }
4981 if let Some(item) = item
4982 && let Some(board_id) = item.named_board()
4983 && item.defines(ORIGIN_FIELD)
4984 && (!writes_status || item.defines("Status"))
4985 && (!selects_priority || item.defines(PRIORITY_FIELD))
4986 && self
4987 .metadata_fields
4988 .iter()
4989 .all(|projection| item.defines(&projection.field))
4990 {
4991 return Ok(BoardFields {
4992 id: board_id,
4993 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
4994 });
4995 }
4996 self.board_fields().await
4997 }
4998
4999 /// Everything filed under one issue of this board, walked to exhaustion — or `None`
5000 /// when that id names nothing here with a sub-issue relationship to walk.
5001 ///
5002 /// `None` and an empty answer are different: `None` is *this is not an issue of this
5003 /// GitHub*, which is what sends a project selector on to be read as a name, and an
5004 /// empty vector is a project that holds nothing.
5005 async fn sub_issues(&self, id: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
5006 let mut after: Option<String> = None;
5007 let mut children = Vec::new();
5008 loop {
5009 let asked = self
5010 .graphql(
5011 graphql::SUB_ISSUES,
5012 json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after,
5013 "nestedFirst":NESTED_PAGE_SIZE,
5014 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
5015 )
5016 .await;
5017 let data = match asked {
5018 Ok(data) => data,
5019 // A string that is not a node id at all is not a failure to report: it is
5020 // the ordinary answer to a selector naming a project by its name.
5021 Err(error) if unresolvable_node(&error) => return Ok(None),
5022 Err(error) => return Err(error),
5023 };
5024 let Some(connection) = data
5025 .pointer("/node/subIssues")
5026 .filter(|value| !value.is_null())
5027 else {
5028 // No such node, or one with no sub-issue relationship — a board draft is
5029 // the one this board can really hold.
5030 return Ok(None);
5031 };
5032 for node in connection
5033 .get("nodes")
5034 .and_then(Value::as_array)
5035 .ok_or_else(|| SourceError::Malformed {
5036 message: "GitHub subIssues.nodes is not an array".into(),
5037 })?
5038 {
5039 if let Some(resolved) = self.resolve_issue(node).await? {
5040 children.push(resolved);
5041 }
5042 }
5043 let info = connection
5044 .get("pageInfo")
5045 .ok_or_else(|| SourceError::Malformed {
5046 message: "GitHub subIssues connection has no pageInfo".into(),
5047 })?;
5048 let next = required_bool(info, "hasNextPage")?
5049 .then(|| required_str(info, "endCursor"))
5050 .transpose()?;
5051 match next {
5052 Some(next) => {
5053 validate_cursor_progress(after.as_deref(), next)?;
5054 after = Some(next.to_owned());
5055 }
5056 None => return Ok(Some(children)),
5057 }
5058 }
5059 }
5060
5061 /// Which issue of this board a project *name* is, or `None` when none is.
5062 ///
5063 /// One bounded query which filters on that name at the server, rather than a walk of
5064 /// every issue the board holds. The name is compared again here: the qualifier narrows
5065 /// what GitHub sends, and this source decides what it names.
5066 async fn project_by_name(&self, name: &str) -> Result<Option<NativeId>, SourceError> {
5067 let search = self.board_search(Some(&title_qualifier(name)));
5068 let mut after = None;
5069 loop {
5070 let (candidates, next) = self
5071 .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
5072 .await?;
5073 if let Some(item) = candidates.into_iter().find(|item| {
5074 item.kind == BoardKind::Work(ItemKind::Project)
5075 && item.title.eq_ignore_ascii_case(name)
5076 }) {
5077 return Ok(Some(item.id));
5078 }
5079 match next {
5080 Some(next) => after = Some(next),
5081 None => return Ok(None),
5082 }
5083 }
5084 }
5085
5086 /// Everything filed under one project of this board: the sub-issues of the issue that
5087 /// project is.
5088 ///
5089 /// Tasks *and* documents, because a document filed under a project is a sub-issue of it
5090 /// too — the caller keeps the kind it asked for. Nothing about this grows as the board
5091 /// gains projects, or as another project gains tasks.
5092 ///
5093 /// A qualified id names the issue and is asked for its sub-issues directly: one
5094 /// request, no search of any kind. Only a selector GitHub cannot resolve that way is
5095 /// read as a project *name*, which costs the one bounded search
5096 /// [`Self::project_by_name`] makes.
5097 ///
5098 /// What GitHub answered is held for the rest of the command — see [`Self::children_cache`]
5099 /// — and each answer is still cut to the items whose parent is this project, so one this
5100 /// process has since filed elsewhere is not reported here, and completed with what this
5101 /// process filed under it.
5102 async fn project_children(&self, selector: &NativeId) -> Result<Vec<Resolved>, SourceError> {
5103 let held = self.children_cache()?.get(selector).cloned();
5104 let (project, children) = match held {
5105 Some(held) => held,
5106 None => {
5107 let answered = match self.sub_issues(selector).await? {
5108 Some(children) => (selector.clone(), children),
5109 None => match self.project_by_name(&selector.0).await? {
5110 Some(project) => {
5111 let children = self.sub_issues(&project).await?.unwrap_or_default();
5112 (project, children)
5113 }
5114 None => return Ok(Vec::new()),
5115 },
5116 };
5117 self.children_cache()?
5118 .insert(selector.clone(), answered.clone());
5119 answered
5120 }
5121 };
5122 let mut children: Vec<Resolved> = children
5123 .into_iter()
5124 .filter(|child| child.parent.as_ref() == Some(&project))
5125 .collect();
5126 for own in self.updated()?.iter() {
5127 if own.parent.as_ref() == Some(&project) && !children.iter().any(|c| c.id == own.id) {
5128 children.push(own.clone());
5129 }
5130 }
5131 self.completed_with_written(children, |own| own.parent.as_ref() == Some(&project))
5132 }
5133
5134 /// Every issue of this board GitHub's issue search reports updated at or after `since`,
5135 /// completed with what this run wrote — the candidates a comment-activity read confirms.
5136 ///
5137 /// Scoped by the board and by nothing else: `project:<owner>/<number>` reaches every issue
5138 /// on the board whatever repository, and whatever owner, it lives in, so no repository or
5139 /// owner qualifier is added and none is needed. What makes the `updated:` qualifier
5140 /// sufficient is a fact about GitHub rather than about this source: a comment written on an
5141 /// issue **and a comment edited on it** both move that issue's `updatedAt`. The credentialed
5142 /// journey `an_edited_comment_moves_its_issue_and_is_selected_since` in `tests/journey`
5143 /// re-takes that fact on every run of the lane, so a change on GitHub's side fails there
5144 /// rather than silently narrowing a caller's answer.
5145 ///
5146 /// The instant is written to the second, rounded down, which can only widen what the
5147 /// search returns; confirmation against each candidate's own comments is what makes the
5148 /// answer exact. The search is an index that lags a write by a second or two — the module
5149 /// documentation records it — so a caller that asks again from its last instant should
5150 /// overlap the two by more than that.
5151 async fn updated_since(&self, since: DateTime<Utc>) -> Result<Vec<Resolved>, SourceError> {
5152 let found = self.searched(&updated_qualifier(since)).await?;
5153 self.completed_with_written(found, |_| true)
5154 }
5155
5156 /// Every issue of this board GitHub's issue search reports for the board-scoped search
5157 /// narrowed by `also`, in pages of [`SEARCH_PAGE_SIZE`].
5158 ///
5159 /// Uncompleted: what this process wrote is added by the caller, which knows whether its
5160 /// own record is the fresher of the two.
5161 async fn searched(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
5162 let search = self.board_search(Some(also));
5163 let mut after: Option<String> = None;
5164 let mut found = Vec::new();
5165 loop {
5166 let (page, next) = self
5167 .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
5168 .await?;
5169 found.extend(page);
5170 match next {
5171 Some(next) => after = Some(next),
5172 None => return Ok(found),
5173 }
5174 }
5175 }
5176
5177 /// A bounded task answer; the versioned cursor carries the connection position, how
5178 /// many rows of the page starting there were already handed out, and the own-write ids
5179 /// already observed, including across a new source instance.
5180 ///
5181 /// Every page is sent at [`SEARCH_PAGE_SIZE`] whatever the caller's limit, and a limit is
5182 /// sliced from the pages it needs; why is the module documentation's paging contract.
5183 async fn search_tasks(
5184 &self,
5185 query: &TaskQuery,
5186 page: &PageRequest,
5187 also: &str,
5188 ) -> Result<Page<Task>, SourceError> {
5189 let mut position = match &page.cursor {
5190 None => SearchPosition::default(),
5191 Some(cursor) => serde_json::from_str::<SearchPosition>(&cursor.0)
5192 .ok()
5193 .filter(|position| {
5194 position.version == SEARCH_CURSOR_VERSION
5195 && position.connection.valid_resume(position.offset)
5196 })
5197 .ok_or_else(|| SourceError::Config {
5198 message: "page cursor is invalid".into(),
5199 })?,
5200 };
5201 let search = self.board_search(Some(also));
5202 let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
5203 let own = self.with_own_writes(Vec::new())?;
5204 // An issue this process commented on is a candidate of a comment-activity read
5205 // whether or not the search has caught up with the comment; see `Self::commented`.
5206 let commented = match query.commented_since {
5207 Some(_) => self.commented()?.clone(),
5208 None => Vec::new(),
5209 };
5210 for id in own.iter().map(|item| &item.id).chain(&commented) {
5211 if !position.own.contains(id) {
5212 position.own.push(id.clone());
5213 }
5214 }
5215 let mut tasks = Vec::new();
5216 while !position.connection.exhausted() && tasks.len() < limit {
5217 let first = SEARCH_PAGE_SIZE;
5218 // Page size is part of the key: a short cached answer cannot answer a wider ask.
5219 let key =
5220 serde_json::to_string(&("page", &search, &position.connection.after(), first))
5221 .expect("search page key is serializable");
5222 let cached = if query.commented_since.is_none() {
5223 self.narrowed_cache()?.get(&key).cloned()
5224 } else {
5225 None
5226 };
5227 let (found, next) = match cached {
5228 Some(found) => {
5229 let next = self
5230 .search_next
5231 .lock()
5232 .map_err(|_| SourceError::Unavailable {
5233 message:
5234 "search pagination was left inconsistent; run the command again"
5235 .into(),
5236 })?
5237 .get(&key)
5238 .cloned()
5239 .flatten();
5240 (found, next)
5241 }
5242 None => {
5243 let (found, next) = self
5244 .search_page(&search, first, position.connection.after())
5245 .await?;
5246 if query.commented_since.is_none() {
5247 self.search_next
5248 .lock()
5249 .map_err(|_| SourceError::Unavailable {
5250 message:
5251 "search pagination was left inconsistent; run the command again"
5252 .into(),
5253 })?
5254 .insert(key.clone(), next.clone());
5255 self.narrowed_cache()?.insert(key, found.clone());
5256 }
5257 (found, next)
5258 }
5259 };
5260 let rows = found.len();
5261 for mut item in found.into_iter().skip(position.offset) {
5262 if tasks.len() == limit {
5263 break;
5264 }
5265 position.offset += 1;
5266 if position.own.contains(&item.id) {
5267 if position.seen.contains(&item.id) {
5268 continue;
5269 }
5270 position.seen.push(item.id.clone());
5271 // The search's own copy of an issue this process only commented on is as
5272 // good as a node read of it, since its comments are read either way.
5273 let only_commented = commented.contains(&item.id)
5274 && !own.iter().any(|written| written.id == item.id);
5275 if !only_commented {
5276 let updated_at = item.updated_at;
5277 let Some(written) = self.search_written(&own, &item.id).await? else {
5278 continue;
5279 };
5280 item = written;
5281 item.updated_at = item.updated_at.max(updated_at);
5282 self.resolved_cache()?.insert(item.id.clone(), item.clone());
5283 }
5284 }
5285 if item.kind == BoardKind::Work(ItemKind::Task) {
5286 let task = item.task()?;
5287 if task_matches(&task, query, &query.project)
5288 && self.commented_since(&item, query.commented_since).await?
5289 {
5290 tasks.push(task);
5291 }
5292 }
5293 }
5294 if position.offset < rows {
5295 continue;
5296 }
5297 position.offset = 0;
5298 position.connection = match next {
5299 Some(after) => SearchConnection::Continuing {
5300 after: Cursor(after),
5301 },
5302 None => SearchConnection::Exhausted {},
5303 };
5304 }
5305 if position.connection.exhausted() {
5306 for id in position.own.clone() {
5307 if position.seen.contains(&id) {
5308 continue;
5309 }
5310 if tasks.len() == limit {
5311 break;
5312 }
5313 position.seen.push(id.clone());
5314 let Some(item) = self.search_written(&own, &id).await? else {
5315 continue;
5316 };
5317 if item.kind == BoardKind::Work(ItemKind::Task) {
5318 let task = item.task()?;
5319 if task_matches(&task, query, &query.project)
5320 && self.commented_since(&item, query.commented_since).await?
5321 {
5322 tasks.push(task);
5323 }
5324 }
5325 }
5326 }
5327 let more = !position.connection.exhausted()
5328 || position.own.iter().any(|id| !position.seen.contains(id));
5329 Ok(Page {
5330 items: tasks,
5331 next: more.then(|| {
5332 Cursor(serde_json::to_string(&position).expect("search position is serializable"))
5333 }),
5334 })
5335 }
5336
5337 /// A resumed process has the ids but no write records; resolve only a record the
5338 /// current page needs, by its uncached node read rather than the lagging search index.
5339 async fn search_written(
5340 &self,
5341 own: &[Resolved],
5342 id: &NativeId,
5343 ) -> Result<Option<Resolved>, SourceError> {
5344 match own.iter().find(|item| item.id == *id) {
5345 Some(item) => Ok(Some(item.clone())),
5346 None => self.item_by_id(id).await,
5347 }
5348 }
5349
5350 /// The candidates for a task query carrying a text, metadata or origin predicate, read
5351 /// without enumerating the board — or `None` for a query carrying none of the three, which
5352 /// keeps the reads it always had.
5353 ///
5354 /// An origin is answered by [`Self::origin_candidates`], whatever else the query carries,
5355 /// because it names at most a handful of items. Text and metadata are answered by one
5356 /// board-scoped issue search carrying every term — see [`narrowing_qualifiers`] — narrowed
5357 /// further by `updated:>=` when the query also asks for comment activity, since both
5358 /// qualifiers must hold of an issue the answer keeps. Every candidate is confirmed in
5359 /// process afterwards by the same predicates [`task_matches`] applies to every read.
5360 ///
5361 /// Completed with what this process wrote, its own record winning over the index's copy
5362 /// of the same item: see [`Self::with_own_writes`].
5363 async fn narrowed(&self, query: &TaskQuery) -> Result<Option<Vec<Resolved>>, SourceError> {
5364 let asked = match (&query.origin, narrowing_qualifiers(query)) {
5365 (Some(origin), _) => Narrowing::Origin(origin.clone()),
5366 (None, Some(qualifiers)) => Narrowing::Search(match query.commented_since {
5367 Some(since) => format!("{} {qualifiers}", updated_qualifier(since)),
5368 None => qualifiers,
5369 }),
5370 (None, None) => return Ok(None),
5371 };
5372 // A question about comment activity is asked afresh every time, as it always was: it
5373 // is the one a caller polls from one source while waiting for the index, and an
5374 // answer held from the first poll would be the answer to every later one.
5375 let key = query.commented_since.is_none().then(|| asked.key());
5376 let cached = match &key {
5377 Some(key) => self.narrowed_cache()?.get(key).cloned(),
5378 None => None,
5379 };
5380 let found = match cached {
5381 Some(found) => found,
5382 None => {
5383 let found = match &asked {
5384 Narrowing::Origin(origin) => self.origin_candidates(origin).await?,
5385 Narrowing::Search(also) => self.searched(also).await?,
5386 };
5387 if let Some(key) = key {
5388 self.narrowed_cache()?.insert(key, found.clone());
5389 }
5390 found
5391 }
5392 };
5393 self.with_own_writes(found).map(Some)
5394 }
5395
5396 /// The candidates for a project or unscoped document query carrying a searchable text,
5397 /// read without enumerating the board — or `None` for a query with no text or a blank one,
5398 /// which keeps the read it always had.
5399 ///
5400 /// The text is sent as the very phrase a task query's text is — see [`text_qualifiers`] —
5401 /// in one board-scoped issue search walked to its end at [`SEARCH_PAGE_SIZE`], so what it
5402 /// costs is the issues that match and never the board. Its answer is held for the command
5403 /// under the same key [`Self::narrowed`] holds that search under, so a walk of the caller's
5404 /// pages asks GitHub once. Every candidate is confirmed afterwards by its kind and by the
5405 /// substring rule, exactly as an item of the wider read was, and is completed with what this
5406 /// process wrote: see [`Self::with_own_writes`].
5407 async fn text_searched(
5408 &self,
5409 text: Option<&TextQuery>,
5410 ) -> Result<Option<Vec<Resolved>>, SourceError> {
5411 let Some(also) = text_qualifiers(text) else {
5412 return Ok(None);
5413 };
5414 let key = Narrowing::Search(also.clone()).key();
5415 let cached = self.narrowed_cache()?.get(&key).cloned();
5416 let found = match cached {
5417 Some(found) => found,
5418 None => {
5419 let found = self.searched(&also).await?;
5420 self.narrowed_cache()?.insert(key, found.clone());
5421 found
5422 }
5423 };
5424 self.with_own_writes(found).map(Some)
5425 }
5426
5427 /// Every item of this board that may carry `origin` — a superset of those that do — found
5428 /// by [`graphql::ORIGIN_LOOKUP`] and never by enumerating the board.
5429 ///
5430 /// The union of the board's own field filter over the `onetaskgraph.origin` text field —
5431 /// which reads the field every carrier holds, whichever release wrote it — and the
5432 /// board-scoped issue search for the same id as a phrase in the body, where this source
5433 /// mirrors it. The caller adds the third read, this process's own writes. Candidates are
5434 /// returned unconfirmed; [`task_matches`] compares each one's own origin field with the
5435 /// query's, exactly.
5436 ///
5437 /// Both connections are walked to exhaustion, each from its own cursor. One that has
5438 /// already ended is sent its last cursor again, which answers an empty page, so the one
5439 /// document serves every page of either. What the two leave is stated in the module
5440 /// documentation: a carrier another process added within the last second or two, before
5441 /// either index has it.
5442 async fn origin_candidates(&self, origin: &str) -> Result<Vec<Resolved>, SourceError> {
5443 let filter = format!("{ORIGIN_FIELD}:{}", quoted(origin));
5444 let search = self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))));
5445 let mut items_after: Option<String> = None;
5446 let mut search_after: Option<String> = None;
5447 let mut found: Vec<Resolved> = Vec::new();
5448 let keep = |resolved: Resolved, found: &mut Vec<Resolved>| {
5449 if !found.iter().any(|held| held.id == resolved.id) {
5450 found.push(resolved);
5451 }
5452 };
5453 loop {
5454 let data = self
5455 .graphql(
5456 graphql::ORIGIN_LOOKUP,
5457 json!({"owner":self.owner,"number":self.project_number,"filter":filter,
5458 "search":search,"type":"ISSUE","originFirst":ORIGIN_PAGE_SIZE,
5459 "itemsAfter":items_after,"searchAfter":search_after,
5460 "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
5461 "duplicates":true}),
5462 )
5463 .await?;
5464 let items = data
5465 .pointer("/originItems/projectV2/items")
5466 .filter(|value| !value.is_null())
5467 .ok_or_else(|| SourceError::Refused {
5468 message: format!(
5469 "GitHub project {}/{} was not found or is not visible to the token",
5470 self.owner, self.project_number
5471 ),
5472 })?;
5473 for item in optional_nodes(Some(items), "project items")?
5474 .into_iter()
5475 .flatten()
5476 {
5477 // The board's own items list its drafts too, and a draft is not an issue: no
5478 // narrowed read answers with one, whatever its origin field holds.
5479 if let Some(resolved) = self.resolve(item)?
5480 && resolved.content_kind == ContentKind::Issue
5481 {
5482 keep(resolved, &mut found);
5483 }
5484 }
5485 let searched = data.get("search").ok_or_else(|| SourceError::Malformed {
5486 message: "GitHub search response has no search connection".into(),
5487 })?;
5488 for node in optional_nodes(Some(searched), "search")?
5489 .into_iter()
5490 .flatten()
5491 {
5492 if let Some(resolved) = self.resolve_issue(node).await? {
5493 keep(resolved, &mut found);
5494 }
5495 }
5496 let items_next = resumed(items, items_after.as_deref())?;
5497 let search_next = resumed(searched, search_after.as_deref())?;
5498 if !items_next.has_more() && !search_next.has_more() {
5499 return Ok(found);
5500 }
5501 items_after = items_next.cursor();
5502 search_after = search_next.cursor();
5503 }
5504 }
5505
5506 /// `found`, with every item this process created or wrote in its place, and every one of
5507 /// them the read did not report added.
5508 ///
5509 /// This process's own record wins over the read's copy of the same item, because a read
5510 /// of an item written moments ago can still be behind what was written onto it — the
5511 /// origin field included, which is the one a narrowed read is confirmed against — and a
5512 /// read that still names an item under a predicate this process's write moved it out of
5513 /// must not return it. The one thing the read knows that the record cannot is when GitHub
5514 /// last saw the item change, which is what a comment-activity read rules a candidate out
5515 /// by, so the read's `updatedAt` is kept when the record has none of its own. See
5516 /// [`Self::created`] and [`Self::updated`](GitHubProjectsSource::updated).
5517 fn with_own_writes(&self, mut found: Vec<Resolved>) -> Result<Vec<Resolved>, SourceError> {
5518 // A board draft is not an issue, so no narrowed read returns one, and this process
5519 // having written one does not make it an answer either.
5520 let own: Vec<Resolved> = self
5521 .created()?
5522 .iter()
5523 .chain(self.updated()?.iter())
5524 .filter(|own| own.content_kind == ContentKind::Issue)
5525 .cloned()
5526 .collect();
5527 for mut own in own {
5528 self.resolved_cache()?.insert(own.id.clone(), own.clone());
5529 match found.iter_mut().find(|read| read.id == own.id) {
5530 Some(read) => {
5531 own.updated_at = own.updated_at.max(read.updated_at);
5532 *read = own;
5533 }
5534 None => found.push(own),
5535 }
5536 }
5537 Ok(found)
5538 }
5539
5540 /// Whether `item` has a comment created or last edited at or after `since` — always, when
5541 /// there is no instant to hold it to.
5542 ///
5543 /// The candidate's own `updatedAt` is read first, because a comment written or edited at
5544 /// or after the instant moved it there: an issue not updated since holds no such comment,
5545 /// and its comments are never asked for — unless this process commented on it in this
5546 /// command, when the `updatedAt` held may predate that comment; see [`Self::commented`]. Otherwise its comments are walked, oldest first,
5547 /// only as far as the first that matches. A board draft is not an issue and has no
5548 /// comments, so it never matches.
5549 async fn commented_since(
5550 &self,
5551 item: &Resolved,
5552 since: Option<DateTime<Utc>>,
5553 ) -> Result<bool, SourceError> {
5554 let Some(since) = since else {
5555 return Ok(true);
5556 };
5557 if item.content_kind == ContentKind::DraftIssue {
5558 return Ok(false);
5559 }
5560 // An `updatedAt` this process's own record or a lagging index holds can predate a
5561 // comment this process wrote since, so only an issue it did not comment on is ruled
5562 // out by one.
5563 if item.updated_at.is_some_and(|updated| updated < since)
5564 && !self.commented()?.contains(&item.id)
5565 {
5566 return Ok(false);
5567 }
5568 let query = TaskQuery {
5569 commented_since: Some(since),
5570 ..TaskQuery::default()
5571 };
5572 let mut after: Option<String> = None;
5573 loop {
5574 let data = self
5575 .graphql(
5576 graphql::ISSUE_COMMENTS,
5577 json!({"id":item.id.0,"first":MAX_PAGE_SIZE,"after":after}),
5578 )
5579 .await?;
5580 let Some(connection) = data
5581 .get("node")
5582 .filter(|value| !value.is_null())
5583 .and_then(|node| node.get("comments"))
5584 .filter(|value| !value.is_null())
5585 else {
5586 // Removed since the search reported it: no longer an issue with comments.
5587 return Ok(false);
5588 };
5589 let comments = optional_nodes(Some(connection), "issue comments")?
5590 .into_iter()
5591 .flatten()
5592 .map(comment_from)
5593 .collect::<Result<Vec<_>, _>>()?;
5594 if query.comments_match(&comments) {
5595 return Ok(true);
5596 }
5597 match next_cursor(connection)? {
5598 Some(next) => {
5599 validate_cursor_progress(after.as_deref(), &next.0)?;
5600 after = Some(next.0);
5601 }
5602 None => return Ok(false),
5603 }
5604 }
5605 }
5606
5607 /// Every item on the board: the union of both enumerations GitHub offers of one.
5608 ///
5609 /// Neither contains the other, so neither is dropped — only `ProjectV2.items` lists a
5610 /// board **draft** and reads the board's own fields beside its items, and only the search
5611 /// reports an item that connection is behind on. The module documentation is where the lag and the
5612 /// measurements behind it are written down.
5613 ///
5614 /// A search result is admitted on the same terms as any other issue this source reaches
5615 /// directly — [`Self::resolve_issue`] keeps it only if that issue's own `projectItems`
5616 /// names *this* board — so an issue the index still believes is here after it was taken
5617 /// off is refused rather than reported.
5618 ///
5619 /// See [`Self::board_cache`]. Both completions happen on every call rather than once,
5620 /// which is what the cache could otherwise have broken.
5621 async fn board(&self) -> Result<Board, SourceError> {
5622 let cached = self.board_cache()?.clone();
5623 let mut board = match cached {
5624 Some(board) => board,
5625 None => {
5626 let read = self.read_board().await?;
5627 *self.board_cache()? = Some(read.clone());
5628 read
5629 }
5630 };
5631 for held in self.searched_issues().await? {
5632 if !board.items.iter().any(|item| item.id == held.id) {
5633 board.items.push(held);
5634 }
5635 }
5636 for own in self.created()?.iter() {
5637 if !board.items.iter().any(|item| item.id == own.id) {
5638 board.items.push(own.clone());
5639 }
5640 }
5641 Ok(board)
5642 }
5643
5644 /// This process's own view of the board, or the refusal a poisoned lock is.
5645 fn board_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<Board>>, SourceError> {
5646 self.board_cache
5647 .lock()
5648 .map_err(|_| SourceError::Unavailable {
5649 message: "this source's view of the board was left inconsistent by an earlier \
5650 failure; next: run the command again"
5651 .into(),
5652 })
5653 }
5654
5655 /// Bring this process's own view of the board up to an item it has just written.
5656 ///
5657 /// A created item goes to `created`, which is what completes a board read GitHub's own
5658 /// eventual consistency has left behind. An item that was already there is replaced
5659 /// where it sits, so a second write of it in the same command reads its real parent
5660 /// rather than the one it had before the first write.
5661 ///
5662 /// "Where it sits" is three places, and missing an earlier one leaves a stale record
5663 /// that wins: an item this same run created is held in `created` and not in the cached
5664 /// board, and `board` completes the cached board *from* `created`, so replacing only
5665 /// the cached copy of such an item replaces nothing and the read still reports the
5666 /// title it was created with. The search is the third, and it is the one an item the
5667 /// board's own projection is behind on sits in *alone* — which is exactly the item this
5668 /// source is least able to re-read, so leaving it out would put the stale title back on
5669 /// the only items the completion in [`Self::board`] exists for.
5670 fn remember_written(&self, item: Resolved, created: bool) -> Result<(), SourceError> {
5671 self.resolved_cache()?.insert(item.id.clone(), item.clone());
5672 if created {
5673 self.created()?.push(item);
5674 return Ok(());
5675 }
5676 {
5677 let mut own = self.created()?;
5678 if let Some(held) = own.iter_mut().find(|held| held.id == item.id) {
5679 *held = item;
5680 return Ok(());
5681 }
5682 }
5683 {
5684 let mut own = self.updated()?;
5685 match own.iter_mut().find(|held| held.id == item.id) {
5686 Some(held) => *held = item.clone(),
5687 None => own.push(item.clone()),
5688 }
5689 }
5690 if let Some(board) = self.board_cache()?.as_mut()
5691 && let Some(held) = board.items.iter_mut().find(|held| held.id == item.id)
5692 {
5693 *held = item.clone();
5694 }
5695 if let Some(found) = self.search_cache()?.as_mut()
5696 && let Some(held) = found.iter_mut().find(|held| held.id == item.id)
5697 {
5698 *held = item.clone();
5699 }
5700 for found in self.narrowed_cache()?.values_mut() {
5701 if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
5702 *held = item.clone();
5703 }
5704 }
5705 for (_, found) in self.children_cache()?.values_mut() {
5706 if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
5707 *held = item.clone();
5708 }
5709 }
5710 Ok(())
5711 }
5712
5713 /// Forget one item this process has just deleted, from every half of its own view.
5714 fn forget(&self, id: &NativeId) -> Result<(), SourceError> {
5715 self.resolved_cache()?.remove(id);
5716 self.created()?.retain(|own| own.id != *id);
5717 self.updated()?.retain(|own| own.id != *id);
5718 self.commented()?.retain(|own| own != id);
5719 if let Some(board) = self.board_cache()?.as_mut() {
5720 board.items.retain(|item| item.id != *id);
5721 }
5722 if let Some(found) = self.search_cache()?.as_mut() {
5723 found.retain(|item| item.id != *id);
5724 }
5725 for found in self.narrowed_cache()?.values_mut() {
5726 found.retain(|item| item.id != *id);
5727 }
5728 for (_, found) in self.children_cache()?.values_mut() {
5729 found.retain(|item| item.id != *id);
5730 }
5731 Ok(())
5732 }
5733
5734 /// This process's own record of each narrowed answer, or the refusal a poisoned lock is.
5735 fn narrowed_cache(
5736 &self,
5737 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<String, Vec<Resolved>>>, SourceError> {
5738 self.narrowed_cache
5739 .lock()
5740 .map_err(|_| SourceError::Unavailable {
5741 message: "this source's view of a narrowed read was left inconsistent by an \
5742 earlier failure; next: run the command again"
5743 .into(),
5744 })
5745 }
5746
5747 /// This process's own record of each project's sub-issues, or the refusal a poisoned lock
5748 /// is.
5749 fn children_cache(&self) -> Result<std::sync::MutexGuard<'_, ProjectChildren>, SourceError> {
5750 self.children_cache
5751 .lock()
5752 .map_err(|_| SourceError::Unavailable {
5753 message: "this source's view of a project's tasks was left inconsistent by an \
5754 earlier failure; next: run the command again"
5755 .into(),
5756 })
5757 }
5758
5759 /// Every page of the board, read from GitHub.
5760 async fn read_board(&self) -> Result<Board, SourceError> {
5761 let mut after: Option<String> = None;
5762 let mut items = Vec::new();
5763 let mut board;
5764 loop {
5765 let page = self.board_page(after.as_deref(), MAX_PAGE_SIZE).await?;
5766 for item in page
5767 .pointer("/items/nodes")
5768 .and_then(Value::as_array)
5769 .ok_or_else(|| SourceError::Malformed {
5770 message: "GitHub project items.nodes is not an array".into(),
5771 })?
5772 {
5773 if let Some(resolved) = self.resolve(item)? {
5774 items.push(resolved);
5775 }
5776 }
5777 let info = page
5778 .pointer("/items/pageInfo")
5779 .ok_or_else(|| SourceError::Malformed {
5780 message: "GitHub project items have no pageInfo".into(),
5781 })?;
5782 let has_next = required_bool(info, "hasNextPage")?;
5783 let next = has_next
5784 .then(|| required_str(info, "endCursor"))
5785 .transpose()?;
5786 board = page.clone();
5787 match next {
5788 Some(next) => {
5789 validate_cursor_progress(after.as_deref(), next)?;
5790 after = Some(next.to_owned());
5791 }
5792 None => break,
5793 }
5794 }
5795 Ok(Board {
5796 id: required_str(&board, "id")?.to_owned(),
5797 fields: board.get("fields").cloned().unwrap_or(Value::Null),
5798 items,
5799 })
5800 }
5801
5802 /// The existing items this source has written, for completing a narrowed read that is
5803 /// behind; see [`Self::updated`](GitHubProjectsSource::updated).
5804 fn updated(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
5805 self.updated.lock().map_err(|_| SourceError::Unavailable {
5806 message: "this source's record of what it wrote in this run was left inconsistent \
5807 by an earlier failure; next: run the command again"
5808 .into(),
5809 })
5810 }
5811
5812 /// The issues this source has commented on in this command; see
5813 /// [`Self::commented`](GitHubProjectsSource::commented).
5814 fn commented(&self) -> Result<std::sync::MutexGuard<'_, Vec<NativeId>>, SourceError> {
5815 self.commented.lock().map_err(|_| SourceError::Unavailable {
5816 message: "this source's record of what it commented on in this run was left \
5817 inconsistent by an earlier failure; next: run the command again"
5818 .into(),
5819 })
5820 }
5821
5822 /// Called only once GitHub has answered the comment write, so an issue whose comment
5823 /// failed is never made a candidate a later read would pay a node read for.
5824 fn remember_commented(&self, issue: &NativeId) -> Result<(), SourceError> {
5825 let mut commented = self.commented()?;
5826 if !commented.contains(issue) {
5827 commented.push(issue.clone());
5828 }
5829 Ok(())
5830 }
5831
5832 /// The items this source has created, for completing a board read that is behind.
5833 fn created(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
5834 self.created.lock().map_err(|_| SourceError::Unavailable {
5835 message: "this source's record of what it created in this run was left \
5836 inconsistent by an earlier failure; next: run the command again"
5837 .into(),
5838 })
5839 }
5840
5841 /// One board item as this source reports it, or `None` for content it ignores.
5842 ///
5843 /// A pull request is neither a project nor a task — it is somebody's change, not a
5844 /// unit of plan — and an item whose content the token cannot see has nothing to
5845 /// report at all.
5846 fn resolve(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
5847 let content = item.get("content").ok_or_else(|| SourceError::Malformed {
5848 message: "GitHub project item is missing content".into(),
5849 })?;
5850 if content.is_null() {
5851 return Ok(None);
5852 }
5853 let content_kind = match required_str(content, "__typename")? {
5854 "Issue" => ContentKind::Issue,
5855 "DraftIssue" => ContentKind::DraftIssue,
5856 _ => return Ok(None),
5857 };
5858 let field_values = item
5859 .get("fieldValues")
5860 .ok_or_else(|| SourceError::Malformed {
5861 message: "GitHub project item is missing fieldValues".into(),
5862 })?;
5863 complete_connection(field_values, "project item field values", NESTED_PAGE_SIZE)?;
5864 let nodes = field_values
5865 .get("nodes")
5866 .and_then(Value::as_array)
5867 .ok_or_else(|| SourceError::Malformed {
5868 message: "GitHub project item fieldValues.nodes is not an array".into(),
5869 })?;
5870 if let Some(labels) = content.get("labels") {
5871 complete_connection(labels, "content labels", NESTED_PAGE_SIZE)?;
5872 }
5873 let raw_body = optional_str(content, "body")?.map(str::to_owned);
5874 let (body, slot) = metadata_body(raw_body.clone())?;
5875 let parent = optional_str(content.get("parent").unwrap_or(&Value::Null), "id")?
5876 .map(|id| NativeId(id.to_owned()));
5877 // A draft has no sub-issues to summarise, and GitHub's schema gives it no field
5878 // to read one from; it is a task, and never a project.
5879 let sub_issues = match content_kind {
5880 ContentKind::Issue => sub_issue_total(content)?,
5881 ContentKind::DraftIssue => 0,
5882 };
5883 let content_id = required_str(content, "id")?;
5884 let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
5885 message: format!("GitHub issue {content_id}: {message}"),
5886 })?;
5887 let raw_title = required_str(content, "title")?;
5888 // The design prefix is read *first*, before either of the two rules that separate
5889 // a project from a task. A document is not work whatever sub-issues it has and
5890 // whatever marker it carries, and reading the prefix later would make a design
5891 // issue with none of either an empty project.
5892 let kind = if raw_title.starts_with(DESIGN_TITLE_PREFIX) {
5893 BoardKind::Document
5894 } else if parent.is_some() {
5895 // Being a sub-issue wins outright, and no marker overrides it: an issue filed
5896 // under a project is that project's task even when it has sub-issues of its
5897 // own.
5898 BoardKind::Work(ItemKind::Task)
5899 } else if sub_issues > 0 || marked == Some(ItemKind::Project) {
5900 BoardKind::Work(ItemKind::Project)
5901 } else {
5902 BoardKind::Work(ItemKind::Task)
5903 };
5904 // The title a person wrote, which for a document is the one without the prefix —
5905 // the same way `content` above is the body without this source's metadata slot.
5906 let title = match kind {
5907 BoardKind::Document => raw_title[DESIGN_TITLE_PREFIX.len()..].to_owned(),
5908 BoardKind::Work(_) => raw_title.to_owned(),
5909 };
5910 let own_repository = content
5911 .pointer("/repository/nameWithOwner")
5912 .and_then(Value::as_str)
5913 .map(|origin| Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST)))
5914 .transpose()
5915 .map_err(|message| SourceError::Malformed { message })?;
5916 let repositories = if slot.contains_key(Repository::METADATA_KEY) {
5917 Repository::from_metadata(&slot)
5918 .map_err(|message| SourceError::Malformed { message })?
5919 } else {
5920 own_repository.clone().into_iter().collect()
5921 };
5922 let classification = Classification::from_metadata(&slot)
5923 .map_err(|message| SourceError::Malformed { message })?;
5924 let id = NativeId(content_id.to_owned());
5925 // Read only for a task, because only a task has either list: a project or a
5926 // document holding one of these keys holds nothing this source reports, and the
5927 // keys are left out of its caller-visible metadata all the same.
5928 let (delivers, delivered_by) = if kind == BoardKind::Work(ItemKind::Task) {
5929 let listed = |key: &str| {
5930 TaskRef::from_value(key, &id, Some(&self.name), slot.get(key))
5931 .map_err(|message| SourceError::Malformed { message })
5932 };
5933 (
5934 listed(TaskRef::DELIVERS_KEY)?,
5935 listed(TaskRef::DELIVERED_BY_KEY)?,
5936 )
5937 } else {
5938 (Vec::new(), Vec::new())
5939 };
5940 let (option, closed, reason) = Self::status_parts(nodes, content)?;
5941 let priority = self.held_priority(nodes)?;
5942 // Present when the item was reached through its own issue, whose board entry
5943 // names the board; a read of the board's own items has the board already. An
5944 // empty id names nothing a field write could address, so it is read as absent and
5945 // the write goes back to reading the board.
5946 let board_id = item
5947 .pointer("/project/id")
5948 .and_then(Value::as_str)
5949 .filter(|id| !id.is_empty());
5950 let resolved = Resolved {
5951 item_id: required_str(item, "id")?.to_owned(),
5952 id,
5953 content_kind,
5954 kind,
5955 title,
5956 body: body.filter(|value| !value.is_empty()),
5957 raw_body,
5958 status: self
5959 .statuses
5960 .status(kind.status_kind(), option, closed, reason),
5961 option: option.map(str::to_owned),
5962 priority,
5963 closed,
5964 delivers,
5965 delivered_by,
5966 labels: labels(content)?,
5967 parent,
5968 origin: text_field(nodes, ORIGIN_FIELD)?.filter(|value| !value.is_empty()),
5969 projected: self.held_projections(nodes)?,
5970 number: match content_kind {
5971 ContentKind::Issue => Some(issue_number(content)?),
5972 // A draft is filed in no repository, so nothing ever numbered it:
5973 // `DraftIssue` declares no `number` at all, exactly as it declares no
5974 // `subIssuesSummary` the branch above reads.
5975 ContentKind::DraftIssue => None,
5976 },
5977 url: optional_str(content, "url")?.map(str::to_owned),
5978 created_at: optional_time(content, "createdAt")?,
5979 updated_at: optional_time(content, "updatedAt")?,
5980 own_repository,
5981 repositories,
5982 classification,
5983 slot,
5984 board_id: board_id.map(str::to_owned),
5985 fields: field_definitions(nodes),
5986 board_fields: Self::carried_board_fields(content, board_id)?,
5987 blocked_by: carried_blocked_by(content)?,
5988 };
5989 self.resolved_cache()?
5990 .insert(resolved.id.clone(), resolved.clone());
5991 Ok(Some(resolved))
5992 }
5993
5994 /// The field definitions of the board `board_id` names — the project this issue's own
5995 /// board item is on — off the `boards` page a read of an issue by its own id carries, or
5996 /// `None` when the read carried none, carried no entry for that board, or the board item
5997 /// named no board, which a write then answers by reading the board's fields itself.
5998 ///
5999 /// Matched by the board's node id and never by its number alone: a project number is
6000 /// unique only within its owner, so another owner's board numbered alike can sit on the
6001 /// same page, and its field and option ids address nothing on this one.
6002 fn carried_board_fields(
6003 content: &Value,
6004 board_id: Option<&str>,
6005 ) -> Result<Option<Value>, SourceError> {
6006 let (Some(nodes), Some(board_id)) = (
6007 content.pointer("/boards/nodes").and_then(Value::as_array),
6008 board_id,
6009 ) else {
6010 return Ok(None);
6011 };
6012 let Some(board) = nodes.iter().find_map(|node| {
6013 let project = node.get("project")?;
6014 (project.get("id").and_then(Value::as_str) == Some(board_id)).then_some(project)
6015 }) else {
6016 return Ok(None);
6017 };
6018 let Some(fields) = board.get("fields").filter(|fields| !fields.is_null()) else {
6019 return Ok(None);
6020 };
6021 complete_connection(fields, "board fields", NESTED_PAGE_SIZE)?;
6022 Ok(Some(fields.clone()))
6023 }
6024
6025 /// What one board item's `Priority` field says, through this instance's mapping.
6026 ///
6027 /// An instance with no mapping holds no priority, so every item reads as `none` whatever
6028 /// its board holds. With one, no value is `none`, a mapped option is its level, and an
6029 /// option the mapping does not name is kept as itself — never read as a level or as
6030 /// `none` — for a read of the task to report by name.
6031 fn held_priority(&self, field_values: &[Value]) -> Result<HeldPriority, SourceError> {
6032 let Some(mapping) = &self.priorities else {
6033 return Ok(HeldPriority::Read(Priority::None));
6034 };
6035 // A value of the field that names no option — a text field someone called `Priority` —
6036 // is malformed rather than `none`: reading it as no priority would let the next copy
6037 // clear one a person set.
6038 let Some(option) = field_values
6039 .iter()
6040 .find(|value| {
6041 value.pointer("/field/name").and_then(Value::as_str) == Some(PRIORITY_FIELD)
6042 })
6043 .map(|value| required_str(value, "name"))
6044 .transpose()?
6045 else {
6046 return Ok(HeldPriority::Read(Priority::None));
6047 };
6048 Ok(mapping.priority_of(option).map_or_else(
6049 || HeldPriority::Unmapped(option.to_owned()),
6050 HeldPriority::Read,
6051 ))
6052 }
6053
6054 /// What each projected metadata field holds for one board item, by field name — every text
6055 /// value GitHub answers, an empty one included.
6056 fn held_projections(
6057 &self,
6058 field_values: &[Value],
6059 ) -> Result<BTreeMap<String, String>, SourceError> {
6060 let mut held = BTreeMap::new();
6061 for projection in &self.metadata_fields {
6062 if let Some(text) = text_field(field_values, &projection.field)? {
6063 held.insert(projection.field.to_string(), text);
6064 }
6065 }
6066 Ok(held)
6067 }
6068
6069 /// What one board item's status is read from: its `Status` option, whether its issue
6070 /// is closed, and the reason it was closed with. [`BoardStatuses::status`] turns the
6071 /// three into the status it reports.
6072 fn status_parts<'a>(
6073 field_values: &'a [Value],
6074 content: &'a Value,
6075 ) -> Result<(Option<&'a str>, bool, Option<&'a str>), SourceError> {
6076 let option = field_values
6077 .iter()
6078 .find(|value| value.pointer("/field/name").and_then(Value::as_str) == Some("Status"))
6079 .map(|value| required_str(value, "name"))
6080 .transpose()?;
6081 let closed = optional_str(content, "state")? == Some("CLOSED");
6082 Ok((option, closed, optional_str(content, "stateReason")?))
6083 }
6084
6085 /// The board Status option this write selects, or the refusal that says why not.
6086 ///
6087 /// The mapped option is required for both open and terminal targets. A terminal write
6088 /// validates it before changing either representation, so it can never fall back to
6089 /// closing an issue whose board cannot display the matching status.
6090 ///
6091 /// Answers the field's id, the option's id, and the option's name as the board spells
6092 /// it — which is the name a read of the item reports once it sits there.
6093 fn column_for(
6094 &self,
6095 fields: &Value,
6096 kind: ItemKind,
6097 category: StatusCategory,
6098 target: &StatusTarget,
6099 ) -> Result<Option<(String, String, String)>, SourceError> {
6100 let Some(wanted) = target.option() else {
6101 return Ok(None);
6102 };
6103 let missing = |detail: &str| SourceError::Refused {
6104 message: format!(
6105 "{} status {} of source {} needs the board Status option {wanted:?}, and \
6106 {detail}; next: add that option to the board, which `onetaskgraph sources \
6107 fields {} --apply` does, or point status_mapping.{}.{} of this source at one \
6108 it has",
6109 kind.marker(),
6110 category_name(category),
6111 self.name,
6112 self.name,
6113 category_name(category),
6114 kind.marker()
6115 ),
6116 };
6117 let Some(field) = Board::field(fields, "Status")? else {
6118 return Err(missing("this board has no Status field"));
6119 };
6120 if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
6121 return Err(missing(
6122 "this board's Status field is not a single-select field",
6123 ));
6124 }
6125 let option = field
6126 .get("options")
6127 .and_then(Value::as_array)
6128 .and_then(|options| {
6129 options.iter().find(|option| {
6130 option
6131 .get("name")
6132 .and_then(Value::as_str)
6133 .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
6134 })
6135 });
6136 match option {
6137 None => Err(missing("this board does not have it")),
6138 Some(option) => Ok(Some((
6139 required_str(field, "id")?.to_owned(),
6140 required_str(option, "id")?.to_owned(),
6141 required_str(option, "name")?.to_owned(),
6142 ))),
6143 }
6144 }
6145
6146 /// The refusal a status that closes an issue is answered with over a board draft.
6147 fn closes_a_draft(&self, category: StatusCategory) -> SourceError {
6148 SourceError::Refused {
6149 message: format!(
6150 "status {} of source {} closes the item's issue, and GitHub draft items have \
6151 no open or closed state",
6152 category_name(category),
6153 self.name
6154 ),
6155 }
6156 }
6157
6158 /// What a status write to one item needs of the board: the board's id and the
6159 /// definition of its `Status` field, read off the item when the item says both.
6160 ///
6161 /// The same reasoning as [`Self::fields_for`]: a node read of the item names its board,
6162 /// and its `Status` value carries that field's definition, options and all. An item that
6163 /// does not say — no board id, or no `Status` value to read the field off — takes them
6164 /// from [`Self::board_fields`], which reads no item.
6165 async fn status_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
6166 if let Some(board) = item.carried_board() {
6167 return Ok(board);
6168 }
6169 if item.defines("Status")
6170 && let Some(board_id) = item.named_board()
6171 {
6172 return Ok(BoardFields {
6173 id: board_id,
6174 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
6175 });
6176 }
6177 self.board_fields().await
6178 }
6179
6180 /// Set one task's status and nothing else; see [`TaskSource::set_task_status`].
6181 async fn set_status(
6182 &self,
6183 id: &NativeId,
6184 category: StatusCategory,
6185 ) -> Result<Option<Status>, SourceError> {
6186 // Refused before anything is read, in the words a write of the same status is.
6187 let target = self.resolved_target(ItemKind::Task, category)?;
6188 let Some(mut item) = self
6189 .bound_item(id)
6190 .await?
6191 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
6192 else {
6193 return Ok(None);
6194 };
6195 let board = self.status_board(&item).await?;
6196 let (field, option, name) = self
6197 .column_for(&board.fields, ItemKind::Task, category, &target)?
6198 .ok_or_else(|| SourceError::Malformed {
6199 message: format!(
6200 "status {} of source {} names no board Status option",
6201 category_name(category),
6202 self.name
6203 ),
6204 })?;
6205 if item.status.category == category && item.option.as_deref() == Some(&name) {
6206 return Ok(Some(item.status));
6207 }
6208 match &target {
6209 StatusTarget::Terminal(_, reason) => {
6210 if item.content_kind == ContentKind::DraftIssue {
6211 return Err(self.closes_a_draft(category));
6212 }
6213 self.set_item_field(
6214 board.id.as_str(),
6215 &item.item_id,
6216 &field,
6217 json!({"singleSelectOptionId": option}),
6218 )
6219 .await?;
6220 self.update_content(
6221 ContentKind::Issue,
6222 &item.id,
6223 json!({"stateInput": state_input(Some(&target))}),
6224 )
6225 .await?;
6226 item.closed = true;
6227 item.status =
6228 self.statuses
6229 .status(ItemKind::Task, Some(&name), true, Some(reason.reason()));
6230 item.option = Some(name);
6231 }
6232 StatusTarget::Column(_) => {
6233 // An option is what an open item's status is, so a closed issue is reopened
6234 // first — sitting closed in the column, it would read back as closed. A draft has
6235 // no state to reopen.
6236 if item.content_kind == ContentKind::Issue && item.closed {
6237 self.update_content(
6238 ContentKind::Issue,
6239 &item.id,
6240 json!({"stateInput": state_input(Some(&target))}),
6241 )
6242 .await?;
6243 item.closed = false;
6244 }
6245 self.set_item_field(
6246 board.id.as_str(),
6247 &item.item_id,
6248 &field,
6249 json!({"singleSelectOptionId": option}),
6250 )
6251 .await?;
6252 item.status = self
6253 .statuses
6254 .status(ItemKind::Task, Some(&name), false, None);
6255 item.option = Some(name);
6256 }
6257 StatusTarget::Disabled(_) => {
6258 unreachable!("resolved_target refused a disabled status")
6259 }
6260 }
6261 let status = item.status.clone();
6262 self.remember_written(item, false)?;
6263 Ok(Some(status))
6264 }
6265
6266 /// Replace one task's `delivered_by` and nothing else; see
6267 /// [`TaskSource::set_delivered_by`].
6268 ///
6269 /// One update of the body, which differs from the body GitHub holds only inside the
6270 /// metadata slot — see [`with_slot`]. A body that would not change is not sent at all.
6271 async fn replace_delivered_by(
6272 &self,
6273 id: &NativeId,
6274 delivered_by: &[TaskRef],
6275 ) -> Result<Option<()>, SourceError> {
6276 let entries = TaskRef::listed(
6277 TaskRef::DELIVERED_BY_KEY,
6278 id,
6279 Some(&self.name),
6280 delivered_by.to_vec(),
6281 )
6282 .map_err(|message| SourceError::Refused { message })?;
6283 let Some(mut item) = self
6284 .bound_item(id)
6285 .await?
6286 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
6287 else {
6288 return Ok(None);
6289 };
6290 let mut slot = item.slot.clone();
6291 set_task_list(&mut slot, TaskRef::DELIVERED_BY_KEY, &entries);
6292 self.write_slot(&mut item, &slot).await?;
6293 item.delivered_by = entries;
6294 self.remember_written(item, false)?;
6295 Ok(Some(()))
6296 }
6297
6298 /// Set one caller key of the metadata slot of one issue of `kind`, and nothing else;
6299 /// see [`TaskSource::set_task_metadata`].
6300 ///
6301 /// `None` when this board holds no item by that id, or holds one of another kind. The
6302 /// answer is the item as this source now reads it, so what a caller is told the key
6303 /// holds is what the slot holds.
6304 ///
6305 /// A key already holding the value is answered without a write, compared as JSON rather
6306 /// than as the body's bytes: a slot a person spelled with other whitespace would
6307 /// otherwise be re-encoded, which is a write that changes nothing the caller asked for.
6308 async fn set_slot_key(
6309 &self,
6310 id: &NativeId,
6311 kind: BoardKind,
6312 key: &MetadataKey,
6313 value: &Value,
6314 ) -> Result<Option<Resolved>, SourceError> {
6315 let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
6316 return Ok(None);
6317 };
6318 if item.slot.get(key.as_str()) == Some(value) {
6319 return Ok(Some(item));
6320 }
6321 let mut slot = item.slot.clone();
6322 slot.insert(key.as_str().to_owned(), value.clone());
6323 // A key a board text field projects moves that field first, so the body — the value's
6324 // one authoritative home — is written last, as every write of an existing item is.
6325 if self
6326 .metadata_fields
6327 .iter()
6328 .any(|projection| &*projection.key == key.as_str())
6329 {
6330 let board = self.projection_board(&item).await?;
6331 let (writes, projected) = self.projection_writes(
6332 &board.fields,
6333 Some(&item.projected),
6334 &slot,
6335 Some(&[key.as_str()]),
6336 )?;
6337 let (mut values, mut clears) = (Vec::new(), Vec::new());
6338 for write in &writes {
6339 write.join(&mut values, &mut clears);
6340 }
6341 self.set_item_fields(board.id.as_str(), &item.item_id, &values, &clears)
6342 .await?;
6343 item.projected = projected;
6344 }
6345 self.write_slot(&mut item, &slot).await?;
6346 self.remember_written(item.clone(), false)?;
6347 Ok(Some(item))
6348 }
6349
6350 /// What a projection write to `item` needs of the board, read off the item when it says
6351 /// enough, as [`Self::fields_for`] reads it.
6352 async fn projection_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
6353 if let Some(board) = item.carried_board() {
6354 return Ok(board);
6355 }
6356 if let Some(board_id) = item.named_board()
6357 && self
6358 .metadata_fields
6359 .iter()
6360 .all(|projection| item.defines(&projection.field))
6361 {
6362 return Ok(BoardFields {
6363 id: board_id,
6364 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
6365 });
6366 }
6367 self.board_fields().await
6368 }
6369
6370 /// Put `slot` in one item's metadata slot with a single update of its body, and bring
6371 /// `item` up to what that write left.
6372 ///
6373 /// The body sent differs from the body GitHub holds only inside the slot — see
6374 /// [`with_slot`] — and a body that would not change is not sent at all. It goes through
6375 /// the mutation the item's content takes, so a board draft's body is written with
6376 /// `updateProjectV2DraftIssue` exactly as an issue's is with `updateIssue`.
6377 async fn write_slot(
6378 &self,
6379 item: &mut Resolved,
6380 slot: &BTreeMap<String, Value>,
6381 ) -> Result<(), SourceError> {
6382 let held = item.raw_body.clone().unwrap_or_default();
6383 let body = with_slot(&held, slot)?;
6384 if body != held {
6385 self.update_content(item.content_kind, &item.id, json!({"body": body}))
6386 .await?;
6387 }
6388 let (visible, slot) = metadata_body(Some(body.clone()))?;
6389 item.body = visible.filter(|value| !value.is_empty());
6390 item.raw_body = Some(body);
6391 item.slot = slot;
6392 Ok(())
6393 }
6394
6395 /// This instance's target for a category written to an item of `kind`, refusing one
6396 /// that kind has no option for — before anything is read or written.
6397 ///
6398 /// Nothing here mutates the board's option set to make room for a status. GitHub
6399 /// documents `UpdateProjectV2FieldInput.singleSelectOptions` as *"provided values
6400 /// overwrite existing options"*, so no addition is additive and a mistake destroys the
6401 /// field and every item's status.
6402 fn resolved_target(
6403 &self,
6404 kind: ItemKind,
6405 category: StatusCategory,
6406 ) -> Result<StatusTarget, SourceError> {
6407 let target = self.statuses.target(kind, category).clone();
6408 let StatusTarget::Disabled(why) = target else {
6409 return Ok(target);
6410 };
6411 let refusal = why.refusal(&self.name, category, kind);
6412 // Why there is no shipped default, which is the question a person meeting this
6413 // refusal on a source that never mentioned the category asks.
6414 let shipped_none = match category {
6415 StatusCategory::Draft => Some(
6416 "draft has no shipped default because GitHub draft issues cannot have \
6417 sub-issues, and this source stores a project's tasks as its issue's sub-issues",
6418 ),
6419 StatusCategory::Unknown => Some(
6420 "unknown has no shipped default because this board keeps no open-ended status \
6421 word: every word classified unknown is written to the one board Status option \
6422 status_mapping.unknown names",
6423 ),
6424 _ => None,
6425 };
6426 Err(match (refusal, shipped_none, why) {
6427 (SourceError::Refused { message }, Some(note), UnmappedStatus::Unconfigured) => {
6428 SourceError::Refused {
6429 message: format!("{message}; {note}"),
6430 }
6431 }
6432 (refusal, _, _) => refusal,
6433 })
6434 }
6435
6436 /// What writing `priority` does to one item's `Priority` field on this board, or the
6437 /// refusal naming what the board lacks.
6438 ///
6439 /// `none` is no value, so it clears the field — and asks nothing of an item that holds
6440 /// none already, or of an item not created yet. Every other priority selects the option
6441 /// the mapping names, matched case-insensitively; a board with no `Priority` field, or
6442 /// without that option, is refused rather than given one: reads and writes never create
6443 /// a field or an option.
6444 fn priority_write(
6445 &self,
6446 fields: &Value,
6447 existing: Option<&Resolved>,
6448 priority: Priority,
6449 ) -> Result<Option<PriorityWrite>, SourceError> {
6450 let Some(mapping) = &self.priorities else {
6451 return Err(self.holds_no_priority());
6452 };
6453 let Some(wanted) = mapping.option(priority) else {
6454 if !existing.is_some_and(Resolved::holds_priority) {
6455 return Ok(None);
6456 }
6457 let field =
6458 Board::field(fields, PRIORITY_FIELD)?.ok_or_else(|| SourceError::Malformed {
6459 message: format!(
6460 "an item holding a {PRIORITY_FIELD} value was read without that field"
6461 ),
6462 })?;
6463 return Ok(Some(PriorityWrite::Clear {
6464 field: required_str(field, "id")?.to_owned(),
6465 }));
6466 };
6467 let missing = |detail: &str| SourceError::Refused {
6468 message: format!(
6469 "priority {priority} of source {} needs the board {PRIORITY_FIELD} option \
6470 {wanted:?}, and {detail}; run `onetaskgraph sources fields {} --apply` to add \
6471 it, or point priority_mapping.{priority} of this source at an option the board \
6472 has",
6473 self.name, self.name
6474 ),
6475 };
6476 let Some(field) = Board::field(fields, PRIORITY_FIELD)? else {
6477 return Err(missing(&format!(
6478 "this board has no {PRIORITY_FIELD} field"
6479 )));
6480 };
6481 if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
6482 return Err(missing(&format!(
6483 "this board's {PRIORITY_FIELD} field is not a single-select field"
6484 )));
6485 }
6486 // An options list that is absent or not a list is an answer this source cannot read,
6487 // not a board lacking the option: `sources fields --apply` is no remedy for it.
6488 let option = field
6489 .get("options")
6490 .and_then(Value::as_array)
6491 .ok_or_else(|| SourceError::Malformed {
6492 message: format!("GitHub {PRIORITY_FIELD} field options is not an array"),
6493 })?
6494 .iter()
6495 .find(|option| {
6496 option
6497 .get("name")
6498 .and_then(Value::as_str)
6499 .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
6500 })
6501 .ok_or_else(|| missing("this board does not have it"))?;
6502 Ok(Some(PriorityWrite::Select {
6503 field: required_str(field, "id")?.to_owned(),
6504 option: required_str(option, "id")?.to_owned(),
6505 }))
6506 }
6507
6508 /// What writing `metadata` does to each projected text field of one item, and what those
6509 /// fields hold after it — or the refusal, before anything is sent.
6510 ///
6511 /// `held` is what the item's fields hold now, `None` for an item not created yet. `keys`
6512 /// narrows the work to the entries projecting one of those metadata keys, for a write that
6513 /// touches only them; `None` is every entry. A string the field already holds, and nothing
6514 /// for a field holding nothing, sends nothing. A value no text field can hold is refused,
6515 /// and so is a board without the field, or with a field of that name that is not a text
6516 /// field: reads and writes never create a field.
6517 fn projection_writes(
6518 &self,
6519 fields: &Value,
6520 held: Option<&BTreeMap<String, String>>,
6521 metadata: &BTreeMap<String, Value>,
6522 keys: Option<&[&str]>,
6523 ) -> Result<(Vec<ProjectionWrite>, BTreeMap<String, String>), SourceError> {
6524 let mut projected = held.cloned().unwrap_or_default();
6525 let mut writes = Vec::new();
6526 for projection in self
6527 .metadata_fields
6528 .iter()
6529 .filter(|projection| keys.is_none_or(|keys| keys.contains(&&*projection.key)))
6530 {
6531 let wanted = projection.projected(metadata, &self.name)?;
6532 let field = self.projection_field(fields, projection)?;
6533 let holding = projected.get(&*projection.field);
6534 match wanted {
6535 Projected::Text(text) if holding != Some(&text) => {
6536 writes.push(ProjectionWrite::Set {
6537 field,
6538 text: text.clone(),
6539 });
6540 projected.insert(projection.field.to_string(), text);
6541 }
6542 Projected::Nothing if holding.is_some() => {
6543 writes.push(ProjectionWrite::Clear { field });
6544 projected.remove(&*projection.field);
6545 }
6546 Projected::Text(_) | Projected::Nothing => {}
6547 }
6548 }
6549 Ok((writes, projected))
6550 }
6551
6552 /// The id of the board text field one projection writes, or the refusal naming what the
6553 /// board has instead.
6554 fn projection_field(
6555 &self,
6556 fields: &Value,
6557 projection: &MetadataField,
6558 ) -> Result<String, SourceError> {
6559 let refuse = |detail: String| SourceError::Refused {
6560 message: format!(
6561 "source {} projects metadata key {:?} onto the board text field {:?}, and \
6562 {detail}; next: run `onetaskgraph sources fields {} --apply` to create it as a \
6563 text field, or name another field in metadata_fields",
6564 self.name, &*projection.key, &*projection.field, self.name
6565 ),
6566 };
6567 let Some(field) = Board::field(fields, &projection.field)? else {
6568 return Err(refuse("this board has no field of that name".to_owned()));
6569 };
6570 let typename = optional_str(field, "__typename")?.unwrap_or("field of another type");
6571 let data_type = optional_str(field, "dataType")?;
6572 if typename != "ProjectV2Field" || data_type != Some("TEXT") {
6573 return Err(refuse(format!(
6574 "this board's field of that name is not a text field (it is a {}), so it is \
6575 left as it is; rename or remove that field first",
6576 data_type.unwrap_or(typename)
6577 )));
6578 }
6579 Ok(required_str(field, "id")?.to_owned())
6580 }
6581
6582 /// Apply one priority write to one board item.
6583 async fn write_priority(
6584 &self,
6585 board_id: &str,
6586 item_id: &str,
6587 write: &PriorityWrite,
6588 ) -> Result<(), SourceError> {
6589 match write {
6590 PriorityWrite::Select { field, option } => {
6591 self.set_item_field(
6592 board_id,
6593 item_id,
6594 field,
6595 json!({"singleSelectOptionId": option}),
6596 )
6597 .await
6598 }
6599 PriorityWrite::Clear { field } => {
6600 let data = self
6601 .graphql(
6602 graphql::CLEAR_FIELD,
6603 json!({"input":{"projectId":board_id,"itemId":item_id,"fieldId":field},
6604 "readPriority":false,"priorityName":PRIORITY_FIELD}),
6605 )
6606 .await?;
6607 let returned = data
6608 .pointer("/clearProjectV2ItemFieldValue/projectV2Item")
6609 .ok_or_else(|| SourceError::Malformed {
6610 message: "GitHub field clear returned no project item".into(),
6611 })?;
6612 if required_str(returned, "id")? != item_id {
6613 return Err(SourceError::Malformed {
6614 message: "GitHub field clear returned the wrong project item".into(),
6615 });
6616 }
6617 Ok(())
6618 }
6619 }
6620 }
6621
6622 /// The refusal a priority is answered with by an instance configured with no
6623 /// `priority_mapping`, which holds none.
6624 fn holds_no_priority(&self) -> SourceError {
6625 SourceError::Refused {
6626 message: format!(
6627 "source {} holds no task priority: its configuration sets no priority_mapping; \
6628 next: set priority_mapping on this source, then run `onetaskgraph sources \
6629 fields {} --apply` to set its board up",
6630 self.name, self.name
6631 ),
6632 }
6633 }
6634
6635 /// Set one task's priority and nothing else; see [`TaskSource::set_task_priority`].
6636 ///
6637 /// One field write — a select, or a clear for `none` — and no title, body, label, state
6638 /// or `Status` request. Clearing a priority an item does not hold sends nothing.
6639 async fn set_priority(
6640 &self,
6641 id: &NativeId,
6642 priority: Priority,
6643 ) -> Result<Option<Priority>, SourceError> {
6644 if self.priorities.is_none() {
6645 return Err(self.holds_no_priority());
6646 }
6647 let Some(mut item) = self
6648 .bound_item(id)
6649 .await?
6650 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
6651 else {
6652 return Ok(None);
6653 };
6654 if priority == Priority::None && !item.holds_priority() {
6655 return Ok(Some(priority));
6656 }
6657 // The item's own read carries the field's definition whenever it holds a value of
6658 // it, which a clear always does; a select onto an item holding none reads the board.
6659 let board = match (item.carried_board(), item.named_board()) {
6660 (Some(board), _) => board,
6661 (None, Some(id)) if item.defines(PRIORITY_FIELD) => BoardFields {
6662 id,
6663 fields: json!({"nodes": item.fields.clone(), "pageInfo": {"hasNextPage": false}}),
6664 },
6665 _ => self.board_fields().await?,
6666 };
6667 let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? else {
6668 return Ok(Some(priority));
6669 };
6670 let (document, root, input) = match write {
6671 PriorityWrite::Select { field, option } => (
6672 graphql::UPDATE_FIELD,
6673 "updateProjectV2ItemFieldValue",
6674 json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field,"value":{"singleSelectOptionId":option}}),
6675 ),
6676 PriorityWrite::Clear { field } => (
6677 graphql::CLEAR_FIELD,
6678 "clearProjectV2ItemFieldValue",
6679 json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field}),
6680 ),
6681 };
6682 let data = self
6683 .graphql(
6684 document,
6685 json!({"input":input,"readPriority":true,"priorityName":PRIORITY_FIELD}),
6686 )
6687 .await?;
6688 let returned = data
6689 .get(root)
6690 .and_then(|value| value.get("projectV2Item"))
6691 .ok_or_else(|| SourceError::Malformed {
6692 message: "GitHub priority write returned no project item".into(),
6693 })?;
6694 if required_str(returned, "id")? != item.item_id {
6695 return Err(SourceError::Malformed {
6696 message: "GitHub priority write returned the wrong project item".into(),
6697 });
6698 }
6699 let value = returned
6700 .get("fieldValueByName")
6701 .ok_or_else(|| SourceError::Malformed {
6702 message: "GitHub priority write returned no priority read-back".into(),
6703 })?;
6704 if !value.is_null()
6705 && value.pointer("/field/name").and_then(Value::as_str) != Some(PRIORITY_FIELD)
6706 {
6707 return Err(SourceError::Malformed {
6708 message: "GitHub priority read-back is not a Priority field value".into(),
6709 });
6710 }
6711 let values = if value.is_null() {
6712 Vec::new()
6713 } else {
6714 vec![value.clone()]
6715 };
6716 item.priority = self.held_priority(&values)?;
6717 let answer = item.task()?.priority;
6718 self.remember_written(item, false)?;
6719 Ok(Some(answer))
6720 }
6721
6722 /// Replace one task's visible body and nothing else; see
6723 /// [`TaskSource::set_task_content`].
6724 ///
6725 /// One update of the body, which differs from the body GitHub holds only outside the
6726 /// metadata slot — the slot is kept byte for byte, so every caller key and every list
6727 /// this source keeps there reads back as it was. A body that would not change is not
6728 /// sent at all.
6729 async fn replace_content(
6730 &self,
6731 id: &NativeId,
6732 content: &str,
6733 ) -> Result<Option<()>, SourceError> {
6734 let Some(mut item) = self
6735 .bound_item(id)
6736 .await?
6737 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
6738 else {
6739 return Ok(None);
6740 };
6741 let held = item.raw_body.clone().unwrap_or_default();
6742 let body = with_content(&held, content)?;
6743 // Checked before anything is sent: content ending in what this source reads as its own
6744 // metadata slot would read back as metadata rather than as the content it was.
6745 let (visible, slot) = metadata_body(Some(body.clone()))?;
6746 if visible.as_deref().unwrap_or_default() != content || slot != item.slot {
6747 return Err(SourceError::Refused {
6748 message: format!(
6749 "this content ends in what source {} reads as its own metadata slot \
6750 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
6751 as content; next: remove that trailing block from the content",
6752 self.name
6753 ),
6754 });
6755 }
6756 if body != held {
6757 self.update_content(item.content_kind, &item.id, json!({"body": body}))
6758 .await?;
6759 }
6760 item.body = visible.filter(|value| !value.is_empty());
6761 item.raw_body = Some(body);
6762 item.slot = slot;
6763 self.remember_written(item, false)?;
6764 Ok(Some(()))
6765 }
6766
6767 /// Apply one targeted update to one task; see [`TaskSource::update_task`].
6768 ///
6769 /// One read of the item — which carries the board's field definitions and the issue's
6770 /// `blockedBy`, so neither is read again — and then only what differs from it: the
6771 /// `Status` option and the `Priority` field together in one request, the `blockedBy`
6772 /// additions and removals the named edges differ by, and last one `updateIssue` carrying
6773 /// the title, the body — visible content and metadata slot together — and a state change.
6774 /// So an update naming any of title, body, metadata, status and priority is one read and
6775 /// at most two writes. The body goes last so that a write refused part-way leaves it, and
6776 /// the metadata in it, as it stood. A terminal status selects its option and then closes,
6777 /// as a whole write does; an open one selects its option and then reopens. The origin
6778 /// field is never written: an update is of an item that already exists, whose origin is
6779 /// what it is.
6780 ///
6781 /// The task answered is the item as those writes left it, built from the read and what was
6782 /// sent rather than read again — the same record a later read in this run answers from.
6783 async fn targeted_update(
6784 &self,
6785 id: &NativeId,
6786 update: &TaskUpdate,
6787 ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
6788 // Everything this source can refuse without reading the item is refused first, in the
6789 // words a whole write of the same fields is refused with.
6790 update.consistent()?;
6791 if update
6792 .title
6793 .as_deref()
6794 .is_some_and(|title| title.starts_with(DESIGN_TITLE_PREFIX))
6795 {
6796 return Err(SourceError::Refused {
6797 message: format!(
6798 "the title of this task begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
6799 spells a document, so it would read back as one rather than as a task; \
6800 retitle it",
6801 self.name
6802 ),
6803 });
6804 }
6805 if let Some(delivers) = &update.delivers {
6806 TaskRef::listed(
6807 TaskRef::DELIVERS_KEY,
6808 id,
6809 Some(&self.name),
6810 delivers.clone(),
6811 )
6812 .map_err(|message| SourceError::Refused { message })?;
6813 }
6814 if self.priorities.is_none()
6815 && update
6816 .priority
6817 .is_some_and(|priority| priority != Priority::None)
6818 {
6819 return Err(self.holds_no_priority());
6820 }
6821 let target = update
6822 .status
6823 .as_ref()
6824 .map(|status| self.resolved_target(ItemKind::Task, status.category))
6825 .transpose()?;
6826 let Some(mut item) = self
6827 .bound_item(id)
6828 .await?
6829 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
6830 else {
6831 return Ok(None);
6832 };
6833 let before = item.task()?;
6834
6835 let mut status_move = None;
6836 if let (Some(status), Some(target)) = (&update.status, target) {
6837 let board = self.status_board(&item).await?;
6838 let (field, option, name) = self
6839 .column_for(&board.fields, ItemKind::Task, status.category, &target)?
6840 .ok_or_else(|| SourceError::Malformed {
6841 message: format!(
6842 "status {} of source {} names no board Status option",
6843 category_name(status.category),
6844 self.name
6845 ),
6846 })?;
6847 let terminal = matches!(target, StatusTarget::Terminal(_, _));
6848 if terminal && item.content_kind == ContentKind::DraftIssue {
6849 return Err(self.closes_a_draft(status.category));
6850 }
6851 let landed = match &target {
6852 StatusTarget::Terminal(_, reason) => {
6853 self.statuses
6854 .status(ItemKind::Task, Some(&name), true, Some(reason.reason()))
6855 }
6856 _ => self
6857 .statuses
6858 .status(ItemKind::Task, Some(&name), false, None),
6859 };
6860 let option_moves = item
6861 .option
6862 .as_deref()
6863 .is_none_or(|held| !held.eq_ignore_ascii_case(&name));
6864 let state_moves = item.content_kind == ContentKind::Issue
6865 && (item.closed != terminal || (terminal && item.status != landed));
6866 if let Some(moves) = Moves::of(option_moves, state_moves) {
6867 status_move = Some(StatusMove {
6868 board: board.id,
6869 field,
6870 option,
6871 name,
6872 target,
6873 landed,
6874 moves,
6875 });
6876 }
6877 }
6878
6879 let mut priority_move = None;
6880 if let Some(priority) = update.priority
6881 && self.priorities.is_some()
6882 && item.priority != HeldPriority::Read(priority)
6883 {
6884 let board = match (item.carried_board(), item.named_board()) {
6885 (Some(board), _) => board,
6886 (None, Some(board)) if item.defines(PRIORITY_FIELD) => BoardFields {
6887 id: board,
6888 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
6889 },
6890 _ => self.board_fields().await?,
6891 };
6892 if let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? {
6893 priority_move = Some((board.id, write, priority));
6894 }
6895 }
6896
6897 // The metadata keys this update names that a board text field projects, and what
6898 // that does to those fields — refused, as a whole write refuses it, before anything
6899 // is sent.
6900 let touched: Vec<&str> = update
6901 .metadata_set
6902 .keys()
6903 .chain(update.metadata_remove.iter())
6904 .map(MetadataKey::as_str)
6905 .collect();
6906 let mut projection_move = None;
6907 if self
6908 .metadata_fields
6909 .iter()
6910 .any(|projection| touched.contains(&&*projection.key))
6911 {
6912 let mut metadata = item.slot.clone();
6913 for (key, value) in &update.metadata_set {
6914 metadata.insert(key.as_str().to_owned(), value.clone());
6915 }
6916 for key in &update.metadata_remove {
6917 metadata.remove(key.as_str());
6918 }
6919 let board = self.projection_board(&item).await?;
6920 let (writes, projected) = self.projection_writes(
6921 &board.fields,
6922 Some(&item.projected),
6923 &metadata,
6924 Some(&touched),
6925 )?;
6926 projection_move = Some((board.id, writes, projected));
6927 }
6928
6929 // Resolved before the body is composed, because a far end `blockedBy` cannot name is
6930 // recorded in the slot, and the slot travels in the one body update below.
6931 let edges = match &update.depends_on {
6932 Some(edges) => Some(
6933 self.partition_edges(
6934 BoardKind::Work(ItemKind::Task),
6935 item.content_kind,
6936 item.blocked_by.as_deref(),
6937 edges,
6938 )
6939 .await?,
6940 ),
6941 None => None,
6942 };
6943
6944 let mut slot = item.slot.clone();
6945 for (key, value) in &update.metadata_set {
6946 slot.insert(key.as_str().to_owned(), value.clone());
6947 }
6948 for key in &update.metadata_remove {
6949 slot.remove(key.as_str());
6950 }
6951 if let Some(delivers) = &update.delivers {
6952 set_task_list(&mut slot, TaskRef::DELIVERS_KEY, delivers);
6953 }
6954 if let Some((_, recorded)) = &edges {
6955 record_edges(&mut slot, recorded);
6956 }
6957 let held = item.raw_body.clone().unwrap_or_default();
6958 let content = match &update.content {
6959 Some(content) => with_content(&held, content)?,
6960 None => held.clone(),
6961 };
6962 // A slot holding what it held is kept byte for byte, compared as JSON rather than as
6963 // the body's bytes, as a metadata write compares it: a slot a person spelled with
6964 // other whitespace would otherwise be re-encoded, which is a write nobody asked for.
6965 let body = if slot == item.slot {
6966 content
6967 } else {
6968 with_slot(&content, &slot)?
6969 };
6970 // Checked before anything is sent, as a content write checks it: content ending in
6971 // what this source reads as its own slot would read back as metadata.
6972 let (visible, read) = metadata_body(Some(body.clone()))?;
6973 let wanted = update.content.as_deref().or(item.body.as_deref());
6974 if visible.as_deref().unwrap_or_default() != wanted.unwrap_or_default() || read != slot {
6975 return Err(SourceError::Refused {
6976 message: format!(
6977 "this content ends in what source {} reads as its own metadata slot \
6978 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
6979 as content; next: remove that trailing block from the content",
6980 self.name
6981 ),
6982 });
6983 }
6984 let recorded_moves =
6985 slot.get(DependencyEdge::RECORDED_KEY) != item.slot.get(DependencyEdge::RECORDED_KEY);
6986
6987 // One `updateIssue` carries all three, because every mutation spends the secondary
6988 // limiter and the title, body and state are one mutation's inputs.
6989 let mut fields = serde_json::Map::new();
6990 if let Some(title) = update.title.as_ref().filter(|title| **title != item.title) {
6991 fields.insert("title".to_owned(), json!(title));
6992 }
6993 if body != held {
6994 fields.insert("body".to_owned(), json!(body));
6995 }
6996 if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.state()) {
6997 fields.insert("stateInput".to_owned(), state_input(Some(&moving.target)));
6998 }
6999 // **The body is written last, and that is the guarantee a refusal part-way keeps.**
7000 // GitHub runs no two requests as one, and runs one document's mutation fields in order
7001 // without undoing an earlier field when a later one fails — so a body written before a
7002 // board field the board then refused would be left changed. Written after every other
7003 // write has landed, a refusal anywhere leaves the item's body, and every metadata key
7004 // it carries, exactly as they stood. So the `Status` option and the `Priority` field go
7005 // first, together in one request — a terminal option selected before the issue
7006 // closes, as a whole write does — then the `blockedBy` difference, then the body.
7007 let mut board_writes: Vec<(&BoardId, (String, Value))> = Vec::new();
7008 let mut clears: Vec<(&BoardId, &str)> = Vec::new();
7009 if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.option()) {
7010 board_writes.push((
7011 &moving.board,
7012 (
7013 moving.field.clone(),
7014 json!({"singleSelectOptionId": moving.option}),
7015 ),
7016 ));
7017 }
7018 match &priority_move {
7019 Some((board, PriorityWrite::Select { field, option }, _)) => board_writes.push((
7020 board,
7021 (field.clone(), json!({"singleSelectOptionId": option})),
7022 )),
7023 Some((board, PriorityWrite::Clear { field }, _)) => clears.push((board, field)),
7024 None => {}
7025 }
7026 if let Some((board, writes, _)) = &projection_move {
7027 for write in writes {
7028 let (mut values, mut cleared) = (Vec::new(), Vec::new());
7029 write.join(&mut values, &mut cleared);
7030 board_writes.extend(values.into_iter().map(|value| (board, value)));
7031 clears.extend(cleared.into_iter().map(|field| (board, field)));
7032 }
7033 }
7034 let mut boards: Vec<&BoardId> = board_writes.iter().map(|(board, _)| *board).collect();
7035 boards.extend(clears.iter().map(|(board, _)| *board));
7036 boards.dedup_by(|one, other| one.as_str() == other.as_str());
7037 for board in boards {
7038 let writes = board_writes
7039 .iter()
7040 .filter(|(on, _)| on.as_str() == board.as_str())
7041 .map(|(_, write)| write.clone())
7042 .collect::<Vec<_>>();
7043 let cleared = clears
7044 .iter()
7045 .filter(|(on, _)| on.as_str() == board.as_str())
7046 .map(|(_, field)| *field)
7047 .collect::<Vec<_>>();
7048 self.set_item_fields(board.as_str(), &item.item_id, &writes, &cleared)
7049 .await?;
7050 }
7051 let mut blocked_by_moved = false;
7052 if let Some((native, _)) = &edges
7053 && item.content_kind == ContentKind::Issue
7054 {
7055 blocked_by_moved = self
7056 .reconcile_blocked_by(
7057 &item.id,
7058 native,
7059 Issue::Existing(item.blocked_by.as_deref()),
7060 )
7061 .await?;
7062 }
7063 if !fields.is_empty() {
7064 self.update_content(item.content_kind, &item.id, Value::Object(fields))
7065 .await?;
7066 }
7067
7068 if let Some(title) = &update.title {
7069 item.title.clone_from(title);
7070 }
7071 item.body = visible.filter(|value| !value.is_empty());
7072 item.raw_body = (!body.is_empty() || item.raw_body.is_some()).then_some(body);
7073 item.slot = slot;
7074 if let Some(delivers) = &update.delivers {
7075 item.delivers.clone_from(delivers);
7076 }
7077 if let Some(moving) = status_move {
7078 item.closed = matches!(moving.target, StatusTarget::Terminal(_, _))
7079 && item.content_kind == ContentKind::Issue;
7080 item.status = moving.landed;
7081 item.option = Some(moving.name);
7082 }
7083 if let Some((_, _, priority)) = priority_move {
7084 item.priority = HeldPriority::Read(priority);
7085 }
7086 if let Some((_, _, projected)) = projection_move {
7087 item.projected = projected;
7088 }
7089 let task = item.task()?;
7090 let mut written = update.changed(&before, &task);
7091 if blocked_by_moved || recorded_moves {
7092 written.insert(UpdatedField::DependsOn);
7093 }
7094 self.remember_written(item, false)?;
7095 Ok(Some(TaskUpdateOutcome {
7096 task,
7097 written,
7098 delivers_before: before.delivers,
7099 }))
7100 }
7101
7102 /// Replace one issue's visible body and its [`MetadataKey::TEMPLATE_KEY`] slot entry
7103 /// together, and nothing else; see [`TaskSource::set_task_rendering`].
7104 ///
7105 /// One update of the body: the content outside the slot, and inside it that one entry,
7106 /// every other entry kept as it was. This source keeps no template answers — an issue has
7107 /// no room beside itself that is not its body, and answers written there would duplicate
7108 /// what the content already says and count against GitHub's body limit — so `answers`
7109 /// reaches nothing here. A body that would not change is not sent at all.
7110 async fn replace_rendering(
7111 &self,
7112 id: &NativeId,
7113 kind: BoardKind,
7114 content: &str,
7115 provenance: &Value,
7116 assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
7117 ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
7118 let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
7119 return Ok(None);
7120 };
7121 let held = item.raw_body.clone().unwrap_or_default();
7122 let mut slot = item.slot.clone();
7123 slot.insert(MetadataKey::TEMPLATE_KEY.to_owned(), provenance.clone());
7124 let rewritten;
7125 let content = if let Some(assets) = assets {
7126 let uploads = self
7127 .upload_assets(item.own_repository.as_ref(), assets)
7128 .await?;
7129 rewritten =
7130 onetaskgraph_plugin_api::serve_asset_references(content, &mut slot, &uploads);
7131 rewritten.as_str()
7132 } else {
7133 content
7134 };
7135 let body = with_slot(&with_content(&held, content)?, &slot)?;
7136 // Checked before anything is sent, as a content write checks it.
7137 let (visible, read) = metadata_body(Some(body.clone()))?;
7138 if visible.as_deref().unwrap_or_default() != content || read != slot {
7139 return Err(SourceError::Refused {
7140 message: format!(
7141 "this content ends in what source {} reads as its own metadata slot \
7142 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
7143 as content; next: remove that trailing block from the template",
7144 self.name
7145 ),
7146 });
7147 }
7148 if body != held {
7149 self.update_content(item.content_kind, &item.id, json!({"body": body}))
7150 .await?;
7151 }
7152 item.body = visible.filter(|value| !value.is_empty());
7153 item.raw_body = Some(body);
7154 item.slot = read;
7155 self.remember_written(item, false)?;
7156 Ok(Some(onetaskgraph_plugin_api::AssetsWritten {
7157 id: id.clone(),
7158 content: Some(content.to_owned()),
7159 }))
7160 }
7161
7162 async fn set_item_field(
7163 &self,
7164 board_id: &str,
7165 item_id: &str,
7166 field_id: &str,
7167 value: Value,
7168 ) -> Result<(), SourceError> {
7169 let data = self
7170 .graphql(
7171 graphql::UPDATE_FIELD,
7172 json!({"input":{
7173 "projectId":board_id,"itemId":item_id,"fieldId":field_id,"value":value
7174 },"readPriority":false,"priorityName":PRIORITY_FIELD}),
7175 )
7176 .await?;
7177 let returned = data
7178 .pointer("/updateProjectV2ItemFieldValue/projectV2Item")
7179 .ok_or_else(|| SourceError::Malformed {
7180 message: "GitHub field update returned no project item".into(),
7181 })?;
7182 if required_str(returned, "id")? != item_id {
7183 return Err(SourceError::Malformed {
7184 message: "GitHub field update returned the wrong project item".into(),
7185 });
7186 }
7187 Ok(())
7188 }
7189
7190 /// GitHub accepts one value per field mutation; aliases combine those mutations in
7191 /// one request — writes in the order given, then clears. A lone write is
7192 /// [`graphql::UPDATE_FIELD`] and a lone clear [`graphql::CLEAR_FIELD`]; more than one
7193 /// request's slots hold go in further requests of the same document, in order. Every
7194 /// returned item id is checked, including optional aliases.
7195 async fn set_item_fields(
7196 &self,
7197 board: &str,
7198 item: &str,
7199 fields: &[(String, Value)],
7200 clears: &[&str],
7201 ) -> Result<(), SourceError> {
7202 match (fields, clears) {
7203 ([], []) => return Ok(()),
7204 ([(field, value)], []) => {
7205 return self.set_item_field(board, item, field, value.clone()).await;
7206 }
7207 ([], [field]) => {
7208 return self
7209 .write_priority(
7210 board,
7211 item,
7212 &PriorityWrite::Clear {
7213 field: (*field).to_owned(),
7214 },
7215 )
7216 .await;
7217 }
7218 _ => {}
7219 }
7220 // Any field of the request stands in for an input a slot leaves out, which GitHub
7221 // still requires to be well formed although it never runs it.
7222 let filler = fields
7223 .first()
7224 .map_or_else(|| clears[0], |(field, _)| field.as_str());
7225 let mut writes = fields.iter();
7226 let mut cleared = clears.iter();
7227 loop {
7228 let batch: Vec<&(String, Value)> =
7229 writes.by_ref().take(FIELD_WRITE_SLOTS.len()).collect();
7230 let batch_clears: Vec<&&str> = cleared.by_ref().take(FIELD_CLEAR_SLOTS.len()).collect();
7231 if batch.is_empty() && batch_clears.is_empty() {
7232 return Ok(());
7233 }
7234 let mut variables = serde_json::Map::new();
7235 for (index, slot) in FIELD_WRITE_SLOTS.iter().enumerate() {
7236 let input = match batch.get(index) {
7237 Some((field, value)) => {
7238 json!({"projectId":board,"itemId":item,"fieldId":field,"value":value})
7239 }
7240 None => json!({"projectId":board,"itemId":item,"fieldId":filler,
7241 "value":{"text":""}}),
7242 };
7243 variables.insert(slot.variable.to_owned(), input);
7244 variables.insert(slot.include.to_owned(), json!(index < batch.len()));
7245 }
7246 for (index, slot) in FIELD_CLEAR_SLOTS.iter().enumerate() {
7247 let field = batch_clears.get(index).map_or(filler, |field| **field);
7248 variables.insert(
7249 slot.variable.to_owned(),
7250 json!({"projectId":board,"itemId":item,"fieldId":field}),
7251 );
7252 variables.insert(slot.include.to_owned(), json!(index < batch_clears.len()));
7253 }
7254 let data = self
7255 .graphql(graphql::UPDATE_FIELDS, Value::Object(variables))
7256 .await?;
7257 let sent = FIELD_WRITE_SLOTS[..batch.len()]
7258 .iter()
7259 .chain(&FIELD_CLEAR_SLOTS[..batch_clears.len()]);
7260 for slot in sent {
7261 let alias = slot.alias;
7262 let returned = data
7263 .get(alias)
7264 .and_then(|value| value.get("projectV2Item"))
7265 .ok_or_else(|| SourceError::Malformed {
7266 message: format!("GitHub field update {alias} returned no project item"),
7267 })?;
7268 if required_str(returned, "id")? != item {
7269 return Err(SourceError::Malformed {
7270 message: format!(
7271 "GitHub field update {alias} returned the wrong project item"
7272 ),
7273 });
7274 }
7275 }
7276 }
7277 }
7278
7279 async fn native_dependency_ids(&self, id: &NativeId) -> Result<Vec<String>, SourceError> {
7280 let mut after: Option<String> = None;
7281 let mut ids = Vec::new();
7282 loop {
7283 let data = self
7284 .graphql(
7285 graphql::ISSUE_DEPENDENCIES,
7286 json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after}),
7287 )
7288 .await?;
7289 let connection =
7290 data.pointer("/node/blockedBy")
7291 .ok_or_else(|| SourceError::Malformed {
7292 message: "GitHub dependency response has no blockedBy connection".into(),
7293 })?;
7294 ids.extend(
7295 connection
7296 .get("nodes")
7297 .and_then(Value::as_array)
7298 .ok_or_else(|| SourceError::Malformed {
7299 message: "GitHub dependency response nodes is not an array".into(),
7300 })?
7301 .iter()
7302 .map(|value| required_str(value, "id").map(str::to_owned))
7303 .collect::<Result<Vec<_>, _>>()?,
7304 );
7305 let next = next_cursor(connection)?;
7306 if let Some(next) = &next {
7307 validate_cursor_progress(after.as_deref(), &next.0)?;
7308 }
7309 after = next.map(|cursor| cursor.0);
7310 if after.is_none() {
7311 return Ok(ids);
7312 }
7313 }
7314 }
7315
7316 async fn dependencies(
7317 &self,
7318 id: &NativeId,
7319 near_kind: ItemKind,
7320 direction: Direction,
7321 page: &PageRequest,
7322 ) -> Result<Page<DependencyEdge>, SourceError> {
7323 validate_page(page)?;
7324 let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
7325 let cursor = page.cursor.as_ref().map(|c| c.0.as_str());
7326 let recorded = recorded_offset(cursor, direction)?;
7327 // What this issue is blocked by, when a read of it by its own id in this command
7328 // already carried the whole connection — a copy reads the item it writes before it
7329 // reads its edges — and the page asked for is the whole of it, or the recorded tail
7330 // after it. Answered from that read, in the shape the dependency read answers in;
7331 // anything else is asked of GitHub.
7332 let carried = match direction {
7333 Direction::DependsOn => self
7334 .resolved_cache()?
7335 .get(id)
7336 .filter(|item| item.content_kind == ContentKind::Issue)
7337 .and_then(|item| Some((item.blocked_by.clone()?, item.raw_body.clone()))),
7338 Direction::DependedOnBy => None,
7339 }
7340 .filter(|(nodes, _)| recorded.is_some() || (cursor.is_none() && nodes.len() <= limit));
7341 // Asked for even in the recorded phase, whose page reads nothing from the
7342 // connection: `__typename` is what says whether this item has a native
7343 // relationship at all, and that is what decides which far ends the reserved key is
7344 // allowed to hold.
7345 let data = match carried {
7346 Some((nodes, body)) => json!({"node":{"__typename":"Issue","body":body,
7347 "blockedBy":{"nodes":nodes,"pageInfo":{"hasNextPage":false,"endCursor":null}}}}),
7348 None => {
7349 self.graphql(
7350 graphql::ISSUE_DEPENDENCIES,
7351 json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),
7352 "after":if recorded.is_some() {None} else {cursor}}),
7353 )
7354 .await?
7355 }
7356 };
7357 let node =
7358 data.get("node")
7359 .filter(|v| !v.is_null())
7360 .ok_or_else(|| SourceError::Refused {
7361 message: format!(
7362 "GitHub item {} was not found or does not support dependencies",
7363 id.0
7364 ),
7365 })?;
7366 let connection_name = match direction {
7367 Direction::DependsOn => "blockedBy",
7368 Direction::DependedOnBy => "blocking",
7369 };
7370 // A draft has neither `blockedBy` nor `blocking`, so nothing it depends on can be
7371 // named natively and the reserved key may hold any far end. An issue's connections
7372 // hold issues, and this source reads them at the near item's own level.
7373 let natively_names = (required_str(node, "__typename")? == "Issue").then_some(near_kind);
7374 if let Some(offset) = recorded {
7375 return Ok(recorded_page(
7376 self.recorded_edges(id, near_kind, direction, natively_names, node)
7377 .await?,
7378 offset,
7379 limit,
7380 ));
7381 }
7382 if natively_names.is_none() {
7383 return Ok(recorded_page(
7384 self.recorded_edges(id, near_kind, direction, natively_names, node)
7385 .await?,
7386 0,
7387 limit,
7388 ));
7389 }
7390 let connection = node
7391 .get(connection_name)
7392 .ok_or_else(|| SourceError::Malformed {
7393 message: "GitHub dependency response is missing its connection".into(),
7394 })?;
7395 let nodes = connection
7396 .get("nodes")
7397 .and_then(Value::as_array)
7398 .ok_or_else(|| SourceError::Malformed {
7399 message: "GitHub dependency response nodes is not an array".into(),
7400 })?;
7401 // `from` depends on `to`, always. GitHub spells the same relationship from either
7402 // end — `blockedBy` lists what this item waits on, `blocking` lists what waits on
7403 // it — so the near item is `from` in one direction and `to` in the other.
7404 let items = nodes
7405 .iter()
7406 .map(|value| {
7407 let related = NativeId(required_str(value, "id")?.into());
7408 let related_kind = related_kind(value)?;
7409 let (from, to) = match direction {
7410 Direction::DependsOn => (
7411 DependencyEndpoint::from_native(id.clone(), near_kind),
7412 DependencyEndpoint::from_native(related, related_kind),
7413 ),
7414 Direction::DependedOnBy => (
7415 DependencyEndpoint::from_native(related, related_kind),
7416 DependencyEndpoint::from_native(id.clone(), near_kind),
7417 ),
7418 };
7419 Ok(DependencyEdge {
7420 from,
7421 to,
7422 kind: DependencyKind::Blocks,
7423 })
7424 })
7425 .collect::<Result<Vec<_>, SourceError>>()?;
7426 let mut next = next_cursor(connection)?;
7427 if let Some(next) = &next {
7428 validate_cursor_progress(cursor, &next.0)?;
7429 }
7430 if next.is_none()
7431 && !self
7432 .recorded_edges(id, near_kind, direction, natively_names, node)
7433 .await?
7434 .is_empty()
7435 {
7436 next = Some(Cursor(format!("{RECORDED_CURSOR}0")));
7437 }
7438 Ok(Page { items, next })
7439 }
7440
7441 /// The edges this item records under [`DependencyEdge::RECORDED_KEY`], which is where
7442 /// a far end in another source has to live: no GitHub issue relationship can name one.
7443 ///
7444 /// Only forwards. The reverse of a recorded edge is derived from the far end, and this
7445 /// source never writes one down.
7446 ///
7447 /// The metadata lives in the item's own body slot, and `node` is the dependency read's
7448 /// own answer, which carries an issue's body — so an issue's recorded edges cost no
7449 /// request beyond the read already made, and reading the board for them would be a
7450 /// walk of every item for one field of one. A draft has no body in that answer, because
7451 /// a draft is not an issue, so a draft's are read off its own read by id — never off a
7452 /// listing of the board, which can be behind on the very item asked about.
7453 async fn recorded_edges(
7454 &self,
7455 id: &NativeId,
7456 near_kind: ItemKind,
7457 direction: Direction,
7458 natively_names: Option<ItemKind>,
7459 node: &Value,
7460 ) -> Result<Vec<DependencyEdge>, SourceError> {
7461 if direction != Direction::DependsOn {
7462 return Ok(Vec::new());
7463 }
7464 let slot = match node.get("body") {
7465 Some(body) if natively_names.is_some() => {
7466 metadata_body(body.as_str().map(str::to_owned))?.1
7467 }
7468 _ => {
7469 let Some(item) = self.bound_item(id).await? else {
7470 return Ok(Vec::new());
7471 };
7472 item.slot
7473 }
7474 };
7475 DependencyEdge::recorded(&slot, id, near_kind, &self.name, natively_names)
7476 .map_err(|message| SourceError::Malformed { message })
7477 }
7478
7479 fn configured_repository(&self) -> Result<&RepositoryTarget, SourceError> {
7480 self.repository
7481 .as_ref()
7482 .ok_or_else(|| SourceError::Refused {
7483 message: format!(
7484 "source {} has no repository configured, and a GitHub Projects board has no \
7485 repository of its own to create an issue in; set repository: owner/name on \
7486 this source",
7487 self.name
7488 ),
7489 })
7490 }
7491
7492 /// The repository one new issue is created in, under the rule [`RepositoryTarget`]
7493 /// states.
7494 ///
7495 /// The fallback is demanded first, whichever arm answers: a write without a configured
7496 /// repository is refused naming the field exactly as it was before the rule existed,
7497 /// so a source that could not write before cannot write now, rather than writing for
7498 /// the one item whose own field happens to decide it.
7499 ///
7500 /// Everything this refuses is refused before `createIssue`, so a refusal leaves no
7501 /// issue behind: an entry that is not a repository on [`RepositoryTarget::HOST`], an
7502 /// entry owned by someone other than the owner of the parent issue's repository —
7503 /// GitHub accepts a sub-issue from another repository of the same owner and from no
7504 /// other, so `addSubIssue` would refuse it after the issue existed — a parent the
7505 /// board does not hold, and a parent that is a draft, which GitHub gives no sub-issues,
7506 /// both of which `addSubIssue` would likewise refuse too late. Whether the entry exists
7507 /// and is visible to the token is checked where its node id is resolved, still before
7508 /// `createIssue`. The parent is read by its own id through [`Self::item_by_id`] — never
7509 /// looked up in a listing of the board, which can be minutes behind an issue its own
7510 /// `projectItems` already places on it — and that read answers first from this process's
7511 /// own record, so a project created moments ago in this command answers though GitHub
7512 /// has not caught up.
7513 async fn creation_target(
7514 &self,
7515 incoming: &Incoming<'_>,
7516 ) -> Result<RepositoryTarget, SourceError> {
7517 let fallback = self.configured_repository()?;
7518 let what = |incoming: &Incoming<'_>| {
7519 format!(
7520 "{} {:?}",
7521 incoming.written.kind().describes(),
7522 incoming.title
7523 )
7524 };
7525 let parent = match incoming.parent {
7526 Some(parent) => Some(self.bound_item(parent).await?.ok_or_else(|| {
7527 SourceError::Refused {
7528 message: format!(
7529 "GitHub project issue {} was not found on the board of source {}, so {} \
7530 cannot be filed under it",
7531 parent.0,
7532 self.name,
7533 what(incoming)
7534 ),
7535 }
7536 })?),
7537 None => None,
7538 };
7539 let parents_repository = parent
7540 .as_ref()
7541 .map(|parent| {
7542 // A draft is on the board and so is found, but it has no repository to
7543 // place a task in and GitHub gives it no sub-issues, so `addSubIssue`
7544 // would refuse the task only once `createIssue` had made it.
7545 if parent.content_kind == ContentKind::DraftIssue {
7546 return Err(SourceError::Refused {
7547 message: format!(
7548 "GitHub project item {} on the board of source {} is a draft, \
7549 which cannot have sub-issues, so {} cannot be filed under it",
7550 parent.id.0,
7551 self.name,
7552 what(incoming)
7553 ),
7554 });
7555 }
7556 // An issue's repository is where a sub-issue is placed and whose owner it
7557 // is compared against, so a parent whose repository this source cannot
7558 // spell as `owner/name` — GitHub's login grammar is wider than this
7559 // source's floor — is one nothing can be filed under.
7560 parent
7561 .own_repository
7562 .as_ref()
7563 .and_then(|origin| RepositoryTarget::from_origin(origin).ok())
7564 .ok_or_else(|| SourceError::Malformed {
7565 message: format!(
7566 "GitHub project issue {} on the board of source {} is in {}, which \
7567 is not a {}/owner/name repository this source can place {} in",
7568 parent.id.0,
7569 self.name,
7570 parent
7571 .own_repository
7572 .as_ref()
7573 .map_or("no repository", Repository::as_str),
7574 RepositoryTarget::HOST,
7575 what(incoming)
7576 ),
7577 })
7578 })
7579 .transpose()?;
7580 match incoming.repositories {
7581 [named] => {
7582 let target =
7583 RepositoryTarget::from_origin(named).map_err(|_| SourceError::Refused {
7584 message: format!(
7585 "{} names repository {}, which is not a {}/owner/name repository \
7586 source {} can create an issue in; name one that is, or name none",
7587 what(incoming),
7588 named.as_str(),
7589 RepositoryTarget::HOST,
7590 self.name
7591 ),
7592 })?;
7593 if let Some(parents) = &parents_repository
7594 && parents.owner != target.owner
7595 {
7596 return Err(SourceError::Refused {
7597 message: format!(
7598 "{} names repository {}, owned by {}, but its project's issue is in \
7599 {}, owned by {}, and GitHub files a sub-issue only in a repository \
7600 of the same owner as its parent issue; name a repository of {}, or \
7601 name none",
7602 what(incoming),
7603 target.slug(),
7604 target.owner,
7605 parents.slug(),
7606 parents.owner,
7607 parents.owner
7608 ),
7609 });
7610 }
7611 Ok(target)
7612 }
7613 _ => Ok(parents_repository.unwrap_or_else(|| fallback.clone())),
7614 }
7615 }
7616
7617 /// The node id of the repository `incoming` is being created in, or the refusal naming
7618 /// the item and the repository the token cannot see.
7619 ///
7620 /// Resolved once per command per repository; see [`Self::repository_cache`].
7621 async fn repository_id(
7622 &self,
7623 repository: &RepositoryTarget,
7624 incoming: &Incoming<'_>,
7625 ) -> Result<String, SourceError> {
7626 if let Some(id) = self.repository_cache()?.get(repository).cloned() {
7627 return Ok(id);
7628 }
7629 let data = self
7630 .graphql(
7631 graphql::REPOSITORY,
7632 json!({"owner":repository.owner,"name":repository.name}),
7633 )
7634 .await?;
7635 self.repository_read(&data, repository, incoming)
7636 }
7637
7638 /// The repository's node id out of an answer carrying the `repository` root, held for
7639 /// the rest of this command, or the refusal naming the item that cannot be created in it.
7640 fn repository_read(
7641 &self,
7642 data: &Value,
7643 repository: &RepositoryTarget,
7644 incoming: &Incoming<'_>,
7645 ) -> Result<String, SourceError> {
7646 let node = data
7647 .get("repository")
7648 .filter(|value| !value.is_null())
7649 .ok_or_else(|| SourceError::Refused {
7650 message: format!(
7651 "GitHub repository {} was not found or is not visible to the token, so {} \
7652 {:?} cannot be created in it",
7653 repository.slug(),
7654 incoming.written.kind().describes(),
7655 incoming.title
7656 ),
7657 })?;
7658 let id = required_str(node, "id")?.to_owned();
7659 self.repository_cache()?
7660 .insert(repository.clone(), id.clone());
7661 Ok(id)
7662 }
7663
7664 fn repository_cache(
7665 &self,
7666 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<RepositoryTarget, String>>, SourceError> {
7667 self.repository_cache
7668 .lock()
7669 .map_err(|_| SourceError::Unavailable {
7670 message: "this source's record of the destination repository was left \
7671 inconsistent by an earlier failure; next: run the command again"
7672 .into(),
7673 })
7674 }
7675
7676 /// Create or update one board item, whichever kind it is.
7677 async fn write_item(
7678 &self,
7679 incoming: &Incoming<'_>,
7680 target: Option<&NativeId>,
7681 depends_on: &[DependencyEdge],
7682 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
7683 // Refused before anything is read or written: a task or a project titled the way
7684 // this board spells a document would land as an issue this same source reads back
7685 // as a document, so the field this destination cannot carry is named rather than
7686 // written and silently reclassified.
7687 if let Written::Work(kind, _) = incoming.written
7688 && incoming.title.starts_with(DESIGN_TITLE_PREFIX)
7689 {
7690 return Err(SourceError::Refused {
7691 message: format!(
7692 "the title of this {} begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
7693 spells a document, so it would read back as one rather than as a {}; \
7694 retitle it, or copy it as a document",
7695 kind.marker(),
7696 self.name,
7697 kind.marker()
7698 ),
7699 });
7700 }
7701 // The destination is read by its own id, and whether this board holds it is decided
7702 // by that read — its own `projectItems` — rather than by whether a listing of the
7703 // board happens to include it yet. See the module documentation.
7704 let existing = match target {
7705 Some(target) => {
7706 Some(
7707 self.bound_item(target)
7708 .await?
7709 .ok_or_else(|| SourceError::Refused {
7710 message: format!("GitHub destination item {} was not found", target.0),
7711 })?,
7712 )
7713 }
7714 None => None,
7715 };
7716 let existing = existing.as_ref();
7717 // An existing issue is never moved; a new one is created where the rule says — and
7718 // knowing where is what lets the board's fields and that repository's id be read
7719 // together, before anything below needs either.
7720 let creation_target = match existing {
7721 Some(_) => None,
7722 None => {
7723 let target = self.creation_target(incoming).await?;
7724 self.creation_context(&target, incoming).await?;
7725 Some(target)
7726 }
7727 };
7728 let board = self
7729 .fields_for(
7730 existing,
7731 incoming.written.status().is_some(),
7732 incoming
7733 .priority
7734 .is_some_and(|priority| priority != Priority::None),
7735 )
7736 .await?;
7737 let status_target = incoming
7738 .written
7739 .work_status()
7740 .map(|(kind, status)| self.resolved_target(kind, status.category))
7741 .transpose()?;
7742 let column = match (incoming.written.work_status(), status_target.as_ref()) {
7743 (Some((kind, status)), Some(target)) => {
7744 self.column_for(&board.fields, kind, status.category, target)?
7745 }
7746 _ => None,
7747 };
7748 // Resolved before anything is created, for the reason the column above is: a
7749 // priority this board has no option for is refused while nothing has been written.
7750 let priority_write = match incoming.priority {
7751 Some(priority) => self.priority_write(&board.fields, existing, priority)?,
7752 None => None,
7753 };
7754 // And for the reason the priority is: a value no projected field can hold, or a board
7755 // that cannot hold the field, is refused while nothing has been written.
7756 let (projections, projected) = self.projection_writes(
7757 &board.fields,
7758 existing.map(|item| &item.projected),
7759 incoming.metadata,
7760 None,
7761 )?;
7762 let content_kind = existing.map_or(ContentKind::Issue, |item| item.content_kind);
7763 if content_kind == ContentKind::DraftIssue {
7764 if let (Some(StatusTarget::Terminal(_, _)), Some(status)) =
7765 (status_target.as_ref(), incoming.written.status())
7766 {
7767 return Err(self.closes_a_draft(status.category));
7768 }
7769 if incoming.parent.is_some() {
7770 return Err(SourceError::Refused {
7771 message: "GitHub draft items cannot be a project's sub-issue".into(),
7772 });
7773 }
7774 }
7775 match existing {
7776 Some(item) if content_kind == ContentKind::Issue => {
7777 if item.labels != incoming.labels {
7778 return Err(SourceError::Refused {
7779 message: "GitHub issue labels differ from the labels being written".into(),
7780 });
7781 }
7782 }
7783 _ => {
7784 if !incoming.labels.is_empty() {
7785 return Err(SourceError::Refused {
7786 message: "GitHub items created by this destination carry no labels".into(),
7787 });
7788 }
7789 }
7790 }
7791
7792 // The repository the issue really lives in is what the slot below is written against,
7793 // so a single entry that is where the issue is created travels as no key at all, and
7794 // the read side derives it back from the issue.
7795 let own_repository = match (existing, &creation_target) {
7796 (Some(item), _) => item.own_repository.clone(),
7797 (None, Some(target)) => Some(
7798 Repository::try_from(target.origin())
7799 .map_err(|message| SourceError::Config { message })?,
7800 ),
7801 (None, None) => None,
7802 };
7803 let (native, fallback) = self
7804 .partition_edges(
7805 incoming.written.kind(),
7806 content_kind,
7807 existing.and_then(|item| item.blocked_by.as_deref()),
7808 depends_on,
7809 )
7810 .await?;
7811 let mut slot = slot_metadata(incoming, own_repository.as_ref(), &fallback);
7812 let content = match incoming.assets {
7813 Some(assets) => {
7814 let uploads = self.upload_assets(own_repository.as_ref(), assets).await?;
7815 let rewritten = onetaskgraph_plugin_api::serve_asset_references(
7816 incoming.content.unwrap_or_default(),
7817 &mut slot,
7818 &uploads,
7819 );
7820 incoming.content.map(|_| rewritten)
7821 }
7822 None => incoming.content.map(str::to_owned),
7823 };
7824 let body = compose_body(content.as_deref(), &slot)?;
7825 // Read before anything is created, for the reason the field below is: a value
7826 // this destination cannot store has to refuse, and refusing after `createIssue`
7827 // would leave an issue behind that nothing asked for. The engine writes a
7828 // qualified id here; a caller handing this key anything else is told so rather
7829 // than having it silently stored as no origin at all.
7830 // 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.
7831 let origin = match incoming.metadata.get(ORIGIN_KEY) {
7832 None => "",
7833 Some(Value::String(origin)) => origin.as_str(),
7834 Some(other) => {
7835 return Err(SourceError::Refused {
7836 message: format!(
7837 "{ORIGIN_KEY} holds a qualified id spelled as a string, and this item's \
7838 is {other}"
7839 ),
7840 });
7841 }
7842 };
7843 // Resolved before anything is created: a board that cannot carry the copy origin
7844 // has to refuse the write, and refusing it after `createIssue` would leave an
7845 // issue behind that nothing asked for.
7846 let origin_field = match Board::field(&board.fields, ORIGIN_FIELD)? {
7847 Some(field) => {
7848 if required_str(field, "__typename")? != "ProjectV2Field" {
7849 return Err(SourceError::Refused {
7850 message: format!(
7851 "GitHub board source-owned {ORIGIN_FIELD} field is not a text field"
7852 ),
7853 });
7854 }
7855 Some(required_str(field, "id")?.to_owned())
7856 }
7857 None if incoming.metadata.contains_key(ORIGIN_KEY) => {
7858 return Err(SourceError::Refused {
7859 message: format!(
7860 "GitHub board has no source-owned {ORIGIN_FIELD} text field, and the \
7861 item carries {ORIGIN_KEY}; add a text field named {ORIGIN_FIELD} to \
7862 the board"
7863 ),
7864 });
7865 }
7866 None => None,
7867 };
7868
7869 let Landed {
7870 content_id,
7871 item_id,
7872 url,
7873 number,
7874 } = match existing {
7875 // Its content is written last, below, once everything else has landed.
7876 Some(item) => Landed {
7877 content_id: item.id.clone(),
7878 item_id: item.item_id.clone(),
7879 url: item.url.clone(),
7880 number: item.number,
7881 },
7882 None => {
7883 let target = creation_target
7884 .as_ref()
7885 .ok_or_else(|| SourceError::Malformed {
7886 message: "a new item was decided without a repository to create it in"
7887 .into(),
7888 })?;
7889 self.create_and_file_issue(board.id.as_str(), target, incoming, &body)
7890 .await?
7891 }
7892 };
7893
7894 let written_option = column.as_ref().map(|(_, _, name)| name.clone());
7895 let column = column
7896 .filter(|(_, _, name)| existing.is_none_or(|item| item.option.as_ref() != Some(name)))
7897 .map(|(field, option, _)| (field, option));
7898 // Creating an item here is several calls — `createIssue`, which files it on the
7899 // board, then its board fields, the parent and the dependencies — and GitHub can fail
7900 // at any of them. Everything this source can refuse *before* the first of those is
7901 // already checked above, so what is left is GitHub itself failing part way. When it
7902 // does over an item this call created, the issue is taken back: a write that
7903 // refused must not leave an item behind that nobody asked for, and one that does
7904 // makes the retry create a second.
7905 // Whether the board-field write carrying a moved origin was answered as landing whole.
7906 // When it was refused, GitHub does not say which of its fields ran before the one that
7907 // failed, so the origin may or may not have moved.
7908 let mut origin_landed = false;
7909 let landed = self
7910 .finish_write(
7911 board.id.as_str(),
7912 incoming,
7913 &content_id,
7914 &item_id,
7915 content_kind,
7916 existing,
7917 origin_field.as_deref(),
7918 origin,
7919 column,
7920 status_target.as_ref(),
7921 priority_write.as_ref(),
7922 &projections,
7923 &native,
7924 &mut origin_landed,
7925 )
7926 .await;
7927 // An existing item's title, body and state go last, in one `updateIssue`, once its board
7928 // fields and its relationships have landed: a refusal of any of those then leaves its
7929 // body — and the metadata slot inside it — exactly as it stood.
7930 let landed = match (landed, existing) {
7931 (Ok(()), Some(item)) => {
7932 self.update_existing(item, incoming, &body, status_target.as_ref())
7933 .await
7934 }
7935 (landed, _) => landed,
7936 };
7937 if let Err(error) = landed {
7938 match existing {
7939 // Best effort, and the write's own failure is what the caller is told: a
7940 // refusal naming the tidy-up would hide why the write failed at all.
7941 None => {
7942 let _ = self.delete_issue(&content_id).await;
7943 }
7944 // The origin field is the one piece of an existing item's metadata written
7945 // before its body, so a write refused after it puts it back as it was. When
7946 // that is refused too, the write's own failure is still what the caller is
7947 // told — with what it left behind added, because the item's metadata is then
7948 // not as it stood and a caller retrying has to know which key moved.
7949 Some(item) => {
7950 let before = item.origin.as_deref().unwrap_or("");
7951 if let Some(field) = origin_field.as_deref()
7952 && before != origin
7953 && let Err(restore) = self
7954 .set_item_field(
7955 board.id.as_str(),
7956 &item.item_id,
7957 field,
7958 json!({"text": before}),
7959 )
7960 .await
7961 {
7962 let left = if origin_landed {
7963 format!(
7964 "its {ORIGIN_KEY} was moved to {origin:?} before that and could \
7965 not be put back to {before:?} ({restore}), so item {} still \
7966 holds {origin:?} there",
7967 item.id.0
7968 )
7969 } else {
7970 format!(
7971 "the refused write carried its {ORIGIN_KEY} from {before:?} to \
7972 {origin:?}, GitHub does not say whether that part of it ran, \
7973 and putting it back to {before:?} was refused ({restore}), so \
7974 item {} holds {origin:?} or {before:?} there",
7975 item.id.0
7976 )
7977 };
7978 return Err(noting(
7979 error,
7980 &format!(
7981 "; {left}; next: set {ORIGIN_KEY} on it back to {before:?}, or \
7982 run the write again"
7983 ),
7984 ));
7985 }
7986 }
7987 }
7988 return Err(error);
7989 }
7990
7991 let written_status = match (incoming.written.work_status(), status_target.as_ref()) {
7992 (Some((kind, _)), Some(StatusTarget::Terminal(_, reason))) => {
7993 self.statuses
7994 .status(kind, written_option.as_deref(), true, Some(reason.reason()))
7995 }
7996 (Some((kind, _)), Some(StatusTarget::Column(_))) => {
7997 self.statuses
7998 .status(kind, written_option.as_deref(), false, None)
7999 }
8000 (Some((_, status)), _) => status.clone(),
8001 (None, _) => Status {
8002 category: StatusCategory::Unknown,
8003 name: "Open".to_owned(),
8004 },
8005 };
8006
8007 // So the rest of this command reads what it just did rather than what the board
8008 // said before it. See `remember_written` for which half takes it.
8009 let remembered = Resolved {
8010 item_id,
8011 id: content_id.clone(),
8012 content_kind,
8013 kind: incoming.written.kind(),
8014 title: incoming.title.to_owned(),
8015 // The visible half of the body this write composed, split back off it the
8016 // way a read splits it — so what this record reports is what a read of the
8017 // same issue reports, rather than the person's text with the metadata slot
8018 // still on the end of it.
8019 body: metadata_body(body.clone())?.0,
8020 raw_body: body.clone(),
8021 // A document has no status of its own; what it reads back as is whatever
8022 // the issue's own state says, which is what a re-read reports.
8023 status: written_status,
8024 option: written_option.or_else(|| existing.and_then(|item| item.option.clone())),
8025 priority: match incoming.priority {
8026 Some(priority) => HeldPriority::Read(priority),
8027 None => existing.map_or(HeldPriority::Read(Priority::None), |item| {
8028 item.priority.clone()
8029 }),
8030 },
8031 // What `state_input` asked for: closed for a terminal target, open for any other
8032 // status, and the issue's own state left as it was by a document write.
8033 closed: content_kind == ContentKind::Issue
8034 && match status_target.as_ref() {
8035 Some(StatusTarget::Terminal(_, _)) => true,
8036 Some(_) => false,
8037 None => existing.is_some_and(|item| item.closed),
8038 },
8039 delivers: incoming.delivers.to_vec(),
8040 delivered_by: incoming.delivered_by.to_vec(),
8041 labels: incoming.labels.to_vec(),
8042 parent: incoming.parent.cloned(),
8043 origin: (!origin.is_empty()).then(|| origin.to_owned()),
8044 projected,
8045 number,
8046 // In the update path this is the item's own url, read off `existing` where the
8047 // record above was bound, so one expression serves both halves.
8048 url,
8049 created_at: existing.and_then(|item| item.created_at),
8050 updated_at: existing.and_then(|item| item.updated_at),
8051 own_repository,
8052 repositories: incoming.repositories.to_vec(),
8053 classification: incoming.classification,
8054 slot,
8055 board_id: Some(board.id.as_str().to_owned()),
8056 fields: board
8057 .fields
8058 .get("nodes")
8059 .and_then(Value::as_array)
8060 .cloned()
8061 .unwrap_or_default(),
8062 board_fields: Some(board.fields.clone()),
8063 // What this write left the relationship holding is known by id alone, and a
8064 // later read of its edges needs each far end's kind, so it reads them again.
8065 blocked_by: None,
8066 };
8067 self.remember_written(remembered, existing.is_none())?;
8068 Ok(onetaskgraph_plugin_api::AssetsWritten {
8069 id: content_id,
8070 content,
8071 })
8072 }
8073
8074 /// Everything a write does after the item exists: its board fields, its parent, and
8075 /// its dependencies.
8076 ///
8077 /// Split out of `write_item` so there is one place a failure past the point of no
8078 /// return is caught, rather than a tidy-up repeated at each `?` above.
8079 // llmlint: ignore[suppressions_justified] This is the tail of `write_item` lifted out
8080 // so there is one place a failure past the point of no return is caught, and its
8081 // arguments are exactly the values that tail already had in scope. Bundling them into a
8082 // struct would describe no concept — it would be "the arguments of this function" — and
8083 // would put the whole of `write_item`'s locals behind one more indirection.
8084 #[allow(clippy::too_many_arguments)]
8085 async fn finish_write(
8086 &self,
8087 board_id: &str,
8088 incoming: &Incoming<'_>,
8089 content_id: &NativeId,
8090 item_id: &str,
8091 content_kind: ContentKind,
8092 existing: Option<&Resolved>,
8093 origin_field: Option<&str>,
8094 origin: &str,
8095 column: Option<(String, String)>,
8096 status_target: Option<&StatusTarget>,
8097 priority: Option<&PriorityWrite>,
8098 projections: &[ProjectionWrite],
8099 native: &[String],
8100 origin_landed: &mut bool,
8101 ) -> Result<(), SourceError> {
8102 let mut fields = Vec::new();
8103 if let Some(field_id) = origin_field
8104 && existing.map_or(!origin.is_empty(), |item| {
8105 item.origin.as_deref().unwrap_or("") != origin
8106 })
8107 {
8108 fields.push((field_id.to_owned(), json!({"text":origin})));
8109 }
8110 if let Some((field_id, option_id)) = column {
8111 fields.push((field_id, json!({"singleSelectOptionId":option_id})));
8112 }
8113 let mut clears = Vec::new();
8114 match priority {
8115 Some(PriorityWrite::Select { field, option }) => {
8116 fields.push((field.clone(), json!({"singleSelectOptionId":option})));
8117 }
8118 Some(PriorityWrite::Clear { field }) => clears.push(field.as_str()),
8119 None => {}
8120 }
8121 for projection in projections {
8122 projection.join(&mut fields, &mut clears);
8123 }
8124 self.set_item_fields(board_id, item_id, &fields, &clears)
8125 .await?;
8126 *origin_landed = true;
8127
8128 // An existing issue closes in the `updateIssue` its write ends with; one created just
8129 // now closes here, once its option is selected.
8130 if existing.is_none()
8131 && content_kind == ContentKind::Issue
8132 && matches!(status_target, Some(StatusTarget::Terminal(_, _)))
8133 {
8134 self.update_content(
8135 ContentKind::Issue,
8136 content_id,
8137 json!({"stateInput":state_input(status_target)}),
8138 )
8139 .await?;
8140 }
8141
8142 if content_kind == ContentKind::Issue {
8143 self.reparent(
8144 existing.and_then(|item| item.parent.clone()),
8145 content_id,
8146 incoming.parent,
8147 )
8148 .await?;
8149 // A document takes part in no dependency graph, so writing one neither reads
8150 // nor changes the issue's own `blockedBy` relationships. Reconciling them
8151 // against the empty list a document write carries would *delete* whatever
8152 // relationships a person had made on that issue, which is a write nobody
8153 // asked for.
8154 if incoming.written.kind() != BoardKind::Document {
8155 let issue = match existing {
8156 Some(item) => Issue::Existing(item.blocked_by.as_deref()),
8157 None => Issue::Created,
8158 };
8159 self.reconcile_blocked_by(content_id, native, issue).await?;
8160 }
8161 }
8162 Ok(())
8163 }
8164
8165 /// Delete one issue, which takes its board item with it.
8166 async fn delete_issue(&self, id: &NativeId) -> Result<(), SourceError> {
8167 let data = self
8168 .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
8169 .await?;
8170 data.pointer("/deleteIssue/repository")
8171 .filter(|value| !value.is_null())
8172 .ok_or_else(|| SourceError::Malformed {
8173 message: "GitHub issue deletion returned no repository".into(),
8174 })?;
8175 self.forget(id)?;
8176 Ok(())
8177 }
8178
8179 /// Remove one item this copy created, so a copy that could not finish leaves the board
8180 /// as it found it.
8181 ///
8182 /// Deleting the issue takes its board item with it, so there is no second mutation to
8183 /// keep in step. An id the board does not hold is not an error: the item is already
8184 /// gone, which is the state this asks for. Which that is, is decided by reading the item
8185 /// by its own id — a listing of the board can still be missing an item it holds, and
8186 /// reading that as *already gone* would leave behind the very item this was asked to
8187 /// take back.
8188 async fn delete_item(&self, id: &NativeId) -> Result<(), SourceError> {
8189 let Some(item) = self.bound_item(id).await? else {
8190 return Ok(());
8191 };
8192 if item.content_kind == ContentKind::DraftIssue {
8193 return Err(SourceError::Refused {
8194 message: format!(
8195 "GitHub item {} is a draft, and this source removes an item by deleting \
8196 its issue; next: remove it from the board by hand",
8197 id.0
8198 ),
8199 });
8200 }
8201 let data = self
8202 .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
8203 .await?;
8204 data.pointer("/deleteIssue/repository")
8205 .filter(|value| !value.is_null())
8206 .ok_or_else(|| SourceError::Malformed {
8207 message: "GitHub issue deletion returned no repository".into(),
8208 })?;
8209 self.forget(id)?;
8210 Ok(())
8211 }
8212
8213 /// The issue a comment call on `task` is about, or `None` when this board holds no such
8214 /// task.
8215 ///
8216 /// Resolved exactly as [`TaskSource::get_task`] resolves it, so the comment verbs and a
8217 /// read of the task cannot disagree about which ids name one: a project or a document of
8218 /// this board is not a task here either.
8219 ///
8220 /// A **draft** is a task with nowhere to keep a comment, because GitHub keeps comments on
8221 /// issues and a draft is not one. It is refused rather than answered with an empty page,
8222 /// which would read as a task nobody has commented on yet.
8223 async fn commented_issue(&self, task: &NativeId) -> Result<Option<NativeId>, SourceError> {
8224 let cached = self.resolved_cache()?.get(task).cloned();
8225 let Some(item) = (match cached {
8226 Some(item) => Some(item),
8227 None => self.item_by_id(task).await?,
8228 })
8229 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
8230 return Ok(None);
8231 };
8232 if item.content_kind == ContentKind::DraftIssue {
8233 return Err(self.draft_has_no_comments(task));
8234 }
8235 Ok(Some(item.id))
8236 }
8237
8238 /// The refusal a comment call on a board draft is answered with: GitHub keeps comments on
8239 /// issues, and a draft is not one.
8240 fn draft_has_no_comments(&self, task: &NativeId) -> SourceError {
8241 SourceError::Refused {
8242 message: format!(
8243 "task {} of source {} is a draft item on the board, and GitHub keeps \
8244 comments on issues alone, so a draft has none to read or write; next: \
8245 convert the draft to an issue on the board, then comment on the issue it \
8246 becomes",
8247 task.0, self.name
8248 ),
8249 }
8250 }
8251
8252 /// One task and a page of its comments, read with [`graphql::ISSUE_DETAIL`] in one
8253 /// request — or `None` when this board holds no task by that id.
8254 ///
8255 /// What `task show` and a comment listing read. A draft is a task with no comments, so it
8256 /// is answered with the draft and the refusal, at the price of the draft's own read.
8257 async fn issue_detail(
8258 &self,
8259 id: &NativeId,
8260 page: &PageRequest,
8261 ) -> Result<Option<TaskDetailRead>, SourceError> {
8262 let after = page.cursor.as_ref().map(|cursor| cursor.0.as_str());
8263 let asked = self
8264 .graphql(
8265 graphql::ISSUE_DETAIL,
8266 json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),"after":after,
8267 "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
8268 "duplicates":true}),
8269 )
8270 .await;
8271 let data = match asked {
8272 Ok(data) => data,
8273 Err(error) if unresolvable_node(&error) => return Ok(None),
8274 Err(error) => return Err(error),
8275 };
8276 // `node` is null for an id that names nothing, and absent only from an answer this
8277 // source cannot read — never the same thing.
8278 let node = data.get("node").ok_or_else(|| SourceError::Malformed {
8279 message: format!("GitHub answered the read of {} with no node", id.0),
8280 })?;
8281 self.detail_of(id, node, true, after).await
8282 }
8283
8284 /// Several tasks, each with the first page of its comments when `comments` is set, read
8285 /// [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] — one answer per id, in
8286 /// order.
8287 ///
8288 /// A batch GitHub refuses because one of its ids resolves to no node at all is read again
8289 /// one item at a time, so that id is answered as missing and the others as themselves; any
8290 /// other refusal is every id of that batch's answer.
8291 async fn issue_details(
8292 &self,
8293 ids: &[NativeId],
8294 comments: Option<&PageRequest>,
8295 ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
8296 let mut read = Vec::with_capacity(ids.len());
8297 for batch in ids.chunks(DETAIL_BATCH) {
8298 match self
8299 .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, comments))
8300 .await
8301 {
8302 Ok(data) => {
8303 for (slot, id) in batch.iter().enumerate() {
8304 // Every alias asked for is answered, null for an id naming nothing;
8305 // one missing is an answer this source cannot read.
8306 let read_one = match data.get(format!("i{slot}")) {
8307 Some(node) => self.detail_of(id, node, comments.is_some(), None).await,
8308 None => Err(SourceError::Malformed {
8309 message: format!(
8310 "GitHub answered a batch read with no item for {}",
8311 id.0
8312 ),
8313 }),
8314 };
8315 read.push(read_one);
8316 }
8317 }
8318 Err(error) if unresolvable_node(&error) => {
8319 for id in batch {
8320 read.push(match comments {
8321 Some(page) => self.issue_detail(id, page).await,
8322 None => self.task_read(id).await,
8323 });
8324 }
8325 }
8326 Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
8327 }
8328 }
8329 read
8330 }
8331
8332 /// One task and nothing of its comments, as [`TaskSource::get_task`] reads it.
8333 async fn task_read(&self, id: &NativeId) -> Result<Option<TaskDetailRead>, SourceError> {
8334 Ok(self.get_task(id).await?.map(|task| TaskDetailRead {
8335 task,
8336 comments: None,
8337 }))
8338 }
8339
8340 /// What one node a detail read reached says: the task this board holds by `id`, with the
8341 /// page of comments the node carries when `commented` — or `None` for a node that is no
8342 /// task of this board.
8343 ///
8344 /// Resolved as [`Self::item_by_id`] resolves an item: a draft is read again as a draft,
8345 /// and an item this process created answers from this process's own record, which a node
8346 /// read taken moments after the write can still be behind.
8347 async fn detail_of(
8348 &self,
8349 id: &NativeId,
8350 node: &Value,
8351 commented: bool,
8352 after: Option<&str>,
8353 ) -> Result<Option<TaskDetailRead>, SourceError> {
8354 if node.is_null() {
8355 return Ok(None);
8356 }
8357 let draft = optional_str(node, "__typename")? == Some("DraftIssue");
8358 // An issue answered under one id is that id's, or the answer is not one this source
8359 // can report: reporting another issue's task and comments under the qualified id asked
8360 // for would be the one wrong answer here. A draft's own read checks the same.
8361 if !draft
8362 && optional_str(node, "__typename")? == Some("Issue")
8363 && required_str(node, "id")? != id.0
8364 {
8365 return Err(SourceError::Malformed {
8366 message: format!(
8367 "GitHub answered the read of {} with issue {}",
8368 id.0,
8369 required_str(node, "id")?
8370 ),
8371 });
8372 }
8373 let item = if draft {
8374 self.draft_by_id(id).await?
8375 } else {
8376 self.resolve_issue(node).await?
8377 };
8378 let Some(item) = item.filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
8379 return Ok(None);
8380 };
8381 let own = self.created()?.iter().find(|own| own.id == *id).cloned();
8382 let task = own.unwrap_or(item).task()?;
8383 let comments = match (commented, draft) {
8384 (false, _) => None,
8385 (true, true) => Some(Err(self.draft_has_no_comments(id))),
8386 (true, false) => Some(comment_page(node, &id.0, after).map(Some)),
8387 };
8388 Ok(Some(TaskDetailRead { task, comments }))
8389 }
8390
8391 /// Whether the comment `comment` is one of `issue`'s own.
8392 ///
8393 /// Read before an edit or a removal is sent, because GitHub's comment mutations take the
8394 /// comment's id and nothing else: a comment id given against the wrong task would
8395 /// otherwise change a comment on some other issue entirely. An id that names nothing, or
8396 /// names something that is not an issue comment, is a comment this task does not have —
8397 /// which is what GitHub refusing to resolve it means too.
8398 async fn comment_is_on(
8399 &self,
8400 issue: &NativeId,
8401 comment: &NativeId,
8402 ) -> Result<bool, SourceError> {
8403 let asked = self
8404 .graphql(graphql::COMMENT_ISSUE, json!({"id":comment.0}))
8405 .await;
8406 let data = match asked {
8407 Ok(data) => data,
8408 Err(error) if unresolvable_node(&error) => return Ok(false),
8409 Err(error) => return Err(error),
8410 };
8411 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
8412 return Ok(false);
8413 };
8414 if optional_str(node, "__typename")? != Some("IssueComment") {
8415 return Ok(false);
8416 }
8417 let on = node.get("issue").ok_or_else(|| SourceError::Malformed {
8418 message: format!("GitHub issue comment {} names no issue", comment.0),
8419 })?;
8420 Ok(required_str(on, "id")? == issue.0)
8421 }
8422
8423 /// Which far ends this item's own `blockedBy` relationship holds, and which it cannot.
8424 async fn partition_edges(
8425 &self,
8426 near_kind: BoardKind,
8427 near_content: ContentKind,
8428 carried: Option<&[Value]>,
8429 depends_on: &[DependencyEdge],
8430 ) -> Result<(Vec<String>, Vec<DependencyEdge>), SourceError> {
8431 let mut native = Vec::new();
8432 let mut fallback = Vec::new();
8433 let far_ends: Vec<(&DependencyEdge, &str, bool, Option<&Value>)> = depends_on
8434 .iter()
8435 .map(|edge| {
8436 let same_source = edge
8437 .to
8438 .source()
8439 .is_none_or(|source| source == self.name.as_str());
8440 // A qualified id's source segment runs to its *first* colon — `GlobalId` and
8441 // `DependencyEndpoint::source` both read it that way — and a native id may hold
8442 // colons of its own, so the far end is everything after that one separator.
8443 // Splitting at the last would truncate `work:urn:task:7` to `7`.
8444 let far_id = if edge.to.is_qualified() {
8445 edge.to
8446 .id()
8447 .split_once(':')
8448 .map_or(edge.to.id(), |(_, native)| native)
8449 } else {
8450 edge.to.id()
8451 };
8452 // One that already blocks the near issue was answered by that issue's own
8453 // read, which carried each of its blockers' kinds — an issue every one — so it
8454 // is not read again.
8455 let blocking = carried.and_then(|nodes| {
8456 nodes
8457 .iter()
8458 .find(|node| node.get("id").and_then(Value::as_str) == Some(far_id))
8459 });
8460 (edge, far_id, same_source, blocking)
8461 })
8462 .collect();
8463 // Every other same-source far end is read by its own id, exactly as the item it is a
8464 // far end of is: whether this board holds it is that read's answer, never a listing's.
8465 // They are read together, [`DETAIL_BATCH`] to a request, rather than one each.
8466 let mut unread: Vec<NativeId> = Vec::new();
8467 for (_, far_id, same_source, blocking) in &far_ends {
8468 let id = NativeId((*far_id).to_owned());
8469 if *same_source && blocking.is_none() && !unread.contains(&id) {
8470 unread.push(id);
8471 }
8472 }
8473 let read: BTreeMap<NativeId, Option<Resolved>> = unread
8474 .iter()
8475 .cloned()
8476 .zip(self.items_by_ids(&unread).await?)
8477 .collect();
8478 for (edge, far_id, same_source, blocking) in far_ends {
8479 let far = match (same_source, blocking) {
8480 (false, _) => None,
8481 (true, Some(node)) => Some(FarEnd {
8482 kind: if required_str(node, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
8483 BoardKind::Document
8484 } else {
8485 BoardKind::Work(related_kind(node)?)
8486 },
8487 content_kind: ContentKind::Issue,
8488 }),
8489 (true, None) => {
8490 let read = read
8491 .get(&NativeId(far_id.to_owned()))
8492 .cloned()
8493 .flatten()
8494 .ok_or_else(|| SourceError::Refused {
8495 message: format!("GitHub dependency item {far_id} was not found"),
8496 })?;
8497 Some(FarEnd {
8498 kind: read.kind,
8499 content_kind: read.content_kind,
8500 })
8501 }
8502 };
8503 let far = far.as_ref();
8504 // The caller says which kind the far end is, and this board holds the far end
8505 // itself, so a disagreement is settled here rather than stored: recorded, the
8506 // wrong kind would read back as a cross-level edge that never existed; written
8507 // natively, it would name a relationship of a different level than the caller
8508 // asked for.
8509 //
8510 // A far end this board holds as a *document* fails the same comparison and is
8511 // refused by the same sentence: `ItemKind` has no document variant because
8512 // nothing may point at one, so no caller can name it correctly and the refusal
8513 // is the only honest answer.
8514 if let Some(disagreeing) = far.filter(|far| far.kind != BoardKind::Work(edge.to.kind)) {
8515 return Err(SourceError::Refused {
8516 message: format!(
8517 "GitHub dependency item {far_id} is a {} of this board, and this item \
8518 names it as a {}; record the kind it is",
8519 disagreeing.kind.describes(),
8520 edge.to.kind.marker()
8521 ),
8522 });
8523 }
8524 // A draft has neither `blockedBy` nor `blocking`, so no edge of one is native
8525 // however the far end is spelled — and one classified native here would be
8526 // written nowhere at all, because a draft's native reconciliation never runs.
8527 let native_here = near_content == ContentKind::Issue
8528 && far.is_some_and(|far| {
8529 far.content_kind == ContentKind::Issue
8530 && BoardKind::Work(edge.to.kind) == near_kind
8531 });
8532 if native_here {
8533 native.push(far_id.to_owned());
8534 } else {
8535 fallback.push(edge.clone());
8536 }
8537 }
8538 Ok((native, fallback))
8539 }
8540
8541 async fn update_existing(
8542 &self,
8543 item: &Resolved,
8544 incoming: &Incoming<'_>,
8545 body: &Option<String>,
8546 status_target: Option<&StatusTarget>,
8547 ) -> Result<(), SourceError> {
8548 let title = incoming.written_title();
8549 // A terminal status closes the issue here, in the same mutation as its body: its board
8550 // option was selected before this, so a close never lands on an item whose board cannot
8551 // show it.
8552 let fields = match item.content_kind {
8553 ContentKind::DraftIssue => json!({"title":title,"body":body}),
8554 ContentKind::Issue => json!({"title":title,"body":body,
8555 "stateInput":state_input(status_target)}),
8556 };
8557 self.update_content(item.content_kind, &item.id, fields)
8558 .await
8559 }
8560
8561 /// Update one board item's content with exactly `fields` beside its id, through the
8562 /// mutation its kind takes: `updateIssue` for an issue, `updateProjectV2DraftIssue` for
8563 /// a draft.
8564 ///
8565 /// Every input field either mutation leaves out is a field GitHub leaves as it is, which
8566 /// is what lets a narrow write carry the one thing it changes and nothing else.
8567 async fn update_content(
8568 &self,
8569 kind: ContentKind,
8570 id: &NativeId,
8571 fields: Value,
8572 ) -> Result<(), SourceError> {
8573 let (operation, id_key, pointer) = match kind {
8574 ContentKind::DraftIssue => (
8575 graphql::UPDATE_DRAFT,
8576 "draftIssueId",
8577 "/updateProjectV2DraftIssue/draftIssue",
8578 ),
8579 ContentKind::Issue => (graphql::UPDATE_ISSUE, "id", "/updateIssue/issue"),
8580 };
8581 let mut input = fields;
8582 input[id_key] = json!(id.0);
8583 let data = self.graphql(operation, json!({"input":input})).await?;
8584 let returned = data
8585 .pointer(pointer)
8586 .ok_or_else(|| SourceError::Malformed {
8587 message: "GitHub item update returned no item".into(),
8588 })?;
8589 if required_str(returned, "id")? != id.0 {
8590 return Err(SourceError::Malformed {
8591 message: "GitHub item update returned the wrong item".into(),
8592 });
8593 }
8594 Ok(())
8595 }
8596
8597 /// Creates one issue, files it on the board, and reports what a read of it would say:
8598 /// its content id, its board item id, and the web address GitHub gave it.
8599 ///
8600 /// Two calls rather than one: `createIssue` answers with an issue that is on no board,
8601 /// and `addProjectV2ItemById` is what puts it there. Filing it at creation through
8602 /// `CreateIssueInput.projectV2Ids` was tried and is not done: GitHub answered with no
8603 /// board item, and the `addProjectV2ItemById` that then had to follow was refused
8604 /// "Content already exists in this project". A terminal status is not written here:
8605 /// `finish_write` selects its option first and closes the issue after, so a close never
8606 /// lands on an item whose board cannot show it.
8607 ///
8608 /// The address and the number come back here because this is the only place either is
8609 /// known before GitHub's own board read catches up — an item this run created answers
8610 /// the reads that follow it out of the record below, and one remembered without them
8611 /// would report no location and no key for the rest of the run.
8612 async fn create_and_file_issue(
8613 &self,
8614 board_id: &str,
8615 repository: &RepositoryTarget,
8616 incoming: &Incoming<'_>,
8617 body: &Option<String>,
8618 ) -> Result<Landed, SourceError> {
8619 let repository_id = self.repository_id(repository, incoming).await?;
8620 let data = self
8621 .graphql(
8622 graphql::CREATE_ISSUE,
8623 json!({"input":{
8624 "repositoryId":repository_id,"title":incoming.written_title(),"body":body
8625 }}),
8626 )
8627 .await?;
8628 let created = data
8629 .pointer("/createIssue/issue")
8630 .filter(|value| !value.is_null())
8631 .ok_or_else(|| SourceError::Malformed {
8632 message: "GitHub issue creation returned no issue".into(),
8633 })?;
8634 let content_id = NativeId(required_str(created, "id")?.to_owned());
8635 // Optional although GitHub's schema makes it non-null: the issue exists by now, so
8636 // a response without it is not worth failing a landed write over — the item simply
8637 // reports no location until the board read catches up, which is what it did before.
8638 let url = optional_str(created, "url")?.map(str::to_owned);
8639 // The issue exists from here on, so an unreadable number and a refused board
8640 // filing below each try, best effort, to take it back: an issue in the repository
8641 // that is on no board is an item nobody asked for and nothing here would find again.
8642 //
8643 // Its number is optional on the same terms its address is — a landed write is not
8644 // worth failing over a member that came back missing, and such an item reports no
8645 // handle until a board read catches up. A number that is *present* and is not an
8646 // unsigned integer is still a response this source cannot read.
8647 let number = match created_issue_number(created) {
8648 Ok(number) => number,
8649 Err(error) => {
8650 let _ = self.delete_issue(&content_id).await;
8651 return Err(error);
8652 }
8653 };
8654 let added = match self
8655 .graphql(
8656 graphql::ADD_TO_BOARD,
8657 json!({"input":{"projectId":board_id,"contentId":content_id.0}}),
8658 )
8659 .await
8660 {
8661 Ok(added) => added,
8662 Err(error) => {
8663 let _ = self.delete_issue(&content_id).await;
8664 return Err(error);
8665 }
8666 };
8667 let item = added
8668 .pointer("/addProjectV2ItemById/item")
8669 .filter(|value| !value.is_null())
8670 .ok_or_else(|| SourceError::Malformed {
8671 message: "GitHub board addition returned no project item".into(),
8672 })?;
8673 Ok(Landed {
8674 content_id,
8675 item_id: required_str(item, "id")?.to_owned(),
8676 url,
8677 number,
8678 })
8679 }
8680
8681 /// Move one issue under the project it now belongs to, or out of the one it left.
8682 async fn reparent(
8683 &self,
8684 held: Option<NativeId>,
8685 child: &NativeId,
8686 wanted: Option<&NativeId>,
8687 ) -> Result<(), SourceError> {
8688 if held.as_ref() == wanted {
8689 return Ok(());
8690 }
8691 if let Some(held) = &held {
8692 self.sub_issue(graphql::REMOVE_SUB_ISSUE, held, child, "removeSubIssue")
8693 .await?;
8694 }
8695 if let Some(wanted) = wanted {
8696 self.sub_issue(graphql::ADD_SUB_ISSUE, wanted, child, "addSubIssue")
8697 .await?;
8698 }
8699 Ok(())
8700 }
8701
8702 async fn sub_issue(
8703 &self,
8704 operation: &str,
8705 parent: &NativeId,
8706 child: &NativeId,
8707 root: &str,
8708 ) -> Result<(), SourceError> {
8709 let data = self
8710 .graphql(
8711 operation,
8712 json!({"input":{"issueId":parent.0,"subIssueId":child.0}}),
8713 )
8714 .await?;
8715 let issue =
8716 data.pointer(&format!("/{root}/issue"))
8717 .ok_or_else(|| SourceError::Malformed {
8718 message: "GitHub sub-issue update returned no issue".into(),
8719 })?;
8720 let sub =
8721 data.pointer(&format!("/{root}/subIssue"))
8722 .ok_or_else(|| SourceError::Malformed {
8723 message: "GitHub sub-issue update returned no sub-issue".into(),
8724 })?;
8725 if required_str(issue, "id")? != parent.0 || required_str(sub, "id")? != child.0 {
8726 return Err(SourceError::Malformed {
8727 message: "GitHub sub-issue update returned the wrong issues".into(),
8728 });
8729 }
8730 Ok(())
8731 }
8732
8733 /// Bring one issue's `blockedBy` to exactly `native`, sending only the difference, and say
8734 /// whether there was one.
8735 ///
8736 /// An issue [`Issue::Created`] by this very write is blocked by nothing yet, so its
8737 /// relationships are not read: there is nothing a read of them could find.
8738 async fn reconcile_blocked_by(
8739 &self,
8740 content_id: &NativeId,
8741 native: &[String],
8742 issue: Issue<'_>,
8743 ) -> Result<bool, SourceError> {
8744 let current = match issue {
8745 Issue::Created => Vec::new(),
8746 Issue::Existing(Some(held)) => held
8747 .iter()
8748 .map(|far| required_str(far, "id").map(str::to_owned))
8749 .collect::<Result<Vec<_>, _>>()?,
8750 Issue::Existing(None) => self.native_dependency_ids(content_id).await?,
8751 };
8752 let mut changed = false;
8753 for (operation, far_id) in current
8754 .iter()
8755 .filter(|id| !native.contains(id))
8756 .map(|id| (graphql::REMOVE_BLOCKED_BY, id))
8757 .chain(
8758 native
8759 .iter()
8760 .filter(|id| !current.contains(id))
8761 .map(|id| (graphql::ADD_BLOCKED_BY, id)),
8762 )
8763 {
8764 let data = self
8765 .graphql(
8766 operation,
8767 json!({"input":{"issueId":content_id.0,"blockingIssueId":far_id}}),
8768 )
8769 .await?;
8770 let root = if operation == graphql::ADD_BLOCKED_BY {
8771 "addBlockedBy"
8772 } else {
8773 "removeBlockedBy"
8774 };
8775 let issue =
8776 data.pointer(&format!("/{root}/issue"))
8777 .ok_or_else(|| SourceError::Malformed {
8778 message: "GitHub dependency update returned no issue".into(),
8779 })?;
8780 let blocker = data
8781 .pointer(&format!("/{root}/blockingIssue"))
8782 .ok_or_else(|| SourceError::Malformed {
8783 message: "GitHub dependency update returned no blocking issue".into(),
8784 })?;
8785 if required_str(issue, "id")? != content_id.0 || required_str(blocker, "id")? != far_id
8786 {
8787 return Err(SourceError::Malformed {
8788 message: "GitHub dependency update returned the wrong issues".into(),
8789 });
8790 }
8791 changed = true;
8792 }
8793 Ok(changed)
8794 }
8795}
8796
8797/// What a write needs to know of one far end it names: which kind of item it is, and whether
8798/// it is an issue a native relationship can name.
8799struct FarEnd {
8800 kind: BoardKind,
8801 content_kind: ContentKind,
8802}
8803
8804/// Whether the issue one write reconciles was created by that write or was already there.
8805#[derive(Clone, Copy, PartialEq, Eq)]
8806enum Issue<'a> {
8807 /// Created by this write, so it holds no relationships yet.
8808 Created,
8809 /// On the board before this write, holding whatever relationships it holds — the far
8810 /// ends of its whole `blockedBy`, when the read that reached it carried them.
8811 Existing(Option<&'a [Value]>),
8812}
8813
8814/// What resolving one node id reached; see [`GitHubProjectsSource::reach`].
8815enum Reached {
8816 /// An issue this board holds, resolved into everything this source reports about it.
8817 Held(Box<Resolved>),
8818 /// Nothing this board holds: no such node, or a node on some other board.
8819 Nothing,
8820 /// A board draft, which [`graphql::ISSUE`] reaches and reads nothing of, so it is read
8821 /// again by [`GitHubProjectsSource::draft_by_id`].
8822 Draft,
8823}
8824
8825/// What GitHub says when a string is not a node id it can resolve.
8826///
8827/// Matched because it is the ordinary answer to a project selector naming a project by its
8828/// *name*, and reporting that as a failure would make naming one impossible. It is read
8829/// off the refusal GitHub sent, never guessed from the shape of the string: this source
8830/// does not define the syntax of a GitHub node id and would be wrong about it.
8831const UNRESOLVABLE_NODE: &str = "could not resolve to a node";
8832
8833/// `error` with `note` added to the end of what it says, its kind and every other member
8834/// unchanged — so a caller still branches on the failure that happened, and reads beside it
8835/// what that failure left behind.
8836fn noting(error: SourceError, note: &str) -> SourceError {
8837 match error {
8838 SourceError::Config { message } => SourceError::Config {
8839 message: message + note,
8840 },
8841 SourceError::Auth { message } => SourceError::Auth {
8842 message: message + note,
8843 },
8844 SourceError::Refused { message } => SourceError::Refused {
8845 message: message + note,
8846 },
8847 SourceError::RateLimited {
8848 retry_after_seconds,
8849 message,
8850 } => SourceError::RateLimited {
8851 retry_after_seconds,
8852 message: Some(message.unwrap_or_default() + note),
8853 },
8854 SourceError::Unavailable { message } => SourceError::Unavailable {
8855 message: message + note,
8856 },
8857 SourceError::Malformed { message } => SourceError::Malformed {
8858 message: message + note,
8859 },
8860 }
8861}
8862
8863/// The variables of one [`graphql::ISSUE_DETAILS`] request over `batch` — at most
8864/// [`DETAIL_BATCH`] ids — each item with the first page of its comments when `comments` asks
8865/// for them.
8866///
8867/// The document is fixed-size, so a slot `batch` has no id for is bound to its last id, which
8868/// is read again at no added price.
8869fn detail_batch(batch: &[NativeId], comments: Option<&PageRequest>) -> Value {
8870 let mut variables = serde_json::Map::new();
8871 for slot in 0..DETAIL_BATCH {
8872 let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
8873 variables.insert(format!("id{slot}"), json!(id));
8874 }
8875 variables.insert(
8876 "first".to_owned(),
8877 json!(comments.map_or(MAX_PAGE_SIZE, |page| page.limit.min(MAX_PAGE_SIZE))),
8878 );
8879 variables.insert("comments".to_owned(), json!(comments.is_some()));
8880 variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
8881 variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
8882 variables.insert("duplicates".to_owned(), json!(true));
8883 Value::Object(variables)
8884}
8885
8886/// Whether this refusal is GitHub saying the id names no node at all.
8887fn unresolvable_node(error: &SourceError) -> bool {
8888 matches!(error, SourceError::Refused { message }
8889 if message.to_ascii_lowercase().contains(UNRESOLVABLE_NODE))
8890}
8891
8892/// One project name, as a search qualifier which filters on it at the server.
8893///
8894/// Quoted so the whole title is one phrase rather than a bag of words, with the two
8895/// characters GitHub's own quoting grammar gives a meaning inside a quoted phrase escaped
8896/// the way it documents. A title matched here is still compared for equality afterwards:
8897/// the qualifier narrows what the server sends, and this source decides what it names.
8898fn title_qualifier(name: &str) -> String {
8899 format!("in:title {}", quoted(name))
8900}
8901
8902/// `value` as one quoted phrase of a GitHub search or a board filter, with the two
8903/// characters GitHub's quoting grammar gives a meaning inside a quoted phrase escaped the way
8904/// it documents — so a value holding a qualifier's spelling is searched for rather than
8905/// obeyed.
8906fn quoted(value: &str) -> String {
8907 let escaped = value.replace('\\', "\\\\").replace('"', "\\\"");
8908 format!("\"{escaped}\"")
8909}
8910
8911/// The search qualifier for the issues updated at or after `since`.
8912///
8913/// Written to the second, rounded down, which can only widen what the search returns.
8914fn updated_qualifier(since: DateTime<Utc>) -> String {
8915 format!("updated:>={}", since.format("%Y-%m-%dT%H:%M:%S+00:00"))
8916}
8917
8918/// The search terms that narrow a board-scoped issue search to a task query's text and
8919/// metadata predicates, or `None` when it carries neither.
8920///
8921/// The text is one quoted phrase, searched `in:title`, `in:body` or both as its fields say,
8922/// and each metadata value is one more quoted phrase, which GitHub finds in the body because
8923/// its index covers the metadata comment the value is stored in. GitHub ANDs the phrases and
8924/// matches each in any field the `in:` qualifier names, so a query naming a title search and
8925/// a metadata value searches both fields for both — wider than asked, never narrower, and
8926/// every candidate is confirmed in process afterwards.
8927///
8928/// **This narrows a text search, and that is this source's declared semantics.** GitHub
8929/// matches whole tokens where a substring rule would match inside a word, so an item holding
8930/// the text only inside a longer word is not returned. A text of nothing but whitespace
8931/// matches every item, so it narrows nothing and is not sent.
8932fn narrowing_qualifiers(query: &TaskQuery) -> Option<String> {
8933 let text = query
8934 .text
8935 .as_ref()
8936 .filter(|text| !text.terms.trim().is_empty());
8937 if text.is_none() && query.metadata.is_empty() {
8938 return None;
8939 }
8940 let (title, body) = match text.map(|text| text.fields) {
8941 None => (false, true),
8942 Some(TextFields::Title) => (true, !query.metadata.is_empty()),
8943 Some(TextFields::Content) => (false, true),
8944 Some(TextFields::TitleOrContent) => (true, true),
8945 };
8946 let fields = match (title, body) {
8947 (true, true) => "in:title,body",
8948 (true, false) => "in:title",
8949 _ => "in:body",
8950 };
8951 let phrases = text
8952 .map(|text| text.terms.clone())
8953 .into_iter()
8954 .chain(
8955 query
8956 .metadata
8957 .iter()
8958 .map(|wanted| as_stored(wanted.value())),
8959 )
8960 .map(|phrase| quoted(&phrase))
8961 .collect::<Vec<_>>();
8962 Some(format!("{fields} {}", phrases.join(" ")))
8963}
8964
8965/// The search terms that narrow a board-scoped issue search to a project or document query's
8966/// text, or `None` when it has none or a blank one: the phrase, in the fields, a task query
8967/// carrying that text alone is sent as by [`narrowing_qualifiers`].
8968fn text_qualifiers(text: Option<&TextQuery>) -> Option<String> {
8969 narrowing_qualifiers(&TaskQuery {
8970 text: text.cloned(),
8971 ..TaskQuery::default()
8972 })
8973}
8974
8975/// Refuses a project or document query's text GitHub's issue search cannot find, before
8976/// anything is asked of GitHub, on exactly the terms [`refuse_unsearchable`] refuses a task
8977/// query's.
8978fn refuse_unsearchable_text(text: Option<&TextQuery>) -> Result<(), SourceError> {
8979 refuse_unsearchable(&TaskQuery {
8980 text: text.cloned(),
8981 ..TaskQuery::default()
8982 })
8983}
8984
8985/// Refuses a task query naming a text or a metadata value GitHub's issue search cannot find,
8986/// before anything is asked of GitHub.
8987///
8988/// GitHub's index holds words, so a phrase with no letter or digit names none to find, and no
8989/// bounded query answers it: sent, GitHub's answer to it is nothing this source may rely on;
8990/// left out, the search is every issue of the board. So this source says it cannot answer
8991/// rather than reading the board or answering nothing. A blank text is not refused: it narrows
8992/// nothing GitHub could search for, and keeps the board read it always had.
8993fn refuse_unsearchable(query: &TaskQuery) -> Result<(), SourceError> {
8994 const WHY: &str = "GitHub's issue search indexes words, so it cannot answer a value with no \
8995 letter or digit with a bounded query";
8996 if let Some(text) = &query.text
8997 && !text.terms.trim().is_empty()
8998 && !has_words(&text.terms)
8999 {
9000 return Err(SourceError::Refused {
9001 message: format!(
9002 "cannot search for the text {:?}: {WHY}; search for a text holding a letter or a digit",
9003 text.terms
9004 ),
9005 });
9006 }
9007 if let Some(wanted) = query
9008 .metadata
9009 .iter()
9010 .find(|wanted| !has_words(wanted.value()))
9011 {
9012 return Err(SourceError::Refused {
9013 message: format!(
9014 "cannot filter by the metadata value {:?} at {:?}: {WHY}; filter by a value holding a letter or a digit",
9015 wanted.value(),
9016 std::iter::once(wanted.key())
9017 .chain(wanted.path().iter().map(String::as_str))
9018 .collect::<Vec<_>>()
9019 .join("/"),
9020 ),
9021 });
9022 }
9023 Ok(())
9024}
9025
9026/// Whether GitHub's index could hold a word of `phrase`: whether it has a letter or a digit.
9027fn has_words(phrase: &str) -> bool {
9028 phrase.chars().any(char::is_alphanumeric)
9029}
9030
9031/// `value` spelled the way the metadata slot stores it: as the inside of its JSON string.
9032///
9033/// What GitHub indexes is the slot's JSON text, so a value holding a character JSON escapes —
9034/// a newline, a tab, a quote — is found by the escape the body holds and not by the character,
9035/// which GitHub's word match would read as different words.
9036fn as_stored(value: &str) -> String {
9037 let encoded = Value::String(value.to_owned()).to_string();
9038 encoded[1..encoded.len() - 1].to_owned()
9039}
9040
9041/// The one narrower question a task query carrying a text, metadata or origin predicate is
9042/// sent as.
9043enum Narrowing {
9044 /// Every carrier of this origin: [`graphql::ORIGIN_LOOKUP`].
9045 Origin(String),
9046 /// The board-scoped issue search narrowed by these qualifiers.
9047 Search(String),
9048}
9049
9050impl Narrowing {
9051 /// What this question is remembered under for the length of one command.
9052 fn key(&self) -> String {
9053 match self {
9054 Self::Origin(origin) => format!("origin {origin}"),
9055 Self::Search(also) => format!("search {also}"),
9056 }
9057 }
9058}
9059
9060/// Where one connection of [`graphql::ORIGIN_LOOKUP`] resumes.
9061enum Resumed {
9062 /// It reported another page, which starts after this cursor.
9063 More(String),
9064 /// It has ended. Sending this cursor again — the page's own end when it had one, and
9065 /// otherwise the cursor it was reached from — answers an empty page, so the one document
9066 /// can go on walking the other connection.
9067 Ended(Option<String>),
9068}
9069
9070impl Resumed {
9071 /// Whether the connection has another page.
9072 const fn has_more(&self) -> bool {
9073 matches!(self, Self::More(_))
9074 }
9075
9076 /// The cursor to send this connection next.
9077 fn cursor(self) -> Option<String> {
9078 match self {
9079 Self::More(next) => Some(next),
9080 Self::Ended(last) => last,
9081 }
9082 }
9083}
9084
9085/// Where `connection`, reached from `after`, resumes — refused when it reports another page
9086/// with no cursor to it, or from a cursor that does not advance.
9087fn resumed(connection: &Value, after: Option<&str>) -> Result<Resumed, SourceError> {
9088 let info = connection
9089 .get("pageInfo")
9090 .ok_or_else(|| SourceError::Malformed {
9091 message: "GitHub connection has no pageInfo".into(),
9092 })?;
9093 let end = optional_str(info, "endCursor")?;
9094 if required_bool(info, "hasNextPage")? {
9095 let next = end.ok_or_else(|| SourceError::Malformed {
9096 message: "GitHub connection reports another page and no endCursor".into(),
9097 })?;
9098 validate_cursor_progress(after, next)?;
9099 return Ok(Resumed::More(next.to_owned()));
9100 }
9101 Ok(Resumed::Ended(
9102 end.map(str::to_owned).or_else(|| after.map(str::to_owned)),
9103 ))
9104}
9105
9106/// The board, and every item on it this source reports.
9107#[derive(Clone)]
9108struct Board {
9109 id: String,
9110 fields: Value,
9111 items: Vec<Resolved>,
9112}
9113
9114/// What a write needs of the board and nothing more: its node id and its field
9115/// definitions, in the shape a read of the board's own `fields` gives them.
9116///
9117/// Deliberately no items. A write decides which item it writes, which parent it files
9118/// under and which far ends it names by reading each of them by its own id; this is the
9119/// half of the board those reads cannot carry, and holding no item is what keeps it from
9120/// ever being asked whether an item is there.
9121#[derive(Clone)]
9122struct BoardFields {
9123 id: BoardId,
9124 fields: Value,
9125}
9126
9127/// A board's node id: what a field write and `addProjectV2ItemById` address.
9128///
9129/// Never blank, because a blank one addresses no board — so an id GitHub answers blank is
9130/// refused where it is read, and one an item names blank is read as not named at all.
9131#[derive(Clone)]
9132struct BoardId(String);
9133
9134/// Where one write left its item, for the record the rest of the command reads it out of.
9135///
9136/// A named record rather than a tuple because the update arm and the create arm each fill
9137/// all four, and two `Option`s of different meaning side by side in a tuple are two
9138/// positions a reader has to count.
9139struct Landed {
9140 /// The issue's own node id, which is the [`NativeId`] this source reports.
9141 content_id: NativeId,
9142 /// The board item's id, which is what a field write addresses.
9143 // 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.
9144 item_id: String,
9145 /// The web address GitHub gave the issue, when it gave one.
9146 // 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.
9147 url: Option<String>,
9148 /// The issue's number on its repository, when GitHub reported one.
9149 number: Option<u64>,
9150}
9151
9152impl BoardId {
9153 fn parse(id: &str) -> Result<Self, SourceError> {
9154 if id.trim().is_empty() {
9155 return Err(SourceError::Malformed {
9156 message: "GitHub named a board with a blank node id".into(),
9157 });
9158 }
9159 Ok(Self(id.to_owned()))
9160 }
9161
9162 fn as_str(&self) -> &str {
9163 &self.0
9164 }
9165}
9166
9167impl Board {
9168 fn field<'a>(fields: &'a Value, name: &str) -> Result<Option<&'a Value>, SourceError> {
9169 complete_connection(fields, "project fields", NESTED_PAGE_SIZE)?;
9170 let nodes = fields
9171 .get("nodes")
9172 .and_then(Value::as_array)
9173 .ok_or_else(|| SourceError::Malformed {
9174 message: "GitHub project fields.nodes is not an array".into(),
9175 })?;
9176 Ok(nodes
9177 .iter()
9178 .find(|field| field.get("name").and_then(Value::as_str) == Some(name)))
9179 }
9180}
9181
9182/// One board item, resolved into everything this source reports about it.
9183#[derive(Clone)]
9184struct Resolved {
9185 item_id: String,
9186 id: NativeId,
9187 content_kind: ContentKind,
9188 kind: BoardKind,
9189 title: String,
9190 body: Option<String>,
9191 /// The body exactly as GitHub holds it, metadata slot and all, which is what a write
9192 /// that changes the slot alone has to keep byte for byte outside it.
9193 raw_body: Option<String>,
9194 status: Status,
9195 /// The name of the board `Status` option this item sits in, as the board spells it.
9196 option: Option<String>,
9197 /// What its `Priority` field says, read through this instance's mapping.
9198 priority: HeldPriority,
9199 /// Whether this item's issue is closed. A draft has no such state and is never closed.
9200 closed: bool,
9201 /// The tasks this one delivers, read out of its slot. Empty for anything not a task.
9202 delivers: Vec<TaskRef>,
9203 /// Every task that delivers this one, read out of its slot. Empty for anything not a
9204 /// task.
9205 delivered_by: Vec<TaskRef>,
9206 labels: Vec<Label>,
9207 parent: Option<NativeId>,
9208 // 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.
9209 origin: Option<String>,
9210 /// What each board text field this instance projects metadata onto holds for this item,
9211 /// by field name — only the fields holding a value, an empty text included. What a write compares the
9212 /// item's metadata against, so a field already holding its value is not written again;
9213 /// never read back as metadata, which the slot alone reports.
9214 projected: BTreeMap<String, String>,
9215 /// The issue's own number on its repository, as GitHub reports it.
9216 ///
9217 /// `None` in exactly two cases: a draft, which has no number at all — `DraftIssue`
9218 /// declares none, and a draft is not filed in a repository to be numbered by one — and
9219 /// an issue this run created whose creating mutation answered without one, which is a
9220 /// response GitHub's own schema says cannot happen and which a landed write is not
9221 /// worth failing over. An `Issue` read off the board always has one.
9222 number: Option<u64>,
9223 url: Option<String>,
9224 created_at: Option<DateTime<Utc>>,
9225 updated_at: Option<DateTime<Utc>>,
9226 own_repository: Option<Repository>,
9227 repositories: Vec<Repository>,
9228 classification: Classification,
9229 slot: BTreeMap<String, Value>,
9230 /// The node id of the board this item sits on, when the read that reached it said.
9231 board_id: Option<String>,
9232 /// The definition of every board field this item holds a value of, in the shape a read
9233 /// of the board's own `fields` gives one.
9234 ///
9235 /// Only the fields this item has a value in: a field it holds nothing of is not here,
9236 /// which says nothing about whether the board has it.
9237 fields: Vec<Value>,
9238 /// Every field the board this item sits on defines, as its own read of the board's
9239 /// `fields` gives them — when the read that reached the item carried them, which a read
9240 /// of it by its own id does. What a write of it needs of the board, then, needs no read
9241 /// of the board.
9242 board_fields: Option<Value>,
9243 /// The far ends of this issue's whole `blockedBy` connection, each as a dependency read
9244 /// selects one — when the read that reached it carried the connection to its end, which a
9245 /// read of it by its own id does for any issue blocked by no more than a page. What a
9246 /// write reconciles that relationship against, and what a read of its forward edges in
9247 /// the same command answers with.
9248 blocked_by: Option<Vec<Value>>,
9249}
9250
9251impl Resolved {
9252 /// The board this item's own read names it on, when that read named one this source can
9253 /// address.
9254 fn named_board(&self) -> Option<BoardId> {
9255 self.board_id
9256 .as_deref()
9257 .and_then(|id| BoardId::parse(id).ok())
9258 }
9259
9260 /// The board's id and every field it defines, when the read that reached this item
9261 /// carried both — which a read of it by its own id does.
9262 fn carried_board(&self) -> Option<BoardFields> {
9263 Some(BoardFields {
9264 id: self.named_board()?,
9265 fields: self.board_fields.clone()?,
9266 })
9267 }
9268
9269 /// Whether this item holds a value of the board field called `name`, and so carries
9270 /// that field's definition. `false` says nothing about whether the board has the field.
9271 fn defines(&self, name: &str) -> bool {
9272 self.fields
9273 .iter()
9274 .any(|field| field.get("name").and_then(Value::as_str) == Some(name))
9275 }
9276
9277 /// The metadata a caller sees: their own keys, plus the copy origin this source keeps
9278 /// in a field of its own, and none of the five keys that are only an encoding.
9279 ///
9280 /// The two delivery keys are left out for every kind, not only for a task: they are
9281 /// the encoding of [`Task::delivers`] and [`Task::delivered_by`], and a project or a
9282 /// document carrying one holds nothing a caller's own metadata could mean by it.
9283 fn metadata(&self) -> BTreeMap<String, Value> {
9284 let mut metadata = self.slot.clone();
9285 metadata.remove(Repository::METADATA_KEY);
9286 metadata.remove(DependencyEdge::RECORDED_KEY);
9287 metadata.remove(ItemKind::METADATA_KEY);
9288 metadata.remove(TaskRef::DELIVERS_KEY);
9289 metadata.remove(TaskRef::DELIVERED_BY_KEY);
9290 metadata.remove(Classification::METADATA_KEY);
9291 // The board field is the origin, and the body's copy of it is only a mirror for the
9292 // issue search to find: an item whose field holds none has none, whatever its body
9293 // says, so no reader ever sees two answers.
9294 metadata.remove(ORIGIN_KEY);
9295 if let Some(origin) = &self.origin {
9296 metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin.clone()));
9297 }
9298 metadata
9299 }
9300
9301 /// Where this item is, as a link a reader can open.
9302 ///
9303 /// A board is a hosted place and every issue on it has a web address, so that address
9304 /// is what "where is this?" means here — and [`Location::Url`] is what says which kind
9305 /// of place it is, so a reader knows to open it rather than to read a file out. It
9306 /// does not replace or derive from `url`: the field goes on reporting exactly what it
9307 /// reported before, and this says what that address *is*.
9308 ///
9309 /// An item GitHub gave no `url` for — a draft has none — reports no location at all
9310 /// rather than a third variant, which is the contract's "the source did not say". An
9311 /// issue this run created is not one of those: its address comes back from the
9312 /// creating mutation, so it is somewhere a reader can open from the moment it exists
9313 /// rather than from whenever the board read catches up.
9314 fn location(&self) -> Option<Location> {
9315 self.url.clone().map(Location::Url)
9316 }
9317
9318 /// The short handle this board's backend shows people for a task: the issue's number
9319 /// alone, as a decimal string.
9320 ///
9321 /// The number alone rather than `owner/repo#1043`, because that is the contract's
9322 /// value for this backend. A draft has no number and so no handle, which is the
9323 /// contract's *absent* rather than a handle of some other shape — and the native
9324 /// [`Task::id`] here is the issue's GraphQL node id, which this neither replaces nor
9325 /// derives from.
9326 fn key(&self) -> Option<String> {
9327 self.number.map(|number| number.to_string())
9328 }
9329
9330 /// Whether its `Priority` field holds a value at all, mapped or not.
9331 fn holds_priority(&self) -> bool {
9332 self.priority != HeldPriority::Read(Priority::None)
9333 }
9334
9335 /// The task this item is.
9336 ///
9337 /// Fails for an item whose `Priority` field holds an option the mapping does not name:
9338 /// reading that as a level would be a guess, and reading it as `none` would let the next
9339 /// copy clear a priority a person set.
9340 fn task(&self) -> Result<Task, SourceError> {
9341 let priority = match &self.priority {
9342 HeldPriority::Read(priority) => *priority,
9343 HeldPriority::Unmapped(option) => {
9344 return Err(SourceError::Malformed {
9345 message: format!(
9346 "task {}{} sits in the board {PRIORITY_FIELD} option {option:?}, which \
9347 this source's priority_mapping does not name, so its priority cannot be \
9348 read; next: name {option:?} under priority_mapping, or move the item to \
9349 a mapped option",
9350 self.id,
9351 self.number
9352 .map(|number| format!(" (#{number})"))
9353 .unwrap_or_default()
9354 ),
9355 });
9356 }
9357 };
9358 Ok(Task {
9359 id: self.id.clone(),
9360 key: self.key(),
9361 title: self.title.clone(),
9362 content: self.body.clone(),
9363 status: self.status.clone(),
9364 priority,
9365 labels: self.labels.clone(),
9366 project: self.parent.clone(),
9367 url: self.url.clone(),
9368 location: self.location(),
9369 created_at: self.created_at,
9370 updated_at: self.updated_at,
9371 metadata: self.metadata(),
9372 repositories: self.repositories.clone(),
9373 delivers: self.delivers.clone(),
9374 delivered_by: self.delivered_by.clone(),
9375 classification: self.classification,
9376 })
9377 }
9378
9379 fn project(&self) -> Project {
9380 Project {
9381 id: self.id.clone(),
9382 title: self.title.clone(),
9383 content: self.body.clone(),
9384 status: self.status.clone(),
9385 labels: self.labels.clone(),
9386 url: self.url.clone(),
9387 location: self.location(),
9388 created_at: self.created_at,
9389 updated_at: self.updated_at,
9390 metadata: self.metadata(),
9391 repositories: self.repositories.clone(),
9392 classification: self.classification,
9393 }
9394 }
9395
9396 /// The same issue as a document: the project it is filed under, and no status and no
9397 /// dependencies, because a document is not work.
9398 fn document(&self) -> Document {
9399 Document {
9400 id: self.id.clone(),
9401 title: self.title.clone(),
9402 content: self.body.clone(),
9403 project: self.parent.clone(),
9404 labels: self.labels.clone(),
9405 url: self.url.clone(),
9406 location: self.location(),
9407 created_at: self.created_at,
9408 updated_at: self.updated_at,
9409 metadata: self.metadata(),
9410 repositories: self.repositories.clone(),
9411 classification: self.classification,
9412 }
9413 }
9414}
9415
9416/// Where one targeted update moves an item's status, and which of its two halves move.
9417struct StatusMove {
9418 /// The board the item's `Status` field is on.
9419 board: BoardId,
9420 /// The `Status` field's id.
9421 field: String,
9422 /// The option's id.
9423 option: String,
9424 /// The option's name, as the board spells it.
9425 name: String,
9426 /// What the status asks of the issue's state.
9427 target: StatusTarget,
9428 /// The status the item reads as once it is there.
9429 landed: Status,
9430 /// Which of the status's two halves differ from what the item holds.
9431 moves: Moves,
9432}
9433
9434/// Which halves of an item's status one targeted update moves: its `Status` option, the open or
9435/// closed state of its issue, or both. A status neither half of which differs is no move at all,
9436/// and is not a value of this type.
9437#[derive(Clone, Copy, PartialEq, Eq)]
9438enum Moves {
9439 /// The option alone.
9440 Option,
9441 /// The issue's state alone: open, closed, or closed with another reason.
9442 State,
9443 /// Both.
9444 Both,
9445}
9446
9447impl Moves {
9448 /// What differs, or `None` when nothing does.
9449 const fn of(option: bool, state: bool) -> Option<Self> {
9450 match (option, state) {
9451 (true, true) => Some(Self::Both),
9452 (true, false) => Some(Self::Option),
9453 (false, true) => Some(Self::State),
9454 (false, false) => None,
9455 }
9456 }
9457
9458 /// Whether the option moves.
9459 const fn option(self) -> bool {
9460 matches!(self, Self::Option | Self::Both)
9461 }
9462
9463 /// Whether the issue's state moves.
9464 const fn state(self) -> bool {
9465 matches!(self, Self::State | Self::Both)
9466 }
9467}
9468
9469/// What one write is, and the status that comes with being it.
9470///
9471/// One value rather than a [`BoardKind`] beside an `Option<Status>`: a document has no
9472/// status and a task or a project always has one, so "a document carrying a status" and
9473/// "a task carrying none" are states a write cannot be in rather than states every use
9474/// site below has to defend against.
9475enum Written<'a> {
9476 /// A document, which is not work and so has no status at all.
9477 Document,
9478 /// A task or a project, and the status it is being written with.
9479 Work(ItemKind, &'a Status),
9480}
9481
9482impl Written<'_> {
9483 /// Which of the board's three kinds this write is.
9484 const fn kind(&self) -> BoardKind {
9485 match self {
9486 Self::Document => BoardKind::Document,
9487 Self::Work(kind, _) => BoardKind::Work(*kind),
9488 }
9489 }
9490
9491 /// The status this write carries. A document carries none, so a write of one says
9492 /// nothing about the issue's open or closed state and selects no board `Status`
9493 /// option.
9494 const fn status(&self) -> Option<&Status> {
9495 match self {
9496 Self::Document => None,
9497 Self::Work(_, status) => Some(status),
9498 }
9499 }
9500
9501 /// The status this write carries with the kind whose half of `status_mapping` it is
9502 /// written through.
9503 const fn work_status(&self) -> Option<(ItemKind, &Status)> {
9504 match self {
9505 Self::Document => None,
9506 Self::Work(kind, status) => Some((*kind, status)),
9507 }
9508 }
9509}
9510
9511/// The item being written, in the one shape all three write methods reach.
9512struct Incoming<'a> {
9513 written: Written<'a>,
9514 /// The title a person wrote. A document's goes onto the issue with
9515 /// [`DESIGN_TITLE_PREFIX`] put back, so a round trip returns the title that went in.
9516 title: &'a str,
9517 content: Option<&'a str>,
9518 assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
9519 labels: &'a [Label],
9520 metadata: &'a BTreeMap<String, Value>,
9521 repositories: &'a [Repository],
9522 /// Recorded in the slot while private, and nowhere while public.
9523 classification: Classification,
9524 parent: Option<&'a NativeId>,
9525 /// [`Task::delivers`], already checked. Empty for a project or a document, which is
9526 /// what keeps either key out of their slot.
9527 delivers: &'a [TaskRef],
9528 /// [`Task::delivered_by`], already checked. Empty for a project or a document.
9529 delivered_by: &'a [TaskRef],
9530 /// [`Task::priority`], for a task written to an instance that holds one; `None` for a
9531 /// project, a document, and every write to an instance with no `priority_mapping` —
9532 /// which is what keeps such a write's requests exactly what they were before.
9533 priority: Option<Priority>,
9534}
9535
9536/// What one write does to one projected metadata text field, by the field's id.
9537#[derive(Debug, Clone)]
9538enum ProjectionWrite {
9539 /// Write this text.
9540 Set {
9541 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.
9542 text: String,
9543 },
9544 /// Clear the field's value.
9545 Clear { field: String }, // llmlint: ignore[invalid_states_unrepresentable] As above.
9546}
9547
9548impl ProjectionWrite {
9549 /// Add this write to a field write's list of values and list of clears.
9550 fn join<'a>(&'a self, values: &mut Vec<(String, Value)>, clears: &mut Vec<&'a str>) {
9551 match self {
9552 Self::Set { field, text } => values.push((field.clone(), json!({"text": text}))),
9553 Self::Clear { field } => clears.push(field),
9554 }
9555 }
9556}
9557
9558/// What one write does to an item's `Priority` field.
9559enum PriorityWrite {
9560 /// Select this option of this field.
9561 Select {
9562 /// The `Priority` field's id.
9563 field: String,
9564 /// The mapped option's id.
9565 option: String,
9566 },
9567 /// Clear the field's value, which is what `none` is.
9568 Clear {
9569 /// The `Priority` field's id.
9570 field: String,
9571 },
9572}
9573
9574impl Incoming<'_> {
9575 /// The title this write puts on the issue.
9576 fn written_title(&self) -> String {
9577 match self.written {
9578 Written::Document => format!("{DESIGN_TITLE_PREFIX}{}", self.title),
9579 Written::Work(..) => self.title.to_owned(),
9580 }
9581 }
9582}
9583
9584#[derive(Clone, Copy, PartialEq, Eq)]
9585enum ContentKind {
9586 DraftIssue,
9587 Issue,
9588}
9589
9590/// What one board issue is: a document, or the work an [`ItemKind`] names.
9591///
9592/// A type of this source's own rather than an `ItemKind` with a third variant, because
9593/// `ItemKind` names what a dependency endpoint points at and nothing may point at a
9594/// document — the contract keeps a document out of that enum deliberately. Holding the
9595/// board's three answers in one value is what makes every place that asks "which is this?"
9596/// answer all three, rather than a `document: bool` beside a `kind` that means nothing for
9597/// two thirds of the board.
9598#[derive(Clone, Copy, PartialEq, Eq)]
9599enum BoardKind {
9600 /// An issue whose title begins [`DESIGN_TITLE_PREFIX`].
9601 Document,
9602 /// Every other issue, and every draft.
9603 Work(ItemKind),
9604}
9605
9606impl BoardKind {
9607 /// Whose half of `status_mapping` an item of this kind reads its status through. A
9608 /// document has no status of its own, so the task half stands in for whatever the issue
9609 /// holds; nothing reports it.
9610 const fn status_kind(self) -> ItemKind {
9611 match self {
9612 Self::Document => ItemKind::Task,
9613 Self::Work(kind) => kind,
9614 }
9615 }
9616
9617 /// How a refusal names this kind to the person reading it.
9618 const fn describes(self) -> &'static str {
9619 match self {
9620 Self::Document => "document",
9621 Self::Work(kind) => kind.marker(),
9622 }
9623 }
9624}
9625
9626/// Whether `labels` satisfies `filter`, matching by name, case-insensitively.
9627///
9628/// This is the local Markdown source's `labels_match`, spelled the same way on purpose:
9629/// the shared cross-source journeys assert one answer to one question, so two sources
9630/// that disagree about what "carries the label bug" means fail them.
9631fn labels_match(labels: &[Label], filter: &LabelFilter) -> bool {
9632 let holds = |name: &String| {
9633 labels
9634 .iter()
9635 .any(|label| label.name.eq_ignore_ascii_case(name))
9636 };
9637 (filter.any_of.is_empty() || filter.any_of.iter().any(holds))
9638 && filter.all_of.iter().all(holds)
9639 && !filter.none_of.iter().any(holds)
9640}
9641
9642/// Whether `category` is one of `statuses`. An empty list is unfiltered rather than
9643/// "keeps nothing", which is what lets a `Vec<StatusCategory>` spell no filter at all.
9644fn status_matches(category: StatusCategory, statuses: &[StatusCategory]) -> bool {
9645 statuses.is_empty() || statuses.contains(&category)
9646}
9647
9648/// Whether `title`/`content` satisfies `query`, matching case-insensitively.
9649///
9650/// `content` is the item's own prose — the body with this source's trailing metadata
9651/// comment already taken off — so a search never matches an encoding the author of the
9652/// issue never wrote.
9653fn text_matches(title: &str, content: Option<&str>, query: &TextQuery) -> bool {
9654 let terms = query.terms.to_lowercase();
9655 let in_title = title.to_lowercase().contains(&terms);
9656 let in_content = content.is_some_and(|body| body.to_lowercase().contains(&terms));
9657 match query.fields {
9658 TextFields::Title => in_title,
9659 TextFields::Content => in_content,
9660 TextFields::TitleOrContent => in_title || in_content,
9661 }
9662}
9663
9664/// Whether `task` satisfies `query`, with `project` deciding the project predicate.
9665///
9666/// The project predicate is passed separately because a read narrowed to one project has
9667/// already answered it by asking *that project* for its own items — and re-applying it
9668/// there would compare the caller's selector, which may be a project's **name**, against
9669/// the id of the project that name resolved to, and keep nothing. Every other read passes
9670/// `query.project` and applies it here, which is what keeps `projects` a predicate this
9671/// source really does apply.
9672fn task_matches(task: &Task, query: &TaskQuery, project: &ProjectFilter) -> bool {
9673 labels_match(&task.labels, &query.labels)
9674 && status_matches(task.status.category, &query.statuses)
9675 && (query.priorities.is_empty() || query.priorities.contains(&task.priority))
9676 && match project {
9677 ProjectFilter::Any => true,
9678 ProjectFilter::Orphans => task.project.is_none(),
9679 ProjectFilter::Is(id) => task.project.as_ref() == Some(id),
9680 }
9681 && query
9682 .text
9683 .as_ref()
9684 .is_none_or(|text| text_matches(&task.title, task.content.as_deref(), text))
9685 // Against the parsed metadata slot, and against the origin field, which is where
9686 // `Resolved::metadata` reads each of them from.
9687 && query.metadata_matches(&task.metadata)
9688 && query.origin_matches(&task.metadata)
9689}
9690
9691fn project_matches(project: &Project, query: &ProjectQuery) -> bool {
9692 labels_match(&project.labels, &query.labels)
9693 && status_matches(project.status.category, &query.statuses)
9694 && query
9695 .text
9696 .as_ref()
9697 .is_none_or(|text| text_matches(&project.title, project.content.as_deref(), text))
9698}
9699
9700/// The same three predicates a task query carries, minus the status filter.
9701///
9702/// A document is not work, so it has no status for one to compare against and the query
9703/// type carries none. The project predicate is the same one — a design issue filed under a
9704/// project issue is in that project, and one filed under nothing is in none — so it is
9705/// spelled the same way here rather than answered differently.
9706fn document_matches(document: &Document, query: &DocumentQuery, project: &ProjectFilter) -> bool {
9707 labels_match(&document.labels, &query.labels)
9708 && match project {
9709 ProjectFilter::Any => true,
9710 ProjectFilter::Orphans => document.project.is_none(),
9711 ProjectFilter::Is(id) => document.project.as_ref() == Some(id),
9712 }
9713 && query
9714 .text
9715 .as_ref()
9716 .is_none_or(|text| text_matches(&document.title, document.content.as_deref(), text))
9717}
9718
9719#[async_trait::async_trait]
9720impl TaskSource for GitHubProjectsSource {
9721 fn kind(&self) -> &'static str {
9722 KIND
9723 }
9724 fn capabilities(&self) -> Capabilities {
9725 Capabilities {
9726 projects: Support::Native,
9727 documents: Support::Native,
9728 comments: Support::Native,
9729 assets: Support::Native,
9730 priority: if self.priorities.is_some() {
9731 Support::Native
9732 } else {
9733 Support::Unsupported
9734 },
9735 filter_by_priority: Support::Native,
9736 filter_by_comment_activity: Support::Native,
9737 filter_by_metadata: Support::Native,
9738 filter_by_origin: Support::Native,
9739 orphan_tasks: Support::Native,
9740 filter_by_label: Support::Native,
9741 filter_by_status: Support::Native,
9742 search_title: Support::Native,
9743 search_content: Support::Native,
9744 task_dependencies: DependencySupport::BothDirections,
9745 project_dependencies: DependencySupport::BothDirections,
9746 max_page_size: MAX_PAGE_SIZE,
9747 }
9748 }
9749 async fn visibility(
9750 &self,
9751 target: &onetaskgraph_plugin_api::WriteTarget<'_>,
9752 ) -> Result<onetaskgraph_plugin_api::Visibility, SourceError> {
9753 self.write_visibility(target).await
9754 }
9755 async fn health(&self) -> Result<Health, SourceError> {
9756 let board = self.board_page(None, 1).await?;
9757 Ok(Health {
9758 reachable: true,
9759 detail: Some(format!(
9760 "reading GitHub project {}/{} ({})",
9761 self.owner,
9762 self.project_number,
9763 required_str(&board, "title")?
9764 )),
9765 })
9766 }
9767 async fn get_task(&self, id: &NativeId) -> Result<Option<Task>, SourceError> {
9768 self.item_by_id(id)
9769 .await?
9770 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
9771 .map(|item| item.task())
9772 .transpose()
9773 }
9774 async fn task_assets(
9775 &self,
9776 id: &NativeId,
9777 ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
9778 self.held_assets(id, BoardKind::Work(ItemKind::Task)).await
9779 }
9780 async fn task_asset(
9781 &self,
9782 id: &NativeId,
9783 name: &onetaskgraph_plugin_api::AssetName,
9784 ) -> Result<Option<Vec<u8>>, SourceError> {
9785 self.held_asset(id, BoardKind::Work(ItemKind::Task), name)
9786 .await
9787 }
9788 async fn set_task_rendering_with_assets(
9789 &self,
9790 id: &NativeId,
9791 content: &str,
9792 provenance: &Value,
9793 _answers: &BTreeMap<String, Value>,
9794 assets: &onetaskgraph_plugin_api::AssetWrite,
9795 ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
9796 self.replace_rendering(
9797 id,
9798 BoardKind::Work(ItemKind::Task),
9799 content,
9800 provenance,
9801 Some(assets),
9802 )
9803 .await
9804 }
9805 async fn document_assets(
9806 &self,
9807 id: &NativeId,
9808 ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
9809 self.held_assets(id, BoardKind::Document).await
9810 }
9811 async fn document_asset(
9812 &self,
9813 id: &NativeId,
9814 name: &onetaskgraph_plugin_api::AssetName,
9815 ) -> Result<Option<Vec<u8>>, SourceError> {
9816 self.held_asset(id, BoardKind::Document, name).await
9817 }
9818 async fn set_document_rendering_with_assets(
9819 &self,
9820 id: &NativeId,
9821 content: &str,
9822 provenance: &Value,
9823 _answers: &BTreeMap<String, Value>,
9824 assets: &onetaskgraph_plugin_api::AssetWrite,
9825 ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
9826 self.replace_rendering(id, BoardKind::Document, content, provenance, Some(assets))
9827 .await
9828 }
9829 async fn get_project(&self, id: &NativeId) -> Result<Option<Project>, SourceError> {
9830 Ok(self
9831 .item_by_id(id)
9832 .await?
9833 .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
9834 .map(|item| item.project()))
9835 }
9836 async fn query_tasks(
9837 &self,
9838 query: &TaskQuery,
9839 page: &PageRequest,
9840 ) -> Result<Page<Task>, SourceError> {
9841 validate_page(page)?;
9842 refuse_unsearchable(query)?;
9843 if query.origin.is_none() && !matches!(query.project, ProjectFilter::Is(_)) {
9844 let qualifiers = match (narrowing_qualifiers(query), query.commented_since) {
9845 (Some(also), Some(since)) => Some(format!("{} {also}", updated_qualifier(since))),
9846 (Some(also), None) => Some(also),
9847 (None, Some(since)) => Some(updated_qualifier(since)),
9848 (None, None) => None,
9849 };
9850 if let Some(also) = qualifiers {
9851 return self.search_tasks(query, page, &also).await;
9852 }
9853 }
9854
9855 // A read narrowed to one project asks that project for its own tasks, so nothing
9856 // about it costs what the rest of the board holds. A read carrying a text, metadata
9857 // or origin predicate asks GitHub the narrower question those predicates are, and a
9858 // read narrowed to comment activity alone asks the board's own issue search for the
9859 // issues updated since, which is every issue a comment could have been written or
9860 // edited on since. Every other task read is a question about the whole board and is
9861 // answered by reading it.
9862 let (held, membership) = match (&query.project, query.commented_since) {
9863 (ProjectFilter::Is(project), _) => (
9864 self.project_children(project).await?,
9865 // Answered by where these items came from; see `task_matches`.
9866 &ProjectFilter::Any,
9867 ),
9868 (ProjectFilter::Any | ProjectFilter::Orphans, since) => {
9869 match (self.narrowed(query).await?, since) {
9870 (Some(narrowed), _) => (narrowed, &query.project),
9871 (None, Some(since)) => (self.updated_since(since).await?, &query.project),
9872 (None, None) => (self.board().await?.items, &query.project),
9873 }
9874 }
9875 };
9876 // Filtered before paged: a page of a filtered result is a page of the survivors,
9877 // never the survivors of a page.
9878 let mut tasks = Vec::new();
9879 for item in held
9880 .iter()
9881 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
9882 {
9883 let task = item.task()?;
9884 if task_matches(&task, query, membership)
9885 && self.commented_since(item, query.commented_since).await?
9886 {
9887 tasks.push(task);
9888 }
9889 }
9890 Ok(offset_page(
9891 tasks,
9892 numeric_cursor(page.cursor.as_ref())?,
9893 page.limit.min(MAX_PAGE_SIZE) as usize,
9894 ))
9895 }
9896 async fn query_projects(
9897 &self,
9898 query: &ProjectQuery,
9899 page: &PageRequest,
9900 ) -> Result<Page<Project>, SourceError> {
9901 validate_page(page)?;
9902 refuse_unsearchable_text(query.text.as_ref())?;
9903 // The projects a board holds are found by an issue search scoped to that board,
9904 // never by walking the board's own item connection: what tells a project from a
9905 // task is the `parent` each issue carries, which costs nothing to read. A query
9906 // carrying a text asks that search for the text too, so it reads the issues that
9907 // hold it rather than every issue of the board.
9908 let held = match self.text_searched(query.text.as_ref()).await? {
9909 Some(searched) => searched,
9910 None => self.board_issues().await?,
9911 };
9912 let projects = held
9913 .iter()
9914 .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
9915 .map(Resolved::project)
9916 .filter(|project| project_matches(project, query))
9917 .collect();
9918 Ok(offset_page(
9919 projects,
9920 numeric_cursor(page.cursor.as_ref())?,
9921 page.limit.min(MAX_PAGE_SIZE) as usize,
9922 ))
9923 }
9924 async fn get_document(&self, id: &NativeId) -> Result<Option<Document>, SourceError> {
9925 Ok(self
9926 .item_by_id(id)
9927 .await?
9928 .filter(|item| item.kind == BoardKind::Document)
9929 .map(|item| item.document()))
9930 }
9931 async fn query_documents(
9932 &self,
9933 query: &DocumentQuery,
9934 page: &PageRequest,
9935 ) -> Result<Page<Document>, SourceError> {
9936 validate_page(page)?;
9937 // Narrowed to one project, this is the same sub-issue read a task list scoped to
9938 // that project makes — a document filed under a project is a sub-issue of it too,
9939 // and which of them come back is the kind this caller asked for. Unscoped, a query
9940 // carrying a text asks the board-scoped issue search for it, as a task query does,
9941 // and only one carrying none reads the board.
9942 let (held, membership) = match &query.project {
9943 ProjectFilter::Is(project) => (
9944 self.project_children(project).await?,
9945 // Answered by where these items came from; see `task_matches`.
9946 &ProjectFilter::Any,
9947 ),
9948 ProjectFilter::Any | ProjectFilter::Orphans => {
9949 refuse_unsearchable_text(query.text.as_ref())?;
9950 match self.text_searched(query.text.as_ref()).await? {
9951 Some(searched) => (searched, &query.project),
9952 None => (self.board().await?.items, &query.project),
9953 }
9954 }
9955 };
9956 // Filtered before paged, exactly as a task read is: a page of a filtered result is
9957 // a page of the survivors, never the survivors of a page.
9958 let documents = held
9959 .iter()
9960 .filter(|item| item.kind == BoardKind::Document)
9961 .map(Resolved::document)
9962 .filter(|document| document_matches(document, query, membership))
9963 .collect();
9964 Ok(offset_page(
9965 documents,
9966 numeric_cursor(page.cursor.as_ref())?,
9967 page.limit.min(MAX_PAGE_SIZE) as usize,
9968 ))
9969 }
9970 async fn labels(&self, page: &PageRequest) -> Result<Page<Label>, SourceError> {
9971 validate_page(page)?;
9972 let offset = numeric_cursor(page.cursor.as_ref())?;
9973 let mut labels = self
9974 .board()
9975 .await?
9976 .items
9977 .into_iter()
9978 .flat_map(|item| item.labels)
9979 .fold(Vec::new(), |mut all, label| {
9980 if !all.iter().any(|x: &Label| x.id == label.id) {
9981 all.push(label);
9982 }
9983 all
9984 });
9985 labels.sort_by(|a, b| a.name.cmp(&b.name).then(a.id.0.cmp(&b.id.0)));
9986 Ok(offset_page(
9987 labels,
9988 offset,
9989 page.limit.min(MAX_PAGE_SIZE) as usize,
9990 ))
9991 }
9992 async fn task_dependencies(
9993 &self,
9994 id: &NativeId,
9995 direction: Direction,
9996 page: &PageRequest,
9997 ) -> Result<Page<DependencyEdge>, SourceError> {
9998 self.dependencies(id, ItemKind::Task, direction, page).await
9999 }
10000 async fn project_dependencies(
10001 &self,
10002 id: &NativeId,
10003 direction: Direction,
10004 page: &PageRequest,
10005 ) -> Result<Page<DependencyEdge>, SourceError> {
10006 self.dependencies(id, ItemKind::Project, direction, page)
10007 .await
10008 }
10009
10010 fn writes(&self) -> WriteSupport {
10011 WriteSupport::Supported
10012 }
10013
10014 /// Create or update one task.
10015 ///
10016 /// Its `delivers` and `delivered_by` are checked before anything is read or written —
10017 /// neither may name the task itself or name one task twice — and land in the body's
10018 /// metadata slot under their reserved keys, in place of any caller metadata of those
10019 /// names.
10020 async fn write_task(&self, write: &ItemWrite<Task>) -> Result<NativeId, SourceError> {
10021 self.write_task_assets(write, None)
10022 .await
10023 .map(|written| written.id)
10024 }
10025
10026 async fn write_task_with_assets(
10027 &self,
10028 write: &ItemWrite<Task>,
10029 _answers: Option<&BTreeMap<String, Value>>,
10030 assets: &onetaskgraph_plugin_api::AssetWrite,
10031 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
10032 self.write_task_assets(write, Some(assets)).await
10033 }
10034
10035 async fn write_project(&self, write: &ItemWrite<Project>) -> Result<NativeId, SourceError> {
10036 self.write_item(
10037 &Incoming {
10038 written: Written::Work(ItemKind::Project, &write.item.status),
10039 title: &write.item.title,
10040 content: write.item.content.as_deref(),
10041 assets: None,
10042 labels: &write.item.labels,
10043 metadata: &write.item.metadata,
10044 repositories: &write.item.repositories,
10045 classification: write.item.classification,
10046 parent: None,
10047 delivers: &[],
10048 delivered_by: &[],
10049 priority: None,
10050 },
10051 write.target.as_ref(),
10052 &write.depends_on,
10053 )
10054 .await
10055 .map(|written| written.id)
10056 }
10057
10058 /// Create or update one document, which is one issue titled the way this board spells
10059 /// a document.
10060 ///
10061 /// Everything else is exactly a task write: caller metadata goes to the same canonical
10062 /// JSON slot at the end of the body and comes back with its JSON types intact, a key
10063 /// or a field this board cannot carry is refused by name rather than dropped, a target
10064 /// naming an issue this board does not hold is refused rather than created, and an
10065 /// issue this call created is taken back when the rest of the write fails.
10066 async fn write_document(&self, write: &ItemWrite<Document>) -> Result<NativeId, SourceError> {
10067 self.write_document_assets(write, None)
10068 .await
10069 .map(|written| written.id)
10070 }
10071
10072 async fn write_document_with_assets(
10073 &self,
10074 write: &ItemWrite<Document>,
10075 _answers: Option<&BTreeMap<String, Value>>,
10076 assets: &onetaskgraph_plugin_api::AssetWrite,
10077 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
10078 self.write_document_assets(write, Some(assets)).await
10079 }
10080
10081 /// Refused exactly as the write refuses it, from what the write reads: the mapping first,
10082 /// which reads nothing; then the board's `Status` option. Over an existing item that is
10083 /// read off the item, as the write reads it, and the item is held among this command's
10084 /// resolved records so the write that follows reuses that read rather than repeating it;
10085 /// an item that does not carry the field takes the board's fields, which are held once
10086 /// read. A create is checked against the board's fields only when this command already
10087 /// holds them, because a create reads them together with its repository, in one request,
10088 /// and refuses a missing option before it writes anything.
10089 async fn check_status_write(
10090 &self,
10091 kind: ItemKind,
10092 category: StatusCategory,
10093 target: Option<&NativeId>,
10094 ) -> Result<(), SourceError> {
10095 let status = self.resolved_target(kind, category)?;
10096 if status.option().is_none() {
10097 return Ok(());
10098 }
10099 let fields = match target {
10100 Some(target) => {
10101 // A target this board does not hold is the write's own refusal to make.
10102 let Some(item) = self.bound_item(target).await? else {
10103 return Ok(());
10104 };
10105 self.resolved_cache()?.insert(target.clone(), item.clone());
10106 self.fields_for(Some(&item), true, false).await?.fields
10107 }
10108 None => {
10109 let held = self
10110 .board_cache()?
10111 .as_ref()
10112 .map(|board| board.fields.clone());
10113 match held.or_else(|| {
10114 self.fields_cache()
10115 .ok()
10116 .and_then(|cache| cache.as_ref().map(|board| board.fields.clone()))
10117 }) {
10118 Some(fields) => fields,
10119 None => return Ok(()),
10120 }
10121 }
10122 };
10123 self.column_for(&fields, kind, category, &status)
10124 .map(|_| ())
10125 }
10126
10127 /// Set one task's status alone.
10128 ///
10129 /// An open target reopens a closed issue with an `updateIssue` carrying only its
10130 /// `stateInput`, then selects the board option with `updateProjectV2ItemFieldValue`; a
10131 /// terminal target selects its mapped option, then closes with its fixed reason. No
10132 /// request carries a title, a body or a label. The status
10133 /// answered is what [`BoardStatuses::status`] reads off the state just written, which is
10134 /// what a re-read reports.
10135 async fn set_task_status(
10136 &self,
10137 id: &NativeId,
10138 category: StatusCategory,
10139 ) -> Result<Option<Status>, SourceError> {
10140 self.set_status(id, category).await
10141 }
10142
10143 /// Set one task's priority alone: one `updateProjectV2ItemFieldValue` selecting the
10144 /// mapped option of the board's `Priority` field, or one `clearProjectV2ItemFieldValue`
10145 /// for `none`. Refused by an instance with no `priority_mapping`.
10146 async fn set_task_priority(
10147 &self,
10148 id: &NativeId,
10149 priority: Priority,
10150 ) -> Result<Option<Priority>, SourceError> {
10151 self.set_priority(id, priority).await
10152 }
10153
10154 /// Replace one task's content with a single body update that keeps the metadata slot
10155 /// byte for byte.
10156 async fn set_task_content(
10157 &self,
10158 id: &NativeId,
10159 content: &str,
10160 ) -> Result<Option<()>, SourceError> {
10161 self.replace_content(id, content).await
10162 }
10163
10164 /// Replace one task issue's content and its provenance slot entry with a single body
10165 /// update. The answers are not kept: see `replace_rendering`.
10166 async fn set_task_rendering(
10167 &self,
10168 id: &NativeId,
10169 content: &str,
10170 provenance: &Value,
10171 _answers: &BTreeMap<String, Value>,
10172 ) -> Result<Option<()>, SourceError> {
10173 self.replace_rendering(
10174 id,
10175 BoardKind::Work(ItemKind::Task),
10176 content,
10177 provenance,
10178 None,
10179 )
10180 .await
10181 .map(|written| written.map(|_| ()))
10182 }
10183
10184 /// Replace one design-document issue's content and its provenance slot entry, on exactly
10185 /// the terms of [`set_task_rendering`](TaskSource::set_task_rendering).
10186 async fn set_document_rendering(
10187 &self,
10188 id: &NativeId,
10189 content: &str,
10190 provenance: &Value,
10191 _answers: &BTreeMap<String, Value>,
10192 ) -> Result<Option<()>, SourceError> {
10193 self.replace_rendering(id, BoardKind::Document, content, provenance, None)
10194 .await
10195 .map(|written| written.map(|_| ()))
10196 }
10197
10198 /// Replace one project issue's content and its provenance slot entry, on exactly the
10199 /// terms of [`set_task_rendering`](TaskSource::set_task_rendering).
10200 async fn set_project_rendering(
10201 &self,
10202 id: &NativeId,
10203 content: &str,
10204 provenance: &Value,
10205 _answers: &BTreeMap<String, Value>,
10206 ) -> Result<Option<()>, SourceError> {
10207 self.replace_rendering(
10208 id,
10209 BoardKind::Work(ItemKind::Project),
10210 content,
10211 provenance,
10212 None,
10213 )
10214 .await
10215 .map(|written| written.map(|_| ()))
10216 }
10217
10218 /// Apply a targeted update with one read of the item and a write only for what differs:
10219 /// the `Status` and `Priority` field writes in one request, the `blockedBy` difference,
10220 /// and last one `updateIssue` for title, body and state. See `targeted_update`.
10221 async fn update_task(
10222 &self,
10223 id: &NativeId,
10224 update: &TaskUpdate,
10225 ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
10226 self.targeted_update(id, update).await
10227 }
10228
10229 /// Replace one task's `delivered_by` with a single body update that changes the
10230 /// metadata slot and nothing outside it.
10231 async fn set_delivered_by(
10232 &self,
10233 id: &NativeId,
10234 delivered_by: &[TaskRef],
10235 ) -> Result<Option<()>, SourceError> {
10236 self.replace_delivered_by(id, delivered_by).await
10237 }
10238
10239 /// Set one key of one task issue's metadata with a single body update that changes the
10240 /// metadata slot and nothing outside it — no title, label, state or board field request —
10241 /// and sends nothing when the task already holds that value under the key.
10242 async fn set_task_metadata(
10243 &self,
10244 id: &NativeId,
10245 key: &MetadataKey,
10246 value: &Value,
10247 ) -> Result<Option<Task>, SourceError> {
10248 Ok(self
10249 .set_slot_key(id, BoardKind::Work(ItemKind::Task), key, value)
10250 .await?
10251 .map(|item| item.task())
10252 .transpose()?)
10253 }
10254
10255 /// Set one key of one project issue's metadata, on exactly the terms of
10256 /// [`set_task_metadata`](TaskSource::set_task_metadata).
10257 async fn set_project_metadata(
10258 &self,
10259 id: &NativeId,
10260 key: &MetadataKey,
10261 value: &Value,
10262 ) -> Result<Option<Project>, SourceError> {
10263 Ok(self
10264 .set_slot_key(id, BoardKind::Work(ItemKind::Project), key, value)
10265 .await?
10266 .map(|item| item.project()))
10267 }
10268
10269 /// Set one key of one design-document issue's metadata, on exactly the terms of
10270 /// [`set_task_metadata`](TaskSource::set_task_metadata).
10271 async fn set_document_metadata(
10272 &self,
10273 id: &NativeId,
10274 key: &MetadataKey,
10275 value: &Value,
10276 ) -> Result<Option<Document>, SourceError> {
10277 Ok(self
10278 .set_slot_key(id, BoardKind::Document, key, value)
10279 .await?
10280 .map(|item| item.document()))
10281 }
10282
10283 async fn delete_task(&self, id: &NativeId) -> Result<(), SourceError> {
10284 self.delete_item(id).await
10285 }
10286
10287 async fn delete_project(&self, id: &NativeId) -> Result<(), SourceError> {
10288 self.delete_item(id).await
10289 }
10290
10291 async fn delete_document(&self, id: &NativeId) -> Result<(), SourceError> {
10292 self.delete_item(id).await
10293 }
10294
10295 /// One page of the task issue's own comments, walked by GitHub's own cursor.
10296 ///
10297 /// Nothing here filters, so nothing has to be read ahead of the page: the caller's limit is
10298 /// the page GitHub is asked for and GitHub's `endCursor` is the cursor handed back.
10299 ///
10300 /// One request, [`graphql::ISSUE_DETAIL`]: the read that says the id names a task of this
10301 /// board is the read of its comments. A draft this process already resolved is refused
10302 /// without one.
10303 async fn task_comments(
10304 &self,
10305 task: &NativeId,
10306 page: &PageRequest,
10307 ) -> Result<Option<Page<Comment>>, SourceError> {
10308 validate_page(page)?;
10309 let cached = self.resolved_cache()?.get(task).cloned();
10310 if let Some(item) = cached {
10311 if item.kind != BoardKind::Work(ItemKind::Task) {
10312 return Ok(None);
10313 }
10314 if item.content_kind == ContentKind::DraftIssue {
10315 return Err(self.draft_has_no_comments(task));
10316 }
10317 }
10318 match self.issue_detail(task, page).await? {
10319 Some(TaskDetailRead {
10320 comments: Some(comments),
10321 ..
10322 }) => comments,
10323 _ => Ok(None),
10324 }
10325 }
10326
10327 /// Every id's task, with the first page of its comments when `comments` names it:
10328 /// [`DETAIL_BATCH`] items per [`graphql::ISSUE_DETAILS`] request, and one item with its
10329 /// comments in one [`graphql::ISSUE_DETAIL`] request.
10330 async fn get_task_details(
10331 &self,
10332 ids: &[NativeId],
10333 comments: Option<&PageRequest>,
10334 ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
10335 if let Some(page) = comments
10336 && let Err(error) = validate_page(page)
10337 {
10338 return ids.iter().map(|_| Err(error.clone())).collect();
10339 }
10340 match (ids, comments) {
10341 ([id], Some(page)) => vec![self.issue_detail(id, page).await],
10342 ([id], None) => vec![self.task_read(id).await],
10343 _ => self.issue_details(ids, comments).await,
10344 }
10345 }
10346
10347 /// Add one comment to the task's issue, as the account the token belongs to.
10348 ///
10349 /// The author is refused before anything is sent — not even the task is read — because
10350 /// no answer GitHub could give would make posting under another name than the one asked
10351 /// for the right outcome.
10352 async fn add_comment(
10353 &self,
10354 task: &NativeId,
10355 comment: &NewComment,
10356 ) -> Result<Option<Comment>, SourceError> {
10357 if let Some(author) = &comment.author {
10358 return Err(SourceError::Refused {
10359 message: format!(
10360 "source {} cannot post a comment as {author:?}: GitHub records the account \
10361 the token signs in as the author of every comment; next: leave --author \
10362 out, and the comment is posted as that account",
10363 self.name
10364 ),
10365 });
10366 }
10367 let Some(issue) = self.commented_issue(task).await? else {
10368 return Ok(None);
10369 };
10370 let data = self
10371 .graphql(
10372 graphql::ADD_COMMENT,
10373 json!({"input":{"subjectId":issue.0,"body":comment.body.as_str()}}),
10374 )
10375 .await?;
10376 let subject = data
10377 .pointer("/addComment/subject")
10378 .filter(|value| !value.is_null())
10379 .ok_or_else(|| SourceError::Malformed {
10380 message: "GitHub comment addition returned no subject".into(),
10381 })?;
10382 if required_str(subject, "id")? != issue.0 {
10383 return Err(SourceError::Malformed {
10384 message: "GitHub comment addition answered about another issue".into(),
10385 });
10386 }
10387 let added = data
10388 .pointer("/addComment/commentEdge/node")
10389 .filter(|value| !value.is_null())
10390 .ok_or_else(|| SourceError::Malformed {
10391 message: "GitHub comment addition returned no comment".into(),
10392 })?;
10393 let added = comment_from(added)?;
10394 self.remember_commented(&issue)?;
10395 Ok(Some(added))
10396 }
10397
10398 async fn edit_comment(
10399 &self,
10400 task: &NativeId,
10401 comment: &NativeId,
10402 body: &CommentBody,
10403 ) -> Result<Option<Comment>, SourceError> {
10404 let Some(issue) = self.commented_issue(task).await? else {
10405 return Ok(None);
10406 };
10407 if !self.comment_is_on(&issue, comment).await? {
10408 return Ok(None);
10409 }
10410 let data = self
10411 .graphql(
10412 graphql::UPDATE_COMMENT,
10413 json!({"input":{"id":comment.0,"body":body.as_str()}}),
10414 )
10415 .await?;
10416 let edited = data
10417 .pointer("/updateIssueComment/issueComment")
10418 .filter(|value| !value.is_null())
10419 .ok_or_else(|| SourceError::Malformed {
10420 message: "GitHub comment update returned no comment".into(),
10421 })?;
10422 let edited = comment_from(edited)?;
10423 if edited.id != *comment {
10424 return Err(SourceError::Malformed {
10425 message: "GitHub comment update returned the wrong comment".into(),
10426 });
10427 }
10428 self.remember_commented(&issue)?;
10429 Ok(Some(edited))
10430 }
10431
10432 async fn delete_comment(
10433 &self,
10434 task: &NativeId,
10435 comment: &NativeId,
10436 ) -> Result<Option<NativeId>, SourceError> {
10437 let Some(issue) = self.commented_issue(task).await? else {
10438 return Ok(None);
10439 };
10440 if !self.comment_is_on(&issue, comment).await? {
10441 return Ok(None);
10442 }
10443 let data = self
10444 .graphql(graphql::DELETE_COMMENT, json!({"input":{"id":comment.0}}))
10445 .await?;
10446 // The payload says nothing about the comment it removed, so what is checked is that
10447 // GitHub answered the mutation at all rather than leaving it unanswered.
10448 data.get("deleteIssueComment")
10449 .filter(|value| !value.is_null())
10450 .ok_or_else(|| SourceError::Malformed {
10451 message: "GitHub comment deletion returned no payload".into(),
10452 })?;
10453 Ok(Some(comment.clone()))
10454 }
10455
10456 /// Every request this source has recorded, and what each of GitHub's two budgets was
10457 /// attributed — read off the same accounting the session report is rendered from, so
10458 /// the two cannot count one request two ways.
10459 async fn metering(&self) -> Result<Option<Metering>, SourceError> {
10460 Ok(Some(self.ledger.snapshot().metering()))
10461 }
10462
10463 /// Drop every item, search answer and board read this source holds, so the next command
10464 /// reads the board as a person has since left it.
10465 ///
10466 /// Every one of those is held on the assumption that nothing but this source writes the
10467 /// board while a command runs, which stops being true the moment the command is over: a
10468 /// body a person edited would be overwritten from the record held here, and a card they
10469 /// moved would be read as still where this source left it. The board's own field
10470 /// definitions go too, because a person can add or delete a `Status` option and a write
10471 /// resolved against the held list would not re-read on a miss. What stays is what stays
10472 /// valid in normal use: each repository's node id, which a miss re-reads, the pacing of
10473 /// mutations, which is about GitHub's limiter rather than anybody's work, and the running
10474 /// accounting [`metering`](TaskSource::metering) answers from.
10475 ///
10476 /// Infallible in practice: a lock an earlier failure poisoned is cleared rather than
10477 /// refused, because clearing it is what puts it right.
10478 async fn end_command(&self) -> Result<(), SourceError> {
10479 fn clear<T: Default>(held: &Mutex<T>) {
10480 *held
10481 .lock()
10482 .unwrap_or_else(std::sync::PoisonError::into_inner) = T::default();
10483 held.clear_poison();
10484 }
10485 clear(&self.created);
10486 clear(&self.updated);
10487 clear(&self.commented);
10488 clear(&self.board_cache);
10489 clear(&self.search_cache);
10490 clear(&self.narrowed_cache);
10491 clear(&self.search_next);
10492 clear(&self.resolved_cache);
10493 clear(&self.children_cache);
10494 clear(&self.fields_cache);
10495 Ok(())
10496 }
10497}
10498
10499/// Each project's sub-issues as one command read them, keyed by the selector they were asked
10500/// for under, beside the project that selector named; see `children_cache`.
10501type ProjectChildren = BTreeMap<NativeId, (NativeId, Vec<Resolved>)>;
10502
10503/// One issue comment as the contract carries it.
10504///
10505/// `author` is absent both when GitHub answers `null` for an account that no longer exists
10506/// and when it answers an actor with no login, because either way the source did not say who
10507/// wrote it — which is what an absent author means, rather than an author called nothing.
10508fn comment_from(value: &Value) -> Result<Comment, SourceError> {
10509 Ok(Comment {
10510 id: NativeId(required_str(value, "id")?.to_owned()),
10511 author: optional_str(value.get("author").unwrap_or(&Value::Null), "login")?
10512 .map(str::to_owned),
10513 created_at: optional_time(value, "createdAt")?,
10514 updated_at: optional_time(value, "updatedAt")?,
10515 body: required_str(value, "body")?.to_owned(),
10516 url: optional_str(value, "url")?.map(str::to_owned),
10517 })
10518}
10519
10520/// The page of comments one issue node carries, resumed from `after`.
10521fn comment_page(
10522 node: &Value,
10523 issue: &str,
10524 after: Option<&str>,
10525) -> Result<Page<Comment>, SourceError> {
10526 let connection = node
10527 .get("comments")
10528 .filter(|value| !value.is_null())
10529 .ok_or_else(|| SourceError::Malformed {
10530 message: format!("GitHub issue {issue} answered with no comments connection"),
10531 })?;
10532 let items = optional_nodes(Some(connection), "issue comments")?
10533 .into_iter()
10534 .flatten()
10535 .map(comment_from)
10536 .collect::<Result<Vec<_>, _>>()?;
10537 let next = next_cursor(connection)?;
10538 if let Some(next) = &next {
10539 validate_cursor_progress(after, &next.0)?;
10540 }
10541 Ok(Page { items, next })
10542}
10543
10544/// The far ends of an issue's whole `blockedBy` connection, when the read carried it to its
10545/// end — `None` when it carried none, or a page with more past it.
10546fn carried_blocked_by(content: &Value) -> Result<Option<Vec<Value>>, SourceError> {
10547 let Some(connection) = content.get("blockedBy").filter(|value| !value.is_null()) else {
10548 return Ok(None);
10549 };
10550 if next_cursor(connection)?.is_some() {
10551 return Ok(None);
10552 }
10553 Ok(Some(
10554 optional_nodes(Some(connection), "blocked-by issues")?
10555 .into_iter()
10556 .flatten()
10557 .cloned()
10558 .collect(),
10559 ))
10560}
10561
10562/// Where the recorded tail of a dependency walk resumes; see
10563/// [`GitHubProjectsSource::recorded_edges`].
10564const RECORDED_CURSOR: &str = "onetaskgraph.depends_on:";
10565
10566/// The board text field this source keeps a copy's origin in.
10567///
10568/// Named after the key it holds, and held to that name by the guard below rather than by
10569/// a reader noticing.
10570const ORIGIN_FIELD: &str = "onetaskgraph.origin";
10571
10572/// The metadata key that field holds.
10573///
10574/// The engine owns this key and spells it once as `GlobalId::ORIGIN_KEY`; a plugin never
10575/// constructs or interprets the qualified id it carries. This source names it only to
10576/// route it — a short, typed value belongs in a typed field rather than in the body slot
10577/// a caller's own prose shares.
10578///
10579/// Restated rather than imported, because no plugin crate may depend on the engine. What
10580/// keeps the two spellings one contract is `scripts/check-origin-key-spelling.sh`, a
10581/// target in `check`: it reads the engine's own literal and fails naming the file and the
10582/// line when a plugin's parts from it either way. Drift here has one symptom — a copy
10583/// that creates a second item every run instead of finding the one it wrote — and that is
10584/// too late to learn it.
10585const ORIGIN_KEY: &str = "onetaskgraph.origin";
10586
10587/// Where a recorded tail resumes, refusing a cursor no walk in `direction` reported.
10588///
10589/// The reserved key holds forward edges and nothing else — the reverse of a recorded edge
10590/// is derived from the far end, never written down on the near item — so only a forward
10591/// walk ever reports one of these cursors. A reverse read carrying one is resuming a walk
10592/// it did not come from, and it is told so rather than answered with an empty page that
10593/// reads as a walk which ended.
10594fn recorded_offset(
10595 cursor: Option<&str>,
10596 direction: Direction,
10597) -> Result<Option<usize>, SourceError> {
10598 cursor
10599 .and_then(|cursor| cursor.strip_prefix(RECORDED_CURSOR))
10600 .map(|offset| {
10601 if direction != Direction::DependsOn {
10602 return Err(SourceError::Config {
10603 message: format!(
10604 "{RECORDED_CURSOR}{offset} resumes recorded forward edges, which a \
10605 reverse dependency read never issues; resume it in the direction \
10606 that reported it"
10607 ),
10608 });
10609 }
10610 offset.parse().map_err(|_| SourceError::Config {
10611 message: format!("{RECORDED_CURSOR}{offset} is not a recorded-edge cursor"),
10612 })
10613 })
10614 .transpose()
10615}
10616
10617fn recorded_page(edges: Vec<DependencyEdge>, offset: usize, limit: usize) -> Page<DependencyEdge> {
10618 let mut page = offset_page(edges, offset, limit.max(1));
10619 page.next = page
10620 .next
10621 .map(|cursor| Cursor(format!("{RECORDED_CURSOR}{}", cursor.0)));
10622 page
10623}
10624
10625/// The kind of one issue reached through a dependency connection.
10626///
10627/// The same questions the board scan asks, over the fields the dependency document
10628/// selects, and in the same order: the design prefix first, then a sub-issue is a task,
10629/// then anything with sub-issues or the marker is a project.
10630///
10631/// # Errors
10632///
10633/// A far end this board holds as a document is refused rather than reported. The two
10634/// answers that are not refusals would both be wrong: reporting it as a task names an id
10635/// no task read of this source can find, and reporting it as a project names one no
10636/// project read can. There is no third value to return — `ItemKind` has no document
10637/// variant, because nothing may point at a document — so the relationship itself is what
10638/// the person is told about.
10639fn related_kind(value: &Value) -> Result<ItemKind, SourceError> {
10640 let id = required_str(value, "id")?;
10641 if required_str(value, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
10642 return Err(SourceError::Refused {
10643 message: format!(
10644 "GitHub issue {id} is a document of this board — its title begins \
10645 {DESIGN_TITLE_PREFIX:?} — and nothing may depend on a document or be depended \
10646 on by one; next: remove that issue's blocking relationship on this board"
10647 ),
10648 });
10649 }
10650 let parent = optional_str(value.get("parent").unwrap_or(&Value::Null), "id")?;
10651 if parent.is_some() {
10652 return Ok(ItemKind::Task);
10653 }
10654 let (_, slot) = metadata_body(optional_str(value, "body")?.map(str::to_owned))?;
10655 let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
10656 message: format!("GitHub issue {id}: {message}"),
10657 })?;
10658 let sub_issues = sub_issue_total(value)?;
10659 Ok(if sub_issues > 0 || marked == Some(ItemKind::Project) {
10660 ItemKind::Project
10661 } else {
10662 ItemKind::Task
10663 })
10664}
10665
10666/// The `IssueStateUpdateInput` one status target asks for.
10667///
10668/// `stateInput` and `state` are mutually exclusive on `UpdateIssueInput`, and only this
10669/// one is ever sent. A non-terminal status always asks for `OPEN`, which is what reopens
10670/// a currently-closed issue: without that the item would read back `Unknown` and a copy
10671/// would report a change forever. A document has no status at all, and asks for neither.
10672fn state_input(target: Option<&StatusTarget>) -> Value {
10673 match target {
10674 Some(StatusTarget::Terminal(_, reason)) => {
10675 json!({"value":"CLOSED","stateReason":reason.reason()})
10676 }
10677 Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => json!({"value":"OPEN"}),
10678 // A document has no status, so a write of one says nothing about the issue's open
10679 // or closed state rather than forcing it open: `stateInput` is what carries that
10680 // instruction, and an explicit null asks for no change to it.
10681 None => Value::Null,
10682 }
10683}
10684
10685/// The metadata one write stores in the item's body slot.
10686///
10687/// The typed fields travel as themselves, so the three reserved keys are rebuilt here
10688/// rather than carried: the kind marker so an empty project stays readable, the
10689/// repository list only when it is not exactly the issue's own repository, and the far
10690/// ends no relationship here can name.
10691///
10692/// The copy origin is the one typed field that is also mirrored here, and only as a
10693/// mirror: it lands in the board's origin field as well, which stays the one every reader
10694/// takes it from, and it is here so that GitHub's issue search — which indexes this comment
10695/// and catches up with a write in seconds rather than minutes — can find the item by it.
10696/// A reader of the release before this one drops the slot's copy and reads the field, so an
10697/// item written here still reads with exactly one origin there.
10698fn slot_metadata(
10699 incoming: &Incoming<'_>,
10700 own_repository: Option<&Repository>,
10701 fallback: &[DependencyEdge],
10702) -> BTreeMap<String, Value> {
10703 let mut metadata = incoming.metadata.clone();
10704 match metadata.remove(ORIGIN_KEY) {
10705 Some(Value::String(origin)) if !origin.is_empty() => {
10706 metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin));
10707 }
10708 _ => {}
10709 }
10710 match incoming.written.kind() {
10711 BoardKind::Work(kind) => metadata.insert(
10712 ItemKind::METADATA_KEY.to_owned(),
10713 Value::String(kind.marker().to_owned()),
10714 ),
10715 // A document is told by its title, so it carries no kind marker: that key names
10716 // what a dependency endpoint points at, and nothing may point at a document.
10717 BoardKind::Document => metadata.remove(ItemKind::METADATA_KEY),
10718 };
10719 incoming.classification.record(&mut metadata);
10720 let derivable = own_repository
10721 .map(|own| incoming.repositories == [own.clone()])
10722 .unwrap_or(incoming.repositories.is_empty());
10723 if derivable {
10724 metadata.remove(Repository::METADATA_KEY);
10725 } else {
10726 metadata.insert(
10727 Repository::METADATA_KEY.to_owned(),
10728 Value::Array(
10729 incoming
10730 .repositories
10731 .iter()
10732 .map(|repository| Value::String(repository.as_str().to_owned()))
10733 .collect(),
10734 ),
10735 );
10736 }
10737 // The typed lists are what land, whatever the caller's own metadata held under their
10738 // keys: a key of either name travelling beside the field would otherwise be a second
10739 // answer to the same question, and the field is the one the contract names.
10740 for (key, entries) in [
10741 (TaskRef::DELIVERS_KEY, incoming.delivers),
10742 (TaskRef::DELIVERED_BY_KEY, incoming.delivered_by),
10743 ] {
10744 set_task_list(&mut metadata, key, entries);
10745 }
10746 record_edges(&mut metadata, fallback);
10747 metadata
10748}
10749
10750/// Hold the far ends no relationship here can name under [`DependencyEdge::RECORDED_KEY`] in
10751/// one slot's metadata, or no such key when there are none.
10752fn record_edges(metadata: &mut BTreeMap<String, Value>, fallback: &[DependencyEdge]) {
10753 if fallback.is_empty() {
10754 metadata.remove(DependencyEdge::RECORDED_KEY);
10755 } else {
10756 metadata.insert(
10757 DependencyEdge::RECORDED_KEY.to_owned(),
10758 Value::Array(
10759 fallback
10760 .iter()
10761 .map(|edge| json!({"id":edge.to.id(),"kind":edge.to.kind}))
10762 .collect(),
10763 ),
10764 );
10765 }
10766}
10767
10768/// Every label one item carries, from its content's own connection and nowhere else.
10769///
10770/// There is no second place to read one from: no document this source sends selects the
10771/// board's built-in `Labels` field, because GitHub derives it from the content and a draft
10772/// cannot carry one at all. The module documentation records the three schema facts that
10773/// settle it.
10774fn labels(content: &Value) -> Result<Vec<Label>, SourceError> {
10775 optional_nodes(content.get("labels"), "content labels")?
10776 .into_iter()
10777 .flatten()
10778 .map(|v| {
10779 Ok(Label {
10780 id: NativeId(required_str(v, "id")?.to_owned()),
10781 name: required_str(v, "name")?.to_owned(),
10782 color: optional_str(v, "color")?.map(str::to_owned),
10783 })
10784 })
10785 .collect()
10786}
10787
10788/// The definition of each board field one item's values are values of, in the shape a read
10789/// of the board's own `fields` gives one.
10790///
10791/// A value names its field through a fragment on that field's own type, so the type is
10792/// known from which kind of value it is: a single-select value's field is a
10793/// `ProjectV2SingleSelectField`, options and all, and a text value's is a `ProjectV2Field`.
10794/// A value whose field carried no id, or an empty one, says nothing usable and is left out.
10795fn field_definitions(field_values: &[Value]) -> Vec<Value> {
10796 field_values
10797 .iter()
10798 .filter_map(|value| {
10799 let field = value.get("field")?.as_object()?;
10800 field.get("id")?.as_str().filter(|id| !id.is_empty())?;
10801 let typename = if value.get("text").is_some() {
10802 "ProjectV2Field"
10803 } else if value.get("name").is_some() {
10804 "ProjectV2SingleSelectField"
10805 } else {
10806 return None;
10807 };
10808 let mut defined = field.clone();
10809 defined.insert("__typename".to_owned(), json!(typename));
10810 // Only a text field holds a text value, so the value says the field's type too.
10811 if typename == "ProjectV2Field" {
10812 defined.insert("dataType".to_owned(), json!("TEXT"));
10813 }
10814 Some(Value::Object(defined))
10815 })
10816 .collect()
10817}
10818
10819fn text_field(field_values: &[Value], name: &str) -> Result<Option<String>, SourceError> {
10820 let Some(node) = field_values
10821 .iter()
10822 .find(|node| node.pointer("/field/name").and_then(Value::as_str) == Some(name))
10823 else {
10824 return Ok(None);
10825 };
10826 Ok(optional_str(node, "text")?.map(str::to_owned))
10827}
10828
10829fn valid_github_owner(owner: &str) -> bool {
10830 !owner.is_empty()
10831 && owner.len() <= 39
10832 && !owner.starts_with('-')
10833 && !owner.ends_with('-')
10834 && !owner.contains("--")
10835 && owner
10836 .bytes()
10837 .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
10838}
10839
10840/// GitHub's repository-name grammar: 1-100 ASCII letters, digits, `-`, `_` or `.`, and
10841/// neither of the two names a path segment already means.
10842fn valid_github_repository_name(name: &str) -> bool {
10843 !name.is_empty()
10844 && name.len() <= 100
10845 && name != "."
10846 && name != ".."
10847 && name
10848 .bytes()
10849 .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
10850}
10851
10852fn valid_environment_name(name: &str) -> bool {
10853 let mut bytes = name.bytes();
10854 bytes
10855 .next()
10856 .is_some_and(|byte| byte.is_ascii_alphabetic() || byte == b'_')
10857 && bytes.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
10858}
10859
10860/// How many sub-issues one issue has.
10861///
10862/// `Issue.subIssuesSummary` is `SubIssuesSummary!` and its `total` is `Int!`, so an
10863/// absent or non-integer one is a response this source cannot read — and reading it as
10864/// zero would classify a project as a task, which is exactly the mistake the marker
10865/// exists to keep from happening quietly.
10866fn sub_issue_total(issue: &Value) -> Result<u64, SourceError> {
10867 let summary = issue
10868 .get("subIssuesSummary")
10869 .ok_or_else(|| SourceError::Malformed {
10870 message: "GitHub issue is missing subIssuesSummary".into(),
10871 })?;
10872 summary
10873 .get("total")
10874 .and_then(Value::as_u64)
10875 .ok_or_else(|| SourceError::Malformed {
10876 message: "GitHub issue subIssuesSummary.total is not an unsigned integer".into(),
10877 })
10878}
10879
10880/// One issue's own `number`.
10881///
10882/// An issue always has one: GitHub declares `Issue.number` as `Int!` and every selection of
10883/// an issue in this module asks for it. So a read of one that comes back without it, or
10884/// with something that is not an unsigned integer, is a response this source cannot read —
10885/// absence here is **not** "this issue has no number". A draft is the content that has
10886/// none, and a draft never reaches this: the caller decides on `__typename` first, the way
10887/// it does for `subIssuesSummary`, which `DraftIssue` equally declares nothing for.
10888fn issue_number(issue: &Value) -> Result<u64, SourceError> {
10889 issue
10890 .get("number")
10891 .and_then(Value::as_u64)
10892 .ok_or_else(|| SourceError::Malformed {
10893 message: "GitHub issue number is missing or is not an unsigned integer".into(),
10894 })
10895}
10896
10897/// The `number` a creating mutation answered with, and `None` when it answered without one;
10898/// why a missing one is tolerated is at the call in `create_and_file_issue`.
10899fn created_issue_number(created: &Value) -> Result<Option<u64>, SourceError> {
10900 match created.get("number") {
10901 None | Some(Value::Null) => Ok(None),
10902 Some(value) => value
10903 .as_u64()
10904 .map(Some)
10905 .ok_or_else(|| SourceError::Malformed {
10906 message: "GitHub created issue number is not an unsigned integer".into(),
10907 }),
10908 }
10909}
10910
10911fn required_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
10912 value
10913 .get(field)
10914 .and_then(Value::as_str)
10915 .ok_or_else(|| SourceError::Malformed {
10916 message: format!("GitHub response is missing string field {field}"),
10917 })
10918}
10919
10920fn required_nonblank_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
10921 let found = required_str(value, field)?;
10922 if found.trim().is_empty() {
10923 return Err(SourceError::Malformed {
10924 message: format!("GitHub response has blank string field {field}"),
10925 });
10926 }
10927 Ok(found)
10928}
10929
10930/// The slot's delimiters, which `docs/metadata.md` settles once for every source that
10931/// needs one — Linear spells them too, in its own description field.
10932///
10933/// Restated rather than shared, because a plugin crate depends on the contract crate and
10934/// nothing else of this workspace. `scripts/check-metadata-slot-encoding.sh`, a target in
10935/// `check`, is what keeps the two one encoding: drift is otherwise quiet, since each
10936/// source round-trips its own writes perfectly well under its own spelling.
10937const METADATA_OPEN: &str = "<!-- onetaskgraph.metadata\n";
10938const METADATA_CLOSE: &str = "\n-->";
10939
10940/// What the composer puts between a non-empty visible body and the slot, and the one thing
10941/// the parser takes off the visible body when it takes the slot off — exactly once, so every
10942/// other trailing byte of the body comes back as it was written.
10943// 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.
10944const METADATA_SEPARATOR: &str = "\n\n";
10945
10946/// The visible body and the metadata slot at the end of it.
10947///
10948/// The encoding is the one `docs/metadata.md` settles for Linear, which is where its
10949/// reasons are. Only a comment at the very end is a slot; one in the middle is a person's
10950/// own content and is left alone. The visible body is everything before the slot less the
10951/// one [`METADATA_SEPARATOR`] the composer put there, byte for byte.
10952fn metadata_body(
10953 body: Option<String>,
10954) -> Result<(Option<String>, BTreeMap<String, Value>), SourceError> {
10955 let Some(body) = body else {
10956 return Ok((None, BTreeMap::new()));
10957 };
10958 let Some(slot) = slot_span(&body)? else {
10959 return Ok((Some(body), BTreeMap::new()));
10960 };
10961 let metadata =
10962 serde_json::from_str(&body[slot.encoded_start..slot.encoded_end]).map_err(|error| {
10963 SourceError::Malformed {
10964 message: format!(
10965 "invalid canonical JSON in GitHub issue onetaskgraph metadata slot: {error}"
10966 ),
10967 }
10968 })?;
10969 let before = &body[..slot.start];
10970 let visible = before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before);
10971 Ok(((!visible.is_empty()).then(|| visible.to_owned()), metadata))
10972}
10973
10974/// Where the metadata slot sits in one body, as byte offsets into it.
10975struct SlotSpan {
10976 /// Where [`METADATA_OPEN`] begins.
10977 start: usize,
10978 /// Where the encoded JSON begins, just past [`METADATA_OPEN`].
10979 encoded_start: usize,
10980 /// Where the encoded JSON ends, at the start of [`METADATA_CLOSE`].
10981 encoded_end: usize,
10982 /// Just past [`METADATA_CLOSE`].
10983 end: usize,
10984}
10985
10986/// The slot at the very end of `body`, or `None` when it has none.
10987///
10988/// The one reading of *where the slot is*, shared by [`metadata_body`], which reads it, and
10989/// [`with_slot`], which rewrites it — so the two cannot disagree about which comment is the
10990/// slot.
10991fn slot_span(body: &str) -> Result<Option<SlotSpan>, SourceError> {
10992 let Some(start) = body.rfind(METADATA_OPEN) else {
10993 return Ok(None);
10994 };
10995 let encoded_start = start + METADATA_OPEN.len();
10996 let Some(relative_end) = body[encoded_start..].find(METADATA_CLOSE) else {
10997 return Err(SourceError::Malformed {
10998 message: "unterminated onetaskgraph metadata slot in GitHub issue body".into(),
10999 });
11000 };
11001 let encoded_end = encoded_start + relative_end;
11002 let end = encoded_end + METADATA_CLOSE.len();
11003 if !body[end..].trim().is_empty() {
11004 return Ok(None);
11005 }
11006 Ok(Some(SlotSpan {
11007 start,
11008 encoded_start,
11009 encoded_end,
11010 end,
11011 }))
11012}
11013
11014/// `body` with its metadata slot holding exactly `metadata`, and every byte outside the
11015/// slot as it was.
11016///
11017/// A slot that is there has its JSON replaced in place; one that becomes empty is removed
11018/// together with the one [`METADATA_SEPARATOR`] separating it from the prose before it. A
11019/// body with no slot gains one the way [`compose_body`] writes it — after that separator,
11020/// or alone in an empty body — and a body with no slot that is given no metadata is
11021/// returned as it is.
11022fn with_slot(body: &str, metadata: &BTreeMap<String, Value>) -> Result<String, SourceError> {
11023 let encoded = if metadata.is_empty() {
11024 None
11025 } else {
11026 Some(
11027 serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
11028 message: error.to_string(),
11029 })?,
11030 )
11031 };
11032 Ok(match (slot_span(body)?, encoded) {
11033 (Some(slot), Some(encoded)) => format!(
11034 "{}{encoded}{}",
11035 &body[..slot.encoded_start],
11036 &body[slot.encoded_end..]
11037 ),
11038 (Some(slot), None) => {
11039 let before = &body[..slot.start];
11040 format!(
11041 "{}{}",
11042 before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before),
11043 &body[slot.end..]
11044 )
11045 }
11046 (None, None) => body.to_owned(),
11047 (None, Some(encoded)) if body.is_empty() => {
11048 format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
11049 }
11050 (None, Some(encoded)) => {
11051 format!("{body}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
11052 }
11053 })
11054}
11055
11056/// `body` with everything before its metadata slot replaced by `content`, and the slot
11057/// itself kept byte for byte.
11058///
11059/// The inverse of how [`metadata_body`] splits a body: the slot, when there is one, follows
11060/// `content` after the one [`METADATA_SEPARATOR`] the composer puts there — or alone, when
11061/// `content` is empty — so a read of the result reports `content` as the visible body and
11062/// the slot's metadata exactly as it was.
11063fn with_content(body: &str, content: &str) -> Result<String, SourceError> {
11064 let Some(slot) = slot_span(body)? else {
11065 return Ok(content.to_owned());
11066 };
11067 let kept = &body[slot.start..];
11068 Ok(if content.is_empty() {
11069 kept.to_owned()
11070 } else {
11071 format!("{content}{METADATA_SEPARATOR}{kept}")
11072 })
11073}
11074
11075/// Hold `entries` under `key` in one slot's metadata, or no such key when there are none.
11076fn set_task_list(metadata: &mut BTreeMap<String, Value>, key: &str, entries: &[TaskRef]) {
11077 if entries.is_empty() {
11078 metadata.remove(key);
11079 } else {
11080 metadata.insert(
11081 key.to_owned(),
11082 Value::Array(
11083 entries
11084 .iter()
11085 .map(|entry| Value::String(entry.as_str().to_owned()))
11086 .collect(),
11087 ),
11088 );
11089 }
11090}
11091
11092fn compose_body(
11093 content: Option<&str>,
11094 metadata: &BTreeMap<String, Value>,
11095) -> Result<Option<String>, SourceError> {
11096 let visible = content.unwrap_or_default();
11097 if metadata.is_empty() {
11098 return Ok((!visible.is_empty()).then(|| visible.to_owned()));
11099 }
11100 let encoded = serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
11101 message: error.to_string(),
11102 })?;
11103 Ok(Some(if visible.is_empty() {
11104 format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
11105 } else {
11106 format!("{visible}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
11107 }))
11108}
11109
11110fn required_bool(value: &Value, field: &str) -> Result<bool, SourceError> {
11111 value
11112 .get(field)
11113 .and_then(Value::as_bool)
11114 .ok_or_else(|| SourceError::Malformed {
11115 message: format!("GitHub response is missing boolean field {field}"),
11116 })
11117}
11118fn optional_str<'a>(value: &'a Value, field: &str) -> Result<Option<&'a str>, SourceError> {
11119 match value.get(field) {
11120 None | Some(Value::Null) => Ok(None),
11121 Some(value) => value
11122 .as_str()
11123 .map(Some)
11124 .ok_or_else(|| SourceError::Malformed {
11125 message: format!("GitHub response field {field} is not a string or null"),
11126 }),
11127 }
11128}
11129fn optional_nodes<'a>(
11130 connection: Option<&'a Value>,
11131 name: &str,
11132) -> Result<Option<&'a Vec<Value>>, SourceError> {
11133 match connection {
11134 None | Some(Value::Null) => Ok(None),
11135 Some(value) => value
11136 .get("nodes")
11137 .and_then(Value::as_array)
11138 .map(Some)
11139 .ok_or_else(|| SourceError::Malformed {
11140 message: format!("GitHub {name}.nodes is not an array"),
11141 }),
11142 }
11143}
11144fn complete_connection(connection: &Value, name: &str, size: u32) -> Result<(), SourceError> {
11145 let page_info = connection
11146 .get("pageInfo")
11147 .ok_or_else(|| SourceError::Malformed {
11148 message: format!("GitHub {name} has no pageInfo"),
11149 })?;
11150 if required_bool(page_info, "hasNextPage")? {
11151 return Err(SourceError::Malformed {
11152 message: format!(
11153 "GitHub {name} exceeds the supported nested connection size of {size}"
11154 ),
11155 });
11156 }
11157 Ok(())
11158}
11159fn optional_time(value: &Value, field: &str) -> Result<Option<DateTime<Utc>>, SourceError> {
11160 optional_str(value, field)?
11161 .map(|timestamp| {
11162 timestamp.parse().map_err(|error| SourceError::Malformed {
11163 message: format!("GitHub response field {field} is not a timestamp: {error}"),
11164 })
11165 })
11166 .transpose()
11167}
11168fn validate_page(page: &PageRequest) -> Result<(), SourceError> {
11169 if page.limit == 0 {
11170 Err(SourceError::Config {
11171 message: "page limit must be at least 1".into(),
11172 })
11173 } else {
11174 Ok(())
11175 }
11176}
11177fn next_cursor(connection: &Value) -> Result<Option<Cursor>, SourceError> {
11178 let page = connection
11179 .get("pageInfo")
11180 .filter(|value| value.is_object())
11181 .ok_or_else(|| SourceError::Malformed {
11182 message: "GitHub connection is missing pageInfo".into(),
11183 })?;
11184 if required_bool(page, "hasNextPage")? {
11185 let cursor = required_str(page, "endCursor")?;
11186 validate_cursor_progress(None, cursor)?;
11187 Ok(Some(Cursor(cursor.into())))
11188 } else {
11189 Ok(None)
11190 }
11191}
11192fn validate_cursor_progress(previous: Option<&str>, next: &str) -> Result<(), SourceError> {
11193 if next.is_empty() || previous == Some(next) {
11194 Err(SourceError::Malformed {
11195 message: "GitHub pagination cursor is empty or did not advance".into(),
11196 })
11197 } else {
11198 Ok(())
11199 }
11200}
11201/// The version of this plugin's opaque narrowing-search cursor.
11202pub const SEARCH_CURSOR_VERSION: u32 = 4;
11203
11204#[derive(Serialize, Deserialize)]
11205#[serde(tag = "state", rename_all = "snake_case", deny_unknown_fields)]
11206enum SearchConnection {
11207 Initial {},
11208 Continuing { after: Cursor },
11209 Exhausted {},
11210}
11211impl SearchConnection {
11212 fn after(&self) -> Option<&str> {
11213 match self {
11214 Self::Continuing { after } => Some(&after.0),
11215 _ => None,
11216 }
11217 }
11218 fn exhausted(&self) -> bool {
11219 matches!(self, Self::Exhausted { .. })
11220 }
11221 /// Whether a cursor naming this position, `offset` rows into its page, is one this
11222 /// plugin could have handed out: a page is resumed only part of the way through it — an
11223 /// offset of a whole page or more would skip rows nobody was given — an initial page
11224 /// only once some of it was handed out, and an exhausted connection has no page to be
11225 /// part of the way through.
11226 fn valid_resume(&self, offset: usize) -> bool {
11227 let within = offset < SEARCH_PAGE_SIZE as usize;
11228 match self {
11229 Self::Initial { .. } => offset > 0 && within,
11230 Self::Continuing { after } => !after.0.is_empty() && within,
11231 Self::Exhausted { .. } => offset == 0,
11232 }
11233 }
11234}
11235
11236/// Versioned source cursor. A zero offset and empty own-write ids are omitted.
11237#[derive(Serialize, Deserialize)]
11238#[serde(deny_unknown_fields)]
11239struct SearchPosition {
11240 version: u32,
11241 connection: SearchConnection,
11242 /// How many rows of the page `connection` starts were already handed out.
11243 #[serde(default, skip_serializing_if = "is_zero")]
11244 offset: usize,
11245 #[serde(default, skip_serializing_if = "Vec::is_empty")]
11246 seen: Vec<NativeId>,
11247 #[serde(default, skip_serializing_if = "Vec::is_empty")]
11248 own: Vec<NativeId>,
11249}
11250impl Default for SearchPosition {
11251 fn default() -> Self {
11252 Self {
11253 version: SEARCH_CURSOR_VERSION,
11254 connection: SearchConnection::Initial {},
11255 offset: 0,
11256 seen: Vec::new(),
11257 own: Vec::new(),
11258 }
11259 }
11260}
11261
11262fn is_zero(offset: &usize) -> bool {
11263 *offset == 0
11264}
11265
11266fn numeric_cursor(cursor: Option<&Cursor>) -> Result<usize, SourceError> {
11267 cursor.map_or(Ok(0), |c| {
11268 c.0.parse().map_err(|_| SourceError::Config {
11269 message: "page cursor is invalid".into(),
11270 })
11271 })
11272}
11273fn offset_page<T>(mut items: Vec<T>, offset: usize, limit: usize) -> Page<T> {
11274 if offset > items.len() {
11275 return Page::last(vec![]);
11276 }
11277 let tail = items.split_off(offset);
11278 let mut selected = tail;
11279 let next = (selected.len() > limit).then(|| Cursor((offset + limit).to_string()));
11280 selected.truncate(limit);
11281 Page {
11282 items: selected,
11283 next,
11284 }
11285}
11286
11287impl GitHubProjectsSource {
11288 async fn write_task_assets(
11289 &self,
11290 write: &ItemWrite<Task>,
11291 assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
11292 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
11293 let near = write.target.as_ref().unwrap_or(&write.item.id);
11294 for (key, entries) in [
11295 (TaskRef::DELIVERS_KEY, &write.item.delivers),
11296 (TaskRef::DELIVERED_BY_KEY, &write.item.delivered_by),
11297 ] {
11298 TaskRef::listed(key, near, Some(&self.name), entries.clone())
11299 .map_err(|message| SourceError::Refused { message })?;
11300 }
11301 if self.priorities.is_none() && write.item.priority != Priority::None {
11302 return Err(self.holds_no_priority());
11303 }
11304 self.write_item(
11305 &Incoming {
11306 written: Written::Work(ItemKind::Task, &write.item.status),
11307 title: &write.item.title,
11308 content: write.item.content.as_deref(),
11309 assets,
11310 labels: &write.item.labels,
11311 metadata: &write.item.metadata,
11312 repositories: &write.item.repositories,
11313 classification: write.item.classification,
11314 parent: write.item.project.as_ref(),
11315 delivers: &write.item.delivers,
11316 delivered_by: &write.item.delivered_by,
11317 priority: self.priorities.as_ref().map(|_| write.item.priority),
11318 },
11319 write.target.as_ref(),
11320 &write.depends_on,
11321 )
11322 .await
11323 }
11324 async fn write_document_assets(
11325 &self,
11326 write: &ItemWrite<Document>,
11327 assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
11328 ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
11329 // A document takes part in no dependency graph, so there is no far end to write
11330 // natively and none to record: a caller naming one is told so rather than having it
11331 // stored under the reserved key, where a later read would report an edge the
11332 // contract says cannot exist.
11333 if !write.depends_on.is_empty() {
11334 return Err(SourceError::Refused {
11335 message: format!(
11336 "this write names {} dependencies for a document, and a document takes \
11337 part in no dependency graph; next: put the dependency on the task or \
11338 project the document is about",
11339 write.depends_on.len()
11340 ),
11341 });
11342 }
11343 self.write_item(
11344 &Incoming {
11345 written: Written::Document,
11346 title: &write.item.title,
11347 content: write.item.content.as_deref(),
11348 assets,
11349 labels: &write.item.labels,
11350 metadata: &write.item.metadata,
11351 repositories: &write.item.repositories,
11352 classification: write.item.classification,
11353 parent: write.item.project.as_ref(),
11354 delivers: &[],
11355 delivered_by: &[],
11356 priority: None,
11357 },
11358 write.target.as_ref(),
11359 &[],
11360 )
11361 .await
11362 }
11363}
11364
11365/// What [`TaskSource::end_command`] leaves of this source's held state, asserted on the state
11366/// itself, for the two things no journey can observe.
11367///
11368/// The journeys in `crates/onetaskgraph-e2e/tests/e2e/end_command.rs` prove through the engine,
11369/// with and without the call, that a settlement, a board listing and a metadata search each
11370/// read afresh after it — the resolved records, the written-item overlay, the board and its
11371/// search, and the narrowed searches. What they cannot reach is the held field definitions,
11372/// because a status write naming an option a person deleted is refused the same whether or
11373/// not the list is held, and a poisoned lock, because nothing outside the source can panic
11374/// while one of its locks is held. So these assert those directly, and every other holder
11375/// beside them so a holder added later without a clear in the call fails here.
11376#[cfg(test)]
11377mod end_command_tests {
11378 use super::*;
11379
11380 struct Token;
11381
11382 impl SecretResolver for Token {
11383 fn get(&self, var: &str) -> Option<SecretString> {
11384 (var == "GH_PROJECTS_TOKEN").then(|| "test-token".into())
11385 }
11386 }
11387
11388 fn source() -> GitHubProjectsSource {
11389 let config = serde_json::from_value(json!({
11390 "owner": "octo-org", "project_number": 7, "repository": "acme/work",
11391 // Nothing here is sent: the source is only built and its state inspected.
11392 "endpoint": "http://127.0.0.1:9/graphql",
11393 }))
11394 .expect("a usable configuration");
11395 GitHubProjectsSource::new(&SourceName::new("work").unwrap(), config, &Token)
11396 .expect("the source builds")
11397 }
11398
11399 /// One issue as a board read answers it.
11400 fn resolved(source: &GitHubProjectsSource) -> Resolved {
11401 source
11402 .resolve(&json!({
11403 "id": "ITEM-1",
11404 "content": {"__typename": "Issue", "id": "I_1", "title": "Held",
11405 "body": "what a person may since have edited", "state": "OPEN",
11406 "stateReason": null, "url": null, "number": 1,
11407 "subIssuesSummary": {"total": 0},
11408 "labels": {"nodes": [], "pageInfo": {"hasNextPage": false}}},
11409 "fieldValues": {"nodes": [], "pageInfo": {"hasNextPage": false}},
11410 }))
11411 .expect("the item reads")
11412 .expect("an issue")
11413 }
11414
11415 /// Hold something in every holder the call clears, and the repository id it keeps.
11416 fn fill(source: &GitHubProjectsSource) {
11417 let item = resolved(source);
11418 source.created.lock().unwrap().push(item.clone());
11419 source.updated.lock().unwrap().push(item.clone());
11420 *source.board_cache.lock().unwrap() = Some(Board {
11421 id: "PVT-board".into(),
11422 fields: json!({"nodes": []}),
11423 items: vec![item.clone()],
11424 });
11425 *source.search_cache.lock().unwrap() = Some(vec![item.clone()]);
11426 source
11427 .narrowed_cache
11428 .lock()
11429 .unwrap()
11430 .insert("status:todo".into(), vec![item.clone()]);
11431 source.children_cache.lock().unwrap().insert(
11432 NativeId("P-1".into()),
11433 (NativeId("P-1".into()), vec![item.clone()]),
11434 );
11435 source
11436 .search_next
11437 .lock()
11438 .unwrap()
11439 .insert("status:todo".into(), Some("cursor".into()));
11440 source
11441 .resolved_cache
11442 .lock()
11443 .unwrap()
11444 .insert(item.id.clone(), item);
11445 *source.fields_cache.lock().unwrap() = Some(BoardFields {
11446 id: BoardId::parse("PVT-board").unwrap(),
11447 fields: json!({"nodes": []}),
11448 });
11449 source
11450 .repository_cache
11451 .lock()
11452 .unwrap()
11453 .insert(RepositoryTarget::parse("acme/work").unwrap(), "R_1".into());
11454 }
11455
11456 fn assert_dropped(source: &GitHubProjectsSource) {
11457 assert!(source.created().unwrap().is_empty(), "created");
11458 assert!(source.updated().unwrap().is_empty(), "updated");
11459 assert!(source.board_cache().unwrap().is_none(), "board");
11460 assert!(source.search_cache.lock().unwrap().is_none(), "search");
11461 assert!(source.narrowed_cache.lock().unwrap().is_empty(), "narrowed");
11462 assert!(
11463 source.children_cache.lock().unwrap().is_empty(),
11464 "project children"
11465 );
11466 assert!(
11467 source.search_next.lock().unwrap().is_empty(),
11468 "search paging"
11469 );
11470 assert!(
11471 source.resolved_cache().unwrap().is_empty(),
11472 "resolved records"
11473 );
11474 assert!(source.fields_cache().unwrap().is_none(), "board fields");
11475 assert_eq!(
11476 source.repository_cache().unwrap().len(),
11477 1,
11478 "a repository's node id stays valid and is kept"
11479 );
11480 }
11481
11482 fn end(source: &GitHubProjectsSource) {
11483 tokio::runtime::Builder::new_current_thread()
11484 .build()
11485 .unwrap()
11486 .block_on(source.end_command())
11487 .expect("the command ends");
11488 }
11489
11490 #[test]
11491 fn the_call_drops_every_item_search_and_board_read_and_keeps_repository_ids() {
11492 let source = source();
11493 fill(&source);
11494 end(&source);
11495 assert_dropped(&source);
11496 }
11497
11498 #[test]
11499 fn the_call_clears_a_lock_an_earlier_failure_poisoned() {
11500 fn poison<T: Send>(held: &Mutex<T>) {
11501 std::thread::scope(|scope| {
11502 let _ = scope
11503 .spawn(|| {
11504 let _guard = held.lock().unwrap();
11505 panic!("a failure while the lock is held");
11506 })
11507 .join();
11508 });
11509 assert!(held.is_poisoned());
11510 }
11511 let source = source();
11512 fill(&source);
11513 poison(&source.created);
11514 poison(&source.updated);
11515 poison(&source.board_cache);
11516 poison(&source.search_cache);
11517 poison(&source.narrowed_cache);
11518 poison(&source.search_next);
11519 poison(&source.resolved_cache);
11520 poison(&source.fields_cache);
11521 assert!(
11522 source.resolved_cache().is_err(),
11523 "a poisoned lock is refused before the call"
11524 );
11525 end(&source);
11526 assert_dropped(&source);
11527 }
11528}
11529
11530#[cfg(test)]
11531mod unprojectable_fields_tests {
11532 use super::*;
11533
11534 /// The field names `docs/metadata.md` says a `metadata_fields` entry may not name.
11535 fn documented() -> std::collections::BTreeSet<String> {
11536 let document = std::fs::read_to_string(
11537 std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../docs/metadata.md"),
11538 )
11539 .expect("docs/metadata.md is readable");
11540 let document = document.split_whitespace().collect::<Vec<_>>().join(" ");
11541 let lead = "a `field` equal, ignoring case, to ";
11542 let start = document.find(lead).expect("the refused-field sentence") + lead.len();
11543 let sentence = &document[start..];
11544 let sentence = &sentence[..sentence
11545 .find("; and two entries naming one field")
11546 .expect("the refused-field sentence ends at the duplicate rule")];
11547 let (own, github) = sentence
11548 .split_once(" or a field GitHub owns on every board (")
11549 .expect("the sentence names GitHub's own fields");
11550 own.split('`')
11551 .skip(1)
11552 .step_by(2)
11553 .chain(
11554 github
11555 .strip_suffix(')')
11556 .expect("GitHub's own fields close the sentence")
11557 .split(", "),
11558 )
11559 .map(str::to_owned)
11560 .collect()
11561 }
11562
11563 #[test]
11564 fn the_documented_refused_fields_are_exactly_the_ones_the_configuration_refuses() {
11565 let refused: std::collections::BTreeSet<String> = UNPROJECTABLE_FIELDS
11566 .iter()
11567 .map(|&name| name.to_owned())
11568 .collect();
11569 assert_eq!(documented(), refused, "the fields docs/metadata.md lists");
11570 let instance = SourceName::new("work").unwrap();
11571 for name in UNPROJECTABLE_FIELDS {
11572 for spelled in [name.to_owned(), name.to_uppercase()] {
11573 let entry = MetadataFieldConfig {
11574 field: spelled.clone(),
11575 key: "team.machine".to_owned(),
11576 path: Vec::new(),
11577 };
11578 let refused = MetadataField::resolve(vec![entry], &instance)
11579 .expect_err("an unprojectable field is refused");
11580 assert!(
11581 refused.to_string().contains(&format!("{name:?}")),
11582 "{spelled}: {refused}"
11583 );
11584 }
11585 }
11586 }
11587}