Skip to content

Commit

Permalink
feat: Azure Functions v4 support (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
ctcampbell authored Dec 12, 2023
1 parent 69e86c6 commit 9acbb12
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 4 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions azure-function-v4.js
Original file line number Diff line number Diff line change
@@ -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<import('@azure/functions').HttpResponseInit>}
*/
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",
};
}
40 changes: 40 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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
18 changes: 17 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -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,
};

/**
*
Expand All @@ -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);
}
16 changes: 15 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
19 changes: 19 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"
}
}

0 comments on commit 9acbb12

Please sign in to comment.