Skip to content

Commit

Permalink
build: add prepare job
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Cohnen committed Nov 19, 2021
1 parent 9b255d6 commit 8b22c74
Show file tree
Hide file tree
Showing 26 changed files with 880 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -61,7 +61,7 @@ typings/
.env

# Generated code
dist/
# dist/

### VisualStudioCode ###
.vscode/*
Expand Down
20 changes: 20 additions & 0 deletions dist/checkstyleconfig.d.ts
@@ -0,0 +1,20 @@
interface CheckstyleConfig {
/**
* File mask used to find XML checkstyle reports.
*/
fileMask: string;
/**
* If set to true, the severity will be used to switch between the different message formats (message, warn, fail).
*/
reportSeverity: boolean;
/**
* If set to true, only issues will be reported that are contained in the current changeset (line comparison).
* If set to false, all issues that are in modified files will be reported.
*/
requireLineModification: boolean;
/**
* Optional: Sets a prefix foreach violation message.
* This can be useful if there are multiple reports being parsed to make them distinguishable.
*/
outputPrefix?: string;
}
Empty file added dist/checkstyleconfig.js
Empty file.
9 changes: 9 additions & 0 deletions dist/file/file.d.ts
@@ -0,0 +1,9 @@
import { GitDSL } from "danger";
/**
* If a git object is supplied, this function checks if a given relative file path
* is within the changeset (modified + created files).
* @param git Git object
* @param relativeFilePath Sanitized file path to match with the changeset
*/
export declare function isFileInChangeset(git: GitDSL | undefined, relativeFilePath: string): boolean;
export declare function getChangedLinesByFile(git: GitDSL, relativeFilePath: string): Promise<number[]>;
41 changes: 41 additions & 0 deletions dist/file/file.js
@@ -0,0 +1,41 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getChangedLinesByFile = exports.isFileInChangeset = void 0;
/**
* If a git object is supplied, this function checks if a given relative file path
* is within the changeset (modified + created files).
* @param git Git object
* @param relativeFilePath Sanitized file path to match with the changeset
*/
function isFileInChangeset(git, relativeFilePath) {
if (git) {
const isModified = git.modified_files.includes(relativeFilePath);
const isAdded = git.created_files.includes(relativeFilePath);
if (isModified || isAdded) {
// get changed lines
return true;
}
}
else {
return true;
}
return false;
}
exports.isFileInChangeset = isFileInChangeset;
function getChangedLinesByFile(git, relativeFilePath) {
return git.structuredDiffForFile(relativeFilePath).then(diff => {
const lines = [];
if (diff) {
diff.chunks.forEach(chunk => {
chunk.changes.forEach(change => {
if (change.type === "add") {
// is addition
lines.push(change.ln);
}
});
});
}
return lines;
});
}
exports.getChangedLinesByFile = getChangedLinesByFile;
1 change: 1 addition & 0 deletions dist/file/file.test.d.ts
@@ -0,0 +1 @@
export {};
50 changes: 50 additions & 0 deletions dist/file/file.test.js
@@ -0,0 +1,50 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const file_1 = require("./file");
describe("isFileInChangeset()", () => {
it("Undefined git returns true as fallback", () => {
const path = "/file/path";
global.danger = {
github: { pr: { title: "My Test Title" } },
git: undefined,
};
const result = file_1.isFileInChangeset(global.danger.git, path);
expect(result).toBeTruthy();
});
it("Path is in modified files", () => {
const path = "/file/path";
global.danger = {
github: { pr: { title: "My Test Title" } },
git: {
modified_files: [path],
created_files: [],
},
};
const result = file_1.isFileInChangeset(global.danger.git, path);
expect(result).toBeTruthy();
});
it("Path is not in modified or created files", () => {
const path = "/file/path";
global.danger = {
github: { pr: { title: "My Test Title" } },
git: {
modified_files: [],
created_files: [],
},
};
const result = file_1.isFileInChangeset(global.danger.git, path);
expect(result).toBeFalsy();
});
it("Path is in created files", () => {
const path = "/file/path";
global.danger = {
github: { pr: { title: "My Test Title" } },
git: {
modified_files: [],
created_files: [path],
},
};
const result = file_1.isFileInChangeset(global.danger.git, path);
expect(result).toBeTruthy();
});
});
4 changes: 4 additions & 0 deletions dist/filediff.d.ts
@@ -0,0 +1,4 @@
interface FileDiff {
file: string;
added_lines: number[];
}
Empty file added dist/filediff.js
Empty file.
12 changes: 12 additions & 0 deletions dist/index.d.ts
@@ -0,0 +1,12 @@
export declare const maxParallel = 10;
declare type MarkdownString = string;
export declare function message(message: MarkdownString, file?: string, line?: number): void;
export declare function warn(message: string, file?: string, line?: number): void;
export declare function fail(message: string, file?: string, line?: number): void;
export declare function markdown(message: string, file?: string, line?: number): void;
/**
* This plugin reads checkstyle reports (XML) and posts inline comments in pull requests.
*/
export declare function scan(config: CheckstyleConfig): Promise<void>;
export declare function scanXmlReport(git: any, xmlReport: any, root: any, requireLineModification: any, messageCallback: (msg: any, file: any, line: any, severity: any) => void): Promise<void>;
export {};
67 changes: 67 additions & 0 deletions dist/index.js
@@ -0,0 +1,67 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.scanXmlReport = exports.scan = exports.maxParallel = void 0;
const fs_1 = require("fs");
const checkstyle_1 = require("./parse/checkstyle");
exports.maxParallel = 10;
/**
* This plugin reads checkstyle reports (XML) and posts inline comments in pull requests.
*/
function scan(config) {
return __awaiter(this, void 0, void 0, function* () {
const glob = require("glob");
const root = process.cwd();
const git = danger.git;
const files = yield new Promise((resolve, reject) => glob(config.fileMask, (err, result) => (err ? reject(err) : resolve(result))));
for (const batch of Array.from({ length: Math.ceil(files.length / exports.maxParallel) }, (_, batchIdx) => files.slice(batchIdx * exports.maxParallel, (batchIdx + 1) * exports.maxParallel))) {
yield Promise.all(batch.map((fileName) => __awaiter(this, void 0, void 0, function* () {
const xmlReport = fs_1.readFileSync(fileName);
yield scanXmlReport(git, xmlReport, root, config.requireLineModification, (msg, file, line, severity) => {
if (!config.reportSeverity) {
severity = "info";
}
if (config.outputPrefix) {
msg = config.outputPrefix + msg;
}
sendViolationBySeverity(msg, file, line, severity);
});
})));
}
});
}
exports.scan = scan;
function scanXmlReport(git, xmlReport, root, requireLineModification, messageCallback) {
return __awaiter(this, void 0, void 0, function* () {
const xmlConverter = require("xml-js");
const report = xmlConverter.xml2js(xmlReport);
yield checkstyle_1.scanReport(git, report, root, requireLineModification, messageCallback);
});
}
exports.scanXmlReport = scanXmlReport;
function sendViolationBySeverity(msg, fileName, line, severity) {
switch (severity.toLowerCase()) {
case "information":
case "info":
message(msg, fileName, line);
break;
case "warning":
case "warn":
warn(msg, fileName, line);
break;
case "error":
fail(msg, fileName, line);
break;
case "fatal":
fail(msg, fileName, line);
break;
}
}
9 changes: 9 additions & 0 deletions dist/index.test.d.ts
@@ -0,0 +1,9 @@
declare global {
namespace NodeJS {
interface Global {
danger: any;
warn: any;
}
}
}
export {};

0 comments on commit 8b22c74

Please sign in to comment.