Skip to content

Commit

Permalink
chore(gha): more broken-link-checker cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ndom91 committed May 4, 2024
1 parent b4c740a commit bd5168b
Showing 1 changed file with 43 additions and 30 deletions.
73 changes: 43 additions & 30 deletions .github/broken-link-checker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> => {
try {
const { context, getOctokit } = github
const octokit = getOctokit(process.env.GITHUB_TOKEN!)
Expand All @@ -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({
Expand All @@ -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,
Expand All @@ -123,6 +129,7 @@ const postComment = async (outputMd: string) => {
})
return data.html_url
}
return ""
} catch (error) {
setFailed("Error commenting: " + error)
return ""
Expand Down Expand Up @@ -212,23 +219,29 @@ async function brokenLinkChecker(): Promise<void> {
},
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`)
}
},
})
Expand Down

0 comments on commit bd5168b

Please sign in to comment.