Skip to content

Commit 92a1650

Browse files
authored
Merge pull request #65 from utopia-php/ser-1129
Implement `getInstallationRepository` method
2 parents c92d09d + b33588a commit 92a1650

4 files changed

Lines changed: 65 additions & 0 deletions

File tree

src/VCS/Adapter.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ abstract public function getOwnerName(string $installationId): string;
9797
*/
9898
abstract public function searchRepositories(string $installationId, string $owner, int $page, int $per_page, string $search = ''): array;
9999

100+
/**
101+
* Get repository for the installation
102+
*
103+
* @param string $repositoryName
104+
* @return array<mixed>
105+
*/
106+
abstract public function getInstallationRepository(string $repositoryName): array;
107+
100108
/**
101109
* Get repository
102110
*

src/VCS/Adapter/Git/GitHub.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,41 @@ public function searchRepositories(string $installationId, string $owner, int $p
218218
];
219219
}
220220

221+
public function getInstallationRepository(string $repositoryName): array
222+
{
223+
$currentPage = 1;
224+
$perPage = 100;
225+
$totalRepositories = 0;
226+
$maxRepositories = 1000;
227+
$url = '/installation/repositories';
228+
229+
while ($totalRepositories < $maxRepositories) {
230+
$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "Bearer $this->accessToken"], [
231+
'page' => $currentPage,
232+
'per_page' => $perPage,
233+
]);
234+
235+
if (!isset($response['body']['repositories'])) {
236+
throw new Exception("Repositories list missing in the response.");
237+
}
238+
239+
foreach ($response['body']['repositories'] as $repo) {
240+
if (\strtolower($repo['name']) === \strtolower($repositoryName)) {
241+
return $repo;
242+
}
243+
}
244+
245+
if (\count($response['body']['repositories']) < $perPage) {
246+
break;
247+
}
248+
249+
$currentPage++;
250+
$totalRepositories += $perPage;
251+
}
252+
253+
throw new RepositoryNotFound("Repository not found.");
254+
}
255+
221256
/**
222257
* Get GitHub repository
223258
*

src/VCS/Adapter/Git/Gitea.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ public function searchRepositories(string $installationId, string $owner, int $p
123123
throw new Exception("Not implemented yet");
124124
}
125125

126+
public function getInstallationRepository(string $repositoryName): array
127+
{
128+
throw new Exception("Not implemented yet");
129+
}
130+
126131
public function getRepository(string $owner, string $repositoryName): array
127132
{
128133
$url = "/repos/{$owner}/{$repositoryName}";

tests/VCS/Adapter/GitHubTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,23 @@ public function testGetComment(): void
173173
$this->assertNotEmpty($result);
174174
}
175175

176+
public function testGetInstallationRepository(): void
177+
{
178+
$repositoryName = 'astro-starter';
179+
$repo = $this->vcsAdapter->getInstallationRepository($repositoryName);
180+
$this->assertIsArray($repo);
181+
$this->assertSame($repositoryName, $repo['name']);
182+
}
183+
184+
public function testGetRepository(): void
185+
{
186+
$owner = 'vermakhushboo';
187+
$repositoryName = 'basic-js-crud';
188+
$repo = $this->vcsAdapter->getRepository($owner, $repositoryName);
189+
$this->assertIsArray($repo);
190+
$this->assertSame($repositoryName, $repo['name']);
191+
}
192+
176193
public function testGetRepositoryName(): void
177194
{
178195
$repositoryName = $this->vcsAdapter->getRepositoryName('432284323');

0 commit comments

Comments
 (0)