5454OUTPUT_DIR = "output"
5555REPO_DIR = "repo"
5656SOURCE_DIR = "source"
57-
58- _REPO_URL = "https://github.com/googleapis/google-cloud-python"
57+ _REPO_URL_SUFFIX = f"https://github.com/googleapis"
5958
6059
6160def _read_text_file (path : str ) -> str :
@@ -494,6 +493,17 @@ def _create_repo_metadata_from_service_config(
494493 "api_shortname" : api_shortname ,
495494 }
496495
496+ def _get_repo_metadata_file_path (output : str , library_id : str , is_mono_repo : bool ):
497+ path_to_library = f"packages/{ library_id } " if is_mono_repo else "."
498+ return f"{ output } /{ path_to_library } /.repo-metadata.json"
499+
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 )
502+ repo_metadata = _read_json_file (file_path )
503+ repo_name = repo_metadata .get ("repo" )
504+ if not repo_name :
505+ raise ValueError ("`.repo-metadata.json` file is missing required 'repo' field." )
506+ return repo_name
497507
498508def _generate_repo_metadata_file (
499509 output : str , library_id : str , source : str , apis : List [Dict ], is_mono_repo : bool
@@ -508,7 +518,7 @@ def _generate_repo_metadata_file(
508518 is_mono_repo(bool): True if the current repository is a mono-repo.
509519 """
510520 path_to_library = f"packages/{ library_id } " if is_mono_repo else "."
511- output_repo_metadata = f" { output } / { path_to_library } /.repo-metadata.json"
521+ output_repo_metadata = _get_repo_metadata_file_path ( output , library_id , is_mono_repo )
512522
513523 # TODO(https://github.com/googleapis/librarian/issues/2334)): If `.repo-metadata.json`
514524 # already exists in the `output` dir, then this means that it has been successfully copied
@@ -1306,7 +1316,7 @@ def _get_previous_version(library_id: str, librarian: str) -> str:
13061316
13071317
13081318def _create_main_version_header (
1309- version : str , previous_version : str , library_id : str
1319+ version : str , previous_version : str , library_id : str , repo_name : str
13101320) -> str :
13111321 """This function creates a header to be used in a changelog. The header has the following format:
13121322 `## [{version}](https://github.com/googleapis/google-cloud-python/compare/{library_id}-v{previous_version}...{library_id}-v{version}) (YYYY-MM-DD)`
@@ -1323,7 +1333,7 @@ def _create_main_version_header(
13231333 current_date = datetime .now ().strftime ("%Y-%m-%d" )
13241334 # Return the main version header
13251335 return (
1326- f"## [{ version } ]({ _REPO_URL } /compare/{ library_id } -v{ previous_version } "
1336+ f"## [{ version } ]({ _REPO_URL_SUFFIX } / { repo_name } /compare/{ library_id } -v{ previous_version } "
13271337 f"...{ library_id } -v{ version } ) ({ current_date } )"
13281338 )
13291339
@@ -1334,6 +1344,7 @@ def _process_changelog(
13341344 version : str ,
13351345 previous_version : str ,
13361346 library_id : str ,
1347+ repo_name : str
13371348):
13381349 """This function searches the given content for the anchor pattern
13391350 `[1]: https://pypi.org/project/{library_id}/#history`
@@ -1361,7 +1372,7 @@ def _process_changelog(
13611372 entry_parts = []
13621373 entry_parts .append (
13631374 _create_main_version_header (
1364- version = version , previous_version = previous_version , library_id = library_id
1375+ version = version , previous_version = previous_version , library_id = library_id , repo_name = repo_name
13651376 )
13661377 )
13671378
@@ -1383,7 +1394,7 @@ def _process_changelog(
13831394 if adjusted_change_type in change_type_map :
13841395 entry_parts .append (f"\n \n ### { change_type_map [adjusted_change_type ]} \n " )
13851396 for change in library_changes :
1386- commit_link = f"([{ change [commit_hash_key ]} ]({ _REPO_URL } /commit/{ change [commit_hash_key ]} ))"
1397+ commit_link = f"([{ change [commit_hash_key ]} ]({ _REPO_URL_SUFFIX } / { repo_name } /commit/{ change [commit_hash_key ]} ))"
13871398 entry_parts .append (f"* { change [subject_key ]} { commit_link } " )
13881399
13891400 new_entry_text = "\n " .join (entry_parts )
@@ -1433,12 +1444,14 @@ def _update_changelog_for_library(
14331444
14341445 changelog_src = f"{ repo } /{ relative_path } "
14351446 changelog_dest = f"{ output } /{ relative_path } "
1447+ repo_name = _get_repo_name_from_repo_metadata (output , library_id , is_mono_repo )
14361448 updated_content = _process_changelog (
14371449 _read_text_file (changelog_src ),
14381450 library_changes ,
14391451 version ,
14401452 previous_version ,
14411453 library_id ,
1454+ repo_name ,
14421455 )
14431456 _write_text_file (changelog_dest , updated_content )
14441457
0 commit comments