Skip to content

Commit d0ef4de

Browse files
authored
Merge pull request #66 from jaysomani/feat/gitea-commit-endpoints
feat: Add Gitea commit endpoints (getCommit, getLatestCommit)
2 parents 96f7009 + 66eaa7c commit d0ef4de

File tree

2 files changed

+149
-4
lines changed

2 files changed

+149
-4
lines changed

src/VCS/Adapter/Git/Gitea.php

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,14 +387,84 @@ public function listBranches(string $owner, string $repositoryName): array
387387
throw new Exception("Not implemented yet");
388388
}
389389

390+
/**
391+
* Get details of a commit using commit hash
392+
*
393+
* @param string $owner Owner name of the repository
394+
* @param string $repositoryName Name of the repository
395+
* @param string $commitHash SHA of the commit
396+
* @return array<mixed> Details of the commit
397+
*/
390398
public function getCommit(string $owner, string $repositoryName, string $commitHash): array
391399
{
392-
throw new Exception("Not implemented yet");
400+
$url = "/repos/{$owner}/{$repositoryName}/git/commits/{$commitHash}";
401+
402+
$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "token $this->accessToken"]);
403+
404+
$responseHeaders = $response['headers'] ?? [];
405+
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
406+
if ($responseHeadersStatusCode >= 400) {
407+
throw new Exception("Commit not found or inaccessible");
408+
}
409+
410+
$responseBody = $response['body'] ?? [];
411+
$responseBodyCommit = $responseBody['commit'] ?? [];
412+
$responseBodyCommitAuthor = $responseBodyCommit['author'] ?? [];
413+
$responseBodyAuthor = $responseBody['author'] ?? [];
414+
415+
return [
416+
'commitAuthor' => $responseBodyCommitAuthor['name'] ?? 'Unknown',
417+
'commitMessage' => $responseBodyCommit['message'] ?? 'No message',
418+
'commitAuthorAvatar' => $responseBodyAuthor['avatar_url'] ?? '',
419+
'commitAuthorUrl' => $responseBodyAuthor['html_url'] ?? '',
420+
'commitHash' => $responseBody['sha'] ?? '',
421+
'commitUrl' => $responseBody['html_url'] ?? '',
422+
];
393423
}
394424

425+
/**
426+
* Get latest commit of a branch
427+
*
428+
* @param string $owner Owner name of the repository
429+
* @param string $repositoryName Name of the repository
430+
* @param string $branch Name of the branch
431+
* @return array<mixed> Details of the commit
432+
*/
395433
public function getLatestCommit(string $owner, string $repositoryName, string $branch): array
396434
{
397-
throw new Exception("Not implemented yet");
435+
$query = http_build_query([
436+
'sha' => $branch,
437+
'limit' => 1,
438+
]);
439+
$url = "/repos/{$owner}/{$repositoryName}/commits?{$query}";
440+
441+
$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "token $this->accessToken"]);
442+
443+
$responseHeaders = $response['headers'] ?? [];
444+
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
445+
if ($responseHeadersStatusCode >= 400) {
446+
throw new Exception("Latest commit response failed with status code {$responseHeadersStatusCode}");
447+
}
448+
449+
$responseBody = $response['body'] ?? [];
450+
451+
if (empty($responseBody[0] ?? [])) {
452+
throw new Exception("Latest commit response is missing required information.");
453+
}
454+
455+
$responseBodyFirst = $responseBody[0];
456+
$responseBodyFirstCommit = $responseBodyFirst['commit'] ?? [];
457+
$responseBodyFirstCommitAuthor = $responseBodyFirstCommit['author'] ?? [];
458+
$responseBodyFirstAuthor = $responseBodyFirst['author'] ?? [];
459+
460+
return [
461+
'commitAuthor' => $responseBodyFirstCommitAuthor['name'] ?? 'Unknown',
462+
'commitMessage' => $responseBodyFirstCommit['message'] ?? 'No message',
463+
'commitHash' => $responseBodyFirst['sha'] ?? '',
464+
'commitUrl' => $responseBodyFirst['html_url'] ?? '',
465+
'commitAuthorAvatar' => $responseBodyFirstAuthor['avatar_url'] ?? '',
466+
'commitAuthorUrl' => $responseBodyFirstAuthor['html_url'] ?? '',
467+
];
398468
}
399469

400470
public function updateCommitStatus(string $repositoryName, string $commitHash, string $owner, string $state, string $description = '', string $target_url = '', string $context = ''): void

tests/VCS/Adapter/GiteaTest.php

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,87 @@ public function testUpdateComment(): void
365365

366366
public function testGetCommit(): void
367367
{
368-
$this->markTestSkipped('Will be implemented in follow-up PR');
368+
$repositoryName = 'test-get-commit-' . \uniqid();
369+
$this->vcsAdapter->createRepository(self::$owner, $repositoryName, false);
370+
371+
$customMessage = 'Test commit message';
372+
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'README.md', '# Test Commit', $customMessage);
373+
374+
$latestCommit = $this->vcsAdapter->getLatestCommit(self::$owner, $repositoryName, 'main');
375+
$commitHash = $latestCommit['commitHash'];
376+
377+
$result = $this->vcsAdapter->getCommit(self::$owner, $repositoryName, $commitHash);
378+
379+
$this->assertIsArray($result);
380+
$this->assertArrayHasKey('commitHash', $result);
381+
$this->assertArrayHasKey('commitMessage', $result);
382+
$this->assertArrayHasKey('commitAuthor', $result);
383+
$this->assertArrayHasKey('commitUrl', $result);
384+
$this->assertArrayHasKey('commitAuthorAvatar', $result);
385+
$this->assertArrayHasKey('commitAuthorUrl', $result);
386+
387+
$this->assertSame($commitHash, $result['commitHash']);
388+
$this->assertStringContainsString($customMessage, $result['commitMessage']);
389+
$this->assertNotEmpty($result['commitAuthor']);
390+
$this->assertNotEmpty($result['commitUrl']);
391+
392+
$this->vcsAdapter->deleteRepository(self::$owner, $repositoryName);
369393
}
370394

371395
public function testGetLatestCommit(): void
372396
{
373-
$this->markTestSkipped('Will be implemented in follow-up PR');
397+
$repositoryName = 'test-get-latest-commit-' . \uniqid();
398+
$this->vcsAdapter->createRepository(self::$owner, $repositoryName, false);
399+
400+
$firstMessage = 'First commit';
401+
$secondMessage = 'Second commit';
402+
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'README.md', '# Test', $firstMessage);
403+
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'test.txt', 'test content', $secondMessage);
404+
405+
$result = $this->vcsAdapter->getLatestCommit(self::$owner, $repositoryName, 'main');
406+
407+
$this->assertIsArray($result);
408+
$this->assertArrayHasKey('commitHash', $result);
409+
$this->assertArrayHasKey('commitMessage', $result);
410+
$this->assertArrayHasKey('commitAuthor', $result);
411+
$this->assertArrayHasKey('commitUrl', $result);
412+
$this->assertArrayHasKey('commitAuthorAvatar', $result);
413+
$this->assertArrayHasKey('commitAuthorUrl', $result);
414+
415+
$this->assertNotEmpty($result['commitHash']);
416+
$this->assertStringContainsString($secondMessage, $result['commitMessage']);
417+
$this->assertNotEmpty($result['commitAuthor']);
418+
$this->assertNotEmpty($result['commitUrl']);
419+
420+
$this->vcsAdapter->deleteRepository(self::$owner, $repositoryName);
421+
}
422+
423+
public function testGetCommitWithInvalidSha(): void
424+
{
425+
$repositoryName = 'test-get-commit-invalid-' . \uniqid();
426+
$this->vcsAdapter->createRepository(self::$owner, $repositoryName, false);
427+
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'README.md', '# Test');
428+
429+
try {
430+
$this->expectException(\Exception::class);
431+
$this->vcsAdapter->getCommit(self::$owner, $repositoryName, 'invalid-sha-12345');
432+
} finally {
433+
$this->vcsAdapter->deleteRepository(self::$owner, $repositoryName);
434+
}
435+
}
436+
437+
public function testGetLatestCommitWithInvalidBranch(): void
438+
{
439+
$repositoryName = 'test-get-latest-commit-invalid-' . \uniqid();
440+
$this->vcsAdapter->createRepository(self::$owner, $repositoryName, false);
441+
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'README.md', '# Test');
442+
443+
try {
444+
$this->expectException(\Exception::class);
445+
$this->vcsAdapter->getLatestCommit(self::$owner, $repositoryName, 'non-existing-branch');
446+
} finally {
447+
$this->vcsAdapter->deleteRepository(self::$owner, $repositoryName);
448+
}
374449
}
375450

376451
public function testGetEvent(): void

0 commit comments

Comments
 (0)