Skip to content

Commit

Permalink
Merge pull request #311 from mlabrum/add-vsts-ci-support
Browse files Browse the repository at this point in the history
Add VSTS CI provider
  • Loading branch information
orta committed Jul 26, 2017
2 parents b736b5a + c93cb4c commit 2cefe7d
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ This provides another logical step in your process, through this Danger can help

You can use Danger to codify your teams norms, leaving humans to think about harder problems.

Danger JS currently works with GitHub and Travis CI, Circle CI, Semaphore, Jenkins, Docker Cloud, surf-build, Codeship, Drone, Buildkite, or buddybuild.
Danger JS currently works with GitHub and Travis CI, Circle CI, Semaphore, Jenkins, Docker Cloud, surf-build, Codeship, Drone, Buildkite, buddybuild or Visual Studio Team Services.

## For example?

Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

- Remove the DSL duplication on the `danger` export, it wasn't needed or used. - orta
- Update to TypeScript 2.4.x - orta
- Add support for VSTS CI - mlabrum
- Rename github test static_file to remove `:` from the filename to fix a checkout issue on windows - mlabrum

### 1.0.0
Expand Down
50 changes: 50 additions & 0 deletions source/ci_source/providers/VSTS.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Env, CISource } from "../ci_source"
import { ensureEnvKeysExist } from "../ci_source_helpers"
/**
* ### CI Setup
* You'll need to add a npm build step and set the custom command to "run danger"
*
* Only supports VSTS with github as the repository, danger doesn't yet support VSTS as a repository platform
*
* ### Token Setup
*
* You need to add the `DANGER_GITHUB_API_TOKEN` environment variable
*/
export class VSTS implements CISource {
constructor(private readonly env: Env) {}

get name(): string {
return "Visual Studio Team Services"
}

get isCI(): boolean {
return (
ensureEnvKeysExist(this.env, ["SYSTEM_TEAMFOUNDATIONCOLLECTIONURI", "BUILD_REPOSITORY_PROVIDER"]) &&
this.env.BUILD_REPOSITORY_PROVIDER == "GitHub"
)
}

get isPR(): boolean {
const mustHave = ["BUILD_SOURCEBRANCH", "BUILD_REPOSITORY_PROVIDER", "BUILD_REASON", "BUILD_REPOSITORY_NAME"]

return ensureEnvKeysExist(this.env, mustHave) && this.env.BUILD_REASON == "PullRequest"
}

get pullRequestID(): string {
const match = this.env.BUILD_SOURCEBRANCH.match(/refs\/pull\/([0-9]+)\/merge/)

if (match && match.length > 1) {
return match[1]
}

return ""
}

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

get supportedPlatforms(): string[] {
return ["github"]
}
}
87 changes: 87 additions & 0 deletions source/ci_source/providers/_tests/_vsts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { VSTS } from "../VSTS"
import { getCISourceForEnv } from "../../get_ci_source"

const PRNum = "2398"
const correctEnv = {
SYSTEM_TEAMFOUNDATIONCOLLECTIONURI: "https://test.visualstudio.com/",
BUILD_REPOSITORY_PROVIDER: "GitHub",
BUILD_REASON: "PullRequest",
BUILD_REPOSITORY_NAME: "artsy/eigen",
BUILD_SOURCEBRANCH: `refs/pull/${PRNum}/merge`,
}

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

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

test("does not validate without environment vars", () => {
const vsts = new VSTS({})
expect(vsts.isCI).toBeFalsy()
})

test("does not validate without the repository provider being set to github", () => {
const vsts = new VSTS({ ...correctEnv, BUILD_REPOSITORY_PROVIDER: "VSTS" })
expect(vsts.isCI).toBeFalsy()
})
})

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

test("does not validate without environment vars", () => {
const vsts = new VSTS({})
expect(vsts.isPR).toBeFalsy()
})

const envs = ["BUILD_SOURCEBRANCH", "BUILD_REPOSITORY_PROVIDER", "BUILD_REASON", "BUILD_REPOSITORY_NAME"]
envs.forEach((key: string) => {
let env = { ...correctEnv, [key]: null }

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

it("needs to have a PR number", () => {
let env = { ...correctEnv, BUILD_SOURCEBRANCH: null }
const vsts = new VSTS(env)
expect(vsts.isPR).toBeFalsy()
})

it("validates with the correct build reason", () => {
const vsts = new VSTS({ ...correctEnv, BUILD_REASON: "PullRequest" })
expect(vsts.isPR).toBeTruthy()
})

it("does not validate without the correct build reason", () => {
const vsts = new VSTS({ ...correctEnv, BUILD_REASON: "Unknown" })
expect(vsts.isPR).toBeFalsy()
})
})

describe(".pullRequestID", () => {
it("pulls it out of the env", () => {
const vsts = new VSTS(correctEnv)
expect(vsts.pullRequestID).toEqual(PRNum)
})
})

describe(".repoSlug", () => {
it("pulls it out of the env", () => {
const vsts = new VSTS(correctEnv)
expect(vsts.repoSlug).toEqual("artsy/eigen")
})
})
16 changes: 15 additions & 1 deletion source/ci_source/providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Codeship } from "./Codeship"
import { Drone } from "./Drone"
import { Buildkite } from "./Buildkite"
import { BuddyBuild } from "./BuddyBuild"
import { VSTS } from "./VSTS"

const providers = [
Travis,
Expand All @@ -22,9 +23,22 @@ const providers = [
Drone,
Buildkite,
BuddyBuild,
VSTS,
]

// Mainly used for Dangerfile linting
const realProviders = [Travis, Circle, Semaphore, Jenkins, Surf, DockerCloud, Codeship, Drone, Buildkite, BuddyBuild]
const realProviders = [
Travis,
Circle,
Semaphore,
Jenkins,
Surf,
DockerCloud,
Codeship,
Drone,
Buildkite,
BuddyBuild,
VSTS,
]

export { providers, realProviders }

0 comments on commit 2cefe7d

Please sign in to comment.