Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// 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)
{
if (!_fileSystem.File.Exists(input.MetadataPath))
{
_logger.LogInformation("Metadata file not found at {Path}, nothing to evaluate", input.MetadataPath);
return true;
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

ChangelogArtifactMetadata? metadata;
try
{
var artifactMetadataJson = await _fileSystem.File.ReadAllTextAsync(input.MetadataPath, ctx);
metadata = JsonSerializer.Deserialize(artifactMetadataJson, ChangelogArtifactMetadataJsonContext.Default.ChangelogArtifactMetadata);
}
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;
}
}
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;
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];
Comment thread
cotti marked this conversation as resolved.
}

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;
}
}
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; }
}
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; }
}
2 changes: 1 addition & 1 deletion src/services/Elastic.Changelog/GitHub/GitHubPrService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,6 @@ private sealed class GitHubCommitListItem
[JsonSerializable(typeof(List<GitHubCommitListItem>))]
private sealed partial class GitHubPrJsonContext : JsonSerializerContext;

[GeneratedRegex(@"https://github\.com/([a-zA-Z0-9_-]+/[a-zA-Z0-9_-]+)/pull/(\d+)", RegexOptions.IgnoreCase, "en-CA")]
[GeneratedRegex(@"https://github\.com/([a-zA-Z0-9_-]+/[a-zA-Z0-9_-]+)/pull/(\d+)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)]
private static partial Regex MyRegex();
}
Loading
Loading