Skip to content

Commit

Permalink
fix post-artifacts-link.js
Browse files Browse the repository at this point in the history
  • Loading branch information
tido64 committed Nov 3, 2021
1 parent c83fef7 commit 33695c2
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 41 deletions.
54 changes: 54 additions & 0 deletions bots/make-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,60 @@ async function createOrUpdateComment(
});
}

/**
* 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=${GITHUB_TOKEN}`);
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 = {
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
31 changes: 6 additions & 25 deletions bots/report-bundle-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ const {

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 Down Expand Up @@ -176,7 +179,7 @@ function isPullRequest(ref) {

/**
* Validates that required environment variables are set.
* @returns `true` if everything is in order; `false` otherwise.
* @returns {boolean} `true` if everything is in order; `false` otherwise.
*/
function validateEnvironment() {
if (!GITHUB_REF) {
Expand All @@ -185,24 +188,7 @@ function validateEnvironment() {
}

if (isPullRequest(GITHUB_REF)) {
// We need the following variables to post a comment on a PR
if (!GITHUB_TOKEN || !GITHUB_OWNER || !GITHUB_REPO || !GITHUB_PR_NUMBER) {
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 (!validateEnvironmentForMakeComment()) {
return false;
}
} else if (!GITHUB_SHA) {
Expand All @@ -211,11 +197,6 @@ function validateEnvironment() {
return false;
}

console.log(` GITHUB_TOKEN=${GITHUB_TOKEN}`);
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}`);
console.log(` GITHUB_SHA=${GITHUB_SHA}`);

return true;
Expand Down

0 comments on commit 33695c2

Please sign in to comment.