Skip to content

Commit 9f8bf5f

Browse files
AlexTugarevroboquat
authored andcommitted
[server][github] record "pull" webhook events
1 parent 9cc9c35 commit 9f8bf5f

File tree

1 file changed

+43
-9
lines changed

1 file changed

+43
-9
lines changed

components/server/ee/src/prebuilds/github-app.ts

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,17 @@ export class GithubApp {
181181
options.getRouter("/reconfigure").get(
182182
"/",
183183
asyncHandler(async (req: express.Request, res: express.Response) => {
184-
const gh = await app.auth();
185-
const data = await gh.apps.getAuthenticated();
186-
const slug = data.data.slug;
187-
188-
const state = req.query.state;
189-
res.redirect(`https://github.com/apps/${slug}/installations/new?state=${state}`);
184+
try {
185+
const gh = await app.auth();
186+
const data = await gh.apps.getAuthenticated();
187+
const slug = data.data.slug;
188+
189+
const state = req.query.state;
190+
res.redirect(`https://github.com/apps/${slug}/installations/new?state=${state}`);
191+
} catch (error) {
192+
console.error(error, { error });
193+
res.status(500).send("GitHub App is not configured.");
194+
}
190195
}),
191196
);
192197
options.getRouter &&
@@ -398,6 +403,12 @@ export class GithubApp {
398403
const span = TraceContext.startSpan("GithubApp.handlePullRequest", {});
399404
span.setTag("request", ctx.id);
400405

406+
const event = await this.webhookEvents.createEvent({
407+
type: ctx.name,
408+
status: "received",
409+
rawEvent: JSON.stringify(ctx.payload),
410+
});
411+
401412
try {
402413
const installationId = ctx.payload.installation?.id;
403414
const cloneURL = ctx.payload.repository.clone_url;
@@ -423,14 +434,37 @@ export class GithubApp {
423434
user = r.user;
424435
project = r.project;
425436

426-
const prebuildStartPromise = await this.onPrStartPrebuild({ span }, ctx, config, context, user, project);
427-
if (prebuildStartPromise) {
428-
await this.onPrAddCheck({ span }, config, ctx, prebuildStartPromise);
437+
await this.webhookEvents.updateEvent(event.id, {
438+
authorizedUserId: user.id,
439+
projectId: project?.id,
440+
cloneUrl: context.repository.cloneUrl,
441+
branch: context.ref,
442+
commit: context.revision,
443+
});
444+
445+
const prebuildStartResult = await this.onPrStartPrebuild({ span }, ctx, config, context, user, project);
446+
if (prebuildStartResult) {
447+
await this.webhookEvents.updateEvent(event.id, {
448+
prebuildStatus: "prebuild_triggered",
449+
status: "processed",
450+
prebuildId: prebuildStartResult.prebuildId,
451+
});
452+
453+
await this.onPrAddCheck({ span }, config, ctx, prebuildStartResult);
429454
this.onPrAddBadge(config, ctx);
430455
await this.onPrAddComment(config, ctx);
456+
} else {
457+
await this.webhookEvents.updateEvent(event.id, {
458+
prebuildStatus: "ignored_unconfigured",
459+
status: "processed",
460+
});
431461
}
432462
} catch (e) {
433463
TraceContext.setError({ span }, e);
464+
await this.webhookEvents.updateEvent(event.id, {
465+
prebuildStatus: "prebuild_trigger_failed",
466+
status: "processed",
467+
});
434468
throw e;
435469
} finally {
436470
span.finish();

0 commit comments

Comments
 (0)