add: webhook
This commit is contained in:
@@ -1,13 +1,26 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk.Worker">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net9.0</TargetFramework>
|
<TargetFramework>net9.0</TargetFramework>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>disable</Nullable>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<UserSecretsId>dotnet-Alchegos.Gitea.Webhook-61e3506a-2cdc-487d-86e8-706d75fc08ee</UserSecretsId>
|
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.2"/>
|
<Content Include="..\.dockerignore">
|
||||||
|
<Link>.dockerignore</Link>
|
||||||
|
</Content>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="RabbitMQ.Client" Version="7.1.1" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\Alchegos.Core\Alchegos.Core.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
23
Dockerfile
Normal file
23
Dockerfile
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base
|
||||||
|
USER $APP_UID
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
EXPOSE 8080
|
||||||
|
|
||||||
|
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
|
||||||
|
ARG BUILD_CONFIGURATION=Release
|
||||||
|
WORKDIR /src
|
||||||
|
COPY ["Alchegos.Gitea.Webhook/Alchegos.Gitea.Webhook.csproj", "Alchegos.Gitea.Webhook/"]
|
||||||
|
RUN dotnet restore "Alchegos.Gitea.Webhook/Alchegos.Gitea.Webhook.csproj"
|
||||||
|
COPY . .
|
||||||
|
WORKDIR "/src/Alchegos.Gitea.Webhook"
|
||||||
|
RUN dotnet build "Alchegos.Gitea.Webhook.csproj" -c $BUILD_CONFIGURATION -o /app/build
|
||||||
|
|
||||||
|
FROM build AS publish
|
||||||
|
ARG BUILD_CONFIGURATION=Release
|
||||||
|
RUN dotnet publish "Alchegos.Gitea.Webhook.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
|
||||||
|
|
||||||
|
FROM base AS final
|
||||||
|
WORKDIR /app
|
||||||
|
COPY --from=publish /app/publish .
|
||||||
|
ENTRYPOINT ["dotnet", "Alchegos.Gitea.Webhook.dll"]
|
||||||
39
Models/GiteaWebhookPayload.cs
Normal file
39
Models/GiteaWebhookPayload.cs
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
namespace Alchegos.Gitea.Webhook.Models;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
public class GiteaWebhookPayload
|
||||||
|
{
|
||||||
|
[JsonPropertyName("repository")]
|
||||||
|
public Repository? Repository { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("pusher")]
|
||||||
|
public User? Pusher { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("commits")]
|
||||||
|
public List<CommitInfo>? Commits { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("head_commit")]
|
||||||
|
public CommitInfo? HeadCommit { get; set; }
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Repository
|
||||||
|
{
|
||||||
|
[JsonPropertyName("name")]
|
||||||
|
public string? Name { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class User
|
||||||
|
{
|
||||||
|
[JsonPropertyName("username")]
|
||||||
|
public string? UserName { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class CommitInfo
|
||||||
|
{
|
||||||
|
[JsonPropertyName("id")]
|
||||||
|
public string? Id { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("message")]
|
||||||
|
public string? Message { get; set; }
|
||||||
|
}
|
||||||
42
Program.cs
42
Program.cs
@@ -1,7 +1,39 @@
|
|||||||
using Alchegos.Gitea.Webhook;
|
using Alchegos.Core;
|
||||||
|
using Alchegos.Core.Services.RabbitMQ;
|
||||||
|
|
||||||
var builder = Host.CreateApplicationBuilder(args);
|
|
||||||
builder.Services.AddHostedService<Worker>();
|
|
||||||
|
|
||||||
var host = builder.Build();
|
GlobalRegistry.Instance.Start();
|
||||||
host.Run();
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
builder.Logging.ClearProviders();
|
||||||
|
builder.Logging.AddConsole();
|
||||||
|
builder.Services.Configure<RabbitConnectionOptions>(
|
||||||
|
builder.Configuration.GetSection("RabbitMQ")
|
||||||
|
);
|
||||||
|
|
||||||
|
builder.Services.AddSingleton<IRabbitService, RabbitService>();
|
||||||
|
builder.Services.AddSingleton<IRabbitPublisher, RabbitPublisher>();
|
||||||
|
|
||||||
|
builder.Services.AddSingleton<IRabbitPublisher, RabbitPublisher>();
|
||||||
|
var app = builder.Build();
|
||||||
|
var logger = app.Services.GetRequiredService<ILogger<Program>>();
|
||||||
|
logger.LogInformation("Starting web hook /webhook/gitea");
|
||||||
|
app.MapPost("/webhook/gitea", async (HttpRequest request, HttpResponse response, IRabbitPublisher publisher) =>
|
||||||
|
{
|
||||||
|
logger.LogInformation("Received gitea webhook request");
|
||||||
|
using var reader = new StreamReader(request.Body);
|
||||||
|
var jsonBody = await reader.ReadToEndAsync();
|
||||||
|
|
||||||
|
var giteaEvent = request.Headers["X-Gitea-Event"].ToString() ?? "unknown";
|
||||||
|
|
||||||
|
logger.LogInformation($"Received gitea webhook post: {giteaEvent}");
|
||||||
|
logger.LogInformation($" {jsonBody}");
|
||||||
|
|
||||||
|
await publisher.PublishAsync(
|
||||||
|
exchange: "gitea.webhook.exchange",
|
||||||
|
routingKey: giteaEvent,
|
||||||
|
message: jsonBody
|
||||||
|
);
|
||||||
|
response.StatusCode = 200;
|
||||||
|
await response.WriteAsync(jsonBody);
|
||||||
|
});
|
||||||
|
app.Run();
|
||||||
24
Worker.cs
24
Worker.cs
@@ -1,24 +0,0 @@
|
|||||||
namespace Alchegos.Gitea.Webhook;
|
|
||||||
|
|
||||||
public class Worker : BackgroundService
|
|
||||||
{
|
|
||||||
private readonly ILogger<Worker> _logger;
|
|
||||||
|
|
||||||
public Worker(ILogger<Worker> logger)
|
|
||||||
{
|
|
||||||
_logger = logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
||||||
{
|
|
||||||
while (!stoppingToken.IsCancellationRequested)
|
|
||||||
{
|
|
||||||
if (_logger.IsEnabled(LogLevel.Information))
|
|
||||||
{
|
|
||||||
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
|
|
||||||
}
|
|
||||||
|
|
||||||
await Task.Delay(1000, stoppingToken);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -4,5 +4,11 @@
|
|||||||
"Default": "Information",
|
"Default": "Information",
|
||||||
"Microsoft.Hosting.Lifetime": "Information"
|
"Microsoft.Hosting.Lifetime": "Information"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"RabbitMQ": {
|
||||||
|
"HostName": "rabbitmq",
|
||||||
|
"Username": "guest",
|
||||||
|
"Password": "guest",
|
||||||
|
"Port": "5672"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user