@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.
- package/README.md +139 -1
- package/dist/LICENSE +13 -0
- package/dist/authorization.d.ts +16 -0
- package/dist/authorization.d.ts.map +1 -0
- package/dist/binding.d.ts +5 -0
- package/dist/binding.d.ts.map +1 -0
- package/dist/capability.d.ts +11 -0
- package/dist/capability.d.ts.map +1 -0
- package/dist/chunk-6X3F3VHK.js +344 -0
- package/dist/chunk-6X3F3VHK.js.map +1 -0
- package/dist/client.d.ts +200 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/external.d.ts +150 -0
- package/dist/external.d.ts.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +790 -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 +31 -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 +8 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/testing/tool-fixture.d.ts +45 -0
- package/dist/testing/tool-fixture.d.ts.map +1 -0
- package/dist/testing.d.ts +145 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +322 -0
- package/dist/testing.js.map +1 -0
- package/dist/tools.d.ts +4 -0
- package/dist/tools.d.ts.map +1 -0
- package/package.json +54 -14
- package/src/authorization.ts +62 -0
- package/src/binding.ts +7 -0
- package/src/capability.ts +16 -0
- package/src/client.ts +526 -0
- package/src/external.ts +192 -0
- package/src/index.ts +65 -0
- package/src/issue-provider.ts +278 -0
- package/src/issue-watcher.ts +45 -0
- package/src/poll-report.ts +47 -0
- package/src/provider.ts +216 -0
- package/src/testing/tool-fixture.ts +126 -0
- package/src/testing.ts +466 -0
- package/src/tools.ts +287 -0
package/src/client.ts
ADDED
|
@@ -0,0 +1,526 @@
|
|
|
1
|
+
import { asUnknownRecord } from "@leitwerk-dev/domain";
|
|
2
|
+
import {
|
|
3
|
+
type RepositoryFeedbackItem as GitHubFeedbackItem,
|
|
4
|
+
type RepositoryIssue as GitHubIssue,
|
|
5
|
+
type RepositoryPullRequest as GitHubPullRequest,
|
|
6
|
+
IntegrationHttpError,
|
|
7
|
+
normalizeRepositoryFeedback as normalize,
|
|
8
|
+
RepositoryHttpClient,
|
|
9
|
+
} from "@leitwerk-dev/process-sdk";
|
|
10
|
+
|
|
11
|
+
import { actionableFeedback, authorizedTrigger } from "./authorization.js";
|
|
12
|
+
|
|
13
|
+
export type { GitHubFeedbackItem, GitHubIssue, GitHubPullRequest };
|
|
14
|
+
|
|
15
|
+
/** @public */
|
|
16
|
+
export interface GitHubRepository {
|
|
17
|
+
/** @public */
|
|
18
|
+
name: string;
|
|
19
|
+
/** @internal */
|
|
20
|
+
full_name: string;
|
|
21
|
+
/** @public */
|
|
22
|
+
owner: {
|
|
23
|
+
/** @public */
|
|
24
|
+
login: string;
|
|
25
|
+
};
|
|
26
|
+
/** @public */
|
|
27
|
+
ssh_url: string;
|
|
28
|
+
/** @public */
|
|
29
|
+
default_branch: string;
|
|
30
|
+
/** @internal */
|
|
31
|
+
html_url: string;
|
|
32
|
+
/** @internal */
|
|
33
|
+
archived: boolean;
|
|
34
|
+
/** @internal */
|
|
35
|
+
has_issues: boolean;
|
|
36
|
+
}
|
|
37
|
+
/** @public */
|
|
38
|
+
export interface GitHubGitIdentity {
|
|
39
|
+
/** @internal */
|
|
40
|
+
provider: "github";
|
|
41
|
+
/** @internal */
|
|
42
|
+
profile: string;
|
|
43
|
+
/** @internal */
|
|
44
|
+
login: string;
|
|
45
|
+
/** @public */
|
|
46
|
+
name: string;
|
|
47
|
+
/** @public */
|
|
48
|
+
email: string;
|
|
49
|
+
}
|
|
50
|
+
/** @public */
|
|
51
|
+
export interface GitHubLabelEvent {
|
|
52
|
+
/** @internal */
|
|
53
|
+
id: number;
|
|
54
|
+
/** @internal */
|
|
55
|
+
event: string;
|
|
56
|
+
/** @internal */
|
|
57
|
+
label?: {
|
|
58
|
+
/** @internal */
|
|
59
|
+
name: string;
|
|
60
|
+
};
|
|
61
|
+
/** @internal */
|
|
62
|
+
actor: {
|
|
63
|
+
/** @internal */
|
|
64
|
+
login: string;
|
|
65
|
+
} | null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** @internal */
|
|
69
|
+
interface GitHubLabel {
|
|
70
|
+
/** @internal */
|
|
71
|
+
id: number;
|
|
72
|
+
/** @internal */
|
|
73
|
+
name: string;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** @internal */
|
|
77
|
+
export interface GitHubProfile {
|
|
78
|
+
/** @internal */
|
|
79
|
+
apiBaseUrl: string;
|
|
80
|
+
/** @internal */
|
|
81
|
+
token: string;
|
|
82
|
+
/** @internal */
|
|
83
|
+
botLogin: string;
|
|
84
|
+
/** @internal */
|
|
85
|
+
allowedOrganization?: string;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** @public */
|
|
89
|
+
export interface GitHubCheckSummary {
|
|
90
|
+
/** @public */
|
|
91
|
+
headSha: string;
|
|
92
|
+
/** @public */
|
|
93
|
+
status: "pending" | "success" | "failure";
|
|
94
|
+
/** @public */
|
|
95
|
+
total: number;
|
|
96
|
+
/** @public */
|
|
97
|
+
failed: Array<{
|
|
98
|
+
/** @public */
|
|
99
|
+
name: string;
|
|
100
|
+
/** @public */
|
|
101
|
+
conclusion: string | null;
|
|
102
|
+
/** @public */
|
|
103
|
+
url: string | null;
|
|
104
|
+
}>;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** @public */
|
|
108
|
+
export interface GitHubRelease {
|
|
109
|
+
/** @internal */
|
|
110
|
+
id: number;
|
|
111
|
+
/** @public */
|
|
112
|
+
tag_name: string;
|
|
113
|
+
/** @internal */
|
|
114
|
+
target_commitish: string;
|
|
115
|
+
/** @internal */
|
|
116
|
+
draft: boolean;
|
|
117
|
+
/** @internal */
|
|
118
|
+
prerelease: boolean;
|
|
119
|
+
/** @internal */
|
|
120
|
+
html_url: string;
|
|
121
|
+
/** @public */
|
|
122
|
+
assets: Array<{
|
|
123
|
+
/** @internal */
|
|
124
|
+
name: string;
|
|
125
|
+
/** @internal */
|
|
126
|
+
url: string;
|
|
127
|
+
/** @internal */
|
|
128
|
+
browser_download_url: string;
|
|
129
|
+
}>;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** @internal */
|
|
133
|
+
export function parseGitHubProfiles(value: unknown): Map<string, GitHubProfile> {
|
|
134
|
+
const profiles = new Map<string, GitHubProfile>();
|
|
135
|
+
for (const [name, raw] of Object.entries(
|
|
136
|
+
asUnknownRecord(asUnknownRecord(value)?.profiles) ?? {},
|
|
137
|
+
)) {
|
|
138
|
+
const config = asUnknownRecord(raw) ?? {};
|
|
139
|
+
const apiBaseUrl =
|
|
140
|
+
typeof config.api_base_url === "string"
|
|
141
|
+
? config.api_base_url.replace(/\/+$/, "")
|
|
142
|
+
: "https://api.github.com";
|
|
143
|
+
const token = typeof config.token === "string" ? config.token.trim() : "";
|
|
144
|
+
const botLogin = typeof config.bot_login === "string" ? config.bot_login.trim() : "leitwerk";
|
|
145
|
+
if (!/^https:\/\//.test(apiBaseUrl))
|
|
146
|
+
throw new Error(`GitHub profile '${name}' requires an HTTPS api_base_url`);
|
|
147
|
+
if (!token) throw new Error(`GitHub profile '${name}' requires a token`);
|
|
148
|
+
if (!botLogin) throw new Error(`GitHub profile '${name}' requires bot_login`);
|
|
149
|
+
const allowedOrganization = config.allowed_organization;
|
|
150
|
+
if (
|
|
151
|
+
allowedOrganization !== undefined &&
|
|
152
|
+
(typeof allowedOrganization !== "string" || !/^[a-z0-9-]+$/i.test(allowedOrganization))
|
|
153
|
+
) {
|
|
154
|
+
throw new Error(`GitHub profile '${name}' requires a valid allowed_organization`);
|
|
155
|
+
}
|
|
156
|
+
profiles.set(name, {
|
|
157
|
+
apiBaseUrl,
|
|
158
|
+
token,
|
|
159
|
+
botLogin,
|
|
160
|
+
...(typeof allowedOrganization === "string"
|
|
161
|
+
? { allowedOrganization: allowedOrganization.toLowerCase() }
|
|
162
|
+
: {}),
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
return profiles;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** @internal */
|
|
169
|
+
export function assertGitHubRepository(profile: GitHubProfile, owner: string, repo: string): void {
|
|
170
|
+
if (
|
|
171
|
+
!/^[a-z0-9-]+$/i.test(owner) ||
|
|
172
|
+
!/^[a-z0-9_.-]+$/i.test(repo) ||
|
|
173
|
+
repo === "." ||
|
|
174
|
+
repo === ".."
|
|
175
|
+
) {
|
|
176
|
+
throw new Error("Invalid GitHub repository");
|
|
177
|
+
}
|
|
178
|
+
if (
|
|
179
|
+
profile.allowedOrganization &&
|
|
180
|
+
owner.toLowerCase() !== profile.allowedOrganization.toLowerCase()
|
|
181
|
+
) {
|
|
182
|
+
throw new Error(`GitHub project must belong to ${profile.allowedOrganization}`);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** @public */
|
|
187
|
+
export class GitHubClient extends RepositoryHttpClient {
|
|
188
|
+
/** @internal */
|
|
189
|
+
constructor(
|
|
190
|
+
/** @internal */
|
|
191
|
+
readonly profile: GitHubProfile,
|
|
192
|
+
) {
|
|
193
|
+
super("GitHub", profile.apiBaseUrl, {
|
|
194
|
+
Accept: "application/vnd.github+json",
|
|
195
|
+
Authorization: `Bearer ${profile.token}`,
|
|
196
|
+
"X-GitHub-Api-Version": "2022-11-28",
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** @internal */
|
|
201
|
+
protected override response(path: string, init: RequestInit = {}): Promise<Response> {
|
|
202
|
+
return super.response(path, {
|
|
203
|
+
...init,
|
|
204
|
+
redirect: "error",
|
|
205
|
+
signal: init.signal ?? AbortSignal.timeout(30_000),
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** @internal */
|
|
210
|
+
protected override repositoryPath(owner: string, repo: string): string {
|
|
211
|
+
assertGitHubRepository(this.profile, owner, repo);
|
|
212
|
+
return super.repositoryPath(owner, repo);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** @internal */
|
|
216
|
+
async listRepositories() {
|
|
217
|
+
return (
|
|
218
|
+
await this.pages<GitHubRepository>(
|
|
219
|
+
this.profile.allowedOrganization
|
|
220
|
+
? `/orgs/${this.profile.allowedOrganization}/repos?type=all`
|
|
221
|
+
: "/user/repos?affiliation=owner,collaborator,organization_member",
|
|
222
|
+
)
|
|
223
|
+
).filter(
|
|
224
|
+
(repo) =>
|
|
225
|
+
(!this.profile.allowedOrganization ||
|
|
226
|
+
repo.owner.login.toLowerCase() === this.profile.allowedOrganization.toLowerCase()) &&
|
|
227
|
+
!repo.archived &&
|
|
228
|
+
repo.has_issues,
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
/** @internal */
|
|
232
|
+
async listOpenIssues(owner: string, repo: string) {
|
|
233
|
+
return (
|
|
234
|
+
await this.pages<
|
|
235
|
+
GitHubIssue & {
|
|
236
|
+
/** @internal */
|
|
237
|
+
pull_request?: unknown;
|
|
238
|
+
}
|
|
239
|
+
>(`${this.repositoryPath(owner, repo)}/issues?state=open`)
|
|
240
|
+
).filter((issue) => !issue.pull_request);
|
|
241
|
+
}
|
|
242
|
+
/** @internal */
|
|
243
|
+
listIssueEvents(owner: string, repo: string, number: number) {
|
|
244
|
+
return this.pages<GitHubLabelEvent>(
|
|
245
|
+
`${this.repositoryPath(owner, repo)}/issues/${number}/events`,
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
/** @internal */
|
|
249
|
+
async isOrganizationMember(login: string): Promise<boolean> {
|
|
250
|
+
if (!this.profile.allowedOrganization) return true;
|
|
251
|
+
if (!/^[a-z0-9-]+$/i.test(login)) return false;
|
|
252
|
+
try {
|
|
253
|
+
const response = await this.response(
|
|
254
|
+
`/orgs/${this.profile.allowedOrganization}/members/${encodeURIComponent(login)}`,
|
|
255
|
+
);
|
|
256
|
+
return response.status === 204;
|
|
257
|
+
} catch (error) {
|
|
258
|
+
if (error instanceof IntegrationHttpError) {
|
|
259
|
+
if (error.status === 404) return false;
|
|
260
|
+
throw new Error(`GitHub membership check failed with ${error.status}`);
|
|
261
|
+
}
|
|
262
|
+
throw error;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
/** @internal */
|
|
266
|
+
async authorizedTrigger(
|
|
267
|
+
owner: string,
|
|
268
|
+
repo: string,
|
|
269
|
+
number: number,
|
|
270
|
+
trigger: string,
|
|
271
|
+
done: string,
|
|
272
|
+
) {
|
|
273
|
+
return authorizedTrigger(this, owner, repo, number, trigger, done);
|
|
274
|
+
}
|
|
275
|
+
/** @public */
|
|
276
|
+
async resolveGitIdentity(profile: string): Promise<GitHubGitIdentity> {
|
|
277
|
+
const user = await this.request<{ login: string; name: string | null; id: number }>("/user");
|
|
278
|
+
if (user.login !== this.profile.botLogin)
|
|
279
|
+
throw new Error("GitHub authenticated user does not match bot_login");
|
|
280
|
+
return {
|
|
281
|
+
provider: "github",
|
|
282
|
+
profile,
|
|
283
|
+
login: user.login,
|
|
284
|
+
name: user.name || user.login,
|
|
285
|
+
email: `${user.id}+${user.login}@users.noreply.github.com`,
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
/** @public */
|
|
289
|
+
updateIssue(owner: string, repo: string, number: number, patch: Record<string, unknown>) {
|
|
290
|
+
return this.request<GitHubIssue>(`${this.repositoryPath(owner, repo)}/issues/${number}`, {
|
|
291
|
+
method: "PATCH",
|
|
292
|
+
body: JSON.stringify(patch),
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
/** @internal */
|
|
296
|
+
listLabels(owner: string, repo: string) {
|
|
297
|
+
return this.pages<GitHubLabel>(`${this.repositoryPath(owner, repo)}/labels`);
|
|
298
|
+
}
|
|
299
|
+
/** @internal */
|
|
300
|
+
createLabel(owner: string, repo: string, name: string) {
|
|
301
|
+
return this.request<GitHubLabel>(`${this.repositoryPath(owner, repo)}/labels`, {
|
|
302
|
+
method: "POST",
|
|
303
|
+
body: JSON.stringify({ name, color: "238636" }),
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
/** @internal */
|
|
307
|
+
async ensureLabel(owner: string, repo: string, name: string) {
|
|
308
|
+
const labels = await this.listLabels(owner, repo);
|
|
309
|
+
return labels.find((label) => label.name === name) ?? this.createLabel(owner, repo, name);
|
|
310
|
+
}
|
|
311
|
+
/** @internal */
|
|
312
|
+
addFeedbackReaction(owner: string, repo: string, kind: string, id: number) {
|
|
313
|
+
const segment = kind === "inline" ? "pulls/comments" : "issues/comments";
|
|
314
|
+
return this.request(`${this.repositoryPath(owner, repo)}/${segment}/${id}/reactions`, {
|
|
315
|
+
method: "POST",
|
|
316
|
+
body: JSON.stringify({ content: "eyes" }),
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
/** @internal */
|
|
320
|
+
async replyFeedback(
|
|
321
|
+
owner: string,
|
|
322
|
+
repo: string,
|
|
323
|
+
pr: number,
|
|
324
|
+
kind: string,
|
|
325
|
+
id: number,
|
|
326
|
+
body: string,
|
|
327
|
+
) {
|
|
328
|
+
if (kind === "inline")
|
|
329
|
+
return this.writeJson(
|
|
330
|
+
`${this.repositoryPath(owner, repo)}/pulls/${pr}/comments/${id}/replies`,
|
|
331
|
+
"POST",
|
|
332
|
+
{ body },
|
|
333
|
+
);
|
|
334
|
+
return this.addIssueComment(owner, repo, pr, body);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/** @internal */
|
|
338
|
+
getCommit(owner: string, repo: string, ref: string) {
|
|
339
|
+
return this.request<{
|
|
340
|
+
/** @internal */
|
|
341
|
+
sha: string;
|
|
342
|
+
}>(`${this.repositoryPath(owner, repo)}/commits/${encodeURIComponent(ref)}`);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** @public */
|
|
346
|
+
async isAncestor(owner: string, repo: string, ancestor: string, descendant: string) {
|
|
347
|
+
const comparison = await this.request<{ status: string }>(
|
|
348
|
+
`${this.repositoryPath(owner, repo)}/compare/${encodeURIComponent(ancestor)}...${encodeURIComponent(descendant)}`,
|
|
349
|
+
);
|
|
350
|
+
return comparison.status === "identical" || comparison.status === "ahead";
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/** @public */
|
|
354
|
+
async listPullRequestFeedback(
|
|
355
|
+
owner: string,
|
|
356
|
+
repo: string,
|
|
357
|
+
number: number,
|
|
358
|
+
signal?: AbortSignal,
|
|
359
|
+
): Promise<GitHubFeedbackItem[]> {
|
|
360
|
+
const prefix = `${this.repositoryPath(owner, repo)}`;
|
|
361
|
+
const [conversation, reviews, inline] = await Promise.all([
|
|
362
|
+
this.pages<Record<string, unknown>>(`${prefix}/issues/${number}/comments`, signal),
|
|
363
|
+
this.pages<Record<string, unknown>>(`${prefix}/pulls/${number}/reviews`, signal),
|
|
364
|
+
this.pages<Record<string, unknown>>(`${prefix}/pulls/${number}/comments`, signal),
|
|
365
|
+
]);
|
|
366
|
+
|
|
367
|
+
const candidates = [
|
|
368
|
+
...conversation.map((item) => ({ kind: "conversation" as const, item })),
|
|
369
|
+
...reviews.map((item) => ({ kind: "review" as const, item })),
|
|
370
|
+
...inline.map((item) => ({ kind: "inline" as const, item })),
|
|
371
|
+
];
|
|
372
|
+
const feedback: GitHubFeedbackItem[] = [];
|
|
373
|
+
const needsProvenance: typeof candidates = [];
|
|
374
|
+
for (const candidate of candidates) {
|
|
375
|
+
const { kind, item } = candidate;
|
|
376
|
+
const normalized = normalize(kind, item);
|
|
377
|
+
if (!normalized) continue;
|
|
378
|
+
const createdAt = typeof item.created_at === "string" ? Date.parse(item.created_at) : NaN;
|
|
379
|
+
const updatedAt = typeof item.updated_at === "string" ? Date.parse(item.updated_at) : NaN;
|
|
380
|
+
if (
|
|
381
|
+
!this.profile.allowedOrganization ||
|
|
382
|
+
(kind !== "review" && Number.isFinite(createdAt) && createdAt === updatedAt)
|
|
383
|
+
) {
|
|
384
|
+
feedback.push(normalized);
|
|
385
|
+
} else if (typeof item.node_id === "string" && item.node_id) {
|
|
386
|
+
// REST attributes edited bodies to their original author. Reviews also
|
|
387
|
+
// omit edit timestamps, so obtain the body and latest editor together.
|
|
388
|
+
needsProvenance.push(candidate);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
for (let offset = 0; offset < needsProvenance.length; offset += 100) {
|
|
392
|
+
const batch = needsProvenance.slice(offset, offset + 100);
|
|
393
|
+
const response = await this.request<{
|
|
394
|
+
data?: { nodes?: Array<Record<string, unknown> | null> };
|
|
395
|
+
errors?: unknown[];
|
|
396
|
+
}>("/graphql", {
|
|
397
|
+
method: "POST",
|
|
398
|
+
signal,
|
|
399
|
+
body: JSON.stringify({
|
|
400
|
+
query: `query LeitwerkFeedbackProvenance($ids: [ID!]!) {
|
|
401
|
+
nodes(ids: $ids) {
|
|
402
|
+
id __typename
|
|
403
|
+
... on IssueComment { body createdAt lastEditedAt author { login } editor { login } }
|
|
404
|
+
... on PullRequestReview { body createdAt submittedAt lastEditedAt author { login } editor { login } }
|
|
405
|
+
... on PullRequestReviewComment { body createdAt lastEditedAt author { login } editor { login } path line }
|
|
406
|
+
}
|
|
407
|
+
}`,
|
|
408
|
+
variables: { ids: batch.map(({ item }) => item.node_id) },
|
|
409
|
+
}),
|
|
410
|
+
});
|
|
411
|
+
if (response.errors?.length || !Array.isArray(response.data?.nodes)) {
|
|
412
|
+
throw new Error("GitHub feedback provenance lookup failed");
|
|
413
|
+
}
|
|
414
|
+
const snapshots = new Map(
|
|
415
|
+
response.data.nodes.filter((node) => node !== null).map((node) => [node.id, node]),
|
|
416
|
+
);
|
|
417
|
+
for (const { kind, item } of batch) {
|
|
418
|
+
const snapshot = snapshots.get(item.node_id);
|
|
419
|
+
const expectedType = {
|
|
420
|
+
conversation: "IssueComment",
|
|
421
|
+
review: "PullRequestReview",
|
|
422
|
+
inline: "PullRequestReviewComment",
|
|
423
|
+
}[kind];
|
|
424
|
+
if (!snapshot || snapshot.__typename !== expectedType) continue;
|
|
425
|
+
const publishedAt = kind === "review" ? snapshot.submittedAt : snapshot.createdAt;
|
|
426
|
+
if (typeof publishedAt !== "string" || !Number.isFinite(Date.parse(publishedAt))) continue;
|
|
427
|
+
const editedAt = snapshot.lastEditedAt;
|
|
428
|
+
if (editedAt !== null) {
|
|
429
|
+
if (typeof editedAt !== "string" || !Number.isFinite(Date.parse(editedAt))) continue;
|
|
430
|
+
const editor = asUnknownRecord(snapshot.editor)?.login;
|
|
431
|
+
if (typeof editor !== "string" || !(await this.isOrganizationMember(editor))) continue;
|
|
432
|
+
}
|
|
433
|
+
const normalized = normalize(kind, {
|
|
434
|
+
...snapshot,
|
|
435
|
+
id: item.id,
|
|
436
|
+
user: snapshot.author,
|
|
437
|
+
created_at: editedAt ?? publishedAt,
|
|
438
|
+
});
|
|
439
|
+
if (normalized) feedback.push(normalized);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
return feedback;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/** @public */
|
|
446
|
+
async getCheckSummary(owner: string, repo: string, headSha: string): Promise<GitHubCheckSummary> {
|
|
447
|
+
const runs: Array<Record<string, unknown>> = [];
|
|
448
|
+
for (let page = 1; ; page++) {
|
|
449
|
+
const response = await this.request<{ check_runs: Array<Record<string, unknown>> }>(
|
|
450
|
+
`${this.repositoryPath(owner, repo)}/commits/${headSha}/check-runs?per_page=100&page=${page}`,
|
|
451
|
+
);
|
|
452
|
+
runs.push(...response.check_runs);
|
|
453
|
+
if (response.check_runs.length < 100) break;
|
|
454
|
+
}
|
|
455
|
+
const terminalSuccess = new Set(["success", "neutral", "skipped"]);
|
|
456
|
+
const failed = runs
|
|
457
|
+
.filter((run) => run.status === "completed" && !terminalSuccess.has(String(run.conclusion)))
|
|
458
|
+
.map((run) => ({
|
|
459
|
+
name: String(run.name ?? "unknown"),
|
|
460
|
+
conclusion: typeof run.conclusion === "string" ? run.conclusion : null,
|
|
461
|
+
url: typeof run.html_url === "string" ? run.html_url : null,
|
|
462
|
+
}));
|
|
463
|
+
const pending = runs.some((run) => run.status !== "completed");
|
|
464
|
+
return {
|
|
465
|
+
headSha,
|
|
466
|
+
status: failed.length ? "failure" : pending || !runs.length ? "pending" : "success",
|
|
467
|
+
total: runs.length,
|
|
468
|
+
failed,
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/** Human feedback checked against current membership; delivery receipts use raw reads. @internal */
|
|
473
|
+
async listActionablePullRequestFeedback(
|
|
474
|
+
owner: string,
|
|
475
|
+
repo: string,
|
|
476
|
+
number: number,
|
|
477
|
+
signal?: AbortSignal,
|
|
478
|
+
) {
|
|
479
|
+
return actionableFeedback(
|
|
480
|
+
this,
|
|
481
|
+
await this.listPullRequestFeedback(owner, repo, number, signal),
|
|
482
|
+
);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/** @internal */
|
|
486
|
+
listFeedbackReplies(owner: string, repo: string, pr: number, kind: string) {
|
|
487
|
+
return kind === "inline"
|
|
488
|
+
? this.pages<Record<string, unknown>>(
|
|
489
|
+
`${this.repositoryPath(owner, repo)}/pulls/${pr}/comments`,
|
|
490
|
+
)
|
|
491
|
+
: this.listIssueComments(owner, repo, pr);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
/** @internal */
|
|
495
|
+
listFeedbackReactions(owner: string, repo: string, kind: string, id: number) {
|
|
496
|
+
if (kind !== "inline" && kind !== "conversation")
|
|
497
|
+
throw new Error("Reviews do not support reactions");
|
|
498
|
+
const segment = kind === "inline" ? "pulls/comments" : "issues/comments";
|
|
499
|
+
return this.pages<{
|
|
500
|
+
/** @internal */
|
|
501
|
+
id: number;
|
|
502
|
+
/** @internal */
|
|
503
|
+
content: string;
|
|
504
|
+
/** @internal */
|
|
505
|
+
user: {
|
|
506
|
+
/** @internal */
|
|
507
|
+
login: string;
|
|
508
|
+
};
|
|
509
|
+
}>(`${this.repositoryPath(owner, repo)}/${segment}/${id}/reactions`);
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/** @internal */
|
|
513
|
+
listReleases(owner: string, repo: string) {
|
|
514
|
+
return this.pages<GitHubRelease>(`${this.repositoryPath(owner, repo)}/releases`);
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/** @public */
|
|
518
|
+
downloadReleaseAsset(asset: GitHubRelease["assets"][number]): Promise<string> {
|
|
519
|
+
const url = new URL(asset.url);
|
|
520
|
+
if (`${url.protocol}//${url.host}` !== this.profile.apiBaseUrl)
|
|
521
|
+
throw new Error("GitHub release asset URL does not match the configured API origin");
|
|
522
|
+
return this.response(`${url.pathname}${url.search}`, {
|
|
523
|
+
headers: { Accept: "application/octet-stream" },
|
|
524
|
+
}).then((response) => response.text());
|
|
525
|
+
}
|
|
526
|
+
}
|
package/src/external.ts
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineExternalActionSource,
|
|
3
|
+
type ExternalEventDescription,
|
|
4
|
+
} from "@leitwerk-dev/process-sdk";
|
|
5
|
+
import { describeConflict } from "@leitwerk-dev/repository-rebase";
|
|
6
|
+
|
|
7
|
+
/** @internal */
|
|
8
|
+
export const GITHUB_PR_STATE_KIND = "@leitwerk-private/github.pr_state";
|
|
9
|
+
|
|
10
|
+
/** @public */
|
|
11
|
+
export interface GitHubPullRequestSourceConfig {
|
|
12
|
+
/** @public */
|
|
13
|
+
profile: string;
|
|
14
|
+
/** @public */
|
|
15
|
+
owner: string;
|
|
16
|
+
/** @public */
|
|
17
|
+
repo: string;
|
|
18
|
+
/** @public */
|
|
19
|
+
prNumber: number;
|
|
20
|
+
/** @public */
|
|
21
|
+
headSha: string;
|
|
22
|
+
/** @public */
|
|
23
|
+
feedbackCursor: number;
|
|
24
|
+
/** @public */
|
|
25
|
+
lastConflictKey?: string | null;
|
|
26
|
+
/** @public */
|
|
27
|
+
pollInterval?: string;
|
|
28
|
+
/** @internal */
|
|
29
|
+
disabled?: boolean;
|
|
30
|
+
/** @public */
|
|
31
|
+
eventKinds?: Array<"checks" | "feedback" | "merged" | "closed" | "merge_conflict">;
|
|
32
|
+
/** @internal */
|
|
33
|
+
checkStatuses?: Array<"pending" | "success" | "failure">;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** @internal */
|
|
37
|
+
export const GITHUB_RELEASE_KIND = "@leitwerk-private/github.release";
|
|
38
|
+
|
|
39
|
+
/** @internal */
|
|
40
|
+
export interface GitHubReleaseSourceConfig {
|
|
41
|
+
/** @internal */
|
|
42
|
+
profile: string;
|
|
43
|
+
/** @internal */
|
|
44
|
+
owner: string;
|
|
45
|
+
/** @internal */
|
|
46
|
+
repo: string;
|
|
47
|
+
/** @internal */
|
|
48
|
+
mergeSha: string;
|
|
49
|
+
/** @internal */
|
|
50
|
+
pollInterval?: string;
|
|
51
|
+
/** @internal */
|
|
52
|
+
disabled?: boolean;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** @internal */
|
|
56
|
+
export function describeGitHubEvent(event: unknown): ExternalEventDescription {
|
|
57
|
+
const value = event as {
|
|
58
|
+
kind?: string;
|
|
59
|
+
checks?: {
|
|
60
|
+
headSha: string;
|
|
61
|
+
status: string;
|
|
62
|
+
failed: Array<{ name: string; url: string | null }>;
|
|
63
|
+
};
|
|
64
|
+
pullRequest?: { number: number; merged?: boolean; state?: string };
|
|
65
|
+
};
|
|
66
|
+
if (value.kind === "merge_conflict") return describeConflict(event);
|
|
67
|
+
if (value.checks) {
|
|
68
|
+
const checks = value.checks;
|
|
69
|
+
return {
|
|
70
|
+
summary: `Checks ${checks.status} on ${checks.headSha}${checks.failed.length ? `: ${checks.failed.map((check) => check.name).join(", ")}` : ""}`,
|
|
71
|
+
links: checks.failed.flatMap((check, index) =>
|
|
72
|
+
check.url
|
|
73
|
+
? [{ id: `check-${index}`, label: check.name, url: check.url, kind: "pipeline" as const }]
|
|
74
|
+
: [],
|
|
75
|
+
),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
summary:
|
|
80
|
+
value.kind === "merged" || value.kind === "closed" || value.pullRequest
|
|
81
|
+
? `PR #${value.pullRequest?.number} ${value.kind ?? (value.pullRequest?.merged ? "merged" : "closed")}`
|
|
82
|
+
: value.kind === "feedback"
|
|
83
|
+
? "Pull request feedback received"
|
|
84
|
+
: "GitHub event received",
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** @public */
|
|
89
|
+
export const GITHUB_PR_TERMINAL_KIND = "@leitwerk-public/github.pr_terminal";
|
|
90
|
+
/** @public */
|
|
91
|
+
export const GITHUB_PR_FEEDBACK_KIND = "@leitwerk-public/github.pr_feedback";
|
|
92
|
+
/** @public */
|
|
93
|
+
export const GITHUB_ISSUE_CANCELLED_KIND = "@leitwerk-public/github.issue_cancelled";
|
|
94
|
+
|
|
95
|
+
/** @public */
|
|
96
|
+
export interface GitHubPullRequestTerminalSourceConfig {
|
|
97
|
+
/** @public */
|
|
98
|
+
profile: string;
|
|
99
|
+
/** @public */
|
|
100
|
+
owner: string;
|
|
101
|
+
/** @public */
|
|
102
|
+
repo: string;
|
|
103
|
+
/** @public */
|
|
104
|
+
prNumber: number;
|
|
105
|
+
/** @public */
|
|
106
|
+
pollInterval?: string;
|
|
107
|
+
/** @public */
|
|
108
|
+
terminalOutcome?: "merged" | "closed";
|
|
109
|
+
/** @internal */
|
|
110
|
+
disabled?: boolean;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** @public */
|
|
114
|
+
export interface GitHubFeedbackSourceConfig extends GitHubPullRequestTerminalSourceConfig {
|
|
115
|
+
/** @public */
|
|
116
|
+
conversationCursor: number;
|
|
117
|
+
/** @public */
|
|
118
|
+
reviewCursor: number;
|
|
119
|
+
/** @public */
|
|
120
|
+
inlineCursor: number;
|
|
121
|
+
/** @public */
|
|
122
|
+
quietPeriodMs: number;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** @public */
|
|
126
|
+
export interface GitHubIssueCancelledSourceConfig {
|
|
127
|
+
/** @public */
|
|
128
|
+
profile: string;
|
|
129
|
+
/** @public */
|
|
130
|
+
owner: string;
|
|
131
|
+
/** @public */
|
|
132
|
+
repo: string;
|
|
133
|
+
/** @public */
|
|
134
|
+
issueNumber: number;
|
|
135
|
+
/** @public */
|
|
136
|
+
triggerLabel: string;
|
|
137
|
+
/** @public */
|
|
138
|
+
pollInterval?: string;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** @public */
|
|
142
|
+
export const GITHUB_CHECKS_KIND = "@leitwerk-public/github.checks";
|
|
143
|
+
|
|
144
|
+
/** @public */
|
|
145
|
+
export const githubExternal = {
|
|
146
|
+
/** @public */
|
|
147
|
+
checks: defineExternalActionSource<{
|
|
148
|
+
profile: string;
|
|
149
|
+
owner: string;
|
|
150
|
+
repo: string;
|
|
151
|
+
prNumber: number;
|
|
152
|
+
headSha: string;
|
|
153
|
+
}>({
|
|
154
|
+
kind: GITHUB_CHECKS_KIND,
|
|
155
|
+
describeEvent: describeGitHubEvent,
|
|
156
|
+
label: "GitHub Actions failures",
|
|
157
|
+
}),
|
|
158
|
+
/** @public */
|
|
159
|
+
issueCancelled: defineExternalActionSource<GitHubIssueCancelledSourceConfig>({
|
|
160
|
+
kind: GITHUB_ISSUE_CANCELLED_KIND,
|
|
161
|
+
describeEvent: () => ({ summary: "Source issue cancelled" }),
|
|
162
|
+
label: "GitHub source issue cancelled",
|
|
163
|
+
description: "Fires when the source issue closes or loses its trigger label",
|
|
164
|
+
}),
|
|
165
|
+
/** @public */
|
|
166
|
+
pullRequestTerminal: defineExternalActionSource<GitHubPullRequestTerminalSourceConfig>({
|
|
167
|
+
kind: GITHUB_PR_TERMINAL_KIND,
|
|
168
|
+
describeEvent: describeGitHubEvent,
|
|
169
|
+
label: "GitHub pull request merged or closed",
|
|
170
|
+
description: "Fires when the tracked pull request reaches a terminal state",
|
|
171
|
+
}),
|
|
172
|
+
/** @public */
|
|
173
|
+
pullRequestFeedback: defineExternalActionSource<GitHubFeedbackSourceConfig>({
|
|
174
|
+
kind: GITHUB_PR_FEEDBACK_KIND,
|
|
175
|
+
describeEvent: () => ({ summary: "Pull request feedback received" }),
|
|
176
|
+
label: "GitHub pull request feedback",
|
|
177
|
+
description: "Fires after unseen human feedback has been quiet long enough to batch",
|
|
178
|
+
}),
|
|
179
|
+
/** @internal */
|
|
180
|
+
releaseContaining: defineExternalActionSource<GitHubReleaseSourceConfig>({
|
|
181
|
+
kind: GITHUB_RELEASE_KIND,
|
|
182
|
+
label: "Published GitHub release",
|
|
183
|
+
description: "Fires when a published release contains the tracked merge commit",
|
|
184
|
+
}),
|
|
185
|
+
/** @public */
|
|
186
|
+
pullRequestState: defineExternalActionSource<GitHubPullRequestSourceConfig>({
|
|
187
|
+
kind: GITHUB_PR_STATE_KIND,
|
|
188
|
+
describeEvent: describeGitHubEvent,
|
|
189
|
+
label: "GitHub pull request state",
|
|
190
|
+
description: "Fires for failed or successful checks, review feedback, and terminal PR state",
|
|
191
|
+
}),
|
|
192
|
+
};
|