Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master' into feature/gitlab
- Loading branch information
Showing
7 changed files
with
173 additions
and
12 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
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,69 @@ | ||
import { Env, CISource } from "../ci_source" | ||
import { ensureEnvKeysExist, ensureEnvKeysAreInt } from "../ci_source_helpers" | ||
|
||
/** | ||
* ### CI Setup | ||
* | ||
* To set up Danger on Codefresh, create a freestyle step in your Codefresh yaml configuration: | ||
* | ||
* ```yml | ||
* Danger: | ||
* title: Run Danger | ||
* image: node:latest | ||
* working_directory: ${{main_clone}} | ||
* entry_point: '/bin/bash' | ||
* cmd: | ||
* - '-ce' | ||
* - | | ||
* npm install -g yarn | ||
* yarn add danger --dev | ||
* yarn danger ci --failOnErrors | ||
* when: | ||
* steps: | ||
* - name: main_clone | ||
* on: | ||
* - success | ||
* ``` | ||
* | ||
* The `failOnErrors` option is required in order to ensure that the step fails properly when Danger fails. If you don't wnat this behavior, you can remove this option. | ||
* | ||
* Don't forget to add the `DANGER_GITHUB_API_TOKEN` variable to your pipeline settings so that Danger can properly post comments to your pull request. | ||
* | ||
*/ | ||
|
||
export class Codefresh implements CISource { | ||
constructor(private readonly env: Env) {} | ||
|
||
get name(): string { | ||
return "Codefresh" | ||
} | ||
|
||
get isCI(): boolean { | ||
return ensureEnvKeysExist(this.env, ["CF_BUILD_ID", "CF_BUILD_URL"]) | ||
} | ||
|
||
get isPR(): boolean { | ||
return ( | ||
ensureEnvKeysExist(this.env, ["CF_PULL_REQUEST_NUMBER"]) && | ||
ensureEnvKeysAreInt(this.env, ["CF_PULL_REQUEST_NUMBER"]) | ||
) | ||
} | ||
|
||
get pullRequestID(): string { | ||
if (this.env.CF_PULL_REQUEST_NUMBER) { | ||
return this.env.CF_PULL_REQUEST_NUMBER | ||
} else { | ||
return "" | ||
} | ||
} | ||
|
||
get repoSlug(): string { | ||
const owner = this.env.CF_REPO_OWNER | ||
const reponame = this.env.CF_REPO_NAME | ||
return owner && reponame ? `${owner}/${reponame}` : "" | ||
} | ||
|
||
get ciRunURL() { | ||
return this.env.CF_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,80 @@ | ||
import { Codefresh } from "../Codefresh" | ||
import { getCISourceForEnv } from "../../get_ci_source" | ||
|
||
const correctEnv = { | ||
CF_REPO_OWNER: "codefresh", | ||
CF_REPO_NAME: "someproject", | ||
CF_BUILD_ID: "1501", | ||
CF_PULL_REQUEST_NUMBER: "800", | ||
CF_BUILD_URL: "https://g.codefresh.io/build/1234", | ||
} | ||
|
||
describe("being found when looking for CI", () => { | ||
it("finds Codefresh with the right ENV", () => { | ||
const ci = getCISourceForEnv(correctEnv) | ||
expect(ci).toBeInstanceOf(Codefresh) | ||
}) | ||
}) | ||
|
||
describe(".isCI", () => { | ||
it("validates when all Codefresh environment vars are set", () => { | ||
const codefresh = new Codefresh(correctEnv) | ||
expect(codefresh.isCI).toBeTruthy() | ||
}) | ||
|
||
it("does not validate without env", () => { | ||
const codefresh = new Codefresh({}) | ||
expect(codefresh.isCI).toBeFalsy() | ||
}) | ||
}) | ||
|
||
describe(".isPR", () => { | ||
it("validates when all Codefresh environment vars are set", () => { | ||
const codefresh = new Codefresh(correctEnv) | ||
expect(codefresh.isPR).toBeTruthy() | ||
}) | ||
|
||
it("does not validate outside of Codefresh", () => { | ||
const codefresh = new Codefresh({}) | ||
expect(codefresh.isPR).toBeFalsy() | ||
}) | ||
|
||
const envs = ["CF_REPO_OWNER", "CF_REPO_NAME", "CF_BUILD_ID", "CF_PULL_REQUEST_NUMBER", "CF_BUILD_URL"] | ||
envs.forEach((key: string) => { | ||
let env = { | ||
CF_REPO_OWNER: "codefresh", | ||
CF_REPO_NAME: "someproject", | ||
CF_BUILD_ID: "1501", | ||
CF_PULL_REQUEST_NUMBER: "800", | ||
CF_BUILD_URL: "https://g.codefresh.io/build/1234", | ||
} | ||
env[key] = null | ||
|
||
it(`does not validate when ${key} is missing`, () => { | ||
const codefresh = new Codefresh({}) | ||
expect(codefresh.isPR).toBeFalsy() | ||
}) | ||
}) | ||
|
||
it("needs to have an integer PR number", () => { | ||
let env = { | ||
CF_PULL_REQUEST_NUMBER: "asdasd", | ||
} | ||
const codefresh = new Codefresh(env) | ||
expect(codefresh.isPR).toBeFalsy() | ||
}) | ||
}) | ||
|
||
describe(".pullRequestID", () => { | ||
it("pulls it out of the env", () => { | ||
const codefresh = new Codefresh({ CF_PULL_REQUEST_NUMBER: "800" }) | ||
expect(codefresh.pullRequestID).toEqual("800") | ||
}) | ||
}) | ||
|
||
describe(".repoSlug", () => { | ||
it("derives it from the PR Url", () => { | ||
const codefresh = new Codefresh(correctEnv) | ||
expect(codefresh.repoSlug).toEqual("codefresh/someproject") | ||
}) | ||
}) |
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