From 2249ec99a3cfae75558a0fa93a2832cdb244e5c3 Mon Sep 17 00:00:00 2001 From: Daniel Camargo Date: Sat, 7 Apr 2018 14:36:41 -0600 Subject: [PATCH 1/6] adding invitations to repo --- .env-example | 9 ++ .gitignore | 4 + composer.json | 3 +- lib/Github/Api/Repo.php | 13 +++ lib/Github/Api/Repository/Invitations.php | 33 ++++++ .../Tests/Integration/InvitationsTest.php | 101 ++++++++++++++++++ test/Github/Tests/Integration/TestCase.php | 14 +++ 7 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 .env-example create mode 100644 lib/Github/Api/Repository/Invitations.php create mode 100644 test/Github/Tests/Integration/InvitationsTest.php diff --git a/.env-example b/.env-example new file mode 100644 index 00000000000..4c06ba6e4b8 --- /dev/null +++ b/.env-example @@ -0,0 +1,9 @@ +GITHUB_AUTH_METHOD=token +GITHUB_USERNAME= +GITHUB_PASSWORD= +GITHUB_TOKEN= + +GITHUB_DEFAULT_USER=KnpLabs +GITHUB_DEFAULT_REPO=php-github-api + +GITHUB_DEFAULT_COLLABORATOR= diff --git a/.gitignore b/.gitignore index 2c9d04ccd11..37d239ef1fc 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,7 @@ phpunit.xml composer.lock composer.phar vendor/* +# +.idea +.DS_STORE +.env diff --git a/composer.json b/composer.json index 9c96c716a3f..10a4458b432 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,8 @@ "php-http/mock-client": "^1.0", "guzzlehttp/psr7": "^1.2", "sllh/php-cs-fixer-styleci-bridge": "^1.3", - "cache/array-adapter": "^0.4" + "cache/array-adapter": "^0.4", + "vlucas/phpdotenv": "^2.4" }, "autoload": { "psr-4": { "Github\\": "lib/Github/" } diff --git a/lib/Github/Api/Repo.php b/lib/Github/Api/Repo.php index c0c54a95f15..497c733ea27 100644 --- a/lib/Github/Api/Repo.php +++ b/lib/Github/Api/Repo.php @@ -10,6 +10,7 @@ use Github\Api\Repository\Downloads; use Github\Api\Repository\Forks; use Github\Api\Repository\Hooks; +use Github\Api\Repository\Invitations; use Github\Api\Repository\Labels; use Github\Api\Repository\Projects; use Github\Api\Repository\Protection; @@ -283,6 +284,18 @@ public function collaborators() return new Collaborators($this->client); } + /** + * Manage the invitations of a repository. + * + * @link http://developer.github.com/v3/repos/invitations/ + * + * @return Invitations + */ + public function invitations() + { + return new Invitations($this->client); + } + /** * Manage the comments of a repository. * diff --git a/lib/Github/Api/Repository/Invitations.php b/lib/Github/Api/Repository/Invitations.php new file mode 100644 index 00000000000..a577db83f05 --- /dev/null +++ b/lib/Github/Api/Repository/Invitations.php @@ -0,0 +1,33 @@ +get('/repos/' . rawurlencode($username) . '/' . rawurlencode($repository) . '/invitations', array_merge(['page' => 1], $params)); + } + + public function update($username, $repository, $invitation, array $params) + { + return $this->patch('/repos/' . rawurlencode($username) . '/' . rawurlencode($repository) . '/invitations/' . rawurlencode($invitation), $params); + } + + public function accept($invitation) + { + return $this->patch('/user/repository_invitations/' . rawurlencode($invitation)); + } + + public function decline($invitation) + { + return $this->delete('/user/repository_invitations/' . rawurlencode($invitation)); + } + + public function remove($username, $repository, $invitation) + { + return $this->delete('/repos/' . rawurlencode($username) . '/' . rawurlencode($repository) . '/invitations/' . rawurlencode($invitation)); + } +} diff --git a/test/Github/Tests/Integration/InvitationsTest.php b/test/Github/Tests/Integration/InvitationsTest.php new file mode 100644 index 00000000000..dbc303ab081 --- /dev/null +++ b/test/Github/Tests/Integration/InvitationsTest.php @@ -0,0 +1,101 @@ +username = getenv("GITHUB_USER_1"); + $this->repo = getenv("GITHUB_REPO_1"); + + $this->invitedClient = new Client(); + $this->auth($this->invitedClient, 2); + } + + /** + * @test + */ + public function test() + { + $invitations = $this->listInvitations(); + $originalSize = count($invitations); + + $this->client->repo()->collaborators()->add($this->username, $this->repo, getenv('GITHUB_USER_2')); + $invitations = $this->listInvitations(); + $this->assertEquals($originalSize + 1, count($invitations)); + $invitation = $invitations[$originalSize]; + + $collaborators = $this->client->repo()->collaborators()->all($this->username, $this->repo); + $collaboratorsCount = count($collaborators); + + $this->accept($invitation['id']); + $collaborators = $this->client->repo()->collaborators()->all($this->username, $this->repo); + $this->assertEquals($collaboratorsCount + 1, count($collaborators)); + + $this->client->repo()->collaborators()->remove($this->username, $this->repo, getenv('GITHUB_USER_2')); + + $this->client->repo()->collaborators()->add($this->username, $this->repo, getenv('GITHUB_USER_2')); + $invitations = $this->listInvitations(); + $this->assertEquals($originalSize + 1, count($invitations)); + $invitation = $invitations[$originalSize]; + + $this->decline($invitation['id']); + $invitations = $this->listInvitations(); + $this->assertEquals($originalSize, count($invitations)); + + $this->client->repo()->collaborators()->add($this->username, $this->repo, getenv('GITHUB_USER_2')); + $invitations = $this->listInvitations(); + $this->assertEquals($originalSize + 1, count($invitations)); + $invitation = $invitations[$originalSize]; + + $this->updateInvitation($invitation['id']); + $invitations = $this->listInvitations(); + $invitation = $invitations[$originalSize]; + $this->assertEquals("read", $invitation['permissions']); + + $this->removeInvitation($invitation['id']); + $invitations = $this->listInvitations(); + $this->assertEquals($originalSize, count($invitations)); + } + + public function decline($id) + { + return $this->invitedClient->repo()->invitations()->decline($id); + } + + public function accept($id) + { + return $this->invitedClient->repo()->invitations()->accept($id); + } + + public function updateInvitation($id) + { + return $this->client->repo()->invitations()->update($this->username, $this->repo, $id, [ + 'permissions' => 'read', + ]); + } + + public function listInvitations() + { + return $this->client->repo()->invitations()->all($this->username, $this->repo); + } + + public function removeInvitation($id) + { + return $this->client->repo()->invitations()->remove($this->username, $this->repo, $id); + } +} diff --git a/test/Github/Tests/Integration/TestCase.php b/test/Github/Tests/Integration/TestCase.php index 6f8e5815719..0a0f50bd8d3 100644 --- a/test/Github/Tests/Integration/TestCase.php +++ b/test/Github/Tests/Integration/TestCase.php @@ -2,6 +2,7 @@ namespace Github\Tests\Integration; +use Dotenv\Dotenv; use Github\Client; use Github\Exception\ApiLimitExceedException; use Github\Exception\RuntimeException; @@ -20,6 +21,7 @@ public function setUp() { // You have to specify authentication here to run full suite $client = new Client(); + $this->auth($client); try { $client->api('current_user')->show(); @@ -33,4 +35,16 @@ public function setUp() $this->client = $client; } + + protected function auth(Client &$client, $accountNumber = 1) + { + (new Dotenv(__DIR__ . "/../../../../"))->load(); + $method = getenv('GITHUB_AUTH_METHOD'); + if ($method) { + $client->authenticate($method, getenv("GITHUB_TOKEN_{$accountNumber}")); + } else { + $client->authenticate($method, getenv("GITHUB_USERNAME_{$accountNumber}"), + getenv("GITHUB_PASSWORD_{$accountNumber}")); + } + } } From ce4db9c417efb7d01b8554ea12ce7b6f25340831 Mon Sep 17 00:00:00 2001 From: Daniel Camargo Date: Sat, 7 Apr 2018 14:48:56 -0600 Subject: [PATCH 2/6] silently ignoring errors from auth --- .env-example | 19 ++++++++++------- test/Github/Tests/Integration/TestCase.php | 24 +++++++++++++++------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/.env-example b/.env-example index 4c06ba6e4b8..4f6da72cb4a 100644 --- a/.env-example +++ b/.env-example @@ -1,9 +1,14 @@ -GITHUB_AUTH_METHOD=token -GITHUB_USERNAME= -GITHUB_PASSWORD= -GITHUB_TOKEN= +# token, login +GITHUB_AUTH_METHOD= -GITHUB_DEFAULT_USER=KnpLabs -GITHUB_DEFAULT_REPO=php-github-api +GITHUB_REPO_1= +GITHUB_USER_1= +GITHUB_USERNAME_1= +GITHUB_PASSWORD_1= +GITHUB_TOKEN_1= -GITHUB_DEFAULT_COLLABORATOR= +GITHUB_REPO_2= +GITHUB_USER_2= +GITHUB_USERNAME_2= +GITHUB_PASSWORD_2= +GITHUB_TOKEN_2= diff --git a/test/Github/Tests/Integration/TestCase.php b/test/Github/Tests/Integration/TestCase.php index 0a0f50bd8d3..d77d8b4eb32 100644 --- a/test/Github/Tests/Integration/TestCase.php +++ b/test/Github/Tests/Integration/TestCase.php @@ -38,13 +38,23 @@ public function setUp() protected function auth(Client &$client, $accountNumber = 1) { - (new Dotenv(__DIR__ . "/../../../../"))->load(); - $method = getenv('GITHUB_AUTH_METHOD'); - if ($method) { - $client->authenticate($method, getenv("GITHUB_TOKEN_{$accountNumber}")); - } else { - $client->authenticate($method, getenv("GITHUB_USERNAME_{$accountNumber}"), - getenv("GITHUB_PASSWORD_{$accountNumber}")); + try { + (new Dotenv(__DIR__ . "/../../../../"))->load(); + $method = getenv('GITHUB_AUTH_METHOD'); + if (!getenv('GITHUB_AUTH_METHOD')) { + return; + } + switch ($method) { + case "token": + $client->authenticate($method, getenv("GITHUB_TOKEN_{$accountNumber}")); + break; + case "login": + $client->authenticate($method, getenv("GITHUB_USERNAME_{$accountNumber}"), + getenv("GITHUB_PASSWORD_{$accountNumber}")); + break; + } + } catch (\Exception $e) { + error_log("Unable to authenticated", 0); } } } From e1f0cbfc959f5fb02f6d20b141e9298c2f9a08f4 Mon Sep 17 00:00:00 2001 From: Daniel Camargo Date: Sat, 7 Apr 2018 14:55:52 -0600 Subject: [PATCH 3/6] fixing style --- lib/Github/Api/Repository/Invitations.php | 10 +++++----- test/Github/Tests/Integration/InvitationsTest.php | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/Github/Api/Repository/Invitations.php b/lib/Github/Api/Repository/Invitations.php index a577db83f05..bc4c966da3a 100644 --- a/lib/Github/Api/Repository/Invitations.php +++ b/lib/Github/Api/Repository/Invitations.php @@ -8,26 +8,26 @@ class Invitations extends AbstractApi { public function all($username, $repository, array $params = []) { - return $this->get('/repos/' . rawurlencode($username) . '/' . rawurlencode($repository) . '/invitations', array_merge(['page' => 1], $params)); + return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/invitations', array_merge(['page' => 1], $params)); } public function update($username, $repository, $invitation, array $params) { - return $this->patch('/repos/' . rawurlencode($username) . '/' . rawurlencode($repository) . '/invitations/' . rawurlencode($invitation), $params); + return $this->patch('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/invitations/'.rawurlencode($invitation), $params); } public function accept($invitation) { - return $this->patch('/user/repository_invitations/' . rawurlencode($invitation)); + return $this->patch('/user/repository_invitations/'.rawurlencode($invitation)); } public function decline($invitation) { - return $this->delete('/user/repository_invitations/' . rawurlencode($invitation)); + return $this->delete('/user/repository_invitations/'.rawurlencode($invitation)); } public function remove($username, $repository, $invitation) { - return $this->delete('/repos/' . rawurlencode($username) . '/' . rawurlencode($repository) . '/invitations/' . rawurlencode($invitation)); + return $this->delete('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/invitations/'.rawurlencode($invitation)); } } diff --git a/test/Github/Tests/Integration/InvitationsTest.php b/test/Github/Tests/Integration/InvitationsTest.php index dbc303ab081..0818f80c45e 100644 --- a/test/Github/Tests/Integration/InvitationsTest.php +++ b/test/Github/Tests/Integration/InvitationsTest.php @@ -20,7 +20,7 @@ public function setUp() { parent::setUp(); $this->username = getenv("GITHUB_USER_1"); - $this->repo = getenv("GITHUB_REPO_1"); + $this->repo = getenv("GITHUB_REPO_1"); $this->invitedClient = new Client(); $this->auth($this->invitedClient, 2); @@ -31,7 +31,7 @@ public function setUp() */ public function test() { - $invitations = $this->listInvitations(); + $invitations = $this->listInvitations(); $originalSize = count($invitations); $this->client->repo()->collaborators()->add($this->username, $this->repo, getenv('GITHUB_USER_2')); @@ -39,7 +39,7 @@ public function test() $this->assertEquals($originalSize + 1, count($invitations)); $invitation = $invitations[$originalSize]; - $collaborators = $this->client->repo()->collaborators()->all($this->username, $this->repo); + $collaborators = $this->client->repo()->collaborators()->all($this->username, $this->repo); $collaboratorsCount = count($collaborators); $this->accept($invitation['id']); @@ -64,7 +64,7 @@ public function test() $this->updateInvitation($invitation['id']); $invitations = $this->listInvitations(); - $invitation = $invitations[$originalSize]; + $invitation = $invitations[$originalSize]; $this->assertEquals("read", $invitation['permissions']); $this->removeInvitation($invitation['id']); From 4c507ced707567b7f4be81c613d35e16c0716497 Mon Sep 17 00:00:00 2001 From: Daniel Camargo Date: Sat, 7 Apr 2018 14:58:13 -0600 Subject: [PATCH 4/6] fixing style --- test/Github/Tests/Integration/InvitationsTest.php | 6 +++--- test/Github/Tests/Integration/TestCase.php | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/test/Github/Tests/Integration/InvitationsTest.php b/test/Github/Tests/Integration/InvitationsTest.php index 0818f80c45e..be7f14993cc 100644 --- a/test/Github/Tests/Integration/InvitationsTest.php +++ b/test/Github/Tests/Integration/InvitationsTest.php @@ -19,8 +19,8 @@ class InvitationsTest extends TestCase public function setUp() { parent::setUp(); - $this->username = getenv("GITHUB_USER_1"); - $this->repo = getenv("GITHUB_REPO_1"); + $this->username = getenv('GITHUB_USER_1'); + $this->repo = getenv('GITHUB_REPO_1'); $this->invitedClient = new Client(); $this->auth($this->invitedClient, 2); @@ -65,7 +65,7 @@ public function test() $this->updateInvitation($invitation['id']); $invitations = $this->listInvitations(); $invitation = $invitations[$originalSize]; - $this->assertEquals("read", $invitation['permissions']); + $this->assertEquals('read', $invitation['permissions']); $this->removeInvitation($invitation['id']); $invitations = $this->listInvitations(); diff --git a/test/Github/Tests/Integration/TestCase.php b/test/Github/Tests/Integration/TestCase.php index d77d8b4eb32..9dd2ec8b1b1 100644 --- a/test/Github/Tests/Integration/TestCase.php +++ b/test/Github/Tests/Integration/TestCase.php @@ -45,16 +45,16 @@ protected function auth(Client &$client, $accountNumber = 1) return; } switch ($method) { - case "token": - $client->authenticate($method, getenv("GITHUB_TOKEN_{$accountNumber}")); + case 'token': + $client->authenticate($method, getenv('GITHUB_TOKEN_'.$accountNumber)); break; - case "login": - $client->authenticate($method, getenv("GITHUB_USERNAME_{$accountNumber}"), - getenv("GITHUB_PASSWORD_{$accountNumber}")); + case 'login': + $client->authenticate($method, getenv('GITHUB_USERNAME_'.$accountNumber), + getenv('GITHUB_PASSWORD_{$accountNumber}')); break; } } catch (\Exception $e) { - error_log("Unable to authenticated", 0); + error_log('Unable to authenticated', 0); } } } From 43fb818af7a3262f6daef304b5b6b9e7206f0592 Mon Sep 17 00:00:00 2001 From: Daniel Camargo Date: Sat, 7 Apr 2018 14:59:37 -0600 Subject: [PATCH 5/6] fixing style --- test/Github/Tests/Integration/InvitationsTest.php | 1 - test/Github/Tests/Integration/TestCase.php | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/test/Github/Tests/Integration/InvitationsTest.php b/test/Github/Tests/Integration/InvitationsTest.php index be7f14993cc..9321e337622 100644 --- a/test/Github/Tests/Integration/InvitationsTest.php +++ b/test/Github/Tests/Integration/InvitationsTest.php @@ -2,7 +2,6 @@ namespace Github\Tests\Integration; -use function Clue\StreamFilter\remove; use Github\Client; /** diff --git a/test/Github/Tests/Integration/TestCase.php b/test/Github/Tests/Integration/TestCase.php index 9dd2ec8b1b1..f6206c9c31f 100644 --- a/test/Github/Tests/Integration/TestCase.php +++ b/test/Github/Tests/Integration/TestCase.php @@ -39,7 +39,7 @@ public function setUp() protected function auth(Client &$client, $accountNumber = 1) { try { - (new Dotenv(__DIR__ . "/../../../../"))->load(); + (new Dotenv(__DIR__.'/../../../../'))->load(); $method = getenv('GITHUB_AUTH_METHOD'); if (!getenv('GITHUB_AUTH_METHOD')) { return; From e6289b1151ac49d0b716b13846f2b255fe98ed55 Mon Sep 17 00:00:00 2001 From: Daniel Camargo Date: Sat, 7 Apr 2018 19:06:42 -0600 Subject: [PATCH 6/6] changing name --- composer.json | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 10a4458b432..5ee0d93afcb 100644 --- a/composer.json +++ b/composer.json @@ -1,9 +1,14 @@ { - "name": "knplabs/github-api", + "name": "teamdevsquad/github-api", "type": "library", "description": "GitHub API v3 client", "homepage": "https://github.com/KnpLabs/php-github-api", - "keywords": ["github", "gh", "api", "gist"], + "keywords": [ + "github", + "gh", + "api", + "gist" + ], "license": "MIT", "authors": [ { @@ -36,10 +41,14 @@ "vlucas/phpdotenv": "^2.4" }, "autoload": { - "psr-4": { "Github\\": "lib/Github/" } + "psr-4": { + "Github\\": "lib/Github/" + } }, "autoload-dev": { - "psr-4": { "Github\\Tests\\": "test/Github/Tests/"} + "psr-4": { + "Github\\Tests\\": "test/Github/Tests/" + } }, "minimum-stability": "dev", "prefer-stable": true,