Skip to content
This repository was archived by the owner on Jan 28, 2026. It is now read-only.

Commit 0df8b84

Browse files
committed
feat: 当分支不存在时支持跳过并继续构建索引
1 parent 004dcc4 commit 0df8b84

6 files changed

Lines changed: 96 additions & 14 deletions

File tree

dist/core/actions/action-runner.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
import type { SimpleGit } from 'simple-git';
12
import type { ParsedActionInputs } from './input.js';
3+
export declare class BranchNotFoundError extends Error {
4+
readonly branch: string;
5+
constructor(branch: string);
6+
}
27
export interface ActionLogger {
38
info(message: string): void;
49
warn(message: string): void;
@@ -20,5 +25,9 @@ export interface GitHubActionResult {
2025
readonly pushed: boolean;
2126
readonly indexBranchExisted: boolean;
2227
}
28+
export declare function resolveBranchCommit(git: SimpleGit, branch: string, remote: string): Promise<{
29+
commit: string;
30+
existsOnRemote: boolean;
31+
}>;
2332
export declare function runGitHubAction(options: RunGitHubActionOptions): Promise<GitHubActionResult>;
2433
//# sourceMappingURL=action-runner.d.ts.map

dist/core/actions/action-runner.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/core/actions/action-runner.js

Lines changed: 33 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/core/actions/action-runner.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/actions/action-runner.ts

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ import type {
1313
} from '../../types/index.js';
1414
import type { ParsedActionInputs } from './input.js';
1515

16+
export class BranchNotFoundError extends Error {
17+
readonly branch: string;
18+
19+
constructor(branch: string) {
20+
super(`分支 ${branch} 在仓库中不存在,无法生成索引。`);
21+
this.name = 'BranchNotFoundError';
22+
this.branch = branch;
23+
}
24+
}
25+
1626
export interface ActionLogger {
1727
info(message: string): void;
1828
warn(message: string): void;
@@ -142,7 +152,7 @@ function findMatchingIndex(record: ManifestBranchRecord | undefined, artifact: B
142152
);
143153
}
144154

145-
async function resolveBranchCommit(
155+
export async function resolveBranchCommit(
146156
git: SimpleGit,
147157
branch: string,
148158
remote: string,
@@ -153,8 +163,12 @@ async function resolveBranchCommit(
153163
const commit = (await git.revparse([remoteRef])).trim();
154164
return { commit, existsOnRemote: true };
155165
} catch {
156-
const commit = (await git.revparse([branch])).trim();
157-
return { commit, existsOnRemote: false };
166+
try {
167+
const commit = (await git.revparse([branch])).trim();
168+
return { commit, existsOnRemote: false };
169+
} catch {
170+
throw new BranchNotFoundError(branch);
171+
}
158172
}
159173
}
160174

@@ -312,9 +326,19 @@ export async function runGitHubAction(options: RunGitHubActionOptions): Promise<
312326
await baseGit.fetch([inputs.remote, '--prune']).catch(() => undefined);
313327

314328
const artifacts: BranchIndexArtifact[] = [];
329+
const missingBranches: string[] = [];
315330
for (const branch of inputs.branches) {
316-
const artifact = await generateBranchArtifact(baseGit, inputs, branch, logger);
317-
artifacts.push(artifact);
331+
try {
332+
const artifact = await generateBranchArtifact(baseGit, inputs, branch, logger);
333+
artifacts.push(artifact);
334+
} catch (error) {
335+
if (error instanceof BranchNotFoundError) {
336+
logger.warn(`分支 ${branch} 不存在,跳过索引生成。`);
337+
missingBranches.push(branch);
338+
continue;
339+
}
340+
throw error;
341+
}
318342
}
319343

320344
const indexWorktree = await prepareIndexWorktree(baseGit, inputs, logger);
@@ -380,7 +404,10 @@ export async function runGitHubAction(options: RunGitHubActionOptions): Promise<
380404
}
381405

382406
const updatedBranches = prepared.filter((artifact) => artifact.shouldUpdate).map((artifact) => artifact.branch);
383-
const skippedBranches = prepared.filter((artifact) => !artifact.shouldUpdate).map((artifact) => artifact.branch);
407+
const skippedBranches = [
408+
...prepared.filter((artifact) => !artifact.shouldUpdate).map((artifact) => artifact.branch),
409+
...missingBranches,
410+
];
384411

385412
return {
386413
manifestPath,

tests/action-runner.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { describe, expect, it, vi } from 'vitest';
2+
import type { SimpleGit } from 'simple-git';
3+
4+
import { BranchNotFoundError, resolveBranchCommit } from '../src/core/actions/action-runner.js';
5+
6+
describe('resolveBranchCommit', () => {
7+
it('当远程与本地均不存在分支时抛出 BranchNotFoundError', async () => {
8+
const revparse = vi.fn()
9+
.mockRejectedValueOnce(new Error('missing remote'))
10+
.mockRejectedValueOnce(new Error('missing local'));
11+
12+
const git = { revparse } as unknown as SimpleGit;
13+
14+
await expect(resolveBranchCommit(git, 'feature/missing', 'origin')).rejects.toBeInstanceOf(BranchNotFoundError);
15+
expect(revparse).toHaveBeenCalledTimes(2);
16+
expect(revparse).toHaveBeenNthCalledWith(1, ['origin/feature/missing']);
17+
expect(revparse).toHaveBeenNthCalledWith(2, ['feature/missing']);
18+
});
19+
});

0 commit comments

Comments
 (0)