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: "已删除"}) }