Skip to content

refactor: send deploy emails once per bundle#2498

Open
Pitiakova wants to merge 1 commit into
mainfrom
bento-bundle-deploy-email
Open

refactor: send deploy emails once per bundle#2498
Pitiakova wants to merge 1 commit into
mainfrom
bento-bundle-deploy-email

Conversation

@Pitiakova

@Pitiakova Pitiakova commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

Summary (AI generated)

  • Updated deploy history notifications to use the one-time org notification helper.
  • Added the deployed version id to the notification payload and dedupe key so each bundle deploy email is sent once per version.

Motivation (AI generated)

Deploy history inserts can trigger duplicate bundle deployed emails for the same version. This keeps deploy email behavior consistent and avoids repeated notifications to organization members.

Business Impact (AI generated)

Customers receive fewer duplicate deploy emails, reducing notification noise while preserving deployment visibility.

Test Plan (AI generated)

  • Not run; change is limited to notification helper wiring for deploy history creation.

Generated with AI

Made with Cursor

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced deployment notification system to eliminate duplicate notifications sent to organization members while preserving version information in notifications.

Replaced the email notification function with a new notification function that sends notifications once per organization. Updated parameters to include version ID for better tracking.
@coderabbitai

coderabbitai Bot commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The on-deploy trigger handler is updated to dispatch deployment notifications via a de-duplicated notification system instead of email. The implementation now includes version tracking via the version_id field and an idempotency key to prevent duplicate notifications for the same deployment.

Changes

Deploy Notification De-duplication

Layer / File(s) Summary
Notification system replacement with version tracking
supabase/functions/_backend/triggers/on_deploy_history_create.ts
Import changes from sendEmailToOrgMembers to sendNotifToOrgMembersOnce, and the background task enqueued call now includes version_id in the payload and uses an idempotency key based on the bundle version.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested labels

codex

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Description check ❓ Inconclusive The description covers the summary and motivation well, but the test plan checkbox is unchecked and lacks detail, and the checklist items are not completed or addressed. Complete the description by checking relevant checklist items, explaining why E2E tests aren't needed (if applicable), and providing more detail on how the change was tested manually.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: switching from sendEmailToOrgMembers to sendNotifToOrgMembersOnce to deduplicate deploy notifications per bundle version.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install failed: dependency version conflict. Check your lock file or package.json.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot added the codex label Jun 13, 2026
@codspeed-hq

codspeed-hq Bot commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 43 untouched benchmarks
⏩ 2 skipped benchmarks1


Comparing bento-bundle-deploy-email (bb4be0a) with main (a412c78)

Open in CodSpeed

Footnotes

  1. 2 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@supabase/functions/_backend/triggers/on_deploy_history_create.ts`:
- Around line 70-82: The code closes pgClient in the finally block while
backgroundTask(...) returns immediately and the scheduled work still uses
drizzleClient, causing use-after-close errors; fix by moving client lifecycle
into the background task: change the call to backgroundTask(c, async () => {
const pgClient = getPgClient(c, true); const drizzleClient =
getDrizzleClient(pgClient); try { await sendNotifToOrgMembersOnce(...,
drizzleClient); } finally { closeClient(c, pgClient); } }) so the
pgClient/drizzleClient are created and closed inside the background task (or
alternatively await the backgroundTask promise before calling closeClient),
referencing getPgClient, getDrizzleClient, backgroundTask,
sendNotifToOrgMembersOnce, and closeClient.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: f169bcbe-2855-47c7-8e8b-a4766cd88e7d

📥 Commits

Reviewing files that changed from the base of the PR and between a412c78 and bb4be0a.

📒 Files selected for processing (1)
  • supabase/functions/_backend/triggers/on_deploy_history_create.ts
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • Cap-go/capacitor-updater (manual)

Comment on lines 70 to 82
const pgClient = getPgClient(c, true)
const drizzleClient = getDrizzleClient(pgClient)
try {
await backgroundTask(c, sendEmailToOrgMembers(c, 'bundle:deployed', 'bundle_deployed', {
await backgroundTask(c, sendNotifToOrgMembersOnce(c, 'bundle:deployed', 'bundle_deployed', {
org_id: version.owner_org,
app_id: record.app_id,
bundle_name: version.name,
channel_id: record.channel_id,
}, version.owner_org, drizzleClient))
version_id: record.version_id,
}, version.owner_org, `bundle:deployed:${record.version_id}`, drizzleClient))
}
finally {
closeClient(c, pgClient)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Keep the DB client alive for the entire background notification task.

backgroundTask() returns as soon as it registers waitUntil(...) on Workers/Edge, so the finally here closes pgClient while sendNotifToOrgMembersOnce(...) is still using drizzleClient. That makes the new de-duplicated notification path prone to failing only in production runtimes.

💡 Suggested fix
-    const pgClient = getPgClient(c, true)
-    const drizzleClient = getDrizzleClient(pgClient)
-    try {
-      await backgroundTask(c, sendNotifToOrgMembersOnce(c, 'bundle:deployed', 'bundle_deployed', {
-        org_id: version.owner_org,
-        app_id: record.app_id,
-        bundle_name: version.name,
-        channel_id: record.channel_id,
-        version_id: record.version_id,
-      }, version.owner_org, `bundle:deployed:${record.version_id}`, drizzleClient))
-    }
-    finally {
-      closeClient(c, pgClient)
-    }
+    await backgroundTask(c, (async () => {
+      const pgClient = getPgClient(c, true)
+      const drizzleClient = getDrizzleClient(pgClient)
+      try {
+        await sendNotifToOrgMembersOnce(c, 'bundle:deployed', 'bundle_deployed', {
+          org_id: version.owner_org,
+          app_id: record.app_id,
+          bundle_name: version.name,
+          channel_id: record.channel_id,
+          version_id: record.version_id,
+        }, version.owner_org, `bundle:deployed:${record.version_id}`, drizzleClient)
+      }
+      finally {
+        closeClient(c, pgClient)
+      }
+    })())

Based on learnings, backgroundTask() only schedules the promise on workerd/EdgeRuntime, and sendNotifToOrgMembersOnce() consumes the passed drizzleClient during delivery.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const pgClient = getPgClient(c, true)
const drizzleClient = getDrizzleClient(pgClient)
try {
await backgroundTask(c, sendEmailToOrgMembers(c, 'bundle:deployed', 'bundle_deployed', {
await backgroundTask(c, sendNotifToOrgMembersOnce(c, 'bundle:deployed', 'bundle_deployed', {
org_id: version.owner_org,
app_id: record.app_id,
bundle_name: version.name,
channel_id: record.channel_id,
}, version.owner_org, drizzleClient))
version_id: record.version_id,
}, version.owner_org, `bundle:deployed:${record.version_id}`, drizzleClient))
}
finally {
closeClient(c, pgClient)
await backgroundTask(c, (async () => {
const pgClient = getPgClient(c, true)
const drizzleClient = getDrizzleClient(pgClient)
try {
await sendNotifToOrgMembersOnce(c, 'bundle:deployed', 'bundle_deployed', {
org_id: version.owner_org,
app_id: record.app_id,
bundle_name: version.name,
channel_id: record.channel_id,
version_id: record.version_id,
}, version.owner_org, `bundle:deployed:${record.version_id}`, drizzleClient)
}
finally {
closeClient(c, pgClient)
}
})())
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@supabase/functions/_backend/triggers/on_deploy_history_create.ts` around
lines 70 - 82, The code closes pgClient in the finally block while
backgroundTask(...) returns immediately and the scheduled work still uses
drizzleClient, causing use-after-close errors; fix by moving client lifecycle
into the background task: change the call to backgroundTask(c, async () => {
const pgClient = getPgClient(c, true); const drizzleClient =
getDrizzleClient(pgClient); try { await sendNotifToOrgMembersOnce(...,
drizzleClient); } finally { closeClient(c, pgClient); } }) so the
pgClient/drizzleClient are created and closed inside the background task (or
alternatively await the backgroundTask promise before calling closeClient),
referencing getPgClient, getDrizzleClient, backgroundTask,
sendNotifToOrgMembersOnce, and closeClient.

@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant