Skip to content

Commit 175589b

Browse files
tido64facebook-github-bot
authored andcommitted
Add diffing to app bundle size reports (facebook#28284)
Summary: Add diffing to app bundle size reports. ## Changelog [Internal] [Changed] - Add diffing to app bundle size reports Pull Request resolved: facebook#28284 Test Plan: - App bundle size reports should now display a diff where available - Right now, the database contains only one entry for the last known good iOS build - Triggering a new build should not create additional comments Reviewed By: cpojer Differential Revision: D20450158 Pulled By: hramos fbshipit-source-id: 720772275f24d3ff0a49705f4dada2efe2e99bd3
1 parent f2ffa03 commit 175589b

File tree

6 files changed

+1279
-89
lines changed

6 files changed

+1279
-89
lines changed

bots/datastore.js

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
*/
9+
10+
'use strict';
11+
12+
const firebase = require('firebase/app');
13+
require('firebase/auth');
14+
require('firebase/firestore');
15+
16+
/**
17+
* Initializes store, and optionally authenticates current user.
18+
* @param {string?} email
19+
* @param {string?} password
20+
* @returns {Promise<firebase.firestore.Firestore>} Reference to store instance
21+
*/
22+
async function initializeStore(email, password) {
23+
const PROJECT_ID = 'react-native-1583841384889';
24+
const apiKey = [
25+
'AIzaSyCm',
26+
'5hN3nVNY',
27+
'tF9zkSHa',
28+
'oFpeVe3g',
29+
'LceuC0Q',
30+
].join('');
31+
const app = firebase.initializeApp({
32+
apiKey,
33+
authDomain: `${PROJECT_ID}.firebaseapp.com`,
34+
databaseURL: `https://${PROJECT_ID}.firebaseio.com`,
35+
projectId: PROJECT_ID,
36+
storageBucket: `${PROJECT_ID}.appspot.com`,
37+
messagingSenderId: '329254200967',
38+
appId: '1:329254200967:web:c465681d024115bc303a22',
39+
measurementId: 'G-ZKSZ7SCLHK',
40+
});
41+
42+
if (email && password) {
43+
await app
44+
.auth()
45+
.signInWithEmailAndPassword(email, password)
46+
.catch(error => console.log(error));
47+
}
48+
49+
return app.firestore();
50+
}
51+
52+
/**
53+
* Initializes 'binary-sizes' collection using the initial commit's data.
54+
* @param {firebase.firestore.Firestore} firestore Reference to store instance
55+
*/
56+
function initializeBinarySizesCollection(firestore) {
57+
return getBinarySizesCollection(firestore)
58+
.doc('a15603d8f1ecdd673d80be318293cee53eb4475d')
59+
.set({
60+
'android-hermes-arm64-v8a': 0,
61+
'android-hermes-armeabi-v7a': 0,
62+
'android-hermes-x86': 0,
63+
'android-hermes-x86_64': 0,
64+
'android-jsc-arm64-v8a': 0,
65+
'android-jsc-armeabi-v7a': 0,
66+
'android-jsc-x86': 0,
67+
'android-jsc-x86_64': 0,
68+
'ios-universal': 0,
69+
timestamp: new Date('Thu Jan 29 17:10:49 2015 -0800'),
70+
});
71+
}
72+
73+
/**
74+
* Returns 'binary-sizes' collection.
75+
* @param {firebase.firestore.Firestore} firestore Reference to store instance
76+
*/
77+
function getBinarySizesCollection(firestore) {
78+
const BINARY_SIZES_COLLECTION = 'binary-sizes';
79+
return firestore.collection(BINARY_SIZES_COLLECTION);
80+
}
81+
82+
/**
83+
* Creates or updates the specified entry.
84+
* @param {firebase.firestore.CollectionReference<firebase.firestore.DocumentData>} collection
85+
* @param {string} sha The Git SHA used to identify the entry
86+
* @param {firebase.firestore.UpdateData} data The data to be inserted/updated
87+
* @returns {Promise<void>}
88+
*/
89+
function createOrUpdateDocument(collection, sha, data) {
90+
const stampedData = {
91+
...data,
92+
timestamp: Date.now(),
93+
};
94+
const docRef = collection.doc(sha);
95+
return docRef.update(stampedData).catch(async error => {
96+
if (error.code === 'not-found') {
97+
await docRef.set(stampedData).catch(setError => console.log(setError));
98+
} else {
99+
console.log(error);
100+
}
101+
});
102+
}
103+
104+
/**
105+
* Returns the latest document in collection.
106+
* @param {firebase.firestore.CollectionReference<firebase.firestore.DocumentData>} collection
107+
* @returns {Promise<firebase.firestore.DocumentData | undefined>}
108+
*/
109+
function getLatestDocument(collection) {
110+
return collection
111+
.orderBy('timestamp', 'desc')
112+
.limit(1)
113+
.get()
114+
.then(snapshot => {
115+
if (snapshot.empty) {
116+
return undefined;
117+
}
118+
119+
const doc = snapshot.docs[0];
120+
return {
121+
...doc.data(),
122+
commit: doc.id,
123+
};
124+
})
125+
.catch(error => {
126+
console.log(error);
127+
return undefined;
128+
});
129+
}
130+
131+
/**
132+
* Example usage:
133+
*
134+
* const datastore = require('./datastore');
135+
* const store = datastore.initializeStore();
136+
* const binarySizes = datastore.getBinarySizesCollection(store);
137+
* console.log(await getLatestDocument(binarySizes));
138+
* console.log(await createOrUpdateDocument(binarySizes, 'some-id', {data: 0}));
139+
*
140+
* // Documentation says that we don't need to call `terminate()` but the script
141+
* // will just hang around until the connection times out if we don't.
142+
* firestore.terminate();
143+
*/
144+
module.exports = {
145+
initializeStore,
146+
initializeBinarySizesCollection,
147+
getBinarySizesCollection,
148+
createOrUpdateDocument,
149+
getLatestDocument,
150+
};

bots/make-comment.js

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,13 @@
99

1010
'use strict';
1111

12-
const {GITHUB_TOKEN, GITHUB_OWNER, GITHUB_REPO, GITHUB_PR_NUMBER} = process.env;
13-
if (!GITHUB_TOKEN || !GITHUB_OWNER || !GITHUB_REPO || !GITHUB_PR_NUMBER) {
14-
if (!GITHUB_TOKEN) {
15-
console.error(
16-
'Missing GITHUB_TOKEN. Example: 5fd88b964fa214c4be2b144dc5af5d486a2f8c1e. PR feedback cannot be provided on GitHub without a valid token.',
17-
);
18-
}
19-
if (!GITHUB_OWNER) {
20-
console.error('Missing GITHUB_OWNER. Example: facebook');
21-
}
22-
if (!GITHUB_REPO) {
23-
console.error('Missing GITHUB_REPO. Example: react-native');
24-
}
25-
if (!GITHUB_PR_NUMBER) {
26-
console.error(
27-
'Missing GITHUB_PR_NUMBER. Example: 4687. PR feedback cannot be provided on GitHub without a valid pull request number.',
28-
);
29-
}
30-
process.exit(1);
31-
}
32-
12+
/**
13+
* Updates the comment matching specified pattern.
14+
* @param {import('@octokit/rest').Octokit} octokit Octokit instance
15+
* @param {{ owner: string; repo: string; issue_number: string; }} issueParams
16+
* @param {string} body Comment body
17+
* @param {string} replacePattern Pattern for finding the comment to update
18+
*/
3319
async function updateComment(octokit, issueParams, body, replacePattern) {
3420
if (!replacePattern) {
3521
return false;
@@ -64,7 +50,38 @@ async function updateComment(octokit, issueParams, body, replacePattern) {
6450
return true;
6551
}
6652

67-
async function main(body, replacePattern) {
53+
/**
54+
* Creates or updates a comment with specified pattern.
55+
* @param {string} body Comment body
56+
* @param {string} replacePattern Pattern for finding the comment to update
57+
*/
58+
async function createOrUpdateComment(body, replacePattern) {
59+
const {
60+
GITHUB_TOKEN,
61+
GITHUB_OWNER,
62+
GITHUB_REPO,
63+
GITHUB_PR_NUMBER,
64+
} = process.env;
65+
if (!GITHUB_TOKEN || !GITHUB_OWNER || !GITHUB_REPO || !GITHUB_PR_NUMBER) {
66+
if (!GITHUB_TOKEN) {
67+
console.error(
68+
'Missing GITHUB_TOKEN. Example: 5fd88b964fa214c4be2b144dc5af5d486a2f8c1e. PR feedback cannot be provided on GitHub without a valid token.',
69+
);
70+
}
71+
if (!GITHUB_OWNER) {
72+
console.error('Missing GITHUB_OWNER. Example: facebook');
73+
}
74+
if (!GITHUB_REPO) {
75+
console.error('Missing GITHUB_REPO. Example: react-native');
76+
}
77+
if (!GITHUB_PR_NUMBER) {
78+
console.error(
79+
'Missing GITHUB_PR_NUMBER. Example: 4687. PR feedback cannot be provided on GitHub without a valid pull request number.',
80+
);
81+
}
82+
process.exit(1);
83+
}
84+
6885
if (!body) {
6986
return;
7087
}
@@ -90,5 +107,6 @@ async function main(body, replacePattern) {
90107
});
91108
}
92109

93-
const {[2]: body, [3]: replacePattern} = process.argv;
94-
main(body, replacePattern);
110+
module.exports = {
111+
createOrUpdateComment,
112+
};

bots/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"minimatch": "^3.0.4"
1010
},
1111
"dependencies": {
12-
"@octokit/rest": "^16.43.0"
12+
"@octokit/rest": "^16.43.0",
13+
"firebase": "^7.10.0"
1314
}
1415
}

0 commit comments

Comments
 (0)