| | | 1 | | // Copyright DotNet API Diff Project Contributors - SPDX Identifier: MIT |
| | | 2 | | using System.Text.RegularExpressions; |
| | | 3 | | using DotNetApiDiff.Interfaces; |
| | | 4 | | using DotNetApiDiff.Models; |
| | | 5 | | using DotNetApiDiff.Models.Configuration; |
| | | 6 | | using Microsoft.Extensions.Logging; |
| | | 7 | | |
| | | 8 | | namespace DotNetApiDiff.ApiExtraction; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Classifies API changes as breaking, non-breaking, or excluded based on configuration rules |
| | | 12 | | /// </summary> |
| | | 13 | | public class ChangeClassifier : IChangeClassifier |
| | | 14 | | { |
| | | 15 | | private readonly BreakingChangeRules _breakingChangeRules; |
| | | 16 | | private readonly ExclusionConfiguration _exclusionConfig; |
| | | 17 | | private readonly ILogger<ChangeClassifier> _logger; |
| | 23 | 18 | | private readonly Dictionary<string, Regex> _typePatternCache = new(); |
| | 23 | 19 | | private readonly Dictionary<string, Regex> _memberPatternCache = new(); |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Creates a new instance of the ChangeClassifier |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="breakingChangeRules">Rules for determining breaking changes</param> |
| | | 25 | | /// <param name="exclusionConfig">Configuration for exclusions</param> |
| | | 26 | | /// <param name="logger">Logger for diagnostic information</param> |
| | 23 | 27 | | public ChangeClassifier( |
| | 23 | 28 | | BreakingChangeRules breakingChangeRules, |
| | 23 | 29 | | ExclusionConfiguration exclusionConfig, |
| | 23 | 30 | | ILogger<ChangeClassifier> logger) |
| | 23 | 31 | | { |
| | 23 | 32 | | _breakingChangeRules = breakingChangeRules ?? throw new ArgumentNullException(nameof(breakingChangeRules)); |
| | 23 | 33 | | _exclusionConfig = exclusionConfig ?? throw new ArgumentNullException(nameof(exclusionConfig)); |
| | 23 | 34 | | _logger = logger ?? throw new ArgumentNullException(nameof(logger)); |
| | | 35 | | |
| | | 36 | | // Initialize regex pattern caches |
| | 23 | 37 | | InitializePatternCaches(); |
| | 23 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Classifies an API difference as breaking, non-breaking, or excluded |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="difference">The API difference to classify</param> |
| | | 44 | | /// <returns>The classified API difference with updated properties</returns> |
| | | 45 | | public ApiDifference ClassifyChange(ApiDifference difference) |
| | 9 | 46 | | { |
| | 9 | 47 | | if (difference == null) |
| | 1 | 48 | | { |
| | 1 | 49 | | throw new ArgumentNullException(nameof(difference)); |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | // Check if the element should be excluded |
| | 8 | 53 | | if (ShouldExcludeElement(difference)) |
| | 2 | 54 | | { |
| | 2 | 55 | | difference.ChangeType = ChangeType.Excluded; |
| | 2 | 56 | | difference.IsBreakingChange = false; |
| | 2 | 57 | | difference.Severity = SeverityLevel.Info; |
| | 2 | 58 | | difference.Description = $"Excluded {difference.ElementType}: {difference.ElementName}"; |
| | | 59 | | |
| | 2 | 60 | | _logger.LogDebug( |
| | 2 | 61 | | "Classified {ElementType} '{ElementName}' as excluded", |
| | 2 | 62 | | difference.ElementType, |
| | 2 | 63 | | difference.ElementName); |
| | | 64 | | |
| | 2 | 65 | | return difference; |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | // Classify based on change type and breaking change rules |
| | 6 | 69 | | switch (difference.ChangeType) |
| | | 70 | | { |
| | | 71 | | case ChangeType.Added: |
| | 2 | 72 | | ClassifyAddedChange(difference); |
| | 2 | 73 | | break; |
| | | 74 | | |
| | | 75 | | case ChangeType.Removed: |
| | 2 | 76 | | ClassifyRemovedChange(difference); |
| | 2 | 77 | | break; |
| | | 78 | | |
| | | 79 | | case ChangeType.Modified: |
| | 2 | 80 | | ClassifyModifiedChange(difference); |
| | 2 | 81 | | break; |
| | | 82 | | |
| | | 83 | | case ChangeType.Moved: |
| | 0 | 84 | | ClassifyMovedChange(difference); |
| | 0 | 85 | | break; |
| | | 86 | | } |
| | | 87 | | |
| | 6 | 88 | | _logger.LogDebug( |
| | 6 | 89 | | "Classified {ElementType} '{ElementName}' as {ChangeType}, Breaking: {IsBreaking}", |
| | 6 | 90 | | difference.ElementType, |
| | 6 | 91 | | difference.ElementName, |
| | 6 | 92 | | difference.ChangeType, |
| | 6 | 93 | | difference.IsBreakingChange); |
| | | 94 | | |
| | 6 | 95 | | return difference; |
| | 8 | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Determines if a type should be excluded from comparison |
| | | 100 | | /// </summary> |
| | | 101 | | /// <param name="typeName">The fully qualified type name</param> |
| | | 102 | | /// <returns>True if the type should be excluded, false otherwise</returns> |
| | | 103 | | public bool IsTypeExcluded(string typeName) |
| | 13 | 104 | | { |
| | 13 | 105 | | if (string.IsNullOrWhiteSpace(typeName)) |
| | 0 | 106 | | { |
| | 0 | 107 | | return false; |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | // Check exact matches first |
| | 13 | 111 | | if (_exclusionConfig.ExcludedTypes.Contains(typeName)) |
| | 3 | 112 | | { |
| | 3 | 113 | | _logger.LogDebug("Type '{TypeName}' excluded by exact match", typeName); |
| | 3 | 114 | | return true; |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | // Check pattern matches |
| | 32 | 118 | | foreach (var pattern in _typePatternCache.Keys) |
| | 2 | 119 | | { |
| | 2 | 120 | | if (_typePatternCache[pattern].IsMatch(typeName)) |
| | 2 | 121 | | { |
| | 2 | 122 | | _logger.LogDebug("Type '{TypeName}' excluded by pattern '{Pattern}'", typeName, pattern); |
| | 2 | 123 | | return true; |
| | | 124 | | } |
| | 0 | 125 | | } |
| | | 126 | | |
| | 8 | 127 | | return false; |
| | 13 | 128 | | } |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// Determines if a member should be excluded from comparison |
| | | 132 | | /// </summary> |
| | | 133 | | /// <param name="memberName">The fully qualified member name</param> |
| | | 134 | | /// <returns>True if the member should be excluded, false otherwise</returns> |
| | | 135 | | public bool IsMemberExcluded(string memberName) |
| | 6 | 136 | | { |
| | 6 | 137 | | if (string.IsNullOrWhiteSpace(memberName)) |
| | 0 | 138 | | { |
| | 0 | 139 | | return false; |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | // Check exact matches first |
| | 6 | 143 | | if (_exclusionConfig.ExcludedMembers.Contains(memberName)) |
| | 2 | 144 | | { |
| | 2 | 145 | | _logger.LogDebug("Member '{MemberName}' excluded by exact match", memberName); |
| | 2 | 146 | | return true; |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | // Check pattern matches |
| | 13 | 150 | | foreach (var pattern in _memberPatternCache.Keys) |
| | 1 | 151 | | { |
| | 1 | 152 | | if (_memberPatternCache[pattern].IsMatch(memberName)) |
| | 1 | 153 | | { |
| | 1 | 154 | | _logger.LogDebug("Member '{MemberName}' excluded by pattern '{Pattern}'", memberName, pattern); |
| | 1 | 155 | | return true; |
| | | 156 | | } |
| | 0 | 157 | | } |
| | | 158 | | |
| | | 159 | | // Check if the declaring type is excluded |
| | 3 | 160 | | int lastDotIndex = memberName.LastIndexOf('.'); |
| | 3 | 161 | | if (lastDotIndex > 0) |
| | 3 | 162 | | { |
| | 3 | 163 | | string declaringTypeName = memberName.Substring(0, lastDotIndex); |
| | 3 | 164 | | if (IsTypeExcluded(declaringTypeName)) |
| | 2 | 165 | | { |
| | 2 | 166 | | _logger.LogDebug("Member '{MemberName}' excluded because its declaring type is excluded", memberName); |
| | 2 | 167 | | return true; |
| | | 168 | | } |
| | 1 | 169 | | } |
| | | 170 | | |
| | 1 | 171 | | return false; |
| | 6 | 172 | | } |
| | | 173 | | |
| | | 174 | | /// <summary> |
| | | 175 | | /// Converts a wildcard pattern to a regular expression |
| | | 176 | | /// </summary> |
| | | 177 | | /// <param name="pattern">The wildcard pattern</param> |
| | | 178 | | /// <returns>A regular expression pattern</returns> |
| | | 179 | | private static string WildcardToRegex(string pattern) |
| | 3 | 180 | | { |
| | 3 | 181 | | return "^" + Regex.Escape(pattern) |
| | 3 | 182 | | .Replace("\\*", ".*") |
| | 3 | 183 | | .Replace("\\?", ".") + "$"; |
| | 3 | 184 | | } |
| | | 185 | | |
| | | 186 | | /// <summary> |
| | | 187 | | /// Initializes the regex pattern caches for type and member exclusion patterns |
| | | 188 | | /// </summary> |
| | | 189 | | private void InitializePatternCaches() |
| | 23 | 190 | | { |
| | | 191 | | // Convert type patterns to regex |
| | 73 | 192 | | foreach (var pattern in _exclusionConfig.ExcludedTypePatterns) |
| | 2 | 193 | | { |
| | | 194 | | try |
| | 2 | 195 | | { |
| | 2 | 196 | | var regex = new Regex(WildcardToRegex(pattern), RegexOptions.Compiled); |
| | 2 | 197 | | _typePatternCache[pattern] = regex; |
| | 2 | 198 | | } |
| | 0 | 199 | | catch (Exception ex) |
| | 0 | 200 | | { |
| | 0 | 201 | | _logger.LogWarning(ex, "Invalid type exclusion pattern: {Pattern}", pattern); |
| | 0 | 202 | | } |
| | 2 | 203 | | } |
| | | 204 | | |
| | | 205 | | // Convert member patterns to regex |
| | 71 | 206 | | foreach (var pattern in _exclusionConfig.ExcludedMemberPatterns) |
| | 1 | 207 | | { |
| | | 208 | | try |
| | 1 | 209 | | { |
| | 1 | 210 | | var regex = new Regex(WildcardToRegex(pattern), RegexOptions.Compiled); |
| | 1 | 211 | | _memberPatternCache[pattern] = regex; |
| | 1 | 212 | | } |
| | 0 | 213 | | catch (Exception ex) |
| | 0 | 214 | | { |
| | 0 | 215 | | _logger.LogWarning(ex, "Invalid member exclusion pattern: {Pattern}", pattern); |
| | 0 | 216 | | } |
| | 1 | 217 | | } |
| | | 218 | | |
| | 23 | 219 | | _logger.LogDebug( |
| | 23 | 220 | | "Initialized exclusion pattern caches with {TypePatternCount} type patterns and {MemberPatternCount} member |
| | 23 | 221 | | _typePatternCache.Count, |
| | 23 | 222 | | _memberPatternCache.Count); |
| | 23 | 223 | | } |
| | | 224 | | |
| | | 225 | | /// <summary> |
| | | 226 | | /// Determines if an API difference should be excluded based on configuration |
| | | 227 | | /// </summary> |
| | | 228 | | /// <param name="difference">The API difference to check</param> |
| | | 229 | | /// <returns>True if the difference should be excluded, false otherwise</returns> |
| | | 230 | | private bool ShouldExcludeElement(ApiDifference difference) |
| | 8 | 231 | | { |
| | | 232 | | // Check if the element is excluded by name |
| | 8 | 233 | | switch (difference.ElementType) |
| | | 234 | | { |
| | | 235 | | case ApiElementType.Type: |
| | 7 | 236 | | return IsTypeExcluded(difference.ElementName); |
| | | 237 | | |
| | | 238 | | case ApiElementType.Method: |
| | | 239 | | case ApiElementType.Property: |
| | | 240 | | case ApiElementType.Field: |
| | | 241 | | case ApiElementType.Event: |
| | | 242 | | case ApiElementType.Constructor: |
| | 1 | 243 | | return IsMemberExcluded(difference.ElementName); |
| | | 244 | | |
| | | 245 | | default: |
| | 0 | 246 | | return false; |
| | | 247 | | } |
| | 8 | 248 | | } |
| | | 249 | | |
| | | 250 | | /// <summary> |
| | | 251 | | /// Classifies an added change based on breaking change rules |
| | | 252 | | /// </summary> |
| | | 253 | | /// <param name="difference">The difference to classify</param> |
| | | 254 | | private void ClassifyAddedChange(ApiDifference difference) |
| | 2 | 255 | | { |
| | | 256 | | // By default, added changes are not breaking |
| | 2 | 257 | | difference.IsBreakingChange = false; |
| | 2 | 258 | | difference.Severity = SeverityLevel.Info; |
| | | 259 | | |
| | | 260 | | // Check specific rules for added changes |
| | 2 | 261 | | if (difference.ElementType == ApiElementType.Type && _breakingChangeRules.TreatAddedTypeAsBreaking) |
| | 1 | 262 | | { |
| | 1 | 263 | | difference.IsBreakingChange = true; |
| | 1 | 264 | | difference.Severity = SeverityLevel.Warning; |
| | 1 | 265 | | } |
| | 1 | 266 | | else if (difference.ElementType != ApiElementType.Type && _breakingChangeRules.TreatAddedMemberAsBreaking) |
| | 0 | 267 | | { |
| | 0 | 268 | | difference.IsBreakingChange = true; |
| | 0 | 269 | | difference.Severity = SeverityLevel.Warning; |
| | 0 | 270 | | } |
| | 2 | 271 | | } |
| | | 272 | | |
| | | 273 | | /// <summary> |
| | | 274 | | /// Classifies a removed change based on breaking change rules |
| | | 275 | | /// </summary> |
| | | 276 | | /// <param name="difference">The difference to classify</param> |
| | | 277 | | private void ClassifyRemovedChange(ApiDifference difference) |
| | 2 | 278 | | { |
| | | 279 | | // By default, removed changes are breaking |
| | 2 | 280 | | difference.IsBreakingChange = true; |
| | 2 | 281 | | difference.Severity = SeverityLevel.Error; |
| | | 282 | | |
| | | 283 | | // Check specific rules for removed changes |
| | 2 | 284 | | if (difference.ElementType == ApiElementType.Type && !_breakingChangeRules.TreatTypeRemovalAsBreaking) |
| | 1 | 285 | | { |
| | 1 | 286 | | difference.IsBreakingChange = false; |
| | 1 | 287 | | difference.Severity = SeverityLevel.Warning; |
| | 1 | 288 | | } |
| | 1 | 289 | | else if (difference.ElementType != ApiElementType.Type && !_breakingChangeRules.TreatMemberRemovalAsBreaking) |
| | 0 | 290 | | { |
| | 0 | 291 | | difference.IsBreakingChange = false; |
| | 0 | 292 | | difference.Severity = SeverityLevel.Warning; |
| | 0 | 293 | | } |
| | 2 | 294 | | } |
| | | 295 | | |
| | | 296 | | /// <summary> |
| | | 297 | | /// Classifies a modified change based on breaking change rules |
| | | 298 | | /// </summary> |
| | | 299 | | /// <param name="difference">The difference to classify</param> |
| | | 300 | | private void ClassifyModifiedChange(ApiDifference difference) |
| | 2 | 301 | | { |
| | | 302 | | // For modified changes, we need to analyze what changed |
| | | 303 | | // The DifferenceCalculator already set IsBreakingChange based on its analysis |
| | | 304 | | // Here we can refine that classification based on additional rules |
| | | 305 | | |
| | | 306 | | // If signature changed and we treat signature changes as breaking |
| | 2 | 307 | | if (difference.OldSignature != difference.NewSignature && _breakingChangeRules.TreatSignatureChangeAsBreaking) |
| | 1 | 308 | | { |
| | 1 | 309 | | difference.IsBreakingChange = true; |
| | 1 | 310 | | difference.Severity = SeverityLevel.Error; |
| | 1 | 311 | | } |
| | | 312 | | |
| | | 313 | | // If not already classified as breaking, keep the original classification |
| | 1 | 314 | | else if (!difference.IsBreakingChange) |
| | 1 | 315 | | { |
| | 1 | 316 | | difference.Severity = SeverityLevel.Info; |
| | 1 | 317 | | } |
| | 2 | 318 | | } |
| | | 319 | | |
| | | 320 | | /// <summary> |
| | | 321 | | /// Classifies a moved change based on breaking change rules |
| | | 322 | | /// </summary> |
| | | 323 | | /// <param name="difference">The difference to classify</param> |
| | | 324 | | private void ClassifyMovedChange(ApiDifference difference) |
| | 0 | 325 | | { |
| | | 326 | | // Moved changes are typically breaking unless configured otherwise |
| | 0 | 327 | | difference.IsBreakingChange = true; |
| | 0 | 328 | | difference.Severity = SeverityLevel.Warning; |
| | 0 | 329 | | } |
| | | 330 | | } |