Add commit URL to Slack notifications with link and backtick formatting

- Add commit_url column to webhook_events and deployments tables
- Extract commit URL from webhook payload (from commit object or repo URL)
- Format Slack messages with backticks for branch and commit SHA
- Link commit SHA to the actual commit URL on the git server
- Keep plain text format for ntfy notifications
This commit is contained in:
2025-12-31 16:29:22 -08:00
parent 4cd12d717c
commit c4362c3143
6 changed files with 109 additions and 45 deletions

View File

@@ -19,6 +19,7 @@ type WebhookEvent struct {
EventType string
Branch string
CommitSHA sql.NullString
CommitURL sql.NullString
Payload sql.NullString
Matched bool
Processed bool
@@ -42,8 +43,8 @@ func (w *WebhookEvent) Save(ctx context.Context) error {
// Reload refreshes the webhook event from the database.
func (w *WebhookEvent) Reload(ctx context.Context) error {
query := `
SELECT id, app_id, event_type, branch, commit_sha, payload,
matched, processed, created_at
SELECT id, app_id, event_type, branch, commit_sha, commit_url,
payload, matched, processed, created_at
FROM webhook_events WHERE id = ?`
row := w.db.QueryRow(ctx, query, w.ID)
@@ -54,11 +55,11 @@ func (w *WebhookEvent) Reload(ctx context.Context) error {
func (w *WebhookEvent) insert(ctx context.Context) error {
query := `
INSERT INTO webhook_events (
app_id, event_type, branch, commit_sha, payload, matched, processed
) VALUES (?, ?, ?, ?, ?, ?, ?)`
app_id, event_type, branch, commit_sha, commit_url, payload, matched, processed
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
result, err := w.db.Exec(ctx, query,
w.AppID, w.EventType, w.Branch, w.CommitSHA,
w.AppID, w.EventType, w.Branch, w.CommitSHA, w.CommitURL,
w.Payload, w.Matched, w.Processed,
)
if err != nil {
@@ -86,7 +87,7 @@ func (w *WebhookEvent) update(ctx context.Context) error {
func (w *WebhookEvent) scan(row *sql.Row) error {
return row.Scan(
&w.ID, &w.AppID, &w.EventType, &w.Branch, &w.CommitSHA,
&w.Payload, &w.Matched, &w.Processed, &w.CreatedAt,
&w.CommitURL, &w.Payload, &w.Matched, &w.Processed, &w.CreatedAt,
)
}
@@ -102,8 +103,8 @@ func FindWebhookEvent(
event.ID = id
row := db.QueryRow(ctx, `
SELECT id, app_id, event_type, branch, commit_sha, payload,
matched, processed, created_at
SELECT id, app_id, event_type, branch, commit_sha, commit_url,
payload, matched, processed, created_at
FROM webhook_events WHERE id = ?`,
id,
)
@@ -128,8 +129,8 @@ func FindWebhookEventsByAppID(
limit int,
) ([]*WebhookEvent, error) {
query := `
SELECT id, app_id, event_type, branch, commit_sha, payload,
matched, processed, created_at
SELECT id, app_id, event_type, branch, commit_sha, commit_url,
payload, matched, processed, created_at
FROM webhook_events WHERE app_id = ? ORDER BY created_at DESC LIMIT ?`
rows, err := db.Query(ctx, query, appID, limit)
@@ -146,7 +147,7 @@ func FindWebhookEventsByAppID(
scanErr := rows.Scan(
&event.ID, &event.AppID, &event.EventType, &event.Branch,
&event.CommitSHA, &event.Payload, &event.Matched,
&event.CommitSHA, &event.CommitURL, &event.Payload, &event.Matched,
&event.Processed, &event.CreatedAt,
)
if scanErr != nil {
@@ -165,8 +166,8 @@ func FindUnprocessedWebhookEvents(
db *database.Database,
) ([]*WebhookEvent, error) {
query := `
SELECT id, app_id, event_type, branch, commit_sha, payload,
matched, processed, created_at
SELECT id, app_id, event_type, branch, commit_sha, commit_url,
payload, matched, processed, created_at
FROM webhook_events
WHERE matched = 1 AND processed = 0 ORDER BY created_at ASC`
@@ -184,7 +185,7 @@ func FindUnprocessedWebhookEvents(
scanErr := rows.Scan(
&event.ID, &event.AppID, &event.EventType, &event.Branch,
&event.CommitSHA, &event.Payload, &event.Matched,
&event.CommitSHA, &event.CommitURL, &event.Payload, &event.Matched,
&event.Processed, &event.CreatedAt,
)
if scanErr != nil {