Implement alpha foundation workflow and GitHub sync
This commit is contained in:
parent
5d62ea2df4
commit
3fd655e1f2
37 changed files with 7352 additions and 365 deletions
|
|
@ -3,12 +3,14 @@ using System.Security.Claims;
|
|||
using System.Text.Json;
|
||||
using Bifrost.Web.Configuration;
|
||||
using Bifrost.Web.Data;
|
||||
using Bifrost.Web.Features.GitHub;
|
||||
using Bifrost.Web.Features.Membership;
|
||||
using Bifrost.Web.Features.Shared;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Microsoft.AspNetCore.Authentication.OAuth;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.HttpLogging;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
|
|
@ -18,10 +20,18 @@ builder.Services
|
|||
.AddOptions<GitHubOAuthOptions>()
|
||||
.BindConfiguration(GitHubOAuthOptions.SectionName);
|
||||
|
||||
builder.Services
|
||||
.AddOptions<GitHubAppOptions>()
|
||||
.BindConfiguration(GitHubAppOptions.SectionName);
|
||||
|
||||
builder.Services
|
||||
.AddOptions<BootstrapOptions>()
|
||||
.BindConfiguration(BootstrapOptions.SectionName);
|
||||
|
||||
builder.Services
|
||||
.AddOptions<BifrostHostOptions>()
|
||||
.BindConfiguration(BifrostHostOptions.SectionName);
|
||||
|
||||
builder.Services.AddRazorPages(options =>
|
||||
{
|
||||
options.Conventions.AuthorizePage("/Membership/Status");
|
||||
|
|
@ -36,12 +46,25 @@ builder.Services.AddDbContext<BifrostDbContext>(options =>
|
|||
options.UseNpgsql(connectionString);
|
||||
});
|
||||
|
||||
builder.Services.AddHttpLogging(options =>
|
||||
{
|
||||
options.LoggingFields =
|
||||
HttpLoggingFields.RequestMethod |
|
||||
HttpLoggingFields.RequestPath |
|
||||
HttpLoggingFields.ResponseStatusCode |
|
||||
HttpLoggingFields.Duration;
|
||||
});
|
||||
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
builder.Services.AddSingleton(TimeProvider.System);
|
||||
builder.Services.AddScoped<ICurrentBifrostActorAccessor, CurrentBifrostActorAccessor>();
|
||||
builder.Services.AddScoped<AuditTrailService>();
|
||||
builder.Services.AddScoped<DashboardSnapshotService>();
|
||||
builder.Services.AddScoped<MembershipSynchronizationService>();
|
||||
builder.Services.AddScoped<GitHubWebhookService>();
|
||||
builder.Services.AddScoped<ReadinessService>();
|
||||
builder.Services.AddScoped<ApplicationBootstrapper>();
|
||||
builder.Services.AddSingleton<StartupConfigurationValidator>();
|
||||
builder.Services.AddScoped<IAuthorizationHandler, ActiveMemberAuthorizationHandler>();
|
||||
|
||||
builder.Services
|
||||
|
|
@ -107,12 +130,21 @@ builder.Services.AddAuthorization(options =>
|
|||
|
||||
var app = builder.Build();
|
||||
|
||||
app.Services.GetRequiredService<StartupConfigurationValidator>().Validate();
|
||||
await using (var scope = app.Services.CreateAsyncScope())
|
||||
{
|
||||
await scope.ServiceProvider
|
||||
.GetRequiredService<ApplicationBootstrapper>()
|
||||
.RunAsync(CancellationToken.None);
|
||||
}
|
||||
|
||||
if (!app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseExceptionHandler("/Error");
|
||||
app.UseHsts();
|
||||
}
|
||||
|
||||
app.UseHttpLogging();
|
||||
app.UseHttpsRedirection();
|
||||
app.UseStaticFiles();
|
||||
app.UseRouting();
|
||||
|
|
@ -120,6 +152,49 @@ app.UseAuthentication();
|
|||
app.UseMiddleware<GitHubMembershipSynchronizationMiddleware>();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapGet("/healthz", () => Results.Text("ok")).AllowAnonymous();
|
||||
|
||||
app.MapGet("/readyz", async (ReadinessService readinessService, CancellationToken cancellationToken) =>
|
||||
{
|
||||
var report = await readinessService.GetReportAsync(cancellationToken);
|
||||
return report.IsReady
|
||||
? Results.Text("ready")
|
||||
: Results.Text(
|
||||
string.Join(Environment.NewLine, report.Failures),
|
||||
statusCode: StatusCodes.Status503ServiceUnavailable);
|
||||
}).AllowAnonymous();
|
||||
|
||||
app.MapPost("/github/webhooks", async (
|
||||
HttpRequest request,
|
||||
GitHubWebhookService gitHubWebhookService,
|
||||
CancellationToken cancellationToken) =>
|
||||
{
|
||||
using var reader = new StreamReader(request.Body);
|
||||
var payload = await reader.ReadToEndAsync(cancellationToken);
|
||||
var eventName = request.Headers["X-GitHub-Event"].ToString();
|
||||
var deliveryId = request.Headers["X-GitHub-Delivery"].ToString();
|
||||
var signature = request.Headers["X-Hub-Signature-256"].ToString();
|
||||
|
||||
var result = await gitHubWebhookService.ProcessAsync(
|
||||
eventName,
|
||||
deliveryId,
|
||||
signature,
|
||||
payload,
|
||||
cancellationToken);
|
||||
|
||||
return result.Status switch
|
||||
{
|
||||
GitHubWebhookProcessStatus.Processed => Results.Text(result.Message, statusCode: StatusCodes.Status202Accepted),
|
||||
GitHubWebhookProcessStatus.Ignored => Results.Text(result.Message),
|
||||
GitHubWebhookProcessStatus.NotConfigured => Results.Text(
|
||||
result.Message,
|
||||
statusCode: StatusCodes.Status503ServiceUnavailable),
|
||||
_ => Results.Text(
|
||||
result.Message,
|
||||
statusCode: StatusCodes.Status401Unauthorized)
|
||||
};
|
||||
}).AllowAnonymous();
|
||||
|
||||
app.MapGet("/auth/sign-in", async (HttpContext httpContext, IOptions<GitHubOAuthOptions> gitHubOptions) =>
|
||||
{
|
||||
if (!gitHubOptions.Value.IsConfigured)
|
||||
|
|
|
|||
Loading…
Reference in a new issue