Skip to content

Commit 24a6c05

Browse files
authored
Merge pull request #3 from utopia-php/feat-update-list-repos
Updated search repositories logic
2 parents ec388e0 + 488aa57 commit 24a6c05

3 files changed

Lines changed: 20 additions & 10 deletions

File tree

src/VCS/Adapter.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,16 @@ abstract public function getUser(string $username): array;
8888
abstract public function getOwnerName(string $installationId): string;
8989

9090
/**
91-
* List repositories for Git App
91+
* Search repositories for GitHub App
92+
* @param string $owner Name of user or org
9293
* @param int $page page number
9394
* @param int $per_page number of results per page
95+
* @param string $search Query to be searched to filter repo names
9496
* @return array<mixed>
97+
*
98+
* @throws Exception
9599
*/
96-
abstract public function listRepositories($page, $per_page): array;
100+
abstract public function searchRepositories(string $owner, int $page, int $per_page, string $search=''): array;
97101

98102
/**
99103
* Get repository

src/VCS/Adapter/Git/GitHub.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,24 +89,30 @@ public function createRepository(string $owner, string $repositoryName, bool $pr
8989
}
9090

9191
/**
92-
* List repositories for GitHub App
92+
* Search repositories for GitHub App
93+
* @param string $owner Name of user or org
9394
* @param int $page page number
9495
* @param int $per_page number of results per page
96+
* @param string $search Query to be searched to filter repo names
9597
* @return array<mixed>
9698
*
9799
* @throws Exception
98100
*/
99-
public function listRepositories($page, $per_page): array
101+
public function searchRepositories(string $owner, int $page, int $per_page, string $search=''): array
100102
{
101-
$url = '/installation/repositories?page=' . $page . '&per_page=' . $per_page;
103+
$url = '/search/repositories';
102104

103-
$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "Bearer $this->accessToken"]);
105+
$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "Bearer $this->accessToken"], [
106+
'q' => "${search} user:${owner} fork:true",
107+
'per_page' => $per_page,
108+
'sort' => 'updated'
109+
]);
104110

105-
if (!isset($response['body']['repositories'])) {
111+
if (!isset($response['body']['items'])) {
106112
throw new Exception("Repositories list missing in the response.");
107113
}
108114

109-
return $response['body']['repositories'];
115+
return $response['body']['items'];
110116
}
111117

112118
/**

tests/VCS/Base.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public function testGetOwnerName(): void
4545
$this->assertEquals('test-kh', $owner);
4646
}
4747

48-
public function testListRepositories(): void
48+
public function testSearchRepositories(): void
4949
{
50-
$repos = $this->vcsAdapter->listRepositories(1, 2);
50+
$repos = $this->vcsAdapter->searchRepositories('test-kh', 1, 2);
5151
$this->assertCount(2, $repos);
5252
}
5353

0 commit comments

Comments
 (0)