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// 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.
84//! **Status.** `status_mapping` is per-instance configuration, in the shared grammar
85//! [`onetaskgraph_plugin_api::StatusMapping`] documents, from a status category to an
86//! option of the board's one `Status` field for a task and for a project: a bare name is
87//! the option for both kinds, `null` disables the category for both, and `{task, project}`
88//! names it per kind. A category the mapping does not mention keeps its shipped default for
89//! both kinds; one it mentions is exactly what it configures, so a per-kind object no longer
90//! gets the shipped default for the kind it leaves out. Two categories one kind would read
91//! back from one option are refused as the configuration is read, ignoring case, while one
92//! option may stand for different categories of the two kinds. Writes go by the kind of the
93//! item written: a status that kind has no option for, or whose option the board lacks, is
94//! refused before any mutation, naming the source, the kind, the category and the key
95//! `status_mapping.<category>.<kind>` — there is no fallback. `done` selects its mapped
96//! option and closes the issue as `COMPLETED`; `cancelled` selects its mapped option and
97//! closes it as `NOT_PLANNED`, for either kind. Every open category reopens a closed issue
98//! before selecting its option. Reads give a closed issue's reason precedence over its
99//! option, while an open issue's option decides its category through its own kind's
100//! mapping, and an option that mapping does not name reads as `unknown` under its own name.
101//! The guarded [`GitHubProjectsSource::status_options`] and
102//! [`GitHubProjectsSource::fields`] operations are the one path here that calls
103//! `updateProjectV2Field`: GitHub replaces the whole option list, so they preserve every
104//! existing option id and verify the field and item assignments immediately afterwards.
105//! They ask for both kinds' options, counting a terminal category's mapped option as
106//! configured because a terminal write refuses without it. No ordinary source read or
107//! write calls that mutation, whose
108//! `singleSelectOptions` *overwrites* a field's option set, so no addition is additive
109//! and a mistake destroys every item's status. A status this board cannot represent is a
110//! refusal naming the status and the instance instead.
111//!
112//! `unknown` has no shipped option because this source cannot preserve an open-ended
113//! status word: it writes an existing board option and never
114//! creates an option. An operator may map `unknown` to one existing option, in which case
115//! every unknown word lands on that option and reads back as `unknown` under the option's
116//! name. This differs from `local-md`, which writes and reads the original word itself.
117//!
118//! The shipped terminal options are exactly `done: Done` and `cancelled: Cancelled`.
119//! `done` also closes the issue because GitHub derives `subIssuesSummary.completed`
120//! and the board's own `Sub-issues progress` field from closed sub-issues: a plan whose
121//! finished tasks were only moved to a "Done" column would read 0% complete forever.
122// llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
123//!
124//! # What this source declares, field by field
125//!
126//! One verdict per field of [`Capabilities`], and what `Native` means when this source
127//! says it. *Proven* means a shared journey drives it against the real
128//! binary over this source's own row in `crates/onetaskgraph-e2e-support/src/fixtures.rs`, and
129//! `every_row_declares_exactly_what_its_plugin_reports` is what keeps this list and
130//! [`capabilities`](TaskSource::capabilities) from parting.
131//!
132//! | Field | Verdict |
133//! | --- | --- |
134//! | `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. |
135//! | `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. |
136//! | `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. |
137//! | `assets` | **Unsupported — unimplemented.** A copy of a record carrying an image asset into a board is refused, naming the source, the record and the asset, before anything is written for that record. Storing the bytes where the issue renders them is tracked in `docs/follow-ups.md`. |
138//! | `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. |
139//! | `filter_by_priority` | **Supported and proven,** over the priority each task reads as — `none` for every task of an instance without `priority_mapping`. |
140//! | `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. |
141//! | `orphan_tasks` | **Supported and proven.** A task issue with no `parent` is in no project. |
142//! | `filter_by_label` | **Supported and proven,** over the issue's own labels. |
143//! | `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. |
144//! | `filter_by_metadata` | **Supported, and asked of GitHub.** 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. |
145//! | `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. |
146//! | `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. |
147//! | `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. |
148//! | `task_dependencies` | **Supported and proven,** in both directions: `blockedBy` and `blocking`. |
149//! | `project_dependencies` | **Supported and proven,** in both directions, over the same two connections, because a project here is an issue. |
150//! | `max_page_size` | **Supported and proven.** [`MAX_PAGE_SIZE`], GitHub's own connection maximum. |
151//!
152//! Nothing here is unsupported. `documents` and `comments` are not predicates — they say this
153//! source has documents and that its tasks have comments, both of which hold — and the three
154//! facts behind the uniform `Native` on the
155//! predicates beside it are recorded below rather than re-derived, because a reader who
156//! takes `Native` to mean *the remote service filters* will read that uniformity as a
157//! lie.
158//!
159//! First, the plugin contract defines `Support::Native` as *the source applies this
160//! predicate itself*, and says nothing about where it applies it. What the declaration
161//! promises the engine is capability rule 1 — a predicate declared `Native` **is** applied
162//! — so that the engine may push it down and apply nothing of its own.
163//!
164//! Second, this source can keep that promise for every predicate at no additional API
165//! cost, because whichever of the reads below answers a query has already read every
166//! candidate that query will return before it filters anything. Filtering those items is
167//! in-process work over data already in hand.
168//!
169//! Third, six task predicates are asked of GitHub as a narrower question and the rest are
170//! applied in process over what that question returned. A project filter has a relationship — a
171//! project's tasks are that issue's sub-issues, and asking the issue for them is both cheaper
172//! and exact. Comment activity is the issue search's `updated:` qualifier. A text search, and
173//! a search for metadata values, is the board-scoped issue search carrying the text and each
174//! value as quoted phrases; an origin is the board's own field filter over its origin field
175//! beside the same search for the id. **The text search narrows, and that is this source's
176//! declared semantics:** GitHub matches whole words where the substring rule this source and
177//! the local Markdown source confirm with would match inside one, so an item holding the text
178//! only inside a longer word is never a candidate. Every item returned does contain the text.
179//! A project query's text, and a document query's scoped to no project, is that same search
180//! and narrows on the same terms, its candidates confirmed by their kind as well.
181//! GitHub's issue search offers no qualifier for a label set, a status column or a priority,
182//! so those three are applied in process over the candidates, and a query carrying none of
183//! the six narrowing predicates reads the board. Declaring one `Unsupported` would make the
184//! engine compensate for work this source has already done, and declaring `projects` native
185//! while ignoring the filter (which this source once did) silently returns another project's
186//! tasks, because the engine trusts the declaration and applies nothing locally.
187//!
188//! # The three ways this source reaches an item, and what each costs
189//!
190//! A board read is charged for what its *nested* connections could return rather than for
191//! what was asked, so one whole-board read costs the same whether the question was about
192//! one project or about all of them. That is why a question about one project is never
193//! answered by reading the board:
194//!
195//! | The question | What is sent | What it costs |
196//! | --- | --- | --- |
197//! | 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 |
198//! | 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 |
199//! | 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 |
200//! | 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 |
201//! | one project's tasks or documents | [`graphql::SUB_ISSUES`] — that issue's own `subIssues` | that project |
202//! | which projects this board holds | [`graphql::SEARCH_ISSUES`] — an issue search scoped to the board | the board's issues, without their board items |
203//! | 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 |
204//! | 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 |
205//! | 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 |
206//! | 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 |
207//! | 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 |
208//! | 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 |
209//!
210//! The following standalone-ticket requests are pinned by the real CLI fixture journeys
211//! `follow_up_writes_resolve_each_item_once_and_batch_the_copy_fields` and
212//! `a_batched_detail_read_costs_one_request_and_one_point_per_detail_batch`, as request count
213//! equal to declared points equal to the row. They include the origin lookup and the
214//! field/repository discovery a create needs. A bound re-copy changes status, priority,
215//! content and metadata; comment recount means a subsequent detail read. Each request here
216//! costs one declared point. A membership beyond the embedded page can additionally require
217//! the one-point membership recovery described above. A bound re-copy of a task filed under a
218//! project adds one read, the engine confirming that project's link by its own id once per
219//! command; and the same-source far ends a write newly names — those that do not already block
220//! the item, whose own read answered for them — are read together by their own ids,
221//! [`DETAIL_BATCH`] to one [`graphql::ISSUE_DETAILS`] request, each new edge then one
222//! [`graphql::ADD_BLOCKED_BY`]. Both additions are rows of the table below, pinned by
223//! `a_bound_recopy_adds_one_project_read_and_batches_the_dependencies_it_newly_names`.
224//!
225//! **[`DETAIL_BATCH`] is 24**: the largest batch of [`graphql::ISSUE_DETAILS`] the node-count
226//! model prices at one point. Each aliased item is six of GitHub's aggregate, so 24 are 144,
227//! which rounds to one point, and 25 are 150, which rounds to two; `tests/point_cost.rs`
228//! holds both halves.
229//!
230//! **An existing item is written body last.** A bound re-copy and a `task update` send its
231//! board fields first — the `Status` option and the `Priority` together, in one request — then
232//! its parent and its `blockedBy`, and its title, body and state in one `updateIssue` last.
233//! GitHub runs no two requests as one, and runs a document's mutation fields in order without
234//! undoing an earlier field when a later one fails, so that order is what makes a write
235//! refused part-way leave the item's body, and every metadata key in it, exactly as it stood;
236//! the one piece of metadata written before the body, an origin a copy re-points, is put back
237//! when a later write is refused — and when putting it back is refused too, the write's own
238//! refusal names that key, what it now holds and what it held. `crates/onetaskgraph-github-projects-e2e/tests/e2e/write_order.rs` refuses each
239//! of those writes in turn, whole and as one aliased field failing after the one before it.
240//!
241//! **Two facts about GitHub the write rows rest on, each read off GitHub's published schema
242//! artifact <https://docs.github.com/public/fpt/schema.docs.graphql> on 2026-10-01 and pinned
243//! in `tests/fixtures/schema.graphql`, and the first then put to GitHub itself:**
244//!
245//! - **A board is accepted at creation but its item is not answered, so a create still files
246//! the issue itself: a new copy is 5 requests, and 4 with `--create`.**
247//! `CreateIssueInput.projectV2Ids: [ID!]` is declared there — "An array of Node IDs for
248//! Projects V2 associated with this issue", `@possibleTypes(concreteTypes: ["ProjectV2"])`.
249//! The credentialed journey `real_projects_v2_contract_writes_and_leaves_no_residue` was run
250//! against a real board on 2026-10-01 with a create sending the board there and reading the
251//! item off the payload's `Issue.projectItems`: every one of its four creates answered with
252//! no item on the board, so each went on to [`graphql::ADD_TO_BOARD`], and the fourth was
253//! refused "Content already exists in this project" — GitHub had filed the issue after
254//! answering, and refuses a second filing rather than answering with the item it holds. A
255//! create therefore sends no `projectV2Ids` and files the issue with
256//! `addProjectV2ItemById`, the one call whose answer names the board item. The saving that is
257//! real is the read before it: the board's fields and the repository's id together, in
258//! [`graphql::CREATION_CONTEXT`], at the point the repository is known.
259//! - **A comment still reads its target first, so a comment is 2 requests.**
260//! `AddCommentInput.subjectId: ID!` is declared there with
261//! `@possibleTypes(concreteTypes: ["Issue", "PullRequest"], abstractType:
262//! "IssueOrPullRequest")`. A board draft is no such subject and would be refused, but a
263//! project's issue, a document's issue, an issue on no board of this source and a pull
264//! request all are: GitHub writes the comment, so there is no refusal to map into "that is
265//! not a task of this board". [`graphql::ISSUE`] before [`graphql::ADD_COMMENT`] is what
266//! refuses those by name.
267//!
268//! | Verb | Requests / points | Documents |
269//! | --- | --- | --- |
270//! | new copy | 5 | ORIGIN_LOOKUP, CREATION_CONTEXT (the board's fields and the repository's id together), CREATE_ISSUE, ADD_TO_BOARD, UPDATE_FIELDS |
271//! | copy --create | 4 | CREATION_CONTEXT, CREATE_ISSUE, ADD_TO_BOARD, UPDATE_FIELDS: the new copy without its ORIGIN_LOOKUP |
272//! | 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 |
273//! | 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 |
274//! | 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 |
275//! | comment | 2 | ISSUE, ADD_COMMENT: the target is read first, because GitHub accepts a comment on any issue or pull request (see below) |
276//! | detail | 1 | ISSUE_DETAIL: the item and its first page of comments, for `task show` and `task comment list`; `--no-comments` is ISSUE alone |
277//! | batched detail | ceil(n / DETAIL_BATCH) | ISSUE_DETAILS: `task show-many` of `n` items, DETAIL_BATCH (24) at a time, comments included or not |
278//! | recount | 1 | ISSUE_DETAIL |
279//! | status | 2 | ISSUE, UPDATE_FIELD; a terminal status additionally updates issue state |
280//! | priority | 2 | ISSUE, UPDATE_FIELD or CLEAR_FIELD, with stored priority in the mutation response |
281//! | content | 2 | ISSUE, UPDATE_ISSUE |
282//! | metadata | 2 | ISSUE, UPDATE_ISSUE |
283//! | 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 |
284//! | record only | 1 | ISSUE |
285//!
286//! <!-- github-search-paging:start -->
287//! Board-scoped text, metadata, project-name and comment-activity searches send every
288//! page at `first = 20` (SEARCH_PAGE_SIZE), the SEARCH_ISSUES document's one-point
289//! ceiling. A later page is sent only when `hasNextPage` is true and the caller still
290//! needs rows. A page is never resized to the rows still needed: GitHub orders one
291//! search differently at different page sizes, so one fixed size makes a paged walk
292//! send exactly the requests one whole read sends, and the answer's order is the order
293//! those pages arrive in. A page below twenty would cost the same one point, and GitHub
294//! prices this document by rows, so twenty-row pages cost per row what 100-row pages do.
295//! Project-name lookup continues until an exact match or exhaustion. A task limit bounds
296//! returned and fetched pages: a limit is sliced from the pages it needs, and local
297//! confirmation can require more candidates than matching rows. Walking all pages
298//! returns the whole answer. The opaque version-4 source cursor carries GitHub's page
299//! cursor and how far into that page the last answer stopped, and resumes in the same
300//! process or a new one, without duplicates or gaps. It carries no rows: one process
301//! sends each page's search once, and a new process re-reads only the page it resumes
302//! in, then sends a further page once, never as a re-read, only when its limit still
303//! needs rows. Every request either walk sends is the one a whole read sends for that page. Own writes replace stale index
304//! copies and complete missing rows at exhaustion. Cache entries are whole GitHub pages,
305//! so a small answer cannot truncate a wider question. Origin pages remain three; whole-board sizing is unchanged.
306//! Read-after-write is a per-process guarantee. A cursor resumed in a new process is
307//! not required to include the original process's writes still omitted by the index.
308//! <!-- github-search-paging:end -->
309//!
310//! The board half of an issue — its board item's id, its `Status` option and this
311//! source's origin text field — rides along on `Issue.projectItems` in the first three, so
312//! an item reached any of those ways resolves through the same
313//! [`GitHubProjectsSource::resolve`] the board walk uses and reports the same title, the
314//! same status, the same labels and the same qualified id. That connection comes back a
315//! *page* at a time, at `BOARD_ITEMS_PAGE_SIZE`, so the entry for this board is looked for
316//! on the page in hand and — only if that page reports more of the connection — in the
317//! last row's read of that one issue's memberships, resumed from the page's own cursor and
318//! walked to exhaustion. An issue with no entry for *this* board is not this source's to
319//! report, which is what keeps an id naming another repository's issue from being answered
320//! as an item of this board; and because the page is where the search starts rather than
321//! where it ends, that answer is one about a connection read to exhaustion and never about
322//! an unread page. Nothing costs the extra read but an issue on more boards than a page
323//! holds: an issue this board really does not hold reports no next page, so its
324//! memberships are already exhausted where they arrived.
325//!
326//! **No document here selects the board's own `Labels` field, and nothing is lost by
327//! that.** An item's labels are read from its content alone, wherever that content is
328//! reached: the three documents above select `Issue.labels` on the fragment, and
329//! [`graphql::BOARD`] selects the same connection on the `... on Issue` arm of its
330//! `content`. A board's `Labels` field is not one anybody fills in: it is a built-in
331//! `ProjectV2FieldType`, it is absent from `ProjectV2CustomFieldType` so no project can
332//! create one, and `ProjectV2FieldValue` — the whole of what
333//! `updateProjectV2ItemFieldValue` accepts — offers no way to write one. So GitHub derives
334//! it from the content, for every content type it exists on, and there is nothing it can
335//! hold that the content does not already say: for an `Issue` it *is* that issue's own
336//! labels, so selecting it beside them unions a set with itself.
337//!
338//! **A draft loses nothing by that either**, which is the reasoning this paragraph once had
339//! backwards. `DraftIssue` exposes no `labels` field, and by the three schema facts above
340//! it cannot carry a board `Labels` value to be derived from one — so a draft has nothing
341//! to select *and nothing to lose*, and reports no labels at all. A `PullRequest` item is
342//! discarded by [`GitHubProjectsSource::resolve`] before labels are read. Both halves are
343//! held to that by tests in `tests/plugin.rs`: the four ways an item is reached report one
344//! label set, and that set is the fixture issue's own, by
345//! `an_item_reports_the_same_labels_title_status_and_id_however_it_is_reached`; and a board
346//! item whose content is a draft reports an empty set, by
347//! `a_board_item_whose_content_is_a_draft_reports_no_labels_at_all`. The absence of the
348//! selection is held over [`graphql::DOCUMENTS`] by
349//! `no_document_selects_the_boards_own_labels_field`.
350//!
351//! The whole-board row is still the board's own item connection, and deliberately: a
352//! **draft** board item is not an issue, so no search can list one, and the reads that have
353//! to answer for the whole board are the ones whose cost is the board's size anyway.
354//!
355//! **A question about one item this source already names by id never lists the board.**
356//! Whether that item is on this board, and what its board fields are, is answered by reading
357//! that item — its own `Issue.projectItems`, walked to exhaustion by
358//! [`GitHubProjectsSource::resolve_issue`], or a draft's own board item — and never by
359//! looking for it in [`graphql::BOARD`]'s `items` or in a listing this command already
360//! holds. That covers a write's destination, the project a new item is filed under, a
361//! same-source far end a dependency names, a status write, the dependency slot a draft keeps,
362//! and the delete that takes back an item a copy made. What such a write needs of the board
363//! and the item does not carry — the board's id, the `Status` and origin field definitions —
364//! comes from [`graphql::BOARD_FIELDS`], which reads no item at all. The reason is evidence,
365//! not economy alone: `ProjectV2.items` is a projection that lags the membership GitHub
366//! itself reports — an issue added with `addProjectV2ItemById` can be missing from it for
367//! minutes. Scanning this host's 842-item board has refused a document copy and an update
368//! even though the items' own reads named that board. A scan there gives the wrong answer
369//! as well as paying for every page. So a `board.items` lookup does not belong on any of
370//! those paths.
371//!
372//! **What a read may return is capped too, and that cap is on the document rather than on
373//! the board.** GitHub limits the number of nodes **one query may return** to
374//! [`NODE_COUNT_LIMIT`] and refuses a query above that before executing it: the answer is
375//! an error naming the connection the count crossed at, not a slow or a partial result.
376//! Every board this source reads is refused the same way, so no board is too big for these
377//! documents and none is small enough to save one that is over.
378//!
379//! The count is arithmetic over the document's own text: each connection contributes the
380//! `first:` it asks for, counts **multiply** down a nested path and **sum** across sibling
381//! paths. Those are [GitHub's published rules][node-limits] and this workspace does not
382//! restate them — `github-graphql-node-count` implements them, and
383//! [`worst_case_node_count`] under [`largest_page_sizes`] is where every node count here
384//! comes from. `every_document_this_source_sends_stays_under_githubs_node_limit`, in
385//! `tests/node_count.rs`, recomputes every document in [`graphql::DOCUMENTS`] from that
386//! same text on every run and fails naming any that reaches the limit — so a connection
387//! added to a shared fragment is caught there rather than by GitHub.
388//!
389//! What decides those counts is the page sizes: [`MAX_PAGE_SIZE`] on the outer page,
390//! `NESTED_PAGE_SIZE` on the connections hanging off one item, and
391//! `BOARD_ITEMS_PAGE_SIZE` on the page of an issue's board memberships a read carries.
392//! `$nestedFirst` is spent twice down one path of a board read, so that constant is
393//! effectively squared there, which is why it is the one the limit is most sensitive to.
394//! `BOARD_ITEMS_PAGE_SIZE` is small for a reason of its own, recorded beside it: what a
395//! page of memberships misses is recovered by one further read rather than refused, so it
396//! buys a bound every read pays for at the price of a request only a multi-board issue
397//! pays.
398//!
399//! **`nodeCount` and `cost` are two numbers against two limits, and both are computed
400//! offline here — per document, one document at a time.** `nodeCount` is the one above: the
401//! most nodes one query may return, checked per query and bounded by [`NODE_COUNT_LIMIT`].
402//! `cost` is rate-limit points, metered per hour across everything one credential does; it
403//! is what the two limiters [`Limiter`] tells apart meter, and a document under
404//! [`NODE_COUNT_LIMIT`] still says nothing about its price. [`worst_case_point_cost`] is
405//! that second number, and `tests/point_cost.rs` pins every document in
406//! [`graphql::DOCUMENTS`] at what it costs — there being no per-call point ceiling to hold
407//! one under, the pin itself is the check. The credentialed lane reconciles both figures
408//! against GitHub's own, off a probe it already sends.
409//!
410//! **What is pinned that way is a per-document price and never a session's.** The record in
411//! `session-cost.md` measures the two quantities a whole session can be counted in offline —
412//! **requests** and **worst-case nodes** — and neither is points. What one whole session
413//! consumes of the hourly point allowance is observable only from a credentialed run's own
414//! `x-ratelimit-*` headers, which is what [`accounting`] fills its per-budget figures from
415//! and what `tests/live.rs` prints at the end of every run.
416//!
417//! [node-limits]: https://docs.github.com/en/graphql/overview/rate-limits-and-node-limits-for-the-graphql-api
418//!
419//! **Where a read-after-write guarantee comes from, since neither of GitHub's two
420//! enumerations of a board can supply one alone.** Resolving a node id is strongly
421//! consistent, so a read by id and a project's own sub-issues are already current. The
422//! other two are not, and they are behind by different amounts and in different directions:
423//!
424//! - GitHub's **issue search** is an index and answers a write made moments ago with the
425//! value from before it — usually for a second or two.
426//! - **`ProjectV2.items`** is a projection GitHub rebuilds behind the write, and an item put
427//! on a board with `addProjectV2ItemById` can be **absent** from it — not present with its
428//! content withheld, absent, with the connection walked to its own `hasNextPage: false` —
429//! for *minutes*, while `Issue.projectItems` names the same membership at once.
430//!
431//! That second one is a measurement rather than a caution. This repository's own
432//! credentialed journey writes a project and waits for the board to report it, then writes a
433//! task and waits for the same thing seconds later on the same board: the project wait is
434//! answered through the search and converged in two or three attempts in each of three runs,
435//! and the task wait is answered through `ProjectV2.items` and converged in none of them
436//! inside thirty. Separately, an item added to a second and larger board was read back by
437//! `Issue.projectItems` on that board's own id while every one of that connection's nine
438//! pages, walked to exhaustion nine minutes after the add, did not name it. Reading a board
439//! through the lagging one alone is what had a board read deny an issue that had certainly
440//! landed on it.
441//!
442//! So [`GitHubProjectsSource::board`] is the **union** of both — each search result still
443//! admitted only on this board's own strongly-consistent `Issue.projectItems`, and neither
444//! enumeration dropped, because only `ProjectV2.items` lists a board draft and only the
445//! search reports what the projection is behind on. What closes the last
446//! gap, the one where both are behind, is [`GitHubProjectsSource::created`]: every read this
447//! source answers is completed with what this process itself wrote, so an item created
448//! seconds ago is reported whether or not GitHub has caught up. Nothing else is remembered,
449//! nothing is written down, and the record dies with the process. **A wait that has to
450//! observe GitHub's own data cannot be answered from that record** — which is why the
451//! credentialed journey asks through a source built afresh, and why the union above rather
452//! than a longer wait is what makes such a wait converge.
453//!
454//! **A narrowed read is the same bargain, stated for each of the three predicates it
455//! answers.** A read carrying a text, metadata or origin predicate asks GitHub's index rather
456//! than walking the board, and every such answer is completed with what this process wrote —
457//! its [`created`](GitHubProjectsSource::created) record and every existing item it wrote,
458//! each filtered by the same predicates as the rest — so an item this command wrote a moment
459//! ago is returned by a query that matches it whether or not the index has caught up. An item
460//! a caller holds the id of is read by that id, with `node(id:)`, which is strongly
461//! consistent. What is left is stated rather than papered over:
462//!
463//! | Read | Finds | Behind by |
464//! | --- | --- | --- |
465//! | text, metadata | the issue search for the phrases | what another process wrote in the last second or two, until GitHub indexes it |
466//! | 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 |
467//! | 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 |
468//! | origin, third read | this process's own writes | nothing |
469//!
470//! So an origin carrier another process added within the last second or two, before either
471//! index has it, can be missing from an origin query, and one written by the release before
472//! this one — its origin in the field alone — can be missing for as long as the board's own
473//! item connection is behind on it. A copy that must not duplicate its own earlier write
474//! relies on the link it records, not on either index. **A board draft is not an issue**, so
475//! a draft is never returned by a text, metadata or origin query, whatever it holds: no search
476//! lists one, the origin lookup drops any the board's own field filter names, and one this
477//! process wrote is not added back either.
478//!
479//! **The origin lives in the board field, and the body holds a mirror of it.** A write that
480//! carries an origin writes it to the `onetaskgraph.origin` text field and also into the
481//! body's metadata slot, so the issue search can find it in seconds. The field is
482//! authoritative: this source reads an item's origin from the field alone, so a slot that
483//! disagrees with it, or holds one where the field holds none, is never read as a second
484//! origin — and the release before this one reads the slot, drops that key's copy for the
485//! field's, and sees the same one origin.
486//!
487//! Filtering happens before paging, so a page of a filtered result is a page of the
488//! survivors rather than the survivors of a page. Label matching and the substring rule a
489//! text candidate is confirmed by answer the same question the same way the local Markdown
490//! source's do; which candidates a text search has to confirm is GitHub's word match, which
491//! is the one place the two sources can answer the same text differently.
492//!
493//! <!-- llmlint: ignore[contracts_have_one_source_or_a_drift_gate] The declaration itself
494//! has one source, `capabilities`, and the note above is the reasoning behind it rather
495//! than a second copy of it: without the three facts recorded here a reader takes the
496//! uniform `Native` for a lie and reverts it. The drift gate on the declaration is this
497//! crate's own capabilities test, which pins every field of it against a fully spelled-out
498//! `Capabilities` literal — a struct with no `Default`, so a field added to the contract
499//! fails to compile there rather than going unasserted. -->
500//! The fixture-server tests above run wherever this crate is selected; the credentialed
501//! lane runs in the same required check, beside them, and can fail it — it verifies the
502//! 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,
503//! one filed under neither, a label on one of the three and a closed status on another —
504//! because that shape is what tells an honoured predicate from an ignored one: a board
505//! holding a single project answers a project filter the same way whether or not this
506//! source applies it, which is exactly how the defect above went unseen.
507//!
508//! That lane writes only to the board `GH_PROJECTS_OWNER` and `GH_PROJECTS_NUMBER` name,
509//! and only into the repository `GH_PROJECTS_REPOSITORY` names, and skips — as it does
510//! without `GH_PROJECTS_TOKEN` — when any of them is absent. Requiring both to be
511//! nominated is what keeps a credentialed write lane off a board and a repository nobody
512//! nominated; it never asks GitHub which project was updated most recently. Before it
513//! starts, the lane also clears any item titled — and any repository label named — the way
514//! it titles and names its own artifacts, which is self-healing after an interrupted run:
515//! a process killed between its writes and its cleanup leaves artifacts the next run
516//! removes.
517//!
518//! # What a session of requests costs, and where the report is
519//!
520//! This source records **every** request it sends into [`accounting::Accounting`], at
521//! `send_once` — the one place a request leaves this crate, which is why a read path added
522//! later is counted without anybody remembering to count it. That is the whole of what this
523//! crate adds to the arrangement; [`accounting`] is where what a record carries, how a
524//! session's spend is arrived at, and what it deliberately does not know are set out.
525//!
526//! What one whole session of the live journey costs, counted that way against this crate's
527//! loopback fixture board, is written down in `session-cost.md` beside this crate — with the
528//! reduction it came out of, and with what it does and does not say about rate-limit points.
529//!
530//! [`GitHubProjectsSource::accounting`] is the read: a snapshot to hold and compare, which
531//! [`accounting::Session::report`] renders the session report from. It is on the ordinary
532//! code path — no environment variable, no feature, no build configuration — because an
533//! instrument nobody switches on measures nothing, and
534//! [`Plugin::build_recording_into`] is how a caller making its own calls beside this
535//! source's counts the whole session rather than this source's share. The credentialed lane
536//! in `tests/live.rs` does exactly that, and prints the report at the end of every run,
537//! passed or failed.
538//!
539//! **A live session refuses to start unless the account can afford it.** Before it does any
540//! of the work it exists to do, the journey makes one request — `GET /rate_limit`, which
541//! GitHub documents as not counting against the REST rate limit and which answers both of
542//! its budgets at once — and starts only if, for each of them, what remains minus this
543//! session's estimated cost is still at least
544//! `onetaskgraph_live::RETAINED_BUFFER` — twenty per cent — of that budget's whole
545//! allowance. A session that cannot **declines**: it did not run, so it is
546//! neither a pass nor a failing assertion, and it says which budget was short, that budget's
547//! limit, what remained, the estimate, the buffer and when it resets — then stops, without
548//! waiting for the budget to come back. The estimate is derived offline from
549//! `tests/fixtures/session-cost.txt` and a cost model stated in `tests/journey/budget.rs`,
550//! which is also where the published rule that model rests on is cited; the accounting
551//! above records the gate's own read like any other request, and
552//! [`accounting::Session::report`] prints the estimate beside what the session really spent.
553//!
554//! **GitHub is the authority on both of its own numbers, and the credentialed lane goes and
555//! asks it.** Everything above computes `nodeCount` and `cost` offline from a document's own
556//! text, which is what lets it run on every platform and on a pull request from a fork with
557//! no credential — and that is what actually stops a regression merging. But an offline
558//! arithmetic can only ever agree with itself: if GitHub changes its rules, this workspace
559//! goes on computing the old answer and nothing notices. So `tests/live.rs` reconciles them.
560//! GitHub's schema exposes `rateLimit(dryRun: true)`, whose `nodeCount` is *"the maximum
561//! number of nodes this query may return"* and whose `cost` is what that document would
562//! spend, both for a document **without executing it**, and the lane asks it for every query
563//! document this source sends, under the largest bindings this source sends, and fails when
564//! GitHub's figure and [`worst_case_node_count`] or [`worst_case_point_cost`] disagree. A
565//! mutation is skipped, because `rateLimit` is a field of `Query` and cannot be asked about
566//! one; the offline pins still cover it. It records what those calls reported about the
567//! account's own allowance, because whether asking is free is a thing to observe rather than
568//! to assume. Two quantities, not one: [`NODE_COUNT_LIMIT`] bounds `nodeCount` per query,
569//! and `cost` is metered against an hourly allowance the accounting above reads off a
570//! credentialed run's own response headers.
571//!
572//! **GitHub has two rate limiters and this source is refused by both, so nothing here
573//! treats them as one thing.** The primary budget is the hourly allowance `gh api
574//! rate_limit` reports; the secondary limiter is a burst limiter over content-generating
575//! requests, and *nothing* reports it. Which one refused decides the operator's next step,
576//! so [`Limiter`] is a type rather than a detail, and it is what [`MIN_MUTATION_INTERVAL_MS`],
577//! [`GitHubProjectsSource::board_cache`] and [`GitHubProjectsSource::graphql`] each answer
578//! one part of.
579#![deny(missing_docs)]
580
581use std::collections::BTreeMap;
582use std::sync::{Arc, Mutex};
583use std::time::{Duration, Instant};
584
585use chrono::{DateTime, Utc};
586use onetaskgraph_plugin_api::{
587 Capabilities, Comment, CommentBody, Cursor, DependencyEdge, DependencyEndpoint, DependencyKind,
588 DependencySupport, Direction, Document, DocumentQuery, Health, ItemKind, ItemWrite, Label,
589 LabelFilter, Location, MetadataKey, Metering, NativeId, NewComment, Page, PageRequest,
590 Priority, Project, ProjectFilter, ProjectQuery, Repository, SecretResolver, SourceError,
591 SourceName, SourcePlugin, Status, StatusCategory, StatusMapping, Support, Task, TaskDetailRead,
592 TaskQuery, TaskRef, TaskSource, TaskUpdate, TaskUpdateOutcome, TextFields, TextQuery,
593 UnmappedStatus, UpdatedField, WriteSupport,
594};
595use reqwest::{Client, StatusCode, Url};
596use schemars::{Schema, schema_for};
597use secrecy::{ExposeSecret, SecretString};
598use serde::{Deserialize, Serialize};
599use serde_json::{Value, json};
600
601pub mod accounting;
602
603use accounting::Accounting;
604
605/// The registry name for this plugin.
606pub const KIND: &str = "github-projects";
607/// GitHub's maximum connection page size.
608pub const MAX_PAGE_SIZE: u32 = 100;
609/// Every page of a board-scoped narrowing search: 20 rows, one point of SEARCH_ISSUES, the
610/// most one point buys. GitHub prices that document by rows, so pages of 20 cost what pages
611/// of 100 cost per row, and a page of fewer than 20 costs the same one point.
612pub const SEARCH_PAGE_SIZE: u32 = 20;
613/// How many items one [`graphql::ISSUE_DETAILS`] request reads, each with the first page of
614/// its comments: the largest batch the node-count model prices at one point.
615///
616/// Each aliased item is resolved once, and what GitHub charges for it is the connections
617/// under it — its labels, its page of board memberships, the field values of each of those
618/// three memberships, and its comments: six requests' worth of the aggregate GitHub divides
619/// by a hundred and rounds. Twenty-four items come to 144, which rounds to one point;
620/// twenty-five come to 150, which rounds to two. `tests/point_cost.rs` prices the document at
621/// one point and fails if one item more would still be priced at one.
622pub const DETAIL_BATCH: usize = 24;
623
624/// The most nodes any one document this source sends may be asked to return.
625///
626/// GitHub's own published per-query ceiling, taken from
627/// [`github_graphql_node_count::NODE_LIMIT`] rather than written out again here, so this
628/// workspace cannot hold a stale copy of somebody else's number. A query above it is
629/// **refused before it is executed**, whoever is asking and whatever board they are
630/// asking about — so this is a bound on the documents rather than a budget that runs out.
631///
632/// This is `nodeCount`, the maximum number of nodes *one query may return*. It is not
633/// `cost`, the rate-limit points a call spends against an hourly allowance shared by
634/// everything the credential does — two numbers against two limits, and this constant
635/// bounds only the first. The second is computed offline too, per document:
636/// [`worst_case_point_cost`], pinned for every document in [`graphql::DOCUMENTS`] by
637/// `tests/point_cost.rs`, and reconciled against GitHub's own `cost` by the credentialed
638/// lane. There is no constant like this one to hold a price under, because points are an
639/// hourly allowance rather than a per-call bound.
640///
641/// Neither is a session's price. What `session-cost.md` records of a whole session is its
642/// **requests** and its **worst-case nodes**; what a whole session spends in points is
643/// reported only by a credentialed run's own `x-ratelimit-*` headers, through
644/// [`accounting`]. The module section on the three ways this source reaches an item says how
645/// the count is arrived at, and which of the page sizes below decide it.
646pub const NODE_COUNT_LIMIT: u64 = github_graphql_node_count::NODE_LIMIT;
647
648/// Nested connection size for the connections that hang off one item.
649///
650/// It multiplies through every document that reaches an item under a page — the count
651/// rules multiply down a nested path — so it is the constant [`NODE_COUNT_LIMIT`] is most
652/// sensitive to. `tests/node_count.rs` is what holds the pair together: it recomputes
653/// every document under these constants and fails naming any that reaches the limit, so
654/// raising this is caught there rather than by GitHub.
655const NESTED_PAGE_SIZE: u32 = 50;
656/// How many of one issue's board memberships are read when an issue is reached directly.
657///
658/// An issue reached through a search or through its own node id carries its board half in
659/// `Issue.projectItems`, and only the entry for *this* board is read. This connection sits
660/// under a page of issues, so every point of it multiplies through the whole document and
661/// is paid for whether or not any issue is on a second board — which is why it is
662/// deliberately far smaller than [`NESTED_PAGE_SIZE`].
663///
664/// **Three, because what a page misses is now recovered rather than refused**, and the
665/// recovery is what the value is chosen against. An issue whose entry for this board sits
666/// past this page costs one further request — [`graphql::ISSUE_BOARD_ITEMS`], resumed from
667/// that page's own cursor — so the value trades a bound every read pays for a request only
668/// a multi-board issue pays. At one, a deployment whose issues commonly sit on two or more
669/// boards would pay that request *per issue*, which is order N against the one page per
670/// hundred issues a read costs today. At three it is only reached by an issue on four or
671/// more boards at once, which keeps the recovery path exceptional rather than routine for
672/// a plausible deployment.
673const BOARD_ITEMS_PAGE_SIZE: u32 = 3;
674/// How many carriers of one copy origin one page of [`graphql::ORIGIN_LOOKUP`] asks each of
675/// its two connections for.
676///
677/// An origin names one item, so the answer an origin lookup expects is one carrier, and a
678/// second is a duplicate a copy already takes the first of. Both connections are walked to
679/// exhaustion whatever this is, so it decides how many requests an unusual answer costs and
680/// never what the answer is. It is small because every point of it is paid on every lookup,
681/// and a copy makes one lookup per item it has no link for: at three, ten lookups cost fewer
682/// worst-case nodes than the one whole-board read they replaced.
683const ORIGIN_PAGE_SIZE: u32 = 3;
684
685pub use github_graphql_node_count::{NodeCountError, Variables};
686
687/// The largest value this source can bind to each page-size variable its documents name.
688///
689/// Every `first:` in [`graphql`] reads one of these four, and each is capped at the
690/// constant above it wherever a caller's own limit could reach it — `$first` at
691/// [`MAX_PAGE_SIZE`], `$nestedFirst` at `NESTED_PAGE_SIZE`, `$boardItems` at
692/// `BOARD_ITEMS_PAGE_SIZE`, `$originFirst` at `ORIGIN_PAGE_SIZE`. So this is the worst case a caller can drive this source to,
693/// not one configuration of it, which is what makes a bound computed under it a bound on
694/// every read.
695pub fn largest_page_sizes() -> Variables {
696 Variables::from([
697 ("first".to_owned(), MAX_PAGE_SIZE),
698 ("nestedFirst".to_owned(), NESTED_PAGE_SIZE),
699 ("boardItems".to_owned(), BOARD_ITEMS_PAGE_SIZE),
700 ("originFirst".to_owned(), ORIGIN_PAGE_SIZE),
701 ])
702}
703
704/// The most nodes `document` could be asked to return, by GitHub's published rules.
705///
706/// Computed offline from the document's own text under [`largest_page_sizes`] — no
707/// network, no credential and no schema — by
708/// [`github_graphql_node_count::node_count`], which is where the rules themselves live.
709/// A document at or above [`NODE_COUNT_LIMIT`] is one GitHub refuses before executing, so
710/// this is what a check holds every document in [`graphql::DOCUMENTS`] below.
711///
712/// # Errors
713///
714/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
715/// no single operation, or binds a page size this source does not name — each of which is
716/// a defect in the document rather than a number.
717pub fn worst_case_node_count(document: &str) -> Result<u64, NodeCountError> {
718 node_count(document, &largest_page_sizes())
719}
720
721/// The most rate-limit points one call of `document` could spend, by GitHub's published
722/// rules.
723///
724/// Computed offline from the document's own text under [`largest_page_sizes`] — no
725/// network, no credential and no schema — by
726/// [`github_graphql_node_count::point_cost`], which is where the rules themselves live.
727/// This is `cost`, metered **per hour** against the allowance one credential shares across
728/// everything it does; it is not `nodeCount`, which is [`worst_case_node_count`] and is
729/// bounded per query by [`NODE_COUNT_LIMIT`]. There is no per-call ceiling to hold this
730/// under, so what `tests/point_cost.rs` does with it is pin every document in
731/// [`graphql::DOCUMENTS`] at what it costs, and the credentialed lane reconciles those
732/// figures against GitHub's own reported `cost`.
733///
734/// # Errors
735///
736/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
737/// no single operation, or binds a page size this source does not name — each of which is
738/// a defect in the document rather than a number.
739pub fn worst_case_point_cost(document: &str) -> Result<u64, NodeCountError> {
740 github_graphql_node_count::point_cost(document, &largest_page_sizes())
741}
742
743/// The most nodes `document` could be asked to return under `variables`.
744///
745/// [`worst_case_node_count`] is this under [`largest_page_sizes`], and the accounting in
746/// [`accounting`] is this under the bindings one request really sent — one spelling of the
747/// calculation, so a bound checked offline and a cost recorded at run time cannot come to
748/// disagree. The rules themselves live in [`github_graphql_node_count::node_count`].
749///
750/// # Errors
751///
752/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
753/// no single operation, or binds a page size `variables` does not name.
754pub fn node_count(document: &str, variables: &Variables) -> Result<u64, NodeCountError> {
755 github_graphql_node_count::node_count(document, variables)
756}
757
758/// The issue-title prefix that makes a board issue a document.
759///
760/// A GitHub Projects board has no document type — it holds issues — so the discriminator
761/// is the title, and this is the whole of it: an issue whose title begins with these bytes
762/// is a document and every other issue is the task or project the sub-issue rule makes it.
763///
764/// It is spelled **once**, here, and read rather than restated everywhere else — including
765/// by the shared journeys, which take it from this constant so a board fixture cannot
766/// drift from what this source reads. `docs/metadata.md` records the two consequences that
767/// are not obvious from the bytes: the reported title has this prefix taken off, exactly
768/// as the body's metadata slot is taken off `content`, and this prefix is read *before*
769/// the sub-issue rule, so a design issue with no sub-issues is never an empty project.
770pub const DESIGN_TITLE_PREFIX: &str = "DESIGN: ";
771
772/// Exact GraphQL query documents issued by this plugin.
773///
774/// Keeping the production documents here lets the pinned-schema test validate the same
775/// bytes that are sent to GitHub, rather than a test-only copy which could drift
776/// independently. [`STATUS_OPTIONS_UPDATE`] is the sole document that may rewrite a board
777/// field, and its guarded caller always supplies the complete existing option set with ids.
778pub mod graphql {
779 /// The board half of one item: the field values every document here reads it from.
780 ///
781 /// A macro for the same reason [`board_issue!`] below is one, a level further in. This
782 /// selection is needed by that fragment, by [`BOARD`] under the board's own `items`,
783 /// and by [`ISSUE_BOARD_ITEMS`] under a membership walk — and all three have to produce
784 /// *the same value*, because
785 /// [`GitHubProjectsSource::resolve`](super::GitHubProjectsSource) reads them through
786 /// one path. Three spellings of it is what would drift, so there is one.
787 ///
788 /// The `Status` option and this source's own origin text field are the whole of it. It
789 /// selects no `ProjectV2ItemFieldLabelValue`: GitHub derives that field from the item's
790 /// content, so it holds nothing the content's own `labels` do not already say, and it
791 /// would sit a label connection two page sizes deep.
792 macro_rules! board_item_values {
793 () => {
794 r#"fieldValues(first:$nestedFirst){nodes{
795 ... on ProjectV2ItemFieldSingleSelectValue{name field{
796 ... on ProjectV2SingleSelectField{id name options{id name}}
797 }}
798 ... on ProjectV2ItemFieldTextValue{text field{... on ProjectV2Field{id name}}}
799 }pageInfo{hasNextPage}}"#
800 };
801 }
802
803 /// Everything this source reads about one issue, wherever it reaches that issue.
804 ///
805 /// A macro rather than a constant so the three documents below can `concat!` it: one
806 /// spelling of these fields is what makes an issue read through the board-scoped
807 /// search, through its own node id, and through its project's sub-issue relationship
808 /// resolve to *the same* item, which is the whole of what
809 /// [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource) relies on.
810 ///
811 /// `projectItems` is what carries the board half of an issue: the board item's own id
812 /// and the [`board_item_values!`] above — the `Status` option and this source's origin
813 /// text field — that a `ProjectV2.items` read used to carry. It is asked for on the
814 /// issue rather than on the board, which is what makes the cost of a read proportional
815 /// to what was asked for instead of to the board's size.
816 ///
817 /// It carries a *page* of that connection, at `BOARD_ITEMS_PAGE_SIZE`, and its
818 /// `endCursor` is what [`ISSUE_BOARD_ITEMS`] resumes from when this board's entry is
819 /// not on that page: a page here is where the search for the entry starts rather than
820 /// where it ends.
821 ///
822 /// It does **not** select the board's `Labels` field value, and that is the whole of
823 /// what keeps the three documents below under [`NODE_COUNT_LIMIT`](super::NODE_COUNT_LIMIT):
824 /// a label connection there sits under `fieldValues` under `projectItems` under a page
825 /// of issues, spending `$nestedFirst` twice down one path, and took
826 /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] to 2,556,100 nodes against a limit of 500,000.
827 /// No label is lost — this is a fragment `on Issue`, whose own `labels` are selected
828 /// above, and that connection is where every label this source reports comes from. No
829 /// document in this module selects the board field any longer, [`BOARD`] included; the
830 /// module documentation records why nothing it could have held is lost.
831 macro_rules! board_issue {
832 () => {
833 concat!(
834 r#" fragment BoardIssue on Issue{__typename id number title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total}
835 labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}
836 projectItems(first:$boardItems){nodes{id project{id number}
837 "#,
838 board_item_values!(),
839 r#"}pageInfo{hasNextPage endCursor}}}"#
840 )
841 };
842 }
843
844 /// Every issue of one board, found by a search scoped to that board.
845 ///
846 /// This is how the projects a board holds are listed, and it selects no `items`
847 /// connection on `ProjectV2`: the board is a *qualifier of the search* rather than a
848 /// container walked page by page, so nothing nested inside a board item is paid for.
849 /// Which of the issues it returns is a project is then read off `parent` — GitHub
850 /// accepts `-has:parent` as a search qualifier and silently ignores it, so the
851 /// discriminator has to be applied to the field, which is a scalar on the issue and
852 /// costs nothing.
853 pub const SEARCH_ISSUES: &str = concat!(
854 r#"query($search:String!,$type:SearchType!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!){
855 search(query:$search,type:$type,first:$first,after:$after){
856 pageInfo{hasNextPage endCursor}
857 nodes{__typename ...BoardIssue}
858 }
859 }"#,
860 board_issue!()
861 );
862
863 /// What a dependency read selects of each far end: enough to say which kind of item it
864 /// is, its body included for the kind marker.
865 macro_rules! related_issue {
866 () => {
867 " fragment Related on Issue{id title body parent{id} subIssuesSummary{total}}"
868 };
869 }
870
871 /// One issue by its own node id, which is what a qualified id names here — with what a
872 /// write of it needs and the issue does not carry in `board_issue!`: the field
873 /// definitions of the boards it sits on, and the far ends of its `blockedBy`.
874 ///
875 /// Strongly consistent, unlike the search above: GitHub's issue search is an index and
876 /// answers a write made moments ago with the value from before it, and resolving a node
877 /// id does not.
878 ///
879 /// **Why those two ride here and not on the fragment.** A copy or an update of an item
880 /// reads it by its own id, and with them that one read answers everything the write
881 /// needs: which option ids the board's `Status` and `Priority` fields hold — so no
882 /// [`BOARD_FIELDS`] — and which issues block it, with each one's kind — so no
883 /// [`ISSUE_DEPENDENCIES`]. On `board_issue!` they would sit under the hundred-issue
884 /// pages of [`SEARCH_ISSUES`] and [`SUB_ISSUES`], multiplying both documents' price. Here
885 /// they sit under one item, and this read is still one point.
886 pub const ISSUE: &str = concat!(
887 r#"query($id:ID!,$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!){
888 node(id:$id){__typename ...BoardIssue ... on Issue{
889 boards:projectItems(first:$boardItems){nodes{project{id number fields(first:$nestedFirst){nodes{
890 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
891 ... on ProjectV2Field{__typename id name}
892 }pageInfo{hasNextPage}}}}}
893 blockedBy(first:$first){nodes{...Related}pageInfo{hasNextPage endCursor}}
894 }}
895 }"#,
896 board_issue!(),
897 related_issue!()
898 );
899
900 /// One project's tasks: the sub-issues of the issue that project is.
901 ///
902 /// The work this costs is the project's own size. Nothing about it grows as the board
903 /// gains projects, or as those projects gain tasks.
904 pub const SUB_ISSUES: &str = concat!(
905 r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!){
906 node(id:$id){__typename
907 ... on Issue{subIssues(first:$first,after:$after){
908 pageInfo{hasNextPage endCursor}
909 nodes{__typename ...BoardIssue}
910 }}}
911 }"#,
912 board_issue!()
913 );
914
915 /// What a read of the board's own `items` selects of each item's content.
916 ///
917 /// A macro for the reason [`board_item_values!`] is one: [`BOARD`] and [`ORIGIN_LOOKUP`]
918 /// both walk `ProjectV2.items` and hand each item to one resolver, so they select its
919 /// content by one spelling.
920 macro_rules! board_item_content {
921 () => {
922 r#" content{
923 ... 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}}}
924 ... on PullRequest{__typename id}
925 ... on DraftIssue{__typename id title body createdAt updatedAt}
926 }"#
927 };
928 }
929
930 /// Reads the board's fields and one page of its items.
931 pub const BOARD: &str = concat!(
932 r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$nestedFirst:Int!,$duplicates:Boolean!){
933 owner:repositoryOwner(login:$owner){
934 ... on ProjectV2Owner{projectV2(number:$number){...Board}}
935 }
936 } fragment Board on ProjectV2 { id title
937 fields(first:$nestedFirst){nodes{
938 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
939 ... on ProjectV2Field{__typename id name}
940 }pageInfo{hasNextPage}}
941 items(first:$first,after:$after){nodes{id "#,
942 board_item_values!(),
943 board_item_content!(),
944 r#"} pageInfo{hasNextPage endCursor}}
945 }"#
946 );
947
948 /// Every carrier of one copy origin, by two reads in one request, and nothing else of
949 /// the board.
950 ///
951 /// **`originItems`** is the board's own items narrowed by its own field filter —
952 /// `ProjectV2.items(query:)`, which GitHub's schema declares as "Search query for
953 /// filtering items" — to those whose `onetaskgraph.origin` text field holds the
954 /// qualified id, quoted. It reads the field every carrier already holds, whichever release
955 /// wrote it, and matches it exactly: measured on 2026-09-29 against a 394-item board,
956 /// the quoted, the unquoted and the bare-value spellings each returned exactly the one
957 /// carrier and a prefix of the value returned none. It is `ProjectV2.items`, so it lags a
958 /// fresh `addProjectV2ItemById` the way that connection does.
959 ///
960 /// **`search`** is the board-scoped issue search for the same id as a quoted phrase in
961 /// the body, which is where this source mirrors the origin into its metadata slot. GitHub
962 /// indexes that comment, and the index catches up with a write in a second or two rather
963 /// than in minutes, so it finds a carrier another process wrote that the first read is
964 /// still behind on.
965 ///
966 /// Each connection pages at `$originFirst`, its own small size — see `ORIGIN_PAGE_SIZE`
967 /// — and resumes from its own cursor; a connection already walked to its end is resumed
968 /// from its last cursor, which answers an empty page. Every candidate either read returns
969 /// is confirmed against its own origin field before it is reported, so a token match of
970 /// the search or anything else the filter admits never is.
971 ///
972 /// The root is aliased `originItems` rather than `owner`, so nothing counting the board's
973 /// own whole reads counts this one among them.
974 pub const ORIGIN_LOOKUP: &str = concat!(
975 r#"query($owner:String!,$number:Int!,$filter:String!,$search:String!,$type:SearchType!,$originFirst:Int!,$itemsAfter:String,$searchAfter:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!){
976 originItems:repositoryOwner(login:$owner){
977 ... on ProjectV2Owner{projectV2(number:$number){
978 items(first:$originFirst,after:$itemsAfter,query:$filter){nodes{id "#,
979 board_item_values!(),
980 board_item_content!(),
981 r#"} pageInfo{hasNextPage endCursor}}
982 }}
983 }
984 search(query:$search,type:$type,first:$originFirst,after:$searchAfter){
985 pageInfo{hasNextPage endCursor}
986 nodes{__typename ...BoardIssue}
987 }
988 }"#,
989 board_issue!()
990 );
991
992 /// The board's own id and field definitions, and not one of its items.
993 ///
994 /// What a write needs of the board when the item it writes does not say: the id a field
995 /// write and `addProjectV2ItemById` address, and the definitions of the `Status` and
996 /// origin fields. It selects no `items`, so what it costs is the board's field list
997 /// however many items the board holds — and it decides nothing about which items those
998 /// are, which is the question a read of one item by its own id answers instead.
999 ///
1000 /// The root is aliased `boardFields` rather than `owner`, so nothing counting the
1001 /// board's item reads by their root counts this one among them.
1002 pub const BOARD_FIELDS: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!){
1003 boardFields:repositoryOwner(login:$owner){
1004 ... on ProjectV2Owner{projectV2(number:$number){id
1005 fields(first:$nestedFirst){nodes{
1006 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1007 ... on ProjectV2Field{__typename id name}
1008 }pageInfo{hasNextPage}}
1009 }}
1010 }
1011 }"#;
1012
1013 /// One board draft by its own node id, with the board item it sits in.
1014 ///
1015 /// A draft is not an issue, so [`ISSUE`] reaches it and reads nothing of it; this is the
1016 /// second read that answers it. `DraftIssue.projectV2Items` names the board item a draft
1017 /// is — GitHub links a draft to one item — with the same [`board_item_values!`] the
1018 /// issue fragment reads, so a draft reached by id resolves through the same resolver a
1019 /// board listing hands it to, and nothing has to list the board to find one.
1020 pub const DRAFT: &str = concat!(
1021 r#"query($id:ID!,$nestedFirst:Int!,$boardItems:Int!){
1022 node(id:$id){__typename ... on DraftIssue{id title body createdAt updatedAt
1023 projectV2Items(first:$boardItems){nodes{id project{id number}
1024 "#,
1025 board_item_values!(),
1026 r#"}pageInfo{hasNextPage endCursor}}}}
1027 }"#
1028 );
1029
1030 /// One issue's board memberships alone, walked past the page a read of it carried.
1031 ///
1032 /// The recovery read behind [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource):
1033 /// every document above carries a *page* of `Issue.projectItems`, and an issue on more
1034 /// boards than that page holds may have this board's entry past its end. This asks that
1035 /// one issue for its memberships and nothing else — the caller already holds the issue —
1036 /// so an answer of "this board does not hold it" is only ever given about a connection
1037 /// read to exhaustion.
1038 ///
1039 /// It selects the board item's id, its project number and the same
1040 /// [`board_item_values!`] the fragment does, because what it produces is handed to the
1041 /// very same resolver: an issue recovered this way reports the same title, the same
1042 /// status, the same labels and the same qualified id as one whose entry was on the
1043 /// page.
1044 ///
1045 /// `$first` rather than `$boardItems`: this document reads one issue, so nothing
1046 /// multiplies through it and the membership connection can be walked at
1047 /// [`MAX_PAGE_SIZE`](super::MAX_PAGE_SIZE) — which is what keeps the recovery to one
1048 /// further request for any issue a person really keeps.
1049 pub const ISSUE_BOARD_ITEMS: &str = concat!(
1050 r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!){
1051 node(id:$id){
1052 ... on Issue{projectItems(first:$first,after:$after){
1053 nodes{id project{id number}
1054 "#,
1055 board_item_values!(),
1056 r#"}
1057 pageInfo{hasNextPage endCursor}}}
1058 }
1059 }"#
1060 );
1061 /// Resolves the configured repository's node id, which creating an issue requires.
1062 pub const REPOSITORY: &str = r#"query($owner:String!,$name:String!){repository(owner:$owner,name:$name){id nameWithOwner}}"#;
1063 /// What creating an issue needs and has not read yet: the board's own id and field
1064 /// definitions, as [`BOARD_FIELDS`] reads them, and the node id of the repository the
1065 /// issue is created in, as [`REPOSITORY`] reads it — in one request.
1066 ///
1067 /// Sent at the point a create knows which repository it is for, when neither half is
1068 /// already known to this process; a create needing only one of them sends that one's own
1069 /// document. Neither half is kept past the process: a field's option ids are re-minted by
1070 /// `sources fields --apply`, so a copy of them held between runs would write the wrong
1071 /// status.
1072 pub const CREATION_CONTEXT: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!,$repositoryOwner:String!,$repositoryName:String!){
1073 boardFields:repositoryOwner(login:$owner){
1074 ... on ProjectV2Owner{projectV2(number:$number){id
1075 fields(first:$nestedFirst){nodes{
1076 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1077 ... on ProjectV2Field{__typename id name}
1078 }pageInfo{hasNextPage}}
1079 }}
1080 }
1081 repository(owner:$repositoryOwner,name:$repositoryName){id nameWithOwner}
1082 }"#;
1083 /// Reads both dependency directions for one issue, with each far end's own kind — and
1084 /// the issue's own body, which is where an edge to another source is recorded, so that
1085 /// half of a dependency read needs no second read of the issue or of the board.
1086 pub const ISSUE_DEPENDENCIES: &str = concat!(
1087 r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
1088 ... on Issue{body
1089 blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1090 blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1091 }}}"#,
1092 related_issue!()
1093 );
1094 /// Creates one issue in the configured repository, on no board: [`ADD_TO_BOARD`] files
1095 /// it. `CreateIssueInput.projectV2Ids` is not sent — see the crate's notes on what GitHub
1096 /// answered when it was.
1097 pub const CREATE_ISSUE: &str =
1098 r#"mutation($input:CreateIssueInput!){createIssue(input:$input){issue{id number url}}}"#;
1099 /// Puts an existing issue on the configured board.
1100 pub const ADD_TO_BOARD: &str = r#"mutation($input:AddProjectV2ItemByIdInput!){addProjectV2ItemById(input:$input){item{id}}}"#;
1101 /// Updates an issue's visible fields and its open or closed state in one call.
1102 pub const UPDATE_ISSUE: &str =
1103 r#"mutation($input:UpdateIssueInput!){updateIssue(input:$input){issue{id}}}"#;
1104 /// Updates an existing draft's user-visible fields.
1105 pub const UPDATE_DRAFT: &str = r#"mutation($input:UpdateProjectV2DraftIssueInput!){updateProjectV2DraftIssue(input:$input){draftIssue{id}}}"#;
1106 /// Updates a text or single-select value on one project item.
1107 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}}}}}}}}"#;
1108 /// Writes up to three board fields and an optional clear in one ordered mutation.
1109 pub const UPDATE_FIELDS: &str = r#"mutation($input:UpdateProjectV2ItemFieldValueInput!,$second:UpdateProjectV2ItemFieldValueInput!,$third:UpdateProjectV2ItemFieldValueInput!,$clear:ClearProjectV2ItemFieldValueInput!,$writeSecond:Boolean!,$writeThird:Boolean!,$writeClear:Boolean!){updateProjectV2ItemFieldValue(input:$input){projectV2Item{id}} second:updateProjectV2ItemFieldValue(input:$second) @include(if:$writeSecond){projectV2Item{id}} third:updateProjectV2ItemFieldValue(input:$third) @include(if:$writeThird){projectV2Item{id}} cleared:clearProjectV2ItemFieldValue(input:$clear) @include(if:$writeClear){projectV2Item{id}}}"#;
1110 /// Clears one project item's value of one field, which is what a `none` priority is.
1111 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}}}}}}}}"#;
1112 /// Creates one single-select field with its options. Only the guarded field setup may use
1113 /// this document, and only for a field the board lacks.
1114 pub const CREATE_FIELD: &str = r#"mutation($input:CreateProjectV2FieldInput!){createProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id name options{id name color description}}}}}"#;
1115 /// Replaces a single-select field's options. Only the guarded field setup — the
1116 /// `status-options` and `fields` operations — may use this document, because GitHub
1117 /// treats the input as the complete option list.
1118 pub const STATUS_OPTIONS_UPDATE: &str = r#"mutation($input:UpdateProjectV2FieldInput!){updateProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id options{id name color description}}}}}"#;
1119 /// A fresh snapshot of the Status field and every board item's assignment.
1120 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}}}}}}"#;
1121 /// Files one issue under another as a sub-issue, which is what project membership is.
1122 pub const ADD_SUB_ISSUE: &str =
1123 r#"mutation($input:AddSubIssueInput!){addSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1124 /// Takes one issue back out of its parent.
1125 pub const REMOVE_SUB_ISSUE: &str = r#"mutation($input:RemoveSubIssueInput!){removeSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1126 /// Adds GitHub's native issue blocked-by relationship.
1127 pub const ADD_BLOCKED_BY: &str = r#"mutation($input:AddBlockedByInput!){addBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1128 /// Removes one native issue blocked-by relationship.
1129 pub const REMOVE_BLOCKED_BY: &str = r#"mutation($input:RemoveBlockedByInput!){removeBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1130 /// Deletes one issue, which takes its board item with it.
1131 ///
1132 /// The engine sends this in one situation only: undoing a copy that could not finish,
1133 /// over the items that same copy created. Deleting the issue removes the board item
1134 /// too, so there is no second `deleteProjectV2Item` to keep in step with it.
1135 pub const DELETE_ISSUE: &str =
1136 r#"mutation($input:DeleteIssueInput!){deleteIssue(input:$input){repository{id}}}"#;
1137
1138 /// Everything this source reads about one issue comment, wherever it reaches one.
1139 ///
1140 /// A macro for the reason [`board_issue!`] is one: a comment listed, a comment just added
1141 /// and a comment just edited are handed to one mapper, so they are selected by one
1142 /// spelling. `author` is `Actor`, which GitHub answers `null` for an account that no
1143 /// longer exists, and `login` is the one member every kind of actor carries.
1144 macro_rules! issue_comment {
1145 () => {
1146 "id author{login} createdAt updatedAt body url"
1147 };
1148 }
1149
1150 /// One task's comments: a page of its issue's own `comments` connection.
1151 ///
1152 /// **No `orderBy`, and that is what makes the page oldest first.** GitHub's only
1153 /// `IssueCommentOrder` field is `UPDATED_AT`, which would move a comment to the end of the
1154 /// list every time somebody edited it; left unordered the connection answers in the order
1155 /// the comments were written, which is the order GitHub documents for the same collection
1156 /// over REST — ascending id. Nothing multiplies through it, so `$first` is the whole of its
1157 /// node count and the caller's own page size is pushed straight down.
1158 pub const ISSUE_COMMENTS: &str = concat!(
1159 r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename ... on Issue{comments(first:$first,after:$after){nodes{"#,
1160 issue_comment!(),
1161 r#"}pageInfo{hasNextPage endCursor}}}}}"#
1162 );
1163 /// One issue by its own node id, with a page of its comments: what `task show` and a
1164 /// comment listing read, in one request.
1165 ///
1166 /// [`ISSUE`] and [`ISSUE_COMMENTS`] in one document, rather than one then the other. The
1167 /// comments are selected here and **not** on the shared `board_issue!` fragment, which
1168 /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] nest under a page of a hundred issues: a comment
1169 /// connection there would multiply through both of those documents' price, and neither
1170 /// needs one.
1171 pub const ISSUE_DETAIL: &str = concat!(
1172 r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!){
1173 node(id:$id){__typename ...BoardIssue ... on Issue{comments(first:$first,after:$after){nodes{"#,
1174 issue_comment!(),
1175 r#"}pageInfo{hasNextPage endCursor}}}}
1176 }"#,
1177 board_issue!()
1178 );
1179
1180 /// One alias of [`ISSUE_DETAILS`]: the item a batch's `$id<n>` names, with the first
1181 /// page of its comments when `$comments` asks for them.
1182 macro_rules! issue_details_alias {
1183 ($n:literal) => {
1184 concat!(
1185 "\n i",
1186 stringify!($n),
1187 ":node(id:$id",
1188 stringify!($n),
1189 "){__typename ...BoardIssue ... on Issue{comments(first:$first) @include(if:$comments){nodes{",
1190 issue_comment!(),
1191 "}pageInfo{hasNextPage endCursor}}}}"
1192 )
1193 };
1194 }
1195
1196 /// [`ISSUE_DETAIL`] for [`DETAIL_BATCH`](super::DETAIL_BATCH) items at once, each by its
1197 /// own node id, as one fixed-size document of aliased `node(id:)` fields.
1198 ///
1199 /// **Aliased `node(id:)` rather than `nodes(ids:)`, and that is what keeps its price
1200 /// honest.** The `github-graphql-node-count` model this workspace prices with treats a
1201 /// field that supplies neither `first` nor `last` as free, and `nodes(ids:)` supplies
1202 /// neither — so every connection under it would be priced at nothing and the pin in
1203 /// `tests/point_cost.rs` would understate what GitHub charges. Each alias here is the
1204 /// one-item read the model already prices, so the batch costs what its aliases cost.
1205 ///
1206 /// **Fixed-size, so there is one document to price.** A batch of fewer items binds the
1207 /// slots it has no item for to the last item it does, and reads that item again; the
1208 /// price is the document's, whatever its variables, so a short batch costs what a full
1209 /// one does and nothing more.
1210 pub const ISSUE_DETAILS: &str = concat!(
1211 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!){"#,
1212 issue_details_alias!(0),
1213 issue_details_alias!(1),
1214 issue_details_alias!(2),
1215 issue_details_alias!(3),
1216 issue_details_alias!(4),
1217 issue_details_alias!(5),
1218 issue_details_alias!(6),
1219 issue_details_alias!(7),
1220 issue_details_alias!(8),
1221 issue_details_alias!(9),
1222 issue_details_alias!(10),
1223 issue_details_alias!(11),
1224 issue_details_alias!(12),
1225 issue_details_alias!(13),
1226 issue_details_alias!(14),
1227 issue_details_alias!(15),
1228 issue_details_alias!(16),
1229 issue_details_alias!(17),
1230 issue_details_alias!(18),
1231 issue_details_alias!(19),
1232 issue_details_alias!(20),
1233 issue_details_alias!(21),
1234 issue_details_alias!(22),
1235 issue_details_alias!(23),
1236 "\n }",
1237 board_issue!()
1238 );
1239
1240 /// Which issue one comment is on, read before that comment is edited or removed.
1241 ///
1242 /// GitHub's comment mutations take the comment's id and nothing else, so without this a
1243 /// comment id given against the wrong task would change a comment on another issue.
1244 pub const COMMENT_ISSUE: &str =
1245 r#"query($id:ID!){node(id:$id){__typename ... on IssueComment{id issue{id}}}}"#;
1246 /// Adds one comment to an issue, signed as the account the token belongs to.
1247 pub const ADD_COMMENT: &str = concat!(
1248 r#"mutation($input:AddCommentInput!){addComment(input:$input){subject{id} commentEdge{node{"#,
1249 issue_comment!(),
1250 r#"}}}}"#
1251 );
1252 /// Replaces the body of one issue comment.
1253 pub const UPDATE_COMMENT: &str = concat!(
1254 r#"mutation($input:UpdateIssueCommentInput!){updateIssueComment(input:$input){issueComment{"#,
1255 issue_comment!(),
1256 r#"}}}"#
1257 );
1258 /// Removes one issue comment. Its payload carries nothing about the comment it removed.
1259 pub const DELETE_COMMENT: &str = r#"mutation($input:DeleteIssueCommentInput!){deleteIssueComment(input:$input){clientMutationId}}"#;
1260
1261 /// Every document above, with what this source is doing when it sends one.
1262 ///
1263 /// One list rather than a `match` beside the constants: a rate-limit diagnostic has to
1264 /// name the call that was refused, and a `match` with a catch-all arm would answer a
1265 /// document added later with "talking to GitHub" and never say so.
1266 ///
1267 /// `documents_are_all_inventoried` reads this file back and fails naming any `pub
1268 /// const` here that this list omits, so the two cannot part — which is the same guard
1269 /// `CATEGORIES` carries, in the one shape available to a set of `&str` constants.
1270 pub const DOCUMENTS: [(&str, &str); 33] = [
1271 (SEARCH_ISSUES, "searching this board's issues"),
1272 (ISSUE, "reading one issue"),
1273 (
1274 ISSUE_BOARD_ITEMS,
1275 "reading one issue's board memberships past the page it came with",
1276 ),
1277 (SUB_ISSUES, "reading a project's tasks"),
1278 (BOARD, "reading the board"),
1279 (ORIGIN_LOOKUP, "looking up the items copied from one origin"),
1280 (BOARD_FIELDS, "reading the board's fields"),
1281 (DRAFT, "reading one draft"),
1282 (REPOSITORY, "reading the destination repository"),
1283 (
1284 CREATION_CONTEXT,
1285 "reading the board's fields and the destination repository",
1286 ),
1287 (ISSUE_DEPENDENCIES, "reading an issue's dependencies"),
1288 (CREATE_ISSUE, "creating an issue"),
1289 (ADD_TO_BOARD, "adding an issue to the board"),
1290 (UPDATE_ISSUE, "updating an issue"),
1291 (UPDATE_DRAFT, "updating a draft item"),
1292 (UPDATE_FIELD, "writing a board field"),
1293 (UPDATE_FIELDS, "writing board fields together"),
1294 (CLEAR_FIELD, "clearing a board field"),
1295 (
1296 CREATE_FIELD,
1297 "creating a board single-select field with its options",
1298 ),
1299 (
1300 STATUS_OPTIONS_SNAPSHOT,
1301 "snapshotting board Status options and assignments",
1302 ),
1303 (
1304 STATUS_OPTIONS_UPDATE,
1305 "safely replacing the board Status option list",
1306 ),
1307 (ADD_SUB_ISSUE, "filing an issue under its project"),
1308 (REMOVE_SUB_ISSUE, "taking an issue out of its project"),
1309 (ADD_BLOCKED_BY, "recording a dependency"),
1310 (REMOVE_BLOCKED_BY, "removing a dependency"),
1311 (DELETE_ISSUE, "deleting an issue"),
1312 (ISSUE_COMMENTS, "reading a task's comments"),
1313 (ISSUE_DETAIL, "reading one issue with its comments"),
1314 (
1315 ISSUE_DETAILS,
1316 "reading a batch of issues with their comments",
1317 ),
1318 (COMMENT_ISSUE, "reading which issue a comment is on"),
1319 (ADD_COMMENT, "adding a comment"),
1320 (UPDATE_COMMENT, "editing a comment"),
1321 (DELETE_COMMENT, "deleting a comment"),
1322 ];
1323}
1324
1325/// Which of GitHub's two rate limiters refused a request.
1326///
1327/// Waiting is the whole answer to the primary budget, and polling is what *extends* the
1328/// secondary one — so an operator told the wrong one takes the wrong next step, which is
1329/// the whole reason this is carried rather than collapsed into "rate limited".
1330#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1331enum Limiter {
1332 /// The hourly API budget, which `gh api rate_limit` reports and a wait answers.
1333 Primary,
1334 /// The burst limiter over content-generating requests, which nothing reports.
1335 Secondary,
1336}
1337
1338/// The wordings GitHub answers a secondary rate limit with.
1339///
1340/// It sends them under a forbidden status, under a too-many-requests status, and inside
1341/// the `errors` of a *successful* response, which is why the text is what this matches on
1342/// rather than the status. `abuse detection` is the wording GitHub used before the
1343/// limiter was renamed and still returns from some endpoints; `submitted too quickly` is
1344/// what a burst of content creation is refused with.
1345///
1346/// This is GitHub's vocabulary rather than this source's, so it is pinned rather than
1347/// remembered: `tests/fixtures/rate-limits.json` records where each wording was read and
1348/// when, and the drift gate reconciles the two lists both ways. Public for that gate
1349/// alone — a caller has no use for it, and matching on a refusal is this source's job.
1350pub const SECONDARY_WORDINGS: [&str; 5] = [
1351 "secondary rate limit",
1352 "temporarily blocked from content creation",
1353 "abuse detection",
1354 "submitted too quickly",
1355 "exceeded a secondary",
1356];
1357
1358/// The wordings GitHub answers an exhausted primary budget with.
1359///
1360/// `rate_limited` is the `type` its GraphQL error carries, which is read as a field rather
1361/// than looked for in the response text. `api rate limit already exceeded` is what GraphQL
1362/// answers a request made once the hour's budget is spent — "API rate limit already exceeded
1363/// for user ID …" in the `errors` of an HTTP 200, with no `type` — and neither of the other
1364/// two phrases is a substring of it, so without it that answer read as a refusal that will
1365/// never lift. Pinned and gated exactly as [`SECONDARY_WORDINGS`] is, and public for the same
1366/// one reason.
1367pub const PRIMARY_WORDINGS: [&str; 4] = [
1368 "api rate limit exceeded",
1369 "api rate limit already exceeded",
1370 "rate limit exceeded",
1371 "rate_limited",
1372];
1373
1374/// What a response *says about itself*, which is the only place a refusal can be read.
1375///
1376/// Deliberately not the whole response body. A board is a place people write about their
1377/// own work, and a task on it titled "the secondary rate limit" would, matched across the
1378/// raw text, turn a perfectly good answer into a refusal this source then waited out and
1379/// reported. So the item data is never read: what is read is GitHub's own REST-style
1380/// `message` envelope, which is what a forbidden status carries, and the `message` and
1381/// `type` of each GraphQL error, which is where a *successful* response says it.
1382///
1383/// A body that is not JSON at all has nothing structured to read, so only a failing
1384/// response's own text is taken — a successful response that is not JSON is malformed
1385/// rather than refused, and [`GitHubProjectsSource::answer`] says so.
1386fn refusal_wording(status: StatusCode, body: &str) -> String {
1387 let Ok(parsed) = serde_json::from_str::<Value>(body) else {
1388 return if status.is_success() {
1389 String::new()
1390 } else {
1391 body.to_owned()
1392 };
1393 };
1394 let mut said: Vec<&str> = parsed
1395 .get("message")
1396 .and_then(Value::as_str)
1397 .into_iter()
1398 .collect();
1399 if let Some(errors) = parsed.get("errors").and_then(Value::as_array) {
1400 for error in errors {
1401 said.extend(
1402 ["message", "type"]
1403 .into_iter()
1404 .filter_map(|key| error.get(key).and_then(Value::as_str)),
1405 );
1406 }
1407 }
1408 said.join("; ")
1409}
1410
1411impl Limiter {
1412 /// Which limiter refused this response, or `None` when none of them did.
1413 ///
1414 /// The wording is read first and the status only decides what carries none of it,
1415 /// because GitHub answers a secondary limit with a forbidden status far more often
1416 /// than with too-many-requests — while a forbidden status saying nothing about a limit
1417 /// really is a credential this token lacks.
1418 ///
1419 /// A response is a refusal because of its status or its own wording. A spent budget
1420 /// only ever explains one; it never turns an answer into a refusal.
1421 fn classify(status: StatusCode, budget_exhausted: bool, body: &str) -> Option<Self> {
1422 let normalized = refusal_wording(status, body).to_ascii_lowercase();
1423 if SECONDARY_WORDINGS
1424 .iter()
1425 .any(|wording| normalized.contains(wording))
1426 {
1427 return Some(Self::Secondary);
1428 }
1429 if status == StatusCode::TOO_MANY_REQUESTS {
1430 return Some(Self::Primary);
1431 }
1432 // An exhausted budget *explains* a response that failed; it does not make one that
1433 // succeeded into a failure. GitHub sets `x-ratelimit-remaining: 0` on the last
1434 // request the budget allowed as well as on the ones it then refuses, so reading
1435 // the header alone threw away a good answer — and, once refusals were retried,
1436 // replayed a request that had already taken effect.
1437 if !status.is_success() && budget_exhausted {
1438 return Some(Self::Primary);
1439 }
1440 // A successful response saying it: GitHub reports a GraphQL rate limit in the
1441 // `errors` of an HTTP 200, where nothing about the status says so at all.
1442 if status.is_success()
1443 && PRIMARY_WORDINGS
1444 .iter()
1445 .any(|wording| normalized.contains(wording))
1446 {
1447 return Some(Self::Primary);
1448 }
1449 None
1450 }
1451
1452 /// What this limiter is called where an operator can look it up.
1453 const fn name(self) -> &'static str {
1454 match self {
1455 Self::Primary => "GitHub's primary API rate limit",
1456 Self::Secondary => "GitHub's secondary rate limit",
1457 }
1458 }
1459
1460 /// What the endpoint an operator would go and check says about this limiter.
1461 const fn where_to_look(self) -> &'static str {
1462 match self {
1463 Self::Primary => {
1464 "That is the budget `gh api rate_limit` reports, so that endpoint says when it \
1465 comes back."
1466 }
1467 Self::Secondary => {
1468 "That limiter is not the primary API budget: `gh api rate_limit` reports the \
1469 primary budget and does not report this one, so budget showing there says \
1470 nothing about this refusal, and every further attempt extends it."
1471 }
1472 }
1473 }
1474
1475 /// The next step this limiter actually calls for.
1476 const fn what_to_do(self) -> &'static str {
1477 match self {
1478 Self::Primary => {
1479 "wait for the reset `gh api rate_limit` reports, then run the command again."
1480 }
1481 Self::Secondary => {
1482 "leave this board alone for a few minutes, then run the command again — or \
1483 raise pacing.min_mutation_interval_ms on this source so it writes more slowly."
1484 }
1485 }
1486 }
1487}
1488
1489/// One rate-limit refusal, and the wait GitHub asked for if it asked for one.
1490#[derive(Debug, Clone, Copy)]
1491struct Limited {
1492 limiter: Limiter,
1493 hint: Option<u64>,
1494}
1495
1496impl Limited {
1497 /// What the caller is told once this source has waited as long as it may.
1498 ///
1499 /// Both limiters report as [`SourceError::RateLimited`], because that is what
1500 /// happened: the kind a caller matches on says a rate limit refused this, and nothing
1501 /// about *which* limiter it was makes it a different kind of failure. What differs is
1502 /// the operator's next step, and that is what the message carries — a secondary
1503 /// refusal read as a primary one sends an operator to `gh api rate_limit`, where the
1504 /// budget looks fine, and then back to retry the very burst that was refused.
1505 fn exhausted(
1506 self,
1507 doing: &str,
1508 waits: u32,
1509 waited: Duration,
1510 needed: Duration,
1511 budget: Duration,
1512 ) -> SourceError {
1513 SourceError::RateLimited {
1514 retry_after_seconds: self.hint,
1515 message: Some(format!(
1516 "{} refused this source while {doing}; it waited {} out over {} and was refused \
1517 again, and the next wait of {} would take it past the {} one call may spend \
1518 waiting. {} next: {}",
1519 self.limiter.name(),
1520 plural(waits, "refusal"),
1521 seconds(waited),
1522 seconds(needed),
1523 seconds(budget),
1524 self.limiter.where_to_look(),
1525 self.limiter.what_to_do(),
1526 )),
1527 }
1528 }
1529}
1530
1531/// One HTTP attempt's result, with what its response said about the rate limit.
1532///
1533/// The two travel together so the record and the outcome are written from the same place:
1534/// what a response said about the budget is only readable while that response is in hand,
1535/// and what the attempt *meant* is only decidable once its body has been read.
1536struct Attempted {
1537 result: Result<Value, Attempt>,
1538 limits: accounting::RateLimit,
1539 /// GitHub's own reported cost for this call, for a document that asked for it.
1540 reported_cost: Option<u64>,
1541}
1542
1543/// One attempt's outcome: an error to report, or a rate limit to wait out.
1544enum Attempt {
1545 Failed(SourceError),
1546 Limited(Limited),
1547}
1548
1549fn plural(count: u32, thing: &str) -> String {
1550 if count == 1 {
1551 format!("{count} {thing}")
1552 } else {
1553 format!("{count} {thing}s")
1554 }
1555}
1556
1557fn seconds(duration: Duration) -> String {
1558 format!("{:.1}s", duration.as_secs_f64())
1559}
1560
1561/// A header GitHub spells as a whole number of seconds, or `None` when this one is not.
1562///
1563/// A value that is present and unreadable is deliberately *not* an error. `retry-after` is
1564/// allowed by HTTP to be a date rather than a count, an intermediary can rewrite either
1565/// header, and neither is what makes a response a refusal — so the whole cost of one this
1566/// cannot read is that the refusal carries no hint and the backing-off schedule answers it
1567/// instead. Refusing the response over the header would turn a readable refusal into an
1568/// unreadable one, and refusing to *wait* would be the one wrong direction to fail in.
1569fn whole_seconds(value: Option<&reqwest::header::HeaderValue>) -> Option<u64> {
1570 value
1571 .and_then(|value| value.to_str().ok())
1572 .and_then(|value| value.trim().parse::<u64>().ok())
1573}
1574
1575/// Every mutation this source sends creates content — an issue, a board item, a field of
1576/// one, a sub-issue link, a dependency, a comment — or edits or removes content of that
1577/// kind, and no query in [`graphql::DOCUMENTS`] does, so what the secondary limiter counts
1578/// and what the keyword says are the same set. That is what makes the keyword a sound test
1579/// rather than a convenient one: pacing an edit or a removal the limiter might not have
1580/// counted costs a wait, and not pacing one it did count costs the next fifty minutes.
1581fn is_mutation(query: &str) -> bool {
1582 query.trim_start().starts_with("mutation")
1583}
1584
1585/// What this source was doing, for a diagnostic that has to say so.
1586///
1587/// Read out of [`graphql::DOCUMENTS`], which is the inventory rather than a copy of it, so
1588/// a document added without a description is caught by that list's own gate instead of
1589/// falling through to the vague arm below.
1590fn operation_description(query: &str) -> &'static str {
1591 graphql::DOCUMENTS
1592 .iter()
1593 .find(|(document, _)| *document == query)
1594 .map_or("talking to GitHub", |(_, doing)| *doing)
1595}
1596
1597/// GitHub's published ceiling on content-generating requests, per minute.
1598///
1599/// Pinned in `tests/fixtures/rate-limits.json` and gated against it, because it is
1600/// GitHub's number rather than this source's: [`MIN_MUTATION_INTERVAL_MS`] is *derived*
1601/// from it, so a pacing value checked only against itself cannot go stale here.
1602pub const CONTENT_CREATION_PER_MINUTE: u64 = 80;
1603/// The same ceiling as GitHub publishes it per hour, which this source does **not** pace
1604/// at. See [`MIN_MUTATION_INTERVAL_MS`] for why the per-minute bound is the one that
1605/// governs; it is pinned beside its sibling so the gate would notice either one moving.
1606pub const CONTENT_CREATION_PER_HOUR: u64 = 500;
1607/// Shortest interval between two content-creating mutations, in milliseconds.
1608///
1609/// GitHub documents two secondary limits on content-generating requests:
1610/// [`CONTENT_CREATION_PER_MINUTE`] and [`CONTENT_CREATION_PER_HOUR`]. 60000/80 is 750, so
1611/// a mutation every 750 ms is the fastest rate that cannot exceed the per-minute bound,
1612/// and that is the bound a copy actually trips: a copy of one plan-sized project is a
1613/// burst of a few dozen mutations inside a few seconds. The hourly bound works out at one
1614/// every 7.2 seconds sustained, which no single copy reaches and which, used as the
1615/// spacing here, would turn an ordinary copy into an hour of waiting — so it is
1616/// deliberately *not* what this paces at. An installation that wants the hourly bound
1617/// honoured for a long sequence of copies says so through
1618/// `pacing.min_mutation_interval_ms`.
1619pub const MIN_MUTATION_INTERVAL_MS: u64 = 60_000 / CONTENT_CREATION_PER_MINUTE;
1620/// First wait when a rate-limit refusal carries no hint; each further wait doubles it.
1621///
1622/// A doubling schedule from one second reaches a minute in six waits, which is GitHub's
1623/// own advice for a secondary limit — wait, and wait longer each time — without spending
1624/// the first minute of a transient refusal doing nothing.
1625pub const RETRY_BACKOFF_MS: u64 = 1_000;
1626/// Total time one call may spend waiting out rate limits before it reports a failure.
1627///
1628/// Two minutes is long enough to ride out the refusals a paced copy still collects and
1629/// short enough that a command an operator is watching returns. The bound is what makes
1630/// the wait a wait rather than a hang: a call refused past it ends in a diagnostic naming
1631/// the limiter, not in a process nobody can tell from a wedged one.
1632pub const RETRY_BUDGET_MS: u64 = 120_000;
1633
1634fn default_token_env() -> String {
1635 "GH_PROJECTS_TOKEN".to_owned()
1636}
1637fn default_endpoint() -> String {
1638 "https://api.github.com/graphql".to_owned()
1639}
1640
1641/// The name of a `Status` single-select option on the board.
1642///
1643/// Validated on the way in rather than checked later, so a blank option name — which
1644/// nothing on a board can be — is a state this type cannot hold.
1645#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
1646#[serde(try_from = "String")]
1647#[schemars(extend("minLength" = 1))]
1648pub struct ColumnName(String);
1649
1650impl ColumnName {
1651 /// The option name, as the board spells it.
1652 fn as_str(&self) -> &str {
1653 &self.0
1654 }
1655}
1656
1657impl TryFrom<String> for ColumnName {
1658 type Error = String;
1659
1660 fn try_from(name: String) -> Result<Self, Self::Error> {
1661 if name.trim().is_empty() {
1662 return Err("a status_mapping option name cannot be blank".to_owned());
1663 }
1664 Ok(Self(name))
1665 }
1666}
1667
1668/// The two closed states this product can mean.
1669///
1670/// GitHub's `IssueClosedStateReason` also spells `DUPLICATE`, which is neither finished
1671/// work nor abandoned work, so nothing here ever writes it.
1672#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, schemars::JsonSchema)]
1673#[serde(rename_all = "kebab-case")]
1674pub enum ClosedState {
1675 /// `COMPLETED` — precisely done.
1676 Completed,
1677 /// `NOT_PLANNED` — precisely cancelled.
1678 NotPlanned,
1679}
1680
1681impl ClosedState {
1682 const fn reason(self) -> &'static str {
1683 match self {
1684 Self::Completed => "COMPLETED",
1685 Self::NotPlanned => "NOT_PLANNED",
1686 }
1687 }
1688}
1689
1690/// Configuration for one GitHub Projects v2 board.
1691#[derive(Debug, Clone, Default, Deserialize, schemars::JsonSchema)]
1692#[serde(default, deny_unknown_fields)]
1693pub struct GitHubProjectsConfig {
1694 /// Login of the user or organization which owns the board.
1695 pub owner: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates GitHub's owner grammar before private construction.
1696 /// The project number shown in the board's GitHub URL.
1697 pub project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` bounds this to a positive GraphQL Int.
1698 // 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.
1699 /// `owner/name` of the repository this source creates an issue in when the item's own
1700 /// `repositories` field does not decide it.
1701 ///
1702 /// An item naming exactly one repository is created there; a task or a document naming
1703 /// none or several is created in its parent project's repository; and a project, or a
1704 /// task or document with no parent, naming none or several is created here. A board
1705 /// has no repository of its own and `createIssue` requires one, so a write without
1706 /// this is refused naming the field. Reads never need it.
1707 pub repository: Option<String>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the `owner/name` grammar before private construction.
1708 // llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
1709 /// Environment variable containing a fine-grained token with Projects and Issues
1710 /// read/write plus Pull requests read-only access for every repository represented on
1711 /// the board.
1712 #[serde(default = "default_token_env")]
1713 pub token_env: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the environment-variable grammar.
1714 /// GraphQL endpoint. GitHub Enterprise installations may override it.
1715 #[serde(default = "default_endpoint")]
1716 pub endpoint: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` converts it to the private validated `Url`.
1717 /// Per-instance mapping from a status category to the option of the board's one
1718 /// `Status` field it lands on, for a task and for a project.
1719 ///
1720 /// The shared `StatusMapping` grammar: each value is one option name for both kinds,
1721 /// `null` to disable the category for both, or `{task, project}` naming it per kind,
1722 /// where a kind left out leaves the category unmapped for that kind. A category this
1723 /// does not mention keeps its shipped default for both kinds: `backlog` to "Backlog",
1724 /// `todo` to "Todo", `queued` to "Queued", `in-progress` to "In Progress", `done` to
1725 /// "Done" plus closed as completed, `cancelled` to "Cancelled" plus closed as not
1726 /// planned, and `draft` and `unknown` unmapped. A category it does mention gets no
1727 /// shipped default for a kind it leaves out. `done` and `cancelled` close the issue for
1728 /// either kind. No two categories may name one option for the same kind, ignoring case.
1729 /// `unknown` may name one existing option; every unknown word then lands on it and
1730 /// reads back as `unknown` under its name. Unlike `local-md`, this source cannot keep
1731 /// each unknown word because it never creates board options.
1732 #[serde(default)]
1733 pub status_mapping: StatusMapping,
1734 /// Per-instance mapping from a task's priority to an option of this board's
1735 /// single-select field named `Priority`.
1736 ///
1737 /// Absent, this source holds no priority: every task reads as `none`, and a write of any
1738 /// other priority is refused before it reaches this board. Present, each of `urgent`,
1739 /// `high`, `medium` and `low` it does not mention keeps its shipped default — `Urgent`,
1740 /// `High`, `Medium` and `Low` — and an item with no value in the `Priority` field reads
1741 /// as `none`, so writing `none` clears the value. Option names match case-insensitively;
1742 /// no two levels may name one option. Reads and writes never create the field or an
1743 /// option: `onetaskgraph sources fields <source> --apply` does, and a write naming one
1744 /// the board lacks is refused pointing there.
1745 #[serde(default)]
1746 pub priority_mapping: Option<PriorityMappingConfig>,
1747 /// How fast this source writes, and how long it waits out a rate-limit refusal.
1748 ///
1749 /// Every field keeps its shipped default when it is absent, and the defaults are
1750 /// GitHub's own published limits rather than taste. See [`Pacing`].
1751 #[serde(default)]
1752 pub pacing: PacingConfig,
1753}
1754
1755/// Which option of the board's `Priority` field each priority lands on.
1756///
1757/// One member per level rather than a map, so a key that is not a level is refused where
1758/// the configuration is read, naming the levels there are. `none` is not a member: it is no
1759/// value in the field, not an option of it.
1760#[derive(Debug, Clone, Default, Deserialize, schemars::JsonSchema)]
1761#[serde(default, deny_unknown_fields)]
1762pub struct PriorityMappingConfig {
1763 /// The option `urgent` lands on; `Urgent` when absent.
1764 pub urgent: Option<PriorityOptionName>,
1765 /// The option `high` lands on; `High` when absent.
1766 pub high: Option<PriorityOptionName>,
1767 /// The option `medium` lands on; `Medium` when absent.
1768 pub medium: Option<PriorityOptionName>,
1769 /// The option `low` lands on; `Low` when absent.
1770 pub low: Option<PriorityOptionName>,
1771}
1772
1773/// The name of an option of the board's `Priority` single-select field.
1774///
1775/// Validated on the way in, for the reason [`ColumnName`] is: nothing on a board can have a
1776/// blank name.
1777#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
1778#[serde(try_from = "String")]
1779#[schemars(extend("minLength" = 1))]
1780pub struct PriorityOptionName(String);
1781
1782impl PriorityOptionName {
1783 /// The option name, as the board spells it.
1784 fn as_str(&self) -> &str {
1785 &self.0
1786 }
1787}
1788
1789impl TryFrom<String> for PriorityOptionName {
1790 type Error = String;
1791
1792 fn try_from(name: String) -> Result<Self, Self::Error> {
1793 if name.trim().is_empty() {
1794 return Err("a priority_mapping option name cannot be blank".to_owned());
1795 }
1796 Ok(Self(name))
1797 }
1798}
1799
1800/// The name of the board field a priority is held in.
1801pub const PRIORITY_FIELD: &str = "Priority";
1802
1803/// The four priorities a board option can hold, in the order a new `Priority` field lists
1804/// them. `none` is not among them: it is the field holding no value.
1805///
1806/// This list mirrors `Priority`, so it carries its own drift gate, in the shape [`CATEGORIES`]
1807/// does: [`level_position`] is a wildcard-free match, so a priority added to the shared
1808/// vocabulary fails to compile until it is placed there, and this crate's suite reconciles
1809/// this list and [`PriorityMappingConfig`]'s members against that enum's own derived schema.
1810pub const PRIORITY_LEVELS: [Priority; 4] = [
1811 Priority::Urgent,
1812 Priority::High,
1813 Priority::Medium,
1814 Priority::Low,
1815];
1816
1817/// Where one priority sits in [`PRIORITY_LEVELS`], or `None` for `none`, which is no option;
1818/// see that list for what this pins.
1819#[must_use]
1820pub const fn level_position(priority: Priority) -> Option<usize> {
1821 match priority {
1822 Priority::None => None,
1823 Priority::Urgent => Some(0),
1824 Priority::High => Some(1),
1825 Priority::Medium => Some(2),
1826 Priority::Low => Some(3),
1827 }
1828}
1829
1830/// This instance's complete priority-to-option mapping, read in both directions.
1831///
1832/// One option per level, held in [`PRIORITY_LEVELS`] order, once it is established that no
1833/// two levels name one option.
1834#[derive(Debug, Clone)]
1835struct PriorityMapping {
1836 options: [PriorityOptionName; 4],
1837}
1838
1839impl PriorityMapping {
1840 fn resolve(config: PriorityMappingConfig, instance: &SourceName) -> Result<Self, SourceError> {
1841 let shipped = |name: &str| PriorityOptionName(name.to_owned());
1842 let mapping = Self {
1843 options: [
1844 config.urgent.unwrap_or_else(|| shipped("Urgent")),
1845 config.high.unwrap_or_else(|| shipped("High")),
1846 config.medium.unwrap_or_else(|| shipped("Medium")),
1847 config.low.unwrap_or_else(|| shipped("Low")),
1848 ],
1849 };
1850 for (index, option) in mapping.options.iter().enumerate() {
1851 if let Some(earlier) = mapping.options[..index]
1852 .iter()
1853 .position(|other| other.as_str().eq_ignore_ascii_case(option.as_str()))
1854 {
1855 return Err(SourceError::Config {
1856 message: format!(
1857 "priority_mapping of source {instance} sends both {} and {} to the board \
1858 option {:?}; one option cannot read back as two priorities",
1859 PRIORITY_LEVELS[earlier],
1860 PRIORITY_LEVELS[index],
1861 option.as_str()
1862 ),
1863 });
1864 }
1865 }
1866 Ok(mapping)
1867 }
1868
1869 /// The option `priority` lands on, or `None` for `none`, which is no option at all.
1870 fn option(&self, priority: Priority) -> Option<&str> {
1871 level_position(priority).map(|index| self.options[index].as_str())
1872 }
1873
1874 /// The priority a board option name reports, or `None` when nothing maps to it.
1875 fn priority_of(&self, option: &str) -> Option<Priority> {
1876 self.options
1877 .iter()
1878 .position(|name| name.as_str().eq_ignore_ascii_case(option))
1879 .map(|index| PRIORITY_LEVELS[index])
1880 }
1881
1882 /// Every mapped option name, in the order a new `Priority` field lists them.
1883 fn names(&self) -> impl Iterator<Item = &str> {
1884 self.options.iter().map(PriorityOptionName::as_str)
1885 }
1886}
1887
1888/// What one item's `Priority` field says, read through this instance's mapping.
1889#[derive(Debug, Clone, PartialEq, Eq)]
1890enum HeldPriority {
1891 /// A priority this source reports: an option the mapping names, or no value (`none`).
1892 Read(Priority),
1893 /// An option the mapping does not name, which is never read as a level or as `none`.
1894 Unmapped(String),
1895}
1896
1897/// How fast this source writes, and how long it waits out a rate-limit refusal.
1898///
1899/// Configurable because a GitHub Enterprise installation sets its own limits and an
1900/// operator who has already been refused may want to go slower still — not because the
1901/// defaults are guesses.
1902#[derive(Debug, Clone, Default, Deserialize, schemars::JsonSchema)]
1903#[serde(default, deny_unknown_fields)]
1904pub struct PacingConfig {
1905 /// Shortest interval between two content-creating mutations, in milliseconds.
1906 ///
1907 /// Zero sends them as fast as they are asked for, which is what a fixture server on
1908 /// loopback wants and what no board on github.com does. At most [`MAX_PACING_MS`].
1909 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.
1910 /// First wait when a rate-limit refusal carries no hint, in milliseconds. Each
1911 /// further wait of the same call doubles it. At most [`MAX_PACING_MS`], and never
1912 /// zero while there is a budget to spend, because a schedule of zero-length waits
1913 /// consumes none of it and so never ends.
1914 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.
1915 /// Total time one call may spend waiting out rate limits, in milliseconds.
1916 ///
1917 /// Zero reports the refusal rather than waiting at all. At most [`MAX_PACING_MS`]:
1918 /// the bound is what makes this a wait rather than a hang.
1919 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.
1920}
1921
1922/// The largest any pacing setting may be, in milliseconds.
1923///
1924/// One hour. GitHub's own harshest published bound on content-generating requests works
1925/// out at one every 7.2 seconds, so an hour is already three orders of magnitude past
1926/// anything a real limit asks for, and past it the settings stop describing pacing at all:
1927/// a wait budget beyond it is the unbounded wait this whole mechanism exists to replace,
1928/// and an interval beyond it is a command that never sends its second mutation. It also
1929/// keeps the clock arithmetic in [`GitHubProjectsSource::reserve_mutation_slot`] inside
1930/// what an `Instant` can hold on every platform.
1931pub const MAX_PACING_MS: u64 = 3_600_000;
1932
1933/// [`PacingConfig`] with every default resolved and every value checked, which is what the
1934/// source holds.
1935#[derive(Debug, Clone, Copy)]
1936struct Pacing {
1937 min_mutation_interval: Duration,
1938 retry_backoff: Duration,
1939 retry_budget: Duration,
1940}
1941
1942impl Pacing {
1943 /// Resolve one instance's pacing, refusing a configuration that would not pace at all.
1944 fn resolve(config: PacingConfig, instance: &SourceName) -> Result<Self, SourceError> {
1945 let bounded = |value: Option<u64>, default: u64, field: &str| match value {
1946 Some(value) if value > MAX_PACING_MS => Err(SourceError::Config {
1947 message: format!(
1948 "pacing.{field} of source {instance} is {value} ms, and the most any pacing \
1949 setting may be is {MAX_PACING_MS} ms — an hour, which is already far past \
1950 GitHub's own harshest published limit"
1951 ),
1952 }),
1953 Some(value) => Ok(Duration::from_millis(value)),
1954 None => Ok(Duration::from_millis(default)),
1955 };
1956 let retry_backoff = bounded(
1957 config.retry_backoff_ms,
1958 RETRY_BACKOFF_MS,
1959 "retry_backoff_ms",
1960 )?;
1961 let retry_budget = bounded(config.retry_budget_ms, RETRY_BUDGET_MS, "retry_budget_ms")?;
1962 if retry_backoff.is_zero() && !retry_budget.is_zero() {
1963 return Err(SourceError::Config {
1964 message: format!(
1965 "pacing.retry_backoff_ms of source {instance} is 0 while \
1966 pacing.retry_budget_ms is {} ms; a schedule of zero-length waits spends \
1967 none of that budget, so it would retry a refusal forever. Set a backoff of \
1968 at least 1 ms, or set retry_budget_ms to 0 to report a refusal without \
1969 waiting at all",
1970 retry_budget.as_millis()
1971 ),
1972 });
1973 }
1974 Ok(Self {
1975 min_mutation_interval: bounded(
1976 config.min_mutation_interval_ms,
1977 MIN_MUTATION_INTERVAL_MS,
1978 "min_mutation_interval_ms",
1979 )?,
1980 retry_backoff,
1981 retry_budget,
1982 })
1983 }
1984}
1985
1986/// Factory for [`GitHubProjectsSource`].
1987#[derive(Debug, Clone, Copy, Default)]
1988pub struct Plugin;
1989
1990impl SourcePlugin for Plugin {
1991 fn kind(&self) -> &'static str {
1992 KIND
1993 }
1994 fn config_schema(&self) -> Schema {
1995 schema_for!(GitHubProjectsConfig)
1996 }
1997 fn build(
1998 &self,
1999 name: &SourceName,
2000 config: &Value,
2001 secrets: &dyn SecretResolver,
2002 ) -> Result<Box<dyn TaskSource>, SourceError> {
2003 self.build_recording_into(name, config, secrets, Arc::new(Accounting::new()))
2004 }
2005}
2006
2007impl Plugin {
2008 /// Build a source recording every request it sends into an accounting the caller holds.
2009 ///
2010 /// [`SourcePlugin::build`] is this with an accounting of its own, which is what the
2011 /// registry gets. This is for a caller that is also calling GitHub itself and wants one
2012 /// session total rather than two — see [`accounting`] and
2013 /// [`GitHubProjectsSource::recording_into`].
2014 ///
2015 /// # Errors
2016 ///
2017 /// Exactly [`SourcePlugin::build`]'s, with the same source name in front of each:
2018 /// [`SourceError::Config`] for configuration this plugin cannot use and
2019 /// [`SourceError::Auth`] for a credential it cannot find.
2020 pub fn build_recording_into(
2021 &self,
2022 name: &SourceName,
2023 config: &Value,
2024 secrets: &dyn SecretResolver,
2025 ledger: Arc<Accounting>,
2026 ) -> Result<Box<dyn TaskSource>, SourceError> {
2027 let config: GitHubProjectsConfig =
2028 serde_json::from_value(config.clone()).map_err(|e| SourceError::Config {
2029 message: format!("source {name}: {e}"),
2030 })?;
2031 let prefix = format!("source {name}: ");
2032 let source = GitHubProjectsSource::recording_into(name, config, secrets, ledger).map_err(
2033 |error| match error {
2034 // The shared `StatusMapping::distinct` names the source itself.
2035 SourceError::Config { message } if message.starts_with(&prefix) => {
2036 SourceError::Config { message }
2037 }
2038 SourceError::Config { message } => SourceError::Config {
2039 message: format!("{prefix}{message}"),
2040 },
2041 SourceError::Auth { message } => SourceError::Auth {
2042 message: format!("source {name}: {message}"),
2043 },
2044 other => other,
2045 },
2046 )?;
2047 Ok(Box::new(source))
2048 }
2049}
2050
2051/// Where a status category lands on this board, once configuration is resolved.
2052#[derive(Debug, Clone, PartialEq, Eq)]
2053enum StatusTarget {
2054 /// Not usable against this instance for this kind, and why.
2055 Disabled(UnmappedStatus),
2056 /// The board's `Status` option of this name.
2057 Column(ColumnName),
2058 /// A closed issue, with both its board option and the reason that says which closed it means.
2059 // 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.
2060 Terminal(ColumnName, ClosedState),
2061}
2062
2063/// Every status category, in the order the vocabulary declares them.
2064///
2065/// This list mirrors `StatusCategory`, so it carries its own drift gate rather than a
2066/// reviewer's attention: [`category_position`] is a wildcard-free match, so a variant
2067/// added to the shared vocabulary fails to compile until it is named there, and this
2068/// crate's suite reconciles this list against that enum's own derived schema, which is
2069/// generated from the variants rather than written beside them. The schema is what
2070/// catches a list left one short — a list checking only the positions it already holds
2071/// would pass while every mapping indexed by the new position panicked.
2072pub const CATEGORIES: [StatusCategory; 8] = [
2073 StatusCategory::Draft,
2074 StatusCategory::Backlog,
2075 StatusCategory::Todo,
2076 StatusCategory::Queued,
2077 StatusCategory::InProgress,
2078 StatusCategory::Done,
2079 StatusCategory::Cancelled,
2080 StatusCategory::Unknown,
2081];
2082
2083/// Where one category sits in [`CATEGORIES`]; see that list for what this pins.
2084#[must_use]
2085pub const fn category_position(category: StatusCategory) -> usize {
2086 match category {
2087 StatusCategory::Draft => 0,
2088 StatusCategory::Backlog => 1,
2089 StatusCategory::Todo => 2,
2090 StatusCategory::Queued => 3,
2091 StatusCategory::InProgress => 4,
2092 StatusCategory::Done => 5,
2093 StatusCategory::Cancelled => 6,
2094 StatusCategory::Unknown => 7,
2095 }
2096}
2097
2098/// The spelling a status category is configured and reported under.
2099fn category_name(category: StatusCategory) -> &'static str {
2100 match category {
2101 StatusCategory::Draft => "draft",
2102 StatusCategory::Backlog => "backlog",
2103 StatusCategory::Todo => "todo",
2104 StatusCategory::Queued => "queued",
2105 StatusCategory::InProgress => "in-progress",
2106 StatusCategory::Done => "done",
2107 StatusCategory::Cancelled => "cancelled",
2108 StatusCategory::Unknown => "unknown",
2109 }
2110}
2111
2112/// A shipped default's option name.
2113///
2114/// The literals below are this file's own and non-blank, and they are validated by the
2115/// one constructor a configured name goes through rather than beside it.
2116fn shipped_column(name: &'static str) -> ColumnName {
2117 ColumnName::try_from(name.to_owned()).expect("a shipped default names a board option")
2118}
2119
2120/// The shipped default for one category this instance's `status_mapping` does not mention,
2121/// for either kind.
2122fn shipped_default(category: StatusCategory) -> StatusTarget {
2123 match category {
2124 StatusCategory::Backlog => StatusTarget::Column(shipped_column("Backlog")),
2125 StatusCategory::Todo => StatusTarget::Column(shipped_column("Todo")),
2126 StatusCategory::Queued => StatusTarget::Column(shipped_column("Queued")),
2127 StatusCategory::InProgress => StatusTarget::Column(shipped_column("In Progress")),
2128 StatusCategory::Done => {
2129 StatusTarget::Terminal(shipped_column("Done"), ClosedState::Completed)
2130 }
2131 StatusCategory::Cancelled => {
2132 StatusTarget::Terminal(shipped_column("Cancelled"), ClosedState::NotPlanned)
2133 }
2134 StatusCategory::Draft | StatusCategory::Unknown => {
2135 StatusTarget::Disabled(UnmappedStatus::Unconfigured)
2136 }
2137 }
2138}
2139
2140/// The two kinds a status is written and read for, each with its own half of the mapping.
2141const STATUS_KINDS: [ItemKind; 2] = [ItemKind::Task, ItemKind::Project];
2142
2143/// This instance's complete category-to-target mapping for each kind, read in both
2144/// directions.
2145///
2146/// One target per category per kind, held at that category's own [`category_position`], so
2147/// a category missing from the mapping, named twice in it, or filed out of order is a state
2148/// this type cannot hold rather than one [`Self::target`] has to defend against. Both kinds'
2149/// targets are options of the board's one `Status` field.
2150#[derive(Debug, Clone)]
2151struct BoardStatuses {
2152 tasks: [StatusTarget; CATEGORIES.len()],
2153 projects: [StatusTarget; CATEGORIES.len()],
2154}
2155
2156impl BoardStatuses {
2157 /// Resolve `configured` against the shipped defaults, refusing two categories one kind
2158 /// would read back from one option.
2159 ///
2160 /// A category the mapping does not mention keeps its shipped default for both kinds; one
2161 /// it does mention is exactly what it configures, so a per-kind object leaves the kind it
2162 /// omits unmapped rather than defaulted.
2163 fn resolve(configured: &StatusMapping, instance: &SourceName) -> Result<Self, SourceError> {
2164 let resolve_kind =
2165 |kind: ItemKind| -> Result<[StatusTarget; CATEGORIES.len()], SourceError> {
2166 // `CATEGORIES[position] == category` for every category — the crate's suite
2167 // asserts it — so mapping the list in order fills each category's own slot.
2168 let mut targets = CATEGORIES.map(shipped_default);
2169 for (slot, category) in targets.iter_mut().zip(CATEGORIES) {
2170 if !configured.mentions(category) {
2171 continue;
2172 }
2173 *slot = match configured.name_for(category, kind) {
2174 Err(why) => StatusTarget::Disabled(why),
2175 Ok(name) => {
2176 let option = ColumnName::try_from(name.as_str().to_owned())
2177 .map_err(|message| SourceError::Config { message })?;
2178 match category {
2179 StatusCategory::Done => {
2180 StatusTarget::Terminal(option, ClosedState::Completed)
2181 }
2182 StatusCategory::Cancelled => {
2183 StatusTarget::Terminal(option, ClosedState::NotPlanned)
2184 }
2185 _ => StatusTarget::Column(option),
2186 }
2187 }
2188 };
2189 }
2190 StatusMapping::distinct(
2191 instance,
2192 kind,
2193 CATEGORIES
2194 .iter()
2195 .zip(&targets)
2196 .filter_map(|(category, target)| target.option().map(|o| (*category, o))),
2197 )?;
2198 Ok(targets)
2199 };
2200 Ok(Self {
2201 tasks: resolve_kind(ItemKind::Task)?,
2202 projects: resolve_kind(ItemKind::Project)?,
2203 })
2204 }
2205
2206 /// Every category's target for `kind`, in category order.
2207 const fn targets(&self, kind: ItemKind) -> &[StatusTarget; CATEGORIES.len()] {
2208 match kind {
2209 ItemKind::Task => &self.tasks,
2210 ItemKind::Project => &self.projects,
2211 }
2212 }
2213
2214 fn target(&self, kind: ItemKind, category: StatusCategory) -> &StatusTarget {
2215 &self.targets(kind)[category_position(category)]
2216 }
2217
2218 /// The category a board option name reports for `kind`, or `None` when nothing of that
2219 /// kind maps to it.
2220 fn category_of(&self, kind: ItemKind, option: &str) -> Option<StatusCategory> {
2221 CATEGORIES.into_iter().find(|category| {
2222 self.target(kind, *category)
2223 .option()
2224 .is_some_and(|name| name.eq_ignore_ascii_case(option))
2225 })
2226 }
2227
2228 /// Every option name either kind maps a category to, each once ignoring case, in
2229 /// category order with a task's name before a project's — what the guarded setup asks
2230 /// the `Status` field to hold.
2231 fn wanted(&self) -> Vec<String> {
2232 let mut wanted: Vec<String> = Vec::new();
2233 for category in CATEGORIES {
2234 for kind in STATUS_KINDS {
2235 if let Some(name) = self.target(kind, category).option()
2236 && !wanted.iter().any(|held| held.eq_ignore_ascii_case(name))
2237 {
2238 wanted.push(name.to_owned());
2239 }
2240 }
2241 }
2242 wanted
2243 }
2244
2245 /// The status an item of `kind` reports, from the three things a read of it says: its
2246 /// board `Status` option, whether its issue is closed, and the reason it was closed with.
2247 ///
2248 /// The closed state decides the category and the `Status` option decides the name, so
2249 /// a closed issue sitting in a "Shipped" column reports `done` named `Shipped`, whatever
2250 /// its kind. A closed issue whose reason is `DUPLICATE` or `REOPENED` reports `Unknown`:
2251 /// a duplicate is not finished work, and calling it done is a lie the next copy would
2252 /// write back. `REOPENED`-while-closed is a state this source can never produce, so
2253 /// it is read permissively rather than refused — reads are faithful, and refusals
2254 /// belong on writes. An open item's option reads through its own kind's mapping, and an
2255 /// option that mapping does not name reads as `Unknown` under its own name.
2256 ///
2257 /// One function of those three rather than of a response, so a narrow status write can
2258 /// answer what a re-read would report by applying it to the state it has just written.
2259 fn status(
2260 &self,
2261 kind: ItemKind,
2262 option: Option<&str>,
2263 closed: bool,
2264 reason: Option<&str>,
2265 ) -> Status {
2266 if closed {
2267 let category = match reason {
2268 None | Some("COMPLETED") => StatusCategory::Done,
2269 Some("NOT_PLANNED") => StatusCategory::Cancelled,
2270 Some(_) => StatusCategory::Unknown,
2271 };
2272 let fallback = match category {
2273 StatusCategory::Done => "Done",
2274 StatusCategory::Cancelled => "Cancelled",
2275 _ => "Closed",
2276 };
2277 return Status {
2278 category,
2279 name: option.unwrap_or(fallback).to_owned(),
2280 };
2281 }
2282 let name = option.unwrap_or("Open").to_owned();
2283 Status {
2284 category: self
2285 .category_of(kind, &name)
2286 .unwrap_or(StatusCategory::Unknown),
2287 name,
2288 }
2289 }
2290}
2291
2292impl BoardStatuses {
2293 /// For each kind, the option names it maps a category to that `existing` lacks, ignoring
2294 /// case; a kind lacking none is left out.
2295 fn missing_by_kind(&self, existing: &[StatusOption]) -> Vec<KindMissing> {
2296 STATUS_KINDS
2297 .into_iter()
2298 .filter_map(|kind| {
2299 let missing: Vec<String> = self
2300 .targets(kind)
2301 .iter()
2302 .filter_map(StatusTarget::option)
2303 .filter(|wanted| {
2304 !existing
2305 .iter()
2306 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
2307 })
2308 .map(str::to_owned)
2309 .collect();
2310 (!missing.is_empty()).then_some(KindMissing { kind, missing })
2311 })
2312 .collect()
2313 }
2314}
2315
2316impl StatusTarget {
2317 /// The board option this target selects, or `None` for an unmapped one.
2318 fn option(&self) -> Option<&str> {
2319 match self {
2320 Self::Column(name) | Self::Terminal(name, _) => Some(name.as_str()),
2321 Self::Disabled(_) => None,
2322 }
2323 }
2324}
2325
2326// 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.
2327/// One repository this source can create an issue in, as `owner/name`.
2328///
2329/// Every `createIssue` this source sends names one of these: the item's own single
2330/// `repositories` entry, else its parent project issue's repository, else the configured
2331/// [`GitHubProjectsConfig::repository`]. [`GitHubProjectsSource::creation_target`] makes
2332/// that choice and says what it refuses before `createIssue`.
2333// llmlint: ignore-end[comments_earn_their_place, contracts_have_one_source_or_a_drift_gate]
2334#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
2335struct RepositoryTarget {
2336 owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
2337 name: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
2338}
2339
2340impl RepositoryTarget {
2341 fn parse(value: &str) -> Result<Self, SourceError> {
2342 let (owner, name) = value.split_once('/').ok_or_else(|| SourceError::Config {
2343 message: format!(
2344 "repository must be spelled owner/name; {value:?} names no repository"
2345 ),
2346 })?;
2347 if !valid_github_owner(owner) || !valid_github_repository_name(name) {
2348 return Err(SourceError::Config {
2349 message: format!(
2350 "repository must be spelled owner/name with a GitHub login and one \
2351 repository name; {value:?} is not"
2352 ),
2353 });
2354 }
2355 Ok(Self {
2356 owner: owner.to_owned(),
2357 name: name.to_owned(),
2358 })
2359 }
2360
2361 /// The one host whose repositories this source creates issues in, spelled once: it is
2362 /// what [`Self::origin`] renders and what [`Self::from_origin`] accepts.
2363 const HOST: &str = "github.com";
2364
2365 fn origin(&self) -> String {
2366 format!("{}/{}/{}", Self::HOST, self.owner, self.name)
2367 }
2368
2369 /// The repository a normalized origin names, or why it is none this source can create
2370 /// an issue in: another host, or more or fewer than `owner/name` under this one.
2371 fn from_origin(origin: &Repository) -> Result<Self, String> {
2372 let not_here = || {
2373 format!(
2374 "{} is not a {}/owner/name repository",
2375 origin.as_str(),
2376 Self::HOST
2377 )
2378 };
2379 let (host, rest) = origin.as_str().split_once('/').ok_or_else(not_here)?;
2380 if host != Self::HOST {
2381 return Err(not_here());
2382 }
2383 Self::parse(rest).map_err(|_| not_here())
2384 }
2385
2386 fn slug(&self) -> String {
2387 format!("{}/{}", self.owner, self.name)
2388 }
2389}
2390
2391/// A source which reads GitHub afresh for every operation.
2392pub struct GitHubProjectsSource {
2393 /// This source's configured name, used both to tell a far end naming this source
2394 /// from one naming a system it knows nothing about, and to name the instance a
2395 /// status refusal is about.
2396 name: SourceName,
2397 owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after full GitHub-owner validation.
2398 project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after GraphQL-Int validation.
2399 repository: Option<RepositoryTarget>,
2400 endpoint: Url,
2401 token: SecretString,
2402 credential_name: String, // llmlint: ignore[invalid_states_unrepresentable] Private diagnostic value constructed only after environment-name validation.
2403 statuses: BoardStatuses,
2404 /// Where each priority lands on this board, or `None` when this instance holds none.
2405 priorities: Option<PriorityMapping>,
2406 client: Client,
2407 /// Every item this source has created in this command, in the order it created them —
2408 /// dropped by [`TaskSource::end_command`].
2409 ///
2410 /// GitHub's `projectV2.items` is eventually consistent: an issue added to a board with
2411 /// `addProjectV2ItemById` is routinely absent from the very next read of that board, so
2412 /// a copy resolving a dependency on an item it had just created refused it as not
2413 /// found. A board read is completed from this — an item remembered here and absent from
2414 /// the read is added back, because the board really does hold it and only the read is
2415 /// behind.
2416 ///
2417 /// It is not a cache of a user's work: nothing is remembered that this process did not
2418 /// itself just write, it lives and dies with the process, and it is never consulted for
2419 /// an item this source did not create.
2420 created: Mutex<Vec<Resolved>>,
2421 /// Every item that already existed and that this source has written in this command, as
2422 /// it wrote it — dropped by [`TaskSource::end_command`].
2423 ///
2424 /// The other half of [`Self::created`], held on the same terms and for the reason a
2425 /// narrowed read needs it: an answer from GitHub's search or from the board's own field
2426 /// filter is an index behind a write this process made moments ago, so a query matching
2427 /// what this process just wrote onto an existing item would otherwise miss it. Nothing
2428 /// is remembered that this process did not itself just write.
2429 updated: Mutex<Vec<Resolved>>,
2430 /// Every issue this source has added a comment to or edited a comment of in this command
2431 /// — dropped by [`TaskSource::end_command`].
2432 ///
2433 /// A comment-activity read is narrowed by GitHub's issue search, whose `updated:` index
2434 /// lags the write that moved an issue's `updatedAt`, and neither [`Self::created`] nor
2435 /// [`Self::updated`] is moved by a comment, so an issue this process had just commented
2436 /// on was missing from such a read — or ruled out by the `updatedAt` its own record held
2437 /// from before — until the index caught up. Each id here is a candidate of every such
2438 /// search-narrowed read, and wherever it is a candidate its comments are read rather than
2439 /// it being ruled out by a stale `updatedAt`; that read is of the issue's own node, so it
2440 /// is current. It holds ids alone: nothing of a comment is remembered. A comment another
2441 /// process wrote is still found only once the index has it.
2442 commented: Mutex<Vec<NativeId>>,
2443 /// How fast this source writes, and how long it waits out a refusal.
2444 pacing: Pacing,
2445 /// When the last content-creating mutation finished, or the moment the furthest-out
2446 /// reserved slot releases the next one, whichever is later — so the one after it can be
2447 /// spaced from that. See [`MIN_MUTATION_INTERVAL_MS`] for the interval and
2448 /// [`GitHubProjectsSource::finish_mutation`] for why completion rather than release is
2449 /// what it is measured from.
2450 last_mutation: Mutex<Option<Instant>>,
2451 /// The board as this process last read it, for the length of one command — dropped by
2452 /// [`TaskSource::end_command`].
2453 ///
2454 /// A copy of a project used to re-read the whole board, paged, before writing each of
2455 /// its items, which is by far the largest part of a copy's request count and none of
2456 /// its work. Nothing else changes this board while a command runs — this source's own
2457 /// writes are the only writer — so one read answers them all.
2458 ///
2459 /// It is not a store of a user's work and it is not the cache the no-persistence
2460 /// invariant forbids: it lives and dies with the process exactly as `created` does,
2461 /// nothing is written down, and [`Self::board`] still completes it from `created`, so
2462 /// an item this command created and then depends on resolves whether or not GitHub's
2463 /// own eventually-consistent read has caught up. A write to an item already on the
2464 /// board updates the entry here too, so what this holds is the last read plus this
2465 /// process's own writes rather than a snapshot taken before them.
2466 board_cache: Mutex<Option<Board>>,
2467 /// Every issue this board's own search reported, for the length of one command — dropped
2468 /// by [`TaskSource::end_command`].
2469 ///
2470 /// The second half of a board read, and cached for the same reason and on the same
2471 /// terms as the first: it lives and dies with the process, nothing is written down, and
2472 /// a write this process makes updates the entry here exactly as it updates the one in
2473 /// [`Self::board_cache`]. One read answers every question a command asks, so a command
2474 /// that lists this board's projects and its tasks pays for one search rather than two.
2475 search_cache: Mutex<Option<Vec<Resolved>>>,
2476 /// What each narrowed question GitHub was asked answered, keyed by that question, for
2477 /// the length of one command — dropped by [`TaskSource::end_command`].
2478 ///
2479 /// The narrowed counterpart of [`Self::search_cache`], held on the same terms: it lives
2480 /// and dies with the process, nothing is written down, a write this process makes
2481 /// updates the entry here as it updates the other two, and every answer is completed
2482 /// with this process's own writes each time it is given. A command that asks the same
2483 /// narrowed question twice — a wait polling for its own items, a listing repeated after a
2484 /// write — pays for it once, which is what the whole-board read it replaced gave it.
2485 narrowed_cache: Mutex<BTreeMap<String, Vec<Resolved>>>,
2486 search_next: Mutex<BTreeMap<String, Option<String>>>,
2487 /// Records already resolved in this command, reused by writes and for comment identity.
2488 /// Explicit item reads still reach GitHub. Nothing is persisted, and
2489 /// [`TaskSource::end_command`] drops every record, so a write in the next command reads
2490 /// its item as a person has since left it.
2491 resolved_cache: Mutex<BTreeMap<NativeId, Resolved>>,
2492 /// The board's own id and field definitions as this process last read them on their
2493 /// own, for the length of one command — dropped by [`TaskSource::end_command`].
2494 ///
2495 /// What a write needs of the board and its item does not say, read once per command
2496 /// rather than once per item written, on the terms [`Self::board_cache`] is held on: it
2497 /// lives and dies with the process and nothing is written down. It holds no item and so
2498 /// can answer no question about one — see [`Self::board_fields`].
2499 fields_cache: Mutex<Option<BoardFields>>,
2500 /// Each destination repository's node id, resolved once per repository
2501 /// rather than per issue created.
2502 ///
2503 /// A repository's node id does not change, and re-reading it for every issue of a copy
2504 /// spent one request per item on an answer this source already had. It is a map rather
2505 /// than one entry because a copy files each item in the repository its own
2506 /// `repositories` field names, so a plan across five repositories asks GitHub five
2507 /// times and not once per item.
2508 repository_cache: Mutex<BTreeMap<RepositoryTarget, String>>,
2509 /// What every request this source sends is recorded into.
2510 ///
2511 /// Ordinary code path, not a mode: [`Self::send_once`] records into it at the one place
2512 /// a request leaves this crate, so nothing has to be switched on for a session to be
2513 /// counted. It is shared rather than owned so a caller accounting for a whole session —
2514 /// its own schema verification, board lookups, residue sweep and cleanup beside this
2515 /// source's reads and writes — adds up one accounting instead of two. See
2516 /// [`accounting`] for what a record carries and what a session's spend is and is not.
2517 ledger: Arc<Accounting>,
2518}
2519
2520/// GitHub's closed single-select color vocabulary.
2521#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2522#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
2523pub enum StatusOptionColor {
2524 /// Gray.
2525 Gray,
2526 /// Blue.
2527 Blue,
2528 /// Green.
2529 Green,
2530 /// Yellow.
2531 Yellow,
2532 /// Purple.
2533 Purple,
2534 /// Red.
2535 Red,
2536 /// Orange.
2537 Orange,
2538 /// Pink.
2539 Pink,
2540}
2541
2542/// Whether a guarded board setup — of the fields, or of the Status options alone — plans or
2543/// applies its additions.
2544#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2545pub enum SetupMode {
2546 /// Read without mutation.
2547 Plan,
2548 /// Apply and verify.
2549 Apply,
2550}
2551
2552/// The name [`SetupMode`] had when Status was the one field set up, kept so a caller written
2553/// against it goes on compiling.
2554pub type StatusOptionsMode = SetupMode;
2555
2556/// The explicit result of the requested operation.
2557#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2558#[serde(rename_all = "kebab-case")]
2559pub enum StatusOptionsOutcome {
2560 /// A read-only plan.
2561 Planned,
2562 /// Apply found nothing missing.
2563 Unchanged,
2564 /// Additions were applied and verified.
2565 Applied,
2566}
2567
2568/// A GitHub single-select option's opaque GraphQL node identifier.
2569#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2570#[serde(transparent)]
2571pub struct StatusOptionId(#[schemars(length(min = 1))] String);
2572
2573impl TryFrom<String> for StatusOptionId {
2574 type Error = String;
2575
2576 fn try_from(id: String) -> Result<Self, Self::Error> {
2577 if id.trim().is_empty() {
2578 return Err("a GitHub Status option id cannot be blank".to_owned());
2579 }
2580 Ok(Self(id))
2581 }
2582}
2583
2584/// One existing or proposed option in a guarded Status-field update.
2585#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2586pub struct StatusOption {
2587 /// GitHub's stable id.
2588 pub id: StatusOptionId,
2589 /// The visible option name.
2590 pub name: ColumnName,
2591 /// GitHub's single-select color token.
2592 pub color: StatusOptionColor,
2593 /// The option description, including an empty one.
2594 pub description: String,
2595}
2596
2597/// One board item's Status assignment, retained as recovery data.
2598#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2599pub struct StatusAssignment {
2600 /// The project item id whose assignment this is.
2601 // llmlint: ignore[invalid_states_unrepresentable] This opaque GraphQL node ID is
2602 // carried verbatim as operator recovery data; introducing a semantic type would claim
2603 // validation rules GitHub does not publish and no operation here interprets.
2604 pub item_id: String,
2605 /// The selected option, absent when the item has no status.
2606 #[serde(skip_serializing_if = "Option::is_none")]
2607 pub option: Option<AssignedStatusOption>,
2608}
2609
2610/// The inseparable id and name of an assigned option.
2611#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2612pub struct AssignedStatusOption {
2613 /// GitHub's stable id.
2614 pub id: StatusOptionId,
2615 /// The visible name.
2616 pub name: ColumnName,
2617}
2618
2619/// The plan and verified outcome of reconciling configured Status options.
2620#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2621pub struct StatusOptionsReport {
2622 /// The configured source name.
2623 pub source: SourceName,
2624 /// Configured option names absent before the operation.
2625 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a
2626 // `ColumnName` and has therefore already passed its nonblank validation; retaining the
2627 // serialized string here preserves the report's intentionally simple public contract.
2628 pub missing: Vec<String>,
2629 /// What the requested operation did.
2630 pub outcome: StatusOptionsOutcome,
2631 /// The complete option list observed before any mutation.
2632 pub existing: Vec<StatusOption>,
2633}
2634
2635#[derive(Debug, Clone, PartialEq, Eq)]
2636struct StatusSnapshot {
2637 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
2638 // passed back as the mutation's project identity; a newtype could enforce no stronger
2639 // invariant because GitHub publishes no grammar for it.
2640 board_id: String,
2641 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
2642 // passed back as the mutation's field identity; a newtype could enforce no stronger
2643 // invariant because GitHub publishes no grammar for it.
2644 field_id: String,
2645 options: Vec<StatusOption>,
2646 assignments: Vec<StatusAssignment>,
2647}
2648
2649/// The name of the board field a status is held in.
2650const STATUS_FIELD: &str = "Status";
2651
2652/// Every item's value of each field `report` names, as it stood before the setup wrote
2653/// anything — what a person puts back when the setup is refused part way.
2654fn recovery(report: &FieldsReport, before: &BoardSnapshot) -> Result<String, SourceError> {
2655 let assignments: BTreeMap<&str, Vec<StatusAssignment>> = report
2656 .fields
2657 .iter()
2658 .map(|field| (field.field.name(), before.assignments(field.field)))
2659 .collect();
2660 serde_json::to_string_pretty(&assignments).map_err(|error| SourceError::Malformed {
2661 message: format!("cannot render the pre-write field recovery snapshot: {error}"),
2662 })
2663}
2664
2665/// One board field the guarded setup reads and writes — every one it reads, and the only
2666/// ones it writes.
2667#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, schemars::JsonSchema)]
2668pub enum BoardField {
2669 /// The single-select `Status` field every instance's `status_mapping` resolves into.
2670 Status,
2671 /// The single-select `Priority` field an instance's `priority_mapping` resolves into.
2672 Priority,
2673}
2674
2675impl BoardField {
2676 /// The field's name on the board.
2677 #[must_use]
2678 pub const fn name(self) -> &'static str {
2679 match self {
2680 Self::Status => STATUS_FIELD,
2681 Self::Priority => PRIORITY_FIELD,
2682 }
2683 }
2684
2685 /// The field a board calls `name`, or `None` for one this setup does not own.
2686 fn named(name: &str) -> Option<Self> {
2687 [Self::Status, Self::Priority]
2688 .into_iter()
2689 .find(|field| field.name() == name)
2690 }
2691}
2692
2693/// What the guarded setup did to one field.
2694#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2695#[serde(rename_all = "kebab-case")]
2696pub enum FieldOutcome {
2697 /// A read-only plan.
2698 Planned,
2699 /// Apply found the field there with every configured option.
2700 Unchanged,
2701 /// Missing options were added to the field that was there, and verified.
2702 Applied,
2703 /// The field was not there; it was created holding the configured options, and verified.
2704 Created,
2705}
2706
2707/// One field's plan, or its verified outcome.
2708#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2709pub struct FieldReport {
2710 /// Which field.
2711 pub field: BoardField,
2712 /// Whether the board had the field before the operation.
2713 // llmlint: ignore[invalid_states_unrepresentable] `exists` beside `outcome` is the report's
2714 // wire shape as its consumer's contract fixes it — `{"field", "exists", "missing",
2715 // "outcome", "existing"}` — so folding one into the other would change a published JSON
2716 // shape. The contradictory pairings cannot be built: `GitHubProjectsSource::fields` is the
2717 // one constructor, and it derives `outcome` from `exists` in one match.
2718 pub exists: bool,
2719 /// Configured option names the field lacked before the operation — every one of them,
2720 /// in the order a new field lists them, when the field was not there at all.
2721 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
2722 // mapping name and has therefore already passed its nonblank validation; the serialized
2723 // string is the report's intentionally simple public contract, as `StatusOptionsReport`'s is.
2724 pub missing: Vec<String>,
2725 /// For the `Status` field, which item kind each missing name is configured for: one
2726 /// entry per kind `status_mapping` names a missing option for, task before project, each
2727 /// listing that kind's missing names in category order. A name both kinds use is in
2728 /// both. Empty — and left out of the JSON — when nothing is missing, and always for
2729 /// `Priority`, which only a task holds.
2730 #[serde(default, skip_serializing_if = "Vec::is_empty")]
2731 // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
2732 // both SDKs model an absent `kinds` as an empty list rather than as `null`.
2733 #[schemars(!skip_serializing_if)]
2734 pub kinds: Vec<KindMissing>,
2735 /// What the requested operation did.
2736 pub outcome: FieldOutcome,
2737 /// The field's complete option list observed before any mutation; empty when the field
2738 /// was not there.
2739 pub existing: Vec<StatusOption>,
2740}
2741
2742/// The `Status` option names one item kind's `status_mapping` names that the field lacked.
2743#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2744pub struct KindMissing {
2745 /// The kind these names are configured for.
2746 pub kind: ItemKind,
2747 /// The names that kind maps a category to and the field lacked, in category order.
2748 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
2749 // mapping name, as `FieldReport::missing`'s do, and the serialized string is the report's
2750 // intentionally simple public contract.
2751 pub missing: Vec<String>,
2752}
2753
2754/// The plan and verified outcome of setting up every field a source's configuration names.
2755#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2756pub struct FieldsReport {
2757 /// The configured source name.
2758 pub source: SourceName,
2759 /// `Status`, always, and `Priority` when the source sets `priority_mapping`.
2760 // llmlint: ignore[invalid_states_unrepresentable] A list is the report's wire shape as its
2761 // consumer's contract fixes it — `{"source", "fields": [...]}` — so a struct with one member
2762 // per field would change a published JSON shape. The states the list could hold and the
2763 // contract forbids cannot be built: `GitHubProjectsSource::fields` is the one constructor,
2764 // and it pushes `Status` first and exactly once, then `Priority` exactly when configured.
2765 pub fields: Vec<FieldReport>,
2766}
2767
2768/// Which options one field is configured with, in the order a new field would list them.
2769struct FieldPlan {
2770 field: BoardField,
2771 wanted: Vec<String>,
2772}
2773
2774/// One single-select field as the guarded setup snapshots it.
2775#[derive(Debug, Clone, PartialEq, Eq)]
2776struct SnapshotField {
2777 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
2778 // passed back as the mutation's field identity; a newtype could enforce no stronger
2779 // invariant because GitHub publishes no grammar for it.
2780 field_id: String,
2781 options: Vec<StatusOption>,
2782}
2783
2784/// Every single-select field of a board and every item's value of each.
2785#[derive(Debug, Clone, PartialEq, Eq)]
2786struct BoardSnapshot {
2787 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
2788 // passed back as the mutation's project identity; a newtype could enforce no stronger
2789 // invariant because GitHub publishes no grammar for it.
2790 board_id: String,
2791 fields: BTreeMap<BoardField, SnapshotField>,
2792 /// Each board item's id, and its value of each field this setup owns that it holds one of.
2793 items: Vec<(String, BTreeMap<BoardField, AssignedStatusOption>)>,
2794}
2795
2796impl BoardSnapshot {
2797 /// Every item's value of `field`, in board order — the recovery data a drift refusal
2798 /// carries.
2799 fn assignments(&self, field: BoardField) -> Vec<StatusAssignment> {
2800 self.items
2801 .iter()
2802 .map(|(item_id, values)| StatusAssignment {
2803 item_id: item_id.clone(),
2804 option: values.get(&field).cloned(),
2805 })
2806 .collect()
2807 }
2808}
2809
2810impl GitHubProjectsSource {
2811 /// Report missing configured Status options and, when `apply` is true, add them with
2812 /// a whole-list mutation that preserves every existing id and verifies the result.
2813 ///
2814 /// # Errors
2815 ///
2816 /// Refuses a board without a single-select `Status` field. A post-write difference in
2817 /// any pre-existing option id or item assignment is refused with the complete pre-write
2818 /// assignment snapshot in the diagnostic for recovery.
2819 // llmlint: ignore[changed_behavior_has_e2e] The CLI journeys cover plan, no-op apply,
2820 // successful mutation, both drift refusals, source selection, missing Status, casing,
2821 // and paging. Transport errors remain the shared `graphql` boundary's behavior rather
2822 // than a new status-options behavior, and the pinned-schema test prevents valid GitHub
2823 // responses from entering the defensive malformed-response branches below.
2824 pub async fn status_options(
2825 &self,
2826 mode: StatusOptionsMode,
2827 ) -> Result<StatusOptionsReport, SourceError> {
2828 let before = self.status_snapshot().await?;
2829 // A terminal category's option is as configured as an open one's: a terminal
2830 // write validates it before closing and refuses when the board lacks it. Both
2831 // kinds' names are options of the one field, so both are asked for.
2832 let missing = self
2833 .statuses
2834 .wanted()
2835 .into_iter()
2836 .filter(|wanted| {
2837 !before
2838 .options
2839 .iter()
2840 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
2841 })
2842 .collect::<Vec<_>>();
2843 let report = StatusOptionsReport {
2844 source: self.name.clone(),
2845 missing: missing.clone(),
2846 outcome: match (mode, missing.is_empty()) {
2847 (StatusOptionsMode::Plan, _) => StatusOptionsOutcome::Planned,
2848 (StatusOptionsMode::Apply, true) => StatusOptionsOutcome::Unchanged,
2849 (StatusOptionsMode::Apply, false) => StatusOptionsOutcome::Applied,
2850 },
2851 existing: before.options.clone(),
2852 };
2853 if mode == StatusOptionsMode::Plan || missing.is_empty() {
2854 return Ok(report);
2855 }
2856 let mut options = before
2857 .options
2858 .iter()
2859 .map(|option| {
2860 json!({
2861 "id": option.id, "name": option.name, "color": option.color,
2862 "description": option.description,
2863 })
2864 })
2865 .collect::<Vec<_>>();
2866 options.extend(missing.iter().map(|name| {
2867 json!({
2868 "name": name, "color": "GRAY", "description": ""
2869 })
2870 }));
2871 self.graphql(
2872 graphql::STATUS_OPTIONS_UPDATE,
2873 json!({"input": {
2874 "projectId": before.board_id, "fieldId": before.field_id,
2875 "singleSelectOptions": options,
2876 }}),
2877 )
2878 .await?;
2879 let after = self.status_snapshot().await?;
2880 let options_preserved = before
2881 .options
2882 .iter()
2883 .all(|old| after.options.iter().any(|new| new == old));
2884 let additions_present = missing.iter().all(|wanted| {
2885 after
2886 .options
2887 .iter()
2888 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
2889 });
2890 if !options_preserved || !additions_present || after.assignments != before.assignments {
2891 let recovery = serde_json::to_string_pretty(&before.assignments).map_err(|error| {
2892 SourceError::Malformed {
2893 message: format!("cannot render pre-write Status recovery snapshot: {error}"),
2894 }
2895 })?;
2896 return Err(SourceError::Refused {
2897 message: format!(
2898 "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}"
2899 ),
2900 });
2901 }
2902 Ok(report)
2903 }
2904
2905 /// A fresh snapshot of the Status field and every board item's assignment of it.
2906 ///
2907 /// # Errors
2908 ///
2909 /// Refuses a board without a single-select `Status` field, and one the token cannot see.
2910 async fn status_snapshot(&self) -> Result<StatusSnapshot, SourceError> {
2911 // Status alone, as this operation has always read it: a `Priority` field is another
2912 // operation's, so nothing about it can refuse this one.
2913 let mut board = self.board_snapshot(&[BoardField::Status]).await?;
2914 let field = board
2915 .fields
2916 .remove(&BoardField::Status)
2917 .ok_or_else(|| self.no_status_field())?;
2918 Ok(StatusSnapshot {
2919 assignments: board.assignments(BoardField::Status),
2920 board_id: board.board_id,
2921 field_id: field.field_id,
2922 options: field.options,
2923 })
2924 }
2925
2926 /// The refusal a board with no `Status` field is answered with by the guarded setup.
2927 fn no_status_field(&self) -> SourceError {
2928 SourceError::Refused {
2929 message: format!("source {} board has no Status field", self.name),
2930 }
2931 }
2932
2933 // llmlint: ignore-block[changed_behavior_has_e2e] Valid snapshot shapes are exercised through
2934 // the real CLI loopback journey, including pagination. The individual malformed guards
2935 // are defensive validation of a schema-pinned third-party response, not separate user
2936 // journeys; drift and missing-field failures cover the operation's recovery behavior.
2937 /// A fresh snapshot of each of the `owned` fields on the board, with its options, and of
2938 /// every board item's value of each, walked to the end of the board's items. A field not
2939 /// in `owned` is read past whatever it holds.
2940 async fn board_snapshot(&self, owned: &[BoardField]) -> Result<BoardSnapshot, SourceError> {
2941 let mut after: Option<String> = None;
2942 let mut snapshot: Option<BoardSnapshot> = None;
2943 loop {
2944 let data = self
2945 .graphql(
2946 graphql::STATUS_OPTIONS_SNAPSHOT,
2947 json!({
2948 "owner": self.owner, "number": self.project_number,
2949 "first": MAX_PAGE_SIZE, "after": after, "nestedFirst": MAX_PAGE_SIZE,
2950 }),
2951 )
2952 .await?;
2953 let board = data
2954 .pointer("/owner/projectV2")
2955 .filter(|board| board.is_object())
2956 .ok_or_else(|| SourceError::Refused {
2957 message: format!(
2958 "source {} has no accessible GitHub Projects board",
2959 self.name
2960 ),
2961 })?;
2962 if board
2963 .pointer("/fields/pageInfo/hasNextPage")
2964 .and_then(Value::as_bool)
2965 != Some(false)
2966 {
2967 return Err(SourceError::Malformed {
2968 message:
2969 "GitHub project fields is incomplete or has malformed pageInfo.hasNextPage"
2970 .into(),
2971 });
2972 }
2973 let mut fields = BTreeMap::new();
2974 // Only the fields this setup owns, by name: a node the single-select fragment did not
2975 // match carries no name, and a person's own single-select field — a `Size`, a
2976 // `Team` — is none of this setup's business, so nothing about it can refuse one. A
2977 // `Status` or `Priority` field without its options is malformed, not absent.
2978 // 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.
2979 for (owned, field) in board
2980 .pointer("/fields/nodes")
2981 .and_then(Value::as_array)
2982 .ok_or_else(|| SourceError::Malformed {
2983 message: "GitHub project fields.nodes is not an array".into(),
2984 })?
2985 .iter()
2986 .filter_map(|field| {
2987 let named = BoardField::named(field.get("name")?.as_str()?)?;
2988 owned.contains(&named).then_some((named, field))
2989 })
2990 {
2991 let options = field
2992 .get("options")
2993 .and_then(Value::as_array)
2994 .ok_or_else(|| SourceError::Malformed {
2995 message: "GitHub single-select field options is not an array".into(),
2996 })?
2997 .iter()
2998 .map(|option| {
2999 Ok(StatusOption {
3000 id: StatusOptionId::try_from(required_str(option, "id")?.to_owned())
3001 .map_err(|message| SourceError::Malformed { message })?,
3002 name: ColumnName::try_from(required_str(option, "name")?.to_owned())
3003 .map_err(|message| SourceError::Malformed {
3004 message: format!(
3005 "GitHub single-select option name is invalid: {message}"
3006 ),
3007 })?,
3008 color: serde_json::from_value(
3009 option.get("color").cloned().unwrap_or(Value::Null),
3010 )
3011 .map_err(|error| {
3012 SourceError::Malformed {
3013 message: format!(
3014 "GitHub single-select option color is invalid: {error}"
3015 ),
3016 }
3017 })?,
3018 description: optional_str(option, "description")?
3019 .unwrap_or_default()
3020 .to_owned(),
3021 })
3022 })
3023 .collect::<Result<Vec<_>, SourceError>>()?;
3024 let snapshot = SnapshotField {
3025 field_id: required_nonblank_str(field, "id")?.to_owned(),
3026 options,
3027 };
3028 // A board's field names are unique, so a second one is an answer that cannot
3029 // say which field the setup would act on — refused rather than one chosen.
3030 if fields.insert(owned, snapshot).is_some() {
3031 return Err(SourceError::Malformed {
3032 message: format!(
3033 "GitHub answered two {} fields for this board",
3034 owned.name()
3035 ),
3036 });
3037 }
3038 }
3039 let board_id = required_nonblank_str(board, "id")?.to_owned();
3040 let current = snapshot.get_or_insert_with(|| BoardSnapshot {
3041 board_id,
3042 fields,
3043 items: Vec::new(),
3044 });
3045 let items = board
3046 .pointer("/items/nodes")
3047 .and_then(Value::as_array)
3048 .ok_or_else(|| SourceError::Malformed {
3049 message: "GitHub project items.nodes is not an array".into(),
3050 })?;
3051 for item in items {
3052 let field_values =
3053 item.get("fieldValues")
3054 .ok_or_else(|| SourceError::Malformed {
3055 message: "GitHub project item is missing fieldValues".into(),
3056 })?;
3057 if field_values
3058 .pointer("/pageInfo/hasNextPage")
3059 .and_then(Value::as_bool)
3060 != Some(false)
3061 {
3062 return Err(SourceError::Malformed {
3063 message: "GitHub project item fieldValues is incomplete or has malformed pageInfo.hasNextPage".into(),
3064 });
3065 }
3066 let values = item
3067 .pointer("/fieldValues/nodes")
3068 .and_then(Value::as_array)
3069 .ok_or_else(|| SourceError::Malformed {
3070 message: "GitHub project item fieldValues.nodes is not an array".into(),
3071 })?;
3072 let item_id = required_nonblank_str(item, "id")?;
3073 let mut assigned = BTreeMap::new();
3074 for value in values {
3075 let Some(field) = value
3076 .pointer("/field/name")
3077 .and_then(Value::as_str)
3078 .and_then(BoardField::named)
3079 .filter(|field| owned.contains(field))
3080 else {
3081 continue;
3082 };
3083 let held = assigned.insert(
3084 field,
3085 AssignedStatusOption {
3086 id: StatusOptionId::try_from(
3087 required_str(value, "optionId")?.to_owned(),
3088 )
3089 .map_err(|message| SourceError::Malformed { message })?,
3090 name: ColumnName::try_from(required_str(value, "name")?.to_owned())
3091 .map_err(|message| SourceError::Malformed {
3092 message: format!(
3093 "GitHub assigned {} name is invalid: {message}",
3094 field.name()
3095 ),
3096 })?,
3097 },
3098 );
3099 // An item holds one value of a field, so a second one leaves no way to
3100 // tell which it holds — and a verification or recovery built on either
3101 // could restore the wrong one.
3102 if held.is_some() {
3103 return Err(SourceError::Malformed {
3104 message: format!(
3105 "GitHub answered two {} values for board item {item_id}",
3106 field.name()
3107 ),
3108 });
3109 }
3110 }
3111 current.items.push((item_id.to_owned(), assigned));
3112 }
3113 let page = board.get("items").ok_or_else(|| SourceError::Malformed {
3114 message: "GitHub project is missing items".into(),
3115 })?;
3116 let has_next = page
3117 .pointer("/pageInfo/hasNextPage")
3118 .and_then(Value::as_bool)
3119 .ok_or_else(|| SourceError::Malformed {
3120 message: "GitHub project items.pageInfo.hasNextPage is not a boolean".into(),
3121 })?;
3122 if !has_next {
3123 break;
3124 }
3125 let next =
3126 required_nonblank_str(page.get("pageInfo").unwrap_or(&Value::Null), "endCursor")?;
3127 validate_cursor_progress(after.as_deref(), next)?;
3128 after = Some(next.to_owned());
3129 }
3130 snapshot.ok_or_else(|| SourceError::Malformed {
3131 message: "GitHub returned no board field snapshot".into(),
3132 })
3133 }
3134 // llmlint: ignore-end[changed_behavior_has_e2e]
3135
3136 /// Report every board field this source's configuration names and, with
3137 /// [`SetupMode::Apply`], set each up: add the options a field lacks, and create
3138 /// the `Priority` field when the board has none.
3139 ///
3140 /// The fields are `Status`, always, with the options `status_mapping` resolves to; and
3141 /// `Priority`, when `priority_mapping` is set, with its four mapped options — created in
3142 /// the order urgent, high, medium, low. An option a field already has keeps its id, name,
3143 /// color and description: the whole option list goes back with every existing id, because
3144 /// a re-minted id clears every item's value.
3145 ///
3146 /// # Errors
3147 ///
3148 /// Refuses a board without a single-select `Status` field. After an apply the board is
3149 /// read again, and a pre-existing option or any item's value of either field that moved is
3150 /// refused with the complete pre-write assignments in the diagnostic, for recovery.
3151 // llmlint: ignore[changed_behavior_has_e2e] The `sources fields` journeys drive plan,
3152 // unchanged apply, a created field, an added option to each field, drift refusal, a board
3153 // with no Status field and a non-github-projects source through the compiled CLI against
3154 // the loopback board. Transport errors are the shared `graphql` boundary's behavior.
3155 pub async fn fields(&self, mode: SetupMode) -> Result<FieldsReport, SourceError> {
3156 let owned: Vec<BoardField> = if self.priorities.is_some() {
3157 vec![BoardField::Status, BoardField::Priority]
3158 } else {
3159 vec![BoardField::Status]
3160 };
3161 let before = self.board_snapshot(&owned).await?;
3162 let mut plans = vec![FieldPlan {
3163 field: BoardField::Status,
3164 wanted: self.statuses.wanted(),
3165 }];
3166 if !before.fields.contains_key(&BoardField::Status) {
3167 return Err(self.no_status_field());
3168 }
3169 if let Some(mapping) = &self.priorities {
3170 plans.push(FieldPlan {
3171 field: BoardField::Priority,
3172 wanted: mapping.names().map(str::to_owned).collect(),
3173 });
3174 }
3175 // The snapshot reads single-select fields alone, so a field it did not find may still
3176 // be on the board under the name, of another type: creating one beside it would fail
3177 // part way, or leave two fields of one name. Asked of the board's own field list, and
3178 // only when a field is missing.
3179 if plans
3180 .iter()
3181 .any(|plan| !before.fields.contains_key(&plan.field))
3182 {
3183 let board = self.board_fields().await?;
3184 for plan in plans
3185 .iter()
3186 .filter(|plan| !before.fields.contains_key(&plan.field))
3187 {
3188 if let Some(field) = Board::field(&board.fields, plan.field.name())? {
3189 return Err(SourceError::Refused {
3190 message: format!(
3191 "source {}'s board has a {} field that is not a single-select field \
3192 (it is a {}), so it cannot hold this source's options; next: rename \
3193 or remove that field, then run this again",
3194 self.name,
3195 plan.field.name(),
3196 optional_str(field, "__typename")?.unwrap_or("field of another type")
3197 ),
3198 });
3199 }
3200 }
3201 }
3202 let mut reports = Vec::new();
3203 for plan in &plans {
3204 let held = before.fields.get(&plan.field);
3205 let existing = held.map(|field| field.options.clone()).unwrap_or_default();
3206 let mut missing: Vec<String> = Vec::new();
3207 for wanted in &plan.wanted {
3208 let present = existing
3209 .iter()
3210 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3211 || missing
3212 .iter()
3213 .any(|named| named.eq_ignore_ascii_case(wanted));
3214 if !present {
3215 missing.push(wanted.clone());
3216 }
3217 }
3218 let kinds = match plan.field {
3219 BoardField::Status => self.statuses.missing_by_kind(&existing),
3220 BoardField::Priority => Vec::new(),
3221 };
3222 reports.push(FieldReport {
3223 field: plan.field,
3224 exists: held.is_some(),
3225 kinds,
3226 outcome: match (mode, held.is_some(), missing.is_empty()) {
3227 (SetupMode::Plan, _, _) => FieldOutcome::Planned,
3228 (SetupMode::Apply, true, true) => FieldOutcome::Unchanged,
3229 (SetupMode::Apply, true, false) => FieldOutcome::Applied,
3230 (SetupMode::Apply, false, _) => FieldOutcome::Created,
3231 },
3232 missing,
3233 existing,
3234 });
3235 }
3236 let report = FieldsReport {
3237 source: self.name.clone(),
3238 fields: reports,
3239 };
3240 let writes: Vec<&FieldReport> = report
3241 .fields
3242 .iter()
3243 .filter(|field| !field.missing.is_empty() || !field.exists)
3244 .collect();
3245 if mode == SetupMode::Plan || writes.is_empty() {
3246 return Ok(report);
3247 }
3248 let mut landed: Vec<&str> = Vec::new();
3249 for field in &writes {
3250 let added = field
3251 .missing
3252 .iter()
3253 .map(|name| json!({"name": name, "color": "GRAY", "description": ""}));
3254 let sent = match before.fields.get(&field.field) {
3255 Some(held) => {
3256 let mut options = held
3257 .options
3258 .iter()
3259 .map(|option| {
3260 json!({
3261 "id": option.id, "name": option.name, "color": option.color,
3262 "description": option.description,
3263 })
3264 })
3265 .collect::<Vec<_>>();
3266 options.extend(added);
3267 self.graphql(
3268 graphql::STATUS_OPTIONS_UPDATE,
3269 json!({"input": {
3270 "projectId": before.board_id, "fieldId": held.field_id,
3271 "singleSelectOptions": options,
3272 }}),
3273 )
3274 .await
3275 }
3276 None => {
3277 self.graphql(
3278 graphql::CREATE_FIELD,
3279 json!({"input": {
3280 "projectId": before.board_id, "dataType": "SINGLE_SELECT",
3281 "name": field.field.name(),
3282 "singleSelectOptions": added.collect::<Vec<_>>(),
3283 }}),
3284 )
3285 .await
3286 }
3287 };
3288 // A mutation that failed does not establish that GitHub left its field as it was,
3289 // so every failure from here on carries the recovery data a drift refusal does.
3290 match sent {
3291 Ok(_) => landed.push(field.field.name()),
3292 Err(error) => {
3293 let changed = if landed.is_empty() {
3294 String::new()
3295 } else {
3296 format!("changed the {} field and then ", landed.join(" and "))
3297 };
3298 return Err(SourceError::Refused {
3299 message: format!(
3300 "the guarded field setup {changed}failed on the {} field, which it may \
3301 have changed part way: {error}; the pre-write item assignments \
3302 are:\n{}",
3303 field.field.name(),
3304 recovery(&report, &before)?
3305 ),
3306 });
3307 }
3308 }
3309 }
3310 // The board has been written, so a verification read that fails leaves it unverified
3311 // rather than unchanged, and says what to put back.
3312 let after = match self.board_snapshot(&owned).await {
3313 Ok(after) => after,
3314 Err(error) => {
3315 return Err(SourceError::Refused {
3316 message: format!(
3317 "the guarded field setup changed the {} field and then could not read the \
3318 board back to verify it: {error}; the pre-write item assignments are:\n{}",
3319 landed.join(" and "),
3320 recovery(&report, &before)?
3321 ),
3322 });
3323 }
3324 };
3325 let mut moved = Vec::new();
3326 for field in &report.fields {
3327 let name = field.field.name();
3328 let now = after
3329 .fields
3330 .get(&field.field)
3331 .map(|held| held.options.as_slice())
3332 .unwrap_or_default();
3333 if !field.existing.iter().all(|old| now.contains(old)) {
3334 moved.push(format!(
3335 "a pre-existing {name} option id, name, color or description"
3336 ));
3337 }
3338 if !field.missing.iter().all(|wanted| {
3339 now.iter()
3340 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3341 }) {
3342 moved.push(format!("an added {name} option"));
3343 }
3344 if after.assignments(field.field) != before.assignments(field.field) {
3345 moved.push(format!("an item's {name} value"));
3346 }
3347 }
3348 if !moved.is_empty() {
3349 return Err(SourceError::Refused {
3350 message: format!(
3351 "GitHub changed {} after the guarded field setup; the pre-write item \
3352 assignments are:\n{}",
3353 moved.join(", "),
3354 recovery(&report, &before)?
3355 ),
3356 });
3357 }
3358 Ok(report)
3359 }
3360
3361 /// Validate configuration and capture the named credential without exposing it.
3362 ///
3363 /// # Errors
3364 ///
3365 /// Returns [`SourceError::Config`] for a configuration this instance cannot use and
3366 /// [`SourceError::Auth`] when the named credential is missing or empty.
3367 pub fn new(
3368 name: &SourceName,
3369 config: GitHubProjectsConfig,
3370 secrets: &dyn SecretResolver,
3371 ) -> Result<Self, SourceError> {
3372 Self::recording_into(name, config, secrets, Arc::new(Accounting::new()))
3373 }
3374
3375 /// The same, recording every request it sends into an accounting the caller holds too.
3376 ///
3377 /// [`Self::new`] is this with an accounting of its own. A caller that is also making
3378 /// its own calls to GitHub — a lane verifying a schema, sweeping residue or cleaning
3379 /// up — passes the one it records those into, so the session total accounts for the
3380 /// whole session rather than for this source's share of it.
3381 ///
3382 /// # Errors
3383 ///
3384 /// Exactly [`Self::new`]'s: [`SourceError::Config`] for a configuration this instance
3385 /// cannot use and [`SourceError::Auth`] when the named credential is missing or empty.
3386 pub fn recording_into(
3387 name: &SourceName,
3388 config: GitHubProjectsConfig,
3389 secrets: &dyn SecretResolver,
3390 ledger: Arc<Accounting>,
3391 ) -> Result<Self, SourceError> {
3392 if !valid_github_owner(&config.owner) {
3393 return Err(SourceError::Config {
3394 message: "owner must be 1-39 ASCII letters, digits, or single hyphens, and cannot start or end with a hyphen".into(),
3395 });
3396 }
3397 if config.project_number == 0 || config.project_number > i32::MAX as u32 {
3398 return Err(SourceError::Config {
3399 message: format!("project_number must be between 1 and {}", i32::MAX),
3400 });
3401 }
3402 if !valid_environment_name(&config.token_env) {
3403 return Err(SourceError::Config {
3404 message: "token_env must be a valid environment-variable name".into(),
3405 });
3406 }
3407 let repository = config
3408 .repository
3409 .as_deref()
3410 .map(RepositoryTarget::parse)
3411 .transpose()?;
3412 let endpoint = Url::parse(&config.endpoint).map_err(|e| SourceError::Config {
3413 message: format!("endpoint is not a valid URL: {e}"),
3414 })?;
3415 if endpoint.scheme() != "https"
3416 && !(endpoint.scheme() == "http"
3417 && endpoint
3418 .host_str()
3419 .is_some_and(|h| h == "127.0.0.1" || h == "localhost" || h == "::1"))
3420 {
3421 return Err(SourceError::Config {
3422 message:
3423 "endpoint must use HTTPS (HTTP is accepted only for a loopback test server)"
3424 .into(),
3425 });
3426 }
3427 let token = secrets.get(&config.token_env).filter(|token| !token.expose_secret().trim().is_empty()).ok_or_else(|| SourceError::Auth {
3428 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),
3429 })?;
3430 Ok(Self {
3431 name: name.clone(),
3432 owner: config.owner,
3433 project_number: config.project_number,
3434 repository,
3435 endpoint,
3436 token,
3437 credential_name: config.token_env,
3438 statuses: BoardStatuses::resolve(&config.status_mapping, name)?,
3439 priorities: config
3440 .priority_mapping
3441 .map(|mapping| PriorityMapping::resolve(mapping, name))
3442 .transpose()?,
3443 client: Client::builder()
3444 .user_agent("onetaskgraph")
3445 .build()
3446 .map_err(|e| SourceError::Config {
3447 message: format!("cannot build HTTP client: {e}"),
3448 })?,
3449 created: Mutex::new(Vec::new()),
3450 updated: Mutex::new(Vec::new()),
3451 commented: Mutex::new(Vec::new()),
3452 pacing: Pacing::resolve(config.pacing, name)?,
3453 last_mutation: Mutex::new(None),
3454 board_cache: Mutex::new(None),
3455 search_cache: Mutex::new(None),
3456 narrowed_cache: Mutex::new(BTreeMap::new()),
3457 resolved_cache: Mutex::new(BTreeMap::new()),
3458 search_next: Mutex::new(BTreeMap::new()),
3459 fields_cache: Mutex::new(None),
3460 repository_cache: Mutex::new(BTreeMap::new()),
3461 ledger,
3462 })
3463 }
3464
3465 /// A snapshot of every request this source has sent, and what each cost.
3466 ///
3467 /// A value to hold and compare rather than a borrow of the accounting itself, so two
3468 /// of them can sit side by side. When this source was built with
3469 /// [`Self::recording_into`] the snapshot is the whole shared session, which is the
3470 /// point of building it that way.
3471 #[must_use]
3472 pub fn accounting(&self) -> accounting::Session {
3473 self.ledger.snapshot()
3474 }
3475
3476 /// Send one GraphQL document, pacing this source's own mutations and waiting out a
3477 /// rate limit rather than handing it straight back as an error.
3478 ///
3479 /// Retrying is safe for every document here, including the mutations, and the reason
3480 /// is that only a *refusal* is retried: [`Limiter::classify`] rules on a response
3481 /// GitHub sent, and a request GitHub refused for a rate limit did not run, so nothing
3482 /// this replays has already taken effect. An outcome this source cannot know — the
3483 /// send failed, or the body could not be read, so the mutation may well have landed —
3484 /// is [`Attempt::Failed`] in [`send_once`] and leaves this loop without a second
3485 /// attempt. A duplicate write would come from replaying one of those, and none is
3486 /// replayed.
3487 async fn graphql(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
3488 if is_mutation(query)
3489 && ![
3490 graphql::ADD_COMMENT,
3491 graphql::UPDATE_COMMENT,
3492 graphql::DELETE_COMMENT,
3493 ]
3494 .contains(&query)
3495 {
3496 let mut cache = self.resolved_cache()?;
3497 for argument in ["input", "second", "third", "clear"] {
3498 if let Some(input) = variables.get(argument) {
3499 cache.retain(|id, item| {
3500 !["id", "issueId", "subjectId", "itemId"].iter().any(|key| {
3501 input
3502 .get(key)
3503 .and_then(Value::as_str)
3504 .is_some_and(|value| value == id.0 || value == item.item_id)
3505 })
3506 });
3507 }
3508 }
3509 }
3510 let doing = operation_description(query);
3511 let mut waited = Duration::ZERO;
3512 let mut waits = 0_u32;
3513 let mut backoff = self.pacing.retry_backoff;
3514 loop {
3515 if is_mutation(query) {
3516 let spacing = self.reserve_mutation_slot();
3517 if !spacing.is_zero() {
3518 tokio::time::sleep(spacing).await;
3519 }
3520 }
3521 let attempt = self.send_once(query, &variables).await;
3522 if is_mutation(query) {
3523 self.finish_mutation();
3524 }
3525 let limited = match attempt {
3526 Ok(data) => return Ok(data),
3527 Err(Attempt::Failed(error)) => return Err(error),
3528 Err(Attempt::Limited(limited)) => limited,
3529 };
3530 // GitHub really does send `retry-after: 0`, and retrying at once is the one
3531 // move that extends a secondary limit, so a hint below the schedule's own next
3532 // wait is raised to it.
3533 let wait = match limited.hint {
3534 Some(hint) => Duration::from_secs(hint).max(backoff),
3535 None => backoff,
3536 };
3537 let remaining = self.pacing.retry_budget.saturating_sub(waited);
3538 // A wait of nothing spends none of the budget, so it is exhaustion rather
3539 // than a retry. `Pacing::resolve` rules out every way of configuring one
3540 // except a budget of zero, where reporting the first refusal is the ask.
3541 if wait.is_zero() || wait > remaining {
3542 return Err(limited.exhausted(
3543 doing,
3544 waits,
3545 waited,
3546 wait,
3547 self.pacing.retry_budget,
3548 ));
3549 }
3550 tokio::time::sleep(wait).await;
3551 waited += wait;
3552 waits += 1;
3553 backoff = backoff.saturating_mul(2);
3554 }
3555 }
3556
3557 /// The next moment a content-creating mutation may leave this source, as a wait from
3558 /// now.
3559 ///
3560 /// The slot is reserved under the lock and the waiting happens outside it, so two
3561 /// callers take two slots rather than the same one — and no lock is held across an
3562 /// await.
3563 ///
3564 /// The moment it is spaced from is the previous mutation's *completion*, which
3565 /// [`Self::finish_mutation`] records. See that method for why the release moment on its
3566 /// own is the wrong thing to measure from.
3567 fn reserve_mutation_slot(&self) -> Duration {
3568 if self.pacing.min_mutation_interval.is_zero() {
3569 return Duration::ZERO;
3570 }
3571 // A poisoned lock here costs pacing, not correctness, and refusing the write over
3572 // it would turn an earlier failure into a second one for no gain.
3573 let mut last = self
3574 .last_mutation
3575 .lock()
3576 .unwrap_or_else(std::sync::PoisonError::into_inner);
3577 let now = Instant::now();
3578 // `checked_add` rather than `+`: `Instant + Duration` panics on overflow, and
3579 // pacing is not worth a panic even at a bound `MAX_PACING_MS` already rules out.
3580 let at = last.map_or(now, |previous| {
3581 previous
3582 .checked_add(self.pacing.min_mutation_interval)
3583 .map_or(now, |earliest| earliest.max(now))
3584 });
3585 *last = Some(at);
3586 at.saturating_duration_since(now)
3587 }
3588
3589 /// Record that a content-creating mutation has finished, so the next one is spaced
3590 /// from here rather than from the moment this one was released.
3591 ///
3592 /// This source can only choose when a request *departs*; the limiter counts when it
3593 /// *arrives*, and the two differ by whatever the request spent in transit. Spacing one
3594 /// departure from the last therefore hands the limiter a gap of the interval less that
3595 /// transit, so a source pacing at 750 ms can still be seen arriving faster — which is
3596 /// exactly how a copy paced well inside a board's threshold was refused by it on a
3597 /// slower machine while passing on a quick one.
3598 ///
3599 /// Spacing from completion removes the subtraction rather than budgeting for it. The
3600 /// previous request had already arrived before its response came back, so its arrival
3601 /// is no later than this moment, and the next mutation is released at least the
3602 /// interval after this moment and arrives no earlier than it is released: the gap the
3603 /// limiter measures is therefore at least the interval, whatever transit costs and on
3604 /// whatever platform. The price is that a mutation's own round trip no longer counts
3605 /// towards its spacing, which makes this source slightly slower than the configured
3606 /// rate rather than slightly faster — the safe side of a limit that punishes being
3607 /// wrong by refusing reads for the next fifty minutes.
3608 ///
3609 /// A failed attempt is recorded too: a request refused by the limiter still arrived,
3610 /// and one that never left costs only a wait nobody needed.
3611 fn finish_mutation(&self) {
3612 if self.pacing.min_mutation_interval.is_zero() {
3613 return;
3614 }
3615 // A poisoned lock here costs pacing, not correctness, exactly as in the reservation.
3616 let mut last = self
3617 .last_mutation
3618 .lock()
3619 .unwrap_or_else(std::sync::PoisonError::into_inner);
3620 let now = Instant::now();
3621 // `max` rather than an assignment: a concurrent caller may already have reserved a
3622 // slot further out, and completing this request must never pull that slot back in.
3623 *last = Some(last.map_or(now, |reserved| reserved.max(now)));
3624 }
3625
3626 /// One HTTP attempt, classified into an answer, a rate limit to wait out, or a
3627 /// failure that waiting cannot help — and recorded, whichever of the three it was.
3628 ///
3629 /// This is the one place a request leaves this crate, which is why the accounting is
3630 /// here rather than at each of the callers: a read path added later is counted without
3631 /// anybody remembering to count it, and
3632 /// `the_session_report_counts_every_request_the_board_served_and_what_each_cost` fails
3633 /// when one is not.
3634 async fn send_once(&self, query: &str, variables: &Value) -> Result<Value, Attempt> {
3635 let Attempted {
3636 result,
3637 limits,
3638 reported_cost,
3639 } = self.attempt(query, variables).await;
3640 // No `otherwise` name: every document this source sends is one of its own, and the
3641 // inventory gate on `graphql::DOCUMENTS` is what keeps that true.
3642 let sending = accounting::Request::graphql(query, variables, None, reported_cost);
3643 let outcome = match &result {
3644 Ok(_) => accounting::Outcome::Answered,
3645 Err(Attempt::Limited(_)) => accounting::Outcome::RateLimited,
3646 Err(Attempt::Failed(_)) => accounting::Outcome::Refused,
3647 };
3648 self.ledger.record(sending.finished(outcome, limits));
3649 result
3650 }
3651
3652 /// The attempt itself, with what its response said about the rate limit alongside.
3653 ///
3654 /// The two are returned together rather than recorded here because every one of the
3655 /// early exits below is a different outcome, and a record written at each of them is a
3656 /// record one of them can be added without.
3657 async fn attempt(&self, query: &str, variables: &Value) -> Attempted {
3658 let mut limits = accounting::RateLimit::default();
3659 let mut reported_cost = None;
3660 let result = self
3661 .attempted(query, variables, &mut limits, &mut reported_cost)
3662 .await;
3663 Attempted {
3664 result,
3665 limits,
3666 reported_cost,
3667 }
3668 }
3669
3670 /// One HTTP attempt, filling in what its response said about the rate limit as it goes.
3671 async fn attempted(
3672 &self,
3673 query: &str,
3674 variables: &Value,
3675 limits: &mut accounting::RateLimit,
3676 reported_cost: &mut Option<u64>,
3677 ) -> Result<Value, Attempt> {
3678 let response = self
3679 .client
3680 .post(self.endpoint.clone())
3681 .bearer_auth(self.token.expose_secret())
3682 .json(&json!({"query": query, "variables": variables}))
3683 .send()
3684 .await
3685 .map_err(|e| {
3686 Attempt::Failed(SourceError::Unavailable {
3687 message: format!("GitHub GraphQL request failed: {e}"),
3688 })
3689 })?;
3690 let status = response.status();
3691 let header = |name: &str| whole_seconds(response.headers().get(name));
3692 *limits = accounting::RateLimit::read(|name| {
3693 response
3694 .headers()
3695 .get(name)
3696 .and_then(|value| value.to_str().ok())
3697 .map(str::to_owned)
3698 });
3699 // Exactly `0` is exhaustion and everything else — a count, an empty value, bytes
3700 // that are not text at all — is "not known to be exhausted". This never makes a
3701 // response a refusal on its own: it says which limiter a refusal is attributed to
3702 // and where its hint comes from, so a value this cannot read costs a hint rather
3703 // than an answer.
3704 let exhausted = response
3705 .headers()
3706 .get("x-ratelimit-remaining")
3707 .and_then(|value| value.to_str().ok())
3708 == Some("0");
3709 // `retry-after` is what GitHub asks for when it asks; when it does not and the
3710 // primary budget is spent, `x-ratelimit-reset` says when that budget comes back,
3711 // which is the same question answered as an absolute time. Nothing else here is a
3712 // hint, and a schedule is what answers a refusal that carries none.
3713 let hint = header("retry-after").or_else(|| {
3714 exhausted
3715 .then(|| header("x-ratelimit-reset"))
3716 .flatten()
3717 .map(|reset| reset.saturating_sub(Utc::now().timestamp().max(0).unsigned_abs()))
3718 });
3719 // Read before it is parsed, because the evidence which tells a secondary rate
3720 // limit from a rejected credential is in the body of a response whose status says
3721 // only "forbidden" — and a non-success response was never parsed at all.
3722 let body = response.text().await.map_err(|e| {
3723 Attempt::Failed(SourceError::Unavailable {
3724 message: format!("GitHub GraphQL response could not be read: {e}"),
3725 })
3726 })?;
3727 if let Some(limiter) = Limiter::classify(status, exhausted, &body) {
3728 return Err(Attempt::Limited(Limited { limiter, hint }));
3729 }
3730 if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {
3731 return Err(Attempt::Failed(SourceError::Auth {
3732 message: format!(
3733 "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"
3734 ),
3735 }));
3736 }
3737 if !status.is_success() {
3738 return Err(Attempt::Failed(SourceError::Unavailable {
3739 message: format!("GitHub GraphQL returned HTTP {status}"),
3740 }));
3741 }
3742 // GitHub reports what a call cost only when the document asked it to, and no
3743 // document this source sends does — so this is `None` here and carries the figure
3744 // for a caller whose own document selects `rateLimit { cost }`. What it must never
3745 // pick up is a `dryRun` probe's cost, which is some other document's.
3746 *reported_cost = serde_json::from_str::<Value>(&body)
3747 .ok()
3748 .as_ref()
3749 .and_then(|body| body.pointer("/data/rateLimit/cost"))
3750 .and_then(Value::as_u64);
3751 self.answer(&body).map_err(Attempt::Failed)
3752 }
3753
3754 /// What one successful HTTP response says, once its GraphQL errors are read.
3755 fn answer(&self, body: &str) -> Result<Value, SourceError> {
3756 let body: Value = serde_json::from_str(body).map_err(|e| SourceError::Malformed {
3757 message: format!("GitHub returned invalid JSON: {e}"),
3758 })?;
3759 let errors = body
3760 .get("errors")
3761 .map(|value| {
3762 value.as_array().ok_or_else(|| SourceError::Malformed {
3763 message: "GitHub response errors is not an array".into(),
3764 })
3765 })
3766 .transpose()?;
3767 if let Some(errors) = errors.filter(|errors| !errors.is_empty()) {
3768 let messages = errors
3769 .iter()
3770 .filter_map(|e| e.get("message").and_then(Value::as_str))
3771 .collect::<Vec<_>>()
3772 .join("; ");
3773 let message = if messages.is_empty() {
3774 "GitHub returned GraphQL errors".into()
3775 } else {
3776 messages
3777 };
3778 let normalized = message.to_ascii_lowercase();
3779 if normalized.contains("resource not accessible") || normalized.contains("scope") {
3780 return Err(SourceError::Auth {
3781 message: format!(
3782 "{message}; grant {} Projects and Issues read/write plus Pull requests read-only access for every repository represented on the board",
3783 self.credential_name
3784 ),
3785 });
3786 }
3787 return Err(SourceError::Refused { message });
3788 }
3789 body.get("data")
3790 .filter(|data| data.is_object())
3791 .cloned()
3792 .ok_or_else(|| SourceError::Malformed {
3793 message: "GitHub response has no data object".into(),
3794 })
3795 }
3796
3797 // llmlint: ignore[boundary_inputs_validated] GitHub caps nested connections at 100 and
3798 // GraphQL cannot independently page them inside the outer item page. This source page is
3799 // deliberately bounded at that published maximum; the live drift journey exercises it.
3800 async fn board_page(
3801 &self,
3802 items_after: Option<&str>,
3803 items_first: u32,
3804 ) -> Result<Value, SourceError> {
3805 let data = self
3806 .graphql(
3807 graphql::BOARD,
3808 json!({"owner":self.owner,"number":self.project_number,
3809 "first":items_first.min(MAX_PAGE_SIZE),"after":items_after,
3810 "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
3811 )
3812 .await?;
3813 data.pointer("/owner/projectV2")
3814 .filter(|v| !v.is_null())
3815 .cloned()
3816 .ok_or_else(|| SourceError::Refused {
3817 message: format!(
3818 "GitHub project {}/{} was not found or is not visible to the token",
3819 self.owner, self.project_number
3820 ),
3821 })
3822 }
3823
3824 /// The search that finds the issues of this board, narrowed by `also` when it is
3825 /// given.
3826 ///
3827 /// `project:owner/number` is what scopes a search to one board, and `is:issue` is what
3828 /// keeps pull requests out of it: GitHub's `ISSUE` search type covers both, and a pull
3829 /// request is somebody's change rather than a unit of plan. `-has:parent` is *not*
3830 /// here on purpose — GitHub accepts it and silently ignores it, so a project is told
3831 /// from a task by the `parent` field each issue carries rather than by the search.
3832 fn board_search(&self, also: Option<&str>) -> String {
3833 let scope = format!("project:{}/{} is:issue", self.owner, self.project_number);
3834 match also {
3835 Some(also) => format!("{scope} {also}"),
3836 None => scope,
3837 }
3838 }
3839
3840 /// One issue this source reached directly, as the board item a read of the board would
3841 /// have produced — or `None` when this board does not hold it.
3842 ///
3843 /// The board half of an issue rides along on `Issue.projectItems`, so the value handed
3844 /// to [`Self::resolve`] is the very shape a `ProjectV2.items` read gives it: the board
3845 /// item's own id, that item's field values, and the issue as its content. One resolver
3846 /// for both routes is what makes an issue read through a search, through its own node
3847 /// id, or through its project's sub-issues report the same title, the same status, the
3848 /// same labels and the same qualified id.
3849 ///
3850 /// An issue with no entry for *this* board is not this source's to report, which is
3851 /// what keeps an id naming some other repository's issue from being answered as an item
3852 /// of this board. That answer is given about an **exhausted** connection and never
3853 /// about an unread page: the entry is looked for on the page in hand, and only if that
3854 /// page reports more of the connection, in [`Self::board_membership`]'s walk of the
3855 /// rest of it.
3856 async fn resolve_issue(&self, issue: &Value) -> Result<Option<Resolved>, SourceError> {
3857 if optional_str(issue, "__typename")? != Some("Issue") {
3858 return Ok(None);
3859 }
3860 let memberships = issue
3861 .get("projectItems")
3862 .ok_or_else(|| SourceError::Malformed {
3863 message: "GitHub issue is missing projectItems".into(),
3864 })?;
3865 let nodes = memberships
3866 .get("nodes")
3867 .and_then(Value::as_array)
3868 .ok_or_else(|| SourceError::Malformed {
3869 message: "GitHub issue projectItems.nodes is not an array".into(),
3870 })?;
3871 let held = match self.board_entry(nodes) {
3872 Some(held) => held.clone(),
3873 None => {
3874 let info = memberships
3875 .get("pageInfo")
3876 .ok_or_else(|| SourceError::Malformed {
3877 message: "GitHub issue projectItems has no pageInfo".into(),
3878 })?;
3879 // The page held no entry for this board. Whether that means the issue is
3880 // not on it is a question about the rest of the connection, and only a
3881 // connection with no rest answers it here.
3882 if !required_bool(info, "hasNextPage")? {
3883 return Ok(None);
3884 }
3885 let cursor = required_str(info, "endCursor")?;
3886 validate_cursor_progress(None, cursor)?;
3887 let issue_id = required_str(issue, "id")?;
3888 match self.board_membership(issue_id, cursor).await? {
3889 Some(held) => held,
3890 None => return Ok(None),
3891 }
3892 }
3893 };
3894 let item = json!({
3895 "id": required_str(&held, "id")?,
3896 "project": held.get("project"),
3897 "fieldValues": held.get("fieldValues"),
3898 "content": issue,
3899 });
3900 self.resolve(&item)
3901 }
3902
3903 /// This board's own entry among one page of an issue's `Issue.projectItems`.
3904 ///
3905 /// One spelling of *which membership is this board's*, so the page a read carries and
3906 /// the pages [`Self::board_membership`] walks are searched by the same rule.
3907 fn board_entry<'a>(&self, nodes: &'a [Value]) -> Option<&'a Value> {
3908 nodes.iter().find(|node| {
3909 node.pointer("/project/number").and_then(Value::as_u64)
3910 == Some(u64::from(self.project_number))
3911 })
3912 }
3913
3914 /// The rest of one issue's board memberships, from `after`, for this board's entry.
3915 ///
3916 /// The recovery read: a page of memberships that holds no entry for this board says
3917 /// nothing about the memberships past it, so the connection is walked to exhaustion
3918 /// before an issue is reported as one this board does not hold. `Ok(None)` is that
3919 /// positive answer — the whole connection was read and no entry named this board —
3920 /// rather than a failure, and the walk is held to
3921 /// [`validate_cursor_progress`] like every other page walk here, so a source answering
3922 /// with a cursor that does not advance is refused instead of spun on.
3923 async fn board_membership(
3924 &self,
3925 issue: &str,
3926 after: &str,
3927 ) -> Result<Option<Value>, SourceError> {
3928 let mut after = after.to_owned();
3929 loop {
3930 let data = self
3931 .graphql(
3932 graphql::ISSUE_BOARD_ITEMS,
3933 json!({"id":issue,"first":MAX_PAGE_SIZE,"after":after,
3934 "nestedFirst":NESTED_PAGE_SIZE}),
3935 )
3936 .await?;
3937 let Some(connection) = data
3938 .pointer("/node/projectItems")
3939 .filter(|value| !value.is_null())
3940 else {
3941 // The id resolved to nothing, or to something with no memberships to walk —
3942 // which is the same answer as a connection holding no entry for this board.
3943 return Ok(None);
3944 };
3945 let nodes = connection
3946 .get("nodes")
3947 .and_then(Value::as_array)
3948 .ok_or_else(|| SourceError::Malformed {
3949 message: "GitHub issue projectItems.nodes is not an array".into(),
3950 })?;
3951 if let Some(held) = self.board_entry(nodes) {
3952 return Ok(Some(held.clone()));
3953 }
3954 let info = connection
3955 .get("pageInfo")
3956 .ok_or_else(|| SourceError::Malformed {
3957 message: "GitHub issue projectItems has no pageInfo".into(),
3958 })?;
3959 let next = required_bool(info, "hasNextPage")?
3960 .then(|| required_str(info, "endCursor"))
3961 .transpose()?;
3962 match next {
3963 Some(next) => {
3964 validate_cursor_progress(Some(&after), next)?;
3965 after = next.to_owned();
3966 }
3967 None => return Ok(None),
3968 }
3969 }
3970 }
3971
3972 /// One page of a board-scoped issue search, and where the next page resumes.
3973 async fn search_page(
3974 &self,
3975 search: &str,
3976 first: u32,
3977 after: Option<&str>,
3978 ) -> Result<(Vec<Resolved>, Option<String>), SourceError> {
3979 let data = self
3980 .graphql(
3981 graphql::SEARCH_ISSUES,
3982 json!({"search":search,"type":"ISSUE","first":first.min(MAX_PAGE_SIZE),
3983 "after":after,"nestedFirst":NESTED_PAGE_SIZE,
3984 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
3985 )
3986 .await?;
3987 let connection = data.get("search").ok_or_else(|| SourceError::Malformed {
3988 message: "GitHub search response has no search connection".into(),
3989 })?;
3990 let mut found = Vec::new();
3991 for node in connection
3992 .get("nodes")
3993 .and_then(Value::as_array)
3994 .ok_or_else(|| SourceError::Malformed {
3995 message: "GitHub search nodes is not an array".into(),
3996 })?
3997 {
3998 if let Some(resolved) = self.resolve_issue(node).await? {
3999 found.push(resolved);
4000 }
4001 }
4002 let info = connection
4003 .get("pageInfo")
4004 .ok_or_else(|| SourceError::Malformed {
4005 message: "GitHub search connection has no pageInfo".into(),
4006 })?;
4007 let next = required_bool(info, "hasNextPage")?
4008 .then(|| required_str(info, "endCursor"))
4009 .transpose()?
4010 .map(str::to_owned);
4011 if let Some(next) = &next {
4012 validate_cursor_progress(after, next)?;
4013 }
4014 Ok((found, next))
4015 }
4016
4017 /// Every issue this board holds, completed with what this run wrote.
4018 ///
4019 /// The completion is not an optimisation and it is not a cache: GitHub's issue search
4020 /// is an index and is eventually consistent, so an issue this run created seconds ago
4021 /// can be absent from it, and a project listed straight after being written would
4022 /// otherwise be missing from its own board. What is added back is only what this
4023 /// process itself wrote, out of [`Self::created`], which lives and dies with the
4024 /// process.
4025 async fn board_issues(&self) -> Result<Vec<Resolved>, SourceError> {
4026 let found = self.searched_issues().await?;
4027 self.completed_with_written(found, |_| true)
4028 }
4029
4030 /// Every issue this board's own search reports, walked to exhaustion, read once per
4031 /// source.
4032 ///
4033 /// The uncompleted half of [`Self::board_issues`], separated because [`Self::board`]
4034 /// needs it too and the two would otherwise walk the same search twice in one command.
4035 /// See [`Self::search_cache`] for why holding it is the same bargain holding the board
4036 /// is.
4037 async fn searched_issues(&self) -> Result<Vec<Resolved>, SourceError> {
4038 let cached = self.search_cache()?.clone();
4039 if let Some(held) = cached {
4040 return Ok(held);
4041 }
4042 let mut after: Option<String> = None;
4043 let mut found = Vec::new();
4044 let search = self.board_search(None);
4045 loop {
4046 let (page, next) = self
4047 .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
4048 .await?;
4049 found.extend(page);
4050 match next {
4051 Some(next) => after = Some(next),
4052 None => break,
4053 }
4054 }
4055 *self.search_cache()? = Some(found.clone());
4056 Ok(found)
4057 }
4058
4059 /// This process's own view of the board's issues, or the refusal a poisoned lock is.
4060 fn search_cache(
4061 &self,
4062 ) -> Result<std::sync::MutexGuard<'_, Option<Vec<Resolved>>>, SourceError> {
4063 self.search_cache
4064 .lock()
4065 .map_err(|_| SourceError::Unavailable {
4066 message: "this source's view of the board's issues was left inconsistent by an \
4067 earlier failure; next: run the command again"
4068 .into(),
4069 })
4070 }
4071
4072 /// `found`, with everything this run wrote that `keep` accepts and the read did not
4073 /// report.
4074 ///
4075 /// See [`Self::created`] and [`Self::board_issues`] for why a read has to be completed
4076 /// at all: the search index is behind, and a node read of an item filed moments ago can
4077 /// be too.
4078 fn completed_with_written(
4079 &self,
4080 mut found: Vec<Resolved>,
4081 keep: impl Fn(&Resolved) -> bool,
4082 ) -> Result<Vec<Resolved>, SourceError> {
4083 for own in self.created()?.iter().filter(|own| keep(own)) {
4084 if !found.iter().any(|item| item.id == own.id) {
4085 found.push(own.clone());
4086 }
4087 }
4088 Ok(found)
4089 }
4090
4091 /// What resolving one node id reached.
4092 ///
4093 /// Three answers rather than an `Option`, because a board *draft* is none of the other
4094 /// two: it is not an issue, so the issue fragment reads nothing of it, and a read of one
4095 /// is completed by a read of the draft itself rather than reported as nothing.
4096 async fn reach(&self, id: &NativeId) -> Result<Reached, SourceError> {
4097 let asked = self
4098 .graphql(
4099 graphql::ISSUE,
4100 json!({"id":id.0,"first":MAX_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
4101 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
4102 )
4103 .await;
4104 let data = match asked {
4105 Ok(data) => data,
4106 // A string that is not a node id at all is not a failure to report: it is an id
4107 // this board does not hold, which is what every read of one already answers.
4108 Err(error) if unresolvable_node(&error) => return Ok(Reached::Nothing),
4109 Err(error) => return Err(error),
4110 };
4111 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
4112 return Ok(Reached::Nothing);
4113 };
4114 if optional_str(node, "__typename")? == Some("DraftIssue") {
4115 return Ok(Reached::Draft);
4116 }
4117 Ok(match self.resolve_issue(node).await? {
4118 Some(item) => Reached::Held(Box::new(item)),
4119 None => Reached::Nothing,
4120 })
4121 }
4122
4123 /// One item of this board by its own id, whatever kind it is.
4124 ///
4125 /// Resolved from the identifier alone: no search, board-wide or otherwise. What this
4126 /// run wrote is read first, because a node read of an item created moments ago can
4127 /// still be behind the board field values written onto it — see [`Self::created`].
4128 async fn item_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
4129 if let Some(own) = self.created()?.iter().find(|own| own.id == *id) {
4130 return Ok(Some(own.clone()));
4131 }
4132 match self.reach(id).await? {
4133 Reached::Held(item) => Ok(Some(*item)),
4134 Reached::Nothing => Ok(None),
4135 Reached::Draft => self.draft_by_id(id).await,
4136 }
4137 }
4138
4139 /// Several items of this board, each by its own id, in order — what [`Self::item_by_id`]
4140 /// answers for each, read [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] rather
4141 /// than one request per id.
4142 ///
4143 /// What this run wrote answers first, as it does there, and only the rest is read. One id
4144 /// left to read is read by [`Self::item_by_id`] itself, which costs what a batch does. A
4145 /// batch GitHub refuses because one of its ids resolves to no node at all is read again one
4146 /// id at a time, so that id is answered as not held and the others as themselves; a draft
4147 /// is completed by a read of the draft, exactly as there.
4148 async fn items_by_ids(&self, ids: &[NativeId]) -> Result<Vec<Option<Resolved>>, SourceError> {
4149 let mut found: Vec<Option<Option<Resolved>>> = {
4150 let created = self.created()?;
4151 ids.iter()
4152 .map(|id| {
4153 created
4154 .iter()
4155 .find(|own| own.id == *id)
4156 .map(|own| Some(own.clone()))
4157 })
4158 .collect()
4159 };
4160 let unread: Vec<NativeId> = ids
4161 .iter()
4162 .zip(&found)
4163 .filter(|(_, found)| found.is_none())
4164 .map(|(id, _)| id.clone())
4165 .collect();
4166 let mut read = Vec::with_capacity(unread.len());
4167 if let [one] = unread.as_slice() {
4168 read.push(self.item_by_id(one).await?);
4169 } else {
4170 for batch in unread.chunks(DETAIL_BATCH) {
4171 let data = match self
4172 .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, None))
4173 .await
4174 {
4175 Ok(data) => data,
4176 Err(error) if unresolvable_node(&error) => {
4177 for id in batch {
4178 read.push(self.item_by_id(id).await?);
4179 }
4180 continue;
4181 }
4182 Err(error) => return Err(error),
4183 };
4184 for (slot, id) in batch.iter().enumerate() {
4185 let node =
4186 data.get(format!("i{slot}"))
4187 .ok_or_else(|| SourceError::Malformed {
4188 message: format!(
4189 "GitHub answered a batch read with no item for {}",
4190 id.0
4191 ),
4192 })?;
4193 read.push(if node.is_null() {
4194 None
4195 } else if optional_str(node, "__typename")? == Some("DraftIssue") {
4196 self.draft_by_id(id).await?
4197 } else {
4198 if optional_str(node, "__typename")? == Some("Issue")
4199 && required_str(node, "id")? != id.0
4200 {
4201 return Err(SourceError::Malformed {
4202 message: format!(
4203 "GitHub answered the read of {} with issue {}",
4204 id.0,
4205 required_str(node, "id")?
4206 ),
4207 });
4208 }
4209 self.resolve_issue(node).await?
4210 });
4211 }
4212 }
4213 }
4214 let mut read = read.into_iter();
4215 Ok(found
4216 .iter_mut()
4217 .map(|slot| slot.take().unwrap_or_else(|| read.next().flatten()))
4218 .collect())
4219 }
4220
4221 fn resolved_cache(
4222 &self,
4223 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
4224 self.resolved_cache
4225 .lock()
4226 .map_err(|_| SourceError::Unavailable {
4227 message: "resolved item records were left inconsistent; run the command again"
4228 .into(),
4229 })
4230 }
4231
4232 /// Reuse a record this invocation already resolved. The mutation sender invalidates
4233 /// it before writing, so a partial failure cannot leave a pre-write binding behind.
4234 async fn bound_item(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
4235 let cached = self.resolved_cache()?.get(id).cloned();
4236 match cached {
4237 Some(item) => Ok(Some(item)),
4238 None => self.item_by_id(id).await,
4239 }
4240 }
4241
4242 /// One board draft by its own id, with the board item it sits in — or `None` when no
4243 /// item of this board is that draft's.
4244 ///
4245 /// The same decision [`Self::resolve_issue`] makes for an issue, over the draft's own
4246 /// `projectV2Items`: an entry naming this board is what makes it this board's. GitHub
4247 /// links a draft to one board item, so the page this read carries is the whole of that
4248 /// connection, and a page that reports more than it holds is refused rather than read
4249 /// as an answer about memberships nobody read.
4250 async fn draft_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
4251 let data = self
4252 .graphql(
4253 graphql::DRAFT,
4254 json!({"id":id.0,"nestedFirst":NESTED_PAGE_SIZE,
4255 "boardItems":BOARD_ITEMS_PAGE_SIZE}),
4256 )
4257 .await?;
4258 // Gone between the two reads is an answer — the draft is no longer there. Anything
4259 // else than the draft [`Self::reach`] was just told this id is, is not one.
4260 let Some(draft) = data.get("node").filter(|node| !node.is_null()) else {
4261 return Ok(None);
4262 };
4263 if optional_str(draft, "__typename")? != Some("DraftIssue") {
4264 return Err(SourceError::Malformed {
4265 message: format!(
4266 "GitHub answered {} as a draft and then as something else",
4267 id.0
4268 ),
4269 });
4270 }
4271 if required_str(draft, "id")? != id.0 {
4272 return Err(SourceError::Malformed {
4273 message: format!("GitHub answered a different draft for {}", id.0),
4274 });
4275 }
4276 let memberships = draft
4277 .get("projectV2Items")
4278 .ok_or_else(|| SourceError::Malformed {
4279 message: format!("GitHub draft {} is missing projectV2Items", id.0),
4280 })?;
4281 let nodes = memberships
4282 .get("nodes")
4283 .and_then(Value::as_array)
4284 .ok_or_else(|| SourceError::Malformed {
4285 message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
4286 })?;
4287 let info = memberships
4288 .get("pageInfo")
4289 .ok_or_else(|| SourceError::Malformed {
4290 message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
4291 })?;
4292 // Read whether or not this board's entry is on the page: a page claiming more than
4293 // the one item GitHub links a draft to is a malformed answer either way.
4294 if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
4295 return Err(SourceError::Malformed {
4296 message: format!(
4297 "GitHub draft {} reports more board items than the one GitHub links a draft \
4298 to",
4299 id.0
4300 ),
4301 });
4302 }
4303 if let Some(node) = nodes.first()
4304 && node
4305 .pointer("/project/number")
4306 .and_then(Value::as_u64)
4307 .is_none()
4308 {
4309 return Err(SourceError::Malformed {
4310 message: format!(
4311 "GitHub draft {} board item has no numeric project number",
4312 id.0
4313 ),
4314 });
4315 }
4316 let Some(held) = self.board_entry(nodes) else {
4317 return Ok(None);
4318 };
4319 if required_str(
4320 held.get("project").ok_or_else(|| SourceError::Malformed {
4321 message: format!("GitHub draft {} board item has no project", id.0),
4322 })?,
4323 "id",
4324 )? != self.board_fields().await?.id.as_str()
4325 {
4326 return Ok(None);
4327 }
4328 let item = json!({
4329 "id": required_str(held, "id")?,
4330 "project": held.get("project"),
4331 "fieldValues": held.get("fieldValues"),
4332 "content": draft,
4333 });
4334 self.resolve(&item)
4335 }
4336
4337 /// The board's own id and field definitions, for a write whose item does not carry
4338 /// them — never its items.
4339 ///
4340 /// A board this command has already listed supplies them, since it read them beside its
4341 /// items; otherwise they come from [`graphql::BOARD_FIELDS`], once per command. Neither
4342 /// is consulted about which items the board holds: see the module documentation for
4343 /// why a question about one known item is answered by reading that item.
4344 async fn board_fields(&self) -> Result<BoardFields, SourceError> {
4345 if let Some(board) = self.board_cache()?.as_ref() {
4346 return Ok(BoardFields {
4347 id: BoardId::parse(&board.id)?,
4348 fields: board.fields.clone(),
4349 });
4350 }
4351 if let Some(held) = self.fields_cache()?.clone() {
4352 return Ok(held);
4353 }
4354 let data = self
4355 .graphql(
4356 graphql::BOARD_FIELDS,
4357 json!({"owner":self.owner,"number":self.project_number,
4358 "nestedFirst":NESTED_PAGE_SIZE}),
4359 )
4360 .await?;
4361 self.fields_read(&data)
4362 }
4363
4364 /// The board's id and fields out of an answer carrying the `boardFields` root, held for
4365 /// the rest of this command.
4366 fn fields_read(&self, data: &Value) -> Result<BoardFields, SourceError> {
4367 let board = data
4368 .pointer("/boardFields/projectV2")
4369 .filter(|value| !value.is_null())
4370 .ok_or_else(|| SourceError::Refused {
4371 message: format!(
4372 "GitHub project {}/{} was not found or is not visible to the token",
4373 self.owner, self.project_number
4374 ),
4375 })?;
4376 let read = BoardFields {
4377 id: BoardId::parse(required_str(board, "id")?)?,
4378 fields: board.get("fields").cloned().unwrap_or(Value::Null),
4379 };
4380 *self.fields_cache()? = Some(read.clone());
4381 Ok(read)
4382 }
4383
4384 /// Read what creating an issue in `repository` needs and this command has not read yet —
4385 /// the board's fields and the repository's node id — in one request when it needs both.
4386 ///
4387 /// When either is already known this sends nothing, and the other is read by its own
4388 /// document where it is asked for, so no create reads anything twice.
4389 async fn creation_context(
4390 &self,
4391 repository: &RepositoryTarget,
4392 incoming: &Incoming<'_>,
4393 ) -> Result<(), SourceError> {
4394 let fields_known = self.board_cache()?.is_some() || self.fields_cache()?.is_some();
4395 if fields_known || self.repository_cache()?.contains_key(repository) {
4396 return Ok(());
4397 }
4398 let data = self
4399 .graphql(
4400 graphql::CREATION_CONTEXT,
4401 json!({"owner":self.owner,"number":self.project_number,
4402 "nestedFirst":NESTED_PAGE_SIZE,"repositoryOwner":repository.owner,
4403 "repositoryName":repository.name}),
4404 )
4405 .await?;
4406 self.fields_read(&data)?;
4407 self.repository_read(&data, repository, incoming)?;
4408 Ok(())
4409 }
4410
4411 /// This process's own view of the board's fields, or the refusal a poisoned lock is.
4412 fn fields_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<BoardFields>>, SourceError> {
4413 self.fields_cache
4414 .lock()
4415 .map_err(|_| SourceError::Unavailable {
4416 message: "this source's view of the board's fields was left inconsistent by an \
4417 earlier failure; next: run the command again"
4418 .into(),
4419 })
4420 }
4421
4422 /// What a write to `item` needs of the board, read off that item when it says enough and
4423 /// off [`Self::board_fields`] when it does not.
4424 ///
4425 /// A node read of an item names its board and carries the definition of every field it
4426 /// holds a value of — so an item naming its board, holding a value of the origin field,
4427 /// and, when the write carries a status, holding a `Status` value, needs no read of the
4428 /// board at all. **Nothing the item does not say is guessed:** a field it holds no value
4429 /// of may still be on the board, and a view reading it as absent would refuse a write the
4430 /// board can take or skip a field write the board needs, so such an item — and a create,
4431 /// which has no item yet — takes the board's fields from their own read instead.
4432 async fn fields_for(
4433 &self,
4434 item: Option<&Resolved>,
4435 writes_status: bool,
4436 selects_priority: bool,
4437 ) -> Result<BoardFields, SourceError> {
4438 if let Some(board) = item.and_then(Resolved::carried_board) {
4439 return Ok(board);
4440 }
4441 if let Some(item) = item
4442 && let Some(board_id) = item.named_board()
4443 && item.defines(ORIGIN_FIELD)
4444 && (!writes_status || item.defines("Status"))
4445 && (!selects_priority || item.defines(PRIORITY_FIELD))
4446 {
4447 return Ok(BoardFields {
4448 id: board_id,
4449 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
4450 });
4451 }
4452 self.board_fields().await
4453 }
4454
4455 /// Everything filed under one issue of this board, walked to exhaustion — or `None`
4456 /// when that id names nothing here with a sub-issue relationship to walk.
4457 ///
4458 /// `None` and an empty answer are different: `None` is *this is not an issue of this
4459 /// GitHub*, which is what sends a project selector on to be read as a name, and an
4460 /// empty vector is a project that holds nothing.
4461 async fn sub_issues(&self, id: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
4462 let mut after: Option<String> = None;
4463 let mut children = Vec::new();
4464 loop {
4465 let asked = self
4466 .graphql(
4467 graphql::SUB_ISSUES,
4468 json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after,
4469 "nestedFirst":NESTED_PAGE_SIZE,
4470 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
4471 )
4472 .await;
4473 let data = match asked {
4474 Ok(data) => data,
4475 // A string that is not a node id at all is not a failure to report: it is
4476 // the ordinary answer to a selector naming a project by its name.
4477 Err(error) if unresolvable_node(&error) => return Ok(None),
4478 Err(error) => return Err(error),
4479 };
4480 let Some(connection) = data
4481 .pointer("/node/subIssues")
4482 .filter(|value| !value.is_null())
4483 else {
4484 // No such node, or one with no sub-issue relationship — a board draft is
4485 // the one this board can really hold.
4486 return Ok(None);
4487 };
4488 for node in connection
4489 .get("nodes")
4490 .and_then(Value::as_array)
4491 .ok_or_else(|| SourceError::Malformed {
4492 message: "GitHub subIssues.nodes is not an array".into(),
4493 })?
4494 {
4495 if let Some(resolved) = self.resolve_issue(node).await? {
4496 children.push(resolved);
4497 }
4498 }
4499 let info = connection
4500 .get("pageInfo")
4501 .ok_or_else(|| SourceError::Malformed {
4502 message: "GitHub subIssues connection has no pageInfo".into(),
4503 })?;
4504 let next = required_bool(info, "hasNextPage")?
4505 .then(|| required_str(info, "endCursor"))
4506 .transpose()?;
4507 match next {
4508 Some(next) => {
4509 validate_cursor_progress(after.as_deref(), next)?;
4510 after = Some(next.to_owned());
4511 }
4512 None => return Ok(Some(children)),
4513 }
4514 }
4515 }
4516
4517 /// Which issue of this board a project *name* is, or `None` when none is.
4518 ///
4519 /// One bounded query which filters on that name at the server, rather than a walk of
4520 /// every issue the board holds. The name is compared again here: the qualifier narrows
4521 /// what GitHub sends, and this source decides what it names.
4522 async fn project_by_name(&self, name: &str) -> Result<Option<NativeId>, SourceError> {
4523 let search = self.board_search(Some(&title_qualifier(name)));
4524 let mut after = None;
4525 loop {
4526 let (candidates, next) = self
4527 .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
4528 .await?;
4529 if let Some(item) = candidates.into_iter().find(|item| {
4530 item.kind == BoardKind::Work(ItemKind::Project)
4531 && item.title.eq_ignore_ascii_case(name)
4532 }) {
4533 return Ok(Some(item.id));
4534 }
4535 match next {
4536 Some(next) => after = Some(next),
4537 None => return Ok(None),
4538 }
4539 }
4540 }
4541
4542 /// Everything filed under one project of this board: the sub-issues of the issue that
4543 /// project is.
4544 ///
4545 /// Tasks *and* documents, because a document filed under a project is a sub-issue of it
4546 /// too — the caller keeps the kind it asked for. Nothing about this grows as the board
4547 /// gains projects, or as another project gains tasks.
4548 ///
4549 /// A qualified id names the issue and is asked for its sub-issues directly: one
4550 /// request, no search of any kind. Only a selector GitHub cannot resolve that way is
4551 /// read as a project *name*, which costs the one bounded search
4552 /// [`Self::project_by_name`] makes.
4553 async fn project_children(&self, selector: &NativeId) -> Result<Vec<Resolved>, SourceError> {
4554 let (project, children) = match self.sub_issues(selector).await? {
4555 Some(children) => (selector.clone(), children),
4556 None => match self.project_by_name(&selector.0).await? {
4557 Some(project) => {
4558 let children = self.sub_issues(&project).await?.unwrap_or_default();
4559 (project, children)
4560 }
4561 None => return Ok(Vec::new()),
4562 },
4563 };
4564 self.completed_with_written(children, |own| own.parent.as_ref() == Some(&project))
4565 }
4566
4567 /// Every issue of this board GitHub's issue search reports updated at or after `since`,
4568 /// completed with what this run wrote — the candidates a comment-activity read confirms.
4569 ///
4570 /// Scoped by the board and by nothing else: `project:<owner>/<number>` reaches every issue
4571 /// on the board whatever repository, and whatever owner, it lives in, so no repository or
4572 /// owner qualifier is added and none is needed. What makes the `updated:` qualifier
4573 /// sufficient is a fact about GitHub rather than about this source: a comment written on an
4574 /// issue **and a comment edited on it** both move that issue's `updatedAt`. The credentialed
4575 /// journey `an_edited_comment_moves_its_issue_and_is_selected_since` in `tests/journey`
4576 /// re-takes that fact on every run of the lane, so a change on GitHub's side fails there
4577 /// rather than silently narrowing a caller's answer.
4578 ///
4579 /// The instant is written to the second, rounded down, which can only widen what the
4580 /// search returns; confirmation against each candidate's own comments is what makes the
4581 /// answer exact. The search is an index that lags a write by a second or two — the module
4582 /// documentation records it — so a caller that asks again from its last instant should
4583 /// overlap the two by more than that.
4584 async fn updated_since(&self, since: DateTime<Utc>) -> Result<Vec<Resolved>, SourceError> {
4585 let found = self.searched(&updated_qualifier(since)).await?;
4586 self.completed_with_written(found, |_| true)
4587 }
4588
4589 /// Every issue of this board GitHub's issue search reports for the board-scoped search
4590 /// narrowed by `also`, in pages of [`SEARCH_PAGE_SIZE`].
4591 ///
4592 /// Uncompleted: what this process wrote is added by the caller, which knows whether its
4593 /// own record is the fresher of the two.
4594 async fn searched(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
4595 let search = self.board_search(Some(also));
4596 let mut after: Option<String> = None;
4597 let mut found = Vec::new();
4598 loop {
4599 let (page, next) = self
4600 .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
4601 .await?;
4602 found.extend(page);
4603 match next {
4604 Some(next) => after = Some(next),
4605 None => return Ok(found),
4606 }
4607 }
4608 }
4609
4610 /// A bounded task answer; the versioned cursor carries the connection position, how
4611 /// many rows of the page starting there were already handed out, and the own-write ids
4612 /// already observed, including across a new source instance.
4613 ///
4614 /// Every page is sent at [`SEARCH_PAGE_SIZE`] whatever the caller's limit, and a limit is
4615 /// sliced from the pages it needs; why is the module documentation's paging contract.
4616 async fn search_tasks(
4617 &self,
4618 query: &TaskQuery,
4619 page: &PageRequest,
4620 also: &str,
4621 ) -> Result<Page<Task>, SourceError> {
4622 let mut position = match &page.cursor {
4623 None => SearchPosition::default(),
4624 Some(cursor) => serde_json::from_str::<SearchPosition>(&cursor.0)
4625 .ok()
4626 .filter(|position| {
4627 position.version == SEARCH_CURSOR_VERSION
4628 && position.connection.valid_resume(position.offset)
4629 })
4630 .ok_or_else(|| SourceError::Config {
4631 message: "page cursor is invalid".into(),
4632 })?,
4633 };
4634 let search = self.board_search(Some(also));
4635 let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
4636 let own = self.with_own_writes(Vec::new())?;
4637 // An issue this process commented on is a candidate of a comment-activity read
4638 // whether or not the search has caught up with the comment; see `Self::commented`.
4639 let commented = match query.commented_since {
4640 Some(_) => self.commented()?.clone(),
4641 None => Vec::new(),
4642 };
4643 for id in own.iter().map(|item| &item.id).chain(&commented) {
4644 if !position.own.contains(id) {
4645 position.own.push(id.clone());
4646 }
4647 }
4648 let mut tasks = Vec::new();
4649 while !position.connection.exhausted() && tasks.len() < limit {
4650 let first = SEARCH_PAGE_SIZE;
4651 // Page size is part of the key: a short cached answer cannot answer a wider ask.
4652 let key =
4653 serde_json::to_string(&("page", &search, &position.connection.after(), first))
4654 .expect("search page key is serializable");
4655 let cached = if query.commented_since.is_none() {
4656 self.narrowed_cache()?.get(&key).cloned()
4657 } else {
4658 None
4659 };
4660 let (found, next) = match cached {
4661 Some(found) => {
4662 let next = self
4663 .search_next
4664 .lock()
4665 .map_err(|_| SourceError::Unavailable {
4666 message:
4667 "search pagination was left inconsistent; run the command again"
4668 .into(),
4669 })?
4670 .get(&key)
4671 .cloned()
4672 .flatten();
4673 (found, next)
4674 }
4675 None => {
4676 let (found, next) = self
4677 .search_page(&search, first, position.connection.after())
4678 .await?;
4679 if query.commented_since.is_none() {
4680 self.search_next
4681 .lock()
4682 .map_err(|_| SourceError::Unavailable {
4683 message:
4684 "search pagination was left inconsistent; run the command again"
4685 .into(),
4686 })?
4687 .insert(key.clone(), next.clone());
4688 self.narrowed_cache()?.insert(key, found.clone());
4689 }
4690 (found, next)
4691 }
4692 };
4693 let rows = found.len();
4694 for mut item in found.into_iter().skip(position.offset) {
4695 if tasks.len() == limit {
4696 break;
4697 }
4698 position.offset += 1;
4699 if position.own.contains(&item.id) {
4700 if position.seen.contains(&item.id) {
4701 continue;
4702 }
4703 position.seen.push(item.id.clone());
4704 // The search's own copy of an issue this process only commented on is as
4705 // good as a node read of it, since its comments are read either way.
4706 let only_commented = commented.contains(&item.id)
4707 && !own.iter().any(|written| written.id == item.id);
4708 if !only_commented {
4709 let updated_at = item.updated_at;
4710 let Some(written) = self.search_written(&own, &item.id).await? else {
4711 continue;
4712 };
4713 item = written;
4714 item.updated_at = item.updated_at.max(updated_at);
4715 self.resolved_cache()?.insert(item.id.clone(), item.clone());
4716 }
4717 }
4718 if item.kind == BoardKind::Work(ItemKind::Task) {
4719 let task = item.task()?;
4720 if task_matches(&task, query, &query.project)
4721 && self.commented_since(&item, query.commented_since).await?
4722 {
4723 tasks.push(task);
4724 }
4725 }
4726 }
4727 if position.offset < rows {
4728 continue;
4729 }
4730 position.offset = 0;
4731 position.connection = match next {
4732 Some(after) => SearchConnection::Continuing {
4733 after: Cursor(after),
4734 },
4735 None => SearchConnection::Exhausted {},
4736 };
4737 }
4738 if position.connection.exhausted() {
4739 for id in position.own.clone() {
4740 if position.seen.contains(&id) {
4741 continue;
4742 }
4743 if tasks.len() == limit {
4744 break;
4745 }
4746 position.seen.push(id.clone());
4747 let Some(item) = self.search_written(&own, &id).await? else {
4748 continue;
4749 };
4750 if item.kind == BoardKind::Work(ItemKind::Task) {
4751 let task = item.task()?;
4752 if task_matches(&task, query, &query.project)
4753 && self.commented_since(&item, query.commented_since).await?
4754 {
4755 tasks.push(task);
4756 }
4757 }
4758 }
4759 }
4760 let more = !position.connection.exhausted()
4761 || position.own.iter().any(|id| !position.seen.contains(id));
4762 Ok(Page {
4763 items: tasks,
4764 next: more.then(|| {
4765 Cursor(serde_json::to_string(&position).expect("search position is serializable"))
4766 }),
4767 })
4768 }
4769
4770 /// A resumed process has the ids but no write records; resolve only a record the
4771 /// current page needs, by its uncached node read rather than the lagging search index.
4772 async fn search_written(
4773 &self,
4774 own: &[Resolved],
4775 id: &NativeId,
4776 ) -> Result<Option<Resolved>, SourceError> {
4777 match own.iter().find(|item| item.id == *id) {
4778 Some(item) => Ok(Some(item.clone())),
4779 None => self.item_by_id(id).await,
4780 }
4781 }
4782
4783 /// The candidates for a task query carrying a text, metadata or origin predicate, read
4784 /// without enumerating the board — or `None` for a query carrying none of the three, which
4785 /// keeps the reads it always had.
4786 ///
4787 /// An origin is answered by [`Self::origin_candidates`], whatever else the query carries,
4788 /// because it names at most a handful of items. Text and metadata are answered by one
4789 /// board-scoped issue search carrying every term — see [`narrowing_qualifiers`] — narrowed
4790 /// further by `updated:>=` when the query also asks for comment activity, since both
4791 /// qualifiers must hold of an issue the answer keeps. Every candidate is confirmed in
4792 /// process afterwards by the same predicates [`task_matches`] applies to every read.
4793 ///
4794 /// Completed with what this process wrote, its own record winning over the index's copy
4795 /// of the same item: see [`Self::with_own_writes`].
4796 async fn narrowed(&self, query: &TaskQuery) -> Result<Option<Vec<Resolved>>, SourceError> {
4797 let asked = match (&query.origin, narrowing_qualifiers(query)) {
4798 (Some(origin), _) => Narrowing::Origin(origin.clone()),
4799 (None, Some(qualifiers)) => Narrowing::Search(match query.commented_since {
4800 Some(since) => format!("{} {qualifiers}", updated_qualifier(since)),
4801 None => qualifiers,
4802 }),
4803 (None, None) => return Ok(None),
4804 };
4805 // A question about comment activity is asked afresh every time, as it always was: it
4806 // is the one a caller polls from one source while waiting for the index, and an
4807 // answer held from the first poll would be the answer to every later one.
4808 let key = query.commented_since.is_none().then(|| asked.key());
4809 let cached = match &key {
4810 Some(key) => self.narrowed_cache()?.get(key).cloned(),
4811 None => None,
4812 };
4813 let found = match cached {
4814 Some(found) => found,
4815 None => {
4816 let found = match &asked {
4817 Narrowing::Origin(origin) => self.origin_candidates(origin).await?,
4818 Narrowing::Search(also) => self.searched(also).await?,
4819 };
4820 if let Some(key) = key {
4821 self.narrowed_cache()?.insert(key, found.clone());
4822 }
4823 found
4824 }
4825 };
4826 self.with_own_writes(found).map(Some)
4827 }
4828
4829 /// The candidates for a project or unscoped document query carrying a searchable text,
4830 /// read without enumerating the board — or `None` for a query with no text or a blank one,
4831 /// which keeps the read it always had.
4832 ///
4833 /// The text is sent as the very phrase a task query's text is — see [`text_qualifiers`] —
4834 /// in one board-scoped issue search walked to its end at [`SEARCH_PAGE_SIZE`], so what it
4835 /// costs is the issues that match and never the board. Its answer is held for the command
4836 /// under the same key [`Self::narrowed`] holds that search under, so a walk of the caller's
4837 /// pages asks GitHub once. Every candidate is confirmed afterwards by its kind and by the
4838 /// substring rule, exactly as an item of the wider read was, and is completed with what this
4839 /// process wrote: see [`Self::with_own_writes`].
4840 async fn text_searched(
4841 &self,
4842 text: Option<&TextQuery>,
4843 ) -> Result<Option<Vec<Resolved>>, SourceError> {
4844 let Some(also) = text_qualifiers(text) else {
4845 return Ok(None);
4846 };
4847 let key = Narrowing::Search(also.clone()).key();
4848 let cached = self.narrowed_cache()?.get(&key).cloned();
4849 let found = match cached {
4850 Some(found) => found,
4851 None => {
4852 let found = self.searched(&also).await?;
4853 self.narrowed_cache()?.insert(key, found.clone());
4854 found
4855 }
4856 };
4857 self.with_own_writes(found).map(Some)
4858 }
4859
4860 /// Every item of this board that may carry `origin` — a superset of those that do — found
4861 /// by [`graphql::ORIGIN_LOOKUP`] and never by enumerating the board.
4862 ///
4863 /// The union of the board's own field filter over the `onetaskgraph.origin` text field —
4864 /// which reads the field every carrier holds, whichever release wrote it — and the
4865 /// board-scoped issue search for the same id as a phrase in the body, where this source
4866 /// mirrors it. The caller adds the third read, this process's own writes. Candidates are
4867 /// returned unconfirmed; [`task_matches`] compares each one's own origin field with the
4868 /// query's, exactly.
4869 ///
4870 /// Both connections are walked to exhaustion, each from its own cursor. One that has
4871 /// already ended is sent its last cursor again, which answers an empty page, so the one
4872 /// document serves every page of either. What the two leave is stated in the module
4873 /// documentation: a carrier another process added within the last second or two, before
4874 /// either index has it.
4875 async fn origin_candidates(&self, origin: &str) -> Result<Vec<Resolved>, SourceError> {
4876 let filter = format!("{ORIGIN_FIELD}:{}", quoted(origin));
4877 let search = self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))));
4878 let mut items_after: Option<String> = None;
4879 let mut search_after: Option<String> = None;
4880 let mut found: Vec<Resolved> = Vec::new();
4881 let keep = |resolved: Resolved, found: &mut Vec<Resolved>| {
4882 if !found.iter().any(|held| held.id == resolved.id) {
4883 found.push(resolved);
4884 }
4885 };
4886 loop {
4887 let data = self
4888 .graphql(
4889 graphql::ORIGIN_LOOKUP,
4890 json!({"owner":self.owner,"number":self.project_number,"filter":filter,
4891 "search":search,"type":"ISSUE","originFirst":ORIGIN_PAGE_SIZE,
4892 "itemsAfter":items_after,"searchAfter":search_after,
4893 "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
4894 "duplicates":true}),
4895 )
4896 .await?;
4897 let items = data
4898 .pointer("/originItems/projectV2/items")
4899 .filter(|value| !value.is_null())
4900 .ok_or_else(|| SourceError::Refused {
4901 message: format!(
4902 "GitHub project {}/{} was not found or is not visible to the token",
4903 self.owner, self.project_number
4904 ),
4905 })?;
4906 for item in optional_nodes(Some(items), "project items")?
4907 .into_iter()
4908 .flatten()
4909 {
4910 // The board's own items list its drafts too, and a draft is not an issue: no
4911 // narrowed read answers with one, whatever its origin field holds.
4912 if let Some(resolved) = self.resolve(item)?
4913 && resolved.content_kind == ContentKind::Issue
4914 {
4915 keep(resolved, &mut found);
4916 }
4917 }
4918 let searched = data.get("search").ok_or_else(|| SourceError::Malformed {
4919 message: "GitHub search response has no search connection".into(),
4920 })?;
4921 for node in optional_nodes(Some(searched), "search")?
4922 .into_iter()
4923 .flatten()
4924 {
4925 if let Some(resolved) = self.resolve_issue(node).await? {
4926 keep(resolved, &mut found);
4927 }
4928 }
4929 let items_next = resumed(items, items_after.as_deref())?;
4930 let search_next = resumed(searched, search_after.as_deref())?;
4931 if !items_next.has_more() && !search_next.has_more() {
4932 return Ok(found);
4933 }
4934 items_after = items_next.cursor();
4935 search_after = search_next.cursor();
4936 }
4937 }
4938
4939 /// `found`, with every item this process created or wrote in its place, and every one of
4940 /// them the read did not report added.
4941 ///
4942 /// This process's own record wins over the read's copy of the same item, because a read
4943 /// of an item written moments ago can still be behind what was written onto it — the
4944 /// origin field included, which is the one a narrowed read is confirmed against — and a
4945 /// read that still names an item under a predicate this process's write moved it out of
4946 /// must not return it. The one thing the read knows that the record cannot is when GitHub
4947 /// last saw the item change, which is what a comment-activity read rules a candidate out
4948 /// by, so the read's `updatedAt` is kept when the record has none of its own. See
4949 /// [`Self::created`] and [`Self::updated`](GitHubProjectsSource::updated).
4950 fn with_own_writes(&self, mut found: Vec<Resolved>) -> Result<Vec<Resolved>, SourceError> {
4951 // A board draft is not an issue, so no narrowed read returns one, and this process
4952 // having written one does not make it an answer either.
4953 let own: Vec<Resolved> = self
4954 .created()?
4955 .iter()
4956 .chain(self.updated()?.iter())
4957 .filter(|own| own.content_kind == ContentKind::Issue)
4958 .cloned()
4959 .collect();
4960 for mut own in own {
4961 self.resolved_cache()?.insert(own.id.clone(), own.clone());
4962 match found.iter_mut().find(|read| read.id == own.id) {
4963 Some(read) => {
4964 own.updated_at = own.updated_at.max(read.updated_at);
4965 *read = own;
4966 }
4967 None => found.push(own),
4968 }
4969 }
4970 Ok(found)
4971 }
4972
4973 /// Whether `item` has a comment created or last edited at or after `since` — always, when
4974 /// there is no instant to hold it to.
4975 ///
4976 /// The candidate's own `updatedAt` is read first, because a comment written or edited at
4977 /// or after the instant moved it there: an issue not updated since holds no such comment,
4978 /// and its comments are never asked for — unless this process commented on it in this
4979 /// command, when the `updatedAt` held may predate that comment; see [`Self::commented`]. Otherwise its comments are walked, oldest first,
4980 /// only as far as the first that matches. A board draft is not an issue and has no
4981 /// comments, so it never matches.
4982 async fn commented_since(
4983 &self,
4984 item: &Resolved,
4985 since: Option<DateTime<Utc>>,
4986 ) -> Result<bool, SourceError> {
4987 let Some(since) = since else {
4988 return Ok(true);
4989 };
4990 if item.content_kind == ContentKind::DraftIssue {
4991 return Ok(false);
4992 }
4993 // An `updatedAt` this process's own record or a lagging index holds can predate a
4994 // comment this process wrote since, so only an issue it did not comment on is ruled
4995 // out by one.
4996 if item.updated_at.is_some_and(|updated| updated < since)
4997 && !self.commented()?.contains(&item.id)
4998 {
4999 return Ok(false);
5000 }
5001 let query = TaskQuery {
5002 commented_since: Some(since),
5003 ..TaskQuery::default()
5004 };
5005 let mut after: Option<String> = None;
5006 loop {
5007 let data = self
5008 .graphql(
5009 graphql::ISSUE_COMMENTS,
5010 json!({"id":item.id.0,"first":MAX_PAGE_SIZE,"after":after}),
5011 )
5012 .await?;
5013 let Some(connection) = data
5014 .get("node")
5015 .filter(|value| !value.is_null())
5016 .and_then(|node| node.get("comments"))
5017 .filter(|value| !value.is_null())
5018 else {
5019 // Removed since the search reported it: no longer an issue with comments.
5020 return Ok(false);
5021 };
5022 let comments = optional_nodes(Some(connection), "issue comments")?
5023 .into_iter()
5024 .flatten()
5025 .map(comment_from)
5026 .collect::<Result<Vec<_>, _>>()?;
5027 if query.comments_match(&comments) {
5028 return Ok(true);
5029 }
5030 match next_cursor(connection)? {
5031 Some(next) => {
5032 validate_cursor_progress(after.as_deref(), &next.0)?;
5033 after = Some(next.0);
5034 }
5035 None => return Ok(false),
5036 }
5037 }
5038 }
5039
5040 /// Every item on the board: the union of both enumerations GitHub offers of one.
5041 ///
5042 /// Neither contains the other, so neither is dropped — only `ProjectV2.items` lists a
5043 /// board **draft** and reads the board's own fields beside its items, and only the search
5044 /// reports an item that connection is behind on. The module documentation is where the lag and the
5045 /// measurements behind it are written down.
5046 ///
5047 /// A search result is admitted on the same terms as any other issue this source reaches
5048 /// directly — [`Self::resolve_issue`] keeps it only if that issue's own `projectItems`
5049 /// names *this* board — so an issue the index still believes is here after it was taken
5050 /// off is refused rather than reported.
5051 ///
5052 /// See [`Self::board_cache`]. Both completions happen on every call rather than once,
5053 /// which is what the cache could otherwise have broken.
5054 async fn board(&self) -> Result<Board, SourceError> {
5055 let cached = self.board_cache()?.clone();
5056 let mut board = match cached {
5057 Some(board) => board,
5058 None => {
5059 let read = self.read_board().await?;
5060 *self.board_cache()? = Some(read.clone());
5061 read
5062 }
5063 };
5064 for held in self.searched_issues().await? {
5065 if !board.items.iter().any(|item| item.id == held.id) {
5066 board.items.push(held);
5067 }
5068 }
5069 for own in self.created()?.iter() {
5070 if !board.items.iter().any(|item| item.id == own.id) {
5071 board.items.push(own.clone());
5072 }
5073 }
5074 Ok(board)
5075 }
5076
5077 /// This process's own view of the board, or the refusal a poisoned lock is.
5078 fn board_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<Board>>, SourceError> {
5079 self.board_cache
5080 .lock()
5081 .map_err(|_| SourceError::Unavailable {
5082 message: "this source's view of the board was left inconsistent by an earlier \
5083 failure; next: run the command again"
5084 .into(),
5085 })
5086 }
5087
5088 /// Bring this process's own view of the board up to an item it has just written.
5089 ///
5090 /// A created item goes to `created`, which is what completes a board read GitHub's own
5091 /// eventual consistency has left behind. An item that was already there is replaced
5092 /// where it sits, so a second write of it in the same command reads its real parent
5093 /// rather than the one it had before the first write.
5094 ///
5095 /// "Where it sits" is three places, and missing an earlier one leaves a stale record
5096 /// that wins: an item this same run created is held in `created` and not in the cached
5097 /// board, and `board` completes the cached board *from* `created`, so replacing only
5098 /// the cached copy of such an item replaces nothing and the read still reports the
5099 /// title it was created with. The search is the third, and it is the one an item the
5100 /// board's own projection is behind on sits in *alone* — which is exactly the item this
5101 /// source is least able to re-read, so leaving it out would put the stale title back on
5102 /// the only items the completion in [`Self::board`] exists for.
5103 fn remember_written(&self, item: Resolved, created: bool) -> Result<(), SourceError> {
5104 self.resolved_cache()?.insert(item.id.clone(), item.clone());
5105 if created {
5106 self.created()?.push(item);
5107 return Ok(());
5108 }
5109 {
5110 let mut own = self.created()?;
5111 if let Some(held) = own.iter_mut().find(|held| held.id == item.id) {
5112 *held = item;
5113 return Ok(());
5114 }
5115 }
5116 {
5117 let mut own = self.updated()?;
5118 match own.iter_mut().find(|held| held.id == item.id) {
5119 Some(held) => *held = item.clone(),
5120 None => own.push(item.clone()),
5121 }
5122 }
5123 if let Some(board) = self.board_cache()?.as_mut()
5124 && let Some(held) = board.items.iter_mut().find(|held| held.id == item.id)
5125 {
5126 *held = item.clone();
5127 }
5128 if let Some(found) = self.search_cache()?.as_mut()
5129 && let Some(held) = found.iter_mut().find(|held| held.id == item.id)
5130 {
5131 *held = item.clone();
5132 }
5133 for found in self.narrowed_cache()?.values_mut() {
5134 if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
5135 *held = item.clone();
5136 }
5137 }
5138 Ok(())
5139 }
5140
5141 /// Forget one item this process has just deleted, from every half of its own view.
5142 fn forget(&self, id: &NativeId) -> Result<(), SourceError> {
5143 self.resolved_cache()?.remove(id);
5144 self.created()?.retain(|own| own.id != *id);
5145 self.updated()?.retain(|own| own.id != *id);
5146 self.commented()?.retain(|own| own != id);
5147 if let Some(board) = self.board_cache()?.as_mut() {
5148 board.items.retain(|item| item.id != *id);
5149 }
5150 if let Some(found) = self.search_cache()?.as_mut() {
5151 found.retain(|item| item.id != *id);
5152 }
5153 for found in self.narrowed_cache()?.values_mut() {
5154 found.retain(|item| item.id != *id);
5155 }
5156 Ok(())
5157 }
5158
5159 /// This process's own record of each narrowed answer, or the refusal a poisoned lock is.
5160 fn narrowed_cache(
5161 &self,
5162 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<String, Vec<Resolved>>>, SourceError> {
5163 self.narrowed_cache
5164 .lock()
5165 .map_err(|_| SourceError::Unavailable {
5166 message: "this source's view of a narrowed read was left inconsistent by an \
5167 earlier failure; next: run the command again"
5168 .into(),
5169 })
5170 }
5171
5172 /// Every page of the board, read from GitHub.
5173 async fn read_board(&self) -> Result<Board, SourceError> {
5174 let mut after: Option<String> = None;
5175 let mut items = Vec::new();
5176 let mut board;
5177 loop {
5178 let page = self.board_page(after.as_deref(), MAX_PAGE_SIZE).await?;
5179 for item in page
5180 .pointer("/items/nodes")
5181 .and_then(Value::as_array)
5182 .ok_or_else(|| SourceError::Malformed {
5183 message: "GitHub project items.nodes is not an array".into(),
5184 })?
5185 {
5186 if let Some(resolved) = self.resolve(item)? {
5187 items.push(resolved);
5188 }
5189 }
5190 let info = page
5191 .pointer("/items/pageInfo")
5192 .ok_or_else(|| SourceError::Malformed {
5193 message: "GitHub project items have no pageInfo".into(),
5194 })?;
5195 let has_next = required_bool(info, "hasNextPage")?;
5196 let next = has_next
5197 .then(|| required_str(info, "endCursor"))
5198 .transpose()?;
5199 board = page.clone();
5200 match next {
5201 Some(next) => {
5202 validate_cursor_progress(after.as_deref(), next)?;
5203 after = Some(next.to_owned());
5204 }
5205 None => break,
5206 }
5207 }
5208 Ok(Board {
5209 id: required_str(&board, "id")?.to_owned(),
5210 fields: board.get("fields").cloned().unwrap_or(Value::Null),
5211 items,
5212 })
5213 }
5214
5215 /// The existing items this source has written, for completing a narrowed read that is
5216 /// behind; see [`Self::updated`](GitHubProjectsSource::updated).
5217 fn updated(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
5218 self.updated.lock().map_err(|_| SourceError::Unavailable {
5219 message: "this source's record of what it wrote in this run was left inconsistent \
5220 by an earlier failure; next: run the command again"
5221 .into(),
5222 })
5223 }
5224
5225 /// The issues this source has commented on in this command; see
5226 /// [`Self::commented`](GitHubProjectsSource::commented).
5227 fn commented(&self) -> Result<std::sync::MutexGuard<'_, Vec<NativeId>>, SourceError> {
5228 self.commented.lock().map_err(|_| SourceError::Unavailable {
5229 message: "this source's record of what it commented on in this run was left \
5230 inconsistent by an earlier failure; next: run the command again"
5231 .into(),
5232 })
5233 }
5234
5235 /// Called only once GitHub has answered the comment write, so an issue whose comment
5236 /// failed is never made a candidate a later read would pay a node read for.
5237 fn remember_commented(&self, issue: &NativeId) -> Result<(), SourceError> {
5238 let mut commented = self.commented()?;
5239 if !commented.contains(issue) {
5240 commented.push(issue.clone());
5241 }
5242 Ok(())
5243 }
5244
5245 /// The items this source has created, for completing a board read that is behind.
5246 fn created(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
5247 self.created.lock().map_err(|_| SourceError::Unavailable {
5248 message: "this source's record of what it created in this run was left \
5249 inconsistent by an earlier failure; next: run the command again"
5250 .into(),
5251 })
5252 }
5253
5254 /// One board item as this source reports it, or `None` for content it ignores.
5255 ///
5256 /// A pull request is neither a project nor a task — it is somebody's change, not a
5257 /// unit of plan — and an item whose content the token cannot see has nothing to
5258 /// report at all.
5259 fn resolve(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
5260 let content = item.get("content").ok_or_else(|| SourceError::Malformed {
5261 message: "GitHub project item is missing content".into(),
5262 })?;
5263 if content.is_null() {
5264 return Ok(None);
5265 }
5266 let content_kind = match required_str(content, "__typename")? {
5267 "Issue" => ContentKind::Issue,
5268 "DraftIssue" => ContentKind::DraftIssue,
5269 _ => return Ok(None),
5270 };
5271 let field_values = item
5272 .get("fieldValues")
5273 .ok_or_else(|| SourceError::Malformed {
5274 message: "GitHub project item is missing fieldValues".into(),
5275 })?;
5276 complete_connection(field_values, "project item field values", NESTED_PAGE_SIZE)?;
5277 let nodes = field_values
5278 .get("nodes")
5279 .and_then(Value::as_array)
5280 .ok_or_else(|| SourceError::Malformed {
5281 message: "GitHub project item fieldValues.nodes is not an array".into(),
5282 })?;
5283 if let Some(labels) = content.get("labels") {
5284 complete_connection(labels, "content labels", NESTED_PAGE_SIZE)?;
5285 }
5286 let raw_body = optional_str(content, "body")?.map(str::to_owned);
5287 let (body, slot) = metadata_body(raw_body.clone())?;
5288 let parent = optional_str(content.get("parent").unwrap_or(&Value::Null), "id")?
5289 .map(|id| NativeId(id.to_owned()));
5290 // A draft has no sub-issues to summarise, and GitHub's schema gives it no field
5291 // to read one from; it is a task, and never a project.
5292 let sub_issues = match content_kind {
5293 ContentKind::Issue => sub_issue_total(content)?,
5294 ContentKind::DraftIssue => 0,
5295 };
5296 let content_id = required_str(content, "id")?;
5297 let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
5298 message: format!("GitHub issue {content_id}: {message}"),
5299 })?;
5300 let raw_title = required_str(content, "title")?;
5301 // The design prefix is read *first*, before either of the two rules that separate
5302 // a project from a task. A document is not work whatever sub-issues it has and
5303 // whatever marker it carries, and reading the prefix later would make a design
5304 // issue with none of either an empty project.
5305 let kind = if raw_title.starts_with(DESIGN_TITLE_PREFIX) {
5306 BoardKind::Document
5307 } else if parent.is_some() {
5308 // Being a sub-issue wins outright, and no marker overrides it: an issue filed
5309 // under a project is that project's task even when it has sub-issues of its
5310 // own.
5311 BoardKind::Work(ItemKind::Task)
5312 } else if sub_issues > 0 || marked == Some(ItemKind::Project) {
5313 BoardKind::Work(ItemKind::Project)
5314 } else {
5315 BoardKind::Work(ItemKind::Task)
5316 };
5317 // The title a person wrote, which for a document is the one without the prefix —
5318 // the same way `content` above is the body without this source's metadata slot.
5319 let title = match kind {
5320 BoardKind::Document => raw_title[DESIGN_TITLE_PREFIX.len()..].to_owned(),
5321 BoardKind::Work(_) => raw_title.to_owned(),
5322 };
5323 let own_repository = content
5324 .pointer("/repository/nameWithOwner")
5325 .and_then(Value::as_str)
5326 .map(|origin| Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST)))
5327 .transpose()
5328 .map_err(|message| SourceError::Malformed { message })?;
5329 let repositories = if slot.contains_key(Repository::METADATA_KEY) {
5330 Repository::from_metadata(&slot)
5331 .map_err(|message| SourceError::Malformed { message })?
5332 } else {
5333 own_repository.clone().into_iter().collect()
5334 };
5335 let id = NativeId(content_id.to_owned());
5336 // Read only for a task, because only a task has either list: a project or a
5337 // document holding one of these keys holds nothing this source reports, and the
5338 // keys are left out of its caller-visible metadata all the same.
5339 let (delivers, delivered_by) = if kind == BoardKind::Work(ItemKind::Task) {
5340 let listed = |key: &str| {
5341 TaskRef::from_value(key, &id, Some(&self.name), slot.get(key))
5342 .map_err(|message| SourceError::Malformed { message })
5343 };
5344 (
5345 listed(TaskRef::DELIVERS_KEY)?,
5346 listed(TaskRef::DELIVERED_BY_KEY)?,
5347 )
5348 } else {
5349 (Vec::new(), Vec::new())
5350 };
5351 let (option, closed, reason) = Self::status_parts(nodes, content)?;
5352 let priority = self.held_priority(nodes)?;
5353 // Present when the item was reached through its own issue, whose board entry
5354 // names the board; a read of the board's own items has the board already. An
5355 // empty id names nothing a field write could address, so it is read as absent and
5356 // the write goes back to reading the board.
5357 let board_id = item
5358 .pointer("/project/id")
5359 .and_then(Value::as_str)
5360 .filter(|id| !id.is_empty());
5361 let resolved = Resolved {
5362 item_id: required_str(item, "id")?.to_owned(),
5363 id,
5364 content_kind,
5365 kind,
5366 title,
5367 body: body.filter(|value| !value.is_empty()),
5368 raw_body,
5369 status: self
5370 .statuses
5371 .status(kind.status_kind(), option, closed, reason),
5372 option: option.map(str::to_owned),
5373 priority,
5374 closed,
5375 delivers,
5376 delivered_by,
5377 labels: labels(content)?,
5378 parent,
5379 origin: text_field(nodes, ORIGIN_FIELD)?.filter(|value| !value.is_empty()),
5380 number: match content_kind {
5381 ContentKind::Issue => Some(issue_number(content)?),
5382 // A draft is filed in no repository, so nothing ever numbered it:
5383 // `DraftIssue` declares no `number` at all, exactly as it declares no
5384 // `subIssuesSummary` the branch above reads.
5385 ContentKind::DraftIssue => None,
5386 },
5387 url: optional_str(content, "url")?.map(str::to_owned),
5388 created_at: optional_time(content, "createdAt")?,
5389 updated_at: optional_time(content, "updatedAt")?,
5390 own_repository,
5391 repositories,
5392 slot,
5393 board_id: board_id.map(str::to_owned),
5394 fields: field_definitions(nodes),
5395 board_fields: Self::carried_board_fields(content, board_id)?,
5396 blocked_by: carried_blocked_by(content)?,
5397 };
5398 self.resolved_cache()?
5399 .insert(resolved.id.clone(), resolved.clone());
5400 Ok(Some(resolved))
5401 }
5402
5403 /// The field definitions of the board `board_id` names — the project this issue's own
5404 /// board item is on — off the `boards` page a read of an issue by its own id carries, or
5405 /// `None` when the read carried none, carried no entry for that board, or the board item
5406 /// named no board, which a write then answers by reading the board's fields itself.
5407 ///
5408 /// Matched by the board's node id and never by its number alone: a project number is
5409 /// unique only within its owner, so another owner's board numbered alike can sit on the
5410 /// same page, and its field and option ids address nothing on this one.
5411 fn carried_board_fields(
5412 content: &Value,
5413 board_id: Option<&str>,
5414 ) -> Result<Option<Value>, SourceError> {
5415 let (Some(nodes), Some(board_id)) = (
5416 content.pointer("/boards/nodes").and_then(Value::as_array),
5417 board_id,
5418 ) else {
5419 return Ok(None);
5420 };
5421 let Some(board) = nodes.iter().find_map(|node| {
5422 let project = node.get("project")?;
5423 (project.get("id").and_then(Value::as_str) == Some(board_id)).then_some(project)
5424 }) else {
5425 return Ok(None);
5426 };
5427 let Some(fields) = board.get("fields").filter(|fields| !fields.is_null()) else {
5428 return Ok(None);
5429 };
5430 complete_connection(fields, "board fields", NESTED_PAGE_SIZE)?;
5431 Ok(Some(fields.clone()))
5432 }
5433
5434 /// What one board item's `Priority` field says, through this instance's mapping.
5435 ///
5436 /// An instance with no mapping holds no priority, so every item reads as `none` whatever
5437 /// its board holds. With one, no value is `none`, a mapped option is its level, and an
5438 /// option the mapping does not name is kept as itself — never read as a level or as
5439 /// `none` — for a read of the task to report by name.
5440 fn held_priority(&self, field_values: &[Value]) -> Result<HeldPriority, SourceError> {
5441 let Some(mapping) = &self.priorities else {
5442 return Ok(HeldPriority::Read(Priority::None));
5443 };
5444 // A value of the field that names no option — a text field someone called `Priority` —
5445 // is malformed rather than `none`: reading it as no priority would let the next copy
5446 // clear one a person set.
5447 let Some(option) = field_values
5448 .iter()
5449 .find(|value| {
5450 value.pointer("/field/name").and_then(Value::as_str) == Some(PRIORITY_FIELD)
5451 })
5452 .map(|value| required_str(value, "name"))
5453 .transpose()?
5454 else {
5455 return Ok(HeldPriority::Read(Priority::None));
5456 };
5457 Ok(mapping.priority_of(option).map_or_else(
5458 || HeldPriority::Unmapped(option.to_owned()),
5459 HeldPriority::Read,
5460 ))
5461 }
5462
5463 /// What one board item's status is read from: its `Status` option, whether its issue
5464 /// is closed, and the reason it was closed with. [`BoardStatuses::status`] turns the
5465 /// three into the status it reports.
5466 fn status_parts<'a>(
5467 field_values: &'a [Value],
5468 content: &'a Value,
5469 ) -> Result<(Option<&'a str>, bool, Option<&'a str>), SourceError> {
5470 let option = field_values
5471 .iter()
5472 .find(|value| value.pointer("/field/name").and_then(Value::as_str) == Some("Status"))
5473 .map(|value| required_str(value, "name"))
5474 .transpose()?;
5475 let closed = optional_str(content, "state")? == Some("CLOSED");
5476 Ok((option, closed, optional_str(content, "stateReason")?))
5477 }
5478
5479 /// The board Status option this write selects, or the refusal that says why not.
5480 ///
5481 /// The mapped option is required for both open and terminal targets. A terminal write
5482 /// validates it before changing either representation, so it can never fall back to
5483 /// closing an issue whose board cannot display the matching status.
5484 ///
5485 /// Answers the field's id, the option's id, and the option's name as the board spells
5486 /// it — which is the name a read of the item reports once it sits there.
5487 fn column_for(
5488 &self,
5489 fields: &Value,
5490 kind: ItemKind,
5491 category: StatusCategory,
5492 target: &StatusTarget,
5493 ) -> Result<Option<(String, String, String)>, SourceError> {
5494 let Some(wanted) = target.option() else {
5495 return Ok(None);
5496 };
5497 let missing = |detail: &str| SourceError::Refused {
5498 message: format!(
5499 "{} status {} of source {} needs the board Status option {wanted:?}, and \
5500 {detail}; next: add that option to the board, which `onetaskgraph sources \
5501 fields {} --apply` does, or point status_mapping.{}.{} of this source at one \
5502 it has",
5503 kind.marker(),
5504 category_name(category),
5505 self.name,
5506 self.name,
5507 category_name(category),
5508 kind.marker()
5509 ),
5510 };
5511 let Some(field) = Board::field(fields, "Status")? else {
5512 return Err(missing("this board has no Status field"));
5513 };
5514 if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
5515 return Err(missing(
5516 "this board's Status field is not a single-select field",
5517 ));
5518 }
5519 let option = field
5520 .get("options")
5521 .and_then(Value::as_array)
5522 .and_then(|options| {
5523 options.iter().find(|option| {
5524 option
5525 .get("name")
5526 .and_then(Value::as_str)
5527 .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
5528 })
5529 });
5530 match option {
5531 None => Err(missing("this board does not have it")),
5532 Some(option) => Ok(Some((
5533 required_str(field, "id")?.to_owned(),
5534 required_str(option, "id")?.to_owned(),
5535 required_str(option, "name")?.to_owned(),
5536 ))),
5537 }
5538 }
5539
5540 /// The refusal a status that closes an issue is answered with over a board draft.
5541 fn closes_a_draft(&self, category: StatusCategory) -> SourceError {
5542 SourceError::Refused {
5543 message: format!(
5544 "status {} of source {} closes the item's issue, and GitHub draft items have \
5545 no open or closed state",
5546 category_name(category),
5547 self.name
5548 ),
5549 }
5550 }
5551
5552 /// What a status write to one item needs of the board: the board's id and the
5553 /// definition of its `Status` field, read off the item when the item says both.
5554 ///
5555 /// The same reasoning as [`Self::fields_for`]: a node read of the item names its board,
5556 /// and its `Status` value carries that field's definition, options and all. An item that
5557 /// does not say — no board id, or no `Status` value to read the field off — takes them
5558 /// from [`Self::board_fields`], which reads no item.
5559 async fn status_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
5560 if let Some(board) = item.carried_board() {
5561 return Ok(board);
5562 }
5563 if item.defines("Status")
5564 && let Some(board_id) = item.named_board()
5565 {
5566 return Ok(BoardFields {
5567 id: board_id,
5568 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
5569 });
5570 }
5571 self.board_fields().await
5572 }
5573
5574 /// Set one task's status and nothing else; see [`TaskSource::set_task_status`].
5575 async fn set_status(
5576 &self,
5577 id: &NativeId,
5578 category: StatusCategory,
5579 ) -> Result<Option<Status>, SourceError> {
5580 // Refused before anything is read, in the words a write of the same status is.
5581 let target = self.resolved_target(ItemKind::Task, category)?;
5582 let Some(mut item) = self
5583 .bound_item(id)
5584 .await?
5585 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
5586 else {
5587 return Ok(None);
5588 };
5589 let board = self.status_board(&item).await?;
5590 let (field, option, name) = self
5591 .column_for(&board.fields, ItemKind::Task, category, &target)?
5592 .ok_or_else(|| SourceError::Malformed {
5593 message: format!(
5594 "status {} of source {} names no board Status option",
5595 category_name(category),
5596 self.name
5597 ),
5598 })?;
5599 if item.status.category == category && item.option.as_deref() == Some(&name) {
5600 return Ok(Some(item.status));
5601 }
5602 match &target {
5603 StatusTarget::Terminal(_, reason) => {
5604 if item.content_kind == ContentKind::DraftIssue {
5605 return Err(self.closes_a_draft(category));
5606 }
5607 self.set_item_field(
5608 board.id.as_str(),
5609 &item.item_id,
5610 &field,
5611 json!({"singleSelectOptionId": option}),
5612 )
5613 .await?;
5614 self.update_content(
5615 ContentKind::Issue,
5616 &item.id,
5617 json!({"stateInput": state_input(Some(&target))}),
5618 )
5619 .await?;
5620 item.closed = true;
5621 item.status =
5622 self.statuses
5623 .status(ItemKind::Task, Some(&name), true, Some(reason.reason()));
5624 item.option = Some(name);
5625 }
5626 StatusTarget::Column(_) => {
5627 // An option is what an open item's status is, so a closed issue is reopened
5628 // first — sitting closed in the column, it would read back as closed. A draft has
5629 // no state to reopen.
5630 if item.content_kind == ContentKind::Issue && item.closed {
5631 self.update_content(
5632 ContentKind::Issue,
5633 &item.id,
5634 json!({"stateInput": state_input(Some(&target))}),
5635 )
5636 .await?;
5637 item.closed = false;
5638 }
5639 self.set_item_field(
5640 board.id.as_str(),
5641 &item.item_id,
5642 &field,
5643 json!({"singleSelectOptionId": option}),
5644 )
5645 .await?;
5646 item.status = self
5647 .statuses
5648 .status(ItemKind::Task, Some(&name), false, None);
5649 item.option = Some(name);
5650 }
5651 StatusTarget::Disabled(_) => {
5652 unreachable!("resolved_target refused a disabled status")
5653 }
5654 }
5655 let status = item.status.clone();
5656 self.remember_written(item, false)?;
5657 Ok(Some(status))
5658 }
5659
5660 /// Replace one task's `delivered_by` and nothing else; see
5661 /// [`TaskSource::set_delivered_by`].
5662 ///
5663 /// One update of the body, which differs from the body GitHub holds only inside the
5664 /// metadata slot — see [`with_slot`]. A body that would not change is not sent at all.
5665 async fn replace_delivered_by(
5666 &self,
5667 id: &NativeId,
5668 delivered_by: &[TaskRef],
5669 ) -> Result<Option<()>, SourceError> {
5670 let entries = TaskRef::listed(
5671 TaskRef::DELIVERED_BY_KEY,
5672 id,
5673 Some(&self.name),
5674 delivered_by.to_vec(),
5675 )
5676 .map_err(|message| SourceError::Refused { message })?;
5677 let Some(mut item) = self
5678 .bound_item(id)
5679 .await?
5680 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
5681 else {
5682 return Ok(None);
5683 };
5684 let mut slot = item.slot.clone();
5685 set_task_list(&mut slot, TaskRef::DELIVERED_BY_KEY, &entries);
5686 self.write_slot(&mut item, &slot).await?;
5687 item.delivered_by = entries;
5688 self.remember_written(item, false)?;
5689 Ok(Some(()))
5690 }
5691
5692 /// Set one caller key of the metadata slot of one issue of `kind`, and nothing else;
5693 /// see [`TaskSource::set_task_metadata`].
5694 ///
5695 /// `None` when this board holds no item by that id, or holds one of another kind. The
5696 /// answer is the item as this source now reads it, so what a caller is told the key
5697 /// holds is what the slot holds.
5698 ///
5699 /// A key already holding the value is answered without a write, compared as JSON rather
5700 /// than as the body's bytes: a slot a person spelled with other whitespace would
5701 /// otherwise be re-encoded, which is a write that changes nothing the caller asked for.
5702 async fn set_slot_key(
5703 &self,
5704 id: &NativeId,
5705 kind: BoardKind,
5706 key: &MetadataKey,
5707 value: &Value,
5708 ) -> Result<Option<Resolved>, SourceError> {
5709 let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
5710 return Ok(None);
5711 };
5712 if item.slot.get(key.as_str()) == Some(value) {
5713 return Ok(Some(item));
5714 }
5715 let mut slot = item.slot.clone();
5716 slot.insert(key.as_str().to_owned(), value.clone());
5717 self.write_slot(&mut item, &slot).await?;
5718 self.remember_written(item.clone(), false)?;
5719 Ok(Some(item))
5720 }
5721
5722 /// Put `slot` in one item's metadata slot with a single update of its body, and bring
5723 /// `item` up to what that write left.
5724 ///
5725 /// The body sent differs from the body GitHub holds only inside the slot — see
5726 /// [`with_slot`] — and a body that would not change is not sent at all. It goes through
5727 /// the mutation the item's content takes, so a board draft's body is written with
5728 /// `updateProjectV2DraftIssue` exactly as an issue's is with `updateIssue`.
5729 async fn write_slot(
5730 &self,
5731 item: &mut Resolved,
5732 slot: &BTreeMap<String, Value>,
5733 ) -> Result<(), SourceError> {
5734 let held = item.raw_body.clone().unwrap_or_default();
5735 let body = with_slot(&held, slot)?;
5736 if body != held {
5737 self.update_content(item.content_kind, &item.id, json!({"body": body}))
5738 .await?;
5739 }
5740 let (visible, slot) = metadata_body(Some(body.clone()))?;
5741 item.body = visible.filter(|value| !value.is_empty());
5742 item.raw_body = Some(body);
5743 item.slot = slot;
5744 Ok(())
5745 }
5746
5747 /// This instance's target for a category written to an item of `kind`, refusing one
5748 /// that kind has no option for — before anything is read or written.
5749 ///
5750 /// Nothing here mutates the board's option set to make room for a status. GitHub
5751 /// documents `UpdateProjectV2FieldInput.singleSelectOptions` as *"provided values
5752 /// overwrite existing options"*, so no addition is additive and a mistake destroys the
5753 /// field and every item's status.
5754 fn resolved_target(
5755 &self,
5756 kind: ItemKind,
5757 category: StatusCategory,
5758 ) -> Result<StatusTarget, SourceError> {
5759 let target = self.statuses.target(kind, category).clone();
5760 let StatusTarget::Disabled(why) = target else {
5761 return Ok(target);
5762 };
5763 let refusal = why.refusal(&self.name, category, kind);
5764 // Why there is no shipped default, which is the question a person meeting this
5765 // refusal on a source that never mentioned the category asks.
5766 let shipped_none = match category {
5767 StatusCategory::Draft => Some(
5768 "draft has no shipped default because GitHub draft issues cannot have \
5769 sub-issues, and this source stores a project's tasks as its issue's sub-issues",
5770 ),
5771 StatusCategory::Unknown => Some(
5772 "unknown has no shipped default because this board keeps no open-ended status \
5773 word: every word classified unknown is written to the one board Status option \
5774 status_mapping.unknown names",
5775 ),
5776 _ => None,
5777 };
5778 Err(match (refusal, shipped_none, why) {
5779 (SourceError::Refused { message }, Some(note), UnmappedStatus::Unconfigured) => {
5780 SourceError::Refused {
5781 message: format!("{message}; {note}"),
5782 }
5783 }
5784 (refusal, _, _) => refusal,
5785 })
5786 }
5787
5788 /// What writing `priority` does to one item's `Priority` field on this board, or the
5789 /// refusal naming what the board lacks.
5790 ///
5791 /// `none` is no value, so it clears the field — and asks nothing of an item that holds
5792 /// none already, or of an item not created yet. Every other priority selects the option
5793 /// the mapping names, matched case-insensitively; a board with no `Priority` field, or
5794 /// without that option, is refused rather than given one: reads and writes never create
5795 /// a field or an option.
5796 fn priority_write(
5797 &self,
5798 fields: &Value,
5799 existing: Option<&Resolved>,
5800 priority: Priority,
5801 ) -> Result<Option<PriorityWrite>, SourceError> {
5802 let Some(mapping) = &self.priorities else {
5803 return Err(self.holds_no_priority());
5804 };
5805 let Some(wanted) = mapping.option(priority) else {
5806 if !existing.is_some_and(Resolved::holds_priority) {
5807 return Ok(None);
5808 }
5809 let field =
5810 Board::field(fields, PRIORITY_FIELD)?.ok_or_else(|| SourceError::Malformed {
5811 message: format!(
5812 "an item holding a {PRIORITY_FIELD} value was read without that field"
5813 ),
5814 })?;
5815 return Ok(Some(PriorityWrite::Clear {
5816 field: required_str(field, "id")?.to_owned(),
5817 }));
5818 };
5819 let missing = |detail: &str| SourceError::Refused {
5820 message: format!(
5821 "priority {priority} of source {} needs the board {PRIORITY_FIELD} option \
5822 {wanted:?}, and {detail}; run `onetaskgraph sources fields {} --apply` to add \
5823 it, or point priority_mapping.{priority} of this source at an option the board \
5824 has",
5825 self.name, self.name
5826 ),
5827 };
5828 let Some(field) = Board::field(fields, PRIORITY_FIELD)? else {
5829 return Err(missing(&format!(
5830 "this board has no {PRIORITY_FIELD} field"
5831 )));
5832 };
5833 if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
5834 return Err(missing(&format!(
5835 "this board's {PRIORITY_FIELD} field is not a single-select field"
5836 )));
5837 }
5838 // An options list that is absent or not a list is an answer this source cannot read,
5839 // not a board lacking the option: `sources fields --apply` is no remedy for it.
5840 let option = field
5841 .get("options")
5842 .and_then(Value::as_array)
5843 .ok_or_else(|| SourceError::Malformed {
5844 message: format!("GitHub {PRIORITY_FIELD} field options is not an array"),
5845 })?
5846 .iter()
5847 .find(|option| {
5848 option
5849 .get("name")
5850 .and_then(Value::as_str)
5851 .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
5852 })
5853 .ok_or_else(|| missing("this board does not have it"))?;
5854 Ok(Some(PriorityWrite::Select {
5855 field: required_str(field, "id")?.to_owned(),
5856 option: required_str(option, "id")?.to_owned(),
5857 }))
5858 }
5859
5860 /// Apply one priority write to one board item.
5861 async fn write_priority(
5862 &self,
5863 board_id: &str,
5864 item_id: &str,
5865 write: &PriorityWrite,
5866 ) -> Result<(), SourceError> {
5867 match write {
5868 PriorityWrite::Select { field, option } => {
5869 self.set_item_field(
5870 board_id,
5871 item_id,
5872 field,
5873 json!({"singleSelectOptionId": option}),
5874 )
5875 .await
5876 }
5877 PriorityWrite::Clear { field } => {
5878 let data = self
5879 .graphql(
5880 graphql::CLEAR_FIELD,
5881 json!({"input":{"projectId":board_id,"itemId":item_id,"fieldId":field},
5882 "readPriority":false,"priorityName":PRIORITY_FIELD}),
5883 )
5884 .await?;
5885 let returned = data
5886 .pointer("/clearProjectV2ItemFieldValue/projectV2Item")
5887 .ok_or_else(|| SourceError::Malformed {
5888 message: "GitHub field clear returned no project item".into(),
5889 })?;
5890 if required_str(returned, "id")? != item_id {
5891 return Err(SourceError::Malformed {
5892 message: "GitHub field clear returned the wrong project item".into(),
5893 });
5894 }
5895 Ok(())
5896 }
5897 }
5898 }
5899
5900 /// The refusal a priority is answered with by an instance configured with no
5901 /// `priority_mapping`, which holds none.
5902 fn holds_no_priority(&self) -> SourceError {
5903 SourceError::Refused {
5904 message: format!(
5905 "source {} holds no task priority: its configuration sets no priority_mapping; \
5906 next: set priority_mapping on this source, then run `onetaskgraph sources \
5907 fields {} --apply` to set its board up",
5908 self.name, self.name
5909 ),
5910 }
5911 }
5912
5913 /// Set one task's priority and nothing else; see [`TaskSource::set_task_priority`].
5914 ///
5915 /// One field write — a select, or a clear for `none` — and no title, body, label, state
5916 /// or `Status` request. Clearing a priority an item does not hold sends nothing.
5917 async fn set_priority(
5918 &self,
5919 id: &NativeId,
5920 priority: Priority,
5921 ) -> Result<Option<Priority>, SourceError> {
5922 if self.priorities.is_none() {
5923 return Err(self.holds_no_priority());
5924 }
5925 let Some(mut item) = self
5926 .bound_item(id)
5927 .await?
5928 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
5929 else {
5930 return Ok(None);
5931 };
5932 if priority == Priority::None && !item.holds_priority() {
5933 return Ok(Some(priority));
5934 }
5935 // The item's own read carries the field's definition whenever it holds a value of
5936 // it, which a clear always does; a select onto an item holding none reads the board.
5937 let board = match (item.carried_board(), item.named_board()) {
5938 (Some(board), _) => board,
5939 (None, Some(id)) if item.defines(PRIORITY_FIELD) => BoardFields {
5940 id,
5941 fields: json!({"nodes": item.fields.clone(), "pageInfo": {"hasNextPage": false}}),
5942 },
5943 _ => self.board_fields().await?,
5944 };
5945 let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? else {
5946 return Ok(Some(priority));
5947 };
5948 let (document, root, input) = match write {
5949 PriorityWrite::Select { field, option } => (
5950 graphql::UPDATE_FIELD,
5951 "updateProjectV2ItemFieldValue",
5952 json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field,"value":{"singleSelectOptionId":option}}),
5953 ),
5954 PriorityWrite::Clear { field } => (
5955 graphql::CLEAR_FIELD,
5956 "clearProjectV2ItemFieldValue",
5957 json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field}),
5958 ),
5959 };
5960 let data = self
5961 .graphql(
5962 document,
5963 json!({"input":input,"readPriority":true,"priorityName":PRIORITY_FIELD}),
5964 )
5965 .await?;
5966 let returned = data
5967 .get(root)
5968 .and_then(|value| value.get("projectV2Item"))
5969 .ok_or_else(|| SourceError::Malformed {
5970 message: "GitHub priority write returned no project item".into(),
5971 })?;
5972 if required_str(returned, "id")? != item.item_id {
5973 return Err(SourceError::Malformed {
5974 message: "GitHub priority write returned the wrong project item".into(),
5975 });
5976 }
5977 let value = returned
5978 .get("fieldValueByName")
5979 .ok_or_else(|| SourceError::Malformed {
5980 message: "GitHub priority write returned no priority read-back".into(),
5981 })?;
5982 if !value.is_null()
5983 && value.pointer("/field/name").and_then(Value::as_str) != Some(PRIORITY_FIELD)
5984 {
5985 return Err(SourceError::Malformed {
5986 message: "GitHub priority read-back is not a Priority field value".into(),
5987 });
5988 }
5989 let values = if value.is_null() {
5990 Vec::new()
5991 } else {
5992 vec![value.clone()]
5993 };
5994 item.priority = self.held_priority(&values)?;
5995 let answer = item.task()?.priority;
5996 self.remember_written(item, false)?;
5997 Ok(Some(answer))
5998 }
5999
6000 /// Replace one task's visible body and nothing else; see
6001 /// [`TaskSource::set_task_content`].
6002 ///
6003 /// One update of the body, which differs from the body GitHub holds only outside the
6004 /// metadata slot — the slot is kept byte for byte, so every caller key and every list
6005 /// this source keeps there reads back as it was. A body that would not change is not
6006 /// sent at all.
6007 async fn replace_content(
6008 &self,
6009 id: &NativeId,
6010 content: &str,
6011 ) -> Result<Option<()>, SourceError> {
6012 let Some(mut item) = self
6013 .bound_item(id)
6014 .await?
6015 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
6016 else {
6017 return Ok(None);
6018 };
6019 let held = item.raw_body.clone().unwrap_or_default();
6020 let body = with_content(&held, content)?;
6021 // Checked before anything is sent: content ending in what this source reads as its own
6022 // metadata slot would read back as metadata rather than as the content it was.
6023 let (visible, slot) = metadata_body(Some(body.clone()))?;
6024 if visible.as_deref().unwrap_or_default() != content || slot != item.slot {
6025 return Err(SourceError::Refused {
6026 message: format!(
6027 "this content ends in what source {} reads as its own metadata slot \
6028 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
6029 as content; next: remove that trailing block from the content",
6030 self.name
6031 ),
6032 });
6033 }
6034 if body != held {
6035 self.update_content(item.content_kind, &item.id, json!({"body": body}))
6036 .await?;
6037 }
6038 item.body = visible.filter(|value| !value.is_empty());
6039 item.raw_body = Some(body);
6040 item.slot = slot;
6041 self.remember_written(item, false)?;
6042 Ok(Some(()))
6043 }
6044
6045 /// Apply one targeted update to one task; see [`TaskSource::update_task`].
6046 ///
6047 /// One read of the item — which carries the board's field definitions and the issue's
6048 /// `blockedBy`, so neither is read again — and then only what differs from it: the
6049 /// `Status` option and the `Priority` field together in one request, the `blockedBy`
6050 /// additions and removals the named edges differ by, and last one `updateIssue` carrying
6051 /// the title, the body — visible content and metadata slot together — and a state change.
6052 /// So an update naming any of title, body, metadata, status and priority is one read and
6053 /// at most two writes. The body goes last so that a write refused part-way leaves it, and
6054 /// the metadata in it, as it stood. A terminal status selects its option and then closes,
6055 /// as a whole write does; an open one selects its option and then reopens. The origin
6056 /// field is never written: an update is of an item that already exists, whose origin is
6057 /// what it is.
6058 ///
6059 /// The task answered is the item as those writes left it, built from the read and what was
6060 /// sent rather than read again — the same record a later read in this run answers from.
6061 async fn targeted_update(
6062 &self,
6063 id: &NativeId,
6064 update: &TaskUpdate,
6065 ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
6066 // Everything this source can refuse without reading the item is refused first, in the
6067 // words a whole write of the same fields is refused with.
6068 update.consistent()?;
6069 if update
6070 .title
6071 .as_deref()
6072 .is_some_and(|title| title.starts_with(DESIGN_TITLE_PREFIX))
6073 {
6074 return Err(SourceError::Refused {
6075 message: format!(
6076 "the title of this task begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
6077 spells a document, so it would read back as one rather than as a task; \
6078 retitle it",
6079 self.name
6080 ),
6081 });
6082 }
6083 if let Some(delivers) = &update.delivers {
6084 TaskRef::listed(
6085 TaskRef::DELIVERS_KEY,
6086 id,
6087 Some(&self.name),
6088 delivers.clone(),
6089 )
6090 .map_err(|message| SourceError::Refused { message })?;
6091 }
6092 if self.priorities.is_none()
6093 && update
6094 .priority
6095 .is_some_and(|priority| priority != Priority::None)
6096 {
6097 return Err(self.holds_no_priority());
6098 }
6099 let target = update
6100 .status
6101 .as_ref()
6102 .map(|status| self.resolved_target(ItemKind::Task, status.category))
6103 .transpose()?;
6104 let Some(mut item) = self
6105 .bound_item(id)
6106 .await?
6107 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
6108 else {
6109 return Ok(None);
6110 };
6111 let before = item.task()?;
6112
6113 let mut status_move = None;
6114 if let (Some(status), Some(target)) = (&update.status, target) {
6115 let board = self.status_board(&item).await?;
6116 let (field, option, name) = self
6117 .column_for(&board.fields, ItemKind::Task, status.category, &target)?
6118 .ok_or_else(|| SourceError::Malformed {
6119 message: format!(
6120 "status {} of source {} names no board Status option",
6121 category_name(status.category),
6122 self.name
6123 ),
6124 })?;
6125 let terminal = matches!(target, StatusTarget::Terminal(_, _));
6126 if terminal && item.content_kind == ContentKind::DraftIssue {
6127 return Err(self.closes_a_draft(status.category));
6128 }
6129 let landed = match &target {
6130 StatusTarget::Terminal(_, reason) => {
6131 self.statuses
6132 .status(ItemKind::Task, Some(&name), true, Some(reason.reason()))
6133 }
6134 _ => self
6135 .statuses
6136 .status(ItemKind::Task, Some(&name), false, None),
6137 };
6138 let option_moves = item
6139 .option
6140 .as_deref()
6141 .is_none_or(|held| !held.eq_ignore_ascii_case(&name));
6142 let state_moves = item.content_kind == ContentKind::Issue
6143 && (item.closed != terminal || (terminal && item.status != landed));
6144 if let Some(moves) = Moves::of(option_moves, state_moves) {
6145 status_move = Some(StatusMove {
6146 board: board.id,
6147 field,
6148 option,
6149 name,
6150 target,
6151 landed,
6152 moves,
6153 });
6154 }
6155 }
6156
6157 let mut priority_move = None;
6158 if let Some(priority) = update.priority
6159 && self.priorities.is_some()
6160 && item.priority != HeldPriority::Read(priority)
6161 {
6162 let board = match (item.carried_board(), item.named_board()) {
6163 (Some(board), _) => board,
6164 (None, Some(board)) if item.defines(PRIORITY_FIELD) => BoardFields {
6165 id: board,
6166 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
6167 },
6168 _ => self.board_fields().await?,
6169 };
6170 if let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? {
6171 priority_move = Some((board.id, write, priority));
6172 }
6173 }
6174
6175 // Resolved before the body is composed, because a far end `blockedBy` cannot name is
6176 // recorded in the slot, and the slot travels in the one body update below.
6177 let edges = match &update.depends_on {
6178 Some(edges) => Some(
6179 self.partition_edges(
6180 BoardKind::Work(ItemKind::Task),
6181 item.content_kind,
6182 item.blocked_by.as_deref(),
6183 edges,
6184 )
6185 .await?,
6186 ),
6187 None => None,
6188 };
6189
6190 let mut slot = item.slot.clone();
6191 for (key, value) in &update.metadata_set {
6192 slot.insert(key.as_str().to_owned(), value.clone());
6193 }
6194 for key in &update.metadata_remove {
6195 slot.remove(key.as_str());
6196 }
6197 if let Some(delivers) = &update.delivers {
6198 set_task_list(&mut slot, TaskRef::DELIVERS_KEY, delivers);
6199 }
6200 if let Some((_, recorded)) = &edges {
6201 record_edges(&mut slot, recorded);
6202 }
6203 let held = item.raw_body.clone().unwrap_or_default();
6204 let content = match &update.content {
6205 Some(content) => with_content(&held, content)?,
6206 None => held.clone(),
6207 };
6208 // A slot holding what it held is kept byte for byte, compared as JSON rather than as
6209 // the body's bytes, as a metadata write compares it: a slot a person spelled with
6210 // other whitespace would otherwise be re-encoded, which is a write nobody asked for.
6211 let body = if slot == item.slot {
6212 content
6213 } else {
6214 with_slot(&content, &slot)?
6215 };
6216 // Checked before anything is sent, as a content write checks it: content ending in
6217 // what this source reads as its own slot would read back as metadata.
6218 let (visible, read) = metadata_body(Some(body.clone()))?;
6219 let wanted = update.content.as_deref().or(item.body.as_deref());
6220 if visible.as_deref().unwrap_or_default() != wanted.unwrap_or_default() || read != slot {
6221 return Err(SourceError::Refused {
6222 message: format!(
6223 "this content ends in what source {} reads as its own metadata slot \
6224 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
6225 as content; next: remove that trailing block from the content",
6226 self.name
6227 ),
6228 });
6229 }
6230 let recorded_moves =
6231 slot.get(DependencyEdge::RECORDED_KEY) != item.slot.get(DependencyEdge::RECORDED_KEY);
6232
6233 // One `updateIssue` carries all three, because every mutation spends the secondary
6234 // limiter and the title, body and state are one mutation's inputs.
6235 let mut fields = serde_json::Map::new();
6236 if let Some(title) = update.title.as_ref().filter(|title| **title != item.title) {
6237 fields.insert("title".to_owned(), json!(title));
6238 }
6239 if body != held {
6240 fields.insert("body".to_owned(), json!(body));
6241 }
6242 if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.state()) {
6243 fields.insert("stateInput".to_owned(), state_input(Some(&moving.target)));
6244 }
6245 // **The body is written last, and that is the guarantee a refusal part-way keeps.**
6246 // GitHub runs no two requests as one, and runs one document's mutation fields in order
6247 // without undoing an earlier field when a later one fails — so a body written before a
6248 // board field the board then refused would be left changed. Written after every other
6249 // write has landed, a refusal anywhere leaves the item's body, and every metadata key
6250 // it carries, exactly as they stood. So the `Status` option and the `Priority` field go
6251 // first, together in one request — a terminal option selected before the issue
6252 // closes, as a whole write does — then the `blockedBy` difference, then the body.
6253 let mut board_writes: Vec<(&BoardId, (String, Value))> = Vec::new();
6254 let mut clear: Option<(&BoardId, &str)> = None;
6255 if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.option()) {
6256 board_writes.push((
6257 &moving.board,
6258 (
6259 moving.field.clone(),
6260 json!({"singleSelectOptionId": moving.option}),
6261 ),
6262 ));
6263 }
6264 match &priority_move {
6265 Some((board, PriorityWrite::Select { field, option }, _)) => board_writes.push((
6266 board,
6267 (field.clone(), json!({"singleSelectOptionId": option})),
6268 )),
6269 Some((board, PriorityWrite::Clear { field }, _)) => clear = Some((board, field)),
6270 None => {}
6271 }
6272 let mut boards: Vec<&BoardId> = board_writes.iter().map(|(board, _)| *board).collect();
6273 boards.extend(clear.map(|(board, _)| board));
6274 boards.dedup_by(|one, other| one.as_str() == other.as_str());
6275 for board in boards {
6276 let writes = board_writes
6277 .iter()
6278 .filter(|(on, _)| on.as_str() == board.as_str())
6279 .map(|(_, write)| write.clone())
6280 .collect::<Vec<_>>();
6281 let cleared = clear
6282 .filter(|(on, _)| on.as_str() == board.as_str())
6283 .map(|(_, field)| field);
6284 self.set_item_fields(board.as_str(), &item.item_id, &writes, cleared)
6285 .await?;
6286 }
6287 let mut blocked_by_moved = false;
6288 if let Some((native, _)) = &edges
6289 && item.content_kind == ContentKind::Issue
6290 {
6291 blocked_by_moved = self
6292 .reconcile_blocked_by(
6293 &item.id,
6294 native,
6295 Issue::Existing(item.blocked_by.as_deref()),
6296 )
6297 .await?;
6298 }
6299 if !fields.is_empty() {
6300 self.update_content(item.content_kind, &item.id, Value::Object(fields))
6301 .await?;
6302 }
6303
6304 if let Some(title) = &update.title {
6305 item.title.clone_from(title);
6306 }
6307 item.body = visible.filter(|value| !value.is_empty());
6308 item.raw_body = (!body.is_empty() || item.raw_body.is_some()).then_some(body);
6309 item.slot = slot;
6310 if let Some(delivers) = &update.delivers {
6311 item.delivers.clone_from(delivers);
6312 }
6313 if let Some(moving) = status_move {
6314 item.closed = matches!(moving.target, StatusTarget::Terminal(_, _))
6315 && item.content_kind == ContentKind::Issue;
6316 item.status = moving.landed;
6317 item.option = Some(moving.name);
6318 }
6319 if let Some((_, _, priority)) = priority_move {
6320 item.priority = HeldPriority::Read(priority);
6321 }
6322 let task = item.task()?;
6323 let mut written = update.changed(&before, &task);
6324 if blocked_by_moved || recorded_moves {
6325 written.insert(UpdatedField::DependsOn);
6326 }
6327 self.remember_written(item, false)?;
6328 Ok(Some(TaskUpdateOutcome {
6329 task,
6330 written,
6331 delivers_before: before.delivers,
6332 }))
6333 }
6334
6335 /// Replace one issue's visible body and its [`MetadataKey::TEMPLATE_KEY`] slot entry
6336 /// together, and nothing else; see [`TaskSource::set_task_rendering`].
6337 ///
6338 /// One update of the body: the content outside the slot, and inside it that one entry,
6339 /// every other entry kept as it was. This source keeps no template answers — an issue has
6340 /// no room beside itself that is not its body, and answers written there would duplicate
6341 /// what the content already says and count against GitHub's body limit — so `answers`
6342 /// reaches nothing here. A body that would not change is not sent at all.
6343 async fn replace_rendering(
6344 &self,
6345 id: &NativeId,
6346 kind: BoardKind,
6347 content: &str,
6348 provenance: &Value,
6349 ) -> Result<Option<()>, SourceError> {
6350 let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
6351 return Ok(None);
6352 };
6353 let held = item.raw_body.clone().unwrap_or_default();
6354 let mut slot = item.slot.clone();
6355 slot.insert(MetadataKey::TEMPLATE_KEY.to_owned(), provenance.clone());
6356 let body = with_slot(&with_content(&held, content)?, &slot)?;
6357 // Checked before anything is sent, as a content write checks it.
6358 let (visible, read) = metadata_body(Some(body.clone()))?;
6359 if visible.as_deref().unwrap_or_default() != content || read != slot {
6360 return Err(SourceError::Refused {
6361 message: format!(
6362 "this content ends in what source {} reads as its own metadata slot \
6363 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
6364 as content; next: remove that trailing block from the template",
6365 self.name
6366 ),
6367 });
6368 }
6369 if body != held {
6370 self.update_content(item.content_kind, &item.id, json!({"body": body}))
6371 .await?;
6372 }
6373 item.body = visible.filter(|value| !value.is_empty());
6374 item.raw_body = Some(body);
6375 item.slot = read;
6376 self.remember_written(item, false)?;
6377 Ok(Some(()))
6378 }
6379
6380 async fn set_item_field(
6381 &self,
6382 board_id: &str,
6383 item_id: &str,
6384 field_id: &str,
6385 value: Value,
6386 ) -> Result<(), SourceError> {
6387 let data = self
6388 .graphql(
6389 graphql::UPDATE_FIELD,
6390 json!({"input":{
6391 "projectId":board_id,"itemId":item_id,"fieldId":field_id,"value":value
6392 },"readPriority":false,"priorityName":PRIORITY_FIELD}),
6393 )
6394 .await?;
6395 let returned = data
6396 .pointer("/updateProjectV2ItemFieldValue/projectV2Item")
6397 .ok_or_else(|| SourceError::Malformed {
6398 message: "GitHub field update returned no project item".into(),
6399 })?;
6400 if required_str(returned, "id")? != item_id {
6401 return Err(SourceError::Malformed {
6402 message: "GitHub field update returned the wrong project item".into(),
6403 });
6404 }
6405 Ok(())
6406 }
6407
6408 /// GitHub accepts one value per field mutation; aliases combine those mutations in
6409 /// one request. Every returned item id is checked, including optional aliases.
6410 async fn set_item_fields(
6411 &self,
6412 board: &str,
6413 item: &str,
6414 fields: &[(String, Value)],
6415 clear: Option<&str>,
6416 ) -> Result<(), SourceError> {
6417 if fields.len() <= 1 && clear.is_none() {
6418 if let Some((field, value)) = fields.first() {
6419 self.set_item_field(board, item, field, value.clone())
6420 .await?;
6421 }
6422 return Ok(());
6423 }
6424 if fields.is_empty() {
6425 if let Some(field) = clear {
6426 self.write_priority(
6427 board,
6428 item,
6429 &PriorityWrite::Clear {
6430 field: field.to_owned(),
6431 },
6432 )
6433 .await?;
6434 }
6435 return Ok(());
6436 }
6437 let input = |index: usize| {
6438 let (field, value) = fields.get(index).unwrap_or(&fields[0]);
6439 json!({"projectId":board,"itemId":item,"fieldId":field,"value":value})
6440 };
6441 let data = self.graphql(graphql::UPDATE_FIELDS, json!({
6442 "input":input(0),"second":input(1),"third":input(2),
6443 "writeSecond":fields.len()>1,"writeThird":fields.len()>2,"writeClear":clear.is_some(),
6444 "clear":{"projectId":board,"itemId":item,"fieldId":clear.unwrap_or(&fields[0].0)}
6445 })).await?;
6446 for alias in [
6447 Some("updateProjectV2ItemFieldValue"),
6448 (fields.len() > 1).then_some("second"),
6449 (fields.len() > 2).then_some("third"),
6450 clear.map(|_| "cleared"),
6451 ]
6452 .into_iter()
6453 .flatten()
6454 {
6455 let returned = data
6456 .get(alias)
6457 .and_then(|value| value.get("projectV2Item"))
6458 .ok_or_else(|| SourceError::Malformed {
6459 message: format!("GitHub field update {alias} returned no project item"),
6460 })?;
6461 if required_str(returned, "id")? != item {
6462 return Err(SourceError::Malformed {
6463 message: format!("GitHub field update {alias} returned the wrong project item"),
6464 });
6465 }
6466 }
6467 Ok(())
6468 }
6469
6470 async fn native_dependency_ids(&self, id: &NativeId) -> Result<Vec<String>, SourceError> {
6471 let mut after: Option<String> = None;
6472 let mut ids = Vec::new();
6473 loop {
6474 let data = self
6475 .graphql(
6476 graphql::ISSUE_DEPENDENCIES,
6477 json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after}),
6478 )
6479 .await?;
6480 let connection =
6481 data.pointer("/node/blockedBy")
6482 .ok_or_else(|| SourceError::Malformed {
6483 message: "GitHub dependency response has no blockedBy connection".into(),
6484 })?;
6485 ids.extend(
6486 connection
6487 .get("nodes")
6488 .and_then(Value::as_array)
6489 .ok_or_else(|| SourceError::Malformed {
6490 message: "GitHub dependency response nodes is not an array".into(),
6491 })?
6492 .iter()
6493 .map(|value| required_str(value, "id").map(str::to_owned))
6494 .collect::<Result<Vec<_>, _>>()?,
6495 );
6496 let next = next_cursor(connection)?;
6497 if let Some(next) = &next {
6498 validate_cursor_progress(after.as_deref(), &next.0)?;
6499 }
6500 after = next.map(|cursor| cursor.0);
6501 if after.is_none() {
6502 return Ok(ids);
6503 }
6504 }
6505 }
6506
6507 async fn dependencies(
6508 &self,
6509 id: &NativeId,
6510 near_kind: ItemKind,
6511 direction: Direction,
6512 page: &PageRequest,
6513 ) -> Result<Page<DependencyEdge>, SourceError> {
6514 validate_page(page)?;
6515 let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
6516 let cursor = page.cursor.as_ref().map(|c| c.0.as_str());
6517 let recorded = recorded_offset(cursor, direction)?;
6518 // What this issue is blocked by, when a read of it by its own id in this command
6519 // already carried the whole connection — a copy reads the item it writes before it
6520 // reads its edges — and the page asked for is the whole of it, or the recorded tail
6521 // after it. Answered from that read, in the shape the dependency read answers in;
6522 // anything else is asked of GitHub.
6523 let carried = match direction {
6524 Direction::DependsOn => self
6525 .resolved_cache()?
6526 .get(id)
6527 .filter(|item| item.content_kind == ContentKind::Issue)
6528 .and_then(|item| Some((item.blocked_by.clone()?, item.raw_body.clone()))),
6529 Direction::DependedOnBy => None,
6530 }
6531 .filter(|(nodes, _)| recorded.is_some() || (cursor.is_none() && nodes.len() <= limit));
6532 // Asked for even in the recorded phase, whose page reads nothing from the
6533 // connection: `__typename` is what says whether this item has a native
6534 // relationship at all, and that is what decides which far ends the reserved key is
6535 // allowed to hold.
6536 let data = match carried {
6537 Some((nodes, body)) => json!({"node":{"__typename":"Issue","body":body,
6538 "blockedBy":{"nodes":nodes,"pageInfo":{"hasNextPage":false,"endCursor":null}}}}),
6539 None => {
6540 self.graphql(
6541 graphql::ISSUE_DEPENDENCIES,
6542 json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),
6543 "after":if recorded.is_some() {None} else {cursor}}),
6544 )
6545 .await?
6546 }
6547 };
6548 let node =
6549 data.get("node")
6550 .filter(|v| !v.is_null())
6551 .ok_or_else(|| SourceError::Refused {
6552 message: format!(
6553 "GitHub item {} was not found or does not support dependencies",
6554 id.0
6555 ),
6556 })?;
6557 let connection_name = match direction {
6558 Direction::DependsOn => "blockedBy",
6559 Direction::DependedOnBy => "blocking",
6560 };
6561 // A draft has neither `blockedBy` nor `blocking`, so nothing it depends on can be
6562 // named natively and the reserved key may hold any far end. An issue's connections
6563 // hold issues, and this source reads them at the near item's own level.
6564 let natively_names = (required_str(node, "__typename")? == "Issue").then_some(near_kind);
6565 if let Some(offset) = recorded {
6566 return Ok(recorded_page(
6567 self.recorded_edges(id, near_kind, direction, natively_names, node)
6568 .await?,
6569 offset,
6570 limit,
6571 ));
6572 }
6573 if natively_names.is_none() {
6574 return Ok(recorded_page(
6575 self.recorded_edges(id, near_kind, direction, natively_names, node)
6576 .await?,
6577 0,
6578 limit,
6579 ));
6580 }
6581 let connection = node
6582 .get(connection_name)
6583 .ok_or_else(|| SourceError::Malformed {
6584 message: "GitHub dependency response is missing its connection".into(),
6585 })?;
6586 let nodes = connection
6587 .get("nodes")
6588 .and_then(Value::as_array)
6589 .ok_or_else(|| SourceError::Malformed {
6590 message: "GitHub dependency response nodes is not an array".into(),
6591 })?;
6592 // `from` depends on `to`, always. GitHub spells the same relationship from either
6593 // end — `blockedBy` lists what this item waits on, `blocking` lists what waits on
6594 // it — so the near item is `from` in one direction and `to` in the other.
6595 let items = nodes
6596 .iter()
6597 .map(|value| {
6598 let related = NativeId(required_str(value, "id")?.into());
6599 let related_kind = related_kind(value)?;
6600 let (from, to) = match direction {
6601 Direction::DependsOn => (
6602 DependencyEndpoint::from_native(id.clone(), near_kind),
6603 DependencyEndpoint::from_native(related, related_kind),
6604 ),
6605 Direction::DependedOnBy => (
6606 DependencyEndpoint::from_native(related, related_kind),
6607 DependencyEndpoint::from_native(id.clone(), near_kind),
6608 ),
6609 };
6610 Ok(DependencyEdge {
6611 from,
6612 to,
6613 kind: DependencyKind::Blocks,
6614 })
6615 })
6616 .collect::<Result<Vec<_>, SourceError>>()?;
6617 let mut next = next_cursor(connection)?;
6618 if let Some(next) = &next {
6619 validate_cursor_progress(cursor, &next.0)?;
6620 }
6621 if next.is_none()
6622 && !self
6623 .recorded_edges(id, near_kind, direction, natively_names, node)
6624 .await?
6625 .is_empty()
6626 {
6627 next = Some(Cursor(format!("{RECORDED_CURSOR}0")));
6628 }
6629 Ok(Page { items, next })
6630 }
6631
6632 /// The edges this item records under [`DependencyEdge::RECORDED_KEY`], which is where
6633 /// a far end in another source has to live: no GitHub issue relationship can name one.
6634 ///
6635 /// Only forwards. The reverse of a recorded edge is derived from the far end, and this
6636 /// source never writes one down.
6637 ///
6638 /// The metadata lives in the item's own body slot, and `node` is the dependency read's
6639 /// own answer, which carries an issue's body — so an issue's recorded edges cost no
6640 /// request beyond the read already made, and reading the board for them would be a
6641 /// walk of every item for one field of one. A draft has no body in that answer, because
6642 /// a draft is not an issue, so a draft's are read off its own read by id — never off a
6643 /// listing of the board, which can be behind on the very item asked about.
6644 async fn recorded_edges(
6645 &self,
6646 id: &NativeId,
6647 near_kind: ItemKind,
6648 direction: Direction,
6649 natively_names: Option<ItemKind>,
6650 node: &Value,
6651 ) -> Result<Vec<DependencyEdge>, SourceError> {
6652 if direction != Direction::DependsOn {
6653 return Ok(Vec::new());
6654 }
6655 let slot = match node.get("body") {
6656 Some(body) if natively_names.is_some() => {
6657 metadata_body(body.as_str().map(str::to_owned))?.1
6658 }
6659 _ => {
6660 let Some(item) = self.bound_item(id).await? else {
6661 return Ok(Vec::new());
6662 };
6663 item.slot
6664 }
6665 };
6666 DependencyEdge::recorded(&slot, id, near_kind, &self.name, natively_names)
6667 .map_err(|message| SourceError::Malformed { message })
6668 }
6669
6670 fn configured_repository(&self) -> Result<&RepositoryTarget, SourceError> {
6671 self.repository
6672 .as_ref()
6673 .ok_or_else(|| SourceError::Refused {
6674 message: format!(
6675 "source {} has no repository configured, and a GitHub Projects board has no \
6676 repository of its own to create an issue in; set repository: owner/name on \
6677 this source",
6678 self.name
6679 ),
6680 })
6681 }
6682
6683 /// The repository one new issue is created in, under the rule [`RepositoryTarget`]
6684 /// states.
6685 ///
6686 /// The fallback is demanded first, whichever arm answers: a write without a configured
6687 /// repository is refused naming the field exactly as it was before the rule existed,
6688 /// so a source that could not write before cannot write now, rather than writing for
6689 /// the one item whose own field happens to decide it.
6690 ///
6691 /// Everything this refuses is refused before `createIssue`, so a refusal leaves no
6692 /// issue behind: an entry that is not a repository on [`RepositoryTarget::HOST`], an
6693 /// entry owned by someone other than the owner of the parent issue's repository —
6694 /// GitHub accepts a sub-issue from another repository of the same owner and from no
6695 /// other, so `addSubIssue` would refuse it after the issue existed — a parent the
6696 /// board does not hold, and a parent that is a draft, which GitHub gives no sub-issues,
6697 /// both of which `addSubIssue` would likewise refuse too late. Whether the entry exists
6698 /// and is visible to the token is checked where its node id is resolved, still before
6699 /// `createIssue`. The parent is read by its own id through [`Self::item_by_id`] — never
6700 /// looked up in a listing of the board, which can be minutes behind an issue its own
6701 /// `projectItems` already places on it — and that read answers first from this process's
6702 /// own record, so a project created moments ago in this command answers though GitHub
6703 /// has not caught up.
6704 async fn creation_target(
6705 &self,
6706 incoming: &Incoming<'_>,
6707 ) -> Result<RepositoryTarget, SourceError> {
6708 let fallback = self.configured_repository()?;
6709 let what = |incoming: &Incoming<'_>| {
6710 format!(
6711 "{} {:?}",
6712 incoming.written.kind().describes(),
6713 incoming.title
6714 )
6715 };
6716 let parent = match incoming.parent {
6717 Some(parent) => Some(self.bound_item(parent).await?.ok_or_else(|| {
6718 SourceError::Refused {
6719 message: format!(
6720 "GitHub project issue {} was not found on the board of source {}, so {} \
6721 cannot be filed under it",
6722 parent.0,
6723 self.name,
6724 what(incoming)
6725 ),
6726 }
6727 })?),
6728 None => None,
6729 };
6730 let parents_repository = parent
6731 .as_ref()
6732 .map(|parent| {
6733 // A draft is on the board and so is found, but it has no repository to
6734 // place a task in and GitHub gives it no sub-issues, so `addSubIssue`
6735 // would refuse the task only once `createIssue` had made it.
6736 if parent.content_kind == ContentKind::DraftIssue {
6737 return Err(SourceError::Refused {
6738 message: format!(
6739 "GitHub project item {} on the board of source {} is a draft, \
6740 which cannot have sub-issues, so {} cannot be filed under it",
6741 parent.id.0,
6742 self.name,
6743 what(incoming)
6744 ),
6745 });
6746 }
6747 // An issue's repository is where a sub-issue is placed and whose owner it
6748 // is compared against, so a parent whose repository this source cannot
6749 // spell as `owner/name` — GitHub's login grammar is wider than this
6750 // source's floor — is one nothing can be filed under.
6751 parent
6752 .own_repository
6753 .as_ref()
6754 .and_then(|origin| RepositoryTarget::from_origin(origin).ok())
6755 .ok_or_else(|| SourceError::Malformed {
6756 message: format!(
6757 "GitHub project issue {} on the board of source {} is in {}, which \
6758 is not a {}/owner/name repository this source can place {} in",
6759 parent.id.0,
6760 self.name,
6761 parent
6762 .own_repository
6763 .as_ref()
6764 .map_or("no repository", Repository::as_str),
6765 RepositoryTarget::HOST,
6766 what(incoming)
6767 ),
6768 })
6769 })
6770 .transpose()?;
6771 match incoming.repositories {
6772 [named] => {
6773 let target =
6774 RepositoryTarget::from_origin(named).map_err(|_| SourceError::Refused {
6775 message: format!(
6776 "{} names repository {}, which is not a {}/owner/name repository \
6777 source {} can create an issue in; name one that is, or name none",
6778 what(incoming),
6779 named.as_str(),
6780 RepositoryTarget::HOST,
6781 self.name
6782 ),
6783 })?;
6784 if let Some(parents) = &parents_repository
6785 && parents.owner != target.owner
6786 {
6787 return Err(SourceError::Refused {
6788 message: format!(
6789 "{} names repository {}, owned by {}, but its project's issue is in \
6790 {}, owned by {}, and GitHub files a sub-issue only in a repository \
6791 of the same owner as its parent issue; name a repository of {}, or \
6792 name none",
6793 what(incoming),
6794 target.slug(),
6795 target.owner,
6796 parents.slug(),
6797 parents.owner,
6798 parents.owner
6799 ),
6800 });
6801 }
6802 Ok(target)
6803 }
6804 _ => Ok(parents_repository.unwrap_or_else(|| fallback.clone())),
6805 }
6806 }
6807
6808 /// The node id of the repository `incoming` is being created in, or the refusal naming
6809 /// the item and the repository the token cannot see.
6810 ///
6811 /// Resolved once per command per repository; see [`Self::repository_cache`].
6812 async fn repository_id(
6813 &self,
6814 repository: &RepositoryTarget,
6815 incoming: &Incoming<'_>,
6816 ) -> Result<String, SourceError> {
6817 if let Some(id) = self.repository_cache()?.get(repository).cloned() {
6818 return Ok(id);
6819 }
6820 let data = self
6821 .graphql(
6822 graphql::REPOSITORY,
6823 json!({"owner":repository.owner,"name":repository.name}),
6824 )
6825 .await?;
6826 self.repository_read(&data, repository, incoming)
6827 }
6828
6829 /// The repository's node id out of an answer carrying the `repository` root, held for
6830 /// the rest of this command, or the refusal naming the item that cannot be created in it.
6831 fn repository_read(
6832 &self,
6833 data: &Value,
6834 repository: &RepositoryTarget,
6835 incoming: &Incoming<'_>,
6836 ) -> Result<String, SourceError> {
6837 let node = data
6838 .get("repository")
6839 .filter(|value| !value.is_null())
6840 .ok_or_else(|| SourceError::Refused {
6841 message: format!(
6842 "GitHub repository {} was not found or is not visible to the token, so {} \
6843 {:?} cannot be created in it",
6844 repository.slug(),
6845 incoming.written.kind().describes(),
6846 incoming.title
6847 ),
6848 })?;
6849 let id = required_str(node, "id")?.to_owned();
6850 self.repository_cache()?
6851 .insert(repository.clone(), id.clone());
6852 Ok(id)
6853 }
6854
6855 fn repository_cache(
6856 &self,
6857 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<RepositoryTarget, String>>, SourceError> {
6858 self.repository_cache
6859 .lock()
6860 .map_err(|_| SourceError::Unavailable {
6861 message: "this source's record of the destination repository was left \
6862 inconsistent by an earlier failure; next: run the command again"
6863 .into(),
6864 })
6865 }
6866
6867 /// Create or update one board item, whichever kind it is.
6868 async fn write_item(
6869 &self,
6870 incoming: &Incoming<'_>,
6871 target: Option<&NativeId>,
6872 depends_on: &[DependencyEdge],
6873 ) -> Result<NativeId, SourceError> {
6874 // Refused before anything is read or written: a task or a project titled the way
6875 // this board spells a document would land as an issue this same source reads back
6876 // as a document, so the field this destination cannot carry is named rather than
6877 // written and silently reclassified.
6878 if let Written::Work(kind, _) = incoming.written
6879 && incoming.title.starts_with(DESIGN_TITLE_PREFIX)
6880 {
6881 return Err(SourceError::Refused {
6882 message: format!(
6883 "the title of this {} begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
6884 spells a document, so it would read back as one rather than as a {}; \
6885 retitle it, or copy it as a document",
6886 kind.marker(),
6887 self.name,
6888 kind.marker()
6889 ),
6890 });
6891 }
6892 // The destination is read by its own id, and whether this board holds it is decided
6893 // by that read — its own `projectItems` — rather than by whether a listing of the
6894 // board happens to include it yet. See the module documentation.
6895 let existing = match target {
6896 Some(target) => {
6897 Some(
6898 self.bound_item(target)
6899 .await?
6900 .ok_or_else(|| SourceError::Refused {
6901 message: format!("GitHub destination item {} was not found", target.0),
6902 })?,
6903 )
6904 }
6905 None => None,
6906 };
6907 let existing = existing.as_ref();
6908 // An existing issue is never moved; a new one is created where the rule says — and
6909 // knowing where is what lets the board's fields and that repository's id be read
6910 // together, before anything below needs either.
6911 let creation_target = match existing {
6912 Some(_) => None,
6913 None => {
6914 let target = self.creation_target(incoming).await?;
6915 self.creation_context(&target, incoming).await?;
6916 Some(target)
6917 }
6918 };
6919 let board = self
6920 .fields_for(
6921 existing,
6922 incoming.written.status().is_some(),
6923 incoming
6924 .priority
6925 .is_some_and(|priority| priority != Priority::None),
6926 )
6927 .await?;
6928 let status_target = incoming
6929 .written
6930 .work_status()
6931 .map(|(kind, status)| self.resolved_target(kind, status.category))
6932 .transpose()?;
6933 let column = match (incoming.written.work_status(), status_target.as_ref()) {
6934 (Some((kind, status)), Some(target)) => {
6935 self.column_for(&board.fields, kind, status.category, target)?
6936 }
6937 _ => None,
6938 };
6939 // Resolved before anything is created, for the reason the column above is: a
6940 // priority this board has no option for is refused while nothing has been written.
6941 let priority_write = match incoming.priority {
6942 Some(priority) => self.priority_write(&board.fields, existing, priority)?,
6943 None => None,
6944 };
6945 let content_kind = existing.map_or(ContentKind::Issue, |item| item.content_kind);
6946 if content_kind == ContentKind::DraftIssue {
6947 if let (Some(StatusTarget::Terminal(_, _)), Some(status)) =
6948 (status_target.as_ref(), incoming.written.status())
6949 {
6950 return Err(self.closes_a_draft(status.category));
6951 }
6952 if incoming.parent.is_some() {
6953 return Err(SourceError::Refused {
6954 message: "GitHub draft items cannot be a project's sub-issue".into(),
6955 });
6956 }
6957 }
6958 match existing {
6959 Some(item) if content_kind == ContentKind::Issue => {
6960 if item.labels != incoming.labels {
6961 return Err(SourceError::Refused {
6962 message: "GitHub issue labels differ from the labels being written".into(),
6963 });
6964 }
6965 }
6966 _ => {
6967 if !incoming.labels.is_empty() {
6968 return Err(SourceError::Refused {
6969 message: "GitHub items created by this destination carry no labels".into(),
6970 });
6971 }
6972 }
6973 }
6974
6975 // The repository the issue really lives in is what the slot below is written against,
6976 // so a single entry that is where the issue is created travels as no key at all, and
6977 // the read side derives it back from the issue.
6978 let own_repository = match (existing, &creation_target) {
6979 (Some(item), _) => item.own_repository.clone(),
6980 (None, Some(target)) => Some(
6981 Repository::try_from(target.origin())
6982 .map_err(|message| SourceError::Config { message })?,
6983 ),
6984 (None, None) => None,
6985 };
6986 let (native, fallback) = self
6987 .partition_edges(
6988 incoming.written.kind(),
6989 content_kind,
6990 existing.and_then(|item| item.blocked_by.as_deref()),
6991 depends_on,
6992 )
6993 .await?;
6994 let slot = slot_metadata(incoming, own_repository.as_ref(), &fallback);
6995 let body = compose_body(incoming.content, &slot)?;
6996 // Read before anything is created, for the reason the field below is: a value
6997 // this destination cannot store has to refuse, and refusing after `createIssue`
6998 // would leave an issue behind that nothing asked for. The engine writes a
6999 // qualified id here; a caller handing this key anything else is told so rather
7000 // than having it silently stored as no origin at all.
7001 // 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.
7002 let origin = match incoming.metadata.get(ORIGIN_KEY) {
7003 None => "",
7004 Some(Value::String(origin)) => origin.as_str(),
7005 Some(other) => {
7006 return Err(SourceError::Refused {
7007 message: format!(
7008 "{ORIGIN_KEY} holds a qualified id spelled as a string, and this item's \
7009 is {other}"
7010 ),
7011 });
7012 }
7013 };
7014 // Resolved before anything is created: a board that cannot carry the copy origin
7015 // has to refuse the write, and refusing it after `createIssue` would leave an
7016 // issue behind that nothing asked for.
7017 let origin_field = match Board::field(&board.fields, ORIGIN_FIELD)? {
7018 Some(field) => {
7019 if required_str(field, "__typename")? != "ProjectV2Field" {
7020 return Err(SourceError::Refused {
7021 message: format!(
7022 "GitHub board source-owned {ORIGIN_FIELD} field is not a text field"
7023 ),
7024 });
7025 }
7026 Some(required_str(field, "id")?.to_owned())
7027 }
7028 None if incoming.metadata.contains_key(ORIGIN_KEY) => {
7029 return Err(SourceError::Refused {
7030 message: format!(
7031 "GitHub board has no source-owned {ORIGIN_FIELD} text field, and the \
7032 item carries {ORIGIN_KEY}; add a text field named {ORIGIN_FIELD} to \
7033 the board"
7034 ),
7035 });
7036 }
7037 None => None,
7038 };
7039
7040 let Landed {
7041 content_id,
7042 item_id,
7043 url,
7044 number,
7045 } = match existing {
7046 // Its content is written last, below, once everything else has landed.
7047 Some(item) => Landed {
7048 content_id: item.id.clone(),
7049 item_id: item.item_id.clone(),
7050 url: item.url.clone(),
7051 number: item.number,
7052 },
7053 None => {
7054 let target = creation_target
7055 .as_ref()
7056 .ok_or_else(|| SourceError::Malformed {
7057 message: "a new item was decided without a repository to create it in"
7058 .into(),
7059 })?;
7060 self.create_and_file_issue(board.id.as_str(), target, incoming, &body)
7061 .await?
7062 }
7063 };
7064
7065 let written_option = column.as_ref().map(|(_, _, name)| name.clone());
7066 let column = column
7067 .filter(|(_, _, name)| existing.is_none_or(|item| item.option.as_ref() != Some(name)))
7068 .map(|(field, option, _)| (field, option));
7069 // Creating an item here is several calls — `createIssue`, which files it on the
7070 // board, then its board fields, the parent and the dependencies — and GitHub can fail
7071 // at any of them. Everything this source can refuse *before* the first of those is
7072 // already checked above, so what is left is GitHub itself failing part way. When it
7073 // does over an item this call created, the issue is taken back: a write that
7074 // refused must not leave an item behind that nobody asked for, and one that does
7075 // makes the retry create a second.
7076 // Whether the board-field write carrying a moved origin was answered as landing whole.
7077 // When it was refused, GitHub does not say which of its fields ran before the one that
7078 // failed, so the origin may or may not have moved.
7079 let mut origin_landed = false;
7080 let landed = self
7081 .finish_write(
7082 board.id.as_str(),
7083 incoming,
7084 &content_id,
7085 &item_id,
7086 content_kind,
7087 existing,
7088 origin_field.as_deref(),
7089 origin,
7090 column,
7091 status_target.as_ref(),
7092 priority_write.as_ref(),
7093 &native,
7094 &mut origin_landed,
7095 )
7096 .await;
7097 // An existing item's title, body and state go last, in one `updateIssue`, once its board
7098 // fields and its relationships have landed: a refusal of any of those then leaves its
7099 // body — and the metadata slot inside it — exactly as it stood.
7100 let landed = match (landed, existing) {
7101 (Ok(()), Some(item)) => {
7102 self.update_existing(item, incoming, &body, status_target.as_ref())
7103 .await
7104 }
7105 (landed, _) => landed,
7106 };
7107 if let Err(error) = landed {
7108 match existing {
7109 // Best effort, and the write's own failure is what the caller is told: a
7110 // refusal naming the tidy-up would hide why the write failed at all.
7111 None => {
7112 let _ = self.delete_issue(&content_id).await;
7113 }
7114 // The origin field is the one piece of an existing item's metadata written
7115 // before its body, so a write refused after it puts it back as it was. When
7116 // that is refused too, the write's own failure is still what the caller is
7117 // told — with what it left behind added, because the item's metadata is then
7118 // not as it stood and a caller retrying has to know which key moved.
7119 Some(item) => {
7120 let before = item.origin.as_deref().unwrap_or("");
7121 if let Some(field) = origin_field.as_deref()
7122 && before != origin
7123 && let Err(restore) = self
7124 .set_item_field(
7125 board.id.as_str(),
7126 &item.item_id,
7127 field,
7128 json!({"text": before}),
7129 )
7130 .await
7131 {
7132 let left = if origin_landed {
7133 format!(
7134 "its {ORIGIN_KEY} was moved to {origin:?} before that and could \
7135 not be put back to {before:?} ({restore}), so item {} still \
7136 holds {origin:?} there",
7137 item.id.0
7138 )
7139 } else {
7140 format!(
7141 "the refused write carried its {ORIGIN_KEY} from {before:?} to \
7142 {origin:?}, GitHub does not say whether that part of it ran, \
7143 and putting it back to {before:?} was refused ({restore}), so \
7144 item {} holds {origin:?} or {before:?} there",
7145 item.id.0
7146 )
7147 };
7148 return Err(noting(
7149 error,
7150 &format!(
7151 "; {left}; next: set {ORIGIN_KEY} on it back to {before:?}, or \
7152 run the write again"
7153 ),
7154 ));
7155 }
7156 }
7157 }
7158 return Err(error);
7159 }
7160
7161 let written_status = match (incoming.written.work_status(), status_target.as_ref()) {
7162 (Some((kind, _)), Some(StatusTarget::Terminal(_, reason))) => {
7163 self.statuses
7164 .status(kind, written_option.as_deref(), true, Some(reason.reason()))
7165 }
7166 (Some((kind, _)), Some(StatusTarget::Column(_))) => {
7167 self.statuses
7168 .status(kind, written_option.as_deref(), false, None)
7169 }
7170 (Some((_, status)), _) => status.clone(),
7171 (None, _) => Status {
7172 category: StatusCategory::Unknown,
7173 name: "Open".to_owned(),
7174 },
7175 };
7176
7177 // So the rest of this command reads what it just did rather than what the board
7178 // said before it. See `remember_written` for which half takes it.
7179 let remembered = Resolved {
7180 item_id,
7181 id: content_id.clone(),
7182 content_kind,
7183 kind: incoming.written.kind(),
7184 title: incoming.title.to_owned(),
7185 // The visible half of the body this write composed, split back off it the
7186 // way a read splits it — so what this record reports is what a read of the
7187 // same issue reports, rather than the person's text with the metadata slot
7188 // still on the end of it.
7189 body: metadata_body(body.clone())?.0,
7190 raw_body: body.clone(),
7191 // A document has no status of its own; what it reads back as is whatever
7192 // the issue's own state says, which is what a re-read reports.
7193 status: written_status,
7194 option: written_option.or_else(|| existing.and_then(|item| item.option.clone())),
7195 priority: match incoming.priority {
7196 Some(priority) => HeldPriority::Read(priority),
7197 None => existing.map_or(HeldPriority::Read(Priority::None), |item| {
7198 item.priority.clone()
7199 }),
7200 },
7201 // What `state_input` asked for: closed for a terminal target, open for any other
7202 // status, and the issue's own state left as it was by a document write.
7203 closed: content_kind == ContentKind::Issue
7204 && match status_target.as_ref() {
7205 Some(StatusTarget::Terminal(_, _)) => true,
7206 Some(_) => false,
7207 None => existing.is_some_and(|item| item.closed),
7208 },
7209 delivers: incoming.delivers.to_vec(),
7210 delivered_by: incoming.delivered_by.to_vec(),
7211 labels: incoming.labels.to_vec(),
7212 parent: incoming.parent.cloned(),
7213 origin: (!origin.is_empty()).then(|| origin.to_owned()),
7214 number,
7215 // In the update path this is the item's own url, read off `existing` where the
7216 // record above was bound, so one expression serves both halves.
7217 url,
7218 created_at: existing.and_then(|item| item.created_at),
7219 updated_at: existing.and_then(|item| item.updated_at),
7220 own_repository,
7221 repositories: incoming.repositories.to_vec(),
7222 slot,
7223 board_id: Some(board.id.as_str().to_owned()),
7224 fields: board
7225 .fields
7226 .get("nodes")
7227 .and_then(Value::as_array)
7228 .cloned()
7229 .unwrap_or_default(),
7230 board_fields: Some(board.fields.clone()),
7231 // What this write left the relationship holding is known by id alone, and a
7232 // later read of its edges needs each far end's kind, so it reads them again.
7233 blocked_by: None,
7234 };
7235 self.remember_written(remembered, existing.is_none())?;
7236 Ok(content_id)
7237 }
7238
7239 /// Everything a write does after the item exists: its board fields, its parent, and
7240 /// its dependencies.
7241 ///
7242 /// Split out of `write_item` so there is one place a failure past the point of no
7243 /// return is caught, rather than a tidy-up repeated at each `?` above.
7244 // llmlint: ignore[suppressions_justified] This is the tail of `write_item` lifted out
7245 // so there is one place a failure past the point of no return is caught, and its
7246 // arguments are exactly the values that tail already had in scope. Bundling them into a
7247 // struct would describe no concept — it would be "the arguments of this function" — and
7248 // would put the whole of `write_item`'s locals behind one more indirection.
7249 #[allow(clippy::too_many_arguments)]
7250 async fn finish_write(
7251 &self,
7252 board_id: &str,
7253 incoming: &Incoming<'_>,
7254 content_id: &NativeId,
7255 item_id: &str,
7256 content_kind: ContentKind,
7257 existing: Option<&Resolved>,
7258 origin_field: Option<&str>,
7259 origin: &str,
7260 column: Option<(String, String)>,
7261 status_target: Option<&StatusTarget>,
7262 priority: Option<&PriorityWrite>,
7263 native: &[String],
7264 origin_landed: &mut bool,
7265 ) -> Result<(), SourceError> {
7266 let mut fields = Vec::new();
7267 if let Some(field_id) = origin_field
7268 && existing.map_or(!origin.is_empty(), |item| {
7269 item.origin.as_deref().unwrap_or("") != origin
7270 })
7271 {
7272 fields.push((field_id.to_owned(), json!({"text":origin})));
7273 }
7274 if let Some((field_id, option_id)) = column {
7275 fields.push((field_id, json!({"singleSelectOptionId":option_id})));
7276 }
7277 let clear = match priority {
7278 Some(PriorityWrite::Select { field, option }) => {
7279 fields.push((field.clone(), json!({"singleSelectOptionId":option})));
7280 None
7281 }
7282 Some(PriorityWrite::Clear { field }) => Some(field.as_str()),
7283 None => None,
7284 };
7285 self.set_item_fields(board_id, item_id, &fields, clear)
7286 .await?;
7287 *origin_landed = true;
7288
7289 // An existing issue closes in the `updateIssue` its write ends with; one created just
7290 // now closes here, once its option is selected.
7291 if existing.is_none()
7292 && content_kind == ContentKind::Issue
7293 && matches!(status_target, Some(StatusTarget::Terminal(_, _)))
7294 {
7295 self.update_content(
7296 ContentKind::Issue,
7297 content_id,
7298 json!({"stateInput":state_input(status_target)}),
7299 )
7300 .await?;
7301 }
7302
7303 if content_kind == ContentKind::Issue {
7304 self.reparent(
7305 existing.and_then(|item| item.parent.clone()),
7306 content_id,
7307 incoming.parent,
7308 )
7309 .await?;
7310 // A document takes part in no dependency graph, so writing one neither reads
7311 // nor changes the issue's own `blockedBy` relationships. Reconciling them
7312 // against the empty list a document write carries would *delete* whatever
7313 // relationships a person had made on that issue, which is a write nobody
7314 // asked for.
7315 if incoming.written.kind() != BoardKind::Document {
7316 let issue = match existing {
7317 Some(item) => Issue::Existing(item.blocked_by.as_deref()),
7318 None => Issue::Created,
7319 };
7320 self.reconcile_blocked_by(content_id, native, issue).await?;
7321 }
7322 }
7323 Ok(())
7324 }
7325
7326 /// Delete one issue, which takes its board item with it.
7327 async fn delete_issue(&self, id: &NativeId) -> Result<(), SourceError> {
7328 let data = self
7329 .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
7330 .await?;
7331 data.pointer("/deleteIssue/repository")
7332 .filter(|value| !value.is_null())
7333 .ok_or_else(|| SourceError::Malformed {
7334 message: "GitHub issue deletion returned no repository".into(),
7335 })?;
7336 self.forget(id)?;
7337 Ok(())
7338 }
7339
7340 /// Remove one item this copy created, so a copy that could not finish leaves the board
7341 /// as it found it.
7342 ///
7343 /// Deleting the issue takes its board item with it, so there is no second mutation to
7344 /// keep in step. An id the board does not hold is not an error: the item is already
7345 /// gone, which is the state this asks for. Which that is, is decided by reading the item
7346 /// by its own id — a listing of the board can still be missing an item it holds, and
7347 /// reading that as *already gone* would leave behind the very item this was asked to
7348 /// take back.
7349 async fn delete_item(&self, id: &NativeId) -> Result<(), SourceError> {
7350 let Some(item) = self.bound_item(id).await? else {
7351 return Ok(());
7352 };
7353 if item.content_kind == ContentKind::DraftIssue {
7354 return Err(SourceError::Refused {
7355 message: format!(
7356 "GitHub item {} is a draft, and this source removes an item by deleting \
7357 its issue; next: remove it from the board by hand",
7358 id.0
7359 ),
7360 });
7361 }
7362 let data = self
7363 .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
7364 .await?;
7365 data.pointer("/deleteIssue/repository")
7366 .filter(|value| !value.is_null())
7367 .ok_or_else(|| SourceError::Malformed {
7368 message: "GitHub issue deletion returned no repository".into(),
7369 })?;
7370 self.forget(id)?;
7371 Ok(())
7372 }
7373
7374 /// The issue a comment call on `task` is about, or `None` when this board holds no such
7375 /// task.
7376 ///
7377 /// Resolved exactly as [`TaskSource::get_task`] resolves it, so the comment verbs and a
7378 /// read of the task cannot disagree about which ids name one: a project or a document of
7379 /// this board is not a task here either.
7380 ///
7381 /// A **draft** is a task with nowhere to keep a comment, because GitHub keeps comments on
7382 /// issues and a draft is not one. It is refused rather than answered with an empty page,
7383 /// which would read as a task nobody has commented on yet.
7384 async fn commented_issue(&self, task: &NativeId) -> Result<Option<NativeId>, SourceError> {
7385 let cached = self.resolved_cache()?.get(task).cloned();
7386 let Some(item) = (match cached {
7387 Some(item) => Some(item),
7388 None => self.item_by_id(task).await?,
7389 })
7390 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
7391 return Ok(None);
7392 };
7393 if item.content_kind == ContentKind::DraftIssue {
7394 return Err(self.draft_has_no_comments(task));
7395 }
7396 Ok(Some(item.id))
7397 }
7398
7399 /// The refusal a comment call on a board draft is answered with: GitHub keeps comments on
7400 /// issues, and a draft is not one.
7401 fn draft_has_no_comments(&self, task: &NativeId) -> SourceError {
7402 SourceError::Refused {
7403 message: format!(
7404 "task {} of source {} is a draft item on the board, and GitHub keeps \
7405 comments on issues alone, so a draft has none to read or write; next: \
7406 convert the draft to an issue on the board, then comment on the issue it \
7407 becomes",
7408 task.0, self.name
7409 ),
7410 }
7411 }
7412
7413 /// One task and a page of its comments, read with [`graphql::ISSUE_DETAIL`] in one
7414 /// request — or `None` when this board holds no task by that id.
7415 ///
7416 /// What `task show` and a comment listing read. A draft is a task with no comments, so it
7417 /// is answered with the draft and the refusal, at the price of the draft's own read.
7418 async fn issue_detail(
7419 &self,
7420 id: &NativeId,
7421 page: &PageRequest,
7422 ) -> Result<Option<TaskDetailRead>, SourceError> {
7423 let after = page.cursor.as_ref().map(|cursor| cursor.0.as_str());
7424 let asked = self
7425 .graphql(
7426 graphql::ISSUE_DETAIL,
7427 json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),"after":after,
7428 "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
7429 "duplicates":true}),
7430 )
7431 .await;
7432 let data = match asked {
7433 Ok(data) => data,
7434 Err(error) if unresolvable_node(&error) => return Ok(None),
7435 Err(error) => return Err(error),
7436 };
7437 // `node` is null for an id that names nothing, and absent only from an answer this
7438 // source cannot read — never the same thing.
7439 let node = data.get("node").ok_or_else(|| SourceError::Malformed {
7440 message: format!("GitHub answered the read of {} with no node", id.0),
7441 })?;
7442 self.detail_of(id, node, true, after).await
7443 }
7444
7445 /// Several tasks, each with the first page of its comments when `comments` is set, read
7446 /// [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] — one answer per id, in
7447 /// order.
7448 ///
7449 /// A batch GitHub refuses because one of its ids resolves to no node at all is read again
7450 /// one item at a time, so that id is answered as missing and the others as themselves; any
7451 /// other refusal is every id of that batch's answer.
7452 async fn issue_details(
7453 &self,
7454 ids: &[NativeId],
7455 comments: Option<&PageRequest>,
7456 ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
7457 let mut read = Vec::with_capacity(ids.len());
7458 for batch in ids.chunks(DETAIL_BATCH) {
7459 match self
7460 .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, comments))
7461 .await
7462 {
7463 Ok(data) => {
7464 for (slot, id) in batch.iter().enumerate() {
7465 // Every alias asked for is answered, null for an id naming nothing;
7466 // one missing is an answer this source cannot read.
7467 let read_one = match data.get(format!("i{slot}")) {
7468 Some(node) => self.detail_of(id, node, comments.is_some(), None).await,
7469 None => Err(SourceError::Malformed {
7470 message: format!(
7471 "GitHub answered a batch read with no item for {}",
7472 id.0
7473 ),
7474 }),
7475 };
7476 read.push(read_one);
7477 }
7478 }
7479 Err(error) if unresolvable_node(&error) => {
7480 for id in batch {
7481 read.push(match comments {
7482 Some(page) => self.issue_detail(id, page).await,
7483 None => self.task_read(id).await,
7484 });
7485 }
7486 }
7487 Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
7488 }
7489 }
7490 read
7491 }
7492
7493 /// One task and nothing of its comments, as [`TaskSource::get_task`] reads it.
7494 async fn task_read(&self, id: &NativeId) -> Result<Option<TaskDetailRead>, SourceError> {
7495 Ok(self.get_task(id).await?.map(|task| TaskDetailRead {
7496 task,
7497 comments: None,
7498 }))
7499 }
7500
7501 /// What one node a detail read reached says: the task this board holds by `id`, with the
7502 /// page of comments the node carries when `commented` — or `None` for a node that is no
7503 /// task of this board.
7504 ///
7505 /// Resolved as [`Self::item_by_id`] resolves an item: a draft is read again as a draft,
7506 /// and an item this process created answers from this process's own record, which a node
7507 /// read taken moments after the write can still be behind.
7508 async fn detail_of(
7509 &self,
7510 id: &NativeId,
7511 node: &Value,
7512 commented: bool,
7513 after: Option<&str>,
7514 ) -> Result<Option<TaskDetailRead>, SourceError> {
7515 if node.is_null() {
7516 return Ok(None);
7517 }
7518 let draft = optional_str(node, "__typename")? == Some("DraftIssue");
7519 // An issue answered under one id is that id's, or the answer is not one this source
7520 // can report: reporting another issue's task and comments under the qualified id asked
7521 // for would be the one wrong answer here. A draft's own read checks the same.
7522 if !draft
7523 && optional_str(node, "__typename")? == Some("Issue")
7524 && required_str(node, "id")? != id.0
7525 {
7526 return Err(SourceError::Malformed {
7527 message: format!(
7528 "GitHub answered the read of {} with issue {}",
7529 id.0,
7530 required_str(node, "id")?
7531 ),
7532 });
7533 }
7534 let item = if draft {
7535 self.draft_by_id(id).await?
7536 } else {
7537 self.resolve_issue(node).await?
7538 };
7539 let Some(item) = item.filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
7540 return Ok(None);
7541 };
7542 let own = self.created()?.iter().find(|own| own.id == *id).cloned();
7543 let task = own.unwrap_or(item).task()?;
7544 let comments = match (commented, draft) {
7545 (false, _) => None,
7546 (true, true) => Some(Err(self.draft_has_no_comments(id))),
7547 (true, false) => Some(comment_page(node, &id.0, after).map(Some)),
7548 };
7549 Ok(Some(TaskDetailRead { task, comments }))
7550 }
7551
7552 /// Whether the comment `comment` is one of `issue`'s own.
7553 ///
7554 /// Read before an edit or a removal is sent, because GitHub's comment mutations take the
7555 /// comment's id and nothing else: a comment id given against the wrong task would
7556 /// otherwise change a comment on some other issue entirely. An id that names nothing, or
7557 /// names something that is not an issue comment, is a comment this task does not have —
7558 /// which is what GitHub refusing to resolve it means too.
7559 async fn comment_is_on(
7560 &self,
7561 issue: &NativeId,
7562 comment: &NativeId,
7563 ) -> Result<bool, SourceError> {
7564 let asked = self
7565 .graphql(graphql::COMMENT_ISSUE, json!({"id":comment.0}))
7566 .await;
7567 let data = match asked {
7568 Ok(data) => data,
7569 Err(error) if unresolvable_node(&error) => return Ok(false),
7570 Err(error) => return Err(error),
7571 };
7572 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
7573 return Ok(false);
7574 };
7575 if optional_str(node, "__typename")? != Some("IssueComment") {
7576 return Ok(false);
7577 }
7578 let on = node.get("issue").ok_or_else(|| SourceError::Malformed {
7579 message: format!("GitHub issue comment {} names no issue", comment.0),
7580 })?;
7581 Ok(required_str(on, "id")? == issue.0)
7582 }
7583
7584 /// Which far ends this item's own `blockedBy` relationship holds, and which it cannot.
7585 async fn partition_edges(
7586 &self,
7587 near_kind: BoardKind,
7588 near_content: ContentKind,
7589 carried: Option<&[Value]>,
7590 depends_on: &[DependencyEdge],
7591 ) -> Result<(Vec<String>, Vec<DependencyEdge>), SourceError> {
7592 let mut native = Vec::new();
7593 let mut fallback = Vec::new();
7594 let far_ends: Vec<(&DependencyEdge, &str, bool, Option<&Value>)> = depends_on
7595 .iter()
7596 .map(|edge| {
7597 let same_source = edge
7598 .to
7599 .source()
7600 .is_none_or(|source| source == self.name.as_str());
7601 // A qualified id's source segment runs to its *first* colon — `GlobalId` and
7602 // `DependencyEndpoint::source` both read it that way — and a native id may hold
7603 // colons of its own, so the far end is everything after that one separator.
7604 // Splitting at the last would truncate `work:urn:task:7` to `7`.
7605 let far_id = if edge.to.is_qualified() {
7606 edge.to
7607 .id()
7608 .split_once(':')
7609 .map_or(edge.to.id(), |(_, native)| native)
7610 } else {
7611 edge.to.id()
7612 };
7613 // One that already blocks the near issue was answered by that issue's own
7614 // read, which carried each of its blockers' kinds — an issue every one — so it
7615 // is not read again.
7616 let blocking = carried.and_then(|nodes| {
7617 nodes
7618 .iter()
7619 .find(|node| node.get("id").and_then(Value::as_str) == Some(far_id))
7620 });
7621 (edge, far_id, same_source, blocking)
7622 })
7623 .collect();
7624 // Every other same-source far end is read by its own id, exactly as the item it is a
7625 // far end of is: whether this board holds it is that read's answer, never a listing's.
7626 // They are read together, [`DETAIL_BATCH`] to a request, rather than one each.
7627 let mut unread: Vec<NativeId> = Vec::new();
7628 for (_, far_id, same_source, blocking) in &far_ends {
7629 let id = NativeId((*far_id).to_owned());
7630 if *same_source && blocking.is_none() && !unread.contains(&id) {
7631 unread.push(id);
7632 }
7633 }
7634 let read: BTreeMap<NativeId, Option<Resolved>> = unread
7635 .iter()
7636 .cloned()
7637 .zip(self.items_by_ids(&unread).await?)
7638 .collect();
7639 for (edge, far_id, same_source, blocking) in far_ends {
7640 let far = match (same_source, blocking) {
7641 (false, _) => None,
7642 (true, Some(node)) => Some(FarEnd {
7643 kind: if required_str(node, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
7644 BoardKind::Document
7645 } else {
7646 BoardKind::Work(related_kind(node)?)
7647 },
7648 content_kind: ContentKind::Issue,
7649 }),
7650 (true, None) => {
7651 let read = read
7652 .get(&NativeId(far_id.to_owned()))
7653 .cloned()
7654 .flatten()
7655 .ok_or_else(|| SourceError::Refused {
7656 message: format!("GitHub dependency item {far_id} was not found"),
7657 })?;
7658 Some(FarEnd {
7659 kind: read.kind,
7660 content_kind: read.content_kind,
7661 })
7662 }
7663 };
7664 let far = far.as_ref();
7665 // The caller says which kind the far end is, and this board holds the far end
7666 // itself, so a disagreement is settled here rather than stored: recorded, the
7667 // wrong kind would read back as a cross-level edge that never existed; written
7668 // natively, it would name a relationship of a different level than the caller
7669 // asked for.
7670 //
7671 // A far end this board holds as a *document* fails the same comparison and is
7672 // refused by the same sentence: `ItemKind` has no document variant because
7673 // nothing may point at one, so no caller can name it correctly and the refusal
7674 // is the only honest answer.
7675 if let Some(disagreeing) = far.filter(|far| far.kind != BoardKind::Work(edge.to.kind)) {
7676 return Err(SourceError::Refused {
7677 message: format!(
7678 "GitHub dependency item {far_id} is a {} of this board, and this item \
7679 names it as a {}; record the kind it is",
7680 disagreeing.kind.describes(),
7681 edge.to.kind.marker()
7682 ),
7683 });
7684 }
7685 // A draft has neither `blockedBy` nor `blocking`, so no edge of one is native
7686 // however the far end is spelled — and one classified native here would be
7687 // written nowhere at all, because a draft's native reconciliation never runs.
7688 let native_here = near_content == ContentKind::Issue
7689 && far.is_some_and(|far| {
7690 far.content_kind == ContentKind::Issue
7691 && BoardKind::Work(edge.to.kind) == near_kind
7692 });
7693 if native_here {
7694 native.push(far_id.to_owned());
7695 } else {
7696 fallback.push(edge.clone());
7697 }
7698 }
7699 Ok((native, fallback))
7700 }
7701
7702 async fn update_existing(
7703 &self,
7704 item: &Resolved,
7705 incoming: &Incoming<'_>,
7706 body: &Option<String>,
7707 status_target: Option<&StatusTarget>,
7708 ) -> Result<(), SourceError> {
7709 let title = incoming.written_title();
7710 // A terminal status closes the issue here, in the same mutation as its body: its board
7711 // option was selected before this, so a close never lands on an item whose board cannot
7712 // show it.
7713 let fields = match item.content_kind {
7714 ContentKind::DraftIssue => json!({"title":title,"body":body}),
7715 ContentKind::Issue => json!({"title":title,"body":body,
7716 "stateInput":state_input(status_target)}),
7717 };
7718 self.update_content(item.content_kind, &item.id, fields)
7719 .await
7720 }
7721
7722 /// Update one board item's content with exactly `fields` beside its id, through the
7723 /// mutation its kind takes: `updateIssue` for an issue, `updateProjectV2DraftIssue` for
7724 /// a draft.
7725 ///
7726 /// Every input field either mutation leaves out is a field GitHub leaves as it is, which
7727 /// is what lets a narrow write carry the one thing it changes and nothing else.
7728 async fn update_content(
7729 &self,
7730 kind: ContentKind,
7731 id: &NativeId,
7732 fields: Value,
7733 ) -> Result<(), SourceError> {
7734 let (operation, id_key, pointer) = match kind {
7735 ContentKind::DraftIssue => (
7736 graphql::UPDATE_DRAFT,
7737 "draftIssueId",
7738 "/updateProjectV2DraftIssue/draftIssue",
7739 ),
7740 ContentKind::Issue => (graphql::UPDATE_ISSUE, "id", "/updateIssue/issue"),
7741 };
7742 let mut input = fields;
7743 input[id_key] = json!(id.0);
7744 let data = self.graphql(operation, json!({"input":input})).await?;
7745 let returned = data
7746 .pointer(pointer)
7747 .ok_or_else(|| SourceError::Malformed {
7748 message: "GitHub item update returned no item".into(),
7749 })?;
7750 if required_str(returned, "id")? != id.0 {
7751 return Err(SourceError::Malformed {
7752 message: "GitHub item update returned the wrong item".into(),
7753 });
7754 }
7755 Ok(())
7756 }
7757
7758 /// Creates one issue, files it on the board, and reports what a read of it would say:
7759 /// its content id, its board item id, and the web address GitHub gave it.
7760 ///
7761 /// Two calls rather than one: `createIssue` answers with an issue that is on no board,
7762 /// and `addProjectV2ItemById` is what puts it there. Filing it at creation through
7763 /// `CreateIssueInput.projectV2Ids` was tried and is not done: GitHub answered with no
7764 /// board item, and the `addProjectV2ItemById` that then had to follow was refused
7765 /// "Content already exists in this project". A terminal status is not written here:
7766 /// `finish_write` selects its option first and closes the issue after, so a close never
7767 /// lands on an item whose board cannot show it.
7768 ///
7769 /// The address and the number come back here because this is the only place either is
7770 /// known before GitHub's own board read catches up — an item this run created answers
7771 /// the reads that follow it out of the record below, and one remembered without them
7772 /// would report no location and no key for the rest of the run.
7773 async fn create_and_file_issue(
7774 &self,
7775 board_id: &str,
7776 repository: &RepositoryTarget,
7777 incoming: &Incoming<'_>,
7778 body: &Option<String>,
7779 ) -> Result<Landed, SourceError> {
7780 let repository_id = self.repository_id(repository, incoming).await?;
7781 let data = self
7782 .graphql(
7783 graphql::CREATE_ISSUE,
7784 json!({"input":{
7785 "repositoryId":repository_id,"title":incoming.written_title(),"body":body
7786 }}),
7787 )
7788 .await?;
7789 let created = data
7790 .pointer("/createIssue/issue")
7791 .filter(|value| !value.is_null())
7792 .ok_or_else(|| SourceError::Malformed {
7793 message: "GitHub issue creation returned no issue".into(),
7794 })?;
7795 let content_id = NativeId(required_str(created, "id")?.to_owned());
7796 // Optional although GitHub's schema makes it non-null: the issue exists by now, so
7797 // a response without it is not worth failing a landed write over — the item simply
7798 // reports no location until the board read catches up, which is what it did before.
7799 let url = optional_str(created, "url")?.map(str::to_owned);
7800 // The issue exists from here on, so an unreadable number and a refused board
7801 // filing below each try, best effort, to take it back: an issue in the repository
7802 // that is on no board is an item nobody asked for and nothing here would find again.
7803 //
7804 // Its number is optional on the same terms its address is — a landed write is not
7805 // worth failing over a member that came back missing, and such an item reports no
7806 // handle until a board read catches up. A number that is *present* and is not an
7807 // unsigned integer is still a response this source cannot read.
7808 let number = match created_issue_number(created) {
7809 Ok(number) => number,
7810 Err(error) => {
7811 let _ = self.delete_issue(&content_id).await;
7812 return Err(error);
7813 }
7814 };
7815 let added = match self
7816 .graphql(
7817 graphql::ADD_TO_BOARD,
7818 json!({"input":{"projectId":board_id,"contentId":content_id.0}}),
7819 )
7820 .await
7821 {
7822 Ok(added) => added,
7823 Err(error) => {
7824 let _ = self.delete_issue(&content_id).await;
7825 return Err(error);
7826 }
7827 };
7828 let item = added
7829 .pointer("/addProjectV2ItemById/item")
7830 .filter(|value| !value.is_null())
7831 .ok_or_else(|| SourceError::Malformed {
7832 message: "GitHub board addition returned no project item".into(),
7833 })?;
7834 Ok(Landed {
7835 content_id,
7836 item_id: required_str(item, "id")?.to_owned(),
7837 url,
7838 number,
7839 })
7840 }
7841
7842 /// Move one issue under the project it now belongs to, or out of the one it left.
7843 async fn reparent(
7844 &self,
7845 held: Option<NativeId>,
7846 child: &NativeId,
7847 wanted: Option<&NativeId>,
7848 ) -> Result<(), SourceError> {
7849 if held.as_ref() == wanted {
7850 return Ok(());
7851 }
7852 if let Some(held) = &held {
7853 self.sub_issue(graphql::REMOVE_SUB_ISSUE, held, child, "removeSubIssue")
7854 .await?;
7855 }
7856 if let Some(wanted) = wanted {
7857 self.sub_issue(graphql::ADD_SUB_ISSUE, wanted, child, "addSubIssue")
7858 .await?;
7859 }
7860 Ok(())
7861 }
7862
7863 async fn sub_issue(
7864 &self,
7865 operation: &str,
7866 parent: &NativeId,
7867 child: &NativeId,
7868 root: &str,
7869 ) -> Result<(), SourceError> {
7870 let data = self
7871 .graphql(
7872 operation,
7873 json!({"input":{"issueId":parent.0,"subIssueId":child.0}}),
7874 )
7875 .await?;
7876 let issue =
7877 data.pointer(&format!("/{root}/issue"))
7878 .ok_or_else(|| SourceError::Malformed {
7879 message: "GitHub sub-issue update returned no issue".into(),
7880 })?;
7881 let sub =
7882 data.pointer(&format!("/{root}/subIssue"))
7883 .ok_or_else(|| SourceError::Malformed {
7884 message: "GitHub sub-issue update returned no sub-issue".into(),
7885 })?;
7886 if required_str(issue, "id")? != parent.0 || required_str(sub, "id")? != child.0 {
7887 return Err(SourceError::Malformed {
7888 message: "GitHub sub-issue update returned the wrong issues".into(),
7889 });
7890 }
7891 Ok(())
7892 }
7893
7894 /// Bring one issue's `blockedBy` to exactly `native`, sending only the difference, and say
7895 /// whether there was one.
7896 ///
7897 /// An issue [`Issue::Created`] by this very write is blocked by nothing yet, so its
7898 /// relationships are not read: there is nothing a read of them could find.
7899 async fn reconcile_blocked_by(
7900 &self,
7901 content_id: &NativeId,
7902 native: &[String],
7903 issue: Issue<'_>,
7904 ) -> Result<bool, SourceError> {
7905 let current = match issue {
7906 Issue::Created => Vec::new(),
7907 Issue::Existing(Some(held)) => held
7908 .iter()
7909 .map(|far| required_str(far, "id").map(str::to_owned))
7910 .collect::<Result<Vec<_>, _>>()?,
7911 Issue::Existing(None) => self.native_dependency_ids(content_id).await?,
7912 };
7913 let mut changed = false;
7914 for (operation, far_id) in current
7915 .iter()
7916 .filter(|id| !native.contains(id))
7917 .map(|id| (graphql::REMOVE_BLOCKED_BY, id))
7918 .chain(
7919 native
7920 .iter()
7921 .filter(|id| !current.contains(id))
7922 .map(|id| (graphql::ADD_BLOCKED_BY, id)),
7923 )
7924 {
7925 let data = self
7926 .graphql(
7927 operation,
7928 json!({"input":{"issueId":content_id.0,"blockingIssueId":far_id}}),
7929 )
7930 .await?;
7931 let root = if operation == graphql::ADD_BLOCKED_BY {
7932 "addBlockedBy"
7933 } else {
7934 "removeBlockedBy"
7935 };
7936 let issue =
7937 data.pointer(&format!("/{root}/issue"))
7938 .ok_or_else(|| SourceError::Malformed {
7939 message: "GitHub dependency update returned no issue".into(),
7940 })?;
7941 let blocker = data
7942 .pointer(&format!("/{root}/blockingIssue"))
7943 .ok_or_else(|| SourceError::Malformed {
7944 message: "GitHub dependency update returned no blocking issue".into(),
7945 })?;
7946 if required_str(issue, "id")? != content_id.0 || required_str(blocker, "id")? != far_id
7947 {
7948 return Err(SourceError::Malformed {
7949 message: "GitHub dependency update returned the wrong issues".into(),
7950 });
7951 }
7952 changed = true;
7953 }
7954 Ok(changed)
7955 }
7956}
7957
7958/// What a write needs to know of one far end it names: which kind of item it is, and whether
7959/// it is an issue a native relationship can name.
7960struct FarEnd {
7961 kind: BoardKind,
7962 content_kind: ContentKind,
7963}
7964
7965/// Whether the issue one write reconciles was created by that write or was already there.
7966#[derive(Clone, Copy, PartialEq, Eq)]
7967enum Issue<'a> {
7968 /// Created by this write, so it holds no relationships yet.
7969 Created,
7970 /// On the board before this write, holding whatever relationships it holds — the far
7971 /// ends of its whole `blockedBy`, when the read that reached it carried them.
7972 Existing(Option<&'a [Value]>),
7973}
7974
7975/// What resolving one node id reached; see [`GitHubProjectsSource::reach`].
7976enum Reached {
7977 /// An issue this board holds, resolved into everything this source reports about it.
7978 Held(Box<Resolved>),
7979 /// Nothing this board holds: no such node, or a node on some other board.
7980 Nothing,
7981 /// A board draft, which [`graphql::ISSUE`] reaches and reads nothing of, so it is read
7982 /// again by [`GitHubProjectsSource::draft_by_id`].
7983 Draft,
7984}
7985
7986/// What GitHub says when a string is not a node id it can resolve.
7987///
7988/// Matched because it is the ordinary answer to a project selector naming a project by its
7989/// *name*, and reporting that as a failure would make naming one impossible. It is read
7990/// off the refusal GitHub sent, never guessed from the shape of the string: this source
7991/// does not define the syntax of a GitHub node id and would be wrong about it.
7992const UNRESOLVABLE_NODE: &str = "could not resolve to a node";
7993
7994/// `error` with `note` added to the end of what it says, its kind and every other member
7995/// unchanged — so a caller still branches on the failure that happened, and reads beside it
7996/// what that failure left behind.
7997fn noting(error: SourceError, note: &str) -> SourceError {
7998 match error {
7999 SourceError::Config { message } => SourceError::Config {
8000 message: message + note,
8001 },
8002 SourceError::Auth { message } => SourceError::Auth {
8003 message: message + note,
8004 },
8005 SourceError::Refused { message } => SourceError::Refused {
8006 message: message + note,
8007 },
8008 SourceError::RateLimited {
8009 retry_after_seconds,
8010 message,
8011 } => SourceError::RateLimited {
8012 retry_after_seconds,
8013 message: Some(message.unwrap_or_default() + note),
8014 },
8015 SourceError::Unavailable { message } => SourceError::Unavailable {
8016 message: message + note,
8017 },
8018 SourceError::Malformed { message } => SourceError::Malformed {
8019 message: message + note,
8020 },
8021 }
8022}
8023
8024/// The variables of one [`graphql::ISSUE_DETAILS`] request over `batch` — at most
8025/// [`DETAIL_BATCH`] ids — each item with the first page of its comments when `comments` asks
8026/// for them.
8027///
8028/// The document is fixed-size, so a slot `batch` has no id for is bound to its last id, which
8029/// is read again at no added price.
8030fn detail_batch(batch: &[NativeId], comments: Option<&PageRequest>) -> Value {
8031 let mut variables = serde_json::Map::new();
8032 for slot in 0..DETAIL_BATCH {
8033 let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
8034 variables.insert(format!("id{slot}"), json!(id));
8035 }
8036 variables.insert(
8037 "first".to_owned(),
8038 json!(comments.map_or(MAX_PAGE_SIZE, |page| page.limit.min(MAX_PAGE_SIZE))),
8039 );
8040 variables.insert("comments".to_owned(), json!(comments.is_some()));
8041 variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
8042 variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
8043 variables.insert("duplicates".to_owned(), json!(true));
8044 Value::Object(variables)
8045}
8046
8047/// Whether this refusal is GitHub saying the id names no node at all.
8048fn unresolvable_node(error: &SourceError) -> bool {
8049 matches!(error, SourceError::Refused { message }
8050 if message.to_ascii_lowercase().contains(UNRESOLVABLE_NODE))
8051}
8052
8053/// One project name, as a search qualifier which filters on it at the server.
8054///
8055/// Quoted so the whole title is one phrase rather than a bag of words, with the two
8056/// characters GitHub's own quoting grammar gives a meaning inside a quoted phrase escaped
8057/// the way it documents. A title matched here is still compared for equality afterwards:
8058/// the qualifier narrows what the server sends, and this source decides what it names.
8059fn title_qualifier(name: &str) -> String {
8060 format!("in:title {}", quoted(name))
8061}
8062
8063/// `value` as one quoted phrase of a GitHub search or a board filter, with the two
8064/// characters GitHub's quoting grammar gives a meaning inside a quoted phrase escaped the way
8065/// it documents — so a value holding a qualifier's spelling is searched for rather than
8066/// obeyed.
8067fn quoted(value: &str) -> String {
8068 let escaped = value.replace('\\', "\\\\").replace('"', "\\\"");
8069 format!("\"{escaped}\"")
8070}
8071
8072/// The search qualifier for the issues updated at or after `since`.
8073///
8074/// Written to the second, rounded down, which can only widen what the search returns.
8075fn updated_qualifier(since: DateTime<Utc>) -> String {
8076 format!("updated:>={}", since.format("%Y-%m-%dT%H:%M:%S+00:00"))
8077}
8078
8079/// The search terms that narrow a board-scoped issue search to a task query's text and
8080/// metadata predicates, or `None` when it carries neither.
8081///
8082/// The text is one quoted phrase, searched `in:title`, `in:body` or both as its fields say,
8083/// and each metadata value is one more quoted phrase, which GitHub finds in the body because
8084/// its index covers the metadata comment the value is stored in. GitHub ANDs the phrases and
8085/// matches each in any field the `in:` qualifier names, so a query naming a title search and
8086/// a metadata value searches both fields for both — wider than asked, never narrower, and
8087/// every candidate is confirmed in process afterwards.
8088///
8089/// **This narrows a text search, and that is this source's declared semantics.** GitHub
8090/// matches whole tokens where a substring rule would match inside a word, so an item holding
8091/// the text only inside a longer word is not returned. A text of nothing but whitespace
8092/// matches every item, so it narrows nothing and is not sent.
8093fn narrowing_qualifiers(query: &TaskQuery) -> Option<String> {
8094 let text = query
8095 .text
8096 .as_ref()
8097 .filter(|text| !text.terms.trim().is_empty());
8098 if text.is_none() && query.metadata.is_empty() {
8099 return None;
8100 }
8101 let (title, body) = match text.map(|text| text.fields) {
8102 None => (false, true),
8103 Some(TextFields::Title) => (true, !query.metadata.is_empty()),
8104 Some(TextFields::Content) => (false, true),
8105 Some(TextFields::TitleOrContent) => (true, true),
8106 };
8107 let fields = match (title, body) {
8108 (true, true) => "in:title,body",
8109 (true, false) => "in:title",
8110 _ => "in:body",
8111 };
8112 let phrases = text
8113 .map(|text| text.terms.clone())
8114 .into_iter()
8115 .chain(
8116 query
8117 .metadata
8118 .iter()
8119 .map(|wanted| as_stored(wanted.value())),
8120 )
8121 .map(|phrase| quoted(&phrase))
8122 .collect::<Vec<_>>();
8123 Some(format!("{fields} {}", phrases.join(" ")))
8124}
8125
8126/// The search terms that narrow a board-scoped issue search to a project or document query's
8127/// text, or `None` when it has none or a blank one: the phrase, in the fields, a task query
8128/// carrying that text alone is sent as by [`narrowing_qualifiers`].
8129fn text_qualifiers(text: Option<&TextQuery>) -> Option<String> {
8130 narrowing_qualifiers(&TaskQuery {
8131 text: text.cloned(),
8132 ..TaskQuery::default()
8133 })
8134}
8135
8136/// Refuses a project or document query's text GitHub's issue search cannot find, before
8137/// anything is asked of GitHub, on exactly the terms [`refuse_unsearchable`] refuses a task
8138/// query's.
8139fn refuse_unsearchable_text(text: Option<&TextQuery>) -> Result<(), SourceError> {
8140 refuse_unsearchable(&TaskQuery {
8141 text: text.cloned(),
8142 ..TaskQuery::default()
8143 })
8144}
8145
8146/// Refuses a task query naming a text or a metadata value GitHub's issue search cannot find,
8147/// before anything is asked of GitHub.
8148///
8149/// GitHub's index holds words, so a phrase with no letter or digit names none to find, and no
8150/// bounded query answers it: sent, GitHub's answer to it is nothing this source may rely on;
8151/// left out, the search is every issue of the board. So this source says it cannot answer
8152/// rather than reading the board or answering nothing. A blank text is not refused: it narrows
8153/// nothing GitHub could search for, and keeps the board read it always had.
8154fn refuse_unsearchable(query: &TaskQuery) -> Result<(), SourceError> {
8155 const WHY: &str = "GitHub's issue search indexes words, so it cannot answer a value with no \
8156 letter or digit with a bounded query";
8157 if let Some(text) = &query.text
8158 && !text.terms.trim().is_empty()
8159 && !has_words(&text.terms)
8160 {
8161 return Err(SourceError::Refused {
8162 message: format!(
8163 "cannot search for the text {:?}: {WHY}; search for a text holding a letter or a digit",
8164 text.terms
8165 ),
8166 });
8167 }
8168 if let Some(wanted) = query
8169 .metadata
8170 .iter()
8171 .find(|wanted| !has_words(wanted.value()))
8172 {
8173 return Err(SourceError::Refused {
8174 message: format!(
8175 "cannot filter by the metadata value {:?} at {:?}: {WHY}; filter by a value holding a letter or a digit",
8176 wanted.value(),
8177 std::iter::once(wanted.key())
8178 .chain(wanted.path().iter().map(String::as_str))
8179 .collect::<Vec<_>>()
8180 .join("/"),
8181 ),
8182 });
8183 }
8184 Ok(())
8185}
8186
8187/// Whether GitHub's index could hold a word of `phrase`: whether it has a letter or a digit.
8188fn has_words(phrase: &str) -> bool {
8189 phrase.chars().any(char::is_alphanumeric)
8190}
8191
8192/// `value` spelled the way the metadata slot stores it: as the inside of its JSON string.
8193///
8194/// What GitHub indexes is the slot's JSON text, so a value holding a character JSON escapes —
8195/// a newline, a tab, a quote — is found by the escape the body holds and not by the character,
8196/// which GitHub's word match would read as different words.
8197fn as_stored(value: &str) -> String {
8198 let encoded = Value::String(value.to_owned()).to_string();
8199 encoded[1..encoded.len() - 1].to_owned()
8200}
8201
8202/// The one narrower question a task query carrying a text, metadata or origin predicate is
8203/// sent as.
8204enum Narrowing {
8205 /// Every carrier of this origin: [`graphql::ORIGIN_LOOKUP`].
8206 Origin(String),
8207 /// The board-scoped issue search narrowed by these qualifiers.
8208 Search(String),
8209}
8210
8211impl Narrowing {
8212 /// What this question is remembered under for the length of one command.
8213 fn key(&self) -> String {
8214 match self {
8215 Self::Origin(origin) => format!("origin {origin}"),
8216 Self::Search(also) => format!("search {also}"),
8217 }
8218 }
8219}
8220
8221/// Where one connection of [`graphql::ORIGIN_LOOKUP`] resumes.
8222enum Resumed {
8223 /// It reported another page, which starts after this cursor.
8224 More(String),
8225 /// It has ended. Sending this cursor again — the page's own end when it had one, and
8226 /// otherwise the cursor it was reached from — answers an empty page, so the one document
8227 /// can go on walking the other connection.
8228 Ended(Option<String>),
8229}
8230
8231impl Resumed {
8232 /// Whether the connection has another page.
8233 const fn has_more(&self) -> bool {
8234 matches!(self, Self::More(_))
8235 }
8236
8237 /// The cursor to send this connection next.
8238 fn cursor(self) -> Option<String> {
8239 match self {
8240 Self::More(next) => Some(next),
8241 Self::Ended(last) => last,
8242 }
8243 }
8244}
8245
8246/// Where `connection`, reached from `after`, resumes — refused when it reports another page
8247/// with no cursor to it, or from a cursor that does not advance.
8248fn resumed(connection: &Value, after: Option<&str>) -> Result<Resumed, SourceError> {
8249 let info = connection
8250 .get("pageInfo")
8251 .ok_or_else(|| SourceError::Malformed {
8252 message: "GitHub connection has no pageInfo".into(),
8253 })?;
8254 let end = optional_str(info, "endCursor")?;
8255 if required_bool(info, "hasNextPage")? {
8256 let next = end.ok_or_else(|| SourceError::Malformed {
8257 message: "GitHub connection reports another page and no endCursor".into(),
8258 })?;
8259 validate_cursor_progress(after, next)?;
8260 return Ok(Resumed::More(next.to_owned()));
8261 }
8262 Ok(Resumed::Ended(
8263 end.map(str::to_owned).or_else(|| after.map(str::to_owned)),
8264 ))
8265}
8266
8267/// The board, and every item on it this source reports.
8268#[derive(Clone)]
8269struct Board {
8270 id: String,
8271 fields: Value,
8272 items: Vec<Resolved>,
8273}
8274
8275/// What a write needs of the board and nothing more: its node id and its field
8276/// definitions, in the shape a read of the board's own `fields` gives them.
8277///
8278/// Deliberately no items. A write decides which item it writes, which parent it files
8279/// under and which far ends it names by reading each of them by its own id; this is the
8280/// half of the board those reads cannot carry, and holding no item is what keeps it from
8281/// ever being asked whether an item is there.
8282#[derive(Clone)]
8283struct BoardFields {
8284 id: BoardId,
8285 fields: Value,
8286}
8287
8288/// A board's node id: what a field write and `addProjectV2ItemById` address.
8289///
8290/// Never blank, because a blank one addresses no board — so an id GitHub answers blank is
8291/// refused where it is read, and one an item names blank is read as not named at all.
8292#[derive(Clone)]
8293struct BoardId(String);
8294
8295/// Where one write left its item, for the record the rest of the command reads it out of.
8296///
8297/// A named record rather than a tuple because the update arm and the create arm each fill
8298/// all four, and two `Option`s of different meaning side by side in a tuple are two
8299/// positions a reader has to count.
8300struct Landed {
8301 /// The issue's own node id, which is the [`NativeId`] this source reports.
8302 content_id: NativeId,
8303 /// The board item's id, which is what a field write addresses.
8304 // 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.
8305 item_id: String,
8306 /// The web address GitHub gave the issue, when it gave one.
8307 // 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.
8308 url: Option<String>,
8309 /// The issue's number on its repository, when GitHub reported one.
8310 number: Option<u64>,
8311}
8312
8313impl BoardId {
8314 fn parse(id: &str) -> Result<Self, SourceError> {
8315 if id.trim().is_empty() {
8316 return Err(SourceError::Malformed {
8317 message: "GitHub named a board with a blank node id".into(),
8318 });
8319 }
8320 Ok(Self(id.to_owned()))
8321 }
8322
8323 fn as_str(&self) -> &str {
8324 &self.0
8325 }
8326}
8327
8328impl Board {
8329 fn field<'a>(fields: &'a Value, name: &str) -> Result<Option<&'a Value>, SourceError> {
8330 complete_connection(fields, "project fields", NESTED_PAGE_SIZE)?;
8331 let nodes = fields
8332 .get("nodes")
8333 .and_then(Value::as_array)
8334 .ok_or_else(|| SourceError::Malformed {
8335 message: "GitHub project fields.nodes is not an array".into(),
8336 })?;
8337 Ok(nodes
8338 .iter()
8339 .find(|field| field.get("name").and_then(Value::as_str) == Some(name)))
8340 }
8341}
8342
8343/// One board item, resolved into everything this source reports about it.
8344#[derive(Clone)]
8345struct Resolved {
8346 item_id: String,
8347 id: NativeId,
8348 content_kind: ContentKind,
8349 kind: BoardKind,
8350 title: String,
8351 body: Option<String>,
8352 /// The body exactly as GitHub holds it, metadata slot and all, which is what a write
8353 /// that changes the slot alone has to keep byte for byte outside it.
8354 raw_body: Option<String>,
8355 status: Status,
8356 /// The name of the board `Status` option this item sits in, as the board spells it.
8357 option: Option<String>,
8358 /// What its `Priority` field says, read through this instance's mapping.
8359 priority: HeldPriority,
8360 /// Whether this item's issue is closed. A draft has no such state and is never closed.
8361 closed: bool,
8362 /// The tasks this one delivers, read out of its slot. Empty for anything not a task.
8363 delivers: Vec<TaskRef>,
8364 /// Every task that delivers this one, read out of its slot. Empty for anything not a
8365 /// task.
8366 delivered_by: Vec<TaskRef>,
8367 labels: Vec<Label>,
8368 parent: Option<NativeId>,
8369 // 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.
8370 origin: Option<String>,
8371 /// The issue's own number on its repository, as GitHub reports it.
8372 ///
8373 /// `None` in exactly two cases: a draft, which has no number at all — `DraftIssue`
8374 /// declares none, and a draft is not filed in a repository to be numbered by one — and
8375 /// an issue this run created whose creating mutation answered without one, which is a
8376 /// response GitHub's own schema says cannot happen and which a landed write is not
8377 /// worth failing over. An `Issue` read off the board always has one.
8378 number: Option<u64>,
8379 url: Option<String>,
8380 created_at: Option<DateTime<Utc>>,
8381 updated_at: Option<DateTime<Utc>>,
8382 own_repository: Option<Repository>,
8383 repositories: Vec<Repository>,
8384 slot: BTreeMap<String, Value>,
8385 /// The node id of the board this item sits on, when the read that reached it said.
8386 board_id: Option<String>,
8387 /// The definition of every board field this item holds a value of, in the shape a read
8388 /// of the board's own `fields` gives one.
8389 ///
8390 /// Only the fields this item has a value in: a field it holds nothing of is not here,
8391 /// which says nothing about whether the board has it.
8392 fields: Vec<Value>,
8393 /// Every field the board this item sits on defines, as its own read of the board's
8394 /// `fields` gives them — when the read that reached the item carried them, which a read
8395 /// of it by its own id does. What a write of it needs of the board, then, needs no read
8396 /// of the board.
8397 board_fields: Option<Value>,
8398 /// The far ends of this issue's whole `blockedBy` connection, each as a dependency read
8399 /// selects one — when the read that reached it carried the connection to its end, which a
8400 /// read of it by its own id does for any issue blocked by no more than a page. What a
8401 /// write reconciles that relationship against, and what a read of its forward edges in
8402 /// the same command answers with.
8403 blocked_by: Option<Vec<Value>>,
8404}
8405
8406impl Resolved {
8407 /// The board this item's own read names it on, when that read named one this source can
8408 /// address.
8409 fn named_board(&self) -> Option<BoardId> {
8410 self.board_id
8411 .as_deref()
8412 .and_then(|id| BoardId::parse(id).ok())
8413 }
8414
8415 /// The board's id and every field it defines, when the read that reached this item
8416 /// carried both — which a read of it by its own id does.
8417 fn carried_board(&self) -> Option<BoardFields> {
8418 Some(BoardFields {
8419 id: self.named_board()?,
8420 fields: self.board_fields.clone()?,
8421 })
8422 }
8423
8424 /// Whether this item holds a value of the board field called `name`, and so carries
8425 /// that field's definition. `false` says nothing about whether the board has the field.
8426 fn defines(&self, name: &str) -> bool {
8427 self.fields
8428 .iter()
8429 .any(|field| field.get("name").and_then(Value::as_str) == Some(name))
8430 }
8431
8432 /// The metadata a caller sees: their own keys, plus the copy origin this source keeps
8433 /// in a field of its own, and none of the five keys that are only an encoding.
8434 ///
8435 /// The two delivery keys are left out for every kind, not only for a task: they are
8436 /// the encoding of [`Task::delivers`] and [`Task::delivered_by`], and a project or a
8437 /// document carrying one holds nothing a caller's own metadata could mean by it.
8438 fn metadata(&self) -> BTreeMap<String, Value> {
8439 let mut metadata = self.slot.clone();
8440 metadata.remove(Repository::METADATA_KEY);
8441 metadata.remove(DependencyEdge::RECORDED_KEY);
8442 metadata.remove(ItemKind::METADATA_KEY);
8443 metadata.remove(TaskRef::DELIVERS_KEY);
8444 metadata.remove(TaskRef::DELIVERED_BY_KEY);
8445 // The board field is the origin, and the body's copy of it is only a mirror for the
8446 // issue search to find: an item whose field holds none has none, whatever its body
8447 // says, so no reader ever sees two answers.
8448 metadata.remove(ORIGIN_KEY);
8449 if let Some(origin) = &self.origin {
8450 metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin.clone()));
8451 }
8452 metadata
8453 }
8454
8455 /// Where this item is, as a link a reader can open.
8456 ///
8457 /// A board is a hosted place and every issue on it has a web address, so that address
8458 /// is what "where is this?" means here — and [`Location::Url`] is what says which kind
8459 /// of place it is, so a reader knows to open it rather than to read a file out. It
8460 /// does not replace or derive from `url`: the field goes on reporting exactly what it
8461 /// reported before, and this says what that address *is*.
8462 ///
8463 /// An item GitHub gave no `url` for — a draft has none — reports no location at all
8464 /// rather than a third variant, which is the contract's "the source did not say". An
8465 /// issue this run created is not one of those: its address comes back from the
8466 /// creating mutation, so it is somewhere a reader can open from the moment it exists
8467 /// rather than from whenever the board read catches up.
8468 fn location(&self) -> Option<Location> {
8469 self.url.clone().map(Location::Url)
8470 }
8471
8472 /// The short handle this board's backend shows people for a task: the issue's number
8473 /// alone, as a decimal string.
8474 ///
8475 /// The number alone rather than `owner/repo#1043`, because that is the contract's
8476 /// value for this backend. A draft has no number and so no handle, which is the
8477 /// contract's *absent* rather than a handle of some other shape — and the native
8478 /// [`Task::id`] here is the issue's GraphQL node id, which this neither replaces nor
8479 /// derives from.
8480 fn key(&self) -> Option<String> {
8481 self.number.map(|number| number.to_string())
8482 }
8483
8484 /// Whether its `Priority` field holds a value at all, mapped or not.
8485 fn holds_priority(&self) -> bool {
8486 self.priority != HeldPriority::Read(Priority::None)
8487 }
8488
8489 /// The task this item is.
8490 ///
8491 /// Fails for an item whose `Priority` field holds an option the mapping does not name:
8492 /// reading that as a level would be a guess, and reading it as `none` would let the next
8493 /// copy clear a priority a person set.
8494 fn task(&self) -> Result<Task, SourceError> {
8495 let priority = match &self.priority {
8496 HeldPriority::Read(priority) => *priority,
8497 HeldPriority::Unmapped(option) => {
8498 return Err(SourceError::Malformed {
8499 message: format!(
8500 "task {}{} sits in the board {PRIORITY_FIELD} option {option:?}, which \
8501 this source's priority_mapping does not name, so its priority cannot be \
8502 read; next: name {option:?} under priority_mapping, or move the item to \
8503 a mapped option",
8504 self.id,
8505 self.number
8506 .map(|number| format!(" (#{number})"))
8507 .unwrap_or_default()
8508 ),
8509 });
8510 }
8511 };
8512 Ok(Task {
8513 id: self.id.clone(),
8514 key: self.key(),
8515 title: self.title.clone(),
8516 content: self.body.clone(),
8517 status: self.status.clone(),
8518 priority,
8519 labels: self.labels.clone(),
8520 project: self.parent.clone(),
8521 url: self.url.clone(),
8522 location: self.location(),
8523 created_at: self.created_at,
8524 updated_at: self.updated_at,
8525 metadata: self.metadata(),
8526 repositories: self.repositories.clone(),
8527 delivers: self.delivers.clone(),
8528 delivered_by: self.delivered_by.clone(),
8529 })
8530 }
8531
8532 fn project(&self) -> Project {
8533 Project {
8534 id: self.id.clone(),
8535 title: self.title.clone(),
8536 content: self.body.clone(),
8537 status: self.status.clone(),
8538 labels: self.labels.clone(),
8539 url: self.url.clone(),
8540 location: self.location(),
8541 created_at: self.created_at,
8542 updated_at: self.updated_at,
8543 metadata: self.metadata(),
8544 repositories: self.repositories.clone(),
8545 }
8546 }
8547
8548 /// The same issue as a document: the project it is filed under, and no status and no
8549 /// dependencies, because a document is not work.
8550 fn document(&self) -> Document {
8551 Document {
8552 id: self.id.clone(),
8553 title: self.title.clone(),
8554 content: self.body.clone(),
8555 project: self.parent.clone(),
8556 labels: self.labels.clone(),
8557 url: self.url.clone(),
8558 location: self.location(),
8559 created_at: self.created_at,
8560 updated_at: self.updated_at,
8561 metadata: self.metadata(),
8562 repositories: self.repositories.clone(),
8563 }
8564 }
8565}
8566
8567/// Where one targeted update moves an item's status, and which of its two halves move.
8568struct StatusMove {
8569 /// The board the item's `Status` field is on.
8570 board: BoardId,
8571 /// The `Status` field's id.
8572 field: String,
8573 /// The option's id.
8574 option: String,
8575 /// The option's name, as the board spells it.
8576 name: String,
8577 /// What the status asks of the issue's state.
8578 target: StatusTarget,
8579 /// The status the item reads as once it is there.
8580 landed: Status,
8581 /// Which of the status's two halves differ from what the item holds.
8582 moves: Moves,
8583}
8584
8585/// Which halves of an item's status one targeted update moves: its `Status` option, the open or
8586/// closed state of its issue, or both. A status neither half of which differs is no move at all,
8587/// and is not a value of this type.
8588#[derive(Clone, Copy, PartialEq, Eq)]
8589enum Moves {
8590 /// The option alone.
8591 Option,
8592 /// The issue's state alone: open, closed, or closed with another reason.
8593 State,
8594 /// Both.
8595 Both,
8596}
8597
8598impl Moves {
8599 /// What differs, or `None` when nothing does.
8600 const fn of(option: bool, state: bool) -> Option<Self> {
8601 match (option, state) {
8602 (true, true) => Some(Self::Both),
8603 (true, false) => Some(Self::Option),
8604 (false, true) => Some(Self::State),
8605 (false, false) => None,
8606 }
8607 }
8608
8609 /// Whether the option moves.
8610 const fn option(self) -> bool {
8611 matches!(self, Self::Option | Self::Both)
8612 }
8613
8614 /// Whether the issue's state moves.
8615 const fn state(self) -> bool {
8616 matches!(self, Self::State | Self::Both)
8617 }
8618}
8619
8620/// What one write is, and the status that comes with being it.
8621///
8622/// One value rather than a [`BoardKind`] beside an `Option<Status>`: a document has no
8623/// status and a task or a project always has one, so "a document carrying a status" and
8624/// "a task carrying none" are states a write cannot be in rather than states every use
8625/// site below has to defend against.
8626enum Written<'a> {
8627 /// A document, which is not work and so has no status at all.
8628 Document,
8629 /// A task or a project, and the status it is being written with.
8630 Work(ItemKind, &'a Status),
8631}
8632
8633impl Written<'_> {
8634 /// Which of the board's three kinds this write is.
8635 const fn kind(&self) -> BoardKind {
8636 match self {
8637 Self::Document => BoardKind::Document,
8638 Self::Work(kind, _) => BoardKind::Work(*kind),
8639 }
8640 }
8641
8642 /// The status this write carries. A document carries none, so a write of one says
8643 /// nothing about the issue's open or closed state and selects no board `Status`
8644 /// option.
8645 const fn status(&self) -> Option<&Status> {
8646 match self {
8647 Self::Document => None,
8648 Self::Work(_, status) => Some(status),
8649 }
8650 }
8651
8652 /// The status this write carries with the kind whose half of `status_mapping` it is
8653 /// written through.
8654 const fn work_status(&self) -> Option<(ItemKind, &Status)> {
8655 match self {
8656 Self::Document => None,
8657 Self::Work(kind, status) => Some((*kind, status)),
8658 }
8659 }
8660}
8661
8662/// The item being written, in the one shape all three write methods reach.
8663struct Incoming<'a> {
8664 written: Written<'a>,
8665 /// The title a person wrote. A document's goes onto the issue with
8666 /// [`DESIGN_TITLE_PREFIX`] put back, so a round trip returns the title that went in.
8667 title: &'a str,
8668 content: Option<&'a str>,
8669 labels: &'a [Label],
8670 metadata: &'a BTreeMap<String, Value>,
8671 repositories: &'a [Repository],
8672 parent: Option<&'a NativeId>,
8673 /// [`Task::delivers`], already checked. Empty for a project or a document, which is
8674 /// what keeps either key out of their slot.
8675 delivers: &'a [TaskRef],
8676 /// [`Task::delivered_by`], already checked. Empty for a project or a document.
8677 delivered_by: &'a [TaskRef],
8678 /// [`Task::priority`], for a task written to an instance that holds one; `None` for a
8679 /// project, a document, and every write to an instance with no `priority_mapping` —
8680 /// which is what keeps such a write's requests exactly what they were before.
8681 priority: Option<Priority>,
8682}
8683
8684/// What one write does to an item's `Priority` field.
8685enum PriorityWrite {
8686 /// Select this option of this field.
8687 Select {
8688 /// The `Priority` field's id.
8689 field: String,
8690 /// The mapped option's id.
8691 option: String,
8692 },
8693 /// Clear the field's value, which is what `none` is.
8694 Clear {
8695 /// The `Priority` field's id.
8696 field: String,
8697 },
8698}
8699
8700impl Incoming<'_> {
8701 /// The title this write puts on the issue.
8702 fn written_title(&self) -> String {
8703 match self.written {
8704 Written::Document => format!("{DESIGN_TITLE_PREFIX}{}", self.title),
8705 Written::Work(..) => self.title.to_owned(),
8706 }
8707 }
8708}
8709
8710#[derive(Clone, Copy, PartialEq, Eq)]
8711enum ContentKind {
8712 DraftIssue,
8713 Issue,
8714}
8715
8716/// What one board issue is: a document, or the work an [`ItemKind`] names.
8717///
8718/// A type of this source's own rather than an `ItemKind` with a third variant, because
8719/// `ItemKind` names what a dependency endpoint points at and nothing may point at a
8720/// document — the contract keeps a document out of that enum deliberately. Holding the
8721/// board's three answers in one value is what makes every place that asks "which is this?"
8722/// answer all three, rather than a `document: bool` beside a `kind` that means nothing for
8723/// two thirds of the board.
8724#[derive(Clone, Copy, PartialEq, Eq)]
8725enum BoardKind {
8726 /// An issue whose title begins [`DESIGN_TITLE_PREFIX`].
8727 Document,
8728 /// Every other issue, and every draft.
8729 Work(ItemKind),
8730}
8731
8732impl BoardKind {
8733 /// Whose half of `status_mapping` an item of this kind reads its status through. A
8734 /// document has no status of its own, so the task half stands in for whatever the issue
8735 /// holds; nothing reports it.
8736 const fn status_kind(self) -> ItemKind {
8737 match self {
8738 Self::Document => ItemKind::Task,
8739 Self::Work(kind) => kind,
8740 }
8741 }
8742
8743 /// How a refusal names this kind to the person reading it.
8744 const fn describes(self) -> &'static str {
8745 match self {
8746 Self::Document => "document",
8747 Self::Work(kind) => kind.marker(),
8748 }
8749 }
8750}
8751
8752/// Whether `labels` satisfies `filter`, matching by name, case-insensitively.
8753///
8754/// This is the local Markdown source's `labels_match`, spelled the same way on purpose:
8755/// the shared cross-source journeys assert one answer to one question, so two sources
8756/// that disagree about what "carries the label bug" means fail them.
8757fn labels_match(labels: &[Label], filter: &LabelFilter) -> bool {
8758 let holds = |name: &String| {
8759 labels
8760 .iter()
8761 .any(|label| label.name.eq_ignore_ascii_case(name))
8762 };
8763 (filter.any_of.is_empty() || filter.any_of.iter().any(holds))
8764 && filter.all_of.iter().all(holds)
8765 && !filter.none_of.iter().any(holds)
8766}
8767
8768/// Whether `category` is one of `statuses`. An empty list is unfiltered rather than
8769/// "keeps nothing", which is what lets a `Vec<StatusCategory>` spell no filter at all.
8770fn status_matches(category: StatusCategory, statuses: &[StatusCategory]) -> bool {
8771 statuses.is_empty() || statuses.contains(&category)
8772}
8773
8774/// Whether `title`/`content` satisfies `query`, matching case-insensitively.
8775///
8776/// `content` is the item's own prose — the body with this source's trailing metadata
8777/// comment already taken off — so a search never matches an encoding the author of the
8778/// issue never wrote.
8779fn text_matches(title: &str, content: Option<&str>, query: &TextQuery) -> bool {
8780 let terms = query.terms.to_lowercase();
8781 let in_title = title.to_lowercase().contains(&terms);
8782 let in_content = content.is_some_and(|body| body.to_lowercase().contains(&terms));
8783 match query.fields {
8784 TextFields::Title => in_title,
8785 TextFields::Content => in_content,
8786 TextFields::TitleOrContent => in_title || in_content,
8787 }
8788}
8789
8790/// Whether `task` satisfies `query`, with `project` deciding the project predicate.
8791///
8792/// The project predicate is passed separately because a read narrowed to one project has
8793/// already answered it by asking *that project* for its own items — and re-applying it
8794/// there would compare the caller's selector, which may be a project's **name**, against
8795/// the id of the project that name resolved to, and keep nothing. Every other read passes
8796/// `query.project` and applies it here, which is what keeps `projects` a predicate this
8797/// source really does apply.
8798fn task_matches(task: &Task, query: &TaskQuery, project: &ProjectFilter) -> bool {
8799 labels_match(&task.labels, &query.labels)
8800 && status_matches(task.status.category, &query.statuses)
8801 && (query.priorities.is_empty() || query.priorities.contains(&task.priority))
8802 && match project {
8803 ProjectFilter::Any => true,
8804 ProjectFilter::Orphans => task.project.is_none(),
8805 ProjectFilter::Is(id) => task.project.as_ref() == Some(id),
8806 }
8807 && query
8808 .text
8809 .as_ref()
8810 .is_none_or(|text| text_matches(&task.title, task.content.as_deref(), text))
8811 // Against the parsed metadata slot, and against the origin field, which is where
8812 // `Resolved::metadata` reads each of them from.
8813 && query.metadata_matches(&task.metadata)
8814 && query.origin_matches(&task.metadata)
8815}
8816
8817fn project_matches(project: &Project, query: &ProjectQuery) -> bool {
8818 labels_match(&project.labels, &query.labels)
8819 && status_matches(project.status.category, &query.statuses)
8820 && query
8821 .text
8822 .as_ref()
8823 .is_none_or(|text| text_matches(&project.title, project.content.as_deref(), text))
8824}
8825
8826/// The same three predicates a task query carries, minus the status filter.
8827///
8828/// A document is not work, so it has no status for one to compare against and the query
8829/// type carries none. The project predicate is the same one — a design issue filed under a
8830/// project issue is in that project, and one filed under nothing is in none — so it is
8831/// spelled the same way here rather than answered differently.
8832fn document_matches(document: &Document, query: &DocumentQuery, project: &ProjectFilter) -> bool {
8833 labels_match(&document.labels, &query.labels)
8834 && match project {
8835 ProjectFilter::Any => true,
8836 ProjectFilter::Orphans => document.project.is_none(),
8837 ProjectFilter::Is(id) => document.project.as_ref() == Some(id),
8838 }
8839 && query
8840 .text
8841 .as_ref()
8842 .is_none_or(|text| text_matches(&document.title, document.content.as_deref(), text))
8843}
8844
8845#[async_trait::async_trait]
8846impl TaskSource for GitHubProjectsSource {
8847 fn kind(&self) -> &'static str {
8848 KIND
8849 }
8850 fn capabilities(&self) -> Capabilities {
8851 Capabilities {
8852 projects: Support::Native,
8853 documents: Support::Native,
8854 comments: Support::Native,
8855 assets: Support::Unsupported,
8856 priority: if self.priorities.is_some() {
8857 Support::Native
8858 } else {
8859 Support::Unsupported
8860 },
8861 filter_by_priority: Support::Native,
8862 filter_by_comment_activity: Support::Native,
8863 filter_by_metadata: Support::Native,
8864 filter_by_origin: Support::Native,
8865 orphan_tasks: Support::Native,
8866 filter_by_label: Support::Native,
8867 filter_by_status: Support::Native,
8868 search_title: Support::Native,
8869 search_content: Support::Native,
8870 task_dependencies: DependencySupport::BothDirections,
8871 project_dependencies: DependencySupport::BothDirections,
8872 max_page_size: MAX_PAGE_SIZE,
8873 }
8874 }
8875 async fn health(&self) -> Result<Health, SourceError> {
8876 let board = self.board_page(None, 1).await?;
8877 Ok(Health {
8878 reachable: true,
8879 detail: Some(format!(
8880 "reading GitHub project {}/{} ({})",
8881 self.owner,
8882 self.project_number,
8883 required_str(&board, "title")?
8884 )),
8885 })
8886 }
8887 async fn get_task(&self, id: &NativeId) -> Result<Option<Task>, SourceError> {
8888 self.item_by_id(id)
8889 .await?
8890 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8891 .map(|item| item.task())
8892 .transpose()
8893 }
8894 async fn get_project(&self, id: &NativeId) -> Result<Option<Project>, SourceError> {
8895 Ok(self
8896 .item_by_id(id)
8897 .await?
8898 .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
8899 .map(|item| item.project()))
8900 }
8901 async fn query_tasks(
8902 &self,
8903 query: &TaskQuery,
8904 page: &PageRequest,
8905 ) -> Result<Page<Task>, SourceError> {
8906 validate_page(page)?;
8907 refuse_unsearchable(query)?;
8908 if query.origin.is_none() && !matches!(query.project, ProjectFilter::Is(_)) {
8909 let qualifiers = match (narrowing_qualifiers(query), query.commented_since) {
8910 (Some(also), Some(since)) => Some(format!("{} {also}", updated_qualifier(since))),
8911 (Some(also), None) => Some(also),
8912 (None, Some(since)) => Some(updated_qualifier(since)),
8913 (None, None) => None,
8914 };
8915 if let Some(also) = qualifiers {
8916 return self.search_tasks(query, page, &also).await;
8917 }
8918 }
8919
8920 // A read narrowed to one project asks that project for its own tasks, so nothing
8921 // about it costs what the rest of the board holds. A read carrying a text, metadata
8922 // or origin predicate asks GitHub the narrower question those predicates are, and a
8923 // read narrowed to comment activity alone asks the board's own issue search for the
8924 // issues updated since, which is every issue a comment could have been written or
8925 // edited on since. Every other task read is a question about the whole board and is
8926 // answered by reading it.
8927 let (held, membership) = match (&query.project, query.commented_since) {
8928 (ProjectFilter::Is(project), _) => (
8929 self.project_children(project).await?,
8930 // Answered by where these items came from; see `task_matches`.
8931 &ProjectFilter::Any,
8932 ),
8933 (ProjectFilter::Any | ProjectFilter::Orphans, since) => {
8934 match (self.narrowed(query).await?, since) {
8935 (Some(narrowed), _) => (narrowed, &query.project),
8936 (None, Some(since)) => (self.updated_since(since).await?, &query.project),
8937 (None, None) => (self.board().await?.items, &query.project),
8938 }
8939 }
8940 };
8941 // Filtered before paged: a page of a filtered result is a page of the survivors,
8942 // never the survivors of a page.
8943 let mut tasks = Vec::new();
8944 for item in held
8945 .iter()
8946 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8947 {
8948 let task = item.task()?;
8949 if task_matches(&task, query, membership)
8950 && self.commented_since(item, query.commented_since).await?
8951 {
8952 tasks.push(task);
8953 }
8954 }
8955 Ok(offset_page(
8956 tasks,
8957 numeric_cursor(page.cursor.as_ref())?,
8958 page.limit.min(MAX_PAGE_SIZE) as usize,
8959 ))
8960 }
8961 async fn query_projects(
8962 &self,
8963 query: &ProjectQuery,
8964 page: &PageRequest,
8965 ) -> Result<Page<Project>, SourceError> {
8966 validate_page(page)?;
8967 refuse_unsearchable_text(query.text.as_ref())?;
8968 // The projects a board holds are found by an issue search scoped to that board,
8969 // never by walking the board's own item connection: what tells a project from a
8970 // task is the `parent` each issue carries, which costs nothing to read. A query
8971 // carrying a text asks that search for the text too, so it reads the issues that
8972 // hold it rather than every issue of the board.
8973 let held = match self.text_searched(query.text.as_ref()).await? {
8974 Some(searched) => searched,
8975 None => self.board_issues().await?,
8976 };
8977 let projects = held
8978 .iter()
8979 .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
8980 .map(Resolved::project)
8981 .filter(|project| project_matches(project, query))
8982 .collect();
8983 Ok(offset_page(
8984 projects,
8985 numeric_cursor(page.cursor.as_ref())?,
8986 page.limit.min(MAX_PAGE_SIZE) as usize,
8987 ))
8988 }
8989 async fn get_document(&self, id: &NativeId) -> Result<Option<Document>, SourceError> {
8990 Ok(self
8991 .item_by_id(id)
8992 .await?
8993 .filter(|item| item.kind == BoardKind::Document)
8994 .map(|item| item.document()))
8995 }
8996 async fn query_documents(
8997 &self,
8998 query: &DocumentQuery,
8999 page: &PageRequest,
9000 ) -> Result<Page<Document>, SourceError> {
9001 validate_page(page)?;
9002 // Narrowed to one project, this is the same sub-issue read a task list scoped to
9003 // that project makes — a document filed under a project is a sub-issue of it too,
9004 // and which of them come back is the kind this caller asked for. Unscoped, a query
9005 // carrying a text asks the board-scoped issue search for it, as a task query does,
9006 // and only one carrying none reads the board.
9007 let (held, membership) = match &query.project {
9008 ProjectFilter::Is(project) => (
9009 self.project_children(project).await?,
9010 // Answered by where these items came from; see `task_matches`.
9011 &ProjectFilter::Any,
9012 ),
9013 ProjectFilter::Any | ProjectFilter::Orphans => {
9014 refuse_unsearchable_text(query.text.as_ref())?;
9015 match self.text_searched(query.text.as_ref()).await? {
9016 Some(searched) => (searched, &query.project),
9017 None => (self.board().await?.items, &query.project),
9018 }
9019 }
9020 };
9021 // Filtered before paged, exactly as a task read is: a page of a filtered result is
9022 // a page of the survivors, never the survivors of a page.
9023 let documents = held
9024 .iter()
9025 .filter(|item| item.kind == BoardKind::Document)
9026 .map(Resolved::document)
9027 .filter(|document| document_matches(document, query, membership))
9028 .collect();
9029 Ok(offset_page(
9030 documents,
9031 numeric_cursor(page.cursor.as_ref())?,
9032 page.limit.min(MAX_PAGE_SIZE) as usize,
9033 ))
9034 }
9035 async fn labels(&self, page: &PageRequest) -> Result<Page<Label>, SourceError> {
9036 validate_page(page)?;
9037 let offset = numeric_cursor(page.cursor.as_ref())?;
9038 let mut labels = self
9039 .board()
9040 .await?
9041 .items
9042 .into_iter()
9043 .flat_map(|item| item.labels)
9044 .fold(Vec::new(), |mut all, label| {
9045 if !all.iter().any(|x: &Label| x.id == label.id) {
9046 all.push(label);
9047 }
9048 all
9049 });
9050 labels.sort_by(|a, b| a.name.cmp(&b.name).then(a.id.0.cmp(&b.id.0)));
9051 Ok(offset_page(
9052 labels,
9053 offset,
9054 page.limit.min(MAX_PAGE_SIZE) as usize,
9055 ))
9056 }
9057 async fn task_dependencies(
9058 &self,
9059 id: &NativeId,
9060 direction: Direction,
9061 page: &PageRequest,
9062 ) -> Result<Page<DependencyEdge>, SourceError> {
9063 self.dependencies(id, ItemKind::Task, direction, page).await
9064 }
9065 async fn project_dependencies(
9066 &self,
9067 id: &NativeId,
9068 direction: Direction,
9069 page: &PageRequest,
9070 ) -> Result<Page<DependencyEdge>, SourceError> {
9071 self.dependencies(id, ItemKind::Project, direction, page)
9072 .await
9073 }
9074
9075 fn writes(&self) -> WriteSupport {
9076 WriteSupport::Supported
9077 }
9078
9079 /// Create or update one task.
9080 ///
9081 /// Its `delivers` and `delivered_by` are checked before anything is read or written —
9082 /// neither may name the task itself or name one task twice — and land in the body's
9083 /// metadata slot under their reserved keys, in place of any caller metadata of those
9084 /// names.
9085 async fn write_task(&self, write: &ItemWrite<Task>) -> Result<NativeId, SourceError> {
9086 let near = write.target.as_ref().unwrap_or(&write.item.id);
9087 for (key, entries) in [
9088 (TaskRef::DELIVERS_KEY, &write.item.delivers),
9089 (TaskRef::DELIVERED_BY_KEY, &write.item.delivered_by),
9090 ] {
9091 TaskRef::listed(key, near, Some(&self.name), entries.clone())
9092 .map_err(|message| SourceError::Refused { message })?;
9093 }
9094 if self.priorities.is_none() && write.item.priority != Priority::None {
9095 return Err(self.holds_no_priority());
9096 }
9097 self.write_item(
9098 &Incoming {
9099 written: Written::Work(ItemKind::Task, &write.item.status),
9100 title: &write.item.title,
9101 content: write.item.content.as_deref(),
9102 labels: &write.item.labels,
9103 metadata: &write.item.metadata,
9104 repositories: &write.item.repositories,
9105 parent: write.item.project.as_ref(),
9106 delivers: &write.item.delivers,
9107 delivered_by: &write.item.delivered_by,
9108 priority: self.priorities.as_ref().map(|_| write.item.priority),
9109 },
9110 write.target.as_ref(),
9111 &write.depends_on,
9112 )
9113 .await
9114 }
9115
9116 async fn write_project(&self, write: &ItemWrite<Project>) -> Result<NativeId, SourceError> {
9117 self.write_item(
9118 &Incoming {
9119 written: Written::Work(ItemKind::Project, &write.item.status),
9120 title: &write.item.title,
9121 content: write.item.content.as_deref(),
9122 labels: &write.item.labels,
9123 metadata: &write.item.metadata,
9124 repositories: &write.item.repositories,
9125 parent: None,
9126 delivers: &[],
9127 delivered_by: &[],
9128 priority: None,
9129 },
9130 write.target.as_ref(),
9131 &write.depends_on,
9132 )
9133 .await
9134 }
9135
9136 /// Create or update one document, which is one issue titled the way this board spells
9137 /// a document.
9138 ///
9139 /// Everything else is exactly a task write: caller metadata goes to the same canonical
9140 /// JSON slot at the end of the body and comes back with its JSON types intact, a key
9141 /// or a field this board cannot carry is refused by name rather than dropped, a target
9142 /// naming an issue this board does not hold is refused rather than created, and an
9143 /// issue this call created is taken back when the rest of the write fails.
9144 async fn write_document(&self, write: &ItemWrite<Document>) -> Result<NativeId, SourceError> {
9145 // A document takes part in no dependency graph, so there is no far end to write
9146 // natively and none to record: a caller naming one is told so rather than having it
9147 // stored under the reserved key, where a later read would report an edge the
9148 // contract says cannot exist.
9149 if !write.depends_on.is_empty() {
9150 return Err(SourceError::Refused {
9151 message: format!(
9152 "this write names {} dependencies for a document, and a document takes \
9153 part in no dependency graph; next: put the dependency on the task or \
9154 project the document is about",
9155 write.depends_on.len()
9156 ),
9157 });
9158 }
9159 self.write_item(
9160 &Incoming {
9161 written: Written::Document,
9162 title: &write.item.title,
9163 content: write.item.content.as_deref(),
9164 labels: &write.item.labels,
9165 metadata: &write.item.metadata,
9166 repositories: &write.item.repositories,
9167 parent: write.item.project.as_ref(),
9168 delivers: &[],
9169 delivered_by: &[],
9170 priority: None,
9171 },
9172 write.target.as_ref(),
9173 &[],
9174 )
9175 .await
9176 }
9177
9178 /// Refused exactly as the write refuses it, from what the write reads: the mapping first,
9179 /// which reads nothing; then the board's `Status` option. Over an existing item that is
9180 /// read off the item, as the write reads it, and the item is held among this command's
9181 /// resolved records so the write that follows reuses that read rather than repeating it;
9182 /// an item that does not carry the field takes the board's fields, which are held once
9183 /// read. A create is checked against the board's fields only when this command already
9184 /// holds them, because a create reads them together with its repository, in one request,
9185 /// and refuses a missing option before it writes anything.
9186 async fn check_status_write(
9187 &self,
9188 kind: ItemKind,
9189 category: StatusCategory,
9190 target: Option<&NativeId>,
9191 ) -> Result<(), SourceError> {
9192 let status = self.resolved_target(kind, category)?;
9193 if status.option().is_none() {
9194 return Ok(());
9195 }
9196 let fields = match target {
9197 Some(target) => {
9198 // A target this board does not hold is the write's own refusal to make.
9199 let Some(item) = self.bound_item(target).await? else {
9200 return Ok(());
9201 };
9202 self.resolved_cache()?.insert(target.clone(), item.clone());
9203 self.fields_for(Some(&item), true, false).await?.fields
9204 }
9205 None => {
9206 let held = self
9207 .board_cache()?
9208 .as_ref()
9209 .map(|board| board.fields.clone());
9210 match held.or_else(|| {
9211 self.fields_cache()
9212 .ok()
9213 .and_then(|cache| cache.as_ref().map(|board| board.fields.clone()))
9214 }) {
9215 Some(fields) => fields,
9216 None => return Ok(()),
9217 }
9218 }
9219 };
9220 self.column_for(&fields, kind, category, &status)
9221 .map(|_| ())
9222 }
9223
9224 /// Set one task's status alone.
9225 ///
9226 /// An open target reopens a closed issue with an `updateIssue` carrying only its
9227 /// `stateInput`, then selects the board option with `updateProjectV2ItemFieldValue`; a
9228 /// terminal target selects its mapped option, then closes with its fixed reason. No
9229 /// request carries a title, a body or a label. The status
9230 /// answered is what [`BoardStatuses::status`] reads off the state just written, which is
9231 /// what a re-read reports.
9232 async fn set_task_status(
9233 &self,
9234 id: &NativeId,
9235 category: StatusCategory,
9236 ) -> Result<Option<Status>, SourceError> {
9237 self.set_status(id, category).await
9238 }
9239
9240 /// Set one task's priority alone: one `updateProjectV2ItemFieldValue` selecting the
9241 /// mapped option of the board's `Priority` field, or one `clearProjectV2ItemFieldValue`
9242 /// for `none`. Refused by an instance with no `priority_mapping`.
9243 async fn set_task_priority(
9244 &self,
9245 id: &NativeId,
9246 priority: Priority,
9247 ) -> Result<Option<Priority>, SourceError> {
9248 self.set_priority(id, priority).await
9249 }
9250
9251 /// Replace one task's content with a single body update that keeps the metadata slot
9252 /// byte for byte.
9253 async fn set_task_content(
9254 &self,
9255 id: &NativeId,
9256 content: &str,
9257 ) -> Result<Option<()>, SourceError> {
9258 self.replace_content(id, content).await
9259 }
9260
9261 /// Replace one task issue's content and its provenance slot entry with a single body
9262 /// update. The answers are not kept: see `replace_rendering`.
9263 async fn set_task_rendering(
9264 &self,
9265 id: &NativeId,
9266 content: &str,
9267 provenance: &Value,
9268 _answers: &BTreeMap<String, Value>,
9269 ) -> Result<Option<()>, SourceError> {
9270 self.replace_rendering(id, BoardKind::Work(ItemKind::Task), content, provenance)
9271 .await
9272 }
9273
9274 /// Replace one design-document issue's content and its provenance slot entry, on exactly
9275 /// the terms of [`set_task_rendering`](TaskSource::set_task_rendering).
9276 async fn set_document_rendering(
9277 &self,
9278 id: &NativeId,
9279 content: &str,
9280 provenance: &Value,
9281 _answers: &BTreeMap<String, Value>,
9282 ) -> Result<Option<()>, SourceError> {
9283 self.replace_rendering(id, BoardKind::Document, content, provenance)
9284 .await
9285 }
9286
9287 /// Replace one project issue's content and its provenance slot entry, on exactly the
9288 /// terms of [`set_task_rendering`](TaskSource::set_task_rendering).
9289 async fn set_project_rendering(
9290 &self,
9291 id: &NativeId,
9292 content: &str,
9293 provenance: &Value,
9294 _answers: &BTreeMap<String, Value>,
9295 ) -> Result<Option<()>, SourceError> {
9296 self.replace_rendering(id, BoardKind::Work(ItemKind::Project), content, provenance)
9297 .await
9298 }
9299
9300 /// Apply a targeted update with one read of the item and a write only for what differs:
9301 /// the `Status` and `Priority` field writes in one request, the `blockedBy` difference,
9302 /// and last one `updateIssue` for title, body and state. See `targeted_update`.
9303 async fn update_task(
9304 &self,
9305 id: &NativeId,
9306 update: &TaskUpdate,
9307 ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
9308 self.targeted_update(id, update).await
9309 }
9310
9311 /// Replace one task's `delivered_by` with a single body update that changes the
9312 /// metadata slot and nothing outside it.
9313 async fn set_delivered_by(
9314 &self,
9315 id: &NativeId,
9316 delivered_by: &[TaskRef],
9317 ) -> Result<Option<()>, SourceError> {
9318 self.replace_delivered_by(id, delivered_by).await
9319 }
9320
9321 /// Set one key of one task issue's metadata with a single body update that changes the
9322 /// metadata slot and nothing outside it — no title, label, state or board field request —
9323 /// and sends nothing when the task already holds that value under the key.
9324 async fn set_task_metadata(
9325 &self,
9326 id: &NativeId,
9327 key: &MetadataKey,
9328 value: &Value,
9329 ) -> Result<Option<Task>, SourceError> {
9330 Ok(self
9331 .set_slot_key(id, BoardKind::Work(ItemKind::Task), key, value)
9332 .await?
9333 .map(|item| item.task())
9334 .transpose()?)
9335 }
9336
9337 /// Set one key of one project issue's metadata, on exactly the terms of
9338 /// [`set_task_metadata`](TaskSource::set_task_metadata).
9339 async fn set_project_metadata(
9340 &self,
9341 id: &NativeId,
9342 key: &MetadataKey,
9343 value: &Value,
9344 ) -> Result<Option<Project>, SourceError> {
9345 Ok(self
9346 .set_slot_key(id, BoardKind::Work(ItemKind::Project), key, value)
9347 .await?
9348 .map(|item| item.project()))
9349 }
9350
9351 /// Set one key of one design-document issue's metadata, on exactly the terms of
9352 /// [`set_task_metadata`](TaskSource::set_task_metadata).
9353 async fn set_document_metadata(
9354 &self,
9355 id: &NativeId,
9356 key: &MetadataKey,
9357 value: &Value,
9358 ) -> Result<Option<Document>, SourceError> {
9359 Ok(self
9360 .set_slot_key(id, BoardKind::Document, key, value)
9361 .await?
9362 .map(|item| item.document()))
9363 }
9364
9365 async fn delete_task(&self, id: &NativeId) -> Result<(), SourceError> {
9366 self.delete_item(id).await
9367 }
9368
9369 async fn delete_project(&self, id: &NativeId) -> Result<(), SourceError> {
9370 self.delete_item(id).await
9371 }
9372
9373 async fn delete_document(&self, id: &NativeId) -> Result<(), SourceError> {
9374 self.delete_item(id).await
9375 }
9376
9377 /// One page of the task issue's own comments, walked by GitHub's own cursor.
9378 ///
9379 /// Nothing here filters, so nothing has to be read ahead of the page: the caller's limit is
9380 /// the page GitHub is asked for and GitHub's `endCursor` is the cursor handed back.
9381 ///
9382 /// One request, [`graphql::ISSUE_DETAIL`]: the read that says the id names a task of this
9383 /// board is the read of its comments. A draft this process already resolved is refused
9384 /// without one.
9385 async fn task_comments(
9386 &self,
9387 task: &NativeId,
9388 page: &PageRequest,
9389 ) -> Result<Option<Page<Comment>>, SourceError> {
9390 validate_page(page)?;
9391 let cached = self.resolved_cache()?.get(task).cloned();
9392 if let Some(item) = cached {
9393 if item.kind != BoardKind::Work(ItemKind::Task) {
9394 return Ok(None);
9395 }
9396 if item.content_kind == ContentKind::DraftIssue {
9397 return Err(self.draft_has_no_comments(task));
9398 }
9399 }
9400 match self.issue_detail(task, page).await? {
9401 Some(TaskDetailRead {
9402 comments: Some(comments),
9403 ..
9404 }) => comments,
9405 _ => Ok(None),
9406 }
9407 }
9408
9409 /// Every id's task, with the first page of its comments when `comments` names it:
9410 /// [`DETAIL_BATCH`] items per [`graphql::ISSUE_DETAILS`] request, and one item with its
9411 /// comments in one [`graphql::ISSUE_DETAIL`] request.
9412 async fn get_task_details(
9413 &self,
9414 ids: &[NativeId],
9415 comments: Option<&PageRequest>,
9416 ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
9417 if let Some(page) = comments
9418 && let Err(error) = validate_page(page)
9419 {
9420 return ids.iter().map(|_| Err(error.clone())).collect();
9421 }
9422 match (ids, comments) {
9423 ([id], Some(page)) => vec![self.issue_detail(id, page).await],
9424 ([id], None) => vec![self.task_read(id).await],
9425 _ => self.issue_details(ids, comments).await,
9426 }
9427 }
9428
9429 /// Add one comment to the task's issue, as the account the token belongs to.
9430 ///
9431 /// The author is refused before anything is sent — not even the task is read — because
9432 /// no answer GitHub could give would make posting under another name than the one asked
9433 /// for the right outcome.
9434 async fn add_comment(
9435 &self,
9436 task: &NativeId,
9437 comment: &NewComment,
9438 ) -> Result<Option<Comment>, SourceError> {
9439 if let Some(author) = &comment.author {
9440 return Err(SourceError::Refused {
9441 message: format!(
9442 "source {} cannot post a comment as {author:?}: GitHub records the account \
9443 the token signs in as the author of every comment; next: leave --author \
9444 out, and the comment is posted as that account",
9445 self.name
9446 ),
9447 });
9448 }
9449 let Some(issue) = self.commented_issue(task).await? else {
9450 return Ok(None);
9451 };
9452 let data = self
9453 .graphql(
9454 graphql::ADD_COMMENT,
9455 json!({"input":{"subjectId":issue.0,"body":comment.body.as_str()}}),
9456 )
9457 .await?;
9458 let subject = data
9459 .pointer("/addComment/subject")
9460 .filter(|value| !value.is_null())
9461 .ok_or_else(|| SourceError::Malformed {
9462 message: "GitHub comment addition returned no subject".into(),
9463 })?;
9464 if required_str(subject, "id")? != issue.0 {
9465 return Err(SourceError::Malformed {
9466 message: "GitHub comment addition answered about another issue".into(),
9467 });
9468 }
9469 let added = data
9470 .pointer("/addComment/commentEdge/node")
9471 .filter(|value| !value.is_null())
9472 .ok_or_else(|| SourceError::Malformed {
9473 message: "GitHub comment addition returned no comment".into(),
9474 })?;
9475 let added = comment_from(added)?;
9476 self.remember_commented(&issue)?;
9477 Ok(Some(added))
9478 }
9479
9480 async fn edit_comment(
9481 &self,
9482 task: &NativeId,
9483 comment: &NativeId,
9484 body: &CommentBody,
9485 ) -> Result<Option<Comment>, SourceError> {
9486 let Some(issue) = self.commented_issue(task).await? else {
9487 return Ok(None);
9488 };
9489 if !self.comment_is_on(&issue, comment).await? {
9490 return Ok(None);
9491 }
9492 let data = self
9493 .graphql(
9494 graphql::UPDATE_COMMENT,
9495 json!({"input":{"id":comment.0,"body":body.as_str()}}),
9496 )
9497 .await?;
9498 let edited = data
9499 .pointer("/updateIssueComment/issueComment")
9500 .filter(|value| !value.is_null())
9501 .ok_or_else(|| SourceError::Malformed {
9502 message: "GitHub comment update returned no comment".into(),
9503 })?;
9504 let edited = comment_from(edited)?;
9505 if edited.id != *comment {
9506 return Err(SourceError::Malformed {
9507 message: "GitHub comment update returned the wrong comment".into(),
9508 });
9509 }
9510 self.remember_commented(&issue)?;
9511 Ok(Some(edited))
9512 }
9513
9514 async fn delete_comment(
9515 &self,
9516 task: &NativeId,
9517 comment: &NativeId,
9518 ) -> Result<Option<NativeId>, SourceError> {
9519 let Some(issue) = self.commented_issue(task).await? else {
9520 return Ok(None);
9521 };
9522 if !self.comment_is_on(&issue, comment).await? {
9523 return Ok(None);
9524 }
9525 let data = self
9526 .graphql(graphql::DELETE_COMMENT, json!({"input":{"id":comment.0}}))
9527 .await?;
9528 // The payload says nothing about the comment it removed, so what is checked is that
9529 // GitHub answered the mutation at all rather than leaving it unanswered.
9530 data.get("deleteIssueComment")
9531 .filter(|value| !value.is_null())
9532 .ok_or_else(|| SourceError::Malformed {
9533 message: "GitHub comment deletion returned no payload".into(),
9534 })?;
9535 Ok(Some(comment.clone()))
9536 }
9537
9538 /// Every request this source has recorded, and what each of GitHub's two budgets was
9539 /// attributed — read off the same accounting the session report is rendered from, so
9540 /// the two cannot count one request two ways.
9541 async fn metering(&self) -> Result<Option<Metering>, SourceError> {
9542 Ok(Some(self.ledger.snapshot().metering()))
9543 }
9544
9545 /// Drop every item, search answer and board read this source holds, so the next command
9546 /// reads the board as a person has since left it.
9547 ///
9548 /// Every one of those is held on the assumption that nothing but this source writes the
9549 /// board while a command runs, which stops being true the moment the command is over: a
9550 /// body a person edited would be overwritten from the record held here, and a card they
9551 /// moved would be read as still where this source left it. The board's own field
9552 /// definitions go too, because a person can add or delete a `Status` option and a write
9553 /// resolved against the held list would not re-read on a miss. What stays is what stays
9554 /// valid in normal use: each repository's node id, which a miss re-reads, the pacing of
9555 /// mutations, which is about GitHub's limiter rather than anybody's work, and the running
9556 /// accounting [`metering`](TaskSource::metering) answers from.
9557 ///
9558 /// Infallible in practice: a lock an earlier failure poisoned is cleared rather than
9559 /// refused, because clearing it is what puts it right.
9560 async fn end_command(&self) -> Result<(), SourceError> {
9561 fn clear<T: Default>(held: &Mutex<T>) {
9562 *held
9563 .lock()
9564 .unwrap_or_else(std::sync::PoisonError::into_inner) = T::default();
9565 held.clear_poison();
9566 }
9567 clear(&self.created);
9568 clear(&self.updated);
9569 clear(&self.commented);
9570 clear(&self.board_cache);
9571 clear(&self.search_cache);
9572 clear(&self.narrowed_cache);
9573 clear(&self.search_next);
9574 clear(&self.resolved_cache);
9575 clear(&self.fields_cache);
9576 Ok(())
9577 }
9578}
9579
9580/// One issue comment as the contract carries it.
9581///
9582/// `author` is absent both when GitHub answers `null` for an account that no longer exists
9583/// and when it answers an actor with no login, because either way the source did not say who
9584/// wrote it — which is what an absent author means, rather than an author called nothing.
9585fn comment_from(value: &Value) -> Result<Comment, SourceError> {
9586 Ok(Comment {
9587 id: NativeId(required_str(value, "id")?.to_owned()),
9588 author: optional_str(value.get("author").unwrap_or(&Value::Null), "login")?
9589 .map(str::to_owned),
9590 created_at: optional_time(value, "createdAt")?,
9591 updated_at: optional_time(value, "updatedAt")?,
9592 body: required_str(value, "body")?.to_owned(),
9593 url: optional_str(value, "url")?.map(str::to_owned),
9594 })
9595}
9596
9597/// The page of comments one issue node carries, resumed from `after`.
9598fn comment_page(
9599 node: &Value,
9600 issue: &str,
9601 after: Option<&str>,
9602) -> Result<Page<Comment>, SourceError> {
9603 let connection = node
9604 .get("comments")
9605 .filter(|value| !value.is_null())
9606 .ok_or_else(|| SourceError::Malformed {
9607 message: format!("GitHub issue {issue} answered with no comments connection"),
9608 })?;
9609 let items = optional_nodes(Some(connection), "issue comments")?
9610 .into_iter()
9611 .flatten()
9612 .map(comment_from)
9613 .collect::<Result<Vec<_>, _>>()?;
9614 let next = next_cursor(connection)?;
9615 if let Some(next) = &next {
9616 validate_cursor_progress(after, &next.0)?;
9617 }
9618 Ok(Page { items, next })
9619}
9620
9621/// The far ends of an issue's whole `blockedBy` connection, when the read carried it to its
9622/// end — `None` when it carried none, or a page with more past it.
9623fn carried_blocked_by(content: &Value) -> Result<Option<Vec<Value>>, SourceError> {
9624 let Some(connection) = content.get("blockedBy").filter(|value| !value.is_null()) else {
9625 return Ok(None);
9626 };
9627 if next_cursor(connection)?.is_some() {
9628 return Ok(None);
9629 }
9630 Ok(Some(
9631 optional_nodes(Some(connection), "blocked-by issues")?
9632 .into_iter()
9633 .flatten()
9634 .cloned()
9635 .collect(),
9636 ))
9637}
9638
9639/// Where the recorded tail of a dependency walk resumes; see
9640/// [`GitHubProjectsSource::recorded_edges`].
9641const RECORDED_CURSOR: &str = "onetaskgraph.depends_on:";
9642
9643/// The board text field this source keeps a copy's origin in.
9644///
9645/// Named after the key it holds, and held to that name by the guard below rather than by
9646/// a reader noticing.
9647const ORIGIN_FIELD: &str = "onetaskgraph.origin";
9648
9649/// The metadata key that field holds.
9650///
9651/// The engine owns this key and spells it once as `GlobalId::ORIGIN_KEY`; a plugin never
9652/// constructs or interprets the qualified id it carries. This source names it only to
9653/// route it — a short, typed value belongs in a typed field rather than in the body slot
9654/// a caller's own prose shares.
9655///
9656/// Restated rather than imported, because no plugin crate may depend on the engine. What
9657/// keeps the two spellings one contract is `scripts/check-origin-key-spelling.sh`, a
9658/// target in `check`: it reads the engine's own literal and fails naming the file and the
9659/// line when a plugin's parts from it either way. Drift here has one symptom — a copy
9660/// that creates a second item every run instead of finding the one it wrote — and that is
9661/// too late to learn it.
9662const ORIGIN_KEY: &str = "onetaskgraph.origin";
9663
9664/// Where a recorded tail resumes, refusing a cursor no walk in `direction` reported.
9665///
9666/// The reserved key holds forward edges and nothing else — the reverse of a recorded edge
9667/// is derived from the far end, never written down on the near item — so only a forward
9668/// walk ever reports one of these cursors. A reverse read carrying one is resuming a walk
9669/// it did not come from, and it is told so rather than answered with an empty page that
9670/// reads as a walk which ended.
9671fn recorded_offset(
9672 cursor: Option<&str>,
9673 direction: Direction,
9674) -> Result<Option<usize>, SourceError> {
9675 cursor
9676 .and_then(|cursor| cursor.strip_prefix(RECORDED_CURSOR))
9677 .map(|offset| {
9678 if direction != Direction::DependsOn {
9679 return Err(SourceError::Config {
9680 message: format!(
9681 "{RECORDED_CURSOR}{offset} resumes recorded forward edges, which a \
9682 reverse dependency read never issues; resume it in the direction \
9683 that reported it"
9684 ),
9685 });
9686 }
9687 offset.parse().map_err(|_| SourceError::Config {
9688 message: format!("{RECORDED_CURSOR}{offset} is not a recorded-edge cursor"),
9689 })
9690 })
9691 .transpose()
9692}
9693
9694fn recorded_page(edges: Vec<DependencyEdge>, offset: usize, limit: usize) -> Page<DependencyEdge> {
9695 let mut page = offset_page(edges, offset, limit.max(1));
9696 page.next = page
9697 .next
9698 .map(|cursor| Cursor(format!("{RECORDED_CURSOR}{}", cursor.0)));
9699 page
9700}
9701
9702/// The kind of one issue reached through a dependency connection.
9703///
9704/// The same questions the board scan asks, over the fields the dependency document
9705/// selects, and in the same order: the design prefix first, then a sub-issue is a task,
9706/// then anything with sub-issues or the marker is a project.
9707///
9708/// # Errors
9709///
9710/// A far end this board holds as a document is refused rather than reported. The two
9711/// answers that are not refusals would both be wrong: reporting it as a task names an id
9712/// no task read of this source can find, and reporting it as a project names one no
9713/// project read can. There is no third value to return — `ItemKind` has no document
9714/// variant, because nothing may point at a document — so the relationship itself is what
9715/// the person is told about.
9716fn related_kind(value: &Value) -> Result<ItemKind, SourceError> {
9717 let id = required_str(value, "id")?;
9718 if required_str(value, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
9719 return Err(SourceError::Refused {
9720 message: format!(
9721 "GitHub issue {id} is a document of this board — its title begins \
9722 {DESIGN_TITLE_PREFIX:?} — and nothing may depend on a document or be depended \
9723 on by one; next: remove that issue's blocking relationship on this board"
9724 ),
9725 });
9726 }
9727 let parent = optional_str(value.get("parent").unwrap_or(&Value::Null), "id")?;
9728 if parent.is_some() {
9729 return Ok(ItemKind::Task);
9730 }
9731 let (_, slot) = metadata_body(optional_str(value, "body")?.map(str::to_owned))?;
9732 let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
9733 message: format!("GitHub issue {id}: {message}"),
9734 })?;
9735 let sub_issues = sub_issue_total(value)?;
9736 Ok(if sub_issues > 0 || marked == Some(ItemKind::Project) {
9737 ItemKind::Project
9738 } else {
9739 ItemKind::Task
9740 })
9741}
9742
9743/// The `IssueStateUpdateInput` one status target asks for.
9744///
9745/// `stateInput` and `state` are mutually exclusive on `UpdateIssueInput`, and only this
9746/// one is ever sent. A non-terminal status always asks for `OPEN`, which is what reopens
9747/// a currently-closed issue: without that the item would read back `Unknown` and a copy
9748/// would report a change forever. A document has no status at all, and asks for neither.
9749fn state_input(target: Option<&StatusTarget>) -> Value {
9750 match target {
9751 Some(StatusTarget::Terminal(_, reason)) => {
9752 json!({"value":"CLOSED","stateReason":reason.reason()})
9753 }
9754 Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => json!({"value":"OPEN"}),
9755 // A document has no status, so a write of one says nothing about the issue's open
9756 // or closed state rather than forcing it open: `stateInput` is what carries that
9757 // instruction, and an explicit null asks for no change to it.
9758 None => Value::Null,
9759 }
9760}
9761
9762/// The metadata one write stores in the item's body slot.
9763///
9764/// The typed fields travel as themselves, so the three reserved keys are rebuilt here
9765/// rather than carried: the kind marker so an empty project stays readable, the
9766/// repository list only when it is not exactly the issue's own repository, and the far
9767/// ends no relationship here can name.
9768///
9769/// The copy origin is the one typed field that is also mirrored here, and only as a
9770/// mirror: it lands in the board's origin field as well, which stays the one every reader
9771/// takes it from, and it is here so that GitHub's issue search — which indexes this comment
9772/// and catches up with a write in seconds rather than minutes — can find the item by it.
9773/// A reader of the release before this one drops the slot's copy and reads the field, so an
9774/// item written here still reads with exactly one origin there.
9775fn slot_metadata(
9776 incoming: &Incoming<'_>,
9777 own_repository: Option<&Repository>,
9778 fallback: &[DependencyEdge],
9779) -> BTreeMap<String, Value> {
9780 let mut metadata = incoming.metadata.clone();
9781 match metadata.remove(ORIGIN_KEY) {
9782 Some(Value::String(origin)) if !origin.is_empty() => {
9783 metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin));
9784 }
9785 _ => {}
9786 }
9787 match incoming.written.kind() {
9788 BoardKind::Work(kind) => metadata.insert(
9789 ItemKind::METADATA_KEY.to_owned(),
9790 Value::String(kind.marker().to_owned()),
9791 ),
9792 // A document is told by its title, so it carries no kind marker: that key names
9793 // what a dependency endpoint points at, and nothing may point at a document.
9794 BoardKind::Document => metadata.remove(ItemKind::METADATA_KEY),
9795 };
9796 let derivable = own_repository
9797 .map(|own| incoming.repositories == [own.clone()])
9798 .unwrap_or(incoming.repositories.is_empty());
9799 if derivable {
9800 metadata.remove(Repository::METADATA_KEY);
9801 } else {
9802 metadata.insert(
9803 Repository::METADATA_KEY.to_owned(),
9804 Value::Array(
9805 incoming
9806 .repositories
9807 .iter()
9808 .map(|repository| Value::String(repository.as_str().to_owned()))
9809 .collect(),
9810 ),
9811 );
9812 }
9813 // The typed lists are what land, whatever the caller's own metadata held under their
9814 // keys: a key of either name travelling beside the field would otherwise be a second
9815 // answer to the same question, and the field is the one the contract names.
9816 for (key, entries) in [
9817 (TaskRef::DELIVERS_KEY, incoming.delivers),
9818 (TaskRef::DELIVERED_BY_KEY, incoming.delivered_by),
9819 ] {
9820 set_task_list(&mut metadata, key, entries);
9821 }
9822 record_edges(&mut metadata, fallback);
9823 metadata
9824}
9825
9826/// Hold the far ends no relationship here can name under [`DependencyEdge::RECORDED_KEY`] in
9827/// one slot's metadata, or no such key when there are none.
9828fn record_edges(metadata: &mut BTreeMap<String, Value>, fallback: &[DependencyEdge]) {
9829 if fallback.is_empty() {
9830 metadata.remove(DependencyEdge::RECORDED_KEY);
9831 } else {
9832 metadata.insert(
9833 DependencyEdge::RECORDED_KEY.to_owned(),
9834 Value::Array(
9835 fallback
9836 .iter()
9837 .map(|edge| json!({"id":edge.to.id(),"kind":edge.to.kind}))
9838 .collect(),
9839 ),
9840 );
9841 }
9842}
9843
9844/// Every label one item carries, from its content's own connection and nowhere else.
9845///
9846/// There is no second place to read one from: no document this source sends selects the
9847/// board's built-in `Labels` field, because GitHub derives it from the content and a draft
9848/// cannot carry one at all. The module documentation records the three schema facts that
9849/// settle it.
9850fn labels(content: &Value) -> Result<Vec<Label>, SourceError> {
9851 optional_nodes(content.get("labels"), "content labels")?
9852 .into_iter()
9853 .flatten()
9854 .map(|v| {
9855 Ok(Label {
9856 id: NativeId(required_str(v, "id")?.to_owned()),
9857 name: required_str(v, "name")?.to_owned(),
9858 color: optional_str(v, "color")?.map(str::to_owned),
9859 })
9860 })
9861 .collect()
9862}
9863
9864/// The definition of each board field one item's values are values of, in the shape a read
9865/// of the board's own `fields` gives one.
9866///
9867/// A value names its field through a fragment on that field's own type, so the type is
9868/// known from which kind of value it is: a single-select value's field is a
9869/// `ProjectV2SingleSelectField`, options and all, and a text value's is a `ProjectV2Field`.
9870/// A value whose field carried no id, or an empty one, says nothing usable and is left out.
9871fn field_definitions(field_values: &[Value]) -> Vec<Value> {
9872 field_values
9873 .iter()
9874 .filter_map(|value| {
9875 let field = value.get("field")?.as_object()?;
9876 field.get("id")?.as_str().filter(|id| !id.is_empty())?;
9877 let typename = if value.get("text").is_some() {
9878 "ProjectV2Field"
9879 } else if value.get("name").is_some() {
9880 "ProjectV2SingleSelectField"
9881 } else {
9882 return None;
9883 };
9884 let mut defined = field.clone();
9885 defined.insert("__typename".to_owned(), json!(typename));
9886 Some(Value::Object(defined))
9887 })
9888 .collect()
9889}
9890
9891fn text_field(field_values: &[Value], name: &str) -> Result<Option<String>, SourceError> {
9892 let Some(node) = field_values
9893 .iter()
9894 .find(|node| node.pointer("/field/name").and_then(Value::as_str) == Some(name))
9895 else {
9896 return Ok(None);
9897 };
9898 Ok(optional_str(node, "text")?.map(str::to_owned))
9899}
9900
9901fn valid_github_owner(owner: &str) -> bool {
9902 !owner.is_empty()
9903 && owner.len() <= 39
9904 && !owner.starts_with('-')
9905 && !owner.ends_with('-')
9906 && !owner.contains("--")
9907 && owner
9908 .bytes()
9909 .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
9910}
9911
9912/// GitHub's repository-name grammar: 1-100 ASCII letters, digits, `-`, `_` or `.`, and
9913/// neither of the two names a path segment already means.
9914fn valid_github_repository_name(name: &str) -> bool {
9915 !name.is_empty()
9916 && name.len() <= 100
9917 && name != "."
9918 && name != ".."
9919 && name
9920 .bytes()
9921 .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
9922}
9923
9924fn valid_environment_name(name: &str) -> bool {
9925 let mut bytes = name.bytes();
9926 bytes
9927 .next()
9928 .is_some_and(|byte| byte.is_ascii_alphabetic() || byte == b'_')
9929 && bytes.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
9930}
9931
9932/// How many sub-issues one issue has.
9933///
9934/// `Issue.subIssuesSummary` is `SubIssuesSummary!` and its `total` is `Int!`, so an
9935/// absent or non-integer one is a response this source cannot read — and reading it as
9936/// zero would classify a project as a task, which is exactly the mistake the marker
9937/// exists to keep from happening quietly.
9938fn sub_issue_total(issue: &Value) -> Result<u64, SourceError> {
9939 let summary = issue
9940 .get("subIssuesSummary")
9941 .ok_or_else(|| SourceError::Malformed {
9942 message: "GitHub issue is missing subIssuesSummary".into(),
9943 })?;
9944 summary
9945 .get("total")
9946 .and_then(Value::as_u64)
9947 .ok_or_else(|| SourceError::Malformed {
9948 message: "GitHub issue subIssuesSummary.total is not an unsigned integer".into(),
9949 })
9950}
9951
9952/// One issue's own `number`.
9953///
9954/// An issue always has one: GitHub declares `Issue.number` as `Int!` and every selection of
9955/// an issue in this module asks for it. So a read of one that comes back without it, or
9956/// with something that is not an unsigned integer, is a response this source cannot read —
9957/// absence here is **not** "this issue has no number". A draft is the content that has
9958/// none, and a draft never reaches this: the caller decides on `__typename` first, the way
9959/// it does for `subIssuesSummary`, which `DraftIssue` equally declares nothing for.
9960fn issue_number(issue: &Value) -> Result<u64, SourceError> {
9961 issue
9962 .get("number")
9963 .and_then(Value::as_u64)
9964 .ok_or_else(|| SourceError::Malformed {
9965 message: "GitHub issue number is missing or is not an unsigned integer".into(),
9966 })
9967}
9968
9969/// The `number` a creating mutation answered with, and `None` when it answered without one;
9970/// why a missing one is tolerated is at the call in `create_and_file_issue`.
9971fn created_issue_number(created: &Value) -> Result<Option<u64>, SourceError> {
9972 match created.get("number") {
9973 None | Some(Value::Null) => Ok(None),
9974 Some(value) => value
9975 .as_u64()
9976 .map(Some)
9977 .ok_or_else(|| SourceError::Malformed {
9978 message: "GitHub created issue number is not an unsigned integer".into(),
9979 }),
9980 }
9981}
9982
9983fn required_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
9984 value
9985 .get(field)
9986 .and_then(Value::as_str)
9987 .ok_or_else(|| SourceError::Malformed {
9988 message: format!("GitHub response is missing string field {field}"),
9989 })
9990}
9991
9992fn required_nonblank_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
9993 let found = required_str(value, field)?;
9994 if found.trim().is_empty() {
9995 return Err(SourceError::Malformed {
9996 message: format!("GitHub response has blank string field {field}"),
9997 });
9998 }
9999 Ok(found)
10000}
10001
10002/// The slot's delimiters, which `docs/metadata.md` settles once for every source that
10003/// needs one — Linear spells them too, in its own description field.
10004///
10005/// Restated rather than shared, because a plugin crate depends on the contract crate and
10006/// nothing else of this workspace. `scripts/check-metadata-slot-encoding.sh`, a target in
10007/// `check`, is what keeps the two one encoding: drift is otherwise quiet, since each
10008/// source round-trips its own writes perfectly well under its own spelling.
10009const METADATA_OPEN: &str = "<!-- onetaskgraph.metadata\n";
10010const METADATA_CLOSE: &str = "\n-->";
10011
10012/// What the composer puts between a non-empty visible body and the slot, and the one thing
10013/// the parser takes off the visible body when it takes the slot off — exactly once, so every
10014/// other trailing byte of the body comes back as it was written.
10015// 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.
10016const METADATA_SEPARATOR: &str = "\n\n";
10017
10018/// The visible body and the metadata slot at the end of it.
10019///
10020/// The encoding is the one `docs/metadata.md` settles for Linear, which is where its
10021/// reasons are. Only a comment at the very end is a slot; one in the middle is a person's
10022/// own content and is left alone. The visible body is everything before the slot less the
10023/// one [`METADATA_SEPARATOR`] the composer put there, byte for byte.
10024fn metadata_body(
10025 body: Option<String>,
10026) -> Result<(Option<String>, BTreeMap<String, Value>), SourceError> {
10027 let Some(body) = body else {
10028 return Ok((None, BTreeMap::new()));
10029 };
10030 let Some(slot) = slot_span(&body)? else {
10031 return Ok((Some(body), BTreeMap::new()));
10032 };
10033 let metadata =
10034 serde_json::from_str(&body[slot.encoded_start..slot.encoded_end]).map_err(|error| {
10035 SourceError::Malformed {
10036 message: format!(
10037 "invalid canonical JSON in GitHub issue onetaskgraph metadata slot: {error}"
10038 ),
10039 }
10040 })?;
10041 let before = &body[..slot.start];
10042 let visible = before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before);
10043 Ok(((!visible.is_empty()).then(|| visible.to_owned()), metadata))
10044}
10045
10046/// Where the metadata slot sits in one body, as byte offsets into it.
10047struct SlotSpan {
10048 /// Where [`METADATA_OPEN`] begins.
10049 start: usize,
10050 /// Where the encoded JSON begins, just past [`METADATA_OPEN`].
10051 encoded_start: usize,
10052 /// Where the encoded JSON ends, at the start of [`METADATA_CLOSE`].
10053 encoded_end: usize,
10054 /// Just past [`METADATA_CLOSE`].
10055 end: usize,
10056}
10057
10058/// The slot at the very end of `body`, or `None` when it has none.
10059///
10060/// The one reading of *where the slot is*, shared by [`metadata_body`], which reads it, and
10061/// [`with_slot`], which rewrites it — so the two cannot disagree about which comment is the
10062/// slot.
10063fn slot_span(body: &str) -> Result<Option<SlotSpan>, SourceError> {
10064 let Some(start) = body.rfind(METADATA_OPEN) else {
10065 return Ok(None);
10066 };
10067 let encoded_start = start + METADATA_OPEN.len();
10068 let Some(relative_end) = body[encoded_start..].find(METADATA_CLOSE) else {
10069 return Err(SourceError::Malformed {
10070 message: "unterminated onetaskgraph metadata slot in GitHub issue body".into(),
10071 });
10072 };
10073 let encoded_end = encoded_start + relative_end;
10074 let end = encoded_end + METADATA_CLOSE.len();
10075 if !body[end..].trim().is_empty() {
10076 return Ok(None);
10077 }
10078 Ok(Some(SlotSpan {
10079 start,
10080 encoded_start,
10081 encoded_end,
10082 end,
10083 }))
10084}
10085
10086/// `body` with its metadata slot holding exactly `metadata`, and every byte outside the
10087/// slot as it was.
10088///
10089/// A slot that is there has its JSON replaced in place; one that becomes empty is removed
10090/// together with the one [`METADATA_SEPARATOR`] separating it from the prose before it. A
10091/// body with no slot gains one the way [`compose_body`] writes it — after that separator,
10092/// or alone in an empty body — and a body with no slot that is given no metadata is
10093/// returned as it is.
10094fn with_slot(body: &str, metadata: &BTreeMap<String, Value>) -> Result<String, SourceError> {
10095 let encoded = if metadata.is_empty() {
10096 None
10097 } else {
10098 Some(
10099 serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
10100 message: error.to_string(),
10101 })?,
10102 )
10103 };
10104 Ok(match (slot_span(body)?, encoded) {
10105 (Some(slot), Some(encoded)) => format!(
10106 "{}{encoded}{}",
10107 &body[..slot.encoded_start],
10108 &body[slot.encoded_end..]
10109 ),
10110 (Some(slot), None) => {
10111 let before = &body[..slot.start];
10112 format!(
10113 "{}{}",
10114 before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before),
10115 &body[slot.end..]
10116 )
10117 }
10118 (None, None) => body.to_owned(),
10119 (None, Some(encoded)) if body.is_empty() => {
10120 format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
10121 }
10122 (None, Some(encoded)) => {
10123 format!("{body}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
10124 }
10125 })
10126}
10127
10128/// `body` with everything before its metadata slot replaced by `content`, and the slot
10129/// itself kept byte for byte.
10130///
10131/// The inverse of how [`metadata_body`] splits a body: the slot, when there is one, follows
10132/// `content` after the one [`METADATA_SEPARATOR`] the composer puts there — or alone, when
10133/// `content` is empty — so a read of the result reports `content` as the visible body and
10134/// the slot's metadata exactly as it was.
10135fn with_content(body: &str, content: &str) -> Result<String, SourceError> {
10136 let Some(slot) = slot_span(body)? else {
10137 return Ok(content.to_owned());
10138 };
10139 let kept = &body[slot.start..];
10140 Ok(if content.is_empty() {
10141 kept.to_owned()
10142 } else {
10143 format!("{content}{METADATA_SEPARATOR}{kept}")
10144 })
10145}
10146
10147/// Hold `entries` under `key` in one slot's metadata, or no such key when there are none.
10148fn set_task_list(metadata: &mut BTreeMap<String, Value>, key: &str, entries: &[TaskRef]) {
10149 if entries.is_empty() {
10150 metadata.remove(key);
10151 } else {
10152 metadata.insert(
10153 key.to_owned(),
10154 Value::Array(
10155 entries
10156 .iter()
10157 .map(|entry| Value::String(entry.as_str().to_owned()))
10158 .collect(),
10159 ),
10160 );
10161 }
10162}
10163
10164fn compose_body(
10165 content: Option<&str>,
10166 metadata: &BTreeMap<String, Value>,
10167) -> Result<Option<String>, SourceError> {
10168 let visible = content.unwrap_or_default();
10169 if metadata.is_empty() {
10170 return Ok((!visible.is_empty()).then(|| visible.to_owned()));
10171 }
10172 let encoded = serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
10173 message: error.to_string(),
10174 })?;
10175 Ok(Some(if visible.is_empty() {
10176 format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
10177 } else {
10178 format!("{visible}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
10179 }))
10180}
10181
10182fn required_bool(value: &Value, field: &str) -> Result<bool, SourceError> {
10183 value
10184 .get(field)
10185 .and_then(Value::as_bool)
10186 .ok_or_else(|| SourceError::Malformed {
10187 message: format!("GitHub response is missing boolean field {field}"),
10188 })
10189}
10190fn optional_str<'a>(value: &'a Value, field: &str) -> Result<Option<&'a str>, SourceError> {
10191 match value.get(field) {
10192 None | Some(Value::Null) => Ok(None),
10193 Some(value) => value
10194 .as_str()
10195 .map(Some)
10196 .ok_or_else(|| SourceError::Malformed {
10197 message: format!("GitHub response field {field} is not a string or null"),
10198 }),
10199 }
10200}
10201fn optional_nodes<'a>(
10202 connection: Option<&'a Value>,
10203 name: &str,
10204) -> Result<Option<&'a Vec<Value>>, SourceError> {
10205 match connection {
10206 None | Some(Value::Null) => Ok(None),
10207 Some(value) => value
10208 .get("nodes")
10209 .and_then(Value::as_array)
10210 .map(Some)
10211 .ok_or_else(|| SourceError::Malformed {
10212 message: format!("GitHub {name}.nodes is not an array"),
10213 }),
10214 }
10215}
10216fn complete_connection(connection: &Value, name: &str, size: u32) -> Result<(), SourceError> {
10217 let page_info = connection
10218 .get("pageInfo")
10219 .ok_or_else(|| SourceError::Malformed {
10220 message: format!("GitHub {name} has no pageInfo"),
10221 })?;
10222 if required_bool(page_info, "hasNextPage")? {
10223 return Err(SourceError::Malformed {
10224 message: format!(
10225 "GitHub {name} exceeds the supported nested connection size of {size}"
10226 ),
10227 });
10228 }
10229 Ok(())
10230}
10231fn optional_time(value: &Value, field: &str) -> Result<Option<DateTime<Utc>>, SourceError> {
10232 optional_str(value, field)?
10233 .map(|timestamp| {
10234 timestamp.parse().map_err(|error| SourceError::Malformed {
10235 message: format!("GitHub response field {field} is not a timestamp: {error}"),
10236 })
10237 })
10238 .transpose()
10239}
10240fn validate_page(page: &PageRequest) -> Result<(), SourceError> {
10241 if page.limit == 0 {
10242 Err(SourceError::Config {
10243 message: "page limit must be at least 1".into(),
10244 })
10245 } else {
10246 Ok(())
10247 }
10248}
10249fn next_cursor(connection: &Value) -> Result<Option<Cursor>, SourceError> {
10250 let page = connection
10251 .get("pageInfo")
10252 .filter(|value| value.is_object())
10253 .ok_or_else(|| SourceError::Malformed {
10254 message: "GitHub connection is missing pageInfo".into(),
10255 })?;
10256 if required_bool(page, "hasNextPage")? {
10257 let cursor = required_str(page, "endCursor")?;
10258 validate_cursor_progress(None, cursor)?;
10259 Ok(Some(Cursor(cursor.into())))
10260 } else {
10261 Ok(None)
10262 }
10263}
10264fn validate_cursor_progress(previous: Option<&str>, next: &str) -> Result<(), SourceError> {
10265 if next.is_empty() || previous == Some(next) {
10266 Err(SourceError::Malformed {
10267 message: "GitHub pagination cursor is empty or did not advance".into(),
10268 })
10269 } else {
10270 Ok(())
10271 }
10272}
10273/// The version of this plugin's opaque narrowing-search cursor.
10274pub const SEARCH_CURSOR_VERSION: u32 = 4;
10275
10276#[derive(Serialize, Deserialize)]
10277#[serde(tag = "state", rename_all = "snake_case", deny_unknown_fields)]
10278enum SearchConnection {
10279 Initial {},
10280 Continuing { after: Cursor },
10281 Exhausted {},
10282}
10283impl SearchConnection {
10284 fn after(&self) -> Option<&str> {
10285 match self {
10286 Self::Continuing { after } => Some(&after.0),
10287 _ => None,
10288 }
10289 }
10290 fn exhausted(&self) -> bool {
10291 matches!(self, Self::Exhausted { .. })
10292 }
10293 /// Whether a cursor naming this position, `offset` rows into its page, is one this
10294 /// plugin could have handed out: a page is resumed only part of the way through it — an
10295 /// offset of a whole page or more would skip rows nobody was given — an initial page
10296 /// only once some of it was handed out, and an exhausted connection has no page to be
10297 /// part of the way through.
10298 fn valid_resume(&self, offset: usize) -> bool {
10299 let within = offset < SEARCH_PAGE_SIZE as usize;
10300 match self {
10301 Self::Initial { .. } => offset > 0 && within,
10302 Self::Continuing { after } => !after.0.is_empty() && within,
10303 Self::Exhausted { .. } => offset == 0,
10304 }
10305 }
10306}
10307
10308/// Versioned source cursor. A zero offset and empty own-write ids are omitted.
10309#[derive(Serialize, Deserialize)]
10310#[serde(deny_unknown_fields)]
10311struct SearchPosition {
10312 version: u32,
10313 connection: SearchConnection,
10314 /// How many rows of the page `connection` starts were already handed out.
10315 #[serde(default, skip_serializing_if = "is_zero")]
10316 offset: usize,
10317 #[serde(default, skip_serializing_if = "Vec::is_empty")]
10318 seen: Vec<NativeId>,
10319 #[serde(default, skip_serializing_if = "Vec::is_empty")]
10320 own: Vec<NativeId>,
10321}
10322impl Default for SearchPosition {
10323 fn default() -> Self {
10324 Self {
10325 version: SEARCH_CURSOR_VERSION,
10326 connection: SearchConnection::Initial {},
10327 offset: 0,
10328 seen: Vec::new(),
10329 own: Vec::new(),
10330 }
10331 }
10332}
10333
10334fn is_zero(offset: &usize) -> bool {
10335 *offset == 0
10336}
10337
10338fn numeric_cursor(cursor: Option<&Cursor>) -> Result<usize, SourceError> {
10339 cursor.map_or(Ok(0), |c| {
10340 c.0.parse().map_err(|_| SourceError::Config {
10341 message: "page cursor is invalid".into(),
10342 })
10343 })
10344}
10345fn offset_page<T>(mut items: Vec<T>, offset: usize, limit: usize) -> Page<T> {
10346 if offset > items.len() {
10347 return Page::last(vec![]);
10348 }
10349 let tail = items.split_off(offset);
10350 let mut selected = tail;
10351 let next = (selected.len() > limit).then(|| Cursor((offset + limit).to_string()));
10352 selected.truncate(limit);
10353 Page {
10354 items: selected,
10355 next,
10356 }
10357}
10358
10359/// What [`TaskSource::end_command`] leaves of this source's held state, asserted on the state
10360/// itself, for the two things no journey can observe.
10361///
10362/// The journeys in `crates/onetaskgraph-e2e/tests/e2e/end_command.rs` prove through the engine,
10363/// with and without the call, that a settlement, a board listing and a metadata search each
10364/// read afresh after it — the resolved records, the written-item overlay, the board and its
10365/// search, and the narrowed searches. What they cannot reach is the held field definitions,
10366/// because a status write naming an option a person deleted is refused the same whether or
10367/// not the list is held, and a poisoned lock, because nothing outside the source can panic
10368/// while one of its locks is held. So these assert those directly, and every other holder
10369/// beside them so a holder added later without a clear in the call fails here.
10370#[cfg(test)]
10371mod end_command_tests {
10372 use super::*;
10373
10374 struct Token;
10375
10376 impl SecretResolver for Token {
10377 fn get(&self, var: &str) -> Option<SecretString> {
10378 (var == "GH_PROJECTS_TOKEN").then(|| "test-token".into())
10379 }
10380 }
10381
10382 fn source() -> GitHubProjectsSource {
10383 let config = serde_json::from_value(json!({
10384 "owner": "octo-org", "project_number": 7, "repository": "acme/work",
10385 // Nothing here is sent: the source is only built and its state inspected.
10386 "endpoint": "http://127.0.0.1:9/graphql",
10387 }))
10388 .expect("a usable configuration");
10389 GitHubProjectsSource::new(&SourceName::new("work").unwrap(), config, &Token)
10390 .expect("the source builds")
10391 }
10392
10393 /// One issue as a board read answers it.
10394 fn resolved(source: &GitHubProjectsSource) -> Resolved {
10395 source
10396 .resolve(&json!({
10397 "id": "ITEM-1",
10398 "content": {"__typename": "Issue", "id": "I_1", "title": "Held",
10399 "body": "what a person may since have edited", "state": "OPEN",
10400 "stateReason": null, "url": null, "number": 1,
10401 "subIssuesSummary": {"total": 0},
10402 "labels": {"nodes": [], "pageInfo": {"hasNextPage": false}}},
10403 "fieldValues": {"nodes": [], "pageInfo": {"hasNextPage": false}},
10404 }))
10405 .expect("the item reads")
10406 .expect("an issue")
10407 }
10408
10409 /// Hold something in every holder the call clears, and the repository id it keeps.
10410 fn fill(source: &GitHubProjectsSource) {
10411 let item = resolved(source);
10412 source.created.lock().unwrap().push(item.clone());
10413 source.updated.lock().unwrap().push(item.clone());
10414 *source.board_cache.lock().unwrap() = Some(Board {
10415 id: "PVT-board".into(),
10416 fields: json!({"nodes": []}),
10417 items: vec![item.clone()],
10418 });
10419 *source.search_cache.lock().unwrap() = Some(vec![item.clone()]);
10420 source
10421 .narrowed_cache
10422 .lock()
10423 .unwrap()
10424 .insert("status:todo".into(), vec![item.clone()]);
10425 source
10426 .search_next
10427 .lock()
10428 .unwrap()
10429 .insert("status:todo".into(), Some("cursor".into()));
10430 source
10431 .resolved_cache
10432 .lock()
10433 .unwrap()
10434 .insert(item.id.clone(), item);
10435 *source.fields_cache.lock().unwrap() = Some(BoardFields {
10436 id: BoardId::parse("PVT-board").unwrap(),
10437 fields: json!({"nodes": []}),
10438 });
10439 source
10440 .repository_cache
10441 .lock()
10442 .unwrap()
10443 .insert(RepositoryTarget::parse("acme/work").unwrap(), "R_1".into());
10444 }
10445
10446 fn assert_dropped(source: &GitHubProjectsSource) {
10447 assert!(source.created().unwrap().is_empty(), "created");
10448 assert!(source.updated().unwrap().is_empty(), "updated");
10449 assert!(source.board_cache().unwrap().is_none(), "board");
10450 assert!(source.search_cache.lock().unwrap().is_none(), "search");
10451 assert!(source.narrowed_cache.lock().unwrap().is_empty(), "narrowed");
10452 assert!(
10453 source.search_next.lock().unwrap().is_empty(),
10454 "search paging"
10455 );
10456 assert!(
10457 source.resolved_cache().unwrap().is_empty(),
10458 "resolved records"
10459 );
10460 assert!(source.fields_cache().unwrap().is_none(), "board fields");
10461 assert_eq!(
10462 source.repository_cache().unwrap().len(),
10463 1,
10464 "a repository's node id stays valid and is kept"
10465 );
10466 }
10467
10468 fn end(source: &GitHubProjectsSource) {
10469 tokio::runtime::Builder::new_current_thread()
10470 .build()
10471 .unwrap()
10472 .block_on(source.end_command())
10473 .expect("the command ends");
10474 }
10475
10476 #[test]
10477 fn the_call_drops_every_item_search_and_board_read_and_keeps_repository_ids() {
10478 let source = source();
10479 fill(&source);
10480 end(&source);
10481 assert_dropped(&source);
10482 }
10483
10484 #[test]
10485 fn the_call_clears_a_lock_an_earlier_failure_poisoned() {
10486 fn poison<T: Send>(held: &Mutex<T>) {
10487 std::thread::scope(|scope| {
10488 let _ = scope
10489 .spawn(|| {
10490 let _guard = held.lock().unwrap();
10491 panic!("a failure while the lock is held");
10492 })
10493 .join();
10494 });
10495 assert!(held.is_poisoned());
10496 }
10497 let source = source();
10498 fill(&source);
10499 poison(&source.created);
10500 poison(&source.updated);
10501 poison(&source.board_cache);
10502 poison(&source.search_cache);
10503 poison(&source.narrowed_cache);
10504 poison(&source.search_next);
10505 poison(&source.resolved_cache);
10506 poison(&source.fields_cache);
10507 assert!(
10508 source.resolved_cache().is_err(),
10509 "a poisoned lock is refused before the call"
10510 );
10511 end(&source);
10512 assert_dropped(&source);
10513 }
10514}