Skip to content

Commit bfe9f2a

Browse files
committed
feat: add support to release handwritten clients
1 parent b46d293 commit bfe9f2a

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

.generator/cli.py

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,9 @@ def _process_version_file(content, version, version_path) -> str:
11091109
11101110
Returns: A string with the modified content.
11111111
"""
1112-
if version_path.name.endswith("gapic_version.py"):
1112+
if version_path.name.endswith("gapic_version.py") or version_path.name.endswith(
1113+
"version.py"
1114+
):
11131115
pattern = r"(__version__\s*=\s*[\"'])([^\"']+)([\"'].*)"
11141116
else:
11151117
pattern = r"(version\s*=\s*[\"'])([^\"']+)([\"'].*)"
@@ -1125,7 +1127,7 @@ def _process_version_file(content, version, version_path) -> str:
11251127
def _update_version_for_library(
11261128
repo: str, output: str, path_to_library: str, version: str
11271129
):
1128-
"""Updates the version string in `**/gapic_version.py`, `setup.py`,
1130+
"""Updates the version string in `**/gapic_version.py`, `**/version.py`, `setup.py`,
11291131
`pyproject.toml` and `samples/**/snippet_metadata.json` for a
11301132
given library, if applicable.
11311133
@@ -1139,12 +1141,15 @@ def _update_version_for_library(
11391141
version(str): The new version of the library
11401142
11411143
Raises: `ValueError` if a version string could not be located in `**/gapic_version.py`
1142-
within the given library.
1144+
or `**/version.py` within the given library.
11431145
"""
11441146

1145-
# Find and update gapic_version.py files
1146-
version_files = list(Path(f"{repo}/{path_to_library}").rglob("**/gapic_version.py"))
1147-
if len(version_files) == 0:
1147+
# Find and update version.py or gapic_version.py files
1148+
search_base = Path(f"{repo}/{path_to_library}")
1149+
version_files = list(search_base.rglob("**/gapic_version.py"))
1150+
version_files.extend(list(search_base.glob("google/**/version.py")))
1151+
1152+
if not version_files:
11481153
# Fallback to `pyproject.toml`` or `setup.py``. Proto-only libraries have
11491154
# version information in `setup.py` or `pyproject.toml` instead of `gapic_version.py`.
11501155
pyproject_toml = Path(f"{repo}/{path_to_library}/pyproject.toml")
@@ -1160,7 +1165,7 @@ def _update_version_for_library(
11601165

11611166
# Find and update snippet_metadata.json files
11621167
snippet_metadata_files = Path(f"{repo}/{path_to_library}").rglob(
1163-
"samples/**/*.json"
1168+
"samples/**/*snippet*.json"
11641169
)
11651170
for metadata_file in snippet_metadata_files:
11661171
output_path = f"{output}/{metadata_file.relative_to(repo)}"
@@ -1300,6 +1305,7 @@ def _update_changelog_for_library(
13001305
version: str,
13011306
previous_version: str,
13021307
library_id: str,
1308+
relative_path: str,
13031309
):
13041310
"""Prepends a new release entry with multiple, grouped changes, to a changelog.
13051311
@@ -1316,8 +1322,6 @@ def _update_changelog_for_library(
13161322
library_id(str): The id of the library where the changelog should
13171323
be updated.
13181324
"""
1319-
1320-
relative_path = f"packages/{library_id}/CHANGELOG.md"
13211325
changelog_src = f"{repo}/{relative_path}"
13221326
changelog_dest = f"{output}/{relative_path}"
13231327
updated_content = _process_changelog(
@@ -1357,27 +1361,28 @@ def handle_release_init(
13571361
`release-init-request.json` file in the given
13581362
librarian directory cannot be read.
13591363
"""
1360-
13611364
try:
1365+
is_generated = Path(f"{repo}/packages").exists()
1366+
13621367
# Read a release-init-request.json file
13631368
request_data = _read_json_file(f"{librarian}/{RELEASE_INIT_REQUEST_FILE}")
13641369
libraries_to_prep_for_release = _get_libraries_to_prepare_for_release(
13651370
request_data
13661371
)
13671372

1368-
_update_global_changelog(
1369-
f"{repo}/CHANGELOG.md",
1370-
f"{output}/CHANGELOG.md",
1371-
libraries_to_prep_for_release,
1372-
)
1373+
if is_generated:
1374+
_update_global_changelog(
1375+
f"{repo}/CHANGELOG.md",
1376+
f"{output}/CHANGELOG.md",
1377+
libraries_to_prep_for_release,
1378+
)
13731379

13741380
# Prepare the release for each library by updating the
13751381
# library specific version files and library specific changelog.
13761382
for library_release_data in libraries_to_prep_for_release:
13771383
version = library_release_data["version"]
13781384
library_id = library_release_data["id"]
13791385
library_changes = library_release_data["changes"]
1380-
path_to_library = f"packages/{library_id}"
13811386

13821387
# Get previous version from state.yaml
13831388
previous_version = _get_previous_version(library_id, librarian)
@@ -1387,6 +1392,13 @@ def handle_release_init(
13871392
f"{library_id} version: {previous_version}\n"
13881393
)
13891394

1395+
if is_generated:
1396+
path_to_library = f"packages/{library_id}"
1397+
changelog_relative_path = f"packages/{library_id}/CHANGELOG.md"
1398+
else:
1399+
path_to_library = "."
1400+
changelog_relative_path = "CHANGELOG.md"
1401+
13901402
_update_version_for_library(repo, output, path_to_library, version)
13911403
_update_changelog_for_library(
13921404
repo,
@@ -1395,6 +1407,7 @@ def handle_release_init(
13951407
version,
13961408
previous_version,
13971409
library_id,
1410+
relative_path=changelog_relative_path,
13981411
)
13991412

14001413
except Exception as e:

.generator/test_cli.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,7 @@ def test_update_changelog_for_library_success(mocker):
11081108
"1.2.3",
11091109
"1.2.2",
11101110
"google-cloud-language",
1111+
"CHANGELOG.md",
11111112
)
11121113

11131114

@@ -1157,6 +1158,7 @@ def test_update_changelog_for_library_failure(mocker):
11571158
"1.2.3",
11581159
"1.2.2",
11591160
"google-cloud-language",
1161+
"CHANGELOG.md",
11601162
)
11611163

11621164

0 commit comments

Comments
 (0)