-
-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #311 from mlabrum/add-vsts-ci-support
Add VSTS CI provider
- Loading branch information
Showing
5 changed files
with
154 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters