Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: User Migration #1115

Merged
merged 5 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ v3 APIs:
* [Tags](repo/tags.md)
* [Search](search.md)
* [Users](users.md)
* [Migrations](user/migration.md)

Additional features:

Expand Down
79 changes: 79 additions & 0 deletions doc/user/migration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
## 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
$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

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);
```
10 changes: 10 additions & 0 deletions lib/Github/Api/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Github\Api;

use Github\Api\User\Migration;

/**
* Searching users, getting user information.
*
Expand Down Expand Up @@ -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());
}
}
83 changes: 83 additions & 0 deletions lib/Github/Api/User/Migration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Github\Api\User;

use Github\Api\AbstractApi;

class Migration extends AbstractApi
{
/**
* @link https://docs.github.com/en/rest/migrations/users?apiVersion=2022-11-28#list-user-migrations
*
* @param array $params
*
* @return array|string
*/
public function list(array $params = [])
{
return $this->get('/user/migrations', $params);
}

/**
* @link https://docs.github.com/en/rest/migrations/users?apiVersion=2022-11-28#start-a-user-migration
*
* @param array $params
*
* @return array|string
*/
public function start(array $params)
{
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);
}
}
186 changes: 186 additions & 0 deletions test/Github/Tests/Api/User/MigrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
<?php

namespace Github\Tests\Api\User;

use Github\Api\User\Migration;
use Github\Tests\Api\TestCase;
use PHPUnit\Framework\MockObject\MockObject;

class MigrationTest extends TestCase
{
/**
* @test
*/
public function shouldListUserMigrations()
{
$expectedArray = [
[
'id' => 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 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;
}
}