Skip to content

Commit 75941c1

Browse files
committed
Merge pull request #197 from valtlfelipe/master
Add Starred API, revised Watchers API and Improved Docs
2 parents e67a83e + 6acfc0f commit 75941c1

File tree

9 files changed

+288
-18
lines changed

9 files changed

+288
-18
lines changed

doc/activity.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
## Activity API (incomplete)
2+
[Back to the navigation](index.md)
3+
4+
Access to Starring and Watching a Repository for [non] authenticated users.
5+
Wrap [GitHub Activity API](https://developer.github.com/v3/activity/).
6+
7+
> *** No authentication required. ***
8+
9+
### Get repos that a specific user has starred
10+
11+
```php
12+
$users = $client->api('user')->starred('ornicar');
13+
```
14+
15+
Returns an array of starred repos.
16+
17+
### Get repos that a specific user is watching
18+
19+
```php
20+
$users = $client->api('user')->watched('ornicar');
21+
```
22+
23+
Returns an array of watched repos.
24+
25+
> *** Requires [authentication](security.md). ***
26+
27+
### Get repos that a authenticated user has starred
28+
29+
```php
30+
$activity = $client->api('current_user')->starring()->all();
31+
```
32+
Returns an array of starred repos.
33+
34+
### Check if authenticated user has starred a specific repo
35+
36+
```php
37+
$owner = "KnpLabs";
38+
$repo = "php-github-api";
39+
$activity = $client->api('current_user')->starring()->check($owner, $repo);
40+
```
41+
Throws an Exception with code 404 in case that the repo is not starred by the authenticated user or NULL in case that it is starred by the authenticated user.
42+
43+
### Star a specific repo for authenticated user
44+
45+
```php
46+
$owner = "KnpLabs";
47+
$repo = "php-github-api";
48+
$activity = $client->api('current_user')->starring()->star($owner, $repo);
49+
```
50+
Throws an Exception in case of failure or NULL in case of success.
51+
52+
### Unstar a specific repo for authenticated user
53+
54+
```php
55+
$owner = "KnpLabs";
56+
$repo = "php-github-api";
57+
$activity = $client->api('current_user')->starring()->unstar($owner, $repo);
58+
```
59+
Throws an Exception in case of failure or NULL in case of success.
60+
61+
62+
### Get repos that a authenticated user is watching
63+
64+
```php
65+
$activity = $client->api('current_user')->watchers()->all();
66+
```
67+
Returns an array of watched repos.
68+
69+
### Check if authenticated user is watching a specific repo
70+
71+
```php
72+
$owner = "KnpLabs";
73+
$repo = "php-github-api";
74+
$activity = $client->api('current_user')->watchers()->check($owner, $repo);
75+
```
76+
Throws an Exception with code 404 in case that the repo is not beeing watched by the authenticated user or NULL in case that it is beeing watched by the authenticated user.
77+
78+
### Watch a specific repo for authenticated user
79+
80+
```php
81+
$owner = "KnpLabs";
82+
$repo = "php-github-api";
83+
$activity = $client->api('current_user')->watchers()->watch($owner, $repo);
84+
```
85+
Throws an Exception in case of failure or NULL in case of success.
86+
87+
### Stop watching a specific repo for authenticated user
88+
89+
```php
90+
$owner = "KnpLabs";
91+
$repo = "php-github-api";
92+
$activity = $client->api('current_user')->watchers()->unwatch($owner, $repo);
93+
```
94+
Throws an Exception in case of failure or NULL in case of success.

doc/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ APIs:
2020
* [Assets](repo/assets.md)
2121
* [Users](users.md)
2222
* [Meta](meta.md)
23+
* [Activity](activity.md)
2324

2425
Additional features:
2526

doc/users.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,24 @@ $client->api('current_user')->follow()->unfollow('symfony');
108108
Returns an array of followed users.
109109

110110
### Get repos that a specific user is watching
111+
> See [more](activity.md).
111112
112113
```php
113114
$users = $client->api('user')->watched('ornicar');
114115
```
115116

117+
For authenticated user use.
118+
119+
> Requires [authentication](security.md).
120+
121+
```php
122+
$users = $client->api('current_user')->watchers()->all();
123+
```
124+
125+
Returns an array of watched repos.
126+
116127
### Get repos that a specific user has starred
128+
> See [more](activity.md).
117129
118130
```php
119131
$users = $client->api('user')->starred('ornicar');
@@ -124,10 +136,10 @@ For authenticated user use.
124136
> Requires [authentication](security.md).
125137
126138
```php
127-
$users = $client->api('current_user')->watched();
139+
$users = $client->api('current_user')->starring()->all();
128140
```
129141

130-
Returns an array of watched repos.
142+
Returns an array of starred repos.
131143

132144
### Get the authenticated user emails
133145

lib/Github/Api/CurrentUser.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
use Github\Api\CurrentUser\Followers;
88
use Github\Api\CurrentUser\Notifications;
99
use Github\Api\CurrentUser\Watchers;
10+
use Github\Api\CurrentUser\Starring;
1011

1112
/**
1213
* @link http://developer.github.com/v3/users/
1314
* @author Joseph Bielawski <[email protected]>
15+
* @revised Felipe Valtl de Mello <[email protected]>
1416
*/
1517
class CurrentUser extends AbstractApi
1618
{
@@ -111,16 +113,27 @@ public function watchers()
111113
{
112114
return new Watchers($this->client);
113115
}
114-
116+
117+
/**
118+
* @Deprecated Use watchers() instead
119+
*/
115120
public function watched($page = 1)
116121
{
117122
return $this->get('user/watched', array(
118123
'page' => $page
119124
));
120125
}
126+
127+
/**
128+
* @return Starring
129+
*/
130+
public function starring()
131+
{
132+
return new Starring($this->client);
133+
}
121134

122-
/**
123-
* @link http://developer.github.com/changes/2012-9-5-watcher-api/
135+
/**
136+
* @Deprecated Use starring() instead
124137
*/
125138
public function starred($page = 1)
126139
{
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Github\Api\CurrentUser;
4+
5+
use Github\Api\AbstractApi;
6+
7+
/**
8+
* @link https://developer.github.com/v3/activity/starring/
9+
* @author Felipe Valtl de Mello <[email protected]>
10+
*/
11+
class Starring extends AbstractApi
12+
{
13+
/**
14+
* List repositories starred by the authenticated user
15+
* @link https://developer.github.com/v3/activity/starring/
16+
*
17+
* @param integer $page
18+
* @return array
19+
*/
20+
public function all($page = 1)
21+
{
22+
return $this->get('user/starred', array(
23+
'page' => $page
24+
));
25+
}
26+
27+
/**
28+
* Check that the authenticated user starres a repository
29+
* @link https://developer.github.com/v3/activity/starring/
30+
*
31+
* @param string $username the user who owns the repo
32+
* @param string $repository the name of the repo
33+
* @return array
34+
*/
35+
public function check($username, $repository)
36+
{
37+
return $this->get('user/starred/'.rawurlencode($username).'/'.rawurlencode($repository));
38+
}
39+
40+
/**
41+
* Make the authenticated user star a repository
42+
* @link https://developer.github.com/v3/activity/starring/
43+
*
44+
* @param string $username the user who owns the repo
45+
* @param string $repository the name of the repo
46+
* @return array
47+
*/
48+
public function star($username, $repository)
49+
{
50+
return $this->put('user/starred/'.rawurlencode($username).'/'.rawurlencode($repository));
51+
}
52+
53+
/**
54+
* Make the authenticated user unstar a repository
55+
* @link https://developer.github.com/v3/activity/starring
56+
*
57+
* @param string $username the user who owns the repo
58+
* @param string $repository the name of the repo
59+
* @return array
60+
*/
61+
public function unstar($username, $repository)
62+
{
63+
return $this->delete('user/starred/'.rawurlencode($username).'/'.rawurlencode($repository));
64+
}
65+
}

lib/Github/Api/CurrentUser/Watchers.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,61 +5,62 @@
55
use Github\Api\AbstractApi;
66

77
/**
8-
* @link http://developer.github.com/v3/repos/watching/
8+
* @link https://developer.github.com/v3/activity/watching/
99
* @author Joseph Bielawski <[email protected]>
10+
* @revised Felipe Valtl de Mello <[email protected]>
1011
*/
1112
class Watchers extends AbstractApi
1213
{
1314
/**
1415
* List repositories watched by the authenticated user
15-
* @link http://developer.github.com/v3/repos/watching/
16+
* @link https://developer.github.com/v3/activity/watching/
1617
*
1718
* @param integer $page
1819
* @return array
1920
*/
2021
public function all($page = 1)
2122
{
22-
return $this->get('user/watched', array(
23+
return $this->get('user/subscriptions', array(
2324
'page' => $page
2425
));
2526
}
2627

2728
/**
2829
* Check that the authenticated user watches a repository
29-
* @link http://developer.github.com/v3/repos/watching/
30+
* @link https://developer.github.com/v3/activity/watching/
3031
*
3132
* @param string $username the user who owns the repo
3233
* @param string $repository the name of the repo
3334
* @return array
3435
*/
3536
public function check($username, $repository)
3637
{
37-
return $this->get('user/watched/'.rawurlencode($username).'/'.rawurlencode($repository));
38+
return $this->get('user/subscriptions/'.rawurlencode($username).'/'.rawurlencode($repository));
3839
}
3940

4041
/**
4142
* Make the authenticated user watch a repository
42-
* @link http://developer.github.com/v3/repos/watching/
43+
* @link https://developer.github.com/v3/activity/watching/
4344
*
4445
* @param string $username the user who owns the repo
4546
* @param string $repository the name of the repo
4647
* @return array
4748
*/
4849
public function watch($username, $repository)
4950
{
50-
return $this->put('user/watched/'.rawurlencode($username).'/'.rawurlencode($repository));
51+
return $this->put('user/subscriptions/'.rawurlencode($username).'/'.rawurlencode($repository));
5152
}
5253

5354
/**
5455
* Make the authenticated user unwatch a repository
55-
* @link http://developer.github.com/v3/repos/watching/
56+
* @link https://developer.github.com/v3/activity/watching/
5657
*
5758
* @param string $username the user who owns the repo
5859
* @param string $repository the name of the repo
5960
* @return array
6061
*/
6162
public function unwatch($username, $repository)
6263
{
63-
return $this->delete('user/watched/'.rawurlencode($username).'/'.rawurlencode($repository));
64+
return $this->delete('user/subscriptions/'.rawurlencode($username).'/'.rawurlencode($repository));
6465
}
6566
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Github\Tests\Api;
4+
5+
use Github\Tests\Api\TestCase;
6+
7+
class StarringTest extends TestCase
8+
{
9+
/**
10+
* @test
11+
*/
12+
public function shouldGetStarred()
13+
{
14+
$expectedValue = array(
15+
array('name' => 'l3l0/test'),
16+
array('name' => 'cordoval/test')
17+
);
18+
19+
$api = $this->getApiMock();
20+
$api->expects($this->once())
21+
->method('get')
22+
->with('user/starred')
23+
->will($this->returnValue($expectedValue));
24+
25+
$this->assertEquals($expectedValue, $api->all());
26+
}
27+
28+
/**
29+
* @test
30+
*/
31+
public function shouldCheckStar()
32+
{
33+
$api = $this->getApiMock();
34+
$api->expects($this->once())
35+
->method('get')
36+
->with('user/starred/l3l0/test')
37+
->will($this->returnValue(null));
38+
39+
$this->assertNull($api->check('l3l0', 'test'));
40+
}
41+
42+
/**
43+
* @test
44+
*/
45+
public function shouldStarUser()
46+
{
47+
$api = $this->getApiMock();
48+
$api->expects($this->once())
49+
->method('put')
50+
->with('user/starred/l3l0/test')
51+
->will($this->returnValue(null));
52+
53+
$this->assertNull($api->star('l3l0', 'test'));
54+
}
55+
56+
/**
57+
* @test
58+
*/
59+
public function shouldUnstarUser()
60+
{
61+
$api = $this->getApiMock();
62+
$api->expects($this->once())
63+
->method('delete')
64+
->with('user/starred/l3l0/test')
65+
->will($this->returnValue(null));
66+
67+
$this->assertNull($api->unstar('l3l0', 'test'));
68+
}
69+
70+
protected function getApiClass()
71+
{
72+
return 'Github\Api\CurrentUser\Starring';
73+
}
74+
}

0 commit comments

Comments
 (0)