using System.ComponentModel;
using Microsoft.AspNetCore.Mvc;
using Stardust.Data.Deployment;
using Stardust.Web.Services;
using NewLife;
using NewLife.Cube;
using NewLife.Cube.Extensions;
using NewLife.Cube.ViewModels;
using NewLife.Web;
using Stardust.Data.Platform;
using XCode;
using XCode.Membership;
namespace Stardust.Web.Areas.Deployment.Controllers;
/// <summary>流水线运行。每次 webhook 触发或手动触发,都会产生一条运行记录,记录状态、关联步骤、TraceId 等信息</summary>
[Menu(40, false)]
[DeploymentArea]
public class AppPipelineRunController : DeploymentEntityController<AppPipelineRun>
{
private readonly PipelineService _pipelineService;
static AppPipelineRunController()
{
// 列表字段:去掉敏感字段
ListFields.RemoveField("Id", "TraceId", "TriggerSource");
ListFields.RemoveCreateField().RemoveRemarkField();
ListFields.AddListField("Reprocess", null, "Status");
ListFields.TraceUrl("TraceId");
// 步骤列:跳转到该运行的步骤记录列表
{
var df = ListFields.AddListField("Steps", "UpdateUserId");
df.DisplayName = "步骤";
df.Url = "/Deployment/AppPipelineStep?runId={Id}";
}
// 设置操作按钮
{
var df = ListFields.GetField("Reprocess") as ListField;
df.DisplayName = "重跑";
df.Description = "基于本次运行重新触发流水线";
df.Url = "/Deployment/AppPipelineRun/Reprocess?id={Id}";
df.DataAction = "action";
}
// 取消按钮:仅未到终态的运行会真正取消
{
var df = ListFields.AddListField("Cancel", "UpdateUserId");
df.DisplayName = "取消";
df.Description = "取消未完成的运行";
df.Url = "/Deployment/AppPipelineRun/Cancel?id={Id}";
df.DataAction = "action";
}
}
public AppPipelineRunController(PipelineService pipelineService)
{
_pipelineService = pipelineService;
}
/// <summary>高级搜索</summary>
/// <param name="p">分页器。包含分页排序参数,以及Http请求参数</param>
/// <returns></returns>
/// <summary>高级搜索。按条件分页查询</summary>
/// <param name="p">分页参数</param>
/// <returns>实体列表</returns>
protected override IEnumerable<AppPipelineRun> Search(Pager p)
{
var pipelineId = p["pipelineId"].ToInt(-1);
var status = p["status"].ToInt(-1);
return AppPipelineRun.Search(pipelineId, status, p["dtStart"].ToDateTime(), p["dtEnd"].ToDateTime(), p);
}
/// <summary>重新处理(重试失败或被中断的运行)。委托 PipelineService.Reprocess</summary>
/// <param name="id">运行编号</param>
/// <returns></returns>
[EntityAuthorize(PermissionFlags.Update)]
public async Task<ActionResult> Reprocess(Int32 id)
{
try
{
var run = await _pipelineService.Reprocess(id, UserHost);
return JsonRefresh($"流水线已重新排队,运行编号[{run.Id}]", 1);
}
catch (Exception ex)
{
return Json(500, ex.Message);
}
}
/// <summary>取消运行。仅对未到终态的流水线运行有效,取消后不再续发部署</summary>
/// <param name="id">运行编号</param>
/// <returns></returns>
[EntityAuthorize(PermissionFlags.Update)]
public ActionResult Cancel(Int32 id)
{
try
{
var rs = _pipelineService.Cancel(id, UserHost);
if (!rs) return Json(500, L.T("Run is already in a terminal state."));
return JsonRefresh($"流水线运行[{id}]已取消", 1);
}
catch (Exception ex)
{
return Json(500, ex.Message);
}
}
/// <summary>运行详情。返回运行记录及其步骤列表</summary>
/// <param name="id">运行编号</param>
/// <returns>运行字段 + steps 数组;不存在时业务错误「Run record does not exist.」</returns>
[DisplayName("运行详情")]
[EntityAuthorize(PermissionFlags.Detail)]
public ActionResult Detail(Int32 id)
{
var run = ProjectScopeRules.FilterVisible(AppPipelineRun.FindById(id));
if (run == null)
{
// 契约固定文案,便于前端识别空运行
return Json(500, "Run record does not exist.");
}
var steps = ProjectScopeRules.FilterVisible(AppPipelineStep.FindAllByRunId(id)).OrderBy(e => e.StepIndex).ToList();
return Json(0, null, new
{
run.Id,
run.PipelineId,
Status = run.Status + "",
run.TriggerSource,
run.CommitId,
run.CommitMessage,
run.CommitAuthor,
run.CommitTime,
run.Branch,
run.BuildNodeId,
run.AppVersionId,
run.BuildStartedTime,
run.BuildFinishedTime,
run.DeployStartedTime,
run.DeployFinishedTime,
run.TraceId,
run.CreateUserId,
run.CreateTime,
run.CreateIP,
run.UpdateUserId,
run.UpdateTime,
run.UpdateIP,
run.Remark,
steps = steps.Select(s => new
{
s.Id,
s.RunId,
s.StepType,
s.StepIndex,
s.NodeId,
s.CommandId,
s.Status,
s.Message,
s.StartedTime,
s.FinishedTime,
s.CreateTime,
}).ToArray(),
});
}
}
|