From 7a7390dee24ee81474876a86819e2b8a6fe5f772 Mon Sep 17 00:00:00 2001 From: Jeroen Thora Date: Sun, 10 Dec 2017 11:43:52 +0100 Subject: [PATCH] Missing code of conduct api endpoints --- doc/README.md | 1 + doc/miscellaneous/codeofconduct.md | 14 +++++ doc/repos.md | 6 +++ .../Api/Miscellaneous/CodeOfConduct.php | 44 +++++++++++++++ lib/Github/Api/Repo.php | 20 +++++++ lib/Github/Client.php | 4 ++ .../Api/Miscellaneous/CodeOfConductTest.php | 54 +++++++++++++++++++ test/Github/Tests/Api/RepoTest.php | 16 ++++++ 8 files changed, 159 insertions(+) create mode 100644 doc/miscellaneous/codeofconduct.md create mode 100644 lib/Github/Api/Miscellaneous/CodeOfConduct.php create mode 100644 test/Github/Tests/Api/Miscellaneous/CodeOfConductTest.php diff --git a/doc/README.md b/doc/README.md index 00faee5a846..7ba3696b7b4 100644 --- a/doc/README.md +++ b/doc/README.md @@ -30,6 +30,7 @@ v3 APIs: * [Timeline](issue/timeline.md) * [Meta](meta.md) * Miscellaneous + * [Code of conduct](miscellaneous/codeofconduct.md) * [Emojis](miscellaneous/emojis.md) * [Gitignore](miscellaneous/gitignore.md) * [Markdown](miscellaneous/markdown.md) diff --git a/doc/miscellaneous/codeofconduct.md b/doc/miscellaneous/codeofconduct.md new file mode 100644 index 00000000000..c47b3c49774 --- /dev/null +++ b/doc/miscellaneous/codeofconduct.md @@ -0,0 +1,14 @@ +## CodeOfConduct API +[Back to the navigation](../README.md) + +### Lists all code of conducts. + +```php +$codeOfConducts = $client->api('codeOfConduct')->all(); +``` + +### Get a code of conduct. + +```php +$codeOfConducts = $client->api('codeOfConduct')->show('contributor_covenant'); +``` diff --git a/doc/repos.md b/doc/repos.md index 1eb24a278b7..4fa2fb076d1 100644 --- a/doc/repos.md +++ b/doc/repos.md @@ -303,3 +303,9 @@ $milestones = $client->api('repo')->milestones('ornicar', 'php-github-api'); ``` Returns a list of milestones. + +### Get the contents of a repository's code of conduct + +```php +$codeOfConduct = $client->api('repo')->codeOfConduct('ornicar', 'php-github-api'); +``` diff --git a/lib/Github/Api/Miscellaneous/CodeOfConduct.php b/lib/Github/Api/Miscellaneous/CodeOfConduct.php new file mode 100644 index 00000000000..7d3c69ff469 --- /dev/null +++ b/lib/Github/Api/Miscellaneous/CodeOfConduct.php @@ -0,0 +1,44 @@ +acceptHeaderValue = 'application/vnd.github.scarlet-witch-preview+json'; + + return $this; + } + + /** + * List all codes of conduct. + * + * @link https://developer.github.com/v3/codes_of_conduct/#list-all-codes-of-conduct + * + * @return array + */ + public function all() + { + return $this->get('/codes_of_conduct'); + } + + /** + * Get an individual code of conduct + * + * @link https://developer.github.com/v3/codes_of_conduct/#get-an-individual-code-of-conduct + * + * @param string $key + * + * @return array + */ + public function show($key) + { + return $this->get('/codes_of_conduct/' . rawurlencode($key)); + } +} diff --git a/lib/Github/Api/Repo.php b/lib/Github/Api/Repo.php index 9598d6d04ec..684b28cdddf 100644 --- a/lib/Github/Api/Repo.php +++ b/lib/Github/Api/Repo.php @@ -28,6 +28,8 @@ */ class Repo extends AbstractApi { + use AcceptHeaderTrait; + /** * Search repositories by keyword. * @@ -599,4 +601,22 @@ public function events($username, $repository, $page = 1) { return $this->get('/repos/'.rawurldecode($username).'/'.rawurldecode($repository).'/events', ['page' => $page]); } + + /** + * Get the contents of a repository's code of conduct + * + * @link https://developer.github.com/v3/codes_of_conduct/#get-the-contents-of-a-repositorys-code-of-conduct + * + * @param string $username + * @param string $repository + * + * @return array + */ + public function codeOfConduct($username, $repository) + { + //This api is in preview mode, so set the correct accept-header + $this->acceptHeaderValue = 'application/vnd.github.scarlet-witch-preview+json'; + + return $this->get('/repos/'.rawurldecode($username).'/'.rawurldecode($repository).'/community/code_of_conduct'); + } } diff --git a/lib/Github/Client.php b/lib/Github/Client.php index d93c703e936..ea22b512a6c 100644 --- a/lib/Github/Client.php +++ b/lib/Github/Client.php @@ -23,6 +23,7 @@ * @method Api\CurrentUser me() * @method Api\Enterprise ent() * @method Api\Enterprise enterprise() + * @method Api\Miscellaneous\CodeOfConduct codeOfConduct() * @method Api\Miscellaneous\Emojis emojis() * @method Api\GitData git() * @method Api\GitData gitData() @@ -169,6 +170,9 @@ public function api($name) case 'currentUser': $api = new Api\CurrentUser($this); break; + case 'codeOfConduct': + $api = new Api\Miscellaneous\CodeOfConduct($this); + break; case 'deployment': case 'deployments': diff --git a/test/Github/Tests/Api/Miscellaneous/CodeOfConductTest.php b/test/Github/Tests/Api/Miscellaneous/CodeOfConductTest.php new file mode 100644 index 00000000000..0e07dea5752 --- /dev/null +++ b/test/Github/Tests/Api/Miscellaneous/CodeOfConductTest.php @@ -0,0 +1,54 @@ + 'CoC1'], + ['name' => 'CoC2'], + ]; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('/codes_of_conduct') + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->all()); + } + + /** + * @test + */ + public function shouldGetSingleCodeOfConducts() + { + $expectedArray = [ + 'name' => 'CoC', + ]; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('/codes_of_conduct/contributor_covenant') + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->show('contributor_covenant')); + } + + /** + * @return string + */ + protected function getApiClass() + { + return CodeOfConduct::class; + } +} diff --git a/test/Github/Tests/Api/RepoTest.php b/test/Github/Tests/Api/RepoTest.php index d84f841009a..9ffd9986519 100644 --- a/test/Github/Tests/Api/RepoTest.php +++ b/test/Github/Tests/Api/RepoTest.php @@ -535,6 +535,22 @@ public function shouldGetRepositoryEvents() $this->assertEquals($expectedArray, $api->events('KnpLabs', 'php-github-api', 3)); } + /** + * @test + */ + public function shouldGetRepositoryCodeOfConduct() + { + $expectedArray = array('name' => 'Contributor Covenant', 'url' => 'http://...'); + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('/repos/KnpLabs/php-github-api/community/code_of_conduct') + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->codeOfConduct('KnpLabs', 'php-github-api')); + } + /** * @return string */