From 6a4953e3471ae4916b85d294219399b8060f8242 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B9=9B=E5=85=AE?= Date: Fri, 12 Jun 2026 01:47:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=A1=A5=E9=BD=90=20Gitea=20ref=20?= =?UTF-8?q?=E6=8F=90=E4=BA=A4=E6=91=98=E8=A6=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/projects/project.types.ts | 12 +++++++++ src/projects/projects.service.spec.ts | 39 ++++++++++++++++++++++++--- src/projects/projects.service.ts | 38 ++++++++++++++++++++++++-- 3 files changed, 84 insertions(+), 5 deletions(-) diff --git a/src/projects/project.types.ts b/src/projects/project.types.ts index 54bc857..760a26b 100644 --- a/src/projects/project.types.ts +++ b/src/projects/project.types.ts @@ -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; + }; }>; }; diff --git a/src/projects/projects.service.spec.ts b/src/projects/projects.service.spec.ts index 3123edf..964530e 100644 --- a/src/projects/projects.service.spec.ts +++ b/src/projects/projects.service.spec.ts @@ -20,6 +20,7 @@ describe('ProjectsService', () => { giteaConfigured?: boolean; branches?: Array<{ name: string; commitSha: string }>; tags?: Array<{ name: string; commitSha: string }>; + commits?: Record; }) { 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; + 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 () => { diff --git a/src/projects/projects.service.ts b/src/projects/projects.service.ts index bd14f44..7f5bcdf 100644 --- a/src/projects/projects.service.ts +++ b/src/projects/projects.service.ts @@ -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( + repo: { owner: string; name: string }, + refs: T[], + ): Promise> { + 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 {