Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

Implement /project/:projectId/hook endpoint

天舟a592a1cac244
diff --git a/api/project_hook.go b/api/project_hook.go
index 2ab9a7a856..446bbe6602 100644
--- a/api/project_hook.go
+++ b/api/project_hook.go
@@ -6,7 +6,7 @@ import (
)
type ProjectHook struct {
- ID int `jsonapi:"primary,projectWebhookMember"`
+ ID int `jsonapi:"primary,projectHookMember"`
// Standard fields
CreatorId int
diff --git a/server/acl_casbin_policy_dba.csv b/server/acl_casbin_policy_dba.csv
index ba0f582713..f078500ac0 100644
--- a/server/acl_casbin_policy_dba.csv
+++ b/server/acl_casbin_policy_dba.csv
@@ -13,6 +13,10 @@ p, DBA, /project/{id}/repository, DELETE
p, DBA, /project/{projectId}/member, POST
p, DBA, /project/{projectId}/member/{memberId}, PATCH
p, DBA, /project/{projectId}/member/{memberId}, DELETE
+p, DBA, /project/{projectId}/hook, GET
+p, DBA, /project/{projectId}/hook, POST
+p, DBA, /project/{projectId}/hook/{hookId}, PATCH
+p, DBA, /project/{projectId}/hook/{hookId}, DELETE
p, DBA, /environment, POST
p, DBA, /environment, GET
p, DBA, /environment/{id}, PATCH
diff --git a/server/acl_casbin_policy_developer.csv b/server/acl_casbin_policy_developer.csv
index 3e4bc5194f..8c3feafd27 100644
--- a/server/acl_casbin_policy_developer.csv
+++ b/server/acl_casbin_policy_developer.csv
@@ -13,6 +13,10 @@ p, DEVELOPER, /project/{id}/repository, DELETE
p, DEVELOPER, /project/{projectId}/member, POST
p, DEVELOPER, /project/{projectId}/member/{memberId}, PATCH
p, DEVELOPER, /project/{projectId}/member/{memberId}, DELETE
+p, DEVELOPER, /project/{projectId}/hook, GET
+p, DEVELOPER, /project/{projectId}/hook, POST
+p, DEVELOPER, /project/{projectId}/hook/{hookId}, PATCH
+p, DEVELOPER, /project/{projectId}/hook/{hookId}, DELETE
p, DEVELOPER, /environment, GET
p, DEVELOPER, /instance, GET
p, DEVELOPER, /instance/{id}, GET
diff --git a/server/acl_casbin_policy_owner.csv b/server/acl_casbin_policy_owner.csv
index a7c0524a97..464dfd533d 100644
--- a/server/acl_casbin_policy_owner.csv
+++ b/server/acl_casbin_policy_owner.csv
@@ -17,6 +17,10 @@ p, OWNER, /project/{id}/repository, DELETE
p, OWNER, /project/{projectId}/member, POST
p, OWNER, /project/{projectId}/member/{memberId}, PATCH
p, OWNER, /project/{projectId}/member/{memberId}, DELETE
+p, OWNER, /project/{projectId}/hook, GET
+p, OWNER, /project/{projectId}/hook, POST
+p, OWNER, /project/{projectId}/hook/{hookId}, PATCH
+p, OWNER, /project/{projectId}/hook/{hookId}, DELETE
p, OWNER, /environment, POST
p, OWNER, /environment, GET
p, OWNER, /environment/{id}, PATCH
diff --git a/server/project_hook.go b/server/project_hook.go
new file mode 100644
index 0000000000..c291e85e64
--- /dev/null
+++ b/server/project_hook.go
@@ -0,0 +1,160 @@
+package server
+
+import (
+ "context"
+ "fmt"
+ "net/http"
+ "strconv"
+
+ "github.com/bytebase/bytebase"
+ "github.com/bytebase/bytebase/api"
+ "github.com/google/jsonapi"
+ "github.com/labstack/echo/v4"
+)
+
+func (s *Server) registerProjectHookRoutes(g *echo.Group) {
+ g.GET("/project/:projectId/hook", func(c echo.Context) error {
+ 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)
+ }
+
+ find := &api.ProjectHookFind{
+ ProjectId: &projectId,
+ }
+ list, err := s.ProjectHookService.FindProjectHookList(context.Background(), find)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch webhook list for project ID: %d", projectId)).SetInternal(err)
+ }
+
+ for _, hook := range list {
+ if err := s.ComposeProjectHookRelationship(context.Background(), hook); err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch webhook relationship: %v", hook.Name)).SetInternal(err)
+ }
+ }
+
+ c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
+ if err := jsonapi.MarshalPayload(c.Response().Writer, list); 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/hook", func(c echo.Context) error {
+ 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.ProjectHookCreate{
+ CreatorId: c.Get(GetPrincipalIdContextKey()).(int),
+ ProjectId: projectId,
+ }
+ if err := jsonapi.UnmarshalPayload(c.Request().Body, hookCreate); err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, "Malformatted create project webhook request").SetInternal(err)
+ }
+
+ hook, err := s.ProjectHookService.CreateProjectHook(context.Background(), hookCreate)
+ if err != nil {
+ if bytebase.ErrorCode(err) == bytebase.ECONFLICT {
+ return echo.NewHTTPError(http.StatusConflict, fmt.Sprintf("Webhook url already exists in the project: %s", hook.URL))
+ }
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create project webhook").SetInternal(err)
+ }
+
+ if err := s.ComposeProjectHookRelationship(context.Background(), hook); err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch webhook relationship").SetInternal(err)
+ }
+
+ c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
+ if err := jsonapi.MarshalPayload(c.Response().Writer, hook); err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal create project webhook response").SetInternal(err)
+ }
+ return nil
+ })
+
+ g.PATCH("/project/:projectId/hook/:hookId", func(c echo.Context) error {
+ _, 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("hookId"))
+ if err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Webhook ID is not a number: %s", c.Param("hookId"))).SetInternal(err)
+ }
+
+ hookPatch := &api.ProjectHookPatch{
+ ID: id,
+ UpdaterId: c.Get(GetPrincipalIdContextKey()).(int),
+ }
+ if err := jsonapi.UnmarshalPayload(c.Request().Body, hookPatch); err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, "Malformatted change project webhook").SetInternal(err)
+ }
+
+ hook, err := s.ProjectHookService.PatchProjectHook(context.Background(), hookPatch)
+ if err != nil {
+ if bytebase.ErrorCode(err) == bytebase.ENOTFOUND {
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Project webhook ID not found: %d", id))
+ }
+ if bytebase.ErrorCode(err) == bytebase.ECONFLICT {
+ return echo.NewHTTPError(http.StatusConflict, fmt.Sprintf("Webhook 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)
+ }
+
+ if err := s.ComposeProjectHookRelationship(context.Background(), hook); err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch updated project webhook relationship").SetInternal(err)
+ }
+
+ c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
+ if err := jsonapi.MarshalPayload(c.Response().Writer, hook); 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/hook/:hookId", func(c echo.Context) error {
+ _, 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("hookId"))
+ if err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Webhook ID is not a number: %s", c.Param("hookId"))).SetInternal(err)
+ }
+
+ hookDelete := &api.ProjectHookDelete{
+ ID: id,
+ DeleterId: c.Get(GetPrincipalIdContextKey()).(int),
+ }
+ err = s.ProjectHookService.DeleteProjectHook(context.Background(), hookDelete)
+ if err != nil {
+ if bytebase.ErrorCode(err) == bytebase.ENOTFOUND {
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Project webhook ID not found: %d", id))
+ }
+ 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
+ })
+}
+
+func (s *Server) ComposeProjectHookRelationship(ctx context.Context, hook *api.ProjectHook) error {
+ var err error
+
+ hook.Creator, err = s.ComposePrincipalById(context.Background(), hook.CreatorId)
+ if err != nil {
+ return err
+ }
+
+ hook.Updater, err = s.ComposePrincipalById(context.Background(), hook.UpdaterId)
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
diff --git a/server/server.go b/server/server.go
index 5343d18477..37fca9b930 100644
--- a/server/server.go
+++ b/server/server.go
@@ -160,6 +160,7 @@ func NewServer(logger *zap.Logger, version string, host string, port int, fronte
s.registerPrincipalRoutes(apiGroup)
s.registerMemberRoutes(apiGroup)
s.registerProjectRoutes(apiGroup)
+ s.registerProjectHookRoutes(apiGroup)
s.registerProjectMemberRoutes(apiGroup)
s.registerEnvironmentRoutes(apiGroup)
s.registerInstanceRoutes(apiGroup)
diff --git a/store/sqlite.go b/store/sqlite.go
index 1c0f6c6671..6da548cf9f 100644
--- a/store/sqlite.go
+++ b/store/sqlite.go
@@ -337,6 +337,8 @@ func FormatError(err error) error {
return bytebase.Errorf(bytebase.ECONFLICT, "environment name already exists")
case "UNIQUE constraint failed: project.key":
return bytebase.Errorf(bytebase.ECONFLICT, "project key already exists")
+ case "UNIQUE constraint failed: project_hook.project_id, project_hook.url":
+ return bytebase.Errorf(bytebase.ECONFLICT, "webhook url already exists")
case "UNIQUE constraint failed: project_member.project_id, project_member.principal_id":
return bytebase.Errorf(bytebase.ECONFLICT, "project member already exists")
case "UNIQUE constraint failed: db.instance_id, db.name":