16
16
from enum import Enum
17
17
from typing import Optional
18
18
from git import Commit , Repo
19
+
20
+ from library_generation .model .gapic_inputs import parse_build_str
19
21
from library_generation .model .generation_config import GenerationConfig
20
22
from library_generation .model .library_config import LibraryConfig
21
23
from library_generation .utils .utilities import sh_util
22
24
from library_generation .utils .proto_path_utils import find_versioned_proto_path
23
25
26
+ INSERTIONS = "insertions"
27
+ LINES = "lines"
28
+
24
29
25
30
class ChangeType (Enum ):
26
31
GOOGLEAPIS_COMMIT = 1
@@ -77,6 +82,7 @@ def get_changed_libraries(self) -> Optional[list[str]]:
77
82
Returns a unique, sorted list of library name of changed libraries.
78
83
None if there is a repository level change, which means all libraries
79
84
in the current_config will be generated.
85
+
80
86
:return: library names of change libraries.
81
87
"""
82
88
if ChangeType .REPO_LEVEL_CHANGE in self .change_to_libraries :
@@ -143,11 +149,23 @@ def __create_qualified_commit(
143
149
:return: qualified commits.
144
150
"""
145
151
libraries = set ()
146
- for file in commit .stats .files .keys ():
147
- if file .endswith ("BUILD.bazel" ):
148
- continue
149
- versioned_proto_path = find_versioned_proto_path (file )
152
+ for file_path , changes in commit .stats .files .items ():
153
+ versioned_proto_path = find_versioned_proto_path (file_path )
150
154
if versioned_proto_path in proto_paths :
155
+ if (
156
+ file_path .endswith ("BUILD.bazel" )
157
+ # Qualify a commit if the commit only added BUILD.bazel
158
+ # because it's very unlikely that a commit added BUILD.bazel
159
+ # without adding proto files. Therefore, the commit is
160
+ # qualified duo to the proto change eventually.
161
+ and (not ConfigChange .__is_added (changes ))
162
+ and (
163
+ not ConfigChange .__is_qualified_build_change (
164
+ commit = commit , build_file_path = file_path
165
+ )
166
+ )
167
+ ):
168
+ continue
151
169
# Even though a commit usually only changes one
152
170
# library, we don't want to miss generating a
153
171
# library because the commit may change multiple
@@ -156,3 +174,30 @@ def __create_qualified_commit(
156
174
if len (libraries ) == 0 :
157
175
return None
158
176
return QualifiedCommit (commit = commit , libraries = libraries )
177
+
178
+ @staticmethod
179
+ def __is_added (changes : dict [str , int ]) -> bool :
180
+ return changes [INSERTIONS ] == changes [LINES ]
181
+
182
+ @staticmethod
183
+ def __is_qualified_build_change (commit : Commit , build_file_path : str ) -> bool :
184
+ """
185
+ Checks if the given commit containing a BUILD.bazel change is a
186
+ qualified commit.
187
+
188
+ The commit is a qualified commit if the
189
+ :class:`library_generation.model.gapic_inputs.GapicInputs` objects
190
+ parsed from the commit and its parent are different, since there are
191
+ changes in fields that used in library generation.
192
+
193
+ :param commit: a GitHub commit object.
194
+ :param build_file_path: the path of the BUILD.bazel
195
+ :return: True if the commit is a qualified commit; False otherwise.
196
+ """
197
+ versioned_proto_path = find_versioned_proto_path (build_file_path )
198
+ build = str ((commit .tree / build_file_path ).data_stream .read ())
199
+ parent_commit = commit .parents [0 ]
200
+ parent_build = str ((parent_commit .tree / build_file_path ).data_stream .read ())
201
+ inputs = parse_build_str (build , versioned_proto_path )
202
+ parent_inputs = parse_build_str (parent_build , versioned_proto_path )
203
+ return inputs != parent_inputs
0 commit comments