This repository was archived by the owner on Jan 28, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 464
/
Copy pathcomment-handler-sizes.ts
99 lines (81 loc) · 2.7 KB
/
comment-handler-sizes.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
#!/usr/bin/env node
import fetch from "node-fetch";
import { calculateHandlerSizes } from "./handler-size-utils";
import * as _ from "lodash";
import { getAuth } from "./get-auth";
/**
* Get sizes that were calculated from existing commit SHA
* @param commitSha
*/
const getCommitSizes = async (
commitSha: string
): Promise<Record<string, any>> => {
const SIZES_URL =
process.env.SIZES_URL ?? "https://d3m7nebxuhlnm8.cloudfront.net";
const url = `${SIZES_URL}/sizes-github-sha-${commitSha}.json`;
console.info("Retrieving url at: " + url);
const response = await fetch(url);
if (response.ok) {
return JSON.parse(await response.text());
} else {
console.warn(
"Unable to get commit sizes due to response status: " + response.status
);
return {};
}
};
const postCommentToPullRequest = async (
prNumber: number,
comment: string
): Promise<void> => {
const existingSearchString = "# Handler Size Report";
// Using locked down API proxy to post comment for both fork and non-fork PRs
await fetch(
"https://l7ycsrlajd.execute-api.us-east-1.amazonaws.com/prod/post-comment",
{
method: "POST",
body: JSON.stringify({
auth: getAuth(),
comment: comment,
prNumber: prNumber,
existingSearchString: existingSearchString
})
}
);
};
const main = async (): Promise<void> => {
const PULL_REQUEST_ID = parseInt(process.env.PULL_REQUEST_ID ?? "0");
const GITHUB_BASE_SHA = process.env.GITHUB_BASE_SHA ?? "";
const GITHUB_NEW_SHA = process.env.GITHUB_NEW_SHA ?? "";
console.info("Get base commit's sizes");
// Get sizes from base branch commit
const baseSizes = await getCommitSizes(GITHUB_BASE_SHA);
console.info("Calculate all uncompressed handler sizes");
// Get sizes from PR branch latest commit
const newSizes: Record<string, any> = calculateHandlerSizes();
let output = "# Handler Size Report\n";
if (_.isEqual(baseSizes, newSizes)) {
output += "> No changes to handler sizes.\n";
} else {
output += "> There are changes to handler sizes. Please review.\n";
}
output += `### Base Handler Sizes (kB) (commit ${GITHUB_BASE_SHA})\n`;
output += "```ts\n";
output += JSON.stringify(baseSizes, null, 4) + "\n";
output += "```\n";
output += `### New Handler Sizes (kB) (commit ${GITHUB_NEW_SHA})\n`;
output += "```ts\n";
output += JSON.stringify(newSizes, null, 4) + "\n";
output += "```\n";
// Post comment to pull request
await postCommentToPullRequest(PULL_REQUEST_ID, output);
};
main()
.then(() => {
console.info("Commented handler sizes successfully.");
process.exit(0);
})
.catch((error) => {
console.error(`Unhandled error: ${error}`);
process.exit(1);
});