Skip to content

Commit 412ce35

Browse files
authored
Merge pull request #4 from keboola/miro-PS-2486
PS-2486: Added Job warning type
2 parents a06bd65 + a7aa7bd commit 412ce35

13 files changed

Lines changed: 155 additions & 39 deletions

README.md

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ composer require keboola/notification-api-php-client
1212

1313
```php
1414
use Keboola\NotificationClient\EventsClient;
15-
use Keboola\NotificationClient\Requests\PostEvent\FailedJobEventData;
15+
use Keboola\NotificationClient\Requests\PostEvent\JobFailedEventData;
1616
use Keboola\NotificationClient\Requests\PostEvent\JobData;
1717
use Keboola\NotificationClient\Requests\Event;
1818
use Psr\Log\NullLogger;
@@ -23,10 +23,11 @@ $client = new EventsClient(
2323
'xxx-xxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
2424
);
2525
$client->postEvent(
26-
new Event(
27-
'job_failed',
28-
new FailedJobEventData(
29-
'job failed',
26+
new Event(
27+
new JobFailedEventData(
28+
'123',
29+
'My Project',
30+
'Job finished with error',
3031
new JobData('my-project', '123', 'http://someUrl', '2020-01-02', '2020-01-01', 'my-orchestration')
3132
)
3233
)
@@ -68,7 +69,8 @@ $clientFactory->getEventsClient('xxx-xxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
6869

6970
- Set the following environment variables in `set-env.sh` file (use `set-env.template.sh` as sample):
7071
- `STORAGE_API_URL` - Keboola Connection URL to arbitrary stack where the notification service is registered.
71-
- `TEST_STORAGE_API_TOKEN` - Token to a test project.
72+
- `TEST_STORAGE_API_TOKEN` - Token to a test project.
73+
- `TEST_STORAGE_API_PROJECT_ID` - Project ID of the test project.
7274
- `TEST_MANAGE_API_APPLICATION_TOKEN` - Application token with scope `notifications:push-event`.
7375

7476
- Set one of Azure or AWS resources (or both, but only one is needed).
@@ -111,18 +113,19 @@ $clientFactory->getEventsClient('xxx-xxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
111113
SERVICE_PRINCIPAL_ID=$(az ad sp list --display-name testing-notification-api-php-client --query "[0].objectId" --output tsv)
112114
```
113115

114-
- Deploy the key vault, provide tenant ID, service principal ID and group ID from the previous commands:
116+
- Deploy the Storage Account for logs, provide tenant ID, service principal ID and group ID from the previous commands:
115117
```bash
116-
az deployment group create --resource-group testing-job-queue-api-php-client --template-file provisioning/azure.json --parameters vault_name=test-job-queue-client tenant_id=$TEST_AZURE_TENANT_ID service_principal_object_id=$SERVICE_PRINCIPAL_ID
118+
az deployment group create --resource-group testing-notification-api-php-client --template-file provisioning/azure.json --parameters vault_name=test-notification-client tenant_id=$TEST_AZURE_TENANT_ID service_principal_object_id=$SERVICE_PRINCIPAL_ID
117119
```
118120

119-
- Show Key Vault URL
121+
- Get the connection string
120122
```bash
121-
az keyvault show --name test-job-queue-client --query "properties.vaultUri" --output tsv
123+
az storage account show-connection-string -g testing-notification-api-php-client -n mirontfcnacc2 --query "connectionString" --output tsv
122124
```
123125

124-
returns e.g. `https://test-job-queue-client.vault.azure.net/`, use this to set values in `set-env.sh` file:
125-
- `test_azure_key_vault_url` - https://test-job-queue-client.vault.azure.net/
126+
Set the connection string and container name you provided as parameter to the create command to following environment variables in the `set-env.sh` file:
127+
- AZURE_LOGS_ABS_CONTAINER
128+
- AZURE_LOGS_ABS_CONNECTION_STRING
126129

127130
## Generate environment configuration
128131

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
},
3434
"require-dev": {
3535
"infection/infection": "^0.18.2|^0.21.2",
36-
"keboola/coding-standard": ">=10.0.0",
36+
"keboola/coding-standard": "^12.0",
3737
"php-parallel-lint/php-parallel-lint": "^1.2",
3838
"phpstan/phpstan": "^0.12.80",
3939
"phpunit/phpunit": "^9.5"

set-env.template.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ export TEST_AZURE_TENANT_ID=
1919
export TEST_NOTIFICATION_API_URL=https://localhost:8181/
2020
export TEST_STORAGE_API_TOKEN=
2121
export TEST_MANAGE_API_APPLICATION_TOKEN=
22+
export TEST_STORAGE_API_PROJECT_ID=

src/ClientFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Keboola\NotificationClient;
66

7-
use Keboola\NotificationClient\Requests\PostEvent\FailedJobEventData;
7+
use Keboola\NotificationClient\Requests\PostEvent\JobFailedEventData;
88

99
class ClientFactory
1010
{

src/Requests/PostEvent/FailedJobEventData.php renamed to src/Requests/PostEvent/JobFailedEventData.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use Keboola\NotificationClient\Requests\EventDataInterface;
88

9-
class FailedJobEventData implements EventDataInterface
9+
class JobFailedEventData implements EventDataInterface
1010
{
1111
private string $projectId;
1212
private string $projectName;
@@ -21,10 +21,7 @@ public function __construct(string $projectId, string $projectName, string $erro
2121
$this->jobData = $jobData;
2222
}
2323

24-
/**
25-
* @return mixed
26-
*/
27-
public function jsonSerialize()
24+
public function jsonSerialize(): array
2825
{
2926
return [
3027
'errorMessage' => $this->errorMessage,
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Keboola\NotificationClient\Requests\PostEvent;
6+
7+
use Keboola\NotificationClient\Requests\EventDataInterface;
8+
9+
class JobSucceededWithWarningEventData implements EventDataInterface
10+
{
11+
private string $projectId;
12+
private string $projectName;
13+
private string $errorMessage;
14+
private JobData $jobData;
15+
16+
public function __construct(string $projectId, string $projectName, string $errorMessage, JobData $jobData)
17+
{
18+
$this->projectId = $projectId;
19+
$this->projectName = $projectName;
20+
$this->errorMessage = $errorMessage;
21+
$this->jobData = $jobData;
22+
}
23+
24+
public function jsonSerialize(): array
25+
{
26+
return [
27+
'errorMessage' => $this->errorMessage,
28+
'job' => $this->jobData->jsonSerialize(),
29+
'project' => [
30+
'id' => $this->projectId,
31+
'name' => $this->projectName,
32+
],
33+
];
34+
}
35+
36+
public static function getEventTypeName(): string
37+
{
38+
return 'job-succeeded-with-warning';
39+
}
40+
}

src/Requests/Subscription.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
use JsonSerializable;
88
use Keboola\NotificationClient\ClientFactory;
99
use Keboola\NotificationClient\Exception\ClientException;
10-
use Keboola\NotificationClient\Requests\PostEvent\FailedJobEventData;
10+
use Keboola\NotificationClient\Requests\PostEvent\JobFailedEventData;
11+
use Keboola\NotificationClient\Requests\PostEvent\JobSucceededWithWarningEventData;
1112
use Keboola\NotificationClient\Requests\PostSubscription\EmailRecipient;
1213
use Keboola\NotificationClient\Requests\PostSubscription\Filter;
1314

@@ -26,13 +27,7 @@ class Subscription implements JsonSerializable
2627
*/
2728
public function __construct(string $eventType, EmailRecipient $recipient, array $filters)
2829
{
29-
if (!in_array($eventType, [FailedJobEventData::getEventTypeName()])) {
30-
throw new ClientException(sprintf(
31-
'Invalid event type "%s", valid types are: "%s".',
32-
$eventType,
33-
implode(', ', [FailedJobEventData::getEventTypeName()])
34-
));
35-
}
30+
$this->checkEventType($eventType);
3631
$this->eventType = $eventType;
3732
$this->recipient = $recipient;
3833
$this->filters = $filters;
@@ -53,4 +48,20 @@ public function jsonSerialize()
5348
'recipient' => $this->recipient->jsonSerialize(),
5449
];
5550
}
51+
52+
private function checkEventType(string $eventType): void
53+
{
54+
$validEventTypes = [
55+
JobFailedEventData::getEventTypeName(),
56+
JobSucceededWithWarningEventData::getEventTypeName(),
57+
];
58+
59+
if (!in_array($eventType, $validEventTypes)) {
60+
throw new ClientException(sprintf(
61+
'Invalid event type "%s", valid types are: "%s".',
62+
$eventType,
63+
implode(', ', $validEventTypes)
64+
));
65+
}
66+
}
5667
}

tests/ClientTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
use Keboola\NotificationClient\EventsClient;
1414
use Keboola\NotificationClient\Exception\ClientException;
1515
use Keboola\NotificationClient\Requests\Event;
16-
use Keboola\NotificationClient\Requests\PostEvent\FailedJobEventData;
1716
use Keboola\NotificationClient\Requests\PostEvent\JobData;
17+
use Keboola\NotificationClient\Requests\PostEvent\JobFailedEventData;
1818
use PHPUnit\Framework\TestCase;
1919
use Psr\Log\Test\TestLogger;
2020

@@ -99,7 +99,7 @@ public function testCreateClientMultipleErrors(): void
9999
private function getPostEventData(): Event
100100
{
101101
return new Event(
102-
new FailedJobEventData(
102+
new JobFailedEventData(
103103
'1234',
104104
'My project',
105105
'Some Error',

tests/EventsClientFunctionalTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use DateTimeImmutable;
88
use Keboola\NotificationClient\EventsClient;
99
use Keboola\NotificationClient\Requests\Event;
10-
use Keboola\NotificationClient\Requests\PostEvent\FailedJobEventData;
1110
use Keboola\NotificationClient\Requests\PostEvent\JobData;
11+
use Keboola\NotificationClient\Requests\PostEvent\JobFailedEventData;
1212
use PHPUnit\Framework\TestCase;
1313

1414
class EventsClientFunctionalTest extends TestCase
@@ -25,7 +25,7 @@ public function testPostEvent(): void
2525
{
2626
$client = $this->getClient();
2727
$client->postEvent(new Event(
28-
new FailedJobEventData(
28+
new JobFailedEventData(
2929
'1234',
3030
'My project',
3131
'job failed',

tests/Requests/EventTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66

77
use DateTimeImmutable;
88
use Keboola\NotificationClient\Requests\Event;
9-
use Keboola\NotificationClient\Requests\PostEvent\FailedJobEventData;
109
use Keboola\NotificationClient\Requests\PostEvent\JobData;
10+
use Keboola\NotificationClient\Requests\PostEvent\JobFailedEventData;
1111
use PHPUnit\Framework\TestCase;
1212

1313
class EventTest extends TestCase
1414
{
1515
public function testJsonSerialize(): void
1616
{
1717
$postEventRequest = new Event(
18-
new FailedJobEventData(
18+
new JobFailedEventData(
1919
'1234',
2020
'My Project',
2121
'My failed job',

0 commit comments

Comments
 (0)