Listen to GitHub webhooks to update issueSources cache (#9414)

* Listen to GitHub webhooks to update issue-sources cache

* Add `GitHubWebhookTask`

* review
This commit is contained in:
Hemachandar
2025-07-16 08:37:14 +05:30
committed by GitHub
parent cd83f41294
commit 7d315288dd
13 changed files with 397 additions and 8 deletions
+33
View File
@@ -6,10 +6,13 @@ import apexAuthRedirect from "@server/middlewares/apexAuthRedirect";
import auth from "@server/middlewares/authentication";
import { transaction } from "@server/middlewares/transaction";
import validate from "@server/middlewares/validate";
import validateWebhook from "@server/middlewares/validateWebhook";
import { IntegrationAuthentication, Integration } from "@server/models";
import { APIContext } from "@server/types";
import { GitHubUtils } from "../../shared/GitHubUtils";
import env from "../env";
import { GitHub } from "../github";
import GitHubWebhookTask from "../tasks/GitHubWebhookTask";
import * as T from "./schema";
const router = new Router();
@@ -60,11 +63,16 @@ router.get(
return ctx.redirect(GitHubUtils.errorUrl("unauthenticated"));
}
const scopes = Object.entries(installation.permissions).map(
([name, permission]) => `${name}:${permission}`
);
const authentication = await IntegrationAuthentication.create(
{
service: IntegrationService.GitHub,
userId: user.id,
teamId: user.teamId,
scopes,
},
{ transaction }
);
@@ -92,4 +100,29 @@ router.get(
}
);
router.post(
"github.webhooks",
validateWebhook({
secretKey: env.GITHUB_WEBHOOK_SECRET!,
getSignatureFromHeader: (ctx) => {
const { headers } = ctx.request;
const signatureHeader = headers["x-hub-signature-256"];
const signature = Array.isArray(signatureHeader)
? signatureHeader[0]
: signatureHeader;
return signature?.split("=")[1];
},
}),
async (ctx: APIContext) => {
const { headers, body } = ctx.request;
await new GitHubWebhookTask().schedule({
payload: body,
headers,
});
ctx.status = 202;
}
);
export default router;