All files / src/discovery profile-matcher.ts

100% Statements 23/23
100% Branches 8/8
100% Functions 4/4
100% Lines 22/22

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            4x 4x                                                           4x       14x       14x 18x       14x 13x 13x 7x       7x                   7x 5x 5x 2x       2x               5x 5x                   4x       2x 2x 2x    
/**
 * Profile matcher for auto-discovery
 *
 * Matches GitLab host from git remote to user-defined profiles.
 */
 
import { ProfileLoader, ProfileInfo } from "../profiles";
import { logger } from "../logger";
 
// ============================================================================
// Types
// ============================================================================
 
export interface ProfileMatchResult {
  /** Matched profile name */
  profileName: string;
  /** Profile info */
  profile: ProfileInfo;
  /** How the match was made */
  matchType: "exact" | "subdomain";
}
 
// ============================================================================
// Matching Logic
// ============================================================================
 
/**
 * Match a host to a user-defined profile
 *
 * Match priority:
 * 1. Exact host match (gitlab.company.com === gitlab.company.com)
 * 2. Subdomain match (gitlab.company.com matches profile with host company.com)
 *
 * @param host Host to match
 * @param profiles Available profiles
 * @returns Match result or null
 */
export function matchProfileByHost(
  host: string,
  profiles: ProfileInfo[]
): ProfileMatchResult | null {
  const normalizedHost = host.toLowerCase();
 
  // Only consider user profiles (with host defined)
  // Type guard ensures profile.host is string (not undefined)
  const userProfiles = profiles.filter(
    (p): p is ProfileInfo & { host: string } => typeof p.host === "string" && !p.isPreset
  );
 
  // 1. Try exact match first
  for (const profile of userProfiles) {
    const profileHost = profile.host.toLowerCase();
    if (normalizedHost === profileHost) {
      logger.debug(
        { host, profile: profile.name, matchType: "exact" },
        "Matched profile by exact host"
      );
      return {
        profileName: profile.name,
        profile,
        matchType: "exact",
      };
    }
  }
 
  // 2. Try subdomain match
  // e.g., git.company.com should match profile with host company.com
  for (const profile of userProfiles) {
    const profileHost = profile.host.toLowerCase();
    if (normalizedHost.endsWith(`.${profileHost}`)) {
      logger.debug(
        { host, profile: profile.name, matchType: "subdomain" },
        "Matched profile by subdomain"
      );
      return {
        profileName: profile.name,
        profile,
        matchType: "subdomain",
      };
    }
  }
 
  logger.debug({ host, availableHosts: userProfiles.map(p => p.host) }, "No profile match found");
  return null;
}
 
/**
 * Find a profile matching the given host using ProfileLoader
 *
 * @param host Host to match
 * @param loader Profile loader (optional, creates default if not provided)
 * @returns Match result or null
 */
export async function findProfileByHost(
  host: string,
  loader?: ProfileLoader
): Promise<ProfileMatchResult | null> {
  const profileLoader = loader ?? new ProfileLoader();
  const profiles = await profileLoader.listProfiles();
  return matchProfileByHost(host, profiles);
}