Skip to content

Commit 49e3f0d

Browse files
committed
feat: create repo-metadata during generation
1 parent 0f10cfd commit 49e3f0d

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

.generator/cli.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,68 @@ def _clean_up_files_after_post_processing(output: str, library_id: str):
359359
): # pragma: NO COVER
360360
os.remove(gapic_version_file)
361361

362+
def _create_repo_metadata_from_service_config(
363+
service_config_name: str, api_path: str, source: str, library_id: str
364+
) -> Dict:
365+
"""Creates the .repo-metadata.json content from the service config.
366+
367+
Args:
368+
service_config_name (str): The name of the service config file.
369+
api_path (str): The path to the API.
370+
source (str): The path to the source directory.
371+
library_id (str): The ID of the library.
372+
373+
Returns:
374+
Dict: The content of the .repo-metadata.json file.
375+
"""
376+
full_service_config_path = f"{source}/{api_path}/{service_config_name}"
377+
378+
# TODO(ohmayr): Read the api service config to backfill .repo-metadata.json
379+
return {
380+
"api_shortname": "",
381+
"name_pretty": "",
382+
"product_documentation": "",
383+
"api_description": "",
384+
"client_documentation": "",
385+
"issue_tracker": "",
386+
"release_level": "",
387+
"language": "python",
388+
"library_type": "GAPIC_AUTO",
389+
"repo": "googleapis/google-cloud-python",
390+
"distribution_name": "",
391+
"api_id": "",
392+
}
393+
394+
395+
def _generate_repo_metadata_file(
396+
output: str, library_id: str, source: str, apis: List[Dict]
397+
):
398+
"""Generates the .repo-metadata.json file from the primary API service config.
399+
400+
Args:
401+
output (str): The path to the output directory.
402+
library_id (str): The ID of the library.
403+
source (str): The path to the source directory.
404+
apis (List[Dict]): A list of APIs to generate.
405+
"""
406+
path_to_library = f"packages/{library_id}"
407+
output_repo_metadata = f"{output}/{path_to_library}/.repo-metadata.json"
408+
409+
os.makedirs(f"{output}/{path_to_library}", exist_ok=True)
410+
411+
# TODO(ohmayr): Programatically determine the primary api to be used to
412+
# to determine the information for metadata. For now, let's use the first
413+
# api in the list.
414+
primary_api = apis[0]
415+
416+
metadata_content = _create_repo_metadata_from_service_config(
417+
primary_api.get("service_config"),
418+
primary_api.get("path"),
419+
source,
420+
library_id,
421+
)
422+
_write_json_file(output_repo_metadata, metadata_content)
423+
362424

363425
def handle_generate(
364426
librarian: str = LIBRARIAN_DIR,
@@ -397,6 +459,7 @@ def handle_generate(
397459
api_path = api.get("path")
398460
if api_path:
399461
_generate_api(api_path, library_id, source, output)
462+
_generate_repo_metadata_file(output, library_id, source, apis_to_generate)
400463
_copy_files_needed_for_post_processing(output, input, library_id)
401464
_run_post_processor(output, library_id)
402465
_clean_up_files_after_post_processing(output, library_id)

.generator/test_cli.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@
3737
_clean_up_files_after_post_processing,
3838
_copy_files_needed_for_post_processing,
3939
_create_main_version_header,
40+
_create_repo_metadata_from_service_config,
4041
_determine_generator_command,
4142
_determine_library_namespace,
4243
_generate_api,
44+
_generate_repo_metadata_file,
4345
_get_api_generator_options,
4446
_get_library_dist_name,
4547
_get_library_id,
@@ -1071,6 +1073,52 @@ def test_determine_library_namespace_success(
10711073
assert namespace == expected_namespace
10721074

10731075

1076+
def test_create_repo_metadata_from_service_config():
1077+
"""Tests the creation of .repo-metadata.json content."""
1078+
service_config_name = "service_config.yaml"
1079+
api_path = "google/cloud/language/v1"
1080+
source = "/source"
1081+
library_id = "google-cloud-language"
1082+
1083+
metadata = _create_repo_metadata_from_service_config(
1084+
service_config_name, api_path, source, library_id
1085+
)
1086+
1087+
assert metadata["language"] == "python"
1088+
assert metadata["library_type"] == "GAPIC_AUTO"
1089+
assert metadata["repo"] == "googleapis/google-cloud-python"
1090+
1091+
1092+
def test_generate_repo_metadata_file(mocker):
1093+
"""Tests the generation of the .repo-metadata.json file."""
1094+
mock_write_json = mocker.patch("cli._write_json_file")
1095+
mock_create_metadata = mocker.patch(
1096+
"cli._create_repo_metadata_from_service_config",
1097+
return_value={"repo": "googleapis/google-cloud-python"},
1098+
)
1099+
mocker.patch("os.makedirs")
1100+
1101+
output = "/output"
1102+
library_id = "google-cloud-language"
1103+
source = "/source"
1104+
apis = [
1105+
{
1106+
"service_config": "service_config.yaml",
1107+
"path": "google/cloud/language/v1",
1108+
}
1109+
]
1110+
1111+
_generate_repo_metadata_file(output, library_id, source, apis)
1112+
1113+
mock_create_metadata.assert_called_once_with(
1114+
"service_config.yaml", "google/cloud/language/v1", source, library_id
1115+
)
1116+
mock_write_json.assert_called_once_with(
1117+
f"{output}/packages/{library_id}/.repo-metadata.json",
1118+
{"repo": "googleapis/google-cloud-python"},
1119+
)
1120+
1121+
10741122
def test_determine_library_namespace_fails_not_subpath():
10751123
"""Tests that a ValueError is raised if the gapic path is not inside the package root."""
10761124
pkg_root_path = Path("repo/packages/my-lib")

0 commit comments

Comments
 (0)