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.
81//!
82// 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.
83//! **Status.** `status_mapping` is per-instance configuration from a status category to
84//! `null` or a board `Status` option name. `done` selects its mapped option and closes the
85//! issue as `COMPLETED`; `cancelled` selects its mapped option and closes it as
86//! `NOT_PLANNED`. Every open category reopens a closed issue before selecting its option.
87//! A missing mapped option refuses the write before either representation changes. Reads
88//! give a closed issue's reason precedence over its option, while an open issue's option
89//! decides its category. The guarded [`GitHubProjectsSource::status_options`] operation is
90//! the one path here that calls `updateProjectV2Field`: GitHub replaces the whole option
91//! list, so it preserves every existing option id and verifies the field and item
92//! assignments immediately afterwards. It counts a terminal category's mapped option as
93//! configured, because a terminal write refuses without it. No ordinary source read or
94//! write calls that mutation, whose
95//! `singleSelectOptions` *overwrites* a field's option set, so no addition is additive
96//! and a mistake destroys every item's status. A status this board cannot represent is a
97//! refusal naming the status and the instance instead.
98//!
99//! `unknown` is disabled by default because this source cannot preserve an open-ended
100//! status word: it writes an existing board option and never
101//! creates an option. An operator may map `unknown` to one existing option, in which case
102//! every unknown word lands on that option and reads back as `unknown` under the option's
103//! name. This differs from `local-md`, which writes and reads the original word itself.
104//!
105//! The shipped terminal options are exactly `done: Done` and `cancelled: Cancelled`.
106//! `done` also closes the issue because GitHub derives `subIssuesSummary.completed`
107//! and the board's own `Sub-issues progress` field from closed sub-issues: a plan whose
108//! finished tasks were only moved to a "Done" column would read 0% complete forever.
109// llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
110//!
111//! # What this source declares, field by field
112//!
113//! One verdict per field of [`Capabilities`], and what `Native` means when this source
114//! says it. *Proven* means a shared journey drives it against the real
115//! binary over this source's own row in `crates/onetaskgraph/tests/e2e/fixtures.rs`, and
116//! `every_row_declares_exactly_what_its_plugin_reports` is what keeps this list and
117//! [`capabilities`](TaskSource::capabilities) from parting.
118//!
119//! | Field | Verdict |
120//! | --- | --- |
121//! | `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. |
122//! | `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. |
123//! | `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. |
124//! | `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. |
125//! | `filter_by_priority` | **Supported and proven,** over the priority each task reads as — `none` for every task of an instance without `priority_mapping`. |
126//! | `orphan_tasks` | **Supported and proven.** A task issue with no `parent` is in no project. |
127//! | `filter_by_label` | **Supported and proven,** over the issue's own labels. |
128//! | `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`. |
129//! | `search_title` | **Supported and proven,** over `Issue.title`. |
130//! | `search_content` | **Supported and proven,** over the visible body — the trailing metadata comment is not part of it. |
131//! | `task_dependencies` | **Supported and proven,** in both directions: `blockedBy` and `blocking`. |
132//! | `project_dependencies` | **Supported and proven,** in both directions, over the same two connections, because a project here is an issue. |
133//! | `max_page_size` | **Supported and proven.** [`MAX_PAGE_SIZE`], GitHub's own connection maximum. |
134//!
135//! Nothing here is unsupported. `documents` and `comments` are not predicates — they say this
136//! source has documents and that its tasks have comments, both of which hold — and the three
137//! facts behind the uniform `Native` on the
138//! predicates beside it are recorded below rather than re-derived, because a reader who
139//! takes `Native` to mean *the remote service filters* will read that uniformity as a
140//! lie.
141//!
142//! First, the plugin contract defines `Support::Native` as *the source applies this
143//! predicate itself*, and says nothing about where it applies it. What the declaration
144//! promises the engine is capability rule 1 — a predicate declared `Native` **is** applied
145//! — so that the engine may push it down and apply nothing of its own.
146//!
147//! Second, this source can keep that promise for every predicate at no additional API
148//! cost, because whichever of the three reads below answers a query has already read every
149//! item that query could keep before it filters anything. Filtering those items is
150//! in-process work over data already in hand.
151//!
152//! Third, no predicate but `projects` could be pushed into the API even if that were
153//! wanted, and `projects` is pushed down: `ProjectV2.items` takes `first` and `after` and
154//! offers no filter argument of any kind, GitHub's issue search offers no qualifier for a
155//! label set, a status column or a substring of a body, and its title qualifier matches
156//! tokens where this source — and the local Markdown source beside it — match substrings,
157//! so pushing a search down would silently *narrow* the answer. What a project filter has
158//! instead is a relationship: a project's tasks are that issue's sub-issues, and asking
159//! the issue for them is both cheaper and exact. So there is one predicate this source
160//! applies by asking a narrower question, six it applies in process, and none it is unable
161//! to apply. Declaring one `Unsupported` would make the engine compensate for work this
162//! source has already done, and declaring `projects` native while ignoring the filter
163//! (which this source once did) silently returns another project's tasks, because the
164//! engine trusts the declaration and applies nothing locally.
165//!
166//! # The three ways this source reaches an item, and what each costs
167//!
168//! A board read is charged for what its *nested* connections could return rather than for
169//! what was asked, so one whole-board read costs the same whether the question was about
170//! one project or about all of them. That is why a question about one project is never
171//! answered by reading the board:
172//!
173//! | The question | What is sent | What it costs |
174//! | --- | --- | --- |
175//! | one item, by its own id | [`graphql::ISSUE`] — `node(id:)` — and, when that node is a board draft, [`graphql::DRAFT`] — the draft and the one board item it is | the item |
176//! | 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` | the board's fields |
177//! | one project's tasks or documents | [`graphql::SUB_ISSUES`] — that issue's own `subIssues` | that project |
178//! | which projects this board holds | [`graphql::SEARCH_ISSUES`] — an issue search scoped to the board | the board's issues, without their board items |
179//! | every task, every document, every label | [`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 |
180//! | 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 |
181//!
182//! The board half of an issue — its board item's id, its `Status` option and this
183//! source's origin text field — rides along on `Issue.projectItems` in the first three, so
184//! an item reached any of those ways resolves through the same
185//! [`GitHubProjectsSource::resolve`] the board walk uses and reports the same title, the
186//! same status, the same labels and the same qualified id. That connection comes back a
187//! *page* at a time, at `BOARD_ITEMS_PAGE_SIZE`, so the entry for this board is looked for
188//! on the page in hand and — only if that page reports more of the connection — in the
189//! last row's read of that one issue's memberships, resumed from the page's own cursor and
190//! walked to exhaustion. An issue with no entry for *this* board is not this source's to
191//! report, which is what keeps an id naming another repository's issue from being answered
192//! as an item of this board; and because the page is where the search starts rather than
193//! where it ends, that answer is one about a connection read to exhaustion and never about
194//! an unread page. Nothing costs the extra read but an issue on more boards than a page
195//! holds: an issue this board really does not hold reports no next page, so its
196//! memberships are already exhausted where they arrived.
197//!
198//! **No document here selects the board's own `Labels` field, and nothing is lost by
199//! that.** An item's labels are read from its content alone, wherever that content is
200//! reached: the three documents above select `Issue.labels` on the fragment, and
201//! [`graphql::BOARD`] selects the same connection on the `... on Issue` arm of its
202//! `content`. A board's `Labels` field is not one anybody fills in: it is a built-in
203//! `ProjectV2FieldType`, it is absent from `ProjectV2CustomFieldType` so no project can
204//! create one, and `ProjectV2FieldValue` — the whole of what
205//! `updateProjectV2ItemFieldValue` accepts — offers no way to write one. So GitHub derives
206//! it from the content, for every content type it exists on, and there is nothing it can
207//! hold that the content does not already say: for an `Issue` it *is* that issue's own
208//! labels, so selecting it beside them unions a set with itself.
209//!
210//! **A draft loses nothing by that either**, which is the reasoning this paragraph once had
211//! backwards. `DraftIssue` exposes no `labels` field, and by the three schema facts above
212//! it cannot carry a board `Labels` value to be derived from one — so a draft has nothing
213//! to select *and nothing to lose*, and reports no labels at all. A `PullRequest` item is
214//! discarded by [`GitHubProjectsSource::resolve`] before labels are read. Both halves are
215//! held to that by tests in `tests/plugin.rs`: the four ways an item is reached report one
216//! label set, and that set is the fixture issue's own, by
217//! `an_item_reports_the_same_labels_title_status_and_id_however_it_is_reached`; and a board
218//! item whose content is a draft reports an empty set, by
219//! `a_board_item_whose_content_is_a_draft_reports_no_labels_at_all`. The absence of the
220//! selection is held over [`graphql::DOCUMENTS`] by
221//! `no_document_selects_the_boards_own_labels_field`.
222//!
223//! The whole-board row is still the board's own item connection, and deliberately: a
224//! **draft** board item is not an issue, so no search can list one, and the reads that have
225//! to answer for the whole board are the ones whose cost is the board's size anyway.
226//!
227//! **A question about one item this source already names by id never lists the board.**
228//! Whether that item is on this board, and what its board fields are, is answered by reading
229//! that item — its own `Issue.projectItems`, walked to exhaustion by
230//! [`GitHubProjectsSource::resolve_issue`], or a draft's own board item — and never by
231//! looking for it in [`graphql::BOARD`]'s `items` or in a listing this command already
232//! holds. That covers a write's destination, the project a new item is filed under, a
233//! same-source far end a dependency names, a status write, the dependency slot a draft keeps,
234//! and the delete that takes back an item a copy made. What such a write needs of the board
235//! and the item does not carry — the board's id, the `Status` and origin field definitions —
236//! comes from [`graphql::BOARD_FIELDS`], which reads no item at all. The reason is evidence,
237//! not economy alone: `ProjectV2.items` is a projection that lags the membership GitHub
238//! itself reports — an issue added with `addProjectV2ItemById` can be missing from it for
239//! minutes. Scanning this host's 842-item board has refused a document copy and an update
240//! even though the items' own reads named that board. A scan there gives the wrong answer
241//! as well as paying for every page. So a `board.items` lookup does not belong on any of
242//! those paths.
243//!
244//! **What a read may return is capped too, and that cap is on the document rather than on
245//! the board.** GitHub limits the number of nodes **one query may return** to
246//! [`NODE_COUNT_LIMIT`] and refuses a query above that before executing it: the answer is
247//! an error naming the connection the count crossed at, not a slow or a partial result.
248//! Every board this source reads is refused the same way, so no board is too big for these
249//! documents and none is small enough to save one that is over.
250//!
251//! The count is arithmetic over the document's own text: each connection contributes the
252//! `first:` it asks for, counts **multiply** down a nested path and **sum** across sibling
253//! paths. Those are [GitHub's published rules][node-limits] and this workspace does not
254//! restate them — `github-graphql-node-count` implements them, and
255//! [`worst_case_node_count`] under [`largest_page_sizes`] is where every node count here
256//! comes from. `every_document_this_source_sends_stays_under_githubs_node_limit`, in
257//! `tests/node_count.rs`, recomputes every document in [`graphql::DOCUMENTS`] from that
258//! same text on every run and fails naming any that reaches the limit — so a connection
259//! added to a shared fragment is caught there rather than by GitHub.
260//!
261//! What decides those counts is the page sizes: [`MAX_PAGE_SIZE`] on the outer page,
262//! `NESTED_PAGE_SIZE` on the connections hanging off one item, and
263//! `BOARD_ITEMS_PAGE_SIZE` on the page of an issue's board memberships a read carries.
264//! `$nestedFirst` is spent twice down one path of a board read, so that constant is
265//! effectively squared there, which is why it is the one the limit is most sensitive to.
266//! `BOARD_ITEMS_PAGE_SIZE` is small for a reason of its own, recorded beside it: what a
267//! page of memberships misses is recovered by one further read rather than refused, so it
268//! buys a bound every read pays for at the price of a request only a multi-board issue
269//! pays.
270//!
271//! **`nodeCount` and `cost` are two numbers against two limits, and both are computed
272//! offline here — per document, one document at a time.** `nodeCount` is the one above: the
273//! most nodes one query may return, checked per query and bounded by [`NODE_COUNT_LIMIT`].
274//! `cost` is rate-limit points, metered per hour across everything one credential does; it
275//! is what the two limiters [`Limiter`] tells apart meter, and a document under
276//! [`NODE_COUNT_LIMIT`] still says nothing about its price. [`worst_case_point_cost`] is
277//! that second number, and `tests/point_cost.rs` pins every document in
278//! [`graphql::DOCUMENTS`] at what it costs — there being no per-call point ceiling to hold
279//! one under, the pin itself is the check. The credentialed lane reconciles both figures
280//! against GitHub's own, off a probe it already sends.
281//!
282//! **What is pinned that way is a per-document price and never a session's.** The record in
283//! `session-cost.md` measures the two quantities a whole session can be counted in offline —
284//! **requests** and **worst-case nodes** — and neither is points. What one whole session
285//! consumes of the hourly point allowance is observable only from a credentialed run's own
286//! `x-ratelimit-*` headers, which is what [`accounting`] fills its per-budget figures from
287//! and what `tests/live.rs` prints at the end of every run.
288//!
289//! [node-limits]: https://docs.github.com/en/graphql/overview/rate-limits-and-node-limits-for-the-graphql-api
290//!
291//! **Where a read-after-write guarantee comes from, since neither of GitHub's two
292//! enumerations of a board can supply one alone.** Resolving a node id is strongly
293//! consistent, so a read by id and a project's own sub-issues are already current. The
294//! other two are not, and they are behind by different amounts and in different directions:
295//!
296//! - GitHub's **issue search** is an index and answers a write made moments ago with the
297//! value from before it — usually for a second or two.
298//! - **`ProjectV2.items`** is a projection GitHub rebuilds behind the write, and an item put
299//! on a board with `addProjectV2ItemById` can be **absent** from it — not present with its
300//! content withheld, absent, with the connection walked to its own `hasNextPage: false` —
301//! for *minutes*, while `Issue.projectItems` names the same membership at once.
302//!
303//! That second one is a measurement rather than a caution. This repository's own
304//! credentialed journey writes a project and waits for the board to report it, then writes a
305//! task and waits for the same thing seconds later on the same board: the project wait is
306//! answered through the search and converged in two or three attempts in each of three runs,
307//! and the task wait is answered through `ProjectV2.items` and converged in none of them
308//! inside thirty. Separately, an item added to a second and larger board was read back by
309//! `Issue.projectItems` on that board's own id while every one of that connection's nine
310//! pages, walked to exhaustion nine minutes after the add, did not name it. Reading a board
311//! through the lagging one alone is what had a board read deny an issue that had certainly
312//! landed on it.
313//!
314//! So [`GitHubProjectsSource::board`] is the **union** of both — each search result still
315//! admitted only on this board's own strongly-consistent `Issue.projectItems`, and neither
316//! enumeration dropped, because only `ProjectV2.items` lists a board draft and only the
317//! search reports what the projection is behind on. What closes the last
318//! gap, the one where both are behind, is [`GitHubProjectsSource::created`]: every read this
319//! source answers is completed with what this process itself wrote, so an item created
320//! seconds ago is reported whether or not GitHub has caught up. Nothing else is remembered,
321//! nothing is written down, and the record dies with the process. **A wait that has to
322//! observe GitHub's own data cannot be answered from that record** — which is why the
323//! credentialed journey asks through a source built afresh, and why the union above rather
324//! than a longer wait is what makes such a wait converge.
325//!
326//! Filtering happens before paging, so a page of a filtered result is a page of the
327//! survivors rather than the survivors of a page. Label and text matching answer the same
328//! question the same way the local Markdown source's do, so one cross-source expectation
329//! holds for both.
330//!
331//! <!-- llmlint: ignore[contracts_have_one_source_or_a_drift_gate] The declaration itself
332//! has one source, `capabilities`, and the note above is the reasoning behind it rather
333//! than a second copy of it: without the three facts recorded here a reader takes the
334//! uniform `Native` for a lie and reverts it. The drift gate on the declaration is this
335//! crate's own capabilities test, which pins every field of it against a fully spelled-out
336//! `Capabilities` literal — a struct with no `Default`, so a field added to the contract
337//! fails to compile there rather than going unasserted. -->
338//! The fixture-server tests above run wherever this crate is selected; the credentialed
339//! lane runs in the same required check, beside them, and can fail it — it verifies the
340//! 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,
341//! one filed under neither, a label on one of the three and a closed status on another —
342//! because that shape is what tells an honoured predicate from an ignored one: a board
343//! holding a single project answers a project filter the same way whether or not this
344//! source applies it, which is exactly how the defect above went unseen.
345//!
346//! That lane writes only to the board `GH_PROJECTS_OWNER` and `GH_PROJECTS_NUMBER` name,
347//! and only into the repository `GH_PROJECTS_REPOSITORY` names, and skips — as it does
348//! without `GH_PROJECTS_TOKEN` — when any of them is absent. Requiring both to be
349//! nominated is what keeps a credentialed write lane off a board and a repository nobody
350//! nominated; it never asks GitHub which project was updated most recently. Before it
351//! starts, the lane also clears any item titled — and any repository label named — the way
352//! it titles and names its own artifacts, which is self-healing after an interrupted run:
353//! a process killed between its writes and its cleanup leaves artifacts the next run
354//! removes.
355//!
356//! # What a session of requests costs, and where the report is
357//!
358//! This source records **every** request it sends into [`accounting::Accounting`], at
359//! `send_once` — the one place a request leaves this crate, which is why a read path added
360//! later is counted without anybody remembering to count it. That is the whole of what this
361//! crate adds to the arrangement; [`accounting`] is where what a record carries, how a
362//! session's spend is arrived at, and what it deliberately does not know are set out.
363//!
364//! What one whole session of the live journey costs, counted that way against this crate's
365//! loopback fixture board, is written down in `session-cost.md` beside this crate — with the
366//! reduction it came out of, and with what it does and does not say about rate-limit points.
367//!
368//! [`GitHubProjectsSource::accounting`] is the read: a snapshot to hold and compare, which
369//! [`accounting::Session::report`] renders the session report from. It is on the ordinary
370//! code path — no environment variable, no feature, no build configuration — because an
371//! instrument nobody switches on measures nothing, and
372//! [`Plugin::build_recording_into`] is how a caller making its own calls beside this
373//! source's counts the whole session rather than this source's share. The credentialed lane
374//! in `tests/live.rs` does exactly that, and prints the report at the end of every run,
375//! passed or failed.
376//!
377//! **A live session refuses to start unless the account can afford it.** Before it does any
378//! of the work it exists to do, the journey makes one request — `GET /rate_limit`, which
379//! GitHub documents as not counting against the REST rate limit and which answers both of
380//! its budgets at once — and starts only if, for each of them, what remains minus this
381//! session's estimated cost is still at least
382//! `onetaskgraph_live::RETAINED_BUFFER` — twenty per cent — of that budget's whole
383//! allowance. A session that cannot **declines**: it did not run, so it is
384//! neither a pass nor a failing assertion, and it says which budget was short, that budget's
385//! limit, what remained, the estimate, the buffer and when it resets — then stops, without
386//! waiting for the budget to come back. The estimate is derived offline from
387//! `tests/fixtures/session-cost.txt` and a cost model stated in `tests/journey/budget.rs`,
388//! which is also where the published rule that model rests on is cited; the accounting
389//! above records the gate's own read like any other request, and
390//! [`accounting::Session::report`] prints the estimate beside what the session really spent.
391//!
392//! **GitHub is the authority on both of its own numbers, and the credentialed lane goes and
393//! asks it.** Everything above computes `nodeCount` and `cost` offline from a document's own
394//! text, which is what lets it run on every platform and on a pull request from a fork with
395//! no credential — and that is what actually stops a regression merging. But an offline
396//! arithmetic can only ever agree with itself: if GitHub changes its rules, this workspace
397//! goes on computing the old answer and nothing notices. So `tests/live.rs` reconciles them.
398//! GitHub's schema exposes `rateLimit(dryRun: true)`, whose `nodeCount` is *"the maximum
399//! number of nodes this query may return"* and whose `cost` is what that document would
400//! spend, both for a document **without executing it**, and the lane asks it for every query
401//! document this source sends, under the largest bindings this source sends, and fails when
402//! GitHub's figure and [`worst_case_node_count`] or [`worst_case_point_cost`] disagree. A
403//! mutation is skipped, because `rateLimit` is a field of `Query` and cannot be asked about
404//! one; the offline pins still cover it. It records what those calls reported about the
405//! account's own allowance, because whether asking is free is a thing to observe rather than
406//! to assume. Two quantities, not one: [`NODE_COUNT_LIMIT`] bounds `nodeCount` per query,
407//! and `cost` is metered against an hourly allowance the accounting above reads off a
408//! credentialed run's own response headers.
409//!
410//! **GitHub has two rate limiters and this source is refused by both, so nothing here
411//! treats them as one thing.** The primary budget is the hourly allowance `gh api
412//! rate_limit` reports; the secondary limiter is a burst limiter over content-generating
413//! requests, and *nothing* reports it. Which one refused decides the operator's next step,
414//! so [`Limiter`] is a type rather than a detail, and it is what [`MIN_MUTATION_INTERVAL_MS`],
415//! [`GitHubProjectsSource::board_cache`] and [`GitHubProjectsSource::graphql`] each answer
416//! one part of.
417#![deny(missing_docs)]
418
419use std::collections::BTreeMap;
420use std::sync::{Arc, Mutex};
421use std::time::{Duration, Instant};
422
423use chrono::{DateTime, Utc};
424use onetaskgraph_plugin_api::{
425 Capabilities, Comment, CommentBody, Cursor, DependencyEdge, DependencyEndpoint, DependencyKind,
426 DependencySupport, Direction, Document, DocumentQuery, Health, ItemKind, ItemWrite, Label,
427 LabelFilter, Location, MetadataKey, Metering, NativeId, NewComment, Page, PageRequest,
428 Priority, Project, ProjectFilter, ProjectQuery, Repository, SecretResolver, SourceError,
429 SourceName, SourcePlugin, Status, StatusCategory, Support, Task, TaskQuery, TaskRef,
430 TaskSource, TextFields, TextQuery, WriteSupport,
431};
432use reqwest::{Client, StatusCode, Url};
433use schemars::{Schema, schema_for};
434use secrecy::{ExposeSecret, SecretString};
435use serde::{Deserialize, Serialize};
436use serde_json::{Value, json};
437
438pub mod accounting;
439
440use accounting::Accounting;
441
442/// The registry name for this plugin.
443pub const KIND: &str = "github-projects";
444/// GitHub's maximum connection page size.
445pub const MAX_PAGE_SIZE: u32 = 100;
446
447/// The most nodes any one document this source sends may be asked to return.
448///
449/// GitHub's own published per-query ceiling, taken from
450/// [`github_graphql_node_count::NODE_LIMIT`] rather than written out again here, so this
451/// workspace cannot hold a stale copy of somebody else's number. A query above it is
452/// **refused before it is executed**, whoever is asking and whatever board they are
453/// asking about — so this is a bound on the documents rather than a budget that runs out.
454///
455/// This is `nodeCount`, the maximum number of nodes *one query may return*. It is not
456/// `cost`, the rate-limit points a call spends against an hourly allowance shared by
457/// everything the credential does — two numbers against two limits, and this constant
458/// bounds only the first. The second is computed offline too, per document:
459/// [`worst_case_point_cost`], pinned for every document in [`graphql::DOCUMENTS`] by
460/// `tests/point_cost.rs`, and reconciled against GitHub's own `cost` by the credentialed
461/// lane. There is no constant like this one to hold a price under, because points are an
462/// hourly allowance rather than a per-call bound.
463///
464/// Neither is a session's price. What `session-cost.md` records of a whole session is its
465/// **requests** and its **worst-case nodes**; what a whole session spends in points is
466/// reported only by a credentialed run's own `x-ratelimit-*` headers, through
467/// [`accounting`]. The module section on the three ways this source reaches an item says how
468/// the count is arrived at, and which of the page sizes below decide it.
469pub const NODE_COUNT_LIMIT: u64 = github_graphql_node_count::NODE_LIMIT;
470
471/// Nested connection size for the connections that hang off one item.
472///
473/// It multiplies through every document that reaches an item under a page — the count
474/// rules multiply down a nested path — so it is the constant [`NODE_COUNT_LIMIT`] is most
475/// sensitive to. `tests/node_count.rs` is what holds the pair together: it recomputes
476/// every document under these constants and fails naming any that reaches the limit, so
477/// raising this is caught there rather than by GitHub.
478const NESTED_PAGE_SIZE: u32 = 50;
479/// How many of one issue's board memberships are read when an issue is reached directly.
480///
481/// An issue reached through a search or through its own node id carries its board half in
482/// `Issue.projectItems`, and only the entry for *this* board is read. This connection sits
483/// under a page of issues, so every point of it multiplies through the whole document and
484/// is paid for whether or not any issue is on a second board — which is why it is
485/// deliberately far smaller than [`NESTED_PAGE_SIZE`].
486///
487/// **Three, because what a page misses is now recovered rather than refused**, and the
488/// recovery is what the value is chosen against. An issue whose entry for this board sits
489/// past this page costs one further request — [`graphql::ISSUE_BOARD_ITEMS`], resumed from
490/// that page's own cursor — so the value trades a bound every read pays for a request only
491/// a multi-board issue pays. At one, a deployment whose issues commonly sit on two or more
492/// boards would pay that request *per issue*, which is order N against the one page per
493/// hundred issues a read costs today. At three it is only reached by an issue on four or
494/// more boards at once, which keeps the recovery path exceptional rather than routine for
495/// a plausible deployment.
496const BOARD_ITEMS_PAGE_SIZE: u32 = 3;
497
498pub use github_graphql_node_count::{NodeCountError, Variables};
499
500/// The largest value this source can bind to each page-size variable its documents name.
501///
502/// Every `first:` in [`graphql`] reads one of these three, and each is capped at the
503/// constant above it wherever a caller's own limit could reach it — `$first` at
504/// [`MAX_PAGE_SIZE`], `$nestedFirst` at `NESTED_PAGE_SIZE`, `$boardItems` at
505/// `BOARD_ITEMS_PAGE_SIZE`. So this is the worst case a caller can drive this source to,
506/// not one configuration of it, which is what makes a bound computed under it a bound on
507/// every read.
508pub fn largest_page_sizes() -> Variables {
509 Variables::from([
510 ("first".to_owned(), MAX_PAGE_SIZE),
511 ("nestedFirst".to_owned(), NESTED_PAGE_SIZE),
512 ("boardItems".to_owned(), BOARD_ITEMS_PAGE_SIZE),
513 ])
514}
515
516/// The most nodes `document` could be asked to return, by GitHub's published rules.
517///
518/// Computed offline from the document's own text under [`largest_page_sizes`] — no
519/// network, no credential and no schema — by
520/// [`github_graphql_node_count::node_count`], which is where the rules themselves live.
521/// A document at or above [`NODE_COUNT_LIMIT`] is one GitHub refuses before executing, so
522/// this is what a check holds every document in [`graphql::DOCUMENTS`] below.
523///
524/// # Errors
525///
526/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
527/// no single operation, or binds a page size this source does not name — each of which is
528/// a defect in the document rather than a number.
529pub fn worst_case_node_count(document: &str) -> Result<u64, NodeCountError> {
530 node_count(document, &largest_page_sizes())
531}
532
533/// The most rate-limit points one call of `document` could spend, by GitHub's published
534/// rules.
535///
536/// Computed offline from the document's own text under [`largest_page_sizes`] — no
537/// network, no credential and no schema — by
538/// [`github_graphql_node_count::point_cost`], which is where the rules themselves live.
539/// This is `cost`, metered **per hour** against the allowance one credential shares across
540/// everything it does; it is not `nodeCount`, which is [`worst_case_node_count`] and is
541/// bounded per query by [`NODE_COUNT_LIMIT`]. There is no per-call ceiling to hold this
542/// under, so what `tests/point_cost.rs` does with it is pin every document in
543/// [`graphql::DOCUMENTS`] at what it costs, and the credentialed lane reconciles those
544/// figures against GitHub's own reported `cost`.
545///
546/// # Errors
547///
548/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
549/// no single operation, or binds a page size this source does not name — each of which is
550/// a defect in the document rather than a number.
551pub fn worst_case_point_cost(document: &str) -> Result<u64, NodeCountError> {
552 github_graphql_node_count::point_cost(document, &largest_page_sizes())
553}
554
555/// The most nodes `document` could be asked to return under `variables`.
556///
557/// [`worst_case_node_count`] is this under [`largest_page_sizes`], and the accounting in
558/// [`accounting`] is this under the bindings one request really sent — one spelling of the
559/// calculation, so a bound checked offline and a cost recorded at run time cannot come to
560/// disagree. The rules themselves live in [`github_graphql_node_count::node_count`].
561///
562/// # Errors
563///
564/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
565/// no single operation, or binds a page size `variables` does not name.
566pub fn node_count(document: &str, variables: &Variables) -> Result<u64, NodeCountError> {
567 github_graphql_node_count::node_count(document, variables)
568}
569
570/// The issue-title prefix that makes a board issue a document.
571///
572/// A GitHub Projects board has no document type — it holds issues — so the discriminator
573/// is the title, and this is the whole of it: an issue whose title begins with these bytes
574/// is a document and every other issue is the task or project the sub-issue rule makes it.
575///
576/// It is spelled **once**, here, and read rather than restated everywhere else — including
577/// by the shared journeys, which take it from this constant so a board fixture cannot
578/// drift from what this source reads. `docs/metadata.md` records the two consequences that
579/// are not obvious from the bytes: the reported title has this prefix taken off, exactly
580/// as the body's metadata slot is taken off `content`, and this prefix is read *before*
581/// the sub-issue rule, so a design issue with no sub-issues is never an empty project.
582pub const DESIGN_TITLE_PREFIX: &str = "DESIGN: ";
583
584/// Exact GraphQL query documents issued by this plugin.
585///
586/// Keeping the production documents here lets the pinned-schema test validate the same
587/// bytes that are sent to GitHub, rather than a test-only copy which could drift
588/// independently. [`STATUS_OPTIONS_UPDATE`] is the sole document that may rewrite a board
589/// field, and its guarded caller always supplies the complete existing option set with ids.
590pub mod graphql {
591 /// The board half of one item: the field values every document here reads it from.
592 ///
593 /// A macro for the same reason [`board_issue!`] below is one, a level further in. This
594 /// selection is needed by that fragment, by [`BOARD`] under the board's own `items`,
595 /// and by [`ISSUE_BOARD_ITEMS`] under a membership walk — and all three have to produce
596 /// *the same value*, because
597 /// [`GitHubProjectsSource::resolve`](super::GitHubProjectsSource) reads them through
598 /// one path. Three spellings of it is what would drift, so there is one.
599 ///
600 /// The `Status` option and this source's own origin text field are the whole of it. It
601 /// selects no `ProjectV2ItemFieldLabelValue`: GitHub derives that field from the item's
602 /// content, so it holds nothing the content's own `labels` do not already say, and it
603 /// would sit a label connection two page sizes deep.
604 macro_rules! board_item_values {
605 () => {
606 r#"fieldValues(first:$nestedFirst){nodes{
607 ... on ProjectV2ItemFieldSingleSelectValue{name field{
608 ... on ProjectV2SingleSelectField{id name options{id name}}
609 }}
610 ... on ProjectV2ItemFieldTextValue{text field{... on ProjectV2Field{id name}}}
611 }pageInfo{hasNextPage}}"#
612 };
613 }
614
615 /// Everything this source reads about one issue, wherever it reaches that issue.
616 ///
617 /// A macro rather than a constant so the three documents below can `concat!` it: one
618 /// spelling of these fields is what makes an issue read through the board-scoped
619 /// search, through its own node id, and through its project's sub-issue relationship
620 /// resolve to *the same* item, which is the whole of what
621 /// [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource) relies on.
622 ///
623 /// `projectItems` is what carries the board half of an issue: the board item's own id
624 /// and the [`board_item_values!`] above — the `Status` option and this source's origin
625 /// text field — that a `ProjectV2.items` read used to carry. It is asked for on the
626 /// issue rather than on the board, which is what makes the cost of a read proportional
627 /// to what was asked for instead of to the board's size.
628 ///
629 /// It carries a *page* of that connection, at `BOARD_ITEMS_PAGE_SIZE`, and its
630 /// `endCursor` is what [`ISSUE_BOARD_ITEMS`] resumes from when this board's entry is
631 /// not on that page: a page here is where the search for the entry starts rather than
632 /// where it ends.
633 ///
634 /// It does **not** select the board's `Labels` field value, and that is the whole of
635 /// what keeps the three documents below under [`NODE_COUNT_LIMIT`](super::NODE_COUNT_LIMIT):
636 /// a label connection there sits under `fieldValues` under `projectItems` under a page
637 /// of issues, spending `$nestedFirst` twice down one path, and took
638 /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] to 2,556,100 nodes against a limit of 500,000.
639 /// No label is lost — this is a fragment `on Issue`, whose own `labels` are selected
640 /// above, and that connection is where every label this source reports comes from. No
641 /// document in this module selects the board field any longer, [`BOARD`] included; the
642 /// module documentation records why nothing it could have held is lost.
643 macro_rules! board_issue {
644 () => {
645 concat!(
646 r#" fragment BoardIssue on Issue{__typename id number title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total}
647 labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}
648 projectItems(first:$boardItems){nodes{id project{id number}
649 "#,
650 board_item_values!(),
651 r#"}pageInfo{hasNextPage endCursor}}}"#
652 )
653 };
654 }
655
656 /// Every issue of one board, found by a search scoped to that board.
657 ///
658 /// This is how the projects a board holds are listed, and it selects no `items`
659 /// connection on `ProjectV2`: the board is a *qualifier of the search* rather than a
660 /// container walked page by page, so nothing nested inside a board item is paid for.
661 /// Which of the issues it returns is a project is then read off `parent` — GitHub
662 /// accepts `-has:parent` as a search qualifier and silently ignores it, so the
663 /// discriminator has to be applied to the field, which is a scalar on the issue and
664 /// costs nothing.
665 pub const SEARCH_ISSUES: &str = concat!(
666 r#"query($search:String!,$type:SearchType!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!){
667 search(query:$search,type:$type,first:$first,after:$after){
668 pageInfo{hasNextPage endCursor}
669 nodes{__typename ...BoardIssue}
670 }
671 }"#,
672 board_issue!()
673 );
674
675 /// One issue by its own node id, which is what a qualified id names here.
676 ///
677 /// Strongly consistent, unlike the search above: GitHub's issue search is an index and
678 /// answers a write made moments ago with the value from before it, and resolving a node
679 /// id does not.
680 pub const ISSUE: &str = concat!(
681 r#"query($id:ID!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!){
682 node(id:$id){__typename ...BoardIssue}
683 }"#,
684 board_issue!()
685 );
686
687 /// One project's tasks: the sub-issues of the issue that project is.
688 ///
689 /// The work this costs is the project's own size. Nothing about it grows as the board
690 /// gains projects, or as those projects gain tasks.
691 pub const SUB_ISSUES: &str = concat!(
692 r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!){
693 node(id:$id){__typename
694 ... on Issue{subIssues(first:$first,after:$after){
695 pageInfo{hasNextPage endCursor}
696 nodes{__typename ...BoardIssue}
697 }}}
698 }"#,
699 board_issue!()
700 );
701
702 /// Reads the board's fields and one page of its items.
703 pub const BOARD: &str = concat!(
704 r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$nestedFirst:Int!,$duplicates:Boolean!){
705 owner:repositoryOwner(login:$owner){
706 ... on ProjectV2Owner{projectV2(number:$number){...Board}}
707 }
708 } fragment Board on ProjectV2 { id title
709 fields(first:$nestedFirst){nodes{
710 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
711 ... on ProjectV2Field{__typename id name}
712 }pageInfo{hasNextPage}}
713 items(first:$first,after:$after){nodes{id "#,
714 board_item_values!(),
715 r#" content{
716 ... 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}}}
717 ... on PullRequest{__typename id}
718 ... on DraftIssue{__typename id title body createdAt updatedAt}
719 }} pageInfo{hasNextPage endCursor}}
720 }"#
721 );
722
723 /// The board's own id and field definitions, and not one of its items.
724 ///
725 /// What a write needs of the board when the item it writes does not say: the id a field
726 /// write and `addProjectV2ItemById` address, and the definitions of the `Status` and
727 /// origin fields. It selects no `items`, so what it costs is the board's field list
728 /// however many items the board holds — and it decides nothing about which items those
729 /// are, which is the question a read of one item by its own id answers instead.
730 ///
731 /// The root is aliased `boardFields` rather than `owner`, so nothing counting the
732 /// board's item reads by their root counts this one among them.
733 pub const BOARD_FIELDS: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!){
734 boardFields:repositoryOwner(login:$owner){
735 ... on ProjectV2Owner{projectV2(number:$number){id
736 fields(first:$nestedFirst){nodes{
737 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
738 ... on ProjectV2Field{__typename id name}
739 }pageInfo{hasNextPage}}
740 }}
741 }
742 }"#;
743
744 /// One board draft by its own node id, with the board item it sits in.
745 ///
746 /// A draft is not an issue, so [`ISSUE`] reaches it and reads nothing of it; this is the
747 /// second read that answers it. `DraftIssue.projectV2Items` names the board item a draft
748 /// is — GitHub links a draft to one item — with the same [`board_item_values!`] the
749 /// issue fragment reads, so a draft reached by id resolves through the same resolver a
750 /// board listing hands it to, and nothing has to list the board to find one.
751 pub const DRAFT: &str = concat!(
752 r#"query($id:ID!,$nestedFirst:Int!,$boardItems:Int!){
753 node(id:$id){__typename ... on DraftIssue{id title body createdAt updatedAt
754 projectV2Items(first:$boardItems){nodes{id project{id number}
755 "#,
756 board_item_values!(),
757 r#"}pageInfo{hasNextPage endCursor}}}}
758 }"#
759 );
760
761 /// One issue's board memberships alone, walked past the page a read of it carried.
762 ///
763 /// The recovery read behind [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource):
764 /// every document above carries a *page* of `Issue.projectItems`, and an issue on more
765 /// boards than that page holds may have this board's entry past its end. This asks that
766 /// one issue for its memberships and nothing else — the caller already holds the issue —
767 /// so an answer of "this board does not hold it" is only ever given about a connection
768 /// read to exhaustion.
769 ///
770 /// It selects the board item's id, its project number and the same
771 /// [`board_item_values!`] the fragment does, because what it produces is handed to the
772 /// very same resolver: an issue recovered this way reports the same title, the same
773 /// status, the same labels and the same qualified id as one whose entry was on the
774 /// page.
775 ///
776 /// `$first` rather than `$boardItems`: this document reads one issue, so nothing
777 /// multiplies through it and the membership connection can be walked at
778 /// [`MAX_PAGE_SIZE`](super::MAX_PAGE_SIZE) — which is what keeps the recovery to one
779 /// further request for any issue a person really keeps.
780 pub const ISSUE_BOARD_ITEMS: &str = concat!(
781 r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!){
782 node(id:$id){
783 ... on Issue{projectItems(first:$first,after:$after){
784 nodes{id project{id number}
785 "#,
786 board_item_values!(),
787 r#"}
788 pageInfo{hasNextPage endCursor}}}
789 }
790 }"#
791 );
792 /// Resolves the configured repository's node id, which creating an issue requires.
793 pub const REPOSITORY: &str = r#"query($owner:String!,$name:String!){repository(owner:$owner,name:$name){id nameWithOwner}}"#;
794 /// Reads both dependency directions for one issue, with each far end's own kind — and
795 /// the issue's own body, which is where an edge to another source is recorded, so that
796 /// half of a dependency read needs no second read of the issue or of the board.
797 pub const ISSUE_DEPENDENCIES: &str = r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
798 ... on Issue{body
799 blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
800 blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
801 }}} fragment Related on Issue{id title body parent{id} subIssuesSummary{total}}"#;
802 /// Creates one issue in the configured repository.
803 pub const CREATE_ISSUE: &str =
804 r#"mutation($input:CreateIssueInput!){createIssue(input:$input){issue{id number url}}}"#;
805 /// Puts an existing issue on the configured board.
806 pub const ADD_TO_BOARD: &str = r#"mutation($input:AddProjectV2ItemByIdInput!){addProjectV2ItemById(input:$input){item{id}}}"#;
807 /// Updates an issue's visible fields and its open or closed state in one call.
808 pub const UPDATE_ISSUE: &str =
809 r#"mutation($input:UpdateIssueInput!){updateIssue(input:$input){issue{id}}}"#;
810 /// Updates an existing draft's user-visible fields.
811 pub const UPDATE_DRAFT: &str = r#"mutation($input:UpdateProjectV2DraftIssueInput!){updateProjectV2DraftIssue(input:$input){draftIssue{id}}}"#;
812 /// Updates a text or single-select value on one project item.
813 pub const UPDATE_FIELD: &str = r#"mutation($input:UpdateProjectV2ItemFieldValueInput!){updateProjectV2ItemFieldValue(input:$input){projectV2Item{id}}}"#;
814 /// Clears one project item's value of one field, which is what a `none` priority is.
815 pub const CLEAR_FIELD: &str = r#"mutation($input:ClearProjectV2ItemFieldValueInput!){clearProjectV2ItemFieldValue(input:$input){projectV2Item{id}}}"#;
816 /// Creates one single-select field with its options. Only the guarded field setup may use
817 /// this document, and only for a field the board lacks.
818 pub const CREATE_FIELD: &str = r#"mutation($input:CreateProjectV2FieldInput!){createProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id name options{id name color description}}}}}"#;
819 /// Replaces a single-select field's options. Only the guarded field setup — the
820 /// `status-options` and `fields` operations — may use this document, because GitHub
821 /// treats the input as the complete option list.
822 pub const STATUS_OPTIONS_UPDATE: &str = r#"mutation($input:UpdateProjectV2FieldInput!){updateProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id options{id name color description}}}}}"#;
823 /// A fresh snapshot of the Status field and every board item's assignment.
824 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}}}}}}"#;
825 /// Files one issue under another as a sub-issue, which is what project membership is.
826 pub const ADD_SUB_ISSUE: &str =
827 r#"mutation($input:AddSubIssueInput!){addSubIssue(input:$input){issue{id} subIssue{id}}}"#;
828 /// Takes one issue back out of its parent.
829 pub const REMOVE_SUB_ISSUE: &str = r#"mutation($input:RemoveSubIssueInput!){removeSubIssue(input:$input){issue{id} subIssue{id}}}"#;
830 /// Adds GitHub's native issue blocked-by relationship.
831 pub const ADD_BLOCKED_BY: &str = r#"mutation($input:AddBlockedByInput!){addBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
832 /// Removes one native issue blocked-by relationship.
833 pub const REMOVE_BLOCKED_BY: &str = r#"mutation($input:RemoveBlockedByInput!){removeBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
834 /// Deletes one issue, which takes its board item with it.
835 ///
836 /// The engine sends this in one situation only: undoing a copy that could not finish,
837 /// over the items that same copy created. Deleting the issue removes the board item
838 /// too, so there is no second `deleteProjectV2Item` to keep in step with it.
839 pub const DELETE_ISSUE: &str =
840 r#"mutation($input:DeleteIssueInput!){deleteIssue(input:$input){repository{id}}}"#;
841
842 /// Everything this source reads about one issue comment, wherever it reaches one.
843 ///
844 /// A macro for the reason [`board_issue!`] is one: a comment listed, a comment just added
845 /// and a comment just edited are handed to one mapper, so they are selected by one
846 /// spelling. `author` is `Actor`, which GitHub answers `null` for an account that no
847 /// longer exists, and `login` is the one member every kind of actor carries.
848 macro_rules! issue_comment {
849 () => {
850 "id author{login} createdAt updatedAt body url"
851 };
852 }
853
854 /// One task's comments: a page of its issue's own `comments` connection.
855 ///
856 /// **No `orderBy`, and that is what makes the page oldest first.** GitHub's only
857 /// `IssueCommentOrder` field is `UPDATED_AT`, which would move a comment to the end of the
858 /// list every time somebody edited it; left unordered the connection answers in the order
859 /// the comments were written, which is the order GitHub documents for the same collection
860 /// over REST — ascending id. Nothing multiplies through it, so `$first` is the whole of its
861 /// node count and the caller's own page size is pushed straight down.
862 pub const ISSUE_COMMENTS: &str = concat!(
863 r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename ... on Issue{comments(first:$first,after:$after){nodes{"#,
864 issue_comment!(),
865 r#"}pageInfo{hasNextPage endCursor}}}}}"#
866 );
867 /// Which issue one comment is on, read before that comment is edited or removed.
868 ///
869 /// GitHub's comment mutations take the comment's id and nothing else, so without this a
870 /// comment id given against the wrong task would change a comment on another issue.
871 pub const COMMENT_ISSUE: &str =
872 r#"query($id:ID!){node(id:$id){__typename ... on IssueComment{id issue{id}}}}"#;
873 /// Adds one comment to an issue, signed as the account the token belongs to.
874 pub const ADD_COMMENT: &str = concat!(
875 r#"mutation($input:AddCommentInput!){addComment(input:$input){subject{id} commentEdge{node{"#,
876 issue_comment!(),
877 r#"}}}}"#
878 );
879 /// Replaces the body of one issue comment.
880 pub const UPDATE_COMMENT: &str = concat!(
881 r#"mutation($input:UpdateIssueCommentInput!){updateIssueComment(input:$input){issueComment{"#,
882 issue_comment!(),
883 r#"}}}"#
884 );
885 /// Removes one issue comment. Its payload carries nothing about the comment it removed.
886 pub const DELETE_COMMENT: &str = r#"mutation($input:DeleteIssueCommentInput!){deleteIssueComment(input:$input){clientMutationId}}"#;
887
888 /// Every document above, with what this source is doing when it sends one.
889 ///
890 /// One list rather than a `match` beside the constants: a rate-limit diagnostic has to
891 /// name the call that was refused, and a `match` with a catch-all arm would answer a
892 /// document added later with "talking to GitHub" and never say so.
893 ///
894 /// `documents_are_all_inventoried` reads this file back and fails naming any `pub
895 /// const` here that this list omits, so the two cannot part — which is the same guard
896 /// `CATEGORIES` carries, in the one shape available to a set of `&str` constants.
897 pub const DOCUMENTS: [(&str, &str); 28] = [
898 (SEARCH_ISSUES, "searching this board's issues"),
899 (ISSUE, "reading one issue"),
900 (
901 ISSUE_BOARD_ITEMS,
902 "reading one issue's board memberships past the page it came with",
903 ),
904 (SUB_ISSUES, "reading a project's tasks"),
905 (BOARD, "reading the board"),
906 (BOARD_FIELDS, "reading the board's fields"),
907 (DRAFT, "reading one draft"),
908 (REPOSITORY, "reading the destination repository"),
909 (ISSUE_DEPENDENCIES, "reading an issue's dependencies"),
910 (CREATE_ISSUE, "creating an issue"),
911 (ADD_TO_BOARD, "adding an issue to the board"),
912 (UPDATE_ISSUE, "updating an issue"),
913 (UPDATE_DRAFT, "updating a draft item"),
914 (UPDATE_FIELD, "writing a board field"),
915 (CLEAR_FIELD, "clearing a board field"),
916 (
917 CREATE_FIELD,
918 "creating a board single-select field with its options",
919 ),
920 (
921 STATUS_OPTIONS_SNAPSHOT,
922 "snapshotting board Status options and assignments",
923 ),
924 (
925 STATUS_OPTIONS_UPDATE,
926 "safely replacing the board Status option list",
927 ),
928 (ADD_SUB_ISSUE, "filing an issue under its project"),
929 (REMOVE_SUB_ISSUE, "taking an issue out of its project"),
930 (ADD_BLOCKED_BY, "recording a dependency"),
931 (REMOVE_BLOCKED_BY, "removing a dependency"),
932 (DELETE_ISSUE, "deleting an issue"),
933 (ISSUE_COMMENTS, "reading a task's comments"),
934 (COMMENT_ISSUE, "reading which issue a comment is on"),
935 (ADD_COMMENT, "adding a comment"),
936 (UPDATE_COMMENT, "editing a comment"),
937 (DELETE_COMMENT, "deleting a comment"),
938 ];
939}
940
941/// Which of GitHub's two rate limiters refused a request.
942///
943/// Waiting is the whole answer to the primary budget, and polling is what *extends* the
944/// secondary one — so an operator told the wrong one takes the wrong next step, which is
945/// the whole reason this is carried rather than collapsed into "rate limited".
946#[derive(Debug, Clone, Copy, PartialEq, Eq)]
947enum Limiter {
948 /// The hourly API budget, which `gh api rate_limit` reports and a wait answers.
949 Primary,
950 /// The burst limiter over content-generating requests, which nothing reports.
951 Secondary,
952}
953
954/// The wordings GitHub answers a secondary rate limit with.
955///
956/// It sends them under a forbidden status, under a too-many-requests status, and inside
957/// the `errors` of a *successful* response, which is why the text is what this matches on
958/// rather than the status. `abuse detection` is the wording GitHub used before the
959/// limiter was renamed and still returns from some endpoints; `submitted too quickly` is
960/// what a burst of content creation is refused with.
961///
962/// This is GitHub's vocabulary rather than this source's, so it is pinned rather than
963/// remembered: `tests/fixtures/rate-limits.json` records where each wording was read and
964/// when, and the drift gate reconciles the two lists both ways. Public for that gate
965/// alone — a caller has no use for it, and matching on a refusal is this source's job.
966pub const SECONDARY_WORDINGS: [&str; 5] = [
967 "secondary rate limit",
968 "temporarily blocked from content creation",
969 "abuse detection",
970 "submitted too quickly",
971 "exceeded a secondary",
972];
973
974/// The wordings GitHub answers an exhausted primary budget with.
975///
976/// `rate_limited` is the `type` its GraphQL error carries, which is read as a field rather
977/// than looked for in the response text. Pinned and gated exactly as
978/// [`SECONDARY_WORDINGS`] is, and public for the same one reason.
979pub const PRIMARY_WORDINGS: [&str; 3] = [
980 "api rate limit exceeded",
981 "rate limit exceeded",
982 "rate_limited",
983];
984
985/// What a response *says about itself*, which is the only place a refusal can be read.
986///
987/// Deliberately not the whole response body. A board is a place people write about their
988/// own work, and a task on it titled "the secondary rate limit" would, matched across the
989/// raw text, turn a perfectly good answer into a refusal this source then waited out and
990/// reported. So the item data is never read: what is read is GitHub's own REST-style
991/// `message` envelope, which is what a forbidden status carries, and the `message` and
992/// `type` of each GraphQL error, which is where a *successful* response says it.
993///
994/// A body that is not JSON at all has nothing structured to read, so only a failing
995/// response's own text is taken — a successful response that is not JSON is malformed
996/// rather than refused, and [`GitHubProjectsSource::answer`] says so.
997fn refusal_wording(status: StatusCode, body: &str) -> String {
998 let Ok(parsed) = serde_json::from_str::<Value>(body) else {
999 return if status.is_success() {
1000 String::new()
1001 } else {
1002 body.to_owned()
1003 };
1004 };
1005 let mut said: Vec<&str> = parsed
1006 .get("message")
1007 .and_then(Value::as_str)
1008 .into_iter()
1009 .collect();
1010 if let Some(errors) = parsed.get("errors").and_then(Value::as_array) {
1011 for error in errors {
1012 said.extend(
1013 ["message", "type"]
1014 .into_iter()
1015 .filter_map(|key| error.get(key).and_then(Value::as_str)),
1016 );
1017 }
1018 }
1019 said.join("; ")
1020}
1021
1022impl Limiter {
1023 /// Which limiter refused this response, or `None` when none of them did.
1024 ///
1025 /// The wording is read first and the status only decides what carries none of it,
1026 /// because GitHub answers a secondary limit with a forbidden status far more often
1027 /// than with too-many-requests — while a forbidden status saying nothing about a limit
1028 /// really is a credential this token lacks.
1029 ///
1030 /// A response is a refusal because of its status or its own wording. A spent budget
1031 /// only ever explains one; it never turns an answer into a refusal.
1032 fn classify(status: StatusCode, budget_exhausted: bool, body: &str) -> Option<Self> {
1033 let normalized = refusal_wording(status, body).to_ascii_lowercase();
1034 if SECONDARY_WORDINGS
1035 .iter()
1036 .any(|wording| normalized.contains(wording))
1037 {
1038 return Some(Self::Secondary);
1039 }
1040 if status == StatusCode::TOO_MANY_REQUESTS {
1041 return Some(Self::Primary);
1042 }
1043 // An exhausted budget *explains* a response that failed; it does not make one that
1044 // succeeded into a failure. GitHub sets `x-ratelimit-remaining: 0` on the last
1045 // request the budget allowed as well as on the ones it then refuses, so reading
1046 // the header alone threw away a good answer — and, once refusals were retried,
1047 // replayed a request that had already taken effect.
1048 if !status.is_success() && budget_exhausted {
1049 return Some(Self::Primary);
1050 }
1051 // A successful response saying it: GitHub reports a GraphQL rate limit in the
1052 // `errors` of an HTTP 200, where nothing about the status says so at all.
1053 if status.is_success()
1054 && PRIMARY_WORDINGS
1055 .iter()
1056 .any(|wording| normalized.contains(wording))
1057 {
1058 return Some(Self::Primary);
1059 }
1060 None
1061 }
1062
1063 /// What this limiter is called where an operator can look it up.
1064 const fn name(self) -> &'static str {
1065 match self {
1066 Self::Primary => "GitHub's primary API rate limit",
1067 Self::Secondary => "GitHub's secondary rate limit",
1068 }
1069 }
1070
1071 /// What the endpoint an operator would go and check says about this limiter.
1072 const fn where_to_look(self) -> &'static str {
1073 match self {
1074 Self::Primary => {
1075 "That is the budget `gh api rate_limit` reports, so that endpoint says when it \
1076 comes back."
1077 }
1078 Self::Secondary => {
1079 "That limiter is not the primary API budget: `gh api rate_limit` reports the \
1080 primary budget and does not report this one, so budget showing there says \
1081 nothing about this refusal, and every further attempt extends it."
1082 }
1083 }
1084 }
1085
1086 /// The next step this limiter actually calls for.
1087 const fn what_to_do(self) -> &'static str {
1088 match self {
1089 Self::Primary => {
1090 "wait for the reset `gh api rate_limit` reports, then run the command again."
1091 }
1092 Self::Secondary => {
1093 "leave this board alone for a few minutes, then run the command again — or \
1094 raise pacing.min_mutation_interval_ms on this source so it writes more slowly."
1095 }
1096 }
1097 }
1098}
1099
1100/// One rate-limit refusal, and the wait GitHub asked for if it asked for one.
1101#[derive(Debug, Clone, Copy)]
1102struct Limited {
1103 limiter: Limiter,
1104 hint: Option<u64>,
1105}
1106
1107impl Limited {
1108 /// What the caller is told once this source has waited as long as it may.
1109 ///
1110 /// Both limiters report as [`SourceError::RateLimited`], because that is what
1111 /// happened: the kind a caller matches on says a rate limit refused this, and nothing
1112 /// about *which* limiter it was makes it a different kind of failure. What differs is
1113 /// the operator's next step, and that is what the message carries — a secondary
1114 /// refusal read as a primary one sends an operator to `gh api rate_limit`, where the
1115 /// budget looks fine, and then back to retry the very burst that was refused.
1116 fn exhausted(
1117 self,
1118 doing: &str,
1119 waits: u32,
1120 waited: Duration,
1121 needed: Duration,
1122 budget: Duration,
1123 ) -> SourceError {
1124 SourceError::RateLimited {
1125 retry_after_seconds: self.hint,
1126 message: Some(format!(
1127 "{} refused this source while {doing}; it waited {} out over {} and was refused \
1128 again, and the next wait of {} would take it past the {} one call may spend \
1129 waiting. {} next: {}",
1130 self.limiter.name(),
1131 plural(waits, "refusal"),
1132 seconds(waited),
1133 seconds(needed),
1134 seconds(budget),
1135 self.limiter.where_to_look(),
1136 self.limiter.what_to_do(),
1137 )),
1138 }
1139 }
1140}
1141
1142/// One HTTP attempt's result, with what its response said about the rate limit.
1143///
1144/// The two travel together so the record and the outcome are written from the same place:
1145/// what a response said about the budget is only readable while that response is in hand,
1146/// and what the attempt *meant* is only decidable once its body has been read.
1147struct Attempted {
1148 result: Result<Value, Attempt>,
1149 limits: accounting::RateLimit,
1150 /// GitHub's own reported cost for this call, for a document that asked for it.
1151 reported_cost: Option<u64>,
1152}
1153
1154/// One attempt's outcome: an error to report, or a rate limit to wait out.
1155enum Attempt {
1156 Failed(SourceError),
1157 Limited(Limited),
1158}
1159
1160fn plural(count: u32, thing: &str) -> String {
1161 if count == 1 {
1162 format!("{count} {thing}")
1163 } else {
1164 format!("{count} {thing}s")
1165 }
1166}
1167
1168fn seconds(duration: Duration) -> String {
1169 format!("{:.1}s", duration.as_secs_f64())
1170}
1171
1172/// A header GitHub spells as a whole number of seconds, or `None` when this one is not.
1173///
1174/// A value that is present and unreadable is deliberately *not* an error. `retry-after` is
1175/// allowed by HTTP to be a date rather than a count, an intermediary can rewrite either
1176/// header, and neither is what makes a response a refusal — so the whole cost of one this
1177/// cannot read is that the refusal carries no hint and the backing-off schedule answers it
1178/// instead. Refusing the response over the header would turn a readable refusal into an
1179/// unreadable one, and refusing to *wait* would be the one wrong direction to fail in.
1180fn whole_seconds(value: Option<&reqwest::header::HeaderValue>) -> Option<u64> {
1181 value
1182 .and_then(|value| value.to_str().ok())
1183 .and_then(|value| value.trim().parse::<u64>().ok())
1184}
1185
1186/// Every mutation this source sends creates content — an issue, a board item, a field of
1187/// one, a sub-issue link, a dependency, a comment — or edits or removes content of that
1188/// kind, and no query in [`graphql::DOCUMENTS`] does, so what the secondary limiter counts
1189/// and what the keyword says are the same set. That is what makes the keyword a sound test
1190/// rather than a convenient one: pacing an edit or a removal the limiter might not have
1191/// counted costs a wait, and not pacing one it did count costs the next fifty minutes.
1192fn is_mutation(query: &str) -> bool {
1193 query.trim_start().starts_with("mutation")
1194}
1195
1196/// What this source was doing, for a diagnostic that has to say so.
1197///
1198/// Read out of [`graphql::DOCUMENTS`], which is the inventory rather than a copy of it, so
1199/// a document added without a description is caught by that list's own gate instead of
1200/// falling through to the vague arm below.
1201fn operation_description(query: &str) -> &'static str {
1202 graphql::DOCUMENTS
1203 .iter()
1204 .find(|(document, _)| *document == query)
1205 .map_or("talking to GitHub", |(_, doing)| *doing)
1206}
1207
1208/// GitHub's published ceiling on content-generating requests, per minute.
1209///
1210/// Pinned in `tests/fixtures/rate-limits.json` and gated against it, because it is
1211/// GitHub's number rather than this source's: [`MIN_MUTATION_INTERVAL_MS`] is *derived*
1212/// from it, so a pacing value checked only against itself cannot go stale here.
1213pub const CONTENT_CREATION_PER_MINUTE: u64 = 80;
1214/// The same ceiling as GitHub publishes it per hour, which this source does **not** pace
1215/// at. See [`MIN_MUTATION_INTERVAL_MS`] for why the per-minute bound is the one that
1216/// governs; it is pinned beside its sibling so the gate would notice either one moving.
1217pub const CONTENT_CREATION_PER_HOUR: u64 = 500;
1218/// Shortest interval between two content-creating mutations, in milliseconds.
1219///
1220/// GitHub documents two secondary limits on content-generating requests:
1221/// [`CONTENT_CREATION_PER_MINUTE`] and [`CONTENT_CREATION_PER_HOUR`]. 60000/80 is 750, so
1222/// a mutation every 750 ms is the fastest rate that cannot exceed the per-minute bound,
1223/// and that is the bound a copy actually trips: a copy of one plan-sized project is a
1224/// burst of a few dozen mutations inside a few seconds. The hourly bound works out at one
1225/// every 7.2 seconds sustained, which no single copy reaches and which, used as the
1226/// spacing here, would turn an ordinary copy into an hour of waiting — so it is
1227/// deliberately *not* what this paces at. An installation that wants the hourly bound
1228/// honoured for a long sequence of copies says so through
1229/// `pacing.min_mutation_interval_ms`.
1230pub const MIN_MUTATION_INTERVAL_MS: u64 = 60_000 / CONTENT_CREATION_PER_MINUTE;
1231/// First wait when a rate-limit refusal carries no hint; each further wait doubles it.
1232///
1233/// A doubling schedule from one second reaches a minute in six waits, which is GitHub's
1234/// own advice for a secondary limit — wait, and wait longer each time — without spending
1235/// the first minute of a transient refusal doing nothing.
1236pub const RETRY_BACKOFF_MS: u64 = 1_000;
1237/// Total time one call may spend waiting out rate limits before it reports a failure.
1238///
1239/// Two minutes is long enough to ride out the refusals a paced copy still collects and
1240/// short enough that a command an operator is watching returns. The bound is what makes
1241/// the wait a wait rather than a hang: a call refused past it ends in a diagnostic naming
1242/// the limiter, not in a process nobody can tell from a wedged one.
1243pub const RETRY_BUDGET_MS: u64 = 120_000;
1244
1245fn default_token_env() -> String {
1246 "GH_PROJECTS_TOKEN".to_owned()
1247}
1248fn default_endpoint() -> String {
1249 "https://api.github.com/graphql".to_owned()
1250}
1251
1252/// Where one status category lands on this board.
1253///
1254/// `null` — an absent value — disables the category for this instance, and using a
1255/// disabled status is a refusal naming the status and the instance.
1256#[derive(Debug, Clone, Deserialize, schemars::JsonSchema)]
1257#[serde(untagged)]
1258pub enum StatusTargetConfig {
1259 /// The name of a `Status` single-select option already on the board.
1260 Column(ColumnName),
1261}
1262
1263/// The name of a `Status` single-select option on the board.
1264///
1265/// Validated on the way in rather than checked later, so a blank option name — which
1266/// nothing on a board can be — is a state this type cannot hold.
1267#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
1268#[serde(try_from = "String")]
1269#[schemars(extend("minLength" = 1))]
1270pub struct ColumnName(String);
1271
1272impl ColumnName {
1273 /// The option name, as the board spells it.
1274 fn as_str(&self) -> &str {
1275 &self.0
1276 }
1277}
1278
1279impl TryFrom<String> for ColumnName {
1280 type Error = String;
1281
1282 fn try_from(name: String) -> Result<Self, Self::Error> {
1283 if name.trim().is_empty() {
1284 return Err("a status_mapping option name cannot be blank".to_owned());
1285 }
1286 Ok(Self(name))
1287 }
1288}
1289
1290/// The two closed states this product can mean.
1291///
1292/// GitHub's `IssueClosedStateReason` also spells `DUPLICATE`, which is neither finished
1293/// work nor abandoned work, so nothing here ever writes it.
1294#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, schemars::JsonSchema)]
1295#[serde(rename_all = "kebab-case")]
1296pub enum ClosedState {
1297 /// `COMPLETED` — precisely done.
1298 Completed,
1299 /// `NOT_PLANNED` — precisely cancelled.
1300 NotPlanned,
1301}
1302
1303impl ClosedState {
1304 const fn reason(self) -> &'static str {
1305 match self {
1306 Self::Completed => "COMPLETED",
1307 Self::NotPlanned => "NOT_PLANNED",
1308 }
1309 }
1310}
1311
1312/// Configuration for one GitHub Projects v2 board.
1313#[derive(Debug, Clone, Default, Deserialize, schemars::JsonSchema)]
1314#[serde(default, deny_unknown_fields)]
1315pub struct GitHubProjectsConfig {
1316 /// Login of the user or organization which owns the board.
1317 pub owner: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates GitHub's owner grammar before private construction.
1318 /// The project number shown in the board's GitHub URL.
1319 pub project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` bounds this to a positive GraphQL Int.
1320 // 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.
1321 /// `owner/name` of the repository this source creates an issue in when the item's own
1322 /// `repositories` field does not decide it.
1323 ///
1324 /// An item naming exactly one repository is created there; a task or a document naming
1325 /// none or several is created in its parent project's repository; and a project, or a
1326 /// task or document with no parent, naming none or several is created here. A board
1327 /// has no repository of its own and `createIssue` requires one, so a write without
1328 /// this is refused naming the field. Reads never need it.
1329 pub repository: Option<String>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the `owner/name` grammar before private construction.
1330 // llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
1331 /// Environment variable containing a fine-grained token with Projects and Issues
1332 /// read/write plus Pull requests read-only access for every repository represented on
1333 /// the board.
1334 #[serde(default = "default_token_env")]
1335 pub token_env: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the environment-variable grammar.
1336 /// GraphQL endpoint. GitHub Enterprise installations may override it.
1337 #[serde(default = "default_endpoint")]
1338 pub endpoint: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` converts it to the private validated `Url`.
1339 /// Per-instance mapping from a status category to where it lands on this board.
1340 ///
1341 /// A category this does not mention keeps its shipped default: `backlog` to
1342 /// "Backlog", `todo` to "Todo", `queued` to "Queued", `in-progress` to "In Progress",
1343 /// `done` to "Done" plus closed as completed, `cancelled` to "Cancelled" plus closed
1344 /// as not planned, and `draft` and `unknown` disabled. `unknown` may name one existing
1345 /// board option; every unknown word then lands on that option and reads back as
1346 /// `unknown` under its name. Unlike `local-md`, this source cannot keep each unknown
1347 /// word because it never creates board options.
1348 #[serde(default)]
1349 pub status_mapping: BTreeMap<String, Option<StatusTargetConfig>>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` parses each key into a `StatusCategory` and reports an unknown one against this instance.
1350 /// Per-instance mapping from a task's priority to an option of this board's
1351 /// single-select field named `Priority`.
1352 ///
1353 /// Absent, this source holds no priority: every task reads as `none`, and a write of any
1354 /// other priority is refused before it reaches this board. Present, each of `urgent`,
1355 /// `high`, `medium` and `low` it does not mention keeps its shipped default — `Urgent`,
1356 /// `High`, `Medium` and `Low` — and an item with no value in the `Priority` field reads
1357 /// as `none`, so writing `none` clears the value. Option names match case-insensitively;
1358 /// no two levels may name one option. Reads and writes never create the field or an
1359 /// option: `onetaskgraph sources fields <source> --apply` does, and a write naming one
1360 /// the board lacks is refused pointing there.
1361 #[serde(default)]
1362 pub priority_mapping: Option<PriorityMappingConfig>,
1363 /// How fast this source writes, and how long it waits out a rate-limit refusal.
1364 ///
1365 /// Every field keeps its shipped default when it is absent, and the defaults are
1366 /// GitHub's own published limits rather than taste. See [`Pacing`].
1367 #[serde(default)]
1368 pub pacing: PacingConfig,
1369}
1370
1371/// Which option of the board's `Priority` field each priority lands on.
1372///
1373/// One member per level rather than a map, so a key that is not a level is refused where
1374/// the configuration is read, naming the levels there are. `none` is not a member: it is no
1375/// value in the field, not an option of it.
1376#[derive(Debug, Clone, Default, Deserialize, schemars::JsonSchema)]
1377#[serde(default, deny_unknown_fields)]
1378pub struct PriorityMappingConfig {
1379 /// The option `urgent` lands on; `Urgent` when absent.
1380 pub urgent: Option<PriorityOptionName>,
1381 /// The option `high` lands on; `High` when absent.
1382 pub high: Option<PriorityOptionName>,
1383 /// The option `medium` lands on; `Medium` when absent.
1384 pub medium: Option<PriorityOptionName>,
1385 /// The option `low` lands on; `Low` when absent.
1386 pub low: Option<PriorityOptionName>,
1387}
1388
1389/// The name of an option of the board's `Priority` single-select field.
1390///
1391/// Validated on the way in, for the reason [`ColumnName`] is: nothing on a board can have a
1392/// blank name.
1393#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
1394#[serde(try_from = "String")]
1395#[schemars(extend("minLength" = 1))]
1396pub struct PriorityOptionName(String);
1397
1398impl PriorityOptionName {
1399 /// The option name, as the board spells it.
1400 fn as_str(&self) -> &str {
1401 &self.0
1402 }
1403}
1404
1405impl TryFrom<String> for PriorityOptionName {
1406 type Error = String;
1407
1408 fn try_from(name: String) -> Result<Self, Self::Error> {
1409 if name.trim().is_empty() {
1410 return Err("a priority_mapping option name cannot be blank".to_owned());
1411 }
1412 Ok(Self(name))
1413 }
1414}
1415
1416/// The name of the board field a priority is held in.
1417pub const PRIORITY_FIELD: &str = "Priority";
1418
1419/// The four priorities a board option can hold, in the order a new `Priority` field lists
1420/// them. `none` is not among them: it is the field holding no value.
1421///
1422/// This list mirrors `Priority`, so it carries its own drift gate, in the shape [`CATEGORIES`]
1423/// does: [`level_position`] is a wildcard-free match, so a priority added to the shared
1424/// vocabulary fails to compile until it is placed there, and this crate's suite reconciles
1425/// this list and [`PriorityMappingConfig`]'s members against that enum's own derived schema.
1426pub const PRIORITY_LEVELS: [Priority; 4] = [
1427 Priority::Urgent,
1428 Priority::High,
1429 Priority::Medium,
1430 Priority::Low,
1431];
1432
1433/// Where one priority sits in [`PRIORITY_LEVELS`], or `None` for `none`, which is no option;
1434/// see that list for what this pins.
1435#[must_use]
1436pub const fn level_position(priority: Priority) -> Option<usize> {
1437 match priority {
1438 Priority::None => None,
1439 Priority::Urgent => Some(0),
1440 Priority::High => Some(1),
1441 Priority::Medium => Some(2),
1442 Priority::Low => Some(3),
1443 }
1444}
1445
1446/// This instance's complete priority-to-option mapping, read in both directions.
1447///
1448/// One option per level, held in [`PRIORITY_LEVELS`] order, once it is established that no
1449/// two levels name one option.
1450#[derive(Debug, Clone)]
1451struct PriorityMapping {
1452 options: [PriorityOptionName; 4],
1453}
1454
1455impl PriorityMapping {
1456 fn resolve(config: PriorityMappingConfig, instance: &SourceName) -> Result<Self, SourceError> {
1457 let shipped = |name: &str| PriorityOptionName(name.to_owned());
1458 let mapping = Self {
1459 options: [
1460 config.urgent.unwrap_or_else(|| shipped("Urgent")),
1461 config.high.unwrap_or_else(|| shipped("High")),
1462 config.medium.unwrap_or_else(|| shipped("Medium")),
1463 config.low.unwrap_or_else(|| shipped("Low")),
1464 ],
1465 };
1466 for (index, option) in mapping.options.iter().enumerate() {
1467 if let Some(earlier) = mapping.options[..index]
1468 .iter()
1469 .position(|other| other.as_str().eq_ignore_ascii_case(option.as_str()))
1470 {
1471 return Err(SourceError::Config {
1472 message: format!(
1473 "priority_mapping of source {instance} sends both {} and {} to the board \
1474 option {:?}; one option cannot read back as two priorities",
1475 PRIORITY_LEVELS[earlier],
1476 PRIORITY_LEVELS[index],
1477 option.as_str()
1478 ),
1479 });
1480 }
1481 }
1482 Ok(mapping)
1483 }
1484
1485 /// The option `priority` lands on, or `None` for `none`, which is no option at all.
1486 fn option(&self, priority: Priority) -> Option<&str> {
1487 level_position(priority).map(|index| self.options[index].as_str())
1488 }
1489
1490 /// The priority a board option name reports, or `None` when nothing maps to it.
1491 fn priority_of(&self, option: &str) -> Option<Priority> {
1492 self.options
1493 .iter()
1494 .position(|name| name.as_str().eq_ignore_ascii_case(option))
1495 .map(|index| PRIORITY_LEVELS[index])
1496 }
1497
1498 /// Every mapped option name, in the order a new `Priority` field lists them.
1499 fn names(&self) -> impl Iterator<Item = &str> {
1500 self.options.iter().map(PriorityOptionName::as_str)
1501 }
1502}
1503
1504/// What one item's `Priority` field says, read through this instance's mapping.
1505#[derive(Debug, Clone, PartialEq, Eq)]
1506enum HeldPriority {
1507 /// A priority this source reports: an option the mapping names, or no value (`none`).
1508 Read(Priority),
1509 /// An option the mapping does not name, which is never read as a level or as `none`.
1510 Unmapped(String),
1511}
1512
1513/// How fast this source writes, and how long it waits out a rate-limit refusal.
1514///
1515/// Configurable because a GitHub Enterprise installation sets its own limits and an
1516/// operator who has already been refused may want to go slower still — not because the
1517/// defaults are guesses.
1518#[derive(Debug, Clone, Default, Deserialize, schemars::JsonSchema)]
1519#[serde(default, deny_unknown_fields)]
1520pub struct PacingConfig {
1521 /// Shortest interval between two content-creating mutations, in milliseconds.
1522 ///
1523 /// Zero sends them as fast as they are asked for, which is what a fixture server on
1524 /// loopback wants and what no board on github.com does. At most [`MAX_PACING_MS`].
1525 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.
1526 /// First wait when a rate-limit refusal carries no hint, in milliseconds. Each
1527 /// further wait of the same call doubles it. At most [`MAX_PACING_MS`], and never
1528 /// zero while there is a budget to spend, because a schedule of zero-length waits
1529 /// consumes none of it and so never ends.
1530 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.
1531 /// Total time one call may spend waiting out rate limits, in milliseconds.
1532 ///
1533 /// Zero reports the refusal rather than waiting at all. At most [`MAX_PACING_MS`]:
1534 /// the bound is what makes this a wait rather than a hang.
1535 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.
1536}
1537
1538/// The largest any pacing setting may be, in milliseconds.
1539///
1540/// One hour. GitHub's own harshest published bound on content-generating requests works
1541/// out at one every 7.2 seconds, so an hour is already three orders of magnitude past
1542/// anything a real limit asks for, and past it the settings stop describing pacing at all:
1543/// a wait budget beyond it is the unbounded wait this whole mechanism exists to replace,
1544/// and an interval beyond it is a command that never sends its second mutation. It also
1545/// keeps the clock arithmetic in [`GitHubProjectsSource::reserve_mutation_slot`] inside
1546/// what an `Instant` can hold on every platform.
1547pub const MAX_PACING_MS: u64 = 3_600_000;
1548
1549/// [`PacingConfig`] with every default resolved and every value checked, which is what the
1550/// source holds.
1551#[derive(Debug, Clone, Copy)]
1552struct Pacing {
1553 min_mutation_interval: Duration,
1554 retry_backoff: Duration,
1555 retry_budget: Duration,
1556}
1557
1558impl Pacing {
1559 /// Resolve one instance's pacing, refusing a configuration that would not pace at all.
1560 fn resolve(config: PacingConfig, instance: &SourceName) -> Result<Self, SourceError> {
1561 let bounded = |value: Option<u64>, default: u64, field: &str| match value {
1562 Some(value) if value > MAX_PACING_MS => Err(SourceError::Config {
1563 message: format!(
1564 "pacing.{field} of source {instance} is {value} ms, and the most any pacing \
1565 setting may be is {MAX_PACING_MS} ms — an hour, which is already far past \
1566 GitHub's own harshest published limit"
1567 ),
1568 }),
1569 Some(value) => Ok(Duration::from_millis(value)),
1570 None => Ok(Duration::from_millis(default)),
1571 };
1572 let retry_backoff = bounded(
1573 config.retry_backoff_ms,
1574 RETRY_BACKOFF_MS,
1575 "retry_backoff_ms",
1576 )?;
1577 let retry_budget = bounded(config.retry_budget_ms, RETRY_BUDGET_MS, "retry_budget_ms")?;
1578 if retry_backoff.is_zero() && !retry_budget.is_zero() {
1579 return Err(SourceError::Config {
1580 message: format!(
1581 "pacing.retry_backoff_ms of source {instance} is 0 while \
1582 pacing.retry_budget_ms is {} ms; a schedule of zero-length waits spends \
1583 none of that budget, so it would retry a refusal forever. Set a backoff of \
1584 at least 1 ms, or set retry_budget_ms to 0 to report a refusal without \
1585 waiting at all",
1586 retry_budget.as_millis()
1587 ),
1588 });
1589 }
1590 Ok(Self {
1591 min_mutation_interval: bounded(
1592 config.min_mutation_interval_ms,
1593 MIN_MUTATION_INTERVAL_MS,
1594 "min_mutation_interval_ms",
1595 )?,
1596 retry_backoff,
1597 retry_budget,
1598 })
1599 }
1600}
1601
1602/// Factory for [`GitHubProjectsSource`].
1603#[derive(Debug, Clone, Copy, Default)]
1604pub struct Plugin;
1605
1606impl SourcePlugin for Plugin {
1607 fn kind(&self) -> &'static str {
1608 KIND
1609 }
1610 fn config_schema(&self) -> Schema {
1611 schema_for!(GitHubProjectsConfig)
1612 }
1613 fn build(
1614 &self,
1615 name: &SourceName,
1616 config: &Value,
1617 secrets: &dyn SecretResolver,
1618 ) -> Result<Box<dyn TaskSource>, SourceError> {
1619 self.build_recording_into(name, config, secrets, Arc::new(Accounting::new()))
1620 }
1621}
1622
1623impl Plugin {
1624 /// Build a source recording every request it sends into an accounting the caller holds.
1625 ///
1626 /// [`SourcePlugin::build`] is this with an accounting of its own, which is what the
1627 /// registry gets. This is for a caller that is also calling GitHub itself and wants one
1628 /// session total rather than two — see [`accounting`] and
1629 /// [`GitHubProjectsSource::recording_into`].
1630 ///
1631 /// # Errors
1632 ///
1633 /// Exactly [`SourcePlugin::build`]'s, with the same source name in front of each:
1634 /// [`SourceError::Config`] for configuration this plugin cannot use and
1635 /// [`SourceError::Auth`] for a credential it cannot find.
1636 pub fn build_recording_into(
1637 &self,
1638 name: &SourceName,
1639 config: &Value,
1640 secrets: &dyn SecretResolver,
1641 ledger: Arc<Accounting>,
1642 ) -> Result<Box<dyn TaskSource>, SourceError> {
1643 let config: GitHubProjectsConfig =
1644 serde_json::from_value(config.clone()).map_err(|e| SourceError::Config {
1645 message: format!("source {name}: {e}"),
1646 })?;
1647 let source = GitHubProjectsSource::recording_into(name, config, secrets, ledger).map_err(
1648 |error| match error {
1649 SourceError::Config { message } => SourceError::Config {
1650 message: format!("source {name}: {message}"),
1651 },
1652 SourceError::Auth { message } => SourceError::Auth {
1653 message: format!("source {name}: {message}"),
1654 },
1655 other => other,
1656 },
1657 )?;
1658 Ok(Box::new(source))
1659 }
1660}
1661
1662/// Where a status category lands on this board, once configuration is resolved.
1663#[derive(Debug, Clone, PartialEq, Eq)]
1664enum StatusTarget {
1665 /// Not usable against this instance.
1666 Disabled,
1667 /// The board's `Status` option of this name.
1668 Column(ColumnName),
1669 /// A closed issue, with both its board option and the reason that says which closed it means.
1670 // 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, `StatusMapping::new`, 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.
1671 Terminal(ColumnName, ClosedState),
1672}
1673
1674/// Every status category, in the order the vocabulary declares them.
1675///
1676/// This list mirrors `StatusCategory`, so it carries its own drift gate rather than a
1677/// reviewer's attention: [`category_position`] is a wildcard-free match, so a variant
1678/// added to the shared vocabulary fails to compile until it is named there, and this
1679/// crate's suite reconciles this list against that enum's own derived schema, which is
1680/// generated from the variants rather than written beside them. The schema is what
1681/// catches a list left one short — a list checking only the positions it already holds
1682/// would pass while every mapping indexed by the new position panicked.
1683pub const CATEGORIES: [StatusCategory; 8] = [
1684 StatusCategory::Draft,
1685 StatusCategory::Backlog,
1686 StatusCategory::Todo,
1687 StatusCategory::Queued,
1688 StatusCategory::InProgress,
1689 StatusCategory::Done,
1690 StatusCategory::Cancelled,
1691 StatusCategory::Unknown,
1692];
1693
1694/// Where one category sits in [`CATEGORIES`]; see that list for what this pins.
1695#[must_use]
1696pub const fn category_position(category: StatusCategory) -> usize {
1697 match category {
1698 StatusCategory::Draft => 0,
1699 StatusCategory::Backlog => 1,
1700 StatusCategory::Todo => 2,
1701 StatusCategory::Queued => 3,
1702 StatusCategory::InProgress => 4,
1703 StatusCategory::Done => 5,
1704 StatusCategory::Cancelled => 6,
1705 StatusCategory::Unknown => 7,
1706 }
1707}
1708
1709/// The spelling a status category is configured and reported under.
1710fn category_name(category: StatusCategory) -> &'static str {
1711 match category {
1712 StatusCategory::Draft => "draft",
1713 StatusCategory::Backlog => "backlog",
1714 StatusCategory::Todo => "todo",
1715 StatusCategory::Queued => "queued",
1716 StatusCategory::InProgress => "in-progress",
1717 StatusCategory::Done => "done",
1718 StatusCategory::Cancelled => "cancelled",
1719 StatusCategory::Unknown => "unknown",
1720 }
1721}
1722
1723/// A shipped default's option name.
1724///
1725/// The literals below are this file's own and non-blank, and they are validated by the
1726/// one constructor a configured name goes through rather than beside it.
1727fn shipped_column(name: &'static str) -> ColumnName {
1728 ColumnName::try_from(name.to_owned()).expect("a shipped default names a board option")
1729}
1730
1731/// The shipped default for one category, before this instance's configuration.
1732fn shipped_default(category: StatusCategory) -> StatusTarget {
1733 match category {
1734 StatusCategory::Backlog => StatusTarget::Column(shipped_column("Backlog")),
1735 StatusCategory::Todo => StatusTarget::Column(shipped_column("Todo")),
1736 StatusCategory::Queued => StatusTarget::Column(shipped_column("Queued")),
1737 StatusCategory::InProgress => StatusTarget::Column(shipped_column("In Progress")),
1738 StatusCategory::Done => {
1739 StatusTarget::Terminal(shipped_column("Done"), ClosedState::Completed)
1740 }
1741 StatusCategory::Cancelled => {
1742 StatusTarget::Terminal(shipped_column("Cancelled"), ClosedState::NotPlanned)
1743 }
1744 StatusCategory::Draft | StatusCategory::Unknown => StatusTarget::Disabled,
1745 }
1746}
1747
1748/// This instance's complete category-to-target mapping, read in both directions.
1749///
1750/// One target per category, held at that category's own [`category_position`], so a
1751/// category missing from the mapping, named twice in it, or filed out of order is a
1752/// state this type cannot hold rather than one [`Self::target`] has to defend against.
1753#[derive(Debug, Clone)]
1754struct StatusMapping {
1755 targets: [StatusTarget; CATEGORIES.len()],
1756}
1757
1758impl StatusMapping {
1759 fn resolve(
1760 configured: BTreeMap<String, Option<StatusTargetConfig>>,
1761 instance: &SourceName,
1762 ) -> Result<Self, SourceError> {
1763 let mut overrides: BTreeMap<&'static str, Option<StatusTargetConfig>> = BTreeMap::new();
1764 for (key, value) in configured {
1765 let category = CATEGORIES
1766 .iter()
1767 .find(|category| category_name(**category) == key)
1768 .ok_or_else(|| SourceError::Config {
1769 message: format!(
1770 "status_mapping names {key:?}, which is not a status category of source \
1771 {instance}; the categories are {}",
1772 CATEGORIES
1773 .iter()
1774 .map(|category| category_name(*category))
1775 .collect::<Vec<_>>()
1776 .join(", ")
1777 ),
1778 })?;
1779 overrides.insert(category_name(*category), value);
1780 }
1781 // `CATEGORIES[position] == category` for every category — the crate's suite
1782 // asserts it — so mapping the list in order fills each category's own slot.
1783 let targets = CATEGORIES.map(|category| match overrides.remove(category_name(category)) {
1784 None => shipped_default(category),
1785 Some(None) => StatusTarget::Disabled,
1786 Some(Some(StatusTargetConfig::Column(option))) => match category {
1787 StatusCategory::Done => StatusTarget::Terminal(option, ClosedState::Completed),
1788 StatusCategory::Cancelled => {
1789 StatusTarget::Terminal(option, ClosedState::NotPlanned)
1790 }
1791 _ => StatusTarget::Column(option),
1792 },
1793 });
1794 let mapping = Self { targets };
1795 for (index, category) in CATEGORIES.into_iter().enumerate() {
1796 let option = match mapping.target(category) {
1797 StatusTarget::Column(option) | StatusTarget::Terminal(option, _) => option,
1798 StatusTarget::Disabled => continue,
1799 };
1800 if let Some(other) = CATEGORIES[..index].iter().find(|earlier| {
1801 matches!(mapping.target(**earlier), StatusTarget::Column(name) | StatusTarget::Terminal(name, _)
1802 if name.as_str().eq_ignore_ascii_case(option.as_str()))
1803 }) {
1804 return Err(SourceError::Config {
1805 message: format!(
1806 "status_mapping of source {instance} sends both {} and {} to the board \
1807 option {:?}; one option cannot read back as two categories",
1808 category_name(*other),
1809 category_name(category),
1810 option.as_str()
1811 ),
1812 });
1813 }
1814 }
1815 Ok(mapping)
1816 }
1817
1818 fn target(&self, category: StatusCategory) -> &StatusTarget {
1819 &self.targets[category_position(category)]
1820 }
1821
1822 /// The category a board option name reports, or `None` when nothing maps to it.
1823 fn category_of(&self, option: &str) -> Option<StatusCategory> {
1824 CATEGORIES.into_iter().find(|category| {
1825 matches!(self.target(*category), StatusTarget::Column(name) | StatusTarget::Terminal(name, _)
1826 if name.as_str().eq_ignore_ascii_case(option))
1827 })
1828 }
1829
1830 /// The status an item reports, from the three things a read of it says: its board
1831 /// `Status` option, whether its issue is closed, and the reason it was closed with.
1832 ///
1833 /// The closed state decides the category and the `Status` option decides the name, so
1834 /// a closed issue sitting in a "Shipped" column reports `done` named `Shipped`. A
1835 /// closed issue whose reason is `DUPLICATE` or `REOPENED` reports `Unknown`: a
1836 /// duplicate is not finished work, and calling it done is a lie the next copy would
1837 /// write back. `REOPENED`-while-closed is a state this source can never produce, so
1838 /// it is read permissively rather than refused — reads are faithful, and refusals
1839 /// belong on writes.
1840 ///
1841 /// One function of those three rather than of a response, so a narrow status write can
1842 /// answer what a re-read would report by applying it to the state it has just written.
1843 fn status(&self, option: Option<&str>, closed: bool, reason: Option<&str>) -> Status {
1844 if closed {
1845 let category = match reason {
1846 None | Some("COMPLETED") => StatusCategory::Done,
1847 Some("NOT_PLANNED") => StatusCategory::Cancelled,
1848 Some(_) => StatusCategory::Unknown,
1849 };
1850 let fallback = match category {
1851 StatusCategory::Done => "Done",
1852 StatusCategory::Cancelled => "Cancelled",
1853 _ => "Closed",
1854 };
1855 return Status {
1856 category,
1857 name: option.unwrap_or(fallback).to_owned(),
1858 };
1859 }
1860 let name = option.unwrap_or("Open").to_owned();
1861 Status {
1862 category: self.category_of(&name).unwrap_or(StatusCategory::Unknown),
1863 name,
1864 }
1865 }
1866}
1867
1868// 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.
1869/// One repository this source can create an issue in, as `owner/name`.
1870///
1871/// Every `createIssue` this source sends names one of these: the item's own single
1872/// `repositories` entry, else its parent project issue's repository, else the configured
1873/// [`GitHubProjectsConfig::repository`]. [`GitHubProjectsSource::creation_target`] makes
1874/// that choice and says what it refuses before `createIssue`.
1875// llmlint: ignore-end[comments_earn_their_place, contracts_have_one_source_or_a_drift_gate]
1876#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
1877struct RepositoryTarget {
1878 owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
1879 name: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
1880}
1881
1882impl RepositoryTarget {
1883 fn parse(value: &str) -> Result<Self, SourceError> {
1884 let (owner, name) = value.split_once('/').ok_or_else(|| SourceError::Config {
1885 message: format!(
1886 "repository must be spelled owner/name; {value:?} names no repository"
1887 ),
1888 })?;
1889 if !valid_github_owner(owner) || !valid_github_repository_name(name) {
1890 return Err(SourceError::Config {
1891 message: format!(
1892 "repository must be spelled owner/name with a GitHub login and one \
1893 repository name; {value:?} is not"
1894 ),
1895 });
1896 }
1897 Ok(Self {
1898 owner: owner.to_owned(),
1899 name: name.to_owned(),
1900 })
1901 }
1902
1903 /// The one host whose repositories this source creates issues in, spelled once: it is
1904 /// what [`Self::origin`] renders and what [`Self::from_origin`] accepts.
1905 const HOST: &str = "github.com";
1906
1907 fn origin(&self) -> String {
1908 format!("{}/{}/{}", Self::HOST, self.owner, self.name)
1909 }
1910
1911 /// The repository a normalized origin names, or why it is none this source can create
1912 /// an issue in: another host, or more or fewer than `owner/name` under this one.
1913 fn from_origin(origin: &Repository) -> Result<Self, String> {
1914 let not_here = || {
1915 format!(
1916 "{} is not a {}/owner/name repository",
1917 origin.as_str(),
1918 Self::HOST
1919 )
1920 };
1921 let (host, rest) = origin.as_str().split_once('/').ok_or_else(not_here)?;
1922 if host != Self::HOST {
1923 return Err(not_here());
1924 }
1925 Self::parse(rest).map_err(|_| not_here())
1926 }
1927
1928 fn slug(&self) -> String {
1929 format!("{}/{}", self.owner, self.name)
1930 }
1931}
1932
1933/// A source which reads GitHub afresh for every operation.
1934pub struct GitHubProjectsSource {
1935 /// This source's configured name, used both to tell a far end naming this source
1936 /// from one naming a system it knows nothing about, and to name the instance a
1937 /// status refusal is about.
1938 name: SourceName,
1939 owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after full GitHub-owner validation.
1940 project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after GraphQL-Int validation.
1941 repository: Option<RepositoryTarget>,
1942 endpoint: Url,
1943 token: SecretString,
1944 credential_name: String, // llmlint: ignore[invalid_states_unrepresentable] Private diagnostic value constructed only after environment-name validation.
1945 statuses: StatusMapping,
1946 /// Where each priority lands on this board, or `None` when this instance holds none.
1947 priorities: Option<PriorityMapping>,
1948 client: Client,
1949 /// Every item this source has created since it was built, in the order it created
1950 /// them.
1951 ///
1952 /// GitHub's `projectV2.items` is eventually consistent: an issue added to a board with
1953 /// `addProjectV2ItemById` is routinely absent from the very next read of that board, so
1954 /// a copy resolving a dependency on an item it had just created refused it as not
1955 /// found. A board read is completed from this — an item remembered here and absent from
1956 /// the read is added back, because the board really does hold it and only the read is
1957 /// behind.
1958 ///
1959 /// It is not a cache of a user's work: nothing is remembered that this process did not
1960 /// itself just write, it lives and dies with the process, and it is never consulted for
1961 /// an item this source did not create.
1962 created: Mutex<Vec<Resolved>>,
1963 /// How fast this source writes, and how long it waits out a refusal.
1964 pacing: Pacing,
1965 /// When the last content-creating mutation finished, or the moment the furthest-out
1966 /// reserved slot releases the next one, whichever is later — so the one after it can be
1967 /// spaced from that. See [`MIN_MUTATION_INTERVAL_MS`] for the interval and
1968 /// [`GitHubProjectsSource::finish_mutation`] for why completion rather than release is
1969 /// what it is measured from.
1970 last_mutation: Mutex<Option<Instant>>,
1971 /// The board as this process last read it, for the length of one command.
1972 ///
1973 /// A copy of a project used to re-read the whole board, paged, before writing each of
1974 /// its items, which is by far the largest part of a copy's request count and none of
1975 /// its work. Nothing else changes this board while a command runs — this source's own
1976 /// writes are the only writer — so one read answers them all.
1977 ///
1978 /// It is not a store of a user's work and it is not the cache the no-persistence
1979 /// invariant forbids: it lives and dies with the process exactly as `created` does,
1980 /// nothing is written down, and [`Self::board`] still completes it from `created`, so
1981 /// an item this command created and then depends on resolves whether or not GitHub's
1982 /// own eventually-consistent read has caught up. A write to an item already on the
1983 /// board updates the entry here too, so what this holds is the last read plus this
1984 /// process's own writes rather than a snapshot taken before them.
1985 board_cache: Mutex<Option<Board>>,
1986 /// Every issue this board's own search reported, for the length of one command.
1987 ///
1988 /// The second half of a board read, and cached for the same reason and on the same
1989 /// terms as the first: it lives and dies with the process, nothing is written down, and
1990 /// a write this process makes updates the entry here exactly as it updates the one in
1991 /// [`Self::board_cache`]. One read answers every question a command asks, so a command
1992 /// that lists this board's projects and its tasks pays for one search rather than two.
1993 search_cache: Mutex<Option<Vec<Resolved>>>,
1994 /// The board's own id and field definitions as this process last read them on their
1995 /// own, for the length of one command.
1996 ///
1997 /// What a write needs of the board and its item does not say, read once per command
1998 /// rather than once per item written, on the terms [`Self::board_cache`] is held on: it
1999 /// lives and dies with the process and nothing is written down. It holds no item and so
2000 /// can answer no question about one — see [`Self::board_fields`].
2001 fields_cache: Mutex<Option<BoardFields>>,
2002 /// Each destination repository's node id, resolved once per repository
2003 /// rather than per issue created.
2004 ///
2005 /// A repository's node id does not change, and re-reading it for every issue of a copy
2006 /// spent one request per item on an answer this source already had. It is a map rather
2007 /// than one entry because a copy files each item in the repository its own
2008 /// `repositories` field names, so a plan across five repositories asks GitHub five
2009 /// times and not once per item.
2010 repository_cache: Mutex<BTreeMap<RepositoryTarget, String>>,
2011 /// What every request this source sends is recorded into.
2012 ///
2013 /// Ordinary code path, not a mode: [`Self::send_once`] records into it at the one place
2014 /// a request leaves this crate, so nothing has to be switched on for a session to be
2015 /// counted. It is shared rather than owned so a caller accounting for a whole session —
2016 /// its own schema verification, board lookups, residue sweep and cleanup beside this
2017 /// source's reads and writes — adds up one accounting instead of two. See
2018 /// [`accounting`] for what a record carries and what a session's spend is and is not.
2019 ledger: Arc<Accounting>,
2020}
2021
2022/// GitHub's closed single-select color vocabulary.
2023#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2024#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
2025pub enum StatusOptionColor {
2026 /// Gray.
2027 Gray,
2028 /// Blue.
2029 Blue,
2030 /// Green.
2031 Green,
2032 /// Yellow.
2033 Yellow,
2034 /// Purple.
2035 Purple,
2036 /// Red.
2037 Red,
2038 /// Orange.
2039 Orange,
2040 /// Pink.
2041 Pink,
2042}
2043
2044/// Whether a guarded board setup — of the fields, or of the Status options alone — plans or
2045/// applies its additions.
2046#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2047pub enum SetupMode {
2048 /// Read without mutation.
2049 Plan,
2050 /// Apply and verify.
2051 Apply,
2052}
2053
2054/// The name [`SetupMode`] had when Status was the one field set up, kept so a caller written
2055/// against it goes on compiling.
2056pub type StatusOptionsMode = SetupMode;
2057
2058/// The explicit result of the requested operation.
2059#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2060#[serde(rename_all = "kebab-case")]
2061pub enum StatusOptionsOutcome {
2062 /// A read-only plan.
2063 Planned,
2064 /// Apply found nothing missing.
2065 Unchanged,
2066 /// Additions were applied and verified.
2067 Applied,
2068}
2069
2070/// A GitHub single-select option's opaque GraphQL node identifier.
2071#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2072#[serde(transparent)]
2073pub struct StatusOptionId(#[schemars(length(min = 1))] String);
2074
2075impl TryFrom<String> for StatusOptionId {
2076 type Error = String;
2077
2078 fn try_from(id: String) -> Result<Self, Self::Error> {
2079 if id.trim().is_empty() {
2080 return Err("a GitHub Status option id cannot be blank".to_owned());
2081 }
2082 Ok(Self(id))
2083 }
2084}
2085
2086/// One existing or proposed option in a guarded Status-field update.
2087#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2088pub struct StatusOption {
2089 /// GitHub's stable id.
2090 pub id: StatusOptionId,
2091 /// The visible option name.
2092 pub name: ColumnName,
2093 /// GitHub's single-select color token.
2094 pub color: StatusOptionColor,
2095 /// The option description, including an empty one.
2096 pub description: String,
2097}
2098
2099/// One board item's Status assignment, retained as recovery data.
2100#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2101pub struct StatusAssignment {
2102 /// The project item id whose assignment this is.
2103 // llmlint: ignore[invalid_states_unrepresentable] This opaque GraphQL node ID is
2104 // carried verbatim as operator recovery data; introducing a semantic type would claim
2105 // validation rules GitHub does not publish and no operation here interprets.
2106 pub item_id: String,
2107 /// The selected option, absent when the item has no status.
2108 #[serde(skip_serializing_if = "Option::is_none")]
2109 pub option: Option<AssignedStatusOption>,
2110}
2111
2112/// The inseparable id and name of an assigned option.
2113#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2114pub struct AssignedStatusOption {
2115 /// GitHub's stable id.
2116 pub id: StatusOptionId,
2117 /// The visible name.
2118 pub name: ColumnName,
2119}
2120
2121/// The plan and verified outcome of reconciling configured Status options.
2122#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2123pub struct StatusOptionsReport {
2124 /// The configured source name.
2125 pub source: SourceName,
2126 /// Configured option names absent before the operation.
2127 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a
2128 // `ColumnName` and has therefore already passed its nonblank validation; retaining the
2129 // serialized string here preserves the report's intentionally simple public contract.
2130 pub missing: Vec<String>,
2131 /// What the requested operation did.
2132 pub outcome: StatusOptionsOutcome,
2133 /// The complete option list observed before any mutation.
2134 pub existing: Vec<StatusOption>,
2135}
2136
2137#[derive(Debug, Clone, PartialEq, Eq)]
2138struct StatusSnapshot {
2139 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
2140 // passed back as the mutation's project identity; a newtype could enforce no stronger
2141 // invariant because GitHub publishes no grammar for it.
2142 board_id: String,
2143 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
2144 // passed back as the mutation's field identity; a newtype could enforce no stronger
2145 // invariant because GitHub publishes no grammar for it.
2146 field_id: String,
2147 options: Vec<StatusOption>,
2148 assignments: Vec<StatusAssignment>,
2149}
2150
2151/// The name of the board field a status is held in.
2152const STATUS_FIELD: &str = "Status";
2153
2154/// Every item's value of each field `report` names, as it stood before the setup wrote
2155/// anything — what a person puts back when the setup is refused part way.
2156fn recovery(report: &FieldsReport, before: &BoardSnapshot) -> Result<String, SourceError> {
2157 let assignments: BTreeMap<&str, Vec<StatusAssignment>> = report
2158 .fields
2159 .iter()
2160 .map(|field| (field.field.name(), before.assignments(field.field)))
2161 .collect();
2162 serde_json::to_string_pretty(&assignments).map_err(|error| SourceError::Malformed {
2163 message: format!("cannot render the pre-write field recovery snapshot: {error}"),
2164 })
2165}
2166
2167/// One board field the guarded setup reads and writes — every one it reads, and the only
2168/// ones it writes.
2169#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, schemars::JsonSchema)]
2170pub enum BoardField {
2171 /// The single-select `Status` field every instance's `status_mapping` resolves into.
2172 Status,
2173 /// The single-select `Priority` field an instance's `priority_mapping` resolves into.
2174 Priority,
2175}
2176
2177impl BoardField {
2178 /// The field's name on the board.
2179 #[must_use]
2180 pub const fn name(self) -> &'static str {
2181 match self {
2182 Self::Status => STATUS_FIELD,
2183 Self::Priority => PRIORITY_FIELD,
2184 }
2185 }
2186
2187 /// The field a board calls `name`, or `None` for one this setup does not own.
2188 fn named(name: &str) -> Option<Self> {
2189 [Self::Status, Self::Priority]
2190 .into_iter()
2191 .find(|field| field.name() == name)
2192 }
2193}
2194
2195/// What the guarded setup did to one field.
2196#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2197#[serde(rename_all = "kebab-case")]
2198pub enum FieldOutcome {
2199 /// A read-only plan.
2200 Planned,
2201 /// Apply found the field there with every configured option.
2202 Unchanged,
2203 /// Missing options were added to the field that was there, and verified.
2204 Applied,
2205 /// The field was not there; it was created holding the configured options, and verified.
2206 Created,
2207}
2208
2209/// One field's plan, or its verified outcome.
2210#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2211pub struct FieldReport {
2212 /// Which field.
2213 pub field: BoardField,
2214 /// Whether the board had the field before the operation.
2215 // llmlint: ignore[invalid_states_unrepresentable] `exists` beside `outcome` is the report's
2216 // wire shape as its consumer's contract fixes it — `{"field", "exists", "missing",
2217 // "outcome", "existing"}` — so folding one into the other would change a published JSON
2218 // shape. The contradictory pairings cannot be built: `GitHubProjectsSource::fields` is the
2219 // one constructor, and it derives `outcome` from `exists` in one match.
2220 pub exists: bool,
2221 /// Configured option names the field lacked before the operation — every one of them,
2222 /// in the order a new field lists them, when the field was not there at all.
2223 // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
2224 // mapping name and has therefore already passed its nonblank validation; the serialized
2225 // string is the report's intentionally simple public contract, as `StatusOptionsReport`'s is.
2226 pub missing: Vec<String>,
2227 /// What the requested operation did.
2228 pub outcome: FieldOutcome,
2229 /// The field's complete option list observed before any mutation; empty when the field
2230 /// was not there.
2231 pub existing: Vec<StatusOption>,
2232}
2233
2234/// The plan and verified outcome of setting up every field a source's configuration names.
2235#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2236pub struct FieldsReport {
2237 /// The configured source name.
2238 pub source: SourceName,
2239 /// `Status`, always, and `Priority` when the source sets `priority_mapping`.
2240 // llmlint: ignore[invalid_states_unrepresentable] A list is the report's wire shape as its
2241 // consumer's contract fixes it — `{"source", "fields": [...]}` — so a struct with one member
2242 // per field would change a published JSON shape. The states the list could hold and the
2243 // contract forbids cannot be built: `GitHubProjectsSource::fields` is the one constructor,
2244 // and it pushes `Status` first and exactly once, then `Priority` exactly when configured.
2245 pub fields: Vec<FieldReport>,
2246}
2247
2248/// Which options one field is configured with, in the order a new field would list them.
2249struct FieldPlan {
2250 field: BoardField,
2251 wanted: Vec<String>,
2252}
2253
2254/// One single-select field as the guarded setup snapshots it.
2255#[derive(Debug, Clone, PartialEq, Eq)]
2256struct SnapshotField {
2257 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
2258 // passed back as the mutation's field identity; a newtype could enforce no stronger
2259 // invariant because GitHub publishes no grammar for it.
2260 field_id: String,
2261 options: Vec<StatusOption>,
2262}
2263
2264/// Every single-select field of a board and every item's value of each.
2265#[derive(Debug, Clone, PartialEq, Eq)]
2266struct BoardSnapshot {
2267 // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
2268 // passed back as the mutation's project identity; a newtype could enforce no stronger
2269 // invariant because GitHub publishes no grammar for it.
2270 board_id: String,
2271 fields: BTreeMap<BoardField, SnapshotField>,
2272 /// Each board item's id, and its value of each field this setup owns that it holds one of.
2273 items: Vec<(String, BTreeMap<BoardField, AssignedStatusOption>)>,
2274}
2275
2276impl BoardSnapshot {
2277 /// Every item's value of `field`, in board order — the recovery data a drift refusal
2278 /// carries.
2279 fn assignments(&self, field: BoardField) -> Vec<StatusAssignment> {
2280 self.items
2281 .iter()
2282 .map(|(item_id, values)| StatusAssignment {
2283 item_id: item_id.clone(),
2284 option: values.get(&field).cloned(),
2285 })
2286 .collect()
2287 }
2288}
2289
2290impl GitHubProjectsSource {
2291 /// Report missing configured Status options and, when `apply` is true, add them with
2292 /// a whole-list mutation that preserves every existing id and verifies the result.
2293 ///
2294 /// # Errors
2295 ///
2296 /// Refuses a board without a single-select `Status` field. A post-write difference in
2297 /// any pre-existing option id or item assignment is refused with the complete pre-write
2298 /// assignment snapshot in the diagnostic for recovery.
2299 // llmlint: ignore[changed_behavior_has_e2e] The CLI journeys cover plan, no-op apply,
2300 // successful mutation, both drift refusals, source selection, missing Status, casing,
2301 // and paging. Transport errors remain the shared `graphql` boundary's behavior rather
2302 // than a new status-options behavior, and the pinned-schema test prevents valid GitHub
2303 // responses from entering the defensive malformed-response branches below.
2304 pub async fn status_options(
2305 &self,
2306 mode: StatusOptionsMode,
2307 ) -> Result<StatusOptionsReport, SourceError> {
2308 let before = self.status_snapshot().await?;
2309 let configured = self
2310 .statuses
2311 .targets
2312 .iter()
2313 // A terminal category's option is as configured as an open one's: a terminal
2314 // write validates it before closing and refuses when the board lacks it.
2315 .filter_map(|target| match target {
2316 StatusTarget::Column(name) | StatusTarget::Terminal(name, _) => {
2317 Some(name.as_str().to_owned())
2318 }
2319 StatusTarget::Disabled => None,
2320 });
2321 let missing = configured
2322 .filter(|wanted| {
2323 !before
2324 .options
2325 .iter()
2326 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
2327 })
2328 .collect::<Vec<_>>();
2329 let report = StatusOptionsReport {
2330 source: self.name.clone(),
2331 missing: missing.clone(),
2332 outcome: match (mode, missing.is_empty()) {
2333 (StatusOptionsMode::Plan, _) => StatusOptionsOutcome::Planned,
2334 (StatusOptionsMode::Apply, true) => StatusOptionsOutcome::Unchanged,
2335 (StatusOptionsMode::Apply, false) => StatusOptionsOutcome::Applied,
2336 },
2337 existing: before.options.clone(),
2338 };
2339 if mode == StatusOptionsMode::Plan || missing.is_empty() {
2340 return Ok(report);
2341 }
2342 let mut options = before
2343 .options
2344 .iter()
2345 .map(|option| {
2346 json!({
2347 "id": option.id, "name": option.name, "color": option.color,
2348 "description": option.description,
2349 })
2350 })
2351 .collect::<Vec<_>>();
2352 options.extend(missing.iter().map(|name| {
2353 json!({
2354 "name": name, "color": "GRAY", "description": ""
2355 })
2356 }));
2357 self.graphql(
2358 graphql::STATUS_OPTIONS_UPDATE,
2359 json!({"input": {
2360 "projectId": before.board_id, "fieldId": before.field_id,
2361 "singleSelectOptions": options,
2362 }}),
2363 )
2364 .await?;
2365 let after = self.status_snapshot().await?;
2366 let options_preserved = before
2367 .options
2368 .iter()
2369 .all(|old| after.options.iter().any(|new| new == old));
2370 let additions_present = missing.iter().all(|wanted| {
2371 after
2372 .options
2373 .iter()
2374 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
2375 });
2376 if !options_preserved || !additions_present || after.assignments != before.assignments {
2377 let recovery = serde_json::to_string_pretty(&before.assignments).map_err(|error| {
2378 SourceError::Malformed {
2379 message: format!("cannot render pre-write Status recovery snapshot: {error}"),
2380 }
2381 })?;
2382 return Err(SourceError::Refused {
2383 message: format!(
2384 "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}"
2385 ),
2386 });
2387 }
2388 Ok(report)
2389 }
2390
2391 /// A fresh snapshot of the Status field and every board item's assignment of it.
2392 ///
2393 /// # Errors
2394 ///
2395 /// Refuses a board without a single-select `Status` field, and one the token cannot see.
2396 async fn status_snapshot(&self) -> Result<StatusSnapshot, SourceError> {
2397 // Status alone, as this operation has always read it: a `Priority` field is another
2398 // operation's, so nothing about it can refuse this one.
2399 let mut board = self.board_snapshot(&[BoardField::Status]).await?;
2400 let field = board
2401 .fields
2402 .remove(&BoardField::Status)
2403 .ok_or_else(|| self.no_status_field())?;
2404 Ok(StatusSnapshot {
2405 assignments: board.assignments(BoardField::Status),
2406 board_id: board.board_id,
2407 field_id: field.field_id,
2408 options: field.options,
2409 })
2410 }
2411
2412 /// The refusal a board with no `Status` field is answered with by the guarded setup.
2413 fn no_status_field(&self) -> SourceError {
2414 SourceError::Refused {
2415 message: format!("source {} board has no Status field", self.name),
2416 }
2417 }
2418
2419 // llmlint: ignore-block[changed_behavior_has_e2e] Valid snapshot shapes are exercised through
2420 // the real CLI loopback journey, including pagination. The individual malformed guards
2421 // are defensive validation of a schema-pinned third-party response, not separate user
2422 // journeys; drift and missing-field failures cover the operation's recovery behavior.
2423 /// A fresh snapshot of each of the `owned` fields on the board, with its options, and of
2424 /// every board item's value of each, walked to the end of the board's items. A field not
2425 /// in `owned` is read past whatever it holds.
2426 async fn board_snapshot(&self, owned: &[BoardField]) -> Result<BoardSnapshot, SourceError> {
2427 let mut after: Option<String> = None;
2428 let mut snapshot: Option<BoardSnapshot> = None;
2429 loop {
2430 let data = self
2431 .graphql(
2432 graphql::STATUS_OPTIONS_SNAPSHOT,
2433 json!({
2434 "owner": self.owner, "number": self.project_number,
2435 "first": MAX_PAGE_SIZE, "after": after, "nestedFirst": MAX_PAGE_SIZE,
2436 }),
2437 )
2438 .await?;
2439 let board = data
2440 .pointer("/owner/projectV2")
2441 .filter(|board| board.is_object())
2442 .ok_or_else(|| SourceError::Refused {
2443 message: format!(
2444 "source {} has no accessible GitHub Projects board",
2445 self.name
2446 ),
2447 })?;
2448 if board
2449 .pointer("/fields/pageInfo/hasNextPage")
2450 .and_then(Value::as_bool)
2451 != Some(false)
2452 {
2453 return Err(SourceError::Malformed {
2454 message:
2455 "GitHub project fields is incomplete or has malformed pageInfo.hasNextPage"
2456 .into(),
2457 });
2458 }
2459 let mut fields = BTreeMap::new();
2460 // Only the fields this setup owns, by name: a node the single-select fragment did not
2461 // match carries no name, and a person's own single-select field — a `Size`, a
2462 // `Team` — is none of this setup's business, so nothing about it can refuse one. A
2463 // `Status` or `Priority` field without its options is malformed, not absent.
2464 // 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.
2465 for (owned, field) in board
2466 .pointer("/fields/nodes")
2467 .and_then(Value::as_array)
2468 .ok_or_else(|| SourceError::Malformed {
2469 message: "GitHub project fields.nodes is not an array".into(),
2470 })?
2471 .iter()
2472 .filter_map(|field| {
2473 let named = BoardField::named(field.get("name")?.as_str()?)?;
2474 owned.contains(&named).then_some((named, field))
2475 })
2476 {
2477 let options = field
2478 .get("options")
2479 .and_then(Value::as_array)
2480 .ok_or_else(|| SourceError::Malformed {
2481 message: "GitHub single-select field options is not an array".into(),
2482 })?
2483 .iter()
2484 .map(|option| {
2485 Ok(StatusOption {
2486 id: StatusOptionId::try_from(required_str(option, "id")?.to_owned())
2487 .map_err(|message| SourceError::Malformed { message })?,
2488 name: ColumnName::try_from(required_str(option, "name")?.to_owned())
2489 .map_err(|message| SourceError::Malformed {
2490 message: format!(
2491 "GitHub single-select option name is invalid: {message}"
2492 ),
2493 })?,
2494 color: serde_json::from_value(
2495 option.get("color").cloned().unwrap_or(Value::Null),
2496 )
2497 .map_err(|error| {
2498 SourceError::Malformed {
2499 message: format!(
2500 "GitHub single-select option color is invalid: {error}"
2501 ),
2502 }
2503 })?,
2504 description: optional_str(option, "description")?
2505 .unwrap_or_default()
2506 .to_owned(),
2507 })
2508 })
2509 .collect::<Result<Vec<_>, SourceError>>()?;
2510 let snapshot = SnapshotField {
2511 field_id: required_nonblank_str(field, "id")?.to_owned(),
2512 options,
2513 };
2514 // A board's field names are unique, so a second one is an answer that cannot
2515 // say which field the setup would act on — refused rather than one chosen.
2516 if fields.insert(owned, snapshot).is_some() {
2517 return Err(SourceError::Malformed {
2518 message: format!(
2519 "GitHub answered two {} fields for this board",
2520 owned.name()
2521 ),
2522 });
2523 }
2524 }
2525 let board_id = required_nonblank_str(board, "id")?.to_owned();
2526 let current = snapshot.get_or_insert_with(|| BoardSnapshot {
2527 board_id,
2528 fields,
2529 items: Vec::new(),
2530 });
2531 let items = board
2532 .pointer("/items/nodes")
2533 .and_then(Value::as_array)
2534 .ok_or_else(|| SourceError::Malformed {
2535 message: "GitHub project items.nodes is not an array".into(),
2536 })?;
2537 for item in items {
2538 let field_values =
2539 item.get("fieldValues")
2540 .ok_or_else(|| SourceError::Malformed {
2541 message: "GitHub project item is missing fieldValues".into(),
2542 })?;
2543 if field_values
2544 .pointer("/pageInfo/hasNextPage")
2545 .and_then(Value::as_bool)
2546 != Some(false)
2547 {
2548 return Err(SourceError::Malformed {
2549 message: "GitHub project item fieldValues is incomplete or has malformed pageInfo.hasNextPage".into(),
2550 });
2551 }
2552 let values = item
2553 .pointer("/fieldValues/nodes")
2554 .and_then(Value::as_array)
2555 .ok_or_else(|| SourceError::Malformed {
2556 message: "GitHub project item fieldValues.nodes is not an array".into(),
2557 })?;
2558 let item_id = required_nonblank_str(item, "id")?;
2559 let mut assigned = BTreeMap::new();
2560 for value in values {
2561 let Some(field) = value
2562 .pointer("/field/name")
2563 .and_then(Value::as_str)
2564 .and_then(BoardField::named)
2565 .filter(|field| owned.contains(field))
2566 else {
2567 continue;
2568 };
2569 let held = assigned.insert(
2570 field,
2571 AssignedStatusOption {
2572 id: StatusOptionId::try_from(
2573 required_str(value, "optionId")?.to_owned(),
2574 )
2575 .map_err(|message| SourceError::Malformed { message })?,
2576 name: ColumnName::try_from(required_str(value, "name")?.to_owned())
2577 .map_err(|message| SourceError::Malformed {
2578 message: format!(
2579 "GitHub assigned {} name is invalid: {message}",
2580 field.name()
2581 ),
2582 })?,
2583 },
2584 );
2585 // An item holds one value of a field, so a second one leaves no way to
2586 // tell which it holds — and a verification or recovery built on either
2587 // could restore the wrong one.
2588 if held.is_some() {
2589 return Err(SourceError::Malformed {
2590 message: format!(
2591 "GitHub answered two {} values for board item {item_id}",
2592 field.name()
2593 ),
2594 });
2595 }
2596 }
2597 current.items.push((item_id.to_owned(), assigned));
2598 }
2599 let page = board.get("items").ok_or_else(|| SourceError::Malformed {
2600 message: "GitHub project is missing items".into(),
2601 })?;
2602 let has_next = page
2603 .pointer("/pageInfo/hasNextPage")
2604 .and_then(Value::as_bool)
2605 .ok_or_else(|| SourceError::Malformed {
2606 message: "GitHub project items.pageInfo.hasNextPage is not a boolean".into(),
2607 })?;
2608 if !has_next {
2609 break;
2610 }
2611 let next =
2612 required_nonblank_str(page.get("pageInfo").unwrap_or(&Value::Null), "endCursor")?;
2613 validate_cursor_progress(after.as_deref(), next)?;
2614 after = Some(next.to_owned());
2615 }
2616 snapshot.ok_or_else(|| SourceError::Malformed {
2617 message: "GitHub returned no board field snapshot".into(),
2618 })
2619 }
2620 // llmlint: ignore-end[changed_behavior_has_e2e]
2621
2622 /// Report every board field this source's configuration names and, with
2623 /// [`SetupMode::Apply`], set each up: add the options a field lacks, and create
2624 /// the `Priority` field when the board has none.
2625 ///
2626 /// The fields are `Status`, always, with the options `status_mapping` resolves to; and
2627 /// `Priority`, when `priority_mapping` is set, with its four mapped options — created in
2628 /// the order urgent, high, medium, low. An option a field already has keeps its id, name,
2629 /// color and description: the whole option list goes back with every existing id, because
2630 /// a re-minted id clears every item's value.
2631 ///
2632 /// # Errors
2633 ///
2634 /// Refuses a board without a single-select `Status` field. After an apply the board is
2635 /// read again, and a pre-existing option or any item's value of either field that moved is
2636 /// refused with the complete pre-write assignments in the diagnostic, for recovery.
2637 // llmlint: ignore[changed_behavior_has_e2e] The `sources fields` journeys drive plan,
2638 // unchanged apply, a created field, an added option to each field, drift refusal, a board
2639 // with no Status field and a non-github-projects source through the compiled CLI against
2640 // the loopback board. Transport errors are the shared `graphql` boundary's behavior.
2641 pub async fn fields(&self, mode: SetupMode) -> Result<FieldsReport, SourceError> {
2642 let owned: Vec<BoardField> = if self.priorities.is_some() {
2643 vec![BoardField::Status, BoardField::Priority]
2644 } else {
2645 vec![BoardField::Status]
2646 };
2647 let before = self.board_snapshot(&owned).await?;
2648 let mut plans = vec![FieldPlan {
2649 field: BoardField::Status,
2650 wanted: self
2651 .statuses
2652 .targets
2653 .iter()
2654 .filter_map(|target| match target {
2655 StatusTarget::Column(name) | StatusTarget::Terminal(name, _) => {
2656 Some(name.as_str().to_owned())
2657 }
2658 StatusTarget::Disabled => None,
2659 })
2660 .collect(),
2661 }];
2662 if !before.fields.contains_key(&BoardField::Status) {
2663 return Err(self.no_status_field());
2664 }
2665 if let Some(mapping) = &self.priorities {
2666 plans.push(FieldPlan {
2667 field: BoardField::Priority,
2668 wanted: mapping.names().map(str::to_owned).collect(),
2669 });
2670 }
2671 // The snapshot reads single-select fields alone, so a field it did not find may still
2672 // be on the board under the name, of another type: creating one beside it would fail
2673 // part way, or leave two fields of one name. Asked of the board's own field list, and
2674 // only when a field is missing.
2675 if plans
2676 .iter()
2677 .any(|plan| !before.fields.contains_key(&plan.field))
2678 {
2679 let board = self.board_fields().await?;
2680 for plan in plans
2681 .iter()
2682 .filter(|plan| !before.fields.contains_key(&plan.field))
2683 {
2684 if let Some(field) = Board::field(&board.fields, plan.field.name())? {
2685 return Err(SourceError::Refused {
2686 message: format!(
2687 "source {}'s board has a {} field that is not a single-select field \
2688 (it is a {}), so it cannot hold this source's options; next: rename \
2689 or remove that field, then run this again",
2690 self.name,
2691 plan.field.name(),
2692 optional_str(field, "__typename")?.unwrap_or("field of another type")
2693 ),
2694 });
2695 }
2696 }
2697 }
2698 let mut reports = Vec::new();
2699 for plan in &plans {
2700 let held = before.fields.get(&plan.field);
2701 let existing = held.map(|field| field.options.clone()).unwrap_or_default();
2702 let mut missing: Vec<String> = Vec::new();
2703 for wanted in &plan.wanted {
2704 let present = existing
2705 .iter()
2706 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
2707 || missing
2708 .iter()
2709 .any(|named| named.eq_ignore_ascii_case(wanted));
2710 if !present {
2711 missing.push(wanted.clone());
2712 }
2713 }
2714 reports.push(FieldReport {
2715 field: plan.field,
2716 exists: held.is_some(),
2717 outcome: match (mode, held.is_some(), missing.is_empty()) {
2718 (SetupMode::Plan, _, _) => FieldOutcome::Planned,
2719 (SetupMode::Apply, true, true) => FieldOutcome::Unchanged,
2720 (SetupMode::Apply, true, false) => FieldOutcome::Applied,
2721 (SetupMode::Apply, false, _) => FieldOutcome::Created,
2722 },
2723 missing,
2724 existing,
2725 });
2726 }
2727 let report = FieldsReport {
2728 source: self.name.clone(),
2729 fields: reports,
2730 };
2731 let writes: Vec<&FieldReport> = report
2732 .fields
2733 .iter()
2734 .filter(|field| !field.missing.is_empty() || !field.exists)
2735 .collect();
2736 if mode == SetupMode::Plan || writes.is_empty() {
2737 return Ok(report);
2738 }
2739 let mut landed: Vec<&str> = Vec::new();
2740 for field in &writes {
2741 let added = field
2742 .missing
2743 .iter()
2744 .map(|name| json!({"name": name, "color": "GRAY", "description": ""}));
2745 let sent = match before.fields.get(&field.field) {
2746 Some(held) => {
2747 let mut options = held
2748 .options
2749 .iter()
2750 .map(|option| {
2751 json!({
2752 "id": option.id, "name": option.name, "color": option.color,
2753 "description": option.description,
2754 })
2755 })
2756 .collect::<Vec<_>>();
2757 options.extend(added);
2758 self.graphql(
2759 graphql::STATUS_OPTIONS_UPDATE,
2760 json!({"input": {
2761 "projectId": before.board_id, "fieldId": held.field_id,
2762 "singleSelectOptions": options,
2763 }}),
2764 )
2765 .await
2766 }
2767 None => {
2768 self.graphql(
2769 graphql::CREATE_FIELD,
2770 json!({"input": {
2771 "projectId": before.board_id, "dataType": "SINGLE_SELECT",
2772 "name": field.field.name(),
2773 "singleSelectOptions": added.collect::<Vec<_>>(),
2774 }}),
2775 )
2776 .await
2777 }
2778 };
2779 // A mutation that failed does not establish that GitHub left its field as it was,
2780 // so every failure from here on carries the recovery data a drift refusal does.
2781 match sent {
2782 Ok(_) => landed.push(field.field.name()),
2783 Err(error) => {
2784 let changed = if landed.is_empty() {
2785 String::new()
2786 } else {
2787 format!("changed the {} field and then ", landed.join(" and "))
2788 };
2789 return Err(SourceError::Refused {
2790 message: format!(
2791 "the guarded field setup {changed}failed on the {} field, which it may \
2792 have changed part way: {error}; the pre-write item assignments \
2793 are:\n{}",
2794 field.field.name(),
2795 recovery(&report, &before)?
2796 ),
2797 });
2798 }
2799 }
2800 }
2801 // The board has been written, so a verification read that fails leaves it unverified
2802 // rather than unchanged, and says what to put back.
2803 let after = match self.board_snapshot(&owned).await {
2804 Ok(after) => after,
2805 Err(error) => {
2806 return Err(SourceError::Refused {
2807 message: format!(
2808 "the guarded field setup changed the {} field and then could not read the \
2809 board back to verify it: {error}; the pre-write item assignments are:\n{}",
2810 landed.join(" and "),
2811 recovery(&report, &before)?
2812 ),
2813 });
2814 }
2815 };
2816 let mut moved = Vec::new();
2817 for field in &report.fields {
2818 let name = field.field.name();
2819 let now = after
2820 .fields
2821 .get(&field.field)
2822 .map(|held| held.options.as_slice())
2823 .unwrap_or_default();
2824 if !field.existing.iter().all(|old| now.contains(old)) {
2825 moved.push(format!(
2826 "a pre-existing {name} option id, name, color or description"
2827 ));
2828 }
2829 if !field.missing.iter().all(|wanted| {
2830 now.iter()
2831 .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
2832 }) {
2833 moved.push(format!("an added {name} option"));
2834 }
2835 if after.assignments(field.field) != before.assignments(field.field) {
2836 moved.push(format!("an item's {name} value"));
2837 }
2838 }
2839 if !moved.is_empty() {
2840 return Err(SourceError::Refused {
2841 message: format!(
2842 "GitHub changed {} after the guarded field setup; the pre-write item \
2843 assignments are:\n{}",
2844 moved.join(", "),
2845 recovery(&report, &before)?
2846 ),
2847 });
2848 }
2849 Ok(report)
2850 }
2851
2852 /// Validate configuration and capture the named credential without exposing it.
2853 ///
2854 /// # Errors
2855 ///
2856 /// Returns [`SourceError::Config`] for a configuration this instance cannot use and
2857 /// [`SourceError::Auth`] when the named credential is missing or empty.
2858 pub fn new(
2859 name: &SourceName,
2860 config: GitHubProjectsConfig,
2861 secrets: &dyn SecretResolver,
2862 ) -> Result<Self, SourceError> {
2863 Self::recording_into(name, config, secrets, Arc::new(Accounting::new()))
2864 }
2865
2866 /// The same, recording every request it sends into an accounting the caller holds too.
2867 ///
2868 /// [`Self::new`] is this with an accounting of its own. A caller that is also making
2869 /// its own calls to GitHub — a lane verifying a schema, sweeping residue or cleaning
2870 /// up — passes the one it records those into, so the session total accounts for the
2871 /// whole session rather than for this source's share of it.
2872 ///
2873 /// # Errors
2874 ///
2875 /// Exactly [`Self::new`]'s: [`SourceError::Config`] for a configuration this instance
2876 /// cannot use and [`SourceError::Auth`] when the named credential is missing or empty.
2877 pub fn recording_into(
2878 name: &SourceName,
2879 config: GitHubProjectsConfig,
2880 secrets: &dyn SecretResolver,
2881 ledger: Arc<Accounting>,
2882 ) -> Result<Self, SourceError> {
2883 if !valid_github_owner(&config.owner) {
2884 return Err(SourceError::Config {
2885 message: "owner must be 1-39 ASCII letters, digits, or single hyphens, and cannot start or end with a hyphen".into(),
2886 });
2887 }
2888 if config.project_number == 0 || config.project_number > i32::MAX as u32 {
2889 return Err(SourceError::Config {
2890 message: format!("project_number must be between 1 and {}", i32::MAX),
2891 });
2892 }
2893 if !valid_environment_name(&config.token_env) {
2894 return Err(SourceError::Config {
2895 message: "token_env must be a valid environment-variable name".into(),
2896 });
2897 }
2898 let repository = config
2899 .repository
2900 .as_deref()
2901 .map(RepositoryTarget::parse)
2902 .transpose()?;
2903 let endpoint = Url::parse(&config.endpoint).map_err(|e| SourceError::Config {
2904 message: format!("endpoint is not a valid URL: {e}"),
2905 })?;
2906 if endpoint.scheme() != "https"
2907 && !(endpoint.scheme() == "http"
2908 && endpoint
2909 .host_str()
2910 .is_some_and(|h| h == "127.0.0.1" || h == "localhost" || h == "::1"))
2911 {
2912 return Err(SourceError::Config {
2913 message:
2914 "endpoint must use HTTPS (HTTP is accepted only for a loopback test server)"
2915 .into(),
2916 });
2917 }
2918 let token = secrets.get(&config.token_env).filter(|token| !token.expose_secret().trim().is_empty()).ok_or_else(|| SourceError::Auth {
2919 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),
2920 })?;
2921 Ok(Self {
2922 name: name.clone(),
2923 owner: config.owner,
2924 project_number: config.project_number,
2925 repository,
2926 endpoint,
2927 token,
2928 credential_name: config.token_env,
2929 statuses: StatusMapping::resolve(config.status_mapping, name)?,
2930 priorities: config
2931 .priority_mapping
2932 .map(|mapping| PriorityMapping::resolve(mapping, name))
2933 .transpose()?,
2934 client: Client::builder()
2935 .user_agent("onetaskgraph")
2936 .build()
2937 .map_err(|e| SourceError::Config {
2938 message: format!("cannot build HTTP client: {e}"),
2939 })?,
2940 created: Mutex::new(Vec::new()),
2941 pacing: Pacing::resolve(config.pacing, name)?,
2942 last_mutation: Mutex::new(None),
2943 board_cache: Mutex::new(None),
2944 search_cache: Mutex::new(None),
2945 fields_cache: Mutex::new(None),
2946 repository_cache: Mutex::new(BTreeMap::new()),
2947 ledger,
2948 })
2949 }
2950
2951 /// A snapshot of every request this source has sent, and what each cost.
2952 ///
2953 /// A value to hold and compare rather than a borrow of the accounting itself, so two
2954 /// of them can sit side by side. When this source was built with
2955 /// [`Self::recording_into`] the snapshot is the whole shared session, which is the
2956 /// point of building it that way.
2957 #[must_use]
2958 pub fn accounting(&self) -> accounting::Session {
2959 self.ledger.snapshot()
2960 }
2961
2962 /// Send one GraphQL document, pacing this source's own mutations and waiting out a
2963 /// rate limit rather than handing it straight back as an error.
2964 ///
2965 /// Retrying is safe for every document here, including the mutations, and the reason
2966 /// is that only a *refusal* is retried: [`Limiter::classify`] rules on a response
2967 /// GitHub sent, and a request GitHub refused for a rate limit did not run, so nothing
2968 /// this replays has already taken effect. An outcome this source cannot know — the
2969 /// send failed, or the body could not be read, so the mutation may well have landed —
2970 /// is [`Attempt::Failed`] in [`send_once`] and leaves this loop without a second
2971 /// attempt. A duplicate write would come from replaying one of those, and none is
2972 /// replayed.
2973 async fn graphql(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
2974 let doing = operation_description(query);
2975 let mut waited = Duration::ZERO;
2976 let mut waits = 0_u32;
2977 let mut backoff = self.pacing.retry_backoff;
2978 loop {
2979 if is_mutation(query) {
2980 let spacing = self.reserve_mutation_slot();
2981 if !spacing.is_zero() {
2982 tokio::time::sleep(spacing).await;
2983 }
2984 }
2985 let attempt = self.send_once(query, &variables).await;
2986 if is_mutation(query) {
2987 self.finish_mutation();
2988 }
2989 let limited = match attempt {
2990 Ok(data) => return Ok(data),
2991 Err(Attempt::Failed(error)) => return Err(error),
2992 Err(Attempt::Limited(limited)) => limited,
2993 };
2994 // GitHub really does send `retry-after: 0`, and retrying at once is the one
2995 // move that extends a secondary limit, so a hint below the schedule's own next
2996 // wait is raised to it.
2997 let wait = match limited.hint {
2998 Some(hint) => Duration::from_secs(hint).max(backoff),
2999 None => backoff,
3000 };
3001 let remaining = self.pacing.retry_budget.saturating_sub(waited);
3002 // A wait of nothing spends none of the budget, so it is exhaustion rather
3003 // than a retry. `Pacing::resolve` rules out every way of configuring one
3004 // except a budget of zero, where reporting the first refusal is the ask.
3005 if wait.is_zero() || wait > remaining {
3006 return Err(limited.exhausted(
3007 doing,
3008 waits,
3009 waited,
3010 wait,
3011 self.pacing.retry_budget,
3012 ));
3013 }
3014 tokio::time::sleep(wait).await;
3015 waited += wait;
3016 waits += 1;
3017 backoff = backoff.saturating_mul(2);
3018 }
3019 }
3020
3021 /// The next moment a content-creating mutation may leave this source, as a wait from
3022 /// now.
3023 ///
3024 /// The slot is reserved under the lock and the waiting happens outside it, so two
3025 /// callers take two slots rather than the same one — and no lock is held across an
3026 /// await.
3027 ///
3028 /// The moment it is spaced from is the previous mutation's *completion*, which
3029 /// [`Self::finish_mutation`] records. See that method for why the release moment on its
3030 /// own is the wrong thing to measure from.
3031 fn reserve_mutation_slot(&self) -> Duration {
3032 if self.pacing.min_mutation_interval.is_zero() {
3033 return Duration::ZERO;
3034 }
3035 // A poisoned lock here costs pacing, not correctness, and refusing the write over
3036 // it would turn an earlier failure into a second one for no gain.
3037 let mut last = self
3038 .last_mutation
3039 .lock()
3040 .unwrap_or_else(std::sync::PoisonError::into_inner);
3041 let now = Instant::now();
3042 // `checked_add` rather than `+`: `Instant + Duration` panics on overflow, and
3043 // pacing is not worth a panic even at a bound `MAX_PACING_MS` already rules out.
3044 let at = last.map_or(now, |previous| {
3045 previous
3046 .checked_add(self.pacing.min_mutation_interval)
3047 .map_or(now, |earliest| earliest.max(now))
3048 });
3049 *last = Some(at);
3050 at.saturating_duration_since(now)
3051 }
3052
3053 /// Record that a content-creating mutation has finished, so the next one is spaced
3054 /// from here rather than from the moment this one was released.
3055 ///
3056 /// This source can only choose when a request *departs*; the limiter counts when it
3057 /// *arrives*, and the two differ by whatever the request spent in transit. Spacing one
3058 /// departure from the last therefore hands the limiter a gap of the interval less that
3059 /// transit, so a source pacing at 750 ms can still be seen arriving faster — which is
3060 /// exactly how a copy paced well inside a board's threshold was refused by it on a
3061 /// slower machine while passing on a quick one.
3062 ///
3063 /// Spacing from completion removes the subtraction rather than budgeting for it. The
3064 /// previous request had already arrived before its response came back, so its arrival
3065 /// is no later than this moment, and the next mutation is released at least the
3066 /// interval after this moment and arrives no earlier than it is released: the gap the
3067 /// limiter measures is therefore at least the interval, whatever transit costs and on
3068 /// whatever platform. The price is that a mutation's own round trip no longer counts
3069 /// towards its spacing, which makes this source slightly slower than the configured
3070 /// rate rather than slightly faster — the safe side of a limit that punishes being
3071 /// wrong by refusing reads for the next fifty minutes.
3072 ///
3073 /// A failed attempt is recorded too: a request refused by the limiter still arrived,
3074 /// and one that never left costs only a wait nobody needed.
3075 fn finish_mutation(&self) {
3076 if self.pacing.min_mutation_interval.is_zero() {
3077 return;
3078 }
3079 // A poisoned lock here costs pacing, not correctness, exactly as in the reservation.
3080 let mut last = self
3081 .last_mutation
3082 .lock()
3083 .unwrap_or_else(std::sync::PoisonError::into_inner);
3084 let now = Instant::now();
3085 // `max` rather than an assignment: a concurrent caller may already have reserved a
3086 // slot further out, and completing this request must never pull that slot back in.
3087 *last = Some(last.map_or(now, |reserved| reserved.max(now)));
3088 }
3089
3090 /// One HTTP attempt, classified into an answer, a rate limit to wait out, or a
3091 /// failure that waiting cannot help — and recorded, whichever of the three it was.
3092 ///
3093 /// This is the one place a request leaves this crate, which is why the accounting is
3094 /// here rather than at each of the callers: a read path added later is counted without
3095 /// anybody remembering to count it, and
3096 /// `the_session_report_counts_every_request_the_board_served_and_what_each_cost` fails
3097 /// when one is not.
3098 async fn send_once(&self, query: &str, variables: &Value) -> Result<Value, Attempt> {
3099 let Attempted {
3100 result,
3101 limits,
3102 reported_cost,
3103 } = self.attempt(query, variables).await;
3104 // No `otherwise` name: every document this source sends is one of its own, and the
3105 // inventory gate on `graphql::DOCUMENTS` is what keeps that true.
3106 let sending = accounting::Request::graphql(query, variables, None, reported_cost);
3107 let outcome = match &result {
3108 Ok(_) => accounting::Outcome::Answered,
3109 Err(Attempt::Limited(_)) => accounting::Outcome::RateLimited,
3110 Err(Attempt::Failed(_)) => accounting::Outcome::Refused,
3111 };
3112 self.ledger.record(sending.finished(outcome, limits));
3113 result
3114 }
3115
3116 /// The attempt itself, with what its response said about the rate limit alongside.
3117 ///
3118 /// The two are returned together rather than recorded here because every one of the
3119 /// early exits below is a different outcome, and a record written at each of them is a
3120 /// record one of them can be added without.
3121 async fn attempt(&self, query: &str, variables: &Value) -> Attempted {
3122 let mut limits = accounting::RateLimit::default();
3123 let mut reported_cost = None;
3124 let result = self
3125 .attempted(query, variables, &mut limits, &mut reported_cost)
3126 .await;
3127 Attempted {
3128 result,
3129 limits,
3130 reported_cost,
3131 }
3132 }
3133
3134 /// One HTTP attempt, filling in what its response said about the rate limit as it goes.
3135 async fn attempted(
3136 &self,
3137 query: &str,
3138 variables: &Value,
3139 limits: &mut accounting::RateLimit,
3140 reported_cost: &mut Option<u64>,
3141 ) -> Result<Value, Attempt> {
3142 let response = self
3143 .client
3144 .post(self.endpoint.clone())
3145 .bearer_auth(self.token.expose_secret())
3146 .json(&json!({"query": query, "variables": variables}))
3147 .send()
3148 .await
3149 .map_err(|e| {
3150 Attempt::Failed(SourceError::Unavailable {
3151 message: format!("GitHub GraphQL request failed: {e}"),
3152 })
3153 })?;
3154 let status = response.status();
3155 let header = |name: &str| whole_seconds(response.headers().get(name));
3156 *limits = accounting::RateLimit::read(|name| {
3157 response
3158 .headers()
3159 .get(name)
3160 .and_then(|value| value.to_str().ok())
3161 .map(str::to_owned)
3162 });
3163 // Exactly `0` is exhaustion and everything else — a count, an empty value, bytes
3164 // that are not text at all — is "not known to be exhausted". This never makes a
3165 // response a refusal on its own: it says which limiter a refusal is attributed to
3166 // and where its hint comes from, so a value this cannot read costs a hint rather
3167 // than an answer.
3168 let exhausted = response
3169 .headers()
3170 .get("x-ratelimit-remaining")
3171 .and_then(|value| value.to_str().ok())
3172 == Some("0");
3173 // `retry-after` is what GitHub asks for when it asks; when it does not and the
3174 // primary budget is spent, `x-ratelimit-reset` says when that budget comes back,
3175 // which is the same question answered as an absolute time. Nothing else here is a
3176 // hint, and a schedule is what answers a refusal that carries none.
3177 let hint = header("retry-after").or_else(|| {
3178 exhausted
3179 .then(|| header("x-ratelimit-reset"))
3180 .flatten()
3181 .map(|reset| reset.saturating_sub(Utc::now().timestamp().max(0).unsigned_abs()))
3182 });
3183 // Read before it is parsed, because the evidence which tells a secondary rate
3184 // limit from a rejected credential is in the body of a response whose status says
3185 // only "forbidden" — and a non-success response was never parsed at all.
3186 let body = response.text().await.map_err(|e| {
3187 Attempt::Failed(SourceError::Unavailable {
3188 message: format!("GitHub GraphQL response could not be read: {e}"),
3189 })
3190 })?;
3191 if let Some(limiter) = Limiter::classify(status, exhausted, &body) {
3192 return Err(Attempt::Limited(Limited { limiter, hint }));
3193 }
3194 if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {
3195 return Err(Attempt::Failed(SourceError::Auth {
3196 message: format!(
3197 "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"
3198 ),
3199 }));
3200 }
3201 if !status.is_success() {
3202 return Err(Attempt::Failed(SourceError::Unavailable {
3203 message: format!("GitHub GraphQL returned HTTP {status}"),
3204 }));
3205 }
3206 // GitHub reports what a call cost only when the document asked it to, and no
3207 // document this source sends does — so this is `None` here and carries the figure
3208 // for a caller whose own document selects `rateLimit { cost }`. What it must never
3209 // pick up is a `dryRun` probe's cost, which is some other document's.
3210 *reported_cost = serde_json::from_str::<Value>(&body)
3211 .ok()
3212 .as_ref()
3213 .and_then(|body| body.pointer("/data/rateLimit/cost"))
3214 .and_then(Value::as_u64);
3215 self.answer(&body).map_err(Attempt::Failed)
3216 }
3217
3218 /// What one successful HTTP response says, once its GraphQL errors are read.
3219 fn answer(&self, body: &str) -> Result<Value, SourceError> {
3220 let body: Value = serde_json::from_str(body).map_err(|e| SourceError::Malformed {
3221 message: format!("GitHub returned invalid JSON: {e}"),
3222 })?;
3223 let errors = body
3224 .get("errors")
3225 .map(|value| {
3226 value.as_array().ok_or_else(|| SourceError::Malformed {
3227 message: "GitHub response errors is not an array".into(),
3228 })
3229 })
3230 .transpose()?;
3231 if let Some(errors) = errors.filter(|errors| !errors.is_empty()) {
3232 let messages = errors
3233 .iter()
3234 .filter_map(|e| e.get("message").and_then(Value::as_str))
3235 .collect::<Vec<_>>()
3236 .join("; ");
3237 let message = if messages.is_empty() {
3238 "GitHub returned GraphQL errors".into()
3239 } else {
3240 messages
3241 };
3242 let normalized = message.to_ascii_lowercase();
3243 if normalized.contains("resource not accessible") || normalized.contains("scope") {
3244 return Err(SourceError::Auth {
3245 message: format!(
3246 "{message}; grant {} Projects and Issues read/write plus Pull requests read-only access for every repository represented on the board",
3247 self.credential_name
3248 ),
3249 });
3250 }
3251 return Err(SourceError::Refused { message });
3252 }
3253 body.get("data")
3254 .filter(|data| data.is_object())
3255 .cloned()
3256 .ok_or_else(|| SourceError::Malformed {
3257 message: "GitHub response has no data object".into(),
3258 })
3259 }
3260
3261 // llmlint: ignore[boundary_inputs_validated] GitHub caps nested connections at 100 and
3262 // GraphQL cannot independently page them inside the outer item page. This source page is
3263 // deliberately bounded at that published maximum; the live drift journey exercises it.
3264 async fn board_page(
3265 &self,
3266 items_after: Option<&str>,
3267 items_first: u32,
3268 ) -> Result<Value, SourceError> {
3269 let data = self
3270 .graphql(
3271 graphql::BOARD,
3272 json!({"owner":self.owner,"number":self.project_number,
3273 "first":items_first.min(MAX_PAGE_SIZE),"after":items_after,
3274 "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
3275 )
3276 .await?;
3277 data.pointer("/owner/projectV2")
3278 .filter(|v| !v.is_null())
3279 .cloned()
3280 .ok_or_else(|| SourceError::Refused {
3281 message: format!(
3282 "GitHub project {}/{} was not found or is not visible to the token",
3283 self.owner, self.project_number
3284 ),
3285 })
3286 }
3287
3288 /// The search that finds the issues of this board, narrowed by `also` when it is
3289 /// given.
3290 ///
3291 /// `project:owner/number` is what scopes a search to one board, and `is:issue` is what
3292 /// keeps pull requests out of it: GitHub's `ISSUE` search type covers both, and a pull
3293 /// request is somebody's change rather than a unit of plan. `-has:parent` is *not*
3294 /// here on purpose — GitHub accepts it and silently ignores it, so a project is told
3295 /// from a task by the `parent` field each issue carries rather than by the search.
3296 fn board_search(&self, also: Option<&str>) -> String {
3297 let scope = format!("project:{}/{} is:issue", self.owner, self.project_number);
3298 match also {
3299 Some(also) => format!("{scope} {also}"),
3300 None => scope,
3301 }
3302 }
3303
3304 /// One issue this source reached directly, as the board item a read of the board would
3305 /// have produced — or `None` when this board does not hold it.
3306 ///
3307 /// The board half of an issue rides along on `Issue.projectItems`, so the value handed
3308 /// to [`Self::resolve`] is the very shape a `ProjectV2.items` read gives it: the board
3309 /// item's own id, that item's field values, and the issue as its content. One resolver
3310 /// for both routes is what makes an issue read through a search, through its own node
3311 /// id, or through its project's sub-issues report the same title, the same status, the
3312 /// same labels and the same qualified id.
3313 ///
3314 /// An issue with no entry for *this* board is not this source's to report, which is
3315 /// what keeps an id naming some other repository's issue from being answered as an item
3316 /// of this board. That answer is given about an **exhausted** connection and never
3317 /// about an unread page: the entry is looked for on the page in hand, and only if that
3318 /// page reports more of the connection, in [`Self::board_membership`]'s walk of the
3319 /// rest of it.
3320 async fn resolve_issue(&self, issue: &Value) -> Result<Option<Resolved>, SourceError> {
3321 if optional_str(issue, "__typename")? != Some("Issue") {
3322 return Ok(None);
3323 }
3324 let memberships = issue
3325 .get("projectItems")
3326 .ok_or_else(|| SourceError::Malformed {
3327 message: "GitHub issue is missing projectItems".into(),
3328 })?;
3329 let nodes = memberships
3330 .get("nodes")
3331 .and_then(Value::as_array)
3332 .ok_or_else(|| SourceError::Malformed {
3333 message: "GitHub issue projectItems.nodes is not an array".into(),
3334 })?;
3335 let held = match self.board_entry(nodes) {
3336 Some(held) => held.clone(),
3337 None => {
3338 let info = memberships
3339 .get("pageInfo")
3340 .ok_or_else(|| SourceError::Malformed {
3341 message: "GitHub issue projectItems has no pageInfo".into(),
3342 })?;
3343 // The page held no entry for this board. Whether that means the issue is
3344 // not on it is a question about the rest of the connection, and only a
3345 // connection with no rest answers it here.
3346 if !required_bool(info, "hasNextPage")? {
3347 return Ok(None);
3348 }
3349 let cursor = required_str(info, "endCursor")?;
3350 validate_cursor_progress(None, cursor)?;
3351 let issue_id = required_str(issue, "id")?;
3352 match self.board_membership(issue_id, cursor).await? {
3353 Some(held) => held,
3354 None => return Ok(None),
3355 }
3356 }
3357 };
3358 let item = json!({
3359 "id": required_str(&held, "id")?,
3360 "project": held.get("project"),
3361 "fieldValues": held.get("fieldValues"),
3362 "content": issue,
3363 });
3364 self.resolve(&item)
3365 }
3366
3367 /// This board's own entry among one page of an issue's `Issue.projectItems`.
3368 ///
3369 /// One spelling of *which membership is this board's*, so the page a read carries and
3370 /// the pages [`Self::board_membership`] walks are searched by the same rule.
3371 fn board_entry<'a>(&self, nodes: &'a [Value]) -> Option<&'a Value> {
3372 nodes.iter().find(|node| {
3373 node.pointer("/project/number").and_then(Value::as_u64)
3374 == Some(u64::from(self.project_number))
3375 })
3376 }
3377
3378 /// The rest of one issue's board memberships, from `after`, for this board's entry.
3379 ///
3380 /// The recovery read: a page of memberships that holds no entry for this board says
3381 /// nothing about the memberships past it, so the connection is walked to exhaustion
3382 /// before an issue is reported as one this board does not hold. `Ok(None)` is that
3383 /// positive answer — the whole connection was read and no entry named this board —
3384 /// rather than a failure, and the walk is held to
3385 /// [`validate_cursor_progress`] like every other page walk here, so a source answering
3386 /// with a cursor that does not advance is refused instead of spun on.
3387 async fn board_membership(
3388 &self,
3389 issue: &str,
3390 after: &str,
3391 ) -> Result<Option<Value>, SourceError> {
3392 let mut after = after.to_owned();
3393 loop {
3394 let data = self
3395 .graphql(
3396 graphql::ISSUE_BOARD_ITEMS,
3397 json!({"id":issue,"first":MAX_PAGE_SIZE,"after":after,
3398 "nestedFirst":NESTED_PAGE_SIZE}),
3399 )
3400 .await?;
3401 let Some(connection) = data
3402 .pointer("/node/projectItems")
3403 .filter(|value| !value.is_null())
3404 else {
3405 // The id resolved to nothing, or to something with no memberships to walk —
3406 // which is the same answer as a connection holding no entry for this board.
3407 return Ok(None);
3408 };
3409 let nodes = connection
3410 .get("nodes")
3411 .and_then(Value::as_array)
3412 .ok_or_else(|| SourceError::Malformed {
3413 message: "GitHub issue projectItems.nodes is not an array".into(),
3414 })?;
3415 if let Some(held) = self.board_entry(nodes) {
3416 return Ok(Some(held.clone()));
3417 }
3418 let info = connection
3419 .get("pageInfo")
3420 .ok_or_else(|| SourceError::Malformed {
3421 message: "GitHub issue projectItems has no pageInfo".into(),
3422 })?;
3423 let next = required_bool(info, "hasNextPage")?
3424 .then(|| required_str(info, "endCursor"))
3425 .transpose()?;
3426 match next {
3427 Some(next) => {
3428 validate_cursor_progress(Some(&after), next)?;
3429 after = next.to_owned();
3430 }
3431 None => return Ok(None),
3432 }
3433 }
3434 }
3435
3436 /// One page of a board-scoped issue search, and where the next page resumes.
3437 async fn search_page(
3438 &self,
3439 search: &str,
3440 first: u32,
3441 after: Option<&str>,
3442 ) -> Result<(Vec<Resolved>, Option<String>), SourceError> {
3443 let data = self
3444 .graphql(
3445 graphql::SEARCH_ISSUES,
3446 json!({"search":search,"type":"ISSUE","first":first.min(MAX_PAGE_SIZE),
3447 "after":after,"nestedFirst":NESTED_PAGE_SIZE,
3448 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
3449 )
3450 .await?;
3451 let connection = data.get("search").ok_or_else(|| SourceError::Malformed {
3452 message: "GitHub search response has no search connection".into(),
3453 })?;
3454 let mut found = Vec::new();
3455 for node in connection
3456 .get("nodes")
3457 .and_then(Value::as_array)
3458 .ok_or_else(|| SourceError::Malformed {
3459 message: "GitHub search nodes is not an array".into(),
3460 })?
3461 {
3462 if let Some(resolved) = self.resolve_issue(node).await? {
3463 found.push(resolved);
3464 }
3465 }
3466 let info = connection
3467 .get("pageInfo")
3468 .ok_or_else(|| SourceError::Malformed {
3469 message: "GitHub search connection has no pageInfo".into(),
3470 })?;
3471 let next = required_bool(info, "hasNextPage")?
3472 .then(|| required_str(info, "endCursor"))
3473 .transpose()?
3474 .map(str::to_owned);
3475 if let Some(next) = &next {
3476 validate_cursor_progress(after, next)?;
3477 }
3478 Ok((found, next))
3479 }
3480
3481 /// Every issue this board holds, completed with what this run wrote.
3482 ///
3483 /// The completion is not an optimisation and it is not a cache: GitHub's issue search
3484 /// is an index and is eventually consistent, so an issue this run created seconds ago
3485 /// can be absent from it, and a project listed straight after being written would
3486 /// otherwise be missing from its own board. What is added back is only what this
3487 /// process itself wrote, out of [`Self::created`], which lives and dies with the
3488 /// process.
3489 async fn board_issues(&self) -> Result<Vec<Resolved>, SourceError> {
3490 let found = self.searched_issues().await?;
3491 self.completed_with_written(found, |_| true)
3492 }
3493
3494 /// Every issue this board's own search reports, walked to exhaustion, read once per
3495 /// source.
3496 ///
3497 /// The uncompleted half of [`Self::board_issues`], separated because [`Self::board`]
3498 /// needs it too and the two would otherwise walk the same search twice in one command.
3499 /// See [`Self::search_cache`] for why holding it is the same bargain holding the board
3500 /// is.
3501 async fn searched_issues(&self) -> Result<Vec<Resolved>, SourceError> {
3502 let cached = self.search_cache()?.clone();
3503 if let Some(held) = cached {
3504 return Ok(held);
3505 }
3506 let mut after: Option<String> = None;
3507 let mut found = Vec::new();
3508 let search = self.board_search(None);
3509 loop {
3510 let (page, next) = self
3511 .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
3512 .await?;
3513 found.extend(page);
3514 match next {
3515 Some(next) => after = Some(next),
3516 None => break,
3517 }
3518 }
3519 *self.search_cache()? = Some(found.clone());
3520 Ok(found)
3521 }
3522
3523 /// This process's own view of the board's issues, or the refusal a poisoned lock is.
3524 fn search_cache(
3525 &self,
3526 ) -> Result<std::sync::MutexGuard<'_, Option<Vec<Resolved>>>, SourceError> {
3527 self.search_cache
3528 .lock()
3529 .map_err(|_| SourceError::Unavailable {
3530 message: "this source's view of the board's issues was left inconsistent by an \
3531 earlier failure; next: run the command again"
3532 .into(),
3533 })
3534 }
3535
3536 /// `found`, with everything this run wrote that `keep` accepts and the read did not
3537 /// report.
3538 ///
3539 /// See [`Self::created`] and [`Self::board_issues`] for why a read has to be completed
3540 /// at all: the search index is behind, and a node read of an item filed moments ago can
3541 /// be too.
3542 fn completed_with_written(
3543 &self,
3544 mut found: Vec<Resolved>,
3545 keep: impl Fn(&Resolved) -> bool,
3546 ) -> Result<Vec<Resolved>, SourceError> {
3547 for own in self.created()?.iter().filter(|own| keep(own)) {
3548 if !found.iter().any(|item| item.id == own.id) {
3549 found.push(own.clone());
3550 }
3551 }
3552 Ok(found)
3553 }
3554
3555 /// What resolving one node id reached.
3556 ///
3557 /// Three answers rather than an `Option`, because a board *draft* is none of the other
3558 /// two: it is not an issue, so the issue fragment reads nothing of it, and a read of one
3559 /// is completed by a read of the draft itself rather than reported as nothing.
3560 async fn reach(&self, id: &NativeId) -> Result<Reached, SourceError> {
3561 let asked = self
3562 .graphql(
3563 graphql::ISSUE,
3564 json!({"id":id.0,"nestedFirst":NESTED_PAGE_SIZE,
3565 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
3566 )
3567 .await;
3568 let data = match asked {
3569 Ok(data) => data,
3570 // A string that is not a node id at all is not a failure to report: it is an id
3571 // this board does not hold, which is what every read of one already answers.
3572 Err(error) if unresolvable_node(&error) => return Ok(Reached::Nothing),
3573 Err(error) => return Err(error),
3574 };
3575 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
3576 return Ok(Reached::Nothing);
3577 };
3578 if optional_str(node, "__typename")? == Some("DraftIssue") {
3579 return Ok(Reached::Draft);
3580 }
3581 Ok(match self.resolve_issue(node).await? {
3582 Some(item) => Reached::Held(Box::new(item)),
3583 None => Reached::Nothing,
3584 })
3585 }
3586
3587 /// One item of this board by its own id, whatever kind it is.
3588 ///
3589 /// Resolved from the identifier alone: no search, board-wide or otherwise. What this
3590 /// run wrote is read first, because a node read of an item created moments ago can
3591 /// still be behind the board field values written onto it — see [`Self::created`].
3592 async fn item_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
3593 if let Some(own) = self.created()?.iter().find(|own| own.id == *id) {
3594 return Ok(Some(own.clone()));
3595 }
3596 match self.reach(id).await? {
3597 Reached::Held(item) => Ok(Some(*item)),
3598 Reached::Nothing => Ok(None),
3599 Reached::Draft => self.draft_by_id(id).await,
3600 }
3601 }
3602
3603 /// One board draft by its own id, with the board item it sits in — or `None` when no
3604 /// item of this board is that draft's.
3605 ///
3606 /// The same decision [`Self::resolve_issue`] makes for an issue, over the draft's own
3607 /// `projectV2Items`: an entry naming this board is what makes it this board's. GitHub
3608 /// links a draft to one board item, so the page this read carries is the whole of that
3609 /// connection, and a page that reports more than it holds is refused rather than read
3610 /// as an answer about memberships nobody read.
3611 async fn draft_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
3612 let data = self
3613 .graphql(
3614 graphql::DRAFT,
3615 json!({"id":id.0,"nestedFirst":NESTED_PAGE_SIZE,
3616 "boardItems":BOARD_ITEMS_PAGE_SIZE}),
3617 )
3618 .await?;
3619 // Gone between the two reads is an answer — the draft is no longer there. Anything
3620 // else than the draft [`Self::reach`] was just told this id is, is not one.
3621 let Some(draft) = data.get("node").filter(|node| !node.is_null()) else {
3622 return Ok(None);
3623 };
3624 if optional_str(draft, "__typename")? != Some("DraftIssue") {
3625 return Err(SourceError::Malformed {
3626 message: format!(
3627 "GitHub answered {} as a draft and then as something else",
3628 id.0
3629 ),
3630 });
3631 }
3632 if required_str(draft, "id")? != id.0 {
3633 return Err(SourceError::Malformed {
3634 message: format!("GitHub answered a different draft for {}", id.0),
3635 });
3636 }
3637 let memberships = draft
3638 .get("projectV2Items")
3639 .ok_or_else(|| SourceError::Malformed {
3640 message: format!("GitHub draft {} is missing projectV2Items", id.0),
3641 })?;
3642 let nodes = memberships
3643 .get("nodes")
3644 .and_then(Value::as_array)
3645 .ok_or_else(|| SourceError::Malformed {
3646 message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
3647 })?;
3648 let info = memberships
3649 .get("pageInfo")
3650 .ok_or_else(|| SourceError::Malformed {
3651 message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
3652 })?;
3653 // Read whether or not this board's entry is on the page: a page claiming more than
3654 // the one item GitHub links a draft to is a malformed answer either way.
3655 if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
3656 return Err(SourceError::Malformed {
3657 message: format!(
3658 "GitHub draft {} reports more board items than the one GitHub links a draft \
3659 to",
3660 id.0
3661 ),
3662 });
3663 }
3664 if let Some(node) = nodes.first()
3665 && node
3666 .pointer("/project/number")
3667 .and_then(Value::as_u64)
3668 .is_none()
3669 {
3670 return Err(SourceError::Malformed {
3671 message: format!(
3672 "GitHub draft {} board item has no numeric project number",
3673 id.0
3674 ),
3675 });
3676 }
3677 let Some(held) = self.board_entry(nodes) else {
3678 return Ok(None);
3679 };
3680 if required_str(
3681 held.get("project").ok_or_else(|| SourceError::Malformed {
3682 message: format!("GitHub draft {} board item has no project", id.0),
3683 })?,
3684 "id",
3685 )? != self.board_fields().await?.id.as_str()
3686 {
3687 return Ok(None);
3688 }
3689 let item = json!({
3690 "id": required_str(held, "id")?,
3691 "project": held.get("project"),
3692 "fieldValues": held.get("fieldValues"),
3693 "content": draft,
3694 });
3695 self.resolve(&item)
3696 }
3697
3698 /// The board's own id and field definitions, for a write whose item does not carry
3699 /// them — never its items.
3700 ///
3701 /// A board this command has already listed supplies them, since it read them beside its
3702 /// items; otherwise they come from [`graphql::BOARD_FIELDS`], once per command. Neither
3703 /// is consulted about which items the board holds: see the module documentation for
3704 /// why a question about one known item is answered by reading that item.
3705 async fn board_fields(&self) -> Result<BoardFields, SourceError> {
3706 if let Some(board) = self.board_cache()?.as_ref() {
3707 return Ok(BoardFields {
3708 id: BoardId::parse(&board.id)?,
3709 fields: board.fields.clone(),
3710 });
3711 }
3712 if let Some(held) = self.fields_cache()?.clone() {
3713 return Ok(held);
3714 }
3715 let data = self
3716 .graphql(
3717 graphql::BOARD_FIELDS,
3718 json!({"owner":self.owner,"number":self.project_number,
3719 "nestedFirst":NESTED_PAGE_SIZE}),
3720 )
3721 .await?;
3722 let board = data
3723 .pointer("/boardFields/projectV2")
3724 .filter(|value| !value.is_null())
3725 .ok_or_else(|| SourceError::Refused {
3726 message: format!(
3727 "GitHub project {}/{} was not found or is not visible to the token",
3728 self.owner, self.project_number
3729 ),
3730 })?;
3731 let read = BoardFields {
3732 id: BoardId::parse(required_str(board, "id")?)?,
3733 fields: board.get("fields").cloned().unwrap_or(Value::Null),
3734 };
3735 *self.fields_cache()? = Some(read.clone());
3736 Ok(read)
3737 }
3738
3739 /// This process's own view of the board's fields, or the refusal a poisoned lock is.
3740 fn fields_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<BoardFields>>, SourceError> {
3741 self.fields_cache
3742 .lock()
3743 .map_err(|_| SourceError::Unavailable {
3744 message: "this source's view of the board's fields was left inconsistent by an \
3745 earlier failure; next: run the command again"
3746 .into(),
3747 })
3748 }
3749
3750 /// What a write to `item` needs of the board, read off that item when it says enough and
3751 /// off [`Self::board_fields`] when it does not.
3752 ///
3753 /// A node read of an item names its board and carries the definition of every field it
3754 /// holds a value of — so an item naming its board, holding a value of the origin field,
3755 /// and, when the write carries a status, holding a `Status` value, needs no read of the
3756 /// board at all. **Nothing the item does not say is guessed:** a field it holds no value
3757 /// of may still be on the board, and a view reading it as absent would refuse a write the
3758 /// board can take or skip a field write the board needs, so such an item — and a create,
3759 /// which has no item yet — takes the board's fields from their own read instead.
3760 async fn fields_for(
3761 &self,
3762 item: Option<&Resolved>,
3763 writes_status: bool,
3764 selects_priority: bool,
3765 ) -> Result<BoardFields, SourceError> {
3766 if let Some(item) = item
3767 && let Some(board_id) = item.named_board()
3768 && item.defines(ORIGIN_FIELD)
3769 && (!writes_status || item.defines("Status"))
3770 && (!selects_priority || item.defines(PRIORITY_FIELD))
3771 {
3772 return Ok(BoardFields {
3773 id: board_id,
3774 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
3775 });
3776 }
3777 self.board_fields().await
3778 }
3779
3780 /// Everything filed under one issue of this board, walked to exhaustion — or `None`
3781 /// when that id names nothing here with a sub-issue relationship to walk.
3782 ///
3783 /// `None` and an empty answer are different: `None` is *this is not an issue of this
3784 /// GitHub*, which is what sends a project selector on to be read as a name, and an
3785 /// empty vector is a project that holds nothing.
3786 async fn sub_issues(&self, id: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
3787 let mut after: Option<String> = None;
3788 let mut children = Vec::new();
3789 loop {
3790 let asked = self
3791 .graphql(
3792 graphql::SUB_ISSUES,
3793 json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after,
3794 "nestedFirst":NESTED_PAGE_SIZE,
3795 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
3796 )
3797 .await;
3798 let data = match asked {
3799 Ok(data) => data,
3800 // A string that is not a node id at all is not a failure to report: it is
3801 // the ordinary answer to a selector naming a project by its name.
3802 Err(error) if unresolvable_node(&error) => return Ok(None),
3803 Err(error) => return Err(error),
3804 };
3805 let Some(connection) = data
3806 .pointer("/node/subIssues")
3807 .filter(|value| !value.is_null())
3808 else {
3809 // No such node, or one with no sub-issue relationship — a board draft is
3810 // the one this board can really hold.
3811 return Ok(None);
3812 };
3813 for node in connection
3814 .get("nodes")
3815 .and_then(Value::as_array)
3816 .ok_or_else(|| SourceError::Malformed {
3817 message: "GitHub subIssues.nodes is not an array".into(),
3818 })?
3819 {
3820 if let Some(resolved) = self.resolve_issue(node).await? {
3821 children.push(resolved);
3822 }
3823 }
3824 let info = connection
3825 .get("pageInfo")
3826 .ok_or_else(|| SourceError::Malformed {
3827 message: "GitHub subIssues connection has no pageInfo".into(),
3828 })?;
3829 let next = required_bool(info, "hasNextPage")?
3830 .then(|| required_str(info, "endCursor"))
3831 .transpose()?;
3832 match next {
3833 Some(next) => {
3834 validate_cursor_progress(after.as_deref(), next)?;
3835 after = Some(next.to_owned());
3836 }
3837 None => return Ok(Some(children)),
3838 }
3839 }
3840 }
3841
3842 /// Which issue of this board a project *name* is, or `None` when none is.
3843 ///
3844 /// One bounded query which filters on that name at the server, rather than a walk of
3845 /// every issue the board holds. The name is compared again here: the qualifier narrows
3846 /// what GitHub sends, and this source decides what it names.
3847 async fn project_by_name(&self, name: &str) -> Result<Option<NativeId>, SourceError> {
3848 let search = self.board_search(Some(&title_qualifier(name)));
3849 let (candidates, _) = self.search_page(&search, MAX_PAGE_SIZE, None).await?;
3850 Ok(candidates
3851 .into_iter()
3852 .find(|item| {
3853 item.kind == BoardKind::Work(ItemKind::Project)
3854 && item.title.eq_ignore_ascii_case(name)
3855 })
3856 .map(|item| item.id))
3857 }
3858
3859 /// Everything filed under one project of this board: the sub-issues of the issue that
3860 /// project is.
3861 ///
3862 /// Tasks *and* documents, because a document filed under a project is a sub-issue of it
3863 /// too — the caller keeps the kind it asked for. Nothing about this grows as the board
3864 /// gains projects, or as another project gains tasks.
3865 ///
3866 /// A qualified id names the issue and is asked for its sub-issues directly: one
3867 /// request, no search of any kind. Only a selector GitHub cannot resolve that way is
3868 /// read as a project *name*, which costs the one bounded search
3869 /// [`Self::project_by_name`] makes.
3870 async fn project_children(&self, selector: &NativeId) -> Result<Vec<Resolved>, SourceError> {
3871 let (project, children) = match self.sub_issues(selector).await? {
3872 Some(children) => (selector.clone(), children),
3873 None => match self.project_by_name(&selector.0).await? {
3874 Some(project) => {
3875 let children = self.sub_issues(&project).await?.unwrap_or_default();
3876 (project, children)
3877 }
3878 None => return Ok(Vec::new()),
3879 },
3880 };
3881 self.completed_with_written(children, |own| own.parent.as_ref() == Some(&project))
3882 }
3883
3884 /// Every item on the board: the union of both enumerations GitHub offers of one.
3885 ///
3886 /// Neither contains the other, so neither is dropped — only `ProjectV2.items` lists a
3887 /// board **draft** and reads the board's own fields beside its items, and only the search
3888 /// reports an item that connection is behind on. The module documentation is where the lag and the
3889 /// measurements behind it are written down.
3890 ///
3891 /// A search result is admitted on the same terms as any other issue this source reaches
3892 /// directly — [`Self::resolve_issue`] keeps it only if that issue's own `projectItems`
3893 /// names *this* board — so an issue the index still believes is here after it was taken
3894 /// off is refused rather than reported.
3895 ///
3896 /// See [`Self::board_cache`]. Both completions happen on every call rather than once,
3897 /// which is what the cache could otherwise have broken.
3898 async fn board(&self) -> Result<Board, SourceError> {
3899 let cached = self.board_cache()?.clone();
3900 let mut board = match cached {
3901 Some(board) => board,
3902 None => {
3903 let read = self.read_board().await?;
3904 *self.board_cache()? = Some(read.clone());
3905 read
3906 }
3907 };
3908 for held in self.searched_issues().await? {
3909 if !board.items.iter().any(|item| item.id == held.id) {
3910 board.items.push(held);
3911 }
3912 }
3913 for own in self.created()?.iter() {
3914 if !board.items.iter().any(|item| item.id == own.id) {
3915 board.items.push(own.clone());
3916 }
3917 }
3918 Ok(board)
3919 }
3920
3921 /// This process's own view of the board, or the refusal a poisoned lock is.
3922 fn board_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<Board>>, SourceError> {
3923 self.board_cache
3924 .lock()
3925 .map_err(|_| SourceError::Unavailable {
3926 message: "this source's view of the board was left inconsistent by an earlier \
3927 failure; next: run the command again"
3928 .into(),
3929 })
3930 }
3931
3932 /// Bring this process's own view of the board up to an item it has just written.
3933 ///
3934 /// A created item goes to `created`, which is what completes a board read GitHub's own
3935 /// eventual consistency has left behind. An item that was already there is replaced
3936 /// where it sits, so a second write of it in the same command reads its real parent
3937 /// rather than the one it had before the first write.
3938 ///
3939 /// "Where it sits" is three places, and missing an earlier one leaves a stale record
3940 /// that wins: an item this same run created is held in `created` and not in the cached
3941 /// board, and `board` completes the cached board *from* `created`, so replacing only
3942 /// the cached copy of such an item replaces nothing and the read still reports the
3943 /// title it was created with. The search is the third, and it is the one an item the
3944 /// board's own projection is behind on sits in *alone* — which is exactly the item this
3945 /// source is least able to re-read, so leaving it out would put the stale title back on
3946 /// the only items the completion in [`Self::board`] exists for.
3947 fn remember_written(&self, item: Resolved, created: bool) -> Result<(), SourceError> {
3948 if created {
3949 self.created()?.push(item);
3950 return Ok(());
3951 }
3952 {
3953 let mut own = self.created()?;
3954 if let Some(held) = own.iter_mut().find(|held| held.id == item.id) {
3955 *held = item;
3956 return Ok(());
3957 }
3958 }
3959 if let Some(board) = self.board_cache()?.as_mut()
3960 && let Some(held) = board.items.iter_mut().find(|held| held.id == item.id)
3961 {
3962 *held = item.clone();
3963 }
3964 if let Some(found) = self.search_cache()?.as_mut()
3965 && let Some(held) = found.iter_mut().find(|held| held.id == item.id)
3966 {
3967 *held = item;
3968 }
3969 Ok(())
3970 }
3971
3972 /// Forget one item this process has just deleted, from every half of its own view.
3973 fn forget(&self, id: &NativeId) -> Result<(), SourceError> {
3974 self.created()?.retain(|own| own.id != *id);
3975 if let Some(board) = self.board_cache()?.as_mut() {
3976 board.items.retain(|item| item.id != *id);
3977 }
3978 if let Some(found) = self.search_cache()?.as_mut() {
3979 found.retain(|item| item.id != *id);
3980 }
3981 Ok(())
3982 }
3983
3984 /// Every page of the board, read from GitHub.
3985 async fn read_board(&self) -> Result<Board, SourceError> {
3986 let mut after: Option<String> = None;
3987 let mut items = Vec::new();
3988 let mut board;
3989 loop {
3990 let page = self.board_page(after.as_deref(), MAX_PAGE_SIZE).await?;
3991 for item in page
3992 .pointer("/items/nodes")
3993 .and_then(Value::as_array)
3994 .ok_or_else(|| SourceError::Malformed {
3995 message: "GitHub project items.nodes is not an array".into(),
3996 })?
3997 {
3998 if let Some(resolved) = self.resolve(item)? {
3999 items.push(resolved);
4000 }
4001 }
4002 let info = page
4003 .pointer("/items/pageInfo")
4004 .ok_or_else(|| SourceError::Malformed {
4005 message: "GitHub project items have no pageInfo".into(),
4006 })?;
4007 let has_next = required_bool(info, "hasNextPage")?;
4008 let next = has_next
4009 .then(|| required_str(info, "endCursor"))
4010 .transpose()?;
4011 board = page.clone();
4012 match next {
4013 Some(next) => {
4014 validate_cursor_progress(after.as_deref(), next)?;
4015 after = Some(next.to_owned());
4016 }
4017 None => break,
4018 }
4019 }
4020 Ok(Board {
4021 id: required_str(&board, "id")?.to_owned(),
4022 fields: board.get("fields").cloned().unwrap_or(Value::Null),
4023 items,
4024 })
4025 }
4026
4027 /// The items this source has created, for completing a board read that is behind.
4028 fn created(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
4029 self.created.lock().map_err(|_| SourceError::Unavailable {
4030 message: "this source's record of what it created in this run was left \
4031 inconsistent by an earlier failure; next: run the command again"
4032 .into(),
4033 })
4034 }
4035
4036 /// One board item as this source reports it, or `None` for content it ignores.
4037 ///
4038 /// A pull request is neither a project nor a task — it is somebody's change, not a
4039 /// unit of plan — and an item whose content the token cannot see has nothing to
4040 /// report at all.
4041 fn resolve(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
4042 let content = item.get("content").ok_or_else(|| SourceError::Malformed {
4043 message: "GitHub project item is missing content".into(),
4044 })?;
4045 if content.is_null() {
4046 return Ok(None);
4047 }
4048 let content_kind = match required_str(content, "__typename")? {
4049 "Issue" => ContentKind::Issue,
4050 "DraftIssue" => ContentKind::DraftIssue,
4051 _ => return Ok(None),
4052 };
4053 let field_values = item
4054 .get("fieldValues")
4055 .ok_or_else(|| SourceError::Malformed {
4056 message: "GitHub project item is missing fieldValues".into(),
4057 })?;
4058 complete_connection(field_values, "project item field values", NESTED_PAGE_SIZE)?;
4059 let nodes = field_values
4060 .get("nodes")
4061 .and_then(Value::as_array)
4062 .ok_or_else(|| SourceError::Malformed {
4063 message: "GitHub project item fieldValues.nodes is not an array".into(),
4064 })?;
4065 if let Some(labels) = content.get("labels") {
4066 complete_connection(labels, "content labels", NESTED_PAGE_SIZE)?;
4067 }
4068 let raw_body = optional_str(content, "body")?.map(str::to_owned);
4069 let (body, slot) = metadata_body(raw_body.clone())?;
4070 let parent = optional_str(content.get("parent").unwrap_or(&Value::Null), "id")?
4071 .map(|id| NativeId(id.to_owned()));
4072 // A draft has no sub-issues to summarise, and GitHub's schema gives it no field
4073 // to read one from; it is a task, and never a project.
4074 let sub_issues = match content_kind {
4075 ContentKind::Issue => sub_issue_total(content)?,
4076 ContentKind::DraftIssue => 0,
4077 };
4078 let content_id = required_str(content, "id")?;
4079 let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
4080 message: format!("GitHub issue {content_id}: {message}"),
4081 })?;
4082 let raw_title = required_str(content, "title")?;
4083 // The design prefix is read *first*, before either of the two rules that separate
4084 // a project from a task. A document is not work whatever sub-issues it has and
4085 // whatever marker it carries, and reading the prefix later would make a design
4086 // issue with none of either an empty project.
4087 let kind = if raw_title.starts_with(DESIGN_TITLE_PREFIX) {
4088 BoardKind::Document
4089 } else if parent.is_some() {
4090 // Being a sub-issue wins outright, and no marker overrides it: an issue filed
4091 // under a project is that project's task even when it has sub-issues of its
4092 // own.
4093 BoardKind::Work(ItemKind::Task)
4094 } else if sub_issues > 0 || marked == Some(ItemKind::Project) {
4095 BoardKind::Work(ItemKind::Project)
4096 } else {
4097 BoardKind::Work(ItemKind::Task)
4098 };
4099 // The title a person wrote, which for a document is the one without the prefix —
4100 // the same way `content` above is the body without this source's metadata slot.
4101 let title = match kind {
4102 BoardKind::Document => raw_title[DESIGN_TITLE_PREFIX.len()..].to_owned(),
4103 BoardKind::Work(_) => raw_title.to_owned(),
4104 };
4105 let own_repository = content
4106 .pointer("/repository/nameWithOwner")
4107 .and_then(Value::as_str)
4108 .map(|origin| Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST)))
4109 .transpose()
4110 .map_err(|message| SourceError::Malformed { message })?;
4111 let repositories = if slot.contains_key(Repository::METADATA_KEY) {
4112 Repository::from_metadata(&slot)
4113 .map_err(|message| SourceError::Malformed { message })?
4114 } else {
4115 own_repository.clone().into_iter().collect()
4116 };
4117 let id = NativeId(content_id.to_owned());
4118 // Read only for a task, because only a task has either list: a project or a
4119 // document holding one of these keys holds nothing this source reports, and the
4120 // keys are left out of its caller-visible metadata all the same.
4121 let (delivers, delivered_by) = if kind == BoardKind::Work(ItemKind::Task) {
4122 let listed = |key: &str| {
4123 TaskRef::from_value(key, &id, Some(&self.name), slot.get(key))
4124 .map_err(|message| SourceError::Malformed { message })
4125 };
4126 (
4127 listed(TaskRef::DELIVERS_KEY)?,
4128 listed(TaskRef::DELIVERED_BY_KEY)?,
4129 )
4130 } else {
4131 (Vec::new(), Vec::new())
4132 };
4133 let (option, closed, reason) = Self::status_parts(nodes, content)?;
4134 let priority = self.held_priority(nodes)?;
4135 Ok(Some(Resolved {
4136 item_id: required_str(item, "id")?.to_owned(),
4137 id,
4138 content_kind,
4139 kind,
4140 title,
4141 body: body.filter(|value| !value.is_empty()),
4142 raw_body,
4143 status: self.statuses.status(option, closed, reason),
4144 option: option.map(str::to_owned),
4145 priority,
4146 closed,
4147 delivers,
4148 delivered_by,
4149 labels: labels(content)?,
4150 parent,
4151 origin: text_field(nodes, ORIGIN_FIELD)?.filter(|value| !value.is_empty()),
4152 number: match content_kind {
4153 ContentKind::Issue => Some(issue_number(content)?),
4154 // A draft is filed in no repository, so nothing ever numbered it:
4155 // `DraftIssue` declares no `number` at all, exactly as it declares no
4156 // `subIssuesSummary` the branch above reads.
4157 ContentKind::DraftIssue => None,
4158 },
4159 url: optional_str(content, "url")?.map(str::to_owned),
4160 created_at: optional_time(content, "createdAt")?,
4161 updated_at: optional_time(content, "updatedAt")?,
4162 own_repository,
4163 repositories,
4164 slot,
4165 // Present when the item was reached through its own issue, whose board entry
4166 // names the board; a read of the board's own items has the board already. An
4167 // empty id names nothing a field write could address, so it is read as absent and
4168 // the write goes back to reading the board.
4169 board_id: item
4170 .pointer("/project/id")
4171 .and_then(Value::as_str)
4172 .filter(|id| !id.is_empty())
4173 .map(str::to_owned),
4174 fields: field_definitions(nodes),
4175 }))
4176 }
4177
4178 /// What one board item's `Priority` field says, through this instance's mapping.
4179 ///
4180 /// An instance with no mapping holds no priority, so every item reads as `none` whatever
4181 /// its board holds. With one, no value is `none`, a mapped option is its level, and an
4182 /// option the mapping does not name is kept as itself — never read as a level or as
4183 /// `none` — for a read of the task to report by name.
4184 fn held_priority(&self, field_values: &[Value]) -> Result<HeldPriority, SourceError> {
4185 let Some(mapping) = &self.priorities else {
4186 return Ok(HeldPriority::Read(Priority::None));
4187 };
4188 // A value of the field that names no option — a text field someone called `Priority` —
4189 // is malformed rather than `none`: reading it as no priority would let the next copy
4190 // clear one a person set.
4191 let Some(option) = field_values
4192 .iter()
4193 .find(|value| {
4194 value.pointer("/field/name").and_then(Value::as_str) == Some(PRIORITY_FIELD)
4195 })
4196 .map(|value| required_str(value, "name"))
4197 .transpose()?
4198 else {
4199 return Ok(HeldPriority::Read(Priority::None));
4200 };
4201 Ok(mapping.priority_of(option).map_or_else(
4202 || HeldPriority::Unmapped(option.to_owned()),
4203 HeldPriority::Read,
4204 ))
4205 }
4206
4207 /// What one board item's status is read from: its `Status` option, whether its issue
4208 /// is closed, and the reason it was closed with. [`StatusMapping::status`] turns the
4209 /// three into the status it reports.
4210 fn status_parts<'a>(
4211 field_values: &'a [Value],
4212 content: &'a Value,
4213 ) -> Result<(Option<&'a str>, bool, Option<&'a str>), SourceError> {
4214 let option = field_values
4215 .iter()
4216 .find(|value| value.pointer("/field/name").and_then(Value::as_str) == Some("Status"))
4217 .map(|value| required_str(value, "name"))
4218 .transpose()?;
4219 let closed = optional_str(content, "state")? == Some("CLOSED");
4220 Ok((option, closed, optional_str(content, "stateReason")?))
4221 }
4222
4223 /// The board Status option this write selects, or the refusal that says why not.
4224 ///
4225 /// The mapped option is required for both open and terminal targets. A terminal write
4226 /// validates it before changing either representation, so it can never fall back to
4227 /// closing an issue whose board cannot display the matching status.
4228 ///
4229 /// Answers the field's id, the option's id, and the option's name as the board spells
4230 /// it — which is the name a read of the item reports once it sits there.
4231 fn column_for(
4232 &self,
4233 fields: &Value,
4234 status: &Status,
4235 target: &StatusTarget,
4236 ) -> Result<Option<(String, String, String)>, SourceError> {
4237 let wanted = match target {
4238 StatusTarget::Column(wanted) | StatusTarget::Terminal(wanted, _) => wanted.as_str(),
4239 StatusTarget::Disabled => return Ok(None),
4240 };
4241 let missing = |detail: &str| SourceError::Refused {
4242 message: format!(
4243 "status {} of source {} needs the board Status option {wanted:?}, and {detail}; add that option to the board, or point status_mapping.{} of this source at one it has",
4244 category_name(status.category),
4245 self.name,
4246 category_name(status.category)
4247 ),
4248 };
4249 let Some(field) = Board::field(fields, "Status")? else {
4250 return Err(missing("this board has no Status field"));
4251 };
4252 if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
4253 return Err(missing(
4254 "this board's Status field is not a single-select field",
4255 ));
4256 }
4257 let option = field
4258 .get("options")
4259 .and_then(Value::as_array)
4260 .and_then(|options| {
4261 options.iter().find(|option| {
4262 option
4263 .get("name")
4264 .and_then(Value::as_str)
4265 .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
4266 })
4267 });
4268 match option {
4269 None => Err(missing("this board does not have it")),
4270 Some(option) => Ok(Some((
4271 required_str(field, "id")?.to_owned(),
4272 required_str(option, "id")?.to_owned(),
4273 required_str(option, "name")?.to_owned(),
4274 ))),
4275 }
4276 }
4277
4278 /// The refusal a status that closes an issue is answered with over a board draft.
4279 fn closes_a_draft(&self, category: StatusCategory) -> SourceError {
4280 SourceError::Refused {
4281 message: format!(
4282 "status {} of source {} closes the item's issue, and GitHub draft items have \
4283 no open or closed state",
4284 category_name(category),
4285 self.name
4286 ),
4287 }
4288 }
4289
4290 /// What a status write to one item needs of the board: the board's id and the
4291 /// definition of its `Status` field, read off the item when the item says both.
4292 ///
4293 /// The same reasoning as [`Self::fields_for`]: a node read of the item names its board,
4294 /// and its `Status` value carries that field's definition, options and all. An item that
4295 /// does not say — no board id, or no `Status` value to read the field off — takes them
4296 /// from [`Self::board_fields`], which reads no item.
4297 async fn status_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
4298 if item.defines("Status")
4299 && let Some(board_id) = item.named_board()
4300 {
4301 return Ok(BoardFields {
4302 id: board_id,
4303 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
4304 });
4305 }
4306 self.board_fields().await
4307 }
4308
4309 /// Set one task's status and nothing else; see [`TaskSource::set_task_status`].
4310 async fn set_status(
4311 &self,
4312 id: &NativeId,
4313 category: StatusCategory,
4314 ) -> Result<Option<Status>, SourceError> {
4315 // Refused before anything is read, in the words a write of the same status is.
4316 let target = self.resolved_target(category)?;
4317 let Some(mut item) = self
4318 .item_by_id(id)
4319 .await?
4320 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
4321 else {
4322 return Ok(None);
4323 };
4324 let board = self.status_board(&item).await?;
4325 let wanted = Status {
4326 category,
4327 name: category_name(category).to_owned(),
4328 };
4329 let (field, option, name) = self
4330 .column_for(&board.fields, &wanted, &target)?
4331 .ok_or_else(|| SourceError::Malformed {
4332 message: format!(
4333 "status {} of source {} names no board Status option",
4334 category_name(category),
4335 self.name
4336 ),
4337 })?;
4338 match &target {
4339 StatusTarget::Terminal(_, reason) => {
4340 if item.content_kind == ContentKind::DraftIssue {
4341 return Err(self.closes_a_draft(category));
4342 }
4343 self.set_item_field(
4344 board.id.as_str(),
4345 &item.item_id,
4346 &field,
4347 json!({"singleSelectOptionId": option}),
4348 )
4349 .await?;
4350 self.update_content(
4351 ContentKind::Issue,
4352 &item.id,
4353 json!({"stateInput": state_input(Some(&target))}),
4354 )
4355 .await?;
4356 item.closed = true;
4357 item.status = self
4358 .statuses
4359 .status(Some(&name), true, Some(reason.reason()));
4360 item.option = Some(name);
4361 }
4362 StatusTarget::Column(_) => {
4363 // An option is what an open item's status is, so a closed issue is reopened
4364 // first — sitting closed in the column, it would read back as closed. A draft has
4365 // no state to reopen.
4366 if item.content_kind == ContentKind::Issue && item.closed {
4367 self.update_content(
4368 ContentKind::Issue,
4369 &item.id,
4370 json!({"stateInput": state_input(Some(&target))}),
4371 )
4372 .await?;
4373 item.closed = false;
4374 }
4375 self.set_item_field(
4376 board.id.as_str(),
4377 &item.item_id,
4378 &field,
4379 json!({"singleSelectOptionId": option}),
4380 )
4381 .await?;
4382 item.status = self.statuses.status(Some(&name), false, None);
4383 item.option = Some(name);
4384 }
4385 StatusTarget::Disabled => unreachable!("resolved_target refused a disabled status"),
4386 }
4387 let status = item.status.clone();
4388 self.remember_written(item, false)?;
4389 Ok(Some(status))
4390 }
4391
4392 /// Replace one task's `delivered_by` and nothing else; see
4393 /// [`TaskSource::set_delivered_by`].
4394 ///
4395 /// One update of the body, which differs from the body GitHub holds only inside the
4396 /// metadata slot — see [`with_slot`]. A body that would not change is not sent at all.
4397 async fn replace_delivered_by(
4398 &self,
4399 id: &NativeId,
4400 delivered_by: &[TaskRef],
4401 ) -> Result<Option<()>, SourceError> {
4402 let entries = TaskRef::listed(
4403 TaskRef::DELIVERED_BY_KEY,
4404 id,
4405 Some(&self.name),
4406 delivered_by.to_vec(),
4407 )
4408 .map_err(|message| SourceError::Refused { message })?;
4409 let Some(mut item) = self
4410 .item_by_id(id)
4411 .await?
4412 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
4413 else {
4414 return Ok(None);
4415 };
4416 let mut slot = item.slot.clone();
4417 set_task_list(&mut slot, TaskRef::DELIVERED_BY_KEY, &entries);
4418 self.write_slot(&mut item, &slot).await?;
4419 item.delivered_by = entries;
4420 self.remember_written(item, false)?;
4421 Ok(Some(()))
4422 }
4423
4424 /// Set one caller key of the metadata slot of one issue of `kind`, and nothing else;
4425 /// see [`TaskSource::set_task_metadata`].
4426 ///
4427 /// `None` when this board holds no item by that id, or holds one of another kind. The
4428 /// answer is the item as this source now reads it, so what a caller is told the key
4429 /// holds is what the slot holds.
4430 ///
4431 /// A key already holding the value is answered without a write, compared as JSON rather
4432 /// than as the body's bytes: a slot a person spelled with other whitespace would
4433 /// otherwise be re-encoded, which is a write that changes nothing the caller asked for.
4434 async fn set_slot_key(
4435 &self,
4436 id: &NativeId,
4437 kind: BoardKind,
4438 key: &MetadataKey,
4439 value: &Value,
4440 ) -> Result<Option<Resolved>, SourceError> {
4441 let Some(mut item) = self.item_by_id(id).await?.filter(|item| item.kind == kind) else {
4442 return Ok(None);
4443 };
4444 if item.slot.get(key.as_str()) == Some(value) {
4445 return Ok(Some(item));
4446 }
4447 let mut slot = item.slot.clone();
4448 slot.insert(key.as_str().to_owned(), value.clone());
4449 self.write_slot(&mut item, &slot).await?;
4450 self.remember_written(item.clone(), false)?;
4451 Ok(Some(item))
4452 }
4453
4454 /// Put `slot` in one item's metadata slot with a single update of its body, and bring
4455 /// `item` up to what that write left.
4456 ///
4457 /// The body sent differs from the body GitHub holds only inside the slot — see
4458 /// [`with_slot`] — and a body that would not change is not sent at all. It goes through
4459 /// the mutation the item's content takes, so a board draft's body is written with
4460 /// `updateProjectV2DraftIssue` exactly as an issue's is with `updateIssue`.
4461 async fn write_slot(
4462 &self,
4463 item: &mut Resolved,
4464 slot: &BTreeMap<String, Value>,
4465 ) -> Result<(), SourceError> {
4466 let held = item.raw_body.clone().unwrap_or_default();
4467 let body = with_slot(&held, slot)?;
4468 if body != held {
4469 self.update_content(item.content_kind, &item.id, json!({"body": body}))
4470 .await?;
4471 }
4472 let (visible, slot) = metadata_body(Some(body.clone()))?;
4473 item.body = visible.filter(|value| !value.is_empty());
4474 item.raw_body = Some(body);
4475 item.slot = slot;
4476 Ok(())
4477 }
4478
4479 /// This instance's target for a category, refusing one it has disabled.
4480 ///
4481 /// Nothing here mutates the board's option set to make room for a status. GitHub
4482 /// documents `UpdateProjectV2FieldInput.singleSelectOptions` as *"provided values
4483 /// overwrite existing options"*, so no addition is additive and a mistake destroys the
4484 /// field and every item's status.
4485 fn resolved_target(&self, category: StatusCategory) -> Result<StatusTarget, SourceError> {
4486 let target = self.statuses.target(category).clone();
4487 if target != StatusTarget::Disabled {
4488 return Ok(target);
4489 }
4490 Err(SourceError::Refused {
4491 message: if category == StatusCategory::Draft {
4492 format!(
4493 "status draft is disabled for source {}: draft is incompatible with this \
4494 integration because GitHub draft issues cannot have sub-issues, and this \
4495 source stores a project's tasks as its issue's sub-issues",
4496 self.name
4497 )
4498 } else if category == StatusCategory::Unknown {
4499 format!(
4500 "status {} is disabled for source {}; set status_mapping.{} of this source \
4501 to one board Status option name; every word classified unknown is written \
4502 to that one option",
4503 category_name(category),
4504 self.name,
4505 category_name(category)
4506 )
4507 } else {
4508 format!(
4509 "status {} is disabled for source {}; set status_mapping.{} of this source \
4510 to a board Status option name",
4511 category_name(category),
4512 self.name,
4513 category_name(category)
4514 )
4515 },
4516 })
4517 }
4518
4519 /// What writing `priority` does to one item's `Priority` field on this board, or the
4520 /// refusal naming what the board lacks.
4521 ///
4522 /// `none` is no value, so it clears the field — and asks nothing of an item that holds
4523 /// none already, or of an item not created yet. Every other priority selects the option
4524 /// the mapping names, matched case-insensitively; a board with no `Priority` field, or
4525 /// without that option, is refused rather than given one: reads and writes never create
4526 /// a field or an option.
4527 fn priority_write(
4528 &self,
4529 fields: &Value,
4530 existing: Option<&Resolved>,
4531 priority: Priority,
4532 ) -> Result<Option<PriorityWrite>, SourceError> {
4533 let Some(mapping) = &self.priorities else {
4534 return Err(self.holds_no_priority());
4535 };
4536 let Some(wanted) = mapping.option(priority) else {
4537 if !existing.is_some_and(Resolved::holds_priority) {
4538 return Ok(None);
4539 }
4540 let field =
4541 Board::field(fields, PRIORITY_FIELD)?.ok_or_else(|| SourceError::Malformed {
4542 message: format!(
4543 "an item holding a {PRIORITY_FIELD} value was read without that field"
4544 ),
4545 })?;
4546 return Ok(Some(PriorityWrite::Clear {
4547 field: required_str(field, "id")?.to_owned(),
4548 }));
4549 };
4550 let missing = |detail: &str| SourceError::Refused {
4551 message: format!(
4552 "priority {priority} of source {} needs the board {PRIORITY_FIELD} option \
4553 {wanted:?}, and {detail}; run `onetaskgraph sources fields {} --apply` to add \
4554 it, or point priority_mapping.{priority} of this source at an option the board \
4555 has",
4556 self.name, self.name
4557 ),
4558 };
4559 let Some(field) = Board::field(fields, PRIORITY_FIELD)? else {
4560 return Err(missing(&format!(
4561 "this board has no {PRIORITY_FIELD} field"
4562 )));
4563 };
4564 if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
4565 return Err(missing(&format!(
4566 "this board's {PRIORITY_FIELD} field is not a single-select field"
4567 )));
4568 }
4569 // An options list that is absent or not a list is an answer this source cannot read,
4570 // not a board lacking the option: `sources fields --apply` is no remedy for it.
4571 let option = field
4572 .get("options")
4573 .and_then(Value::as_array)
4574 .ok_or_else(|| SourceError::Malformed {
4575 message: format!("GitHub {PRIORITY_FIELD} field options is not an array"),
4576 })?
4577 .iter()
4578 .find(|option| {
4579 option
4580 .get("name")
4581 .and_then(Value::as_str)
4582 .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
4583 })
4584 .ok_or_else(|| missing("this board does not have it"))?;
4585 Ok(Some(PriorityWrite::Select {
4586 field: required_str(field, "id")?.to_owned(),
4587 option: required_str(option, "id")?.to_owned(),
4588 }))
4589 }
4590
4591 /// Apply one priority write to one board item.
4592 async fn write_priority(
4593 &self,
4594 board_id: &str,
4595 item_id: &str,
4596 write: &PriorityWrite,
4597 ) -> Result<(), SourceError> {
4598 match write {
4599 PriorityWrite::Select { field, option } => {
4600 self.set_item_field(
4601 board_id,
4602 item_id,
4603 field,
4604 json!({"singleSelectOptionId": option}),
4605 )
4606 .await
4607 }
4608 PriorityWrite::Clear { field } => {
4609 let data = self
4610 .graphql(
4611 graphql::CLEAR_FIELD,
4612 json!({"input":{"projectId":board_id,"itemId":item_id,"fieldId":field}}),
4613 )
4614 .await?;
4615 let returned = data
4616 .pointer("/clearProjectV2ItemFieldValue/projectV2Item")
4617 .ok_or_else(|| SourceError::Malformed {
4618 message: "GitHub field clear returned no project item".into(),
4619 })?;
4620 if required_str(returned, "id")? != item_id {
4621 return Err(SourceError::Malformed {
4622 message: "GitHub field clear returned the wrong project item".into(),
4623 });
4624 }
4625 Ok(())
4626 }
4627 }
4628 }
4629
4630 /// The refusal a priority is answered with by an instance configured with no
4631 /// `priority_mapping`, which holds none.
4632 fn holds_no_priority(&self) -> SourceError {
4633 SourceError::Refused {
4634 message: format!(
4635 "source {} holds no task priority: its configuration sets no priority_mapping; \
4636 next: set priority_mapping on this source, then run `onetaskgraph sources \
4637 fields {} --apply` to set its board up",
4638 self.name, self.name
4639 ),
4640 }
4641 }
4642
4643 /// Set one task's priority and nothing else; see [`TaskSource::set_task_priority`].
4644 ///
4645 /// One field write — a select, or a clear for `none` — and no title, body, label, state
4646 /// or `Status` request. Clearing a priority an item does not hold sends nothing.
4647 async fn set_priority(
4648 &self,
4649 id: &NativeId,
4650 priority: Priority,
4651 ) -> Result<Option<Priority>, SourceError> {
4652 if self.priorities.is_none() {
4653 return Err(self.holds_no_priority());
4654 }
4655 let Some(item) = self
4656 .item_by_id(id)
4657 .await?
4658 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
4659 else {
4660 return Ok(None);
4661 };
4662 if priority == Priority::None && !item.holds_priority() {
4663 return Ok(Some(priority));
4664 }
4665 // The item's own read carries the field's definition whenever it holds a value of
4666 // it, which a clear always does; a select onto an item holding none reads the board.
4667 let board = match item.named_board() {
4668 Some(id) if item.defines(PRIORITY_FIELD) => BoardFields {
4669 id,
4670 fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
4671 },
4672 _ => self.board_fields().await?,
4673 };
4674 let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? else {
4675 return Ok(Some(priority));
4676 };
4677 self.write_priority(board.id.as_str(), &item.item_id, &write)
4678 .await?;
4679 // Read back rather than echoed: the answer is what the board now holds, read by the
4680 // item's own id — strongly consistent, unlike a search — and past what this run
4681 // remembers writing, so a write the board did not keep is reported as it stands.
4682 let read = match self.reach(id).await? {
4683 Reached::Held(item) => Some(*item),
4684 Reached::Draft => self.draft_by_id(id).await?,
4685 Reached::Nothing => None,
4686 }
4687 .ok_or_else(|| SourceError::Malformed {
4688 message: format!("task {id} was written and then could not be read back"),
4689 })?;
4690 let answer = read.task()?.priority;
4691 self.remember_written(read, false)?;
4692 Ok(Some(answer))
4693 }
4694
4695 /// Replace one task's visible body and nothing else; see
4696 /// [`TaskSource::set_task_content`].
4697 ///
4698 /// One update of the body, which differs from the body GitHub holds only outside the
4699 /// metadata slot — the slot is kept byte for byte, so every caller key and every list
4700 /// this source keeps there reads back as it was. A body that would not change is not
4701 /// sent at all.
4702 async fn replace_content(
4703 &self,
4704 id: &NativeId,
4705 content: &str,
4706 ) -> Result<Option<()>, SourceError> {
4707 let Some(mut item) = self
4708 .item_by_id(id)
4709 .await?
4710 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
4711 else {
4712 return Ok(None);
4713 };
4714 let held = item.raw_body.clone().unwrap_or_default();
4715 let body = with_content(&held, content)?;
4716 // Checked before anything is sent: content ending in what this source reads as its own
4717 // metadata slot would read back as metadata rather than as the content it was.
4718 let (visible, slot) = metadata_body(Some(body.clone()))?;
4719 if visible.as_deref().unwrap_or_default() != content || slot != item.slot {
4720 return Err(SourceError::Refused {
4721 message: format!(
4722 "this content ends in what source {} reads as its own metadata slot \
4723 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
4724 as content; next: remove that trailing block from the content",
4725 self.name
4726 ),
4727 });
4728 }
4729 if body != held {
4730 self.update_content(item.content_kind, &item.id, json!({"body": body}))
4731 .await?;
4732 }
4733 item.body = visible.filter(|value| !value.is_empty());
4734 item.raw_body = Some(body);
4735 item.slot = slot;
4736 self.remember_written(item, false)?;
4737 Ok(Some(()))
4738 }
4739
4740 /// Replace one issue's visible body and its [`MetadataKey::TEMPLATE_KEY`] slot entry
4741 /// together, and nothing else; see [`TaskSource::set_task_rendering`].
4742 ///
4743 /// One update of the body: the content outside the slot, and inside it that one entry,
4744 /// every other entry kept as it was. This source keeps no template answers — an issue has
4745 /// no room beside itself that is not its body, and answers written there would duplicate
4746 /// what the content already says and count against GitHub's body limit — so `answers`
4747 /// reaches nothing here. A body that would not change is not sent at all.
4748 async fn replace_rendering(
4749 &self,
4750 id: &NativeId,
4751 kind: BoardKind,
4752 content: &str,
4753 provenance: &Value,
4754 ) -> Result<Option<()>, SourceError> {
4755 let Some(mut item) = self.item_by_id(id).await?.filter(|item| item.kind == kind) else {
4756 return Ok(None);
4757 };
4758 let held = item.raw_body.clone().unwrap_or_default();
4759 let mut slot = item.slot.clone();
4760 slot.insert(MetadataKey::TEMPLATE_KEY.to_owned(), provenance.clone());
4761 let body = with_slot(&with_content(&held, content)?, &slot)?;
4762 // Checked before anything is sent, as a content write checks it.
4763 let (visible, read) = metadata_body(Some(body.clone()))?;
4764 if visible.as_deref().unwrap_or_default() != content || read != slot {
4765 return Err(SourceError::Refused {
4766 message: format!(
4767 "this content ends in what source {} reads as its own metadata slot \
4768 ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
4769 as content; next: remove that trailing block from the template",
4770 self.name
4771 ),
4772 });
4773 }
4774 if body != held {
4775 self.update_content(item.content_kind, &item.id, json!({"body": body}))
4776 .await?;
4777 }
4778 item.body = visible.filter(|value| !value.is_empty());
4779 item.raw_body = Some(body);
4780 item.slot = read;
4781 self.remember_written(item, false)?;
4782 Ok(Some(()))
4783 }
4784
4785 async fn set_item_field(
4786 &self,
4787 board_id: &str,
4788 item_id: &str,
4789 field_id: &str,
4790 value: Value,
4791 ) -> Result<(), SourceError> {
4792 let data = self
4793 .graphql(
4794 graphql::UPDATE_FIELD,
4795 json!({"input":{
4796 "projectId":board_id,"itemId":item_id,"fieldId":field_id,"value":value
4797 }}),
4798 )
4799 .await?;
4800 let returned = data
4801 .pointer("/updateProjectV2ItemFieldValue/projectV2Item")
4802 .ok_or_else(|| SourceError::Malformed {
4803 message: "GitHub field update returned no project item".into(),
4804 })?;
4805 if required_str(returned, "id")? != item_id {
4806 return Err(SourceError::Malformed {
4807 message: "GitHub field update returned the wrong project item".into(),
4808 });
4809 }
4810 Ok(())
4811 }
4812
4813 async fn native_dependency_ids(&self, id: &NativeId) -> Result<Vec<String>, SourceError> {
4814 let mut after: Option<String> = None;
4815 let mut ids = Vec::new();
4816 loop {
4817 let data = self
4818 .graphql(
4819 graphql::ISSUE_DEPENDENCIES,
4820 json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after}),
4821 )
4822 .await?;
4823 let connection =
4824 data.pointer("/node/blockedBy")
4825 .ok_or_else(|| SourceError::Malformed {
4826 message: "GitHub dependency response has no blockedBy connection".into(),
4827 })?;
4828 ids.extend(
4829 connection
4830 .get("nodes")
4831 .and_then(Value::as_array)
4832 .ok_or_else(|| SourceError::Malformed {
4833 message: "GitHub dependency response nodes is not an array".into(),
4834 })?
4835 .iter()
4836 .map(|value| required_str(value, "id").map(str::to_owned))
4837 .collect::<Result<Vec<_>, _>>()?,
4838 );
4839 let next = next_cursor(connection)?;
4840 if let Some(next) = &next {
4841 validate_cursor_progress(after.as_deref(), &next.0)?;
4842 }
4843 after = next.map(|cursor| cursor.0);
4844 if after.is_none() {
4845 return Ok(ids);
4846 }
4847 }
4848 }
4849
4850 async fn dependencies(
4851 &self,
4852 id: &NativeId,
4853 near_kind: ItemKind,
4854 direction: Direction,
4855 page: &PageRequest,
4856 ) -> Result<Page<DependencyEdge>, SourceError> {
4857 validate_page(page)?;
4858 let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
4859 let cursor = page.cursor.as_ref().map(|c| c.0.as_str());
4860 let recorded = recorded_offset(cursor, direction)?;
4861 // Asked for even in the recorded phase, whose page reads nothing from the
4862 // connection: `__typename` is what says whether this item has a native
4863 // relationship at all, and that is what decides which far ends the reserved key is
4864 // allowed to hold.
4865 let data = self
4866 .graphql(
4867 graphql::ISSUE_DEPENDENCIES,
4868 json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),
4869 "after":if recorded.is_some() {None} else {cursor}}),
4870 )
4871 .await?;
4872 let node =
4873 data.get("node")
4874 .filter(|v| !v.is_null())
4875 .ok_or_else(|| SourceError::Refused {
4876 message: format!(
4877 "GitHub item {} was not found or does not support dependencies",
4878 id.0
4879 ),
4880 })?;
4881 let connection_name = match direction {
4882 Direction::DependsOn => "blockedBy",
4883 Direction::DependedOnBy => "blocking",
4884 };
4885 // A draft has neither `blockedBy` nor `blocking`, so nothing it depends on can be
4886 // named natively and the reserved key may hold any far end. An issue's connections
4887 // hold issues, and this source reads them at the near item's own level.
4888 let natively_names = (required_str(node, "__typename")? == "Issue").then_some(near_kind);
4889 if let Some(offset) = recorded {
4890 return Ok(recorded_page(
4891 self.recorded_edges(id, near_kind, direction, natively_names, node)
4892 .await?,
4893 offset,
4894 limit,
4895 ));
4896 }
4897 if natively_names.is_none() {
4898 return Ok(recorded_page(
4899 self.recorded_edges(id, near_kind, direction, natively_names, node)
4900 .await?,
4901 0,
4902 limit,
4903 ));
4904 }
4905 let connection = node
4906 .get(connection_name)
4907 .ok_or_else(|| SourceError::Malformed {
4908 message: "GitHub dependency response is missing its connection".into(),
4909 })?;
4910 let nodes = connection
4911 .get("nodes")
4912 .and_then(Value::as_array)
4913 .ok_or_else(|| SourceError::Malformed {
4914 message: "GitHub dependency response nodes is not an array".into(),
4915 })?;
4916 // `from` depends on `to`, always. GitHub spells the same relationship from either
4917 // end — `blockedBy` lists what this item waits on, `blocking` lists what waits on
4918 // it — so the near item is `from` in one direction and `to` in the other.
4919 let items = nodes
4920 .iter()
4921 .map(|value| {
4922 let related = NativeId(required_str(value, "id")?.into());
4923 let related_kind = related_kind(value)?;
4924 let (from, to) = match direction {
4925 Direction::DependsOn => (
4926 DependencyEndpoint::from_native(id.clone(), near_kind),
4927 DependencyEndpoint::from_native(related, related_kind),
4928 ),
4929 Direction::DependedOnBy => (
4930 DependencyEndpoint::from_native(related, related_kind),
4931 DependencyEndpoint::from_native(id.clone(), near_kind),
4932 ),
4933 };
4934 Ok(DependencyEdge {
4935 from,
4936 to,
4937 kind: DependencyKind::Blocks,
4938 })
4939 })
4940 .collect::<Result<Vec<_>, SourceError>>()?;
4941 let mut next = next_cursor(connection)?;
4942 if let Some(next) = &next {
4943 validate_cursor_progress(cursor, &next.0)?;
4944 }
4945 if next.is_none()
4946 && !self
4947 .recorded_edges(id, near_kind, direction, natively_names, node)
4948 .await?
4949 .is_empty()
4950 {
4951 next = Some(Cursor(format!("{RECORDED_CURSOR}0")));
4952 }
4953 Ok(Page { items, next })
4954 }
4955
4956 /// The edges this item records under [`DependencyEdge::RECORDED_KEY`], which is where
4957 /// a far end in another source has to live: no GitHub issue relationship can name one.
4958 ///
4959 /// Only forwards. The reverse of a recorded edge is derived from the far end, and this
4960 /// source never writes one down.
4961 ///
4962 /// The metadata lives in the item's own body slot, and `node` is the dependency read's
4963 /// own answer, which carries an issue's body — so an issue's recorded edges cost no
4964 /// request beyond the read already made, and reading the board for them would be a
4965 /// walk of every item for one field of one. A draft has no body in that answer, because
4966 /// a draft is not an issue, so a draft's are read off its own read by id — never off a
4967 /// listing of the board, which can be behind on the very item asked about.
4968 async fn recorded_edges(
4969 &self,
4970 id: &NativeId,
4971 near_kind: ItemKind,
4972 direction: Direction,
4973 natively_names: Option<ItemKind>,
4974 node: &Value,
4975 ) -> Result<Vec<DependencyEdge>, SourceError> {
4976 if direction != Direction::DependsOn {
4977 return Ok(Vec::new());
4978 }
4979 let slot = match node.get("body") {
4980 Some(body) if natively_names.is_some() => {
4981 metadata_body(body.as_str().map(str::to_owned))?.1
4982 }
4983 _ => {
4984 let Some(item) = self.item_by_id(id).await? else {
4985 return Ok(Vec::new());
4986 };
4987 item.slot
4988 }
4989 };
4990 DependencyEdge::recorded(&slot, id, near_kind, &self.name, natively_names)
4991 .map_err(|message| SourceError::Malformed { message })
4992 }
4993
4994 fn configured_repository(&self) -> Result<&RepositoryTarget, SourceError> {
4995 self.repository
4996 .as_ref()
4997 .ok_or_else(|| SourceError::Refused {
4998 message: format!(
4999 "source {} has no repository configured, and a GitHub Projects board has no \
5000 repository of its own to create an issue in; set repository: owner/name on \
5001 this source",
5002 self.name
5003 ),
5004 })
5005 }
5006
5007 /// The repository one new issue is created in, under the rule [`RepositoryTarget`]
5008 /// states.
5009 ///
5010 /// The fallback is demanded first, whichever arm answers: a write without a configured
5011 /// repository is refused naming the field exactly as it was before the rule existed,
5012 /// so a source that could not write before cannot write now, rather than writing for
5013 /// the one item whose own field happens to decide it.
5014 ///
5015 /// Everything this refuses is refused before `createIssue`, so a refusal leaves no
5016 /// issue behind: an entry that is not a repository on [`RepositoryTarget::HOST`], an
5017 /// entry owned by someone other than the owner of the parent issue's repository —
5018 /// GitHub accepts a sub-issue from another repository of the same owner and from no
5019 /// other, so `addSubIssue` would refuse it after the issue existed — a parent the
5020 /// board does not hold, and a parent that is a draft, which GitHub gives no sub-issues,
5021 /// both of which `addSubIssue` would likewise refuse too late. Whether the entry exists
5022 /// and is visible to the token is checked where its node id is resolved, still before
5023 /// `createIssue`. The parent is read by its own id through [`Self::item_by_id`] — never
5024 /// looked up in a listing of the board, which can be minutes behind an issue its own
5025 /// `projectItems` already places on it — and that read answers first from this process's
5026 /// own record, so a project created moments ago in this command answers though GitHub
5027 /// has not caught up.
5028 async fn creation_target(
5029 &self,
5030 incoming: &Incoming<'_>,
5031 ) -> Result<RepositoryTarget, SourceError> {
5032 let fallback = self.configured_repository()?;
5033 let what = |incoming: &Incoming<'_>| {
5034 format!(
5035 "{} {:?}",
5036 incoming.written.kind().describes(),
5037 incoming.title
5038 )
5039 };
5040 let parent = match incoming.parent {
5041 Some(parent) => Some(self.item_by_id(parent).await?.ok_or_else(|| {
5042 SourceError::Refused {
5043 message: format!(
5044 "GitHub project issue {} was not found on the board of source {}, so {} \
5045 cannot be filed under it",
5046 parent.0,
5047 self.name,
5048 what(incoming)
5049 ),
5050 }
5051 })?),
5052 None => None,
5053 };
5054 let parents_repository = parent
5055 .as_ref()
5056 .map(|parent| {
5057 // A draft is on the board and so is found, but it has no repository to
5058 // place a task in and GitHub gives it no sub-issues, so `addSubIssue`
5059 // would refuse the task only once `createIssue` had made it.
5060 if parent.content_kind == ContentKind::DraftIssue {
5061 return Err(SourceError::Refused {
5062 message: format!(
5063 "GitHub project item {} on the board of source {} is a draft, \
5064 which cannot have sub-issues, so {} cannot be filed under it",
5065 parent.id.0,
5066 self.name,
5067 what(incoming)
5068 ),
5069 });
5070 }
5071 // An issue's repository is where a sub-issue is placed and whose owner it
5072 // is compared against, so a parent whose repository this source cannot
5073 // spell as `owner/name` — GitHub's login grammar is wider than this
5074 // source's floor — is one nothing can be filed under.
5075 parent
5076 .own_repository
5077 .as_ref()
5078 .and_then(|origin| RepositoryTarget::from_origin(origin).ok())
5079 .ok_or_else(|| SourceError::Malformed {
5080 message: format!(
5081 "GitHub project issue {} on the board of source {} is in {}, which \
5082 is not a {}/owner/name repository this source can place {} in",
5083 parent.id.0,
5084 self.name,
5085 parent
5086 .own_repository
5087 .as_ref()
5088 .map_or("no repository", Repository::as_str),
5089 RepositoryTarget::HOST,
5090 what(incoming)
5091 ),
5092 })
5093 })
5094 .transpose()?;
5095 match incoming.repositories {
5096 [named] => {
5097 let target =
5098 RepositoryTarget::from_origin(named).map_err(|_| SourceError::Refused {
5099 message: format!(
5100 "{} names repository {}, which is not a {}/owner/name repository \
5101 source {} can create an issue in; name one that is, or name none",
5102 what(incoming),
5103 named.as_str(),
5104 RepositoryTarget::HOST,
5105 self.name
5106 ),
5107 })?;
5108 if let Some(parents) = &parents_repository
5109 && parents.owner != target.owner
5110 {
5111 return Err(SourceError::Refused {
5112 message: format!(
5113 "{} names repository {}, owned by {}, but its project's issue is in \
5114 {}, owned by {}, and GitHub files a sub-issue only in a repository \
5115 of the same owner as its parent issue; name a repository of {}, or \
5116 name none",
5117 what(incoming),
5118 target.slug(),
5119 target.owner,
5120 parents.slug(),
5121 parents.owner,
5122 parents.owner
5123 ),
5124 });
5125 }
5126 Ok(target)
5127 }
5128 _ => Ok(parents_repository.unwrap_or_else(|| fallback.clone())),
5129 }
5130 }
5131
5132 /// The node id of the repository `incoming` is being created in, or the refusal naming
5133 /// the item and the repository the token cannot see.
5134 ///
5135 /// Resolved once per command per repository; see [`Self::repository_cache`].
5136 async fn repository_id(
5137 &self,
5138 repository: &RepositoryTarget,
5139 incoming: &Incoming<'_>,
5140 ) -> Result<String, SourceError> {
5141 if let Some(id) = self.repository_cache()?.get(repository).cloned() {
5142 return Ok(id);
5143 }
5144 let data = self
5145 .graphql(
5146 graphql::REPOSITORY,
5147 json!({"owner":repository.owner,"name":repository.name}),
5148 )
5149 .await?;
5150 let node = data
5151 .get("repository")
5152 .filter(|value| !value.is_null())
5153 .ok_or_else(|| SourceError::Refused {
5154 message: format!(
5155 "GitHub repository {} was not found or is not visible to the token, so {} \
5156 {:?} cannot be created in it",
5157 repository.slug(),
5158 incoming.written.kind().describes(),
5159 incoming.title
5160 ),
5161 })?;
5162 let id = required_str(node, "id")?.to_owned();
5163 self.repository_cache()?
5164 .insert(repository.clone(), id.clone());
5165 Ok(id)
5166 }
5167
5168 fn repository_cache(
5169 &self,
5170 ) -> Result<std::sync::MutexGuard<'_, BTreeMap<RepositoryTarget, String>>, SourceError> {
5171 self.repository_cache
5172 .lock()
5173 .map_err(|_| SourceError::Unavailable {
5174 message: "this source's record of the destination repository was left \
5175 inconsistent by an earlier failure; next: run the command again"
5176 .into(),
5177 })
5178 }
5179
5180 /// Create or update one board item, whichever kind it is.
5181 async fn write_item(
5182 &self,
5183 incoming: &Incoming<'_>,
5184 target: Option<&NativeId>,
5185 depends_on: &[DependencyEdge],
5186 ) -> Result<NativeId, SourceError> {
5187 // Refused before anything is read or written: a task or a project titled the way
5188 // this board spells a document would land as an issue this same source reads back
5189 // as a document, so the field this destination cannot carry is named rather than
5190 // written and silently reclassified.
5191 if let Written::Work(kind, _) = incoming.written
5192 && incoming.title.starts_with(DESIGN_TITLE_PREFIX)
5193 {
5194 return Err(SourceError::Refused {
5195 message: format!(
5196 "the title of this {} begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
5197 spells a document, so it would read back as one rather than as a {}; \
5198 retitle it, or copy it as a document",
5199 kind.marker(),
5200 self.name,
5201 kind.marker()
5202 ),
5203 });
5204 }
5205 // The destination is read by its own id, and whether this board holds it is decided
5206 // by that read — its own `projectItems` — rather than by whether a listing of the
5207 // board happens to include it yet. See the module documentation.
5208 let existing = match target {
5209 Some(target) => {
5210 Some(
5211 self.item_by_id(target)
5212 .await?
5213 .ok_or_else(|| SourceError::Refused {
5214 message: format!("GitHub destination item {} was not found", target.0),
5215 })?,
5216 )
5217 }
5218 None => None,
5219 };
5220 let existing = existing.as_ref();
5221 let board = self
5222 .fields_for(
5223 existing,
5224 incoming.written.status().is_some(),
5225 incoming
5226 .priority
5227 .is_some_and(|priority| priority != Priority::None),
5228 )
5229 .await?;
5230 let status_target = incoming
5231 .written
5232 .status()
5233 .map(|status| self.resolved_target(status.category))
5234 .transpose()?;
5235 let column = match (incoming.written.status(), status_target.as_ref()) {
5236 (Some(status), Some(target)) => self.column_for(&board.fields, status, target)?,
5237 _ => None,
5238 };
5239 // Resolved before anything is created, for the reason the column above is: a
5240 // priority this board has no option for is refused while nothing has been written.
5241 let priority_write = match incoming.priority {
5242 Some(priority) => self.priority_write(&board.fields, existing, priority)?,
5243 None => None,
5244 };
5245 let content_kind = existing.map_or(ContentKind::Issue, |item| item.content_kind);
5246 if content_kind == ContentKind::DraftIssue {
5247 if let (Some(StatusTarget::Terminal(_, _)), Some(status)) =
5248 (status_target.as_ref(), incoming.written.status())
5249 {
5250 return Err(self.closes_a_draft(status.category));
5251 }
5252 if incoming.parent.is_some() {
5253 return Err(SourceError::Refused {
5254 message: "GitHub draft items cannot be a project's sub-issue".into(),
5255 });
5256 }
5257 }
5258 match existing {
5259 Some(item) if content_kind == ContentKind::Issue => {
5260 if item.labels != incoming.labels {
5261 return Err(SourceError::Refused {
5262 message: "GitHub issue labels differ from the labels being written".into(),
5263 });
5264 }
5265 }
5266 _ => {
5267 if !incoming.labels.is_empty() {
5268 return Err(SourceError::Refused {
5269 message: "GitHub items created by this destination carry no labels".into(),
5270 });
5271 }
5272 }
5273 }
5274
5275 // An existing issue is never moved; a new one is created where the rule says. The
5276 // repository the issue really lives in is what the slot below is written against,
5277 // so a single entry that is where the issue is created travels as no key at all,
5278 // and the read side derives it back from the issue.
5279 let (own_repository, creation_target) = match existing {
5280 Some(item) => (item.own_repository.clone(), None),
5281 None => {
5282 let target = self.creation_target(incoming).await?;
5283 let origin = Repository::try_from(target.origin())
5284 .map_err(|message| SourceError::Config { message })?;
5285 (Some(origin), Some(target))
5286 }
5287 };
5288 let (native, fallback) = self
5289 .partition_edges(incoming.written.kind(), content_kind, depends_on)
5290 .await?;
5291 let slot = slot_metadata(incoming, own_repository.as_ref(), &fallback);
5292 let body = compose_body(incoming.content, &slot)?;
5293 // Read before anything is created, for the reason the field below is: a value
5294 // this destination cannot store has to refuse, and refusing after `createIssue`
5295 // would leave an issue behind that nothing asked for. The engine writes a
5296 // qualified id here; a caller handing this key anything else is told so rather
5297 // than having it silently stored as no origin at all.
5298 // 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.
5299 let origin = match incoming.metadata.get(ORIGIN_KEY) {
5300 None => "",
5301 Some(Value::String(origin)) => origin.as_str(),
5302 Some(other) => {
5303 return Err(SourceError::Refused {
5304 message: format!(
5305 "{ORIGIN_KEY} holds a qualified id spelled as a string, and this item's \
5306 is {other}"
5307 ),
5308 });
5309 }
5310 };
5311 // Resolved before anything is created: a board that cannot carry the copy origin
5312 // has to refuse the write, and refusing it after `createIssue` would leave an
5313 // issue behind that nothing asked for.
5314 let origin_field = match Board::field(&board.fields, ORIGIN_FIELD)? {
5315 Some(field) => {
5316 if required_str(field, "__typename")? != "ProjectV2Field" {
5317 return Err(SourceError::Refused {
5318 message: format!(
5319 "GitHub board source-owned {ORIGIN_FIELD} field is not a text field"
5320 ),
5321 });
5322 }
5323 Some(required_str(field, "id")?.to_owned())
5324 }
5325 None if incoming.metadata.contains_key(ORIGIN_KEY) => {
5326 return Err(SourceError::Refused {
5327 message: format!(
5328 "GitHub board has no source-owned {ORIGIN_FIELD} text field, and the \
5329 item carries {ORIGIN_KEY}; add a text field named {ORIGIN_FIELD} to \
5330 the board"
5331 ),
5332 });
5333 }
5334 None => None,
5335 };
5336
5337 let Landed {
5338 content_id,
5339 item_id,
5340 url,
5341 number,
5342 } = match existing {
5343 Some(item) => {
5344 self.update_existing(item, incoming, &body, status_target.as_ref())
5345 .await?;
5346 Landed {
5347 content_id: item.id.clone(),
5348 item_id: item.item_id.clone(),
5349 url: item.url.clone(),
5350 number: item.number,
5351 }
5352 }
5353 None => {
5354 let target = creation_target
5355 .as_ref()
5356 .ok_or_else(|| SourceError::Malformed {
5357 message: "a new item was decided without a repository to create it in"
5358 .into(),
5359 })?;
5360 self.create_and_file_issue(board.id.as_str(), target, incoming, &body)
5361 .await?
5362 }
5363 };
5364
5365 let written_option = column.as_ref().map(|(_, _, name)| name.clone());
5366 let column = column.map(|(field, option, _)| (field, option));
5367 // Creating an item here is several calls — `createIssue`, `addProjectV2ItemById`,
5368 // then each board field, the parent and the dependencies — and GitHub can fail at
5369 // any of them. Everything this source can refuse *before* the first of those is
5370 // already checked above, so what is left is GitHub itself failing part way. When it
5371 // does over an item this call created, the issue is taken back: a write that
5372 // refused must not leave an item behind that nobody asked for, and one that does
5373 // makes the retry create a second.
5374 let landed = self
5375 .finish_write(
5376 board.id.as_str(),
5377 incoming,
5378 &content_id,
5379 &item_id,
5380 content_kind,
5381 existing,
5382 origin_field.as_deref(),
5383 origin,
5384 column,
5385 status_target.as_ref(),
5386 priority_write.as_ref(),
5387 &native,
5388 )
5389 .await;
5390 if let Err(error) = landed {
5391 if existing.is_none() {
5392 // Best effort, and the write's own failure is what the caller is told: a
5393 // refusal naming the tidy-up would hide why the write failed at all.
5394 let _ = self.delete_issue(&content_id).await;
5395 }
5396 return Err(error);
5397 }
5398
5399 let written_status = match (incoming.written.status(), status_target.as_ref()) {
5400 (Some(_), Some(StatusTarget::Terminal(_, reason))) => {
5401 self.statuses
5402 .status(written_option.as_deref(), true, Some(reason.reason()))
5403 }
5404 (Some(_), Some(StatusTarget::Column(_))) => {
5405 self.statuses.status(written_option.as_deref(), false, None)
5406 }
5407 (Some(status), _) => status.clone(),
5408 (None, _) => Status {
5409 category: StatusCategory::Unknown,
5410 name: "Open".to_owned(),
5411 },
5412 };
5413
5414 // So the rest of this command reads what it just did rather than what the board
5415 // said before it. See `remember_written` for which half takes it.
5416 let remembered = Resolved {
5417 item_id,
5418 id: content_id.clone(),
5419 content_kind,
5420 kind: incoming.written.kind(),
5421 title: incoming.title.to_owned(),
5422 // The visible half of the body this write composed, split back off it the
5423 // way a read splits it — so what this record reports is what a read of the
5424 // same issue reports, rather than the person's text with the metadata slot
5425 // still on the end of it.
5426 body: metadata_body(body.clone())?.0,
5427 raw_body: body.clone(),
5428 // A document has no status of its own; what it reads back as is whatever
5429 // the issue's own state says, which is what a re-read reports.
5430 status: written_status,
5431 option: written_option.or_else(|| existing.and_then(|item| item.option.clone())),
5432 priority: match incoming.priority {
5433 Some(priority) => HeldPriority::Read(priority),
5434 None => existing.map_or(HeldPriority::Read(Priority::None), |item| {
5435 item.priority.clone()
5436 }),
5437 },
5438 // What `state_input` asked for: closed for a terminal target, open for any other
5439 // status, and the issue's own state left as it was by a document write.
5440 closed: content_kind == ContentKind::Issue
5441 && match status_target.as_ref() {
5442 Some(StatusTarget::Terminal(_, _)) => true,
5443 Some(_) => false,
5444 None => existing.is_some_and(|item| item.closed),
5445 },
5446 delivers: incoming.delivers.to_vec(),
5447 delivered_by: incoming.delivered_by.to_vec(),
5448 labels: incoming.labels.to_vec(),
5449 parent: incoming.parent.cloned(),
5450 origin: (!origin.is_empty()).then(|| origin.to_owned()),
5451 number,
5452 // In the update path this is the item's own url, read off `existing` where the
5453 // record above was bound, so one expression serves both halves.
5454 url,
5455 created_at: existing.and_then(|item| item.created_at),
5456 updated_at: existing.and_then(|item| item.updated_at),
5457 own_repository,
5458 repositories: incoming.repositories.to_vec(),
5459 slot,
5460 board_id: Some(board.id.as_str().to_owned()),
5461 fields: board
5462 .fields
5463 .get("nodes")
5464 .and_then(Value::as_array)
5465 .cloned()
5466 .unwrap_or_default(),
5467 };
5468 self.remember_written(remembered, existing.is_none())?;
5469 Ok(content_id)
5470 }
5471
5472 /// Everything a write does after the item exists: its board fields, its parent, and
5473 /// its dependencies.
5474 ///
5475 /// Split out of `write_item` so there is one place a failure past the point of no
5476 /// return is caught, rather than a tidy-up repeated at each `?` above.
5477 // llmlint: ignore[suppressions_justified] This is the tail of `write_item` lifted out
5478 // so there is one place a failure past the point of no return is caught, and its
5479 // arguments are exactly the values that tail already had in scope. Bundling them into a
5480 // struct would describe no concept — it would be "the arguments of this function" — and
5481 // would put the whole of `write_item`'s locals behind one more indirection.
5482 #[allow(clippy::too_many_arguments)]
5483 async fn finish_write(
5484 &self,
5485 board_id: &str,
5486 incoming: &Incoming<'_>,
5487 content_id: &NativeId,
5488 item_id: &str,
5489 content_kind: ContentKind,
5490 existing: Option<&Resolved>,
5491 origin_field: Option<&str>,
5492 origin: &str,
5493 column: Option<(String, String)>,
5494 status_target: Option<&StatusTarget>,
5495 priority: Option<&PriorityWrite>,
5496 native: &[String],
5497 ) -> Result<(), SourceError> {
5498 if let Some(field_id) = origin_field {
5499 self.set_item_field(board_id, item_id, field_id, json!({"text":origin}))
5500 .await?;
5501 }
5502
5503 if let Some((field_id, option_id)) = column {
5504 self.set_item_field(
5505 board_id,
5506 item_id,
5507 &field_id,
5508 json!({"singleSelectOptionId":option_id}),
5509 )
5510 .await?;
5511 }
5512
5513 if let Some(priority) = priority {
5514 self.write_priority(board_id, item_id, priority).await?;
5515 }
5516
5517 if content_kind == ContentKind::Issue
5518 && matches!(status_target, Some(StatusTarget::Terminal(_, _)))
5519 {
5520 self.update_content(
5521 ContentKind::Issue,
5522 content_id,
5523 json!({"stateInput":state_input(status_target)}),
5524 )
5525 .await?;
5526 }
5527
5528 if content_kind == ContentKind::Issue {
5529 self.reparent(
5530 existing.and_then(|item| item.parent.clone()),
5531 content_id,
5532 incoming.parent,
5533 )
5534 .await?;
5535 // A document takes part in no dependency graph, so writing one neither reads
5536 // nor changes the issue's own `blockedBy` relationships. Reconciling them
5537 // against the empty list a document write carries would *delete* whatever
5538 // relationships a person had made on that issue, which is a write nobody
5539 // asked for.
5540 if incoming.written.kind() != BoardKind::Document {
5541 self.reconcile_blocked_by(content_id, native).await?;
5542 }
5543 }
5544 Ok(())
5545 }
5546
5547 /// Delete one issue, which takes its board item with it.
5548 async fn delete_issue(&self, id: &NativeId) -> Result<(), SourceError> {
5549 let data = self
5550 .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
5551 .await?;
5552 data.pointer("/deleteIssue/repository")
5553 .filter(|value| !value.is_null())
5554 .ok_or_else(|| SourceError::Malformed {
5555 message: "GitHub issue deletion returned no repository".into(),
5556 })?;
5557 self.forget(id)?;
5558 Ok(())
5559 }
5560
5561 /// Remove one item this copy created, so a copy that could not finish leaves the board
5562 /// as it found it.
5563 ///
5564 /// Deleting the issue takes its board item with it, so there is no second mutation to
5565 /// keep in step. An id the board does not hold is not an error: the item is already
5566 /// gone, which is the state this asks for. Which that is, is decided by reading the item
5567 /// by its own id — a listing of the board can still be missing an item it holds, and
5568 /// reading that as *already gone* would leave behind the very item this was asked to
5569 /// take back.
5570 async fn delete_item(&self, id: &NativeId) -> Result<(), SourceError> {
5571 let Some(item) = self.item_by_id(id).await? else {
5572 return Ok(());
5573 };
5574 if item.content_kind == ContentKind::DraftIssue {
5575 return Err(SourceError::Refused {
5576 message: format!(
5577 "GitHub item {} is a draft, and this source removes an item by deleting \
5578 its issue; next: remove it from the board by hand",
5579 id.0
5580 ),
5581 });
5582 }
5583 let data = self
5584 .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
5585 .await?;
5586 data.pointer("/deleteIssue/repository")
5587 .filter(|value| !value.is_null())
5588 .ok_or_else(|| SourceError::Malformed {
5589 message: "GitHub issue deletion returned no repository".into(),
5590 })?;
5591 self.forget(id)?;
5592 Ok(())
5593 }
5594
5595 /// The issue a comment call on `task` is about, or `None` when this board holds no such
5596 /// task.
5597 ///
5598 /// Resolved exactly as [`TaskSource::get_task`] resolves it, so the comment verbs and a
5599 /// read of the task cannot disagree about which ids name one: a project or a document of
5600 /// this board is not a task here either.
5601 ///
5602 /// A **draft** is a task with nowhere to keep a comment, because GitHub keeps comments on
5603 /// issues and a draft is not one. It is refused rather than answered with an empty page,
5604 /// which would read as a task nobody has commented on yet.
5605 async fn commented_issue(&self, task: &NativeId) -> Result<Option<NativeId>, SourceError> {
5606 let Some(item) = self
5607 .item_by_id(task)
5608 .await?
5609 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
5610 else {
5611 return Ok(None);
5612 };
5613 if item.content_kind == ContentKind::DraftIssue {
5614 return Err(SourceError::Refused {
5615 message: format!(
5616 "task {} of source {} is a draft item on the board, and GitHub keeps \
5617 comments on issues alone, so a draft has none to read or write; next: \
5618 convert the draft to an issue on the board, then comment on the issue it \
5619 becomes",
5620 task.0, self.name
5621 ),
5622 });
5623 }
5624 Ok(Some(item.id))
5625 }
5626
5627 /// Whether the comment `comment` is one of `issue`'s own.
5628 ///
5629 /// Read before an edit or a removal is sent, because GitHub's comment mutations take the
5630 /// comment's id and nothing else: a comment id given against the wrong task would
5631 /// otherwise change a comment on some other issue entirely. An id that names nothing, or
5632 /// names something that is not an issue comment, is a comment this task does not have —
5633 /// which is what GitHub refusing to resolve it means too.
5634 async fn comment_is_on(
5635 &self,
5636 issue: &NativeId,
5637 comment: &NativeId,
5638 ) -> Result<bool, SourceError> {
5639 let asked = self
5640 .graphql(graphql::COMMENT_ISSUE, json!({"id":comment.0}))
5641 .await;
5642 let data = match asked {
5643 Ok(data) => data,
5644 Err(error) if unresolvable_node(&error) => return Ok(false),
5645 Err(error) => return Err(error),
5646 };
5647 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
5648 return Ok(false);
5649 };
5650 if optional_str(node, "__typename")? != Some("IssueComment") {
5651 return Ok(false);
5652 }
5653 let on = node.get("issue").ok_or_else(|| SourceError::Malformed {
5654 message: format!("GitHub issue comment {} names no issue", comment.0),
5655 })?;
5656 Ok(required_str(on, "id")? == issue.0)
5657 }
5658
5659 /// Which far ends this item's own `blockedBy` relationship holds, and which it cannot.
5660 async fn partition_edges(
5661 &self,
5662 near_kind: BoardKind,
5663 near_content: ContentKind,
5664 depends_on: &[DependencyEdge],
5665 ) -> Result<(Vec<String>, Vec<DependencyEdge>), SourceError> {
5666 let mut native = Vec::new();
5667 let mut fallback = Vec::new();
5668 for edge in depends_on {
5669 let same_source = edge
5670 .to
5671 .source()
5672 .is_none_or(|source| source == self.name.as_str());
5673 // A qualified id's source segment runs to its *first* colon — `GlobalId` and
5674 // `DependencyEndpoint::source` both read it that way — and a native id may hold
5675 // colons of its own, so the far end is everything after that one separator.
5676 // Splitting at the last would truncate `work:urn:task:7` to `7`.
5677 let far_id = if edge.to.is_qualified() {
5678 edge.to
5679 .id()
5680 .split_once(':')
5681 .map_or(edge.to.id(), |(_, native)| native)
5682 } else {
5683 edge.to.id()
5684 };
5685 // A same-source far end is read by its own id, exactly as the item it is a far end
5686 // of is: whether this board holds it is that read's answer, never a listing's.
5687 let far = if same_source {
5688 Some(
5689 self.item_by_id(&NativeId(far_id.to_owned()))
5690 .await?
5691 .ok_or_else(|| SourceError::Refused {
5692 message: format!("GitHub dependency item {far_id} was not found"),
5693 })?,
5694 )
5695 } else {
5696 None
5697 };
5698 let far = far.as_ref();
5699 // The caller says which kind the far end is, and this board holds the far end
5700 // itself, so a disagreement is settled here rather than stored: recorded, the
5701 // wrong kind would read back as a cross-level edge that never existed; written
5702 // natively, it would name a relationship of a different level than the caller
5703 // asked for.
5704 //
5705 // A far end this board holds as a *document* fails the same comparison and is
5706 // refused by the same sentence: `ItemKind` has no document variant because
5707 // nothing may point at one, so no caller can name it correctly and the refusal
5708 // is the only honest answer.
5709 if let Some(disagreeing) = far.filter(|far| far.kind != BoardKind::Work(edge.to.kind)) {
5710 return Err(SourceError::Refused {
5711 message: format!(
5712 "GitHub dependency item {far_id} is a {} of this board, and this item \
5713 names it as a {}; record the kind it is",
5714 disagreeing.kind.describes(),
5715 edge.to.kind.marker()
5716 ),
5717 });
5718 }
5719 // A draft has neither `blockedBy` nor `blocking`, so no edge of one is native
5720 // however the far end is spelled — and one classified native here would be
5721 // written nowhere at all, because a draft's native reconciliation never runs.
5722 let native_here = near_content == ContentKind::Issue
5723 && far.is_some_and(|far| {
5724 far.content_kind == ContentKind::Issue
5725 && BoardKind::Work(edge.to.kind) == near_kind
5726 });
5727 if native_here {
5728 native.push(far_id.to_owned());
5729 } else {
5730 fallback.push(edge.clone());
5731 }
5732 }
5733 Ok((native, fallback))
5734 }
5735
5736 async fn update_existing(
5737 &self,
5738 item: &Resolved,
5739 incoming: &Incoming<'_>,
5740 body: &Option<String>,
5741 status_target: Option<&StatusTarget>,
5742 ) -> Result<(), SourceError> {
5743 let title = incoming.written_title();
5744 let mut fields = match item.content_kind {
5745 ContentKind::DraftIssue => json!({"title":title,"body":body}),
5746 ContentKind::Issue => json!({"title":title,"body":body,
5747 "stateInput":state_input(status_target)}),
5748 };
5749 if matches!(status_target, Some(StatusTarget::Terminal(_, _))) {
5750 fields
5751 .as_object_mut()
5752 .expect("update fields are an object")
5753 .remove("stateInput");
5754 }
5755 self.update_content(item.content_kind, &item.id, fields)
5756 .await
5757 }
5758
5759 /// Update one board item's content with exactly `fields` beside its id, through the
5760 /// mutation its kind takes: `updateIssue` for an issue, `updateProjectV2DraftIssue` for
5761 /// a draft.
5762 ///
5763 /// Every input field either mutation leaves out is a field GitHub leaves as it is, which
5764 /// is what lets a narrow write carry the one thing it changes and nothing else.
5765 async fn update_content(
5766 &self,
5767 kind: ContentKind,
5768 id: &NativeId,
5769 fields: Value,
5770 ) -> Result<(), SourceError> {
5771 let (operation, id_key, pointer) = match kind {
5772 ContentKind::DraftIssue => (
5773 graphql::UPDATE_DRAFT,
5774 "draftIssueId",
5775 "/updateProjectV2DraftIssue/draftIssue",
5776 ),
5777 ContentKind::Issue => (graphql::UPDATE_ISSUE, "id", "/updateIssue/issue"),
5778 };
5779 let mut input = fields;
5780 input[id_key] = json!(id.0);
5781 let data = self.graphql(operation, json!({"input":input})).await?;
5782 let returned = data
5783 .pointer(pointer)
5784 .ok_or_else(|| SourceError::Malformed {
5785 message: "GitHub item update returned no item".into(),
5786 })?;
5787 if required_str(returned, "id")? != id.0 {
5788 return Err(SourceError::Malformed {
5789 message: "GitHub item update returned the wrong item".into(),
5790 });
5791 }
5792 Ok(())
5793 }
5794
5795 /// Creates one issue, files it on the board, and reports what a read of it would say:
5796 /// its content id, its board item id, and the web address GitHub gave it.
5797 ///
5798 /// Two calls rather than one: `createIssue` needs a repository and answers with an
5799 /// issue that is on no board, and `addProjectV2ItemById` is what puts it there. A
5800 /// terminal status is not written here: `finish_write` selects its option first and
5801 /// closes the issue after, so a close never lands on an item whose board cannot show it.
5802 ///
5803 /// The address and the number come back here because this is the only place either is
5804 /// known before GitHub's own board read catches up — an item this run created answers
5805 /// the reads that follow it out of the record below, and one remembered without them
5806 /// would report no location and no key for the rest of the run.
5807 async fn create_and_file_issue(
5808 &self,
5809 board_id: &str,
5810 repository: &RepositoryTarget,
5811 incoming: &Incoming<'_>,
5812 body: &Option<String>,
5813 ) -> Result<Landed, SourceError> {
5814 let repository_id = self.repository_id(repository, incoming).await?;
5815 let data = self
5816 .graphql(
5817 graphql::CREATE_ISSUE,
5818 json!({"input":{
5819 "repositoryId":repository_id,"title":incoming.written_title(),"body":body
5820 }}),
5821 )
5822 .await?;
5823 let created = data
5824 .pointer("/createIssue/issue")
5825 .filter(|value| !value.is_null())
5826 .ok_or_else(|| SourceError::Malformed {
5827 message: "GitHub issue creation returned no issue".into(),
5828 })?;
5829 let content_id = NativeId(required_str(created, "id")?.to_owned());
5830 // Optional although GitHub's schema makes it non-null: the issue exists by now, so
5831 // a response without it is not worth failing a landed write over — the item simply
5832 // reports no location until the board read catches up, which is what it did before.
5833 let url = optional_str(created, "url")?.map(str::to_owned);
5834 // The issue exists from here on, so an unreadable number and a refused board
5835 // filing below each try, best effort, to take it back: an issue in the repository
5836 // that is on no board is an item nobody asked for and nothing here would find again.
5837 //
5838 // Its number is optional on the same terms its address is — a landed write is not
5839 // worth failing over a member that came back missing, and such an item reports no
5840 // handle until a board read catches up. A number that is *present* and is not an
5841 // unsigned integer is still a response this source cannot read.
5842 let number = match created_issue_number(created) {
5843 Ok(number) => number,
5844 Err(error) => {
5845 let _ = self.delete_issue(&content_id).await;
5846 return Err(error);
5847 }
5848 };
5849 let added = match self
5850 .graphql(
5851 graphql::ADD_TO_BOARD,
5852 json!({"input":{"projectId":board_id,"contentId":content_id.0}}),
5853 )
5854 .await
5855 {
5856 Ok(added) => added,
5857 Err(error) => {
5858 let _ = self.delete_issue(&content_id).await;
5859 return Err(error);
5860 }
5861 };
5862 let item = added
5863 .pointer("/addProjectV2ItemById/item")
5864 .filter(|value| !value.is_null())
5865 .ok_or_else(|| SourceError::Malformed {
5866 message: "GitHub board addition returned no project item".into(),
5867 })?;
5868 Ok(Landed {
5869 content_id,
5870 item_id: required_str(item, "id")?.to_owned(),
5871 url,
5872 number,
5873 })
5874 }
5875
5876 /// Move one issue under the project it now belongs to, or out of the one it left.
5877 async fn reparent(
5878 &self,
5879 held: Option<NativeId>,
5880 child: &NativeId,
5881 wanted: Option<&NativeId>,
5882 ) -> Result<(), SourceError> {
5883 if held.as_ref() == wanted {
5884 return Ok(());
5885 }
5886 if let Some(held) = &held {
5887 self.sub_issue(graphql::REMOVE_SUB_ISSUE, held, child, "removeSubIssue")
5888 .await?;
5889 }
5890 if let Some(wanted) = wanted {
5891 self.sub_issue(graphql::ADD_SUB_ISSUE, wanted, child, "addSubIssue")
5892 .await?;
5893 }
5894 Ok(())
5895 }
5896
5897 async fn sub_issue(
5898 &self,
5899 operation: &str,
5900 parent: &NativeId,
5901 child: &NativeId,
5902 root: &str,
5903 ) -> Result<(), SourceError> {
5904 let data = self
5905 .graphql(
5906 operation,
5907 json!({"input":{"issueId":parent.0,"subIssueId":child.0}}),
5908 )
5909 .await?;
5910 let issue =
5911 data.pointer(&format!("/{root}/issue"))
5912 .ok_or_else(|| SourceError::Malformed {
5913 message: "GitHub sub-issue update returned no issue".into(),
5914 })?;
5915 let sub =
5916 data.pointer(&format!("/{root}/subIssue"))
5917 .ok_or_else(|| SourceError::Malformed {
5918 message: "GitHub sub-issue update returned no sub-issue".into(),
5919 })?;
5920 if required_str(issue, "id")? != parent.0 || required_str(sub, "id")? != child.0 {
5921 return Err(SourceError::Malformed {
5922 message: "GitHub sub-issue update returned the wrong issues".into(),
5923 });
5924 }
5925 Ok(())
5926 }
5927
5928 async fn reconcile_blocked_by(
5929 &self,
5930 content_id: &NativeId,
5931 native: &[String],
5932 ) -> Result<(), SourceError> {
5933 let current = self.native_dependency_ids(content_id).await?;
5934 for (operation, far_id) in current
5935 .iter()
5936 .filter(|id| !native.contains(id))
5937 .map(|id| (graphql::REMOVE_BLOCKED_BY, id))
5938 .chain(
5939 native
5940 .iter()
5941 .filter(|id| !current.contains(id))
5942 .map(|id| (graphql::ADD_BLOCKED_BY, id)),
5943 )
5944 {
5945 let data = self
5946 .graphql(
5947 operation,
5948 json!({"input":{"issueId":content_id.0,"blockingIssueId":far_id}}),
5949 )
5950 .await?;
5951 let root = if operation == graphql::ADD_BLOCKED_BY {
5952 "addBlockedBy"
5953 } else {
5954 "removeBlockedBy"
5955 };
5956 let issue =
5957 data.pointer(&format!("/{root}/issue"))
5958 .ok_or_else(|| SourceError::Malformed {
5959 message: "GitHub dependency update returned no issue".into(),
5960 })?;
5961 let blocker = data
5962 .pointer(&format!("/{root}/blockingIssue"))
5963 .ok_or_else(|| SourceError::Malformed {
5964 message: "GitHub dependency update returned no blocking issue".into(),
5965 })?;
5966 if required_str(issue, "id")? != content_id.0 || required_str(blocker, "id")? != far_id
5967 {
5968 return Err(SourceError::Malformed {
5969 message: "GitHub dependency update returned the wrong issues".into(),
5970 });
5971 }
5972 }
5973 Ok(())
5974 }
5975}
5976
5977/// What resolving one node id reached; see [`GitHubProjectsSource::reach`].
5978enum Reached {
5979 /// An issue this board holds, resolved into everything this source reports about it.
5980 Held(Box<Resolved>),
5981 /// Nothing this board holds: no such node, or a node on some other board.
5982 Nothing,
5983 /// A board draft, which [`graphql::ISSUE`] reaches and reads nothing of, so it is read
5984 /// again by [`GitHubProjectsSource::draft_by_id`].
5985 Draft,
5986}
5987
5988/// What GitHub says when a string is not a node id it can resolve.
5989///
5990/// Matched because it is the ordinary answer to a project selector naming a project by its
5991/// *name*, and reporting that as a failure would make naming one impossible. It is read
5992/// off the refusal GitHub sent, never guessed from the shape of the string: this source
5993/// does not define the syntax of a GitHub node id and would be wrong about it.
5994const UNRESOLVABLE_NODE: &str = "could not resolve to a node";
5995
5996/// Whether this refusal is GitHub saying the id names no node at all.
5997fn unresolvable_node(error: &SourceError) -> bool {
5998 matches!(error, SourceError::Refused { message }
5999 if message.to_ascii_lowercase().contains(UNRESOLVABLE_NODE))
6000}
6001
6002/// One project name, as a search qualifier which filters on it at the server.
6003///
6004/// Quoted so the whole title is one phrase rather than a bag of words, with the two
6005/// characters GitHub's own quoting grammar gives a meaning inside a quoted phrase escaped
6006/// the way it documents. A title matched here is still compared for equality afterwards:
6007/// the qualifier narrows what the server sends, and this source decides what it names.
6008fn title_qualifier(name: &str) -> String {
6009 let escaped = name.replace('\\', "\\\\").replace('"', "\\\"");
6010 format!("in:title \"{escaped}\"")
6011}
6012
6013/// The board, and every item on it this source reports.
6014#[derive(Clone)]
6015struct Board {
6016 id: String,
6017 fields: Value,
6018 items: Vec<Resolved>,
6019}
6020
6021/// What a write needs of the board and nothing more: its node id and its field
6022/// definitions, in the shape a read of the board's own `fields` gives them.
6023///
6024/// Deliberately no items. A write decides which item it writes, which parent it files
6025/// under and which far ends it names by reading each of them by its own id; this is the
6026/// half of the board those reads cannot carry, and holding no item is what keeps it from
6027/// ever being asked whether an item is there.
6028#[derive(Clone)]
6029struct BoardFields {
6030 id: BoardId,
6031 fields: Value,
6032}
6033
6034/// A board's node id: what a field write and `addProjectV2ItemById` address.
6035///
6036/// Never blank, because a blank one addresses no board — so an id GitHub answers blank is
6037/// refused where it is read, and one an item names blank is read as not named at all.
6038#[derive(Clone)]
6039struct BoardId(String);
6040
6041/// Where one write left its item, for the record the rest of the command reads it out of.
6042///
6043/// A named record rather than a tuple because the update arm and the create arm each fill
6044/// all four, and two `Option`s of different meaning side by side in a tuple are two
6045/// positions a reader has to count.
6046struct Landed {
6047 /// The issue's own node id, which is the [`NativeId`] this source reports.
6048 content_id: NativeId,
6049 /// The board item's id, which is what a field write addresses.
6050 // 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.
6051 item_id: String,
6052 /// The web address GitHub gave the issue, when it gave one.
6053 // 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.
6054 url: Option<String>,
6055 /// The issue's number on its repository, when GitHub reported one.
6056 number: Option<u64>,
6057}
6058
6059impl BoardId {
6060 fn parse(id: &str) -> Result<Self, SourceError> {
6061 if id.trim().is_empty() {
6062 return Err(SourceError::Malformed {
6063 message: "GitHub named a board with a blank node id".into(),
6064 });
6065 }
6066 Ok(Self(id.to_owned()))
6067 }
6068
6069 fn as_str(&self) -> &str {
6070 &self.0
6071 }
6072}
6073
6074impl Board {
6075 fn field<'a>(fields: &'a Value, name: &str) -> Result<Option<&'a Value>, SourceError> {
6076 complete_connection(fields, "project fields", NESTED_PAGE_SIZE)?;
6077 let nodes = fields
6078 .get("nodes")
6079 .and_then(Value::as_array)
6080 .ok_or_else(|| SourceError::Malformed {
6081 message: "GitHub project fields.nodes is not an array".into(),
6082 })?;
6083 Ok(nodes
6084 .iter()
6085 .find(|field| field.get("name").and_then(Value::as_str) == Some(name)))
6086 }
6087}
6088
6089/// One board item, resolved into everything this source reports about it.
6090#[derive(Clone)]
6091struct Resolved {
6092 item_id: String,
6093 id: NativeId,
6094 content_kind: ContentKind,
6095 kind: BoardKind,
6096 title: String,
6097 body: Option<String>,
6098 /// The body exactly as GitHub holds it, metadata slot and all, which is what a write
6099 /// that changes the slot alone has to keep byte for byte outside it.
6100 raw_body: Option<String>,
6101 status: Status,
6102 /// The name of the board `Status` option this item sits in, as the board spells it.
6103 option: Option<String>,
6104 /// What its `Priority` field says, read through this instance's mapping.
6105 priority: HeldPriority,
6106 /// Whether this item's issue is closed. A draft has no such state and is never closed.
6107 closed: bool,
6108 /// The tasks this one delivers, read out of its slot. Empty for anything not a task.
6109 delivers: Vec<TaskRef>,
6110 /// Every task that delivers this one, read out of its slot. Empty for anything not a
6111 /// task.
6112 delivered_by: Vec<TaskRef>,
6113 labels: Vec<Label>,
6114 parent: Option<NativeId>,
6115 // 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.
6116 origin: Option<String>,
6117 /// The issue's own number on its repository, as GitHub reports it.
6118 ///
6119 /// `None` in exactly two cases: a draft, which has no number at all — `DraftIssue`
6120 /// declares none, and a draft is not filed in a repository to be numbered by one — and
6121 /// an issue this run created whose creating mutation answered without one, which is a
6122 /// response GitHub's own schema says cannot happen and which a landed write is not
6123 /// worth failing over. An `Issue` read off the board always has one.
6124 number: Option<u64>,
6125 url: Option<String>,
6126 created_at: Option<DateTime<Utc>>,
6127 updated_at: Option<DateTime<Utc>>,
6128 own_repository: Option<Repository>,
6129 repositories: Vec<Repository>,
6130 slot: BTreeMap<String, Value>,
6131 /// The node id of the board this item sits on, when the read that reached it said.
6132 board_id: Option<String>,
6133 /// The definition of every board field this item holds a value of, in the shape a read
6134 /// of the board's own `fields` gives one.
6135 ///
6136 /// Only the fields this item has a value in: a field it holds nothing of is not here,
6137 /// which says nothing about whether the board has it.
6138 fields: Vec<Value>,
6139}
6140
6141impl Resolved {
6142 /// The board this item's own read names it on, when that read named one this source can
6143 /// address.
6144 fn named_board(&self) -> Option<BoardId> {
6145 self.board_id
6146 .as_deref()
6147 .and_then(|id| BoardId::parse(id).ok())
6148 }
6149
6150 /// Whether this item holds a value of the board field called `name`, and so carries
6151 /// that field's definition. `false` says nothing about whether the board has the field.
6152 fn defines(&self, name: &str) -> bool {
6153 self.fields
6154 .iter()
6155 .any(|field| field.get("name").and_then(Value::as_str) == Some(name))
6156 }
6157
6158 /// The metadata a caller sees: their own keys, plus the copy origin this source keeps
6159 /// in a field of its own, and none of the five keys that are only an encoding.
6160 ///
6161 /// The two delivery keys are left out for every kind, not only for a task: they are
6162 /// the encoding of [`Task::delivers`] and [`Task::delivered_by`], and a project or a
6163 /// document carrying one holds nothing a caller's own metadata could mean by it.
6164 fn metadata(&self) -> BTreeMap<String, Value> {
6165 let mut metadata = self.slot.clone();
6166 metadata.remove(Repository::METADATA_KEY);
6167 metadata.remove(DependencyEdge::RECORDED_KEY);
6168 metadata.remove(ItemKind::METADATA_KEY);
6169 metadata.remove(TaskRef::DELIVERS_KEY);
6170 metadata.remove(TaskRef::DELIVERED_BY_KEY);
6171 if let Some(origin) = &self.origin {
6172 metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin.clone()));
6173 }
6174 metadata
6175 }
6176
6177 /// Where this item is, as a link a reader can open.
6178 ///
6179 /// A board is a hosted place and every issue on it has a web address, so that address
6180 /// is what "where is this?" means here — and [`Location::Url`] is what says which kind
6181 /// of place it is, so a reader knows to open it rather than to read a file out. It
6182 /// does not replace or derive from `url`: the field goes on reporting exactly what it
6183 /// reported before, and this says what that address *is*.
6184 ///
6185 /// An item GitHub gave no `url` for — a draft has none — reports no location at all
6186 /// rather than a third variant, which is the contract's "the source did not say". An
6187 /// issue this run created is not one of those: its address comes back from the
6188 /// creating mutation, so it is somewhere a reader can open from the moment it exists
6189 /// rather than from whenever the board read catches up.
6190 fn location(&self) -> Option<Location> {
6191 self.url.clone().map(Location::Url)
6192 }
6193
6194 /// The short handle this board's backend shows people for a task: the issue's number
6195 /// alone, as a decimal string.
6196 ///
6197 /// The number alone rather than `owner/repo#1043`, because that is the contract's
6198 /// value for this backend. A draft has no number and so no handle, which is the
6199 /// contract's *absent* rather than a handle of some other shape — and the native
6200 /// [`Task::id`] here is the issue's GraphQL node id, which this neither replaces nor
6201 /// derives from.
6202 fn key(&self) -> Option<String> {
6203 self.number.map(|number| number.to_string())
6204 }
6205
6206 /// Whether its `Priority` field holds a value at all, mapped or not.
6207 fn holds_priority(&self) -> bool {
6208 self.priority != HeldPriority::Read(Priority::None)
6209 }
6210
6211 /// The task this item is.
6212 ///
6213 /// Fails for an item whose `Priority` field holds an option the mapping does not name:
6214 /// reading that as a level would be a guess, and reading it as `none` would let the next
6215 /// copy clear a priority a person set.
6216 fn task(&self) -> Result<Task, SourceError> {
6217 let priority = match &self.priority {
6218 HeldPriority::Read(priority) => *priority,
6219 HeldPriority::Unmapped(option) => {
6220 return Err(SourceError::Malformed {
6221 message: format!(
6222 "task {}{} sits in the board {PRIORITY_FIELD} option {option:?}, which \
6223 this source's priority_mapping does not name, so its priority cannot be \
6224 read; next: name {option:?} under priority_mapping, or move the item to \
6225 a mapped option",
6226 self.id,
6227 self.number
6228 .map(|number| format!(" (#{number})"))
6229 .unwrap_or_default()
6230 ),
6231 });
6232 }
6233 };
6234 Ok(Task {
6235 id: self.id.clone(),
6236 key: self.key(),
6237 title: self.title.clone(),
6238 content: self.body.clone(),
6239 status: self.status.clone(),
6240 priority,
6241 labels: self.labels.clone(),
6242 project: self.parent.clone(),
6243 url: self.url.clone(),
6244 location: self.location(),
6245 created_at: self.created_at,
6246 updated_at: self.updated_at,
6247 metadata: self.metadata(),
6248 repositories: self.repositories.clone(),
6249 delivers: self.delivers.clone(),
6250 delivered_by: self.delivered_by.clone(),
6251 })
6252 }
6253
6254 fn project(&self) -> Project {
6255 Project {
6256 id: self.id.clone(),
6257 title: self.title.clone(),
6258 content: self.body.clone(),
6259 status: self.status.clone(),
6260 labels: self.labels.clone(),
6261 url: self.url.clone(),
6262 location: self.location(),
6263 created_at: self.created_at,
6264 updated_at: self.updated_at,
6265 metadata: self.metadata(),
6266 repositories: self.repositories.clone(),
6267 }
6268 }
6269
6270 /// The same issue as a document: the project it is filed under, and no status and no
6271 /// dependencies, because a document is not work.
6272 fn document(&self) -> Document {
6273 Document {
6274 id: self.id.clone(),
6275 title: self.title.clone(),
6276 content: self.body.clone(),
6277 project: self.parent.clone(),
6278 labels: self.labels.clone(),
6279 url: self.url.clone(),
6280 location: self.location(),
6281 created_at: self.created_at,
6282 updated_at: self.updated_at,
6283 metadata: self.metadata(),
6284 repositories: self.repositories.clone(),
6285 }
6286 }
6287}
6288
6289/// What one write is, and the status that comes with being it.
6290///
6291/// One value rather than a [`BoardKind`] beside an `Option<Status>`: a document has no
6292/// status and a task or a project always has one, so "a document carrying a status" and
6293/// "a task carrying none" are states a write cannot be in rather than states every use
6294/// site below has to defend against.
6295enum Written<'a> {
6296 /// A document, which is not work and so has no status at all.
6297 Document,
6298 /// A task or a project, and the status it is being written with.
6299 Work(ItemKind, &'a Status),
6300}
6301
6302impl Written<'_> {
6303 /// Which of the board's three kinds this write is.
6304 const fn kind(&self) -> BoardKind {
6305 match self {
6306 Self::Document => BoardKind::Document,
6307 Self::Work(kind, _) => BoardKind::Work(*kind),
6308 }
6309 }
6310
6311 /// The status this write carries. A document carries none, so a write of one says
6312 /// nothing about the issue's open or closed state and selects no board `Status`
6313 /// option.
6314 const fn status(&self) -> Option<&Status> {
6315 match self {
6316 Self::Document => None,
6317 Self::Work(_, status) => Some(status),
6318 }
6319 }
6320}
6321
6322/// The item being written, in the one shape all three write methods reach.
6323struct Incoming<'a> {
6324 written: Written<'a>,
6325 /// The title a person wrote. A document's goes onto the issue with
6326 /// [`DESIGN_TITLE_PREFIX`] put back, so a round trip returns the title that went in.
6327 title: &'a str,
6328 content: Option<&'a str>,
6329 labels: &'a [Label],
6330 metadata: &'a BTreeMap<String, Value>,
6331 repositories: &'a [Repository],
6332 parent: Option<&'a NativeId>,
6333 /// [`Task::delivers`], already checked. Empty for a project or a document, which is
6334 /// what keeps either key out of their slot.
6335 delivers: &'a [TaskRef],
6336 /// [`Task::delivered_by`], already checked. Empty for a project or a document.
6337 delivered_by: &'a [TaskRef],
6338 /// [`Task::priority`], for a task written to an instance that holds one; `None` for a
6339 /// project, a document, and every write to an instance with no `priority_mapping` —
6340 /// which is what keeps such a write's requests exactly what they were before.
6341 priority: Option<Priority>,
6342}
6343
6344/// What one write does to an item's `Priority` field.
6345enum PriorityWrite {
6346 /// Select this option of this field.
6347 Select {
6348 /// The `Priority` field's id.
6349 field: String,
6350 /// The mapped option's id.
6351 option: String,
6352 },
6353 /// Clear the field's value, which is what `none` is.
6354 Clear {
6355 /// The `Priority` field's id.
6356 field: String,
6357 },
6358}
6359
6360impl Incoming<'_> {
6361 /// The title this write puts on the issue.
6362 fn written_title(&self) -> String {
6363 match self.written {
6364 Written::Document => format!("{DESIGN_TITLE_PREFIX}{}", self.title),
6365 Written::Work(..) => self.title.to_owned(),
6366 }
6367 }
6368}
6369
6370#[derive(Clone, Copy, PartialEq, Eq)]
6371enum ContentKind {
6372 DraftIssue,
6373 Issue,
6374}
6375
6376/// What one board issue is: a document, or the work an [`ItemKind`] names.
6377///
6378/// A type of this source's own rather than an `ItemKind` with a third variant, because
6379/// `ItemKind` names what a dependency endpoint points at and nothing may point at a
6380/// document — the contract keeps a document out of that enum deliberately. Holding the
6381/// board's three answers in one value is what makes every place that asks "which is this?"
6382/// answer all three, rather than a `document: bool` beside a `kind` that means nothing for
6383/// two thirds of the board.
6384#[derive(Clone, Copy, PartialEq, Eq)]
6385enum BoardKind {
6386 /// An issue whose title begins [`DESIGN_TITLE_PREFIX`].
6387 Document,
6388 /// Every other issue, and every draft.
6389 Work(ItemKind),
6390}
6391
6392impl BoardKind {
6393 /// How a refusal names this kind to the person reading it.
6394 const fn describes(self) -> &'static str {
6395 match self {
6396 Self::Document => "document",
6397 Self::Work(kind) => kind.marker(),
6398 }
6399 }
6400}
6401
6402/// Whether `labels` satisfies `filter`, matching by name, case-insensitively.
6403///
6404/// This is the local Markdown source's `labels_match`, spelled the same way on purpose:
6405/// the shared cross-source journeys assert one answer to one question, so two sources
6406/// that disagree about what "carries the label bug" means fail them.
6407fn labels_match(labels: &[Label], filter: &LabelFilter) -> bool {
6408 let holds = |name: &String| {
6409 labels
6410 .iter()
6411 .any(|label| label.name.eq_ignore_ascii_case(name))
6412 };
6413 (filter.any_of.is_empty() || filter.any_of.iter().any(holds))
6414 && filter.all_of.iter().all(holds)
6415 && !filter.none_of.iter().any(holds)
6416}
6417
6418/// Whether `category` is one of `statuses`. An empty list is unfiltered rather than
6419/// "keeps nothing", which is what lets a `Vec<StatusCategory>` spell no filter at all.
6420fn status_matches(category: StatusCategory, statuses: &[StatusCategory]) -> bool {
6421 statuses.is_empty() || statuses.contains(&category)
6422}
6423
6424/// Whether `title`/`content` satisfies `query`, matching case-insensitively.
6425///
6426/// `content` is the item's own prose — the body with this source's trailing metadata
6427/// comment already taken off — so a search never matches an encoding the author of the
6428/// issue never wrote.
6429fn text_matches(title: &str, content: Option<&str>, query: &TextQuery) -> bool {
6430 let terms = query.terms.to_lowercase();
6431 let in_title = title.to_lowercase().contains(&terms);
6432 let in_content = content.is_some_and(|body| body.to_lowercase().contains(&terms));
6433 match query.fields {
6434 TextFields::Title => in_title,
6435 TextFields::Content => in_content,
6436 TextFields::TitleOrContent => in_title || in_content,
6437 }
6438}
6439
6440/// Whether `task` satisfies `query`, with `project` deciding the project predicate.
6441///
6442/// The project predicate is passed separately because a read narrowed to one project has
6443/// already answered it by asking *that project* for its own items — and re-applying it
6444/// there would compare the caller's selector, which may be a project's **name**, against
6445/// the id of the project that name resolved to, and keep nothing. Every other read passes
6446/// `query.project` and applies it here, which is what keeps `projects` a predicate this
6447/// source really does apply.
6448fn task_matches(task: &Task, query: &TaskQuery, project: &ProjectFilter) -> bool {
6449 labels_match(&task.labels, &query.labels)
6450 && status_matches(task.status.category, &query.statuses)
6451 && (query.priorities.is_empty() || query.priorities.contains(&task.priority))
6452 && match project {
6453 ProjectFilter::Any => true,
6454 ProjectFilter::Orphans => task.project.is_none(),
6455 ProjectFilter::Is(id) => task.project.as_ref() == Some(id),
6456 }
6457 && query
6458 .text
6459 .as_ref()
6460 .is_none_or(|text| text_matches(&task.title, task.content.as_deref(), text))
6461}
6462
6463fn project_matches(project: &Project, query: &ProjectQuery) -> bool {
6464 labels_match(&project.labels, &query.labels)
6465 && status_matches(project.status.category, &query.statuses)
6466 && query
6467 .text
6468 .as_ref()
6469 .is_none_or(|text| text_matches(&project.title, project.content.as_deref(), text))
6470}
6471
6472/// The same three predicates a task query carries, minus the status filter.
6473///
6474/// A document is not work, so it has no status for one to compare against and the query
6475/// type carries none. The project predicate is the same one — a design issue filed under a
6476/// project issue is in that project, and one filed under nothing is in none — so it is
6477/// spelled the same way here rather than answered differently.
6478fn document_matches(document: &Document, query: &DocumentQuery, project: &ProjectFilter) -> bool {
6479 labels_match(&document.labels, &query.labels)
6480 && match project {
6481 ProjectFilter::Any => true,
6482 ProjectFilter::Orphans => document.project.is_none(),
6483 ProjectFilter::Is(id) => document.project.as_ref() == Some(id),
6484 }
6485 && query
6486 .text
6487 .as_ref()
6488 .is_none_or(|text| text_matches(&document.title, document.content.as_deref(), text))
6489}
6490
6491#[async_trait::async_trait]
6492impl TaskSource for GitHubProjectsSource {
6493 fn kind(&self) -> &'static str {
6494 KIND
6495 }
6496 fn capabilities(&self) -> Capabilities {
6497 Capabilities {
6498 projects: Support::Native,
6499 documents: Support::Native,
6500 comments: Support::Native,
6501 priority: if self.priorities.is_some() {
6502 Support::Native
6503 } else {
6504 Support::Unsupported
6505 },
6506 filter_by_priority: Support::Native,
6507 orphan_tasks: Support::Native,
6508 filter_by_label: Support::Native,
6509 filter_by_status: Support::Native,
6510 search_title: Support::Native,
6511 search_content: Support::Native,
6512 task_dependencies: DependencySupport::BothDirections,
6513 project_dependencies: DependencySupport::BothDirections,
6514 max_page_size: MAX_PAGE_SIZE,
6515 }
6516 }
6517 async fn health(&self) -> Result<Health, SourceError> {
6518 let board = self.board_page(None, 1).await?;
6519 Ok(Health {
6520 reachable: true,
6521 detail: Some(format!(
6522 "reading GitHub project {}/{} ({})",
6523 self.owner,
6524 self.project_number,
6525 required_str(&board, "title")?
6526 )),
6527 })
6528 }
6529 async fn get_task(&self, id: &NativeId) -> Result<Option<Task>, SourceError> {
6530 self.item_by_id(id)
6531 .await?
6532 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
6533 .map(|item| item.task())
6534 .transpose()
6535 }
6536 async fn get_project(&self, id: &NativeId) -> Result<Option<Project>, SourceError> {
6537 Ok(self
6538 .item_by_id(id)
6539 .await?
6540 .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
6541 .map(|item| item.project()))
6542 }
6543 async fn query_tasks(
6544 &self,
6545 query: &TaskQuery,
6546 page: &PageRequest,
6547 ) -> Result<Page<Task>, SourceError> {
6548 validate_page(page)?;
6549 // A read narrowed to one project asks that project for its own tasks, so nothing
6550 // about it costs what the rest of the board holds. Every other task read is a
6551 // question about the whole board and is answered by reading it.
6552 let (held, membership) = match &query.project {
6553 ProjectFilter::Is(project) => (
6554 self.project_children(project).await?,
6555 // Answered by where these items came from; see `task_matches`.
6556 &ProjectFilter::Any,
6557 ),
6558 ProjectFilter::Any | ProjectFilter::Orphans => {
6559 (self.board().await?.items, &query.project)
6560 }
6561 };
6562 // Filtered before paged: a page of a filtered result is a page of the survivors,
6563 // never the survivors of a page.
6564 let mut tasks = Vec::new();
6565 for item in held
6566 .iter()
6567 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
6568 {
6569 let task = item.task()?;
6570 if task_matches(&task, query, membership) {
6571 tasks.push(task);
6572 }
6573 }
6574 Ok(offset_page(
6575 tasks,
6576 numeric_cursor(page.cursor.as_ref())?,
6577 page.limit.min(MAX_PAGE_SIZE) as usize,
6578 ))
6579 }
6580 async fn query_projects(
6581 &self,
6582 query: &ProjectQuery,
6583 page: &PageRequest,
6584 ) -> Result<Page<Project>, SourceError> {
6585 validate_page(page)?;
6586 // The projects a board holds are found by an issue search scoped to that board,
6587 // never by walking the board's own item connection: what tells a project from a
6588 // task is the `parent` each issue carries, which costs nothing to read.
6589 let projects = self
6590 .board_issues()
6591 .await?
6592 .iter()
6593 .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
6594 .map(Resolved::project)
6595 .filter(|project| project_matches(project, query))
6596 .collect();
6597 Ok(offset_page(
6598 projects,
6599 numeric_cursor(page.cursor.as_ref())?,
6600 page.limit.min(MAX_PAGE_SIZE) as usize,
6601 ))
6602 }
6603 async fn get_document(&self, id: &NativeId) -> Result<Option<Document>, SourceError> {
6604 Ok(self
6605 .item_by_id(id)
6606 .await?
6607 .filter(|item| item.kind == BoardKind::Document)
6608 .map(|item| item.document()))
6609 }
6610 async fn query_documents(
6611 &self,
6612 query: &DocumentQuery,
6613 page: &PageRequest,
6614 ) -> Result<Page<Document>, SourceError> {
6615 validate_page(page)?;
6616 // Narrowed to one project, this is the same sub-issue read a task list scoped to
6617 // that project makes — a document filed under a project is a sub-issue of it too,
6618 // and which of them come back is the kind this caller asked for.
6619 let (held, membership) = match &query.project {
6620 ProjectFilter::Is(project) => (
6621 self.project_children(project).await?,
6622 // Answered by where these items came from; see `task_matches`.
6623 &ProjectFilter::Any,
6624 ),
6625 ProjectFilter::Any | ProjectFilter::Orphans => {
6626 (self.board().await?.items, &query.project)
6627 }
6628 };
6629 // Filtered before paged, exactly as a task read is: a page of a filtered result is
6630 // a page of the survivors, never the survivors of a page.
6631 let documents = held
6632 .iter()
6633 .filter(|item| item.kind == BoardKind::Document)
6634 .map(Resolved::document)
6635 .filter(|document| document_matches(document, query, membership))
6636 .collect();
6637 Ok(offset_page(
6638 documents,
6639 numeric_cursor(page.cursor.as_ref())?,
6640 page.limit.min(MAX_PAGE_SIZE) as usize,
6641 ))
6642 }
6643 async fn labels(&self, page: &PageRequest) -> Result<Page<Label>, SourceError> {
6644 validate_page(page)?;
6645 let offset = numeric_cursor(page.cursor.as_ref())?;
6646 let mut labels = self
6647 .board()
6648 .await?
6649 .items
6650 .into_iter()
6651 .flat_map(|item| item.labels)
6652 .fold(Vec::new(), |mut all, label| {
6653 if !all.iter().any(|x: &Label| x.id == label.id) {
6654 all.push(label);
6655 }
6656 all
6657 });
6658 labels.sort_by(|a, b| a.name.cmp(&b.name).then(a.id.0.cmp(&b.id.0)));
6659 Ok(offset_page(
6660 labels,
6661 offset,
6662 page.limit.min(MAX_PAGE_SIZE) as usize,
6663 ))
6664 }
6665 async fn task_dependencies(
6666 &self,
6667 id: &NativeId,
6668 direction: Direction,
6669 page: &PageRequest,
6670 ) -> Result<Page<DependencyEdge>, SourceError> {
6671 self.dependencies(id, ItemKind::Task, direction, page).await
6672 }
6673 async fn project_dependencies(
6674 &self,
6675 id: &NativeId,
6676 direction: Direction,
6677 page: &PageRequest,
6678 ) -> Result<Page<DependencyEdge>, SourceError> {
6679 self.dependencies(id, ItemKind::Project, direction, page)
6680 .await
6681 }
6682
6683 fn writes(&self) -> WriteSupport {
6684 WriteSupport::Supported
6685 }
6686
6687 /// Create or update one task.
6688 ///
6689 /// Its `delivers` and `delivered_by` are checked before anything is read or written —
6690 /// neither may name the task itself or name one task twice — and land in the body's
6691 /// metadata slot under their reserved keys, in place of any caller metadata of those
6692 /// names.
6693 async fn write_task(&self, write: &ItemWrite<Task>) -> Result<NativeId, SourceError> {
6694 let near = write.target.as_ref().unwrap_or(&write.item.id);
6695 for (key, entries) in [
6696 (TaskRef::DELIVERS_KEY, &write.item.delivers),
6697 (TaskRef::DELIVERED_BY_KEY, &write.item.delivered_by),
6698 ] {
6699 TaskRef::listed(key, near, Some(&self.name), entries.clone())
6700 .map_err(|message| SourceError::Refused { message })?;
6701 }
6702 if self.priorities.is_none() && write.item.priority != Priority::None {
6703 return Err(self.holds_no_priority());
6704 }
6705 self.write_item(
6706 &Incoming {
6707 written: Written::Work(ItemKind::Task, &write.item.status),
6708 title: &write.item.title,
6709 content: write.item.content.as_deref(),
6710 labels: &write.item.labels,
6711 metadata: &write.item.metadata,
6712 repositories: &write.item.repositories,
6713 parent: write.item.project.as_ref(),
6714 delivers: &write.item.delivers,
6715 delivered_by: &write.item.delivered_by,
6716 priority: self.priorities.as_ref().map(|_| write.item.priority),
6717 },
6718 write.target.as_ref(),
6719 &write.depends_on,
6720 )
6721 .await
6722 }
6723
6724 async fn write_project(&self, write: &ItemWrite<Project>) -> Result<NativeId, SourceError> {
6725 self.write_item(
6726 &Incoming {
6727 written: Written::Work(ItemKind::Project, &write.item.status),
6728 title: &write.item.title,
6729 content: write.item.content.as_deref(),
6730 labels: &write.item.labels,
6731 metadata: &write.item.metadata,
6732 repositories: &write.item.repositories,
6733 parent: None,
6734 delivers: &[],
6735 delivered_by: &[],
6736 priority: None,
6737 },
6738 write.target.as_ref(),
6739 &write.depends_on,
6740 )
6741 .await
6742 }
6743
6744 /// Create or update one document, which is one issue titled the way this board spells
6745 /// a document.
6746 ///
6747 /// Everything else is exactly a task write: caller metadata goes to the same canonical
6748 /// JSON slot at the end of the body and comes back with its JSON types intact, a key
6749 /// or a field this board cannot carry is refused by name rather than dropped, a target
6750 /// naming an issue this board does not hold is refused rather than created, and an
6751 /// issue this call created is taken back when the rest of the write fails.
6752 async fn write_document(&self, write: &ItemWrite<Document>) -> Result<NativeId, SourceError> {
6753 // A document takes part in no dependency graph, so there is no far end to write
6754 // natively and none to record: a caller naming one is told so rather than having it
6755 // stored under the reserved key, where a later read would report an edge the
6756 // contract says cannot exist.
6757 if !write.depends_on.is_empty() {
6758 return Err(SourceError::Refused {
6759 message: format!(
6760 "this write names {} dependencies for a document, and a document takes \
6761 part in no dependency graph; next: put the dependency on the task or \
6762 project the document is about",
6763 write.depends_on.len()
6764 ),
6765 });
6766 }
6767 self.write_item(
6768 &Incoming {
6769 written: Written::Document,
6770 title: &write.item.title,
6771 content: write.item.content.as_deref(),
6772 labels: &write.item.labels,
6773 metadata: &write.item.metadata,
6774 repositories: &write.item.repositories,
6775 parent: write.item.project.as_ref(),
6776 delivers: &[],
6777 delivered_by: &[],
6778 priority: None,
6779 },
6780 write.target.as_ref(),
6781 &[],
6782 )
6783 .await
6784 }
6785
6786 /// Set one task's status alone.
6787 ///
6788 /// An open target reopens a closed issue with an `updateIssue` carrying only its
6789 /// `stateInput`, then selects the board option with `updateProjectV2ItemFieldValue`; a
6790 /// terminal target selects its mapped option, then closes with its fixed reason. No
6791 /// request carries a title, a body or a label. The status
6792 /// answered is what [`StatusMapping::status`] reads off the state just written, which is
6793 /// what a re-read reports.
6794 async fn set_task_status(
6795 &self,
6796 id: &NativeId,
6797 category: StatusCategory,
6798 ) -> Result<Option<Status>, SourceError> {
6799 self.set_status(id, category).await
6800 }
6801
6802 /// Set one task's priority alone: one `updateProjectV2ItemFieldValue` selecting the
6803 /// mapped option of the board's `Priority` field, or one `clearProjectV2ItemFieldValue`
6804 /// for `none`. Refused by an instance with no `priority_mapping`.
6805 async fn set_task_priority(
6806 &self,
6807 id: &NativeId,
6808 priority: Priority,
6809 ) -> Result<Option<Priority>, SourceError> {
6810 self.set_priority(id, priority).await
6811 }
6812
6813 /// Replace one task's content with a single body update that keeps the metadata slot
6814 /// byte for byte.
6815 async fn set_task_content(
6816 &self,
6817 id: &NativeId,
6818 content: &str,
6819 ) -> Result<Option<()>, SourceError> {
6820 self.replace_content(id, content).await
6821 }
6822
6823 /// Replace one task issue's content and its provenance slot entry with a single body
6824 /// update. The answers are not kept: see `replace_rendering`.
6825 async fn set_task_rendering(
6826 &self,
6827 id: &NativeId,
6828 content: &str,
6829 provenance: &Value,
6830 _answers: &BTreeMap<String, Value>,
6831 ) -> Result<Option<()>, SourceError> {
6832 self.replace_rendering(id, BoardKind::Work(ItemKind::Task), content, provenance)
6833 .await
6834 }
6835
6836 /// Replace one design-document issue's content and its provenance slot entry, on exactly
6837 /// the terms of [`set_task_rendering`](TaskSource::set_task_rendering).
6838 async fn set_document_rendering(
6839 &self,
6840 id: &NativeId,
6841 content: &str,
6842 provenance: &Value,
6843 _answers: &BTreeMap<String, Value>,
6844 ) -> Result<Option<()>, SourceError> {
6845 self.replace_rendering(id, BoardKind::Document, content, provenance)
6846 .await
6847 }
6848
6849 /// Replace one task's `delivered_by` with a single body update that changes the
6850 /// metadata slot and nothing outside it.
6851 async fn set_delivered_by(
6852 &self,
6853 id: &NativeId,
6854 delivered_by: &[TaskRef],
6855 ) -> Result<Option<()>, SourceError> {
6856 self.replace_delivered_by(id, delivered_by).await
6857 }
6858
6859 /// Set one key of one task issue's metadata with a single body update that changes the
6860 /// metadata slot and nothing outside it — no title, label, state or board field request —
6861 /// and sends nothing when the task already holds that value under the key.
6862 async fn set_task_metadata(
6863 &self,
6864 id: &NativeId,
6865 key: &MetadataKey,
6866 value: &Value,
6867 ) -> Result<Option<Task>, SourceError> {
6868 Ok(self
6869 .set_slot_key(id, BoardKind::Work(ItemKind::Task), key, value)
6870 .await?
6871 .map(|item| item.task())
6872 .transpose()?)
6873 }
6874
6875 /// Set one key of one project issue's metadata, on exactly the terms of
6876 /// [`set_task_metadata`](TaskSource::set_task_metadata).
6877 async fn set_project_metadata(
6878 &self,
6879 id: &NativeId,
6880 key: &MetadataKey,
6881 value: &Value,
6882 ) -> Result<Option<Project>, SourceError> {
6883 Ok(self
6884 .set_slot_key(id, BoardKind::Work(ItemKind::Project), key, value)
6885 .await?
6886 .map(|item| item.project()))
6887 }
6888
6889 /// Set one key of one design-document issue's metadata, on exactly the terms of
6890 /// [`set_task_metadata`](TaskSource::set_task_metadata).
6891 async fn set_document_metadata(
6892 &self,
6893 id: &NativeId,
6894 key: &MetadataKey,
6895 value: &Value,
6896 ) -> Result<Option<Document>, SourceError> {
6897 Ok(self
6898 .set_slot_key(id, BoardKind::Document, key, value)
6899 .await?
6900 .map(|item| item.document()))
6901 }
6902
6903 async fn delete_task(&self, id: &NativeId) -> Result<(), SourceError> {
6904 self.delete_item(id).await
6905 }
6906
6907 async fn delete_project(&self, id: &NativeId) -> Result<(), SourceError> {
6908 self.delete_item(id).await
6909 }
6910
6911 async fn delete_document(&self, id: &NativeId) -> Result<(), SourceError> {
6912 self.delete_item(id).await
6913 }
6914
6915 /// One page of the task issue's own comments, walked by GitHub's own cursor.
6916 ///
6917 /// Nothing here filters, so nothing has to be read ahead of the page: the caller's limit is
6918 /// the page GitHub is asked for and GitHub's `endCursor` is the cursor handed back.
6919 async fn task_comments(
6920 &self,
6921 task: &NativeId,
6922 page: &PageRequest,
6923 ) -> Result<Option<Page<Comment>>, SourceError> {
6924 validate_page(page)?;
6925 let Some(issue) = self.commented_issue(task).await? else {
6926 return Ok(None);
6927 };
6928 let after = page.cursor.as_ref().map(|cursor| cursor.0.as_str());
6929 let data = self
6930 .graphql(
6931 graphql::ISSUE_COMMENTS,
6932 json!({"id":issue.0,"first":page.limit.min(MAX_PAGE_SIZE),"after":after}),
6933 )
6934 .await?;
6935 // The issue was there a moment ago; one removed since is no longer a task here.
6936 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
6937 return Ok(None);
6938 };
6939 let connection = node
6940 .get("comments")
6941 .filter(|value| !value.is_null())
6942 .ok_or_else(|| SourceError::Malformed {
6943 message: format!(
6944 "GitHub issue {} answered with no comments connection",
6945 issue.0
6946 ),
6947 })?;
6948 let items = optional_nodes(Some(connection), "issue comments")?
6949 .into_iter()
6950 .flatten()
6951 .map(comment_from)
6952 .collect::<Result<Vec<_>, _>>()?;
6953 let next = next_cursor(connection)?;
6954 if let Some(next) = &next {
6955 validate_cursor_progress(after, &next.0)?;
6956 }
6957 Ok(Some(Page { items, next }))
6958 }
6959
6960 /// Add one comment to the task's issue, as the account the token belongs to.
6961 ///
6962 /// The author is refused before anything is sent — not even the task is read — because
6963 /// no answer GitHub could give would make posting under another name than the one asked
6964 /// for the right outcome.
6965 async fn add_comment(
6966 &self,
6967 task: &NativeId,
6968 comment: &NewComment,
6969 ) -> Result<Option<Comment>, SourceError> {
6970 if let Some(author) = &comment.author {
6971 return Err(SourceError::Refused {
6972 message: format!(
6973 "source {} cannot post a comment as {author:?}: GitHub records the account \
6974 the token signs in as the author of every comment; next: leave --author \
6975 out, and the comment is posted as that account",
6976 self.name
6977 ),
6978 });
6979 }
6980 let Some(issue) = self.commented_issue(task).await? else {
6981 return Ok(None);
6982 };
6983 let data = self
6984 .graphql(
6985 graphql::ADD_COMMENT,
6986 json!({"input":{"subjectId":issue.0,"body":comment.body.as_str()}}),
6987 )
6988 .await?;
6989 let subject = data
6990 .pointer("/addComment/subject")
6991 .filter(|value| !value.is_null())
6992 .ok_or_else(|| SourceError::Malformed {
6993 message: "GitHub comment addition returned no subject".into(),
6994 })?;
6995 if required_str(subject, "id")? != issue.0 {
6996 return Err(SourceError::Malformed {
6997 message: "GitHub comment addition answered about another issue".into(),
6998 });
6999 }
7000 let added = data
7001 .pointer("/addComment/commentEdge/node")
7002 .filter(|value| !value.is_null())
7003 .ok_or_else(|| SourceError::Malformed {
7004 message: "GitHub comment addition returned no comment".into(),
7005 })?;
7006 comment_from(added).map(Some)
7007 }
7008
7009 async fn edit_comment(
7010 &self,
7011 task: &NativeId,
7012 comment: &NativeId,
7013 body: &CommentBody,
7014 ) -> Result<Option<Comment>, SourceError> {
7015 let Some(issue) = self.commented_issue(task).await? else {
7016 return Ok(None);
7017 };
7018 if !self.comment_is_on(&issue, comment).await? {
7019 return Ok(None);
7020 }
7021 let data = self
7022 .graphql(
7023 graphql::UPDATE_COMMENT,
7024 json!({"input":{"id":comment.0,"body":body.as_str()}}),
7025 )
7026 .await?;
7027 let edited = data
7028 .pointer("/updateIssueComment/issueComment")
7029 .filter(|value| !value.is_null())
7030 .ok_or_else(|| SourceError::Malformed {
7031 message: "GitHub comment update returned no comment".into(),
7032 })?;
7033 let edited = comment_from(edited)?;
7034 if edited.id != *comment {
7035 return Err(SourceError::Malformed {
7036 message: "GitHub comment update returned the wrong comment".into(),
7037 });
7038 }
7039 Ok(Some(edited))
7040 }
7041
7042 async fn delete_comment(
7043 &self,
7044 task: &NativeId,
7045 comment: &NativeId,
7046 ) -> Result<Option<NativeId>, SourceError> {
7047 let Some(issue) = self.commented_issue(task).await? else {
7048 return Ok(None);
7049 };
7050 if !self.comment_is_on(&issue, comment).await? {
7051 return Ok(None);
7052 }
7053 let data = self
7054 .graphql(graphql::DELETE_COMMENT, json!({"input":{"id":comment.0}}))
7055 .await?;
7056 // The payload says nothing about the comment it removed, so what is checked is that
7057 // GitHub answered the mutation at all rather than leaving it unanswered.
7058 data.get("deleteIssueComment")
7059 .filter(|value| !value.is_null())
7060 .ok_or_else(|| SourceError::Malformed {
7061 message: "GitHub comment deletion returned no payload".into(),
7062 })?;
7063 Ok(Some(comment.clone()))
7064 }
7065
7066 /// Every request this source has recorded, and what each of GitHub's two budgets was
7067 /// attributed — read off the same accounting the session report is rendered from, so
7068 /// the two cannot count one request two ways.
7069 async fn metering(&self) -> Result<Option<Metering>, SourceError> {
7070 Ok(Some(self.ledger.snapshot().metering()))
7071 }
7072}
7073
7074/// One issue comment as the contract carries it.
7075///
7076/// `author` is absent both when GitHub answers `null` for an account that no longer exists
7077/// and when it answers an actor with no login, because either way the source did not say who
7078/// wrote it — which is what an absent author means, rather than an author called nothing.
7079fn comment_from(value: &Value) -> Result<Comment, SourceError> {
7080 Ok(Comment {
7081 id: NativeId(required_str(value, "id")?.to_owned()),
7082 author: optional_str(value.get("author").unwrap_or(&Value::Null), "login")?
7083 .map(str::to_owned),
7084 created_at: optional_time(value, "createdAt")?,
7085 updated_at: optional_time(value, "updatedAt")?,
7086 body: required_str(value, "body")?.to_owned(),
7087 url: optional_str(value, "url")?.map(str::to_owned),
7088 })
7089}
7090
7091/// Where the recorded tail of a dependency walk resumes; see
7092/// [`GitHubProjectsSource::recorded_edges`].
7093const RECORDED_CURSOR: &str = "onetaskgraph.depends_on:";
7094
7095/// The board text field this source keeps a copy's origin in.
7096///
7097/// Named after the key it holds, and held to that name by the guard below rather than by
7098/// a reader noticing.
7099const ORIGIN_FIELD: &str = "onetaskgraph.origin";
7100
7101/// The metadata key that field holds.
7102///
7103/// The engine owns this key and spells it once as `GlobalId::ORIGIN_KEY`; a plugin never
7104/// constructs or interprets the qualified id it carries. This source names it only to
7105/// route it — a short, typed value belongs in a typed field rather than in the body slot
7106/// a caller's own prose shares.
7107///
7108/// Restated rather than imported, because no plugin crate may depend on the engine. What
7109/// keeps the two spellings one contract is `scripts/check-origin-key-spelling.sh`, a
7110/// target in `check`: it reads the engine's own literal and fails naming the file and the
7111/// line when a plugin's parts from it either way. Drift here has one symptom — a copy
7112/// that creates a second item every run instead of finding the one it wrote — and that is
7113/// too late to learn it.
7114const ORIGIN_KEY: &str = "onetaskgraph.origin";
7115
7116/// Where a recorded tail resumes, refusing a cursor no walk in `direction` reported.
7117///
7118/// The reserved key holds forward edges and nothing else — the reverse of a recorded edge
7119/// is derived from the far end, never written down on the near item — so only a forward
7120/// walk ever reports one of these cursors. A reverse read carrying one is resuming a walk
7121/// it did not come from, and it is told so rather than answered with an empty page that
7122/// reads as a walk which ended.
7123fn recorded_offset(
7124 cursor: Option<&str>,
7125 direction: Direction,
7126) -> Result<Option<usize>, SourceError> {
7127 cursor
7128 .and_then(|cursor| cursor.strip_prefix(RECORDED_CURSOR))
7129 .map(|offset| {
7130 if direction != Direction::DependsOn {
7131 return Err(SourceError::Config {
7132 message: format!(
7133 "{RECORDED_CURSOR}{offset} resumes recorded forward edges, which a \
7134 reverse dependency read never issues; resume it in the direction \
7135 that reported it"
7136 ),
7137 });
7138 }
7139 offset.parse().map_err(|_| SourceError::Config {
7140 message: format!("{RECORDED_CURSOR}{offset} is not a recorded-edge cursor"),
7141 })
7142 })
7143 .transpose()
7144}
7145
7146fn recorded_page(edges: Vec<DependencyEdge>, offset: usize, limit: usize) -> Page<DependencyEdge> {
7147 let mut page = offset_page(edges, offset, limit.max(1));
7148 page.next = page
7149 .next
7150 .map(|cursor| Cursor(format!("{RECORDED_CURSOR}{}", cursor.0)));
7151 page
7152}
7153
7154/// The kind of one issue reached through a dependency connection.
7155///
7156/// The same questions the board scan asks, over the fields the dependency document
7157/// selects, and in the same order: the design prefix first, then a sub-issue is a task,
7158/// then anything with sub-issues or the marker is a project.
7159///
7160/// # Errors
7161///
7162/// A far end this board holds as a document is refused rather than reported. The two
7163/// answers that are not refusals would both be wrong: reporting it as a task names an id
7164/// no task read of this source can find, and reporting it as a project names one no
7165/// project read can. There is no third value to return — `ItemKind` has no document
7166/// variant, because nothing may point at a document — so the relationship itself is what
7167/// the person is told about.
7168fn related_kind(value: &Value) -> Result<ItemKind, SourceError> {
7169 let id = required_str(value, "id")?;
7170 if required_str(value, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
7171 return Err(SourceError::Refused {
7172 message: format!(
7173 "GitHub issue {id} is a document of this board — its title begins \
7174 {DESIGN_TITLE_PREFIX:?} — and nothing may depend on a document or be depended \
7175 on by one; next: remove that issue's blocking relationship on this board"
7176 ),
7177 });
7178 }
7179 let parent = optional_str(value.get("parent").unwrap_or(&Value::Null), "id")?;
7180 if parent.is_some() {
7181 return Ok(ItemKind::Task);
7182 }
7183 let (_, slot) = metadata_body(optional_str(value, "body")?.map(str::to_owned))?;
7184 let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
7185 message: format!("GitHub issue {id}: {message}"),
7186 })?;
7187 let sub_issues = sub_issue_total(value)?;
7188 Ok(if sub_issues > 0 || marked == Some(ItemKind::Project) {
7189 ItemKind::Project
7190 } else {
7191 ItemKind::Task
7192 })
7193}
7194
7195/// The `IssueStateUpdateInput` one status target asks for.
7196///
7197/// `stateInput` and `state` are mutually exclusive on `UpdateIssueInput`, and only this
7198/// one is ever sent. A non-terminal status always asks for `OPEN`, which is what reopens
7199/// a currently-closed issue: without that the item would read back `Unknown` and a copy
7200/// would report a change forever. A document has no status at all, and asks for neither.
7201fn state_input(target: Option<&StatusTarget>) -> Value {
7202 match target {
7203 Some(StatusTarget::Terminal(_, reason)) => {
7204 json!({"value":"CLOSED","stateReason":reason.reason()})
7205 }
7206 Some(StatusTarget::Column(_) | StatusTarget::Disabled) => json!({"value":"OPEN"}),
7207 // A document has no status, so a write of one says nothing about the issue's open
7208 // or closed state rather than forcing it open: `stateInput` is what carries that
7209 // instruction, and an explicit null asks for no change to it.
7210 None => Value::Null,
7211 }
7212}
7213
7214/// The metadata one write stores in the item's body slot.
7215///
7216/// The typed fields travel as themselves, so the three reserved keys are rebuilt here
7217/// rather than carried: the kind marker so an empty project stays readable, the
7218/// repository list only when it is not exactly the issue's own repository, and the far
7219/// ends no relationship here can name.
7220fn slot_metadata(
7221 incoming: &Incoming<'_>,
7222 own_repository: Option<&Repository>,
7223 fallback: &[DependencyEdge],
7224) -> BTreeMap<String, Value> {
7225 let mut metadata = incoming.metadata.clone();
7226 metadata.remove(ORIGIN_KEY);
7227 match incoming.written.kind() {
7228 BoardKind::Work(kind) => metadata.insert(
7229 ItemKind::METADATA_KEY.to_owned(),
7230 Value::String(kind.marker().to_owned()),
7231 ),
7232 // A document is told by its title, so it carries no kind marker: that key names
7233 // what a dependency endpoint points at, and nothing may point at a document.
7234 BoardKind::Document => metadata.remove(ItemKind::METADATA_KEY),
7235 };
7236 let derivable = own_repository
7237 .map(|own| incoming.repositories == [own.clone()])
7238 .unwrap_or(incoming.repositories.is_empty());
7239 if derivable {
7240 metadata.remove(Repository::METADATA_KEY);
7241 } else {
7242 metadata.insert(
7243 Repository::METADATA_KEY.to_owned(),
7244 Value::Array(
7245 incoming
7246 .repositories
7247 .iter()
7248 .map(|repository| Value::String(repository.as_str().to_owned()))
7249 .collect(),
7250 ),
7251 );
7252 }
7253 // The typed lists are what land, whatever the caller's own metadata held under their
7254 // keys: a key of either name travelling beside the field would otherwise be a second
7255 // answer to the same question, and the field is the one the contract names.
7256 for (key, entries) in [
7257 (TaskRef::DELIVERS_KEY, incoming.delivers),
7258 (TaskRef::DELIVERED_BY_KEY, incoming.delivered_by),
7259 ] {
7260 set_task_list(&mut metadata, key, entries);
7261 }
7262 if fallback.is_empty() {
7263 metadata.remove(DependencyEdge::RECORDED_KEY);
7264 } else {
7265 metadata.insert(
7266 DependencyEdge::RECORDED_KEY.to_owned(),
7267 Value::Array(
7268 fallback
7269 .iter()
7270 .map(|edge| json!({"id":edge.to.id(),"kind":edge.to.kind}))
7271 .collect(),
7272 ),
7273 );
7274 }
7275 metadata
7276}
7277
7278/// Every label one item carries, from its content's own connection and nowhere else.
7279///
7280/// There is no second place to read one from: no document this source sends selects the
7281/// board's built-in `Labels` field, because GitHub derives it from the content and a draft
7282/// cannot carry one at all. The module documentation records the three schema facts that
7283/// settle it.
7284fn labels(content: &Value) -> Result<Vec<Label>, SourceError> {
7285 optional_nodes(content.get("labels"), "content labels")?
7286 .into_iter()
7287 .flatten()
7288 .map(|v| {
7289 Ok(Label {
7290 id: NativeId(required_str(v, "id")?.to_owned()),
7291 name: required_str(v, "name")?.to_owned(),
7292 color: optional_str(v, "color")?.map(str::to_owned),
7293 })
7294 })
7295 .collect()
7296}
7297
7298/// The definition of each board field one item's values are values of, in the shape a read
7299/// of the board's own `fields` gives one.
7300///
7301/// A value names its field through a fragment on that field's own type, so the type is
7302/// known from which kind of value it is: a single-select value's field is a
7303/// `ProjectV2SingleSelectField`, options and all, and a text value's is a `ProjectV2Field`.
7304/// A value whose field carried no id, or an empty one, says nothing usable and is left out.
7305fn field_definitions(field_values: &[Value]) -> Vec<Value> {
7306 field_values
7307 .iter()
7308 .filter_map(|value| {
7309 let field = value.get("field")?.as_object()?;
7310 field.get("id")?.as_str().filter(|id| !id.is_empty())?;
7311 let typename = if value.get("text").is_some() {
7312 "ProjectV2Field"
7313 } else if value.get("name").is_some() {
7314 "ProjectV2SingleSelectField"
7315 } else {
7316 return None;
7317 };
7318 let mut defined = field.clone();
7319 defined.insert("__typename".to_owned(), json!(typename));
7320 Some(Value::Object(defined))
7321 })
7322 .collect()
7323}
7324
7325fn text_field(field_values: &[Value], name: &str) -> Result<Option<String>, SourceError> {
7326 let Some(node) = field_values
7327 .iter()
7328 .find(|node| node.pointer("/field/name").and_then(Value::as_str) == Some(name))
7329 else {
7330 return Ok(None);
7331 };
7332 Ok(optional_str(node, "text")?.map(str::to_owned))
7333}
7334
7335fn valid_github_owner(owner: &str) -> bool {
7336 !owner.is_empty()
7337 && owner.len() <= 39
7338 && !owner.starts_with('-')
7339 && !owner.ends_with('-')
7340 && !owner.contains("--")
7341 && owner
7342 .bytes()
7343 .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
7344}
7345
7346/// GitHub's repository-name grammar: 1-100 ASCII letters, digits, `-`, `_` or `.`, and
7347/// neither of the two names a path segment already means.
7348fn valid_github_repository_name(name: &str) -> bool {
7349 !name.is_empty()
7350 && name.len() <= 100
7351 && name != "."
7352 && name != ".."
7353 && name
7354 .bytes()
7355 .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
7356}
7357
7358fn valid_environment_name(name: &str) -> bool {
7359 let mut bytes = name.bytes();
7360 bytes
7361 .next()
7362 .is_some_and(|byte| byte.is_ascii_alphabetic() || byte == b'_')
7363 && bytes.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
7364}
7365
7366/// How many sub-issues one issue has.
7367///
7368/// `Issue.subIssuesSummary` is `SubIssuesSummary!` and its `total` is `Int!`, so an
7369/// absent or non-integer one is a response this source cannot read — and reading it as
7370/// zero would classify a project as a task, which is exactly the mistake the marker
7371/// exists to keep from happening quietly.
7372fn sub_issue_total(issue: &Value) -> Result<u64, SourceError> {
7373 let summary = issue
7374 .get("subIssuesSummary")
7375 .ok_or_else(|| SourceError::Malformed {
7376 message: "GitHub issue is missing subIssuesSummary".into(),
7377 })?;
7378 summary
7379 .get("total")
7380 .and_then(Value::as_u64)
7381 .ok_or_else(|| SourceError::Malformed {
7382 message: "GitHub issue subIssuesSummary.total is not an unsigned integer".into(),
7383 })
7384}
7385
7386/// One issue's own `number`.
7387///
7388/// An issue always has one: GitHub declares `Issue.number` as `Int!` and every selection of
7389/// an issue in this module asks for it. So a read of one that comes back without it, or
7390/// with something that is not an unsigned integer, is a response this source cannot read —
7391/// absence here is **not** "this issue has no number". A draft is the content that has
7392/// none, and a draft never reaches this: the caller decides on `__typename` first, the way
7393/// it does for `subIssuesSummary`, which `DraftIssue` equally declares nothing for.
7394fn issue_number(issue: &Value) -> Result<u64, SourceError> {
7395 issue
7396 .get("number")
7397 .and_then(Value::as_u64)
7398 .ok_or_else(|| SourceError::Malformed {
7399 message: "GitHub issue number is missing or is not an unsigned integer".into(),
7400 })
7401}
7402
7403/// The `number` a creating mutation answered with, and `None` when it answered without one;
7404/// why a missing one is tolerated is at the call in `create_and_file_issue`.
7405fn created_issue_number(created: &Value) -> Result<Option<u64>, SourceError> {
7406 match created.get("number") {
7407 None | Some(Value::Null) => Ok(None),
7408 Some(value) => value
7409 .as_u64()
7410 .map(Some)
7411 .ok_or_else(|| SourceError::Malformed {
7412 message: "GitHub created issue number is not an unsigned integer".into(),
7413 }),
7414 }
7415}
7416
7417fn required_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
7418 value
7419 .get(field)
7420 .and_then(Value::as_str)
7421 .ok_or_else(|| SourceError::Malformed {
7422 message: format!("GitHub response is missing string field {field}"),
7423 })
7424}
7425
7426fn required_nonblank_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
7427 let found = required_str(value, field)?;
7428 if found.trim().is_empty() {
7429 return Err(SourceError::Malformed {
7430 message: format!("GitHub response has blank string field {field}"),
7431 });
7432 }
7433 Ok(found)
7434}
7435
7436/// The slot's delimiters, which `docs/metadata.md` settles once for every source that
7437/// needs one — Linear spells them too, in its own description field.
7438///
7439/// Restated rather than shared, because a plugin crate depends on the contract crate and
7440/// nothing else of this workspace. `scripts/check-metadata-slot-encoding.sh`, a target in
7441/// `check`, is what keeps the two one encoding: drift is otherwise quiet, since each
7442/// source round-trips its own writes perfectly well under its own spelling.
7443const METADATA_OPEN: &str = "<!-- onetaskgraph.metadata\n";
7444const METADATA_CLOSE: &str = "\n-->";
7445
7446/// What the composer puts between a non-empty visible body and the slot, and the one thing
7447/// the parser takes off the visible body when it takes the slot off — exactly once, so every
7448/// other trailing byte of the body comes back as it was written.
7449// 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.
7450const METADATA_SEPARATOR: &str = "\n\n";
7451
7452/// The visible body and the metadata slot at the end of it.
7453///
7454/// The encoding is the one `docs/metadata.md` settles for Linear, which is where its
7455/// reasons are. Only a comment at the very end is a slot; one in the middle is a person's
7456/// own content and is left alone. The visible body is everything before the slot less the
7457/// one [`METADATA_SEPARATOR`] the composer put there, byte for byte.
7458fn metadata_body(
7459 body: Option<String>,
7460) -> Result<(Option<String>, BTreeMap<String, Value>), SourceError> {
7461 let Some(body) = body else {
7462 return Ok((None, BTreeMap::new()));
7463 };
7464 let Some(slot) = slot_span(&body)? else {
7465 return Ok((Some(body), BTreeMap::new()));
7466 };
7467 let metadata =
7468 serde_json::from_str(&body[slot.encoded_start..slot.encoded_end]).map_err(|error| {
7469 SourceError::Malformed {
7470 message: format!(
7471 "invalid canonical JSON in GitHub issue onetaskgraph metadata slot: {error}"
7472 ),
7473 }
7474 })?;
7475 let before = &body[..slot.start];
7476 let visible = before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before);
7477 Ok(((!visible.is_empty()).then(|| visible.to_owned()), metadata))
7478}
7479
7480/// Where the metadata slot sits in one body, as byte offsets into it.
7481struct SlotSpan {
7482 /// Where [`METADATA_OPEN`] begins.
7483 start: usize,
7484 /// Where the encoded JSON begins, just past [`METADATA_OPEN`].
7485 encoded_start: usize,
7486 /// Where the encoded JSON ends, at the start of [`METADATA_CLOSE`].
7487 encoded_end: usize,
7488 /// Just past [`METADATA_CLOSE`].
7489 end: usize,
7490}
7491
7492/// The slot at the very end of `body`, or `None` when it has none.
7493///
7494/// The one reading of *where the slot is*, shared by [`metadata_body`], which reads it, and
7495/// [`with_slot`], which rewrites it — so the two cannot disagree about which comment is the
7496/// slot.
7497fn slot_span(body: &str) -> Result<Option<SlotSpan>, SourceError> {
7498 let Some(start) = body.rfind(METADATA_OPEN) else {
7499 return Ok(None);
7500 };
7501 let encoded_start = start + METADATA_OPEN.len();
7502 let Some(relative_end) = body[encoded_start..].find(METADATA_CLOSE) else {
7503 return Err(SourceError::Malformed {
7504 message: "unterminated onetaskgraph metadata slot in GitHub issue body".into(),
7505 });
7506 };
7507 let encoded_end = encoded_start + relative_end;
7508 let end = encoded_end + METADATA_CLOSE.len();
7509 if !body[end..].trim().is_empty() {
7510 return Ok(None);
7511 }
7512 Ok(Some(SlotSpan {
7513 start,
7514 encoded_start,
7515 encoded_end,
7516 end,
7517 }))
7518}
7519
7520/// `body` with its metadata slot holding exactly `metadata`, and every byte outside the
7521/// slot as it was.
7522///
7523/// A slot that is there has its JSON replaced in place; one that becomes empty is removed
7524/// together with the one [`METADATA_SEPARATOR`] separating it from the prose before it. A
7525/// body with no slot gains one the way [`compose_body`] writes it — after that separator,
7526/// or alone in an empty body — and a body with no slot that is given no metadata is
7527/// returned as it is.
7528fn with_slot(body: &str, metadata: &BTreeMap<String, Value>) -> Result<String, SourceError> {
7529 let encoded = if metadata.is_empty() {
7530 None
7531 } else {
7532 Some(
7533 serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
7534 message: error.to_string(),
7535 })?,
7536 )
7537 };
7538 Ok(match (slot_span(body)?, encoded) {
7539 (Some(slot), Some(encoded)) => format!(
7540 "{}{encoded}{}",
7541 &body[..slot.encoded_start],
7542 &body[slot.encoded_end..]
7543 ),
7544 (Some(slot), None) => {
7545 let before = &body[..slot.start];
7546 format!(
7547 "{}{}",
7548 before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before),
7549 &body[slot.end..]
7550 )
7551 }
7552 (None, None) => body.to_owned(),
7553 (None, Some(encoded)) if body.is_empty() => {
7554 format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
7555 }
7556 (None, Some(encoded)) => {
7557 format!("{body}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
7558 }
7559 })
7560}
7561
7562/// `body` with everything before its metadata slot replaced by `content`, and the slot
7563/// itself kept byte for byte.
7564///
7565/// The inverse of how [`metadata_body`] splits a body: the slot, when there is one, follows
7566/// `content` after the one [`METADATA_SEPARATOR`] the composer puts there — or alone, when
7567/// `content` is empty — so a read of the result reports `content` as the visible body and
7568/// the slot's metadata exactly as it was.
7569fn with_content(body: &str, content: &str) -> Result<String, SourceError> {
7570 let Some(slot) = slot_span(body)? else {
7571 return Ok(content.to_owned());
7572 };
7573 let kept = &body[slot.start..];
7574 Ok(if content.is_empty() {
7575 kept.to_owned()
7576 } else {
7577 format!("{content}{METADATA_SEPARATOR}{kept}")
7578 })
7579}
7580
7581/// Hold `entries` under `key` in one slot's metadata, or no such key when there are none.
7582fn set_task_list(metadata: &mut BTreeMap<String, Value>, key: &str, entries: &[TaskRef]) {
7583 if entries.is_empty() {
7584 metadata.remove(key);
7585 } else {
7586 metadata.insert(
7587 key.to_owned(),
7588 Value::Array(
7589 entries
7590 .iter()
7591 .map(|entry| Value::String(entry.as_str().to_owned()))
7592 .collect(),
7593 ),
7594 );
7595 }
7596}
7597
7598fn compose_body(
7599 content: Option<&str>,
7600 metadata: &BTreeMap<String, Value>,
7601) -> Result<Option<String>, SourceError> {
7602 let visible = content.unwrap_or_default();
7603 if metadata.is_empty() {
7604 return Ok((!visible.is_empty()).then(|| visible.to_owned()));
7605 }
7606 let encoded = serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
7607 message: error.to_string(),
7608 })?;
7609 Ok(Some(if visible.is_empty() {
7610 format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
7611 } else {
7612 format!("{visible}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
7613 }))
7614}
7615
7616fn required_bool(value: &Value, field: &str) -> Result<bool, SourceError> {
7617 value
7618 .get(field)
7619 .and_then(Value::as_bool)
7620 .ok_or_else(|| SourceError::Malformed {
7621 message: format!("GitHub response is missing boolean field {field}"),
7622 })
7623}
7624fn optional_str<'a>(value: &'a Value, field: &str) -> Result<Option<&'a str>, SourceError> {
7625 match value.get(field) {
7626 None | Some(Value::Null) => Ok(None),
7627 Some(value) => value
7628 .as_str()
7629 .map(Some)
7630 .ok_or_else(|| SourceError::Malformed {
7631 message: format!("GitHub response field {field} is not a string or null"),
7632 }),
7633 }
7634}
7635fn optional_nodes<'a>(
7636 connection: Option<&'a Value>,
7637 name: &str,
7638) -> Result<Option<&'a Vec<Value>>, SourceError> {
7639 match connection {
7640 None | Some(Value::Null) => Ok(None),
7641 Some(value) => value
7642 .get("nodes")
7643 .and_then(Value::as_array)
7644 .map(Some)
7645 .ok_or_else(|| SourceError::Malformed {
7646 message: format!("GitHub {name}.nodes is not an array"),
7647 }),
7648 }
7649}
7650fn complete_connection(connection: &Value, name: &str, size: u32) -> Result<(), SourceError> {
7651 let page_info = connection
7652 .get("pageInfo")
7653 .ok_or_else(|| SourceError::Malformed {
7654 message: format!("GitHub {name} has no pageInfo"),
7655 })?;
7656 if required_bool(page_info, "hasNextPage")? {
7657 return Err(SourceError::Malformed {
7658 message: format!(
7659 "GitHub {name} exceeds the supported nested connection size of {size}"
7660 ),
7661 });
7662 }
7663 Ok(())
7664}
7665fn optional_time(value: &Value, field: &str) -> Result<Option<DateTime<Utc>>, SourceError> {
7666 optional_str(value, field)?
7667 .map(|timestamp| {
7668 timestamp.parse().map_err(|error| SourceError::Malformed {
7669 message: format!("GitHub response field {field} is not a timestamp: {error}"),
7670 })
7671 })
7672 .transpose()
7673}
7674fn validate_page(page: &PageRequest) -> Result<(), SourceError> {
7675 if page.limit == 0 {
7676 Err(SourceError::Config {
7677 message: "page limit must be at least 1".into(),
7678 })
7679 } else {
7680 Ok(())
7681 }
7682}
7683fn next_cursor(connection: &Value) -> Result<Option<Cursor>, SourceError> {
7684 let page = connection
7685 .get("pageInfo")
7686 .filter(|value| value.is_object())
7687 .ok_or_else(|| SourceError::Malformed {
7688 message: "GitHub connection is missing pageInfo".into(),
7689 })?;
7690 if required_bool(page, "hasNextPage")? {
7691 let cursor = required_str(page, "endCursor")?;
7692 validate_cursor_progress(None, cursor)?;
7693 Ok(Some(Cursor(cursor.into())))
7694 } else {
7695 Ok(None)
7696 }
7697}
7698fn validate_cursor_progress(previous: Option<&str>, next: &str) -> Result<(), SourceError> {
7699 if next.is_empty() || previous == Some(next) {
7700 Err(SourceError::Malformed {
7701 message: "GitHub pagination cursor is empty or did not advance".into(),
7702 })
7703 } else {
7704 Ok(())
7705 }
7706}
7707fn numeric_cursor(cursor: Option<&Cursor>) -> Result<usize, SourceError> {
7708 cursor.map_or(Ok(0), |c| {
7709 c.0.parse().map_err(|_| SourceError::Config {
7710 message: "page cursor is invalid".into(),
7711 })
7712 })
7713}
7714fn offset_page<T>(mut items: Vec<T>, offset: usize, limit: usize) -> Page<T> {
7715 if offset > items.len() {
7716 return Page::last(vec![]);
7717 }
7718 let tail = items.split_off(offset);
7719 let mut selected = tail;
7720 let next = (selected.len() > limit).then(|| Cursor((offset + limit).to_string()));
7721 selected.truncate(limit);
7722 Page {
7723 items: selected,
7724 next,
7725 }
7726}