Skip to content

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

refactor: add ProjectWebhookRaw to represent store model for ProjectWebhook (#915)

Yilong Li66c95d79d263
diff --git a/api/project_webhook.go b/api/project_webhook.go
index 18d55383b9..a300b830c0 100644
--- a/api/project_webhook.go
+++ b/api/project_webhook.go
@@ -5,6 +5,51 @@ import (
"encoding/json"
)
+// ProjectWebhookRaw is the store model for an ProjectWebhook.
+// Fields have exactly the same meanings as ProjectWebhook.
+type ProjectWebhookRaw struct {
+ ID int
+
+ // Standard fields
+ CreatorID int
+ CreatedTs int64
+ UpdaterID int
+ UpdatedTs int64
+
+ // Related fields
+ ProjectID int
+
+ // Domain specific fields
+ Type string
+ Name string
+ URL string
+ ActivityList []string
+}
+
+// ToProjectWebhook creates an instance of ProjectWebhook based on the ProjectWebhookRaw.
+// This is intended to be called when we need to compose an ProjectWebhook relationship.
+func (raw *ProjectWebhookRaw) ToProjectWebhook() *ProjectWebhook {
+ projectWebhook := ProjectWebhook{
+ ID: raw.ID,
+
+ // Standard fields
+ CreatorID: raw.CreatorID,
+ CreatedTs: raw.CreatedTs,
+ UpdaterID: raw.UpdaterID,
+ UpdatedTs: raw.UpdatedTs,
+
+ // Related fields
+ ProjectID: raw.ProjectID,
+
+ // Domain specific fields
+ Type: raw.Type,
+ Name: raw.Name,
+ URL: raw.URL,
+ }
+ projectWebhook.ActivityList = append(projectWebhook.ActivityList, raw.ActivityList...)
+ return &projectWebhook
+}
+
// ProjectWebhook is the API message for project webhooks.
type ProjectWebhook struct {
ID int `jsonapi:"primary,projectWebhookMember"`
@@ -91,9 +136,9 @@ type ProjectWebhookTestResult struct {
// ProjectWebhookService is the service for project webhooks.
type ProjectWebhookService interface {
- CreateProjectWebhook(ctx context.Context, create *ProjectWebhookCreate) (*ProjectWebhook, error)
- FindProjectWebhookList(ctx context.Context, find *ProjectWebhookFind) ([]*ProjectWebhook, error)
- FindProjectWebhook(ctx context.Context, find *ProjectWebhookFind) (*ProjectWebhook, error)
- PatchProjectWebhook(ctx context.Context, patch *ProjectWebhookPatch) (*ProjectWebhook, error)
+ CreateProjectWebhook(ctx context.Context, create *ProjectWebhookCreate) (*ProjectWebhookRaw, error)
+ FindProjectWebhookList(ctx context.Context, find *ProjectWebhookFind) ([]*ProjectWebhookRaw, error)
+ FindProjectWebhook(ctx context.Context, find *ProjectWebhookFind) (*ProjectWebhookRaw, error)
+ PatchProjectWebhook(ctx context.Context, patch *ProjectWebhookPatch) (*ProjectWebhookRaw, error)
DeleteProjectWebhook(ctx context.Context, delete *ProjectWebhookDelete) error
}
diff --git a/server/project_webhook.go b/server/project_webhook.go
index 667702407b..e5fbec9e4c 100644
--- a/server/project_webhook.go
+++ b/server/project_webhook.go
@@ -9,7 +9,7 @@ import (
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
- "github.com/bytebase/bytebase/plugin/webhook"
+ webhookPlugin "github.com/bytebase/bytebase/plugin/webhook"
"github.com/google/jsonapi"
"github.com/labstack/echo/v4"
)
@@ -25,15 +25,17 @@ func (s *Server) registerProjectWebhookRoutes(g *echo.Group) {
find := &api.ProjectWebhookFind{
ProjectID: &projectID,
}
- webhookList, err := s.ProjectWebhookService.FindProjectWebhookList(ctx, find)
+ webhookRawList, err := s.ProjectWebhookService.FindProjectWebhookList(ctx, 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 webhookList {
- if err := s.composeProjectWebhookRelationship(ctx, hook); err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch webhook relationship: %v", hook.Name)).SetInternal(err)
+ var webhookList []*api.ProjectWebhook
+ for _, raw := range webhookRawList {
+ webhook, err := s.composeProjectWebhookRelationship(ctx, raw)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch webhook relationship: %v", raw.Name)).SetInternal(err)
}
+ webhookList = append(webhookList, webhook)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
@@ -58,7 +60,7 @@ func (s *Server) registerProjectWebhookRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted create project webhook request").SetInternal(err)
}
- hook, err := s.ProjectWebhookService.CreateProjectWebhook(ctx, hookCreate)
+ webhookRaw, err := s.ProjectWebhookService.CreateProjectWebhook(ctx, hookCreate)
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))
@@ -66,12 +68,13 @@ func (s *Server) registerProjectWebhookRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create project webhook").SetInternal(err)
}
- if err := s.composeProjectWebhookRelationship(ctx, hook); err != nil {
+ webhook, err := s.composeProjectWebhookRelationship(ctx, webhookRaw)
+ if 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 {
+ if err := jsonapi.MarshalPayload(c.Response().Writer, webhook); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal create project webhook response").SetInternal(err)
}
return nil
@@ -92,20 +95,21 @@ func (s *Server) registerProjectWebhookRoutes(g *echo.Group) {
find := &api.ProjectWebhookFind{
ID: &id,
}
- hook, err := s.ProjectWebhookService.FindProjectWebhook(ctx, find)
+ webhookRaw, err := s.ProjectWebhookService.FindProjectWebhook(ctx, find)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch project webhook ID: %v", id)).SetInternal(err)
}
- if hook == nil {
+ if webhookRaw == nil {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Project webhook ID not found: %d", id))
}
- if err := s.composeProjectWebhookRelationship(ctx, hook); err != nil {
+ webhook, err := s.composeProjectWebhookRelationship(ctx, webhookRaw)
+ if 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 {
+ if err := jsonapi.MarshalPayload(c.Response().Writer, webhook); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal project webhook ID response: %v", id)).SetInternal(err)
}
return nil
@@ -131,7 +135,7 @@ func (s *Server) registerProjectWebhookRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted change project webhook").SetInternal(err)
}
- hook, err := s.ProjectWebhookService.PatchProjectWebhook(ctx, hookPatch)
+ webhookRaw, err := s.ProjectWebhookService.PatchProjectWebhook(ctx, hookPatch)
if err != nil {
if common.ErrorCode(err) == common.NotFound {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Project webhook ID not found: %d", id))
@@ -142,12 +146,13 @@ func (s *Server) registerProjectWebhookRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to change project webhook ID: %v", id)).SetInternal(err)
}
- if err := s.composeProjectWebhookRelationship(ctx, hook); err != nil {
+ webhook, err := s.composeProjectWebhookRelationship(ctx, webhookRaw)
+ if 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 {
+ if err := jsonapi.MarshalPayload(c.Response().Writer, webhook); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal project webhook change response: %v", id)).SetInternal(err)
}
return nil
@@ -204,27 +209,31 @@ func (s *Server) registerProjectWebhookRoutes(g *echo.Group) {
find := &api.ProjectWebhookFind{
ID: &id,
}
- hook, err := s.ProjectWebhookService.FindProjectWebhook(ctx, find)
+ webhookRaw, err := s.ProjectWebhookService.FindProjectWebhook(ctx, find)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch project webhook ID: %v", id)).SetInternal(err)
}
- if hook == nil {
+ if webhookRaw == nil {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Project webhook ID not found: %d", id))
}
+ webhook, err := s.composeProjectWebhookRelationship(ctx, webhookRaw)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to compose project webhook relationship with ID %d", id)).SetInternal(err)
+ }
result := &api.ProjectWebhookTestResult{}
- err = webhook.Post(
- hook.Type,
- webhook.Context{
- URL: hook.URL,
- Level: webhook.WebhookInfo,
- Title: fmt.Sprintf("Test webhook %q", hook.Name),
+ err = webhookPlugin.Post(
+ webhook.Type,
+ webhookPlugin.Context{
+ URL: webhook.URL,
+ Level: webhookPlugin.WebhookInfo,
+ Title: fmt.Sprintf("Test webhook %q", webhook.Name),
Description: "This is a test",
- Link: fmt.Sprintf("%s:%d/project/%s/webhook/%s", s.frontendHost, s.frontendPort, api.ProjectRawSlug(projectRaw), api.ProjectWebhookSlug(hook)),
+ Link: fmt.Sprintf("%s:%d/project/%s/webhook/%s", s.frontendHost, s.frontendPort, api.ProjectRawSlug(projectRaw), api.ProjectWebhookSlug(webhook)),
CreatorName: "Bytebase",
CreatorEmail: "[email protected]",
CreatedTs: time.Now().Unix(),
- MetaList: []webhook.Meta{
+ MetaList: []webhookPlugin.Meta{
{
Name: "Project",
Value: projectRaw.Name,
@@ -245,18 +254,20 @@ func (s *Server) registerProjectWebhookRoutes(g *echo.Group) {
})
}
-func (s *Server) composeProjectWebhookRelationship(ctx context.Context, hook *api.ProjectWebhook) error {
- var err error
+func (s *Server) composeProjectWebhookRelationship(ctx context.Context, raw *api.ProjectWebhookRaw) (*api.ProjectWebhook, error) {
+ webhook := raw.ToProjectWebhook()
- hook.Creator, err = s.composePrincipalByID(ctx, hook.CreatorID)
+ creator, err := s.composePrincipalByID(ctx, webhook.CreatorID)
if err != nil {
- return err
+ return nil, err
}
+ webhook.Creator = creator
- hook.Updater, err = s.composePrincipalByID(ctx, hook.UpdaterID)
+ updater, err := s.composePrincipalByID(ctx, webhook.UpdaterID)
if err != nil {
- return err
+ return nil, err
}
+ webhook.Updater = updater
- return nil
+ return webhook, nil
}
diff --git a/store/project_webhook.go b/store/project_webhook.go
index 7825c6fe6d..5b02f13bf4 100644
--- a/store/project_webhook.go
+++ b/store/project_webhook.go
@@ -28,7 +28,7 @@ func NewProjectWebhookService(logger *zap.Logger, db *DB) *ProjectWebhookService
}
// CreateProjectWebhook creates a new projectWebhook.
-func (s *ProjectWebhookService) CreateProjectWebhook(ctx context.Context, create *api.ProjectWebhookCreate) (*api.ProjectWebhook, error) {
+func (s *ProjectWebhookService) CreateProjectWebhook(ctx context.Context, create *api.ProjectWebhookCreate) (*api.ProjectWebhookRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -48,7 +48,7 @@ func (s *ProjectWebhookService) CreateProjectWebhook(ctx context.Context, create
}
// FindProjectWebhookList retrieves a list of projectWebhooks based on find.
-func (s *ProjectWebhookService) FindProjectWebhookList(ctx context.Context, find *api.ProjectWebhookFind) ([]*api.ProjectWebhook, error) {
+func (s *ProjectWebhookService) FindProjectWebhookList(ctx context.Context, find *api.ProjectWebhookFind) ([]*api.ProjectWebhookRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -65,7 +65,7 @@ func (s *ProjectWebhookService) FindProjectWebhookList(ctx context.Context, find
// FindProjectWebhook retrieves a single projectWebhook based on find.
// Returns ECONFLICT if finding more than 1 matching records.
-func (s *ProjectWebhookService) FindProjectWebhook(ctx context.Context, find *api.ProjectWebhookFind) (*api.ProjectWebhook, error) {
+func (s *ProjectWebhookService) FindProjectWebhook(ctx context.Context, find *api.ProjectWebhookFind) (*api.ProjectWebhookRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -87,7 +87,7 @@ func (s *ProjectWebhookService) FindProjectWebhook(ctx context.Context, find *ap
// PatchProjectWebhook updates an existing projectWebhook by ID.
// Returns ENOTFOUND if projectWebhook does not exist.
-func (s *ProjectWebhookService) PatchProjectWebhook(ctx context.Context, patch *api.ProjectWebhookPatch) (*api.ProjectWebhook, error) {
+func (s *ProjectWebhookService) PatchProjectWebhook(ctx context.Context, patch *api.ProjectWebhookPatch) (*api.ProjectWebhookRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -126,7 +126,7 @@ func (s *ProjectWebhookService) DeleteProjectWebhook(ctx context.Context, delete
}
// createProjectWebhook creates a new projectWebhook.
-func createProjectWebhook(ctx context.Context, tx *sql.Tx, create *api.ProjectWebhookCreate) (*api.ProjectWebhook, error) {
+func createProjectWebhook(ctx context.Context, tx *sql.Tx, create *api.ProjectWebhookCreate) (*api.ProjectWebhookRaw, error) {
// Insert row into database.
row, err := tx.QueryContext(ctx, `
INSERT INTO project_webhook (
@@ -156,26 +156,26 @@ func createProjectWebhook(ctx context.Context, tx *sql.Tx, create *api.ProjectWe
defer row.Close()
row.Next()
- var projectWebhook api.ProjectWebhook
+ var projectWebhookRaw api.ProjectWebhookRaw
if err := row.Scan(
- &projectWebhook.ID,
- &projectWebhook.CreatorID,
- &projectWebhook.CreatedTs,
- &projectWebhook.UpdaterID,
- &projectWebhook.UpdatedTs,
- &projectWebhook.ProjectID,
- &projectWebhook.Type,
- &projectWebhook.Name,
- &projectWebhook.URL,
- pq.Array(&projectWebhook.ActivityList),
+ &projectWebhookRaw.ID,
+ &projectWebhookRaw.CreatorID,
+ &projectWebhookRaw.CreatedTs,
+ &projectWebhookRaw.UpdaterID,
+ &projectWebhookRaw.UpdatedTs,
+ &projectWebhookRaw.ProjectID,
+ &projectWebhookRaw.Type,
+ &projectWebhookRaw.Name,
+ &projectWebhookRaw.URL,
+ pq.Array(&projectWebhookRaw.ActivityList),
); err != nil {
return nil, FormatError(err)
}
- return &projectWebhook, nil
+ return &projectWebhookRaw, nil
}
-func findProjectWebhookList(ctx context.Context, tx *sql.Tx, find *api.ProjectWebhookFind) ([]*api.ProjectWebhook, error) {
+func findProjectWebhookList(ctx context.Context, tx *sql.Tx, find *api.ProjectWebhookFind) ([]*api.ProjectWebhookRaw, error) {
// Build WHERE clause.
where, args := []string{"1 = 1"}, []interface{}{}
if v := find.ID; v != nil {
@@ -206,45 +206,45 @@ func findProjectWebhookList(ctx context.Context, tx *sql.Tx, find *api.ProjectWe
}
defer rows.Close()
- // Iterate over result set and deserialize rows into projectWebhookList.
- var projectWebhookList []*api.ProjectWebhook
+ // Iterate over result set and deserialize rows into projectWebhookRawList.
+ var projectWebhookRawList []*api.ProjectWebhookRaw
for rows.Next() {
- var projectWebhook api.ProjectWebhook
+ var projectWebhookRaw api.ProjectWebhookRaw
if err := rows.Scan(
- &projectWebhook.ID,
- &projectWebhook.CreatorID,
- &projectWebhook.CreatedTs,
- &projectWebhook.UpdaterID,
- &projectWebhook.UpdatedTs,
- &projectWebhook.ProjectID,
- &projectWebhook.Type,
- &projectWebhook.Name,
- &projectWebhook.URL,
- pq.Array(&projectWebhook.ActivityList),
+ &projectWebhookRaw.ID,
+ &projectWebhookRaw.CreatorID,
+ &projectWebhookRaw.CreatedTs,
+ &projectWebhookRaw.UpdaterID,
+ &projectWebhookRaw.UpdatedTs,
+ &projectWebhookRaw.ProjectID,
+ &projectWebhookRaw.Type,
+ &projectWebhookRaw.Name,
+ &projectWebhookRaw.URL,
+ pq.Array(&projectWebhookRaw.ActivityList),
); err != nil {
return nil, FormatError(err)
}
if v := find.ActivityType; v != nil {
- for _, activity := range projectWebhook.ActivityList {
+ for _, activity := range projectWebhookRaw.ActivityList {
if api.ActivityType(activity) == *v {
- projectWebhookList = append(projectWebhookList, &projectWebhook)
+ projectWebhookRawList = append(projectWebhookRawList, &projectWebhookRaw)
break
}
}
} else {
- projectWebhookList = append(projectWebhookList, &projectWebhook)
+ projectWebhookRawList = append(projectWebhookRawList, &projectWebhookRaw)
}
}
if err := rows.Err(); err != nil {
return nil, FormatError(err)
}
- return projectWebhookList, nil
+ return projectWebhookRawList, nil
}
// patchProjectWebhook updates a projectWebhook by ID. Returns the new state of the projectWebhook after update.
-func patchProjectWebhook(ctx context.Context, tx *sql.Tx, patch *api.ProjectWebhookPatch) (*api.ProjectWebhook, error) {
+func patchProjectWebhook(ctx context.Context, tx *sql.Tx, patch *api.ProjectWebhookPatch) (*api.ProjectWebhookRaw, error) {
// Build UPDATE clause.
set, args := []string{"updater_id = $1"}, []interface{}{patch.UpdaterID}
if v := patch.Name; v != nil {
@@ -275,23 +275,23 @@ func patchProjectWebhook(ctx context.Context, tx *sql.Tx, patch *api.ProjectWebh
defer row.Close()
if row.Next() {
- var projectWebhook api.ProjectWebhook
+ var projectWebhookRaw api.ProjectWebhookRaw
if err := row.Scan(
- &projectWebhook.ID,
- &projectWebhook.CreatorID,
- &projectWebhook.CreatedTs,
- &projectWebhook.UpdaterID,
- &projectWebhook.UpdatedTs,
- &projectWebhook.ProjectID,
- &projectWebhook.Type,
- &projectWebhook.Name,
- &projectWebhook.URL,
- pq.Array(&projectWebhook.ActivityList),
+ &projectWebhookRaw.ID,
+ &projectWebhookRaw.CreatorID,
+ &projectWebhookRaw.CreatedTs,
+ &projectWebhookRaw.UpdaterID,
+ &projectWebhookRaw.UpdatedTs,
+ &projectWebhookRaw.ProjectID,
+ &projectWebhookRaw.Type,
+ &projectWebhookRaw.Name,
+ &projectWebhookRaw.URL,
+ pq.Array(&projectWebhookRaw.ActivityList),
); err != nil {
return nil, FormatError(err)
}
- return &projectWebhook, nil
+ return &projectWebhookRaw, nil
}
return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("project hook ID not found: %d", patch.ID)}