UNPKG

bundlewatch

Version:
104 lines (85 loc) 3.39 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _logger = require('../../../logger'); var _logger2 = _interopRequireDefault(_logger); var _axios = require('axios'); var _axios2 = _interopRequireDefault(_axios); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } class bundlewatchService { constructor({ repoOwner, repoName, repoBranchBase, repoCurrentBranch, commitSha, bundlewatchServiceHost, githubAccessToken }) { this.repoOwner = repoOwner; this.repoName = repoName; this.repoBranchBase = repoBranchBase; this.repoCurrentBranch = repoCurrentBranch; this.commitSha = commitSha; this.bundlewatchServiceHost = bundlewatchServiceHost; this.githubAccessToken = githubAccessToken; } get bundlewatchServiceStoreUrl() { return `${this.bundlewatchServiceHost}/store`; } get enabled() { if (this.githubAccessToken && this.repoOwner && this.repoName && this.bundlewatchServiceHost) { return true; } return false; } getFileDetailsForBaseBranch() { if (!this.enabled || !this.repoBranchBase) { return Promise.resolve({}); } _logger2.default.info(`Retrieving comparison`); return _axios2.default.post(`${this.bundlewatchServiceStoreUrl}/lookup`, { repoOwner: this.repoOwner, repoName: this.repoName, repoBranch: this.repoBranchBase, githubAccessToken: this.githubAccessToken, commitSha: this.commitSha }, { timeout: 10000 }).then(response => { return response.data.fileDetailsByPath; }).catch(error => { _logger2.default.debug(error); _logger2.default.error(`Unable to fetch fileDetails for baseBranch=${this.repoBranchBase} from ${this.bundlewatchServiceStoreUrl} code=${error.code || error.message}`); return {}; }); } saveFileDetailsForCurrentBranch({ fileDetailsByPath, trackBranches }) { if (!this.enabled || !this.repoCurrentBranch) { return Promise.resolve(); } if (this.repoBranchBase && this.repoCurrentBranch !== this.repoBranchBase) { _logger2.default.info(`${this.repoBranchBase} !== ${this.repoCurrentBranch}, no results saved`); } if (!trackBranches.includes(this.repoCurrentBranch)) { _logger2.default.info(`${this.repoCurrentBranch} is not a branch to track, no results saved`); return Promise.resolve(); } _logger2.default.info(`Saving results`); return _axios2.default.post(`${this.bundlewatchServiceStoreUrl}`, { repoOwner: this.repoOwner, repoName: this.repoName, repoBranch: this.repoCurrentBranch, githubAccessToken: this.githubAccessToken, commitSha: this.commitSha, fileDetailsByPath }, { timeout: 10000 }).catch(error => { _logger2.default.debug(error); _logger2.default.error(`Unable to save fileDetails for currentBranch=${this.repoCurrentBranch} code=${error.code || error.message}`); }); } } exports.default = bundlewatchService;