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