Skip to content

Commit

Permalink
feature #1086 App: add hook endpoints (glaubinix)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.9.x-dev branch.

Discussion
----------

A GitHub app can have a single webhook configured. Add endpoints to manage the hook to the API client https://docs.github.com/en/rest/apps/webhooks

Commits
-------

4e2900e App: add hook endpoints
  • Loading branch information
acrobat committed Oct 23, 2022
2 parents 0c28f89 + 4e2900e commit cb4b4eb
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 0 deletions.
76 changes: 76 additions & 0 deletions lib/Github/Api/App/Hook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace Github\Api\App;

use Github\Api\AbstractApi;

class Hook extends AbstractApi
{
/**
* Show the app hook configuration.
*
* @link https://docs.github.com/en/rest/apps/webhooks#get-a-webhook-configuration-for-an-app
*
* @return array
*/
public function showConfig()
{
return $this->get('/app/hook/config');
}

/**
* Update the hook configuration of an app.
*
* @link https://docs.github.com/en/rest/apps/webhooks#update-a-webhook-configuration-for-an-app
*
* @param array $params
*
* @return array
*/
public function updateConfig(array $params)
{
return $this->patch('/app/hook/config', $params);
}

/**
* List deliveries for an app webhook.
*
* @link https://docs.github.com/en/rest/apps/webhooks#list-deliveries-for-an-app-webhook
*
* @return array
*/
public function deliveries()
{
return $this->get('/app/hook/deliveries');
}

/**
* Get a delivery for an app webhook.
*
* @link https://docs.github.com/en/rest/apps/webhooks#get-a-delivery-for-an-app-webhook
*
* @param int $delivery
*
* @return array
*/
public function delivery($delivery)
{
return $this->get('/app/hook/deliveries/'.$delivery);
}

/**
* Redeliver a delivery for an app webhook.
*
* @link https://docs.github.com/en/rest/apps/webhooks#redeliver-a-delivery-for-an-app-webhook
*
* @param int $delivery
*
* @return array
*/
public function redeliver($delivery)
{
return $this->post('/app/hook/deliveries/'.$delivery.'/attempts');
}
}
14 changes: 14 additions & 0 deletions lib/Github/Api/Apps.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Github\Api;

use Github\Api\App\Hook;

/**
* @link https://developer.github.com/v3/apps/
*
Expand Down Expand Up @@ -198,4 +200,16 @@ public function getAuthenticatedApp()
{
return $this->get('/app');
}

/**
* Manage the hook of an app.
*
* @link https://docs.github.com/en/rest/apps/webhooks
*
* @return Hook
*/
public function hook()
{
return new Hook($this->getClient());
}
}
109 changes: 109 additions & 0 deletions test/Github/Tests/Api/App/HookTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

declare(strict_types=1);

namespace Github\Api\App;

use Github\Tests\Api\TestCase;

class HookTest extends TestCase
{
/**
* @test
*/
public function shouldShowHookConfiguration()
{
$result = [
'content_type' => 'json',
'insecure_ssl' => 0,
'secret' => '********',
'url' => 'https://localhost/',
];

$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('/app/hook/config', [])
->willReturn($result);

$this->assertEquals($result, $api->showConfig());
}

/**
* @test
*/
public function shouldUpdateHookConfiguration()
{
$parameters = [
'content_type' => 'json',
];

$api = $this->getApiMock();
$api->expects($this->once())
->method('patch')
->with('/app/hook/config', $parameters)
->willReturn([]);

$this->assertEquals([], $api->updateConfig($parameters));
}

/**
* @test
*/
public function shouldListHookDelivieries()
{
$result = [];

$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('/app/hook/deliveries', [])
->willReturn($result);

$this->assertEquals($result, $api->deliveries());
}

/**
* @test
*/
public function shouldListHookDeliviery()
{
$result = [];

$delivery = 1234567;

$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('/app/hook/deliveries/'.$delivery, [])
->willReturn($result);

$this->assertEquals($result, $api->delivery($delivery));
}

/**
* @test
*/
public function shouldRedeliveryHook()
{
$result = [];

$delivery = 1234567;

$api = $this->getApiMock();
$api->expects($this->once())
->method('post')
->with('/app/hook/deliveries/'.$delivery.'/attempts', [])
->willReturn($result);

$this->assertEquals($result, $api->redeliver($delivery));
}

/**
* @return string
*/
protected function getApiClass()
{
return \Github\Api\App\Hook::class;
}
}

0 comments on commit cb4b4eb

Please sign in to comment.