diff --git a/backend/src/services/external-migration/external-migration-fns.ts b/backend/src/services/external-migration/external-migration-fns.ts index b299d00c7..6d996022a 100644 --- a/backend/src/services/external-migration/external-migration-fns.ts +++ b/backend/src/services/external-migration/external-migration-fns.ts @@ -126,6 +126,7 @@ export const importDataIntoInfisicalFn = async ({ const originalToNewProjectId = new Map(); const originalToNewEnvironmentId = new Map(); + const projectsNotImported: string[] = []; await projectDAL.transaction(async (tx) => { for await (const project of data.projects) { @@ -143,16 +144,21 @@ export const importDataIntoInfisicalFn = async ({ logger.error(e, `Failed to import to project [name:${project.name}]`); throw new BadRequestError({ message: `Failed to import to project [name:${project.name}]` }); }); - originalToNewProjectId.set(project.id, newProject.id); } // Import environments if (data.environments) { for await (const environment of data.environments) { - const projectId = originalToNewProjectId.get(environment.projectId)!; + const projectId = originalToNewProjectId.get(environment.projectId); const slug = slugify(`${environment.name}-${alphaNumericNanoId(4)}`); + if (!projectId) { + projectsNotImported.push(environment.projectId); + // eslint-disable-next-line no-continue + continue; + } + const existingEnv = await projectEnvDAL.findOne({ projectId, slug }, tx); if (existingEnv) { @@ -180,6 +186,11 @@ export const importDataIntoInfisicalFn = async ({ >(); for (const secret of data.secrets) { + if (!originalToNewEnvironmentId.get(secret.environmentId)) { + // eslint-disable-next-line no-continue + continue; + } + if (!mappedToEnvironmentId.has(secret.environmentId)) { mappedToEnvironmentId.set(secret.environmentId, []); } @@ -254,4 +265,6 @@ export const importDataIntoInfisicalFn = async ({ } } }); + + return { projectsNotImported }; }; diff --git a/backend/src/services/external-migration/external-migration-queue.ts b/backend/src/services/external-migration/external-migration-queue.ts index 0ee51c5fa..76c7ceb1e 100644 --- a/backend/src/services/external-migration/external-migration-queue.ts +++ b/backend/src/services/external-migration/external-migration-queue.ts @@ -97,7 +97,7 @@ export const externalMigrationQueueFactory = ({ const decryptedJson = JSON.parse(decrypted) as TImportInfisicalDataCreate; - await importDataIntoInfisicalFn({ + const { projectsNotImported } = await importDataIntoInfisicalFn({ input: decryptedJson, projectDAL, projectEnvDAL, @@ -112,6 +112,17 @@ export const externalMigrationQueueFactory = ({ secretV2BridgeService }); + if (projectsNotImported.length) { + logger.info( + { + actorEmail, + actorOrgId: decryptedJson.actorOrgId, + projectsNotImported + }, + "One or more projects were not imported during import from external source" + ); + } + await smtpService.sendMail({ recipients: [actorEmail], subjectLine: "Infisical import successful",