Skip to content

Commit 46edae6

Browse files
devversionkara
authored andcommitted
build: separate payload diffs for packages (#5106)
1 parent 90e6d3c commit 46edae6

File tree

3 files changed

+53
-24
lines changed

3 files changed

+53
-24
lines changed

tools/dashboard/functions/github/github-status.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,25 @@ export type GithubStatusData = {
1818
export function setGithubStatus(commitSHA: string, data: GithubStatusData) {
1919
const state = data.result ? 'success' : 'failure';
2020

21-
const requestData = JSON.stringify({
21+
const requestData = {
2222
state: state,
2323
target_url: data.url,
2424
context: data.name,
2525
description: data.description
26-
});
26+
};
2727

2828
const headers = {
2929
'Authorization': `token ${repoToken}`,
30-
'User-Agent': `${name}/${version}`,
31-
'Content-Type': 'application/json',
32-
'Content-Length': Buffer.byteLength(requestData)
30+
'User-Agent': `${name}/${version}`
3331
};
3432

35-
return new Promise((resolve) => {
33+
return new Promise((resolve, reject) => {
3634
request({
3735
url: `https://api.github.com/repos/angular/material2/statuses/${commitSHA}`,
3836
method: 'POST',
39-
form: requestData,
40-
headers: headers
41-
}, (error: any, response: any) => resolve(response.statusCode));
37+
body: requestData,
38+
headers: headers,
39+
json: true
40+
}, (error: any, response: any) => error ? reject(error) : resolve(response.statusCode));
4241
});
4342
}

tools/dashboard/functions/payload-github-status.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import {setGithubStatus} from './github/github-status';
55
export const payloadGithubStatus = https.onRequest(async (request, response) => {
66
const authToken = request.header('auth-token');
77
const commitSha = request.header('commit-sha');
8-
const payloadDiff = parseFloat(request.header('commit-payload-diff'));
8+
const packageName = request.header('package-name');
9+
const packageSize = parseFloat(request.header('package-full-size'));
10+
const packageDiff = parseFloat(request.header('package-size-diff'));
911

1012
if (!verifyToken(authToken)) {
1113
return response.status(403).json({message: 'Auth token is not valid'});
@@ -15,15 +17,36 @@ export const payloadGithubStatus = https.onRequest(async (request, response) =>
1517
return response.status(404).json({message: 'No commit has been specified'});
1618
}
1719

18-
if (isNaN(payloadDiff)) {
19-
return response.status(400).json({message: 'No valid payload diff has been specified.'});
20+
if (isNaN(packageDiff)) {
21+
return response.status(400).json({message: 'No valid package difference has been specified.'});
2022
}
2123

24+
if (isNaN(packageSize)) {
25+
return response.status(400).json({message: 'No full size of the package has been specified.'});
26+
}
27+
28+
if (!packageName) {
29+
return response.status(400).json({message: 'No package name has been specified.'});
30+
}
31+
32+
if (packageDiff === 0) {
33+
return response.status(400).json({message: `The difference equals zero. Status won't be set.`});
34+
}
35+
36+
// Better message about the diff that shows whether the payload increased or decreased.
37+
const diffMessage = packageDiff < 0 ? 'decrease' : 'increase';
38+
const diffFormatted = Math.abs(packageDiff).toFixed(2);
39+
2240
await setGithubStatus(commitSha, {
2341
result: true,
24-
name: 'Library Payload',
25-
description: `${payloadDiff >= 0 ? `+` : ''}${payloadDiff.toFixed(2)}KB`
42+
name: `${capitalizeFirstLetter(packageName)} Payload Size`,
43+
description: `${packageSize}kb / ${diffFormatted}kb ${diffMessage} (ES2015 bundle)`
2644
});
2745

2846
response.json({message: 'Payload Github status successfully set.'});
2947
});
48+
49+
/** Capitalizes the first letter of a string. */
50+
function capitalizeFirstLetter(word: string) {
51+
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
52+
}

tools/gulp/tasks/payload.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,28 +86,35 @@ async function calculatePayloadDiff(database: firebaseAdmin.database.Database, c
8686
return;
8787
}
8888

89-
// Calculate library sizes by combining the CDK and Material FESM 2015 bundles.
90-
const previousSize = previousPayload.cdk_fesm_2015 + previousPayload.material_fesm_2015;
91-
const currentSize = currentPayload.cdk_fesm_2015 + currentPayload.material_fesm_2015;
92-
const deltaSize = currentSize - previousSize;
93-
94-
// Update the Github status of the current commit by sending a request to the dashboard
95-
// firebase http trigger function.
96-
await updateGithubStatus(currentSha, deltaSize, authToken);
89+
// Calculate the payload diffs by subtracting the previous size of the FESM ES2015 bundles.
90+
const cdkFullSize = currentPayload.cdk_fesm_2015;
91+
const cdkDiff = cdkFullSize - previousPayload.cdk_fesm_2015;
92+
93+
const materialFullSize = currentPayload.material_fesm_2015;
94+
const materialDiff = materialFullSize - previousPayload.material_fesm_2015;
95+
96+
// Set the Github statuses for the packages by sending a HTTP request to the dashboard functions.
97+
await Promise.all([
98+
updateGithubStatus(currentSha, 'material', materialDiff, materialFullSize, authToken),
99+
updateGithubStatus(currentSha, 'cdk', cdkDiff, cdkFullSize, authToken)
100+
]);
97101
}
98102

99103
/**
100104
* Updates the Github status of a given commit by sending a request to a Firebase function of
101105
* the dashboard. The function has access to the Github repository and can set status for PRs too.
102106
*/
103-
async function updateGithubStatus(commitSha: string, payloadDiff: number, authToken: string) {
107+
async function updateGithubStatus(commitSha: string, packageName: string, packageDiff: number,
108+
packageFullSize: number, authToken: string) {
104109
const options = {
105110
url: 'https://us-central1-material2-board.cloudfunctions.net/payloadGithubStatus',
106111
headers: {
107112
'User-Agent': 'Material2/PayloadTask',
108113
'auth-token': authToken,
109114
'commit-sha': commitSha,
110-
'commit-payload-diff': payloadDiff
115+
'package-name': packageName,
116+
'package-full-size': packageFullSize,
117+
'package-size-diff': packageDiff
111118
}
112119
};
113120

0 commit comments

Comments
 (0)