fix(api): infinite loop with github app with many repos (#8052)
Co-authored-by: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com>
This commit is contained in:
parent
14e33ba56a
commit
442d38c277
1 changed files with 18 additions and 13 deletions
|
|
@ -20,6 +20,7 @@
|
|||
use App\Services\DockerImageParser;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Validation\Rule;
|
||||
use OpenApi\Attributes as OA;
|
||||
use Spatie\Url\Url;
|
||||
|
|
@ -1344,24 +1345,28 @@ private function create_application(Request $request, $type)
|
|||
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);
|
||||
$gitRepository = str($gitRepository)->trim('/')->replaceEnd('.git', '')->toString();
|
||||
|
||||
// Use direct API call to verify repository access instead of loading all repositories
|
||||
// This is much faster and avoids timeouts for GitHub Apps with many repositories
|
||||
$response = Http::GitHub($githubApp->api_url, $token)
|
||||
->timeout(20)
|
||||
->retry(3, 200, throw: false)
|
||||
->get("/repos/{$gitRepository}");
|
||||
|
||||
if ($response->status() === 404 || $response->status() === 403) {
|
||||
return response()->json(['message' => 'Repository not found or not accessible by the GitHub App.'], 404);
|
||||
}
|
||||
|
||||
if (! $response->successful()) {
|
||||
return response()->json(['message' => 'Failed to verify repository access: '.($response->json()['message'] ?? 'Unknown error')], 400);
|
||||
}
|
||||
|
||||
$gitRepositoryFound = $response->json();
|
||||
$repository_project_id = data_get($gitRepositoryFound, 'id');
|
||||
|
||||
$application = new Application;
|
||||
|
|
|
|||
Loading…
Reference in a new issue