-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
main.ts
587 lines (529 loc) · 14.3 KB
/
main.ts
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
import {
getInput,
getBooleanInput,
setFailed,
info,
error,
warning,
setOutput,
startGroup,
endGroup
} from '@actions/core'
import got from 'got'
import { readFileSync } from 'fs'
import { join, normalize } from 'path'
import semverDiff from 'semver-diff'
const semverRE =
/^(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(?:-(?<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/
const semverReGlobal =
/(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(?:-(?<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?/gm
const packageFileName = normalize(getInput('file-name') || 'package.json'),
dir = process.env.GITHUB_WORKSPACE || '/github/workspace',
eventFile = process.env.GITHUB_EVENT_PATH || '/github/workflow/event.json',
assumeSameVersion = getInput('assume-same-version') as
| 'old'
| 'new'
| undefined,
staticChecking = getInput('static-checking') as
| 'localIsNew'
| 'remoteIsNew'
| undefined,
token = getInput('token')
let packageFileURL = (getInput('file-url') || '').trim()
const allowedTags = ['::before']
type outputKey = 'changed' | 'type' | 'version' | 'commit'
const outputs: Partial<Record<outputKey, string | boolean>> = {}
// #region Functions
async function main() {
if (
packageFileURL &&
!isURL(packageFileURL) &&
!allowedTags.includes(packageFileURL)
)
return setFailed(
`The provided package file URL is not valid (received: ${packageFileURL})`
)
if (assumeSameVersion && !['old', 'new'].includes(assumeSameVersion))
return setFailed(
`The provided assume-same-version parameter is not valid (received ${assumeSameVersion})`
)
if (staticChecking && !['localIsNew', 'remoteIsNew'].includes(staticChecking))
return setFailed(
`The provided static-checking parameter is not valid (received ${staticChecking})`
)
const isPackageFileURLBefore = packageFileURL === '::before'
if (isPackageFileURLBefore) {
const event = await readJson(eventFile)
if (!event) throw new Error(`Can't find event file (${eventFile})`)
const { before, repository } = event
if (before && repository) {
packageFileURL = `https://raw.githubusercontent.com/${repository?.full_name}/${before}/${packageFileName}`
startGroup('URL tag resolution...')
info(
`::before tag resolved to ${repository?.full_name}/${String(
before
).substr(0, 7)}/${packageFileName}`
)
info(`Current package file URL: ${packageFileURL}`)
info(`Using token for remote url: ${!!token}`)
endGroup()
} else
throw new Error(
`Can't correctly read event file (before: ${before}, repository: ${repository})`
)
}
if (staticChecking) {
if (!packageFileURL)
return setFailed(
'Static checking cannot be performed without a `file-url` argument.'
)
startGroup('Static-checking files...')
info(`Package file name: "${packageFileName}"`)
info(`Package file URL: "${packageFileURL}"`)
const local: string = (await readJson(join(dir, packageFileName)))?.version,
remote: string = (
await readJson(
packageFileURL,
(isPackageFileURLBefore || isRawGithubUrl(packageFileURL)) && token
? token
: undefined
)
)?.version
if (!local || !remote) {
endGroup()
return setFailed(`Couldn't find ${local ? 'local' : 'remote'} version.`)
}
if (!semverRE.test(local)) {
return setFailed(`Local version does not match semver pattern`)
}
if (!semverRE.test(remote)) {
return setFailed(`Remote version does not match semver pattern`)
}
const versionDiff =
staticChecking === 'localIsNew'
? semverDiff(remote, local)
: semverDiff(local, remote)
if (versionDiff) {
output('changed', true)
output('version', staticChecking == 'localIsNew' ? local : remote)
output('type', versionDiff)
endGroup()
info(
`Found match for version ${
staticChecking == 'localIsNew' ? local : remote
}`
)
}
} else {
const eventObj = await readJson(eventFile)
const commits =
eventObj.commits ||
(await request(eventObj.pull_request._links.commits.href))
await processDirectory(dir, commits)
}
}
function isURL(str: string) {
try {
new URL(str)
return true
} catch {
return false
}
}
function isRawGithubUrl(str: string) {
const url = new URL(str)
return url.hostname === 'raw.githubusercontent.com'
}
async function readJson(file: string, token?: string) {
if (isURL(file)) {
const headers = token
? {
Authorization: `token ${token}`
}
: {}
return (
await got({
url: file,
method: 'GET',
headers,
responseType: 'json'
})
).body
} else {
const data = readFileSync(file, { encoding: 'utf8' })
if (typeof data == 'string')
try {
return JSON.parse(data)
} catch (e) {
error(e instanceof Error ? e.stack || e.message : e + '')
}
}
}
async function request(url: string) {
const headers = token
? {
Authorization: `Bearer ${token}`
}
: {}
return (
await got({
url,
method: 'GET',
headers,
responseType: 'json'
})
).body
}
async function processDirectory(
dir: string,
commits: LocalCommit[] | PartialCommitResponse[]
) {
try {
const packageObj = await (
packageFileURL
? readJson(packageFileURL)
: readJson(join(dir, packageFileName))
).catch(() => {
Promise.reject(
new NeutralExitError(`Package file not found: ${packageFileName}`)
)
})
if (!isPackageObj(packageObj)) throw new Error("Can't find version field")
if (commits.length >= 20)
warning(
'This workflow run topped the commit limit set by GitHub webhooks: that means that commits could not appear and that the run could not find the version change.'
)
if (commits.length <= 0) {
info('There are no commits to look at.')
return
}
await checkCommits(commits, packageObj.version)
} catch (e) {
setFailed(`${e}`)
}
}
async function checkCommits(
commits: LocalCommit[] | PartialCommitResponse[],
version: string
) {
try {
startGroup(
`Searching in ${commits.length} commit${
commits.length == 1 ? '' : 's'
}...`
)
info(`Package file name: "${packageFileName}"`)
info(
`Package file URL: ${
packageFileURL ? `"${packageFileURL}"` : 'undefined'
}`
)
info(
`Version assumptions: ${
assumeSameVersion ? `"${assumeSameVersion}"` : 'undefined'
}`
)
for (const commit of commits) {
const { message, sha } = getBasicInfo(commit)
const match: string[] = message.match(semverReGlobal) || []
if (match.includes(version)) {
if (await checkDiff(sha, version)) {
endGroup()
info(
`Found match for version ${version}: ${sha.substring(
0,
7
)} ${message}`
)
return true
}
}
}
endGroup()
if (getBooleanInput('diff-search')) {
info(
'No standard npm version commit found, switching to diff search (this could take more time...)'
)
if (!isLocalCommitArray(commits)) {
commits = commits.sort(
(a, b) =>
new Date(b.commit.committer.date).getTime() -
new Date(a.commit.committer.date).getTime()
)
}
startGroup(
`Checking the diffs of ${commits.length} commit${
commits.length == 1 ? '' : 's'
}...`
)
for (const commit of commits) {
const { message, sha } = getBasicInfo(commit)
if (await checkDiff(sha, version)) {
endGroup()
info(
`Found match for version ${version}: ${sha.substring(
0,
7
)} - ${message}`
)
return true
}
}
}
endGroup()
info('No matching commit found.')
output('changed', false)
return false
} catch (e) {
setFailed(`${e}`)
}
}
function getBasicInfo(commit: LocalCommit | PartialCommitResponse) {
let message: string, sha: string
if (isLocalCommit(commit)) {
message = commit.message
sha = commit.id
} else {
message = commit.commit.message
sha = commit.sha
}
return {
message,
sha
}
}
async function checkDiff(sha: string, version: string) {
try {
const commit = await getCommit(sha)
const pkg = commit.files.find((f) => f.filename == packageFileName)
if (!pkg) {
info(`- ${sha.substr(0, 7)}: no changes to the package file`)
return false
}
const versionLines: {
added?: string
deleted?: string
} = {}
const rawLines = pkg.patch
.split('\n')
.filter(
(line) => line.includes('"version":') && ['+', '-'].includes(line[0])
)
if (rawLines.length > 2) {
info(`- ${sha.substr(0, 7)}: too many version lines`)
return false
}
for (const line of rawLines)
versionLines[line.startsWith('+') ? 'added' : 'deleted'] = line
if (!versionLines.added) {
info(`- ${sha.substr(0, 7)}: no "+ version" line`)
return false
}
const versions = {
added:
assumeSameVersion == 'new'
? version
: parseVersionLine(versionLines.added),
deleted:
assumeSameVersion == 'old'
? version
: !!versionLines.deleted && parseVersionLine(versionLines.deleted)
}
if (versions.added != version && !assumeSameVersion) {
info(
`- ${sha.substr(
0,
7
)}: added version doesn't match current one (added: "${
versions.added
}"; current: "${version}")`
)
return false
}
output('changed', true)
output('version', version)
if (versions.deleted)
output('type', semverDiff(versions.deleted, versions.added as string))
output('commit', commit.sha)
info(`- ${sha.substr(0, 7)}: match found, more info below`)
return true
} catch (e) {
error(`An error occurred in checkDiff:\n${e}`)
throw new ExitError(1)
}
}
function trimTrailingSlashFromUrl(rawUrl) {
const url = rawUrl.trim()
return url.endsWith('/') ? url.slice(0, -1) : url
}
async function getCommit(sha: string): Promise<CommitResponse> {
const githubApiUrl = trimTrailingSlashFromUrl(getInput('github-api-url'))
const url = `${githubApiUrl}/repos/${process.env.GITHUB_REPOSITORY}/commits/${sha}`
const res = await request(url)
if (typeof res != 'object' || !res)
throw new Error('Response data must be an object.')
return res as CommitResponse
}
function parseVersionLine(str: string) {
return (str.split('"') || []).map((s) => matchVersion(s)).find((e) => !!e)
}
function matchVersion(str: string): string {
return (str.match(semverReGlobal) || ([] as string[]))[0]
}
function output(name: outputKey, value?: string | boolean) {
outputs[name] = value
return setOutput(name, `${value}`)
}
function logOutputs() {
startGroup('Outputs:')
for (const key in outputs) {
info(`${key}: ${outputs[key]}`)
}
endGroup()
}
// #endregion
// #region Error classes
class ExitError extends Error {
code?: number
constructor(code: number | null) {
super(`Command failed with code ${code}`)
if (typeof code == 'number') this.code = code
}
}
class NeutralExitError extends Error {}
// #endregion
if (require.main == module) {
info('Searching for version update...')
main()
.then(() => {
if (typeof outputs.changed == 'undefined') {
output('changed', false)
}
logOutputs()
})
.catch((e) => {
if (e instanceof NeutralExitError) process.exitCode = 78
else {
process.exitCode = 1
error(e.message || e)
}
})
}
// #region Types and interfaces
interface CommitResponse extends PartialCommitResponse {
files: {
filename: string
additions: number
deletions: number
changes: number
status: string
raw_url: string
blob_url: string
patch: string
}[]
}
interface LocalCommit {
id: string
message: string
author: {
name: string
email: string
}
url: string
distinct: boolean
}
function isLocalCommit(value): value is LocalCommit {
return typeof value.id == 'string'
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isLocalCommitArray(value: any[]): value is LocalCommit[] {
return isLocalCommit(value[0])
}
interface PackageObj {
version: string
}
function isPackageObj(value): value is PackageObj {
return !!value && !!value.version
}
interface PartialCommitResponse {
url: string
sha: string
node_id: string
html_url: string
comments_url: string
commit: {
url: string
author: {
name: string
email: string
date: string
}
committer: {
name: string
email: string
date: string
}
message: string
tree: {
url: string
sha: string
}
comment_count: number
verification: {
verified: boolean
reason: string
signature: object | null
payload: object | null
}
}
author: {
login: string
id: number
node_id: string
avatar_url: string
gravatar_id: string
url: string
html_url: string
followers_url: string
following_url: string
gists_url: string
starred_url: string
subscriptions_url: string
organizations_url: string
repos_url: string
events_url: string
receive_events_url: string
type: string
site_admin: boolean
}
committer: {
login: string
id: number
node_id: string
avatar_url: string
gravatar_id: string
url: string
html_url: string
followers_url: string
following_url: string
gists_url: string
starred_url: string
subscriptions_url: string
organizations_url: string
repos_url: string
events_url: string
receive_events_url: string
type: string
site_admin: boolean
}
parents: {
url: string
sha: string
}[]
stats: {
additions: number
deletions: number
total: number
}
}
// #endregion