feat: 补齐 Gitea ref 提交摘要

This commit is contained in:
湛兮
2026-06-12 01:47:17 +08:00
parent a21ec249b7
commit 6a4953e347
3 changed files with 84 additions and 5 deletions
+12
View File
@@ -21,9 +21,21 @@ export type ProjectRefSummary = {
branches: Array<{
name: string;
commitSha?: string;
commit?: {
sha: string;
message: string;
authorName?: string;
authoredAt?: string;
};
}>;
tags: Array<{
name: string;
commitSha?: string;
commit?: {
sha: string;
message: string;
authorName?: string;
authoredAt?: string;
};
}>;
};
+36 -3
View File
@@ -20,6 +20,7 @@ describe('ProjectsService', () => {
giteaConfigured?: boolean;
branches?: Array<{ name: string; commitSha: string }>;
tags?: Array<{ name: string; commitSha: string }>;
commits?: Record<string, { sha: string; message: string }>;
}) {
const repository = {
findMany: jest.fn(() => Promise.resolve([project])),
@@ -37,7 +38,18 @@ describe('ProjectsService', () => {
listTags: jest.fn(() =>
Promise.resolve(options?.tags ?? [{ name: 'v1.0.0', commitSha: 'def' }]),
),
} satisfies Pick<GiteaClient, 'healthSummary' | 'listBranches' | 'listTags'>;
getCommit: jest.fn((owner: string, repo: string, sha: string) =>
Promise.resolve(
options?.commits?.[sha] ?? {
sha,
message: `commit ${sha}`,
},
),
),
} satisfies Pick<
GiteaClient,
'healthSummary' | 'listBranches' | 'listTags' | 'getCommit'
>;
const auth = {
listAllowedProjectKeys: jest.fn(() => Promise.resolve([project.key])),
requireProjectPermission: jest.fn(() => Promise.resolve()),
@@ -74,16 +86,37 @@ describe('ProjectsService', () => {
giteaConfigured: true,
branches: [{ name: 'feature/devops-platform', commitSha: 'abc123' }],
tags: [{ name: 'v2026.06.11.1', commitSha: 'def456' }],
commits: {
abc123: { sha: 'abc123', message: 'feat: add platform refs' },
def456: { sha: 'def456', message: 'chore: tag release' },
},
});
const refs = await service.listProjectRefs(project.key);
expect(refs).toEqual({
source: 'gitea',
branches: [{ name: 'feature/devops-platform', commitSha: 'abc123' }],
tags: [{ name: 'v2026.06.11.1', commitSha: 'def456' }],
branches: [
{
name: 'feature/devops-platform',
commitSha: 'abc123',
commit: { sha: 'abc123', message: 'feat: add platform refs' },
},
],
tags: [
{
name: 'v2026.06.11.1',
commitSha: 'def456',
commit: { sha: 'def456', message: 'chore: tag release' },
},
],
});
expect(gitea.listBranches).toHaveBeenCalledWith('my-project', 'access-manage');
expect(gitea.getCommit).toHaveBeenCalledWith(
'my-project',
'access-manage',
'abc123',
);
});
it('raises a not found error for unknown projects', async () => {
+36 -2
View File
@@ -3,6 +3,7 @@ import { AuthService } from '../auth/auth.service';
import { AuthenticatedUser } from '../auth/auth.types';
import { AppError } from '../common/errors/app-error';
import { GiteaClient } from '../integrations/gitea/gitea.client';
import { GiteaCommitSummary } from '../integrations/gitea/gitea.types';
import { ProjectRepository } from './project.repository';
import { ProjectRefSummary, ProjectSummary } from './project.types';
@@ -60,11 +61,15 @@ export class ProjectsService {
this.giteaClient.listBranches(repo.owner, repo.name),
this.giteaClient.listTags(repo.owner, repo.name),
]);
const [branchesWithCommits, tagsWithCommits] = await Promise.all([
this.enrichRefsWithCommit(repo, branches),
this.enrichRefsWithCommit(repo, tags),
]);
return {
source: 'gitea',
branches,
tags,
branches: branchesWithCommits,
tags: tagsWithCommits,
};
} catch {
return this.fallbackRefs(project);
@@ -74,6 +79,35 @@ export class ProjectsService {
return this.fallbackRefs(project);
}
/**
* Commit 摘要只作为发布前辅助信息;单个摘要读取失败不能影响 refs 主流程。
*/
private async enrichRefsWithCommit<T extends { name: string; commitSha?: string }>(
repo: { owner: string; name: string },
refs: T[],
): Promise<Array<T & { commit?: GiteaCommitSummary }>> {
return Promise.all(
refs.map(async (ref) => {
if (!ref.commitSha) {
return ref;
}
try {
return {
...ref,
commit: await this.giteaClient.getCommit(
repo.owner,
repo.name,
ref.commitSha,
),
};
} catch {
return ref;
}
}),
);
}
private parseRepository(
project: ProjectSummary,
): { owner: string; name: string } | null {