feat: 补齐 Gitea ref 提交摘要
This commit is contained in:
@@ -21,9 +21,21 @@ export type ProjectRefSummary = {
|
|||||||
branches: Array<{
|
branches: Array<{
|
||||||
name: string;
|
name: string;
|
||||||
commitSha?: string;
|
commitSha?: string;
|
||||||
|
commit?: {
|
||||||
|
sha: string;
|
||||||
|
message: string;
|
||||||
|
authorName?: string;
|
||||||
|
authoredAt?: string;
|
||||||
|
};
|
||||||
}>;
|
}>;
|
||||||
tags: Array<{
|
tags: Array<{
|
||||||
name: string;
|
name: string;
|
||||||
commitSha?: string;
|
commitSha?: string;
|
||||||
|
commit?: {
|
||||||
|
sha: string;
|
||||||
|
message: string;
|
||||||
|
authorName?: string;
|
||||||
|
authoredAt?: string;
|
||||||
|
};
|
||||||
}>;
|
}>;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ describe('ProjectsService', () => {
|
|||||||
giteaConfigured?: boolean;
|
giteaConfigured?: boolean;
|
||||||
branches?: Array<{ name: string; commitSha: string }>;
|
branches?: Array<{ name: string; commitSha: string }>;
|
||||||
tags?: Array<{ name: string; commitSha: string }>;
|
tags?: Array<{ name: string; commitSha: string }>;
|
||||||
|
commits?: Record<string, { sha: string; message: string }>;
|
||||||
}) {
|
}) {
|
||||||
const repository = {
|
const repository = {
|
||||||
findMany: jest.fn(() => Promise.resolve([project])),
|
findMany: jest.fn(() => Promise.resolve([project])),
|
||||||
@@ -37,7 +38,18 @@ describe('ProjectsService', () => {
|
|||||||
listTags: jest.fn(() =>
|
listTags: jest.fn(() =>
|
||||||
Promise.resolve(options?.tags ?? [{ name: 'v1.0.0', commitSha: 'def' }]),
|
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 = {
|
const auth = {
|
||||||
listAllowedProjectKeys: jest.fn(() => Promise.resolve([project.key])),
|
listAllowedProjectKeys: jest.fn(() => Promise.resolve([project.key])),
|
||||||
requireProjectPermission: jest.fn(() => Promise.resolve()),
|
requireProjectPermission: jest.fn(() => Promise.resolve()),
|
||||||
@@ -74,16 +86,37 @@ describe('ProjectsService', () => {
|
|||||||
giteaConfigured: true,
|
giteaConfigured: true,
|
||||||
branches: [{ name: 'feature/devops-platform', commitSha: 'abc123' }],
|
branches: [{ name: 'feature/devops-platform', commitSha: 'abc123' }],
|
||||||
tags: [{ name: 'v2026.06.11.1', commitSha: 'def456' }],
|
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);
|
const refs = await service.listProjectRefs(project.key);
|
||||||
|
|
||||||
expect(refs).toEqual({
|
expect(refs).toEqual({
|
||||||
source: 'gitea',
|
source: 'gitea',
|
||||||
branches: [{ name: 'feature/devops-platform', commitSha: 'abc123' }],
|
branches: [
|
||||||
tags: [{ name: 'v2026.06.11.1', commitSha: 'def456' }],
|
{
|
||||||
|
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.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 () => {
|
it('raises a not found error for unknown projects', async () => {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { AuthService } from '../auth/auth.service';
|
|||||||
import { AuthenticatedUser } from '../auth/auth.types';
|
import { AuthenticatedUser } from '../auth/auth.types';
|
||||||
import { AppError } from '../common/errors/app-error';
|
import { AppError } from '../common/errors/app-error';
|
||||||
import { GiteaClient } from '../integrations/gitea/gitea.client';
|
import { GiteaClient } from '../integrations/gitea/gitea.client';
|
||||||
|
import { GiteaCommitSummary } from '../integrations/gitea/gitea.types';
|
||||||
import { ProjectRepository } from './project.repository';
|
import { ProjectRepository } from './project.repository';
|
||||||
import { ProjectRefSummary, ProjectSummary } from './project.types';
|
import { ProjectRefSummary, ProjectSummary } from './project.types';
|
||||||
|
|
||||||
@@ -60,11 +61,15 @@ export class ProjectsService {
|
|||||||
this.giteaClient.listBranches(repo.owner, repo.name),
|
this.giteaClient.listBranches(repo.owner, repo.name),
|
||||||
this.giteaClient.listTags(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 {
|
return {
|
||||||
source: 'gitea',
|
source: 'gitea',
|
||||||
branches,
|
branches: branchesWithCommits,
|
||||||
tags,
|
tags: tagsWithCommits,
|
||||||
};
|
};
|
||||||
} catch {
|
} catch {
|
||||||
return this.fallbackRefs(project);
|
return this.fallbackRefs(project);
|
||||||
@@ -74,6 +79,35 @@ export class ProjectsService {
|
|||||||
return this.fallbackRefs(project);
|
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(
|
private parseRepository(
|
||||||
project: ProjectSummary,
|
project: ProjectSummary,
|
||||||
): { owner: string; name: string } | null {
|
): { owner: string; name: string } | null {
|
||||||
|
|||||||
Reference in New Issue
Block a user