@sentry/junior-github 0.302.0 → 0.304.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/dist/events-DGd7UVVs.js +2452 -0
- package/dist/index.d.ts +71 -3
- package/dist/index.js +7193 -8134
- package/dist/testing.d.ts +51 -1
- package/dist/testing.js +1 -6
- package/package.json +10 -13
- package/dist/account-links.d.ts +0 -8
- package/dist/annotations.d.ts +0 -33
- package/dist/chunk-6SOJ5TN6.js +0 -2618
- package/dist/clone-command.d.ts +0 -7
- package/dist/configured-repository.d.ts +0 -10
- package/dist/conversation-title-context.d.ts +0 -20
- package/dist/credential-support.d.ts +0 -107
- package/dist/db/database.d.ts +0 -5
- package/dist/db/schema.d.ts +0 -1412
- package/dist/default-branch.d.ts +0 -15
- package/dist/draft-readiness.d.ts +0 -31
- package/dist/egress-policy.d.ts +0 -25
- package/dist/events/comment.d.ts +0 -14
- package/dist/events/declarations.d.ts +0 -18
- package/dist/events/deployment.d.ts +0 -38
- package/dist/events/issue.d.ts +0 -42
- package/dist/events/pull-request.d.ts +0 -75
- package/dist/events/release.d.ts +0 -42
- package/dist/events/repository.d.ts +0 -23
- package/dist/events/resource-url.d.ts +0 -5
- package/dist/events/scope.d.ts +0 -13
- package/dist/git-config.d.ts +0 -52
- package/dist/github-status.d.ts +0 -16
- package/dist/graphql-documents.d.ts +0 -44
- package/dist/hooks-path-command.d.ts +0 -5
- package/dist/images.d.ts +0 -8
- package/dist/issue-outcomes/store.d.ts +0 -42
- package/dist/merge-conflict-checks/heartbeat.d.ts +0 -31
- package/dist/merge-conflict-checks/policy.d.ts +0 -74
- package/dist/merge-conflict-checks/store.d.ts +0 -44
- package/dist/merge-conflict-checks/webhooks.d.ts +0 -23
- package/dist/object-facts.d.ts +0 -29
- package/dist/permissions.d.ts +0 -6
- package/dist/plugin.d.ts +0 -45
- package/dist/pull-request-outcomes/apply.d.ts +0 -22
- package/dist/pull-request-outcomes/commit-composition.d.ts +0 -6
- package/dist/pull-request-outcomes/reconcile.d.ts +0 -42
- package/dist/pull-request-outcomes/store.d.ts +0 -120
- package/dist/pull-request-requesters/draft-reviewers.d.ts +0 -15
- package/dist/pull-request-requesters/store.d.ts +0 -41
- package/dist/pull-request-review-policy.d.ts +0 -6
- package/dist/receive-pack-policy.d.ts +0 -21
- package/dist/reply-markdown.d.ts +0 -18
- package/dist/sandbox-paths.d.ts +0 -4
- package/dist/tool-support/attribution.d.ts +0 -52
- package/dist/tool-support/github-api.d.ts +0 -51
- package/dist/tool-support/github-text.d.ts +0 -8
- package/dist/tool-support/pull-request-description.d.ts +0 -8
- package/dist/tools/clone-repository.d.ts +0 -287
- package/dist/tools/comment-on-issue.d.ts +0 -289
- package/dist/tools/comment-on-pull-request.d.ts +0 -294
- package/dist/tools/create-issue.d.ts +0 -300
- package/dist/tools/create-pull-request.d.ts +0 -322
- package/dist/tools/footer.d.ts +0 -17
- package/dist/tools/get-deployment.d.ts +0 -346
- package/dist/tools/get-issue.d.ts +0 -330
- package/dist/tools/get-pull-request.d.ts +0 -358
- package/dist/tools/get-release.d.ts +0 -325
- package/dist/tools/get-repository.d.ts +0 -308
- package/dist/tools/get-status.d.ts +0 -295
- package/dist/tools/react-to-issue.d.ts +0 -289
- package/dist/tools/resolve-pull-request-review-thread.d.ts +0 -288
- package/dist/tools/stack-pull-request.d.ts +0 -43
- package/dist/tools/submit-pull-request-review.d.ts +0 -300
- package/dist/tools/update-issue-comment.d.ts +0 -290
- package/dist/tools/update-issue.d.ts +0 -331
- package/dist/tools/update-pull-request-feedback.d.ts +0 -324
- package/dist/tools/update-pull-request.d.ts +0 -338
- package/dist/tools.d.ts +0 -13
- package/dist/webhooks/check-suite.d.ts +0 -121
- package/dist/webhooks/event-targets.d.ts +0 -44
- package/dist/webhooks/events.d.ts +0 -23
- package/dist/webhooks/fail-open-log.d.ts +0 -10
- package/dist/webhooks/handler.d.ts +0 -57
- package/dist/webhooks/installation-requests.d.ts +0 -105
- package/dist/webhooks/issue-outcome.d.ts +0 -11
- package/dist/webhooks/issue-update.d.ts +0 -8
- package/dist/webhooks/merge-conflict.d.ts +0 -53
- package/dist/webhooks/ownership.d.ts +0 -14
- package/dist/webhooks/pull-request-action.d.ts +0 -37
- package/dist/webhooks/pull-request-activity.d.ts +0 -11
- package/dist/webhooks/pull-request-match-data.d.ts +0 -31
- package/dist/webhooks/pull-request-outcome.d.ts +0 -16
- package/dist/webhooks/pull-request-people.d.ts +0 -51
- package/dist/webhooks/pull-request-preview.d.ts +0 -12
- package/dist/webhooks/pull-request-ready-reviewers.d.ts +0 -25
- package/dist/workspace-prepare.d.ts +0 -3
package/dist/chunk-6SOJ5TN6.js
DELETED
|
@@ -1,2618 +0,0 @@
|
|
|
1
|
-
// src/webhooks/events.ts
|
|
2
|
-
import * as z6 from "zod";
|
|
3
|
-
|
|
4
|
-
// src/events/deployment.ts
|
|
5
|
-
import {
|
|
6
|
-
eventMatchFieldsSchema as eventMatchFieldsSchema2
|
|
7
|
-
} from "@sentry/junior-plugin-api";
|
|
8
|
-
|
|
9
|
-
// src/events/scope.ts
|
|
10
|
-
import {
|
|
11
|
-
eventMatchFieldsSchema
|
|
12
|
-
} from "@sentry/junior-plugin-api";
|
|
13
|
-
import * as z from "zod";
|
|
14
|
-
var gitHubRepoDataSchema = z.string().min(1).toLowerCase();
|
|
15
|
-
var gitHubCommitShaDataSchema = z.string().regex(/^[0-9a-f]{40}$/i).toLowerCase();
|
|
16
|
-
function gitHubRepoMatchFields(events) {
|
|
17
|
-
return eventMatchFieldsSchema.parse({
|
|
18
|
-
repo: {
|
|
19
|
-
kind: "string",
|
|
20
|
-
case: "lower",
|
|
21
|
-
description: "lowercase repository as owner/repo, such as getsentry/junior",
|
|
22
|
-
events
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
function gitHubCommitMatchFields(events) {
|
|
27
|
-
return eventMatchFieldsSchema.parse({
|
|
28
|
-
commitSha: {
|
|
29
|
-
kind: "string",
|
|
30
|
-
case: "lower",
|
|
31
|
-
description: "full lowercase Git commit SHA",
|
|
32
|
-
events
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// src/events/deployment.ts
|
|
38
|
-
var GITHUB_DEPLOYMENT_EVENTS = [
|
|
39
|
-
"deployment.created",
|
|
40
|
-
"deployment.queued",
|
|
41
|
-
"deployment.pending",
|
|
42
|
-
"deployment.in_progress",
|
|
43
|
-
"deployment.succeeded",
|
|
44
|
-
"deployment.failed",
|
|
45
|
-
"deployment.error"
|
|
46
|
-
];
|
|
47
|
-
var GITHUB_DEPLOYMENT_SUGGESTED_EVENTS = [
|
|
48
|
-
"deployment.succeeded",
|
|
49
|
-
"deployment.failed",
|
|
50
|
-
"deployment.error"
|
|
51
|
-
];
|
|
52
|
-
var GITHUB_DEPLOYMENT_MATCH_FIELDS = eventMatchFieldsSchema2.parse({
|
|
53
|
-
...gitHubRepoMatchFields(GITHUB_DEPLOYMENT_EVENTS),
|
|
54
|
-
...gitHubCommitMatchFields(GITHUB_DEPLOYMENT_EVENTS),
|
|
55
|
-
deploymentId: {
|
|
56
|
-
kind: "number",
|
|
57
|
-
description: "numeric GitHub deployment id, such as 3001",
|
|
58
|
-
events: [...GITHUB_DEPLOYMENT_EVENTS]
|
|
59
|
-
},
|
|
60
|
-
environment: {
|
|
61
|
-
kind: "string",
|
|
62
|
-
case: "lower",
|
|
63
|
-
description: "lowercase deployment environment name, such as production",
|
|
64
|
-
events: [...GITHUB_DEPLOYMENT_EVENTS]
|
|
65
|
-
},
|
|
66
|
-
workflowId: {
|
|
67
|
-
kind: "number",
|
|
68
|
-
description: "numeric GitHub Actions workflow id when a workflow created the deployment",
|
|
69
|
-
events: [...GITHUB_DEPLOYMENT_EVENTS]
|
|
70
|
-
},
|
|
71
|
-
workflowPath: {
|
|
72
|
-
kind: "string",
|
|
73
|
-
description: "GitHub Actions workflow file, such as .github/workflows/deploy.yml, when a workflow created the deployment",
|
|
74
|
-
events: [...GITHUB_DEPLOYMENT_EVENTS]
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
var GITHUB_DEPLOYMENT_SOURCE_IDENTIFIER = {
|
|
78
|
-
format: "deployment-source:owner/repo[:environment]:<full-commit-sha>",
|
|
79
|
-
pattern: String.raw`^deployment-source:[^\s/:#]+\/[^\s/:#]+(?::[^\s:]+)?:[0-9a-f]+$`
|
|
80
|
-
};
|
|
81
|
-
function gitHubDeploymentSourceResource(input) {
|
|
82
|
-
const commitSha = input.commitSha.toLowerCase();
|
|
83
|
-
const environment = input.environment?.trim();
|
|
84
|
-
const repo = input.repo.toLowerCase();
|
|
85
|
-
return {
|
|
86
|
-
label: `GitHub deployment for ${repo} at ${commitSha.slice(0, 12)}`,
|
|
87
|
-
namespace: "github",
|
|
88
|
-
identifier: environment ? `deployment-source:${repo}:${encodeURIComponent(environment.toLowerCase())}:${commitSha}` : `deployment-source:${repo}:${commitSha}`
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
function gitHubDeploymentSourceSubscribable(input) {
|
|
92
|
-
if (!input.canSubscribe) return void 0;
|
|
93
|
-
const environment = input.environment?.trim().toLowerCase();
|
|
94
|
-
return {
|
|
95
|
-
...gitHubDeploymentSourceResource(input),
|
|
96
|
-
match: {
|
|
97
|
-
repo: gitHubRepoDataSchema.parse(input.repo),
|
|
98
|
-
commit_sha: gitHubCommitShaDataSchema.parse(input.commitSha),
|
|
99
|
-
...environment ? { environment } : void 0
|
|
100
|
-
},
|
|
101
|
-
suggested_events: GITHUB_DEPLOYMENT_SUGGESTED_EVENTS,
|
|
102
|
-
supported_events: [...GITHUB_DEPLOYMENT_EVENTS],
|
|
103
|
-
type: "deployment_source"
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// src/events/issue.ts
|
|
108
|
-
import {
|
|
109
|
-
eventMatchFieldsSchema as eventMatchFieldsSchema4,
|
|
110
|
-
mergeEventMatchFields
|
|
111
|
-
} from "@sentry/junior-plugin-api";
|
|
112
|
-
|
|
113
|
-
// src/events/comment.ts
|
|
114
|
-
import {
|
|
115
|
-
eventMatchFieldsSchema as eventMatchFieldsSchema3
|
|
116
|
-
} from "@sentry/junior-plugin-api";
|
|
117
|
-
function gitHubSenderMatchFields(events) {
|
|
118
|
-
return eventMatchFieldsSchema3.parse({
|
|
119
|
-
senderId: {
|
|
120
|
-
kind: "number",
|
|
121
|
-
description: "immutable numeric GitHub user id of who wrote the comment or review, or made the update; prefer over logins",
|
|
122
|
-
events
|
|
123
|
-
},
|
|
124
|
-
senderLogin: {
|
|
125
|
-
kind: "string",
|
|
126
|
-
description: "GitHub login of who wrote the comment or review, or made the update; logins can be renamed",
|
|
127
|
-
events
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
function gitHubCommentMatchFields(events) {
|
|
132
|
-
return eventMatchFieldsSchema3.parse({
|
|
133
|
-
mentionsBot: {
|
|
134
|
-
kind: "boolean",
|
|
135
|
-
description: "true when the comment or review mentions this GitHub App",
|
|
136
|
-
events
|
|
137
|
-
},
|
|
138
|
-
...gitHubSenderMatchFields(events)
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
// src/events/issue.ts
|
|
143
|
-
var GITHUB_ISSUE_EVENTS = [
|
|
144
|
-
"issue.comment.created",
|
|
145
|
-
"issue.opened",
|
|
146
|
-
"issue.closed",
|
|
147
|
-
"issue.reopened",
|
|
148
|
-
"issue.updated"
|
|
149
|
-
];
|
|
150
|
-
var GITHUB_ISSUE_SUGGESTED_EVENTS = [
|
|
151
|
-
"issue.comment.created",
|
|
152
|
-
"issue.closed",
|
|
153
|
-
"issue.reopened",
|
|
154
|
-
"issue.updated"
|
|
155
|
-
];
|
|
156
|
-
var GITHUB_ISSUE_UPDATE_CHANGES = [
|
|
157
|
-
"edited",
|
|
158
|
-
"labeled",
|
|
159
|
-
"unlabeled",
|
|
160
|
-
"assigned",
|
|
161
|
-
"unassigned",
|
|
162
|
-
"milestoned",
|
|
163
|
-
"demilestoned"
|
|
164
|
-
];
|
|
165
|
-
var GITHUB_ISSUE_MATCH_FIELDS = mergeEventMatchFields(
|
|
166
|
-
gitHubCommentMatchFields(["issue.comment.created"]),
|
|
167
|
-
gitHubSenderMatchFields(["issue.updated"]),
|
|
168
|
-
eventMatchFieldsSchema4.parse({
|
|
169
|
-
...gitHubRepoMatchFields(GITHUB_ISSUE_EVENTS),
|
|
170
|
-
issue: {
|
|
171
|
-
kind: "number",
|
|
172
|
-
description: "issue number, such as 12",
|
|
173
|
-
events: GITHUB_ISSUE_EVENTS
|
|
174
|
-
},
|
|
175
|
-
change: {
|
|
176
|
-
kind: "string",
|
|
177
|
-
enum: [...GITHUB_ISSUE_UPDATE_CHANGES],
|
|
178
|
-
description: "what the issue update changed, such as labeled",
|
|
179
|
-
events: ["issue.updated"]
|
|
180
|
-
},
|
|
181
|
-
label: {
|
|
182
|
-
kind: "string",
|
|
183
|
-
case: "lower",
|
|
184
|
-
description: "lowercase name of the label that was added or removed, set only when change is labeled or unlabeled",
|
|
185
|
-
events: ["issue.updated"]
|
|
186
|
-
}
|
|
187
|
-
})
|
|
188
|
-
);
|
|
189
|
-
var GITHUB_ISSUE_IDENTIFIER = {
|
|
190
|
-
format: "owner/repo#number",
|
|
191
|
-
pattern: String.raw`^[^\s/:#]+\/[^\s/:#]+#\d+$`
|
|
192
|
-
};
|
|
193
|
-
function gitHubIssueResource(input) {
|
|
194
|
-
return {
|
|
195
|
-
label: `GitHub issue ${input.repo}#${input.number}`,
|
|
196
|
-
namespace: "github",
|
|
197
|
-
identifier: `${input.repo.toLowerCase()}#${input.number}`
|
|
198
|
-
};
|
|
199
|
-
}
|
|
200
|
-
function gitHubIssueSubscribable(input) {
|
|
201
|
-
if (!input.canSubscribe) return void 0;
|
|
202
|
-
return {
|
|
203
|
-
...gitHubIssueResource(input),
|
|
204
|
-
match: {
|
|
205
|
-
repo: gitHubRepoDataSchema.parse(input.repo),
|
|
206
|
-
issue: input.number
|
|
207
|
-
},
|
|
208
|
-
suggested_events: GITHUB_ISSUE_SUGGESTED_EVENTS,
|
|
209
|
-
supported_events: GITHUB_ISSUE_EVENTS,
|
|
210
|
-
type: "issue"
|
|
211
|
-
};
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
// src/events/pull-request.ts
|
|
215
|
-
import {
|
|
216
|
-
eventMatchFieldsSchema as eventMatchFieldsSchema5
|
|
217
|
-
} from "@sentry/junior-plugin-api";
|
|
218
|
-
var GITHUB_PULL_REQUEST_EVENTS = [
|
|
219
|
-
"pull_request.checks.failed",
|
|
220
|
-
"pull_request.checks.passed",
|
|
221
|
-
"pull_request.checks.recovered",
|
|
222
|
-
"pull_request.comment.created",
|
|
223
|
-
"pull_request.opened",
|
|
224
|
-
"pull_request.ready_for_review",
|
|
225
|
-
"pull_request.review.approved",
|
|
226
|
-
"pull_request.review.changes_requested",
|
|
227
|
-
"pull_request.review.commented",
|
|
228
|
-
"pull_request.review_comment.created",
|
|
229
|
-
"pull_request.merged",
|
|
230
|
-
"pull_request.closed_unmerged",
|
|
231
|
-
"pull_request.merge_conflict"
|
|
232
|
-
];
|
|
233
|
-
var STACK_CHECK = "Check if the pull request is in a stack: `gh api 'repos/OWNER/REPO/stacks?pull_request=NUMBER'`.";
|
|
234
|
-
var STACK_CONFLICT_STEPS = "run `gh stack rebase`, resolve each conflict so that both changes stay intact, stage the files, and run `gh stack rebase --continue`. Run the checks again, run `gh stack push`, and confirm that each open pull request in the stack can merge. Report the files that had conflicts.";
|
|
235
|
-
var PULL_REQUEST_FEEDBACK = 'When Verified details include comment_id, call github_updatePullRequestFeedback after acting on actionable feedback, with the exact repo, pull_request, comment_id, and comment_kind from Verified details, and status "addressed" or "declined".';
|
|
236
|
-
var GITHUB_PULL_REQUEST_EVENT_GUIDANCE = {
|
|
237
|
-
"pull_request.checks.failed": "Fix only failures that the pull request causes. Do not skip, disable, or weaken a check to make it pass.",
|
|
238
|
-
// An approval with a body also arrives as review.commented in the same
|
|
239
|
-
// Turn, so this guidance must not tell the agent to hold off on that body.
|
|
240
|
-
"pull_request.review.approved": `When the approval includes actionable feedback, address it like any other review feedback. ${PULL_REQUEST_FEEDBACK}`,
|
|
241
|
-
"pull_request.review.changes_requested": PULL_REQUEST_FEEDBACK,
|
|
242
|
-
"pull_request.review.commented": PULL_REQUEST_FEEDBACK,
|
|
243
|
-
"pull_request.comment.created": PULL_REQUEST_FEEDBACK,
|
|
244
|
-
"pull_request.review_comment.created": `${PULL_REQUEST_FEEDBACK} Then resolve the thread with github_resolvePullRequestReviewThread.`,
|
|
245
|
-
"pull_request.merge_conflict": `${STACK_CHECK} If it is, run \`gh stack checkout NUMBER\`, then ${STACK_CONFLICT_STEPS} If it is not, fetch base_branch from Verified details and merge it into the pull request branch. Do not rebase. Resolve each conflict so that both the pull request change and the base branch change stay intact. Run the checks again, push, and confirm that the pull request can merge. Report the files that had conflicts.`
|
|
246
|
-
};
|
|
247
|
-
function gitHubPullRequestEventGuidance(guidance) {
|
|
248
|
-
const merged = { ...GITHUB_PULL_REQUEST_EVENT_GUIDANCE };
|
|
249
|
-
for (const eventType of GITHUB_PULL_REQUEST_EVENTS) {
|
|
250
|
-
const extra = guidance?.[eventType]?.trim();
|
|
251
|
-
if (!extra) continue;
|
|
252
|
-
const base = merged[eventType];
|
|
253
|
-
merged[eventType] = base ? `${base} ${extra}` : extra;
|
|
254
|
-
}
|
|
255
|
-
return merged;
|
|
256
|
-
}
|
|
257
|
-
var DEFAULT_PULL_REQUEST_SUBSCRIPTION = {
|
|
258
|
-
events: [
|
|
259
|
-
"pull_request.checks.failed",
|
|
260
|
-
"pull_request.closed_unmerged",
|
|
261
|
-
"pull_request.comment.created",
|
|
262
|
-
"pull_request.review.changes_requested",
|
|
263
|
-
"pull_request.review.commented",
|
|
264
|
-
"pull_request.review_comment.created"
|
|
265
|
-
],
|
|
266
|
-
intent: "Maintain pull requests Junior creates. For actionable review feedback or failed checks, inspect the evidence, make scoped fixes, verify them, and push, without asking first. Reply on GitHub to review comments and questions, in the thread where they were asked. Report completed work or blockers in the originating thread. Report when the pull request closes unmerged. When a reviewer submits a review, notify the person who asked for the pull request by mentioning them, with the reviewer, the review outcome, and the pull request link. Stay silent on other events that are not feedback."
|
|
267
|
-
};
|
|
268
|
-
function gitHubPullRequestSubscription(config) {
|
|
269
|
-
if (config === false) return void 0;
|
|
270
|
-
return config ?? DEFAULT_PULL_REQUEST_SUBSCRIPTION;
|
|
271
|
-
}
|
|
272
|
-
var MERGE_STATE_CHECK = "Do not guess whether it can merge. Before you say anything about that, call github_getPullRequest and read its `checks`. If `checks.required` is `not_reported`, the required checks may not have started on the latest commit yet. It does not mean that none are required. If `checks.auto_merge` is true and only required checks remain, say only that it merges when they pass. If `checks.merge_state` shows that GitHub blocks it for another reason, run `gh api repos/OWNER/REPO/rules/branches/BASE` and name the rule that blocks it. Do not say that you did not merge it.";
|
|
273
|
-
var GITHUB_PULL_REQUEST_OUTCOME_GUIDANCE = {
|
|
274
|
-
"pull_request.review.approved": `Mention each person in People involved, or the person who asked for the pull request if that line is missing. Say who approved it and link it. Do not merge it unless asked. ${MERGE_STATE_CHECK}`,
|
|
275
|
-
"pull_request.merged": "Mention each person in People involved, or the person who asked for the pull request if that line is missing. Say that it merged and link it."
|
|
276
|
-
};
|
|
277
|
-
var DRAFT_CHECKS_RECOVERED_GUIDANCE = "If the pull request is still a draft, call github_getPullRequest and read its `checks`, `code_owners`, and `agents_md`. When `checks.required` is `passing` and no decision is open, in the pull request body or in an open review item that waits on the person who asked for it: mark it ready with github_updatePullRequest and draft false, and report that in the originating thread, when the person who asked for it said to mark it ready, or when `code_owners` is empty and `agents_md` does not ask for draft pull requests or say that a person marks them ready. Otherwise keep it a draft: marking it ready would ask the code owners to review, or break the AGENTS.md rule. Tell the person who asked for it in the originating thread that the checks pass and the draft waits for their review, and name why: the code owners that marking it ready asks, or the AGENTS.md rule. If you already told them for this head commit, stay silent. When it is `failing`, fix the failing checks like a failed check. When it is `pending` or `not_reported`, stay silent: a later passing suite wakes you again. `not_reported` means the required checks may not have started on the latest commit yet, not that none are required.";
|
|
278
|
-
function gitHubPullRequestCreateWatch(config, pullRequest) {
|
|
279
|
-
const outcomeEvents = Object.keys(
|
|
280
|
-
GITHUB_PULL_REQUEST_OUTCOME_GUIDANCE
|
|
281
|
-
);
|
|
282
|
-
return {
|
|
283
|
-
events: [
|
|
284
|
-
.../* @__PURE__ */ new Set([
|
|
285
|
-
...config.events,
|
|
286
|
-
// GitHub sends no webhook for a new conflict, so keep the pull
|
|
287
|
-
// request ready to merge even when the app config omits this event.
|
|
288
|
-
"pull_request.merge_conflict",
|
|
289
|
-
...outcomeEvents,
|
|
290
|
-
...pullRequest.draft ? ["pull_request.checks.recovered"] : []
|
|
291
|
-
])
|
|
292
|
-
],
|
|
293
|
-
guidance: pullRequest.draft ? {
|
|
294
|
-
...GITHUB_PULL_REQUEST_OUTCOME_GUIDANCE,
|
|
295
|
-
"pull_request.checks.recovered": DRAFT_CHECKS_RECOVERED_GUIDANCE
|
|
296
|
-
} : GITHUB_PULL_REQUEST_OUTCOME_GUIDANCE,
|
|
297
|
-
intent: config.intent
|
|
298
|
-
};
|
|
299
|
-
}
|
|
300
|
-
var GITHUB_PULL_REQUEST_SUGGESTED_EVENTS = [
|
|
301
|
-
"pull_request.checks.failed",
|
|
302
|
-
"pull_request.comment.created",
|
|
303
|
-
"pull_request.ready_for_review",
|
|
304
|
-
"pull_request.review.changes_requested",
|
|
305
|
-
"pull_request.review.commented",
|
|
306
|
-
"pull_request.review_comment.created",
|
|
307
|
-
"pull_request.merged",
|
|
308
|
-
"pull_request.closed_unmerged",
|
|
309
|
-
"pull_request.merge_conflict"
|
|
310
|
-
];
|
|
311
|
-
var GITHUB_PULL_REQUEST_PAYLOAD_EVENTS = GITHUB_PULL_REQUEST_EVENTS.filter(
|
|
312
|
-
(eventType) => eventType !== "pull_request.merge_conflict"
|
|
313
|
-
);
|
|
314
|
-
var GITHUB_PULL_REQUEST_COMMENT_EVENTS = [
|
|
315
|
-
"pull_request.comment.created",
|
|
316
|
-
"pull_request.review.approved",
|
|
317
|
-
"pull_request.review.changes_requested",
|
|
318
|
-
"pull_request.review.commented",
|
|
319
|
-
"pull_request.review_comment.created"
|
|
320
|
-
];
|
|
321
|
-
var GITHUB_PULL_REQUEST_CHECK_EVENTS = [
|
|
322
|
-
"pull_request.checks.failed",
|
|
323
|
-
"pull_request.checks.passed",
|
|
324
|
-
"pull_request.checks.recovered"
|
|
325
|
-
];
|
|
326
|
-
var GITHUB_PULL_REQUEST_MATCH_FIELDS = eventMatchFieldsSchema5.parse({
|
|
327
|
-
...gitHubCommentMatchFields(GITHUB_PULL_REQUEST_COMMENT_EVENTS),
|
|
328
|
-
...gitHubRepoMatchFields(GITHUB_PULL_REQUEST_EVENTS),
|
|
329
|
-
...gitHubCommitMatchFields(GITHUB_PULL_REQUEST_CHECK_EVENTS),
|
|
330
|
-
pullRequest: {
|
|
331
|
-
kind: "number",
|
|
332
|
-
description: "pull request number, such as 946",
|
|
333
|
-
events: [...GITHUB_PULL_REQUEST_EVENTS]
|
|
334
|
-
},
|
|
335
|
-
authors: {
|
|
336
|
-
kind: "string",
|
|
337
|
-
list: true,
|
|
338
|
-
description: "GitHub logins of the commit authors and Co-authored-by people, including bots such as this GitHub App",
|
|
339
|
-
events: [...GITHUB_PULL_REQUEST_PAYLOAD_EVENTS]
|
|
340
|
-
},
|
|
341
|
-
headBranch: {
|
|
342
|
-
kind: "string",
|
|
343
|
-
description: "head branch name from the webhook",
|
|
344
|
-
events: GITHUB_PULL_REQUEST_PAYLOAD_EVENTS.filter(
|
|
345
|
-
(eventType) => eventType !== "pull_request.comment.created"
|
|
346
|
-
)
|
|
347
|
-
},
|
|
348
|
-
isDraft: {
|
|
349
|
-
kind: "boolean",
|
|
350
|
-
description: "true when the pull request is a draft",
|
|
351
|
-
events: [...GITHUB_PULL_REQUEST_PAYLOAD_EVENTS]
|
|
352
|
-
},
|
|
353
|
-
openerEmail: {
|
|
354
|
-
kind: "string",
|
|
355
|
-
description: "opener email when GitHub sends it; missing on pull requests that this GitHub App opened",
|
|
356
|
-
events: [...GITHUB_PULL_REQUEST_PAYLOAD_EVENTS]
|
|
357
|
-
},
|
|
358
|
-
openerUsername: {
|
|
359
|
-
kind: "string",
|
|
360
|
-
description: "login of who opened the pull request; on pull requests that this GitHub App opened, the person who asked for it, missing until that person is saved",
|
|
361
|
-
events: [...GITHUB_PULL_REQUEST_PAYLOAD_EVENTS]
|
|
362
|
-
},
|
|
363
|
-
openerRole: {
|
|
364
|
-
kind: "string",
|
|
365
|
-
description: "opener's repository role: admin, maintain, write, triage, read, none (no access, such as most bots), or a custom role name",
|
|
366
|
-
events: [...GITHUB_PULL_REQUEST_PAYLOAD_EVENTS]
|
|
367
|
-
}
|
|
368
|
-
});
|
|
369
|
-
var GITHUB_PULL_REQUEST_IDENTIFIER = {
|
|
370
|
-
format: "owner/repo#number",
|
|
371
|
-
pattern: String.raw`^[^\s/:#]+\/[^\s/:#]+#\d+$`
|
|
372
|
-
};
|
|
373
|
-
function gitHubPullRequestResource(input) {
|
|
374
|
-
return {
|
|
375
|
-
label: `GitHub PR ${input.repo}#${input.number}`,
|
|
376
|
-
namespace: "github",
|
|
377
|
-
identifier: `${input.repo.toLowerCase()}#${input.number}`
|
|
378
|
-
};
|
|
379
|
-
}
|
|
380
|
-
function gitHubPullRequestSubscribable(input) {
|
|
381
|
-
if (!input.canSubscribe) return void 0;
|
|
382
|
-
const omitted = new Set(input.omitSuggestedEvents ?? []);
|
|
383
|
-
const suggestedEvents = GITHUB_PULL_REQUEST_SUGGESTED_EVENTS.filter(
|
|
384
|
-
(eventType) => !omitted.has(eventType)
|
|
385
|
-
);
|
|
386
|
-
return {
|
|
387
|
-
...gitHubPullRequestResource(input),
|
|
388
|
-
match: {
|
|
389
|
-
repo: gitHubRepoDataSchema.parse(input.repo),
|
|
390
|
-
pull_request: input.number
|
|
391
|
-
},
|
|
392
|
-
...suggestedEvents.length > 0 ? { suggested_events: [...suggestedEvents] } : void 0,
|
|
393
|
-
supported_events: [...GITHUB_PULL_REQUEST_EVENTS],
|
|
394
|
-
type: "pull_request"
|
|
395
|
-
};
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
// src/events/release.ts
|
|
399
|
-
import {
|
|
400
|
-
eventMatchFieldsSchema as eventMatchFieldsSchema6
|
|
401
|
-
} from "@sentry/junior-plugin-api";
|
|
402
|
-
import * as z2 from "zod";
|
|
403
|
-
var GITHUB_RELEASE_EVENTS = ["release.published"];
|
|
404
|
-
var gitHubReleaseTagDataSchema = z2.string().trim().min(1).transform(
|
|
405
|
-
(tag) => tag.replace(/[A-Z]+/g, (letters) => letters.toLowerCase())
|
|
406
|
-
);
|
|
407
|
-
var GITHUB_RELEASE_MATCH_FIELDS = eventMatchFieldsSchema6.parse({
|
|
408
|
-
...gitHubRepoMatchFields(GITHUB_RELEASE_EVENTS),
|
|
409
|
-
tag: {
|
|
410
|
-
kind: "string",
|
|
411
|
-
description: "release tag with ASCII letters in lowercase, such as v1.2.0 or release/1.0",
|
|
412
|
-
events: [...GITHUB_RELEASE_EVENTS]
|
|
413
|
-
}
|
|
414
|
-
});
|
|
415
|
-
var GITHUB_RELEASE_SUGGESTED_EVENTS = ["release.published"];
|
|
416
|
-
var GITHUB_RELEASE_SOURCE_IDENTIFIER = {
|
|
417
|
-
format: "release-source:owner/repo[:tag]",
|
|
418
|
-
pattern: String.raw`^release-source:[^\s/:#]+\/[^\s/:#]+(?::[^\s:]+)?$`
|
|
419
|
-
};
|
|
420
|
-
function gitHubReleaseSourceResource(input) {
|
|
421
|
-
const repo = input.repo.toLowerCase();
|
|
422
|
-
const tag = input.tag?.trim();
|
|
423
|
-
return {
|
|
424
|
-
label: `GitHub release for ${repo}`,
|
|
425
|
-
namespace: "github",
|
|
426
|
-
identifier: tag ? `release-source:${repo}:${encodeURIComponent(tag)}` : `release-source:${repo}`
|
|
427
|
-
};
|
|
428
|
-
}
|
|
429
|
-
function gitHubReleaseSourceSubscribable(input) {
|
|
430
|
-
if (!input.canSubscribe) return void 0;
|
|
431
|
-
const tag = input.tag?.trim();
|
|
432
|
-
return {
|
|
433
|
-
...gitHubReleaseSourceResource(input),
|
|
434
|
-
match: {
|
|
435
|
-
repo: gitHubRepoDataSchema.parse(input.repo),
|
|
436
|
-
...tag ? { tag: gitHubReleaseTagDataSchema.parse(tag) } : void 0
|
|
437
|
-
},
|
|
438
|
-
suggested_events: GITHUB_RELEASE_SUGGESTED_EVENTS,
|
|
439
|
-
supported_events: [...GITHUB_RELEASE_EVENTS],
|
|
440
|
-
type: "release_source"
|
|
441
|
-
};
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
// src/events/repository.ts
|
|
445
|
-
var GITHUB_REPOSITORY_IDENTIFIER = {
|
|
446
|
-
format: "owner/repo",
|
|
447
|
-
pattern: String.raw`^[^\s/:#]+\/[^\s/:#]+$`
|
|
448
|
-
};
|
|
449
|
-
function gitHubRepositoryResource(input) {
|
|
450
|
-
return {
|
|
451
|
-
label: `GitHub repository ${input.repo}`,
|
|
452
|
-
namespace: "github",
|
|
453
|
-
identifier: input.repo.toLowerCase()
|
|
454
|
-
};
|
|
455
|
-
}
|
|
456
|
-
var GITHUB_REPOSITORY_EVENTS = [
|
|
457
|
-
...GITHUB_DEPLOYMENT_EVENTS,
|
|
458
|
-
...GITHUB_ISSUE_EVENTS,
|
|
459
|
-
...GITHUB_PULL_REQUEST_EVENTS
|
|
460
|
-
];
|
|
461
|
-
var GITHUB_REPOSITORY_SUGGESTED_EVENTS = [
|
|
462
|
-
"issue.opened",
|
|
463
|
-
"pull_request.opened",
|
|
464
|
-
// Label, assignee, and edit updates are too frequent across a repository.
|
|
465
|
-
...GITHUB_ISSUE_SUGGESTED_EVENTS.filter(
|
|
466
|
-
(eventType) => eventType !== "issue.updated"
|
|
467
|
-
),
|
|
468
|
-
...GITHUB_PULL_REQUEST_SUGGESTED_EVENTS
|
|
469
|
-
];
|
|
470
|
-
function gitHubRepositorySubscribable(input) {
|
|
471
|
-
if (!input.canSubscribe) return void 0;
|
|
472
|
-
return {
|
|
473
|
-
...gitHubRepositoryResource(input),
|
|
474
|
-
match: { repo: gitHubRepoDataSchema.parse(input.repo) },
|
|
475
|
-
suggested_events: [...GITHUB_REPOSITORY_SUGGESTED_EVENTS],
|
|
476
|
-
supported_events: [...GITHUB_REPOSITORY_EVENTS],
|
|
477
|
-
type: "repository"
|
|
478
|
-
};
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
// src/webhooks/check-suite.ts
|
|
482
|
-
import * as z4 from "zod";
|
|
483
|
-
|
|
484
|
-
// src/credential-support.ts
|
|
485
|
-
import { createPrivateKey, createSign } from "crypto";
|
|
486
|
-
|
|
487
|
-
// src/permissions.ts
|
|
488
|
-
var LEVELS = /* @__PURE__ */ new Set(["read", "write", "admin"]);
|
|
489
|
-
var WRITE_ONLY_PERMISSIONS = /* @__PURE__ */ new Set(["profile", "workflows"]);
|
|
490
|
-
function isLevel(value) {
|
|
491
|
-
return LEVELS.has(value);
|
|
492
|
-
}
|
|
493
|
-
function normalizeScope(rawScope) {
|
|
494
|
-
return String(rawScope).trim().replace(/-/g, "_");
|
|
495
|
-
}
|
|
496
|
-
function normalizePermissions(permissions) {
|
|
497
|
-
if (permissions === void 0) {
|
|
498
|
-
return void 0;
|
|
499
|
-
}
|
|
500
|
-
const entries = Object.entries(permissions);
|
|
501
|
-
if (entries.length === 0) {
|
|
502
|
-
throw new Error(
|
|
503
|
-
"githubPlugin appPermissions must contain at least one permission when provided."
|
|
504
|
-
);
|
|
505
|
-
}
|
|
506
|
-
const request = {};
|
|
507
|
-
for (const [rawScope, rawLevel] of entries) {
|
|
508
|
-
const normalizedScope = normalizeScope(rawScope);
|
|
509
|
-
if (!normalizedScope) {
|
|
510
|
-
throw new Error(
|
|
511
|
-
"githubPlugin appPermissions contains an empty permission name."
|
|
512
|
-
);
|
|
513
|
-
}
|
|
514
|
-
if (!/^[a-z][a-z0-9_]*$/.test(normalizedScope)) {
|
|
515
|
-
throw new Error(
|
|
516
|
-
`githubPlugin appPermissions contains invalid permission "${rawScope}".`
|
|
517
|
-
);
|
|
518
|
-
}
|
|
519
|
-
if (!isLevel(rawLevel)) {
|
|
520
|
-
throw new Error(
|
|
521
|
-
`githubPlugin appPermissions.${rawScope} must be "read", "write", or "admin".`
|
|
522
|
-
);
|
|
523
|
-
}
|
|
524
|
-
request[normalizedScope] = rawLevel;
|
|
525
|
-
}
|
|
526
|
-
return request;
|
|
527
|
-
}
|
|
528
|
-
function readGrantPermissions(permissions) {
|
|
529
|
-
const readOnly = { metadata: "read" };
|
|
530
|
-
for (const [scope, level] of Object.entries(permissions ?? {})) {
|
|
531
|
-
if (!isLevel(level)) {
|
|
532
|
-
throw new Error(
|
|
533
|
-
`GitHub permission "${scope}" returned invalid level "${String(level)}".`
|
|
534
|
-
);
|
|
535
|
-
}
|
|
536
|
-
if (!WRITE_ONLY_PERMISSIONS.has(scope)) {
|
|
537
|
-
readOnly[scope] = "read";
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
return readOnly;
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
// src/credential-support.ts
|
|
544
|
-
var GITHUB_APP_ID_ENV = "GITHUB_APP_ID";
|
|
545
|
-
var GITHUB_APP_PRIVATE_KEY_ENV = "GITHUB_APP_PRIVATE_KEY";
|
|
546
|
-
var GITHUB_INSTALLATION_ID_ENV = "GITHUB_INSTALLATION_ID";
|
|
547
|
-
var GITHUB_AUTH_TOKEN_ENV = "GITHUB_TOKEN";
|
|
548
|
-
var GITHUB_AUTH_TOKEN_PLACEHOLDER = "ghp_host_managed_credential";
|
|
549
|
-
var MAX_LEASE_MS = 60 * 60 * 1e3;
|
|
550
|
-
var REFRESH_BUFFER_MS = 5 * 60 * 1e3;
|
|
551
|
-
var USER_REFRESH_TIMEOUT_MS = 2e4;
|
|
552
|
-
var GITHUB_GRAPHQL_RESPONSE_BODY_LIMIT_BYTES = 64 * 1024;
|
|
553
|
-
var HTTP_READ_METHODS = /* @__PURE__ */ new Set(["GET", "HEAD", "OPTIONS"]);
|
|
554
|
-
var USER_TOKEN_GRANTS = /* @__PURE__ */ new Set(["user-read", "user-write"]);
|
|
555
|
-
var CREATE_TOOL_ROUTING_GUIDANCE = "This is a tool-routing denial, not a GitHub permission failure. Do not ask the user for GitHub permissions; retry with the required tool.";
|
|
556
|
-
var USER_WRITE_REQUIREMENTS = [
|
|
557
|
-
"requesting GitHub user permission to perform this operation"
|
|
558
|
-
];
|
|
559
|
-
var GITHUB_CREDENTIAL_DOMAINS = ["api.github.com", "github.com"];
|
|
560
|
-
var GITHUB_ASSET_UPLOAD_CREDENTIAL_DOMAINS = [
|
|
561
|
-
...GITHUB_CREDENTIAL_DOMAINS,
|
|
562
|
-
"uploads.github.com"
|
|
563
|
-
];
|
|
564
|
-
var GitHubUserRefreshRejectedError = class extends Error {
|
|
565
|
-
constructor(message) {
|
|
566
|
-
super(message);
|
|
567
|
-
this.name = "GitHubUserRefreshRejectedError";
|
|
568
|
-
}
|
|
569
|
-
};
|
|
570
|
-
var GitHubRequestError = class extends Error {
|
|
571
|
-
status;
|
|
572
|
-
constructor(message, status) {
|
|
573
|
-
super(message);
|
|
574
|
-
this.name = "GitHubRequestError";
|
|
575
|
-
this.status = status;
|
|
576
|
-
}
|
|
577
|
-
};
|
|
578
|
-
var GitHubPluginSetupError = class extends Error {
|
|
579
|
-
constructor(message) {
|
|
580
|
-
super(message);
|
|
581
|
-
this.name = "GitHubPluginSetupError";
|
|
582
|
-
}
|
|
583
|
-
};
|
|
584
|
-
function isRecord(value) {
|
|
585
|
-
return Boolean(value && typeof value === "object" && !Array.isArray(value));
|
|
586
|
-
}
|
|
587
|
-
function readEnv(name) {
|
|
588
|
-
const value = process.env[name];
|
|
589
|
-
if (typeof value !== "string") {
|
|
590
|
-
return void 0;
|
|
591
|
-
}
|
|
592
|
-
const trimmed = value.trim();
|
|
593
|
-
return trimmed ? trimmed : void 0;
|
|
594
|
-
}
|
|
595
|
-
function requireEnv(name) {
|
|
596
|
-
const value = readEnv(name);
|
|
597
|
-
if (!value) {
|
|
598
|
-
throw new GitHubPluginSetupError(`Missing ${name}`);
|
|
599
|
-
}
|
|
600
|
-
return value;
|
|
601
|
-
}
|
|
602
|
-
function normalizeScopeList(scopes) {
|
|
603
|
-
return [
|
|
604
|
-
...new Set(
|
|
605
|
-
(scopes ?? []).flatMap((scope) => String(scope).split(/\s+/)).map((scope) => scope.trim()).filter(Boolean)
|
|
606
|
-
)
|
|
607
|
-
].sort();
|
|
608
|
-
}
|
|
609
|
-
function normalizeOAuthScope(scope) {
|
|
610
|
-
const normalized = normalizeScopeList(scope ? [scope] : []);
|
|
611
|
-
return normalized.length ? normalized.join(" ") : void 0;
|
|
612
|
-
}
|
|
613
|
-
function hasRequiredOAuthScope(storedScope, requiredScope) {
|
|
614
|
-
const required = normalizeScopeList(requiredScope ? [requiredScope] : []);
|
|
615
|
-
if (required.length === 0) {
|
|
616
|
-
return true;
|
|
617
|
-
}
|
|
618
|
-
const stored = new Set(normalizeScopeList(storedScope ? [storedScope] : []));
|
|
619
|
-
if (stored.size === 0) {
|
|
620
|
-
return false;
|
|
621
|
-
}
|
|
622
|
-
return required.every((scope) => stored.has(scope));
|
|
623
|
-
}
|
|
624
|
-
function isGitHubApiUrl(upstreamUrl) {
|
|
625
|
-
return upstreamUrl.hostname.toLowerCase() === "api.github.com";
|
|
626
|
-
}
|
|
627
|
-
function base64Url(input) {
|
|
628
|
-
return Buffer.from(input).toString("base64").replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
|
|
629
|
-
}
|
|
630
|
-
function getPrivateKey(envName) {
|
|
631
|
-
const raw = requireEnv(envName);
|
|
632
|
-
let key;
|
|
633
|
-
try {
|
|
634
|
-
key = createPrivateKey({ key: raw, format: "pem" });
|
|
635
|
-
} catch {
|
|
636
|
-
throw new GitHubPluginSetupError(
|
|
637
|
-
`Invalid ${envName}: expected a PEM-encoded RSA private key`
|
|
638
|
-
);
|
|
639
|
-
}
|
|
640
|
-
if (key.asymmetricKeyType !== "rsa") {
|
|
641
|
-
throw new GitHubPluginSetupError(
|
|
642
|
-
`Invalid ${envName}: GitHub App signing requires an RSA private key`
|
|
643
|
-
);
|
|
644
|
-
}
|
|
645
|
-
return key;
|
|
646
|
-
}
|
|
647
|
-
function createAppJwt(appId, privateKeyEnv) {
|
|
648
|
-
const now = Math.floor(Date.now() / 1e3);
|
|
649
|
-
const header = { alg: "RS256", typ: "JWT" };
|
|
650
|
-
const payload = { iat: now - 60, exp: now + 9 * 60, iss: appId };
|
|
651
|
-
const encodedHeader = base64Url(JSON.stringify(header));
|
|
652
|
-
const encodedPayload = base64Url(JSON.stringify(payload));
|
|
653
|
-
const signingInput = `${encodedHeader}.${encodedPayload}`;
|
|
654
|
-
const signer = createSign("RSA-SHA256");
|
|
655
|
-
signer.update(signingInput);
|
|
656
|
-
signer.end();
|
|
657
|
-
const signature = signer.sign(getPrivateKey(privateKeyEnv)).toString("base64").replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
|
|
658
|
-
return `${signingInput}.${signature}`;
|
|
659
|
-
}
|
|
660
|
-
async function githubRequest(apiBase, path, params) {
|
|
661
|
-
const response = await fetch(`${apiBase}${path}`, {
|
|
662
|
-
method: params.method ?? "GET",
|
|
663
|
-
headers: {
|
|
664
|
-
Accept: "application/vnd.github+json",
|
|
665
|
-
Authorization: `Bearer ${params.token}`,
|
|
666
|
-
"X-GitHub-Api-Version": "2022-11-28",
|
|
667
|
-
...params.body ? { "Content-Type": "application/json" } : void 0
|
|
668
|
-
},
|
|
669
|
-
...params.body ? { body: JSON.stringify(params.body) } : void 0
|
|
670
|
-
});
|
|
671
|
-
const text = await response.text();
|
|
672
|
-
let parsed;
|
|
673
|
-
if (text) {
|
|
674
|
-
try {
|
|
675
|
-
parsed = JSON.parse(text);
|
|
676
|
-
} catch {
|
|
677
|
-
parsed = void 0;
|
|
678
|
-
}
|
|
679
|
-
}
|
|
680
|
-
if (!response.ok) {
|
|
681
|
-
const message = isRecord(parsed) && typeof parsed.message === "string" ? parsed.message : `GitHub API error ${response.status}`;
|
|
682
|
-
throw new GitHubRequestError(message, response.status);
|
|
683
|
-
}
|
|
684
|
-
return parsed;
|
|
685
|
-
}
|
|
686
|
-
function buildOAuthTokenRequest(input) {
|
|
687
|
-
const payload = {
|
|
688
|
-
...input.payload,
|
|
689
|
-
client_id: input.clientId,
|
|
690
|
-
client_secret: input.clientSecret
|
|
691
|
-
};
|
|
692
|
-
return {
|
|
693
|
-
headers: {
|
|
694
|
-
Accept: "application/json",
|
|
695
|
-
"Content-Type": "application/x-www-form-urlencoded"
|
|
696
|
-
},
|
|
697
|
-
body: new URLSearchParams(payload)
|
|
698
|
-
};
|
|
699
|
-
}
|
|
700
|
-
function parseOAuthResponseJson(responseText) {
|
|
701
|
-
if (!responseText.trim()) {
|
|
702
|
-
return void 0;
|
|
703
|
-
}
|
|
704
|
-
try {
|
|
705
|
-
return JSON.parse(responseText);
|
|
706
|
-
} catch {
|
|
707
|
-
return void 0;
|
|
708
|
-
}
|
|
709
|
-
}
|
|
710
|
-
function oauthErrorCode(data) {
|
|
711
|
-
return isRecord(data) && typeof data.error === "string" ? data.error : void 0;
|
|
712
|
-
}
|
|
713
|
-
function isRejectedRefreshError(errorCode) {
|
|
714
|
-
return errorCode === "bad_refresh_token" || errorCode === "invalid_grant";
|
|
715
|
-
}
|
|
716
|
-
function parseOAuthTokenResponse(data, requestedScope) {
|
|
717
|
-
if (!isRecord(data)) {
|
|
718
|
-
throw new Error("OAuth token response is invalid");
|
|
719
|
-
}
|
|
720
|
-
if (typeof data.access_token !== "string" || !data.access_token.trim()) {
|
|
721
|
-
throw new Error("OAuth token response missing access_token");
|
|
722
|
-
}
|
|
723
|
-
if (typeof data.refresh_token !== "string" || !data.refresh_token.trim()) {
|
|
724
|
-
throw new Error("OAuth token response missing refresh_token");
|
|
725
|
-
}
|
|
726
|
-
let scope = normalizeOAuthScope(requestedScope);
|
|
727
|
-
if (data.scope !== void 0) {
|
|
728
|
-
if (typeof data.scope !== "string") {
|
|
729
|
-
throw new Error("OAuth token response returned invalid scope");
|
|
730
|
-
}
|
|
731
|
-
scope = normalizeOAuthScope(data.scope) ?? scope;
|
|
732
|
-
}
|
|
733
|
-
const result = {
|
|
734
|
-
accessToken: data.access_token,
|
|
735
|
-
refreshToken: data.refresh_token,
|
|
736
|
-
...scope ? { scope } : void 0
|
|
737
|
-
};
|
|
738
|
-
if (data.expires_in !== void 0) {
|
|
739
|
-
if (typeof data.expires_in !== "number" || !Number.isFinite(data.expires_in) || data.expires_in <= 0) {
|
|
740
|
-
throw new Error("OAuth token response returned invalid expires_in");
|
|
741
|
-
}
|
|
742
|
-
result.expiresAt = Date.now() + data.expires_in * 1e3;
|
|
743
|
-
}
|
|
744
|
-
if (data.refresh_token_expires_in !== void 0) {
|
|
745
|
-
if (typeof data.refresh_token_expires_in !== "number" || !Number.isFinite(data.refresh_token_expires_in) || data.refresh_token_expires_in <= 0) {
|
|
746
|
-
throw new Error(
|
|
747
|
-
"OAuth token response returned invalid refresh_token_expires_in"
|
|
748
|
-
);
|
|
749
|
-
}
|
|
750
|
-
result.refreshTokenExpiresAt = Date.now() + data.refresh_token_expires_in * 1e3;
|
|
751
|
-
}
|
|
752
|
-
return result;
|
|
753
|
-
}
|
|
754
|
-
async function refreshUserAccessToken(input) {
|
|
755
|
-
const clientId = requireEnv(input.clientIdEnv);
|
|
756
|
-
const clientSecret = requireEnv(input.clientSecretEnv);
|
|
757
|
-
const request = buildOAuthTokenRequest({
|
|
758
|
-
clientId,
|
|
759
|
-
clientSecret,
|
|
760
|
-
payload: {
|
|
761
|
-
grant_type: "refresh_token",
|
|
762
|
-
refresh_token: input.refreshToken
|
|
763
|
-
}
|
|
764
|
-
});
|
|
765
|
-
const response = await fetch("https://github.com/login/oauth/access_token", {
|
|
766
|
-
method: "POST",
|
|
767
|
-
headers: request.headers,
|
|
768
|
-
body: request.body,
|
|
769
|
-
signal: AbortSignal.timeout(USER_REFRESH_TIMEOUT_MS)
|
|
770
|
-
});
|
|
771
|
-
const responseText = await response.text();
|
|
772
|
-
const responseData = parseOAuthResponseJson(responseText);
|
|
773
|
-
const errorCode = oauthErrorCode(responseData);
|
|
774
|
-
if (isRejectedRefreshError(errorCode)) {
|
|
775
|
-
throw new GitHubUserRefreshRejectedError(
|
|
776
|
-
`GitHub user token refresh rejected: ${errorCode}`
|
|
777
|
-
);
|
|
778
|
-
}
|
|
779
|
-
if (!response.ok || errorCode) {
|
|
780
|
-
throw new Error(
|
|
781
|
-
`GitHub user token refresh failed: ${response.status}${errorCode ? ` ${errorCode}` : ""}`
|
|
782
|
-
);
|
|
783
|
-
}
|
|
784
|
-
try {
|
|
785
|
-
return parseOAuthTokenResponse(responseData, input.requestedScope);
|
|
786
|
-
} catch (error) {
|
|
787
|
-
if (error instanceof Error && error.message === "OAuth token response missing access_token") {
|
|
788
|
-
throw new GitHubUserRefreshRejectedError(error.message);
|
|
789
|
-
}
|
|
790
|
-
throw error;
|
|
791
|
-
}
|
|
792
|
-
}
|
|
793
|
-
function leaseExpiry(expiresAt) {
|
|
794
|
-
return expiresAt ? Math.min(expiresAt, Date.now() + MAX_LEASE_MS) : Date.now() + MAX_LEASE_MS;
|
|
795
|
-
}
|
|
796
|
-
function isGitSmartHttpDomain(domain) {
|
|
797
|
-
return domain.toLowerCase() === "github.com";
|
|
798
|
-
}
|
|
799
|
-
function authorizationFor(domain, token) {
|
|
800
|
-
if (isGitSmartHttpDomain(domain)) {
|
|
801
|
-
return `Basic ${Buffer.from(`x-access-token:${token}`).toString("base64")}`;
|
|
802
|
-
}
|
|
803
|
-
return `Bearer ${token}`;
|
|
804
|
-
}
|
|
805
|
-
function createCredentialLease(input) {
|
|
806
|
-
return {
|
|
807
|
-
type: "lease",
|
|
808
|
-
lease: {
|
|
809
|
-
...input.account ? { account: input.account } : void 0,
|
|
810
|
-
...input.authorization ? { authorization: input.authorization } : void 0,
|
|
811
|
-
expiresAt: new Date(input.expiresAtMs).toISOString(),
|
|
812
|
-
headerTransforms: (input.domains ?? (input.authorization ? GITHUB_ASSET_UPLOAD_CREDENTIAL_DOMAINS : GITHUB_CREDENTIAL_DOMAINS)).map((domain) => ({
|
|
813
|
-
domain,
|
|
814
|
-
headers: {
|
|
815
|
-
Authorization: authorizationFor(domain, input.token)
|
|
816
|
-
}
|
|
817
|
-
}))
|
|
818
|
-
}
|
|
819
|
-
};
|
|
820
|
-
}
|
|
821
|
-
function githubUserAuthorization(scope) {
|
|
822
|
-
return {
|
|
823
|
-
type: "oauth",
|
|
824
|
-
provider: "github",
|
|
825
|
-
...scope ? { scope } : void 0
|
|
826
|
-
};
|
|
827
|
-
}
|
|
828
|
-
function credentialNeeded(message, scope, allowAuthorization = true) {
|
|
829
|
-
return {
|
|
830
|
-
type: "needed",
|
|
831
|
-
message,
|
|
832
|
-
...allowAuthorization ? { authorization: githubUserAuthorization(scope) } : void 0
|
|
833
|
-
};
|
|
834
|
-
}
|
|
835
|
-
function credentialUnavailable(message) {
|
|
836
|
-
return {
|
|
837
|
-
type: "unavailable",
|
|
838
|
-
message
|
|
839
|
-
};
|
|
840
|
-
}
|
|
841
|
-
function parseInstallationTokenResponse(data) {
|
|
842
|
-
if (!isRecord(data)) {
|
|
843
|
-
throw new Error("GitHub installation token response is invalid");
|
|
844
|
-
}
|
|
845
|
-
const token = data.token;
|
|
846
|
-
if (typeof token !== "string" || !token.trim()) {
|
|
847
|
-
throw new Error("GitHub installation token response missing token");
|
|
848
|
-
}
|
|
849
|
-
const expiresAt = data.expires_at;
|
|
850
|
-
const expiresAtMs = typeof expiresAt === "string" ? Date.parse(expiresAt) : Number.NaN;
|
|
851
|
-
if (!Number.isFinite(expiresAtMs) || expiresAtMs <= Date.now()) {
|
|
852
|
-
throw new Error(
|
|
853
|
-
"GitHub installation token response returned invalid expires_at"
|
|
854
|
-
);
|
|
855
|
-
}
|
|
856
|
-
return { token, expiresAtMs };
|
|
857
|
-
}
|
|
858
|
-
function readInstallationPermissions(installation) {
|
|
859
|
-
if (!isRecord(installation) || !isRecord(installation.permissions)) {
|
|
860
|
-
throw new Error("GitHub installation response missing permissions");
|
|
861
|
-
}
|
|
862
|
-
return readGrantPermissions(installation.permissions);
|
|
863
|
-
}
|
|
864
|
-
function decodeGitHubPathSegment(value) {
|
|
865
|
-
try {
|
|
866
|
-
const decoded = decodeURIComponent(value).trim();
|
|
867
|
-
return decoded && !decoded.includes("/") ? decoded : void 0;
|
|
868
|
-
} catch {
|
|
869
|
-
return void 0;
|
|
870
|
-
}
|
|
871
|
-
}
|
|
872
|
-
function githubRepositoryFromUrl(upstreamUrl) {
|
|
873
|
-
const segments = upstreamUrl.pathname.split("/").filter(Boolean);
|
|
874
|
-
if (isGitHubApiUrl(upstreamUrl) && segments[0]?.toLowerCase() === "repos") {
|
|
875
|
-
const owner2 = segments[1] ? decodeGitHubPathSegment(segments[1]) : void 0;
|
|
876
|
-
const name2 = segments[2] ? decodeGitHubPathSegment(segments[2]) : void 0;
|
|
877
|
-
return owner2 && name2 ? { owner: owner2, name: name2 } : void 0;
|
|
878
|
-
}
|
|
879
|
-
if (upstreamUrl.hostname.toLowerCase() !== "github.com") {
|
|
880
|
-
return void 0;
|
|
881
|
-
}
|
|
882
|
-
const owner = segments[0] ? decodeGitHubPathSegment(segments[0]) : void 0;
|
|
883
|
-
const rawName = segments[1]?.replace(/\.git$/i, "");
|
|
884
|
-
const name = rawName ? decodeGitHubPathSegment(rawName) : void 0;
|
|
885
|
-
return owner && name ? { owner, name } : void 0;
|
|
886
|
-
}
|
|
887
|
-
async function resolveUserAccount(tokens) {
|
|
888
|
-
const account = await githubRequest("https://api.github.com", "/user", {
|
|
889
|
-
token: tokens.accessToken
|
|
890
|
-
});
|
|
891
|
-
if (!isRecord(account)) {
|
|
892
|
-
throw new Error("GitHub user response is invalid");
|
|
893
|
-
}
|
|
894
|
-
const id = account.id;
|
|
895
|
-
const login = account.login;
|
|
896
|
-
if (typeof id !== "number" && typeof id !== "string" || typeof login !== "string" || !login.trim()) {
|
|
897
|
-
throw new Error("GitHub user response missing id or login");
|
|
898
|
-
}
|
|
899
|
-
const url = typeof account.html_url === "string" ? account.html_url : void 0;
|
|
900
|
-
return {
|
|
901
|
-
handle: login.trim(),
|
|
902
|
-
id: String(id),
|
|
903
|
-
label: login.trim(),
|
|
904
|
-
...url ? { url } : void 0
|
|
905
|
-
};
|
|
906
|
-
}
|
|
907
|
-
async function listVerifiedUserEmails(tokens) {
|
|
908
|
-
let emails;
|
|
909
|
-
try {
|
|
910
|
-
emails = await githubRequest("https://api.github.com", "/user/emails", {
|
|
911
|
-
token: tokens.accessToken
|
|
912
|
-
});
|
|
913
|
-
} catch (error) {
|
|
914
|
-
if (error instanceof GitHubRequestError && (error.status === 403 || error.status === 404)) {
|
|
915
|
-
return [];
|
|
916
|
-
}
|
|
917
|
-
throw error;
|
|
918
|
-
}
|
|
919
|
-
if (!Array.isArray(emails)) {
|
|
920
|
-
throw new Error("GitHub user emails response is invalid");
|
|
921
|
-
}
|
|
922
|
-
return emails.flatMap(
|
|
923
|
-
(entry) => isRecord(entry) && entry.verified === true && typeof entry.email === "string" && entry.email.trim() ? [entry.email.trim()] : []
|
|
924
|
-
);
|
|
925
|
-
}
|
|
926
|
-
async function tokensWithAccount(tokenSlot, stored, scope) {
|
|
927
|
-
if (stored.account) {
|
|
928
|
-
return { ok: true, tokens: stored };
|
|
929
|
-
}
|
|
930
|
-
let account;
|
|
931
|
-
try {
|
|
932
|
-
account = await resolveUserAccount(stored);
|
|
933
|
-
} catch (error) {
|
|
934
|
-
if (error instanceof GitHubRequestError && (error.status === 401 || error.status === 403)) {
|
|
935
|
-
return {
|
|
936
|
-
ok: false,
|
|
937
|
-
result: credentialNeeded(
|
|
938
|
-
"Your GitHub authorization needs to be refreshed.",
|
|
939
|
-
scope
|
|
940
|
-
)
|
|
941
|
-
};
|
|
942
|
-
}
|
|
943
|
-
throw error;
|
|
944
|
-
}
|
|
945
|
-
const updated = { ...stored, account };
|
|
946
|
-
await tokenSlot.set(updated);
|
|
947
|
-
return { ok: true, tokens: updated };
|
|
948
|
-
}
|
|
949
|
-
function shouldRefreshUserToken(stored, now = Date.now()) {
|
|
950
|
-
return stored.expiresAt !== void 0 && stored.expiresAt - now < REFRESH_BUFFER_MS;
|
|
951
|
-
}
|
|
952
|
-
function canUseStoredUserToken(stored) {
|
|
953
|
-
return stored.expiresAt === void 0 || stored.expiresAt > Date.now() && !shouldRefreshUserToken(stored);
|
|
954
|
-
}
|
|
955
|
-
async function refreshUserTokensWithLock(tokenSlot, scope, options) {
|
|
956
|
-
return await tokenSlot.withRefresh(async () => {
|
|
957
|
-
const latest = await tokenSlot.get();
|
|
958
|
-
if (!latest) {
|
|
959
|
-
return {
|
|
960
|
-
ok: false,
|
|
961
|
-
result: credentialNeeded("Connect your GitHub account.", scope)
|
|
962
|
-
};
|
|
963
|
-
}
|
|
964
|
-
if (!hasRequiredOAuthScope(latest.scope, scope)) {
|
|
965
|
-
return {
|
|
966
|
-
ok: false,
|
|
967
|
-
result: credentialNeeded(
|
|
968
|
-
"Your GitHub authorization needs to be refreshed.",
|
|
969
|
-
scope
|
|
970
|
-
)
|
|
971
|
-
};
|
|
972
|
-
}
|
|
973
|
-
if (canUseStoredUserToken(latest)) {
|
|
974
|
-
return { ok: true, tokens: latest };
|
|
975
|
-
}
|
|
976
|
-
let refreshed;
|
|
977
|
-
try {
|
|
978
|
-
refreshed = await refreshUserAccessToken({
|
|
979
|
-
clientIdEnv: options.clientIdEnv,
|
|
980
|
-
clientSecretEnv: options.clientSecretEnv,
|
|
981
|
-
refreshToken: latest.refreshToken,
|
|
982
|
-
requestedScope: latest.scope ?? scope
|
|
983
|
-
});
|
|
984
|
-
} catch (error) {
|
|
985
|
-
if (!(error instanceof GitHubUserRefreshRejectedError)) {
|
|
986
|
-
throw error;
|
|
987
|
-
}
|
|
988
|
-
return {
|
|
989
|
-
ok: false,
|
|
990
|
-
result: credentialNeeded(
|
|
991
|
-
"Your GitHub authorization has expired.",
|
|
992
|
-
scope
|
|
993
|
-
)
|
|
994
|
-
};
|
|
995
|
-
}
|
|
996
|
-
if (!hasRequiredOAuthScope(refreshed.scope, scope)) {
|
|
997
|
-
return {
|
|
998
|
-
ok: false,
|
|
999
|
-
result: credentialNeeded(
|
|
1000
|
-
"Your GitHub authorization needs to be refreshed.",
|
|
1001
|
-
scope
|
|
1002
|
-
)
|
|
1003
|
-
};
|
|
1004
|
-
}
|
|
1005
|
-
const refreshedTokens = {
|
|
1006
|
-
...latest.refreshTokenExpiresAt ? { refreshTokenExpiresAt: latest.refreshTokenExpiresAt } : void 0,
|
|
1007
|
-
...refreshed,
|
|
1008
|
-
...latest.account ? { account: latest.account } : void 0
|
|
1009
|
-
};
|
|
1010
|
-
await tokenSlot.set(refreshedTokens);
|
|
1011
|
-
return { ok: true, tokens: refreshedTokens };
|
|
1012
|
-
});
|
|
1013
|
-
}
|
|
1014
|
-
async function issueUserCredential(ctx, options) {
|
|
1015
|
-
const scope = options.userScope;
|
|
1016
|
-
const tokenSlot = ctx.tokens.currentUser ?? ctx.tokens.credentialSubject;
|
|
1017
|
-
if (!tokenSlot) {
|
|
1018
|
-
return credentialNeeded(
|
|
1019
|
-
"GitHub write access requires a current user or delegated user credential subject.",
|
|
1020
|
-
scope,
|
|
1021
|
-
false
|
|
1022
|
-
);
|
|
1023
|
-
}
|
|
1024
|
-
const stored = await tokenSlot.get();
|
|
1025
|
-
if (!stored) {
|
|
1026
|
-
return credentialNeeded(
|
|
1027
|
-
"GitHub write access requires user authorization.",
|
|
1028
|
-
scope
|
|
1029
|
-
);
|
|
1030
|
-
}
|
|
1031
|
-
if (!hasRequiredOAuthScope(stored.scope, scope)) {
|
|
1032
|
-
return credentialNeeded(
|
|
1033
|
-
"Your GitHub authorization needs to be refreshed.",
|
|
1034
|
-
scope
|
|
1035
|
-
);
|
|
1036
|
-
}
|
|
1037
|
-
const now = Date.now();
|
|
1038
|
-
if (stored.expiresAt !== void 0 && stored.expiresAt - now < REFRESH_BUFFER_MS) {
|
|
1039
|
-
const refreshResult = await refreshUserTokensWithLock(
|
|
1040
|
-
tokenSlot,
|
|
1041
|
-
scope,
|
|
1042
|
-
options
|
|
1043
|
-
);
|
|
1044
|
-
if (!refreshResult.ok) {
|
|
1045
|
-
return refreshResult.result;
|
|
1046
|
-
}
|
|
1047
|
-
const withAccount = await tokensWithAccount(
|
|
1048
|
-
tokenSlot,
|
|
1049
|
-
refreshResult.tokens,
|
|
1050
|
-
scope
|
|
1051
|
-
);
|
|
1052
|
-
if (!withAccount.ok) {
|
|
1053
|
-
return withAccount.result;
|
|
1054
|
-
}
|
|
1055
|
-
return createCredentialLease({
|
|
1056
|
-
account: withAccount.tokens.account,
|
|
1057
|
-
token: withAccount.tokens.accessToken,
|
|
1058
|
-
expiresAtMs: leaseExpiry(withAccount.tokens.expiresAt),
|
|
1059
|
-
authorization: githubUserAuthorization(scope)
|
|
1060
|
-
});
|
|
1061
|
-
}
|
|
1062
|
-
if (stored.expiresAt === void 0 || stored.expiresAt > Date.now()) {
|
|
1063
|
-
const withAccount = await tokensWithAccount(tokenSlot, stored, scope);
|
|
1064
|
-
if (!withAccount.ok) {
|
|
1065
|
-
return withAccount.result;
|
|
1066
|
-
}
|
|
1067
|
-
return createCredentialLease({
|
|
1068
|
-
account: withAccount.tokens.account,
|
|
1069
|
-
token: withAccount.tokens.accessToken,
|
|
1070
|
-
expiresAtMs: leaseExpiry(withAccount.tokens.expiresAt),
|
|
1071
|
-
authorization: githubUserAuthorization(scope)
|
|
1072
|
-
});
|
|
1073
|
-
}
|
|
1074
|
-
return credentialNeeded("Your GitHub authorization has expired.", scope);
|
|
1075
|
-
}
|
|
1076
|
-
async function issueInstallationToken(options) {
|
|
1077
|
-
const appId = requireEnv(options.appIdEnv);
|
|
1078
|
-
const installationIdRaw = requireEnv(options.installationIdEnv);
|
|
1079
|
-
const installationId = Number(installationIdRaw);
|
|
1080
|
-
if (!Number.isSafeInteger(installationId) || installationId <= 0) {
|
|
1081
|
-
throw new GitHubPluginSetupError(`Invalid ${options.installationIdEnv}`);
|
|
1082
|
-
}
|
|
1083
|
-
const appJwt = createAppJwt(appId, options.privateKeyEnv);
|
|
1084
|
-
const permissions = "permissions" in options ? options.permissions : typeof options.loadPermissions === "function" ? await options.loadPermissions({ appJwt, installationId }) : void 0;
|
|
1085
|
-
const repositories = "repositories" in options ? options.repositories : void 0;
|
|
1086
|
-
const repositoryIds = "repositoryIds" in options ? options.repositoryIds : void 0;
|
|
1087
|
-
const body = {
|
|
1088
|
-
...permissions ? { permissions } : void 0,
|
|
1089
|
-
...repositories ? { repositories } : void 0,
|
|
1090
|
-
...repositoryIds ? { repository_ids: repositoryIds } : void 0
|
|
1091
|
-
};
|
|
1092
|
-
const accessTokenResponse = await githubRequest(
|
|
1093
|
-
"https://api.github.com",
|
|
1094
|
-
`/app/installations/${installationId}/access_tokens`,
|
|
1095
|
-
{
|
|
1096
|
-
method: "POST",
|
|
1097
|
-
token: appJwt,
|
|
1098
|
-
body
|
|
1099
|
-
}
|
|
1100
|
-
);
|
|
1101
|
-
const parsedToken = parseInstallationTokenResponse(accessTokenResponse);
|
|
1102
|
-
return {
|
|
1103
|
-
expiresAtMs: Math.min(parsedToken.expiresAtMs, Date.now() + MAX_LEASE_MS),
|
|
1104
|
-
token: parsedToken.token
|
|
1105
|
-
};
|
|
1106
|
-
}
|
|
1107
|
-
async function issueInstallationCredential(options) {
|
|
1108
|
-
const token = await issueInstallationToken(options);
|
|
1109
|
-
return createCredentialLease({
|
|
1110
|
-
token: token.token,
|
|
1111
|
-
expiresAtMs: token.expiresAtMs
|
|
1112
|
-
});
|
|
1113
|
-
}
|
|
1114
|
-
async function issueGitHubCredential(ctx, options) {
|
|
1115
|
-
const installation = {
|
|
1116
|
-
appIdEnv: options.appIdEnv,
|
|
1117
|
-
installationIdEnv: options.installationIdEnv,
|
|
1118
|
-
privateKeyEnv: options.privateKeyEnv
|
|
1119
|
-
};
|
|
1120
|
-
try {
|
|
1121
|
-
if (ctx.grant.name === "installation-read") {
|
|
1122
|
-
return await issueInstallationCredential({
|
|
1123
|
-
...installation,
|
|
1124
|
-
...options.readPermissions
|
|
1125
|
-
});
|
|
1126
|
-
}
|
|
1127
|
-
if (ctx.grant.name === "installation-write") {
|
|
1128
|
-
return await issueInstallationCredential(installation);
|
|
1129
|
-
}
|
|
1130
|
-
if (USER_TOKEN_GRANTS.has(ctx.grant.name)) {
|
|
1131
|
-
return await issueUserCredential(ctx, options);
|
|
1132
|
-
}
|
|
1133
|
-
} catch (error) {
|
|
1134
|
-
if (error instanceof GitHubPluginSetupError) {
|
|
1135
|
-
return credentialUnavailable(error.message);
|
|
1136
|
-
}
|
|
1137
|
-
throw error;
|
|
1138
|
-
}
|
|
1139
|
-
throw new Error(
|
|
1140
|
-
`GitHub plugin cannot issue unknown grant "${ctx.grant.name}".`
|
|
1141
|
-
);
|
|
1142
|
-
}
|
|
1143
|
-
function createPermissionCache() {
|
|
1144
|
-
let cached;
|
|
1145
|
-
let pending;
|
|
1146
|
-
return async ({ appJwt, installationId }) => {
|
|
1147
|
-
if (cached && cached.expiresAtMs > Date.now()) {
|
|
1148
|
-
return cached.permissions;
|
|
1149
|
-
}
|
|
1150
|
-
pending ??= githubRequest(
|
|
1151
|
-
"https://api.github.com",
|
|
1152
|
-
`/app/installations/${installationId}`,
|
|
1153
|
-
{ token: appJwt }
|
|
1154
|
-
).then((installation) => {
|
|
1155
|
-
const permissions = readInstallationPermissions(installation);
|
|
1156
|
-
cached = {
|
|
1157
|
-
expiresAtMs: Date.now() + MAX_LEASE_MS,
|
|
1158
|
-
permissions
|
|
1159
|
-
};
|
|
1160
|
-
return permissions;
|
|
1161
|
-
}).finally(() => {
|
|
1162
|
-
pending = void 0;
|
|
1163
|
-
});
|
|
1164
|
-
return await pending;
|
|
1165
|
-
};
|
|
1166
|
-
}
|
|
1167
|
-
|
|
1168
|
-
// src/webhooks/event-targets.ts
|
|
1169
|
-
import * as z3 from "zod";
|
|
1170
|
-
var gitHubSenderSchema = z3.object({
|
|
1171
|
-
id: z3.number(),
|
|
1172
|
-
login: z3.string().min(1)
|
|
1173
|
-
});
|
|
1174
|
-
function gitHubSenderData(sender) {
|
|
1175
|
-
return { senderId: sender?.id, senderLogin: sender?.login };
|
|
1176
|
-
}
|
|
1177
|
-
function gitHubDoneBy(login) {
|
|
1178
|
-
const handle = login?.trim();
|
|
1179
|
-
return handle ? { doneBy: { provider: "github", handle } } : void 0;
|
|
1180
|
-
}
|
|
1181
|
-
function nonBlank(value) {
|
|
1182
|
-
return value?.trim() || void 0;
|
|
1183
|
-
}
|
|
1184
|
-
function titleAndBodyText(item) {
|
|
1185
|
-
const parts = [
|
|
1186
|
-
item.title ? `Title: ${item.title}` : void 0,
|
|
1187
|
-
nonBlank(item.body)
|
|
1188
|
-
].filter((part) => part !== void 0);
|
|
1189
|
-
return parts.length > 0 ? parts.join("\n\n") : void 0;
|
|
1190
|
-
}
|
|
1191
|
-
function providerTime(value) {
|
|
1192
|
-
if (!value) return void 0;
|
|
1193
|
-
const parsed = Date.parse(value);
|
|
1194
|
-
return Number.isFinite(parsed) ? parsed : void 0;
|
|
1195
|
-
}
|
|
1196
|
-
function definedValues(data) {
|
|
1197
|
-
return Object.fromEntries(
|
|
1198
|
-
Object.entries(data).filter(([, value]) => value !== void 0)
|
|
1199
|
-
);
|
|
1200
|
-
}
|
|
1201
|
-
function gitHubEventKey(deliveryId, eventType) {
|
|
1202
|
-
return `github:${deliveryId}:${eventType}`;
|
|
1203
|
-
}
|
|
1204
|
-
function pullRequestTargets(event, repo) {
|
|
1205
|
-
const { terminal: _terminal, ...repositoryEvent } = event;
|
|
1206
|
-
return [
|
|
1207
|
-
event,
|
|
1208
|
-
{
|
|
1209
|
-
...repositoryEvent,
|
|
1210
|
-
identifier: gitHubRepositoryResource({ repo }).identifier
|
|
1211
|
-
}
|
|
1212
|
-
];
|
|
1213
|
-
}
|
|
1214
|
-
|
|
1215
|
-
// src/webhooks/fail-open-log.ts
|
|
1216
|
-
function logFailOpenFailure(log, message, error, metadata) {
|
|
1217
|
-
const attributes = {
|
|
1218
|
-
...metadata,
|
|
1219
|
-
errorType: error instanceof Error ? error.name : "UnknownError"
|
|
1220
|
-
};
|
|
1221
|
-
if (error instanceof GitHubRequestError && error.status >= 500) {
|
|
1222
|
-
log?.warn(message, {
|
|
1223
|
-
...attributes,
|
|
1224
|
-
"http.response.status_code": error.status
|
|
1225
|
-
});
|
|
1226
|
-
return;
|
|
1227
|
-
}
|
|
1228
|
-
log?.error(message, attributes);
|
|
1229
|
-
}
|
|
1230
|
-
|
|
1231
|
-
// src/webhooks/check-suite.ts
|
|
1232
|
-
var CHECK_SUITE_PULL_REQUEST_MATCH_KEYS = /* @__PURE__ */ new Set([
|
|
1233
|
-
"isDraft",
|
|
1234
|
-
"openerEmail",
|
|
1235
|
-
"openerRole",
|
|
1236
|
-
"openerUsername"
|
|
1237
|
-
]);
|
|
1238
|
-
var LATE_MATCH_KEYS = /* @__PURE__ */ new Set([
|
|
1239
|
-
...CHECK_SUITE_PULL_REQUEST_MATCH_KEYS,
|
|
1240
|
-
"authors"
|
|
1241
|
-
]);
|
|
1242
|
-
var checkRunRepoRefSchema = z4.object({
|
|
1243
|
-
id: z4.number(),
|
|
1244
|
-
name: z4.string().min(1),
|
|
1245
|
-
url: z4.string().min(1)
|
|
1246
|
-
}).passthrough();
|
|
1247
|
-
var checkRunPullRequestSideSchema = z4.object({
|
|
1248
|
-
ref: z4.string().optional(),
|
|
1249
|
-
repo: checkRunRepoRefSchema,
|
|
1250
|
-
sha: z4.string().optional()
|
|
1251
|
-
}).passthrough();
|
|
1252
|
-
var checkRunPullRequestSchema = z4.object({
|
|
1253
|
-
base: checkRunPullRequestSideSchema,
|
|
1254
|
-
head: checkRunPullRequestSideSchema.optional(),
|
|
1255
|
-
id: z4.number().optional(),
|
|
1256
|
-
number: z4.number(),
|
|
1257
|
-
url: z4.string().optional()
|
|
1258
|
-
}).passthrough();
|
|
1259
|
-
var repositorySchema = z4.object({
|
|
1260
|
-
full_name: z4.string().min(1),
|
|
1261
|
-
id: z4.number(),
|
|
1262
|
-
name: z4.string().min(1),
|
|
1263
|
-
owner: z4.object({ login: z4.string().min(1) }).passthrough()
|
|
1264
|
-
}).passthrough();
|
|
1265
|
-
var checkSuiteWebhookSchema = z4.object({
|
|
1266
|
-
action: z4.string(),
|
|
1267
|
-
check_suite: z4.object({
|
|
1268
|
-
app: z4.object({
|
|
1269
|
-
name: z4.string().optional().nullable(),
|
|
1270
|
-
slug: z4.string().optional().nullable()
|
|
1271
|
-
}).optional().nullable(),
|
|
1272
|
-
conclusion: z4.string().optional().nullable(),
|
|
1273
|
-
/** Branch name for the suite head commit when GitHub sends it. */
|
|
1274
|
-
head_branch: z4.string().optional().nullable(),
|
|
1275
|
-
head_sha: z4.string().optional(),
|
|
1276
|
-
id: z4.number().optional(),
|
|
1277
|
-
latest_check_runs_count: z4.number().optional().nullable(),
|
|
1278
|
-
pull_requests: z4.array(checkRunPullRequestSchema)
|
|
1279
|
-
}),
|
|
1280
|
-
repository: repositorySchema
|
|
1281
|
-
});
|
|
1282
|
-
var checkSuitesListResponseSchema = z4.object({
|
|
1283
|
-
check_suites: z4.array(
|
|
1284
|
-
z4.object({
|
|
1285
|
-
app: z4.object({ slug: z4.string().nullable().optional() }).nullable().optional(),
|
|
1286
|
-
conclusion: z4.string().nullable().optional(),
|
|
1287
|
-
id: z4.number(),
|
|
1288
|
-
latest_check_runs_count: z4.number().nullable().optional(),
|
|
1289
|
-
status: z4.string().nullable().optional()
|
|
1290
|
-
})
|
|
1291
|
-
),
|
|
1292
|
-
total_count: z4.number()
|
|
1293
|
-
});
|
|
1294
|
-
var checkRunsListResponseSchema = z4.object({
|
|
1295
|
-
check_runs: z4.array(z4.unknown()).optional()
|
|
1296
|
-
}).passthrough();
|
|
1297
|
-
function isCheckSuiteRepositoryPullRequest(pullRequest, repositoryId) {
|
|
1298
|
-
return pullRequest.base.repo.id === repositoryId;
|
|
1299
|
-
}
|
|
1300
|
-
var CHECKS_PASSED = "pull_request.checks.passed";
|
|
1301
|
-
var PASSING_SUITE_CONCLUSIONS = /* @__PURE__ */ new Set(["neutral", "skipped", "success"]);
|
|
1302
|
-
var MAX_CHECK_SUITE_PAGES = 10;
|
|
1303
|
-
function checkSuiteEventType(conclusion) {
|
|
1304
|
-
if (conclusion === "failure" || conclusion === "timed_out") {
|
|
1305
|
-
return "pull_request.checks.failed";
|
|
1306
|
-
}
|
|
1307
|
-
if (conclusion === "success") {
|
|
1308
|
-
return "pull_request.checks.recovered";
|
|
1309
|
-
}
|
|
1310
|
-
return void 0;
|
|
1311
|
-
}
|
|
1312
|
-
function checkSuiteEventTypes(conclusion) {
|
|
1313
|
-
const eventType = checkSuiteEventType(conclusion);
|
|
1314
|
-
const eventTypes = eventType ? [eventType] : [];
|
|
1315
|
-
if (PASSING_SUITE_CONCLUSIONS.has(conclusion ?? "")) {
|
|
1316
|
-
eventTypes.push(CHECKS_PASSED);
|
|
1317
|
-
}
|
|
1318
|
-
return eventTypes;
|
|
1319
|
-
}
|
|
1320
|
-
function isGitHubNotFoundError(error) {
|
|
1321
|
-
return error instanceof Error && error.name === "GitHubRequestError" && "status" in error && error.status === 404;
|
|
1322
|
-
}
|
|
1323
|
-
var FAILING_CHECK_CONCLUSIONS = /* @__PURE__ */ new Set([
|
|
1324
|
-
"failure",
|
|
1325
|
-
"timed_out",
|
|
1326
|
-
"cancelled",
|
|
1327
|
-
"action_required",
|
|
1328
|
-
"startup_failure"
|
|
1329
|
-
]);
|
|
1330
|
-
function buildCheckSuiteUrl(args) {
|
|
1331
|
-
return `https://github.com/${args.repo}/commit/${args.headSha}/checks?check_suite_id=${args.checkSuiteId}`;
|
|
1332
|
-
}
|
|
1333
|
-
function oneLineLabel(value, maxLength = 80) {
|
|
1334
|
-
return value.replace(/[\r\n]+/g, " ").replace(/\s+/g, " ").trim().slice(0, maxLength);
|
|
1335
|
-
}
|
|
1336
|
-
function checkSuiteHeadBranch(value) {
|
|
1337
|
-
const branch = value?.trim();
|
|
1338
|
-
return branch ? branch : void 0;
|
|
1339
|
-
}
|
|
1340
|
-
var checkSuiteEventDataSchema = z4.object({
|
|
1341
|
-
repo: gitHubRepoDataSchema,
|
|
1342
|
-
pullRequest: z4.number().optional(),
|
|
1343
|
-
commitSha: gitHubCommitShaDataSchema.optional(),
|
|
1344
|
-
suiteConclusion: z4.string().min(1),
|
|
1345
|
-
isDraft: z4.boolean().optional(),
|
|
1346
|
-
openerUsername: z4.string().min(1).optional(),
|
|
1347
|
-
openerEmail: z4.string().min(1).optional(),
|
|
1348
|
-
headBranch: z4.string().min(1).optional(),
|
|
1349
|
-
checkSuiteId: z4.number().optional(),
|
|
1350
|
-
checkSuiteUrl: z4.string().min(1).optional(),
|
|
1351
|
-
appName: z4.string().min(1).optional(),
|
|
1352
|
-
latestCheckRunsCount: z4.number().optional(),
|
|
1353
|
-
// Only system-set handles. Check names come from workflow YAML, so they
|
|
1354
|
-
// go in the untrusted text.
|
|
1355
|
-
failingChecks: z4.array(
|
|
1356
|
-
z4.object({
|
|
1357
|
-
conclusion: z4.string(),
|
|
1358
|
-
htmlUrl: z4.string().optional(),
|
|
1359
|
-
checkRunId: z4.number()
|
|
1360
|
-
}).strict()
|
|
1361
|
-
).optional()
|
|
1362
|
-
}).strict();
|
|
1363
|
-
function buildCheckSuiteEvent(args) {
|
|
1364
|
-
const pullRequestNumber = args.pullRequestNumber;
|
|
1365
|
-
const resource = pullRequestNumber === void 0 ? gitHubRepositoryResource({ repo: args.repo }) : gitHubPullRequestResource({
|
|
1366
|
-
number: pullRequestNumber,
|
|
1367
|
-
repo: args.repo
|
|
1368
|
-
});
|
|
1369
|
-
const shortSha = args.headSha?.slice(0, 12);
|
|
1370
|
-
const failingChecks = (args.failingChecks ?? []).slice(0, 12);
|
|
1371
|
-
const failingCount = failingChecks.length;
|
|
1372
|
-
const branchSuffix = args.headBranch ? ` on ${args.headBranch}` : "";
|
|
1373
|
-
const incidents = args.eventType === "pull_request.checks.failed" ? args.githubIncidents ?? [] : [];
|
|
1374
|
-
const incidentSuffix = incidents.length > 0 ? ` githubstatus.com lists ${incidents.length} open or recent GitHub incident${incidents.length === 1 ? "" : "s"}.` : "";
|
|
1375
|
-
const shaSuffix = shortSha ? ` for ${shortSha}` : "";
|
|
1376
|
-
const trustedSummary = args.eventType === "pull_request.checks.failed" ? `${resource.label} checks failed${failingCount > 0 ? ` (${failingCount})` : ""}${shaSuffix}${branchSuffix}.${incidentSuffix}` : args.eventType === CHECKS_PASSED ? `${resource.label} all check suites passed${shaSuffix}${branchSuffix}.` : `${resource.label} check suite recovered${shaSuffix}${branchSuffix}.`;
|
|
1377
|
-
const data = checkSuiteEventDataSchema.parse(
|
|
1378
|
-
definedValues({
|
|
1379
|
-
repo: args.repo,
|
|
1380
|
-
pullRequest: pullRequestNumber,
|
|
1381
|
-
// A short SHA counts as missing, so a commitSha filter does not match.
|
|
1382
|
-
commitSha: gitHubCommitShaDataSchema.safeParse(args.headSha).data,
|
|
1383
|
-
suiteConclusion: args.suiteConclusion,
|
|
1384
|
-
isDraft: args.isDraft,
|
|
1385
|
-
openerUsername: args.openerUsername,
|
|
1386
|
-
openerEmail: args.openerEmail,
|
|
1387
|
-
headBranch: args.headBranch,
|
|
1388
|
-
checkSuiteId: args.checkSuiteId,
|
|
1389
|
-
checkSuiteUrl: args.checkSuiteId !== void 0 && args.headSha ? buildCheckSuiteUrl({
|
|
1390
|
-
checkSuiteId: args.checkSuiteId,
|
|
1391
|
-
headSha: args.headSha,
|
|
1392
|
-
repo: args.repo
|
|
1393
|
-
}) : void 0,
|
|
1394
|
-
appName: args.appName ? oneLineLabel(args.appName, 120) : void 0,
|
|
1395
|
-
latestCheckRunsCount: args.latestCheckRunsCount,
|
|
1396
|
-
failingChecks: args.eventType === "pull_request.checks.failed" && failingCount > 0 ? failingChecks.map((check) => ({
|
|
1397
|
-
conclusion: check.conclusion,
|
|
1398
|
-
...check.htmlUrl ? { htmlUrl: check.htmlUrl } : void 0,
|
|
1399
|
-
checkRunId: check.checkRunId
|
|
1400
|
-
})) : void 0
|
|
1401
|
-
})
|
|
1402
|
-
);
|
|
1403
|
-
const untrustedParts = args.eventType === "pull_request.checks.failed" ? failingChecks.map((check) => {
|
|
1404
|
-
const name = oneLineLabel(check.name);
|
|
1405
|
-
if (!name) return void 0;
|
|
1406
|
-
return check.htmlUrl ? `${name}: ${check.htmlUrl}` : name;
|
|
1407
|
-
}).filter((part) => part !== void 0) : [];
|
|
1408
|
-
const untrustedSections = [
|
|
1409
|
-
...untrustedParts.length > 0 ? [
|
|
1410
|
-
[`Failed checks:`, ...untrustedParts.map((part) => `- ${part}`)].join(
|
|
1411
|
-
"\n"
|
|
1412
|
-
)
|
|
1413
|
-
] : [],
|
|
1414
|
-
...incidents.length > 0 ? [
|
|
1415
|
-
[
|
|
1416
|
-
"GitHub status incidents:",
|
|
1417
|
-
...incidents.map(
|
|
1418
|
-
(incident) => `- ${oneLineLabel(incident.name, 160)} (${incident.status}; ${incident.components.join(", ") || "no components"}; started ${incident.startedAt}${incident.resolvedAt ? `, resolved ${incident.resolvedAt}` : ""}): ${incident.url}`
|
|
1419
|
-
)
|
|
1420
|
-
].join("\n")
|
|
1421
|
-
] : []
|
|
1422
|
-
];
|
|
1423
|
-
const untrustedText = untrustedSections.length > 0 ? untrustedSections.join("\n\n") : void 0;
|
|
1424
|
-
return {
|
|
1425
|
-
// Each suite that finishes last can see every suite green, so name the
|
|
1426
|
-
// head commit, not the delivery. Core then delivers checks.passed once
|
|
1427
|
-
// per head commit to each Watch and event automation.
|
|
1428
|
-
eventKey: args.eventType === CHECKS_PASSED && args.headSha ? `github:${CHECKS_PASSED}:${resource.identifier}:${args.headSha.toLowerCase()}` : gitHubEventKey(
|
|
1429
|
-
args.deliveryId,
|
|
1430
|
-
pullRequestNumber === void 0 ? args.eventType : `${args.eventType}:${pullRequestNumber}`
|
|
1431
|
-
),
|
|
1432
|
-
eventType: args.eventType,
|
|
1433
|
-
occurredAtMs: Date.now(),
|
|
1434
|
-
identifier: resource.identifier,
|
|
1435
|
-
trustedSummary,
|
|
1436
|
-
data,
|
|
1437
|
-
...untrustedText ? { untrustedText } : void 0
|
|
1438
|
-
};
|
|
1439
|
-
}
|
|
1440
|
-
function selectFailingChecks(checkRuns, options) {
|
|
1441
|
-
if (!Array.isArray(checkRuns)) return [];
|
|
1442
|
-
const failing = [];
|
|
1443
|
-
for (const run of checkRuns) {
|
|
1444
|
-
if (!run || typeof run !== "object" || Array.isArray(run)) continue;
|
|
1445
|
-
const record2 = run;
|
|
1446
|
-
if (options?.checkSuiteId !== void 0) {
|
|
1447
|
-
const suite = record2.check_suite;
|
|
1448
|
-
const suiteId = suite && typeof suite === "object" && !Array.isArray(suite) && typeof suite.id === "number" ? suite.id : void 0;
|
|
1449
|
-
if (suiteId !== void 0 && suiteId !== options.checkSuiteId) continue;
|
|
1450
|
-
}
|
|
1451
|
-
const conclusion = typeof record2.conclusion === "string" ? record2.conclusion : void 0;
|
|
1452
|
-
const name = typeof record2.name === "string" ? record2.name.trim() : "";
|
|
1453
|
-
const checkRunId = typeof record2.id === "number" && Number.isSafeInteger(record2.id) ? record2.id : void 0;
|
|
1454
|
-
if (!conclusion || !FAILING_CHECK_CONCLUSIONS.has(conclusion)) continue;
|
|
1455
|
-
if (!name || checkRunId === void 0) continue;
|
|
1456
|
-
const htmlUrl = typeof record2.html_url === "string" && record2.html_url.length > 0 ? record2.html_url : void 0;
|
|
1457
|
-
failing.push({
|
|
1458
|
-
checkRunId,
|
|
1459
|
-
conclusion,
|
|
1460
|
-
...htmlUrl ? { htmlUrl } : void 0,
|
|
1461
|
-
name
|
|
1462
|
-
});
|
|
1463
|
-
}
|
|
1464
|
-
return failing.slice(0, 12);
|
|
1465
|
-
}
|
|
1466
|
-
function normalizeCheckSuiteEvents(deliveryId, body, options) {
|
|
1467
|
-
const parsed = checkSuiteWebhookSchema.safeParse(body);
|
|
1468
|
-
if (!parsed.success || parsed.data.action !== "completed") return [];
|
|
1469
|
-
const conclusion = parsed.data.check_suite.conclusion;
|
|
1470
|
-
const eventType = checkSuiteEventType(conclusion);
|
|
1471
|
-
const eventTypes = checkSuiteEventTypes(conclusion).filter(
|
|
1472
|
-
(type) => type !== CHECKS_PASSED || options?.allCheckSuitesPassed
|
|
1473
|
-
);
|
|
1474
|
-
if (eventTypes.length === 0 || typeof conclusion !== "string") return [];
|
|
1475
|
-
const suite = parsed.data.check_suite;
|
|
1476
|
-
const appName = suite.app?.name?.trim() || suite.app?.slug?.trim() || void 0;
|
|
1477
|
-
const headBranch = checkSuiteHeadBranch(suite.head_branch);
|
|
1478
|
-
const headSha = typeof suite.head_sha === "string" && /^[0-9a-f]{7,40}$/i.test(suite.head_sha) ? suite.head_sha : void 0;
|
|
1479
|
-
const repository = parsed.data.repository;
|
|
1480
|
-
const repo = repository.full_name;
|
|
1481
|
-
const shared = {
|
|
1482
|
-
appName,
|
|
1483
|
-
checkSuiteId: suite.id,
|
|
1484
|
-
deliveryId,
|
|
1485
|
-
failingChecks: eventType === "pull_request.checks.failed" ? options?.failingChecks : void 0,
|
|
1486
|
-
githubIncidents: eventType === "pull_request.checks.failed" ? options?.githubIncidents : void 0,
|
|
1487
|
-
headSha,
|
|
1488
|
-
latestCheckRunsCount: typeof suite.latest_check_runs_count === "number" ? suite.latest_check_runs_count : void 0,
|
|
1489
|
-
repo,
|
|
1490
|
-
suiteConclusion: conclusion
|
|
1491
|
-
};
|
|
1492
|
-
const sameRepoPullRequests = suite.pull_requests.filter(
|
|
1493
|
-
(pullRequest) => isCheckSuiteRepositoryPullRequest(pullRequest, repository.id)
|
|
1494
|
-
);
|
|
1495
|
-
if (sameRepoPullRequests.length === 0) {
|
|
1496
|
-
return eventTypes.map(
|
|
1497
|
-
(type) => buildCheckSuiteEvent({
|
|
1498
|
-
...shared,
|
|
1499
|
-
eventType: type,
|
|
1500
|
-
...headBranch ? { headBranch } : void 0
|
|
1501
|
-
})
|
|
1502
|
-
);
|
|
1503
|
-
}
|
|
1504
|
-
return eventTypes.flatMap(
|
|
1505
|
-
(type) => sameRepoPullRequests.flatMap((pullRequest) => {
|
|
1506
|
-
const facts = options?.pullRequestFactsByNumber?.[pullRequest.number];
|
|
1507
|
-
const pullRequestHeadBranch = checkSuiteHeadBranch(pullRequest.head?.ref) ?? headBranch;
|
|
1508
|
-
return pullRequestTargets(
|
|
1509
|
-
buildCheckSuiteEvent({
|
|
1510
|
-
...shared,
|
|
1511
|
-
eventType: type,
|
|
1512
|
-
...facts?.openerEmail ? { openerEmail: facts.openerEmail } : void 0,
|
|
1513
|
-
...facts?.openerUsername ? { openerUsername: facts.openerUsername } : void 0,
|
|
1514
|
-
...pullRequestHeadBranch ? { headBranch: pullRequestHeadBranch } : void 0,
|
|
1515
|
-
...typeof facts?.isDraft === "boolean" ? { isDraft: facts.isDraft } : void 0,
|
|
1516
|
-
pullRequestNumber: pullRequest.number
|
|
1517
|
-
}),
|
|
1518
|
-
repo
|
|
1519
|
-
);
|
|
1520
|
-
})
|
|
1521
|
-
);
|
|
1522
|
-
}
|
|
1523
|
-
function checkSuitePullRequests(body) {
|
|
1524
|
-
const parsed = checkSuiteWebhookSchema.safeParse(body);
|
|
1525
|
-
if (!parsed.success || parsed.data.action !== "completed") return [];
|
|
1526
|
-
const repository = parsed.data.repository;
|
|
1527
|
-
return parsed.data.check_suite.pull_requests.filter(
|
|
1528
|
-
(pullRequest) => isCheckSuiteRepositoryPullRequest(pullRequest, repository.id)
|
|
1529
|
-
).map((pullRequest) => ({
|
|
1530
|
-
number: pullRequest.number,
|
|
1531
|
-
repositoryFullName: repository.full_name
|
|
1532
|
-
}));
|
|
1533
|
-
}
|
|
1534
|
-
function parseCheckSuitePublishTargets(body) {
|
|
1535
|
-
const parsed = checkSuiteWebhookSchema.safeParse(body);
|
|
1536
|
-
if (!parsed.success || parsed.data.action !== "completed") return void 0;
|
|
1537
|
-
const eventTypes = checkSuiteEventTypes(parsed.data.check_suite.conclusion);
|
|
1538
|
-
if (eventTypes.length === 0) return void 0;
|
|
1539
|
-
const repository = parsed.data.repository;
|
|
1540
|
-
const repo = repository.full_name;
|
|
1541
|
-
const identifiers = /* @__PURE__ */ new Set([
|
|
1542
|
-
gitHubRepositoryResource({ repo }).identifier
|
|
1543
|
-
]);
|
|
1544
|
-
for (const pullRequest of parsed.data.check_suite.pull_requests) {
|
|
1545
|
-
if (!isCheckSuiteRepositoryPullRequest(pullRequest, repository.id))
|
|
1546
|
-
continue;
|
|
1547
|
-
identifiers.add(
|
|
1548
|
-
gitHubPullRequestResource({ number: pullRequest.number, repo }).identifier
|
|
1549
|
-
);
|
|
1550
|
-
}
|
|
1551
|
-
return {
|
|
1552
|
-
// Every copy sets this repo, so a broad trigger for another repo adds
|
|
1553
|
-
// no lazy keys.
|
|
1554
|
-
data: { repo: gitHubRepoDataSchema.parse(repo) },
|
|
1555
|
-
eventTypes,
|
|
1556
|
-
identifiers: [...identifiers]
|
|
1557
|
-
};
|
|
1558
|
-
}
|
|
1559
|
-
async function wantsCheckSuitePassed(args) {
|
|
1560
|
-
const targets = parseCheckSuitePublishTargets(args.body);
|
|
1561
|
-
if (!args.hasMatch || !targets?.eventTypes.includes(CHECKS_PASSED)) {
|
|
1562
|
-
return false;
|
|
1563
|
-
}
|
|
1564
|
-
const keys = await args.neededMatchKeys?.({
|
|
1565
|
-
...targets,
|
|
1566
|
-
eventTypes: [CHECKS_PASSED]
|
|
1567
|
-
}) ?? [];
|
|
1568
|
-
if (keys.some((key) => LATE_MATCH_KEYS.has(key))) return true;
|
|
1569
|
-
return await args.hasMatch(
|
|
1570
|
-
normalizeCheckSuiteEvents(args.deliveryId, args.body, {
|
|
1571
|
-
allCheckSuitesPassed: true
|
|
1572
|
-
}).filter((event) => event.eventType === CHECKS_PASSED)
|
|
1573
|
-
);
|
|
1574
|
-
}
|
|
1575
|
-
function parseCheckSuiteFactsTarget(body, options) {
|
|
1576
|
-
const parsed = checkSuiteWebhookSchema.safeParse(body);
|
|
1577
|
-
if (!parsed.success || parsed.data.action !== "completed") return void 0;
|
|
1578
|
-
const conclusion = parsed.data.check_suite.conclusion;
|
|
1579
|
-
if (checkSuiteEventTypes(conclusion).length === 0) return void 0;
|
|
1580
|
-
const headSha = parsed.data.check_suite.head_sha;
|
|
1581
|
-
const checkSuiteId = parsed.data.check_suite.id;
|
|
1582
|
-
if (typeof headSha !== "string" || !/^[0-9a-f]{7,40}$/i.test(headSha) || typeof checkSuiteId !== "number") {
|
|
1583
|
-
return void 0;
|
|
1584
|
-
}
|
|
1585
|
-
const repository = parsed.data.repository;
|
|
1586
|
-
const pullRequestNumbers = [
|
|
1587
|
-
...new Set(
|
|
1588
|
-
parsed.data.check_suite.pull_requests.filter(
|
|
1589
|
-
(pullRequest) => isCheckSuiteRepositoryPullRequest(pullRequest, repository.id)
|
|
1590
|
-
).map((pullRequest) => pullRequest.number)
|
|
1591
|
-
)
|
|
1592
|
-
];
|
|
1593
|
-
const loadFailingChecks = conclusion === "failure" || conclusion === "timed_out";
|
|
1594
|
-
const loadPullRequestFacts = Boolean(
|
|
1595
|
-
options?.loadPullRequestFacts && pullRequestNumbers.length > 0
|
|
1596
|
-
);
|
|
1597
|
-
const loadCheckSuites = Boolean(
|
|
1598
|
-
options?.loadCheckSuites && PASSING_SUITE_CONCLUSIONS.has(conclusion ?? "")
|
|
1599
|
-
);
|
|
1600
|
-
if (!loadFailingChecks && !loadPullRequestFacts && !loadCheckSuites) {
|
|
1601
|
-
return void 0;
|
|
1602
|
-
}
|
|
1603
|
-
return {
|
|
1604
|
-
checkSuiteId,
|
|
1605
|
-
headSha,
|
|
1606
|
-
loadCheckSuites,
|
|
1607
|
-
loadFailingChecks,
|
|
1608
|
-
loadPullRequestFacts,
|
|
1609
|
-
owner: repository.owner.login,
|
|
1610
|
-
pullRequestNumbers,
|
|
1611
|
-
repoName: repository.name
|
|
1612
|
-
};
|
|
1613
|
-
}
|
|
1614
|
-
function checkRunsFromResponse(value) {
|
|
1615
|
-
const parsed = checkRunsListResponseSchema.safeParse(value);
|
|
1616
|
-
if (!parsed.success) return [];
|
|
1617
|
-
return parsed.data.check_runs ?? [];
|
|
1618
|
-
}
|
|
1619
|
-
function pullRequestFactsFromResponse(value) {
|
|
1620
|
-
if (!isRecord(value)) return void 0;
|
|
1621
|
-
const facts = {};
|
|
1622
|
-
if (typeof value.draft === "boolean") facts.isDraft = value.draft;
|
|
1623
|
-
const user = value.user;
|
|
1624
|
-
if (isRecord(user)) {
|
|
1625
|
-
const username = typeof user.login === "string" ? user.login.trim() : void 0;
|
|
1626
|
-
if (username) facts.openerUsername = username;
|
|
1627
|
-
const email = typeof user.email === "string" ? user.email.trim() : void 0;
|
|
1628
|
-
if (email) facts.openerEmail = email;
|
|
1629
|
-
}
|
|
1630
|
-
return Object.keys(facts).length > 0 ? facts : void 0;
|
|
1631
|
-
}
|
|
1632
|
-
function needsCheckSuitePullRequestFacts(matchKeys) {
|
|
1633
|
-
for (const key of matchKeys) {
|
|
1634
|
-
if (CHECK_SUITE_PULL_REQUEST_MATCH_KEYS.has(key)) return true;
|
|
1635
|
-
}
|
|
1636
|
-
return false;
|
|
1637
|
-
}
|
|
1638
|
-
async function loadAllCheckSuitesPassed(args) {
|
|
1639
|
-
const suites = [];
|
|
1640
|
-
let totalCount = 0;
|
|
1641
|
-
for (let page = 1; page <= MAX_CHECK_SUITE_PAGES; page += 1) {
|
|
1642
|
-
const response = checkSuitesListResponseSchema.parse(
|
|
1643
|
-
await githubRequest(
|
|
1644
|
-
"https://api.github.com",
|
|
1645
|
-
`/repos/${encodeURIComponent(args.owner)}/${encodeURIComponent(args.repoName)}/commits/${args.headSha}/check-suites?per_page=100&page=${page}`,
|
|
1646
|
-
{ token: args.token }
|
|
1647
|
-
)
|
|
1648
|
-
);
|
|
1649
|
-
suites.push(...response.check_suites);
|
|
1650
|
-
totalCount = response.total_count;
|
|
1651
|
-
if (suites.length >= totalCount || response.check_suites.length === 0) {
|
|
1652
|
-
break;
|
|
1653
|
-
}
|
|
1654
|
-
}
|
|
1655
|
-
if (suites.length < totalCount) return false;
|
|
1656
|
-
return suites.every(
|
|
1657
|
-
(suite) => suite.id === args.checkSuiteId || suite.status === "queued" && suite.latest_check_runs_count === 0 && suite.app?.slug !== "github-actions" || suite.status === "completed" && PASSING_SUITE_CONCLUSIONS.has(suite.conclusion ?? "")
|
|
1658
|
-
);
|
|
1659
|
-
}
|
|
1660
|
-
async function loadCheckSuiteFacts(args) {
|
|
1661
|
-
const target = parseCheckSuiteFactsTarget(args.body, {
|
|
1662
|
-
loadCheckSuites: args.loadCheckSuites,
|
|
1663
|
-
loadPullRequestFacts: args.loadPullRequestFacts
|
|
1664
|
-
});
|
|
1665
|
-
if (!target) return void 0;
|
|
1666
|
-
const facts = {};
|
|
1667
|
-
if (target.loadFailingChecks) {
|
|
1668
|
-
try {
|
|
1669
|
-
const token = await issueInstallationToken({
|
|
1670
|
-
appIdEnv: args.appIdEnv,
|
|
1671
|
-
installationIdEnv: args.installationIdEnv,
|
|
1672
|
-
permissions: { checks: "read" },
|
|
1673
|
-
privateKeyEnv: args.privateKeyEnv,
|
|
1674
|
-
repositories: [target.repoName]
|
|
1675
|
-
});
|
|
1676
|
-
const response = await githubRequest(
|
|
1677
|
-
"https://api.github.com",
|
|
1678
|
-
`/repos/${encodeURIComponent(target.owner)}/${encodeURIComponent(target.repoName)}/check-suites/${target.checkSuiteId}/check-runs?filter=latest&per_page=100`,
|
|
1679
|
-
{ token: token.token }
|
|
1680
|
-
);
|
|
1681
|
-
const failing = selectFailingChecks(checkRunsFromResponse(response), {
|
|
1682
|
-
checkSuiteId: target.checkSuiteId
|
|
1683
|
-
});
|
|
1684
|
-
if (failing.length > 0) facts.failingChecks = failing;
|
|
1685
|
-
} catch (error) {
|
|
1686
|
-
logFailOpenFailure(args.log, "GitHub check suite load failed", error, {
|
|
1687
|
-
checkSuiteId: target.checkSuiteId,
|
|
1688
|
-
repository: `${target.owner}/${target.repoName}`
|
|
1689
|
-
});
|
|
1690
|
-
}
|
|
1691
|
-
}
|
|
1692
|
-
if (target.loadCheckSuites) {
|
|
1693
|
-
try {
|
|
1694
|
-
const token = await issueInstallationToken({
|
|
1695
|
-
appIdEnv: args.appIdEnv,
|
|
1696
|
-
installationIdEnv: args.installationIdEnv,
|
|
1697
|
-
permissions: { checks: "read" },
|
|
1698
|
-
privateKeyEnv: args.privateKeyEnv,
|
|
1699
|
-
repositories: [target.repoName]
|
|
1700
|
-
});
|
|
1701
|
-
if (await loadAllCheckSuitesPassed({
|
|
1702
|
-
checkSuiteId: target.checkSuiteId,
|
|
1703
|
-
headSha: target.headSha,
|
|
1704
|
-
owner: target.owner,
|
|
1705
|
-
repoName: target.repoName,
|
|
1706
|
-
token: token.token
|
|
1707
|
-
})) {
|
|
1708
|
-
facts.allCheckSuitesPassed = true;
|
|
1709
|
-
}
|
|
1710
|
-
} catch (error) {
|
|
1711
|
-
logFailOpenFailure(
|
|
1712
|
-
args.log,
|
|
1713
|
-
"GitHub commit check suites load failed",
|
|
1714
|
-
error,
|
|
1715
|
-
{
|
|
1716
|
-
checkSuiteId: target.checkSuiteId,
|
|
1717
|
-
repository: `${target.owner}/${target.repoName}`
|
|
1718
|
-
}
|
|
1719
|
-
);
|
|
1720
|
-
}
|
|
1721
|
-
}
|
|
1722
|
-
if (target.loadFailingChecks && args.loadGitHubIncidents) {
|
|
1723
|
-
try {
|
|
1724
|
-
const incidents = await args.loadGitHubIncidents();
|
|
1725
|
-
if (incidents.length > 0) facts.githubIncidents = incidents;
|
|
1726
|
-
} catch (error) {
|
|
1727
|
-
logFailOpenFailure(args.log, "GitHub status load failed", error, {});
|
|
1728
|
-
}
|
|
1729
|
-
}
|
|
1730
|
-
if (target.loadPullRequestFacts) {
|
|
1731
|
-
try {
|
|
1732
|
-
const token = await issueInstallationToken({
|
|
1733
|
-
appIdEnv: args.appIdEnv,
|
|
1734
|
-
installationIdEnv: args.installationIdEnv,
|
|
1735
|
-
permissions: { pull_requests: "read" },
|
|
1736
|
-
privateKeyEnv: args.privateKeyEnv,
|
|
1737
|
-
repositories: [target.repoName]
|
|
1738
|
-
});
|
|
1739
|
-
const loaded = await Promise.all(
|
|
1740
|
-
target.pullRequestNumbers.map(async (number5) => {
|
|
1741
|
-
try {
|
|
1742
|
-
const response = await githubRequest(
|
|
1743
|
-
"https://api.github.com",
|
|
1744
|
-
`/repos/${encodeURIComponent(target.owner)}/${encodeURIComponent(target.repoName)}/pulls/${number5}`,
|
|
1745
|
-
{ token: token.token }
|
|
1746
|
-
);
|
|
1747
|
-
const loadedFacts = pullRequestFactsFromResponse(response);
|
|
1748
|
-
return loadedFacts ? [number5, loadedFacts] : void 0;
|
|
1749
|
-
} catch (error) {
|
|
1750
|
-
if (!isGitHubNotFoundError(error)) {
|
|
1751
|
-
logFailOpenFailure(
|
|
1752
|
-
args.log,
|
|
1753
|
-
"GitHub pull request data load failed",
|
|
1754
|
-
error,
|
|
1755
|
-
{
|
|
1756
|
-
pullRequest: number5,
|
|
1757
|
-
repository: `${target.owner}/${target.repoName}`
|
|
1758
|
-
}
|
|
1759
|
-
);
|
|
1760
|
-
}
|
|
1761
|
-
return void 0;
|
|
1762
|
-
}
|
|
1763
|
-
})
|
|
1764
|
-
);
|
|
1765
|
-
const pullRequestFactsByNumber = {};
|
|
1766
|
-
for (const entry of loaded) {
|
|
1767
|
-
if (!entry) continue;
|
|
1768
|
-
pullRequestFactsByNumber[entry[0]] = entry[1];
|
|
1769
|
-
}
|
|
1770
|
-
if (Object.keys(pullRequestFactsByNumber).length > 0) {
|
|
1771
|
-
facts.pullRequestFactsByNumber = pullRequestFactsByNumber;
|
|
1772
|
-
}
|
|
1773
|
-
} catch (error) {
|
|
1774
|
-
logFailOpenFailure(
|
|
1775
|
-
args.log,
|
|
1776
|
-
"GitHub check suite pull request data load failed",
|
|
1777
|
-
error,
|
|
1778
|
-
{
|
|
1779
|
-
checkSuiteId: target.checkSuiteId,
|
|
1780
|
-
repository: `${target.owner}/${target.repoName}`
|
|
1781
|
-
}
|
|
1782
|
-
);
|
|
1783
|
-
}
|
|
1784
|
-
}
|
|
1785
|
-
return facts.failingChecks || facts.allCheckSuitesPassed || facts.pullRequestFactsByNumber ? facts : void 0;
|
|
1786
|
-
}
|
|
1787
|
-
|
|
1788
|
-
// src/webhooks/issue-update.ts
|
|
1789
|
-
import * as z5 from "zod";
|
|
1790
|
-
|
|
1791
|
-
// src/webhooks/ownership.ts
|
|
1792
|
-
import { lexer } from "marked";
|
|
1793
|
-
var GITHUB_BOT_NOREPLY_EMAIL = /^(?<userId>[1-9]\d*)\+(?<login>[^@]+\[bot\])@users\.noreply\.github\.com$/i;
|
|
1794
|
-
function parseGitHubBotIdentity(value) {
|
|
1795
|
-
const match = GITHUB_BOT_NOREPLY_EMAIL.exec(value?.trim() ?? "");
|
|
1796
|
-
const login = match?.groups?.login;
|
|
1797
|
-
const userId = match?.groups?.userId;
|
|
1798
|
-
if (!login || !userId) return void 0;
|
|
1799
|
-
return {
|
|
1800
|
-
login,
|
|
1801
|
-
userId: Number(userId)
|
|
1802
|
-
};
|
|
1803
|
-
}
|
|
1804
|
-
function botLoginFromEmail(value) {
|
|
1805
|
-
return parseGitHubBotIdentity(value)?.login;
|
|
1806
|
-
}
|
|
1807
|
-
function isBotLogin(login, botEmail) {
|
|
1808
|
-
const botLogin = botLoginFromEmail(botEmail)?.toLowerCase();
|
|
1809
|
-
return Boolean(botLogin && login?.trim().toLowerCase() === botLogin);
|
|
1810
|
-
}
|
|
1811
|
-
function botUserIdFromEmail(value) {
|
|
1812
|
-
return parseGitHubBotIdentity(value)?.userId;
|
|
1813
|
-
}
|
|
1814
|
-
var SKIPPED_TOKEN_TYPES = /* @__PURE__ */ new Set(["blockquote", "code", "codespan", "html"]);
|
|
1815
|
-
function ownTokenText(tokens, separator = "") {
|
|
1816
|
-
return tokens.map((token) => {
|
|
1817
|
-
if (SKIPPED_TOKEN_TYPES.has(token.type)) return " ";
|
|
1818
|
-
if (token.type === "list") {
|
|
1819
|
-
return token.items.map((item) => ownTokenText(item.tokens, "\n")).join("\n");
|
|
1820
|
-
}
|
|
1821
|
-
if (token.type === "table") {
|
|
1822
|
-
const table = token;
|
|
1823
|
-
return [table.header, ...table.rows].flat().map((cell) => ownTokenText(cell.tokens)).join(" ");
|
|
1824
|
-
}
|
|
1825
|
-
if ("tokens" in token && token.tokens) return ownTokenText(token.tokens);
|
|
1826
|
-
return "text" in token ? token.text : token.raw;
|
|
1827
|
-
}).join(separator);
|
|
1828
|
-
}
|
|
1829
|
-
function ownCommentText(text) {
|
|
1830
|
-
return ownTokenText(lexer(text, { gfm: true }), "\n");
|
|
1831
|
-
}
|
|
1832
|
-
function mentionsBotLogin(text, botEmail) {
|
|
1833
|
-
const slug = botLoginFromEmail(botEmail)?.replace(/\[bot\]$/i, "");
|
|
1834
|
-
if (!slug) return false;
|
|
1835
|
-
const escaped = slug.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
1836
|
-
return new RegExp(`(?:^|[^\\w-])@${escaped}(?![\\w-])`, "i").test(
|
|
1837
|
-
ownCommentText(text)
|
|
1838
|
-
);
|
|
1839
|
-
}
|
|
1840
|
-
|
|
1841
|
-
// src/webhooks/issue-update.ts
|
|
1842
|
-
var issueUpdateWebhookSchema = z5.object({
|
|
1843
|
-
action: z5.enum(GITHUB_ISSUE_UPDATE_CHANGES),
|
|
1844
|
-
assignee: z5.object({ login: z5.string().nullish() }).nullish(),
|
|
1845
|
-
changes: z5.record(z5.string(), z5.unknown()).optional(),
|
|
1846
|
-
issue: z5.object({
|
|
1847
|
-
body: z5.string().optional().nullable(),
|
|
1848
|
-
number: z5.number(),
|
|
1849
|
-
title: z5.string().optional(),
|
|
1850
|
-
updated_at: z5.string().optional()
|
|
1851
|
-
}),
|
|
1852
|
-
label: z5.object({ name: z5.string() }).nullish(),
|
|
1853
|
-
milestone: z5.object({ title: z5.string() }).nullish(),
|
|
1854
|
-
repository: z5.object({ full_name: z5.string().min(1) }),
|
|
1855
|
-
sender: gitHubSenderSchema.optional()
|
|
1856
|
-
});
|
|
1857
|
-
function describeIssueUpdate(delivery) {
|
|
1858
|
-
const assignee = nonBlank(delivery.assignee?.login);
|
|
1859
|
-
switch (delivery.action) {
|
|
1860
|
-
case "edited": {
|
|
1861
|
-
const fields = ["title", "body"].filter(
|
|
1862
|
-
(field) => field in (delivery.changes ?? {})
|
|
1863
|
-
);
|
|
1864
|
-
return {
|
|
1865
|
-
change: fields.length ? `edited (${fields.join(", ")})` : "edited"
|
|
1866
|
-
};
|
|
1867
|
-
}
|
|
1868
|
-
case "labeled":
|
|
1869
|
-
case "unlabeled":
|
|
1870
|
-
return {
|
|
1871
|
-
change: delivery.action,
|
|
1872
|
-
untrustedText: delivery.label ? `Label: ${delivery.label.name}` : void 0
|
|
1873
|
-
};
|
|
1874
|
-
case "assigned":
|
|
1875
|
-
return { change: assignee ? `assigned to ${assignee}` : "assigned" };
|
|
1876
|
-
case "unassigned":
|
|
1877
|
-
return {
|
|
1878
|
-
change: assignee ? `unassigned from ${assignee}` : "unassigned"
|
|
1879
|
-
};
|
|
1880
|
-
case "milestoned":
|
|
1881
|
-
case "demilestoned":
|
|
1882
|
-
return {
|
|
1883
|
-
change: delivery.action,
|
|
1884
|
-
untrustedText: delivery.milestone ? `Milestone: ${delivery.milestone.title}` : void 0
|
|
1885
|
-
};
|
|
1886
|
-
}
|
|
1887
|
-
}
|
|
1888
|
-
function normalizeIssueUpdateEvents(deliveryId, body, botEmail) {
|
|
1889
|
-
const parsed = issueUpdateWebhookSchema.safeParse(body);
|
|
1890
|
-
if (!parsed.success) return void 0;
|
|
1891
|
-
const update = describeIssueUpdate(parsed.data);
|
|
1892
|
-
const { action, label, sender } = parsed.data;
|
|
1893
|
-
const doneBy = sender?.login;
|
|
1894
|
-
if (isBotLogin(doneBy, botEmail)) return [];
|
|
1895
|
-
const input = {
|
|
1896
|
-
number: parsed.data.issue.number,
|
|
1897
|
-
repo: parsed.data.repository.full_name
|
|
1898
|
-
};
|
|
1899
|
-
const issue = gitHubIssueResource(input);
|
|
1900
|
-
const untrustedText = [
|
|
1901
|
-
update.untrustedText,
|
|
1902
|
-
titleAndBodyText(parsed.data.issue)
|
|
1903
|
-
].filter((part) => part !== void 0).join("\n\n");
|
|
1904
|
-
return [issue, gitHubRepositoryResource(input)].map((resource) => ({
|
|
1905
|
-
eventKey: gitHubEventKey(deliveryId, "issue.updated"),
|
|
1906
|
-
eventType: "issue.updated",
|
|
1907
|
-
occurredAtMs: providerTime(parsed.data.issue.updated_at) ?? Date.now(),
|
|
1908
|
-
identifier: resource.identifier,
|
|
1909
|
-
trustedSummary: `${issue.label} was ${update.change}${doneBy ? ` by ${doneBy}` : ""}.`,
|
|
1910
|
-
...gitHubDoneBy(doneBy),
|
|
1911
|
-
// `change` and `label` let an automation select one label change in code,
|
|
1912
|
-
// so the model does not decide from untrusted text.
|
|
1913
|
-
data: definedValues({
|
|
1914
|
-
repo: gitHubRepoDataSchema.parse(input.repo),
|
|
1915
|
-
issue: input.number,
|
|
1916
|
-
change: action,
|
|
1917
|
-
label: label?.name.toLowerCase(),
|
|
1918
|
-
...gitHubSenderData(sender)
|
|
1919
|
-
}),
|
|
1920
|
-
...untrustedText ? { untrustedText } : void 0
|
|
1921
|
-
}));
|
|
1922
|
-
}
|
|
1923
|
-
|
|
1924
|
-
// src/webhooks/events.ts
|
|
1925
|
-
var repositorySchema2 = z6.object({ full_name: z6.string().min(1) }).passthrough();
|
|
1926
|
-
var deploymentSchema = z6.object({
|
|
1927
|
-
created_at: z6.string().optional(),
|
|
1928
|
-
environment: z6.string().trim().min(1),
|
|
1929
|
-
id: z6.number(),
|
|
1930
|
-
sha: z6.string().regex(/^[0-9a-f]{40}$/i)
|
|
1931
|
-
}).passthrough();
|
|
1932
|
-
var deploymentWorkflowSchema = {
|
|
1933
|
-
workflow: z6.object({ id: z6.number(), path: z6.string().min(1) }).passthrough().nullish(),
|
|
1934
|
-
workflow_run: z6.object({ id: z6.number() }).passthrough().nullish()
|
|
1935
|
-
};
|
|
1936
|
-
var deploymentWebhookSchema = z6.object({
|
|
1937
|
-
action: z6.string(),
|
|
1938
|
-
deployment: deploymentSchema,
|
|
1939
|
-
repository: repositorySchema2,
|
|
1940
|
-
...deploymentWorkflowSchema
|
|
1941
|
-
}).passthrough();
|
|
1942
|
-
var canonicalDeploymentEventSchema = z6.object({
|
|
1943
|
-
action: z6.string(),
|
|
1944
|
-
commitSha: z6.string().regex(/^[0-9a-f]{40}$/i),
|
|
1945
|
-
createdAt: z6.string().optional(),
|
|
1946
|
-
deploymentId: z6.number(),
|
|
1947
|
-
environment: z6.string().min(1),
|
|
1948
|
-
repo: z6.string().min(1),
|
|
1949
|
-
workflowId: z6.number().optional(),
|
|
1950
|
-
workflowPath: z6.string().min(1).optional(),
|
|
1951
|
-
workflowRunId: z6.number().optional()
|
|
1952
|
-
}).strict();
|
|
1953
|
-
function canonicalDeploymentFields(data) {
|
|
1954
|
-
return {
|
|
1955
|
-
action: data.action,
|
|
1956
|
-
commitSha: data.deployment.sha,
|
|
1957
|
-
createdAt: data.deployment.created_at,
|
|
1958
|
-
deploymentId: data.deployment.id,
|
|
1959
|
-
environment: data.deployment.environment,
|
|
1960
|
-
repo: data.repository.full_name,
|
|
1961
|
-
workflowId: data.workflow?.id,
|
|
1962
|
-
workflowPath: data.workflow?.path,
|
|
1963
|
-
workflowRunId: data.workflow_run?.id
|
|
1964
|
-
};
|
|
1965
|
-
}
|
|
1966
|
-
function parseDeploymentEvent(body) {
|
|
1967
|
-
const parsed = deploymentWebhookSchema.safeParse(body);
|
|
1968
|
-
if (!parsed.success) return void 0;
|
|
1969
|
-
return canonicalDeploymentEventSchema.parse(
|
|
1970
|
-
canonicalDeploymentFields(parsed.data)
|
|
1971
|
-
);
|
|
1972
|
-
}
|
|
1973
|
-
function normalizeDeploymentEvent(deliveryId, body) {
|
|
1974
|
-
const parsed = parseDeploymentEvent(body);
|
|
1975
|
-
if (!parsed || parsed.action !== "created") return [];
|
|
1976
|
-
const eventType = "deployment.created";
|
|
1977
|
-
const { label } = gitHubDeploymentSourceResource(parsed);
|
|
1978
|
-
return deploymentTargets(parsed).map(({ identifier }) => ({
|
|
1979
|
-
eventKey: gitHubEventKey(deliveryId, eventType),
|
|
1980
|
-
eventType,
|
|
1981
|
-
occurredAtMs: providerTime(parsed.createdAt) ?? Date.now(),
|
|
1982
|
-
identifier,
|
|
1983
|
-
trustedSummary: `${label} was created (deployment ${parsed.deploymentId}).`,
|
|
1984
|
-
data: deploymentEventData(parsed)
|
|
1985
|
-
}));
|
|
1986
|
-
}
|
|
1987
|
-
var deploymentStatusWebhookSchema = deploymentWebhookSchema.extend({
|
|
1988
|
-
deployment_status: z6.object({
|
|
1989
|
-
created_at: z6.string().optional(),
|
|
1990
|
-
description: z6.string().optional().nullable(),
|
|
1991
|
-
state: z6.string()
|
|
1992
|
-
}).passthrough()
|
|
1993
|
-
});
|
|
1994
|
-
var canonicalDeploymentStatusEventSchema = canonicalDeploymentEventSchema.extend({
|
|
1995
|
-
description: z6.string().optional().nullable(),
|
|
1996
|
-
state: z6.string(),
|
|
1997
|
-
statusCreatedAt: z6.string().optional()
|
|
1998
|
-
}).strict();
|
|
1999
|
-
function parseDeploymentStatusEvent(body) {
|
|
2000
|
-
const parsed = deploymentStatusWebhookSchema.safeParse(body);
|
|
2001
|
-
if (!parsed.success) return void 0;
|
|
2002
|
-
return canonicalDeploymentStatusEventSchema.parse({
|
|
2003
|
-
...canonicalDeploymentFields(parsed.data),
|
|
2004
|
-
description: parsed.data.deployment_status.description,
|
|
2005
|
-
state: parsed.data.deployment_status.state,
|
|
2006
|
-
statusCreatedAt: parsed.data.deployment_status.created_at
|
|
2007
|
-
});
|
|
2008
|
-
}
|
|
2009
|
-
function deploymentStatusEventType(state) {
|
|
2010
|
-
switch (state) {
|
|
2011
|
-
case "queued":
|
|
2012
|
-
case "pending":
|
|
2013
|
-
case "in_progress":
|
|
2014
|
-
return `deployment.${state}`;
|
|
2015
|
-
case "success":
|
|
2016
|
-
return "deployment.succeeded";
|
|
2017
|
-
case "failure":
|
|
2018
|
-
return "deployment.failed";
|
|
2019
|
-
case "error":
|
|
2020
|
-
return "deployment.error";
|
|
2021
|
-
default:
|
|
2022
|
-
return void 0;
|
|
2023
|
-
}
|
|
2024
|
-
}
|
|
2025
|
-
function normalizeDeploymentStatusEvent(deliveryId, body) {
|
|
2026
|
-
const parsed = parseDeploymentStatusEvent(body);
|
|
2027
|
-
if (!parsed || parsed.action !== "created") return [];
|
|
2028
|
-
const eventType = deploymentStatusEventType(parsed.state);
|
|
2029
|
-
if (!eventType) return [];
|
|
2030
|
-
const outcome = eventType === "deployment.succeeded" ? "succeeded" : eventType === "deployment.failed" ? "failed" : eventType === "deployment.error" ? "reported an error" : eventType === "deployment.in_progress" ? "started" : `is ${parsed.state}`;
|
|
2031
|
-
const terminal = eventType === "deployment.succeeded" || eventType === "deployment.failed" || eventType === "deployment.error";
|
|
2032
|
-
const { label } = gitHubDeploymentSourceResource(parsed);
|
|
2033
|
-
return deploymentTargets(parsed).map(
|
|
2034
|
-
({ completeOnTerminalEvent, identifier }) => ({
|
|
2035
|
-
eventKey: gitHubEventKey(deliveryId, eventType),
|
|
2036
|
-
eventType,
|
|
2037
|
-
occurredAtMs: providerTime(parsed.statusCreatedAt) ?? Date.now(),
|
|
2038
|
-
identifier,
|
|
2039
|
-
...terminal && completeOnTerminalEvent ? { terminal: true } : void 0,
|
|
2040
|
-
trustedSummary: `${label} ${outcome} (deployment ${parsed.deploymentId}).`,
|
|
2041
|
-
data: deploymentEventData(parsed),
|
|
2042
|
-
untrustedText: parsed.description ?? void 0
|
|
2043
|
-
})
|
|
2044
|
-
);
|
|
2045
|
-
}
|
|
2046
|
-
var deploymentEventDataSchema = z6.object({
|
|
2047
|
-
repo: gitHubRepoDataSchema,
|
|
2048
|
-
commitSha: gitHubCommitShaDataSchema,
|
|
2049
|
-
deploymentId: z6.number(),
|
|
2050
|
-
// Trimmed and lowercase, the same as in the identifier.
|
|
2051
|
-
environment: z6.string().trim().min(1).toLowerCase(),
|
|
2052
|
-
workflowId: z6.number().optional(),
|
|
2053
|
-
workflowPath: z6.string().min(1).optional(),
|
|
2054
|
-
workflowRunId: z6.number().optional()
|
|
2055
|
-
}).strict();
|
|
2056
|
-
function deploymentEventData(input) {
|
|
2057
|
-
return deploymentEventDataSchema.parse(
|
|
2058
|
-
definedValues({
|
|
2059
|
-
repo: input.repo,
|
|
2060
|
-
commitSha: input.commitSha,
|
|
2061
|
-
deploymentId: input.deploymentId,
|
|
2062
|
-
environment: input.environment,
|
|
2063
|
-
workflowId: input.workflowId,
|
|
2064
|
-
workflowPath: input.workflowPath,
|
|
2065
|
-
workflowRunId: input.workflowRunId
|
|
2066
|
-
})
|
|
2067
|
-
);
|
|
2068
|
-
}
|
|
2069
|
-
function deploymentTargets(input) {
|
|
2070
|
-
return [
|
|
2071
|
-
{
|
|
2072
|
-
completeOnTerminalEvent: true,
|
|
2073
|
-
identifier: gitHubDeploymentSourceResource(input).identifier
|
|
2074
|
-
},
|
|
2075
|
-
{
|
|
2076
|
-
completeOnTerminalEvent: false,
|
|
2077
|
-
identifier: gitHubDeploymentSourceResource({
|
|
2078
|
-
commitSha: input.commitSha,
|
|
2079
|
-
repo: input.repo
|
|
2080
|
-
}).identifier
|
|
2081
|
-
},
|
|
2082
|
-
{
|
|
2083
|
-
completeOnTerminalEvent: false,
|
|
2084
|
-
identifier: gitHubRepositoryResource({ repo: input.repo }).identifier
|
|
2085
|
-
}
|
|
2086
|
-
];
|
|
2087
|
-
}
|
|
2088
|
-
var gitHubUserSchema = z6.object({
|
|
2089
|
-
email: z6.string().nullish(),
|
|
2090
|
-
login: z6.string().nullish()
|
|
2091
|
-
});
|
|
2092
|
-
var commentSchema = z6.object({
|
|
2093
|
-
body: z6.string(),
|
|
2094
|
-
id: z6.number(),
|
|
2095
|
-
user: z6.object({ login: z6.string().optional() }).optional()
|
|
2096
|
-
});
|
|
2097
|
-
var commentAuthorDataSchema = z6.object({
|
|
2098
|
-
mentionsBot: z6.boolean().optional(),
|
|
2099
|
-
senderId: z6.number().optional(),
|
|
2100
|
-
senderLogin: z6.string().min(1).optional()
|
|
2101
|
-
});
|
|
2102
|
-
function commentAuthorData(input) {
|
|
2103
|
-
return {
|
|
2104
|
-
mentionsBot: input.botEmail ? mentionsBotLogin(input.text ?? "", input.botEmail) : void 0,
|
|
2105
|
-
...gitHubSenderData(input.sender)
|
|
2106
|
-
};
|
|
2107
|
-
}
|
|
2108
|
-
var pullRequestSchema = z6.object({
|
|
2109
|
-
draft: z6.boolean().optional(),
|
|
2110
|
-
head: z6.object({ ref: z6.string().nullish() }).nullish(),
|
|
2111
|
-
number: z6.number(),
|
|
2112
|
-
user: gitHubUserSchema.nullish()
|
|
2113
|
-
});
|
|
2114
|
-
var pullRequestEventDataSchema = commentAuthorDataSchema.extend({
|
|
2115
|
-
repo: gitHubRepoDataSchema,
|
|
2116
|
-
pullRequest: z6.number(),
|
|
2117
|
-
headBranch: z6.string().min(1).optional(),
|
|
2118
|
-
isDraft: z6.boolean().optional(),
|
|
2119
|
-
openerEmail: z6.string().min(1).optional(),
|
|
2120
|
-
openerUsername: z6.string().min(1).optional(),
|
|
2121
|
-
commentId: z6.number().optional(),
|
|
2122
|
-
commentKind: z6.enum(["conversation", "review", "review_body"]).optional()
|
|
2123
|
-
}).strict();
|
|
2124
|
-
function pullRequestEventData(input) {
|
|
2125
|
-
const { pullRequest } = input;
|
|
2126
|
-
return pullRequestEventDataSchema.parse(
|
|
2127
|
-
definedValues({
|
|
2128
|
-
repo: input.repo,
|
|
2129
|
-
pullRequest: pullRequest.number,
|
|
2130
|
-
headBranch: nonBlank(pullRequest.head?.ref),
|
|
2131
|
-
isDraft: pullRequest.draft,
|
|
2132
|
-
openerEmail: nonBlank(pullRequest.user?.email),
|
|
2133
|
-
openerUsername: nonBlank(pullRequest.user?.login),
|
|
2134
|
-
...input.comment
|
|
2135
|
-
})
|
|
2136
|
-
);
|
|
2137
|
-
}
|
|
2138
|
-
var issueEventDataSchema = commentAuthorDataSchema.extend({
|
|
2139
|
-
repo: gitHubRepoDataSchema,
|
|
2140
|
-
issue: z6.number(),
|
|
2141
|
-
commentId: z6.number().optional()
|
|
2142
|
-
}).strict();
|
|
2143
|
-
function issueEventData(input) {
|
|
2144
|
-
return issueEventDataSchema.parse(definedValues(input));
|
|
2145
|
-
}
|
|
2146
|
-
var issueCommentWebhookSchema = z6.object({
|
|
2147
|
-
action: z6.string(),
|
|
2148
|
-
comment: commentSchema,
|
|
2149
|
-
issue: z6.object({
|
|
2150
|
-
draft: z6.boolean().optional(),
|
|
2151
|
-
number: z6.number(),
|
|
2152
|
-
pull_request: z6.object({ url: z6.string().min(1) }).optional(),
|
|
2153
|
-
user: gitHubUserSchema.nullish()
|
|
2154
|
-
}),
|
|
2155
|
-
repository: repositorySchema2,
|
|
2156
|
-
sender: gitHubSenderSchema.optional()
|
|
2157
|
-
});
|
|
2158
|
-
function normalizeIssueCommentEvents(deliveryId, body, botEmail) {
|
|
2159
|
-
const parsed = issueCommentWebhookSchema.safeParse(body);
|
|
2160
|
-
if (!parsed.success || parsed.data.action !== "created") return [];
|
|
2161
|
-
const { comment, issue, sender } = parsed.data;
|
|
2162
|
-
const author = comment.user?.login;
|
|
2163
|
-
if (isBotLogin(author, botEmail)) return [];
|
|
2164
|
-
const input = {
|
|
2165
|
-
number: issue.number,
|
|
2166
|
-
repo: parsed.data.repository.full_name
|
|
2167
|
-
};
|
|
2168
|
-
const writtenBy = commentAuthorData({ botEmail, sender, text: comment.body });
|
|
2169
|
-
if (issue.pull_request) {
|
|
2170
|
-
const eventType = "pull_request.comment.created";
|
|
2171
|
-
const resource = gitHubPullRequestResource(input);
|
|
2172
|
-
return pullRequestTargets(
|
|
2173
|
-
{
|
|
2174
|
-
eventKey: gitHubEventKey(deliveryId, eventType),
|
|
2175
|
-
eventType,
|
|
2176
|
-
occurredAtMs: Date.now(),
|
|
2177
|
-
identifier: resource.identifier,
|
|
2178
|
-
trustedSummary: `${resource.label} received a comment${author ? ` from ${author}` : ""}.`,
|
|
2179
|
-
...gitHubDoneBy(author),
|
|
2180
|
-
data: pullRequestEventData({
|
|
2181
|
-
comment: {
|
|
2182
|
-
commentId: comment.id,
|
|
2183
|
-
commentKind: "conversation",
|
|
2184
|
-
...writtenBy
|
|
2185
|
-
},
|
|
2186
|
-
pullRequest: issue,
|
|
2187
|
-
repo: input.repo
|
|
2188
|
-
}),
|
|
2189
|
-
untrustedText: comment.body
|
|
2190
|
-
},
|
|
2191
|
-
input.repo
|
|
2192
|
-
);
|
|
2193
|
-
}
|
|
2194
|
-
const issueResource = gitHubIssueResource(input);
|
|
2195
|
-
const repository = gitHubRepositoryResource(input);
|
|
2196
|
-
const data = issueEventData({
|
|
2197
|
-
repo: input.repo,
|
|
2198
|
-
issue: input.number,
|
|
2199
|
-
commentId: comment.id,
|
|
2200
|
-
...writtenBy
|
|
2201
|
-
});
|
|
2202
|
-
return [issueResource, repository].map((resource) => ({
|
|
2203
|
-
eventKey: gitHubEventKey(deliveryId, "issue.comment.created"),
|
|
2204
|
-
eventType: "issue.comment.created",
|
|
2205
|
-
occurredAtMs: Date.now(),
|
|
2206
|
-
identifier: resource.identifier,
|
|
2207
|
-
trustedSummary: `${issueResource.label} received a comment${author ? ` from ${author}` : ""}.`,
|
|
2208
|
-
...gitHubDoneBy(author),
|
|
2209
|
-
data,
|
|
2210
|
-
untrustedText: comment.body
|
|
2211
|
-
}));
|
|
2212
|
-
}
|
|
2213
|
-
var issueWebhookSchema = z6.object({
|
|
2214
|
-
action: z6.string(),
|
|
2215
|
-
issue: z6.object({
|
|
2216
|
-
body: z6.string().optional().nullable(),
|
|
2217
|
-
closed_at: z6.string().optional().nullable(),
|
|
2218
|
-
created_at: z6.string().optional(),
|
|
2219
|
-
number: z6.number(),
|
|
2220
|
-
title: z6.string().optional(),
|
|
2221
|
-
updated_at: z6.string().optional()
|
|
2222
|
-
}),
|
|
2223
|
-
repository: repositorySchema2
|
|
2224
|
-
});
|
|
2225
|
-
function normalizeIssueEvents(deliveryId, body, botEmail) {
|
|
2226
|
-
const updates = normalizeIssueUpdateEvents(deliveryId, body, botEmail);
|
|
2227
|
-
if (updates) return updates;
|
|
2228
|
-
const parsed = issueWebhookSchema.safeParse(body);
|
|
2229
|
-
if (!parsed.success) return [];
|
|
2230
|
-
const state = parsed.data.action === "opened" ? "opened" : parsed.data.action === "closed" ? "closed" : parsed.data.action === "reopened" ? "reopened" : void 0;
|
|
2231
|
-
if (!state) return [];
|
|
2232
|
-
const input = {
|
|
2233
|
-
number: parsed.data.issue.number,
|
|
2234
|
-
repo: parsed.data.repository.full_name
|
|
2235
|
-
};
|
|
2236
|
-
const issue = gitHubIssueResource(input);
|
|
2237
|
-
const repository = gitHubRepositoryResource(input);
|
|
2238
|
-
const occurredAtMs = providerTime(
|
|
2239
|
-
state === "opened" ? parsed.data.issue.created_at : state === "closed" ? parsed.data.issue.closed_at : parsed.data.issue.updated_at
|
|
2240
|
-
) ?? Date.now();
|
|
2241
|
-
const data = issueEventData({ repo: input.repo, issue: input.number });
|
|
2242
|
-
const untrustedText = titleAndBodyText(parsed.data.issue);
|
|
2243
|
-
return [issue, repository].map((resource) => ({
|
|
2244
|
-
eventKey: gitHubEventKey(deliveryId, `issue.${state}`),
|
|
2245
|
-
eventType: `issue.${state}`,
|
|
2246
|
-
occurredAtMs,
|
|
2247
|
-
identifier: resource.identifier,
|
|
2248
|
-
trustedSummary: `${issue.label} was ${state}.`,
|
|
2249
|
-
data,
|
|
2250
|
-
...untrustedText ? { untrustedText } : void 0
|
|
2251
|
-
}));
|
|
2252
|
-
}
|
|
2253
|
-
var pullRequestReviewCommentWebhookSchema = z6.object({
|
|
2254
|
-
action: z6.string(),
|
|
2255
|
-
comment: commentSchema,
|
|
2256
|
-
pull_request: pullRequestSchema,
|
|
2257
|
-
repository: repositorySchema2,
|
|
2258
|
-
sender: gitHubSenderSchema.optional()
|
|
2259
|
-
});
|
|
2260
|
-
function normalizePullRequestReviewCommentEvent(deliveryId, body, botEmail) {
|
|
2261
|
-
const parsed = pullRequestReviewCommentWebhookSchema.safeParse(body);
|
|
2262
|
-
if (!parsed.success || parsed.data.action !== "created") return [];
|
|
2263
|
-
const { comment, pull_request: pullRequest, sender } = parsed.data;
|
|
2264
|
-
const author = comment.user?.login;
|
|
2265
|
-
if (isBotLogin(author, botEmail)) return [];
|
|
2266
|
-
const eventType = "pull_request.review_comment.created";
|
|
2267
|
-
const repo = parsed.data.repository.full_name;
|
|
2268
|
-
const resource = gitHubPullRequestResource({
|
|
2269
|
-
number: pullRequest.number,
|
|
2270
|
-
repo
|
|
2271
|
-
});
|
|
2272
|
-
return pullRequestTargets(
|
|
2273
|
-
{
|
|
2274
|
-
eventKey: gitHubEventKey(deliveryId, eventType),
|
|
2275
|
-
eventType,
|
|
2276
|
-
occurredAtMs: Date.now(),
|
|
2277
|
-
identifier: resource.identifier,
|
|
2278
|
-
trustedSummary: `${resource.label} received an inline review comment${author ? ` from ${author}` : ""}.`,
|
|
2279
|
-
...gitHubDoneBy(author),
|
|
2280
|
-
data: pullRequestEventData({
|
|
2281
|
-
comment: {
|
|
2282
|
-
commentId: comment.id,
|
|
2283
|
-
commentKind: "review",
|
|
2284
|
-
...commentAuthorData({ botEmail, sender, text: comment.body })
|
|
2285
|
-
},
|
|
2286
|
-
pullRequest,
|
|
2287
|
-
repo
|
|
2288
|
-
}),
|
|
2289
|
-
untrustedText: comment.body
|
|
2290
|
-
},
|
|
2291
|
-
repo
|
|
2292
|
-
);
|
|
2293
|
-
}
|
|
2294
|
-
var pullRequestReviewWebhookSchema = z6.object({
|
|
2295
|
-
action: z6.string(),
|
|
2296
|
-
pull_request: pullRequestSchema,
|
|
2297
|
-
repository: repositorySchema2,
|
|
2298
|
-
review: z6.object({
|
|
2299
|
-
body: z6.string().optional().nullable(),
|
|
2300
|
-
// Only the feedback reaction needs the id, so a review without one is
|
|
2301
|
-
// still delivered.
|
|
2302
|
-
id: z6.number().optional(),
|
|
2303
|
-
state: z6.string(),
|
|
2304
|
-
user: z6.object({ login: z6.string().optional() }).optional()
|
|
2305
|
-
}),
|
|
2306
|
-
sender: gitHubSenderSchema.optional()
|
|
2307
|
-
});
|
|
2308
|
-
function normalizePullRequestReviewEvent(deliveryId, body, botEmail) {
|
|
2309
|
-
const parsed = pullRequestReviewWebhookSchema.safeParse(body);
|
|
2310
|
-
if (!parsed.success || parsed.data.action !== "submitted") return [];
|
|
2311
|
-
const { pull_request: pullRequest, review, sender } = parsed.data;
|
|
2312
|
-
if (isBotLogin(review.user?.login, botEmail)) return [];
|
|
2313
|
-
const reviewState = review.state.toUpperCase();
|
|
2314
|
-
const eventType = reviewState === "APPROVED" ? "pull_request.review.approved" : reviewState === "CHANGES_REQUESTED" ? "pull_request.review.changes_requested" : reviewState === "COMMENTED" ? "pull_request.review.commented" : void 0;
|
|
2315
|
-
if (!eventType) return [];
|
|
2316
|
-
const repo = parsed.data.repository.full_name;
|
|
2317
|
-
const resource = gitHubPullRequestResource({
|
|
2318
|
-
number: pullRequest.number,
|
|
2319
|
-
repo
|
|
2320
|
-
});
|
|
2321
|
-
const reviewer = review.user?.login;
|
|
2322
|
-
const event = {
|
|
2323
|
-
eventKey: gitHubEventKey(deliveryId, eventType),
|
|
2324
|
-
eventType,
|
|
2325
|
-
occurredAtMs: Date.now(),
|
|
2326
|
-
identifier: resource.identifier,
|
|
2327
|
-
trustedSummary: eventType === "pull_request.review.approved" ? `${resource.label} was approved${reviewer ? ` by ${reviewer}` : ""}.` : eventType === "pull_request.review.changes_requested" ? `${resource.label} received requested changes${reviewer ? ` from ${reviewer}` : ""}.` : `${resource.label} received a review comment${reviewer ? ` from ${reviewer}` : ""}.`,
|
|
2328
|
-
...gitHubDoneBy(reviewer),
|
|
2329
|
-
data: pullRequestEventData({
|
|
2330
|
-
comment: {
|
|
2331
|
-
...review.id !== void 0 && nonBlank(review.body) ? { commentId: review.id, commentKind: "review_body" } : void 0,
|
|
2332
|
-
...commentAuthorData({ botEmail, sender, text: review.body })
|
|
2333
|
-
},
|
|
2334
|
-
pullRequest,
|
|
2335
|
-
repo
|
|
2336
|
-
}),
|
|
2337
|
-
untrustedText: review.body ?? void 0
|
|
2338
|
-
};
|
|
2339
|
-
const events = pullRequestTargets(event, repo);
|
|
2340
|
-
if (eventType !== "pull_request.review.approved" || !nonBlank(review.body)) {
|
|
2341
|
-
return events;
|
|
2342
|
-
}
|
|
2343
|
-
return [
|
|
2344
|
-
...events,
|
|
2345
|
-
...pullRequestTargets(
|
|
2346
|
-
{
|
|
2347
|
-
...event,
|
|
2348
|
-
eventKey: gitHubEventKey(deliveryId, "pull_request.review.commented"),
|
|
2349
|
-
eventType: "pull_request.review.commented"
|
|
2350
|
-
},
|
|
2351
|
-
repo
|
|
2352
|
-
)
|
|
2353
|
-
];
|
|
2354
|
-
}
|
|
2355
|
-
var pullRequestWebhookSchema = z6.object({
|
|
2356
|
-
action: z6.string(),
|
|
2357
|
-
pull_request: pullRequestSchema.extend({
|
|
2358
|
-
body: z6.string().optional().nullable(),
|
|
2359
|
-
closed_at: z6.string().optional().nullable(),
|
|
2360
|
-
created_at: z6.string().optional(),
|
|
2361
|
-
merged: z6.boolean().optional(),
|
|
2362
|
-
merged_at: z6.string().optional().nullable(),
|
|
2363
|
-
title: z6.string().optional(),
|
|
2364
|
-
updated_at: z6.string().optional()
|
|
2365
|
-
}),
|
|
2366
|
-
repository: repositorySchema2
|
|
2367
|
-
});
|
|
2368
|
-
function normalizePullRequestEvent(deliveryId, body) {
|
|
2369
|
-
const parsed = pullRequestWebhookSchema.safeParse(body);
|
|
2370
|
-
if (!parsed.success) return [];
|
|
2371
|
-
const { pull_request: pullRequest } = parsed.data;
|
|
2372
|
-
const repo = parsed.data.repository.full_name;
|
|
2373
|
-
const resource = gitHubPullRequestResource({
|
|
2374
|
-
number: pullRequest.number,
|
|
2375
|
-
repo
|
|
2376
|
-
});
|
|
2377
|
-
const data = pullRequestEventData({ pullRequest, repo });
|
|
2378
|
-
const readyData = pullRequestEventData({
|
|
2379
|
-
pullRequest: { ...pullRequest, draft: false },
|
|
2380
|
-
repo
|
|
2381
|
-
});
|
|
2382
|
-
const untrustedText = titleAndBodyText(pullRequest);
|
|
2383
|
-
const lifecycleEvents = (event) => pullRequestTargets(
|
|
2384
|
-
{
|
|
2385
|
-
eventKey: gitHubEventKey(deliveryId, event.eventType),
|
|
2386
|
-
eventType: event.eventType,
|
|
2387
|
-
occurredAtMs: event.occurredAtMs,
|
|
2388
|
-
identifier: resource.identifier,
|
|
2389
|
-
trustedSummary: event.trustedSummary,
|
|
2390
|
-
data: event.isDraft === false ? readyData : data,
|
|
2391
|
-
...event.untrustedText ? { untrustedText: event.untrustedText } : void 0
|
|
2392
|
-
},
|
|
2393
|
-
repo
|
|
2394
|
-
);
|
|
2395
|
-
if (parsed.data.action === "opened") {
|
|
2396
|
-
const openedAtMs = providerTime(pullRequest.created_at) ?? Date.now();
|
|
2397
|
-
const events = lifecycleEvents({
|
|
2398
|
-
eventType: "pull_request.opened",
|
|
2399
|
-
occurredAtMs: openedAtMs,
|
|
2400
|
-
trustedSummary: `${resource.label} was opened.`,
|
|
2401
|
-
untrustedText
|
|
2402
|
-
});
|
|
2403
|
-
if (pullRequest.draft !== true) {
|
|
2404
|
-
events.push(
|
|
2405
|
-
...lifecycleEvents({
|
|
2406
|
-
eventType: "pull_request.ready_for_review",
|
|
2407
|
-
isDraft: false,
|
|
2408
|
-
occurredAtMs: openedAtMs,
|
|
2409
|
-
trustedSummary: `${resource.label} is ready for review.`,
|
|
2410
|
-
untrustedText
|
|
2411
|
-
})
|
|
2412
|
-
);
|
|
2413
|
-
}
|
|
2414
|
-
return events;
|
|
2415
|
-
}
|
|
2416
|
-
if (parsed.data.action === "ready_for_review") {
|
|
2417
|
-
return lifecycleEvents({
|
|
2418
|
-
eventType: "pull_request.ready_for_review",
|
|
2419
|
-
isDraft: false,
|
|
2420
|
-
occurredAtMs: providerTime(pullRequest.updated_at) ?? Date.now(),
|
|
2421
|
-
trustedSummary: `${resource.label} is ready for review.`,
|
|
2422
|
-
untrustedText
|
|
2423
|
-
});
|
|
2424
|
-
}
|
|
2425
|
-
return gitHubPullRequestTerminalEvents(body);
|
|
2426
|
-
}
|
|
2427
|
-
function gitHubPullRequestTerminalEvents(body) {
|
|
2428
|
-
const parsed = pullRequestWebhookSchema.safeParse(body);
|
|
2429
|
-
if (!parsed.success || parsed.data.action !== "closed") return [];
|
|
2430
|
-
const { pull_request: pullRequest } = parsed.data;
|
|
2431
|
-
const repo = parsed.data.repository.full_name;
|
|
2432
|
-
const resource = gitHubPullRequestResource({
|
|
2433
|
-
number: pullRequest.number,
|
|
2434
|
-
repo
|
|
2435
|
-
});
|
|
2436
|
-
const merged = pullRequest.merged === true;
|
|
2437
|
-
const eventType = merged ? "pull_request.merged" : "pull_request.closed_unmerged";
|
|
2438
|
-
const occurredAtMs = providerTime(merged ? pullRequest.merged_at : pullRequest.closed_at) ?? providerTime(pullRequest.updated_at) ?? Date.now();
|
|
2439
|
-
return pullRequestTargets(
|
|
2440
|
-
{
|
|
2441
|
-
// A pull request merges once. It can close, reopen, and close again,
|
|
2442
|
-
// so the key of a close also has its time.
|
|
2443
|
-
eventKey: merged ? `github:${resource.identifier}:${eventType}` : `github:${resource.identifier}:${eventType}:${occurredAtMs}`,
|
|
2444
|
-
eventType,
|
|
2445
|
-
occurredAtMs,
|
|
2446
|
-
identifier: resource.identifier,
|
|
2447
|
-
terminal: true,
|
|
2448
|
-
trustedSummary: merged ? `${resource.label} was merged.` : `${resource.label} was closed without being merged.`,
|
|
2449
|
-
data: pullRequestEventData({ pullRequest, repo })
|
|
2450
|
-
},
|
|
2451
|
-
repo
|
|
2452
|
-
);
|
|
2453
|
-
}
|
|
2454
|
-
var releaseWebhookSchema = z6.object({
|
|
2455
|
-
action: z6.string(),
|
|
2456
|
-
release: z6.object({
|
|
2457
|
-
body: z6.string().optional().nullable(),
|
|
2458
|
-
draft: z6.boolean().optional(),
|
|
2459
|
-
id: z6.number(),
|
|
2460
|
-
name: z6.string().optional().nullable(),
|
|
2461
|
-
prerelease: z6.boolean().optional(),
|
|
2462
|
-
published_at: z6.string().optional().nullable(),
|
|
2463
|
-
tag_name: z6.string().min(1)
|
|
2464
|
-
}).passthrough(),
|
|
2465
|
-
repository: repositorySchema2
|
|
2466
|
-
}).passthrough();
|
|
2467
|
-
var releaseEventDataSchema = z6.object({
|
|
2468
|
-
repo: gitHubRepoDataSchema,
|
|
2469
|
-
tag: gitHubReleaseTagDataSchema
|
|
2470
|
-
}).strict();
|
|
2471
|
-
function releaseSourceTargets(input) {
|
|
2472
|
-
return [
|
|
2473
|
-
{
|
|
2474
|
-
completeOnTerminalEvent: true,
|
|
2475
|
-
resource: gitHubReleaseSourceResource(input)
|
|
2476
|
-
},
|
|
2477
|
-
{
|
|
2478
|
-
completeOnTerminalEvent: false,
|
|
2479
|
-
resource: gitHubReleaseSourceResource({ repo: input.repo })
|
|
2480
|
-
}
|
|
2481
|
-
];
|
|
2482
|
-
}
|
|
2483
|
-
function normalizeReleaseEvent(deliveryId, body) {
|
|
2484
|
-
const parsed = releaseWebhookSchema.safeParse(body);
|
|
2485
|
-
if (!parsed.success || parsed.data.action !== "published") return [];
|
|
2486
|
-
if (parsed.data.release.draft) return [];
|
|
2487
|
-
const eventType = "release.published";
|
|
2488
|
-
const repo = parsed.data.repository.full_name;
|
|
2489
|
-
const tag = parsed.data.release.tag_name.trim();
|
|
2490
|
-
if (!tag) return [];
|
|
2491
|
-
const untrustedParts = [
|
|
2492
|
-
`Tag: ${tag}`,
|
|
2493
|
-
parsed.data.release.name?.trim() ? `Name: ${parsed.data.release.name.trim()}` : void 0,
|
|
2494
|
-
parsed.data.release.body?.trim() || void 0
|
|
2495
|
-
].filter((part) => part !== void 0);
|
|
2496
|
-
const untrustedText = untrustedParts.length > 0 ? untrustedParts.join("\n\n") : void 0;
|
|
2497
|
-
const data = releaseEventDataSchema.parse({ repo, tag });
|
|
2498
|
-
return releaseSourceTargets({ repo, tag }).map(
|
|
2499
|
-
({ completeOnTerminalEvent, resource }) => ({
|
|
2500
|
-
eventKey: gitHubEventKey(deliveryId, eventType),
|
|
2501
|
-
eventType,
|
|
2502
|
-
occurredAtMs: providerTime(parsed.data.release.published_at) ?? Date.now(),
|
|
2503
|
-
identifier: resource.identifier,
|
|
2504
|
-
...completeOnTerminalEvent ? { terminal: true } : void 0,
|
|
2505
|
-
trustedSummary: `${resource.label} was published (release ${parsed.data.release.id}).`,
|
|
2506
|
-
data,
|
|
2507
|
-
...untrustedText ? { untrustedText } : void 0
|
|
2508
|
-
})
|
|
2509
|
-
);
|
|
2510
|
-
}
|
|
2511
|
-
function normalizeGitHubEvents(args) {
|
|
2512
|
-
switch (args.eventName) {
|
|
2513
|
-
case "deployment":
|
|
2514
|
-
return normalizeDeploymentEvent(args.deliveryId, args.body);
|
|
2515
|
-
case "deployment_status":
|
|
2516
|
-
return normalizeDeploymentStatusEvent(args.deliveryId, args.body);
|
|
2517
|
-
case "pull_request":
|
|
2518
|
-
return normalizePullRequestEvent(args.deliveryId, args.body);
|
|
2519
|
-
case "issues":
|
|
2520
|
-
return normalizeIssueEvents(args.deliveryId, args.body, args.botEmail);
|
|
2521
|
-
case "pull_request_review":
|
|
2522
|
-
return normalizePullRequestReviewEvent(
|
|
2523
|
-
args.deliveryId,
|
|
2524
|
-
args.body,
|
|
2525
|
-
args.botEmail
|
|
2526
|
-
);
|
|
2527
|
-
case "issue_comment":
|
|
2528
|
-
return normalizeIssueCommentEvents(
|
|
2529
|
-
args.deliveryId,
|
|
2530
|
-
args.body,
|
|
2531
|
-
args.botEmail
|
|
2532
|
-
);
|
|
2533
|
-
case "pull_request_review_comment":
|
|
2534
|
-
return normalizePullRequestReviewCommentEvent(
|
|
2535
|
-
args.deliveryId,
|
|
2536
|
-
args.body,
|
|
2537
|
-
args.botEmail
|
|
2538
|
-
);
|
|
2539
|
-
case "check_suite":
|
|
2540
|
-
return normalizeCheckSuiteEvents(
|
|
2541
|
-
args.deliveryId,
|
|
2542
|
-
args.body,
|
|
2543
|
-
args.checkSuiteFacts
|
|
2544
|
-
);
|
|
2545
|
-
case "release":
|
|
2546
|
-
return normalizeReleaseEvent(args.deliveryId, args.body);
|
|
2547
|
-
default:
|
|
2548
|
-
return [];
|
|
2549
|
-
}
|
|
2550
|
-
}
|
|
2551
|
-
|
|
2552
|
-
export {
|
|
2553
|
-
normalizePermissions,
|
|
2554
|
-
readGrantPermissions,
|
|
2555
|
-
gitHubRepoDataSchema,
|
|
2556
|
-
GITHUB_ISSUE_EVENTS,
|
|
2557
|
-
GITHUB_ISSUE_SUGGESTED_EVENTS,
|
|
2558
|
-
GITHUB_ISSUE_MATCH_FIELDS,
|
|
2559
|
-
GITHUB_ISSUE_IDENTIFIER,
|
|
2560
|
-
gitHubIssueSubscribable,
|
|
2561
|
-
GITHUB_DEPLOYMENT_EVENTS,
|
|
2562
|
-
GITHUB_DEPLOYMENT_SUGGESTED_EVENTS,
|
|
2563
|
-
GITHUB_DEPLOYMENT_MATCH_FIELDS,
|
|
2564
|
-
GITHUB_DEPLOYMENT_SOURCE_IDENTIFIER,
|
|
2565
|
-
gitHubDeploymentSourceSubscribable,
|
|
2566
|
-
GITHUB_PULL_REQUEST_EVENTS,
|
|
2567
|
-
gitHubPullRequestEventGuidance,
|
|
2568
|
-
gitHubPullRequestSubscription,
|
|
2569
|
-
gitHubPullRequestCreateWatch,
|
|
2570
|
-
GITHUB_PULL_REQUEST_SUGGESTED_EVENTS,
|
|
2571
|
-
GITHUB_PULL_REQUEST_MATCH_FIELDS,
|
|
2572
|
-
GITHUB_PULL_REQUEST_IDENTIFIER,
|
|
2573
|
-
gitHubPullRequestResource,
|
|
2574
|
-
gitHubPullRequestSubscribable,
|
|
2575
|
-
GITHUB_RELEASE_EVENTS,
|
|
2576
|
-
GITHUB_RELEASE_MATCH_FIELDS,
|
|
2577
|
-
GITHUB_RELEASE_SUGGESTED_EVENTS,
|
|
2578
|
-
GITHUB_RELEASE_SOURCE_IDENTIFIER,
|
|
2579
|
-
gitHubReleaseSourceSubscribable,
|
|
2580
|
-
GITHUB_REPOSITORY_IDENTIFIER,
|
|
2581
|
-
GITHUB_REPOSITORY_EVENTS,
|
|
2582
|
-
GITHUB_REPOSITORY_SUGGESTED_EVENTS,
|
|
2583
|
-
gitHubRepositorySubscribable,
|
|
2584
|
-
botLoginFromEmail,
|
|
2585
|
-
isBotLogin,
|
|
2586
|
-
botUserIdFromEmail,
|
|
2587
|
-
GITHUB_APP_ID_ENV,
|
|
2588
|
-
GITHUB_APP_PRIVATE_KEY_ENV,
|
|
2589
|
-
GITHUB_INSTALLATION_ID_ENV,
|
|
2590
|
-
GITHUB_AUTH_TOKEN_ENV,
|
|
2591
|
-
GITHUB_AUTH_TOKEN_PLACEHOLDER,
|
|
2592
|
-
GITHUB_GRAPHQL_RESPONSE_BODY_LIMIT_BYTES,
|
|
2593
|
-
HTTP_READ_METHODS,
|
|
2594
|
-
CREATE_TOOL_ROUTING_GUIDANCE,
|
|
2595
|
-
USER_WRITE_REQUIREMENTS,
|
|
2596
|
-
GitHubRequestError,
|
|
2597
|
-
isRecord,
|
|
2598
|
-
readEnv,
|
|
2599
|
-
requireEnv,
|
|
2600
|
-
normalizeScopeList,
|
|
2601
|
-
githubRequest,
|
|
2602
|
-
githubRepositoryFromUrl,
|
|
2603
|
-
resolveUserAccount,
|
|
2604
|
-
listVerifiedUserEmails,
|
|
2605
|
-
issueInstallationToken,
|
|
2606
|
-
issueGitHubCredential,
|
|
2607
|
-
createPermissionCache,
|
|
2608
|
-
definedValues,
|
|
2609
|
-
pullRequestTargets,
|
|
2610
|
-
logFailOpenFailure,
|
|
2611
|
-
checkSuitePullRequests,
|
|
2612
|
-
parseCheckSuitePublishTargets,
|
|
2613
|
-
wantsCheckSuitePassed,
|
|
2614
|
-
needsCheckSuitePullRequestFacts,
|
|
2615
|
-
loadCheckSuiteFacts,
|
|
2616
|
-
gitHubPullRequestTerminalEvents,
|
|
2617
|
-
normalizeGitHubEvents
|
|
2618
|
-
};
|