fix payload empty error (#1612)

This commit is contained in:
Sarthak Agarwal 2025-05-29 20:04:57 +05:30 committed by GitHub
parent 4fb9e84e41
commit 8e7af6656f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 36 additions and 38 deletions

View file

@ -391,9 +391,8 @@ githubApp.webhooks.on("installation_repositories", async ({ payload }) => {
export const ghAppPathPrefix: string = "/cfapi/github"
export const ghAppMiddleware = createNodeMiddleware(githubApp.webhooks, {
path: `${ghAppPathPrefix}/webhooks`,
})
// Path is already prefixed with /cfapi/github in the appExpress.postAsync handler
export const ghAppMiddleware = createNodeMiddleware(githubApp.webhooks)
const deleteBranchIfExists = async (installationOctokit, payload, branchName) => {
try {

View file

@ -45,44 +45,43 @@ appExpress.use(logRequestDetails)
// MUST be mounted before express.json() middleware
console.log(`Mounting GitHub webhook middleware at path: ${ghAppPathPrefix}`)
appExpress.post(
`${ghAppPathPrefix}/webhooks`,
express.raw({ type: "application/json", limit: "50mb" }),
(req, res) => {
try {
// Log important headers for debugging
const contentLength = req.headers["content-length"]
const eventType = req.headers["x-github-event"]
const deliveryId = req.headers["x-github-delivery"]
//Caution/Note: DO NOT use express.raw() or express.json() before ghAppMiddleware for this route. Let ghAppMiddleware handle the raw stream directly.
console.log(
`Processing GitHub webhook: ${eventType} (${deliveryId}), Content-Length: ${contentLength}`,
appExpress.postAsync(`${ghAppPathPrefix}/webhooks`, async (req, res, next) => {
// Use postAsync, handler is async
try {
const contentLength = req.headers["content-length"]
const eventType = req.headers["x-github-event"]
const deliveryId = req.headers["x-github-delivery"]
console.log(
`Processing GitHub webhook: <span class="math-inline">\{eventType\} \(</span>{deliveryId}), Content-Length: ${contentLength}`,
)
// AWAIT Octokit's middleware. It will handle the request and response.
await ghAppMiddleware(req, res, next) // Pass 'next' if ghAppMiddleware is designed to call it on error
// After await, ghAppMiddleware should have handled the response if the event was processed.
// If it didn't (e.g., an error it didn't send a response for, or path not matched by its internal logic),
// then res.headersSent would be false.
if (!res.headersSent) {
console.warn(
`ghAppMiddleware for <span class="math-inline">\{eventType\} \(</span>{deliveryId}) did not send a response. Sending 404.`,
)
// Let Octokit middleware handle the request with original stream intact
const willHandle = ghAppMiddleware(req, res)
if (willHandle) {
console.log(`GitHub App middleware handled the request for ${eventType} (${deliveryId})`)
} else if (!res.headersSent) {
// Only send response if middleware hasn't already responded
console.log(
`GitHub App middleware did not handle the request for ${eventType} (${deliveryId})`,
)
res.status(400).send("Webhook not processed by GitHub App middleware")
}
} catch (error) {
console.error("Error in GitHub webhook handling:", error)
Sentry.captureException(error)
// Only send response if no response has been sent yet
if (!res.headersSent) {
// Always return 200 for webhook errors to prevent GitHub retries
res.status(200).send("Webhook received, but processing encountered errors")
}
res
.status(404)
.send("Webhook event not processed by GitHub App or path mismatch within middleware.")
}
},
)
} catch (error) {
console.error(
`Error in GitHub webhook handling for <span class="math-inline">\{req\.headers\['x\-github\-event'\]\} \(</span>{req.headers['x-github-delivery']}):`,
error,
)
Sentry.captureException(error)
if (!res.headersSent) {
res.status(200).send("Webhook received, but processing encountered errors.")
}
}
})
// Mount Stripe webhook handler (must be before body parsers)
appExpress.post(