From 394817408ae5cc542815e181d6ee82f66c874740 Mon Sep 17 00:00:00 2001 From: Hari Darshan Gorana Date: Mon, 24 Apr 2023 16:15:34 +0530 Subject: [PATCH 1/5] feat: Add User Migrations Api Signed-off-by: Hari Darshan Gorana --- doc/README.md | 1 + doc/user/migration.md | 70 +++++++ lib/Github/Api/User.php | 10 + lib/Github/Api/User/Migration.php | 90 ++++++++ test/Github/Tests/Api/User/MigrationTest.php | 203 +++++++++++++++++++ 5 files changed, 374 insertions(+) create mode 100644 doc/user/migration.md create mode 100644 lib/Github/Api/User/Migration.php create mode 100644 test/Github/Tests/Api/User/MigrationTest.php diff --git a/doc/README.md b/doc/README.md index ee453269d35..d67bf87aff0 100644 --- a/doc/README.md +++ b/doc/README.md @@ -76,6 +76,7 @@ v3 APIs: * [Tags](repo/tags.md) * [Search](search.md) * [Users](users.md) + * [Migrations](user/migration.md) Additional features: diff --git a/doc/user/migration.md b/doc/user/migration.md new file mode 100644 index 00000000000..2e8079be56b --- /dev/null +++ b/doc/user/migration.md @@ -0,0 +1,70 @@ +## User / Migrations API +[Back to the "Users API"](../../users.md) | [Back to the navigation](../../README.md) + +# List user migrations + +https://docs.github.com/en/rest/migrations/users?apiVersion=2022-11-28#list-user-migrations + +```php +$migrations = $client->api('user')->migration()->list([ + 'per_page' => 10 +]); +``` + +# Start a User Migration + +https://docs.github.com/en/rest/migrations/users?apiVersion=2022-11-28#start-a-user-migration + +```php +$client->users()->migration()->start([ + 'repositories' => [ + 'KnpLabs/php-github-api' + ], + 'lock_repositories' => true, + 'exclude_metadata' => false, + 'exclude_git_data' => false, + 'exclude_attachments' => true, + 'exclude_releases' => false, + 'exclude_owner_projects' => true, + 'org_metadata_only' => false, + 'exclude' => [ + 'Exclude attributes from the API response to improve performance' + ] +]); +``` + +# Get a User Migration Status + +https://docs.github.com/en/rest/migrations/users?apiVersion=2022-11-28#get-a-user-migration-status + +```php +$status = $client->user()->migration()->status(12, [ + 'exclude' => [ + 'exclude attributes' + ] +]); +``` + +# Delete a User Migration Archive + +https://docs.github.com/en/rest/migrations/users?apiVersion=2022-11-28#delete-a-user-migration-archive + +```php +$client->user()->migration()->deleteArchive(12); +``` + +# Unlock a User Repository + +https://docs.github.com/en/rest/migrations/users?apiVersion=2022-11-28#unlock-a-user-repository + +```php +$client->user()->migration()->unlockRepo(12, 'php-github-api'); +``` + +# List repositories for a User Migration + +https://docs.github.com/en/rest/migrations/users?apiVersion=2022-11-28#list-repositories-for-a-user-migration + +```php +$repos = $client->user()->migration()->repos(2); +``` diff --git a/lib/Github/Api/User.php b/lib/Github/Api/User.php index c1ccc89e8c1..d436a2b835e 100644 --- a/lib/Github/Api/User.php +++ b/lib/Github/Api/User.php @@ -2,6 +2,8 @@ namespace Github\Api; +use Github\Api\User\Migration; + /** * Searching users, getting user information. * @@ -246,4 +248,12 @@ public function events(string $username) { return $this->get('/users/'.rawurlencode($username).'/events'); } + + /** + * @return Migration + */ + public function migration(): Migration + { + return new Migration($this->getClient()); + } } diff --git a/lib/Github/Api/User/Migration.php b/lib/Github/Api/User/Migration.php new file mode 100644 index 00000000000..133d6436525 --- /dev/null +++ b/lib/Github/Api/User/Migration.php @@ -0,0 +1,90 @@ +get('/user/migrations', $params); + } + + /** + * @link https://docs.github.com/en/rest/migrations/users?apiVersion=2022-11-28#start-a-user-migration + * + * @param array $params + * + * @throws MissingArgumentException + * + * @return array|string + */ + public function start(array $params) + { + if (!isset($params['repositories'])) { + throw new MissingArgumentException(['repositories']); + } + + return $this->post('/user/migrations', $params); + } + + /** + * @link https://docs.github.com/en/rest/migrations/users?apiVersion=2022-11-28#get-a-user-migration-status + * + * @param int $migrationId + * @param array $params + * + * @return array|string + */ + public function status(int $migrationId, array $params = []) + { + return $this->get('/user/migrations/'.$migrationId, $params); + } + + /** + * @link https://docs.github.com/en/rest/migrations/users?apiVersion=2022-11-28#delete-a-user-migration-archive + * + * @param int $migrationId + * + * @return array|string + */ + public function deleteArchive(int $migrationId) + { + return $this->delete('/user/migrations/'.$migrationId.'/archive'); + } + + /** + * @link https://docs.github.com/en/rest/migrations/users?apiVersion=2022-11-28#unlock-a-user-repository + * + * @param int $migrationId + * @param string $repository + * + * @return array|string + */ + public function unlockRepo(int $migrationId, string $repository) + { + return $this->delete('/user/migrations/'.$migrationId.'/repos/'.rawurlencode($repository).'/lock'); + } + + /** + * @link https://docs.github.com/en/rest/migrations/users?apiVersion=2022-11-28#list-repositories-for-a-user-migration + * + * @param int $migrationId + * @param array $params + * + * @return array|string + */ + public function repos(int $migrationId, array $params = []) + { + return $this->get('/user/migrations/'.$migrationId.'/repositories', $params); + } +} diff --git a/test/Github/Tests/Api/User/MigrationTest.php b/test/Github/Tests/Api/User/MigrationTest.php new file mode 100644 index 00000000000..83cd546b0d3 --- /dev/null +++ b/test/Github/Tests/Api/User/MigrationTest.php @@ -0,0 +1,203 @@ + 79, + 'state' => 'pending', + 'lock_repositories' => true, + 'repositories' => [ + [ + 'id' => 1296269, + 'name' => 'Hello-World', + 'full_name' => 'octocat/Hello-World', + ], + ], + ], + [ + 'id' => 2, + 'name' => 'pending', + 'lock_repositories' => false, + 'repositories' => [ + [ + 'id' => 123, + 'name' => 'php-github-api', + 'full_name' => 'KnpLabs/php-github-api', + ], + ], + ], + ]; + + /** @var Migration|MockObject $api */ + $api = $this->getApiMock(); + + $api + ->expects($this->once()) + ->method('get') + ->with('/user/migrations') + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->list()); + } + + /** + * @test + */ + public function shouldNotStartMigration() + { + $this->expectException(MissingArgumentException::class); + + /** @var Migration|MockObject $api */ + $api = $this->getApiMock(); + + $api->expects($this->never()) + ->method('post'); + + $api->start([]); + } + + /** + * @test + */ + public function shouldStartMigration() + { + $expectedArray = [ + 'id' => 79, + 'state' => 'pending', + 'lock_repositories' => true, + 'repositories' => [ + [ + 'id' => 1296269, + 'name' => 'Hello-World', + 'full_name' => 'octocat/Hello-World', + ], + ], + ]; + + /** @var Migration|MockObject $api */ + $api = $this->getApiMock(); + + $api->expects($this->once()) + ->method('post') + ->with('/user/migrations') + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->start([ + 'lock_repositories' => true, + 'repositories' => [ + 'KnpLabs/php-github-api', + ], + ])); + } + + /** + * @test + */ + public function shouldGetMigrationStatus() + { + $expectedArray = [ + 'id' => 79, + 'state' => 'exported', + 'lock_repositories' => true, + 'repositories' => [ + [ + 'id' => 1296269, + 'name' => 'Hello-World', + 'full_name' => 'octocat/Hello-World', + ], + ], + ]; + + /** @var Migration|MockObject $api */ + $api = $this->getApiMock(); + + $api->expects($this->once()) + ->method('get') + ->with('/user/migrations/79') + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->status(79)); + } + + /** + * @test + */ + public function shouldDeleteMigrationArchive() + { + /** @var Migration|MockObject $api */ + $api = $this->getApiMock(); + + $api->expects($this->once()) + ->method('delete') + ->with('/user/migrations/79/archive') + ->will($this->returnValue(204)); + + $this->assertEquals(204, $api->deleteArchive(79)); + } + + /** + * @test + */ + public function shouldUnlockUserRepo() + { + /** @var Migration|MockObject $api */ + $api = $this->getApiMock(); + + $api->expects($this->once()) + ->method('delete') + ->with('/user/migrations/79/repos/php-github-api/lock') + ->will($this->returnValue(204)); + + $this->assertEquals(204, $api->unlockRepo(79, 'php-github-api')); + } + + /** + * @test + */ + public function shouldListRepos() + { + $expectedArray = [ + [ + 'id' => 1296269, + 'name' => 'Hello-World', + 'full_name' => 'test/Hello-World', + ], + [ + 'id' => 234324, + 'name' => 'Hello-World2', + 'full_name' => 'test/Hello-World2', + ], + ]; + + /** @var Migration|MockObject $api */ + $api = $this->getApiMock(); + + $api->expects($this->once()) + ->method('get') + ->with('/user/migrations/79/repositories') + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->repos(79)); + } + + /** + * @return string + */ + protected function getApiClass() + { + return \Github\Api\User\Migration::class; + } +} From fc5a76cd166cf3e3d42298383d8fca34faf8367b Mon Sep 17 00:00:00 2001 From: Hari Darshan Gorana Date: Mon, 15 May 2023 18:48:38 +0530 Subject: [PATCH 2/5] docs: Add migration list example with `ResultPager` and remove `per_page` Signed-off-by: Hari Darshan Gorana --- doc/user/migration.md | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/doc/user/migration.md b/doc/user/migration.md index 2e8079be56b..a2c3eb14c5e 100644 --- a/doc/user/migration.md +++ b/doc/user/migration.md @@ -6,9 +6,18 @@ https://docs.github.com/en/rest/migrations/users?apiVersion=2022-11-28#list-user-migrations ```php -$migrations = $client->api('user')->migration()->list([ - 'per_page' => 10 -]); +$api = $github->api('user')->migration(); +$paginator = new Github\ResultPager($github); +$parameters = []; +$migrations = $paginator->fetchAll($api, 'list', $parameters); + +do { + foreach ($migrations as $migration) { + // do something + } + $migrations = $paginator->fetchNext(); +} +while($paginator->hasNext()); ``` # Start a User Migration From 5389602c78e918781d8a3850de600244c87dea51 Mon Sep 17 00:00:00 2001 From: Hari Darshan Gorana Date: Mon, 15 May 2023 18:49:22 +0530 Subject: [PATCH 3/5] perf: remove `$params['repositories']` validation check Signed-off-by: Hari Darshan Gorana --- lib/Github/Api/User/Migration.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lib/Github/Api/User/Migration.php b/lib/Github/Api/User/Migration.php index 133d6436525..3d2d495de87 100644 --- a/lib/Github/Api/User/Migration.php +++ b/lib/Github/Api/User/Migration.php @@ -24,16 +24,10 @@ public function list(array $params = []) * * @param array $params * - * @throws MissingArgumentException - * * @return array|string */ public function start(array $params) { - if (!isset($params['repositories'])) { - throw new MissingArgumentException(['repositories']); - } - return $this->post('/user/migrations', $params); } From 1bec9eb3ac20e46bece11563255bf9b35fa0370d Mon Sep 17 00:00:00 2001 From: Hari Darshan Gorana Date: Mon, 15 May 2023 19:14:12 +0530 Subject: [PATCH 4/5] test: remove `shouldNotStartMigration` test-case Signed-off-by: Hari Darshan Gorana --- test/Github/Tests/Api/User/MigrationTest.php | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/test/Github/Tests/Api/User/MigrationTest.php b/test/Github/Tests/Api/User/MigrationTest.php index 83cd546b0d3..c731dd13725 100644 --- a/test/Github/Tests/Api/User/MigrationTest.php +++ b/test/Github/Tests/Api/User/MigrationTest.php @@ -53,22 +53,6 @@ public function shouldListUserMigrations() $this->assertEquals($expectedArray, $api->list()); } - /** - * @test - */ - public function shouldNotStartMigration() - { - $this->expectException(MissingArgumentException::class); - - /** @var Migration|MockObject $api */ - $api = $this->getApiMock(); - - $api->expects($this->never()) - ->method('post'); - - $api->start([]); - } - /** * @test */ From 69fc86460f80e3ca5db8181ffcaca7e2c27d550f Mon Sep 17 00:00:00 2001 From: Hari Darshan Gorana Date: Mon, 15 May 2023 19:15:22 +0530 Subject: [PATCH 5/5] chore(styleci): apply styleci patch Signed-off-by: Hari Darshan Gorana --- lib/Github/Api/User/Migration.php | 1 - test/Github/Tests/Api/User/MigrationTest.php | 1 - 2 files changed, 2 deletions(-) diff --git a/lib/Github/Api/User/Migration.php b/lib/Github/Api/User/Migration.php index 3d2d495de87..4e1b61ca244 100644 --- a/lib/Github/Api/User/Migration.php +++ b/lib/Github/Api/User/Migration.php @@ -3,7 +3,6 @@ namespace Github\Api\User; use Github\Api\AbstractApi; -use Github\Exception\MissingArgumentException; class Migration extends AbstractApi { diff --git a/test/Github/Tests/Api/User/MigrationTest.php b/test/Github/Tests/Api/User/MigrationTest.php index c731dd13725..3ee1620a3ae 100644 --- a/test/Github/Tests/Api/User/MigrationTest.php +++ b/test/Github/Tests/Api/User/MigrationTest.php @@ -3,7 +3,6 @@ namespace Github\Tests\Api\User; use Github\Api\User\Migration; -use Github\Exception\MissingArgumentException; use Github\Tests\Api\TestCase; use PHPUnit\Framework\MockObject\MockObject;