Skip to content

Commit

Permalink
feat(ci_source): add Buddy.works
Browse files Browse the repository at this point in the history
  • Loading branch information
kristofdombi committed Sep 2, 2019
1 parent 58ec25f commit 8664c19
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
35 changes: 35 additions & 0 deletions source/ci_source/providers/BuddyWorks.ts
@@ -0,0 +1,35 @@
import { Env, CISource } from "../ci_source"
import { ensureEnvKeysExist, ensureEnvKeysAreInt } from "../ci_source_helpers"
// TODO add Setup comments
export class BuddyWorks implements CISource {
constructor(private readonly env: Env) {}

get name(): string {
return "Buddy.works"
}

get isCI(): boolean {
return ensureEnvKeysExist(this.env, ["BUDDY_PIPELINE_ID"])
}

get isPR(): boolean {
const mustHave = ["BUDDY_PIPELINE_ID", "BUDDY_EXECUTION_PULL_REQUEST_ID", "BUDDY_REPO_SLUG"]
const mustBeInts = ["BUDDY_EXECUTION_PULL_REQUEST_ID"]
return ensureEnvKeysExist(this.env, mustHave) && ensureEnvKeysAreInt(this.env, mustBeInts)
}

get pullRequestID(): string {
return this.env.BUDDY_EXECUTION_PULL_REQUEST_ID
}

get repoSlug(): string {
return this.env.BUDDY_REPO_SLUG
}

get ciRunURL() {
return this.env.BUDDY_EXECUTION_URL
}
}

// Default ENV vars provided by Buddy.works
// https://buddy.works/docs/pipelines/environment-variables#default-environment-variables
82 changes: 82 additions & 0 deletions source/ci_source/providers/_tests/_buddyWorks.test.ts
@@ -0,0 +1,82 @@
import { BuddyWorks } from "../BuddyWorks"
import { getCISourceForEnv } from "../../get_ci_source"

const correctEnv = {
BUDDY_PIPELINE_ID: "170873",
BUDDY_EXECUTION_PULL_REQUEST_ID: "1799",
BUDDY_REPO_SLUG: "danger/dangerjs",
BUDDY_EXECUTION_URL:
"https://app.buddy.works/danger/dangerjs/pipelines/pipeline/170873/execution/5d6d49dbaab2cb6fdf975c71",
}

describe("being found when looking for CI", () => {
it("finds Buddy.works with the right ENV", () => {
const ci = getCISourceForEnv(correctEnv)
expect(ci).toBeInstanceOf(BuddyWorks)
})
})

describe(".isCI", () => {
test("validates when all Buddy.works environment vars are set", () => {
const buddyWorks = new BuddyWorks(correctEnv)
expect(buddyWorks.isCI).toBeTruthy()
})

test("does not validate without pipeline ID", () => {
const buddyWorks = new BuddyWorks({})
expect(buddyWorks.isCI).toBeFalsy()
})
})

describe(".isPR", () => {
test("validates when all Buddy.works environment vars are set", () => {
const buddyWorks = new BuddyWorks(correctEnv)
expect(buddyWorks.isPR).toBeTruthy()
})

test("does not validate without pipeline ID", () => {
const buddyWorks = new BuddyWorks({})
expect(buddyWorks.isPR).toBeFalsy()
})

const envs = ["BUDDY_EXECUTION_PULL_REQUEST_ID", "BUDDY_REPO_SLUG"]
envs.forEach((key: string) => {
const env = Object.assign({}, correctEnv)
env[key] = null

test(`does not validate when ${key} is missing`, () => {
const buddyWorks = new BuddyWorks(env)
expect(buddyWorks.isPR).toBeFalsy()
})
})

it("needs to have a PR number", () => {
const env = Object.assign({}, correctEnv)
delete env.BUDDY_EXECUTION_PULL_REQUEST_ID
const buddyWorks = new BuddyWorks(env)
expect(buddyWorks.isPR).toBeFalsy()
})
})

describe(".pullRequestID", () => {
it("pulls it out of the env", () => {
const buddyWorks = new BuddyWorks(correctEnv)
expect(buddyWorks.pullRequestID).toEqual("1799")
})
})

describe(".repoSlug", () => {
it("pulls it out of the env", () => {
const buddyWorks = new BuddyWorks(correctEnv)
expect(buddyWorks.repoSlug).toEqual("danger/dangerjs")
})
})

describe(".ciRunURL", () => {
it("pulls it out of the env", () => {
const buddyWorks = new BuddyWorks(correctEnv)
expect(buddyWorks.ciRunURL).toEqual(
"https://app.buddy.works/danger/dangerjs/pipelines/pipeline/170873/execution/5d6d49dbaab2cb6fdf975c71"
)
})
})
3 changes: 3 additions & 0 deletions source/ci_source/providers/index.ts
@@ -1,6 +1,7 @@
import { AppCenter } from "./AppCenter"
import { Bitrise } from "./Bitrise"
import { BuddyBuild } from "./BuddyBuild"
import { BuddyWorks } from "./BuddyWorks"
import { Buildkite } from "./Buildkite"
import { Circle } from "./Circle"
import { CodeBuild } from "./CodeBuild"
Expand Down Expand Up @@ -38,6 +39,7 @@ const providers = [
Drone,
Buildkite,
BuddyBuild,
BuddyWorks,
VSTS,
Bitrise,
TeamCity,
Expand Down Expand Up @@ -65,6 +67,7 @@ const realProviders = [
Drone,
Buildkite,
BuddyBuild,
BuddyWorks,
VSTS,
TeamCity,
Screwdriver,
Expand Down

0 comments on commit 8664c19

Please sign in to comment.