-
Notifications
You must be signed in to change notification settings - Fork 43
Add redirect URL and docfx file path checks #736
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2bcfdf3
add redirect URL and docfx file path checks
gewarren 8efb65e
add more tests
gewarren 26810f6
respond to feedback
gewarren 29ca5e2
more feedback
gewarren fe4b6f0
Apply batched suggestions from code review
gewarren ec5e4a7
respond to feedback
gewarren 9299da5
resolve merge conflict
gewarren 9b098ff
only verify fileMetadata paths
gewarren f898694
simplify verification logic
gewarren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net10.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| </Project> |
208 changes: 208 additions & 0 deletions
208
actions/docs-verifier/src/DocfxVerifier/PathVerifier.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,208 @@ | ||
| using System.Text.Json; | ||
|
|
||
| namespace DocfxVerifier | ||
| { | ||
| /// <summary> | ||
| /// Validates file path entries declared | ||
| /// under build.fileMetadata in a docfx.json file. | ||
| /// </summary> | ||
| public static class PathVerifier | ||
| { | ||
| private static readonly JsonDocumentOptions s_jsonDocumentOptions = new() | ||
| { | ||
| AllowTrailingCommas = true | ||
| }; | ||
|
|
||
| /// <summary> | ||
| /// Verifies that file paths in the docfx.json file are valid. | ||
| /// </summary> | ||
| public static Task<bool> WriteResultsAsync(TextWriter writer) => | ||
| WriteResultsAsync(writer, configurationPath: null); | ||
|
|
||
| /// <summary> | ||
| /// Verifies that file paths in a specific docfx.json file are valid. | ||
| /// </summary> | ||
| public static async Task<bool> WriteResultsAsync(TextWriter writer, string? configurationPath) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(writer, nameof(writer)); | ||
|
|
||
| configurationPath ??= FindDocfxConfigurationPath(); | ||
| if (configurationPath is null) | ||
| { | ||
| await writer.WriteLineAsync("::error::Unable to find docfx.json in the repository root or its immediate subdirectories."); | ||
| return false; | ||
| } | ||
|
|
||
| if (!File.Exists(configurationPath)) | ||
| { | ||
| await writer.WriteLineAsync($"::error::docfx.json file '{configurationPath}' does not exist."); | ||
| return false; | ||
| } | ||
|
|
||
| using FileStream stream = File.OpenRead(configurationPath); | ||
| using JsonDocument json = await JsonDocument.ParseAsync(stream, s_jsonDocumentOptions); | ||
|
|
||
| string repositoryRoot = Directory.GetCurrentDirectory(); | ||
| string configurationDirectory = Path.GetDirectoryName(Path.GetFullPath(configurationPath)) ?? repositoryRoot; | ||
| string configurationPathForLog = configurationPath.Replace('\\', '/'); | ||
|
|
||
| var errors = new List<string>(); | ||
| ValidateFileMetadataPaths(json.RootElement, repositoryRoot, configurationDirectory, errors); | ||
|
|
||
| foreach (string error in errors) | ||
| { | ||
| await writer.WriteLineAsync($"::error file={configurationPathForLog}::{error}"); | ||
| } | ||
|
|
||
| return errors.Count == 0; | ||
| } | ||
|
|
||
| private static void ValidateFileMetadataPaths( | ||
| JsonElement element, | ||
| string repositoryRoot, | ||
| string configurationDirectory, | ||
| List<string> errors) | ||
| { | ||
| if (element.ValueKind != JsonValueKind.Object) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (element.TryGetProperty("build", out JsonElement buildSection) | ||
| && buildSection.ValueKind == JsonValueKind.Object) | ||
| { | ||
| ValidateBuildFileMetadataSection(buildSection, "$.build", repositoryRoot, configurationDirectory, errors); | ||
| } | ||
| } | ||
|
|
||
| private static void ValidateBuildFileMetadataSection( | ||
| JsonElement buildSection, | ||
| string jsonPath, | ||
| string repositoryRoot, | ||
| string configurationDirectory, | ||
| List<string> errors) | ||
| { | ||
| if (buildSection.TryGetProperty("fileMetadata", out JsonElement fileMetadata) | ||
| && fileMetadata.ValueKind == JsonValueKind.Object) | ||
| { | ||
| foreach (JsonProperty metadataProperty in fileMetadata.EnumerateObject()) | ||
| { | ||
| if (metadataProperty.Value.ValueKind != JsonValueKind.Object) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| foreach (JsonProperty pathProperty in metadataProperty.Value.EnumerateObject()) | ||
| { | ||
| ValidatePath( | ||
| pathProperty.Name, | ||
| $"{jsonPath}.fileMetadata.{metadataProperty.Name}.{pathProperty.Name}", | ||
| repositoryRoot, | ||
| configurationDirectory, | ||
| errors); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static void ValidatePath( | ||
| string? path, | ||
| string jsonPath, | ||
| string repositoryRoot, | ||
| string resolutionBaseDirectory, | ||
| List<string> errors) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(path) || path is ".") | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (Uri.TryCreate(path, UriKind.Absolute, out Uri? uri) | ||
| && uri is not null | ||
| && (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps)) | ||
| { | ||
| return; | ||
|
gewarren marked this conversation as resolved.
|
||
| } | ||
|
|
||
| string normalizedPath = path.Replace('\\', '/'); | ||
| string nonWildcardPrefix = GetNonWildcardPrefix(normalizedPath); | ||
| if (string.IsNullOrEmpty(nonWildcardPrefix)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (!ExistsInRepository(nonWildcardPrefix, repositoryRoot, resolutionBaseDirectory)) | ||
| { | ||
| errors.Add($"{jsonPath}: Path '{path}' is invalid."); | ||
| } | ||
| } | ||
|
|
||
| private static bool ExistsInRepository(string path, string repositoryRoot, string resolutionBaseDirectory) | ||
| { | ||
| string? combinedPath = TryResolvePathWithinRepository(path, repositoryRoot, resolutionBaseDirectory); | ||
| return combinedPath is not null && (File.Exists(combinedPath) || Directory.Exists(combinedPath)); | ||
| } | ||
|
|
||
| private static string? TryResolvePathWithinRepository( | ||
| string? path, | ||
| string repositoryRoot, | ||
| string resolutionBaseDirectory) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(path)) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| string combinedPath = Path.GetFullPath(Path.Combine(resolutionBaseDirectory, path)); | ||
| string relative = Path.GetRelativePath(Path.GetFullPath(repositoryRoot), combinedPath); | ||
|
|
||
| if (relative.Equals("..", StringComparison.Ordinal) | ||
| || relative.StartsWith(".." + Path.DirectorySeparatorChar, StringComparison.Ordinal) | ||
| || relative.StartsWith(".." + Path.AltDirectorySeparatorChar, StringComparison.Ordinal)) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| return combinedPath; | ||
| } | ||
|
|
||
| private static string GetNonWildcardPrefix(string path) | ||
| { | ||
| ReadOnlySpan<char> wildcardChars = ['*', '?', '[', ']', '{', '}']; | ||
| string[] segments = path.Split('/', StringSplitOptions.RemoveEmptyEntries); | ||
|
|
||
| var prefixSegments = new List<string>(); | ||
| foreach (string segment in segments) | ||
| { | ||
| if (segment.AsSpan().IndexOfAny(wildcardChars) >= 0) | ||
| { | ||
| break; | ||
| } | ||
|
|
||
| prefixSegments.Add(segment); | ||
| } | ||
|
|
||
| return string.Join('/', prefixSegments); | ||
| } | ||
|
|
||
| private static string? FindDocfxConfigurationPath() | ||
| { | ||
| const string fileName = "docfx.json"; | ||
| if (File.Exists(fileName)) | ||
| { | ||
| return fileName; | ||
| } | ||
|
|
||
| foreach (string directory in Directory.GetDirectories(".", "*", SearchOption.TopDirectoryOnly)) | ||
| { | ||
| string candidate = Path.Combine(directory, fileName); | ||
| if (File.Exists(candidate)) | ||
| { | ||
| return candidate; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
| } | ||
3 changes: 3 additions & 0 deletions
3
actions/docs-verifier/src/RedirectionVerifier/Properties/AssemblyInfo.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| using System.Runtime.CompilerServices; | ||
|
|
||
| [assembly: InternalsVisibleTo("GitHub.UnitTests")] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.