-
-
Notifications
You must be signed in to change notification settings - Fork 365
/
runDangerSubprocess.ts
166 lines (143 loc) · 5.3 KB
/
runDangerSubprocess.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { debug } from "../../debug"
import { join, basename } from "path"
import { spawn } from "child_process"
import { DangerDSLJSONType, DangerJSON } from "../../dsl/DangerDSL"
import { Executor } from "../../runner/Executor"
import { jsonToDSL } from "../../runner/jsonToDSL"
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")
// If a sub-process passes this to stdout then danger-js will pass
// the DSL back to the process
const messageToSendDSL = "danger://send-dsl"
// Sanitizes the DSL so for sending via STDOUT
export const prepareDangerDSL = (dangerDSL: DangerDSLJSONType) => {
if (dangerDSL.github && dangerDSL.github.api) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
delete dangerDSL.github.api
}
const dangerJSONOutput: DangerJSON = { danger: dangerDSL }
return JSON.stringify(dangerJSONOutput, null, " ") + "\n"
}
export const addSubprocessCallArguments = (call: string[], args: string[] = process.argv): string[] => {
return call.concat(["runner"], args.slice(1, args.length))
}
// @deprecated
export const addSubprocessCallAguments = addSubprocessCallArguments
// Runs the Danger process
export const runDangerSubprocess = (
subprocessName: string[],
dslJSON: DangerDSLJSONType,
exec: Executor,
config: RunnerConfig
) => {
let processName = subprocessName[0]
let args = subprocessName
let results = null as any
args.shift() // mutate and remove the first element
const processDisplayName = basename(processName)
const dslJSONString = prepareDangerDSL(dslJSON)
d(`Running sub-process: ${processDisplayName} - ${args}`)
const child = spawn(processName, args, { env: { ...process.env, ...config.additionalEnvVars } })
const sendDSLToSubprocess = () => {
if (exec.options.passURLForDSL) {
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}`)
child.stdin.write(url)
child.stdin.end()
} else {
d(`Started passing in STDIN`)
child.stdin.write(dslJSONString)
child.stdin.end()
}
d(`Passed DSL in via STDIN`)
}
// Initial sending of the DSL
sendDSLToSubprocess()
let allLogs = ""
child.stdout.on("data", async (data) => {
const stdout: string = data.toString()
allLogs += stdout
// Provide a way for a process to request the DSL
// if they missed the first go.
if (stdout.includes(messageToSendDSL)) {
sendDSLToSubprocess()
}
// There are two checks for a response
const maybeJSON = getJSONFromSTDOUT(stdout)
const maybeJSONURL = getJSONURLFromSTDOUT(stdout)
// Remove message sent back to danger-js
const withoutURLs: string = data
.toString()
.replace(maybeJSON, "")
.replace(maybeJSONURL + "\n", "")
.replace(maybeJSONURL, "")
.replace(messageToSendDSL + "\n", "")
.replace(messageToSendDSL, "")
console.log(withoutURLs)
// Pass it back to the user
if (!results && maybeJSONURL) {
d("Got JSON URL from STDOUT, results are at: \n" + maybeJSONURL)
const url = maybeJSONURL.replace("danger-results:/", "")
if (!existsSync(url)) {
// prettier-ignore
throw new Error(`Process '${subprocessName.join(" ")}' reported that its JSON results could be found at ${url}, but the file was missing. The STDOUT was: ${stdout}`)
}
results = JSON.parse(readFileSync(url, "utf8"))
} else if (!results && maybeJSON) {
d("Got JSON results from STDOUT, results: \n" + maybeJSON)
results = JSON.parse(maybeJSON)
}
})
child.stderr.on("data", (data) => {
if (data.toString().trim().length !== 0) {
console.log(data.toString())
}
})
child.on("close", async (code) => {
d(`child process exited with code ${code}`)
// Submit an error back to the PR
if (code) {
d(`Handling fail from subprocess`)
process.exitCode = code
const failResults = resultsWithFailure(`\`${processDisplayName}\` failed.`, "### Log\n\n" + markdownCode(allLogs))
if (results) {
results = mergeResults(results, failResults)
} else {
results = failResults
}
}
const danger = await jsonToDSL(dslJSON, config.source)
await exec.handleResults(results, danger.git)
})
}
/** Pulls out a URL that's from the STDOUT */
const getJSONURLFromSTDOUT = (stdout: string): string | undefined => {
const match = stdout.match(/danger-results:\/\/*.+json/)
if (!match) {
return undefined
}
return match[0]
}
/** 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) => {
const trimmed = line.trim()
return (
trimmed.startsWith("{") &&
trimmed.endsWith("}") &&
trimmed.includes("markdowns") &&
trimmed.includes("fails") &&
trimmed.includes("warnings")
)
})
}
export default runDangerSubprocess