Files
react-native/packages/react-native-bots/datastore.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

179 lines
4.9 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';
const {initializeApp} = require('firebase/app');
const {getAuth, signInWithEmailAndPassword} = require('firebase/auth');
const firestore = require('firebase/firestore');
/**
* Initializes store, and optionally authenticates current user.
*
* @param {string?} email
* @param {string?} password
* @returns {Promise<firebase.firestore.Firestore>} Reference to store instance
*/
async function initializeStore(email, password) {
const PROJECT_ID = 'react-native-1583841384889';
const apiKey = [
'AIzaSyCm',
'5hN3nVNY',
'tF9zkSHa',
'oFpeVe3g',
'LceuC0Q',
].join('');
const firebaseApp = initializeApp({
apiKey,
authDomain: `${PROJECT_ID}.firebaseapp.com`,
databaseURL: `https://${PROJECT_ID}.firebaseio.com`,
projectId: PROJECT_ID,
storageBucket: `${PROJECT_ID}.appspot.com`,
messagingSenderId: '329254200967',
appId: '1:329254200967:web:c465681d024115bc303a22',
measurementId: 'G-ZKSZ7SCLHK',
});
if (email && password) {
await signInWithEmailAndPassword(
getAuth(firebaseApp),
email,
password,
).catch(error => console.log(error));
}
return firestore.getFirestore(firebaseApp);
}
/**
* Initializes 'binary-sizes' collection using the initial commit's data.
*
* @param {firebase.firestore.Firestore} db Reference to store instance
*/
function initializeBinarySizesCollection(db) {
const collectionRef = getBinarySizesCollection(db);
const docRef = firestore.doc(
collectionRef,
'a15603d8f1ecdd673d80be318293cee53eb4475d',
);
firestore.setDoc(docRef, {
'android-hermes-arm64-v8a': 0,
'android-hermes-armeabi-v7a': 0,
'android-hermes-x86': 0,
'android-hermes-x86_64': 0,
'android-jsc-arm64-v8a': 0,
'android-jsc-armeabi-v7a': 0,
'android-jsc-x86': 0,
'android-jsc-x86_64': 0,
'ios-universal': 0,
timestamp: new Date('Thu Jan 29 17:10:49 2015 -0800'),
});
}
/**
* Returns 'binary-sizes' collection.
*
* @param {firebase.firestore.Firestore} db Reference to store instance
*/
function getBinarySizesCollection(db) {
const BINARY_SIZES_COLLECTION = 'binary-sizes';
return firestore.collection(db, BINARY_SIZES_COLLECTION);
}
/**
* Creates or updates the specified entry.
*
* @param {firebase.firestore.CollectionReference<firebase.firestore.DocumentData>} collection
* @param {string} sha The Git SHA used to identify the entry
* @param {firebase.firestore.UpdateData} data The data to be inserted/updated
* @param {string} branch The Git branch where this data was computed for
* @returns {Promise<void>}
*/
function createOrUpdateDocument(collectionRef, sha, data, branch) {
const stampedData = {
...data,
timestamp: firestore.Timestamp.now(),
branch,
};
const docRef = firestore.doc(collectionRef, sha);
return firestore.updateDoc(docRef, stampedData).catch(async error => {
if (error.code === 'not-found') {
await firestore
.setDoc(docRef, stampedData)
.catch(setError => console.log(setError));
} else {
console.log(error);
}
});
}
/**
* Returns the latest document in collection.
*
* @param {firebase.firestore.CollectionReference<firebase.firestore.DocumentData>} collection
* @param {string} branch The Git branch for the data
* @returns {Promise<firebase.firestore.DocumentData | undefined>}
*/
async function getLatestDocument(collectionRef, branch) {
try {
const querySnapshot = await firestore.getDocs(
firestore.query(
collectionRef,
firestore.orderBy('timestamp', 'desc'),
firestore.where('branch', '==', branch),
firestore.limit(1),
),
);
if (querySnapshot.empty) {
return undefined;
}
const doc = querySnapshot.docs[0];
return {
...doc.data(),
commit: doc.id,
};
} catch (error) {
console.log(error);
return undefined;
}
}
/**
* Terminates the supplied store.
*
* Documentation says that we don't need to call `terminate()` but the script
* will just hang around until the connection times out if we don't.
*
* @param {Promise<firebase.firestore.Firestore>} db
*/
async function terminateStore(db) {
await firestore.terminate(db);
}
/**
* Example usage:
*
* const datastore = require('./datastore');
* const store = datastore.initializeStore();
* const binarySizes = datastore.getBinarySizesCollection(store);
* console.log(await getLatestDocument(binarySizes));
* console.log(await createOrUpdateDocument(binarySizes, 'some-id', {data: 0}));
* terminateStore(store);
*
*/
module.exports = {
initializeStore,
initializeBinarySizesCollection,
getBinarySizesCollection,
createOrUpdateDocument,
getLatestDocument,
terminateStore,
};