Skip to content

Commit f1cb6b6

Browse files
authored
feature #1084 Add sync a fork branch with the upstream repository (DAGpro)
This PR was squashed before being merged into the 3.9.x-dev branch. Discussion ---------- | Q | A | ------------- | --- | New feature? | ✔️ | Fixed issues | #1083 Commits ------- 184826b Add sync a fork branch with the upstream repository c6b2d39 Mark the default branch 06e6c9b Add tests and documentation for the mergeUpstream method 0de3d78 Make parameter required d5d9aff Add typings to the mergeUpstream method
1 parent cb4b4eb commit f1cb6b6

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

doc/repos.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,15 @@ $repository = $client->api('repo')->forks()->create('ornicar', 'php-github-api')
223223

224224
Creates a fork of the 'php-github-api' owned by 'ornicar' and returns the newly created repository.
225225

226+
### Merge upstream repository
227+
228+
> Requires [authentication](security.md).
229+
230+
```php
231+
$repository = $client->api('repo')->mergeUpstream('ornicar', 'php-github-api', 'branchName');
232+
```
233+
Merge upstream a branch of a forked repository to keep it up-to-date with the upstream repository.
234+
226235
### Get the tags of a repository
227236

228237
```php

lib/Github/Api/Repo.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,21 @@ public function branches($username, $repository, $branch = null, array $paramete
535535
return $this->get($url, $parameters);
536536
}
537537

538+
/**
539+
* Sync a fork branch with the upstream repository.
540+
*
541+
* @link https://docs.github.com/en/rest/branches/branches#sync-a-fork-branch-with-the-upstream-repository
542+
*
543+
* @return array|string
544+
*/
545+
public function mergeUpstream(string $username, string $repository, string $branchName)
546+
{
547+
return $this->post(
548+
'/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/merge-upstream',
549+
['branch' => $branchName]
550+
);
551+
}
552+
538553
/**
539554
* Manage the protection of a repository branch.
540555
*

test/Github/Tests/Api/RepoTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,26 @@ public function shouldGetRepositoryBranch()
240240
$this->assertEquals($expectedArray, $api->branches('KnpLabs', 'php-github-api', 'master'));
241241
}
242242

243+
/**
244+
* @test
245+
*/
246+
public function shouldMergeUpstreamRepository()
247+
{
248+
$expectedArray = [
249+
'message' => 'Successfully fetched and fast-forwarded from upstream upstreamRepo:main',
250+
'merge_type' => 'fast-forward',
251+
'merge_branch' => 'upstreamRepo:main',
252+
];
253+
254+
$api = $this->getApiMock();
255+
$api->expects($this->once())
256+
->method('post')
257+
->with('/repos/KnpLabs/php-github-api/merge-upstream', ['branch' => 'main'])
258+
->will($this->returnValue($expectedArray));
259+
260+
$this->assertEquals($expectedArray, $api->mergeUpstream('KnpLabs', 'php-github-api', 'main'));
261+
}
262+
243263
/**
244264
* @test
245265
*/

0 commit comments

Comments
 (0)