-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bdc0c69
commit 080b44a
Showing
1 changed file
with
40 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,68 @@ | ||
// Provides dev-time type structures for `danger` - doesn't affect runtime. | ||
// @ts-ignore | ||
import * as generateMarkdownTable from "markdown-table" | ||
import { DangerDSLType } from "../node_modules/danger/distribution/dsl/DangerDSL" | ||
import { generateCoverageTable } from "./services/generateCoverageTable" | ||
import * as generateMarkdownTable from "markdown-table"; | ||
import { DangerDSLType } from "../node_modules/danger/distribution/dsl/DangerDSL"; | ||
import { generateCoverageTable } from "./services/generateCoverageTable"; | ||
|
||
declare var danger: DangerDSLType | ||
export declare function message(message: string): void | ||
export declare function warn(message: string): void | ||
export declare function fail(message: string): void | ||
export declare function markdown(message: string): void | ||
declare var danger: DangerDSLType; | ||
export declare function message(message: string): void; | ||
export declare function warn(message: string): void; | ||
export declare function fail(message: string): void; | ||
export declare function markdown(message: string): void; | ||
|
||
export interface PluginOptions { | ||
title: string | ||
ignoreCoveragePattern: string[] | ||
coverageFilesPath: string | ||
projectRoot?: string | ||
title: string; | ||
ignoreCoveragePattern: string[]; | ||
coverageFilesPath: string; | ||
projectRoot?: string; | ||
} | ||
|
||
export const defaultPluginOptions: PluginOptions[] = [ | ||
{ | ||
title: "Coverage", | ||
ignoreCoveragePattern: [".test.", ".snap"], | ||
coverageFilesPath: "coverage/coverage-final.json", | ||
}, | ||
] | ||
coverageFilesPath: "coverage/coverage-final.json" | ||
} | ||
]; | ||
/** | ||
* Danger.JS plugin to display the code coverage on a pull request by commenting it via the CI | ||
*/ | ||
export function codeCoverage(pluginOptions: PluginOptions[] = defaultPluginOptions) { | ||
export function codeCoverage( | ||
pluginOptions: PluginOptions[] = defaultPluginOptions | ||
) { | ||
pluginOptions.forEach(options => { | ||
const filterFiles = (file: string) => { | ||
let isFileDisplayed = true | ||
let isFileDisplayed = true; | ||
options.ignoreCoveragePattern.forEach(pattern => { | ||
if (file.includes(pattern)) { | ||
isFileDisplayed = false | ||
isFileDisplayed = false; | ||
} | ||
}) | ||
return isFileDisplayed | ||
} | ||
}); | ||
return isFileDisplayed; | ||
}; | ||
|
||
try { | ||
const coverageTable = [["File", "Branches", "Statements"]] | ||
.concat([[], [":heavy_plus_sign: **NEW FILES**"], []]) | ||
.concat(generateCoverageTable(danger.git.created_files.filter(filterFiles), options)) | ||
.concat( | ||
generateCoverageTable( | ||
danger.git.created_files.filter(filterFiles), | ||
options | ||
) | ||
) | ||
.concat([[], [":pencil2: **MODIFIED FILES**"], []]) | ||
.concat(generateCoverageTable(danger.git.modified_files.filter(filterFiles), options)) | ||
.concat( | ||
generateCoverageTable( | ||
danger.git.modified_files.filter(filterFiles), | ||
options | ||
) | ||
); | ||
|
||
markdown(`# ${options.title}\n${generateMarkdownTable(coverageTable)}`) | ||
markdown(`# ${options.title}\n${generateMarkdownTable(coverageTable)}`); | ||
} catch (error) { | ||
fail(`An error occurred when getting the code coverage: ${error.message}. Danger exits with code: ${error.code}`) | ||
fail( | ||
`An error occurred when getting the code coverage: ${error.message}. Danger exits with code: ${error.code}` | ||
); | ||
} | ||
}) | ||
}); | ||
} |