Skip to content

Commit

Permalink
Add forkRepository() method
Browse files Browse the repository at this point in the history
  • Loading branch information
vermakhushboo committed Feb 20, 2023
1 parent 5e130c4 commit 68d1605
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/vendor/
/.idea/
.phpunit.result.cache
.env
.env
.DS_Store
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ services:
- ./src:/usr/local/src/src
- ./tests:/usr/local/src/tests
- ./phpunit.xml:/usr/local/src/phpunit.xml
- /Users/khushbooverma/desktop:/usr/local/src/desktop
environment:
- GITHUB_PRIVATE_KEY
- GITHUB_APP_IDENTIFIER
Expand Down
27 changes: 27 additions & 0 deletions src/VCS/Adapter/Git/GitHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,31 @@ public function downloadRepositoryZip(string $repoName, string $ref, string $pat
// Return the contents of the ZIP archive
return $response['body'];
}

/**
* Forks a repository on GitHub.
*
* @param string $owner The owner of the repository to fork.
* @param string $repo The name of the repository to fork.
* @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.
* @param string|null $name The name of the new forked repository. If not provided, the name will be the same as the original repository.
* @param bool $defaultBranchOnly Whether to include only the default branch in the forked repository. Defaults to false.
*
* @return array|null The data of the newly forked repository, or null if the fork operation failed.
*/
public function forkRepository(string $owner, string $repo, ?string $organization = null, ?string $name = null, bool $defaultBranchOnly = false): ?array
{
$url = "/repos/$owner/$repo/forks";

// Create the payload data for the API request
$data = [
'organization' => $organization,
'name' => $name,
'default_branch_only' => $defaultBranchOnly,
];

// Send the API request to fork the repository
$response = $this->call(self::METHOD_POST, $url, ["Authorization" => "Bearer $this->accessToken"], $data);
return $response['body'];
}
}
8 changes: 7 additions & 1 deletion tests/VCS/GitHubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public function testDownloadRepositoryZip(): void
$zipContents = $this->github->downloadRepositoryZip("gatsby-ecommerce-theme", "main");

// Save the ZIP archive to a file
file_put_contents('hello-world.zip', $zipContents);
file_put_contents('./desktop/hello-world.zip', $zipContents);
}

public function testForkRepository(): void
{
// Fork a repository into authenticated user's account with custom name
$response = $this->github->forkRepository("appwrite", "demos-for-astro", name: "fork-api-test-clone");
}
}

0 comments on commit 68d1605

Please sign in to comment.