Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 37 additions & 14 deletions .generator/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,7 @@ def _get_new_library_config(request_data: Dict) -> Dict:
return {}


def _add_new_library_version(
library_config: Dict
) -> None:
def _add_new_library_version(library_config: Dict) -> None:
"""Adds the library version to the configuration if it's not present.

Args:
Expand Down Expand Up @@ -383,6 +381,19 @@ def _clean_up_files_after_post_processing(output: str, library_id: str):
os.remove(gapic_version_file)


def _determine_release_level_from_api_path(api_path: str) -> str:
"""Determines the release level from the API path.

Args:
api_path (str): The path to the API.

Returns:
str: The release level, which can be 'alpha', 'beta', or 'stable'.
"""
version = Path(api_path).name
return next((level for level in ["beta", "alpha"] if level in version), "stable")


def _create_repo_metadata_from_service_config(
service_config_name: str, api_path: str, source: str, library_id: str
) -> Dict:
Expand All @@ -398,22 +409,34 @@ def _create_repo_metadata_from_service_config(
Dict: The content of the .repo-metadata.json file.
"""
full_service_config_path = f"{source}/{api_path}/{service_config_name}"
with open(full_service_config_path, "r") as f:
service_config = yaml.safe_load(f)

api_id = service_config.get("name", {})
publishing = service_config.get("publishing", {})
name_pretty = service_config.get("title", "")
product_documentation = publishing.get("documentation_uri", "")
api_shortname = service_config.get("name", "").split(".")[0]
documentation = service_config.get("documentation", {})
api_description = documentation.get("summary", "")
issue_tracker = service_config.get(
"new_issue_uri", "https://github.com/googleapis/google-cloud-python/issues"
)
release_level = _determine_release_level_from_api_path(api_path)

# TODO(https://github.com/googleapis/librarian/issues/2332): Read the api
# service config to backfill `.repo-metadata.json`.
return {
"api_shortname": "",
"name_pretty": "",
"product_documentation": "",
"api_description": "",
"client_documentation": "",
"issue_tracker": "",
"release_level": "",
"api_shortname": api_shortname,
"name_pretty": name_pretty,
"product_documentation": product_documentation,
"api_description": api_description,
"client_documentation": f"https://cloud.google.com/python/docs/reference/{library_id}/latest",
"issue_tracker": issue_tracker,
"release_level": release_level,
"language": "python",
"library_type": "GAPIC_AUTO",
"repo": "googleapis/google-cloud-python",
"distribution_name": "",
"api_id": "",
"distribution_name": library_id,
"api_id": api_id,
}


Expand Down
37 changes: 36 additions & 1 deletion .generator/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
_create_repo_metadata_from_service_config,
_determine_generator_command,
_determine_library_namespace,
_determine_release_level_from_api_path,
_generate_api,
_generate_repo_metadata_file,
_get_api_generator_options,
Expand Down Expand Up @@ -581,6 +582,7 @@ def test_handle_generate_success(
mock_clean_up_files_after_post_processing = mocker.patch(
"cli._clean_up_files_after_post_processing"
)
mocker.patch("cli._generate_repo_metadata_file")

handle_generate()

Expand Down Expand Up @@ -1108,13 +1110,46 @@ def test_determine_library_namespace_success(
assert namespace == expected_namespace


def test_create_repo_metadata_from_service_config():
def test_determine_release_level_alpha():
"""Tests that the release level is alpha."""
api_path = "google/cloud/language/v1alpha1"
release_level = _determine_release_level_from_api_path(api_path)
assert release_level == "alpha"


def test_determine_release_level_beta():
"""Tests that the release level is beta."""
api_path = "google/cloud/language/v1beta1"
release_level = _determine_release_level_from_api_path(api_path)
assert release_level == "beta"


def test_determine_release_level_stable():
"""Tests that the release level is stable."""
api_path = "google/cloud/language/v1"
release_level = _determine_release_level_from_api_path(api_path)
assert release_level == "stable"


def test_create_repo_metadata_from_service_config(mocker):
"""Tests the creation of .repo-metadata.json content."""
service_config_name = "service_config.yaml"
api_path = "google/cloud/language/v1"
source = "/source"
library_id = "google-cloud-language"

mock_yaml_content = {
"name": "google.cloud.language.v1",
"title": "Cloud Natural Language API",
"publishing": {
"documentation_uri": "https://cloud.google.com/natural-language/docs"
},
"documentation": {"summary": "A comprehensive summary."},
"new_issue_uri": "https://example.com/issues",
}
mocker.patch("builtins.open", mocker.mock_open(read_data=""))
mocker.patch("yaml.safe_load", return_value=mock_yaml_content)

metadata = _create_repo_metadata_from_service_config(
service_config_name, api_path, source, library_id
)
Expand Down
Loading