Skip to content

Commit 67398b0

Browse files
authored
feature KnpLabs#1115 feat: User Migration (haridarshan)
This PR was squashed before being merged into the 3.12-dev branch. Discussion ---------- Feature: - User Migrations Doc: - User Migrations doc Closes KnpLabs#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
1 parent 90360bc commit 67398b0

File tree

5 files changed

+359
-0
lines changed

5 files changed

+359
-0
lines changed

doc/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ v3 APIs:
7979
* [Secret Scanning Alert](repo/secret-scanning.md)
8080
* [Search](search.md)
8181
* [Users](users.md)
82+
* [Migrations](user/migration.md)
8283

8384
Additional features:
8485

doc/user/migration.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
## User / Migrations API
2+
[Back to the "Users API"](../../users.md) | [Back to the navigation](../../README.md)
3+
4+
# List user migrations
5+
6+
https://docs.github.com/en/rest/migrations/users?apiVersion=2022-11-28#list-user-migrations
7+
8+
```php
9+
$api = $github->api('user')->migration();
10+
$paginator = new Github\ResultPager($github);
11+
$parameters = [];
12+
$migrations = $paginator->fetchAll($api, 'list', $parameters);
13+
14+
do {
15+
foreach ($migrations as $migration) {
16+
// do something
17+
}
18+
$migrations = $paginator->fetchNext();
19+
}
20+
while($paginator->hasNext());
21+
```
22+
23+
# Start a User Migration
24+
25+
https://docs.github.com/en/rest/migrations/users?apiVersion=2022-11-28#start-a-user-migration
26+
27+
```php
28+
$client->users()->migration()->start([
29+
'repositories' => [
30+
'KnpLabs/php-github-api'
31+
],
32+
'lock_repositories' => true,
33+
'exclude_metadata' => false,
34+
'exclude_git_data' => false,
35+
'exclude_attachments' => true,
36+
'exclude_releases' => false,
37+
'exclude_owner_projects' => true,
38+
'org_metadata_only' => false,
39+
'exclude' => [
40+
'Exclude attributes from the API response to improve performance'
41+
]
42+
]);
43+
```
44+
45+
# Get a User Migration Status
46+
47+
https://docs.github.com/en/rest/migrations/users?apiVersion=2022-11-28#get-a-user-migration-status
48+
49+
```php
50+
$status = $client->user()->migration()->status(12, [
51+
'exclude' => [
52+
'exclude attributes'
53+
]
54+
]);
55+
```
56+
57+
# Delete a User Migration Archive
58+
59+
https://docs.github.com/en/rest/migrations/users?apiVersion=2022-11-28#delete-a-user-migration-archive
60+
61+
```php
62+
$client->user()->migration()->deleteArchive(12);
63+
```
64+
65+
# Unlock a User Repository
66+
67+
https://docs.github.com/en/rest/migrations/users?apiVersion=2022-11-28#unlock-a-user-repository
68+
69+
```php
70+
$client->user()->migration()->unlockRepo(12, 'php-github-api');
71+
```
72+
73+
# List repositories for a User Migration
74+
75+
https://docs.github.com/en/rest/migrations/users?apiVersion=2022-11-28#list-repositories-for-a-user-migration
76+
77+
```php
78+
$repos = $client->user()->migration()->repos(2);
79+
```

lib/Github/Api/User.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Github\Api;
44

5+
use Github\Api\User\Migration;
6+
57
/**
68
* Searching users, getting user information.
79
*
@@ -246,4 +248,12 @@ public function events(string $username)
246248
{
247249
return $this->get('/users/'.rawurlencode($username).'/events');
248250
}
251+
252+
/**
253+
* @return Migration
254+
*/
255+
public function migration(): Migration
256+
{
257+
return new Migration($this->getClient());
258+
}
249259
}

lib/Github/Api/User/Migration.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace Github\Api\User;
4+
5+
use Github\Api\AbstractApi;
6+
7+
class Migration extends AbstractApi
8+
{
9+
/**
10+
* @link https://docs.github.com/en/rest/migrations/users?apiVersion=2022-11-28#list-user-migrations
11+
*
12+
* @param array $params
13+
*
14+
* @return array|string
15+
*/
16+
public function list(array $params = [])
17+
{
18+
return $this->get('/user/migrations', $params);
19+
}
20+
21+
/**
22+
* @link https://docs.github.com/en/rest/migrations/users?apiVersion=2022-11-28#start-a-user-migration
23+
*
24+
* @param array $params
25+
*
26+
* @return array|string
27+
*/
28+
public function start(array $params)
29+
{
30+
return $this->post('/user/migrations', $params);
31+
}
32+
33+
/**
34+
* @link https://docs.github.com/en/rest/migrations/users?apiVersion=2022-11-28#get-a-user-migration-status
35+
*
36+
* @param int $migrationId
37+
* @param array $params
38+
*
39+
* @return array|string
40+
*/
41+
public function status(int $migrationId, array $params = [])
42+
{
43+
return $this->get('/user/migrations/'.$migrationId, $params);
44+
}
45+
46+
/**
47+
* @link https://docs.github.com/en/rest/migrations/users?apiVersion=2022-11-28#delete-a-user-migration-archive
48+
*
49+
* @param int $migrationId
50+
*
51+
* @return array|string
52+
*/
53+
public function deleteArchive(int $migrationId)
54+
{
55+
return $this->delete('/user/migrations/'.$migrationId.'/archive');
56+
}
57+
58+
/**
59+
* @link https://docs.github.com/en/rest/migrations/users?apiVersion=2022-11-28#unlock-a-user-repository
60+
*
61+
* @param int $migrationId
62+
* @param string $repository
63+
*
64+
* @return array|string
65+
*/
66+
public function unlockRepo(int $migrationId, string $repository)
67+
{
68+
return $this->delete('/user/migrations/'.$migrationId.'/repos/'.rawurlencode($repository).'/lock');
69+
}
70+
71+
/**
72+
* @link https://docs.github.com/en/rest/migrations/users?apiVersion=2022-11-28#list-repositories-for-a-user-migration
73+
*
74+
* @param int $migrationId
75+
* @param array $params
76+
*
77+
* @return array|string
78+
*/
79+
public function repos(int $migrationId, array $params = [])
80+
{
81+
return $this->get('/user/migrations/'.$migrationId.'/repositories', $params);
82+
}
83+
}
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
<?php
2+
3+
namespace Github\Tests\Api\User;
4+
5+
use Github\Api\User\Migration;
6+
use Github\Tests\Api\TestCase;
7+
use PHPUnit\Framework\MockObject\MockObject;
8+
9+
class MigrationTest extends TestCase
10+
{
11+
/**
12+
* @test
13+
*/
14+
public function shouldListUserMigrations()
15+
{
16+
$expectedArray = [
17+
[
18+
'id' => 79,
19+
'state' => 'pending',
20+
'lock_repositories' => true,
21+
'repositories' => [
22+
[
23+
'id' => 1296269,
24+
'name' => 'Hello-World',
25+
'full_name' => 'octocat/Hello-World',
26+
],
27+
],
28+
],
29+
[
30+
'id' => 2,
31+
'name' => 'pending',
32+
'lock_repositories' => false,
33+
'repositories' => [
34+
[
35+
'id' => 123,
36+
'name' => 'php-github-api',
37+
'full_name' => 'KnpLabs/php-github-api',
38+
],
39+
],
40+
],
41+
];
42+
43+
/** @var Migration|MockObject $api */
44+
$api = $this->getApiMock();
45+
46+
$api
47+
->expects($this->once())
48+
->method('get')
49+
->with('/user/migrations')
50+
->will($this->returnValue($expectedArray));
51+
52+
$this->assertEquals($expectedArray, $api->list());
53+
}
54+
55+
/**
56+
* @test
57+
*/
58+
public function shouldStartMigration()
59+
{
60+
$expectedArray = [
61+
'id' => 79,
62+
'state' => 'pending',
63+
'lock_repositories' => true,
64+
'repositories' => [
65+
[
66+
'id' => 1296269,
67+
'name' => 'Hello-World',
68+
'full_name' => 'octocat/Hello-World',
69+
],
70+
],
71+
];
72+
73+
/** @var Migration|MockObject $api */
74+
$api = $this->getApiMock();
75+
76+
$api->expects($this->once())
77+
->method('post')
78+
->with('/user/migrations')
79+
->will($this->returnValue($expectedArray));
80+
81+
$this->assertEquals($expectedArray, $api->start([
82+
'lock_repositories' => true,
83+
'repositories' => [
84+
'KnpLabs/php-github-api',
85+
],
86+
]));
87+
}
88+
89+
/**
90+
* @test
91+
*/
92+
public function shouldGetMigrationStatus()
93+
{
94+
$expectedArray = [
95+
'id' => 79,
96+
'state' => 'exported',
97+
'lock_repositories' => true,
98+
'repositories' => [
99+
[
100+
'id' => 1296269,
101+
'name' => 'Hello-World',
102+
'full_name' => 'octocat/Hello-World',
103+
],
104+
],
105+
];
106+
107+
/** @var Migration|MockObject $api */
108+
$api = $this->getApiMock();
109+
110+
$api->expects($this->once())
111+
->method('get')
112+
->with('/user/migrations/79')
113+
->will($this->returnValue($expectedArray));
114+
115+
$this->assertEquals($expectedArray, $api->status(79));
116+
}
117+
118+
/**
119+
* @test
120+
*/
121+
public function shouldDeleteMigrationArchive()
122+
{
123+
/** @var Migration|MockObject $api */
124+
$api = $this->getApiMock();
125+
126+
$api->expects($this->once())
127+
->method('delete')
128+
->with('/user/migrations/79/archive')
129+
->will($this->returnValue(204));
130+
131+
$this->assertEquals(204, $api->deleteArchive(79));
132+
}
133+
134+
/**
135+
* @test
136+
*/
137+
public function shouldUnlockUserRepo()
138+
{
139+
/** @var Migration|MockObject $api */
140+
$api = $this->getApiMock();
141+
142+
$api->expects($this->once())
143+
->method('delete')
144+
->with('/user/migrations/79/repos/php-github-api/lock')
145+
->will($this->returnValue(204));
146+
147+
$this->assertEquals(204, $api->unlockRepo(79, 'php-github-api'));
148+
}
149+
150+
/**
151+
* @test
152+
*/
153+
public function shouldListRepos()
154+
{
155+
$expectedArray = [
156+
[
157+
'id' => 1296269,
158+
'name' => 'Hello-World',
159+
'full_name' => 'test/Hello-World',
160+
],
161+
[
162+
'id' => 234324,
163+
'name' => 'Hello-World2',
164+
'full_name' => 'test/Hello-World2',
165+
],
166+
];
167+
168+
/** @var Migration|MockObject $api */
169+
$api = $this->getApiMock();
170+
171+
$api->expects($this->once())
172+
->method('get')
173+
->with('/user/migrations/79/repositories')
174+
->will($this->returnValue($expectedArray));
175+
176+
$this->assertEquals($expectedArray, $api->repos(79));
177+
}
178+
179+
/**
180+
* @return string
181+
*/
182+
protected function getApiClass()
183+
{
184+
return \Github\Api\User\Migration::class;
185+
}
186+
}

0 commit comments

Comments
 (0)