From f0b3ea6927c1cc23400d15d407142befefa79f36 Mon Sep 17 00:00:00 2001 From: Jan Keromnes Date: Thu, 2 Jun 2022 09:53:13 +0000 Subject: [PATCH] [server] When GitHub (Enterprise) sends webhooks without a host header, fall back to the hostname from the repository URL Fixes "Unsupported GitHub Enterprise host: undefined" --- .../server/ee/src/prebuilds/github-enterprise-app.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/components/server/ee/src/prebuilds/github-enterprise-app.ts b/components/server/ee/src/prebuilds/github-enterprise-app.ts index 8c68ae42dde3af..97c6eda32d65dc 100644 --- a/components/server/ee/src/prebuilds/github-enterprise-app.ts +++ b/components/server/ee/src/prebuilds/github-enterprise-app.ts @@ -77,9 +77,14 @@ export class GitHubEnterpriseApp { ): Promise { const span = TraceContext.startSpan("GitHubEnterpriseApp.findUser", ctx); try { - const host = req.header("X-Github-Enterprise-Host"); + let host = req.header("X-Github-Enterprise-Host"); + if (!host) { + // If the GitHub installation doesn't identify itself, we fall back to the hostname from the repository URL. + const repoUrl = new URL(payload.repository.url); + host = repoUrl.hostname; + } const hostContext = this.hostContextProvider.get(host || ""); - if (!host || !hostContext) { + if (!hostContext) { throw new Error("Unsupported GitHub Enterprise host: " + host); } const { authProviderId } = hostContext.authProvider;