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

S48 1126 r d automation of webhook triggered http functions #60

Open
wants to merge 46 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
6d938ed
added steps in the bundler to detect BC webhook functions and generat…
joeward98 Aug 2, 2022
49a3503
added 'publish event' http function, which is automatically bundled w…
joeward98 Aug 2, 2022
e863c10
refactored getWebHookGCPFunctionUrl to be extendible to other webhook…
joeward98 Aug 2, 2022
d479e6f
added autodetection of name o bigcommerce webhook publisher function…
joeward98 Aug 5, 2022
01a0ff8
wrote cloud function for configuring BC webhooks, based on SRBC
joeward98 Aug 5, 2022
5f2477d
added pipeline file
joeward98 Oct 14, 2022
cb8d604
removed pipeline file
joeward98 Oct 14, 2022
804517d
readded pipelines file
joeward98 Oct 14, 2022
6debd16
added bb pipelines dir
joeward98 Oct 14, 2022
4b179d9
updated gitignore
joeward98 Oct 14, 2022
f8726f3
added automatic creation of pubsub topics for webhook functions
joeward98 Oct 14, 2022
32acfbc
added automatic generation webhook config in bundler
joeward98 Oct 14, 2022
9248134
deleted unnecesary files
joeward98 Oct 14, 2022
8acd0dc
updated lockfiles
joeward98 Oct 14, 2022
39444e3
added test entry point, cloudseed config file, test cloud function co…
joeward98 Oct 14, 2022
94e7c7c
Merge branch 'S48-1126-r-d-automation-of-webhook-triggered-http-funct…
joeward98 Oct 14, 2022
c30616a
replaced call to cloudseed with test entry point in pipeline
joeward98 Oct 14, 2022
9a63cac
moved bulid step before lint step to stop errors due to .gen files
joeward98 Oct 14, 2022
6827475
added cdktf.json
joeward98 Oct 14, 2022
772bbf6
amended cdktf.json
joeward98 Oct 14, 2022
d64da45
updated with master
joeward98 Oct 14, 2022
9a87e5a
update lockfile
joeward98 Oct 14, 2022
ec535d3
aded runtime to GcpConfig on test function
joeward98 Oct 14, 2022
a324747
added runtime to gcp config for webhooks function
joeward98 Oct 14, 2022
d7207df
fixed filepath on test script in pipeline
joeward98 Oct 14, 2022
32d57e7
added dist dir to artifacts after build step (for testing purposes
joeward98 Oct 17, 2022
9071421
uninstalled ts-node and refactore pipeline to use 'node' cmd instead
joeward98 Oct 17, 2022
bf940d7
fixed configure hooks script
joeward98 Oct 17, 2022
93fa864
changed webhook scope
joeward98 Oct 17, 2022
6a0a0a6
changed webhook scope
joeward98 Oct 17, 2022
aaf5a33
added second test function
joeward98 Oct 17, 2022
4317808
renamed webhook publisher function
joeward98 Oct 17, 2022
b300578
resolved merge conflict
joeward98 Oct 17, 2022
366bf6e
readded src dir
joeward98 Oct 17, 2022
bc3d5b6
added full scope onto gcp pubsub tpic name and changes scopes on test…
joeward98 Oct 17, 2022
314d4d9
main commit
joeward98 Oct 17, 2022
9f1c5af
removed bb pipelines
joeward98 Oct 17, 2022
385111b
removed unneeded npm packages
joeward98 Oct 17, 2022
cc8de2c
readded files for testing and tweaked pipeline
joeward98 Oct 17, 2022
490eece
new test functions
joeward98 Oct 17, 2022
b94da89
renamed configure webhooks script to mjs
joeward98 Oct 18, 2022
4bd64fc
renamed test-script webhooks script to mjs
joeward98 Oct 18, 2022
461d4bc
fixed mjs files
joeward98 Oct 18, 2022
5a81644
adding webhooks.md
joeward98 Oct 18, 2022
f70be64
updated docs to include webhooks
joeward98 Oct 18, 2022
034d39a
removed BC API
joeward98 Oct 18, 2022
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
57 changes: 54 additions & 3 deletions build/esbuild/bundle.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import ts from "typescript";
import { sync } from "glob";
import { readFileSync, writeFileSync } from "fs";
import { readFileSync, writeFileSync, mkdirSync } from "fs";
import { BuildOptions, buildSync } from "esbuild";
import { join, relative } from "path";
import { BaseConfig } from "../../utils/rootConfig";

const bundle = (dir: string, outDir: string, esbuildOptions?: Partial<BuildOptions>) => {
const files = sync(join(dir, "**/*.ts"));
const bundle = (
dir: string,
outDir: string,
cloudConfig: BaseConfig["cloud"],
esbuildOptions?: Partial<BuildOptions>,
) => {
const files = [...sync(join(dir, "**/*.ts")), ...sync(join("webhooks", "**/*.ts"))];

let runtimeConfig: any = null;
const runtimeConfigs: any[] = [];
Expand All @@ -30,6 +36,7 @@ const bundle = (dir: string, outDir: string, esbuildOptions?: Partial<BuildOptio
});

writeFileSync(join(outDir, "functions.json"), JSON.stringify(runtimeConfigs, null, 2));

runtimeConfigs.forEach(config => {
buildSync({
entryPoints: [config.file],
Expand All @@ -42,6 +49,28 @@ const bundle = (dir: string, outDir: string, esbuildOptions?: Partial<BuildOptio
...esbuildOptions,
});
});

mkdirSync(join(outDir, "webhooks"));

const webhooks = runtimeConfigs.filter(config => config.type === "webhook");

const bigcommerceWebhookScopes = webhooks
.filter(config => config.webhook.type === "bigcommerce")
.map(config => config.webhook.scope)
.filter((scope, index, scopes) => scopes.indexOf(scope) === index);

if (bigcommerceWebhookScopes.length)
writeFileSync(
join(outDir, "webhooks", "bigcommerce-webhooks.json"),
JSON.stringify(
{
destinationUrl: getGCPFunctionUrl(cloudConfig.gcp, "bigcommerce"),
scopes: bigcommerceWebhookScopes,
},
null,
2,
),
);
};
export default bundle;

Expand Down Expand Up @@ -118,3 +147,25 @@ function mapNode(node: ts.Node): any {

return "UNSUPPORTED";
}

function findFunctionFilePath(identifier: string) {
const files = sync(join("webhooks", "**/*.ts"));
let sourceFile = "";
files.some(file => {
const fileContentsBuffer = readFileSync(file);
const [fileFirstLine] = fileContentsBuffer.toString().split(/\r?\n/);
if (fileFirstLine === identifier) {
return (sourceFile = file);
}
});
return sourceFile;
}

function getGCPFunctionUrl(GcpConfig: BaseConfig["cloud"]["gcp"], type: "bigcommerce") {
if (type === "bigcommerce") {
const functionIdentifier = "//BIGCOMMERCE_WEBHOOK_PUBLISHER_FUNCTION//";
return `https://${GcpConfig.region}-${
GcpConfig.project
}.cloudfunctions.net/${generateFunctionName(findFunctionFilePath(functionIdentifier))}`;
}
}
2 changes: 1 addition & 1 deletion build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default (buildOpts: Partial<BuildOpts>): { config: BaseConfig; app: App }
mkdirSync(buildOutDir, { recursive: true });

// Run bundler.
bundle(buildDir, buildOutDir, options.buildConfig.esbuildOptions);
bundle(buildDir, buildOutDir, options.cloud, options.buildConfig.esbuildOptions);

// Generate stacks.
const app = new App({ outdir: options.buildConfig.outDir });
Expand Down
27 changes: 27 additions & 0 deletions docs/webhooks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Functions to be triggered by webhooks

If you want your function to be triggered by a webhook, you can use the `webhook` type in the runtimeConfig. This requires the `webhook` property which is an object defining
- webhook type (eg bigcommerce)
- webhook scope

## Code Sample:

```typescript
import { CloudEventFunction } from "@google-cloud/functions-framework";
import { GcpConfig } from "../types/runtime";

const test: CloudEventFunction = async event => {
console.log("success");
};
export default test;

export const runtimeConfig: GcpConfig = {
runtime: "nodejs14",
cloud: "gcp",
type: "webhook",
webhook: {
type: "bigcommerce",
scope: "store/category/created",
},
};
```
Loading
Loading