Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 150 additions & 0 deletions bots/datastore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/**
* 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 firebase = require('firebase/app');
require('firebase/auth');
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 app = firebase.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 app
.auth()
.signInWithEmailAndPassword(email, password)
.catch(error => console.log(error));
}

return app.firestore();
}

/**
* Initializes 'binary-sizes' collection using the initial commit's data.
* @param {firebase.firestore.Firestore} firestore Reference to store instance
*/
function initializeBinarySizesCollection(firestore) {
return getBinarySizesCollection(firestore)
.doc('a15603d8f1ecdd673d80be318293cee53eb4475d')
.set({
'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} firestore Reference to store instance
*/
function getBinarySizesCollection(firestore) {
const BINARY_SIZES_COLLECTION = 'binary-sizes';
return firestore.collection(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
* @returns {Promise<void>}
*/
function createOrUpdateDocument(collection, sha, data) {
const stampedData = {
...data,
timestamp: Date.now(),
};
const docRef = collection.doc(sha);
return docRef.update(stampedData).catch(async error => {
if (error.code === 'not-found') {
await docRef.set(stampedData).catch(error => console.log(error));
} else {
console.log(error);
}
});
}

/**
* Returns the latest document in collection.
* @param {firebase.firestore.CollectionReference<firebase.firestore.DocumentData>} collection
* @returns {Promise<firebase.firestore.DocumentData | undefined>}
*/
function getLatestDocument(collection) {
return collection
.orderBy('timestamp', 'desc')
.limit(1)
.get()
.then(snapshot => {
if (snapshot.empty) {
return undefined;
}

const doc = snapshot.docs[0];
return {
...doc.data(),
commit: doc.id,
};
})
.catch(error => {
console.log(error);
return undefined;
});
}

/**
* 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}));
*
* // 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.
* firestore.terminate();
*/
module.exports = {
initializeStore,
initializeBinarySizesCollection,
getBinarySizesCollection,
createOrUpdateDocument,
getLatestDocument,
};
61 changes: 37 additions & 24 deletions bots/make-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,13 @@

'use strict';

const {GITHUB_TOKEN, GITHUB_OWNER, GITHUB_REPO, GITHUB_PR_NUMBER} = process.env;
if (!GITHUB_TOKEN || !GITHUB_OWNER || !GITHUB_REPO || !GITHUB_PR_NUMBER) {
if (!GITHUB_TOKEN) {
console.error(
'Missing GITHUB_TOKEN. Example: 5fd88b964fa214c4be2b144dc5af5d486a2f8c1e. 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.',
);
}
process.exit(1);
}

/**
* 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;
Expand Down Expand Up @@ -64,7 +50,33 @@ async function updateComment(octokit, issueParams, body, replacePattern) {
return true;
}

async function main(body, replacePattern) {
/**
* Creates or updates a comment with specified pattern.
* @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) {
if (!GITHUB_TOKEN) {
console.error(
'Missing GITHUB_TOKEN. Example: 5fd88b964fa214c4be2b144dc5af5d486a2f8c1e. 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.',
);
}
process.exit(1);
}

if (!body) {
return;
}
Expand All @@ -90,5 +102,6 @@ async function main(body, replacePattern) {
});
}

const {[2]: body, [3]: replacePattern} = process.argv;
main(body, replacePattern);
module.exports = {
createOrUpdateComment,
};
3 changes: 2 additions & 1 deletion bots/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"minimatch": "^3.0.4"
},
"dependencies": {
"@octokit/rest": "^16.43.0"
"@octokit/rest": "^16.43.0",
"firebase": "^7.10.0"
}
}
Loading