65a6b4afaf
Co-authored-by: Copilot <copilot@github.com>
88 lines
3.7 KiB
C#
88 lines
3.7 KiB
C#
using ControlPlane.Api.Services;
|
|
using ControlPlane.Core.Models;
|
|
|
|
namespace ControlPlane.Api.Endpoints;
|
|
|
|
public static class GiteaEndpoints
|
|
{
|
|
public static IEndpointRouteBuilder MapGiteaEndpoints(this IEndpointRouteBuilder app)
|
|
{
|
|
var g = app.MapGroup("/api/gitea").WithTags("Gitea");
|
|
|
|
g.MapGet ("/repo", GetRepo);
|
|
g.MapGet ("/branches", ListBranches);
|
|
g.MapPost("/branches", CreateBranch);
|
|
g.MapGet ("/pulls", ListPulls);
|
|
g.MapGet ("/pulls/{number:long}", GetPull);
|
|
g.MapPost("/pulls", CreatePull);
|
|
g.MapGet ("/tags", ListTags);
|
|
g.MapPost("/tags", CreateTag);
|
|
g.MapGet ("/webhooks", ListWebhooks);
|
|
g.MapPost("/webhooks", RegisterWebhook);
|
|
|
|
return app;
|
|
}
|
|
|
|
private static async Task<IResult> GetRepo(GiteaService svc, string? repo, CancellationToken ct)
|
|
{
|
|
var result = await svc.GetRepoAsync(repo, ct);
|
|
return result is null ? Results.StatusCode(503) : Results.Ok(result);
|
|
}
|
|
|
|
private static async Task<IResult> ListBranches(GiteaService svc, string? repo, CancellationToken ct)
|
|
{
|
|
// repo=all returns branches from all registered repos, each tagged with repoKey
|
|
if (repo == "all")
|
|
{
|
|
var all = await svc.ListAllBranchesAsync(ct);
|
|
return Results.Ok(all.Select(x => new { repoKey = x.RepoKey, x.Branch.Name, x.Branch.CommitSha, x.Branch.Protected }));
|
|
}
|
|
return Results.Ok(await svc.ListBranchesAsync(repo, ct));
|
|
}
|
|
|
|
private static async Task<IResult> CreateBranch(
|
|
CreateBranchRequest req, GiteaService svc, CancellationToken ct)
|
|
{
|
|
var branch = await svc.CreateBranchAsync(req, ct);
|
|
return branch is null ? Results.BadRequest("Failed to create branch in Gitea.") : Results.Ok(branch);
|
|
}
|
|
|
|
private static async Task<IResult> ListPulls(
|
|
GiteaService svc, string state = "open", string? repo = null, CancellationToken ct = default) =>
|
|
Results.Ok(await svc.ListPullRequestsAsync(state, repo, ct));
|
|
|
|
private static async Task<IResult> GetPull(
|
|
long number, GiteaService svc, string? repo = null, CancellationToken ct = default)
|
|
{
|
|
var pr = await svc.GetPullRequestAsync(number, repo, ct);
|
|
return pr is null ? Results.NotFound() : Results.Ok(pr);
|
|
}
|
|
|
|
private static async Task<IResult> CreatePull(
|
|
CreatePullRequestRequest req, GiteaService svc, string? repo = null, CancellationToken ct = default)
|
|
{
|
|
var pr = await svc.CreatePullRequestAsync(req, repo, ct);
|
|
return pr is null ? Results.BadRequest("Failed to create PR in Gitea.") : Results.Ok(pr);
|
|
}
|
|
|
|
private static async Task<IResult> ListTags(GiteaService svc, string? repo = null, CancellationToken ct = default) =>
|
|
Results.Ok(await svc.ListTagsAsync(repo, ct));
|
|
|
|
private static async Task<IResult> CreateTag(
|
|
CreateTagRequest req, GiteaService svc, string? repo = null, CancellationToken ct = default)
|
|
{
|
|
var tag = await svc.CreateTagAsync(req, repo, ct);
|
|
return tag is null ? Results.BadRequest("Failed to create tag in Gitea.") : Results.Ok(tag);
|
|
}
|
|
|
|
private static async Task<IResult> ListWebhooks(GiteaService svc, string? repo = null, CancellationToken ct = default) =>
|
|
Results.Ok(await svc.ListWebhooksAsync(repo, ct));
|
|
|
|
private static async Task<IResult> RegisterWebhook(
|
|
CreateWebhookRequest req, GiteaService svc, string? repo = null, CancellationToken ct = default)
|
|
{
|
|
var hook = await svc.RegisterWebhookAsync(req, repo, ct);
|
|
return hook is null ? Results.BadRequest("Failed to register webhook in Gitea.") : Results.Ok(hook);
|
|
}
|
|
}
|