Permalink
Switch branches/tags
actions_dockerfile actions add_create_pr add_message_system add_update_issue adds_hmtl_url async-orta-dangerfile azz-bitbucket-server bbs_fixture both_issue_check checks_2 ci comments create_label docs_docs exec_to_spawn fake fb/update_github_rest_dependency fix_logs fix_pr greenkeeper/babel-cli-pin-6.22.1 greenkeeper/babel-plugin-transform-regenerator-pin-6.21.0 greenkeeper/babel-preset-es2015-pin-6.18.0 greenkeeper/commander-2.19.0 greenkeeper/commander-pin-2.18.0 greenkeeper/debug-4.1.0 greenkeeper/debug-pin-2.6.1 greenkeeper/flow-bin-0.78.0 greenkeeper/flow-bin-0.79.0 greenkeeper/flow-bin-0.79.1 greenkeeper/flow-bin-0.80.0 greenkeeper/flow-bin-0.81.0 greenkeeper/flow-bin-0.82.0 greenkeeper/flow-bin-0.83.0 greenkeeper/flow-bin-0.84.0 greenkeeper/flow-bin-0.85.0 greenkeeper/husky-0.13.1 greenkeeper/husky-1.1.1 greenkeeper/husky-1.1.2 greenkeeper/husky-pin-1.1.0 greenkeeper/jest-config-pin-20.0.3 greenkeeper/jest-pin-19.0.0 greenkeeper/jest-runtime-pin-20.0.1 greenkeeper/jest-runtime-pin-20.0.3 greenkeeper/lint-staged-8.0.0 greenkeeper/lint-staged-8.0.1 greenkeeper/lint-staged-8.0.2 greenkeeper/lint-staged-8.0.3 greenkeeper/lint-staged-pin-3.2.6 greenkeeper/lint-staged-pin-3.4.1 greenkeeper/monorepo.babel7-7.1.3 greenkeeper/monorepo.babel7-7.1.4 greenkeeper/node-fetch-pin-1.7.0 greenkeeper/@octokit/rest-15.13.0 greenkeeper/@octokit/rest-15.13.1 greenkeeper/@octokit/rest-15.14.0 greenkeeper/@octokit/rest-15.15.0 greenkeeper/@octokit/rest-15.15.1 greenkeeper/@octokit/rest-pin-15.12.1 greenkeeper/ts-jest-23.10.4 greenkeeper/ts-jest-pin-18.0.1 greenkeeper/ts-jest-pin-19.0.2 greenkeeper/ts-jest-pin-20.0.5 greenkeeper/ts-jest-pin-23.10.3 greenkeeper/ts-node-pin-3.0.4 greenkeeper/tslint-pin-4.3.1 greenkeeper/tslint-pin-4.4.1 greenkeeper/tslint-pin-4.4.2 greenkeeper/tslint-pin-5.2.0 greenkeeper/tslint-pin-5.3.0 greenkeeper/typedoc-0.10.0 greenkeeper/typedoc-0.11.0 greenkeeper/typedoc-0.11.1 greenkeeper/typedoc-0.12.0 greenkeeper/typedoc-0.13.0 greenkeeper/@types/commander-pin-2.9.0 greenkeeper/@types/debug-0.0.31 greenkeeper/@types/jest-23.3.3 greenkeeper/@types/jest-23.3.4 greenkeeper/@types/jest-23.3.5 greenkeeper/@types/jest-23.3.6 greenkeeper/@types/jest-23.3.7 greenkeeper/@types/jest-23.3.8 greenkeeper/@types/jest-pin-16.0.4 greenkeeper/@types/jest-pin-23.3.3 greenkeeper/@types/node-10.11.4 greenkeeper/@types/node-10.11.5 greenkeeper/@types/node-10.11.6 greenkeeper/@types/node-10.11.7 greenkeeper/@types/node-10.12.0 greenkeeper/@types/node-10.12.1 greenkeeper/typescript-json-schema-0.33.0 greenkeeper/typescript-pin-2.2.0 hybrid_checks improve_peril_checks jenkins-dx lodash logs_ci master matcher more_dep_up more_error_logs multi-files n10 orta-patch-1 package_updates paulmelnikow-refactor-cli peril_override_require prettier_new pull/156/head relative_files remove_changelog_step report_status_err special_markdowns strict tdoc tighten_commands type_fetch types_jest_updates typescript/strict update_deps_ update_or_delete updates_to_iterate_issue use_absolute_resolving ux_fixes vm2_published wip_remove_ghapi
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
262 lines (227 sloc) 7.37 KB
import { Violation, isInline } from "../dsl/Violation"
/**
* The representation of what running a Dangerfile generates.
* This needs to be passed between processes, so data only please.
*/
export interface DangerResults {
/**
* Failed messages
*/
fails: Violation[]
/**
* Messages for info
*/
warnings: Violation[]
/**
* A set of messages to show inline
*/
messages: Violation[]
/**
* Markdown messages to attach at the bottom of the comment
*/
markdowns: Violation[]
}
export interface DangerRuntimeContainer extends DangerResults {
/**
* Asynchronous functions to be run after parsing
*/
scheduled?: any[]
}
export interface DangerInlineResults {
/**
* Path to the file
*/
file: string
/**
* Line in the file
*/
line: number
/**
* Failed messages
*/
fails: string[]
/**
* Messages for info
*/
warnings: string[]
/**
* A set of messages to show inline
*/
messages: string[]
/**
* Markdown messages to attach at the bottom of the comment
*/
markdowns: string[]
}
/// End of Danger DSL definition
export const emptyDangerResults = {
fails: [],
warnings: [],
messages: [],
markdowns: [],
}
export function validateResults(results: DangerResults) {
// The data we get back is JSON sent by STDIN that can come from many
// consumers, let's take the time to ensure the data is how we think it is.
const { fails, warnings, messages, markdowns } = results
const props = { fails, warnings, messages, markdowns }
Object.keys(props).forEach(name => {
//
// Must include the key 4 types
if (!props[name]) {
throw new Error(`Results passed to Danger JS did not include ${name}.\n\n${JSON.stringify(results, null, " ")}`)
}
const violations: Violation[] = props[name]
violations.forEach(v => {
// They should always have a message
if (!v.message) {
throw new Error(
`A violation passed to Danger JS in ${name} did not include \`message\`.\n\n${JSON.stringify(v, null, " ")}`
)
}
// Warn if anything other than the initial API is on a violation
const officialAPI = ["message", "line", "file"]
const keys = Object.keys(v).filter(f => !officialAPI.includes(f))
if (keys.length) {
console.warn(`Received unexpected key in Violation, expected only ${officialAPI} but got ${Object.keys(v)}`)
}
})
})
}
/** Returns only the inline violations from Danger results */
export function inlineResults(results: DangerResults): DangerResults {
return {
fails: results.fails.filter(m => isInline(m)),
warnings: results.warnings.filter(m => isInline(m)),
messages: results.messages.filter(m => isInline(m)),
markdowns: results.markdowns.filter(m => isInline(m)),
}
}
/** Returns only the main-comment comments violations from Danger results */
export function regularResults(results: DangerResults): DangerResults {
return {
fails: results.fails.filter(m => !isInline(m)),
warnings: results.warnings.filter(m => !isInline(m)),
messages: results.messages.filter(m => !isInline(m)),
markdowns: results.markdowns.filter(m => !isInline(m)),
}
}
/** Concat all the violations into a new results */
export function mergeResults(results1: DangerResults, results2: DangerResults): DangerResults {
return {
fails: results1.fails.concat(results2.fails),
warnings: results1.warnings.concat(results2.warnings),
messages: results1.messages.concat(results2.messages),
markdowns: results1.markdowns.concat(results2.markdowns),
}
}
/** Sorts all of the results according to their files and lines */
export function sortInlineResults(inlineResults: DangerInlineResults[]): DangerInlineResults[] {
// First sort messages in every inline result
const sortedInlineResults = inlineResults.map(i => {
return {
file: i.file,
line: i.line,
fails: i.fails.sort(),
warnings: i.warnings.sort(),
messages: i.messages.sort(),
markdowns: i.markdowns.sort(),
}
})
// Then sort a whole array of inline results based on file/line
return sortedInlineResults.sort((a, b) => {
if (a.file < b.file) {
return -1
} else if (a.file > b.file) {
return 1
} else if (a.line < b.line) {
return -1
} else if (a.line > b.line) {
return 1
} else {
// both file & line are the same
return 0
}
})
}
export function sortResults(results: DangerResults): DangerResults {
const sortByFile = (a: Violation, b: Violation): number => {
if (a.file === undefined) {
return -1
}
if (b.file === undefined) {
return 1
}
if (a.file == b.file) {
if (a.line == undefined) {
return -1
}
if (b.line == undefined) {
return 1
}
if (a.line < b.line) {
return -1
} else if (a.line > b.line) {
return 1
} else {
return 0
}
}
if (a.file < b.file) {
return -1
} else {
return 1
}
}
return {
fails: results.fails.sort(sortByFile),
warnings: results.warnings.sort(sortByFile),
messages: results.messages.sort(sortByFile),
markdowns: results.markdowns.sort(sortByFile),
}
}
export const emptyResults = (): DangerResults => ({ fails: [], markdowns: [], warnings: [], messages: [] })
export const isEmptyResults = (results: DangerResults): boolean =>
[...results.fails, ...results.warnings, ...results.messages, ...results.markdowns].length === 0
export const isMarkdownOnlyResults = (results: DangerResults): boolean =>
results.markdowns.length > 0 && [...results.fails, ...results.warnings, ...results.messages].length === 0
export function resultsIntoInlineResults(results: DangerResults): DangerInlineResults[] {
// Here we iterate through all keys ("fails", "warnings", "messages", "markdowns") and for each violation
// in given kind we produce new DangerInlineResult or append a violation to existing result. This is all
// happening in a `violationsIntoInlineResults` function that mutates an out-of-scope variable `dangerInlineResults`.
const dangerInlineResults: DangerInlineResults[] = []
const violationsIntoInlineResults = (kind: string) => {
for (let violation of results[kind]) {
if (violation.file && violation.line) {
const findInlineResult = dangerInlineResults.find(r => r.file == violation.file && r.line == violation.line)
if (findInlineResult) {
findInlineResult[kind].push(violation.message)
} else {
const inlineResult = {
file: violation.file,
line: violation.line,
fails: [],
warnings: [],
messages: [],
markdowns: [],
}
inlineResult[kind].push(violation.message)
dangerInlineResults.push(inlineResult)
}
}
}
}
Object.keys(results).forEach(violationsIntoInlineResults)
return dangerInlineResults
}
export function inlineResultsIntoResults(inlineResults: DangerInlineResults): DangerResults {
const messageToViolation = (message: string): Violation => {
return { message: message, file: inlineResults.file, line: inlineResults.line }
}
return {
fails: inlineResults.fails.map(messageToViolation),
warnings: inlineResults.warnings.map(messageToViolation),
messages: inlineResults.messages.map(messageToViolation),
markdowns: inlineResults.markdowns.map(messageToViolation),
}
}