Skip to content

Fixes for danger pr vs danger WRT async code #452

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

Merged
merged 6 commits into from
Dec 24, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ matrix:
script:
- yarn lint
- yarn add jest
- yarn jest --runInBand
- yarn jest --runInBand

notifications:
slack: dangergem:9FOZou9EGBV9xO1Ol4bxRPz9
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ developers, so please limit technical // terminology to in here.

// ### Master

* Updates documentation dependencies - [@orta][]
* Fixes to running `danger` with params - [@orta][]
* Fixes for `danger pr` not acting like `danger` WRT async code - [@orta][]

### 2.1.6

* Updates dependencies - [@orta][]
Expand Down
39 changes: 8 additions & 31 deletions source/commands/danger-pr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@ import * as jsome from "jsome"
import { FakeCI } from "../ci_source/providers/Fake"
import { GitHub } from "../platforms/GitHub"
import { GitHubAPI } from "../platforms/github/GitHubAPI"
import { Executor, ExecutorOptions } from "../runner/Executor"
import { pullRequestParser } from "../platforms/github/pullRequestParser"
import { runDangerfileEnvironment } from "../runner/runners/inline"
import { dangerfilePath } from "./utils/file-utils"
import validateDangerfileExists from "./utils/validateDangerfileExists"
import openRepl from "./utils/repl"
import setSharedArgs, { SharedCLI } from "./utils/sharedDangerfileArgs"

import inlineRunner from "../runner/runners/inline"
import { jsonDSLGenerator } from "../runner/dslGenerator"
import { prepareDangerDSL } from "./utils/runDangerSubprocess"
import { runRunner } from "./run/runner"

// yarn build; cat source/_tests/fixtures/danger-js-pr-384.json | node --inspect --inspect-brk distribution/commands/danger-runner.js --text-only

Expand All @@ -32,8 +28,8 @@ interface App extends SharedCLI {
program
.usage("[options] <pr_url>")
.description("Emulate running Danger against an existing GitHub Pull Request.")
.option("-J, --json", "Output the JSON that would be passed into `danger process` for this PR.")
.option("-j, --js", "Strips the readbility changes to the DSL JSON.")
.option("-J, --json", "Output the raw JSON that would be passed into `danger process` for this PR.")
.option("-j, --js", "A more human-readable version of the JSON.")

setSharedArgs(program).parse(process.argv)

Expand All @@ -60,43 +56,24 @@ if (program.args.length === 0) {
const api = new GitHubAPI(source, process.env["DANGER_GITHUB_API_TOKEN"])
const platform = new GitHub(api)
if (app.json || app.js) {
runProcessJSON(platform)
runHalfProcessJSON(platform)
} else {
runDanger(source, platform, dangerFile)
runRunner(app, { source, platform })
}
}
}
}

// Run Danger traditionally
async function runDanger(source: FakeCI, platform: GitHub, file: string) {
const config: ExecutorOptions = {
stdoutOnly: app.textOnly,
verbose: app.verbose,
jsonOnly: false,
dangerID: "default",
}
const exec = new Executor(source, platform, inlineRunner, config)

const runtimeEnv = await exec.setupDanger()
const results = await runDangerfileEnvironment(file, undefined, runtimeEnv)
if (program.repl) {
openRepl(runtimeEnv)
} else {
jsome(results)
}
}

// Run Danger Process and output the JSON to CLI
async function runProcessJSON(platform: GitHub) {
// Run the first part of a Danger Process and output the JSON to CLI
async function runHalfProcessJSON(platform: GitHub) {
const dangerDSL = await jsonDSLGenerator(platform)
const processInput = prepareDangerDSL(dangerDSL)
const output = JSON.parse(processInput)
const dsl = { danger: output }
// See https://github.com/Javascipt/Jsome/issues/12
if (app.json) {
process.stdout.write(JSON.stringify(dsl, null, 2))
} else {
} else if (app.js) {
jsome(dsl)
}
}
3 changes: 1 addition & 2 deletions source/commands/danger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ program
.command("runner", "Runs a dangerfile against a DSL passed in via STDIN")
.command("run", "Runs danger on your local system")

setSharedArgs(program)
program.parse(process.argv)
setSharedArgs(program).parse(process.argv)

const app = (program as any) as SharedCLI
runRunner(app)
42 changes: 24 additions & 18 deletions source/commands/run/runner.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chalk from "chalk"

import { getPlatformForEnv } from "../../platforms/platform"
import { getPlatformForEnv, Platform } from "../../platforms/platform"
import { Executor, ExecutorOptions } from "../../runner/Executor"
import runDangerSubprocess, { prepareDangerDSL } from "../utils/runDangerSubprocess"
import { SharedCLI } from "../utils/sharedDangerfileArgs"
Expand All @@ -9,9 +9,15 @@ import getRuntimeCISource from "../utils/getRuntimeCISource"
import inlineRunner from "../../runner/runners/inline"
import { jsonDSLGenerator } from "../../runner/dslGenerator"
import dangerRunToRunnerCLI from "../utils/dangerRunToRunnerCLI"
import { CISource } from "../../ci_source/ci_source"

export const runRunner = async (app: SharedCLI) => {
const source = await getRuntimeCISource(app)
export interface RunnerConfig {
source: CISource
platform: Platform
}

export const runRunner = async (app: SharedCLI, config?: RunnerConfig) => {
const source = (config && config.source) || (await getRuntimeCISource(app))

// This does not set a failing exit code
if (source && !source.isPR) {
Expand All @@ -20,7 +26,7 @@ export const runRunner = async (app: SharedCLI) => {

// The optimal path
if (source && source.isPR) {
const platform = getPlatformForEnv(process.env, source)
const platform = (config && config.platform) || getPlatformForEnv(process.env, source)
if (!platform) {
console.log(chalk.red(`Could not find a source code hosting platform for ${source.name}.`))
console.log(
Expand All @@ -30,20 +36,20 @@ export const runRunner = async (app: SharedCLI) => {
}

if (platform) {
jsonDSLGenerator(platform).then(dangerJSONDSL => {
const config: ExecutorOptions = {
stdoutOnly: app.textOnly,
verbose: app.verbose,
jsonOnly: false,
dangerID: app.id || "default",
}

const processInput = prepareDangerDSL(dangerJSONDSL)

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

const config: ExecutorOptions = {
stdoutOnly: app.textOnly,
verbose: app.verbose,
jsonOnly: false,
dangerID: app.id || "default",
}

const processInput = prepareDangerDSL(dangerJSONDSL)

const runnerCommand = dangerRunToRunnerCLI(process.argv)
const exec = new Executor(source, platform, inlineRunner, config)
runDangerSubprocess(runnerCommand, processInput, exec)
}
}
}
39 changes: 0 additions & 39 deletions source/commands/utils/repl.ts

This file was deleted.

1 change: 1 addition & 0 deletions source/commands/utils/sharedDangerfileArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface SharedCLI extends program.CommanderStatic {
textOnly: boolean
dangerfile: string
id: string
repl: string
}

export default (command: any) =>
Expand Down
37 changes: 20 additions & 17 deletions source/runner/runners/_tests/vm2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import vm2 from "../vm2"

import { FakeCI } from "../../../ci_source/providers/Fake"
import { FakePlatform } from "../../../platforms/FakePlatform"
import { Executor } from "../../Executor"
import { Executor, ExecutorOptions } from "../../Executor"

import * as os from "os"
import * as fs from "fs"

import { resolve } from "path"
import { jsonToDSL } from "../../jsonToDSL"
import { jsonDSLGenerator } from "../../dslGenerator"
const fixtures = resolve(__dirname, "../../_tests/fixtures")

// const runners = [{ name: "inline", fn: inlineRunner }, { name: "vm2", fn: vm2 }]
Expand All @@ -20,17 +22,21 @@ const runners = [{ name: "vm2", fn: vm2 }]

runners.forEach(run => {
describe(run.name, () => {
const config: ExecutorOptions = {
stdoutOnly: false,
verbose: false,
jsonOnly: true,
dangerID: run.name,
}

const makeExecutor = () => {
const platform = new FakePlatform()
const config = {
stdoutOnly: false,
verbose: false,
}

exec = new Executor(new FakeCI({}), platform, run.fn, config)

platform.getPlatformGitRepresentation = jest.fn()
platform.getPlatformDSLRepresentation = jest.fn()
platform.getPlatformDSLRepresentation = async () => ({
pr: {},
})
return exec
}

Expand All @@ -40,18 +46,15 @@ runners.forEach(run => {
*/
async function setupDangerfileContext() {
const platform = new FakePlatform()
const config = {
stdoutOnly: false,
verbose: false,
}

exec = new Executor(new FakeCI({}), platform, run.fn, config)

platform.getPlatformGitRepresentation = jest.fn()
platform.getPlatformDSLRepresentation = jest.fn()
// platform.getPlatformGitRepresentation = async () =
// platform.getPlatformDSLRepresentation = jest.fn()

const dsl = await exec.dslForDanger()
return contextForDanger(dsl)
const dsl = await jsonDSLGenerator(platform)
dsl.github = { pr: {} } as any
const realDSL = await jsonToDSL(dsl)
return contextForDanger(realDSL)
}

let exec: Executor
Expand All @@ -78,7 +81,7 @@ runners.forEach(run => {
const exec = makeExecutor()

const dsl = await exec.dslForDanger()
const context = await contextForDanger(dsl)
const context = await setupDangerfileContext()
const runtime = await exec.runner.createDangerfileRuntimeEnvironment(context)

const results = await exec.runner.runDangerfileEnvironment(
Expand Down