Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing slashes in branch names #1281

Merged
merged 2 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions source/ci_source/providers/CodeBuild.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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