后端(35+ 新路由): - AES-256-GCM 字段加密(enc:v1:),密钥自动生成 data/encryption.key - 新模型:Host/K8sCluster/Credential/BuildNode/DeployTool/WebhookEndpoint/ JenkinsServer/SystemSetting;Pipeline 扩展触发器/变量/缓存列, PipelineRun 扩展 buildNumber/externalUrl - 资源 CRUD + 真实测试端点:主机/集群/构建节点 TCP 拨号、 工具真实执行版本检测(保守安装策略)、Webhook HMAC-SHA256 签名发送、 入站流水线触发 /api/hooks/pipeline/:id - Jenkins 完整集成(移植 opsx-jenkins 生产客户端):crumb/BasicAuth/ buildWithParameters/Location 队列/progressiveText 增量控制台, 服务器 CRUD+测试、Job 树拉取(folder 压平)、导入为流水线、 jenkins 类型流水线真实执行回传日志 - 流水线列表附带 lastRun、运行重试、summary 统计、系统设置单行表 - 运行结束触发出站 Webhook 通知;GitOps 同步状态真实推进 前端(14 页全部去 mock): - 流水线列表/发布记录/批量发布/环境/凭证/构建节点/工具/Webhook/ 系统设置/Jenkins 导入/GitOps(资源树+差异) 全部接入真实 API - 编辑器 4 Tab 持久化(基本信息/触发设置含真实 Webhook URL/变量缓存/发布历史) - TopBar 真实流水线切换 + 仅保存/保存并运行 - Sidebar 徽标真实统计;删除未使用组件与 mock 数据文件
248 lines
7.6 KiB
Go
248 lines
7.6 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"flowstack/internal/database"
|
|
"flowstack/internal/model"
|
|
)
|
|
|
|
// ListPipelines GET /api/pipelines —— 附带每条流水线最近一次运行信息
|
|
func ListPipelines(c *gin.Context) {
|
|
var list []model.Pipeline
|
|
database.DB.Order("updated_at DESC").Find(&list)
|
|
|
|
// 每流水线最近一次 run(一次查询取全表后内存分组,数据量小;jianmu 用反规范化表,此处无需)
|
|
var runs []model.PipelineRun
|
|
database.DB.Order("started_at DESC").Limit(500).Find(&runs)
|
|
lastByPipeline := make(map[uint]model.PipelineRun)
|
|
for _, r := range runs {
|
|
if _, ok := lastByPipeline[r.PipelineID]; !ok {
|
|
lastByPipeline[r.PipelineID] = r
|
|
}
|
|
}
|
|
|
|
type pipelineWithLastRun struct {
|
|
model.Pipeline
|
|
LastRun *model.PipelineRun `json:"lastRun"`
|
|
}
|
|
result := make([]pipelineWithLastRun, 0, len(list))
|
|
for _, p := range list {
|
|
item := pipelineWithLastRun{Pipeline: p}
|
|
if r, ok := lastByPipeline[p.ID]; ok {
|
|
item.LastRun = &r
|
|
}
|
|
result = append(result, item)
|
|
}
|
|
c.JSON(http.StatusOK, result)
|
|
}
|
|
|
|
// GetPipelineDetail GET /api/pipelines/:id —— 完整详情(编辑器各 Tab 数据源)
|
|
func GetPipelineDetail(c *gin.Context) {
|
|
var p model.Pipeline
|
|
if err := database.DB.First(&p, c.Param("id")).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "pipeline not found"})
|
|
return
|
|
}
|
|
var nodes []model.PipelineNode
|
|
database.DB.Where("pipeline_id = ?", p.ID).Order("position_x").Find(&nodes)
|
|
var edges []model.PipelineEdge
|
|
database.DB.Where("pipeline_id = ?", p.ID).Find(&edges)
|
|
var lastRun model.PipelineRun
|
|
hasRun := database.DB.Where("pipeline_id = ?", p.ID).Order("started_at DESC").First(&lastRun).Error == nil
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"pipeline": p, "nodes": nodes, "edges": edges,
|
|
"lastRun": func() *model.PipelineRun {
|
|
if hasRun {
|
|
return &lastRun
|
|
}
|
|
return nil
|
|
}(),
|
|
})
|
|
}
|
|
|
|
// GetSummary GET /api/summary —— 侧边栏徽标与首页统计
|
|
func GetSummary(c *gin.Context) {
|
|
var buildNodes, plugins, running, pipelines, hosts, clusters int64
|
|
database.DB.Model(&model.BuildNode{}).Count(&buildNodes)
|
|
database.DB.Model(&model.Plugin{}).Count(&plugins)
|
|
database.DB.Model(&model.PipelineRun{}).Where("status IN ('pending','queued','running')").Count(&running)
|
|
database.DB.Model(&model.Pipeline{}).Count(&pipelines)
|
|
database.DB.Model(&model.Host{}).Count(&hosts)
|
|
database.DB.Model(&model.K8sCluster{}).Count(&clusters)
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"buildNodes": buildNodes, "plugins": plugins, "running": running,
|
|
"pipelines": pipelines, "hosts": hosts, "clusters": clusters,
|
|
})
|
|
}
|
|
|
|
// GetSettings GET /api/settings —— 单行设置
|
|
func GetSettings(c *gin.Context) {
|
|
var s model.SystemSetting
|
|
if err := database.DB.First(&s, 1).Error; err != nil {
|
|
s = model.SystemSetting{ID: 1}
|
|
database.DB.Create(&s)
|
|
}
|
|
c.JSON(http.StatusOK, s)
|
|
}
|
|
|
|
// UpdateSettings PUT /api/settings
|
|
func UpdateSettings(c *gin.Context) {
|
|
var s model.SystemSetting
|
|
if err := database.DB.First(&s, 1).Error; err != nil {
|
|
s = model.SystemSetting{ID: 1}
|
|
database.DB.Create(&s)
|
|
}
|
|
var body map[string]any
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
updates := map[string]any{"updated_at": time.Now()}
|
|
if v, ok := body["theme"].(string); ok {
|
|
updates["theme"] = v
|
|
}
|
|
if v, ok := body["notification"].(string); ok {
|
|
updates["notification"] = v
|
|
}
|
|
if v, ok := body["toolMode"].(string); ok {
|
|
updates["tool_mode"] = v
|
|
}
|
|
if v, ok := body["extra"].(string); ok {
|
|
updates["extra"] = v
|
|
}
|
|
database.DB.Model(&s).Updates(updates)
|
|
c.JSON(http.StatusOK, s)
|
|
}
|
|
|
|
// CreatePipeline POST /api/pipelines
|
|
func CreatePipeline(c *gin.Context) {
|
|
var input struct {
|
|
Name string `json:"name" binding:"required"`
|
|
Description string `json:"description"`
|
|
Type string `json:"type"`
|
|
SourceRepo string `json:"sourceRepo"`
|
|
SourceBranch string `json:"sourceBranch"`
|
|
DeployType string `json:"deployType"`
|
|
Env string `json:"env"`
|
|
}
|
|
if err := c.ShouldBindJSON(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "name 不能为空"})
|
|
return
|
|
}
|
|
setDefault(&input.Type, "native")
|
|
setDefault(&input.SourceBranch, "main")
|
|
setDefault(&input.DeployType, "host")
|
|
setDefault(&input.Env, "staging")
|
|
|
|
p := model.Pipeline{
|
|
Name: input.Name, Description: input.Description, Type: input.Type,
|
|
SourceRepo: input.SourceRepo, SourceBranch: input.SourceBranch,
|
|
DeployType: input.DeployType, Env: input.Env,
|
|
}
|
|
if err := database.DB.Create(&p).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, p)
|
|
}
|
|
|
|
// UpdatePipeline PUT /api/pipelines/:id
|
|
func UpdatePipeline(c *gin.Context) {
|
|
var p model.Pipeline
|
|
if err := database.DB.First(&p, c.Param("id")).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "pipeline not found"})
|
|
return
|
|
}
|
|
var input map[string]any
|
|
if err := c.ShouldBindJSON(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
allowed := map[string]bool{
|
|
"name": true, "description": true, "sourceRepo": true, "sourceBranch": true,
|
|
"gitopsRepo": true, "gitopsPath": true, "deployType": true, "env": true,
|
|
"triggerMode": true, "cronExpr": true, "webhookToken": true, "watchBranches": true,
|
|
"variables": true, "cachePaths": true, "notifyWebhookURL": true, "jenkinsJobName": true,
|
|
}
|
|
updates := map[string]any{}
|
|
for k, v := range input {
|
|
if allowed[k] {
|
|
updates[k] = v
|
|
}
|
|
}
|
|
if len(updates) > 0 {
|
|
updates["updated_at"] = time.Now()
|
|
database.DB.Model(&p).Updates(updates)
|
|
}
|
|
c.JSON(http.StatusOK, p)
|
|
}
|
|
|
|
// DeletePipeline DELETE /api/pipelines/:id —— 级联删除节点与连线
|
|
func DeletePipeline(c *gin.Context) {
|
|
id := c.Param("id")
|
|
database.DB.Where("pipeline_id = ?", id).Delete(&model.PipelineEdge{})
|
|
database.DB.Where("pipeline_id = ?", id).Delete(&model.PipelineNode{})
|
|
database.DB.Delete(&model.Pipeline{}, id)
|
|
c.JSON(http.StatusOK, gin.H{"success": true})
|
|
}
|
|
|
|
// SaveNodes PUT /api/pipelines/:id/nodes —— 全量覆盖保存
|
|
func SaveNodes(c *gin.Context) {
|
|
pipelineID := c.Param("id")
|
|
var p model.Pipeline
|
|
if err := database.DB.First(&p, pipelineID).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "pipeline not found"})
|
|
return
|
|
}
|
|
var body struct {
|
|
Nodes []model.PipelineNode `json:"nodes"`
|
|
}
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
database.DB.Where("pipeline_id = ?", p.ID).Delete(&model.PipelineNode{})
|
|
for i := range body.Nodes {
|
|
body.Nodes[i].ID = 0
|
|
body.Nodes[i].PipelineID = p.ID
|
|
database.DB.Create(&body.Nodes[i])
|
|
}
|
|
database.DB.Model(&p).Update("updated_at", time.Now())
|
|
c.JSON(http.StatusOK, gin.H{"success": true})
|
|
}
|
|
|
|
// SaveEdges PUT /api/pipelines/:id/edges —— 全量覆盖保存
|
|
func SaveEdges(c *gin.Context) {
|
|
pipelineID := c.Param("id")
|
|
var p model.Pipeline
|
|
if err := database.DB.First(&p, pipelineID).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "pipeline not found"})
|
|
return
|
|
}
|
|
var body struct {
|
|
Edges []model.PipelineEdge `json:"edges"`
|
|
}
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
database.DB.Where("pipeline_id = ?", p.ID).Delete(&model.PipelineEdge{})
|
|
for i := range body.Edges {
|
|
body.Edges[i].ID = 0
|
|
body.Edges[i].PipelineID = p.ID
|
|
database.DB.Create(&body.Edges[i])
|
|
}
|
|
database.DB.Model(&p).Update("updated_at", time.Now())
|
|
c.JSON(http.StatusOK, gin.H{"success": true})
|
|
}
|
|
|
|
func setDefault(v *string, def string) {
|
|
if *v == "" {
|
|
*v = def
|
|
}
|
|
}
|