mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-11 06:29:04 +00:00
add try catch
This commit is contained in:
@@ -48,79 +48,103 @@ export const RailwaySyncFns = {
|
||||
* If there's a service, triggers a redeploy to pick up the changes.
|
||||
*/
|
||||
async syncSecrets(secretSync: TRailwaySyncWithCredentials, secretMap: TSecretMap) {
|
||||
const {
|
||||
syncOptions: { disableSecretDeletion }
|
||||
} = secretSync;
|
||||
const railwaySecrets = await this.getSecrets(secretSync);
|
||||
const config = secretSync.destinationConfig;
|
||||
try {
|
||||
const {
|
||||
syncOptions: { disableSecretDeletion }
|
||||
} = secretSync;
|
||||
const railwaySecrets = await this.getSecrets(secretSync);
|
||||
const config = secretSync.destinationConfig;
|
||||
|
||||
const railwaySecretsMap = Object.fromEntries(Object.entries(railwaySecrets).map(([key, secret]) => [key, secret.value]));
|
||||
const secretMapMap = Object.fromEntries(Object.entries(secretMap).map(([key, secret]) => [key, secret.value]));
|
||||
const railwaySecretsMap = Object.fromEntries(
|
||||
Object.entries(railwaySecrets).map(([key, secret]) => [key, secret.value])
|
||||
);
|
||||
const secretMapMap = Object.fromEntries(Object.entries(secretMap).map(([key, secret]) => [key, secret.value]));
|
||||
|
||||
const toReplace = disableSecretDeletion
|
||||
? { ...railwaySecretsMap, ...secretMapMap }
|
||||
: secretMapMap;
|
||||
const toReplace = disableSecretDeletion ? { ...railwaySecretsMap, ...secretMapMap } : secretMapMap;
|
||||
|
||||
const upserted = await RailwayPublicAPI.upsertCollection(secretSync.connection, {
|
||||
input: {
|
||||
projectId: config.projectId,
|
||||
environmentId: config.environmentId,
|
||||
serviceId: config.serviceId || undefined,
|
||||
skipDeploys: true,
|
||||
variables: toReplace,
|
||||
replace: true,
|
||||
}
|
||||
});
|
||||
const upserted = await RailwayPublicAPI.upsertCollection(secretSync.connection, {
|
||||
input: {
|
||||
projectId: config.projectId,
|
||||
environmentId: config.environmentId,
|
||||
serviceId: config.serviceId || undefined,
|
||||
skipDeploys: true,
|
||||
variables: toReplace,
|
||||
replace: true
|
||||
}
|
||||
});
|
||||
|
||||
if (!upserted) throw new SecretSyncError({
|
||||
message: "Failed to upsert secrets to Railway",
|
||||
})
|
||||
if (!upserted)
|
||||
throw new SecretSyncError({
|
||||
message: "Failed to upsert secrets to Railway"
|
||||
});
|
||||
|
||||
if (!config.serviceId) return;
|
||||
if (!config.serviceId) return;
|
||||
|
||||
const latestDeployment = await RailwayPublicAPI.getDeployments(secretSync.connection, {
|
||||
input: {
|
||||
serviceId: config.serviceId,
|
||||
environmentId: config.environmentId
|
||||
},
|
||||
first: 1,
|
||||
});
|
||||
const latestDeployment = await RailwayPublicAPI.getDeployments(secretSync.connection, {
|
||||
input: {
|
||||
serviceId: config.serviceId,
|
||||
environmentId: config.environmentId
|
||||
},
|
||||
first: 1
|
||||
});
|
||||
|
||||
const latestDeploymentId = latestDeployment?.deployments.edges[0].node.id;
|
||||
const latestDeploymentId = latestDeployment?.deployments.edges[0].node.id;
|
||||
|
||||
if (!latestDeploymentId) throw new SecretSyncError({
|
||||
message: "Failed to get latest deployment from Railway",
|
||||
})
|
||||
if (!latestDeploymentId)
|
||||
throw new SecretSyncError({
|
||||
message: "Failed to get latest deployment from Railway"
|
||||
});
|
||||
|
||||
await RailwayPublicAPI.redeployDeployment(secretSync.connection, {
|
||||
input: {
|
||||
deploymentId: latestDeploymentId
|
||||
}
|
||||
});
|
||||
await RailwayPublicAPI.redeployDeployment(secretSync.connection, {
|
||||
input: {
|
||||
deploymentId: latestDeploymentId
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof SecretSyncError) throw error;
|
||||
|
||||
throw new SecretSyncError({
|
||||
error,
|
||||
message: "Failed to sync secrets to Railway"
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
async removeSecrets(secretSync: TRailwaySyncWithCredentials, secretMap: TSecretMap) {
|
||||
const existing = await this.getSecrets(secretSync);
|
||||
const config = secretSync.destinationConfig;
|
||||
|
||||
for await (const secret of Object.keys(existing)) {
|
||||
try {
|
||||
if (secret in secretMap) {
|
||||
await RailwayPublicAPI.deleteVariable(secretSync.connection, {
|
||||
input: {
|
||||
projectId: config.projectId,
|
||||
environmentId: config.environmentId,
|
||||
serviceId: config.serviceId || undefined,
|
||||
name: secret
|
||||
}
|
||||
});
|
||||
// Create a new variables object excluding secrets that exist in secretMap
|
||||
const remainingVariables = Object.fromEntries(
|
||||
Object.entries(existing)
|
||||
.filter(([key]) => !(key in secretMap))
|
||||
.map(([key, secret]) => [key, secret.value])
|
||||
);
|
||||
|
||||
try {
|
||||
const upserted = await RailwayPublicAPI.upsertCollection(secretSync.connection, {
|
||||
input: {
|
||||
projectId: config.projectId,
|
||||
environmentId: config.environmentId,
|
||||
serviceId: config.serviceId || undefined,
|
||||
skipDeploys: true,
|
||||
variables: remainingVariables,
|
||||
replace: true
|
||||
}
|
||||
} catch (error) {
|
||||
});
|
||||
|
||||
if (!upserted) {
|
||||
throw new SecretSyncError({
|
||||
error,
|
||||
secretKey: secret
|
||||
message: "Failed to remove secrets from Railway"
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
if (error instanceof SecretSyncError) throw error;
|
||||
|
||||
throw new SecretSyncError({
|
||||
error,
|
||||
message: "Failed to remove secrets from Railway"
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user