The best way to control Claude Code, Codex and more from any computer or phone

main
ts 92 lines 3.63 KB
Raw
1 import type { IssueAssigneeCandidate, IssueDetail, IssueLabelCandidate } from "@t3tools/contracts";
2
3 import { selectionChanges, type SelectionDraft } from "./pullRequestSelection.ts";
4
5 type Labelled = Pick<IssueDetail, "labels">;
6 type Assigned = Pick<IssueDetail, "assignees">;
7
8 /**
9 * An issue write replaces the whole set, so a picker's picks become one write when it closes.
10 * Both return null when the picks leave the issue as the tracker already has it.
11 */
12 export function issueLabelsAfterPicks(
13 detail: Labelled,
14 draft: SelectionDraft<IssueLabelCandidate>,
15 ): ReadonlyArray<string> | null {
16 const names = detail.labels.map((label) => label.name);
17 const { add, remove } = selectionChanges(draft, (name) => names.includes(name));
18 if (add.length + remove.length === 0) return null;
19 const removed = new Set(remove.map((label) => label.name));
20 return [...names.filter((name) => !removed.has(name)), ...add.map((label) => label.name)];
21 }
22
23 export const issueAssigneeHeld = (detail: Assigned, candidate: IssueAssigneeCandidate) =>
24 detail.assignees.some((actor) => actor.login.toLowerCase() === candidate.login.toLowerCase());
25
26 /** People already assigned are written by login, the newly picked by the tracker's own id. */
27 export function issueAssigneesAfterPicks(
28 detail: Assigned,
29 draft: SelectionDraft<IssueAssigneeCandidate>,
30 ): ReadonlyArray<string> | null {
31 const { add, remove } = selectionChanges(draft, (_, candidate) =>
32 issueAssigneeHeld(detail, candidate),
33 );
34 if (add.length + remove.length === 0) return null;
35 const removed = new Set(remove.map((candidate) => candidate.login.toLowerCase()));
36 return [
37 ...detail.assignees
38 .map((actor) => actor.login)
39 .filter((login) => !removed.has(login.toLowerCase())),
40 ...add.map((candidate) => candidate.id),
41 ];
42 }
43
44 /** A picker's picks, the read of the issue they were made against, and the set they last wrote. */
45 export interface IssuePicks<T> {
46 readonly basis: object | null;
47 readonly draft: SelectionDraft<T>;
48 readonly written: ReadonlyArray<string> | null;
49 }
50
51 export function noIssuePicks<T>(): IssuePicks<T> {
52 return { basis: null, draft: new Map(), written: null };
53 }
54
55 /**
56 * The picks that count against `detail`. A write replaces the whole set and the detail only
57 * catches up once it is re-read, so picks outlive their write until then: a picker reopened in
58 * between builds on them instead of on the stale set. A newer read forgets them, except in an
59 * open picker, whose picks are diffed against that read when it closes.
60 */
61 export function issuePicksFor<T>(
62 picks: IssuePicks<T>,
63 detail: object,
64 open: boolean,
65 ): IssuePicks<T> {
66 if (picks.basis === detail) return picks;
67 return { basis: detail, draft: open ? picks.draft : new Map(), written: null };
68 }
69
70 /**
71 * A write that failed is forgotten along with the picks it carried, unless the picker was picked
72 * in again since: those picks stay, and the next close writes them with the ones that failed.
73 */
74 export function issuePicksAfterFailedWrite<T>(
75 latest: IssuePicks<T>,
76 sent: IssuePicks<T>,
77 ): IssuePicks<T> {
78 if (latest.written !== sent.written) return latest;
79 return latest.draft === sent.draft ? noIssuePicks() : { ...latest, written: null };
80 }
81
82 /** The set a closing picker writes: null when the tracker has it or was already sent it. */
83 export function issueWriteAfterPicks(
84 after: ReadonlyArray<string> | null,
85 held: ReadonlyArray<string>,
86 written: ReadonlyArray<string> | null,
87 ): ReadonlyArray<string> | null {
88 const target = after ?? held;
89 const sent = written ?? held;
90 const same = target.length === sent.length && target.every((value) => sent.includes(value));
91 return same ? null : target;
92 }