|
| 1 | +name: 'Java Format Check' |
| 2 | +description: 'Check Java files formatting using google-java-format (Ubuntu/Linux runners only)' |
| 3 | + |
| 4 | +inputs: |
| 5 | + version: |
| 6 | + description: 'The version of google-java-format to use. Use without the initial "v".' |
| 7 | + required: false |
| 8 | + default: '1.28.0' |
| 9 | + working-directory: |
| 10 | + description: 'The directory to search for Java files (relative to repository root)' |
| 11 | + required: true |
| 12 | + |
| 13 | +runs: |
| 14 | + using: 'composite' |
| 15 | + steps: |
| 16 | + - name: Run google-java-format |
| 17 | + if: runner.os == 'Linux' |
| 18 | + shell: bash |
| 19 | + run: | |
| 20 | + GJF_VERSION="${{ inputs.version }}" |
| 21 | + WORKING_DIR="${{ inputs.working-directory }}" |
| 22 | + |
| 23 | + echo "Downloading google-java-format v${GJF_VERSION}..." |
| 24 | + curl -L -s -o google-java-format.jar "https://github.com/google/google-java-format/releases/download/v${GJF_VERSION}/google-java-format-${GJF_VERSION}-all-deps.jar" |
| 25 | + |
| 26 | + echo "Finding all .java files in ${WORKING_DIR}..." |
| 27 | + java_files=$(find "${WORKING_DIR}" -type f -name "*.java" -not -path "./.git/*") |
| 28 | + |
| 29 | + if [ -z "$java_files" ]; then |
| 30 | + echo "No Java files found in ${WORKING_DIR}" |
| 31 | + exit 0 |
| 32 | + fi |
| 33 | + |
| 34 | + echo "Found $(echo "$java_files" | wc -l) Java files to check" |
| 35 | +
|
| 36 | + non_compliant_files=$(echo "$java_files" | xargs java -jar google-java-format.jar -n) |
| 37 | + |
| 38 | + if [ -n "$non_compliant_files" ]; then |
| 39 | + echo "❌ The following Java files are not formatted correctly:" |
| 40 | + echo "$non_compliant_files" |
| 41 | + echo "" |
| 42 | + echo "To fix formatting issues, run:" |
| 43 | + echo "find \"${WORKING_DIR}\" -name '*.java' -not -path './.git/*' | xargs java -jar google-java-format.jar --replace" |
| 44 | + exit 1 |
| 45 | + else |
| 46 | + echo "✅ All Java files are properly formatted." |
| 47 | + fi |
| 48 | + |
| 49 | + - name: Unsupported OS |
| 50 | + if: runner.os != 'Linux' |
| 51 | + shell: bash |
| 52 | + run: | |
| 53 | + echo "❌ This action only supports Linux runners" |
| 54 | + echo "Please use a Linux runner to run this action." |
| 55 | + exit 1 |
| 56 | +
|
| 57 | +branding: |
| 58 | + icon: 'check-circle' |
| 59 | + color: 'green' |
0 commit comments