Skip to content

Commit daec577

Browse files
authored
Add change highlight threshold option (#1)
1 parent ef0a56c commit daec577

File tree

5 files changed

+93
-33
lines changed

5 files changed

+93
-33
lines changed

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ inputs:
3535
required: false
3636
default: 0.0125
3737
description: "The percentage threshold for size changes before posting a comment"
38+
change_highlight_threshold:
39+
required: false
40+
default: 0
41+
description: "The percentage threshold for changes to be highlighted with an emoji"
3842
runs:
3943
using: "node16"
4044
main: "dist/index.js"

dist/index.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21686,38 +21686,38 @@ class SizeLimit {
2168621686
}
2168721687
return `${Math.ceil(seconds * 1000)} ms`;
2168821688
}
21689-
formatChange(base = 0, current = 0) {
21689+
formatChange(base = 0, current = 0, changeHighlightThreshold = 0) {
2169021690
if (base === 0) {
21691-
return "added";
21691+
return "added 🆕";
2169221692
}
2169321693
if (current === 0) {
21694-
return "removed";
21694+
return "removed 🚮";
2169521695
}
2169621696
const value = ((current - base) / base) * 100;
2169721697
const formatted = (Math.sign(value) * Math.ceil(Math.abs(value) * 100)) / 100;
2169821698
if (value > 0) {
21699-
return `+${formatted}% 🔺`;
21699+
return value - changeHighlightThreshold > 0 ? `+${formatted}% 🔺` : `+${formatted}%`;
2170021700
}
2170121701
if (value === 0) {
2170221702
return `${formatted}%`;
2170321703
}
21704-
return `${formatted}% 🔽`;
21704+
return value + changeHighlightThreshold < 0 ? `${formatted}% 🔽` : `${formatted}%`;
2170521705
}
2170621706
formatLine(value, change) {
2170721707
return `${value} (${change})`;
2170821708
}
21709-
formatSizeResult(name, base, current) {
21709+
formatSizeResult(name, base, current, changeHighlightThreshold) {
2171021710
return [
2171121711
name,
21712-
this.formatLine(this.formatBytes(current.size), this.formatChange(base.size, current.size)),
21712+
this.formatLine(this.formatBytes(current.size), this.formatChange(base.size, current.size, changeHighlightThreshold)),
2171321713
];
2171421714
}
21715-
formatTimeResult(name, base, current) {
21715+
formatTimeResult(name, base, current, changeHighlightThreshold) {
2171621716
return [
2171721717
name,
21718-
this.formatLine(this.formatBytes(current.size), this.formatChange(base.size, current.size)),
21719-
this.formatLine(this.formatTime(current.loading), this.formatChange(base.loading, current.loading)),
21720-
this.formatLine(this.formatTime(current.running), this.formatChange(base.running, current.running)),
21718+
this.formatLine(this.formatBytes(current.size), this.formatChange(base.size, current.size, changeHighlightThreshold)),
21719+
this.formatLine(this.formatTime(current.loading), this.formatChange(base.loading, current.loading, changeHighlightThreshold)),
21720+
this.formatLine(this.formatTime(current.running), this.formatChange(base.running, current.running, changeHighlightThreshold)),
2172121721
this.formatTime(current.total),
2172221722
];
2172321723
}
@@ -21757,7 +21757,7 @@ class SizeLimit {
2175721757
threshold);
2175821758
});
2175921759
}
21760-
formatResults(base, current) {
21760+
formatResults(base, current, changeHighlightThreshold = 0) {
2176121761
const names = [
2176221762
...new Set([...(base ? Object.keys(base) : []), ...Object.keys(current)]),
2176321763
];
@@ -21769,9 +21769,9 @@ class SizeLimit {
2176921769
const baseResult = (base === null || base === void 0 ? void 0 : base[name]) || EmptyResult;
2177021770
const currentResult = current[name] || EmptyResult;
2177121771
if (isSize) {
21772-
return this.formatSizeResult(name, baseResult, currentResult);
21772+
return this.formatSizeResult(name, baseResult, currentResult, changeHighlightThreshold);
2177321773
}
21774-
return this.formatTimeResult(name, baseResult, currentResult);
21774+
return this.formatTimeResult(name, baseResult, currentResult, changeHighlightThreshold);
2177521775
});
2177621776
return [header, ...fields];
2177721777
}
@@ -21941,6 +21941,7 @@ function run() {
2194121941
const windowsVerbatimArguments = getInput("windows_verbatim_arguments") === "true" ? true : false;
2194221942
const githubToken = getInput("github_token");
2194321943
const threshold = getInput("threshold");
21944+
const changeHighlightThreshold = getInput("change_highlight_threshold");
2194421945
const octokit = (0, github_1.getOctokit)(githubToken);
2194521946
const term = new Term_1.default();
2194621947
const limit = new SizeLimit_1.default();
@@ -21997,9 +21998,10 @@ function run() {
2199721998
limit.hasSizeChanges(base, current, thresholdNumber) ||
2199821999
sizeLimitComment;
2199922000
if (shouldComment) {
22001+
const changeHighlightThresholdNumber = Number(changeHighlightThreshold);
2200022002
const body = [
2200122003
SIZE_LIMIT_HEADING,
22002-
(0, markdown_table_1.markdownTable)(limit.formatResults(base, current)),
22004+
(0, markdown_table_1.markdownTable)(limit.formatResults(base, current, changeHighlightThresholdNumber)),
2200322005
].join("\r\n");
2200422006
try {
2200522007
if (!sizeLimitComment) {

src/SizeLimit.spec.ts

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ describe("SizeLimit", () => {
118118
expect(limit.formatResults(base, current)).toEqual([
119119
SizeLimit.SIZE_RESULTS_HEADER,
120120
["dist/index.js", "98.53 KB (-9.02% 🔽)"],
121-
["dist/new.js", "98.53 KB (added)"],
121+
["dist/new.js", "98.53 KB (added 🆕)"],
122122
]);
123123
});
124124

@@ -139,8 +139,56 @@ describe("SizeLimit", () => {
139139

140140
expect(limit.formatResults(base, current)).toEqual([
141141
SizeLimit.SIZE_RESULTS_HEADER,
142-
["dist/index.js", "0 B (removed)"],
143-
["dist/new.js", "98.53 KB (added)"],
142+
["dist/index.js", "0 B (removed 🚮)"],
143+
["dist/new.js", "98.53 KB (added 🆕)"],
144+
]);
145+
});
146+
147+
test("should format size-limit with change emojis only if change highlight threshold surpassed", () => {
148+
const limit = new SizeLimit();
149+
const base = {
150+
"dist/around-same-size.js": {
151+
name: "dist/around-same-size.js",
152+
size: 110894,
153+
},
154+
"dist/much-bigger.js": {
155+
name: "dist/much-bigger.js",
156+
size: 133742,
157+
},
158+
"dist/slightly-smaller.js": {
159+
name: "dist/slightly-smaller.js",
160+
size: 1402,
161+
},
162+
};
163+
const current = {
164+
"dist/around-same-size.js": {
165+
name: "dist/around-same-size.js",
166+
size: 110642,
167+
},
168+
"dist/much-bigger.js": {
169+
name: "dist/much-bigger.js",
170+
size: 153293,
171+
},
172+
"dist/slightly-smaller.js": {
173+
name: "dist/slightly-smaller.js",
174+
size: 1374,
175+
},
176+
};
177+
178+
expect(limit.formatResults(base, current, 2)).toEqual([
179+
SizeLimit.SIZE_RESULTS_HEADER,
180+
[
181+
"dist/around-same-size.js",
182+
"108.05 KB (-0.23%)",
183+
],
184+
[
185+
"dist/much-bigger.js",
186+
"149.7 KB (+14.62% 🔺)",
187+
],
188+
[
189+
"dist/slightly-smaller.js",
190+
"1.34 KB (-2%)",
191+
],
144192
]);
145193
});
146194

src/SizeLimit.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,28 @@ class SizeLimit {
4040
return `${Math.ceil(seconds * 1000)} ms`;
4141
}
4242

43-
private formatChange(base: number = 0, current: number = 0): string {
43+
private formatChange(base: number = 0, current: number = 0, changeHighlightThreshold = 0): string {
4444
if (base === 0) {
45-
return "added";
45+
return "added 🆕";
4646
}
4747

4848
if (current === 0) {
49-
return "removed";
49+
return "removed 🚮";
5050
}
5151

5252
const value = ((current - base) / base) * 100;
5353
const formatted =
5454
(Math.sign(value) * Math.ceil(Math.abs(value) * 100)) / 100;
5555

5656
if (value > 0) {
57-
return `+${formatted}% 🔺`;
57+
return value - changeHighlightThreshold > 0 ? `+${formatted}% 🔺` : `+${formatted}%`;
5858
}
5959

6060
if (value === 0) {
6161
return `${formatted}%`;
6262
}
6363

64-
return `${formatted}% 🔽`;
64+
return value + changeHighlightThreshold < 0 ? `${formatted}% 🔽` : `${formatted}%`;
6565
}
6666

6767
private formatLine(value: string, change: string) {
@@ -71,35 +71,37 @@ class SizeLimit {
7171
private formatSizeResult(
7272
name: string,
7373
base: IResult,
74-
current: IResult
74+
current: IResult,
75+
changeHighlightThreshold: number,
7576
): Array<string> {
7677
return [
7778
name,
7879
this.formatLine(
7980
this.formatBytes(current.size),
80-
this.formatChange(base.size, current.size)
81+
this.formatChange(base.size, current.size, changeHighlightThreshold)
8182
),
8283
];
8384
}
8485

8586
private formatTimeResult(
8687
name: string,
8788
base: IResult,
88-
current: IResult
89+
current: IResult,
90+
changeHighlightThreshold: number,
8991
): Array<string> {
9092
return [
9193
name,
9294
this.formatLine(
9395
this.formatBytes(current.size),
94-
this.formatChange(base.size, current.size)
96+
this.formatChange(base.size, current.size, changeHighlightThreshold)
9597
),
9698
this.formatLine(
9799
this.formatTime(current.loading),
98-
this.formatChange(base.loading, current.loading)
100+
this.formatChange(base.loading, current.loading, changeHighlightThreshold)
99101
),
100102
this.formatLine(
101103
this.formatTime(current.running),
102-
this.formatChange(base.running, current.running)
104+
this.formatChange(base.running, current.running, changeHighlightThreshold)
103105
),
104106
this.formatTime(current.total),
105107
];
@@ -171,7 +173,8 @@ class SizeLimit {
171173

172174
formatResults(
173175
base: { [name: string]: IResult },
174-
current: { [name: string]: IResult }
176+
current: { [name: string]: IResult },
177+
changeHighlightThreshold: number = 0,
175178
): Array<Array<string>> {
176179
const names = [
177180
...new Set([...(base ? Object.keys(base) : []), ...Object.keys(current)]),
@@ -187,9 +190,9 @@ class SizeLimit {
187190
const currentResult = current[name] || EmptyResult;
188191

189192
if (isSize) {
190-
return this.formatSizeResult(name, baseResult, currentResult);
193+
return this.formatSizeResult(name, baseResult, currentResult, changeHighlightThreshold);
191194
}
192-
return this.formatTimeResult(name, baseResult, currentResult);
195+
return this.formatTimeResult(name, baseResult, currentResult, changeHighlightThreshold);
193196
});
194197

195198
return [header, ...fields];

src/main.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ async function run() {
6363
getInput("windows_verbatim_arguments") === "true" ? true : false;
6464
const githubToken = getInput("github_token");
6565
const threshold = getInput("threshold");
66+
const changeHighlightThreshold = getInput("change_highlight_threshold");
6667

6768
const octokit = getOctokit(githubToken);
6869
const term = new Term();
@@ -153,9 +154,11 @@ async function run() {
153154
sizeLimitComment;
154155

155156
if (shouldComment) {
157+
const changeHighlightThresholdNumber = Number(changeHighlightThreshold);
158+
156159
const body = [
157160
SIZE_LIMIT_HEADING,
158-
markdownTable(limit.formatResults(base, current)),
161+
markdownTable(limit.formatResults(base, current, changeHighlightThresholdNumber)),
159162
].join("\r\n");
160163

161164
try {

0 commit comments

Comments
 (0)