Skip to content

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

refactor: migrate project_webhook columns to payload (#17791)

Danny Xu67ea45f79844
diff --git a/backend/api/v1/project_service.go b/backend/api/v1/project_service.go
index f6568de2b2..1ed1a4d7f9 100644
--- a/backend/api/v1/project_service.go
+++ b/backend/api/v1/project_service.go
@@ -17,6 +17,7 @@ import (
"github.com/pkg/errors"
"google.golang.org/genproto/googleapis/type/expr"
"google.golang.org/protobuf/encoding/protojson"
+ "google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/emptypb"
@@ -919,6 +920,11 @@ func (s *ProjectService) AddWebhook(ctx context.Context, req *connect.Request[v1
return nil, connect.NewError(connect.CodeInvalidArgument, err)
}
+ // Validate webhook URL against allowed domains
+ if err := webhookplugin.ValidateWebhookURL(create.Payload.GetType(), create.Payload.GetUrl()); err != nil {
+ return nil, connect.NewError(connect.CodeInvalidArgument, errors.Wrapf(err, "invalid webhook URL"))
+ }
+
if _, err := s.store.CreateProjectWebhookV2(ctx, project.ResourceID, create); err != nil {
return nil, connect.NewError(connect.CodeInternal, err)
}
@@ -987,15 +993,22 @@ func (s *ProjectService) UpdateWebhook(ctx context.Context, req *connect.Request
return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("webhook %q not found", req.Msg.Webhook.Url))
}
- update := &store.UpdateProjectWebhookMessage{}
+ // Start with existing webhook payload
+ // nolint:revive
+ updatedPayload := proto.Clone(webhook.Payload).(*storepb.ProjectWebhook)
+
for _, path := range req.Msg.UpdateMask.Paths {
switch path {
case "type":
return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("type cannot be updated"))
case "title":
- update.Title = &req.Msg.Webhook.Title
+ updatedPayload.Title = req.Msg.Webhook.Title
case "url":
- update.URL = &req.Msg.Webhook.Url
+ updatedPayload.Url = req.Msg.Webhook.Url
+ // Validate webhook URL against allowed domains
+ if err := webhookplugin.ValidateWebhookURL(updatedPayload.Type, updatedPayload.Url); err != nil {
+ return nil, connect.NewError(connect.CodeInvalidArgument, errors.Wrapf(err, "invalid webhook URL"))
+ }
case "notification_type":
types, err := convertToStoreActivityTypes(req.Msg.Webhook.NotificationTypes)
if err != nil {
@@ -1004,16 +1017,18 @@ func (s *ProjectService) UpdateWebhook(ctx context.Context, req *connect.Request
if len(types) == 0 {
return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("notification types should not be empty"))
}
- update.Events = types
+ updatedPayload.Activities = types
case "direct_message":
- update.Payload = &storepb.ProjectWebhook{
- DirectMessage: req.Msg.Webhook.DirectMessage,
- }
+ updatedPayload.DirectMessage = req.Msg.Webhook.DirectMessage
default:
return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("invalid field %q", path))
}
}
+ update := &store.UpdateProjectWebhookMessage{
+ Payload: updatedPayload,
+ }
+
if _, err := s.store.UpdateProjectWebhookV2(ctx, project.ResourceID, webhook.ID, update); err != nil {
return nil, connect.NewError(connect.CodeInternal, err)
}
@@ -1107,17 +1122,22 @@ func (s *ProjectService) TestWebhook(ctx context.Context, req *connect.Request[v
return nil, connect.NewError(connect.CodeInvalidArgument, err)
}
+ // Validate webhook URL against allowed domains
+ if err := webhookplugin.ValidateWebhookURL(webhook.Payload.GetType(), webhook.Payload.GetUrl()); err != nil {
+ return nil, connect.NewError(connect.CodeInvalidArgument, errors.Wrapf(err, "invalid webhook URL"))
+ }
+
resp := &v1pb.TestWebhookResponse{}
err = webhookplugin.Post(
- webhook.Type,
+ webhook.Payload.GetType(),
webhookplugin.Context{
- URL: webhook.URL,
+ URL: webhook.Payload.GetUrl(),
Level: webhookplugin.WebhookInfo,
EventType: storepb.Activity_ISSUE_CREATE.String(),
- Title: fmt.Sprintf("Test webhook %q", webhook.Title),
- TitleZh: fmt.Sprintf("测试 webhook %q", webhook.Title),
+ Title: fmt.Sprintf("Test webhook %q", webhook.Payload.GetTitle()),
+ TitleZh: fmt.Sprintf("测试 webhook %q", webhook.Payload.GetTitle()),
Description: "This is a test",
- Link: fmt.Sprintf("%s/projects/%s/webhooks/%s", setting.ExternalUrl, project.ResourceID, fmt.Sprintf("%s-%d", slug.Make(webhook.Title), webhook.ID)),
+ Link: fmt.Sprintf("%s/projects/%s/webhooks/%s", setting.ExternalUrl, project.ResourceID, fmt.Sprintf("%s-%d", slug.Make(webhook.Payload.GetTitle()), webhook.ID)),
ActorID: common.SystemBotID,
ActorName: "Bytebase",
ActorEmail: s.store.GetSystemBotUser(ctx).Email,
diff --git a/backend/api/v1/project_service_converter.go b/backend/api/v1/project_service_converter.go
index ed40c8329b..fd1b28818a 100644
--- a/backend/api/v1/project_service_converter.go
+++ b/backend/api/v1/project_service_converter.go
@@ -16,7 +16,6 @@ import (
"github.com/bytebase/bytebase/backend/common/log"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
- webhookplugin "github.com/bytebase/bytebase/backend/plugin/webhook"
"github.com/bytebase/bytebase/backend/store"
"github.com/bytebase/bytebase/backend/utils"
)
@@ -41,21 +40,16 @@ func convertToStoreProjectWebhookMessage(webhook *v1pb.Webhook) (*store.ProjectW
return nil, err
}
- // Validate webhook URL against allowed domains
- if err := webhookplugin.ValidateWebhookURL(storeType, webhook.Url); err != nil {
- return nil, common.Errorf(common.Invalid, "invalid webhook URL: %v", err)
- }
-
activityTypes, err := convertToStoreActivityTypes(webhook.NotificationTypes)
if err != nil {
return nil, err
}
return &store.ProjectWebhookMessage{
- Type: storeType,
- URL: webhook.Url,
- Title: webhook.Title,
- Events: activityTypes,
Payload: &storepb.ProjectWebhook{
+ Type: storeType,
+ Title: webhook.Title,
+ Url: webhook.Url,
+ Activities: activityTypes,
DirectMessage: webhook.DirectMessage,
},
}, nil
@@ -301,10 +295,10 @@ func convertToProject(projectMessage *store.ProjectMessage) *v1pb.Project {
for _, webhook := range projectMessage.Webhooks {
projectWebhooks = append(projectWebhooks, &v1pb.Webhook{
Name: fmt.Sprintf("%s/%s%d", common.FormatProject(projectMessage.ResourceID), common.WebhookIDPrefix, webhook.ID),
- Type: convertToV1WebhookType(webhook.Type),
- Title: webhook.Title,
- Url: webhook.URL,
- NotificationTypes: convertToV1ActivityTypes(webhook.Events),
+ Type: convertToV1WebhookType(webhook.Payload.GetType()),
+ Title: webhook.Payload.GetTitle(),
+ Url: webhook.Payload.GetUrl(),
+ NotificationTypes: convertToV1ActivityTypes(webhook.Payload.GetActivities()),
DirectMessage: webhook.Payload.GetDirectMessage(),
})
}
diff --git a/backend/component/webhook/manager.go b/backend/component/webhook/manager.go
index ec049ed3f2..ad7eeedaf0 100644
--- a/backend/component/webhook/manager.go
+++ b/backend/component/webhook/manager.go
@@ -329,17 +329,17 @@ func (m *Manager) postWebhookList(ctx context.Context, webhookCtx *webhook.Conte
for _, hook := range webhookList {
webhookCtx := *webhookCtx
- webhookCtx.URL = hook.URL
+ webhookCtx.URL = hook.Payload.GetUrl()
webhookCtx.CreatedTS = time.Now().Unix()
webhookCtx.DirectMessage = hook.Payload.GetDirectMessage()
go func(webhookCtx *webhook.Context, hook *store.ProjectWebhookMessage) {
if err := common.Retry(ctx, func() error {
- return webhook.Post(hook.Type, *webhookCtx)
+ return webhook.Post(hook.Payload.GetType(), *webhookCtx)
}); err != nil {
// The external webhook endpoint might be invalid which is out of our code control, so we just emit a warning
slog.Warn("Failed to post webhook event on activity",
- slog.String("webhook type", hook.Type.String()),
- slog.String("webhook name", hook.Title),
+ slog.String("webhook type", hook.Payload.GetType().String()),
+ slog.String("webhook name", hook.Payload.GetTitle()),
slog.String("activity type", webhookCtx.EventType),
slog.String("title", webhookCtx.Title),
log.BBError(err))
diff --git a/backend/generated-go/store/project_webhook.pb.go b/backend/generated-go/store/project_webhook.pb.go
index 04f2e04471..d919da935e 100644
--- a/backend/generated-go/store/project_webhook.pb.go
+++ b/backend/generated-go/store/project_webhook.pb.go
@@ -217,10 +217,18 @@ func (*Activity) Descriptor() ([]byte, []int) {
type ProjectWebhook struct {
state protoimpl.MessageState `protogen:"open.v1"`
+ // Webhook type.
+ Type ProjectWebhook_Type `protobuf:"varint,1,opt,name=type,proto3,enum=bytebase.store.ProjectWebhook_Type" json:"type,omitempty"`
+ // Webhook title.
+ Title string `protobuf:"bytes,2,opt,name=title,proto3" json:"title,omitempty"`
+ // Webhook URL.
+ Url string `protobuf:"bytes,3,opt,name=url,proto3" json:"url,omitempty"`
+ // List of activities that trigger this webhook.
+ Activities []Activity_Type `protobuf:"varint,4,rep,packed,name=activities,proto3,enum=bytebase.store.Activity_Type" json:"activities,omitempty"`
// If direct_message is set, the notification is sent directly
// to the persons and url will be ignored.
// IM integration setting should be set for this function to work.
- DirectMessage bool `protobuf:"varint,1,opt,name=direct_message,json=directMessage,proto3" json:"direct_message,omitempty"`
+ DirectMessage bool `protobuf:"varint,5,opt,name=direct_message,json=directMessage,proto3" json:"direct_message,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
@@ -255,6 +263,34 @@ func (*ProjectWebhook) Descriptor() ([]byte, []int) {
return file_store_project_webhook_proto_rawDescGZIP(), []int{1}
}
+func (x *ProjectWebhook) GetType() ProjectWebhook_Type {
+ if x != nil {
+ return x.Type
+ }
+ return ProjectWebhook_TYPE_UNSPECIFIED
+}
+
+func (x *ProjectWebhook) GetTitle() string {
+ if x != nil {
+ return x.Title
+ }
+ return ""
+}
+
+func (x *ProjectWebhook) GetUrl() string {
+ if x != nil {
+ return x.Url
+ }
+ return ""
+}
+
+func (x *ProjectWebhook) GetActivities() []Activity_Type {
+ if x != nil {
+ return x.Activities
+ }
+ return nil
+}
+
func (x *ProjectWebhook) GetDirectMessage() bool {
if x != nil {
return x.DirectMessage
@@ -278,9 +314,15 @@ const file_store_project_webhook_proto_rawDesc = "" +
"\x13ISSUE_STATUS_UPDATE\x10\x04\x12\x19\n" +
"\x15ISSUE_APPROVAL_NOTIFY\x10\x15\x12&\n" +
"\"ISSUE_PIPELINE_STAGE_STATUS_UPDATE\x10\x05\x12)\n" +
- "%ISSUE_PIPELINE_TASK_RUN_STATUS_UPDATE\x10\x16\"\xa7\x01\n" +
- "\x0eProjectWebhook\x12%\n" +
- "\x0edirect_message\x18\x01 \x01(\bR\rdirectMessage\"n\n" +
+ "%ISSUE_PIPELINE_TASK_RUN_STATUS_UPDATE\x10\x16\"\xc7\x02\n" +
+ "\x0eProjectWebhook\x127\n" +
+ "\x04type\x18\x01 \x01(\x0e2#.bytebase.store.ProjectWebhook.TypeR\x04type\x12\x14\n" +
+ "\x05title\x18\x02 \x01(\tR\x05title\x12\x10\n" +
+ "\x03url\x18\x03 \x01(\tR\x03url\x12=\n" +
+ "\n" +
+ "activities\x18\x04 \x03(\x0e2\x1d.bytebase.store.Activity.TypeR\n" +
+ "activities\x12%\n" +
+ "\x0edirect_message\x18\x05 \x01(\bR\rdirectMessage\"n\n" +
"\x04Type\x12\x14\n" +
"\x10TYPE_UNSPECIFIED\x10\x00\x12\t\n" +
"\x05SLACK\x10\x01\x12\v\n" +
@@ -313,11 +355,13 @@ var file_store_project_webhook_proto_goTypes = []any{
(*ProjectWebhook)(nil), // 3: bytebase.store.ProjectWebhook
}
var file_store_project_webhook_proto_depIdxs = []int32{
- 0, // [0:0] is the sub-list for method output_type
- 0, // [0:0] is the sub-list for method input_type
- 0, // [0:0] is the sub-list for extension type_name
- 0, // [0:0] is the sub-list for extension extendee
- 0, // [0:0] is the sub-list for field type_name
+ 1, // 0: bytebase.store.ProjectWebhook.type:type_name -> bytebase.store.ProjectWebhook.Type
+ 0, // 1: bytebase.store.ProjectWebhook.activities:type_name -> bytebase.store.Activity.Type
+ 2, // [2:2] is the sub-list for method output_type
+ 2, // [2:2] is the sub-list for method input_type
+ 2, // [2:2] is the sub-list for extension type_name
+ 2, // [2:2] is the sub-list for extension extendee
+ 0, // [0:2] is the sub-list for field type_name
}
func init() { file_store_project_webhook_proto_init() }
diff --git a/backend/generated-go/store/project_webhook_equal.pb.go b/backend/generated-go/store/project_webhook_equal.pb.go
index 3d83186195..434a8784f9 100644
--- a/backend/generated-go/store/project_webhook_equal.pb.go
+++ b/backend/generated-go/store/project_webhook_equal.pb.go
@@ -20,6 +20,23 @@ func (x *ProjectWebhook) Equal(y *ProjectWebhook) bool {
if x == nil || y == nil {
return x == nil && y == nil
}
+ if x.Type != y.Type {
+ return false
+ }
+ if x.Title != y.Title {
+ return false
+ }
+ if x.Url != y.Url {
+ return false
+ }
+ if len(x.Activities) != len(y.Activities) {
+ return false
+ }
+ for i := 0; i < len(x.Activities); i++ {
+ if x.Activities[i] != y.Activities[i] {
+ return false
+ }
+ }
if x.DirectMessage != y.DirectMessage {
return false
}
diff --git a/backend/migrator/migration/3.11/0017##migrate_webhook_to_payload.sql b/backend/migrator/migration/3.11/0017##migrate_webhook_to_payload.sql
new file mode 100644
index 0000000000..fbceb30353
--- /dev/null
+++ b/backend/migrator/migration/3.11/0017##migrate_webhook_to_payload.sql
@@ -0,0 +1,22 @@
+-- Migrate project_webhook columns to payload.
+-- This migration moves type, name, url, and event_list fields into the JSONB payload column.
+
+-- Step 1: Backfill payload with existing column data
+UPDATE project_webhook
+SET payload = jsonb_build_object(
+ 'type', type,
+ 'title', name,
+ 'url', url,
+ 'activities', (
+ SELECT jsonb_agg(event)
+ FROM unnest(event_list) AS event
+ ),
+ 'directMessage', COALESCE(payload->'directMessage', 'false'::jsonb)
+)
+WHERE payload = '{}' OR payload->'type' IS NULL;
+
+-- Step 2: Drop the old columns
+ALTER TABLE project_webhook DROP COLUMN IF EXISTS type;
+ALTER TABLE project_webhook DROP COLUMN IF EXISTS name;
+ALTER TABLE project_webhook DROP COLUMN IF EXISTS url;
+ALTER TABLE project_webhook DROP COLUMN IF EXISTS event_list;
diff --git a/backend/migrator/migration/LATEST.sql b/backend/migrator/migration/LATEST.sql
index a287aa2478..ae1a2325d6 100644
--- a/backend/migrator/migration/LATEST.sql
+++ b/backend/migrator/migration/LATEST.sql
@@ -108,12 +108,6 @@ CREATE UNIQUE INDEX idx_project_unique_resource_id ON project(resource_id);
CREATE TABLE project_webhook (
id serial PRIMARY KEY,
project text NOT NULL REFERENCES project(resource_id),
- -- Enum name from ProjectWebhook.Type (proto/store/store/project_webhook.proto)
- type text NOT NULL,
- name text NOT NULL,
- url text NOT NULL,
- -- Array of enum names from Activity.Type (proto/store/store/project_webhook.proto)
- event_list text ARRAY NOT NULL,
-- Stored as ProjectWebhook (proto/store/store/project_webhook.proto)
payload jsonb NOT NULL DEFAULT '{}'
);
diff --git a/backend/migrator/migrator_test.go b/backend/migrator/migrator_test.go
index 7bcd704281..4accd68d42 100644
--- a/backend/migrator/migrator_test.go
+++ b/backend/migrator/migrator_test.go
@@ -12,7 +12,7 @@ import (
func TestLatestVersion(t *testing.T) {
files, err := getSortedVersionedFiles()
require.NoError(t, err)
- require.Equal(t, semver.MustParse("3.11.16"), *files[len(files)-1].version)
+ require.Equal(t, semver.MustParse("3.11.17"), *files[len(files)-1].version)
}
func TestVersionUnique(t *testing.T) {
diff --git a/backend/plugin/webhook/validator.go b/backend/plugin/webhook/validator.go
index d5ad3ceb98..c73b152e1b 100644
--- a/backend/plugin/webhook/validator.go
+++ b/backend/plugin/webhook/validator.go
@@ -10,8 +10,8 @@ import (
)
var (
- // AllowedDomains maps webhook types to their allowed domains.
- AllowedDomains = map[storepb.ProjectWebhook_Type][]string{
+ // allowedDomains maps webhook types to their allowed domains.
+ allowedDomains = map[storepb.ProjectWebhook_Type][]string{
storepb.ProjectWebhook_SLACK: {
"hooks.slack.com",
"hooks.slack-gov.com",
@@ -58,13 +58,13 @@ func ValidateWebhookURL(webhookType storepb.ProjectWebhook_Type, webhookURL stri
}
// Get allowed domains for this webhook type
- allowedDomains, ok := AllowedDomains[webhookType]
+ allowedDomainsForType, ok := allowedDomains[webhookType]
if !ok {
return errors.Errorf("unknown webhook type: %s", webhookType)
}
// Merge with test-only allowed domains
- allAllowedDomains := append([]string{}, allowedDomains...)
+ allAllowedDomains := append([]string{}, allowedDomainsForType...)
if testDomains, exists := TestOnlyAllowedDomains[webhookType]; exists {
allAllowedDomains = append(allAllowedDomains, testDomains...)
}
@@ -88,5 +88,5 @@ func ValidateWebhookURL(webhookType storepb.ProjectWebhook_Type, webhookURL stri
}
return errors.Errorf("webhook URL domain %q is not allowed for webhook type %s (allowed domains: %v)",
- hostname, webhookType, allowedDomains)
+ hostname, webhookType, allowedDomainsForType)
}
diff --git a/backend/store/project_webhook.go b/backend/store/project_webhook.go
index acf736102c..498a8f52fa 100644
--- a/backend/store/project_webhook.go
+++ b/backend/store/project_webhook.go
@@ -6,7 +6,6 @@ import (
"fmt"
"strings"
- "github.com/jackc/pgtype"
"github.com/pkg/errors"
"google.golang.org/protobuf/encoding/protojson"
@@ -16,14 +15,6 @@ import (
// ProjectWebhookMessage is the store model for an project webhook.
type ProjectWebhookMessage struct {
- // Type is the webhook type (e.g. SLACK, DISCORD, etc.).
- Type storepb.ProjectWebhook_Type
- // Title is the webhook name.
- Title string
- // URL is the webhook URL.
- URL string
- // Events is the list of activities that the webhook is interested in.
- Events []storepb.Activity_Type
// Output only fields.
//
// ID is the unique identifier of the project webhook.
@@ -34,12 +25,6 @@ type ProjectWebhookMessage struct {
// UpdateProjectWebhookMessage is the message for updating project webhooks.
type UpdateProjectWebhookMessage struct {
- // Title is the webhook name.
- Title *string
- // URL is the webhook URL.
- URL *string
- // Events is the list of activities that the webhook is interested in.
- Events []storepb.Activity_Type
Payload *storepb.ProjectWebhook
}
@@ -57,13 +42,9 @@ func (s *Store) CreateProjectWebhookV2(ctx context.Context, projectID string, cr
query := `
INSERT INTO project_webhook (
project,
- type,
- name,
- url,
- event_list,
payload
)
- VALUES ($1, $2, $3, $4, $5, $6)
+ VALUES ($1, $2)
RETURNING id
`
payload := []byte("{}")
@@ -75,18 +56,8 @@ func (s *Store) CreateProjectWebhookV2(ctx context.Context, projectID string, cr
payload = p
}
- // Convert events to string array (enum names)
- events := make([]string, len(create.Events))
- for i, event := range create.Events {
- events[i] = event.String()
- }
-
projectWebhook := ProjectWebhookMessage{
- Type: create.Type,
- Title: create.Title,
- URL: create.URL,
- Events: create.Events,
- ProjectID: create.ProjectID,
+ ProjectID: projectID,
Payload: create.Payload,
}
@@ -97,10 +68,6 @@ func (s *Store) CreateProjectWebhookV2(ctx context.Context, projectID string, cr
if err := tx.QueryRowContext(ctx, query,
projectID,
- create.Type.String(),
- create.Title,
- create.URL,
- events,
payload,
).Scan(
&projectWebhook.ID,
@@ -168,79 +135,41 @@ func (s *Store) UpdateProjectWebhookV2(ctx context.Context, projectResourceID st
if err != nil {
return nil, errors.Wrapf(err, "failed to begin transaction")
}
- set, args := []string{}, []any{}
- if v := update.Title; v != nil {
- set, args = append(set, fmt.Sprintf("name = $%d", len(args)+1)), append(args, *v)
- }
- if v := update.URL; v != nil {
- set, args = append(set, fmt.Sprintf("url = $%d", len(args)+1)), append(args, *v)
- }
- if v := update.Events; v != nil {
- // Convert events to string array (enum names)
- events := make([]string, len(v))
- for i, event := range v {
- events[i] = event.String()
- }
- set, args = append(set, fmt.Sprintf("event_list = $%d", len(args)+1)), append(args, events)
- }
- if v := update.Payload; v != nil {
- p, err := protojson.Marshal(v)
+
+ var payload []byte
+ if update.Payload != nil {
+ p, err := protojson.Marshal(update.Payload)
if err != nil {
return nil, errors.Wrapf(err, "failed to marshal payload")
}
- set, args = append(set, fmt.Sprintf("payload = $%d", len(args)+1)), append(args, p)
+ payload = p
}
- args = append(args, projectWebhookID)
-
projectWebhook := ProjectWebhookMessage{
Payload: &storepb.ProjectWebhook{},
}
- var txtArray pgtype.TextArray
- var webhookType string
- var payload []byte
+ var returnedPayload []byte
// Execute update query with RETURNING.
- if err := tx.QueryRowContext(ctx, fmt.Sprintf(`
- UPDATE project_webhook
- SET `+strings.Join(set, ", ")+`
- WHERE id = $%d
- RETURNING id, project, type, name, url, event_list, payload
-`, len(args)),
- args...,
+ if err := tx.QueryRowContext(ctx, `
+ UPDATE project_webhook
+ SET payload = $1
+ WHERE id = $2
+ RETURNING id, project, payload
+ `,
+ payload,
+ projectWebhookID,
).Scan(
&projectWebhook.ID,
&projectWebhook.ProjectID,
- &webhookType,
- &projectWebhook.Title,
- &projectWebhook.URL,
- &txtArray,
- &payload,
+ &returnedPayload,
); err != nil {
if err == sql.ErrNoRows {
return nil, &common.Error{Code: common.NotFound, Err: errors.Errorf("project hook ID not found: %d", projectWebhookID)}
}
return nil, err
}
- webhookTypeValue, ok := storepb.ProjectWebhook_Type_value[webhookType]
- if !ok {
- return nil, errors.Errorf("invalid webhook type in database: %s", webhookType)
- }
- projectWebhook.Type = storepb.ProjectWebhook_Type(webhookTypeValue)
-
- var eventStrs []string
- if err := txtArray.AssignTo(&eventStrs); err != nil {
- return nil, err
- }
- projectWebhook.Events = make([]storepb.Activity_Type, len(eventStrs))
- for i, eventStr := range eventStrs {
- eventValue, ok := storepb.Activity_Type_value[eventStr]
- if !ok {
- return nil, errors.Errorf("invalid activity type in database: %s", eventStr)
- }
- projectWebhook.Events[i] = storepb.Activity_Type(eventValue)
- }
- if err := common.ProtojsonUnmarshaler.Unmarshal(payload, projectWebhook.Payload); err != nil {
+ if err := common.ProtojsonUnmarshaler.Unmarshal(returnedPayload, projectWebhook.Payload); err != nil {
return nil, errors.Wrapf(err, "failed to unmarshal")
}
@@ -281,16 +210,13 @@ func (*Store) findProjectWebhookImplV2(ctx context.Context, txn *sql.Tx, find *F
where, args = append(where, fmt.Sprintf("project = $%d", len(args)+1)), append(args, *v)
}
if v := find.URL; v != nil {
- where, args = append(where, fmt.Sprintf("url = $%d", len(args)+1)), append(args, *v)
+ where, args = append(where, fmt.Sprintf("payload->>'url' = $%d", len(args)+1)), append(args, *v)
}
rows, err := txn.QueryContext(ctx, `
SELECT
id,
- type,
- name,
- url,
- event_list,
+ project,
payload
FROM project_webhook
WHERE `+strings.Join(where, " AND "),
@@ -306,51 +232,31 @@ func (*Store) findProjectWebhookImplV2(ctx context.Context, txn *sql.Tx, find *F
projectWebhook := ProjectWebhookMessage{
Payload: &storepb.ProjectWebhook{},
}
- var txtArray pgtype.TextArray
- var webhookType string
var payload []byte
if err := rows.Scan(
&projectWebhook.ID,
- &webhookType,
- &projectWebhook.Title,
- &projectWebhook.URL,
- &txtArray,
+ &projectWebhook.ProjectID,
&payload,
); err != nil {
return nil, err
}
- webhookTypeValue, ok := storepb.ProjectWebhook_Type_value[webhookType]
- if !ok {
- return nil, errors.Errorf("invalid webhook type in database: %s", webhookType)
- }
- projectWebhook.Type = storepb.ProjectWebhook_Type(webhookTypeValue)
-
- var eventStrs []string
- if err := txtArray.AssignTo(&eventStrs); err != nil {
- return nil, err
- }
- projectWebhook.Events = make([]storepb.Activity_Type, len(eventStrs))
- for i, eventStr := range eventStrs {
- eventValue, ok := storepb.Activity_Type_value[eventStr]
- if !ok {
- return nil, errors.Errorf("invalid activity type in database: %s", eventStr)
- }
- projectWebhook.Events[i] = storepb.Activity_Type(eventValue)
- }
-
if err := common.ProtojsonUnmarshaler.Unmarshal(payload, projectWebhook.Payload); err != nil {
return nil, errors.Wrapf(err, "failed to unmarshal")
}
if v := find.EventType; v != nil {
- for _, activity := range projectWebhook.Events {
+ found := false
+ for _, activity := range projectWebhook.Payload.Activities {
if activity == *v {
- projectWebhooks = append(projectWebhooks, &projectWebhook)
+ found = true
break
}
}
+ if found {
+ projectWebhooks = append(projectWebhooks, &projectWebhook)
+ }
} else {
projectWebhooks = append(projectWebhooks, &projectWebhook)
}
diff --git a/proto/gen/grpc-doc/store/README.md b/proto/gen/grpc-doc/store/README.md
index 4663cf341f..9af0a5ce6b 100644
--- a/proto/gen/grpc-doc/store/README.md
+++ b/proto/gen/grpc-doc/store/README.md
@@ -3881,6 +3881,10 @@ Activity types for webhook notifications.
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
+| type | [ProjectWebhook.Type](#bytebase-store-ProjectWebhook-Type) | | Webhook type. |
+| title | [string](#string) | | Webhook title. |
+| url | [string](#string) | | Webhook URL. |
+| activities | [Activity.Type](#bytebase-store-Activity-Type) | repeated | List of activities that trigger this webhook. |
| direct_message | [bool](#bool) | | If direct_message is set, the notification is sent directly to the persons and url will be ignored. IM integration setting should be set for this function to work. |
diff --git a/proto/gen/grpc-doc/store/index.html b/proto/gen/grpc-doc/store/index.html
index c51b7019f0..9de9ea0b5d 100644
--- a/proto/gen/grpc-doc/store/index.html
+++ b/proto/gen/grpc-doc/store/index.html
@@ -10520,6 +10520,34 @@ If enabled, issues cannot be created when SQL review finds errors. </p></td>
</thead>
<tbody>
+ <tr>
+ <td>type</td>
+ <td><a href="#bytebase.store.ProjectWebhook.Type">ProjectWebhook.Type</a></td>
+ <td></td>
+ <td><p>Webhook type. </p></td>
+ </tr>
+
+ <tr>
+ <td>title</td>
+ <td><a href="#string">string</a></td>
+ <td></td>
+ <td><p>Webhook title. </p></td>
+ </tr>
+
+ <tr>
+ <td>url</td>
+ <td><a href="#string">string</a></td>
+ <td></td>
+ <td><p>Webhook URL. </p></td>
+ </tr>
+
+ <tr>
+ <td>activities</td>
+ <td><a href="#bytebase.store.Activity.Type">Activity.Type</a></td>
+ <td>repeated</td>
+ <td><p>List of activities that trigger this webhook. </p></td>
+ </tr>
+
<tr>
<td>direct_message</td>
<td><a href="#bool">bool</a></td>
diff --git a/proto/store/store/project_webhook.proto b/proto/store/store/project_webhook.proto
index 01724842d2..8631287077 100644
--- a/proto/store/store/project_webhook.proto
+++ b/proto/store/store/project_webhook.proto
@@ -55,8 +55,16 @@ message ProjectWebhook {
LARK = 8;
}
+ // Webhook type.
+ Type type = 1;
+ // Webhook title.
+ string title = 2;
+ // Webhook URL.
+ string url = 3;
+ // List of activities that trigger this webhook.
+ repeated Activity.Type activities = 4;
// If direct_message is set, the notification is sent directly
// to the persons and url will be ignored.
// IM integration setting should be set for this function to work.
- bool direct_message = 1;
+ bool direct_message = 5;
}