diff --git a/doc/repos.md b/doc/repos.md index 4fa2fb076d1..3c6526b759b 100644 --- a/doc/repos.md +++ b/doc/repos.md @@ -309,3 +309,15 @@ Returns a list of milestones. ```php $codeOfConduct = $client->api('repo')->codeOfConduct('ornicar', 'php-github-api'); ``` + +### List all topics for a repository + +```php +$topics = $client->api('repo')->topics('ornicar', 'php-github-api'); +``` + +### Replace all topics for a repository + +```php +$currentTopics = $client->api('repo')->replaceTopics('ornicar', 'php-github-api', ['new', 'topics']); +``` diff --git a/lib/Github/Api/Repo.php b/lib/Github/Api/Repo.php index e611599fa72..b00f12ded48 100644 --- a/lib/Github/Api/Repo.php +++ b/lib/Github/Api/Repo.php @@ -621,4 +621,41 @@ public function codeOfConduct($username, $repository) return $this->get('/repos/'.rawurldecode($username).'/'.rawurldecode($repository).'/community/code_of_conduct'); } + + /** + * List all topics for a repository + * + * @link https://developer.github.com/v3/repos/#list-all-topics-for-a-repository + * + * @param string $username + * @param string $repository + * + * @return array + */ + public function topics($username, $repository) + { + //This api is in preview mode, so set the correct accept-header + $this->acceptHeaderValue = 'application/vnd.github.mercy-preview+json'; + + return $this->get('/repos/'.rawurldecode($username).'/'.rawurldecode($repository).'/topics'); + } + + /** + * Replace all topics for a repository + * + * @link https://developer.github.com/v3/repos/#replace-all-topics-for-a-repository + * + * @param string $username + * @param string $repository + * @param array $topics + * + * @return array + */ + public function replaceTopics($username, $repository, array $topics) + { + //This api is in preview mode, so set the correct accept-header + $this->acceptHeaderValue = 'application/vnd.github.mercy-preview+json'; + + return $this->put('/repos/'.rawurldecode($username).'/'.rawurldecode($repository).'/topics', ['names' => $topics]); + } } diff --git a/test/Github/Tests/Api/RepoTest.php b/test/Github/Tests/Api/RepoTest.php index 9ffd9986519..24a4be1fe51 100644 --- a/test/Github/Tests/Api/RepoTest.php +++ b/test/Github/Tests/Api/RepoTest.php @@ -551,6 +551,40 @@ public function shouldGetRepositoryCodeOfConduct() $this->assertEquals($expectedArray, $api->codeOfConduct('KnpLabs', 'php-github-api')); } + /** + * @test + */ + public function shouldGetRepositoryTopics() + { + $expectedArray = ['names' => ['octocat', 'atom', 'electron', 'API']]; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('/repos/KnpLabs/php-github-api/topics') + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->topics('KnpLabs', 'php-github-api')); + } + + /** + * @test + */ + public function shouldReplaceRepositoryTopics() + { + $expectedArray = array('id' => 6122723754, 'type' => 'ForkEvent'); + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('put') + ->with('/repos/KnpLabs/php-github-api/topics', array( + 'names' => ['octocat', 'atom', 'electron', 'API'], + )) + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->replaceTopics('KnpLabs', 'php-github-api', ['octocat', 'atom', 'electron', 'API'])); + } + /** * @return string */