From 2a605c30e45f8f437cf9a935583825daf1fbe371 Mon Sep 17 00:00:00 2001 From: Luna Wei Date: Fri, 8 Oct 2021 17:53:18 -0700 Subject: [PATCH] Comment on PR with link to PR artifact Summary: Changelog: [Internal] Configure circleCI to comment on PR after building tarball Reviewed By: hramos Differential Revision: D31387660 fbshipit-source-id: 28902148cf5e2ea15320333b90a6a7fa9d553c3b --- .circleci/config.yml | 7 ++++ bots/post-artifacts-link.js | 48 +++++++++++++++++++++++++ scripts/circleci/post-artifacts-link.sh | 11 ++++++ 3 files changed, 66 insertions(+) create mode 100644 bots/post-artifacts-link.js create mode 100755 scripts/circleci/post-artifacts-link.sh diff --git a/.circleci/config.yml b/.circleci/config.yml index 2d8e41eaeae8e3..4846fb669d3d05 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -774,6 +774,13 @@ jobs: - store_artifacts: path: ~/react-native/build/ destination: build + - when: + condition: + matches: { pattern: '^pull\/.*$', value: << pipeline.git.branch >> } + steps: + - run: + name: Post link to PR build artifacts (pull-bot) + command: GITHUB_TOKEN="$PUBLIC_PULLBOT_GITHUB_TOKEN_A""$PUBLIC_PULLBOT_GITHUB_TOKEN_B" scripts/circleci/post-artifacts-link.sh # ------------------------- # JOBS: Nightly diff --git a/bots/post-artifacts-link.js b/bots/post-artifacts-link.js new file mode 100644 index 00000000000000..cc93fc5ad2d0ec --- /dev/null +++ b/bots/post-artifacts-link.js @@ -0,0 +1,48 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + */ + +'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 {createOrUpdateComment} = require('./make-comment'); + +/** + * Creates or updates a comment with specified pattern. + * @param {string} buildURL link to circleCI build + * @param {string} commitSha github sha of PR + */ +function postArtifactLink(buildUrl, commitSha) { + const artifactLink = buildUrl + '/artifacts'; + const comment = [ + `PR build artifact${ + commitSha != null ? ` for ${commitSha}` : '' + } is ready.`, + `To use, download tarball from [this CircleCI job](${artifactLink}) then run \`yarn add \` in your React Native project.`, + ].join('\n'); + createOrUpdateComment(comment); +} + +try { + postArtifactLink(CIRCLE_BUILD_URL, GITHUB_SHA); +} catch (error) { + console.error(error); + process.exitCode = 1; +} diff --git a/scripts/circleci/post-artifacts-link.sh b/scripts/circleci/post-artifacts-link.sh new file mode 100755 index 00000000000000..eb9fe28a5c05d6 --- /dev/null +++ b/scripts/circleci/post-artifacts-link.sh @@ -0,0 +1,11 @@ +#!/bin/bash +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +GITHUB_OWNER=${CIRCLE_PROJECT_USERNAME:-facebook} \ +GITHUB_REPO=${CIRCLE_PROJECT_REPONAME:-react-native} \ +GITHUB_PR_NUMBER="${CIRCLE_PR_NUMBER:-${CIRCLE_PULL_REQUEST##*/}}" \ +GITHUB_SHA=${CIRCLE_SHA1} \ +exec node bots/post-artifacts-link.js