Skip to content

Commit caefc87

Browse files
committed
feat: create repo-metadata during generation
1 parent 5339bcb commit caefc87

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
@@ -360,6 +360,68 @@ def _clean_up_files_after_post_processing(output: str, library_id: str):
360360
): # pragma: NO COVER
361361
os.remove(gapic_version_file)
362362

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

364426
def handle_generate(
365427
librarian: str = LIBRARIAN_DIR,
@@ -398,6 +460,7 @@ def handle_generate(
398460
api_path = api.get("path")
399461
if api_path:
400462
_generate_api(api_path, library_id, source, output)
463+
_generate_repo_metadata_file(output, library_id, source, apis_to_generate)
401464
_copy_files_needed_for_post_processing(output, input, library_id)
402465
_run_post_processor(output, library_id)
403466
_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)