forked from release-drafter/release-drafter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
325 lines (294 loc) · 9.02 KB
/
index.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
const yaml = require('js-yaml')
const { getConfig } = require('./lib/config')
const { isTriggerableReference } = require('./lib/triggerable-reference')
const {
findReleases,
generateReleaseInfo,
createRelease,
updateRelease,
} = require('./lib/releases')
const { findCommitsWithAssociatedPullRequests } = require('./lib/commits')
const { sortPullRequests } = require('./lib/sort-pull-requests')
const { log } = require('./lib/log')
const core = require('@actions/core')
const { runnerIsActions } = require('./lib/utils')
const ignore = require('ignore')
module.exports = (app, { getRouter }) => {
if (!runnerIsActions() && typeof getRouter === 'function') {
getRouter().get('/healthz', (request, response) => {
response.status(200).json({ status: 'pass' })
})
}
app.on(
[
'pull_request.opened',
'pull_request.reopened',
'pull_request.synchronize',
'pull_request.edited',
'pull_request_target.opened',
'pull_request_target.reopened',
'pull_request_target.synchronize',
'pull_request_target.edited',
],
async (context) => {
const { disableAutolabeler } = getInput()
const config = await getConfig({
context,
configName: core.getInput('config-name'),
})
if (config === null || disableAutolabeler) return
let issue = {
...context.issue({ pull_number: context.payload.pull_request.number }),
}
const changedFiles = await context.octokit.paginate(
context.octokit.pulls.listFiles.endpoint.merge(issue),
(response) => response.data.map((file) => file.filename)
)
const labels = new Set()
for (const autolabel of config['autolabeler']) {
let found = false
// check modified files
if (!found && autolabel.files.length > 0) {
const matcher = ignore().add(autolabel.files)
if (changedFiles.some((file) => matcher.ignores(file))) {
labels.add(autolabel.label)
found = true
log({
context,
message: `Found label for files: '${autolabel.label}'`,
})
}
}
// check branch names
if (!found && autolabel.branch.length > 0) {
for (const matcher of autolabel.branch) {
if (matcher.test(context.payload.pull_request.head.ref)) {
labels.add(autolabel.label)
found = true
log({
context,
message: `Found label for branch: '${autolabel.label}'`,
})
break
}
}
}
// check pr title
if (!found && autolabel.title.length > 0) {
for (const matcher of autolabel.title) {
if (matcher.test(context.payload.pull_request.title)) {
labels.add(autolabel.label)
found = true
log({
context,
message: `Found label for title: '${autolabel.label}'`,
})
break
}
}
}
// check pr body
if (
!found &&
context.payload.pull_request.body != null &&
autolabel.body.length > 0
) {
for (const matcher of autolabel.body) {
if (matcher.test(context.payload.pull_request.body)) {
labels.add(autolabel.label)
found = true
log({
context,
message: `Found label for body: '${autolabel.label}'`,
})
break
}
}
}
}
const labelsToAdd = [...labels]
if (labelsToAdd.length > 0) {
let labelIssue = {
...context.issue({
issue_number: context.payload.pull_request.number,
labels: labelsToAdd,
}),
}
await context.octokit.issues.addLabels(labelIssue)
if (runnerIsActions()) {
core.setOutput('number', context.payload.pull_request.number)
core.setOutput('labels', labelsToAdd.join(','))
}
return
}
}
)
const drafter = async (context) => {
const {
shouldDraft,
configName,
version,
tag,
name,
disableReleaser,
commitish,
} = getInput()
const config = await getConfig({
context,
configName,
})
const { isPreRelease, latest } = getInput({ config })
if (config === null || disableReleaser) return
// GitHub Actions merge payloads slightly differ, in that their ref points
// to the PR branch instead of refs/heads/master
const ref = process.env['GITHUB_REF'] || context.payload.ref
if (!isTriggerableReference({ ref, context, config })) {
return
}
const targetCommitish = commitish || config['commitish'] || ref
const {
'filter-by-commitish': filterByCommitish,
'include-pre-releases': includePreReleases,
'tag-prefix': tagPrefix,
} = config
// overrides passed as input
const header = core.getInput('header')
const footer = core.getInput('footer')
const includePaths = parseInput(core.getInput('include-paths'))
if (header) {
config['header'] = header
}
if (footer) {
config['footer'] = footer
}
if (includePaths) {
config['include-paths'] = includePaths
}
const { draftRelease, lastRelease } = await findReleases({
context,
targetCommitish,
filterByCommitish,
includePreReleases,
tagPrefix,
})
const { commits, pullRequests: mergedPullRequests } =
await findCommitsWithAssociatedPullRequests({
context,
targetCommitish,
lastRelease,
config,
})
const sortedMergedPullRequests = sortPullRequests(
mergedPullRequests,
config['sort-by'],
config['sort-direction']
)
const releaseInfo = generateReleaseInfo({
context,
commits,
config,
lastRelease,
mergedPullRequests: sortedMergedPullRequests,
version,
tag,
name,
isPreRelease,
latest,
shouldDraft,
targetCommitish,
})
let createOrUpdateReleaseResponse
if (!draftRelease) {
log({ context, message: 'Creating new release' })
createOrUpdateReleaseResponse = await createRelease({
context,
releaseInfo,
config,
})
} else {
log({ context, message: 'Updating existing release' })
createOrUpdateReleaseResponse = await updateRelease({
context,
draftRelease,
releaseInfo,
config,
})
}
if (runnerIsActions()) {
setActionOutput(createOrUpdateReleaseResponse, releaseInfo)
}
}
if (runnerIsActions()) {
app.onAny(drafter)
} else {
app.on('push', drafter)
}
}
function parseInput(input) {
try {
// First, attempt to parse as JSON
return JSON.parse(input)
} catch {
// If that throws an error, attempt to parse as YAML
try {
return yaml.load(input)
} catch {
throw new Error('Input could not be parsed as JSON or YAML')
}
}
}
function getInput({ config } = {}) {
// Returns all the inputs that doesn't need a merge with the config file
if (!config) {
return {
shouldDraft: core.getInput('publish').toLowerCase() !== 'true',
configName: core.getInput('config-name'),
version: core.getInput('version') || undefined,
tag: core.getInput('tag') || undefined,
name: core.getInput('name') || undefined,
disableReleaser:
core.getInput('disable-releaser').toLowerCase() === 'true',
disableAutolabeler:
core.getInput('disable-autolabeler').toLowerCase() === 'true',
commitish: core.getInput('commitish') || undefined,
}
}
// Merges the config file with the input
// the input takes precedence, because it's more easy to change at runtime
const preRelease = core.getInput('prerelease').toLowerCase()
const isPreRelease =
preRelease === 'true' || (!preRelease && config.prerelease)
const latestInput = core.getInput('latest').toLowerCase()
const latest = isPreRelease
? 'false'
: (!latestInput && config.latest) || latestInput || undefined
return {
isPreRelease,
latest,
}
}
function setActionOutput(
releaseResponse,
{ body, resolvedVersion, majorVersion, minorVersion, patchVersion }
) {
const {
data: {
id: releaseId,
html_url: htmlUrl,
upload_url: uploadUrl,
tag_name: tagName,
name: name,
},
} = releaseResponse
if (releaseId && Number.isInteger(releaseId))
core.setOutput('id', releaseId.toString())
if (htmlUrl) core.setOutput('html_url', htmlUrl)
if (uploadUrl) core.setOutput('upload_url', uploadUrl)
if (tagName) core.setOutput('tag_name', tagName)
if (name) core.setOutput('name', name)
if (resolvedVersion) core.setOutput('resolved_version', resolvedVersion)
if (majorVersion) core.setOutput('major_version', majorVersion)
if (minorVersion) core.setOutput('minor_version', minorVersion)
if (patchVersion) core.setOutput('patch_version', patchVersion)
core.setOutput('body', body)
}