@leitwerk-dev/github 0.0.0-bootstrap.0 → 0.2.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/README.md +113 -1
- package/dist/LICENSE +13 -0
- package/dist/authorization.d.ts +12 -0
- package/dist/authorization.d.ts.map +1 -0
- package/dist/binding.d.ts +4 -0
- package/dist/binding.d.ts.map +1 -0
- package/dist/capability.d.ts +8 -0
- package/dist/capability.d.ts.map +1 -0
- package/dist/chunk-PVE52UFH.js +318 -0
- package/dist/chunk-PVE52UFH.js.map +1 -0
- package/dist/client.d.ts +111 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/external.d.ts +85 -0
- package/dist/external.d.ts.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +809 -0
- package/dist/index.js.map +1 -0
- package/dist/issue-provider.d.ts +8 -0
- package/dist/issue-provider.d.ts.map +1 -0
- package/dist/issue-watcher.d.ts +19 -0
- package/dist/issue-watcher.d.ts.map +1 -0
- package/dist/poll-report.d.ts +22 -0
- package/dist/poll-report.d.ts.map +1 -0
- package/dist/provider.d.ts +6 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/testing.d.ts +81 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +297 -0
- package/dist/testing.js.map +1 -0
- package/dist/tools.d.ts +5 -0
- package/dist/tools.d.ts.map +1 -0
- package/package.json +55 -14
- package/src/authorization.ts +54 -0
- package/src/binding.ts +6 -0
- package/src/capability.ts +13 -0
- package/src/client.ts +421 -0
- package/src/external.ts +139 -0
- package/src/index.ts +45 -0
- package/src/issue-provider.ts +278 -0
- package/src/issue-watcher.ts +30 -0
- package/src/poll-report.ts +47 -0
- package/src/provider.ts +216 -0
- package/src/testing.ts +384 -0
- package/src/tools.ts +304 -0
package/src/tools.ts
ADDED
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createWriteIdentity,
|
|
3
|
+
type ExternalWriteLogRepoLike,
|
|
4
|
+
ensureWrite,
|
|
5
|
+
recordWriteIfMissing,
|
|
6
|
+
} from "@leitwerk-dev/external-writes";
|
|
7
|
+
import {
|
|
8
|
+
type IntegrationToolExecutionContext,
|
|
9
|
+
numberArg,
|
|
10
|
+
objectArg,
|
|
11
|
+
projectParameters,
|
|
12
|
+
type ServerExtensionAPI,
|
|
13
|
+
stringArg,
|
|
14
|
+
} from "@leitwerk-dev/process-sdk";
|
|
15
|
+
import { resolveGitHubProjectBinding } from "./binding.js";
|
|
16
|
+
import type { GitHubIntegration } from "./capability.js";
|
|
17
|
+
import { assertGitHubRepository } from "./client.js";
|
|
18
|
+
|
|
19
|
+
const object = (value: unknown) => objectArg(value, "Expected an object");
|
|
20
|
+
|
|
21
|
+
async function reconcileComment(
|
|
22
|
+
comments: Promise<Array<{ body?: unknown }>>,
|
|
23
|
+
marker: string,
|
|
24
|
+
body: () => string,
|
|
25
|
+
post: (body: string) => Promise<unknown>,
|
|
26
|
+
) {
|
|
27
|
+
const existing = (await comments).find((comment) => String(comment.body).includes(marker));
|
|
28
|
+
return existing ?? post(`${body()}\n\n${marker}`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function registerGitHubTools(
|
|
32
|
+
api: ServerExtensionAPI,
|
|
33
|
+
integration: GitHubIntegration,
|
|
34
|
+
writes: ExternalWriteLogRepoLike,
|
|
35
|
+
) {
|
|
36
|
+
const target = (ctx: IntegrationToolExecutionContext) => {
|
|
37
|
+
const binding = resolveGitHubProjectBinding(ctx);
|
|
38
|
+
assertGitHubRepository(
|
|
39
|
+
integration.client(binding.profile).profile,
|
|
40
|
+
binding.owner,
|
|
41
|
+
binding.repo,
|
|
42
|
+
);
|
|
43
|
+
return binding;
|
|
44
|
+
};
|
|
45
|
+
api.tool({
|
|
46
|
+
name: "github_resolve_git_identity",
|
|
47
|
+
description: "Resolve the configured GitHub Git identity",
|
|
48
|
+
parameters: {
|
|
49
|
+
type: "object",
|
|
50
|
+
properties: { projectKey: { type: "string" } },
|
|
51
|
+
required: ["projectKey"],
|
|
52
|
+
},
|
|
53
|
+
async execute(ctx) {
|
|
54
|
+
const t = target(ctx);
|
|
55
|
+
return integration.client(t.profile).resolveGitIdentity(t.profile);
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
for (const name of [
|
|
59
|
+
"github_ensure_label",
|
|
60
|
+
"github_update_issue",
|
|
61
|
+
"github_add_issue_comment",
|
|
62
|
+
"github_add_pull_request_feedback_reaction",
|
|
63
|
+
"github_reply_to_pull_request_feedback",
|
|
64
|
+
] as const) {
|
|
65
|
+
api.tool({
|
|
66
|
+
name,
|
|
67
|
+
description: "Reconcile GitHub delivery in the current process project",
|
|
68
|
+
parameters: {
|
|
69
|
+
type: "object",
|
|
70
|
+
properties: {
|
|
71
|
+
projectKey: { type: "string" },
|
|
72
|
+
issueNumber: { type: "integer" },
|
|
73
|
+
pullRequestNumber: { type: "integer" },
|
|
74
|
+
name: { type: "string" },
|
|
75
|
+
patch: { type: "object" },
|
|
76
|
+
body: { type: "string" },
|
|
77
|
+
feedbackKind: { type: "string" },
|
|
78
|
+
feedbackId: { type: "integer" },
|
|
79
|
+
writeKey: { type: "string" },
|
|
80
|
+
},
|
|
81
|
+
required: ["projectKey"],
|
|
82
|
+
},
|
|
83
|
+
async execute(ctx, args) {
|
|
84
|
+
const input = object(args);
|
|
85
|
+
const t = target(ctx);
|
|
86
|
+
const client = integration.client(t.profile);
|
|
87
|
+
const key =
|
|
88
|
+
typeof input.writeKey === "string" ? stringArg(input, "writeKey") : ctx.idempotencyKey;
|
|
89
|
+
const marker = `<!-- leitwerk-write:${ctx.process.id}:${key} -->`;
|
|
90
|
+
let value: unknown;
|
|
91
|
+
const result = await ensureWrite(
|
|
92
|
+
writes,
|
|
93
|
+
ctx.process.id,
|
|
94
|
+
createWriteIdentity(name, key),
|
|
95
|
+
async () => {
|
|
96
|
+
if (name === "github_ensure_label")
|
|
97
|
+
value = await client.ensureLabel(t.owner, t.repo, stringArg(input, "name"));
|
|
98
|
+
else if (name === "github_update_issue") {
|
|
99
|
+
const issueNumber = numberArg(input, "issueNumber");
|
|
100
|
+
const current = await client.getIssue(t.owner, t.repo, issueNumber);
|
|
101
|
+
const patch = object(input.patch);
|
|
102
|
+
const same = Object.entries(patch).every(
|
|
103
|
+
([key, expected]) =>
|
|
104
|
+
JSON.stringify(
|
|
105
|
+
key === "labels"
|
|
106
|
+
? current.labels.map((label) => label.name).sort()
|
|
107
|
+
: object(current)[key],
|
|
108
|
+
) ===
|
|
109
|
+
JSON.stringify(
|
|
110
|
+
key === "labels" && Array.isArray(expected) ? [...expected].sort() : expected,
|
|
111
|
+
),
|
|
112
|
+
);
|
|
113
|
+
value = same
|
|
114
|
+
? current
|
|
115
|
+
: await client.updateIssue(t.owner, t.repo, issueNumber, patch);
|
|
116
|
+
} else if (name === "github_add_pull_request_feedback_reaction") {
|
|
117
|
+
const kind = stringArg(input, "feedbackKind");
|
|
118
|
+
const id = numberArg(input, "feedbackId");
|
|
119
|
+
const reactions = await client.listFeedbackReactions(t.owner, t.repo, kind, id);
|
|
120
|
+
value =
|
|
121
|
+
reactions.find(
|
|
122
|
+
(reaction) =>
|
|
123
|
+
reaction.content === "eyes" &&
|
|
124
|
+
reaction.user.login.toLowerCase() === client.profile.botLogin.toLowerCase(),
|
|
125
|
+
) ?? (await client.addFeedbackReaction(t.owner, t.repo, kind, id));
|
|
126
|
+
} else {
|
|
127
|
+
const issueNumber = numberArg(
|
|
128
|
+
{ issueNumber: input.issueNumber ?? input.pullRequestNumber },
|
|
129
|
+
"issueNumber",
|
|
130
|
+
);
|
|
131
|
+
value = await reconcileComment(
|
|
132
|
+
name === "github_reply_to_pull_request_feedback"
|
|
133
|
+
? client.listFeedbackReplies(
|
|
134
|
+
t.owner,
|
|
135
|
+
t.repo,
|
|
136
|
+
issueNumber,
|
|
137
|
+
stringArg(input, "feedbackKind"),
|
|
138
|
+
)
|
|
139
|
+
: client.listIssueComments(t.owner, t.repo, issueNumber),
|
|
140
|
+
marker,
|
|
141
|
+
() => stringArg(input, "body"),
|
|
142
|
+
(body) =>
|
|
143
|
+
name === "github_add_issue_comment"
|
|
144
|
+
? client.addIssueComment(t.owner, t.repo, issueNumber, body)
|
|
145
|
+
: client.replyFeedback(
|
|
146
|
+
t.owner,
|
|
147
|
+
t.repo,
|
|
148
|
+
issueNumber,
|
|
149
|
+
stringArg(input, "feedbackKind"),
|
|
150
|
+
numberArg(input, "feedbackId"),
|
|
151
|
+
body,
|
|
152
|
+
),
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
return { value };
|
|
156
|
+
},
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
if (name === "github_ensure_label") return { name: stringArg(input, "name") };
|
|
160
|
+
return value ?? result;
|
|
161
|
+
},
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
api.tool<Record<string, unknown>>({
|
|
166
|
+
name: "github_ensure_pull_request",
|
|
167
|
+
description: "Create a GitHub pull request unless the branch pair already has one",
|
|
168
|
+
parameters: projectParameters({
|
|
169
|
+
title: { type: "string" },
|
|
170
|
+
body: { type: "string" },
|
|
171
|
+
head: { type: "string" },
|
|
172
|
+
base: { type: "string" },
|
|
173
|
+
}),
|
|
174
|
+
async execute(ctx, input) {
|
|
175
|
+
const t = target(ctx);
|
|
176
|
+
const client = integration.client(t.profile);
|
|
177
|
+
const head = stringArg(input, "head");
|
|
178
|
+
const base = stringArg(input, "base");
|
|
179
|
+
if (head !== ctx.project?.workBranch || base !== ctx.project?.baseBranch)
|
|
180
|
+
throw new Error("Pull request branches must match the process project");
|
|
181
|
+
const find = async () =>
|
|
182
|
+
(await client.listPullRequests(t.owner, t.repo, "all")).find(
|
|
183
|
+
(candidate) => candidate.head.ref === head && candidate.base.ref === base,
|
|
184
|
+
) ?? null;
|
|
185
|
+
let pr = await find();
|
|
186
|
+
const identity = createWriteIdentity("github.ensure_pr", ctx.idempotencyKey);
|
|
187
|
+
if (!pr) {
|
|
188
|
+
try {
|
|
189
|
+
await ensureWrite(writes, ctx.process.id, identity, async () => {
|
|
190
|
+
pr = await client.createPullRequest(t.owner, t.repo, {
|
|
191
|
+
title: stringArg(input, "title"),
|
|
192
|
+
body: stringArg(input, "body"),
|
|
193
|
+
head,
|
|
194
|
+
base,
|
|
195
|
+
});
|
|
196
|
+
return { number: pr.number, url: pr.html_url };
|
|
197
|
+
});
|
|
198
|
+
} catch (error) {
|
|
199
|
+
pr = await find();
|
|
200
|
+
if (!pr) throw error;
|
|
201
|
+
}
|
|
202
|
+
pr ??= await find();
|
|
203
|
+
}
|
|
204
|
+
if (!pr) throw new Error("GitHub pull request creation could not be reconciled");
|
|
205
|
+
recordWriteIfMissing(writes, ctx.process.id, identity, {
|
|
206
|
+
number: pr.number,
|
|
207
|
+
url: pr.html_url,
|
|
208
|
+
});
|
|
209
|
+
return pr;
|
|
210
|
+
},
|
|
211
|
+
});
|
|
212
|
+
for (const [name, description, numberName, method] of [
|
|
213
|
+
[
|
|
214
|
+
"github_get_issue",
|
|
215
|
+
"Read a GitHub issue in the current process project",
|
|
216
|
+
"issueNumber",
|
|
217
|
+
"getIssue",
|
|
218
|
+
],
|
|
219
|
+
[
|
|
220
|
+
"github_get_pull_request",
|
|
221
|
+
"Read a GitHub pull request in the current process project",
|
|
222
|
+
"pullRequestNumber",
|
|
223
|
+
"getPullRequest",
|
|
224
|
+
],
|
|
225
|
+
[
|
|
226
|
+
"github_list_pull_request_feedback",
|
|
227
|
+
"Read GitHub pull request conversation, reviews, and inline comments",
|
|
228
|
+
"pullRequestNumber",
|
|
229
|
+
"listActionablePullRequestFeedback",
|
|
230
|
+
],
|
|
231
|
+
] as const) {
|
|
232
|
+
api.tool<Record<string, unknown>>({
|
|
233
|
+
name,
|
|
234
|
+
description,
|
|
235
|
+
parameters: projectParameters({ [numberName]: { type: "integer" } }),
|
|
236
|
+
async execute(ctx, input) {
|
|
237
|
+
const t = target(ctx);
|
|
238
|
+
return integration
|
|
239
|
+
.client(t.profile)
|
|
240
|
+
[method](t.owner, t.repo, numberArg(input, numberName), ctx.signal);
|
|
241
|
+
},
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
api.tool<Record<string, unknown>>({
|
|
245
|
+
name: "github_get_checks",
|
|
246
|
+
description: "Read GitHub Actions check runs for a commit",
|
|
247
|
+
parameters: projectParameters({ headSha: { type: "string" } }),
|
|
248
|
+
async execute(ctx, input) {
|
|
249
|
+
const t = target(ctx);
|
|
250
|
+
return integration
|
|
251
|
+
.client(t.profile)
|
|
252
|
+
.getCheckSummary(t.owner, t.repo, stringArg(input, "headSha"));
|
|
253
|
+
},
|
|
254
|
+
});
|
|
255
|
+
for (const definition of [
|
|
256
|
+
{ name: "github_add_pull_request_comment", update: false },
|
|
257
|
+
{ name: "github_update_pull_request", update: true },
|
|
258
|
+
] as const) {
|
|
259
|
+
api.tool<Record<string, unknown>>({
|
|
260
|
+
name: definition.name,
|
|
261
|
+
description: definition.update
|
|
262
|
+
? "Update a GitHub pull request"
|
|
263
|
+
: "Comment on a GitHub pull request",
|
|
264
|
+
parameters: projectParameters(
|
|
265
|
+
{
|
|
266
|
+
pullRequestNumber: { type: "integer" },
|
|
267
|
+
...(definition.update
|
|
268
|
+
? { patch: { type: "object" } }
|
|
269
|
+
: { body: { type: "string" }, writeKey: { type: "string" } }),
|
|
270
|
+
},
|
|
271
|
+
["pullRequestNumber", definition.update ? "patch" : "body"],
|
|
272
|
+
),
|
|
273
|
+
async execute(ctx, input) {
|
|
274
|
+
const t = target(ctx);
|
|
275
|
+
const pr = numberArg(input, "pullRequestNumber");
|
|
276
|
+
const key =
|
|
277
|
+
!definition.update && typeof input.writeKey === "string"
|
|
278
|
+
? stringArg(input, "writeKey")
|
|
279
|
+
: ctx.idempotencyKey;
|
|
280
|
+
return ensureWrite(
|
|
281
|
+
writes,
|
|
282
|
+
ctx.process.id,
|
|
283
|
+
createWriteIdentity(definition.update ? "github.update_pr" : "github.comment", key),
|
|
284
|
+
async () => {
|
|
285
|
+
if (definition.update)
|
|
286
|
+
await integration
|
|
287
|
+
.client(t.profile)
|
|
288
|
+
.updatePullRequest(t.owner, t.repo, pr, object(input.patch), ctx.signal);
|
|
289
|
+
else {
|
|
290
|
+
const client = integration.client(t.profile);
|
|
291
|
+
await reconcileComment(
|
|
292
|
+
client.listIssueComments(t.owner, t.repo, pr, ctx.signal),
|
|
293
|
+
`<!-- leitwerk-write:${ctx.process.id}:${key} -->`,
|
|
294
|
+
() => stringArg(input, "body"),
|
|
295
|
+
(body) => client.addIssueComment(t.owner, t.repo, pr, body, ctx.signal),
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
return { owner: t.owner, repo: t.repo, pullRequestNumber: pr };
|
|
299
|
+
},
|
|
300
|
+
);
|
|
301
|
+
},
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
}
|