UNPKG

@zooshdigital/bitbucket-eslint-report

Version:

Command-line tool to upload eslint reports to Bitbucket

130 lines (129 loc) 6.35 kB
#!/usr/bin/env node "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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const promises_1 = __importDefault(require("fs/promises")); const path_1 = __importDefault(require("path")); const command_line_args_1 = __importDefault(require("command-line-args")); const bitbucket_code_insights_1 = require("@zooshdigital/bitbucket-code-insights"); const logger_1 = require("./utils/logger"); const optionDefinitions = [ { name: 'name', alias: 'n', type: String }, { name: 'path', alias: 'p', type: String, multiple: true }, { name: 'add-build', alias: 'a', type: Boolean }, { name: 'strict', alias: 's', type: Boolean }, ]; const args = (0, command_line_args_1.default)(optionDefinitions); const logger = (0, logger_1.createLogger)('Zoosh Bitbucket Eslint Report'); const severities = { 0: 'LOW', 1: 'MEDIUM', 2: 'HIGH', }; function uploadReport() { return __awaiter(this, void 0, void 0, function* () { const items = []; let counter = 0; let totalErrors = 0; let fixableErrors = 0; let totalWarnings = 0; let fixableWarnings = 0; try { const { name, path: reportPaths, 'add-build': addBuild, strict } = args; if (!name || !reportPaths || reportPaths.length === 0) { throw new Error('Bitbucket Eslint Report - Usage: bitbucket-eslint-report -n <report-name> -p <report-path> [-b] [-s]'); } yield Promise.all(reportPaths.map((reportPath) => __awaiter(this, void 0, void 0, function* () { const lint = JSON.parse(yield promises_1.default.readFile(reportPath, 'utf8')); lint.forEach((file) => { var _a; // const filePath = file.filePath.replace(/^\/opt\/atlassian\/pipelines\/agent\/build\//, ''); const filePath = path_1.default.relative((_a = process.env.BITBUCKET_CLONE_DIR) !== null && _a !== void 0 ? _a : process.cwd(), file.filePath); totalErrors = totalErrors + file.errorCount; fixableErrors = fixableErrors + file.fixableErrorCount; totalWarnings = totalWarnings + file.warningCount; fixableWarnings = fixableWarnings + file.fixableWarningCount; file.messages.forEach((message) => { counter++; items.push({ external_id: `${name}.${process.env.BITBUCKET_COMMIT}.${counter}`, title: `Eslint: ${message.ruleId}`, annotation_type: 'CODE_SMELL', summary: `Eslint error: ${message.ruleId} ${message.fix ? `| fix: ${message.fix.text}` : ''}`, severity: severities[message.severity], path: filePath, line: message.line, }); }); }); }))); const passed = strict ? totalErrors + totalWarnings === 0 : totalErrors === 0; yield (0, bitbucket_code_insights_1.uploadReportToBitbucket)(name, { title: `${name} (${totalErrors} errors, ${totalWarnings} warnings)`, report_type: 'TEST', details: `${name} report`, result: passed ? 'PASSED' : 'FAILED', data: [ { title: 'Errors', type: 'NUMBER', value: totalErrors, }, { title: 'Fixable Errors', type: 'NUMBER', value: fixableErrors, }, { title: 'Warnings', type: 'NUMBER', value: totalWarnings, }, { title: 'Fixable Warnings', type: 'NUMBER', value: fixableWarnings, }, ], }); if (items.length > 0) { const chunkSize = 100; const chunks = items.reduce((acc, _, i) => { if (i % chunkSize === 0) acc.push(items.slice(i, i + chunkSize)); return acc; }, []); yield Promise.all(chunks.map((chunk) => (0, bitbucket_code_insights_1.uploadAnnotationsToBitbucket)(name, chunk))); } logger.log('Report uploaded successfully', name); if (addBuild) { const url = process.env.BITBUCKET_PR_ID ? `https://bitbucket.org/${process.env.BITBUCKET_REPO_FULL_NAME}/pull-requests/${process.env.BITBUCKET_PR_ID}/diff` : `https://bitbucket.org/${process.env.BITBUCKET_REPO_FULL_NAME}/commit/${process.env.BITBUCKET_COMMIT}`; yield (0, bitbucket_code_insights_1.createBuildOnBitbucket)({ key: name, state: passed ? 'SUCCESSFUL' : 'FAILED', name, description: `${totalErrors} errors, ${totalWarnings} warnings`, url, }); } } catch (error) { logger.log(`Error uploading report: ${error}`); process.exit(1); } }); } uploadReport();