Skip to content

Commit

Permalink
Merge pull request #1281 from OJPARKINSON/main
Browse files Browse the repository at this point in the history
Allowing slashes in branch names
  • Loading branch information
orta committed May 31, 2022
2 parents b822b0b + 5fe3da1 commit 630f2a4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
6 changes: 3 additions & 3 deletions source/ci_source/providers/CodeBuild.ts
Expand Up @@ -66,13 +66,13 @@ export class CodeBuild implements CISource {

private async _getPrId(): Promise<string> {
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) {
Expand Down
16 changes: 14 additions & 2 deletions source/ci_source/providers/_tests/_codebuild.test.ts
Expand Up @@ -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)
Expand All @@ -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")
Expand All @@ -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",
Expand Down

0 comments on commit 630f2a4

Please sign in to comment.