Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: Azure Functions v4 support #164

Merged
merged 5 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
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
14 changes: 13 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -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 };

/**
*
Expand All @@ -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);
}
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need a tsconfig file given that this is a JS project? Is index.d.ts generated or did you hand craft it? If we need it, shouldn't it be a jsconfig.json file instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The index.d.ts file was generated, following docs at https://www.typescriptlang.org/docs/handbook/declaration-files/dts-from-js.html, which describe adding tsconfig.json. I did try jsconfig.json but using that creates the following error:
image

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"
}
}
Loading