|
| 1 | +import { existsSync, readFileSync } from "node:fs"; |
| 2 | + |
| 3 | +import * as core from "@actions/core"; |
| 4 | +import * as github from "@actions/github"; |
| 5 | + |
| 6 | +import { COMMENT_MARKER, GITHUB_ACTIONS_BOT_ID } from "./constants"; |
| 7 | + |
| 8 | +interface LinkValidationError { |
| 9 | + docsPath: string; |
| 10 | + link: string; |
| 11 | + position: { line: number; column: number } | null; |
| 12 | + message: string; |
| 13 | + documentationUrl: string; |
| 14 | +} |
| 15 | + |
| 16 | +interface LinkValidationReport { |
| 17 | + errorCount: number; |
| 18 | + errorFileCount: number; |
| 19 | + errors: LinkValidationError[]; |
| 20 | +} |
| 21 | + |
| 22 | +async function findExistingComment( |
| 23 | + octokit: ReturnType<typeof github.getOctokit>, |
| 24 | + owner: string, |
| 25 | + repo: string, |
| 26 | + pullRequestNumber: number, |
| 27 | +) { |
| 28 | + const { data: comments } = await octokit.rest.issues.listComments({ |
| 29 | + owner, |
| 30 | + repo, |
| 31 | + issue_number: pullRequestNumber, |
| 32 | + per_page: 100, |
| 33 | + }); |
| 34 | + |
| 35 | + return comments.find( |
| 36 | + (c) => |
| 37 | + c.user?.id === GITHUB_ACTIONS_BOT_ID && c.body?.includes(COMMENT_MARKER), |
| 38 | + ); |
| 39 | +} |
| 40 | + |
| 41 | +async function run(): Promise<void> { |
| 42 | + try { |
| 43 | + if (!process.env.GITHUB_TOKEN) { |
| 44 | + core.setFailed("Could not find GITHUB_TOKEN in env"); |
| 45 | + process.exit(); |
| 46 | + } |
| 47 | + |
| 48 | + const octokit = github.getOctokit(process.env.GITHUB_TOKEN); |
| 49 | + const { owner, repo } = github.context.repo; |
| 50 | + const payload = github.context.payload; |
| 51 | + const pullRequestNumber = payload.pull_request?.number; |
| 52 | + |
| 53 | + if (!pullRequestNumber) { |
| 54 | + core.setFailed("Could not find pull request number"); |
| 55 | + process.exit(); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + const reportPath = |
| 60 | + process.env.REPORT_PATH ?? ".starlight-links-validator/errors.json"; |
| 61 | + |
| 62 | + // No report means no broken links — clean up any existing comment |
| 63 | + if (!existsSync(reportPath)) { |
| 64 | + const existing = await findExistingComment( |
| 65 | + octokit, |
| 66 | + owner, |
| 67 | + repo, |
| 68 | + pullRequestNumber, |
| 69 | + ); |
| 70 | + |
| 71 | + if (existing) { |
| 72 | + core.info( |
| 73 | + `No broken links found. Removing existing comment ${existing.id}`, |
| 74 | + ); |
| 75 | + await octokit.rest.issues.deleteComment({ |
| 76 | + owner, |
| 77 | + repo, |
| 78 | + comment_id: existing.id, |
| 79 | + }); |
| 80 | + } else { |
| 81 | + core.info("No broken links found."); |
| 82 | + } |
| 83 | + |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + let report: LinkValidationReport; |
| 88 | + try { |
| 89 | + report = JSON.parse(readFileSync(reportPath, "utf8")); |
| 90 | + } catch { |
| 91 | + core.setFailed(`Could not read report at ${reportPath}`); |
| 92 | + process.exit(); |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + // Build the comment body |
| 97 | + const rows = report.errors.map((error) => { |
| 98 | + const position = error.position |
| 99 | + ? `\`${error.position.line}:${error.position.column}\`` |
| 100 | + : "-"; |
| 101 | + const link = decodeURIComponent(error.link); |
| 102 | + return `| \`${error.docsPath}\` | \`${link}\` | ${position} | ${error.message} |`; |
| 103 | + }); |
| 104 | + |
| 105 | + const comment = [ |
| 106 | + `## ${COMMENT_MARKER}`, |
| 107 | + "", |
| 108 | + `Found **${report.errorCount}** broken link(s) across **${report.errorFileCount}** file(s).`, |
| 109 | + "", |
| 110 | + "| File | Link | Position | Error |", |
| 111 | + "| --- | --- | :---: | --- |", |
| 112 | + ...rows, |
| 113 | + ].join("\n"); |
| 114 | + |
| 115 | + // Find existing comment |
| 116 | + const existingComment = await findExistingComment( |
| 117 | + octokit, |
| 118 | + owner, |
| 119 | + repo, |
| 120 | + pullRequestNumber, |
| 121 | + ); |
| 122 | + |
| 123 | + if (existingComment) { |
| 124 | + core.info(`Updating existing comment ${existingComment.id}`); |
| 125 | + await octokit.rest.issues.updateComment({ |
| 126 | + owner, |
| 127 | + repo, |
| 128 | + comment_id: existingComment.id, |
| 129 | + body: comment, |
| 130 | + }); |
| 131 | + } else { |
| 132 | + core.info("Creating new comment"); |
| 133 | + await octokit.rest.issues.createComment({ |
| 134 | + owner, |
| 135 | + repo, |
| 136 | + issue_number: pullRequestNumber, |
| 137 | + body: comment, |
| 138 | + }); |
| 139 | + } |
| 140 | + |
| 141 | + core.setFailed( |
| 142 | + `Found ${report.errorCount} broken link(s) across ${report.errorFileCount} file(s).`, |
| 143 | + ); |
| 144 | + } catch (error) { |
| 145 | + if (error instanceof Error) { |
| 146 | + core.setFailed(error.message); |
| 147 | + } |
| 148 | + process.exit(); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +run(); |
0 commit comments