Skip to content

Commit ee0cce5

Browse files
authored
Merge pull request #10 from packagist/t/project-token-endpoints
Project: add token endpoints
2 parents 476c87b + 912bed1 commit ee0cce5

File tree

3 files changed

+175
-37
lines changed

3 files changed

+175
-37
lines changed

Diff for: README.md

+48-12
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ Returns an array of projects.
125125

126126
##### Show a project
127127
```php
128-
$projectId = 42;
129-
$project = $client->projects()->show($projectId);
128+
$projectName = 'project';
129+
$project = $client->projects()->show($projectName);
130130
```
131131
Returns a single project.
132132

@@ -138,45 +138,81 @@ Returns the project.
138138

139139
##### Delete a project
140140
```php
141-
$projectId = 42;
142-
$client->projects()->remove($projectId);
141+
$projectName = 'project';
142+
$client->projects()->remove($projectName);
143143
```
144144

145145
##### List a projects's teams
146146
```php
147-
$projectId = 42;
148-
$teams = $client->projects()->listTeams($projectId);
147+
$projectName = 'project';
148+
$teams = $client->projects()->listTeams($projectName);
149149
```
150150
Returns an array of projects teams.
151151

152152
##### Add a team to a project or update the permission
153153
```php
154-
$projectId = 42;
154+
$projectName = 'project';
155155
$teams = [
156156
[
157157
'id' => 12,
158158
'permission' => 'owner',
159159
],
160160
];
161-
$teams = $client->customers()->addOrUpdateTeams($projectId, $teams);
161+
$teams = $client->customers()->addOrUpdateTeams($projectName, $teams);
162162
```
163163
Returns an array of added project teams.
164164

165165

166166
##### Remove a team from a customer
167167
```php
168-
$projectId = 42;
168+
$projectName = 'project';
169169
$teamId = 12;
170-
$client->customers()->removeTeam($projectId, $teamId);
170+
$client->customers()->removeTeam($projectName, $teamId);
171171
```
172172

173173
##### List a projects's packages
174174
```php
175-
$projectId = 42;
176-
$packages = $client->projects()->listPackages($projectId);
175+
$projectName = 'project';
176+
$packages = $client->projects()->listPackages($projectName);
177177
```
178178
Returns an array of projects packages.
179179

180+
##### List a projects's authentication tokens
181+
```php
182+
$projectName = 'project';
183+
$tokens = $client->projects()->listTokens($projectName);
184+
```
185+
Returns an array of authentication tokens.
186+
187+
##### Create a project authentication token
188+
```php
189+
$projectName = 'project';
190+
$data = [
191+
'description' => 'Project Token',
192+
'access' => 'read',
193+
];
194+
$token = $client->projects()->createToken($projectName, $data);
195+
```
196+
Returns the authentication token.
197+
198+
##### Delete a project authentication token
199+
```php
200+
$projectName = 'project';
201+
$tokenId = 33;
202+
$client->projects()->removeToken($projectName, $tokenId);
203+
```
204+
205+
##### Regenerate a project authentication token
206+
```php
207+
$projectName = 'project';
208+
$tokenId = 33;
209+
$confirmation = [
210+
'IConfirmOldTokenWillStopWorkingImmediately' => true,
211+
];
212+
$token = $client->projects()->regenerateToken($projectName, $confirmation);
213+
```
214+
Returns the authentication token.
215+
180216
#### Package
181217

182218
##### List an organization's packages

Diff for: src/Api/Projects.php

+36-12
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,27 @@ public function all()
1111
return $this->get('/projects/');
1212
}
1313

14-
public function show($projectId)
14+
public function show($projectName)
1515
{
16-
return $this->get(sprintf('/projects/%s/', $projectId));
16+
return $this->get(sprintf('/projects/%s/', $projectName));
1717
}
1818

1919
public function create($name)
2020
{
2121
return $this->post('/projects/', ['name' => $name]);
2222
}
2323

24-
public function remove($projectId)
24+
public function remove($projectName)
2525
{
26-
return $this->delete(sprintf('/projects/%s/', $projectId));
26+
return $this->delete(sprintf('/projects/%s/', $projectName));
2727
}
2828

29-
public function listTeams($projectsId)
29+
public function listTeams($projectsName)
3030
{
31-
return $this->get(sprintf('/projects/%s/teams/', $projectsId));
31+
return $this->get(sprintf('/projects/%s/teams/', $projectsName));
3232
}
3333

34-
public function addOrUpdateTeams($projectId, array $teams)
34+
public function addOrUpdateTeams($projectName, array $teams)
3535
{
3636
foreach ($teams as $team) {
3737
if (!isset($team['id'])) {
@@ -43,16 +43,40 @@ public function addOrUpdateTeams($projectId, array $teams)
4343
}
4444
}
4545

46-
return $this->post(sprintf('/projects/%s/teams/', $projectId), $teams);
46+
return $this->post(sprintf('/projects/%s/teams/', $projectName), $teams);
4747
}
4848

49-
public function removeTeam($projectId, $teamId)
49+
public function removeTeam($projectName, $teamId)
5050
{
51-
return $this->delete(sprintf('/projects/%s/teams/%s/', $projectId, $teamId));
51+
return $this->delete(sprintf('/projects/%s/teams/%s/', $projectName, $teamId));
5252
}
5353

54-
public function listPackages($projectsId)
54+
public function listPackages($projectName)
5555
{
56-
return $this->get(sprintf('/projects/%s/packages/', $projectsId));
56+
return $this->get(sprintf('/projects/%s/packages/', $projectName));
57+
}
58+
59+
public function listTokens($projectName)
60+
{
61+
return $this->get(sprintf('/projects/%s/tokens/', $projectName));
62+
}
63+
64+
public function createToken($projectName, array $tokenData)
65+
{
66+
return $this->post(sprintf('/projects/%s/tokens/', $projectName), $tokenData);
67+
}
68+
69+
public function removeToken($projectName, $tokenId)
70+
{
71+
return $this->delete(sprintf('/projects/%s/tokens/%s/', $projectName, $tokenId));
72+
}
73+
74+
public function regenerateToken($projectName, $tokenId, array $confirmation)
75+
{
76+
if (!isset($confirmation['IConfirmOldTokenWillStopWorkingImmediately'])) {
77+
throw new InvalidArgumentException('Confirmation is required to regenerate the Composer repository token.');
78+
}
79+
80+
return $this->post(sprintf('/projects/%s/tokens/%s/regenerate', $projectName, $tokenId), $confirmation);
5781
}
5882
}

Diff for: tests/Api/ProjectsTest.php

+91-13
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ public function testShow()
2424
{
2525
$expected = $this->getProjectDefinition();
2626

27-
$projectId = 1;
27+
$projectName = 'project';
2828

2929
/** @var Projects&\PHPUnit_Framework_MockObject_MockObject $api */
3030
$api = $this->getApiMock();
3131
$api->expects($this->once())
3232
->method('get')
33-
->with($this->equalTo('/projects/1/'))
33+
->with($this->equalTo('/projects/project/'))
3434
->will($this->returnValue($expected));
3535

36-
$this->assertSame($expected, $api->show($projectId));
36+
$this->assertSame($expected, $api->show($projectName));
3737
}
3838

3939
public function testCreate()
@@ -58,10 +58,10 @@ public function testRemove()
5858
$api = $this->getApiMock();
5959
$api->expects($this->once())
6060
->method('delete')
61-
->with($this->equalTo('/projects/1/'))
61+
->with($this->equalTo('/projects/project/'))
6262
->will($this->returnValue($expected));
6363

64-
$this->assertSame($expected, $api->remove(1));
64+
$this->assertSame($expected, $api->remove('project'));
6565
}
6666

6767
public function testListTeams()
@@ -80,10 +80,10 @@ public function testListTeams()
8080
$api = $this->getApiMock();
8181
$api->expects($this->once())
8282
->method('get')
83-
->with($this->equalTo('/projects/1/teams/'))
83+
->with($this->equalTo('/projects/project/teams/'))
8484
->will($this->returnValue($expected));
8585

86-
$this->assertSame($expected, $api->listTeams(1));
86+
$this->assertSame($expected, $api->listTeams('project'));
8787
}
8888

8989
public function testAddOrUpdateTeam()
@@ -104,10 +104,10 @@ public function testAddOrUpdateTeam()
104104
$api = $this->getApiMock();
105105
$api->expects($this->once())
106106
->method('post')
107-
->with($this->equalTo('/projects/1/teams/'), $this->equalTo($teams))
107+
->with($this->equalTo('/projects/project/teams/'), $this->equalTo($teams))
108108
->will($this->returnValue($expected));
109109

110-
$this->assertSame($expected, $api->addOrUpdateTeams(1, $teams));
110+
$this->assertSame($expected, $api->addOrUpdateTeams('project', $teams));
111111
}
112112

113113
public function testRemoveTeam()
@@ -118,10 +118,10 @@ public function testRemoveTeam()
118118
$api = $this->getApiMock();
119119
$api->expects($this->once())
120120
->method('delete')
121-
->with($this->equalTo('/projects/1/teams/42/'))
121+
->with($this->equalTo('/projects/project/teams/42/'))
122122
->will($this->returnValue($expected));
123123

124-
$this->assertSame($expected, $api->removeTeam(1, 42));
124+
$this->assertSame($expected, $api->removeTeam('project', 42));
125125
}
126126

127127
public function testListPackages()
@@ -139,10 +139,88 @@ public function testListPackages()
139139
$api = $this->getApiMock();
140140
$api->expects($this->once())
141141
->method('get')
142-
->with($this->equalTo('/projects/1/packages/'))
142+
->with($this->equalTo('/projects/project/packages/'))
143143
->will($this->returnValue($expected));
144144

145-
$this->assertSame($expected, $api->listPackages(1));
145+
$this->assertSame($expected, $api->listPackages('project'));
146+
}
147+
148+
public function testListTokens()
149+
{
150+
$expected = [
151+
[
152+
'description' => 'Generated Client Token',
153+
'access' => 'read',
154+
'url' => 'https://vendor-org.repo.packagist.com/acme-websites/',
155+
'user' => 'token',
156+
'token' => 'password',
157+
'lastUsed' => '2018-03-14T11:36:00+00:00'
158+
],
159+
];
160+
161+
/** @var Projects&\PHPUnit_Framework_MockObject_MockObject $api */
162+
$api = $this->getApiMock();
163+
$api->expects($this->once())
164+
->method('get')
165+
->with($this->equalTo('/projects/project/tokens/'))
166+
->will($this->returnValue($expected));
167+
168+
$this->assertSame($expected, $api->listTokens('project'));
169+
}
170+
171+
public function testCreateToken()
172+
{
173+
$expected = [
174+
'description' => 'Project Token',
175+
'access' => 'read',
176+
'url' => 'https://vendor-org.repo.packagist.com/acme-websites/',
177+
'user' => 'token',
178+
'token' => 'password',
179+
'lastUsed' => '2018-03-14T11:36:00+00:00'
180+
];
181+
182+
/** @var Projects&\PHPUnit_Framework_MockObject_MockObject $api */
183+
$api = $this->getApiMock();
184+
$api->expects($this->once())
185+
->method('post')
186+
->with($this->equalTo('/projects/project/tokens/'), $this->equalTo([
187+
'description' => 'Project Token',
188+
'access' => 'read',
189+
]))
190+
->will($this->returnValue($expected));
191+
192+
$this->assertSame($expected, $api->createToken('project', [
193+
'description' => 'Project Token',
194+
'access' => 'read',
195+
]));
196+
}
197+
198+
public function testRemoveToken()
199+
{
200+
$expected = [];
201+
202+
/** @var Projects&\PHPUnit_Framework_MockObject_MockObject $api */
203+
$api = $this->getApiMock();
204+
$api->expects($this->once())
205+
->method('delete')
206+
->with($this->equalTo('/projects/project/tokens/1/'))
207+
->will($this->returnValue($expected));
208+
209+
$this->assertSame($expected, $api->removeToken('project', 1));
210+
}
211+
212+
public function testRegenerateToken()
213+
{
214+
$expected = [];
215+
216+
/** @var Projects&\PHPUnit_Framework_MockObject_MockObject $api */
217+
$api = $this->getApiMock();
218+
$api->expects($this->once())
219+
->method('post')
220+
->with($this->equalTo('/projects/project/tokens/1/regenerate'), $this->equalTo(['IConfirmOldTokenWillStopWorkingImmediately' => true]))
221+
->will($this->returnValue($expected));
222+
223+
$this->assertSame($expected, $api->regenerateToken('project', 1, ['IConfirmOldTokenWillStopWorkingImmediately' => true]));
146224
}
147225

148226
private function getProjectDefinition()

0 commit comments

Comments
 (0)