From 184826b93898f9fca03397891a35d46b92262635 Mon Sep 17 00:00:00 2001 From: Mahmud Date: Sun, 18 Sep 2022 19:13:06 +0300 Subject: [PATCH 1/5] Add sync a fork branch with the upstream repository --- lib/Github/Api/Repo.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/Github/Api/Repo.php b/lib/Github/Api/Repo.php index f5762279e7a..92737e3c216 100644 --- a/lib/Github/Api/Repo.php +++ b/lib/Github/Api/Repo.php @@ -535,6 +535,25 @@ 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 + * + * @param string $username + * @param string $repository + * @param string|null $branchName + * + * @return array|string + */ + public function mergeUpstream($username, $repository, $branchName = null) + { + return $this->post( + '/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/merge-upstream', + ['branch' => $branchName ?? 'main'] + ); + } + /** * Manage the protection of a repository branch. * From c6b2d3966202586a4688192995e7b86ae2b445af Mon Sep 17 00:00:00 2001 From: Mahmud Date: Sun, 23 Oct 2022 22:01:11 +0300 Subject: [PATCH 2/5] Mark the default branch --- lib/Github/Api/Repo.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Github/Api/Repo.php b/lib/Github/Api/Repo.php index 92737e3c216..ce713972d6d 100644 --- a/lib/Github/Api/Repo.php +++ b/lib/Github/Api/Repo.php @@ -546,11 +546,11 @@ public function branches($username, $repository, $branch = null, array $paramete * * @return array|string */ - public function mergeUpstream($username, $repository, $branchName = null) + public function mergeUpstream($username, $repository, $branchName = 'main') { return $this->post( '/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/merge-upstream', - ['branch' => $branchName ?? 'main'] + ['branch' => $branchName] ); } From 06e6c9bab0d154ec232c833b57fa87ef4c0acffa Mon Sep 17 00:00:00 2001 From: Mahmud Date: Sun, 23 Oct 2022 22:09:14 +0300 Subject: [PATCH 3/5] Add tests and documentation for the mergeUpstream method --- doc/repos.md | 9 +++++++++ test/Github/Tests/Api/RepoTest.php | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/doc/repos.md b/doc/repos.md index ab412dc77c2..9ed70d5fb54 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/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 */ From 0de3d781a404198dde89473a4a64a74ac656d7e2 Mon Sep 17 00:00:00 2001 From: Mahmud Date: Mon, 24 Oct 2022 00:38:18 +0300 Subject: [PATCH 4/5] Make parameter required --- lib/Github/Api/Repo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Github/Api/Repo.php b/lib/Github/Api/Repo.php index ce713972d6d..765c840e154 100644 --- a/lib/Github/Api/Repo.php +++ b/lib/Github/Api/Repo.php @@ -546,7 +546,7 @@ public function branches($username, $repository, $branch = null, array $paramete * * @return array|string */ - public function mergeUpstream($username, $repository, $branchName = 'main') + public function mergeUpstream($username, $repository, $branchName) { return $this->post( '/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/merge-upstream', From d5d9aff69d85343b05d36b0d608dd1dccf7d8aaf Mon Sep 17 00:00:00 2001 From: Mahmud Date: Mon, 24 Oct 2022 15:20:40 +0300 Subject: [PATCH 5/5] Add typings to the mergeUpstream method --- lib/Github/Api/Repo.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/Github/Api/Repo.php b/lib/Github/Api/Repo.php index 765c840e154..7536d7007dd 100644 --- a/lib/Github/Api/Repo.php +++ b/lib/Github/Api/Repo.php @@ -540,13 +540,9 @@ public function branches($username, $repository, $branch = null, array $paramete * * @link https://docs.github.com/en/rest/branches/branches#sync-a-fork-branch-with-the-upstream-repository * - * @param string $username - * @param string $repository - * @param string|null $branchName - * * @return array|string */ - public function mergeUpstream($username, $repository, $branchName) + public function mergeUpstream(string $username, string $repository, string $branchName) { return $this->post( '/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/merge-upstream',