1 | const {template} = require('lodash');
|
2 | const debug = require('debug')('semantic-release:github');
|
3 | const parseGithubUrl = require('./parse-github-url');
|
4 | const {ISSUE_ID} = require('./definitions/constants');
|
5 | const resolveConfig = require('./resolve-config');
|
6 | const getClient = require('./get-client');
|
7 | const findSRIssues = require('./find-sr-issues');
|
8 | const getFailComment = require('./get-fail-comment');
|
9 |
|
10 | module.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 |
|
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 | };
|