From 403c75d97514cc65ba4f0944c52574139ca2b993 Mon Sep 17 00:00:00 2001 From: Zachary Keeping Date: Mon, 27 Nov 2023 14:52:41 +1100 Subject: [PATCH 1/3] Bump version and use newer api --- api/functions/firestore.js | 67 +++++++++++------------- api/functions/index.js | 2 - api/functions/package-lock.json | 92 +++++++++++++++++++++++++++------ api/functions/package.json | 4 +- 4 files changed, 106 insertions(+), 59 deletions(-) diff --git a/api/functions/firestore.js b/api/functions/firestore.js index f82cece7..1bdd64c3 100644 --- a/api/functions/firestore.js +++ b/api/functions/firestore.js @@ -1,45 +1,36 @@ -const admin = require('firebase-admin'); +const { initializeApp } = require('firebase-admin/app'); +const { getFirestore } = require('firebase-admin/firestore'); const { CONSTANTS } = require('./consts'); -exports.getUserIdFromApiKey = (api) => { - return admin - .firestore() - .collection(CONSTANTS.users) - .where(CONSTANTS.apiKey, '==', api) - .get() - .then((x) => (x.docs.length === 1 ? x.docs[0].id : null)); +initializeApp(); +const db = getFirestore(); + +exports.getUserIdFromApiKey = async (api) => { + const q = db.collection(CONSTANTS.users); + const users = await q.where(CONSTANTS.apiKey, '==', api).limit(1).get(); + return users.empty ? null : users.docs[0].id; }; -exports.updateLastBuild = (userId, apikey, runId) => { - return admin - .firestore() - .collection(CONSTANTS.users) - .doc(userId) - .update({ - lastBuild: new Date(), - runId, - }) - .then(() => - admin.firestore().collection(CONSTANTS.runs).doc(runId).set({ - apikey, - runId, - }) - ); +exports.updateLastBuild = async (userId, apikey, runId) => { + await db.collection(CONSTANTS.users).doc(userId).set({ + lastBuild: new Date(), + runId, + }, { merge: true }); + await db.collection(CONSTANTS.runs).doc(runId).set({ + apikey, + runId, + }, { merge: true }); }; -exports.getRun = (runId) => - admin - .firestore() - .collection(CONSTANTS.runs) - .doc(runId) - .get() - .then((doc) => doc.data()); +exports.getRun = async (runId) => { + const doc = await db.collection(CONSTANTS.runs).doc(runId).get(); + return doc.data(); +}; -exports.getAlertEmailConfig = () => { - return admin - .firestore() - .collection(CONSTANTS.config) - .doc('alertEmailConfig') - .get() - .then((doc) => doc.data()) -}; \ No newline at end of file +exports.getAlertEmailConfig = async () => { + const doc = await db + .collection(CONSTANTS.config) + .doc('alertEmailConfig') + .get(); + return doc.data(); +}; diff --git a/api/functions/index.js b/api/functions/index.js index cd98721a..7ff28b98 100644 --- a/api/functions/index.js +++ b/api/functions/index.js @@ -1,6 +1,5 @@ const functions = require('firebase-functions'); const express = require('express'); -const admin = require('firebase-admin'); const R = require('ramda'); const fetch = require('node-fetch'); const Queue = require('better-queue'); @@ -55,7 +54,6 @@ const { } = require('./firestore'); var cors = require('cors'); -admin.initializeApp(); const app = express(); // middlewares diff --git a/api/functions/package-lock.json b/api/functions/package-lock.json index 5b50ff71..7aac9a30 100644 --- a/api/functions/package-lock.json +++ b/api/functions/package-lock.json @@ -12,7 +12,7 @@ "cors": "^2.8.5", "dotenv": "^8.6.0", "express": "^4.18.2", - "firebase-admin": "^11.8.0", + "firebase-admin": "^11.11.1", "firebase-functions": "^3.24.1", "node-fetch": "^2.6.11", "ramda": "^0.27.2", @@ -1021,15 +1021,45 @@ "optional": true }, "node_modules/@google-cloud/firestore": { - "version": "6.5.0", - "resolved": "https://registry.npmjs.org/@google-cloud/firestore/-/firestore-6.5.0.tgz", - "integrity": "sha512-U0QwG6pEQxO5c0v0eUylswozmuvlvz7iXSW+I18jzqR2hAFrUq2Weu1wm3NaH8wGD4ZL7W9Be4cMHG5CYU8LuQ==", + "version": "6.8.0", + "resolved": "https://registry.npmjs.org/@google-cloud/firestore/-/firestore-6.8.0.tgz", + "integrity": "sha512-JRpk06SmZXLGz0pNx1x7yU3YhkUXheKgH5hbDZ4kMsdhtfV5qPLJLRI4wv69K0cZorIk+zTMOwptue7hizo0eA==", "optional": true, "dependencies": { "fast-deep-equal": "^3.1.1", "functional-red-black-tree": "^1.0.1", "google-gax": "^3.5.7", - "protobufjs": "^7.0.0" + "protobufjs": "^7.2.5" + }, + "engines": { + "node": ">=12.0.0" + } + }, + "node_modules/@google-cloud/firestore/node_modules/long": { + "version": "5.2.3", + "resolved": "https://registry.npmjs.org/long/-/long-5.2.3.tgz", + "integrity": "sha512-lcHwpNoggQTObv5apGNCTdJrO69eHOZMi4BNC+rTLER8iHAqGrUVeLh/irVIM7zTw2bOXA8T6uNPeujwOLg/2Q==", + "optional": true + }, + "node_modules/@google-cloud/firestore/node_modules/protobufjs": { + "version": "7.2.5", + "resolved": "https://registry.npmjs.org/protobufjs/-/protobufjs-7.2.5.tgz", + "integrity": "sha512-gGXRSXvxQ7UiPgfw8gevrfRWcTlSbOFg+p/N+JVJEK5VhueL2miT6qTymqAmjr1Q5WbOCyJbyrk6JfWKwlFn6A==", + "hasInstallScript": true, + "optional": true, + "dependencies": { + "@protobufjs/aspromise": "^1.1.2", + "@protobufjs/base64": "^1.1.2", + "@protobufjs/codegen": "^2.0.4", + "@protobufjs/eventemitter": "^1.1.0", + "@protobufjs/fetch": "^1.1.0", + "@protobufjs/float": "^1.0.2", + "@protobufjs/inquire": "^1.1.0", + "@protobufjs/path": "^1.1.2", + "@protobufjs/pool": "^1.1.0", + "@protobufjs/utf8": "^1.1.0", + "@types/node": ">=13.7.0", + "long": "^5.0.0" }, "engines": { "node": ">=12.0.0" @@ -6264,9 +6294,9 @@ } }, "node_modules/firebase-admin": { - "version": "11.8.0", - "resolved": "https://registry.npmjs.org/firebase-admin/-/firebase-admin-11.8.0.tgz", - "integrity": "sha512-RxO0wWDnuqVikXExhVjnhVaaXpziKCad4D1rOX5c1WJdk1jAu9hfE4rbrFKZQZgF1okZS04kgCBIFJro7xn8NQ==", + "version": "11.11.1", + "resolved": "https://registry.npmjs.org/firebase-admin/-/firebase-admin-11.11.1.tgz", + "integrity": "sha512-UyEbq+3u6jWzCYbUntv/HuJiTixwh36G1R9j0v71mSvGAx/YZEWEW7uSGLYxBYE6ckVRQoKMr40PYUEzrm/4dg==", "dependencies": { "@fastify/busboy": "^1.2.1", "@firebase/database-compat": "^0.3.4", @@ -6281,7 +6311,7 @@ "node": ">=14" }, "optionalDependencies": { - "@google-cloud/firestore": "^6.5.0", + "@google-cloud/firestore": "^6.8.0", "@google-cloud/storage": "^6.9.5" } }, @@ -17472,15 +17502,43 @@ "optional": true }, "@google-cloud/firestore": { - "version": "6.5.0", - "resolved": "https://registry.npmjs.org/@google-cloud/firestore/-/firestore-6.5.0.tgz", - "integrity": "sha512-U0QwG6pEQxO5c0v0eUylswozmuvlvz7iXSW+I18jzqR2hAFrUq2Weu1wm3NaH8wGD4ZL7W9Be4cMHG5CYU8LuQ==", + "version": "6.8.0", + "resolved": "https://registry.npmjs.org/@google-cloud/firestore/-/firestore-6.8.0.tgz", + "integrity": "sha512-JRpk06SmZXLGz0pNx1x7yU3YhkUXheKgH5hbDZ4kMsdhtfV5qPLJLRI4wv69K0cZorIk+zTMOwptue7hizo0eA==", "optional": true, "requires": { "fast-deep-equal": "^3.1.1", "functional-red-black-tree": "^1.0.1", "google-gax": "^3.5.7", - "protobufjs": "^7.0.0" + "protobufjs": "^7.2.5" + }, + "dependencies": { + "long": { + "version": "5.2.3", + "resolved": "https://registry.npmjs.org/long/-/long-5.2.3.tgz", + "integrity": "sha512-lcHwpNoggQTObv5apGNCTdJrO69eHOZMi4BNC+rTLER8iHAqGrUVeLh/irVIM7zTw2bOXA8T6uNPeujwOLg/2Q==", + "optional": true + }, + "protobufjs": { + "version": "7.2.5", + "resolved": "https://registry.npmjs.org/protobufjs/-/protobufjs-7.2.5.tgz", + "integrity": "sha512-gGXRSXvxQ7UiPgfw8gevrfRWcTlSbOFg+p/N+JVJEK5VhueL2miT6qTymqAmjr1Q5WbOCyJbyrk6JfWKwlFn6A==", + "optional": true, + "requires": { + "@protobufjs/aspromise": "^1.1.2", + "@protobufjs/base64": "^1.1.2", + "@protobufjs/codegen": "^2.0.4", + "@protobufjs/eventemitter": "^1.1.0", + "@protobufjs/fetch": "^1.1.0", + "@protobufjs/float": "^1.0.2", + "@protobufjs/inquire": "^1.1.0", + "@protobufjs/path": "^1.1.2", + "@protobufjs/pool": "^1.1.0", + "@protobufjs/utf8": "^1.1.0", + "@types/node": ">=13.7.0", + "long": "^5.0.0" + } + } } }, "@google-cloud/paginator": { @@ -21675,14 +21733,14 @@ } }, "firebase-admin": { - "version": "11.8.0", - "resolved": "https://registry.npmjs.org/firebase-admin/-/firebase-admin-11.8.0.tgz", - "integrity": "sha512-RxO0wWDnuqVikXExhVjnhVaaXpziKCad4D1rOX5c1WJdk1jAu9hfE4rbrFKZQZgF1okZS04kgCBIFJro7xn8NQ==", + "version": "11.11.1", + "resolved": "https://registry.npmjs.org/firebase-admin/-/firebase-admin-11.11.1.tgz", + "integrity": "sha512-UyEbq+3u6jWzCYbUntv/HuJiTixwh36G1R9j0v71mSvGAx/YZEWEW7uSGLYxBYE6ckVRQoKMr40PYUEzrm/4dg==", "requires": { "@fastify/busboy": "^1.2.1", "@firebase/database-compat": "^0.3.4", "@firebase/database-types": "^0.10.4", - "@google-cloud/firestore": "^6.5.0", + "@google-cloud/firestore": "^6.8.0", "@google-cloud/storage": "^6.9.5", "@types/node": ">=12.12.47", "jsonwebtoken": "^9.0.0", diff --git a/api/functions/package.json b/api/functions/package.json index 7334e450..8b5bf5f8 100644 --- a/api/functions/package.json +++ b/api/functions/package.json @@ -4,7 +4,7 @@ "scripts": { "lint": "eslint .", "test": "jest", - "serve": "firebase serve --only functions --port 5001", + "serve": "firebase serve -o 0.0.0.0 --only functions --port 5001", "shell": "firebase functions:shell", "start": "npm run shell", "deploy": "firebase deploy --token $FIREBASETOKEN --only functions", @@ -21,7 +21,7 @@ "cors": "^2.8.5", "dotenv": "^8.6.0", "express": "^4.18.2", - "firebase-admin": "^11.8.0", + "firebase-admin": "^11.11.1", "firebase-functions": "^3.24.1", "node-fetch": "^2.6.11", "ramda": "^0.27.2", From b30a111f134662a8de4a0978cfca2cf207339454 Mon Sep 17 00:00:00 2001 From: Zachary Keeping Date: Mon, 27 Nov 2023 14:56:00 +1100 Subject: [PATCH 2/3] Formatting --- api/functions/firestore.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/api/functions/firestore.js b/api/functions/firestore.js index 1bdd64c3..8173ba3b 100644 --- a/api/functions/firestore.js +++ b/api/functions/firestore.js @@ -12,14 +12,20 @@ exports.getUserIdFromApiKey = async (api) => { }; exports.updateLastBuild = async (userId, apikey, runId) => { - await db.collection(CONSTANTS.users).doc(userId).set({ - lastBuild: new Date(), - runId, - }, { merge: true }); - await db.collection(CONSTANTS.runs).doc(runId).set({ - apikey, - runId, - }, { merge: true }); + await db.collection(CONSTANTS.users).doc(userId).set( + { + lastBuild: new Date(), + runId, + }, + { merge: true } + ); + await db.collection(CONSTANTS.runs).doc(runId).set( + { + apikey, + runId, + }, + { merge: true } + ); }; exports.getRun = async (runId) => { From 750a6d790400f5f9b33ad433116c3238ae96c19f Mon Sep 17 00:00:00 2001 From: Zachary Keeping Date: Mon, 27 Nov 2023 16:33:44 +1100 Subject: [PATCH 3/3] Increase timeout --- api/functions/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/functions/index.js b/api/functions/index.js index 7ff28b98..e1be6ca8 100644 --- a/api/functions/index.js +++ b/api/functions/index.js @@ -362,5 +362,5 @@ app.post('/scanresult/:api/:buildId', async (req, res) => { res.json(runId); }); -exports.api = functions.region('asia-east2').https.onRequest(app); -exports.api2 = functions.region('asia-northeast1').https.onRequest(app); \ No newline at end of file +exports.api = functions.runWith({ timeoutSeconds: 540 }).region('asia-east2').https.onRequest(app); +exports.api2 = functions.runWith({ timeoutSeconds: 540 }).region('asia-northeast1').https.onRequest(app); \ No newline at end of file