mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
7024dc49f8
* Updates the package dependencies of the Dashboard dependencies. There seem to be errors with the old `node-github` API, but updating to the latest version (`@octokit/rest`) seems to work locally.
59 lines
2.2 KiB
TypeScript
59 lines
2.2 KiB
TypeScript
import {https} from 'firebase-functions';
|
|
import {verifyToken} from './jwt/verify-token';
|
|
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 packageName = request.header('package-name');
|
|
const packageSize = parseFloat(request.header('package-full-size') || '');
|
|
const packageDiff = parseFloat(request.header('package-size-diff') || '');
|
|
|
|
if (!authToken || !verifyToken(authToken)) {
|
|
return response.status(403).json({message: 'Auth token is not valid'});
|
|
}
|
|
|
|
if (!commitSha) {
|
|
return response.status(404).json({message: 'No commit 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);
|
|
const packageSizeFormatted = packageSize.toFixed(2);
|
|
|
|
try {
|
|
await setGithubStatus(commitSha, {
|
|
result: true,
|
|
name: `${capitalizeFirstLetter(packageName)} Payload Size`,
|
|
description: `${packageSizeFormatted}kb / ${diffFormatted}kb ${diffMessage} (ES2015 bundle)`
|
|
});
|
|
|
|
response.json({message: 'Payload Github status successfully set.'});
|
|
} catch (e) {
|
|
response.json({message: 'An error occurred while setting the GitHub status.'});
|
|
console.error('An error occurred while setting Github status.', e);
|
|
}
|
|
});
|
|
|
|
/** Capitalizes the first letter of a string. */
|
|
function capitalizeFirstLetter(word: string) {
|
|
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
|
|
}
|