danger-plugin-todos
Version:
A danger-js plugin to list all todos/fixmes/etc added/changed in a PR
132 lines (131 loc) • 5.11 kB
JavaScript
;
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 });
const lodash_1 = require("lodash");
function getCreatedOrModifiedFiles() {
return [...danger.git.created_files, ...danger.git.modified_files];
}
function shouldIgnoreFile(filepath, ignore) {
for (const ignorePath of ignore) {
if (ignorePath instanceof RegExp && ignorePath.test(filepath)) {
return true;
}
else if (typeof ignorePath === "string" && filepath.includes(ignorePath)) {
return true;
}
}
return false;
}
exports.shouldIgnoreFile = shouldIgnoreFile;
function getMatches(diffString, keyword) {
if (!diffString) {
return [];
}
const escapedKeyword = lodash_1.escapeRegExp(keyword);
const regex = new RegExp(`(?:\\/\\/|#|<!--|;|\\/\\*|^|\\--|\\/\\*\\*\\s*\\**)\\s*${escapedKeyword}(?::\\s*|\\s+)(.+)`, "gi");
const matches = diffString.match(regex);
if (!matches || !matches.length) {
return [];
}
return matches;
}
exports.getMatches = getMatches;
function getFormattedSrcLink(filepath, repoUrl) {
let srcLink = `\`${filepath}\``;
if (repoUrl === true) {
try {
const packageFile = require(`${process.cwd()}/package.json`);
repoUrl = packageFile.repository.url.replace(/\.git$/, '');
}
catch (err) {
//
}
}
// Github style url
if (typeof repoUrl === "string") {
srcLink = `[${filepath}](${repoUrl}/blob/${danger.git.commits.slice(-1)[0].sha}/${filepath})`;
}
else if (typeof repoUrl === "function") {
srcLink = `[${filepath}](${repoUrl(filepath)})`;
}
return srcLink;
}
exports.getFormattedSrcLink = getFormattedSrcLink;
function prepareTodosForDanger(keywords, addedText, removedText, filepath, repoUrl, keywordMatches) {
if (keywords === undefined)
return;
const result = keywordMatches;
keywords.forEach(keyword => {
const addedMatches = getMatches(addedText, keyword);
const removedMatches = getMatches(removedText, keyword);
const srcLink = getFormattedSrcLink(filepath, repoUrl);
addedMatches.forEach(match => {
result[keyword].push(`\`\`${match}\`\`: ${srcLink}`);
});
removedMatches.forEach(match => {
result[keyword].push(`~~${match}~~: ${srcLink}`);
});
});
return result;
}
exports.prepareTodosForDanger = prepareTodosForDanger;
/**
* A danger-js plugin to list all todos/fixmes/etc added/changed in a PR
*/
function todos({ repoUrl = true, ignore = [], keywords = ["TODO", "FIXME"], } = {}) {
return __awaiter(this, void 0, void 0, function* () {
const keywordMatches = {};
keywords.forEach(keyword => {
keywordMatches[keyword] = [];
});
const results = yield Promise.all(getCreatedOrModifiedFiles().map((filepath) => __awaiter(this, void 0, void 0, function* () {
if (shouldIgnoreFile(filepath, ignore)) {
return;
}
let addedText = '';
let removedText = '';
try {
const diff = yield danger.git.diffForFile(filepath);
if (diff) {
addedText = diff.added;
removedText = diff.removed;
}
}
catch (err) {
if (danger.gitlab) {
// Could not get diff, will be using full file
const fileContent = yield danger.gitlab.utils.fileContents(filepath);
addedText = fileContent;
removedText = fileContent;
}
else {
throw err;
}
}
if (!addedText || !removedText) {
return;
}
return prepareTodosForDanger(keywords, addedText, removedText, filepath, repoUrl, keywordMatches);
})));
const mergedKeywordMatches = lodash_1.merge(keywordMatches, ...results);
const output = [];
Object.values(mergedKeywordMatches).forEach(matches => {
if (matches.length) {
output.push(...matches);
}
});
if (!output.length) {
return;
}
markdown(`### ${keywords.join("s/")}s:\n- ${output.join("\n- ")}`);
});
}
exports.default = todos;