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
3 changes: 2 additions & 1 deletion src/VCS/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,10 @@ abstract public function listRepositoryLanguages(string $owner, string $reposito
* @param string $owner Owner name of the repository
* @param string $repositoryName Name of the repository
* @param string $path Path to list contents from
* @param string $ref The name of the commit/branch/tag
* @return array<mixed> List of contents at the specified path
*/
abstract public function listRepositoryContents(string $owner, string $repositoryName, string $path = ''): array;
abstract public function listRepositoryContents(string $owner, string $repositoryName, string $path = '', string $ref = ''): array;

/**
* Get details of a commit using commit hash
Expand Down
6 changes: 5 additions & 1 deletion src/VCS/Adapter/Git/GitHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,18 @@ public function listRepositoryLanguages(string $owner, string $repositoryName):
* @param string $owner Owner name of the repository
* @param string $repositoryName Name of the GitHub repository
* @param string $path Path to list contents from
* @param string $ref The name of the commit/branch/tag
* @return array<mixed> List of contents at the specified path
*/
public function listRepositoryContents(string $owner, string $repositoryName, string $path = ''): array
public function listRepositoryContents(string $owner, string $repositoryName, string $path = '', string $ref = ''): array
{
$url = "/repos/$owner/$repositoryName/contents";
if (!empty($path)) {
$url .= "/$path";
}
if (!empty($ref)) {
$url .= "?ref=$ref";
}
Comment on lines +214 to +216
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add URL encoding for the ref parameter to handle special characters.

The implementation correctly adds the ref query parameter, but the value should be URL encoded to handle branch names with special characters (e.g., "feature/new-feature") or tag names with special characters.

Apply this diff to fix the URL encoding:

-        if (!empty($ref)) {
-            $url .= "?ref=$ref";
-        }
+        if (!empty($ref)) {
+            $url .= "?ref=" . urlencode($ref);
+        }
🤖 Prompt for AI Agents
In src/VCS/Adapter/Git/GitHub.php around lines 214 to 216, the ref parameter is
appended to the URL without URL encoding, which can cause issues with special
characters in branch or tag names. Fix this by applying URL encoding to the $ref
value using a function like urlencode() before appending it to the URL query
string.


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

Expand Down
7 changes: 6 additions & 1 deletion tests/VCS/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,17 @@ public function testListRepositoryContents(): void
$this->assertIsArray($contents);
$this->assertNotEmpty($contents);


$contents = $this->vcsAdapter->listRepositoryContents('appwrite', 'appwrite', '');
$this->assertIsArray($contents);
$this->assertNotEmpty($contents);
$this->assertGreaterThan(0, \count($contents));

// Test with ref parameter
$contents = $this->vcsAdapter->listRepositoryContents('appwrite', 'appwrite', '', 'main');
$this->assertIsArray($contents);
$this->assertNotEmpty($contents);
$this->assertGreaterThan(0, \count($contents));

$fileContent = null;
foreach ($contents as $content) {
if ($content['type'] === GitHub::CONTENTS_FILE) {
Expand Down