Skip to content

Commit 10f950e

Browse files
JoeWang1127blakeli0diegomarquezp
authored
chore: make generator version an optional param (#3040)
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 <[email protected]> Co-authored-by: diegomarquezp <[email protected]>
1 parent 43de5b5 commit 10f950e

File tree

7 files changed

+46
-31
lines changed

7 files changed

+46
-31
lines changed

.github/scripts/hermetic_library_generation.sh

+4
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ fi
9090
git show "${target_branch}":"${generation_config}" > "${baseline_generation_config}"
9191
config_diff=$(diff "${generation_config}" "${baseline_generation_config}" || true)
9292

93+
generator_version=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout -pl gapic-generator-java)
94+
echo "Local generator version: ${generator_version}"
95+
9396
# install generator locally since we're using a SNAPSHOT version.
9497
mvn -V -B -ntp clean install -DskipTests
9598

@@ -104,6 +107,7 @@ docker run \
104107
-u "$(id -u):$(id -g)" \
105108
-v "$(pwd):${workspace_name}" \
106109
-v "$HOME"/.m2:/home/.m2 \
110+
-e GENERATOR_VERSION="${generator_version}" \
107111
gcr.io/cloud-devrel-public-resources/java-library-generation:"${image_tag}" \
108112
--baseline-generation-config-path="${workspace_name}/${baseline_generation_config}" \
109113
--current-generation-config-path="${workspace_name}/${generation_config}"

generation_config.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
gapic_generator_version: 2.42.1-SNAPSHOT # {x-version-update:gapic-generator-java:current}
21
googleapis_commitish: 7160b0c31e9b99fe896d1fa29b6fe6cfbf42588f
32
# the libraries are ordered with respect to library name, which is
43
# java-{library.library_name} or java-{library.api-shortname} when

library_generation/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ They are shared by library level parameters.
9393

9494
| Name | Required | Notes |
9595
|:------------------------|:--------:|:---------------------------------------------|
96-
| gapic_generator_version | Yes | |
96+
| gapic_generator_version | No | set through env variable if not specified |
9797
| protoc_version | No | inferred from the generator if not specified |
9898
| grpc_version | No | inferred from the generator if not specified |
9999
| googleapis-commitish | Yes | |

library_generation/model/generation_config.py

+23-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
15+
import os
16+
1517
import yaml
1618
from typing import Optional
1719
from library_generation.model.library_config import LibraryConfig
@@ -23,6 +25,7 @@
2325
COMMON_PROTOS_LIBRARY_NAME = "common-protos"
2426
GAPIC_GENERATOR_VERSION = "gapic_generator_version"
2527
LIBRARIES_BOM_VERSION = "libraries_bom_version"
28+
GENERATOR_VERSION_ENV_KEY = "GENERATOR_VERSION"
2629

2730

2831
class GenerationConfig:
@@ -32,18 +35,20 @@ class GenerationConfig:
3235

3336
def __init__(
3437
self,
35-
gapic_generator_version: str,
3638
googleapis_commitish: str,
3739
libraries: list[LibraryConfig],
40+
gapic_generator_version: Optional[str] = None,
3841
libraries_bom_version: Optional[str] = None,
3942
grpc_version: Optional[str] = None,
4043
protoc_version: Optional[str] = None,
4144
):
42-
self.gapic_generator_version = gapic_generator_version
4345
self.googleapis_commitish = googleapis_commitish
4446
self.libraries_bom_version = (
4547
libraries_bom_version if libraries_bom_version else ""
4648
)
49+
self.gapic_generator_version = GenerationConfig.__set_generator_version(
50+
gapic_generator_version
51+
)
4752
self.libraries = libraries
4853
self.grpc_version = grpc_version
4954
self.protoc_version = protoc_version
@@ -76,6 +81,21 @@ def contains_common_protos(self) -> bool:
7681
break
7782
return self.__contains_common_protos
7883

84+
@staticmethod
85+
def __set_generator_version(gapic_generator_version: Optional[str]) -> str:
86+
if gapic_generator_version is not None:
87+
return gapic_generator_version
88+
# if the generator version is not set through generation config,
89+
# get it from environment variable.
90+
gapic_generator_version = os.getenv(GENERATOR_VERSION_ENV_KEY)
91+
if not gapic_generator_version:
92+
raise ValueError(
93+
f"Environment variable {GENERATOR_VERSION_ENV_KEY}"
94+
f" is not set when the generator version is not"
95+
f" specified in the generation config."
96+
)
97+
return gapic_generator_version
98+
7999
def __validate(self) -> None:
80100
seen_library_names = dict()
81101
for library in self.libraries:
@@ -148,12 +168,10 @@ def from_yaml(path_to_yaml: str) -> GenerationConfig:
148168
parsed_libraries.append(new_library)
149169

150170
parsed_config = GenerationConfig(
151-
gapic_generator_version=__required(
152-
config, GAPIC_GENERATOR_VERSION, REPO_LEVEL_PARAMETER
153-
),
154171
googleapis_commitish=__required(
155172
config, "googleapis_commitish", REPO_LEVEL_PARAMETER
156173
),
174+
gapic_generator_version=__optional(config, GAPIC_GENERATOR_VERSION, None),
157175
grpc_version=__optional(config, "grpc_version", None),
158176
protoc_version=__optional(config, "protoc_version", None),
159177
libraries_bom_version=__optional(config, LIBRARIES_BOM_VERSION, None),

library_generation/test/model/generation_config_unit_tests.py

+18-8
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,24 @@ def test_generation_config_default_value(self):
5353
)
5454
self.assertEqual("", config.libraries_bom_version)
5555

56+
def test_generation_config_with_generator_version_env_raise_exception(self):
57+
self.assertRaisesRegex(
58+
ValueError,
59+
"Environment variable GENERATOR_VERSION is not set",
60+
GenerationConfig,
61+
googleapis_commitish="",
62+
libraries=[],
63+
)
64+
65+
def test_generation_config_set_generator_version_from_env(self):
66+
os.environ["GENERATOR_VERSION"] = "1.2.3"
67+
config = GenerationConfig(
68+
googleapis_commitish="",
69+
libraries=[],
70+
)
71+
self.assertEqual("1.2.3", config.gapic_generator_version)
72+
os.environ.pop("GENERATOR_VERSION")
73+
5674
def test_from_yaml_succeeds(self):
5775
config = from_yaml(f"{test_config_dir}/generation_config.yaml")
5876
self.assertEqual("2.34.0", config.gapic_generator_version)
@@ -160,14 +178,6 @@ def test_validate_with_duplicate_library_name_raise_exception(self):
160178
],
161179
)
162180

163-
def test_from_yaml_without_gapic_generator_version_raise_exception(self):
164-
self.assertRaisesRegex(
165-
ValueError,
166-
"Repo level parameter, gapic_generator_version",
167-
from_yaml,
168-
f"{test_config_dir}/config_without_generator.yaml",
169-
)
170-
171181
def test_from_yaml_without_googleapis_commitish_raise_exception(self):
172182
self.assertRaisesRegex(
173183
ValueError,

library_generation/test/resources/test-config/config_without_generator.yaml

-7
This file was deleted.

library_generation/test/resources/test-config/config_without_libraries_bom_version.yaml

-9
This file was deleted.

0 commit comments

Comments
 (0)