diff --git a/src/processor.ts b/src/processor.ts index eacf3c1..418ba0e 100644 --- a/src/processor.ts +++ b/src/processor.ts @@ -71,14 +71,24 @@ export async function processPullRequest( (config.overwriteTitle || weakTitle(pr.title, pr.headBranch)); const updateBody = config.updateBody && (config.overwriteBody || emptyBody(pr.body)); - if (!updateTitle && !updateBody && !config.comment) return; + console.info( + `PR #${pr.number} in ${pr.owner}/${pr.repo}: title=${updateTitle}, body=${updateBody}, comment=${config.comment}.`, + ); + if (!updateTitle && !updateBody && !config.comment) { + console.info(`PR #${pr.number}: no fields are eligible; skipping.`); + return; + } const client = new GitHubClient({ appId: config.appId, privateKey: config.privateKey, installationId: payload.installation.id, }); + console.info(`PR #${pr.number}: collecting commits and changed files.`); const context = collectContext(await client.collectPullRequest(pr), config); + console.info( + `PR #${pr.number}: collected ${context.commits.length} commits and ${context.files.length} included files.`, + ); let generated = fallbackNote(context); if (config.apiKey) { try { @@ -91,10 +101,16 @@ export async function processPullRequest( } } const note = finalizeNote(generated, context); - if (updateTitle || updateBody) + if (updateTitle || updateBody) { + console.info(`PR #${pr.number}: updating pull request fields.`); await client.updatePullRequest(pr, { ...(updateTitle ? { title: note.title } : {}), ...(updateBody ? { body: mergeBody(note) } : {}), }); - if (config.comment) await client.upsertComment(pr, renderComment(note)); + console.info(`PR #${pr.number}: pull request fields updated.`); + } + if (config.comment) { + console.info(`PR #${pr.number}: writing managed comment.`); + await client.upsertComment(pr, renderComment(note)); + } } diff --git a/src/server.ts b/src/server.ts index 9f0d798..e9de061 100644 --- a/src/server.ts +++ b/src/server.ts @@ -49,14 +49,25 @@ const server = createServer(async (request, response) => { const event = request.headers["x-github-event"]; const delivery = request.headers["x-github-delivery"]; + let payload: PullRequestOpenedPayload | undefined; + try { + payload = JSON.parse(body.toString("utf8")) as PullRequestOpenedPayload; + } catch (error) { + console.error( + `Webhook ${delivery ?? "unknown"} contains invalid JSON.`, + error, + ); + response.writeHead(400).end("Invalid JSON"); + return; + } + console.info( + `Received ${String(event)} webhook ${delivery ?? "unknown"} (action: ${payload.action ?? "none"}).`, + ); response .writeHead(202, { "Content-Type": "application/json" }) .end(JSON.stringify({ accepted: true, delivery })); if (event !== "pull_request") return; try { - const payload = JSON.parse( - body.toString("utf8"), - ) as PullRequestOpenedPayload; await processPullRequest(payload, config); console.info(`Processed webhook ${delivery ?? "unknown"}.`); } catch (error) {