diff --git a/CHANGELOG.md b/CHANGELOG.md index 74be6614f..34cf3f247 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,7 @@ ## Main - +- Append random string to danger-results.json and danger-dsl.json files to better support concurrent processes #1311 diff --git a/source/commands/danger-runner.ts b/source/commands/danger-runner.ts index 8efebb810..81366260d 100644 --- a/source/commands/danger-runner.ts +++ b/source/commands/danger-runner.ts @@ -18,6 +18,7 @@ import { getPlatformForEnv } from "../platforms/platform" import { tmpdir } from "os" import { writeFileSync } from "fs" import { join } from "path" +import { randomBytes } from "crypto" const d = debug("runner") @@ -72,12 +73,11 @@ nodeCleanup((exitCode: number, signal: string) => { const results: DangerResults = runtimeEnv.results d(`Process has finished with ${exitCode}, sending the results back to the host process ${signal || ""}`) d( - `Got md ${results.markdowns.length} w ${results.warnings.length} f ${results.fails.length} m ${ - results.messages.length - }` + `Got md ${results.markdowns.length} w ${results.warnings.length} f ${results.fails.length} m ${results.messages.length}` ) if (foundDSL) { - const resultsPath = join(tmpdir(), "danger-results.json") + const filename = `danger-results-${randomBytes(4).toString("hex")}.json` + const resultsPath = join(tmpdir(), filename) d(`Writing results into ${resultsPath}`) writeFileSync(resultsPath, JSON.stringify(results, null, 2), "utf8") process.stdout.write("danger-results:/" + resultsPath) diff --git a/source/commands/utils/runDangerSubprocess.ts b/source/commands/utils/runDangerSubprocess.ts index 7793cdd6f..65bb49b81 100644 --- a/source/commands/utils/runDangerSubprocess.ts +++ b/source/commands/utils/runDangerSubprocess.ts @@ -9,6 +9,7 @@ import { markdownCode, resultsWithFailure, mergeResults } from "./reporting" import { readFileSync, existsSync, writeFileSync } from "fs" import { RunnerConfig } from "../ci/runner" import { tmpdir } from "os" +import { randomBytes } from "crypto" const d = debug("runDangerSubprocess") @@ -54,7 +55,8 @@ export const runDangerSubprocess = ( const sendDSLToSubprocess = () => { if (exec.options.passURLForDSL) { - const resultsPath = join(tmpdir(), "danger-dsl.json") + const filename = `danger-dsl-${randomBytes(4).toString("hex")}.json` + const resultsPath = join(tmpdir(), filename) writeFileSync(resultsPath, dslJSONString, "utf8") const url = `danger://dsl/${resultsPath}` d(`Started passing in STDIN via the URL: ${url}`) @@ -72,7 +74,7 @@ export const runDangerSubprocess = ( sendDSLToSubprocess() let allLogs = "" - child.stdout.on("data", async data => { + child.stdout.on("data", async (data) => { const stdout: string = data.toString() allLogs += stdout @@ -112,13 +114,13 @@ export const runDangerSubprocess = ( } }) - child.stderr.on("data", data => { + child.stderr.on("data", (data) => { if (data.toString().trim().length !== 0) { console.log(data.toString()) } }) - child.on("close", async code => { + child.on("close", async (code) => { d(`child process exited with code ${code}`) // Submit an error back to the PR if (code) { @@ -149,7 +151,7 @@ const getJSONURLFromSTDOUT = (stdout: string): string | undefined => { /** Pulls the JSON directly out, this has proven to be less reliable */ const getJSONFromSTDOUT = (stdout: string): string | undefined => { const lines = stdout.split("\n") - return lines.find(line => { + return lines.find((line) => { const trimmed = line.trim() return ( trimmed.startsWith("{") &&