Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/add easy way to test webhooks #3610

Merged
Merged
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
10 changes: 9 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ version: "2.1"
services:

emulator:
container_name: backend
build:
context: ./
dockerfile: ./functions/emulator/Dockerfile
Expand All @@ -11,4 +12,11 @@ services:
volumes:
- ./functions:/app/functions
- ./functions/data/emulator:/seed
- ./functions/logs:/app/logs
- ./functions/logs:/app/logs
- ./functions/src/emailNotifications/templates:/templates

simulated-webhook-receiver:
container_name: simulated-webhook-receiver
build:
context: ./packages/simulated-webhook-receiver/
dockerfile: Dockerfile
11 changes: 11 additions & 0 deletions functions/emulator/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#
# Optionally, initial data can be setup by mounting `/seed`.
#
# If you want so not get errors due to email missing templates,
# you also need to mount those. The functions code while in the
# emulator expects them to be in `/templates`.
#
# COMMANDS
# We need some files from the root directory of the project,
# therefore these commands should be ran from the root directory.
Expand All @@ -19,6 +23,9 @@
# RUN WITH SEED DATA
# docker run -v ./functions/data/emulator:/seed -v ./functions:/app/functions -p 4001-4008:4001-4008 -it emulator
#
# RUN WITH EMAIL TEMPLATES
# docker run -v ./functions/src/emailNotifications/templates:/templates -v ./functions:/app/functions -p 4001-4008:4001-4008 -it emulator
#
# EXPORT (while the container is running)
# docker exec -it <conatiner_name> /app/export.js
# docker cp <conatiner_name>:/app/dump ./whatever
Expand Down Expand Up @@ -54,6 +61,7 @@ RUN \
# https://firebase.google.com/docs/emulator-suite/install_and_configure
apt-get -y install openjdk-11-jre-headless && \
# For debugging
apt-get -y install curl && \
apt-get -y install nano && \
apt-get clean

Expand Down Expand Up @@ -88,6 +96,9 @@ RUN mkdir /seed
# These should be the ports specified in firebase.json
EXPOSE 4001 4002 4003 4004 4005 4006 4007 4008

# Used to tell the code we are in the emulator
ENV IS_EMULATED=true

CMD \
./link-logs.js & \
# Do firebase emulators:start --help for details
Expand Down
1 change: 1 addition & 0 deletions functions/src/Integrations/firebase-discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,6 @@ const handleResponse = (res: AxiosResponse) => {
}
const handleErr = (err: AxiosError) => {
console.error('error')
console.log(err)
throw err
}
27 changes: 24 additions & 3 deletions functions/src/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
import { config } from 'firebase-functions'

/* config variables are attached attached directly to firebase using the cli
/* config variables are attached directly to firebase using the cli
$firebase functions:config:set ...
to have additional config passed contact admin who will add it
to have additional config passed contact an admin who will add it
*/
let c = config() as configVars
// If running emulated or without firebase login provide dummy data instead
if (Object.keys(c).length === 0) {
c = { analytics: {}, deployment: {}, integrations: {}, service: null } as any
console.log('config is empty')
if (process.env.IS_EMULATED === 'true') {
console.log('using emulator config')
c = {
analytics: {},
deployment: {
site_url: 'http://localhost:4000',
},
integrations: {
discord_webhook: 'http://simulated-webhook-receiver:30102/discord',
slack_webhook: 'http://simulated-webhook-receiver:30102/slack',
},
service: null,
} as any
} else {
c = {
analytics: {},
deployment: {},
integrations: {},
service: null,
} as any
}
}
// strip additional character escapes (\\n -> \n)
if (c.service?.private_key) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"start:platform:for-emulated-backend": "yarn build:shared && vite --port 4000",
"start:platform-ci": "yarn build:shared && vite --port 3456",
"frontend:for-emulated-backend:watch": "concurrently --kill-others --names themes,components,platform --prefix-colors yellow,cyan,blue,magenta \"yarn start:themes\" \"yarn start:components\" \"yarn start:platform:for-emulated-backend\"",
"backend:emulator:watch": "concurrently --kill-others \"yarn workspace functions watch\" \"docker-compose up --force-recreate --build emulator\"",
"backend:emulator:watch": "concurrently --kill-others --names functions-watcher,docker-compose \"yarn workspace functions watch\" \"docker-compose up --force-recreate --build\"",
"backend:emulator:stop": "docker stop $(docker ps -a -q)",
"build:themes": "yarn workspace oa-themes build",
"build:components": "yarn workspace oa-components build",
Expand Down
31 changes: 31 additions & 0 deletions packages/simulated-webhook-receiver/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
####################################################################
#
# COMMANDS:
# This commands are ran from `packages/simulated-webhook-reciever/`.
#
# BUILD
# docker build -t simulated-webook-receiver .
#
# RUN
# docker run -it simulated-webook-receiver
#
####################################################################

FROM node:20.9.0-bullseye-slim

WORKDIR /app

RUN \
apt-get update && \
# For debugging
apt-get -y install nano && \
apt-get clean

COPY ./package.json ./package.json
RUN yarn install

COPY ./server.js ./server.js

EXPOSE 30102

ENTRYPOINT [ "node", "./server.js" ]
1 change: 1 addition & 0 deletions packages/simulated-webhook-receiver/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# simulated-webhook-receiver
7 changes: 7 additions & 0 deletions packages/simulated-webhook-receiver/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "simulated-webook-receiver",
"packageManager": "[email protected]",
"dependencies": {
"express": "^4.19.2"
}
}
60 changes: 60 additions & 0 deletions packages/simulated-webhook-receiver/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*global require, process, console*/

const express = require('express')

const app = express()

console.log('Starting server...')

app.use(express.json())

app.get('/health', (_, response) => {
response.status(200).json({ status: 'success' })
})

const requests = []

app.get('/requests', (_, response) => {
console.log('Returning requests...')
response.status(200).json({ requests: requests })
})

app.get('*', (request, response) => handleRequest(request, response, 'GET'))
app.put('*', (request, response) => handleRequest(request, response, 'PUT'))
app.post('*', (request, response) => handleRequest(request, response, 'POST'))
app.delete('*', (request, response) =>
handleRequest(request, response, 'DELETE'),
)

function handleRequest(request, response, method) {
requests.push({
method: method,
url: request.url,
body: request.body,
})
console.log('=============================')
console.log('Request received!')
console.log('Details:')
console.log('URL: ' + request.originalUrl)
console.log('method: ' + method)
console.log('params: ' + JSON.stringify(request.params))
console.log('body: ' + JSON.stringify(request.body))
console.log('=============================')

response.status(200).json({ status: 'success' })
}

const port = 30102

app.listen(port, () => {
console.log('Server running on port: ' + port)
})

process.on('SIGINT', () => {
console.log('Received termination signal...')
process.exit()
})

process.on('exit', () => {
console.log('Exiting... bye-bye!')
})
35 changes: 10 additions & 25 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5464,13 +5464,6 @@ __metadata:
languageName: node
linkType: hard

"@polka/url@npm:^1.0.0-next.24":
version: 1.0.0-next.25
resolution: "@polka/url@npm:1.0.0-next.25"
checksum: 4ab1d7a37163139c0e7bfc9d1e3f6a2a0db91a78b9f0a21f571d6aec2cdaeaacced744d47886c117aa7579aa5694b303fe3e0bd1922bb9cb3ce6bf7c2dc09801
languageName: node
linkType: hard

"@popperjs/core@npm:^2.11.8":
version: 2.11.8
resolution: "@popperjs/core@npm:2.11.8"
Expand Down Expand Up @@ -16152,7 +16145,7 @@ __metadata:
languageName: node
linkType: hard

"express@npm:^4.16.4, express@npm:^4.17.1, express@npm:^4.17.3":
"express@npm:^4.16.4, express@npm:^4.17.1, express@npm:^4.17.3, express@npm:^4.19.2":
version: 4.19.2
resolution: "express@npm:4.19.2"
dependencies:
Expand Down Expand Up @@ -23193,13 +23186,6 @@ __metadata:
languageName: node
linkType: hard

"mrmime@npm:^2.0.0":
version: 2.0.0
resolution: "mrmime@npm:2.0.0"
checksum: f6fe11ec667c3d96f1ce5fd41184ed491d5f0a5f4045e82446a471ccda5f84c7f7610dff61d378b73d964f73a320bd7f89788f9e6b9403e32cc4be28ba99f569
languageName: node
linkType: hard

"ms@npm:2.0.0":
version: 2.0.0
resolution: "ms@npm:2.0.0"
Expand Down Expand Up @@ -28023,13 +28009,19 @@ __metadata:
languageName: node
linkType: hard

"simulated-webook-receiver@workspace:packages/simulated-webhook-receiver":
version: 0.0.0-use.local
resolution: "simulated-webook-receiver@workspace:packages/simulated-webhook-receiver"
dependencies:
express: ^4.19.2
languageName: unknown
linkType: soft

"sirv@npm:^2.0.3":
version: 2.0.4
resolution: "sirv@npm:2.0.4"
dependencies:
"@polka/url": ^1.0.0-next.24
mrmime: ^2.0.0
totalist: ^3.0.0
semver: ^7.5.3
checksum: 6853384a51d6ee9377dd657e2b257e0e98b29abbfbfa6333e105197f0f100c8c56a4520b47028b04ab1833cf2312526206f38fcd4f891c6df453f40da1a15a57
languageName: node
linkType: hard
Expand Down Expand Up @@ -29520,13 +29512,6 @@ __metadata:
languageName: node
linkType: hard

"totalist@npm:^3.0.0":
version: 3.0.1
resolution: "totalist@npm:3.0.1"
checksum: 5132d562cf88ff93fd710770a92f31dbe67cc19b5c6ccae2efc0da327f0954d211bbfd9456389655d726c624f284b4a23112f56d1da931ca7cfabbe1f45e778a
languageName: node
linkType: hard

"tough-cookie@npm:^4.1.2, tough-cookie@npm:^4.1.3":
version: 4.1.4
resolution: "tough-cookie@npm:4.1.4"
Expand Down
Loading