Skip to content

Commit 68d1605

Browse files
committed
Add forkRepository() method
1 parent 5e130c4 commit 68d1605

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/vendor/
22
/.idea/
33
.phpunit.result.cache
4-
.env
4+
.env
5+
.DS_Store

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ services:
88
- ./src:/usr/local/src/src
99
- ./tests:/usr/local/src/tests
1010
- ./phpunit.xml:/usr/local/src/phpunit.xml
11+
- /Users/khushbooverma/desktop:/usr/local/src/desktop
1112
environment:
1213
- GITHUB_PRIVATE_KEY
1314
- GITHUB_APP_IDENTIFIER

src/VCS/Adapter/Git/GitHub.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,31 @@ public function downloadRepositoryZip(string $repoName, string $ref, string $pat
179179
// Return the contents of the ZIP archive
180180
return $response['body'];
181181
}
182+
183+
/**
184+
* Forks a repository on GitHub.
185+
*
186+
* @param string $owner The owner of the repository to fork.
187+
* @param string $repo The name of the repository to fork.
188+
* @param string|null $organization The name of the organization to fork the repository into. If not provided, the repository will be forked into the authenticated user's account.
189+
* @param string|null $name The name of the new forked repository. If not provided, the name will be the same as the original repository.
190+
* @param bool $defaultBranchOnly Whether to include only the default branch in the forked repository. Defaults to false.
191+
*
192+
* @return array|null The data of the newly forked repository, or null if the fork operation failed.
193+
*/
194+
public function forkRepository(string $owner, string $repo, ?string $organization = null, ?string $name = null, bool $defaultBranchOnly = false): ?array
195+
{
196+
$url = "/repos/$owner/$repo/forks";
197+
198+
// Create the payload data for the API request
199+
$data = [
200+
'organization' => $organization,
201+
'name' => $name,
202+
'default_branch_only' => $defaultBranchOnly,
203+
];
204+
205+
// Send the API request to fork the repository
206+
$response = $this->call(self::METHOD_POST, $url, ["Authorization" => "Bearer $this->accessToken"], $data);
207+
return $response['body'];
208+
}
182209
}

tests/VCS/GitHubTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ public function testDownloadRepositoryZip(): void
4444
$zipContents = $this->github->downloadRepositoryZip("gatsby-ecommerce-theme", "main");
4545

4646
// Save the ZIP archive to a file
47-
file_put_contents('hello-world.zip', $zipContents);
47+
file_put_contents('./desktop/hello-world.zip', $zipContents);
48+
}
49+
50+
public function testForkRepository(): void
51+
{
52+
// Fork a repository into authenticated user's account with custom name
53+
$response = $this->github->forkRepository("appwrite", "demos-for-astro", name: "fork-api-test-clone");
4854
}
4955
}

0 commit comments

Comments
 (0)