Skip to content

Commit

Permalink
ci: ignore bundle size reporter failures (#32490)
Browse files Browse the repository at this point in the history
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: #32490

Test Plan: CI should ignore bundle size reporter failures.

Reviewed By: fkgozali

Differential Revision: D32008694

Pulled By: lunaleaps

fbshipit-source-id: 68e25ac2fbb23c1d7a55e667c90aec3a61302b8a
  • Loading branch information
tido64 authored and facebook-github-bot committed Nov 3, 2021
1 parent 5c04586 commit 8649174
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ commands:
- install_github_bot_deps
- run:
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
Expand Down
86 changes: 58 additions & 28 deletions bots/make-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,58 @@ async function updateComment(octokit, issueParams, body, replacePattern) {

/**
* 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} replacePattern Pattern for finding the comment to update
*/
async function createOrUpdateComment(body, replacePattern) {
const {GITHUB_TOKEN, GITHUB_OWNER, GITHUB_REPO, GITHUB_PR_NUMBER} =
process.env;
if (!GITHUB_TOKEN || !GITHUB_OWNER || !GITHUB_REPO || !GITHUB_PR_NUMBER) {
async function createOrUpdateComment(
{auth, ...issueParams},
body,
replacePattern,
) {
if (!body) {
return;
}

const {Octokit} = require('@octokit/rest');
const octokit = new Octokit({auth});

if (await updateComment(octokit, issueParams, body, replacePattern)) {
return;
}

// We found no comments to replace, so we'll create a new one.

octokit.issues.createComment({
...issueParams,
body,
});
}

/**
* 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: 5fd88b964fa214c4be2b144dc5af5d486a2f8c1e. PR feedback cannot be provided on GitHub without a valid token.',
'Missing GITHUB_TOKEN. Example: ghp_5fd88b964fa214c4be2b144dc5af5d486a2. PR feedback cannot be provided on GitHub without a valid token.',
);
}
if (!GITHUB_OWNER) {
Expand All @@ -75,34 +117,22 @@ async function createOrUpdateComment(body, replacePattern) {
'Missing GITHUB_PR_NUMBER. Example: 4687. PR feedback cannot be provided on GitHub without a valid pull request number.',
);
}
process.exit(1);
}

if (!body) {
return;
}

const {Octokit} = require('@octokit/rest');
const octokit = new Octokit({auth: GITHUB_TOKEN});

const issueParams = {
owner: GITHUB_OWNER,
repo: GITHUB_REPO,
issue_number: GITHUB_PR_NUMBER,
};
if (!GITHUB_REF) {
console.error("Missing GITHUB_REF. This should've been set by the CI.");
}

if (await updateComment(octokit, issueParams, body, replacePattern)) {
return;
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}`);

// We found no comments to replace, so we'll create a new one.

octokit.issues.createComment({
...issueParams,
body,
});
return true;
}

module.exports = {
createOrUpdateComment,
validateEnvironment,
};
69 changes: 53 additions & 16 deletions bots/post-artifacts-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@

'use strict';

const {GITHUB_TOKEN, CIRCLE_BUILD_URL, GITHUB_SHA} = process.env;
if (!GITHUB_TOKEN || !CIRCLE_BUILD_URL) {
if (!GITHUB_TOKEN) {
console.error("Missing GITHUB_TOKEN. 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.",
);
}
process.exit(1);
}
const {
CIRCLE_BUILD_URL,
GITHUB_OWNER,
GITHUB_PR_NUMBER,
GITHUB_REPO,
GITHUB_SHA,
GITHUB_TOKEN,
} = process.env;

const {createOrUpdateComment} = require('./make-comment');
const {
createOrUpdateComment,
validateEnvironment: validateEnvironmentForMakeComment,
} = require('./make-comment');

/**
* 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} 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
const artifactLink = buildUrl;
const comment = [
Expand All @@ -38,11 +38,48 @@ function postArtifactLink(buildUrl, commitSha) {
} 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.`,
].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 {
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) {
console.error(error);
process.exitCode = 1;
Expand Down
70 changes: 57 additions & 13 deletions bots/report-bundle-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@

'use strict';

const {GITHUB_REF, GITHUB_SHA} = process.env;
if (!GITHUB_REF || !GITHUB_SHA) {
if (!GITHUB_REF) {
console.error("Missing GITHUB_REF. This should've been set by the CI.");
}
if (!GITHUB_SHA) {
console.error("Missing GITHUB_SHA. This should've been set by the CI.");
}
process.exit(1);
}
const {
GITHUB_TOKEN,
GITHUB_OWNER,
GITHUB_REPO,
GITHUB_PR_NUMBER,
GITHUB_REF,
GITHUB_SHA,
} = process.env;

const fs = require('fs');
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
Expand All @@ -47,7 +48,7 @@ async function reportSizeStats(stats, replacePattern) {
);
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.
const validatedStats = Object.keys(stats).reduce((validated, key) => {
const value = stats[key];
Expand All @@ -74,11 +75,18 @@ async function reportSizeStats(stats, replacePattern) {
);
}
} else {
const params = {
auth: GITHUB_TOKEN,
owner: GITHUB_OWNER,
repo: GITHUB_REPO,
issue_number: GITHUB_PR_NUMBER,
};

// For PRs, always compare vs main.
const document =
(await datastore.getLatestDocument(collection, 'main')) || {};
const comment = formatBundleStats(document, stats);
createOrUpdateComment(comment, replacePattern);
createOrUpdateComment(params, comment, replacePattern);
}

await datastore.terminateStore(store);
Expand Down Expand Up @@ -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.
* @param {string} target
Expand Down Expand Up @@ -207,6 +247,10 @@ async function report(target) {
}
}

if (!validateEnvironment()) {
process.exit(1);
}

const {[2]: target} = process.argv;
report(target).catch(error => {
console.error(error);
Expand Down

0 comments on commit 8649174

Please sign in to comment.