-
Notifications
You must be signed in to change notification settings - Fork 43
Changelogs: Perform artifact handling on docs-builder #3199
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
8 commits
Select commit
Hold shift + click to select a range
2126f38
Reintroduce artifact handling commands
cotti 7ee73ea
Fix scrubber
cotti cd41cba
Fix build
cotti a584d49
using ordering
cotti 90b2d4e
Improve guarding at EvaluateArtifact
cotti 906ab18
Enable docs-preview-local S3 uploads (#3198)
Mpdreamz 43afaa3
Merge branch 'main' into changelog_fork_pr
cotti a07216e
Merge branch 'main' into changelog_fork_pr
cotti 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
114 changes: 114 additions & 0 deletions
114
src/services/Elastic.Changelog/Evaluation/ChangelogArtifactEvaluationService.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,114 @@ | ||
| // 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.Globalization; | ||
| using System.IO.Abstractions; | ||
| using System.Text.Json; | ||
| using Actions.Core.Services; | ||
| using Elastic.Changelog.Creation; | ||
| using Elastic.Changelog.GitHub; | ||
| using Elastic.Documentation.Diagnostics; | ||
| using Elastic.Documentation.Services; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Elastic.Changelog.Evaluation; | ||
|
|
||
| /// <summary>Service implementing the changelog evaluate-artifact CI command.</summary> | ||
| public class ChangelogArtifactEvaluationService( | ||
| ILoggerFactory logFactory, | ||
| IGitHubPrService gitHubPrService, | ||
| ICoreService coreService, | ||
| IFileSystem? fileSystem = null | ||
| ) : IService | ||
| { | ||
| private readonly ILogger _logger = logFactory.CreateLogger<ChangelogArtifactEvaluationService>(); | ||
| private readonly IFileSystem _fileSystem = fileSystem ?? new FileSystem(); | ||
|
|
||
| public async Task<bool> EvaluateArtifact(IDiagnosticsCollector collector, EvaluateArtifactArguments input, Cancel ctx) | ||
| { | ||
| ChangelogArtifactMetadata? metadata; | ||
| try | ||
| { | ||
| var artifactMetadataJson = await _fileSystem.File.ReadAllTextAsync(input.MetadataPath, ctx); | ||
| metadata = JsonSerializer.Deserialize(artifactMetadataJson, ChangelogArtifactMetadataJsonContext.Default.ChangelogArtifactMetadata); | ||
| } | ||
| catch (FileNotFoundException) | ||
| { | ||
| _logger.LogInformation("Metadata file not found at {Path}, nothing to evaluate", input.MetadataPath); | ||
| return true; | ||
| } | ||
| catch (DirectoryNotFoundException) | ||
| { | ||
| _logger.LogInformation("Metadata file not found at {Path}, nothing to evaluate", input.MetadataPath); | ||
| return true; | ||
| } | ||
| catch (IOException ex) | ||
| { | ||
| collector.EmitError(input.MetadataPath, $"Failed to read artifact metadata: {ex.Message}"); | ||
| return false; | ||
| } | ||
| catch (JsonException ex) | ||
| { | ||
| collector.EmitError(input.MetadataPath, $"Failed to deserialize artifact metadata: {ex.Message}"); | ||
| return false; | ||
| } | ||
| if (metadata is null) | ||
| { | ||
| collector.EmitError(input.MetadataPath, "Failed to deserialize artifact metadata"); | ||
| return false; | ||
| } | ||
|
|
||
| var prInfo = await gitHubPrService.FetchPrInfoAsync( | ||
| metadata.PrNumber.ToString(CultureInfo.InvariantCulture), input.Owner, input.Repo, ctx | ||
| ); | ||
| if (prInfo is null) | ||
| { | ||
| collector.EmitError(input.MetadataPath, $"Failed to fetch PR #{metadata.PrNumber} from GitHub"); | ||
| return false; | ||
| } | ||
|
|
||
| if (!string.Equals(prInfo.HeadSha, metadata.HeadSha, StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| _logger.LogInformation("PR head has moved ({OldSha} → {NewSha}), skipping — newer run will handle it", | ||
| metadata.HeadSha, prInfo.HeadSha); | ||
| return true; | ||
| } | ||
|
|
||
| if (PrInfoProcessor.AreAllProductsBlocked(prInfo.Labels.ToArray(), metadata.CreateRules)) | ||
| { | ||
| _logger.LogInformation("Labels changed since generate, all products blocked — aborting gracefully"); | ||
| return true; | ||
| } | ||
|
|
||
| var statusParsed = PrEvaluationResultExtensions.TryParse( | ||
| metadata.Status, out var metadataStatus, ignoreCase: true, allowMatchingMetadataAttribute: true | ||
| ); | ||
|
|
||
| var shouldCommit = statusParsed && metadataStatus == PrEvaluationResult.Success && metadata.CanCommit; | ||
| var shouldCommentSuccess = statusParsed && metadataStatus == PrEvaluationResult.Success && !metadata.CanCommit; | ||
| var shouldCommentFailure = statusParsed && metadataStatus == PrEvaluationResult.NoLabel; | ||
|
|
||
| await coreService.SetOutputAsync("pr-number", metadata.PrNumber.ToString(CultureInfo.InvariantCulture)); | ||
| await coreService.SetOutputAsync("head-ref", metadata.HeadRef); | ||
| await coreService.SetOutputAsync("head-sha", metadata.HeadSha); | ||
| await coreService.SetOutputAsync("status", metadata.Status); | ||
| await coreService.SetOutputAsync("is-fork", metadata.IsFork ? "true" : "false"); | ||
| await coreService.SetOutputAsync("head-repo", metadata.HeadRepo ?? string.Empty); | ||
| await coreService.SetOutputAsync("config-file", metadata.ConfigFile ?? string.Empty); | ||
| await coreService.SetOutputAsync("changelog-dir", metadata.ChangelogDir ?? string.Empty); | ||
| await coreService.SetOutputAsync("changelog-filename", metadata.ChangelogFilename ?? string.Empty); | ||
| await coreService.SetOutputAsync("label-table", metadata.LabelTable ?? string.Empty); | ||
| await coreService.SetOutputAsync("product-label-table", metadata.ProductLabelTable ?? string.Empty); | ||
| await coreService.SetOutputAsync("skip-labels", metadata.SkipLabels ?? string.Empty); | ||
| await coreService.SetOutputAsync("should-commit", shouldCommit ? "true" : "false"); | ||
| await coreService.SetOutputAsync("should-comment-success", shouldCommentSuccess ? "true" : "false"); | ||
| await coreService.SetOutputAsync("should-comment-failure", shouldCommentFailure ? "true" : "false"); | ||
|
|
||
| _logger.LogInformation( | ||
| "Artifact evaluation complete: status={Status}, commit={Commit}, commentSuccess={CommentSuccess}, commentFailure={CommentFailure}", | ||
| metadata.Status, shouldCommit, shouldCommentSuccess, shouldCommentFailure); | ||
|
|
||
| return true; | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
src/services/Elastic.Changelog/Evaluation/ChangelogArtifactMetadata.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,40 @@ | ||
| // 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.Text.Json.Serialization; | ||
| using Elastic.Documentation.Configuration.Changelog; | ||
| using Elastic.Documentation.ReleaseNotes; | ||
|
|
||
| namespace Elastic.Changelog.Evaluation; | ||
|
|
||
| /// <summary>Artifact metadata transferred between the generate and commit CI workflows.</summary> | ||
| public record ChangelogArtifactMetadata | ||
| { | ||
| public required int PrNumber { get; init; } | ||
| public required string HeadRef { get; init; } | ||
| public required string HeadSha { get; init; } | ||
| public required string Status { get; init; } | ||
| public required bool IsFork { get; init; } | ||
| public required bool CanCommit { get; init; } | ||
| public required bool MaintainerCanModify { get; init; } | ||
| public string? HeadRepo { get; init; } | ||
| public string? LabelTable { get; init; } | ||
| public string? ProductLabelTable { get; init; } | ||
| public string? SkipLabels { get; init; } | ||
| public string? ConfigFile { get; init; } | ||
| public string? ChangelogDir { get; init; } | ||
| public string? ChangelogFilename { get; init; } | ||
| public CreateRules? CreateRules { get; init; } | ||
| } | ||
|
|
||
| [JsonSourceGenerationOptions( | ||
| WriteIndented = true, | ||
| UseStringEnumConverter = true, | ||
| PropertyNamingPolicy = JsonKnownNamingPolicy.SnakeCaseLower | ||
| )] | ||
| [JsonSerializable(typeof(ChangelogArtifactMetadata))] | ||
| [JsonSerializable(typeof(CreateRules))] | ||
| [JsonSerializable(typeof(FieldMode))] | ||
| [JsonSerializable(typeof(MatchMode))] | ||
| public sealed partial class ChangelogArtifactMetadataJsonContext : JsonSerializerContext; |
126 changes: 126 additions & 0 deletions
126
src/services/Elastic.Changelog/Evaluation/ChangelogPrepareArtifactService.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,126 @@ | ||
| // 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.IO.Abstractions; | ||
| using System.Text.Json; | ||
| using Actions.Core.Services; | ||
| using Elastic.Changelog.Configuration; | ||
| using Elastic.Documentation.Configuration; | ||
| using Elastic.Documentation.Diagnostics; | ||
| using Elastic.Documentation.Services; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Elastic.Changelog.Evaluation; | ||
|
|
||
| /// <summary>Service implementing the changelog prepare-artifact CI command.</summary> | ||
| public class ChangelogPrepareArtifactService( | ||
| ILoggerFactory logFactory, | ||
| IConfigurationContext configurationContext, | ||
| ICoreService coreService, | ||
| IFileSystem? fileSystem = null | ||
| ) : IService | ||
| { | ||
| private readonly ILogger _logger = logFactory.CreateLogger<ChangelogPrepareArtifactService>(); | ||
| private readonly IFileSystem _fileSystem = fileSystem ?? new FileSystem(); | ||
| private readonly ChangelogConfigurationLoader _configLoader = new(logFactory, configurationContext, fileSystem ?? new FileSystem()); | ||
|
|
||
| public async Task<bool> PrepareArtifact(IDiagnosticsCollector collector, PrepareArtifactArguments input, Cancel ctx) | ||
| { | ||
| var status = ResolveStatus(input.EvaluateStatus, input.GenerateOutcome); | ||
| _logger.LogInformation("Resolved artifact status: {Status} (evaluate={Evaluate}, generate={Generate})", | ||
| status, input.EvaluateStatus, input.GenerateOutcome); | ||
|
|
||
| _ = _fileSystem.Directory.CreateDirectory(input.OutputDir); | ||
|
|
||
| string? changelogFilename = null; | ||
| if (status == PrEvaluationResult.Success) | ||
| { | ||
| var sourceYaml = FindStagingYaml(input.StagingDir); | ||
|
|
||
| if (sourceYaml != null) | ||
| { | ||
| changelogFilename = input.ExistingChangelogFilename != null | ||
| ? _fileSystem.Path.GetFileName(input.ExistingChangelogFilename) | ||
| : _fileSystem.Path.GetFileName(sourceYaml); | ||
|
|
||
| if (input.ExistingChangelogFilename != null) | ||
| _logger.LogInformation("Reusing existing filename {Filename} for stable path on branch", changelogFilename); | ||
|
|
||
| var destYaml = _fileSystem.Path.Combine(input.OutputDir, changelogFilename); | ||
| _fileSystem.File.Copy(sourceYaml, destYaml, overwrite: true); | ||
| _logger.LogInformation("Copied changelog YAML: {Source} → {Dest}", sourceYaml, destYaml); | ||
| } | ||
| else | ||
| { | ||
| collector.EmitError(input.StagingDir, "No generated changelog YAML found in staging directory"); | ||
| status = PrEvaluationResult.Error; | ||
| } | ||
| } | ||
|
|
||
| var config = await _configLoader.LoadChangelogConfiguration(collector, input.Config, ctx); | ||
| var createRules = config?.Rules?.Create; | ||
| var changelogDir = config?.Bundle?.Directory ?? "docs/changelog"; | ||
|
|
||
| var statusString = status.ToStringFast(true); | ||
| var metadata = new ChangelogArtifactMetadata | ||
| { | ||
| PrNumber = input.PrNumber, | ||
| HeadRef = input.HeadRef, | ||
| HeadSha = input.HeadSha, | ||
| Status = statusString, | ||
| IsFork = input.IsFork, | ||
| HeadRepo = input.HeadRepo, | ||
| CanCommit = input.CanCommit, | ||
| MaintainerCanModify = input.MaintainerCanModify, | ||
| LabelTable = input.LabelTable, | ||
| ProductLabelTable = input.ProductLabelTable, | ||
| SkipLabels = input.SkipLabels, | ||
| ConfigFile = input.Config, | ||
| ChangelogDir = changelogDir, | ||
| ChangelogFilename = changelogFilename, | ||
| CreateRules = createRules | ||
| }; | ||
|
|
||
| var metadataPath = _fileSystem.Path.Combine(input.OutputDir, "metadata.json"); | ||
| var json = JsonSerializer.Serialize(metadata, ChangelogArtifactMetadataJsonContext.Default.ChangelogArtifactMetadata); | ||
| await _fileSystem.File.WriteAllTextAsync(metadataPath, json, ctx); | ||
| _logger.LogInformation("Wrote artifact metadata to {Path}", metadataPath); | ||
|
|
||
| await coreService.SetOutputAsync("status", statusString); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| private string? FindStagingYaml(string stagingDir) | ||
| { | ||
| if (!_fileSystem.Directory.Exists(stagingDir)) | ||
| return null; | ||
|
|
||
| var yamlFiles = _fileSystem.Directory.GetFiles(stagingDir, "*.yaml"); | ||
| if (yamlFiles.Length == 0) | ||
| return null; | ||
|
|
||
| if (yamlFiles.Length > 1) | ||
| { | ||
| _logger.LogError("Multiple YAML files found in staging directory: {Files}", string.Join(", ", yamlFiles)); | ||
| return null; | ||
| } | ||
|
|
||
| return yamlFiles[0]; | ||
| } | ||
|
|
||
| internal static PrEvaluationResult ResolveStatus(string evaluateStatus, string generateOutcome) | ||
| { | ||
| if (string.Equals(evaluateStatus, ChangelogPrEvaluationService.ProceedStatus, StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| return generateOutcome.Equals("success", StringComparison.OrdinalIgnoreCase) | ||
| ? PrEvaluationResult.Success | ||
| : PrEvaluationResult.Error; | ||
| } | ||
|
|
||
| return PrEvaluationResultExtensions.TryParse(evaluateStatus, out var parsed, ignoreCase: true, allowMatchingMetadataAttribute: true) | ||
| ? parsed | ||
| : PrEvaluationResult.Error; | ||
| } | ||
| } | ||
13 changes: 13 additions & 0 deletions
13
src/services/Elastic.Changelog/Evaluation/EvaluateArtifactArguments.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,13 @@ | ||
| // 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.Changelog.Evaluation; | ||
|
|
||
| /// <summary>Arguments for the changelog evaluate-artifact command (commit workflow).</summary> | ||
| public record EvaluateArtifactArguments | ||
| { | ||
| public required string MetadataPath { get; init; } | ||
| public required string Owner { get; init; } | ||
| public required string Repo { get; init; } | ||
| } |
31 changes: 31 additions & 0 deletions
31
src/services/Elastic.Changelog/Evaluation/PrepareArtifactArguments.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,31 @@ | ||
| // 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.Changelog.Evaluation; | ||
|
|
||
| /// <summary>Arguments for the changelog prepare-artifact command.</summary> | ||
| public record PrepareArtifactArguments | ||
| { | ||
| public required string StagingDir { get; init; } | ||
| public required string OutputDir { get; init; } | ||
| public required string EvaluateStatus { get; init; } | ||
| public required string GenerateOutcome { get; init; } | ||
| public required int PrNumber { get; init; } | ||
| public required string HeadRef { get; init; } | ||
| public required string HeadSha { get; init; } | ||
| public bool IsFork { get; init; } | ||
| public string? HeadRepo { get; init; } | ||
| public bool CanCommit { get; init; } | ||
| public bool MaintainerCanModify { get; init; } | ||
| public string? LabelTable { get; init; } | ||
| public string? ProductLabelTable { get; init; } | ||
| public string? SkipLabels { get; init; } | ||
| public string? Config { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// Filename of a previously committed changelog for this PR. | ||
| /// When set, the staging file is renamed to match so the same path is overwritten on the branch. | ||
| /// </summary> | ||
| public string? ExistingChangelogFilename { get; init; } | ||
| } |
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.