Skip to content

Commit 3948174

Browse files
committed
feat: Add User Migrations Api
Signed-off-by: Hari Darshan Gorana <[email protected]>
1 parent 0e2399c commit 3948174

File tree

5 files changed

+374
-0
lines changed

5 files changed

+374
-0
lines changed

doc/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ v3 APIs:
7676
* [Tags](repo/tags.md)
7777
* [Search](search.md)
7878
* [Users](users.md)
79+
* [Migrations](user/migration.md)
7980

8081
Additional features:
8182

doc/user/migration.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
$migrations = $client->api('user')->migration()->list([
10+
'per_page' => 10
11+
]);
12+
```
13+
14+
# Start a User Migration
15+
16+
https://docs.github.com/en/rest/migrations/users?apiVersion=2022-11-28#start-a-user-migration
17+
18+
```php
19+
$client->users()->migration()->start([
20+
'repositories' => [
21+
'KnpLabs/php-github-api'
22+
],
23+
'lock_repositories' => true,
24+
'exclude_metadata' => false,
25+
'exclude_git_data' => false,
26+
'exclude_attachments' => true,
27+
'exclude_releases' => false,
28+
'exclude_owner_projects' => true,
29+
'org_metadata_only' => false,
30+
'exclude' => [
31+
'Exclude attributes from the API response to improve performance'
32+
]
33+
]);
34+
```
35+
36+
# Get a User Migration Status
37+
38+
https://docs.github.com/en/rest/migrations/users?apiVersion=2022-11-28#get-a-user-migration-status
39+
40+
```php
41+
$status = $client->user()->migration()->status(12, [
42+
'exclude' => [
43+
'exclude attributes'
44+
]
45+
]);
46+
```
47+
48+
# Delete a User Migration Archive
49+
50+
https://docs.github.com/en/rest/migrations/users?apiVersion=2022-11-28#delete-a-user-migration-archive
51+
52+
```php
53+
$client->user()->migration()->deleteArchive(12);
54+
```
55+
56+
# Unlock a User Repository
57+
58+
https://docs.github.com/en/rest/migrations/users?apiVersion=2022-11-28#unlock-a-user-repository
59+
60+
```php
61+
$client->user()->migration()->unlockRepo(12, 'php-github-api');
62+
```
63+
64+
# List repositories for a User Migration
65+
66+
https://docs.github.com/en/rest/migrations/users?apiVersion=2022-11-28#list-repositories-for-a-user-migration
67+
68+
```php
69+
$repos = $client->user()->migration()->repos(2);
70+
```

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

0 commit comments

Comments
 (0)