Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: deprecate project webhook legacy API (#6262)
Danny Xud5fd877ee78e
diff --git a/backend/server/project_webhook.go b/backend/server/project_webhook.godeleted file mode 100644index a282c82e3f..0000000000--- a/backend/server/project_webhook.go+++ /dev/null@@ -1,280 +0,0 @@-package server--import (- "fmt"- "net/http"- "strconv"- "strings"- "time"-- "github.com/google/jsonapi"- "github.com/gosimple/slug"- "github.com/labstack/echo/v4"-- "github.com/bytebase/bytebase/backend/common"- api "github.com/bytebase/bytebase/backend/legacyapi"- webhookPlugin "github.com/bytebase/bytebase/backend/plugin/webhook"- "github.com/bytebase/bytebase/backend/store"-)--func (s *Server) registerProjectWebhookRoutes(g *echo.Group) {- g.GET("/project/:projectID/webhook", func(c echo.Context) error {- ctx := c.Request().Context()- projectID, err := strconv.Atoi(c.Param("projectID"))- if err != nil {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Project ID is not a number: %s", c.Param("projectID"))).SetInternal(err)- }- webhookList, err := s.store.FindProjectWebhookV2(ctx, &store.FindProjectWebhookMessage{- ProjectID: &projectID,- })- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch webhook list for project ID: %d", projectID)).SetInternal(err)- }-- var apiWebhookList []*api.ProjectWebhook- for _, webhook := range webhookList {- apiWebhookList = append(apiWebhookList, webhook.ToAPIProjectWebhook())- }-- c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)- if err := jsonapi.MarshalPayload(c.Response().Writer, apiWebhookList); err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal project webhook response: %v", projectID)).SetInternal(err)- }- return nil- })-- g.POST("/project/:projectID/webhook", func(c echo.Context) error {- ctx := c.Request().Context()-- setting, err := s.store.GetWorkspaceGeneralSetting(ctx)- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, "failed to get workspace setting").SetInternal(err)- }- if setting.ExternalUrl == "" {- return echo.NewHTTPError(http.StatusBadRequest, "external URL isn't setup yet, see https://www.bytebase.com/docs/get-started/install/external-url")- }-- projectID, err := strconv.Atoi(c.Param("projectID"))- if err != nil {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Project ID is not a number: %s", c.Param("projectID"))).SetInternal(err)- }-- hookCreate := &api.ProjectWebhookCreate{- CreatorID: c.Get(getPrincipalIDContextKey()).(int),- ProjectID: projectID,- }- if err := jsonapi.UnmarshalPayload(c.Request().Body, hookCreate); err != nil {- return echo.NewHTTPError(http.StatusBadRequest, "Malformed create project webhook request").SetInternal(err)- }- project, err := s.store.GetProjectByID(ctx, projectID)- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch project ID: %v", projectID)).SetInternal(err)- }- if project == nil {- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Project ID not found: %v", projectID))- }-- webhook, err := s.store.CreateProjectWebhookV2(ctx, c.Get(getPrincipalIDContextKey()).(int), projectID, project.ResourceID, &store.ProjectWebhookMessage{- Type: hookCreate.Type,- Title: hookCreate.Name,- URL: hookCreate.URL,- ActivityList: hookCreate.ActivityList,- })- if err != nil {- if common.ErrorCode(err) == common.Conflict {- return echo.NewHTTPError(http.StatusConflict, fmt.Sprintf("Webhook url already exists in the project: %s", hookCreate.URL))- }- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create project webhook").SetInternal(err)- }-- c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)- if err := jsonapi.MarshalPayload(c.Response().Writer, webhook.ToAPIProjectWebhook()); err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal create project webhook response").SetInternal(err)- }- return nil- })-- g.GET("/project/:projectID/webhook/:webhookID", func(c echo.Context) error {- ctx := c.Request().Context()- _, err := strconv.Atoi(c.Param("projectID"))- if err != nil {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Project ID is not a number: %s", c.Param("projectID"))).SetInternal(err)- }-- id, err := strconv.Atoi(c.Param("webhookID"))- if err != nil {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Project webhook ID is not a number: %s", c.Param("webhookID"))).SetInternal(err)- }-- webhook, err := s.store.GetProjectWebhookV2(ctx, &store.FindProjectWebhookMessage{- ID: &id,- })- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch project webhook ID: %v", id)).SetInternal(err)- }- if webhook == nil {- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Project webhook ID not found: %d", id))- }-- c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)- if err := jsonapi.MarshalPayload(c.Response().Writer, webhook.ToAPIProjectWebhook()); err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal project webhook ID response: %v", id)).SetInternal(err)- }- return nil- })-- g.PATCH("/project/:projectID/webhook/:webhookID", func(c echo.Context) error {- ctx := c.Request().Context()- projectID, err := strconv.Atoi(c.Param("projectID"))- if err != nil {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Project ID is not a number: %s", c.Param("projectID"))).SetInternal(err)- }-- id, err := strconv.Atoi(c.Param("webhookID"))- if err != nil {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Project webhook ID is not a number: %s", c.Param("webhookID"))).SetInternal(err)- }-- project, err := s.store.GetProjectByID(ctx, projectID)- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch project ID: %v", projectID)).SetInternal(err)- }- if project == nil {- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Project ID not found: %v", projectID))- }-- hookPatch := &api.ProjectWebhookPatch{- ID: id,- UpdaterID: c.Get(getPrincipalIDContextKey()).(int),- }- if err := jsonapi.UnmarshalPayload(c.Request().Body, hookPatch); err != nil {- return echo.NewHTTPError(http.StatusBadRequest, "Malformed change project webhook").SetInternal(err)- }- var activityList []string- if hookPatch.ActivityList != nil {- activityList = strings.Split(*hookPatch.ActivityList, ",")- }-- webhook, err := s.store.UpdateProjectWebhookV2(ctx, c.Get(getPrincipalIDContextKey()).(int), project.ID, project.ResourceID, id, &store.UpdateProjectWebhookMessage{- Title: hookPatch.Name,- URL: hookPatch.URL,- ActivityList: activityList,- })- if err != nil {- if common.ErrorCode(err) == common.NotFound {- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Project webhook ID not found: %d", id))- }- if common.ErrorCode(err) == common.Conflict {- return echo.NewHTTPError(http.StatusConflict, fmt.Sprintf("Hook url already exists in the project: %s", *hookPatch.URL))- }- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to change project webhook ID: %v", id)).SetInternal(err)- }-- c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)- if err := jsonapi.MarshalPayload(c.Response().Writer, webhook.ToAPIProjectWebhook()); err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal project webhook change response: %v", id)).SetInternal(err)- }- return nil- })-- g.DELETE("/project/:projectID/webhook/:webhookID", func(c echo.Context) error {- ctx := c.Request().Context()- projectID, err := strconv.Atoi(c.Param("projectID"))- if err != nil {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Project ID is not a number: %s", c.Param("projectID"))).SetInternal(err)- }-- id, err := strconv.Atoi(c.Param("webhookID"))- if err != nil {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Webhook ID is not a number: %s", c.Param("webhookID"))).SetInternal(err)- }-- project, err := s.store.GetProjectByID(ctx, projectID)- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch project ID: %v", projectID)).SetInternal(err)- }- if project == nil {- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Project ID not found: %v", projectID))- }- if err := s.store.DeleteProjectWebhookV2(ctx, project.ID, project.ResourceID, id); err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete project webhook ID: %v", id)).SetInternal(err)- }-- c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)- c.Response().WriteHeader(http.StatusOK)- return nil- })-- g.GET("/project/:projectID/webhook/:webhookID/test", func(c echo.Context) error {- ctx := c.Request().Context()-- setting, err := s.store.GetWorkspaceGeneralSetting(ctx)- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, "failed to get workspace setting").SetInternal(err)- }- if setting.ExternalUrl == "" {- return echo.NewHTTPError(http.StatusBadRequest, "external URL isn't setup yet, see https://www.bytebase.com/docs/get-started/install/external-url")- }-- projectID, err := strconv.Atoi(c.Param("projectID"))- if err != nil {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Project ID is not a number: %s", c.Param("projectID"))).SetInternal(err)- }-- project, err := s.store.GetProjectV2(ctx, &store.FindProjectMessage{UID: &projectID})- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch project ID: %v", projectID)).SetInternal(err)- }- if project == nil {- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Project ID not found: %d", projectID))- }-- id, err := strconv.Atoi(c.Param("webhookID"))- if err != nil {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Project webhook ID is not a number: %s", c.Param("webhookID"))).SetInternal(err)- }-- webhook, err := s.store.GetProjectWebhookV2(ctx, &store.FindProjectWebhookMessage{- ID: &id,- })- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch project webhook ID: %v", id)).SetInternal(err)- }- if webhook == nil {- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Project webhook ID not found: %d", id))- }-- result := &api.ProjectWebhookTestResult{}- err = webhookPlugin.Post(- webhook.Type,- webhookPlugin.Context{- URL: webhook.URL,- Level: webhookPlugin.WebhookInfo,- ActivityType: string(api.ActivityIssueCreate),- Title: fmt.Sprintf("Test webhook %q", webhook.Title),- Description: "This is a test",- Link: fmt.Sprintf("%s/project/%s/webhook/%s", setting.ExternalUrl, getProjectSlug(project), api.ProjectWebhookSlug(webhook.Title, webhook.ID)),- CreatorID: api.SystemBotID,- CreatorName: "Bytebase",- CreatorEmail: api.SystemBotEmail,- CreatedTs: time.Now().Unix(),- Project: &webhookPlugin.Project{Name: project.Title},- },- )-- if err != nil {- result.Error = err.Error()- }-- c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)- if err := jsonapi.MarshalPayload(c.Response().Writer, result); err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal project webhook response: %v", projectID)).SetInternal(err)- }- return nil- })-}--// getProjectSlug is the slug formatter for Project.-func getProjectSlug(project *store.ProjectMessage) string {- return fmt.Sprintf("%s-%d", slug.Make(project.Title), project.UID)-}diff --git a/backend/server/server.go b/backend/server/server.goindex dc8ccb25e2..578f9fe10c 100644--- a/backend/server/server.go+++ b/backend/server/server.go@@ -510,7 +510,6 @@ func NewServer(ctx context.Context, profile config.Profile) (*Server, error) {s.registerDebugRoutes(apiGroup)s.registerOAuthRoutes(apiGroup)s.registerProjectRoutes(apiGroup)- s.registerProjectWebhookRoutes(apiGroup)s.registerEnvironmentRoutes(apiGroup)s.registerInstanceRoutes(apiGroup)s.registerDatabaseRoutes(apiGroup)