repomatic.metadata.core module¶

Extract metadata from repository and Python projects to be used by GitHub workflows.

This module solves a fundamental limitation of GitHub Actions: a workflow run is triggered by a singular event, which might encapsulate multiple commits. GitHub only exposes github.event.head_commit (the most recent commit), but workflows often need to process all commits in the push event.

This is critical for releases, where two commits are pushed together:

  1. [changelog] Release vX.Y.Z: the release commit to be tagged and published

  2. [changelog] Post-release bump vX.Y.Z → vX.Y.Z: the next dev cycle’s bump

Since github.event.head_commit only sees the post-release bump, this module extracts the full commit range from the push event and identifies release commits that need special handling (tagging, PyPI publishing, GitHub release creation).

Output shapes

Every key is printed to the environment file as one key=value line. Values take three shapes:

is_python_project=true
doc_files="changelog.md" "readme.md" "docs/license.md"
new_commits_matrix={"commit": ["346ce66…", "6f27db4…"], "include": [{"commit": "346ce66…", "short_sha": "346ce66"}]}

A scalar prints bare. A list prints as space-joined, individually quoted items, not a JSON array: workflow if: conditions test membership with a padded contains() against that string. A matrix prints as inlined JSON for fromJSON() to parse into a job matrix. See format_github_value() for the encoding, and Dialect for the other output formats.

The full key inventory is generated from this module rather than listed here, so it cannot go stale: run repomatic show-metadata --list-keys, or read the rendered table in the workflows documentation.

repomatic.metadata.core.FILE_GROUP_KEYS: Final[frozenset[str]] = frozenset({'doc_files', 'gitignore_exists', 'image_files', 'json_files', 'markdown_files', 'pyproject_files', 'python_files', 'shfmt_files', 'workflow_files', 'yaml_files', 'zsh_files'})¶

Metadata keys read off Metadata.files, the attribute of the same name.

The inventory answers them with no CI context, no git history and no pyproject.toml, so Metadata.dump_factories() reads them there directly.

repomatic.metadata.core.METADATA_CONFIG_FIELDS: Final[tuple[str, ...]] = ('binaries_sync', 'manpages_script', 'release_assets', 'site_deploy', 'sphinx_builder', 'sphinx_fail_on_warning')¶

Config fields a workflow reads through the metadata output, sorted.

Every other field stays out of the output: the subcommand that needs one reads it from [tool.repomatic] directly. A new field joins this list only when a workflow condition or step reads it, like the binaries.sync toggle a separate pr-sync step depends on.

repomatic.metadata.core.METADATA_KEYS_HEADER_DEFS: tuple[ColumnSpec, ...] = (ColumnSpec(id='key', label='Key', description='', max_width=None, optional=False, sortable=True), ColumnSpec(id='description', label='Description', description='', max_width=None, optional=False, sortable=True))¶

Column definitions for the metadata keys reference table.

repomatic.metadata.core.metadata_keys_reference()[source]¶

Build the metadata keys reference as table rows.

Returns a list of (key, description) tuples for all keys produced by Metadata.dump(), including the METADATA_CONFIG_FIELDS of [tool.repomatic]. Rows are unsorted: sorting is handled by the CLI’s SortByOption.

Return type:

list[tuple[str, str]]

repomatic.metadata.core.all_metadata_keys()[source]¶

Returns the set of all valid metadata key names.

Return type:

frozenset[str]

repomatic.metadata.core.METADATA_VALUE_OPTIONS: frozenset[str] = frozenset({'--format', '--output', '--output-format', '--sort-by', '-o'})¶

Options on the show-metadata command consuming the token that follows them.

Needed by unknown_metadata_keys() to tell a positional key from an option’s value while reading a workflow’s run: line. --format is the spelling of releases before 8.0.0, kept so that a workflow pinned to one of them still parses.

Listed here rather than derived, and pinned against the real command by repomatic’s own test suite, so an option added later cannot quietly turn its value into a token the lint reports as an unknown key.

repomatic.metadata.core.METADATA_SUBCOMMAND: Final[str] = 'show-metadata'¶

The subcommand that prints the metadata keys a workflow asks for.

repomatic.metadata.core.REMOVED_METADATA_ALIAS: Final[str] = 'metadata'¶

The alias of METADATA_SUBCOMMAND that 8.0.0 removed.

A workflow still calling it fails at its first job, so the readers below recognize it in order to report it.

repomatic.metadata.core.RENAMED_METADATA_OPTIONS: Final[dict[str, str]] = {'--format': '--output-format'}¶

Options of METADATA_SUBCOMMAND that 8.0.0 renamed, mapped to the new name.

The command rejects the old spelling with a UsageError, so a workflow still passing one fails at its first job, like a call of the removed alias. Header-only sync never reads the job body holding the call, so the readers below recognize the old spelling in order to report it.

class repomatic.metadata.core.MetadataRequest(path, job_id, keys, unknown, removed_alias, renamed_options=())[source]¶

Bases: NamedTuple

The metadata one workflow step asks for.

Create new instance of MetadataRequest(path, job_id, keys, unknown, removed_alias, renamed_options)

path: Path¶

The workflow file holding the step.

job_id: str¶

The job holding the step.

keys: tuple[str, ...]¶

Every key the step requests, in the order written.

unknown: tuple[str, ...]¶

The requested keys this version does not emit, sorted.

removed_alias: bool¶

Whether the step calls the REMOVED_METADATA_ALIAS spelling.

renamed_options: tuple[str, ...]¶

The RENAMED_METADATA_OPTIONS the step still passes, sorted.

repomatic.metadata.core.unknown_metadata_keys(workflows, package)[source]¶

Read every metadata call of workflows, and the keys it gets wrong.

The one scan behind the lint-repo check and the repomatic init warning, so the two verdicts cannot disagree about what a run: line requests.

Parameters:
  • workflows (Mapping[Path, Mapping[str, Any]]) – Parsed workflow documents, keyed by path.

  • package (str) – Upstream package name (like repomatic).

Return type:

list[MetadataRequest]

Returns:

One entry per step that calls the metadata subcommand with keys, or that calls the removed alias or passes a renamed option at all, in workflow order. A step requesting only valid keys is listed with no unknown key.

class repomatic.metadata.core.Metadata[source]¶

Bases: EnvironmentMetadata, GitMetadata, MatrixMetadata, ProjectMetadata

Metadata class.

Implemented as a singleton: every Metadata() call returns the same instance within a process, and each property keeps the first value it reads. Env vars do not change during a single CLI invocation. Project files can: a command that rewrites one, like bump-version, reads the new content through get_current_version(), not through a cached property. Use reset() in test teardown to discard the cached instance between tests.

classmethod reset()[source]¶

Discard the singleton so the next call creates a fresh instance.

Intended for test teardown only. Production code should never call this.

Return type:

None

property mailmap_exists: bool[source]¶
property cloudflare_project: str[source]¶

The Cloudflare Pages project the site deploys into.

[tool.repomatic] site.cloudflare-project when it names one, else the repository name. The deploy job reads this key, and the cloudflare-pages and cloudflare-r2 commands fall back to it when --project is absent, so all of them address the same project.

Returns:

The project name, empty when neither source names one.

property files: FileInventory[source]¶

What this repository holds on disk, .gitignore applied.

The inventory is its own concern (repomatic.file_inventory): answering “which Markdown files are there” needs no CI context, no git history and no pyproject.toml. The FILE_GROUP_KEYS output keys read the inventory attribute of the same name.

property current_version: str | None[source]¶

Returns the current version.

Current version is fetched from the bump-my-version configuration file.

During a release, two commits are bundled into a single push event:

  1. [changelog] Release vX.Y.Z: freezes the version to the release number

  2. [changelog] Post-release bump vX.Y.Z → vX.Y.Z: the next dev version

In this situation, the current version returned is the one from the most recent commit (the post-release bump), which represents the next development version. Use released_version to get the version from the release commit.

The commit matrix lists the new commits oldest first, so the most recent one is its last entry.

property released_version: str | None[source]¶

Returns the version of the release commit.

During a release push event, this extracts the version from the [changelog] Release vX.Y.Z commit, which is distinct from current_version (the post-release bump version).

Returns None if no release commit is found in the current event.

dump_factories()[source]¶

Lazy value factories for every metadata key, in output order.

Each value is computed only when its key is included, so keys=("is_python_project",) skips nuitka_matrix and the git history walk it pulls in.

Split out of dump() so the key inventory is inspectable without computing anything: tests/test_metadata.py asserts these names match _METADATA_KEY_DESCRIPTIONS plus METADATA_CONFIG_FIELDS, which is what keeps --list-keys, all_metadata_keys() and the emitted output from drifting apart.

Derived from _METADATA_KEY_DESCRIPTIONS rather than re-listing every key: most keys read the attribute of the same name, on this instance or on files for the FILE_GROUP_KEYS, so only the handful whose value is not a plain attribute carry an explicit factory.

Return type:

dict[str, Callable[[], Any]]

Returns:

Key name to a zero-argument callable producing its value.

dump(dialect=Dialect.github, keys=())[source]¶

Returns metadata in the specified format.

Defaults to GitHub dialect. When keys is non-empty, only the requested keys are computed and included in the output. Filtered-out keys are never accessed, so callers requesting a small subset avoid triggering expensive dependent computations (git history walks, file system scans, build matrix expansion). See dump_factories().

Return type:

str