Skip to content

Commit

Permalink
feature #1115 feat: User Migration (haridarshan)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 3.12-dev branch.

Discussion
----------

Feature:

- User Migrations

Doc:
- User Migrations doc

Closes #795 

Commits
-------

3948174 feat: Add User Migrations Api
fc5a76c docs: Add migration list example with `ResultPager` and remove `per_page`
5389602 perf: remove `$params['repositories']` validation check
1bec9eb test: remove `shouldNotStartMigration` test-case
69fc864 chore(styleci): apply styleci patch
  • Loading branch information
haridarshan committed Oct 17, 2023
1 parent 90360bc commit 67398b0
Show file tree
Hide file tree
Showing 5 changed files with 359 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/README.md
Expand Up @@ -79,6 +79,7 @@ v3 APIs:
* [Secret Scanning Alert](repo/secret-scanning.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
@@ -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
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
@@ -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
@@ -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;
}
}

0 comments on commit 67398b0

Please sign in to comment.