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

feat: Add webhook urls using env variables #57

Open
wants to merge 1 commit into
base: nocodb-update-may-2024
Choose a base branch
from
Open
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
51 changes: 51 additions & 0 deletions packages/nocodb/src/services/hooks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,42 @@ export class HooksService {
}
}

private replaceEnvVariable(str: string): string {
const regex = /<<(\w+)>>/;
const match = str.match(regex);

if (match) {
const key = `WEBHOOK_URLS_${match[1]}`;
const value = process.env[key];

if (value) {
return value;
} else {
console.warn(`Environment variable ${key} not found`);
NcError.badRequest('Invalid webhook url provided');
}
}
return str;
}

private updateNotificationPayload(
notification: Record<string, any>,
): Record<string, any> {
const path = notification.payload?.path || '';
const webhookUrl = this.replaceEnvVariable(path);

if (path) {
return {
...notification,
payload: {
...notification.payload,
path: webhookUrl,
},
};
}
return notification;
}

async hookList(param: { tableId: string }) {
return await Hook.list({ fk_model_id: param.tableId });
}
Expand All @@ -46,6 +82,11 @@ export class HooksService {
validatePayload('swagger.json#/components/schemas/HookReq', param.hook);

this.validateHookPayload(param.hook.notification);
if (typeof param.hook.notification === 'object') {
param.hook.notification = this.updateNotificationPayload(
param.hook.notification,
);
}

const hook = await Hook.insert({
...param.hook,
Expand Down Expand Up @@ -89,6 +130,11 @@ export class HooksService {
}

this.validateHookPayload(param.hook.notification);
if (typeof param.hook.notification === 'object') {
param.hook.notification = this.updateNotificationPayload(
param.hook.notification,
);
}

const res = await Hook.update(param.hookId, param.hook);

Expand Down Expand Up @@ -121,6 +167,11 @@ export class HooksService {
hook,
payload: { data, user },
} = param.hookTest;

if (typeof hook.notification === 'object') {
hook.notification = this.updateNotificationPayload(hook.notification);
}

try {
await invokeWebhook(
new Hook(hook),
Expand Down