feat: improve ControlPlane seeding#2562
Conversation
WalkthroughThis change modifies the seed script to handle existing users and organizations differently. Instead of exiting when a user exists, it now reuses the existing user ID. It adds organization existence validation via group queries and introduces a variable to hold the Keycloak user ID across conditional branches. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@controlplane/src/bin/seed.ts`:
- Around line 80-84: The code assigns keycloakUserID with a non-null assertion
users[0].id! which is unsafe; update the logic in seed.ts where keycloakUserID
is set (look for the users array handling and the users[0].id reference) to
defensively check that users[0] exists and that users[0].id is defined before
assigning to keycloakUserID — if the id is missing, log a clear error (including
the user email/context) and abort/throw so downstream code doesn't run with an
undefined id. Ensure all places that use keycloakUserID (the variable and any
subsequent calls) rely on this validated assignment.
- Around line 96-106: Rename the unclear variable o to existingGroups and stop
using the unreliable search+exact pattern with
keycloakClient.client.groups.find; instead call
keycloakClient.client.groups.find({ realm }) to load groups and then filter the
returned array for an exact name match against user.organization.slug
restricting to root groups (e.g., by checking that the group is a top-level/root
group via parentId being absent or the path depth used in migrate-groups.ts),
then use the filtered existingGroups.length to decide whether to exit. Ensure
you update the identifier from o to existingGroups and follow the same
root-group filtering logic used in migrate-groups.ts when comparing group.name
to user.organization.slug.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2562 +/- ##
===========================================
+ Coverage 43.84% 63.59% +19.75%
===========================================
Files 1034 298 -736
Lines 144938 41917 -103021
Branches 9274 4439 -4835
===========================================
- Hits 63542 26657 -36885
+ Misses 79700 15238 -64462
+ Partials 1696 22 -1674
🚀 New features to boost your workflow:
|
Summary by CodeRabbit
New Features
Bug Fixes
Checklist