UNPKG

2.03 kBJavaScriptView Raw
1const {template} = require('lodash');
2const debug = require('debug')('semantic-release:github');
3const parseGithubUrl = require('./parse-github-url');
4const {ISSUE_ID} = require('./definitions/constants');
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: {repositoryUrl},
13 branch,
14 errors,
15 logger,
16 } = context;
17 const {githubToken, githubUrl, githubApiPathPrefix, proxy, failComment, failTitle, labels, assignees} = resolveConfig(
18 pluginConfig,
19 context
20 );
21
22 if (failComment === false || failTitle === false) {
23 logger.log('Skip issue creation.');
24 } else {
25 const github = getClient({githubToken, githubUrl, githubApiPathPrefix, proxy});
26 // In case the repo changed name, get the new `repo`/`owner` as the search API will not follow redirects
27 const [owner, repo] = (await github.repos.get(parseGithubUrl(repositoryUrl))).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 const comment = {owner, repo, issue_number: srIssue.number, body};
34 debug('create comment: %O', comment);
35 const {
36 data: {html_url: url},
37 } = await github.issues.createComment(comment);
38 logger.log('Added comment to issue #%d: %s.', srIssue.number, url);
39 } else {
40 const newIssue = {owner, repo, title: failTitle, body: `${body}\n\n${ISSUE_ID}`, labels: labels || [], assignees};
41 debug('create issue: %O', newIssue);
42 const {
43 data: {html_url: url, number},
44 } = await github.issues.create(newIssue);
45 logger.log('Created issue #%d: %s.', number, url);
46 }
47 }
48};