Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/pulp_docs/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,32 @@ class OpenAPIGenerator:
DRY_RUN_SPECFILE_TEMPLATE = "dry-run specfile for: {plugin_label}"

def __init__(self, plugins: list[OpenApiPlugin], dry_run: bool = False):
self.plugins = plugins
self.repository_paths = list({p.repository_path for p in plugins if p.repository_path})
missing = [p.plugin_label for p in plugins if not p.repository_path]
if missing:
raise ValueError(f"Plugins missing repository path: {missing}")
self.label_to_path = {p.plugin_label: p.repository_path for p in plugins}
self.dry_run = dry_run

def generate(self) -> dict[str, Path]:
"""Generate openapi json files.

Returns map of plugin labels to generated spec path.
"""
if not self.plugins:
if not self.label_to_path:
return {}
output_dir = Path(tempfile.mkdtemp())
label_to_specfile = {}
for plugin in self.plugins:
filename = f"{plugin.plugin_label}-api.json"
for label in self.label_to_path:
filename = f"{label}-api.json"
file_path = output_dir / filename
self._generate_schema(plugin.plugin_label, file_path)
label_to_specfile[plugin.plugin_label] = file_path
self._generate_schema(label, file_path)
label_to_specfile[label] = file_path
return label_to_specfile

def _generate_schema(self, plugin_label: str, output_file: Path):
cmd = ["uv", "run", "--isolated", "--with", "setuptools"]
for repo_path in self.repository_paths:
cmd.extend(["--with", str(repo_path.resolve())])
repo_path = self.label_to_path[plugin_label]
cmd.extend(["--with", str(repo_path.resolve())])
cmd.extend(
["pulpcore-manager", "openapi", "--component", plugin_label, "--file", str(output_file)]
)
Expand Down
9 changes: 5 additions & 4 deletions tests/test_openapi_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ def repositories(tmp_path_factory: pytest.TempPathFactory) -> dict[str, OpenApiP


class TestOpenAPIGenerator:
def test_deduplicate_repository_paths(self, repositories: dict[str, OpenApiPlugin]):
"""pulpcore and pulp_file share the same repository path."""
def test_label_to_path_mapping(self, repositories: dict[str, OpenApiPlugin]):
"""Each plugin label maps to its repository path."""
filter = ["rpm", "file", "core"]
plugins = [p for p in repositories.values() if p.plugin_label in filter]
gen = OpenAPIGenerator(plugins, dry_run=True)
# pulpcore and pulp_file share the same repository
assert len(gen.repository_paths) == 2
assert len(gen.label_to_path) == len(plugins)
for plugin in plugins:
assert gen.label_to_path[plugin.plugin_label] == plugin.repository_path

@pytest.mark.parametrize(
"filter",
Expand Down
Loading