Files
react-native/packages/react-native-bots/make-comment.js
T
Luna Wei 767f8e0249 Add bots as a yarn workspace and update danger action (#34652)
Summary:
allow-large-files

When working on https://github.com/facebook/react-native/pull/34614, danger is failing because it doesn't share `node_modules` with the root directory where `typescript` is installed as we added it as a parser in our eslint config.

By setting `bots` as a yarn workspace, dependencies are all installed under the root `node_modules` folder and in local testing (detailed in test section) we no longer have the `typescript module not found` error. However, danger will continue to fail on https://github.com/facebook/react-native/pull/34614 as the `danger_pr` Github action runs from what's defined on `main`.

Once these changes land, I can rebase https://github.com/facebook/react-native/pull/34614 on it and danger's eslint should pass.

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->

[Internal][Fixed] - Add `bots` directory as a yarn workspace and update `danger_pr` Github action

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

Test Plan:
To verify this fix I had to run:
```
react-native $ yarn && cd bots
react-native/bots$ yarn run danger pr https://github.com/facebook/react-native/pull/34614
```

which resulted in
```
❯ yarn run danger pr https://github.com/facebook/react-native/pull/34614
yarn run v1.22.19
$ lunaleaps/react-native/node_modules/.bin/danger pr https://github.com/facebook/react-native/pull/34614
Starting Danger PR on facebook/react-native#34614

Danger: ✓ found only warnings, not failing the build
## Warnings
🔒 package.json - <i>Changes were made to package.json. This will require a manual import by a Facebook employee.</i>

  Done in 12.78s.
```
Verified this also on another PR:
```
yarn run danger pr https://github.com/facebook/react-native/pull/34650
```

Reviewed By: NickGerleman

Differential Revision: D39435286

Pulled By: lunaleaps

fbshipit-source-id: 8c82f49facf162f4fc0918e3abd95eb7e4ad1e37
2022-09-12 22:03:34 -07:00

140 lines
3.6 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and 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';
/**
* Updates the comment matching specified pattern.
* @param {import('@octokit/rest').Octokit} octokit Octokit instance
* @param {{ owner: string; repo: string; issue_number: string; }} issueParams
* @param {string} body Comment body
* @param {string} replacePattern Pattern for finding the comment to update
*/
async function updateComment(octokit, issueParams, body, replacePattern) {
if (!replacePattern) {
return false;
}
const authenticatedUser = await octokit.users.getAuthenticated();
if (authenticatedUser.status !== 200 || !authenticatedUser.data) {
console.warn(authenticatedUser);
return false;
}
const comments = await octokit.issues.listComments(issueParams);
if (comments.status !== 200 || !comments.data) {
console.warn(comments);
return false;
}
const authedUserId = authenticatedUser.data.id;
const pattern = new RegExp(replacePattern, 'g');
const comment = comments.data.find(
// eslint-disable-next-line no-shadow
({user, body}) => user.id === authedUserId && pattern.test(body),
);
if (!comment) {
return false;
}
octokit.issues.updateComment({
...issueParams,
comment_id: comment.id,
body,
});
return true;
}
/**
* 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(
{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: 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 = {
createOrUpdateComment,
validateEnvironment,
};