-
Notifications
You must be signed in to change notification settings - Fork 0
140 lines (118 loc) · 4.46 KB
/
pr-build-check.yml
File metadata and controls
140 lines (118 loc) · 4.46 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
name: Pull Request Build Check
on:
pull_request:
branches:
- main
- development
jobs:
build:
name: Build and Check Code Format
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
cache: 'gradle'
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew build --console=plain
- name: Check code formatting
id: spotless
run: |
# Run spotlessCheck and capture output
set -o pipefail
if ./gradlew spotlessCheck --console=plain 2>&1 | tee spotless-output.txt; then
echo "formatted=true" >> $GITHUB_OUTPUT
echo "✓ Code formatting check passed"
else
echo "formatted=false" >> $GITHUB_OUTPUT
echo "⚠️ Code formatting violations detected"
fi
continue-on-error: true
- name: Comment on PR with formatting violations
if: steps.spotless.outputs.formatted == 'false'
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const output = fs.readFileSync('spotless-output.txt', 'utf8');
// Extract the relevant violation information
const violationMatch = output.match(/The following files had format violations:([\s\S]*?)(?:Run '\.\/gradlew|$)/);
const violations = violationMatch ? violationMatch[0] : output;
const comment = `## ⚠️ Code Formatting Issues Detected
The following formatting violations were found:
\`\`\`
${violations}
\`\`\`
**To fix these issues locally:**
\`\`\`bash
./gradlew spotlessApply
\`\`\`
Then commit and push the changes.`;
// Check if we already commented
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const botComment = comments.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('Code Formatting Issues Detected')
);
if (botComment) {
// Update existing comment
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: comment
});
} else {
// Create new comment
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: comment
});
}
- name: Comment on PR - All checks passed
if: steps.spotless.outputs.formatted == 'true'
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
// Check if there's an existing formatting comment to delete
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const botComment = comments.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('Code Formatting Issues Detected')
);
if (botComment) {
// Delete the old formatting warning since it's now fixed
await github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id
});
}
// Post success comment
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: '✓ Build successful and code formatting check passed!'
});