Skip to content

refactor: add ownerId to create key for backward compat#3312

Merged
chronark merged 3 commits intomainfrom
fix-owner-id-compat
Jun 17, 2025
Merged

refactor: add ownerId to create key for backward compat#3312
chronark merged 3 commits intomainfrom
fix-owner-id-compat

Conversation

@ogzhanolguncu
Copy link
Contributor

@ogzhanolguncu ogzhanolguncu commented Jun 10, 2025

What does this PR do?

This PR adds selected externalId data to owner_id column in our keys table for backward compat.
Fixes # (issue)

If there is not an issue for this, please create one first. This is used to tracking purposes and also helps use understand why this PR exists

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • Chore (refactoring code, technical debt, workflow improvements)
  • [] Enhancement (small improvements)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How should this be tested?

  • Test A
  • Test B

Checklist

Required

  • Filled out the "How to test" section in this PR
  • Read Contributing Guide
  • Self-reviewed my own code
  • Commented on my code in hard-to-understand areas
  • Ran pnpm build
  • Ran pnpm fmt
  • Checked for warnings, there are none
  • Removed all console.logs
  • Merged the latest changes from main onto my branch with git pull origin main
  • My changes don't cause any responsiveness issues

Appreciated

  • If a UI change was made: Added a screen recording or screenshots to this PR
  • Updated the Unkey Docs if changes were necessary

Summary by CodeRabbit

  • New Features
    • Added support for managing both identity and external IDs during key creation and updates.
    • Enhanced form inputs and batch editing to handle and validate separate identity and external ID fields.
  • Bug Fixes
    • Improved key creation and owner updates to ensure accurate assignment of ownership information.

@changeset-bot
Copy link

changeset-bot bot commented Jun 10, 2025

⚠️ No Changeset found

Latest commit: 15952a8

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@vercel
Copy link

vercel bot commented Jun 10, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
dashboard ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 10, 2025 2:52pm
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
engineering ⬜️ Ignored (Inspect) Visit Preview Jun 10, 2025 2:52pm

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 10, 2025

📝 Walkthrough

Walkthrough

The key creation mutation now assigns identityId from input.identityId and sets a new ownerId field to input.externalId in the database. The ExternalIdField component and its usage in the GeneralSetup form were updated to handle both identityId and externalId. Validation schemas and form utilities were extended to include the optional identityId field. Additional components managing external IDs in key editing and batch editing were updated to track and submit both IDs. The key owner update mutation now sets ownerId from externalId instead of null.

Changes

File Change Summary
apps/dashboard/lib/trpc/routers/key/create.ts Mutation updated to set identityId from input.identityId and ownerId from input.externalId.
apps/dashboard/lib/trpc/routers/key/updateOwnerId.ts Added externalId to input schema; ownerId set from input.identity.externalId instead of null.
apps/dashboard/app/(app)/apis/[apiId]/_components/create-key/components/external-id-field.tsx ExternalIdField's onChange prop updated to accept (identityId, externalId) instead of single ID.
apps/dashboard/app/(app)/apis/[apiId]/_components/create-key/components/general-setup.tsx Form controller changed to handle both identityId and externalId from ExternalIdField.
apps/dashboard/app/(app)/apis/[apiId]/_components/create-key/create-key.schema.ts Added optional, nullable identityId field to validation schemas with max length 256.
apps/dashboard/app/(app)/apis/[apiId]/_components/create-key/create-key.utils.ts formValuesToApiInput updated to include identityId property; type assertion expanded.
apps/dashboard/app/(app)/apis/[apiId]/keys/[keyAuthId]/_components/components/table/components/actions/components/edit-external-id/index.tsx Added state for selectedExternalId; updated handleSubmit and ExternalIdField onChange to handle both IDs.
apps/dashboard/app/(app)/apis/[apiId]/keys/[keyAuthId]/_components/components/table/components/selection-controls/components/batch-edit-external-id.tsx Added state for selectedExternalId; updated submit and clear handlers and ExternalIdField onChange to handle both IDs.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ExternalIdField
    participant GeneralSetupForm
    participant TRPC Router
    participant Database

    User->>ExternalIdField: Select or create identity (identityId, externalId)
    ExternalIdField->>GeneralSetupForm: onChange(identityId, externalId)
    GeneralSetupForm->>FormState: Update identityId and externalId fields
    User->>GeneralSetupForm: Submit form
    GeneralSetupForm->>TRPC Router: createKey(input with identityId and externalId)
    TRPC Router->>Database: Insert key (identityId = input.identityId, ownerId = input.externalId)
    Database-->>TRPC Router: Key creation result
    TRPC Router-->>GeneralSetupForm: Response with key details
Loading

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between becb210 and 15952a8.

📒 Files selected for processing (3)
  • apps/dashboard/app/(app)/apis/[apiId]/keys/[keyAuthId]/_components/components/table/components/actions/components/edit-external-id/index.tsx (3 hunks)
  • apps/dashboard/app/(app)/apis/[apiId]/keys/[keyAuthId]/_components/components/table/components/selection-controls/components/batch-edit-external-id.tsx (4 hunks)
  • apps/dashboard/lib/trpc/routers/key/updateOwnerId.ts (2 hunks)
🧰 Additional context used
🧠 Learnings (1)
apps/dashboard/lib/trpc/routers/key/updateOwnerId.ts (1)
Learnt from: chronark
PR: unkeyed/unkey#2693
File: apps/api/src/routes/v1_keys_updateKey.ts:350-368
Timestamp: 2024-11-29T15:15:47.308Z
Learning: In `apps/api/src/routes/v1_keys_updateKey.ts`, the code intentionally handles `externalId` and `ownerId` separately for clarity. The `ownerId` field will be removed in the future, simplifying the code.
⏰ Context from checks skipped due to timeout of 90000ms (8)
  • GitHub Check: Test Packages / Test ./packages/rbac
  • GitHub Check: Test Packages / Test ./packages/cache
  • GitHub Check: Test Packages / Test ./internal/clickhouse
  • GitHub Check: Test Packages / Test ./packages/nextjs
  • GitHub Check: Test Packages / Test ./internal/resend
  • GitHub Check: Test Packages / Test ./internal/billing
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: autofix
🔇 Additional comments (1)
apps/dashboard/lib/trpc/routers/key/updateOwnerId.ts (1)

21-21: LGTM! Clean implementation for backward compatibility.

The addition of the optional externalId field and its assignment to ownerId properly supports the backward compatibility objective without breaking existing functionality.

Also applies to: 198-198

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions
Copy link
Contributor

github-actions bot commented Jun 10, 2025

Thank you for following the naming conventions for pull request titles! 🙏

@vercel vercel bot temporarily deployed to Preview – dashboard June 10, 2025 14:09 Inactive
@chronark chronark self-requested a review June 10, 2025 14:10
Copy link
Contributor Author

dont dont

Copy link
Contributor Author

dont merge it

Copy link
Collaborator

lol that was like 10 min ago

Copy link
Contributor Author

I got baited by slack :pepeout:

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 848134a and becb210.

📒 Files selected for processing (5)
  • apps/dashboard/app/(app)/apis/[apiId]/_components/create-key/components/external-id-field.tsx (3 hunks)
  • apps/dashboard/app/(app)/apis/[apiId]/_components/create-key/components/general-setup.tsx (2 hunks)
  • apps/dashboard/app/(app)/apis/[apiId]/_components/create-key/create-key.schema.ts (2 hunks)
  • apps/dashboard/app/(app)/apis/[apiId]/_components/create-key/create-key.utils.ts (2 hunks)
  • apps/dashboard/lib/trpc/routers/key/create.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/dashboard/lib/trpc/routers/key/create.ts
🧰 Additional context used
🧬 Code Graph Analysis (1)
apps/dashboard/app/(app)/apis/[apiId]/_components/create-key/components/general-setup.tsx (1)
apps/dashboard/app/(app)/apis/[apiId]/_components/create-key/components/external-id-field.tsx (1)
  • ExternalIdField (17-158)
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: Test Packages / Test ./packages/cache
  • GitHub Check: Test Packages / Test ./packages/nextjs
  • GitHub Check: Test Packages / Test ./packages/hono
  • GitHub Check: Test Packages / Test ./internal/clickhouse
  • GitHub Check: autofix
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (8)
apps/dashboard/app/(app)/apis/[apiId]/_components/create-key/create-key.utils.ts (2)

23-23: LGTM! Consistent addition of identityId field.

The addition of identityId to the API input follows the same pattern as externalId with proper null handling.


80-83: LGTM! Improved code readability.

The multi-line type assertion format improves readability without changing functionality.

apps/dashboard/app/(app)/apis/[apiId]/_components/create-key/create-key.schema.ts (2)

63-68: LGTM! Consistent schema definition for identityId.

The identityId field definition mirrors the externalId validation rules appropriately.


323-326: LGTM! Consistent API schema definition.

The identityId field in the API schema maintains consistency with the form schema validation.

apps/dashboard/app/(app)/apis/[apiId]/_components/create-key/components/general-setup.tsx (1)

11-11: LGTM! Added setValue for dual field management.

The addition of setValue enables proper management of both identityId and externalId fields.

apps/dashboard/app/(app)/apis/[apiId]/_components/create-key/components/external-id-field.tsx (3)

12-12: LGTM! Enhanced onChange signature for dual field handling.

The updated onChange prop type correctly accepts both identityId and externalId parameters to support the new data flow.


27-27: LGTM! Consistent callback update.

The createIdentity callback correctly passes both identityId and externalId to the onChange handler.


100-100: LGTM! Proper dual parameter handling.

The onSelect handler correctly extracts and passes both the identity ID and external ID to the onChange callback.

Comment on lines +40 to +49
name="identityId"
control={control}
defaultValue=""
render={({ field }) => (
<ExternalIdField
value={field.value ?? null}
onChange={field.onChange}
onChange={(identityId: string | null, externalId: string | null) => {
field.onChange(identityId);
setValue("externalId", externalId);
}}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix error handling inconsistency.

The Controller now manages identityId but the error handling on line 50 still references errors.externalId?.message. This could display the wrong error message.

           <ExternalIdField
             value={field.value ?? null}
             onChange={(identityId: string | null, externalId: string | null) => {
               field.onChange(identityId);
               setValue("externalId", externalId);
             }}
-            error={errors.externalId?.message}
+            error={errors.identityId?.message}
           />
📝 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
name="identityId"
control={control}
defaultValue=""
render={({ field }) => (
<ExternalIdField
value={field.value ?? null}
onChange={field.onChange}
onChange={(identityId: string | null, externalId: string | null) => {
field.onChange(identityId);
setValue("externalId", externalId);
}}
name="identityId"
control={control}
defaultValue=""
render={({ field }) => (
<ExternalIdField
value={field.value ?? null}
onChange={(identityId: string | null, externalId: string | null) => {
field.onChange(identityId);
setValue("externalId", externalId);
}}
error={errors.identityId?.message}
/>
)}
🤖 Prompt for AI Agents
In
apps/dashboard/app/(app)/apis/[apiId]/_components/create-key/components/general-setup.tsx
around lines 40 to 49, the error handling for the Controller managing identityId
incorrectly references errors.externalId?.message. Update the error reference to
errors.identityId?.message to ensure the correct error message is displayed for
the identityId field.

@vercel vercel bot temporarily deployed to Preview – dashboard June 10, 2025 14:38 Inactive
Copy link
Contributor Author

Okay should be good to go now

@vercel vercel bot temporarily deployed to Preview – dashboard June 10, 2025 14:52 Inactive
@chronark chronark enabled auto-merge June 17, 2025 15:46
@chronark chronark disabled auto-merge June 17, 2025 15:53
@chronark chronark merged commit 27f05d0 into main Jun 17, 2025
61 of 67 checks passed
@chronark chronark deleted the fix-owner-id-compat branch June 17, 2025 15:53
@coderabbitai coderabbitai bot mentioned this pull request Jun 19, 2025
18 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants