From 86765d6ebf0b41d53a70c1afb460f994fd0b6f0b Mon Sep 17 00:00:00 2001 From: Chris Campbell <808531+ctcampbell@users.noreply.github.com> Date: Thu, 9 Nov 2023 10:07:44 +0000 Subject: [PATCH 1/5] Add Azure Functions v4 support --- README.md | 26 +++++++++++++++++++++++++- azure-function-v4.js | 21 +++++++++++++++++++++ index.js | 14 +++++++++++++- 3 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 azure-function-v4.js diff --git a/README.md b/README.md index ae824a7b..71986ab3 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,31 @@ module.exports = (app) => { }; ``` -Then create a folder with `function.json` and `index.js`, e.g. +### Azure Functions v4 + +In your Azure function file: + +```js +// src/functions/probot.js +const { app } = require("@azure/functions"); +const { + createAzureFunctionV4, + createProbot, +} = require("@probot/adapter-azure-functions"); +const probotapp = require("../app"); + +app.http('probot', { + methods: ['POST'], + authLevel: 'anonymous', + handler: createAzureFunctionV4(probotapp, { + probot: createProbot(), + }) +}); +``` + +### Azure Functions v3 + +Create a folder with `function.json` and `index.js`, e.g. ```js // ProbotFunction/function.json diff --git a/azure-function-v4.js b/azure-function-v4.js new file mode 100644 index 00000000..1eed90ac --- /dev/null +++ b/azure-function-v4.js @@ -0,0 +1,21 @@ +module.exports = azureFunctionV4; + +/** + * @param {import('probot').Probot} probot + * @param {import('@azure/functions').HttpRequest} request + * @param {import('@azure/functions').InvocationContext} context + * @returns {Promise} + */ +async function azureFunctionV4(probot, request, context) { + await probot.webhooks.verifyAndReceive({ + id: request.headers.get("X-GitHub-Delivery"), + name: request.headers.get("X-GitHub-Event"), + signature: request.headers.get("X-Hub-Signature-256") || request.headers.get("X-Hub-Signature"), + payload: await request.text(), + }); + + return { + status: 200, + body: "ok" + } +}; \ No newline at end of file diff --git a/index.js b/index.js index 4691aae5..53fda258 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,8 @@ const ProbotExports = require("probot"); const azureFunction = require("./azure-function"); +const azureFunctionV4 = require("./azure-function-v4"); -module.exports = { ...ProbotExports, createAzureFunction }; +module.exports = { ...ProbotExports, createAzureFunction, createAzureFunctionV4 }; /** * @@ -15,3 +16,14 @@ function createAzureFunction(app, { probot }) { return azureFunction.bind(null, probot); } + +/** + * @param {import('probot').ApplicationFunction} app + * @param { { probot: import('probot').Probot } } options + */ +function createAzureFunctionV4(app, { probot }) { + // load app once outside of the function to prevent double + // event handlers in case of container reuse + probot.load(app); + return azureFunctionV4.bind(null, probot); +} From c7e9c73331e67d3a5bb7a5e30b2495f67022a7a0 Mon Sep 17 00:00:00 2001 From: Chris Campbell <808531+ctcampbell@users.noreply.github.com> Date: Thu, 9 Nov 2023 11:56:16 +0000 Subject: [PATCH 2/5] Add first type file --- index.d.ts | 1 + package.json | 1 + 2 files changed, 2 insertions(+) create mode 100644 index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 00000000..094aeb2c --- /dev/null +++ b/index.d.ts @@ -0,0 +1 @@ +declare module "@probot/adapter-azure-functions" { } \ No newline at end of file diff --git a/package.json b/package.json index 755d6095..c71e8106 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "author": "Gregor Martynus (https://github.com/gr2m)", "license": "ISC", "repository": "github:probot/adapter-azure-functions", + "types": "index.d.ts", "devDependencies": { "@azure/functions": "^3.2.0", "@types/jest": "^29.0.0", From 441fa1590da8156161a07efcf145499dea65b8db Mon Sep 17 00:00:00 2001 From: Chris Campbell <808531+ctcampbell@users.noreply.github.com> Date: Thu, 9 Nov 2023 12:59:48 +0000 Subject: [PATCH 3/5] Add TS types --- index.d.ts | 41 ++++++++++++++++++++++++++++++++++++++++- package-lock.json | 16 +++++++++++++++- package.json | 3 ++- tsconfig.json | 19 +++++++++++++++++++ 4 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 tsconfig.json diff --git a/index.d.ts b/index.d.ts index 094aeb2c..c0ad3754 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1 +1,40 @@ -declare module "@probot/adapter-azure-functions" { } \ No newline at end of file +declare module "@probot/adapter-azure-functions" { + import ProbotExports = require("probot"); + + /** + * @param {import('probot').ApplicationFunction} app + * @param { { probot: import('probot').Probot } } options + */ + function createAzureFunction(app: ProbotExports.ApplicationFunction, { probot }: { + probot: ProbotExports.Probot; + }): any; + + /** + * @param {import('probot').ApplicationFunction} app + * @param { { probot: import('probot').Probot } } options + */ + function createAzureFunctionV4(app: ProbotExports.ApplicationFunction, { probot }: { + probot: ProbotExports.Probot; + }): any; + + const _exports: { + createAzureFunction: typeof createAzureFunction; + createAzureFunctionV4: typeof createAzureFunctionV4; + Context: typeof ProbotExports.Context; + ProbotOctokit: typeof import("@octokit/core").Octokit & import("@octokit/core/dist-types/types").Constructor<{ + retry: { + retryRequest: (error: import("@octokit/request-error").RequestError, retries: number, retryAfter: number) => import("@octokit/request-error").RequestError; + }; + } & { + paginate: import("@octokit/plugin-paginate-rest").PaginateInterface; + } & import("@octokit/plugin-rest-endpoint-methods/dist-types/generated/method-types").RestEndpointMethods & import("@octokit/plugin-rest-endpoint-methods/dist-types/types").Api & import("@probot/octokit-plugin-config/dist-types/types").API>; + run: typeof ProbotExports.run; + Probot: typeof ProbotExports.Probot; + Server: typeof ProbotExports.Server; + createNodeMiddleware: typeof ProbotExports.createNodeMiddleware; + createProbot: typeof ProbotExports.createProbot; + }; + + export = _exports; +} +//# sourceMappingURL=index.d.ts.map \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 7f1fc7ca..431f0b44 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,7 +16,8 @@ "@types/jest": "^29.0.0", "jest": "^29.0.2", "nock": "^13.2.9", - "prettier": "^2.7.1" + "prettier": "^2.7.1", + "typescript": "^5.2.2" } }, "node_modules/@ampproject/remapping": { @@ -6137,6 +6138,19 @@ "node": ">= 0.6" } }, + "node_modules/typescript": { + "version": "5.2.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.2.2.tgz", + "integrity": "sha512-mI4WrpHsbCIcwT9cF4FZvr80QUeKvsUsUvKDoR+X/7XHQH98xYD8YHZg7ANtz2GtZt/CBq2QJ0thkGJMHfqc1w==", + "dev": true, + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, "node_modules/uglify-js": { "version": "3.17.4", "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.17.4.tgz", diff --git a/package.json b/package.json index c71e8106..b0588d32 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,8 @@ "@types/jest": "^29.0.0", "jest": "^29.0.2", "nock": "^13.2.9", - "prettier": "^2.7.1" + "prettier": "^2.7.1", + "typescript": "^5.2.2" }, "dependencies": { "probot": "^12.2.7" diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 00000000..10fbe72a --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,19 @@ +{ + // Change this to match your project + "include": ["./index.js"], + "compilerOptions": { + // Tells TypeScript to read JS files, as + // normally they are ignored as source files + "allowJs": true, + // Generate d.ts files + "declaration": true, + // This compiler run should + // only output d.ts files + "emitDeclarationOnly": true, + // go to js file when using IDE functions like + // "Go to Definition" in VSCode + "declarationMap": true, + "esModuleInterop": true, + "outFile": "index.d.ts" + } +} \ No newline at end of file From 81c6a993d30f86da3a6a87956b161e68b23f64e1 Mon Sep 17 00:00:00 2001 From: Chris Campbell <808531+ctcampbell@users.noreply.github.com> Date: Mon, 11 Dec 2023 10:26:24 +0000 Subject: [PATCH 4/5] Remove legacy signature header --- azure-function-v4.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azure-function-v4.js b/azure-function-v4.js index 1eed90ac..3bab43a5 100644 --- a/azure-function-v4.js +++ b/azure-function-v4.js @@ -10,7 +10,7 @@ async function azureFunctionV4(probot, request, context) { await probot.webhooks.verifyAndReceive({ id: request.headers.get("X-GitHub-Delivery"), name: request.headers.get("X-GitHub-Event"), - signature: request.headers.get("X-Hub-Signature-256") || request.headers.get("X-Hub-Signature"), + signature: request.headers.get("X-Hub-Signature-256"), payload: await request.text(), }); From 2d4e0e12bb2fef713466c49732b3db1de7e81bd1 Mon Sep 17 00:00:00 2001 From: Gregor Martynus <39992+gr2m@users.noreply.github.com> Date: Mon, 11 Dec 2023 15:20:17 -0800 Subject: [PATCH 5/5] style: prettier --- README.md | 8 ++++---- azure-function-v4.js | 6 +++--- index.js | 6 +++++- tsconfig.json | 2 +- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 71986ab3..555d9e2f 100644 --- a/README.md +++ b/README.md @@ -31,12 +31,12 @@ const { } = require("@probot/adapter-azure-functions"); const probotapp = require("../app"); -app.http('probot', { - methods: ['POST'], - authLevel: 'anonymous', +app.http("probot", { + methods: ["POST"], + authLevel: "anonymous", handler: createAzureFunctionV4(probotapp, { probot: createProbot(), - }) + }), }); ``` diff --git a/azure-function-v4.js b/azure-function-v4.js index 3bab43a5..c7e2f7bf 100644 --- a/azure-function-v4.js +++ b/azure-function-v4.js @@ -16,6 +16,6 @@ async function azureFunctionV4(probot, request, context) { return { status: 200, - body: "ok" - } -}; \ No newline at end of file + body: "ok", + }; +} diff --git a/index.js b/index.js index 53fda258..a64bef0c 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,11 @@ const ProbotExports = require("probot"); const azureFunction = require("./azure-function"); const azureFunctionV4 = require("./azure-function-v4"); -module.exports = { ...ProbotExports, createAzureFunction, createAzureFunctionV4 }; +module.exports = { + ...ProbotExports, + createAzureFunction, + createAzureFunctionV4, +}; /** * diff --git a/tsconfig.json b/tsconfig.json index 10fbe72a..f429a0fb 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -16,4 +16,4 @@ "esModuleInterop": true, "outFile": "index.d.ts" } -} \ No newline at end of file +}