Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add random chars to files to support concurrency #1312

Merged
merged 1 commit into from Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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