diff --git a/src/github/handlers/push-event.ts b/src/github/handlers/push-event.ts index f549f54..f3c4cf1 100644 --- a/src/github/handlers/push-event.ts +++ b/src/github/handlers/push-event.ts @@ -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[] | 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; @@ -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,