From 279958d54cd66da4aa7f0503c1920acd89e846b2 Mon Sep 17 00:00:00 2001 From: Andrew Atimapre Date: Tue, 31 Oct 2023 07:18:59 +0100 Subject: [PATCH] Checkly group level sync support --- backend/src/integrations/sync.ts | 246 ++++++++++++------ .../src/pages/integrations/checkly/create.tsx | 14 +- 2 files changed, 178 insertions(+), 82 deletions(-) diff --git a/backend/src/integrations/sync.ts b/backend/src/integrations/sync.ts index 5f7d53345..f4a076320 100644 --- a/backend/src/integrations/sync.ts +++ b/backend/src/integrations/sync.ts @@ -2121,94 +2121,178 @@ const syncSecretsCheckly = async ({ accessToken: string; appendices?: { prefix: string; suffix: string }; }) => { - let getSecretsRes = ( - await standardRequest.get(`${INTEGRATION_CHECKLY_API_URL}/v1/variables`, { - headers: { - Authorization: `Bearer ${accessToken}`, - "Accept-Encoding": "application/json", - "X-Checkly-Account": integration.appId - } - }) - ).data.reduce( - (obj: any, secret: any) => ({ - ...obj, - [secret.key]: secret.value - }), - {} - ); - getSecretsRes = Object.keys(getSecretsRes).reduce( - ( - result: { - [key: string]: string; - }, - key - ) => { - if ( - (appendices?.prefix !== undefined ? key.startsWith(appendices?.prefix) : true) && - (appendices?.suffix !== undefined ? key.endsWith(appendices?.suffix) : true) - ) { - result[key] = getSecretsRes[key]; - } - return result; - }, - {} - ); - - // add secrets - for await (const key of Object.keys(secrets)) { - if (!(key in getSecretsRes)) { - // case: secret does not exist in checkly - // -> add secret - await standardRequest.post( - `${INTEGRATION_CHECKLY_API_URL}/v1/variables`, - { - key, - value: secrets[key].value - }, - { - headers: { - Authorization: `Bearer ${accessToken}`, - Accept: "application/json", - "Content-Type": "application/json", - "X-Checkly-Account": integration.appId - } - } - ); - } else { - // case: secret exists in checkly - // -> update/set secret - - if (secrets[key] !== getSecretsRes[key]) { - await standardRequest.put( - `${INTEGRATION_CHECKLY_API_URL}/v1/variables/${key}`, - { - value: secrets[key].value - }, - { - headers: { - Authorization: `Bearer ${accessToken}`, - "Content-Type": "application/json", - Accept: "application/json", - "X-Checkly-Account": integration.appId - } - } - ); - } - } - } - - for await (const key of Object.keys(getSecretsRes)) { - if (!(key in secrets)) { - // delete secret - await standardRequest.delete(`${INTEGRATION_CHECKLY_API_URL}/v1/variables/${key}`, { + if (integration.targetServiceId) { + let getGroupSecretsRes = ( + await standardRequest.get(`${INTEGRATION_CHECKLY_API_URL}/v1/check-groups/${integration.targetServiceId}`, { headers: { Authorization: `Bearer ${accessToken}`, Accept: "application/json", "X-Checkly-Account": integration.appId } - }); + }) + ).data.environmentVariables.reduce( + (obj: any, secret: any) => ({ + ...obj, + [secret.key]: secret.value + }), + {} + ); + + getGroupSecretsRes = Object.keys(getGroupSecretsRes).reduce( + ( + result: { + [key: string]: string; + }, + key + ) => { + if ( + (appendices?.prefix !== undefined ? key.startsWith(appendices?.prefix) : true) && + (appendices?.suffix !== undefined ? key.endsWith(appendices?.suffix) : true) + ) { + result[key] = getGroupSecretsRes[key]; + } + return result; + }, + {} + ); + + const groupEnvironmentVariables = Object.keys(secrets).map(key => ({ + key, + value: secrets[key].value + })); + + // add secrets + for await (const key of Object.keys(secrets)) { + + if (!(key in getGroupSecretsRes)) { + // case: secret does not exist in checkly group + // -> add secret + + await standardRequest.put( + `${INTEGRATION_CHECKLY_API_URL}/v1/check-groups/${integration.targetServiceId}`, + { + environmentVariables: groupEnvironmentVariables + }, + { + headers: { + Authorization: `Bearer ${accessToken}`, + Accept: "application/json", + "X-Checkly-Account": integration.appId + } + } + ); + } else { + // case: secret exists in checkly group + // -> update/set secret + + if (secrets[key] !== getGroupSecretsRes[key]) { + await standardRequest.put( + `${INTEGRATION_CHECKLY_API_URL}/v1/check-groups/${integration.targetServiceId}`, + { + environmentVariables: groupEnvironmentVariables + }, + { + headers: { + Authorization: `Bearer ${accessToken}`, + Accept: "application/json", + "X-Checkly-Account": integration.appId + } + } + ); + } + } } + } else { + let getSecretsRes = ( + await standardRequest.get(`${INTEGRATION_CHECKLY_API_URL}/v1/variables`, { + headers: { + Authorization: `Bearer ${accessToken}`, + "Accept-Encoding": "application/json", + "X-Checkly-Account": integration.appId + } + }) + ).data.reduce( + (obj: any, secret: any) => ({ + ...obj, + [secret.key]: secret.value + }), + {} + ); + + getSecretsRes = Object.keys(getSecretsRes).reduce( + ( + result: { + [key: string]: string; + }, + key + ) => { + if ( + (appendices?.prefix !== undefined ? key.startsWith(appendices?.prefix) : true) && + (appendices?.suffix !== undefined ? key.endsWith(appendices?.suffix) : true) + ) { + result[key] = getSecretsRes[key]; + } + return result; + }, + {} + ); + + // add secrets + for await (const key of Object.keys(secrets)) { + if (!(key in getSecretsRes)) { + // case: secret does not exist in checkly + // -> add secret + await standardRequest.post( + `${INTEGRATION_CHECKLY_API_URL}/v1/variables`, + { + key, + value: secrets[key].value + }, + { + headers: { + Authorization: `Bearer ${accessToken}`, + Accept: "application/json", + "Content-Type": "application/json", + "X-Checkly-Account": integration.appId + } + } + ); + } else { + // case: secret exists in checkly + // -> update/set secret + + if (secrets[key] !== getSecretsRes[key]) { + await standardRequest.put( + `${INTEGRATION_CHECKLY_API_URL}/v1/variables/${key}`, + { + value: secrets[key].value + }, + { + headers: { + Authorization: `Bearer ${accessToken}`, + "Content-Type": "application/json", + Accept: "application/json", + "X-Checkly-Account": integration.appId + } + } + ); + } + } + } + + for await (const key of Object.keys(getSecretsRes)) { + if (!(key in secrets)) { + // delete secret + await standardRequest.delete(`${INTEGRATION_CHECKLY_API_URL}/v1/variables/${key}`, { + headers: { + Authorization: `Bearer ${accessToken}`, + Accept: "application/json", + "X-Checkly-Account": integration.appId + } + }); + } + } } }; diff --git a/frontend/src/pages/integrations/checkly/create.tsx b/frontend/src/pages/integrations/checkly/create.tsx index 9c203bb4c..41122548b 100644 --- a/frontend/src/pages/integrations/checkly/create.tsx +++ b/frontend/src/pages/integrations/checkly/create.tsx @@ -84,6 +84,16 @@ export default function ChecklyCreateIntegrationPage() { } }, [integrationAuthApps]); + const handleValueChange = (val) => { + const selectedGroup = integrationAuthGroups.find(group => group.name === val); + if (selectedGroup) { + setTargetGroupId(selectedGroup.groupId); + } else { + setTargetGroupId(""); + } + setTargetGroup(val); + } + const handleButtonClick = async () => { try { if (!integrationAuth?._id) return; @@ -96,6 +106,8 @@ export default function ChecklyCreateIntegrationPage() { app: targetApp, appId: targetAppId, sourceEnvironment: selectedSourceEnvironment, + targetService: targetGroup, + targetServiceId: targetGroupId.toString(), secretPath, metadata: { secretSuffix @@ -188,7 +200,7 @@ export default function ChecklyCreateIntegrationPage() {