Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated search repositories logic #3

Merged
merged 1 commit into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/VCS/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<mixed>
*
* @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
Expand Down
18 changes: 12 additions & 6 deletions src/VCS/Adapter/Git/GitHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<mixed>
*
* @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'];
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/VCS/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down