-
Notifications
You must be signed in to change notification settings - Fork 101
75 lines (64 loc) · 2.71 KB
/
formatting.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
name: Format code
on:
pull_request:
branches: [ master ]
paths-ignore:
- '**.md'
- '.github/workflows/**'
env:
java_version: '17'
java_distribution: 'zulu'
jobs:
formatting:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK ${{ env.java_version }}-${{ env.java_distribution }}
uses: actions/setup-java@v3
with:
java-version: ${{ env.java_version }}
distribution: ${{ env.java_distribution }}
- name: Cache Gradle packages
uses: actions/cache@v3
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
restore-keys: ${{ runner.os }}-gradle
- name: Run Spotless Check
run: |
./gradlew spotlessCheck --quiet --continue 2>&1 | tee spotless-output.txt
if grep -q "\.java" spotless-output.txt; then
exit 1
fi
- name: Upload Spotless Output
if: ${{ failure() }}
uses: actions/upload-artifact@v4
with:
name: spotless-output
path: spotless-output.txt
- name: Extract Affected Files
if: ${{ failure() }}
run: |
grep "\.java" spotless-output.txt > affected-files.txt || true
- name: Comment PR if code does not comply to Google Java style guide
if: ${{ failure() }}
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const files = fs.existsSync('affected-files.txt') ? fs.readFileSync('affected-files.txt', 'utf8').trim().split('\n') : [];
if (files.length === 0) return;
const actionsDashboardUrl = `https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}`;
const commentBody = `### 🚨 Code Formatting Issue 🚨\n\n`
+ `This contribution does not follow the conventions set by the [Google Java Style Guide](https://google.github.io/styleguide/javaguide.html).\n\n`
+ `Please run the following command at the root of the project to fix formatting errors:\n\n`
+ "```sh\n./gradlew spotlessApply\n```\n\n"
+ `<details><summary><strong>🗂️ Affected files</strong></summary>\n${files.map(file => `- \`${file.trim()}\``).join('\n')}</details>\n\n`
+ `[Go to the Actions Dashboard to download the full Spotless output](${actionsDashboardUrl})\n\n`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: commentBody
});