Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert permissions table to three column table #232

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 67 additions & 35 deletions ApiDoctor.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2785,7 +2785,7 @@ private static async Task<bool> GeneratePermissionFilesAsync(GeneratePermissionF
break;
case PermissionsInsertionState.InsertPermissionBlock:
var permissionFileContents = !isBootstrapped
? string.Join("\r\n", originalFileContents.Skip(insertionStartLine).Take(insertionEndLine + 1 - insertionStartLine))
? ConvertToThreeColumnPermissionsTable(originalFileContents.Skip(insertionStartLine + 2).Take(insertionEndLine - insertionStartLine - 1))
: string.Empty;

if (!options.BootstrappingOnly)
Expand All @@ -2799,7 +2799,6 @@ private static async Task<bool> GeneratePermissionFilesAsync(GeneratePermissionF
var httpRequests = new List<string>(originalFileContents.Skip(httpRequestStartLine + 1).Take(httpRequestEndLine - httpRequestStartLine - 1));
FancyConsole.WriteLine($"Fetching permissions table ({foundPermissionTablesOrBlocks}) for {docFile.DisplayName}");
var newPermissionFileContents = GetPermissionsMarkdownTableForHttpRequestBlock(httpRequests, permissionsDocument); // get from Kibali

if (!string.IsNullOrWhiteSpace(newPermissionFileContents))
{
permissionFileContents = $"---\r\ndescription: \"Automatically generated file. DO NOT MODIFY\"\r\nms.topic: include\r\nms.localizationpriority: medium\r\n---\r\n\r\n{newPermissionFileContents}";
Expand All @@ -2808,49 +2807,47 @@ private static async Task<bool> GeneratePermissionFilesAsync(GeneratePermissionF
else
{
FancyConsole.WriteLine($"No permissions data found for '{httpRequests.FirstOrDefault()}' in {docFile.DisplayName}");
parseStatus = PermissionsInsertionState.FindNextPermissionBlock;
break;
}
}

if (string.IsNullOrWhiteSpace(permissionFileContents))
{
parseStatus = PermissionsInsertionState.FindNextPermissionBlock;
break;
}

// update boilerplate text
if (!isBootstrapped && boilerplateStartLine > -1 && !string.IsNullOrWhiteSpace(newPermissionFileContents))
// update boilerplate text
if (!isBootstrapped && boilerplateStartLine > -1)
{
if (foundPermissionTablesOrBlocks == 1)
{
if (foundPermissionTablesOrBlocks == 1)
// We do not have a boilerplate text in this case, add new next line
if(boilerplateStartLine == permissionsHeaderIndex)
{
// We do not have a boilerplate text in this case, add new next line
if(boilerplateStartLine == permissionsHeaderIndex)
// insert a new line to hold boilerplate text
originalFileContents = FileSplicer(originalFileContents, boilerplateStartLine, Constants.PermissionConstants.DefaultBoilerPlateText).ToArray();
boilerplateStartLine++;
insertionStartLine++;
insertionEndLine++;
}
else {
if (boilerplateEndLine > boilerplateStartLine)
{
// insert a new line to hold boilerplate text
originalFileContents = FileSplicer(originalFileContents, boilerplateStartLine, Constants.PermissionConstants.DefaultBoilerPlateText).ToArray();
boilerplateStartLine++;
insertionStartLine++;
insertionEndLine++;
}
else {
if (boilerplateEndLine > boilerplateStartLine)
{
int extraLinesToRemove = boilerplateEndLine - boilerplateStartLine;
originalFileContents = originalFileContents.Splice(boilerplateStartLine + 1, extraLinesToRemove).ToArray();
insertionStartLine -= extraLinesToRemove;
insertionEndLine -= extraLinesToRemove;
}
originalFileContents[boilerplateStartLine] = Constants.PermissionConstants.DefaultBoilerPlateText;
int extraLinesToRemove = boilerplateEndLine - boilerplateStartLine;
originalFileContents = originalFileContents.Splice(boilerplateStartLine + 1, extraLinesToRemove).ToArray();
insertionStartLine -= extraLinesToRemove;
insertionEndLine -= extraLinesToRemove;
}
}
else if (foundPermissionTablesOrBlocks == 2) {
originalFileContents[boilerplateStartLine] = Constants.PermissionConstants.MultipleTableBoilerPlateText;
originalFileContents[boilerplateStartLine] = Constants.PermissionConstants.DefaultBoilerPlateText;
}
}

if (!isBootstrapped && !string.IsNullOrWhiteSpace(newPermissionFileContents) && boilerplateStartLine == -1)
FancyConsole.WriteLine(ConsoleColor.Yellow, $"Permissions boilerplate text for {docFile.DisplayName} was not updated");
else if (foundPermissionTablesOrBlocks == 2) {
originalFileContents[boilerplateStartLine] = Constants.PermissionConstants.MultipleTableBoilerPlateText;
}
}

if (string.IsNullOrWhiteSpace(permissionFileContents))
{
parseStatus = PermissionsInsertionState.FindNextPermissionBlock;
break;
}
if (!isBootstrapped && boilerplateStartLine == -1)
FancyConsole.WriteLine(ConsoleColor.Yellow, $"Permissions boilerplate text for {docFile.DisplayName} was not updated");

// create folder and file names
var permissionsFileRelativePath = Path.Combine("includes", "permissions");
Expand Down Expand Up @@ -2896,6 +2893,41 @@ private static async Task<bool> GeneratePermissionFilesAsync(GeneratePermissionF
return true;
}

private static List<string> PermissionKeywordsToIgnore = new List<string>()
millicentachieng marked this conversation as resolved.
Show resolved Hide resolved
{
"None",
"None.",
"Not supported.",
"Not supported",
"Not available.",
"Not available",
"Not applicable",
"Not applicable."
};

private static string ConvertToThreeColumnPermissionsTable(IEnumerable<string> tableRows)
{
var tableString = "|Permission type|Least privileged permissions|Higher privileged permissions|" +
millicentachieng marked this conversation as resolved.
Show resolved Hide resolved
"\r\n|:---|:---|:---|";
foreach (string row in tableRows)
{
string[] cells = Regex.Split(row.Trim(), @"\s*\|\s*").Where(x => !string.IsNullOrWhiteSpace(x)).ToArray();
millicentachieng marked this conversation as resolved.
Show resolved Hide resolved
var allPermissions = cells[1].Trim().Split(',', StringSplitOptions.TrimEntries)
.Where(x => !string.IsNullOrWhiteSpace(x) && !PermissionKeywordsToIgnore.Contains(x, StringComparer.OrdinalIgnoreCase))
millicentachieng marked this conversation as resolved.
Show resolved Hide resolved
.ToList();

var permissionType = cells[0];
var leastPrivilegePermission = allPermissions.Any() ? allPermissions.First().Trim() : "Not supported.";
var higherPrivilegePermissions = !allPermissions.Any()
? "Not supported."
: allPermissions.Count() == 1
? "Not available."
: string.Join(", ", allPermissions.Skip(1).Select(x => x.Trim()).ToList());
tableString += $"\r\n|{permissionType}|{leastPrivilegePermission}|{higherPrivilegePermissions}|";
}
return tableString;
}

private static string GetPermissionsMarkdownTableForHttpRequestBlock(List<string> httpRequests, PermissionsDocument permissionsDoc)
{
// use the first HTTP Request, we are assuming the group of URLs will have the same set of permissions
Expand Down
Loading