Skip to content

Commit

Permalink
Process URL or file path as permission file source
Browse files Browse the repository at this point in the history
  • Loading branch information
millicentachieng committed Feb 24, 2023
1 parent f4e6671 commit 8d07c9f
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions ApiDoctor.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace ApiDoctor.ConsoleApp
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
Expand Down Expand Up @@ -2573,9 +2574,7 @@ private static async Task<bool> GeneratePermissionFilesAsync(GeneratePermissionF

var docSet = docs ?? await GetDocSetAsync(options, issues);
if (null == docSet)
{
return false;
}

// we only expect to have permission definitions in documents of ApiPageType
var docFiles = docSet.Files.Where(x => x.DocumentPageType == DocFile.PageType.ApiPageType);
Expand All @@ -2584,8 +2583,9 @@ private static async Task<bool> GeneratePermissionFilesAsync(GeneratePermissionF
var permissionsDocument = new PermissionsDocument();
if (docFiles.Any() && !options.BootstrappingOnly)
{
using var fileStream = new FileStream(options.PermissionsSourceFile, FileMode.Open);
permissionsDocument = PermissionsDocument.Load(fileStream);
permissionsDocument = await GetPermissionsDocumentAsync(options.PermissionsSourceFile);
if (permissionsDocument == null)
return false;
}

foreach (var docFile in docFiles)
Expand Down Expand Up @@ -2662,7 +2662,7 @@ private static async Task<bool> GeneratePermissionFilesAsync(GeneratePermissionF
break;
}

if (currentLine.Contains("```http"))
if (currentLine.Contains("```"))
{
foundHttpRequestBlocks++;
if (foundHttpRequestBlocks == foundPermissionTablesOrBlocks)
Expand Down Expand Up @@ -2704,7 +2704,7 @@ private static async Task<bool> GeneratePermissionFilesAsync(GeneratePermissionF
}

// update boilerplate text
if (!isBootstrapped && foundPermissionTablesOrBlocks == 1)
if (!isBootstrapped && boilerplateStartLine > -1 && foundPermissionTablesOrBlocks == 1)
{
originalFileContents[boilerplateStartLine] = "Choose the permission marked as least privileged for this API." +
" Use a higher privileged permission only if your app requires it. For details about delegated and application permissions," +
Expand Down Expand Up @@ -2764,6 +2764,31 @@ private static string GetPermissionsMarkdownTableForHttpRequestBlock(List<string
return null;
}

private static async Task<PermissionsDocument> GetPermissionsDocumentAsync(string filePathOrUrl)
{
var permissionsDocument = new PermissionsDocument();
try
{
if (Uri.IsWellFormedUriString(filePathOrUrl, UriKind.Absolute))
{
using var client = new HttpClient();
using var stream = await client.GetStreamAsync(filePathOrUrl);
permissionsDocument = PermissionsDocument.Load(stream);
}
else
{
using var fileStream = new FileStream(filePathOrUrl, FileMode.Open);
permissionsDocument = PermissionsDocument.Load(fileStream);
}
return permissionsDocument;
}
catch (Exception ex)
{
FancyConsole.WriteLine(FancyConsole.ConsoleErrorColor, $"Failed to parse permission source file {ex.Message}");
return null;
}
}

private enum PermissionsInsertionState
{
FindPermissionsHeader,
Expand Down

0 comments on commit 8d07c9f

Please sign in to comment.