UNPKG

semantic-release-gh

Version:

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

122 lines 7.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SuccessGitHub = void 0; const tslib_1 = require("tslib"); const aggregate_error_1 = tslib_1.__importDefault(require("aggregate-error")); const debug_1 = tslib_1.__importDefault(require("debug")); const issue_parser_1 = tslib_1.__importDefault(require("issue-parser")); const lodash_1 = require("lodash"); const p_filter_1 = tslib_1.__importDefault(require("p-filter")); const constants_1 = require("./definitions/constants"); const find_sr_issues_1 = require("./find-sr-issues"); const get_client_1 = require("./get-client"); const get_release_links_1 = require("./get-release-links"); const get_search_queries_1 = require("./get-search-queries"); const get_success_comment_1 = require("./get-success-comment"); const parse_github_url_1 = require("./parse-github-url"); const resolve_config_1 = require("./resolve-config"); const debug = (0, debug_1.default)('semantic-release:github'); const SuccessGitHub = async (pluginConfig, context) => { const { options, commits, nextRelease, releases, logger } = context; const { githubToken, githubUrl, githubApiPathPrefix, proxy, successComment, failComment, failTitle, releasedLabels, addReleases, repositoryUrl } = (0, resolve_config_1.ResolveConfig)(pluginConfig, context); debug('options: %O', options); const github = (0, get_client_1.GetClient)({ githubApiPathPrefix, githubToken, githubUrl, proxy }); const [owner, repo] = (await github.repos.get((0, parse_github_url_1.ParseGitHubURL)(repositoryUrl))).data.full_name.split('/'); const errors = []; if (successComment === false) { logger.log('Skipping commenting on issues and pull requests.'); } else { const parser = (0, issue_parser_1.default)('github', githubUrl ? { hosts: [githubUrl] } : {}); const releaseInfos = releases.filter((release) => Boolean(release.name)); const shas = commits.map(({ hash }) => hash); const searchQueries = (0, get_search_queries_1.GetSearchQueries)(`repo:${owner}/${repo}+type:pr+is:merged`, shas).map(async (q) => (await github.search.issuesAndPullRequests({ q })).data.items); const prs = await (0, p_filter_1.default)((0, lodash_1.uniqBy)((0, lodash_1.flatten)(await Promise.all(searchQueries)), 'number'), async ({ pullNumber }) => (await github.pulls.listCommits({ owner, pull_number: pullNumber, repo })).data.find(({ sha }) => shas.includes(sha)) || shas.includes((await github.pulls.get({ owner, pull_number: pullNumber, repo })).data.merge_commit_sha)); debug('found pull requests: %O', prs.map((pr) => pr.number)); const issues = [...prs.map((pr) => pr.body), ...commits.map((commit) => commit.message)].reduce((issues_, message) => { return message ? issues_.concat(parser(message) .actions.close.filter((action) => (0, lodash_1.isNil)(action.slug) || action.slug === `${owner}/${repo}`) .map((action) => ({ number: Number.parseInt(action.issue, 10) }))) : issues_; }, []); debug('found issues via comments: %O', issues); await Promise.all((0, lodash_1.uniqBy)([...prs, ...issues], 'number').map(async (issue) => { const body = successComment ? (0, lodash_1.template)(successComment)(Object.assign(Object.assign({}, context), { issue })) : (0, get_success_comment_1.GetSuccessComment)(issue, releaseInfos, nextRelease); try { const comment = { body, issue_number: issue.number, owner, repo }; debug('create comment: %O', comment); const { data: { html_url: url } } = await github.issues.createComment(comment); logger.log('Added comment to issue #%d: %s', issue.number, url); if (releasedLabels) { const labels = releasedLabels.map((label) => (0, lodash_1.template)(label)(context)); await github.request('POST /repos/:owner/:repo/issues/:number/labels', { data: labels, number: issue.number, owner, repo }); logger.log('Added labels %O to issue #%d', labels, issue.number); } } catch (error) { const HTTP_STATUS_403 = 403; const HTTP_STATUS_404 = 404; if (error.status === HTTP_STATUS_403) { logger.error('Not allowed to add a comment to the issue #%d.', issue.number); } else if (error.status === HTTP_STATUS_404) { logger.log("Failed to add a comment to the issue #%d as it doesn't exist. This issue might be on GitLab.", issue.number); } else { ; errors.push(error); logger.error('Failed to add a comment to the issue #%d.', issue.number); } } })); } if (failComment === false || failTitle === false) { logger.log('Skip closing issue.'); } else { const srIssues = await (0, find_sr_issues_1.FindSRIssues)(github, failTitle, owner, repo); debug('found semantic-release issues: %O', srIssues); await Promise.all(srIssues.map(async (issue) => { debug('close issue: %O', issue); try { const updateIssue = { issue_number: issue.number, owner, repo, state: 'closed' }; debug('closing issue: %O', updateIssue); const { data: { html_url: url } } = await github.issues.update(updateIssue); logger.log('Closed issue #%d: %s.', issue.number, url); } catch (error) { ; errors.push(error); logger.error('Failed to close the issue #%d.', issue.number); } })); } if (addReleases !== false && errors.length === 0) { const ghRelease = releases.find((release) => release.name && release.name === constants_1.RELEASE_NAME); if (!(0, lodash_1.isNil)(ghRelease)) { const ghRelaseId = ghRelease.id; const additionalReleases = (0, get_release_links_1.GetReleaseLinks)(releases); if (!(0, lodash_1.isEmpty)(additionalReleases) && !(0, lodash_1.isNil)(ghRelaseId)) { const newBody = addReleases === 'top' ? [...additionalReleases, '\n---\n', ...nextRelease.notes].join('') : [...nextRelease.notes, '\n---\n', ...additionalReleases].join(''); await github.repos.updateRelease({ body: newBody, owner, release_id: ghRelaseId, repo }); } } } if (errors.length > 0) { throw new aggregate_error_1.default(errors); } }; exports.SuccessGitHub = SuccessGitHub; //# sourceMappingURL=success.js.map