Skip to content

Commit 0e784a2

Browse files
chore: generate poms with grpc dependencies as test scoped (#3072)
Fixes googleapis/google-cloud-java#10037 ☕ ### Overview This will modify the behavior of library generation at the postprocessing stage. The main change is that generated _cloud_ poms (i.e. `google-cloud-${service}/pom.xml`) will now have the dependencies `proto-google-common-protos` and `grpc-google-iam-v1` declared as `<scope>test</scope>` if the libraries belong to a monorepo. ### Integration test It will be failing until we update the test branch. Once this approach is validated, I will proceed with updating the branch. ### Approach #### For new libraries Via template in `owlbot/teplates/poms/cloud_pom.xml.j2`. New libraries will have these two dependencies declared as test scope if they are part of a monorepo. #### For existing libraries The script `fix_poms.py` will now modify the existing poms by setting the two dependencies as test-scoped, only if they are part of a monorepo. ### Confirmation that it works * java-bigquerystorage: no effects since these dependencies are [not found in the pom](https://github.com/search?q=repo%3Agoogleapis%2Fjava-bigquerystorage%20path%3Agoogle-cloud-bigquerystorage%2Fpom.xml%20grpc-google&type=code) * java-datastore: [no relevant changes](https://github.com/googleapis/java-datastore/pull/1530/files) since the dependencies are [not found in the pom](https://github.com/googleapis/java-datastore/blob/main/google-cloud-datastore/pom.xml) * java-firestore: [no relevant changes](googleapis/java-firestore#1764) since the one targeted dependency is [already declared as test scoped](https://github.com/googleapis/java-firestore/blob/main/google-cloud-firestore/pom.xml#L160-L164) * java-logging: [no relevant changes](googleapis/java-logging#1660)] since the dependencies are [not declared in the pom](https://github.com/googleapis/java-logging/blob/main/google-cloud-logging/pom.xml) * java-pubsub: no changes. Relevant dependency [already declared as test scoped](https://github.com/googleapis/java-pubsub/blob/2b15b0a3f43c9ccef663fedf0398375f58fd9183/google-cloud-pubsub/pom.xml#L135-L139) * java-pubsublite: [no relevant changes](googleapis/java-pubsublite#1692) since there are [no relevant dependencies declared in the pom](https://github.com/googleapis/java-pubsublite/blob/main/google-cloud-pubsublite/pom.xml) * java-spanner: ignored - dependency kept as is * java-storage: ignored - kept as is * **google-cloud-java: [91 poms affected](https://github.com/googleapis/google-cloud-java/pull/11031/files) by converting dependencies to test-scoped (PR only contains pom changes for simplicity + outdated googleapis_committish)** * We had a special situation with `java-dataproc` because it references `google-iam-v1` via `grpc-google-iam-v1`. However this is incorrect because what this library really needs is the proto classes. The fix for this was to treat this library as any other library and add `proto-google-iam-v1` as a dependency.
1 parent afecb8c commit 0e784a2

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

library_generation/owlbot/src/fix_poms.py

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import sys
1616
import glob
1717
import json
18+
from xml.etree.ElementTree import ElementTree
19+
1820
from lxml import etree
1921
import os
2022
import re
@@ -92,7 +94,10 @@ def _is_cloud_client(existing_modules: List[module.Module]) -> bool:
9294

9395

9496
def update_cloud_pom(
95-
filename: str, proto_modules: List[module.Module], grpc_modules: List[module.Module]
97+
filename: str,
98+
proto_modules: List[module.Module],
99+
grpc_modules: List[module.Module],
100+
is_monorepo: bool,
96101
):
97102
tree = etree.parse(filename)
98103
root = tree.getroot()
@@ -104,6 +109,9 @@ def update_cloud_pom(
104109
if m.find("{http://maven.apache.org/POM/4.0.0}artifactId") is not None
105110
]
106111

112+
if is_monorepo:
113+
_set_test_scoped_deps(dependencies)
114+
107115
try:
108116
grpc_index = _find_dependency_index(
109117
dependencies, "com.google.api.grpc", "grpc-"
@@ -169,6 +177,39 @@ def update_cloud_pom(
169177
tree.write(filename, pretty_print=True, xml_declaration=True, encoding="utf-8")
170178

171179

180+
def _set_test_scoped_deps(dependencies: list[ElementTree]) -> None:
181+
"""
182+
As of July 2024, we have two dependencies that should be declared as
183+
test-scoped in a monorepo: grpc-google-common-protos and grpc-google-iam-v1.
184+
HW libraries are treated as usual
185+
:param dependencies: List of XML Objects representing a <dependency/>
186+
"""
187+
TEST_SCOPED_DEPENDENCIES = ["grpc-google-common-protos", "grpc-google-iam-v1"]
188+
print(
189+
'converting dependencies "grpc-google-common-protos" and "grpc-google-iam-v1" to test-scoped'
190+
)
191+
for d in dependencies:
192+
artifact_query = "{http://maven.apache.org/POM/4.0.0}artifactId"
193+
scope_query = "{http://maven.apache.org/POM/4.0.0}scope"
194+
current_scope = d.find(scope_query)
195+
artifact_id_elem = d.find(artifact_query)
196+
if artifact_id_elem is None:
197+
continue
198+
artifact_id = artifact_id_elem.text
199+
is_test_scoped = (
200+
current_scope.text == "test" if current_scope is not None else False
201+
)
202+
if artifact_id in TEST_SCOPED_DEPENDENCIES and not is_test_scoped:
203+
new_scope = etree.Element(scope_query)
204+
new_scope.text = "test"
205+
if current_scope is not None:
206+
d.replace(current_scope, new_scope)
207+
else:
208+
d.append(new_scope)
209+
new_scope.tail = "\n "
210+
new_scope.getprevious().tail = "\n "
211+
212+
172213
def update_parent_pom(filename: str, modules: List[module.Module]):
173214
tree = etree.parse(filename)
174215
root = tree.getroot()
@@ -492,7 +533,9 @@ def main(versions_file, monorepo):
492533
if os.path.isfile(f"{artifact_id}/pom.xml"):
493534
print("updating modules in cloud pom.xml")
494535
if artifact_id not in excluded_poms_list:
495-
update_cloud_pom(f"{artifact_id}/pom.xml", proto_modules, grpc_modules)
536+
update_cloud_pom(
537+
f"{artifact_id}/pom.xml", proto_modules, grpc_modules, monorepo
538+
)
496539
elif artifact_id not in excluded_poms_list:
497540
print("creating missing cloud pom.xml")
498541
templates.render(

library_generation/owlbot/templates/poms/cloud_pom.xml.j2

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,36 @@
6666
</dependency>
6767
<dependency>
6868
<groupId>com.google.api.grpc</groupId>
69-
<artifactId>grpc-google-common-protos</artifactId>
69+
<artifactId>proto-google-iam-v1</artifactId>
7070
</dependency>
71+
{%- if not monorepo %}
7172
<dependency>
7273
<groupId>com.google.api.grpc</groupId>
73-
<artifactId>proto-google-iam-v1</artifactId>
74+
<artifactId>grpc-google-common-protos</artifactId>
7475
</dependency>
7576
<dependency>
7677
<groupId>com.google.api.grpc</groupId>
7778
<artifactId>grpc-google-iam-v1</artifactId>
7879
</dependency>
80+
{%- endif %}
7981
<dependency>
8082
<groupId>org.threeten</groupId>
8183
<artifactId>threetenbp</artifactId>
8284
</dependency>
8385

8486
<!-- Test dependencies -->
87+
{%- if monorepo %}
88+
<dependency>
89+
<groupId>com.google.api.grpc</groupId>
90+
<artifactId>grpc-google-common-protos</artifactId>
91+
<scope>test</scope>
92+
</dependency>
93+
<dependency>
94+
<groupId>com.google.api.grpc</groupId>
95+
<artifactId>grpc-google-iam-v1</artifactId>
96+
<scope>test</scope>
97+
</dependency>
98+
{%- endif %}
8599
<dependency>
86100
<groupId>junit</groupId>
87101
<artifactId>junit</artifactId>

library_generation/test/resources/test-owlbot/java-admanager/ad-manager/pom-golden.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
<dependency>
6565
<groupId>com.google.api.grpc</groupId>
6666
<artifactId>grpc-google-common-protos</artifactId>
67+
<scope>test</scope>
6768
</dependency>
6869
<dependency>
6970
<groupId>com.google.api.grpc</groupId>
@@ -72,6 +73,7 @@
7273
<dependency>
7374
<groupId>com.google.api.grpc</groupId>
7475
<artifactId>grpc-google-iam-v1</artifactId>
76+
<scope>test</scope>
7577
</dependency>
7678
<dependency>
7779
<groupId>org.threeten</groupId>

0 commit comments

Comments
 (0)