Skip to content

Commit 82a5c60

Browse files
authored
Merge branch 'main' into test-ai-review
2 parents 8d8a544 + e334888 commit 82a5c60

File tree

1 file changed

+105
-12
lines changed

1 file changed

+105
-12
lines changed
Lines changed: 105 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,112 @@
1-
name: AI Code Review
1+
name: Codacy Review & Auto Merge with Email
22

3-
# 触发条件:当有新的Pull Request创建时
4-
on: [pull_request]
3+
on:
4+
pull_request:
5+
types: [opened, reopened, synchronize]
6+
7+
permissions:
8+
contents: write
9+
pull-requests: write
510

611
jobs:
7-
# 定义一个名为 “review” 的任务
8-
review:
9-
# 任务在最新版的Ubuntu系统上运行
12+
codacy-analysis:
1013
runs-on: ubuntu-latest
11-
14+
outputs: # 定义此任务的输出,供其他任务使用
15+
result: ${{ steps.check_result.outputs.result }}
1216
steps:
13-
# 第一步:获取你的代码
14-
- name: Checkout repository code
17+
- name: Checkout repository
1518
uses: actions/checkout@v4
1619

17-
# 第二步:打印一条信息,证明工作流成功运行了
18-
- name: Say Hello (Placeholder for AI)
19-
run: echo "🎉 AI代码审查流程已触发!这里未来将集成真正的AI服务。"
20+
- name: Run Codacy Analysis
21+
id: codacy_analysis # 为步骤添加ID,便于引用其状态
22+
continue-on-error: true # 即使Codacy检查失败,也让工作流继续执行
23+
uses: codacy/codacy-analysis-cli-action@v4
24+
with:
25+
project-token: ${{ secrets.CODACY_PROJECT_TOKEN }}
26+
verbose: true
27+
28+
- name: Evaluate Codacy Result
29+
id: check_result
30+
run: |
31+
# 根据上一步Codacy分析的结果进行判断
32+
if [ "${{ steps.codacy_analysis.outcome }}" = "success" ]; then
33+
echo "Codacy分析通过"
34+
echo "result=pass" >> $GITHUB_OUTPUT
35+
else
36+
echo "Codacy分析未通过"
37+
echo "result=fail" >> $GITHUB_OUTPUT
38+
fi
39+
40+
auto-merge:
41+
runs-on: ubuntu-latest
42+
needs: [codacy-analysis]
43+
if: needs.codacy-analysis.outputs.result == 'pass'
44+
steps:
45+
- name: Auto Merge Pull Request
46+
uses: actions/github-script@v7
47+
with:
48+
script: |
49+
github.rest.pulls.merge({
50+
owner: context.repo.owner,
51+
repo: context.repo.repo,
52+
pull_number: context.issue.number,
53+
merge_method: 'merge'
54+
})
55+
console.log(`PR #${context.issue.number} 已自动合并。`)
56+
57+
notify-by-email: # 关键任务:发送邮件
58+
runs-on: ubuntu-latest
59+
needs: [codacy-analysis]
60+
if: needs.codacy-analysis.outputs.result == 'fail'
61+
steps:
62+
- name: Send Failure Email via SMTP
63+
uses: dawidd6/action-send-mail@v3
64+
with:
65+
# 邮件服务器配置 (以Gmail为例)
66+
server_address: smtp.gmail.com
67+
server_port: 465
68+
# !!!务必在GitHub Secrets中配置以下两项!!!
69+
username: ${{ secrets.MAIL_USERNAME }}
70+
password: ${{ secrets.MAIL_PASSWORD }}
71+
# 收件人:自动获取PR提交者的邮箱
72+
to: ${{ github.event.pull_request.user.email }}
73+
# 邮件主题
74+
subject: |
75+
[Codacy审查] PR #${{ github.event.pull_request.number }} 未通过自动代码检查
76+
# 邮件正文
77+
html: true
78+
body: |
79+
<p>你好,${{ github.event.pull_request.user.login }}:</p>
80+
<p>你在仓库 <strong>${{ github.repository }}</strong> 提交的 Pull Request <strong>#${{ github.event.pull_request.number }}</strong> 未能通过 Codacy 自动代码质量检查。</p>
81+
<p><strong>PR标题</strong>:${{ github.event.pull_request.title }}</p>
82+
<p><strong>问题详情</strong>:请登录 Codacy 或查看 GitHub PR 页面上的 Codacy 检查结果,了解具体的代码问题和建议。</p>
83+
<p><strong>PR链接</strong>:<a href="${{ github.event.pull_request.html_url }}">${{ github.event.pull_request.html_url }}</a></p>
84+
<p>请根据 Codacy 的报告修改代码,然后重新推送至当前分支。系统将在你更新后自动重新运行检查。</p>
85+
<hr/>
86+
<p><em>此邮件由 GitHub Actions 工作流自动发送,请勿回复。</em></p>
87+
# 发件人信息 (可选,但建议设置)
88+
from: GitHub Actions Bot <${{ secrets.MAIL_USERNAME }}>
89+
# 安全连接设置
90+
secure: true
91+
92+
cleanup:
93+
runs-on: ubuntu-latest
94+
needs: [codacy-analysis]
95+
if: always()
96+
steps:
97+
- name: Delete Source Branch
98+
if: github.event.pull_request.merged == true || github.event.action == 'closed'
99+
uses: actions/github-script@v7
100+
with:
101+
script: |
102+
const branchRef = `heads/${{ github.event.pull_request.head.ref }}`;
103+
try {
104+
await github.rest.git.deleteRef({
105+
owner: context.repo.owner,
106+
repo: context.repo.repo,
107+
ref: branchRef
108+
});
109+
console.log(`✅ 源分支 ${branchRef} 已删除。`);
110+
} catch (error) {
111+
console.log(`ℹ️ 提示:${error.message}`);
112+
}

0 commit comments

Comments
 (0)