Merge pull request 'add option to define the level in the mail #10' (#11) from feature/status-setting into develop

Reviewed-on: deblan/woodpecker-email#11
This commit is contained in:
Simon Vieille 2025-02-18 20:53:07 +01:00
commit 57a0edab06
5 changed files with 76 additions and 36 deletions

42
DOCS.md
View file

@ -11,21 +11,22 @@ url: https://gitnet.fr/deblan/woodpecker-email
## Settings
| Settings Name | Required | Type | Description | Documentation |
| --------------- | -------- | ------------------ | ------------------------------------------------------------------- | ---------------------------------------------------------------------------------- |
| 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.name | no | `string` | Name of the sender | |
| recipients | no | `string` or `list` | List of recipients to send this mail to (besides the commit author) | YAML list or comma separated list |
| recipients_only | no | `boolean` | Exclude the committer (default: `false`) | |
| content.subject | no | `string` | Define the email subject template | |
| content.body | no | `string` | Define the email body template | |
| attachments | no | `string` or `list` | List of files to attach | YAML list or comma separated list |
| debug | no | `boolean` | Debug mode (email are sent!, default: `false`) | |
| Settings Name | Required | Type | Description | Documentation |
| --------------- | -------- | ------------------ | ------------------------------------------------------------------- | ---------------------------------------------------------------------------------- |
| 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.name | no | `string` | Name of the sender | |
| recipients | no | `string` or `list` | List of recipients to send this mail to (besides the commit author) | YAML list or comma separated list |
| recipients_only | no | `boolean` | Exclude the committer (default: `false`) | |
| content.subject | no | `string` | Define the email subject template | |
| content.body | no | `string` | Define the email body template | |
| attachments | no | `string` or `list` | List of files to attach | YAML list or comma separated list |
| debug | no | `boolean` | Debug mode (email are sent!, default: `false`) | |
| level | no | `string` | Define the level of the mail (default: `info`, values: `info`, `success`, `warning`, `failure`) | |
### Example
### Examples
```
```yaml
steps:
mail:
image: deblan/woodpecker-email
@ -34,10 +35,11 @@ steps:
from:
address: "woodpecker@example.com"
name: "Woodpecker"
evaluate: "prev_pipeline.status == 'failure'"
evaluate: 'commit.branch == "master"'
recipients:
- dev1@example.com
- dev2@example.com
level: success
recipients_only: false
content:
subject: "[{{ pipeline.status }}] {{ repo.full_name }} ({{ commit.branch }} - {{ commit.sha[0:8] }}"
@ -49,6 +51,18 @@ steps:
- log/*
```
```yaml
steps:
mail:
image: deblan/woodpecker-email
settings:
level: failure
# ...
when:
status:
- failure
```
### Evaluation and content
See the [Twig documentation](https://twig.symfony.com/doc/3.x/).

View file

@ -87,6 +87,7 @@ $config = EnvVarLoader::buildArray([
'attachments' => 'PLUGIN_ATTACHMENTS',
'evaluate' => 'PLUGIN_EVALUATE',
'content' => 'PLUGIN_CONTENT',
'level' => 'PLUGIN_LEVEL',
'is_debug' => 'PLUGIN_DEBUG',
], [
'PLUGIN_RECIPIENTS_ONLY' => false,

View file

@ -28,13 +28,17 @@ class EmailFactory
{
$from = json_decode($this->config['from'], true);
$content = json_decode($this->config['content'], true);
$level = isset($this->config['level']) && is_string($this->config['level'])
? $this->config['level']
: 'info'
;
$subject = $this->twig->createTemplate(
$content['subject'] ?? '{{ repo.full_name }} ({{ commit.branch }} - {{ commit.sha[0:8] }})'
$content['subject'] ?? '[{{ level }}] {{ repo.full_name }} ({{ commit.branch }} - {{ commit.sha[0:8] }})'
);
$email = (new Email())
->subject($subject->render($this->build))
->subject($subject->render(array_merge($this->build, ['level' => $level])))
->from(
new Address(
$from['address'] ?? '',
@ -68,6 +72,7 @@ class EmailFactory
$email->html($this->twig->render('build_status.html.twig', [
'build' => $this->build,
'level' => $level,
'body' => $content['body'] ?? null,
]));

View file

@ -11,6 +11,7 @@
box-sizing: border-box;
font-size: 14px;
}
body {
-webkit-font-smoothing: antialiased;
-webkit-text-size-adjust: none;
@ -19,13 +20,16 @@
line-height: 1.6;
background-color: #f6f6f6;
}
table td {
vertical-align: top;
}
.body-wrap {
background-color: #f6f6f6;
width: 100%;
}
.container {
display: block !important;
max-width: 600px !important;
@ -33,27 +37,33 @@
/* makes it centered */
clear: both !important;
}
.content {
max-width: 600px;
margin: 0 auto;
display: block;
padding: 20px;
}
.main {
background: #fff;
border: 1px solid #e9e9e9;
border-radius: 3px;
}
.content-wrap {
padding: 20px;
}
.content-block {
padding: 0 0 20px;
}
.header {
width: 100%;
margin-bottom: 20px;
}
h1, h2, h3 {
font-family: "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
color: #000;
@ -61,59 +71,74 @@
line-height: 1.2;
font-weight: 400;
}
h1 {
font-size: 32px;
font-weight: 500;
}
h2 {
font-size: 24px;
}
h3 {
font-size: 18px;
}
hr {
border: 1px solid #e9e9e9;
margin: 20px 0;
height: 1px;
padding: 0;
}
p,
ul,
ol {
margin-bottom: 10px;
font-weight: normal;
}
p li,
ul li,
ol li {
margin-left: 5px;
list-style-position: inside;
}
a {
color: #348eda;
text-decoration: underline;
}
.last {
margin-bottom: 0;
}
.first {
margin-top: 0;
}
.padding {
padding: 10px 0;
}
.aligncenter {
text-align: center;
}
.alignright {
text-align: right;
}
.alignleft {
text-align: left;
}
.clear {
clear: both;
}
.alert {
font-size: 16px;
color: #fff;
@ -122,24 +147,30 @@
text-align: center;
border-radius: 3px 3px 0 0;
}
.alert a {
color: #fff;
text-decoration: none;
font-weight: 500;
font-size: 16px;
}
.alert.alert-warning {
background: #ff9f00;
}
.alert.alert-bad {
.alert.alert-failure {
background: #d0021b;
}
.alert.alert-good {
.alert.alert-success {
background: #68b90f;
}
.alert.alert-info {
background: #117eb9;
}
@media only screen and (max-width: 640px) {
h1,
h2,
@ -147,18 +178,23 @@
font-weight: 600 !important;
margin: 20px 0 5px !important;
}
h1 {
font-size: 22px !important;
}
h2 {
font-size: 18px !important;
}
h3 {
font-size: 16px !important;
}
.container {
width: 100% !important;
}
.content,
.content-wrapper {
padding: 10px !important;

View file

@ -8,28 +8,12 @@
<div class="content">
<table class="main" width="100%" cellpadding="0" cellspacing="0">
<tr>
<td class="alert alert-info">
<td class="alert alert-{{ level }}">
{{ level|capitalize }} -
<a href="{{ build.pipeline.url }}">
Pipeline #{{ build.pipeline.number }}
</a>
</td>
{# REGRESSION #}
{#
{% if build.pipeline.status == 'success' %}
<td class="alert alert-good">
<a href="{{ build.pipeline.url }}">
Successful pipeline #{{ build.pipeline.number }}
</a>
</td>
{% else %}
<td class="alert alert-bad">
<a href="{{ build.pipeline.url }}">
Failed pipeline #{{ build.pipeline.number }}
</a>
</td>
{% endif %}
#}
</tr>
<tr>
<td class="content-wrap">