From 3fef6e4849781b8a1a7789b00b567ea2c6c020c7 Mon Sep 17 00:00:00 2001 From: Tuan Dang Date: Thu, 23 Feb 2023 21:38:11 +0700 Subject: [PATCH] Replace Promise.all with for-await Vercel getting envars --- backend/src/integrations/sync.ts | 98 ++++++++++++++++++++++---------- 1 file changed, 67 insertions(+), 31 deletions(-) diff --git a/backend/src/integrations/sync.ts b/backend/src/integrations/sync.ts index 760cd6497..718da1299 100644 --- a/backend/src/integrations/sync.ts +++ b/backend/src/integrations/sync.ts @@ -558,44 +558,80 @@ const syncSecretsVercel = async ({ : {}), }; - const res = ( - await Promise.all( - ( - await axios.get( - `${INTEGRATION_VERCEL_API_URL}/v9/projects/${integration.app}/env`, + // const res = ( + // await Promise.all( + // ( + // await axios.get( + // `${INTEGRATION_VERCEL_API_URL}/v9/projects/${integration.app}/env`, + // { + // params, + // headers: { + // Authorization: `Bearer ${accessToken}`, + // 'Accept-Encoding': 'application/json' + // } + // } + // )) + // .data + // .envs + // .filter((secret: VercelSecret) => secret.target.includes(integration.targetEnvironment)) + // .map(async (secret: VercelSecret) => { + // if (secret.type === 'encrypted') { + // // case: secret is encrypted -> need to decrypt + // const decryptedSecret = (await axios.get( + // `${INTEGRATION_VERCEL_API_URL}/v9/projects/${integration.app}/env/${secret.id}`, + // { + // params, + // headers: { + // Authorization: `Bearer ${accessToken}`, + // 'Accept-Encoding': 'application/json' + // } + // } + // )).data; + + // return decryptedSecret; + // } + + // return secret; + // }))).reduce((obj: any, secret: any) => ({ + // ...obj, + // [secret.key]: secret + // }), {}); + + const vercelSecrets: VercelSecret[] = (await axios.get( + `${INTEGRATION_VERCEL_API_URL}/v9/projects/${integration.app}/env`, + { + params, + headers: { + Authorization: `Bearer ${accessToken}`, + 'Accept-Encoding': 'application/json' + } + } + )) + .data + .envs + .filter((secret: VercelSecret) => secret.target.includes(integration.targetEnvironment)); + + const res: { [key: string]: VercelSecret } = {}; + + for await (const vercelSecret of vercelSecrets) { + if (vercelSecret.type === 'encrypted') { + // case: secret is encrypted -> need to decrypt + const decryptedSecret = (await axios.get( + `${INTEGRATION_VERCEL_API_URL}/v9/projects/${integration.app}/env/${vercelSecret.id}`, { params, headers: { Authorization: `Bearer ${accessToken}`, 'Accept-Encoding': 'application/json' } - } - )) - .data - .envs - .filter((secret: VercelSecret) => secret.target.includes(integration.targetEnvironment)) - .map(async (secret: VercelSecret) => { - if (secret.type === 'encrypted') { - // case: secret is encrypted -> need to decrypt - const decryptedSecret = (await axios.get( - `${INTEGRATION_VERCEL_API_URL}/v9/projects/${integration.app}/env/${secret.id}`, - { - params, - headers: { - Authorization: `Bearer ${accessToken}`, - 'Accept-Encoding': 'application/json' - } - } - )).data; + } + )).data; - return decryptedSecret; - } - - return secret; - }))).reduce((obj: any, secret: any) => ({ - ...obj, - [secret.key]: secret - }), {}); + res[vercelSecret.key] = decryptedSecret; + } else { + res[vercelSecret.key] = vercelSecret; + } + } const updateSecrets: VercelSecret[] = []; const deleteSecrets: VercelSecret[] = [];