Skip to content

Commit

Permalink
✨ (TfsGitRepositoryTool): add validation and logging for TfsGitReposi…
Browse files Browse the repository at this point in the history
…toryTool options

Add a check to ensure the TfsGitRepositoryEnricher is enabled before proceeding, logging a warning if it is not. This prevents unnecessary operations when the feature is disabled. Introduce a default initialization for the Mappings dictionary to avoid null references. Implement a new options validator class, TfsGitRepositoryToolOptionsValidator, to ensure that Mappings is always initialized, enhancing robustness and preventing runtime errors. These changes improve the reliability and maintainability of the tool by ensuring proper configuration and logging.
  • Loading branch information
MrHinsh committed Oct 3, 2024
1 parent d589bdd commit a07a4bc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public int Enrich(TfsProcessor processor, WorkItemData sourceWorkItem, WorkItem
{
throw new ArgumentNullException(nameof(targetWorkItem));
}
if (!Options.Enabled)
{
Log.LogWarning("TfsGitRepositoryEnricher is not enabled! We will not fix any git commit links in Work items and they will be ignored.");
return 0;
}

Log.LogInformation("GitRepositoryEnricher: Enriching {Id} To fix Git Repo Links", targetWorkItem.Id);
var changeSetMappings = Services.GetService<TfsChangeSetMappingTool>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
using DotNet.Globbing;
using Microsoft.Extensions.Options;
using System.Text.RegularExpressions;
using Microsoft.TeamFoundation.Build.Client;
using MigrationTools.Enrichers;
using MigrationTools.Tools.Infrastructure;
Expand All @@ -12,6 +15,7 @@ public class TfsGitRepositoryToolOptions : ToolOptions
/// List of work item mappings.
/// </summary>
/// <default>{}</default>
public Dictionary<string, string> Mappings { get; set; }
public Dictionary<string, string> Mappings { get; set; } = new Dictionary<string, string>();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;

namespace MigrationTools.Tools
{
internal class TfsGitRepositoryToolOptionsValidator : IValidateOptions<TfsGitRepositoryToolOptions>
{
public ValidateOptionsResult Validate(string name, TfsGitRepositoryToolOptions options)
{
if (options.Mappings == null)
{
return ValidateOptionsResult.Fail("Mappings must be set to at least an empty array");
}
return ValidateOptionsResult.Success;
}
}
}

0 comments on commit a07a4bc

Please sign in to comment.