Optimize Cloud66 integration sync function

This commit is contained in:
Tuan Dang
2023-07-23 22:16:27 +07:00
parent d48b5157d4
commit 77fdb6307c
4 changed files with 71 additions and 40 deletions
+6 -3
View File
@@ -826,7 +826,6 @@ const getAppsBitBucket = async ({
* @returns {Object[]} apps - names of Supabase apps * @returns {Object[]} apps - names of Supabase apps
* @returns {String} apps.name - name of Supabase app * @returns {String} apps.name - name of Supabase app
*/ */
const getAppsCodefresh = async ({ const getAppsCodefresh = async ({
accessToken, accessToken,
}: { }: {
@@ -851,9 +850,13 @@ const getAppsCodefresh = async ({
}; };
/** /**
* Return list of projects for Cloud 66 integration * Return list of applications for Cloud66 integration
* @param {Object} obj
* @param {String} obj.accessToken - personal access token for Cloud66 API
* @returns {Object[]} apps - Cloud66 apps
* @returns {String} apps.name - name of Cloud66 app
* @returns {String} apps.appId - uid of Cloud66 app
*/ */
const getAppsCloud66 = async ({ accessToken }: { accessToken: string }) => { const getAppsCloud66 = async ({ accessToken }: { accessToken: string }) => {
interface Cloud66Apps { interface Cloud66Apps {
uid: string; uid: string;
+61 -33
View File
@@ -2077,10 +2077,11 @@ const syncSecretsBitBucket = async ({
} }
} }
/* /**
* Sync/push [secrets] to Codefresh with name [integration.app] * Sync/push [secrets] to Codefresh project with name [integration.app]
* @param {Object} obj * @param {Object} obj
* @param {IIntegration} obj.integration - integration details * @param {IIntegration} obj.integration - integration details
* @param {IIntegrationAuth} obj.integrationAuth - integration auth details
* @param {Object} obj.secrets - secrets to push to integration (object where keys are secret keys and values are secret values) * @param {Object} obj.secrets - secrets to push to integration (object where keys are secret keys and values are secret values)
* @param {String} obj.accessToken - access token for Codefresh integration * @param {String} obj.accessToken - access token for Codefresh integration
*/ */
@@ -2110,7 +2111,14 @@ const syncSecretsCodefresh = async ({
); );
}; };
/**
* Sync/push [secrets] to Cloud66 application with name [integration.app]
* @param {Object} obj
* @param {IIntegration} obj.integration - integration details
* @param {IIntegrationAuth} obj.integrationAuth - integration auth details
* @param {Object} obj.secrets - secrets to push to integration (object where keys are secret keys and values are secret values)
* @param {String} obj.accessToken - access token for Cloud66 integration
*/
const syncSecretsCloud66 = async ({ const syncSecretsCloud66 = async ({
integration, integration,
secrets, secrets,
@@ -2133,7 +2141,7 @@ const syncSecretsCloud66 = async ({
} }
// get all current secrets // get all current secrets
const currentSecrets = ( const res = (
await standardRequest.get( await standardRequest.get(
`${INTEGRATION_CLOUD_66_API_URL}/3/stacks/${integration.appId}/environments`, `${INTEGRATION_CLOUD_66_API_URL}/3/stacks/${integration.appId}/environments`,
{ {
@@ -2143,36 +2151,26 @@ const syncSecretsCloud66 = async ({
} }
} }
) )
).data.response as Cloud66Secret[]; )
.data
// filter out generated secrets .response
const secretsToDelete = currentSecrets.filter((s) => !s.is_generated); .filter((secret: Cloud66Secret) => !secret.readonly || !secret.is_generated)
.reduce(
// delete all current secrets (obj: any, secret: any) => ({
await Promise.all( ...obj,
secretsToDelete.map( [secret.key]: secret
async (s) => }),
await standardRequest.delete( {}
`${INTEGRATION_CLOUD_66_API_URL}/3/stacks/${integration.appId}/environments/${s.key}`,
{
headers: {
Authorization: `Bearer ${accessToken}`,
Accept: "application/json"
}
}
)
)
); );
// add new secrets for await (const key of Object.keys(secrets)) {
await Promise.all( if (key in res) {
Object.entries(secrets).map( // update existing secret
async ([key, value]) => await standardRequest.put(
await standardRequest.post( `${INTEGRATION_CLOUD_66_API_URL}/3/stacks/${integration.appId}/environments/${key}`,
`${INTEGRATION_CLOUD_66_API_URL}/3/stacks/${integration.appId}/environments`,
{ {
key, key,
value value: secrets[key]
}, },
{ {
headers: { headers: {
@@ -2180,9 +2178,39 @@ const syncSecretsCloud66 = async ({
Accept: "application/json" Accept: "application/json"
} }
} }
) );
) } else {
); // create new secret
await standardRequest.post(
`${INTEGRATION_CLOUD_66_API_URL}/3/stacks/${integration.appId}/environments`,
{
key,
value: secrets[key]
},
{
headers: {
Authorization: `Bearer ${accessToken}`,
Accept: "application/json"
}
}
);
}
}
for await (const key of Object.keys(res)) {
if (!(key in secrets)) {
// delete secret
await standardRequest.delete(
`${INTEGRATION_CLOUD_66_API_URL}/3/stacks/${integration.appId}/environments/${key}`,
{
headers: {
Authorization: `Bearer ${accessToken}`,
Accept: "application/json"
}
}
);
}
}
}; };
export { syncSecrets }; export { syncSecrets };
@@ -14,7 +14,7 @@ export default function Cloud66CreateIntegrationPage() {
try { try {
setApiKeyErrorText(""); setApiKeyErrorText("");
if (apiKey.length === 0) { if (apiKey.length === 0) {
setApiKeyErrorText("API Key cannot be blank"); setApiKeyErrorText("Access token cannot be blank");
return; return;
} }
@@ -42,7 +42,7 @@ export default function Cloud66CreateIntegrationPage() {
<Card className="max-w-md rounded-md p-8"> <Card className="max-w-md rounded-md p-8">
<CardTitle className="text-center">Cloud 66 Integration</CardTitle> <CardTitle className="text-center">Cloud 66 Integration</CardTitle>
<FormControl <FormControl
label="Cloud 66 API Key" label="Cloud 66 Personal Access Token"
errorText={apiKeyErrorText} errorText={apiKeyErrorText}
isError={apiKeyErrorText !== "" ?? false} isError={apiKeyErrorText !== "" ?? false}
> >
@@ -113,7 +113,7 @@ export default function Cloud66CreateIntegrationPage() {
placeholder="Provide a path, default is /" placeholder="Provide a path, default is /"
/> />
</FormControl> </FormControl>
<FormControl label="Cloud 66 Service" className="mt-4"> <FormControl label="Cloud 66 Application" className="mt-4">
<Select <Select
value={targetApp} value={targetApp}
onValueChange={(val) => setTargetApp(val)} onValueChange={(val) => setTargetApp(val)}
@@ -131,7 +131,7 @@ export default function Cloud66CreateIntegrationPage() {
)) ))
) : ( ) : (
<SelectItem value="none" key="target-app-none"> <SelectItem value="none" key="target-app-none">
No services found No applications found
</SelectItem> </SelectItem>
)} )}
</Select> </Select>