cloudru-qa-pytest-notifier 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cloudru_qa_pytest_notifier-1.0.0.dist-info/METADATA +163 -0
- cloudru_qa_pytest_notifier-1.0.0.dist-info/RECORD +15 -0
- cloudru_qa_pytest_notifier-1.0.0.dist-info/WHEEL +4 -0
- cloudru_qa_pytest_notifier-1.0.0.dist-info/entry_points.txt +2 -0
- cloudru_qa_pytest_notifier-1.0.0.dist-info/licenses/LICENSE +202 -0
- pytest_notifier/__init__.py +0 -0
- pytest_notifier/models.py +30 -0
- pytest_notifier/notifiers/__init__.py +20 -0
- pytest_notifier/notifiers/base.py +23 -0
- pytest_notifier/notifiers/console.py +58 -0
- pytest_notifier/notifiers/mattermost.py +84 -0
- pytest_notifier/notifiers/telegram.py +78 -0
- pytest_notifier/plugin.py +131 -0
- pytest_notifier/py.typed +0 -0
- pytest_notifier/settings.py +72 -0
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: cloudru-qa-pytest-notifier
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Pytest-плагин для уведомлений о результатах прогона тестов
|
|
5
|
+
Project-URL: Repository, https://github.com/cloud-ru-tech/qa-libraries
|
|
6
|
+
Project-URL: Issues, https://github.com/cloud-ru-tech/qa-libraries/issues
|
|
7
|
+
Author-email: Алексей Кирютин <ayukiryutin@cloud.ru>
|
|
8
|
+
License-Expression: Apache-2.0
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Framework :: Pytest
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
16
|
+
Classifier: Topic :: Software Development :: Testing
|
|
17
|
+
Classifier: Typing :: Typed
|
|
18
|
+
Requires-Python: >=3.13
|
|
19
|
+
Requires-Dist: httpx<0.29,>=0.27.0
|
|
20
|
+
Requires-Dist: pydantic-settings<3,>=2.6.0
|
|
21
|
+
Requires-Dist: pydantic<3,>=2.7.0
|
|
22
|
+
Requires-Dist: pytest<10,>=7.0
|
|
23
|
+
Requires-Dist: rich<16,>=14.2.0
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
|
|
26
|
+
# pytest-notifier
|
|
27
|
+
|
|
28
|
+
[](https://pypi.org/project/cloudru-qa-pytest-notifier/)
|
|
29
|
+
[](https://pypi.org/project/cloudru-qa-pytest-notifier/)
|
|
30
|
+
[](https://github.com/cloud-ru-tech/qa-libraries/blob/master/LICENSE)
|
|
31
|
+
|
|
32
|
+
English | [Русский](README.ru.md)
|
|
33
|
+
|
|
34
|
+
A pytest plugin that sends notifications about failed tests to different channels (Console, Mattermost, Telegram).
|
|
35
|
+
|
|
36
|
+
## Features
|
|
37
|
+
|
|
38
|
+
- Notifications when tests fail.
|
|
39
|
+
- Support for multiple channels at once.
|
|
40
|
+
- Flexible configuration through environment variables or `.env` files.
|
|
41
|
+
- Detailed error information, including tracebacks.
|
|
42
|
+
- Custom messages appended to notifications (for Telegram and Mattermost).
|
|
43
|
+
|
|
44
|
+
## Installation
|
|
45
|
+
|
|
46
|
+
Requires Python 3.13 or later.
|
|
47
|
+
|
|
48
|
+
```shell
|
|
49
|
+
pip install cloudru-qa-pytest-notifier
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Import the plugin as the `pytest_notifier` module.
|
|
53
|
+
|
|
54
|
+
## Skills for AI agents
|
|
55
|
+
|
|
56
|
+
This library does not have its own skill yet. If your project has other
|
|
57
|
+
libraries from this collection with [skills for AI agents](https://github.com/cloud-ru-tech/qa-libraries#skills-for-ai-agents),
|
|
58
|
+
select and install them from the project root (requires [`uv`](https://docs.astral.sh/uv/)):
|
|
59
|
+
|
|
60
|
+
```shell
|
|
61
|
+
uvx library-skills
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
For Claude Code, add `--claude`.
|
|
65
|
+
|
|
66
|
+
## Usage
|
|
67
|
+
|
|
68
|
+
The plugin activates automatically when running `pytest` if installed.
|
|
69
|
+
|
|
70
|
+
## Configuration
|
|
71
|
+
|
|
72
|
+
Configure it through environment variables. All variables use the `PYTEST_NOTIFIER_` prefix.
|
|
73
|
+
|
|
74
|
+
### Main settings
|
|
75
|
+
|
|
76
|
+
`PYTEST_NOTIFIER_DESTINATIONS` is the main variable specifying notification destinations (a bitmask).
|
|
77
|
+
|
|
78
|
+
#### Calculating DESTINATIONS
|
|
79
|
+
|
|
80
|
+
Add the flags for the required channels:
|
|
81
|
+
|
|
82
|
+
| Channel | Flag value |
|
|
83
|
+
|----------------|:--------------:|
|
|
84
|
+
| **Console** | `1` |
|
|
85
|
+
| **Mattermost** | `2` |
|
|
86
|
+
| **Telegram** | `4` |
|
|
87
|
+
|
|
88
|
+
**Example combinations:**
|
|
89
|
+
|
|
90
|
+
| Combination | Calculation | Result |
|
|
91
|
+
|-----------------------|-----------|:-----------------:|
|
|
92
|
+
| Console only | 1 | `1` |
|
|
93
|
+
| Mattermost only | 2 | `2` |
|
|
94
|
+
| Telegram only | 4 | `4` |
|
|
95
|
+
| Console + Mattermost | 1 + 2 | `3` |
|
|
96
|
+
| Console + Telegram | 1 + 4 | `5` |
|
|
97
|
+
| Mattermost + Telegram | 2 + 4 | `6` |
|
|
98
|
+
| **All channels** | 1 + 2 + 4 | `7` |
|
|
99
|
+
|
|
100
|
+
### Mattermost
|
|
101
|
+
|
|
102
|
+
| Variable | Description |
|
|
103
|
+
|-------------------------------------------------|----------------------------------------------------------------------------|
|
|
104
|
+
| `PYTEST_NOTIFIER_MATTERMOST__WEBHOOK_URL` | Webhook URL for sending messages. |
|
|
105
|
+
| `PYTEST_NOTIFIER_MATTERMOST__USERNAME` | Bot name. |
|
|
106
|
+
| `PYTEST_NOTIFIER_MATTERMOST__CHANNEL` | Channel to send messages to. |
|
|
107
|
+
| `PYTEST_NOTIFIER_MATTERMOST__ICON_EMOJI` | Bot icon emoji (optional, for example `:ghost:`). |
|
|
108
|
+
| `PYTEST_NOTIFIER_MATTERMOST__TIMEOUT_SECONDS` | Request timeout in seconds. Default: 10 seconds. |
|
|
109
|
+
| `PYTEST_NOTIFIER_MATTERMOST__SEND_TRACEBACK` | Whether to send the error traceback: `true` or `false`. Default: `true`. |
|
|
110
|
+
| `PYTEST_NOTIFIER_MATTERMOST__ADDITIONAL_MESSAGE` | Extra message appended to the notification. |
|
|
111
|
+
|
|
112
|
+
### Telegram
|
|
113
|
+
|
|
114
|
+
| Variable | Description |
|
|
115
|
+
|-----------------------------------------------|----------------------------------------------------------------------------|
|
|
116
|
+
| `PYTEST_NOTIFIER_TELEGRAM__BOT_TOKEN` | Your bot's token. |
|
|
117
|
+
| `PYTEST_NOTIFIER_TELEGRAM__CHAT_ID` | Chat or channel ID to send messages to. |
|
|
118
|
+
| `PYTEST_NOTIFIER_TELEGRAM__TIMEOUT_SECONDS` | Request timeout in seconds. Default: 10.0. |
|
|
119
|
+
| `PYTEST_NOTIFIER_TELEGRAM__SEND_TRACEBACK` | Whether to send the error traceback: `true` or `false`. Default: `true`. |
|
|
120
|
+
| `PYTEST_NOTIFIER_TELEGRAM__ADDITIONAL_MESSAGE` | Extra message appended to the notification. |
|
|
121
|
+
|
|
122
|
+
### Example `.env` file
|
|
123
|
+
|
|
124
|
+
```ini
|
|
125
|
+
# Enable Mattermost and Telegram (2 + 4 = 6)
|
|
126
|
+
PYTEST_NOTIFIER_DESTINATIONS=6
|
|
127
|
+
|
|
128
|
+
# Mattermost
|
|
129
|
+
PYTEST_NOTIFIER_MATTERMOST__WEBHOOK_URL=https://mattermost.example.com/hooks/xxx
|
|
130
|
+
PYTEST_NOTIFIER_MATTERMOST__USERNAME=TestBot
|
|
131
|
+
PYTEST_NOTIFIER_MATTERMOST__CHANNEL=qa-alerts
|
|
132
|
+
PYTEST_NOTIFIER_MATTERMOST__ADDITIONAL_MESSAGE="Documentation: https://docs.example.com"
|
|
133
|
+
|
|
134
|
+
# Telegram
|
|
135
|
+
PYTEST_NOTIFIER_TELEGRAM__BOT_TOKEN=123456789:ABCdefGHIjklMNOpqrsTUVwxyz
|
|
136
|
+
PYTEST_NOTIFIER_TELEGRAM__CHAT_ID=-1001234567890
|
|
137
|
+
PYTEST_NOTIFIER_TELEGRAM__SEND_TRACEBACK=false
|
|
138
|
+
PYTEST_NOTIFIER_TELEGRAM__ADDITIONAL_MESSAGE="Contact the QA team for details"
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## Development
|
|
142
|
+
|
|
143
|
+
The plugin is developed in the [qa-libraries](https://github.com/cloud-ru-tech/qa-libraries) monorepo
|
|
144
|
+
alongside the other `cloudru-qa-*` libraries. You need [`uv`](https://docs.astral.sh/uv/), GNU Make,
|
|
145
|
+
and [`checkmake`](https://github.com/checkmake/checkmake).
|
|
146
|
+
Run commands from the repository root:
|
|
147
|
+
|
|
148
|
+
```shell
|
|
149
|
+
make sync # install dependencies for all libraries from uv.lock
|
|
150
|
+
make test-pytest-notifier # tests for this plugin
|
|
151
|
+
make typecheck-pytest-notifier # mypy for this plugin
|
|
152
|
+
make check # formatting, ruff, mypy, and tests for the entire workspace
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
The plugin loads in every monorepo test run, so `make` always sets
|
|
156
|
+
`PYTEST_NOTIFIER_DESTINATIONS=1`: notifications go only to the console, even if `.env`
|
|
157
|
+
configures Mattermost or Telegram.
|
|
158
|
+
|
|
159
|
+
Tests run on Python 3.13 and 3.14. Create a separate environment for another version:
|
|
160
|
+
`make sync test-pytest-notifier PYTHON_VERSION=3.14`.
|
|
161
|
+
|
|
162
|
+
Publish a release with the git tag `pytest-notifier/v<version>`. The tag version must match the version
|
|
163
|
+
in `packages/pytest-notifier/pyproject.toml`.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
pytest_notifier/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
pytest_notifier/models.py,sha256=Q2ygyS5iXP5D1SwgXTjVmLlihNUTRVYJk-dHWJnmnKQ,601
|
|
3
|
+
pytest_notifier/plugin.py,sha256=fw2NpNgCohd9BBlbpKQ4xU5IA4tNsfsfEN00uUrKtEc,4000
|
|
4
|
+
pytest_notifier/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
5
|
+
pytest_notifier/settings.py,sha256=gbJNYp59jH0Z1qVgzhQ7IFl_9DejY0nrzoLfV8BnfJM,2045
|
|
6
|
+
pytest_notifier/notifiers/__init__.py,sha256=9S0lBmvZTPiBrzOEIoGTL7ioJa7lY1_Ie62rClR6Mq8,536
|
|
7
|
+
pytest_notifier/notifiers/base.py,sha256=rKDg_rQxaaMat4hAcIB8Mxo8aTwwnszVsPtIKd0OpM8,636
|
|
8
|
+
pytest_notifier/notifiers/console.py,sha256=-QBFnIjYOwHNEEUfV8C9bRWpuHznZ2_5KN1dLbe6HzM,2044
|
|
9
|
+
pytest_notifier/notifiers/mattermost.py,sha256=iZG46FqWDlBp0JJWkwqM0diEz9RfJ1cvejFic6-cIf4,2786
|
|
10
|
+
pytest_notifier/notifiers/telegram.py,sha256=MDl-ynahOZIULMtTy3HVtMlFzErTgjU2DcwN0kwSCKw,2383
|
|
11
|
+
cloudru_qa_pytest_notifier-1.0.0.dist-info/METADATA,sha256=jwxU0d7ipASt-L4kJhfEvGX3i4v0bul30BOohP1igz8,6606
|
|
12
|
+
cloudru_qa_pytest_notifier-1.0.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
13
|
+
cloudru_qa_pytest_notifier-1.0.0.dist-info/entry_points.txt,sha256=2Fs36bdtxbYDoQ1SnPjUWK1DvKUz1WRdo5b0ZQ3zGAM,52
|
|
14
|
+
cloudru_qa_pytest_notifier-1.0.0.dist-info/licenses/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
|
|
15
|
+
cloudru_qa_pytest_notifier-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
File without changes
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from dataclasses import dataclass, field
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
@dataclass
|
|
5
|
+
class TestFailureInfo:
|
|
6
|
+
"""Information about a failed test or error."""
|
|
7
|
+
|
|
8
|
+
nodeid: str
|
|
9
|
+
name: str
|
|
10
|
+
file_path: str
|
|
11
|
+
error_message: str
|
|
12
|
+
traceback: str | None
|
|
13
|
+
duration: float
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass
|
|
17
|
+
class TestResults:
|
|
18
|
+
"""Test run results."""
|
|
19
|
+
|
|
20
|
+
collected: int
|
|
21
|
+
passed: int
|
|
22
|
+
failed: int
|
|
23
|
+
error: int
|
|
24
|
+
skipped: int
|
|
25
|
+
xfailed: int
|
|
26
|
+
xpassed: int
|
|
27
|
+
exitstatus: int
|
|
28
|
+
exitstatus_text: str
|
|
29
|
+
failures: list[TestFailureInfo] = field(default_factory=list)
|
|
30
|
+
errors: list[TestFailureInfo] = field(default_factory=list)
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
from pytest_notifier.settings import Destinations
|
|
2
|
+
|
|
3
|
+
from .base import NotifierBase
|
|
4
|
+
from .console import ConsoleNotifier
|
|
5
|
+
from .mattermost import MattermostNotifier
|
|
6
|
+
from .telegram import TelegramNotifier
|
|
7
|
+
|
|
8
|
+
__all__ = [
|
|
9
|
+
"NOTIFIER_REGISTRY",
|
|
10
|
+
"ConsoleNotifier",
|
|
11
|
+
"MattermostNotifier",
|
|
12
|
+
"NotifierBase",
|
|
13
|
+
"TelegramNotifier",
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
NOTIFIER_REGISTRY: dict[Destinations, type[NotifierBase]] = {
|
|
17
|
+
Destinations.console: ConsoleNotifier,
|
|
18
|
+
Destinations.mattermost: MattermostNotifier,
|
|
19
|
+
Destinations.telegram: TelegramNotifier,
|
|
20
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
|
|
3
|
+
from pytest_notifier.models import TestResults
|
|
4
|
+
from pytest_notifier.settings import Settings
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class NotifierBase(ABC):
|
|
8
|
+
"""Base class for all notifiers."""
|
|
9
|
+
|
|
10
|
+
def __init__(self, settings: Settings) -> None:
|
|
11
|
+
"""Save notification settings.
|
|
12
|
+
|
|
13
|
+
:param settings: channel settings.
|
|
14
|
+
"""
|
|
15
|
+
self.settings = settings
|
|
16
|
+
|
|
17
|
+
@abstractmethod
|
|
18
|
+
def send(self, results: TestResults) -> None:
|
|
19
|
+
"""Send a notification about test results."""
|
|
20
|
+
|
|
21
|
+
@abstractmethod
|
|
22
|
+
def is_enabled(self) -> bool:
|
|
23
|
+
"""Check whether the notifier is enabled and configured correctly."""
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
from rich.console import Console
|
|
2
|
+
from rich.panel import Panel
|
|
3
|
+
from rich.syntax import Syntax
|
|
4
|
+
from rich.table import Table
|
|
5
|
+
|
|
6
|
+
from pytest_notifier.models import TestResults
|
|
7
|
+
|
|
8
|
+
from .base import NotifierBase
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class ConsoleNotifier(NotifierBase):
|
|
12
|
+
"""Notifier that outputs results to the terminal."""
|
|
13
|
+
|
|
14
|
+
def is_enabled(self) -> bool:
|
|
15
|
+
"""Check the configured channel availability.
|
|
16
|
+
|
|
17
|
+
:return: whether the settings are sufficient for sending.
|
|
18
|
+
"""
|
|
19
|
+
return True # Terminal notifications are always available.
|
|
20
|
+
|
|
21
|
+
def send(self, results: TestResults) -> None:
|
|
22
|
+
"""Print information about failed tests to the console."""
|
|
23
|
+
console = Console()
|
|
24
|
+
|
|
25
|
+
# Header
|
|
26
|
+
parts = []
|
|
27
|
+
if results.failed:
|
|
28
|
+
parts.append(f"Failed tests: {results.failed}")
|
|
29
|
+
if results.error:
|
|
30
|
+
parts.append(f"Errors: {results.error}")
|
|
31
|
+
|
|
32
|
+
title = f"[bold red]{' | '.join(parts)}[/bold red]"
|
|
33
|
+
console.print(Panel(title, expand=False))
|
|
34
|
+
|
|
35
|
+
all_failures = results.failures + results.errors
|
|
36
|
+
for idx, failure in enumerate(all_failures, 1):
|
|
37
|
+
# Test information
|
|
38
|
+
table = Table(show_header=False, box=None)
|
|
39
|
+
table.add_row("[bold]Test:[/bold]", f"[cyan]{failure.nodeid}[/cyan]")
|
|
40
|
+
table.add_row("[bold]File:[/bold]", failure.file_path)
|
|
41
|
+
table.add_row("[bold]Duration:[/bold]", f"{failure.duration:.2f}s")
|
|
42
|
+
table.add_row("[bold]Error:[/bold]", f"[red]{failure.error_message}[/red]")
|
|
43
|
+
|
|
44
|
+
console.print(f"\n[bold white on red] {idx} [/bold white on red] {failure.name}")
|
|
45
|
+
console.print(table)
|
|
46
|
+
|
|
47
|
+
if failure.traceback:
|
|
48
|
+
console.print("\n[bold]Traceback:[/bold]")
|
|
49
|
+
syntax = Syntax(
|
|
50
|
+
failure.traceback,
|
|
51
|
+
"python",
|
|
52
|
+
theme="monokai",
|
|
53
|
+
line_numbers=True,
|
|
54
|
+
background_color="default",
|
|
55
|
+
)
|
|
56
|
+
console.print(syntax)
|
|
57
|
+
|
|
58
|
+
console.print("\n")
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import httpx
|
|
2
|
+
|
|
3
|
+
from pytest_notifier.models import TestFailureInfo, TestResults
|
|
4
|
+
from pytest_notifier.settings import Settings
|
|
5
|
+
|
|
6
|
+
from .base import NotifierBase
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class MattermostNotifier(NotifierBase):
|
|
10
|
+
"""Notifier that sends results to Mattermost through a Slack-compatible webhook."""
|
|
11
|
+
|
|
12
|
+
def __init__(self, settings: Settings) -> None:
|
|
13
|
+
"""Save notification settings.
|
|
14
|
+
|
|
15
|
+
:param settings: channel settings.
|
|
16
|
+
"""
|
|
17
|
+
super().__init__(settings)
|
|
18
|
+
self.mm = settings.mattermost
|
|
19
|
+
|
|
20
|
+
def is_enabled(self) -> bool:
|
|
21
|
+
"""Check the configured channel availability.
|
|
22
|
+
|
|
23
|
+
:return: whether the settings are sufficient for sending.
|
|
24
|
+
"""
|
|
25
|
+
return self.mm.webhook_url is not None
|
|
26
|
+
|
|
27
|
+
def send(self, results: TestResults) -> None:
|
|
28
|
+
"""Send results to Mattermost."""
|
|
29
|
+
all_failures = results.failures + results.errors
|
|
30
|
+
for failure in all_failures:
|
|
31
|
+
payload = self._build_failure_payload(failure)
|
|
32
|
+
self._post_to_webhook(payload)
|
|
33
|
+
|
|
34
|
+
def _build_failure_payload(self, failure: TestFailureInfo) -> dict[str, object]:
|
|
35
|
+
"""Create a payload for a specific failed test."""
|
|
36
|
+
payload = self._build_base_payload()
|
|
37
|
+
payload["text"] = "" # All content will be in attachments.
|
|
38
|
+
|
|
39
|
+
pretext = f"Test failed: **{failure.name}**"
|
|
40
|
+
if self.mm.additional_message:
|
|
41
|
+
pretext += "\n"
|
|
42
|
+
pretext += self.mm.additional_message
|
|
43
|
+
|
|
44
|
+
details = [
|
|
45
|
+
f"nodeid: `{failure.nodeid}`",
|
|
46
|
+
f"duration: {failure.duration:.2f}s",
|
|
47
|
+
f"failure:\n```\n{failure.error_message}\n```",
|
|
48
|
+
]
|
|
49
|
+
|
|
50
|
+
if failure.traceback and self.mm.send_traceback:
|
|
51
|
+
details.append("")
|
|
52
|
+
details.append("traceback:")
|
|
53
|
+
details.append(f"```\n{failure.traceback}\n```")
|
|
54
|
+
|
|
55
|
+
attachment = {
|
|
56
|
+
"color": "#FF0000",
|
|
57
|
+
"pretext": pretext,
|
|
58
|
+
"text": "\n".join(details),
|
|
59
|
+
"mrkdwn_in": ["pretext", "text"],
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
payload["attachments"] = [attachment]
|
|
63
|
+
|
|
64
|
+
return payload
|
|
65
|
+
|
|
66
|
+
def _build_base_payload(self) -> dict[str, object]:
|
|
67
|
+
"""Create a base payload with shared settings (username, channel, icon)."""
|
|
68
|
+
payload: dict[str, object] = {}
|
|
69
|
+
|
|
70
|
+
if self.mm.username:
|
|
71
|
+
payload["username"] = self.mm.username
|
|
72
|
+
if self.mm.icon_emoji:
|
|
73
|
+
payload["icon_emoji"] = self.mm.icon_emoji
|
|
74
|
+
if self.mm.channel:
|
|
75
|
+
payload["channel"] = self.mm.channel
|
|
76
|
+
|
|
77
|
+
return payload
|
|
78
|
+
|
|
79
|
+
def _post_to_webhook(self, payload: dict[str, object]) -> None:
|
|
80
|
+
url = str(self.mm.webhook_url)
|
|
81
|
+
timeout = self.mm.timeout_seconds
|
|
82
|
+
|
|
83
|
+
with httpx.Client(timeout=timeout) as client:
|
|
84
|
+
client.post(url, json=payload).raise_for_status()
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import httpx
|
|
2
|
+
|
|
3
|
+
from pytest_notifier.models import TestResults
|
|
4
|
+
from pytest_notifier.settings import Settings
|
|
5
|
+
|
|
6
|
+
from .base import NotifierBase
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class TelegramNotifier(NotifierBase):
|
|
10
|
+
"""Notifier that sends results to Telegram through the Bot API."""
|
|
11
|
+
|
|
12
|
+
BASE_URL = "https://api.telegram.org/bot"
|
|
13
|
+
|
|
14
|
+
def __init__(self, settings: Settings) -> None:
|
|
15
|
+
"""Save notification settings.
|
|
16
|
+
|
|
17
|
+
:param settings: channel settings.
|
|
18
|
+
"""
|
|
19
|
+
super().__init__(settings)
|
|
20
|
+
self.tg = settings.telegram
|
|
21
|
+
|
|
22
|
+
def is_enabled(self) -> bool:
|
|
23
|
+
"""Check the configured channel availability.
|
|
24
|
+
|
|
25
|
+
:return: whether the settings are sufficient for sending.
|
|
26
|
+
"""
|
|
27
|
+
return self.tg.bot_token is not None and self.tg.chat_id is not None
|
|
28
|
+
|
|
29
|
+
def send(self, results: TestResults) -> None:
|
|
30
|
+
"""Send messages about failed tests.
|
|
31
|
+
|
|
32
|
+
:param results: run results.
|
|
33
|
+
"""
|
|
34
|
+
all_failures = results.failures + results.errors
|
|
35
|
+
for _, failure in enumerate(all_failures, 1):
|
|
36
|
+
lines = [
|
|
37
|
+
f"Test failed: *{failure.name}*",
|
|
38
|
+
"",
|
|
39
|
+
f"nodeid: `{failure.nodeid}`",
|
|
40
|
+
f"duration: {failure.duration:.2f}s",
|
|
41
|
+
"failure:",
|
|
42
|
+
f"```\n{failure.error_message}\n```",
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
if failure.traceback and self.settings.telegram.send_traceback:
|
|
46
|
+
lines.extend(
|
|
47
|
+
[
|
|
48
|
+
"",
|
|
49
|
+
"traceback:",
|
|
50
|
+
f"```\n{failure.traceback}\n```",
|
|
51
|
+
]
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
if self.tg.additional_message:
|
|
55
|
+
lines.extend(
|
|
56
|
+
[
|
|
57
|
+
"",
|
|
58
|
+
self.tg.additional_message,
|
|
59
|
+
]
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
message = "\n".join(lines)
|
|
63
|
+
self._send_message(message)
|
|
64
|
+
|
|
65
|
+
def _send_message(self, text: str) -> None:
|
|
66
|
+
if self.tg.bot_token is None:
|
|
67
|
+
return
|
|
68
|
+
bot_token = self.tg.bot_token.get_secret_value()
|
|
69
|
+
url = f"{self.BASE_URL}{bot_token}/sendMessage"
|
|
70
|
+
|
|
71
|
+
payload = {
|
|
72
|
+
"chat_id": self.tg.chat_id,
|
|
73
|
+
"text": text,
|
|
74
|
+
"parse_mode": "Markdown",
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
with httpx.Client(timeout=self.tg.timeout_seconds) as client:
|
|
78
|
+
client.post(url, json=payload).raise_for_status()
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import contextlib
|
|
2
|
+
from typing import TYPE_CHECKING, cast
|
|
3
|
+
|
|
4
|
+
import pytest
|
|
5
|
+
from _pytest._code.code import ExceptionRepr
|
|
6
|
+
|
|
7
|
+
from .models import TestFailureInfo, TestResults
|
|
8
|
+
from .notifiers import NOTIFIER_REGISTRY, NotifierBase
|
|
9
|
+
from .settings import Settings
|
|
10
|
+
|
|
11
|
+
if TYPE_CHECKING:
|
|
12
|
+
from collections.abc import Mapping, Sequence
|
|
13
|
+
|
|
14
|
+
from _pytest.terminal import TerminalReporter
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def pytest_sessionfinish(session: pytest.Session, exitstatus: int) -> None:
|
|
18
|
+
"""Pytest hook that sends a result summary to all active channels."""
|
|
19
|
+
settings = Settings()
|
|
20
|
+
results = extract_test_results(session, exitstatus)
|
|
21
|
+
|
|
22
|
+
if results.failed == 0 and results.error == 0:
|
|
23
|
+
return
|
|
24
|
+
|
|
25
|
+
notifiers = get_active_notifiers(settings)
|
|
26
|
+
|
|
27
|
+
for notifier in notifiers:
|
|
28
|
+
with contextlib.suppress(Exception):
|
|
29
|
+
notifier.send(results)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def extract_test_results(session: pytest.Session, exitstatus: int) -> TestResults:
|
|
33
|
+
"""Extract results from pytest.Session into an independent structure."""
|
|
34
|
+
tr = cast("TerminalReporter | None", session.config.pluginmanager.get_plugin("terminalreporter"))
|
|
35
|
+
|
|
36
|
+
passed = failed = error = skipped = xfailed = xpassed = 0
|
|
37
|
+
collected = session.testscollected
|
|
38
|
+
failures: list[TestFailureInfo] = []
|
|
39
|
+
errors: list[TestFailureInfo] = []
|
|
40
|
+
|
|
41
|
+
if tr is not None:
|
|
42
|
+
stats = cast("Mapping[str, Sequence[pytest.TestReport]]", tr.stats)
|
|
43
|
+
passed = len(stats.get("passed", []))
|
|
44
|
+
failed = len(stats.get("failed", []))
|
|
45
|
+
error = len(stats.get("error", []))
|
|
46
|
+
skipped = len(stats.get("skipped", []))
|
|
47
|
+
xfailed = len(stats.get("xfailed", []))
|
|
48
|
+
xpassed = len(stats.get("xpassed", []))
|
|
49
|
+
|
|
50
|
+
failures.extend(extract_failure_info(report) for report in stats.get("failed", []))
|
|
51
|
+
|
|
52
|
+
errors.extend(extract_failure_info(report) for report in stats.get("error", []))
|
|
53
|
+
|
|
54
|
+
exitstatus_text = exitstatus_to_text(exitstatus)
|
|
55
|
+
|
|
56
|
+
return TestResults(
|
|
57
|
+
collected=collected,
|
|
58
|
+
passed=passed,
|
|
59
|
+
failed=failed,
|
|
60
|
+
error=error,
|
|
61
|
+
skipped=skipped,
|
|
62
|
+
xfailed=xfailed,
|
|
63
|
+
xpassed=xpassed,
|
|
64
|
+
exitstatus=exitstatus,
|
|
65
|
+
exitstatus_text=exitstatus_text,
|
|
66
|
+
failures=failures,
|
|
67
|
+
errors=errors,
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def get_active_notifiers(settings: Settings) -> list[NotifierBase]:
|
|
72
|
+
"""Return active notifiers based on the destinations flags."""
|
|
73
|
+
notifiers = []
|
|
74
|
+
|
|
75
|
+
for destination, notifier_class in NOTIFIER_REGISTRY.items():
|
|
76
|
+
if settings.destinations & destination:
|
|
77
|
+
notifier = notifier_class(settings)
|
|
78
|
+
if notifier.is_enabled():
|
|
79
|
+
notifiers.append(notifier)
|
|
80
|
+
|
|
81
|
+
return notifiers
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def extract_failure_info(report: pytest.TestReport) -> TestFailureInfo:
|
|
85
|
+
"""Extract error information from TestReport."""
|
|
86
|
+
file_path, _line, test_name = report.location
|
|
87
|
+
error_message = get_short_error_message(report)
|
|
88
|
+
traceback = report.longreprtext
|
|
89
|
+
|
|
90
|
+
return TestFailureInfo(
|
|
91
|
+
nodeid=report.nodeid,
|
|
92
|
+
name=test_name,
|
|
93
|
+
file_path=file_path,
|
|
94
|
+
error_message=error_message,
|
|
95
|
+
traceback=traceback,
|
|
96
|
+
duration=report.duration,
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def get_short_error_message(report: pytest.TestReport) -> str:
|
|
101
|
+
"""Extract a short error message from the report."""
|
|
102
|
+
longrepr = report.longrepr
|
|
103
|
+
|
|
104
|
+
if longrepr is None:
|
|
105
|
+
return "Unknown error"
|
|
106
|
+
|
|
107
|
+
if isinstance(longrepr, str):
|
|
108
|
+
return longrepr[:200]
|
|
109
|
+
|
|
110
|
+
reprcrash = longrepr.reprcrash if isinstance(longrepr, ExceptionRepr) else None
|
|
111
|
+
if reprcrash:
|
|
112
|
+
message = reprcrash.message
|
|
113
|
+
if message:
|
|
114
|
+
return message[:200]
|
|
115
|
+
|
|
116
|
+
longreprtext = report.longreprtext
|
|
117
|
+
if longreprtext:
|
|
118
|
+
first_line = longreprtext.split("\n")[0]
|
|
119
|
+
return first_line[:200]
|
|
120
|
+
|
|
121
|
+
return "Unknown error"
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def exitstatus_to_text(exitstatus: int) -> str:
|
|
125
|
+
"""Convert a pytest exit code to text."""
|
|
126
|
+
try:
|
|
127
|
+
code = pytest.ExitCode(exitstatus)
|
|
128
|
+
except ValueError:
|
|
129
|
+
return str(exitstatus)
|
|
130
|
+
else:
|
|
131
|
+
return code.name
|
pytest_notifier/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from enum import Flag
|
|
3
|
+
|
|
4
|
+
from pydantic import AnyUrl, BaseModel, SecretStr, field_validator
|
|
5
|
+
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
6
|
+
|
|
7
|
+
env = os.environ.get("ENV", "dev")
|
|
8
|
+
|
|
9
|
+
DEFAULT_TIMEOUT_SECONDS = 10.0
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class Destinations(Flag):
|
|
13
|
+
"""Notification destinations."""
|
|
14
|
+
|
|
15
|
+
console = 0x1
|
|
16
|
+
mattermost = 0x2
|
|
17
|
+
telegram = 0x4
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class MattermostSettings(BaseModel):
|
|
21
|
+
"""Mattermost webhook settings."""
|
|
22
|
+
|
|
23
|
+
webhook_url: AnyUrl | None = None
|
|
24
|
+
icon_emoji: str | None = None
|
|
25
|
+
username: str | None = None
|
|
26
|
+
channel: str | None = None
|
|
27
|
+
timeout_seconds: float = DEFAULT_TIMEOUT_SECONDS
|
|
28
|
+
send_traceback: bool = True
|
|
29
|
+
additional_message: str | None = None
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class TelegramSettings(BaseModel):
|
|
33
|
+
"""Telegram notification settings."""
|
|
34
|
+
|
|
35
|
+
bot_token: SecretStr | None = None
|
|
36
|
+
chat_id: str | None = None
|
|
37
|
+
timeout_seconds: float = DEFAULT_TIMEOUT_SECONDS
|
|
38
|
+
send_traceback: bool = True
|
|
39
|
+
additional_message: str | None = None
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class Settings(BaseSettings):
|
|
43
|
+
"""Notification channel settings from the environment."""
|
|
44
|
+
|
|
45
|
+
destinations: Destinations = Destinations.console
|
|
46
|
+
mattermost: MattermostSettings = MattermostSettings()
|
|
47
|
+
telegram: TelegramSettings = TelegramSettings()
|
|
48
|
+
|
|
49
|
+
model_config = SettingsConfigDict(
|
|
50
|
+
extra="ignore",
|
|
51
|
+
env_prefix="PYTEST_NOTIFIER_",
|
|
52
|
+
env_nested_delimiter="__",
|
|
53
|
+
env_file=[".env", f".{env}.env"],
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
@field_validator("destinations", mode="before")
|
|
57
|
+
@classmethod
|
|
58
|
+
def parse_destinations(cls, v: object) -> Destinations:
|
|
59
|
+
"""Read the channel bitmask.
|
|
60
|
+
|
|
61
|
+
:param v: integer, numeric string, or an existing mask.
|
|
62
|
+
:return: selected channels.
|
|
63
|
+
:raises ValueError: if the value is not a bitmask.
|
|
64
|
+
"""
|
|
65
|
+
if isinstance(v, Destinations):
|
|
66
|
+
return v
|
|
67
|
+
if isinstance(v, str):
|
|
68
|
+
return Destinations(int(v))
|
|
69
|
+
if isinstance(v, int):
|
|
70
|
+
return Destinations(v)
|
|
71
|
+
message = f"Cannot parse destinations from {v}"
|
|
72
|
+
raise ValueError(message)
|