feat: 初始化DevOps平台后端

This commit is contained in:
湛兮
2026-06-11 20:49:59 +08:00
commit caec5a618d
88 changed files with 11893 additions and 0 deletions
+19
View File
@@ -0,0 +1,19 @@
import { Controller, Get } from '@nestjs/common';
import { ApiOkResponse, ApiTags } from '@nestjs/swagger';
import { SettingsService } from './settings.service';
import { IntegrationConfigStatusResponse } from './settings.types';
@ApiTags('settings')
@Controller('settings')
export class SettingsController {
constructor(private readonly settingsService: SettingsService) {}
@Get('integration-config')
@ApiOkResponse({
description:
'Integration configuration status with secret values redacted server-side.',
})
getIntegrationConfig(): IntegrationConfigStatusResponse {
return this.settingsService.getIntegrationConfigStatus();
}
}
+9
View File
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { SettingsController } from './settings.controller';
import { SettingsService } from './settings.service';
@Module({
controllers: [SettingsController],
providers: [SettingsService],
})
export class SettingsModule {}
+165
View File
@@ -0,0 +1,165 @@
import { ConfigService } from '@nestjs/config';
import { EnvConfig } from '../config/env.schema';
import { SettingsService } from './settings.service';
describe('SettingsService', () => {
it('returns configured when every required integration variable is real', () => {
const secretValues = [
'jenkins-real-token',
'gitea-real-token',
'gitea-real-webhook-secret',
'wecom-real-key',
'llm-real-api-key',
'0123456789abcdef0123456789abcdef',
];
const service = new SettingsService(
createConfig({
DATABASE_URL: 'mysql://devops_user:local-password@mysql.internal:3306/devops',
REDIS_URL: 'redis://redis.internal:6379/0',
JENKINS_BASE_URL: 'https://jenkins.internal',
JENKINS_USERNAME: 'devops-bot',
JENKINS_API_TOKEN: secretValues[0],
GITEA_BASE_URL: 'https://gitea.internal',
GITEA_TOKEN: secretValues[1],
GITEA_WEBHOOK_SECRET: secretValues[2],
NOTIFICATION_PROVIDER: 'wecom',
WECOM_WEBHOOK_URL: `https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=${secretValues[3]}`,
LLM_BASE_URL: 'https://llm.internal/v1',
LLM_API_KEY: secretValues[4],
LLM_MODEL: 'gpt-enterprise',
SECRET_ENCRYPTION_KEY: secretValues[5],
}),
);
const result = service.getIntegrationConfigStatus();
expect(Date.parse(result.checkedAt)).not.toBeNaN();
expect(result.integrations).toHaveLength(7);
expect(result.integrations.map((integration) => integration.status)).toEqual(
Array(7).fill('configured'),
);
});
it('marks partially configured integrations as partial with missing variable names only', () => {
const service = new SettingsService(
createConfig({
LLM_BASE_URL: 'https://llm.internal/v1',
LLM_MODEL: 'gpt-enterprise',
}),
);
const llm = service
.getIntegrationConfigStatus()
.integrations.find((integration) => integration.key === 'llm');
expect(llm).toMatchObject({
status: 'partial',
configured: ['LLM_BASE_URL', 'LLM_MODEL'],
missing: ['LLM_API_KEY'],
});
});
it('treats env example placeholders as missing values', () => {
const service = new SettingsService(
createConfig({
JENKINS_BASE_URL: 'https://jenkins.example.internal',
JENKINS_USERNAME: 'devops-bot',
JENKINS_API_TOKEN: 'replace-with-secret-manager-value',
GITEA_BASE_URL: 'https://gitea.example.internal',
GITEA_TOKEN: 'replace-with-secret-manager-value',
GITEA_WEBHOOK_SECRET: 'replace-with-secret-manager-value',
NOTIFICATION_PROVIDER: 'replace-with-provider',
WECOM_WEBHOOK_URL:
'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=replace-with-secret-manager-value',
LLM_BASE_URL: 'https://llm.example.internal/v1',
LLM_API_KEY: 'replace-with-secret-manager-value',
LLM_MODEL: 'replace-with-model-name',
SECRET_ENCRYPTION_KEY: 'replace-with-32-byte-key',
}),
);
const result = service.getIntegrationConfigStatus();
const byKey = Object.fromEntries(
result.integrations.map((integration) => [integration.key, integration]),
);
expect(byKey.jenkins).toMatchObject({
status: 'partial',
configured: ['JENKINS_USERNAME'],
missing: ['JENKINS_BASE_URL', 'JENKINS_API_TOKEN'],
});
expect(byKey.gitea).toMatchObject({
status: 'missing',
missing: ['GITEA_BASE_URL', 'GITEA_TOKEN'],
});
expect(byKey.notification).toMatchObject({
status: 'missing',
missing: ['NOTIFICATION_PROVIDER'],
});
expect(byKey.llm).toMatchObject({
status: 'missing',
missing: ['LLM_BASE_URL', 'LLM_API_KEY', 'LLM_MODEL'],
});
expect(byKey.secrets).toMatchObject({
status: 'missing',
missing: ['SECRET_ENCRYPTION_KEY'],
});
});
it('does not serialize secret, token, base URL, or webhook values', () => {
const hiddenValues = [
'https://jenkins.internal',
'jenkins-real-token',
'https://gitea.internal',
'gitea-real-token',
'gitea-real-webhook-secret',
'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=wecom-real-key',
'https://open.feishu.cn/open-apis/bot/v2/hook/feishu-real-key',
'https://notification.internal/webhook/generic-real-key',
'https://llm.internal/v1',
'llm-real-api-key',
'mysql://devops_user:local-password@mysql.internal:3306/devops',
'redis://redis.internal:6379/0',
'0123456789abcdef0123456789abcdef',
];
const service = new SettingsService(
createConfig({
DATABASE_URL: hiddenValues[10],
REDIS_URL: hiddenValues[11],
JENKINS_BASE_URL: hiddenValues[0],
JENKINS_USERNAME: 'devops-bot',
JENKINS_API_TOKEN: hiddenValues[1],
GITEA_BASE_URL: hiddenValues[2],
GITEA_TOKEN: hiddenValues[3],
GITEA_WEBHOOK_SECRET: hiddenValues[4],
WECOM_WEBHOOK_URL: hiddenValues[5],
FEISHU_WEBHOOK_URL: hiddenValues[6],
NOTIFICATION_WEBHOOK_URL: hiddenValues[7],
NOTIFICATION_PROVIDER: 'wecom',
LLM_BASE_URL: hiddenValues[8],
LLM_API_KEY: hiddenValues[9],
LLM_MODEL: 'gpt-enterprise',
SECRET_ENCRYPTION_KEY: hiddenValues[12],
}),
);
const serialized = JSON.stringify(service.getIntegrationConfigStatus());
hiddenValues.forEach((value) => {
expect(serialized).not.toContain(value);
});
expect(serialized).toContain('JENKINS_API_TOKEN');
expect(serialized).toContain('NOTIFICATION_PROVIDER');
expect(serialized).toContain('WECOM_WEBHOOK_URL');
expect(serialized).toContain('FEISHU_WEBHOOK_URL');
expect(serialized).toContain('SECRET_ENCRYPTION_KEY');
});
});
function createConfig(
values: Partial<Record<keyof EnvConfig, string | number | boolean | undefined>>,
): ConfigService<EnvConfig, true> {
return {
get: jest.fn((key: keyof EnvConfig) => values[key]),
} as unknown as ConfigService<EnvConfig, true>;
}
+133
View File
@@ -0,0 +1,133 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { EnvConfig } from '../config/env.schema';
import {
IntegrationConfigKey,
IntegrationConfigState,
IntegrationConfigStatus,
IntegrationConfigStatusResponse,
} from './settings.types';
type EnvStringKey = {
[Key in keyof EnvConfig]-?: Extract<EnvConfig[Key], string> extends never
? never
: Key;
}[keyof EnvConfig];
type EnvKey = keyof EnvConfig;
type IntegrationDefinition = {
key: IntegrationConfigKey;
name: string;
required: EnvStringKey[];
optional?: EnvKey[];
note?: string;
};
const INTEGRATION_DEFINITIONS: IntegrationDefinition[] = [
{
key: 'jenkins',
name: 'Jenkins',
required: ['JENKINS_BASE_URL', 'JENKINS_USERNAME', 'JENKINS_API_TOKEN'],
},
{
key: 'gitea',
name: 'Gitea',
required: ['GITEA_BASE_URL', 'GITEA_TOKEN'],
optional: ['GITEA_WEBHOOK_SECRET'],
note: 'GITEA_WEBHOOK_SECRET 用于开启 webhook 签名校验。',
},
{
key: 'notification',
name: '通知平台',
required: ['NOTIFICATION_PROVIDER'],
optional: ['WECOM_WEBHOOK_URL', 'FEISHU_WEBHOOK_URL', 'NOTIFICATION_WEBHOOK_URL'],
note: '当前不强制配置机器人;后续可按 provider 接入企微、飞书或通用 webhook。',
},
{
key: 'llm',
name: 'LLM Agent',
required: ['LLM_BASE_URL', 'LLM_API_KEY', 'LLM_MODEL'],
},
{
key: 'database',
name: 'MySQL / Prisma',
required: ['DATABASE_URL'],
optional: ['USE_DATABASE_READS', 'PRISMA_CONNECT_ON_BOOT'],
},
{
key: 'redis',
name: 'Redis / BullMQ',
required: ['REDIS_URL'],
},
{
key: 'secrets',
name: '服务端密钥加密',
required: ['SECRET_ENCRYPTION_KEY'],
},
];
const PLACEHOLDER_PATTERNS = [
/replace-with-/i,
/\.example\.internal(?:[/:]|$)/i,
];
@Injectable()
export class SettingsService {
constructor(private readonly config: ConfigService<EnvConfig, true>) {}
getIntegrationConfigStatus(): IntegrationConfigStatusResponse {
return {
checkedAt: new Date().toISOString(),
integrations: INTEGRATION_DEFINITIONS.map((definition) =>
this.toIntegrationStatus(definition),
),
};
}
private toIntegrationStatus(
definition: IntegrationDefinition,
): IntegrationConfigStatus {
const configured = definition.required.filter((key) =>
this.isConfiguredValue(this.getConfigValue(key)),
);
const missing = definition.required.filter(
(key) => !this.isConfiguredValue(this.getConfigValue(key)),
);
return {
key: definition.key,
name: definition.name,
status: this.resolveStatus(configured.length, missing.length),
required: [...definition.required],
configured,
missing,
...(definition.optional ? { optional: [...definition.optional] } : {}),
...(definition.note ? { note: definition.note } : {}),
};
}
private getConfigValue(key: EnvStringKey): string | undefined {
const value = this.config.get(key, { infer: true });
return typeof value === 'string' ? value.trim() : undefined;
}
private isConfiguredValue(value: string | undefined): boolean {
if (!value) {
return false;
}
return !PLACEHOLDER_PATTERNS.some((pattern) => pattern.test(value));
}
private resolveStatus(
configuredCount: number,
missingCount: number,
): IntegrationConfigState {
if (missingCount === 0) {
return 'configured';
}
return configuredCount > 0 ? 'partial' : 'missing';
}
}
+27
View File
@@ -0,0 +1,27 @@
export type IntegrationConfigKey =
| 'jenkins'
| 'gitea'
| 'notification'
| 'wecom'
| 'llm'
| 'database'
| 'redis'
| 'secrets';
export type IntegrationConfigState = 'configured' | 'partial' | 'missing';
export type IntegrationConfigStatus = {
key: IntegrationConfigKey;
name: string;
status: IntegrationConfigState;
required: string[];
configured: string[];
missing: string[];
optional?: string[];
note?: string;
};
export type IntegrationConfigStatusResponse = {
checkedAt: string;
integrations: IntegrationConfigStatus[];
};