Skip to content

Commit 40ed677

Browse files
committed
REST API: implement acknowledge
1 parent 6b615b2 commit 40ed677

File tree

4 files changed

+91
-17
lines changed

4 files changed

+91
-17
lines changed

application/controllers/IssueController.php

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,17 @@ public function acknowledgeAction()
283283
throw new NotFoundError('Not found');
284284
}
285285

286-
// TODO: implement.
286+
$this->runForApi(function () {
287+
if (! $this->checkBearerToken('issue/acknowledge')) {
288+
return;
289+
}
290+
$uuids = $this->findApiRequestIssues();
291+
292+
$this->apiRpcRequestForIssues('issue.acknowledge', 'issues', $uuids, [
293+
$this->params->getRequired('owner'),
294+
$this->params->get('ticket_ref'),
295+
]);
296+
});
287297
}
288298

289299
/**
@@ -295,24 +305,25 @@ public function closeAction()
295305
throw new NotFoundError('Not found');
296306
}
297307
$this->runForApi(function () {
298-
$this->closeForApi();
308+
if (! $this->checkBearerToken('issue/close')) {
309+
return;
310+
}
311+
$uuids = $this->findApiRequestIssues(true);
312+
313+
$this->apiRpcRequestForIssues('issue.close', 'closedIssues', $uuids, [
314+
$this->params->getRequired('closedBy')
315+
]);
299316
});
300317
}
301318

302-
public function closeForApi()
319+
protected function apiRpcRequestForIssues(string $method, string $resultKey, array $issueUuids, ...$rpcParams)
303320
{
304-
if (! $this->checkBearerToken('issue/close')) {
305-
return;
306-
}
307-
$uuids = $this->findApiRequestIssues();
308-
$closedBy = $this->params->getRequired('closedBy');
309321
$client = $this->remoteClient();
310322
$requests = [];
311-
foreach ($uuids as $uuid) {
312-
$requests[$uuid] = $client->request('issue.close', [
323+
foreach ($issueUuids as $uuid) {
324+
$requests[$uuid] = $client->request($method, array_merge([
313325
$uuid,
314-
$closedBy
315-
]);
326+
], $rpcParams));
316327
}
317328
$result = [];
318329
foreach (awaitAll($requests) as $uuid => $success) {
@@ -328,16 +339,16 @@ public function closeForApi()
328339
], 201);
329340
} else {
330341
$this->sendJsonResponse((object) [
331-
'success' => true,
332-
'closedIssues' => $result
342+
'success' => true,
343+
$resultKey => $result
333344
]);
334345
}
335346
}
336347

337-
protected function findApiRequestIssues(): array
348+
protected function findApiRequestIssues(bool $allowTicketRef = false): array
338349
{
339350
$db = $this->db();
340-
if ($ticket = $this->params->get('ticket')) {
351+
if ($allowTicketRef && ($ticket = $this->params->get('ticket'))) {
341352
$uuids = $db->fetchCol($db->select()->from('issue', 'issue_uuid')->where('ticket_ref = ?', $ticket));
342353
foreach ($uuids as $idx => $uuid) {
343354
$uuids[$idx] = Uuid::fromBytes($uuid)->toString();

doc/61-REST_API.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,46 @@ The `closedBy` parameter is required for all `close` variants.
230230
}
231231
```
232232

233+
### Acknowledge an Issue
234+
235+
To acknowledge an issue, please provide its UUID in an HTTP POST request. Example:
236+
237+
```http
238+
POST https://monitoring.example.com/icingaweb2/eventtracker/issue/acknowledge?uuid=0f9ab9e0-600a-4e05-8e13-e48b20b1d37e&owner=John&ticker_ref=ITSM0231
239+
Authorization: Bearer e756ca41-875f-4f92-991c-706dc07af192
240+
Accept: application/json
241+
```
242+
243+
Acknowledging issues via `sender_event_id` is also supported:
244+
245+
```http
246+
POST https://icinga.example.com/icingaweb2/eventtracker/issue/close?sender_event_id=My%20Job%20Name&owner=me
247+
Authorization: Bearer e756ca41-875f-4f92-991c-706dc07af192
248+
Accept: application/json
249+
```
250+
251+
The `owner` parameter is required for all `acknowledge` variants, the `ticket_ref` parameter is optional.
252+
253+
#### Sample Responses
254+
255+
##### Success (200 Ok)
256+
257+
```json
258+
{
259+
"success": true,
260+
"issues": ["0f9ab9e0-600a-4e05-8e13-e48b20b1d37e"]
261+
}
262+
```
263+
264+
##### No related issue found (201 Unmodified)
265+
266+
```json
267+
{
268+
"success": true,
269+
"error": "Found no issue for the given ticket/issue"
270+
}
271+
```
272+
233273
Creating Issues
234274
---------------
235275

library/Eventtracker/Daemon/RpcNamespace/RpcNamespaceIssue.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,29 @@ protected function close(UuidInterface $uuid, $reason, $closedBy = null): bool
9494
return true;
9595
}
9696

97+
/**
98+
* @param string $issueUuid
99+
* @param string $owner
100+
* @param string $ticketReference
101+
* @return bool
102+
*/
103+
protected function acknowledge(UuidInterface $uuid, string $owner, ?string $ticketReference = null): bool
104+
{
105+
if ($this->db === null) {
106+
throw new RuntimeException('Cannot close the given issue, I have no DB connection');
107+
}
108+
$db = $this->db;
109+
$issue = Issue::load($uuid->getBytes(), $db);
110+
$issue->set('status', 'acknowledged');
111+
$issue->set('owner', $owner);
112+
if ($ticketReference !== null) {
113+
$issue->set('ticket_ref', $ticketReference);
114+
}
115+
$issue->storeToDb($db);
116+
117+
return true;
118+
}
119+
97120
public function initDb(Db $db): ExtendedPromiseInterface
98121
{
99122
$this->db = $db;

library/Eventtracker/Web/Form/ApiTokenForm.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ protected function assemble()
5555
'required' => true,
5656
'multiple' => true,
5757
'options' => [
58-
'issue/acknowledge' => $this->translate('Acknowledge Issues (not yet)'),
58+
'issue/acknowledge' => $this->translate('Acknowledge'),
5959
'issue/close' => $this->translate('Close Issues'),
6060
'issues/fetch' => $this->translate('Fetch Issues'),
6161
'host_list/read' => $this->translate('Read permission for Host list'),

0 commit comments

Comments
 (0)