@@ -71,13 +71,18 @@ case ${JOB_TYPE} in
7171 if [[ " $( release_please_snapshot_pull_request) " == " true" ]]; then
7272 echo " Skipping integration tests as this is Release Please SNAPSHOT pull request."
7373 elif [[ ${# modified_module_list[@]} -gt 0 ]]; then
74- module_list=$(
75- IFS=,
76- echo " ${modified_module_list[*]} "
77- )
78- setup_cloud " $module_list "
79- install_modules " $module_list "
80- run_integration_tests " $module_list "
74+ filter_modules_with_integration_tests
75+ if [[ ${# filtered_it_module_list[@]} -eq 0 ]]; then
76+ echo " No modified modules contain integration tests. Skipping."
77+ else
78+ module_list=$(
79+ IFS=,
80+ echo " ${filtered_it_module_list[*]} "
81+ )
82+ setup_cloud " $module_list "
83+ install_modules " $module_list "
84+ run_integration_tests " $module_list "
85+ fi
8186 else
8287 echo " No Integration Tests to run"
8388 fi
@@ -157,7 +162,7 @@ case ${JOB_TYPE} in
157162 fi
158163 ;;
159164 graalvm-single)
160- generate_modified_modules_list false
165+ generate_modified_modules_list false
161166 if [[ " $( release_please_snapshot_pull_request) " == " true" ]]; then
162167 echo " Not running GraalVM checks -- this is Release Please SNAPSHOT pull request."
163168 elif [[ ! " ${modified_module_list[*]} " =~ " ${BUILD_SUBDIR} " ]]; then
@@ -194,12 +199,19 @@ case ${JOB_TYPE} in
194199 echo " Running in subdir: ${BUILD_SUBDIR} "
195200 pushd " ${BUILD_SUBDIR} "
196201 fi
202+
203+ MODULE_FILTER=" "
204+
197205 if [ -n " ${BASE_SHA} " ] && [ -n " ${HEAD_SHA} " ]; then
198- changed_file_list=$( git diff --name-only " ${BASE_SHA} " " ${HEAD_SHA} " )
206+ # Optimize the build by identifying ONLY the Maven modules that contain changed Java source files.
207+ # Format those specific modules instead of the entire codebase, reducing format check time.
208+ # The --relative flag is when building in the submodule as only files modified in the module
209+ # should be accounted for.
210+ changed_file_list=$( git diff --name-only " ${BASE_SHA} " " ${HEAD_SHA} " --relative)
199211 echo " ${changed_file_list} "
200-
212+
201213 has_code_change=" false"
202-
214+
203215 while IFS= read -r changed_file; do
204216 # Checks if the line is not empty AND if it matches a .java file
205217 if [ -n " ${changed_file} " ] && [[ " ${changed_file} " == * .java ]]; then
@@ -208,19 +220,60 @@ case ${JOB_TYPE} in
208220 break
209221 fi
210222 done <<< " ${changed_file_list}"
211-
223+
212224 if [ " ${has_code_change} " == " false" ]; then
213225 echo " No java modules affected. Skipping linter check."
214226 exit 0
215227 fi
228+
229+ # Compute list of changed Maven modules from changed Java files.
230+ # We walk each changed .java file up to its nearest pom.xml to find the correct module.
231+ # e.g., if "java-asset/google-cloud-asset/src/main/java/Foo.java" is changed,
232+ # it traverses upward until finding "java-asset/google-cloud-asset/pom.xml" and adds that module.
233+ changed_modules=()
234+ while IFS= read -r changed_file; do
235+ if [ -n " ${changed_file} " ] && [[ " ${changed_file} " == * .java ]]; then
236+ dir=$( dirname " ${changed_file} " )
237+ while [ " ${dir} " != " ." ] && [ ! -f " ${dir} /pom.xml" ]; do
238+ dir=$( dirname " ${dir} " )
239+ done
240+ if [ -f " ${dir} /pom.xml" ] && [ " ${dir} " != " ." ]; then
241+ # Filter out directories not participating in the default formatting reactor:
242+ # - samples are handwritten by developers
243+ # - proto-*/grpc-* are generated code and should use the compiler format
244+ # - *-bom/parents are POM-only and contain no Java source
245+ if [[ " ${dir} " != * " samples" * ]] && \
246+ [[ " $( basename " ${dir} " ) " != " proto-google-" * ]] && \
247+ [[ " $( basename " ${dir} " ) " != " grpc-google-" * ]] && \
248+ [[ " $( basename " ${dir} " ) " != * " -bom" ]] && \
249+ [[ " $( basename " ${dir} " ) " != " google-cloud-pom-parent" ]] && \
250+ [[ " $( basename " ${dir} " ) " != " google-cloud-jar-parent" ]]; then
251+
252+ changed_modules+=(" ${dir} " )
253+ fi
254+ fi
255+ fi
256+ done <<< " ${changed_file_list}"
257+
258+ echo " Changed Modules: ${changed_modules[*]} "
259+
260+ # Deduplicate the modules using sort -u to pass a concise list of unique modules
261+ # via the Maven `-pl` argument.
262+ if [ ${# changed_modules[@]} -gt 0 ]; then
263+ unique_modules=$( printf ' %s\n' " ${changed_modules[@]} " | sort -u | paste -sd ' ,' -)
264+ MODULE_FILTER=" -pl ${unique_modules} "
265+ echo " Formatting only changed modules: ${unique_modules} "
266+ fi
216267 else
217- echo " BASE_SHA or HEAD_SHA is empty. Skipping file difference check."
268+ echo " BASE_SHA or HEAD_SHA is empty. Cannot continue linting."
269+ exit 1
218270 fi
219-
271+
220272 mvn -B -ntp \
221- -T 1.5C \
273+ -T 1C \
274+ ${MODULE_FILTER} \
222275 com.spotify.fmt:fmt-maven-plugin:check
223- mvn -B -ntp checkstyle:check@checkstyle
276+ mvn -B -ntp ${MODULE_FILTER} checkstyle:check@checkstyle
224277
225278 if [[ -n " ${BUILD_SUBDIR} " ]]
226279 then
@@ -245,4 +298,4 @@ if [[ "${ENABLE_FLAKYBOT}" == "true" ]]; then
245298fi
246299
247300echo " exiting with ${RETURN_CODE} "
248- exit ${RETURN_CODE}
301+ exit ${RETURN_CODE}
0 commit comments