UNPKG

2.11 kBJavaScriptView Raw
1const {template} = require('lodash');
2const parseGithubUrl = require('parse-github-url');
3const debug = require('debug')('semantic-release:github');
4const ISSUE_ID = require('./definitions/sr-issue-id');
5const resolveConfig = require('./resolve-config');
6const getClient = require('./get-client');
7const findSRIssues = require('./find-sr-issues');
8const getFailComment = require('./get-fail-comment');
9
10module.exports = async (pluginConfig, context) => {
11 const {
12 options: {branch, repositoryUrl},
13 errors,
14 logger,
15 } = context;
16 const {githubToken, githubUrl, githubApiPathPrefix, proxy, failComment, failTitle, labels, assignees} = resolveConfig(
17 pluginConfig,
18 context
19 );
20
21 if (failComment === false || failTitle === false) {
22 logger.log('Skip issue creation.');
23 } else {
24 const github = getClient({githubToken, githubUrl, githubApiPathPrefix, proxy});
25 let {name: repo, owner} = parseGithubUrl(repositoryUrl);
26 // In case the repo changed name, get the new `repo`/`owner` as the search API will not follow redirects
27 [owner, repo] = (await github.repos.get({repo, owner})).data.full_name.split('/');
28 const body = failComment ? template(failComment)({branch, errors}) : getFailComment(branch, errors);
29 const [srIssue] = await findSRIssues(github, failTitle, owner, repo);
30
31 if (srIssue) {
32 logger.log('Found existing semantic-release issue #%d.', srIssue.number);
33 // eslint-disable-next-line camelcase
34 const comment = {owner, repo, issue_number: srIssue.number, body};
35 debug('create comment: %O', comment);
36 const {
37 data: {html_url: url},
38 } = await github.issues.createComment(comment);
39 logger.log('Added comment to issue #%d: %s.', srIssue.number, url);
40 } else {
41 const newIssue = {owner, repo, title: failTitle, body: `${body}\n\n${ISSUE_ID}`, labels: labels || [], assignees};
42 debug('create issue: %O', newIssue);
43 const {
44 data: {html_url: url, number},
45 } = await github.issues.create(newIssue);
46 logger.log('Created issue #%d: %s.', number, url);
47 }
48 }
49};