-
-
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.
- Loading branch information
Showing
5 changed files
with
146 additions
and
5 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,57 @@ | ||
import { Env, CISource } from "../ci_source" | ||
import { ensureEnvKeysExist, ensureEnvKeysAreInt } from "../ci_source_helpers" | ||
|
||
/** | ||
* ### CI Setup | ||
* | ||
* With BuildKite you run the server yourself, so you will want to run it as a part of your build process. | ||
* It is common to have build steps, so we would recommend adding this to your scrip: | ||
* | ||
* ``` shell | ||
* echo "--- Running Danger" | ||
* bundle exec danger | ||
* ``` | ||
* | ||
* ### Token Setup | ||
* | ||
* #### GitHub | ||
* | ||
* As this is self-hosted, you will need to add the `DANGER_GITHUB_API_TOKEN` to your build user's ENV. The alternative | ||
* is to pass in the token as a prefix to the command `DANGER_GITHUB_API_TOKEN="123" bundle exec danger`. | ||
*/ | ||
export class Screwdriver implements CISource { | ||
constructor(private readonly env: Env) {} | ||
|
||
get name(): string { | ||
return "Screwdriver" | ||
} | ||
|
||
get isCI(): boolean { | ||
return ensureEnvKeysExist(this.env, ["SCREWDRIVER"]) | ||
} | ||
|
||
get isPR(): boolean { | ||
const mustHave = ["SCM_URL"] | ||
const mustBeInts = ["SD_PULL_REQUEST"] | ||
return ensureEnvKeysExist(this.env, mustHave) && ensureEnvKeysAreInt(this.env, mustBeInts) | ||
} | ||
|
||
private _parseRepoURL(): string { | ||
const repoURL = this.env.SCM_URL | ||
const regexp = new RegExp("([/:])([^/]+/[^/.]+)(?:.git)?$") | ||
const matches = repoURL.match(regexp) | ||
return matches ? matches[2] : "" | ||
} | ||
|
||
get pullRequestID(): string { | ||
return this.env.SD_PULL_REQUEST | ||
} | ||
|
||
get repoSlug(): string { | ||
return this._parseRepoURL() | ||
} | ||
|
||
get ciRunURL() { | ||
return process.env.BUILDKITE_BUILD_URL | ||
} | ||
} |
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,75 @@ | ||
import { Screwdriver } from "../Screwdriver" | ||
import { getCISourceForEnv } from "../../get_ci_source" | ||
|
||
const correctEnv = { | ||
SCREWDRIVER: "true", | ||
SD_PULL_REQUEST: "42", | ||
SCM_URL: "git@github.com:danger/danger-js", | ||
} | ||
|
||
describe("being found when looking for CI", () => { | ||
it("finds Screwdriver with the right ENV", () => { | ||
const ci = getCISourceForEnv(correctEnv) | ||
expect(ci).toBeInstanceOf(Screwdriver) | ||
}) | ||
}) | ||
|
||
describe(".isCI", () => { | ||
it("validates when SCREWDRIVER is present in environment", () => { | ||
const screwdriver = new Screwdriver(correctEnv) | ||
expect(screwdriver.isCI).toBeTruthy() | ||
}) | ||
|
||
it("does not validate without SCREWDRIVER present in environment", () => { | ||
const screwdriver = new Screwdriver({}) | ||
expect(screwdriver.isCI).toBeFalsy() | ||
}) | ||
}) | ||
|
||
describe(".isPR", () => { | ||
it("validates when all Screwdriver environment variables are set", () => { | ||
const screwdriver = new Screwdriver(correctEnv) | ||
expect(screwdriver.isPR).toBeTruthy() | ||
}) | ||
|
||
it("does not validate with required environment variables", () => { | ||
const screwdriver = new Screwdriver({}) | ||
expect(screwdriver.isPR).toBeFalsy() | ||
}) | ||
|
||
const envs = ["SD_PULL_REQUEST", "SCM_URL"] | ||
envs.forEach((key: string) => { | ||
const env = { | ||
...correctEnv, | ||
[key]: null, | ||
} | ||
|
||
it(`does not validate when ${key} is missing`, () => { | ||
const screwdriver = new Screwdriver(env) | ||
expect(screwdriver.isPR).toBeFalsy() | ||
}) | ||
}) | ||
|
||
it("needs to have a PR number", () => { | ||
const env = { | ||
...correctEnv, | ||
SD_PULL_REQUEST: "not a number", | ||
} | ||
const screwdriver = new Screwdriver(env) | ||
expect(screwdriver.isPR).toBeFalsy() | ||
}) | ||
}) | ||
|
||
describe(".pullRequestID", () => { | ||
it("pulls it out of environment", () => { | ||
const screwdriver = new Screwdriver(correctEnv) | ||
expect(screwdriver.pullRequestID).toEqual("42") | ||
}) | ||
}) | ||
|
||
describe(".repoSlug", () => { | ||
it("pulls it out of environment", () => { | ||
const screwdriver = new Screwdriver(correctEnv) | ||
expect(screwdriver.repoSlug).toEqual("danger/danger-js") | ||
}) | ||
}) |
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