From f1cb6b620426184f3351dcbcf3b1c32b83d53bca Mon Sep 17 00:00:00 2001 From: Mahmud Date: Mon, 24 Oct 2022 15:40:34 +0300 Subject: [PATCH] feature #1084 Add sync a fork branch with the upstream repository (DAGpro) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR was squashed before being merged into the 3.9.x-dev branch. Discussion ---------- | Q | A | ------------- | --- | New feature? | ✔️ | Fixed issues | #1083 Commits ------- 184826b93898f9fca03397891a35d46b92262635 Add sync a fork branch with the upstream repository c6b2d3966202586a4688192995e7b86ae2b445af Mark the default branch 06e6c9bab0d154ec232c833b57fa87ef4c0acffa Add tests and documentation for the mergeUpstream method 0de3d781a404198dde89473a4a64a74ac656d7e2 Make parameter required d5d9aff69d85343b05d36b0d608dd1dccf7d8aaf Add typings to the mergeUpstream method --- doc/repos.md | 9 +++++++++ lib/Github/Api/Repo.php | 15 +++++++++++++++ test/Github/Tests/Api/RepoTest.php | 20 ++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/doc/repos.md b/doc/repos.md index a0f822ce446..7e091e005fe 100644 --- a/doc/repos.md +++ b/doc/repos.md @@ -223,6 +223,15 @@ $repository = $client->api('repo')->forks()->create('ornicar', 'php-github-api') Creates a fork of the 'php-github-api' owned by 'ornicar' and returns the newly created repository. +### Merge upstream repository + +> Requires [authentication](security.md). + +```php +$repository = $client->api('repo')->mergeUpstream('ornicar', 'php-github-api', 'branchName'); +``` +Merge upstream a branch of a forked repository to keep it up-to-date with the upstream repository. + ### Get the tags of a repository ```php diff --git a/lib/Github/Api/Repo.php b/lib/Github/Api/Repo.php index f5762279e7a..7536d7007dd 100644 --- a/lib/Github/Api/Repo.php +++ b/lib/Github/Api/Repo.php @@ -535,6 +535,21 @@ public function branches($username, $repository, $branch = null, array $paramete return $this->get($url, $parameters); } + /** + * Sync a fork branch with the upstream repository. + * + * @link https://docs.github.com/en/rest/branches/branches#sync-a-fork-branch-with-the-upstream-repository + * + * @return array|string + */ + public function mergeUpstream(string $username, string $repository, string $branchName) + { + return $this->post( + '/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/merge-upstream', + ['branch' => $branchName] + ); + } + /** * Manage the protection of a repository branch. * diff --git a/test/Github/Tests/Api/RepoTest.php b/test/Github/Tests/Api/RepoTest.php index 9cfa7f84a88..b4603bcddf9 100644 --- a/test/Github/Tests/Api/RepoTest.php +++ b/test/Github/Tests/Api/RepoTest.php @@ -240,6 +240,26 @@ public function shouldGetRepositoryBranch() $this->assertEquals($expectedArray, $api->branches('KnpLabs', 'php-github-api', 'master')); } + /** + * @test + */ + public function shouldMergeUpstreamRepository() + { + $expectedArray = [ + 'message' => 'Successfully fetched and fast-forwarded from upstream upstreamRepo:main', + 'merge_type' => 'fast-forward', + 'merge_branch' => 'upstreamRepo:main', + ]; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('post') + ->with('/repos/KnpLabs/php-github-api/merge-upstream', ['branch' => 'main']) + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->mergeUpstream('KnpLabs', 'php-github-api', 'main')); + } + /** * @test */