using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NewLife;
using NewLife.Cube;
using NewLife.Cube.ViewModels;
using NewLife.Log;
using Stardust.Data.Deployment;
using Stardust.Web.Services;
namespace Stardust.Web.Areas.Deployment.Controllers;
/// <summary>流水线 webhook 接入。无鉴权,仅靠 token + 可选 secret。
/// 接收 git push JSON 负载,委托 PipelineService.HandleWebhookAsync 处理</summary>
/// <remarks>
/// 这里只标 <see cref="AreaAttribute"/>,不用区域特有的 <see cref="DeploymentArea"/>:
/// 魔方按"是否带区域特性"扫描区域控制器的菜单,本控制器只有 Webhook 动作、没有管理页面,
/// 被扫描到就会生成一条点开即 404 的死菜单。路由模板里的 [area] 由 <see cref="AreaAttribute"/> 提供,功能不受影响。
/// </remarks>
[Area("Deployment")]
[Route("[area]/[controller]/[action]")]
public class PipelineWebhookController(PipelineService pipelineService, ITracer tracer) : ControllerBaseX
{
/// <summary>接收 git push webhook。对外地址固定为 /Deployment/Pipeline/Webhook,与 MVC 版一致</summary>
/// <param name="token">流水线鉴权 token</param>
[HttpPost]
[Route("/Deployment/Pipeline/Webhook")]
[AllowAnonymous]
public async Task<Object> Webhook(String token)
{
if (token.IsNullOrEmpty()) return new { result = "error", reason = "missing token" };
// 读取原始 body 用于签名校验
Request.EnableBuffering();
Request.Body.Position = 0;
using var reader = new StreamReader(Request.Body, leaveOpen: true);
var body = await reader.ReadToEndAsync();
Request.Body.Position = 0;
// 平台识别与签名头/令牌头提取统一在 WebhookRequest.Create 内完成
var request = WebhookRequest.Create(body, name => Request.Headers[name].ToString());
using var span = tracer?.NewSpan("Pipeline-Webhook", new { token, platform = request.Platform, len = body?.Length ?? 0 });
var rs = await pipelineService.HandleWebhookAsync(token, request);
span?.AppendTag($"result={rs}");
return rs;
}
/// <summary>获取字段信息。webhook 接收器无关联实体,仅返回 token/secret 鉴权参数说明,供前端构造 webhook URL 与签名配置参考</summary>
/// <param name="kind">字段类型:1-列表List、2-详情Detail、3-添加AddForm、4-编辑EditForm、5-搜索Search</param>
/// <returns></returns>
[AllowAnonymous]
[HttpGet]
public virtual List<DataField> GetFields(ViewKinds kind)
{
// webhook 接收器无关联实体,仅返回 token/secret 鉴权参数说明,
// 供前端构造 webhook URL 与签名配置参考。返回统一 ApiResponse 契约的字段列表。
return new List<DataField>
{
new() { Name = "token", DisplayName = "Token", Description = "流水线鉴权 token,拼接到 webhook URL 的 ?token= 参数" },
new() { Name = "secret", DisplayName = "Secret", Description = "可选,签名密钥。支持 HMAC-SHA256 十六进制/Base64 摘要,以及 GitLab/Gitee 明文令牌头" },
};
}
}
|