Skip to content

Commit

Permalink
Merge pull request #177 from CheckMateSG/feature/nonprod-setup
Browse files Browse the repository at this point in the history
Feature/nonprod setup
  • Loading branch information
sarge1989 authored Nov 4, 2023
2 parents 644c833 + bc4cbc0 commit 5813625
Show file tree
Hide file tree
Showing 12 changed files with 307 additions and 271 deletions.
10 changes: 7 additions & 3 deletions .firebaserc
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"projects": {
"default": "checkmate-373101"
}
}
"default": "checkmate-373101",
"prod": "checkmate-373101",
"uat": "checkmate-uat"
},
"targets": {},
"etags": {}
}
28 changes: 24 additions & 4 deletions .github/workflows/firebase-deploy.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
name: Deploy Cloud Functions

on:
workflow_dispatch:
push:
branches:
- main
- develop
paths:
- 'functions/**'
- "functions/**"

jobs:
build_and_deploy:
runs-on: ubuntu-latest
Expand All @@ -22,15 +25,32 @@ jobs:
${{ runner.os }}-node-
- name: Create env file
run: |
echo "${{ secrets.ENV_FILE }}" > .env
if [ "${{ github.ref }}" == "refs/heads/main" ]; then
echo "${{ secrets.ENV_FILE }}" > .env
else
echo "${{ secrets.ENV_FILE_NONPROD }}" > .env
fi
working-directory: functions
- name: Build Cloud Functions
run: npm ci
working-directory: functions
- name: Create SA key
run: echo '${{ secrets.FIREBASE_SERVICE_ACCOUNT }}' > $HOME/gcloud.json
run: |
if [ "${{ github.ref }}" == "refs/heads/main" ]; then
echo '${{ secrets.FIREBASE_SERVICE_ACCOUNT }}' > $HOME/gcloud.json
else
echo '${{ secrets.FIREBASE_SERVICE_ACCOUNT_NONPROD }}' > $HOME/gcloud.json
fi
- name: Install Firebase CLI
run: npm install -g firebase-tools
- name: Set Firebase project
run: |
if [ "${{ github.ref }}" == "refs/heads/main" ]; then
firebase use prod
else
firebase use uat
fi
working-directory: functions
- name: Deploy Cloud Functions
run: export GOOGLE_APPLICATION_CREDENTIALS=$HOME/gcloud.json && npm run deploy
working-directory: functions
working-directory: functions
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Start docker compose for testing
run: docker-compose -f docker-compose-base.yml up -d
- name: Wait for emulator to start
run: timeout 30 bash -c 'while [[ "$(curl -s -o /dev/null -w ''%{http_code}'' http://localhost:15001/checkmate-373101/asia-southeast1/healthcheck)" != "200" ]]; do sleep 1; done' || false
run: timeout 30 bash -c 'while [[ "$(curl -s -o /dev/null -w ''%{http_code}'' http://localhost:15001/checkmate-uat/asia-southeast1/healthcheck)" != "200" ]]; do sleep 1; done' || false
- name: Install newman and lodash
run: npm ci
working-directory: integration-tests
Expand Down
5 changes: 5 additions & 0 deletions functions/.env.checkmate-uat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
NUM_SHARDS_INSTANCE_COUNT=5
NUM_SHARDS_VOTE_COUNT=5
GRAPH_API_VERSION=v15.0
ENVIRONMENT=UAT
SIMILARITY_THRESHOLD=0.85
2 changes: 1 addition & 1 deletion functions/.secret.local.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ WHATSAPP_TOKEN=WHATSAPP_TOKEN
TELEGRAM_CHECKER_BOT_TOKEN=TELEGRAM_CHECKER_BOT_TOKEN
TELEGRAM_REPORT_BOT_TOKEN=TELEGRAM_REPORT_BOT_TOKEN
VERIFY_TOKEN=VERIFY_TOKEN
BITLY_TOKEN=BITLY_TOKEN
OPENAI_API_KEY=OPEN_API_KEY
TYPESENSE_TOKEN=TYPESENSE_TOKEN
ML_SERVER_TOKEN=ML_SERVER_TOKEN
TELEGRAM_BOT_TOKEN=TELEGRAM_BOT_TOKEN
Expand Down
2 changes: 1 addition & 1 deletion functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "",
"serve": "firebase emulators:start",
"serve": "firebase emulators:start --project checkmate-uat",
"shell": "firebase functions:shell",
"start": "npm run shell",
"predeploy": "npm run build",
Expand Down
2 changes: 1 addition & 1 deletion functions/src/definitions/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ main.use("/api", app)

const apiHandler = onRequest(
{
secrets: ["TELEGRAM_BOT_TOKEN"],
secrets: ["TELEGRAM_CHECKER_BOT_TOKEN"],
},
main
)
Expand Down
4 changes: 2 additions & 2 deletions functions/src/definitions/api/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const CHECKER1_PHONE_NUMBER: string = String(process.env.CHECKER1_PHONE_NUMBER)

app.post("/", async (req, res) => {
const initData = req.body // Assuming you send initData in the body of your requests
const botToken = String(process.env.TELEGRAM_BOT_TOKEN) // Replace with your bot token
const botToken = String(process.env.TELEGRAM_CHECKER_BOT_TOKEN) // Replace with your bot token

// Extract the data from initData (convert from query string format)
const params = new URLSearchParams(initData)
Expand Down Expand Up @@ -99,7 +99,7 @@ main.use("/telegramAuth", app)

const telegramAuthHandler = onRequest(
{
secrets: ["TELEGRAM_BOT_TOKEN"],
secrets: ["TELEGRAM_CHECKER_BOT_TOKEN"],
},
main
)
Expand Down
4 changes: 2 additions & 2 deletions functions/src/definitions/webhookHandlers/telegram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import express from "express"
import TelegramBot from "node-telegram-bot-api"
import { onRequest } from "firebase-functions/v2/https"

const TOKEN = "YOUR_TELEGRAM_BOT_TOKEN"
const TOKEN = String(process.env.TELEGRAM_CHECKER_BOT_TOKEN)
const bot = new TelegramBot(TOKEN)

const app = express()
Expand All @@ -23,7 +23,7 @@ app.post("/", async (req, res) => {

const telegramHandler = onRequest(
{
secrets: ["TELEGRAM_BOT_TOKEN"],
secrets: ["TELEGRAM_CHECKER_BOT_TOKEN"],
},
app
)
Expand Down
9 changes: 3 additions & 6 deletions functions/src/definitions/webhookHandlers/whatsapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { Request, Response } from "express"
const runtimeEnvironment = defineString("ENVIRONMENT")

const webhookPath = process.env.WEBHOOK_PATH
const ingressSetting =
process.env.ENVIRONMENT === "PROD" ? "ALLOW_INTERNAL_AND_GCLB" : "ALLOW_ALL"

if (!admin.apps.length) {
admin.initializeApp()
Expand Down Expand Up @@ -139,11 +141,6 @@ const postHandler = async (req: Request, res: Response) => {
}
}

// Accepts POST requests at /webhook endpoint
// Note: TODO: Will delete after webhook is pointed to new endpoint and everything is stable
app.post("/whatsapp", postHandler)
app.get("/whatsapp", getHandler)

// Accepts POST requests at /{webhookPath} endpoint
app.post(`/${webhookPath}`, postHandler)
app.get(`/${webhookPath}`, getHandler)
Expand All @@ -159,7 +156,7 @@ app.post("/telegram", async (req, res) => {

const webhookHandlerV2 = onRequest(
{
ingressSettings: "ALLOW_INTERNAL_AND_GCLB",
ingressSettings: ingressSetting,
secrets: [
"WHATSAPP_USER_BOT_PHONE_NUMBER_ID",
"WHATSAPP_CHECKERS_BOT_PHONE_NUMBER_ID",
Expand Down
Loading

0 comments on commit 5813625

Please sign in to comment.