Skip to content

Commit 3ae4e74

Browse files
committed
Updating template for Owlbot.yaml
1 parent 3d2fa59 commit 3ae4e74

2 files changed

Lines changed: 119 additions & 20 deletions

File tree

generation/new_client/new-client.py

Lines changed: 88 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,28 @@
2929
def main(ctx):
3030
pass
3131

32+
def add_module_to_root_pom(pom_path: Path, new_module: str):
33+
with open(pom_path, "r") as fp:
34+
content = fp.read()
35+
36+
matches = re.findall(r'<module>([\w\-]+?)</module>', content)
37+
matches.append(new_module)
38+
39+
# Make normal modules first and BOM and CoverageAggregator at the end
40+
matches.sort(key=lambda m: f"0{m}" if m.startswith('java-') else m)
41+
modules_lines = []
42+
for match in matches:
43+
modules_lines.append(f"<module>{match}</module>")
44+
45+
modules_line = "\n ".join(modules_lines)
46+
ordered_content = re.sub(r"<modules>.+</modules>",
47+
f"<modules>\n {modules_line}\n </modules>",
48+
content,
49+
flags=re.MULTILINE | re.DOTALL)
50+
51+
with open(pom_path, "w") as fp:
52+
fp.truncate(0)
53+
fp.write(ordered_content)
3254

3355
@main.command()
3456
@click.option("--api_shortname", required=True, type=str, prompt="Service name? (e.g. automl)")
@@ -61,7 +83,7 @@ def main(ctx):
6183
prompt=True,
6284
default="grpc",
6385
)
64-
@click.option("--language", required=True, type=str, prompt=True, default="java")
86+
@click.option("--language", required=True, type=str, default="java")
6587
@click.option("--distribution-name", type=str)
6688
@click.option("--api-id", type=str)
6789
@click.option("--requires-billing", type=bool, default=True)
@@ -73,6 +95,7 @@ def main(ctx):
7395
"--owlbot-image", type=str, default="gcr.io/cloud-devrel-public-resources/owlbot-java"
7496
)
7597
@click.option("--library-type", type=str)
98+
@click.option("--monorepo-url", type=str, default=None)
7699
def generate(
77100
api_shortname,
78101
name_pretty,
@@ -90,6 +113,7 @@ def generate(
90113
group_id,
91114
owlbot_image,
92115
library_type,
116+
monorepo_url,
93117
):
94118
cloud_prefix = "cloud-" if cloud_api else ""
95119

@@ -133,14 +157,30 @@ def generate(
133157
if requires_billing:
134158
repo_metadata["requires_billing"] = True
135159

160+
# Initialize workdir
161+
if monorepo_url:
162+
print("Creating a new module in monorepo " + monorepo_url)
163+
subprocess.check_call(["rm", "-fr", "monorepo"], cwd="workspace")
164+
subprocess.check_call(["git", "clone", monorepo_url, "monorepo"],
165+
cwd="workspace")
166+
workdir = Path(f"workspace/monorepo/java-{output_name}")
167+
os.makedirs(workdir, exist_ok=True)
168+
subprocess.check_call(["git", "checkout", "-b",
169+
f"new_module_java-{output_name}"],
170+
cwd=workdir)
171+
add_module_to_root_pom(workdir / ".." / "pom.xml",
172+
f"java-{output_name}")
173+
subprocess.check_call(["git", "add", "."], cwd=workdir / "..")
174+
else:
175+
print("Creating a new split repo")
176+
workdir = Path(f"workspace/java-{output_name}")
177+
os.makedirs(workdir, exist_ok=True)
178+
subprocess.check_call(["git", "init", "-b", "main"], cwd=workdir)
179+
136180
# write .repo-metadata.json file
137-
workdir = Path(f"workspace/java-{output_name}")
138-
os.makedirs(workdir, exist_ok=True)
139181
with open(workdir / ".repo-metadata.json", "w") as fp:
140182
json.dump(repo_metadata, fp, indent=2)
141183

142-
subprocess.check_call(["git", "init", "-b", "main"], cwd=workdir)
143-
144184
# create owlbot.py
145185
templates.render(
146186
template_name="new-client/owlbot.py.j2",
@@ -149,12 +189,17 @@ def generate(
149189
template_excludes=[],
150190
)
151191

192+
# In monorepo, .OwlBot.yaml needs to be in the directory of the module.
193+
owlbot_yaml_location_from_module = ".OwlBot.yaml" if monorepo_url else \
194+
".github/.OwlBot.yaml"
152195
# create owlbot config
153196
templates.render(
154-
template_name="new-client/owlbot.yaml.j2",
155-
output_name=str(workdir / ".github" / ".OwlBot.yaml"),
197+
template_name="new-client/owlbot.yaml.monorepo.j2" if monorepo_url else
198+
"new-client/owlbot.yaml.j2",
199+
output_name=str(workdir / owlbot_yaml_location_from_module),
156200
artifact_name=distribution_name_short,
157201
proto_path=proto_path,
202+
module_name=f"java-{output_name}"
158203
)
159204

160205
# get the sha256 digets for the owlbot image
@@ -168,20 +213,21 @@ def generate(
168213
.split("@")[-1]
169214
)
170215

171-
# create owlbot lock
172-
templates.render(
173-
template_name="new-client/owlbot.lock.yaml.j2",
174-
output_name=str(workdir / ".github" / ".OwlBot.lock.yaml"),
175-
owlbot_image_digest=owlbot_image_digest,
176-
owlbot_image=owlbot_image,
177-
)
216+
# create owlbot lock. Monorepo does not need this file.
217+
if not monorepo_url:
218+
templates.render(
219+
template_name="new-client/owlbot.lock.yaml.j2",
220+
output_name=str(workdir / ".github" / ".OwlBot.lock.yaml"),
221+
owlbot_image_digest=owlbot_image_digest,
222+
owlbot_image=owlbot_image,
223+
)
178224

179225
user = subprocess.check_output(["id", "-u"], encoding="utf8").strip()
180226
group = subprocess.check_output(["id", "-g"], encoding="utf8").strip()
181227

182228
# run owlbot copy
183229
print("Cloning googleapis-gen...")
184-
subprocess.check_call(["git", "clone", "git@github.com:googleapis/googleapis-gen.git", "./gen/googleapis-gen"], cwd=workdir)
230+
subprocess.check_call(["git", "clone", "https://github.com/googleapis/googleapis-gen.git", "./gen/googleapis-gen"], cwd=workdir)
185231
subprocess.check_call(["docker", "pull", "gcr.io/cloud-devrel-public-resources/owlbot-cli:latest"])
186232
print("Running copy-code...")
187233
subprocess.check_call(
@@ -200,8 +246,8 @@ def generate(
200246
"--env", "HOME=/tmp",
201247
"gcr.io/cloud-devrel-public-resources/owlbot-cli:latest",
202248
"copy-code",
203-
"--source-repo=/googleapis-gen"
204-
249+
"--source-repo=/googleapis-gen",
250+
f"--config-file={owlbot_yaml_location_from_module}"
205251
],
206252
cwd=workdir,
207253
)
@@ -210,24 +256,46 @@ def generate(
210256
# run post processor owl-bot image
211257
subprocess.check_call(["git", "add", "."], cwd=workdir)
212258
subprocess.check_call(
213-
["git", "commit", "-m", "feat: initial generation"], cwd=workdir
259+
["git", "commit", "-m", f"feat: initial generation of {api_shortname}"], cwd=workdir
214260
)
261+
262+
# Bringing owl-bot-staging from the new module's directory to the root
263+
# directory so that owlbot-java can process them.
264+
if monorepo_url:
265+
subprocess.check_call(
266+
[
267+
"mv",
268+
"owl-bot-staging",
269+
"../"
270+
],
271+
cwd=workdir,
272+
)
215273
print("Running the post-processor...")
274+
workdir_parent=(workdir / '..').resolve()
216275
subprocess.check_call(
217276
[
218277
"docker",
219278
"run",
220279
"--rm",
221280
"-v",
222-
f"{workdir.resolve()}:/workspace",
281+
f"{workdir_parent}:/workspace" if monorepo_url else f"{workdir.resolve()}:/workspace",
223282
"--user",
224283
f"{user}:{group}",
225284
owlbot_image,
226285
],
227-
cwd=workdir,
286+
cwd=workdir_parent if monorepo_url else workdir,
228287
)
288+
if monorepo_url:
289+
# In monorpeo, .github and .kokoro under the module is unused
290+
subprocess.check_call(["rm", "-fr", ".github"],
291+
cwd=workdir)
292+
subprocess.check_call(["rm", "-fr", ".kokoro"],
293+
cwd=workdir)
294+
229295
subprocess.check_call(["git", "add", "."], cwd=workdir)
230296
subprocess.check_call(["git", "commit", "--amend", "--no-edit"], cwd=workdir)
297+
print(f"Prepared new library in {workdir}")
231298

232299
if __name__ == "__main__":
233300
main()
301+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
{% if artifact_name %}
16+
deep-remove-regex:
17+
- "/grpc-google-.*/src"
18+
- "/proto-google-.*/src"
19+
- "/google-.*/src"
20+
21+
deep-preserve-regex:
22+
- "/google-.*/src/test/java/com/google/cloud/.*/v.*/it/IT.*Test.java"
23+
24+
deep-copy-regex:
25+
- source: "/{{ proto_path }}/(v.*)/.*-java/proto-google-.*/src"
26+
dest: "/owl-bot-staging/{{ module_name }}/$1/proto-{{ artifact_name }}-$1/src"
27+
- source: "/{{ proto_path }}/(v.*)/.*-java/grpc-google-.*/src"
28+
dest: "/owl-bot-staging/{{ module_name }}/$1/grpc-{{ artifact_name }}-$1/src"
29+
- source: "/{{ proto_path }}/(v.*)/.*-java/gapic-google-.*/src"
30+
dest: "/owl-bot-staging/{{ module_name }}/$1/{{ artifact_name }}/src"
31+
{% endif %}

0 commit comments

Comments
 (0)