From 826fb3fe82cdc56c0af0fe824cd7faa60dcbb1a6 Mon Sep 17 00:00:00 2001 From: Edoardo Rivello Date: Wed, 27 Jun 2012 16:37:46 +0200 Subject: [PATCH 01/16] Gists API integration: get list by username and get single gist --- lib/Github/Api/Gist.php | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 lib/Github/Api/Gist.php diff --git a/lib/Github/Api/Gist.php b/lib/Github/Api/Gist.php new file mode 100644 index 00000000000..388233e6994 --- /dev/null +++ b/lib/Github/Api/Gist.php @@ -0,0 +1,37 @@ + + * @license MIT License + */ +class Gist extends Api +{ + /** + * List gists by username + * http://developer.github.com/v3/gists/ + * + * @param string $username the username + * @return array list of gist found + */ + public function getList($username) + { + return $this->get('users/'.urlencode($username).'/gists'); + } + + /** + * Show a specific gist + * http://developer.github.com/v3/gists/ + * + * @param string $id the gist id + * @return array data from gist + */ + public function getGist($id) + { + return $this->get('/gists/'.urlencode($id)); + } +} \ No newline at end of file From c53e1ba8fdef9ddcad463d9864a7b7c4ff5ecb1b Mon Sep 17 00:00:00 2001 From: Edoardo Rivello Date: Wed, 27 Jun 2012 16:40:02 +0200 Subject: [PATCH 02/16] added gist api to client --- lib/Github/Client.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/Github/Client.php b/lib/Github/Client.php index adf4aa4caad..3d8e58fb23d 100644 --- a/lib/Github/Client.php +++ b/lib/Github/Client.php @@ -224,6 +224,20 @@ public function getCommitApi() return $this->apis['commit']; } + + /** + * Get the gist API + * + * @return Api\Gist the gist API + */ + public function getGistApi() + { + if (!isset($this->apis['gist'])) { + $this->apis['gist'] = new Api\Gist($this); + } + + return $this->apis['gist']; + } /** * Get the repo API From 8de6339eecc7e5eb84fe2ce5ceb01e86ecfa5c74 Mon Sep 17 00:00:00 2001 From: Edoardo Rivello Date: Wed, 27 Jun 2012 17:21:52 +0200 Subject: [PATCH 03/16] gist create api --- lib/Github/Api/Gist.php | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/Github/Api/Gist.php b/lib/Github/Api/Gist.php index 388233e6994..24b92bab7af 100644 --- a/lib/Github/Api/Gist.php +++ b/lib/Github/Api/Gist.php @@ -13,7 +13,7 @@ class Gist extends Api { /** * List gists by username - * http://developer.github.com/v3/gists/ + * @link http://developer.github.com/v3/gists/ * * @param string $username the username * @return array list of gist found @@ -25,7 +25,7 @@ public function getList($username) /** * Show a specific gist - * http://developer.github.com/v3/gists/ + * @link http://developer.github.com/v3/gists/ * * @param string $id the gist id * @return array data from gist @@ -33,5 +33,34 @@ public function getList($username) public function getGist($id) { return $this->get('/gists/'.urlencode($id)); + } + + /** + * Create a new gist. + * The gist is assigned to the authenticated user. Requires authentication. + * @link http://developer.github.com/v3/issues/ + * + * @param string $description the new gist description + * @param bool $public the new gist visibility + * @param string $filename the new gist filename + * @param string $content the new gist file contents + * @return array information about the gist + */ + public function create($public, $filename, $content, $description = null) + { + $input = array( + 'public' => $public, + 'files' => array( + $filename => array( + 'content' => $content + ) + ) + ); + + if ($description) { + $input['description'] = $description; + } + + return $this->post('gists', $input); } } \ No newline at end of file From 5245ad557aaf5d07a37a1837b8355e3133bf0193 Mon Sep 17 00:00:00 2001 From: Edoardo Rivello Date: Wed, 27 Jun 2012 18:39:56 +0200 Subject: [PATCH 04/16] create gist api refactor --- lib/Github/Api/Gist.php | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/lib/Github/Api/Gist.php b/lib/Github/Api/Gist.php index 24b92bab7af..a9c135ff7f6 100644 --- a/lib/Github/Api/Gist.php +++ b/lib/Github/Api/Gist.php @@ -40,15 +40,16 @@ public function getGist($id) * The gist is assigned to the authenticated user. Requires authentication. * @link http://developer.github.com/v3/issues/ * - * @param string $description the new gist description - * @param bool $public the new gist visibility - * @param string $filename the new gist filename - * @param string $content the new gist file contents + * @param string $description gist description + * @param bool $public 1 for public, 0 for private + * @param string $filename gist filename + * @param string $content gist file contents * @return array information about the gist */ - public function create($public, $filename, $content, $description = null) + public function create($filename, $content, $description = '', $public = false) { $input = array( + 'description' => $description, 'public' => $public, 'files' => array( $filename => array( @@ -57,10 +58,6 @@ public function create($public, $filename, $content, $description = null) ) ); - if ($description) { - $input['description'] = $description; - } - return $this->post('gists', $input); } } \ No newline at end of file From 4baecb763d8f6014da1be9eb1acac4ed6af7e2bb Mon Sep 17 00:00:00 2001 From: Edoardo Rivello Date: Wed, 27 Jun 2012 18:48:56 +0200 Subject: [PATCH 05/16] delete gist api --- lib/Github/Api/Gist.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/Github/Api/Gist.php b/lib/Github/Api/Gist.php index a9c135ff7f6..482682ccfd8 100644 --- a/lib/Github/Api/Gist.php +++ b/lib/Github/Api/Gist.php @@ -5,7 +5,7 @@ /** * Creating, editing, deleting and listing gists * - * @link http://develop.github.com/p/issues.html + * @link http://developer.github.com/v3/gists/ * @author Edoardo Rivello * @license MIT License */ @@ -60,4 +60,17 @@ public function create($filename, $content, $description = '', $public = false) return $this->post('gists', $input); } + + /** + * Remove a gist by id + * Requires authentication. + * @link http://developer.github.com/v3/issues/ + * + * @param string $id the gist id + * @return + */ + public function delete($id) + { + return $this->delete('gists/'.urlencode($id)); + } } \ No newline at end of file From 5acc8895f55a8f7cbd75bb20c2e60018178dec23 Mon Sep 17 00:00:00 2001 From: Edoardo Rivello Date: Thu, 28 Jun 2012 10:52:53 +0200 Subject: [PATCH 06/16] gist api functional test for getList method --- test/Github/Tests/Functional/GistTest.php | 28 +++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 test/Github/Tests/Functional/GistTest.php diff --git a/test/Github/Tests/Functional/GistTest.php b/test/Github/Tests/Functional/GistTest.php new file mode 100644 index 00000000000..2d248be4296 --- /dev/null +++ b/test/Github/Tests/Functional/GistTest.php @@ -0,0 +1,28 @@ +getGistApi()->getList($username); + $gist = array_pop($gists); + + $this->assertArrayHasKey('url', $gist); + $this->assertArrayHasKey('files', $gist); + $this->assertArrayHasKey('comments', $gist); + $this->assertArrayHasKey('created_at', $gist); + $this->assertArrayHasKey('updated_at', $gist); + $this->assertArrayHasKey('user', $gist); + $this->assertEquals('KnpLabs', $gist['user']['login']); + } +} \ No newline at end of file From 690a969815b7e5a52e0e67016ae5f7acfd808ab5 Mon Sep 17 00:00:00 2001 From: Edoardo Rivello Date: Thu, 28 Jun 2012 11:40:37 +0200 Subject: [PATCH 07/16] gist api refactor: renamed delete method in remove method --- lib/Github/Api/Gist.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/Github/Api/Gist.php b/lib/Github/Api/Gist.php index 482682ccfd8..bab45db3f9d 100644 --- a/lib/Github/Api/Gist.php +++ b/lib/Github/Api/Gist.php @@ -37,7 +37,7 @@ public function getGist($id) /** * Create a new gist. - * The gist is assigned to the authenticated user. Requires authentication. + * The gist is assigned to the authenticated user. * @link http://developer.github.com/v3/issues/ * * @param string $description gist description @@ -66,10 +66,10 @@ public function create($filename, $content, $description = '', $public = false) * Requires authentication. * @link http://developer.github.com/v3/issues/ * - * @param string $id the gist id - * @return + * @param int $id the gist id + * @return null */ - public function delete($id) + public function remove($id) { return $this->delete('gists/'.urlencode($id)); } From 1d3d647c87d49cbb4c73d4d91b3e1c30e34f9a3e Mon Sep 17 00:00:00 2001 From: Edoardo Rivello Date: Thu, 28 Jun 2012 11:41:31 +0200 Subject: [PATCH 08/16] unit tests for gist api --- test/Github/Tests/Api/GistTest.php | 54 ++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 test/Github/Tests/Api/GistTest.php diff --git a/test/Github/Tests/Api/GistTest.php b/test/Github/Tests/Api/GistTest.php new file mode 100644 index 00000000000..514aee686b1 --- /dev/null +++ b/test/Github/Tests/Api/GistTest.php @@ -0,0 +1,54 @@ +getApiMock(); + + $input = array( + 'description' => '', + 'public' => false, + 'files' => array( + 'filename.txt' => array( + 'content' => 'content' + ) + ) + ); + + $filename = 'filename.txt'; + $content = 'content'; + + $api->expects($this->once()) + ->method('post') + ->with('gists', $input); + + $gist = $api->create($filename, $content); + } + + /** + * @test + */ + public function shouldDeleteGist() + { + $api = $this->getApiMock(); + + $api->expects($this->once()) + ->method('delete') + ->with('gists/5'); + + $api->remove(5); + } + + protected function getApiClass() + { + return 'Github\Api\Gist'; + } +} \ No newline at end of file From 58bc417b5c2c48e88ccabe90d3f1278686ce22eb Mon Sep 17 00:00:00 2001 From: Edoardo Rivello Date: Thu, 28 Jun 2012 11:53:42 +0200 Subject: [PATCH 09/16] functional test for gist api: get by id --- test/Github/Tests/Functional/GistTest.php | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/test/Github/Tests/Functional/GistTest.php b/test/Github/Tests/Functional/GistTest.php index 2d248be4296..0261bdb3a6e 100644 --- a/test/Github/Tests/Functional/GistTest.php +++ b/test/Github/Tests/Functional/GistTest.php @@ -9,7 +9,7 @@ class GistTest extends \PHPUnit_Framework_TestCase /** * @test */ - public function shouldRetrieveGistByUser() + public function shouldRetrieveGistsListByUser() { $username = 'KnpLabs'; @@ -25,4 +25,24 @@ public function shouldRetrieveGistByUser() $this->assertArrayHasKey('user', $gist); $this->assertEquals('KnpLabs', $gist['user']['login']); } + + /** + * @test + */ + public function shouldRetrieveGistById() + { + $id = 1; + + $github = new Client(); + $gist = $github->getGistApi()->getGist($id); + + $this->assertArrayHasKey('url', $gist); + $this->assertArrayHasKey('files', $gist); + $this->assertArrayHasKey('comments', $gist); + $this->assertArrayHasKey('created_at', $gist); + $this->assertArrayHasKey('updated_at', $gist); + $this->assertArrayHasKey('user', $gist); + $this->assertArrayHasKey('gistfile1.txt', $gist['files']); + $this->assertEquals('schacon', $gist['user']['login']); + } } \ No newline at end of file From b8c15026934cbe64d212e7aba9e82a30d0fe43a9 Mon Sep 17 00:00:00 2001 From: Edoardo Rivello Date: Thu, 28 Jun 2012 12:06:39 +0200 Subject: [PATCH 10/16] comment fix for gist api --- lib/Github/Api/Gist.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Github/Api/Gist.php b/lib/Github/Api/Gist.php index bab45db3f9d..8dd1bd0afbf 100644 --- a/lib/Github/Api/Gist.php +++ b/lib/Github/Api/Gist.php @@ -37,7 +37,7 @@ public function getGist($id) /** * Create a new gist. - * The gist is assigned to the authenticated user. + * The gist is assigned to null user. * @link http://developer.github.com/v3/issues/ * * @param string $description gist description From 36701a5c5f92da13e7c5a3041e031e86ab37a650 Mon Sep 17 00:00:00 2001 From: Edoardo Rivello Date: Sun, 8 Jul 2012 11:35:51 +0200 Subject: [PATCH 11/16] fix gist api documentation --- lib/Github/Api/Gist.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/Github/Api/Gist.php b/lib/Github/Api/Gist.php index 8dd1bd0afbf..c7c56eab43b 100644 --- a/lib/Github/Api/Gist.php +++ b/lib/Github/Api/Gist.php @@ -37,7 +37,6 @@ public function getGist($id) /** * Create a new gist. - * The gist is assigned to null user. * @link http://developer.github.com/v3/issues/ * * @param string $description gist description @@ -63,7 +62,6 @@ public function create($filename, $content, $description = '', $public = false) /** * Remove a gist by id - * Requires authentication. * @link http://developer.github.com/v3/issues/ * * @param int $id the gist id From 2adb0450da062dc5322fa2aa205e2af8c71f8836 Mon Sep 17 00:00:00 2001 From: Edoardo Rivello Date: Sun, 8 Jul 2012 12:37:38 +0200 Subject: [PATCH 12/16] added delete method to gist api --- lib/Github/Api/Gist.php | 19 ++++++++++++++++--- test/Github/Tests/Api/GistTest.php | 29 ++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/lib/Github/Api/Gist.php b/lib/Github/Api/Gist.php index c7c56eab43b..d4c991d7bdd 100644 --- a/lib/Github/Api/Gist.php +++ b/lib/Github/Api/Gist.php @@ -37,13 +37,13 @@ public function getGist($id) /** * Create a new gist. - * @link http://developer.github.com/v3/issues/ + * @link http://developer.github.com/v3/gists/ * * @param string $description gist description * @param bool $public 1 for public, 0 for private * @param string $filename gist filename * @param string $content gist file contents - * @return array information about the gist + * @return array returns gist data */ public function create($filename, $content, $description = '', $public = false) { @@ -58,11 +58,24 @@ public function create($filename, $content, $description = '', $public = false) ); return $this->post('gists', $input); + } + + /** + * Edit a gist + * @link http://developer.github.com/v3/gists/ + * + * @param string $$id the gist id + * @param array $values the key => value pairs to post + * @return array informations about the gist + */ + public function update($id, $values) + { + return $this->patch('gists/'.urlencode($id), $values); } /** * Remove a gist by id - * @link http://developer.github.com/v3/issues/ + * @link http://developer.github.com/v3/gists/ * * @param int $id the gist id * @return null diff --git a/test/Github/Tests/Api/GistTest.php b/test/Github/Tests/Api/GistTest.php index 514aee686b1..9be2657256c 100644 --- a/test/Github/Tests/Api/GistTest.php +++ b/test/Github/Tests/Api/GistTest.php @@ -9,7 +9,7 @@ class GistTest extends ApiTestCase /** * @test */ - public function shouldCreateAnonymousGist() + public function shouldCreateGist() { $api = $this->getApiMock(); @@ -33,6 +33,33 @@ public function shouldCreateAnonymousGist() $gist = $api->create($filename, $content); } + /** + * @test + */ + public function shouldUpdateGist() + { + $api = $this->getApiMock(); + + $input = array( + 'description' => '', + 'files' => array( + 'filename.txt' => array( + 'filename' => 'new_name.txt', + 'content' => 'content' + ), + 'filename_new.txt' => array( + 'content' => 'content new' + ) + ) + ); + + $api->expects($this->once()) + ->method('patch') + ->with('gists/5', $input); + + $gist = $api->update(5, $input); + } + /** * @test */ From 2dc6c8713f1b2b50ab4f168c7df9f9f37109694c Mon Sep 17 00:00:00 2001 From: Edoardo Rivello Date: Sun, 8 Jul 2012 12:38:08 +0200 Subject: [PATCH 13/16] readme update --- README.markdown | 53 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/README.markdown b/README.markdown index 9adf5050173..9dbd1ac0a66 100755 --- a/README.markdown +++ b/README.markdown @@ -50,7 +50,7 @@ From this object, you can access to all GitHub apis, listed below. ## Navigation -[Users][] | [Issues][] | [Commits][] | [Objects][] | [Repos][] | [Pull Requests][] | [Request any Route][] | [Authentication & Security][] | [Customize php-github-api][] | [Run Test Suite][] +[Users][] | [Issues][] | [Commits][] | [Objects][] | [Repos][] | [Pull Requests][] | [Gists][] | [Request any Route][] | [Authentication & Security][] | [Customize php-github-api][] | [Run Test Suite][] ## Users @@ -556,8 +556,6 @@ To include non GitHub users, add a third parameter to true: ``` - - ## Pull Requests Go back to the Navigation @@ -629,6 +627,55 @@ Requires authentication. The issue ID is provided instead of title and body. This returns the details of the pull request. + +## Gists +Go back to the Navigation + +Creating, editing, deleting and listing gists. Wraps [GitHub Gists API](http://developer.github.com/v3/gists/). + +#### List gists of a specific user + +```php + $gists = $github->getGistApi()->getList( 'ornicar' ); +``` + +Returns a list of user’s gists. + +#### Get a single gist + +```php + $gist = $github->getGistApi()->getGist( 1 ); +``` + +Returns a single gist. + +#### Create a gist + +```php + $gist = $github->getGistApi()->create( 'filename.php', 'file contents', 'This is an optional description of my gist', true ); +``` + +Creates and returns a public gist. + +#### Update a gist + +```php + $description = 'This is a new description of my gist'; + $files = array( 'new_file.txt' => array( 'content' => 'a new file' )); + $gist = $github->getGistApi()->update( 1234, array( 'description' => $description, 'files' => $files )); +``` + +Updates and returns my gist with id 1234. + +#### Delete a gist + +```php + $gist = $github->getGistApi()->remove( 1234 ); +``` + +Delete my gist with id 1234. + + ## Request any Route Go back to the Navigation From a6171776062735e69f64bc50788d5842e228ec1d Mon Sep 17 00:00:00 2001 From: Edoardo Rivello Date: Sun, 8 Jul 2012 12:39:44 +0200 Subject: [PATCH 14/16] readme update --- README.markdown | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index 9dbd1ac0a66..587b442376d 100755 --- a/README.markdown +++ b/README.markdown @@ -627,7 +627,7 @@ Requires authentication. The issue ID is provided instead of title and body. This returns the details of the pull request. - + ## Gists Go back to the Navigation @@ -817,6 +817,7 @@ Thanks to GitHub for the high quality API and documentation. [Commits]: #commits [Objects]: #objects [Repos]: #repos +[Gists]: #gists [Pull Requests]: #pull_requests [Request any Route]: #request_any_route [Authentication & Security]: #authentication_and_security From 48126211c085e62b139474fea73cb878a02ff9c6 Mon Sep 17 00:00:00 2001 From: Edoardo Rivello Date: Mon, 9 Jul 2012 10:57:22 +0200 Subject: [PATCH 15/16] pull request fixes --- lib/Github/Api/Gist.php | 18 +++++++++--------- test/Github/Tests/Api/GistTest.php | 2 +- test/Github/Tests/Functional/GistTest.php | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/Github/Api/Gist.php b/lib/Github/Api/Gist.php index d4c991d7bdd..2b26c3bb1a6 100644 --- a/lib/Github/Api/Gist.php +++ b/lib/Github/Api/Gist.php @@ -32,20 +32,20 @@ public function getList($username) */ public function getGist($id) { - return $this->get('/gists/'.urlencode($id)); + return $this->get('gists/'.urlencode($id)); } /** * Create a new gist. * @link http://developer.github.com/v3/gists/ * - * @param string $description gist description - * @param bool $public 1 for public, 0 for private * @param string $filename gist filename * @param string $content gist file contents + * @param bool $public 1 for public, 0 for private + * @param string $description gist description * @return array returns gist data */ - public function create($filename, $content, $description = '', $public = false) + public function create($filename, $content, $public = false, $description = '') { $input = array( 'description' => $description, @@ -64,9 +64,9 @@ public function create($filename, $content, $description = '', $public = false) * Edit a gist * @link http://developer.github.com/v3/gists/ * - * @param string $$id the gist id - * @param array $values the key => value pairs to post - * @return array informations about the gist + * @param string $id the gist id + * @param array $values the key => value pairs to post + * @return array informations about the gist */ public function update($id, $values) { @@ -78,10 +78,10 @@ public function update($id, $values) * @link http://developer.github.com/v3/gists/ * * @param int $id the gist id - * @return null + * @return Response */ public function remove($id) { return $this->delete('gists/'.urlencode($id)); } -} \ No newline at end of file +} diff --git a/test/Github/Tests/Api/GistTest.php b/test/Github/Tests/Api/GistTest.php index 9be2657256c..2702023f3b8 100644 --- a/test/Github/Tests/Api/GistTest.php +++ b/test/Github/Tests/Api/GistTest.php @@ -78,4 +78,4 @@ protected function getApiClass() { return 'Github\Api\Gist'; } -} \ No newline at end of file +} diff --git a/test/Github/Tests/Functional/GistTest.php b/test/Github/Tests/Functional/GistTest.php index 0261bdb3a6e..e29d8031cd7 100644 --- a/test/Github/Tests/Functional/GistTest.php +++ b/test/Github/Tests/Functional/GistTest.php @@ -45,4 +45,4 @@ public function shouldRetrieveGistById() $this->assertArrayHasKey('gistfile1.txt', $gist['files']); $this->assertEquals('schacon', $gist['user']['login']); } -} \ No newline at end of file +} From 49bb59edca53e24211f012164e5142d93e69f1e1 Mon Sep 17 00:00:00 2001 From: Edoardo Rivello Date: Wed, 27 Jun 2012 16:37:46 +0200 Subject: [PATCH 16/16] Gists API integration added gist api to client gist create api create gist api refactor delete gist api gist api functional test for getList method gist api refactor: renamed delete method in remove method unit tests for gist api functional test for gist api: get by id comment fix for gist api fix gist api documentation added delete method to gist api readme update readme update pull request fixes list all public gists small refactor list starred gists small refactor doc fix create gist method refactor gist update method refactor readme updated small refactor readme update --- README.markdown | 96 ++++++++++++++++- lib/Github/Api/Gist.php | 120 ++++++++++++++++++++++ lib/Github/Client.php | 14 +++ test/Github/Tests/Api/GistTest.php | 96 +++++++++++++++++ test/Github/Tests/Functional/GistTest.php | 93 +++++++++++++++++ 5 files changed, 416 insertions(+), 3 deletions(-) create mode 100644 lib/Github/Api/Gist.php create mode 100644 test/Github/Tests/Api/GistTest.php create mode 100644 test/Github/Tests/Functional/GistTest.php diff --git a/README.markdown b/README.markdown index 9adf5050173..212a8fdf385 100755 --- a/README.markdown +++ b/README.markdown @@ -50,7 +50,7 @@ From this object, you can access to all GitHub apis, listed below. ## Navigation -[Users][] | [Issues][] | [Commits][] | [Objects][] | [Repos][] | [Pull Requests][] | [Request any Route][] | [Authentication & Security][] | [Customize php-github-api][] | [Run Test Suite][] +[Users][] | [Issues][] | [Commits][] | [Objects][] | [Repos][] | [Pull Requests][] | [Gists][] | [Request any Route][] | [Authentication & Security][] | [Customize php-github-api][] | [Run Test Suite][] ## Users @@ -556,8 +556,6 @@ To include non GitHub users, add a third parameter to true: ``` - - ## Pull Requests Go back to the Navigation @@ -629,6 +627,97 @@ Requires authentication. The issue ID is provided instead of title and body. This returns the details of the pull request. + +## Gists +Go back to the Navigation + +Creating, editing, deleting and listing gists. Wraps [GitHub Gists API](http://developer.github.com/v3/gists/). + +#### List gists of a specific user + +```php + $gists = $github->getGistApi()->getListByUser( 'ornicar' ); +``` + +#### List all public gists. + +```php + $gists = $github->getGistApi()->getPublicList(); +``` + +#### List the authenticated user’s starred gists. + +```php + $gists = $github->getGistApi()->getStarredList(); +``` + +Requires authentication. + +#### List the authenticated user’s gists or if called anonymously, this will return all public gists. + +```php + $gists = $github->getGistApi()->getList(); +``` + +#### Get a single gist + +```php + $gist = $github->getGistApi()->getGist( 1 ); +``` + +#### Create a gist + +```php + $files = array( 'filename.txt' => array( 'content' => 'txt file content' )); + $gist = $github->getGistApi()->create( $files, true, 'This is an optional description' ); +``` + +Creates and returns a public gist. + +#### Update a gist + +You can update ``description``. + +```php + $files = array(); + $gist = $github->getGistApi()->update( 1234, $files, 'This is a new description' ); +``` + +You can update ``content`` of a previous file's version. + +```php + $files = array( 'filename.txt' => array( 'content' => 'updated txt file content' )); + $gist = $github->getGistApi()->update( 1234, $files ); +``` + +You can update the ``filename`` of a previous file's version. + +```php + $files = array( 'filename.txt' => array( 'filename' => 'new-filename.txt' )); + $gist = $github->getGistApi()->update( 1234, $files ); +``` + +You can add a new file to the gist. + +```php + $files = array( 'new-filename.php' => array( 'content' => 'a new file content' )); + $gist = $github->getGistApi()->update( 1234, $files ); +``` + +You can remove a file from the gist. + +```php + $files = array( 'filename.txt' => null ); + $gist = $github->getGistApi()->update( 1234, $files ); +``` + +#### Delete a gist + +```php + $gist = $github->getGistApi()->remove( 1234 ); +``` + + ## Request any Route Go back to the Navigation @@ -770,6 +859,7 @@ Thanks to GitHub for the high quality API and documentation. [Commits]: #commits [Objects]: #objects [Repos]: #repos +[Gists]: #gists [Pull Requests]: #pull_requests [Request any Route]: #request_any_route [Authentication & Security]: #authentication_and_security diff --git a/lib/Github/Api/Gist.php b/lib/Github/Api/Gist.php new file mode 100644 index 00000000000..c9f7a0b0a9d --- /dev/null +++ b/lib/Github/Api/Gist.php @@ -0,0 +1,120 @@ + + * @license MIT License + */ +class Gist extends Api +{ + /** + * List the authenticated user’s gists or if called anonymously, + * this will return all public gists. + * @link http://developer.github.com/v3/gists/ + * + * @return array list of gist found + */ + public function getList() + { + return $this->get('gists'); + } + + /** + * List all public gists. + * @link http://developer.github.com/v3/gists/ + * + * @return array list of gist found + */ + public function getPublicList() + { + return $this->get('gists/public'); + } + + /** + * List the authenticated user’s starred gists. Requires authentication. + * @link http://developer.github.com/v3/gists/ + * + * @return array list of gist found + */ + public function getStarredList() + { + return $this->get('gists/starred'); + } + + /** + * List gists by username. + * @link http://developer.github.com/v3/gists/ + * + * @param string $username the username + * @return array list of gist found + */ + public function getListByUser($username) + { + return $this->get('users/'.urlencode($username).'/gists'); + } + + /** + * Show a specific gist. + * @link http://developer.github.com/v3/gists/ + * + * @param string $id the gist id + * @return array data from gist + */ + public function getGist($id) + { + return $this->get('gists/'.urlencode($id)); + } + + /** + * Create a new gist for the authenticated user otherwise for an + * anonymous user. + * + * @link http://developer.github.com/v3/gists/ + * + * @param array $files files that make up this gist + * @param bool $public 1 for public, 0 for private + * @param string $description optional gist description + * @return array returns gist data + */ + public function create($files, $public = false, $description = '') + { + return $this->post('gists', array( + 'description' => $description, + 'public' => $public, + 'files' => $files + )); + } + + /** + * Edit a gist. + * @link http://developer.github.com/v3/gists/ + * + * @param string $id the gist id + * @param array $files files that make up this gist + * @param string $description optional gist description + * @return array informations about the gist + */ + public function update($id, array $files = array(), $description = '') + { + return $this->patch('gists/'.urlencode($id), array( + 'description' => $description, + 'files' => $files + )); + } + + /** + * Remove a gist by id. + * @link http://developer.github.com/v3/gists/ + * + * @param int $id the gist id + * @return Response + */ + public function remove($id) + { + return $this->delete('gists/'.urlencode($id)); + } +} diff --git a/lib/Github/Client.php b/lib/Github/Client.php index adf4aa4caad..3d8e58fb23d 100644 --- a/lib/Github/Client.php +++ b/lib/Github/Client.php @@ -224,6 +224,20 @@ public function getCommitApi() return $this->apis['commit']; } + + /** + * Get the gist API + * + * @return Api\Gist the gist API + */ + public function getGistApi() + { + if (!isset($this->apis['gist'])) { + $this->apis['gist'] = new Api\Gist($this); + } + + return $this->apis['gist']; + } /** * Get the repo API diff --git a/test/Github/Tests/Api/GistTest.php b/test/Github/Tests/Api/GistTest.php new file mode 100644 index 00000000000..f92cafb9801 --- /dev/null +++ b/test/Github/Tests/Api/GistTest.php @@ -0,0 +1,96 @@ +getApiMock(); + + $api->expects($this->once()) + ->method('get') + ->with('gists/starred'); + + $gists = $api->getStarredList(); + } + + /** + * @test + */ + public function shouldCreateAnonymousGist() + { + $api = $this->getApiMock(); + + $files = array( + 'filename.txt' => array( + 'content' => 'content' + ) + ); + + $input = array( + 'description' => '', + 'public' => false, + 'files' => $files + ); + + $api->expects($this->once()) + ->method('post') + ->with('gists', $input); + + $gist = $api->create($files); + } + + /** + * @test + */ + public function shouldUpdateGist() + { + $api = $this->getApiMock(); + + $files = array( + 'filename.txt' => array( + 'filename' => 'new_name.txt', + 'content' => 'content' + ), + 'filename_new.txt' => array( + 'content' => 'content new' + ) + ); + + $input = array( + 'description' => 'jimbo', + 'files' => $files + ); + + $api->expects($this->once()) + ->method('patch') + ->with('gists/5', $input); + + $gist = $api->update(5, $files, 'jimbo'); + } + + /** + * @test + */ + public function shouldDeleteGist() + { + $api = $this->getApiMock(); + + $api->expects($this->once()) + ->method('delete') + ->with('gists/5'); + + $api->remove(5); + } + + protected function getApiClass() + { + return 'Github\Api\Gist'; + } +} diff --git a/test/Github/Tests/Functional/GistTest.php b/test/Github/Tests/Functional/GistTest.php new file mode 100644 index 00000000000..05785ede4bb --- /dev/null +++ b/test/Github/Tests/Functional/GistTest.php @@ -0,0 +1,93 @@ +getGistApi()->getList(); + $gist = array_pop($gists); + + $this->assertArrayHasKey('url', $gist); + $this->assertArrayHasKey('files', $gist); + $this->assertArrayHasKey('comments', $gist); + $this->assertArrayHasKey('created_at', $gist); + $this->assertArrayHasKey('updated_at', $gist); + $this->assertArrayHasKey('user', $gist); + } + + /** + * @test + */ + public function shouldNotGetStarredListWithoutAuthorization() + { + $github = new Client(); + $response = $github->getGistApi()->getStarredList(); + + $this->assertEquals('Requires authentication', $response['message']); + } + + /** + * @test + */ + public function shouldRetrieveGistsListByUser() + { + $username = 'KnpLabs'; + + $github = new Client(); + $gists = $github->getGistApi()->getListByUser($username); + $gist = array_pop($gists); + + $this->assertArrayHasKey('url', $gist); + $this->assertArrayHasKey('files', $gist); + $this->assertArrayHasKey('comments', $gist); + $this->assertArrayHasKey('created_at', $gist); + $this->assertArrayHasKey('updated_at', $gist); + $this->assertArrayHasKey('user', $gist); + $this->assertEquals('KnpLabs', $gist['user']['login']); + } + + /** + * @test + */ + public function shouldRetrievePublicGistsList() + { + $github = new Client(); + $gists = $github->getGistApi()->getPublicList(); + $gist = array_pop($gists); + + $this->assertArrayHasKey('url', $gist); + $this->assertArrayHasKey('files', $gist); + $this->assertArrayHasKey('comments', $gist); + $this->assertArrayHasKey('created_at', $gist); + $this->assertArrayHasKey('updated_at', $gist); + $this->assertArrayHasKey('user', $gist); + } + + /** + * @test + */ + public function shouldRetrieveGistById() + { + $id = 1; + + $github = new Client(); + $gist = $github->getGistApi()->getGist($id); + + $this->assertArrayHasKey('url', $gist); + $this->assertArrayHasKey('files', $gist); + $this->assertArrayHasKey('comments', $gist); + $this->assertArrayHasKey('created_at', $gist); + $this->assertArrayHasKey('updated_at', $gist); + $this->assertArrayHasKey('user', $gist); + $this->assertArrayHasKey('gistfile1.txt', $gist['files']); + $this->assertEquals('schacon', $gist['user']['login']); + } +}