Skip to content

Commit

Permalink
Merge pull request #285 from lucasmichot/feature/orgs-webhooks
Browse files Browse the repository at this point in the history
Add organizations webhooks
  • Loading branch information
cursedcoder committed Jul 2, 2015
2 parents 9375b47 + 648bc86 commit 989549a
Show file tree
Hide file tree
Showing 4 changed files with 357 additions and 0 deletions.
95 changes: 95 additions & 0 deletions doc/organization/webhooks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
## Organization / Webhooks API
[Back to the navigation](README.md)

Listing, showing, creating, updating, testing and removing organizations webhooks.
Wraps [GitHub Organization Webhooks API](https://developer.github.com/v3/orgs/hooks/).

Additional APIs:
* [Organization](issue/organization.md)

### List webhooks for an organization

> Requires [authentication](security.md).
```php
$webhooks = $client->organization()->all('KnpLabs');
```

Returns an array of webhooks for the organization.

### Get a webhook for an organization

> Requires [authentication](security.md).
```php
$webhook = $client->organization()->show('KnpLabs', 123);
```

Returns the webhook with the ID 123 as an array for the organization.

### Create a new webhook for an organization

> Requires [authentication](security.md).
```php
$webhook = $client->organization()->create('KnpLabs', array(
'name' => 'web',
'active' => true,
'events' => array(
'push',
'pull_request'
),
'config' => array(
'url' => 'http=>//example.com/webhook',
'content_type' => 'json'
)
));
```

Creates a new webhook for the organization.
*name* and *url* parameters are required.

The create webhook will be returned as an array.

### Update an existing webhook for an organization

> Requires [authentication](security.md).
```php
$success = $client->organization()->update('KnpLabs', 123, array(
'active' => true,
'events' => array(
'push',
'pull_request'
),
'config' => array(
'url' => 'http=>//example.com/webhook',
'content_type' => 'json'
)
));
```

Update an existing webhook with ID 123 for the organization.
*url* parameter is required.

In case of success, an array of informations about the webhook will be returned.

### Ping a webhook for an organization

> Requires [authentication](security.md).
```php
$client->organization()->pings('KnpLabs', 123);
```

No content is returned.

### Delete a webhook for an organization

> Requires [authentication](security.md).
```php
$client->organization()->delete('KnpLabs', 123);
```

No content is returned.
9 changes: 9 additions & 0 deletions lib/Github/Api/Organization.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Github\Api;

use Github\Api\Organization\Hooks;
use Github\Api\Organization\Members;
use Github\Api\Organization\Teams;

Expand Down Expand Up @@ -58,6 +59,14 @@ public function members()
return new Members($this->client);
}

/**
* @return Hooks
*/
public function hooks()
{
return new Hooks($this->client);
}

/**
* @return Teams
*/
Expand Down
97 changes: 97 additions & 0 deletions lib/Github/Api/Organization/Hooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace Github\Api\Organization;

use Github\Api\AbstractApi;
use Github\Exception\MissingArgumentException;

class Hooks extends AbstractApi
{
/**
* List hooks.
*
* @link https://developer.github.com/v3/orgs/hooks/#list-hooks
* @param string $organization
* @return array
*/
public function all($organization)
{
return $this->get('orgs/'.rawurlencode($organization).'/hooks');
}

/**
* Get a single hook.
* @link https://developer.github.com/v3/orgs/hooks/#get-single-hook
*
* @param string $organization
* @param int $id
* @return array
*/
public function show($organization, $id)
{
return $this->get('orgs/'.rawurlencode($organization).'/hooks/'.rawurlencode($id));
}

/**
* Create a hook.
*
* @link https://developer.github.com/v3/orgs/hooks/#create-a-hook
* @param string $organization
* @param array $params
* @return array
* @throws \Github\Exception\MissingArgumentException
*/
public function create($organization, array $params)
{
if (!isset($params['name'], $params['config'])) {
throw new MissingArgumentException(array('name', 'config'));
}

return $this->post('orgs/'.rawurlencode($organization).'/hooks', $params);
}

/**
* Edit a hook.
*
* @link https://developer.github.com/v3/orgs/hooks/#edit-a-hook
* @param string $organization
* @param int $id
* @param array $params
* @return array
* @throws \Github\Exception\MissingArgumentException
*/
public function update($organization, $id, array $params)
{
if (!isset($params['config'])) {
throw new MissingArgumentException(array('config'));
}

return $this->patch('orgs/'.rawurlencode($organization).'/hooks/'.rawurlencode($id), $params);
}

/**
* Ping a hook.
*
* @link https://developer.github.com/v3/orgs/hooks/#ping-a-hook
* @param string $organization
* @param int $id
* @return null
*/
public function ping($organization, $id)
{
return $this->post('orgs/'.rawurlencode($organization).'/hooks/'.rawurlencode($id).'/pings');
}

/**
* Delete a hook.
*
* @link https://developer.github.com/v3/orgs/hooks/#delete-a-hook
* @param string $organization
* @param int $id
* @return null
*/
public function remove($organization, $id)
{
return $this->delete('orgs/'.rawurlencode($organization).'/hooks/'.rawurlencode($id));
}
}
156 changes: 156 additions & 0 deletions test/Github/Tests/Api/Organization/HooksTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php

namespace Github\Tests\Api\Organization;

use Github\Tests\Api\TestCase;

class HooksTest extends TestCase
{
/**
* @test
*/
public function shouldGetAllOrganizationsHooks()
{
$expectedValue = array(array('name' => 'hook'));

$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('orgs/KnpLabs/hooks')
->will($this->returnValue($expectedValue));

$this->assertEquals($expectedValue, $api->all('KnpLabs'));
}

/**
* @test
*/
public function shouldShowHook()
{
$expectedValue = array('hook' => 'somename');

$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('orgs/KnpLabs/hooks/123')
->will($this->returnValue($expectedValue));

$this->assertEquals($expectedValue, $api->show('KnpLabs', 123));
}

/**
* @test
*/
public function shouldRemoveHook()
{
$expectedValue = array('someOutput');

$api = $this->getApiMock();
$api->expects($this->once())
->method('delete')
->with('orgs/KnpLabs/hooks/123')
->will($this->returnValue($expectedValue));

$this->assertEquals($expectedValue, $api->remove('KnpLabs', 123));
}

/**
* @test
* @expectedException \Github\Exception\MissingArgumentException
*/
public function shouldNotCreateHookWithoutName()
{
$data = array('config' => 'conf');

$api = $this->getApiMock();
$api->expects($this->never())
->method('post');

$api->create('KnpLabs', $data);
}

/**
* @test
* @expectedException \Github\Exception\MissingArgumentException
*/
public function shouldNotCreateHookWithoutConfig()
{
$data = array('name' => 'test');

$api = $this->getApiMock();
$api->expects($this->never())
->method('post');

$api->create('KnpLabs', $data);
}

/**
* @test
*/
public function shouldCreateHook()
{
$expectedValue = array('hook' => 'somename');
$data = array('name' => 'test', 'config' => 'someconfig');

$api = $this->getApiMock();
$api->expects($this->once())
->method('post')
->with('orgs/KnpLabs/hooks', $data)
->will($this->returnValue($expectedValue));

$this->assertEquals($expectedValue, $api->create('KnpLabs', $data));
}

/**
* @test
* @expectedException \Github\Exception\MissingArgumentException
*/
public function shouldNotUpdateHookWithoutConfig()
{
$data = array();

$api = $this->getApiMock();
$api->expects($this->never())
->method('patch');

$api->update('KnpLabs', 123, $data);
}

/**
* @test
*/
public function shouldUpdateHook()
{
$expectedValue = array('hook' => 'somename');
$data = array('config' => 'config');

$api = $this->getApiMock();
$api->expects($this->once())
->method('patch')
->with('orgs/KnpLabs/hooks/123', $data)
->will($this->returnValue($expectedValue));

$this->assertEquals($expectedValue, $api->update('KnpLabs', 123, $data));
}

/**
* @test
*/
public function shouldPingHook()
{
$expectedValue = null;

$api = $this->getApiMock();
$api->expects($this->once())
->method('post')
->with('orgs/KnpLabs/hooks/123/pings')
->will($this->returnValue($expectedValue));

$this->assertEquals($expectedValue, $api->ping('KnpLabs', 123));
}

protected function getApiClass()
{
return 'Github\Api\Organization\Hooks';
}
}

0 comments on commit 989549a

Please sign in to comment.