1
0
mirror of synced 2025-12-19 18:10:59 -05:00
Files
docs/script/rendered-content-link-checker.js
Evan Bonsignori 7b4429418b Migrate links check to JS pattern (#30175)
Co-authored-by: Sarah Schneider <sarahs@users.noreply.github.com>
Co-authored-by: Peter Bengtsson <peterbe@github.com>
2022-10-17 21:47:21 +00:00

141 lines
4.2 KiB
JavaScript
Executable File

#!/usr/bin/env node
// [start-readme]
//
// This script goes through all content and renders their HTML and from there
// can analyze for various flaws (e.g. broken links)
//
// [end-readme]
import fs from 'fs'
import path from 'path'
import { program, Option, InvalidArgumentError } from 'commander'
import { languageKeys } from '../lib/languages.js'
import renderedContentLinkChecker from '../.github/actions/rendered-content-link-checker.js'
import { getCoreInject, getUploadArtifactInject } from './helpers/action-injections.js'
import github from './helpers/github.js'
const STATIC_PREFIXES = {
assets: path.resolve('assets'),
public: path.resolve(path.join('data', 'graphql')),
}
// Sanity check that these are valid paths
Object.entries(STATIC_PREFIXES).forEach(([key, value]) => {
if (!fs.existsSync(value)) {
throw new Error(`Can't find static prefix (${key}): ${value}`)
}
})
program
.description('Analyze all checked content files, render them, and check for flaws.')
.addOption(
new Option('-L, --level <LEVEL>', 'Filter of flaw level').choices([
'all',
'warning',
'critical',
])
)
.addOption(
new Option(
'-l, --language <LANGUAGE...>',
'Which languages to focus on. (default: "en")'
).choices(languageKeys)
)
.option('-f, --filter <FILTER...>', 'Search filter(s) on the paths')
.option('-l, --level', 'Level of broken link to be marked as a flaw. (default: "critical")')
.option('-v, --verbose', 'Verbose outputs')
.option(
'--create-report',
'Create a report issue in report-repository if there are flaws. (default: false)'
)
.option(
'--report-repository <REPOSITORY>',
'Repository to create issue in. (default: "github/docs-content")'
)
.option(
'--link-reports',
'If comments should be made on previous report and new report "linking" them. (default: false)'
)
.option(
'--report-author <AUTHOR>',
'Previous author of report PR for linking. (default: "docubot")'
)
.option(
'--report-label <LABEL>',
'Label to assign to report issue. (default: "broken link report")'
)
.option(
'--comment-on-pr <URI>',
'For debugging. Comment on a PR in form "owner/repo-name:pr_number"'
)
.option('--should-comment', 'Comments failed links on PR')
.option('--check-anchors', "Validate links that start with a '#' too")
.option('--check-images', 'Validate local images too')
.option('--check-external-links', 'Check external URLs too')
.option('--debug', "Loud about everything it's doing")
.option('--patient', 'Give external link checking longer timeouts and more retries')
.option('--random', 'Load pages in a random order (useful for debugging)')
.option('--verbose-url <BASE_URL>', 'Print the absolute URL if set')
.option('--fail-on-flaw', 'Throw error on link flaws (default: false)')
.option('--max <number>', 'integer argument (default: none)', (value) => {
const parsed = parseInt(value, 10)
if (isNaN(parsed)) {
throw new InvalidArgumentError('Not a number.')
}
return parsed
})
.option(
'--list <file>.json',
'JSON file containing an array of specific files to check (default: none)',
(filePath) => {
const resolvedPath = path.resolve(filePath)
let stats
try {
stats = fs.statSync(resolvedPath)
} catch (error) {
// Ignore
}
if (!stats || !stats.isFile()) {
throw new InvalidArgumentError('Not an existing file.')
}
return resolvedPath
}
)
.arguments('[files...]', 'Specific files to check')
.parse(process.argv)
const opts = program.opts()
const files = program.args || opts.list
const octokit = github()
if (opts.list && Array.isArray(files) && files.length > 0) {
throw new InvalidArgumentError('Cannot specify both --list and a file list.')
}
// For debugging PR comment. e.g. "github/howie-testing-ebonsignori:140"
if (opts.commentOnPr) {
const [owner, repoPRNumber] = opts.commentOnPr.split('/')
const [repo, number] = repoPRNumber.split(':')
opts.shouldComment = true
opts.actionContext = {
owner,
repo,
pull_request: {
number,
},
}
}
renderedContentLinkChecker(
getCoreInject(opts.debug),
octokit,
getUploadArtifactInject(opts.debug),
{
...opts,
files,
}
)