Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 16 additions & 2 deletions actions/docs-verifier/MSDocsBuildVerifier.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33402.96
# Visual Studio Version 18
VisualStudioVersion = 18.9.12128.139 oobstable
MinimumVisualStudioVersion = 15.0.26124.0
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{FB9E2FB6-DF28-4985-87D6-0334B8260365}"
ProjectSection(SolutionItems) = preProject
Expand All @@ -28,6 +28,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ActionRunner", "src\ActionR
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BuildVerifier.IO.Abstractions", "src\BuildVerifier.IO.Abstractions\BuildVerifier.IO.Abstractions.csproj", "{FD67DE5D-D8C0-48AD-A7F9-E6F316821E10}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DocfxVerifier", "src\DocfxVerifier\DocfxVerifier.csproj", "{3E0FB4E3-3080-D891-0CB6-382B700A7AA8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -122,6 +124,18 @@ Global
{FD67DE5D-D8C0-48AD-A7F9-E6F316821E10}.Release|x64.Build.0 = Release|Any CPU
{FD67DE5D-D8C0-48AD-A7F9-E6F316821E10}.Release|x86.ActiveCfg = Release|Any CPU
{FD67DE5D-D8C0-48AD-A7F9-E6F316821E10}.Release|x86.Build.0 = Release|Any CPU
{3E0FB4E3-3080-D891-0CB6-382B700A7AA8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3E0FB4E3-3080-D891-0CB6-382B700A7AA8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3E0FB4E3-3080-D891-0CB6-382B700A7AA8}.Debug|x64.ActiveCfg = Debug|Any CPU
{3E0FB4E3-3080-D891-0CB6-382B700A7AA8}.Debug|x64.Build.0 = Debug|Any CPU
{3E0FB4E3-3080-D891-0CB6-382B700A7AA8}.Debug|x86.ActiveCfg = Debug|Any CPU
{3E0FB4E3-3080-D891-0CB6-382B700A7AA8}.Debug|x86.Build.0 = Debug|Any CPU
{3E0FB4E3-3080-D891-0CB6-382B700A7AA8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3E0FB4E3-3080-D891-0CB6-382B700A7AA8}.Release|Any CPU.Build.0 = Release|Any CPU
{3E0FB4E3-3080-D891-0CB6-382B700A7AA8}.Release|x64.ActiveCfg = Release|Any CPU
{3E0FB4E3-3080-D891-0CB6-382B700A7AA8}.Release|x64.Build.0 = Release|Any CPU
{3E0FB4E3-3080-D891-0CB6-382B700A7AA8}.Release|x86.ActiveCfg = Release|Any CPU
{3E0FB4E3-3080-D891-0CB6-382B700A7AA8}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\DocfxVerifier\DocfxVerifier.csproj" />
<ProjectReference Include="..\MarkdownLinksVerifier\MarkdownLinksVerifier.csproj" />
<ProjectReference Include="..\RedirectionVerifier\RedirectionVerifier.csproj" />
</ItemGroup>
Expand Down
57 changes: 52 additions & 5 deletions actions/docs-verifier/src/ActionRunner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,37 @@
IEnumerable<Matcher> matchers = await docfxConfigurationReader.MapConfigurationAsync();
IEnumerable<PullRequestFile> pullRequestFiles = await GitHubPullRequest.GetPullRequestFilesAsync(pullRequestNumber);

IEnumerable<string> modifiedDocfxFiles = pullRequestFiles
.Where(file => !file.IsRemoved() && IsDocfxJsonPath(file.FileName))
.Select(file => file.FileName)
.Distinct(StringComparer.OrdinalIgnoreCase);

foreach (string docfxFilePath in modifiedDocfxFiles)
{
if (!await DocfxVerifier.PathVerifier.WriteResultsAsync(Console.Out, docfxFilePath))
{
returnCode++;
}
}

// Verify that all redirection URLs in modified
// redirection files are valid and reachable.
HashSet<string> redirectionFileSet =
new(redirectionFiles.Select(NormalizePath), StringComparer.OrdinalIgnoreCase);

IEnumerable<string> modifiedRedirectionFiles = pullRequestFiles
.Where(file => !file.IsRemoved() && IsRegisteredRedirectionFile(file.FileName, redirectionFileSet))
.Select(file => file.FileName)
.Distinct(StringComparer.OrdinalIgnoreCase);

foreach (string redirectionFilePath in modifiedRedirectionFiles)
{
if (!await RedirectTargetVerifier.WriteResultsAsync(Console.Out, redirectionFilePath))
{
returnCode++;
}
}

List<PullRequestFile> files =
[.. pullRequestFiles.Where(f => IsRedirectableFile(f, matchers))];

Expand All @@ -132,8 +163,7 @@

return returnCode;

static bool IsRedirectableFile(
PullRequestFile file, IEnumerable<Matcher> matchers)
static bool IsRedirectableFile(PullRequestFile file, IEnumerable<Matcher> matchers)
{
string? deletedFileName = file.IsRenamed()
? file.PreviousFileName
Expand All @@ -144,15 +174,32 @@ static bool IsRedirectableFile(

// A deleted toc.yml doesn't need redirection.
// Also, don't require a redirection for file patterns specified as "exclude"s in docfx config file.
return !isDeletedToc && IsYmlOrMarkdownFile(deletedFileName)
return !isDeletedToc
&& IsYmlOrMarkdownFile(deletedFileName)
&& matchers.Any(m => m.Match(deletedFileName).HasMatches);
}

static bool IsYmlOrMarkdownFile([NotNullWhen(true)] string? fileName) =>
Path.GetExtension(fileName) is ".yml" or ".md";

static bool IsDocfxJsonPath(string? path)
{
if (path is null)
{
return false;
}

string normalized = path.Replace('\\', '/');
return normalized.Equals("docfx.json", StringComparison.OrdinalIgnoreCase)
|| normalized.EndsWith("/docfx.json", StringComparison.OrdinalIgnoreCase);
}

static bool IsRegisteredRedirectionFile(string? path, HashSet<string> redirectionFilesSet) =>
path is not null && redirectionFilesSet.Contains(NormalizePath(path));

static string NormalizePath(string path) => path.Replace('\\', '/');

static bool IsExtensionChangeOnly(string file1, string file2) =>
RemoveExtension(file1).Equals(RemoveExtension(file2), StringComparison.OrdinalIgnoreCase);

static string RemoveExtension(string file) =>
file.Substring(0, file.Length - Path.GetExtension(file).Length);
static string RemoveExtension(string file) => file[..^Path.GetExtension(file).Length];
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 actions/docs-verifier/src/DocfxVerifier/PathVerifier.cs
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);
Comment thread
gewarren marked this conversation as resolved.

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;
Comment thread
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;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("GitHub.UnitTests")]
Loading
Loading