From 7648408fa75e16343e43fe9e298db56e8d7f422f Mon Sep 17 00:00:00 2001 From: Orta Therox Date: Wed, 25 Apr 2018 09:58:58 +0100 Subject: [PATCH 1/2] Allow returning a value and having Danger execute the code --- CHANGELOG.md | 1 + scripts/run-fixtures.js | 2 +- .../fixtures/__DangerfileDefaultExport.js | 1 + .../__DangerfileDefaultExportAsync.js | 7 ++++ .../results/__DangerfileDefaultExport.js.json | 11 +++++++ .../__DangerfileDefaultExportAsync.js.json | 11 +++++++ source/runner/runners/_tests/vm2.test.ts | 32 +++++++++++++++++++ source/runner/runners/inline.ts | 18 ++++++++--- source/runner/runners/runner.ts | 8 +++-- source/runner/runners/vm2.ts | 8 +++-- 10 files changed, 88 insertions(+), 11 deletions(-) create mode 100644 source/runner/_tests/fixtures/__DangerfileDefaultExport.js create mode 100644 source/runner/_tests/fixtures/__DangerfileDefaultExportAsync.js create mode 100644 source/runner/_tests/fixtures/results/__DangerfileDefaultExport.js.json create mode 100644 source/runner/_tests/fixtures/results/__DangerfileDefaultExportAsync.js.json diff --git a/CHANGELOG.md b/CHANGELOG.md index bfa590e67..4bcbf3581 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ ## Master * Prints a link to the comment in the build log [@orta][] +* A Dangerfile can return a default export, and then Danger will handle the execution of that code [@orta][] ## 3.5.0 - 3.5.1 diff --git a/scripts/run-fixtures.js b/scripts/run-fixtures.js index 9bb353f35..056c0782f 100644 --- a/scripts/run-fixtures.js +++ b/scripts/run-fixtures.js @@ -1,5 +1,5 @@ // Note: Keep this ES6 only, want to be able to run this directly via node -// to ensur that something like ts-node doesn't mess up paths etc +// to ensure that something like ts-node doesn't mess up paths etc // yarn build; cat source/_tests/fixtures/danger-js-pr-395.json | env DANGER_FAKE_CI="YEP" DANGER_TEST_REPO='danger/danger-js' DANGER_TEST_PR='395' node --inspect distribution/commands/danger-runner.js --text-only --dangerfile /Users/orta/dev/projects/danger/danger-js/source/runner/_tests/fixtures/__DangerfileAsync.js diff --git a/source/runner/_tests/fixtures/__DangerfileDefaultExport.js b/source/runner/_tests/fixtures/__DangerfileDefaultExport.js new file mode 100644 index 000000000..e3a68bfb3 --- /dev/null +++ b/source/runner/_tests/fixtures/__DangerfileDefaultExport.js @@ -0,0 +1 @@ +export default () => warn("Synchronous Warning") diff --git a/source/runner/_tests/fixtures/__DangerfileDefaultExportAsync.js b/source/runner/_tests/fixtures/__DangerfileDefaultExportAsync.js new file mode 100644 index 000000000..72738bbfa --- /dev/null +++ b/source/runner/_tests/fixtures/__DangerfileDefaultExportAsync.js @@ -0,0 +1,7 @@ +export default async () => + new Promise(res => { + setTimeout(() => { + warn("Asynchronous Warning") + res() + }, 10) + }) diff --git a/source/runner/_tests/fixtures/results/__DangerfileDefaultExport.js.json b/source/runner/_tests/fixtures/results/__DangerfileDefaultExport.js.json new file mode 100644 index 000000000..7ec77d7eb --- /dev/null +++ b/source/runner/_tests/fixtures/results/__DangerfileDefaultExport.js.json @@ -0,0 +1,11 @@ +{ + "fails": [], + "warnings": [ + { + "message": "Synchronous Warning" + } + ], + "messages": [], + "markdowns": [], + "scheduled": [] +} diff --git a/source/runner/_tests/fixtures/results/__DangerfileDefaultExportAsync.js.json b/source/runner/_tests/fixtures/results/__DangerfileDefaultExportAsync.js.json new file mode 100644 index 000000000..9cc86bab8 --- /dev/null +++ b/source/runner/_tests/fixtures/results/__DangerfileDefaultExportAsync.js.json @@ -0,0 +1,11 @@ +{ + "fails": [], + "warnings": [ + { + "message": "Asynchronous Warning" + } + ], + "messages": [], + "markdowns": [], + "scheduled": [] +} diff --git a/source/runner/runners/_tests/vm2.test.ts b/source/runner/runners/_tests/vm2.test.ts index ad3bbec48..dcad94abc 100644 --- a/source/runner/runners/_tests/vm2.test.ts +++ b/source/runner/runners/_tests/vm2.test.ts @@ -269,6 +269,38 @@ runners.forEach(run => { expect(results.fails[0].message).toContain("Danger failed to run") expect(results.markdowns[0].message).toContain("Error: failure") }) + + it("handles running default export code", async () => { + const context = await setupDangerfileContext() + const runtime = await exec.runner.createDangerfileRuntimeEnvironment(context) + const results = await exec.runner.runDangerfileEnvironment( + resolve(fixtures, "__DangerfileDefaultExport.js"), + undefined, + runtime + ) + expect(results).toEqual({ + fails: [], + messages: [], + markdowns: [], + warnings: [{ message: "Synchronous Warning" }], + }) + }) + + it("handles running default export async code", async () => { + const context = await setupDangerfileContext() + const runtime = await exec.runner.createDangerfileRuntimeEnvironment(context) + const results = await exec.runner.runDangerfileEnvironment( + resolve(fixtures, "__DangerfileDefaultExportAsync.js"), + undefined, + runtime + ) + expect(results).toEqual({ + fails: [], + messages: [], + markdowns: [], + warnings: [{ message: "Asynchronous Warning" }], + }) + }) }) }) }) diff --git a/source/runner/runners/inline.ts b/source/runner/runners/inline.ts index b402eb394..1e481c8d7 100644 --- a/source/runner/runners/inline.ts +++ b/source/runner/runners/inline.ts @@ -29,17 +29,20 @@ export async function createDangerfileRuntimeEnvironment(dangerfileContext: Dang * The values inside a Danger context are applied as globals to the Dangerfiles runtime. * * @param {string} filename the file path for the dangerfile - * @param {any} environment the results of createDangerfileRuntimeEnvironment + * @param {string} originalContents optional, the JS pre-compiled + * @param {DangerContext} environment the results of createDangerfileRuntimeEnvironment + * @param {any | undefined} injectedObjectToExport an optional object for passing into default exports * @returns {DangerResults} the results of the run */ export async function runDangerfileEnvironment( filename: string, originalContents: string | undefined, - environment: DangerContext + environment: DangerContext, + injectedObjectToExport: any | undefined = undefined ): Promise { // We need to change the local runtime to support running JavaScript - // and TypeScript through babel first. This is a simple implmentation - // and if we need more nuance, then we can look at other implementations + // and TypeScript through babel first. This is a simple implementation + // and if we need more nuance, then we can look at other options const customModuleHandler = (module: any, filename: string) => { if (!filename.includes("node_modules")) { d("Handling custom module: ", filename) @@ -70,7 +73,12 @@ export async function runDangerfileEnvironment( } d("Started parsing Dangerfile: ", filename) - _require(compiled, filename, {}) + const optionalExport = _require(compiled, filename, {}) + + if (typeof optionalExport.default === "function") { + d("Running default export from Dangerfile", filename) + await optionalExport.default(injectedObjectToExport) + } d("Finished running dangerfile: ", filename) // Don't stop all current async code from breaking, // however new code (without Peril support) can run diff --git a/source/runner/runners/runner.ts b/source/runner/runners/runner.ts index 64f60a1dd..3ce915979 100644 --- a/source/runner/runners/runner.ts +++ b/source/runner/runners/runner.ts @@ -7,14 +7,16 @@ export interface DangerRunner { * The values inside a Danger context are applied as globals to the Dangerfiles runtime. * * @param {string} filename the file path for the dangerfile - * @param {string | undefined} originalContents the contents of the Dangerfile, or undefined to use fs.readFileSync - * @param {any} environment the resuts of createDangerfileRuntimeEnvironment + * @param {string} originalContents optional, the JS pre-compiled + * @param {DangerContext} environment the results of createDangerfileRuntimeEnvironment + * @param {any | undefined} injectedObjectToExport an optional object for passing into default exports * @returns {DangerResults} the results of the run */ runDangerfileEnvironment: ( filename: string, originalContents: string | undefined, - environment: any + environment: any, + injectedObjectToExport?: any ) => Promise /** diff --git a/source/runner/runners/vm2.ts b/source/runner/runners/vm2.ts index 29208845e..64b34eb94 100644 --- a/source/runner/runners/vm2.ts +++ b/source/runner/runners/vm2.ts @@ -39,7 +39,8 @@ export async function createDangerfileRuntimeEnvironment(dangerfileContext: Dang export async function runDangerfileEnvironment( filename: string, originalContents: string | undefined, - environment: NodeVMOptions + environment: NodeVMOptions, + injectedObjectToExport: any | undefined = undefined ) { const vm = new NodeVM(environment) @@ -48,7 +49,10 @@ export async function runDangerfileEnvironment( let content = cleanDangerfile(originalContents) try { - vm.run(content, filename) + const optionalExport = vm.run(content, filename) + if (typeof optionalExport.default === "function") { + await optionalExport.default(injectedObjectToExport) + } const results = environment.sandbox!.results! await Promise.all( From 33f0871d854283cedf380d36d4e4b3e0da559750 Mon Sep 17 00:00:00 2001 From: Orta Therox Date: Wed, 25 Apr 2018 11:27:37 +0100 Subject: [PATCH 2/2] Punt on testing node 10 while it is 10.0.0 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 3711c197f..34f96858d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,7 +29,7 @@ matrix: - node scripts/run-fixtures.js # Does the real danger run - - node_js: node + - node_js: '9' script: - yarn jest --outputFile test-results.json --json --runInBand - yarn run link