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( ...@@ -305,19 +305,56 @@ async function updateComment(
} }
} }
async function createComment(comment: string): Promise<string> { async function createComment(
try { comment: string,
const { data } = await octokit.rest.issues.createComment({ allErrors: Errors[]
owner, ): Promise<string> {
repo, const isFork = pullRequest.head.repo.fork
issue_number: pullRequest.number,
body: comment, 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 console.log('This PR introduces broken links to the docs:')
} catch (error) { console.table(errorTableData, ['path', 'link'])
setFailed('Error creating comment: ' + error) 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 '' 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> { ...@@ -404,6 +441,8 @@ async function validateAllInternalLinks(): Promise<void> {
let errorComment = 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' '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) => { allErrors.forEach((errors) => {
const { const {
doc: { path: docPath }, doc: { path: docPath },
...@@ -414,24 +453,28 @@ async function validateAllInternalLinks(): Promise<void> { ...@@ -414,24 +453,28 @@ async function validateAllInternalLinks(): Promise<void> {
} = errors } = errors
if (brokenLinks.length > 0) { if (brokenLinks.length > 0) {
errorsExist = true
brokenLinks.forEach((link) => { brokenLinks.forEach((link) => {
errorComment += formatTableRow(link, docPath) errorComment += formatTableRow(link, docPath)
}) })
} }
if (brokenHashes.length > 0) { if (brokenHashes.length > 0) {
errorsExist = true
brokenHashes.forEach((hash) => { brokenHashes.forEach((hash) => {
errorComment += formatTableRow(hash, docPath) errorComment += formatTableRow(hash, docPath)
}) })
} }
if (brokenSourceLinks.length > 0) { if (brokenSourceLinks.length > 0) {
errorsExist = true
brokenSourceLinks.forEach((link) => { brokenSourceLinks.forEach((link) => {
errorComment += formatTableRow(link, docPath) errorComment += formatTableRow(link, docPath)
}) })
} }
if (brokenRelatedLinks.length > 0) { if (brokenRelatedLinks.length > 0) {
errorsExist = true
brokenRelatedLinks.forEach((link) => { brokenRelatedLinks.forEach((link) => {
errorComment += formatTableRow(link, docPath) errorComment += formatTableRow(link, docPath)
}) })
...@@ -440,14 +483,6 @@ async function validateAllInternalLinks(): Promise<void> { ...@@ -440,14 +483,6 @@ async function validateAllInternalLinks(): Promise<void> {
errorComment += '\nThank you :pray:' 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() const botComment = await findBotComment()
let commentUrl let commentUrl
...@@ -457,7 +492,7 @@ async function validateAllInternalLinks(): Promise<void> { ...@@ -457,7 +492,7 @@ async function validateAllInternalLinks(): Promise<void> {
if (botComment) { if (botComment) {
commentUrl = await updateComment(comment, botComment) commentUrl = await updateComment(comment, botComment)
} else { } else {
commentUrl = await createComment(comment) commentUrl = await createComment(comment, allErrors)
} }
} else { } else {
const comment = `${COMMENT_TAG}\nAll broken links are now fixed, thank you!` 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