diff --git a/.github/scripts/hermetic_library_generation.sh b/.github/scripts/hermetic_library_generation.sh index cf29c87b3f..e69e8aa223 100755 --- a/.github/scripts/hermetic_library_generation.sh +++ b/.github/scripts/hermetic_library_generation.sh @@ -90,6 +90,9 @@ fi git show "${target_branch}":"${generation_config}" > "${baseline_generation_config}" config_diff=$(diff "${generation_config}" "${baseline_generation_config}" || true) +generator_version=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout -pl gapic-generator-java) +echo "Local generator version: ${generator_version}" + # install generator locally since we're using a SNAPSHOT version. mvn -V -B -ntp clean install -DskipTests @@ -104,6 +107,7 @@ docker run \ -u "$(id -u):$(id -g)" \ -v "$(pwd):${workspace_name}" \ -v "$HOME"/.m2:/home/.m2 \ + -e GENERATOR_VERSION="${generator_version}" \ gcr.io/cloud-devrel-public-resources/java-library-generation:"${image_tag}" \ --baseline-generation-config-path="${workspace_name}/${baseline_generation_config}" \ --current-generation-config-path="${workspace_name}/${generation_config}" diff --git a/generation_config.yaml b/generation_config.yaml index d9439fb7db..e894663ac0 100644 --- a/generation_config.yaml +++ b/generation_config.yaml @@ -1,4 +1,3 @@ -gapic_generator_version: 2.42.1-SNAPSHOT # {x-version-update:gapic-generator-java:current} googleapis_commitish: 7160b0c31e9b99fe896d1fa29b6fe6cfbf42588f # the libraries are ordered with respect to library name, which is # java-{library.library_name} or java-{library.api-shortname} when diff --git a/library_generation/README.md b/library_generation/README.md index 8bc47bad73..bead08e290 100644 --- a/library_generation/README.md +++ b/library_generation/README.md @@ -93,7 +93,7 @@ They are shared by library level parameters. | Name | Required | Notes | |:------------------------|:--------:|:---------------------------------------------| -| gapic_generator_version | Yes | | +| gapic_generator_version | No | set through env variable if not specified | | protoc_version | No | inferred from the generator if not specified | | grpc_version | No | inferred from the generator if not specified | | googleapis-commitish | Yes | | diff --git a/library_generation/model/generation_config.py b/library_generation/model/generation_config.py index 87242fd1a6..bc3ff6d440 100644 --- a/library_generation/model/generation_config.py +++ b/library_generation/model/generation_config.py @@ -12,6 +12,8 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +import os + import yaml from typing import Optional from library_generation.model.library_config import LibraryConfig @@ -23,6 +25,7 @@ COMMON_PROTOS_LIBRARY_NAME = "common-protos" GAPIC_GENERATOR_VERSION = "gapic_generator_version" LIBRARIES_BOM_VERSION = "libraries_bom_version" +GENERATOR_VERSION_ENV_KEY = "GENERATOR_VERSION" class GenerationConfig: @@ -32,18 +35,20 @@ class GenerationConfig: def __init__( self, - gapic_generator_version: str, googleapis_commitish: str, libraries: list[LibraryConfig], + gapic_generator_version: Optional[str] = None, libraries_bom_version: Optional[str] = None, grpc_version: Optional[str] = None, protoc_version: Optional[str] = None, ): - self.gapic_generator_version = gapic_generator_version self.googleapis_commitish = googleapis_commitish self.libraries_bom_version = ( libraries_bom_version if libraries_bom_version else "" ) + self.gapic_generator_version = GenerationConfig.__set_generator_version( + gapic_generator_version + ) self.libraries = libraries self.grpc_version = grpc_version self.protoc_version = protoc_version @@ -76,6 +81,21 @@ def contains_common_protos(self) -> bool: break return self.__contains_common_protos + @staticmethod + def __set_generator_version(gapic_generator_version: Optional[str]) -> str: + if gapic_generator_version is not None: + return gapic_generator_version + # if the generator version is not set through generation config, + # get it from environment variable. + gapic_generator_version = os.getenv(GENERATOR_VERSION_ENV_KEY) + if not gapic_generator_version: + raise ValueError( + f"Environment variable {GENERATOR_VERSION_ENV_KEY}" + f" is not set when the generator version is not" + f" specified in the generation config." + ) + return gapic_generator_version + def __validate(self) -> None: seen_library_names = dict() for library in self.libraries: @@ -148,12 +168,10 @@ def from_yaml(path_to_yaml: str) -> GenerationConfig: parsed_libraries.append(new_library) parsed_config = GenerationConfig( - gapic_generator_version=__required( - config, GAPIC_GENERATOR_VERSION, REPO_LEVEL_PARAMETER - ), googleapis_commitish=__required( config, "googleapis_commitish", REPO_LEVEL_PARAMETER ), + gapic_generator_version=__optional(config, GAPIC_GENERATOR_VERSION, None), grpc_version=__optional(config, "grpc_version", None), protoc_version=__optional(config, "protoc_version", None), libraries_bom_version=__optional(config, LIBRARIES_BOM_VERSION, None), diff --git a/library_generation/test/model/generation_config_unit_tests.py b/library_generation/test/model/generation_config_unit_tests.py index 258b0db415..f6a2f2a2d8 100644 --- a/library_generation/test/model/generation_config_unit_tests.py +++ b/library_generation/test/model/generation_config_unit_tests.py @@ -53,6 +53,24 @@ def test_generation_config_default_value(self): ) self.assertEqual("", config.libraries_bom_version) + def test_generation_config_with_generator_version_env_raise_exception(self): + self.assertRaisesRegex( + ValueError, + "Environment variable GENERATOR_VERSION is not set", + GenerationConfig, + googleapis_commitish="", + libraries=[], + ) + + def test_generation_config_set_generator_version_from_env(self): + os.environ["GENERATOR_VERSION"] = "1.2.3" + config = GenerationConfig( + googleapis_commitish="", + libraries=[], + ) + self.assertEqual("1.2.3", config.gapic_generator_version) + os.environ.pop("GENERATOR_VERSION") + def test_from_yaml_succeeds(self): config = from_yaml(f"{test_config_dir}/generation_config.yaml") self.assertEqual("2.34.0", config.gapic_generator_version) @@ -160,14 +178,6 @@ def test_validate_with_duplicate_library_name_raise_exception(self): ], ) - def test_from_yaml_without_gapic_generator_version_raise_exception(self): - self.assertRaisesRegex( - ValueError, - "Repo level parameter, gapic_generator_version", - from_yaml, - f"{test_config_dir}/config_without_generator.yaml", - ) - def test_from_yaml_without_googleapis_commitish_raise_exception(self): self.assertRaisesRegex( ValueError, diff --git a/library_generation/test/resources/test-config/config_without_generator.yaml b/library_generation/test/resources/test-config/config_without_generator.yaml deleted file mode 100644 index c78b8b9700..0000000000 --- a/library_generation/test/resources/test-config/config_without_generator.yaml +++ /dev/null @@ -1,7 +0,0 @@ -libraries: - - api_shortname: apigeeconnect - name_pretty: Apigee Connect - api_description: "allows the Apigee hybrid management" - product_documentation: "https://cloud.google.com/apigee/docs/hybrid/v1.3/apigee-connect/" - GAPICs: - - proto_path: google/cloud/apigeeconnect/v1 diff --git a/library_generation/test/resources/test-config/config_without_libraries_bom_version.yaml b/library_generation/test/resources/test-config/config_without_libraries_bom_version.yaml deleted file mode 100644 index 7921f68bd2..0000000000 --- a/library_generation/test/resources/test-config/config_without_libraries_bom_version.yaml +++ /dev/null @@ -1,9 +0,0 @@ -gapic_generator_version: 2.34.0 -googleapis_commitish: 1a45bf7393b52407188c82e63101db7dc9c72026 -libraries: - - api_shortname: apigeeconnect - name_pretty: Apigee Connect - api_description: "allows the Apigee hybrid management" - product_documentation: "https://cloud.google.com/apigee/docs/hybrid/v1.3/apigee-connect/" - GAPICs: - - proto_path: google/cloud/apigeeconnect/v1