From fe1818b97d9083c1becf0bcd45851ca5623867ea Mon Sep 17 00:00:00 2001 From: Oliver Parkinson Date: Mon, 30 May 2022 22:23:27 +0100 Subject: [PATCH 1/2] removed spliting logic on CODEBUILD_WEBHOOK_TRIGGER so that branch names with / still work --- source/ci_source/providers/CodeBuild.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/ci_source/providers/CodeBuild.ts b/source/ci_source/providers/CodeBuild.ts index b3419ba9f..4dd788402 100644 --- a/source/ci_source/providers/CodeBuild.ts +++ b/source/ci_source/providers/CodeBuild.ts @@ -66,13 +66,13 @@ export class CodeBuild implements CISource { private async _getPrId(): Promise { const sourceParts = (this.env.CODEBUILD_SOURCE_VERSION || "").split("/") - const triggerParts = (this.env.CODEBUILD_WEBHOOK_TRIGGER || "").split("/") + const triggerParts = this.env.CODEBUILD_WEBHOOK_TRIGGER || "" - const branchName = triggerParts[0] === "branch" ? triggerParts[1] : null + const branchName = triggerParts.startsWith("branch/") ? triggerParts.replace("branch/", "") : null let prId = sourceParts[0] === "pr" ? sourceParts[1] : null if (!prId) { - prId = triggerParts[0] === "pr" ? triggerParts[1] : null + prId = triggerParts.startsWith("pr/") ? triggerParts.replace("pr/", "") : null } if (!prId && branchName) { From 5fe3da1698e1e13f1c3a6bad6b39775f29ac32ae Mon Sep 17 00:00:00 2001 From: Oliver Parkinson Date: Mon, 30 May 2022 23:08:23 +0100 Subject: [PATCH 2/2] added some testing to support the edge case --- .../providers/_tests/_codebuild.test.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/source/ci_source/providers/_tests/_codebuild.test.ts b/source/ci_source/providers/_tests/_codebuild.test.ts index 26e12889a..1e0faac01 100644 --- a/source/ci_source/providers/_tests/_codebuild.test.ts +++ b/source/ci_source/providers/_tests/_codebuild.test.ts @@ -51,7 +51,7 @@ describe(".isPR", () => { expect(codebuild.isPR).toBeFalsy() }) - it.each(["CODEBUILD_BUILD_ID", "CODEBUILD_SOURCE_REPO_URL"])(`does not validate when %s is missing`, async key => { + it.each(["CODEBUILD_BUILD_ID", "CODEBUILD_SOURCE_REPO_URL"])(`does not validate when %s is missing`, async (key) => { const copiedEnv = { ...correctEnv } delete copiedEnv[key] const codebuild = await setupCodeBuildSource(copiedEnv) @@ -71,7 +71,7 @@ describe(".pullRequestID", () => { jest.resetAllMocks() }) - it.each(["CODEBUILD_SOURCE_VERSION", "CODEBUILD_WEBHOOK_TRIGGER"])("splits it from %s", async key => { + it.each(["CODEBUILD_SOURCE_VERSION", "CODEBUILD_WEBHOOK_TRIGGER"])("splits it from %s", async (key) => { const codebuild = await setupCodeBuildSource({ [key]: "pr/2" }) await codebuild.setup() expect(codebuild.pullRequestID).toEqual("2") @@ -90,6 +90,18 @@ describe(".pullRequestID", () => { expect(getPullRequestIDForBranch).toHaveBeenCalledWith(codebuild, env, "my-branch") }) + it('allows for branch names with "/" in them', async () => { + const env = { + CODEBUILD_SOURCE_REPO_URL: "https://github.com/sharkysharks/some-repo", + CODEBUILD_WEBHOOK_TRIGGER: "branch/my-branch/with/slashes", + DANGER_GITHUB_API_TOKEN: "xxx", + } + const codebuild = await setupCodeBuildSource(env) + expect(codebuild.pullRequestID).toBe("0") + expect(getPullRequestIDForBranch).toHaveBeenCalledTimes(1) + expect(getPullRequestIDForBranch).toHaveBeenCalledWith(codebuild, env, "my-branch/with/slashes") + }) + it("does not call the API if no PR number or branch name available in the env vars", async () => { const env = { CODEBUILD_SOURCE_REPO_URL: "https://github.com/sharkysharks/some-repo",