CreateWebhook registers a new webhook for a workspace.
(workspaceID string, input models.WebhookCreate)
| 9 | |
| 10 | // CreateWebhook registers a new webhook for a workspace. |
| 11 | func (s *Store) CreateWebhook(workspaceID string, input models.WebhookCreate) (*models.Webhook, error) { |
| 12 | id := newID() |
| 13 | ts := now() |
| 14 | |
| 15 | evts := input.Events |
| 16 | if evts == "" { |
| 17 | evts = `["*"]` |
| 18 | } |
| 19 | |
| 20 | // Encrypt the HMAC secret at rest. With no encryption key configured |
| 21 | // (common on self-host) encrypt() returns the plaintext unchanged, so |
| 22 | // this stays a no-op fallback rather than a hard requirement. |
| 23 | encSecret, err := s.encrypt(input.Secret) |
| 24 | if err != nil { |
| 25 | return nil, fmt.Errorf("encrypt webhook secret: %w", err) |
| 26 | } |
| 27 | |
| 28 | _, err = s.db.Exec(s.q(` |
| 29 | INSERT INTO webhooks (id, workspace_id, url, secret, events, active, created_at, updated_at, failure_count) |
| 30 | VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0) |
| 31 | `), id, workspaceID, input.URL, encSecret, evts, s.dialect.BoolToInt(true), ts, ts) |
| 32 | if err != nil { |
| 33 | return nil, fmt.Errorf("insert webhook: %w", err) |
| 34 | } |
| 35 | |
| 36 | return s.GetWebhook(id) |
| 37 | } |
| 38 | |
| 39 | // GetWebhook retrieves a single webhook by ID. |
| 40 | func (s *Store) GetWebhook(id string) (*models.Webhook, error) { |