Skip to content

Commit 770cc02

Browse files
committed
Update openapi module to read from mkdocs.yml
1 parent 592e8aa commit 770cc02

File tree

3 files changed

+55
-30
lines changed

3 files changed

+55
-30
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ dependencies = [
1717
"httpx",
1818
"rich",
1919
"GitPython~=3.1.44",
20+
"pytest>=8.4.1",
2021
]
2122
license = "GPL-3.0-or-later"
2223
license-files = ["LICENSE"]

src/pulp_docs/openapi.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,47 @@
77
import shutil
88
import subprocess
99
import tempfile
10-
from importlib.resources import files
1110
from pathlib import Path
1211
from typing import NamedTuple, Optional
1312

14-
from pulp_docs.repository import Repos
13+
from mkdocs.config import load_config
1514

1615
BASE_TMPDIR_NAME = "pulpdocs_tmp"
16+
CURRENT_DIR = Path(__file__).parent.absolute()
1717

1818

1919
def main(output_dir: Path, plugins_filter: Optional[list[str]] = None, dry_run: bool = False):
2020
"""Creates openapi json files for all or selected plugins in output dir."""
21-
repolist = str(files("pulp_docs").joinpath("data/repolist.yml"))
22-
repos = Repos.from_yaml(repolist).get_repos(["content"])
23-
if plugins_filter:
24-
repos = [p for p in repos if p.name in plugins_filter]
21+
22+
def get_plugin_name(path: str) -> str:
23+
return path.rpartition("/")[-1]
24+
25+
def is_subpackage(path: str) -> bool:
26+
return "/" in path
27+
28+
def filter_plugin(path: str) -> bool:
29+
if not plugins_filter:
30+
return True
31+
return get_plugin_name(path) in plugins_filter or path == "pulpcore"
32+
33+
def get_plugins():
34+
mkdocs_yml = str(CURRENT_DIR.parent.parent / "mkdocs.yml")
35+
pulpdocs_plugin = load_config(mkdocs_yml).plugins["PulpDocs"]
36+
all_components = pulpdocs_plugin.config.components
37+
return [c for c in all_components if c.rest_api]
38+
39+
all_plugins = get_plugins()
40+
all_plugins = [p for p in all_plugins if filter_plugin(p.path)]
2541

2642
pulp_plugins = []
27-
for repo in repos:
28-
name = repo.name
29-
label = name.split("_")[-1]
30-
is_subpackage = bool(getattr(repo, "subpackage_of", False))
31-
pulp_plugins.append(PulpPlugin(name, label, is_subpackage))
43+
for plugin in all_plugins:
44+
plugin = PulpPlugin(
45+
name=get_plugin_name(plugin.path),
46+
label=plugin.rest_api,
47+
is_subpackage=is_subpackage(plugin.path),
48+
git_url=plugin.git_url,
49+
)
50+
pulp_plugins.append(plugin)
3251

3352
openapi = OpenAPIGenerator(plugins=pulp_plugins, dry_run=dry_run)
3453
openapi.generate(target_dir=output_dir)
@@ -47,10 +66,10 @@ class PulpPlugin(NamedTuple):
4766
name: str
4867
label: str
4968
is_subpackage: bool
50-
remote_template: str = "https://github.com/pulp/{name}"
69+
git_url: str
5170

5271
def get_remote_url(self):
53-
return self.remote_template.format(name=self.name)
72+
return self.git_url
5473

5574

5675
class OpenAPIGenerator:
@@ -64,7 +83,7 @@ class OpenAPIGenerator:
6483
"""
6584

6685
def __init__(self, plugins: list[PulpPlugin], dry_run=False):
67-
self.pulpcore = PulpPlugin("pulpcore", "core", False)
86+
self.pulpcore = next(filter(lambda p: p.name == "pulpcore", plugins))
6887
self.plugins = plugins + [self.pulpcore]
6988
self.dry_run = dry_run
7089

tests/test_openapi_generation.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,28 @@
44
from pulp_docs.openapi import main as openapi_main
55

66

7-
def test_openapi_generation(tmp_path: Path, monkeypatch):
8-
output_dir = tmp_path / "openapi"
9-
output_dir.mkdir()
10-
assert len(list(output_dir.glob("*.json"))) == 0
11-
12-
with monkeypatch.context() as m:
13-
m.setenv("TMPDIR", str(tmp_path))
7+
class TestOpenApiGeneration:
8+
def test_dry_run(self, tmp_path: Path, monkeypatch):
9+
output_dir = tmp_path / "openapi"
1410
plugins_filter = ["pulp_rpm", "pulp_file"]
1511
openapi_main(output_dir=output_dir, plugins_filter=plugins_filter, dry_run=True)
16-
openapi_main(output_dir=output_dir, plugins_filter=plugins_filter)
1712

18-
output_paths = [f for f in output_dir.glob("*.json")]
19-
output_ls = [f.name for f in output_paths]
20-
output_labels = [f.rpartition("-")[0] for f in output_ls]
21-
assert len(output_ls) == 3
22-
assert {"core-api.json", "rpm-api.json", "file-api.json"} == set(output_ls)
13+
def test_sample_generation(self, tmp_path: Path, monkeypatch):
14+
output_dir = tmp_path / "openapi"
15+
output_dir.mkdir()
16+
assert len(list(output_dir.glob("*.json"))) == 0
17+
18+
with monkeypatch.context() as m:
19+
m.setenv("TMPDIR", str(tmp_path))
20+
plugins_filter = ["pulp_rpm", "pulp_file"]
21+
openapi_main(output_dir=output_dir, plugins_filter=plugins_filter)
22+
23+
output_paths = [f for f in output_dir.glob("*.json")]
24+
output_ls = [f.name for f in output_paths]
25+
output_labels = [f.rpartition("-")[0] for f in output_ls]
26+
assert len(output_ls) == 3
27+
assert {"core-api.json", "rpm-api.json", "file-api.json"} == set(output_ls)
2328

24-
for label, path in zip(output_labels, output_paths):
25-
openapi_data = json.loads(path.read_text())
26-
assert label in openapi_data["info"]["x-pulp-app-versions"].keys()
29+
for label, path in zip(output_labels, output_paths):
30+
openapi_data = json.loads(path.read_text())
31+
assert label in openapi_data["info"]["x-pulp-app-versions"].keys()

0 commit comments

Comments
 (0)