From e4644ecbf7a75eda7496003b48051dc60ff85b96 Mon Sep 17 00:00:00 2001 From: Hagen Date: Tue, 1 Feb 2022 09:32:56 +0000 Subject: [PATCH 1/2] fix: non-github trigger errors --- src/pr-trigger.ts | 6 +++--- src/util/verify.ts | 14 +++++++------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/pr-trigger.ts b/src/pr-trigger.ts index b38d779c..0f18dd25 100644 --- a/src/pr-trigger.ts +++ b/src/pr-trigger.ts @@ -36,13 +36,13 @@ class IgnoredBecause { export async function httpTrigger(context: Context, req: HttpRequest) { httpLog(context, req); - const { headers, body } = req, githubId = headers["x-github-delivery"]; - const evName = headers["x-github-event"], evAction = body.action; - if (!(await shouldRunRequest(context, req))) { return reply(context, 204, "Can't handle this request"); } + const { headers, body } = req, githubId = headers["x-github-delivery"]; + const evName = headers["x-github-event"], evAction = body.action; + if (evName === "check_run" && evAction === "completed") { context.log(`>>>>>> name: ${body?.check_run?.name}, sha: ${body?.check_run?.head_sha}`); if (body?.check_run?.head_sha && body?.repository?.full_name === "DefinitelyTyped/DefinitelyTyped") { diff --git a/src/util/verify.ts b/src/util/verify.ts index 6b42c808..921a032d 100644 --- a/src/util/verify.ts +++ b/src/util/verify.ts @@ -5,7 +5,7 @@ export async function httpLog(context: Context, req: HttpRequest) { const { headers, body } = req; const githubId = headers["x-github-delivery"]; const event = headers["x-github-event"]!; - const action = body.action; + const action = body?.action; context.log(`>>> HTTP Trigger ${context.executionContext.functionName} [${ event}.${action }; gh: ${githubId @@ -15,11 +15,6 @@ export async function httpLog(context: Context, req: HttpRequest) { export async function shouldRunRequest(context: Context, req: HttpRequest, canHandleRequest?: (event: string, action: string) => boolean) { const isDev = process.env.AZURE_FUNCTIONS_ENVIRONMENT === "Development"; - const { headers, body } = req; - - const event = headers["x-github-event"]!; - const action = body.action; - // For process.env.GITHUB_WEBHOOK_SECRET see // https://ms.portal.azure.com/#blade/WebsitesExtension/FunctionsIFrameBlade/id/%2Fsubscriptions%2F57bfeeed-c34a-4ffd-a06b-ccff27ac91b8%2FresourceGroups%2Fdtmergebot%2Fproviders%2FMicrosoft.Web%2Fsites%2FDTMergeBot const fromGitHub = await verifyIsFromGitHub(req); @@ -28,6 +23,10 @@ export async function shouldRunRequest(context: Context, req: HttpRequest, canHa return false; } + const { headers, body } = req; + const event = headers["x-github-event"]!; + const action = body.action; + // Optional function for early bailing if it returns false if (canHandleRequest && !canHandleRequest(event, action)) { context.log("canHandleRequest returned false"); @@ -39,10 +38,11 @@ export async function shouldRunRequest(context: Context, req: HttpRequest, canHa export async function verifyIsFromGitHub(req: HttpRequest) { + if (!req.headers["x-hub-signature-256"] || !req.rawBody) return false; const secret = process.env.GITHUB_WEBHOOK_SECRET; // For process.env.GITHUB_WEBHOOK_SECRET see // https://ms.portal.azure.com/#blade/WebsitesExtension/FunctionsIFrameBlade/id/%2Fsubscriptions%2F57bfeeed-c34a-4ffd-a06b-ccff27ac91b8%2FresourceGroups%2Fdtmergebot%2Fproviders%2FMicrosoft.Web%2Fsites%2FDTMergeBot - return await verify(secret!, req.rawBody, req.headers["x-hub-signature-256"]!); + return await verify(secret!, req.rawBody, req.headers["x-hub-signature-256"]); } From 21beaac1ffc91efe12225da463045242601099ea Mon Sep 17 00:00:00 2001 From: Hagen Date: Tue, 1 Feb 2022 09:50:25 +0000 Subject: [PATCH 2/2] remove unnecessary optional chaining --- src/pr-trigger.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/pr-trigger.ts b/src/pr-trigger.ts index 0f18dd25..002d6747 100644 --- a/src/pr-trigger.ts +++ b/src/pr-trigger.ts @@ -44,9 +44,9 @@ export async function httpTrigger(context: Context, req: HttpRequest) { const evName = headers["x-github-event"], evAction = body.action; if (evName === "check_run" && evAction === "completed") { - context.log(`>>>>>> name: ${body?.check_run?.name}, sha: ${body?.check_run?.head_sha}`); - if (body?.check_run?.head_sha && body?.repository?.full_name === "DefinitelyTyped/DefinitelyTyped") { - const pr = await runQueryToGetPRMetadataForSHA1("DefinitelyTyped", "DefinitelyTyped", body?.check_run?.head_sha); + context.log(`>>>>>> name: ${body.check_run?.name}, sha: ${body.check_run?.head_sha}`); + if (body.check_run?.head_sha && body.repository?.full_name === "DefinitelyTyped/DefinitelyTyped") { + const pr = await runQueryToGetPRMetadataForSHA1("DefinitelyTyped", "DefinitelyTyped", body.check_run?.head_sha); if (pr) { context.log(`>>>>>>>>> pr => num: ${pr.number}, title: "${pr.title}" closed: ${pr.closed}`); } else {