Repository navigation
Community Markdown validation works without PR-template files #86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Marius Storhaug (MariusStorhaug)
wants to merge
10
commits into
main
Choose a base branch
from
pr-validation-gate
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,081
−0
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f61c80e
Add test-first shared Markdown file validation
MariusStorhaug 0b749ae
Run read-only community validation on pull request heads
MariusStorhaug 8b50109
Escape validation paths in failure diagnostics
MariusStorhaug 0ac62ba
Validate complete Git-aware community Markdown inputs
MariusStorhaug 772423f
Require an exact checked-out PR head before validation
MariusStorhaug 66cd708
Isolate fixture Git environment and disable template copying
MariusStorhaug a009b62
Clarify validation applies only to central community sources
MariusStorhaug 6609aa5
Keep resolved Markdown inputs inside the central repository
MariusStorhaug 1d45385
Allow intentional PR-template absence in community validation
MariusStorhaug 26463d1
Exclude intentionally deleted optional Markdown from scan inputs
MariusStorhaug File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| --- | ||
| version: 2 | ||
| updates: | ||
| - package-ecosystem: github-actions | ||
| directory: / | ||
| schedule: | ||
| interval: weekly | ||
|
|
||
| - package-ecosystem: npm | ||
| directory: / | ||
| schedule: | ||
| interval: weekly |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| ########################### | ||
| ## Markdown Linter rules ## | ||
| ########################### | ||
|
|
||
| # Linter rules doc: | ||
| # - https://github.com/DavidAnson/markdownlint | ||
|
|
||
| ############### | ||
| # Rules by id # | ||
| ############### | ||
| MD004: false # Unordered list style | ||
| MD007: | ||
| indent: 2 # Unordered list indentation | ||
| MD013: | ||
| line_length: 3000 # Line length | ||
| MD025: | ||
| front_matter_title: "" # Allow a body H1 alongside the front matter title | ||
| MD026: | ||
| punctuation: ".,;:!。,;:" # List of not allowed | ||
| MD029: false # Ordered list item prefix | ||
| MD033: false # Allow inline HTML | ||
| MD036: false # Emphasis used instead of a heading | ||
| MD041: false # First line in file should be a top level heading, PULL_REQUEST_TEMPLATE.md is an exception | ||
| MD060: false # Table column style — content mixes compact and aligned tables | ||
|
|
||
| ################# | ||
| # Rules by tags # | ||
| ################# | ||
| blank_lines: false # Error on blank lines |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { spawnSync } from 'node:child_process'; | ||
| import { lstatSync, readFileSync, realpathSync } from 'node:fs'; | ||
| import { isAbsolute, join, relative, resolve, sep } from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
| import { lint, readConfig } from 'markdownlint-cli2/markdownlint/promise'; | ||
| import yamlParse from 'markdownlint-cli2/parsers/yaml'; | ||
|
|
||
| /** Read repository file metadata without invoking a shell. */ | ||
| function readGitOutput(directory, args) { | ||
| const result = spawnSync('git', ['-C', directory, ...args], { encoding: 'utf8' }); | ||
| if (result.error) { | ||
| throw result.error; | ||
| } | ||
| if (result.status !== 0) { | ||
| throw new Error(`Git file discovery failed in ${JSON.stringify(directory)}: ${JSON.stringify(result.stderr.trim())}`); | ||
| } | ||
| return result.stdout; | ||
| } | ||
|
|
||
| /** Select existing tracked and nonignored Markdown, retaining Git symlink metadata. */ | ||
| function findMarkdownFiles(directory) { | ||
| const deleted = new Set(readGitOutput(directory, ['ls-files', '--deleted', '-z']).split('\0')); | ||
| const files = readGitOutput(directory, [ | ||
| 'ls-files', '--cached', '--others', '--exclude-standard', '--deduplicate', '-z', | ||
| ]).split('\0') | ||
| .filter((name) => !deleted.has(name)) | ||
| .filter((name) => /\.(?:md|markdown)$/i.test(name) && !name.split('/').includes('node_modules')) | ||
| .map((name) => join(directory, ...name.split('/'))) | ||
| .sort(); | ||
| // Git symlinks can be ordinary path-text files in Windows checkouts. | ||
| const symlinks = new Set(readGitOutput(directory, ['ls-files', '--stage', '-z']) | ||
| .split('\0') | ||
| .filter((record) => record.startsWith('120000 ')) | ||
| .map((record) => join(directory, ...record.slice(record.indexOf('\t') + 1).split('/')))); | ||
| return { files, symlinks }; | ||
| } | ||
|
|
||
| // Optional first argument selects an isolated root; the shared rules stay fixed. | ||
| const directory = resolve(process.argv[2] ?? process.cwd()); | ||
| if (!lstatSync(directory).isDirectory()) { | ||
| throw new Error(`Validation root is not a directory: ${JSON.stringify(directory)}`); | ||
| } | ||
| const repositoryRoot = readGitOutput(directory, ['rev-parse', '--show-toplevel']).replace(/\r?\n$/, ''); | ||
| const resolvedRepositoryRoot = realpathSync(directory); | ||
| if (resolvedRepositoryRoot !== realpathSync(repositoryRoot)) { | ||
| throw new Error(`Validation must run from the repository root: ${JSON.stringify(repositoryRoot)}`); | ||
| } | ||
| const configurationPath = fileURLToPath(new URL('../linters/.markdown-lint.yml', import.meta.url)); | ||
| const configuration = await readConfig(configurationPath, [yamlParse]); | ||
| const { files, symlinks } = findMarkdownFiles(directory); | ||
| if (files.length === 0) { | ||
| throw new Error(`No Markdown files were checked in ${JSON.stringify(directory)}. Check the validation root.`); | ||
| } | ||
| const contributionSource = join(directory, '.github', 'CONTRIBUTING.md'); | ||
| if (!files.includes(contributionSource)) { | ||
| throw new Error(`Required community file is missing from the validation input: ${JSON.stringify(contributionSource)}. Restore the file or explicitly change the contract.`); | ||
| } | ||
| for (const file of files) { | ||
| const metadata = lstatSync(file); | ||
| if (metadata.isSymbolicLink() || symlinks.has(file)) { | ||
| throw new Error(`Markdown symlinks are not allowed: ${JSON.stringify(file)}. Use a regular file instead.`); | ||
| } | ||
| if (!metadata.isFile()) { | ||
| throw new Error(`Markdown input must be a regular file: ${JSON.stringify(file)}`); | ||
| } | ||
| const resolvedLocation = relative(resolvedRepositoryRoot, realpathSync(file)); | ||
| if (resolvedLocation === '..' || resolvedLocation.startsWith(`..${sep}`) || isAbsolute(resolvedLocation)) { | ||
| throw new Error(`Markdown input resolves outside the repository root: ${JSON.stringify(file)}. Use a repository-local source.`); | ||
| } | ||
| if (readFileSync(file, 'utf8').trim().length === 0) { | ||
| throw new Error(`Empty Markdown file: ${JSON.stringify(file)}. Restore its content before validation.`); | ||
| } | ||
| } | ||
|
|
||
| const results = await lint({ files, config: configuration }); | ||
| const checkedFiles = Object.keys(results); | ||
| if (checkedFiles.length !== files.length) { | ||
| throw new Error(`Markdown lint checked ${checkedFiles.length} of ${files.length} selected files.`); | ||
| } | ||
| for (const file of checkedFiles) { | ||
| console.log(JSON.stringify(relative(directory, file))); | ||
| } | ||
| const hasErrors = Object.values(results).some((errors) => errors.length > 0); | ||
| if (hasErrors) { | ||
| console.error(JSON.stringify(results, null, 2)); | ||
| } | ||
| console.log(`Checked ${checkedFiles.length} Markdown files; result: ${hasErrors ? 'failed' : 'passed'}.`); | ||
| process.exitCode = hasErrors ? 1 : 0; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.