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 neither `ProjectV2.items` nor any issue the search did not name is 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 by a second or two, so a caller asking again from its last instant should overlap the two by more than that. |
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 /// How fast this source writes, and how long it waits out a refusal.
2431 pacing: Pacing,
2432 /// When the last content-creating mutation finished, or the moment the furthest-out
2433 /// reserved slot releases the next one, whichever is later — so the one after it can be
2434 /// spaced from that. See [`MIN_MUTATION_INTERVAL_MS`] for the interval and
2435 /// [`GitHubProjectsSource::finish_mutation`] for why completion rather than release is
2436 /// what it is measured from.
2437 last_mutation: Mutex<Option<Instant>>,
2438 /// The board as this process last read it, for the length of one command — dropped by
2439 /// [`TaskSource::end_command`].
2440 ///
2441 /// A copy of a project used to re-read the whole board, paged, before writing each of
2442 /// its items, which is by far the largest part of a copy's request count and none of
2443 /// its work. Nothing else changes this board while a command runs — this source's own
2444 /// writes are the only writer — so one read answers them all.
2445 ///
2446 /// It is not a store of a user's work and it is not the cache the no-persistence
2447 /// invariant forbids: it lives and dies with the process exactly as `created` does,
2448 /// nothing is written down, and [`Self::board`] still completes it from `created`, so
2449 /// an item this command created and then depends on resolves whether or not GitHub's
2450 /// own eventually-consistent read has caught up. A write to an item already on the
2451 /// board updates the entry here too, so what this holds is the last read plus this
2452 /// process's own writes rather than a snapshot taken before them.
2453 board_cache: Mutex<Option<Board>>,
2454 /// Every issue this board's own search reported, for the length of one command — dropped
2455 /// by [`TaskSource::end_command`].
2456 ///
2457 /// The second half of a board read, and cached for the same reason and on the same
2458 /// terms as the first: it lives and dies with the process, nothing is written down, and
2459 /// a write this process makes updates the entry here exactly as it updates the one in
2460 /// [`Self::board_cache`]. One read answers every question a command asks, so a command
2461 /// that lists this board's projects and its tasks pays for one search rather than two.
2462 search_cache: Mutex<Option<Vec<Resolved>>>,
2463 /// What each narrowed question GitHub was asked answered, keyed by that question, for
2464 /// the length of one command — dropped by [`TaskSource::end_command`].
2465 ///
2466 /// The narrowed counterpart of [`Self::search_cache`], held on the same terms: it lives
2467 /// and dies with the process, nothing is written down, a write this process makes
2468 /// updates the entry here as it updates the other two, and every answer is completed
2469 /// with this process's own writes each time it is given. A command that asks the same
2470 /// narrowed question twice — a wait polling for its own items, a listing repeated after a
2471 /// write — pays for it once, which is what the whole-board read it replaced gave it.
2472 narrowed_cache: Mutex<BTreeMap<String, Vec<Resolved>>>,
2473 search_next: Mutex<BTreeMap<String, Option<String>>>,
2474 /// Records already resolved in this command, reused by writes and for comment identity.
2475 /// Explicit item reads still reach GitHub. Nothing is persisted, and
2476 /// [`TaskSource::end_command`] drops every record, so a write in the next command reads
2477 /// its item as a person has since left it.
2478 resolved_cache: Mutex<BTreeMap<NativeId, Resolved>>,
2479 /// The board's own id and field definitions as this process last read them on their
2480 /// own, for the length of one command — dropped by [`TaskSource::end_command`].
2481 ///
2482 /// What a write needs of the board and its item does not say, read once per command
2483 /// rather than once per item written, on the terms [`Self::board_cache`] is held on: it
2484 /// lives and dies with the process and nothing is written down. It holds no item and so
2485 /// can answer no question about one — see [`Self::board_fields`].
2486 fields_cache: Mutex<Option<BoardFields>>,
2487 /// Each destination repository's node id, resolved once per repository
2488 /// rather than per issue created.
2489 ///
2490 /// A repository's node id does not change, and re-reading it for every issue of a copy
2491 /// spent one request per item on an answer this source already had. It is a map rather
2492 /// than one entry because a copy files each item in the repository its own
2493 /// `repositories` field names, so a plan across five repositories asks GitHub five
2494 /// times and not once per item.
2495 repository_cache: Mutex<BTreeMap<RepositoryTarget, String>>,
2496 /// What every request this source sends is recorded into.
2497 ///
2498 /// Ordinary code path, not a mode: [`Self::send_once`] records into it at the one place
2499 /// a request leaves this crate, so nothing has to be switched on for a session to be
2500 /// counted. It is shared rather than owned so a caller accounting for a whole session —
2501 /// its own schema verification, board lookups, residue sweep and cleanup beside this
2502 /// source's reads and writes — adds up one accounting instead of two. See
2503 /// [`accounting`] for what a record carries and what a session's spend is and is not.
2504 ledger: Arc<Accounting>,
2505}
2506
2507/// GitHub's closed single-select color vocabulary.
2508#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2509#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
2510pub enum StatusOptionColor {
2511 /// Gray.
2512 Gray,
2513 /// Blue.
2514 Blue,
2515 /// Green.
2516 Green,
2517 /// Yellow.
2518 Yellow,
2519 /// Purple.
2520 Purple,
2521 /// Red.
2522 Red,
2523 /// Orange.
2524 Orange,
2525 /// Pink.
2526 Pink,
2527}
2528
2529/// Whether a guarded board setup — of the fields, or of the Status options alone — plans or
2530/// applies its additions.
2531#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2532pub enum SetupMode {
2533 /// Read without mutation.
2534 Plan,
2535 /// Apply and verify.
2536 Apply,
2537}
2538
2539/// The name [`SetupMode`] had when Status was the one field set up, kept so a caller written
2540/// against it goes on compiling.
2541pub type StatusOptionsMode = SetupMode;
2542
2543/// The explicit result of the requested operation.
2544#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2545#[serde(rename_all = "kebab-case")]
2546pub enum StatusOptionsOutcome {
2547 /// A read-only plan.
2548 Planned,
2549 /// Apply found nothing missing.
2550 Unchanged,
2551 /// Additions were applied and verified.
2552 Applied,
2553}
2554
2555/// A GitHub single-select option's opaque GraphQL node identifier.
2556#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2557#[serde(transparent)]
2558pub struct StatusOptionId(#[schemars(length(min = 1))] String);
2559
2560impl TryFrom<String> for StatusOptionId {
2561 type Error = String;
2562
2563 fn try_from(id: String) -> Result<Self, Self::Error> {
2564 if id.trim().is_empty() {
2565 return Err("a GitHub Status option id cannot be blank".to_owned());
2566 }
2567 Ok(Self(id))
2568 }
2569}
2570
2571/// One existing or proposed option in a guarded Status-field update.
2572#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2573pub struct StatusOption {
2574 /// GitHub's stable id.
2575 pub id: StatusOptionId,
2576 /// The visible option name.
2577 pub name: ColumnName,
2578 /// GitHub's single-select color token.
2579 pub color: StatusOptionColor,
2580 /// The option description, including an empty one.
2581 pub description: String,
2582}
2583
2584/// One board item's Status assignment, retained as recovery data.
2585#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2586pub struct StatusAssignment {
2587 /// The project item id whose assignment this is.
2588 // llmlint: ignore[invalid_states_unrepresentable] This opaque GraphQL node ID is
2589 // carried verbatim as operator recovery data; introducing a semantic type would claim
2590 // validation rules GitHub does not publish and no operation here interprets.
2591 pub item_id: String,
2592 /// The selected option, absent when the item has no status.
2593 #[serde(skip_serializing_if = "Option::is_none")]
2594 pub option: Option<AssignedStatusOption>,
2595}
2596
2597/// The inseparable id and name of an assigned option.
2598#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2599pub struct AssignedStatusOption {
2600 /// GitHub's stable id.
2601 pub id: StatusOptionId,
2602 /// The visible name.
2603 pub name: ColumnName,
2604}
2605
2606/// The plan and verified outcome of reconciling configured Status options.
2607#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2608pub struct StatusOptionsReport {
2609 /// The configured source name.
2610 pub source: SourceName,
2611 /// Configured option names absent before the operation.
2612 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a
2613 // `ColumnName` and has therefore already passed its nonblank validation; retaining the
2614 // serialized string here preserves the report's intentionally simple public contract.
2615 pub missing: Vec<String>,
2616 /// What the requested operation did.
2617 pub outcome: StatusOptionsOutcome,
2618 /// The complete option list observed before any mutation.
2619 pub existing: Vec<StatusOption>,
2620}
2621
2622#[derive(Debug, Clone, PartialEq, Eq)]
2623struct StatusSnapshot {
2624 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
2625 // passed back as the mutation's project identity; a newtype could enforce no stronger
2626 // invariant because GitHub publishes no grammar for it.
2627 board_id: String,
2628 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
2629 // passed back as the mutation's field identity; a newtype could enforce no stronger
2630 // invariant because GitHub publishes no grammar for it.
2631 field_id: String,
2632 options: Vec<StatusOption>,
2633 assignments: Vec<StatusAssignment>,
2634}
2635
2636/// The name of the board field a status is held in.
2637const STATUS_FIELD: &str = "Status";
2638
2639/// Every item's value of each field `report` names, as it stood before the setup wrote
2640/// anything — what a person puts back when the setup is refused part way.
2641fn recovery(report: &FieldsReport, before: &BoardSnapshot) -> Result<String, SourceError> {
2642 let assignments: BTreeMap<&str, Vec<StatusAssignment>> = report
2643 .fields
2644 .iter()
2645 .map(|field| (field.field.name(), before.assignments(field.field)))
2646 .collect();
2647 serde_json::to_string_pretty(&assignments).map_err(|error| SourceError::Malformed {
2648 message: format!("cannot render the pre-write field recovery snapshot: {error}"),
2649 })
2650}
2651
2652/// One board field the guarded setup reads and writes — every one it reads, and the only
2653/// ones it writes.
2654#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, schemars::JsonSchema)]
2655pub enum BoardField {
2656 /// The single-select `Status` field every instance's `status_mapping` resolves into.
2657 Status,
2658 /// The single-select `Priority` field an instance's `priority_mapping` resolves into.
2659 Priority,
2660}
2661
2662impl BoardField {
2663 /// The field's name on the board.
2664 #[must_use]
2665 pub const fn name(self) -> &'static str {
2666 match self {
2667 Self::Status => STATUS_FIELD,
2668 Self::Priority => PRIORITY_FIELD,
2669 }
2670 }
2671
2672 /// The field a board calls `name`, or `None` for one this setup does not own.
2673 fn named(name: &str) -> Option<Self> {
2674 [Self::Status, Self::Priority]
2675 .into_iter()
2676 .find(|field| field.name() == name)
2677 }
2678}
2679
2680/// What the guarded setup did to one field.
2681#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2682#[serde(rename_all = "kebab-case")]
2683pub enum FieldOutcome {
2684 /// A read-only plan.
2685 Planned,
2686 /// Apply found the field there with every configured option.
2687 Unchanged,
2688 /// Missing options were added to the field that was there, and verified.
2689 Applied,
2690 /// The field was not there; it was created holding the configured options, and verified.
2691 Created,
2692}
2693
2694/// One field's plan, or its verified outcome.
2695#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2696pub struct FieldReport {
2697 /// Which field.
2698 pub field: BoardField,
2699 /// Whether the board had the field before the operation.
2700 // llmlint: ignore[invalid_states_unrepresentable] `exists` beside `outcome` is the report's
2701 // wire shape as its consumer's contract fixes it — `{"field", "exists", "missing",
2702 // "outcome", "existing"}` — so folding one into the other would change a published JSON
2703 // shape. The contradictory pairings cannot be built: `GitHubProjectsSource::fields` is the
2704 // one constructor, and it derives `outcome` from `exists` in one match.
2705 pub exists: bool,
2706 /// Configured option names the field lacked before the operation — every one of them,
2707 /// in the order a new field lists them, when the field was not there at all.
2708 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
2709 // mapping name and has therefore already passed its nonblank validation; the serialized
2710 // string is the report's intentionally simple public contract, as `StatusOptionsReport`'s is.
2711 pub missing: Vec<String>,
2712 /// For the `Status` field, which item kind each missing name is configured for: one
2713 /// entry per kind `status_mapping` names a missing option for, task before project, each
2714 /// listing that kind's missing names in category order. A name both kinds use is in
2715 /// both. Empty — and left out of the JSON — when nothing is missing, and always for
2716 /// `Priority`, which only a task holds.
2717 #[serde(default, skip_serializing_if = "Vec::is_empty")]
2718 // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
2719 // both SDKs model an absent `kinds` as an empty list rather than as `null`.
2720 #[schemars(!skip_serializing_if)]
2721 pub kinds: Vec<KindMissing>,
2722 /// What the requested operation did.
2723 pub outcome: FieldOutcome,
2724 /// The field's complete option list observed before any mutation; empty when the field
2725 /// was not there.
2726 pub existing: Vec<StatusOption>,
2727}
2728
2729/// The `Status` option names one item kind's `status_mapping` names that the field lacked.
2730#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2731pub struct KindMissing {
2732 /// The kind these names are configured for.
2733 pub kind: ItemKind,
2734 /// The names that kind maps a category to and the field lacked, in category order.
2735 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
2736 // mapping name, as `FieldReport::missing`'s do, and the serialized string is the report's
2737 // intentionally simple public contract.
2738 pub missing: Vec<String>,
2739}
2740
2741/// The plan and verified outcome of setting up every field a source's configuration names.
2742#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2743pub struct FieldsReport {
2744 /// The configured source name.
2745 pub source: SourceName,
2746 /// `Status`, always, and `Priority` when the source sets `priority_mapping`.
2747 // llmlint: ignore[invalid_states_unrepresentable] A list is the report's wire shape as its
2748 // consumer's contract fixes it — `{"source", "fields": [...]}` — so a struct with one member
2749 // per field would change a published JSON shape. The states the list could hold and the
2750 // contract forbids cannot be built: `GitHubProjectsSource::fields` is the one constructor,
2751 // and it pushes `Status` first and exactly once, then `Priority` exactly when configured.
2752 pub fields: Vec<FieldReport>,
2753}
2754
2755/// Which options one field is configured with, in the order a new field would list them.
2756struct FieldPlan {
2757 field: BoardField,
2758 wanted: Vec<String>,
2759}
2760
2761/// One single-select field as the guarded setup snapshots it.
2762#[derive(Debug, Clone, PartialEq, Eq)]
2763struct SnapshotField {
2764 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
2765 // passed back as the mutation's field identity; a newtype could enforce no stronger
2766 // invariant because GitHub publishes no grammar for it.
2767 field_id: String,
2768 options: Vec<StatusOption>,
2769}
2770
2771/// Every single-select field of a board and every item's value of each.
2772#[derive(Debug, Clone, PartialEq, Eq)]
2773struct BoardSnapshot {
2774 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
2775 // passed back as the mutation's project identity; a newtype could enforce no stronger
2776 // invariant because GitHub publishes no grammar for it.
2777 board_id: String,
2778 fields: BTreeMap<BoardField, SnapshotField>,
2779 /// Each board item's id, and its value of each field this setup owns that it holds one of.
2780 items: Vec<(String, BTreeMap<BoardField, AssignedStatusOption>)>,
2781}
2782
2783impl BoardSnapshot {
2784 /// Every item's value of `field`, in board order — the recovery data a drift refusal
2785 /// carries.
2786 fn assignments(&self, field: BoardField) -> Vec<StatusAssignment> {
2787 self.items
2788 .iter()
2789 .map(|(item_id, values)| StatusAssignment {
2790 item_id: item_id.clone(),
2791 option: values.get(&field).cloned(),
2792 })
2793 .collect()
2794 }
2795}
2796
2797impl GitHubProjectsSource {
2798 /// Report missing configured Status options and, when `apply` is true, add them with
2799 /// a whole-list mutation that preserves every existing id and verifies the result.
2800 ///
2801 /// # Errors
2802 ///
2803 /// Refuses a board without a single-select `Status` field. A post-write difference in
2804 /// any pre-existing option id or item assignment is refused with the complete pre-write
2805 /// assignment snapshot in the diagnostic for recovery.
2806 // llmlint: ignore[changed_behavior_has_e2e] The CLI journeys cover plan, no-op apply,
2807 // successful mutation, both drift refusals, source selection, missing Status, casing,
2808 // and paging. Transport errors remain the shared `graphql` boundary's behavior rather
2809 // than a new status-options behavior, and the pinned-schema test prevents valid GitHub
2810 // responses from entering the defensive malformed-response branches below.
2811 pub async fn status_options(
2812 &self,
2813 mode: StatusOptionsMode,
2814 ) -> Result<StatusOptionsReport, SourceError> {
2815 let before = self.status_snapshot().await?;
2816 // A terminal category's option is as configured as an open one's: a terminal
2817 // write validates it before closing and refuses when the board lacks it. Both
2818 // kinds' names are options of the one field, so both are asked for.
2819 let missing = self
2820 .statuses
2821 .wanted()
2822 .into_iter()
2823 .filter(|wanted| {
2824 !before
2825 .options
2826 .iter()
2827 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
2828 })
2829 .collect::<Vec<_>>();
2830 let report = StatusOptionsReport {
2831 source: self.name.clone(),
2832 missing: missing.clone(),
2833 outcome: match (mode, missing.is_empty()) {
2834 (StatusOptionsMode::Plan, _) => StatusOptionsOutcome::Planned,
2835 (StatusOptionsMode::Apply, true) => StatusOptionsOutcome::Unchanged,
2836 (StatusOptionsMode::Apply, false) => StatusOptionsOutcome::Applied,
2837 },
2838 existing: before.options.clone(),
2839 };
2840 if mode == StatusOptionsMode::Plan || missing.is_empty() {
2841 return Ok(report);
2842 }
2843 let mut options = before
2844 .options
2845 .iter()
2846 .map(|option| {
2847 json!({
2848 "id": option.id, "name": option.name, "color": option.color,
2849 "description": option.description,
2850 })
2851 })
2852 .collect::<Vec<_>>();
2853 options.extend(missing.iter().map(|name| {
2854 json!({
2855 "name": name, "color": "GRAY", "description": ""
2856 })
2857 }));
2858 self.graphql(
2859 graphql::STATUS_OPTIONS_UPDATE,
2860 json!({"input": {
2861 "projectId": before.board_id, "fieldId": before.field_id,
2862 "singleSelectOptions": options,
2863 }}),
2864 )
2865 .await?;
2866 let after = self.status_snapshot().await?;
2867 let options_preserved = before
2868 .options
2869 .iter()
2870 .all(|old| after.options.iter().any(|new| new == old));
2871 let additions_present = missing.iter().all(|wanted| {
2872 after
2873 .options
2874 .iter()
2875 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
2876 });
2877 if !options_preserved || !additions_present || after.assignments != before.assignments {
2878 let recovery = serde_json::to_string_pretty(&before.assignments).map_err(|error| {
2879 SourceError::Malformed {
2880 message: format!("cannot render pre-write Status recovery snapshot: {error}"),
2881 }
2882 })?;
2883 return Err(SourceError::Refused {
2884 message: format!(
2885 "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}"
2886 ),
2887 });
2888 }
2889 Ok(report)
2890 }
2891
2892 /// A fresh snapshot of the Status field and every board item's assignment of it.
2893 ///
2894 /// # Errors
2895 ///
2896 /// Refuses a board without a single-select `Status` field, and one the token cannot see.
2897 async fn status_snapshot(&self) -> Result<StatusSnapshot, SourceError> {
2898 // Status alone, as this operation has always read it: a `Priority` field is another
2899 // operation's, so nothing about it can refuse this one.
2900 let mut board = self.board_snapshot(&[BoardField::Status]).await?;
2901 let field = board
2902 .fields
2903 .remove(&BoardField::Status)
2904 .ok_or_else(|| self.no_status_field())?;
2905 Ok(StatusSnapshot {
2906 assignments: board.assignments(BoardField::Status),
2907 board_id: board.board_id,
2908 field_id: field.field_id,
2909 options: field.options,
2910 })
2911 }
2912
2913 /// The refusal a board with no `Status` field is answered with by the guarded setup.
2914 fn no_status_field(&self) -> SourceError {
2915 SourceError::Refused {
2916 message: format!("source {} board has no Status field", self.name),
2917 }
2918 }
2919
2920 // llmlint: ignore-block[changed_behavior_has_e2e] Valid snapshot shapes are exercised through
2921 // the real CLI loopback journey, including pagination. The individual malformed guards
2922 // are defensive validation of a schema-pinned third-party response, not separate user
2923 // journeys; drift and missing-field failures cover the operation's recovery behavior.
2924 /// A fresh snapshot of each of the `owned` fields on the board, with its options, and of
2925 /// every board item's value of each, walked to the end of the board's items. A field not
2926 /// in `owned` is read past whatever it holds.
2927 async fn board_snapshot(&self, owned: &[BoardField]) -> Result<BoardSnapshot, SourceError> {
2928 let mut after: Option<String> = None;
2929 let mut snapshot: Option<BoardSnapshot> = None;
2930 loop {
2931 let data = self
2932 .graphql(
2933 graphql::STATUS_OPTIONS_SNAPSHOT,
2934 json!({
2935 "owner": self.owner, "number": self.project_number,
2936 "first": MAX_PAGE_SIZE, "after": after, "nestedFirst": MAX_PAGE_SIZE,
2937 }),
2938 )
2939 .await?;
2940 let board = data
2941 .pointer("/owner/projectV2")
2942 .filter(|board| board.is_object())
2943 .ok_or_else(|| SourceError::Refused {
2944 message: format!(
2945 "source {} has no accessible GitHub Projects board",
2946 self.name
2947 ),
2948 })?;
2949 if board
2950 .pointer("/fields/pageInfo/hasNextPage")
2951 .and_then(Value::as_bool)
2952 != Some(false)
2953 {
2954 return Err(SourceError::Malformed {
2955 message:
2956 "GitHub project fields is incomplete or has malformed pageInfo.hasNextPage"
2957 .into(),
2958 });
2959 }
2960 let mut fields = BTreeMap::new();
2961 // Only the fields this setup owns, by name: a node the single-select fragment did not
2962 // match carries no name, and a person's own single-select field — a `Size`, a
2963 // `Team` — is none of this setup's business, so nothing about it can refuse one. A
2964 // `Status` or `Priority` field without its options is malformed, not absent.
2965 // 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.
2966 for (owned, field) in board
2967 .pointer("/fields/nodes")
2968 .and_then(Value::as_array)
2969 .ok_or_else(|| SourceError::Malformed {
2970 message: "GitHub project fields.nodes is not an array".into(),
2971 })?
2972 .iter()
2973 .filter_map(|field| {
2974 let named = BoardField::named(field.get("name")?.as_str()?)?;
2975 owned.contains(&named).then_some((named, field))
2976 })
2977 {
2978 let options = field
2979 .get("options")
2980 .and_then(Value::as_array)
2981 .ok_or_else(|| SourceError::Malformed {
2982 message: "GitHub single-select field options is not an array".into(),
2983 })?
2984 .iter()
2985 .map(|option| {
2986 Ok(StatusOption {
2987 id: StatusOptionId::try_from(required_str(option, "id")?.to_owned())
2988 .map_err(|message| SourceError::Malformed { message })?,
2989 name: ColumnName::try_from(required_str(option, "name")?.to_owned())
2990 .map_err(|message| SourceError::Malformed {
2991 message: format!(
2992 "GitHub single-select option name is invalid: {message}"
2993 ),
2994 })?,
2995 color: serde_json::from_value(
2996 option.get("color").cloned().unwrap_or(Value::Null),
2997 )
2998 .map_err(|error| {
2999 SourceError::Malformed {
3000 message: format!(
3001 "GitHub single-select option color is invalid: {error}"
3002 ),
3003 }
3004 })?,
3005 description: optional_str(option, "description")?
3006 .unwrap_or_default()
3007 .to_owned(),
3008 })
3009 })
3010 .collect::<Result<Vec<_>, SourceError>>()?;
3011 let snapshot = SnapshotField {
3012 field_id: required_nonblank_str(field, "id")?.to_owned(),
3013 options,
3014 };
3015 // A board's field names are unique, so a second one is an answer that cannot
3016 // say which field the setup would act on — refused rather than one chosen.
3017 if fields.insert(owned, snapshot).is_some() {
3018 return Err(SourceError::Malformed {
3019 message: format!(
3020 "GitHub answered two {} fields for this board",
3021 owned.name()
3022 ),
3023 });
3024 }
3025 }
3026 let board_id = required_nonblank_str(board, "id")?.to_owned();
3027 let current = snapshot.get_or_insert_with(|| BoardSnapshot {
3028 board_id,
3029 fields,
3030 items: Vec::new(),
3031 });
3032 let items = board
3033 .pointer("/items/nodes")
3034 .and_then(Value::as_array)
3035 .ok_or_else(|| SourceError::Malformed {
3036 message: "GitHub project items.nodes is not an array".into(),
3037 })?;
3038 for item in items {
3039 let field_values =
3040 item.get("fieldValues")
3041 .ok_or_else(|| SourceError::Malformed {
3042 message: "GitHub project item is missing fieldValues".into(),
3043 })?;
3044 if field_values
3045 .pointer("/pageInfo/hasNextPage")
3046 .and_then(Value::as_bool)
3047 != Some(false)
3048 {
3049 return Err(SourceError::Malformed {
3050 message: "GitHub project item fieldValues is incomplete or has malformed pageInfo.hasNextPage".into(),
3051 });
3052 }
3053 let values = item
3054 .pointer("/fieldValues/nodes")
3055 .and_then(Value::as_array)
3056 .ok_or_else(|| SourceError::Malformed {
3057 message: "GitHub project item fieldValues.nodes is not an array".into(),
3058 })?;
3059 let item_id = required_nonblank_str(item, "id")?;
3060 let mut assigned = BTreeMap::new();
3061 for value in values {
3062 let Some(field) = value
3063 .pointer("/field/name")
3064 .and_then(Value::as_str)
3065 .and_then(BoardField::named)
3066 .filter(|field| owned.contains(field))
3067 else {
3068 continue;
3069 };
3070 let held = assigned.insert(
3071 field,
3072 AssignedStatusOption {
3073 id: StatusOptionId::try_from(
3074 required_str(value, "optionId")?.to_owned(),
3075 )
3076 .map_err(|message| SourceError::Malformed { message })?,
3077 name: ColumnName::try_from(required_str(value, "name")?.to_owned())
3078 .map_err(|message| SourceError::Malformed {
3079 message: format!(
3080 "GitHub assigned {} name is invalid: {message}",
3081 field.name()
3082 ),
3083 })?,
3084 },
3085 );
3086 // An item holds one value of a field, so a second one leaves no way to
3087 // tell which it holds — and a verification or recovery built on either
3088 // could restore the wrong one.
3089 if held.is_some() {
3090 return Err(SourceError::Malformed {
3091 message: format!(
3092 "GitHub answered two {} values for board item {item_id}",
3093 field.name()
3094 ),
3095 });
3096 }
3097 }
3098 current.items.push((item_id.to_owned(), assigned));
3099 }
3100 let page = board.get("items").ok_or_else(|| SourceError::Malformed {
3101 message: "GitHub project is missing items".into(),
3102 })?;
3103 let has_next = page
3104 .pointer("/pageInfo/hasNextPage")
3105 .and_then(Value::as_bool)
3106 .ok_or_else(|| SourceError::Malformed {
3107 message: "GitHub project items.pageInfo.hasNextPage is not a boolean".into(),
3108 })?;
3109 if !has_next {
3110 break;
3111 }
3112 let next =
3113 required_nonblank_str(page.get("pageInfo").unwrap_or(&Value::Null), "endCursor")?;
3114 validate_cursor_progress(after.as_deref(), next)?;
3115 after = Some(next.to_owned());
3116 }
3117 snapshot.ok_or_else(|| SourceError::Malformed {
3118 message: "GitHub returned no board field snapshot".into(),
3119 })
3120 }
3121 // llmlint: ignore-end[changed_behavior_has_e2e]
3122
3123 /// Report every board field this source's configuration names and, with
3124 /// [`SetupMode::Apply`], set each up: add the options a field lacks, and create
3125 /// the `Priority` field when the board has none.
3126 ///
3127 /// The fields are `Status`, always, with the options `status_mapping` resolves to; and
3128 /// `Priority`, when `priority_mapping` is set, with its four mapped options — created in
3129 /// the order urgent, high, medium, low. An option a field already has keeps its id, name,
3130 /// color and description: the whole option list goes back with every existing id, because
3131 /// a re-minted id clears every item's value.
3132 ///
3133 /// # Errors
3134 ///
3135 /// Refuses a board without a single-select `Status` field. After an apply the board is
3136 /// read again, and a pre-existing option or any item's value of either field that moved is
3137 /// refused with the complete pre-write assignments in the diagnostic, for recovery.
3138 // llmlint: ignore[changed_behavior_has_e2e] The `sources fields` journeys drive plan,
3139 // unchanged apply, a created field, an added option to each field, drift refusal, a board
3140 // with no Status field and a non-github-projects source through the compiled CLI against
3141 // the loopback board. Transport errors are the shared `graphql` boundary's behavior.
3142 pub async fn fields(&self, mode: SetupMode) -> Result<FieldsReport, SourceError> {
3143 let owned: Vec<BoardField> = if self.priorities.is_some() {
3144 vec![BoardField::Status, BoardField::Priority]
3145 } else {
3146 vec![BoardField::Status]
3147 };
3148 let before = self.board_snapshot(&owned).await?;
3149 let mut plans = vec![FieldPlan {
3150 field: BoardField::Status,
3151 wanted: self.statuses.wanted(),
3152 }];
3153 if !before.fields.contains_key(&BoardField::Status) {
3154 return Err(self.no_status_field());
3155 }
3156 if let Some(mapping) = &self.priorities {
3157 plans.push(FieldPlan {
3158 field: BoardField::Priority,
3159 wanted: mapping.names().map(str::to_owned).collect(),
3160 });
3161 }
3162 // The snapshot reads single-select fields alone, so a field it did not find may still
3163 // be on the board under the name, of another type: creating one beside it would fail
3164 // part way, or leave two fields of one name. Asked of the board's own field list, and
3165 // only when a field is missing.
3166 if plans
3167 .iter()
3168 .any(|plan| !before.fields.contains_key(&plan.field))
3169 {
3170 let board = self.board_fields().await?;
3171 for plan in plans
3172 .iter()
3173 .filter(|plan| !before.fields.contains_key(&plan.field))
3174 {
3175 if let Some(field) = Board::field(&board.fields, plan.field.name())? {
3176 return Err(SourceError::Refused {
3177 message: format!(
3178 "source {}'s board has a {} field that is not a single-select field \
3179 (it is a {}), so it cannot hold this source's options; next: rename \
3180 or remove that field, then run this again",
3181 self.name,
3182 plan.field.name(),
3183 optional_str(field, "__typename")?.unwrap_or("field of another type")
3184 ),
3185 });
3186 }
3187 }
3188 }
3189 let mut reports = Vec::new();
3190 for plan in &plans {
3191 let held = before.fields.get(&plan.field);
3192 let existing = held.map(|field| field.options.clone()).unwrap_or_default();
3193 let mut missing: Vec<String> = Vec::new();
3194 for wanted in &plan.wanted {
3195 let present = existing
3196 .iter()
3197 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3198 || missing
3199 .iter()
3200 .any(|named| named.eq_ignore_ascii_case(wanted));
3201 if !present {
3202 missing.push(wanted.clone());
3203 }
3204 }
3205 let kinds = match plan.field {
3206 BoardField::Status => self.statuses.missing_by_kind(&existing),
3207 BoardField::Priority => Vec::new(),
3208 };
3209 reports.push(FieldReport {
3210 field: plan.field,
3211 exists: held.is_some(),
3212 kinds,
3213 outcome: match (mode, held.is_some(), missing.is_empty()) {
3214 (SetupMode::Plan, _, _) => FieldOutcome::Planned,
3215 (SetupMode::Apply, true, true) => FieldOutcome::Unchanged,
3216 (SetupMode::Apply, true, false) => FieldOutcome::Applied,
3217 (SetupMode::Apply, false, _) => FieldOutcome::Created,
3218 },
3219 missing,
3220 existing,
3221 });
3222 }
3223 let report = FieldsReport {
3224 source: self.name.clone(),
3225 fields: reports,
3226 };
3227 let writes: Vec<&FieldReport> = report
3228 .fields
3229 .iter()
3230 .filter(|field| !field.missing.is_empty() || !field.exists)
3231 .collect();
3232 if mode == SetupMode::Plan || writes.is_empty() {
3233 return Ok(report);
3234 }
3235 let mut landed: Vec<&str> = Vec::new();
3236 for field in &writes {
3237 let added = field
3238 .missing
3239 .iter()
3240 .map(|name| json!({"name": name, "color": "GRAY", "description": ""}));
3241 let sent = match before.fields.get(&field.field) {
3242 Some(held) => {
3243 let mut options = held
3244 .options
3245 .iter()
3246 .map(|option| {
3247 json!({
3248 "id": option.id, "name": option.name, "color": option.color,
3249 "description": option.description,
3250 })
3251 })
3252 .collect::<Vec<_>>();
3253 options.extend(added);
3254 self.graphql(
3255 graphql::STATUS_OPTIONS_UPDATE,
3256 json!({"input": {
3257 "projectId": before.board_id, "fieldId": held.field_id,
3258 "singleSelectOptions": options,
3259 }}),
3260 )
3261 .await
3262 }
3263 None => {
3264 self.graphql(
3265 graphql::CREATE_FIELD,
3266 json!({"input": {
3267 "projectId": before.board_id, "dataType": "SINGLE_SELECT",
3268 "name": field.field.name(),
3269 "singleSelectOptions": added.collect::<Vec<_>>(),
3270 }}),
3271 )
3272 .await
3273 }
3274 };
3275 // A mutation that failed does not establish that GitHub left its field as it was,
3276 // so every failure from here on carries the recovery data a drift refusal does.
3277 match sent {
3278 Ok(_) => landed.push(field.field.name()),
3279 Err(error) => {
3280 let changed = if landed.is_empty() {
3281 String::new()
3282 } else {
3283 format!("changed the {} field and then ", landed.join(" and "))
3284 };
3285 return Err(SourceError::Refused {
3286 message: format!(
3287 "the guarded field setup {changed}failed on the {} field, which it may \
3288 have changed part way: {error}; the pre-write item assignments \
3289 are:\n{}",
3290 field.field.name(),
3291 recovery(&report, &before)?
3292 ),
3293 });
3294 }
3295 }
3296 }
3297 // The board has been written, so a verification read that fails leaves it unverified
3298 // rather than unchanged, and says what to put back.
3299 let after = match self.board_snapshot(&owned).await {
3300 Ok(after) => after,
3301 Err(error) => {
3302 return Err(SourceError::Refused {
3303 message: format!(
3304 "the guarded field setup changed the {} field and then could not read the \
3305 board back to verify it: {error}; the pre-write item assignments are:\n{}",
3306 landed.join(" and "),
3307 recovery(&report, &before)?
3308 ),
3309 });
3310 }
3311 };
3312 let mut moved = Vec::new();
3313 for field in &report.fields {
3314 let name = field.field.name();
3315 let now = after
3316 .fields
3317 .get(&field.field)
3318 .map(|held| held.options.as_slice())
3319 .unwrap_or_default();
3320 if !field.existing.iter().all(|old| now.contains(old)) {
3321 moved.push(format!(
3322 "a pre-existing {name} option id, name, color or description"
3323 ));
3324 }
3325 if !field.missing.iter().all(|wanted| {
3326 now.iter()
3327 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3328 }) {
3329 moved.push(format!("an added {name} option"));
3330 }
3331 if after.assignments(field.field) != before.assignments(field.field) {
3332 moved.push(format!("an item's {name} value"));
3333 }
3334 }
3335 if !moved.is_empty() {
3336 return Err(SourceError::Refused {
3337 message: format!(
3338 "GitHub changed {} after the guarded field setup; the pre-write item \
3339 assignments are:\n{}",
3340 moved.join(", "),
3341 recovery(&report, &before)?
3342 ),
3343 });
3344 }
3345 Ok(report)
3346 }
3347
3348 /// Validate configuration and capture the named credential without exposing it.
3349 ///
3350 /// # Errors
3351 ///
3352 /// Returns [`SourceError::Config`] for a configuration this instance cannot use and
3353 /// [`SourceError::Auth`] when the named credential is missing or empty.
3354 pub fn new(
3355 name: &SourceName,
3356 config: GitHubProjectsConfig,
3357 secrets: &dyn SecretResolver,
3358 ) -> Result<Self, SourceError> {
3359 Self::recording_into(name, config, secrets, Arc::new(Accounting::new()))
3360 }
3361
3362 /// The same, recording every request it sends into an accounting the caller holds too.
3363 ///
3364 /// [`Self::new`] is this with an accounting of its own. A caller that is also making
3365 /// its own calls to GitHub — a lane verifying a schema, sweeping residue or cleaning
3366 /// up — passes the one it records those into, so the session total accounts for the
3367 /// whole session rather than for this source's share of it.
3368 ///
3369 /// # Errors
3370 ///
3371 /// Exactly [`Self::new`]'s: [`SourceError::Config`] for a configuration this instance
3372 /// cannot use and [`SourceError::Auth`] when the named credential is missing or empty.
3373 pub fn recording_into(
3374 name: &SourceName,
3375 config: GitHubProjectsConfig,
3376 secrets: &dyn SecretResolver,
3377 ledger: Arc<Accounting>,
3378 ) -> Result<Self, SourceError> {
3379 if !valid_github_owner(&config.owner) {
3380 return Err(SourceError::Config {
3381 message: "owner must be 1-39 ASCII letters, digits, or single hyphens, and cannot start or end with a hyphen".into(),
3382 });
3383 }
3384 if config.project_number == 0 || config.project_number > i32::MAX as u32 {
3385 return Err(SourceError::Config {
3386 message: format!("project_number must be between 1 and {}", i32::MAX),
3387 });
3388 }
3389 if !valid_environment_name(&config.token_env) {
3390 return Err(SourceError::Config {
3391 message: "token_env must be a valid environment-variable name".into(),
3392 });
3393 }
3394 let repository = config
3395 .repository
3396 .as_deref()
3397 .map(RepositoryTarget::parse)
3398 .transpose()?;
3399 let endpoint = Url::parse(&config.endpoint).map_err(|e| SourceError::Config {
3400 message: format!("endpoint is not a valid URL: {e}"),
3401 })?;
3402 if endpoint.scheme() != "https"
3403 && !(endpoint.scheme() == "http"
3404 && endpoint
3405 .host_str()
3406 .is_some_and(|h| h == "127.0.0.1" || h == "localhost" || h == "::1"))
3407 {
3408 return Err(SourceError::Config {
3409 message:
3410 "endpoint must use HTTPS (HTTP is accepted only for a loopback test server)"
3411 .into(),
3412 });
3413 }
3414 let token = secrets.get(&config.token_env).filter(|token| !token.expose_secret().trim().is_empty()).ok_or_else(|| SourceError::Auth {
3415 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),
3416 })?;
3417 Ok(Self {
3418 name: name.clone(),
3419 owner: config.owner,
3420 project_number: config.project_number,
3421 repository,
3422 endpoint,
3423 token,
3424 credential_name: config.token_env,
3425 statuses: BoardStatuses::resolve(&config.status_mapping, name)?,
3426 priorities: config
3427 .priority_mapping
3428 .map(|mapping| PriorityMapping::resolve(mapping, name))
3429 .transpose()?,
3430 client: Client::builder()
3431 .user_agent("onetaskgraph")
3432 .build()
3433 .map_err(|e| SourceError::Config {
3434 message: format!("cannot build HTTP client: {e}"),
3435 })?,
3436 created: Mutex::new(Vec::new()),
3437 updated: Mutex::new(Vec::new()),
3438 pacing: Pacing::resolve(config.pacing, name)?,
3439 last_mutation: Mutex::new(None),
3440 board_cache: Mutex::new(None),
3441 search_cache: Mutex::new(None),
3442 narrowed_cache: Mutex::new(BTreeMap::new()),
3443 resolved_cache: Mutex::new(BTreeMap::new()),
3444 search_next: Mutex::new(BTreeMap::new()),
3445 fields_cache: Mutex::new(None),
3446 repository_cache: Mutex::new(BTreeMap::new()),
3447 ledger,
3448 })
3449 }
3450
3451 /// A snapshot of every request this source has sent, and what each cost.
3452 ///
3453 /// A value to hold and compare rather than a borrow of the accounting itself, so two
3454 /// of them can sit side by side. When this source was built with
3455 /// [`Self::recording_into`] the snapshot is the whole shared session, which is the
3456 /// point of building it that way.
3457 #[must_use]
3458 pub fn accounting(&self) -> accounting::Session {
3459 self.ledger.snapshot()
3460 }
3461
3462 /// Send one GraphQL document, pacing this source's own mutations and waiting out a
3463 /// rate limit rather than handing it straight back as an error.
3464 ///
3465 /// Retrying is safe for every document here, including the mutations, and the reason
3466 /// is that only a *refusal* is retried: [`Limiter::classify`] rules on a response
3467 /// GitHub sent, and a request GitHub refused for a rate limit did not run, so nothing
3468 /// this replays has already taken effect. An outcome this source cannot know — the
3469 /// send failed, or the body could not be read, so the mutation may well have landed —
3470 /// is [`Attempt::Failed`] in [`send_once`] and leaves this loop without a second
3471 /// attempt. A duplicate write would come from replaying one of those, and none is
3472 /// replayed.
3473 async fn graphql(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
3474 if is_mutation(query)
3475 && ![
3476 graphql::ADD_COMMENT,
3477 graphql::UPDATE_COMMENT,
3478 graphql::DELETE_COMMENT,
3479 ]
3480 .contains(&query)
3481 {
3482 let mut cache = self.resolved_cache()?;
3483 for argument in ["input", "second", "third", "clear"] {
3484 if let Some(input) = variables.get(argument) {
3485 cache.retain(|id, item| {
3486 !["id", "issueId", "subjectId", "itemId"].iter().any(|key| {
3487 input
3488 .get(key)
3489 .and_then(Value::as_str)
3490 .is_some_and(|value| value == id.0 || value == item.item_id)
3491 })
3492 });
3493 }
3494 }
3495 }
3496 let doing = operation_description(query);
3497 let mut waited = Duration::ZERO;
3498 let mut waits = 0_u32;
3499 let mut backoff = self.pacing.retry_backoff;
3500 loop {
3501 if is_mutation(query) {
3502 let spacing = self.reserve_mutation_slot();
3503 if !spacing.is_zero() {
3504 tokio::time::sleep(spacing).await;
3505 }
3506 }
3507 let attempt = self.send_once(query, &variables).await;
3508 if is_mutation(query) {
3509 self.finish_mutation();
3510 }
3511 let limited = match attempt {
3512 Ok(data) => return Ok(data),
3513 Err(Attempt::Failed(error)) => return Err(error),
3514 Err(Attempt::Limited(limited)) => limited,
3515 };
3516 // GitHub really does send `retry-after: 0`, and retrying at once is the one
3517 // move that extends a secondary limit, so a hint below the schedule's own next
3518 // wait is raised to it.
3519 let wait = match limited.hint {
3520 Some(hint) => Duration::from_secs(hint).max(backoff),
3521 None => backoff,
3522 };
3523 let remaining = self.pacing.retry_budget.saturating_sub(waited);
3524 // A wait of nothing spends none of the budget, so it is exhaustion rather
3525 // than a retry. `Pacing::resolve` rules out every way of configuring one
3526 // except a budget of zero, where reporting the first refusal is the ask.
3527 if wait.is_zero() || wait > remaining {
3528 return Err(limited.exhausted(
3529 doing,
3530 waits,
3531 waited,
3532 wait,
3533 self.pacing.retry_budget,
3534 ));
3535 }
3536 tokio::time::sleep(wait).await;
3537 waited += wait;
3538 waits += 1;
3539 backoff = backoff.saturating_mul(2);
3540 }
3541 }
3542
3543 /// The next moment a content-creating mutation may leave this source, as a wait from
3544 /// now.
3545 ///
3546 /// The slot is reserved under the lock and the waiting happens outside it, so two
3547 /// callers take two slots rather than the same one — and no lock is held across an
3548 /// await.
3549 ///
3550 /// The moment it is spaced from is the previous mutation's *completion*, which
3551 /// [`Self::finish_mutation`] records. See that method for why the release moment on its
3552 /// own is the wrong thing to measure from.
3553 fn reserve_mutation_slot(&self) -> Duration {
3554 if self.pacing.min_mutation_interval.is_zero() {
3555 return Duration::ZERO;
3556 }
3557 // A poisoned lock here costs pacing, not correctness, and refusing the write over
3558 // it would turn an earlier failure into a second one for no gain.
3559 let mut last = self
3560 .last_mutation
3561 .lock()
3562 .unwrap_or_else(std::sync::PoisonError::into_inner);
3563 let now = Instant::now();
3564 // `checked_add` rather than `+`: `Instant + Duration` panics on overflow, and
3565 // pacing is not worth a panic even at a bound `MAX_PACING_MS` already rules out.
3566 let at = last.map_or(now, |previous| {
3567 previous
3568 .checked_add(self.pacing.min_mutation_interval)
3569 .map_or(now, |earliest| earliest.max(now))
3570 });
3571 *last = Some(at);
3572 at.saturating_duration_since(now)
3573 }
3574
3575 /// Record that a content-creating mutation has finished, so the next one is spaced
3576 /// from here rather than from the moment this one was released.
3577 ///
3578 /// This source can only choose when a request *departs*; the limiter counts when it
3579 /// *arrives*, and the two differ by whatever the request spent in transit. Spacing one
3580 /// departure from the last therefore hands the limiter a gap of the interval less that
3581 /// transit, so a source pacing at 750 ms can still be seen arriving faster — which is
3582 /// exactly how a copy paced well inside a board's threshold was refused by it on a
3583 /// slower machine while passing on a quick one.
3584 ///
3585 /// Spacing from completion removes the subtraction rather than budgeting for it. The
3586 /// previous request had already arrived before its response came back, so its arrival
3587 /// is no later than this moment, and the next mutation is released at least the
3588 /// interval after this moment and arrives no earlier than it is released: the gap the
3589 /// limiter measures is therefore at least the interval, whatever transit costs and on
3590 /// whatever platform. The price is that a mutation's own round trip no longer counts
3591 /// towards its spacing, which makes this source slightly slower than the configured
3592 /// rate rather than slightly faster — the safe side of a limit that punishes being
3593 /// wrong by refusing reads for the next fifty minutes.
3594 ///
3595 /// A failed attempt is recorded too: a request refused by the limiter still arrived,
3596 /// and one that never left costs only a wait nobody needed.
3597 fn finish_mutation(&self) {
3598 if self.pacing.min_mutation_interval.is_zero() {
3599 return;
3600 }
3601 // A poisoned lock here costs pacing, not correctness, exactly as in the reservation.
3602 let mut last = self
3603 .last_mutation
3604 .lock()
3605 .unwrap_or_else(std::sync::PoisonError::into_inner);
3606 let now = Instant::now();
3607 // `max` rather than an assignment: a concurrent caller may already have reserved a
3608 // slot further out, and completing this request must never pull that slot back in.
3609 *last = Some(last.map_or(now, |reserved| reserved.max(now)));
3610 }
3611
3612 /// One HTTP attempt, classified into an answer, a rate limit to wait out, or a
3613 /// failure that waiting cannot help — and recorded, whichever of the three it was.
3614 ///
3615 /// This is the one place a request leaves this crate, which is why the accounting is
3616 /// here rather than at each of the callers: a read path added later is counted without
3617 /// anybody remembering to count it, and
3618 /// `the_session_report_counts_every_request_the_board_served_and_what_each_cost` fails
3619 /// when one is not.
3620 async fn send_once(&self, query: &str, variables: &Value) -> Result<Value, Attempt> {
3621 let Attempted {
3622 result,
3623 limits,
3624 reported_cost,
3625 } = self.attempt(query, variables).await;
3626 // No `otherwise` name: every document this source sends is one of its own, and the
3627 // inventory gate on `graphql::DOCUMENTS` is what keeps that true.
3628 let sending = accounting::Request::graphql(query, variables, None, reported_cost);
3629 let outcome = match &result {
3630 Ok(_) => accounting::Outcome::Answered,
3631 Err(Attempt::Limited(_)) => accounting::Outcome::RateLimited,
3632 Err(Attempt::Failed(_)) => accounting::Outcome::Refused,
3633 };
3634 self.ledger.record(sending.finished(outcome, limits));
3635 result
3636 }
3637
3638 /// The attempt itself, with what its response said about the rate limit alongside.
3639 ///
3640 /// The two are returned together rather than recorded here because every one of the
3641 /// early exits below is a different outcome, and a record written at each of them is a
3642 /// record one of them can be added without.
3643 async fn attempt(&self, query: &str, variables: &Value) -> Attempted {
3644 let mut limits = accounting::RateLimit::default();
3645 let mut reported_cost = None;
3646 let result = self
3647 .attempted(query, variables, &mut limits, &mut reported_cost)
3648 .await;
3649 Attempted {
3650 result,
3651 limits,
3652 reported_cost,
3653 }
3654 }
3655
3656 /// One HTTP attempt, filling in what its response said about the rate limit as it goes.
3657 async fn attempted(
3658 &self,
3659 query: &str,
3660 variables: &Value,
3661 limits: &mut accounting::RateLimit,
3662 reported_cost: &mut Option<u64>,
3663 ) -> Result<Value, Attempt> {
3664 let response = self
3665 .client
3666 .post(self.endpoint.clone())
3667 .bearer_auth(self.token.expose_secret())
3668 .json(&json!({"query": query, "variables": variables}))
3669 .send()
3670 .await
3671 .map_err(|e| {
3672 Attempt::Failed(SourceError::Unavailable {
3673 message: format!("GitHub GraphQL request failed: {e}"),
3674 })
3675 })?;
3676 let status = response.status();
3677 let header = |name: &str| whole_seconds(response.headers().get(name));
3678 *limits = accounting::RateLimit::read(|name| {
3679 response
3680 .headers()
3681 .get(name)
3682 .and_then(|value| value.to_str().ok())
3683 .map(str::to_owned)
3684 });
3685 // Exactly `0` is exhaustion and everything else — a count, an empty value, bytes
3686 // that are not text at all — is "not known to be exhausted". This never makes a
3687 // response a refusal on its own: it says which limiter a refusal is attributed to
3688 // and where its hint comes from, so a value this cannot read costs a hint rather
3689 // than an answer.
3690 let exhausted = response
3691 .headers()
3692 .get("x-ratelimit-remaining")
3693 .and_then(|value| value.to_str().ok())
3694 == Some("0");
3695 // `retry-after` is what GitHub asks for when it asks; when it does not and the
3696 // primary budget is spent, `x-ratelimit-reset` says when that budget comes back,
3697 // which is the same question answered as an absolute time. Nothing else here is a
3698 // hint, and a schedule is what answers a refusal that carries none.
3699 let hint = header("retry-after").or_else(|| {
3700 exhausted
3701 .then(|| header("x-ratelimit-reset"))
3702 .flatten()
3703 .map(|reset| reset.saturating_sub(Utc::now().timestamp().max(0).unsigned_abs()))
3704 });
3705 // Read before it is parsed, because the evidence which tells a secondary rate
3706 // limit from a rejected credential is in the body of a response whose status says
3707 // only "forbidden" — and a non-success response was never parsed at all.
3708 let body = response.text().await.map_err(|e| {
3709 Attempt::Failed(SourceError::Unavailable {
3710 message: format!("GitHub GraphQL response could not be read: {e}"),
3711 })
3712 })?;
3713 if let Some(limiter) = Limiter::classify(status, exhausted, &body) {
3714 return Err(Attempt::Limited(Limited { limiter, hint }));
3715 }
3716 if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {
3717 return Err(Attempt::Failed(SourceError::Auth {
3718 message: format!(
3719 "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"
3720 ),
3721 }));
3722 }
3723 if !status.is_success() {
3724 return Err(Attempt::Failed(SourceError::Unavailable {
3725 message: format!("GitHub GraphQL returned HTTP {status}"),
3726 }));
3727 }
3728 // GitHub reports what a call cost only when the document asked it to, and no
3729 // document this source sends does — so this is `None` here and carries the figure
3730 // for a caller whose own document selects `rateLimit { cost }`. What it must never
3731 // pick up is a `dryRun` probe's cost, which is some other document's.
3732 *reported_cost = serde_json::from_str::<Value>(&body)
3733 .ok()
3734 .as_ref()
3735 .and_then(|body| body.pointer("/data/rateLimit/cost"))
3736 .and_then(Value::as_u64);
3737 self.answer(&body).map_err(Attempt::Failed)
3738 }
3739
3740 /// What one successful HTTP response says, once its GraphQL errors are read.
3741 fn answer(&self, body: &str) -> Result<Value, SourceError> {
3742 let body: Value = serde_json::from_str(body).map_err(|e| SourceError::Malformed {
3743 message: format!("GitHub returned invalid JSON: {e}"),
3744 })?;
3745 let errors = body
3746 .get("errors")
3747 .map(|value| {
3748 value.as_array().ok_or_else(|| SourceError::Malformed {
3749 message: "GitHub response errors is not an array".into(),
3750 })
3751 })
3752 .transpose()?;
3753 if let Some(errors) = errors.filter(|errors| !errors.is_empty()) {
3754 let messages = errors
3755 .iter()
3756 .filter_map(|e| e.get("message").and_then(Value::as_str))
3757 .collect::<Vec<_>>()
3758 .join("; ");
3759 let message = if messages.is_empty() {
3760 "GitHub returned GraphQL errors".into()
3761 } else {
3762 messages
3763 };
3764 let normalized = message.to_ascii_lowercase();
3765 if normalized.contains("resource not accessible") || normalized.contains("scope") {
3766 return Err(SourceError::Auth {
3767 message: format!(
3768 "{message}; grant {} Projects and Issues read/write plus Pull requests read-only access for every repository represented on the board",
3769 self.credential_name
3770 ),
3771 });
3772 }
3773 return Err(SourceError::Refused { message });
3774 }
3775 body.get("data")
3776 .filter(|data| data.is_object())
3777 .cloned()
3778 .ok_or_else(|| SourceError::Malformed {
3779 message: "GitHub response has no data object".into(),
3780 })
3781 }
3782
3783 // llmlint: ignore[boundary_inputs_validated] GitHub caps nested connections at 100 and
3784 // GraphQL cannot independently page them inside the outer item page. This source page is
3785 // deliberately bounded at that published maximum; the live drift journey exercises it.
3786 async fn board_page(
3787 &self,
3788 items_after: Option<&str>,
3789 items_first: u32,
3790 ) -> Result<Value, SourceError> {
3791 let data = self
3792 .graphql(
3793 graphql::BOARD,
3794 json!({"owner":self.owner,"number":self.project_number,
3795 "first":items_first.min(MAX_PAGE_SIZE),"after":items_after,
3796 "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
3797 )
3798 .await?;
3799 data.pointer("/owner/projectV2")
3800 .filter(|v| !v.is_null())
3801 .cloned()
3802 .ok_or_else(|| SourceError::Refused {
3803 message: format!(
3804 "GitHub project {}/{} was not found or is not visible to the token",
3805 self.owner, self.project_number
3806 ),
3807 })
3808 }
3809
3810 /// The search that finds the issues of this board, narrowed by `also` when it is
3811 /// given.
3812 ///
3813 /// `project:owner/number` is what scopes a search to one board, and `is:issue` is what
3814 /// keeps pull requests out of it: GitHub's `ISSUE` search type covers both, and a pull
3815 /// request is somebody's change rather than a unit of plan. `-has:parent` is *not*
3816 /// here on purpose — GitHub accepts it and silently ignores it, so a project is told
3817 /// from a task by the `parent` field each issue carries rather than by the search.
3818 fn board_search(&self, also: Option<&str>) -> String {
3819 let scope = format!("project:{}/{} is:issue", self.owner, self.project_number);
3820 match also {
3821 Some(also) => format!("{scope} {also}"),
3822 None => scope,
3823 }
3824 }
3825
3826 /// One issue this source reached directly, as the board item a read of the board would
3827 /// have produced — or `None` when this board does not hold it.
3828 ///
3829 /// The board half of an issue rides along on `Issue.projectItems`, so the value handed
3830 /// to [`Self::resolve`] is the very shape a `ProjectV2.items` read gives it: the board
3831 /// item's own id, that item's field values, and the issue as its content. One resolver
3832 /// for both routes is what makes an issue read through a search, through its own node
3833 /// id, or through its project's sub-issues report the same title, the same status, the
3834 /// same labels and the same qualified id.
3835 ///
3836 /// An issue with no entry for *this* board is not this source's to report, which is
3837 /// what keeps an id naming some other repository's issue from being answered as an item
3838 /// of this board. That answer is given about an **exhausted** connection and never
3839 /// about an unread page: the entry is looked for on the page in hand, and only if that
3840 /// page reports more of the connection, in [`Self::board_membership`]'s walk of the
3841 /// rest of it.
3842 async fn resolve_issue(&self, issue: &Value) -> Result<Option<Resolved>, SourceError> {
3843 if optional_str(issue, "__typename")? != Some("Issue") {
3844 return Ok(None);
3845 }
3846 let memberships = issue
3847 .get("projectItems")
3848 .ok_or_else(|| SourceError::Malformed {
3849 message: "GitHub issue is missing projectItems".into(),
3850 })?;
3851 let nodes = memberships
3852 .get("nodes")
3853 .and_then(Value::as_array)
3854 .ok_or_else(|| SourceError::Malformed {
3855 message: "GitHub issue projectItems.nodes is not an array".into(),
3856 })?;
3857 let held = match self.board_entry(nodes) {
3858 Some(held) => held.clone(),
3859 None => {
3860 let info = memberships
3861 .get("pageInfo")
3862 .ok_or_else(|| SourceError::Malformed {
3863 message: "GitHub issue projectItems has no pageInfo".into(),
3864 })?;
3865 // The page held no entry for this board. Whether that means the issue is
3866 // not on it is a question about the rest of the connection, and only a
3867 // connection with no rest answers it here.
3868 if !required_bool(info, "hasNextPage")? {
3869 return Ok(None);
3870 }
3871 let cursor = required_str(info, "endCursor")?;
3872 validate_cursor_progress(None, cursor)?;
3873 let issue_id = required_str(issue, "id")?;
3874 match self.board_membership(issue_id, cursor).await? {
3875 Some(held) => held,
3876 None => return Ok(None),
3877 }
3878 }
3879 };
3880 let item = json!({
3881 "id": required_str(&held, "id")?,
3882 "project": held.get("project"),
3883 "fieldValues": held.get("fieldValues"),
3884 "content": issue,
3885 });
3886 self.resolve(&item)
3887 }
3888
3889 /// This board's own entry among one page of an issue's `Issue.projectItems`.
3890 ///
3891 /// One spelling of *which membership is this board's*, so the page a read carries and
3892 /// the pages [`Self::board_membership`] walks are searched by the same rule.
3893 fn board_entry<'a>(&self, nodes: &'a [Value]) -> Option<&'a Value> {
3894 nodes.iter().find(|node| {
3895 node.pointer("/project/number").and_then(Value::as_u64)
3896 == Some(u64::from(self.project_number))
3897 })
3898 }
3899
3900 /// The rest of one issue's board memberships, from `after`, for this board's entry.
3901 ///
3902 /// The recovery read: a page of memberships that holds no entry for this board says
3903 /// nothing about the memberships past it, so the connection is walked to exhaustion
3904 /// before an issue is reported as one this board does not hold. `Ok(None)` is that
3905 /// positive answer — the whole connection was read and no entry named this board —
3906 /// rather than a failure, and the walk is held to
3907 /// [`validate_cursor_progress`] like every other page walk here, so a source answering
3908 /// with a cursor that does not advance is refused instead of spun on.
3909 async fn board_membership(
3910 &self,
3911 issue: &str,
3912 after: &str,
3913 ) -> Result<Option<Value>, SourceError> {
3914 let mut after = after.to_owned();
3915 loop {
3916 let data = self
3917 .graphql(
3918 graphql::ISSUE_BOARD_ITEMS,
3919 json!({"id":issue,"first":MAX_PAGE_SIZE,"after":after,
3920 "nestedFirst":NESTED_PAGE_SIZE}),
3921 )
3922 .await?;
3923 let Some(connection) = data
3924 .pointer("/node/projectItems")
3925 .filter(|value| !value.is_null())
3926 else {
3927 // The id resolved to nothing, or to something with no memberships to walk —
3928 // which is the same answer as a connection holding no entry for this board.
3929 return Ok(None);
3930 };
3931 let nodes = connection
3932 .get("nodes")
3933 .and_then(Value::as_array)
3934 .ok_or_else(|| SourceError::Malformed {
3935 message: "GitHub issue projectItems.nodes is not an array".into(),
3936 })?;
3937 if let Some(held) = self.board_entry(nodes) {
3938 return Ok(Some(held.clone()));
3939 }
3940 let info = connection
3941 .get("pageInfo")
3942 .ok_or_else(|| SourceError::Malformed {
3943 message: "GitHub issue projectItems has no pageInfo".into(),
3944 })?;
3945 let next = required_bool(info, "hasNextPage")?
3946 .then(|| required_str(info, "endCursor"))
3947 .transpose()?;
3948 match next {
3949 Some(next) => {
3950 validate_cursor_progress(Some(&after), next)?;
3951 after = next.to_owned();
3952 }
3953 None => return Ok(None),
3954 }
3955 }
3956 }
3957
3958 /// One page of a board-scoped issue search, and where the next page resumes.
3959 async fn search_page(
3960 &self,
3961 search: &str,
3962 first: u32,
3963 after: Option<&str>,
3964 ) -> Result<(Vec<Resolved>, Option<String>), SourceError> {
3965 let data = self
3966 .graphql(
3967 graphql::SEARCH_ISSUES,
3968 json!({"search":search,"type":"ISSUE","first":first.min(MAX_PAGE_SIZE),
3969 "after":after,"nestedFirst":NESTED_PAGE_SIZE,
3970 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
3971 )
3972 .await?;
3973 let connection = data.get("search").ok_or_else(|| SourceError::Malformed {
3974 message: "GitHub search response has no search connection".into(),
3975 })?;
3976 let mut found = Vec::new();
3977 for node in connection
3978 .get("nodes")
3979 .and_then(Value::as_array)
3980 .ok_or_else(|| SourceError::Malformed {
3981 message: "GitHub search nodes is not an array".into(),
3982 })?
3983 {
3984 if let Some(resolved) = self.resolve_issue(node).await? {
3985 found.push(resolved);
3986 }
3987 }
3988 let info = connection
3989 .get("pageInfo")
3990 .ok_or_else(|| SourceError::Malformed {
3991 message: "GitHub search connection has no pageInfo".into(),
3992 })?;
3993 let next = required_bool(info, "hasNextPage")?
3994 .then(|| required_str(info, "endCursor"))
3995 .transpose()?
3996 .map(str::to_owned);
3997 if let Some(next) = &next {
3998 validate_cursor_progress(after, next)?;
3999 }
4000 Ok((found, next))
4001 }
4002
4003 /// Every issue this board holds, completed with what this run wrote.
4004 ///
4005 /// The completion is not an optimisation and it is not a cache: GitHub's issue search
4006 /// is an index and is eventually consistent, so an issue this run created seconds ago
4007 /// can be absent from it, and a project listed straight after being written would
4008 /// otherwise be missing from its own board. What is added back is only what this
4009 /// process itself wrote, out of [`Self::created`], which lives and dies with the
4010 /// process.
4011 async fn board_issues(&self) -> Result<Vec<Resolved>, SourceError> {
4012 let found = self.searched_issues().await?;
4013 self.completed_with_written(found, |_| true)
4014 }
4015
4016 /// Every issue this board's own search reports, walked to exhaustion, read once per
4017 /// source.
4018 ///
4019 /// The uncompleted half of [`Self::board_issues`], separated because [`Self::board`]
4020 /// needs it too and the two would otherwise walk the same search twice in one command.
4021 /// See [`Self::search_cache`] for why holding it is the same bargain holding the board
4022 /// is.
4023 async fn searched_issues(&self) -> Result<Vec<Resolved>, SourceError> {
4024 let cached = self.search_cache()?.clone();
4025 if let Some(held) = cached {
4026 return Ok(held);
4027 }
4028 let mut after: Option<String> = None;
4029 let mut found = Vec::new();
4030 let search = self.board_search(None);
4031 loop {
4032 let (page, next) = self
4033 .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
4034 .await?;
4035 found.extend(page);
4036 match next {
4037 Some(next) => after = Some(next),
4038 None => break,
4039 }
4040 }
4041 *self.search_cache()? = Some(found.clone());
4042 Ok(found)
4043 }
4044
4045 /// This process's own view of the board's issues, or the refusal a poisoned lock is.
4046 fn search_cache(
4047 &self,
4048 ) -> Result<std::sync::MutexGuard<'_, Option<Vec<Resolved>>>, SourceError> {
4049 self.search_cache
4050 .lock()
4051 .map_err(|_| SourceError::Unavailable {
4052 message: "this source's view of the board's issues was left inconsistent by an \
4053 earlier failure; next: run the command again"
4054 .into(),
4055 })
4056 }
4057
4058 /// `found`, with everything this run wrote that `keep` accepts and the read did not
4059 /// report.
4060 ///
4061 /// See [`Self::created`] and [`Self::board_issues`] for why a read has to be completed
4062 /// at all: the search index is behind, and a node read of an item filed moments ago can
4063 /// be too.
4064 fn completed_with_written(
4065 &self,
4066 mut found: Vec<Resolved>,
4067 keep: impl Fn(&Resolved) -> bool,
4068 ) -> Result<Vec<Resolved>, SourceError> {
4069 for own in self.created()?.iter().filter(|own| keep(own)) {
4070 if !found.iter().any(|item| item.id == own.id) {
4071 found.push(own.clone());
4072 }
4073 }
4074 Ok(found)
4075 }
4076
4077 /// What resolving one node id reached.
4078 ///
4079 /// Three answers rather than an `Option`, because a board *draft* is none of the other
4080 /// two: it is not an issue, so the issue fragment reads nothing of it, and a read of one
4081 /// is completed by a read of the draft itself rather than reported as nothing.
4082 async fn reach(&self, id: &NativeId) -> Result<Reached, SourceError> {
4083 let asked = self
4084 .graphql(
4085 graphql::ISSUE,
4086 json!({"id":id.0,"first":MAX_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
4087 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
4088 )
4089 .await;
4090 let data = match asked {
4091 Ok(data) => data,
4092 // A string that is not a node id at all is not a failure to report: it is an id
4093 // this board does not hold, which is what every read of one already answers.
4094 Err(error) if unresolvable_node(&error) => return Ok(Reached::Nothing),
4095 Err(error) => return Err(error),
4096 };
4097 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
4098 return Ok(Reached::Nothing);
4099 };
4100 if optional_str(node, "__typename")? == Some("DraftIssue") {
4101 return Ok(Reached::Draft);
4102 }
4103 Ok(match self.resolve_issue(node).await? {
4104 Some(item) => Reached::Held(Box::new(item)),
4105 None => Reached::Nothing,
4106 })
4107 }
4108
4109 /// One item of this board by its own id, whatever kind it is.
4110 ///
4111 /// Resolved from the identifier alone: no search, board-wide or otherwise. What this
4112 /// run wrote is read first, because a node read of an item created moments ago can
4113 /// still be behind the board field values written onto it — see [`Self::created`].
4114 async fn item_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
4115 if let Some(own) = self.created()?.iter().find(|own| own.id == *id) {
4116 return Ok(Some(own.clone()));
4117 }
4118 match self.reach(id).await? {
4119 Reached::Held(item) => Ok(Some(*item)),
4120 Reached::Nothing => Ok(None),
4121 Reached::Draft => self.draft_by_id(id).await,
4122 }
4123 }
4124
4125 /// Several items of this board, each by its own id, in order — what [`Self::item_by_id`]
4126 /// answers for each, read [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] rather
4127 /// than one request per id.
4128 ///
4129 /// What this run wrote answers first, as it does there, and only the rest is read. One id
4130 /// left to read is read by [`Self::item_by_id`] itself, which costs what a batch does. A
4131 /// batch GitHub refuses because one of its ids resolves to no node at all is read again one
4132 /// id at a time, so that id is answered as not held and the others as themselves; a draft
4133 /// is completed by a read of the draft, exactly as there.
4134 async fn items_by_ids(&self, ids: &[NativeId]) -> Result<Vec<Option<Resolved>>, SourceError> {
4135 let mut found: Vec<Option<Option<Resolved>>> = {
4136 let created = self.created()?;
4137 ids.iter()
4138 .map(|id| {
4139 created
4140 .iter()
4141 .find(|own| own.id == *id)
4142 .map(|own| Some(own.clone()))
4143 })
4144 .collect()
4145 };
4146 let unread: Vec<NativeId> = ids
4147 .iter()
4148 .zip(&found)
4149 .filter(|(_, found)| found.is_none())
4150 .map(|(id, _)| id.clone())
4151 .collect();
4152 let mut read = Vec::with_capacity(unread.len());
4153 if let [one] = unread.as_slice() {
4154 read.push(self.item_by_id(one).await?);
4155 } else {
4156 for batch in unread.chunks(DETAIL_BATCH) {
4157 let data = match self
4158 .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, None))
4159 .await
4160 {
4161 Ok(data) => data,
4162 Err(error) if unresolvable_node(&error) => {
4163 for id in batch {
4164 read.push(self.item_by_id(id).await?);
4165 }
4166 continue;
4167 }
4168 Err(error) => return Err(error),
4169 };
4170 for (slot, id) in batch.iter().enumerate() {
4171 let node =
4172 data.get(format!("i{slot}"))
4173 .ok_or_else(|| SourceError::Malformed {
4174 message: format!(
4175 "GitHub answered a batch read with no item for {}",
4176 id.0
4177 ),
4178 })?;
4179 read.push(if node.is_null() {
4180 None
4181 } else if optional_str(node, "__typename")? == Some("DraftIssue") {
4182 self.draft_by_id(id).await?
4183 } else {
4184 if optional_str(node, "__typename")? == Some("Issue")
4185 && required_str(node, "id")? != id.0
4186 {
4187 return Err(SourceError::Malformed {
4188 message: format!(
4189 "GitHub answered the read of {} with issue {}",
4190 id.0,
4191 required_str(node, "id")?
4192 ),
4193 });
4194 }
4195 self.resolve_issue(node).await?
4196 });
4197 }
4198 }
4199 }
4200 let mut read = read.into_iter();
4201 Ok(found
4202 .iter_mut()
4203 .map(|slot| slot.take().unwrap_or_else(|| read.next().flatten()))
4204 .collect())
4205 }
4206
4207 fn resolved_cache(
4208 &self,
4209 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
4210 self.resolved_cache
4211 .lock()
4212 .map_err(|_| SourceError::Unavailable {
4213 message: "resolved item records were left inconsistent; run the command again"
4214 .into(),
4215 })
4216 }
4217
4218 /// Reuse a record this invocation already resolved. The mutation sender invalidates
4219 /// it before writing, so a partial failure cannot leave a pre-write binding behind.
4220 async fn bound_item(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
4221 let cached = self.resolved_cache()?.get(id).cloned();
4222 match cached {
4223 Some(item) => Ok(Some(item)),
4224 None => self.item_by_id(id).await,
4225 }
4226 }
4227
4228 /// One board draft by its own id, with the board item it sits in — or `None` when no
4229 /// item of this board is that draft's.
4230 ///
4231 /// The same decision [`Self::resolve_issue`] makes for an issue, over the draft's own
4232 /// `projectV2Items`: an entry naming this board is what makes it this board's. GitHub
4233 /// links a draft to one board item, so the page this read carries is the whole of that
4234 /// connection, and a page that reports more than it holds is refused rather than read
4235 /// as an answer about memberships nobody read.
4236 async fn draft_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
4237 let data = self
4238 .graphql(
4239 graphql::DRAFT,
4240 json!({"id":id.0,"nestedFirst":NESTED_PAGE_SIZE,
4241 "boardItems":BOARD_ITEMS_PAGE_SIZE}),
4242 )
4243 .await?;
4244 // Gone between the two reads is an answer — the draft is no longer there. Anything
4245 // else than the draft [`Self::reach`] was just told this id is, is not one.
4246 let Some(draft) = data.get("node").filter(|node| !node.is_null()) else {
4247 return Ok(None);
4248 };
4249 if optional_str(draft, "__typename")? != Some("DraftIssue") {
4250 return Err(SourceError::Malformed {
4251 message: format!(
4252 "GitHub answered {} as a draft and then as something else",
4253 id.0
4254 ),
4255 });
4256 }
4257 if required_str(draft, "id")? != id.0 {
4258 return Err(SourceError::Malformed {
4259 message: format!("GitHub answered a different draft for {}", id.0),
4260 });
4261 }
4262 let memberships = draft
4263 .get("projectV2Items")
4264 .ok_or_else(|| SourceError::Malformed {
4265 message: format!("GitHub draft {} is missing projectV2Items", id.0),
4266 })?;
4267 let nodes = memberships
4268 .get("nodes")
4269 .and_then(Value::as_array)
4270 .ok_or_else(|| SourceError::Malformed {
4271 message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
4272 })?;
4273 let info = memberships
4274 .get("pageInfo")
4275 .ok_or_else(|| SourceError::Malformed {
4276 message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
4277 })?;
4278 // Read whether or not this board's entry is on the page: a page claiming more than
4279 // the one item GitHub links a draft to is a malformed answer either way.
4280 if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
4281 return Err(SourceError::Malformed {
4282 message: format!(
4283 "GitHub draft {} reports more board items than the one GitHub links a draft \
4284 to",
4285 id.0
4286 ),
4287 });
4288 }
4289 if let Some(node) = nodes.first()
4290 && node
4291 .pointer("/project/number")
4292 .and_then(Value::as_u64)
4293 .is_none()
4294 {
4295 return Err(SourceError::Malformed {
4296 message: format!(
4297 "GitHub draft {} board item has no numeric project number",
4298 id.0
4299 ),
4300 });
4301 }
4302 let Some(held) = self.board_entry(nodes) else {
4303 return Ok(None);
4304 };
4305 if required_str(
4306 held.get("project").ok_or_else(|| SourceError::Malformed {
4307 message: format!("GitHub draft {} board item has no project", id.0),
4308 })?,
4309 "id",
4310 )? != self.board_fields().await?.id.as_str()
4311 {
4312 return Ok(None);
4313 }
4314 let item = json!({
4315 "id": required_str(held, "id")?,
4316 "project": held.get("project"),
4317 "fieldValues": held.get("fieldValues"),
4318 "content": draft,
4319 });
4320 self.resolve(&item)
4321 }
4322
4323 /// The board's own id and field definitions, for a write whose item does not carry
4324 /// them — never its items.
4325 ///
4326 /// A board this command has already listed supplies them, since it read them beside its
4327 /// items; otherwise they come from [`graphql::BOARD_FIELDS`], once per command. Neither
4328 /// is consulted about which items the board holds: see the module documentation for
4329 /// why a question about one known item is answered by reading that item.
4330 async fn board_fields(&self) -> Result<BoardFields, SourceError> {
4331 if let Some(board) = self.board_cache()?.as_ref() {
4332 return Ok(BoardFields {
4333 id: BoardId::parse(&board.id)?,
4334 fields: board.fields.clone(),
4335 });
4336 }
4337 if let Some(held) = self.fields_cache()?.clone() {
4338 return Ok(held);
4339 }
4340 let data = self
4341 .graphql(
4342 graphql::BOARD_FIELDS,
4343 json!({"owner":self.owner,"number":self.project_number,
4344 "nestedFirst":NESTED_PAGE_SIZE}),
4345 )
4346 .await?;
4347 self.fields_read(&data)
4348 }
4349
4350 /// The board's id and fields out of an answer carrying the `boardFields` root, held for
4351 /// the rest of this command.
4352 fn fields_read(&self, data: &Value) -> Result<BoardFields, SourceError> {
4353 let board = data
4354 .pointer("/boardFields/projectV2")
4355 .filter(|value| !value.is_null())
4356 .ok_or_else(|| SourceError::Refused {
4357 message: format!(
4358 "GitHub project {}/{} was not found or is not visible to the token",
4359 self.owner, self.project_number
4360 ),
4361 })?;
4362 let read = BoardFields {
4363 id: BoardId::parse(required_str(board, "id")?)?,
4364 fields: board.get("fields").cloned().unwrap_or(Value::Null),
4365 };
4366 *self.fields_cache()? = Some(read.clone());
4367 Ok(read)
4368 }
4369
4370 /// Read what creating an issue in `repository` needs and this command has not read yet —
4371 /// the board's fields and the repository's node id — in one request when it needs both.
4372 ///
4373 /// When either is already known this sends nothing, and the other is read by its own
4374 /// document where it is asked for, so no create reads anything twice.
4375 async fn creation_context(
4376 &self,
4377 repository: &RepositoryTarget,
4378 incoming: &Incoming<'_>,
4379 ) -> Result<(), SourceError> {
4380 let fields_known = self.board_cache()?.is_some() || self.fields_cache()?.is_some();
4381 if fields_known || self.repository_cache()?.contains_key(repository) {
4382 return Ok(());
4383 }
4384 let data = self
4385 .graphql(
4386 graphql::CREATION_CONTEXT,
4387 json!({"owner":self.owner,"number":self.project_number,
4388 "nestedFirst":NESTED_PAGE_SIZE,"repositoryOwner":repository.owner,
4389 "repositoryName":repository.name}),
4390 )
4391 .await?;
4392 self.fields_read(&data)?;
4393 self.repository_read(&data, repository, incoming)?;
4394 Ok(())
4395 }
4396
4397 /// This process's own view of the board's fields, or the refusal a poisoned lock is.
4398 fn fields_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<BoardFields>>, SourceError> {
4399 self.fields_cache
4400 .lock()
4401 .map_err(|_| SourceError::Unavailable {
4402 message: "this source's view of the board's fields was left inconsistent by an \
4403 earlier failure; next: run the command again"
4404 .into(),
4405 })
4406 }
4407
4408 /// What a write to `item` needs of the board, read off that item when it says enough and
4409 /// off [`Self::board_fields`] when it does not.
4410 ///
4411 /// A node read of an item names its board and carries the definition of every field it
4412 /// holds a value of — so an item naming its board, holding a value of the origin field,
4413 /// and, when the write carries a status, holding a `Status` value, needs no read of the
4414 /// board at all. **Nothing the item does not say is guessed:** a field it holds no value
4415 /// of may still be on the board, and a view reading it as absent would refuse a write the
4416 /// board can take or skip a field write the board needs, so such an item — and a create,
4417 /// which has no item yet — takes the board's fields from their own read instead.
4418 async fn fields_for(
4419 &self,
4420 item: Option<&Resolved>,
4421 writes_status: bool,
4422 selects_priority: bool,
4423 ) -> Result<BoardFields, SourceError> {
4424 if let Some(board) = item.and_then(Resolved::carried_board) {
4425 return Ok(board);
4426 }
4427 if let Some(item) = item
4428 && let Some(board_id) = item.named_board()
4429 && item.defines(ORIGIN_FIELD)
4430 && (!writes_status || item.defines("Status"))
4431 && (!selects_priority || item.defines(PRIORITY_FIELD))
4432 {
4433 return Ok(BoardFields {
4434 id: board_id,
4435 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
4436 });
4437 }
4438 self.board_fields().await
4439 }
4440
4441 /// Everything filed under one issue of this board, walked to exhaustion — or `None`
4442 /// when that id names nothing here with a sub-issue relationship to walk.
4443 ///
4444 /// `None` and an empty answer are different: `None` is *this is not an issue of this
4445 /// GitHub*, which is what sends a project selector on to be read as a name, and an
4446 /// empty vector is a project that holds nothing.
4447 async fn sub_issues(&self, id: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
4448 let mut after: Option<String> = None;
4449 let mut children = Vec::new();
4450 loop {
4451 let asked = self
4452 .graphql(
4453 graphql::SUB_ISSUES,
4454 json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after,
4455 "nestedFirst":NESTED_PAGE_SIZE,
4456 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
4457 )
4458 .await;
4459 let data = match asked {
4460 Ok(data) => data,
4461 // A string that is not a node id at all is not a failure to report: it is
4462 // the ordinary answer to a selector naming a project by its name.
4463 Err(error) if unresolvable_node(&error) => return Ok(None),
4464 Err(error) => return Err(error),
4465 };
4466 let Some(connection) = data
4467 .pointer("/node/subIssues")
4468 .filter(|value| !value.is_null())
4469 else {
4470 // No such node, or one with no sub-issue relationship — a board draft is
4471 // the one this board can really hold.
4472 return Ok(None);
4473 };
4474 for node in connection
4475 .get("nodes")
4476 .and_then(Value::as_array)
4477 .ok_or_else(|| SourceError::Malformed {
4478 message: "GitHub subIssues.nodes is not an array".into(),
4479 })?
4480 {
4481 if let Some(resolved) = self.resolve_issue(node).await? {
4482 children.push(resolved);
4483 }
4484 }
4485 let info = connection
4486 .get("pageInfo")
4487 .ok_or_else(|| SourceError::Malformed {
4488 message: "GitHub subIssues connection has no pageInfo".into(),
4489 })?;
4490 let next = required_bool(info, "hasNextPage")?
4491 .then(|| required_str(info, "endCursor"))
4492 .transpose()?;
4493 match next {
4494 Some(next) => {
4495 validate_cursor_progress(after.as_deref(), next)?;
4496 after = Some(next.to_owned());
4497 }
4498 None => return Ok(Some(children)),
4499 }
4500 }
4501 }
4502
4503 /// Which issue of this board a project *name* is, or `None` when none is.
4504 ///
4505 /// One bounded query which filters on that name at the server, rather than a walk of
4506 /// every issue the board holds. The name is compared again here: the qualifier narrows
4507 /// what GitHub sends, and this source decides what it names.
4508 async fn project_by_name(&self, name: &str) -> Result<Option<NativeId>, SourceError> {
4509 let search = self.board_search(Some(&title_qualifier(name)));
4510 let mut after = None;
4511 loop {
4512 let (candidates, next) = self
4513 .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
4514 .await?;
4515 if let Some(item) = candidates.into_iter().find(|item| {
4516 item.kind == BoardKind::Work(ItemKind::Project)
4517 && item.title.eq_ignore_ascii_case(name)
4518 }) {
4519 return Ok(Some(item.id));
4520 }
4521 match next {
4522 Some(next) => after = Some(next),
4523 None => return Ok(None),
4524 }
4525 }
4526 }
4527
4528 /// Everything filed under one project of this board: the sub-issues of the issue that
4529 /// project is.
4530 ///
4531 /// Tasks *and* documents, because a document filed under a project is a sub-issue of it
4532 /// too — the caller keeps the kind it asked for. Nothing about this grows as the board
4533 /// gains projects, or as another project gains tasks.
4534 ///
4535 /// A qualified id names the issue and is asked for its sub-issues directly: one
4536 /// request, no search of any kind. Only a selector GitHub cannot resolve that way is
4537 /// read as a project *name*, which costs the one bounded search
4538 /// [`Self::project_by_name`] makes.
4539 async fn project_children(&self, selector: &NativeId) -> Result<Vec<Resolved>, SourceError> {
4540 let (project, children) = match self.sub_issues(selector).await? {
4541 Some(children) => (selector.clone(), children),
4542 None => match self.project_by_name(&selector.0).await? {
4543 Some(project) => {
4544 let children = self.sub_issues(&project).await?.unwrap_or_default();
4545 (project, children)
4546 }
4547 None => return Ok(Vec::new()),
4548 },
4549 };
4550 self.completed_with_written(children, |own| own.parent.as_ref() == Some(&project))
4551 }
4552
4553 /// Every issue of this board GitHub's issue search reports updated at or after `since`,
4554 /// completed with what this run wrote — the candidates a comment-activity read confirms.
4555 ///
4556 /// Scoped by the board and by nothing else: `project:<owner>/<number>` reaches every issue
4557 /// on the board whatever repository, and whatever owner, it lives in, so no repository or
4558 /// owner qualifier is added and none is needed. What makes the `updated:` qualifier
4559 /// sufficient is a fact about GitHub rather than about this source: a comment written on an
4560 /// issue **and a comment edited on it** both move that issue's `updatedAt`. The credentialed
4561 /// journey `an_edited_comment_moves_its_issue_and_is_selected_since` in `tests/journey`
4562 /// re-takes that fact on every run of the lane, so a change on GitHub's side fails there
4563 /// rather than silently narrowing a caller's answer.
4564 ///
4565 /// The instant is written to the second, rounded down, which can only widen what the
4566 /// search returns; confirmation against each candidate's own comments is what makes the
4567 /// answer exact. The search is an index that lags a write by a second or two — the module
4568 /// documentation records it — so a caller that asks again from its last instant should
4569 /// overlap the two by more than that.
4570 async fn updated_since(&self, since: DateTime<Utc>) -> Result<Vec<Resolved>, SourceError> {
4571 let found = self.searched(&updated_qualifier(since)).await?;
4572 self.completed_with_written(found, |_| true)
4573 }
4574
4575 /// Every issue of this board GitHub's issue search reports for the board-scoped search
4576 /// narrowed by `also`, in pages of [`SEARCH_PAGE_SIZE`].
4577 ///
4578 /// Uncompleted: what this process wrote is added by the caller, which knows whether its
4579 /// own record is the fresher of the two.
4580 async fn searched(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
4581 let search = self.board_search(Some(also));
4582 let mut after: Option<String> = None;
4583 let mut found = Vec::new();
4584 loop {
4585 let (page, next) = self
4586 .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
4587 .await?;
4588 found.extend(page);
4589 match next {
4590 Some(next) => after = Some(next),
4591 None => return Ok(found),
4592 }
4593 }
4594 }
4595
4596 /// A bounded task answer; the versioned cursor carries the connection position, how
4597 /// many rows of the page starting there were already handed out, and the own-write ids
4598 /// already observed, including across a new source instance.
4599 ///
4600 /// Every page is sent at [`SEARCH_PAGE_SIZE`] whatever the caller's limit, and a limit is
4601 /// sliced from the pages it needs; why is the module documentation's paging contract.
4602 async fn search_tasks(
4603 &self,
4604 query: &TaskQuery,
4605 page: &PageRequest,
4606 also: &str,
4607 ) -> Result<Page<Task>, SourceError> {
4608 let mut position = match &page.cursor {
4609 None => SearchPosition::default(),
4610 Some(cursor) => serde_json::from_str::<SearchPosition>(&cursor.0)
4611 .ok()
4612 .filter(|position| {
4613 position.version == SEARCH_CURSOR_VERSION
4614 && position.connection.valid_resume(position.offset)
4615 })
4616 .ok_or_else(|| SourceError::Config {
4617 message: "page cursor is invalid".into(),
4618 })?,
4619 };
4620 let search = self.board_search(Some(also));
4621 let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
4622 let own = self.with_own_writes(Vec::new())?;
4623 for item in &own {
4624 if !position.own.contains(&item.id) {
4625 position.own.push(item.id.clone());
4626 }
4627 }
4628 let mut tasks = Vec::new();
4629 while !position.connection.exhausted() && tasks.len() < limit {
4630 let first = SEARCH_PAGE_SIZE;
4631 // Page size is part of the key: a short cached answer cannot answer a wider ask.
4632 let key =
4633 serde_json::to_string(&("page", &search, &position.connection.after(), first))
4634 .expect("search page key is serializable");
4635 let cached = if query.commented_since.is_none() {
4636 self.narrowed_cache()?.get(&key).cloned()
4637 } else {
4638 None
4639 };
4640 let (found, next) = match cached {
4641 Some(found) => {
4642 let next = self
4643 .search_next
4644 .lock()
4645 .map_err(|_| SourceError::Unavailable {
4646 message:
4647 "search pagination was left inconsistent; run the command again"
4648 .into(),
4649 })?
4650 .get(&key)
4651 .cloned()
4652 .flatten();
4653 (found, next)
4654 }
4655 None => {
4656 let (found, next) = self
4657 .search_page(&search, first, position.connection.after())
4658 .await?;
4659 if query.commented_since.is_none() {
4660 self.search_next
4661 .lock()
4662 .map_err(|_| SourceError::Unavailable {
4663 message:
4664 "search pagination was left inconsistent; run the command again"
4665 .into(),
4666 })?
4667 .insert(key.clone(), next.clone());
4668 self.narrowed_cache()?.insert(key, found.clone());
4669 }
4670 (found, next)
4671 }
4672 };
4673 let rows = found.len();
4674 for mut item in found.into_iter().skip(position.offset) {
4675 if tasks.len() == limit {
4676 break;
4677 }
4678 position.offset += 1;
4679 if position.own.contains(&item.id) {
4680 if position.seen.contains(&item.id) {
4681 continue;
4682 }
4683 position.seen.push(item.id.clone());
4684 let updated_at = item.updated_at;
4685 let Some(written) = self.search_written(&own, &item.id).await? else {
4686 continue;
4687 };
4688 item = written;
4689 item.updated_at = item.updated_at.max(updated_at);
4690 self.resolved_cache()?.insert(item.id.clone(), item.clone());
4691 }
4692 if item.kind == BoardKind::Work(ItemKind::Task) {
4693 let task = item.task()?;
4694 if task_matches(&task, query, &query.project)
4695 && self.commented_since(&item, query.commented_since).await?
4696 {
4697 tasks.push(task);
4698 }
4699 }
4700 }
4701 if position.offset < rows {
4702 continue;
4703 }
4704 position.offset = 0;
4705 position.connection = match next {
4706 Some(after) => SearchConnection::Continuing {
4707 after: Cursor(after),
4708 },
4709 None => SearchConnection::Exhausted {},
4710 };
4711 }
4712 if position.connection.exhausted() {
4713 for id in position.own.clone() {
4714 if position.seen.contains(&id) {
4715 continue;
4716 }
4717 if tasks.len() == limit {
4718 break;
4719 }
4720 position.seen.push(id.clone());
4721 let Some(item) = self.search_written(&own, &id).await? else {
4722 continue;
4723 };
4724 if item.kind == BoardKind::Work(ItemKind::Task) {
4725 let task = item.task()?;
4726 if task_matches(&task, query, &query.project)
4727 && self.commented_since(&item, query.commented_since).await?
4728 {
4729 tasks.push(task);
4730 }
4731 }
4732 }
4733 }
4734 let more = !position.connection.exhausted()
4735 || position.own.iter().any(|id| !position.seen.contains(id));
4736 Ok(Page {
4737 items: tasks,
4738 next: more.then(|| {
4739 Cursor(serde_json::to_string(&position).expect("search position is serializable"))
4740 }),
4741 })
4742 }
4743
4744 /// A resumed process has the ids but no write records; resolve only a record the
4745 /// current page needs, by its uncached node read rather than the lagging search index.
4746 async fn search_written(
4747 &self,
4748 own: &[Resolved],
4749 id: &NativeId,
4750 ) -> Result<Option<Resolved>, SourceError> {
4751 match own.iter().find(|item| item.id == *id) {
4752 Some(item) => Ok(Some(item.clone())),
4753 None => self.item_by_id(id).await,
4754 }
4755 }
4756
4757 /// The candidates for a task query carrying a text, metadata or origin predicate, read
4758 /// without enumerating the board — or `None` for a query carrying none of the three, which
4759 /// keeps the reads it always had.
4760 ///
4761 /// An origin is answered by [`Self::origin_candidates`], whatever else the query carries,
4762 /// because it names at most a handful of items. Text and metadata are answered by one
4763 /// board-scoped issue search carrying every term — see [`narrowing_qualifiers`] — narrowed
4764 /// further by `updated:>=` when the query also asks for comment activity, since both
4765 /// qualifiers must hold of an issue the answer keeps. Every candidate is confirmed in
4766 /// process afterwards by the same predicates [`task_matches`] applies to every read.
4767 ///
4768 /// Completed with what this process wrote, its own record winning over the index's copy
4769 /// of the same item: see [`Self::with_own_writes`].
4770 async fn narrowed(&self, query: &TaskQuery) -> Result<Option<Vec<Resolved>>, SourceError> {
4771 let asked = match (&query.origin, narrowing_qualifiers(query)) {
4772 (Some(origin), _) => Narrowing::Origin(origin.clone()),
4773 (None, Some(qualifiers)) => Narrowing::Search(match query.commented_since {
4774 Some(since) => format!("{} {qualifiers}", updated_qualifier(since)),
4775 None => qualifiers,
4776 }),
4777 (None, None) => return Ok(None),
4778 };
4779 // A question about comment activity is asked afresh every time, as it always was: it
4780 // is the one a caller polls from one source while waiting for the index, and an
4781 // answer held from the first poll would be the answer to every later one.
4782 let key = query.commented_since.is_none().then(|| asked.key());
4783 let cached = match &key {
4784 Some(key) => self.narrowed_cache()?.get(key).cloned(),
4785 None => None,
4786 };
4787 let found = match cached {
4788 Some(found) => found,
4789 None => {
4790 let found = match &asked {
4791 Narrowing::Origin(origin) => self.origin_candidates(origin).await?,
4792 Narrowing::Search(also) => self.searched(also).await?,
4793 };
4794 if let Some(key) = key {
4795 self.narrowed_cache()?.insert(key, found.clone());
4796 }
4797 found
4798 }
4799 };
4800 self.with_own_writes(found).map(Some)
4801 }
4802
4803 /// The candidates for a project or unscoped document query carrying a searchable text,
4804 /// read without enumerating the board — or `None` for a query with no text or a blank one,
4805 /// which keeps the read it always had.
4806 ///
4807 /// The text is sent as the very phrase a task query's text is — see [`text_qualifiers`] —
4808 /// in one board-scoped issue search walked to its end at [`SEARCH_PAGE_SIZE`], so what it
4809 /// costs is the issues that match and never the board. Its answer is held for the command
4810 /// under the same key [`Self::narrowed`] holds that search under, so a walk of the caller's
4811 /// pages asks GitHub once. Every candidate is confirmed afterwards by its kind and by the
4812 /// substring rule, exactly as an item of the wider read was, and is completed with what this
4813 /// process wrote: see [`Self::with_own_writes`].
4814 async fn text_searched(
4815 &self,
4816 text: Option<&TextQuery>,
4817 ) -> Result<Option<Vec<Resolved>>, SourceError> {
4818 let Some(also) = text_qualifiers(text) else {
4819 return Ok(None);
4820 };
4821 let key = Narrowing::Search(also.clone()).key();
4822 let cached = self.narrowed_cache()?.get(&key).cloned();
4823 let found = match cached {
4824 Some(found) => found,
4825 None => {
4826 let found = self.searched(&also).await?;
4827 self.narrowed_cache()?.insert(key, found.clone());
4828 found
4829 }
4830 };
4831 self.with_own_writes(found).map(Some)
4832 }
4833
4834 /// Every item of this board that may carry `origin` — a superset of those that do — found
4835 /// by [`graphql::ORIGIN_LOOKUP`] and never by enumerating the board.
4836 ///
4837 /// The union of the board's own field filter over the `onetaskgraph.origin` text field —
4838 /// which reads the field every carrier holds, whichever release wrote it — and the
4839 /// board-scoped issue search for the same id as a phrase in the body, where this source
4840 /// mirrors it. The caller adds the third read, this process's own writes. Candidates are
4841 /// returned unconfirmed; [`task_matches`] compares each one's own origin field with the
4842 /// query's, exactly.
4843 ///
4844 /// Both connections are walked to exhaustion, each from its own cursor. One that has
4845 /// already ended is sent its last cursor again, which answers an empty page, so the one
4846 /// document serves every page of either. What the two leave is stated in the module
4847 /// documentation: a carrier another process added within the last second or two, before
4848 /// either index has it.
4849 async fn origin_candidates(&self, origin: &str) -> Result<Vec<Resolved>, SourceError> {
4850 let filter = format!("{ORIGIN_FIELD}:{}", quoted(origin));
4851 let search = self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))));
4852 let mut items_after: Option<String> = None;
4853 let mut search_after: Option<String> = None;
4854 let mut found: Vec<Resolved> = Vec::new();
4855 let keep = |resolved: Resolved, found: &mut Vec<Resolved>| {
4856 if !found.iter().any(|held| held.id == resolved.id) {
4857 found.push(resolved);
4858 }
4859 };
4860 loop {
4861 let data = self
4862 .graphql(
4863 graphql::ORIGIN_LOOKUP,
4864 json!({"owner":self.owner,"number":self.project_number,"filter":filter,
4865 "search":search,"type":"ISSUE","originFirst":ORIGIN_PAGE_SIZE,
4866 "itemsAfter":items_after,"searchAfter":search_after,
4867 "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
4868 "duplicates":true}),
4869 )
4870 .await?;
4871 let items = data
4872 .pointer("/originItems/projectV2/items")
4873 .filter(|value| !value.is_null())
4874 .ok_or_else(|| SourceError::Refused {
4875 message: format!(
4876 "GitHub project {}/{} was not found or is not visible to the token",
4877 self.owner, self.project_number
4878 ),
4879 })?;
4880 for item in optional_nodes(Some(items), "project items")?
4881 .into_iter()
4882 .flatten()
4883 {
4884 // The board's own items list its drafts too, and a draft is not an issue: no
4885 // narrowed read answers with one, whatever its origin field holds.
4886 if let Some(resolved) = self.resolve(item)?
4887 && resolved.content_kind == ContentKind::Issue
4888 {
4889 keep(resolved, &mut found);
4890 }
4891 }
4892 let searched = data.get("search").ok_or_else(|| SourceError::Malformed {
4893 message: "GitHub search response has no search connection".into(),
4894 })?;
4895 for node in optional_nodes(Some(searched), "search")?
4896 .into_iter()
4897 .flatten()
4898 {
4899 if let Some(resolved) = self.resolve_issue(node).await? {
4900 keep(resolved, &mut found);
4901 }
4902 }
4903 let items_next = resumed(items, items_after.as_deref())?;
4904 let search_next = resumed(searched, search_after.as_deref())?;
4905 if !items_next.has_more() && !search_next.has_more() {
4906 return Ok(found);
4907 }
4908 items_after = items_next.cursor();
4909 search_after = search_next.cursor();
4910 }
4911 }
4912
4913 /// `found`, with every item this process created or wrote in its place, and every one of
4914 /// them the read did not report added.
4915 ///
4916 /// This process's own record wins over the read's copy of the same item, because a read
4917 /// of an item written moments ago can still be behind what was written onto it — the
4918 /// origin field included, which is the one a narrowed read is confirmed against — and a
4919 /// read that still names an item under a predicate this process's write moved it out of
4920 /// must not return it. The one thing the read knows that the record cannot is when GitHub
4921 /// last saw the item change, which is what a comment-activity read rules a candidate out
4922 /// by, so the read's `updatedAt` is kept when the record has none of its own. See
4923 /// [`Self::created`] and [`Self::updated`](GitHubProjectsSource::updated).
4924 fn with_own_writes(&self, mut found: Vec<Resolved>) -> Result<Vec<Resolved>, SourceError> {
4925 // A board draft is not an issue, so no narrowed read returns one, and this process
4926 // having written one does not make it an answer either.
4927 let own: Vec<Resolved> = self
4928 .created()?
4929 .iter()
4930 .chain(self.updated()?.iter())
4931 .filter(|own| own.content_kind == ContentKind::Issue)
4932 .cloned()
4933 .collect();
4934 for mut own in own {
4935 self.resolved_cache()?.insert(own.id.clone(), own.clone());
4936 match found.iter_mut().find(|read| read.id == own.id) {
4937 Some(read) => {
4938 own.updated_at = own.updated_at.max(read.updated_at);
4939 *read = own;
4940 }
4941 None => found.push(own),
4942 }
4943 }
4944 Ok(found)
4945 }
4946
4947 /// Whether `item` has a comment created or last edited at or after `since` — always, when
4948 /// there is no instant to hold it to.
4949 ///
4950 /// The candidate's own `updatedAt` is read first, because a comment written or edited at
4951 /// or after the instant moved it there: an issue not updated since holds no such comment,
4952 /// and its comments are never asked for. Otherwise its comments are walked, oldest first,
4953 /// only as far as the first that matches. A board draft is not an issue and has no
4954 /// comments, so it never matches.
4955 async fn commented_since(
4956 &self,
4957 item: &Resolved,
4958 since: Option<DateTime<Utc>>,
4959 ) -> Result<bool, SourceError> {
4960 let Some(since) = since else {
4961 return Ok(true);
4962 };
4963 if item.content_kind == ContentKind::DraftIssue
4964 || item.updated_at.is_some_and(|updated| updated < since)
4965 {
4966 return Ok(false);
4967 }
4968 let query = TaskQuery {
4969 commented_since: Some(since),
4970 ..TaskQuery::default()
4971 };
4972 let mut after: Option<String> = None;
4973 loop {
4974 let data = self
4975 .graphql(
4976 graphql::ISSUE_COMMENTS,
4977 json!({"id":item.id.0,"first":MAX_PAGE_SIZE,"after":after}),
4978 )
4979 .await?;
4980 let Some(connection) = data
4981 .get("node")
4982 .filter(|value| !value.is_null())
4983 .and_then(|node| node.get("comments"))
4984 .filter(|value| !value.is_null())
4985 else {
4986 // Removed since the search reported it: no longer an issue with comments.
4987 return Ok(false);
4988 };
4989 let comments = optional_nodes(Some(connection), "issue comments")?
4990 .into_iter()
4991 .flatten()
4992 .map(comment_from)
4993 .collect::<Result<Vec<_>, _>>()?;
4994 if query.comments_match(&comments) {
4995 return Ok(true);
4996 }
4997 match next_cursor(connection)? {
4998 Some(next) => {
4999 validate_cursor_progress(after.as_deref(), &next.0)?;
5000 after = Some(next.0);
5001 }
5002 None => return Ok(false),
5003 }
5004 }
5005 }
5006
5007 /// Every item on the board: the union of both enumerations GitHub offers of one.
5008 ///
5009 /// Neither contains the other, so neither is dropped — only `ProjectV2.items` lists a
5010 /// board **draft** and reads the board's own fields beside its items, and only the search
5011 /// reports an item that connection is behind on. The module documentation is where the lag and the
5012 /// measurements behind it are written down.
5013 ///
5014 /// A search result is admitted on the same terms as any other issue this source reaches
5015 /// directly — [`Self::resolve_issue`] keeps it only if that issue's own `projectItems`
5016 /// names *this* board — so an issue the index still believes is here after it was taken
5017 /// off is refused rather than reported.
5018 ///
5019 /// See [`Self::board_cache`]. Both completions happen on every call rather than once,
5020 /// which is what the cache could otherwise have broken.
5021 async fn board(&self) -> Result<Board, SourceError> {
5022 let cached = self.board_cache()?.clone();
5023 let mut board = match cached {
5024 Some(board) => board,
5025 None => {
5026 let read = self.read_board().await?;
5027 *self.board_cache()? = Some(read.clone());
5028 read
5029 }
5030 };
5031 for held in self.searched_issues().await? {
5032 if !board.items.iter().any(|item| item.id == held.id) {
5033 board.items.push(held);
5034 }
5035 }
5036 for own in self.created()?.iter() {
5037 if !board.items.iter().any(|item| item.id == own.id) {
5038 board.items.push(own.clone());
5039 }
5040 }
5041 Ok(board)
5042 }
5043
5044 /// This process's own view of the board, or the refusal a poisoned lock is.
5045 fn board_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<Board>>, SourceError> {
5046 self.board_cache
5047 .lock()
5048 .map_err(|_| SourceError::Unavailable {
5049 message: "this source's view of the board was left inconsistent by an earlier \
5050 failure; next: run the command again"
5051 .into(),
5052 })
5053 }
5054
5055 /// Bring this process's own view of the board up to an item it has just written.
5056 ///
5057 /// A created item goes to `created`, which is what completes a board read GitHub's own
5058 /// eventual consistency has left behind. An item that was already there is replaced
5059 /// where it sits, so a second write of it in the same command reads its real parent
5060 /// rather than the one it had before the first write.
5061 ///
5062 /// "Where it sits" is three places, and missing an earlier one leaves a stale record
5063 /// that wins: an item this same run created is held in `created` and not in the cached
5064 /// board, and `board` completes the cached board *from* `created`, so replacing only
5065 /// the cached copy of such an item replaces nothing and the read still reports the
5066 /// title it was created with. The search is the third, and it is the one an item the
5067 /// board's own projection is behind on sits in *alone* — which is exactly the item this
5068 /// source is least able to re-read, so leaving it out would put the stale title back on
5069 /// the only items the completion in [`Self::board`] exists for.
5070 fn remember_written(&self, item: Resolved, created: bool) -> Result<(), SourceError> {
5071 self.resolved_cache()?.insert(item.id.clone(), item.clone());
5072 if created {
5073 self.created()?.push(item);
5074 return Ok(());
5075 }
5076 {
5077 let mut own = self.created()?;
5078 if let Some(held) = own.iter_mut().find(|held| held.id == item.id) {
5079 *held = item;
5080 return Ok(());
5081 }
5082 }
5083 {
5084 let mut own = self.updated()?;
5085 match own.iter_mut().find(|held| held.id == item.id) {
5086 Some(held) => *held = item.clone(),
5087 None => own.push(item.clone()),
5088 }
5089 }
5090 if let Some(board) = self.board_cache()?.as_mut()
5091 && let Some(held) = board.items.iter_mut().find(|held| held.id == item.id)
5092 {
5093 *held = item.clone();
5094 }
5095 if let Some(found) = self.search_cache()?.as_mut()
5096 && let Some(held) = found.iter_mut().find(|held| held.id == item.id)
5097 {
5098 *held = item.clone();
5099 }
5100 for found in self.narrowed_cache()?.values_mut() {
5101 if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
5102 *held = item.clone();
5103 }
5104 }
5105 Ok(())
5106 }
5107
5108 /// Forget one item this process has just deleted, from every half of its own view.
5109 fn forget(&self, id: &NativeId) -> Result<(), SourceError> {
5110 self.resolved_cache()?.remove(id);
5111 self.created()?.retain(|own| own.id != *id);
5112 self.updated()?.retain(|own| own.id != *id);
5113 if let Some(board) = self.board_cache()?.as_mut() {
5114 board.items.retain(|item| item.id != *id);
5115 }
5116 if let Some(found) = self.search_cache()?.as_mut() {
5117 found.retain(|item| item.id != *id);
5118 }
5119 for found in self.narrowed_cache()?.values_mut() {
5120 found.retain(|item| item.id != *id);
5121 }
5122 Ok(())
5123 }
5124
5125 /// This process's own record of each narrowed answer, or the refusal a poisoned lock is.
5126 fn narrowed_cache(
5127 &self,
5128 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<String, Vec<Resolved>>>, SourceError> {
5129 self.narrowed_cache
5130 .lock()
5131 .map_err(|_| SourceError::Unavailable {
5132 message: "this source's view of a narrowed read was left inconsistent by an \
5133 earlier failure; next: run the command again"
5134 .into(),
5135 })
5136 }
5137
5138 /// Every page of the board, read from GitHub.
5139 async fn read_board(&self) -> Result<Board, SourceError> {
5140 let mut after: Option<String> = None;
5141 let mut items = Vec::new();
5142 let mut board;
5143 loop {
5144 let page = self.board_page(after.as_deref(), MAX_PAGE_SIZE).await?;
5145 for item in page
5146 .pointer("/items/nodes")
5147 .and_then(Value::as_array)
5148 .ok_or_else(|| SourceError::Malformed {
5149 message: "GitHub project items.nodes is not an array".into(),
5150 })?
5151 {
5152 if let Some(resolved) = self.resolve(item)? {
5153 items.push(resolved);
5154 }
5155 }
5156 let info = page
5157 .pointer("/items/pageInfo")
5158 .ok_or_else(|| SourceError::Malformed {
5159 message: "GitHub project items have no pageInfo".into(),
5160 })?;
5161 let has_next = required_bool(info, "hasNextPage")?;
5162 let next = has_next
5163 .then(|| required_str(info, "endCursor"))
5164 .transpose()?;
5165 board = page.clone();
5166 match next {
5167 Some(next) => {
5168 validate_cursor_progress(after.as_deref(), next)?;
5169 after = Some(next.to_owned());
5170 }
5171 None => break,
5172 }
5173 }
5174 Ok(Board {
5175 id: required_str(&board, "id")?.to_owned(),
5176 fields: board.get("fields").cloned().unwrap_or(Value::Null),
5177 items,
5178 })
5179 }
5180
5181 /// The existing items this source has written, for completing a narrowed read that is
5182 /// behind; see [`Self::updated`](GitHubProjectsSource::updated).
5183 fn updated(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
5184 self.updated.lock().map_err(|_| SourceError::Unavailable {
5185 message: "this source's record of what it wrote in this run was left inconsistent \
5186 by an earlier failure; next: run the command again"
5187 .into(),
5188 })
5189 }
5190
5191 /// The items this source has created, for completing a board read that is behind.
5192 fn created(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
5193 self.created.lock().map_err(|_| SourceError::Unavailable {
5194 message: "this source's record of what it created in this run was left \
5195 inconsistent by an earlier failure; next: run the command again"
5196 .into(),
5197 })
5198 }
5199
5200 /// One board item as this source reports it, or `None` for content it ignores.
5201 ///
5202 /// A pull request is neither a project nor a task — it is somebody's change, not a
5203 /// unit of plan — and an item whose content the token cannot see has nothing to
5204 /// report at all.
5205 fn resolve(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
5206 let content = item.get("content").ok_or_else(|| SourceError::Malformed {
5207 message: "GitHub project item is missing content".into(),
5208 })?;
5209 if content.is_null() {
5210 return Ok(None);
5211 }
5212 let content_kind = match required_str(content, "__typename")? {
5213 "Issue" => ContentKind::Issue,
5214 "DraftIssue" => ContentKind::DraftIssue,
5215 _ => return Ok(None),
5216 };
5217 let field_values = item
5218 .get("fieldValues")
5219 .ok_or_else(|| SourceError::Malformed {
5220 message: "GitHub project item is missing fieldValues".into(),
5221 })?;
5222 complete_connection(field_values, "project item field values", NESTED_PAGE_SIZE)?;
5223 let nodes = field_values
5224 .get("nodes")
5225 .and_then(Value::as_array)
5226 .ok_or_else(|| SourceError::Malformed {
5227 message: "GitHub project item fieldValues.nodes is not an array".into(),
5228 })?;
5229 if let Some(labels) = content.get("labels") {
5230 complete_connection(labels, "content labels", NESTED_PAGE_SIZE)?;
5231 }
5232 let raw_body = optional_str(content, "body")?.map(str::to_owned);
5233 let (body, slot) = metadata_body(raw_body.clone())?;
5234 let parent = optional_str(content.get("parent").unwrap_or(&Value::Null), "id")?
5235 .map(|id| NativeId(id.to_owned()));
5236 // A draft has no sub-issues to summarise, and GitHub's schema gives it no field
5237 // to read one from; it is a task, and never a project.
5238 let sub_issues = match content_kind {
5239 ContentKind::Issue => sub_issue_total(content)?,
5240 ContentKind::DraftIssue => 0,
5241 };
5242 let content_id = required_str(content, "id")?;
5243 let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
5244 message: format!("GitHub issue {content_id}: {message}"),
5245 })?;
5246 let raw_title = required_str(content, "title")?;
5247 // The design prefix is read *first*, before either of the two rules that separate
5248 // a project from a task. A document is not work whatever sub-issues it has and
5249 // whatever marker it carries, and reading the prefix later would make a design
5250 // issue with none of either an empty project.
5251 let kind = if raw_title.starts_with(DESIGN_TITLE_PREFIX) {
5252 BoardKind::Document
5253 } else if parent.is_some() {
5254 // Being a sub-issue wins outright, and no marker overrides it: an issue filed
5255 // under a project is that project's task even when it has sub-issues of its
5256 // own.
5257 BoardKind::Work(ItemKind::Task)
5258 } else if sub_issues > 0 || marked == Some(ItemKind::Project) {
5259 BoardKind::Work(ItemKind::Project)
5260 } else {
5261 BoardKind::Work(ItemKind::Task)
5262 };
5263 // The title a person wrote, which for a document is the one without the prefix —
5264 // the same way `content` above is the body without this source's metadata slot.
5265 let title = match kind {
5266 BoardKind::Document => raw_title[DESIGN_TITLE_PREFIX.len()..].to_owned(),
5267 BoardKind::Work(_) => raw_title.to_owned(),
5268 };
5269 let own_repository = content
5270 .pointer("/repository/nameWithOwner")
5271 .and_then(Value::as_str)
5272 .map(|origin| Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST)))
5273 .transpose()
5274 .map_err(|message| SourceError::Malformed { message })?;
5275 let repositories = if slot.contains_key(Repository::METADATA_KEY) {
5276 Repository::from_metadata(&slot)
5277 .map_err(|message| SourceError::Malformed { message })?
5278 } else {
5279 own_repository.clone().into_iter().collect()
5280 };
5281 let id = NativeId(content_id.to_owned());
5282 // Read only for a task, because only a task has either list: a project or a
5283 // document holding one of these keys holds nothing this source reports, and the
5284 // keys are left out of its caller-visible metadata all the same.
5285 let (delivers, delivered_by) = if kind == BoardKind::Work(ItemKind::Task) {
5286 let listed = |key: &str| {
5287 TaskRef::from_value(key, &id, Some(&self.name), slot.get(key))
5288 .map_err(|message| SourceError::Malformed { message })
5289 };
5290 (
5291 listed(TaskRef::DELIVERS_KEY)?,
5292 listed(TaskRef::DELIVERED_BY_KEY)?,
5293 )
5294 } else {
5295 (Vec::new(), Vec::new())
5296 };
5297 let (option, closed, reason) = Self::status_parts(nodes, content)?;
5298 let priority = self.held_priority(nodes)?;
5299 // Present when the item was reached through its own issue, whose board entry
5300 // names the board; a read of the board's own items has the board already. An
5301 // empty id names nothing a field write could address, so it is read as absent and
5302 // the write goes back to reading the board.
5303 let board_id = item
5304 .pointer("/project/id")
5305 .and_then(Value::as_str)
5306 .filter(|id| !id.is_empty());
5307 let resolved = Resolved {
5308 item_id: required_str(item, "id")?.to_owned(),
5309 id,
5310 content_kind,
5311 kind,
5312 title,
5313 body: body.filter(|value| !value.is_empty()),
5314 raw_body,
5315 status: self
5316 .statuses
5317 .status(kind.status_kind(), option, closed, reason),
5318 option: option.map(str::to_owned),
5319 priority,
5320 closed,
5321 delivers,
5322 delivered_by,
5323 labels: labels(content)?,
5324 parent,
5325 origin: text_field(nodes, ORIGIN_FIELD)?.filter(|value| !value.is_empty()),
5326 number: match content_kind {
5327 ContentKind::Issue => Some(issue_number(content)?),
5328 // A draft is filed in no repository, so nothing ever numbered it:
5329 // `DraftIssue` declares no `number` at all, exactly as it declares no
5330 // `subIssuesSummary` the branch above reads.
5331 ContentKind::DraftIssue => None,
5332 },
5333 url: optional_str(content, "url")?.map(str::to_owned),
5334 created_at: optional_time(content, "createdAt")?,
5335 updated_at: optional_time(content, "updatedAt")?,
5336 own_repository,
5337 repositories,
5338 slot,
5339 board_id: board_id.map(str::to_owned),
5340 fields: field_definitions(nodes),
5341 board_fields: Self::carried_board_fields(content, board_id)?,
5342 blocked_by: carried_blocked_by(content)?,
5343 };
5344 self.resolved_cache()?
5345 .insert(resolved.id.clone(), resolved.clone());
5346 Ok(Some(resolved))
5347 }
5348
5349 /// The field definitions of the board `board_id` names — the project this issue's own
5350 /// board item is on — off the `boards` page a read of an issue by its own id carries, or
5351 /// `None` when the read carried none, carried no entry for that board, or the board item
5352 /// named no board, which a write then answers by reading the board's fields itself.
5353 ///
5354 /// Matched by the board's node id and never by its number alone: a project number is
5355 /// unique only within its owner, so another owner's board numbered alike can sit on the
5356 /// same page, and its field and option ids address nothing on this one.
5357 fn carried_board_fields(
5358 content: &Value,
5359 board_id: Option<&str>,
5360 ) -> Result<Option<Value>, SourceError> {
5361 let (Some(nodes), Some(board_id)) = (
5362 content.pointer("/boards/nodes").and_then(Value::as_array),
5363 board_id,
5364 ) else {
5365 return Ok(None);
5366 };
5367 let Some(board) = nodes.iter().find_map(|node| {
5368 let project = node.get("project")?;
5369 (project.get("id").and_then(Value::as_str) == Some(board_id)).then_some(project)
5370 }) else {
5371 return Ok(None);
5372 };
5373 let Some(fields) = board.get("fields").filter(|fields| !fields.is_null()) else {
5374 return Ok(None);
5375 };
5376 complete_connection(fields, "board fields", NESTED_PAGE_SIZE)?;
5377 Ok(Some(fields.clone()))
5378 }
5379
5380 /// What one board item's `Priority` field says, through this instance's mapping.
5381 ///
5382 /// An instance with no mapping holds no priority, so every item reads as `none` whatever
5383 /// its board holds. With one, no value is `none`, a mapped option is its level, and an
5384 /// option the mapping does not name is kept as itself — never read as a level or as
5385 /// `none` — for a read of the task to report by name.
5386 fn held_priority(&self, field_values: &[Value]) -> Result<HeldPriority, SourceError> {
5387 let Some(mapping) = &self.priorities else {
5388 return Ok(HeldPriority::Read(Priority::None));
5389 };
5390 // A value of the field that names no option — a text field someone called `Priority` —
5391 // is malformed rather than `none`: reading it as no priority would let the next copy
5392 // clear one a person set.
5393 let Some(option) = field_values
5394 .iter()
5395 .find(|value| {
5396 value.pointer("/field/name").and_then(Value::as_str) == Some(PRIORITY_FIELD)
5397 })
5398 .map(|value| required_str(value, "name"))
5399 .transpose()?
5400 else {
5401 return Ok(HeldPriority::Read(Priority::None));
5402 };
5403 Ok(mapping.priority_of(option).map_or_else(
5404 || HeldPriority::Unmapped(option.to_owned()),
5405 HeldPriority::Read,
5406 ))
5407 }
5408
5409 /// What one board item's status is read from: its `Status` option, whether its issue
5410 /// is closed, and the reason it was closed with. [`BoardStatuses::status`] turns the
5411 /// three into the status it reports.
5412 fn status_parts<'a>(
5413 field_values: &'a [Value],
5414 content: &'a Value,
5415 ) -> Result<(Option<&'a str>, bool, Option<&'a str>), SourceError> {
5416 let option = field_values
5417 .iter()
5418 .find(|value| value.pointer("/field/name").and_then(Value::as_str) == Some("Status"))
5419 .map(|value| required_str(value, "name"))
5420 .transpose()?;
5421 let closed = optional_str(content, "state")? == Some("CLOSED");
5422 Ok((option, closed, optional_str(content, "stateReason")?))
5423 }
5424
5425 /// The board Status option this write selects, or the refusal that says why not.
5426 ///
5427 /// The mapped option is required for both open and terminal targets. A terminal write
5428 /// validates it before changing either representation, so it can never fall back to
5429 /// closing an issue whose board cannot display the matching status.
5430 ///
5431 /// Answers the field's id, the option's id, and the option's name as the board spells
5432 /// it — which is the name a read of the item reports once it sits there.
5433 fn column_for(
5434 &self,
5435 fields: &Value,
5436 kind: ItemKind,
5437 category: StatusCategory,
5438 target: &StatusTarget,
5439 ) -> Result<Option<(String, String, String)>, SourceError> {
5440 let Some(wanted) = target.option() else {
5441 return Ok(None);
5442 };
5443 let missing = |detail: &str| SourceError::Refused {
5444 message: format!(
5445 "{} status {} of source {} needs the board Status option {wanted:?}, and \
5446 {detail}; next: add that option to the board, which `onetaskgraph sources \
5447 fields {} --apply` does, or point status_mapping.{}.{} of this source at one \
5448 it has",
5449 kind.marker(),
5450 category_name(category),
5451 self.name,
5452 self.name,
5453 category_name(category),
5454 kind.marker()
5455 ),
5456 };
5457 let Some(field) = Board::field(fields, "Status")? else {
5458 return Err(missing("this board has no Status field"));
5459 };
5460 if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
5461 return Err(missing(
5462 "this board's Status field is not a single-select field",
5463 ));
5464 }
5465 let option = field
5466 .get("options")
5467 .and_then(Value::as_array)
5468 .and_then(|options| {
5469 options.iter().find(|option| {
5470 option
5471 .get("name")
5472 .and_then(Value::as_str)
5473 .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
5474 })
5475 });
5476 match option {
5477 None => Err(missing("this board does not have it")),
5478 Some(option) => Ok(Some((
5479 required_str(field, "id")?.to_owned(),
5480 required_str(option, "id")?.to_owned(),
5481 required_str(option, "name")?.to_owned(),
5482 ))),
5483 }
5484 }
5485
5486 /// The refusal a status that closes an issue is answered with over a board draft.
5487 fn closes_a_draft(&self, category: StatusCategory) -> SourceError {
5488 SourceError::Refused {
5489 message: format!(
5490 "status {} of source {} closes the item's issue, and GitHub draft items have \
5491 no open or closed state",
5492 category_name(category),
5493 self.name
5494 ),
5495 }
5496 }
5497
5498 /// What a status write to one item needs of the board: the board's id and the
5499 /// definition of its `Status` field, read off the item when the item says both.
5500 ///
5501 /// The same reasoning as [`Self::fields_for`]: a node read of the item names its board,
5502 /// and its `Status` value carries that field's definition, options and all. An item that
5503 /// does not say — no board id, or no `Status` value to read the field off — takes them
5504 /// from [`Self::board_fields`], which reads no item.
5505 async fn status_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
5506 if let Some(board) = item.carried_board() {
5507 return Ok(board);
5508 }
5509 if item.defines("Status")
5510 && let Some(board_id) = item.named_board()
5511 {
5512 return Ok(BoardFields {
5513 id: board_id,
5514 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
5515 });
5516 }
5517 self.board_fields().await
5518 }
5519
5520 /// Set one task's status and nothing else; see [`TaskSource::set_task_status`].
5521 async fn set_status(
5522 &self,
5523 id: &NativeId,
5524 category: StatusCategory,
5525 ) -> Result<Option<Status>, SourceError> {
5526 // Refused before anything is read, in the words a write of the same status is.
5527 let target = self.resolved_target(ItemKind::Task, category)?;
5528 let Some(mut item) = self
5529 .bound_item(id)
5530 .await?
5531 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
5532 else {
5533 return Ok(None);
5534 };
5535 let board = self.status_board(&item).await?;
5536 let (field, option, name) = self
5537 .column_for(&board.fields, ItemKind::Task, category, &target)?
5538 .ok_or_else(|| SourceError::Malformed {
5539 message: format!(
5540 "status {} of source {} names no board Status option",
5541 category_name(category),
5542 self.name
5543 ),
5544 })?;
5545 if item.status.category == category && item.option.as_deref() == Some(&name) {
5546 return Ok(Some(item.status));
5547 }
5548 match &target {
5549 StatusTarget::Terminal(_, reason) => {
5550 if item.content_kind == ContentKind::DraftIssue {
5551 return Err(self.closes_a_draft(category));
5552 }
5553 self.set_item_field(
5554 board.id.as_str(),
5555 &item.item_id,
5556 &field,
5557 json!({"singleSelectOptionId": option}),
5558 )
5559 .await?;
5560 self.update_content(
5561 ContentKind::Issue,
5562 &item.id,
5563 json!({"stateInput": state_input(Some(&target))}),
5564 )
5565 .await?;
5566 item.closed = true;
5567 item.status =
5568 self.statuses
5569 .status(ItemKind::Task, Some(&name), true, Some(reason.reason()));
5570 item.option = Some(name);
5571 }
5572 StatusTarget::Column(_) => {
5573 // An option is what an open item's status is, so a closed issue is reopened
5574 // first — sitting closed in the column, it would read back as closed. A draft has
5575 // no state to reopen.
5576 if item.content_kind == ContentKind::Issue && item.closed {
5577 self.update_content(
5578 ContentKind::Issue,
5579 &item.id,
5580 json!({"stateInput": state_input(Some(&target))}),
5581 )
5582 .await?;
5583 item.closed = false;
5584 }
5585 self.set_item_field(
5586 board.id.as_str(),
5587 &item.item_id,
5588 &field,
5589 json!({"singleSelectOptionId": option}),
5590 )
5591 .await?;
5592 item.status = self
5593 .statuses
5594 .status(ItemKind::Task, Some(&name), false, None);
5595 item.option = Some(name);
5596 }
5597 StatusTarget::Disabled(_) => {
5598 unreachable!("resolved_target refused a disabled status")
5599 }
5600 }
5601 let status = item.status.clone();
5602 self.remember_written(item, false)?;
5603 Ok(Some(status))
5604 }
5605
5606 /// Replace one task's `delivered_by` and nothing else; see
5607 /// [`TaskSource::set_delivered_by`].
5608 ///
5609 /// One update of the body, which differs from the body GitHub holds only inside the
5610 /// metadata slot — see [`with_slot`]. A body that would not change is not sent at all.
5611 async fn replace_delivered_by(
5612 &self,
5613 id: &NativeId,
5614 delivered_by: &[TaskRef],
5615 ) -> Result<Option<()>, SourceError> {
5616 let entries = TaskRef::listed(
5617 TaskRef::DELIVERED_BY_KEY,
5618 id,
5619 Some(&self.name),
5620 delivered_by.to_vec(),
5621 )
5622 .map_err(|message| SourceError::Refused { message })?;
5623 let Some(mut item) = self
5624 .bound_item(id)
5625 .await?
5626 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
5627 else {
5628 return Ok(None);
5629 };
5630 let mut slot = item.slot.clone();
5631 set_task_list(&mut slot, TaskRef::DELIVERED_BY_KEY, &entries);
5632 self.write_slot(&mut item, &slot).await?;
5633 item.delivered_by = entries;
5634 self.remember_written(item, false)?;
5635 Ok(Some(()))
5636 }
5637
5638 /// Set one caller key of the metadata slot of one issue of `kind`, and nothing else;
5639 /// see [`TaskSource::set_task_metadata`].
5640 ///
5641 /// `None` when this board holds no item by that id, or holds one of another kind. The
5642 /// answer is the item as this source now reads it, so what a caller is told the key
5643 /// holds is what the slot holds.
5644 ///
5645 /// A key already holding the value is answered without a write, compared as JSON rather
5646 /// than as the body's bytes: a slot a person spelled with other whitespace would
5647 /// otherwise be re-encoded, which is a write that changes nothing the caller asked for.
5648 async fn set_slot_key(
5649 &self,
5650 id: &NativeId,
5651 kind: BoardKind,
5652 key: &MetadataKey,
5653 value: &Value,
5654 ) -> Result<Option<Resolved>, SourceError> {
5655 let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
5656 return Ok(None);
5657 };
5658 if item.slot.get(key.as_str()) == Some(value) {
5659 return Ok(Some(item));
5660 }
5661 let mut slot = item.slot.clone();
5662 slot.insert(key.as_str().to_owned(), value.clone());
5663 self.write_slot(&mut item, &slot).await?;
5664 self.remember_written(item.clone(), false)?;
5665 Ok(Some(item))
5666 }
5667
5668 /// Put `slot` in one item's metadata slot with a single update of its body, and bring
5669 /// `item` up to what that write left.
5670 ///
5671 /// The body sent differs from the body GitHub holds only inside the slot — see
5672 /// [`with_slot`] — and a body that would not change is not sent at all. It goes through
5673 /// the mutation the item's content takes, so a board draft's body is written with
5674 /// `updateProjectV2DraftIssue` exactly as an issue's is with `updateIssue`.
5675 async fn write_slot(
5676 &self,
5677 item: &mut Resolved,
5678 slot: &BTreeMap<String, Value>,
5679 ) -> Result<(), SourceError> {
5680 let held = item.raw_body.clone().unwrap_or_default();
5681 let body = with_slot(&held, slot)?;
5682 if body != held {
5683 self.update_content(item.content_kind, &item.id, json!({"body": body}))
5684 .await?;
5685 }
5686 let (visible, slot) = metadata_body(Some(body.clone()))?;
5687 item.body = visible.filter(|value| !value.is_empty());
5688 item.raw_body = Some(body);
5689 item.slot = slot;
5690 Ok(())
5691 }
5692
5693 /// This instance's target for a category written to an item of `kind`, refusing one
5694 /// that kind has no option for — before anything is read or written.
5695 ///
5696 /// Nothing here mutates the board's option set to make room for a status. GitHub
5697 /// documents `UpdateProjectV2FieldInput.singleSelectOptions` as *"provided values
5698 /// overwrite existing options"*, so no addition is additive and a mistake destroys the
5699 /// field and every item's status.
5700 fn resolved_target(
5701 &self,
5702 kind: ItemKind,
5703 category: StatusCategory,
5704 ) -> Result<StatusTarget, SourceError> {
5705 let target = self.statuses.target(kind, category).clone();
5706 let StatusTarget::Disabled(why) = target else {
5707 return Ok(target);
5708 };
5709 let refusal = why.refusal(&self.name, category, kind);
5710 // Why there is no shipped default, which is the question a person meeting this
5711 // refusal on a source that never mentioned the category asks.
5712 let shipped_none = match category {
5713 StatusCategory::Draft => Some(
5714 "draft has no shipped default because GitHub draft issues cannot have \
5715 sub-issues, and this source stores a project's tasks as its issue's sub-issues",
5716 ),
5717 StatusCategory::Unknown => Some(
5718 "unknown has no shipped default because this board keeps no open-ended status \
5719 word: every word classified unknown is written to the one board Status option \
5720 status_mapping.unknown names",
5721 ),
5722 _ => None,
5723 };
5724 Err(match (refusal, shipped_none, why) {
5725 (SourceError::Refused { message }, Some(note), UnmappedStatus::Unconfigured) => {
5726 SourceError::Refused {
5727 message: format!("{message}; {note}"),
5728 }
5729 }
5730 (refusal, _, _) => refusal,
5731 })
5732 }
5733
5734 /// What writing `priority` does to one item's `Priority` field on this board, or the
5735 /// refusal naming what the board lacks.
5736 ///
5737 /// `none` is no value, so it clears the field — and asks nothing of an item that holds
5738 /// none already, or of an item not created yet. Every other priority selects the option
5739 /// the mapping names, matched case-insensitively; a board with no `Priority` field, or
5740 /// without that option, is refused rather than given one: reads and writes never create
5741 /// a field or an option.
5742 fn priority_write(
5743 &self,
5744 fields: &Value,
5745 existing: Option<&Resolved>,
5746 priority: Priority,
5747 ) -> Result<Option<PriorityWrite>, SourceError> {
5748 let Some(mapping) = &self.priorities else {
5749 return Err(self.holds_no_priority());
5750 };
5751 let Some(wanted) = mapping.option(priority) else {
5752 if !existing.is_some_and(Resolved::holds_priority) {
5753 return Ok(None);
5754 }
5755 let field =
5756 Board::field(fields, PRIORITY_FIELD)?.ok_or_else(|| SourceError::Malformed {
5757 message: format!(
5758 "an item holding a {PRIORITY_FIELD} value was read without that field"
5759 ),
5760 })?;
5761 return Ok(Some(PriorityWrite::Clear {
5762 field: required_str(field, "id")?.to_owned(),
5763 }));
5764 };
5765 let missing = |detail: &str| SourceError::Refused {
5766 message: format!(
5767 "priority {priority} of source {} needs the board {PRIORITY_FIELD} option \
5768 {wanted:?}, and {detail}; run `onetaskgraph sources fields {} --apply` to add \
5769 it, or point priority_mapping.{priority} of this source at an option the board \
5770 has",
5771 self.name, self.name
5772 ),
5773 };
5774 let Some(field) = Board::field(fields, PRIORITY_FIELD)? else {
5775 return Err(missing(&format!(
5776 "this board has no {PRIORITY_FIELD} field"
5777 )));
5778 };
5779 if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
5780 return Err(missing(&format!(
5781 "this board's {PRIORITY_FIELD} field is not a single-select field"
5782 )));
5783 }
5784 // An options list that is absent or not a list is an answer this source cannot read,
5785 // not a board lacking the option: `sources fields --apply` is no remedy for it.
5786 let option = field
5787 .get("options")
5788 .and_then(Value::as_array)
5789 .ok_or_else(|| SourceError::Malformed {
5790 message: format!("GitHub {PRIORITY_FIELD} field options is not an array"),
5791 })?
5792 .iter()
5793 .find(|option| {
5794 option
5795 .get("name")
5796 .and_then(Value::as_str)
5797 .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
5798 })
5799 .ok_or_else(|| missing("this board does not have it"))?;
5800 Ok(Some(PriorityWrite::Select {
5801 field: required_str(field, "id")?.to_owned(),
5802 option: required_str(option, "id")?.to_owned(),
5803 }))
5804 }
5805
5806 /// Apply one priority write to one board item.
5807 async fn write_priority(
5808 &self,
5809 board_id: &str,
5810 item_id: &str,
5811 write: &PriorityWrite,
5812 ) -> Result<(), SourceError> {
5813 match write {
5814 PriorityWrite::Select { field, option } => {
5815 self.set_item_field(
5816 board_id,
5817 item_id,
5818 field,
5819 json!({"singleSelectOptionId": option}),
5820 )
5821 .await
5822 }
5823 PriorityWrite::Clear { field } => {
5824 let data = self
5825 .graphql(
5826 graphql::CLEAR_FIELD,
5827 json!({"input":{"projectId":board_id,"itemId":item_id,"fieldId":field},
5828 "readPriority":false,"priorityName":PRIORITY_FIELD}),
5829 )
5830 .await?;
5831 let returned = data
5832 .pointer("/clearProjectV2ItemFieldValue/projectV2Item")
5833 .ok_or_else(|| SourceError::Malformed {
5834 message: "GitHub field clear returned no project item".into(),
5835 })?;
5836 if required_str(returned, "id")? != item_id {
5837 return Err(SourceError::Malformed {
5838 message: "GitHub field clear returned the wrong project item".into(),
5839 });
5840 }
5841 Ok(())
5842 }
5843 }
5844 }
5845
5846 /// The refusal a priority is answered with by an instance configured with no
5847 /// `priority_mapping`, which holds none.
5848 fn holds_no_priority(&self) -> SourceError {
5849 SourceError::Refused {
5850 message: format!(
5851 "source {} holds no task priority: its configuration sets no priority_mapping; \
5852 next: set priority_mapping on this source, then run `onetaskgraph sources \
5853 fields {} --apply` to set its board up",
5854 self.name, self.name
5855 ),
5856 }
5857 }
5858
5859 /// Set one task's priority and nothing else; see [`TaskSource::set_task_priority`].
5860 ///
5861 /// One field write — a select, or a clear for `none` — and no title, body, label, state
5862 /// or `Status` request. Clearing a priority an item does not hold sends nothing.
5863 async fn set_priority(
5864 &self,
5865 id: &NativeId,
5866 priority: Priority,
5867 ) -> Result<Option<Priority>, SourceError> {
5868 if self.priorities.is_none() {
5869 return Err(self.holds_no_priority());
5870 }
5871 let Some(mut item) = self
5872 .bound_item(id)
5873 .await?
5874 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
5875 else {
5876 return Ok(None);
5877 };
5878 if priority == Priority::None && !item.holds_priority() {
5879 return Ok(Some(priority));
5880 }
5881 // The item's own read carries the field's definition whenever it holds a value of
5882 // it, which a clear always does; a select onto an item holding none reads the board.
5883 let board = match (item.carried_board(), item.named_board()) {
5884 (Some(board), _) => board,
5885 (None, Some(id)) if item.defines(PRIORITY_FIELD) => BoardFields {
5886 id,
5887 fields: json!({"nodes": item.fields.clone(), "pageInfo": {"hasNextPage": false}}),
5888 },
5889 _ => self.board_fields().await?,
5890 };
5891 let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? else {
5892 return Ok(Some(priority));
5893 };
5894 let (document, root, input) = match write {
5895 PriorityWrite::Select { field, option } => (
5896 graphql::UPDATE_FIELD,
5897 "updateProjectV2ItemFieldValue",
5898 json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field,"value":{"singleSelectOptionId":option}}),
5899 ),
5900 PriorityWrite::Clear { field } => (
5901 graphql::CLEAR_FIELD,
5902 "clearProjectV2ItemFieldValue",
5903 json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field}),
5904 ),
5905 };
5906 let data = self
5907 .graphql(
5908 document,
5909 json!({"input":input,"readPriority":true,"priorityName":PRIORITY_FIELD}),
5910 )
5911 .await?;
5912 let returned = data
5913 .get(root)
5914 .and_then(|value| value.get("projectV2Item"))
5915 .ok_or_else(|| SourceError::Malformed {
5916 message: "GitHub priority write returned no project item".into(),
5917 })?;
5918 if required_str(returned, "id")? != item.item_id {
5919 return Err(SourceError::Malformed {
5920 message: "GitHub priority write returned the wrong project item".into(),
5921 });
5922 }
5923 let value = returned
5924 .get("fieldValueByName")
5925 .ok_or_else(|| SourceError::Malformed {
5926 message: "GitHub priority write returned no priority read-back".into(),
5927 })?;
5928 if !value.is_null()
5929 && value.pointer("/field/name").and_then(Value::as_str) != Some(PRIORITY_FIELD)
5930 {
5931 return Err(SourceError::Malformed {
5932 message: "GitHub priority read-back is not a Priority field value".into(),
5933 });
5934 }
5935 let values = if value.is_null() {
5936 Vec::new()
5937 } else {
5938 vec![value.clone()]
5939 };
5940 item.priority = self.held_priority(&values)?;
5941 let answer = item.task()?.priority;
5942 self.remember_written(item, false)?;
5943 Ok(Some(answer))
5944 }
5945
5946 /// Replace one task's visible body and nothing else; see
5947 /// [`TaskSource::set_task_content`].
5948 ///
5949 /// One update of the body, which differs from the body GitHub holds only outside the
5950 /// metadata slot — the slot is kept byte for byte, so every caller key and every list
5951 /// this source keeps there reads back as it was. A body that would not change is not
5952 /// sent at all.
5953 async fn replace_content(
5954 &self,
5955 id: &NativeId,
5956 content: &str,
5957 ) -> Result<Option<()>, SourceError> {
5958 let Some(mut item) = self
5959 .bound_item(id)
5960 .await?
5961 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
5962 else {
5963 return Ok(None);
5964 };
5965 let held = item.raw_body.clone().unwrap_or_default();
5966 let body = with_content(&held, content)?;
5967 // Checked before anything is sent: content ending in what this source reads as its own
5968 // metadata slot would read back as metadata rather than as the content it was.
5969 let (visible, slot) = metadata_body(Some(body.clone()))?;
5970 if visible.as_deref().unwrap_or_default() != content || slot != item.slot {
5971 return Err(SourceError::Refused {
5972 message: format!(
5973 "this content ends in what source {} reads as its own metadata slot \
5974 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
5975 as content; next: remove that trailing block from the content",
5976 self.name
5977 ),
5978 });
5979 }
5980 if body != held {
5981 self.update_content(item.content_kind, &item.id, json!({"body": body}))
5982 .await?;
5983 }
5984 item.body = visible.filter(|value| !value.is_empty());
5985 item.raw_body = Some(body);
5986 item.slot = slot;
5987 self.remember_written(item, false)?;
5988 Ok(Some(()))
5989 }
5990
5991 /// Apply one targeted update to one task; see [`TaskSource::update_task`].
5992 ///
5993 /// One read of the item — which carries the board's field definitions and the issue's
5994 /// `blockedBy`, so neither is read again — and then only what differs from it: the
5995 /// `Status` option and the `Priority` field together in one request, the `blockedBy`
5996 /// additions and removals the named edges differ by, and last one `updateIssue` carrying
5997 /// the title, the body — visible content and metadata slot together — and a state change.
5998 /// So an update naming any of title, body, metadata, status and priority is one read and
5999 /// at most two writes. The body goes last so that a write refused part-way leaves it, and
6000 /// the metadata in it, as it stood. A terminal status selects its option and then closes,
6001 /// as a whole write does; an open one selects its option and then reopens. The origin
6002 /// field is never written: an update is of an item that already exists, whose origin is
6003 /// what it is.
6004 ///
6005 /// The task answered is the item as those writes left it, built from the read and what was
6006 /// sent rather than read again — the same record a later read in this run answers from.
6007 async fn targeted_update(
6008 &self,
6009 id: &NativeId,
6010 update: &TaskUpdate,
6011 ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
6012 // Everything this source can refuse without reading the item is refused first, in the
6013 // words a whole write of the same fields is refused with.
6014 update.consistent()?;
6015 if update
6016 .title
6017 .as_deref()
6018 .is_some_and(|title| title.starts_with(DESIGN_TITLE_PREFIX))
6019 {
6020 return Err(SourceError::Refused {
6021 message: format!(
6022 "the title of this task begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
6023 spells a document, so it would read back as one rather than as a task; \
6024 retitle it",
6025 self.name
6026 ),
6027 });
6028 }
6029 if let Some(delivers) = &update.delivers {
6030 TaskRef::listed(
6031 TaskRef::DELIVERS_KEY,
6032 id,
6033 Some(&self.name),
6034 delivers.clone(),
6035 )
6036 .map_err(|message| SourceError::Refused { message })?;
6037 }
6038 if self.priorities.is_none()
6039 && update
6040 .priority
6041 .is_some_and(|priority| priority != Priority::None)
6042 {
6043 return Err(self.holds_no_priority());
6044 }
6045 let target = update
6046 .status
6047 .as_ref()
6048 .map(|status| self.resolved_target(ItemKind::Task, status.category))
6049 .transpose()?;
6050 let Some(mut item) = self
6051 .bound_item(id)
6052 .await?
6053 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
6054 else {
6055 return Ok(None);
6056 };
6057 let before = item.task()?;
6058
6059 let mut status_move = None;
6060 if let (Some(status), Some(target)) = (&update.status, target) {
6061 let board = self.status_board(&item).await?;
6062 let (field, option, name) = self
6063 .column_for(&board.fields, ItemKind::Task, status.category, &target)?
6064 .ok_or_else(|| SourceError::Malformed {
6065 message: format!(
6066 "status {} of source {} names no board Status option",
6067 category_name(status.category),
6068 self.name
6069 ),
6070 })?;
6071 let terminal = matches!(target, StatusTarget::Terminal(_, _));
6072 if terminal && item.content_kind == ContentKind::DraftIssue {
6073 return Err(self.closes_a_draft(status.category));
6074 }
6075 let landed = match &target {
6076 StatusTarget::Terminal(_, reason) => {
6077 self.statuses
6078 .status(ItemKind::Task, Some(&name), true, Some(reason.reason()))
6079 }
6080 _ => self
6081 .statuses
6082 .status(ItemKind::Task, Some(&name), false, None),
6083 };
6084 let option_moves = item
6085 .option
6086 .as_deref()
6087 .is_none_or(|held| !held.eq_ignore_ascii_case(&name));
6088 let state_moves = item.content_kind == ContentKind::Issue
6089 && (item.closed != terminal || (terminal && item.status != landed));
6090 if let Some(moves) = Moves::of(option_moves, state_moves) {
6091 status_move = Some(StatusMove {
6092 board: board.id,
6093 field,
6094 option,
6095 name,
6096 target,
6097 landed,
6098 moves,
6099 });
6100 }
6101 }
6102
6103 let mut priority_move = None;
6104 if let Some(priority) = update.priority
6105 && self.priorities.is_some()
6106 && item.priority != HeldPriority::Read(priority)
6107 {
6108 let board = match (item.carried_board(), item.named_board()) {
6109 (Some(board), _) => board,
6110 (None, Some(board)) if item.defines(PRIORITY_FIELD) => BoardFields {
6111 id: board,
6112 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
6113 },
6114 _ => self.board_fields().await?,
6115 };
6116 if let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? {
6117 priority_move = Some((board.id, write, priority));
6118 }
6119 }
6120
6121 // Resolved before the body is composed, because a far end `blockedBy` cannot name is
6122 // recorded in the slot, and the slot travels in the one body update below.
6123 let edges = match &update.depends_on {
6124 Some(edges) => Some(
6125 self.partition_edges(
6126 BoardKind::Work(ItemKind::Task),
6127 item.content_kind,
6128 item.blocked_by.as_deref(),
6129 edges,
6130 )
6131 .await?,
6132 ),
6133 None => None,
6134 };
6135
6136 let mut slot = item.slot.clone();
6137 for (key, value) in &update.metadata_set {
6138 slot.insert(key.as_str().to_owned(), value.clone());
6139 }
6140 for key in &update.metadata_remove {
6141 slot.remove(key.as_str());
6142 }
6143 if let Some(delivers) = &update.delivers {
6144 set_task_list(&mut slot, TaskRef::DELIVERS_KEY, delivers);
6145 }
6146 if let Some((_, recorded)) = &edges {
6147 record_edges(&mut slot, recorded);
6148 }
6149 let held = item.raw_body.clone().unwrap_or_default();
6150 let content = match &update.content {
6151 Some(content) => with_content(&held, content)?,
6152 None => held.clone(),
6153 };
6154 // A slot holding what it held is kept byte for byte, compared as JSON rather than as
6155 // the body's bytes, as a metadata write compares it: a slot a person spelled with
6156 // other whitespace would otherwise be re-encoded, which is a write nobody asked for.
6157 let body = if slot == item.slot {
6158 content
6159 } else {
6160 with_slot(&content, &slot)?
6161 };
6162 // Checked before anything is sent, as a content write checks it: content ending in
6163 // what this source reads as its own slot would read back as metadata.
6164 let (visible, read) = metadata_body(Some(body.clone()))?;
6165 let wanted = update.content.as_deref().or(item.body.as_deref());
6166 if visible.as_deref().unwrap_or_default() != wanted.unwrap_or_default() || read != slot {
6167 return Err(SourceError::Refused {
6168 message: format!(
6169 "this content ends in what source {} reads as its own metadata slot \
6170 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
6171 as content; next: remove that trailing block from the content",
6172 self.name
6173 ),
6174 });
6175 }
6176 let recorded_moves =
6177 slot.get(DependencyEdge::RECORDED_KEY) != item.slot.get(DependencyEdge::RECORDED_KEY);
6178
6179 // One `updateIssue` carries all three, because every mutation spends the secondary
6180 // limiter and the title, body and state are one mutation's inputs.
6181 let mut fields = serde_json::Map::new();
6182 if let Some(title) = update.title.as_ref().filter(|title| **title != item.title) {
6183 fields.insert("title".to_owned(), json!(title));
6184 }
6185 if body != held {
6186 fields.insert("body".to_owned(), json!(body));
6187 }
6188 if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.state()) {
6189 fields.insert("stateInput".to_owned(), state_input(Some(&moving.target)));
6190 }
6191 // **The body is written last, and that is the guarantee a refusal part-way keeps.**
6192 // GitHub runs no two requests as one, and runs one document's mutation fields in order
6193 // without undoing an earlier field when a later one fails — so a body written before a
6194 // board field the board then refused would be left changed. Written after every other
6195 // write has landed, a refusal anywhere leaves the item's body, and every metadata key
6196 // it carries, exactly as they stood. So the `Status` option and the `Priority` field go
6197 // first, together in one request — a terminal option selected before the issue
6198 // closes, as a whole write does — then the `blockedBy` difference, then the body.
6199 let mut board_writes: Vec<(&BoardId, (String, Value))> = Vec::new();
6200 let mut clear: Option<(&BoardId, &str)> = None;
6201 if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.option()) {
6202 board_writes.push((
6203 &moving.board,
6204 (
6205 moving.field.clone(),
6206 json!({"singleSelectOptionId": moving.option}),
6207 ),
6208 ));
6209 }
6210 match &priority_move {
6211 Some((board, PriorityWrite::Select { field, option }, _)) => board_writes.push((
6212 board,
6213 (field.clone(), json!({"singleSelectOptionId": option})),
6214 )),
6215 Some((board, PriorityWrite::Clear { field }, _)) => clear = Some((board, field)),
6216 None => {}
6217 }
6218 let mut boards: Vec<&BoardId> = board_writes.iter().map(|(board, _)| *board).collect();
6219 boards.extend(clear.map(|(board, _)| board));
6220 boards.dedup_by(|one, other| one.as_str() == other.as_str());
6221 for board in boards {
6222 let writes = board_writes
6223 .iter()
6224 .filter(|(on, _)| on.as_str() == board.as_str())
6225 .map(|(_, write)| write.clone())
6226 .collect::<Vec<_>>();
6227 let cleared = clear
6228 .filter(|(on, _)| on.as_str() == board.as_str())
6229 .map(|(_, field)| field);
6230 self.set_item_fields(board.as_str(), &item.item_id, &writes, cleared)
6231 .await?;
6232 }
6233 let mut blocked_by_moved = false;
6234 if let Some((native, _)) = &edges
6235 && item.content_kind == ContentKind::Issue
6236 {
6237 blocked_by_moved = self
6238 .reconcile_blocked_by(
6239 &item.id,
6240 native,
6241 Issue::Existing(item.blocked_by.as_deref()),
6242 )
6243 .await?;
6244 }
6245 if !fields.is_empty() {
6246 self.update_content(item.content_kind, &item.id, Value::Object(fields))
6247 .await?;
6248 }
6249
6250 if let Some(title) = &update.title {
6251 item.title.clone_from(title);
6252 }
6253 item.body = visible.filter(|value| !value.is_empty());
6254 item.raw_body = (!body.is_empty() || item.raw_body.is_some()).then_some(body);
6255 item.slot = slot;
6256 if let Some(delivers) = &update.delivers {
6257 item.delivers.clone_from(delivers);
6258 }
6259 if let Some(moving) = status_move {
6260 item.closed = matches!(moving.target, StatusTarget::Terminal(_, _))
6261 && item.content_kind == ContentKind::Issue;
6262 item.status = moving.landed;
6263 item.option = Some(moving.name);
6264 }
6265 if let Some((_, _, priority)) = priority_move {
6266 item.priority = HeldPriority::Read(priority);
6267 }
6268 let task = item.task()?;
6269 let mut written = update.changed(&before, &task);
6270 if blocked_by_moved || recorded_moves {
6271 written.insert(UpdatedField::DependsOn);
6272 }
6273 self.remember_written(item, false)?;
6274 Ok(Some(TaskUpdateOutcome {
6275 task,
6276 written,
6277 delivers_before: before.delivers,
6278 }))
6279 }
6280
6281 /// Replace one issue's visible body and its [`MetadataKey::TEMPLATE_KEY`] slot entry
6282 /// together, and nothing else; see [`TaskSource::set_task_rendering`].
6283 ///
6284 /// One update of the body: the content outside the slot, and inside it that one entry,
6285 /// every other entry kept as it was. This source keeps no template answers — an issue has
6286 /// no room beside itself that is not its body, and answers written there would duplicate
6287 /// what the content already says and count against GitHub's body limit — so `answers`
6288 /// reaches nothing here. A body that would not change is not sent at all.
6289 async fn replace_rendering(
6290 &self,
6291 id: &NativeId,
6292 kind: BoardKind,
6293 content: &str,
6294 provenance: &Value,
6295 ) -> Result<Option<()>, SourceError> {
6296 let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
6297 return Ok(None);
6298 };
6299 let held = item.raw_body.clone().unwrap_or_default();
6300 let mut slot = item.slot.clone();
6301 slot.insert(MetadataKey::TEMPLATE_KEY.to_owned(), provenance.clone());
6302 let body = with_slot(&with_content(&held, content)?, &slot)?;
6303 // Checked before anything is sent, as a content write checks it.
6304 let (visible, read) = metadata_body(Some(body.clone()))?;
6305 if visible.as_deref().unwrap_or_default() != content || read != slot {
6306 return Err(SourceError::Refused {
6307 message: format!(
6308 "this content ends in what source {} reads as its own metadata slot \
6309 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
6310 as content; next: remove that trailing block from the template",
6311 self.name
6312 ),
6313 });
6314 }
6315 if body != held {
6316 self.update_content(item.content_kind, &item.id, json!({"body": body}))
6317 .await?;
6318 }
6319 item.body = visible.filter(|value| !value.is_empty());
6320 item.raw_body = Some(body);
6321 item.slot = read;
6322 self.remember_written(item, false)?;
6323 Ok(Some(()))
6324 }
6325
6326 async fn set_item_field(
6327 &self,
6328 board_id: &str,
6329 item_id: &str,
6330 field_id: &str,
6331 value: Value,
6332 ) -> Result<(), SourceError> {
6333 let data = self
6334 .graphql(
6335 graphql::UPDATE_FIELD,
6336 json!({"input":{
6337 "projectId":board_id,"itemId":item_id,"fieldId":field_id,"value":value
6338 },"readPriority":false,"priorityName":PRIORITY_FIELD}),
6339 )
6340 .await?;
6341 let returned = data
6342 .pointer("/updateProjectV2ItemFieldValue/projectV2Item")
6343 .ok_or_else(|| SourceError::Malformed {
6344 message: "GitHub field update returned no project item".into(),
6345 })?;
6346 if required_str(returned, "id")? != item_id {
6347 return Err(SourceError::Malformed {
6348 message: "GitHub field update returned the wrong project item".into(),
6349 });
6350 }
6351 Ok(())
6352 }
6353
6354 /// GitHub accepts one value per field mutation; aliases combine those mutations in
6355 /// one request. Every returned item id is checked, including optional aliases.
6356 async fn set_item_fields(
6357 &self,
6358 board: &str,
6359 item: &str,
6360 fields: &[(String, Value)],
6361 clear: Option<&str>,
6362 ) -> Result<(), SourceError> {
6363 if fields.len() <= 1 && clear.is_none() {
6364 if let Some((field, value)) = fields.first() {
6365 self.set_item_field(board, item, field, value.clone())
6366 .await?;
6367 }
6368 return Ok(());
6369 }
6370 if fields.is_empty() {
6371 if let Some(field) = clear {
6372 self.write_priority(
6373 board,
6374 item,
6375 &PriorityWrite::Clear {
6376 field: field.to_owned(),
6377 },
6378 )
6379 .await?;
6380 }
6381 return Ok(());
6382 }
6383 let input = |index: usize| {
6384 let (field, value) = fields.get(index).unwrap_or(&fields[0]);
6385 json!({"projectId":board,"itemId":item,"fieldId":field,"value":value})
6386 };
6387 let data = self.graphql(graphql::UPDATE_FIELDS, json!({
6388 "input":input(0),"second":input(1),"third":input(2),
6389 "writeSecond":fields.len()>1,"writeThird":fields.len()>2,"writeClear":clear.is_some(),
6390 "clear":{"projectId":board,"itemId":item,"fieldId":clear.unwrap_or(&fields[0].0)}
6391 })).await?;
6392 for alias in [
6393 Some("updateProjectV2ItemFieldValue"),
6394 (fields.len() > 1).then_some("second"),
6395 (fields.len() > 2).then_some("third"),
6396 clear.map(|_| "cleared"),
6397 ]
6398 .into_iter()
6399 .flatten()
6400 {
6401 let returned = data
6402 .get(alias)
6403 .and_then(|value| value.get("projectV2Item"))
6404 .ok_or_else(|| SourceError::Malformed {
6405 message: format!("GitHub field update {alias} returned no project item"),
6406 })?;
6407 if required_str(returned, "id")? != item {
6408 return Err(SourceError::Malformed {
6409 message: format!("GitHub field update {alias} returned the wrong project item"),
6410 });
6411 }
6412 }
6413 Ok(())
6414 }
6415
6416 async fn native_dependency_ids(&self, id: &NativeId) -> Result<Vec<String>, SourceError> {
6417 let mut after: Option<String> = None;
6418 let mut ids = Vec::new();
6419 loop {
6420 let data = self
6421 .graphql(
6422 graphql::ISSUE_DEPENDENCIES,
6423 json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after}),
6424 )
6425 .await?;
6426 let connection =
6427 data.pointer("/node/blockedBy")
6428 .ok_or_else(|| SourceError::Malformed {
6429 message: "GitHub dependency response has no blockedBy connection".into(),
6430 })?;
6431 ids.extend(
6432 connection
6433 .get("nodes")
6434 .and_then(Value::as_array)
6435 .ok_or_else(|| SourceError::Malformed {
6436 message: "GitHub dependency response nodes is not an array".into(),
6437 })?
6438 .iter()
6439 .map(|value| required_str(value, "id").map(str::to_owned))
6440 .collect::<Result<Vec<_>, _>>()?,
6441 );
6442 let next = next_cursor(connection)?;
6443 if let Some(next) = &next {
6444 validate_cursor_progress(after.as_deref(), &next.0)?;
6445 }
6446 after = next.map(|cursor| cursor.0);
6447 if after.is_none() {
6448 return Ok(ids);
6449 }
6450 }
6451 }
6452
6453 async fn dependencies(
6454 &self,
6455 id: &NativeId,
6456 near_kind: ItemKind,
6457 direction: Direction,
6458 page: &PageRequest,
6459 ) -> Result<Page<DependencyEdge>, SourceError> {
6460 validate_page(page)?;
6461 let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
6462 let cursor = page.cursor.as_ref().map(|c| c.0.as_str());
6463 let recorded = recorded_offset(cursor, direction)?;
6464 // What this issue is blocked by, when a read of it by its own id in this command
6465 // already carried the whole connection — a copy reads the item it writes before it
6466 // reads its edges — and the page asked for is the whole of it, or the recorded tail
6467 // after it. Answered from that read, in the shape the dependency read answers in;
6468 // anything else is asked of GitHub.
6469 let carried = match direction {
6470 Direction::DependsOn => self
6471 .resolved_cache()?
6472 .get(id)
6473 .filter(|item| item.content_kind == ContentKind::Issue)
6474 .and_then(|item| Some((item.blocked_by.clone()?, item.raw_body.clone()))),
6475 Direction::DependedOnBy => None,
6476 }
6477 .filter(|(nodes, _)| recorded.is_some() || (cursor.is_none() && nodes.len() <= limit));
6478 // Asked for even in the recorded phase, whose page reads nothing from the
6479 // connection: `__typename` is what says whether this item has a native
6480 // relationship at all, and that is what decides which far ends the reserved key is
6481 // allowed to hold.
6482 let data = match carried {
6483 Some((nodes, body)) => json!({"node":{"__typename":"Issue","body":body,
6484 "blockedBy":{"nodes":nodes,"pageInfo":{"hasNextPage":false,"endCursor":null}}}}),
6485 None => {
6486 self.graphql(
6487 graphql::ISSUE_DEPENDENCIES,
6488 json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),
6489 "after":if recorded.is_some() {None} else {cursor}}),
6490 )
6491 .await?
6492 }
6493 };
6494 let node =
6495 data.get("node")
6496 .filter(|v| !v.is_null())
6497 .ok_or_else(|| SourceError::Refused {
6498 message: format!(
6499 "GitHub item {} was not found or does not support dependencies",
6500 id.0
6501 ),
6502 })?;
6503 let connection_name = match direction {
6504 Direction::DependsOn => "blockedBy",
6505 Direction::DependedOnBy => "blocking",
6506 };
6507 // A draft has neither `blockedBy` nor `blocking`, so nothing it depends on can be
6508 // named natively and the reserved key may hold any far end. An issue's connections
6509 // hold issues, and this source reads them at the near item's own level.
6510 let natively_names = (required_str(node, "__typename")? == "Issue").then_some(near_kind);
6511 if let Some(offset) = recorded {
6512 return Ok(recorded_page(
6513 self.recorded_edges(id, near_kind, direction, natively_names, node)
6514 .await?,
6515 offset,
6516 limit,
6517 ));
6518 }
6519 if natively_names.is_none() {
6520 return Ok(recorded_page(
6521 self.recorded_edges(id, near_kind, direction, natively_names, node)
6522 .await?,
6523 0,
6524 limit,
6525 ));
6526 }
6527 let connection = node
6528 .get(connection_name)
6529 .ok_or_else(|| SourceError::Malformed {
6530 message: "GitHub dependency response is missing its connection".into(),
6531 })?;
6532 let nodes = connection
6533 .get("nodes")
6534 .and_then(Value::as_array)
6535 .ok_or_else(|| SourceError::Malformed {
6536 message: "GitHub dependency response nodes is not an array".into(),
6537 })?;
6538 // `from` depends on `to`, always. GitHub spells the same relationship from either
6539 // end — `blockedBy` lists what this item waits on, `blocking` lists what waits on
6540 // it — so the near item is `from` in one direction and `to` in the other.
6541 let items = nodes
6542 .iter()
6543 .map(|value| {
6544 let related = NativeId(required_str(value, "id")?.into());
6545 let related_kind = related_kind(value)?;
6546 let (from, to) = match direction {
6547 Direction::DependsOn => (
6548 DependencyEndpoint::from_native(id.clone(), near_kind),
6549 DependencyEndpoint::from_native(related, related_kind),
6550 ),
6551 Direction::DependedOnBy => (
6552 DependencyEndpoint::from_native(related, related_kind),
6553 DependencyEndpoint::from_native(id.clone(), near_kind),
6554 ),
6555 };
6556 Ok(DependencyEdge {
6557 from,
6558 to,
6559 kind: DependencyKind::Blocks,
6560 })
6561 })
6562 .collect::<Result<Vec<_>, SourceError>>()?;
6563 let mut next = next_cursor(connection)?;
6564 if let Some(next) = &next {
6565 validate_cursor_progress(cursor, &next.0)?;
6566 }
6567 if next.is_none()
6568 && !self
6569 .recorded_edges(id, near_kind, direction, natively_names, node)
6570 .await?
6571 .is_empty()
6572 {
6573 next = Some(Cursor(format!("{RECORDED_CURSOR}0")));
6574 }
6575 Ok(Page { items, next })
6576 }
6577
6578 /// The edges this item records under [`DependencyEdge::RECORDED_KEY`], which is where
6579 /// a far end in another source has to live: no GitHub issue relationship can name one.
6580 ///
6581 /// Only forwards. The reverse of a recorded edge is derived from the far end, and this
6582 /// source never writes one down.
6583 ///
6584 /// The metadata lives in the item's own body slot, and `node` is the dependency read's
6585 /// own answer, which carries an issue's body — so an issue's recorded edges cost no
6586 /// request beyond the read already made, and reading the board for them would be a
6587 /// walk of every item for one field of one. A draft has no body in that answer, because
6588 /// a draft is not an issue, so a draft's are read off its own read by id — never off a
6589 /// listing of the board, which can be behind on the very item asked about.
6590 async fn recorded_edges(
6591 &self,
6592 id: &NativeId,
6593 near_kind: ItemKind,
6594 direction: Direction,
6595 natively_names: Option<ItemKind>,
6596 node: &Value,
6597 ) -> Result<Vec<DependencyEdge>, SourceError> {
6598 if direction != Direction::DependsOn {
6599 return Ok(Vec::new());
6600 }
6601 let slot = match node.get("body") {
6602 Some(body) if natively_names.is_some() => {
6603 metadata_body(body.as_str().map(str::to_owned))?.1
6604 }
6605 _ => {
6606 let Some(item) = self.bound_item(id).await? else {
6607 return Ok(Vec::new());
6608 };
6609 item.slot
6610 }
6611 };
6612 DependencyEdge::recorded(&slot, id, near_kind, &self.name, natively_names)
6613 .map_err(|message| SourceError::Malformed { message })
6614 }
6615
6616 fn configured_repository(&self) -> Result<&RepositoryTarget, SourceError> {
6617 self.repository
6618 .as_ref()
6619 .ok_or_else(|| SourceError::Refused {
6620 message: format!(
6621 "source {} has no repository configured, and a GitHub Projects board has no \
6622 repository of its own to create an issue in; set repository: owner/name on \
6623 this source",
6624 self.name
6625 ),
6626 })
6627 }
6628
6629 /// The repository one new issue is created in, under the rule [`RepositoryTarget`]
6630 /// states.
6631 ///
6632 /// The fallback is demanded first, whichever arm answers: a write without a configured
6633 /// repository is refused naming the field exactly as it was before the rule existed,
6634 /// so a source that could not write before cannot write now, rather than writing for
6635 /// the one item whose own field happens to decide it.
6636 ///
6637 /// Everything this refuses is refused before `createIssue`, so a refusal leaves no
6638 /// issue behind: an entry that is not a repository on [`RepositoryTarget::HOST`], an
6639 /// entry owned by someone other than the owner of the parent issue's repository —
6640 /// GitHub accepts a sub-issue from another repository of the same owner and from no
6641 /// other, so `addSubIssue` would refuse it after the issue existed — a parent the
6642 /// board does not hold, and a parent that is a draft, which GitHub gives no sub-issues,
6643 /// both of which `addSubIssue` would likewise refuse too late. Whether the entry exists
6644 /// and is visible to the token is checked where its node id is resolved, still before
6645 /// `createIssue`. The parent is read by its own id through [`Self::item_by_id`] — never
6646 /// looked up in a listing of the board, which can be minutes behind an issue its own
6647 /// `projectItems` already places on it — and that read answers first from this process's
6648 /// own record, so a project created moments ago in this command answers though GitHub
6649 /// has not caught up.
6650 async fn creation_target(
6651 &self,
6652 incoming: &Incoming<'_>,
6653 ) -> Result<RepositoryTarget, SourceError> {
6654 let fallback = self.configured_repository()?;
6655 let what = |incoming: &Incoming<'_>| {
6656 format!(
6657 "{} {:?}",
6658 incoming.written.kind().describes(),
6659 incoming.title
6660 )
6661 };
6662 let parent = match incoming.parent {
6663 Some(parent) => Some(self.bound_item(parent).await?.ok_or_else(|| {
6664 SourceError::Refused {
6665 message: format!(
6666 "GitHub project issue {} was not found on the board of source {}, so {} \
6667 cannot be filed under it",
6668 parent.0,
6669 self.name,
6670 what(incoming)
6671 ),
6672 }
6673 })?),
6674 None => None,
6675 };
6676 let parents_repository = parent
6677 .as_ref()
6678 .map(|parent| {
6679 // A draft is on the board and so is found, but it has no repository to
6680 // place a task in and GitHub gives it no sub-issues, so `addSubIssue`
6681 // would refuse the task only once `createIssue` had made it.
6682 if parent.content_kind == ContentKind::DraftIssue {
6683 return Err(SourceError::Refused {
6684 message: format!(
6685 "GitHub project item {} on the board of source {} is a draft, \
6686 which cannot have sub-issues, so {} cannot be filed under it",
6687 parent.id.0,
6688 self.name,
6689 what(incoming)
6690 ),
6691 });
6692 }
6693 // An issue's repository is where a sub-issue is placed and whose owner it
6694 // is compared against, so a parent whose repository this source cannot
6695 // spell as `owner/name` — GitHub's login grammar is wider than this
6696 // source's floor — is one nothing can be filed under.
6697 parent
6698 .own_repository
6699 .as_ref()
6700 .and_then(|origin| RepositoryTarget::from_origin(origin).ok())
6701 .ok_or_else(|| SourceError::Malformed {
6702 message: format!(
6703 "GitHub project issue {} on the board of source {} is in {}, which \
6704 is not a {}/owner/name repository this source can place {} in",
6705 parent.id.0,
6706 self.name,
6707 parent
6708 .own_repository
6709 .as_ref()
6710 .map_or("no repository", Repository::as_str),
6711 RepositoryTarget::HOST,
6712 what(incoming)
6713 ),
6714 })
6715 })
6716 .transpose()?;
6717 match incoming.repositories {
6718 [named] => {
6719 let target =
6720 RepositoryTarget::from_origin(named).map_err(|_| SourceError::Refused {
6721 message: format!(
6722 "{} names repository {}, which is not a {}/owner/name repository \
6723 source {} can create an issue in; name one that is, or name none",
6724 what(incoming),
6725 named.as_str(),
6726 RepositoryTarget::HOST,
6727 self.name
6728 ),
6729 })?;
6730 if let Some(parents) = &parents_repository
6731 && parents.owner != target.owner
6732 {
6733 return Err(SourceError::Refused {
6734 message: format!(
6735 "{} names repository {}, owned by {}, but its project's issue is in \
6736 {}, owned by {}, and GitHub files a sub-issue only in a repository \
6737 of the same owner as its parent issue; name a repository of {}, or \
6738 name none",
6739 what(incoming),
6740 target.slug(),
6741 target.owner,
6742 parents.slug(),
6743 parents.owner,
6744 parents.owner
6745 ),
6746 });
6747 }
6748 Ok(target)
6749 }
6750 _ => Ok(parents_repository.unwrap_or_else(|| fallback.clone())),
6751 }
6752 }
6753
6754 /// The node id of the repository `incoming` is being created in, or the refusal naming
6755 /// the item and the repository the token cannot see.
6756 ///
6757 /// Resolved once per command per repository; see [`Self::repository_cache`].
6758 async fn repository_id(
6759 &self,
6760 repository: &RepositoryTarget,
6761 incoming: &Incoming<'_>,
6762 ) -> Result<String, SourceError> {
6763 if let Some(id) = self.repository_cache()?.get(repository).cloned() {
6764 return Ok(id);
6765 }
6766 let data = self
6767 .graphql(
6768 graphql::REPOSITORY,
6769 json!({"owner":repository.owner,"name":repository.name}),
6770 )
6771 .await?;
6772 self.repository_read(&data, repository, incoming)
6773 }
6774
6775 /// The repository's node id out of an answer carrying the `repository` root, held for
6776 /// the rest of this command, or the refusal naming the item that cannot be created in it.
6777 fn repository_read(
6778 &self,
6779 data: &Value,
6780 repository: &RepositoryTarget,
6781 incoming: &Incoming<'_>,
6782 ) -> Result<String, SourceError> {
6783 let node = data
6784 .get("repository")
6785 .filter(|value| !value.is_null())
6786 .ok_or_else(|| SourceError::Refused {
6787 message: format!(
6788 "GitHub repository {} was not found or is not visible to the token, so {} \
6789 {:?} cannot be created in it",
6790 repository.slug(),
6791 incoming.written.kind().describes(),
6792 incoming.title
6793 ),
6794 })?;
6795 let id = required_str(node, "id")?.to_owned();
6796 self.repository_cache()?
6797 .insert(repository.clone(), id.clone());
6798 Ok(id)
6799 }
6800
6801 fn repository_cache(
6802 &self,
6803 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<RepositoryTarget, String>>, SourceError> {
6804 self.repository_cache
6805 .lock()
6806 .map_err(|_| SourceError::Unavailable {
6807 message: "this source's record of the destination repository was left \
6808 inconsistent by an earlier failure; next: run the command again"
6809 .into(),
6810 })
6811 }
6812
6813 /// Create or update one board item, whichever kind it is.
6814 async fn write_item(
6815 &self,
6816 incoming: &Incoming<'_>,
6817 target: Option<&NativeId>,
6818 depends_on: &[DependencyEdge],
6819 ) -> Result<NativeId, SourceError> {
6820 // Refused before anything is read or written: a task or a project titled the way
6821 // this board spells a document would land as an issue this same source reads back
6822 // as a document, so the field this destination cannot carry is named rather than
6823 // written and silently reclassified.
6824 if let Written::Work(kind, _) = incoming.written
6825 && incoming.title.starts_with(DESIGN_TITLE_PREFIX)
6826 {
6827 return Err(SourceError::Refused {
6828 message: format!(
6829 "the title of this {} begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
6830 spells a document, so it would read back as one rather than as a {}; \
6831 retitle it, or copy it as a document",
6832 kind.marker(),
6833 self.name,
6834 kind.marker()
6835 ),
6836 });
6837 }
6838 // The destination is read by its own id, and whether this board holds it is decided
6839 // by that read — its own `projectItems` — rather than by whether a listing of the
6840 // board happens to include it yet. See the module documentation.
6841 let existing = match target {
6842 Some(target) => {
6843 Some(
6844 self.bound_item(target)
6845 .await?
6846 .ok_or_else(|| SourceError::Refused {
6847 message: format!("GitHub destination item {} was not found", target.0),
6848 })?,
6849 )
6850 }
6851 None => None,
6852 };
6853 let existing = existing.as_ref();
6854 // An existing issue is never moved; a new one is created where the rule says — and
6855 // knowing where is what lets the board's fields and that repository's id be read
6856 // together, before anything below needs either.
6857 let creation_target = match existing {
6858 Some(_) => None,
6859 None => {
6860 let target = self.creation_target(incoming).await?;
6861 self.creation_context(&target, incoming).await?;
6862 Some(target)
6863 }
6864 };
6865 let board = self
6866 .fields_for(
6867 existing,
6868 incoming.written.status().is_some(),
6869 incoming
6870 .priority
6871 .is_some_and(|priority| priority != Priority::None),
6872 )
6873 .await?;
6874 let status_target = incoming
6875 .written
6876 .work_status()
6877 .map(|(kind, status)| self.resolved_target(kind, status.category))
6878 .transpose()?;
6879 let column = match (incoming.written.work_status(), status_target.as_ref()) {
6880 (Some((kind, status)), Some(target)) => {
6881 self.column_for(&board.fields, kind, status.category, target)?
6882 }
6883 _ => None,
6884 };
6885 // Resolved before anything is created, for the reason the column above is: a
6886 // priority this board has no option for is refused while nothing has been written.
6887 let priority_write = match incoming.priority {
6888 Some(priority) => self.priority_write(&board.fields, existing, priority)?,
6889 None => None,
6890 };
6891 let content_kind = existing.map_or(ContentKind::Issue, |item| item.content_kind);
6892 if content_kind == ContentKind::DraftIssue {
6893 if let (Some(StatusTarget::Terminal(_, _)), Some(status)) =
6894 (status_target.as_ref(), incoming.written.status())
6895 {
6896 return Err(self.closes_a_draft(status.category));
6897 }
6898 if incoming.parent.is_some() {
6899 return Err(SourceError::Refused {
6900 message: "GitHub draft items cannot be a project's sub-issue".into(),
6901 });
6902 }
6903 }
6904 match existing {
6905 Some(item) if content_kind == ContentKind::Issue => {
6906 if item.labels != incoming.labels {
6907 return Err(SourceError::Refused {
6908 message: "GitHub issue labels differ from the labels being written".into(),
6909 });
6910 }
6911 }
6912 _ => {
6913 if !incoming.labels.is_empty() {
6914 return Err(SourceError::Refused {
6915 message: "GitHub items created by this destination carry no labels".into(),
6916 });
6917 }
6918 }
6919 }
6920
6921 // The repository the issue really lives in is what the slot below is written against,
6922 // so a single entry that is where the issue is created travels as no key at all, and
6923 // the read side derives it back from the issue.
6924 let own_repository = match (existing, &creation_target) {
6925 (Some(item), _) => item.own_repository.clone(),
6926 (None, Some(target)) => Some(
6927 Repository::try_from(target.origin())
6928 .map_err(|message| SourceError::Config { message })?,
6929 ),
6930 (None, None) => None,
6931 };
6932 let (native, fallback) = self
6933 .partition_edges(
6934 incoming.written.kind(),
6935 content_kind,
6936 existing.and_then(|item| item.blocked_by.as_deref()),
6937 depends_on,
6938 )
6939 .await?;
6940 let slot = slot_metadata(incoming, own_repository.as_ref(), &fallback);
6941 let body = compose_body(incoming.content, &slot)?;
6942 // Read before anything is created, for the reason the field below is: a value
6943 // this destination cannot store has to refuse, and refusing after `createIssue`
6944 // would leave an issue behind that nothing asked for. The engine writes a
6945 // qualified id here; a caller handing this key anything else is told so rather
6946 // than having it silently stored as no origin at all.
6947 // 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.
6948 let origin = match incoming.metadata.get(ORIGIN_KEY) {
6949 None => "",
6950 Some(Value::String(origin)) => origin.as_str(),
6951 Some(other) => {
6952 return Err(SourceError::Refused {
6953 message: format!(
6954 "{ORIGIN_KEY} holds a qualified id spelled as a string, and this item's \
6955 is {other}"
6956 ),
6957 });
6958 }
6959 };
6960 // Resolved before anything is created: a board that cannot carry the copy origin
6961 // has to refuse the write, and refusing it after `createIssue` would leave an
6962 // issue behind that nothing asked for.
6963 let origin_field = match Board::field(&board.fields, ORIGIN_FIELD)? {
6964 Some(field) => {
6965 if required_str(field, "__typename")? != "ProjectV2Field" {
6966 return Err(SourceError::Refused {
6967 message: format!(
6968 "GitHub board source-owned {ORIGIN_FIELD} field is not a text field"
6969 ),
6970 });
6971 }
6972 Some(required_str(field, "id")?.to_owned())
6973 }
6974 None if incoming.metadata.contains_key(ORIGIN_KEY) => {
6975 return Err(SourceError::Refused {
6976 message: format!(
6977 "GitHub board has no source-owned {ORIGIN_FIELD} text field, and the \
6978 item carries {ORIGIN_KEY}; add a text field named {ORIGIN_FIELD} to \
6979 the board"
6980 ),
6981 });
6982 }
6983 None => None,
6984 };
6985
6986 let Landed {
6987 content_id,
6988 item_id,
6989 url,
6990 number,
6991 } = match existing {
6992 // Its content is written last, below, once everything else has landed.
6993 Some(item) => Landed {
6994 content_id: item.id.clone(),
6995 item_id: item.item_id.clone(),
6996 url: item.url.clone(),
6997 number: item.number,
6998 },
6999 None => {
7000 let target = creation_target
7001 .as_ref()
7002 .ok_or_else(|| SourceError::Malformed {
7003 message: "a new item was decided without a repository to create it in"
7004 .into(),
7005 })?;
7006 self.create_and_file_issue(board.id.as_str(), target, incoming, &body)
7007 .await?
7008 }
7009 };
7010
7011 let written_option = column.as_ref().map(|(_, _, name)| name.clone());
7012 let column = column
7013 .filter(|(_, _, name)| existing.is_none_or(|item| item.option.as_ref() != Some(name)))
7014 .map(|(field, option, _)| (field, option));
7015 // Creating an item here is several calls — `createIssue`, which files it on the
7016 // board, then its board fields, the parent and the dependencies — and GitHub can fail
7017 // at any of them. Everything this source can refuse *before* the first of those is
7018 // already checked above, so what is left is GitHub itself failing part way. When it
7019 // does over an item this call created, the issue is taken back: a write that
7020 // refused must not leave an item behind that nobody asked for, and one that does
7021 // makes the retry create a second.
7022 // Whether the board-field write carrying a moved origin was answered as landing whole.
7023 // When it was refused, GitHub does not say which of its fields ran before the one that
7024 // failed, so the origin may or may not have moved.
7025 let mut origin_landed = false;
7026 let landed = self
7027 .finish_write(
7028 board.id.as_str(),
7029 incoming,
7030 &content_id,
7031 &item_id,
7032 content_kind,
7033 existing,
7034 origin_field.as_deref(),
7035 origin,
7036 column,
7037 status_target.as_ref(),
7038 priority_write.as_ref(),
7039 &native,
7040 &mut origin_landed,
7041 )
7042 .await;
7043 // An existing item's title, body and state go last, in one `updateIssue`, once its board
7044 // fields and its relationships have landed: a refusal of any of those then leaves its
7045 // body — and the metadata slot inside it — exactly as it stood.
7046 let landed = match (landed, existing) {
7047 (Ok(()), Some(item)) => {
7048 self.update_existing(item, incoming, &body, status_target.as_ref())
7049 .await
7050 }
7051 (landed, _) => landed,
7052 };
7053 if let Err(error) = landed {
7054 match existing {
7055 // Best effort, and the write's own failure is what the caller is told: a
7056 // refusal naming the tidy-up would hide why the write failed at all.
7057 None => {
7058 let _ = self.delete_issue(&content_id).await;
7059 }
7060 // The origin field is the one piece of an existing item's metadata written
7061 // before its body, so a write refused after it puts it back as it was. When
7062 // that is refused too, the write's own failure is still what the caller is
7063 // told — with what it left behind added, because the item's metadata is then
7064 // not as it stood and a caller retrying has to know which key moved.
7065 Some(item) => {
7066 let before = item.origin.as_deref().unwrap_or("");
7067 if let Some(field) = origin_field.as_deref()
7068 && before != origin
7069 && let Err(restore) = self
7070 .set_item_field(
7071 board.id.as_str(),
7072 &item.item_id,
7073 field,
7074 json!({"text": before}),
7075 )
7076 .await
7077 {
7078 let left = if origin_landed {
7079 format!(
7080 "its {ORIGIN_KEY} was moved to {origin:?} before that and could \
7081 not be put back to {before:?} ({restore}), so item {} still \
7082 holds {origin:?} there",
7083 item.id.0
7084 )
7085 } else {
7086 format!(
7087 "the refused write carried its {ORIGIN_KEY} from {before:?} to \
7088 {origin:?}, GitHub does not say whether that part of it ran, \
7089 and putting it back to {before:?} was refused ({restore}), so \
7090 item {} holds {origin:?} or {before:?} there",
7091 item.id.0
7092 )
7093 };
7094 return Err(noting(
7095 error,
7096 &format!(
7097 "; {left}; next: set {ORIGIN_KEY} on it back to {before:?}, or \
7098 run the write again"
7099 ),
7100 ));
7101 }
7102 }
7103 }
7104 return Err(error);
7105 }
7106
7107 let written_status = match (incoming.written.work_status(), status_target.as_ref()) {
7108 (Some((kind, _)), Some(StatusTarget::Terminal(_, reason))) => {
7109 self.statuses
7110 .status(kind, written_option.as_deref(), true, Some(reason.reason()))
7111 }
7112 (Some((kind, _)), Some(StatusTarget::Column(_))) => {
7113 self.statuses
7114 .status(kind, written_option.as_deref(), false, None)
7115 }
7116 (Some((_, status)), _) => status.clone(),
7117 (None, _) => Status {
7118 category: StatusCategory::Unknown,
7119 name: "Open".to_owned(),
7120 },
7121 };
7122
7123 // So the rest of this command reads what it just did rather than what the board
7124 // said before it. See `remember_written` for which half takes it.
7125 let remembered = Resolved {
7126 item_id,
7127 id: content_id.clone(),
7128 content_kind,
7129 kind: incoming.written.kind(),
7130 title: incoming.title.to_owned(),
7131 // The visible half of the body this write composed, split back off it the
7132 // way a read splits it — so what this record reports is what a read of the
7133 // same issue reports, rather than the person's text with the metadata slot
7134 // still on the end of it.
7135 body: metadata_body(body.clone())?.0,
7136 raw_body: body.clone(),
7137 // A document has no status of its own; what it reads back as is whatever
7138 // the issue's own state says, which is what a re-read reports.
7139 status: written_status,
7140 option: written_option.or_else(|| existing.and_then(|item| item.option.clone())),
7141 priority: match incoming.priority {
7142 Some(priority) => HeldPriority::Read(priority),
7143 None => existing.map_or(HeldPriority::Read(Priority::None), |item| {
7144 item.priority.clone()
7145 }),
7146 },
7147 // What `state_input` asked for: closed for a terminal target, open for any other
7148 // status, and the issue's own state left as it was by a document write.
7149 closed: content_kind == ContentKind::Issue
7150 && match status_target.as_ref() {
7151 Some(StatusTarget::Terminal(_, _)) => true,
7152 Some(_) => false,
7153 None => existing.is_some_and(|item| item.closed),
7154 },
7155 delivers: incoming.delivers.to_vec(),
7156 delivered_by: incoming.delivered_by.to_vec(),
7157 labels: incoming.labels.to_vec(),
7158 parent: incoming.parent.cloned(),
7159 origin: (!origin.is_empty()).then(|| origin.to_owned()),
7160 number,
7161 // In the update path this is the item's own url, read off `existing` where the
7162 // record above was bound, so one expression serves both halves.
7163 url,
7164 created_at: existing.and_then(|item| item.created_at),
7165 updated_at: existing.and_then(|item| item.updated_at),
7166 own_repository,
7167 repositories: incoming.repositories.to_vec(),
7168 slot,
7169 board_id: Some(board.id.as_str().to_owned()),
7170 fields: board
7171 .fields
7172 .get("nodes")
7173 .and_then(Value::as_array)
7174 .cloned()
7175 .unwrap_or_default(),
7176 board_fields: Some(board.fields.clone()),
7177 // What this write left the relationship holding is known by id alone, and a
7178 // later read of its edges needs each far end's kind, so it reads them again.
7179 blocked_by: None,
7180 };
7181 self.remember_written(remembered, existing.is_none())?;
7182 Ok(content_id)
7183 }
7184
7185 /// Everything a write does after the item exists: its board fields, its parent, and
7186 /// its dependencies.
7187 ///
7188 /// Split out of `write_item` so there is one place a failure past the point of no
7189 /// return is caught, rather than a tidy-up repeated at each `?` above.
7190 // llmlint: ignore[suppressions_justified] This is the tail of `write_item` lifted out
7191 // so there is one place a failure past the point of no return is caught, and its
7192 // arguments are exactly the values that tail already had in scope. Bundling them into a
7193 // struct would describe no concept — it would be "the arguments of this function" — and
7194 // would put the whole of `write_item`'s locals behind one more indirection.
7195 #[allow(clippy::too_many_arguments)]
7196 async fn finish_write(
7197 &self,
7198 board_id: &str,
7199 incoming: &Incoming<'_>,
7200 content_id: &NativeId,
7201 item_id: &str,
7202 content_kind: ContentKind,
7203 existing: Option<&Resolved>,
7204 origin_field: Option<&str>,
7205 origin: &str,
7206 column: Option<(String, String)>,
7207 status_target: Option<&StatusTarget>,
7208 priority: Option<&PriorityWrite>,
7209 native: &[String],
7210 origin_landed: &mut bool,
7211 ) -> Result<(), SourceError> {
7212 let mut fields = Vec::new();
7213 if let Some(field_id) = origin_field
7214 && existing.map_or(!origin.is_empty(), |item| {
7215 item.origin.as_deref().unwrap_or("") != origin
7216 })
7217 {
7218 fields.push((field_id.to_owned(), json!({"text":origin})));
7219 }
7220 if let Some((field_id, option_id)) = column {
7221 fields.push((field_id, json!({"singleSelectOptionId":option_id})));
7222 }
7223 let clear = match priority {
7224 Some(PriorityWrite::Select { field, option }) => {
7225 fields.push((field.clone(), json!({"singleSelectOptionId":option})));
7226 None
7227 }
7228 Some(PriorityWrite::Clear { field }) => Some(field.as_str()),
7229 None => None,
7230 };
7231 self.set_item_fields(board_id, item_id, &fields, clear)
7232 .await?;
7233 *origin_landed = true;
7234
7235 // An existing issue closes in the `updateIssue` its write ends with; one created just
7236 // now closes here, once its option is selected.
7237 if existing.is_none()
7238 && content_kind == ContentKind::Issue
7239 && matches!(status_target, Some(StatusTarget::Terminal(_, _)))
7240 {
7241 self.update_content(
7242 ContentKind::Issue,
7243 content_id,
7244 json!({"stateInput":state_input(status_target)}),
7245 )
7246 .await?;
7247 }
7248
7249 if content_kind == ContentKind::Issue {
7250 self.reparent(
7251 existing.and_then(|item| item.parent.clone()),
7252 content_id,
7253 incoming.parent,
7254 )
7255 .await?;
7256 // A document takes part in no dependency graph, so writing one neither reads
7257 // nor changes the issue's own `blockedBy` relationships. Reconciling them
7258 // against the empty list a document write carries would *delete* whatever
7259 // relationships a person had made on that issue, which is a write nobody
7260 // asked for.
7261 if incoming.written.kind() != BoardKind::Document {
7262 let issue = match existing {
7263 Some(item) => Issue::Existing(item.blocked_by.as_deref()),
7264 None => Issue::Created,
7265 };
7266 self.reconcile_blocked_by(content_id, native, issue).await?;
7267 }
7268 }
7269 Ok(())
7270 }
7271
7272 /// Delete one issue, which takes its board item with it.
7273 async fn delete_issue(&self, id: &NativeId) -> Result<(), SourceError> {
7274 let data = self
7275 .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
7276 .await?;
7277 data.pointer("/deleteIssue/repository")
7278 .filter(|value| !value.is_null())
7279 .ok_or_else(|| SourceError::Malformed {
7280 message: "GitHub issue deletion returned no repository".into(),
7281 })?;
7282 self.forget(id)?;
7283 Ok(())
7284 }
7285
7286 /// Remove one item this copy created, so a copy that could not finish leaves the board
7287 /// as it found it.
7288 ///
7289 /// Deleting the issue takes its board item with it, so there is no second mutation to
7290 /// keep in step. An id the board does not hold is not an error: the item is already
7291 /// gone, which is the state this asks for. Which that is, is decided by reading the item
7292 /// by its own id — a listing of the board can still be missing an item it holds, and
7293 /// reading that as *already gone* would leave behind the very item this was asked to
7294 /// take back.
7295 async fn delete_item(&self, id: &NativeId) -> Result<(), SourceError> {
7296 let Some(item) = self.bound_item(id).await? else {
7297 return Ok(());
7298 };
7299 if item.content_kind == ContentKind::DraftIssue {
7300 return Err(SourceError::Refused {
7301 message: format!(
7302 "GitHub item {} is a draft, and this source removes an item by deleting \
7303 its issue; next: remove it from the board by hand",
7304 id.0
7305 ),
7306 });
7307 }
7308 let data = self
7309 .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
7310 .await?;
7311 data.pointer("/deleteIssue/repository")
7312 .filter(|value| !value.is_null())
7313 .ok_or_else(|| SourceError::Malformed {
7314 message: "GitHub issue deletion returned no repository".into(),
7315 })?;
7316 self.forget(id)?;
7317 Ok(())
7318 }
7319
7320 /// The issue a comment call on `task` is about, or `None` when this board holds no such
7321 /// task.
7322 ///
7323 /// Resolved exactly as [`TaskSource::get_task`] resolves it, so the comment verbs and a
7324 /// read of the task cannot disagree about which ids name one: a project or a document of
7325 /// this board is not a task here either.
7326 ///
7327 /// A **draft** is a task with nowhere to keep a comment, because GitHub keeps comments on
7328 /// issues and a draft is not one. It is refused rather than answered with an empty page,
7329 /// which would read as a task nobody has commented on yet.
7330 async fn commented_issue(&self, task: &NativeId) -> Result<Option<NativeId>, SourceError> {
7331 let cached = self.resolved_cache()?.get(task).cloned();
7332 let Some(item) = (match cached {
7333 Some(item) => Some(item),
7334 None => self.item_by_id(task).await?,
7335 })
7336 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
7337 return Ok(None);
7338 };
7339 if item.content_kind == ContentKind::DraftIssue {
7340 return Err(self.draft_has_no_comments(task));
7341 }
7342 Ok(Some(item.id))
7343 }
7344
7345 /// The refusal a comment call on a board draft is answered with: GitHub keeps comments on
7346 /// issues, and a draft is not one.
7347 fn draft_has_no_comments(&self, task: &NativeId) -> SourceError {
7348 SourceError::Refused {
7349 message: format!(
7350 "task {} of source {} is a draft item on the board, and GitHub keeps \
7351 comments on issues alone, so a draft has none to read or write; next: \
7352 convert the draft to an issue on the board, then comment on the issue it \
7353 becomes",
7354 task.0, self.name
7355 ),
7356 }
7357 }
7358
7359 /// One task and a page of its comments, read with [`graphql::ISSUE_DETAIL`] in one
7360 /// request — or `None` when this board holds no task by that id.
7361 ///
7362 /// What `task show` and a comment listing read. A draft is a task with no comments, so it
7363 /// is answered with the draft and the refusal, at the price of the draft's own read.
7364 async fn issue_detail(
7365 &self,
7366 id: &NativeId,
7367 page: &PageRequest,
7368 ) -> Result<Option<TaskDetailRead>, SourceError> {
7369 let after = page.cursor.as_ref().map(|cursor| cursor.0.as_str());
7370 let asked = self
7371 .graphql(
7372 graphql::ISSUE_DETAIL,
7373 json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),"after":after,
7374 "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
7375 "duplicates":true}),
7376 )
7377 .await;
7378 let data = match asked {
7379 Ok(data) => data,
7380 Err(error) if unresolvable_node(&error) => return Ok(None),
7381 Err(error) => return Err(error),
7382 };
7383 // `node` is null for an id that names nothing, and absent only from an answer this
7384 // source cannot read — never the same thing.
7385 let node = data.get("node").ok_or_else(|| SourceError::Malformed {
7386 message: format!("GitHub answered the read of {} with no node", id.0),
7387 })?;
7388 self.detail_of(id, node, true, after).await
7389 }
7390
7391 /// Several tasks, each with the first page of its comments when `comments` is set, read
7392 /// [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] — one answer per id, in
7393 /// order.
7394 ///
7395 /// A batch GitHub refuses because one of its ids resolves to no node at all is read again
7396 /// one item at a time, so that id is answered as missing and the others as themselves; any
7397 /// other refusal is every id of that batch's answer.
7398 async fn issue_details(
7399 &self,
7400 ids: &[NativeId],
7401 comments: Option<&PageRequest>,
7402 ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
7403 let mut read = Vec::with_capacity(ids.len());
7404 for batch in ids.chunks(DETAIL_BATCH) {
7405 match self
7406 .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, comments))
7407 .await
7408 {
7409 Ok(data) => {
7410 for (slot, id) in batch.iter().enumerate() {
7411 // Every alias asked for is answered, null for an id naming nothing;
7412 // one missing is an answer this source cannot read.
7413 let read_one = match data.get(format!("i{slot}")) {
7414 Some(node) => self.detail_of(id, node, comments.is_some(), None).await,
7415 None => Err(SourceError::Malformed {
7416 message: format!(
7417 "GitHub answered a batch read with no item for {}",
7418 id.0
7419 ),
7420 }),
7421 };
7422 read.push(read_one);
7423 }
7424 }
7425 Err(error) if unresolvable_node(&error) => {
7426 for id in batch {
7427 read.push(match comments {
7428 Some(page) => self.issue_detail(id, page).await,
7429 None => self.task_read(id).await,
7430 });
7431 }
7432 }
7433 Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
7434 }
7435 }
7436 read
7437 }
7438
7439 /// One task and nothing of its comments, as [`TaskSource::get_task`] reads it.
7440 async fn task_read(&self, id: &NativeId) -> Result<Option<TaskDetailRead>, SourceError> {
7441 Ok(self.get_task(id).await?.map(|task| TaskDetailRead {
7442 task,
7443 comments: None,
7444 }))
7445 }
7446
7447 /// What one node a detail read reached says: the task this board holds by `id`, with the
7448 /// page of comments the node carries when `commented` — or `None` for a node that is no
7449 /// task of this board.
7450 ///
7451 /// Resolved as [`Self::item_by_id`] resolves an item: a draft is read again as a draft,
7452 /// and an item this process created answers from this process's own record, which a node
7453 /// read taken moments after the write can still be behind.
7454 async fn detail_of(
7455 &self,
7456 id: &NativeId,
7457 node: &Value,
7458 commented: bool,
7459 after: Option<&str>,
7460 ) -> Result<Option<TaskDetailRead>, SourceError> {
7461 if node.is_null() {
7462 return Ok(None);
7463 }
7464 let draft = optional_str(node, "__typename")? == Some("DraftIssue");
7465 // An issue answered under one id is that id's, or the answer is not one this source
7466 // can report: reporting another issue's task and comments under the qualified id asked
7467 // for would be the one wrong answer here. A draft's own read checks the same.
7468 if !draft
7469 && optional_str(node, "__typename")? == Some("Issue")
7470 && required_str(node, "id")? != id.0
7471 {
7472 return Err(SourceError::Malformed {
7473 message: format!(
7474 "GitHub answered the read of {} with issue {}",
7475 id.0,
7476 required_str(node, "id")?
7477 ),
7478 });
7479 }
7480 let item = if draft {
7481 self.draft_by_id(id).await?
7482 } else {
7483 self.resolve_issue(node).await?
7484 };
7485 let Some(item) = item.filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
7486 return Ok(None);
7487 };
7488 let own = self.created()?.iter().find(|own| own.id == *id).cloned();
7489 let task = own.unwrap_or(item).task()?;
7490 let comments = match (commented, draft) {
7491 (false, _) => None,
7492 (true, true) => Some(Err(self.draft_has_no_comments(id))),
7493 (true, false) => Some(comment_page(node, &id.0, after).map(Some)),
7494 };
7495 Ok(Some(TaskDetailRead { task, comments }))
7496 }
7497
7498 /// Whether the comment `comment` is one of `issue`'s own.
7499 ///
7500 /// Read before an edit or a removal is sent, because GitHub's comment mutations take the
7501 /// comment's id and nothing else: a comment id given against the wrong task would
7502 /// otherwise change a comment on some other issue entirely. An id that names nothing, or
7503 /// names something that is not an issue comment, is a comment this task does not have —
7504 /// which is what GitHub refusing to resolve it means too.
7505 async fn comment_is_on(
7506 &self,
7507 issue: &NativeId,
7508 comment: &NativeId,
7509 ) -> Result<bool, SourceError> {
7510 let asked = self
7511 .graphql(graphql::COMMENT_ISSUE, json!({"id":comment.0}))
7512 .await;
7513 let data = match asked {
7514 Ok(data) => data,
7515 Err(error) if unresolvable_node(&error) => return Ok(false),
7516 Err(error) => return Err(error),
7517 };
7518 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
7519 return Ok(false);
7520 };
7521 if optional_str(node, "__typename")? != Some("IssueComment") {
7522 return Ok(false);
7523 }
7524 let on = node.get("issue").ok_or_else(|| SourceError::Malformed {
7525 message: format!("GitHub issue comment {} names no issue", comment.0),
7526 })?;
7527 Ok(required_str(on, "id")? == issue.0)
7528 }
7529
7530 /// Which far ends this item's own `blockedBy` relationship holds, and which it cannot.
7531 async fn partition_edges(
7532 &self,
7533 near_kind: BoardKind,
7534 near_content: ContentKind,
7535 carried: Option<&[Value]>,
7536 depends_on: &[DependencyEdge],
7537 ) -> Result<(Vec<String>, Vec<DependencyEdge>), SourceError> {
7538 let mut native = Vec::new();
7539 let mut fallback = Vec::new();
7540 let far_ends: Vec<(&DependencyEdge, &str, bool, Option<&Value>)> = depends_on
7541 .iter()
7542 .map(|edge| {
7543 let same_source = edge
7544 .to
7545 .source()
7546 .is_none_or(|source| source == self.name.as_str());
7547 // A qualified id's source segment runs to its *first* colon — `GlobalId` and
7548 // `DependencyEndpoint::source` both read it that way — and a native id may hold
7549 // colons of its own, so the far end is everything after that one separator.
7550 // Splitting at the last would truncate `work:urn:task:7` to `7`.
7551 let far_id = if edge.to.is_qualified() {
7552 edge.to
7553 .id()
7554 .split_once(':')
7555 .map_or(edge.to.id(), |(_, native)| native)
7556 } else {
7557 edge.to.id()
7558 };
7559 // One that already blocks the near issue was answered by that issue's own
7560 // read, which carried each of its blockers' kinds — an issue every one — so it
7561 // is not read again.
7562 let blocking = carried.and_then(|nodes| {
7563 nodes
7564 .iter()
7565 .find(|node| node.get("id").and_then(Value::as_str) == Some(far_id))
7566 });
7567 (edge, far_id, same_source, blocking)
7568 })
7569 .collect();
7570 // Every other same-source far end is read by its own id, exactly as the item it is a
7571 // far end of is: whether this board holds it is that read's answer, never a listing's.
7572 // They are read together, [`DETAIL_BATCH`] to a request, rather than one each.
7573 let mut unread: Vec<NativeId> = Vec::new();
7574 for (_, far_id, same_source, blocking) in &far_ends {
7575 let id = NativeId((*far_id).to_owned());
7576 if *same_source && blocking.is_none() && !unread.contains(&id) {
7577 unread.push(id);
7578 }
7579 }
7580 let read: BTreeMap<NativeId, Option<Resolved>> = unread
7581 .iter()
7582 .cloned()
7583 .zip(self.items_by_ids(&unread).await?)
7584 .collect();
7585 for (edge, far_id, same_source, blocking) in far_ends {
7586 let far = match (same_source, blocking) {
7587 (false, _) => None,
7588 (true, Some(node)) => Some(FarEnd {
7589 kind: if required_str(node, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
7590 BoardKind::Document
7591 } else {
7592 BoardKind::Work(related_kind(node)?)
7593 },
7594 content_kind: ContentKind::Issue,
7595 }),
7596 (true, None) => {
7597 let read = read
7598 .get(&NativeId(far_id.to_owned()))
7599 .cloned()
7600 .flatten()
7601 .ok_or_else(|| SourceError::Refused {
7602 message: format!("GitHub dependency item {far_id} was not found"),
7603 })?;
7604 Some(FarEnd {
7605 kind: read.kind,
7606 content_kind: read.content_kind,
7607 })
7608 }
7609 };
7610 let far = far.as_ref();
7611 // The caller says which kind the far end is, and this board holds the far end
7612 // itself, so a disagreement is settled here rather than stored: recorded, the
7613 // wrong kind would read back as a cross-level edge that never existed; written
7614 // natively, it would name a relationship of a different level than the caller
7615 // asked for.
7616 //
7617 // A far end this board holds as a *document* fails the same comparison and is
7618 // refused by the same sentence: `ItemKind` has no document variant because
7619 // nothing may point at one, so no caller can name it correctly and the refusal
7620 // is the only honest answer.
7621 if let Some(disagreeing) = far.filter(|far| far.kind != BoardKind::Work(edge.to.kind)) {
7622 return Err(SourceError::Refused {
7623 message: format!(
7624 "GitHub dependency item {far_id} is a {} of this board, and this item \
7625 names it as a {}; record the kind it is",
7626 disagreeing.kind.describes(),
7627 edge.to.kind.marker()
7628 ),
7629 });
7630 }
7631 // A draft has neither `blockedBy` nor `blocking`, so no edge of one is native
7632 // however the far end is spelled — and one classified native here would be
7633 // written nowhere at all, because a draft's native reconciliation never runs.
7634 let native_here = near_content == ContentKind::Issue
7635 && far.is_some_and(|far| {
7636 far.content_kind == ContentKind::Issue
7637 && BoardKind::Work(edge.to.kind) == near_kind
7638 });
7639 if native_here {
7640 native.push(far_id.to_owned());
7641 } else {
7642 fallback.push(edge.clone());
7643 }
7644 }
7645 Ok((native, fallback))
7646 }
7647
7648 async fn update_existing(
7649 &self,
7650 item: &Resolved,
7651 incoming: &Incoming<'_>,
7652 body: &Option<String>,
7653 status_target: Option<&StatusTarget>,
7654 ) -> Result<(), SourceError> {
7655 let title = incoming.written_title();
7656 // A terminal status closes the issue here, in the same mutation as its body: its board
7657 // option was selected before this, so a close never lands on an item whose board cannot
7658 // show it.
7659 let fields = match item.content_kind {
7660 ContentKind::DraftIssue => json!({"title":title,"body":body}),
7661 ContentKind::Issue => json!({"title":title,"body":body,
7662 "stateInput":state_input(status_target)}),
7663 };
7664 self.update_content(item.content_kind, &item.id, fields)
7665 .await
7666 }
7667
7668 /// Update one board item's content with exactly `fields` beside its id, through the
7669 /// mutation its kind takes: `updateIssue` for an issue, `updateProjectV2DraftIssue` for
7670 /// a draft.
7671 ///
7672 /// Every input field either mutation leaves out is a field GitHub leaves as it is, which
7673 /// is what lets a narrow write carry the one thing it changes and nothing else.
7674 async fn update_content(
7675 &self,
7676 kind: ContentKind,
7677 id: &NativeId,
7678 fields: Value,
7679 ) -> Result<(), SourceError> {
7680 let (operation, id_key, pointer) = match kind {
7681 ContentKind::DraftIssue => (
7682 graphql::UPDATE_DRAFT,
7683 "draftIssueId",
7684 "/updateProjectV2DraftIssue/draftIssue",
7685 ),
7686 ContentKind::Issue => (graphql::UPDATE_ISSUE, "id", "/updateIssue/issue"),
7687 };
7688 let mut input = fields;
7689 input[id_key] = json!(id.0);
7690 let data = self.graphql(operation, json!({"input":input})).await?;
7691 let returned = data
7692 .pointer(pointer)
7693 .ok_or_else(|| SourceError::Malformed {
7694 message: "GitHub item update returned no item".into(),
7695 })?;
7696 if required_str(returned, "id")? != id.0 {
7697 return Err(SourceError::Malformed {
7698 message: "GitHub item update returned the wrong item".into(),
7699 });
7700 }
7701 Ok(())
7702 }
7703
7704 /// Creates one issue, files it on the board, and reports what a read of it would say:
7705 /// its content id, its board item id, and the web address GitHub gave it.
7706 ///
7707 /// Two calls rather than one: `createIssue` answers with an issue that is on no board,
7708 /// and `addProjectV2ItemById` is what puts it there. Filing it at creation through
7709 /// `CreateIssueInput.projectV2Ids` was tried and is not done: GitHub answered with no
7710 /// board item, and the `addProjectV2ItemById` that then had to follow was refused
7711 /// "Content already exists in this project". A terminal status is not written here:
7712 /// `finish_write` selects its option first and closes the issue after, so a close never
7713 /// lands on an item whose board cannot show it.
7714 ///
7715 /// The address and the number come back here because this is the only place either is
7716 /// known before GitHub's own board read catches up — an item this run created answers
7717 /// the reads that follow it out of the record below, and one remembered without them
7718 /// would report no location and no key for the rest of the run.
7719 async fn create_and_file_issue(
7720 &self,
7721 board_id: &str,
7722 repository: &RepositoryTarget,
7723 incoming: &Incoming<'_>,
7724 body: &Option<String>,
7725 ) -> Result<Landed, SourceError> {
7726 let repository_id = self.repository_id(repository, incoming).await?;
7727 let data = self
7728 .graphql(
7729 graphql::CREATE_ISSUE,
7730 json!({"input":{
7731 "repositoryId":repository_id,"title":incoming.written_title(),"body":body
7732 }}),
7733 )
7734 .await?;
7735 let created = data
7736 .pointer("/createIssue/issue")
7737 .filter(|value| !value.is_null())
7738 .ok_or_else(|| SourceError::Malformed {
7739 message: "GitHub issue creation returned no issue".into(),
7740 })?;
7741 let content_id = NativeId(required_str(created, "id")?.to_owned());
7742 // Optional although GitHub's schema makes it non-null: the issue exists by now, so
7743 // a response without it is not worth failing a landed write over — the item simply
7744 // reports no location until the board read catches up, which is what it did before.
7745 let url = optional_str(created, "url")?.map(str::to_owned);
7746 // The issue exists from here on, so an unreadable number and a refused board
7747 // filing below each try, best effort, to take it back: an issue in the repository
7748 // that is on no board is an item nobody asked for and nothing here would find again.
7749 //
7750 // Its number is optional on the same terms its address is — a landed write is not
7751 // worth failing over a member that came back missing, and such an item reports no
7752 // handle until a board read catches up. A number that is *present* and is not an
7753 // unsigned integer is still a response this source cannot read.
7754 let number = match created_issue_number(created) {
7755 Ok(number) => number,
7756 Err(error) => {
7757 let _ = self.delete_issue(&content_id).await;
7758 return Err(error);
7759 }
7760 };
7761 let added = match self
7762 .graphql(
7763 graphql::ADD_TO_BOARD,
7764 json!({"input":{"projectId":board_id,"contentId":content_id.0}}),
7765 )
7766 .await
7767 {
7768 Ok(added) => added,
7769 Err(error) => {
7770 let _ = self.delete_issue(&content_id).await;
7771 return Err(error);
7772 }
7773 };
7774 let item = added
7775 .pointer("/addProjectV2ItemById/item")
7776 .filter(|value| !value.is_null())
7777 .ok_or_else(|| SourceError::Malformed {
7778 message: "GitHub board addition returned no project item".into(),
7779 })?;
7780 Ok(Landed {
7781 content_id,
7782 item_id: required_str(item, "id")?.to_owned(),
7783 url,
7784 number,
7785 })
7786 }
7787
7788 /// Move one issue under the project it now belongs to, or out of the one it left.
7789 async fn reparent(
7790 &self,
7791 held: Option<NativeId>,
7792 child: &NativeId,
7793 wanted: Option<&NativeId>,
7794 ) -> Result<(), SourceError> {
7795 if held.as_ref() == wanted {
7796 return Ok(());
7797 }
7798 if let Some(held) = &held {
7799 self.sub_issue(graphql::REMOVE_SUB_ISSUE, held, child, "removeSubIssue")
7800 .await?;
7801 }
7802 if let Some(wanted) = wanted {
7803 self.sub_issue(graphql::ADD_SUB_ISSUE, wanted, child, "addSubIssue")
7804 .await?;
7805 }
7806 Ok(())
7807 }
7808
7809 async fn sub_issue(
7810 &self,
7811 operation: &str,
7812 parent: &NativeId,
7813 child: &NativeId,
7814 root: &str,
7815 ) -> Result<(), SourceError> {
7816 let data = self
7817 .graphql(
7818 operation,
7819 json!({"input":{"issueId":parent.0,"subIssueId":child.0}}),
7820 )
7821 .await?;
7822 let issue =
7823 data.pointer(&format!("/{root}/issue"))
7824 .ok_or_else(|| SourceError::Malformed {
7825 message: "GitHub sub-issue update returned no issue".into(),
7826 })?;
7827 let sub =
7828 data.pointer(&format!("/{root}/subIssue"))
7829 .ok_or_else(|| SourceError::Malformed {
7830 message: "GitHub sub-issue update returned no sub-issue".into(),
7831 })?;
7832 if required_str(issue, "id")? != parent.0 || required_str(sub, "id")? != child.0 {
7833 return Err(SourceError::Malformed {
7834 message: "GitHub sub-issue update returned the wrong issues".into(),
7835 });
7836 }
7837 Ok(())
7838 }
7839
7840 /// Bring one issue's `blockedBy` to exactly `native`, sending only the difference, and say
7841 /// whether there was one.
7842 ///
7843 /// An issue [`Issue::Created`] by this very write is blocked by nothing yet, so its
7844 /// relationships are not read: there is nothing a read of them could find.
7845 async fn reconcile_blocked_by(
7846 &self,
7847 content_id: &NativeId,
7848 native: &[String],
7849 issue: Issue<'_>,
7850 ) -> Result<bool, SourceError> {
7851 let current = match issue {
7852 Issue::Created => Vec::new(),
7853 Issue::Existing(Some(held)) => held
7854 .iter()
7855 .map(|far| required_str(far, "id").map(str::to_owned))
7856 .collect::<Result<Vec<_>, _>>()?,
7857 Issue::Existing(None) => self.native_dependency_ids(content_id).await?,
7858 };
7859 let mut changed = false;
7860 for (operation, far_id) in current
7861 .iter()
7862 .filter(|id| !native.contains(id))
7863 .map(|id| (graphql::REMOVE_BLOCKED_BY, id))
7864 .chain(
7865 native
7866 .iter()
7867 .filter(|id| !current.contains(id))
7868 .map(|id| (graphql::ADD_BLOCKED_BY, id)),
7869 )
7870 {
7871 let data = self
7872 .graphql(
7873 operation,
7874 json!({"input":{"issueId":content_id.0,"blockingIssueId":far_id}}),
7875 )
7876 .await?;
7877 let root = if operation == graphql::ADD_BLOCKED_BY {
7878 "addBlockedBy"
7879 } else {
7880 "removeBlockedBy"
7881 };
7882 let issue =
7883 data.pointer(&format!("/{root}/issue"))
7884 .ok_or_else(|| SourceError::Malformed {
7885 message: "GitHub dependency update returned no issue".into(),
7886 })?;
7887 let blocker = data
7888 .pointer(&format!("/{root}/blockingIssue"))
7889 .ok_or_else(|| SourceError::Malformed {
7890 message: "GitHub dependency update returned no blocking issue".into(),
7891 })?;
7892 if required_str(issue, "id")? != content_id.0 || required_str(blocker, "id")? != far_id
7893 {
7894 return Err(SourceError::Malformed {
7895 message: "GitHub dependency update returned the wrong issues".into(),
7896 });
7897 }
7898 changed = true;
7899 }
7900 Ok(changed)
7901 }
7902}
7903
7904/// What a write needs to know of one far end it names: which kind of item it is, and whether
7905/// it is an issue a native relationship can name.
7906struct FarEnd {
7907 kind: BoardKind,
7908 content_kind: ContentKind,
7909}
7910
7911/// Whether the issue one write reconciles was created by that write or was already there.
7912#[derive(Clone, Copy, PartialEq, Eq)]
7913enum Issue<'a> {
7914 /// Created by this write, so it holds no relationships yet.
7915 Created,
7916 /// On the board before this write, holding whatever relationships it holds — the far
7917 /// ends of its whole `blockedBy`, when the read that reached it carried them.
7918 Existing(Option<&'a [Value]>),
7919}
7920
7921/// What resolving one node id reached; see [`GitHubProjectsSource::reach`].
7922enum Reached {
7923 /// An issue this board holds, resolved into everything this source reports about it.
7924 Held(Box<Resolved>),
7925 /// Nothing this board holds: no such node, or a node on some other board.
7926 Nothing,
7927 /// A board draft, which [`graphql::ISSUE`] reaches and reads nothing of, so it is read
7928 /// again by [`GitHubProjectsSource::draft_by_id`].
7929 Draft,
7930}
7931
7932/// What GitHub says when a string is not a node id it can resolve.
7933///
7934/// Matched because it is the ordinary answer to a project selector naming a project by its
7935/// *name*, and reporting that as a failure would make naming one impossible. It is read
7936/// off the refusal GitHub sent, never guessed from the shape of the string: this source
7937/// does not define the syntax of a GitHub node id and would be wrong about it.
7938const UNRESOLVABLE_NODE: &str = "could not resolve to a node";
7939
7940/// `error` with `note` added to the end of what it says, its kind and every other member
7941/// unchanged — so a caller still branches on the failure that happened, and reads beside it
7942/// what that failure left behind.
7943fn noting(error: SourceError, note: &str) -> SourceError {
7944 match error {
7945 SourceError::Config { message } => SourceError::Config {
7946 message: message + note,
7947 },
7948 SourceError::Auth { message } => SourceError::Auth {
7949 message: message + note,
7950 },
7951 SourceError::Refused { message } => SourceError::Refused {
7952 message: message + note,
7953 },
7954 SourceError::RateLimited {
7955 retry_after_seconds,
7956 message,
7957 } => SourceError::RateLimited {
7958 retry_after_seconds,
7959 message: Some(message.unwrap_or_default() + note),
7960 },
7961 SourceError::Unavailable { message } => SourceError::Unavailable {
7962 message: message + note,
7963 },
7964 SourceError::Malformed { message } => SourceError::Malformed {
7965 message: message + note,
7966 },
7967 }
7968}
7969
7970/// The variables of one [`graphql::ISSUE_DETAILS`] request over `batch` — at most
7971/// [`DETAIL_BATCH`] ids — each item with the first page of its comments when `comments` asks
7972/// for them.
7973///
7974/// The document is fixed-size, so a slot `batch` has no id for is bound to its last id, which
7975/// is read again at no added price.
7976fn detail_batch(batch: &[NativeId], comments: Option<&PageRequest>) -> Value {
7977 let mut variables = serde_json::Map::new();
7978 for slot in 0..DETAIL_BATCH {
7979 let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
7980 variables.insert(format!("id{slot}"), json!(id));
7981 }
7982 variables.insert(
7983 "first".to_owned(),
7984 json!(comments.map_or(MAX_PAGE_SIZE, |page| page.limit.min(MAX_PAGE_SIZE))),
7985 );
7986 variables.insert("comments".to_owned(), json!(comments.is_some()));
7987 variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
7988 variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
7989 variables.insert("duplicates".to_owned(), json!(true));
7990 Value::Object(variables)
7991}
7992
7993/// Whether this refusal is GitHub saying the id names no node at all.
7994fn unresolvable_node(error: &SourceError) -> bool {
7995 matches!(error, SourceError::Refused { message }
7996 if message.to_ascii_lowercase().contains(UNRESOLVABLE_NODE))
7997}
7998
7999/// One project name, as a search qualifier which filters on it at the server.
8000///
8001/// Quoted so the whole title is one phrase rather than a bag of words, with the two
8002/// characters GitHub's own quoting grammar gives a meaning inside a quoted phrase escaped
8003/// the way it documents. A title matched here is still compared for equality afterwards:
8004/// the qualifier narrows what the server sends, and this source decides what it names.
8005fn title_qualifier(name: &str) -> String {
8006 format!("in:title {}", quoted(name))
8007}
8008
8009/// `value` as one quoted phrase of a GitHub search or a board filter, with the two
8010/// characters GitHub's quoting grammar gives a meaning inside a quoted phrase escaped the way
8011/// it documents — so a value holding a qualifier's spelling is searched for rather than
8012/// obeyed.
8013fn quoted(value: &str) -> String {
8014 let escaped = value.replace('\\', "\\\\").replace('"', "\\\"");
8015 format!("\"{escaped}\"")
8016}
8017
8018/// The search qualifier for the issues updated at or after `since`.
8019///
8020/// Written to the second, rounded down, which can only widen what the search returns.
8021fn updated_qualifier(since: DateTime<Utc>) -> String {
8022 format!("updated:>={}", since.format("%Y-%m-%dT%H:%M:%S+00:00"))
8023}
8024
8025/// The search terms that narrow a board-scoped issue search to a task query's text and
8026/// metadata predicates, or `None` when it carries neither.
8027///
8028/// The text is one quoted phrase, searched `in:title`, `in:body` or both as its fields say,
8029/// and each metadata value is one more quoted phrase, which GitHub finds in the body because
8030/// its index covers the metadata comment the value is stored in. GitHub ANDs the phrases and
8031/// matches each in any field the `in:` qualifier names, so a query naming a title search and
8032/// a metadata value searches both fields for both — wider than asked, never narrower, and
8033/// every candidate is confirmed in process afterwards.
8034///
8035/// **This narrows a text search, and that is this source's declared semantics.** GitHub
8036/// matches whole tokens where a substring rule would match inside a word, so an item holding
8037/// the text only inside a longer word is not returned. A text of nothing but whitespace
8038/// matches every item, so it narrows nothing and is not sent.
8039fn narrowing_qualifiers(query: &TaskQuery) -> Option<String> {
8040 let text = query
8041 .text
8042 .as_ref()
8043 .filter(|text| !text.terms.trim().is_empty());
8044 if text.is_none() && query.metadata.is_empty() {
8045 return None;
8046 }
8047 let (title, body) = match text.map(|text| text.fields) {
8048 None => (false, true),
8049 Some(TextFields::Title) => (true, !query.metadata.is_empty()),
8050 Some(TextFields::Content) => (false, true),
8051 Some(TextFields::TitleOrContent) => (true, true),
8052 };
8053 let fields = match (title, body) {
8054 (true, true) => "in:title,body",
8055 (true, false) => "in:title",
8056 _ => "in:body",
8057 };
8058 let phrases = text
8059 .map(|text| text.terms.clone())
8060 .into_iter()
8061 .chain(
8062 query
8063 .metadata
8064 .iter()
8065 .map(|wanted| as_stored(wanted.value())),
8066 )
8067 .map(|phrase| quoted(&phrase))
8068 .collect::<Vec<_>>();
8069 Some(format!("{fields} {}", phrases.join(" ")))
8070}
8071
8072/// The search terms that narrow a board-scoped issue search to a project or document query's
8073/// text, or `None` when it has none or a blank one: the phrase, in the fields, a task query
8074/// carrying that text alone is sent as by [`narrowing_qualifiers`].
8075fn text_qualifiers(text: Option<&TextQuery>) -> Option<String> {
8076 narrowing_qualifiers(&TaskQuery {
8077 text: text.cloned(),
8078 ..TaskQuery::default()
8079 })
8080}
8081
8082/// Refuses a project or document query's text GitHub's issue search cannot find, before
8083/// anything is asked of GitHub, on exactly the terms [`refuse_unsearchable`] refuses a task
8084/// query's.
8085fn refuse_unsearchable_text(text: Option<&TextQuery>) -> Result<(), SourceError> {
8086 refuse_unsearchable(&TaskQuery {
8087 text: text.cloned(),
8088 ..TaskQuery::default()
8089 })
8090}
8091
8092/// Refuses a task query naming a text or a metadata value GitHub's issue search cannot find,
8093/// before anything is asked of GitHub.
8094///
8095/// GitHub's index holds words, so a phrase with no letter or digit names none to find, and no
8096/// bounded query answers it: sent, GitHub's answer to it is nothing this source may rely on;
8097/// left out, the search is every issue of the board. So this source says it cannot answer
8098/// rather than reading the board or answering nothing. A blank text is not refused: it narrows
8099/// nothing GitHub could search for, and keeps the board read it always had.
8100fn refuse_unsearchable(query: &TaskQuery) -> Result<(), SourceError> {
8101 const WHY: &str = "GitHub's issue search indexes words, so it cannot answer a value with no \
8102 letter or digit with a bounded query";
8103 if let Some(text) = &query.text
8104 && !text.terms.trim().is_empty()
8105 && !has_words(&text.terms)
8106 {
8107 return Err(SourceError::Refused {
8108 message: format!(
8109 "cannot search for the text {:?}: {WHY}; search for a text holding a letter or a digit",
8110 text.terms
8111 ),
8112 });
8113 }
8114 if let Some(wanted) = query
8115 .metadata
8116 .iter()
8117 .find(|wanted| !has_words(wanted.value()))
8118 {
8119 return Err(SourceError::Refused {
8120 message: format!(
8121 "cannot filter by the metadata value {:?} at {:?}: {WHY}; filter by a value holding a letter or a digit",
8122 wanted.value(),
8123 std::iter::once(wanted.key())
8124 .chain(wanted.path().iter().map(String::as_str))
8125 .collect::<Vec<_>>()
8126 .join("/"),
8127 ),
8128 });
8129 }
8130 Ok(())
8131}
8132
8133/// Whether GitHub's index could hold a word of `phrase`: whether it has a letter or a digit.
8134fn has_words(phrase: &str) -> bool {
8135 phrase.chars().any(char::is_alphanumeric)
8136}
8137
8138/// `value` spelled the way the metadata slot stores it: as the inside of its JSON string.
8139///
8140/// What GitHub indexes is the slot's JSON text, so a value holding a character JSON escapes —
8141/// a newline, a tab, a quote — is found by the escape the body holds and not by the character,
8142/// which GitHub's word match would read as different words.
8143fn as_stored(value: &str) -> String {
8144 let encoded = Value::String(value.to_owned()).to_string();
8145 encoded[1..encoded.len() - 1].to_owned()
8146}
8147
8148/// The one narrower question a task query carrying a text, metadata or origin predicate is
8149/// sent as.
8150enum Narrowing {
8151 /// Every carrier of this origin: [`graphql::ORIGIN_LOOKUP`].
8152 Origin(String),
8153 /// The board-scoped issue search narrowed by these qualifiers.
8154 Search(String),
8155}
8156
8157impl Narrowing {
8158 /// What this question is remembered under for the length of one command.
8159 fn key(&self) -> String {
8160 match self {
8161 Self::Origin(origin) => format!("origin {origin}"),
8162 Self::Search(also) => format!("search {also}"),
8163 }
8164 }
8165}
8166
8167/// Where one connection of [`graphql::ORIGIN_LOOKUP`] resumes.
8168enum Resumed {
8169 /// It reported another page, which starts after this cursor.
8170 More(String),
8171 /// It has ended. Sending this cursor again — the page's own end when it had one, and
8172 /// otherwise the cursor it was reached from — answers an empty page, so the one document
8173 /// can go on walking the other connection.
8174 Ended(Option<String>),
8175}
8176
8177impl Resumed {
8178 /// Whether the connection has another page.
8179 const fn has_more(&self) -> bool {
8180 matches!(self, Self::More(_))
8181 }
8182
8183 /// The cursor to send this connection next.
8184 fn cursor(self) -> Option<String> {
8185 match self {
8186 Self::More(next) => Some(next),
8187 Self::Ended(last) => last,
8188 }
8189 }
8190}
8191
8192/// Where `connection`, reached from `after`, resumes — refused when it reports another page
8193/// with no cursor to it, or from a cursor that does not advance.
8194fn resumed(connection: &Value, after: Option<&str>) -> Result<Resumed, SourceError> {
8195 let info = connection
8196 .get("pageInfo")
8197 .ok_or_else(|| SourceError::Malformed {
8198 message: "GitHub connection has no pageInfo".into(),
8199 })?;
8200 let end = optional_str(info, "endCursor")?;
8201 if required_bool(info, "hasNextPage")? {
8202 let next = end.ok_or_else(|| SourceError::Malformed {
8203 message: "GitHub connection reports another page and no endCursor".into(),
8204 })?;
8205 validate_cursor_progress(after, next)?;
8206 return Ok(Resumed::More(next.to_owned()));
8207 }
8208 Ok(Resumed::Ended(
8209 end.map(str::to_owned).or_else(|| after.map(str::to_owned)),
8210 ))
8211}
8212
8213/// The board, and every item on it this source reports.
8214#[derive(Clone)]
8215struct Board {
8216 id: String,
8217 fields: Value,
8218 items: Vec<Resolved>,
8219}
8220
8221/// What a write needs of the board and nothing more: its node id and its field
8222/// definitions, in the shape a read of the board's own `fields` gives them.
8223///
8224/// Deliberately no items. A write decides which item it writes, which parent it files
8225/// under and which far ends it names by reading each of them by its own id; this is the
8226/// half of the board those reads cannot carry, and holding no item is what keeps it from
8227/// ever being asked whether an item is there.
8228#[derive(Clone)]
8229struct BoardFields {
8230 id: BoardId,
8231 fields: Value,
8232}
8233
8234/// A board's node id: what a field write and `addProjectV2ItemById` address.
8235///
8236/// Never blank, because a blank one addresses no board — so an id GitHub answers blank is
8237/// refused where it is read, and one an item names blank is read as not named at all.
8238#[derive(Clone)]
8239struct BoardId(String);
8240
8241/// Where one write left its item, for the record the rest of the command reads it out of.
8242///
8243/// A named record rather than a tuple because the update arm and the create arm each fill
8244/// all four, and two `Option`s of different meaning side by side in a tuple are two
8245/// positions a reader has to count.
8246struct Landed {
8247 /// The issue's own node id, which is the [`NativeId`] this source reports.
8248 content_id: NativeId,
8249 /// The board item's id, which is what a field write addresses.
8250 // 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.
8251 item_id: String,
8252 /// The web address GitHub gave the issue, when it gave one.
8253 // 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.
8254 url: Option<String>,
8255 /// The issue's number on its repository, when GitHub reported one.
8256 number: Option<u64>,
8257}
8258
8259impl BoardId {
8260 fn parse(id: &str) -> Result<Self, SourceError> {
8261 if id.trim().is_empty() {
8262 return Err(SourceError::Malformed {
8263 message: "GitHub named a board with a blank node id".into(),
8264 });
8265 }
8266 Ok(Self(id.to_owned()))
8267 }
8268
8269 fn as_str(&self) -> &str {
8270 &self.0
8271 }
8272}
8273
8274impl Board {
8275 fn field<'a>(fields: &'a Value, name: &str) -> Result<Option<&'a Value>, SourceError> {
8276 complete_connection(fields, "project fields", NESTED_PAGE_SIZE)?;
8277 let nodes = fields
8278 .get("nodes")
8279 .and_then(Value::as_array)
8280 .ok_or_else(|| SourceError::Malformed {
8281 message: "GitHub project fields.nodes is not an array".into(),
8282 })?;
8283 Ok(nodes
8284 .iter()
8285 .find(|field| field.get("name").and_then(Value::as_str) == Some(name)))
8286 }
8287}
8288
8289/// One board item, resolved into everything this source reports about it.
8290#[derive(Clone)]
8291struct Resolved {
8292 item_id: String,
8293 id: NativeId,
8294 content_kind: ContentKind,
8295 kind: BoardKind,
8296 title: String,
8297 body: Option<String>,
8298 /// The body exactly as GitHub holds it, metadata slot and all, which is what a write
8299 /// that changes the slot alone has to keep byte for byte outside it.
8300 raw_body: Option<String>,
8301 status: Status,
8302 /// The name of the board `Status` option this item sits in, as the board spells it.
8303 option: Option<String>,
8304 /// What its `Priority` field says, read through this instance's mapping.
8305 priority: HeldPriority,
8306 /// Whether this item's issue is closed. A draft has no such state and is never closed.
8307 closed: bool,
8308 /// The tasks this one delivers, read out of its slot. Empty for anything not a task.
8309 delivers: Vec<TaskRef>,
8310 /// Every task that delivers this one, read out of its slot. Empty for anything not a
8311 /// task.
8312 delivered_by: Vec<TaskRef>,
8313 labels: Vec<Label>,
8314 parent: Option<NativeId>,
8315 // 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.
8316 origin: Option<String>,
8317 /// The issue's own number on its repository, as GitHub reports it.
8318 ///
8319 /// `None` in exactly two cases: a draft, which has no number at all — `DraftIssue`
8320 /// declares none, and a draft is not filed in a repository to be numbered by one — and
8321 /// an issue this run created whose creating mutation answered without one, which is a
8322 /// response GitHub's own schema says cannot happen and which a landed write is not
8323 /// worth failing over. An `Issue` read off the board always has one.
8324 number: Option<u64>,
8325 url: Option<String>,
8326 created_at: Option<DateTime<Utc>>,
8327 updated_at: Option<DateTime<Utc>>,
8328 own_repository: Option<Repository>,
8329 repositories: Vec<Repository>,
8330 slot: BTreeMap<String, Value>,
8331 /// The node id of the board this item sits on, when the read that reached it said.
8332 board_id: Option<String>,
8333 /// The definition of every board field this item holds a value of, in the shape a read
8334 /// of the board's own `fields` gives one.
8335 ///
8336 /// Only the fields this item has a value in: a field it holds nothing of is not here,
8337 /// which says nothing about whether the board has it.
8338 fields: Vec<Value>,
8339 /// Every field the board this item sits on defines, as its own read of the board's
8340 /// `fields` gives them — when the read that reached the item carried them, which a read
8341 /// of it by its own id does. What a write of it needs of the board, then, needs no read
8342 /// of the board.
8343 board_fields: Option<Value>,
8344 /// The far ends of this issue's whole `blockedBy` connection, each as a dependency read
8345 /// selects one — when the read that reached it carried the connection to its end, which a
8346 /// read of it by its own id does for any issue blocked by no more than a page. What a
8347 /// write reconciles that relationship against, and what a read of its forward edges in
8348 /// the same command answers with.
8349 blocked_by: Option<Vec<Value>>,
8350}
8351
8352impl Resolved {
8353 /// The board this item's own read names it on, when that read named one this source can
8354 /// address.
8355 fn named_board(&self) -> Option<BoardId> {
8356 self.board_id
8357 .as_deref()
8358 .and_then(|id| BoardId::parse(id).ok())
8359 }
8360
8361 /// The board's id and every field it defines, when the read that reached this item
8362 /// carried both — which a read of it by its own id does.
8363 fn carried_board(&self) -> Option<BoardFields> {
8364 Some(BoardFields {
8365 id: self.named_board()?,
8366 fields: self.board_fields.clone()?,
8367 })
8368 }
8369
8370 /// Whether this item holds a value of the board field called `name`, and so carries
8371 /// that field's definition. `false` says nothing about whether the board has the field.
8372 fn defines(&self, name: &str) -> bool {
8373 self.fields
8374 .iter()
8375 .any(|field| field.get("name").and_then(Value::as_str) == Some(name))
8376 }
8377
8378 /// The metadata a caller sees: their own keys, plus the copy origin this source keeps
8379 /// in a field of its own, and none of the five keys that are only an encoding.
8380 ///
8381 /// The two delivery keys are left out for every kind, not only for a task: they are
8382 /// the encoding of [`Task::delivers`] and [`Task::delivered_by`], and a project or a
8383 /// document carrying one holds nothing a caller's own metadata could mean by it.
8384 fn metadata(&self) -> BTreeMap<String, Value> {
8385 let mut metadata = self.slot.clone();
8386 metadata.remove(Repository::METADATA_KEY);
8387 metadata.remove(DependencyEdge::RECORDED_KEY);
8388 metadata.remove(ItemKind::METADATA_KEY);
8389 metadata.remove(TaskRef::DELIVERS_KEY);
8390 metadata.remove(TaskRef::DELIVERED_BY_KEY);
8391 // The board field is the origin, and the body's copy of it is only a mirror for the
8392 // issue search to find: an item whose field holds none has none, whatever its body
8393 // says, so no reader ever sees two answers.
8394 metadata.remove(ORIGIN_KEY);
8395 if let Some(origin) = &self.origin {
8396 metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin.clone()));
8397 }
8398 metadata
8399 }
8400
8401 /// Where this item is, as a link a reader can open.
8402 ///
8403 /// A board is a hosted place and every issue on it has a web address, so that address
8404 /// is what "where is this?" means here — and [`Location::Url`] is what says which kind
8405 /// of place it is, so a reader knows to open it rather than to read a file out. It
8406 /// does not replace or derive from `url`: the field goes on reporting exactly what it
8407 /// reported before, and this says what that address *is*.
8408 ///
8409 /// An item GitHub gave no `url` for — a draft has none — reports no location at all
8410 /// rather than a third variant, which is the contract's "the source did not say". An
8411 /// issue this run created is not one of those: its address comes back from the
8412 /// creating mutation, so it is somewhere a reader can open from the moment it exists
8413 /// rather than from whenever the board read catches up.
8414 fn location(&self) -> Option<Location> {
8415 self.url.clone().map(Location::Url)
8416 }
8417
8418 /// The short handle this board's backend shows people for a task: the issue's number
8419 /// alone, as a decimal string.
8420 ///
8421 /// The number alone rather than `owner/repo#1043`, because that is the contract's
8422 /// value for this backend. A draft has no number and so no handle, which is the
8423 /// contract's *absent* rather than a handle of some other shape — and the native
8424 /// [`Task::id`] here is the issue's GraphQL node id, which this neither replaces nor
8425 /// derives from.
8426 fn key(&self) -> Option<String> {
8427 self.number.map(|number| number.to_string())
8428 }
8429
8430 /// Whether its `Priority` field holds a value at all, mapped or not.
8431 fn holds_priority(&self) -> bool {
8432 self.priority != HeldPriority::Read(Priority::None)
8433 }
8434
8435 /// The task this item is.
8436 ///
8437 /// Fails for an item whose `Priority` field holds an option the mapping does not name:
8438 /// reading that as a level would be a guess, and reading it as `none` would let the next
8439 /// copy clear a priority a person set.
8440 fn task(&self) -> Result<Task, SourceError> {
8441 let priority = match &self.priority {
8442 HeldPriority::Read(priority) => *priority,
8443 HeldPriority::Unmapped(option) => {
8444 return Err(SourceError::Malformed {
8445 message: format!(
8446 "task {}{} sits in the board {PRIORITY_FIELD} option {option:?}, which \
8447 this source's priority_mapping does not name, so its priority cannot be \
8448 read; next: name {option:?} under priority_mapping, or move the item to \
8449 a mapped option",
8450 self.id,
8451 self.number
8452 .map(|number| format!(" (#{number})"))
8453 .unwrap_or_default()
8454 ),
8455 });
8456 }
8457 };
8458 Ok(Task {
8459 id: self.id.clone(),
8460 key: self.key(),
8461 title: self.title.clone(),
8462 content: self.body.clone(),
8463 status: self.status.clone(),
8464 priority,
8465 labels: self.labels.clone(),
8466 project: self.parent.clone(),
8467 url: self.url.clone(),
8468 location: self.location(),
8469 created_at: self.created_at,
8470 updated_at: self.updated_at,
8471 metadata: self.metadata(),
8472 repositories: self.repositories.clone(),
8473 delivers: self.delivers.clone(),
8474 delivered_by: self.delivered_by.clone(),
8475 })
8476 }
8477
8478 fn project(&self) -> Project {
8479 Project {
8480 id: self.id.clone(),
8481 title: self.title.clone(),
8482 content: self.body.clone(),
8483 status: self.status.clone(),
8484 labels: self.labels.clone(),
8485 url: self.url.clone(),
8486 location: self.location(),
8487 created_at: self.created_at,
8488 updated_at: self.updated_at,
8489 metadata: self.metadata(),
8490 repositories: self.repositories.clone(),
8491 }
8492 }
8493
8494 /// The same issue as a document: the project it is filed under, and no status and no
8495 /// dependencies, because a document is not work.
8496 fn document(&self) -> Document {
8497 Document {
8498 id: self.id.clone(),
8499 title: self.title.clone(),
8500 content: self.body.clone(),
8501 project: self.parent.clone(),
8502 labels: self.labels.clone(),
8503 url: self.url.clone(),
8504 location: self.location(),
8505 created_at: self.created_at,
8506 updated_at: self.updated_at,
8507 metadata: self.metadata(),
8508 repositories: self.repositories.clone(),
8509 }
8510 }
8511}
8512
8513/// Where one targeted update moves an item's status, and which of its two halves move.
8514struct StatusMove {
8515 /// The board the item's `Status` field is on.
8516 board: BoardId,
8517 /// The `Status` field's id.
8518 field: String,
8519 /// The option's id.
8520 option: String,
8521 /// The option's name, as the board spells it.
8522 name: String,
8523 /// What the status asks of the issue's state.
8524 target: StatusTarget,
8525 /// The status the item reads as once it is there.
8526 landed: Status,
8527 /// Which of the status's two halves differ from what the item holds.
8528 moves: Moves,
8529}
8530
8531/// Which halves of an item's status one targeted update moves: its `Status` option, the open or
8532/// closed state of its issue, or both. A status neither half of which differs is no move at all,
8533/// and is not a value of this type.
8534#[derive(Clone, Copy, PartialEq, Eq)]
8535enum Moves {
8536 /// The option alone.
8537 Option,
8538 /// The issue's state alone: open, closed, or closed with another reason.
8539 State,
8540 /// Both.
8541 Both,
8542}
8543
8544impl Moves {
8545 /// What differs, or `None` when nothing does.
8546 const fn of(option: bool, state: bool) -> Option<Self> {
8547 match (option, state) {
8548 (true, true) => Some(Self::Both),
8549 (true, false) => Some(Self::Option),
8550 (false, true) => Some(Self::State),
8551 (false, false) => None,
8552 }
8553 }
8554
8555 /// Whether the option moves.
8556 const fn option(self) -> bool {
8557 matches!(self, Self::Option | Self::Both)
8558 }
8559
8560 /// Whether the issue's state moves.
8561 const fn state(self) -> bool {
8562 matches!(self, Self::State | Self::Both)
8563 }
8564}
8565
8566/// What one write is, and the status that comes with being it.
8567///
8568/// One value rather than a [`BoardKind`] beside an `Option<Status>`: a document has no
8569/// status and a task or a project always has one, so "a document carrying a status" and
8570/// "a task carrying none" are states a write cannot be in rather than states every use
8571/// site below has to defend against.
8572enum Written<'a> {
8573 /// A document, which is not work and so has no status at all.
8574 Document,
8575 /// A task or a project, and the status it is being written with.
8576 Work(ItemKind, &'a Status),
8577}
8578
8579impl Written<'_> {
8580 /// Which of the board's three kinds this write is.
8581 const fn kind(&self) -> BoardKind {
8582 match self {
8583 Self::Document => BoardKind::Document,
8584 Self::Work(kind, _) => BoardKind::Work(*kind),
8585 }
8586 }
8587
8588 /// The status this write carries. A document carries none, so a write of one says
8589 /// nothing about the issue's open or closed state and selects no board `Status`
8590 /// option.
8591 const fn status(&self) -> Option<&Status> {
8592 match self {
8593 Self::Document => None,
8594 Self::Work(_, status) => Some(status),
8595 }
8596 }
8597
8598 /// The status this write carries with the kind whose half of `status_mapping` it is
8599 /// written through.
8600 const fn work_status(&self) -> Option<(ItemKind, &Status)> {
8601 match self {
8602 Self::Document => None,
8603 Self::Work(kind, status) => Some((*kind, status)),
8604 }
8605 }
8606}
8607
8608/// The item being written, in the one shape all three write methods reach.
8609struct Incoming<'a> {
8610 written: Written<'a>,
8611 /// The title a person wrote. A document's goes onto the issue with
8612 /// [`DESIGN_TITLE_PREFIX`] put back, so a round trip returns the title that went in.
8613 title: &'a str,
8614 content: Option<&'a str>,
8615 labels: &'a [Label],
8616 metadata: &'a BTreeMap<String, Value>,
8617 repositories: &'a [Repository],
8618 parent: Option<&'a NativeId>,
8619 /// [`Task::delivers`], already checked. Empty for a project or a document, which is
8620 /// what keeps either key out of their slot.
8621 delivers: &'a [TaskRef],
8622 /// [`Task::delivered_by`], already checked. Empty for a project or a document.
8623 delivered_by: &'a [TaskRef],
8624 /// [`Task::priority`], for a task written to an instance that holds one; `None` for a
8625 /// project, a document, and every write to an instance with no `priority_mapping` —
8626 /// which is what keeps such a write's requests exactly what they were before.
8627 priority: Option<Priority>,
8628}
8629
8630/// What one write does to an item's `Priority` field.
8631enum PriorityWrite {
8632 /// Select this option of this field.
8633 Select {
8634 /// The `Priority` field's id.
8635 field: String,
8636 /// The mapped option's id.
8637 option: String,
8638 },
8639 /// Clear the field's value, which is what `none` is.
8640 Clear {
8641 /// The `Priority` field's id.
8642 field: String,
8643 },
8644}
8645
8646impl Incoming<'_> {
8647 /// The title this write puts on the issue.
8648 fn written_title(&self) -> String {
8649 match self.written {
8650 Written::Document => format!("{DESIGN_TITLE_PREFIX}{}", self.title),
8651 Written::Work(..) => self.title.to_owned(),
8652 }
8653 }
8654}
8655
8656#[derive(Clone, Copy, PartialEq, Eq)]
8657enum ContentKind {
8658 DraftIssue,
8659 Issue,
8660}
8661
8662/// What one board issue is: a document, or the work an [`ItemKind`] names.
8663///
8664/// A type of this source's own rather than an `ItemKind` with a third variant, because
8665/// `ItemKind` names what a dependency endpoint points at and nothing may point at a
8666/// document — the contract keeps a document out of that enum deliberately. Holding the
8667/// board's three answers in one value is what makes every place that asks "which is this?"
8668/// answer all three, rather than a `document: bool` beside a `kind` that means nothing for
8669/// two thirds of the board.
8670#[derive(Clone, Copy, PartialEq, Eq)]
8671enum BoardKind {
8672 /// An issue whose title begins [`DESIGN_TITLE_PREFIX`].
8673 Document,
8674 /// Every other issue, and every draft.
8675 Work(ItemKind),
8676}
8677
8678impl BoardKind {
8679 /// Whose half of `status_mapping` an item of this kind reads its status through. A
8680 /// document has no status of its own, so the task half stands in for whatever the issue
8681 /// holds; nothing reports it.
8682 const fn status_kind(self) -> ItemKind {
8683 match self {
8684 Self::Document => ItemKind::Task,
8685 Self::Work(kind) => kind,
8686 }
8687 }
8688
8689 /// How a refusal names this kind to the person reading it.
8690 const fn describes(self) -> &'static str {
8691 match self {
8692 Self::Document => "document",
8693 Self::Work(kind) => kind.marker(),
8694 }
8695 }
8696}
8697
8698/// Whether `labels` satisfies `filter`, matching by name, case-insensitively.
8699///
8700/// This is the local Markdown source's `labels_match`, spelled the same way on purpose:
8701/// the shared cross-source journeys assert one answer to one question, so two sources
8702/// that disagree about what "carries the label bug" means fail them.
8703fn labels_match(labels: &[Label], filter: &LabelFilter) -> bool {
8704 let holds = |name: &String| {
8705 labels
8706 .iter()
8707 .any(|label| label.name.eq_ignore_ascii_case(name))
8708 };
8709 (filter.any_of.is_empty() || filter.any_of.iter().any(holds))
8710 && filter.all_of.iter().all(holds)
8711 && !filter.none_of.iter().any(holds)
8712}
8713
8714/// Whether `category` is one of `statuses`. An empty list is unfiltered rather than
8715/// "keeps nothing", which is what lets a `Vec<StatusCategory>` spell no filter at all.
8716fn status_matches(category: StatusCategory, statuses: &[StatusCategory]) -> bool {
8717 statuses.is_empty() || statuses.contains(&category)
8718}
8719
8720/// Whether `title`/`content` satisfies `query`, matching case-insensitively.
8721///
8722/// `content` is the item's own prose — the body with this source's trailing metadata
8723/// comment already taken off — so a search never matches an encoding the author of the
8724/// issue never wrote.
8725fn text_matches(title: &str, content: Option<&str>, query: &TextQuery) -> bool {
8726 let terms = query.terms.to_lowercase();
8727 let in_title = title.to_lowercase().contains(&terms);
8728 let in_content = content.is_some_and(|body| body.to_lowercase().contains(&terms));
8729 match query.fields {
8730 TextFields::Title => in_title,
8731 TextFields::Content => in_content,
8732 TextFields::TitleOrContent => in_title || in_content,
8733 }
8734}
8735
8736/// Whether `task` satisfies `query`, with `project` deciding the project predicate.
8737///
8738/// The project predicate is passed separately because a read narrowed to one project has
8739/// already answered it by asking *that project* for its own items — and re-applying it
8740/// there would compare the caller's selector, which may be a project's **name**, against
8741/// the id of the project that name resolved to, and keep nothing. Every other read passes
8742/// `query.project` and applies it here, which is what keeps `projects` a predicate this
8743/// source really does apply.
8744fn task_matches(task: &Task, query: &TaskQuery, project: &ProjectFilter) -> bool {
8745 labels_match(&task.labels, &query.labels)
8746 && status_matches(task.status.category, &query.statuses)
8747 && (query.priorities.is_empty() || query.priorities.contains(&task.priority))
8748 && match project {
8749 ProjectFilter::Any => true,
8750 ProjectFilter::Orphans => task.project.is_none(),
8751 ProjectFilter::Is(id) => task.project.as_ref() == Some(id),
8752 }
8753 && query
8754 .text
8755 .as_ref()
8756 .is_none_or(|text| text_matches(&task.title, task.content.as_deref(), text))
8757 // Against the parsed metadata slot, and against the origin field, which is where
8758 // `Resolved::metadata` reads each of them from.
8759 && query.metadata_matches(&task.metadata)
8760 && query.origin_matches(&task.metadata)
8761}
8762
8763fn project_matches(project: &Project, query: &ProjectQuery) -> bool {
8764 labels_match(&project.labels, &query.labels)
8765 && status_matches(project.status.category, &query.statuses)
8766 && query
8767 .text
8768 .as_ref()
8769 .is_none_or(|text| text_matches(&project.title, project.content.as_deref(), text))
8770}
8771
8772/// The same three predicates a task query carries, minus the status filter.
8773///
8774/// A document is not work, so it has no status for one to compare against and the query
8775/// type carries none. The project predicate is the same one — a design issue filed under a
8776/// project issue is in that project, and one filed under nothing is in none — so it is
8777/// spelled the same way here rather than answered differently.
8778fn document_matches(document: &Document, query: &DocumentQuery, project: &ProjectFilter) -> bool {
8779 labels_match(&document.labels, &query.labels)
8780 && match project {
8781 ProjectFilter::Any => true,
8782 ProjectFilter::Orphans => document.project.is_none(),
8783 ProjectFilter::Is(id) => document.project.as_ref() == Some(id),
8784 }
8785 && query
8786 .text
8787 .as_ref()
8788 .is_none_or(|text| text_matches(&document.title, document.content.as_deref(), text))
8789}
8790
8791#[async_trait::async_trait]
8792impl TaskSource for GitHubProjectsSource {
8793 fn kind(&self) -> &'static str {
8794 KIND
8795 }
8796 fn capabilities(&self) -> Capabilities {
8797 Capabilities {
8798 projects: Support::Native,
8799 documents: Support::Native,
8800 comments: Support::Native,
8801 assets: Support::Unsupported,
8802 priority: if self.priorities.is_some() {
8803 Support::Native
8804 } else {
8805 Support::Unsupported
8806 },
8807 filter_by_priority: Support::Native,
8808 filter_by_comment_activity: Support::Native,
8809 filter_by_metadata: Support::Native,
8810 filter_by_origin: Support::Native,
8811 orphan_tasks: Support::Native,
8812 filter_by_label: Support::Native,
8813 filter_by_status: Support::Native,
8814 search_title: Support::Native,
8815 search_content: Support::Native,
8816 task_dependencies: DependencySupport::BothDirections,
8817 project_dependencies: DependencySupport::BothDirections,
8818 max_page_size: MAX_PAGE_SIZE,
8819 }
8820 }
8821 async fn health(&self) -> Result<Health, SourceError> {
8822 let board = self.board_page(None, 1).await?;
8823 Ok(Health {
8824 reachable: true,
8825 detail: Some(format!(
8826 "reading GitHub project {}/{} ({})",
8827 self.owner,
8828 self.project_number,
8829 required_str(&board, "title")?
8830 )),
8831 })
8832 }
8833 async fn get_task(&self, id: &NativeId) -> Result<Option<Task>, SourceError> {
8834 self.item_by_id(id)
8835 .await?
8836 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8837 .map(|item| item.task())
8838 .transpose()
8839 }
8840 async fn get_project(&self, id: &NativeId) -> Result<Option<Project>, SourceError> {
8841 Ok(self
8842 .item_by_id(id)
8843 .await?
8844 .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
8845 .map(|item| item.project()))
8846 }
8847 async fn query_tasks(
8848 &self,
8849 query: &TaskQuery,
8850 page: &PageRequest,
8851 ) -> Result<Page<Task>, SourceError> {
8852 validate_page(page)?;
8853 refuse_unsearchable(query)?;
8854 if query.origin.is_none() && !matches!(query.project, ProjectFilter::Is(_)) {
8855 let qualifiers = match (narrowing_qualifiers(query), query.commented_since) {
8856 (Some(also), Some(since)) => Some(format!("{} {also}", updated_qualifier(since))),
8857 (Some(also), None) => Some(also),
8858 (None, Some(since)) => Some(updated_qualifier(since)),
8859 (None, None) => None,
8860 };
8861 if let Some(also) = qualifiers {
8862 return self.search_tasks(query, page, &also).await;
8863 }
8864 }
8865
8866 // A read narrowed to one project asks that project for its own tasks, so nothing
8867 // about it costs what the rest of the board holds. A read carrying a text, metadata
8868 // or origin predicate asks GitHub the narrower question those predicates are, and a
8869 // read narrowed to comment activity alone asks the board's own issue search for the
8870 // issues updated since, which is every issue a comment could have been written or
8871 // edited on since. Every other task read is a question about the whole board and is
8872 // answered by reading it.
8873 let (held, membership) = match (&query.project, query.commented_since) {
8874 (ProjectFilter::Is(project), _) => (
8875 self.project_children(project).await?,
8876 // Answered by where these items came from; see `task_matches`.
8877 &ProjectFilter::Any,
8878 ),
8879 (ProjectFilter::Any | ProjectFilter::Orphans, since) => {
8880 match (self.narrowed(query).await?, since) {
8881 (Some(narrowed), _) => (narrowed, &query.project),
8882 (None, Some(since)) => (self.updated_since(since).await?, &query.project),
8883 (None, None) => (self.board().await?.items, &query.project),
8884 }
8885 }
8886 };
8887 // Filtered before paged: a page of a filtered result is a page of the survivors,
8888 // never the survivors of a page.
8889 let mut tasks = Vec::new();
8890 for item in held
8891 .iter()
8892 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8893 {
8894 let task = item.task()?;
8895 if task_matches(&task, query, membership)
8896 && self.commented_since(item, query.commented_since).await?
8897 {
8898 tasks.push(task);
8899 }
8900 }
8901 Ok(offset_page(
8902 tasks,
8903 numeric_cursor(page.cursor.as_ref())?,
8904 page.limit.min(MAX_PAGE_SIZE) as usize,
8905 ))
8906 }
8907 async fn query_projects(
8908 &self,
8909 query: &ProjectQuery,
8910 page: &PageRequest,
8911 ) -> Result<Page<Project>, SourceError> {
8912 validate_page(page)?;
8913 refuse_unsearchable_text(query.text.as_ref())?;
8914 // The projects a board holds are found by an issue search scoped to that board,
8915 // never by walking the board's own item connection: what tells a project from a
8916 // task is the `parent` each issue carries, which costs nothing to read. A query
8917 // carrying a text asks that search for the text too, so it reads the issues that
8918 // hold it rather than every issue of the board.
8919 let held = match self.text_searched(query.text.as_ref()).await? {
8920 Some(searched) => searched,
8921 None => self.board_issues().await?,
8922 };
8923 let projects = held
8924 .iter()
8925 .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
8926 .map(Resolved::project)
8927 .filter(|project| project_matches(project, query))
8928 .collect();
8929 Ok(offset_page(
8930 projects,
8931 numeric_cursor(page.cursor.as_ref())?,
8932 page.limit.min(MAX_PAGE_SIZE) as usize,
8933 ))
8934 }
8935 async fn get_document(&self, id: &NativeId) -> Result<Option<Document>, SourceError> {
8936 Ok(self
8937 .item_by_id(id)
8938 .await?
8939 .filter(|item| item.kind == BoardKind::Document)
8940 .map(|item| item.document()))
8941 }
8942 async fn query_documents(
8943 &self,
8944 query: &DocumentQuery,
8945 page: &PageRequest,
8946 ) -> Result<Page<Document>, SourceError> {
8947 validate_page(page)?;
8948 // Narrowed to one project, this is the same sub-issue read a task list scoped to
8949 // that project makes — a document filed under a project is a sub-issue of it too,
8950 // and which of them come back is the kind this caller asked for. Unscoped, a query
8951 // carrying a text asks the board-scoped issue search for it, as a task query does,
8952 // and only one carrying none reads the board.
8953 let (held, membership) = match &query.project {
8954 ProjectFilter::Is(project) => (
8955 self.project_children(project).await?,
8956 // Answered by where these items came from; see `task_matches`.
8957 &ProjectFilter::Any,
8958 ),
8959 ProjectFilter::Any | ProjectFilter::Orphans => {
8960 refuse_unsearchable_text(query.text.as_ref())?;
8961 match self.text_searched(query.text.as_ref()).await? {
8962 Some(searched) => (searched, &query.project),
8963 None => (self.board().await?.items, &query.project),
8964 }
8965 }
8966 };
8967 // Filtered before paged, exactly as a task read is: a page of a filtered result is
8968 // a page of the survivors, never the survivors of a page.
8969 let documents = held
8970 .iter()
8971 .filter(|item| item.kind == BoardKind::Document)
8972 .map(Resolved::document)
8973 .filter(|document| document_matches(document, query, membership))
8974 .collect();
8975 Ok(offset_page(
8976 documents,
8977 numeric_cursor(page.cursor.as_ref())?,
8978 page.limit.min(MAX_PAGE_SIZE) as usize,
8979 ))
8980 }
8981 async fn labels(&self, page: &PageRequest) -> Result<Page<Label>, SourceError> {
8982 validate_page(page)?;
8983 let offset = numeric_cursor(page.cursor.as_ref())?;
8984 let mut labels = self
8985 .board()
8986 .await?
8987 .items
8988 .into_iter()
8989 .flat_map(|item| item.labels)
8990 .fold(Vec::new(), |mut all, label| {
8991 if !all.iter().any(|x: &Label| x.id == label.id) {
8992 all.push(label);
8993 }
8994 all
8995 });
8996 labels.sort_by(|a, b| a.name.cmp(&b.name).then(a.id.0.cmp(&b.id.0)));
8997 Ok(offset_page(
8998 labels,
8999 offset,
9000 page.limit.min(MAX_PAGE_SIZE) as usize,
9001 ))
9002 }
9003 async fn task_dependencies(
9004 &self,
9005 id: &NativeId,
9006 direction: Direction,
9007 page: &PageRequest,
9008 ) -> Result<Page<DependencyEdge>, SourceError> {
9009 self.dependencies(id, ItemKind::Task, direction, page).await
9010 }
9011 async fn project_dependencies(
9012 &self,
9013 id: &NativeId,
9014 direction: Direction,
9015 page: &PageRequest,
9016 ) -> Result<Page<DependencyEdge>, SourceError> {
9017 self.dependencies(id, ItemKind::Project, direction, page)
9018 .await
9019 }
9020
9021 fn writes(&self) -> WriteSupport {
9022 WriteSupport::Supported
9023 }
9024
9025 /// Create or update one task.
9026 ///
9027 /// Its `delivers` and `delivered_by` are checked before anything is read or written —
9028 /// neither may name the task itself or name one task twice — and land in the body's
9029 /// metadata slot under their reserved keys, in place of any caller metadata of those
9030 /// names.
9031 async fn write_task(&self, write: &ItemWrite<Task>) -> Result<NativeId, SourceError> {
9032 let near = write.target.as_ref().unwrap_or(&write.item.id);
9033 for (key, entries) in [
9034 (TaskRef::DELIVERS_KEY, &write.item.delivers),
9035 (TaskRef::DELIVERED_BY_KEY, &write.item.delivered_by),
9036 ] {
9037 TaskRef::listed(key, near, Some(&self.name), entries.clone())
9038 .map_err(|message| SourceError::Refused { message })?;
9039 }
9040 if self.priorities.is_none() && write.item.priority != Priority::None {
9041 return Err(self.holds_no_priority());
9042 }
9043 self.write_item(
9044 &Incoming {
9045 written: Written::Work(ItemKind::Task, &write.item.status),
9046 title: &write.item.title,
9047 content: write.item.content.as_deref(),
9048 labels: &write.item.labels,
9049 metadata: &write.item.metadata,
9050 repositories: &write.item.repositories,
9051 parent: write.item.project.as_ref(),
9052 delivers: &write.item.delivers,
9053 delivered_by: &write.item.delivered_by,
9054 priority: self.priorities.as_ref().map(|_| write.item.priority),
9055 },
9056 write.target.as_ref(),
9057 &write.depends_on,
9058 )
9059 .await
9060 }
9061
9062 async fn write_project(&self, write: &ItemWrite<Project>) -> Result<NativeId, SourceError> {
9063 self.write_item(
9064 &Incoming {
9065 written: Written::Work(ItemKind::Project, &write.item.status),
9066 title: &write.item.title,
9067 content: write.item.content.as_deref(),
9068 labels: &write.item.labels,
9069 metadata: &write.item.metadata,
9070 repositories: &write.item.repositories,
9071 parent: None,
9072 delivers: &[],
9073 delivered_by: &[],
9074 priority: None,
9075 },
9076 write.target.as_ref(),
9077 &write.depends_on,
9078 )
9079 .await
9080 }
9081
9082 /// Create or update one document, which is one issue titled the way this board spells
9083 /// a document.
9084 ///
9085 /// Everything else is exactly a task write: caller metadata goes to the same canonical
9086 /// JSON slot at the end of the body and comes back with its JSON types intact, a key
9087 /// or a field this board cannot carry is refused by name rather than dropped, a target
9088 /// naming an issue this board does not hold is refused rather than created, and an
9089 /// issue this call created is taken back when the rest of the write fails.
9090 async fn write_document(&self, write: &ItemWrite<Document>) -> Result<NativeId, SourceError> {
9091 // A document takes part in no dependency graph, so there is no far end to write
9092 // natively and none to record: a caller naming one is told so rather than having it
9093 // stored under the reserved key, where a later read would report an edge the
9094 // contract says cannot exist.
9095 if !write.depends_on.is_empty() {
9096 return Err(SourceError::Refused {
9097 message: format!(
9098 "this write names {} dependencies for a document, and a document takes \
9099 part in no dependency graph; next: put the dependency on the task or \
9100 project the document is about",
9101 write.depends_on.len()
9102 ),
9103 });
9104 }
9105 self.write_item(
9106 &Incoming {
9107 written: Written::Document,
9108 title: &write.item.title,
9109 content: write.item.content.as_deref(),
9110 labels: &write.item.labels,
9111 metadata: &write.item.metadata,
9112 repositories: &write.item.repositories,
9113 parent: write.item.project.as_ref(),
9114 delivers: &[],
9115 delivered_by: &[],
9116 priority: None,
9117 },
9118 write.target.as_ref(),
9119 &[],
9120 )
9121 .await
9122 }
9123
9124 /// Refused exactly as the write refuses it, from what the write reads: the mapping first,
9125 /// which reads nothing; then the board's `Status` option. Over an existing item that is
9126 /// read off the item, as the write reads it, and the item is held among this command's
9127 /// resolved records so the write that follows reuses that read rather than repeating it;
9128 /// an item that does not carry the field takes the board's fields, which are held once
9129 /// read. A create is checked against the board's fields only when this command already
9130 /// holds them, because a create reads them together with its repository, in one request,
9131 /// and refuses a missing option before it writes anything.
9132 async fn check_status_write(
9133 &self,
9134 kind: ItemKind,
9135 category: StatusCategory,
9136 target: Option<&NativeId>,
9137 ) -> Result<(), SourceError> {
9138 let status = self.resolved_target(kind, category)?;
9139 if status.option().is_none() {
9140 return Ok(());
9141 }
9142 let fields = match target {
9143 Some(target) => {
9144 // A target this board does not hold is the write's own refusal to make.
9145 let Some(item) = self.bound_item(target).await? else {
9146 return Ok(());
9147 };
9148 self.resolved_cache()?.insert(target.clone(), item.clone());
9149 self.fields_for(Some(&item), true, false).await?.fields
9150 }
9151 None => {
9152 let held = self
9153 .board_cache()?
9154 .as_ref()
9155 .map(|board| board.fields.clone());
9156 match held.or_else(|| {
9157 self.fields_cache()
9158 .ok()
9159 .and_then(|cache| cache.as_ref().map(|board| board.fields.clone()))
9160 }) {
9161 Some(fields) => fields,
9162 None => return Ok(()),
9163 }
9164 }
9165 };
9166 self.column_for(&fields, kind, category, &status)
9167 .map(|_| ())
9168 }
9169
9170 /// Set one task's status alone.
9171 ///
9172 /// An open target reopens a closed issue with an `updateIssue` carrying only its
9173 /// `stateInput`, then selects the board option with `updateProjectV2ItemFieldValue`; a
9174 /// terminal target selects its mapped option, then closes with its fixed reason. No
9175 /// request carries a title, a body or a label. The status
9176 /// answered is what [`BoardStatuses::status`] reads off the state just written, which is
9177 /// what a re-read reports.
9178 async fn set_task_status(
9179 &self,
9180 id: &NativeId,
9181 category: StatusCategory,
9182 ) -> Result<Option<Status>, SourceError> {
9183 self.set_status(id, category).await
9184 }
9185
9186 /// Set one task's priority alone: one `updateProjectV2ItemFieldValue` selecting the
9187 /// mapped option of the board's `Priority` field, or one `clearProjectV2ItemFieldValue`
9188 /// for `none`. Refused by an instance with no `priority_mapping`.
9189 async fn set_task_priority(
9190 &self,
9191 id: &NativeId,
9192 priority: Priority,
9193 ) -> Result<Option<Priority>, SourceError> {
9194 self.set_priority(id, priority).await
9195 }
9196
9197 /// Replace one task's content with a single body update that keeps the metadata slot
9198 /// byte for byte.
9199 async fn set_task_content(
9200 &self,
9201 id: &NativeId,
9202 content: &str,
9203 ) -> Result<Option<()>, SourceError> {
9204 self.replace_content(id, content).await
9205 }
9206
9207 /// Replace one task issue's content and its provenance slot entry with a single body
9208 /// update. The answers are not kept: see `replace_rendering`.
9209 async fn set_task_rendering(
9210 &self,
9211 id: &NativeId,
9212 content: &str,
9213 provenance: &Value,
9214 _answers: &BTreeMap<String, Value>,
9215 ) -> Result<Option<()>, SourceError> {
9216 self.replace_rendering(id, BoardKind::Work(ItemKind::Task), content, provenance)
9217 .await
9218 }
9219
9220 /// Replace one design-document issue's content and its provenance slot entry, on exactly
9221 /// the terms of [`set_task_rendering`](TaskSource::set_task_rendering).
9222 async fn set_document_rendering(
9223 &self,
9224 id: &NativeId,
9225 content: &str,
9226 provenance: &Value,
9227 _answers: &BTreeMap<String, Value>,
9228 ) -> Result<Option<()>, SourceError> {
9229 self.replace_rendering(id, BoardKind::Document, content, provenance)
9230 .await
9231 }
9232
9233 /// Replace one project issue's content and its provenance slot entry, on exactly the
9234 /// terms of [`set_task_rendering`](TaskSource::set_task_rendering).
9235 async fn set_project_rendering(
9236 &self,
9237 id: &NativeId,
9238 content: &str,
9239 provenance: &Value,
9240 _answers: &BTreeMap<String, Value>,
9241 ) -> Result<Option<()>, SourceError> {
9242 self.replace_rendering(id, BoardKind::Work(ItemKind::Project), content, provenance)
9243 .await
9244 }
9245
9246 /// Apply a targeted update with one read of the item and a write only for what differs:
9247 /// the `Status` and `Priority` field writes in one request, the `blockedBy` difference,
9248 /// and last one `updateIssue` for title, body and state. See `targeted_update`.
9249 async fn update_task(
9250 &self,
9251 id: &NativeId,
9252 update: &TaskUpdate,
9253 ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
9254 self.targeted_update(id, update).await
9255 }
9256
9257 /// Replace one task's `delivered_by` with a single body update that changes the
9258 /// metadata slot and nothing outside it.
9259 async fn set_delivered_by(
9260 &self,
9261 id: &NativeId,
9262 delivered_by: &[TaskRef],
9263 ) -> Result<Option<()>, SourceError> {
9264 self.replace_delivered_by(id, delivered_by).await
9265 }
9266
9267 /// Set one key of one task issue's metadata with a single body update that changes the
9268 /// metadata slot and nothing outside it — no title, label, state or board field request —
9269 /// and sends nothing when the task already holds that value under the key.
9270 async fn set_task_metadata(
9271 &self,
9272 id: &NativeId,
9273 key: &MetadataKey,
9274 value: &Value,
9275 ) -> Result<Option<Task>, SourceError> {
9276 Ok(self
9277 .set_slot_key(id, BoardKind::Work(ItemKind::Task), key, value)
9278 .await?
9279 .map(|item| item.task())
9280 .transpose()?)
9281 }
9282
9283 /// Set one key of one project issue's metadata, on exactly the terms of
9284 /// [`set_task_metadata`](TaskSource::set_task_metadata).
9285 async fn set_project_metadata(
9286 &self,
9287 id: &NativeId,
9288 key: &MetadataKey,
9289 value: &Value,
9290 ) -> Result<Option<Project>, SourceError> {
9291 Ok(self
9292 .set_slot_key(id, BoardKind::Work(ItemKind::Project), key, value)
9293 .await?
9294 .map(|item| item.project()))
9295 }
9296
9297 /// Set one key of one design-document issue's metadata, on exactly the terms of
9298 /// [`set_task_metadata`](TaskSource::set_task_metadata).
9299 async fn set_document_metadata(
9300 &self,
9301 id: &NativeId,
9302 key: &MetadataKey,
9303 value: &Value,
9304 ) -> Result<Option<Document>, SourceError> {
9305 Ok(self
9306 .set_slot_key(id, BoardKind::Document, key, value)
9307 .await?
9308 .map(|item| item.document()))
9309 }
9310
9311 async fn delete_task(&self, id: &NativeId) -> Result<(), SourceError> {
9312 self.delete_item(id).await
9313 }
9314
9315 async fn delete_project(&self, id: &NativeId) -> Result<(), SourceError> {
9316 self.delete_item(id).await
9317 }
9318
9319 async fn delete_document(&self, id: &NativeId) -> Result<(), SourceError> {
9320 self.delete_item(id).await
9321 }
9322
9323 /// One page of the task issue's own comments, walked by GitHub's own cursor.
9324 ///
9325 /// Nothing here filters, so nothing has to be read ahead of the page: the caller's limit is
9326 /// the page GitHub is asked for and GitHub's `endCursor` is the cursor handed back.
9327 ///
9328 /// One request, [`graphql::ISSUE_DETAIL`]: the read that says the id names a task of this
9329 /// board is the read of its comments. A draft this process already resolved is refused
9330 /// without one.
9331 async fn task_comments(
9332 &self,
9333 task: &NativeId,
9334 page: &PageRequest,
9335 ) -> Result<Option<Page<Comment>>, SourceError> {
9336 validate_page(page)?;
9337 let cached = self.resolved_cache()?.get(task).cloned();
9338 if let Some(item) = cached {
9339 if item.kind != BoardKind::Work(ItemKind::Task) {
9340 return Ok(None);
9341 }
9342 if item.content_kind == ContentKind::DraftIssue {
9343 return Err(self.draft_has_no_comments(task));
9344 }
9345 }
9346 match self.issue_detail(task, page).await? {
9347 Some(TaskDetailRead {
9348 comments: Some(comments),
9349 ..
9350 }) => comments,
9351 _ => Ok(None),
9352 }
9353 }
9354
9355 /// Every id's task, with the first page of its comments when `comments` names it:
9356 /// [`DETAIL_BATCH`] items per [`graphql::ISSUE_DETAILS`] request, and one item with its
9357 /// comments in one [`graphql::ISSUE_DETAIL`] request.
9358 async fn get_task_details(
9359 &self,
9360 ids: &[NativeId],
9361 comments: Option<&PageRequest>,
9362 ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
9363 if let Some(page) = comments
9364 && let Err(error) = validate_page(page)
9365 {
9366 return ids.iter().map(|_| Err(error.clone())).collect();
9367 }
9368 match (ids, comments) {
9369 ([id], Some(page)) => vec![self.issue_detail(id, page).await],
9370 ([id], None) => vec![self.task_read(id).await],
9371 _ => self.issue_details(ids, comments).await,
9372 }
9373 }
9374
9375 /// Add one comment to the task's issue, as the account the token belongs to.
9376 ///
9377 /// The author is refused before anything is sent — not even the task is read — because
9378 /// no answer GitHub could give would make posting under another name than the one asked
9379 /// for the right outcome.
9380 async fn add_comment(
9381 &self,
9382 task: &NativeId,
9383 comment: &NewComment,
9384 ) -> Result<Option<Comment>, SourceError> {
9385 if let Some(author) = &comment.author {
9386 return Err(SourceError::Refused {
9387 message: format!(
9388 "source {} cannot post a comment as {author:?}: GitHub records the account \
9389 the token signs in as the author of every comment; next: leave --author \
9390 out, and the comment is posted as that account",
9391 self.name
9392 ),
9393 });
9394 }
9395 let Some(issue) = self.commented_issue(task).await? else {
9396 return Ok(None);
9397 };
9398 let data = self
9399 .graphql(
9400 graphql::ADD_COMMENT,
9401 json!({"input":{"subjectId":issue.0,"body":comment.body.as_str()}}),
9402 )
9403 .await?;
9404 let subject = data
9405 .pointer("/addComment/subject")
9406 .filter(|value| !value.is_null())
9407 .ok_or_else(|| SourceError::Malformed {
9408 message: "GitHub comment addition returned no subject".into(),
9409 })?;
9410 if required_str(subject, "id")? != issue.0 {
9411 return Err(SourceError::Malformed {
9412 message: "GitHub comment addition answered about another issue".into(),
9413 });
9414 }
9415 let added = data
9416 .pointer("/addComment/commentEdge/node")
9417 .filter(|value| !value.is_null())
9418 .ok_or_else(|| SourceError::Malformed {
9419 message: "GitHub comment addition returned no comment".into(),
9420 })?;
9421 comment_from(added).map(Some)
9422 }
9423
9424 async fn edit_comment(
9425 &self,
9426 task: &NativeId,
9427 comment: &NativeId,
9428 body: &CommentBody,
9429 ) -> Result<Option<Comment>, SourceError> {
9430 let Some(issue) = self.commented_issue(task).await? else {
9431 return Ok(None);
9432 };
9433 if !self.comment_is_on(&issue, comment).await? {
9434 return Ok(None);
9435 }
9436 let data = self
9437 .graphql(
9438 graphql::UPDATE_COMMENT,
9439 json!({"input":{"id":comment.0,"body":body.as_str()}}),
9440 )
9441 .await?;
9442 let edited = data
9443 .pointer("/updateIssueComment/issueComment")
9444 .filter(|value| !value.is_null())
9445 .ok_or_else(|| SourceError::Malformed {
9446 message: "GitHub comment update returned no comment".into(),
9447 })?;
9448 let edited = comment_from(edited)?;
9449 if edited.id != *comment {
9450 return Err(SourceError::Malformed {
9451 message: "GitHub comment update returned the wrong comment".into(),
9452 });
9453 }
9454 Ok(Some(edited))
9455 }
9456
9457 async fn delete_comment(
9458 &self,
9459 task: &NativeId,
9460 comment: &NativeId,
9461 ) -> Result<Option<NativeId>, SourceError> {
9462 let Some(issue) = self.commented_issue(task).await? else {
9463 return Ok(None);
9464 };
9465 if !self.comment_is_on(&issue, comment).await? {
9466 return Ok(None);
9467 }
9468 let data = self
9469 .graphql(graphql::DELETE_COMMENT, json!({"input":{"id":comment.0}}))
9470 .await?;
9471 // The payload says nothing about the comment it removed, so what is checked is that
9472 // GitHub answered the mutation at all rather than leaving it unanswered.
9473 data.get("deleteIssueComment")
9474 .filter(|value| !value.is_null())
9475 .ok_or_else(|| SourceError::Malformed {
9476 message: "GitHub comment deletion returned no payload".into(),
9477 })?;
9478 Ok(Some(comment.clone()))
9479 }
9480
9481 /// Every request this source has recorded, and what each of GitHub's two budgets was
9482 /// attributed — read off the same accounting the session report is rendered from, so
9483 /// the two cannot count one request two ways.
9484 async fn metering(&self) -> Result<Option<Metering>, SourceError> {
9485 Ok(Some(self.ledger.snapshot().metering()))
9486 }
9487
9488 /// Drop every item, search answer and board read this source holds, so the next command
9489 /// reads the board as a person has since left it.
9490 ///
9491 /// Every one of those is held on the assumption that nothing but this source writes the
9492 /// board while a command runs, which stops being true the moment the command is over: a
9493 /// body a person edited would be overwritten from the record held here, and a card they
9494 /// moved would be read as still where this source left it. The board's own field
9495 /// definitions go too, because a person can add or delete a `Status` option and a write
9496 /// resolved against the held list would not re-read on a miss. What stays is what stays
9497 /// valid in normal use: each repository's node id, which a miss re-reads, the pacing of
9498 /// mutations, which is about GitHub's limiter rather than anybody's work, and the running
9499 /// accounting [`metering`](TaskSource::metering) answers from.
9500 ///
9501 /// Infallible in practice: a lock an earlier failure poisoned is cleared rather than
9502 /// refused, because clearing it is what puts it right.
9503 async fn end_command(&self) -> Result<(), SourceError> {
9504 fn clear<T: Default>(held: &Mutex<T>) {
9505 *held
9506 .lock()
9507 .unwrap_or_else(std::sync::PoisonError::into_inner) = T::default();
9508 held.clear_poison();
9509 }
9510 clear(&self.created);
9511 clear(&self.updated);
9512 clear(&self.board_cache);
9513 clear(&self.search_cache);
9514 clear(&self.narrowed_cache);
9515 clear(&self.search_next);
9516 clear(&self.resolved_cache);
9517 clear(&self.fields_cache);
9518 Ok(())
9519 }
9520}
9521
9522/// One issue comment as the contract carries it.
9523///
9524/// `author` is absent both when GitHub answers `null` for an account that no longer exists
9525/// and when it answers an actor with no login, because either way the source did not say who
9526/// wrote it — which is what an absent author means, rather than an author called nothing.
9527fn comment_from(value: &Value) -> Result<Comment, SourceError> {
9528 Ok(Comment {
9529 id: NativeId(required_str(value, "id")?.to_owned()),
9530 author: optional_str(value.get("author").unwrap_or(&Value::Null), "login")?
9531 .map(str::to_owned),
9532 created_at: optional_time(value, "createdAt")?,
9533 updated_at: optional_time(value, "updatedAt")?,
9534 body: required_str(value, "body")?.to_owned(),
9535 url: optional_str(value, "url")?.map(str::to_owned),
9536 })
9537}
9538
9539/// The page of comments one issue node carries, resumed from `after`.
9540fn comment_page(
9541 node: &Value,
9542 issue: &str,
9543 after: Option<&str>,
9544) -> Result<Page<Comment>, SourceError> {
9545 let connection = node
9546 .get("comments")
9547 .filter(|value| !value.is_null())
9548 .ok_or_else(|| SourceError::Malformed {
9549 message: format!("GitHub issue {issue} answered with no comments connection"),
9550 })?;
9551 let items = optional_nodes(Some(connection), "issue comments")?
9552 .into_iter()
9553 .flatten()
9554 .map(comment_from)
9555 .collect::<Result<Vec<_>, _>>()?;
9556 let next = next_cursor(connection)?;
9557 if let Some(next) = &next {
9558 validate_cursor_progress(after, &next.0)?;
9559 }
9560 Ok(Page { items, next })
9561}
9562
9563/// The far ends of an issue's whole `blockedBy` connection, when the read carried it to its
9564/// end — `None` when it carried none, or a page with more past it.
9565fn carried_blocked_by(content: &Value) -> Result<Option<Vec<Value>>, SourceError> {
9566 let Some(connection) = content.get("blockedBy").filter(|value| !value.is_null()) else {
9567 return Ok(None);
9568 };
9569 if next_cursor(connection)?.is_some() {
9570 return Ok(None);
9571 }
9572 Ok(Some(
9573 optional_nodes(Some(connection), "blocked-by issues")?
9574 .into_iter()
9575 .flatten()
9576 .cloned()
9577 .collect(),
9578 ))
9579}
9580
9581/// Where the recorded tail of a dependency walk resumes; see
9582/// [`GitHubProjectsSource::recorded_edges`].
9583const RECORDED_CURSOR: &str = "onetaskgraph.depends_on:";
9584
9585/// The board text field this source keeps a copy's origin in.
9586///
9587/// Named after the key it holds, and held to that name by the guard below rather than by
9588/// a reader noticing.
9589const ORIGIN_FIELD: &str = "onetaskgraph.origin";
9590
9591/// The metadata key that field holds.
9592///
9593/// The engine owns this key and spells it once as `GlobalId::ORIGIN_KEY`; a plugin never
9594/// constructs or interprets the qualified id it carries. This source names it only to
9595/// route it — a short, typed value belongs in a typed field rather than in the body slot
9596/// a caller's own prose shares.
9597///
9598/// Restated rather than imported, because no plugin crate may depend on the engine. What
9599/// keeps the two spellings one contract is `scripts/check-origin-key-spelling.sh`, a
9600/// target in `check`: it reads the engine's own literal and fails naming the file and the
9601/// line when a plugin's parts from it either way. Drift here has one symptom — a copy
9602/// that creates a second item every run instead of finding the one it wrote — and that is
9603/// too late to learn it.
9604const ORIGIN_KEY: &str = "onetaskgraph.origin";
9605
9606/// Where a recorded tail resumes, refusing a cursor no walk in `direction` reported.
9607///
9608/// The reserved key holds forward edges and nothing else — the reverse of a recorded edge
9609/// is derived from the far end, never written down on the near item — so only a forward
9610/// walk ever reports one of these cursors. A reverse read carrying one is resuming a walk
9611/// it did not come from, and it is told so rather than answered with an empty page that
9612/// reads as a walk which ended.
9613fn recorded_offset(
9614 cursor: Option<&str>,
9615 direction: Direction,
9616) -> Result<Option<usize>, SourceError> {
9617 cursor
9618 .and_then(|cursor| cursor.strip_prefix(RECORDED_CURSOR))
9619 .map(|offset| {
9620 if direction != Direction::DependsOn {
9621 return Err(SourceError::Config {
9622 message: format!(
9623 "{RECORDED_CURSOR}{offset} resumes recorded forward edges, which a \
9624 reverse dependency read never issues; resume it in the direction \
9625 that reported it"
9626 ),
9627 });
9628 }
9629 offset.parse().map_err(|_| SourceError::Config {
9630 message: format!("{RECORDED_CURSOR}{offset} is not a recorded-edge cursor"),
9631 })
9632 })
9633 .transpose()
9634}
9635
9636fn recorded_page(edges: Vec<DependencyEdge>, offset: usize, limit: usize) -> Page<DependencyEdge> {
9637 let mut page = offset_page(edges, offset, limit.max(1));
9638 page.next = page
9639 .next
9640 .map(|cursor| Cursor(format!("{RECORDED_CURSOR}{}", cursor.0)));
9641 page
9642}
9643
9644/// The kind of one issue reached through a dependency connection.
9645///
9646/// The same questions the board scan asks, over the fields the dependency document
9647/// selects, and in the same order: the design prefix first, then a sub-issue is a task,
9648/// then anything with sub-issues or the marker is a project.
9649///
9650/// # Errors
9651///
9652/// A far end this board holds as a document is refused rather than reported. The two
9653/// answers that are not refusals would both be wrong: reporting it as a task names an id
9654/// no task read of this source can find, and reporting it as a project names one no
9655/// project read can. There is no third value to return — `ItemKind` has no document
9656/// variant, because nothing may point at a document — so the relationship itself is what
9657/// the person is told about.
9658fn related_kind(value: &Value) -> Result<ItemKind, SourceError> {
9659 let id = required_str(value, "id")?;
9660 if required_str(value, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
9661 return Err(SourceError::Refused {
9662 message: format!(
9663 "GitHub issue {id} is a document of this board — its title begins \
9664 {DESIGN_TITLE_PREFIX:?} — and nothing may depend on a document or be depended \
9665 on by one; next: remove that issue's blocking relationship on this board"
9666 ),
9667 });
9668 }
9669 let parent = optional_str(value.get("parent").unwrap_or(&Value::Null), "id")?;
9670 if parent.is_some() {
9671 return Ok(ItemKind::Task);
9672 }
9673 let (_, slot) = metadata_body(optional_str(value, "body")?.map(str::to_owned))?;
9674 let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
9675 message: format!("GitHub issue {id}: {message}"),
9676 })?;
9677 let sub_issues = sub_issue_total(value)?;
9678 Ok(if sub_issues > 0 || marked == Some(ItemKind::Project) {
9679 ItemKind::Project
9680 } else {
9681 ItemKind::Task
9682 })
9683}
9684
9685/// The `IssueStateUpdateInput` one status target asks for.
9686///
9687/// `stateInput` and `state` are mutually exclusive on `UpdateIssueInput`, and only this
9688/// one is ever sent. A non-terminal status always asks for `OPEN`, which is what reopens
9689/// a currently-closed issue: without that the item would read back `Unknown` and a copy
9690/// would report a change forever. A document has no status at all, and asks for neither.
9691fn state_input(target: Option<&StatusTarget>) -> Value {
9692 match target {
9693 Some(StatusTarget::Terminal(_, reason)) => {
9694 json!({"value":"CLOSED","stateReason":reason.reason()})
9695 }
9696 Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => json!({"value":"OPEN"}),
9697 // A document has no status, so a write of one says nothing about the issue's open
9698 // or closed state rather than forcing it open: `stateInput` is what carries that
9699 // instruction, and an explicit null asks for no change to it.
9700 None => Value::Null,
9701 }
9702}
9703
9704/// The metadata one write stores in the item's body slot.
9705///
9706/// The typed fields travel as themselves, so the three reserved keys are rebuilt here
9707/// rather than carried: the kind marker so an empty project stays readable, the
9708/// repository list only when it is not exactly the issue's own repository, and the far
9709/// ends no relationship here can name.
9710///
9711/// The copy origin is the one typed field that is also mirrored here, and only as a
9712/// mirror: it lands in the board's origin field as well, which stays the one every reader
9713/// takes it from, and it is here so that GitHub's issue search — which indexes this comment
9714/// and catches up with a write in seconds rather than minutes — can find the item by it.
9715/// A reader of the release before this one drops the slot's copy and reads the field, so an
9716/// item written here still reads with exactly one origin there.
9717fn slot_metadata(
9718 incoming: &Incoming<'_>,
9719 own_repository: Option<&Repository>,
9720 fallback: &[DependencyEdge],
9721) -> BTreeMap<String, Value> {
9722 let mut metadata = incoming.metadata.clone();
9723 match metadata.remove(ORIGIN_KEY) {
9724 Some(Value::String(origin)) if !origin.is_empty() => {
9725 metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin));
9726 }
9727 _ => {}
9728 }
9729 match incoming.written.kind() {
9730 BoardKind::Work(kind) => metadata.insert(
9731 ItemKind::METADATA_KEY.to_owned(),
9732 Value::String(kind.marker().to_owned()),
9733 ),
9734 // A document is told by its title, so it carries no kind marker: that key names
9735 // what a dependency endpoint points at, and nothing may point at a document.
9736 BoardKind::Document => metadata.remove(ItemKind::METADATA_KEY),
9737 };
9738 let derivable = own_repository
9739 .map(|own| incoming.repositories == [own.clone()])
9740 .unwrap_or(incoming.repositories.is_empty());
9741 if derivable {
9742 metadata.remove(Repository::METADATA_KEY);
9743 } else {
9744 metadata.insert(
9745 Repository::METADATA_KEY.to_owned(),
9746 Value::Array(
9747 incoming
9748 .repositories
9749 .iter()
9750 .map(|repository| Value::String(repository.as_str().to_owned()))
9751 .collect(),
9752 ),
9753 );
9754 }
9755 // The typed lists are what land, whatever the caller's own metadata held under their
9756 // keys: a key of either name travelling beside the field would otherwise be a second
9757 // answer to the same question, and the field is the one the contract names.
9758 for (key, entries) in [
9759 (TaskRef::DELIVERS_KEY, incoming.delivers),
9760 (TaskRef::DELIVERED_BY_KEY, incoming.delivered_by),
9761 ] {
9762 set_task_list(&mut metadata, key, entries);
9763 }
9764 record_edges(&mut metadata, fallback);
9765 metadata
9766}
9767
9768/// Hold the far ends no relationship here can name under [`DependencyEdge::RECORDED_KEY`] in
9769/// one slot's metadata, or no such key when there are none.
9770fn record_edges(metadata: &mut BTreeMap<String, Value>, fallback: &[DependencyEdge]) {
9771 if fallback.is_empty() {
9772 metadata.remove(DependencyEdge::RECORDED_KEY);
9773 } else {
9774 metadata.insert(
9775 DependencyEdge::RECORDED_KEY.to_owned(),
9776 Value::Array(
9777 fallback
9778 .iter()
9779 .map(|edge| json!({"id":edge.to.id(),"kind":edge.to.kind}))
9780 .collect(),
9781 ),
9782 );
9783 }
9784}
9785
9786/// Every label one item carries, from its content's own connection and nowhere else.
9787///
9788/// There is no second place to read one from: no document this source sends selects the
9789/// board's built-in `Labels` field, because GitHub derives it from the content and a draft
9790/// cannot carry one at all. The module documentation records the three schema facts that
9791/// settle it.
9792fn labels(content: &Value) -> Result<Vec<Label>, SourceError> {
9793 optional_nodes(content.get("labels"), "content labels")?
9794 .into_iter()
9795 .flatten()
9796 .map(|v| {
9797 Ok(Label {
9798 id: NativeId(required_str(v, "id")?.to_owned()),
9799 name: required_str(v, "name")?.to_owned(),
9800 color: optional_str(v, "color")?.map(str::to_owned),
9801 })
9802 })
9803 .collect()
9804}
9805
9806/// The definition of each board field one item's values are values of, in the shape a read
9807/// of the board's own `fields` gives one.
9808///
9809/// A value names its field through a fragment on that field's own type, so the type is
9810/// known from which kind of value it is: a single-select value's field is a
9811/// `ProjectV2SingleSelectField`, options and all, and a text value's is a `ProjectV2Field`.
9812/// A value whose field carried no id, or an empty one, says nothing usable and is left out.
9813fn field_definitions(field_values: &[Value]) -> Vec<Value> {
9814 field_values
9815 .iter()
9816 .filter_map(|value| {
9817 let field = value.get("field")?.as_object()?;
9818 field.get("id")?.as_str().filter(|id| !id.is_empty())?;
9819 let typename = if value.get("text").is_some() {
9820 "ProjectV2Field"
9821 } else if value.get("name").is_some() {
9822 "ProjectV2SingleSelectField"
9823 } else {
9824 return None;
9825 };
9826 let mut defined = field.clone();
9827 defined.insert("__typename".to_owned(), json!(typename));
9828 Some(Value::Object(defined))
9829 })
9830 .collect()
9831}
9832
9833fn text_field(field_values: &[Value], name: &str) -> Result<Option<String>, SourceError> {
9834 let Some(node) = field_values
9835 .iter()
9836 .find(|node| node.pointer("/field/name").and_then(Value::as_str) == Some(name))
9837 else {
9838 return Ok(None);
9839 };
9840 Ok(optional_str(node, "text")?.map(str::to_owned))
9841}
9842
9843fn valid_github_owner(owner: &str) -> bool {
9844 !owner.is_empty()
9845 && owner.len() <= 39
9846 && !owner.starts_with('-')
9847 && !owner.ends_with('-')
9848 && !owner.contains("--")
9849 && owner
9850 .bytes()
9851 .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
9852}
9853
9854/// GitHub's repository-name grammar: 1-100 ASCII letters, digits, `-`, `_` or `.`, and
9855/// neither of the two names a path segment already means.
9856fn valid_github_repository_name(name: &str) -> bool {
9857 !name.is_empty()
9858 && name.len() <= 100
9859 && name != "."
9860 && name != ".."
9861 && name
9862 .bytes()
9863 .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
9864}
9865
9866fn valid_environment_name(name: &str) -> bool {
9867 let mut bytes = name.bytes();
9868 bytes
9869 .next()
9870 .is_some_and(|byte| byte.is_ascii_alphabetic() || byte == b'_')
9871 && bytes.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
9872}
9873
9874/// How many sub-issues one issue has.
9875///
9876/// `Issue.subIssuesSummary` is `SubIssuesSummary!` and its `total` is `Int!`, so an
9877/// absent or non-integer one is a response this source cannot read — and reading it as
9878/// zero would classify a project as a task, which is exactly the mistake the marker
9879/// exists to keep from happening quietly.
9880fn sub_issue_total(issue: &Value) -> Result<u64, SourceError> {
9881 let summary = issue
9882 .get("subIssuesSummary")
9883 .ok_or_else(|| SourceError::Malformed {
9884 message: "GitHub issue is missing subIssuesSummary".into(),
9885 })?;
9886 summary
9887 .get("total")
9888 .and_then(Value::as_u64)
9889 .ok_or_else(|| SourceError::Malformed {
9890 message: "GitHub issue subIssuesSummary.total is not an unsigned integer".into(),
9891 })
9892}
9893
9894/// One issue's own `number`.
9895///
9896/// An issue always has one: GitHub declares `Issue.number` as `Int!` and every selection of
9897/// an issue in this module asks for it. So a read of one that comes back without it, or
9898/// with something that is not an unsigned integer, is a response this source cannot read —
9899/// absence here is **not** "this issue has no number". A draft is the content that has
9900/// none, and a draft never reaches this: the caller decides on `__typename` first, the way
9901/// it does for `subIssuesSummary`, which `DraftIssue` equally declares nothing for.
9902fn issue_number(issue: &Value) -> Result<u64, SourceError> {
9903 issue
9904 .get("number")
9905 .and_then(Value::as_u64)
9906 .ok_or_else(|| SourceError::Malformed {
9907 message: "GitHub issue number is missing or is not an unsigned integer".into(),
9908 })
9909}
9910
9911/// The `number` a creating mutation answered with, and `None` when it answered without one;
9912/// why a missing one is tolerated is at the call in `create_and_file_issue`.
9913fn created_issue_number(created: &Value) -> Result<Option<u64>, SourceError> {
9914 match created.get("number") {
9915 None | Some(Value::Null) => Ok(None),
9916 Some(value) => value
9917 .as_u64()
9918 .map(Some)
9919 .ok_or_else(|| SourceError::Malformed {
9920 message: "GitHub created issue number is not an unsigned integer".into(),
9921 }),
9922 }
9923}
9924
9925fn required_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
9926 value
9927 .get(field)
9928 .and_then(Value::as_str)
9929 .ok_or_else(|| SourceError::Malformed {
9930 message: format!("GitHub response is missing string field {field}"),
9931 })
9932}
9933
9934fn required_nonblank_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
9935 let found = required_str(value, field)?;
9936 if found.trim().is_empty() {
9937 return Err(SourceError::Malformed {
9938 message: format!("GitHub response has blank string field {field}"),
9939 });
9940 }
9941 Ok(found)
9942}
9943
9944/// The slot's delimiters, which `docs/metadata.md` settles once for every source that
9945/// needs one — Linear spells them too, in its own description field.
9946///
9947/// Restated rather than shared, because a plugin crate depends on the contract crate and
9948/// nothing else of this workspace. `scripts/check-metadata-slot-encoding.sh`, a target in
9949/// `check`, is what keeps the two one encoding: drift is otherwise quiet, since each
9950/// source round-trips its own writes perfectly well under its own spelling.
9951const METADATA_OPEN: &str = "<!-- onetaskgraph.metadata\n";
9952const METADATA_CLOSE: &str = "\n-->";
9953
9954/// What the composer puts between a non-empty visible body and the slot, and the one thing
9955/// the parser takes off the visible body when it takes the slot off — exactly once, so every
9956/// other trailing byte of the body comes back as it was written.
9957// 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.
9958const METADATA_SEPARATOR: &str = "\n\n";
9959
9960/// The visible body and the metadata slot at the end of it.
9961///
9962/// The encoding is the one `docs/metadata.md` settles for Linear, which is where its
9963/// reasons are. Only a comment at the very end is a slot; one in the middle is a person's
9964/// own content and is left alone. The visible body is everything before the slot less the
9965/// one [`METADATA_SEPARATOR`] the composer put there, byte for byte.
9966fn metadata_body(
9967 body: Option<String>,
9968) -> Result<(Option<String>, BTreeMap<String, Value>), SourceError> {
9969 let Some(body) = body else {
9970 return Ok((None, BTreeMap::new()));
9971 };
9972 let Some(slot) = slot_span(&body)? else {
9973 return Ok((Some(body), BTreeMap::new()));
9974 };
9975 let metadata =
9976 serde_json::from_str(&body[slot.encoded_start..slot.encoded_end]).map_err(|error| {
9977 SourceError::Malformed {
9978 message: format!(
9979 "invalid canonical JSON in GitHub issue onetaskgraph metadata slot: {error}"
9980 ),
9981 }
9982 })?;
9983 let before = &body[..slot.start];
9984 let visible = before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before);
9985 Ok(((!visible.is_empty()).then(|| visible.to_owned()), metadata))
9986}
9987
9988/// Where the metadata slot sits in one body, as byte offsets into it.
9989struct SlotSpan {
9990 /// Where [`METADATA_OPEN`] begins.
9991 start: usize,
9992 /// Where the encoded JSON begins, just past [`METADATA_OPEN`].
9993 encoded_start: usize,
9994 /// Where the encoded JSON ends, at the start of [`METADATA_CLOSE`].
9995 encoded_end: usize,
9996 /// Just past [`METADATA_CLOSE`].
9997 end: usize,
9998}
9999
10000/// The slot at the very end of `body`, or `None` when it has none.
10001///
10002/// The one reading of *where the slot is*, shared by [`metadata_body`], which reads it, and
10003/// [`with_slot`], which rewrites it — so the two cannot disagree about which comment is the
10004/// slot.
10005fn slot_span(body: &str) -> Result<Option<SlotSpan>, SourceError> {
10006 let Some(start) = body.rfind(METADATA_OPEN) else {
10007 return Ok(None);
10008 };
10009 let encoded_start = start + METADATA_OPEN.len();
10010 let Some(relative_end) = body[encoded_start..].find(METADATA_CLOSE) else {
10011 return Err(SourceError::Malformed {
10012 message: "unterminated onetaskgraph metadata slot in GitHub issue body".into(),
10013 });
10014 };
10015 let encoded_end = encoded_start + relative_end;
10016 let end = encoded_end + METADATA_CLOSE.len();
10017 if !body[end..].trim().is_empty() {
10018 return Ok(None);
10019 }
10020 Ok(Some(SlotSpan {
10021 start,
10022 encoded_start,
10023 encoded_end,
10024 end,
10025 }))
10026}
10027
10028/// `body` with its metadata slot holding exactly `metadata`, and every byte outside the
10029/// slot as it was.
10030///
10031/// A slot that is there has its JSON replaced in place; one that becomes empty is removed
10032/// together with the one [`METADATA_SEPARATOR`] separating it from the prose before it. A
10033/// body with no slot gains one the way [`compose_body`] writes it — after that separator,
10034/// or alone in an empty body — and a body with no slot that is given no metadata is
10035/// returned as it is.
10036fn with_slot(body: &str, metadata: &BTreeMap<String, Value>) -> Result<String, SourceError> {
10037 let encoded = if metadata.is_empty() {
10038 None
10039 } else {
10040 Some(
10041 serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
10042 message: error.to_string(),
10043 })?,
10044 )
10045 };
10046 Ok(match (slot_span(body)?, encoded) {
10047 (Some(slot), Some(encoded)) => format!(
10048 "{}{encoded}{}",
10049 &body[..slot.encoded_start],
10050 &body[slot.encoded_end..]
10051 ),
10052 (Some(slot), None) => {
10053 let before = &body[..slot.start];
10054 format!(
10055 "{}{}",
10056 before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before),
10057 &body[slot.end..]
10058 )
10059 }
10060 (None, None) => body.to_owned(),
10061 (None, Some(encoded)) if body.is_empty() => {
10062 format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
10063 }
10064 (None, Some(encoded)) => {
10065 format!("{body}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
10066 }
10067 })
10068}
10069
10070/// `body` with everything before its metadata slot replaced by `content`, and the slot
10071/// itself kept byte for byte.
10072///
10073/// The inverse of how [`metadata_body`] splits a body: the slot, when there is one, follows
10074/// `content` after the one [`METADATA_SEPARATOR`] the composer puts there — or alone, when
10075/// `content` is empty — so a read of the result reports `content` as the visible body and
10076/// the slot's metadata exactly as it was.
10077fn with_content(body: &str, content: &str) -> Result<String, SourceError> {
10078 let Some(slot) = slot_span(body)? else {
10079 return Ok(content.to_owned());
10080 };
10081 let kept = &body[slot.start..];
10082 Ok(if content.is_empty() {
10083 kept.to_owned()
10084 } else {
10085 format!("{content}{METADATA_SEPARATOR}{kept}")
10086 })
10087}
10088
10089/// Hold `entries` under `key` in one slot's metadata, or no such key when there are none.
10090fn set_task_list(metadata: &mut BTreeMap<String, Value>, key: &str, entries: &[TaskRef]) {
10091 if entries.is_empty() {
10092 metadata.remove(key);
10093 } else {
10094 metadata.insert(
10095 key.to_owned(),
10096 Value::Array(
10097 entries
10098 .iter()
10099 .map(|entry| Value::String(entry.as_str().to_owned()))
10100 .collect(),
10101 ),
10102 );
10103 }
10104}
10105
10106fn compose_body(
10107 content: Option<&str>,
10108 metadata: &BTreeMap<String, Value>,
10109) -> Result<Option<String>, SourceError> {
10110 let visible = content.unwrap_or_default();
10111 if metadata.is_empty() {
10112 return Ok((!visible.is_empty()).then(|| visible.to_owned()));
10113 }
10114 let encoded = serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
10115 message: error.to_string(),
10116 })?;
10117 Ok(Some(if visible.is_empty() {
10118 format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
10119 } else {
10120 format!("{visible}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
10121 }))
10122}
10123
10124fn required_bool(value: &Value, field: &str) -> Result<bool, SourceError> {
10125 value
10126 .get(field)
10127 .and_then(Value::as_bool)
10128 .ok_or_else(|| SourceError::Malformed {
10129 message: format!("GitHub response is missing boolean field {field}"),
10130 })
10131}
10132fn optional_str<'a>(value: &'a Value, field: &str) -> Result<Option<&'a str>, SourceError> {
10133 match value.get(field) {
10134 None | Some(Value::Null) => Ok(None),
10135 Some(value) => value
10136 .as_str()
10137 .map(Some)
10138 .ok_or_else(|| SourceError::Malformed {
10139 message: format!("GitHub response field {field} is not a string or null"),
10140 }),
10141 }
10142}
10143fn optional_nodes<'a>(
10144 connection: Option<&'a Value>,
10145 name: &str,
10146) -> Result<Option<&'a Vec<Value>>, SourceError> {
10147 match connection {
10148 None | Some(Value::Null) => Ok(None),
10149 Some(value) => value
10150 .get("nodes")
10151 .and_then(Value::as_array)
10152 .map(Some)
10153 .ok_or_else(|| SourceError::Malformed {
10154 message: format!("GitHub {name}.nodes is not an array"),
10155 }),
10156 }
10157}
10158fn complete_connection(connection: &Value, name: &str, size: u32) -> Result<(), SourceError> {
10159 let page_info = connection
10160 .get("pageInfo")
10161 .ok_or_else(|| SourceError::Malformed {
10162 message: format!("GitHub {name} has no pageInfo"),
10163 })?;
10164 if required_bool(page_info, "hasNextPage")? {
10165 return Err(SourceError::Malformed {
10166 message: format!(
10167 "GitHub {name} exceeds the supported nested connection size of {size}"
10168 ),
10169 });
10170 }
10171 Ok(())
10172}
10173fn optional_time(value: &Value, field: &str) -> Result<Option<DateTime<Utc>>, SourceError> {
10174 optional_str(value, field)?
10175 .map(|timestamp| {
10176 timestamp.parse().map_err(|error| SourceError::Malformed {
10177 message: format!("GitHub response field {field} is not a timestamp: {error}"),
10178 })
10179 })
10180 .transpose()
10181}
10182fn validate_page(page: &PageRequest) -> Result<(), SourceError> {
10183 if page.limit == 0 {
10184 Err(SourceError::Config {
10185 message: "page limit must be at least 1".into(),
10186 })
10187 } else {
10188 Ok(())
10189 }
10190}
10191fn next_cursor(connection: &Value) -> Result<Option<Cursor>, SourceError> {
10192 let page = connection
10193 .get("pageInfo")
10194 .filter(|value| value.is_object())
10195 .ok_or_else(|| SourceError::Malformed {
10196 message: "GitHub connection is missing pageInfo".into(),
10197 })?;
10198 if required_bool(page, "hasNextPage")? {
10199 let cursor = required_str(page, "endCursor")?;
10200 validate_cursor_progress(None, cursor)?;
10201 Ok(Some(Cursor(cursor.into())))
10202 } else {
10203 Ok(None)
10204 }
10205}
10206fn validate_cursor_progress(previous: Option<&str>, next: &str) -> Result<(), SourceError> {
10207 if next.is_empty() || previous == Some(next) {
10208 Err(SourceError::Malformed {
10209 message: "GitHub pagination cursor is empty or did not advance".into(),
10210 })
10211 } else {
10212 Ok(())
10213 }
10214}
10215/// The version of this plugin's opaque narrowing-search cursor.
10216pub const SEARCH_CURSOR_VERSION: u32 = 4;
10217
10218#[derive(Serialize, Deserialize)]
10219#[serde(tag = "state", rename_all = "snake_case", deny_unknown_fields)]
10220enum SearchConnection {
10221 Initial {},
10222 Continuing { after: Cursor },
10223 Exhausted {},
10224}
10225impl SearchConnection {
10226 fn after(&self) -> Option<&str> {
10227 match self {
10228 Self::Continuing { after } => Some(&after.0),
10229 _ => None,
10230 }
10231 }
10232 fn exhausted(&self) -> bool {
10233 matches!(self, Self::Exhausted { .. })
10234 }
10235 /// Whether a cursor naming this position, `offset` rows into its page, is one this
10236 /// plugin could have handed out: a page is resumed only part of the way through it — an
10237 /// offset of a whole page or more would skip rows nobody was given — an initial page
10238 /// only once some of it was handed out, and an exhausted connection has no page to be
10239 /// part of the way through.
10240 fn valid_resume(&self, offset: usize) -> bool {
10241 let within = offset < SEARCH_PAGE_SIZE as usize;
10242 match self {
10243 Self::Initial { .. } => offset > 0 && within,
10244 Self::Continuing { after } => !after.0.is_empty() && within,
10245 Self::Exhausted { .. } => offset == 0,
10246 }
10247 }
10248}
10249
10250/// Versioned source cursor. A zero offset and empty own-write ids are omitted.
10251#[derive(Serialize, Deserialize)]
10252#[serde(deny_unknown_fields)]
10253struct SearchPosition {
10254 version: u32,
10255 connection: SearchConnection,
10256 /// How many rows of the page `connection` starts were already handed out.
10257 #[serde(default, skip_serializing_if = "is_zero")]
10258 offset: usize,
10259 #[serde(default, skip_serializing_if = "Vec::is_empty")]
10260 seen: Vec<NativeId>,
10261 #[serde(default, skip_serializing_if = "Vec::is_empty")]
10262 own: Vec<NativeId>,
10263}
10264impl Default for SearchPosition {
10265 fn default() -> Self {
10266 Self {
10267 version: SEARCH_CURSOR_VERSION,
10268 connection: SearchConnection::Initial {},
10269 offset: 0,
10270 seen: Vec::new(),
10271 own: Vec::new(),
10272 }
10273 }
10274}
10275
10276fn is_zero(offset: &usize) -> bool {
10277 *offset == 0
10278}
10279
10280fn numeric_cursor(cursor: Option<&Cursor>) -> Result<usize, SourceError> {
10281 cursor.map_or(Ok(0), |c| {
10282 c.0.parse().map_err(|_| SourceError::Config {
10283 message: "page cursor is invalid".into(),
10284 })
10285 })
10286}
10287fn offset_page<T>(mut items: Vec<T>, offset: usize, limit: usize) -> Page<T> {
10288 if offset > items.len() {
10289 return Page::last(vec![]);
10290 }
10291 let tail = items.split_off(offset);
10292 let mut selected = tail;
10293 let next = (selected.len() > limit).then(|| Cursor((offset + limit).to_string()));
10294 selected.truncate(limit);
10295 Page {
10296 items: selected,
10297 next,
10298 }
10299}
10300
10301/// What [`TaskSource::end_command`] leaves of this source's held state, asserted on the state
10302/// itself, for the two things no journey can observe.
10303///
10304/// The journeys in `crates/onetaskgraph-e2e/tests/e2e/end_command.rs` prove through the engine,
10305/// with and without the call, that a settlement, a board listing and a metadata search each
10306/// read afresh after it — the resolved records, the written-item overlay, the board and its
10307/// search, and the narrowed searches. What they cannot reach is the held field definitions,
10308/// because a status write naming an option a person deleted is refused the same whether or
10309/// not the list is held, and a poisoned lock, because nothing outside the source can panic
10310/// while one of its locks is held. So these assert those directly, and every other holder
10311/// beside them so a holder added later without a clear in the call fails here.
10312#[cfg(test)]
10313mod end_command_tests {
10314 use super::*;
10315
10316 struct Token;
10317
10318 impl SecretResolver for Token {
10319 fn get(&self, var: &str) -> Option<SecretString> {
10320 (var == "GH_PROJECTS_TOKEN").then(|| "test-token".into())
10321 }
10322 }
10323
10324 fn source() -> GitHubProjectsSource {
10325 let config = serde_json::from_value(json!({
10326 "owner": "octo-org", "project_number": 7, "repository": "acme/work",
10327 // Nothing here is sent: the source is only built and its state inspected.
10328 "endpoint": "http://127.0.0.1:9/graphql",
10329 }))
10330 .expect("a usable configuration");
10331 GitHubProjectsSource::new(&SourceName::new("work").unwrap(), config, &Token)
10332 .expect("the source builds")
10333 }
10334
10335 /// One issue as a board read answers it.
10336 fn resolved(source: &GitHubProjectsSource) -> Resolved {
10337 source
10338 .resolve(&json!({
10339 "id": "ITEM-1",
10340 "content": {"__typename": "Issue", "id": "I_1", "title": "Held",
10341 "body": "what a person may since have edited", "state": "OPEN",
10342 "stateReason": null, "url": null, "number": 1,
10343 "subIssuesSummary": {"total": 0},
10344 "labels": {"nodes": [], "pageInfo": {"hasNextPage": false}}},
10345 "fieldValues": {"nodes": [], "pageInfo": {"hasNextPage": false}},
10346 }))
10347 .expect("the item reads")
10348 .expect("an issue")
10349 }
10350
10351 /// Hold something in every holder the call clears, and the repository id it keeps.
10352 fn fill(source: &GitHubProjectsSource) {
10353 let item = resolved(source);
10354 source.created.lock().unwrap().push(item.clone());
10355 source.updated.lock().unwrap().push(item.clone());
10356 *source.board_cache.lock().unwrap() = Some(Board {
10357 id: "PVT-board".into(),
10358 fields: json!({"nodes": []}),
10359 items: vec![item.clone()],
10360 });
10361 *source.search_cache.lock().unwrap() = Some(vec![item.clone()]);
10362 source
10363 .narrowed_cache
10364 .lock()
10365 .unwrap()
10366 .insert("status:todo".into(), vec![item.clone()]);
10367 source
10368 .search_next
10369 .lock()
10370 .unwrap()
10371 .insert("status:todo".into(), Some("cursor".into()));
10372 source
10373 .resolved_cache
10374 .lock()
10375 .unwrap()
10376 .insert(item.id.clone(), item);
10377 *source.fields_cache.lock().unwrap() = Some(BoardFields {
10378 id: BoardId::parse("PVT-board").unwrap(),
10379 fields: json!({"nodes": []}),
10380 });
10381 source
10382 .repository_cache
10383 .lock()
10384 .unwrap()
10385 .insert(RepositoryTarget::parse("acme/work").unwrap(), "R_1".into());
10386 }
10387
10388 fn assert_dropped(source: &GitHubProjectsSource) {
10389 assert!(source.created().unwrap().is_empty(), "created");
10390 assert!(source.updated().unwrap().is_empty(), "updated");
10391 assert!(source.board_cache().unwrap().is_none(), "board");
10392 assert!(source.search_cache.lock().unwrap().is_none(), "search");
10393 assert!(source.narrowed_cache.lock().unwrap().is_empty(), "narrowed");
10394 assert!(
10395 source.search_next.lock().unwrap().is_empty(),
10396 "search paging"
10397 );
10398 assert!(
10399 source.resolved_cache().unwrap().is_empty(),
10400 "resolved records"
10401 );
10402 assert!(source.fields_cache().unwrap().is_none(), "board fields");
10403 assert_eq!(
10404 source.repository_cache().unwrap().len(),
10405 1,
10406 "a repository's node id stays valid and is kept"
10407 );
10408 }
10409
10410 fn end(source: &GitHubProjectsSource) {
10411 tokio::runtime::Builder::new_current_thread()
10412 .build()
10413 .unwrap()
10414 .block_on(source.end_command())
10415 .expect("the command ends");
10416 }
10417
10418 #[test]
10419 fn the_call_drops_every_item_search_and_board_read_and_keeps_repository_ids() {
10420 let source = source();
10421 fill(&source);
10422 end(&source);
10423 assert_dropped(&source);
10424 }
10425
10426 #[test]
10427 fn the_call_clears_a_lock_an_earlier_failure_poisoned() {
10428 fn poison<T: Send>(held: &Mutex<T>) {
10429 std::thread::scope(|scope| {
10430 let _ = scope
10431 .spawn(|| {
10432 let _guard = held.lock().unwrap();
10433 panic!("a failure while the lock is held");
10434 })
10435 .join();
10436 });
10437 assert!(held.is_poisoned());
10438 }
10439 let source = source();
10440 fill(&source);
10441 poison(&source.created);
10442 poison(&source.updated);
10443 poison(&source.board_cache);
10444 poison(&source.search_cache);
10445 poison(&source.narrowed_cache);
10446 poison(&source.search_next);
10447 poison(&source.resolved_cache);
10448 poison(&source.fields_cache);
10449 assert!(
10450 source.resolved_cache().is_err(),
10451 "a poisoned lock is refused before the call"
10452 );
10453 end(&source);
10454 assert_dropped(&source);
10455 }
10456}