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 [`GitHubProjectsConfig::repository`] names the one repository this
11//! source creates its project and task issues in; a write without it is refused naming
12//! the field.
13//!
14//! **A document is an ordinary issue whose title begins [`DESIGN_TITLE_PREFIX`].** A
15//! board has no document type and nothing but issues to hold one in, so the title is the
16//! discriminator and it is the whole of it. The title this source *reports* is the one a
17//! person wrote, with the prefix taken off — the same way the metadata slot is taken off
18//! the body so `content` is what the person wrote — and writing a document puts the prefix
19//! back, so a round trip returns the title that went in.
20//!
21//! **Telling a document from a project from a task.** The design prefix is read **first**:
22//! a document is never a project and never a task, whatever sub-issues it has or does not
23//! have. Only then does the rest apply — a board issue is a project when *either* it has
24//! sub-issues *or* it carries [`ItemKind::METADATA_KEY`]; otherwise it is a task. A
25//! sub-issue is always a task, whatever it carries. The marker is sufficient and never
26//! necessary: it is what makes an *empty* project — the state a project copy passes
27//! through between creating the project and filing its first task — readable as a
28//! project, while the sub-issue arm lets a person author a project on the board by hand
29//! with no knowledge of this product's metadata at all. Reading the prefix later than the
30//! sub-issue rule would make a design issue with no sub-issues an empty project, which is
31//! exactly the state that rule exists to catch. Pull requests are neither a project nor a
32//! task nor a document and are ignored.
33//!
34//! **Where an entity is, is a link.** Every project, task and document this source reports
35//! carries a [`Location::Url`] naming the issue's own web address — the same address the
36//! `url` field already reports, in the shape that says a reader can open it. That is the
37//! contrast the location contract exists for: a reader holding an entity from this source
38//! is handed something to link to and one holding an entity from a folder of Markdown is
39//! handed a path, and neither has to know which plugin answered. It does not replace or
40//! derive from `url`; that field goes on reporting what it always reported.
41//!
42//! **Where metadata lives.** Short typed things go to typed fields and native relations:
43//! status to the board's `Status` single-select and the issue's own state, the copy
44//! origin to a source-owned `onetaskgraph.origin` text field, and dependencies to
45//! `blockedBy` and to sub-issue links. Unbounded caller JSON goes in a trailing
46//! `<!-- onetaskgraph.metadata ... -->` comment at the end of the issue body — the same
47//! encoding `docs/metadata.md` settles for Linear, not a second one. A ProjectV2 text
48//! field is length-bounded and `shortDescription` is capped at 300 characters, which is
49//! why neither can hold a caller's own prose.
50//!
51//! **Status.** `status_mapping` is per-instance configuration from a status category to
52//! `null`, a board `Status` option name, or a closed state of `completed` or
53//! `not-planned`. Nothing here ever calls `updateProjectV2Field`: that mutation's
54//! `singleSelectOptions` *overwrites* a field's option set, so no addition is additive
55//! and a mistake destroys every item's status. A status this board cannot represent is a
56//! refusal naming the status and the instance instead.
57//!
58//! `done` closes the issue by default because GitHub derives `subIssuesSummary.completed`
59//! and the board's own `Sub-issues progress` field from closed sub-issues: a plan whose
60//! finished tasks were only moved to a "Done" column would read 0% complete forever.
61//!
62//! # What this source declares, field by field
63//!
64//! One verdict per field of [`Capabilities`], and what `Native` means when this source
65//! says it. *Proven* means a shared journey drives it against the real
66//! binary over this source's own row in `crates/onetaskgraph/tests/e2e/fixtures.rs`, and
67//! `every_row_declares_exactly_what_its_plugin_reports` is what keeps this list and
68//! [`capabilities`](TaskSource::capabilities) from parting.
69//!
70//! | Field | Verdict |
71//! | --- | --- |
72//! | `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. |
73//! | `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. |
74//! | `orphan_tasks` | **Supported and proven.** A task issue with no `parent` is in no project. |
75//! | `filter_by_label` | **Supported and proven,** over the issue's own labels. |
76//! | `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`. |
77//! | `search_title` | **Supported and proven,** over `Issue.title`. |
78//! | `search_content` | **Supported and proven,** over the visible body — the trailing metadata comment is not part of it. |
79//! | `task_dependencies` | **Supported and proven,** in both directions: `blockedBy` and `blocking`. |
80//! | `project_dependencies` | **Supported and proven,** in both directions, over the same two connections, because a project here is an issue. |
81//! | `max_page_size` | **Supported and proven.** [`MAX_PAGE_SIZE`], GitHub's own connection maximum. |
82//!
83//! Nothing here is unsupported. `documents` is not a predicate — it says this source has
84//! documents, which it does — and the three facts behind the uniform `Native` on the
85//! predicates beside it are recorded below rather than re-derived, because a reader who
86//! takes `Native` to mean *the remote service filters* will read that uniformity as a
87//! lie.
88//!
89//! First, the plugin contract defines `Support::Native` as *the source applies this
90//! predicate itself*, and says nothing about where it applies it. What the declaration
91//! promises the engine is capability rule 1 — a predicate declared `Native` **is** applied
92//! — so that the engine may push it down and apply nothing of its own.
93//!
94//! Second, this source can keep that promise for every predicate at no additional API
95//! cost, because whichever of the three reads below answers a query has already read every
96//! item that query could keep before it filters anything. Filtering those items is
97//! in-process work over data already in hand.
98//!
99//! Third, no predicate but `projects` could be pushed into the API even if that were
100//! wanted, and `projects` is pushed down: `ProjectV2.items` takes `first` and `after` and
101//! offers no filter argument of any kind, GitHub's issue search offers no qualifier for a
102//! label set, a status column or a substring of a body, and its title qualifier matches
103//! tokens where this source — and the local Markdown source beside it — match substrings,
104//! so pushing a search down would silently *narrow* the answer. What a project filter has
105//! instead is a relationship: a project's tasks are that issue's sub-issues, and asking
106//! the issue for them is both cheaper and exact. So there is one predicate this source
107//! applies by asking a narrower question, six it applies in process, and none it is unable
108//! to apply. Declaring one `Unsupported` would make the engine compensate for work this
109//! source has already done, and declaring `projects` native while ignoring the filter
110//! (which this source once did) silently returns another project's tasks, because the
111//! engine trusts the declaration and applies nothing locally.
112//!
113//! # The three ways this source reaches an item, and what each costs
114//!
115//! A board read is charged for what its *nested* connections could return rather than for
116//! what was asked, so one whole-board read costs the same whether the question was about
117//! one project or about all of them. That is why a question about one project is never
118//! answered by reading the board:
119//!
120//! | The question | What is sent | What it costs |
121//! | --- | --- | --- |
122//! | one item, by its own id | [`graphql::ISSUE`] — `node(id:)` | the item |
123//! | one project's tasks or documents | [`graphql::SUB_ISSUES`] — that issue's own `subIssues` | that project |
124//! | which projects this board holds | [`graphql::SEARCH_ISSUES`] — an issue search scoped to the board | the board's issues, without their board items |
125//! | every task, every document, every label | [`graphql::BOARD`] — the board's own `items` | the board |
126//! | 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 |
127//!
128//! The board half of an issue — its board item's id, its `Status` option and this
129//! source's origin text field — rides along on `Issue.projectItems` in the first three, so
130//! an item reached any of those ways resolves through the same
131//! [`GitHubProjectsSource::resolve`] the board walk uses and reports the same title, the
132//! same status, the same labels and the same qualified id. That connection comes back a
133//! *page* at a time, at `BOARD_ITEMS_PAGE_SIZE`, so the entry for this board is looked for
134//! on the page in hand and — only if that page reports more of the connection — in the
135//! last row's read of that one issue's memberships, resumed from the page's own cursor and
136//! walked to exhaustion. An issue with no entry for *this* board is not this source's to
137//! report, which is what keeps an id naming another repository's issue from being answered
138//! as an item of this board; and because the page is where the search starts rather than
139//! where it ends, that answer is one about a connection read to exhaustion and never about
140//! an unread page. Nothing costs the extra read but an issue on more boards than a page
141//! holds: an issue this board really does not hold reports no next page, so its
142//! memberships are already exhausted where they arrived.
143//!
144//! **No document here selects the board's own `Labels` field, and nothing is lost by
145//! that.** An item's labels are read from its content alone, wherever that content is
146//! reached: the three documents above select `Issue.labels` on the fragment, and
147//! [`graphql::BOARD`] selects the same connection on the `... on Issue` arm of its
148//! `content`. A board's `Labels` field is not one anybody fills in: it is a built-in
149//! `ProjectV2FieldType`, it is absent from `ProjectV2CustomFieldType` so no project can
150//! create one, and `ProjectV2FieldValue` — the whole of what
151//! `updateProjectV2ItemFieldValue` accepts — offers no way to write one. So GitHub derives
152//! it from the content, for every content type it exists on, and there is nothing it can
153//! hold that the content does not already say: for an `Issue` it *is* that issue's own
154//! labels, so selecting it beside them unions a set with itself.
155//!
156//! **A draft loses nothing by that either**, which is the reasoning this paragraph once had
157//! backwards. `DraftIssue` exposes no `labels` field, and by the three schema facts above
158//! it cannot carry a board `Labels` value to be derived from one — so a draft has nothing
159//! to select *and nothing to lose*, and reports no labels at all. A `PullRequest` item is
160//! discarded by [`GitHubProjectsSource::resolve`] before labels are read. Both halves are
161//! held to that by tests in `tests/plugin.rs`: the four ways an item is reached report one
162//! label set, and that set is the fixture issue's own, by
163//! `an_item_reports_the_same_labels_title_status_and_id_however_it_is_reached`; and a board
164//! item whose content is a draft reports an empty set, by
165//! `a_board_item_whose_content_is_a_draft_reports_no_labels_at_all`. The absence of the
166//! selection is held over [`graphql::DOCUMENTS`] by
167//! `no_document_selects_the_boards_own_labels_field`.
168//!
169//! The last row is still the board's own item connection, and deliberately: a **draft**
170//! board item is not an issue, so no search and no node read can reach one, and the reads
171//! that have to answer for the whole board are the ones whose cost is the board's size
172//! anyway.
173//!
174//! **What a read may return is capped too, and that cap is on the document rather than on
175//! the board.** GitHub limits the number of nodes **one query may return** to
176//! [`NODE_COUNT_LIMIT`] and refuses a query above that before executing it: the answer is
177//! an error naming the connection the count crossed at, not a slow or a partial result.
178//! Every board this source reads is refused the same way, so no board is too big for these
179//! documents and none is small enough to save one that is over.
180//!
181//! The count is arithmetic over the document's own text: each connection contributes the
182//! `first:` it asks for, counts **multiply** down a nested path and **sum** across sibling
183//! paths. Those are [GitHub's published rules][node-limits] and this workspace does not
184//! restate them — `github-graphql-node-count` implements them, and
185//! [`worst_case_node_count`] under [`largest_page_sizes`] is where every node count here
186//! comes from. `every_document_this_source_sends_stays_under_githubs_node_limit`, in
187//! `tests/node_count.rs`, recomputes every document in [`graphql::DOCUMENTS`] from that
188//! same text on every run and fails naming any that reaches the limit — so a connection
189//! added to a shared fragment is caught there rather than by GitHub.
190//!
191//! What decides those counts is the page sizes: [`MAX_PAGE_SIZE`] on the outer page,
192//! `NESTED_PAGE_SIZE` on the connections hanging off one item, and
193//! `BOARD_ITEMS_PAGE_SIZE` on the page of an issue's board memberships a read carries.
194//! `$nestedFirst` is spent twice down one path of a board read, so that constant is
195//! effectively squared there, which is why it is the one the limit is most sensitive to.
196//! `BOARD_ITEMS_PAGE_SIZE` is small for a reason of its own, recorded beside it: what a
197//! page of memberships misses is recovered by one further read rather than refused, so it
198//! buys a bound every read pays for at the price of a request only a multi-board issue
199//! pays.
200//!
201//! **`nodeCount` and `cost` are two numbers against two limits, and both are computed
202//! offline here — per document, one document at a time.** `nodeCount` is the one above: the
203//! most nodes one query may return, checked per query and bounded by [`NODE_COUNT_LIMIT`].
204//! `cost` is rate-limit points, metered per hour across everything one credential does; it
205//! is what the two limiters [`Limiter`] tells apart meter, and a document under
206//! [`NODE_COUNT_LIMIT`] still says nothing about its price. [`worst_case_point_cost`] is
207//! that second number, and `tests/point_cost.rs` pins every document in
208//! [`graphql::DOCUMENTS`] at what it costs — there being no per-call point ceiling to hold
209//! one under, the pin itself is the check. The credentialed lane reconciles both figures
210//! against GitHub's own, off a probe it already sends.
211//!
212//! **What is pinned that way is a per-document price and never a session's.** The record in
213//! `session-cost.md` measures the two quantities a whole session can be counted in offline —
214//! **requests** and **worst-case nodes** — and neither is points. What one whole session
215//! consumes of the hourly point allowance is observable only from a credentialed run's own
216//! `x-ratelimit-*` headers, which is what [`accounting`] fills its per-budget figures from
217//! and what `tests/live.rs` prints at the end of every run.
218//!
219//! [node-limits]: https://docs.github.com/en/graphql/overview/rate-limits-and-node-limits-for-the-graphql-api
220//!
221//! **Where a read-after-write guarantee comes from, since a search index cannot supply
222//! one.** GitHub's issue search is eventually consistent and answers a write made moments
223//! ago with the value from before it. Resolving a node id is not, so a read by id and a
224//! project's own sub-issues are already current. What closes the gap for the search is
225//! [`GitHubProjectsSource::created`]: every read this source answers is completed with
226//! what this process itself wrote, so an item created seconds ago is reported whether or
227//! not GitHub's index has caught up. Nothing else is remembered, nothing is written down,
228//! and the record dies with the process.
229//!
230//! Filtering happens before paging, so a page of a filtered result is a page of the
231//! survivors rather than the survivors of a page. Label and text matching answer the same
232//! question the same way the local Markdown source's do, so one cross-source expectation
233//! holds for both.
234//!
235//! <!-- llmlint: ignore[contracts_have_one_source_or_a_drift_gate] The declaration itself
236//! has one source, `capabilities`, and the note above is the reasoning behind it rather
237//! than a second copy of it: without the three facts recorded here a reader takes the
238//! uniform `Native` for a lie and reverts it. The drift gate on the declaration is this
239//! crate's own capabilities test, which pins every field of it against a fully spelled-out
240//! `Capabilities` literal — a struct with no `Default`, so a field added to the contract
241//! fails to compile there rather than going unasserted. -->
242//! The fixture-server tests above run wherever this crate is selected; the credentialed
243//! lane runs in the same required check, beside them, and can fail it — it verifies the
244//! 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,
245//! one filed under neither, a label on one of the three and a closed status on another —
246//! because that shape is what tells an honoured predicate from an ignored one: a board
247//! holding a single project answers a project filter the same way whether or not this
248//! source applies it, which is exactly how the defect above went unseen.
249//!
250//! That lane writes only to the board `GH_PROJECTS_OWNER` and `GH_PROJECTS_NUMBER` name,
251//! and only into the repository `GH_PROJECTS_REPOSITORY` names, and skips — as it does
252//! without `GH_PROJECTS_TOKEN` — when any of them is absent. Requiring both to be
253//! nominated is what keeps a credentialed write lane off a board and a repository nobody
254//! nominated; it never asks GitHub which project was updated most recently. Before it
255//! starts, the lane also clears any item titled — and any repository label named — the way
256//! it titles and names its own artifacts, which is self-healing after an interrupted run:
257//! a process killed between its writes and its cleanup leaves artifacts the next run
258//! removes.
259//!
260//! # What a session of requests costs, and where the report is
261//!
262//! This source records **every** request it sends into [`accounting::Accounting`], at
263//! `send_once` — the one place a request leaves this crate, which is why a read path added
264//! later is counted without anybody remembering to count it. That is the whole of what this
265//! crate adds to the arrangement; [`accounting`] is where what a record carries, how a
266//! session's spend is arrived at, and what it deliberately does not know are set out.
267//!
268//! What one whole session of the live journey costs, counted that way against this crate's
269//! loopback fixture board, is written down in `session-cost.md` beside this crate — with the
270//! reduction it came out of, and with what it does and does not say about rate-limit points.
271//!
272//! [`GitHubProjectsSource::accounting`] is the read: a snapshot to hold and compare, which
273//! [`accounting::Session::report`] renders the session report from. It is on the ordinary
274//! code path — no environment variable, no feature, no build configuration — because an
275//! instrument nobody switches on measures nothing, and
276//! [`Plugin::build_recording_into`] is how a caller making its own calls beside this
277//! source's counts the whole session rather than this source's share. The credentialed lane
278//! in `tests/live.rs` does exactly that, and prints the report at the end of every run,
279//! passed or failed.
280//!
281//! **A live session refuses to start unless the account can afford it.** Before it does any
282//! of the work it exists to do, the journey makes one request — `GET /rate_limit`, which
283//! GitHub documents as not counting against the REST rate limit and which answers both of
284//! its budgets at once — and starts only if, for each of them, what remains minus this
285//! session's estimated cost is still at least
286//! `onetaskgraph_live::RETAINED_BUFFER` — twenty per cent — of that budget's whole
287//! allowance. A session that cannot **declines**: it did not run, so it is
288//! neither a pass nor a failing assertion, and it says which budget was short, that budget's
289//! limit, what remained, the estimate, the buffer and when it resets — then stops, without
290//! waiting for the budget to come back. The estimate is derived offline from
291//! `tests/fixtures/session-cost.txt` and a cost model stated in `tests/journey/budget.rs`,
292//! which is also where the published rule that model rests on is cited; the accounting
293//! above records the gate's own read like any other request, and
294//! [`accounting::Session::report`] prints the estimate beside what the session really spent.
295//!
296//! **GitHub is the authority on both of its own numbers, and the credentialed lane goes and
297//! asks it.** Everything above computes `nodeCount` and `cost` offline from a document's own
298//! text, which is what lets it run on every platform and on a pull request from a fork with
299//! no credential — and that is what actually stops a regression merging. But an offline
300//! arithmetic can only ever agree with itself: if GitHub changes its rules, this workspace
301//! goes on computing the old answer and nothing notices. So `tests/live.rs` reconciles them.
302//! GitHub's schema exposes `rateLimit(dryRun: true)`, whose `nodeCount` is *"the maximum
303//! number of nodes this query may return"* and whose `cost` is what that document would
304//! spend, both for a document **without executing it**, and the lane asks it for every query
305//! document this source sends, under the largest bindings this source sends, and fails when
306//! GitHub's figure and [`worst_case_node_count`] or [`worst_case_point_cost`] disagree. A
307//! mutation is skipped, because `rateLimit` is a field of `Query` and cannot be asked about
308//! one; the offline pins still cover it. It records what those calls reported about the
309//! account's own allowance, because whether asking is free is a thing to observe rather than
310//! to assume. Two quantities, not one: [`NODE_COUNT_LIMIT`] bounds `nodeCount` per query,
311//! and `cost` is metered against an hourly allowance the accounting above reads off a
312//! credentialed run's own response headers.
313//!
314//! **GitHub has two rate limiters and this source is refused by both, so nothing here
315//! treats them as one thing.** The primary budget is the hourly allowance `gh api
316//! rate_limit` reports; the secondary limiter is a burst limiter over content-generating
317//! requests, and *nothing* reports it. Which one refused decides the operator's next step,
318//! so [`Limiter`] is a type rather than a detail, and it is what [`MIN_MUTATION_INTERVAL_MS`],
319//! [`GitHubProjectsSource::board_cache`] and [`GitHubProjectsSource::graphql`] each answer
320//! one part of.
321#![deny(missing_docs)]
322
323use std::collections::BTreeMap;
324use std::sync::{Arc, Mutex};
325use std::time::{Duration, Instant};
326
327use chrono::{DateTime, Utc};
328use onetaskgraph_plugin_api::{
329 Capabilities, Cursor, DependencyEdge, DependencyEndpoint, DependencyKind, DependencySupport,
330 Direction, Document, DocumentQuery, Health, ItemKind, ItemWrite, Label, LabelFilter, Location,
331 NativeId, Page, PageRequest, Project, ProjectFilter, ProjectQuery, Repository, SecretResolver,
332 SourceError, SourceName, SourcePlugin, Status, StatusCategory, Support, Task, TaskQuery,
333 TaskSource, TextFields, TextQuery, WriteSupport,
334};
335use reqwest::{Client, StatusCode, Url};
336use schemars::{Schema, schema_for};
337use secrecy::{ExposeSecret, SecretString};
338use serde::Deserialize;
339use serde_json::{Value, json};
340
341pub mod accounting;
342
343use accounting::Accounting;
344
345/// The registry name for this plugin.
346pub const KIND: &str = "github-projects";
347/// GitHub's maximum connection page size.
348pub const MAX_PAGE_SIZE: u32 = 100;
349
350/// The most nodes any one document this source sends may be asked to return.
351///
352/// GitHub's own published per-query ceiling, taken from
353/// [`github_graphql_node_count::NODE_LIMIT`] rather than written out again here, so this
354/// workspace cannot hold a stale copy of somebody else's number. A query above it is
355/// **refused before it is executed**, whoever is asking and whatever board they are
356/// asking about — so this is a bound on the documents rather than a budget that runs out.
357///
358/// This is `nodeCount`, the maximum number of nodes *one query may return*. It is not
359/// `cost`, the rate-limit points a call spends against an hourly allowance shared by
360/// everything the credential does — two numbers against two limits, and this constant
361/// bounds only the first. The second is computed offline too, per document:
362/// [`worst_case_point_cost`], pinned for every document in [`graphql::DOCUMENTS`] by
363/// `tests/point_cost.rs`, and reconciled against GitHub's own `cost` by the credentialed
364/// lane. There is no constant like this one to hold a price under, because points are an
365/// hourly allowance rather than a per-call bound.
366///
367/// Neither is a session's price. What `session-cost.md` records of a whole session is its
368/// **requests** and its **worst-case nodes**; what a whole session spends in points is
369/// reported only by a credentialed run's own `x-ratelimit-*` headers, through
370/// [`accounting`]. The module section on the three ways this source reaches an item says how
371/// the count is arrived at, and which of the page sizes below decide it.
372pub const NODE_COUNT_LIMIT: u64 = github_graphql_node_count::NODE_LIMIT;
373
374/// Nested connection size for the connections that hang off one item.
375///
376/// It multiplies through every document that reaches an item under a page — the count
377/// rules multiply down a nested path — so it is the constant [`NODE_COUNT_LIMIT`] is most
378/// sensitive to. `tests/node_count.rs` is what holds the pair together: it recomputes
379/// every document under these constants and fails naming any that reaches the limit, so
380/// raising this is caught there rather than by GitHub.
381const NESTED_PAGE_SIZE: u32 = 50;
382/// How many of one issue's board memberships are read when an issue is reached directly.
383///
384/// An issue reached through a search or through its own node id carries its board half in
385/// `Issue.projectItems`, and only the entry for *this* board is read. This connection sits
386/// under a page of issues, so every point of it multiplies through the whole document and
387/// is paid for whether or not any issue is on a second board — which is why it is
388/// deliberately far smaller than [`NESTED_PAGE_SIZE`].
389///
390/// **Three, because what a page misses is now recovered rather than refused**, and the
391/// recovery is what the value is chosen against. An issue whose entry for this board sits
392/// past this page costs one further request — [`graphql::ISSUE_BOARD_ITEMS`], resumed from
393/// that page's own cursor — so the value trades a bound every read pays for a request only
394/// a multi-board issue pays. At one, a deployment whose issues commonly sit on two or more
395/// boards would pay that request *per issue*, which is order N against the one page per
396/// hundred issues a read costs today. At three it is only reached by an issue on four or
397/// more boards at once, which keeps the recovery path exceptional rather than routine for
398/// a plausible deployment.
399const BOARD_ITEMS_PAGE_SIZE: u32 = 3;
400
401pub use github_graphql_node_count::{NodeCountError, Variables};
402
403/// The largest value this source can bind to each page-size variable its documents name.
404///
405/// Every `first:` in [`graphql`] reads one of these three, and each is capped at the
406/// constant above it wherever a caller's own limit could reach it — `$first` at
407/// [`MAX_PAGE_SIZE`], `$nestedFirst` at `NESTED_PAGE_SIZE`, `$boardItems` at
408/// `BOARD_ITEMS_PAGE_SIZE`. So this is the worst case a caller can drive this source to,
409/// not one configuration of it, which is what makes a bound computed under it a bound on
410/// every read.
411pub fn largest_page_sizes() -> Variables {
412 Variables::from([
413 ("first".to_owned(), MAX_PAGE_SIZE),
414 ("nestedFirst".to_owned(), NESTED_PAGE_SIZE),
415 ("boardItems".to_owned(), BOARD_ITEMS_PAGE_SIZE),
416 ])
417}
418
419/// The most nodes `document` could be asked to return, by GitHub's published rules.
420///
421/// Computed offline from the document's own text under [`largest_page_sizes`] — no
422/// network, no credential and no schema — by
423/// [`github_graphql_node_count::node_count`], which is where the rules themselves live.
424/// A document at or above [`NODE_COUNT_LIMIT`] is one GitHub refuses before executing, so
425/// this is what a check holds every document in [`graphql::DOCUMENTS`] below.
426///
427/// # Errors
428///
429/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
430/// no single operation, or binds a page size this source does not name — each of which is
431/// a defect in the document rather than a number.
432pub fn worst_case_node_count(document: &str) -> Result<u64, NodeCountError> {
433 node_count(document, &largest_page_sizes())
434}
435
436/// The most rate-limit points one call of `document` could spend, by GitHub's published
437/// rules.
438///
439/// Computed offline from the document's own text under [`largest_page_sizes`] — no
440/// network, no credential and no schema — by
441/// [`github_graphql_node_count::point_cost`], which is where the rules themselves live.
442/// This is `cost`, metered **per hour** against the allowance one credential shares across
443/// everything it does; it is not `nodeCount`, which is [`worst_case_node_count`] and is
444/// bounded per query by [`NODE_COUNT_LIMIT`]. There is no per-call ceiling to hold this
445/// under, so what `tests/point_cost.rs` does with it is pin every document in
446/// [`graphql::DOCUMENTS`] at what it costs, and the credentialed lane reconciles those
447/// figures against GitHub's own reported `cost`.
448///
449/// # Errors
450///
451/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
452/// no single operation, or binds a page size this source does not name — each of which is
453/// a defect in the document rather than a number.
454pub fn worst_case_point_cost(document: &str) -> Result<u64, NodeCountError> {
455 github_graphql_node_count::point_cost(document, &largest_page_sizes())
456}
457
458/// The most nodes `document` could be asked to return under `variables`.
459///
460/// [`worst_case_node_count`] is this under [`largest_page_sizes`], and the accounting in
461/// [`accounting`] is this under the bindings one request really sent — one spelling of the
462/// calculation, so a bound checked offline and a cost recorded at run time cannot come to
463/// disagree. The rules themselves live in [`github_graphql_node_count::node_count`].
464///
465/// # Errors
466///
467/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
468/// no single operation, or binds a page size `variables` does not name.
469pub fn node_count(document: &str, variables: &Variables) -> Result<u64, NodeCountError> {
470 github_graphql_node_count::node_count(document, variables)
471}
472
473/// The issue-title prefix that makes a board issue a document.
474///
475/// A GitHub Projects board has no document type — it holds issues — so the discriminator
476/// is the title, and this is the whole of it: an issue whose title begins with these bytes
477/// is a document and every other issue is the task or project the sub-issue rule makes it.
478///
479/// It is spelled **once**, here, and read rather than restated everywhere else — including
480/// by the shared journeys, which take it from this constant so a board fixture cannot
481/// drift from what this source reads. `docs/metadata.md` records the two consequences that
482/// are not obvious from the bytes: the reported title has this prefix taken off, exactly
483/// as the body's metadata slot is taken off `content`, and this prefix is read *before*
484/// the sub-issue rule, so a design issue with no sub-issues is never an empty project.
485pub const DESIGN_TITLE_PREFIX: &str = "DESIGN: ";
486
487/// Exact GraphQL query documents issued by this plugin.
488///
489/// Keeping the production documents here lets the pinned-schema test validate the same
490/// bytes that are sent to GitHub, rather than a test-only copy which could drift
491/// independently. No document in this module writes the board itself, and none of them
492/// names `updateProjectV2Field`.
493pub mod graphql {
494 /// The board half of one item: the field values every document here reads it from.
495 ///
496 /// A macro for the same reason [`board_issue!`] below is one, a level further in. This
497 /// selection is needed by that fragment, by [`BOARD`] under the board's own `items`,
498 /// and by [`ISSUE_BOARD_ITEMS`] under a membership walk — and all three have to produce
499 /// *the same value*, because
500 /// [`GitHubProjectsSource::resolve`](super::GitHubProjectsSource) reads them through
501 /// one path. Three spellings of it is what would drift, so there is one.
502 ///
503 /// The `Status` option and this source's own origin text field are the whole of it. It
504 /// selects no `ProjectV2ItemFieldLabelValue`: GitHub derives that field from the item's
505 /// content, so it holds nothing the content's own `labels` do not already say, and it
506 /// would sit a label connection two page sizes deep.
507 macro_rules! board_item_values {
508 () => {
509 r#"fieldValues(first:$nestedFirst){nodes{
510 ... on ProjectV2ItemFieldSingleSelectValue{name field{
511 ... on ProjectV2SingleSelectField{id name options{id name}}
512 }}
513 ... on ProjectV2ItemFieldTextValue{text field{... on ProjectV2Field{id name}}}
514 }pageInfo{hasNextPage}}"#
515 };
516 }
517
518 /// Everything this source reads about one issue, wherever it reaches that issue.
519 ///
520 /// A macro rather than a constant so the three documents below can `concat!` it: one
521 /// spelling of these fields is what makes an issue read through the board-scoped
522 /// search, through its own node id, and through its project's sub-issue relationship
523 /// resolve to *the same* item, which is the whole of what
524 /// [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource) relies on.
525 ///
526 /// `projectItems` is what carries the board half of an issue: the board item's own id
527 /// and the [`board_item_values!`] above — the `Status` option and this source's origin
528 /// text field — that a `ProjectV2.items` read used to carry. It is asked for on the
529 /// issue rather than on the board, which is what makes the cost of a read proportional
530 /// to what was asked for instead of to the board's size.
531 ///
532 /// It carries a *page* of that connection, at `BOARD_ITEMS_PAGE_SIZE`, and its
533 /// `endCursor` is what [`ISSUE_BOARD_ITEMS`] resumes from when this board's entry is
534 /// not on that page: a page here is where the search for the entry starts rather than
535 /// where it ends.
536 ///
537 /// It does **not** select the board's `Labels` field value, and that is the whole of
538 /// what keeps the three documents below under [`NODE_COUNT_LIMIT`](super::NODE_COUNT_LIMIT):
539 /// a label connection there sits under `fieldValues` under `projectItems` under a page
540 /// of issues, spending `$nestedFirst` twice down one path, and took
541 /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] to 2,556,100 nodes against a limit of 500,000.
542 /// No label is lost — this is a fragment `on Issue`, whose own `labels` are selected
543 /// above, and that connection is where every label this source reports comes from. No
544 /// document in this module selects the board field any longer, [`BOARD`] included; the
545 /// module documentation records why nothing it could have held is lost.
546 macro_rules! board_issue {
547 () => {
548 concat!(
549 r#" fragment BoardIssue on Issue{__typename id title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total}
550 labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}
551 projectItems(first:$boardItems){nodes{id project{number}
552 "#,
553 board_item_values!(),
554 r#"}pageInfo{hasNextPage endCursor}}}"#
555 )
556 };
557 }
558
559 /// Every issue of one board, found by a search scoped to that board.
560 ///
561 /// This is how the projects a board holds are listed, and it selects no `items`
562 /// connection on `ProjectV2`: the board is a *qualifier of the search* rather than a
563 /// container walked page by page, so nothing nested inside a board item is paid for.
564 /// Which of the issues it returns is a project is then read off `parent` — GitHub
565 /// accepts `-has:parent` as a search qualifier and silently ignores it, so the
566 /// discriminator has to be applied to the field, which is a scalar on the issue and
567 /// costs nothing.
568 pub const SEARCH_ISSUES: &str = concat!(
569 r#"query($search:String!,$type:SearchType!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!){
570 search(query:$search,type:$type,first:$first,after:$after){
571 pageInfo{hasNextPage endCursor}
572 nodes{__typename ...BoardIssue}
573 }
574 }"#,
575 board_issue!()
576 );
577
578 /// One issue by its own node id, which is what a qualified id names here.
579 ///
580 /// Strongly consistent, unlike the search above: GitHub's issue search is an index and
581 /// answers a write made moments ago with the value from before it, and resolving a node
582 /// id does not.
583 pub const ISSUE: &str = concat!(
584 r#"query($id:ID!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!){
585 node(id:$id){__typename ...BoardIssue}
586 }"#,
587 board_issue!()
588 );
589
590 /// One project's tasks: the sub-issues of the issue that project is.
591 ///
592 /// The work this costs is the project's own size. Nothing about it grows as the board
593 /// gains projects, or as those projects gain tasks.
594 pub const SUB_ISSUES: &str = concat!(
595 r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!){
596 node(id:$id){__typename
597 ... on Issue{subIssues(first:$first,after:$after){
598 pageInfo{hasNextPage endCursor}
599 nodes{__typename ...BoardIssue}
600 }}}
601 }"#,
602 board_issue!()
603 );
604
605 /// Reads the board's fields and one page of its items.
606 pub const BOARD: &str = concat!(
607 r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$nestedFirst:Int!,$duplicates:Boolean!){
608 owner:repositoryOwner(login:$owner){
609 ... on ProjectV2Owner{projectV2(number:$number){...Board}}
610 }
611 } fragment Board on ProjectV2 { id title
612 fields(first:$nestedFirst){nodes{
613 ... on ProjectV2SingleSelectField{__typename id name options{id name}}
614 ... on ProjectV2Field{__typename id name}
615 }pageInfo{hasNextPage}}
616 items(first:$first,after:$after){nodes{id "#,
617 board_item_values!(),
618 r#" content{
619 ... on Issue{__typename id title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total} labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}}
620 ... on PullRequest{__typename id}
621 ... on DraftIssue{__typename id title body createdAt updatedAt}
622 }} pageInfo{hasNextPage endCursor}}
623 }"#
624 );
625
626 /// One issue's board memberships alone, walked past the page a read of it carried.
627 ///
628 /// The recovery read behind [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource):
629 /// every document above carries a *page* of `Issue.projectItems`, and an issue on more
630 /// boards than that page holds may have this board's entry past its end. This asks that
631 /// one issue for its memberships and nothing else — the caller already holds the issue —
632 /// so an answer of "this board does not hold it" is only ever given about a connection
633 /// read to exhaustion.
634 ///
635 /// It selects the board item's id, its project number and the same
636 /// [`board_item_values!`] the fragment does, because what it produces is handed to the
637 /// very same resolver: an issue recovered this way reports the same title, the same
638 /// status, the same labels and the same qualified id as one whose entry was on the
639 /// page.
640 ///
641 /// `$first` rather than `$boardItems`: this document reads one issue, so nothing
642 /// multiplies through it and the membership connection can be walked at
643 /// [`MAX_PAGE_SIZE`](super::MAX_PAGE_SIZE) — which is what keeps the recovery to one
644 /// further request for any issue a person really keeps.
645 pub const ISSUE_BOARD_ITEMS: &str = concat!(
646 r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!){
647 node(id:$id){
648 ... on Issue{projectItems(first:$first,after:$after){
649 nodes{id project{number}
650 "#,
651 board_item_values!(),
652 r#"}
653 pageInfo{hasNextPage endCursor}}}
654 }
655 }"#
656 );
657 /// Resolves the configured repository's node id, which creating an issue requires.
658 pub const REPOSITORY: &str = r#"query($owner:String!,$name:String!){repository(owner:$owner,name:$name){id nameWithOwner}}"#;
659 /// Reads both dependency directions for one issue, with each far end's own kind.
660 pub const ISSUE_DEPENDENCIES: &str = r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
661 ... on Issue{
662 blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
663 blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
664 }}} fragment Related on Issue{id title body parent{id} subIssuesSummary{total}}"#;
665 /// Creates one issue in the configured repository.
666 pub const CREATE_ISSUE: &str =
667 r#"mutation($input:CreateIssueInput!){createIssue(input:$input){issue{id url}}}"#;
668 /// Puts an existing issue on the configured board.
669 pub const ADD_TO_BOARD: &str = r#"mutation($input:AddProjectV2ItemByIdInput!){addProjectV2ItemById(input:$input){item{id}}}"#;
670 /// Updates an issue's visible fields and its open or closed state in one call.
671 pub const UPDATE_ISSUE: &str =
672 r#"mutation($input:UpdateIssueInput!){updateIssue(input:$input){issue{id}}}"#;
673 /// Updates an existing draft's user-visible fields.
674 pub const UPDATE_DRAFT: &str = r#"mutation($input:UpdateProjectV2DraftIssueInput!){updateProjectV2DraftIssue(input:$input){draftIssue{id}}}"#;
675 /// Updates a text or single-select value on one project item.
676 pub const UPDATE_FIELD: &str = r#"mutation($input:UpdateProjectV2ItemFieldValueInput!){updateProjectV2ItemFieldValue(input:$input){projectV2Item{id}}}"#;
677 /// Files one issue under another as a sub-issue, which is what project membership is.
678 pub const ADD_SUB_ISSUE: &str =
679 r#"mutation($input:AddSubIssueInput!){addSubIssue(input:$input){issue{id} subIssue{id}}}"#;
680 /// Takes one issue back out of its parent.
681 pub const REMOVE_SUB_ISSUE: &str = r#"mutation($input:RemoveSubIssueInput!){removeSubIssue(input:$input){issue{id} subIssue{id}}}"#;
682 /// Adds GitHub's native issue blocked-by relationship.
683 pub const ADD_BLOCKED_BY: &str = r#"mutation($input:AddBlockedByInput!){addBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
684 /// Removes one native issue blocked-by relationship.
685 pub const REMOVE_BLOCKED_BY: &str = r#"mutation($input:RemoveBlockedByInput!){removeBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
686 /// Deletes one issue, which takes its board item with it.
687 ///
688 /// The engine sends this in one situation only: undoing a copy that could not finish,
689 /// over the items that same copy created. Deleting the issue removes the board item
690 /// too, so there is no second `deleteProjectV2Item` to keep in step with it.
691 pub const DELETE_ISSUE: &str =
692 r#"mutation($input:DeleteIssueInput!){deleteIssue(input:$input){repository{id}}}"#;
693
694 /// Every document above, with what this source is doing when it sends one.
695 ///
696 /// One list rather than a `match` beside the constants: a rate-limit diagnostic has to
697 /// name the call that was refused, and a `match` with a catch-all arm would answer a
698 /// document added later with "talking to GitHub" and never say so.
699 ///
700 /// `documents_are_all_inventoried` reads this file back and fails naming any `pub
701 /// const` here that this list omits, so the two cannot part — which is the same guard
702 /// `CATEGORIES` carries, in the one shape available to a set of `&str` constants.
703 pub const DOCUMENTS: [(&str, &str); 17] = [
704 (SEARCH_ISSUES, "searching this board's issues"),
705 (ISSUE, "reading one issue"),
706 (
707 ISSUE_BOARD_ITEMS,
708 "reading one issue's board memberships past the page it came with",
709 ),
710 (SUB_ISSUES, "reading a project's tasks"),
711 (BOARD, "reading the board"),
712 (REPOSITORY, "reading the destination repository"),
713 (ISSUE_DEPENDENCIES, "reading an issue's dependencies"),
714 (CREATE_ISSUE, "creating an issue"),
715 (ADD_TO_BOARD, "adding an issue to the board"),
716 (UPDATE_ISSUE, "updating an issue"),
717 (UPDATE_DRAFT, "updating a draft item"),
718 (UPDATE_FIELD, "writing a board field"),
719 (ADD_SUB_ISSUE, "filing an issue under its project"),
720 (REMOVE_SUB_ISSUE, "taking an issue out of its project"),
721 (ADD_BLOCKED_BY, "recording a dependency"),
722 (REMOVE_BLOCKED_BY, "removing a dependency"),
723 (DELETE_ISSUE, "deleting an issue"),
724 ];
725}
726
727/// Which of GitHub's two rate limiters refused a request.
728///
729/// Waiting is the whole answer to the primary budget, and polling is what *extends* the
730/// secondary one — so an operator told the wrong one takes the wrong next step, which is
731/// the whole reason this is carried rather than collapsed into "rate limited".
732#[derive(Debug, Clone, Copy, PartialEq, Eq)]
733enum Limiter {
734 /// The hourly API budget, which `gh api rate_limit` reports and a wait answers.
735 Primary,
736 /// The burst limiter over content-generating requests, which nothing reports.
737 Secondary,
738}
739
740/// The wordings GitHub answers a secondary rate limit with.
741///
742/// It sends them under a forbidden status, under a too-many-requests status, and inside
743/// the `errors` of a *successful* response, which is why the text is what this matches on
744/// rather than the status. `abuse detection` is the wording GitHub used before the
745/// limiter was renamed and still returns from some endpoints; `submitted too quickly` is
746/// what a burst of content creation is refused with.
747///
748/// This is GitHub's vocabulary rather than this source's, so it is pinned rather than
749/// remembered: `tests/fixtures/rate-limits.json` records where each wording was read and
750/// when, and the drift gate reconciles the two lists both ways. Public for that gate
751/// alone — a caller has no use for it, and matching on a refusal is this source's job.
752pub const SECONDARY_WORDINGS: [&str; 5] = [
753 "secondary rate limit",
754 "temporarily blocked from content creation",
755 "abuse detection",
756 "submitted too quickly",
757 "exceeded a secondary",
758];
759
760/// The wordings GitHub answers an exhausted primary budget with.
761///
762/// `rate_limited` is the `type` its GraphQL error carries, which is read as a field rather
763/// than looked for in the response text. Pinned and gated exactly as
764/// [`SECONDARY_WORDINGS`] is, and public for the same one reason.
765pub const PRIMARY_WORDINGS: [&str; 3] = [
766 "api rate limit exceeded",
767 "rate limit exceeded",
768 "rate_limited",
769];
770
771/// What a response *says about itself*, which is the only place a refusal can be read.
772///
773/// Deliberately not the whole response body. A board is a place people write about their
774/// own work, and a task on it titled "the secondary rate limit" would, matched across the
775/// raw text, turn a perfectly good answer into a refusal this source then waited out and
776/// reported. So the item data is never read: what is read is GitHub's own REST-style
777/// `message` envelope, which is what a forbidden status carries, and the `message` and
778/// `type` of each GraphQL error, which is where a *successful* response says it.
779///
780/// A body that is not JSON at all has nothing structured to read, so only a failing
781/// response's own text is taken — a successful response that is not JSON is malformed
782/// rather than refused, and [`GitHubProjectsSource::answer`] says so.
783fn refusal_wording(status: StatusCode, body: &str) -> String {
784 let Ok(parsed) = serde_json::from_str::<Value>(body) else {
785 return if status.is_success() {
786 String::new()
787 } else {
788 body.to_owned()
789 };
790 };
791 let mut said: Vec<&str> = parsed
792 .get("message")
793 .and_then(Value::as_str)
794 .into_iter()
795 .collect();
796 if let Some(errors) = parsed.get("errors").and_then(Value::as_array) {
797 for error in errors {
798 said.extend(
799 ["message", "type"]
800 .into_iter()
801 .filter_map(|key| error.get(key).and_then(Value::as_str)),
802 );
803 }
804 }
805 said.join("; ")
806}
807
808impl Limiter {
809 /// Which limiter refused this response, or `None` when none of them did.
810 ///
811 /// The wording is read first and the status only decides what carries none of it,
812 /// because GitHub answers a secondary limit with a forbidden status far more often
813 /// than with too-many-requests — while a forbidden status saying nothing about a limit
814 /// really is a credential this token lacks.
815 ///
816 /// A response is a refusal because of its status or its own wording. A spent budget
817 /// only ever explains one; it never turns an answer into a refusal.
818 fn classify(status: StatusCode, budget_exhausted: bool, body: &str) -> Option<Self> {
819 let normalized = refusal_wording(status, body).to_ascii_lowercase();
820 if SECONDARY_WORDINGS
821 .iter()
822 .any(|wording| normalized.contains(wording))
823 {
824 return Some(Self::Secondary);
825 }
826 if status == StatusCode::TOO_MANY_REQUESTS {
827 return Some(Self::Primary);
828 }
829 // An exhausted budget *explains* a response that failed; it does not make one that
830 // succeeded into a failure. GitHub sets `x-ratelimit-remaining: 0` on the last
831 // request the budget allowed as well as on the ones it then refuses, so reading
832 // the header alone threw away a good answer — and, once refusals were retried,
833 // replayed a request that had already taken effect.
834 if !status.is_success() && budget_exhausted {
835 return Some(Self::Primary);
836 }
837 // A successful response saying it: GitHub reports a GraphQL rate limit in the
838 // `errors` of an HTTP 200, where nothing about the status says so at all.
839 if status.is_success()
840 && PRIMARY_WORDINGS
841 .iter()
842 .any(|wording| normalized.contains(wording))
843 {
844 return Some(Self::Primary);
845 }
846 None
847 }
848
849 /// What this limiter is called where an operator can look it up.
850 const fn name(self) -> &'static str {
851 match self {
852 Self::Primary => "GitHub's primary API rate limit",
853 Self::Secondary => "GitHub's secondary rate limit",
854 }
855 }
856
857 /// What the endpoint an operator would go and check says about this limiter.
858 const fn where_to_look(self) -> &'static str {
859 match self {
860 Self::Primary => {
861 "That is the budget `gh api rate_limit` reports, so that endpoint says when it \
862 comes back."
863 }
864 Self::Secondary => {
865 "That limiter is not the primary API budget: `gh api rate_limit` reports the \
866 primary budget and does not report this one, so budget showing there says \
867 nothing about this refusal, and every further attempt extends it."
868 }
869 }
870 }
871
872 /// The next step this limiter actually calls for.
873 const fn what_to_do(self) -> &'static str {
874 match self {
875 Self::Primary => {
876 "wait for the reset `gh api rate_limit` reports, then run the command again."
877 }
878 Self::Secondary => {
879 "leave this board alone for a few minutes, then run the command again — or \
880 raise pacing.min_mutation_interval_ms on this source so it writes more slowly."
881 }
882 }
883 }
884}
885
886/// One rate-limit refusal, and the wait GitHub asked for if it asked for one.
887#[derive(Debug, Clone, Copy)]
888struct Limited {
889 limiter: Limiter,
890 hint: Option<u64>,
891}
892
893impl Limited {
894 /// What the caller is told once this source has waited as long as it may.
895 ///
896 /// Both limiters report as [`SourceError::RateLimited`], because that is what
897 /// happened: the kind a caller matches on says a rate limit refused this, and nothing
898 /// about *which* limiter it was makes it a different kind of failure. What differs is
899 /// the operator's next step, and that is what the message carries — a secondary
900 /// refusal read as a primary one sends an operator to `gh api rate_limit`, where the
901 /// budget looks fine, and then back to retry the very burst that was refused.
902 fn exhausted(
903 self,
904 doing: &str,
905 waits: u32,
906 waited: Duration,
907 needed: Duration,
908 budget: Duration,
909 ) -> SourceError {
910 SourceError::RateLimited {
911 retry_after_seconds: self.hint,
912 message: Some(format!(
913 "{} refused this source while {doing}; it waited {} out over {} and was refused \
914 again, and the next wait of {} would take it past the {} one call may spend \
915 waiting. {} next: {}",
916 self.limiter.name(),
917 plural(waits, "refusal"),
918 seconds(waited),
919 seconds(needed),
920 seconds(budget),
921 self.limiter.where_to_look(),
922 self.limiter.what_to_do(),
923 )),
924 }
925 }
926}
927
928/// One HTTP attempt's result, with what its response said about the rate limit.
929///
930/// The two travel together so the record and the outcome are written from the same place:
931/// what a response said about the budget is only readable while that response is in hand,
932/// and what the attempt *meant* is only decidable once its body has been read.
933struct Attempted {
934 result: Result<Value, Attempt>,
935 limits: accounting::RateLimit,
936 /// GitHub's own reported cost for this call, for a document that asked for it.
937 reported_cost: Option<u64>,
938}
939
940/// One attempt's outcome: an error to report, or a rate limit to wait out.
941enum Attempt {
942 Failed(SourceError),
943 Limited(Limited),
944}
945
946fn plural(count: u32, thing: &str) -> String {
947 if count == 1 {
948 format!("{count} {thing}")
949 } else {
950 format!("{count} {thing}s")
951 }
952}
953
954fn seconds(duration: Duration) -> String {
955 format!("{:.1}s", duration.as_secs_f64())
956}
957
958/// A header GitHub spells as a whole number of seconds, or `None` when this one is not.
959///
960/// A value that is present and unreadable is deliberately *not* an error. `retry-after` is
961/// allowed by HTTP to be a date rather than a count, an intermediary can rewrite either
962/// header, and neither is what makes a response a refusal — so the whole cost of one this
963/// cannot read is that the refusal carries no hint and the backing-off schedule answers it
964/// instead. Refusing the response over the header would turn a readable refusal into an
965/// unreadable one, and refusing to *wait* would be the one wrong direction to fail in.
966fn whole_seconds(value: Option<&reqwest::header::HeaderValue>) -> Option<u64> {
967 value
968 .and_then(|value| value.to_str().ok())
969 .and_then(|value| value.trim().parse::<u64>().ok())
970}
971
972/// Every mutation this source sends creates content — an issue, a board item, a field of
973/// one, a sub-issue link, a dependency — and no query in [`graphql::DOCUMENTS`] does, so
974/// what the secondary limiter counts and what the keyword says are the same set. That is
975/// what makes the keyword a sound test rather than a convenient one.
976fn is_mutation(query: &str) -> bool {
977 query.trim_start().starts_with("mutation")
978}
979
980/// What this source was doing, for a diagnostic that has to say so.
981///
982/// Read out of [`graphql::DOCUMENTS`], which is the inventory rather than a copy of it, so
983/// a document added without a description is caught by that list's own gate instead of
984/// falling through to the vague arm below.
985fn operation_description(query: &str) -> &'static str {
986 graphql::DOCUMENTS
987 .iter()
988 .find(|(document, _)| *document == query)
989 .map_or("talking to GitHub", |(_, doing)| *doing)
990}
991
992/// GitHub's published ceiling on content-generating requests, per minute.
993///
994/// Pinned in `tests/fixtures/rate-limits.json` and gated against it, because it is
995/// GitHub's number rather than this source's: [`MIN_MUTATION_INTERVAL_MS`] is *derived*
996/// from it, so a pacing value checked only against itself cannot go stale here.
997pub const CONTENT_CREATION_PER_MINUTE: u64 = 80;
998/// The same ceiling as GitHub publishes it per hour, which this source does **not** pace
999/// at. See [`MIN_MUTATION_INTERVAL_MS`] for why the per-minute bound is the one that
1000/// governs; it is pinned beside its sibling so the gate would notice either one moving.
1001pub const CONTENT_CREATION_PER_HOUR: u64 = 500;
1002/// Shortest interval between two content-creating mutations, in milliseconds.
1003///
1004/// GitHub documents two secondary limits on content-generating requests:
1005/// [`CONTENT_CREATION_PER_MINUTE`] and [`CONTENT_CREATION_PER_HOUR`]. 60000/80 is 750, so
1006/// a mutation every 750 ms is the fastest rate that cannot exceed the per-minute bound,
1007/// and that is the bound a copy actually trips: a copy of one plan-sized project is a
1008/// burst of a few dozen mutations inside a few seconds. The hourly bound works out at one
1009/// every 7.2 seconds sustained, which no single copy reaches and which, used as the
1010/// spacing here, would turn an ordinary copy into an hour of waiting — so it is
1011/// deliberately *not* what this paces at. An installation that wants the hourly bound
1012/// honoured for a long sequence of copies says so through
1013/// `pacing.min_mutation_interval_ms`.
1014pub const MIN_MUTATION_INTERVAL_MS: u64 = 60_000 / CONTENT_CREATION_PER_MINUTE;
1015/// First wait when a rate-limit refusal carries no hint; each further wait doubles it.
1016///
1017/// A doubling schedule from one second reaches a minute in six waits, which is GitHub's
1018/// own advice for a secondary limit — wait, and wait longer each time — without spending
1019/// the first minute of a transient refusal doing nothing.
1020pub const RETRY_BACKOFF_MS: u64 = 1_000;
1021/// Total time one call may spend waiting out rate limits before it reports a failure.
1022///
1023/// Two minutes is long enough to ride out the refusals a paced copy still collects and
1024/// short enough that a command an operator is watching returns. The bound is what makes
1025/// the wait a wait rather than a hang: a call refused past it ends in a diagnostic naming
1026/// the limiter, not in a process nobody can tell from a wedged one.
1027pub const RETRY_BUDGET_MS: u64 = 120_000;
1028
1029fn default_token_env() -> String {
1030 "GH_PROJECTS_TOKEN".to_owned()
1031}
1032fn default_endpoint() -> String {
1033 "https://api.github.com/graphql".to_owned()
1034}
1035
1036/// Where one status category lands on this board.
1037///
1038/// `null` — an absent value — disables the category for this instance, and using a
1039/// disabled status is a refusal naming the status and the instance.
1040#[derive(Debug, Clone, Deserialize, schemars::JsonSchema)]
1041#[serde(untagged)]
1042pub enum StatusTargetConfig {
1043 /// The name of a `Status` single-select option already on the board.
1044 Column(ColumnName),
1045 /// A closed issue state, whose reason is what tells done from cancelled.
1046 Closed {
1047 /// The `IssueClosedStateReason` to close with.
1048 closed: ClosedState,
1049 },
1050}
1051
1052/// The name of a `Status` single-select option on the board.
1053///
1054/// Validated on the way in rather than checked later, so a blank option name — which
1055/// nothing on a board can be — is a state this type cannot hold.
1056#[derive(Debug, Clone, PartialEq, Eq, Deserialize, schemars::JsonSchema)]
1057#[serde(try_from = "String")]
1058pub struct ColumnName(String);
1059
1060impl ColumnName {
1061 /// The option name, as the board spells it.
1062 fn as_str(&self) -> &str {
1063 &self.0
1064 }
1065}
1066
1067impl TryFrom<String> for ColumnName {
1068 type Error = String;
1069
1070 fn try_from(name: String) -> Result<Self, Self::Error> {
1071 if name.trim().is_empty() {
1072 return Err("a status_mapping option name cannot be blank".to_owned());
1073 }
1074 Ok(Self(name))
1075 }
1076}
1077
1078/// The two closed states this product can mean.
1079///
1080/// GitHub's `IssueClosedStateReason` also spells `DUPLICATE`, which is neither finished
1081/// work nor abandoned work, so nothing here ever writes it.
1082#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, schemars::JsonSchema)]
1083#[serde(rename_all = "kebab-case")]
1084pub enum ClosedState {
1085 /// `COMPLETED` — precisely done.
1086 Completed,
1087 /// `NOT_PLANNED` — precisely cancelled.
1088 NotPlanned,
1089}
1090
1091impl ClosedState {
1092 const fn reason(self) -> &'static str {
1093 match self {
1094 Self::Completed => "COMPLETED",
1095 Self::NotPlanned => "NOT_PLANNED",
1096 }
1097 }
1098}
1099
1100/// Configuration for one GitHub Projects v2 board.
1101#[derive(Debug, Clone, Default, Deserialize, schemars::JsonSchema)]
1102#[serde(default, deny_unknown_fields)]
1103pub struct GitHubProjectsConfig {
1104 /// Login of the user or organization which owns the board.
1105 pub owner: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates GitHub's owner grammar before private construction.
1106 /// The project number shown in the board's GitHub URL.
1107 pub project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` bounds this to a positive GraphQL Int.
1108 /// `owner/name` of the one repository this source creates its issues in.
1109 ///
1110 /// A board has no repository of its own and `createIssue` requires one, so a write
1111 /// without this is refused naming the field. Reads never need it.
1112 pub repository: Option<String>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the `owner/name` grammar before private construction.
1113 /// Environment variable containing a fine-grained token with Projects and Issues
1114 /// read/write plus Pull requests read-only access for every repository represented on
1115 /// the board.
1116 #[serde(default = "default_token_env")]
1117 pub token_env: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the environment-variable grammar.
1118 /// GraphQL endpoint. GitHub Enterprise installations may override it.
1119 #[serde(default = "default_endpoint")]
1120 pub endpoint: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` converts it to the private validated `Url`.
1121 /// Per-instance mapping from a status category to where it lands on this board.
1122 ///
1123 /// A category this does not mention keeps its shipped default: `backlog` to
1124 /// "Backlog", `todo` to "Todo", `in-progress` to "In Progress", `done` to closed as
1125 /// completed, `cancelled` to closed as not planned, and `draft` and `unknown`
1126 /// disabled.
1127 #[serde(default)]
1128 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.
1129 /// How fast this source writes, and how long it waits out a rate-limit refusal.
1130 ///
1131 /// Every field keeps its shipped default when it is absent, and the defaults are
1132 /// GitHub's own published limits rather than taste. See [`Pacing`].
1133 #[serde(default)]
1134 pub pacing: PacingConfig,
1135}
1136
1137/// How fast this source writes, and how long it waits out a rate-limit refusal.
1138///
1139/// Configurable because a GitHub Enterprise installation sets its own limits and an
1140/// operator who has already been refused may want to go slower still — not because the
1141/// defaults are guesses.
1142#[derive(Debug, Clone, Default, Deserialize, schemars::JsonSchema)]
1143#[serde(default, deny_unknown_fields)]
1144pub struct PacingConfig {
1145 /// Shortest interval between two content-creating mutations, in milliseconds.
1146 ///
1147 /// Zero sends them as fast as they are asked for, which is what a fixture server on
1148 /// loopback wants and what no board on github.com does. At most [`MAX_PACING_MS`].
1149 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.
1150 /// First wait when a rate-limit refusal carries no hint, in milliseconds. Each
1151 /// further wait of the same call doubles it. At most [`MAX_PACING_MS`], and never
1152 /// zero while there is a budget to spend, because a schedule of zero-length waits
1153 /// consumes none of it and so never ends.
1154 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.
1155 /// Total time one call may spend waiting out rate limits, in milliseconds.
1156 ///
1157 /// Zero reports the refusal rather than waiting at all. At most [`MAX_PACING_MS`]:
1158 /// the bound is what makes this a wait rather than a hang.
1159 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.
1160}
1161
1162/// The largest any pacing setting may be, in milliseconds.
1163///
1164/// One hour. GitHub's own harshest published bound on content-generating requests works
1165/// out at one every 7.2 seconds, so an hour is already three orders of magnitude past
1166/// anything a real limit asks for, and past it the settings stop describing pacing at all:
1167/// a wait budget beyond it is the unbounded wait this whole mechanism exists to replace,
1168/// and an interval beyond it is a command that never sends its second mutation. It also
1169/// keeps the clock arithmetic in [`GitHubProjectsSource::reserve_mutation_slot`] inside
1170/// what an `Instant` can hold on every platform.
1171pub const MAX_PACING_MS: u64 = 3_600_000;
1172
1173/// [`PacingConfig`] with every default resolved and every value checked, which is what the
1174/// source holds.
1175#[derive(Debug, Clone, Copy)]
1176struct Pacing {
1177 min_mutation_interval: Duration,
1178 retry_backoff: Duration,
1179 retry_budget: Duration,
1180}
1181
1182impl Pacing {
1183 /// Resolve one instance's pacing, refusing a configuration that would not pace at all.
1184 fn resolve(config: PacingConfig, instance: &SourceName) -> Result<Self, SourceError> {
1185 let bounded = |value: Option<u64>, default: u64, field: &str| match value {
1186 Some(value) if value > MAX_PACING_MS => Err(SourceError::Config {
1187 message: format!(
1188 "pacing.{field} of source {instance} is {value} ms, and the most any pacing \
1189 setting may be is {MAX_PACING_MS} ms — an hour, which is already far past \
1190 GitHub's own harshest published limit"
1191 ),
1192 }),
1193 Some(value) => Ok(Duration::from_millis(value)),
1194 None => Ok(Duration::from_millis(default)),
1195 };
1196 let retry_backoff = bounded(
1197 config.retry_backoff_ms,
1198 RETRY_BACKOFF_MS,
1199 "retry_backoff_ms",
1200 )?;
1201 let retry_budget = bounded(config.retry_budget_ms, RETRY_BUDGET_MS, "retry_budget_ms")?;
1202 if retry_backoff.is_zero() && !retry_budget.is_zero() {
1203 return Err(SourceError::Config {
1204 message: format!(
1205 "pacing.retry_backoff_ms of source {instance} is 0 while \
1206 pacing.retry_budget_ms is {} ms; a schedule of zero-length waits spends \
1207 none of that budget, so it would retry a refusal forever. Set a backoff of \
1208 at least 1 ms, or set retry_budget_ms to 0 to report a refusal without \
1209 waiting at all",
1210 retry_budget.as_millis()
1211 ),
1212 });
1213 }
1214 Ok(Self {
1215 min_mutation_interval: bounded(
1216 config.min_mutation_interval_ms,
1217 MIN_MUTATION_INTERVAL_MS,
1218 "min_mutation_interval_ms",
1219 )?,
1220 retry_backoff,
1221 retry_budget,
1222 })
1223 }
1224}
1225
1226/// Factory for [`GitHubProjectsSource`].
1227#[derive(Debug, Clone, Copy, Default)]
1228pub struct Plugin;
1229
1230impl SourcePlugin for Plugin {
1231 fn kind(&self) -> &'static str {
1232 KIND
1233 }
1234 fn config_schema(&self) -> Schema {
1235 schema_for!(GitHubProjectsConfig)
1236 }
1237 fn build(
1238 &self,
1239 name: &SourceName,
1240 config: &Value,
1241 secrets: &dyn SecretResolver,
1242 ) -> Result<Box<dyn TaskSource>, SourceError> {
1243 self.build_recording_into(name, config, secrets, Arc::new(Accounting::new()))
1244 }
1245}
1246
1247impl Plugin {
1248 /// Build a source recording every request it sends into an accounting the caller holds.
1249 ///
1250 /// [`SourcePlugin::build`] is this with an accounting of its own, which is what the
1251 /// registry gets. This is for a caller that is also calling GitHub itself and wants one
1252 /// session total rather than two — see [`accounting`] and
1253 /// [`GitHubProjectsSource::recording_into`].
1254 ///
1255 /// # Errors
1256 ///
1257 /// Exactly [`SourcePlugin::build`]'s, with the same source name in front of each:
1258 /// [`SourceError::Config`] for configuration this plugin cannot use and
1259 /// [`SourceError::Auth`] for a credential it cannot find.
1260 pub fn build_recording_into(
1261 &self,
1262 name: &SourceName,
1263 config: &Value,
1264 secrets: &dyn SecretResolver,
1265 ledger: Arc<Accounting>,
1266 ) -> Result<Box<dyn TaskSource>, SourceError> {
1267 let config: GitHubProjectsConfig =
1268 serde_json::from_value(config.clone()).map_err(|e| SourceError::Config {
1269 message: format!("source {name}: {e}"),
1270 })?;
1271 let source = GitHubProjectsSource::recording_into(name, config, secrets, ledger).map_err(
1272 |error| match error {
1273 SourceError::Config { message } => SourceError::Config {
1274 message: format!("source {name}: {message}"),
1275 },
1276 SourceError::Auth { message } => SourceError::Auth {
1277 message: format!("source {name}: {message}"),
1278 },
1279 other => other,
1280 },
1281 )?;
1282 Ok(Box::new(source))
1283 }
1284}
1285
1286/// Where a status category lands on this board, once configuration is resolved.
1287#[derive(Debug, Clone, PartialEq, Eq)]
1288enum StatusTarget {
1289 /// Not usable against this instance.
1290 Disabled,
1291 /// The board's `Status` option of this name.
1292 Column(ColumnName),
1293 /// A closed issue, with the reason that says which closed it means.
1294 Closed(ClosedState),
1295}
1296
1297/// Every status category, in the order the vocabulary declares them.
1298///
1299/// This list mirrors `StatusCategory`, so it carries its own drift gate rather than a
1300/// reviewer's attention: [`category_position`] is a wildcard-free match, so a variant
1301/// added to the shared vocabulary fails to compile until it is named there, and this
1302/// crate's suite reconciles this list against that enum's own derived schema, which is
1303/// generated from the variants rather than written beside them. The schema is what
1304/// catches a list left one short — a list checking only the positions it already holds
1305/// would pass while every mapping indexed by the new position panicked.
1306pub const CATEGORIES: [StatusCategory; 7] = [
1307 StatusCategory::Draft,
1308 StatusCategory::Backlog,
1309 StatusCategory::Todo,
1310 StatusCategory::InProgress,
1311 StatusCategory::Done,
1312 StatusCategory::Cancelled,
1313 StatusCategory::Unknown,
1314];
1315
1316/// Where one category sits in [`CATEGORIES`]; see that list for what this pins.
1317#[must_use]
1318pub const fn category_position(category: StatusCategory) -> usize {
1319 match category {
1320 StatusCategory::Draft => 0,
1321 StatusCategory::Backlog => 1,
1322 StatusCategory::Todo => 2,
1323 StatusCategory::InProgress => 3,
1324 StatusCategory::Done => 4,
1325 StatusCategory::Cancelled => 5,
1326 StatusCategory::Unknown => 6,
1327 }
1328}
1329
1330/// The spelling a status category is configured and reported under.
1331fn category_name(category: StatusCategory) -> &'static str {
1332 match category {
1333 StatusCategory::Draft => "draft",
1334 StatusCategory::Backlog => "backlog",
1335 StatusCategory::Todo => "todo",
1336 StatusCategory::InProgress => "in-progress",
1337 StatusCategory::Done => "done",
1338 StatusCategory::Cancelled => "cancelled",
1339 StatusCategory::Unknown => "unknown",
1340 }
1341}
1342
1343/// A shipped default's option name.
1344///
1345/// The literals below are this file's own and non-blank, and they are validated by the
1346/// one constructor a configured name goes through rather than beside it.
1347fn shipped_column(name: &'static str) -> ColumnName {
1348 ColumnName::try_from(name.to_owned()).expect("a shipped default names a board option")
1349}
1350
1351/// The shipped default for one category, before this instance's configuration.
1352fn shipped_default(category: StatusCategory) -> StatusTarget {
1353 match category {
1354 StatusCategory::Backlog => StatusTarget::Column(shipped_column("Backlog")),
1355 StatusCategory::Todo => StatusTarget::Column(shipped_column("Todo")),
1356 StatusCategory::InProgress => StatusTarget::Column(shipped_column("In Progress")),
1357 StatusCategory::Done => StatusTarget::Closed(ClosedState::Completed),
1358 StatusCategory::Cancelled => StatusTarget::Closed(ClosedState::NotPlanned),
1359 StatusCategory::Draft | StatusCategory::Unknown => StatusTarget::Disabled,
1360 }
1361}
1362
1363/// This instance's complete category-to-target mapping, read in both directions.
1364///
1365/// One target per category, held at that category's own [`category_position`], so a
1366/// category missing from the mapping, named twice in it, or filed out of order is a
1367/// state this type cannot hold rather than one [`Self::target`] has to defend against.
1368#[derive(Debug, Clone)]
1369struct StatusMapping {
1370 targets: [StatusTarget; CATEGORIES.len()],
1371}
1372
1373impl StatusMapping {
1374 fn resolve(
1375 configured: BTreeMap<String, Option<StatusTargetConfig>>,
1376 instance: &SourceName,
1377 ) -> Result<Self, SourceError> {
1378 let mut overrides: BTreeMap<&'static str, Option<StatusTargetConfig>> = BTreeMap::new();
1379 for (key, value) in configured {
1380 let category = CATEGORIES
1381 .iter()
1382 .find(|category| category_name(**category) == key)
1383 .ok_or_else(|| SourceError::Config {
1384 message: format!(
1385 "status_mapping names {key:?}, which is not a status category of source \
1386 {instance}; the categories are {}",
1387 CATEGORIES
1388 .iter()
1389 .map(|category| category_name(*category))
1390 .collect::<Vec<_>>()
1391 .join(", ")
1392 ),
1393 })?;
1394 overrides.insert(category_name(*category), value);
1395 }
1396 // `CATEGORIES[position] == category` for every category — the crate's suite
1397 // asserts it — so mapping the list in order fills each category's own slot.
1398 let targets = CATEGORIES.map(|category| match overrides.remove(category_name(category)) {
1399 None => shipped_default(category),
1400 Some(None) => StatusTarget::Disabled,
1401 Some(Some(StatusTargetConfig::Column(option))) => StatusTarget::Column(option),
1402 Some(Some(StatusTargetConfig::Closed { closed })) => StatusTarget::Closed(closed),
1403 });
1404 let mapping = Self { targets };
1405 for (index, category) in CATEGORIES.into_iter().enumerate() {
1406 let StatusTarget::Column(option) = mapping.target(category) else {
1407 continue;
1408 };
1409 if let Some(other) = CATEGORIES[..index].iter().find(|earlier| {
1410 matches!(mapping.target(**earlier), StatusTarget::Column(name)
1411 if name.as_str().eq_ignore_ascii_case(option.as_str()))
1412 }) {
1413 return Err(SourceError::Config {
1414 message: format!(
1415 "status_mapping of source {instance} sends both {} and {} to the board \
1416 option {:?}; one option cannot read back as two categories",
1417 category_name(*other),
1418 category_name(category),
1419 option.as_str()
1420 ),
1421 });
1422 }
1423 }
1424 Ok(mapping)
1425 }
1426
1427 fn target(&self, category: StatusCategory) -> &StatusTarget {
1428 &self.targets[category_position(category)]
1429 }
1430
1431 /// The category a board option name reports, or `None` when nothing maps to it.
1432 fn category_of(&self, option: &str) -> Option<StatusCategory> {
1433 CATEGORIES.into_iter().find(|category| {
1434 matches!(self.target(*category), StatusTarget::Column(name)
1435 if name.as_str().eq_ignore_ascii_case(option))
1436 })
1437 }
1438}
1439
1440/// The one repository this source creates issues in.
1441#[derive(Debug, Clone)]
1442struct RepositoryTarget {
1443 owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
1444 name: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
1445}
1446
1447impl RepositoryTarget {
1448 fn parse(value: &str) -> Result<Self, SourceError> {
1449 let (owner, name) = value.split_once('/').ok_or_else(|| SourceError::Config {
1450 message: format!(
1451 "repository must be spelled owner/name; {value:?} names no repository"
1452 ),
1453 })?;
1454 if !valid_github_owner(owner) || !valid_github_repository_name(name) {
1455 return Err(SourceError::Config {
1456 message: format!(
1457 "repository must be spelled owner/name with a GitHub login and one \
1458 repository name; {value:?} is not"
1459 ),
1460 });
1461 }
1462 Ok(Self {
1463 owner: owner.to_owned(),
1464 name: name.to_owned(),
1465 })
1466 }
1467
1468 fn origin(&self) -> String {
1469 format!("github.com/{}/{}", self.owner, self.name)
1470 }
1471}
1472
1473/// A source which reads GitHub afresh for every operation.
1474pub struct GitHubProjectsSource {
1475 /// This source's configured name, used both to tell a far end naming this source
1476 /// from one naming a system it knows nothing about, and to name the instance a
1477 /// status refusal is about.
1478 name: SourceName,
1479 owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after full GitHub-owner validation.
1480 project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after GraphQL-Int validation.
1481 repository: Option<RepositoryTarget>,
1482 endpoint: Url,
1483 token: SecretString,
1484 credential_name: String, // llmlint: ignore[invalid_states_unrepresentable] Private diagnostic value constructed only after environment-name validation.
1485 statuses: StatusMapping,
1486 client: Client,
1487 /// Every item this source has created since it was built, in the order it created
1488 /// them.
1489 ///
1490 /// GitHub's `projectV2.items` is eventually consistent: an issue added to a board with
1491 /// `addProjectV2ItemById` is routinely absent from the very next read of that board, so
1492 /// a copy resolving a dependency on an item it had just created refused it as not
1493 /// found. A board read is completed from this — an item remembered here and absent from
1494 /// the read is added back, because the board really does hold it and only the read is
1495 /// behind.
1496 ///
1497 /// It is not a cache of a user's work: nothing is remembered that this process did not
1498 /// itself just write, it lives and dies with the process, and it is never consulted for
1499 /// an item this source did not create.
1500 created: Mutex<Vec<Resolved>>,
1501 /// How fast this source writes, and how long it waits out a refusal.
1502 pacing: Pacing,
1503 /// When the last content-creating mutation finished, or the moment the furthest-out
1504 /// reserved slot releases the next one, whichever is later — so the one after it can be
1505 /// spaced from that. See [`MIN_MUTATION_INTERVAL_MS`] for the interval and
1506 /// [`GitHubProjectsSource::finish_mutation`] for why completion rather than release is
1507 /// what it is measured from.
1508 last_mutation: Mutex<Option<Instant>>,
1509 /// The board as this process last read it, for the length of one command.
1510 ///
1511 /// A copy of a project used to re-read the whole board, paged, before writing each of
1512 /// its items, which is by far the largest part of a copy's request count and none of
1513 /// its work. Nothing else changes this board while a command runs — this source's own
1514 /// writes are the only writer — so one read answers them all.
1515 ///
1516 /// It is not a store of a user's work and it is not the cache the no-persistence
1517 /// invariant forbids: it lives and dies with the process exactly as `created` does,
1518 /// nothing is written down, and [`Self::board`] still completes it from `created`, so
1519 /// an item this command created and then depends on resolves whether or not GitHub's
1520 /// own eventually-consistent read has caught up. A write to an item already on the
1521 /// board updates the entry here too, so what this holds is the last read plus this
1522 /// process's own writes rather than a snapshot taken before them.
1523 board_cache: Mutex<Option<Board>>,
1524 /// The destination repository's node id, resolved once rather than per issue created.
1525 ///
1526 /// A repository's node id does not change, and re-reading it for every issue of a copy
1527 /// spent one request per item on an answer this source already had.
1528 repository_cache: Mutex<Option<String>>,
1529 /// What every request this source sends is recorded into.
1530 ///
1531 /// Ordinary code path, not a mode: [`Self::send_once`] records into it at the one place
1532 /// a request leaves this crate, so nothing has to be switched on for a session to be
1533 /// counted. It is shared rather than owned so a caller accounting for a whole session —
1534 /// its own schema verification, board lookups, residue sweep and cleanup beside this
1535 /// source's reads and writes — adds up one accounting instead of two. See
1536 /// [`accounting`] for what a record carries and what a session's spend is and is not.
1537 ledger: Arc<Accounting>,
1538}
1539
1540impl GitHubProjectsSource {
1541 /// Validate configuration and capture the named credential without exposing it.
1542 ///
1543 /// # Errors
1544 ///
1545 /// Returns [`SourceError::Config`] for a configuration this instance cannot use and
1546 /// [`SourceError::Auth`] when the named credential is missing or empty.
1547 pub fn new(
1548 name: &SourceName,
1549 config: GitHubProjectsConfig,
1550 secrets: &dyn SecretResolver,
1551 ) -> Result<Self, SourceError> {
1552 Self::recording_into(name, config, secrets, Arc::new(Accounting::new()))
1553 }
1554
1555 /// The same, recording every request it sends into an accounting the caller holds too.
1556 ///
1557 /// [`Self::new`] is this with an accounting of its own. A caller that is also making
1558 /// its own calls to GitHub — a lane verifying a schema, sweeping residue or cleaning
1559 /// up — passes the one it records those into, so the session total accounts for the
1560 /// whole session rather than for this source's share of it.
1561 ///
1562 /// # Errors
1563 ///
1564 /// Exactly [`Self::new`]'s: [`SourceError::Config`] for a configuration this instance
1565 /// cannot use and [`SourceError::Auth`] when the named credential is missing or empty.
1566 pub fn recording_into(
1567 name: &SourceName,
1568 config: GitHubProjectsConfig,
1569 secrets: &dyn SecretResolver,
1570 ledger: Arc<Accounting>,
1571 ) -> Result<Self, SourceError> {
1572 if !valid_github_owner(&config.owner) {
1573 return Err(SourceError::Config {
1574 message: "owner must be 1-39 ASCII letters, digits, or single hyphens, and cannot start or end with a hyphen".into(),
1575 });
1576 }
1577 if config.project_number == 0 || config.project_number > i32::MAX as u32 {
1578 return Err(SourceError::Config {
1579 message: format!("project_number must be between 1 and {}", i32::MAX),
1580 });
1581 }
1582 if !valid_environment_name(&config.token_env) {
1583 return Err(SourceError::Config {
1584 message: "token_env must be a valid environment-variable name".into(),
1585 });
1586 }
1587 let repository = config
1588 .repository
1589 .as_deref()
1590 .map(RepositoryTarget::parse)
1591 .transpose()?;
1592 let endpoint = Url::parse(&config.endpoint).map_err(|e| SourceError::Config {
1593 message: format!("endpoint is not a valid URL: {e}"),
1594 })?;
1595 if endpoint.scheme() != "https"
1596 && !(endpoint.scheme() == "http"
1597 && endpoint
1598 .host_str()
1599 .is_some_and(|h| h == "127.0.0.1" || h == "localhost" || h == "::1"))
1600 {
1601 return Err(SourceError::Config {
1602 message:
1603 "endpoint must use HTTPS (HTTP is accepted only for a loopback test server)"
1604 .into(),
1605 });
1606 }
1607 let token = secrets.get(&config.token_env).filter(|token| !token.expose_secret().trim().is_empty()).ok_or_else(|| SourceError::Auth {
1608 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),
1609 })?;
1610 Ok(Self {
1611 name: name.clone(),
1612 owner: config.owner,
1613 project_number: config.project_number,
1614 repository,
1615 endpoint,
1616 token,
1617 credential_name: config.token_env,
1618 statuses: StatusMapping::resolve(config.status_mapping, name)?,
1619 client: Client::builder()
1620 .user_agent("onetaskgraph")
1621 .build()
1622 .map_err(|e| SourceError::Config {
1623 message: format!("cannot build HTTP client: {e}"),
1624 })?,
1625 created: Mutex::new(Vec::new()),
1626 pacing: Pacing::resolve(config.pacing, name)?,
1627 last_mutation: Mutex::new(None),
1628 board_cache: Mutex::new(None),
1629 repository_cache: Mutex::new(None),
1630 ledger,
1631 })
1632 }
1633
1634 /// A snapshot of every request this source has sent, and what each cost.
1635 ///
1636 /// A value to hold and compare rather than a borrow of the accounting itself, so two
1637 /// of them can sit side by side. When this source was built with
1638 /// [`Self::recording_into`] the snapshot is the whole shared session, which is the
1639 /// point of building it that way.
1640 #[must_use]
1641 pub fn accounting(&self) -> accounting::Session {
1642 self.ledger.snapshot()
1643 }
1644
1645 /// Send one GraphQL document, pacing this source's own mutations and waiting out a
1646 /// rate limit rather than handing it straight back as an error.
1647 ///
1648 /// Retrying is safe for every document here, including the mutations, and the reason
1649 /// is that only a *refusal* is retried: [`Limiter::classify`] rules on a response
1650 /// GitHub sent, and a request GitHub refused for a rate limit did not run, so nothing
1651 /// this replays has already taken effect. An outcome this source cannot know — the
1652 /// send failed, or the body could not be read, so the mutation may well have landed —
1653 /// is [`Attempt::Failed`] in [`send_once`] and leaves this loop without a second
1654 /// attempt. A duplicate write would come from replaying one of those, and none is
1655 /// replayed.
1656 async fn graphql(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
1657 let doing = operation_description(query);
1658 let mut waited = Duration::ZERO;
1659 let mut waits = 0_u32;
1660 let mut backoff = self.pacing.retry_backoff;
1661 loop {
1662 if is_mutation(query) {
1663 let spacing = self.reserve_mutation_slot();
1664 if !spacing.is_zero() {
1665 tokio::time::sleep(spacing).await;
1666 }
1667 }
1668 let attempt = self.send_once(query, &variables).await;
1669 if is_mutation(query) {
1670 self.finish_mutation();
1671 }
1672 let limited = match attempt {
1673 Ok(data) => return Ok(data),
1674 Err(Attempt::Failed(error)) => return Err(error),
1675 Err(Attempt::Limited(limited)) => limited,
1676 };
1677 // GitHub really does send `retry-after: 0`, and retrying at once is the one
1678 // move that extends a secondary limit, so a hint below the schedule's own next
1679 // wait is raised to it.
1680 let wait = match limited.hint {
1681 Some(hint) => Duration::from_secs(hint).max(backoff),
1682 None => backoff,
1683 };
1684 let remaining = self.pacing.retry_budget.saturating_sub(waited);
1685 // A wait of nothing spends none of the budget, so it is exhaustion rather
1686 // than a retry. `Pacing::resolve` rules out every way of configuring one
1687 // except a budget of zero, where reporting the first refusal is the ask.
1688 if wait.is_zero() || wait > remaining {
1689 return Err(limited.exhausted(
1690 doing,
1691 waits,
1692 waited,
1693 wait,
1694 self.pacing.retry_budget,
1695 ));
1696 }
1697 tokio::time::sleep(wait).await;
1698 waited += wait;
1699 waits += 1;
1700 backoff = backoff.saturating_mul(2);
1701 }
1702 }
1703
1704 /// The next moment a content-creating mutation may leave this source, as a wait from
1705 /// now.
1706 ///
1707 /// The slot is reserved under the lock and the waiting happens outside it, so two
1708 /// callers take two slots rather than the same one — and no lock is held across an
1709 /// await.
1710 ///
1711 /// The moment it is spaced from is the previous mutation's *completion*, which
1712 /// [`Self::finish_mutation`] records. See that method for why the release moment on its
1713 /// own is the wrong thing to measure from.
1714 fn reserve_mutation_slot(&self) -> Duration {
1715 if self.pacing.min_mutation_interval.is_zero() {
1716 return Duration::ZERO;
1717 }
1718 // A poisoned lock here costs pacing, not correctness, and refusing the write over
1719 // it would turn an earlier failure into a second one for no gain.
1720 let mut last = self
1721 .last_mutation
1722 .lock()
1723 .unwrap_or_else(std::sync::PoisonError::into_inner);
1724 let now = Instant::now();
1725 // `checked_add` rather than `+`: `Instant + Duration` panics on overflow, and
1726 // pacing is not worth a panic even at a bound `MAX_PACING_MS` already rules out.
1727 let at = last.map_or(now, |previous| {
1728 previous
1729 .checked_add(self.pacing.min_mutation_interval)
1730 .map_or(now, |earliest| earliest.max(now))
1731 });
1732 *last = Some(at);
1733 at.saturating_duration_since(now)
1734 }
1735
1736 /// Record that a content-creating mutation has finished, so the next one is spaced
1737 /// from here rather than from the moment this one was released.
1738 ///
1739 /// This source can only choose when a request *departs*; the limiter counts when it
1740 /// *arrives*, and the two differ by whatever the request spent in transit. Spacing one
1741 /// departure from the last therefore hands the limiter a gap of the interval less that
1742 /// transit, so a source pacing at 750 ms can still be seen arriving faster — which is
1743 /// exactly how a copy paced well inside a board's threshold was refused by it on a
1744 /// slower machine while passing on a quick one.
1745 ///
1746 /// Spacing from completion removes the subtraction rather than budgeting for it. The
1747 /// previous request had already arrived before its response came back, so its arrival
1748 /// is no later than this moment, and the next mutation is released at least the
1749 /// interval after this moment and arrives no earlier than it is released: the gap the
1750 /// limiter measures is therefore at least the interval, whatever transit costs and on
1751 /// whatever platform. The price is that a mutation's own round trip no longer counts
1752 /// towards its spacing, which makes this source slightly slower than the configured
1753 /// rate rather than slightly faster — the safe side of a limit that punishes being
1754 /// wrong by refusing reads for the next fifty minutes.
1755 ///
1756 /// A failed attempt is recorded too: a request refused by the limiter still arrived,
1757 /// and one that never left costs only a wait nobody needed.
1758 fn finish_mutation(&self) {
1759 if self.pacing.min_mutation_interval.is_zero() {
1760 return;
1761 }
1762 // A poisoned lock here costs pacing, not correctness, exactly as in the reservation.
1763 let mut last = self
1764 .last_mutation
1765 .lock()
1766 .unwrap_or_else(std::sync::PoisonError::into_inner);
1767 let now = Instant::now();
1768 // `max` rather than an assignment: a concurrent caller may already have reserved a
1769 // slot further out, and completing this request must never pull that slot back in.
1770 *last = Some(last.map_or(now, |reserved| reserved.max(now)));
1771 }
1772
1773 /// One HTTP attempt, classified into an answer, a rate limit to wait out, or a
1774 /// failure that waiting cannot help — and recorded, whichever of the three it was.
1775 ///
1776 /// This is the one place a request leaves this crate, which is why the accounting is
1777 /// here rather than at each of the callers: a read path added later is counted without
1778 /// anybody remembering to count it, and
1779 /// `the_session_report_counts_every_request_the_board_served_and_what_each_cost` fails
1780 /// when one is not.
1781 async fn send_once(&self, query: &str, variables: &Value) -> Result<Value, Attempt> {
1782 let Attempted {
1783 result,
1784 limits,
1785 reported_cost,
1786 } = self.attempt(query, variables).await;
1787 // No `otherwise` name: every document this source sends is one of its own, and the
1788 // inventory gate on `graphql::DOCUMENTS` is what keeps that true.
1789 let sending = accounting::Request::graphql(query, variables, None, reported_cost);
1790 let outcome = match &result {
1791 Ok(_) => accounting::Outcome::Answered,
1792 Err(Attempt::Limited(_)) => accounting::Outcome::RateLimited,
1793 Err(Attempt::Failed(_)) => accounting::Outcome::Refused,
1794 };
1795 self.ledger.record(sending.finished(outcome, limits));
1796 result
1797 }
1798
1799 /// The attempt itself, with what its response said about the rate limit alongside.
1800 ///
1801 /// The two are returned together rather than recorded here because every one of the
1802 /// early exits below is a different outcome, and a record written at each of them is a
1803 /// record one of them can be added without.
1804 async fn attempt(&self, query: &str, variables: &Value) -> Attempted {
1805 let mut limits = accounting::RateLimit::default();
1806 let mut reported_cost = None;
1807 let result = self
1808 .attempted(query, variables, &mut limits, &mut reported_cost)
1809 .await;
1810 Attempted {
1811 result,
1812 limits,
1813 reported_cost,
1814 }
1815 }
1816
1817 /// One HTTP attempt, filling in what its response said about the rate limit as it goes.
1818 async fn attempted(
1819 &self,
1820 query: &str,
1821 variables: &Value,
1822 limits: &mut accounting::RateLimit,
1823 reported_cost: &mut Option<u64>,
1824 ) -> Result<Value, Attempt> {
1825 let response = self
1826 .client
1827 .post(self.endpoint.clone())
1828 .bearer_auth(self.token.expose_secret())
1829 .json(&json!({"query": query, "variables": variables}))
1830 .send()
1831 .await
1832 .map_err(|e| {
1833 Attempt::Failed(SourceError::Unavailable {
1834 message: format!("GitHub GraphQL request failed: {e}"),
1835 })
1836 })?;
1837 let status = response.status();
1838 let header = |name: &str| whole_seconds(response.headers().get(name));
1839 *limits = accounting::RateLimit::read(|name| {
1840 response
1841 .headers()
1842 .get(name)
1843 .and_then(|value| value.to_str().ok())
1844 .map(str::to_owned)
1845 });
1846 // Exactly `0` is exhaustion and everything else — a count, an empty value, bytes
1847 // that are not text at all — is "not known to be exhausted". This never makes a
1848 // response a refusal on its own: it says which limiter a refusal is attributed to
1849 // and where its hint comes from, so a value this cannot read costs a hint rather
1850 // than an answer.
1851 let exhausted = response
1852 .headers()
1853 .get("x-ratelimit-remaining")
1854 .and_then(|value| value.to_str().ok())
1855 == Some("0");
1856 // `retry-after` is what GitHub asks for when it asks; when it does not and the
1857 // primary budget is spent, `x-ratelimit-reset` says when that budget comes back,
1858 // which is the same question answered as an absolute time. Nothing else here is a
1859 // hint, and a schedule is what answers a refusal that carries none.
1860 let hint = header("retry-after").or_else(|| {
1861 exhausted
1862 .then(|| header("x-ratelimit-reset"))
1863 .flatten()
1864 .map(|reset| reset.saturating_sub(Utc::now().timestamp().max(0).unsigned_abs()))
1865 });
1866 // Read before it is parsed, because the evidence which tells a secondary rate
1867 // limit from a rejected credential is in the body of a response whose status says
1868 // only "forbidden" — and a non-success response was never parsed at all.
1869 let body = response.text().await.map_err(|e| {
1870 Attempt::Failed(SourceError::Unavailable {
1871 message: format!("GitHub GraphQL response could not be read: {e}"),
1872 })
1873 })?;
1874 if let Some(limiter) = Limiter::classify(status, exhausted, &body) {
1875 return Err(Attempt::Limited(Limited { limiter, hint }));
1876 }
1877 if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {
1878 return Err(Attempt::Failed(SourceError::Auth {
1879 message: format!(
1880 "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"
1881 ),
1882 }));
1883 }
1884 if !status.is_success() {
1885 return Err(Attempt::Failed(SourceError::Unavailable {
1886 message: format!("GitHub GraphQL returned HTTP {status}"),
1887 }));
1888 }
1889 // GitHub reports what a call cost only when the document asked it to, and no
1890 // document this source sends does — so this is `None` here and carries the figure
1891 // for a caller whose own document selects `rateLimit { cost }`. What it must never
1892 // pick up is a `dryRun` probe's cost, which is some other document's.
1893 *reported_cost = serde_json::from_str::<Value>(&body)
1894 .ok()
1895 .as_ref()
1896 .and_then(|body| body.pointer("/data/rateLimit/cost"))
1897 .and_then(Value::as_u64);
1898 self.answer(&body).map_err(Attempt::Failed)
1899 }
1900
1901 /// What one successful HTTP response says, once its GraphQL errors are read.
1902 fn answer(&self, body: &str) -> Result<Value, SourceError> {
1903 let body: Value = serde_json::from_str(body).map_err(|e| SourceError::Malformed {
1904 message: format!("GitHub returned invalid JSON: {e}"),
1905 })?;
1906 let errors = body
1907 .get("errors")
1908 .map(|value| {
1909 value.as_array().ok_or_else(|| SourceError::Malformed {
1910 message: "GitHub response errors is not an array".into(),
1911 })
1912 })
1913 .transpose()?;
1914 if let Some(errors) = errors.filter(|errors| !errors.is_empty()) {
1915 let messages = errors
1916 .iter()
1917 .filter_map(|e| e.get("message").and_then(Value::as_str))
1918 .collect::<Vec<_>>()
1919 .join("; ");
1920 let message = if messages.is_empty() {
1921 "GitHub returned GraphQL errors".into()
1922 } else {
1923 messages
1924 };
1925 let normalized = message.to_ascii_lowercase();
1926 if normalized.contains("resource not accessible") || normalized.contains("scope") {
1927 return Err(SourceError::Auth {
1928 message: format!(
1929 "{message}; grant {} Projects and Issues read/write plus Pull requests read-only access for every repository represented on the board",
1930 self.credential_name
1931 ),
1932 });
1933 }
1934 return Err(SourceError::Refused { message });
1935 }
1936 body.get("data")
1937 .filter(|data| data.is_object())
1938 .cloned()
1939 .ok_or_else(|| SourceError::Malformed {
1940 message: "GitHub response has no data object".into(),
1941 })
1942 }
1943
1944 // llmlint: ignore[boundary_inputs_validated] GitHub caps nested connections at 100 and
1945 // GraphQL cannot independently page them inside the outer item page. This source page is
1946 // deliberately bounded at that published maximum; the live drift journey exercises it.
1947 async fn board_page(
1948 &self,
1949 items_after: Option<&str>,
1950 items_first: u32,
1951 ) -> Result<Value, SourceError> {
1952 let data = self
1953 .graphql(
1954 graphql::BOARD,
1955 json!({"owner":self.owner,"number":self.project_number,
1956 "first":items_first.min(MAX_PAGE_SIZE),"after":items_after,
1957 "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
1958 )
1959 .await?;
1960 data.pointer("/owner/projectV2")
1961 .filter(|v| !v.is_null())
1962 .cloned()
1963 .ok_or_else(|| SourceError::Refused {
1964 message: format!(
1965 "GitHub project {}/{} was not found or is not visible to the token",
1966 self.owner, self.project_number
1967 ),
1968 })
1969 }
1970
1971 /// The search that finds the issues of this board, narrowed by `also` when it is
1972 /// given.
1973 ///
1974 /// `project:owner/number` is what scopes a search to one board, and `is:issue` is what
1975 /// keeps pull requests out of it: GitHub's `ISSUE` search type covers both, and a pull
1976 /// request is somebody's change rather than a unit of plan. `-has:parent` is *not*
1977 /// here on purpose — GitHub accepts it and silently ignores it, so a project is told
1978 /// from a task by the `parent` field each issue carries rather than by the search.
1979 fn board_search(&self, also: Option<&str>) -> String {
1980 let scope = format!("project:{}/{} is:issue", self.owner, self.project_number);
1981 match also {
1982 Some(also) => format!("{scope} {also}"),
1983 None => scope,
1984 }
1985 }
1986
1987 /// One issue this source reached directly, as the board item a read of the board would
1988 /// have produced — or `None` when this board does not hold it.
1989 ///
1990 /// The board half of an issue rides along on `Issue.projectItems`, so the value handed
1991 /// to [`Self::resolve`] is the very shape a `ProjectV2.items` read gives it: the board
1992 /// item's own id, that item's field values, and the issue as its content. One resolver
1993 /// for both routes is what makes an issue read through a search, through its own node
1994 /// id, or through its project's sub-issues report the same title, the same status, the
1995 /// same labels and the same qualified id.
1996 ///
1997 /// An issue with no entry for *this* board is not this source's to report, which is
1998 /// what keeps an id naming some other repository's issue from being answered as an item
1999 /// of this board. That answer is given about an **exhausted** connection and never
2000 /// about an unread page: the entry is looked for on the page in hand, and only if that
2001 /// page reports more of the connection, in [`Self::board_membership`]'s walk of the
2002 /// rest of it.
2003 async fn resolve_issue(&self, issue: &Value) -> Result<Option<Resolved>, SourceError> {
2004 if optional_str(issue, "__typename")? != Some("Issue") {
2005 return Ok(None);
2006 }
2007 let memberships = issue
2008 .get("projectItems")
2009 .ok_or_else(|| SourceError::Malformed {
2010 message: "GitHub issue is missing projectItems".into(),
2011 })?;
2012 let nodes = memberships
2013 .get("nodes")
2014 .and_then(Value::as_array)
2015 .ok_or_else(|| SourceError::Malformed {
2016 message: "GitHub issue projectItems.nodes is not an array".into(),
2017 })?;
2018 let held = match self.board_entry(nodes) {
2019 Some(held) => held.clone(),
2020 None => {
2021 let info = memberships
2022 .get("pageInfo")
2023 .ok_or_else(|| SourceError::Malformed {
2024 message: "GitHub issue projectItems has no pageInfo".into(),
2025 })?;
2026 // The page held no entry for this board. Whether that means the issue is
2027 // not on it is a question about the rest of the connection, and only a
2028 // connection with no rest answers it here.
2029 if !required_bool(info, "hasNextPage")? {
2030 return Ok(None);
2031 }
2032 let cursor = required_str(info, "endCursor")?;
2033 validate_cursor_progress(None, cursor)?;
2034 let issue_id = required_str(issue, "id")?;
2035 match self.board_membership(issue_id, cursor).await? {
2036 Some(held) => held,
2037 None => return Ok(None),
2038 }
2039 }
2040 };
2041 let item = json!({
2042 "id": required_str(&held, "id")?,
2043 "fieldValues": held.get("fieldValues"),
2044 "content": issue,
2045 });
2046 self.resolve(&item)
2047 }
2048
2049 /// This board's own entry among one page of an issue's `Issue.projectItems`.
2050 ///
2051 /// One spelling of *which membership is this board's*, so the page a read carries and
2052 /// the pages [`Self::board_membership`] walks are searched by the same rule.
2053 fn board_entry<'a>(&self, nodes: &'a [Value]) -> Option<&'a Value> {
2054 nodes.iter().find(|node| {
2055 node.pointer("/project/number").and_then(Value::as_u64)
2056 == Some(u64::from(self.project_number))
2057 })
2058 }
2059
2060 /// The rest of one issue's board memberships, from `after`, for this board's entry.
2061 ///
2062 /// The recovery read: a page of memberships that holds no entry for this board says
2063 /// nothing about the memberships past it, so the connection is walked to exhaustion
2064 /// before an issue is reported as one this board does not hold. `Ok(None)` is that
2065 /// positive answer — the whole connection was read and no entry named this board —
2066 /// rather than a failure, and the walk is held to
2067 /// [`validate_cursor_progress`] like every other page walk here, so a source answering
2068 /// with a cursor that does not advance is refused instead of spun on.
2069 async fn board_membership(
2070 &self,
2071 issue: &str,
2072 after: &str,
2073 ) -> Result<Option<Value>, SourceError> {
2074 let mut after = after.to_owned();
2075 loop {
2076 let data = self
2077 .graphql(
2078 graphql::ISSUE_BOARD_ITEMS,
2079 json!({"id":issue,"first":MAX_PAGE_SIZE,"after":after,
2080 "nestedFirst":NESTED_PAGE_SIZE}),
2081 )
2082 .await?;
2083 let Some(connection) = data
2084 .pointer("/node/projectItems")
2085 .filter(|value| !value.is_null())
2086 else {
2087 // The id resolved to nothing, or to something with no memberships to walk —
2088 // which is the same answer as a connection holding no entry for this board.
2089 return Ok(None);
2090 };
2091 let nodes = connection
2092 .get("nodes")
2093 .and_then(Value::as_array)
2094 .ok_or_else(|| SourceError::Malformed {
2095 message: "GitHub issue projectItems.nodes is not an array".into(),
2096 })?;
2097 if let Some(held) = self.board_entry(nodes) {
2098 return Ok(Some(held.clone()));
2099 }
2100 let info = connection
2101 .get("pageInfo")
2102 .ok_or_else(|| SourceError::Malformed {
2103 message: "GitHub issue projectItems has no pageInfo".into(),
2104 })?;
2105 let next = required_bool(info, "hasNextPage")?
2106 .then(|| required_str(info, "endCursor"))
2107 .transpose()?;
2108 match next {
2109 Some(next) => {
2110 validate_cursor_progress(Some(&after), next)?;
2111 after = next.to_owned();
2112 }
2113 None => return Ok(None),
2114 }
2115 }
2116 }
2117
2118 /// One page of a board-scoped issue search, and where the next page resumes.
2119 async fn search_page(
2120 &self,
2121 search: &str,
2122 first: u32,
2123 after: Option<&str>,
2124 ) -> Result<(Vec<Resolved>, Option<String>), SourceError> {
2125 let data = self
2126 .graphql(
2127 graphql::SEARCH_ISSUES,
2128 json!({"search":search,"type":"ISSUE","first":first.min(MAX_PAGE_SIZE),
2129 "after":after,"nestedFirst":NESTED_PAGE_SIZE,
2130 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
2131 )
2132 .await?;
2133 let connection = data.get("search").ok_or_else(|| SourceError::Malformed {
2134 message: "GitHub search response has no search connection".into(),
2135 })?;
2136 let mut found = Vec::new();
2137 for node in connection
2138 .get("nodes")
2139 .and_then(Value::as_array)
2140 .ok_or_else(|| SourceError::Malformed {
2141 message: "GitHub search nodes is not an array".into(),
2142 })?
2143 {
2144 if let Some(resolved) = self.resolve_issue(node).await? {
2145 found.push(resolved);
2146 }
2147 }
2148 let info = connection
2149 .get("pageInfo")
2150 .ok_or_else(|| SourceError::Malformed {
2151 message: "GitHub search connection has no pageInfo".into(),
2152 })?;
2153 let next = required_bool(info, "hasNextPage")?
2154 .then(|| required_str(info, "endCursor"))
2155 .transpose()?
2156 .map(str::to_owned);
2157 if let Some(next) = &next {
2158 validate_cursor_progress(after, next)?;
2159 }
2160 Ok((found, next))
2161 }
2162
2163 /// Every issue this board holds, walked to exhaustion, completed with what this run
2164 /// wrote.
2165 ///
2166 /// The completion is not an optimisation and it is not a cache: GitHub's issue search
2167 /// is an index and is eventually consistent, so an issue this run created seconds ago
2168 /// is routinely absent from it, and a project listed straight after being written would
2169 /// otherwise be missing from its own board. What is added back is only what this
2170 /// process itself wrote, out of [`Self::created`], which lives and dies with the
2171 /// process.
2172 async fn board_issues(&self) -> Result<Vec<Resolved>, SourceError> {
2173 let mut after: Option<String> = None;
2174 let mut found = Vec::new();
2175 let search = self.board_search(None);
2176 loop {
2177 let (page, next) = self
2178 .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
2179 .await?;
2180 found.extend(page);
2181 match next {
2182 Some(next) => after = Some(next),
2183 None => break,
2184 }
2185 }
2186 self.completed_with_written(found, |_| true)
2187 }
2188
2189 /// `found`, with everything this run wrote that `keep` accepts and the read did not
2190 /// report.
2191 ///
2192 /// See [`Self::created`] and [`Self::board_issues`] for why a read has to be completed
2193 /// at all: the search index is behind, and a node read of an item filed moments ago can
2194 /// be too.
2195 fn completed_with_written(
2196 &self,
2197 mut found: Vec<Resolved>,
2198 keep: impl Fn(&Resolved) -> bool,
2199 ) -> Result<Vec<Resolved>, SourceError> {
2200 for own in self.created()?.iter().filter(|own| keep(own)) {
2201 if !found.iter().any(|item| item.id == own.id) {
2202 found.push(own.clone());
2203 }
2204 }
2205 Ok(found)
2206 }
2207
2208 /// What resolving one node id reached.
2209 ///
2210 /// Three answers rather than an `Option`, because a board *draft* is none of the other
2211 /// two: it is not an issue, it has no node of its own this source can read the board
2212 /// half off, and its only home is the board's own item connection — so a read of one
2213 /// is completed from there rather than reported as nothing.
2214 async fn reach(&self, id: &NativeId) -> Result<Reached, SourceError> {
2215 let asked = self
2216 .graphql(
2217 graphql::ISSUE,
2218 json!({"id":id.0,"nestedFirst":NESTED_PAGE_SIZE,
2219 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
2220 )
2221 .await;
2222 let data = match asked {
2223 Ok(data) => data,
2224 // A string that is not a node id at all is not a failure to report: it is an id
2225 // this board does not hold, which is what every read of one already answers.
2226 Err(error) if unresolvable_node(&error) => return Ok(Reached::Nothing),
2227 Err(error) => return Err(error),
2228 };
2229 let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
2230 return Ok(Reached::Nothing);
2231 };
2232 if optional_str(node, "__typename")? == Some("DraftIssue") {
2233 return Ok(Reached::Draft);
2234 }
2235 Ok(match self.resolve_issue(node).await? {
2236 Some(item) => Reached::Held(Box::new(item)),
2237 None => Reached::Nothing,
2238 })
2239 }
2240
2241 /// One item of this board by its own id, whatever kind it is.
2242 ///
2243 /// Resolved from the identifier alone: no search, board-wide or otherwise. What this
2244 /// run wrote is read first, because a node read of an item created moments ago can
2245 /// still be behind the board field values written onto it — see [`Self::created`].
2246 async fn item_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
2247 if let Some(own) = self.created()?.iter().find(|own| own.id == *id) {
2248 return Ok(Some(own.clone()));
2249 }
2250 match self.reach(id).await? {
2251 Reached::Held(item) => Ok(Some(*item)),
2252 Reached::Nothing => Ok(None),
2253 // The one read that still costs the board: a draft lives nowhere else.
2254 Reached::Draft => Ok(self
2255 .board()
2256 .await?
2257 .items
2258 .into_iter()
2259 .find(|item| item.id == *id)),
2260 }
2261 }
2262
2263 /// Everything filed under one issue of this board, walked to exhaustion — or `None`
2264 /// when that id names nothing here with a sub-issue relationship to walk.
2265 ///
2266 /// `None` and an empty answer are different: `None` is *this is not an issue of this
2267 /// GitHub*, which is what sends a project selector on to be read as a name, and an
2268 /// empty vector is a project that holds nothing.
2269 async fn sub_issues(&self, id: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
2270 let mut after: Option<String> = None;
2271 let mut children = Vec::new();
2272 loop {
2273 let asked = self
2274 .graphql(
2275 graphql::SUB_ISSUES,
2276 json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after,
2277 "nestedFirst":NESTED_PAGE_SIZE,
2278 "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
2279 )
2280 .await;
2281 let data = match asked {
2282 Ok(data) => data,
2283 // A string that is not a node id at all is not a failure to report: it is
2284 // the ordinary answer to a selector naming a project by its name.
2285 Err(error) if unresolvable_node(&error) => return Ok(None),
2286 Err(error) => return Err(error),
2287 };
2288 let Some(connection) = data
2289 .pointer("/node/subIssues")
2290 .filter(|value| !value.is_null())
2291 else {
2292 // No such node, or one with no sub-issue relationship — a board draft is
2293 // the one this board can really hold.
2294 return Ok(None);
2295 };
2296 for node in connection
2297 .get("nodes")
2298 .and_then(Value::as_array)
2299 .ok_or_else(|| SourceError::Malformed {
2300 message: "GitHub subIssues.nodes is not an array".into(),
2301 })?
2302 {
2303 if let Some(resolved) = self.resolve_issue(node).await? {
2304 children.push(resolved);
2305 }
2306 }
2307 let info = connection
2308 .get("pageInfo")
2309 .ok_or_else(|| SourceError::Malformed {
2310 message: "GitHub subIssues connection has no pageInfo".into(),
2311 })?;
2312 let next = required_bool(info, "hasNextPage")?
2313 .then(|| required_str(info, "endCursor"))
2314 .transpose()?;
2315 match next {
2316 Some(next) => {
2317 validate_cursor_progress(after.as_deref(), next)?;
2318 after = Some(next.to_owned());
2319 }
2320 None => return Ok(Some(children)),
2321 }
2322 }
2323 }
2324
2325 /// Which issue of this board a project *name* is, or `None` when none is.
2326 ///
2327 /// One bounded query which filters on that name at the server, rather than a walk of
2328 /// every issue the board holds. The name is compared again here: the qualifier narrows
2329 /// what GitHub sends, and this source decides what it names.
2330 async fn project_by_name(&self, name: &str) -> Result<Option<NativeId>, SourceError> {
2331 let search = self.board_search(Some(&title_qualifier(name)));
2332 let (candidates, _) = self.search_page(&search, MAX_PAGE_SIZE, None).await?;
2333 Ok(candidates
2334 .into_iter()
2335 .find(|item| {
2336 item.kind == BoardKind::Work(ItemKind::Project)
2337 && item.title.eq_ignore_ascii_case(name)
2338 })
2339 .map(|item| item.id))
2340 }
2341
2342 /// Everything filed under one project of this board: the sub-issues of the issue that
2343 /// project is.
2344 ///
2345 /// Tasks *and* documents, because a document filed under a project is a sub-issue of it
2346 /// too — the caller keeps the kind it asked for. Nothing about this grows as the board
2347 /// gains projects, or as another project gains tasks.
2348 ///
2349 /// A qualified id names the issue and is asked for its sub-issues directly: one
2350 /// request, no search of any kind. Only a selector GitHub cannot resolve that way is
2351 /// read as a project *name*, which costs the one bounded search
2352 /// [`Self::project_by_name`] makes.
2353 async fn project_children(&self, selector: &NativeId) -> Result<Vec<Resolved>, SourceError> {
2354 let (project, children) = match self.sub_issues(selector).await? {
2355 Some(children) => (selector.clone(), children),
2356 None => match self.project_by_name(&selector.0).await? {
2357 Some(project) => {
2358 let children = self.sub_issues(&project).await?.unwrap_or_default();
2359 (project, children)
2360 }
2361 None => return Ok(Vec::new()),
2362 },
2363 };
2364 self.completed_with_written(children, |own| own.parent.as_ref() == Some(&project))
2365 }
2366
2367 /// Every item on the board, with the one board identity they all share.
2368 ///
2369 /// See [`Self::board_cache`]. The completion from `created` happens on every call
2370 /// rather than once, which is what the cache could otherwise have broken.
2371 async fn board(&self) -> Result<Board, SourceError> {
2372 let cached = self.board_cache()?.clone();
2373 let mut board = match cached {
2374 Some(board) => board,
2375 None => {
2376 let read = self.read_board().await?;
2377 *self.board_cache()? = Some(read.clone());
2378 read
2379 }
2380 };
2381 for own in self.created()?.iter() {
2382 if !board.items.iter().any(|item| item.id == own.id) {
2383 board.items.push(own.clone());
2384 }
2385 }
2386 Ok(board)
2387 }
2388
2389 /// This process's own view of the board, or the refusal a poisoned lock is.
2390 fn board_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<Board>>, SourceError> {
2391 self.board_cache
2392 .lock()
2393 .map_err(|_| SourceError::Unavailable {
2394 message: "this source's view of the board was left inconsistent by an earlier \
2395 failure; next: run the command again"
2396 .into(),
2397 })
2398 }
2399
2400 /// Bring this process's own view of the board up to an item it has just written.
2401 ///
2402 /// A created item goes to `created`, which is what completes a board read GitHub's own
2403 /// eventual consistency has left behind. An item that was already there is replaced
2404 /// where it sits, so a second write of it in the same command reads its real parent
2405 /// rather than the one it had before the first write.
2406 ///
2407 /// "Where it sits" is two places, and missing the first leaves a stale record that
2408 /// wins: an item this same run created is held in `created` and not in the cached
2409 /// board, and `board` completes the cached board *from* `created`, so replacing only
2410 /// the cached copy of such an item replaces nothing and the read still reports the
2411 /// title it was created with.
2412 fn remember_written(&self, item: Resolved, created: bool) -> Result<(), SourceError> {
2413 if created {
2414 self.created()?.push(item);
2415 return Ok(());
2416 }
2417 {
2418 let mut own = self.created()?;
2419 if let Some(held) = own.iter_mut().find(|held| held.id == item.id) {
2420 *held = item;
2421 return Ok(());
2422 }
2423 }
2424 if let Some(board) = self.board_cache()?.as_mut()
2425 && let Some(held) = board.items.iter_mut().find(|held| held.id == item.id)
2426 {
2427 *held = item;
2428 }
2429 Ok(())
2430 }
2431
2432 /// Forget one item this process has just deleted, from both halves of its own view.
2433 fn forget(&self, id: &NativeId) -> Result<(), SourceError> {
2434 self.created()?.retain(|own| own.id != *id);
2435 if let Some(board) = self.board_cache()?.as_mut() {
2436 board.items.retain(|item| item.id != *id);
2437 }
2438 Ok(())
2439 }
2440
2441 /// Every page of the board, read from GitHub.
2442 async fn read_board(&self) -> Result<Board, SourceError> {
2443 let mut after: Option<String> = None;
2444 let mut items = Vec::new();
2445 let mut board;
2446 loop {
2447 let page = self.board_page(after.as_deref(), MAX_PAGE_SIZE).await?;
2448 for item in page
2449 .pointer("/items/nodes")
2450 .and_then(Value::as_array)
2451 .ok_or_else(|| SourceError::Malformed {
2452 message: "GitHub project items.nodes is not an array".into(),
2453 })?
2454 {
2455 if let Some(resolved) = self.resolve(item)? {
2456 items.push(resolved);
2457 }
2458 }
2459 let info = page
2460 .pointer("/items/pageInfo")
2461 .ok_or_else(|| SourceError::Malformed {
2462 message: "GitHub project items have no pageInfo".into(),
2463 })?;
2464 let has_next = required_bool(info, "hasNextPage")?;
2465 let next = has_next
2466 .then(|| required_str(info, "endCursor"))
2467 .transpose()?;
2468 board = page.clone();
2469 match next {
2470 Some(next) => {
2471 validate_cursor_progress(after.as_deref(), next)?;
2472 after = Some(next.to_owned());
2473 }
2474 None => break,
2475 }
2476 }
2477 Ok(Board {
2478 id: required_str(&board, "id")?.to_owned(),
2479 fields: board.get("fields").cloned().unwrap_or(Value::Null),
2480 items,
2481 })
2482 }
2483
2484 /// The items this source has created, for completing a board read that is behind.
2485 fn created(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
2486 self.created.lock().map_err(|_| SourceError::Unavailable {
2487 message: "this source's record of what it created in this run was left \
2488 inconsistent by an earlier failure; next: run the command again"
2489 .into(),
2490 })
2491 }
2492
2493 /// One board item as this source reports it, or `None` for content it ignores.
2494 ///
2495 /// A pull request is neither a project nor a task — it is somebody's change, not a
2496 /// unit of plan — and an item whose content the token cannot see has nothing to
2497 /// report at all.
2498 fn resolve(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
2499 let content = item.get("content").ok_or_else(|| SourceError::Malformed {
2500 message: "GitHub project item is missing content".into(),
2501 })?;
2502 if content.is_null() {
2503 return Ok(None);
2504 }
2505 let content_kind = match required_str(content, "__typename")? {
2506 "Issue" => ContentKind::Issue,
2507 "DraftIssue" => ContentKind::DraftIssue,
2508 _ => return Ok(None),
2509 };
2510 let field_values = item
2511 .get("fieldValues")
2512 .ok_or_else(|| SourceError::Malformed {
2513 message: "GitHub project item is missing fieldValues".into(),
2514 })?;
2515 complete_connection(field_values, "project item field values", NESTED_PAGE_SIZE)?;
2516 let nodes = field_values
2517 .get("nodes")
2518 .and_then(Value::as_array)
2519 .ok_or_else(|| SourceError::Malformed {
2520 message: "GitHub project item fieldValues.nodes is not an array".into(),
2521 })?;
2522 if let Some(labels) = content.get("labels") {
2523 complete_connection(labels, "content labels", NESTED_PAGE_SIZE)?;
2524 }
2525 let (body, slot) = metadata_body(optional_str(content, "body")?.map(str::to_owned))?;
2526 let parent = optional_str(content.get("parent").unwrap_or(&Value::Null), "id")?
2527 .map(|id| NativeId(id.to_owned()));
2528 // A draft has no sub-issues to summarise, and GitHub's schema gives it no field
2529 // to read one from; it is a task, and never a project.
2530 let sub_issues = match content_kind {
2531 ContentKind::Issue => sub_issue_total(content)?,
2532 ContentKind::DraftIssue => 0,
2533 };
2534 let content_id = required_str(content, "id")?;
2535 let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
2536 message: format!("GitHub issue {content_id}: {message}"),
2537 })?;
2538 let raw_title = required_str(content, "title")?;
2539 // The design prefix is read *first*, before either of the two rules that separate
2540 // a project from a task. A document is not work whatever sub-issues it has and
2541 // whatever marker it carries, and reading the prefix later would make a design
2542 // issue with none of either an empty project.
2543 let kind = if raw_title.starts_with(DESIGN_TITLE_PREFIX) {
2544 BoardKind::Document
2545 } else if parent.is_some() {
2546 // Being a sub-issue wins outright, and no marker overrides it: an issue filed
2547 // under a project is that project's task even when it has sub-issues of its
2548 // own.
2549 BoardKind::Work(ItemKind::Task)
2550 } else if sub_issues > 0 || marked == Some(ItemKind::Project) {
2551 BoardKind::Work(ItemKind::Project)
2552 } else {
2553 BoardKind::Work(ItemKind::Task)
2554 };
2555 // The title a person wrote, which for a document is the one without the prefix —
2556 // the same way `content` above is the body without this source's metadata slot.
2557 let title = match kind {
2558 BoardKind::Document => raw_title[DESIGN_TITLE_PREFIX.len()..].to_owned(),
2559 BoardKind::Work(_) => raw_title.to_owned(),
2560 };
2561 let own_repository = content
2562 .pointer("/repository/nameWithOwner")
2563 .and_then(Value::as_str)
2564 .map(|origin| Repository::try_from(format!("github.com/{origin}")))
2565 .transpose()
2566 .map_err(|message| SourceError::Malformed { message })?;
2567 let repositories = if slot.contains_key(Repository::METADATA_KEY) {
2568 Repository::from_metadata(&slot)
2569 .map_err(|message| SourceError::Malformed { message })?
2570 } else {
2571 own_repository.clone().into_iter().collect()
2572 };
2573 Ok(Some(Resolved {
2574 item_id: required_str(item, "id")?.to_owned(),
2575 id: NativeId(content_id.to_owned()),
2576 content_kind,
2577 kind,
2578 title,
2579 body: body.filter(|value| !value.is_empty()),
2580 status: self.status(item, content)?,
2581 labels: labels(content)?,
2582 parent,
2583 origin: text_field(nodes, ORIGIN_FIELD)?.filter(|value| !value.is_empty()),
2584 url: optional_str(content, "url")?.map(str::to_owned),
2585 created_at: optional_time(content, "createdAt")?,
2586 updated_at: optional_time(content, "updatedAt")?,
2587 own_repository,
2588 repositories,
2589 slot,
2590 }))
2591 }
2592
2593 /// The status one board item reports.
2594 ///
2595 /// The closed state decides the category and the `Status` option decides the name, so
2596 /// a closed issue sitting in a "Shipped" column reports `done` named `Shipped`. A
2597 /// closed issue whose reason is `DUPLICATE` or `REOPENED` reports `Unknown`: a
2598 /// duplicate is not finished work, and calling it done is a lie the next copy would
2599 /// write back. `REOPENED`-while-closed is a state this source can never produce, so
2600 /// it is read permissively rather than refused — reads are faithful, and refusals
2601 /// belong on writes.
2602 fn status(&self, item: &Value, content: &Value) -> Result<Status, SourceError> {
2603 let nodes = item
2604 .pointer("/fieldValues/nodes")
2605 .and_then(Value::as_array)
2606 .expect("resolve validates fieldValues.nodes before mapping status");
2607 let option = nodes
2608 .iter()
2609 .find(|value| value.pointer("/field/name").and_then(Value::as_str) == Some("Status"))
2610 .map(|value| required_str(value, "name"))
2611 .transpose()?;
2612 let state = optional_str(content, "state")?;
2613 if state == Some("CLOSED") {
2614 let category = match optional_str(content, "stateReason")? {
2615 None | Some("COMPLETED") => StatusCategory::Done,
2616 Some("NOT_PLANNED") => StatusCategory::Cancelled,
2617 Some(_) => StatusCategory::Unknown,
2618 };
2619 let fallback = match category {
2620 StatusCategory::Done => "Done",
2621 StatusCategory::Cancelled => "Cancelled",
2622 _ => "Closed",
2623 };
2624 return Ok(Status {
2625 category,
2626 name: option.unwrap_or(fallback).to_owned(),
2627 });
2628 }
2629 let name = option.unwrap_or("Open").to_owned();
2630 Ok(Status {
2631 category: self
2632 .statuses
2633 .category_of(&name)
2634 .unwrap_or(StatusCategory::Unknown),
2635 name,
2636 })
2637 }
2638
2639 /// The board Status option this write selects, or the refusal that says why not.
2640 ///
2641 /// For a column target the option is what the status *is*, so a board that has no such
2642 /// option is a refusal naming the status and the instance. For a closed target the
2643 /// issue's own state carries the category, and the option carries only the name a
2644 /// reader reports — so an option spelled the way this status is spelled is selected
2645 /// when the board has one, and nothing is refused when it does not.
2646 fn column_for(
2647 &self,
2648 board: &Board,
2649 status: &Status,
2650 target: &StatusTarget,
2651 ) -> Result<Option<(String, String)>, SourceError> {
2652 let (wanted, required) = match target {
2653 StatusTarget::Column(wanted) => (wanted.as_str(), true),
2654 StatusTarget::Closed(_) => (status.name.as_str(), false),
2655 StatusTarget::Disabled => return Ok(None),
2656 };
2657 let missing = |detail: &str| SourceError::Refused {
2658 message: format!(
2659 "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",
2660 category_name(status.category),
2661 self.name,
2662 category_name(status.category)
2663 ),
2664 };
2665 let Some(field) = Board::field(&board.fields, "Status")? else {
2666 return if required {
2667 Err(missing("this board has no Status field"))
2668 } else {
2669 Ok(None)
2670 };
2671 };
2672 if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
2673 return if required {
2674 Err(missing(
2675 "this board's Status field is not a single-select field",
2676 ))
2677 } else {
2678 Ok(None)
2679 };
2680 }
2681 let option = field
2682 .get("options")
2683 .and_then(Value::as_array)
2684 .and_then(|options| {
2685 options.iter().find(|option| {
2686 option
2687 .get("name")
2688 .and_then(Value::as_str)
2689 .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
2690 })
2691 });
2692 match option {
2693 None if required => Err(missing("this board does not have it")),
2694 None => Ok(None),
2695 Some(option) => Ok(Some((
2696 required_str(field, "id")?.to_owned(),
2697 required_str(option, "id")?.to_owned(),
2698 ))),
2699 }
2700 }
2701
2702 /// This instance's target for a category, refusing one it has disabled.
2703 ///
2704 /// Nothing here mutates the board's option set to make room for a status. GitHub
2705 /// documents `UpdateProjectV2FieldInput.singleSelectOptions` as *"provided values
2706 /// overwrite existing options"*, so no addition is additive and a mistake destroys the
2707 /// field and every item's status.
2708 fn resolved_target(&self, category: StatusCategory) -> Result<StatusTarget, SourceError> {
2709 let target = self.statuses.target(category).clone();
2710 if target != StatusTarget::Disabled {
2711 return Ok(target);
2712 }
2713 Err(SourceError::Refused {
2714 message: if category == StatusCategory::Draft {
2715 format!(
2716 "status draft is disabled for source {}: draft is incompatible with this \
2717 integration because GitHub draft issues cannot have sub-issues, and this \
2718 source stores a project's tasks as its issue's sub-issues",
2719 self.name
2720 )
2721 } else {
2722 format!(
2723 "status {} is disabled for source {}; set status_mapping.{} of this source \
2724 to a board Status option name or to a closed state",
2725 category_name(category),
2726 self.name,
2727 category_name(category)
2728 )
2729 },
2730 })
2731 }
2732
2733 async fn set_item_field(
2734 &self,
2735 board_id: &str,
2736 item_id: &str,
2737 field_id: &str,
2738 value: Value,
2739 ) -> Result<(), SourceError> {
2740 let data = self
2741 .graphql(
2742 graphql::UPDATE_FIELD,
2743 json!({"input":{
2744 "projectId":board_id,"itemId":item_id,"fieldId":field_id,"value":value
2745 }}),
2746 )
2747 .await?;
2748 let returned = data
2749 .pointer("/updateProjectV2ItemFieldValue/projectV2Item")
2750 .ok_or_else(|| SourceError::Malformed {
2751 message: "GitHub field update returned no project item".into(),
2752 })?;
2753 if required_str(returned, "id")? != item_id {
2754 return Err(SourceError::Malformed {
2755 message: "GitHub field update returned the wrong project item".into(),
2756 });
2757 }
2758 Ok(())
2759 }
2760
2761 async fn native_dependency_ids(&self, id: &NativeId) -> Result<Vec<String>, SourceError> {
2762 let mut after: Option<String> = None;
2763 let mut ids = Vec::new();
2764 loop {
2765 let data = self
2766 .graphql(
2767 graphql::ISSUE_DEPENDENCIES,
2768 json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after}),
2769 )
2770 .await?;
2771 let connection =
2772 data.pointer("/node/blockedBy")
2773 .ok_or_else(|| SourceError::Malformed {
2774 message: "GitHub dependency response has no blockedBy connection".into(),
2775 })?;
2776 ids.extend(
2777 connection
2778 .get("nodes")
2779 .and_then(Value::as_array)
2780 .ok_or_else(|| SourceError::Malformed {
2781 message: "GitHub dependency response nodes is not an array".into(),
2782 })?
2783 .iter()
2784 .map(|value| required_str(value, "id").map(str::to_owned))
2785 .collect::<Result<Vec<_>, _>>()?,
2786 );
2787 let next = next_cursor(connection)?;
2788 if let Some(next) = &next {
2789 validate_cursor_progress(after.as_deref(), &next.0)?;
2790 }
2791 after = next.map(|cursor| cursor.0);
2792 if after.is_none() {
2793 return Ok(ids);
2794 }
2795 }
2796 }
2797
2798 async fn dependencies(
2799 &self,
2800 id: &NativeId,
2801 near_kind: ItemKind,
2802 direction: Direction,
2803 page: &PageRequest,
2804 ) -> Result<Page<DependencyEdge>, SourceError> {
2805 validate_page(page)?;
2806 let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
2807 let cursor = page.cursor.as_ref().map(|c| c.0.as_str());
2808 let recorded = recorded_offset(cursor, direction)?;
2809 // Asked for even in the recorded phase, whose page reads nothing from the
2810 // connection: `__typename` is what says whether this item has a native
2811 // relationship at all, and that is what decides which far ends the reserved key is
2812 // allowed to hold.
2813 let data = self
2814 .graphql(
2815 graphql::ISSUE_DEPENDENCIES,
2816 json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),
2817 "after":if recorded.is_some() {None} else {cursor}}),
2818 )
2819 .await?;
2820 let node =
2821 data.get("node")
2822 .filter(|v| !v.is_null())
2823 .ok_or_else(|| SourceError::Refused {
2824 message: format!(
2825 "GitHub item {} was not found or does not support dependencies",
2826 id.0
2827 ),
2828 })?;
2829 let connection_name = match direction {
2830 Direction::DependsOn => "blockedBy",
2831 Direction::DependedOnBy => "blocking",
2832 };
2833 // A draft has neither `blockedBy` nor `blocking`, so nothing it depends on can be
2834 // named natively and the reserved key may hold any far end. An issue's connections
2835 // hold issues, and this source reads them at the near item's own level.
2836 let natively_names = (required_str(node, "__typename")? == "Issue").then_some(near_kind);
2837 if let Some(offset) = recorded {
2838 return Ok(recorded_page(
2839 self.recorded_edges(id, near_kind, direction, natively_names)
2840 .await?,
2841 offset,
2842 limit,
2843 ));
2844 }
2845 if natively_names.is_none() {
2846 return Ok(recorded_page(
2847 self.recorded_edges(id, near_kind, direction, natively_names)
2848 .await?,
2849 0,
2850 limit,
2851 ));
2852 }
2853 let connection = node
2854 .get(connection_name)
2855 .ok_or_else(|| SourceError::Malformed {
2856 message: "GitHub dependency response is missing its connection".into(),
2857 })?;
2858 let nodes = connection
2859 .get("nodes")
2860 .and_then(Value::as_array)
2861 .ok_or_else(|| SourceError::Malformed {
2862 message: "GitHub dependency response nodes is not an array".into(),
2863 })?;
2864 // `from` depends on `to`, always. GitHub spells the same relationship from either
2865 // end — `blockedBy` lists what this item waits on, `blocking` lists what waits on
2866 // it — so the near item is `from` in one direction and `to` in the other.
2867 let items = nodes
2868 .iter()
2869 .map(|value| {
2870 let related = NativeId(required_str(value, "id")?.into());
2871 let related_kind = related_kind(value)?;
2872 let (from, to) = match direction {
2873 Direction::DependsOn => (
2874 DependencyEndpoint::from_native(id.clone(), near_kind),
2875 DependencyEndpoint::from_native(related, related_kind),
2876 ),
2877 Direction::DependedOnBy => (
2878 DependencyEndpoint::from_native(related, related_kind),
2879 DependencyEndpoint::from_native(id.clone(), near_kind),
2880 ),
2881 };
2882 Ok(DependencyEdge {
2883 from,
2884 to,
2885 kind: DependencyKind::Blocks,
2886 })
2887 })
2888 .collect::<Result<Vec<_>, SourceError>>()?;
2889 let mut next = next_cursor(connection)?;
2890 if let Some(next) = &next {
2891 validate_cursor_progress(cursor, &next.0)?;
2892 }
2893 if next.is_none()
2894 && !self
2895 .recorded_edges(id, near_kind, direction, natively_names)
2896 .await?
2897 .is_empty()
2898 {
2899 next = Some(Cursor(format!("{RECORDED_CURSOR}0")));
2900 }
2901 Ok(Page { items, next })
2902 }
2903
2904 /// The edges this item records under [`DependencyEdge::RECORDED_KEY`], which is where
2905 /// a far end in another source has to live: no GitHub issue relationship can name one.
2906 ///
2907 /// Only forwards. The reverse of a recorded edge is derived from the far end, and this
2908 /// source never writes one down.
2909 ///
2910 /// The metadata lives in the item's own body slot, so reading it costs one board scan.
2911 /// That is why it happens once the native connection is spent rather than on every
2912 /// page.
2913 async fn recorded_edges(
2914 &self,
2915 id: &NativeId,
2916 near_kind: ItemKind,
2917 direction: Direction,
2918 natively_names: Option<ItemKind>,
2919 ) -> Result<Vec<DependencyEdge>, SourceError> {
2920 if direction != Direction::DependsOn {
2921 return Ok(Vec::new());
2922 }
2923 let Some(item) = self
2924 .board()
2925 .await?
2926 .items
2927 .into_iter()
2928 .find(|item| item.id == *id)
2929 else {
2930 return Ok(Vec::new());
2931 };
2932 DependencyEdge::recorded(&item.slot, id, near_kind, &self.name, natively_names)
2933 .map_err(|message| SourceError::Malformed { message })
2934 }
2935
2936 /// The configured repository's node id, or the refusal naming the field it needs.
2937 ///
2938 /// Resolved once per command; see [`Self::repository_cache`].
2939 async fn repository_id(&self) -> Result<String, SourceError> {
2940 if let Some(id) = self.repository_cache()?.clone() {
2941 return Ok(id);
2942 }
2943 let repository = self
2944 .repository
2945 .as_ref()
2946 .ok_or_else(|| SourceError::Refused {
2947 message: format!(
2948 "source {} has no repository configured, and a GitHub Projects board has no \
2949 repository of its own to create an issue in; set repository: owner/name on \
2950 this source",
2951 self.name
2952 ),
2953 })?;
2954 let data = self
2955 .graphql(
2956 graphql::REPOSITORY,
2957 json!({"owner":repository.owner,"name":repository.name}),
2958 )
2959 .await?;
2960 let node = data
2961 .get("repository")
2962 .filter(|value| !value.is_null())
2963 .ok_or_else(|| SourceError::Refused {
2964 message: format!(
2965 "GitHub repository {}/{} was not found or is not visible to the token",
2966 repository.owner, repository.name
2967 ),
2968 })?;
2969 let id = required_str(node, "id")?.to_owned();
2970 *self.repository_cache()? = Some(id.clone());
2971 Ok(id)
2972 }
2973
2974 /// This process's own record of the destination repository's node id.
2975 fn repository_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<String>>, SourceError> {
2976 self.repository_cache
2977 .lock()
2978 .map_err(|_| SourceError::Unavailable {
2979 message: "this source's record of the destination repository was left \
2980 inconsistent by an earlier failure; next: run the command again"
2981 .into(),
2982 })
2983 }
2984
2985 /// Create or update one board item, whichever kind it is.
2986 async fn write_item(
2987 &self,
2988 incoming: &Incoming<'_>,
2989 target: Option<&NativeId>,
2990 depends_on: &[DependencyEdge],
2991 ) -> Result<NativeId, SourceError> {
2992 // Refused before anything is read or written: a task or a project titled the way
2993 // this board spells a document would land as an issue this same source reads back
2994 // as a document, so the field this destination cannot carry is named rather than
2995 // written and silently reclassified.
2996 if let Written::Work(kind, _) = incoming.written
2997 && incoming.title.starts_with(DESIGN_TITLE_PREFIX)
2998 {
2999 return Err(SourceError::Refused {
3000 message: format!(
3001 "the title of this {} begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
3002 spells a document, so it would read back as one rather than as a {}; \
3003 retitle it, or copy it as a document",
3004 kind.marker(),
3005 self.name,
3006 kind.marker()
3007 ),
3008 });
3009 }
3010 let board = self.board().await?;
3011 let status_target = incoming
3012 .written
3013 .status()
3014 .map(|status| self.resolved_target(status.category))
3015 .transpose()?;
3016 let column = match (incoming.written.status(), status_target.as_ref()) {
3017 (Some(status), Some(target)) => self.column_for(&board, status, target)?,
3018 _ => None,
3019 };
3020 let existing = target
3021 .map(|target| {
3022 board
3023 .items
3024 .iter()
3025 .find(|item| item.id == *target)
3026 .ok_or_else(|| SourceError::Refused {
3027 message: format!("GitHub destination item {} was not found", target.0),
3028 })
3029 })
3030 .transpose()?;
3031 let content_kind = existing.map_or(ContentKind::Issue, |item| item.content_kind);
3032 if content_kind == ContentKind::DraftIssue {
3033 if let (Some(StatusTarget::Closed(_)), Some(status)) =
3034 (status_target.as_ref(), incoming.written.status())
3035 {
3036 return Err(SourceError::Refused {
3037 message: format!(
3038 "status {} of source {} closes the item's issue, and GitHub draft items \
3039 have no open or closed state",
3040 category_name(status.category),
3041 self.name
3042 ),
3043 });
3044 }
3045 if incoming.parent.is_some() {
3046 return Err(SourceError::Refused {
3047 message: "GitHub draft items cannot be a project's sub-issue".into(),
3048 });
3049 }
3050 }
3051 match existing {
3052 Some(item) if content_kind == ContentKind::Issue => {
3053 if item.labels != incoming.labels {
3054 return Err(SourceError::Refused {
3055 message: "GitHub issue labels differ from the labels being written".into(),
3056 });
3057 }
3058 }
3059 _ => {
3060 if !incoming.labels.is_empty() {
3061 return Err(SourceError::Refused {
3062 message: "GitHub items created by this destination carry no labels".into(),
3063 });
3064 }
3065 }
3066 }
3067
3068 let own_repository = match existing {
3069 Some(item) => item.own_repository.clone(),
3070 None => self
3071 .repository
3072 .as_ref()
3073 .map(|repository| Repository::try_from(repository.origin()))
3074 .transpose()
3075 .map_err(|message| SourceError::Config { message })?,
3076 };
3077 let (native, fallback) = self
3078 .partition_edges(&board, incoming.written.kind(), content_kind, depends_on)
3079 .await?;
3080 let slot = slot_metadata(incoming, own_repository.as_ref(), &fallback);
3081 let body = compose_body(incoming.content, &slot)?;
3082 // Read before anything is created, for the reason the field below is: a value
3083 // this destination cannot store has to refuse, and refusing after `createIssue`
3084 // would leave an issue behind that nothing asked for. The engine writes a
3085 // qualified id here; a caller handing this key anything else is told so rather
3086 // than having it silently stored as no origin at all.
3087 // 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.
3088 let origin = match incoming.metadata.get(ORIGIN_KEY) {
3089 None => "",
3090 Some(Value::String(origin)) => origin.as_str(),
3091 Some(other) => {
3092 return Err(SourceError::Refused {
3093 message: format!(
3094 "{ORIGIN_KEY} holds a qualified id spelled as a string, and this item's \
3095 is {other}"
3096 ),
3097 });
3098 }
3099 };
3100 // Resolved before anything is created: a board that cannot carry the copy origin
3101 // has to refuse the write, and refusing it after `createIssue` would leave an
3102 // issue behind that nothing asked for.
3103 let origin_field = match Board::field(&board.fields, ORIGIN_FIELD)? {
3104 Some(field) => {
3105 if required_str(field, "__typename")? != "ProjectV2Field" {
3106 return Err(SourceError::Refused {
3107 message: format!(
3108 "GitHub board source-owned {ORIGIN_FIELD} field is not a text field"
3109 ),
3110 });
3111 }
3112 Some(required_str(field, "id")?.to_owned())
3113 }
3114 None if incoming.metadata.contains_key(ORIGIN_KEY) => {
3115 return Err(SourceError::Refused {
3116 message: format!(
3117 "GitHub board has no source-owned {ORIGIN_FIELD} text field, and the \
3118 item carries {ORIGIN_KEY}; add a text field named {ORIGIN_FIELD} to \
3119 the board"
3120 ),
3121 });
3122 }
3123 None => None,
3124 };
3125
3126 let (content_id, item_id, url) = match existing {
3127 Some(item) => {
3128 self.update_existing(item, incoming, &body, status_target.as_ref())
3129 .await?;
3130 (item.id.clone(), item.item_id.clone(), item.url.clone())
3131 }
3132 None => {
3133 self.create_and_file_issue(&board, incoming, &body, status_target.as_ref())
3134 .await?
3135 }
3136 };
3137
3138 // Creating an item here is several calls — `createIssue`, `addProjectV2ItemById`,
3139 // then each board field, the parent and the dependencies — and GitHub can fail at
3140 // any of them. Everything this source can refuse *before* the first of those is
3141 // already checked above, so what is left is GitHub itself failing part way. When it
3142 // does over an item this call created, the issue is taken back: a write that
3143 // refused must not leave an item behind that nobody asked for, and one that does
3144 // makes the retry create a second.
3145 let landed = self
3146 .finish_write(
3147 &board,
3148 incoming,
3149 &content_id,
3150 &item_id,
3151 content_kind,
3152 existing,
3153 origin_field.as_deref(),
3154 origin,
3155 column,
3156 &native,
3157 )
3158 .await;
3159 if let Err(error) = landed {
3160 if existing.is_none() {
3161 // Best effort, and the write's own failure is what the caller is told: a
3162 // refusal naming the tidy-up would hide why the write failed at all.
3163 let _ = self.delete_issue(&content_id).await;
3164 }
3165 return Err(error);
3166 }
3167
3168 // So the rest of this command reads what it just did rather than what the board
3169 // said before it. See `remember_written` for which half takes it.
3170 let remembered = Resolved {
3171 item_id,
3172 id: content_id.clone(),
3173 content_kind,
3174 kind: incoming.written.kind(),
3175 title: incoming.title.to_owned(),
3176 // The visible half of the body this write composed, split back off it the
3177 // way a read splits it — so what this record reports is what a read of the
3178 // same issue reports, rather than the person's text with the metadata slot
3179 // still on the end of it.
3180 body: metadata_body(body.clone())?.0,
3181 // A document has no status of its own; what it reads back as is whatever
3182 // the issue's own state says, which is what a re-read reports.
3183 status: incoming
3184 .written
3185 .status()
3186 .cloned()
3187 .unwrap_or_else(|| Status {
3188 category: StatusCategory::Unknown,
3189 name: "Open".to_owned(),
3190 }),
3191 labels: incoming.labels.to_vec(),
3192 parent: incoming.parent.cloned(),
3193 origin: (!origin.is_empty()).then(|| origin.to_owned()),
3194 // In the update path this is the item's own url, read off `existing` where the
3195 // tuple above was bound, so one expression serves both halves.
3196 url,
3197 created_at: existing.and_then(|item| item.created_at),
3198 updated_at: existing.and_then(|item| item.updated_at),
3199 own_repository,
3200 repositories: incoming.repositories.to_vec(),
3201 slot,
3202 };
3203 self.remember_written(remembered, existing.is_none())?;
3204 Ok(content_id)
3205 }
3206
3207 /// Everything a write does after the item exists: its board fields, its parent, and
3208 /// its dependencies.
3209 ///
3210 /// Split out of `write_item` so there is one place a failure past the point of no
3211 /// return is caught, rather than a tidy-up repeated at each `?` above.
3212 // llmlint: ignore[suppressions_justified] This is the tail of `write_item` lifted out
3213 // so there is one place a failure past the point of no return is caught, and its
3214 // arguments are exactly the values that tail already had in scope. Bundling them into a
3215 // struct would describe no concept — it would be "the arguments of this function" — and
3216 // would put the whole of `write_item`'s locals behind one more indirection.
3217 #[allow(clippy::too_many_arguments)]
3218 async fn finish_write(
3219 &self,
3220 board: &Board,
3221 incoming: &Incoming<'_>,
3222 content_id: &NativeId,
3223 item_id: &str,
3224 content_kind: ContentKind,
3225 existing: Option<&Resolved>,
3226 origin_field: Option<&str>,
3227 origin: &str,
3228 column: Option<(String, String)>,
3229 native: &[String],
3230 ) -> Result<(), SourceError> {
3231 if let Some(field_id) = origin_field {
3232 self.set_item_field(&board.id, item_id, field_id, json!({"text":origin}))
3233 .await?;
3234 }
3235
3236 if let Some((field_id, option_id)) = column {
3237 self.set_item_field(
3238 &board.id,
3239 item_id,
3240 &field_id,
3241 json!({"singleSelectOptionId":option_id}),
3242 )
3243 .await?;
3244 }
3245
3246 if content_kind == ContentKind::Issue {
3247 self.reparent(
3248 existing.and_then(|item| item.parent.clone()),
3249 content_id,
3250 incoming.parent,
3251 )
3252 .await?;
3253 // A document takes part in no dependency graph, so writing one neither reads
3254 // nor changes the issue's own `blockedBy` relationships. Reconciling them
3255 // against the empty list a document write carries would *delete* whatever
3256 // relationships a person had made on that issue, which is a write nobody
3257 // asked for.
3258 if incoming.written.kind() != BoardKind::Document {
3259 self.reconcile_blocked_by(content_id, native).await?;
3260 }
3261 }
3262 Ok(())
3263 }
3264
3265 /// Delete one issue, which takes its board item with it.
3266 async fn delete_issue(&self, id: &NativeId) -> Result<(), SourceError> {
3267 let data = self
3268 .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
3269 .await?;
3270 data.pointer("/deleteIssue/repository")
3271 .filter(|value| !value.is_null())
3272 .ok_or_else(|| SourceError::Malformed {
3273 message: "GitHub issue deletion returned no repository".into(),
3274 })?;
3275 self.forget(id)?;
3276 Ok(())
3277 }
3278
3279 /// Remove one item this copy created, so a copy that could not finish leaves the board
3280 /// as it found it.
3281 ///
3282 /// Deleting the issue takes its board item with it, so there is no second mutation to
3283 /// keep in step. An id the board does not hold is not an error: the item is already
3284 /// gone, which is the state this asks for.
3285 async fn delete_item(&self, id: &NativeId) -> Result<(), SourceError> {
3286 let board = self.board().await?;
3287 let Some(item) = board.items.iter().find(|item| item.id == *id) else {
3288 return Ok(());
3289 };
3290 if item.content_kind == ContentKind::DraftIssue {
3291 return Err(SourceError::Refused {
3292 message: format!(
3293 "GitHub item {} is a draft, and this source removes an item by deleting \
3294 its issue; next: remove it from the board by hand",
3295 id.0
3296 ),
3297 });
3298 }
3299 let data = self
3300 .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
3301 .await?;
3302 data.pointer("/deleteIssue/repository")
3303 .filter(|value| !value.is_null())
3304 .ok_or_else(|| SourceError::Malformed {
3305 message: "GitHub issue deletion returned no repository".into(),
3306 })?;
3307 self.forget(id)?;
3308 Ok(())
3309 }
3310
3311 /// Which far ends this item's own `blockedBy` relationship holds, and which it cannot.
3312 async fn partition_edges(
3313 &self,
3314 board: &Board,
3315 near_kind: BoardKind,
3316 near_content: ContentKind,
3317 depends_on: &[DependencyEdge],
3318 ) -> Result<(Vec<String>, Vec<DependencyEdge>), SourceError> {
3319 let mut native = Vec::new();
3320 let mut fallback = Vec::new();
3321 for edge in depends_on {
3322 let same_source = edge
3323 .to
3324 .source()
3325 .is_none_or(|source| source == self.name.as_str());
3326 // A qualified id's source segment runs to its *first* colon — `GlobalId` and
3327 // `DependencyEndpoint::source` both read it that way — and a native id may hold
3328 // colons of its own, so the far end is everything after that one separator.
3329 // Splitting at the last would truncate `work:urn:task:7` to `7`.
3330 let far_id = if edge.to.is_qualified() {
3331 edge.to
3332 .id()
3333 .split_once(':')
3334 .map_or(edge.to.id(), |(_, native)| native)
3335 } else {
3336 edge.to.id()
3337 };
3338 let far = if same_source {
3339 Some(
3340 board
3341 .items
3342 .iter()
3343 .find(|item| item.id.0 == far_id)
3344 .ok_or_else(|| SourceError::Refused {
3345 message: format!("GitHub dependency item {far_id} was not found"),
3346 })?,
3347 )
3348 } else {
3349 None
3350 };
3351 // The caller says which kind the far end is, and this board holds the far end
3352 // itself, so a disagreement is settled here rather than stored: recorded, the
3353 // wrong kind would read back as a cross-level edge that never existed; written
3354 // natively, it would name a relationship of a different level than the caller
3355 // asked for.
3356 //
3357 // A far end this board holds as a *document* fails the same comparison and is
3358 // refused by the same sentence: `ItemKind` has no document variant because
3359 // nothing may point at one, so no caller can name it correctly and the refusal
3360 // is the only honest answer.
3361 if let Some(disagreeing) = far.filter(|far| far.kind != BoardKind::Work(edge.to.kind)) {
3362 return Err(SourceError::Refused {
3363 message: format!(
3364 "GitHub dependency item {far_id} is a {} of this board, and this item \
3365 names it as a {}; record the kind it is",
3366 disagreeing.kind.describes(),
3367 edge.to.kind.marker()
3368 ),
3369 });
3370 }
3371 // A draft has neither `blockedBy` nor `blocking`, so no edge of one is native
3372 // however the far end is spelled — and one classified native here would be
3373 // written nowhere at all, because a draft's native reconciliation never runs.
3374 let native_here = near_content == ContentKind::Issue
3375 && far.is_some_and(|far| {
3376 far.content_kind == ContentKind::Issue
3377 && BoardKind::Work(edge.to.kind) == near_kind
3378 });
3379 if native_here {
3380 native.push(far_id.to_owned());
3381 } else {
3382 fallback.push(edge.clone());
3383 }
3384 }
3385 Ok((native, fallback))
3386 }
3387
3388 async fn update_existing(
3389 &self,
3390 item: &Resolved,
3391 incoming: &Incoming<'_>,
3392 body: &Option<String>,
3393 status_target: Option<&StatusTarget>,
3394 ) -> Result<(), SourceError> {
3395 let title = incoming.written_title();
3396 let (operation, input, pointer) = match item.content_kind {
3397 ContentKind::DraftIssue => (
3398 graphql::UPDATE_DRAFT,
3399 json!({"draftIssueId":item.id.0,"title":title,"body":body}),
3400 "/updateProjectV2DraftIssue/draftIssue",
3401 ),
3402 ContentKind::Issue => (
3403 graphql::UPDATE_ISSUE,
3404 json!({"id":item.id.0,"title":title,"body":body,
3405 "stateInput":state_input(status_target)}),
3406 "/updateIssue/issue",
3407 ),
3408 };
3409 let data = self.graphql(operation, json!({"input":input})).await?;
3410 let returned = data
3411 .pointer(pointer)
3412 .ok_or_else(|| SourceError::Malformed {
3413 message: "GitHub item update returned no item".into(),
3414 })?;
3415 if required_str(returned, "id")? != item.id.0 {
3416 return Err(SourceError::Malformed {
3417 message: "GitHub item update returned the wrong item".into(),
3418 });
3419 }
3420 Ok(())
3421 }
3422
3423 /// Creates one issue, files it on the board, and closes it when the status says so.
3424 ///
3425 /// Three calls rather than one: `createIssue` needs a repository and answers with an
3426 /// issue that is on no board, `addProjectV2ItemById` is what puts it there, and a
3427 /// closed status is a state of the issue rather than a field of the board item.
3428 /// Creates the issue, files it on the board, and reports what a read of it would say:
3429 /// its content id, its board item id, and the web address GitHub gave it.
3430 ///
3431 /// The address comes back here because this is the only place it is known before
3432 /// GitHub's own board read catches up — an item this run created answers the reads
3433 /// that follow it out of the record below, and one remembered without its address
3434 /// would report no location for the rest of the run.
3435 async fn create_and_file_issue(
3436 &self,
3437 board: &Board,
3438 incoming: &Incoming<'_>,
3439 body: &Option<String>,
3440 status_target: Option<&StatusTarget>,
3441 ) -> Result<(NativeId, String, Option<String>), SourceError> {
3442 let repository_id = self.repository_id().await?;
3443 let data = self
3444 .graphql(
3445 graphql::CREATE_ISSUE,
3446 json!({"input":{
3447 "repositoryId":repository_id,"title":incoming.written_title(),"body":body
3448 }}),
3449 )
3450 .await?;
3451 let created = data
3452 .pointer("/createIssue/issue")
3453 .filter(|value| !value.is_null())
3454 .ok_or_else(|| SourceError::Malformed {
3455 message: "GitHub issue creation returned no issue".into(),
3456 })?;
3457 let content_id = NativeId(required_str(created, "id")?.to_owned());
3458 // Optional although GitHub's schema makes it non-null: the issue exists by now, so
3459 // a response without it is not worth failing a landed write over — the item simply
3460 // reports no location until the board read catches up, which is what it did before.
3461 let url = optional_str(created, "url")?.map(str::to_owned);
3462 // The issue exists from here on, so a failure filing it on the board takes it
3463 // back: an issue in the repository that is on no board is an item nobody asked for
3464 // and nothing here would find again.
3465 let added = match self
3466 .graphql(
3467 graphql::ADD_TO_BOARD,
3468 json!({"input":{"projectId":board.id,"contentId":content_id.0}}),
3469 )
3470 .await
3471 {
3472 Ok(added) => added,
3473 Err(error) => {
3474 let _ = self.delete_issue(&content_id).await;
3475 return Err(error);
3476 }
3477 };
3478 let item = added
3479 .pointer("/addProjectV2ItemById/item")
3480 .filter(|value| !value.is_null())
3481 .ok_or_else(|| SourceError::Malformed {
3482 message: "GitHub board addition returned no project item".into(),
3483 })?;
3484 if let Some(StatusTarget::Closed(_)) = status_target {
3485 let closed = self
3486 .graphql(
3487 graphql::UPDATE_ISSUE,
3488 json!({"input":{"id":content_id.0,"stateInput":state_input(status_target)}}),
3489 )
3490 .await?;
3491 let returned =
3492 closed
3493 .pointer("/updateIssue/issue")
3494 .ok_or_else(|| SourceError::Malformed {
3495 message: "GitHub item update returned no item".into(),
3496 })?;
3497 if required_str(returned, "id")? != content_id.0 {
3498 return Err(SourceError::Malformed {
3499 message: "GitHub item update returned the wrong item".into(),
3500 });
3501 }
3502 }
3503 Ok((content_id, required_str(item, "id")?.to_owned(), url))
3504 }
3505
3506 /// Move one issue under the project it now belongs to, or out of the one it left.
3507 async fn reparent(
3508 &self,
3509 held: Option<NativeId>,
3510 child: &NativeId,
3511 wanted: Option<&NativeId>,
3512 ) -> Result<(), SourceError> {
3513 if held.as_ref() == wanted {
3514 return Ok(());
3515 }
3516 if let Some(held) = &held {
3517 self.sub_issue(graphql::REMOVE_SUB_ISSUE, held, child, "removeSubIssue")
3518 .await?;
3519 }
3520 if let Some(wanted) = wanted {
3521 self.sub_issue(graphql::ADD_SUB_ISSUE, wanted, child, "addSubIssue")
3522 .await?;
3523 }
3524 Ok(())
3525 }
3526
3527 async fn sub_issue(
3528 &self,
3529 operation: &str,
3530 parent: &NativeId,
3531 child: &NativeId,
3532 root: &str,
3533 ) -> Result<(), SourceError> {
3534 let data = self
3535 .graphql(
3536 operation,
3537 json!({"input":{"issueId":parent.0,"subIssueId":child.0}}),
3538 )
3539 .await?;
3540 let issue =
3541 data.pointer(&format!("/{root}/issue"))
3542 .ok_or_else(|| SourceError::Malformed {
3543 message: "GitHub sub-issue update returned no issue".into(),
3544 })?;
3545 let sub =
3546 data.pointer(&format!("/{root}/subIssue"))
3547 .ok_or_else(|| SourceError::Malformed {
3548 message: "GitHub sub-issue update returned no sub-issue".into(),
3549 })?;
3550 if required_str(issue, "id")? != parent.0 || required_str(sub, "id")? != child.0 {
3551 return Err(SourceError::Malformed {
3552 message: "GitHub sub-issue update returned the wrong issues".into(),
3553 });
3554 }
3555 Ok(())
3556 }
3557
3558 async fn reconcile_blocked_by(
3559 &self,
3560 content_id: &NativeId,
3561 native: &[String],
3562 ) -> Result<(), SourceError> {
3563 let current = self.native_dependency_ids(content_id).await?;
3564 for (operation, far_id) in current
3565 .iter()
3566 .filter(|id| !native.contains(id))
3567 .map(|id| (graphql::REMOVE_BLOCKED_BY, id))
3568 .chain(
3569 native
3570 .iter()
3571 .filter(|id| !current.contains(id))
3572 .map(|id| (graphql::ADD_BLOCKED_BY, id)),
3573 )
3574 {
3575 let data = self
3576 .graphql(
3577 operation,
3578 json!({"input":{"issueId":content_id.0,"blockingIssueId":far_id}}),
3579 )
3580 .await?;
3581 let root = if operation == graphql::ADD_BLOCKED_BY {
3582 "addBlockedBy"
3583 } else {
3584 "removeBlockedBy"
3585 };
3586 let issue =
3587 data.pointer(&format!("/{root}/issue"))
3588 .ok_or_else(|| SourceError::Malformed {
3589 message: "GitHub dependency update returned no issue".into(),
3590 })?;
3591 let blocker = data
3592 .pointer(&format!("/{root}/blockingIssue"))
3593 .ok_or_else(|| SourceError::Malformed {
3594 message: "GitHub dependency update returned no blocking issue".into(),
3595 })?;
3596 if required_str(issue, "id")? != content_id.0 || required_str(blocker, "id")? != far_id
3597 {
3598 return Err(SourceError::Malformed {
3599 message: "GitHub dependency update returned the wrong issues".into(),
3600 });
3601 }
3602 }
3603 Ok(())
3604 }
3605}
3606
3607/// What resolving one node id reached; see [`GitHubProjectsSource::reach`].
3608enum Reached {
3609 /// An issue this board holds, resolved into everything this source reports about it.
3610 Held(Box<Resolved>),
3611 /// Nothing this board holds: no such node, or a node on some other board.
3612 Nothing,
3613 /// A board draft, which exists only inside the board's own item connection.
3614 Draft,
3615}
3616
3617/// What GitHub says when a string is not a node id it can resolve.
3618///
3619/// Matched because it is the ordinary answer to a project selector naming a project by its
3620/// *name*, and reporting that as a failure would make naming one impossible. It is read
3621/// off the refusal GitHub sent, never guessed from the shape of the string: this source
3622/// does not define the syntax of a GitHub node id and would be wrong about it.
3623const UNRESOLVABLE_NODE: &str = "could not resolve to a node";
3624
3625/// Whether this refusal is GitHub saying the id names no node at all.
3626fn unresolvable_node(error: &SourceError) -> bool {
3627 matches!(error, SourceError::Refused { message }
3628 if message.to_ascii_lowercase().contains(UNRESOLVABLE_NODE))
3629}
3630
3631/// One project name, as a search qualifier which filters on it at the server.
3632///
3633/// Quoted so the whole title is one phrase rather than a bag of words, with the two
3634/// characters GitHub's own quoting grammar gives a meaning inside a quoted phrase escaped
3635/// the way it documents. A title matched here is still compared for equality afterwards:
3636/// the qualifier narrows what the server sends, and this source decides what it names.
3637fn title_qualifier(name: &str) -> String {
3638 let escaped = name.replace('\\', "\\\\").replace('"', "\\\"");
3639 format!("in:title \"{escaped}\"")
3640}
3641
3642/// The board, and every item on it this source reports.
3643#[derive(Clone)]
3644struct Board {
3645 id: String,
3646 fields: Value,
3647 items: Vec<Resolved>,
3648}
3649
3650impl Board {
3651 fn field<'a>(fields: &'a Value, name: &str) -> Result<Option<&'a Value>, SourceError> {
3652 complete_connection(fields, "project fields", NESTED_PAGE_SIZE)?;
3653 let nodes = fields
3654 .get("nodes")
3655 .and_then(Value::as_array)
3656 .ok_or_else(|| SourceError::Malformed {
3657 message: "GitHub project fields.nodes is not an array".into(),
3658 })?;
3659 Ok(nodes
3660 .iter()
3661 .find(|field| field.get("name").and_then(Value::as_str) == Some(name)))
3662 }
3663}
3664
3665/// One board item, resolved into everything this source reports about it.
3666#[derive(Clone)]
3667struct Resolved {
3668 item_id: String,
3669 id: NativeId,
3670 content_kind: ContentKind,
3671 kind: BoardKind,
3672 title: String,
3673 body: Option<String>,
3674 status: Status,
3675 labels: Vec<Label>,
3676 parent: Option<NativeId>,
3677 // 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.
3678 origin: Option<String>,
3679 url: Option<String>,
3680 created_at: Option<DateTime<Utc>>,
3681 updated_at: Option<DateTime<Utc>>,
3682 own_repository: Option<Repository>,
3683 repositories: Vec<Repository>,
3684 slot: BTreeMap<String, Value>,
3685}
3686
3687impl Resolved {
3688 /// The metadata a caller sees: their own keys, plus the copy origin this source keeps
3689 /// in a field of its own, and none of the three keys that are only an encoding.
3690 fn metadata(&self) -> BTreeMap<String, Value> {
3691 let mut metadata = self.slot.clone();
3692 metadata.remove(Repository::METADATA_KEY);
3693 metadata.remove(DependencyEdge::RECORDED_KEY);
3694 metadata.remove(ItemKind::METADATA_KEY);
3695 if let Some(origin) = &self.origin {
3696 metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin.clone()));
3697 }
3698 metadata
3699 }
3700
3701 /// Where this item is, as a link a reader can open.
3702 ///
3703 /// A board is a hosted place and every issue on it has a web address, so that address
3704 /// is what "where is this?" means here — and [`Location::Url`] is what says which kind
3705 /// of place it is, so a reader knows to open it rather than to read a file out. It
3706 /// does not replace or derive from `url`: the field goes on reporting exactly what it
3707 /// reported before, and this says what that address *is*.
3708 ///
3709 /// An item GitHub gave no `url` for — a draft has none — reports no location at all
3710 /// rather than a third variant, which is the contract's "the source did not say". An
3711 /// issue this run created is not one of those: its address comes back from the
3712 /// creating mutation, so it is somewhere a reader can open from the moment it exists
3713 /// rather than from whenever the board read catches up.
3714 fn location(&self) -> Option<Location> {
3715 self.url.clone().map(Location::Url)
3716 }
3717
3718 fn task(&self) -> Task {
3719 Task {
3720 id: self.id.clone(),
3721 title: self.title.clone(),
3722 content: self.body.clone(),
3723 status: self.status.clone(),
3724 labels: self.labels.clone(),
3725 project: self.parent.clone(),
3726 url: self.url.clone(),
3727 location: self.location(),
3728 created_at: self.created_at,
3729 updated_at: self.updated_at,
3730 metadata: self.metadata(),
3731 repositories: self.repositories.clone(),
3732 }
3733 }
3734
3735 fn project(&self) -> Project {
3736 Project {
3737 id: self.id.clone(),
3738 title: self.title.clone(),
3739 content: self.body.clone(),
3740 status: self.status.clone(),
3741 labels: self.labels.clone(),
3742 url: self.url.clone(),
3743 location: self.location(),
3744 created_at: self.created_at,
3745 updated_at: self.updated_at,
3746 metadata: self.metadata(),
3747 repositories: self.repositories.clone(),
3748 }
3749 }
3750
3751 /// The same issue as a document: the project it is filed under, and no status and no
3752 /// dependencies, because a document is not work.
3753 fn document(&self) -> Document {
3754 Document {
3755 id: self.id.clone(),
3756 title: self.title.clone(),
3757 content: self.body.clone(),
3758 project: self.parent.clone(),
3759 labels: self.labels.clone(),
3760 url: self.url.clone(),
3761 location: self.location(),
3762 created_at: self.created_at,
3763 updated_at: self.updated_at,
3764 metadata: self.metadata(),
3765 repositories: self.repositories.clone(),
3766 }
3767 }
3768}
3769
3770/// What one write is, and the status that comes with being it.
3771///
3772/// One value rather than a [`BoardKind`] beside an `Option<Status>`: a document has no
3773/// status and a task or a project always has one, so "a document carrying a status" and
3774/// "a task carrying none" are states a write cannot be in rather than states every use
3775/// site below has to defend against.
3776enum Written<'a> {
3777 /// A document, which is not work and so has no status at all.
3778 Document,
3779 /// A task or a project, and the status it is being written with.
3780 Work(ItemKind, &'a Status),
3781}
3782
3783impl Written<'_> {
3784 /// Which of the board's three kinds this write is.
3785 const fn kind(&self) -> BoardKind {
3786 match self {
3787 Self::Document => BoardKind::Document,
3788 Self::Work(kind, _) => BoardKind::Work(*kind),
3789 }
3790 }
3791
3792 /// The status this write carries. A document carries none, so a write of one says
3793 /// nothing about the issue's open or closed state and selects no board `Status`
3794 /// option.
3795 const fn status(&self) -> Option<&Status> {
3796 match self {
3797 Self::Document => None,
3798 Self::Work(_, status) => Some(status),
3799 }
3800 }
3801}
3802
3803/// The item being written, in the one shape all three write methods reach.
3804struct Incoming<'a> {
3805 written: Written<'a>,
3806 /// The title a person wrote. A document's goes onto the issue with
3807 /// [`DESIGN_TITLE_PREFIX`] put back, so a round trip returns the title that went in.
3808 title: &'a str,
3809 content: Option<&'a str>,
3810 labels: &'a [Label],
3811 metadata: &'a BTreeMap<String, Value>,
3812 repositories: &'a [Repository],
3813 parent: Option<&'a NativeId>,
3814}
3815
3816impl Incoming<'_> {
3817 /// The title this write puts on the issue.
3818 fn written_title(&self) -> String {
3819 match self.written {
3820 Written::Document => format!("{DESIGN_TITLE_PREFIX}{}", self.title),
3821 Written::Work(..) => self.title.to_owned(),
3822 }
3823 }
3824}
3825
3826#[derive(Clone, Copy, PartialEq, Eq)]
3827enum ContentKind {
3828 DraftIssue,
3829 Issue,
3830}
3831
3832/// What one board issue is: a document, or the work an [`ItemKind`] names.
3833///
3834/// A type of this source's own rather than an `ItemKind` with a third variant, because
3835/// `ItemKind` names what a dependency endpoint points at and nothing may point at a
3836/// document — the contract keeps a document out of that enum deliberately. Holding the
3837/// board's three answers in one value is what makes every place that asks "which is this?"
3838/// answer all three, rather than a `document: bool` beside a `kind` that means nothing for
3839/// two thirds of the board.
3840#[derive(Clone, Copy, PartialEq, Eq)]
3841enum BoardKind {
3842 /// An issue whose title begins [`DESIGN_TITLE_PREFIX`].
3843 Document,
3844 /// Every other issue, and every draft.
3845 Work(ItemKind),
3846}
3847
3848impl BoardKind {
3849 /// How a refusal names this kind to the person reading it.
3850 const fn describes(self) -> &'static str {
3851 match self {
3852 Self::Document => "document",
3853 Self::Work(kind) => kind.marker(),
3854 }
3855 }
3856}
3857
3858/// Whether `labels` satisfies `filter`, matching by name, case-insensitively.
3859///
3860/// This is the local Markdown source's `labels_match`, spelled the same way on purpose:
3861/// the shared cross-source journeys assert one answer to one question, so two sources
3862/// that disagree about what "carries the label bug" means fail them.
3863fn labels_match(labels: &[Label], filter: &LabelFilter) -> bool {
3864 let holds = |name: &String| {
3865 labels
3866 .iter()
3867 .any(|label| label.name.eq_ignore_ascii_case(name))
3868 };
3869 (filter.any_of.is_empty() || filter.any_of.iter().any(holds))
3870 && filter.all_of.iter().all(holds)
3871 && !filter.none_of.iter().any(holds)
3872}
3873
3874/// Whether `category` is one of `statuses`. An empty list is unfiltered rather than
3875/// "keeps nothing", which is what lets a `Vec<StatusCategory>` spell no filter at all.
3876fn status_matches(category: StatusCategory, statuses: &[StatusCategory]) -> bool {
3877 statuses.is_empty() || statuses.contains(&category)
3878}
3879
3880/// Whether `title`/`content` satisfies `query`, matching case-insensitively.
3881///
3882/// `content` is the item's own prose — the body with this source's trailing metadata
3883/// comment already taken off — so a search never matches an encoding the author of the
3884/// issue never wrote.
3885fn text_matches(title: &str, content: Option<&str>, query: &TextQuery) -> bool {
3886 let terms = query.terms.to_lowercase();
3887 let in_title = title.to_lowercase().contains(&terms);
3888 let in_content = content.is_some_and(|body| body.to_lowercase().contains(&terms));
3889 match query.fields {
3890 TextFields::Title => in_title,
3891 TextFields::Content => in_content,
3892 TextFields::TitleOrContent => in_title || in_content,
3893 }
3894}
3895
3896/// Whether `task` satisfies `query`, with `project` deciding the project predicate.
3897///
3898/// The project predicate is passed separately because a read narrowed to one project has
3899/// already answered it by asking *that project* for its own items — and re-applying it
3900/// there would compare the caller's selector, which may be a project's **name**, against
3901/// the id of the project that name resolved to, and keep nothing. Every other read passes
3902/// `query.project` and applies it here, which is what keeps `projects` a predicate this
3903/// source really does apply.
3904fn task_matches(task: &Task, query: &TaskQuery, project: &ProjectFilter) -> bool {
3905 labels_match(&task.labels, &query.labels)
3906 && status_matches(task.status.category, &query.statuses)
3907 && match project {
3908 ProjectFilter::Any => true,
3909 ProjectFilter::Orphans => task.project.is_none(),
3910 ProjectFilter::Is(id) => task.project.as_ref() == Some(id),
3911 }
3912 && query
3913 .text
3914 .as_ref()
3915 .is_none_or(|text| text_matches(&task.title, task.content.as_deref(), text))
3916}
3917
3918fn project_matches(project: &Project, query: &ProjectQuery) -> bool {
3919 labels_match(&project.labels, &query.labels)
3920 && status_matches(project.status.category, &query.statuses)
3921 && query
3922 .text
3923 .as_ref()
3924 .is_none_or(|text| text_matches(&project.title, project.content.as_deref(), text))
3925}
3926
3927/// The same three predicates a task query carries, minus the status filter.
3928///
3929/// A document is not work, so it has no status for one to compare against and the query
3930/// type carries none. The project predicate is the same one — a design issue filed under a
3931/// project issue is in that project, and one filed under nothing is in none — so it is
3932/// spelled the same way here rather than answered differently.
3933fn document_matches(document: &Document, query: &DocumentQuery, project: &ProjectFilter) -> bool {
3934 labels_match(&document.labels, &query.labels)
3935 && match project {
3936 ProjectFilter::Any => true,
3937 ProjectFilter::Orphans => document.project.is_none(),
3938 ProjectFilter::Is(id) => document.project.as_ref() == Some(id),
3939 }
3940 && query
3941 .text
3942 .as_ref()
3943 .is_none_or(|text| text_matches(&document.title, document.content.as_deref(), text))
3944}
3945
3946#[async_trait::async_trait]
3947impl TaskSource for GitHubProjectsSource {
3948 fn kind(&self) -> &'static str {
3949 KIND
3950 }
3951 fn capabilities(&self) -> Capabilities {
3952 Capabilities {
3953 projects: Support::Native,
3954 documents: Support::Native,
3955 orphan_tasks: Support::Native,
3956 filter_by_label: Support::Native,
3957 filter_by_status: Support::Native,
3958 search_title: Support::Native,
3959 search_content: Support::Native,
3960 task_dependencies: DependencySupport::BothDirections,
3961 project_dependencies: DependencySupport::BothDirections,
3962 max_page_size: MAX_PAGE_SIZE,
3963 }
3964 }
3965 async fn health(&self) -> Result<Health, SourceError> {
3966 let board = self.board_page(None, 1).await?;
3967 Ok(Health {
3968 reachable: true,
3969 detail: Some(format!(
3970 "reading GitHub project {}/{} ({})",
3971 self.owner,
3972 self.project_number,
3973 required_str(&board, "title")?
3974 )),
3975 })
3976 }
3977 async fn get_task(&self, id: &NativeId) -> Result<Option<Task>, SourceError> {
3978 Ok(self
3979 .item_by_id(id)
3980 .await?
3981 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
3982 .map(|item| item.task()))
3983 }
3984 async fn get_project(&self, id: &NativeId) -> Result<Option<Project>, SourceError> {
3985 Ok(self
3986 .item_by_id(id)
3987 .await?
3988 .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
3989 .map(|item| item.project()))
3990 }
3991 async fn query_tasks(
3992 &self,
3993 query: &TaskQuery,
3994 page: &PageRequest,
3995 ) -> Result<Page<Task>, SourceError> {
3996 validate_page(page)?;
3997 // A read narrowed to one project asks that project for its own tasks, so nothing
3998 // about it costs what the rest of the board holds. Every other task read is a
3999 // question about the whole board and is answered by reading it.
4000 let (held, membership) = match &query.project {
4001 ProjectFilter::Is(project) => (
4002 self.project_children(project).await?,
4003 // Answered by where these items came from; see `task_matches`.
4004 &ProjectFilter::Any,
4005 ),
4006 ProjectFilter::Any | ProjectFilter::Orphans => {
4007 (self.board().await?.items, &query.project)
4008 }
4009 };
4010 // Filtered before paged: a page of a filtered result is a page of the survivors,
4011 // never the survivors of a page.
4012 let tasks = held
4013 .iter()
4014 .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
4015 .map(Resolved::task)
4016 .filter(|task| task_matches(task, query, membership))
4017 .collect();
4018 Ok(offset_page(
4019 tasks,
4020 numeric_cursor(page.cursor.as_ref())?,
4021 page.limit.min(MAX_PAGE_SIZE) as usize,
4022 ))
4023 }
4024 async fn query_projects(
4025 &self,
4026 query: &ProjectQuery,
4027 page: &PageRequest,
4028 ) -> Result<Page<Project>, SourceError> {
4029 validate_page(page)?;
4030 // The projects a board holds are found by an issue search scoped to that board,
4031 // never by walking the board's own item connection: what tells a project from a
4032 // task is the `parent` each issue carries, which costs nothing to read.
4033 let projects = self
4034 .board_issues()
4035 .await?
4036 .iter()
4037 .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
4038 .map(Resolved::project)
4039 .filter(|project| project_matches(project, query))
4040 .collect();
4041 Ok(offset_page(
4042 projects,
4043 numeric_cursor(page.cursor.as_ref())?,
4044 page.limit.min(MAX_PAGE_SIZE) as usize,
4045 ))
4046 }
4047 async fn get_document(&self, id: &NativeId) -> Result<Option<Document>, SourceError> {
4048 Ok(self
4049 .item_by_id(id)
4050 .await?
4051 .filter(|item| item.kind == BoardKind::Document)
4052 .map(|item| item.document()))
4053 }
4054 async fn query_documents(
4055 &self,
4056 query: &DocumentQuery,
4057 page: &PageRequest,
4058 ) -> Result<Page<Document>, SourceError> {
4059 validate_page(page)?;
4060 // Narrowed to one project, this is the same sub-issue read a task list scoped to
4061 // that project makes — a document filed under a project is a sub-issue of it too,
4062 // and which of them come back is the kind this caller asked for.
4063 let (held, membership) = match &query.project {
4064 ProjectFilter::Is(project) => (
4065 self.project_children(project).await?,
4066 // Answered by where these items came from; see `task_matches`.
4067 &ProjectFilter::Any,
4068 ),
4069 ProjectFilter::Any | ProjectFilter::Orphans => {
4070 (self.board().await?.items, &query.project)
4071 }
4072 };
4073 // Filtered before paged, exactly as a task read is: a page of a filtered result is
4074 // a page of the survivors, never the survivors of a page.
4075 let documents = held
4076 .iter()
4077 .filter(|item| item.kind == BoardKind::Document)
4078 .map(Resolved::document)
4079 .filter(|document| document_matches(document, query, membership))
4080 .collect();
4081 Ok(offset_page(
4082 documents,
4083 numeric_cursor(page.cursor.as_ref())?,
4084 page.limit.min(MAX_PAGE_SIZE) as usize,
4085 ))
4086 }
4087 async fn labels(&self, page: &PageRequest) -> Result<Page<Label>, SourceError> {
4088 validate_page(page)?;
4089 let offset = numeric_cursor(page.cursor.as_ref())?;
4090 let mut labels = self
4091 .board()
4092 .await?
4093 .items
4094 .into_iter()
4095 .flat_map(|item| item.labels)
4096 .fold(Vec::new(), |mut all, label| {
4097 if !all.iter().any(|x: &Label| x.id == label.id) {
4098 all.push(label);
4099 }
4100 all
4101 });
4102 labels.sort_by(|a, b| a.name.cmp(&b.name).then(a.id.0.cmp(&b.id.0)));
4103 Ok(offset_page(
4104 labels,
4105 offset,
4106 page.limit.min(MAX_PAGE_SIZE) as usize,
4107 ))
4108 }
4109 async fn task_dependencies(
4110 &self,
4111 id: &NativeId,
4112 direction: Direction,
4113 page: &PageRequest,
4114 ) -> Result<Page<DependencyEdge>, SourceError> {
4115 self.dependencies(id, ItemKind::Task, direction, page).await
4116 }
4117 async fn project_dependencies(
4118 &self,
4119 id: &NativeId,
4120 direction: Direction,
4121 page: &PageRequest,
4122 ) -> Result<Page<DependencyEdge>, SourceError> {
4123 self.dependencies(id, ItemKind::Project, direction, page)
4124 .await
4125 }
4126
4127 fn writes(&self) -> WriteSupport {
4128 WriteSupport::Supported
4129 }
4130
4131 async fn write_task(&self, write: &ItemWrite<Task>) -> Result<NativeId, SourceError> {
4132 self.write_item(
4133 &Incoming {
4134 written: Written::Work(ItemKind::Task, &write.item.status),
4135 title: &write.item.title,
4136 content: write.item.content.as_deref(),
4137 labels: &write.item.labels,
4138 metadata: &write.item.metadata,
4139 repositories: &write.item.repositories,
4140 parent: write.item.project.as_ref(),
4141 },
4142 write.target.as_ref(),
4143 &write.depends_on,
4144 )
4145 .await
4146 }
4147
4148 async fn write_project(&self, write: &ItemWrite<Project>) -> Result<NativeId, SourceError> {
4149 self.write_item(
4150 &Incoming {
4151 written: Written::Work(ItemKind::Project, &write.item.status),
4152 title: &write.item.title,
4153 content: write.item.content.as_deref(),
4154 labels: &write.item.labels,
4155 metadata: &write.item.metadata,
4156 repositories: &write.item.repositories,
4157 parent: None,
4158 },
4159 write.target.as_ref(),
4160 &write.depends_on,
4161 )
4162 .await
4163 }
4164
4165 /// Create or update one document, which is one issue titled the way this board spells
4166 /// a document.
4167 ///
4168 /// Everything else is exactly a task write: caller metadata goes to the same canonical
4169 /// JSON slot at the end of the body and comes back with its JSON types intact, a key
4170 /// or a field this board cannot carry is refused by name rather than dropped, a target
4171 /// naming an issue this board does not hold is refused rather than created, and an
4172 /// issue this call created is taken back when the rest of the write fails.
4173 async fn write_document(&self, write: &ItemWrite<Document>) -> Result<NativeId, SourceError> {
4174 // A document takes part in no dependency graph, so there is no far end to write
4175 // natively and none to record: a caller naming one is told so rather than having it
4176 // stored under the reserved key, where a later read would report an edge the
4177 // contract says cannot exist.
4178 if !write.depends_on.is_empty() {
4179 return Err(SourceError::Refused {
4180 message: format!(
4181 "this write names {} dependencies for a document, and a document takes \
4182 part in no dependency graph; next: put the dependency on the task or \
4183 project the document is about",
4184 write.depends_on.len()
4185 ),
4186 });
4187 }
4188 self.write_item(
4189 &Incoming {
4190 written: Written::Document,
4191 title: &write.item.title,
4192 content: write.item.content.as_deref(),
4193 labels: &write.item.labels,
4194 metadata: &write.item.metadata,
4195 repositories: &write.item.repositories,
4196 parent: write.item.project.as_ref(),
4197 },
4198 write.target.as_ref(),
4199 &[],
4200 )
4201 .await
4202 }
4203
4204 async fn delete_task(&self, id: &NativeId) -> Result<(), SourceError> {
4205 self.delete_item(id).await
4206 }
4207
4208 async fn delete_project(&self, id: &NativeId) -> Result<(), SourceError> {
4209 self.delete_item(id).await
4210 }
4211
4212 async fn delete_document(&self, id: &NativeId) -> Result<(), SourceError> {
4213 self.delete_item(id).await
4214 }
4215}
4216
4217/// Where the recorded tail of a dependency walk resumes; see
4218/// [`GitHubProjectsSource::recorded_edges`].
4219const RECORDED_CURSOR: &str = "onetaskgraph.depends_on:";
4220
4221/// The board text field this source keeps a copy's origin in.
4222///
4223/// Named after the key it holds, and held to that name by the guard below rather than by
4224/// a reader noticing.
4225const ORIGIN_FIELD: &str = "onetaskgraph.origin";
4226
4227/// The metadata key that field holds.
4228///
4229/// The engine owns this key and spells it once as `GlobalId::ORIGIN_KEY`; a plugin never
4230/// constructs or interprets the qualified id it carries. This source names it only to
4231/// route it — a short, typed value belongs in a typed field rather than in the body slot
4232/// a caller's own prose shares.
4233///
4234/// Restated rather than imported, because no plugin crate may depend on the engine. What
4235/// keeps the two spellings one contract is `scripts/check-origin-key-spelling.sh`, a
4236/// target in `check`: it reads the engine's own literal and fails naming the file and the
4237/// line when a plugin's parts from it either way. Drift here has one symptom — a copy
4238/// that creates a second item every run instead of finding the one it wrote — and that is
4239/// too late to learn it.
4240const ORIGIN_KEY: &str = "onetaskgraph.origin";
4241
4242/// Where a recorded tail resumes, refusing a cursor no walk in `direction` reported.
4243///
4244/// The reserved key holds forward edges and nothing else — the reverse of a recorded edge
4245/// is derived from the far end, never written down on the near item — so only a forward
4246/// walk ever reports one of these cursors. A reverse read carrying one is resuming a walk
4247/// it did not come from, and it is told so rather than answered with an empty page that
4248/// reads as a walk which ended.
4249fn recorded_offset(
4250 cursor: Option<&str>,
4251 direction: Direction,
4252) -> Result<Option<usize>, SourceError> {
4253 cursor
4254 .and_then(|cursor| cursor.strip_prefix(RECORDED_CURSOR))
4255 .map(|offset| {
4256 if direction != Direction::DependsOn {
4257 return Err(SourceError::Config {
4258 message: format!(
4259 "{RECORDED_CURSOR}{offset} resumes recorded forward edges, which a \
4260 reverse dependency read never issues; resume it in the direction \
4261 that reported it"
4262 ),
4263 });
4264 }
4265 offset.parse().map_err(|_| SourceError::Config {
4266 message: format!("{RECORDED_CURSOR}{offset} is not a recorded-edge cursor"),
4267 })
4268 })
4269 .transpose()
4270}
4271
4272fn recorded_page(edges: Vec<DependencyEdge>, offset: usize, limit: usize) -> Page<DependencyEdge> {
4273 let mut page = offset_page(edges, offset, limit.max(1));
4274 page.next = page
4275 .next
4276 .map(|cursor| Cursor(format!("{RECORDED_CURSOR}{}", cursor.0)));
4277 page
4278}
4279
4280/// The kind of one issue reached through a dependency connection.
4281///
4282/// The same questions the board scan asks, over the fields the dependency document
4283/// selects, and in the same order: the design prefix first, then a sub-issue is a task,
4284/// then anything with sub-issues or the marker is a project.
4285///
4286/// # Errors
4287///
4288/// A far end this board holds as a document is refused rather than reported. The two
4289/// answers that are not refusals would both be wrong: reporting it as a task names an id
4290/// no task read of this source can find, and reporting it as a project names one no
4291/// project read can. There is no third value to return — `ItemKind` has no document
4292/// variant, because nothing may point at a document — so the relationship itself is what
4293/// the person is told about.
4294fn related_kind(value: &Value) -> Result<ItemKind, SourceError> {
4295 let id = required_str(value, "id")?;
4296 if required_str(value, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
4297 return Err(SourceError::Refused {
4298 message: format!(
4299 "GitHub issue {id} is a document of this board — its title begins \
4300 {DESIGN_TITLE_PREFIX:?} — and nothing may depend on a document or be depended \
4301 on by one; next: remove that issue's blocking relationship on this board"
4302 ),
4303 });
4304 }
4305 let parent = optional_str(value.get("parent").unwrap_or(&Value::Null), "id")?;
4306 if parent.is_some() {
4307 return Ok(ItemKind::Task);
4308 }
4309 let (_, slot) = metadata_body(optional_str(value, "body")?.map(str::to_owned))?;
4310 let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
4311 message: format!("GitHub issue {id}: {message}"),
4312 })?;
4313 let sub_issues = sub_issue_total(value)?;
4314 Ok(if sub_issues > 0 || marked == Some(ItemKind::Project) {
4315 ItemKind::Project
4316 } else {
4317 ItemKind::Task
4318 })
4319}
4320
4321/// The `IssueStateUpdateInput` one status target asks for.
4322///
4323/// `stateInput` and `state` are mutually exclusive on `UpdateIssueInput`, and only this
4324/// one is ever sent. A non-terminal status always asks for `OPEN`, which is what reopens
4325/// a currently-closed issue: without that the item would read back `Unknown` and a copy
4326/// would report a change forever. A document has no status at all, and asks for neither.
4327fn state_input(target: Option<&StatusTarget>) -> Value {
4328 match target {
4329 Some(StatusTarget::Closed(reason)) => {
4330 json!({"value":"CLOSED","stateReason":reason.reason()})
4331 }
4332 Some(StatusTarget::Column(_) | StatusTarget::Disabled) => json!({"value":"OPEN"}),
4333 // A document has no status, so a write of one says nothing about the issue's open
4334 // or closed state rather than forcing it open: `stateInput` is what carries that
4335 // instruction, and an explicit null asks for no change to it.
4336 None => Value::Null,
4337 }
4338}
4339
4340/// The metadata one write stores in the item's body slot.
4341///
4342/// The typed fields travel as themselves, so the three reserved keys are rebuilt here
4343/// rather than carried: the kind marker so an empty project stays readable, the
4344/// repository list only when it is not exactly the issue's own repository, and the far
4345/// ends no relationship here can name.
4346fn slot_metadata(
4347 incoming: &Incoming<'_>,
4348 own_repository: Option<&Repository>,
4349 fallback: &[DependencyEdge],
4350) -> BTreeMap<String, Value> {
4351 let mut metadata = incoming.metadata.clone();
4352 metadata.remove(ORIGIN_KEY);
4353 match incoming.written.kind() {
4354 BoardKind::Work(kind) => metadata.insert(
4355 ItemKind::METADATA_KEY.to_owned(),
4356 Value::String(kind.marker().to_owned()),
4357 ),
4358 // A document is told by its title, so it carries no kind marker: that key names
4359 // what a dependency endpoint points at, and nothing may point at a document.
4360 BoardKind::Document => metadata.remove(ItemKind::METADATA_KEY),
4361 };
4362 let derivable = own_repository
4363 .map(|own| incoming.repositories == [own.clone()])
4364 .unwrap_or(incoming.repositories.is_empty());
4365 if derivable {
4366 metadata.remove(Repository::METADATA_KEY);
4367 } else {
4368 metadata.insert(
4369 Repository::METADATA_KEY.to_owned(),
4370 Value::Array(
4371 incoming
4372 .repositories
4373 .iter()
4374 .map(|repository| Value::String(repository.as_str().to_owned()))
4375 .collect(),
4376 ),
4377 );
4378 }
4379 if fallback.is_empty() {
4380 metadata.remove(DependencyEdge::RECORDED_KEY);
4381 } else {
4382 metadata.insert(
4383 DependencyEdge::RECORDED_KEY.to_owned(),
4384 Value::Array(
4385 fallback
4386 .iter()
4387 .map(|edge| json!({"id":edge.to.id(),"kind":edge.to.kind}))
4388 .collect(),
4389 ),
4390 );
4391 }
4392 metadata
4393}
4394
4395/// Every label one item carries, from its content's own connection and nowhere else.
4396///
4397/// There is no second place to read one from: no document this source sends selects the
4398/// board's built-in `Labels` field, because GitHub derives it from the content and a draft
4399/// cannot carry one at all. The module documentation records the three schema facts that
4400/// settle it.
4401fn labels(content: &Value) -> Result<Vec<Label>, SourceError> {
4402 optional_nodes(content.get("labels"), "content labels")?
4403 .into_iter()
4404 .flatten()
4405 .map(|v| {
4406 Ok(Label {
4407 id: NativeId(required_str(v, "id")?.to_owned()),
4408 name: required_str(v, "name")?.to_owned(),
4409 color: optional_str(v, "color")?.map(str::to_owned),
4410 })
4411 })
4412 .collect()
4413}
4414
4415fn text_field(field_values: &[Value], name: &str) -> Result<Option<String>, SourceError> {
4416 let Some(node) = field_values
4417 .iter()
4418 .find(|node| node.pointer("/field/name").and_then(Value::as_str) == Some(name))
4419 else {
4420 return Ok(None);
4421 };
4422 Ok(optional_str(node, "text")?.map(str::to_owned))
4423}
4424
4425fn valid_github_owner(owner: &str) -> bool {
4426 !owner.is_empty()
4427 && owner.len() <= 39
4428 && !owner.starts_with('-')
4429 && !owner.ends_with('-')
4430 && !owner.contains("--")
4431 && owner
4432 .bytes()
4433 .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
4434}
4435
4436/// GitHub's repository-name grammar: 1-100 ASCII letters, digits, `-`, `_` or `.`, and
4437/// neither of the two names a path segment already means.
4438fn valid_github_repository_name(name: &str) -> bool {
4439 !name.is_empty()
4440 && name.len() <= 100
4441 && name != "."
4442 && name != ".."
4443 && name
4444 .bytes()
4445 .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
4446}
4447
4448fn valid_environment_name(name: &str) -> bool {
4449 let mut bytes = name.bytes();
4450 bytes
4451 .next()
4452 .is_some_and(|byte| byte.is_ascii_alphabetic() || byte == b'_')
4453 && bytes.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
4454}
4455
4456/// How many sub-issues one issue has.
4457///
4458/// `Issue.subIssuesSummary` is `SubIssuesSummary!` and its `total` is `Int!`, so an
4459/// absent or non-integer one is a response this source cannot read — and reading it as
4460/// zero would classify a project as a task, which is exactly the mistake the marker
4461/// exists to keep from happening quietly.
4462fn sub_issue_total(issue: &Value) -> Result<u64, SourceError> {
4463 let summary = issue
4464 .get("subIssuesSummary")
4465 .ok_or_else(|| SourceError::Malformed {
4466 message: "GitHub issue is missing subIssuesSummary".into(),
4467 })?;
4468 summary
4469 .get("total")
4470 .and_then(Value::as_u64)
4471 .ok_or_else(|| SourceError::Malformed {
4472 message: "GitHub issue subIssuesSummary.total is not an unsigned integer".into(),
4473 })
4474}
4475
4476fn required_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
4477 value
4478 .get(field)
4479 .and_then(Value::as_str)
4480 .ok_or_else(|| SourceError::Malformed {
4481 message: format!("GitHub response is missing string field {field}"),
4482 })
4483}
4484
4485/// The slot's delimiters, which `docs/metadata.md` settles once for every source that
4486/// needs one — Linear spells them too, in its own description field.
4487///
4488/// Restated rather than shared, because a plugin crate depends on the contract crate and
4489/// nothing else of this workspace. `scripts/check-metadata-slot-encoding.sh`, a target in
4490/// `check`, is what keeps the two one encoding: drift is otherwise quiet, since each
4491/// source round-trips its own writes perfectly well under its own spelling.
4492const METADATA_OPEN: &str = "<!-- onetaskgraph.metadata\n";
4493const METADATA_CLOSE: &str = "\n-->";
4494
4495/// The visible body and the metadata slot at the end of it.
4496///
4497/// The encoding is the one `docs/metadata.md` settles for Linear, which is where its
4498/// reasons are. Only a comment at the very end is a slot; one in the middle is a person's
4499/// own content and is left alone.
4500fn metadata_body(
4501 body: Option<String>,
4502) -> Result<(Option<String>, BTreeMap<String, Value>), SourceError> {
4503 let Some(body) = body else {
4504 return Ok((None, BTreeMap::new()));
4505 };
4506 let Some(start) = body.rfind(METADATA_OPEN) else {
4507 return Ok((Some(body), BTreeMap::new()));
4508 };
4509 let encoded_start = start + METADATA_OPEN.len();
4510 let Some(relative_end) = body[encoded_start..].find(METADATA_CLOSE) else {
4511 return Err(SourceError::Malformed {
4512 message: "unterminated onetaskgraph metadata slot in GitHub issue body".into(),
4513 });
4514 };
4515 let encoded_end = encoded_start + relative_end;
4516 if !body[encoded_end + METADATA_CLOSE.len()..].trim().is_empty() {
4517 return Ok((Some(body), BTreeMap::new()));
4518 }
4519 let metadata = serde_json::from_str(&body[encoded_start..encoded_end]).map_err(|error| {
4520 SourceError::Malformed {
4521 message: format!(
4522 "invalid canonical JSON in GitHub issue onetaskgraph metadata slot: {error}"
4523 ),
4524 }
4525 })?;
4526 let visible = body[..start].trim_end();
4527 Ok(((!visible.is_empty()).then(|| visible.to_owned()), metadata))
4528}
4529
4530fn compose_body(
4531 content: Option<&str>,
4532 metadata: &BTreeMap<String, Value>,
4533) -> Result<Option<String>, SourceError> {
4534 let visible = content.unwrap_or_default();
4535 if metadata.is_empty() {
4536 return Ok((!visible.is_empty()).then(|| visible.to_owned()));
4537 }
4538 let encoded = serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
4539 message: error.to_string(),
4540 })?;
4541 Ok(Some(if visible.is_empty() {
4542 format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
4543 } else {
4544 format!("{visible}\n\n{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
4545 }))
4546}
4547
4548fn required_bool(value: &Value, field: &str) -> Result<bool, SourceError> {
4549 value
4550 .get(field)
4551 .and_then(Value::as_bool)
4552 .ok_or_else(|| SourceError::Malformed {
4553 message: format!("GitHub response is missing boolean field {field}"),
4554 })
4555}
4556fn optional_str<'a>(value: &'a Value, field: &str) -> Result<Option<&'a str>, SourceError> {
4557 match value.get(field) {
4558 None | Some(Value::Null) => Ok(None),
4559 Some(value) => value
4560 .as_str()
4561 .map(Some)
4562 .ok_or_else(|| SourceError::Malformed {
4563 message: format!("GitHub response field {field} is not a string or null"),
4564 }),
4565 }
4566}
4567fn optional_nodes<'a>(
4568 connection: Option<&'a Value>,
4569 name: &str,
4570) -> Result<Option<&'a Vec<Value>>, SourceError> {
4571 match connection {
4572 None | Some(Value::Null) => Ok(None),
4573 Some(value) => value
4574 .get("nodes")
4575 .and_then(Value::as_array)
4576 .map(Some)
4577 .ok_or_else(|| SourceError::Malformed {
4578 message: format!("GitHub {name}.nodes is not an array"),
4579 }),
4580 }
4581}
4582fn complete_connection(connection: &Value, name: &str, size: u32) -> Result<(), SourceError> {
4583 let page_info = connection
4584 .get("pageInfo")
4585 .ok_or_else(|| SourceError::Malformed {
4586 message: format!("GitHub {name} has no pageInfo"),
4587 })?;
4588 if required_bool(page_info, "hasNextPage")? {
4589 return Err(SourceError::Malformed {
4590 message: format!(
4591 "GitHub {name} exceeds the supported nested connection size of {size}"
4592 ),
4593 });
4594 }
4595 Ok(())
4596}
4597fn optional_time(value: &Value, field: &str) -> Result<Option<DateTime<Utc>>, SourceError> {
4598 optional_str(value, field)?
4599 .map(|timestamp| {
4600 timestamp.parse().map_err(|error| SourceError::Malformed {
4601 message: format!("GitHub response field {field} is not a timestamp: {error}"),
4602 })
4603 })
4604 .transpose()
4605}
4606fn validate_page(page: &PageRequest) -> Result<(), SourceError> {
4607 if page.limit == 0 {
4608 Err(SourceError::Config {
4609 message: "page limit must be at least 1".into(),
4610 })
4611 } else {
4612 Ok(())
4613 }
4614}
4615fn next_cursor(connection: &Value) -> Result<Option<Cursor>, SourceError> {
4616 let page = connection
4617 .get("pageInfo")
4618 .filter(|value| value.is_object())
4619 .ok_or_else(|| SourceError::Malformed {
4620 message: "GitHub connection is missing pageInfo".into(),
4621 })?;
4622 if required_bool(page, "hasNextPage")? {
4623 let cursor = required_str(page, "endCursor")?;
4624 validate_cursor_progress(None, cursor)?;
4625 Ok(Some(Cursor(cursor.into())))
4626 } else {
4627 Ok(None)
4628 }
4629}
4630fn validate_cursor_progress(previous: Option<&str>, next: &str) -> Result<(), SourceError> {
4631 if next.is_empty() || previous == Some(next) {
4632 Err(SourceError::Malformed {
4633 message: "GitHub pagination cursor is empty or did not advance".into(),
4634 })
4635 } else {
4636 Ok(())
4637 }
4638}
4639fn numeric_cursor(cursor: Option<&Cursor>) -> Result<usize, SourceError> {
4640 cursor.map_or(Ok(0), |c| {
4641 c.0.parse().map_err(|_| SourceError::Config {
4642 message: "page cursor is invalid".into(),
4643 })
4644 })
4645}
4646fn offset_page<T>(mut items: Vec<T>, offset: usize, limit: usize) -> Page<T> {
4647 if offset > items.len() {
4648 return Page::last(vec![]);
4649 }
4650 let tail = items.split_off(offset);
4651 let mut selected = tail;
4652 let next = (selected.len() > limit).then(|| Cursor((offset + limit).to_string()));
4653 selected.truncate(limit);
4654 Page {
4655 items: selected,
4656 next,
4657 }
4658}