feat: refresh private repository if updating

This commit is contained in:
Aditya Tripathi 2026-02-25 11:39:43 +00:00
parent cb759b2846
commit b71ad865dc
4 changed files with 145 additions and 114 deletions

View file

@ -4,16 +4,27 @@
<x-modal-input buttonTitle="+ Add GitHub App" title="New GitHub App" closeOutside="false">
<livewire:source.github.create />
</x-modal-input>
@if ($repositories->count() > 0)
</div>
<div class="pb-4">Deploy any public or private Git repositories through a GitHub App.</div>
@if ($repositories->count() > 0)
<div class="flex items-center gap-2 pb-4">
<a target="_blank" class="flex hover:no-underline" href="{{ getInstallationPath($github_app) }}">
<x-forms.button>
Change Repositories on GitHub
<x-external-link />
</x-forms.button>
</a>
@endif
</div>
<div class="pb-4">Deploy any public or private Git repositories through a GitHub App.</div>
<x-forms.button :showLoadingIndicator="false" wire:click.prevent="loadRepositories({{ $github_app->id }})" title="Refresh Repository List">
<svg class="w-4 h-4" wire:loading.remove wire:target="loadRepositories({{ $github_app->id }})" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="M16.023 9.348h4.992v-.001M2.985 19.644v-4.992m0 0h4.992m-4.993 0 3.181 3.183a8.25 8.25 0 0 0 13.803-3.7M4.031 9.865a8.25 8.25 0 0 1 13.803-3.7l3.181 3.182m0-4.991v4.99" />
</svg>
<svg class="w-4 h-4 animate-spin" wire:loading wire:target="loadRepositories({{ $github_app->id }})" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
</svg>
</x-forms.button>
</div>
@endif
@if ($github_apps->count() !== 0)
<div class="flex flex-col gap-2">
@if ($current_step === 'github_apps')

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,126 @@
<?php
use App\Livewire\Project\New\GithubPrivateRepository;
use App\Models\GithubApp;
use App\Models\PrivateKey;
use App\Models\Team;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->team = Team::factory()->create();
$this->user = User::factory()->create();
$this->team->members()->attach($this->user->id, ['role' => 'owner']);
$this->actingAs($this->user);
session(['currentTeam' => $this->team]);
$this->rsaKey = openssl_pkey_new([
'private_key_bits' => 2048,
'private_key_type' => OPENSSL_KEYTYPE_RSA,
]);
openssl_pkey_export($this->rsaKey, $pemKey);
$this->privateKey = PrivateKey::create([
'name' => 'Test Key',
'private_key' => $pemKey,
'team_id' => $this->team->id,
]);
$this->githubApp = GithubApp::create([
'name' => 'Test GitHub App',
'api_url' => 'https://api.github.com',
'html_url' => 'https://github.com',
'custom_user' => 'git',
'custom_port' => 22,
'app_id' => 12345,
'installation_id' => 67890,
'client_id' => 'test-client-id',
'client_secret' => 'test-client-secret',
'webhook_secret' => 'test-webhook-secret',
'private_key_id' => $this->privateKey->id,
'team_id' => $this->team->id,
'is_system_wide' => false,
]);
});
function fakeGithubHttp(array $repositories): void
{
Http::fake([
'https://api.github.com/zen' => Http::response('Keep it logically awesome.', 200, [
'Date' => now()->toRfc7231String(),
]),
'https://api.github.com/app/installations/67890/access_tokens' => Http::response([
'token' => 'fake-installation-token',
], 201),
'https://api.github.com/installation/repositories*' => Http::response([
'total_count' => count($repositories),
'repositories' => $repositories,
], 200),
]);
}
describe('GitHub Private Repository Component', function () {
test('loadRepositories fetches and displays repositories', function () {
$repos = [
['id' => 1, 'name' => 'alpha-repo', 'owner' => ['login' => 'testuser']],
['id' => 2, 'name' => 'beta-repo', 'owner' => ['login' => 'testuser']],
];
fakeGithubHttp($repos);
Livewire::test(GithubPrivateRepository::class, ['type' => 'private-gh-app'])
->assertSet('current_step', 'github_apps')
->call('loadRepositories', $this->githubApp->id)
->assertSet('current_step', 'repository')
->assertSet('total_repositories_count', 2)
->assertSet('selected_repository_id', 1);
});
test('loadRepositories can be called again to refresh the repository list', function () {
$initialRepos = [
['id' => 1, 'name' => 'alpha-repo', 'owner' => ['login' => 'testuser']],
];
fakeGithubHttp($initialRepos);
$component = Livewire::test(GithubPrivateRepository::class, ['type' => 'private-gh-app'])
->call('loadRepositories', $this->githubApp->id)
->assertSet('total_repositories_count', 1);
// Simulate new repos becoming available after changing access on GitHub
$updatedRepos = [
['id' => 1, 'name' => 'alpha-repo', 'owner' => ['login' => 'testuser']],
['id' => 2, 'name' => 'beta-repo', 'owner' => ['login' => 'testuser']],
['id' => 3, 'name' => 'gamma-repo', 'owner' => ['login' => 'testuser']],
];
fakeGithubHttp($updatedRepos);
$component
->call('loadRepositories', $this->githubApp->id)
->assertSet('total_repositories_count', 3)
->assertSet('current_step', 'repository');
});
test('refresh button is visible when repositories are loaded', function () {
$repos = [
['id' => 1, 'name' => 'alpha-repo', 'owner' => ['login' => 'testuser']],
];
fakeGithubHttp($repos);
Livewire::test(GithubPrivateRepository::class, ['type' => 'private-gh-app'])
->call('loadRepositories', $this->githubApp->id)
->assertSeeHtml('title="Refresh Repository List"');
});
test('refresh button is not visible before repositories are loaded', function () {
Livewire::test(GithubPrivateRepository::class, ['type' => 'private-gh-app'])
->assertDontSeeHtml('title="Refresh Repository List"');
});
});