Skip to content

Commit 7638912

Browse files
committed
fix(github): reset branch state when refreshing repositories
Clear `branches` and `total_branches_count` in `loadRepositories` to avoid stale branch data after repo refreshes. Update the Livewire view to use the shared loading button pattern for refresh/load actions, and expand feature coverage for repository refresh behavior and refresh button visibility.
1 parent 25c0c88 commit 7638912

File tree

3 files changed

+64
-30
lines changed

3 files changed

+64
-30
lines changed

app/Livewire/Project/New/GithubPrivateRepository.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ public function updatedBuildPack()
9999
public function loadRepositories($github_app_id)
100100
{
101101
$this->repositories = collect();
102+
$this->branches = collect();
103+
$this->total_branches_count = 0;
102104
$this->page = 1;
103105
$this->selected_github_app_id = $github_app_id;
104106
$this->github_app = GithubApp::where('id', $github_app_id)->first();

resources/views/livewire/project/new/github-private-repository.blade.php

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,18 @@
44
<x-modal-input buttonTitle="+ Add GitHub App" title="New GitHub App" closeOutside="false">
55
<livewire:source.github.create />
66
</x-modal-input>
7+
@if ($repositories->count() > 0)
8+
<x-forms.button wire:click.prevent="loadRepositories({{ $github_app->id }})">
9+
Refresh Repository List
10+
</x-forms.button>
11+
<a target="_blank" class="inline-flex items-center self-center gap-1 text-sm hover:underline dark:text-neutral-400"
12+
href="{{ getInstallationPath($github_app) }}">
13+
Change Repositories on GitHub
14+
<x-external-link />
15+
</a>
16+
@endif
717
</div>
818
<div class="pb-4">Deploy any public or private Git repositories through a GitHub App.</div>
9-
@if ($repositories->count() > 0)
10-
<div class="flex items-center gap-2 pb-4">
11-
<a target="_blank" class="flex hover:no-underline" href="{{ getInstallationPath($github_app) }}">
12-
<x-forms.button>
13-
Change Repositories on GitHub
14-
<x-external-link />
15-
</x-forms.button>
16-
</a>
17-
<x-forms.button :showLoadingIndicator="false" wire:click.prevent="loadRepositories({{ $github_app->id }})" title="Refresh Repository List">
18-
<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">
19-
<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" />
20-
</svg>
21-
<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">
22-
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
23-
<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>
24-
</svg>
25-
</x-forms.button>
26-
</div>
27-
@endif
2819
@if ($github_apps->count() !== 0)
2920
<div class="flex flex-col gap-2">
3021
@if ($current_step === 'github_apps')
@@ -62,7 +53,10 @@
6253
@endforeach
6354
</x-forms.datalist>
6455
</div>
65-
<x-forms.button wire:click.prevent="loadBranches"> Load Repository </x-forms.button>
56+
<x-forms.button :showLoadingIndicator="false" wire:click.prevent="loadBranches" wire:target="loadBranches, selected_repository_id">
57+
Load Repository
58+
<x-loading-on-button wire:loading.delay wire:target="loadBranches, selected_repository_id" />
59+
</x-forms.button>
6660
</div>
6761
@else
6862
<div>No repositories found. Check your GitHub App configuration.</div>

tests/Feature/GithubPrivateRepositoryTest.php

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
'team_id' => $this->team->id,
3232
]);
3333

34-
$this->githubApp = GithubApp::create([
34+
$this->githubApp = GithubApp::forceCreate([
3535
'name' => 'Test GitHub App',
3636
'api_url' => 'https://api.github.com',
3737
'html_url' => 'https://github.com',
@@ -86,25 +86,63 @@ function fakeGithubHttp(array $repositories): void
8686
['id' => 1, 'name' => 'alpha-repo', 'owner' => ['login' => 'testuser']],
8787
];
8888

89-
fakeGithubHttp($initialRepos);
89+
$updatedRepos = [
90+
['id' => 1, 'name' => 'alpha-repo', 'owner' => ['login' => 'testuser']],
91+
['id' => 2, 'name' => 'beta-repo', 'owner' => ['login' => 'testuser']],
92+
['id' => 3, 'name' => 'gamma-repo', 'owner' => ['login' => 'testuser']],
93+
];
94+
95+
$callCount = 0;
96+
Http::fake([
97+
'https://api.github.com/zen' => Http::response('Keep it logically awesome.', 200, [
98+
'Date' => now()->toRfc7231String(),
99+
]),
100+
'https://api.github.com/app/installations/67890/access_tokens' => Http::response([
101+
'token' => 'fake-installation-token',
102+
], 201),
103+
'https://api.github.com/installation/repositories*' => function () use (&$callCount, $initialRepos, $updatedRepos) {
104+
$callCount++;
105+
$repos = $callCount === 1 ? $initialRepos : $updatedRepos;
106+
107+
return Http::response([
108+
'total_count' => count($repos),
109+
'repositories' => $repos,
110+
], 200);
111+
},
112+
]);
90113

91114
$component = Livewire::test(GithubPrivateRepository::class, ['type' => 'private-gh-app'])
92115
->call('loadRepositories', $this->githubApp->id)
93116
->assertSet('total_repositories_count', 1);
94117

95118
// Simulate new repos becoming available after changing access on GitHub
96-
$updatedRepos = [
119+
$component
120+
->call('loadRepositories', $this->githubApp->id)
121+
->assertSet('total_repositories_count', 3)
122+
->assertSet('current_step', 'repository');
123+
});
124+
125+
test('loadRepositories resets branches when refreshing', function () {
126+
$repos = [
97127
['id' => 1, 'name' => 'alpha-repo', 'owner' => ['login' => 'testuser']],
98-
['id' => 2, 'name' => 'beta-repo', 'owner' => ['login' => 'testuser']],
99-
['id' => 3, 'name' => 'gamma-repo', 'owner' => ['login' => 'testuser']],
100128
];
101129

102-
fakeGithubHttp($updatedRepos);
130+
fakeGithubHttp($repos);
131+
132+
$component = Livewire::test(GithubPrivateRepository::class, ['type' => 'private-gh-app'])
133+
->call('loadRepositories', $this->githubApp->id);
134+
135+
// Manually set branches to simulate a previous branch load
136+
$component->set('branches', collect([['name' => 'main'], ['name' => 'develop']]));
137+
$component->set('total_branches_count', 2);
138+
139+
// Refresh repositories should reset branches
140+
fakeGithubHttp($repos);
103141

104142
$component
105143
->call('loadRepositories', $this->githubApp->id)
106-
->assertSet('total_repositories_count', 3)
107-
->assertSet('current_step', 'repository');
144+
->assertSet('total_branches_count', 0)
145+
->assertSet('branches', collect());
108146
});
109147

110148
test('refresh button is visible when repositories are loaded', function () {
@@ -116,11 +154,11 @@ function fakeGithubHttp(array $repositories): void
116154

117155
Livewire::test(GithubPrivateRepository::class, ['type' => 'private-gh-app'])
118156
->call('loadRepositories', $this->githubApp->id)
119-
->assertSeeHtml('title="Refresh Repository List"');
157+
->assertSee('Refresh Repository List');
120158
});
121159

122160
test('refresh button is not visible before repositories are loaded', function () {
123161
Livewire::test(GithubPrivateRepository::class, ['type' => 'private-gh-app'])
124-
->assertDontSeeHtml('title="Refresh Repository List"');
162+
->assertDontSee('Refresh Repository List');
125163
});
126164
});

0 commit comments

Comments
 (0)