Skip to content
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: 8 additions & 0 deletions src/VCS/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ abstract public function getOwnerName(string $installationId): string;
*/
abstract public function searchRepositories(string $installationId, string $owner, int $page, int $per_page, string $search = ''): array;

/**
* Get repository for the installation
*
* @param string $repositoryName
* @return array<mixed>
*/
abstract public function getInstallationRepository(string $repositoryName): array;

/**
* Get repository
*
Expand Down
35 changes: 35 additions & 0 deletions src/VCS/Adapter/Git/GitHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,41 @@ public function searchRepositories(string $installationId, string $owner, int $p
];
}

public function getInstallationRepository(string $repositoryName): array
{
$currentPage = 1;
$perPage = 100;
$totalRepositories = 0;
$maxRepositories = 1000;
$url = '/installation/repositories';

while ($totalRepositories < $maxRepositories) {
$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "Bearer $this->accessToken"], [
'page' => $currentPage,
'per_page' => $perPage,
]);

if (!isset($response['body']['repositories'])) {
throw new Exception("Repositories list missing in the response.");
}

foreach ($response['body']['repositories'] as $repo) {
if (\strtolower($repo['name']) === \strtolower($repositoryName)) {
return $repo;
}
}

if (\count($response['body']['repositories']) < $perPage) {
break;
}

$currentPage++;
$totalRepositories += $perPage;
}

throw new RepositoryNotFound("Repository not found.");
}

/**
* Get GitHub repository
*
Expand Down
5 changes: 5 additions & 0 deletions src/VCS/Adapter/Git/Gitea.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ public function searchRepositories(string $installationId, string $owner, int $p
throw new Exception("Not implemented yet");
}

public function getInstallationRepository(string $repositoryName): array
{
throw new Exception("Not implemented yet");
}

public function getRepository(string $owner, string $repositoryName): array
{
$url = "/repos/{$owner}/{$repositoryName}";
Expand Down
17 changes: 17 additions & 0 deletions tests/VCS/Adapter/GitHubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,23 @@ public function testGetComment(): void
$this->assertNotEmpty($result);
}

public function testGetInstallationRepository(): void
{
$repositoryName = 'astro-starter';
$repo = $this->vcsAdapter->getInstallationRepository($repositoryName);
$this->assertIsArray($repo);
$this->assertSame($repositoryName, $repo['name']);
}

public function testGetRepository(): void
{
$owner = 'vermakhushboo';
$repositoryName = 'basic-js-crud';
$repo = $this->vcsAdapter->getRepository($owner, $repositoryName);
$this->assertIsArray($repo);
$this->assertSame($repositoryName, $repo['name']);
}

public function testGetRepositoryName(): void
{
$repositoryName = $this->vcsAdapter->getRepositoryName('432284323');
Expand Down