-
-
Notifications
You must be signed in to change notification settings - Fork 370
/
Copy pathDangerfile.ts
149 lines (131 loc) · 5.38 KB
/
Dangerfile.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { DangerRuntimeContainer } from "../dsl/DangerResults"
import { DangerDSLType, PerilDSL } from "../dsl/DangerDSL"
import { MarkdownString } from "../dsl/Aliases"
/// Start of Danger DSL definition
/** A function with a callback function, which Danger wraps in a Promise */
export type CallbackableFn = (callback: (done: any) => void) => void
/**
* Types of things which Danger will schedule for you, it's recommended that you
* just throw in an `async () => { [...] }` function. Can also handle a function
* that has a single 'done' arg.
*/
export type Scheduleable = Promise<any> | Promise<void> | CallbackableFn
export interface DangerContext {
/**
* A Dangerfile, in Peril, is evaluated as a script, and so async code does not work
* out of the box. By using the `schedule` function you can now register a
* section of code to evaluate across multiple tick cycles.
*
* `schedule` currently handles two types of arguments, either a promise or a function with a resolve arg.
*
* @param {Function} asyncFunction the function to run asynchronously
*/
schedule(asyncFunction: Scheduleable): void
/**
* Highlights critical issues. Message is shown inside a HTML table.
*
* @param {MarkdownString} message the String to output
* @param {string | undefined} file a file which this message should be attached to
* @param {number | undefined} line the line which this message should be attached to
*/
fail(message: MarkdownString, file?: string, line?: number): void
/**
* Highlights low-priority issues. Message is shown inside a HTML table.
*
* @param {MarkdownString} message the String to output
* @param {string | undefined} file a file which this message should be attached to
* @param {number | undefined} line the line which this message should be attached to
*/
warn(message: MarkdownString, file?: string, line?: number): void
/**
* Adds a message to the Danger table, the only difference between this
* and warn is the emoji which shows in the table.
*
* @param {MarkdownString} message the String to output
* @param {string | undefined} file a file which this message should be attached to
* @param {number | undefined} line the line which this message should be attached to
*/
message(message: MarkdownString, file?: string, line?: number): void
/**
* Adds a message to the Danger table, the only difference between this
* and warn is the default emoji which shows in the table.
* You can also specifiy a custom emoji to show in the table for each message
*
* @param {MarkdownString} message the String to output
* @param {{file?: string, line?: string, icon?: MarkdownString}} [opts]
* @param opts.file a file which this message should be attached to
* @param opts.line the line which this message should be attached to
* @param opts.icon icon string or image to show in table, take care not to break table formatting
*/
message(message: MarkdownString, opts?: { file?: string; line?: number; icon?: MarkdownString }): void
/**
* Adds raw markdown into the Danger comment, under the table
*
* @param {MarkdownString} message the String to output
* @param {string | undefined} file a file which this message should be attached to
* @param {number | undefined} line the line which this message should be attached to
*/
markdown(message: MarkdownString, file?: string, line?: number): void
/**
* The root Danger object. This contains all of the metadata you
* will be looking for in order to generate useful rules.
*/
danger: DangerDSLType
/**
* When Peril is running your Dangerfile, the Danger DSL is
* extended with additional options.
*/
peril: PerilDSL
/**
* The current results of a Danger run, this can be useful if you
* are wanting to introspect on whether a build has already failed.
*/
results: DangerRuntimeContainer
}
/// End of Danger DSL definition
/** Creates a Danger context, this provides all of the global functions
* which are available to the Danger eval runtime.
*
* @param {DangerDSLType} dsl The DSL which is turned into `danger`
* @returns {DangerContext} a DangerContext-like API
*/
export function contextForDanger(dsl: DangerDSLType): DangerContext {
const results: DangerRuntimeContainer = {
fails: [],
warnings: [],
messages: [],
markdowns: [],
scheduled: [],
}
const schedule = (fn: any) => results.scheduled && results.scheduled.push(fn)
const fail = (message: MarkdownString, file?: string, line?: number) => results.fails.push({ message, file, line })
const warn = (message: MarkdownString, file?: string, line?: number) => results.warnings.push({ message, file, line })
const message = (
message: MarkdownString,
opts?: string | { file?: string; line?: number; icon?: MarkdownString },
lineArg?: number
) => {
let file: string | undefined
let line: number | undefined
let icon: MarkdownString | undefined
if (typeof opts === "string") {
file = opts
line = lineArg
} else if (typeof opts === "object") {
;({ file, line, icon } = opts)
}
results.messages.push({ message, file, line, icon })
}
const markdown = (message: MarkdownString, file?: string, line?: number) =>
results.markdowns.push({ message, file, line })
return {
schedule,
fail,
warn,
message,
markdown,
results,
danger: dsl,
peril: {} as any,
}
}