-
-
Notifications
You must be signed in to change notification settings - Fork 368
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
13 changed files
with
226 additions
and
15 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,59 @@ | ||
import { Env, CISource } from "../ci_source" | ||
import { ensureEnvKeysExist } from "../ci_source_helpers" | ||
|
||
/** | ||
* | ||
* ### CI Setup | ||
* | ||
* You need to add `DANGER_GITHUB_API_TOKEN` to the ENV for the build or machine manually. | ||
* Then you also need to figure out how to provide the URL for the pull request in `PULL_REQUEST_URL` ENV. | ||
* | ||
* TeamCity provides the `%teamcity.build.branch%` variable that contains something like `pull/123` that you can use: | ||
* ```sh | ||
* PULL_REQUEST_URL='https://github.com/dager/danger-js/%teamcity.build.branch%' | ||
* ``` | ||
* | ||
*/ | ||
|
||
export class TeamCity implements CISource { | ||
constructor(private readonly env: Env) {} | ||
|
||
get name(): string { | ||
return "TeamCity" | ||
} | ||
|
||
get isCI(): boolean { | ||
return ensureEnvKeysExist(this.env, ["TEAMCITY_VERSION"]) | ||
} | ||
|
||
get isPR(): boolean { | ||
if (ensureEnvKeysExist(this.env, ["PULL_REQUEST_URL"])) { | ||
return true | ||
} | ||
|
||
const mustHave = ["PULL_REQUEST_URL"] | ||
return ensureEnvKeysExist(this.env, mustHave) | ||
} | ||
|
||
private _prParseURL(): { owner?: string; reponame?: string; id?: string } { | ||
const prUrl = this.env.PULL_REQUEST_URL || "" | ||
const splitSlug = prUrl.split("/") | ||
if (splitSlug.length === 7) { | ||
const owner = splitSlug[3] | ||
const reponame = splitSlug[4] | ||
const id = splitSlug[6] | ||
return { owner, reponame, id } | ||
} | ||
return {} | ||
} | ||
|
||
get pullRequestID(): string { | ||
const { id } = this._prParseURL() | ||
return id || "" | ||
} | ||
|
||
get repoSlug(): string { | ||
const { owner, reponame } = this._prParseURL() | ||
return owner && reponame ? `${owner}/${reponame}` : "" | ||
} | ||
} |
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,68 @@ | ||
import { TeamCity } from "../TeamCity" | ||
import { getCISourceForEnv } from "../../get_ci_source" | ||
|
||
const correctEnv = { | ||
TEAMCITY_VERSION: "1.2.3", | ||
PULL_REQUEST_URL: "https://github.com/danger/danger-js/pull/541", | ||
} | ||
|
||
describe("being found when looking for CI", () => { | ||
it("finds TeamCity with the right ENV", () => { | ||
const ci = getCISourceForEnv(correctEnv) | ||
expect(ci).toBeInstanceOf(TeamCity) | ||
}) | ||
}) | ||
|
||
describe(".isCI", () => { | ||
it("validates when all TeamCity environment vars are set", () => { | ||
const teamcity = new TeamCity(correctEnv) | ||
expect(teamcity.isCI).toBeTruthy() | ||
}) | ||
|
||
it("does not validate without env", () => { | ||
const teamcity = new TeamCity({}) | ||
expect(teamcity.isCI).toBeFalsy() | ||
}) | ||
}) | ||
|
||
describe(".isPR", () => { | ||
it("validates when all TeamCity environment vars are set", () => { | ||
const teamcity = new TeamCity(correctEnv) | ||
expect(teamcity.isPR).toBeTruthy() | ||
}) | ||
|
||
it("does not validate outside of TeamCity", () => { | ||
const teamcity = new TeamCity({}) | ||
expect(teamcity.isPR).toBeFalsy() | ||
}) | ||
|
||
const envs = ["TEAMCITY_VERSION", "PULL_REQUEST_URL"] | ||
envs.forEach((key: string) => { | ||
let env = { | ||
TEAMCITY_VERSION: "1.2.3", | ||
PULL_REQUEST_URL: "https://github.com/danger/danger-js/pull/541", | ||
} | ||
env[key] = null | ||
|
||
it(`does not validate when ${key} is missing`, () => { | ||
const teamcity = new TeamCity({}) | ||
expect(teamcity.isPR).toBeFalsy() | ||
}) | ||
}) | ||
}) | ||
|
||
describe(".pullRequestID", () => { | ||
it("pulls it out of the env", () => { | ||
const teamcity = new TeamCity({ | ||
PULL_REQUEST_URL: "https://github.com/danger/danger-js/pull/541", | ||
}) | ||
expect(teamcity.pullRequestID).toEqual("541") | ||
}) | ||
}) | ||
|
||
describe(".repoSlug", () => { | ||
it("derives it from the PR Url", () => { | ||
const teamcity = new TeamCity(correctEnv) | ||
expect(teamcity.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
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,36 @@ | ||
import chalk from "chalk" | ||
import * as debug from "debug" | ||
|
||
import { getPlatformForEnv } from "../../platforms/platform" | ||
import { SharedCLI } from "../utils/sharedDangerfileArgs" | ||
import getRuntimeCISource from "../utils/getRuntimeCISource" | ||
|
||
import { RunnerConfig } from "./runner" | ||
|
||
const d = debug("danger:reset-status") | ||
|
||
export const runRunner = async (app: SharedCLI, config?: RunnerConfig) => { | ||
d(`Starting sub-process run with ${app.args}`) | ||
const source = (config && config.source) || (await getRuntimeCISource(app)) | ||
|
||
// This does not set a failing exit code | ||
if (source && !source.isPR) { | ||
console.log("Skipping Danger due to this run not executing on a PR.") | ||
} | ||
|
||
// The optimal path | ||
if (source && source.isPR) { | ||
const platform = (config && config.platform) || getPlatformForEnv(process.env, source) | ||
if (!platform) { | ||
console.log(chalk.red(`Could not find a source code hosting platform for ${source.name}.`)) | ||
console.log( | ||
`Currently Danger JS only supports GitHub, if you want other platforms, consider the Ruby version or help out.` | ||
) | ||
process.exitCode = 1 | ||
} | ||
|
||
if (platform) { | ||
await platform.updateStatus("pending", "Danger is waiting for your CI run to complete...") | ||
} | ||
} | ||
} |
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,12 @@ | ||
#! /usr/bin/env node | ||
|
||
import * as program from "commander" | ||
|
||
import setSharedArgs, { SharedCLI } from "./utils/sharedDangerfileArgs" | ||
import { runRunner } from "./ci/runner" | ||
|
||
program.usage("[options]").description("Reset the status of a GitHub PR to pending.") | ||
setSharedArgs(program).parse(process.argv) | ||
|
||
const app = (program as any) as SharedCLI | ||
runRunner(app) |
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
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