Repository navigation
fix: check documentation links on pull requests #363
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
Changes from 4 commits
26a3baa
63da7e1
36fb938
ac23eae
e57a48d
de3dfd2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| name: Check Documentation | ||
|
|
||
| on: | ||
| pull_request: | ||
| paths: | ||
| - 'docs/**' | ||
| - 'src/**' | ||
| - 'pyproject.toml' | ||
| - 'uv.lock' | ||
| - '.github/workflows/docs-check.yml' | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| concurrency: | ||
| group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} | ||
| cancel-in-progress: true | ||
|
|
||
| jobs: | ||
| build: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 | ||
| with: | ||
| persist-credentials: false | ||
| - name: Set up uv | ||
| uses: astral-sh/setup-uv@20cfd1bf945f4377ade1205e4dbc17946fc9a30d # v10.0.1 | ||
| with: | ||
| python-version: "3.12" | ||
| - name: Build and check documentation links | ||
| run: uv run --frozen --only-group docs python docs/check_links.py | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,7 +47,29 @@ Verify the site builds without errors: | |
| uv run mkdocs build | ||
| ``` | ||
|
|
||
| This generates the static site in `site/` and validates all internal links and references. | ||
| This generates the static site in `site/` and reports link diagnostics. An ordinary | ||
| build does not fail on warnings; use the checker below to enforce them. Link | ||
| validation levels are shared in `mkdocs.yml`, so `mkdocs serve` shows them too. | ||
|
|
||
| To run the pull-request check from the repository root: | ||
|
|
||
| ```bash | ||
| uv run --frozen python docs/check_links.py | ||
| uv run --frozen pytest tests/test_docs_build.py | ||
| ``` | ||
|
|
||
| The checker fails on logged MkDocs warnings and errors, including awesome-nav | ||
| failures, orphan pages, missing pages/images/anchors, broken API references, and | ||
| unrecognized or absolute links. It tolerates only the nine existing griffe/autorefs | ||
| warnings identified by logger, message and source location in `check_links.py`. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: once the line numbers are gone, "identified by logger, message and source location" should read something like "identified by logger, file and message", and "changed messages/locations" on the next line becomes "changed messages". |
||
| They remain visible; changed messages/locations and all errors fail. Python | ||
| warnings such as `DeprecationWarning` are not routed into the gate. External URLs | ||
| are not fetched. Run `make check` from this directory for the same gate. | ||
| The read-only `docs-check.yml` workflow | ||
| runs for documentation, source, dependency, or checker-workflow changes and never | ||
| deploys the site. Its `uv run --only-group docs` installs the docs group without | ||
| installing the project; the regular test workflow runs | ||
| the checker's tests. | ||
|
|
||
| ## Deployment | ||
|
|
||
|
|
@@ -68,4 +90,4 @@ Automatic deployment is configured in `.github/workflows/docs.yml`. The workflow | |
|
|
||
| --- | ||
|
|
||
| > **Note**: A `Makefile` is provided for convenience with targets: `install`, `docs`, `build`, `deploy`, and `clean`. | ||
| > **Note**: A `Makefile` is provided for convenience with targets: `install`, `docs`, `build`, `check`, `deploy`, and `clean`. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| """Build docs, failing on logged MkDocs diagnostics except nine known warnings. | ||
|
|
||
| Python warnings (such as DeprecationWarning) are not routed into this gate. | ||
| """ | ||
|
|
||
| import argparse | ||
| import logging | ||
| import re | ||
| from pathlib import Path | ||
|
|
||
| from mkdocs.commands.build import build | ||
| from mkdocs.config import load_config | ||
| from mkdocs.exceptions import MkDocsException | ||
|
|
||
| # Issue #355's existing annotation warnings. Keep messages and source locations | ||
| # explicit: a changed diagnostic needs review, not a blanket plugin exemption. | ||
| _GRIFFE_WARNINGS = { | ||
| "griffe: src/stickler/structured_object_evaluator/" + warning | ||
| for warning in ( | ||
| "bulk_structured_model_evaluator.py:965: No type or annotation for parameter 'df'", | ||
| "models/structured_model.py:1842: No type or annotation for parameter '**kwargs'", | ||
| "models/structured_model.py:1845: No type or annotation for returned value 1", | ||
| "models/comparable_field.py:102: No type or annotation for parameter '**field_kwargs'", | ||
| "models/comparable_field.py:105: No type or annotation for returned value 1", | ||
| "models/field.py:51: No type or annotation for parameter '**kwargs'", | ||
| ) | ||
| } | ||
| _AUTOREFS_WARNING = re.compile( | ||
| r"mkdocs_autorefs: API-Reference/models\.md: from (?:.*?/)?" | ||
| r"src/stickler/structured_object_evaluator/models/structured_model\.py:262: " | ||
| r"\(stickler\.structured_object_evaluator\.models\.structured_model\.StructuredModel\) " | ||
| r"Could not find cross-reference target ''(?:tp|fd|derived)''" | ||
| ) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should fix. The Suggestion, keeping your "explicit, not a blanket exemption" intent:
Separately, and optional: the three autorefs warnings come from the unfenced |
||
|
|
||
|
|
||
| class _LinkWarnings(logging.Handler): | ||
| def __init__(self): | ||
| super().__init__(level=logging.WARNING) | ||
| self.count = 0 | ||
|
|
||
| def emit(self, record): | ||
| message = record.getMessage().replace("\\", "/") | ||
| if record.levelno == logging.WARNING: | ||
| if record.name == "mkdocs.plugins.griffe": | ||
| # griffe uses absolute paths when invoked from docs/ (make check). | ||
| message = re.sub( | ||
| r"^griffe: (?:.*?/)?src/stickler/", | ||
| "griffe: src/stickler/", | ||
| message, | ||
| ) | ||
| if message in _GRIFFE_WARNINGS: | ||
| return | ||
| if ( | ||
| record.name == "mkdocs.plugins.mkdocs_autorefs._internal.plugin" | ||
| and _AUTOREFS_WARNING.fullmatch(message) | ||
| ): | ||
| return | ||
| self.count += 1 | ||
|
|
||
|
|
||
| def check_links(config_file: str, site_dir: str | None = None) -> int: | ||
| """Build a site and return nonzero for broken links; build errors propagate.""" | ||
| counter = _LinkWarnings() | ||
| logger = logging.getLogger("mkdocs") | ||
| logger.addHandler(counter) | ||
| try: | ||
| # Configuration and plugin loading can emit warnings before build(). | ||
| config = load_config(config_file=config_file, site_dir=site_dir) | ||
| config.plugins.on_startup(command="build", dirty=False) | ||
| try: | ||
| build(config) | ||
|
Comment on lines
+59
to
+61
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-blocking. This re-creates the CLI's build sequence but skips its |
||
| finally: | ||
| config.plugins.on_shutdown() | ||
| finally: | ||
| logger.removeHandler(counter) | ||
| counter.close() | ||
| if counter.count: | ||
| logger.error( | ||
| "Documentation check failed: %d warning(s) or error(s).", | ||
| counter.count, | ||
| ) | ||
| return 1 | ||
| return 0 | ||
|
|
||
|
|
||
| def main() -> int: | ||
| """Run the same build-only check locally and in pull-request CI.""" | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument( | ||
| "-f", "--config-file", default=str(Path(__file__).with_name("mkdocs.yml")) | ||
| ) | ||
| parser.add_argument("-d", "--site-dir") | ||
| args = parser.parse_args() | ||
| logging.basicConfig(level=logging.INFO, format="%(levelname)s - %(message)s") | ||
| try: | ||
| return check_links(args.config_file, args.site_dir) | ||
| except MkDocsException as error: | ||
| logging.getLogger("mkdocs").error("%s", error) | ||
| return 1 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,4 +3,5 @@ nav: | |
| - Comparators/ | ||
| - Evaluation/ | ||
| - Use-Cases/ | ||
| - Document_Packet_Splitting.md | ||
| - Best-Practices/ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,17 @@ site_url: https://awslabs.github.io/stickler/ | |
| repo_url: https://github.com/awslabs/stickler | ||
| repo_name: awslabs/stickler | ||
|
|
||
| # Use the same diagnostics in local serve/build and the PR checker. | ||
| validation: | ||
| nav: | ||
| not_found: warn | ||
|
Comment on lines
+7
to
+9
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should fix. Adding |
||
| omitted_files: warn | ||
| links: | ||
| not_found: warn | ||
| anchors: warn | ||
| unrecognized_links: warn | ||
| absolute_links: warn | ||
|
|
||
| theme: | ||
| name: material | ||
| palette: | ||
|
|
@@ -71,4 +82,4 @@ plugins: | |
| extra_javascript: | ||
| # Workaround for site_url breaking mermaid rendering; see the following for more info: | ||
| # https://github.com/squidfunk/mkdocs-material/issues/3742#issuecomment-1076068038 | ||
| - https://unpkg.com/mermaid@11/dist/mermaid.min.js | ||
| - https://unpkg.com/mermaid@11/dist/mermaid.min.js | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit, for the future. With
paths:, PRs outside those paths never report this check. That's fine today, but if "Check Documentation" is ever made a required status check, those PRs will wait forever. A line in.github/workflows/README.mdsaying this check must stay non-required (or a paths-filter step that always reports) would prevent the surprise.