@leitwerk-dev/github 0.0.0-bootstrap.0 → 0.2.1

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