diff --git a/.github/broken-link-checker/src/index.ts b/.github/broken-link-checker/src/index.ts index 87ac4f11c6..32ab7475d8 100644 --- a/.github/broken-link-checker/src/index.ts +++ b/.github/broken-link-checker/src/index.ts @@ -51,31 +51,38 @@ async function updateCheckStatus( const { context, getOctokit } = github const octokit = getOctokit(process.env.GITHUB_TOKEN!) const { owner, repo } = context.repo - const pullRequest = context.payload.pull_request - const sha = pullRequest?.head.sha - - const checkParams = { - owner, - repo, - name: checkName, - head_sha: sha, - status: "completed" as const, - conclusion: "failure" as const, - output: { - title: checkName, - summary: summary, - text: text, - }, - } - try { - await octokit.rest.checks.create(checkParams) - } catch (error) { - setFailed("Failed to create check: " + error) + // Can only update status on 'pull_request' events + if (context.payload.pull_request) { + const pullRequest = context.payload.pull_request + const sha = pullRequest?.head.sha + + const checkParams = { + owner, + repo, + name: checkName, + head_sha: sha, + status: "completed" as const, + conclusion: "failure" as const, + output: { + title: checkName, + summary: summary, + text: text, + }, + } + + try { + await octokit.rest.checks.create(checkParams) + } catch (error) { + setFailed("Failed to create check: " + error) + } } } -const postComment = async (outputMd: string) => { +const postComment = async ( + outputMd: string, + brokenLinkCount: number = 0 +): Promise => { try { const { context, getOctokit } = github const octokit = getOctokit(process.env.GITHUB_TOKEN!) @@ -102,7 +109,6 @@ const postComment = async (outputMd: string) => { repo, prNumber, }) - console.log("botComment", botComment) if (botComment) { console.log("Updating Comment") const { data } = await octokit.rest.issues.updateComment({ @@ -113,7 +119,7 @@ const postComment = async (outputMd: string) => { }) return data.html_url - } else { + } else if (brokenLinkCount > 0) { console.log("Creating Comment") const { data } = await octokit.rest.issues.createComment({ owner, @@ -123,6 +129,7 @@ const postComment = async (outputMd: string) => { }) return data.html_url } + return "" } catch (error) { setFailed("Error commenting: " + error) return "" @@ -212,23 +219,29 @@ async function brokenLinkChecker(): Promise { }, end: async () => { if (output.links.length) { + // DEBUG + // console.debug(output.links) + // Skip links that returned 308 - const links404 = output.links.filter( + const brokenLinksForAttention = output.links.filter( (link) => link.broken && !["HTTP_308"].includes(link.brokenReason) ) const outputMd = generateOutputMd({ errors: output.errors, - links: links404, + links: brokenLinksForAttention, pages: [], sites: [], }) - await postComment(outputMd) + const commentUrl = await postComment( + outputMd, + brokenLinksForAttention.length + ) + + // Update GitHub "check" status + await updateCheckStatus(brokenLinksForAttention.length, commentUrl) - // const commentUrl = await postComment(outputMd); - // NOTE: Do we need this additional check in GH output? - // await updateCheckStatus(output.links.length, commentUrl); - setFailed(`Found broken links`) + brokenLinksForAttention.length && setFailed(`Found broken links`) } }, })