Skip to content

Commit c50614e

Browse files
authoredMar 7, 2024··
Merge pull request #75 from packagist/package-subrepository-access
Packages: add support for setting the default subrepository access
2 parents 72340b1 + 3d59933 commit c50614e

File tree

6 files changed

+234
-37
lines changed

6 files changed

+234
-37
lines changed
 

‎src/Api/Packages.php

+27-16
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
use PrivatePackagist\ApiClient\Api\Packages\Artifacts;
1313
use PrivatePackagist\ApiClient\Exception\InvalidArgumentException;
14+
use PrivatePackagist\ApiClient\Payload\ArtifactPackageConfig;
15+
use PrivatePackagist\ApiClient\Payload\CustomPackageConfig;
16+
use PrivatePackagist\ApiClient\Payload\VcsPackageConfig;
1417

1518
class Packages extends AbstractApi
1619
{
@@ -55,23 +58,25 @@ public function show($packageName)
5558
return $this->get(sprintf('/packages/%s/', $packageName));
5659
}
5760

58-
public function createVcsPackage($url, $credentialId = null, $type = 'vcs')
61+
public function createVcsPackage($url, $credentialId = null, $type = 'vcs', $defaultSubrepositoryAccess = null)
5962
{
60-
return $this->post('/packages/', ['repoType' => $type, 'repoUrl' => $url, 'credentials' => $credentialId]);
63+
$data = new VcsPackageConfig($url, $credentialId, $type, $defaultSubrepositoryAccess);
64+
65+
return $this->post('/packages/', $data->toParameters());
6166
}
6267

63-
public function createCustomPackage($customJson, $credentialId = null)
68+
public function createCustomPackage($customJson, $credentialId = null, $defaultSubrepositoryAccess = null)
6469
{
65-
if (is_array($customJson) || is_object($customJson)) {
66-
$customJson = json_encode($customJson);
67-
}
70+
$data = new CustomPackageConfig($customJson, $credentialId, $defaultSubrepositoryAccess);
6871

69-
return $this->post('/packages/', ['repoType' => 'package', 'repoConfig' => $customJson, 'credentials' => $credentialId]);
72+
return $this->post('/packages/', $data->toParameters());
7073
}
7174

72-
public function createArtifactPackage(array $artifactPackageFileIds)
75+
public function createArtifactPackage(array $artifactPackageFileIds, $defaultSubrepositoryAccess = null)
7376
{
74-
return $this->post('/packages/', ['repoType' => 'artifact', 'artifactIds' => $artifactPackageFileIds]);
77+
$data = new ArtifactPackageConfig($artifactPackageFileIds, $defaultSubrepositoryAccess);
78+
79+
return $this->post('/packages/', $data->toParameters());
7580
}
7681

7782
/**
@@ -82,27 +87,33 @@ public function updateVcsPackage($packageName, $url, $credentialId = null)
8287
return $this->editVcsPackage($packageName, $url, $credentialId);
8388
}
8489

85-
public function editVcsPackage($packageName, $url, $credentialId = null, $type = 'vcs')
90+
public function editVcsPackage($packageName, $url, $credentialId = null, $type = 'vcs', $defaultSubrepositoryAccess = null)
8691
{
87-
return $this->put(sprintf('/packages/%s/', $packageName), ['repoType' => $type, 'repoUrl' => $url, 'credentials' => $credentialId]);
92+
$data = new VcsPackageConfig($url, $credentialId, $type, $defaultSubrepositoryAccess);
93+
94+
return $this->put(sprintf('/packages/%s/', $packageName), $data->toParameters());
8895
}
8996

90-
public function editArtifactPackage($packageName, array $artifactPackageFileIds)
97+
public function editArtifactPackage($packageName, array $artifactPackageFileIds, $defaultSubrepositoryAccess = null)
9198
{
92-
return $this->put(sprintf('/packages/%s/', $packageName), ['repoType' => 'artifact', 'artifactIds' => $artifactPackageFileIds]);
99+
$data = new ArtifactPackageConfig($artifactPackageFileIds, $defaultSubrepositoryAccess);
100+
101+
return $this->put(sprintf('/packages/%s/', $packageName), $data->toParameters());
93102
}
94103

95104
/**
96105
* @deprecated Use editCustomPackage instead
97106
*/
98107
public function updateCustomPackage($packageName, $customJson, $credentialId = null)
99108
{
100-
return $this->editVcsPackage($packageName, $customJson, $credentialId);
109+
return $this->editCustomPackage($packageName, $customJson, $credentialId);
101110
}
102111

103-
public function editCustomPackage($packageName, $customJson, $credentialId = null)
112+
public function editCustomPackage($packageName, $customJson, $credentialId = null, $defaultSubrepositoryAccess = null)
104113
{
105-
return $this->put(sprintf('/packages/%s/', $packageName), ['repoType' => 'package', 'repoConfig' => $customJson, 'credentials' => $credentialId]);
114+
$data = new CustomPackageConfig($customJson, $credentialId, $defaultSubrepositoryAccess);
115+
116+
return $this->put(sprintf('/packages/%s/', $packageName), $data->toParameters());
106117
}
107118

108119
public function remove($packageName)

‎src/Api/Subrepositories/Packages.php

+17-11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
use PrivatePackagist\ApiClient\Api\AbstractApi;
1313
use PrivatePackagist\ApiClient\Exception\InvalidArgumentException;
14+
use PrivatePackagist\ApiClient\Payload\CustomPackageConfig;
15+
use PrivatePackagist\ApiClient\Payload\VcsPackageConfig;
1416

1517
class Packages extends AbstractApi
1618
{
@@ -28,28 +30,32 @@ public function show($subrepositoryName, $packageName)
2830
return $this->get(sprintf('/subrepositories/%s/packages/%s', $subrepositoryName, $packageName));
2931
}
3032

31-
public function createVcsPackage($subrepositoryName, $url, $credentialId = null, $type = 'vcs')
33+
public function createVcsPackage($subrepositoryName, $url, $credentialId = null, $type = 'vcs', $defaultSubrepositoryAccess = null)
3234
{
33-
return $this->post(sprintf('/subrepositories/%s/packages/', $subrepositoryName), ['repoType' => $type, 'repoUrl' => $url, 'credentials' => $credentialId]);
35+
$data = new VcsPackageConfig($url, $credentialId, $type, $defaultSubrepositoryAccess);
36+
37+
return $this->post(sprintf('/subrepositories/%s/packages/', $subrepositoryName), $data->toParameters());
3438
}
3539

36-
public function createCustomPackage($subrepositoryName, $customJson, $credentialId = null)
40+
public function createCustomPackage($subrepositoryName, $customJson, $credentialId = null, $defaultSubrepositoryAccess = null)
3741
{
38-
if (is_array($customJson) || is_object($customJson)) {
39-
$customJson = json_encode($customJson);
40-
}
42+
$data = new CustomPackageConfig($customJson, $credentialId, $defaultSubrepositoryAccess);
4143

42-
return $this->post(sprintf('/subrepositories/%s/packages/', $subrepositoryName), ['repoType' => 'package', 'repoConfig' => $customJson, 'credentials' => $credentialId]);
44+
return $this->post(sprintf('/subrepositories/%s/packages/', $subrepositoryName), $data->toParameters());
4345
}
4446

45-
public function editVcsPackage($subrepositoryName, $packageName, $url, $credentialId = null, $type = 'vcs')
47+
public function editVcsPackage($subrepositoryName, $packageName, $url, $credentialId = null, $type = 'vcs', $defaultSubrepositoryAccess = null)
4648
{
47-
return $this->put(sprintf('/subrepositories/%s/packages/%s/', $subrepositoryName, $packageName), ['repoType' => $type, 'repoUrl' => $url, 'credentials' => $credentialId]);
49+
$data = new VcsPackageConfig($url, $credentialId, $type, $defaultSubrepositoryAccess);
50+
51+
return $this->put(sprintf('/subrepositories/%s/packages/%s/', $subrepositoryName, $packageName), $data->toParameters());
4852
}
4953

50-
public function editCustomPackage($subrepositoryName, $packageName, $customJson, $credentialId = null)
54+
public function editCustomPackage($subrepositoryName, $packageName, $customJson, $credentialId = null, $defaultSubrepositoryAccess = null)
5155
{
52-
return $this->put(sprintf('/subrepositories/%s/packages/%s/', $subrepositoryName, $packageName), ['repoType' => 'package', 'repoConfig' => $customJson, 'credentials' => $credentialId]);
56+
$data = new CustomPackageConfig($customJson, $credentialId, $defaultSubrepositoryAccess);
57+
58+
return $this->put(sprintf('/subrepositories/%s/packages/%s/', $subrepositoryName, $packageName), $data->toParameters());
5359
}
5460

5561
public function remove($subrepositoryName, $packageName)

‎src/Payload/ArtifactPackageConfig.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace PrivatePackagist\ApiClient\Payload;
4+
5+
/**
6+
* @internal
7+
* @final
8+
*/
9+
class ArtifactPackageConfig
10+
{
11+
/** @var int[] */
12+
private $artifactPackageFileIds;
13+
/** @var ?string */
14+
private $defaultSubrepositoryAccess;
15+
16+
/**
17+
* @param int[] $artifactPackageFileIds
18+
* @param ?string $defaultSubrepositoryAccess
19+
*/
20+
public function __construct(array $artifactPackageFileIds, $defaultSubrepositoryAccess)
21+
{
22+
$this->artifactPackageFileIds = $artifactPackageFileIds;
23+
$this->defaultSubrepositoryAccess = $defaultSubrepositoryAccess;
24+
}
25+
26+
/**
27+
* @return array{repoType: string, artifactIds: int[], defaultSubrepositoryAccess?: string}
28+
*/
29+
public function toParameters(): array
30+
{
31+
$data = [
32+
'repoType' => 'artifact',
33+
'artifactIds' => $this->artifactPackageFileIds,
34+
];
35+
36+
if ($this->defaultSubrepositoryAccess) {
37+
$data['defaultSubrepositoryAccess'] = $this->defaultSubrepositoryAccess;
38+
}
39+
40+
return $data;
41+
}
42+
}

‎src/Payload/CustomPackageConfig.php

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace PrivatePackagist\ApiClient\Payload;
4+
5+
/**
6+
* @internal
7+
* @final
8+
*/
9+
class CustomPackageConfig
10+
{
11+
/** @var string */
12+
private $customJson;
13+
/** @var ?int */
14+
private $credentialId;
15+
/** @var ?string */
16+
private $defaultSubrepositoryAccess;
17+
18+
/**
19+
* @param string|array|object $customJson
20+
* @param ?int $credentialId
21+
* @param ?string $defaultSubrepositoryAccess
22+
*/
23+
public function __construct($customJson, $credentialId, $defaultSubrepositoryAccess)
24+
{
25+
if (is_array($customJson) || is_object($customJson)) {
26+
$customJson = json_encode($customJson);
27+
}
28+
29+
$this->customJson = $customJson;
30+
$this->credentialId = $credentialId;
31+
$this->defaultSubrepositoryAccess = $defaultSubrepositoryAccess;
32+
}
33+
34+
/**
35+
* @return array{repoType: string, repoConfig: string, credentials: ?int, defaultSubrepositoryAccess?: string}
36+
*/
37+
public function toParameters(): array
38+
{
39+
$data = [
40+
'repoType' => 'package',
41+
'repoConfig' => $this->customJson,
42+
'credentials' => $this->credentialId,
43+
];
44+
45+
if ($this->defaultSubrepositoryAccess) {
46+
$data['defaultSubrepositoryAccess'] = $this->defaultSubrepositoryAccess;
47+
}
48+
49+
return $data;
50+
}
51+
}

‎src/Payload/VcsPackageConfig.php

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace PrivatePackagist\ApiClient\Payload;
4+
5+
/**
6+
* @internal
7+
* @final
8+
*/
9+
class VcsPackageConfig
10+
{
11+
/** @var string */
12+
private $url;
13+
/** @var ?int */
14+
private $credentialId;
15+
/** @var string */
16+
private $type;
17+
/** @var ?string */
18+
private $defaultSubrepositoryAccess;
19+
20+
/**
21+
* @param string $url
22+
* @param ?int $credentialId
23+
* @param string $type
24+
* @param ?string $defaultSubrepositoryAccess
25+
*/
26+
public function __construct($url, $credentialId, $type, $defaultSubrepositoryAccess)
27+
{
28+
$this->url = $url;
29+
$this->credentialId = $credentialId;
30+
$this->type = $type;
31+
$this->defaultSubrepositoryAccess = $defaultSubrepositoryAccess;
32+
}
33+
34+
/**
35+
* @return array{repoType: string, repoUrl: string, credentials: ?int, defaultSubrepositoryAccess?: string}
36+
*/
37+
public function toParameters(): array
38+
{
39+
$data = [
40+
'repoType' => $this->type,
41+
'repoUrl' => $this->url,
42+
'credentials' => $this->credentialId,
43+
];
44+
45+
if ($this->defaultSubrepositoryAccess) {
46+
$data['defaultSubrepositoryAccess'] = $this->defaultSubrepositoryAccess;
47+
}
48+
49+
return $data;
50+
}
51+
}

‎tests/Api/PackagesTest.php

+46-10
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,27 @@ public function testCreateVcsPackage()
106106
$this->assertSame($expected, $api->createVcsPackage('localhost'));
107107
}
108108

109+
public function testCreateVcsPackageWithDefaultSubrepositoryAccess()
110+
{
111+
$expected = [
112+
'id' => 'job-id',
113+
'status' => 'queued',
114+
];
115+
116+
/** @var Packages&MockObject $api */
117+
$api = $this->getApiMock();
118+
$api->expects($this->once())
119+
->method('post')
120+
->with($this->equalTo('/packages/'), $this->equalTo(['repoType' => 'vcs', 'repoUrl' => 'localhost', 'credentials' => null, 'defaultSubrepositoryAccess' => 'no-access']))
121+
->willReturn($expected);
122+
123+
$this->assertSame($expected, $api->createVcsPackage('localhost', null, 'vcs', 'no-access'));
124+
}
125+
109126
/**
110127
* @dataProvider customProvider
111128
*/
112-
public function testCreateCustomPackage($customJson)
129+
public function testCreateCustomPackage($customJson, $defaultSubrepositoryAccess, array $expectedPayload)
113130
{
114131
$expected = [
115132
'id' => 'job-id',
@@ -120,13 +137,23 @@ public function testCreateCustomPackage($customJson)
120137
$api = $this->getApiMock();
121138
$api->expects($this->once())
122139
->method('post')
123-
->with($this->equalTo('/packages/'), $this->equalTo(['repoType' => 'package', 'repoConfig' => '{}', 'credentials' => null]))
140+
->with($this->equalTo('/packages/'), $this->equalTo($expectedPayload))
124141
->willReturn($expected);
125142

126-
$this->assertSame($expected, $api->createCustomPackage($customJson));
143+
$this->assertSame($expected, $api->createCustomPackage($customJson, null, $defaultSubrepositoryAccess));
127144
}
128145

129-
public function testCreateArtifactPackage()
146+
public function customProvider()
147+
{
148+
return [
149+
['{}', null, ['repoType' => 'package', 'repoConfig' => '{}', 'credentials' => null]],
150+
[new \stdClass(), null, ['repoType' => 'package', 'repoConfig' => '{}', 'credentials' => null]],
151+
[[], null, ['repoType' => 'package', 'repoConfig' => '[]', 'credentials' => null]],
152+
['{}', 'no-access', ['repoType' => 'package', 'repoConfig' => '{}', 'credentials' => null, 'defaultSubrepositoryAccess' => 'no-access']],
153+
];
154+
}
155+
156+
public function testCreateArtifactPackageWithDefaultSubrepositoryAccess()
130157
{
131158
$expected = [
132159
'id' => 'job-id',
@@ -137,18 +164,27 @@ public function testCreateArtifactPackage()
137164
$api = $this->getApiMock();
138165
$api->expects($this->once())
139166
->method('post')
140-
->with($this->equalTo('/packages/'), $this->equalTo(['repoType' => 'artifact', 'artifactIds' => [42]]))
167+
->with($this->equalTo('/packages/'), $this->equalTo(['repoType' => 'artifact', 'artifactIds' => [42], 'defaultSubrepositoryAccess' => 'no-access']))
141168
->willReturn($expected);
142169

143-
$this->assertSame($expected, $api->createArtifactPackage([42]));
170+
$this->assertSame($expected, $api->createArtifactPackage([42], 'no-access'));
144171
}
145172

146-
public function customProvider()
173+
public function testCreateArtifactPackage()
147174
{
148-
return [
149-
['{}'],
150-
[new \stdClass()],
175+
$expected = [
176+
'id' => 'job-id',
177+
'status' => 'queued',
151178
];
179+
180+
/** @var Packages&MockObject $api */
181+
$api = $this->getApiMock();
182+
$api->expects($this->once())
183+
->method('post')
184+
->with($this->equalTo('/packages/'), $this->equalTo(['repoType' => 'artifact', 'artifactIds' => [42]]))
185+
->willReturn($expected);
186+
187+
$this->assertSame($expected, $api->createArtifactPackage([42]));
152188
}
153189

154190
public function testEditVcsPackage()

0 commit comments

Comments
 (0)
Please sign in to comment.