Skip to content

Commit 989549a

Browse files
committed
Merge pull request #285 from lucasmichot/feature/orgs-webhooks
Add organizations webhooks
2 parents 9375b47 + 648bc86 commit 989549a

File tree

4 files changed

+357
-0
lines changed

4 files changed

+357
-0
lines changed

doc/organization/webhooks.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
## Organization / Webhooks API
2+
[Back to the navigation](README.md)
3+
4+
Listing, showing, creating, updating, testing and removing organizations webhooks.
5+
Wraps [GitHub Organization Webhooks API](https://developer.github.com/v3/orgs/hooks/).
6+
7+
Additional APIs:
8+
* [Organization](issue/organization.md)
9+
10+
### List webhooks for an organization
11+
12+
> Requires [authentication](security.md).
13+
14+
```php
15+
$webhooks = $client->organization()->all('KnpLabs');
16+
```
17+
18+
Returns an array of webhooks for the organization.
19+
20+
### Get a webhook for an organization
21+
22+
> Requires [authentication](security.md).
23+
24+
```php
25+
$webhook = $client->organization()->show('KnpLabs', 123);
26+
```
27+
28+
Returns the webhook with the ID 123 as an array for the organization.
29+
30+
### Create a new webhook for an organization
31+
32+
> Requires [authentication](security.md).
33+
34+
```php
35+
$webhook = $client->organization()->create('KnpLabs', array(
36+
'name' => 'web',
37+
'active' => true,
38+
'events' => array(
39+
'push',
40+
'pull_request'
41+
),
42+
'config' => array(
43+
'url' => 'http=>//example.com/webhook',
44+
'content_type' => 'json'
45+
)
46+
));
47+
```
48+
49+
Creates a new webhook for the organization.
50+
*name* and *url* parameters are required.
51+
52+
The create webhook will be returned as an array.
53+
54+
### Update an existing webhook for an organization
55+
56+
> Requires [authentication](security.md).
57+
58+
```php
59+
$success = $client->organization()->update('KnpLabs', 123, array(
60+
'active' => true,
61+
'events' => array(
62+
'push',
63+
'pull_request'
64+
),
65+
'config' => array(
66+
'url' => 'http=>//example.com/webhook',
67+
'content_type' => 'json'
68+
)
69+
));
70+
```
71+
72+
Update an existing webhook with ID 123 for the organization.
73+
*url* parameter is required.
74+
75+
In case of success, an array of informations about the webhook will be returned.
76+
77+
### Ping a webhook for an organization
78+
79+
> Requires [authentication](security.md).
80+
81+
```php
82+
$client->organization()->pings('KnpLabs', 123);
83+
```
84+
85+
No content is returned.
86+
87+
### Delete a webhook for an organization
88+
89+
> Requires [authentication](security.md).
90+
91+
```php
92+
$client->organization()->delete('KnpLabs', 123);
93+
```
94+
95+
No content is returned.

lib/Github/Api/Organization.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Github\Api;
44

5+
use Github\Api\Organization\Hooks;
56
use Github\Api\Organization\Members;
67
use Github\Api\Organization\Teams;
78

@@ -58,6 +59,14 @@ public function members()
5859
return new Members($this->client);
5960
}
6061

62+
/**
63+
* @return Hooks
64+
*/
65+
public function hooks()
66+
{
67+
return new Hooks($this->client);
68+
}
69+
6170
/**
6271
* @return Teams
6372
*/

lib/Github/Api/Organization/Hooks.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace Github\Api\Organization;
4+
5+
use Github\Api\AbstractApi;
6+
use Github\Exception\MissingArgumentException;
7+
8+
class Hooks extends AbstractApi
9+
{
10+
/**
11+
* List hooks.
12+
*
13+
* @link https://developer.github.com/v3/orgs/hooks/#list-hooks
14+
* @param string $organization
15+
* @return array
16+
*/
17+
public function all($organization)
18+
{
19+
return $this->get('orgs/'.rawurlencode($organization).'/hooks');
20+
}
21+
22+
/**
23+
* Get a single hook.
24+
* @link https://developer.github.com/v3/orgs/hooks/#get-single-hook
25+
*
26+
* @param string $organization
27+
* @param int $id
28+
* @return array
29+
*/
30+
public function show($organization, $id)
31+
{
32+
return $this->get('orgs/'.rawurlencode($organization).'/hooks/'.rawurlencode($id));
33+
}
34+
35+
/**
36+
* Create a hook.
37+
*
38+
* @link https://developer.github.com/v3/orgs/hooks/#create-a-hook
39+
* @param string $organization
40+
* @param array $params
41+
* @return array
42+
* @throws \Github\Exception\MissingArgumentException
43+
*/
44+
public function create($organization, array $params)
45+
{
46+
if (!isset($params['name'], $params['config'])) {
47+
throw new MissingArgumentException(array('name', 'config'));
48+
}
49+
50+
return $this->post('orgs/'.rawurlencode($organization).'/hooks', $params);
51+
}
52+
53+
/**
54+
* Edit a hook.
55+
*
56+
* @link https://developer.github.com/v3/orgs/hooks/#edit-a-hook
57+
* @param string $organization
58+
* @param int $id
59+
* @param array $params
60+
* @return array
61+
* @throws \Github\Exception\MissingArgumentException
62+
*/
63+
public function update($organization, $id, array $params)
64+
{
65+
if (!isset($params['config'])) {
66+
throw new MissingArgumentException(array('config'));
67+
}
68+
69+
return $this->patch('orgs/'.rawurlencode($organization).'/hooks/'.rawurlencode($id), $params);
70+
}
71+
72+
/**
73+
* Ping a hook.
74+
*
75+
* @link https://developer.github.com/v3/orgs/hooks/#ping-a-hook
76+
* @param string $organization
77+
* @param int $id
78+
* @return null
79+
*/
80+
public function ping($organization, $id)
81+
{
82+
return $this->post('orgs/'.rawurlencode($organization).'/hooks/'.rawurlencode($id).'/pings');
83+
}
84+
85+
/**
86+
* Delete a hook.
87+
*
88+
* @link https://developer.github.com/v3/orgs/hooks/#delete-a-hook
89+
* @param string $organization
90+
* @param int $id
91+
* @return null
92+
*/
93+
public function remove($organization, $id)
94+
{
95+
return $this->delete('orgs/'.rawurlencode($organization).'/hooks/'.rawurlencode($id));
96+
}
97+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php
2+
3+
namespace Github\Tests\Api\Organization;
4+
5+
use Github\Tests\Api\TestCase;
6+
7+
class HooksTest extends TestCase
8+
{
9+
/**
10+
* @test
11+
*/
12+
public function shouldGetAllOrganizationsHooks()
13+
{
14+
$expectedValue = array(array('name' => 'hook'));
15+
16+
$api = $this->getApiMock();
17+
$api->expects($this->once())
18+
->method('get')
19+
->with('orgs/KnpLabs/hooks')
20+
->will($this->returnValue($expectedValue));
21+
22+
$this->assertEquals($expectedValue, $api->all('KnpLabs'));
23+
}
24+
25+
/**
26+
* @test
27+
*/
28+
public function shouldShowHook()
29+
{
30+
$expectedValue = array('hook' => 'somename');
31+
32+
$api = $this->getApiMock();
33+
$api->expects($this->once())
34+
->method('get')
35+
->with('orgs/KnpLabs/hooks/123')
36+
->will($this->returnValue($expectedValue));
37+
38+
$this->assertEquals($expectedValue, $api->show('KnpLabs', 123));
39+
}
40+
41+
/**
42+
* @test
43+
*/
44+
public function shouldRemoveHook()
45+
{
46+
$expectedValue = array('someOutput');
47+
48+
$api = $this->getApiMock();
49+
$api->expects($this->once())
50+
->method('delete')
51+
->with('orgs/KnpLabs/hooks/123')
52+
->will($this->returnValue($expectedValue));
53+
54+
$this->assertEquals($expectedValue, $api->remove('KnpLabs', 123));
55+
}
56+
57+
/**
58+
* @test
59+
* @expectedException \Github\Exception\MissingArgumentException
60+
*/
61+
public function shouldNotCreateHookWithoutName()
62+
{
63+
$data = array('config' => 'conf');
64+
65+
$api = $this->getApiMock();
66+
$api->expects($this->never())
67+
->method('post');
68+
69+
$api->create('KnpLabs', $data);
70+
}
71+
72+
/**
73+
* @test
74+
* @expectedException \Github\Exception\MissingArgumentException
75+
*/
76+
public function shouldNotCreateHookWithoutConfig()
77+
{
78+
$data = array('name' => 'test');
79+
80+
$api = $this->getApiMock();
81+
$api->expects($this->never())
82+
->method('post');
83+
84+
$api->create('KnpLabs', $data);
85+
}
86+
87+
/**
88+
* @test
89+
*/
90+
public function shouldCreateHook()
91+
{
92+
$expectedValue = array('hook' => 'somename');
93+
$data = array('name' => 'test', 'config' => 'someconfig');
94+
95+
$api = $this->getApiMock();
96+
$api->expects($this->once())
97+
->method('post')
98+
->with('orgs/KnpLabs/hooks', $data)
99+
->will($this->returnValue($expectedValue));
100+
101+
$this->assertEquals($expectedValue, $api->create('KnpLabs', $data));
102+
}
103+
104+
/**
105+
* @test
106+
* @expectedException \Github\Exception\MissingArgumentException
107+
*/
108+
public function shouldNotUpdateHookWithoutConfig()
109+
{
110+
$data = array();
111+
112+
$api = $this->getApiMock();
113+
$api->expects($this->never())
114+
->method('patch');
115+
116+
$api->update('KnpLabs', 123, $data);
117+
}
118+
119+
/**
120+
* @test
121+
*/
122+
public function shouldUpdateHook()
123+
{
124+
$expectedValue = array('hook' => 'somename');
125+
$data = array('config' => 'config');
126+
127+
$api = $this->getApiMock();
128+
$api->expects($this->once())
129+
->method('patch')
130+
->with('orgs/KnpLabs/hooks/123', $data)
131+
->will($this->returnValue($expectedValue));
132+
133+
$this->assertEquals($expectedValue, $api->update('KnpLabs', 123, $data));
134+
}
135+
136+
/**
137+
* @test
138+
*/
139+
public function shouldPingHook()
140+
{
141+
$expectedValue = null;
142+
143+
$api = $this->getApiMock();
144+
$api->expects($this->once())
145+
->method('post')
146+
->with('orgs/KnpLabs/hooks/123/pings')
147+
->will($this->returnValue($expectedValue));
148+
149+
$this->assertEquals($expectedValue, $api->ping('KnpLabs', 123));
150+
}
151+
152+
protected function getApiClass()
153+
{
154+
return 'Github\Api\Organization\Hooks';
155+
}
156+
}

0 commit comments

Comments
 (0)