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 | 3x 3x 3x 3x 3x 3x 15x 15x 15x 15x 1x 1x 14x 14x 14x 14x 4x 10x 14x 14x 13x 13x 1x 14x 14x 14x 13x 3x 3x 1x 1x 2x 2x 10x 9x 9x 13x 1x 1x 13x 14x 13x 12x 12x 13x 12x 12x 12x 12x 3x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 1x 1x 2x 2x 1x 16x 16x 2x 2x 2x 1x 2x 1x 14x 14x 14x 16x 16x 16x 7x 7x 6x 6x 5x 5x 2x 3x 1x 2x 1x 6x 1x 7x 2x 2x 1x 9x 16x 16x 16x 16x 16x 16x | /**
* Auto-discovery orchestrator
*
* Automatically detects GitLab configuration from the current git repository:
* 1. Parses git remote to get host and project path
* 2. Matches host to user-defined profile (if available)
* 3. Loads project-level configs from .gitlab-mcp/ (if present)
* 4. Sets default context (namespace/project)
*/
import { parseGitRemote, GitRemoteInfo, listGitRemotes } from "./git-remote";
import { findProfileByHost, ProfileMatchResult } from "./profile-matcher";
import { findProjectConfig, ProjectConfig, loadAndApplyProfile } from "../profiles";
import { logger } from "../logger";
import { extractNamespaceFromPath } from "../utils/namespace";
// ============================================================================
// Types
// ============================================================================
export interface AutoDiscoveryOptions {
/** Path to repository (default: current directory) */
repoPath?: string;
/** Remote name to use (default: origin) */
remoteName?: string;
/** Skip project config loading */
noProjectConfig?: boolean;
/** Dry run - don't apply any changes */
dryRun?: boolean;
}
export interface AutoDiscoveryResult {
/** Detected GitLab host */
host: string;
/** Detected project path (group/project) */
projectPath: string;
/** Git remote info */
remote: GitRemoteInfo;
/** Matched user profile (if any) */
matchedProfile: ProfileMatchResult | null;
/** Project configuration (if found) */
projectConfig: ProjectConfig | null;
/** Computed GitLab API URL */
apiUrl: string;
/** Whether profile was applied */
profileApplied: boolean;
/** Whether project config was applied */
projectConfigApplied: boolean;
/** All available remotes (for multi-remote scenarios) */
availableRemotes: GitRemoteInfo[];
}
// ============================================================================
// Discovery Logic
// ============================================================================
/**
* Auto-discover GitLab configuration from current repository
*
* @param options Discovery options
* @returns Discovery result or null if not in a git repo
*/
export async function autoDiscover(
options: AutoDiscoveryOptions = {}
): Promise<AutoDiscoveryResult | null> {
const repoPath = options.repoPath ?? process.cwd();
logger.info({ path: repoPath }, "Starting auto-discovery");
// 1. Parse git remote
const remote = await parseGitRemote({
repoPath,
remoteName: options.remoteName,
});
if (!remote) {
logger.warn({ path: repoPath }, "Auto-discovery: No git remote found");
return null;
}
logger.info(
{
host: remote.host,
projectPath: remote.projectPath,
remote: remote.remoteName,
},
"Detected git remote"
);
// Get all available remotes for info
const availableRemotes = await listGitRemotes(repoPath);
// 2. Match host to user profile
const matchedProfile = await findProfileByHost(remote.host);
if (matchedProfile) {
logger.info(
{
profile: matchedProfile.profileName,
matchType: matchedProfile.matchType,
},
"Matched host to user profile"
);
} else {
logger.debug({ host: remote.host }, "No matching user profile found");
}
// 3. Load project configs (unless disabled)
let projectConfig: ProjectConfig | null = null;
if (!options.noProjectConfig) {
projectConfig = await findProjectConfig(repoPath);
if (projectConfig) {
logger.info({ path: projectConfig.configPath }, "Found project configuration");
}
}
// Compute API URL
const apiUrl = `https://${remote.host}`;
// Build result
const result: AutoDiscoveryResult = {
host: remote.host,
projectPath: remote.projectPath,
remote,
matchedProfile,
projectConfig,
apiUrl,
profileApplied: false,
projectConfigApplied: false,
availableRemotes,
};
// 4. Apply configuration (unless dry run)
if (!options.dryRun) {
// Apply profile if matched
if (matchedProfile) {
try {
await loadAndApplyProfile(matchedProfile.profileName);
result.profileApplied = true;
logger.info({ profile: matchedProfile.profileName }, "Applied matched profile");
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
logger.error({ error: message }, "Failed to apply matched profile");
}
} else {
// Set API URL from discovered host if no profile matched
if (!process.env.GITLAB_API_URL) {
process.env.GITLAB_API_URL = apiUrl;
logger.info({ apiUrl }, "Set GITLAB_API_URL from discovered host");
}
}
// Apply project config
if (projectConfig) {
result.projectConfigApplied = true;
// Project config application is logged but not enforced yet
// See: https://github.com/structured-world/gitlab-mcp/issues/61
logger.debug({ config: projectConfig }, "Project config loaded (enforcement pending)");
}
// Set default context
setDefaultContext(remote.projectPath);
}
return result;
}
/**
* Set default project/namespace context from discovered project path
*/
function setDefaultContext(projectPath: string): void {
// Set as environment variables for tools to use
if (!process.env.GITLAB_DEFAULT_PROJECT) {
process.env.GITLAB_DEFAULT_PROJECT = projectPath;
logger.debug({ project: projectPath }, "Set default project context");
}
if (!process.env.GITLAB_DEFAULT_NAMESPACE) {
// Use shared utility to extract namespace
const namespace = extractNamespaceFromPath(projectPath);
Eif (namespace) {
process.env.GITLAB_DEFAULT_NAMESPACE = namespace;
logger.debug({ namespace }, "Set default namespace context");
}
}
}
// ============================================================================
// Display Functions
// ============================================================================
/**
* Format auto-discovery result for display (dry-run mode)
*/
export function formatDiscoveryResult(result: AutoDiscoveryResult): string {
const lines: string[] = [];
lines.push("Auto-discovery Results");
lines.push("======================");
lines.push("");
// Git Remote
lines.push("Git Remote:");
lines.push(` Remote: ${result.remote.remoteName}`);
lines.push(` Host: ${result.host}`);
lines.push(` Project: ${result.projectPath}`);
lines.push(` Protocol: ${result.remote.protocol}`);
lines.push(` URL: ${result.remote.url}`);
lines.push("");
// Multiple remotes warning
if (result.availableRemotes.length > 1) {
lines.push("Available Remotes:");
for (const remote of result.availableRemotes) {
const selected = remote.remoteName === result.remote.remoteName ? " (selected)" : "";
lines.push(` ${remote.remoteName}: ${remote.host}/${remote.projectPath}${selected}`);
}
lines.push("");
}
// Profile Match
lines.push("Profile Match:");
if (result.matchedProfile) {
lines.push(` Profile: ${result.matchedProfile.profileName}`);
lines.push(` Match Type: ${result.matchedProfile.matchType}`);
if (result.matchedProfile.profile.authType) {
lines.push(` Auth: ${result.matchedProfile.profile.authType}`);
}
if (result.matchedProfile.profile.readOnly) {
lines.push(` Mode: read-only`);
}
} else {
lines.push(` No matching profile found`);
lines.push(` Will use: ${result.apiUrl} (from discovered host)`);
lines.push(` Auth: GITLAB_TOKEN environment variable required`);
}
lines.push("");
// Project Config
lines.push("Project Configuration:");
if (result.projectConfig) {
lines.push(` Path: ${result.projectConfig.configPath}`);
if (result.projectConfig.preset) {
lines.push(` Preset: ${result.projectConfig.preset.description ?? "custom restrictions"}`);
if (result.projectConfig.preset.scope) {
const scope = result.projectConfig.preset.scope;
if (scope.project) {
lines.push(` Scope: project "${scope.project}"`);
} else if (scope.namespace) {
lines.push(` Scope: namespace "${scope.namespace}/*"`);
} else if (scope.projects) {
lines.push(` Scope: ${scope.projects.length} projects`);
}
}
if (result.projectConfig.preset.read_only) {
lines.push(` Mode: read-only`);
}
}
if (result.projectConfig.profile) {
lines.push(
` Profile: ${result.projectConfig.profile.description ?? "custom tool selection"}`
);
if (result.projectConfig.profile.extends) {
lines.push(` Extends: ${result.projectConfig.profile.extends}`);
}
}
} else {
lines.push(` No .gitlab-mcp/ directory found`);
}
lines.push("");
// Default Context
lines.push("Default Context:");
lines.push(` Project: ${result.projectPath}`);
const namespace = extractNamespaceFromPath(result.projectPath) ?? result.projectPath;
lines.push(` Namespace: ${namespace}`);
return lines.join("\n");
}
|