From c83070058f3511aa1863b36b82b5234edfe4e57a Mon Sep 17 00:00:00 2001 From: elrrrrrrr Date: Fri, 4 Aug 2023 11:05:54 +0800 Subject: [PATCH] fix: publisher info --- app/core/service/PackageManagerService.ts | 6 +++--- app/core/service/PackageSyncerService.ts | 10 +++++----- app/repository/PackageRepository.ts | 4 ++++ .../service/PackageSyncerService/executeTask.test.ts | 9 +++++++-- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/app/core/service/PackageManagerService.ts b/app/core/service/PackageManagerService.ts index 3cc88b301..e5e2606cc 100644 --- a/app/core/service/PackageManagerService.ts +++ b/app/core/service/PackageManagerService.ts @@ -53,7 +53,7 @@ export interface PublishPackageCmd { // name don't include scope name: string; version: string; - description: string; + description?: string; packageJson: PackageJSONType; registryId?: string; readme: string; @@ -107,14 +107,14 @@ export class PackageManagerService extends AbstractService { scope: cmd.scope, name: cmd.name, isPrivate: cmd.isPrivate, - description: cmd.description, + description: cmd.description || '', registryId: cmd.registryId, }); } else { // update description // will read database twice to update description by model to entity and entity to model if (pkg.description !== cmd.description) { - pkg.description = cmd.description; + pkg.description = cmd.description || ''; } /* c8 ignore next 3 */ diff --git a/app/core/service/PackageSyncerService.ts b/app/core/service/PackageSyncerService.ts index df909c44c..cd71251c3 100644 --- a/app/core/service/PackageSyncerService.ts +++ b/app/core/service/PackageSyncerService.ts @@ -18,7 +18,7 @@ import { downloadToTempfile } from '../../common/FileUtil'; import { TaskState, TaskType } from '../../common/enum/Task'; import { AbstractService } from '../../common/AbstractService'; import { TaskRepository } from '../../repository/TaskRepository'; -import { PackageRepository } from '../../repository/PackageRepository'; +import { PackageJSONType, PackageRepository } from '../../repository/PackageRepository'; import { PackageVersionDownloadRepository } from '../../repository/PackageVersionDownloadRepository'; import { UserRepository } from '../../repository/UserRepository'; import { Task, SyncPackageTaskOptions, CreateSyncPackageTask } from '../entity/Task'; @@ -560,7 +560,7 @@ export class PackageSyncerService extends AbstractService { logs.push(`[${isoNow()}] 📦 Add latest tag version "${fullname}: ${distTags.latest}"`); specificVersions.push(distTags.latest); } - const versions = specificVersions ? Object.values(versionMap).filter(verItem => specificVersions.includes(verItem.version)) : Object.values(versionMap); + const versions: PackageJSONType[] = specificVersions ? Object.values(versionMap).filter(verItem => specificVersions.includes(verItem.version)) : Object.values(versionMap); logs.push(`[${isoNow()}] 🚧 Syncing versions ${existsVersionCount} => ${versions.length}`); if (specificVersions) { const availableVersionList = versions.map(item => item.version); @@ -637,13 +637,13 @@ export class PackageSyncerService extends AbstractService { continue; } syncIndex++; - const description: string = item.description; + const description = item.description; // "dist": { // "shasum": "943e0ec03df00ebeb6273a5b94b916ba54b47581", // "tarball": "https://registry.npmjs.org/foo/-/foo-1.0.0.tgz" // }, const dist = item.dist; - const tarball: string = dist && dist.tarball; + const tarball = dist && dist.tarball; if (!tarball) { lastErrorMessage = `missing tarball, dist: ${JSON.stringify(dist)}`; logs.push(`[${isoNow()}] ❌ [${syncIndex}] Synced version ${version} fail, ${lastErrorMessage}`); @@ -690,7 +690,7 @@ export class PackageSyncerService extends AbstractService { }; try { // 当 version 记录已经存在时,还需要校验一下 pkg.manifests 是否存在 - const publisher = users.find(user => user.name === item._npmUser?.name) || users[0]; + const publisher = users.find(user => user.displayName === item._npmUser?.name) || users[0]; const pkgVersion = await this.packageManagerService.publish(publishCmd, publisher); updateVersions.push(pkgVersion.version); logs.push(`[${isoNow()}] 🟢 [${syncIndex}] Synced version ${version} success, packageVersionId: ${pkgVersion.packageVersionId}, db id: ${pkgVersion.id}`); diff --git a/app/repository/PackageRepository.ts b/app/repository/PackageRepository.ts index f0c322e33..5da0b350c 100644 --- a/app/repository/PackageRepository.ts +++ b/app/repository/PackageRepository.ts @@ -95,6 +95,10 @@ export type PackageJSONType = CnpmcorePatchInfo & { hasInstallScript?: boolean; dist?: DistType; workspace?: string[]; + _npmUser?: { + name: string; + email: string; + }; [key: string]: unknown; }; diff --git a/test/core/service/PackageSyncerService/executeTask.test.ts b/test/core/service/PackageSyncerService/executeTask.test.ts index 9b62358e2..98790679e 100644 --- a/test/core/service/PackageSyncerService/executeTask.test.ts +++ b/test/core/service/PackageSyncerService/executeTask.test.ts @@ -2419,8 +2419,11 @@ describe('test/core/service/PackageSyncerService/executeTask.test.ts', () => { }); it('should resync history version if forceSyncHistory is true', async () => { + const manifest = JSON.parse((await TestUtil.readFixturesFile('registry.npmjs.org/foobar.json')).toString()); + manifest.versions['1.0.0']._npmUser = { name: 'apple', email: 'apple@cnpmjs.org' }; + manifest.maintainers = [ ...manifest.maintainers, { name: 'apple', email: 'apple@cnpmjs.org' }]; app.mockHttpclient('https://registry.npmjs.org/foobar', 'GET', { - data: await TestUtil.readFixturesFile('registry.npmjs.org/foobar.json'), + data: manifest, persist: false, repeats: 1, }); @@ -2439,9 +2442,11 @@ describe('test/core/service/PackageSyncerService/executeTask.test.ts', () => { assert(task); await packageSyncerService.executeTask(task); + // should sync publisher + const syncInfo = await packageManagerService.listPackageFullManifests('', 'foobar'); + assert.equal(syncInfo.data?.versions['1.0.0']?._npmUser?.name, 'apple'); // resync - const manifest = JSON.parse((await TestUtil.readFixturesFile('registry.npmjs.org/foobar.json')).toString()); manifest.versions['1.0.0']._npmUser = { name: 'banana', email: 'banana@cnpmjs.org' }; app.mockHttpclient('https://registry.npmjs.org/foobar', 'GET', { data: manifest,