diff --git a/README.md b/README.md index ae824a7b..555d9e2f 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..c7e2f7bf --- /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"), + payload: await request.text(), + }); + + return { + status: 200, + body: "ok", + }; +} diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 00000000..c0ad3754 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,40 @@ +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/index.js b/index.js index 4691aae5..a64bef0c 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,12 @@ 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 +20,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); +} 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 755d6095..b0588d32 100644 --- a/package.json +++ b/package.json @@ -19,12 +19,14 @@ "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", "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..f429a0fb --- /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" + } +}