|
3 | 3 | namespace Github\Api\Repository;
|
4 | 4 |
|
5 | 5 | use Github\Api\AbstractApi;
|
| 6 | +use Github\Exception\ErrorException; |
6 | 7 | use Github\Exception\MissingArgumentException;
|
| 8 | +use Github\HttpClient\HttpClient; |
7 | 9 |
|
8 | 10 | /**
|
9 | 11 | * @link http://developer.github.com/v3/repos/releases/
|
@@ -41,6 +43,47 @@ public function show($username, $repository, $id)
|
41 | 43 | return $this->get('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/releases/assets/'.rawurlencode($id));
|
42 | 44 | }
|
43 | 45 |
|
| 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 | + |
44 | 87 | /**
|
45 | 88 | * Edit an asset in selected repository's release
|
46 | 89 | * PATCH /repos/:owner/:repo/releases/assets/:id
|
|
0 commit comments