Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 19 additions & 3 deletions src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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));
}
}
17 changes: 14 additions & 3 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading