From 80fddf544f173447cb54dd717df3844e52fbfd29 Mon Sep 17 00:00:00 2001 From: Millicent Achieng Date: Tue, 3 Dec 2024 14:19:40 +0300 Subject: [PATCH] Correctly extract permissions table when the HTML metadata spans more than one line --- ApiDoctor.Console/Constants.cs | 2 +- ApiDoctor.Console/Program.cs | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/ApiDoctor.Console/Constants.cs b/ApiDoctor.Console/Constants.cs index ba2a2a06..ded10eaa 100644 --- a/ApiDoctor.Console/Constants.cs +++ b/ApiDoctor.Console/Constants.cs @@ -12,7 +12,7 @@ public static class PermissionsConstants " For details about delegated and application permissions, see [Permission types](/graph/permissions-overview#permission-types). To learn more about these permissions, see the [permissions reference](/graph/permissions-reference)."; public const string MultipleTableBoilerPlateText = "The following tables show the least privileged permission or permissions required to call this API on each supported resource type." + " Follow [best practices](/graph/permissions-overview#best-practices-for-using-microsoft-graph-permissions) to request least privileged permissions." + - " For details about delegated and application permissions, see [Permission types](/graph/permissions-overview#permission-types). To learn more about these permissions, see theĀ [permissions reference](/graph/permissions-reference)."; + " For details about delegated and application permissions, see [Permission types](/graph/permissions-overview#permission-types). To learn more about these permissions, see the [permissions reference](/graph/permissions-reference)."; } public static readonly Regex FunctionParameterRegex = new(@"(?<=\=)[^)]+(?=\))", RegexOptions.Compiled, TimeSpan.FromSeconds(5)); public static readonly Regex QueryOptionSegementRegex = new(@"(\$.*)", RegexOptions.Compiled, TimeSpan.FromSeconds(5)); diff --git a/ApiDoctor.Console/Program.cs b/ApiDoctor.Console/Program.cs index d2501568..dc4d7051 100644 --- a/ApiDoctor.Console/Program.cs +++ b/ApiDoctor.Console/Program.cs @@ -2647,7 +2647,7 @@ private static async Task GeneratePermissionFilesAsync(GeneratePermissionF bool finishedParsing = false, isBootstrapped = false, ignorePermissionTableUpdate = false, foundAllPermissionTables = false, mergePermissions = false, hasBoilerplateText = false; int insertionStartLine = -1, insertionEndLine = -1, httpRequestStartLine = -1, httpRequestEndLine = -1, boilerplateStartLine = -1, - boilerplateEndLine = -1, permissionsHeaderIndex = -1, codeBlockAnnotationEndLine = -1, permissionsBlockLineCount = -1; + boilerplateEndLine = -1, permissionsHeaderIndex = -1, codeBlockAnnotationEndLine = -1, permissionsBlockLineCount = -1, permissionsTableStartLine = -1; string[] requestUrlsForPermissions = null; for (var currentIndex = 0; currentIndex < originalFileContents.Length && !finishedParsing; currentIndex++) { @@ -2662,8 +2662,8 @@ private static async Task GeneratePermissionFilesAsync(GeneratePermissionF } break; case PermissionsInsertionState.FindInsertionStartLine: - if (foundPermissionTablesOrBlocks == 0 && currentLine.Equals(Constants.PermissionsConstants.DefaultBoilerPlateText, StringComparison.OrdinalIgnoreCase) - || currentLine.Equals(Constants.PermissionsConstants.MultipleTableBoilerPlateText, StringComparison.OrdinalIgnoreCase)) + if (foundPermissionTablesOrBlocks == 0 && (currentLine.Equals(Constants.PermissionsConstants.DefaultBoilerPlateText, StringComparison.OrdinalIgnoreCase) + || currentLine.Equals(Constants.PermissionsConstants.MultipleTableBoilerPlateText, StringComparison.OrdinalIgnoreCase))) { hasBoilerplateText = true; boilerplateStartLine = boilerplateEndLine = currentIndex; @@ -2674,6 +2674,7 @@ private static async Task GeneratePermissionFilesAsync(GeneratePermissionF { isBootstrapped = true; foundPermissionTablesOrBlocks++; + permissionsTableStartLine = currentIndex; insertionEndLine = currentIndex; // [!INCLUDE [permissions-table]... is the end of the insertion block if (!options.BootstrappingOnly) @@ -2717,6 +2718,7 @@ private static async Task GeneratePermissionFilesAsync(GeneratePermissionF } else if (currentLine.Contains('|') && currentLine.Contains("Permission type", StringComparison.OrdinalIgnoreCase)) // found the permissions table { + permissionsTableStartLine = currentIndex; foundPermissionTablesOrBlocks++; var annotation = ExtractCodeBlockAnnotationForPermissionsTable( docFile.DisplayName, @@ -2848,7 +2850,7 @@ private static async Task GeneratePermissionFilesAsync(GeneratePermissionF var permissionFileContents = string.Empty; if (!isBootstrapped) { - var existingPermissionsTable = originalFileContents.Skip(insertionStartLine + 2).Take(insertionEndLine - insertionStartLine - 1); + var existingPermissionsTable = originalFileContents.Skip(permissionsTableStartLine).Take(insertionEndLine - permissionsTableStartLine + 1); permissionFileContents = $"{includeFileMetadata}{ConvertToThreeColumnPermissionsTable(existingPermissionsTable, docFile.DisplayName)}"; } @@ -2950,7 +2952,7 @@ private static async Task GeneratePermissionFilesAsync(GeneratePermissionF : insertionStartLine + permissionsBlockLineCount - 1; originalFileContents = newFileContents; insertionStartLine = insertionEndLine = httpRequestStartLine = httpRequestEndLine = - codeBlockAnnotationEndLine = permissionsBlockLineCount = -1; + codeBlockAnnotationEndLine = permissionsBlockLineCount = permissionsTableStartLine = -1; mergePermissions = false; requestUrlsForPermissions = null; foundHttpRequestBlocks = 0;