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

feat: createNodeMiddleware() #509

Merged
merged 21 commits into from
Mar 30, 2021
Merged

feat: createNodeMiddleware() #509

merged 21 commits into from
Mar 30, 2021

Conversation

gr2m
Copy link
Contributor

@gr2m gr2m commented Mar 26, 2021

fixes #507. fixes #510
Part of octokit/octokit.js#15

TODOs

  • implement log option: createNodeMiddleware(webhooks, { log })

View rendered README.md
View rendered src/middleware-legacy/README.md

@gr2m gr2m added the Type: Feature New feature or request label Mar 26, 2021
@gr2m gr2m self-assigned this Mar 26, 2021
@gr2m gr2m force-pushed the 507/createNodeMiddleware branch from f87056d to a42b395 Compare March 26, 2021 23:49
src/middleware/node/index.ts Outdated Show resolved Hide resolved
Copy link
Member

@G-Rath G-Rath left a comment

Choose a reason for hiding this comment

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

Have left a few nity comments - the test ones are a bit more nity (a result of being a core maintainer of eslint-plugin-jest 😅)

}
);

expect(response.status).toEqual(200);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
expect(response.status).toEqual(200);
expect(response.status).toBe(200);

nit: prefer using toBe for literal matchers (or otherwise toStrictEqual)/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

could you elaborate, I'd like to better understand your point. I use .equalTo for comparing simple types such as numbers. I don't think there is a difference to .toStrictEqual() when comparing numbers?

test/integration/node-middleware.test.ts Outdated Show resolved Hide resolved
test/integration/node-middleware.test.ts Outdated Show resolved Hide resolved
});

webhooks.on("push", (event) => {
expect(event.id).toBe("123e4567-e89b-12d3-a456-426655440000");
Copy link
Member

Choose a reason for hiding this comment

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

because this is async, ideally you should use the optional done callback or (preferably) create a promise that gets resolved in this event, which you can await at the end of the test body.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if I follow you here. The event handler is guaranteed to be executed before sending a response, as long as it doesn't take more than 9s, so the code should be executed before the await fetch below resolves

test/integration/node-middleware.test.ts Outdated Show resolved Hide resolved
test/integration/node-middleware.test.ts Show resolved Hide resolved
test/integration/node-middleware.test.ts Show resolved Hide resolved
src/middleware/node/middleware.ts Outdated Show resolved Hide resolved
@gr2m gr2m force-pushed the 507/createNodeMiddleware branch from f31c384 to f000384 Compare March 29, 2021 23:06
@gr2m gr2m changed the title 🚧 feat: createNodeMiddleware() feat: createNodeMiddleware() Mar 29, 2021
@gr2m

This comment has been minimized.

@gr2m gr2m changed the title feat: createNodeMiddleware() 🚧 feat: createNodeMiddleware() Mar 29, 2021
@gr2m

This comment has been minimized.

@gr2m gr2m changed the title 🚧 feat: createNodeMiddleware() feat: createNodeMiddleware() Mar 29, 2021
@gr2m
Copy link
Contributor Author

gr2m commented Mar 29, 2021

@octokit/maintainers this is good to go now

Ignore white space when reviewing changes: https://github.com/octokit/webhooks.js/pull/509/files?w=1


const WEBHOOK_HEADERS = [
"x-github-event",
"x-hub-signature-256",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I intentionally left out the x-hub-signature header here, we want to nudge users to use best practices. GHES support is good enough for the new header

Copy link
Member

@wolfy1339 wolfy1339 left a comment

Choose a reason for hiding this comment

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

Nothing stands out to me.

There were 2 changes that weren't strictly related to this PR

Comment on lines -70 to +93
console.error(
const log = createLogger(options.log);
log.warn(
Copy link
Member

Choose a reason for hiding this comment

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

This isn't strictly related to this PR, but I get the changes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah sorry for that, you are totally right, I've been lazy creating separate PRs for that. Same with the formatting changes in the README, I've to show octokit on Wednesday, I'm in a bit of a rush

Comment on lines -5 to +19
const spy = jest.spyOn(console, "error");
createWebhooksApi({ secret: "foo" });
expect(spy).toBeCalledWith(
const warn = jest.fn();

createWebhooksApi({
secret: "foo",
log: {
debug: () => {},
info: () => {},
warn: warn,
error: () => {},
},
});
expect(warn).toBeCalledWith(
"[@octokit/webhooks] `createWebhooksApi()` is deprecated and will be removed in a future release of `@octokit/webhooks`, please use the `Webhooks` class instead"
);
spy.mockClear();
warn.mockClear();
Copy link
Member

Choose a reason for hiding this comment

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

Not strictly related to this PR.

@gr2m
Copy link
Contributor Author

gr2m commented Mar 30, 2021

I have to go ahead and merge this, but please review and let me know if I missed something, I'll fix it in a follow up PR.

I'll also start a beta release PR for v9 that I can use for @octokit/app and octokit

@gr2m gr2m merged commit 8292596 into master Mar 30, 2021
@gr2m gr2m deleted the 507/createNodeMiddleware branch March 30, 2021 11:39
@github-actions
Copy link
Contributor

🎉 This PR is included in version 8.9.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@honnix
Copy link
Contributor

honnix commented Mar 31, 2021

Thanks for making the change!

With this change, what is the correct way to set middleware to express app?

The deprecated way as following still works, that's great.

const { Webhooks, createNodeMiddleware } = require("@octokit/webhooks");
const webhooks = new Webhooks({
  secret: "mysecret",
  path: "/webhooks",
});

webhooks.onAny(({ id, name, payload }) => {
  console.log(name, "event received");
});

const express = require("express");
const app = express();

app.use(webhooks.middleware);

app.get("/", function (req, res) {
  res.send("hello world");
});

app.listen(3000);

However I tried the following, but it doesn't work well. It seems the middleware hijacks all routes and I got

$ curl localhost:3000/
{"error":"Required headers missing: x-github-event, x-hub-signature-256, x-github-delivery"}% 
const { Webhooks, createNodeMiddleware } = require("@octokit/webhooks");
const webhooks = new Webhooks({
  secret: "mysecret",
});

webhooks.onAny(({ id, name, payload }) => {
  console.log(name, "event received");
});

const express = require("express");
const app = express();

app.use(createNodeMiddleware(webhooks, { path: "/webhooks" }));

app.get("/", function (req, res) {
  res.send("hello world");
});

app.listen(3000);

I tried to do app.use after setting up routes then it works. Is this intended?

@honnix
Copy link
Contributor

honnix commented Mar 31, 2021

I guess the new middleware stops forwarding to next route? We put the middleware before other routes because webhooks URL is much more frequently hit. If this is intended change we can live with it.

honnix added a commit to honnix/webhooks.js that referenced this pull request Apr 15, 2021
As I explained [here](octokit#509 (comment)), it is now required to always install this middleware to the end of the chain, otherwise it rejects all requests it cannot handle.
@gr2m
Copy link
Contributor Author

gr2m commented Apr 17, 2021

I guess the new middleware stops forwarding to next route

that was an unintended change, it slipped due to lack of tests, sorry for that! And thanks for the PR 👍🏼

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Document log option Replace webhooks.middleware with createNodeMiddleware()
5 participants