Skip to content

Commit 3560352

Browse files
author
Your Name
committed
updated with suggestions
1 parent 9f1e674 commit 3560352

2 files changed

Lines changed: 113 additions & 44 deletions

File tree

src/VCS/Adapter/Git/Gitea.php

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,14 @@ public function createOrganization(string $orgName): string
129129
*/
130130
public function searchRepositories(string $installationId, string $owner, int $page, int $per_page, string $search = ''): array
131131
{
132-
$allRepos = [];
132+
$filteredRepos = [];
133133
$currentPage = 1;
134+
$maxPages = 50;
134135

135-
while (true) {
136+
$neededForPage = $page * $per_page;
137+
$maxToCollect = $neededForPage + $per_page;
138+
139+
while ($currentPage <= $maxPages) {
136140
$queryParams = [
137141
'page' => $currentPage,
138142
'limit' => 100,
@@ -155,8 +159,12 @@ public function searchRepositories(string $installationId, string $owner, int $p
155159

156160
$responseBody = $response['body'] ?? [];
157161

162+
if (!is_array($responseBody)) {
163+
throw new Exception('Unexpected response body: ' . json_encode($responseBody));
164+
}
165+
158166
if (!array_key_exists('data', $responseBody)) {
159-
throw new Exception("Repositories list missing in the response.");
167+
throw new Exception("Repositories list missing in response: " . json_encode($responseBody));
160168
}
161169

162170
$repos = $responseBody['data'];
@@ -165,7 +173,16 @@ public function searchRepositories(string $installationId, string $owner, int $p
165173
break;
166174
}
167175

168-
$allRepos = array_merge($allRepos, $repos);
176+
foreach ($repos as $repo) {
177+
$repoOwner = $repo['owner']['login'] ?? '';
178+
if ($repoOwner === $owner) {
179+
$filteredRepos[] = $repo;
180+
181+
if (count($filteredRepos) >= $maxToCollect) {
182+
break 2;
183+
}
184+
}
185+
}
169186

170187
if (count($repos) < 100) {
171188
break;
@@ -174,13 +191,6 @@ public function searchRepositories(string $installationId, string $owner, int $p
174191
$currentPage++;
175192
}
176193

177-
$filteredRepos = array_filter($allRepos, function ($repo) use ($owner) {
178-
$repoOwner = $repo['owner']['login'] ?? '';
179-
return $repoOwner === $owner;
180-
});
181-
182-
$filteredRepos = array_values($filteredRepos);
183-
184194
$total = count($filteredRepos);
185195
$offset = ($page - 1) * $per_page;
186196
$pagedRepos = array_slice($filteredRepos, $offset, $per_page);
@@ -433,16 +443,9 @@ public function getUser(string $username): array
433443
throw new Exception("Not implemented yet");
434444
}
435445

436-
/**
437-
* Get owner name
438-
* @param string $installationId In Gitea context, this is the owner name itself
439-
* @return string Owner name
440-
*/
441446
public function getOwnerName(string $installationId): string
442447
{
443-
// Gitea doesn't have GitHub App installation concept
444-
// Return the installationId as-is since it represents the owner
445-
return $installationId;
448+
throw new Exception("getOwnerName() is not applicable for Gitea");
446449
}
447450

448451
public function getPullRequest(string $owner, string $repositoryName, int $pullRequestNumber): array
@@ -464,30 +467,49 @@ public function getPullRequestFromBranch(string $owner, string $repositoryName,
464467
*/
465468
public function listBranches(string $owner, string $repositoryName): array
466469
{
467-
$url = "/repos/{$owner}/{$repositoryName}/branches";
470+
$allBranches = [];
471+
$perPage = 50;
472+
$maxPages = 100;
468473

469-
$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "token $this->accessToken"]);
474+
for ($currentPage = 1; $currentPage <= $maxPages; $currentPage++) {
475+
$url = "/repos/{$owner}/{$repositoryName}/branches?page={$currentPage}&limit={$perPage}";
470476

471-
$responseHeaders = $response['headers'] ?? [];
472-
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
473-
if ($responseHeadersStatusCode >= 400) {
474-
return [];
475-
}
477+
$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "token $this->accessToken"]);
476478

477-
$responseBody = $response['body'] ?? [];
479+
$responseHeaders = $response['headers'] ?? [];
480+
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
478481

479-
if (!is_array($responseBody)) {
480-
return [];
481-
}
482+
if ($responseHeadersStatusCode === 404) {
483+
return [];
484+
}
485+
486+
if ($responseHeadersStatusCode >= 400) {
487+
if ($currentPage === 1) {
488+
throw new Exception("Failed to list branches: HTTP {$responseHeadersStatusCode}");
489+
}
490+
break;
491+
}
482492

483-
$names = [];
484-
foreach ($responseBody as $branch) {
485-
if (is_array($branch) && array_key_exists('name', $branch)) {
486-
$names[] = $branch['name'] ?? '';
493+
$responseBody = $response['body'] ?? [];
494+
495+
if (!is_array($responseBody)) {
496+
break;
497+
}
498+
499+
$pageCount = 0;
500+
foreach ($responseBody as $branch) {
501+
if (is_array($branch) && array_key_exists('name', $branch)) {
502+
$allBranches[] = $branch['name'] ?? '';
503+
$pageCount++;
504+
}
505+
}
506+
507+
if ($pageCount < $perPage) {
508+
break;
487509
}
488510
}
489511

490-
return $names;
512+
return $allBranches;
491513
}
492514

493515
/**

tests/VCS/Adapter/GiteaTest.php

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,48 @@ public function testSearchRepositories(): void
504504
}
505505
}
506506

507+
public function testSearchRepositoriesPagination(): void
508+
{
509+
$repo1 = 'test-pagination-1-' . \uniqid();
510+
$repo2 = 'test-pagination-2-' . \uniqid();
511+
512+
$this->vcsAdapter->createRepository(self::$owner, $repo1, false);
513+
$this->vcsAdapter->createRepository(self::$owner, $repo2, false);
514+
515+
try {
516+
// Test limit=1 only returns 1 repo
517+
$result = $this->vcsAdapter->searchRepositories('', self::$owner, 1, 1, 'test-pagination');
518+
519+
$this->assertSame(1, count($result['items']));
520+
$this->assertGreaterThanOrEqual(2, $result['total']);
521+
522+
// Test page 2
523+
$result2 = $this->vcsAdapter->searchRepositories('', self::$owner, 2, 1, 'test-pagination');
524+
$this->assertSame(1, count($result2['items']));
525+
} finally {
526+
$this->vcsAdapter->deleteRepository(self::$owner, $repo1);
527+
$this->vcsAdapter->deleteRepository(self::$owner, $repo2);
528+
}
529+
}
530+
531+
public function testSearchRepositoriesNoResults(): void
532+
{
533+
$result = $this->vcsAdapter->searchRepositories('', self::$owner, 1, 10, 'nonexistent-repo-xyz-' . \uniqid());
534+
535+
$this->assertIsArray($result);
536+
$this->assertEmpty($result['items']);
537+
$this->assertSame(0, $result['total']);
538+
}
539+
540+
public function testSearchRepositoriesInvalidOwner(): void
541+
{
542+
$result = $this->vcsAdapter->searchRepositories('', 'nonexistent-owner-' . \uniqid(), 1, 10);
543+
544+
$this->assertIsArray($result);
545+
$this->assertEmpty($result['items']);
546+
$this->assertSame(0, $result['total']);
547+
}
548+
507549
public function testDeleteRepository(): void
508550
{
509551
$repositoryName = 'test-delete-repository-' . \uniqid();
@@ -532,12 +574,10 @@ public function testDeleteNonExistingRepositoryFails(): void
532574

533575
public function testGetOwnerName(): void
534576
{
535-
// For Gitea, getOwnerName simply returns the installationId parameter
536-
// since Gitea doesn't have GitHub App installation concept
537-
$result = $this->vcsAdapter->getOwnerName(self::$owner);
577+
$this->expectException(\Exception::class);
578+
$this->expectExceptionMessage('not applicable for Gitea');
538579

539-
$this->assertIsString($result);
540-
$this->assertSame(self::$owner, $result);
580+
$this->vcsAdapter->getOwnerName('');
541581
}
542582

543583
public function testGetPullRequestFromBranch(): void
@@ -561,12 +601,19 @@ public function testListBranches(): void
561601

562602
// Create additional branches
563603
$this->vcsAdapter->createBranch(self::$owner, $repositoryName, 'feature-1', 'main');
564-
sleep(1);
565604
$this->vcsAdapter->createBranch(self::$owner, $repositoryName, 'feature-2', 'main');
566-
sleep(1);
567605

568-
// Test listBranches
569-
$branches = $this->vcsAdapter->listBranches(self::$owner, $repositoryName);
606+
$branches = [];
607+
$maxAttempts = 10;
608+
for ($attempt = 0; $attempt < $maxAttempts; $attempt++) {
609+
$branches = $this->vcsAdapter->listBranches(self::$owner, $repositoryName);
610+
611+
if (in_array('feature-1', $branches, true) && in_array('feature-2', $branches, true)) {
612+
break;
613+
}
614+
615+
usleep(500000);
616+
}
570617

571618
$this->assertIsArray($branches);
572619
$this->assertNotEmpty($branches);

0 commit comments

Comments
 (0)