fix: 完善 Jenkins 构建追踪

This commit is contained in:
湛兮
2026-06-12 12:26:28 +08:00
parent c2afe6e693
commit 345fa67f3a
8 changed files with 1255 additions and 168 deletions
@@ -1,4 +1,5 @@
import { AuditService } from '../audit/audit.service';
import { IntegrationRequestError } from '../common/errors/integration-error';
import { JenkinsClient } from '../integrations/jenkins/jenkins.client';
import { DeployNotificationService } from '../notifications/wecom/deploy-notification.service';
import { ProjectSummary } from '../projects/project.types';
@@ -45,6 +46,10 @@ describe('DeployExecutionService', () => {
notificationStatus: 'sent' | 'skipped';
queueId?: string;
queueBuildNumber?: number;
queueSearchBuildNumber?: number;
jobBuildable?: boolean;
jobInfoError?: Error;
matchedBuildNumber?: number;
buildResult?: 'SUCCESS' | 'FAILURE' | 'ABORTED' | 'UNSTABLE';
buildRunning?: boolean;
logText?: string;
@@ -72,6 +77,19 @@ describe('DeployExecutionService', () => {
queueId: options.queueId ?? 'queue_001',
}),
),
getJobInfo: jest.fn(() => {
if (options.jobInfoError) {
return Promise.reject(options.jobInfoError);
}
return Promise.resolve({
jobPath: 'my-project-dev/test-access-manage-develop',
name: 'test-access-manage-develop',
fullName: 'my-project-dev/test-access-manage-develop',
url: 'https://jenkins.example/job/my-project-dev/job/test-access-manage-develop/',
buildable: options.jobBuildable ?? true,
});
}),
getQueueItem: jest.fn((queueId: string) =>
Promise.resolve({
queueId,
@@ -81,6 +99,35 @@ describe('DeployExecutionService', () => {
: undefined,
}),
),
findBuildByQueueId: jest.fn((_jobPath: string, queueId: string) =>
Promise.resolve(
options.queueSearchBuildNumber
? {
jobPath: 'my-project-dev/test-access-manage-develop',
buildNumber: options.queueSearchBuildNumber,
building: options.buildRunning ?? false,
result: options.buildResult,
url: `https://jenkins.example/job/${options.queueSearchBuildNumber}/`,
queueId,
}
: undefined,
),
),
findLatestBuildByParameter: jest.fn((_jobPath: string, name: string) =>
Promise.resolve(
options.matchedBuildNumber
? {
jobPath: 'my-project-dev/test-access-manage-develop',
buildNumber: options.matchedBuildNumber,
building: options.buildRunning ?? false,
result: options.buildResult,
url: `https://jenkins.example/job/${options.matchedBuildNumber}/`,
matchedParameterName: name,
matchedParameterValue: 'run_001',
}
: undefined,
),
),
getBuildStatus: jest.fn((_jobPath: string, buildNumber: number) =>
Promise.resolve({
jobPath: 'my-project-dev/test-access-manage-develop',
@@ -101,7 +148,10 @@ describe('DeployExecutionService', () => {
JenkinsClient,
| 'healthSummary'
| 'buildWithParameters'
| 'getJobInfo'
| 'getQueueItem'
| 'findBuildByQueueId'
| 'findLatestBuildByParameter'
| 'getBuildStatus'
| 'getProgressiveText'
>;
@@ -137,7 +187,7 @@ describe('DeployExecutionService', () => {
return run.steps.find((item) => item.bpmnNodeId === bpmnNodeId);
}
it('completes a dry-run release when Jenkins is not configured', async () => {
it('fails a release when Jenkins is not configured instead of dry-running success', async () => {
const { audit, jenkins, repository, service } = createService({
jenkinsHealth: {
status: 'not_configured',
@@ -149,20 +199,41 @@ describe('DeployExecutionService', () => {
const result = await service.execute(run, project);
expect(result.status).toBe('success');
expect(result.status).toBe('failed');
expect(result.jenkinsQueueId).toBeUndefined();
expect(result.failureSummary).toContain('Jenkins 集成尚未配置');
expect(step(result, 'gitea-check')?.status).toBe('success');
expect(step(result, 'jenkins-build')?.status).toBe('success');
expect(step(result, 'health-check')?.status).toBe('success');
expect(step(result, 'jenkins-build')?.status).toBe('failed');
expect(step(result, 'jenkins-build')?.errorSummary).toContain(
'Jenkins 集成尚未配置',
);
expect(step(result, 'health-check')?.status).toBe('pending');
expect(step(result, 'notify-wecom')?.status).toBe('skipped');
expect(step(result, 'finish-release')?.status).toBe('success');
expect(step(result, 'finish-release')?.status).toBe('pending');
expect(jenkins.buildWithParameters).not.toHaveBeenCalled();
expect(audit.record).toHaveBeenCalledWith(
expect.objectContaining({ action: 'JENKINS_DRY_RUN_COMPLETED' }),
expect.objectContaining({ action: 'DEPLOY_FAILED' }),
);
expect(audit.record).toHaveBeenCalledWith(
expect.objectContaining({ action: 'DEPLOY_SUCCEEDED' }),
});
it('fails before triggering when the configured Jenkins job is missing', async () => {
const { jenkins, repository, service } = createService({
jenkinsHealth: { status: 'ok' },
notificationStatus: 'skipped',
jobInfoError: new IntegrationRequestError('jenkins', 'Jenkins 请求失败', {
status: 404,
}),
});
const run = await createRun(repository, 'run_missing_job_001');
const result = await service.execute(run, project);
expect(result.status).toBe('failed');
expect(result.failureSummary).toBe(
'Jenkins job 不存在:my-project-dev/test-access-manage-develop',
);
expect(step(result, 'jenkins-build')?.status).toBe('failed');
expect(jenkins.buildWithParameters).not.toHaveBeenCalled();
});
it('triggers Jenkins and keeps the run queued until status polling completes', async () => {
@@ -230,6 +301,58 @@ describe('DeployExecutionService', () => {
);
});
it('links a Jenkins build by queue id when the queue item has expired', async () => {
const { audit, jenkins, repository, service } = createService({
jenkinsHealth: { status: 'ok' },
notificationStatus: 'sent',
queueId: 'queue_431',
queueSearchBuildNumber: 19,
buildResult: 'SUCCESS',
});
const run = await service.execute(
await createRun(repository, 'run_queue_search_001'),
project,
);
const result = await service.syncJenkinsRun(run, project);
expect(result.status).toBe('success');
expect(result.jenkinsBuildNumber).toBe(19);
expect(jenkins.findBuildByQueueId).toHaveBeenCalledWith(
'my-project-dev/test-access-manage-develop',
'queue_431',
);
expect(audit.record).toHaveBeenCalledWith(
expect.objectContaining({ action: 'JENKINS_BUILD_LINKED' }),
);
});
it('links an existing Jenkins build by DEPLOY_RUN_ID when the run has no build number', async () => {
const { audit, jenkins, repository, service } = createService({
jenkinsHealth: { status: 'ok' },
notificationStatus: 'sent',
matchedBuildNumber: 21,
buildResult: 'SUCCESS',
});
const run = await createRun(repository, 'run_missing_build_001');
const result = await service.syncJenkinsRun(run, project);
expect(result.status).toBe('success');
expect(result.jenkinsBuildNumber).toBe(21);
expect(jenkins.findLatestBuildByParameter).toHaveBeenCalledWith(
'my-project-dev/test-access-manage-develop',
'DEPLOY_RUN_ID',
'run_missing_build_001',
);
expect(audit.record).toHaveBeenCalledWith(
expect.objectContaining({ action: 'JENKINS_BUILD_LINKED' }),
);
expect(audit.record).toHaveBeenCalledWith(
expect.objectContaining({ action: 'DEPLOY_SUCCEEDED' }),
);
});
it('syncs a queued Jenkins run to failure with a log excerpt', async () => {
const { audit, repository, service } = createService({
jenkinsHealth: { status: 'ok' },