Skip to content

Commit d703206

Browse files
committed
ci: Run the fmt plugin on in the Java source files
1 parent d1b481b commit d703206

File tree

1 file changed

+50
-7
lines changed

1 file changed

+50
-7
lines changed

.kokoro/build.sh

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,17 @@ case ${JOB_TYPE} in
194194
echo "Running in subdir: ${BUILD_SUBDIR}"
195195
pushd "${BUILD_SUBDIR}"
196196
fi
197+
198+
MODULE_FILTER=""
199+
197200
if [ -n "${BASE_SHA}" ] && [ -n "${HEAD_SHA}" ]; then
201+
# Optimize the build by identifying ONLY the Maven modules that contain changed Java source files.
202+
# Format those specific modules instead of the entire codebase.
198203
changed_file_list=$(git diff --name-only "${BASE_SHA}" "${HEAD_SHA}")
199204
echo "${changed_file_list}"
200-
205+
201206
has_code_change="false"
202-
207+
203208
while IFS= read -r changed_file; do
204209
# Checks if the line is not empty AND if it matches a .java file
205210
if [ -n "${changed_file}" ] && [[ "${changed_file}" == *.java ]]; then
@@ -208,19 +213,57 @@ case ${JOB_TYPE} in
208213
break
209214
fi
210215
done <<< "${changed_file_list}"
211-
216+
212217
if [ "${has_code_change}" == "false" ]; then
213218
echo "No java modules affected. Skipping linter check."
214219
exit 0
215220
fi
221+
222+
# Compute list of changed Maven modules from changed Java files.
223+
# We walk each changed .java file up to its nearest pom.xml to find the correct module.
224+
# e.g., if "java-asset/google-cloud-asset/src/main/java/Foo.java" is changed,
225+
# it traverses upward until finding "java-asset/google-cloud-asset/pom.xml" and adds that module.
226+
changed_modules=()
227+
while IFS= read -r changed_file; do
228+
if [ -n "${changed_file}" ] && [[ "${changed_file}" == *.java ]]; then
229+
dir=$(dirname "${changed_file}")
230+
while [ "${dir}" != "." ] && [ ! -f "${dir}/pom.xml" ]; do
231+
dir=$(dirname "${dir}")
232+
done
233+
if [ -f "${dir}/pom.xml" ] && [ "${dir}" != "." ]; then
234+
# Filter out samples, directories with no Java source code, and generated protobuf/gRPC code
235+
if [[ "${dir}" != *"samples"* ]] && \
236+
[[ "$(basename "${dir}")" != "proto-google-"* ]] && \
237+
[[ "$(basename "${dir}")" != "grpc-google-"* ]] && \
238+
[[ "$(basename "${dir}")" != *"-bom" ]] && \
239+
[[ "$(basename "${dir}")" != "google-cloud-pom-parent" ]] && \
240+
[[ "$(basename "${dir}")" != "google-cloud-jar-parent" ]]; then
241+
242+
changed_modules+=("${dir}")
243+
fi
244+
fi
245+
fi
246+
done <<< "${changed_file_list}"
247+
248+
echo "Changed Modules: ${changed_modules[*]}"
249+
250+
# Deduplicate the modules using sort -u, so that we pass a concise list of unique modules
251+
# via the Maven `-pl` argument.
252+
if [ ${#changed_modules[@]} -gt 0 ]; then
253+
unique_modules=$(printf '%s\n' "${changed_modules[@]}" | sort -u | paste -sd ',' -)
254+
MODULE_FILTER="-pl ${unique_modules}"
255+
echo "Formatting only changed modules: ${unique_modules}"
256+
fi
216257
else
217-
echo "BASE_SHA or HEAD_SHA is empty. Skipping file difference check."
258+
echo "BASE_SHA or HEAD_SHA is empty. Cannot continue linting."
259+
exit 1
218260
fi
219-
261+
220262
mvn -B -ntp \
221-
-T 1.5C \
263+
-T 1C \
264+
${MODULE_FILTER} \
222265
com.spotify.fmt:fmt-maven-plugin:check
223-
mvn -B -ntp checkstyle:check@checkstyle
266+
mvn -B -ntp ${MODULE_FILTER} checkstyle:check@checkstyle
224267

225268
if [[ -n "${BUILD_SUBDIR}" ]]
226269
then

0 commit comments

Comments
 (0)