1
0
mirror of synced 2026-01-30 15:01:41 -05:00

Broken weekly content linting report (#42504)

Co-authored-by: Kevin Heis <heiskr@users.noreply.github.com>
Co-authored-by: Peter Bengtsson <peterbe@github.com>
This commit is contained in:
Grace Park
2023-09-15 11:37:55 -07:00
committed by GitHub
parent 458a251909
commit 641d566d42
5 changed files with 282 additions and 136 deletions

View File

@@ -26,6 +26,10 @@ import { uploadArtifact as uploadArtifactLib } from '../../../.github/actions-sc
import github from '../../../script/helpers/github.js'
import { getActionContext } from '../../../.github/actions-scripts/lib/action-context.js'
import { createMinimalProcessor } from '#src/content-render/unified/processor.js'
import {
createReportIssue,
linkReports,
} from '../../../.github/actions-scripts/lib/issue-report.js'
const STATIC_PREFIXES = {
assets: path.resolve('assets'),
@@ -187,6 +191,9 @@ async function main(core, octokit, uploadArtifact, opts = {}) {
createReport = false,
failOnFlaw = false,
shouldComment = false,
reportRepository = 'github/docs-content',
reportAuthor = 'docs-bot',
reportLabel = 'broken link report',
} = opts
// Note! The reason we're using `warmServer()` in this script,
@@ -265,9 +272,26 @@ async function main(core, octokit, uploadArtifact, opts = {}) {
core.info(`All flaws written to artifact log.`)
if (createReport) {
core.info(`Creating issue for flaws...`)
const newReport = await createReportIssue(core, octokit, flaws, opts)
const reportProps = {
core,
octokit,
reportTitle: `${flaws.length + 1} broken links found`,
reportBody: flawIssueDisplay(flaws, opts),
reportRepository,
reportLabel,
}
const newReport = await createReportIssue(reportProps)
if (linkReports) {
await linkReports(core, octokit, newReport, opts)
const linkProps = {
core,
octokit,
newReport,
reportRepository,
reportAuthor,
reportLabel,
}
await linkReports(linkProps)
}
}
if (shouldComment) {
@@ -299,128 +323,6 @@ async function main(core, octokit, uploadArtifact, opts = {}) {
}
}
async function createReportIssue(core, octokit, flaws, opts) {
const { reportRepository = 'github/docs-content', reportLabel = 'broken link report' } = opts
const [owner, repo] = reportRepository.split('/')
const brokenLinksDisplay = flawIssueDisplay(flaws, opts)
// Create issue with broken links
let newReport
try {
const { data } = await octokit.request('POST /repos/{owner}/{repo}/issues', {
owner,
repo,
title: `${flaws.length + 1} broken links found`,
body: brokenLinksDisplay,
labels: [reportLabel],
})
newReport = data
core.info(`Created broken links report at ${newReport.html_url}\n`)
} catch (error) {
core.error(error)
core.setFailed('Error creating new issue')
throw error
}
return newReport
}
async function linkReports(core, octokit, newReport, opts) {
const {
reportRepository = 'github/docs-content',
reportAuthor = 'docs-bot',
reportLabel = 'broken link report',
} = opts
const [owner, repo] = reportRepository.split('/')
core.info('Attempting to link reports...')
// Find previous broken link report issue
let previousReports
try {
previousReports = await octokit.rest.issues.listForRepo({
owner,
repo,
creator: reportAuthor,
labels: reportLabel,
state: 'all', // We want to get the previous report, even if it is closed
sort: 'created',
direction: 'desc',
per_page: 25,
})
previousReports = previousReports.data
} catch (error) {
core.setFailed('Error listing issues for repo')
throw error
}
core.info(`Found ${previousReports.length} previous reports`)
if (previousReports.length <= 1) {
core.info('No previous reports to link to')
return
}
// 2nd report should be most recent previous report
const previousReport = previousReports[1]
// Comment the old report link on the new report
try {
await octokit.rest.issues.createComment({
owner,
repo,
issue_number: newReport.number,
body: `⬅️ [Previous report](${previousReport.html_url})`,
})
core.info(`Linked old report to new report via comment on new report, #${newReport.number}`)
} catch (error) {
core.setFailed(`Error commenting on newReport, #${newReport.number}`)
throw error
}
// Comment on all previous reports that are still open
for (const previousReport of previousReports) {
if (previousReport.state === 'closed' || previousReport.html_url === newReport.html_url) {
continue
}
// If an old report is not assigned to someone we close it
const shouldClose = !previousReport.assignees.length
let body = `➡️ [Newer report](${newReport.html_url})`
if (shouldClose) {
body += '\n\nClosing in favor of newer report since there are no assignees on this issue'
}
try {
await octokit.rest.issues.createComment({
owner,
repo,
issue_number: previousReport.number,
body,
})
core.info(
`Linked old report to new report via comment on old report: #${previousReport.number}.`,
)
} catch (error) {
core.setFailed(`Error commenting on previousReport, #${previousReport.number}`)
throw error
}
if (shouldClose) {
try {
await octokit.rest.issues.update({
owner,
repo,
issue_number: previousReport.number,
state: 'closed',
})
core.info(`Closing old report: #${previousReport.number} because it doesn't have assignees`)
} catch (error) {
core.setFailed(`Error closing previousReport, #${previousReport.number}`)
throw error
}
}
}
}
async function commentOnPR(core, octokit, flaws, opts) {
const { actionContext = {} } = opts
const { owner, repo } = actionContext