From 7438e58364b04d248eeab133ef929bbfa2517bcf Mon Sep 17 00:00:00 2001 From: "pavel.butuzov" Date: Fri, 25 Oct 2024 10:37:42 +0300 Subject: [PATCH] TMS-28931 and TMS-28908: Fixed the parsing of format characters for QaseExporter. --- .../QaseExporter/Services/StepService.cs | 5 +- .../QaseExporter/Services/TestCaseService.cs | 10 +- Migrators/QaseExporter/Services/Utils.cs | 134 +++++++++++------- 3 files changed, 96 insertions(+), 53 deletions(-) diff --git a/Migrators/QaseExporter/Services/StepService.cs b/Migrators/QaseExporter/Services/StepService.cs index 85ec8ef..5f6e316 100644 --- a/Migrators/QaseExporter/Services/StepService.cs +++ b/Migrators/QaseExporter/Services/StepService.cs @@ -136,10 +136,13 @@ private static string ConvertingStepDescription(string description) { return Utils.RemoveBackslashCharacters( + Utils.ConvertingAngleBracketsStr( + Utils.ConvertingToggleStrikethroughStr( Utils.ConvertingCodeStr( Utils.ConvertingBlockCodeStr( Utils.ConvertingToggleStrongStr( Utils.ConvertingHyperlinks( - Utils.ConvertingFormatCharactersWithBlockCodeStr(description)))))); + Utils.ConvertingFormatCharacters( + Utils.ConvertingFormatCharactersWithBlockCodeStr(description))))))))); } } diff --git a/Migrators/QaseExporter/Services/TestCaseService.cs b/Migrators/QaseExporter/Services/TestCaseService.cs index 3eacc03..9f7438c 100644 --- a/Migrators/QaseExporter/Services/TestCaseService.cs +++ b/Migrators/QaseExporter/Services/TestCaseService.cs @@ -98,7 +98,7 @@ private async Task> ConvertTestCases(List qaseTestC new TestCase { Id = testCaseId, - Description = qaseTestCase.Description, + Description = ConvertingDescription(qaseTestCase.Description), State = ConvertStatus(qaseTestCase.Status), Priority = ConvertPriority(qaseTestCase.Priority), Steps = steps, @@ -318,4 +318,12 @@ private static StateType ConvertStatus(int status) _ => StateType.NotReady }; } + + private static string ConvertingDescription(string description) + { + return + Utils.RemoveBackslashCharacters( + Utils.RemoveToggleStrongCharacters( + Utils.RemoveToggleStrikethroughCharacters(description))); + } } diff --git a/Migrators/QaseExporter/Services/Utils.cs b/Migrators/QaseExporter/Services/Utils.cs index 6abf6f7..d7ec5ed 100644 --- a/Migrators/QaseExporter/Services/Utils.cs +++ b/Migrators/QaseExporter/Services/Utils.cs @@ -10,32 +10,26 @@ public static class Utils private const string HyperlinkPattern = @"\[[^\[\]]*\]\([^()\s]*\)"; private const string titlePattern = @"\[([^\[\]]+)\]"; private const string ToggleStrongStrPattern = @"\*\*(.*?)\*\*"; // Match: "**{anything}**" + private const string ToggleStrikethroughStrPattern = @"\~\~(.*?)\~\~"; // Match: "~~{anything}~~" private const string BlockCodeStrPattern = @"\`{3}([\s\S]*?)\`{3}"; // Match: "```{anything}```" private const string CodeStrPattern = @"(?() - }; - } - var data = new QaseDescriptionData { Description = description, Attachments = new List() }; - var imgRegex = new Regex(ImgPattern); - - var matches = imgRegex.Matches(description); + var matches = GetAllMatchesByPattern(description, ImgPattern); if (matches.Count == 0) { @@ -65,14 +59,7 @@ public static QaseDescriptionData ExtractAttachments(string? description) public static string ConvertingHyperlinks(string? description) { - if (string.IsNullOrEmpty(description)) - { - return string.Empty; - } - - var hyperlinkRegex = new Regex(HyperlinkPattern); - - var matches = hyperlinkRegex.Matches(description); + var matches = GetAllMatchesByPattern(description, HyperlinkPattern); if (matches.Count == 0) { @@ -100,14 +87,7 @@ public static string ConvertingHyperlinks(string? description) public static string ConvertingToggleStrongStr(string? description) { - if (string.IsNullOrEmpty(description)) - { - return string.Empty; - } - - var toggleStrongStrRegex = new Regex(ToggleStrongStrPattern); - - var matches = toggleStrongStrRegex.Matches(description); + var matches = GetAllMatchesByPattern(description, ToggleStrongStrPattern); if (matches.Count == 0) { @@ -116,23 +96,34 @@ public static string ConvertingToggleStrongStr(string? description) foreach (Match match in matches) { - var matchWithoutToggleStrongFormat = match.Value.Replace("**", ""); + var matchWithoutToggleStrongFormat = RemoveToggleStrongCharacters(match.Value); description = description.Replace(match.Value, $"{matchWithoutToggleStrongFormat}"); } return description; } - public static string ConvertingBlockCodeStr(string? description) + public static string ConvertingToggleStrikethroughStr(string? description) { - if (string.IsNullOrEmpty(description)) + var matches = GetAllMatchesByPattern(description, ToggleStrikethroughStrPattern); + + if (matches.Count == 0) { - return string.Empty; + return description; } - var blockCodeStrRegex = new Regex(BlockCodeStrPattern); + foreach (Match match in matches) + { + var matchWithoutToggleStrikethroughFormat = RemoveToggleStrikethroughCharacters(match.Value); + description = description.Replace(match.Value, $"{matchWithoutToggleStrikethroughFormat}"); + } - var matches = blockCodeStrRegex.Matches(description); + return description; + } + + public static string ConvertingBlockCodeStr(string? description) + { + var matches = GetAllMatchesByPattern(description, BlockCodeStrPattern); if (matches.Count == 0) { @@ -141,7 +132,7 @@ public static string ConvertingBlockCodeStr(string? description) foreach (Match match in matches) { - var matchWithoutBlockCodeFormat = match.Value.Replace("```", ""); + var matchWithoutBlockCodeFormat = RemoveCodeCharacters(match.Value); description = description.Replace(match.Value, $"
{matchWithoutBlockCodeFormat}
"); } @@ -150,14 +141,7 @@ public static string ConvertingBlockCodeStr(string? description) public static string ConvertingCodeStr(string? description) { - if (string.IsNullOrEmpty(description)) - { - return string.Empty; - } - - var codeStrRegex = new Regex(CodeStrPattern); - - var matches = codeStrRegex.Matches(description); + var matches = GetAllMatchesByPattern(description, CodeStrPattern); if (matches.Count == 0) { @@ -166,7 +150,7 @@ public static string ConvertingCodeStr(string? description) foreach (Match match in matches) { - var matchWithoutCodeFormat = match.Value[1..^1]; + var matchWithoutCodeFormat = RemoveCodeCharacters(match.Value); description = description.Replace(match.Value, $"{matchWithoutCodeFormat}"); } @@ -175,14 +159,13 @@ public static string ConvertingCodeStr(string? description) public static string ConvertingFormatCharactersWithBlockCodeStr(string? description) { - if (string.IsNullOrEmpty(description)) + var matches = GetAllMatchesByPattern(description, BlockCodeStrPattern); + + if (matches.Count == 0) { - return string.Empty; + return description; } - var blockCodeStrRegex = new Regex(BlockCodeStrPattern); - - var matches = blockCodeStrRegex.Matches(description); var remainingDescription = description; var convertedDescription = string.Empty; @@ -215,15 +198,64 @@ public static string ConvertingFormatCharacters(string? description) return description; } + public static string ConvertingAngleBracketsStr(string? description) + { + var matches = GetAllMatchesByPattern(description, AngleBracketCharacterPattern); + + if (matches.Count == 0) + { + return description; + } + + foreach (Match match in matches) + { + description = description.Replace(match.Value, "<"); + } + + return description; + } + + private static List GetAllMatchesByPattern(string? description, string pattern) + { + if (string.IsNullOrEmpty(description)) + { + return new List(); + } + + var regex = new Regex(pattern); + + return regex.Matches(description).ToList(); + } + public static string RemoveBackslashCharacters(string? description) + { + return RemoveCharactersFromDescriptionByPattern(description, BackslashCharacterPattern); + } + + public static string RemoveToggleStrongCharacters(string? description) + { + return RemoveCharactersFromDescriptionByPattern(description, ToggleStrongCharacterPattern); + } + + public static string RemoveToggleStrikethroughCharacters(string? description) + { + return RemoveCharactersFromDescriptionByPattern(description, ToggleStrikethroughCharacterPattern); + } + + public static string RemoveCodeCharacters(string? description) + { + return RemoveCharactersFromDescriptionByPattern(description, CodeCharacterPattern); + } + + private static string RemoveCharactersFromDescriptionByPattern(string? description, string pattern) { if (string.IsNullOrEmpty(description)) { return string.Empty; } - var backslashCharacterRegex = new Regex(BackslashCharacterPattern); + var regex = new Regex(pattern); - return backslashCharacterRegex.Replace(description, ""); + return regex.Replace(description, ""); } }