Skip to content

Fixed the API calls for watching and unwatching a repo #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions doc/repos.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,18 @@ $client->api('repo')->collaborators()->remove('username', 'reponame', 'KnpLabs')

Remove the 'username' collaborator from the 'reponame' repository.

### Watch and unwatch a repository
### Subscribe to and unsubscribe from a repository

> Requires [authentication](security.md).

```php
$client->api('current_user')->watchers()->watch('ornicar', 'php-github-api');
$client->api('current_user')->watchers()->unwatch('ornicar', 'php-github-api');
$client->api('current_user')->watchers()->subscribe('ornicar', 'php-github-api');
$client->api('current_user')->watchers()->unsubscribe('ornicar', 'php-github-api');
```

Watches or unwatches the 'php-github-api' repository owned by 'ornicar' and returns the repository.
Subscribes to, or unsubscribes from, the 'php-github-api' repository owned by 'ornicar' and returns the repository.

These methods were previously called watch() and unwatch(); those methods are deprecated, but are still present in the library, and perform exactly as subscribe() and unsubscribe(), respectively.

### Fork a repository

Expand Down
46 changes: 37 additions & 9 deletions lib/Github/Api/CurrentUser/Watchers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,89 @@
use Github\Api\AbstractApi;

/**
* @link http://developer.github.com/v3/repos/watching/
* @link https://developer.github.com/v3/activity/watching/
* @author Joseph Bielawski <stloyd@gmail.com>
*/
class Watchers extends AbstractApi
{
/**
* List repositories watched by the authenticated user
* @link http://developer.github.com/v3/repos/watching/
* @link https://developer.github.com/v3/activity/watching/
*
* @param integer $page
* @return array
*/
public function all($page = 1)
{
return $this->get('user/watched', array(
return $this->get('user/subscriptions', array(
'page' => $page
));
}

/**
* Check that the authenticated user watches a repository
* @link http://developer.github.com/v3/repos/watching/
* @link https://developer.github.com/v3/activity/watching/
*
* @param string $username the user who owns the repo
* @param string $repository the name of the repo
* @return array
*/
public function check($username, $repository)
{
return $this->get('user/watched/'.rawurlencode($username).'/'.rawurlencode($repository));
return $this->get('user/subscriptions/'.rawurlencode($username).'/'.rawurlencode($repository));
}

/**
* Make the authenticated user watch a repository
* @link http://developer.github.com/v3/repos/watching/
* @deprecated use subscribe()
* @link https://developer.github.com/v3/activity/watching/
*
* @param string $username the user who owns the repo
* @param string $repository the name of the repo
* @return array
*/
public function watch($username, $repository)
{
return $this->put('user/watched/'.rawurlencode($username).'/'.rawurlencode($repository));
return $this->subscribe($username, $repository);
}

/**
* Make the authenticated user unwatch a repository
* @link http://developer.github.com/v3/repos/watching/
* @deprecated use unsubscribe()
* @link https://developer.github.com/v3/activity/watching/
*
* @param string $username the user who owns the repo
* @param string $repository the name of the repo
* @return array
*/
public function unwatch($username, $repository)
{
return $this->delete('user/watched/'.rawurlencode($username).'/'.rawurlencode($repository));
return $this->unsubscribe($username, $repository);
}

/**
* Make the authenticated user subscribe to a repository
* @link https://developer.github.com/v3/activity/watching/
*
* @param string $username the user who owns the repo
* @param string $repository the name of the repo
* @return array
*/
public function subscribe($username, $repository)
{
return $this->put('user/subscriptions/'.rawurlencode($username).'/'.rawurlencode($repository));
}

/**
* Make the authenticated user unsubscribe from a repository
* @link https://developer.github.com/v3/activity/watching/
*
* @param string $username the user who owns the repo
* @param string $repository the name of the repo
* @return array
*/
public function unsubscribe($username, $repository)
{
return $this->delete('user/subscriptions/'.rawurlencode($username).'/'.rawurlencode($repository));
}
}
36 changes: 32 additions & 4 deletions test/Github/Tests/Api/CurrentUser/WatchersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function shouldGetWatchers()
$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('user/watched')
->with('user/subscriptions')
->will($this->returnValue($expectedValue));

$this->assertEquals($expectedValue, $api->all());
Expand All @@ -33,7 +33,7 @@ public function shouldCheckWatcher()
$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('user/watched/l3l0/test')
->with('user/subscriptions/l3l0/test')
->will($this->returnValue(null));

$this->assertNull($api->check('l3l0', 'test'));
Expand All @@ -47,7 +47,7 @@ public function shouldWatchUser()
$api = $this->getApiMock();
$api->expects($this->once())
->method('put')
->with('user/watched/l3l0/test')
->with('user/subscriptions/l3l0/test')
->will($this->returnValue(null));

$this->assertNull($api->watch('l3l0', 'test'));
Expand All @@ -61,12 +61,40 @@ public function shouldUnwatchUser()
$api = $this->getApiMock();
$api->expects($this->once())
->method('delete')
->with('user/watched/l3l0/test')
->with('user/subscriptions/l3l0/test')
->will($this->returnValue(null));

$this->assertNull($api->unwatch('l3l0', 'test'));
}

/**
* @test
*/
public function shouldSubscribeUser()
{
$api = $this->getApiMock();
$api->expects($this->once())
->method('put')
->with('user/subscriptions/l3l0/test')
->will($this->returnValue(null));

$this->assertNull($api->subscribe('l3l0', 'test'));
}

/**
* @test
*/
public function shouldUnsubscribeUser()
{
$api = $this->getApiMock();
$api->expects($this->once())
->method('delete')
->with('user/subscriptions/l3l0/test')
->will($this->returnValue(null));

$this->assertNull($api->unsubscribe('l3l0', 'test'));
}

protected function getApiClass()
{
return 'Github\Api\CurrentUser\Watchers';
Expand Down