repomatic.broken_links module¶
Broken links detection and reporting.
Combines Lychee and Sphinx linkcheck results into a single “Broken links” GitHub issue. Sphinx linkcheck parsing detects broken auto-generated links (intersphinx, autodoc, type annotations) that Lychee cannot see because they only exist in the rendered HTML output.
run_lychee() runs the Lychee crawl itself, through the tool runner, and
reads back the report and the exit code. No workflow step has to relay either.
Each checker reports one LinkVerdict. Only a run that completed can
clear the issue: a checker that did not finish says nothing about the links,
so it never closes an open issue.
Issue lifecycle management is delegated to issue.
- repomatic.broken_links.ISSUE_TITLE = 'Broken links'¶
Issue title used for the combined broken links report.
- repomatic.broken_links.DOCUMENTATION_LABEL = '📚 documentation'¶
Label of the broken links issue, outside awesome lists.
- repomatic.broken_links.FIX_LINK_LABEL = '🩹 fix link'¶
Label of the broken links issue in an
awesome-*repository.
- repomatic.broken_links.LYCHEE_BROKEN_LINKS_EXIT = 2¶
The one lychee exit code that reports on the links rather than on the run.
Lychee exits 0 on success, 1 on an unexpected failure, 2 when it found broken links, and 3 on a config error. Only 2 is a verdict about the links; 1 and 3 say the run itself did not complete, so neither “broken links found” nor “no broken links” can be claimed from them.
- repomatic.broken_links.LYCHEE_REPORT_FORMAT: Final[tuple[str, ...]] = ('--format', 'markdown')¶
Flags that make lychee write the markdown report the issue body embeds.
- repomatic.broken_links.LYCHEE_INSTALL_FAILURE_EXIT = 1¶
Exit code a lychee that could not be installed counts as.
The one lychee itself returns on an unexpected failure: like a crash, a missing tool checks no link, so the issue is left as it is.
- repomatic.broken_links.GITHUB_TOKEN_ENV = 'GITHUB_TOKEN'¶
Environment variable holding the token that signs lychee’s github.com requests.
The
check-broken-linksjob ofdocs.yamlsets it. Seelychee_github_auth().
- repomatic.broken_links.RELEASE_DOWNLOAD_PATTERN = '^https://github\\.com/[^/]+/[^/]+/releases/(?:latest/)?download/'¶
The lychee
excludepattern of a release download, which a signed request breaks.See
lychee_github_auth().
- repomatic.broken_links.BLOB_URL_PATTERN = '^https://github\\.com/([^/]+)/([^/]+)/blob/([^?#]+)(?:[?#].*)?$'¶
A github.com
/blob/URL: owner, repository, then the ref and the path.The query and the fragment, like
?plain=1#L5, match outside the groups, soRAW_URL_TEMPLATEdrops them.
- repomatic.broken_links.RAW_URL_TEMPLATE = 'https://raw.githubusercontent.com/${1}/${2}/${3}'¶
The raw.githubusercontent.com URL of the file a
BLOB_URL_PATTERNURL shows.The
${N}references are the replacement syntax of the Rustregexcrate, which lychee uses. Seelychee_github_auth().
- repomatic.broken_links.SPHINX_DEFAULT_OUTPUT = PosixPath('docs/_linkcheck/output.json')¶
Default Sphinx linkcheck output path produced by the
docs.yamlworkflow.
- repomatic.broken_links.NO_BROKEN_LINKS = 'No broken links found.'¶
The section body of a checker that completed and found nothing broken.
- repomatic.broken_links.SPHINX_FIRST_LOCATION_NOTE = 'Sphinx linkcheck checks each URL once and lists only its first location. Search the repository for a URL before you fix it, to find its other locations.'¶
Warning above the broken links of the Sphinx section.
Sphinx linkcheck collects each URI once:
HyperlinkCollector._add_uriskips a URI it already holds (Sphinx9.2.0), sooutput.jsonrecords only the first place the URI appears. A URL used in a page and in a docstring shows under one of them, and a fix made from the section alone misses the other.
- class repomatic.broken_links.LinkVerdict(*values)[source]¶
Bases:
StrEnumWhat one checker’s run says about the links it covers.
- BROKEN = 'broken'¶
The run completed and found broken links.
- CLEAN = 'clean'¶
The run completed and found no broken link.
- UNKNOWN = 'unknown'¶
The run did not complete, or left no report: nothing can be claimed.
- class repomatic.broken_links.LycheeRun(exit_code, report=None)[source]¶
Bases:
objectWhat one lychee crawl of the documentation files returned.
- exit_code: int¶
Exit code of lychee. See
LYCHEE_BROKEN_LINKS_EXIT.
- repomatic.broken_links.lychee_github_auth(scratch)[source]¶
Return the lychee arguments that sign its github.com requests.
A CI runner gets
503from github.com for many anonymous requests to a/blob/page, which pass from a workstation. Lychee0.24.2usesGITHUB_TOKEN_ENVonly in its GitHub API fallback, which reports a URL with a path after the repository as invalid. So the token signs the request itself, asclick_extra.sphinxdoes for Sphinx linkcheck.A signed request to a
/blob/page still gets503on a runner. So the signed crawl remaps eachBLOB_URL_PATTERNURL toRAW_URL_TEMPLATEbefore it checks it: raw.githubusercontent.com serves the same file, for programs. The token signs that host too, which answers404to a token it does not accept: a bad token fails every/blob/link. Without a token, the crawl keeps the/blob/URLs, which pass from a workstation.Lychee reads the headers of one host only from a
[hosts."github.com"]table of a config file: no flag sets them. So the tables and the remap go to a file in scratch, and the token stays out of the command line, which the tool runner logs.Caution
A
--configflag stops lychee from finding the project’s own config, inlychee.tomlor in the[tool.lychee]table ofpyproject.toml. That file comes first, in its own--config. Lychee merges the files: the last one wins a setting that two of them set, and theirexcludeandremaplists add up. Lychee also finds a config inCargo.tomlorpackage.json, whichConfigSourcedoes not know: such a config does not apply to a signed crawl.Lychee matches
excludeagainst the remapped URL, so anexcludewritten for a/blob/URL does not apply to a signed crawl. The report names the remapped URL too: a broken/blob/link shows as its raw.githubusercontent.com URL, at the line and column of the link.The file also excludes
RELEASE_DOWNLOAD_PATTERN. With anAuthorizationheader, github.com redirects a release download to a host whose signed URL answers401, and a lychee config sets headers per host, not per URL. Sphinx linkcheck still checks the release downloads of its pages, without the token.
- repomatic.broken_links.lychee_path_exclusions()[source]¶
Compile the
exclude_pathexpressions of the lychee configuration.Reads the configuration that wins the walk of
ConfigSource. lychee searches each expression in an input path exactly as it was passed, and warnsNo files found for this input sourcefor each input that one of them excludes.run_lychee()leaves those inputs out, so the crawl log names only the files lychee reads.An expression that Python cannot compile stays out of the filter, with a warning: lychee, whose
regexcrate reads it, still applies it.
- repomatic.broken_links.run_lychee()[source]¶
Crawl the documentation files with lychee, and keep its report.
Runs the invocation of a bare
repomatic run lychee: the default arguments of the registry, over thedoc_filesinventory, less the files thatlychee_path_exclusions()excludes. The markdown report goes to a scratch file this function reads back, so no workflow step has to agree on a path with it. WithGITHUB_TOKEN_ENVset, the crawl also signs its github.com requests and checks its/blob/URLs on raw.githubusercontent.com, perlychee_github_auth().A lychee that cannot be installed checked no link, so it counts as a run that did not complete:
LYCHEE_INSTALL_FAILURE_EXIT.
- class repomatic.broken_links.LinkcheckResult(filename, lineno, status, uri, info)[source]¶
Bases:
objectA single result entry from Sphinx linkcheck
output.json.Each line in the JSON-lines file corresponds to one checked URI.
- repomatic.broken_links.parse_output_json(output_json)[source]¶
Parse the Sphinx linkcheck
output.jsonfile.The file uses JSON-lines format: one JSON object per line. Blank lines are skipped.
- Parameters:
output_json (
Path) – Path to theoutput.jsonfile.- Return type:
- Returns:
List of parsed linkcheck results.
- repomatic.broken_links.filter_broken(results)[source]¶
Filter results to only broken and timed-out links.
- Parameters:
results (
Iterable[LinkcheckResult]) – Iterable of linkcheck results.- Return type:
- Returns:
List of results with
statusof"broken"or"timeout".
- repomatic.broken_links.generate_markdown_report(broken, source_url=None)[source]¶
Generate a Markdown report of broken links grouped by source file.
The report starts with H2 file headings, suitable for embedding as a section in the combined broken links issue body.
- Parameters:
broken (
list[LinkcheckResult]) – List of broken linkcheck results.source_url (
str|None) – Base URL for linking filenames and line numbers. When provided, file headers become clickable links and line numbers deep-link to the specific line.
- Return type:
- Returns:
Markdown-formatted report string.
- repomatic.broken_links.get_label(repo_name)[source]¶
Return the label the broken links issue carries in a repository.
- Parameters:
repo_name (
str) – The repository name.- Return type:
- Returns:
FIX_LINK_LABELforawesome-*repos, elseDOCUMENTATION_LABEL.
- repomatic.broken_links.lychee_section(run)[source]¶
Render the Lychee section of the issue body, with its verdict.
Only
LYCHEE_BROKEN_LINKS_EXITreports on the links themselves, and only with a report to show: a broken-links verdict with nothing to show would file an issue whose body claims the links are fine. Every other non-zero code reports that the run failed.- Parameters:
run (
LycheeRun) – The lychee crawl.- Return type:
- Returns:
The section markdown, and what it says about the links.
- repomatic.broken_links.sphinx_section(output_json, source_url=None)[source]¶
Render the Sphinx linkcheck section of the issue body, with its verdict.
- repomatic.broken_links.manage_combined_broken_links_issue(repo_name=None, lychee=None, sphinx_output_json=None, sphinx_source_url=None)[source]¶
Manage the combined broken links issue lifecycle.
Combines results from Lychee and Sphinx linkcheck into a single “Broken links” issue. Each tool’s results appear under its own heading. Tools that were not run are omitted from the report.
The issue is filed or updated when any checker found broken links, and closed when every checker that ran completed clean. When no checker found broken links but one did not complete, the issue is left as it is: the links that run covers might still be broken. When no checker ran at all, the issue is left as it is too, with a warning: nothing was checked.
When running in GitHub Actions, most parameters are auto-detected from
Metadataand well-known file paths:repo_namedefaults toMetadata.repo_name.sphinx_output_jsondefaults toSPHINX_DEFAULT_OUTPUTwhen the file exists.sphinx_source_urlis composed fromMetadata.repo_urlandMetadata.sha.
- Parameters:
repo_name (
str|None) – Repository name, for label selection.lychee (
LycheeRun|None) – The lychee crawl fromrun_lychee(),Noneif lychee did not run.sphinx_output_json (
Path|None) – Path to Sphinx linkcheckoutput.json.sphinx_source_url (
str|None) – Base URL for linking filenames and line numbers in the Sphinx report.
- Return type:
- Returns:
The combined verdict, or
Nonewhen no checker ran.- Raises:
ValueError – If
repo_namecannot be determined.