Skip to content

Commit 1f15f04

Browse files
JoeWang1127ldetmer
authored andcommitted
fix: generate pr description with repo level change (#3182)
In this PR: - Generate PR description if config has repo level change without qualified commit. Context: googleapis/java-storage#2672 doesn't generate PR description even though the generator version and librararies-bom version are changed.
1 parent 7017ca2 commit 1f15f04

File tree

4 files changed

+131
-14
lines changed

4 files changed

+131
-14
lines changed

library_generation/generate_pr_description.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,14 @@ def get_repo_level_commit_messages(
8181
baseline_commit_sha: str,
8282
paths: Dict[str, str],
8383
is_monorepo: bool,
84-
repo_level_message: list[str] = None,
84+
repo_level_message: list[str],
8585
) -> str:
8686
"""
8787
Combine commit messages of a repository from latest_commit to
8888
baseline_commit. Only commits which change files in a pre-defined
8989
file paths will be considered.
90-
Note that baseline_commit should be an ancestor of latest_commit.
90+
Note that baseline_commit should be an ancestor of or the same as
91+
latest_commit.
9192
9293
:param repo_url: the url of the repository.
9394
:param current_commit_sha: the newest commit to be considered in
@@ -101,8 +102,6 @@ def get_repo_level_commit_messages(
101102
:raise ValueError: if current_commit is older than or equal to
102103
baseline_commit.
103104
"""
104-
if current_commit_sha == baseline_commit_sha:
105-
return EMPTY_MESSAGE
106105
tmp_dir = "/tmp/repo"
107106
shutil.rmtree(tmp_dir, ignore_errors=True)
108107
os.mkdir(tmp_dir)
@@ -111,11 +110,12 @@ def get_repo_level_commit_messages(
111110
baseline_commit = repo.commit(baseline_commit_sha)
112111
current_commit_time = __get_commit_timestamp(current_commit)
113112
baseline_commit_time = __get_commit_timestamp(baseline_commit)
114-
if current_commit_time <= baseline_commit_time:
113+
if current_commit_time < baseline_commit_time:
115114
raise ValueError(
116115
f"current_commit ({current_commit_sha[:7]}, committed on "
117-
f"{current_commit_time}) should be newer than baseline_commit "
118-
f"({baseline_commit_sha[:7]}, committed on {baseline_commit_time})."
116+
f"{current_commit_time}) should be newer than or equal to "
117+
f"baseline_commit ({baseline_commit_sha[:7]}, committed on "
118+
f"{baseline_commit_time})."
119119
)
120120
qualified_commits = {}
121121
commit = current_commit
@@ -128,8 +128,6 @@ def get_repo_level_commit_messages(
128128
break
129129
commit = commit_parents[0]
130130
shutil.rmtree(tmp_dir, ignore_errors=True)
131-
if len(qualified_commits) == 0:
132-
return EMPTY_MESSAGE
133131

134132
return __combine_commit_messages(
135133
current_commit=current_commit,
@@ -165,15 +163,19 @@ def __combine_commit_messages(
165163
is_monorepo: bool,
166164
repo_level_message: list[str],
167165
) -> str:
168-
description = [
169-
f"This pull request is generated with proto changes between "
170-
f"{commit_link(baseline_commit)} (exclusive) "
171-
f"and {commit_link(current_commit)} (inclusive).\n",
172-
]
166+
description = []
167+
if current_commit != baseline_commit:
168+
description.append(
169+
f"This pull request is generated with proto changes between "
170+
f"{commit_link(baseline_commit)} (exclusive) "
171+
f"and {commit_link(current_commit)} (inclusive).\n",
172+
)
173173
commit_message = repo_level_message
174174
commit_message.extend(
175175
format_commit_message(commits=commits, is_monorepo=is_monorepo)
176176
)
177+
if len(commit_message) == 0:
178+
return EMPTY_MESSAGE
177179
description.extend(wrap_override_commit(commit_message))
178180
return "\n".join(description)
179181

library_generation/test/generate_pr_description_unit_tests.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def test_get_commit_messages_current_is_older_raise_exception(self):
4747
baseline_commit,
4848
{},
4949
True,
50+
[],
5051
)
5152

5253
def test_get_commit_messages_with_same_current_and_baseline_returns_empty_message(
@@ -63,6 +64,7 @@ def test_get_commit_messages_with_same_current_and_baseline_returns_empty_messag
6364
baseline_commit,
6465
{},
6566
True,
67+
[],
6668
),
6769
)
6870

@@ -168,3 +170,101 @@ def test_generate_pr_description_with_combined_message(
168170
"The generated PR description does not match the expected golden file",
169171
)
170172
os.remove(f"{cwd}/pr_description.txt")
173+
174+
def test_generate_pr_description_with_repo_level_change_without_qualified_commit(
175+
self,
176+
):
177+
# no other commits between these two commits.
178+
baseline_commit_sha = "3b6f144d47b0a1d2115ab2445ec06e80cc324a44"
179+
current_commit_sha = "0cea7170404bec3d994f43db4fa292f5034cbe9a"
180+
cwd = os.getcwd()
181+
library = LibraryConfig(
182+
api_shortname="example_library",
183+
api_description="",
184+
name_pretty="",
185+
product_documentation="",
186+
gapic_configs=[GapicConfig(proto_path="google/example/v1")],
187+
)
188+
generate_pr_descriptions(
189+
config_change=ConfigChange(
190+
change_to_libraries={
191+
ChangeType.REPO_LEVEL_CHANGE: [
192+
LibraryChange(
193+
changed_param="gapic_generator_version",
194+
current_value="1.2.3",
195+
),
196+
LibraryChange(
197+
changed_param="libraries_bom_version", current_value="2.3.4"
198+
),
199+
],
200+
ChangeType.GOOGLEAPIS_COMMIT: [],
201+
},
202+
baseline_config=GenerationConfig(
203+
gapic_generator_version="",
204+
googleapis_commitish=baseline_commit_sha,
205+
libraries=[library],
206+
),
207+
current_config=GenerationConfig(
208+
gapic_generator_version="1.2.3",
209+
googleapis_commitish=current_commit_sha,
210+
libraries_bom_version="2.3.4",
211+
libraries=[library],
212+
),
213+
),
214+
description_path=cwd,
215+
)
216+
self.assertTrue(os.path.isfile(f"{cwd}/pr_description.txt"))
217+
self.assertTrue(
218+
cmp(
219+
f"{resources_dir}/repo_level_and_no_qualified_commit_pr_description-golden.txt",
220+
f"{cwd}/pr_description.txt",
221+
),
222+
"The generated PR description does not match the expected golden file",
223+
)
224+
os.remove(f"{cwd}/pr_description.txt")
225+
226+
def test_generate_pr_description_create_description_with_only_repo_level_change(
227+
self,
228+
):
229+
commit_sha = "3b6f144d47b0a1d2115ab2445ec06e80cc324a44"
230+
cwd = os.getcwd()
231+
library = LibraryConfig(
232+
api_shortname="documentai",
233+
api_description="",
234+
name_pretty="",
235+
product_documentation="",
236+
gapic_configs=[GapicConfig(proto_path="google/cloud/documentai/v1")],
237+
)
238+
generate_pr_descriptions(
239+
config_change=ConfigChange(
240+
change_to_libraries={
241+
ChangeType.REPO_LEVEL_CHANGE: [
242+
LibraryChange(
243+
changed_param="gapic_generator_version",
244+
current_value="1.2.3",
245+
)
246+
],
247+
ChangeType.GOOGLEAPIS_COMMIT: [],
248+
},
249+
baseline_config=GenerationConfig(
250+
gapic_generator_version="1.2.2",
251+
googleapis_commitish=commit_sha,
252+
libraries=[library],
253+
),
254+
current_config=GenerationConfig(
255+
gapic_generator_version="1.2.3",
256+
googleapis_commitish=commit_sha,
257+
libraries=[library],
258+
),
259+
),
260+
description_path=cwd,
261+
)
262+
self.assertTrue(os.path.isfile(f"{cwd}/pr_description.txt"))
263+
self.assertTrue(
264+
cmp(
265+
f"{resources_dir}/repo_level_only_pr_description-golden.txt",
266+
f"{cwd}/pr_description.txt",
267+
),
268+
"The generated PR description does not match the expected golden file",
269+
)
270+
os.remove(f"{cwd}/pr_description.txt")
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
This pull request is generated with proto changes between [googleapis/googleapis@3b6f144](https://github.com/googleapis/googleapis/commit/3b6f144d47b0a1d2115ab2445ec06e80cc324a44) (exclusive) and [googleapis/googleapis@0cea717](https://github.com/googleapis/googleapis/commit/0cea7170404bec3d994f43db4fa292f5034cbe9a) (inclusive).
2+
3+
BEGIN_COMMIT_OVERRIDE
4+
BEGIN_NESTED_COMMIT
5+
fix(deps): update the Java code generator (gapic-generator-java) to 1.2.3
6+
END_NESTED_COMMIT
7+
BEGIN_NESTED_COMMIT
8+
chore: update the libraries_bom version to 2.3.4
9+
END_NESTED_COMMIT
10+
END_COMMIT_OVERRIDE
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
BEGIN_COMMIT_OVERRIDE
2+
BEGIN_NESTED_COMMIT
3+
fix(deps): update the Java code generator (gapic-generator-java) to 1.2.3
4+
END_NESTED_COMMIT
5+
END_COMMIT_OVERRIDE

0 commit comments

Comments
 (0)