Skip to content

Commit 823205f

Browse files
committed
Add lint-changed task in Java workflow
1 parent 7f2ae02 commit 823205f

2 files changed

Lines changed: 73 additions & 2 deletions

File tree

.github/workflows/java.yml

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ jobs:
2525
run: ./gradlew compileStarterTestJava --continue
2626
working-directory: exercises
2727

28-
lint:
29-
name: Lint Java files using Checkstyle
28+
lint-all:
29+
name: Lint all Java files using Checkstyle
30+
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
3031
runs-on: ubuntu-24.04
3132
steps:
3233
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
@@ -39,6 +40,22 @@ jobs:
3940
run: ./gradlew check --exclude-task test --continue
4041
working-directory: exercises
4142

43+
lint-changed:
44+
name: Lint changed Java exercises using Checkstyle
45+
if: github.event_name == 'pull_request'
46+
runs-on: ubuntu-24.04
47+
steps:
48+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
49+
with:
50+
fetch-depth: 0
51+
- name: Set up JDK 21
52+
uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654
53+
with:
54+
java-version: 21
55+
distribution: "temurin"
56+
- name: Lint changed exercises
57+
run: bin/lint-changed-exercise
58+
4259
test-all:
4360
name: Test all exercises using java-test-runner
4461
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'

bin/lint-changed-exercise

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
# 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 lint suite..."
19+
cd exercises && ./gradlew check --exclude-task test --continue
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)
27+
28+
if [ -z "$changed_exercises" ]; then
29+
echo "No relevant exercises changed, skipping linting."
30+
exit 0
31+
fi
32+
33+
# Print exercises
34+
echo "Changed exercises detected:"
35+
echo "$changed_exercises"
36+
echo "----------------------------------------"
37+
38+
# Run lint checks
39+
exit_code=0
40+
for dir in $changed_exercises; do
41+
slug=$(basename "$dir")
42+
43+
echo "========================================"
44+
echo "=== Running checkstyle for $slug ==="
45+
echo "========================================"
46+
47+
if [[ $dir == exercises/practice/* ]]; then
48+
./exercises/gradlew -p exercises ":practice:$slug:check" --exclude-task test || exit_code=1
49+
elif [[ $dir == exercises/concept/* ]]; then
50+
./exercises/gradlew -p exercises ":concept:$slug:check" --exclude-task test || exit_code=1
51+
fi
52+
done
53+
54+
exit $exit_code

0 commit comments

Comments
 (0)