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
70 changes: 27 additions & 43 deletions project-demos/pr-staging-deploy/Apps/PrStagingDeployApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private record PrRow(
var github = this.UseService<GitHubApiClient>();
var deploySvc = this.UseService<StagingDeployService>();
var prComments = this.UseService<PrStagingDeployCommentService>();
var commentUpdateQueue = this.UseService<PrStagingDeployCommentUpdateQueue>();
var errorWatcher = this.UseService<StagingErrorWatcherQueue>();
var sliplane = this.UseService<SliplaneStagingClient>();
var client = this.UseService<IClientProvider>();
var refreshToken = this.UseRefreshToken();
Expand Down Expand Up @@ -219,10 +219,7 @@ private record PrRow(
{
var prsThatHadServices = rowList.Where(r => !RowLooksLikeNoStagingYet(r)).ToList();
foreach (var pr in prsThatHadServices)
{
_ = prComments.TryPostOrUpdateStagingCommentAsync(
owner, repo, pr.Number, null, null, "Deleted", null);
}
_ = prComments.TryPostStagingRemovedAsync(owner, repo, pr.Number);
}

overviewQuery.Mutator.Revalidate();
Expand Down Expand Up @@ -293,8 +290,7 @@ async Task DeployBranchAsync(string branchName, int prNumber, bool clearMessageF
var owner = config["GitHub:Owner"] ?? "";
var repo = config["GitHub:Repo"] ?? "";

var result = await deploySvc.DeployBranchAsync(t, branchName, prNumber);
ShowMessage(result.Message, !result.Success);
var result = await deploySvc.DeployBranchAsync(t, branchName, prNumber, null, owner, repo);

deployingBranches.Set(prev =>
{
Expand All @@ -303,42 +299,39 @@ async Task DeployBranchAsync(string branchName, int prNumber, bool clearMessageF
return next;
});

if (result.SkippedBecausePrNotOpen)
{
ShowMessage(result.Message, false);
return;
}

ShowMessage(result.Message, !result.Success);

if (result.Success)
{
overviewQuery.Mutator.Revalidate();

if (!string.IsNullOrEmpty(owner) && !string.IsNullOrEmpty(repo))
{
if (string.IsNullOrEmpty(result.DocsServiceId) && string.IsNullOrEmpty(result.SamplesServiceId))
if (result.Success && (!string.IsNullOrEmpty(result.DocsServiceId) || !string.IsNullOrEmpty(result.SamplesServiceId)))
{
await prComments.TryPostOrUpdateStagingCommentAsync(
owner, repo, prNumber,
docsUrl: null, samplesUrl: null,
status: "Deploy failed",
logLines: new[] { "No Sliplane services were created: " + TruncLine(result.Message, 200) });
return;
await prComments.TryPostStagingAsync(
owner, repo, prNumber, result.DocsUrl, result.SamplesUrl, error: null);
if (!string.IsNullOrEmpty(result.DocsServiceId) || !string.IsNullOrEmpty(result.SamplesServiceId))
await errorWatcher.EnqueueAsync(new StagingErrorWatchRequest(
owner, repo, prNumber, result.DocsServiceId, result.SamplesServiceId));
}
else
{
await prComments.TryPostStagingAsync(
owner, repo, prNumber, null, null, TruncLine(result.Message, 500));
}

await commentUpdateQueue.EnqueueAsync(new PrStagingDeployCommentUpdateRequest(
Owner: owner,
Repo: repo,
PrNumber: prNumber,
BranchName: branchName,
DocsServiceId: result.DocsServiceId,
SamplesServiceId: result.SamplesServiceId));
await prComments.TryNotifyDeployQueuedAsync(
owner, repo, prNumber, PrStagingDeployCommentService.CommentStatusDeployQueued);

return;
}
}
else if (!string.IsNullOrEmpty(owner) && !string.IsNullOrEmpty(repo))
{
await prComments.TryPostOrUpdateStagingCommentAsync(
owner, repo, prNumber,
docsUrl: null, samplesUrl: null,
status: "Deploy failed",
logLines: new[] { TruncLine(result.Message, 240) });
await prComments.TryPostStagingAsync(
owner, repo, prNumber, null, null, TruncLine(result.Message, 500));
}
}
catch (Exception ex)
Expand Down Expand Up @@ -370,21 +363,12 @@ async Task DeleteBranchAsync(string branchName, int prNumber)
{
overviewQuery.Mutator.Revalidate();
if (!string.IsNullOrEmpty(owner) && !string.IsNullOrEmpty(repo))
{
await prComments.TryPostOrUpdateStagingCommentAsync(
owner, repo, prNumber,
docsUrl: null, samplesUrl: null,
status: "Deleted",
logLines: null);
}
await prComments.TryPostStagingRemovedAsync(owner, repo, prNumber);
}
else if (!string.IsNullOrEmpty(owner) && !string.IsNullOrEmpty(repo))
{
await prComments.TryPostOrUpdateStagingCommentAsync(
owner, repo, prNumber,
docsUrl: null, samplesUrl: null,
status: "Delete failed",
logLines: new[] { TruncLine(result.Message, 240) });
await prComments.TryPostStagingAsync(
owner, repo, prNumber, null, null, TruncLine(result.Message, 500));
}
}
catch (Exception ex) { ShowMessage(ex.Message, true); }
Expand Down
4 changes: 2 additions & 2 deletions project-demos/pr-staging-deploy/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
server.Services.AddScoped<SliplaneStagingClient>();
server.Services.AddScoped<StagingDeployService>();
server.Services.AddScoped<PrStagingDeployCommentService>();
server.Services.AddSingleton<PrStagingDeployCommentUpdateQueue>();
server.Services.AddHostedService<PrStagingDeployCommentUpdateBackgroundService>();
server.Services.AddScoped<GitHubWebhookHandler>();
server.Services.AddSingleton<StagingErrorWatcherQueue>();
server.Services.AddHostedService<StagingErrorWatcherBackgroundService>();
server.Services.AddSingleton<Microsoft.AspNetCore.Hosting.IStartupFilter, WebhookEndpointFilter>();
server.Services.AddHostedService<ExpiryCleanupBackgroundService>();

Expand Down
29 changes: 29 additions & 0 deletions project-demos/pr-staging-deploy/Services/GitHubApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,32 @@ public async Task<List<GitHubPullRequest>> GetPullRequestsAsync(string owner, st
return doc.RootElement.GetProperty("head").GetProperty("ref").GetString();
}

/// <summary>Current PR state from GitHub. Used to avoid deploying after a fast merge/close (race with webhooks).</summary>
public async Task<GitHubPullRequestMergeInfo> GetPullRequestMergeInfoAsync(
string owner,
string repo,
int prNumber,
string? token,
CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(token))
return new GitHubPullRequestMergeInfo(Found: false, IsOpen: false, Merged: false);
var client = CreateClient(token);
using var response = await client.GetAsync(
$"https://api.github.com/repos/{owner}/{repo}/pulls/{prNumber}",
cancellationToken);
if (!response.IsSuccessStatusCode)
return new GitHubPullRequestMergeInfo(Found: false, IsOpen: false, Merged: false);

var json = await response.Content.ReadAsStringAsync(cancellationToken);
using var doc = JsonDocument.Parse(json);
var root = doc.RootElement;
var state = root.GetProperty("state").GetString() ?? "";
var merged = root.TryGetProperty("merged", out var mEl) && mEl.GetBoolean();
var isOpen = state.Equals("open", StringComparison.OrdinalIgnoreCase);
return new GitHubPullRequestMergeInfo(Found: true, IsOpen: isOpen, Merged: merged);
}

/// <summary>Open PR whose head branch matches <paramref name="headBranch"/> (same repo: <c>owner:branch</c>).</summary>
public async Task<int?> FindOpenPullRequestNumberByHeadBranchAsync(
string owner,
Expand Down Expand Up @@ -222,3 +248,6 @@ private HttpClient CreateClient(string? token)
return client;
}
}

/// <param name="Found">False if the API request failed (caller may treat as unknown and proceed).</param>
public readonly record struct GitHubPullRequestMergeInfo(bool Found, bool IsOpen, bool Merged);
Loading
Loading