@sentry/junior-github 0.268.0 → 0.270.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 +2 -73
- package/SETUP.md +13 -258
- package/dist/{chunk-PQFFT2UO.js → chunk-MUWR6X24.js} +38 -10
- package/dist/db/schema.d.ts +170 -0
- package/dist/events/pull-request.d.ts +8 -5
- package/dist/events/repository.d.ts +2 -2
- package/dist/index.js +580 -138
- package/dist/pull-request-outcomes/store.d.ts +36 -0
- package/dist/pull-request-requesters/store.d.ts +11 -4
- package/dist/testing.js +1 -1
- package/dist/tools/update-pull-request-feedback.d.ts +4 -3
- package/dist/webhooks/check-suite.d.ts +5 -0
- package/dist/webhooks/handler.d.ts +5 -1
- package/dist/webhooks/installation-requests.d.ts +34 -0
- package/dist/webhooks/merge-conflict.d.ts +46 -0
- package/dist/webhooks/pull-request-ready.d.ts +29 -0
- package/migrations/0010_sleepy_slayback.sql +5 -0
- package/migrations/meta/0010_snapshot.json +427 -0
- package/migrations/meta/_journal.json +8 -1
- package/package.json +2 -2
- package/skills/github-code/SKILL.md +1 -1
- package/skills/github-code/SPEC.md +1 -1
- package/skills/github-code/references/workflow.md +3 -4
package/dist/index.js
CHANGED
|
@@ -29,12 +29,14 @@ import {
|
|
|
29
29
|
USER_WRITE_REQUIREMENTS,
|
|
30
30
|
botLoginFromEmail,
|
|
31
31
|
botUserIdFromEmail,
|
|
32
|
+
checkSuitePullRequests,
|
|
32
33
|
createPermissionCache,
|
|
33
34
|
definedValues,
|
|
34
35
|
gitHubDeploymentSourceSubscribable,
|
|
35
36
|
gitHubIssueSubscribable,
|
|
36
37
|
gitHubPullRequestCreateWatch,
|
|
37
38
|
gitHubPullRequestEventGuidance,
|
|
39
|
+
gitHubPullRequestResource,
|
|
38
40
|
gitHubPullRequestSubscribable,
|
|
39
41
|
gitHubReleaseSourceSubscribable,
|
|
40
42
|
gitHubRepoDataSchema,
|
|
@@ -52,11 +54,12 @@ import {
|
|
|
52
54
|
normalizePermissions,
|
|
53
55
|
normalizeScopeList,
|
|
54
56
|
parseCheckSuitePublishTargets,
|
|
57
|
+
pullRequestTargets,
|
|
55
58
|
readEnv,
|
|
56
59
|
readGrantPermissions,
|
|
57
60
|
requireEnv,
|
|
58
61
|
resolveUserAccount
|
|
59
|
-
} from "./chunk-
|
|
62
|
+
} from "./chunk-MUWR6X24.js";
|
|
60
63
|
|
|
61
64
|
// src/tool-support/attribution.ts
|
|
62
65
|
var GITHUB_REQUEST_ATTRIBUTION_START = "<!-- junior-request-attribution:start -->";
|
|
@@ -1250,7 +1253,23 @@ var juniorGitHubPullRequests = pgTable(
|
|
|
1250
1253
|
openedAt: timestamp("opened_at", { withTimezone: true }).notNull(),
|
|
1251
1254
|
mergedAt: timestamp("merged_at", { withTimezone: true }),
|
|
1252
1255
|
closedAt: timestamp("closed_at", { withTimezone: true }),
|
|
1253
|
-
updatedAt: timestamp("updated_at", { withTimezone: true }).notNull()
|
|
1256
|
+
updatedAt: timestamp("updated_at", { withTimezone: true }).notNull(),
|
|
1257
|
+
/**
|
|
1258
|
+
* When Junior saw the pull request become ready to merge and added its
|
|
1259
|
+
* review desk notice. Null when it is not ready.
|
|
1260
|
+
*/
|
|
1261
|
+
readyToMergeAt: timestamp("ready_to_merge_at", { withTimezone: true }),
|
|
1262
|
+
/**
|
|
1263
|
+
* Webhooks that asked to check whether the pull request is ready to
|
|
1264
|
+
* merge. The check runs again when this changes during a check.
|
|
1265
|
+
*/
|
|
1266
|
+
readySyncRequests: integer("ready_sync_requests").notNull().default(0),
|
|
1267
|
+
/**
|
|
1268
|
+
* The request that runs the current check. Null when no check runs.
|
|
1269
|
+
*/
|
|
1270
|
+
readySyncId: integer("ready_sync_id"),
|
|
1271
|
+
/** When the current check started. */
|
|
1272
|
+
readySyncAt: timestamp("ready_sync_at", { withTimezone: true })
|
|
1254
1273
|
},
|
|
1255
1274
|
(table2) => [
|
|
1256
1275
|
index("junior_github_pull_requests_opened_at_idx").on(table2.openedAt),
|
|
@@ -1287,6 +1306,8 @@ var juniorGitHubPullRequestRequesters = pgTable(
|
|
|
1287
1306
|
number: integer("number").notNull(),
|
|
1288
1307
|
/** Core `junior_identities.id` of the person who acted. */
|
|
1289
1308
|
identityId: text("identity_id").notNull(),
|
|
1309
|
+
/** The Conversation where the person asked Junior to open the pull request. */
|
|
1310
|
+
conversationId: text("conversation_id"),
|
|
1290
1311
|
createdAt: timestamp("created_at", { withTimezone: true }).notNull(),
|
|
1291
1312
|
updatedAt: timestamp("updated_at", { withTimezone: true }).notNull()
|
|
1292
1313
|
},
|
|
@@ -1302,6 +1323,7 @@ var table = juniorGitHubPullRequestRequesters;
|
|
|
1302
1323
|
async function recordGitHubPullRequestOpener(db, input) {
|
|
1303
1324
|
const repositoryFullName = input.repositoryFullName.toLowerCase();
|
|
1304
1325
|
await db.insert(table).values({
|
|
1326
|
+
conversationId: input.conversationId,
|
|
1305
1327
|
createdAt: input.createdAt,
|
|
1306
1328
|
identityId: input.identityId,
|
|
1307
1329
|
number: input.number,
|
|
@@ -1309,7 +1331,7 @@ async function recordGitHubPullRequestOpener(db, input) {
|
|
|
1309
1331
|
updatedAt: /* @__PURE__ */ new Date()
|
|
1310
1332
|
}).onConflictDoUpdate({
|
|
1311
1333
|
target: [table.repositoryFullName, table.number, table.identityId],
|
|
1312
|
-
set: { createdAt: input.createdAt }
|
|
1334
|
+
set: { conversationId: input.conversationId, createdAt: input.createdAt }
|
|
1313
1335
|
});
|
|
1314
1336
|
await db.delete(table).where(
|
|
1315
1337
|
and(
|
|
@@ -1342,7 +1364,12 @@ async function listGitHubPullRequestOpeners(db, input) {
|
|
|
1342
1364
|
)
|
|
1343
1365
|
).orderBy(asc(table.createdAt));
|
|
1344
1366
|
for (const row of rows) {
|
|
1345
|
-
if (!openers.has(row.number))
|
|
1367
|
+
if (!openers.has(row.number)) {
|
|
1368
|
+
openers.set(row.number, {
|
|
1369
|
+
identityId: row.identityId,
|
|
1370
|
+
...row.conversationId ? { conversationId: row.conversationId } : void 0
|
|
1371
|
+
});
|
|
1372
|
+
}
|
|
1346
1373
|
}
|
|
1347
1374
|
return openers;
|
|
1348
1375
|
}
|
|
@@ -1578,10 +1605,12 @@ async function createGitHubPullRequest(ctx, request) {
|
|
|
1578
1605
|
url: pullRequest.html_url
|
|
1579
1606
|
};
|
|
1580
1607
|
}
|
|
1581
|
-
async function recordOpener(ctx, input, state,
|
|
1608
|
+
async function recordOpener(ctx, input, state, requester) {
|
|
1609
|
+
const { conversationId, identityId } = requester;
|
|
1582
1610
|
if (!identityId) return;
|
|
1583
1611
|
const repo = parseRepo4(input.repo);
|
|
1584
1612
|
await recordGitHubPullRequestOpener(ctx.db, {
|
|
1613
|
+
conversationId,
|
|
1585
1614
|
createdAt: new Date(state.createdAtMs),
|
|
1586
1615
|
identityId,
|
|
1587
1616
|
number: state.number,
|
|
@@ -1722,12 +1751,10 @@ function createGitHubPullRequestTool(ctx, subscriptionConfig) {
|
|
|
1722
1751
|
const state = createPullRequestState(await ctx.state.get(key));
|
|
1723
1752
|
if (state?.status === "completed") {
|
|
1724
1753
|
const completedInput = state.input ?? parsedInput;
|
|
1725
|
-
await recordOpener(
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
(await resolveGitHubRequester(ctx))?.identity?.id
|
|
1730
|
-
);
|
|
1754
|
+
await recordOpener(ctx, completedInput, state, {
|
|
1755
|
+
conversationId,
|
|
1756
|
+
identityId: (await resolveGitHubRequester(ctx))?.identity?.id
|
|
1757
|
+
});
|
|
1731
1758
|
const completedResult = {
|
|
1732
1759
|
facts: state.facts === void 0 ? void 0 : objectFactsSchema2.parse(state.facts),
|
|
1733
1760
|
sourceUpdatedAt: state.sourceUpdatedAt,
|
|
@@ -1792,7 +1819,10 @@ function createGitHubPullRequestTool(ctx, subscriptionConfig) {
|
|
|
1792
1819
|
{ cause: error }
|
|
1793
1820
|
);
|
|
1794
1821
|
}
|
|
1795
|
-
await recordOpener(ctx, parsedInput, completedState,
|
|
1822
|
+
await recordOpener(ctx, parsedInput, completedState, {
|
|
1823
|
+
conversationId,
|
|
1824
|
+
identityId
|
|
1825
|
+
});
|
|
1796
1826
|
await assignPullRequestRequester(ctx, parsedInput, result, requester);
|
|
1797
1827
|
return await gitHubPullRequestStructuredResult(
|
|
1798
1828
|
ctx,
|
|
@@ -2619,9 +2649,12 @@ function parseRepo10(value) {
|
|
|
2619
2649
|
}
|
|
2620
2650
|
return { owner: parts[0], name: parts[1], ref: `${parts[0]}/${parts[1]}` };
|
|
2621
2651
|
}
|
|
2622
|
-
function
|
|
2652
|
+
function pullRequestCommentPath(input) {
|
|
2623
2653
|
const segment = input.commentKind === "conversation" ? "issues" : "pulls";
|
|
2624
|
-
return `/repos/${encodeURIComponent(input.owner)}/${encodeURIComponent(input.name)}/${segment}/comments/${input.commentId}
|
|
2654
|
+
return `/repos/${encodeURIComponent(input.owner)}/${encodeURIComponent(input.name)}/${segment}/comments/${input.commentId}`;
|
|
2655
|
+
}
|
|
2656
|
+
function pullRequestCommentReactionsPath(input) {
|
|
2657
|
+
return `${pullRequestCommentPath(input)}/reactions`;
|
|
2625
2658
|
}
|
|
2626
2659
|
function pullRequestFeedbackReactionContent(status) {
|
|
2627
2660
|
return STATUS_TO_REACTION_CONTENT[status];
|
|
@@ -2647,15 +2680,67 @@ var reactionSchema = z12.object({
|
|
|
2647
2680
|
content: z12.string(),
|
|
2648
2681
|
user: z12.object({ id: z12.number() }).nullable()
|
|
2649
2682
|
});
|
|
2683
|
+
async function assertBotAuthoredPullRequest(egress, input) {
|
|
2684
|
+
const headers = {
|
|
2685
|
+
Accept: "application/vnd.github+json",
|
|
2686
|
+
"X-GitHub-Api-Version": "2022-11-28"
|
|
2687
|
+
};
|
|
2688
|
+
const commentResponse = await egress.fetch({
|
|
2689
|
+
provider: "github",
|
|
2690
|
+
operation: "github.pull.comment.get",
|
|
2691
|
+
request: new Request(
|
|
2692
|
+
`https://api.github.com${pullRequestCommentPath(input)}`,
|
|
2693
|
+
{ headers }
|
|
2694
|
+
)
|
|
2695
|
+
});
|
|
2696
|
+
const commentPayload = await readJson7(commentResponse);
|
|
2697
|
+
if (!commentResponse.ok) {
|
|
2698
|
+
const message = `GitHub comment lookup failed with HTTP ${commentResponse.status}: ${githubError(commentPayload)}`;
|
|
2699
|
+
if (commentResponse.status === 404) throw new PluginToolInputError11(message);
|
|
2700
|
+
throw new Error(message);
|
|
2701
|
+
}
|
|
2702
|
+
const comment = z12.object({
|
|
2703
|
+
issue_url: z12.string().optional(),
|
|
2704
|
+
pull_request_url: z12.string().optional()
|
|
2705
|
+
}).safeParse(commentPayload);
|
|
2706
|
+
const parentUrl = comment.success ? input.commentKind === "conversation" ? comment.data.issue_url : comment.data.pull_request_url : void 0;
|
|
2707
|
+
const number = /\/(?:issues|pulls)\/(\d+)$/.exec(parentUrl ?? "")?.[1];
|
|
2708
|
+
if (!number) {
|
|
2709
|
+
throw new Error("GitHub comment did not name its issue or pull request.");
|
|
2710
|
+
}
|
|
2711
|
+
const pullResponse = await egress.fetch({
|
|
2712
|
+
provider: "github",
|
|
2713
|
+
operation: "github.pull.get",
|
|
2714
|
+
request: new Request(
|
|
2715
|
+
`https://api.github.com/repos/${encodeURIComponent(input.owner)}/${encodeURIComponent(input.name)}/pulls/${number}`,
|
|
2716
|
+
{ headers }
|
|
2717
|
+
)
|
|
2718
|
+
});
|
|
2719
|
+
const pullPayload = await readJson7(pullResponse);
|
|
2720
|
+
if (!pullResponse.ok && pullResponse.status !== 404) {
|
|
2721
|
+
throw new Error(
|
|
2722
|
+
`GitHub pull request lookup failed with HTTP ${pullResponse.status}: ${githubError(pullPayload)}`
|
|
2723
|
+
);
|
|
2724
|
+
}
|
|
2725
|
+
const pull = z12.object({ user: z12.object({ id: z12.number() }).nullable() }).safeParse(pullPayload);
|
|
2726
|
+
if (!pullResponse.ok || !pull.success || pull.data.user?.id !== input.botUserId) {
|
|
2727
|
+
throw new PluginToolInputError11(
|
|
2728
|
+
"This bot can only set feedback status on comments on pull requests it authored."
|
|
2729
|
+
);
|
|
2730
|
+
}
|
|
2731
|
+
}
|
|
2650
2732
|
function createGitHubUpdatePullRequestFeedbackTool(ctx, botEmail) {
|
|
2651
2733
|
return definePluginTool10({
|
|
2734
|
+
// No action review: the tool checks in code that the bot authored the pull
|
|
2735
|
+
// request, and it changes only the bot's own reaction, which is reversible.
|
|
2736
|
+
approvalMode: "approve",
|
|
2652
2737
|
annotations: {
|
|
2653
2738
|
destructiveHint: true,
|
|
2654
2739
|
idempotentHint: true,
|
|
2655
2740
|
openWorldHint: true,
|
|
2656
2741
|
readOnlyHint: false
|
|
2657
2742
|
},
|
|
2658
|
-
description: "Set your outcome reaction on GitHub pull request feedback: addressed (+1) or declined (-1). Use the commentId and commentKind from the event after acting on the feedback. Replaces only your prior status reaction on that comment.",
|
|
2743
|
+
description: "Set your outcome reaction on GitHub pull request feedback: addressed (+1) or declined (-1). Use the commentId and commentKind from the event after acting on the feedback. Replaces only your prior status reaction on that comment. Only works on pull requests the bot authored.",
|
|
2659
2744
|
exposure: "direct",
|
|
2660
2745
|
inputSchema: inputSchema8,
|
|
2661
2746
|
outputSchema: outputSchema8,
|
|
@@ -2673,6 +2758,13 @@ function createGitHubUpdatePullRequestFeedbackTool(ctx, botEmail) {
|
|
|
2673
2758
|
if (botUserId === void 0) {
|
|
2674
2759
|
throw new Error("GitHub App bot identity is not configured.");
|
|
2675
2760
|
}
|
|
2761
|
+
await assertBotAuthoredPullRequest(ctx.egress, {
|
|
2762
|
+
owner: repo.owner,
|
|
2763
|
+
name: repo.name,
|
|
2764
|
+
commentKind,
|
|
2765
|
+
commentId,
|
|
2766
|
+
botUserId
|
|
2767
|
+
});
|
|
2676
2768
|
const targetContent = pullRequestFeedbackReactionContent(status);
|
|
2677
2769
|
const baseUrl = `https://api.github.com${pullRequestCommentReactionsPath({
|
|
2678
2770
|
owner: repo.owner,
|
|
@@ -2801,6 +2893,10 @@ function githubError2(payload) {
|
|
|
2801
2893
|
}
|
|
2802
2894
|
function createGitHubResolvePullRequestReviewThreadTool(ctx, botEmail) {
|
|
2803
2895
|
return definePluginTool11({
|
|
2896
|
+
// No action review: the tool checks in code that the bot authored the pull
|
|
2897
|
+
// request, and it changes only the thread's resolved state, which is
|
|
2898
|
+
// reversible.
|
|
2899
|
+
approvalMode: "approve",
|
|
2804
2900
|
annotations: {
|
|
2805
2901
|
destructiveHint: true,
|
|
2806
2902
|
idempotentHint: true,
|
|
@@ -3097,7 +3193,7 @@ function createGitHubTools(ctx, botEmail, pullRequestSubscription) {
|
|
|
3097
3193
|
|
|
3098
3194
|
// src/webhooks/handler.ts
|
|
3099
3195
|
import { createHmac, timingSafeEqual } from "crypto";
|
|
3100
|
-
import { z as
|
|
3196
|
+
import { z as z24 } from "zod";
|
|
3101
3197
|
|
|
3102
3198
|
// src/issue-outcomes/store.ts
|
|
3103
3199
|
import { and as and2, eq as eq2, lte, sql as sql2 } from "drizzle-orm";
|
|
@@ -3369,6 +3465,44 @@ async function recordGitHubPullRequestLinkedIssues(db, input) {
|
|
|
3369
3465
|
);
|
|
3370
3466
|
return inserted.some((rows) => rows.length > 0);
|
|
3371
3467
|
}
|
|
3468
|
+
function pullRequestRow(target) {
|
|
3469
|
+
return and3(
|
|
3470
|
+
sql3`lower(${juniorGitHubPullRequests.repositoryFullName}) = ${target.repositoryFullName.toLowerCase()}`,
|
|
3471
|
+
eq3(juniorGitHubPullRequests.number, target.number)
|
|
3472
|
+
);
|
|
3473
|
+
}
|
|
3474
|
+
async function claimGitHubPullRequestReadySync(db, target, options) {
|
|
3475
|
+
const t = juniorGitHubPullRequests;
|
|
3476
|
+
const staleBefore = new Date(options.now.getTime() - options.staleMs);
|
|
3477
|
+
const free = sql3`(${t.readySyncId} is null or ${t.readySyncAt} < ${staleBefore.toISOString()}::timestamptz)`;
|
|
3478
|
+
const [row] = await db.update(t).set({
|
|
3479
|
+
readySyncRequests: sql3`${t.readySyncRequests} + 1`,
|
|
3480
|
+
// Each request has its own number, so it is the id of the claim.
|
|
3481
|
+
readySyncId: sql3`case when ${free} then ${t.readySyncRequests} + 1 else ${t.readySyncId} end`,
|
|
3482
|
+
readySyncAt: sql3`case when ${free} then ${options.now.toISOString()}::timestamptz else ${t.readySyncAt} end`
|
|
3483
|
+
}).where(pullRequestRow(target)).returning({
|
|
3484
|
+
id: t.readySyncId,
|
|
3485
|
+
readyToMergeAt: t.readyToMergeAt,
|
|
3486
|
+
requests: t.readySyncRequests
|
|
3487
|
+
});
|
|
3488
|
+
if (!row || row.id !== row.requests) return void 0;
|
|
3489
|
+
return {
|
|
3490
|
+
id: row.id,
|
|
3491
|
+
ready: row.readyToMergeAt !== null,
|
|
3492
|
+
requests: row.requests
|
|
3493
|
+
};
|
|
3494
|
+
}
|
|
3495
|
+
async function setGitHubPullRequestReadyToMerge(db, target) {
|
|
3496
|
+
await db.update(juniorGitHubPullRequests).set({ readyToMergeAt: target.ready ? /* @__PURE__ */ new Date() : null }).where(pullRequestRow(target));
|
|
3497
|
+
}
|
|
3498
|
+
async function releaseGitHubPullRequestReadySync(db, target, claim) {
|
|
3499
|
+
const t = juniorGitHubPullRequests;
|
|
3500
|
+
const claimed = and3(pullRequestRow(target), eq3(t.readySyncId, claim.id));
|
|
3501
|
+
const released = await db.update(t).set({ readySyncAt: null, readySyncId: null }).where(and3(claimed, eq3(t.readySyncRequests, claim.requests))).returning({ number: t.number });
|
|
3502
|
+
if (released.length > 0) return void 0;
|
|
3503
|
+
const [row] = await db.select({ requests: t.readySyncRequests }).from(t).where(claimed);
|
|
3504
|
+
return row?.requests;
|
|
3505
|
+
}
|
|
3372
3506
|
|
|
3373
3507
|
// src/webhooks/issue-outcome.ts
|
|
3374
3508
|
import { z as z17 } from "zod";
|
|
@@ -3671,8 +3805,369 @@ function normalizeGitHubPullRequestLinkedIssues(args) {
|
|
|
3671
3805
|
} : void 0;
|
|
3672
3806
|
}
|
|
3673
3807
|
|
|
3674
|
-
// src/webhooks/
|
|
3808
|
+
// src/webhooks/merge-conflict.ts
|
|
3675
3809
|
import { z as z19 } from "zod";
|
|
3810
|
+
var EVENT_TYPE = "pull_request.merge_conflict";
|
|
3811
|
+
var CLOSED_EVENT_TYPES = /* @__PURE__ */ new Set([
|
|
3812
|
+
"pull_request.closed_unmerged",
|
|
3813
|
+
"pull_request.merged"
|
|
3814
|
+
]);
|
|
3815
|
+
var pullRequestSchema3 = z19.object({
|
|
3816
|
+
base: z19.object({ ref: z19.string().min(1) }),
|
|
3817
|
+
number: z19.number().int().positive().optional(),
|
|
3818
|
+
head: z19.object({ sha: z19.string().min(1) }),
|
|
3819
|
+
html_url: z19.string().min(1).optional(),
|
|
3820
|
+
mergeable: z19.boolean().nullable().optional(),
|
|
3821
|
+
mergeable_state: z19.string().optional(),
|
|
3822
|
+
state: z19.string()
|
|
3823
|
+
});
|
|
3824
|
+
function gitHubMergeConflictEvents(input) {
|
|
3825
|
+
const { conflict } = input;
|
|
3826
|
+
const repo = gitHubRepoDataSchema.parse(input.repositoryFullName);
|
|
3827
|
+
const resource = gitHubPullRequestResource({ number: input.number, repo });
|
|
3828
|
+
return pullRequestTargets(
|
|
3829
|
+
{
|
|
3830
|
+
eventKey: `github:merge-conflict:${resource.identifier}${conflict ? `:${conflict.headSha}` : ""}`,
|
|
3831
|
+
eventType: EVENT_TYPE,
|
|
3832
|
+
identifier: resource.identifier,
|
|
3833
|
+
objectType: "code_change",
|
|
3834
|
+
occurredAtMs: Date.now(),
|
|
3835
|
+
trustedSummary: `${resource.label} has merge conflicts${conflict ? ` with ${conflict.baseBranch}` : ""}.`,
|
|
3836
|
+
data: { repo, pullRequest: input.number, ...conflict }
|
|
3837
|
+
},
|
|
3838
|
+
repo
|
|
3839
|
+
);
|
|
3840
|
+
}
|
|
3841
|
+
function pullRequestRefs(events) {
|
|
3842
|
+
const refs = /* @__PURE__ */ new Map();
|
|
3843
|
+
for (const event of events) {
|
|
3844
|
+
const { pullRequest, repo } = event.data ?? {};
|
|
3845
|
+
if (!event.eventType.startsWith("pull_request.") || CLOSED_EVENT_TYPES.has(event.eventType) || typeof pullRequest !== "number" || typeof repo !== "string") {
|
|
3846
|
+
continue;
|
|
3847
|
+
}
|
|
3848
|
+
refs.set(`${repo}#${pullRequest}`, {
|
|
3849
|
+
number: pullRequest,
|
|
3850
|
+
repositoryFullName: repo
|
|
3851
|
+
});
|
|
3852
|
+
}
|
|
3853
|
+
return [...refs.values()];
|
|
3854
|
+
}
|
|
3855
|
+
async function hasMatch(hasMatchEvent, ref) {
|
|
3856
|
+
for (const event of gitHubMergeConflictEvents(ref)) {
|
|
3857
|
+
if (await hasMatchEvent(event)) return true;
|
|
3858
|
+
}
|
|
3859
|
+
return false;
|
|
3860
|
+
}
|
|
3861
|
+
function deliveredPullRequest(payload, ref) {
|
|
3862
|
+
const parsed = z19.object({ pull_request: pullRequestSchema3 }).safeParse(payload);
|
|
3863
|
+
const pullRequest = parsed.success ? parsed.data.pull_request : void 0;
|
|
3864
|
+
return pullRequest?.number === ref.number && typeof pullRequest.mergeable === "boolean" ? pullRequest : void 0;
|
|
3865
|
+
}
|
|
3866
|
+
async function loadGitHubMergeConflictEvents(args) {
|
|
3867
|
+
const { hasMatch: hasMatchEvent, loadPullRequest: loadPullRequest2 } = args;
|
|
3868
|
+
if (!hasMatchEvent || !loadPullRequest2) return [];
|
|
3869
|
+
const conflicts = [];
|
|
3870
|
+
for (const ref of pullRequestRefs(args.events)) {
|
|
3871
|
+
if (!await hasMatch(hasMatchEvent, ref)) continue;
|
|
3872
|
+
try {
|
|
3873
|
+
const loaded = deliveredPullRequest(args.pullRequestPayload, ref) ?? pullRequestSchema3.parse(await loadPullRequest2(ref));
|
|
3874
|
+
if (loaded.state !== "open" || loaded.mergeable !== false || loaded.mergeable_state !== "dirty") {
|
|
3875
|
+
continue;
|
|
3876
|
+
}
|
|
3877
|
+
conflicts.push(
|
|
3878
|
+
...gitHubMergeConflictEvents({
|
|
3879
|
+
...ref,
|
|
3880
|
+
conflict: {
|
|
3881
|
+
baseBranch: loaded.base.ref,
|
|
3882
|
+
headSha: loaded.head.sha,
|
|
3883
|
+
...loaded.html_url ? { url: loaded.html_url } : void 0
|
|
3884
|
+
}
|
|
3885
|
+
})
|
|
3886
|
+
);
|
|
3887
|
+
} catch (error) {
|
|
3888
|
+
args.log?.error("GitHub pull request merge conflict check failed", {
|
|
3889
|
+
deliveryId: args.deliveryId,
|
|
3890
|
+
errorType: error instanceof Error ? error.name : "UnknownError",
|
|
3891
|
+
pullRequest: ref.number,
|
|
3892
|
+
repository: ref.repositoryFullName
|
|
3893
|
+
});
|
|
3894
|
+
}
|
|
3895
|
+
}
|
|
3896
|
+
return conflicts;
|
|
3897
|
+
}
|
|
3898
|
+
|
|
3899
|
+
// src/webhooks/pull-request-ready.ts
|
|
3900
|
+
import { z as z21 } from "zod";
|
|
3901
|
+
|
|
3902
|
+
// src/webhooks/installation-requests.ts
|
|
3903
|
+
import { z as z20 } from "zod";
|
|
3904
|
+
function splitRepository(fullName, label) {
|
|
3905
|
+
const [owner, name, ...extra] = fullName.split("/");
|
|
3906
|
+
if (!owner || !name || extra.length > 0) {
|
|
3907
|
+
throw new Error(`GitHub ${label} received an invalid repository name`);
|
|
3908
|
+
}
|
|
3909
|
+
return { owner, name };
|
|
3910
|
+
}
|
|
3911
|
+
async function loadPullRequestCommitPages(env, pullRequest) {
|
|
3912
|
+
const { owner, name } = splitRepository(
|
|
3913
|
+
pullRequest.repositoryFullName,
|
|
3914
|
+
"pull request commits"
|
|
3915
|
+
);
|
|
3916
|
+
const token = await issueInstallationToken({
|
|
3917
|
+
...env,
|
|
3918
|
+
permissions: { pull_requests: "read" },
|
|
3919
|
+
repositories: [name]
|
|
3920
|
+
});
|
|
3921
|
+
return async (page, perPage) => await githubRequest(
|
|
3922
|
+
"https://api.github.com",
|
|
3923
|
+
`/repos/${encodeURIComponent(owner)}/${encodeURIComponent(name)}/pulls/${pullRequest.number}/commits?per_page=${perPage}&page=${page}`,
|
|
3924
|
+
{ token: token.token }
|
|
3925
|
+
);
|
|
3926
|
+
}
|
|
3927
|
+
async function loadPullRequest(env, pullRequest) {
|
|
3928
|
+
const { owner, name } = splitRepository(
|
|
3929
|
+
pullRequest.repositoryFullName,
|
|
3930
|
+
"pull request"
|
|
3931
|
+
);
|
|
3932
|
+
const token = await issueInstallationToken({
|
|
3933
|
+
...env,
|
|
3934
|
+
permissions: { pull_requests: "read" },
|
|
3935
|
+
repositories: [name]
|
|
3936
|
+
});
|
|
3937
|
+
return await githubRequest(
|
|
3938
|
+
"https://api.github.com",
|
|
3939
|
+
`/repos/${encodeURIComponent(owner)}/${encodeURIComponent(name)}/pulls/${pullRequest.number}`,
|
|
3940
|
+
{ token: token.token }
|
|
3941
|
+
);
|
|
3942
|
+
}
|
|
3943
|
+
async function markPullRequestFeedbackReviewing(env, target) {
|
|
3944
|
+
const { owner, name } = splitRepository(target.repo, "pull request feedback");
|
|
3945
|
+
const token = await issueInstallationToken({
|
|
3946
|
+
...env,
|
|
3947
|
+
repositories: [name]
|
|
3948
|
+
});
|
|
3949
|
+
await githubRequest(
|
|
3950
|
+
"https://api.github.com",
|
|
3951
|
+
pullRequestCommentReactionsPath({
|
|
3952
|
+
owner,
|
|
3953
|
+
name,
|
|
3954
|
+
commentKind: target.commentKind,
|
|
3955
|
+
commentId: target.commentId
|
|
3956
|
+
}),
|
|
3957
|
+
{
|
|
3958
|
+
body: { content: pullRequestFeedbackReactionContent("reviewing") },
|
|
3959
|
+
method: "POST",
|
|
3960
|
+
token: token.token
|
|
3961
|
+
}
|
|
3962
|
+
);
|
|
3963
|
+
}
|
|
3964
|
+
var repositoryPermissionSchema = z20.object({
|
|
3965
|
+
permission: z20.string().min(1),
|
|
3966
|
+
role_name: z20.string().optional()
|
|
3967
|
+
});
|
|
3968
|
+
var REPOSITORY_ROLE_TTL_MS = 10 * 60 * 1e3;
|
|
3969
|
+
async function loadRepositoryRole(env, cache, target) {
|
|
3970
|
+
const { owner, name } = splitRepository(
|
|
3971
|
+
target.repositoryFullName,
|
|
3972
|
+
"repository role"
|
|
3973
|
+
);
|
|
3974
|
+
return await cache.getOrLoad(
|
|
3975
|
+
`repository-role:${owner}/${name}:${target.login}`.toLowerCase(),
|
|
3976
|
+
async () => {
|
|
3977
|
+
const token = await issueInstallationToken({
|
|
3978
|
+
...env,
|
|
3979
|
+
permissions: { metadata: "read" },
|
|
3980
|
+
repositories: [name]
|
|
3981
|
+
});
|
|
3982
|
+
const response = repositoryPermissionSchema.parse(
|
|
3983
|
+
await githubRequest(
|
|
3984
|
+
"https://api.github.com",
|
|
3985
|
+
`/repos/${encodeURIComponent(owner)}/${encodeURIComponent(name)}/collaborators/${encodeURIComponent(target.login)}/permission`,
|
|
3986
|
+
{ token: token.token }
|
|
3987
|
+
)
|
|
3988
|
+
);
|
|
3989
|
+
return {
|
|
3990
|
+
value: response.role_name?.trim() || response.permission,
|
|
3991
|
+
ttlMs: REPOSITORY_ROLE_TTL_MS
|
|
3992
|
+
};
|
|
3993
|
+
}
|
|
3994
|
+
);
|
|
3995
|
+
}
|
|
3996
|
+
var pullRequestSchema4 = z20.object({
|
|
3997
|
+
draft: z20.boolean().optional(),
|
|
3998
|
+
html_url: z20.string().url(),
|
|
3999
|
+
mergeable_state: z20.string().nullable().optional(),
|
|
4000
|
+
merged: z20.boolean(),
|
|
4001
|
+
state: z20.enum(["open", "closed"]),
|
|
4002
|
+
title: z20.string()
|
|
4003
|
+
});
|
|
4004
|
+
var reviewsSchema = z20.array(
|
|
4005
|
+
z20.object({
|
|
4006
|
+
state: z20.string(),
|
|
4007
|
+
user: z20.object({ login: z20.string() }).nullable()
|
|
4008
|
+
})
|
|
4009
|
+
);
|
|
4010
|
+
async function loadPullRequestMergeState(env, pullRequest) {
|
|
4011
|
+
const { owner, name } = splitRepository(
|
|
4012
|
+
pullRequest.repositoryFullName,
|
|
4013
|
+
"pull request merge state"
|
|
4014
|
+
);
|
|
4015
|
+
const token = await issueInstallationToken({
|
|
4016
|
+
...env,
|
|
4017
|
+
permissions: { pull_requests: "read" },
|
|
4018
|
+
repositories: [name]
|
|
4019
|
+
});
|
|
4020
|
+
const read = async (path) => await githubRequest(
|
|
4021
|
+
"https://api.github.com",
|
|
4022
|
+
`/repos/${encodeURIComponent(owner)}/${encodeURIComponent(name)}/pulls/${pullRequest.number}${path}`,
|
|
4023
|
+
{ token: token.token }
|
|
4024
|
+
);
|
|
4025
|
+
const readPull = async () => {
|
|
4026
|
+
const pull = pullRequestSchema4.parse(await read(""));
|
|
4027
|
+
const mergeableState = pull.mergeable_state && pull.mergeable_state !== "unknown" ? pull.mergeable_state : void 0;
|
|
4028
|
+
return {
|
|
4029
|
+
approved: false,
|
|
4030
|
+
draft: pull.draft === true,
|
|
4031
|
+
...mergeableState ? { mergeableState } : void 0,
|
|
4032
|
+
state: pull.merged ? "merged" : pull.state,
|
|
4033
|
+
title: pull.title,
|
|
4034
|
+
url: pull.html_url
|
|
4035
|
+
};
|
|
4036
|
+
};
|
|
4037
|
+
let state = await readPull();
|
|
4038
|
+
if (state.state !== "open" || state.draft) return state;
|
|
4039
|
+
if (!state.mergeableState) state = await readPull();
|
|
4040
|
+
if (state.mergeableState !== "clean") return state;
|
|
4041
|
+
const reviews = [];
|
|
4042
|
+
for (let page = 1; ; page++) {
|
|
4043
|
+
const batch = reviewsSchema.parse(
|
|
4044
|
+
await read(`/reviews?per_page=100&page=${page}`)
|
|
4045
|
+
);
|
|
4046
|
+
reviews.push(...batch);
|
|
4047
|
+
if (batch.length < 100) break;
|
|
4048
|
+
}
|
|
4049
|
+
const latestReviews = /* @__PURE__ */ new Map();
|
|
4050
|
+
for (const review of reviews) {
|
|
4051
|
+
if (review.user && review.state !== "COMMENTED" && review.state !== "PENDING") {
|
|
4052
|
+
latestReviews.set(review.user.login.toLowerCase(), review.state);
|
|
4053
|
+
}
|
|
4054
|
+
}
|
|
4055
|
+
const reviewStates = [...latestReviews.values()];
|
|
4056
|
+
state.approved = reviewStates.includes("APPROVED") && !reviewStates.includes("CHANGES_REQUESTED");
|
|
4057
|
+
return state;
|
|
4058
|
+
}
|
|
4059
|
+
|
|
4060
|
+
// src/webhooks/pull-request-ready.ts
|
|
4061
|
+
var READY_SYNC_STALE_MS = 6e4;
|
|
4062
|
+
var READY_ACTIONS = {
|
|
4063
|
+
pull_request: /* @__PURE__ */ new Set([
|
|
4064
|
+
"closed",
|
|
4065
|
+
"converted_to_draft",
|
|
4066
|
+
"ready_for_review",
|
|
4067
|
+
"reopened",
|
|
4068
|
+
"synchronize"
|
|
4069
|
+
]),
|
|
4070
|
+
pull_request_review: /* @__PURE__ */ new Set(["dismissed", "submitted"])
|
|
4071
|
+
};
|
|
4072
|
+
var pullRequestWebhookSchema = z21.object({
|
|
4073
|
+
action: z21.string(),
|
|
4074
|
+
pull_request: z21.object({ number: z21.number().int().positive() }),
|
|
4075
|
+
repository: z21.object({ full_name: z21.string().min(1) })
|
|
4076
|
+
});
|
|
4077
|
+
function pullRequestReadyTargets(eventName, body) {
|
|
4078
|
+
if (eventName === "check_suite") return checkSuitePullRequests(body);
|
|
4079
|
+
const actions = READY_ACTIONS[eventName];
|
|
4080
|
+
const parsed = pullRequestWebhookSchema.safeParse(body);
|
|
4081
|
+
if (!actions || !parsed.success || !actions.has(parsed.data.action)) {
|
|
4082
|
+
return [];
|
|
4083
|
+
}
|
|
4084
|
+
return [
|
|
4085
|
+
{
|
|
4086
|
+
number: parsed.data.pull_request.number,
|
|
4087
|
+
repositoryFullName: parsed.data.repository.full_name
|
|
4088
|
+
}
|
|
4089
|
+
];
|
|
4090
|
+
}
|
|
4091
|
+
function closeReason(state) {
|
|
4092
|
+
if (state.state === "merged") {
|
|
4093
|
+
return { reason: "The pull request merged.", status: "done" };
|
|
4094
|
+
}
|
|
4095
|
+
if (state.state === "closed") {
|
|
4096
|
+
return {
|
|
4097
|
+
reason: "The pull request closed without merging.",
|
|
4098
|
+
status: "dismissed"
|
|
4099
|
+
};
|
|
4100
|
+
}
|
|
4101
|
+
return {
|
|
4102
|
+
reason: "The pull request is no longer ready to merge.",
|
|
4103
|
+
status: "dismissed"
|
|
4104
|
+
};
|
|
4105
|
+
}
|
|
4106
|
+
async function syncOne(args, target) {
|
|
4107
|
+
const opener = (await listGitHubPullRequestOpeners(args.db, {
|
|
4108
|
+
numbers: [target.number],
|
|
4109
|
+
repositoryFullName: target.repositoryFullName
|
|
4110
|
+
})).get(target.number);
|
|
4111
|
+
if (!opener?.conversationId) return;
|
|
4112
|
+
const items = args.reviewItems.forConversation(opener.conversationId);
|
|
4113
|
+
const name = `${target.repositoryFullName}#${target.number}`;
|
|
4114
|
+
const dedupeKey = `pull-request-ready:${name.toLowerCase()}`;
|
|
4115
|
+
const claim = await claimGitHubPullRequestReadySync(args.db, target, {
|
|
4116
|
+
now: /* @__PURE__ */ new Date(),
|
|
4117
|
+
staleMs: READY_SYNC_STALE_MS
|
|
4118
|
+
});
|
|
4119
|
+
if (!claim) return;
|
|
4120
|
+
const { id } = claim;
|
|
4121
|
+
let saved = claim.ready;
|
|
4122
|
+
let requests = claim.requests;
|
|
4123
|
+
let failure;
|
|
4124
|
+
while (requests !== void 0) {
|
|
4125
|
+
try {
|
|
4126
|
+
const state = await loadPullRequestMergeState(args.env, target);
|
|
4127
|
+
const open = state.state === "open" && !state.draft;
|
|
4128
|
+
const ready = open && state.mergeableState === "clean" && state.approved;
|
|
4129
|
+
if ((!open || state.mergeableState) && ready !== saved) {
|
|
4130
|
+
if (ready) {
|
|
4131
|
+
await items.upsert({
|
|
4132
|
+
assigneeIdentityIds: [opener.identityId],
|
|
4133
|
+
body: `**${state.title}** is approved, and GitHub says that it can merge. Merge it, or say why not: ${state.url}`,
|
|
4134
|
+
dedupeKey,
|
|
4135
|
+
subject: { id: state.url, title: name, url: state.url },
|
|
4136
|
+
title: `Ready to merge: ${name}`
|
|
4137
|
+
});
|
|
4138
|
+
} else {
|
|
4139
|
+
await items.close({ dedupeKey, ...closeReason(state) });
|
|
4140
|
+
}
|
|
4141
|
+
await setGitHubPullRequestReadyToMerge(args.db, { ...target, ready });
|
|
4142
|
+
saved = ready;
|
|
4143
|
+
}
|
|
4144
|
+
} catch (error) {
|
|
4145
|
+
failure ??= error;
|
|
4146
|
+
}
|
|
4147
|
+
requests = await releaseGitHubPullRequestReadySync(args.db, target, {
|
|
4148
|
+
id,
|
|
4149
|
+
requests
|
|
4150
|
+
});
|
|
4151
|
+
}
|
|
4152
|
+
if (failure !== void 0) throw failure;
|
|
4153
|
+
}
|
|
4154
|
+
async function syncGitHubPullRequestReadyNotices(args) {
|
|
4155
|
+
for (const target of args.targets) {
|
|
4156
|
+
try {
|
|
4157
|
+
await syncOne(args, target);
|
|
4158
|
+
} catch (error) {
|
|
4159
|
+
args.log?.error("GitHub pull request ready notice failed", {
|
|
4160
|
+
deliveryId: args.deliveryId,
|
|
4161
|
+
errorType: error instanceof Error ? error.name : "UnknownError",
|
|
4162
|
+
number: target.number,
|
|
4163
|
+
repository: target.repositoryFullName
|
|
4164
|
+
});
|
|
4165
|
+
}
|
|
4166
|
+
}
|
|
4167
|
+
}
|
|
4168
|
+
|
|
4169
|
+
// src/webhooks/pull-request-match-data.ts
|
|
4170
|
+
import { z as z22 } from "zod";
|
|
3676
4171
|
var LOOKUPS_BY_MATCH_KEY = {
|
|
3677
4172
|
authors: ["authors"],
|
|
3678
4173
|
openerEmail: ["opener"],
|
|
@@ -3680,16 +4175,16 @@ var LOOKUPS_BY_MATCH_KEY = {
|
|
|
3680
4175
|
openerUsername: ["opener"]
|
|
3681
4176
|
};
|
|
3682
4177
|
var ALL_LOOKUPS = ["authors", "opener", "openerRole"];
|
|
3683
|
-
var lookupDataSchema =
|
|
3684
|
-
authors:
|
|
3685
|
-
openerRole:
|
|
3686
|
-
openerUsername:
|
|
4178
|
+
var lookupDataSchema = z22.object({
|
|
4179
|
+
authors: z22.array(z22.string().min(1)).min(1).optional(),
|
|
4180
|
+
openerRole: z22.string().min(1).optional(),
|
|
4181
|
+
openerUsername: z22.string().min(1).optional()
|
|
3687
4182
|
}).strict();
|
|
3688
4183
|
function lookupData(input) {
|
|
3689
4184
|
return lookupDataSchema.parse(definedValues(input));
|
|
3690
4185
|
}
|
|
3691
|
-
var webhookSchema =
|
|
3692
|
-
repository:
|
|
4186
|
+
var webhookSchema = z22.object({
|
|
4187
|
+
repository: z22.object({ full_name: z22.string().min(1) })
|
|
3693
4188
|
});
|
|
3694
4189
|
function pullRequestNumbers(events) {
|
|
3695
4190
|
const numbers = /* @__PURE__ */ new Map();
|
|
@@ -3719,7 +4214,9 @@ async function withJuniorOpeners(context, events) {
|
|
|
3719
4214
|
repositoryFullName: context.repositoryFullName
|
|
3720
4215
|
});
|
|
3721
4216
|
const logins = /* @__PURE__ */ new Map();
|
|
3722
|
-
for (const identityId of new Set(
|
|
4217
|
+
for (const identityId of new Set(
|
|
4218
|
+
[...openers.values()].map((opener) => opener.identityId)
|
|
4219
|
+
)) {
|
|
3723
4220
|
try {
|
|
3724
4221
|
logins.set(identityId, await context.loadGitHubLogin?.(identityId));
|
|
3725
4222
|
} catch (error) {
|
|
@@ -3729,7 +4226,7 @@ async function withJuniorOpeners(context, events) {
|
|
|
3729
4226
|
return events.map((event) => {
|
|
3730
4227
|
const number = botNumber(event);
|
|
3731
4228
|
if (number === void 0) return event;
|
|
3732
|
-
const identityId = openers.get(number);
|
|
4229
|
+
const identityId = openers.get(number)?.identityId;
|
|
3733
4230
|
const login = identityId ? logins.get(identityId) : void 0;
|
|
3734
4231
|
const {
|
|
3735
4232
|
openerEmail: _email,
|
|
@@ -3820,17 +4317,17 @@ async function withGitHubPullRequestMatchData(args) {
|
|
|
3820
4317
|
);
|
|
3821
4318
|
const events = filtered.size > 0 ? await withLookups(context, args.events, filtered) : args.events;
|
|
3822
4319
|
const rest = new Set(ALL_LOOKUPS.filter((lookup) => !filtered.has(lookup)));
|
|
3823
|
-
const { hasMatch } = args;
|
|
3824
|
-
if (rest.size === 0 || !
|
|
4320
|
+
const { hasMatch: hasMatch2 } = args;
|
|
4321
|
+
if (rest.size === 0 || !hasMatch2) return events;
|
|
3825
4322
|
const matches = await Promise.all(
|
|
3826
|
-
events.filter((event) => numbers.has(event.eventKey)).map((event) =>
|
|
4323
|
+
events.filter((event) => numbers.has(event.eventKey)).map((event) => hasMatch2(event))
|
|
3827
4324
|
);
|
|
3828
4325
|
if (!matches.some(Boolean)) return events;
|
|
3829
4326
|
return await withLookups(context, events, rest);
|
|
3830
4327
|
}
|
|
3831
4328
|
|
|
3832
4329
|
// src/webhooks/pull-request-people.ts
|
|
3833
|
-
import { z as
|
|
4330
|
+
import { z as z23 } from "zod";
|
|
3834
4331
|
var PEOPLE_EVENTS = /* @__PURE__ */ new Set([
|
|
3835
4332
|
"pull_request.review.approved",
|
|
3836
4333
|
"pull_request.merged"
|
|
@@ -3840,18 +4337,18 @@ var PAGE_SIZE = 100;
|
|
|
3840
4337
|
var MAX_PAGES = 3;
|
|
3841
4338
|
var CO_AUTHOR_TRAILER = /^co-authored-by:[^<\n]*<([^>\s]+@[^>\s]+)>\s*$/gim;
|
|
3842
4339
|
var GITHUB_NOREPLY_EMAIL = /^(?:\d+\+)?(?<login>[^@+]+)@users\.noreply\.github\.com$/i;
|
|
3843
|
-
var webhookSchema2 =
|
|
3844
|
-
pull_request:
|
|
3845
|
-
assignees:
|
|
3846
|
-
number:
|
|
3847
|
-
user:
|
|
4340
|
+
var webhookSchema2 = z23.object({
|
|
4341
|
+
pull_request: z23.object({
|
|
4342
|
+
assignees: z23.array(z23.object({ login: z23.string().min(1) })).nullish(),
|
|
4343
|
+
number: z23.number(),
|
|
4344
|
+
user: z23.object({ login: z23.string().min(1) }).nullish()
|
|
3848
4345
|
}),
|
|
3849
|
-
repository:
|
|
4346
|
+
repository: z23.object({ full_name: z23.string().min(1) })
|
|
3850
4347
|
});
|
|
3851
|
-
var commitPageSchema =
|
|
3852
|
-
|
|
3853
|
-
author:
|
|
3854
|
-
commit:
|
|
4348
|
+
var commitPageSchema = z23.array(
|
|
4349
|
+
z23.object({
|
|
4350
|
+
author: z23.object({ login: z23.string().min(1) }).nullish(),
|
|
4351
|
+
commit: z23.object({ message: z23.string() })
|
|
3855
4352
|
})
|
|
3856
4353
|
);
|
|
3857
4354
|
function shareCommitPages(load) {
|
|
@@ -3927,16 +4424,16 @@ async function loadGitHubPullRequestAuthors(args) {
|
|
|
3927
4424
|
return [...authors.values()].slice(0, MAX_PEOPLE);
|
|
3928
4425
|
}
|
|
3929
4426
|
async function withGitHubPullRequestPeople(args) {
|
|
3930
|
-
const { hasMatch } = args;
|
|
4427
|
+
const { hasMatch: hasMatch2 } = args;
|
|
3931
4428
|
const peopleEvents = args.events.filter(
|
|
3932
4429
|
(event) => PEOPLE_EVENTS.has(event.eventType)
|
|
3933
4430
|
);
|
|
3934
4431
|
const parsed = webhookSchema2.safeParse(args.body);
|
|
3935
|
-
if (peopleEvents.length === 0 || !
|
|
4432
|
+
if (peopleEvents.length === 0 || !hasMatch2 || !args.load || !parsed.success) {
|
|
3936
4433
|
return args.events;
|
|
3937
4434
|
}
|
|
3938
4435
|
const matches = await Promise.all(
|
|
3939
|
-
peopleEvents.map((event) =>
|
|
4436
|
+
peopleEvents.map((event) => hasMatch2(event))
|
|
3940
4437
|
);
|
|
3941
4438
|
if (!matches.some(Boolean)) return args.events;
|
|
3942
4439
|
const { pull_request: pullRequest, repository: repository2 } = parsed.data;
|
|
@@ -3996,10 +4493,10 @@ function webhookInstallationId(body) {
|
|
|
3996
4493
|
}
|
|
3997
4494
|
return parseInstallationId(installation.id);
|
|
3998
4495
|
}
|
|
3999
|
-
var feedbackTargetSchema =
|
|
4000
|
-
commentId:
|
|
4001
|
-
commentKind:
|
|
4002
|
-
repo:
|
|
4496
|
+
var feedbackTargetSchema = z24.object({
|
|
4497
|
+
commentId: z24.number(),
|
|
4498
|
+
commentKind: z24.enum(["conversation", "review"]),
|
|
4499
|
+
repo: z24.string().min(1)
|
|
4003
4500
|
});
|
|
4004
4501
|
function pullRequestFeedbackTarget(events) {
|
|
4005
4502
|
const event = events.find(
|
|
@@ -4156,7 +4653,7 @@ function createGitHubWebhookRoute(args) {
|
|
|
4156
4653
|
log: args.log,
|
|
4157
4654
|
privateKeyEnv: args.privateKeyEnv
|
|
4158
4655
|
}) : void 0;
|
|
4159
|
-
const
|
|
4656
|
+
const hasMatch2 = args.events.hasMatch;
|
|
4160
4657
|
const commitPages = args.loadPullRequestCommitPages ? shareCommitPages(args.loadPullRequestCommitPages) : void 0;
|
|
4161
4658
|
const events = await withGitHubPullRequestPeople({
|
|
4162
4659
|
body,
|
|
@@ -4173,7 +4670,7 @@ function createGitHubWebhookRoute(args) {
|
|
|
4173
4670
|
deliveryId,
|
|
4174
4671
|
eventName
|
|
4175
4672
|
}),
|
|
4176
|
-
hasMatch,
|
|
4673
|
+
hasMatch: hasMatch2,
|
|
4177
4674
|
...commitPages ? {
|
|
4178
4675
|
loadAuthors: async (target) => await loadGitHubPullRequestAuthors({
|
|
4179
4676
|
loadPage: commitPages(target)
|
|
@@ -4184,7 +4681,7 @@ function createGitHubWebhookRoute(args) {
|
|
|
4184
4681
|
log: args.log,
|
|
4185
4682
|
neededMatchKeys: args.events.neededMatchKeys
|
|
4186
4683
|
}),
|
|
4187
|
-
hasMatch,
|
|
4684
|
+
hasMatch: hasMatch2,
|
|
4188
4685
|
...commitPages ? {
|
|
4189
4686
|
load: async (target) => await loadGitHubPullRequestPeople({
|
|
4190
4687
|
logins: target.logins,
|
|
@@ -4194,7 +4691,7 @@ function createGitHubWebhookRoute(args) {
|
|
|
4194
4691
|
log: args.log
|
|
4195
4692
|
});
|
|
4196
4693
|
const feedbackTarget = pullRequestFeedbackTarget(events);
|
|
4197
|
-
const feedbackHasMatch = feedbackTarget &&
|
|
4694
|
+
const feedbackHasMatch = feedbackTarget && hasMatch2 ? (await Promise.all(events.map((event) => hasMatch2(event)))).some(
|
|
4198
4695
|
Boolean
|
|
4199
4696
|
) : false;
|
|
4200
4697
|
if (feedbackTarget && feedbackHasMatch) {
|
|
@@ -4209,12 +4706,34 @@ function createGitHubWebhookRoute(args) {
|
|
|
4209
4706
|
});
|
|
4210
4707
|
}
|
|
4211
4708
|
}
|
|
4709
|
+
events.push(
|
|
4710
|
+
...await loadGitHubMergeConflictEvents({
|
|
4711
|
+
deliveryId,
|
|
4712
|
+
events,
|
|
4713
|
+
hasMatch: hasMatch2,
|
|
4714
|
+
loadPullRequest: args.loadPullRequest,
|
|
4715
|
+
log: args.log,
|
|
4716
|
+
pullRequestPayload: eventName === "pull_request" ? body : void 0
|
|
4717
|
+
})
|
|
4718
|
+
);
|
|
4212
4719
|
for (const event of events) {
|
|
4213
4720
|
await args.events.publish({
|
|
4214
4721
|
...event,
|
|
4215
4722
|
objectType: event.eventType.startsWith("pull_request.") ? "code_change" : event.eventType.startsWith("issue.") ? "task" : event.eventType.startsWith("deployment.") ? "deployment" : "item"
|
|
4216
4723
|
});
|
|
4217
4724
|
}
|
|
4725
|
+
await syncGitHubPullRequestReadyNotices({
|
|
4726
|
+
db: args.db,
|
|
4727
|
+
deliveryId,
|
|
4728
|
+
env: {
|
|
4729
|
+
appIdEnv: args.appIdEnv,
|
|
4730
|
+
installationIdEnv: args.installationIdEnv,
|
|
4731
|
+
privateKeyEnv: args.privateKeyEnv
|
|
4732
|
+
},
|
|
4733
|
+
log: args.log,
|
|
4734
|
+
reviewItems: args.reviewItems,
|
|
4735
|
+
targets: pullRequestReadyTargets(eventName, body)
|
|
4736
|
+
});
|
|
4218
4737
|
if (!pullRequestOutcome && !issueOutcome && !recordedIssueConversations && !recordedPullRequestConversations && !recordedPullRequestLinkedIssues && events.length === 0) {
|
|
4219
4738
|
return new Response("Ignored", { status: 202 });
|
|
4220
4739
|
}
|
|
@@ -4242,98 +4761,19 @@ function gitHubEventDeclarations(pullRequestGuidance) {
|
|
|
4242
4761
|
return declarations;
|
|
4243
4762
|
}
|
|
4244
4763
|
|
|
4245
|
-
// src/webhooks/installation-requests.ts
|
|
4246
|
-
import { z as z22 } from "zod";
|
|
4247
|
-
function splitRepository(fullName, label) {
|
|
4248
|
-
const [owner, name, ...extra] = fullName.split("/");
|
|
4249
|
-
if (!owner || !name || extra.length > 0) {
|
|
4250
|
-
throw new Error(`GitHub ${label} received an invalid repository name`);
|
|
4251
|
-
}
|
|
4252
|
-
return { owner, name };
|
|
4253
|
-
}
|
|
4254
|
-
async function loadPullRequestCommitPages(env, pullRequest) {
|
|
4255
|
-
const { owner, name } = splitRepository(
|
|
4256
|
-
pullRequest.repositoryFullName,
|
|
4257
|
-
"pull request commits"
|
|
4258
|
-
);
|
|
4259
|
-
const token = await issueInstallationToken({
|
|
4260
|
-
...env,
|
|
4261
|
-
permissions: { pull_requests: "read" },
|
|
4262
|
-
repositories: [name]
|
|
4263
|
-
});
|
|
4264
|
-
return async (page, perPage) => await githubRequest(
|
|
4265
|
-
"https://api.github.com",
|
|
4266
|
-
`/repos/${encodeURIComponent(owner)}/${encodeURIComponent(name)}/pulls/${pullRequest.number}/commits?per_page=${perPage}&page=${page}`,
|
|
4267
|
-
{ token: token.token }
|
|
4268
|
-
);
|
|
4269
|
-
}
|
|
4270
|
-
async function markPullRequestFeedbackReviewing(env, target) {
|
|
4271
|
-
const { owner, name } = splitRepository(target.repo, "pull request feedback");
|
|
4272
|
-
const token = await issueInstallationToken({
|
|
4273
|
-
...env,
|
|
4274
|
-
repositories: [name]
|
|
4275
|
-
});
|
|
4276
|
-
await githubRequest(
|
|
4277
|
-
"https://api.github.com",
|
|
4278
|
-
pullRequestCommentReactionsPath({
|
|
4279
|
-
owner,
|
|
4280
|
-
name,
|
|
4281
|
-
commentKind: target.commentKind,
|
|
4282
|
-
commentId: target.commentId
|
|
4283
|
-
}),
|
|
4284
|
-
{
|
|
4285
|
-
body: { content: pullRequestFeedbackReactionContent("reviewing") },
|
|
4286
|
-
method: "POST",
|
|
4287
|
-
token: token.token
|
|
4288
|
-
}
|
|
4289
|
-
);
|
|
4290
|
-
}
|
|
4291
|
-
var repositoryPermissionSchema = z22.object({
|
|
4292
|
-
permission: z22.string().min(1),
|
|
4293
|
-
role_name: z22.string().optional()
|
|
4294
|
-
});
|
|
4295
|
-
var REPOSITORY_ROLE_TTL_MS = 10 * 60 * 1e3;
|
|
4296
|
-
async function loadRepositoryRole(env, cache, target) {
|
|
4297
|
-
const { owner, name } = splitRepository(
|
|
4298
|
-
target.repositoryFullName,
|
|
4299
|
-
"repository role"
|
|
4300
|
-
);
|
|
4301
|
-
return await cache.getOrLoad(
|
|
4302
|
-
`repository-role:${owner}/${name}:${target.login}`.toLowerCase(),
|
|
4303
|
-
async () => {
|
|
4304
|
-
const token = await issueInstallationToken({
|
|
4305
|
-
...env,
|
|
4306
|
-
permissions: { metadata: "read" },
|
|
4307
|
-
repositories: [name]
|
|
4308
|
-
});
|
|
4309
|
-
const response = repositoryPermissionSchema.parse(
|
|
4310
|
-
await githubRequest(
|
|
4311
|
-
"https://api.github.com",
|
|
4312
|
-
`/repos/${encodeURIComponent(owner)}/${encodeURIComponent(name)}/collaborators/${encodeURIComponent(target.login)}/permission`,
|
|
4313
|
-
{ token: token.token }
|
|
4314
|
-
)
|
|
4315
|
-
);
|
|
4316
|
-
return {
|
|
4317
|
-
value: response.role_name?.trim() || response.permission,
|
|
4318
|
-
ttlMs: REPOSITORY_ROLE_TTL_MS
|
|
4319
|
-
};
|
|
4320
|
-
}
|
|
4321
|
-
);
|
|
4322
|
-
}
|
|
4323
|
-
|
|
4324
4764
|
// src/pull-request-outcomes/commit-composition.ts
|
|
4325
|
-
import { z as
|
|
4326
|
-
var canonicalCommitSchema =
|
|
4327
|
-
authorEmail:
|
|
4328
|
-
authorLogin:
|
|
4765
|
+
import { z as z25 } from "zod";
|
|
4766
|
+
var canonicalCommitSchema = z25.object({
|
|
4767
|
+
authorEmail: z25.string().nullable(),
|
|
4768
|
+
authorLogin: z25.string().nullable()
|
|
4329
4769
|
}).strict();
|
|
4330
|
-
var providerCommitSchema =
|
|
4331
|
-
author:
|
|
4332
|
-
commit:
|
|
4333
|
-
author:
|
|
4770
|
+
var providerCommitSchema = z25.object({
|
|
4771
|
+
author: z25.object({ login: z25.string() }).passthrough().nullable(),
|
|
4772
|
+
commit: z25.object({
|
|
4773
|
+
author: z25.object({ email: z25.string() }).passthrough().nullable()
|
|
4334
4774
|
}).passthrough()
|
|
4335
4775
|
}).passthrough();
|
|
4336
|
-
var commitPageSchema2 =
|
|
4776
|
+
var commitPageSchema2 = z25.array(providerCommitSchema).transform(
|
|
4337
4777
|
(commits) => commits.map(
|
|
4338
4778
|
(commit) => canonicalCommitSchema.parse({
|
|
4339
4779
|
authorEmail: commit.commit.author?.email ?? null,
|
|
@@ -5825,11 +6265,13 @@ function githubPlugin(options = {}) {
|
|
|
5825
6265
|
loadGitHubLogin: async (identityId) => linkedGitHubLogin(
|
|
5826
6266
|
(await ctx.users.readIdentity(identityId))?.user
|
|
5827
6267
|
),
|
|
6268
|
+
loadPullRequest: async (pullRequest) => await loadPullRequest(installationEnv, pullRequest),
|
|
5828
6269
|
loadPullRequestCommitPages: async (pullRequest) => await loadPullRequestCommitPages(installationEnv, pullRequest),
|
|
5829
6270
|
log: ctx.log,
|
|
5830
6271
|
markFeedbackReviewing: async (target) => await markPullRequestFeedbackReviewing(installationEnv, target),
|
|
5831
6272
|
privateKeyEnv,
|
|
5832
6273
|
events: ctx.events,
|
|
6274
|
+
reviewItems: ctx.reviewItems,
|
|
5833
6275
|
webhookSecret: () => readEnv("GITHUB_WEBHOOK_SECRET")
|
|
5834
6276
|
})
|
|
5835
6277
|
];
|