diff --git a/src/VCS/Adapter.php b/src/VCS/Adapter.php index c136c3c2..518d82f1 100644 --- a/src/VCS/Adapter.php +++ b/src/VCS/Adapter.php @@ -88,12 +88,16 @@ abstract public function getUser(string $username): array; abstract public function getOwnerName(string $installationId): string; /** - * List repositories for Git App + * Search repositories for GitHub App + * @param string $owner Name of user or org * @param int $page page number * @param int $per_page number of results per page + * @param string $search Query to be searched to filter repo names * @return array + * + * @throws Exception */ - abstract public function listRepositories($page, $per_page): array; + abstract public function searchRepositories(string $owner, int $page, int $per_page, string $search=''): array; /** * Get repository diff --git a/src/VCS/Adapter/Git/GitHub.php b/src/VCS/Adapter/Git/GitHub.php index 35fd8a38..721ee983 100644 --- a/src/VCS/Adapter/Git/GitHub.php +++ b/src/VCS/Adapter/Git/GitHub.php @@ -89,24 +89,30 @@ public function createRepository(string $owner, string $repositoryName, bool $pr } /** - * List repositories for GitHub App + * Search repositories for GitHub App + * @param string $owner Name of user or org * @param int $page page number * @param int $per_page number of results per page + * @param string $search Query to be searched to filter repo names * @return array * * @throws Exception */ - public function listRepositories($page, $per_page): array + public function searchRepositories(string $owner, int $page, int $per_page, string $search=''): array { - $url = '/installation/repositories?page=' . $page . '&per_page=' . $per_page; + $url = '/search/repositories'; - $response = $this->call(self::METHOD_GET, $url, ['Authorization' => "Bearer $this->accessToken"]); + $response = $this->call(self::METHOD_GET, $url, ['Authorization' => "Bearer $this->accessToken"], [ + 'q' => "${search} user:${owner} fork:true", + 'per_page' => $per_page, + 'sort' => 'updated' + ]); - if (!isset($response['body']['repositories'])) { + if (!isset($response['body']['items'])) { throw new Exception("Repositories list missing in the response."); } - return $response['body']['repositories']; + return $response['body']['items']; } /** diff --git a/tests/VCS/Base.php b/tests/VCS/Base.php index b91504dc..4b718396 100644 --- a/tests/VCS/Base.php +++ b/tests/VCS/Base.php @@ -45,9 +45,9 @@ public function testGetOwnerName(): void $this->assertEquals('test-kh', $owner); } - public function testListRepositories(): void + public function testSearchRepositories(): void { - $repos = $this->vcsAdapter->listRepositories(1, 2); + $repos = $this->vcsAdapter->searchRepositories('test-kh', 1, 2); $this->assertCount(2, $repos); }