Skip to content

Commit c020ed7

Browse files
authored
Add lint-changed and build-changed task in java workflow (#3126)
* Add lint-changed task in Java workflow * ci: implement build-changed strategy for targeted test compilation * Combine common logic * Fix script permissions in Git index * Update Java version to 25 in CI * Add gradle wrapper files in detection
1 parent 22e9bc2 commit c020ed7

5 files changed

Lines changed: 155 additions & 28 deletions

File tree

.github/workflows/java.yml

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,68 @@ on:
1111
workflow_dispatch:
1212

1313
jobs:
14-
build:
14+
build-all:
1515
name: Check if tests compile cleanly with starter sources
16+
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
1617
runs-on: ubuntu-24.04
1718
steps:
1819
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
19-
- name: Set up JDK 21
20+
- name: Set up JDK 25
2021
uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654
2122
with:
22-
java-version: 21
23+
java-version: 25
2324
distribution: "temurin"
2425
- name: Check if tests compile cleanly with starter sources
2526
run: ./gradlew compileStarterTestJava --continue
2627
working-directory: exercises
2728

28-
lint:
29-
name: Lint Java files using Checkstyle
29+
build-changed:
30+
name: Check if changed exercise tests compile cleanly with starter sources
31+
if: github.event_name == 'pull_request'
32+
runs-on: ubuntu-24.04
33+
steps:
34+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
35+
with:
36+
fetch-depth: 0
37+
- name: Set up JDK 25
38+
uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654
39+
with:
40+
java-version: 25
41+
distribution: "temurin"
42+
- name: Check if changed exercise tests compile cleanly
43+
run: bin/build-changed-exercise
44+
45+
lint-all:
46+
name: Lint all Java files using Checkstyle
47+
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
3048
runs-on: ubuntu-24.04
3149
steps:
3250
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
33-
- name: Set up JDK 21
51+
- name: Set up JDK 25
3452
uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654
3553
with:
36-
java-version: 21
54+
java-version: 25
3755
distribution: "temurin"
3856
- name: Run checkstyle
3957
run: ./gradlew check --exclude-task test --continue
4058
working-directory: exercises
4159

60+
lint-changed:
61+
name: Lint changed Java exercises using Checkstyle
62+
if: github.event_name == 'pull_request'
63+
runs-on: ubuntu-24.04
64+
steps:
65+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
66+
with:
67+
fetch-depth: 0
68+
- name: Set up JDK 25
69+
uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654
70+
with:
71+
java-version: 25
72+
distribution: "temurin"
73+
- name: Lint changed exercises
74+
run: bin/lint-changed-exercise
75+
4276
test-all:
4377
name: Test all exercises using java-test-runner
4478
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'

bin/build-changed-exercise

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env bash
2+
set -eo pipefail
3+
4+
source "$(dirname "$0")/common.sh"
5+
6+
# If any Gradle build file changed, run the full suite and exit
7+
check_gradle_changes \
8+
"cd exercises && ./gradlew compileStarterTestJava --continue" \
9+
"Gradle build files changed, running full build suite..."
10+
11+
if [ -z "$changed_exercises" ]; then
12+
echo "No relevant exercises changed, skipping compile checks."
13+
exit 0
14+
fi
15+
16+
# Print exercises
17+
echo "Changed exercises detected:"
18+
echo "$changed_exercises"
19+
echo "----------------------------------------"
20+
21+
# Run build compile checks
22+
exit_code=0
23+
for dir in $changed_exercises; do
24+
slug=$(basename "$dir")
25+
26+
echo "========================================"
27+
echo "=== Running compileStarterTestJava for $slug ==="
28+
echo "========================================"
29+
30+
if [[ $dir == exercises/practice/* ]]; then
31+
./exercises/gradlew -p exercises ":practice:$slug:compileStarterTestJava" || exit_code=1
32+
elif [[ $dir == exercises/concept/* ]]; then
33+
./exercises/gradlew -p exercises ":concept:$slug:compileStarterTestJava" || exit_code=1
34+
fi
35+
done
36+
37+
exit $exit_code

bin/common.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env bash
2+
set -eo pipefail
3+
4+
# Determine the base branch of the PR
5+
BASE_BRANCH=${GITHUB_BASE_REF:-main}
6+
7+
# Fetch full history for proper diff
8+
git fetch origin "$BASE_BRANCH"
9+
10+
# Compute merge base
11+
MERGE_BASE=$(git merge-base HEAD origin/"$BASE_BRANCH")
12+
13+
# Get changed files relative to merge base
14+
changed_files=$(git diff --name-only "$MERGE_BASE" HEAD)
15+
16+
# Function to check if Gradle build files changed and run a command
17+
check_gradle_changes() {
18+
local command="$1"
19+
local message="$2"
20+
21+
if echo "$changed_files" | grep -qE '(\.gradle|gradlew|\.bat|settings\.gradle|gradle-wrapper\.(properties|jar))$'; then
22+
echo "$message"
23+
eval "$command"
24+
exit 0
25+
fi
26+
}
27+
28+
# Extract unique exercise directories
29+
get_changed_exercises() {
30+
echo "$changed_files" | \
31+
grep -E '^exercises/(practice|concept)/[^/]+/.+\.java$' | \
32+
cut -d/ -f1-3 | sort -u || true
33+
}
34+
35+
# Variable for reuse
36+
changed_exercises=$(get_changed_exercises)

bin/lint-changed-exercise

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env bash
2+
set -eo pipefail
3+
4+
source "$(dirname "$0")/common.sh"
5+
6+
# If any Gradle build file changed, run the full suite and exit
7+
check_gradle_changes \
8+
"cd exercises && ./gradlew check --exclude-task test --continue" \
9+
"Gradle build files changed, running full lint suite..."
10+
11+
if [ -z "$changed_exercises" ]; then
12+
echo "No relevant exercises changed, skipping linting."
13+
exit 0
14+
fi
15+
16+
# Print exercises
17+
echo "Changed exercises detected:"
18+
echo "$changed_exercises"
19+
echo "----------------------------------------"
20+
21+
# Run lint checks
22+
exit_code=0
23+
for dir in $changed_exercises; do
24+
slug=$(basename "$dir")
25+
26+
echo "========================================"
27+
echo "=== Running checkstyle for $slug ==="
28+
echo "========================================"
29+
30+
if [[ $dir == exercises/practice/* ]]; then
31+
./exercises/gradlew -p exercises ":practice:$slug:check" --exclude-task test || exit_code=1
32+
elif [[ $dir == exercises/concept/* ]]; then
33+
./exercises/gradlew -p exercises ":concept:$slug:check" --exclude-task test || exit_code=1
34+
fi
35+
done
36+
37+
exit $exit_code

bin/test-changed-exercise

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,12 @@
11
#!/usr/bin/env bash
22
set -eo pipefail
33

4-
# Determine the base branch of the PR
5-
BASE_BRANCH=${GITHUB_BASE_REF:-main}
6-
7-
# Fetch full history for proper diff
8-
git fetch origin "$BASE_BRANCH"
9-
10-
# Compute merge base
11-
MERGE_BASE=$(git merge-base HEAD origin/"$BASE_BRANCH")
12-
13-
# Get changed files relative to merge base
14-
changed_files=$(git diff --name-only "$MERGE_BASE" HEAD)
4+
source "$(dirname "$0")/common.sh"
155

166
# If any Gradle build file changed, run the full suite and exit
17-
if echo "$changed_files" | grep -qE '\.(gradle|gradlew|bat)$|settings\.gradle'; then
18-
echo "Gradle build files changed, running full test suite..."
19-
./bin/test-with-test-runner
20-
exit 0
21-
fi
22-
23-
# Extract unique exercise directories
24-
changed_exercises=$(echo "$changed_files" | \
25-
grep -E '^exercises/(practice|concept)/[^/]+/.+\.java$' | \
26-
cut -d/ -f1-3 | sort -u)
7+
check_gradle_changes \
8+
"./bin/test-with-test-runner" \
9+
"Gradle build files changed, running full test suite..."
2710

2811
if [ -z "$changed_exercises" ]; then
2912
echo "No relevant exercises changed, skipping tests."

0 commit comments

Comments
 (0)