@davidbyoung/software-factory 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/plugin.json +5 -0
- package/LICENSE +21 -0
- package/README.md +135 -2
- package/actions/factory-agent/action.yml +218 -0
- package/agents/factory-test-reviewer.md +129 -0
- package/agents/factory-ticket-readiness.md +107 -0
- package/agents/factory-ui-reviewer.md +160 -0
- package/bin/factory.js +45 -0
- package/bin/launch.js +67 -0
- package/dist/board.d.mts +128 -0
- package/dist/board.mjs +456 -0
- package/dist/build.d.mts +18 -0
- package/dist/build.mjs +60 -0
- package/dist/cli.d.mts +17 -0
- package/dist/cli.mjs +264 -0
- package/dist/code-host/github.d.mts +118 -0
- package/dist/code-host/github.mjs +419 -0
- package/dist/code-host/index.d.mts +9 -0
- package/dist/code-host/index.mjs +18 -0
- package/dist/code-host/types.d.mts +205 -0
- package/dist/code-host/types.mjs +12 -0
- package/dist/config.d.mts +166 -0
- package/dist/config.mjs +548 -0
- package/dist/deps.d.mts +32 -0
- package/dist/deps.mjs +126 -0
- package/dist/doctor.d.mts +34 -0
- package/dist/doctor.mjs +188 -0
- package/dist/guard-commit.d.mts +41 -0
- package/dist/guard-commit.mjs +205 -0
- package/dist/guard-permission.d.mts +15 -0
- package/dist/guard-permission.mjs +52 -0
- package/dist/guard-ticket-text.d.mts +27 -0
- package/dist/guard-ticket-text.mjs +288 -0
- package/dist/guard.d.mts +4 -0
- package/dist/guard.mjs +37 -0
- package/dist/init.d.mts +58 -0
- package/dist/init.mjs +556 -0
- package/dist/lib/clock.d.mts +14 -0
- package/dist/lib/clock.mjs +36 -0
- package/dist/lib/gh.d.mts +28 -0
- package/dist/lib/gh.mjs +92 -0
- package/dist/lib/glob.d.mts +2 -0
- package/dist/lib/glob.mjs +41 -0
- package/dist/lib/pricing.d.mts +25 -0
- package/dist/lib/pricing.mjs +77 -0
- package/dist/lib/usage.d.mts +34 -0
- package/dist/lib/usage.mjs +128 -0
- package/dist/lib/work-status.d.mts +251 -0
- package/dist/lib/work-status.mjs +510 -0
- package/dist/merge-policy.d.mts +66 -0
- package/dist/merge-policy.mjs +146 -0
- package/dist/parts.d.mts +39 -0
- package/dist/parts.mjs +138 -0
- package/dist/poller.d.mts +61 -0
- package/dist/poller.mjs +247 -0
- package/dist/pr-watch.d.mts +45 -0
- package/dist/pr-watch.mjs +244 -0
- package/dist/pr.d.mts +65 -0
- package/dist/pr.mjs +571 -0
- package/dist/review-cost.d.mts +18 -0
- package/dist/review-cost.mjs +95 -0
- package/dist/review-tools.d.mts +5 -0
- package/dist/review-tools.mjs +46 -0
- package/dist/review-verdict.d.mts +50 -0
- package/dist/review-verdict.mjs +171 -0
- package/dist/reviewers.d.mts +54 -0
- package/dist/reviewers.mjs +312 -0
- package/dist/runner/github-actions.d.mts +12 -0
- package/dist/runner/github-actions.mjs +97 -0
- package/dist/runner/index.d.mts +9 -0
- package/dist/runner/index.mjs +22 -0
- package/dist/runner/types.d.mts +60 -0
- package/dist/runner/types.mjs +7 -0
- package/dist/session-start.d.mts +20 -0
- package/dist/session-start.mjs +115 -0
- package/dist/site/capture.d.mts +44 -0
- package/dist/site/capture.mjs +114 -0
- package/dist/site/measure.d.mts +83 -0
- package/dist/site/measure.mjs +225 -0
- package/dist/site/pages.d.mts +10 -0
- package/dist/site/pages.mjs +71 -0
- package/dist/site/pixels.d.mts +47 -0
- package/dist/site/pixels.mjs +157 -0
- package/dist/site/servers.d.mts +47 -0
- package/dist/site/servers.mjs +141 -0
- package/dist/site.d.mts +60 -0
- package/dist/site.mjs +341 -0
- package/dist/status.d.mts +100 -0
- package/dist/status.mjs +514 -0
- package/dist/sync.d.mts +59 -0
- package/dist/sync.mjs +292 -0
- package/dist/ticket.d.mts +61 -0
- package/dist/ticket.mjs +562 -0
- package/dist/tickets/github.d.mts +128 -0
- package/dist/tickets/github.mjs +491 -0
- package/dist/tickets/index.d.mts +15 -0
- package/dist/tickets/index.mjs +27 -0
- package/dist/tickets/transport.d.mts +42 -0
- package/dist/tickets/transport.mjs +158 -0
- package/dist/tickets/types.d.mts +191 -0
- package/dist/tickets/types.mjs +76 -0
- package/docs/builds.md +227 -0
- package/docs/cli.md +221 -0
- package/docs/configuration.md +809 -0
- package/docs/design.md +106 -0
- package/docs/developing.md +400 -0
- package/docs/existing-repo.md +138 -0
- package/docs/github/reference.md +288 -0
- package/docs/github/setup.md +312 -0
- package/docs/glossary.md +106 -0
- package/docs/limitations.md +115 -0
- package/docs/plugins.md +192 -0
- package/docs/quick-start.md +222 -0
- package/docs/removing.md +76 -0
- package/docs/skills.md +270 -0
- package/docs/updating.md +108 -0
- package/docs/workflow.md +195 -0
- package/package.json +67 -4
- package/poller.Dockerfile +21 -0
- package/review.md +170 -0
- package/rules.md +313 -0
- package/settings.schema.json +411 -0
- package/skills/factory-address-review/SKILL.md +547 -0
- package/skills/factory-address-review/patch-coverage.py +68 -0
- package/skills/factory-build/SKILL.md +412 -0
- package/skills/factory-deps/SKILL.md +185 -0
- package/skills/factory-design/SKILL.md +159 -0
- package/skills/factory-diagnose/SKILL.md +93 -0
- package/skills/factory-mutate/SKILL.md +76 -0
- package/skills/factory-refine/SKILL.md +417 -0
- package/skills/factory-resolve-conflicts/SKILL.md +235 -0
- package/workflows/factory-board-sync.yml +91 -0
- package/workflows/factory-build.yml +72 -0
- package/workflows/factory-check.yml +37 -0
- package/workflows/factory-claude-review.yml +133 -0
- package/workflows/factory-conflicts.yml +69 -0
- package/workflows/factory-deps-auto-merge.yml +43 -0
- package/workflows/factory-deps.yml +59 -0
- package/workflows/factory-merge-policy.yml +121 -0
- package/workflows/factory-review-verdict.yml +54 -0
- package/workflows/factory-review.yml +75 -0
|
@@ -0,0 +1,491 @@
|
|
|
1
|
+
// The GitHub adapter: tickets are a repository's issues, and the board is a
|
|
2
|
+
// GitHub Project owned by the repository's owner, a person or an
|
|
3
|
+
// organization. It reaches GitHub through
|
|
4
|
+
// the transport it is given (`transport.mts`), and picks REST or GraphQL per
|
|
5
|
+
// call.
|
|
6
|
+
//
|
|
7
|
+
// Node built-ins only: the poller's image copies this file.
|
|
8
|
+
import { rmSync, writeFileSync } from "node:fs";
|
|
9
|
+
import { tmpdir } from "node:os";
|
|
10
|
+
import { dirname, join } from "node:path";
|
|
11
|
+
import { NotFoundError, } from "./types.mjs";
|
|
12
|
+
import { statusOf } from "./transport.mjs";
|
|
13
|
+
import { sameName } from "../lib/work-status.mjs";
|
|
14
|
+
const toComment = (raw) => ({
|
|
15
|
+
id: raw.id,
|
|
16
|
+
createdAt: raw.created_at,
|
|
17
|
+
body: raw.body ?? "",
|
|
18
|
+
author: raw.user?.login ?? "ghost",
|
|
19
|
+
});
|
|
20
|
+
const labelName = (l) => typeof l === "string" ? l : l.name;
|
|
21
|
+
/** Rethrows a 404 as NotFoundError for `what`. */
|
|
22
|
+
async function found(what, call) {
|
|
23
|
+
try {
|
|
24
|
+
return await call;
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
if (statusOf(error) === 404)
|
|
28
|
+
throw new NotFoundError(what);
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
// What the GitHub adapter's "Read everything at once" asks: every open issue
|
|
33
|
+
// and the 30 closed ones most recently updated, each with its last 100
|
|
34
|
+
// comments.
|
|
35
|
+
export const ISSUES_QUERY = `
|
|
36
|
+
query Tickets($owner: String!, $name: String!, $after: String) {
|
|
37
|
+
repository(owner: $owner, name: $name) {
|
|
38
|
+
open: issues(states: OPEN, first: 100, after: $after) {
|
|
39
|
+
pageInfo { hasNextPage endCursor }
|
|
40
|
+
nodes { ...issue }
|
|
41
|
+
}
|
|
42
|
+
closed: issues(states: CLOSED, first: 30, orderBy: {field: UPDATED_AT, direction: DESC}) {
|
|
43
|
+
nodes { ...issue }
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
fragment issue on Issue {
|
|
48
|
+
number title state closedAt body
|
|
49
|
+
labels(first: 30) { nodes { name } }
|
|
50
|
+
comments(last: 100) {
|
|
51
|
+
nodes { databaseId createdAt body author { login } }
|
|
52
|
+
}
|
|
53
|
+
}`;
|
|
54
|
+
export const toComments = (raw) => (raw?.nodes ?? []).map((c) => ({
|
|
55
|
+
id: c.databaseId,
|
|
56
|
+
createdAt: c.createdAt,
|
|
57
|
+
body: c.body,
|
|
58
|
+
author: c.author?.login ?? "ghost",
|
|
59
|
+
}));
|
|
60
|
+
export function fromRawIssue(raw) {
|
|
61
|
+
return {
|
|
62
|
+
number: raw.number,
|
|
63
|
+
title: raw.title,
|
|
64
|
+
state: raw.state === "OPEN" ? "open" : "closed",
|
|
65
|
+
closedAt: raw.closedAt,
|
|
66
|
+
labels: raw.labels.nodes.map((l) => l.name),
|
|
67
|
+
body: raw.body ?? "",
|
|
68
|
+
comments: toComments(raw.comments),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
/** The tickets in every page of ISSUES_QUERY: the open ones, then the
|
|
72
|
+
* closed. */
|
|
73
|
+
export function fromIssuesPages(pages) {
|
|
74
|
+
const repo = pages.map((p) => p.data.repository);
|
|
75
|
+
return [
|
|
76
|
+
...repo.flatMap((r) => r.open.nodes),
|
|
77
|
+
...(repo[0]?.closed.nodes ?? []),
|
|
78
|
+
].map(fromRawIssue);
|
|
79
|
+
}
|
|
80
|
+
export const BOARD_QUERY = `
|
|
81
|
+
query Board($owner: String!, $number: Int!, $after: String, $session: String!, $runsIn: String!, $cost: String!) {
|
|
82
|
+
owner: repositoryOwner(login: $owner) {
|
|
83
|
+
... on ProjectV2Owner {
|
|
84
|
+
projectV2(number: $number) {
|
|
85
|
+
id
|
|
86
|
+
title
|
|
87
|
+
fields(first: 50) {
|
|
88
|
+
nodes {
|
|
89
|
+
... on ProjectV2FieldCommon { id name dataType }
|
|
90
|
+
... on ProjectV2SingleSelectField { options { id name } }
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
items(first: 100, after: $after) {
|
|
94
|
+
pageInfo { hasNextPage endCursor }
|
|
95
|
+
nodes {
|
|
96
|
+
id
|
|
97
|
+
content {
|
|
98
|
+
... on Issue { number repository { nameWithOwner } }
|
|
99
|
+
}
|
|
100
|
+
status: fieldValueByName(name: "Status") {
|
|
101
|
+
... on ProjectV2ItemFieldSingleSelectValue { name updatedAt }
|
|
102
|
+
}
|
|
103
|
+
session: fieldValueByName(name: $session) {
|
|
104
|
+
... on ProjectV2ItemFieldTextValue { text }
|
|
105
|
+
}
|
|
106
|
+
runsIn: fieldValueByName(name: $runsIn) {
|
|
107
|
+
... on ProjectV2ItemFieldSingleSelectValue { name }
|
|
108
|
+
}
|
|
109
|
+
cost: fieldValueByName(name: $cost) {
|
|
110
|
+
... on ProjectV2ItemFieldNumberValue { number }
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}`;
|
|
118
|
+
export function toBoard(pages, repository) {
|
|
119
|
+
const project = pages[0].data.owner?.projectV2;
|
|
120
|
+
if (!project)
|
|
121
|
+
throw new Error("The Project could not be read.");
|
|
122
|
+
const items = new Map();
|
|
123
|
+
for (const page of pages) {
|
|
124
|
+
for (const raw of page.data.owner.projectV2.items.nodes) {
|
|
125
|
+
const number = raw.content?.number;
|
|
126
|
+
if (number === undefined ||
|
|
127
|
+
raw.content?.repository?.nameWithOwner !== repository)
|
|
128
|
+
continue;
|
|
129
|
+
items.set(number, {
|
|
130
|
+
itemId: raw.id,
|
|
131
|
+
number,
|
|
132
|
+
status: raw.status?.name ?? null,
|
|
133
|
+
updatedAt: raw.status?.updatedAt ?? null,
|
|
134
|
+
session: raw.session?.text ?? "",
|
|
135
|
+
runsIn: raw.runsIn?.name ?? "",
|
|
136
|
+
cost: raw.cost?.number ?? null,
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return {
|
|
141
|
+
projectId: project.id,
|
|
142
|
+
title: project.title,
|
|
143
|
+
fields: project.fields.nodes.filter((f) => f.name),
|
|
144
|
+
items,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
const ADD_CARD = "mutation AddCard($project: ID!, $content: ID!) { addProjectV2ItemById(input: {projectId: $project, contentId: $content}) { item { id } } }";
|
|
148
|
+
const CLEAR_FIELD = "mutation ClearField($project: ID!, $item: ID!, $field: ID!) { clearProjectV2ItemFieldValue(input: {projectId: $project, itemId: $item, fieldId: $field}) { clientMutationId } }";
|
|
149
|
+
const SET_TEXT = "mutation SetText($project: ID!, $item: ID!, $field: ID!, $text: String!) { updateProjectV2ItemFieldValue(input: {projectId: $project, itemId: $item, fieldId: $field, value: {text: $text}}) { clientMutationId } }";
|
|
150
|
+
const SET_OPTION = "mutation SetOption($project: ID!, $item: ID!, $field: ID!, $option: String!) { updateProjectV2ItemFieldValue(input: {projectId: $project, itemId: $item, fieldId: $field, value: {singleSelectOptionId: $option}}) { clientMutationId } }";
|
|
151
|
+
const BOARDS_QUERY = `
|
|
152
|
+
query Boards($owner: String!) {
|
|
153
|
+
owner: repositoryOwner(login: $owner) {
|
|
154
|
+
... on ProjectV2Owner {
|
|
155
|
+
projectsV2(first: 100) { nodes { number title closed } }
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}`;
|
|
159
|
+
const IDS_QUERY = "query Ids($owner: String!, $name: String!) { repository(owner: $owner, name: $name) { id owner { id } } }";
|
|
160
|
+
const CREATE_BOARD = "mutation CreateBoard($owner: ID!, $repo: ID!, $title: String!) { createProjectV2(input: {ownerId: $owner, repositoryId: $repo, title: $title}) { projectV2 { number } } }";
|
|
161
|
+
const STATUS_FIELD = `
|
|
162
|
+
query StatusField($owner: String!, $number: Int!) {
|
|
163
|
+
owner: repositoryOwner(login: $owner) {
|
|
164
|
+
... on ProjectV2Owner {
|
|
165
|
+
projectV2(number: $number) {
|
|
166
|
+
field(name: "Status") {
|
|
167
|
+
... on ProjectV2SingleSelectField {
|
|
168
|
+
id
|
|
169
|
+
options { id name color description }
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}`;
|
|
176
|
+
const BOARD_FIELDS = `
|
|
177
|
+
query BoardFields($owner: String!, $number: Int!) {
|
|
178
|
+
owner: repositoryOwner(login: $owner) {
|
|
179
|
+
... on ProjectV2Owner {
|
|
180
|
+
projectV2(number: $number) {
|
|
181
|
+
id
|
|
182
|
+
fields(first: 50) { nodes { ... on ProjectV2FieldCommon { name } } }
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}`;
|
|
187
|
+
/** The mutation that adds one field to a board, as a literal for the same
|
|
188
|
+
* reason as `setColumnsMutation`. */
|
|
189
|
+
export function addFieldMutation(project, field) {
|
|
190
|
+
const type = { text: "TEXT", number: "NUMBER", select: "SINGLE_SELECT" }[field.type];
|
|
191
|
+
const options = field.type === "select"
|
|
192
|
+
? `, singleSelectOptions: [${field.options
|
|
193
|
+
.map((o) => `{name: ${JSON.stringify(o)}, color: GRAY, description: ""}`)
|
|
194
|
+
.join(", ")}]`
|
|
195
|
+
: "";
|
|
196
|
+
return `mutation { createProjectV2Field(input: {projectId: ${JSON.stringify(project)}, dataType: ${type}, name: ${JSON.stringify(field.name)}${options}}) { clientMutationId } }`;
|
|
197
|
+
}
|
|
198
|
+
/** The mutation that sets a Status field's columns: GraphQL takes a list of
|
|
199
|
+
* objects only as a literal, so the strings are written as JSON, which is
|
|
200
|
+
* valid GraphQL. */
|
|
201
|
+
export function setColumnsMutation(field, options) {
|
|
202
|
+
const list = options
|
|
203
|
+
.map((o) => `{${o.id ? `id: ${JSON.stringify(o.id)}, ` : ""}name: ${JSON.stringify(o.name)}, color: ${o.color}, description: ${JSON.stringify(o.description)}}`)
|
|
204
|
+
.join(", ");
|
|
205
|
+
return `mutation { updateProjectV2Field(input: {fieldId: ${JSON.stringify(field)}, singleSelectOptions: [${list}]}) { clientMutationId } }`;
|
|
206
|
+
}
|
|
207
|
+
/** The id in the URL `gh issue comment` prints. */
|
|
208
|
+
export function commentIdFromUrl(url) {
|
|
209
|
+
const match = /#issuecomment-(\d+)/.exec(url);
|
|
210
|
+
if (!match)
|
|
211
|
+
throw new Error(`gh printed no comment address: ${url.trim()}`);
|
|
212
|
+
return Number(match[1]);
|
|
213
|
+
}
|
|
214
|
+
export function githubTickets(o) {
|
|
215
|
+
const API = `repos/${o.repository}`;
|
|
216
|
+
const [repoOwner, repoName] = o.repository.split("/");
|
|
217
|
+
const t = o.transport;
|
|
218
|
+
let lastBoard = null;
|
|
219
|
+
const issue = (n) => found(`#${n}`, t.rest("GET", `${API}/issues/${n}`));
|
|
220
|
+
const readBoard = async () => {
|
|
221
|
+
if (o.board === null)
|
|
222
|
+
return { projectId: "", fields: [], items: new Map() };
|
|
223
|
+
const pages = [];
|
|
224
|
+
let after = null;
|
|
225
|
+
do {
|
|
226
|
+
const page = await t.graphql(BOARD_QUERY, {
|
|
227
|
+
owner: o.boardOwner ?? repoOwner,
|
|
228
|
+
number: o.board,
|
|
229
|
+
after,
|
|
230
|
+
// A field the settings say is never written is not read: no field
|
|
231
|
+
// has an empty name.
|
|
232
|
+
session: o.fieldNames ? (o.fieldNames.session ?? "") : "Session",
|
|
233
|
+
runsIn: o.fieldNames ? (o.fieldNames.runsIn ?? "") : "Runs in",
|
|
234
|
+
cost: o.fieldNames ? (o.fieldNames.cost ?? "") : "Cost",
|
|
235
|
+
});
|
|
236
|
+
pages.push(page);
|
|
237
|
+
const info = page.data.owner?.projectV2?.items.pageInfo;
|
|
238
|
+
after = info?.hasNextPage ? info.endCursor : null;
|
|
239
|
+
} while (after);
|
|
240
|
+
lastBoard = toBoard(pages, o.repository);
|
|
241
|
+
return lastBoard;
|
|
242
|
+
};
|
|
243
|
+
const tickets = {
|
|
244
|
+
async read(n) {
|
|
245
|
+
const raw = await issue(n);
|
|
246
|
+
if (raw.pull_request)
|
|
247
|
+
throw new NotFoundError(`#${n}`);
|
|
248
|
+
const comments = await t.rest("GET", `${API}/issues/${n}/comments?per_page=100`, { paginate: true });
|
|
249
|
+
return {
|
|
250
|
+
number: raw.number,
|
|
251
|
+
title: raw.title,
|
|
252
|
+
state: raw.state === "closed" ? "closed" : "open",
|
|
253
|
+
closedAt: raw.closed_at,
|
|
254
|
+
labels: raw.labels.map(labelName),
|
|
255
|
+
body: raw.body ?? "",
|
|
256
|
+
comments: comments.map(toComment),
|
|
257
|
+
};
|
|
258
|
+
},
|
|
259
|
+
async listOpen() {
|
|
260
|
+
const all = await t.rest("GET", `${API}/issues?state=open&per_page=100`, { paginate: true });
|
|
261
|
+
return all
|
|
262
|
+
.filter((i) => !i.pull_request)
|
|
263
|
+
.map((i) => ({
|
|
264
|
+
number: i.number,
|
|
265
|
+
title: i.title,
|
|
266
|
+
labels: i.labels.map(labelName),
|
|
267
|
+
}));
|
|
268
|
+
},
|
|
269
|
+
async writeBody(n, body) {
|
|
270
|
+
await found(`#${n}`, t.rest("PATCH", `${API}/issues/${n}`, { fields: { body } }));
|
|
271
|
+
},
|
|
272
|
+
async writeTitle(n, title) {
|
|
273
|
+
await found(`#${n}`, t.rest("PATCH", `${API}/issues/${n}`, { fields: { title } }));
|
|
274
|
+
},
|
|
275
|
+
async comment(n, body) {
|
|
276
|
+
const made = await found(`#${n}`, t.rest("POST", `${API}/issues/${n}/comments`, {
|
|
277
|
+
fields: { body },
|
|
278
|
+
}));
|
|
279
|
+
return made.id;
|
|
280
|
+
},
|
|
281
|
+
async editComment(id, body) {
|
|
282
|
+
await found(`comment ${id}`, t.rest("PATCH", `${API}/issues/comments/${id}`, { fields: { body } }));
|
|
283
|
+
},
|
|
284
|
+
async deleteComment(id) {
|
|
285
|
+
await found(`comment ${id}`, t.rest("DELETE", `${API}/issues/comments/${id}`));
|
|
286
|
+
},
|
|
287
|
+
async open({ title, body, labels }) {
|
|
288
|
+
const fields = { title, body };
|
|
289
|
+
if (labels.length)
|
|
290
|
+
fields.labels = labels;
|
|
291
|
+
const made = await t.rest("POST", `${API}/issues`, {
|
|
292
|
+
fields,
|
|
293
|
+
});
|
|
294
|
+
return made.number;
|
|
295
|
+
},
|
|
296
|
+
async addLabel(n, label) {
|
|
297
|
+
await found(`#${n}`, t.rest("POST", `${API}/issues/${n}/labels`, {
|
|
298
|
+
fields: { labels: [label] },
|
|
299
|
+
}));
|
|
300
|
+
},
|
|
301
|
+
async removeLabel(n, label) {
|
|
302
|
+
try {
|
|
303
|
+
await t.rest("DELETE", `${API}/issues/${n}/labels/${encodeURIComponent(label)}`);
|
|
304
|
+
}
|
|
305
|
+
catch (error) {
|
|
306
|
+
// GitHub answers 404 for a label the issue does not have; removing
|
|
307
|
+
// it is then already done, but a missing issue is not.
|
|
308
|
+
if (statusOf(error) !== 404)
|
|
309
|
+
throw error;
|
|
310
|
+
await issue(n);
|
|
311
|
+
}
|
|
312
|
+
},
|
|
313
|
+
linkText: (n) => `Closes #${n}`,
|
|
314
|
+
address: (n) => `https://github.com/${o.repository}/issues/${n}`,
|
|
315
|
+
board: {
|
|
316
|
+
read: readBoard,
|
|
317
|
+
async add(n) {
|
|
318
|
+
const board = lastBoard ?? (await readBoard());
|
|
319
|
+
if (o.board === null)
|
|
320
|
+
throw new Error("The settings name no board.");
|
|
321
|
+
const raw = await issue(n);
|
|
322
|
+
const added = await t.graphql(ADD_CARD, { project: board.projectId, content: raw.node_id });
|
|
323
|
+
return added.data.addProjectV2ItemById.item.id;
|
|
324
|
+
},
|
|
325
|
+
async set(itemId, writes) {
|
|
326
|
+
if (o.board === null)
|
|
327
|
+
throw new Error("The settings name no board.");
|
|
328
|
+
const board = lastBoard ?? (await readBoard());
|
|
329
|
+
const field = (name) => {
|
|
330
|
+
const f = board.fields.find((x) => sameName(x.name, name));
|
|
331
|
+
if (!f)
|
|
332
|
+
throw new Error(`The board has no ${name} field.`);
|
|
333
|
+
return f;
|
|
334
|
+
};
|
|
335
|
+
for (const write of writes) {
|
|
336
|
+
const f = field(write.field);
|
|
337
|
+
const base = { project: board.projectId, item: itemId, field: f.id };
|
|
338
|
+
if ("clear" in write) {
|
|
339
|
+
await t.graphql(CLEAR_FIELD, base);
|
|
340
|
+
}
|
|
341
|
+
else if ("number" in write) {
|
|
342
|
+
// gh -F types integers only: 18.75 as a variable reaches GitHub
|
|
343
|
+
// as a string, which Float! refuses. A JS number is a safe
|
|
344
|
+
// literal.
|
|
345
|
+
if (!Number.isFinite(write.number))
|
|
346
|
+
throw new Error(`Cost ${write.number} is not a number.`);
|
|
347
|
+
await t.graphql(`mutation SetNumber($project: ID!, $item: ID!, $field: ID!) { updateProjectV2ItemFieldValue(input: {projectId: $project, itemId: $item, fieldId: $field, value: {number: ${write.number}}}) { clientMutationId } }`, base);
|
|
348
|
+
}
|
|
349
|
+
else if ("text" in write) {
|
|
350
|
+
await t.graphql(SET_TEXT, { ...base, text: write.text });
|
|
351
|
+
}
|
|
352
|
+
else {
|
|
353
|
+
const option = f.options?.find((x) => sameName(x.name, write.optionName));
|
|
354
|
+
if (!option)
|
|
355
|
+
throw new Error(`The board's ${write.field} field has no ${write.optionName}.`);
|
|
356
|
+
await t.graphql(SET_OPTION, { ...base, option: option.id });
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
},
|
|
360
|
+
async list() {
|
|
361
|
+
const answer = await t.graphql(BOARDS_QUERY, { owner: o.boardOwner ?? repoOwner });
|
|
362
|
+
return (answer.data.owner?.projectsV2?.nodes ?? [])
|
|
363
|
+
.filter((b) => !b.closed)
|
|
364
|
+
.map(({ number, title }) => ({ number, title }));
|
|
365
|
+
},
|
|
366
|
+
async create(title) {
|
|
367
|
+
const ids = await t.graphql(IDS_QUERY, { owner: repoOwner, name: repoName });
|
|
368
|
+
const made = await t.graphql(CREATE_BOARD, {
|
|
369
|
+
owner: ids.data.repository.owner.id,
|
|
370
|
+
repo: ids.data.repository.id,
|
|
371
|
+
title,
|
|
372
|
+
});
|
|
373
|
+
return made.data.createProjectV2.projectV2.number;
|
|
374
|
+
},
|
|
375
|
+
async addColumns(number, columns) {
|
|
376
|
+
const answer = await t.graphql(STATUS_FIELD, { owner: o.boardOwner ?? repoOwner, number });
|
|
377
|
+
const field = answer.data.owner?.projectV2?.field;
|
|
378
|
+
if (!field)
|
|
379
|
+
throw new Error(`Board ${number} has no Status field.`);
|
|
380
|
+
const has = (name) => field.options.find((x) => sameName(x.name, name));
|
|
381
|
+
const added = columns.filter((c) => !has(c));
|
|
382
|
+
if (added.length === 0)
|
|
383
|
+
return [];
|
|
384
|
+
const ordered = columns.map((c) => has(c) ?? { name: c, color: "GRAY", description: "" });
|
|
385
|
+
const rest = field.options.filter((x) => !columns.some((c) => sameName(c, x.name)));
|
|
386
|
+
await t.graphql(setColumnsMutation(field.id, [...ordered, ...rest]));
|
|
387
|
+
return added;
|
|
388
|
+
},
|
|
389
|
+
async addFields(number, fields) {
|
|
390
|
+
const answer = await t.graphql(BOARD_FIELDS, { owner: o.boardOwner ?? repoOwner, number });
|
|
391
|
+
const project = answer.data.owner?.projectV2;
|
|
392
|
+
if (!project)
|
|
393
|
+
throw new Error(`Board ${number} could not be read.`);
|
|
394
|
+
const missing = fields.filter((f) => !project.fields.nodes.some((n) => n.name && sameName(n.name, f.name)));
|
|
395
|
+
for (const f of missing)
|
|
396
|
+
await t.graphql(addFieldMutation(project.id, f));
|
|
397
|
+
return missing.map((f) => f.name);
|
|
398
|
+
},
|
|
399
|
+
},
|
|
400
|
+
blockers: {
|
|
401
|
+
async list(n) {
|
|
402
|
+
const list = await found(`#${n}`, t.rest("GET", `${API}/issues/${n}/dependencies/blocked_by`, { paginate: true }));
|
|
403
|
+
return list.map((b) => ({
|
|
404
|
+
number: b.number,
|
|
405
|
+
state: b.state === "open" ? "open" : "closed",
|
|
406
|
+
}));
|
|
407
|
+
},
|
|
408
|
+
async add(n, by) {
|
|
409
|
+
const blocker = await issue(by);
|
|
410
|
+
await found(`#${n}`, t.rest("POST", `${API}/issues/${n}/dependencies/blocked_by`, {
|
|
411
|
+
fields: { issue_id: blocker.id },
|
|
412
|
+
}));
|
|
413
|
+
},
|
|
414
|
+
async remove(n, by) {
|
|
415
|
+
const blocker = await issue(by);
|
|
416
|
+
await found(`#${n} waiting on #${by}`, t.rest("DELETE", `${API}/issues/${n}/dependencies/blocked_by/${blocker.id}`));
|
|
417
|
+
},
|
|
418
|
+
},
|
|
419
|
+
labelHistory: {
|
|
420
|
+
async wasRemoved(n, label) {
|
|
421
|
+
const events = await found(`#${n}`, t.rest("GET", `${API}/issues/${n}/events?per_page=100`, { paginate: true }));
|
|
422
|
+
return events.some((e) => e.event === "unlabeled" && e.label?.name === label);
|
|
423
|
+
},
|
|
424
|
+
},
|
|
425
|
+
};
|
|
426
|
+
tickets.readAll = async (options = {}) => {
|
|
427
|
+
const pages = [];
|
|
428
|
+
let after = null;
|
|
429
|
+
do {
|
|
430
|
+
const page = await t.graphql(ISSUES_QUERY, {
|
|
431
|
+
owner: repoOwner,
|
|
432
|
+
name: repoName,
|
|
433
|
+
after,
|
|
434
|
+
});
|
|
435
|
+
pages.push(page);
|
|
436
|
+
const info = page.data.repository.open.pageInfo;
|
|
437
|
+
after = info.hasNextPage ? info.endCursor : null;
|
|
438
|
+
} while (after);
|
|
439
|
+
const read = fromIssuesPages(pages);
|
|
440
|
+
if (!options.board)
|
|
441
|
+
return read;
|
|
442
|
+
const board = await readBoard();
|
|
443
|
+
return read.map((x) => ({
|
|
444
|
+
...x,
|
|
445
|
+
card: board.items.get(x.number) ?? null,
|
|
446
|
+
}));
|
|
447
|
+
};
|
|
448
|
+
if (o.cli) {
|
|
449
|
+
const cli = o.cli;
|
|
450
|
+
// The body goes in a file beside the first image, where the skills
|
|
451
|
+
// keep a body and its images, so its `./name.png` references resolve
|
|
452
|
+
// as written.
|
|
453
|
+
const withFile = async (body, files, post) => {
|
|
454
|
+
const dir = files[0] ? dirname(files[0]) : tmpdir();
|
|
455
|
+
const file = join(dir, `.factory-body-${process.pid}-${Date.now()}.md`);
|
|
456
|
+
writeFileSync(file, body);
|
|
457
|
+
try {
|
|
458
|
+
return await post(file);
|
|
459
|
+
}
|
|
460
|
+
finally {
|
|
461
|
+
rmSync(file, { force: true });
|
|
462
|
+
}
|
|
463
|
+
};
|
|
464
|
+
const attach = (files) => files.flatMap((f) => ["--attach", f]);
|
|
465
|
+
tickets.attachments = {
|
|
466
|
+
comment: (n, body, files) => withFile(body, files, async (file) => commentIdFromUrl(await cli([
|
|
467
|
+
"issue",
|
|
468
|
+
"comment",
|
|
469
|
+
String(n),
|
|
470
|
+
"--repo",
|
|
471
|
+
o.repository,
|
|
472
|
+
"--body-file",
|
|
473
|
+
file,
|
|
474
|
+
...attach(files),
|
|
475
|
+
]))),
|
|
476
|
+
writeBody: (n, body, files) => withFile(body, files, async (file) => {
|
|
477
|
+
await cli([
|
|
478
|
+
"issue",
|
|
479
|
+
"edit",
|
|
480
|
+
String(n),
|
|
481
|
+
"--repo",
|
|
482
|
+
o.repository,
|
|
483
|
+
"--body-file",
|
|
484
|
+
file,
|
|
485
|
+
...attach(files),
|
|
486
|
+
]);
|
|
487
|
+
}),
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
return tickets;
|
|
491
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type Gh } from "../lib/gh.mts";
|
|
2
|
+
import { type Settings } from "../config.mts";
|
|
3
|
+
import type { Tickets } from "./types.mts";
|
|
4
|
+
export * from "./types.mts";
|
|
5
|
+
/** The ticket system's message for a type that is not built. */
|
|
6
|
+
export declare const unsupportedType: (type: string) => string;
|
|
7
|
+
export type Opened = {
|
|
8
|
+
tickets: Tickets;
|
|
9
|
+
};
|
|
10
|
+
/** The settings' adapter, reaching its system through the GitHub CLI. The
|
|
11
|
+
* repository and board are the caller's, since a workflow can name its own. */
|
|
12
|
+
export declare function openTickets(settings: Settings, target: {
|
|
13
|
+
repository: string;
|
|
14
|
+
board: number | null;
|
|
15
|
+
}, run?: Gh): Promise<Opened>;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// Which adapter the settings' `tickets.type` picks, built for a command in a
|
|
2
|
+
// session or CI. `github` is the only one; the settings refuse any other
|
|
3
|
+
// value before this runs (`config.mts`).
|
|
4
|
+
import { gh } from "../lib/gh.mjs";
|
|
5
|
+
import { fieldNamesOf } from "../config.mjs";
|
|
6
|
+
import { githubTickets } from "./github.mjs";
|
|
7
|
+
import { ghTransport } from "./transport.mjs";
|
|
8
|
+
export * from "./types.mjs";
|
|
9
|
+
/** The ticket system's message for a type that is not built. */
|
|
10
|
+
export const unsupportedType = (type) => `tickets.type "${type}" is not supported. "github" is the only ticket system built.`;
|
|
11
|
+
/** The settings' adapter, reaching its system through the GitHub CLI. The
|
|
12
|
+
* repository and board are the caller's, since a workflow can name its own. */
|
|
13
|
+
export async function openTickets(settings, target, run = gh) {
|
|
14
|
+
const type = settings.tickets.type;
|
|
15
|
+
if (type !== "github")
|
|
16
|
+
throw new Error(unsupportedType(type));
|
|
17
|
+
const transport = ghTransport(run);
|
|
18
|
+
return {
|
|
19
|
+
tickets: githubTickets({
|
|
20
|
+
transport,
|
|
21
|
+
repository: target.repository,
|
|
22
|
+
board: target.board,
|
|
23
|
+
cli: run,
|
|
24
|
+
fieldNames: fieldNamesOf(settings),
|
|
25
|
+
}),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { Gh } from "../lib/gh.mts";
|
|
2
|
+
export type Method = "GET" | "POST" | "PATCH" | "PUT" | "DELETE";
|
|
3
|
+
export type Fields = Record<string, string | number | boolean | string[] | Record<string, string | number | boolean>>;
|
|
4
|
+
export type Variables = Record<string, string | number | boolean | null>;
|
|
5
|
+
export type RestOptions = {
|
|
6
|
+
fields?: Fields;
|
|
7
|
+
paginate?: boolean;
|
|
8
|
+
body?: unknown;
|
|
9
|
+
};
|
|
10
|
+
export type Transport = {
|
|
11
|
+
/** A REST call. `paginate` reads every page of a list and joins them.
|
|
12
|
+
* `body` is sent whole as JSON, in place of `fields`, for a shape fields
|
|
13
|
+
* cannot hold, such as a list of objects. */
|
|
14
|
+
rest<T>(method: Method, path: string, options?: RestOptions): Promise<T>;
|
|
15
|
+
/** A GraphQL query or mutation; resolves to the whole answer, `data` and
|
|
16
|
+
* all, and rejects when GitHub answers with errors. */
|
|
17
|
+
graphql<T>(query: string, variables?: Variables): Promise<T>;
|
|
18
|
+
/** A GET whose answer is plain text, not JSON, such as a job's log. */
|
|
19
|
+
text(path: string): Promise<string>;
|
|
20
|
+
};
|
|
21
|
+
/** A call GitHub answered with an HTTP error. */
|
|
22
|
+
export declare class HttpError extends Error {
|
|
23
|
+
readonly status: number;
|
|
24
|
+
/** What GitHub said was wrong: the `message` in its answer, or the status
|
|
25
|
+
* text when it gave none. */
|
|
26
|
+
readonly reason: string;
|
|
27
|
+
constructor(status: number, message: string, reason?: string);
|
|
28
|
+
}
|
|
29
|
+
/** The HTTP status in an error a transport threw, or null. */
|
|
30
|
+
export declare function statusOf(error: unknown): number | null;
|
|
31
|
+
/** The GitHub CLI as a transport. */
|
|
32
|
+
export declare function ghTransport(run: Gh): Transport;
|
|
33
|
+
/** The next page's address from a Link header, or null. */
|
|
34
|
+
export declare function nextLink(link: string | null): string | null;
|
|
35
|
+
/** The API called with a token, as the poller does. Each call gives up
|
|
36
|
+
* after `timeoutMs`. */
|
|
37
|
+
export declare function fetchTransport(options: {
|
|
38
|
+
token: string;
|
|
39
|
+
fetch: typeof fetch;
|
|
40
|
+
timeoutMs?: number;
|
|
41
|
+
userAgent?: string;
|
|
42
|
+
}): Transport;
|