All files / src/adapters TypeMapper.ts

98.71% Statements 77/78
88.15% Branches 67/76
92.85% Functions 13/14
100% Lines 71/71

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                                                                              4x               4x                                                             10x   9x 8x 6x 5x     2x   2x               7x   1x   2x   1x   1x               2x                     4x                           4x                           4x                         22x 22x 22x   22x 11x 11x 9x       2x 2x                         11x     11x 2x 2x       9x 2x 2x 1x 1x   1x       7x 2x 2x 1x 1x   1x       5x 5x   5x                 9x 9x   9x     9x 2x 2x     7x 7x     7x 1x     7x             6x 6x     6x 5x     6x             8x             7x             15x                                           15x 15x             4x 17x   12x 10x        
import { WorkItemType, Provider, AzureProcess } from '../types/index.js';
 
/**
 * Intelligent type mapping between different work item taxonomies
 * Based on the mapping rules defined in PROVIDERS.md
 */
 
export interface UnifiedLabels {
  has(label: string): boolean;
  all: string[];
}
 
export interface GitLabTypeHints {
  glType?: 'epic' | 'issue' | 'task' | 'incident' | 'bug' | 'test_case';
  isGroupEpic?: boolean;
  childCount?: number;
}
 
export interface GitHubTypeHints {
  ghType?: 'bug' | 'enhancement' | 'task' | 'epic';
  labels: UnifiedLabels;
  childCount?: number;
}
 
export interface MappingInput {
  provider: Provider;
  process?: AzureProcess; // For Azure DevOps
  title: string;
  description?: string;
  gitlab?: GitLabTypeHints;
  github?: GitHubTypeHints;
}
 
export interface MappingResult {
  targetType: string; // Platform-specific type
  addTags: string[];
  rationale: string[];
}
 
export class TypeMapper {
  private constructor() {
    // Utility class - prevent instantiation
  }
 
  /**
   * GitLab → Azure DevOps type mapping
   */
  private static gitlabToAzureTypeMap = {
    agile: {
      epic: 'Epic',
      issue: 'User Story',
      task: 'Task',
      incident: 'Bug',
      bug: 'Bug',
      test_case: 'Test Case',
    },
    scrum: {
      epic: 'Epic',
      issue: 'Product Backlog Item',
      task: 'Task',
      incident: 'Bug',
      bug: 'Bug',
      test_case: 'Test Case',
    },
    basic: {
      epic: 'Epic',
      issue: 'Issue',
      task: 'Task',
      incident: 'Issue', // + tag: incident
      bug: 'Issue', // + tag: bug
      test_case: 'Task', // + tag: test-case
    },
  };
 
  /**
   * GitHub → Azure DevOps type mapping with detection logic
   */
  private static detectGitHubType(labels: string[], body?: string, childCount = 0): string {
    const labelSet = new Set(labels.map((l) => l.toLowerCase()));
 
    if (labelSet.has('epic')) return 'epic';
    if (labelSet.has('bug')) return 'bug';
    if (labelSet.has('task')) return 'task';
    if (labelSet.has('enhancement')) return 'enhancement';
 
    // Check for parent-child pattern (epic simulation)
    Iif (body?.includes('- [ ]') && childCount > 3) return 'epic';
 
    return 'issue';
  }
 
  private static mapGitHubToAzure(
    detectedType: string,
    process: AzureProcess,
    childCount = 0,
  ): string {
    switch (detectedType) {
      case 'epic':
        return 'Epic';
      case 'bug':
        return process === 'basic' ? 'Issue' : 'Bug';
      case 'task':
        return 'Task';
      case 'enhancement':
        return childCount >= 2
          ? 'Feature'
          : process === 'scrum'
            ? 'Product Backlog Item'
            : process === 'basic'
              ? 'Issue'
              : 'User Story';
      default:
        return process === 'scrum'
          ? 'Product Backlog Item'
          : process === 'basic'
            ? 'Issue'
            : 'User Story';
    }
  }
 
  /**
   * Azure DevOps → GitLab type mapping
   */
  private static azureToGitlabTypeMap: Record<string, string> = {
    Epic: 'epic',
    Feature: 'issue',
    'User Story': 'issue',
    'Product Backlog Item': 'issue',
    Issue: 'issue',
    Task: 'task',
    Bug: 'incident',
    'Test Case': 'test_case',
  };
 
  /**
   * Azure DevOps → GitHub type mapping
   */
  private static azureToGitHubTypeMap: Record<string, string[]> = {
    Epic: ['epic'],
    Feature: ['enhancement', 'feature'],
    'User Story': ['story'],
    'Product Backlog Item': ['story'],
    Issue: [], // No specific label
    Task: ['task'],
    Bug: ['bug'],
    'Test Case': ['test'],
  };
 
  /**
   * GitLab → GitHub type mapping
   */
  private static gitlabToGitHubTypeMap: Record<string, string[]> = {
    epic: ['epic'],
    issue: [],
    task: ['task'],
    incident: ['bug', 'incident'],
    bug: ['bug'],
    test_case: ['test'],
  };
 
  /**
   * Main type mapping function
   */
  static mapType(input: MappingInput): MappingResult {
    const { provider, process = 'agile' } = input;
    const rationale: string[] = [];
    const addTags: string[] = [];
 
    if (provider === 'gitlab' && input.gitlab) {
      return this.mapGitLabType(input.gitlab, process, rationale, addTags);
    } else if (provider === 'github' && input.github) {
      return this.mapGitHubType(input.github, process, rationale, addTags);
    }
 
    // Default fallback
    rationale.push('Unknown provider or missing type hints; using default mapping');
    return {
      targetType: 'Issue',
      addTags,
      rationale,
    };
  }
 
  private static mapGitLabType(
    gitlab: GitLabTypeHints,
    process: AzureProcess,
    rationale: string[],
    addTags: string[],
  ): MappingResult {
    const glType = gitlab.glType ?? 'issue';
 
    // Handle group-level epics
    if (glType === 'epic' || gitlab.isGroupEpic) {
      rationale.push('GitLab type is epic (group-level)');
      return { targetType: 'Epic', addTags, rationale };
    }
 
    // Handle incidents with special tagging for Basic process
    if (glType === 'incident') {
      rationale.push('GitLab type is incident → map to Bug; preserve tag `incident`');
      if (process === 'basic') {
        addTags.push('incident');
        return { targetType: 'Issue', addTags, rationale };
      }
      return { targetType: 'Bug', addTags, rationale };
    }
 
    // Handle test cases
    if (glType === 'test_case') {
      rationale.push('GitLab type is test_case');
      if (process === 'basic') {
        addTags.push('test-case');
        return { targetType: 'Task', addTags, rationale };
      }
      return { targetType: 'Test Case', addTags, rationale };
    }
 
    // Standard mapping
    const mapped = this.gitlabToAzureTypeMap[process][glType] || 'Issue';
    rationale.push(`GitLab ${glType} → Azure ${mapped} (${process} process)`);
 
    return { targetType: mapped, addTags, rationale };
  }
 
  private static mapGitHubType(
    github: GitHubTypeHints,
    process: AzureProcess,
    rationale: string[],
    addTags: string[],
  ): MappingResult {
    const labels = github.labels.all;
    const detectedType = this.detectGitHubType(labels, '', github.childCount);
 
    rationale.push(`Detected GitHub type: ${detectedType} from labels: [${labels.join(', ')}]`);
 
    // Handle enhancement with child count heuristic
    if (detectedType === 'enhancement' && (github.childCount ?? 0) >= 2) {
      rationale.push(`Enhancement with ${github.childCount} children → Feature`);
      return { targetType: 'Feature', addTags, rationale };
    }
 
    const mapped = this.mapGitHubToAzure(detectedType, process, github.childCount);
    rationale.push(`GitHub ${detectedType} → Azure ${mapped} (${process} process)`);
 
    // Preserve enhancement label
    if (detectedType === 'enhancement' && mapped !== 'Feature') {
      addTags.push('enhancement');
    }
 
    return { targetType: mapped, addTags, rationale };
  }
 
  /**
   * Reverse mapping: Azure → GitLab
   */
  static mapAzureToGitLab(azureType: string): { type: string; tags: string[] } {
    const type = this.azureToGitlabTypeMap[azureType] || 'issue';
    const tags: string[] = [];
 
    // Preserve original type information
    if (azureType !== 'Issue') {
      tags.push(`azure-${azureType.toLowerCase().replace(' ', '-')}`);
    }
 
    return { type, tags };
  }
 
  /**
   * Reverse mapping: Azure → GitHub
   */
  static mapAzureToGitHub(azureType: string): string[] {
    return this.azureToGitHubTypeMap[azureType] ?? [];
  }
 
  /**
   * Reverse mapping: GitLab → GitHub
   */
  static mapGitLabToGitHub(gitlabType: string): string[] {
    return this.gitlabToGitHubTypeMap[gitlabType] ?? [];
  }
 
  /**
   * Normalize type to common WorkItemType
   */
  static normalizeType(type: string): WorkItemType {
    const typeMap: Partial<Record<string, WorkItemType>> = {
      // Common types
      epic: 'epic',
      task: 'task',
      bug: 'bug',
      issue: 'issue',
      story: 'story',
      feature: 'feature',
 
      // GitLab specific
      incident: 'bug',
      test_case: 'test',
 
      // GitHub specific
      enhancement: 'story',
 
      // Azure DevOps specific
      'user story': 'story',
      'product backlog item': 'story',
      'test case': 'test',
    };
 
    const normalized = typeMap[type.toLowerCase()];
    return normalized ?? 'issue';
  }
}
 
/**
 * Helper to create UnifiedLabels from string array
 */
export function createUnifiedLabels(labels: string[]): UnifiedLabels {
  const labelSet = new Set(labels.map((l) => l.toLowerCase()));
 
  return {
    has: (label: string) => labelSet.has(label.toLowerCase()),
    all: labels,
  };
}