Skip to content

Commit e5b70b8

Browse files
authored
Merge pull request #92 from jagdish-15/add-lint-build
Add lint build
2 parents e763014 + 116ea88 commit e5b70b8

5 files changed

Lines changed: 100 additions & 44 deletions

File tree

.github/workflows/java.yml

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ jobs:
4242
- name: Check if changed exercise tests compile cleanly
4343
run: bin/build-changed-exercise
4444

45-
lint:
46-
name: Lint Java files using Checkstyle
45+
lint-all:
46+
name: Lint all Java files using Checkstyle
47+
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
4748
runs-on: ubuntu-24.04
4849
steps:
4950
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
@@ -56,6 +57,22 @@ jobs:
5657
run: ./gradlew check --exclude-task test --continue
5758
working-directory: exercises
5859

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 21
69+
uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654
70+
with:
71+
java-version: 21
72+
distribution: "temurin"
73+
- name: Lint changed exercises
74+
run: bin/lint-changed-exercise
75+
5976
test-all:
6077
name: Test all exercises using java-test-runner
6178
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'

bin/build-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 build suite..."
19-
cd exercises && ./gradlew compileStarterTestJava --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 || true)
7+
check_gradle_changes \
8+
"cd exercises && ./gradlew compileStarterTestJava --continue" \
9+
"Gradle build files changed, running full build suite..."
2710

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

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'; 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)