Skip to content

Commit 8c0f4ef

Browse files
committed
default to mono repo
1 parent 77d44f7 commit 8c0f4ef

File tree

2 files changed

+87
-12
lines changed

2 files changed

+87
-12
lines changed

.generator/cli.py

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import tempfile
2626
import yaml
2727
from datetime import date, datetime
28+
from functools import lru_cache
2829
from pathlib import Path
2930
from typing import Dict, List
3031
import build.util
@@ -493,18 +494,49 @@ def _create_repo_metadata_from_service_config(
493494
"api_shortname": api_shortname,
494495
}
495496

496-
def _get_repo_metadata_file_path(output: str, library_id: str, is_mono_repo: bool):
497+
498+
def _get_repo_metadata_file_path(base: str, library_id: str, is_mono_repo: bool):
499+
"""Constructs the full path to the .repo-metadata.json file.
500+
501+
Args:
502+
base (str): The base directory where the library is located.
503+
library_id (str): The ID of the library.
504+
is_mono_repo (bool): True if the current repository is a mono-repo.
505+
506+
Returns:
507+
str: The absolute path to the .repo-metadata.json file.
508+
"""
497509
path_to_library = f"packages/{library_id}" if is_mono_repo else "."
498-
return f"{output}/{path_to_library}/.repo-metadata.json"
510+
return f"{base}/{path_to_library}/.repo-metadata.json"
511+
512+
513+
@lru_cache(maxsize=None)
514+
def _get_repo_name_from_repo_metadata(base: str, library_id: str, is_mono_repo: bool):
515+
"""Retrieves the repository name from the .repo-metadata.json file.
516+
517+
This function is cached to avoid redundant file I/O.
499518
500-
def _get_repo_name_from_repo_metadata(output: str, library_id: str, is_mono_repo: bool):
501-
file_path = _get_repo_metadata_file_path(output, library_id, is_mono_repo)
519+
Args:
520+
base (str): The base directory where the library is located.
521+
library_id (str): The ID of the library.
522+
is_mono_repo (bool): True if the current repository is a mono-repo.
523+
524+
Returns:
525+
str: The name of the repository (e.g., 'googleapis/google-cloud-python').
526+
527+
Raises:
528+
ValueError: If the '.repo-metadata.json' file is missing the 'repo' field.
529+
"""
530+
if is_mono_repo:
531+
return "google-cloud-python"
532+
file_path = _get_repo_metadata_file_path(base, library_id, is_mono_repo)
502533
repo_metadata = _read_json_file(file_path)
503534
repo_name = repo_metadata.get("repo")
504535
if not repo_name:
505536
raise ValueError("`.repo-metadata.json` file is missing required 'repo' field.")
506537
return repo_name
507538

539+
508540
def _generate_repo_metadata_file(
509541
output: str, library_id: str, source: str, apis: List[Dict], is_mono_repo: bool
510542
):
@@ -518,7 +550,9 @@ def _generate_repo_metadata_file(
518550
is_mono_repo(bool): True if the current repository is a mono-repo.
519551
"""
520552
path_to_library = f"packages/{library_id}" if is_mono_repo else "."
521-
output_repo_metadata = _get_repo_metadata_file_path(output, library_id, is_mono_repo)
553+
output_repo_metadata = _get_repo_metadata_file_path(
554+
output, library_id, is_mono_repo
555+
)
522556

523557
# TODO(https://github.com/googleapis/librarian/issues/2334)): If `.repo-metadata.json`
524558
# already exists in the `output` dir, then this means that it has been successfully copied
@@ -1053,7 +1087,11 @@ def _verify_library_namespace(library_id: str, repo: str, is_mono_repo: bool):
10531087
# Exclude proto paths which are not intended to be used for code generation.
10541088
# Generally any protos under the `samples` or `tests` directories or in a
10551089
# directory called `proto` are not used for code generation.
1056-
if proto_path.startswith("tests") or proto_path.startswith("samples") or proto_path.endswith("proto"):
1090+
if (
1091+
proto_path.startswith("tests")
1092+
or proto_path.startswith("samples")
1093+
or proto_path.endswith("proto")
1094+
):
10571095
continue
10581096
relevant_dirs.add(proto_file.parent)
10591097

@@ -1344,7 +1382,7 @@ def _process_changelog(
13441382
version: str,
13451383
previous_version: str,
13461384
library_id: str,
1347-
repo_name: str
1385+
repo_name: str,
13481386
):
13491387
"""This function searches the given content for the anchor pattern
13501388
`[1]: https://pypi.org/project/{library_id}/#history`
@@ -1372,7 +1410,10 @@ def _process_changelog(
13721410
entry_parts = []
13731411
entry_parts.append(
13741412
_create_main_version_header(
1375-
version=version, previous_version=previous_version, library_id=library_id, repo_name=repo_name
1413+
version=version,
1414+
previous_version=previous_version,
1415+
library_id=library_id,
1416+
repo_name=repo_name,
13761417
)
13771418
)
13781419

@@ -1444,7 +1485,7 @@ def _update_changelog_for_library(
14441485

14451486
changelog_src = f"{repo}/{relative_path}"
14461487
changelog_dest = f"{output}/{relative_path}"
1447-
repo_name = _get_repo_name_from_repo_metadata(output, library_id, is_mono_repo)
1488+
repo_name = _get_repo_name_from_repo_metadata(repo, library_id, is_mono_repo)
14481489
updated_content = _process_changelog(
14491490
_read_text_file(changelog_src),
14501491
library_changes,

.generator/test_cli.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
_get_libraries_to_prepare_for_release,
5050
_get_new_library_config,
5151
_get_previous_version,
52+
_get_repo_name_from_repo_metadata,
5253
_get_staging_child_directory,
5354
_add_new_library_version,
5455
_prepare_new_library_config,
@@ -141,6 +142,13 @@
141142
)"""
142143

143144

145+
@pytest.fixture(autouse=True)
146+
def _clear_lru_cache():
147+
"""Automatically clears the cache of all LRU-cached functions after each test."""
148+
yield
149+
_get_repo_name_from_repo_metadata.cache_clear()
150+
151+
144152
@pytest.fixture
145153
def mock_generate_request_file(tmp_path, monkeypatch):
146154
"""Creates the mock request file at the correct path inside a temp dir."""
@@ -1265,6 +1273,9 @@ def test_update_changelog_for_library_single_repo(mocker):
12651273
mock_read = mocker.patch("cli._read_text_file", return_value=mock_content)
12661274
mock_write = mocker.patch("cli._write_text_file")
12671275
mock_path_exists = mocker.patch("cli.os.path.lexists", return_value=True)
1276+
mocker.patch(
1277+
"cli._get_repo_name_from_repo_metadata", return_value="google-cloud-python"
1278+
)
12681279
_update_changelog_for_library(
12691280
"repo",
12701281
"output",
@@ -1300,15 +1311,20 @@ def test_process_changelog_success():
13001311
library_id = "google-cloud-language"
13011312

13021313
result = _process_changelog(
1303-
mock_content, _MOCK_LIBRARY_CHANGES, version, previous_version, library_id
1314+
mock_content,
1315+
_MOCK_LIBRARY_CHANGES,
1316+
version,
1317+
previous_version,
1318+
library_id,
1319+
"google-cloud-python",
13041320
)
13051321
assert result == expected_result
13061322

13071323

13081324
def test_process_changelog_failure():
13091325
"""Tests that value error is raised if the changelog anchor string cannot be found"""
13101326
with pytest.raises(ValueError):
1311-
_process_changelog("", [], "", "", "")
1327+
_process_changelog("", [], "", "", "", "google-cloud-python")
13121328

13131329

13141330
def test_update_changelog_for_library_failure(mocker):
@@ -1351,7 +1367,9 @@ def test_create_main_version_header():
13511367
previous_version = "1.2.2"
13521368
version = "1.2.3"
13531369
library_id = "google-cloud-language"
1354-
actual_header = _create_main_version_header(version, previous_version, library_id)
1370+
actual_header = _create_main_version_header(
1371+
version, previous_version, library_id, "google-cloud-python"
1372+
)
13551373
assert actual_header == expected_header
13561374

13571375

@@ -1885,3 +1903,19 @@ def test_copy_readme_to_docs_source_not_exists(mocker, is_mono_repo):
18851903
mock_os_remove.assert_not_called()
18861904
mock_makedirs.assert_not_called()
18871905
mock_shutil_copy.assert_not_called()
1906+
1907+
1908+
def test_get_repo_name_from_repo_metadata_success(mocker):
1909+
"""Tests that the repo name is returned when it exists."""
1910+
mocker.patch(
1911+
"cli._read_json_file", return_value={"repo": "googleapis/google-cloud-python"}
1912+
)
1913+
repo_name = _get_repo_name_from_repo_metadata("base", "library_id", False)
1914+
assert repo_name == "googleapis/google-cloud-python"
1915+
1916+
1917+
def test_get_repo_name_from_repo_metadata_missing_repo(mocker):
1918+
"""Tests that a ValueError is raised when the repo field is missing."""
1919+
mocker.patch("cli._read_json_file", return_value={})
1920+
with pytest.raises(ValueError):
1921+
_get_repo_name_from_repo_metadata("base", "library_id", False)

0 commit comments

Comments
 (0)