Skip to content

Commit 5e81929

Browse files
committed
Projects: add endpoints to manage packages
1 parent d9c15df commit 5e81929

File tree

6 files changed

+331
-26
lines changed

6 files changed

+331
-26
lines changed

Diff for: README.md

+61-1
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,70 @@ $client->projects()->removeTeam($projectName, $teamId);
190190
##### List a projects's packages
191191
```php
192192
$projectName = 'project';
193-
$packages = $client->projects()->listPackages($projectName);
193+
$packages = $client->projects()->packages()->all($projectName);
194194
```
195195
Returns an array of projects packages.
196196

197+
##### Show a project package
198+
```php
199+
$projectName = 'project';
200+
$package = $client->projects()->packages()->show($projectName, 'acme-website/package');
201+
```
202+
Returns the package.
203+
204+
##### Create a vcs package in a project
205+
```php
206+
$projectName = 'project';
207+
$job = $client->projects()->packages()->createVcsPackage($projectName, 'https://github.com/acme-website/package');
208+
```
209+
Returns a new job.
210+
211+
##### Create a vcs package with credentials in a project
212+
```php
213+
$projectName = 'project';
214+
$credentialId = 42;
215+
$job = $client->projects()->packages()->createVcsPackage($projectName,'https://github.com/acme-website/package', $credentialId);
216+
```
217+
Returns a new job.
218+
219+
##### Create a custom package in a project
220+
```php
221+
$projectName = 'project';
222+
$packageDefinition = '{...}'
223+
$job = $client->projects()->packages()->createCustomPackage($projectName, $packageDefinition);
224+
```
225+
Returns a new job.
226+
227+
##### Create a custom package with credentials in a project
228+
```php
229+
$projectName = 'project';
230+
$packageDefinition = '{...}'
231+
$credentialId = 42;
232+
$job = $client->projects()->packages()->createCustomPackage($projectName, $packageDefinition, $credentialId);
233+
```
234+
Returns a new job.
235+
236+
##### Edit a vcs package in a project in a project
237+
```php
238+
$projectName = 'project';
239+
$job = $client->projects()->packages()->editVcsPackage($projectName, 'acme-website/package', 'https://github.com/acme-website/package');
240+
```
241+
Returns a new job.
242+
243+
##### Edit a custom package in a project
244+
```php
245+
$projectName = 'project';
246+
$packageDefinition = '{...}'
247+
$job = $client->projects()->packages()->editCustomPackage($projectName, 'acme-website/package', $packageDefinition);
248+
```
249+
Returns a new job.
250+
251+
##### Delete a package from a project
252+
```php
253+
$projectName = 'project';
254+
$client->projects()->packages()->remove($projectName, 'acme-website/package');
255+
```
256+
197257
##### List a projects's authentication tokens
198258
```php
199259
$projectName = 'project';

Diff for: src/Api/Packages.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ class Packages extends AbstractApi
3838
*/
3939
const ORIGIN_PRIVATE_PROXY = self::ORIGIN_PRIVATE_MIRROR;
4040

41+
const AVAILABLE_ORIGINS = [self::ORIGIN_PUBLIC_MIRROR, self::ORIGIN_PRIVATE_MIRROR, self::ORIGIN_PRIVATE, 'public-proxy', 'private-proxy'];
4142

4243
public function all(array $filters = [])
4344
{
44-
$availableOrigins = [self::ORIGIN_PUBLIC_MIRROR, self::ORIGIN_PRIVATE_MIRROR, self::ORIGIN_PRIVATE, 'public-proxy', 'private-proxy'];
45-
if (isset($filters['origin']) && !in_array($filters['origin'], $availableOrigins, true)) {
46-
throw new InvalidArgumentException('Filter "origin" has to be one of: "' . implode('", "', $availableOrigins) . '".');
45+
if (isset($filters['origin']) && !in_array($filters['origin'], self::AVAILABLE_ORIGINS, true)) {
46+
throw new InvalidArgumentException('Filter "origin" has to be one of: "' . implode('", "', self::AVAILABLE_ORIGINS) . '".');
4747
}
4848

4949
return $this->get('/packages/', $filters);

Diff for: src/Api/Projects.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace PrivatePackagist\ApiClient\Api;
1111

12+
use PrivatePackagist\ApiClient\Api\Projects\MirroredRepositories;
1213
use PrivatePackagist\ApiClient\Exception\InvalidArgumentException;
1314

1415
class Projects extends AbstractApi
@@ -66,9 +67,12 @@ public function removeTeam($projectName, $teamId)
6667
return $this->delete(sprintf('/projects/%s/teams/%s/', $projectName, $teamId));
6768
}
6869

70+
/**
71+
* @deprecated use packages()->all()
72+
*/
6973
public function listPackages($projectName)
7074
{
71-
return $this->get(sprintf('/projects/%s/packages/', $projectName));
75+
return $this->packages()->all($projectName);
7276
}
7377

7478
public function listTokens($projectName)
@@ -95,6 +99,11 @@ public function regenerateToken($projectName, $tokenId, array $confirmation)
9599
return $this->post(sprintf('/projects/%s/tokens/%s/regenerate', $projectName, $tokenId), $confirmation);
96100
}
97101

102+
public function packages()
103+
{
104+
return new Projects\Packages($this->client);
105+
}
106+
98107
public function mirroredRepositories()
99108
{
100109
return new Projects\MirroredRepositories($this->client);

Diff for: src/Api/Projects/Packages.php

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/*
4+
* (c) Packagist Conductors UG (haftungsbeschränkt) <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace PrivatePackagist\ApiClient\Api\Projects;
11+
12+
use PrivatePackagist\ApiClient\Api\AbstractApi;
13+
use PrivatePackagist\ApiClient\Exception\InvalidArgumentException;
14+
15+
class Packages extends AbstractApi
16+
{
17+
public function all($projectName, array $filters = [])
18+
{
19+
if (isset($filters['origin']) && !in_array($filters['origin'], \PrivatePackagist\ApiClient\Api\Packages::AVAILABLE_ORIGINS, true)) {
20+
throw new InvalidArgumentException('Filter "origin" has to be one of: "' . implode('", "', \PrivatePackagist\ApiClient\Api\Packages::AVAILABLE_ORIGINS) . '".');
21+
}
22+
23+
return $this->get(sprintf('/projects/%s/packages/', $projectName), $filters);
24+
}
25+
26+
public function show($projectName, $packageName)
27+
{
28+
return $this->get(sprintf('/projects/%s/packages/%s', $projectName, $packageName));
29+
}
30+
31+
public function createVcsPackage($projectName, $url, $credentialId = null)
32+
{
33+
return $this->post(sprintf('/projects/%s/packages/', $projectName), ['repoType' => 'vcs', 'repoUrl' => $url, 'credentials' => $credentialId]);
34+
}
35+
36+
public function createCustomPackage($projectName, $customJson, $credentialId = null)
37+
{
38+
if (is_array($customJson) || is_object($customJson)) {
39+
$customJson = json_encode($customJson);
40+
}
41+
42+
return $this->post(sprintf('/projects/%s/packages/', $projectName), ['repoType' => 'package', 'repoConfig' => $customJson, 'credentials' => $credentialId]);
43+
}
44+
45+
public function editVcsPackage($projectName, $packageName, $url, $credentialId = null)
46+
{
47+
return $this->put(sprintf('/projects/%s/packages/%s/', $projectName, $packageName), ['repoType' => 'vcs', 'repoUrl' => $url, 'credentials' => $credentialId]);
48+
}
49+
50+
public function editCustomPackage($projectName, $packageName, $customJson, $credentialId = null)
51+
{
52+
return $this->put(sprintf('/projects/%s/packages/%s/', $projectName, $packageName), ['repoType' => 'package', 'repoConfig' => $customJson, 'credentials' => $credentialId]);
53+
}
54+
55+
public function remove($projectName, $packageName)
56+
{
57+
return $this->delete(sprintf('/projects/%s/packages/%s/', $projectName, $packageName));
58+
}
59+
}

Diff for: tests/Api/Projects/PackagesTest.php

+198
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
<?php
2+
3+
/*
4+
* (c) Packagist Conductors UG (haftungsbeschränkt) <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace PrivatePackagist\ApiClient\Api\Projects;
11+
12+
use PrivatePackagist\ApiClient\Api\ApiTestCase;
13+
14+
class PackagesTest extends ApiTestCase
15+
{
16+
public function testAll()
17+
{
18+
$projectName = 'project';
19+
$expected = [
20+
[
21+
'id' => 1,
22+
'name' => 'acme-website/package',
23+
],
24+
];
25+
26+
/** @var Packages&\PHPUnit_Framework_MockObject_MockObject $api */
27+
$api = $this->getApiMock();
28+
$api->expects($this->once())
29+
->method('get')
30+
->with($this->equalTo('/projects/project/packages/'))
31+
->willReturn($expected);
32+
33+
$this->assertSame($expected, $api->all($projectName));
34+
}
35+
36+
public function testAllWithFilters()
37+
{
38+
$projectName = 'project';
39+
$expected = [
40+
[
41+
'id' => 1,
42+
'name' => 'acme-website/package',
43+
],
44+
];
45+
46+
$filters = [
47+
'origin' => \PrivatePackagist\ApiClient\Api\Packages::ORIGIN_PRIVATE,
48+
];
49+
50+
/** @var Packages&\PHPUnit_Framework_MockObject_MockObject $api */
51+
$api = $this->getApiMock();
52+
$api->expects($this->once())
53+
->method('get')
54+
->with($this->equalTo('/projects/project/packages/'), $this->equalTo($filters))
55+
->willReturn($expected);
56+
57+
$this->assertSame($expected, $api->all($projectName, $filters));
58+
}
59+
60+
/**
61+
* @expectedException \PrivatePackagist\ApiClient\Exception\InvalidArgumentException
62+
*/
63+
public function testAllWithInvalidFilters()
64+
{
65+
$projectName = 'project';
66+
$filters = [
67+
'origin' => 'invalid'
68+
];
69+
70+
/** @var Packages&\PHPUnit_Framework_MockObject_MockObject $api */
71+
$api = $this->getApiMock();
72+
$api->expects($this->never())
73+
->method('get');
74+
75+
$api->all($projectName, $filters);
76+
}
77+
78+
public function testShow()
79+
{
80+
$projectName = 'project';
81+
$expected = [
82+
'id' => 1,
83+
'name' => 'acme-website/package',
84+
];
85+
86+
/** @var Packages&\PHPUnit_Framework_MockObject_MockObject $api */
87+
$api = $this->getApiMock();
88+
$api->expects($this->once())
89+
->method('get')
90+
->with($this->equalTo('/projects/project/packages/acme-website/package'))
91+
->willReturn($expected);
92+
93+
$this->assertSame($expected, $api->show($projectName, 'acme-website/package'));
94+
}
95+
96+
public function testCreateVcsPackage()
97+
{
98+
$projectName = 'project';
99+
$expected = [
100+
'id' => 'job-id',
101+
'status' => 'queued',
102+
];
103+
104+
/** @var Packages&\PHPUnit_Framework_MockObject_MockObject $api */
105+
$api = $this->getApiMock();
106+
$api->expects($this->once())
107+
->method('post')
108+
->with($this->equalTo('/projects/project/packages/'), $this->equalTo(['repoType' => 'vcs', 'repoUrl' => 'localhost', 'credentials' => null]))
109+
->willReturn($expected);
110+
111+
$this->assertSame($expected, $api->createVcsPackage($projectName, 'localhost'));
112+
}
113+
114+
/**
115+
* @dataProvider customProvider
116+
*/
117+
public function testCreateCustomPackage($customJson)
118+
{
119+
$projectName = 'project';
120+
$expected = [
121+
'id' => 'job-id',
122+
'status' => 'queued',
123+
];
124+
125+
/** @var Packages&\PHPUnit_Framework_MockObject_MockObject $api */
126+
$api = $this->getApiMock();
127+
$api->expects($this->once())
128+
->method('post')
129+
->with($this->equalTo('/projects/project/packages/'), $this->equalTo(['repoType' => 'package', 'repoConfig' => '{}', 'credentials' => null]))
130+
->willReturn($expected);
131+
132+
$this->assertSame($expected, $api->createCustomPackage($projectName, $customJson));
133+
}
134+
135+
public function customProvider()
136+
{
137+
return [
138+
['{}'],
139+
[new \stdClass()],
140+
];
141+
}
142+
143+
public function testEditVcsPackage()
144+
{
145+
$projectName = 'project';
146+
$expected = [
147+
'id' => 'job-id',
148+
'status' => 'queued',
149+
];
150+
151+
/** @var Packages&\PHPUnit_Framework_MockObject_MockObject $api */
152+
$api = $this->getApiMock();
153+
$api->expects($this->once())
154+
->method('put')
155+
->with($this->equalTo('/projects/project/packages/acme-website/package/'), $this->equalTo(['repoType' => 'vcs', 'repoUrl' => 'localhost', 'credentials' => null]))
156+
->willReturn($expected);
157+
158+
$this->assertSame($expected, $api->editVcsPackage($projectName, 'acme-website/package', 'localhost'));
159+
}
160+
161+
public function testEditCustomPackage()
162+
{
163+
$projectName = 'project';
164+
$expected = [
165+
'id' => 'job-id',
166+
'status' => 'queued',
167+
];
168+
169+
/** @var Packages&\PHPUnit_Framework_MockObject_MockObject $api */
170+
$api = $this->getApiMock();
171+
$api->expects($this->once())
172+
->method('put')
173+
->with($this->equalTo('/projects/project/packages/acme-website/package/'), $this->equalTo(['repoType' => 'package', 'repoConfig' => '{}', 'credentials' => null]))
174+
->willReturn($expected);
175+
176+
$this->assertSame($expected, $api->editCustomPackage($projectName, 'acme-website/package', '{}'));
177+
}
178+
179+
public function testRemove()
180+
{
181+
$projectName = 'project';
182+
$expected = [];
183+
184+
/** @var Packages&\PHPUnit_Framework_MockObject_MockObject $api */
185+
$api = $this->getApiMock();
186+
$api->expects($this->once())
187+
->method('delete')
188+
->with($this->equalTo('/projects/project/packages/acme-website/package/'))
189+
->willReturn($expected);
190+
191+
$this->assertSame($expected, $api->remove($projectName, 'acme-website/package'));
192+
}
193+
194+
protected function getApiClass()
195+
{
196+
return Packages::class;
197+
}
198+
}

0 commit comments

Comments
 (0)