-
Notifications
You must be signed in to change notification settings - Fork 119
/
success.js
245 lines (225 loc) · 7.41 KB
/
success.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import { isNil, uniqBy, template, flatten, isEmpty } from "lodash-es";
import pFilter from "p-filter";
import AggregateError from "aggregate-error";
import issueParser from "issue-parser";
import debugFactory from "debug";
import parseGithubUrl from "./parse-github-url.js";
import resolveConfig from "./resolve-config.js";
import { toOctokitOptions } from "./octokit.js";
import getSearchQueries from "./get-search-queries.js";
import getSuccessComment from "./get-success-comment.js";
import findSRIssues from "./find-sr-issues.js";
import { RELEASE_NAME } from "./definitions/constants.js";
import getReleaseLinks from "./get-release-links.js";
const debug = debugFactory("semantic-release:github");
export default async function success(pluginConfig, context, { Octokit }) {
const {
options: { repositoryUrl },
commits,
nextRelease,
releases,
logger,
} = context;
const {
githubToken,
githubUrl,
githubApiPathPrefix,
proxy,
successComment,
failComment,
failTitle,
releasedLabels,
addReleases,
} = resolveConfig(pluginConfig, context);
const octokit = new Octokit(
toOctokitOptions({ githubToken, githubUrl, githubApiPathPrefix, proxy }),
);
// In case the repo changed name, get the new `repo`/`owner` as the search API will not follow redirects
const { data: repoData } = await octokit.request(
"GET /repos/{owner}/{repo}",
parseGithubUrl(repositoryUrl),
);
const [owner, repo] = repoData.full_name.split("/");
const errors = [];
if (successComment === false) {
logger.log("Skip commenting on issues and pull requests.");
} else {
const parser = issueParser(
"github",
githubUrl ? { hosts: [githubUrl] } : {},
);
const releaseInfos = releases.filter((release) => Boolean(release.name));
const shas = commits.map(({ hash }) => hash);
const searchQueries = getSearchQueries(
`repo:${owner}/${repo}+type:pr+is:merged`,
shas,
).map(
async (q) =>
(await octokit.request("GET /search/issues", { q })).data.items,
);
const searchQueriesResults = await Promise.all(searchQueries);
const uniqueSearchQueriesResults = uniqBy(
flatten(searchQueriesResults),
"number",
);
const prs = await pFilter(
uniqueSearchQueriesResults,
async ({ number }) => {
const commits = await octokit.paginate(
"GET /repos/{owner}/{repo}/pulls/{pull_number}/commits",
{
owner,
repo,
pull_number: number,
},
);
const matchingCommit = commits.find(({ sha }) => shas.includes(sha));
if (matchingCommit) return matchingCommit;
const { data: pullRequest } = await octokit.request(
"GET /repos/{owner}/{repo}/pulls/{pull_number}",
{
owner,
repo,
pull_number: number,
},
);
return shas.includes(pullRequest.merge_commit_sha);
},
);
debug(
"found pull requests: %O",
prs.map((pr) => pr.number),
);
// Parse the release commits message and PRs body to find resolved issues/PRs via comment keyworkds
const issues = [
...prs.map((pr) => pr.body),
...commits.map((commit) => commit.message),
].reduce(
(issues, message) =>
message
? issues.concat(
parser(message)
.actions.close.filter(
(action) =>
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(
uniqBy([...prs, ...issues], "number").map(async (issue) => {
const body = successComment
? template(successComment)({ ...context, issue })
: getSuccessComment(issue, releaseInfos, nextRelease);
try {
const comment = { owner, repo, issue_number: issue.number, body };
debug("create comment: %O", comment);
const {
data: { html_url: url },
} = await octokit.request(
"POST /repos/{owner}/{repo}/issues/{issue_number}/comments",
comment,
);
logger.log("Added comment to issue #%d: %s", issue.number, url);
if (releasedLabels) {
const labels = releasedLabels.map((label) =>
template(label)(context),
);
await octokit.request(
"POST /repos/{owner}/{repo}/issues/{issue_number}/labels",
{
owner,
repo,
issue_number: issue.number,
data: labels,
},
);
logger.log("Added labels %O to issue #%d", labels, issue.number);
}
} catch (error) {
if (error.status === 403) {
logger.error(
"Not allowed to add a comment to the issue #%d.",
issue.number,
);
} else if (error.status === 404) {
logger.error(
"Failed to add a comment to the issue #%d as it doesn't exist.",
issue.number,
);
} else {
errors.push(error);
logger.error(
"Failed to add a comment to the issue #%d.",
issue.number,
);
// Don't throw right away and continue to update other issues
}
}
}),
);
}
if (failComment === false || failTitle === false) {
logger.log("Skip closing issue.");
} else {
const srIssues = await findSRIssues(octokit, 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 = {
owner,
repo,
issue_number: issue.number,
state: "closed",
};
debug("closing issue: %O", updateIssue);
const {
data: { html_url: url },
} = await octokit.request(
"PATCH /repos/{owner}/{repo}/issues/{issue_number}",
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);
// Don't throw right away and continue to close other issues
}
}),
);
}
if (addReleases !== false && errors.length === 0) {
const ghRelease = releases.find(
(release) => release.name && release.name === RELEASE_NAME,
);
if (!isNil(ghRelease)) {
const ghRelaseId = ghRelease.id;
const additionalReleases = getReleaseLinks(releases);
if (!isEmpty(additionalReleases) && !isNil(ghRelaseId)) {
const newBody =
addReleases === "top"
? additionalReleases.concat("\n---\n", nextRelease.notes)
: nextRelease.notes.concat("\n---\n", additionalReleases);
await octokit.request(
"PATCH /repos/{owner}/{repo}/releases/{release_id}",
{
owner,
repo,
release_id: ghRelaseId,
body: newBody,
},
);
}
}
}
if (errors.length > 0) {
throw new AggregateError(errors);
}
}