fix(Step9): 優先用 Gitea 狀態阻擋合併
AI / 計算版本號 (pull_request) Successful in 2s
AI / Code Review (pull_request) Successful in 1m39s

This commit is contained in:
2026-06-22 10:32:58 +00:00
parent 75cbfd38b0
commit bd89eb573f
4 changed files with 55 additions and 4 deletions
+26 -1
View File
@@ -1,7 +1,7 @@
import { describe, it, afterEach, mock } from 'node:test';
import assert from 'node:assert/strict';
import axios from 'axios';
import { getPRDiff, filterDiff, postComment, postPullReviewComment, postPullReview, getCommitMessageBySha, getBranchHeadCommitMessage, shouldSkipBotCommit, getBotReviewOutcome } from './gitea.js';
import { getPRDiff, filterDiff, postComment, postPullReviewComment, postPullReview, getCommitMessageBySha, getBranchHeadCommitMessage, shouldSkipBotCommit, getBotReviewOutcome, blockPRMergeWithStatus } from './gitea.js';
afterEach(() => mock.restoreAll());
@@ -57,6 +57,31 @@ describe('gitea', () => {
await assert.rejects(() => postComment('test'), /api error/);
});
it('blockPRMergeWithStatus creates a failure commit status on the PR head sha', async () => {
let capturedUrl, capturedBody, capturedOpts;
mock.method(axios, 'post', async (url, body, opts) => {
capturedUrl = url;
capturedBody = body;
capturedOpts = opts;
return { data: { id: 12 } };
});
const result = await blockPRMergeWithStatus({ sha: 'abc/123', criticalCount: 3 });
assert.deepEqual(result, { id: 12 });
assert.ok(capturedUrl.includes('/api/v1/repos/'));
assert.ok(capturedUrl.endsWith('/statuses/abc%2F123'));
assert.equal(capturedBody.state, 'failure');
assert.equal(capturedBody.context, 'ai-code-review/critical');
assert.ok(capturedBody.description.includes('3 個嚴重問題'));
assert.ok(capturedBody.target_url.includes('/pulls/'));
assert.ok(capturedOpts.headers['Authorization'].startsWith('token '));
});
it('blockPRMergeWithStatus rejects when commit sha is missing', async () => {
await assert.rejects(() => blockPRMergeWithStatus({ sha: '', criticalCount: 1 }), /缺少 PR head commit sha/);
});
it('postPullReviewComment posts an inline review comment to the pulls reviews API', async () => {
let capturedUrl, capturedBody, capturedOpts;
mock.method(axios, 'post', async (url, body, opts) => {