UNPKG

@sectester/reporter

Version:

Provide an abstraction for generating test results as part of the particular test frameworks.

60 lines 2.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GitHubCheckRunReporter = void 0; const tslib_1 = require("tslib"); const api_1 = require("./api"); const builders_1 = require("./builders"); const tsyringe_1 = require("tsyringe"); const node_path_1 = tslib_1.__importDefault(require("node:path")); // TODO add `GitHubCheckRunReporter` description to README let GitHubCheckRunReporter = class GitHubCheckRunReporter { constructor(config, githubClient) { this.config = config; this.githubClient = githubClient; if (!this.config.token) { throw new Error('GitHub token is not set'); } if (!this.config.repository) { throw new Error('GitHub repository is not set'); } if (!this.config.commitSha) { throw new Error('GitHub commitSha is not set'); } } async report(scan) { const issues = await scan.issues(); if (issues.length === 0) return; const checkRunPayload = this.createCheckRunPayloadBuilder(issues).build(); await this.githubClient.createCheckRun(checkRunPayload); } createCheckRunPayloadBuilder(issues) { return issues.length === 1 ? new builders_1.SingleItemPayloadBuilder(issues[0], this.config.commitSha, this.getTestFilePath()) : new builders_1.MultiItemsPayloadBuilder(issues, this.config.commitSha, this.getTestFilePath()); } // TODO subject to improvement getTestFilePath() { var _a; // Check if running in Jest environment const jestState = (_a = global.expect) === null || _a === void 0 ? void 0 : _a.getState(); if (jestState) { const testPath = jestState.testPath; const rootDir = jestState.snapshotState._rootDir; return node_path_1.default.join(node_path_1.default.basename(rootDir), node_path_1.default.relative(rootDir, testPath)); } // Relies on `TestContext` from Node.js built-in test runner appearing in the stack const matchRes = String(new Error().stack).match(/\n\s+at (?:async )?TestContext.* \((.*):\d+:\d+\)\n/); return (matchRes === null || matchRes === void 0 ? void 0 : matchRes[1]) ? node_path_1.default.relative(process.cwd(), matchRes[1] || '') : 'unknown'; } }; exports.GitHubCheckRunReporter = GitHubCheckRunReporter; exports.GitHubCheckRunReporter = GitHubCheckRunReporter = tslib_1.__decorate([ (0, tsyringe_1.injectable)(), tslib_1.__param(0, (0, tsyringe_1.inject)(api_1.GITHUB_CONFIG)), tslib_1.__param(1, (0, tsyringe_1.inject)(api_1.GITHUB_CLIENT)), tslib_1.__metadata("design:paramtypes", [Object, Object]) ], GitHubCheckRunReporter); //# sourceMappingURL=GitHubCheckRunReporter.js.map