Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions .github/workflows/java.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ jobs:
run: ./gradlew compileStarterTestJava --continue
working-directory: exercises

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

lint-changed:
name: Lint changed Java exercises using Checkstyle
if: github.event_name == 'pull_request'
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
with:
fetch-depth: 0
- name: Set up JDK 21
uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654
with:
java-version: 21
distribution: "temurin"
- name: Lint changed exercises
run: bin/lint-changed-exercise

test-all:
name: Test all exercises using java-test-runner
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
Expand Down
54 changes: 54 additions & 0 deletions bin/lint-changed-exercise
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This script seems quite similar to the build changed one in the other PR. Have you considered seeing if there is some way to share the common parts?

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env bash
set -eo pipefail

# Determine the base branch of the PR
BASE_BRANCH=${GITHUB_BASE_REF:-main}

# Fetch full history for proper diff
git fetch origin "$BASE_BRANCH"

# Compute merge base
MERGE_BASE=$(git merge-base HEAD origin/"$BASE_BRANCH")

# Get changed files relative to merge base
changed_files=$(git diff --name-only "$MERGE_BASE" HEAD)

# If any Gradle build file changed, run the full suite and exit
if echo "$changed_files" | grep -qE '\.(gradle|gradlew|bat)$|settings\.gradle'; then
echo "Gradle build files changed, running full lint suite..."
cd exercises && ./gradlew check --exclude-task test --continue
exit 0
fi

# Extract unique exercise directories
changed_exercises=$(echo "$changed_files" | \
grep -E '^exercises/(practice|concept)/[^/]+/.+\.java$' | \
cut -d/ -f1-3 | sort -u)

if [ -z "$changed_exercises" ]; then
echo "No relevant exercises changed, skipping linting."
exit 0
fi

# Print exercises
echo "Changed exercises detected:"
echo "$changed_exercises"
echo "----------------------------------------"

# Run lint checks
exit_code=0
for dir in $changed_exercises; do
slug=$(basename "$dir")

echo "========================================"
echo "=== Running checkstyle for $slug ==="
echo "========================================"

if [[ $dir == exercises/practice/* ]]; then
./exercises/gradlew -p exercises ":practice:$slug:check" --exclude-task test || exit_code=1
elif [[ $dir == exercises/concept/* ]]; then
./exercises/gradlew -p exercises ":concept:$slug:check" --exclude-task test || exit_code=1
fi
done

exit $exit_code