Skip to content

Commit 099042e

Browse files
authored
Projects: add endpoints to manage mirrored repositories + packages (#24)
Projects: add endpoints to manage mirrored repositories + packages
2 parents d9cfc1b + f433ab7 commit 099042e

9 files changed

+639
-31
lines changed

Diff for: README.md

+128-2
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';
@@ -230,6 +290,72 @@ $token = $client->projects()->regenerateToken($projectName, $confirmation);
230290
```
231291
Returns the authentication token.
232292

293+
##### List a projects's mirrored repositories
294+
```php
295+
$projectName = 'project';
296+
$mirroredRepositories = $client->projects()->mirroredRepositories()->all($projectName);
297+
```
298+
Returns an array of mirrored repositories.
299+
300+
##### Show a mirrored repository
301+
```php
302+
$projectName = 'project';
303+
$mirroredRepositoryId = 42;
304+
$mirroredRepository = $client->projects()->mirroredRepositories()->show($projectName, $mirroredRepositoryId);
305+
```
306+
Returns the mirrored repository.
307+
308+
##### Add mirrored repositories to a project
309+
```php
310+
$projectName = 'project';
311+
$mirroredRepositoriesToAdd = [
312+
['id' => 12, 'mirroringBehavior' => 'add_on_use'],
313+
];
314+
$mirroredRepository = $client->projects()->mirroredRepositories()->add($projectName, $mirroredRepositoriesToAdd);
315+
```
316+
Returns a list of added mirrored repositories.
317+
318+
##### Edit the mirroring behaviour of mirrored repository in a project
319+
```php
320+
$projectName = 'project';
321+
$mirroredRepositoryId = 42;
322+
$mirroredRepository = $client->projects()->mirroredRepositories()->create($projectName, $mirroredRepositoryId, 'add_on_use');
323+
```
324+
Returns the edited mirrored repository.
325+
326+
##### Delete a mirrored repository from a project
327+
```php
328+
$projectName = 'project';
329+
$mirroredRepositoryId = 42;
330+
$client->projects()->mirroredRepositories()->remove($projectName, $mirroredRepositoryId);
331+
```
332+
333+
##### List all mirrored packages from a mirrored repository in a project
334+
```php
335+
$projectName = 'project';
336+
$mirroredRepositoryId = 42;
337+
$packages = $client->projects()->mirroredRepositories()->listPackages($projectName, $mirroredRepositoryId);
338+
```
339+
Returns an array of packages.
340+
341+
##### Add mirrored packages from one mirrored repository to a project
342+
```php
343+
$projectName = 'project';
344+
$mirroredRepositoryId = 42;
345+
$packages = [
346+
'acme/cool-lib
347+
];
348+
$jobs = $client->projects()->mirroredRepositories()->addPackages($projectName, $mirroredRepositoryId, $packages);
349+
```
350+
Returns an array of jobs.
351+
352+
##### Remove all mirrored packages from one mirrored repository in a project
353+
```php
354+
$projectName = 'project';
355+
$mirroredRepositoryId = 42;
356+
$client->projects()->mirroredRepositories()->removePackages($projectName, $mirroredRepositoryId);
357+
```
358+
233359
#### Package
234360

235361
##### List an organization's packages
@@ -348,7 +474,7 @@ Returns an array of mirrored repositories.
348474
$mirroredRepositoryId = 42;
349475
$mirroredRepository = $client->mirroredRepositories()->show($mirroredRepositoryId);
350476
```
351-
Returns the credential.
477+
Returns the mirrored repository.
352478

353479
##### Create a mirrored repository
354480
```php

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

+16-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public function remove($projectName)
3333
return $this->delete(sprintf('/projects/%s/', $projectName));
3434
}
3535

36-
public function listTeams($projectsName)
36+
public function listTeams($projectName)
3737
{
38-
return $this->get(sprintf('/projects/%s/teams/', $projectsName));
38+
return $this->get(sprintf('/projects/%s/teams/', $projectName));
3939
}
4040

4141
/**
@@ -66,9 +66,12 @@ public function removeTeam($projectName, $teamId)
6666
return $this->delete(sprintf('/projects/%s/teams/%s/', $projectName, $teamId));
6767
}
6868

69+
/**
70+
* @deprecated use packages()->all()
71+
*/
6972
public function listPackages($projectName)
7073
{
71-
return $this->get(sprintf('/projects/%s/packages/', $projectName));
74+
return $this->packages()->all($projectName);
7275
}
7376

7477
public function listTokens($projectName)
@@ -94,4 +97,14 @@ public function regenerateToken($projectName, $tokenId, array $confirmation)
9497

9598
return $this->post(sprintf('/projects/%s/tokens/%s/regenerate', $projectName, $tokenId), $confirmation);
9699
}
100+
101+
public function packages()
102+
{
103+
return new Projects\Packages($this->client);
104+
}
105+
106+
public function mirroredRepositories()
107+
{
108+
return new Projects\MirroredRepositories($this->client);
109+
}
97110
}

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

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 MirroredRepositories extends AbstractApi
16+
{
17+
public function all($projectName)
18+
{
19+
return $this->get(sprintf('/projects/%s/mirrored-repositories/', $projectName));
20+
}
21+
22+
public function add($projectName, array $mirroredRepositories)
23+
{
24+
foreach ($mirroredRepositories as $mirroredRepository) {
25+
if (!isset($mirroredRepository['id'], $mirroredRepository['mirroringBehavior'])) {
26+
throw new InvalidArgumentException('The "id" and the "mirroringBehavior" are required to add a mirrored repository to a project');
27+
}
28+
}
29+
30+
return $this->post(sprintf('/projects/%s/mirrored-repositories/', $projectName), $mirroredRepositories);
31+
}
32+
33+
public function show($projectName, $mirroredRepositoryId)
34+
{
35+
return $this->get(sprintf('/projects/%s/mirrored-repositories/%s/', $projectName, $mirroredRepositoryId));
36+
}
37+
38+
public function edit($projectName, $mirroredRepositoryId, $mirroringBehavior)
39+
{
40+
return $this->put(sprintf('/projects/%s/mirrored-repositories/%s/', $projectName, $mirroredRepositoryId), [
41+
'mirroringBehavior' => $mirroringBehavior,
42+
]);
43+
}
44+
45+
public function remove($projectName, $mirroredRepositoryId)
46+
{
47+
return $this->delete(sprintf('/projects/%s/mirrored-repositories/%s/', $projectName, $mirroredRepositoryId));
48+
}
49+
50+
public function listPackages($projectName, $mirroredRepositoryId)
51+
{
52+
return $this->get(sprintf('/projects/%s/mirrored-repositories/%s/packages/', $projectName, $mirroredRepositoryId));
53+
}
54+
55+
public function addPackages($projectName, $mirroredRepositoryId, array $packages)
56+
{
57+
return $this->post(sprintf('/projects/%s/mirrored-repositories/%s/packages/', $projectName, $mirroredRepositoryId), $packages);
58+
}
59+
60+
public function removePackages($projectName, $mirroredRepositoryId)
61+
{
62+
return $this->delete(sprintf('/projects/%s/mirrored-repositories/%s/packages/', $projectName, $mirroredRepositoryId));
63+
}
64+
}

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/MirroredRepositoriesTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function testShow()
4444
public function testCreate()
4545
{
4646
$expected = $this->getMirroredRepositoryDefinition();
47-
$data = json_decode( json_encode($expected), true);
47+
$data = json_decode(json_encode($expected), true);
4848
unset($data['id']);
4949

5050
/** @var MirroredRepositories&\PHPUnit_Framework_MockObject_MockObject $api */
@@ -60,7 +60,7 @@ public function testCreate()
6060
public function testEdit()
6161
{
6262
$expected = $this->getMirroredRepositoryDefinition();
63-
$data = json_decode( json_encode($expected), true);
63+
$data = json_decode(json_encode($expected), true);
6464
unset($data['id']);
6565

6666
/** @var MirroredRepositories&\PHPUnit_Framework_MockObject_MockObject $api */

0 commit comments

Comments
 (0)