Skip to content

Commit bad663d

Browse files
committed
Add support for asset creation
Added coresponding unit test for PHP 5.3.4 or newer. SSL extensions are not fully working for 5.3.3. See travis-ci/travis-ci#1385. Updated documentation to show that Asset creation is now supported.
1 parent 5b34368 commit bad663d

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
lines changed

doc/repo/assets.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ $asset = $client->api('repo')->releases()->assets()->show('twbs', 'bootstrap', $
1515

1616
### Create an asset
1717

18-
This feature is not implemented because require usage of `uploads.github.com` subdomain.
18+
```php
19+
$asset = $client->api('repo')->releases()->assets()->show('twbs', 'bootstrap', $releaseId, $name, $contentType, $content);
20+
```
1921

2022
### Edit an asset
2123

lib/Github/Api/AbstractApi.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,11 @@ protected function delete($path, array $parameters = array(), $requestHeaders =
169169
/**
170170
* Create a JSON encoded version of an array of parameters.
171171
*
172-
* @param $parameters Request parameters
172+
* @param array $parameters Request parameters
173173
* @return null|string
174174
*/
175-
protected function createJsonBody(array $parameters) {
175+
protected function createJsonBody(array $parameters)
176+
{
176177
return (count($parameters) === 0) ? null : json_encode($parameters, empty($parameters) ? JSON_FORCE_OBJECT : 0);
177178
}
178179
}

lib/Github/Api/Repository/Assets.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace Github\Api\Repository;
44

55
use Github\Api\AbstractApi;
6+
use Github\Exception\ErrorException;
67
use Github\Exception\MissingArgumentException;
8+
use Github\HttpClient\HttpClient;
79

810
/**
911
* @link http://developer.github.com/v3/repos/releases/
@@ -41,6 +43,47 @@ public function show($username, $repository, $id)
4143
return $this->get('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/releases/assets/'.rawurlencode($id));
4244
}
4345

46+
/**
47+
* Create an asset for selected repository's release
48+
* POST /repos/:owner/:repo/releases/:id/assets?name=:filename
49+
*
50+
* Creating an asset requires support for server name indentification (SNI)
51+
* so this must be supported by your PHP version.
52+
* @see http://developer.github.com/v3/repos/releases/#upload-a-release-asset
53+
* @see http://php.net/manual/en/openssl.constsni.php
54+
*
55+
* @param string $username the user who owns the repo
56+
* @param string $repository the name of the repo
57+
* @param integer $id the id of the release
58+
* @param string $name the filename for the asset
59+
* @param string $contentType the content type for the asset
60+
* @param string $content the content of the asset
61+
*
62+
* @throws MissingArgumentException
63+
* @throws ErrorException
64+
*
65+
* @return array
66+
*/
67+
public function create($username, $repository, $id, $name, $contentType, $content)
68+
{
69+
if (!defined('OPENSSL_TLSEXT_SERVER_NAME') || !OPENSSL_TLSEXT_SERVER_NAME) {
70+
throw new ErrorException('Asset upload support requires Server Name Indication. This is not supported se your PHP version. See http://php.net/manual/en/openssl.constsni.php.');
71+
}
72+
73+
// Asset creation requires a separate endpoint, uploads.github.com.
74+
// Change the base url for the HTTP client temporarily while we execute
75+
// this request.
76+
$baseUrl = $this->client->getHttpClient()->client->getBaseUrl();
77+
$this->client->getHttpClient()->client->setBaseUrl('https://uploads.github.com/');
78+
79+
$response = $this->postRaw('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/releases/'.rawurlencode($id).'/assets?name='.$name, $content, array('Content-Type' => $contentType));
80+
81+
// Reset the base url.
82+
$this->client->getHttpClient()->client->setBaseUrl($baseUrl);
83+
84+
return $response;
85+
}
86+
4487
/**
4588
* Edit an asset in selected repository's release
4689
* PATCH /repos/:owner/:repo/releases/assets/:id

test/Github/Tests/Api/Repository/AssetsTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,26 @@ public function shouldGetSingleReleaseAsset()
4040
$this->assertEquals($expectedValue, $api->show('KnpLabs', 'php-github-api', $assetId));
4141
}
4242

43+
/**
44+
* @test
45+
* @requires PHP 5.3.4
46+
*/
47+
public function shouldCreateReleaseAsset()
48+
{
49+
$name = 'asset.gzip';
50+
$body = 'assetCreatedData';
51+
$contentType = 'application/gzip';
52+
$releaseId = '12345';
53+
54+
$api = $this->getApiMock();
55+
$api->expects($this->once())
56+
->method('postRaw')
57+
->with('repos/KnpLabs/php-github-api/releases/'. $releaseId .'/assets?name='.$name)
58+
->will($this->returnValue($body));
59+
60+
$this->assertEquals($body, $api->create('KnpLabs', 'php-github-api', $releaseId, $name, $contentType, $body));
61+
}
62+
4363
/**
4464
* @test
4565
*/

0 commit comments

Comments
 (0)