in controlplane/src/core/bufservices/feature-flag/enableFeatureFlag.ts [20:200]
export function enableFeatureFlag(
opts: RouterOptions,
req: EnableFeatureFlagRequest,
ctx: HandlerContext,
): Promise<PlainMessage<EnableFeatureFlagResponse>> {
let logger = getLogger(ctx, opts.logger);
return handleError<PlainMessage<EnableFeatureFlagResponse>>(ctx, logger, async () => {
const authContext = await opts.authenticator.authenticate(ctx.requestHeader);
logger = enrichLogger(ctx, logger, authContext);
const featureFlagRepo = new FeatureFlagRepository(logger, opts.db, authContext.organizationId);
const namespaceRepo = new NamespaceRepository(opts.db, authContext.organizationId);
const auditLogRepo = new AuditLogRepository(opts.db);
const orgWebhooks = new OrganizationWebhookService(
opts.db,
authContext.organizationId,
opts.logger,
opts.billingDefaultPlanId,
);
req.namespace = req.namespace || DefaultNamespace;
if (!authContext.hasWriteAccess) {
return {
response: {
code: EnumStatusCode.ERR,
details: `The user does not have the permissions to perform this operation`,
},
compositionErrors: [],
deploymentErrors: [],
compositionWarnings: [],
};
}
const namespace = await namespaceRepo.byName(req.namespace);
if (!namespace) {
return {
response: {
code: EnumStatusCode.ERR_NOT_FOUND,
details: `Could not find namespace ${req.namespace}`,
},
compositionErrors: [],
deploymentErrors: [],
compositionWarnings: [],
};
}
const featureFlag = await featureFlagRepo.getFeatureFlagByName({
featureFlagName: req.name,
namespaceId: namespace.id,
});
if (!featureFlag) {
return {
response: {
code: EnumStatusCode.ERR_NOT_FOUND,
details: `The feature flag "${req.name}" was not found.`,
},
compositionErrors: [],
deploymentErrors: [],
compositionWarnings: [],
};
}
if (featureFlag.isEnabled === req.enabled) {
return {
response: {
code: EnumStatusCode.OK,
},
compositionErrors: [],
deploymentErrors: [],
compositionWarnings: [],
hasChanged: false,
};
}
await featureFlagRepo.enableFeatureFlag({
featureFlagId: featureFlag.id,
namespaceId: namespace.id,
isEnabled: req.enabled,
});
const federatedGraphs = await featureFlagRepo.getFederatedGraphsByFeatureFlag({
featureFlagId: featureFlag.id,
namespaceId: namespace.id,
// fetch the federated graphs based on the state that has just been set for the feature flag above
excludeDisabled: req.enabled,
});
const compositionErrors: PlainMessage<CompositionError>[] = [];
const deploymentErrors: PlainMessage<DeploymentError>[] = [];
const compositionWarnings: PlainMessage<CompositionWarning>[] = [];
await opts.db.transaction(async (tx) => {
const fedGraphRepo = new FederatedGraphRepository(logger, tx, authContext.organizationId);
const composition = await fedGraphRepo.composeAndDeployGraphs({
federatedGraphs,
actorId: authContext.userId,
blobStorage: opts.blobStorage,
admissionConfig: {
cdnBaseUrl: opts.cdnBaseUrl,
webhookJWTSecret: opts.admissionWebhookJWTSecret,
},
chClient: opts.chClient!,
});
compositionErrors.push(...composition.compositionErrors);
deploymentErrors.push(...composition.deploymentErrors);
compositionWarnings.push(...composition.compositionWarnings);
});
for (const graph of federatedGraphs) {
orgWebhooks.send(
{
eventName: OrganizationEventName.FEDERATED_GRAPH_SCHEMA_UPDATED,
payload: {
federated_graph: {
id: graph.id,
name: graph.name,
namespace: graph.namespace,
},
organization: {
id: authContext.organizationId,
slug: authContext.organizationSlug,
},
errors: compositionErrors.length > 0 || deploymentErrors.length > 0,
actor_id: authContext.userId,
},
},
authContext.userId,
);
}
await auditLogRepo.addAuditLog({
organizationId: authContext.organizationId,
auditAction: req.enabled ? 'feature_flag.enabled' : 'feature_flag.disabled',
action: req.enabled ? 'enabled' : 'disabled',
actorId: authContext.userId,
auditableType: 'feature_flag',
auditableDisplayName: featureFlag.name,
apiKeyName: authContext.apiKeyName,
actorDisplayName: authContext.userDisplayName,
actorType: authContext.auth === 'api_key' ? 'api_key' : 'user',
targetNamespaceId: featureFlag.namespaceId,
targetNamespaceDisplayName: featureFlag.namespace,
});
if (compositionErrors.length > 0) {
return {
response: {
code: EnumStatusCode.ERR_SUBGRAPH_COMPOSITION_FAILED,
},
compositionErrors,
deploymentErrors: [],
compositionWarnings,
};
}
if (deploymentErrors.length > 0) {
return {
response: {
code: EnumStatusCode.ERR_DEPLOYMENT_FAILED,
},
compositionErrors: [],
deploymentErrors,
compositionWarnings,
};
}
return {
response: {
code: EnumStatusCode.OK,
},
compositionErrors,
deploymentErrors,
compositionWarnings,
hasChanged: true,
};
});
}