UNPKG

semantic-release-gh

Version:

The official GitHub plugin, modified to accept repositoryUrl as a parameter

87 lines 4.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.VerifyGitHub = void 0; const tslib_1 = require("tslib"); const aggregate_error_1 = tslib_1.__importDefault(require("aggregate-error")); const lodash_1 = require("lodash"); const url_join_1 = tslib_1.__importDefault(require("url-join")); const get_client_1 = require("./get-client"); const get_error_1 = require("./get-error"); const parse_github_url_1 = require("./parse-github-url"); const resolve_config_1 = require("./resolve-config"); const isNonEmptyString = (value) => (0, lodash_1.isString)(value) && value.trim(); const oneOf = (enumArray) => (value) => enumArray.includes(value); const isStringOrStringArray = (value) => isNonEmptyString(value) || ((0, lodash_1.isArray)(value) && value.every((string) => isNonEmptyString(string))); const isArrayOf = (validator) => (array) => (0, lodash_1.isArray)(array) && array.every((value) => validator(value)); const canBeDisabled = (validator) => (value) => value === false || validator(value); const VALIDATORS = { addReleases: canBeDisabled(oneOf(['bottom', 'top'])), assets: isArrayOf((asset) => isStringOrStringArray(asset) || ((0, lodash_1.isPlainObject)(asset) && isStringOrStringArray(asset.path))), assignees: isArrayOf(isNonEmptyString), failComment: canBeDisabled(isNonEmptyString), failTitle: canBeDisabled(isNonEmptyString), labels: canBeDisabled(isArrayOf(isNonEmptyString)), proxy: canBeDisabled((proxy) => isNonEmptyString(proxy) || ((0, lodash_1.isPlainObject)(proxy) && isNonEmptyString(proxy.host) && (0, lodash_1.isNumber)(proxy.port))), releasedLabels: canBeDisabled(isArrayOf(isNonEmptyString)), repositoryUrl: canBeDisabled(isNonEmptyString), successComment: canBeDisabled(isNonEmptyString) }; const VerifyGitHub = async (pluginConfig, context) => { let { env, options: { repositoryUrl }, logger } = context; const _a = (0, resolve_config_1.ResolveConfig)(pluginConfig, context), { githubToken, githubUrl, githubApiPathPrefix, proxy } = _a, options = tslib_1.__rest(_a, ["githubToken", "githubUrl", "githubApiPathPrefix", "proxy"]); repositoryUrl = pluginConfig.repositoryUrl; const verifyErrors = Object.entries(Object.assign(Object.assign({}, options), { proxy })).reduce((errors, [option, value]) => !(0, lodash_1.isNil)(value) && !VALIDATORS[option](value) ? [...errors, (0, get_error_1.GetError)(`EINVALID${option.toUpperCase()}`, { [option]: value })] : errors, []); if (githubUrl) { logger.log('Verify GitHub authentication (%s)', (0, url_join_1.default)(githubUrl, githubApiPathPrefix)); } else { logger.log('Verify GitHub authentication'); } const { repo, owner } = (0, parse_github_url_1.ParseGitHubURL)(repositoryUrl); if (!owner || !repo) { ; verifyErrors.push((0, get_error_1.GetError)('EINVALIDGITHUBURL')); } else if (githubToken && !verifyErrors.some(({ code }) => code === 'EINVALIDPROXY')) { const github = (0, get_client_1.GetClient)({ githubApiPathPrefix, githubToken, githubUrl, proxy }); if (env.GITHUB_ACTION) { return; } try { const { data: { permissions: { push } } } = await github.repos.get({ owner, repo }); if (!push) { if (await github.request('HEAD /installation/repositories', { per_page: 1 }).catch(() => false)) { return; } ; verifyErrors.push((0, get_error_1.GetError)('EGHNOPERMISSION', { owner, repo })); } } catch (error) { const HTTP_STATUS_401 = 401; const HTTP_STATUS_404 = 404; if (error.status === HTTP_STATUS_401) { ; verifyErrors.push((0, get_error_1.GetError)('EINVALIDGHTOKEN', { owner, repo })); } else if (error.status === HTTP_STATUS_404) { ; verifyErrors.push((0, get_error_1.GetError)('EMISSINGREPO', { owner, repo })); } else { throw error; } } } if (!githubToken) { ; verifyErrors.push((0, get_error_1.GetError)('ENOGHTOKEN', { owner, repo })); } if (verifyErrors.length > 0) { throw new aggregate_error_1.default(verifyErrors); } }; exports.VerifyGitHub = VerifyGitHub; //# sourceMappingURL=verify.js.map