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