Skip to content

Commit

Permalink
Convert danger run to use danger process under the hood
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Oct 22, 2017
1 parent e735469 commit 2b6c585
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 53 deletions.
2 changes: 2 additions & 0 deletions dangerfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ declare function schedule(promise: Promise<any | void>): void
declare function schedule(promise: () => Promise<any | void>): void
declare function schedule(callback: (resolve: any) => void): void

warn("this worked")

import * as fs from "fs"

schedule(async () => {
Expand Down
2 changes: 1 addition & 1 deletion docs/usage/danger-process.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ danger pr https://github.com/danger/danger-js/pull/395 --json > danger-js-395.ds

This will work for any open repo, and if you've set up your local shell to include `DANGER_GITHUB_API_TOKEN` then you can
use this with any private repository too. The JSON schema is documented in Danger JS's types as [DangerJSONDSLType][]. I
plan to add a full reference for this, similar to the reference for the user's DSL in the future in these docs.
plan to add a full reference for this, similar to the reference for the user's DSL in the future in these docs. _Note"_ The JSON will include your access token, so you probably want to sanitze that before commiting it to the repo.

A runner can output anything during the process to STDOUT, and it will be logged to the user. However, Danger JS is
listening for a JSON response in this format:
Expand Down
4 changes: 2 additions & 2 deletions scripts/danger_runner.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env ruby

str = STDIN.tty? ? 'Cannot read from STDIN' : $stdin.read
str = STDIN.tty? ? "Cannot read from STDIN" : $stdin.read
exit(1) unless str

# Have a dumb fake response
require 'json'
require "json"
results = { fails: [], warnings: [], messages: [], markdowns: [] }.to_json

STDOUT.write(results)
30 changes: 13 additions & 17 deletions source/commands/danger-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ if (process.env["DANGER_VERBOSE"] || app.verbose) {
global.verbose = true
}

// a dirty wrapper to allow async functionality in the setup
async function run() {
const source = await getRuntimeCISource(app)

getRuntimeCISource(app).then(source => {
// This does not set a failing exit code
if (source && !source.isPR) {
console.log("Skipping Danger due to not this run not executing on a PR.")
Expand All @@ -66,18 +63,17 @@ async function run() {
jsonOnly: false,
}

const dangerDSL = await jsonDSLGenerator(platform)
const processInput = prepareDangerDSL(dangerDSL)

if (!subprocessName) {
// Just pipe it out to the CLI
process.stdout.write(processInput)
} else {
const exec = new Executor(source, platform, inlineRunner, config)
runDangerSubprocess(subprocessName, processInput, exec)
}
jsonDSLGenerator(platform).then(dangerJSONDSL => {
const processInput = prepareDangerDSL(dangerJSONDSL)

if (!subprocessName) {
// Just pipe it out to the CLI
process.stdout.write(processInput)
} else {
const exec = new Executor(source, platform, inlineRunner, config)
runDangerSubprocess([subprocessName], processInput, exec)
}
})
}
}
}

run()
})
44 changes: 14 additions & 30 deletions source/commands/danger-run.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,28 @@
import * as chalk from "chalk"
import * as program from "commander"
import * as debug from "debug"

import { getPlatformForEnv } from "../platforms/platform"
import { Executor } from "../runner/Executor"
import { dangerfilePath } from "./utils/file-utils"
import runDangerSubprocess, { prepareDangerDSL } from "./utils/runDangerSubprocess"
import setSharedArgs, { SharedCLI } from "./utils/sharedDangerfileArgs"
import validateDangerfileExists from "./utils/validateDangerfileExists"
import getRuntimeCISource from "./utils/getRuntimeCISource"
import inlineRunner from "../runner/runners/inline"

const d = debug("danger:run")
declare const global: any
import inlineRunner from "../runner/runners/inline"
import { jsonDSLGenerator } from "../runner/dslGenerator"
import dangerRunToRunnerCLI from "./utils/dangerRunToRunnerCLI"

program.usage("[options]").description("Runs a Dangerfile in JavaScript or TypeScript.")
setSharedArgs(program).parse(process.argv)

const app = (program as any) as SharedCLI

if (process.env["DANGER_VERBOSE"] || app.verbose) {
global.verbose = true
}

// a dirty wrapper to allow async functionality in the setup
async function run() {
const source = await getRuntimeCISource(app)

getRuntimeCISource(app).then(source => {
// This does not set a failing exit code
if (source && !source.isPR) {
// This does not set a failing exit code
console.log("Skipping Danger due to not this run not executing on a PR.")
}

// The optimal path
if (source && source.isPR) {
const platform = getPlatformForEnv(process.env, source)
if (!platform) {
Expand All @@ -42,27 +34,19 @@ async function run() {
}

if (platform) {
d(`${chalk.bold("OK")}, everything looks good: ${source.name} on ${platform.name}`)
const dangerFile = dangerfilePath(program)

const exists = validateDangerfileExists(dangerFile)
if (!exists) {
console.error(chalk.red(`Looks like your path '${dangerFile}' is not a valid path for a Dangerfile.`))
process.exitCode = 1
} else {
d(`executing dangerfile at ${dangerFile}`)

jsonDSLGenerator(platform).then(dangerJSONDSL => {
const config = {
stdoutOnly: app.textOnly,
verbose: app.verbose,
jsonOnly: false,
}

const processInput = prepareDangerDSL(dangerJSONDSL)

const runnerCommand = dangerRunToRunnerCLI(process.argv)
const exec = new Executor(source, platform, inlineRunner, config)
exec.setupAndRunDanger(dangerFile)
}
runDangerSubprocess(runnerCommand, processInput, exec)
})
}
}
}

run()
})
41 changes: 41 additions & 0 deletions source/commands/utils/_tests/dangerRunToRunnerCLI.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import dangerRunToRunnerCLI from "../dangerRunToRunnerCLI"

describe("it can handle the command", () => {
it("`danger`", () => {
expect(dangerRunToRunnerCLI(["danger"])).toEqual("danger runner")
})

it("`danger --dangerfile myDangerfile.ts`", () => {
expect(dangerRunToRunnerCLI(["danger", "--dangerfile", "myDangerfile.ts"])).toEqual(
"danger runner --dangerfile myDangerfile.ts".split(" ")
)
})

it("`danger run`", () => {
expect(dangerRunToRunnerCLI(["danger", "run"])).toEqual("danger runner".split(" "))
})

it("`danger run --dangerfile myDangerfile.ts`", () => {
expect(dangerRunToRunnerCLI(["danger", "run", "--dangerfile", "myDangerfile.ts"])).toEqual(
"danger runner --dangerfile myDangerfile.ts".split(" ")
)
})

it("`node distribution/commands/danger-run.js`", () => {
expect(dangerRunToRunnerCLI(["node", "distribution/commands/danger-run.js"])).toEqual(
"node distribution/commands/danger-runner.js".split(" ")
)
})

it("`node distribution/commands/danger-run.js --dangerfile myDangerfile.ts`", () => {
expect(
dangerRunToRunnerCLI(["node", "distribution/commands/danger-run.js", "--dangerfile", "myDangerfile.ts"])
).toEqual("node distribution/commands/danger-runner.js --dangerfile myDangerfile.ts".split(" "))
})
})

it("`node distribution/commands/danger-run.js --dangerfile 'myDanger file.ts'`", () => {
expect(
dangerRunToRunnerCLI(["node", "distribution/commands/danger-run.js", "--dangerfile", "myDanger file.ts"])
).toEqual(["node", "distribution/commands/danger-runner.js", "--dangerfile", "myDanger file.ts"])
})
26 changes: 26 additions & 0 deletions source/commands/utils/dangerRunToRunnerCLI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const dangerRunToRunnerCLI = (argv: string[]) => {
let newCommand = []
newCommand.push(argv[0])

// e.g. node --inspect distribution/commands/danger-run.js --dangerfile myDangerfile.ts
if (argv.length === 1) {
return ["danger", "runner"]
} else if (argv[0].includes("node")) {
newCommand.push(argv[1].replace("-run", "-runner"))
for (let index = 2; index < argv.length; index++) {
newCommand.push(argv[index])
}
} else {
// e.g. danger --dangerfile
// if you do `danger run` start looking at args later
newCommand.push("runner")
let index = argv[1] === "run" ? 2 : 1
for (; index < argv.length; index++) {
newCommand.push(argv[index])
}
}

return newCommand
}

export default dangerRunToRunnerCLI
10 changes: 7 additions & 3 deletions source/commands/utils/runDangerSubprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ export const prepareDangerDSL = (dangerDSL: DangerDSLJSONType) => {
return JSON.stringify(dangerDSL, null, " ") + "\n"
}

// Runs the Danger process
const runDangerSubprocess = (subprocessName: string, dslJSONString: string, exec: Executor) => {
const child = spawn(subprocessName)
// Runs the Danger process, can either take a simpl
const runDangerSubprocess = (subprocessName: string[], dslJSONString: string, exec: Executor) => {
let processName = subprocessName[0]
let args = subprocessName
args.shift() // mutate and remove the first element

const child = spawn(processName, args)
let allLogs = ""

child.stdin.write(dslJSONString)
Expand Down
1 change: 1 addition & 0 deletions source/runner/_tests/_executor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import inlineRunner from "../runners/inline"
const defaultConfig = {
stdoutOnly: false,
verbose: false,
jsonOnly: false,
}

describe("setup", () => {
Expand Down

0 comments on commit 2b6c585

Please sign in to comment.