Skip to content

Commit 171605c

Browse files
authored
Check redirect rules on CI preview builds (#1693)
* Check redirect rules using IntegrationGitRepositoryTracker on CI preview builds * Adjust message * Typo * Use all_old_new_renamed_files * Github REST API does not provide old file names. * Change hint to regular log
1 parent 8731657 commit 171605c

File tree

5 files changed

+82
-11
lines changed

5 files changed

+82
-11
lines changed

.github/workflows/preview-build.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ jobs:
117117
outputs:
118118
any_modified: ${{ steps.check-files.outputs.any_modified }}
119119
all_changed_files: ${{ steps.check-files.outputs.all_changed_files }}
120+
added_files: ${{ steps.check-files.outputs.added_files }}
121+
modified_files: ${{ steps.check-files.outputs.modified_files }}
122+
deleted_files: ${{ steps.check-files.outputs.deleted_files }}
123+
renamed_files: ${{ steps.check-files.outputs.renamed_files }}
120124
steps:
121125
- name: Checkout
122126
if: contains(fromJSON('["push", "merge_group", "workflow_dispatch"]'), github.event_name)
@@ -149,6 +153,10 @@ jobs:
149153
env:
150154
GITHUB_PR_REF_NAME: ${{ github.event.pull_request.head.ref }}
151155
MATCH: ${{ needs.match.outputs.content-source-match }}
156+
ADDED_FILES: ${{ needs.check.outputs.added_files }}
157+
MODIFIED_FILES: ${{ needs.check.outputs.modified_files }}
158+
DELETED_FILES: ${{ needs.check.outputs.deleted_files }}
159+
RENAMED_FILES: ${{ needs.check.outputs.renamed_files }}
152160
needs:
153161
- check
154162
- match
@@ -234,6 +242,19 @@ jobs:
234242
&& steps.deployment.outputs.result
235243
uses: elastic/docs-builder/.github/actions/bootstrap@main
236244

245+
- name: 'Validate redirect rules'
246+
if: >
247+
env.MATCH == 'true'
248+
&& (
249+
steps.deployment.outputs.result
250+
|| (
251+
needs.check.outputs.any_modified == 'true'
252+
&& github.event_name == 'merge_group'
253+
)
254+
)
255+
run: |
256+
dotnet run --project src/tooling/docs-builder -- diff validate
257+
237258
# we run our artifact directly, please use the prebuild
238259
# elastic/docs-builder@main GitHub Action for all other repositories!
239260
- name: Build documentation

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ internal sealed class DiffCommands(
2222
IConfigurationContext configurationContext
2323
)
2424
{
25+
private readonly ILogger<Program> _log = logFactory.CreateLogger<Program>();
26+
2527
/// <summary>
2628
/// Validates redirect updates in the current branch using the redirect file against changes reported by git.
2729
/// </summary>
@@ -31,6 +33,7 @@ IConfigurationContext configurationContext
3133
[Command("validate")]
3234
public async Task<int> ValidateRedirects([Argument] string? path = null, Cancel ctx = default)
3335
{
36+
var runningOnCi = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GITHUB_ACTIONS"));
3437
path ??= "docs";
3538

3639
await using var collector = new ConsoleDiagnosticsCollector(logFactory, githubActionsService).StartAsync(ctx);
@@ -53,16 +56,21 @@ public async Task<int> ValidateRedirects([Argument] string? path = null, Cancel
5356
return collector.Errors;
5457
}
5558

56-
var tracker = new LocalGitRepositoryTracker(collector, root);
57-
var changed = tracker.GetChangedFiles(path);
59+
IRepositoryTracker tracker = runningOnCi ? new IntegrationGitRepositoryTracker(path) : new LocalGitRepositoryTracker(collector, root, path);
60+
var changed = tracker.GetChangedFiles() as GitChange[] ?? [];
61+
62+
if (changed.Length > 0)
63+
_log.LogInformation($"Found {changed.Length} changes to files related to documentation in the current branch.");
5864

5965
foreach (var notFound in changed.DistinctBy(c => c.FilePath).Where(c => c.ChangeType is GitChangeType.Deleted or GitChangeType.Renamed
6066
&& !redirects.ContainsKey(c is RenamedGitChange renamed ? renamed.OldFilePath : c.FilePath)))
6167
{
6268
if (notFound is RenamedGitChange renamed)
6369
{
6470
collector.EmitError(redirectFileInfo.Name,
65-
$"File '{renamed.OldFilePath}' was renamed to '{renamed.NewFilePath}' but it has no redirect configuration set.");
71+
runningOnCi
72+
? $"A file was renamed to '{renamed.NewFilePath}' but it has no redirect configuration set."
73+
: $"File '{renamed.OldFilePath}' was renamed to '{renamed.NewFilePath}' but it has no redirect configuration set.");
6674
}
6775
else if (notFound.ChangeType is GitChangeType.Deleted)
6876
{

src/tooling/docs-builder/Tracking/IRepositoryTracker.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ public enum GitChangeType
1414
Other
1515
}
1616

17+
public record GitChange(string FilePath, GitChangeType ChangeType);
18+
public record RenamedGitChange(string OldFilePath, string NewFilePath, GitChangeType ChangeType) : GitChange(OldFilePath, ChangeType);
19+
1720
public interface IRepositoryTracker
1821
{
19-
IEnumerable<GitChange> GetChangedFiles(string lookupPath);
22+
IEnumerable<GitChange> GetChangedFiles();
2023
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
namespace Documentation.Builder.Tracking;
6+
7+
public class IntegrationGitRepositoryTracker(string lookupPath) : IRepositoryTracker
8+
{
9+
private string LookupPath { get; } = $"{lookupPath}/";
10+
public IEnumerable<GitChange> GetChangedFiles()
11+
{
12+
var deletedFiles = Environment.GetEnvironmentVariable("DELETED_FILES") ?? string.Empty;
13+
if (!string.IsNullOrEmpty(deletedFiles))
14+
{
15+
foreach (var file in deletedFiles.Split(' ', StringSplitOptions.RemoveEmptyEntries).Where(f => f.StartsWith(LookupPath)))
16+
yield return new GitChange(file, GitChangeType.Deleted);
17+
}
18+
19+
var addedFiles = Environment.GetEnvironmentVariable("ADDED_FILES");
20+
if (!string.IsNullOrEmpty(addedFiles))
21+
{
22+
foreach (var file in addedFiles.Split(' ', StringSplitOptions.RemoveEmptyEntries).Where(f => f.StartsWith(LookupPath)))
23+
yield return new GitChange(file, GitChangeType.Added);
24+
}
25+
26+
var modifiedFiles = Environment.GetEnvironmentVariable("MODIFIED_FILES");
27+
if (!string.IsNullOrEmpty(modifiedFiles))
28+
{
29+
foreach (var file in modifiedFiles.Split(' ', StringSplitOptions.RemoveEmptyEntries).Where(f => f.StartsWith(LookupPath)))
30+
yield return new GitChange(file, GitChangeType.Modified);
31+
}
32+
33+
var renamedFiles = Environment.GetEnvironmentVariable("RENAMED_FILES");
34+
if (!string.IsNullOrEmpty(renamedFiles))
35+
{
36+
foreach (var file in renamedFiles.Split(' ', StringSplitOptions.RemoveEmptyEntries).Where(f => f.StartsWith(LookupPath)))
37+
yield return new RenamedGitChange(string.Empty, file, GitChangeType.Renamed);
38+
}
39+
}
40+
}

src/tooling/docs-builder/Tracking/LocalGitRepositoryTracker.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@
88

99
namespace Documentation.Builder.Tracking;
1010

11-
public record GitChange(string FilePath, GitChangeType ChangeType);
12-
public record RenamedGitChange(string OldFilePath, string NewFilePath, GitChangeType ChangeType) : GitChange(OldFilePath, ChangeType);
13-
14-
public class LocalGitRepositoryTracker(DiagnosticsCollector collector, IDirectoryInfo workingDirectory) : ExternalCommandExecutor(collector, workingDirectory), IRepositoryTracker
11+
public class LocalGitRepositoryTracker(DiagnosticsCollector collector, IDirectoryInfo workingDirectory, string lookupPath) : ExternalCommandExecutor(collector, workingDirectory), IRepositoryTracker
1512
{
16-
public IEnumerable<GitChange> GetChangedFiles(string lookupPath)
13+
private string LookupPath { get; } = lookupPath;
14+
15+
public IEnumerable<GitChange> GetChangedFiles()
1716
{
1817
var defaultBranch = GetDefaultBranch();
19-
var commitChanges = CaptureMultiple("git", "diff", "--name-status", $"{defaultBranch}...HEAD", "--", $"./{lookupPath}");
18+
var commitChanges = CaptureMultiple("git", "diff", "--name-status", $"{defaultBranch}...HEAD", "--", $"./{LookupPath}");
2019
var localChanges = CaptureMultiple("git", "status", "--porcelain");
21-
ExecInSilent([], "git", "stash", "push", "--", $"./{lookupPath}");
20+
ExecInSilent([], "git", "stash", "push", "--", $"./{LookupPath}");
2221
var localUnstagedChanges = CaptureMultiple("git", "stash", "show", "--name-status", "-u");
2322
ExecInSilent([], "git", "stash", "pop");
2423

0 commit comments

Comments
 (0)