Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sync self pkg #532

Merged
merged 1 commit into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion app/core/service/PackageSyncerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { Registry } from '../entity/Registry';
import { BadRequestError } from 'egg-errors';
import { ScopeManagerService } from './ScopeManagerService';
import { EventCorkAdvice } from './EventCorkerAdvice';
import { SyncDeleteMode } from '../../common/constants';
import { PresetRegistryName, SyncDeleteMode } from '../../common/constants';

type syncDeletePkgOptions = {
task: Task,
Expand Down Expand Up @@ -372,6 +372,14 @@ export class PackageSyncerService extends AbstractService {
}
logs.push(`[${isoNow()}] 🚧 log: ${logUrl}`);

if (registry?.name === PresetRegistryName.self) {
logs.push(`[${isoNow()}] ❌❌❌❌❌ ${fullname} has been published to the self registry, skip sync ❌❌❌❌❌`);
await this.taskService.finishTask(task, TaskState.Fail, logs.join('\n'));
this.logger.info('[PackageSyncerService.executeTask:fail] taskId: %s, targetName: %s, invalid registryId',
task.taskId, task.targetName);
return;
}

if (pkg && pkg?.registryId !== registry?.registryId) {
if (pkg.registryId) {
logs.push(`[${isoNow()}] ❌❌❌❌❌ ${fullname} registry is ${pkg.registryId} not belong to ${registry?.registryId}, skip sync ❌❌❌❌❌`);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这段代码的变更是将 SyncDeleteMode 移动到 constants.ts 中并添加了 PresetRegistryName 常量。

对于代码中的修改,似乎没有风险或错误。添加PreserRegistryName 可以使代码更加清晰且易于维护。

在行号为 372 处,如果 registry?.name 是 PresetRegistryName.self 则不需要执行同步操作,会记录一条日志并退出该方法。这样做使代码结构更清晰,也可以避免意外的同步错误。

Expand Down
42 changes: 42 additions & 0 deletions test/core/service/PackageSyncerService/executeTask.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,48 @@ describe('test/core/service/PackageSyncerService/executeTask.test.ts', () => {

});

it('should skip self registry', async () => {
const name = '@cnpmcore/test-self-sync';
const { user } = await userService.create({
name: 'test-user',
password: 'this-is-password',
email: '[email protected]',
ip: '127.0.0.1',
});

const registry = await registryManagerService.ensureSelfRegistry();

const publishCmd = {
scope: '@cnpmcore',
name: 'test-self-sync',
version: '1.0.0',
description: '1.0.0',
readme: '',
registryId: registry.registryId,
packageJson: { name, test: 'test', version: '1.0.0' },
dist: {
content: Buffer.alloc(0),
},
isPrivate: false,
publishTime: new Date(),
skipRefreshPackageManifests: false,
};
const pkgVersion = await packageManagerService.publish(publishCmd, user);
assert(pkgVersion.version === '1.0.0');

await packageSyncerService.createTask(name);
const task = await packageSyncerService.findExecuteTask();
assert(task);
await packageSyncerService.executeTask(task);

const stream = await packageSyncerService.findTaskLog(task);
assert(stream);
const log = await TestUtil.readStreamToLog(stream);
// console.log(log);
assert(log.includes(`${name} has been published to the self registry, skip sync ❌❌❌❌❌`));

});

it('should updated package manifests when version insert duplicated', async () => {
// https://www.npmjs.com/package/@cnpmcore/test-sync-package-has-two-versions
const name = '@cnpmcore/test-sync-package-has-two-versions';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这段代码是针对 test/core/service/PackageSyncerService/executeTask.test.ts 中的一个测试用例。在添加一个新的测试用例,用于确保当同步任务是自注册表时,跳过同步。在测试之前,该用例创建了一个名为 @cnpmcore/test-self-sync 的包,并将它发布到自注册表中进行测试。测试执行后,通过读取任务日志来验证是否成功地跳过了同步任务,并输出消息“已将***发布到自注册表中,跳过同步”。建议对日志编写更加详细的测试用例,以方便调试和验证。

Expand Down