quality-bot
Version:
QBot Online Code Review Application
101 lines (95 loc) • 5.33 kB
JavaScript
const fs = require('fs');
const path = require("path");
const tempDirectory = require('temp-dir');
let currentdate = new Date();
let reportHeaderContent = fs.readFileSync(path.resolve(__dirname, "./../report/report-headers.html"), "utf-8");
let reportCommentCotent = fs.readFileSync(path.resolve(__dirname, "./../report/report-comment.html"), "utf-8");
let precommitContent = fs.readFileSync(path.resolve(__dirname, "./../template/precommit-template-shell.txt"), "utf-8");
let precommitNodeContent = fs.readFileSync(path.resolve(__dirname, "./../template/precommit-template-node.txt"), "utf-8");
let finalReportCommentContent = "";
let finalReportContent = "";
let finalreportHeaderContent = "";
const createReportFile = async (comments, name, filesLength, repoDetails) => {
finalreportHeaderContent = reportHeaderContent.replace("$date$", new Date(currentdate.getTime()))
.replace("$name$", name)
.replace("$filesCount$", filesLength)
.replace("$comments$", comments.length)
.replace("$projectName$", repoDetails.data.projectName)
.replace("$repoName$", repoDetails.data.repoName);
comments.forEach(comment => {
let reportContentForComment = reportCommentCotent.replace("$file$", comment.location.path)
.split("$ruleName$").join(comment.checkName)
.replace("${message}", comment.description || 'No Description Found')
.replace("${startLine}", comment.location.lines.begin)
.replace("${endLine}", comment.location.lines.end)
.replace("$exampleCode$", comment.exampleCode || 'No Example Found')
.replace("$description$", comment.fullDescription || 'No Description Found')
.replace("$Sevirity$", comment.sevirity || 'Error')
.replace("$languageName$", comment.languageName)
finalReportCommentContent = finalReportCommentContent + reportContentForComment;
});
finalReportContent = finalreportHeaderContent.replace("$commentContent$", finalReportCommentContent)
createFileInTempLocation(finalReportContent);
}
const createFileInTempLocation = (finalReportContent) => {
const dir = tempDirectory + '/qbot-report';
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
fs.writeFileSync(dir + "/report.html", finalReportContent);
fs.writeFileSync(
dir + '/qlogo.png',
fs.readFileSync(path.resolve(__dirname, "./../report/qlogo.png"))
);
}
const createPreCommitFile = (path) => {
const devCommand = 'exec npx quality-bot review-dev $against';
const prodCommand = 'exec npx quality-bot review $against';
const devArgument = 'review-dev';
const prodArgument = 'review';
const preCommitCommandArgument = process.argv[2] === "configure-dev" ? devArgument : prodArgument;
const preCommitCommand = process.argv[2] === "configure-dev" ? devCommand : prodCommand;
const fileName = "/pre-commit";
if (fs.existsSync(path + fileName)) {
let existingContent = fs.readFileSync(path + fileName, "utf-8");
if (existingContent.includes("#!/usr/bin/env node")) {
if (existingContent.includes("\'quality-bot\'")) {
if (preCommitCommandArgument === devArgument && existingContent.includes("\'" + prodArgument + "\'")) {
precommitContent = existingContent.replace(prodArgument, devArgument);
fs.writeFileSync(path + fileName, precommitContent);
} else if (preCommitCommandArgument === prodArgument && existingContent.includes("\'" + devArgument + "\'")) {
precommitContent = existingContent.replace(devArgument, prodArgument);
fs.writeFileSync(path + fileName, precommitContent);
}
}
else {
precommitContent = existingContent + "\n" + precommitNodeContent.replace('$env-cmd$', preCommitCommandArgument);
fs.writeFileSync(path + fileName, precommitContent);
}
} else if (existingContent.includes("#!/bin/sh")) {
if (existingContent.includes("npx quality-bot")) {
if (preCommitCommand === devCommand && existingContent.includes(prodCommand)) {
precommitContent = existingContent.replace(prodCommand, devCommand);
fs.writeFileSync(path + fileName, precommitContent);
} else if (preCommitCommand === prodCommand && existingContent.includes(devCommand)) {
precommitContent = existingContent.replace(devCommand, prodCommand);
fs.writeFileSync(path + fileName, precommitContent);
}
}
else {
precommitContent = existingContent + "\n" + precommitContent.replace("#!/bin/sh", "") + preCommitCommand + "\n";
fs.writeFileSync(path + fileName, precommitContent);
}
} else if (!existingContent) {
precommitContent = precommitContent + "\n" + preCommitCommand;
fs.writeFileSync(path + fileName, precommitContent);
}
} else {
precommitContent = precommitContent + "\n" + preCommitCommand;
fs.writeFileSync(path + fileName, precommitContent);
}
}
module.exports = {
createReportFile,
createPreCommitFile,
}