Skip to content

Commit dd7aebc

Browse files
committed
feat: enhance pre-commit workflow with improved caching and cross-platform support
1 parent 4b2d3f9 commit dd7aebc

2 files changed

Lines changed: 59 additions & 37 deletions

File tree

.github/workflows/pre-commit.yml

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,94 @@ name: Lint with Pre-commit
22

33
on:
44
push:
5-
branches: [master, main]
5+
branches: [master, main, develop]
66
pull_request:
7-
branches: [master, main]
7+
branches: [master, main, develop]
88

99
jobs:
10-
gradle-code-quality:
11-
name: Gradle Code Quality
10+
pre-commit-checks:
11+
name: Pre-commit Checks (JDK ${{ matrix.java-version }})
1212
runs-on: ubuntu-latest
1313

1414
strategy:
1515
matrix:
1616
java-version: [21, 25]
17+
fail-fast: false
1718

1819
permissions:
1920
contents: read
2021
pull-requests: write
22+
checks: write
2123

2224
steps:
2325
- name: Checkout repository
2426
uses: actions/checkout@v5
27+
with:
28+
fetch-depth: 0
2529

2630
- name: Set up JDK ${{ matrix.java-version }}
2731
uses: actions/setup-java@v5
2832
with:
2933
distribution: temurin
3034
java-version: ${{ matrix.java-version }}
35+
cache: gradle
3136

3237
- name: Setup Gradle
3338
uses: gradle/actions/setup-gradle@v5
3439
with:
3540
build-scan-publish: true
3641
build-scan-terms-of-use-url: "https://gradle.com/help/legal-terms-of-use"
3742
build-scan-terms-of-use-agree: "yes"
43+
cache-read-only: ${{ github.ref != 'refs/heads/main' && github.ref != 'refs/heads/master' }}
44+
45+
- name: Cache pre-commit environments
46+
uses: actions/cache@v4
47+
with:
48+
path: |
49+
~/.cache/pre-commit
50+
~/.local/share/pre-commit
51+
key: pre-commit-${{ hashFiles('.pre-commit-config.yaml') }}-${{ runner.os }}
52+
restore-keys: |
53+
pre-commit-${{ runner.os }}
3854
39-
- name: Set up Python (for pre-commit)
55+
- name: Set up Python
4056
uses: actions/setup-python@v5
4157
with:
4258
python-version: '3.13'
59+
cache: 'pip'
4360

4461
- name: Install pre-commit
4562
run: pip install pre-commit
4663

64+
- name: Make gradlew executable
65+
run: chmod +x ./gradlew
66+
4767
- name: Run pre-commit hooks
4868
id: precommit
69+
continue-on-error: false
4970
run: |
50-
chmod +x ./gradlew
51-
pre-commit run --all-files --show-diff-on-failure --color always
71+
set -e # Exit immediately on error
72+
echo "Running pre-commit checks..."
73+
pre-commit run --all-files --show-diff-on-failure --color=always --verbose
5274
53-
- name: Upload reports
54-
if: always()
55-
uses: actions/upload-artifact@v4
75+
- name: Comment on PR with results
76+
if: github.event_name == 'pull_request' && always()
77+
uses: actions/github-script@v7
5678
with:
57-
name: reports-jdk-${{ matrix.java-version }}
58-
path: |
59-
build/reports/
60-
**/build/reports/
79+
script: |
80+
const result = '${{ steps.precommit.outcome }}';
81+
if (result === 'failure') {
82+
github.rest.issues.createComment({
83+
issue_number: context.issue.number,
84+
owner: context.repo.owner,
85+
repo: context.repo.repo,
86+
body: `❌ Pre-commit checks failed for JDK ${{ matrix.java-version }}. Please check the workflow run for details.`
87+
});
88+
} else if (result === 'success') {
89+
github.rest.issues.createComment({
90+
issue_number: context.issue.number,
91+
owner: context.repo.owner,
92+
repo: context.repo.repo,
93+
body: `✅ Pre-commit checks passed for JDK ${{ matrix.java-version }}!`
94+
});
95+
}

.pre-commit-config.yaml

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,19 @@ repos:
1616
- id: trailing-whitespace
1717

1818
# -------------------------
19-
# 2. Linting - Spotless
20-
# -------------------------
21-
- repo: local
22-
hooks:
23-
- id: spotless-check
24-
name: Gradle Spotless (format checker)
25-
entry: bash -c "./gradlew spotlessCheck"
26-
language: system
27-
types: [java]
28-
pass_filenames: false
29-
30-
- id: spotless-apply
31-
name: Gradle Spotless (auto format)
32-
entry: bash -c "./gradlew spotlessApply"
33-
language: system
34-
types: [java]
35-
pass_filenames: false
36-
37-
# -------------------------
38-
# 3. Java - Check Code quality
19+
# 2. Java - Check Code quality
3920
# -------------------------
4021
- repo: local
4122
hooks:
4223
- id: code-quality
43-
name: Gradle Code Quality (Spotless + Checkstyle + SpotBugs + PMD)
44-
entry: bash -c "./gradlew codeQuality"
24+
name: Gradle Code Quality
25+
entry: python
26+
args:
27+
- -c
28+
- |
29+
import os, subprocess, sys
30+
cmd = 'gradlew.bat' if os.name == 'nt' else './gradlew'
31+
sys.exit(subprocess.run([cmd, 'codeQuality']).returncode)
4532
language: system
46-
types: [java]
4733
pass_filenames: false
34+
always_run: true

0 commit comments

Comments
 (0)