Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 | 38x 38x 38x 38x 21x 21x 21x 21x 1x 21x 1x 1x 20x 20x 20x 20x 18x 18x 18x 17x 17x 18x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 20x 3x 3x 20x 1x 1x 20x 1x 1x 20x 1x 1x 20x 1x 1x 20x 1x 1x 20x 1x 11x 11x 11x 11x 20x 2x 2x 20x 1x 1x 20x 1x 1x 20x 1x 1x 20x 1x 1x 20x 1x 1x 20x 1x 1x 20x 20x 38x 11x 11x 11x 11x 11x 1x 1x 10x 9x 10x 5x 5x 10x 3x 3x 10x 2x 2x 10x 2x 2x 10x 10x 110x 110x 10x 10x 10x 2x 2x 10x 10x 38x 2x 2x 1x 38x 2x 2x 1x 38x 5x 5x 1x 1x 4x 4x 4x 3x 2x 1x 1x 1x 1x 1x 1x | /**
* Profile Applicator - applies profile settings to environment and config
*
* Converts profile configuration into environment variables and runtime settings
* that the rest of the application understands.
*/
import { Profile, Preset, ProfileValidationResult } from "./types";
import { ProfileLoader } from "./loader";
import { logger } from "../logger";
// ============================================================================
// Environment Variable Mapping
// ============================================================================
/**
* Map of profile feature flags to USE_* environment variables
*/
const FEATURE_ENV_MAP: Record<string, string> = {
wiki: "USE_GITLAB_WIKI",
milestones: "USE_MILESTONE",
pipelines: "USE_PIPELINE",
labels: "USE_LABELS",
mrs: "USE_MRS",
files: "USE_FILES",
variables: "USE_VARIABLES",
workitems: "USE_WORKITEMS",
webhooks: "USE_WEBHOOKS",
snippets: "USE_SNIPPETS",
integrations: "USE_INTEGRATIONS",
};
// ============================================================================
// Profile Application Result
// ============================================================================
export interface ApplyProfileResult {
success: boolean;
profileName: string;
host: string;
appliedSettings: string[];
validation: ProfileValidationResult;
}
export interface ApplyPresetResult {
success: boolean;
presetName: string;
appliedSettings: string[];
validation: ProfileValidationResult;
}
// ============================================================================
// Apply Profile
// ============================================================================
/**
* Apply a profile's settings to environment variables
*
* This function sets environment variables based on the profile configuration.
* The rest of the application reads from environment variables, so this bridges
* the gap between profile config and runtime behavior.
*
* @param profile - The profile to apply
* @param profileName - Name of the profile (for logging)
* @returns Result of applying the profile
*/
export async function applyProfile(
profile: Profile,
profileName: string
): Promise<ApplyProfileResult> {
const appliedSettings: string[] = [];
const loader = new ProfileLoader();
const validation = await loader.validateProfile(profile);
// Log warnings but continue
for (const warning of validation.warnings) {
logger.warn({ profile: profileName }, warning);
}
// Stop on errors
if (!validation.valid) {
logger.error({ profile: profileName, errors: validation.errors }, "Profile validation failed");
return {
success: false,
profileName,
host: profile.host,
appliedSettings,
validation,
};
}
// Apply connection settings
const apiUrl = profile.api_url ?? `https://${profile.host}`;
process.env.GITLAB_API_URL = apiUrl;
appliedSettings.push(`GITLAB_API_URL=${apiUrl}`);
// Apply authentication
switch (profile.auth.type) {
case "pat":
Eif (profile.auth.token_env) {
const token = process.env[profile.auth.token_env];
if (token) {
process.env.GITLAB_TOKEN = token;
appliedSettings.push(`GITLAB_TOKEN=<from ${profile.auth.token_env}>`);
}
}
break;
case "oauth":
Eif (profile.auth.client_id_env) {
const clientId = process.env[profile.auth.client_id_env];
Eif (clientId) {
process.env.GITLAB_OAUTH_CLIENT_ID = clientId;
appliedSettings.push(`GITLAB_OAUTH_CLIENT_ID=<from ${profile.auth.client_id_env}>`);
}
}
Eif (profile.auth.client_secret_env) {
const clientSecret = process.env[profile.auth.client_secret_env];
Eif (clientSecret) {
process.env.GITLAB_OAUTH_CLIENT_SECRET = clientSecret;
appliedSettings.push(
`GITLAB_OAUTH_CLIENT_SECRET=<from ${profile.auth.client_secret_env}>`
);
}
}
process.env.OAUTH_ENABLED = "true";
appliedSettings.push("OAUTH_ENABLED=true");
break;
case "cookie":
Eif (profile.auth.cookie_path) {
process.env.GITLAB_AUTH_COOKIE_PATH = profile.auth.cookie_path;
appliedSettings.push(`GITLAB_AUTH_COOKIE_PATH=${profile.auth.cookie_path}`);
}
break;
}
// Apply access control
if (profile.read_only) {
process.env.GITLAB_READ_ONLY_MODE = "true";
appliedSettings.push("GITLAB_READ_ONLY_MODE=true");
}
if (profile.allowed_projects && profile.allowed_projects.length > 0) {
process.env.GITLAB_ALLOWED_PROJECT_IDS = profile.allowed_projects.join(",");
appliedSettings.push(`GITLAB_ALLOWED_PROJECT_IDS=${profile.allowed_projects.join(",")}`);
}
if (profile.allowed_groups && profile.allowed_groups.length > 0) {
process.env.GITLAB_ALLOWED_GROUP_IDS = profile.allowed_groups.join(",");
appliedSettings.push(`GITLAB_ALLOWED_GROUP_IDS=${profile.allowed_groups.join(",")}`);
}
if (profile.allowed_tools && profile.allowed_tools.length > 0) {
process.env.GITLAB_ALLOWED_TOOLS = profile.allowed_tools.join(",");
appliedSettings.push(`GITLAB_ALLOWED_TOOLS=${profile.allowed_tools.join(",")}`);
}
if (profile.denied_tools_regex) {
process.env.GITLAB_DENIED_TOOLS_REGEX = profile.denied_tools_regex;
appliedSettings.push(`GITLAB_DENIED_TOOLS_REGEX=${profile.denied_tools_regex}`);
}
if (profile.denied_actions && profile.denied_actions.length > 0) {
process.env.GITLAB_DENIED_ACTIONS = profile.denied_actions.join(",");
appliedSettings.push(`GITLAB_DENIED_ACTIONS=${profile.denied_actions.join(",")}`);
}
// Apply feature flags
if (profile.features) {
for (const [feature, envVar] of Object.entries(FEATURE_ENV_MAP)) {
const value = profile.features[feature as keyof typeof profile.features];
Eif (value !== undefined) {
process.env[envVar] = value ? "true" : "false";
appliedSettings.push(`${envVar}=${value}`);
}
}
}
// Apply timeout
if (profile.timeout_ms) {
process.env.GITLAB_API_TIMEOUT_MS = String(profile.timeout_ms);
appliedSettings.push(`GITLAB_API_TIMEOUT_MS=${profile.timeout_ms}`);
}
// Apply TLS settings
if (profile.skip_tls_verify) {
process.env.SKIP_TLS_VERIFY = "true";
appliedSettings.push("SKIP_TLS_VERIFY=true");
}
if (profile.ssl_cert_path) {
process.env.SSL_CERT_PATH = profile.ssl_cert_path;
appliedSettings.push(`SSL_CERT_PATH=${profile.ssl_cert_path}`);
}
if (profile.ssl_key_path) {
process.env.SSL_KEY_PATH = profile.ssl_key_path;
appliedSettings.push(`SSL_KEY_PATH=${profile.ssl_key_path}`);
}
if (profile.ca_cert_path) {
process.env.GITLAB_CA_CERT_PATH = profile.ca_cert_path;
appliedSettings.push(`GITLAB_CA_CERT_PATH=${profile.ca_cert_path}`);
}
// Apply default project/namespace
if (profile.default_project) {
process.env.GITLAB_PROJECT_ID = profile.default_project;
appliedSettings.push(`GITLAB_PROJECT_ID=${profile.default_project}`);
}
if (profile.default_namespace) {
process.env.GITLAB_DEFAULT_NAMESPACE = profile.default_namespace;
appliedSettings.push(`GITLAB_DEFAULT_NAMESPACE=${profile.default_namespace}`);
}
logger.info(
{
profile: profileName,
host: profile.host,
authType: profile.auth.type,
readOnly: profile.read_only ?? false,
settingsCount: appliedSettings.length,
},
"Profile applied successfully"
);
return {
success: true,
profileName,
host: profile.host,
appliedSettings,
validation,
};
}
// ============================================================================
// Apply Preset
// ============================================================================
/**
* Apply a preset's settings to environment variables
*
* Presets are applied ON TOP of existing environment configuration.
* They do NOT set host or auth - those must already be configured via
* GITLAB_API_URL and GITLAB_TOKEN environment variables.
*
* @param preset - The preset to apply
* @param presetName - Name of the preset (for logging)
* @returns Result of applying the preset
*/
export async function applyPreset(preset: Preset, presetName: string): Promise<ApplyPresetResult> {
const appliedSettings: string[] = [];
const loader = new ProfileLoader();
const validation = await loader.validatePreset(preset);
// Log warnings but continue
for (const warning of validation.warnings) {
logger.warn({ preset: presetName }, warning);
}
// Stop on errors
if (!validation.valid) {
logger.error({ preset: presetName, errors: validation.errors }, "Preset validation failed");
return {
success: false,
presetName,
appliedSettings,
validation,
};
}
// Verify that host/auth are already configured (presets require existing connection)
if (!process.env.GITLAB_API_URL && !process.env.GITLAB_TOKEN) {
logger.warn(
{ preset: presetName },
"Preset applied but GITLAB_API_URL/GITLAB_TOKEN not set - connection may fail"
);
}
// Apply access control
if (preset.read_only) {
process.env.GITLAB_READ_ONLY_MODE = "true";
appliedSettings.push("GITLAB_READ_ONLY_MODE=true");
}
if (preset.denied_tools_regex) {
process.env.GITLAB_DENIED_TOOLS_REGEX = preset.denied_tools_regex;
appliedSettings.push(`GITLAB_DENIED_TOOLS_REGEX=${preset.denied_tools_regex}`);
}
if (preset.denied_actions && preset.denied_actions.length > 0) {
process.env.GITLAB_DENIED_ACTIONS = preset.denied_actions.join(",");
appliedSettings.push(`GITLAB_DENIED_ACTIONS=${preset.denied_actions.join(",")}`);
}
if (preset.allowed_tools && preset.allowed_tools.length > 0) {
process.env.GITLAB_ALLOWED_TOOLS = preset.allowed_tools.join(",");
appliedSettings.push(`GITLAB_ALLOWED_TOOLS=${preset.allowed_tools.join(",")}`);
}
// Apply feature flags
Eif (preset.features) {
for (const [feature, envVar] of Object.entries(FEATURE_ENV_MAP)) {
const value = preset.features[feature as keyof typeof preset.features];
if (value !== undefined) {
process.env[envVar] = value ? "true" : "false";
appliedSettings.push(`${envVar}=${value}`);
}
}
}
// Apply timeout
if (preset.timeout_ms) {
process.env.GITLAB_API_TIMEOUT_MS = String(preset.timeout_ms);
appliedSettings.push(`GITLAB_API_TIMEOUT_MS=${preset.timeout_ms}`);
}
logger.info(
{
preset: presetName,
readOnly: preset.read_only ?? false,
settingsCount: appliedSettings.length,
},
"Preset applied successfully"
);
return {
success: true,
presetName,
appliedSettings,
validation,
};
}
// ============================================================================
// Load and Apply Profile/Preset
// ============================================================================
/**
* Load and apply a profile by name
*
* Convenience function that combines loading and applying.
*
* @param profileName - Name of the profile to load and apply
* @returns Result of applying the profile
*/
export async function loadAndApplyProfile(profileName: string): Promise<ApplyProfileResult> {
const loader = new ProfileLoader();
const profile = await loader.loadProfile(profileName);
return applyProfile(profile, profileName);
}
/**
* Load and apply a preset by name
*
* Convenience function that combines loading and applying.
*
* @param presetName - Name of the preset to load and apply
* @returns Result of applying the preset
*/
export async function loadAndApplyPreset(presetName: string): Promise<ApplyPresetResult> {
const loader = new ProfileLoader();
const preset = await loader.loadPreset(presetName);
return applyPreset(preset, presetName);
}
/**
* Try to apply profile or preset from environment or CLI args
*
* Tries user profile first, then falls back to built-in preset.
* This allows using built-in presets like "readonly" with --profile flag.
*
* @param cliProfileName - Profile/preset name from CLI argument (optional)
* @returns Result if a profile/preset was applied, undefined otherwise
*/
export async function tryApplyProfileFromEnv(
cliProfileName?: string
): Promise<ApplyProfileResult | ApplyPresetResult | undefined> {
// Priority: CLI arg > env var > default profile
const name = cliProfileName ?? process.env.GITLAB_PROFILE ?? (await getDefaultProfileName());
if (!name) {
logger.debug("No profile specified, using environment variables directly");
return undefined;
}
try {
const loader = new ProfileLoader();
const loaded = await loader.loadAny(name);
if (loaded.type === "profile") {
return await applyProfile(loaded.data, name);
} else {
return await applyPreset(loaded.data, name);
}
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
logger.error({ profile: name, error: message }, "Failed to apply profile/preset");
throw error;
}
}
/**
* Get default profile name from user config
*/
async function getDefaultProfileName(): Promise<string | undefined> {
const loader = new ProfileLoader();
return loader.getDefaultProfileName();
}
|