Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sync a fork branch with the upstream repository #1084

Merged
merged 5 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions doc/repos.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions lib/Github/Api/Repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
20 changes: 20 additions & 0 deletions test/Github/Tests/Api/RepoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down