Merge branch 'develop'
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Simon Vieille 2024-03-12 23:00:48 +01:00
commit 64582d587a
5 changed files with 26 additions and 21 deletions

11
DOCS.md
View file

@ -12,7 +12,7 @@ url: https://gitnet.fr/deblan/woodpecker-email
## Settings ## Settings
| Settings Name | Required | Type | Description | Documentation | | Settings Name | Required | Type | Description | Documentation |
| --- | --- | --- | --- | --- | | --------------- | -------- | ------------------ | ------------------------------------------------------------------- | ---------------------------------------------------------------------------------- |
| dsn | yes | `string` | Mail transport configuration | [Documentation](https://symfony.com/doc/current/mailer.html#tls-peer-verification) | | dsn | yes | `string` | Mail transport configuration | [Documentation](https://symfony.com/doc/current/mailer.html#tls-peer-verification) |
| from.address | yes | `string` | Email address of the sender | | | from.address | yes | `string` | Email address of the sender | |
| from.name | no | `string` | Name of the sender | | | from.name | no | `string` | Name of the sender | |
@ -23,15 +23,12 @@ url: https://gitnet.fr/deblan/woodpecker-email
| attachments | no | `string` or `list` | List of files to attach | YAML list or comma separated list | | attachments | no | `string` or `list` | List of files to attach | YAML list or comma separated list |
| debug | no | `boolean` | Debug mode (email are sent!) | | | debug | no | `boolean` | Debug mode (email are sent!) | |
### Example ### Example
``` ```
steps: steps:
mail: mail:
image: deblan/woodpecker-email-php image: deblan/woodpecker-email
settings: settings:
dsn: "smtp://username:password@mail.example.com:587?verify_peer=1" dsn: "smtp://username:password@mail.example.com:587?verify_peer=1"
from: from:
@ -52,14 +49,12 @@ steps:
- log/* - log/*
``` ```
### Evaluation and content ### Evaluation and content
See the [Twig documentation](https://twig.symfony.com/doc/3.x/). See the [Twig documentation](https://twig.symfony.com/doc/3.x/).
| Variable | Value | | Variable | Value |
| --- | --- | | ----------------------------- | -------------------------------- |
| `workspace` | `CI_WORKSPACE` | | `workspace` | `CI_WORKSPACE` |
| `repo.full_name` | `CI_REPO` | | `repo.full_name` | `CI_REPO` |
| `repo.owner` | `CI_REPO_OWNER` | | `repo.owner` | `CI_REPO_OWNER` |

View file

@ -9,6 +9,7 @@ use Plugin\Loader\EnvVarLoader;
use Plugin\Pipeline\Evaluation; use Plugin\Pipeline\Evaluation;
use Symfony\Component\Mailer\Exception\TransportException; use Symfony\Component\Mailer\Exception\TransportException;
use Twig\Error\SyntaxError; use Twig\Error\SyntaxError;
use Symfony\Component\Mime\Exception\RfcComplianceException;
$build = EnvVarLoader::buildArray([ $build = EnvVarLoader::buildArray([
'workspace' => 'CI_WORKSPACE', 'workspace' => 'CI_WORKSPACE',
@ -102,13 +103,23 @@ function writeln(...$values)
} }
} }
function handleError($section, Exception $e) function handleError($section, Exception $e, bool $debug)
{ {
writeln( writeln(
sprintf('ERROR - %s', $section), sprintf('ERROR - %s', $section),
$e->getMessage() $e->getMessage()
); );
if ($debug) {
writeln(
"",
"Trace",
"=====",
json_encode($e->getTrace(), JSON_PRETTY_PRINT),
"=====",
);
}
exit(1); exit(1);
} }
@ -116,11 +127,8 @@ if ($config['is_debug']) {
writeln( writeln(
'Debug', 'Debug',
'=====', '=====',
'Config', json_encode(['config' => $config, 'environment' => $_SERVER], JSON_PRETTY_PRINT),
json_encode($config, JSON_PRETTY_PRINT), '=====',
'Environment',
json_encode($_SERVER, JSON_PRETTY_PRINT),
'====='
); );
} }
@ -140,9 +148,11 @@ try {
writeln('Email sent!'); writeln('Email sent!');
} }
} catch (SyntaxError $e) { } catch (SyntaxError $e) {
handleError('Syntax error', $e); handleError('Syntax error', $e, $config['is_debug']);
} catch (TransportException $e) { } catch (TransportException $e) {
handleError('Transport error', $e); handleError('Transport error', $e, $config['is_debug']);
} catch (RfcComplianceException $e) {
handleError('RFC compliance error', $e, $config['is_debug']);
} catch (\Exception $e) { } catch (\Exception $e) {
handleError('Generic error', $e); handleError('Generic error', $e, $config['is_debug']);
} }

View file

@ -21,7 +21,7 @@ class EmailFactory
public function createMailer(): Mailer public function createMailer(): Mailer
{ {
return new Mailer(Transport::fromDsn($this->config['dsn'])); return new Mailer(Transport::fromDsn((string) $this->config['dsn']));
} }
public function createEmail(): Email public function createEmail(): Email

View file

@ -3,9 +3,9 @@
namespace Plugin\Factory; namespace Plugin\Factory;
use Twig\Environment; use Twig\Environment;
use Twig\Loader\FilesystemLoader;
use Twig\Extension\StringLoaderExtension;
use Twig\Extension\DebugExtension; use Twig\Extension\DebugExtension;
use Twig\Extension\StringLoaderExtension;
use Twig\Loader\FilesystemLoader;
class TwigFactory class TwigFactory
{ {

View file

@ -12,7 +12,7 @@ function loadEnVars(array $map, array $defaults = [])
} else { } else {
$data = getenv($value); $data = getenv($value);
if ($data === false) { if (false === $data) {
$data = $defaults[$value] ?? null; $data = $defaults[$value] ?? null;
} }