OPC # 0006: OPC Git Trunk-Based management
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
@@ -166,6 +166,7 @@ await using (var cmd = ds.CreateCommand("""
|
||||
// Idempotent column additions for schema migrations
|
||||
await using (var migCmd = ds.CreateCommand("""
|
||||
ALTER TABLE release_record ADD COLUMN IF NOT EXISTS opc_numbers TEXT[] NOT NULL DEFAULT '{}';
|
||||
ALTER TABLE release_record ADD COLUMN IF NOT EXISTS commit_sha VARCHAR(40);
|
||||
"""))
|
||||
await migCmd.ExecuteNonQueryAsync();
|
||||
|
||||
|
||||
@@ -787,6 +787,64 @@ public class PromotionService(IConfiguration config, ILogger<PromotionService> l
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns distinct, sorted OPC numbers for commits reachable from <paramref name="toSha"/>
|
||||
/// that are NOT reachable from <paramref name="fromSha"/> — i.e. the exact delta for this release.
|
||||
/// Falls back to <see cref="ExtractOpcNumbersAsync"/> (last 50 commits) when <paramref name="fromSha"/>
|
||||
/// is null (first-ever release for this environment).
|
||||
/// </summary>
|
||||
public Task<List<string>> ExtractOpcNumbersDeltaAsync(
|
||||
string repoName,
|
||||
string toSha,
|
||||
string? fromSha,
|
||||
CancellationToken ct = default) =>
|
||||
fromSha is null
|
||||
? ExtractOpcNumbersAsync(repoName, ct: ct)
|
||||
: Task.Run(() => ExtractOpcNumbersDeltaCore(repoName, toSha, fromSha), ct);
|
||||
|
||||
private List<string> ExtractOpcNumbersDeltaCore(string repoName, string toSha, string fromSha)
|
||||
{
|
||||
var repoPath = GetRepoPath(repoName);
|
||||
if (string.IsNullOrWhiteSpace(repoPath) || !Directory.Exists(repoPath))
|
||||
return [];
|
||||
try
|
||||
{
|
||||
using var repo = new Repository(repoPath);
|
||||
var toCommit = repo.Lookup<Commit>(toSha);
|
||||
var fromCommit = repo.Lookup<Commit>(fromSha);
|
||||
if (toCommit is null) return [];
|
||||
|
||||
var filter = fromCommit is null
|
||||
? new CommitFilter { IncludeReachableFrom = toCommit }
|
||||
: new CommitFilter { IncludeReachableFrom = toCommit, ExcludeReachableFrom = fromCommit };
|
||||
|
||||
var set = new HashSet<string>(StringComparer.Ordinal);
|
||||
foreach (var commit in repo.Commits.QueryBy(filter))
|
||||
foreach (System.Text.RegularExpressions.Match m in OpcTagPattern.Matches(commit.Message))
|
||||
set.Add($"OPC # {m.Groups[1].Value.PadLeft(4, '0')}");
|
||||
|
||||
return [.. set.OrderBy(x => x)];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning(ex, "ExtractOpcNumbersDelta failed for {Repo} {From}..{To}", repoName, fromSha[..7], toSha[..7]);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Returns the full HEAD SHA of <paramref name="branch"/> in <paramref name="repoName"/>, or null.</summary>
|
||||
public string? GetBranchTipSha(string repoName, string branch)
|
||||
{
|
||||
var repoPath = GetRepoPath(repoName);
|
||||
if (string.IsNullOrWhiteSpace(repoPath) || !Directory.Exists(repoPath)) return null;
|
||||
try
|
||||
{
|
||||
using var repo = new Repository(repoPath);
|
||||
return (repo.Branches[$"origin/{branch}"] ?? repo.Branches[branch])?.Tip?.Sha;
|
||||
}
|
||||
catch { return null; }
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>A single unreleased commit — carries full SHA for cherry-pick operations.</summary>
|
||||
|
||||
@@ -51,7 +51,12 @@ public class ReleaseService(
|
||||
return blocked;
|
||||
}
|
||||
|
||||
var record = await history.CreateReleaseAsync(targetEnv, ImageName);
|
||||
// Resolve the Clarity branch for this environment and stamp the HEAD SHA
|
||||
// before creating the record so we capture "what was deployed" accurately.
|
||||
var branch = targetEnv switch { "fdev" => "develop", "staging" => "staging", "uat" => "uat", _ => "main" };
|
||||
var currentSha = promotions.GetBranchTipSha("Clarity", branch);
|
||||
|
||||
var record = await history.CreateReleaseAsync(targetEnv, ImageName, currentSha);
|
||||
|
||||
try
|
||||
{
|
||||
@@ -183,9 +188,16 @@ public class ReleaseService(
|
||||
}
|
||||
finally
|
||||
{
|
||||
// Stamp OPC ticket numbers from recent commits on the target branch.
|
||||
var branch = targetEnv switch { "fdev" => "develop", "staging" => "staging", "uat" => "uat", _ => "main" };
|
||||
try { record.OpcNumbers = await promotions.ExtractOpcNumbersAsync("Clarity", branch, 50, ct); }
|
||||
// Stamp the exact OPC ticket numbers introduced by this release:
|
||||
// diff from previous release's SHA to this release's SHA on the Clarity branch.
|
||||
try
|
||||
{
|
||||
var prev = await history.GetLastSuccessfulReleaseForEnvAsync(targetEnv);
|
||||
// Exclude the current (in-flight) record — it's not succeeded yet
|
||||
var prevSha = prev?.Id == record.Id ? null : prev?.CommitSha;
|
||||
if (currentSha is not null)
|
||||
record.OpcNumbers = await promotions.ExtractOpcNumbersDeltaAsync("Clarity", currentSha, prevSha, ct);
|
||||
}
|
||||
catch { /* git not configured — continue without OPC stamp */ }
|
||||
|
||||
await history.UpdateReleaseAsync(record);
|
||||
|
||||
Reference in New Issue
Block a user