Skip to content

Commit 717aeca

Browse files
authored
chore(librarian): update release init changelog (#14799)
This PR ensures that both `CHANGELOG.md` and `docs/CHANGELOG.md` files are updated during release init.
1 parent 46945b3 commit 717aeca

File tree

2 files changed

+65
-21
lines changed

2 files changed

+65
-21
lines changed

.generator/cli.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,7 +1345,7 @@ def _update_changelog_for_library(
13451345
version: str,
13461346
previous_version: str,
13471347
library_id: str,
1348-
relative_path: str,
1348+
is_mono_repo: bool,
13491349
):
13501350
"""Prepends a new release entry with multiple, grouped changes, to a changelog.
13511351
@@ -1361,7 +1361,15 @@ def _update_changelog_for_library(
13611361
previous_version(str): The version in state.yaml for a given library
13621362
library_id(str): The id of the library where the changelog should
13631363
be updated.
1364+
is_mono_repo(bool): True if the current repository is a mono-repo.
13641365
"""
1366+
if is_mono_repo:
1367+
relative_path = f"packages/{library_id}/CHANGELOG.md"
1368+
docs_relative_path = f"packages/{library_id}/docs/CHANGELOG.md"
1369+
else:
1370+
relative_path = "CHANGELOG.md"
1371+
docs_relative_path = f"docs/CHANGELOG.md"
1372+
13651373
changelog_src = f"{repo}/{relative_path}"
13661374
changelog_dest = f"{output}/{relative_path}"
13671375
updated_content = _process_changelog(
@@ -1373,6 +1381,11 @@ def _update_changelog_for_library(
13731381
)
13741382
_write_text_file(changelog_dest, updated_content)
13751383

1384+
docs_changelog_src = f"{repo}/{docs_relative_path}"
1385+
if os.path.lexists(docs_changelog_src):
1386+
docs_changelog_dst = f"{output}/{docs_relative_path}"
1387+
_write_text_file(docs_changelog_dst, updated_content)
1388+
13761389

13771390
def _is_mono_repo(repo: str) -> bool:
13781391
"""Determines if a library is generated or handwritten.
@@ -1424,7 +1437,6 @@ def handle_release_init(
14241437
)
14251438

14261439
if is_mono_repo:
1427-
14281440
# only a mono repo has a global changelog
14291441
_update_global_changelog(
14301442
f"{repo}/CHANGELOG.md",
@@ -1449,10 +1461,8 @@ def handle_release_init(
14491461

14501462
if is_mono_repo:
14511463
path_to_library = f"packages/{library_id}"
1452-
changelog_relative_path = f"packages/{library_id}/CHANGELOG.md"
14531464
else:
14541465
path_to_library = "."
1455-
changelog_relative_path = "CHANGELOG.md"
14561466

14571467
_update_version_for_library(repo, output, path_to_library, version)
14581468
_update_changelog_for_library(
@@ -1462,7 +1472,7 @@ def handle_release_init(
14621472
version,
14631473
previous_version,
14641474
library_id,
1465-
relative_path=changelog_relative_path,
1475+
is_mono_repo=is_mono_repo,
14661476
)
14671477

14681478
except Exception as e:

.generator/test_cli.py

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ def test_handle_configure_no_new_library(mocker):
312312
with pytest.raises(ValueError, match="Configuring a new library failed."):
313313
handle_configure()
314314

315+
315316
def test_create_new_changelog_for_library(mocker):
316317
"""Tests that the changelog files are created correctly."""
317318
library_id = "google-cloud-language"
@@ -325,7 +326,9 @@ def test_create_new_changelog_for_library(mocker):
325326
docs_changelog_path = f"{output}/packages/{library_id}/docs/CHANGELOG.md"
326327

327328
# Check that makedirs was called for both parent directories
328-
mock_makedirs.assert_any_call(os.path.dirname(package_changelog_path), exist_ok=True)
329+
mock_makedirs.assert_any_call(
330+
os.path.dirname(package_changelog_path), exist_ok=True
331+
)
329332
mock_makedirs.assert_any_call(os.path.dirname(docs_changelog_path), exist_ok=True)
330333
assert mock_makedirs.call_count == 2
331334

@@ -1141,29 +1144,60 @@ def test_get_previous_version_failure(mock_state_file):
11411144
_get_previous_version("google-cloud-does-not-exist", LIBRARIAN_DIR)
11421145

11431146

1144-
def test_update_changelog_for_library_success(mocker):
1145-
m = mock_open()
1146-
1147+
def test_update_changelog_for_library_writes_both_changelogs(mocker):
1148+
"""Tests that _update_changelog_for_library writes to both changelogs."""
11471149
mock_content = """# Changelog
11481150
11491151
[PyPI History][1]
11501152
11511153
[1]: https://pypi.org/project/google-cloud-language/#history
1154+
"""
1155+
mock_read = mocker.patch("cli._read_text_file", return_value=mock_content)
1156+
mock_write = mocker.patch("cli._write_text_file")
1157+
mock_path_exists = mocker.patch("cli.os.path.lexists", return_value=True)
1158+
_update_changelog_for_library(
1159+
"repo",
1160+
"output",
1161+
_MOCK_LIBRARY_CHANGES,
1162+
"1.2.3",
1163+
"1.2.2",
1164+
"google-cloud-language",
1165+
is_mono_repo=True,
1166+
)
11521167

1153-
## [2.17.2](https://github.com/googleapis/google-cloud-python/compare/google-cloud-language-v2.17.1...google-cloud-language-v2.17.2) (2025-06-11)
1168+
assert mock_write.call_count == 2
1169+
mock_write.assert_any_call(
1170+
"output/packages/google-cloud-language/CHANGELOG.md", mocker.ANY
1171+
)
1172+
mock_write.assert_any_call(
1173+
"output/packages/google-cloud-language/docs/CHANGELOG.md", mocker.ANY
1174+
)
11541175

1176+
1177+
def test_update_changelog_for_library_single_repo(mocker):
1178+
"""Tests that _update_changelog_for_library writes to both changelogs in a single repo."""
1179+
mock_content = """# Changelog
1180+
1181+
[PyPI History][1]
1182+
1183+
[1]: https://pypi.org/project/google-cloud-language/#history
11551184
"""
1156-
with unittest.mock.patch("cli.open", m):
1157-
mocker.patch("cli._read_text_file", return_value=mock_content)
1158-
_update_changelog_for_library(
1159-
"repo",
1160-
"output",
1161-
_MOCK_LIBRARY_CHANGES,
1162-
"1.2.3",
1163-
"1.2.2",
1164-
"google-cloud-language",
1165-
"CHANGELOG.md",
1166-
)
1185+
mock_read = mocker.patch("cli._read_text_file", return_value=mock_content)
1186+
mock_write = mocker.patch("cli._write_text_file")
1187+
mock_path_exists = mocker.patch("cli.os.path.lexists", return_value=True)
1188+
_update_changelog_for_library(
1189+
"repo",
1190+
"output",
1191+
_MOCK_LIBRARY_CHANGES,
1192+
"1.2.3",
1193+
"1.2.2",
1194+
"google-cloud-language",
1195+
is_mono_repo=False,
1196+
)
1197+
1198+
assert mock_write.call_count == 2
1199+
mock_write.assert_any_call("output/CHANGELOG.md", mocker.ANY)
1200+
mock_write.assert_any_call("output/docs/CHANGELOG.md", mocker.ANY)
11671201

11681202

11691203
def test_process_changelog_success():

0 commit comments

Comments
 (0)