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