Skip to content

Commit 3c9e40d

Browse files
committed
Skip validate-link-reference on ci if its run against a repo with no docs
1 parent a18a3e7 commit 3c9e40d

File tree

5 files changed

+71
-34
lines changed

5 files changed

+71
-34
lines changed

src/Elastic.Documentation.Configuration/BuildContext.cs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public BuildContext(
7777
? ReadFileSystem.DirectoryInfo.New(source)
7878
: ReadFileSystem.DirectoryInfo.New(Path.Combine(Paths.WorkingDirectoryRoot.FullName));
7979

80-
(DocumentationSourceDirectory, ConfigurationPath) = FindDocsFolderFromRoot(rootFolder);
80+
(DocumentationSourceDirectory, ConfigurationPath) = Paths.FindDocsFolderFromRoot(ReadFileSystem, rootFolder);
8181

8282
DocumentationCheckoutDirectory = Paths.DetermineSourceDirectoryRoot(DocumentationSourceDirectory);
8383

@@ -96,29 +96,4 @@ public BuildContext(
9696
};
9797
}
9898

99-
private (IDirectoryInfo, IFileInfo) FindDocsFolderFromRoot(IDirectoryInfo rootPath)
100-
{
101-
string[] files = ["docset.yml", "_docset.yml"];
102-
string[] knownFolders = [rootPath.FullName, Path.Combine(rootPath.FullName, "docs")];
103-
var mostLikelyTargets =
104-
from file in files
105-
from folder in knownFolders
106-
select Path.Combine(folder, file);
107-
108-
var knownConfigPath = mostLikelyTargets.FirstOrDefault(ReadFileSystem.File.Exists);
109-
var configurationPath = knownConfigPath is null ? null : ReadFileSystem.FileInfo.New(knownConfigPath);
110-
if (configurationPath is not null)
111-
return (configurationPath.Directory!, configurationPath);
112-
113-
configurationPath = rootPath
114-
.EnumerateFiles("*docset.yml", SearchOption.AllDirectories)
115-
.FirstOrDefault()
116-
?? throw new Exception($"Can not locate docset.yml file in '{rootPath}'");
117-
118-
var docsFolder = configurationPath.Directory
119-
?? throw new Exception($"Can not locate docset.yml file in '{rootPath}'");
120-
121-
return (docsFolder, configurationPath);
122-
}
123-
12499
}

src/Elastic.Documentation.Configuration/Paths.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
44

5+
using System.Diagnostics.CodeAnalysis;
56
using System.IO.Abstractions;
67

78
namespace Elastic.Documentation.Configuration;
@@ -50,4 +51,47 @@ private static DirectoryInfo GetApplicationFolder()
5051
return new DirectoryInfo(elasticPath);
5152
}
5253

54+
public static (IDirectoryInfo, IFileInfo) FindDocsFolderFromRoot(IFileSystem readFileSystem, IDirectoryInfo rootPath)
55+
{
56+
string[] files = ["docset.yml", "_docset.yml"];
57+
string[] knownFolders = [rootPath.FullName, Path.Combine(rootPath.FullName, "docs")];
58+
var mostLikelyTargets =
59+
from file in files
60+
from folder in knownFolders
61+
select Path.Combine(folder, file);
62+
63+
var knownConfigPath = mostLikelyTargets.FirstOrDefault(readFileSystem.File.Exists);
64+
var configurationPath = knownConfigPath is null ? null : readFileSystem.FileInfo.New(knownConfigPath);
65+
if (configurationPath is not null)
66+
return (configurationPath.Directory!, configurationPath);
67+
68+
configurationPath = rootPath
69+
.EnumerateFiles("*docset.yml", SearchOption.AllDirectories)
70+
.FirstOrDefault()
71+
?? throw new Exception($"Can not locate docset.yml file in '{rootPath}'");
72+
73+
var docsFolder = configurationPath.Directory ?? throw new Exception($"Can not locate docset.yml file in '{rootPath}'");
74+
75+
return (docsFolder, configurationPath);
76+
}
77+
78+
public static bool TryFindDocsFolderFromRoot(
79+
IFileSystem readFileSystem,
80+
IDirectoryInfo rootPath,
81+
[NotNullWhen(true)] out IDirectoryInfo? docDirectory,
82+
[NotNullWhen(true)] out IFileInfo? configurationPath
83+
)
84+
{
85+
docDirectory = null;
86+
configurationPath = null;
87+
try
88+
{
89+
(docDirectory, configurationPath) = FindDocsFolderFromRoot(readFileSystem, rootPath);
90+
return true;
91+
}
92+
catch
93+
{
94+
return false;
95+
}
96+
}
5397
}

src/tooling/docs-assembler/Cli/InboundLinkCommands.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ namespace Documentation.Assembler.Cli;
1717
internal sealed class InboundLinkCommands(ILoggerFactory logger, ICoreService githubActionsService)
1818
{
1919
private readonly LinkIndexLinkChecker _linkIndexLinkChecker = new(logger);
20+
private readonly ILogger<Program> _log = logger.CreateLogger<Program>();
2021

2122
[SuppressMessage("Usage", "CA2254:Template should be a static expression")]
2223
private void AssignOutputLogger()
2324
{
24-
var log = logger.CreateLogger<Program>();
25-
ConsoleApp.Log = msg => log.LogInformation(msg);
26-
ConsoleApp.LogError = msg => log.LogError(msg);
25+
ConsoleApp.Log = msg => _log.LogInformation(msg);
26+
ConsoleApp.LogError = msg => _log.LogError(msg);
2727
}
2828

2929
/// <summary> Validate all published cross_links in all published links.json files. </summary>
@@ -64,19 +64,30 @@ public async Task<int> ValidateRepoInboundLinks(string? from = null, string? to
6464
/// Validate a locally published links.json file against all published links.json files in the registry
6565
/// </summary>
6666
/// <param name="file">Path to `links.json` defaults to '.artifacts/docs/html/links.json'</param>
67+
/// <param name="path"> -p, Defaults to the `{pwd}` folder</param>
6768
/// <param name="ctx"></param>
6869
[Command("validate-link-reference")]
69-
public async Task<int> ValidateLocalLinkReference([Argument] string? file = null, Cancel ctx = default)
70+
public async Task<int> ValidateLocalLinkReference(string? file = null, string? path = null, Cancel ctx = default)
7071
{
7172
AssignOutputLogger();
7273
file ??= ".artifacts/docs/html/links.json";
7374
var fs = new FileSystem();
74-
var root = fs.DirectoryInfo.New(Paths.WorkingDirectoryRoot.FullName);
75+
var root = !string.IsNullOrEmpty(path) ? fs.DirectoryInfo.New(path) : fs.DirectoryInfo.New(Paths.WorkingDirectoryRoot.FullName);
7576
var repository = GitCheckoutInformation.Create(root, new FileSystem(), logger.CreateLogger(nameof(GitCheckoutInformation))).RepositoryName
7677
?? throw new Exception("Unable to determine repository name");
7778

79+
var runningOnCi = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GITHUB_ACTIONS"));
80+
if (runningOnCi && !Paths.TryFindDocsFolderFromRoot(fs, root, out _, out _))
81+
{
82+
_log.LogInformation("Running in CI on a folder with no docset.yml file in {Directory}, skipping the validation", root.FullName);
83+
return 0;
84+
}
85+
86+
var resolvedFile = Path.Combine(root.FullName, file);
87+
_log.LogInformation("Validating {File} in {Directory}", file, root.FullName);
88+
7889
await using var collector = new ConsoleDiagnosticsCollector(logger, githubActionsService).StartAsync(ctx);
79-
await _linkIndexLinkChecker.CheckWithLocalLinksJson(collector, repository, file, ctx);
90+
await _linkIndexLinkChecker.CheckWithLocalLinksJson(collector, repository, resolvedFile, ctx);
8091
await collector.StopAsync(ctx);
8192
return collector.Errors;
8293
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ public async Task<int> Generate(
125125
CanonicalBaseUrl = canonicalBaseUri
126126
};
127127
}
128-
// On CI, we are running on merge commit which may have changes against an older
129-
// docs folder (this can happen on out of date PR's).
128+
// On CI, we are running on a merge commit which may have changes against an older
129+
// docs folder (this can happen on out-of-date PR's).
130130
// At some point in the future we can remove this try catch
131131
catch (Exception e) when (runningOnCi && e.Message.StartsWith("Can not locate docset.yml file in"))
132132
{

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ public async Task<int> ValidateLocalLinkReference(string? file = null, string? p
8383
var repository = GitCheckoutInformation.Create(root, new FileSystem(), logger.CreateLogger(nameof(GitCheckoutInformation))).RepositoryName
8484
?? throw new Exception("Unable to determine repository name");
8585

86+
var runningOnCi = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GITHUB_ACTIONS"));
87+
if (runningOnCi && !Paths.TryFindDocsFolderFromRoot(fs, root, out _, out _))
88+
{
89+
_log.LogInformation("Running in CI on a folder with no docset.yml file in {Directory}, skipping the validation", root.FullName);
90+
return 0;
91+
}
92+
8693
var resolvedFile = Path.Combine(root.FullName, file);
8794
_log.LogInformation("Validating {File} in {Directory}", file, root.FullName);
8895

0 commit comments

Comments
 (0)