Skip to content

Commit

Permalink
Merge pull request #75 from packagist/package-subrepository-access
Browse files Browse the repository at this point in the history
Packages: add support for setting the default subrepository access
  • Loading branch information
pscheit committed Mar 7, 2024
2 parents 72340b1 + 3d59933 commit c50614e
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 37 deletions.
43 changes: 27 additions & 16 deletions src/Api/Packages.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

use PrivatePackagist\ApiClient\Api\Packages\Artifacts;
use PrivatePackagist\ApiClient\Exception\InvalidArgumentException;
use PrivatePackagist\ApiClient\Payload\ArtifactPackageConfig;
use PrivatePackagist\ApiClient\Payload\CustomPackageConfig;
use PrivatePackagist\ApiClient\Payload\VcsPackageConfig;

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

public function createVcsPackage($url, $credentialId = null, $type = 'vcs')
public function createVcsPackage($url, $credentialId = null, $type = 'vcs', $defaultSubrepositoryAccess = null)
{
return $this->post('/packages/', ['repoType' => $type, 'repoUrl' => $url, 'credentials' => $credentialId]);
$data = new VcsPackageConfig($url, $credentialId, $type, $defaultSubrepositoryAccess);

return $this->post('/packages/', $data->toParameters());
}

public function createCustomPackage($customJson, $credentialId = null)
public function createCustomPackage($customJson, $credentialId = null, $defaultSubrepositoryAccess = null)
{
if (is_array($customJson) || is_object($customJson)) {
$customJson = json_encode($customJson);
}
$data = new CustomPackageConfig($customJson, $credentialId, $defaultSubrepositoryAccess);

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

public function createArtifactPackage(array $artifactPackageFileIds)
public function createArtifactPackage(array $artifactPackageFileIds, $defaultSubrepositoryAccess = null)
{
return $this->post('/packages/', ['repoType' => 'artifact', 'artifactIds' => $artifactPackageFileIds]);
$data = new ArtifactPackageConfig($artifactPackageFileIds, $defaultSubrepositoryAccess);

return $this->post('/packages/', $data->toParameters());
}

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

public function editVcsPackage($packageName, $url, $credentialId = null, $type = 'vcs')
public function editVcsPackage($packageName, $url, $credentialId = null, $type = 'vcs', $defaultSubrepositoryAccess = null)
{
return $this->put(sprintf('/packages/%s/', $packageName), ['repoType' => $type, 'repoUrl' => $url, 'credentials' => $credentialId]);
$data = new VcsPackageConfig($url, $credentialId, $type, $defaultSubrepositoryAccess);

return $this->put(sprintf('/packages/%s/', $packageName), $data->toParameters());
}

public function editArtifactPackage($packageName, array $artifactPackageFileIds)
public function editArtifactPackage($packageName, array $artifactPackageFileIds, $defaultSubrepositoryAccess = null)
{
return $this->put(sprintf('/packages/%s/', $packageName), ['repoType' => 'artifact', 'artifactIds' => $artifactPackageFileIds]);
$data = new ArtifactPackageConfig($artifactPackageFileIds, $defaultSubrepositoryAccess);

return $this->put(sprintf('/packages/%s/', $packageName), $data->toParameters());
}

/**
* @deprecated Use editCustomPackage instead
*/
public function updateCustomPackage($packageName, $customJson, $credentialId = null)
{
return $this->editVcsPackage($packageName, $customJson, $credentialId);
return $this->editCustomPackage($packageName, $customJson, $credentialId);
}

public function editCustomPackage($packageName, $customJson, $credentialId = null)
public function editCustomPackage($packageName, $customJson, $credentialId = null, $defaultSubrepositoryAccess = null)
{
return $this->put(sprintf('/packages/%s/', $packageName), ['repoType' => 'package', 'repoConfig' => $customJson, 'credentials' => $credentialId]);
$data = new CustomPackageConfig($customJson, $credentialId, $defaultSubrepositoryAccess);

return $this->put(sprintf('/packages/%s/', $packageName), $data->toParameters());
}

public function remove($packageName)
Expand Down
28 changes: 17 additions & 11 deletions src/Api/Subrepositories/Packages.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

use PrivatePackagist\ApiClient\Api\AbstractApi;
use PrivatePackagist\ApiClient\Exception\InvalidArgumentException;
use PrivatePackagist\ApiClient\Payload\CustomPackageConfig;
use PrivatePackagist\ApiClient\Payload\VcsPackageConfig;

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

public function createVcsPackage($subrepositoryName, $url, $credentialId = null, $type = 'vcs')
public function createVcsPackage($subrepositoryName, $url, $credentialId = null, $type = 'vcs', $defaultSubrepositoryAccess = null)
{
return $this->post(sprintf('/subrepositories/%s/packages/', $subrepositoryName), ['repoType' => $type, 'repoUrl' => $url, 'credentials' => $credentialId]);
$data = new VcsPackageConfig($url, $credentialId, $type, $defaultSubrepositoryAccess);

return $this->post(sprintf('/subrepositories/%s/packages/', $subrepositoryName), $data->toParameters());
}

public function createCustomPackage($subrepositoryName, $customJson, $credentialId = null)
public function createCustomPackage($subrepositoryName, $customJson, $credentialId = null, $defaultSubrepositoryAccess = null)
{
if (is_array($customJson) || is_object($customJson)) {
$customJson = json_encode($customJson);
}
$data = new CustomPackageConfig($customJson, $credentialId, $defaultSubrepositoryAccess);

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

public function editVcsPackage($subrepositoryName, $packageName, $url, $credentialId = null, $type = 'vcs')
public function editVcsPackage($subrepositoryName, $packageName, $url, $credentialId = null, $type = 'vcs', $defaultSubrepositoryAccess = null)
{
return $this->put(sprintf('/subrepositories/%s/packages/%s/', $subrepositoryName, $packageName), ['repoType' => $type, 'repoUrl' => $url, 'credentials' => $credentialId]);
$data = new VcsPackageConfig($url, $credentialId, $type, $defaultSubrepositoryAccess);

return $this->put(sprintf('/subrepositories/%s/packages/%s/', $subrepositoryName, $packageName), $data->toParameters());
}

public function editCustomPackage($subrepositoryName, $packageName, $customJson, $credentialId = null)
public function editCustomPackage($subrepositoryName, $packageName, $customJson, $credentialId = null, $defaultSubrepositoryAccess = null)
{
return $this->put(sprintf('/subrepositories/%s/packages/%s/', $subrepositoryName, $packageName), ['repoType' => 'package', 'repoConfig' => $customJson, 'credentials' => $credentialId]);
$data = new CustomPackageConfig($customJson, $credentialId, $defaultSubrepositoryAccess);

return $this->put(sprintf('/subrepositories/%s/packages/%s/', $subrepositoryName, $packageName), $data->toParameters());
}

public function remove($subrepositoryName, $packageName)
Expand Down
42 changes: 42 additions & 0 deletions src/Payload/ArtifactPackageConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types=1);

namespace PrivatePackagist\ApiClient\Payload;

/**
* @internal
* @final
*/
class ArtifactPackageConfig
{
/** @var int[] */
private $artifactPackageFileIds;
/** @var ?string */
private $defaultSubrepositoryAccess;

/**
* @param int[] $artifactPackageFileIds
* @param ?string $defaultSubrepositoryAccess
*/
public function __construct(array $artifactPackageFileIds, $defaultSubrepositoryAccess)
{
$this->artifactPackageFileIds = $artifactPackageFileIds;
$this->defaultSubrepositoryAccess = $defaultSubrepositoryAccess;
}

/**
* @return array{repoType: string, artifactIds: int[], defaultSubrepositoryAccess?: string}
*/
public function toParameters(): array
{
$data = [
'repoType' => 'artifact',
'artifactIds' => $this->artifactPackageFileIds,
];

if ($this->defaultSubrepositoryAccess) {
$data['defaultSubrepositoryAccess'] = $this->defaultSubrepositoryAccess;
}

return $data;
}
}
51 changes: 51 additions & 0 deletions src/Payload/CustomPackageConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php declare(strict_types=1);

namespace PrivatePackagist\ApiClient\Payload;

/**
* @internal
* @final
*/
class CustomPackageConfig
{
/** @var string */
private $customJson;
/** @var ?int */
private $credentialId;
/** @var ?string */
private $defaultSubrepositoryAccess;

/**
* @param string|array|object $customJson
* @param ?int $credentialId
* @param ?string $defaultSubrepositoryAccess
*/
public function __construct($customJson, $credentialId, $defaultSubrepositoryAccess)
{
if (is_array($customJson) || is_object($customJson)) {
$customJson = json_encode($customJson);
}

$this->customJson = $customJson;
$this->credentialId = $credentialId;
$this->defaultSubrepositoryAccess = $defaultSubrepositoryAccess;
}

/**
* @return array{repoType: string, repoConfig: string, credentials: ?int, defaultSubrepositoryAccess?: string}
*/
public function toParameters(): array
{
$data = [
'repoType' => 'package',
'repoConfig' => $this->customJson,
'credentials' => $this->credentialId,
];

if ($this->defaultSubrepositoryAccess) {
$data['defaultSubrepositoryAccess'] = $this->defaultSubrepositoryAccess;
}

return $data;
}
}
51 changes: 51 additions & 0 deletions src/Payload/VcsPackageConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php declare(strict_types=1);

namespace PrivatePackagist\ApiClient\Payload;

/**
* @internal
* @final
*/
class VcsPackageConfig
{
/** @var string */
private $url;
/** @var ?int */
private $credentialId;
/** @var string */
private $type;
/** @var ?string */
private $defaultSubrepositoryAccess;

/**
* @param string $url
* @param ?int $credentialId
* @param string $type
* @param ?string $defaultSubrepositoryAccess
*/
public function __construct($url, $credentialId, $type, $defaultSubrepositoryAccess)
{
$this->url = $url;
$this->credentialId = $credentialId;
$this->type = $type;
$this->defaultSubrepositoryAccess = $defaultSubrepositoryAccess;
}

/**
* @return array{repoType: string, repoUrl: string, credentials: ?int, defaultSubrepositoryAccess?: string}
*/
public function toParameters(): array
{
$data = [
'repoType' => $this->type,
'repoUrl' => $this->url,
'credentials' => $this->credentialId,
];

if ($this->defaultSubrepositoryAccess) {
$data['defaultSubrepositoryAccess'] = $this->defaultSubrepositoryAccess;
}

return $data;
}
}
56 changes: 46 additions & 10 deletions tests/Api/PackagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,27 @@ public function testCreateVcsPackage()
$this->assertSame($expected, $api->createVcsPackage('localhost'));
}

public function testCreateVcsPackageWithDefaultSubrepositoryAccess()
{
$expected = [
'id' => 'job-id',
'status' => 'queued',
];

/** @var Packages&MockObject $api */
$api = $this->getApiMock();
$api->expects($this->once())
->method('post')
->with($this->equalTo('/packages/'), $this->equalTo(['repoType' => 'vcs', 'repoUrl' => 'localhost', 'credentials' => null, 'defaultSubrepositoryAccess' => 'no-access']))
->willReturn($expected);

$this->assertSame($expected, $api->createVcsPackage('localhost', null, 'vcs', 'no-access'));
}

/**
* @dataProvider customProvider
*/
public function testCreateCustomPackage($customJson)
public function testCreateCustomPackage($customJson, $defaultSubrepositoryAccess, array $expectedPayload)
{
$expected = [
'id' => 'job-id',
Expand All @@ -120,13 +137,23 @@ public function testCreateCustomPackage($customJson)
$api = $this->getApiMock();
$api->expects($this->once())
->method('post')
->with($this->equalTo('/packages/'), $this->equalTo(['repoType' => 'package', 'repoConfig' => '{}', 'credentials' => null]))
->with($this->equalTo('/packages/'), $this->equalTo($expectedPayload))
->willReturn($expected);

$this->assertSame($expected, $api->createCustomPackage($customJson));
$this->assertSame($expected, $api->createCustomPackage($customJson, null, $defaultSubrepositoryAccess));
}

public function testCreateArtifactPackage()
public function customProvider()
{
return [
['{}', null, ['repoType' => 'package', 'repoConfig' => '{}', 'credentials' => null]],
[new \stdClass(), null, ['repoType' => 'package', 'repoConfig' => '{}', 'credentials' => null]],
[[], null, ['repoType' => 'package', 'repoConfig' => '[]', 'credentials' => null]],
['{}', 'no-access', ['repoType' => 'package', 'repoConfig' => '{}', 'credentials' => null, 'defaultSubrepositoryAccess' => 'no-access']],
];
}

public function testCreateArtifactPackageWithDefaultSubrepositoryAccess()
{
$expected = [
'id' => 'job-id',
Expand All @@ -137,18 +164,27 @@ public function testCreateArtifactPackage()
$api = $this->getApiMock();
$api->expects($this->once())
->method('post')
->with($this->equalTo('/packages/'), $this->equalTo(['repoType' => 'artifact', 'artifactIds' => [42]]))
->with($this->equalTo('/packages/'), $this->equalTo(['repoType' => 'artifact', 'artifactIds' => [42], 'defaultSubrepositoryAccess' => 'no-access']))
->willReturn($expected);

$this->assertSame($expected, $api->createArtifactPackage([42]));
$this->assertSame($expected, $api->createArtifactPackage([42], 'no-access'));
}

public function customProvider()
public function testCreateArtifactPackage()
{
return [
['{}'],
[new \stdClass()],
$expected = [
'id' => 'job-id',
'status' => 'queued',
];

/** @var Packages&MockObject $api */
$api = $this->getApiMock();
$api->expects($this->once())
->method('post')
->with($this->equalTo('/packages/'), $this->equalTo(['repoType' => 'artifact', 'artifactIds' => [42]]))
->willReturn($expected);

$this->assertSame($expected, $api->createArtifactPackage([42]));
}

public function testEditVcsPackage()
Expand Down

0 comments on commit c50614e

Please sign in to comment.