Initial commit

This commit is contained in:
Saphire 2022-04-23 04:10:21 +07:00
commit e879804433
Signed by: Saphire
GPG Key ID: B26EB7A1F07044C4
9 changed files with 286 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
/target
/bin
/obj
appsettings.Local.json

View File

@ -0,0 +1,24 @@
using Exchange.Lunar.Pigeonhole.Models;
using Microsoft.AspNetCore.Mvc;
namespace pigeonhole.Controllers;
[ApiController]
[Route("webhook")]
public class WebhookController : ControllerBase
{
private readonly ILogger<WebhookController> Logger;
public WebhookController(ILogger<WebhookController> logger)
{
Logger = logger;
}
[HttpGet("github")]
public async Task<IActionResult> Get(GithubDeploymentHookModel deploymentModel)
{
await Console.Out.WriteLineAsync("Webhook!");
Logger.LogInformation("Got a deploy webhook: {} in {} @ {}", deploymentModel.Action, deploymentModel.Repository.FullName, deploymentModel.Deployment.UpdatedAt);
return Ok();
}
}

90
Models/GithubModels.cs Normal file
View File

@ -0,0 +1,90 @@
using System.Text.Json.Serialization;
using Exchange.Lunar.Pigeonhole.Utilities;
namespace Exchange.Lunar.Pigeonhole.Models;
[JsonConverter(typeof(SnakeCaseConverter))]
public class GithubUser {
public int Id { get; set; }
public string NodeId { get; set; } = null!;
public string Login { get; set; } = null!;
public string AvatarUrl { get; set; } = null!;
public string GravatarUrl { get; set; } = null!;
public string Url { get; set; } = null!;
public string HtmlUrl { get; set; } = null!;
public string Type { get; set; } = null!;
public bool SiteAdmin { get; set; }
}
[JsonConverter(typeof(SnakeCaseConverter))]
public class GithubRepo {
public int Id { get; set; }
public string NodeId { get; set; } = null!;
public string Name { get; set; } = null!;
public string FullName { get; set; } = null!;
public string Url { get; set; } = null!;
public string HtmlUrl { get; set; } = null!;
public bool Fork { get; set; }
public string Visibility { get; set; } = null!;
public string DefaultBranch { get; set; } = null!;
public GithubUser Owner { get; set; } = null!;
}
[JsonConverter(typeof(SnakeCaseConverter))]
public class GithubDeployment {
public int Id { get; set; }
public string NodeId { get; set; } = null!;
public string Action { get; set; } = null!;
public string Repo { get; set; } = null!;
public string Sha { get; set; } = null!;
public string Ref { get; set; } = null!;
public string Task { get; set; } = null!;
public string OriginalEnvironment { get; set; } = null!;
public string Environment { get; set; } = null!;
public string Description { get; set; } = null!;
public GithubUser Creator { get; set; } = null!;
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
}
[JsonConverter(typeof(JsonStringEnumConverter))]
public enum GithubDeploymentStatusState
{
Pending,
Success,
Failure,
Error
}
[JsonConverter(typeof(SnakeCaseConverter))]
public class GithubDeploymentStatus {
public int Id { get; set; }
public string NodeId { get; set; } = null!;
public GithubDeploymentStatusState State { get; set; }
public string Description { get; set; } = null!;
public string Environment { get; set; } = null!;
[Obsolete("Use LogUrl instead")]
public string TargetUrl { get; set; } = null!;
public string LogUrl { get; set; } = null!;
public string EnvironmentUrl { get; set; } = null!;
public GithubUser Creator { get; set; } = null!;
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
}
[JsonConverter(typeof(SnakeCaseConverter))]
public class GithubDeploymentHookModel {
public string Action { get; set; } = null!;
public GithubDeployment Deployment { get; set; } = null!;
public GithubRepo Repository { get; set; } = null!;
public GithubUser Sender { get; set; } = null!;
}
[JsonConverter(typeof(SnakeCaseConverter))]
public class GithubDeploymentStatusHookModel {
public string Action { get; set; } = null!;
public GithubDeployment Deployment { get; set; } = null!;
public GithubRepo Repository { get; set; } = null!;
public GithubUser Sender { get; set; } = null!;
}

25
Program.cs Normal file
View File

@ -0,0 +1,25 @@
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

View File

@ -0,0 +1,31 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:6376",
"sslPort": 44310
}
},
"profiles": {
"pigeonhole": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7138;http://localhost:5072",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": false,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@ -0,0 +1,73 @@
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Exchange.Lunar.Pigeonhole.Utilities;
public class SnakeCaseConverter : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert)
{
throw new NotImplementedException();
}
public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)
{
return (JsonConverter?) Activator.CreateInstance(
typeof(SnakeCaseConverter<>).MakeGenericType(new Type[] { typeToConvert }),
BindingFlags.Instance | BindingFlags.Public,
binder: null,
args: Array.Empty<object>(),
culture: null
);
}
}
public class SnakeCaseConverter<T> : JsonConverter<T>
{
public override T Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options
)
{
return (T) JsonSerializer.Deserialize(ref reader, typeToConvert, new JsonSerializerOptions {
PropertyNamingPolicy = new SnakeCaseNamingPolicy()
})!;
}
public override void Write(
Utf8JsonWriter writer,
T value,
JsonSerializerOptions options)
{
JsonSerializer.Serialize(writer, value, new JsonSerializerOptions {
PropertyNamingPolicy = new SnakeCaseNamingPolicy()
});
}
}
public class SnakeCaseNamingPolicy : JsonNamingPolicy
{
public override string ConvertName(string name)
{
if (name == null) return null!;
var capitals = name.Count(t => char.IsUpper(t));
if (char.IsUpper(name[0])) capitals--;
Span<char> buffer = new char[name.Length + capitals];
for (int i = 0, output = 0; i < name.Length; i++)
{
var @char = name[i];
buffer[output++] = i > 0 && char.IsUpper(@char) ? '_' : @char;
buffer[output++] = @char;
}
if (buffer[^1] == '\0')
throw new Exception("Null leftover in the string buffer");
return new string(buffer).ToLower();
}
}

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

9
appsettings.json Normal file
View File

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

22
pigeonhole.csproj Normal file
View File

@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>
<Target Name="SetSourceRevisionId" BeforeTargets="InitializeSourceControlInformation">
<Exec
Command="git describe --long --always --dirty --exclude=* --abbrev=8"
ConsoleToMSBuild="True"
IgnoreExitCode="False"
>
<Output PropertyName="SourceRevisionId" TaskParameter="ConsoleOutput"/>
</Exec>
</Target>
</Project>