Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ntfy priority environment variable #158

Merged
merged 4 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ To use Gotify, a URL and application token must be given.

To use Ntfy, a URL and topic must be given, all other environment variable are optional.

| Name | Description |
| ------------------------------------ | ---------------------- |
| `VIGILANT_NOTIFICATION_NTFY_URL` | URL used to access Ntfy. |
| `VIGILANT_NOTIFICATION_NTFY_TOPIC` | Ntfy topic. |
| Name | Description |
| ------------------------------------- | --------------------------------------------------------------------------------------- |
| `VIGILANT_NOTIFICATION_NTFY_URL` | URL used to access Ntfy. |
| `VIGILANT_NOTIFICATION_NTFY_TOPIC` | Ntfy topic. |
| `VIGILANT_NOTIFICATION_NTFY_PRIORITY` | Ntfy [message priority](https://docs.ntfy.sh/publish/#message-priority). Defaults to 3. |

Vigilant can be used a with ntfy server that has password or [access token](https://docs.ntfy.sh/config/#access-tokens) authentication enabled.

Expand Down
2 changes: 1 addition & 1 deletion docs/feeds.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ feeds:
| `gotify_priority` | No | Gotify message priority. |
| `ntfy_topic` | No | Ntfy topic. Overrides topic set with an environment variable. |
| `ntfy_token` | No | Ntfy access token. Overrides token set with an environment variable. |
| `ntfy_priority` | No | Ntfy message priority. |
| `ntfy_priority` | No | Ntfy message priority. Overrides default value and/or environment variable. |
16 changes: 16 additions & 0 deletions src/Config/Validate/Ntfy.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ public function url(): void
$this->config['notification_ntfy_url'] = $this->getEnv('NOTIFICATION_NTFY_URL');
}

/**
* Validate `VIGILANT_NOTIFICATION_NTFY_PRIORITY`
*
* @throws ConfigException if priority is not a number
*/
public function priority(): void
{
if ($this->hasEnv('NOTIFICATION_NTFY_PRIORITY') === true) {
if (is_numeric($this->getEnv('NOTIFICATION_NTFY_PRIORITY')) === false) {
throw new ConfigException('Ntfy priority value is not a number [VIGILANT_NOTIFICATION_NTFY_TOPIC]');
}

$this->config['notification_ntfy_priority'] = (int) $this->getEnv('NOTIFICATION_NTFY_PRIORITY');
}
}

/**
* Validate `VIGILANT_NOTIFICATION_NTFY_TOPIC`
*
Expand Down
1 change: 1 addition & 0 deletions src/Config/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public function notificationService(array $supportedServices): void
$ntfy = new Validate\Ntfy($this->config);
$ntfy->url();
$ntfy->topic();
$ntfy->priority();
$ntfy->auth();
$this->config = $ntfy->getConfig();
}
Expand Down
28 changes: 28 additions & 0 deletions tests/Config/Validate/NtfyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,34 @@ public function testNoUrl(): void
$validate->url();
}

/**
* Test priority
*/
public function testPriority(): void
{
putenv('VIGILANT_NOTIFICATION_NTFY_PRIORITY=1');

$validate = new Validate(self::$defaults);
$validate->priority();
$config = $validate->getConfig();

$this->assertEquals(1, $config['notification_ntfy_priority']);
}

/**
* Test priority is not a number
*/
public function testPriorityNotNumber(): void
{
$this->expectException(ConfigException::class);
$this->expectExceptionMessage('Ntfy priority value is not a number');

putenv('VIGILANT_NOTIFICATION_NTFY_PRIORITY=hello');

$validate = new Validate(self::$defaults);
$validate->priority();
}

/**
* Test topic
*/
Expand Down
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ protected function resetEnvs(): void

// Ntfy
putenv('VIGILANT_NOTIFICATION_NTFY_URL');
putenv('VIGILANT_NOTIFICATION_NTFY_PRIORITY');
putenv('VIGILANT_NOTIFICATION_NTFY_TOPIC');
putenv('VIGILANT_NOTIFICATION_NTFY_AUTH');
putenv('VIGILANT_NOTIFICATION_NTFY_TOKEN');
Expand Down