-
Notifications
You must be signed in to change notification settings - Fork 39
Add --repo --owner to changelog init #3042
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
11 commits
Select commit
Hold shift + click to select a range
5c6eb52
Add --repo --owner to changelog init
lcawl b720026
Merge branch 'main' into changelog-allowlist-init
lcawl 62d2ef5
Address CodeRabbit feedback
lcawl c91bab0
Fix worktree path inference
cotti e70a56e
Maintain EOL consistency
cotti dbc074d
Extract template seeding and add tests
cotti 15f954a
Apply CR suggestions
cotti 262d5d7
Merge branch 'main' into changelog-allowlist-init
lcawl 41adf02
Merge branch 'main' into changelog-allowlist-init
lcawl 268c456
Merge branch 'main' into changelog-allowlist-init
lcawl 9895704
Merge branch 'main' into changelog-allowlist-init
lcawl 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
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
58 changes: 58 additions & 0 deletions
58
src/Elastic.Documentation.Configuration/ChangelogTemplateSeeder.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,58 @@ | ||
| // Licensed to Elasticsearch B.V under one or more agreements. | ||
| // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
| // See the LICENSE file in the project root for more information | ||
|
|
||
| namespace Elastic.Documentation.Configuration; | ||
|
|
||
| /// <summary> | ||
| /// Applies <c>bundle.owner</c>, <c>bundle.repo</c>, and <c>bundle.link_allow_repos</c> seeding | ||
| /// to the changelog template placeholder. Pure string transformation with no I/O. | ||
| /// </summary> | ||
| public static class ChangelogTemplateSeeder | ||
| { | ||
| internal const string Placeholder = " # changelog-init-bundle-seed"; | ||
|
|
||
| /// <summary> | ||
| /// Replaces or removes the <c># changelog-init-bundle-seed</c> placeholder in template content. | ||
| /// CLI values take precedence over git-inferred values. When only repo is known, owner defaults to <c>elastic</c>. | ||
| /// </summary> | ||
| public static string ApplyBundleRepoSeed(string content, string? ownerCli, string? repoCli, string? gitOwner, string? gitRepo) | ||
| { | ||
| var gitMatched = gitOwner is not null && gitRepo is not null; | ||
|
|
||
| var resolvedRepo = string.IsNullOrWhiteSpace(repoCli) ? gitRepo : repoCli.Trim(); | ||
| var resolvedOwner = string.IsNullOrWhiteSpace(ownerCli) ? gitOwner : ownerCli.Trim(); | ||
| if (!string.IsNullOrWhiteSpace(resolvedRepo) && string.IsNullOrWhiteSpace(resolvedOwner)) | ||
| resolvedOwner = "elastic"; | ||
|
|
||
| var shouldSeed = !string.IsNullOrWhiteSpace(resolvedOwner) && !string.IsNullOrWhiteSpace(resolvedRepo) | ||
| && (!string.IsNullOrWhiteSpace(ownerCli) || !string.IsNullOrWhiteSpace(repoCli) || gitMatched); | ||
|
|
||
| var eol = content.Contains("\r\n", StringComparison.Ordinal) ? "\r\n" : "\n"; | ||
|
|
||
| var block = shouldSeed | ||
| ? $" owner: {QuoteForYaml(resolvedOwner!)}{eol} repo: {QuoteForYaml(resolvedRepo!)}{eol} link_allow_repos:{eol} - {QuoteForYaml($"{resolvedOwner}/{resolvedRepo}")}{eol}" | ||
| : ""; | ||
|
|
||
| var placeholderWithEol = Placeholder + eol; | ||
| if (content.Contains(placeholderWithEol, StringComparison.Ordinal)) | ||
| return content.Replace(placeholderWithEol, block, StringComparison.Ordinal); | ||
|
|
||
| return content.Replace( | ||
| Placeholder, | ||
| shouldSeed ? block.TrimEnd('\r', '\n') : string.Empty, | ||
| StringComparison.Ordinal | ||
| ); | ||
| } | ||
|
|
||
| internal static string QuoteForYaml(string value) => | ||
| value.Contains(':') || value.Contains(' ') || value.Contains('#') || value.Contains('"') | ||
| || value.Contains('\\') || value.Contains('\n') || value.Contains('\r') || value.Contains('\t') | ||
| ? $"\"{value | ||
| .Replace("\\", "\\\\") | ||
| .Replace("\"", "\\\"") | ||
| .Replace("\r", "\\r") | ||
| .Replace("\n", "\\n") | ||
| .Replace("\t", "\\t")}\"" | ||
| : value; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
56 changes: 56 additions & 0 deletions
56
src/Elastic.Documentation.Configuration/GitConfigOriginParser.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,56 @@ | ||
| // Licensed to Elasticsearch B.V under one or more agreements. | ||
| // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
| // See the LICENSE file in the project root for more information | ||
|
|
||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| namespace Elastic.Documentation.Configuration; | ||
|
|
||
| /// <summary> | ||
| /// Reads <c>remote "origin"</c> URL entries from Git <c>config</c> file text. | ||
| /// </summary> | ||
| public static class GitConfigOriginParser | ||
| { | ||
| /// <summary> | ||
| /// Returns the first <c>url</c> value under <c>[remote "origin"]</c>. | ||
| /// </summary> | ||
| public static bool TryGetRemoteOriginUrl(string configContent, [NotNullWhen(true)] out string? url) | ||
| { | ||
| url = null; | ||
| if (string.IsNullOrEmpty(configContent)) | ||
| return false; | ||
|
|
||
| var inOrigin = false; | ||
| foreach (var rawLine in configContent.Split(['\r', '\n'], StringSplitOptions.None)) | ||
| { | ||
| var line = rawLine.Trim(); | ||
| if (line.StartsWith('[')) | ||
| { | ||
| inOrigin = line.Equals("[remote \"origin\"]", StringComparison.Ordinal); | ||
| continue; | ||
| } | ||
|
|
||
| if (!inOrigin) | ||
| continue; | ||
|
|
||
| if (!line.StartsWith("url", StringComparison.OrdinalIgnoreCase)) | ||
| continue; | ||
|
|
||
| var eq = line.IndexOf('='); | ||
| if (eq < 0 || eq >= line.Length - 1) | ||
| continue; | ||
|
|
||
| var value = line[(eq + 1)..].Trim(); | ||
| if (value.Length >= 2 && value[0] == '"' && value[^1] == '"') | ||
| value = value[1..^1]; | ||
|
|
||
| if (string.IsNullOrEmpty(value)) | ||
| continue; | ||
|
|
||
| url = value; | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } |
73 changes: 73 additions & 0 deletions
73
src/Elastic.Documentation.Configuration/GitHubRemoteParser.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,73 @@ | ||
| // Licensed to Elasticsearch B.V under one or more agreements. | ||
| // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
| // See the LICENSE file in the project root for more information | ||
|
|
||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| namespace Elastic.Documentation.Configuration; | ||
|
|
||
| /// <summary> | ||
| /// Parses GitHub.com remote URLs into owner and repository name (public API for changelog tooling). | ||
| /// </summary> | ||
| public static class GitHubRemoteParser | ||
| { | ||
| /// <summary> | ||
| /// Parses an HTTPS or SSH URL for github.com into <paramref name="owner"/> and <paramref name="repo"/>. | ||
| /// Other hosts are rejected. | ||
| /// </summary> | ||
| public static bool TryParseGitHubComOwnerRepo(string? url, [NotNullWhen(true)] out string? owner, [NotNullWhen(true)] out string? repo) | ||
| { | ||
| owner = null; | ||
| repo = null; | ||
| if (string.IsNullOrWhiteSpace(url)) | ||
| return false; | ||
|
|
||
| var trimmed = url.Trim(); | ||
|
|
||
| if (trimmed.StartsWith("git@github.com:", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| var rest = trimmed["git@github.com:".Length..]; | ||
| return TrySplitOwnerRepoPath(rest, out owner, out repo); | ||
| } | ||
|
|
||
| if (trimmed.StartsWith("ssh://git@github.com/", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| var rest = trimmed["ssh://git@github.com/".Length..]; | ||
| return TrySplitOwnerRepoPath(rest, out owner, out repo); | ||
| } | ||
|
|
||
| if (!Uri.TryCreate(trimmed, UriKind.Absolute, out var uri)) | ||
| return false; | ||
|
|
||
| if (!uri.Host.Equals("github.com", StringComparison.OrdinalIgnoreCase)) | ||
| return false; | ||
|
|
||
| var path = uri.AbsolutePath.Trim('/'); | ||
| return TrySplitOwnerRepoPath(path, out owner, out repo); | ||
| } | ||
|
|
||
| private static bool TrySplitOwnerRepoPath(string path, [NotNullWhen(true)] out string? owner, [NotNullWhen(true)] out string? repo) | ||
| { | ||
| owner = null; | ||
| repo = null; | ||
| if (string.IsNullOrWhiteSpace(path)) | ||
| return false; | ||
|
|
||
| path = path.TrimEnd('/', ' '); | ||
| if (path.EndsWith(".git", StringComparison.OrdinalIgnoreCase)) | ||
| path = path[..^4]; | ||
|
|
||
| var slash = path.IndexOf('/'); | ||
| if (slash <= 0 || slash >= path.Length - 1) | ||
| return false; | ||
|
|
||
| var o = path[..slash]; | ||
| var r = path[(slash + 1)..]; | ||
| if (string.IsNullOrEmpty(o) || string.IsNullOrEmpty(r) || r.Contains('/')) | ||
| return false; | ||
|
|
||
| owner = o; | ||
| repo = r; | ||
| return true; | ||
| } | ||
| } |
94 changes: 94 additions & 0 deletions
94
src/Elastic.Documentation.Configuration/GitRemoteConfigurationReader.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,94 @@ | ||
| // Licensed to Elasticsearch B.V under one or more agreements. | ||
| // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
| // See the LICENSE file in the project root for more information | ||
|
|
||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.IO.Abstractions; | ||
|
|
||
| namespace Elastic.Documentation.Configuration; | ||
|
|
||
| /// <summary> | ||
| /// Reads <c>remote.origin.url</c> from a local Git checkout using the file system (no subprocess). | ||
| /// </summary> | ||
| public static class GitRemoteConfigurationReader | ||
| { | ||
| /// <summary> | ||
| /// Reads <c>.git/config</c>, or the <c>config</c> file referenced by a <c>.git</c> worktree pointer file. | ||
| /// </summary> | ||
| public static bool TryReadOriginUrl(IFileSystem fileSystem, string repositoryRoot, [NotNullWhen(true)] out string? url) | ||
| { | ||
| url = null; | ||
| try | ||
| { | ||
| var gitPath = fileSystem.Path.Combine(repositoryRoot, ".git"); | ||
| if (fileSystem.Directory.Exists(gitPath)) | ||
| { | ||
| var configPath = fileSystem.Path.Combine(gitPath, "config"); | ||
| return TryReadOriginUrlFromConfigPath(fileSystem, configPath, out url); | ||
| } | ||
|
|
||
| if (!fileSystem.File.Exists(gitPath)) | ||
| return false; | ||
|
|
||
| var gitFileText = fileSystem.File.ReadAllText(gitPath); | ||
| var firstLineBreak = gitFileText.IndexOfAny(['\r', '\n']); | ||
| var firstLine = firstLineBreak >= 0 ? gitFileText[..firstLineBreak] : gitFileText; | ||
| firstLine = firstLine.Trim(); | ||
| if (!firstLine.StartsWith("gitdir:", StringComparison.OrdinalIgnoreCase)) | ||
| return false; | ||
|
|
||
| var gitDir = firstLine["gitdir:".Length..].Trim(); | ||
| if (string.IsNullOrEmpty(gitDir)) | ||
| return false; | ||
|
|
||
| var resolvedGitDir = fileSystem.Path.IsPathFullyQualified(gitDir) | ||
| ? gitDir | ||
| : fileSystem.Path.GetFullPath(fileSystem.Path.Combine(repositoryRoot, gitDir)); | ||
|
|
||
| var commonDirFile = fileSystem.Path.Combine(resolvedGitDir, "commondir"); | ||
| if (!fileSystem.File.Exists(commonDirFile)) | ||
| return false; | ||
|
|
||
| var commonDirRelative = fileSystem.File.ReadAllText(commonDirFile).Trim(); | ||
| var commonDir = fileSystem.Path.IsPathFullyQualified(commonDirRelative) | ||
| ? commonDirRelative | ||
| : fileSystem.Path.GetFullPath(fileSystem.Path.Combine(resolvedGitDir, commonDirRelative)); | ||
|
|
||
| var worktreeConfigPath = fileSystem.Path.Combine(commonDir, "config"); | ||
| return TryReadOriginUrlFromConfigPath(fileSystem, worktreeConfigPath, out url); | ||
| } | ||
| catch (IOException) | ||
| { | ||
| url = null; | ||
| return false; | ||
| } | ||
| catch (UnauthorizedAccessException) | ||
| { | ||
| url = null; | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private static bool TryReadOriginUrlFromConfigPath(IFileSystem fileSystem, string configPath, [NotNullWhen(true)] out string? url) | ||
| { | ||
| url = null; | ||
| try | ||
| { | ||
| if (!fileSystem.File.Exists(configPath)) | ||
| return false; | ||
|
|
||
| var content = fileSystem.File.ReadAllText(configPath); | ||
| return GitConfigOriginParser.TryGetRemoteOriginUrl(content, out url); | ||
| } | ||
| catch (IOException) | ||
| { | ||
| url = null; | ||
| return false; | ||
| } | ||
| catch (UnauthorizedAccessException) | ||
| { | ||
| url = null; | ||
| return false; | ||
| } | ||
| } | ||
| } |
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
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.