diff --git a/tools/dashboard/functions/github/github-status.ts b/tools/dashboard/functions/github/github-status.ts index d234bed5583..ac02ee39787 100644 --- a/tools/dashboard/functions/github/github-status.ts +++ b/tools/dashboard/functions/github/github-status.ts @@ -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)); }); } diff --git a/tools/dashboard/functions/payload-github-status.ts b/tools/dashboard/functions/payload-github-status.ts index 374edb80f37..fb0537bf949 100644 --- a/tools/dashboard/functions/payload-github-status.ts +++ b/tools/dashboard/functions/payload-github-status.ts @@ -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'}); @@ -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(); +} diff --git a/tools/gulp/tasks/payload.ts b/tools/gulp/tasks/payload.ts index de2089bca04..ccd04b4aeb8 100644 --- a/tools/gulp/tasks/payload.ts +++ b/tools/gulp/tasks/payload.ts @@ -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; + // 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; - // Update the Github status of the current commit by sending a request to the dashboard - // firebase http trigger function. - await updateGithubStatus(currentSha, deltaSize, authToken); + 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 } };