Skip to content

Commit 5e130c4

Browse files
committed
Add download repository zip method
1 parent f09655f commit 5e130c4

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

src/VCS/Adapter/Git/GitHub.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ public function getUser(): array
119119
*/
120120
public function listRepositoriesForGitHubApp(): array
121121
{
122-
$response = $this->call(self::METHOD_GET, '/installation/repositories', ["Authorization" => "Bearer $this->accessToken"]);
122+
$url = '/installation/repositories';
123+
124+
$response = $this->call(self::METHOD_GET, $url, ["Authorization" => "Bearer $this->accessToken"]);
123125

124126
return $response['body']['repositories'];
125127
}
@@ -132,7 +134,10 @@ public function listRepositoriesForGitHubApp(): array
132134
*/
133135
public function addComment($repoName, $pullRequestNumber)
134136
{
135-
$this->call(self::METHOD_POST, '/repos/' . $this->user . '/' . $repoName . '/issues/' . $pullRequestNumber . '/comments', ["Authorization" => "Bearer $this->accessToken"], ["body" => "hello from Utopia!"]);
137+
$url = '/repos/' . $this->user . '/' . $repoName . '/issues/' . $pullRequestNumber . '/comments';
138+
139+
$this->call(self::METHOD_POST, $url, ["Authorization" => "Bearer $this->accessToken"], ["body" => "hello from Utopia!"]);
140+
136141
return;
137142
}
138143

@@ -144,7 +149,34 @@ public function addComment($repoName, $pullRequestNumber)
144149
*/
145150
public function updateComment($repoName, $commentId)
146151
{
147-
$this->call(self::METHOD_PATCH, '/repos/' . $this->user . '/' . $repoName . '/issues/comments/' . $commentId, ["Authorization" => "Bearer $this->accessToken"], ["body" => "update from Utopia!"]);
152+
$url = '/repos/' . $this->user . '/' . $repoName . '/issues/comments/' . $commentId;
153+
154+
$this->call(self::METHOD_PATCH, $url, ["Authorization" => "Bearer $this->accessToken"], ["body" => "update from Utopia!"]);
155+
148156
return;
149157
}
158+
159+
/**
160+
* Downloads a ZIP archive of a repository.
161+
*
162+
* @param string $repo The name of the repository.
163+
* @param string $ref The name of the commit, branch, or tag to download.
164+
* @param string $path The path of the file or directory to download. Optional.
165+
* @return string The contents of the ZIP archive as a string.
166+
*/
167+
public function downloadRepositoryZip(string $repoName, string $ref, string $path = ''): string
168+
{
169+
// Build the URL for the API request
170+
$url = "/repos/" . $this->user . "/{$repoName}/zipball/{$ref}";
171+
172+
// Add the path parameter to the URL query parameters, if specified
173+
if (!empty($path)) {
174+
$url .= "?path={$path}";
175+
}
176+
177+
$response = $this->call(self::METHOD_GET, $url, ["Authorization" => "Bearer $this->accessToken"]);
178+
179+
// Return the contents of the ZIP archive
180+
return $response['body'];
181+
}
150182
}

tests/VCS/GitHubTest.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ public function testListRepositoriesForGitHubApp(): void
2828
$repos = $this->github->listRepositoriesForGitHubApp();
2929
}
3030

31-
public function testGetRepository(): void
32-
{
33-
$this->github->getRepository("TodoApp");
34-
}
35-
3631
public function testAddComment(): void
3732
{
3833
$this->github->addComment("basic-js-crud", 1);
@@ -42,4 +37,13 @@ public function testUpdateComment(): void
4237
{
4338
$this->github->updateComment("basic-js-crud", 1431560395);
4439
}
40+
41+
public function testDownloadRepositoryZip(): void
42+
{
43+
// download the zip archive of the repo
44+
$zipContents = $this->github->downloadRepositoryZip("gatsby-ecommerce-theme", "main");
45+
46+
// Save the ZIP archive to a file
47+
file_put_contents('hello-world.zip', $zipContents);
48+
}
4549
}

0 commit comments

Comments
 (0)