Skip to content

Commit

Permalink
chore: split error body construction
Browse files Browse the repository at this point in the history
  • Loading branch information
gentlementlegen committed Sep 20, 2024
1 parent 8f14418 commit 3f67859
Showing 1 changed file with 41 additions and 27 deletions.
68 changes: 41 additions & 27 deletions src/github/handlers/push-event.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,46 @@
import { GitHubContext } from "../github-context";
import { CONFIG_FULL_PATH, getConfigurationFromRepo } from "../utils/config";
import YAML, { LineCounter, Node, YAMLError } from "yaml";
import { ValueError } from "typebox-validators";

function constructErrorBody(
errors: Iterable<ValueError> | ValueError[] | YAML.YAMLError[],
rawData: string | null,
repository: GitHubContext<"push">["payload"]["repository"],
after: string
) {
const body = [];
if (errors) {
for (const error of errors) {
body.push("> [!CAUTION]\n");
if (error instanceof YAMLError) {
body.push(`> https://github.com/${repository.owner?.login}/${repository.name}/blob/${after}/${CONFIG_FULL_PATH}#L${error.linePos?.[0].line}`);
} else if (rawData) {
const lineCounter = new LineCounter();
const doc = YAML.parseDocument(rawData, { lineCounter });
const path = error.path
.split("/")
.filter((o) => o)
.slice(0, -1);
const node = doc.getIn(path, true) as Node;
const linePosStart = lineCounter.linePos(node.range?.[0] || 0);
body.push(`> https://github.com/${repository.owner?.login}/${repository.name}/blob/${after}/${CONFIG_FULL_PATH}#L${linePosStart.line}`);
}
const message = [];
if (error instanceof YAMLError) {
message.push(error.message);
} else {
message.push(`path: ${error.path}\n`);
message.push(`value: ${error.value}\n`);
message.push(`message: ${error.message}`);
}
body.push(`\n> \`\`\`\n`);
body.push(`> ${message.join("").replaceAll("\n", "\n> ")}`);
body.push(`\n> \`\`\`\n`);
}
}
return body;
}

export default async function handlePushEvent(context: GitHubContext<"push">) {
const { octokit, payload } = context;
Expand All @@ -22,33 +62,7 @@ export default async function handlePushEvent(context: GitHubContext<"push">) {
const body = [];
body.push(`@${payload.sender?.login} Configuration is ${!errors ? "valid" : "invalid"}.\n`);
if (errors) {
for (const error of errors) {
body.push("> [!CAUTION]\n");
if (error instanceof YAMLError) {
body.push(`> https://github.com/${repository.owner.login}/${repository.name}/blob/${after}/${CONFIG_FULL_PATH}#L${error.linePos?.[0].line}`);
} else if (rawData) {
const lineCounter = new LineCounter();
const doc = YAML.parseDocument(rawData, { lineCounter });
const path = error.path
.split("/")
.filter((o) => o)
.slice(0, -1);
const node = doc.getIn(path, true) as Node;
const linePosStart = lineCounter.linePos(node.range?.[0] || 0);
body.push(`> https://github.com/${repository.owner.login}/${repository.name}/blob/${after}/${CONFIG_FULL_PATH}#L${linePosStart.line}`);
}
const message = [];
if (error instanceof YAMLError) {
message.push(error.message);
} else {
message.push(`path: ${error.path}\n`);
message.push(`value: ${error.value}\n`);
message.push(`message: ${error.message}`);
}
body.push(`\n> \`\`\`\n`);
body.push(`> ${message.join("").replaceAll("\n", "\n> ")}`);
body.push(`\n> \`\`\`\n`);
}
body.push(...constructErrorBody(errors, rawData, repository, after));
}
await octokit.rest.repos.createCommitComment({
owner: repository.owner.login,
Expand Down

0 comments on commit 3f67859

Please sign in to comment.