diff --git a/app/Http/Controllers/Api/ApplicationsController.php b/app/Http/Controllers/Api/ApplicationsController.php index 2aa656320..cbeb6b55d 100644 --- a/app/Http/Controllers/Api/ApplicationsController.php +++ b/app/Http/Controllers/Api/ApplicationsController.php @@ -932,10 +932,31 @@ class ApplicationsController extends Controller if (! $githubApp) { return response()->json(['message' => 'Github App not found.'], 404); } + $token = generateGithubInstallationToken($githubApp); + if (! $token) { + return response()->json(['message' => 'Failed to generate Github App token.'], 400); + } + + $repositories = collect(); + $page = 1; + $repositories = loadRepositoryByPage($githubApp, $token, $page); + if ($repositories['total_count'] > 0) { + while (count($repositories['repositories']) < $repositories['total_count']) { + $page++; + $repositories = loadRepositoryByPage($githubApp, $token, $page); + } + } + $gitRepository = $request->git_repository; if (str($gitRepository)->startsWith('http') || str($gitRepository)->contains('github.com')) { $gitRepository = str($gitRepository)->replace('https://', '')->replace('http://', '')->replace('github.com/', ''); } + $gitRepositoryFound = collect($repositories['repositories'])->firstWhere('full_name', $gitRepository); + if (! $gitRepositoryFound) { + return response()->json(['message' => 'Repository not found.'], 404); + } + $repository_project_id = data_get($gitRepositoryFound, 'id'); + $application = new Application; removeUnnecessaryFieldsFromRequest($request); @@ -966,6 +987,8 @@ class ApplicationsController extends Controller $application->environment_id = $environment->id; $application->source_type = $githubApp->getMorphClass(); $application->source_id = $githubApp->id; + $application->repository_project_id = $repository_project_id; + $application->save(); $application->refresh(); if (isset($useBuildServer)) { diff --git a/app/Livewire/Project/New/GithubPrivateRepository.php b/app/Livewire/Project/New/GithubPrivateRepository.php index 4a81d841f..b1b0aef15 100644 --- a/app/Livewire/Project/New/GithubPrivateRepository.php +++ b/app/Livewire/Project/New/GithubPrivateRepository.php @@ -106,11 +106,15 @@ class GithubPrivateRepository extends Component $this->selected_github_app_id = $github_app_id; $this->github_app = GithubApp::where('id', $github_app_id)->first(); $this->token = generateGithubInstallationToken($this->github_app); - $this->loadRepositoryByPage(); + $repositories = loadRepositoryByPage($this->github_app, $this->token, $this->page); + $this->total_repositories_count = $repositories['total_count']; + $this->repositories = $this->repositories->concat(collect($repositories['repositories'])); if ($this->repositories->count() < $this->total_repositories_count) { while ($this->repositories->count() < $this->total_repositories_count) { $this->page++; - $this->loadRepositoryByPage(); + $repositories = loadRepositoryByPage($this->github_app, $this->token, $this->page); + $this->total_repositories_count = $repositories['total_count']; + $this->repositories = $this->repositories->concat(collect($repositories['repositories'])); } } $this->repositories = $this->repositories->sortBy('name'); @@ -120,21 +124,6 @@ class GithubPrivateRepository extends Component $this->current_step = 'repository'; } - protected function loadRepositoryByPage() - { - $response = Http::withToken($this->token)->get("{$this->github_app->api_url}/installation/repositories?per_page=100&page={$this->page}"); - $json = $response->json(); - if ($response->status() !== 200) { - return $this->dispatch('error', $json['message']); - } - - if ($json['total_count'] === 0) { - return; - } - $this->total_repositories_count = $json['total_count']; - $this->repositories = $this->repositories->concat(collect($json['repositories'])); - } - public function loadBranches() { $this->selected_repository_owner = $this->repositories->where('id', $this->selected_repository_id)->first()['owner']['login']; diff --git a/bootstrap/helpers/github.php b/bootstrap/helpers/github.php index 3a3f6e7b2..81f8ff18a 100644 --- a/bootstrap/helpers/github.php +++ b/bootstrap/helpers/github.php @@ -129,3 +129,27 @@ function getPermissionsPath(GithubApp $source) return "$github->html_url/settings/apps/$name/permissions"; } + +function loadRepositoryByPage(GithubApp $source, string $token, int $page) +{ + $response = Http::withToken($token)->get("{$source->api_url}/installation/repositories?per_page=100&page={$page}"); + $json = $response->json(); + if ($response->status() !== 200) { + return [ + 'total_count' => 0, + 'repositories' => [], + ]; + } + + if ($json['total_count'] === 0) { + return [ + 'total_count' => 0, + 'repositories' => [], + ]; + } + + return [ + 'total_count' => $json['total_count'], + 'repositories' => $json['repositories'], + ]; +}