Skip to content

Commit

Permalink
Merge pull request #3 from utopia-php/feat-update-list-repos
Browse files Browse the repository at this point in the history
Updated search repositories logic
  • Loading branch information
eldadfux authored Aug 29, 2023
2 parents ec388e0 + 488aa57 commit 24a6c05
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
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

0 comments on commit 24a6c05

Please sign in to comment.