Skip to content

Commit 29d7dbd

Browse files
authored
Merge pull request #37 from packagist/t/artifact-uploads-api
Add package artifact upload API
2 parents 749eaff + 9836980 commit 29d7dbd

File tree

8 files changed

+210
-2
lines changed

8 files changed

+210
-2
lines changed

README.md

+28-1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@
7070
* [Delete a package](#delete-a-package)
7171
* [List all dependents of a package](#list-all-dependents-of-a-package)
7272
* [List all customers with access to a package](#list-all-customers-with-access-to-a-package)
73+
* [Create an artifact package file](#create-an-artifact-package-file)
74+
* [Create an artifact package](#create-an-artifact-package)
75+
* [Update artifact files of a package](#update-artifact-files-of-a-package)
7376
* [Credential](#credential)
7477
* [List an organization's credentials](#list-an-organizations-credentials)
7578
* [Show a credential](#show-a-credential)
@@ -95,7 +98,7 @@
9598
* [Validate incoming webhook payloads](#validate-incoming-webhook-payloads)
9699
* [License](#license)
97100

98-
<!-- Added by: glaubinix, at: Mon 6 Jul 2020 14:07:03 BST -->
101+
<!-- Added by: wissem, at: Tue Jul 21 10:32:47 CEST 2020 -->
99102

100103
<!--te-->
101104

@@ -577,6 +580,30 @@ $client->packages()->listCustomers('acme-website/package');
577580
```
578581
Returns a list of customers with access to the package.
579582

583+
#### Create an artifact package file
584+
585+
```php
586+
$fileName = 'package1.zip'; // your package archive artifact containing a valid composer.json in root directory
587+
$file = file_get_contents($fileName);
588+
$client->packages()->artifacts()->create($file, 'application/zip', $fileName);
589+
```
590+
591+
#### Create an artifact package
592+
593+
```php
594+
$fileName = 'package1.zip';
595+
$file = file_get_contents($fileName);
596+
$response = $client->packages()->artifacts()->create($file, 'application/zip', $fileName);
597+
$artifactId = $response['id'];
598+
$client->packages()->createArtifactPackage([$artifactId]);
599+
```
600+
#### Update artifact files of a package
601+
602+
```php
603+
$result = $client->packages()->packages()->artifacts()->showPackageArtifacts('acme-website/package'); // get artifact files details for a package
604+
$artifactFileIds = [42, 43];
605+
$client->packages()->editArtifactPackage('acme-website/package', $artifactFileIds);
606+
```
580607
### Credential
581608

582609
#### List an organization's credentials

src/Api/AbstractApi.php

+13
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ protected function post($path, array $parameters = [], array $headers = [])
6767
return $this->responseMediator->getContent($response);
6868
}
6969

70+
protected function postFile($path, $rawFileContent, array $headers = [])
71+
{
72+
$response = $this->client->getHttpClient()->post(
73+
$path,
74+
array_merge($headers, [
75+
'Accept' => 'application/json',
76+
]),
77+
$rawFileContent
78+
);
79+
80+
return $this->responseMediator->getContent($response);
81+
}
82+
7083
/**
7184
* @param string $path
7285
* @param array $parameters

src/Api/Packages.php

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

1010
namespace PrivatePackagist\ApiClient\Api;
1111

12+
use PrivatePackagist\ApiClient\Api\Packages\Artifacts;
1213
use PrivatePackagist\ApiClient\Exception\InvalidArgumentException;
1314

1415
class Packages extends AbstractApi
@@ -67,6 +68,11 @@ public function createCustomPackage($customJson, $credentialId = null)
6768

6869
return $this->post('/packages/', ['repoType' => 'package', 'repoConfig' => $customJson, 'credentials' => $credentialId]);
6970
}
71+
72+
public function createArtifactPackage(array $artifactPackageFileIds)
73+
{
74+
return $this->post('/packages/', ['repoType' => 'artifact', 'artifactIds' => $artifactPackageFileIds]);
75+
}
7076

7177
/**
7278
* @deprecated Use editVcsPackage instead
@@ -81,6 +87,11 @@ public function editVcsPackage($packageName, $url, $credentialId = null, $type =
8187
return $this->put(sprintf('/packages/%s/', $packageName), ['repoType' => $type, 'repoUrl' => $url, 'credentials' => $credentialId]);
8288
}
8389

90+
public function editArtifactPackage($packageName, array $artifactPackageFileIds)
91+
{
92+
return $this->put(sprintf('/packages/%s/', $packageName), ['repoType' => 'artifact', 'artifactIds' => $artifactPackageFileIds]);
93+
}
94+
8495
/**
8596
* @deprecated Use editCustomPackage instead
8697
*/
@@ -108,4 +119,9 @@ public function listDependents($packageName)
108119
{
109120
return $this->get(sprintf('/packages/%s/dependents/', $packageName));
110121
}
122+
123+
public function artifacts()
124+
{
125+
return new Artifacts($this->client, $this->client->getResponseMediator());
126+
}
111127
}

src/Api/Packages/Artifacts.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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\Packages;
11+
12+
use PrivatePackagist\ApiClient\Api\AbstractApi;
13+
14+
class Artifacts extends AbstractApi
15+
{
16+
public function create($file, $contentType, $fileName)
17+
{
18+
return $this->postFile('/packages/artifacts/', $file, array_filter([
19+
'Content-Type' => $contentType,
20+
'X-FILENAME' => $fileName
21+
]));
22+
}
23+
24+
public function show($artifactId)
25+
{
26+
return $this->get(sprintf('/packages/artifacts/%s/', $artifactId));
27+
}
28+
29+
public function showPackageArtifacts($packageName)
30+
{
31+
return $this->get(sprintf('/packages/%s/artifacts/', $packageName));
32+
}
33+
}

src/Client.php

+5
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ public function getHttpClient()
103103
return $this->getHttpClientBuilder()->getHttpClient();
104104
}
105105

106+
public function getResponseMediator()
107+
{
108+
return $this->responseMediator;
109+
}
110+
106111
protected function getHttpClientBuilder()
107112
{
108113
return $this->httpClientBuilder;

tests/Api/ApiTestCase.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected function getApiMock()
3838
$client = new Client(new HttpPluginClientBuilder($httpClient));
3939

4040
return $this->getMockBuilder($this->getApiClass())
41-
->setMethods(['get', 'post', 'patch', 'delete', 'put', 'head'])
41+
->setMethods(['get', 'post', 'postFile', 'patch', 'delete', 'put', 'head'])
4242
->setConstructorArgs([$client])
4343
->getMock();
4444
}

tests/Api/Packages/ArtifactsTest.php

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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\Packages;
11+
12+
use PrivatePackagist\ApiClient\Api\ApiTestCase;
13+
14+
class ArtifactsTest extends ApiTestCase
15+
{
16+
public function testCreate()
17+
{
18+
$expected = [
19+
'id' => 1,
20+
];
21+
$rawFileContent = 'foobar';
22+
$headers = [
23+
'Content-Type' => 'application/zip',
24+
'X-FILENAME' => 'file.zip'
25+
];
26+
27+
/** @var Artifacts&\PHPUnit_Framework_MockObject_MockObject $api */
28+
$api = $this->getApiMock();
29+
$api->expects($this->once())
30+
->method('postFile')
31+
->with($this->equalTo('/packages/artifacts/'), $rawFileContent, $headers)
32+
->willReturn($expected);
33+
34+
35+
$this->assertSame($expected, $api->create($rawFileContent, $headers['Content-Type'], $headers['X-FILENAME']));
36+
}
37+
38+
public function testShow()
39+
{
40+
$expected = [
41+
'repoType' => 'artifact',
42+
'artifactPackageFileIds' =>[1, 2],
43+
];
44+
45+
/** @var Artifacts&\PHPUnit_Framework_MockObject_MockObject $api */
46+
$api = $this->getApiMock();
47+
$api->expects($this->once())
48+
->method('get')
49+
->with($this->equalTo('/packages/artifacts/1/'))
50+
->willReturn($expected);
51+
52+
$this->assertSame($expected, $api->show('1'));
53+
}
54+
55+
public function testShowPackageArtifacts()
56+
{
57+
$expected = [
58+
'name' => 'acme-website/package',
59+
'repoType' => 'artifact',
60+
'artifactFiles' => 'artifact',
61+
];
62+
63+
/** @var Artifacts&\PHPUnit_Framework_MockObject_MockObject $api */
64+
$api = $this->getApiMock();
65+
$api->expects($this->once())
66+
->method('get')
67+
->with($this->equalTo('/packages/acme-website/package/artifacts/'))
68+
->willReturn($expected);
69+
70+
$this->assertSame($expected, $api->showPackageArtifacts('acme-website/package'));
71+
}
72+
73+
/**
74+
* @return string
75+
*/
76+
protected function getApiClass()
77+
{
78+
return Artifacts::class;
79+
}
80+
}

tests/Api/PackagesTest.php

+34
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,23 @@ public function testCreateCustomPackage($customJson)
124124
$this->assertSame($expected, $api->createCustomPackage($customJson));
125125
}
126126

127+
public function testCreateArtifactPackage()
128+
{
129+
$expected = [
130+
'id' => 'job-id',
131+
'status' => 'queued',
132+
];
133+
134+
/** @var Packages&\PHPUnit_Framework_MockObject_MockObject $api */
135+
$api = $this->getApiMock();
136+
$api->expects($this->once())
137+
->method('post')
138+
->with($this->equalTo('/packages/'), $this->equalTo(['repoType' => 'artifact', 'artifactIds' => [42]]))
139+
->willReturn($expected);
140+
141+
$this->assertSame($expected, $api->createArtifactPackage([42]));
142+
}
143+
127144
public function customProvider()
128145
{
129146
return [
@@ -166,6 +183,23 @@ public function testEditCustomPackage()
166183
$this->assertSame($expected, $api->editCustomPackage('acme-website/package', '{}'));
167184
}
168185

186+
public function testEditArtifactPackage()
187+
{
188+
$expected = [
189+
'id' => 'job-id',
190+
'status' => 'queued',
191+
];
192+
193+
/** @var Packages&\PHPUnit_Framework_MockObject_MockObject $api */
194+
$api = $this->getApiMock();
195+
$api->expects($this->once())
196+
->method('put')
197+
->with($this->equalTo('/packages/acme-website/package/'), $this->equalTo(['repoType' => 'artifact', 'artifactIds' => [1, 3]]))
198+
->willReturn($expected);
199+
200+
$this->assertSame($expected, $api->editArtifactPackage('acme-website/package', [1, 3]));
201+
}
202+
169203
public function testRemove()
170204
{
171205
$expected = [];

0 commit comments

Comments
 (0)