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
74 changes: 72 additions & 2 deletions src/VCS/Adapter/Git/Gitea.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,14 +387,84 @@ public function listBranches(string $owner, string $repositoryName): array
throw new Exception("Not implemented yet");
}

/**
* Get details of a commit using commit hash
*
* @param string $owner Owner name of the repository
* @param string $repositoryName Name of the repository
* @param string $commitHash SHA of the commit
* @return array<mixed> Details of the commit
*/
public function getCommit(string $owner, string $repositoryName, string $commitHash): array
{
throw new Exception("Not implemented yet");
$url = "/repos/{$owner}/{$repositoryName}/git/commits/{$commitHash}";

$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "token $this->accessToken"]);

$responseHeaders = $response['headers'] ?? [];
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
if ($responseHeadersStatusCode >= 400) {
throw new Exception("Commit not found or inaccessible");
}

$responseBody = $response['body'] ?? [];
$responseBodyCommit = $responseBody['commit'] ?? [];
$responseBodyCommitAuthor = $responseBodyCommit['author'] ?? [];
$responseBodyAuthor = $responseBody['author'] ?? [];

return [
'commitAuthor' => $responseBodyCommitAuthor['name'] ?? 'Unknown',
'commitMessage' => $responseBodyCommit['message'] ?? 'No message',
'commitAuthorAvatar' => $responseBodyAuthor['avatar_url'] ?? '',
'commitAuthorUrl' => $responseBodyAuthor['html_url'] ?? '',
'commitHash' => $responseBody['sha'] ?? '',
'commitUrl' => $responseBody['html_url'] ?? '',
];
}

/**
* Get latest commit of a branch
*
* @param string $owner Owner name of the repository
* @param string $repositoryName Name of the repository
* @param string $branch Name of the branch
* @return array<mixed> Details of the commit
*/
public function getLatestCommit(string $owner, string $repositoryName, string $branch): array
{
throw new Exception("Not implemented yet");
$query = http_build_query([
'sha' => $branch,
'limit' => 1,
]);
$url = "/repos/{$owner}/{$repositoryName}/commits?{$query}";

$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "token $this->accessToken"]);

$responseHeaders = $response['headers'] ?? [];
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
if ($responseHeadersStatusCode >= 400) {
throw new Exception("Latest commit response failed with status code {$responseHeadersStatusCode}");
}

$responseBody = $response['body'] ?? [];

if (empty($responseBody[0] ?? [])) {
throw new Exception("Latest commit response is missing required information.");
}

$responseBodyFirst = $responseBody[0];
$responseBodyFirstCommit = $responseBodyFirst['commit'] ?? [];
$responseBodyFirstCommitAuthor = $responseBodyFirstCommit['author'] ?? [];
$responseBodyFirstAuthor = $responseBodyFirst['author'] ?? [];

return [
'commitAuthor' => $responseBodyFirstCommitAuthor['name'] ?? 'Unknown',
'commitMessage' => $responseBodyFirstCommit['message'] ?? 'No message',
'commitHash' => $responseBodyFirst['sha'] ?? '',
'commitUrl' => $responseBodyFirst['html_url'] ?? '',
'commitAuthorAvatar' => $responseBodyFirstAuthor['avatar_url'] ?? '',
'commitAuthorUrl' => $responseBodyFirstAuthor['html_url'] ?? '',
];
}

public function updateCommitStatus(string $repositoryName, string $commitHash, string $owner, string $state, string $description = '', string $target_url = '', string $context = ''): void
Expand Down
79 changes: 77 additions & 2 deletions tests/VCS/Adapter/GiteaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,12 +365,87 @@ public function testUpdateComment(): void

public function testGetCommit(): void
{
$this->markTestSkipped('Will be implemented in follow-up PR');
$repositoryName = 'test-get-commit-' . \uniqid();
$this->vcsAdapter->createRepository(self::$owner, $repositoryName, false);

$customMessage = 'Test commit message';
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'README.md', '# Test Commit', $customMessage);

$latestCommit = $this->vcsAdapter->getLatestCommit(self::$owner, $repositoryName, 'main');
$commitHash = $latestCommit['commitHash'];

$result = $this->vcsAdapter->getCommit(self::$owner, $repositoryName, $commitHash);

$this->assertIsArray($result);
$this->assertArrayHasKey('commitHash', $result);
$this->assertArrayHasKey('commitMessage', $result);
$this->assertArrayHasKey('commitAuthor', $result);
$this->assertArrayHasKey('commitUrl', $result);
$this->assertArrayHasKey('commitAuthorAvatar', $result);
$this->assertArrayHasKey('commitAuthorUrl', $result);

$this->assertSame($commitHash, $result['commitHash']);
$this->assertStringContainsString($customMessage, $result['commitMessage']);
$this->assertNotEmpty($result['commitAuthor']);
$this->assertNotEmpty($result['commitUrl']);

$this->vcsAdapter->deleteRepository(self::$owner, $repositoryName);
}
Comment on lines +368 to 393
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Ensure repo cleanup runs on failing happy-path tests too.

In testGetCommit and testGetLatestCommit, cleanup only runs at the end. If any assertion/API call fails earlier, repositories are left behind and can pollute later runs.

Proposed resilient cleanup pattern
     public function testGetCommit(): void
     {
         $repositoryName = 'test-get-commit-' . \uniqid();
         $this->vcsAdapter->createRepository(self::$owner, $repositoryName, false);
-
-        $customMessage = 'Test commit message';
-        $this->vcsAdapter->createFile(self::$owner, $repositoryName, 'README.md', '# Test Commit', $customMessage);
-
-        $latestCommit = $this->vcsAdapter->getLatestCommit(self::$owner, $repositoryName, 'main');
-        $commitHash = $latestCommit['commitHash'];
-
-        $result = $this->vcsAdapter->getCommit(self::$owner, $repositoryName, $commitHash);
-
-        $this->assertIsArray($result);
-        $this->assertArrayHasKey('commitHash', $result);
-        $this->assertArrayHasKey('commitMessage', $result);
-        $this->assertArrayHasKey('commitAuthor', $result);
-        $this->assertArrayHasKey('commitUrl', $result);
-        $this->assertArrayHasKey('commitAuthorAvatar', $result);
-        $this->assertArrayHasKey('commitAuthorUrl', $result);
-
-        $this->assertSame($commitHash, $result['commitHash']);
-        $this->assertStringContainsString($customMessage, $result['commitMessage']);
-        $this->assertNotEmpty($result['commitAuthor']);
-        $this->assertNotEmpty($result['commitUrl']);
-
-        $this->vcsAdapter->deleteRepository(self::$owner, $repositoryName);
+        try {
+            $customMessage = 'Test commit message';
+            $this->vcsAdapter->createFile(self::$owner, $repositoryName, 'README.md', '# Test Commit', $customMessage);
+
+            $latestCommit = $this->vcsAdapter->getLatestCommit(self::$owner, $repositoryName, 'main');
+            $commitHash = $latestCommit['commitHash'];
+
+            $result = $this->vcsAdapter->getCommit(self::$owner, $repositoryName, $commitHash);
+
+            $this->assertIsArray($result);
+            $this->assertArrayHasKey('commitHash', $result);
+            $this->assertArrayHasKey('commitMessage', $result);
+            $this->assertArrayHasKey('commitAuthor', $result);
+            $this->assertArrayHasKey('commitUrl', $result);
+            $this->assertArrayHasKey('commitAuthorAvatar', $result);
+            $this->assertArrayHasKey('commitAuthorUrl', $result);
+
+            $this->assertSame($commitHash, $result['commitHash']);
+            $this->assertStringContainsString($customMessage, $result['commitMessage']);
+            $this->assertNotEmpty($result['commitAuthor']);
+            $this->assertNotEmpty($result['commitUrl']);
+        } finally {
+            $this->vcsAdapter->deleteRepository(self::$owner, $repositoryName);
+        }
     }

     public function testGetLatestCommit(): void
     {
         $repositoryName = 'test-get-latest-commit-' . \uniqid();
         $this->vcsAdapter->createRepository(self::$owner, $repositoryName, false);
-
-        $firstMessage = 'First commit';
-        $secondMessage = 'Second commit';
-        $this->vcsAdapter->createFile(self::$owner, $repositoryName, 'README.md', '# Test', $firstMessage);
-        $this->vcsAdapter->createFile(self::$owner, $repositoryName, 'test.txt', 'test content', $secondMessage);
-
-        $result = $this->vcsAdapter->getLatestCommit(self::$owner, $repositoryName, 'main');
-
-        $this->assertIsArray($result);
-        $this->assertArrayHasKey('commitHash', $result);
-        $this->assertArrayHasKey('commitMessage', $result);
-        $this->assertArrayHasKey('commitAuthor', $result);
-        $this->assertArrayHasKey('commitUrl', $result);
-        $this->assertArrayHasKey('commitAuthorAvatar', $result);
-        $this->assertArrayHasKey('commitAuthorUrl', $result);
-
-        $this->assertNotEmpty($result['commitHash']);
-        $this->assertStringContainsString($secondMessage, $result['commitMessage']);
-        $this->assertNotEmpty($result['commitAuthor']);
-        $this->assertNotEmpty($result['commitUrl']);
-
-        $this->vcsAdapter->deleteRepository(self::$owner, $repositoryName);
+        try {
+            $firstMessage = 'First commit';
+            $secondMessage = 'Second commit';
+            $this->vcsAdapter->createFile(self::$owner, $repositoryName, 'README.md', '# Test', $firstMessage);
+            $this->vcsAdapter->createFile(self::$owner, $repositoryName, 'test.txt', 'test content', $secondMessage);
+
+            $result = $this->vcsAdapter->getLatestCommit(self::$owner, $repositoryName, 'main');
+
+            $this->assertIsArray($result);
+            $this->assertArrayHasKey('commitHash', $result);
+            $this->assertArrayHasKey('commitMessage', $result);
+            $this->assertArrayHasKey('commitAuthor', $result);
+            $this->assertArrayHasKey('commitUrl', $result);
+            $this->assertArrayHasKey('commitAuthorAvatar', $result);
+            $this->assertArrayHasKey('commitAuthorUrl', $result);
+
+            $this->assertNotEmpty($result['commitHash']);
+            $this->assertStringContainsString($secondMessage, $result['commitMessage']);
+            $this->assertNotEmpty($result['commitAuthor']);
+            $this->assertNotEmpty($result['commitUrl']);
+        } finally {
+            $this->vcsAdapter->deleteRepository(self::$owner, $repositoryName);
+        }
     }

Also applies to: 397-421

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/VCS/Adapter/GiteaTest.php` around lines 368 - 393, Wrap repository
creation and test logic in a try/finally (or implement per-test tearDown) so
deleteRepository(self::$owner, $repositoryName) always runs even if assertions
fail; update testGetCommit and testGetLatestCommit to save $repositoryName after
createRepository and perform deleteRepository in the finally block (or register
a cleanup callback) to ensure cleanup on failure.


public function testGetLatestCommit(): void
{
$this->markTestSkipped('Will be implemented in follow-up PR');
$repositoryName = 'test-get-latest-commit-' . \uniqid();
$this->vcsAdapter->createRepository(self::$owner, $repositoryName, false);

$firstMessage = 'First commit';
$secondMessage = 'Second commit';
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'README.md', '# Test', $firstMessage);
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'test.txt', 'test content', $secondMessage);

$result = $this->vcsAdapter->getLatestCommit(self::$owner, $repositoryName, 'main');

$this->assertIsArray($result);
$this->assertArrayHasKey('commitHash', $result);
$this->assertArrayHasKey('commitMessage', $result);
$this->assertArrayHasKey('commitAuthor', $result);
$this->assertArrayHasKey('commitUrl', $result);
$this->assertArrayHasKey('commitAuthorAvatar', $result);
$this->assertArrayHasKey('commitAuthorUrl', $result);

$this->assertNotEmpty($result['commitHash']);
$this->assertStringContainsString($secondMessage, $result['commitMessage']);
$this->assertNotEmpty($result['commitAuthor']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above - assertHasKey and assertEmpty are most minimal assertions - everytime we can assert value, we should.

For example here I would assertSame for commit hash, message, and author (author only if possible, not sure)

$this->assertNotEmpty($result['commitUrl']);

$this->vcsAdapter->deleteRepository(self::$owner, $repositoryName);
}

public function testGetCommitWithInvalidSha(): void
{
$repositoryName = 'test-get-commit-invalid-' . \uniqid();
$this->vcsAdapter->createRepository(self::$owner, $repositoryName, false);
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'README.md', '# Test');

try {
$this->expectException(\Exception::class);
$this->vcsAdapter->getCommit(self::$owner, $repositoryName, 'invalid-sha-12345');
} finally {
$this->vcsAdapter->deleteRepository(self::$owner, $repositoryName);
}
}

public function testGetLatestCommitWithInvalidBranch(): void
{
$repositoryName = 'test-get-latest-commit-invalid-' . \uniqid();
$this->vcsAdapter->createRepository(self::$owner, $repositoryName, false);
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'README.md', '# Test');

try {
$this->expectException(\Exception::class);
$this->vcsAdapter->getLatestCommit(self::$owner, $repositoryName, 'non-existing-branch');
} finally {
$this->vcsAdapter->deleteRepository(self::$owner, $repositoryName);
}
}

public function testGetEvent(): void
Expand Down