-
Notifications
You must be signed in to change notification settings - Fork 83
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
Conversation
f87056d
to
a42b395
Compare
There was a problem hiding this 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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expect(response.status).toEqual(200); | |
expect(response.status).toBe(200); |
nit: prefer using toBe
for literal matchers (or otherwise toStrictEqual
)/
There was a problem hiding this comment.
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?
}); | ||
|
||
webhooks.on("push", (event) => { | ||
expect(event.id).toBe("123e4567-e89b-12d3-a456-426655440000"); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
aa5f22b
to
3e62596
Compare
… for `new Webhooks({ path })` and `webhooks.middleware`
…te, express is taking care of routing
f31c384
to
f000384
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@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", |
There was a problem hiding this comment.
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
There was a problem hiding this 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
console.error( | ||
const log = createLogger(options.log); | ||
log.warn( |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
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(); |
There was a problem hiding this comment.
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.
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 |
🎉 This PR is included in version 8.9.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
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 |
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. |
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.
that was an unintended change, it slipped due to lack of tests, sorry for that! And thanks for the PR 👍🏼 |
fixes #507. fixes #510
Part of octokit/octokit.js#15
TODOs
log
option:createNodeMiddleware(webhooks, { log })
View rendered README.md
View rendered src/middleware-legacy/README.md