package controller import ( "net/http" "ezops/model" "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 []model.PipelineRepo h.db.Order("updated_at DESC").Find(&items) c.JSON(http.StatusOK, model.Response{Code: 200, Data: model.ListResponse{List: items, Total: int64(len(items))}}) } func (h *DeployHandler) GetRepo(c *gin.Context) { var item model.PipelineRepo if err := h.db.First(&item, c.Param("id")); err.Error != nil { c.JSON(http.StatusNotFound, model.Response{Code: 404, Message: "不存在"}) return } c.JSON(http.StatusOK, model.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 model.RepoAuthType `json:"auth_type"` CredentialID *uint `json:"credential_id"` } if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, model.Response{Code: 400, Message: err.Error()}) return } if req.AuthType == "" { req.AuthType = model.RepoAuthToken } item := model.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, model.Response{Code: 200, Data: item}) } func (h *DeployHandler) UpdateRepo(c *gin.Context) { var item model.PipelineRepo if err := h.db.First(&item, c.Param("id")); err.Error != nil { c.JSON(http.StatusNotFound, model.Response{Code: 404, Message: "不存在"}) return } var req struct { Name *string `json:"name"` Description *string `json:"description"` URL *string `json:"url"` AuthType *model.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, model.Response{Code: 200, Data: item}) } func (h *DeployHandler) DeleteRepo(c *gin.Context) { h.db.Delete(&model.PipelineRepo{}, c.Param("id")) c.JSON(http.StatusOK, model.Response{Code: 200, Message: "已删除"}) } // ==================== 镜像仓库认证 ==================== func (h *DeployHandler) ListRegistries(c *gin.Context) { var items []model.PipelineRegistry h.db.Order("updated_at DESC").Find(&items) c.JSON(http.StatusOK, model.Response{Code: 200, Data: model.ListResponse{List: items, Total: int64(len(items))}}) } func (h *DeployHandler) GetRegistry(c *gin.Context) { var item model.PipelineRegistry if err := h.db.First(&item, c.Param("id")); err.Error != nil { c.JSON(http.StatusNotFound, model.Response{Code: 404, Message: "不存在"}) return } c.JSON(http.StatusOK, model.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 model.RegistryAuthType `json:"auth_type"` CredentialID *uint `json:"credential_id"` } if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, model.Response{Code: 400, Message: err.Error()}) return } if req.AuthType == "" { req.AuthType = model.RegistryAuthPassword } item := model.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, model.Response{Code: 200, Data: item}) } func (h *DeployHandler) UpdateRegistry(c *gin.Context) { var item model.PipelineRegistry if err := h.db.First(&item, c.Param("id")); err.Error != nil { c.JSON(http.StatusNotFound, model.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 *model.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, model.Response{Code: 200, Data: item}) } func (h *DeployHandler) DeleteRegistry(c *gin.Context) { h.db.Delete(&model.PipelineRegistry{}, c.Param("id")) c.JSON(http.StatusOK, model.Response{Code: 200, Message: "已删除"}) }