Skip to content

Commit 33fb56f

Browse files
committed
Add organizations webhooks
1 parent 84ac636 commit 33fb56f

File tree

3 files changed

+264
-0
lines changed

3 files changed

+264
-0
lines changed

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: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
{
48+
throw new MissingArgumentException(array('name', 'config'));
49+
}
50+
51+
return $this->post('orgs/'.rawurlencode($organization).'/hooks', $params);
52+
}
53+
54+
/**
55+
* Edit a hook.
56+
*
57+
* @link https://developer.github.com/v3/orgs/hooks/#edit-a-hook
58+
* @param string $organization
59+
* @param int $id
60+
* @param array $params
61+
* @return array
62+
* @throws \Github\Exception\MissingArgumentException
63+
*/
64+
public function update($organization, $id, array $params)
65+
{
66+
if (!isset($params['config']))
67+
{
68+
throw new MissingArgumentException(array('config'));
69+
}
70+
71+
return $this->patch('orgs/'.rawurlencode($organization).'/hooks/'.rawurlencode($id), $params);
72+
}
73+
74+
/**
75+
* Ping a hook.
76+
*
77+
* @link https://developer.github.com/v3/orgs/hooks/#ping-a-hook
78+
* @param string $organization
79+
* @param int $id
80+
* @return null
81+
*/
82+
public function ping($organization, $id)
83+
{
84+
return $this->post('orgs/'.rawurlencode($organization).'/hooks/'.rawurlencode($id).'/pings');
85+
}
86+
87+
/**
88+
* Delete a hook.
89+
*
90+
* @link https://developer.github.com/v3/orgs/hooks/#delete-a-hook
91+
* @param $organization
92+
* @param $id
93+
* @return null
94+
*/
95+
public function remove($organization, $id)
96+
{
97+
return $this->delete('orgs/'.rawurlencode($organization).'/hooks/'.rawurlencode($id));
98+
}
99+
}
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)