Skip to content

Commit

Permalink
chore: make generator version an optional param (#3040)
Browse files Browse the repository at this point in the history
In this PR:
- Change `gapic_generator_version` as an optional parameter.
- Set `gapic_generator_version` through environment variable if it's not
specified in the generation config.

---------

Co-authored-by: Blake Li <blakeli@google.com>
Co-authored-by: diegomarquezp <diegomarquezp@google.com>
  • Loading branch information
3 people authored Jul 25, 2024
1 parent 43de5b5 commit 10f950e
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 31 deletions.
4 changes: 4 additions & 0 deletions .github/scripts/hermetic_library_generation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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}"
Expand Down
1 change: 0 additions & 1 deletion generation_config.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion library_generation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | |
Expand Down
28 changes: 23 additions & 5 deletions library_generation/model/generation_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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),
Expand Down
26 changes: 18 additions & 8 deletions library_generation/test/model/generation_config_unit_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit 10f950e

Please sign in to comment.