Skip to content

Commit 76b6039

Browse files
authored
Ensure keys in redirect.yml get validated as relative to docset.yml (#1866)
* Ensure keys in redirect.yml get validated as relative to docset.yml * Move from hint to warning * Make root relative keys an error
1 parent 44a0322 commit 76b6039

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

src/tooling/docs-builder/Cli/DiffCommands.cs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -77,31 +77,33 @@ public async Task<int> ValidateRedirects(string? path = null, Cancel ctx = defau
7777
if (changed.Length != 0)
7878
_log.LogInformation("Found {Count} changes to files related to documentation in the current branch.", changed.Length);
7979

80-
var missingRedirects = changed
81-
.Where(c =>
82-
c.ChangeType is GitChangeType.Deleted or GitChangeType.Renamed
83-
&& !redirects.ContainsKey(c is RenamedGitChange renamed ? renamed.OldFilePath : c.FilePath)
84-
)
85-
.ToArray();
86-
87-
if (missingRedirects.Length != 0)
88-
{
89-
var relativeRedirectFile = Path.GetRelativePath(root.FullName, redirectFile.Source.FullName);
90-
_log.LogInformation("Found {Count} changes that still require updates to: {RedirectFile}", missingRedirects.Length, relativeRedirectFile);
91-
}
92-
93-
foreach (var notFound in missingRedirects)
80+
var deletedAndRenamed = changed.Where(c => c.ChangeType is GitChangeType.Deleted or GitChangeType.Renamed).ToArray();
81+
var missingCount = 0;
82+
foreach (var change in deletedAndRenamed)
9483
{
95-
if (notFound is RenamedGitChange renamed)
84+
var lookupPath = change is RenamedGitChange renamed ? renamed.OldFilePath : change.FilePath;
85+
var docSetRelativePath = Path.GetRelativePath(buildContext.DocumentationSourceDirectory.FullName, Path.Combine(root.FullName, lookupPath));
86+
var rootRelativePath = Path.GetRelativePath(root.FullName, Path.Combine(root.FullName, lookupPath));
87+
if (redirects.ContainsKey(docSetRelativePath))
88+
continue;
89+
if (redirects.ContainsKey(rootRelativePath))
9690
{
9791
collector.EmitError(redirectFile.Source,
98-
$"File '{renamed.OldFilePath}' was renamed to '{renamed.NewFilePath}' but it has no redirect configuration set.");
99-
}
100-
else if (notFound.ChangeType is GitChangeType.Deleted)
101-
{
102-
collector.EmitError(redirectFile.Source,
103-
$"File '{notFound.FilePath}' was deleted but it has no redirect targets. This will lead to broken links.");
92+
$"Redirect contains path relative to root '{rootRelativePath}' but should be relative to the documentation set '{docSetRelativePath}'");
93+
continue;
10494
}
95+
missingCount++;
96+
97+
if (change is RenamedGitChange rename)
98+
collector.EmitError(redirectFile.Source, $"Missing '{docSetRelativePath}' in redirects.yml. '{rename.OldFilePath}' was renamed to '{rename.NewFilePath}' but it has no redirect configuration set.");
99+
else if (change.ChangeType is GitChangeType.Deleted)
100+
collector.EmitError(redirectFile.Source, $"Missing '{docSetRelativePath}' in redirects.yml. '{change.FilePath}' was deleted but it has no redirect targets. This will lead to broken links.");
101+
}
102+
103+
if (missingCount != 0)
104+
{
105+
var relativeRedirectFile = Path.GetRelativePath(root.FullName, redirectFile.Source.FullName);
106+
_log.LogInformation("Found {Count} changes that still require updates to: {RedirectFile}", missingCount, relativeRedirectFile);
105107
}
106108

107109
await collector.StopAsync(ctx);

0 commit comments

Comments
 (0)