diff --git a/app/Models/Team.php b/app/Models/Team.php index d5957af5a..4cf639123 100644 --- a/app/Models/Team.php +++ b/app/Models/Team.php @@ -283,8 +283,8 @@ public function deletionBlockers(): array return array_filter([ 'projects' => $this->projects()->count(), 'servers' => $this->servers()->count(), - 'sources' => GithubApp::query()->where('team_id', $this->id)->count() - + GitlabApp::query()->where('team_id', $this->id)->count(), + 'sources' => GithubApp::query()->where('team_id', $this->id)->where('is_system_wide', false)->count() + + GitlabApp::query()->where('team_id', $this->id)->where('is_system_wide', false)->count(), ]); } diff --git a/resources/views/livewire/team/danger-zone.blade.php b/resources/views/livewire/team/danger-zone.blade.php index 3d8d78a91..b31ef2c5a 100644 --- a/resources/views/livewire/team/danger-zone.blade.php +++ b/resources/views/livewire/team/danger-zone.blade.php @@ -85,7 +85,8 @@ class="rounded-lg border border-red-300 bg-red-50 p-4 ring-1 ring-inset ring-red confirmationText="{{ currentTeam()->name }}" confirmationLabel="Enter the team name to confirm permanent deletion" shortConfirmationLabel="Team name" :confirmWithPassword="false" - step2ButtonText="Permanently Delete" /> + step2ButtonText="Permanently Delete" canGate="delete" + :canResource="$team" /> @else Delete team diff --git a/tests/Feature/Team/TeamDeletionTest.php b/tests/Feature/Team/TeamDeletionTest.php index 8d448d2ad..e449a5563 100644 --- a/tests/Feature/Team/TeamDeletionTest.php +++ b/tests/Feature/Team/TeamDeletionTest.php @@ -73,6 +73,39 @@ ->and(PrivateKey::find($privateKey->id))->toBeNull(); }); +test('system-wide git sources do not block team deletion', function () { + Team::forceCreate([ + 'id' => 0, + 'name' => 'Root Team', + 'personal_team' => false, + ]); + + $githubApp = GithubApp::forceCreate([ + 'name' => 'System-wide GitHub source', + 'team_id' => $this->teamToDelete->id, + 'api_url' => 'https://api.github.com', + 'html_url' => 'https://github.com', + 'is_public' => false, + 'is_system_wide' => true, + ]); + $gitlabApp = GitlabApp::forceCreate([ + 'name' => 'System-wide GitLab source', + 'team_id' => $this->teamToDelete->id, + 'api_url' => 'https://gitlab.com/api/v4', + 'html_url' => 'https://gitlab.com', + 'is_public' => false, + 'is_system_wide' => true, + ]); + + expect($this->teamToDelete->isEmpty())->toBeTrue(); + + app(DeleteTeam::class)->handle($this->teamToDelete, $this->owner); + + expect(Team::find($this->teamToDelete->id))->toBeNull() + ->and($githubApp->refresh()->team_id)->toBe(0) + ->and($gitlabApp->refresh()->team_id)->toBe(0); +}); + test('the danger zone names and links blocking resource types', function () { Project::factory()->count(2)->create(['team_id' => $this->teamToDelete->id]);