feat(features): support percentage rollouts in feature:enable

Accept a target like "25%" in feature:enable to activate a feature for a
random sample of the current users, persisted via Pennant. Avoids editing
the feature class and redeploying for a percentage-based rollout.
This commit is contained in:
Víctor Falcón 2026-06-25 12:40:35 +02:00
parent 9a458b1031
commit cbe318debc
2 changed files with 48 additions and 1 deletions

View File

@ -11,7 +11,7 @@ class FeatureEnableCommand extends Command
{
use ResolvesFeatures;
protected $signature = 'feature:enable {feature : The feature name (class name or string-based feature)} {target : User email or "all" for everyone}';
protected $signature = 'feature:enable {feature : The feature name (class name or string-based feature)} {target : User email, "all" for everyone, or a percentage like "25%" for a random rollout}';
protected $description = 'Enable a feature for a specific user or all users';
@ -36,6 +36,10 @@ class FeatureEnableCommand extends Command
return self::SUCCESS;
}
if (preg_match('/^(\d{1,3})%$/', $target, $matches)) {
return $this->enableForPercentage($featureClass, $featureName, (int) $matches[1]);
}
$user = User::where('email', $target)->first();
if (! $user) {
@ -49,4 +53,23 @@ class FeatureEnableCommand extends Command
return self::SUCCESS;
}
private function enableForPercentage(string $featureClass, string $featureName, int $percentage): int
{
if ($percentage < 1 || $percentage > 100) {
$this->error('Percentage must be between 1 and 100.');
return self::FAILURE;
}
$count = (int) ceil(User::count() * $percentage / 100);
User::inRandomOrder()
->take($count)
->each(fn (User $user) => Feature::for($user)->activate($featureClass));
$this->info("Feature '{$featureName}' enabled for {$count} users (~{$percentage}% of current users).");
return self::SUCCESS;
}
}

View File

@ -0,0 +1,24 @@
<?php
use App\Features\AiConsentSettings;
use App\Models\User;
use Laravel\Pennant\Feature;
test('enables a feature for a percentage of users', function () {
$users = User::factory()->count(10)->create();
$this->artisan('feature:enable', ['feature' => 'AiConsentSettings', 'target' => '40%'])
->expectsOutputToContain('enabled for 4 users')
->assertSuccessful();
$enabled = $users->filter(fn (User $user) => Feature::for($user)->active(AiConsentSettings::class));
expect($enabled)->toHaveCount(4);
});
test('rejects an out-of-range percentage', function () {
User::factory()->create();
$this->artisan('feature:enable', ['feature' => 'AiConsentSettings', 'target' => '0%'])
->assertFailed();
});