Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion docs/cli-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -6837,7 +6837,7 @@
],
"name": "serverless",
"summary": "Bundle the release notes of a serverless promotion, from the previously published endpoint ref to serviceVersion.",
"notes": "The previous endpoint ref is the last different production-noncanary-ds-5 version in the\nservice's versions.yaml history in elastic/serverless-gitops. Each repository's\ndocs/changelog.yml is read at its end ref, and one bundle per repository is built with\nits serverless-release profile from the commit range. Kibana produces one bundle.\nElasticsearch produces two: elasticsearch-serverless over the serverless range, and\nelasticsearch, a submodule of it, over the range of submodule commits pinned at the two refs.\n\n\nRequires GITHUB_TOKEN with read access to elastic/serverless-gitops and the service repositories.",
"notes": "The previous endpoint ref is the last different production-noncanary-ds-5 version in the\nservice's versions.yaml history in elastic/serverless-gitops. Each repository's\ndocs/changelog.yml is read at its end ref, and one bundle per repository is built with\nits serverless-release profile from the commit range. Kibana produces one bundle.\nElasticsearch produces two: elasticsearch-serverless over the serverless range, and\nelasticsearch, a submodule of it, over the range of submodule commits pinned at the two refs.\n\n\n\nA range with no notes (for example only dependency bumps) is not an error: it writes a bundle with no entries, so\nthe release stays on record, and the renderer skips it. A pull request whose metadata could not be read is still an error.\nBundle files end with the short ref of the promotion ({repo}-{product}-{date}-{short ref}.yaml), so a later release on the\nsame day, with or without notes, never has the name of an earlier one.\n\n\nRequires GITHUB_TOKEN with read access to elastic/serverless-gitops and the service repositories.",
"usage": "docs-builder release serverless <service> <service-version> [options]",
"examples": [],
"parameters": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ public record BundleChangelogsArguments
/// </summary>
public Action? OnEmptyRange { get; init; }

/// <summary>
/// When true, a commit range with nothing to publish still writes a bundle: the profile's products, the release date and
/// <c>git_ref</c>, and no entries. A release that shipped without notes then stays on record, and the renderer skips it.
/// A pull request whose metadata could not be read is still an error. Takes the place of <see cref="OnEmptyRange"/>.
/// </summary>
public bool WriteEmptyRange { get; init; }

/// <summary>
/// Directory containing changelog YAML files. null = use config default.
/// </summary>
Expand Down Expand Up @@ -534,7 +541,8 @@ private async Task<bool> BuildAndWriteBundle(
ChangelogConfiguration? config,
IReadOnlyList<MatchedChangelogFile> entries,
string outputPath,
Cancel ctx
Cancel ctx,
bool rangeIsIncomplete = false
)
{
// Apply rules.bundle secondary filter (three modes: none, global content, per-product context).
Expand All @@ -557,9 +565,17 @@ Cancel ctx
};
}

if (filteredEntries.Count == 0)
// An empty result is a release with no notes only when the whole range was read. A pull request whose metadata
// could not be fetched may have held the notes, so that is an error, not an empty release.
var emptyIsAllowed = input.WriteEmptyRange && !rangeIsIncomplete;
if (filteredEntries.Count == 0 && !emptyIsAllowed)
{
collector.EmitError(string.Empty, "No changelog entries remained after applying rules.bundle filter");
collector.EmitError(
string.Empty,
input.WriteEmptyRange
? "No changelog entries remained after applying rules.bundle filter, and the metadata of some pull requests in the range could not be read. A failed fetch is not recorded as a release with no notes."
: "No changelog entries remained after applying rules.bundle filter"
);
return false;
}

Expand Down Expand Up @@ -643,11 +659,11 @@ Cancel ctx
bundleData = bundleData with { GitRef = input.EndGitRef };

// Write bundle file
await WriteBundleFileAsync(bundleData, outputPath, ctx);
var writtenPath = await WriteBundleFileAsync(bundleData, outputPath, ctx);

// Notify the caller of the resolved output path (used by ChangelogCommand.Bundle to emit
// bundle_path as a GitHub Actions step output so callers don't need to predict the path).
input.OnBundlePathResolved?.Invoke(outputPath);
input.OnBundlePathResolved?.Invoke(writtenPath);

return true;
}
Expand Down Expand Up @@ -1101,37 +1117,44 @@ Cancel ctx
return false;

if (allEntries.Count == 0)
return FinishEmptyRange(collector, input, result, $"{owner}/{sourcing.Repo}");
return await FinishEmptyRange(collector, input, config, result, ($"{owner}/{sourcing.Repo}", outputPath), ctx);

return await BuildAndWriteBundle(collector, input, config, allEntries, outputPath, ctx);
var rangeIsIncomplete = result.Report.Rows.Any(row => row.Source == GitRangePrSourceKind.Missing);
return await BuildAndWriteBundle(collector, input, config, allEntries, outputPath, ctx, rangeIsIncomplete);
}

/// <summary>
/// A range with nothing to publish. DRA bundling allows it: the product shipped with no user-facing changes.
/// A caller can ask for an empty bundle instead (<see cref="BundleChangelogsArguments.WriteEmptyRange"/>), which keeps the release on record.
/// A pull request whose metadata could not be read is still an error, so a failed fetch is not reported as an empty release.
/// </summary>
private bool FinishEmptyRange(
private async Task<bool> FinishEmptyRange(
IDiagnosticsCollector collector,
BundleChangelogsArguments input,
ChangelogConfiguration? config,
GitRangeEntryResolutionResult result,
string repository
(string Repository, string OutputPath) target,
Cancel ctx
)
{
if (input.OnEmptyRange is not null && RangeHasNoNotes(result))
if ((input.WriteEmptyRange || input.OnEmptyRange is not null) && RangeHasNoNotes(result))
{
_logger.LogInformation(
"No changelog entries in commit range {Start}..{End} of {Repository}. The release has no notes.",
input.StartGitRef,
input.EndGitRef,
repository
target.Repository
);
input.OnEmptyRange();
if (input.WriteEmptyRange)
return await BuildAndWriteBundle(collector, input, config, [], target.OutputPath, ctx);

input.OnEmptyRange!();
return true;
}

collector.EmitError(
string.Empty,
$"No changelog entries could be resolved for commit range {input.StartGitRef}..{input.EndGitRef} of {repository}."
$"No changelog entries could be resolved for commit range {input.StartGitRef}..{input.EndGitRef} of {target.Repository}."
);
return false;
}
Expand Down Expand Up @@ -2259,7 +2282,8 @@ HashSet<string> issuesToMatch
};
}

private async Task WriteBundleFileAsync(Bundle bundledData, string outputPath, Cancel ctx)
/// <summary>Writes the bundle and returns the path it was written to, which is not <paramref name="outputPath"/> when that file already exists.</summary>
private async Task<string> WriteBundleFileAsync(Bundle bundledData, string outputPath, Cancel ctx)
{
// Generate bundled YAML
var bundledYaml = ReleaseNotesSerialization.SerializeBundle(bundledData);
Expand All @@ -2286,6 +2310,7 @@ private async Task WriteBundleFileAsync(Bundle bundledData, string outputPath, C
var normalizedYaml = ChangelogUtf8Normalization.StripLeadingUtf8BomChar(bundledYaml);
await _fileSystem.File.WriteAllTextAsync(outputPath, normalizedYaml, Utf8NoBom, ctx);
_logger.LogInformation("Created bundled changelog: {OutputPath}", outputPath);
return outputPath;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,21 @@ public static bool IsFinalSlicePromotion(string commitMessage, string finalSlice
public static string? ParseFinalSliceVersion(string versionsYaml, string finalSlice) =>
SliceVersionRegex().Matches(versionsYaml).FirstOrDefault(m => m.Groups["slice"].Value == finalSlice)?.Groups["sha"].Value;

/// <summary>The first 12 characters of a ref, in lower case. It names a promotion in serverless-gitops and in its review pull request.</summary>
public static string ShortRef(string gitRef) => (gitRef.Length > 12 ? gitRef[..12] : gitRef).ToLowerInvariant();

/// <summary>
/// Adds the short ref of the promotion to a bundle file name. Bundle names are <c>{repo}-{product}-{date}</c>, so two promotions
/// on one day would share a name and a later release, including one with no notes, could replace an earlier one when published.
/// A name that already ends with the short ref is unchanged.
/// </summary>
public static string WithShortRef(string fileName, string serviceVersion)
{
var shortRef = ShortRef(serviceVersion);
var stem = Path.GetFileNameWithoutExtension(fileName);
return stem.EndsWith($"-{shortRef}", StringComparison.Ordinal) ? fileName : $"{stem}-{shortRef}{Path.GetExtension(fileName)}";
}

/// <summary>Gitops stores 12-character hashes and callers may send a full SHA in any case,
/// so two refs are the same when their first 12 characters match, case-insensitively.</summary>
public static bool SameRef(string a, string b)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ IEnvironmentVariables environmentVariables
/// Elasticsearch produces two: <c>elasticsearch-serverless</c> over the serverless range, and
/// <c>elasticsearch</c>, a submodule of it, over the range of submodule commits pinned at the two refs.
/// </para>
/// <para>
/// A range with no notes (for example only dependency bumps) is not an error: it writes a bundle with no entries, so
/// the release stays on record, and the renderer skips it. A pull request whose metadata could not be read is still an error.
/// Bundle files end with the short ref of the promotion (<c>{repo}-{product}-{date}-{short ref}.yaml</c>), so a later release on the
/// same day, with or without notes, never has the name of an earlier one.
/// </para>
/// <para>Requires <c>GITHUB_TOKEN</c> with read access to <c>elastic/serverless-gitops</c> and the service repositories.</para>
/// </remarks>
/// <param name="service">Serverless service, as emitted by gpctl (for example <c>kibana</c>).</param>
Expand Down Expand Up @@ -113,7 +119,18 @@ public async Task<int> Serverless(
// One bundle per repository: the service repository over the serverless range, then each
// submodule over the range of commits the service repository pins at the two refs.
var bundles = new List<string>();
var primary = await BundleRangeAsync(http, svc.Repository, svc, startRef, serviceVersion, bundleVersion, rootOutput, dryRun, ctx);
var primary = await BundleRangeAsync(
http,
svc.Repository,
svc,
startRef,
serviceVersion,
bundleVersion,
rootOutput,
dryRun,
serviceVersion,
ctx
);
if (!primary.Success)
return 1;
bundles.AddRange(primary.Paths);
Expand Down Expand Up @@ -141,7 +158,18 @@ public async Task<int> Serverless(
continue;
}
_logger.LogInformation("Submodule {Repository}: {Start}..{End}", submodule.Repository, startSha, endSha);
var result = await BundleRangeAsync(http, submodule.Repository, svc, startSha, endSha, bundleVersion, rootOutput, dryRun, ctx);
var result = await BundleRangeAsync(
http,
submodule.Repository,
svc,
startSha,
endSha,
bundleVersion,
rootOutput,
dryRun,
serviceVersion,
ctx
);
if (!result.Success)
return 1;
bundles.AddRange(result.Paths);
Expand All @@ -165,6 +193,7 @@ public async Task<int> Serverless(
string bundleVersion,
DirectoryInfo rootOutput,
bool dryRun,
string serviceVersion,
CancellationToken ctx
)
{
Expand All @@ -189,6 +218,7 @@ CancellationToken ctx
StartGitRef = startRef,
EndGitRef = endRef,
BaseRef = svc.BaseBranch,
WriteEmptyRange = true,
Comment thread
infra-vault-gh-plugin-prod[bot] marked this conversation as resolved.
Resolved
DryRun = dryRun,
OnBundlePathResolved = path => paths.Add(path)
};
Expand All @@ -202,14 +232,23 @@ CancellationToken ctx
);
var bundleService = new ChangelogBundlingService(logFactory, ChangelogFileSystem.FromWorkingDirectory(), configurationContext);
var success = await bundleService.BundleChangelogs(collector, arguments, ctx);
return (success, paths);
return (success, success ? [.. paths.Select(path => AddShortRef(path, serviceVersion))] : paths);
}
finally
{
File.Delete(tempConfig);
}
}

// A bundle is named {repo}-{product}-{date}. The short ref of the promotion keeps two promotions on one day apart.
private static string AddShortRef(string path, string serviceVersion)
{
var target = Path.Join(Path.GetDirectoryName(path), ServerlessPromotion.WithShortRef(Path.GetFileName(path), serviceVersion));
if (target != path)
File.Move(path, target, overwrite: true);
Comment thread
infra-vault-gh-plugin-prod[bot] marked this conversation as resolved.
Resolved
return target;
}

private async Task<string?> GetSubmoduleShaAsync(
HttpClient http,
string repository,
Expand Down
Loading
Loading