Skip to content

Commit 41457f0

Browse files
author
Matt Humphrey
committed
Tidied up adding project hooks.
Projects::addHook() now accepts only 3 parameters: $project_id, $url and an array of optional event types. The `push_events` event is true by default. Projects::updateHook() now accepts only 2 parameters: $project_id and an array of properties to update.
1 parent bbbc030 commit 41457f0

File tree

4 files changed

+31
-183
lines changed

4 files changed

+31
-183
lines changed

lib/Gitlab/Api/Projects.php

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php namespace Gitlab\Api;
22

3+
use Gitlab\Exception\RuntimeException;
4+
35
class Projects extends AbstractApi
46
{
57
const ORDER_BY = 'created_at';
@@ -205,59 +207,28 @@ public function hook($project_id, $hook_id)
205207
/**
206208
* @param int $project_id
207209
* @param string $url
208-
* @param bool $push_events
209-
* @param bool $issues_events
210-
* @param bool $merge_requests_events
211-
* @param bool $tag_push_events
210+
* @param array $params
212211
* @return mixed
213212
*/
214-
public function addHook($project_id, $url, $push_events = true, $issues_events = false, $merge_requests_events = false, $tag_push_events = false)
213+
public function addHook($project_id, $url, array $params = array())
215214
{
216-
return $this->post($this->getProjectPath($project_id, 'hooks'), array(
217-
'url' => $url,
218-
'push_events' => $push_events,
219-
'issues_events' => $issues_events,
220-
'merge_requests_events' => $merge_requests_events,
221-
'tag_push_events' => $tag_push_events
222-
));
215+
if (empty($params)) {
216+
$params = array('push_events' => true);
217+
}
218+
219+
$params['url'] = $url;
220+
221+
return $this->post($this->getProjectPath($project_id, 'hooks'), $params);
223222
}
224223

225224
/**
226225
* @param int $project_id
227226
* @param int $hook_id
228-
* @param string $url
229-
* @param bool $push_events
230-
* @param bool $issues_events
231-
* @param bool $merge_requests_events
232-
* @param bool $tag_push_events
227+
* @param array $params
233228
* @return mixed
234229
*/
235-
public function updateHook($project_id, $hook_id, $url, $push_events = true, $issues_events = false, $merge_requests_events = false, $tag_push_events = false)
230+
public function updateHook($project_id, $hook_id, array $params)
236231
{
237-
if (is_array($url)) {
238-
$params = $url;
239-
} else {
240-
$params = array(
241-
'url' => $url
242-
);
243-
244-
if ($push_events) {
245-
$params['push_events'] = $push_events;
246-
}
247-
248-
if ($issues_events) {
249-
$params['issues_events'] = $issues_events;
250-
}
251-
252-
if ($merge_requests_events) {
253-
$params['merge_requests_events'] = $merge_requests_events;
254-
}
255-
256-
if ($tag_push_events) {
257-
$params['tag_push_events'] = $tag_push_events;
258-
}
259-
}
260-
261232
return $this->put($this->getProjectPath($project_id, 'hooks/'.urlencode($hook_id)), $params);
262233
}
263234

lib/Gitlab/Model/Project.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -229,38 +229,26 @@ public function hook($id)
229229

230230
/**
231231
* @param string $url
232-
* @param bool $push_events
233-
* @param bool $issues_events
234-
* @param bool $merge_requests_events
232+
* @param array $events
235233
* @return ProjectHook
236234
*/
237-
public function addHook($url, $push_events = true, $issues_events = false, $merge_requests_events = false)
235+
public function addHook($url, array $events = array())
238236
{
239-
$data = $this->api('projects')->addHook($this->id, $url, $push_events, $issues_events, $merge_requests_events);
237+
$data = $this->api('projects')->addHook($this->id, $url, $events);
240238

241239
return ProjectHook::fromArray($this->getClient(), $this, $data);
242240
}
243241

244242
/**
245243
* @param int $hook_id
246-
* @param string $url
247-
* @param bool $push_events
248-
* @param bool $issues_events
249-
* @param bool $merge_requests_events
250-
* @param bool $tag_push_events
244+
* @param array $params
251245
* @return mixed
252246
*/
253-
public function updateHook($hook_id, $url, $push_events = true, $issues_events = true, $merge_requests_events = true, $tag_push_events = true)
247+
public function updateHook($hook_id, array $params)
254248
{
255249
$hook = new ProjectHook($this, $hook_id, $this->getClient());
256250

257-
return $hook->update(array(
258-
'url' => $url,
259-
'push_events' => $push_events,
260-
'issues_events' => $issues_events,
261-
'merge_requests_events' => $merge_requests_events,
262-
'tag_push_events' => $tag_push_events
263-
));
251+
return $hook->update($params);
264252
}
265253

266254
/**

lib/Gitlab/Model/ProjectHook.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,7 @@ public function remove()
9191
*/
9292
public function update(array $params)
9393
{
94-
$params = array_merge(array(
95-
'url' => false,
96-
'push_events' => false,
97-
'issues_events' => false,
98-
'merge_requests_events' => false,
99-
'tag_push_events' => false
100-
), $params);
101-
102-
$data = $this->api('projects')->updateHook($this->project->id, $this->id, $params['url'], $params['push_events'], $params['issues_events'], $params['merge_requests_events'], $params['tag_push_events']);
94+
$data = $this->api('projects')->updateHook($this->project->id, $this->id, $params);
10395

10496
return static::fromArray($this->getClient(), $this->project, $data);
10597
}

test/Gitlab/Tests/Api/ProjectsTest.php

Lines changed: 11 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -328,165 +328,62 @@ public function shouldAddHook()
328328
$api = $this->getApiMock();
329329
$api->expects($this->once())
330330
->method('post')
331-
->with('projects/1/hooks', array(
332-
'url' => 'http://www.example.com',
333-
'push_events' => true,
334-
'issues_events' => false,
335-
'merge_requests_events' => false,
336-
'tag_push_events' => false
337-
))
331+
->with('projects/1/hooks', array('url' => 'http://www.example.com', 'push_events' => true, 'issues_events' => true, 'merge_requests_events' => true))
338332
->will($this->returnValue($expectedArray))
339333
;
340334

341-
$this->assertEquals($expectedArray, $api->addHook(1, 'http://www.example.com'));
342-
}
343-
344-
/**
345-
* @test
346-
*/
347-
public function shouldAddHookWithoutPushEvents()
348-
{
349-
$expectedArray = array('id' => 3, 'name' => 'A new hook', 'url' => 'http://www.example.com');
350-
351-
$api = $this->getApiMock();
352-
$api->expects($this->once())
353-
->method('post')
354-
->with('projects/1/hooks', array(
355-
'url' => 'http://www.example.com',
356-
'push_events' => false,
357-
'issues_events' => false,
358-
'merge_requests_events' => false,
359-
'tag_push_events' => false
360-
))
361-
->will($this->returnValue($expectedArray))
362-
;
363-
364-
$this->assertEquals($expectedArray, $api->addHook(1, 'http://www.example.com', false));
365-
}
366-
367-
/**
368-
* @test
369-
*/
370-
public function shouldAddHookWithIssuesEvents()
371-
{
372-
$expectedArray = array('id' => 3, 'name' => 'A new hook', 'url' => 'http://www.example.com');
373-
374-
$api = $this->getApiMock();
375-
$api->expects($this->once())
376-
->method('post')
377-
->with('projects/1/hooks', array(
378-
'url' => 'http://www.example.com',
379-
'push_events' => false,
380-
'issues_events' => true,
381-
'merge_requests_events' => false,
382-
'tag_push_events' => false
383-
))
384-
->will($this->returnValue($expectedArray))
385-
;
386-
387-
$this->assertEquals($expectedArray, $api->addHook(1, 'http://www.example.com', false, true));
335+
$this->assertEquals($expectedArray, $api->addHook(1, 'http://www.example.com', array('push_events' => true, 'issues_events' => true, 'merge_requests_events' => true)));
388336
}
389337

390338
/**
391339
* @test
392340
*/
393-
public function shouldAddHookWithMergeRequestEvents()
341+
public function shouldAddHookWithOnlyUrl()
394342
{
395343
$expectedArray = array('id' => 3, 'name' => 'A new hook', 'url' => 'http://www.example.com');
396344

397345
$api = $this->getApiMock();
398346
$api->expects($this->once())
399347
->method('post')
400-
->with('projects/1/hooks', array(
401-
'url' => 'http://www.example.com',
402-
'push_events' => false,
403-
'issues_events' => false,
404-
'merge_requests_events' => true,
405-
'tag_push_events' => false
406-
))
348+
->with('projects/1/hooks', array('url' => 'http://www.example.com', 'push_events' => true))
407349
->will($this->returnValue($expectedArray))
408350
;
409351

410-
$this->assertEquals($expectedArray, $api->addHook(1, 'http://www.example.com', false, false, true));
352+
$this->assertEquals($expectedArray, $api->addHook(1, 'http://www.example.com'));
411353
}
412354

413355
/**
414356
* @test
415357
*/
416-
public function shouldAddHookWithTagPushEvents()
358+
public function shouldAddHookWithoutPushEvents()
417359
{
418360
$expectedArray = array('id' => 3, 'name' => 'A new hook', 'url' => 'http://www.example.com');
419361

420362
$api = $this->getApiMock();
421363
$api->expects($this->once())
422364
->method('post')
423-
->with('projects/1/hooks', array(
424-
'url' => 'http://www.example.com',
425-
'push_events' => false,
426-
'issues_events' => false,
427-
'merge_requests_events' => false,
428-
'tag_push_events' => true
429-
))
430-
->will($this->returnValue($expectedArray))
431-
;
432-
433-
$this->assertEquals($expectedArray, $api->addHook(1, 'http://www.example.com', false, false, false, true));
434-
}
435-
436-
/**
437-
* @test
438-
*/
439-
public function shouldUpdateHookUrlOnly()
440-
{
441-
$expectedArray = array('id' => 3, 'name' => 'A new hook', 'url' => 'http://www.example.com');
442-
443-
$api = $this->getApiMock();
444-
$api->expects($this->once())
445-
->method('put')
446-
->with('projects/1/hooks/3', array('url' => 'http://www.example-test.com'))
447-
->will($this->returnValue($expectedArray))
448-
;
449-
450-
$this->assertEquals($expectedArray, $api->updateHook(1, 3, array('url' => 'http://www.example-test.com')));
451-
}
452-
453-
/**
454-
* @test
455-
*/
456-
public function shouldUpdateHookWithPushEvents()
457-
{
458-
$expectedArray = array('id' => 3, 'name' => 'A new hook', 'url' => 'http://www.example.com');
459-
460-
$api = $this->getApiMock();
461-
$api->expects($this->once())
462-
->method('put')
463-
->with('projects/1/hooks/3', array('url' => 'http://www.example-test.com', 'push_events' => true))
365+
->with('projects/1/hooks', array('url' => 'http://www.example.com', 'push_events' => false))
464366
->will($this->returnValue($expectedArray))
465367
;
466368

467-
$this->assertEquals($expectedArray, $api->updateHook(1, 3, array('url' => 'http://www.example-test.com', 'push_events' => true)));
369+
$this->assertEquals($expectedArray, $api->addHook(1, 'http://www.example.com', array('push_events' => false)));
468370
}
469371

470372
/**
471373
* @test
472374
*/
473-
public function shouldUpdateHookWithDifferentEvents()
375+
public function shouldUpdateHook()
474376
{
475377
$expectedArray = array('id' => 3, 'name' => 'A new hook', 'url' => 'http://www.example.com');
476378

477379
$api = $this->getApiMock();
478380
$api->expects($this->once())
479381
->method('put')
480-
->with('projects/1/hooks/3', array(
481-
'url' => 'http://www.example-test.com',
482-
'issues_events' => true,
483-
'merge_requests_events' => true,
484-
'tag_push_events' => true
485-
))
382+
->with('projects/1/hooks/3', array('url' => 'http://www.example-test.com', 'push_events' => false))
486383
->will($this->returnValue($expectedArray))
487384
;
488385

489-
$this->assertEquals($expectedArray, $api->updateHook(1, 3, 'http://www.example-test.com', false, true, true, true));
386+
$this->assertEquals($expectedArray, $api->updateHook(1, 3, array('url' => 'http://www.example-test.com', 'push_events' => false)));
490387
}
491388

492389
/**

0 commit comments

Comments
 (0)