Skip to content

Commit

Permalink
Add random chars to files to support concurrency
Browse files Browse the repository at this point in the history
Fixes #1311
  • Loading branch information
squarefrog committed Sep 17, 2022
1 parent 17cba53 commit 116a3a4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -15,7 +15,7 @@
## Main

<!-- Your comment below this -->

- Append random string to danger-results.json and danger-dsl.json files to better support concurrent processes #1311

<!-- Your comment above this -->

Expand Down
8 changes: 4 additions & 4 deletions source/commands/danger-runner.ts
Expand Up @@ -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")

Expand Down Expand Up @@ -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)
Expand Down
12 changes: 7 additions & 5 deletions source/commands/utils/runDangerSubprocess.ts
Expand Up @@ -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")

Expand Down Expand Up @@ -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}`)
Expand All @@ -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

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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("{") &&
Expand Down

0 comments on commit 116a3a4

Please sign in to comment.