chore: 更新代码 #5
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: AI Review & Rebase Merge | |
| on: | |
| pull_request: | |
| types: [opened, reopened, synchronize] | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| jobs: | |
| analyze: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| analysis_result: ${{ steps.evaluate.outputs.result }} | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Run Codacy Analysis | |
| id: codacy_scan | |
| continue-on-error: true | |
| uses: codacy/codacy-analysis-cli-action@v4 | |
| with: | |
| project-token: ${{ secrets.CODACY_PROJECT_TOKEN }} | |
| verbose: true | |
| - name: Evaluate Result | |
| id: evaluate | |
| run: | | |
| if [ "${{ steps.codacy_scan.outcome }}" = "success" ]; then | |
| echo "Codacy检查通过" | |
| echo "result=success" >> $GITHUB_OUTPUT | |
| else | |
| echo "Codacy检查发现问题" | |
| echo "result=failure" >> $GITHUB_OUTPUT | |
| fi | |
| auto-merge: | |
| runs-on: ubuntu-latest | |
| needs: [analyze] | |
| # 仅在Codacy分析成功时,才尝试自动合并 | |
| if: needs.analyze.outputs.analysis_result == 'success' | |
| steps: | |
| - name: Auto Merge PR (using Rebase) | |
| id: merge_step | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| try { | |
| const response = await github.rest.pulls.merge({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| pull_number: context.issue.number, | |
| merge_method: 'rebase' # 使用变基合并,保持主线整洁 | |
| }); | |
| console.log('✅ PR 已通过变基合并。'); | |
| return response; | |
| } catch (error) { | |
| // 如果合并失败(如冲突),将错误信息打印到日志,并抛出错误使步骤失败 | |
| console.error('❌ 自动合并失败,原因:', error.message); | |
| throw error; | |
| } | |
| notify-and-handle-failure: | |
| runs-on: ubuntu-latest | |
| needs: [analyze, auto-merge] | |
| # 核心逻辑:如果分析失败 或 自动合并失败,则执行此任务发送邮件 | |
| if: needs.analyze.result == 'failure' || needs.auto-merge.result == 'failure' | |
| steps: | |
| - name: Send Failure Notification Email | |
| uses: dawidd6/action-send-mail@v3 | |
| with: | |
| server_address: smtp.gmail.com | |
| server_port: 465 | |
| username: ${{ secrets.MAIL_USERNAME }} | |
| password: ${{ secrets.MAIL_PASSWORD }} | |
| to: ${{ github.event.pull_request.user.email }} | |
| from: GitHub Actions Bot <${{ secrets.MAIL_USERNAME }}> | |
| subject: | | |
| [重要] PR #${{ github.event.pull_request.number }} 自动处理未完成 | |
| html: true | |
| body: | | |
| <p>你好 <strong>${{ github.event.pull_request.user.login }}</strong>,</p> | |
| <p>你的 Pull Request <a href="${{ github.event.pull_request.html_url }}"><strong>#${{ github.event.pull_request.number }}</strong></a> 在自动处理过程中遇到问题,需要你检查处理。</p> | |
| <p><strong>📝 详细信息</strong><br> | |
| 仓库:<code>${{ github.repository }}</code><br> | |
| PR标题:${{ github.event.pull_request.title }}</p> | |
| <p><strong>⚠️ 可能的原因及操作指引:</strong></p> | |
| <ul> | |
| <li><strong>Codacy 代码质量检查未通过</strong>:请查看 PR 页面上的 Codacy 检查报告,根据提示修改代码。</li> | |
| <li><strong>存在合并冲突</strong>:你的分支与主分支 (<code>main</code>) 存在冲突,需要你手动解决。</li> | |
| </ul> | |
| <p>请访问 <a href="${{ github.event.pull_request.html_url }}">PR 页面</a> 查看详细状态并处理。处理完成后,重新推送代码即可触发新一轮自动化检查。</p> | |
| <hr> | |
| <p><em>此邮件由 GitHub Actions 工作流自动发送,请勿直接回复。</em></p> | |
| secure: true | |
| cleanup: | |
| runs-on: ubuntu-latest | |
| needs: [analyze, auto-merge] | |
| if: always() | |
| steps: | |
| - name: Delete Source Branch (if merged) | |
| # 仅在PR被合并后才执行删除 | |
| if: github.event.pull_request.merged == true | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| try { | |
| await github.rest.git.deleteRef({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| ref: `heads/${{ github.event.pull_request.head.ref }}` | |
| }); | |
| console.log('✅ 源分支已删除。'); | |
| } catch (error) { | |
| console.log('ℹ️ 分支可能已被删除: ' + error.message); | |
| } |