Skip to content

Commit

Permalink
initial implementation to handle async
Browse files Browse the repository at this point in the history
  • Loading branch information
deecewan committed Feb 20, 2017
1 parent bebde7d commit b8cc328
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
5 changes: 5 additions & 0 deletions source/dsl/DangerResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import { MarkdownString } from "../dsl/Aliases"
* do not add functions, only data to this interface.
*/
export interface DangerResults {
/**
* Asynchronous functions to be run after parsing
* @type {Function[]}
*/
scheduled: Array<Function>
/**
* Failed messages
* @type {Violation[]}
Expand Down
15 changes: 14 additions & 1 deletion source/runner/Dangerfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import { DangerDSLType } from "../dsl/DangerDSL"
import { MarkdownString } from "../dsl/Aliases"

export interface DangerContext {
/**
* Contains asynchronous code to be run after the application has booted.
*
* @param {Function} asyncFunction the function to run asynchronously
*/
schedule(asyncFunction: (p: Promise<any>) => void): void

/**
* Fails a build, outputting a specific reason for failing
*
Expand Down Expand Up @@ -59,15 +66,21 @@ export function contextForDanger(dsl: DangerDSLType): DangerContext {
fails: [],
warnings: [],
messages: [],
markdowns: []
markdowns: [],
scheduled: []
}

const schedule = (fn: Function) => {
console.log("scheduling task", fn)
results.scheduled.push(fn)
}
const fail = (message: MarkdownString) => results.fails.push({ message })
const warn = (message: MarkdownString) => results.warnings.push({ message })
const message = (message: MarkdownString) => results.messages.push({ message })
const markdown = (message: MarkdownString) => results.markdowns.push(message)

return {
schedule,
fail,
warn,
message,
Expand Down
3 changes: 3 additions & 0 deletions source/runner/Executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ export class Executor {
* @returns {void} It's a promise, so a void promise
*/
async handleResults(results: DangerResults) {
// run the asynchronous code first
console.log("handling scheduled tasks", results.scheduled)
await Promise.all(results.scheduled)
// Ensure process fails if there are fails
if (results.fails.length) {
process.exitCode = 1
Expand Down
13 changes: 13 additions & 0 deletions source/runner/_tests/fixtures/ExampleDangerResults.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,49 @@
import { DangerResults } from "../../../dsl/DangerResults"

export const emptyResults: DangerResults = {
scheduled: [],
fails: [],
warnings: [],
messages: [],
markdowns: []
}

export const failsResultsWithoutMessages: DangerResults = {
scheduled: [],
fails: [{}, {}],
warnings: [],
messages: [],
markdowns: []
}

export const warnResults: DangerResults = {
scheduled: [],
fails: [],
warnings: [{ message: "Warning message" }],
messages: [],
markdowns: []
}

export const failsResults: DangerResults = {
scheduled: [],
fails: [{ message: "Failing message" }],
warnings: [],
messages: [],
markdowns: []
}

export const summaryResults: DangerResults = {
scheduled: [],
fails: [{ message: "Failing message Failing message" }],
warnings: [{ message: "Warning message Warning message" }],
messages: [{ message: "message" }],
markdowns: ["markdown"],
}

export const asyncResults: DangerResults = {
scheduled: [async () => 1],
fails: [],
warnings: [],
messages: [],
markdowns: [],
}

0 comments on commit b8cc328

Please sign in to comment.