Skip to content

Commit 51a7b2d

Browse files
committed
Allow sub-processes to request the DSL from danger
1 parent 774b400 commit 51a7b2d

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
## Master
1515

1616
- Adds `html_url` to the PR JSON declaration - [@orta][]
17+
- Adds a way for a sub-process to tell danger-js that it wants a copy of the DSL. This is a potential fix for when you
18+
have a process that might not be ready to grab the DSL instantly from danger-js. The subprocess can print the message
19+
`danger://send-dsl` to stdout and danger-js will re-send the DSL via STDIN. - [@orta][]
20+
21+
These two are to try and figure out [danger/swift#108](https://github.com/danger/swift/issues/108).
1722

1823
# 6.1.4
1924

docs/usage/danger-process.html.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Danger in my Language
3-
subtitle: Using Danger Process
3+
subtitle: Using --process to build danger runners
44
layout: guide_js
55
order: 3
66
blurb: How to use `danger process` to create a Danger runner for any language.
@@ -86,7 +86,7 @@ STDOUT.write(results.to_json)
8686

8787
As Ruby is duck-typed, it doesn't need any infrastructure. You can parse the incoming JSON into an object, then work
8888
with the standard library to provide a Dangerfile environment. If you saved this file as `dangerfile.rb`, and
89-
`chmod +x dangerfile.rb` then you can run `danger process 'dangerfile.rb`.
89+
`chmod +x dangerfile.rb` then you can run `danger pr --process 'dangerfile.rb`.
9090

9191
### Tiny, and not too simple
9292

@@ -192,6 +192,12 @@ It's pretty likely that your CI already has node, so it can just be `npm install
192192

193193
Finally, let me ([@orta][]) know! I want to keep track of them all on the Danger Systems site :+1:.
194194

195+
### Troubleshooting
196+
197+
- If you're having timeout issues on waiting for STDIN from Danger JS, then it's possible that Danger JS is sending the
198+
STDIN too soon for your process to catch it. To work around this, have your process print `danger://send-dsl` and
199+
Danger JS will re-send the JSON to your process.
200+
195201
[danger-swift]: https://github.com/danger/danger-swift
196202
[swift-json]: https://github.com/danger/danger-swift/blob/master/fixtures/eidolon_609.json
197203
[swift-stdin]:

source/commands/utils/runDangerSubprocess.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import { RunnerConfig } from "../ci/runner"
1111

1212
const d = debug("runDangerSubprocess")
1313

14+
// If a sub-process passes this to stdout then danger-js will pass
15+
// the DSL back to the process
16+
const messageToSendDSL = "danger://send-dsl"
17+
1418
// Sanitizes the DSL so for sending via STDOUT
1519
export const prepareDangerDSL = (dangerDSL: DangerDSLJSONType) => {
1620
if (dangerDSL.github && dangerDSL.github.api) {
@@ -38,25 +42,39 @@ export const runDangerSubprocess = (
3842
d(`Running subprocess: ${processDisplayName} - ${args}`)
3943
const child = spawn(processName, args, { env: { ...process.env, ...config.additionalEnvVars } })
4044

41-
d(`Started passing in STDIN`)
42-
child.stdin.write(dslJSONString)
43-
child.stdin.end()
44-
d(`Passed in STDIN`)
45+
const sendDSLToSubprocess = () => {
46+
d(`Started passing in STDIN`)
47+
child.stdin.write(dslJSONString)
48+
child.stdin.end()
49+
d(`Passed in STDIN`)
50+
}
51+
52+
// Initial sending of the DSL
53+
sendDSLToSubprocess()
4554

4655
let allLogs = ""
4756
child.stdout.on("data", async data => {
48-
const stdout = data.toString()
57+
const stdout: string = data.toString()
4958
allLogs += stdout
5059

51-
// There are two checks
60+
// Provide a way for a process to request the DSL
61+
// if they missed the first go.
62+
if (stdout.includes(messageToSendDSL)) {
63+
sendDSLToSubprocess()
64+
}
65+
66+
// There are two checks for a response
5267
const maybeJSON = getJSONFromSTDOUT(stdout)
5368
const maybeJSONURL = getJSONURLFromSTDOUT(stdout)
5469

5570
// Remove message sent back to danger-js
5671
const withoutURLs: string = data
5772
.toString()
5873
.replace(maybeJSON, "")
74+
.replace(maybeJSONURL + "\n", "")
5975
.replace(maybeJSONURL, "")
76+
.replace(messageToSendDSL + "\n", "")
77+
.replace(messageToSendDSL, "")
6078

6179
console.log(withoutURLs)
6280

0 commit comments

Comments
 (0)