From 64899d961109e2ec9799e512014c3eb65d39ef0e Mon Sep 17 00:00:00 2001 From: Orta Therox Date: Sun, 15 Oct 2017 09:37:16 -0400 Subject: [PATCH] Initial work on adding a danger-process style runner for JS --- changelog.md | 2 + package.json | 4 +- source/commands/danger-run.ts | 2 +- source/commands/danger-runner.ts | 49 +++++++++++++++++++++++ source/runner/runners/utils/transpiler.ts | 2 +- yarn.lock | 10 +++++ 6 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 source/commands/danger-runner.ts diff --git a/changelog.md b/changelog.md index 92b21dc12..2b14f01e7 100644 --- a/changelog.md +++ b/changelog.md @@ -4,6 +4,8 @@ ### Master +### 2.0.0-alpha.20 + - Moves away from vm2 to a require-based Dangerfile runner. This removes the sandboxing aspect of the Dangerfile completely, but the sandboxing was mainly for Peril and I have a plan for that. diff --git a/package.json b/package.json index 20b458f71..ce92bab51 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "danger", - "version": "2.0.0-alpha.19", + "version": "2.0.0-alpha.20", "description": "Unit tests for Team Culture", "main": "distribution/danger.js", "typings": "distribution/danger.d.ts", @@ -61,6 +61,7 @@ "@types/chalk": "^0.4.31", "@types/commander": "^2.3.31", "@types/debug": "0.0.29", + "@types/get-stdin": "^5.0.1", "@types/jest": "^20.0.0", "@types/lodash.includes": "^4.3.3", "@types/node-fetch": "^1.6.6", @@ -95,6 +96,7 @@ "chalk": "^2.0.0", "commander": "^2.9.0", "debug": "^3.0.0", + "get-stdin": "^5.0.1", "github": "^11", "jsome": "^2.3.25", "jsonpointer": "^4.0.1", diff --git a/source/commands/danger-run.ts b/source/commands/danger-run.ts index 7b5be3737..1f13906aa 100755 --- a/source/commands/danger-run.ts +++ b/source/commands/danger-run.ts @@ -42,7 +42,7 @@ async function run() { } if (platform) { - console.log(`${chalk.bold("OK")}, everything looks good: ${source.name} on ${platform.name}`) + d.log(`${chalk.bold("OK")}, everything looks good: ${source.name} on ${platform.name}`) const dangerFile = dangerfilePath(program) const exists = validateDangerfileExists(dangerFile) diff --git a/source/commands/danger-runner.ts b/source/commands/danger-runner.ts new file mode 100644 index 000000000..5e0831a9f --- /dev/null +++ b/source/commands/danger-runner.ts @@ -0,0 +1,49 @@ +import setSharedArgs, { SharedCLI } from "./utils/sharedDangerfileArgs" + +import * as chalk from "chalk" +import * as program from "commander" +import * as fs from "fs" +import * as getSTDIN from "get-stdin" + +import { contextForDanger } from "../runner/Dangerfile" +import inline from "../runner/runners/inline" +import { Executor } from "../runner/Executor" +import { getPlatformForEnv } from "../platforms/platform" +import getRuntimeCISource from "./utils/getRuntimeCISource" + +// Given the nature of this command, it can be tricky to test, so I use a command like this: +// +// env DANGER_GITHUB_API_TOKEN='xxx' DANGER_FAKE_CI="YEP" DANGER_TEST_REPO='artsy/eigen' DANGER_TEST_PR='2408' +// yarn ts-node -s -- source/commands/danger-process.ts ./scripts/danger_runner.rb +// + +program + .usage("[options] dangerfile") + .description("Handles running the Dangerfile, expects a DSL from STDIN, which should be passed from `danger run`.") + +setSharedArgs(program).parse(process.argv) + +const app = (program as any) as SharedCLI + +const run = async (jsonString: string) => { + const dsl = JSON.parse(jsonString) + console.log(dsl) + + // Set up the runtime env + const context = contextForDanger(dsl) + const runtimeEnv = await inline.createDangerfileRuntimeEnvironment(context) + const results = await inline.runDangerfileEnvironment(app.dangerfile, undefined, runtimeEnv) + + const config = { + stdoutOnly: app.textOnly, + verbose: app.verbose, + } + + const source = await getRuntimeCISource(app) + const platform = getPlatformForEnv(process.env, source!) + + const exec = new Executor(source!, platform, inline, config) + await exec.handleResults(results) +} + +getSTDIN().then(run) diff --git a/source/runner/runners/utils/transpiler.ts b/source/runner/runners/utils/transpiler.ts index 4779831bf..a14741115 100644 --- a/source/runner/runners/utils/transpiler.ts +++ b/source/runner/runners/utils/transpiler.ts @@ -6,7 +6,7 @@ let hasBabel = false let hasBabelTypeScript = false let hasFlow = false -// Yes, lots of linter disables, but I want to support TS/Babel/Neither correclty +// Yes, lots of linter disables, but I want to support TS/Babel/Neither correctly try { require.resolve("typescript") // tslint:disable-line diff --git a/yarn.lock b/yarn.lock index 5d42094bc..5adddd7e6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -16,6 +16,12 @@ version "0.0.29" resolved "https://registry.yarnpkg.com/@types/debug/-/debug-0.0.29.tgz#a1e514adfbd92f03a224ba54d693111dbf1f3754" +"@types/get-stdin@^5.0.1": + version "5.0.1" + resolved "https://registry.yarnpkg.com/@types/get-stdin/-/get-stdin-5.0.1.tgz#46afbcaf09e94fe025afa07ae994ac3168adbdf3" + dependencies: + "@types/node" "*" + "@types/jest@^20.0.0": version "20.0.4" resolved "https://registry.yarnpkg.com/@types/jest/-/jest-20.0.4.tgz#91e2c76fbe90b3a719b5d689958f0495f005beb9" @@ -2099,6 +2105,10 @@ get-stdin@^4.0.1: version "4.0.1" resolved "https://registry.yarnpkg.com/get-stdin/-/get-stdin-4.0.1.tgz#b968c6b0a04384324902e8bf1a5df32579a450fe" +get-stdin@^5.0.1: + version "5.0.1" + resolved "https://registry.yarnpkg.com/get-stdin/-/get-stdin-5.0.1.tgz#122e161591e21ff4c52530305693f20e6393a398" + get-stream@^2.2.0: version "2.3.1" resolved "https://registry.yarnpkg.com/get-stream/-/get-stream-2.3.1.tgz#5f38f93f346009666ee0150a054167f91bdd95de"