Unverified Commit cc04b62f authored by Delba de Oliveira's avatar Delba de Oliveira Committed by GitHub

[Docs] link validator: log broken links for PRs from forks (#51963)

parent 5ff48177
......@@ -305,19 +305,56 @@ async function updateComment(
}
}
async function createComment(comment: string): Promise<string> {
try {
const { data } = await octokit.rest.issues.createComment({
owner,
repo,
issue_number: pullRequest.number,
body: comment,
async function createComment(
comment: string,
allErrors: Errors[]
): Promise<string> {
const isFork = pullRequest.head.repo.fork
if (isFork) {
const errorTableData = allErrors.flatMap((errors) => {
const {
doc,
brokenLinks,
brokenHashes,
brokenSourceLinks,
brokenRelatedLinks,
} = errors
const allBrokenLinks = [
...brokenLinks,
...brokenHashes,
...brokenSourceLinks,
...brokenRelatedLinks,
]
return allBrokenLinks.map((link) => ({
path: doc.path,
link,
}))
})
return data.html_url
} catch (error) {
setFailed('Error creating comment: ' + error)
console.log('This PR introduces broken links to the docs:')
console.table(errorTableData, ['path', 'link'])
setFailed(
'The action could not create a Github comment because it is initiated from a forked repo. View the action logs for a list of broken links.'
)
return ''
} else {
try {
const { data } = await octokit.rest.issues.createComment({
owner,
repo,
issue_number: pullRequest.number,
body: comment,
})
return data.html_url
} catch (error) {
setFailed('Error creating comment: ' + error)
return ''
}
}
}
......@@ -404,6 +441,8 @@ async function validateAllInternalLinks(): Promise<void> {
let errorComment =
'Hi there :wave:\n\nIt looks like this PR introduces broken links to the docs, please take a moment to fix them before merging:\n\n| :heavy_multiplication_x: Broken link | :page_facing_up: File | \n| ----------- | ----------- | \n'
let errorsExist = false
allErrors.forEach((errors) => {
const {
doc: { path: docPath },
......@@ -414,24 +453,28 @@ async function validateAllInternalLinks(): Promise<void> {
} = errors
if (brokenLinks.length > 0) {
errorsExist = true
brokenLinks.forEach((link) => {
errorComment += formatTableRow(link, docPath)
})
}
if (brokenHashes.length > 0) {
errorsExist = true
brokenHashes.forEach((hash) => {
errorComment += formatTableRow(hash, docPath)
})
}
if (brokenSourceLinks.length > 0) {
errorsExist = true
brokenSourceLinks.forEach((link) => {
errorComment += formatTableRow(link, docPath)
})
}
if (brokenRelatedLinks.length > 0) {
errorsExist = true
brokenRelatedLinks.forEach((link) => {
errorComment += formatTableRow(link, docPath)
})
......@@ -440,14 +483,6 @@ async function validateAllInternalLinks(): Promise<void> {
errorComment += '\nThank you :pray:'
const errorsExist = allErrors.some(
(errors) =>
errors.brokenLinks.length > 0 ||
errors.brokenHashes.length > 0 ||
errors.brokenSourceLinks.length > 0 ||
errors.brokenRelatedLinks.length > 0
)
const botComment = await findBotComment()
let commentUrl
......@@ -457,7 +492,7 @@ async function validateAllInternalLinks(): Promise<void> {
if (botComment) {
commentUrl = await updateComment(comment, botComment)
} else {
commentUrl = await createComment(comment)
commentUrl = await createComment(comment, allErrors)
}
} else {
const comment = `${COMMENT_TAG}\nAll broken links are now fixed, thank you!`
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment