diff --git a/.generator/cli.py b/.generator/cli.py index 2566e72e652e..492ddbf2f582 100644 --- a/.generator/cli.py +++ b/.generator/cli.py @@ -1354,10 +1354,14 @@ def _get_previous_version(library_id: str, librarian: str) -> str: def _create_main_version_header( - version: str, previous_version: str, library_id: str, repo_name: str + version: str, + previous_version: str, + library_id: str, + repo_name: str, + tag_format: str, ) -> str: """This function creates a header to be used in a changelog. The header has the following format: - `## [{version}](https://github.com/googleapis/google-cloud-python/compare/{library_id}-v{previous_version}...{library_id}-v{version}) (YYYY-MM-DD)` + `## [{version}](https://github.com/googleapis/google-cloud-python/compare/{tag_format}{previous_version}...{tag_format}{version}) (YYYY-MM-DD)` Args: version(str): The new version of the library. @@ -1365,15 +1369,23 @@ def _create_main_version_header( library_id(str): The id of the library where the changelog should be updated. repo_name(str): The name of the repository (e.g., 'googleapis/google-cloud-python'). + tag_format(str): The format of the git tag. Returns: A header to be used in the changelog. """ current_date = datetime.now().strftime("%Y-%m-%d") + + # We will assume that version is always at the end of the tag. + tag_format = tag_format.replace("{version}", "") + + if "{id}" in tag_format: + tag_format = tag_format.format(**{"id": library_id}) + # Return the main version header return ( - f"## [{version}]({_GITHUB_BASE}/{repo_name}/compare/{library_id}-v{previous_version}" - f"...{library_id}-v{version}) ({current_date})" + f"## [{version}]({_GITHUB_BASE}/{repo_name}/compare/{tag_format}{previous_version}" + f"...{tag_format}{version}) ({current_date})" ) @@ -1384,12 +1396,13 @@ def _process_changelog( previous_version: str, library_id: str, repo_name: str, + tag_format: str, ): """This function searches the given content for the anchor pattern `[1]: https://pypi.org/project/{library_id}/#history` and adds an entry in the following format: - ## [{version}](https://github.com/googleapis/google-cloud-python/compare/{library_id}-v{previous_version}...{library_id}-v{version}) (YYYY-MM-DD) + ## [{version}](https://github.com/googleapis/google-cloud-python/compare/{tag_format}{previous_version}...{tag_format}{version}) (YYYY-MM-DD) ### Documentation @@ -1404,6 +1417,7 @@ def _process_changelog( library_id(str): The id of the library where the changelog should be updated. repo_name(str): The name of the repository (e.g., 'googleapis/google-cloud-python'). + tag_format(str): The format of the git tag. Raises: ValueError if the anchor pattern string could not be found in the given content @@ -1416,6 +1430,7 @@ def _process_changelog( previous_version=previous_version, library_id=library_id, repo_name=repo_name, + tag_format=tag_format, ) ) @@ -1461,6 +1476,7 @@ def _update_changelog_for_library( previous_version: str, library_id: str, is_mono_repo: bool, + tag_format: str, ): """Prepends a new release entry with multiple, grouped changes, to a changelog. @@ -1477,6 +1493,7 @@ def _update_changelog_for_library( library_id(str): The id of the library where the changelog should be updated. is_mono_repo(bool): True if the current repository is a mono-repo. + tag_format(str): The format of the git tag. """ if is_mono_repo: relative_path = f"packages/{library_id}/CHANGELOG.md" @@ -1495,6 +1512,7 @@ def _update_changelog_for_library( previous_version, library_id, repo_name, + tag_format, ) _write_text_file(changelog_dest, updated_content) @@ -1567,6 +1585,7 @@ def handle_release_stage( version = library_release_data["version"] library_id = library_release_data["id"] library_changes = library_release_data["changes"] + tag_format = library_release_data["tag_format"] # Get previous version from state.yaml previous_version = _get_previous_version(library_id, librarian) @@ -1586,7 +1605,8 @@ def handle_release_stage( version, previous_version, library_id, - is_mono_repo=is_mono_repo, + is_mono_repo, + tag_format, ) except Exception as e: diff --git a/.generator/test_cli.py b/.generator/test_cli.py index eb8db2127c95..b2af744ec855 100644 --- a/.generator/test_cli.py +++ b/.generator/test_cli.py @@ -259,6 +259,7 @@ def mock_release_stage_request_file(tmp_path, monkeypatch): "release_triggered": False, "version": "1.2.3", "changes": [], + "tag_format": "{id}-v{version}", }, { "id": "google-cloud-language", @@ -266,6 +267,7 @@ def mock_release_stage_request_file(tmp_path, monkeypatch): "release_triggered": True, "version": "1.2.3", "changes": [], + "tag_format": "{id}-v{version}", }, ] } @@ -958,6 +960,7 @@ def test_handle_release_stage_fail_value_error_version(mocker): "release_triggered": True, "version": "1.2.2", "changes": [], + "tag_format": "{id}-v{version}", }, ] } @@ -1250,7 +1253,8 @@ def test_update_changelog_for_library_writes_both_changelogs(mocker): "1.2.3", "1.2.2", "google-cloud-language", - is_mono_repo=True, + True, + "{id}-v{version}", ) assert mock_write.call_count == 2 @@ -1274,7 +1278,8 @@ def test_update_changelog_for_library_single_repo(mocker): mock_write = mocker.patch("cli._write_text_file") mock_path_exists = mocker.patch("cli.os.path.lexists", return_value=True) mocker.patch( - "cli._get_repo_name_from_repo_metadata", return_value="googleapis/google-cloud-python" + "cli._get_repo_name_from_repo_metadata", + return_value="googleapis/google-cloud-python", ) _update_changelog_for_library( "repo", @@ -1283,7 +1288,8 @@ def test_update_changelog_for_library_single_repo(mocker): "1.2.3", "1.2.2", "google-cloud-language", - is_mono_repo=False, + False, + "v{version}", ) assert mock_write.call_count == 2 @@ -1309,6 +1315,7 @@ def test_process_changelog_success(): version = "1.2.3" previous_version = "1.2.2" library_id = "google-cloud-language" + tag_format = "{id}-v{version}" result = _process_changelog( mock_content, @@ -1317,6 +1324,7 @@ def test_process_changelog_success(): previous_version, library_id, "googleapis/google-cloud-python", + tag_format, ) assert result == expected_result @@ -1324,7 +1332,7 @@ def test_process_changelog_success(): def test_process_changelog_failure(): """Tests that value error is raised if the changelog anchor string cannot be found""" with pytest.raises(ValueError): - _process_changelog("", [], "", "", "", "googleapis/google-cloud-python") + _process_changelog("", [], "", "", "", "googleapis/google-cloud-python", "") def test_update_changelog_for_library_failure(mocker): @@ -1342,7 +1350,8 @@ def test_update_changelog_for_library_failure(mocker): "1.2.3", "1.2.2", "google-cloud-language", - "CHANGELOG.md", + True, + "{id}-v{version}", ) @@ -1361,14 +1370,23 @@ def test_process_version_file_failure(): _process_version_file("", "", Path("")) -def test_create_main_version_header(): +@pytest.mark.parametrize( + "tag_format,expected_tag_result", + [(r"{id}-v{version}", "google-cloud-language-v"), (r"v{version}", "v")], +) +def test_create_main_version_header(tag_format, expected_tag_result): current_date = datetime.now().strftime("%Y-%m-%d") - expected_header = f"## [1.2.3](https://github.com/googleapis/google-cloud-python/compare/google-cloud-language-v1.2.2...google-cloud-language-v1.2.3) ({current_date})" + expected_header = f"## [1.2.3](https://github.com/googleapis/google-cloud-python/compare/{expected_tag_result}1.2.2...{expected_tag_result}1.2.3) ({current_date})" previous_version = "1.2.2" version = "1.2.3" library_id = "google-cloud-language" + tag_format = tag_format actual_header = _create_main_version_header( - version, previous_version, library_id, "googleapis/google-cloud-python" + version, + previous_version, + library_id, + "googleapis/google-cloud-python", + tag_format, ) assert actual_header == expected_header