Skip to content

Commit 1748bc5

Browse files
authored
Merge pull request #46107 from nextcloud/feat/webhook_listeners/index-endpoint-by-uri
feat(WebhooksController): Allow querying listeners by URI
2 parents 5dcb807 + 0bdecb9 commit 1748bc5

4 files changed

Lines changed: 49 additions & 2 deletions

File tree

‎apps/webhook_listeners/lib/Controller/WebhooksController.php‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,21 @@ public function __construct(
4848
/**
4949
* List registered webhooks
5050
*
51+
* @param string|null $uri The callback URI to filter by
5152
* @return DataResponse<Http::STATUS_OK, WebhookListenersWebhookInfo[], array{}>
5253
* @throws OCSException Other internal error
5354
*
5455
* 200: Webhook registrations returned
5556
*/
5657
#[ApiRoute(verb: 'GET', url: '/api/v1/webhooks')]
5758
#[AuthorizedAdminSetting(settings:Admin::class)]
58-
public function index(): DataResponse {
59+
public function index(?string $uri = null): DataResponse {
5960
try {
60-
$webhookListeners = $this->mapper->getAll();
61+
if ($uri !== null) {
62+
$webhookListeners = $this->mapper->getByUri($uri);
63+
} else {
64+
$webhookListeners = $this->mapper->getAll();
65+
}
6166

6267
return new DataResponse(
6368
array_map(

‎apps/webhook_listeners/lib/Db/WebhookListenerMapper.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,17 @@ public function getByEvent(string $event): array {
204204

205205
return $this->findEntities($qb);
206206
}
207+
208+
/**
209+
* @throws Exception
210+
*/
211+
public function getByUri(string $uri): array {
212+
$qb = $this->db->getQueryBuilder();
213+
214+
$qb->select('*')
215+
->from($this->getTableName())
216+
->where($qb->expr()->eq('uri', $qb->createNamedParameter($uri, IQueryBuilder::PARAM_STR)));
217+
218+
return $this->findEntities($qb);
219+
}
207220
}

‎apps/webhook_listeners/openapi.json‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,15 @@
112112
}
113113
],
114114
"parameters": [
115+
{
116+
"name": "uri",
117+
"in": "query",
118+
"description": "The callback URI to filter by",
119+
"schema": {
120+
"type": "string",
121+
"nullable": true
122+
}
123+
},
115124
{
116125
"name": "OCS-APIRequest",
117126
"in": "header",

‎apps/webhook_listeners/tests/Db/WebhookListenerMapperTest.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,26 @@ public function testInsertListenerAndGetIt() {
8282
$this->assertEquals($listener1, $listener2);
8383
}
8484

85+
public function testInsertListenerAndGetItByUri() {
86+
$uri = 'https://webhook.example.com/endpoint';
87+
$listener1 = $this->mapper->addWebhookListener(
88+
null,
89+
'bob',
90+
'POST',
91+
$uri,
92+
NodeWrittenEvent::class,
93+
null,
94+
null,
95+
AuthMethod::None,
96+
null,
97+
);
98+
99+
$listeners = $this->mapper->getByUri($uri);
100+
101+
$listener1->resetUpdatedFields();
102+
$this->assertContains($listener1->getId(), array_map(fn ($listener) => $listener->getId(), $listeners));
103+
}
104+
85105
public function testInsertListenerAndGetItWithAuthData() {
86106
$listener1 = $this->mapper->addWebhookListener(
87107
null,

0 commit comments

Comments
 (0)