- 新增 DeployRBAC 页面: Jenkins-style Item Role 权限管理 - 正则匹配流水线 code,实时命中预览 - 内置用户分配面板,无需跳转系统管理 - 支持创建/编辑/删除/启用禁用 Item Role - 修复 PipelineEditor 成员管理 http 双解包 bug - 修复 PipelineManager 前端 code 路由迁移(delete/trigger/stop/loadRuns) - 新增批量删除流水线运行记录 API - Sidebar: 应用发布下新增「发布权限」, 仓库认证→发布配置, 移除用户组 - 恢复系统管理菜单(不受发布权限改动影响) - 后端 ItemRole CRUD handler + 正则匹配中间件已就绪
165 lines
6.1 KiB
Go
165 lines
6.1 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"ezops/internal/models"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// DeployHandler 发布系统处理器
|
|
type DeployHandler struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewDeployHandler(db *gorm.DB) *DeployHandler {
|
|
return &DeployHandler{db: db}
|
|
}
|
|
|
|
// ==================== Git 仓库认证 ====================
|
|
|
|
func (h *DeployHandler) ListRepos(c *gin.Context) {
|
|
var items []models.PipelineRepo
|
|
h.db.Order("updated_at DESC").Find(&items)
|
|
c.JSON(http.StatusOK, models.Response{Code: 200, Data: models.ListResponse{List: items, Total: int64(len(items))}})
|
|
}
|
|
|
|
func (h *DeployHandler) GetRepo(c *gin.Context) {
|
|
var item models.PipelineRepo
|
|
if err := h.db.First(&item, c.Param("id")); err.Error != nil {
|
|
c.JSON(http.StatusNotFound, models.Response{Code: 404, Message: "不存在"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, models.Response{Code: 200, Data: item})
|
|
}
|
|
|
|
func (h *DeployHandler) CreateRepo(c *gin.Context) {
|
|
var req struct {
|
|
Name string `json:"name" binding:"required"`
|
|
Description string `json:"description"`
|
|
URL string `json:"url" binding:"required"`
|
|
AuthType models.RepoAuthType `json:"auth_type"`
|
|
CredentialID *uint `json:"credential_id"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, models.Response{Code: 400, Message: err.Error()})
|
|
return
|
|
}
|
|
if req.AuthType == "" { req.AuthType = models.RepoAuthToken }
|
|
item := models.PipelineRepo{
|
|
Name: req.Name, Description: req.Description, URL: req.URL,
|
|
AuthType: req.AuthType, CredentialID: req.CredentialID,
|
|
CreatedBy: c.GetString("username"),
|
|
}
|
|
h.db.Create(&item)
|
|
c.JSON(http.StatusOK, models.Response{Code: 200, Data: item})
|
|
}
|
|
|
|
func (h *DeployHandler) UpdateRepo(c *gin.Context) {
|
|
var item models.PipelineRepo
|
|
if err := h.db.First(&item, c.Param("id")); err.Error != nil {
|
|
c.JSON(http.StatusNotFound, models.Response{Code: 404, Message: "不存在"})
|
|
return
|
|
}
|
|
var req struct {
|
|
Name *string `json:"name"`
|
|
Description *string `json:"description"`
|
|
URL *string `json:"url"`
|
|
AuthType *models.RepoAuthType `json:"auth_type"`
|
|
CredentialID *uint `json:"credential_id"`
|
|
Enabled *bool `json:"enabled"`
|
|
}
|
|
c.ShouldBindJSON(&req)
|
|
updates := map[string]interface{}{}
|
|
if req.Name != nil { updates["name"] = *req.Name }
|
|
if req.Description != nil { updates["description"] = *req.Description }
|
|
if req.URL != nil { updates["url"] = *req.URL }
|
|
if req.AuthType != nil { updates["auth_type"] = *req.AuthType }
|
|
if req.Enabled != nil { updates["enabled"] = *req.Enabled }
|
|
if req.CredentialID != nil { updates["credential_id"] = req.CredentialID }
|
|
h.db.Model(&item).Updates(updates)
|
|
h.db.First(&item, item.ID)
|
|
c.JSON(http.StatusOK, models.Response{Code: 200, Data: item})
|
|
}
|
|
|
|
func (h *DeployHandler) DeleteRepo(c *gin.Context) {
|
|
h.db.Delete(&models.PipelineRepo{}, c.Param("id"))
|
|
c.JSON(http.StatusOK, models.Response{Code: 200, Message: "已删除"})
|
|
}
|
|
|
|
// ==================== 镜像仓库认证 ====================
|
|
|
|
func (h *DeployHandler) ListRegistries(c *gin.Context) {
|
|
var items []models.PipelineRegistry
|
|
h.db.Order("updated_at DESC").Find(&items)
|
|
c.JSON(http.StatusOK, models.Response{Code: 200, Data: models.ListResponse{List: items, Total: int64(len(items))}})
|
|
}
|
|
|
|
func (h *DeployHandler) GetRegistry(c *gin.Context) {
|
|
var item models.PipelineRegistry
|
|
if err := h.db.First(&item, c.Param("id")); err.Error != nil {
|
|
c.JSON(http.StatusNotFound, models.Response{Code: 404, Message: "不存在"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, models.Response{Code: 200, Data: item})
|
|
}
|
|
|
|
func (h *DeployHandler) CreateRegistry(c *gin.Context) {
|
|
var req struct {
|
|
Name string `json:"name" binding:"required"`
|
|
Description string `json:"description"`
|
|
RegistryURL string `json:"registry_url" binding:"required"`
|
|
Namespace string `json:"namespace"`
|
|
AuthType models.RegistryAuthType `json:"auth_type"`
|
|
CredentialID *uint `json:"credential_id"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, models.Response{Code: 400, Message: err.Error()})
|
|
return
|
|
}
|
|
if req.AuthType == "" { req.AuthType = models.RegistryAuthPassword }
|
|
item := models.PipelineRegistry{
|
|
Name: req.Name, Description: req.Description, RegistryURL: req.RegistryURL,
|
|
Namespace: req.Namespace, AuthType: req.AuthType, CredentialID: req.CredentialID,
|
|
CreatedBy: c.GetString("username"),
|
|
}
|
|
h.db.Create(&item)
|
|
c.JSON(http.StatusOK, models.Response{Code: 200, Data: item})
|
|
}
|
|
|
|
func (h *DeployHandler) UpdateRegistry(c *gin.Context) {
|
|
var item models.PipelineRegistry
|
|
if err := h.db.First(&item, c.Param("id")); err.Error != nil {
|
|
c.JSON(http.StatusNotFound, models.Response{Code: 404, Message: "不存在"})
|
|
return
|
|
}
|
|
var req struct {
|
|
Name *string `json:"name"`
|
|
Description *string `json:"description"`
|
|
RegistryURL *string `json:"registry_url"`
|
|
Namespace *string `json:"namespace"`
|
|
AuthType *models.RegistryAuthType `json:"auth_type"`
|
|
CredentialID *uint `json:"credential_id"`
|
|
Enabled *bool `json:"enabled"`
|
|
}
|
|
c.ShouldBindJSON(&req)
|
|
updates := map[string]interface{}{}
|
|
if req.Name != nil { updates["name"] = *req.Name }
|
|
if req.Description != nil { updates["description"] = *req.Description }
|
|
if req.RegistryURL != nil { updates["registry_url"] = *req.RegistryURL }
|
|
if req.Namespace != nil { updates["namespace"] = *req.Namespace }
|
|
if req.AuthType != nil { updates["auth_type"] = *req.AuthType }
|
|
if req.Enabled != nil { updates["enabled"] = *req.Enabled }
|
|
if req.CredentialID != nil { updates["credential_id"] = req.CredentialID }
|
|
h.db.Model(&item).Updates(updates)
|
|
h.db.First(&item, item.ID)
|
|
c.JSON(http.StatusOK, models.Response{Code: 200, Data: item})
|
|
}
|
|
|
|
func (h *DeployHandler) DeleteRegistry(c *gin.Context) {
|
|
h.db.Delete(&models.PipelineRegistry{}, c.Param("id"))
|
|
c.JSON(http.StatusOK, models.Response{Code: 200, Message: "已删除"})
|
|
}
|