@simonecoelhosfo/optimizely-mcp-server
Version:
Optimizely MCP Server for AI assistants with integrated CLI tools
145 lines • 5.76 kB
JavaScript
/**
* Environment Filter for Per-Project Environment Syncing
* @description Manages which environments should be synced for each project
* based on environment variable configuration.
*
* Supports format: PROJECT_ID:env1,env2,env3|PROJECT_ID2:env1,env2
* Example: 20224828075:production,development|8923080126:production
*/
import { getLogger } from '../logging/Logger.js';
export class EnvironmentFilter {
config = {};
isConfigured = false;
constructor(configString) {
if (configString) {
this.parseConfig(configString);
}
}
/**
* Parse environment configuration from string
* Format: PROJECT_ID:env1,env2,env3|PROJECT_ID2:env1,env2
*/
parseConfig(configString) {
try {
if (!configString.trim()) {
this.isConfigured = false;
return;
}
const projectConfigs = configString.split('|');
for (const projectConfig of projectConfigs) {
const [projectId, environmentsStr] = projectConfig.split(':');
if (!projectId || !environmentsStr) {
getLogger().warn({
projectConfig,
configString
}, 'EnvironmentFilter: Invalid project-environment mapping format');
continue;
}
const environments = environmentsStr
.split(',')
.map(env => env.trim())
.filter(env => env.length > 0);
if (environments.length > 0) {
this.config[projectId.trim()] = environments;
}
}
this.isConfigured = Object.keys(this.config).length > 0;
getLogger().info({
config: this.config,
projectCount: Object.keys(this.config).length
}, 'EnvironmentFilter: Successfully parsed environment configuration');
}
catch (error) {
getLogger().error({
configString,
error: error.message
}, 'EnvironmentFilter: Failed to parse environment configuration');
this.config = {};
this.isConfigured = false;
}
}
/**
* Filter environments for a specific project
* @param projectId - Project ID to filter environments for
* @param allEnvironments - All available environments for the project
* @returns Filtered environments or all environments if no filter configured
*/
filterEnvironments(projectId, allEnvironments) {
const projectIdStr = String(projectId);
// If no configuration, return all environments
if (!this.isConfigured || !this.config[projectIdStr]) {
getLogger().debug({
projectId: projectIdStr,
environmentCount: allEnvironments.length,
action: 'no_filter'
}, 'EnvironmentFilter: No environment filter configured for project, syncing all environments');
return allEnvironments;
}
const allowedEnvironments = this.config[projectIdStr];
const filteredEnvironments = allEnvironments.filter(env => {
// Check both 'key' and 'name' properties for environment matching
const envKey = env.key || env.name;
return allowedEnvironments.includes(envKey);
});
getLogger().info({
projectId: projectIdStr,
allowedEnvironments,
totalEnvironments: allEnvironments.length,
filteredEnvironments: filteredEnvironments.length,
filteredNames: filteredEnvironments.map(env => env.key || env.name)
}, 'EnvironmentFilter: Filtered environments for project');
// Warn if no environments match the filter
if (filteredEnvironments.length === 0) {
const availableEnvNames = allEnvironments.map(env => env.key || env.name);
getLogger().warn({
projectId: projectIdStr,
allowedEnvironments,
availableEnvironments: availableEnvNames
}, 'EnvironmentFilter: No environments match the configured filter - this may cause sync issues');
}
return filteredEnvironments;
}
/**
* Get the configured environments for a specific project
* @param projectId - Project ID to get configuration for
* @returns Array of environment names or null if not configured
*/
getProjectConfig(projectId) {
const projectIdStr = String(projectId);
return this.config[projectIdStr] || null;
}
/**
* Check if environment filtering is configured
* @returns True if any environment filters are configured
*/
isFilteringEnabled() {
return this.isConfigured;
}
/**
* Get all configured project IDs
* @returns Array of project IDs that have environment filtering configured
*/
getConfiguredProjects() {
return Object.keys(this.config);
}
/**
* Get summary of current configuration
* @returns Configuration summary for logging/debugging
*/
getConfigSummary() {
return {
isConfigured: this.isConfigured,
projectCount: Object.keys(this.config).length,
config: this.config
};
}
}
/**
* Create EnvironmentFilter from environment variables
* @returns EnvironmentFilter instance configured from SYNC_ENVIRONMENTS_PER_PROJECT
*/
export function createEnvironmentFilterFromEnv() {
const configString = process.env.SYNC_ENVIRONMENTS_PER_PROJECT;
return new EnvironmentFilter(configString);
}
//# sourceMappingURL=EnvironmentFilter.js.map