UNPKG

@codechecks/client

Version:

Open source platform for code review automation

115 lines 4.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const path_1 = require("path"); const Local_1 = require("./ci-providers/Local"); const errors_1 = require("./utils/errors"); const urlJoin = require("url-join"); class NotPrError extends Error { constructor() { super("Not a PR!"); } } exports.NotPrError = NotPrError; class CodechecksClient { constructor(api, context) { this.api = api; this.context = context; this.reports = []; } getPublicProjectSlug() { if (this.context.isLocalMode) { return this.context.isLocalMode.projectSlug; } if (this.context.isFork) { return this.context.projectSlug; } } async getValue(name) { if (!this.context.pr) { throw errors_1.crash("Not a PR!"); } return this.api.getValue(name, this.context.pr.base.sha, this.getPublicProjectSlug()); } async saveValue(name, value) { if (this.context.isLocalMode) { return; } return this.api.saveValue(name, value, this.context.currentSha, this.getPublicProjectSlug()); } async getFile(name, destinationPath) { if (!this.context.pr) { throw errors_1.crash("Not a PR!"); } return this.api.getFile(`${this.context.pr.base.sha}/${name}`, destinationPath, this.getPublicProjectSlug()); } async saveFile(name, filePath) { if (this.context.isLocalMode) { return; } return this.api.saveFile(`${this.context.currentSha}/${name}`, filePath, this.getPublicProjectSlug()); } async getDirectory(name, destinationPath) { if (!this.context.pr) { throw errors_1.crash("Not a PR!"); } return this.api.getDirectory(name, destinationPath, this.context.pr.base.sha, this.getPublicProjectSlug()); } async saveDirectory(name, directoryPath) { if (this.context.isLocalMode) { return; } return this.api.saveDirectory(name, directoryPath, this.context.currentSha, this.getPublicProjectSlug()); } async report(report) { this.reports.push(report); if (this.context.isLocalMode) { return Local_1.processReport(report, this.context); } else { return this.api.makeCommitCheck(this.context.currentSha, this.context.pr && this.context.pr.base.sha, [report], this.getPublicProjectSlug()); } } async success(report) { return this.report(Object.assign({}, report, { status: "success" })); } async failure(report) { return this.report(Object.assign({}, report, { status: "failure" })); } isPr() { return this.context.isPr; } countFailures() { return this.reports.filter(r => r.status === "failure").length; } countSuccesses() { return this.reports.filter(r => r.status === "success").length; } /** * Get browseable link to artifact without it's own domain ie. artifacts.codechecks.io/123/456/report/index.html */ getArtifactLink(path) { if (this.context.isPrivate) { return urlJoin(this.context.artifactsProxy.url, this.context.currentSha, path); } else { // this could be simplified in future and projectSlug could become part of artifactsProxy.url returned from a backend so this wouldn't branch return urlJoin(this.context.artifactsProxy.url, this.context.projectSlug, this.context.currentSha, path); } } /** * Get browseable link to artifact with it's own domain ie. 123--456--report.artifacts.codechecks.io/index.html * Useful for SPAs that manipulate URLs. It will redirect '/' to index.html and you can't customize this behaviour. */ getPageLink(dirPath, filenamePath = "") { // in some environments this can be unavailable so just fallback to getArtifactLink // @todo remove prefixed dir path like /build => build if (!this.context.artifactsProxy.supportsPages) { return this.getArtifactLink(path_1.join(dirPath, filenamePath)); } const [protocol, urlWithoutProtocol] = this.context.artifactsProxy.url.split("://"); const subDomainsChain = [this.context.projectSlug, this.context.currentSha, ...dirPath.split("/")]; return urlJoin(`${protocol}://${subDomainsChain.join("--")}.${urlWithoutProtocol}`, filenamePath); } } exports.CodechecksClient = CodechecksClient; //# sourceMappingURL=client.js.map