Skip to content

Commit 5bae94f

Browse files
author
Evgeniy Guseletov
committed
Add tests, add more release apis, add assets api, update docs
1 parent 9e40d8b commit 5bae94f

File tree

9 files changed

+406
-5
lines changed

9 files changed

+406
-5
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
"ext-curl": "*",
2222
"kriswallsmith/buzz": ">=0.7"
2323
},
24+
"require-dev": {
25+
"phpunit/phpunit": ">=3.6.0"
26+
},
2427
"autoload": {
2528
"psr-0": { "Github\\": "lib/" }
2629
},

doc/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ APIs:
1515
* [Comments](pull_request/comments.md)
1616
* [Repositories](repos.md)
1717
* [Releases](repo/releases.md)
18+
* [Assets](repo/assets.md)
1819
* [Users](users.md)
1920

2021
Additional features:

doc/repo/assets.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## Repo / Releases API
2+
[Back to the "Repos API"](../repos.md) | [Back to the navigation](../index.md)
3+
4+
### List all assets by release
5+
6+
```php
7+
$assets = $client->api('repo')->releases()->assets()->all('twbs', 'bootstrap', $releaseId);
8+
```
9+
10+
### List one asset
11+
12+
```php
13+
$asset = $client->api('repo')->releases()->assets()->show('twbs', 'bootstrap', $assetId);
14+
```
15+
16+
### Create an asset
17+
18+
This feature is not implemented because require usage of `uploads.github.com` subdomain.
19+
20+
### Edit an asset
21+
22+
```php
23+
$asset = $client->api('repo')->releases()->assets()->edit('twbs', 'bootstrap', $assetId, array('name' => 'New name'));
24+
```
25+
26+
### Remove an asset
27+
28+
```php
29+
$asset = $client->api('repo')->releases()->assets()->remove('twbs', 'bootstrap', $assetId);
30+
```

doc/repo/releases.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ $releases = $client->api('repo')->releases()->all('twbs', 'bootstrap');
1616
$release = $client->api('repo')->releases()->show('twbs', 'bootstrap', $id);
1717
```
1818

19+
### Create a release
20+
```php
21+
$release = $client->api('repo')->releases()->create('twbs', 'bootstrap', array('tag_name' => 'v1.1'));
22+
```
23+
24+
### Edit a release
25+
```php
26+
$release = $client->api('repo')->releases()->edit('twbs', 'bootstrap', $id, array('name' => 'New release name'));
27+
```
28+
1929
### Remove a release
2030

2131
This works, but isn't thoroughly tested, use at your own risk.

lib/Github/Api/Repository/Assets.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace Github\Api\Repository;
4+
5+
use Github\Api\AbstractApi;
6+
use Github\Exception\MissingArgumentException;
7+
8+
/**
9+
* @link http://developer.github.com/v3/repos/releases/
10+
* @author Evgeniy Guseletov <[email protected]>
11+
*/
12+
class Assets extends AbstractApi
13+
{
14+
/**
15+
* Get all release's assets in selected repository
16+
* GET /repos/:owner/:repo/releases/:id/assets
17+
*
18+
* @param string $username the user who owns the repo
19+
* @param string $repository the name of the repo
20+
* @param integer $id the id of the release
21+
*
22+
* @return array
23+
*/
24+
public function all($username, $repository, $id)
25+
{
26+
return $this->get('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/releases/'.rawurlencode($id).'/assets');
27+
}
28+
29+
/**
30+
* Get an asset in selected repository's release
31+
* GET /repos/:owner/:repo/releases/assets/:id
32+
*
33+
* @param string $username the user who owns the repo
34+
* @param string $repository the name of the repo
35+
* @param integer $id the id of the asset
36+
*
37+
* @return array
38+
*/
39+
public function show($username, $repository, $id)
40+
{
41+
return $this->get('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/releases/assets/'.rawurlencode($id));
42+
}
43+
44+
/**
45+
* Edit an asset in selected repository's release
46+
* PATCH /repos/:owner/:repo/releases/assets/:id
47+
*
48+
* @param string $username the user who owns the repo
49+
* @param string $repository the name of the repo
50+
* @param integer $id the id of the asset
51+
* @param array $params request parameters
52+
*
53+
* @throws MissingArgumentException
54+
*
55+
* @return array
56+
*/
57+
public function edit($username, $repository, $id, array $params)
58+
{
59+
if (!isset($params['name'])) {
60+
throw new MissingArgumentException('name');
61+
}
62+
63+
return $this->patch('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/releases/assets/'.rawurlencode($id), $params);
64+
}
65+
66+
/**
67+
* Delete an asset in selected repository's release
68+
* DELETE /repos/:owner/:repo/releases/assets/:id
69+
*
70+
* @param string $username the user who owns the repo
71+
* @param string $repository the name of the repo
72+
* @param integer $id the id of the asset
73+
*
74+
* @return array
75+
*/
76+
public function remove($username, $repository, $id)
77+
{
78+
return $this->delete('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/releases/assets/'.rawurlencode($id));
79+
}
80+
}

lib/Github/Api/Repository/Releases.php

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

55
use Github\Api\AbstractApi;
6+
use Github\Exception\MissingArgumentException;
67

78
/**
9+
* @link http://developer.github.com/v3/repos/releases/
810
* @author Matthew Simo <[email protected]>
11+
* @author Evgeniy Guseletov <[email protected]>
912
*/
1013
class Releases extends AbstractApi
1114
{
@@ -19,25 +22,60 @@ class Releases extends AbstractApi
1922
*/
2023
public function all($username, $repository)
2124
{
22-
return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/releases');
25+
return $this->get('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/releases');
2326
}
2427

2528
/**
2629
* Get a release in selected repository
2730
*
2831
* @param string $username the user who owns the repo
2932
* @param string $repository the name of the repo
30-
* @param integer $id the id of the release
33+
* @param integer $id the id of the release
3134
*
3235
* @return array
3336
*/
3437
public function show($username, $repository, $id)
3538
{
36-
return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/releases/'.urlencode($id));
39+
return $this->get('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/releases/'.rawurlencode($id));
40+
}
41+
42+
/**
43+
* Create new release in selected repository
44+
*
45+
* @param string $username
46+
* @param string $repository
47+
* @param array $params
48+
*
49+
* @throws MissingArgumentException
50+
*
51+
* @return array
52+
*/
53+
public function create($username, $repository, array $params)
54+
{
55+
if (!isset($params['tag_name'])) {
56+
throw new MissingArgumentException('tag_name');
57+
}
58+
59+
return $this->post('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/releases', $params);
60+
}
61+
62+
/**
63+
* Edit release in selected repository
64+
*
65+
* @param string $username
66+
* @param string $repository
67+
* @param integer $id
68+
* @param array $params
69+
*
70+
* @return array
71+
*/
72+
public function edit($username, $repository, $id, array $params)
73+
{
74+
return $this->patch('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/releases/'.rawurlencode($id), $params);
3775
}
3876

3977
/**
40-
* Delete a download in selected repository (Not thoroughly tested!)
78+
* Delete a release in selected repository (Not thoroughly tested!)
4179
*
4280
* @param string $username the user who owns the repo
4381
* @param string $repository the name of the repo
@@ -47,6 +85,14 @@ public function show($username, $repository, $id)
4785
*/
4886
public function remove($username, $repository, $id)
4987
{
50-
return $this->delete('repos/'.urlencode($username).'/'.urlencode($repository).'/releases/'.urlencode($id));
88+
return $this->delete('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/releases/'.rawurlencode($id));
89+
}
90+
91+
/**
92+
* @return Assets
93+
*/
94+
public function assets()
95+
{
96+
return new Assets($this->client);
5197
}
5298
}

test/Github/Tests/Api/RepoTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,16 @@ public function shouldGetStatusesApiObject()
407407
$this->assertInstanceOf('Github\Api\Repository\Statuses', $api->statuses());
408408
}
409409

410+
/**
411+
* @test
412+
*/
413+
public function shouldGetReleasesApiObject()
414+
{
415+
$api = $this->getApiMock();
416+
417+
$this->assertInstanceOf('Github\Api\Repository\Releases', $api->releases());
418+
}
419+
410420
protected function getApiClass()
411421
{
412422
return 'Github\Api\Repo';
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace Github\Tests\Api\Repository;
4+
5+
use Github\Tests\Api\TestCase;
6+
7+
class AssetsTest extends TestCase
8+
{
9+
/**
10+
* @test
11+
*/
12+
public function shouldGetAllReleaseAssets()
13+
{
14+
$expectedValue = array(array('asset1data'), array('asset2data'));
15+
$id = 76;
16+
17+
$api = $this->getApiMock();
18+
$api->expects($this->once())
19+
->method('get')
20+
->with('repos/KnpLabs/php-github-api/releases/'.$id.'/assets')
21+
->will($this->returnValue($expectedValue));
22+
23+
$this->assertEquals($expectedValue, $api->all('KnpLabs', 'php-github-api', $id));
24+
}
25+
26+
/**
27+
* @test
28+
*/
29+
public function shouldGetSingleReleaseAsset()
30+
{
31+
$expectedValue = array('assetData');
32+
$assetId = 2;
33+
34+
$api = $this->getApiMock();
35+
$api->expects($this->once())
36+
->method('get')
37+
->with('repos/KnpLabs/php-github-api/releases/assets/'.$assetId)
38+
->will($this->returnValue($expectedValue));
39+
40+
$this->assertEquals($expectedValue, $api->show('KnpLabs', 'php-github-api', $assetId));
41+
}
42+
43+
/**
44+
* @test
45+
*/
46+
public function shouldEditReleaseAsset()
47+
{
48+
$expectedValue = array('assetUpdatedData');
49+
$assetId = 5;
50+
$data = array('name' => 'asset111_name_qweqwe');
51+
52+
$api = $this->getApiMock();
53+
$api->expects($this->once())
54+
->method('patch')
55+
->with('repos/KnpLabs/php-github-api/releases/assets/'.$assetId)
56+
->will($this->returnValue($expectedValue));
57+
58+
$this->assertEquals($expectedValue, $api->edit('KnpLabs', 'php-github-api', $assetId, $data));
59+
}
60+
61+
/**
62+
* @test
63+
* @expectedException Github\Exception\MissingArgumentException
64+
*/
65+
public function shouldNotEditReleaseAssetWithoutName()
66+
{
67+
$assetId = 5;
68+
$data = array('not_a_name' => 'just a value');
69+
70+
$api = $this->getApiMock();
71+
$api->expects($this->never())
72+
->method('patch');
73+
74+
$api->edit('KnpLabs', 'php-github-api', $assetId, $data);
75+
}
76+
77+
/**
78+
* @test
79+
*/
80+
public function shouldRemoveReleaseAsset()
81+
{
82+
$expectedValue = array('assetUpdatedData');
83+
$assetId = 5;
84+
85+
$api = $this->getApiMock();
86+
$api->expects($this->once())
87+
->method('delete')
88+
->with('repos/KnpLabs/php-github-api/releases/assets/'.$assetId)
89+
->will($this->returnValue($expectedValue));
90+
91+
$this->assertEquals($expectedValue, $api->remove('KnpLabs', 'php-github-api', $assetId));
92+
}
93+
94+
protected function getApiClass()
95+
{
96+
return 'Github\Api\Repository\Assets';
97+
}
98+
}

0 commit comments

Comments
 (0)