ci: ignore bundle size reporter failures (#32490)

Summary:
CircleCI stopped populating `CIRCLE_PULL_REQUEST` without providing an
alternative, so now it's impossible to get the PR number unless it comes
from a forked repo.

## Changelog

[Internal] [Fixed] - ignore bundle size reporter failures

Pull Request resolved: https://github.com/facebook/react-native/pull/32490

Test Plan: CI should ignore bundle size reporter failures.

Reviewed By: fkgozali

Differential Revision: D32008694

Pulled By: lunaleaps

fbshipit-source-id: 68e25ac2fbb23c1d7a55e667c90aec3a61302b8a
This commit is contained in:
Tommy Nguyen 2021-11-03 12:09:13 -07:00 коммит произвёл Facebook GitHub Bot
Родитель 5c045861b9
Коммит 86491749ee
4 изменённых файлов: 171 добавлений и 60 удалений

Просмотреть файл

@ -210,7 +210,7 @@ commands:
- install_github_bot_deps - install_github_bot_deps
- run: - run:
name: Report size of RNTester.app (analysis-bot) name: Report size of RNTester.app (analysis-bot)
command: GITHUB_TOKEN="$PUBLIC_ANALYSISBOT_GITHUB_TOKEN_A""$PUBLIC_ANALYSISBOT_GITHUB_TOKEN_B" scripts/circleci/report-bundle-size.sh << parameters.platform >> command: GITHUB_TOKEN="$PUBLIC_ANALYSISBOT_GITHUB_TOKEN_A""$PUBLIC_ANALYSISBOT_GITHUB_TOKEN_B" scripts/circleci/report-bundle-size.sh << parameters.platform >> || true
# ------------------------- # -------------------------
# JOBS # JOBS

Просмотреть файл

@ -52,44 +52,21 @@ async function updateComment(octokit, issueParams, body, replacePattern) {
/** /**
* Creates or updates a comment with specified pattern. * Creates or updates a comment with specified pattern.
* @param {{ auth: string; owner: string; repo: string; issue_number: string; }} params
* @param {string} body Comment body * @param {string} body Comment body
* @param {string} replacePattern Pattern for finding the comment to update * @param {string} replacePattern Pattern for finding the comment to update
*/ */
async function createOrUpdateComment(body, replacePattern) { async function createOrUpdateComment(
const {GITHUB_TOKEN, GITHUB_OWNER, GITHUB_REPO, GITHUB_PR_NUMBER} = {auth, ...issueParams},
process.env; body,
if (!GITHUB_TOKEN || !GITHUB_OWNER || !GITHUB_REPO || !GITHUB_PR_NUMBER) { replacePattern,
if (!GITHUB_TOKEN) { ) {
console.error(
'Missing GITHUB_TOKEN. Example: 5fd88b964fa214c4be2b144dc5af5d486a2f8c1e. PR feedback cannot be provided on GitHub without a valid token.',
);
}
if (!GITHUB_OWNER) {
console.error('Missing GITHUB_OWNER. Example: facebook');
}
if (!GITHUB_REPO) {
console.error('Missing GITHUB_REPO. Example: react-native');
}
if (!GITHUB_PR_NUMBER) {
console.error(
'Missing GITHUB_PR_NUMBER. Example: 4687. PR feedback cannot be provided on GitHub without a valid pull request number.',
);
}
process.exit(1);
}
if (!body) { if (!body) {
return; return;
} }
const {Octokit} = require('@octokit/rest'); const {Octokit} = require('@octokit/rest');
const octokit = new Octokit({auth: GITHUB_TOKEN}); const octokit = new Octokit({auth});
const issueParams = {
owner: GITHUB_OWNER,
repo: GITHUB_REPO,
issue_number: GITHUB_PR_NUMBER,
};
if (await updateComment(octokit, issueParams, body, replacePattern)) { if (await updateComment(octokit, issueParams, body, replacePattern)) {
return; return;
@ -103,6 +80,59 @@ async function createOrUpdateComment(body, replacePattern) {
}); });
} }
/**
* Validates that required environment variables are set.
* @returns {boolean} `true` if everything is in order; `false` otherwise.
*/
function validateEnvironment() {
const {
GITHUB_TOKEN,
GITHUB_OWNER,
GITHUB_REPO,
GITHUB_PR_NUMBER,
GITHUB_REF,
} = process.env;
// We need the following variables to post a comment on a PR
if (
!GITHUB_TOKEN ||
!GITHUB_OWNER ||
!GITHUB_REPO ||
!GITHUB_PR_NUMBER ||
!GITHUB_REF
) {
if (!GITHUB_TOKEN) {
console.error(
'Missing GITHUB_TOKEN. Example: ghp_5fd88b964fa214c4be2b144dc5af5d486a2. PR feedback cannot be provided on GitHub without a valid token.',
);
}
if (!GITHUB_OWNER) {
console.error('Missing GITHUB_OWNER. Example: facebook');
}
if (!GITHUB_REPO) {
console.error('Missing GITHUB_REPO. Example: react-native');
}
if (!GITHUB_PR_NUMBER) {
console.error(
'Missing GITHUB_PR_NUMBER. Example: 4687. PR feedback cannot be provided on GitHub without a valid pull request number.',
);
}
if (!GITHUB_REF) {
console.error("Missing GITHUB_REF. This should've been set by the CI.");
}
return false;
}
console.log(' GITHUB_TOKEN=REDACTED');
console.log(` GITHUB_OWNER=${GITHUB_OWNER}`);
console.log(` GITHUB_REPO=${GITHUB_REPO}`);
console.log(` GITHUB_PR_NUMBER=${GITHUB_PR_NUMBER}`);
console.log(` GITHUB_REF=${GITHUB_REF}`);
return true;
}
module.exports = { module.exports = {
createOrUpdateComment, createOrUpdateComment,
validateEnvironment,
}; };

Просмотреть файл

@ -9,27 +9,27 @@
'use strict'; 'use strict';
const {GITHUB_TOKEN, CIRCLE_BUILD_URL, GITHUB_SHA} = process.env; const {
if (!GITHUB_TOKEN || !CIRCLE_BUILD_URL) { CIRCLE_BUILD_URL,
if (!GITHUB_TOKEN) { GITHUB_OWNER,
console.error("Missing GITHUB_TOKEN. This should've been set by the CI."); GITHUB_PR_NUMBER,
} GITHUB_REPO,
if (!CIRCLE_BUILD_URL) { GITHUB_SHA,
console.error( GITHUB_TOKEN,
"Missing CIRCLE_BUILD_URL. This should've been set by the CI.", } = process.env;
);
}
process.exit(1);
}
const {createOrUpdateComment} = require('./make-comment'); const {
createOrUpdateComment,
validateEnvironment: validateEnvironmentForMakeComment,
} = require('./make-comment');
/** /**
* Creates or updates a comment with specified pattern. * Creates or updates a comment with specified pattern.
* @param {{ auth: string; owner: string; repo: string; issue_number: string; }} params
* @param {string} buildURL link to circleCI build * @param {string} buildURL link to circleCI build
* @param {string} commitSha github sha of PR * @param {string} commitSha github sha of PR
*/ */
function postArtifactLink(buildUrl, commitSha) { function postArtifactLink(params, buildUrl, commitSha) {
// build url link is redirected by CircleCI so appending `/artifacts` doesn't work // build url link is redirected by CircleCI so appending `/artifacts` doesn't work
const artifactLink = buildUrl; const artifactLink = buildUrl;
const comment = [ const comment = [
@ -38,11 +38,48 @@ function postArtifactLink(buildUrl, commitSha) {
} is ready.`, } is ready.`,
`To use, download tarball from "Artifacts" tab in [this CircleCI job](${artifactLink}) then run \`yarn add <path to tarball>\` in your React Native project.`, `To use, download tarball from "Artifacts" tab in [this CircleCI job](${artifactLink}) then run \`yarn add <path to tarball>\` in your React Native project.`,
].join('\n'); ].join('\n');
createOrUpdateComment(comment); createOrUpdateComment(params, comment);
}
/**
* Validates that required environment variables are set.
* @returns {boolean} `true` if everything is in order; `false` otherwise.
*/
function validateEnvironment() {
if (
!validateEnvironmentForMakeComment() ||
!CIRCLE_BUILD_URL ||
!GITHUB_SHA
) {
if (!GITHUB_SHA) {
console.error("Missing GITHUB_SHA. This should've been set by the CI.");
}
if (!CIRCLE_BUILD_URL) {
console.error(
"Missing CIRCLE_BUILD_URL. This should've been set by the CI.",
);
}
return false;
}
console.log(` GITHUB_SHA=${GITHUB_SHA}`);
console.log(` CIRCLE_BUILD_URL=${CIRCLE_BUILD_URL}`);
return true;
}
if (!validateEnvironment()) {
process.exit(1);
} }
try { try {
postArtifactLink(CIRCLE_BUILD_URL, GITHUB_SHA); const params = {
auth: GITHUB_TOKEN,
owner: GITHUB_OWNER,
repo: GITHUB_REPO,
issue_number: GITHUB_PR_NUMBER,
};
postArtifactLink(params, CIRCLE_BUILD_URL, GITHUB_SHA);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
process.exitCode = 1; process.exitCode = 1;

Просмотреть файл

@ -9,20 +9,21 @@
'use strict'; 'use strict';
const {GITHUB_REF, GITHUB_SHA} = process.env; const {
if (!GITHUB_REF || !GITHUB_SHA) { GITHUB_TOKEN,
if (!GITHUB_REF) { GITHUB_OWNER,
console.error("Missing GITHUB_REF. This should've been set by the CI."); GITHUB_REPO,
} GITHUB_PR_NUMBER,
if (!GITHUB_SHA) { GITHUB_REF,
console.error("Missing GITHUB_SHA. This should've been set by the CI."); GITHUB_SHA,
} } = process.env;
process.exit(1);
}
const fs = require('fs'); const fs = require('fs');
const datastore = require('./datastore'); const datastore = require('./datastore');
const {createOrUpdateComment} = require('./make-comment'); const {
createOrUpdateComment,
validateEnvironment: validateEnvironmentForMakeComment,
} = require('./make-comment');
/** /**
* Generates and submits a comment. If this is run on the main or release branch, data is * Generates and submits a comment. If this is run on the main or release branch, data is
@ -47,7 +48,7 @@ async function reportSizeStats(stats, replacePattern) {
); );
const collection = datastore.getBinarySizesCollection(store); const collection = datastore.getBinarySizesCollection(store);
if (GITHUB_REF === 'main' || GITHUB_REF.endsWith('-stable')) { if (!isPullRequest(GITHUB_REF)) {
// Ensure we only store numbers greater than zero. // Ensure we only store numbers greater than zero.
const validatedStats = Object.keys(stats).reduce((validated, key) => { const validatedStats = Object.keys(stats).reduce((validated, key) => {
const value = stats[key]; const value = stats[key];
@ -74,11 +75,18 @@ async function reportSizeStats(stats, replacePattern) {
); );
} }
} else { } else {
const params = {
auth: GITHUB_TOKEN,
owner: GITHUB_OWNER,
repo: GITHUB_REPO,
issue_number: GITHUB_PR_NUMBER,
};
// For PRs, always compare vs main. // For PRs, always compare vs main.
const document = const document =
(await datastore.getLatestDocument(collection, 'main')) || {}; (await datastore.getLatestDocument(collection, 'main')) || {};
const comment = formatBundleStats(document, stats); const comment = formatBundleStats(document, stats);
createOrUpdateComment(comment, replacePattern); createOrUpdateComment(params, comment, replacePattern);
} }
await datastore.terminateStore(store); await datastore.terminateStore(store);
@ -162,6 +170,38 @@ function android_getApkSize(engine, arch) {
); );
} }
/**
* Returns whether the specified ref points to a pull request.
*/
function isPullRequest(ref) {
return ref !== 'main' && !/^\d+\.\d+-stable$/.test(ref);
}
/**
* Validates that required environment variables are set.
* @returns {boolean} `true` if everything is in order; `false` otherwise.
*/
function validateEnvironment() {
if (!GITHUB_REF) {
console.error("Missing GITHUB_REF. This should've been set by the CI.");
return false;
}
if (isPullRequest(GITHUB_REF)) {
if (!validateEnvironmentForMakeComment()) {
return false;
}
} else if (!GITHUB_SHA) {
// To update the data store, we need the SHA associated with the build
console.error("Missing GITHUB_SHA. This should've been set by the CI.");
return false;
}
console.log(` GITHUB_SHA=${GITHUB_SHA}`);
return true;
}
/** /**
* Reports app bundle size. * Reports app bundle size.
* @param {string} target * @param {string} target
@ -207,6 +247,10 @@ async function report(target) {
} }
} }
if (!validateEnvironment()) {
process.exit(1);
}
const {[2]: target} = process.argv; const {[2]: target} = process.argv;
report(target).catch(error => { report(target).catch(error => {
console.error(error); console.error(error);