Skip to content

Commit

Permalink
fix: do not populate repo level change while removing library (#2740)
Browse files Browse the repository at this point in the history
In this PR:
- Do not populate repo level change while removing library

Context: the
[log](https://github.com/googleapis/google-cloud-java/actions/runs/8970167105/job/24633084853?pr=10782)
shows that the script went a full generation when a library is removed
from the generation config.
  • Loading branch information
JoeWang1127 committed May 6, 2024
1 parent f0653b8 commit 43e62b9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,21 @@ def test_compare_config_library_addition(self):
config_change = result.change_to_libraries[ChangeType.LIBRARIES_ADDITION][0]
self.assertEqual("new_library", config_change.library_name)

def test_compare_config_library_removal_does_not_have_repo_or_library_level_change(
self,
):
self.current_config.libraries = []
result = compare_config(
baseline_config=self.baseline_config,
current_config=self.current_config,
)
self.assertTrue(
len(result.change_to_libraries[ChangeType.REPO_LEVEL_CHANGE]) == 0
)
self.assertTrue(
len(result.change_to_libraries[ChangeType.LIBRARY_LEVEL_CHANGE]) == 0
)

def test_compare_config_api_shortname_update_without_library_name(self):
self.current_config.libraries[0].api_shortname = "new_api_shortname"
result = compare_config(
Expand Down
23 changes: 18 additions & 5 deletions library_generation/utils/generation_config_comparator.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,17 @@ def compare_config(
:return: a ConfigChange objects.
"""
diff = defaultdict(list[LibraryChange])
baseline_params = __convert_params_to_sorted_list(baseline_config)
current_params = __convert_params_to_sorted_list(current_config)
# Exclude `libraries` because an empty library list (e.g., by library
# removal) will cause this parameter appears in the sorted param list,
# which leads to unequal list of parameters.
excluded_params = {"libraries"}
baseline_params = __convert_params_to_sorted_list(
obj=baseline_config, excluded_params=excluded_params
)
current_params = __convert_params_to_sorted_list(
obj=current_config, excluded_params=excluded_params
)

for baseline_param, current_param in zip(baseline_params, current_params):
if baseline_param == current_param:
continue
Expand Down Expand Up @@ -216,7 +225,7 @@ def __compare_gapic_configs(
diff[ChangeType.GAPIC_ADDITION].append(config_change)


def __convert_params_to_sorted_list(obj: Any) -> List[tuple]:
def __convert_params_to_sorted_list(obj: Any, excluded_params=None) -> List[tuple]:
"""
Convert the parameter and its value of a given object to a sorted list of
tuples.
Expand All @@ -230,13 +239,17 @@ def __convert_params_to_sorted_list(obj: Any) -> List[tuple]:
Note that built-in params, e.g., __str__, and methods will be skipped.
:param obj: an object
:param obj: an object.
:param excluded_params: excluded params.
:return: a sorted list of tuples.
"""
if excluded_params is None:
excluded_params = set()
param_and_values = []
for param, value in vars(obj).items():
if (
param.startswith("__") # skip built-in params
param in excluded_params
or param.startswith("__") # skip built-in params
or callable(getattr(obj, param)) # skip methods
# skip if the type of param is not one of the following types
# 1. str
Expand Down

0 comments on commit 43e62b9

Please sign in to comment.