-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
103 lines (86 loc) · 3.13 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
const path = require('path')
const fs = require('fs')
const github = require('@actions/github')
const core = require('@actions/core')
const { getStatsDiff } = require('webpack-stats-diff')
const fileSize = require('filesize')
const markdownTable = require('markdown-table')
const doesPathExists = path => {
if (!fs.existsSync(path)) {
throw new Error(`${path} does not exist!`)
}
}
const validatePercentage = percentage => {
if (percentage != 0 && !percentage) {
return percentage
}
const value = Number(percentage)
if (isNaN(value) || !isFinite(value)) {
throw new Error(`unable to parse announcement threshold percentage as a number`)
}
return value
}
async function run() {
try {
const statsPaths = {
base: core.getInput('base_stats_path'),
head: core.getInput('head_stats_path')
}
const paths = {
base: path.resolve(process.cwd(), statsPaths.base),
head: path.resolve(process.cwd(), statsPaths.head)
}
doesPathExists(paths.base)
doesPathExists(paths.head)
const assets = {
base: require(paths.base).assets,
head: require(paths.head).assets
}
const diff = getStatsDiff(assets.base, assets.head, {})
const announcementThresholds = {
increase: validatePercentage(core.getInput('announcement_percentage_threshold_increase')),
decrease: validatePercentage(core.getInput('announcement_percentage_threshold_decrease')),
}
if (announcementThresholds.increase != null && diff.total.diffPercentage >= 0 && (diff.total.diffPercentage < announcementThresholds.increase || (diff.total.diffPercentage == 0 && announcementThresholds.increase == 0))) {
console.log(`skipping adding comment because diff percentage ${diff.total.diffPercentage} is under the increase threshold of ${announcementThresholds.increase}`)
return
}
if (announcementThresholds.decrease != null && diff.total.diffPercentage <= 0 && (diff.total.diffPercentage > announcementThresholds.decrease || (diff.total.diffPercentage == 0 && announcementThresholds.decrease == 0))) {
console.log(`skipping adding comment because diff percentage ${diff.total.diffPercentage} is under the decrease threshold of ${announcementThresholds.decrease}`)
return
}
const summaryTable = markdownTable([
[
'Old size',
'New size',
'Diff'
],
[
fileSize(diff.total.oldSize),
fileSize(diff.total.newSize),
`${fileSize(diff.total.diff)} (${diff.total.diffPercentage.toFixed(2)}%)`
]
])
/**
* Publish a comment in the PR with the diff result.
*/
const octokit = github.getOctokit(core.getInput('token'))
const pullRequestId = github.context.issue.number
if (!pullRequestId) {
throw new Error('Cannot find the PR id.')
}
const commentTitle = core.getInput('comment_title') || 'Bundle difference'
await octokit.issues.createComment({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: pullRequestId,
body: `## ${commentTitle}
${summaryTable}
`
})
}
catch (error) {
core.setFailed(error.message)
}
}
run()