-
Notifications
You must be signed in to change notification settings - Fork 28
280 lines (237 loc) · 8.2 KB
/
Copy pathpr-check.yml
File metadata and controls
280 lines (237 loc) · 8.2 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
name: PR Check
# PR 检查工作流:每次 PR 创建或更新时自动运行
# 确保代码质量、运行测试、验证构建
on:
pull_request:
branches: [ main ]
# 触发事件类型:
# - opened: PR 首次创建
# - synchronize: PR 分支有新 commit(关键!)
# - reopened: PR 重新打开
types: [opened, synchronize, reopened]
# 并发控制:同一 PR 的多次 push 只运行最新的一次
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
# ========================================
# Job 1: 后端检查 (Go)
# ========================================
backend-check:
name: Backend Check (Go)
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version-file: './backend/go.mod'
cache-dependency-path: './backend/go.sum'
# Go 代码格式检查
- name: Go Fmt
run: |
cd backend
echo "检查代码格式..."
if [ -n "$(gofmt -l .)" ]; then
echo "❌ 代码格式不符合规范,请运行 'gofmt -w .'"
gofmt -d .
exit 1
fi
echo "✅ 代码格式检查通过"
# Go 静态分析
- name: Go Vet
run: |
cd backend
echo "运行静态分析..."
go vet ./...
# Go 依赖检查
- name: Go Mod Verify
run: |
cd backend
echo "验证依赖完整性..."
go mod verify
# Go 单元测试
- name: Go Test
run: |
cd backend
echo "运行单元测试..."
go test ./... -v -race -coverprofile=coverage.out
# 上传测试覆盖率
- name: Upload coverage
uses: codecov/codecov-action@v4
with:
file: ./backend/coverage.out
flags: backend
fail_ci_if_error: false
continue-on-error: true
# ========================================
# Job 2: 前端检查 (React + TypeScript)
# ========================================
frontend-check:
name: Frontend Check (React)
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
cache-dependency-path: './desktop/package-lock.json'
# 安装依赖
- name: Install dependencies
run: |
cd desktop
npm ci
# ESLint 代码检查
- name: Lint
continue-on-error: true
run: |
cd desktop
echo "运行 ESLint 检查..."
npm run lint
# TypeScript 类型检查
- name: Type Check
run: |
cd desktop
echo "运行 TypeScript 类型检查..."
npm run type-check || echo "⚠️ type-check 脚本未配置,跳过"
continue-on-error: true
# 构建检查
- name: Build
run: |
cd desktop
echo "验证构建..."
npm run build
# ========================================
# Job 3: 烟雾测试
# ========================================
smoke-test:
name: Smoke Test
runs-on: ubuntu-latest
timeout-minutes: 5
needs: [backend-check, frontend-check]
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version-file: './backend/go.mod'
# 启动后端并验证健康检查
- name: Start backend and health check
run: |
cd backend
# 复制配置文件(如果需要)
if [ -f ".env.example" ]; then
cp .env.example .env
fi
# 先编译,避免首次 go run 下载/编译依赖占用健康检查等待时间
echo "编译后端服务..."
go build -o /tmp/banana-pro-server ./cmd/server/main.go
# 启动后端(后台运行)
echo "启动后端服务..."
LOG_FILE=/tmp/banana-pro-server.log
DISABLE_STDIN_MONITOR=1 /tmp/banana-pro-server > "$LOG_FILE" 2>&1 &
SERVER_PID=$!
# 等待后端启动(最多30秒)
echo "等待后端启动..."
MAX_WAIT=30
for i in $(seq 1 $MAX_WAIT); do
# 检查进程是否存活
if ! kill -0 $SERVER_PID 2>/dev/null; then
echo "❌ 后端进程意外退出"
exit 1
fi
HEALTH_PORT=$(grep -m1 -oE 'SERVER_PORT=[0-9]+' "$LOG_FILE" | cut -d= -f2 || true)
if [ -z "$HEALTH_PORT" ]; then
sleep 1
continue
fi
# 检查标准健康检查接口
if curl -sf "http://localhost:${HEALTH_PORT}/api/v1/health" > /dev/null 2>&1; then
echo "✅ 后端启动成功(${i}秒,端口 ${HEALTH_PORT})"
# 测试其他关键接口(可选)
# curl -sf http://localhost:8080/api/status
# 停止后端
kill $SERVER_PID
exit 0
fi
sleep 1
done
# 超时
echo "❌ 后端启动超时(${MAX_WAIT}秒)"
cat "$LOG_FILE" || true
kill $SERVER_PID 2>/dev/null || true
exit 1
# ========================================
# Job 4: PR 总结(可选)
# ========================================
pr-summary:
name: PR Summary
runs-on: ubuntu-latest
needs: [backend-check, frontend-check, smoke-test]
if: always()
steps:
- name: Create PR summary
uses: actions/github-script@v7
with:
script: |
const backend = '${{ needs.backend-check.result }}';
const frontend = '${{ needs.frontend-check.result }}';
const smoke = '${{ needs.smoke-test.result }}';
const statusEmoji = {
'success': '✅',
'failure': '❌',
'cancelled': '⚠️',
'skipped': '⏭️'
};
const summary = `## 🔍 PR 检查结果
| 检查项 | 状态 |
|--------|------|
| **后端检查** (Go) | ${statusEmoji[backend] || '❓'} ${backend} |
| **前端检查** (React) | ${statusEmoji[frontend] || '❓'} ${frontend} |
| **烟雾测试** | ${statusEmoji[smoke] || '❓'} ${smoke} |
${backend === 'success' && frontend === 'success' && smoke === 'success' ? '### ✅ 所有检查通过,可以合并!' : '### ⚠️ 请修复上述问题后再合并'}
---
*由 GitHub Actions 自动生成*
`;
// 如果 PR 有更新,创建或更新评论
if (context.issue.number) {
try {
// 查找现有的总结评论
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('🔍 PR 检查结果')
);
if (botComment) {
// 更新现有评论
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: summary
});
} else {
// 创建新评论
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: summary
});
}
} catch (error) {
console.log('无法创建 PR 评论:', error);
}
}
core.summary.addRaw(summary).write();