Skip to content

Commit 48c2d54

Browse files
authored
Merge pull request #40 from packagist/t/add-artifact-package
Add artifact to an existing package
2 parents 8568678 + f73c05f commit 48c2d54

File tree

5 files changed

+61
-7
lines changed

5 files changed

+61
-7
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ php:
55
- 7.0
66
- 7.1
77
- 7.2
8+
- 7.4
89

910
before_install:
1011
- if [ -n "$GH_TOKEN" ]; then composer config github-oauth.github.com ${GH_TOKEN}; fi;

README.md

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@
7272
* [List all customers with access to a package](#list-all-customers-with-access-to-a-package)
7373
* [Create an artifact package file](#create-an-artifact-package-file)
7474
* [Create an artifact package](#create-an-artifact-package)
75-
* [Update artifact files of a package](#update-artifact-files-of-a-package)
75+
* [Add an artifact file to an existing package](#add-an-artifact-file-to-an-existing-package)
76+
* [Update or replace artifact files of a package](#update-or-replace-artifact-files-of-a-package)
7677
* [Credential](#credential)
7778
* [List an organization's credentials](#list-an-organizations-credentials)
7879
* [Show a credential](#show-a-credential)
@@ -98,7 +99,7 @@
9899
* [Validate incoming webhook payloads](#validate-incoming-webhook-payloads)
99100
* [License](#license)
100101

101-
<!-- Added by: wissem, at: Tue Jul 21 10:32:47 CEST 2020 -->
102+
<!-- Added by: wissem, at: Fri Oct 16 14:23:54 CEST 2020 -->
102103

103104
<!--te-->
104105

@@ -597,13 +598,34 @@ $response = $client->packages()->artifacts()->create($file, 'application/zip', $
597598
$artifactId = $response['id'];
598599
$client->packages()->createArtifactPackage([$artifactId]);
599600
```
600-
#### Update artifact files of a package
601+
#### Add an artifact file to an existing package
601602

602603
```php
603-
$result = $client->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);
604+
$packageName = 'acme/artifact';
605+
$fileName = 'package1.zip';
606+
$file = file_get_contents($fileName);
607+
$client->packages()->artifacts()->add($packageName, $file, 'application/zip', $fileName);
606608
```
609+
#### Update or replace artifact files of a package
610+
611+
```php
612+
// in case you want to replace the artifact file with a newly uploaded one
613+
// 1. get current artifact ids
614+
$result = $client->packages()->artifacts()->showPackageArtifacts('acme-website/package');
615+
$artifactIds = array_column($result, 'id'); // [41, 42]
616+
617+
// 2. upload the new artifact file
618+
$fileName = 'package1.zip';
619+
$file = file_get_contents($fileName);
620+
$response = $client->packages()->artifacts()->create($file, 'application/zip', $fileName);
621+
$newArtifactId = $response['id'];
622+
623+
// 3. let's say we don't want to have the artifact file id = 41 and use the newly uploaded file instead
624+
$artifactIds = array_shift($artifactIds);
625+
$artifactIds[] = $newArtifactId;
626+
$client->packages()->editArtifactPackage('acme-website/package', $artifactIds);
627+
```
628+
607629
### Credential
608630

609631
#### List an organization's credentials

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"php-http/client-common": "^1.9 || ^2.0"
2020
},
2121
"require-dev": {
22-
"phpunit/phpunit": "^5.5 || ^6.0",
22+
"phpunit/phpunit": "^5.5 || ^7.0",
2323
"php-http/guzzle6-adapter": "^1.0 || ^2.0",
2424
"php-http/mock-client": "^1.0",
2525
"guzzlehttp/psr7": "^1.2",

src/Api/Packages/Artifacts.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ public function create($file, $contentType, $fileName)
2121
]));
2222
}
2323

24+
public function add($packageName, $file, $contentType, $fileName)
25+
{
26+
return $this->postFile('/packages/'.$packageName.'/artifacts/', $file, array_filter([
27+
'Content-Type' => $contentType,
28+
'X-FILENAME' => $fileName
29+
]));
30+
}
31+
2432
public function show($artifactId)
2533
{
2634
return $this->get(sprintf('/packages/artifacts/%s/', $artifactId));

tests/Api/Packages/ArtifactsTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,29 @@ public function testCreate()
3535
$this->assertSame($expected, $api->create($rawFileContent, $headers['Content-Type'], $headers['X-FILENAME']));
3636
}
3737

38+
public function testAdd()
39+
{
40+
$packageName = 'acme/artifact';
41+
$expected = [
42+
'id' => 1,
43+
];
44+
$rawFileContent = 'foobar';
45+
$headers = [
46+
'Content-Type' => 'application/zip',
47+
'X-FILENAME' => 'file.zip'
48+
];
49+
50+
/** @var Artifacts&\PHPUnit_Framework_MockObject_MockObject $api */
51+
$api = $this->getApiMock();
52+
$api->expects($this->once())
53+
->method('postFile')
54+
->with($this->equalTo('/packages/'.$packageName.'/artifacts/'), $rawFileContent, $headers)
55+
->willReturn($expected);
56+
57+
58+
$this->assertSame($expected, $api->add($packageName,$rawFileContent, $headers['Content-Type'], $headers['X-FILENAME']));
59+
}
60+
3861
public function testShow()
3962
{
4063
$expected = [

0 commit comments

Comments
 (0)