Skip to content

Commit

Permalink
When a Dangerfile has an error, raise the error back to the PR instea…
Browse files Browse the repository at this point in the history
…d of just the console.
  • Loading branch information
orta committed Apr 7, 2017
1 parent c779c3a commit 282782f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
2 changes: 1 addition & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### Master


* When a Dangerfile fails to eval, send a message to the PR - orta

### 0.14.2

Expand Down
32 changes: 29 additions & 3 deletions source/runner/Executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { DangerResults } from "../dsl/DangerResults"
import { template as githubResultsTemplate } from "./templates/githubIssueTemplate"
import { createDangerfileRuntimeEnvironment, runDangerfileEnvironment } from "./DangerfileRunner"
import { DangerfileRuntimeEnv } from "./types"
import exceptionRaisedTemplate from "./templates/exceptionRaisedTemplate"

import * as debug from "debug"
import * as chalk from "chalk"
Expand Down Expand Up @@ -53,7 +54,16 @@ export class Executor {
*/

async runDanger(file: string, runtime: DangerfileRuntimeEnv) {
const results = await runDangerfileEnvironment(file, runtime)
let results = {} as DangerResults

// If an eval of the Dangerfile fails, we should generate a
// message that can go back to the CI
try {
results = await runDangerfileEnvironment(file, runtime)
} catch (error) {
results = this.resultsForError(error)
}

await this.handleResults(results)
return results
}
Expand Down Expand Up @@ -87,7 +97,7 @@ export class Executor {
* @param {DangerResults} results a JSON representation of the end-state for a Danger run
*/
async handleResultsPostingToSTDOUT(results: DangerResults) {
const {fails, warnings, messages, markdowns} = results
const { fails, warnings, messages, markdowns } = results

const table = [
{ name: "Failures", messages: fails.map(f => f.message) },
Expand Down Expand Up @@ -127,7 +137,7 @@ export class Executor {
*/
async handleResultsPostingToPlatform(results: DangerResults) {
// Delete the message if there's nothing to say
const {fails, warnings, messages, markdowns} = results
const { fails, warnings, messages, markdowns } = results

const failureCount = [...fails, ...warnings].length
const messageCount = [...messages, ...markdowns].length
Expand Down Expand Up @@ -160,4 +170,20 @@ export class Executor {
await this.handleResultsPostingToSTDOUT(results)
}
}

/**
* Takes an error (maybe a bad eval) and provides a DangerResults compatible object
* @param error Any JS error
*/
resultsForError(error: Error) {
// Need a failing error, otherwise it won't fail CI.
console.error(chalk.red("Danger has errored"))
console.error(error)
return {
fails: [{ message: "Running your Dangerfile has Failed" }],
warnings: [],
messages: [],
markdowns: [exceptionRaisedTemplate(error)]
}
}
}
22 changes: 21 additions & 1 deletion source/runner/_tests/_executor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,31 @@ describe("setup", () => {
expect(platform.getPlatformDSLRepresentation).toBeCalled()
})

it("gets diff / pr info in setup", async () => {
it("gets diff / pr info / utils in setup", async () => {
const exec = new Executor(new FakeCI({}), new FakePlatform(), defaultConfig)
const dsl = await exec.dslForDanger()
expect(dsl.git).toBeTruthy()
expect(dsl.github).toBeTruthy()
expect(dsl.utils).toBeTruthy()
})

it("Creates a DangerResults for a raising dangerfile", async () => {
const platform = new FakePlatform()
const exec = new Executor(new FakeCI({}), platform, defaultConfig)

// This is a real error occuring when Danger modifies the Dangerfile
// as it is given a path of ""
const error = {
name: "Error",
message: "ENOENT: no such file or directory",
}

const results = await exec.runDanger("", {} as any)
expect(results.fails.length).toBeGreaterThan(0)

const markdown = results.markdowns[0]
expect(markdown).toMatch(error.name)
expect(markdown).toMatch(error.message)
})

it("Deletes a post when there are no messages", async () => {
Expand Down
12 changes: 12 additions & 0 deletions source/runner/templates/exceptionRaisedTemplate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const quotes = "```"

export default (error: Error) => `
## Danger has errored
Error: ${error.name}
${quotes}sh
${error.stack}
${quotes}
`

0 comments on commit 282782f

Please sign in to comment.