From 60f3a9beba22debf9b8fe9a9f307827e3b94965f Mon Sep 17 00:00:00 2001 From: Jeffery Date: Fri, 15 May 2026 14:00:59 +0000 Subject: [PATCH] fix: skip ai review bot commits --- .gitea/workflows/review.yaml | 32 ++++++++++++++++++++++++++++++-- README.md | 8 +++++--- app/git.js | 3 ++- app/git.test.js | 11 ++++++++++- 4 files changed, 47 insertions(+), 7 deletions(-) diff --git a/.gitea/workflows/review.yaml b/.gitea/workflows/review.yaml index a7bee69..73e5f6e 100644 --- a/.gitea/workflows/review.yaml +++ b/.gitea/workflows/review.yaml @@ -5,9 +5,36 @@ on: - master types: [opened, synchronize] jobs: + detect-bot-commit: + name: 偵測自動提交 + runs-on: ubuntu + outputs: + skip: ${{ steps.detect.outputs.skip }} + steps: + - name: 檢查 head commit marker + id: detect + env: + GITEA_API_URL: ${{ github.api_url }} + GITEA_REPOSITORY: ${{ github.repository }} + GITEA_SHA: ${{ github.sha }} + GITEA_TOKEN: ${{ github.token }} + run: | + set -e + commit_json="$(curl -fsSL -H "Authorization: token ${GITEA_TOKEN}" "${GITEA_API_URL}/repos/${GITEA_REPOSITORY}/git/commits/${GITEA_SHA}")" || { + echo "skip=false" >> "$GITHUB_OUTPUT" + exit 0 + } + if printf '%s' "$commit_json" | grep -q '\[ai-review-bot\]'; then + echo "skip=true" >> "$GITHUB_OUTPUT" + echo "偵測到 AI Review Bot commit,跳過 review workflow" + else + echo "skip=false" >> "$GITHUB_OUTPUT" + fi version: name: 計算版本號 runs-on: ubuntu + needs: [detect-bot-commit] + if: needs.detect-bot-commit.outputs.skip != 'true' outputs: version: ${{ steps.version.outputs.version }} steps: @@ -25,7 +52,8 @@ jobs: code-review: name: Code Review runs-on: ubuntu - needs: [version] + needs: [detect-bot-commit, version] + if: needs.detect-bot-commit.outputs.skip != 'true' steps: - name: AI Code Review uses: https://gitea.jsc.idv.tw/actions/code-review@v${{ needs.version.outputs.version }} @@ -37,4 +65,4 @@ jobs: permissions: contents: write pull-requests: write - issues: write \ No newline at end of file + issues: write diff --git a/README.md b/README.md index 83ff825..92b5dd9 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ 這是一個 AI Code Review Action。Gitea Workflow 可以使用此 Action 讓 AI 助理根據不同面向分析 Push Request 中變更的內容後,將問題分級 Commnet 到 Push Request 中。 -# 流程(新 Push Request、新 Commit (排除 AI 助理的 Commit) 觸發) +# 流程(新 Push Request、新 Commit 觸發;若偵測到 AI 助理的自動提交則直接跳過) 1. 服務名稱、模型名稱、角色資訊(個性、符合個性的英文名稱、工作內容),Comment 到 Push Request 2. 每個角色個別分析 Git Diff 的內容產生新問題表格(問題等級、角色名稱、問題位置或行數、修改建議) @@ -11,8 +11,8 @@ 5. 從PR問題表格中取出所有舊問題,依照等級排序後 Comment 到 Push Request 6. 從PR問題表格中取出所有新問題,排除嚴重等級的問題後 Comment 到 Push Request 7. 從PR問題表格中取出所有新問題,將每個嚴重等級的問題 Comment 到 Push Request -8. Commit 問題檔案,將 workspace 中實際存在的同步檔覆蓋到記憶區;workspace 沒有的同步檔就略過,不會刪除記憶區既有內容 -9. 如果PR問題表格中有嚴重問題,則不要讓 workflow 執行成功(exit 1) +8. Commit 問題檔案,將 workspace 中實際存在的同步檔覆蓋到記憶區;workspace 沒有的同步檔就略過,不會刪除記憶區既有內容。自動提交的 commit message 會帶上 `[ai-review-bot]`,供 workflow 判斷是否要跳過重跑 +9. 如果 PR 問題表格中有嚴重問題,則不要讓 workflow 執行成功(exit 1) # 設計 @@ -33,6 +33,8 @@ 2. 在 `.gitea/workflows` 資料夾中建立 `ai-review.yaml' 3. 在 `ai-review.yaml` 中填入以下內容(選擇一個使用): +> **自動提交排除說明**:此 Action 會將自己的 commit message 標記為 `[ai-review-bot]`。建議在 review workflow 的最前面先檢查 head commit 是否含有這個 marker,若有就直接成功結束,避免 bot commit 造成重複觸發。 + > **權限說明**:此 Action 需要 `contents: write`(寫入 findings.json)、`pull-requests: write`(發佈 PR comment)、`issues: write`(發佈 issue comment)三項權限,為正常運作所必要,無法縮減。 ### 1. OpenAI diff --git a/app/git.js b/app/git.js index e2da47b..a8c16e2 100644 --- a/app/git.js +++ b/app/git.js @@ -7,6 +7,7 @@ import { GITEA_SERVER_URL, GITEA_REPOSITORY, GITEA_TOKEN, PR_HEAD_BRANCH, FINDIN const ACTION_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); const GENERATED_SYNC_PATHS = [FINDINGS_PATH, '.gitea/ai-review/exclusions.json']; const remoteUrl = `${GITEA_SERVER_URL.replace(/\/$/, '')}/${GITEA_REPOSITORY}.git`; +export const BOT_COMMIT_MARKER = '[ai-review-bot]'; export const SYNC_PATHS = [ '.amazonq/rules/triage-findings.md', '.codex/skills/triage-findings/SKILL.md', @@ -124,7 +125,7 @@ export async function commitAndPush(workspace, repoDir, _spawnSync = spawnSync, return; } - const out = run(['commit', '-m', 'chore: update ai-review findings [skip ci]'], repoDir); + const out = run(['commit', '-m', `chore: update ai-review findings ${BOT_COMMIT_MARKER}`], repoDir); const commitHash = out.match(/\[.+ ([a-f0-9]+)\]/)?.[1] || 'unknown'; try { run(['push', remoteUrl, PR_HEAD_BRANCH], repoDir, credEnv); diff --git a/app/git.test.js b/app/git.test.js index 0786aea..bfb21b7 100644 --- a/app/git.test.js +++ b/app/git.test.js @@ -3,7 +3,7 @@ import assert from 'node:assert/strict'; import fs from 'fs'; import os from 'os'; import path from 'path'; -import { commitAndPush, cloneRepo, SYNC_PATHS } from './git.js'; +import { commitAndPush, cloneRepo, SYNC_PATHS, BOT_COMMIT_MARKER } from './git.js'; // --- helpers --- function makeTmpWorkspace() { @@ -60,6 +60,15 @@ describe('commitAndPush', () => { } }); + it('tags auto commits with the bot marker for workflow filtering', async () => { + const spawn = makeSpawn(); + await commitAndPush(workspace, path.join(workspace, 'repo'), spawn, sourceRoot); + + const commitCall = spawn.calls.find(c => c.args[0] === 'commit'); + assert.ok(commitCall, 'expected git commit to run'); + assert.ok(commitCall.args.some(arg => arg.includes(BOT_COMMIT_MARKER)), 'expected commit message to include bot marker'); + }); + it('uses GIT_ASKPASS env for network operations (fetch, push, clone)', async () => { const spawn = makeSpawn(); await commitAndPush(workspace, path.join(workspace, 'repo'), spawn, sourceRoot);