Skip to content

Commit 9fbfc7b

Browse files
committed
feat: add min delta feature
1 parent 6249836 commit 9fbfc7b

File tree

4 files changed

+99
-58
lines changed

4 files changed

+99
-58
lines changed

action.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ inputs:
3131
package_manager:
3232
required: false
3333
description: "The package manager used to run the build and install commands. If not provided, the manager will be auto detected. Example values: `yarn`, `npm`, `pnpm`, `bun`."
34-
ignore_unchanged:
34+
min_delta:
3535
required: false
36-
description: "Whether to ignore unchanged files"
36+
description: "If specified, only changes exceeding the delta will be reported"
3737
runs:
3838
using: "node20"
3939
main: "dist/index.js"

src/SizeLimit.spec.ts

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ describe("SizeLimit", () => {
99
passed: true,
1010
size: "110894",
1111
running: "0.10210999999999999",
12-
loading: "2.1658984375",
13-
},
12+
loading: "2.1658984375"
13+
}
1414
]);
1515

1616
expect(limit.parseResults(output)).toEqual({
@@ -19,8 +19,8 @@ describe("SizeLimit", () => {
1919
loading: 2.1658984375,
2020
running: 0.10210999999999999,
2121
size: 110894,
22-
total: 2.2680084375000003,
23-
},
22+
total: 2.2680084375000003
23+
}
2424
});
2525
});
2626

@@ -30,15 +30,15 @@ describe("SizeLimit", () => {
3030
{
3131
name: "dist/index.js",
3232
passed: true,
33-
size: "110894",
34-
},
33+
size: "110894"
34+
}
3535
]);
3636

3737
expect(limit.parseResults(output)).toEqual({
3838
"dist/index.js": {
3939
name: "dist/index.js",
40-
size: 110894,
41-
},
40+
size: 110894
41+
}
4242
});
4343
});
4444

@@ -50,28 +50,28 @@ describe("SizeLimit", () => {
5050
size: 110894,
5151
running: 0.10210999999999999,
5252
loading: 2.1658984375,
53-
total: 2.2680084375000003,
54-
},
53+
total: 2.2680084375000003
54+
}
5555
};
5656
const current = {
5757
"dist/index.js": {
5858
name: "dist/index.js",
5959
size: 100894,
6060
running: 0.20210999999999999,
6161
loading: 2.5658984375,
62-
total: 2.7680084375000003,
63-
},
62+
total: 2.7680084375000003
63+
}
6464
};
6565

66-
expect(limit.formatResults(base, current)).toEqual([
66+
expect(limit.formatResults(base, current, { minDelta: 0 })).toEqual([
6767
SizeLimit.TIME_RESULTS_HEADER,
6868
[
6969
"dist/index.js",
7070
"98.53 KB (-9.02% 🔽)",
7171
"2.6 s (+18.47% 🔺)",
7272
"203 ms (+97.94% 🔺)",
73-
"2.8 s",
74-
],
73+
"2.8 s"
74+
]
7575
]);
7676
});
7777

@@ -80,19 +80,19 @@ describe("SizeLimit", () => {
8080
const base = {
8181
"dist/index.js": {
8282
name: "dist/index.js",
83-
size: 110894,
84-
},
83+
size: 110894
84+
}
8585
};
8686
const current = {
8787
"dist/index.js": {
8888
name: "dist/index.js",
89-
size: 100894,
90-
},
89+
size: 100894
90+
}
9191
};
9292

93-
expect(limit.formatResults(base, current)).toEqual([
93+
expect(limit.formatResults(base, current, { minDelta: 0 })).toEqual([
9494
SizeLimit.SIZE_RESULTS_HEADER,
95-
["dist/index.js", "98.53 KB (-9.02% 🔽)"],
95+
["dist/index.js", "98.53 KB (-9.02% 🔽)"]
9696
]);
9797
});
9898

@@ -101,24 +101,24 @@ describe("SizeLimit", () => {
101101
const base = {
102102
"dist/index.js": {
103103
name: "dist/index.js",
104-
size: 110894,
105-
},
104+
size: 110894
105+
}
106106
};
107107
const current = {
108108
"dist/index.js": {
109109
name: "dist/index.js",
110-
size: 100894,
110+
size: 100894
111111
},
112112
"dist/new.js": {
113113
name: "dist/new.js",
114-
size: 100894,
115-
},
114+
size: 100894
115+
}
116116
};
117117

118-
expect(limit.formatResults(base, current)).toEqual([
118+
expect(limit.formatResults(base, current, { minDelta: 0 })).toEqual([
119119
SizeLimit.SIZE_RESULTS_HEADER,
120120
["dist/index.js", "98.53 KB (-9.02% 🔽)"],
121-
["dist/new.js", "98.53 KB (+100% 🔺)"],
121+
["dist/new.js", "98.53 KB (+100% 🔺)"]
122122
]);
123123
});
124124

@@ -127,20 +127,49 @@ describe("SizeLimit", () => {
127127
const base = {
128128
"dist/index.js": {
129129
name: "dist/index.js",
130-
size: 110894,
131-
},
130+
size: 110894
131+
}
132132
};
133133
const current = {
134134
"dist/new.js": {
135135
name: "dist/new.js",
136-
size: 100894,
137-
},
136+
size: 100894
137+
}
138138
};
139139

140-
expect(limit.formatResults(base, current)).toEqual([
140+
expect(limit.formatResults(base, current, { minDelta: 0 })).toEqual([
141141
SizeLimit.SIZE_RESULTS_HEADER,
142142
["dist/index.js", "0 B (-100% 🔽)"],
143-
["dist/new.js", "98.53 KB (+100% 🔺)"],
143+
["dist/new.js", "98.53 KB (+100% 🔺)"]
144+
]);
145+
});
146+
147+
test("should ignore changes below minDelta", () => {
148+
const limit = new SizeLimit();
149+
const base = {
150+
"dist/index.js": {
151+
name: "dist/index.js",
152+
size: 110894
153+
},
154+
"dist/unchanged.js": {
155+
name: "dist/index.js",
156+
size: 110894
157+
}
158+
};
159+
const current = {
160+
"dist/index.js": {
161+
name: "dist/index.js",
162+
size: 150894
163+
},
164+
"dist/unchanged.js": {
165+
name: "dist/index.js",
166+
size: 110894
167+
}
168+
};
169+
170+
expect(limit.formatResults(base, current, { minDelta: 5 })).toEqual([
171+
SizeLimit.SIZE_RESULTS_HEADER,
172+
["dist/index.js", "147.36 KB (+36.08% 🔺)"]
144173
]);
145174
});
146175
});

src/SizeLimit.ts

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

43+
private getDeltaBps(current: number, base: number): number {
44+
return ((current - base) / base) * 100;
45+
}
46+
4347
private formatChange(base: number = 0, current: number = 0): string {
4448
if (base === 0) {
4549
return "+100% 🔺";
4650
}
4751

48-
const value = ((current - base) / base) * 100;
52+
const value = this.getDeltaBps(current, base);
4953
const formatted =
5054
(Math.sign(value) * Math.ceil(Math.abs(value) * 100)) / 100;
5155

@@ -134,7 +138,8 @@ class SizeLimit {
134138

135139
formatResults(
136140
base: { [name: string]: IResult },
137-
current: { [name: string]: IResult }
141+
current: { [name: string]: IResult },
142+
options: { minDelta: number }
138143
): Array<Array<string>> {
139144
const names = [...new Set([...Object.keys(base), ...Object.keys(current)])];
140145
const isSize = names.some(
@@ -143,15 +148,26 @@ class SizeLimit {
143148
const header = isSize
144149
? SizeLimit.SIZE_RESULTS_HEADER
145150
: SizeLimit.TIME_RESULTS_HEADER;
146-
const fields = names.map((name: string) => {
147-
const baseResult = base[name] || EmptyResult;
148-
const currentResult = current[name] || EmptyResult;
149-
150-
if (isSize) {
151-
return this.formatSizeResult(name, baseResult, currentResult);
152-
}
153-
return this.formatTimeResult(name, baseResult, currentResult);
154-
});
151+
const fields = names
152+
.filter(name => {
153+
if (isSize)
154+
return (
155+
!base[name] ||
156+
!current[name] ||
157+
Math.abs(this.getDeltaBps(base[name].size, current[name].size)) >
158+
options.minDelta
159+
);
160+
return true;
161+
})
162+
.map((name: string) => {
163+
const baseResult = base[name] || EmptyResult;
164+
const currentResult = current[name] || EmptyResult;
165+
166+
if (isSize) {
167+
return this.formatSizeResult(name, baseResult, currentResult);
168+
}
169+
return this.formatTimeResult(name, baseResult, currentResult);
170+
});
155171

156172
return [header, ...fields];
157173
}

src/main.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ async function run() {
4646
const script = getInput("script");
4747
const packageManager = getInput("package_manager");
4848
const directory = getInput("directory") || process.cwd();
49-
const ignoreUnchanged = getInput("ignore_unchanged");
49+
const minDelta = getInput("min_delta");
5050
const windowsVerbatimArguments =
5151
getInput("windows_verbatim_arguments") === "true" ? true : false;
5252
const octokit = new GitHub(token);
@@ -87,18 +87,13 @@ async function run() {
8787
throw error;
8888
}
8989

90-
if (ignoreUnchanged) {
91-
Object.keys(base).forEach(key => {
92-
if (base[key].size === current[key].size) {
93-
delete base[key];
94-
delete current[key];
95-
}
96-
});
97-
}
98-
9990
const body = [
10091
SIZE_LIMIT_HEADING,
101-
table(limit.formatResults(base, current))
92+
table(
93+
limit.formatResults(base, current, {
94+
minDelta: minDelta ? Number(minDelta) : 0
95+
})
96+
)
10297
].join("\r\n");
10398

10499
const sizeLimitComment = await fetchPreviousComment(octokit, repo, pr);
@@ -126,6 +121,7 @@ async function run() {
126121
body
127122
});
128123
} catch (error) {
124+
console.log(error);
129125
console.log(
130126
"Error updating comment. This can happen for PR's originating from a fork without write permissions."
131127
);

0 commit comments

Comments
 (0)