Skip to content

build: separate payload diffs for packages #5106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions tools/dashboard/functions/github/github-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,25 @@ export type GithubStatusData = {
export function setGithubStatus(commitSHA: string, data: GithubStatusData) {
const state = data.result ? 'success' : 'failure';

const requestData = JSON.stringify({
const requestData = {
state: state,
target_url: data.url,
context: data.name,
description: data.description
});
};

const headers = {
'Authorization': `token ${repoToken}`,
'User-Agent': `${name}/${version}`,
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(requestData)
'User-Agent': `${name}/${version}`
};

return new Promise((resolve) => {
return new Promise((resolve, reject) => {
request({
url: `https://api.github.com/repos/angular/material2/statuses/${commitSHA}`,
method: 'POST',
form: requestData,
headers: headers
}, (error: any, response: any) => resolve(response.statusCode));
body: requestData,
headers: headers,
json: true
}, (error: any, response: any) => error ? reject(error) : resolve(response.statusCode));
});
}
33 changes: 28 additions & 5 deletions tools/dashboard/functions/payload-github-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {setGithubStatus} from './github/github-status';
export const payloadGithubStatus = https.onRequest(async (request, response) => {
const authToken = request.header('auth-token');
const commitSha = request.header('commit-sha');
const payloadDiff = parseFloat(request.header('commit-payload-diff'));
const packageName = request.header('package-name');
const packageSize = parseFloat(request.header('package-full-size'));
const packageDiff = parseFloat(request.header('package-size-diff'));

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

if (isNaN(payloadDiff)) {
return response.status(400).json({message: 'No valid payload diff has been specified.'});
if (isNaN(packageDiff)) {
return response.status(400).json({message: 'No valid package difference has been specified.'});
}

if (isNaN(packageSize)) {
return response.status(400).json({message: 'No full size of the package has been specified.'});
}

if (!packageName) {
return response.status(400).json({message: 'No package name has been specified.'});
}

if (packageDiff === 0) {
return response.status(400).json({message: `The difference equals zero. Status won't be set.`});
}

// Better message about the diff that shows whether the payload increased or decreased.
const diffMessage = packageDiff < 0 ? 'decrease' : 'increase';
const diffFormatted = Math.abs(packageDiff).toFixed(2);

await setGithubStatus(commitSha, {
result: true,
name: 'Library Payload',
description: `${payloadDiff >= 0 ? `+` : ''}${payloadDiff.toFixed(2)}KB`
name: `${capitalizeFirstLetter(packageName)} Payload Size`,
description: `${packageSize}kb / ${diffFormatted}kb ${diffMessage} (ES2015 bundle)`
});

response.json({message: 'Payload Github status successfully set.'});
});

/** Capitalizes the first letter of a string. */
function capitalizeFirstLetter(word: string) {
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
}
27 changes: 17 additions & 10 deletions tools/gulp/tasks/payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,28 +86,35 @@ async function calculatePayloadDiff(database: firebaseAdmin.database.Database, c
return;
}

// Calculate library sizes by combining the CDK and Material FESM 2015 bundles.
const previousSize = previousPayload.cdk_fesm_2015 + previousPayload.material_fesm_2015;
const currentSize = currentPayload.cdk_fesm_2015 + currentPayload.material_fesm_2015;
const deltaSize = currentSize - previousSize;

// Update the Github status of the current commit by sending a request to the dashboard
// firebase http trigger function.
await updateGithubStatus(currentSha, deltaSize, authToken);
// Calculate the payload diffs by subtracting the previous size of the FESM ES2015 bundles.
const cdkFullSize = currentPayload.cdk_fesm_2015;
const cdkDiff = cdkFullSize - previousPayload.cdk_fesm_2015;

const materialFullSize = currentPayload.material_fesm_2015;
const materialDiff = materialFullSize - previousPayload.material_fesm_2015;

// Set the Github statuses for the packages by sending a HTTP request to the dashboard functions.
await Promise.all([
updateGithubStatus(currentSha, 'material', materialDiff, materialFullSize, authToken),
updateGithubStatus(currentSha, 'cdk', cdkDiff, cdkFullSize, authToken)
]);
}

/**
* Updates the Github status of a given commit by sending a request to a Firebase function of
* the dashboard. The function has access to the Github repository and can set status for PRs too.
*/
async function updateGithubStatus(commitSha: string, payloadDiff: number, authToken: string) {
async function updateGithubStatus(commitSha: string, packageName: string, packageDiff: number,
packageFullSize: number, authToken: string) {
const options = {
url: 'https://us-central1-material2-board.cloudfunctions.net/payloadGithubStatus',
headers: {
'User-Agent': 'Material2/PayloadTask',
'auth-token': authToken,
'commit-sha': commitSha,
'commit-payload-diff': payloadDiff
'package-name': packageName,
'package-full-size': packageFullSize,
'package-size-diff': packageDiff
}
};

Expand Down