Merge ~axino/charm-canonical-is-reviewbot/+git/charm-canonical-is-reviewbot:axino into charm-canonical-is-reviewbot:main
Proposed by
Junien F
Status: | Merged |
---|---|
Approved by: | Junien F |
Approved revision: | 971bbcf68ce690076c434f1c292c6d71ddd11148 |
Merged at revision: | 9763831c66fb1ba544d4b657de58f8bac49ee979 |
Proposed branch: | ~axino/charm-canonical-is-reviewbot/+git/charm-canonical-is-reviewbot:axino |
Merge into: | charm-canonical-is-reviewbot:main |
Diff against target: |
735 lines (+663/-0) 12 files modified
.gitignore (+9/-0) CONTRIBUTING.md (+33/-0) LICENSE (+202/-0) README.md (+7/-0) charmcraft.yaml (+11/-0) config.yaml (+30/-0) metadata.yaml (+18/-0) pyproject.toml (+39/-0) requirements.txt (+1/-0) src/charm.py (+121/-0) tests/unit/test_charm.py (+108/-0) tox.ini (+84/-0) |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Barry Price | Approve | ||
Review via email: mp+436216@code.launchpad.net |
Commit message
initial commit
Description of the change
To post a comment you must log in.
Revision history for this message
Junien F (axino) wrote : | # |
Fixed, thanks !
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote : | # |
This merge proposal is being monitored by mergebot. Change the status to Approved to merge.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote : | # |
Change has no commit message, setting status to needs review.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote : | # |
Change successfully merged at revision 9763831c66fb1ba
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | diff --git a/.gitignore b/.gitignore | |||
2 | 0 | new file mode 100644 | 0 | new file mode 100644 |
3 | index 0000000..a26d707 | |||
4 | --- /dev/null | |||
5 | +++ b/.gitignore | |||
6 | @@ -0,0 +1,9 @@ | |||
7 | 1 | venv/ | ||
8 | 2 | build/ | ||
9 | 3 | *.charm | ||
10 | 4 | .tox/ | ||
11 | 5 | .coverage | ||
12 | 6 | __pycache__/ | ||
13 | 7 | *.py[cod] | ||
14 | 8 | .idea | ||
15 | 9 | .vscode/ | ||
16 | diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md | |||
17 | 0 | new file mode 100644 | 10 | new file mode 100644 |
18 | index 0000000..2969183 | |||
19 | --- /dev/null | |||
20 | +++ b/CONTRIBUTING.md | |||
21 | @@ -0,0 +1,33 @@ | |||
22 | 1 | # Contributing | ||
23 | 2 | |||
24 | 3 | To make contributions to this charm, you'll need a working [development setup](https://juju.is/docs/sdk/dev-setup). | ||
25 | 4 | |||
26 | 5 | You can use the environments created by `tox` for development: | ||
27 | 6 | |||
28 | 7 | ```shell | ||
29 | 8 | tox --notest -e unit | ||
30 | 9 | source .tox/unit/bin/activate | ||
31 | 10 | ``` | ||
32 | 11 | |||
33 | 12 | ## Testing | ||
34 | 13 | |||
35 | 14 | This project uses `tox` for managing test environments. There are some pre-configured environments | ||
36 | 15 | that can be used for linting and formatting code when you're preparing contributions to the charm: | ||
37 | 16 | |||
38 | 17 | ```shell | ||
39 | 18 | tox -e fmt # update your code according to linting rules | ||
40 | 19 | tox -e lint # code style | ||
41 | 20 | tox -e unit # unit tests | ||
42 | 21 | tox -e integration # integration tests | ||
43 | 22 | tox # runs 'lint' and 'unit' environments | ||
44 | 23 | ``` | ||
45 | 24 | |||
46 | 25 | ## Build the charm | ||
47 | 26 | |||
48 | 27 | Build the charm in this git repository using: | ||
49 | 28 | |||
50 | 29 | ```shell | ||
51 | 30 | charmcraft pack | ||
52 | 31 | ``` | ||
53 | 32 | |||
54 | 33 | <!-- You may want to include any contribution/style guidelines in this document> | ||
55 | diff --git a/LICENSE b/LICENSE | |||
56 | 0 | new file mode 100644 | 34 | new file mode 100644 |
57 | index 0000000..ecbda55 | |||
58 | --- /dev/null | |||
59 | +++ b/LICENSE | |||
60 | @@ -0,0 +1,202 @@ | |||
61 | 1 | |||
62 | 2 | Apache License | ||
63 | 3 | Version 2.0, January 2004 | ||
64 | 4 | http://www.apache.org/licenses/ | ||
65 | 5 | |||
66 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION | ||
67 | 7 | |||
68 | 8 | 1. Definitions. | ||
69 | 9 | |||
70 | 10 | "License" shall mean the terms and conditions for use, reproduction, | ||
71 | 11 | and distribution as defined by Sections 1 through 9 of this document. | ||
72 | 12 | |||
73 | 13 | "Licensor" shall mean the copyright owner or entity authorized by | ||
74 | 14 | the copyright owner that is granting the License. | ||
75 | 15 | |||
76 | 16 | "Legal Entity" shall mean the union of the acting entity and all | ||
77 | 17 | other entities that control, are controlled by, or are under common | ||
78 | 18 | control with that entity. For the purposes of this definition, | ||
79 | 19 | "control" means (i) the power, direct or indirect, to cause the | ||
80 | 20 | direction or management of such entity, whether by contract or | ||
81 | 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the | ||
82 | 22 | outstanding shares, or (iii) beneficial ownership of such entity. | ||
83 | 23 | |||
84 | 24 | "You" (or "Your") shall mean an individual or Legal Entity | ||
85 | 25 | exercising permissions granted by this License. | ||
86 | 26 | |||
87 | 27 | "Source" form shall mean the preferred form for making modifications, | ||
88 | 28 | including but not limited to software source code, documentation | ||
89 | 29 | source, and configuration files. | ||
90 | 30 | |||
91 | 31 | "Object" form shall mean any form resulting from mechanical | ||
92 | 32 | transformation or translation of a Source form, including but | ||
93 | 33 | not limited to compiled object code, generated documentation, | ||
94 | 34 | and conversions to other media types. | ||
95 | 35 | |||
96 | 36 | "Work" shall mean the work of authorship, whether in Source or | ||
97 | 37 | Object form, made available under the License, as indicated by a | ||
98 | 38 | copyright notice that is included in or attached to the work | ||
99 | 39 | (an example is provided in the Appendix below). | ||
100 | 40 | |||
101 | 41 | "Derivative Works" shall mean any work, whether in Source or Object | ||
102 | 42 | form, that is based on (or derived from) the Work and for which the | ||
103 | 43 | editorial revisions, annotations, elaborations, or other modifications | ||
104 | 44 | represent, as a whole, an original work of authorship. For the purposes | ||
105 | 45 | of this License, Derivative Works shall not include works that remain | ||
106 | 46 | separable from, or merely link (or bind by name) to the interfaces of, | ||
107 | 47 | the Work and Derivative Works thereof. | ||
108 | 48 | |||
109 | 49 | "Contribution" shall mean any work of authorship, including | ||
110 | 50 | the original version of the Work and any modifications or additions | ||
111 | 51 | to that Work or Derivative Works thereof, that is intentionally | ||
112 | 52 | submitted to Licensor for inclusion in the Work by the copyright owner | ||
113 | 53 | or by an individual or Legal Entity authorized to submit on behalf of | ||
114 | 54 | the copyright owner. For the purposes of this definition, "submitted" | ||
115 | 55 | means any form of electronic, verbal, or written communication sent | ||
116 | 56 | to the Licensor or its representatives, including but not limited to | ||
117 | 57 | communication on electronic mailing lists, source code control systems, | ||
118 | 58 | and issue tracking systems that are managed by, or on behalf of, the | ||
119 | 59 | Licensor for the purpose of discussing and improving the Work, but | ||
120 | 60 | excluding communication that is conspicuously marked or otherwise | ||
121 | 61 | designated in writing by the copyright owner as "Not a Contribution." | ||
122 | 62 | |||
123 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity | ||
124 | 64 | on behalf of whom a Contribution has been received by Licensor and | ||
125 | 65 | subsequently incorporated within the Work. | ||
126 | 66 | |||
127 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of | ||
128 | 68 | this License, each Contributor hereby grants to You a perpetual, | ||
129 | 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable | ||
130 | 70 | copyright license to reproduce, prepare Derivative Works of, | ||
131 | 71 | publicly display, publicly perform, sublicense, and distribute the | ||
132 | 72 | Work and such Derivative Works in Source or Object form. | ||
133 | 73 | |||
134 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of | ||
135 | 75 | this License, each Contributor hereby grants to You a perpetual, | ||
136 | 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable | ||
137 | 77 | (except as stated in this section) patent license to make, have made, | ||
138 | 78 | use, offer to sell, sell, import, and otherwise transfer the Work, | ||
139 | 79 | where such license applies only to those patent claims licensable | ||
140 | 80 | by such Contributor that are necessarily infringed by their | ||
141 | 81 | Contribution(s) alone or by combination of their Contribution(s) | ||
142 | 82 | with the Work to which such Contribution(s) was submitted. If You | ||
143 | 83 | institute patent litigation against any entity (including a | ||
144 | 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work | ||
145 | 85 | or a Contribution incorporated within the Work constitutes direct | ||
146 | 86 | or contributory patent infringement, then any patent licenses | ||
147 | 87 | granted to You under this License for that Work shall terminate | ||
148 | 88 | as of the date such litigation is filed. | ||
149 | 89 | |||
150 | 90 | 4. Redistribution. You may reproduce and distribute copies of the | ||
151 | 91 | Work or Derivative Works thereof in any medium, with or without | ||
152 | 92 | modifications, and in Source or Object form, provided that You | ||
153 | 93 | meet the following conditions: | ||
154 | 94 | |||
155 | 95 | (a) You must give any other recipients of the Work or | ||
156 | 96 | Derivative Works a copy of this License; and | ||
157 | 97 | |||
158 | 98 | (b) You must cause any modified files to carry prominent notices | ||
159 | 99 | stating that You changed the files; and | ||
160 | 100 | |||
161 | 101 | (c) You must retain, in the Source form of any Derivative Works | ||
162 | 102 | that You distribute, all copyright, patent, trademark, and | ||
163 | 103 | attribution notices from the Source form of the Work, | ||
164 | 104 | excluding those notices that do not pertain to any part of | ||
165 | 105 | the Derivative Works; and | ||
166 | 106 | |||
167 | 107 | (d) If the Work includes a "NOTICE" text file as part of its | ||
168 | 108 | distribution, then any Derivative Works that You distribute must | ||
169 | 109 | include a readable copy of the attribution notices contained | ||
170 | 110 | within such NOTICE file, excluding those notices that do not | ||
171 | 111 | pertain to any part of the Derivative Works, in at least one | ||
172 | 112 | of the following places: within a NOTICE text file distributed | ||
173 | 113 | as part of the Derivative Works; within the Source form or | ||
174 | 114 | documentation, if provided along with the Derivative Works; or, | ||
175 | 115 | within a display generated by the Derivative Works, if and | ||
176 | 116 | wherever such third-party notices normally appear. The contents | ||
177 | 117 | of the NOTICE file are for informational purposes only and | ||
178 | 118 | do not modify the License. You may add Your own attribution | ||
179 | 119 | notices within Derivative Works that You distribute, alongside | ||
180 | 120 | or as an addendum to the NOTICE text from the Work, provided | ||
181 | 121 | that such additional attribution notices cannot be construed | ||
182 | 122 | as modifying the License. | ||
183 | 123 | |||
184 | 124 | You may add Your own copyright statement to Your modifications and | ||
185 | 125 | may provide additional or different license terms and conditions | ||
186 | 126 | for use, reproduction, or distribution of Your modifications, or | ||
187 | 127 | for any such Derivative Works as a whole, provided Your use, | ||
188 | 128 | reproduction, and distribution of the Work otherwise complies with | ||
189 | 129 | the conditions stated in this License. | ||
190 | 130 | |||
191 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, | ||
192 | 132 | any Contribution intentionally submitted for inclusion in the Work | ||
193 | 133 | by You to the Licensor shall be under the terms and conditions of | ||
194 | 134 | this License, without any additional terms or conditions. | ||
195 | 135 | Notwithstanding the above, nothing herein shall supersede or modify | ||
196 | 136 | the terms of any separate license agreement you may have executed | ||
197 | 137 | with Licensor regarding such Contributions. | ||
198 | 138 | |||
199 | 139 | 6. Trademarks. This License does not grant permission to use the trade | ||
200 | 140 | names, trademarks, service marks, or product names of the Licensor, | ||
201 | 141 | except as required for reasonable and customary use in describing the | ||
202 | 142 | origin of the Work and reproducing the content of the NOTICE file. | ||
203 | 143 | |||
204 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or | ||
205 | 145 | agreed to in writing, Licensor provides the Work (and each | ||
206 | 146 | Contributor provides its Contributions) on an "AS IS" BASIS, | ||
207 | 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
208 | 148 | implied, including, without limitation, any warranties or conditions | ||
209 | 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A | ||
210 | 150 | PARTICULAR PURPOSE. You are solely responsible for determining the | ||
211 | 151 | appropriateness of using or redistributing the Work and assume any | ||
212 | 152 | risks associated with Your exercise of permissions under this License. | ||
213 | 153 | |||
214 | 154 | 8. Limitation of Liability. In no event and under no legal theory, | ||
215 | 155 | whether in tort (including negligence), contract, or otherwise, | ||
216 | 156 | unless required by applicable law (such as deliberate and grossly | ||
217 | 157 | negligent acts) or agreed to in writing, shall any Contributor be | ||
218 | 158 | liable to You for damages, including any direct, indirect, special, | ||
219 | 159 | incidental, or consequential damages of any character arising as a | ||
220 | 160 | result of this License or out of the use or inability to use the | ||
221 | 161 | Work (including but not limited to damages for loss of goodwill, | ||
222 | 162 | work stoppage, computer failure or malfunction, or any and all | ||
223 | 163 | other commercial damages or losses), even if such Contributor | ||
224 | 164 | has been advised of the possibility of such damages. | ||
225 | 165 | |||
226 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing | ||
227 | 167 | the Work or Derivative Works thereof, You may choose to offer, | ||
228 | 168 | and charge a fee for, acceptance of support, warranty, indemnity, | ||
229 | 169 | or other liability obligations and/or rights consistent with this | ||
230 | 170 | License. However, in accepting such obligations, You may act only | ||
231 | 171 | on Your own behalf and on Your sole responsibility, not on behalf | ||
232 | 172 | of any other Contributor, and only if You agree to indemnify, | ||
233 | 173 | defend, and hold each Contributor harmless for any liability | ||
234 | 174 | incurred by, or claims asserted against, such Contributor by reason | ||
235 | 175 | of your accepting any such warranty or additional liability. | ||
236 | 176 | |||
237 | 177 | END OF TERMS AND CONDITIONS | ||
238 | 178 | |||
239 | 179 | APPENDIX: How to apply the Apache License to your work. | ||
240 | 180 | |||
241 | 181 | To apply the Apache License to your work, attach the following | ||
242 | 182 | boilerplate notice, with the fields enclosed by brackets "[]" | ||
243 | 183 | replaced with your own identifying information. (Don't include | ||
244 | 184 | the brackets!) The text should be enclosed in the appropriate | ||
245 | 185 | comment syntax for the file format. We also recommend that a | ||
246 | 186 | file or class name and description of purpose be included on the | ||
247 | 187 | same "printed page" as the copyright notice for easier | ||
248 | 188 | identification within third-party archives. | ||
249 | 189 | |||
250 | 190 | Copyright 2022 Ubuntu | ||
251 | 191 | |||
252 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); | ||
253 | 193 | you may not use this file except in compliance with the License. | ||
254 | 194 | You may obtain a copy of the License at | ||
255 | 195 | |||
256 | 196 | http://www.apache.org/licenses/LICENSE-2.0 | ||
257 | 197 | |||
258 | 198 | Unless required by applicable law or agreed to in writing, software | ||
259 | 199 | distributed under the License is distributed on an "AS IS" BASIS, | ||
260 | 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
261 | 201 | See the License for the specific language governing permissions and | ||
262 | 202 | limitations under the License. | ||
263 | diff --git a/README.md b/README.md | |||
264 | 0 | new file mode 100644 | 203 | new file mode 100644 |
265 | index 0000000..6cca19d | |||
266 | --- /dev/null | |||
267 | +++ b/README.md | |||
268 | @@ -0,0 +1,7 @@ | |||
269 | 1 | # charm-canonical-is-reviewbot | ||
270 | 2 | |||
271 | 3 | Charmhub package name: mm-reviewbot-charmers-canonical-is-reviewbot | ||
272 | 4 | More information: https://charmhub.io/mm-reviewbot-charmers-canonical-is-reviewbot | ||
273 | 5 | |||
274 | 6 | This charm deploys the Canonical IS reviewbot, which helps doing reviews of | ||
275 | 7 | Launchpad merge proposals for the IS team. | ||
276 | diff --git a/charmcraft.yaml b/charmcraft.yaml | |||
277 | 0 | new file mode 100644 | 8 | new file mode 100644 |
278 | index 0000000..900d34a | |||
279 | --- /dev/null | |||
280 | +++ b/charmcraft.yaml | |||
281 | @@ -0,0 +1,11 @@ | |||
282 | 1 | # This file configures Charmcraft. | ||
283 | 2 | # See https://juju.is/docs/sdk/charmcraft-config for guidance. | ||
284 | 3 | |||
285 | 4 | type: charm | ||
286 | 5 | bases: | ||
287 | 6 | - build-on: | ||
288 | 7 | - name: ubuntu | ||
289 | 8 | channel: "22.04" | ||
290 | 9 | run-on: | ||
291 | 10 | - name: ubuntu | ||
292 | 11 | channel: "22.04" | ||
293 | diff --git a/config.yaml b/config.yaml | |||
294 | 0 | new file mode 100644 | 12 | new file mode 100644 |
295 | index 0000000..79e1a0d | |||
296 | --- /dev/null | |||
297 | +++ b/config.yaml | |||
298 | @@ -0,0 +1,30 @@ | |||
299 | 1 | # This file defines charm config options, and populates the Configure tab on Charmhub. | ||
300 | 2 | # If your charm does not require configuration options, delete this file entirely. | ||
301 | 3 | # | ||
302 | 4 | # See https://juju.is/docs/config for guidance. | ||
303 | 5 | |||
304 | 6 | options: | ||
305 | 7 | launchpad_secret: | ||
306 | 8 | description: "Launchpad credential secret" | ||
307 | 9 | default: "" | ||
308 | 10 | type: string | ||
309 | 11 | launchpad_token: | ||
310 | 12 | description: "Launchpad credential token" | ||
311 | 13 | default: "" | ||
312 | 14 | type: string | ||
313 | 15 | mattermost_token: | ||
314 | 16 | description: "Mattermost API token" | ||
315 | 17 | default: "" | ||
316 | 18 | type: string | ||
317 | 19 | mattermost_channel: | ||
318 | 20 | description: "Mattermost channel in which to expect review requests" | ||
319 | 21 | default: "" | ||
320 | 22 | type: string | ||
321 | 23 | mattermost_url: | ||
322 | 24 | description: "Mattermost URL" | ||
323 | 25 | default: "" | ||
324 | 26 | type: string | ||
325 | 27 | mattermost_team: | ||
326 | 28 | description: "Mattermost team" | ||
327 | 29 | default: "" | ||
328 | 30 | type: string | ||
329 | diff --git a/metadata.yaml b/metadata.yaml | |||
330 | 0 | new file mode 100644 | 31 | new file mode 100644 |
331 | index 0000000..83d6842 | |||
332 | --- /dev/null | |||
333 | +++ b/metadata.yaml | |||
334 | @@ -0,0 +1,18 @@ | |||
335 | 1 | name: canonical-is-reviewbot | ||
336 | 2 | display-name: Mattermost Review Bot | ||
337 | 3 | summary: MatterMost Review Bot charm | ||
338 | 4 | maintainers: | ||
339 | 5 | - https://launchpad.net/~mm-reviewbot-charmers | ||
340 | 6 | description: | | ||
341 | 7 | A charm which deploys Mattermost Review Bot on kubernetes. | ||
342 | 8 | Mattermost is a flexible, open source messaging platform that enables | ||
343 | 9 | secure team collaboration. | ||
344 | 10 | This bot allows reviewing Launchpad merge proposals on Mattermost | ||
345 | 11 | containers: | ||
346 | 12 | reviewbot: | ||
347 | 13 | resource: reviewbot-image | ||
348 | 14 | resources: | ||
349 | 15 | reviewbot-image: | ||
350 | 16 | type: oci-image | ||
351 | 17 | description: Reviewbot docker image | ||
352 | 18 | auto-fetch: true | ||
353 | diff --git a/pyproject.toml b/pyproject.toml | |||
354 | 0 | new file mode 100644 | 19 | new file mode 100644 |
355 | index 0000000..3f51442 | |||
356 | --- /dev/null | |||
357 | +++ b/pyproject.toml | |||
358 | @@ -0,0 +1,39 @@ | |||
359 | 1 | # Testing tools configuration | ||
360 | 2 | [tool.coverage.run] | ||
361 | 3 | branch = true | ||
362 | 4 | |||
363 | 5 | [tool.coverage.report] | ||
364 | 6 | show_missing = true | ||
365 | 7 | |||
366 | 8 | [tool.pytest.ini_options] | ||
367 | 9 | minversion = "6.0" | ||
368 | 10 | log_cli_level = "INFO" | ||
369 | 11 | |||
370 | 12 | # Formatting tools configuration | ||
371 | 13 | [tool.black] | ||
372 | 14 | line-length = 99 | ||
373 | 15 | target-version = ["py38"] | ||
374 | 16 | |||
375 | 17 | # Linting tools configuration | ||
376 | 18 | [tool.ruff] | ||
377 | 19 | line-length = 99 | ||
378 | 20 | select = ["E", "W", "F", "C", "N", "D", "I001"] | ||
379 | 21 | extend-ignore = [ | ||
380 | 22 | "D203", | ||
381 | 23 | "D204", | ||
382 | 24 | "D213", | ||
383 | 25 | "D215", | ||
384 | 26 | "D400", | ||
385 | 27 | "D404", | ||
386 | 28 | "D406", | ||
387 | 29 | "D407", | ||
388 | 30 | "D408", | ||
389 | 31 | "D409", | ||
390 | 32 | "D413", | ||
391 | 33 | ] | ||
392 | 34 | ignore = ["E501", "D107"] | ||
393 | 35 | extend-exclude = ["__pycache__", "*.egg_info"] | ||
394 | 36 | per-file-ignores = {"tests/*" = ["D100","D101","D102","D103","D104"]} | ||
395 | 37 | |||
396 | 38 | [tool.ruff.mccabe] | ||
397 | 39 | max-complexity = 10 | ||
398 | diff --git a/requirements.txt b/requirements.txt | |||
399 | 0 | new file mode 100644 | 40 | new file mode 100644 |
400 | index 0000000..56f5f64 | |||
401 | --- /dev/null | |||
402 | +++ b/requirements.txt | |||
403 | @@ -0,0 +1 @@ | |||
404 | 1 | ops >= 1.5.0 | ||
405 | diff --git a/src/charm.py b/src/charm.py | |||
406 | 0 | new file mode 100755 | 2 | new file mode 100755 |
407 | index 0000000..e2bf66d | |||
408 | --- /dev/null | |||
409 | +++ b/src/charm.py | |||
410 | @@ -0,0 +1,121 @@ | |||
411 | 1 | #!/usr/bin/env python3 | ||
412 | 2 | # Copyright 2022 Ubuntu | ||
413 | 3 | # See LICENSE file for licensing details. | ||
414 | 4 | # | ||
415 | 5 | # Learn more at: https://juju.is/docs/sdk | ||
416 | 6 | |||
417 | 7 | """Charm the service. | ||
418 | 8 | |||
419 | 9 | Refer to the following post for a quick-start guide that will help you | ||
420 | 10 | develop a new k8s charm using the Operator Framework: | ||
421 | 11 | |||
422 | 12 | https://discourse.charmhub.io/t/4208 | ||
423 | 13 | """ | ||
424 | 14 | |||
425 | 15 | import logging | ||
426 | 16 | |||
427 | 17 | from ops.charm import CharmBase | ||
428 | 18 | from ops.main import main | ||
429 | 19 | from ops.model import ActiveStatus, BlockedStatus, WaitingStatus | ||
430 | 20 | |||
431 | 21 | # Log messages can be retrieved using juju debug-log | ||
432 | 22 | logger = logging.getLogger(__name__) | ||
433 | 23 | |||
434 | 24 | VALID_LOG_LEVELS = ["info", "debug", "warning", "error", "critical"] | ||
435 | 25 | |||
436 | 26 | |||
437 | 27 | class CharmReviewbotCharm(CharmBase): | ||
438 | 28 | """Charm the service.""" | ||
439 | 29 | |||
440 | 30 | def __init__(self, *args): | ||
441 | 31 | super().__init__(*args) | ||
442 | 32 | self.framework.observe(self.on.reviewbot_pebble_ready, self._on_reviewbot_pebble_ready) | ||
443 | 33 | self.framework.observe(self.on.config_changed, self._on_config_changed) | ||
444 | 34 | |||
445 | 35 | def _missing_config(self): | ||
446 | 36 | # All config options must be set | ||
447 | 37 | missing_config = [] | ||
448 | 38 | for k, v in self.model.config.items(): | ||
449 | 39 | if not v: | ||
450 | 40 | missing_config.append(k) | ||
451 | 41 | return missing_config | ||
452 | 42 | |||
453 | 43 | def _on_reviewbot_pebble_ready(self, event): | ||
454 | 44 | """Define and start a workload using the Pebble API. | ||
455 | 45 | |||
456 | 46 | Change this example to suit your needs. You'll need to specify the right entrypoint and | ||
457 | 47 | environment configuration for your specific workload. | ||
458 | 48 | |||
459 | 49 | Learn more about interacting with Pebble at at https://juju.is/docs/sdk/pebble. | ||
460 | 50 | """ | ||
461 | 51 | missing_config = self._missing_config() | ||
462 | 52 | if missing_config: | ||
463 | 53 | self.unit.status = BlockedStatus(f"required config not set: '{missing_config}'") | ||
464 | 54 | return | ||
465 | 55 | # Get a reference the container attribute on the PebbleReadyEvent | ||
466 | 56 | container = event.workload | ||
467 | 57 | # Add initial Pebble config layer using the Pebble API | ||
468 | 58 | container.add_layer("reviewbot", self._pebble_layer, combine=True) | ||
469 | 59 | # Make Pebble reevaluate its plan, ensuring any services are started if enabled. | ||
470 | 60 | container.replan() | ||
471 | 61 | # Learn more about statuses in the SDK docs: | ||
472 | 62 | # https://juju.is/docs/sdk/constructs#heading--statuses | ||
473 | 63 | self.unit.status = ActiveStatus() | ||
474 | 64 | |||
475 | 65 | def _get_reviewbot_env_config(self) -> dict: | ||
476 | 66 | """Return an envConfig with configuration.""" | ||
477 | 67 | env_config = { | ||
478 | 68 | "REVIEWBOT_LP_SECRET": self.config["launchpad_secret"], | ||
479 | 69 | "REVIEWBOT_LP_TOKEN": self.config["launchpad_token"], | ||
480 | 70 | "REVIEWBOT_MM_CHANNEL": self.config["mattermost_channel"], | ||
481 | 71 | "REVIEWBOT_MM_TEAM": self.config["mattermost_team"], | ||
482 | 72 | "REVIEWBOT_MM_TOKEN": self.config["mattermost_token"], | ||
483 | 73 | "REVIEWBOT_MM_URL": self.config["mattermost_url"], | ||
484 | 74 | "HOME": "/home/reviewbot", # work around https://github.com/canonical/pebble/issues/183 | ||
485 | 75 | } | ||
486 | 76 | |||
487 | 77 | return env_config | ||
488 | 78 | |||
489 | 79 | def _on_config_changed(self, event): | ||
490 | 80 | """Handle changed configuration.""" | ||
491 | 81 | missing_config = self._missing_config() | ||
492 | 82 | if missing_config: | ||
493 | 83 | self.unit.status = BlockedStatus(f"required config not set: '{missing_config}'") | ||
494 | 84 | return | ||
495 | 85 | |||
496 | 86 | # The config is good, so update the configuration of the workload | ||
497 | 87 | container = self.unit.get_container("reviewbot") | ||
498 | 88 | # Verify that we can connect to the Pebble API in the workload container | ||
499 | 89 | if container.can_connect(): | ||
500 | 90 | # Push an updated layer with the new config | ||
501 | 91 | container.add_layer("reviewbot", self._pebble_layer, combine=True) | ||
502 | 92 | container.replan() | ||
503 | 93 | |||
504 | 94 | self.unit.status = ActiveStatus() | ||
505 | 95 | else: | ||
506 | 96 | # We were unable to connect to the Pebble API, so we defer this event | ||
507 | 97 | event.defer() | ||
508 | 98 | self.unit.status = WaitingStatus("waiting for Pebble API") | ||
509 | 99 | |||
510 | 100 | @property | ||
511 | 101 | def _pebble_layer(self): | ||
512 | 102 | """Return a dictionary representing a Pebble layer.""" | ||
513 | 103 | return { | ||
514 | 104 | "summary": "reviewbot layer", | ||
515 | 105 | "description": "pebble config layer for reviewbot", | ||
516 | 106 | "services": { | ||
517 | 107 | "reviewbot": { | ||
518 | 108 | "override": "replace", | ||
519 | 109 | "summary": "reviewbot", | ||
520 | 110 | "user": "reviewbot", | ||
521 | 111 | "group": "reviewbot", | ||
522 | 112 | "command": "/app/bot.py", | ||
523 | 113 | "startup": "enabled", | ||
524 | 114 | "environment": self._get_reviewbot_env_config(), | ||
525 | 115 | }, | ||
526 | 116 | }, | ||
527 | 117 | } | ||
528 | 118 | |||
529 | 119 | |||
530 | 120 | if __name__ == "__main__": # pragma: nocover | ||
531 | 121 | main(CharmReviewbotCharm) | ||
532 | diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py | |||
533 | 0 | new file mode 100644 | 122 | new file mode 100644 |
534 | index 0000000..3399c04 | |||
535 | --- /dev/null | |||
536 | +++ b/tests/unit/test_charm.py | |||
537 | @@ -0,0 +1,108 @@ | |||
538 | 1 | # Copyright 2022 Ubuntu | ||
539 | 2 | # See LICENSE file for licensing details. | ||
540 | 3 | # | ||
541 | 4 | # Learn more about testing at: https://juju.is/docs/sdk/testing | ||
542 | 5 | |||
543 | 6 | import unittest | ||
544 | 7 | |||
545 | 8 | import ops.testing | ||
546 | 9 | from charm import CharmReviewbotCharm | ||
547 | 10 | from ops.model import ActiveStatus, BlockedStatus, WaitingStatus | ||
548 | 11 | from ops.testing import Harness | ||
549 | 12 | |||
550 | 13 | |||
551 | 14 | class TestCharm(unittest.TestCase): | ||
552 | 15 | base_config = { | ||
553 | 16 | "launchpad_secret": "lps", | ||
554 | 17 | "launchpad_token": "lpt", | ||
555 | 18 | "mattermost_token": "mmtok", | ||
556 | 19 | "mattermost_channel": "mmc", | ||
557 | 20 | "mattermost_url": "mmu", | ||
558 | 21 | "mattermost_team": "mmt", | ||
559 | 22 | } | ||
560 | 23 | |||
561 | 24 | def setUp(self): | ||
562 | 25 | # Enable more accurate simulation of container networking. | ||
563 | 26 | # For more information, see https://juju.is/docs/sdk/testing#heading--simulate-can-connect | ||
564 | 27 | ops.testing.SIMULATE_CAN_CONNECT = True | ||
565 | 28 | self.addCleanup(setattr, ops.testing, "SIMULATE_CAN_CONNECT", False) | ||
566 | 29 | |||
567 | 30 | self.harness = Harness(CharmReviewbotCharm) | ||
568 | 31 | self.addCleanup(self.harness.cleanup) | ||
569 | 32 | self.harness.begin() | ||
570 | 33 | |||
571 | 34 | def test_reviewbot_pebble_ready(self): | ||
572 | 35 | # Expected plan after Pebble ready with default config | ||
573 | 36 | expected_plan = { | ||
574 | 37 | "services": { | ||
575 | 38 | "reviewbot": { | ||
576 | 39 | "override": "replace", | ||
577 | 40 | "summary": "reviewbot", | ||
578 | 41 | "command": "/app/bot.py", | ||
579 | 42 | "startup": "enabled", | ||
580 | 43 | "environment": { | ||
581 | 44 | "REVIEWBOT_LP_SECRET": "lps", | ||
582 | 45 | "REVIEWBOT_LP_TOKEN": "lpt", | ||
583 | 46 | "REVIEWBOT_MM_CHANNEL": "mmc", | ||
584 | 47 | "REVIEWBOT_MM_TEAM": "mmt", | ||
585 | 48 | "REVIEWBOT_MM_TOKEN": "mmtok", | ||
586 | 49 | "REVIEWBOT_MM_URL": "mmu", | ||
587 | 50 | "HOME": "/home/reviewbot", | ||
588 | 51 | }, | ||
589 | 52 | "user": "reviewbot", | ||
590 | 53 | "group": "reviewbot", | ||
591 | 54 | } | ||
592 | 55 | }, | ||
593 | 56 | } | ||
594 | 57 | # Simulate the container coming up and emission of pebble-ready event | ||
595 | 58 | self.harness.update_config(self.base_config) | ||
596 | 59 | self.harness.container_pebble_ready("reviewbot") | ||
597 | 60 | # Get the plan now we've run PebbleReady | ||
598 | 61 | updated_plan = self.harness.get_container_pebble_plan("reviewbot").to_dict() | ||
599 | 62 | # Check we've got the plan we expected | ||
600 | 63 | self.assertEqual(expected_plan, updated_plan) | ||
601 | 64 | # Check the service was started | ||
602 | 65 | service = self.harness.model.unit.get_container("reviewbot").get_service("reviewbot") | ||
603 | 66 | self.assertTrue(service.is_running()) | ||
604 | 67 | # Ensure we set an ActiveStatus with no message | ||
605 | 68 | self.assertEqual(self.harness.model.unit.status, ActiveStatus()) | ||
606 | 69 | |||
607 | 70 | def test_reviewbot_pebble_ready_no_config(self): | ||
608 | 71 | # Expect an empty plan when a config item is missing | ||
609 | 72 | expected_plan = {} | ||
610 | 73 | # Simulate the container coming up and emission of pebble-ready event | ||
611 | 74 | self.harness.container_pebble_ready("reviewbot") | ||
612 | 75 | # Get the plan now we've run PebbleReady | ||
613 | 76 | updated_plan = self.harness.get_container_pebble_plan("reviewbot").to_dict() | ||
614 | 77 | # Check we've got the plan we expected | ||
615 | 78 | self.assertEqual(expected_plan, updated_plan) | ||
616 | 79 | # Ensure we got a BlockedStatus | ||
617 | 80 | self.assertIsInstance(self.harness.model.unit.status, BlockedStatus) | ||
618 | 81 | |||
619 | 82 | def test_config_changed_valid_can_connect(self): | ||
620 | 83 | # Ensure the simulated Pebble API is reachable | ||
621 | 84 | self.harness.set_can_connect("reviewbot", True) | ||
622 | 85 | # Trigger a config-changed event with an updated value | ||
623 | 86 | changed_config = self.base_config.copy() | ||
624 | 87 | changed_config["launchpad_secret"] = "CHANGED" | ||
625 | 88 | self.harness.update_config(changed_config) | ||
626 | 89 | # Get the plan now we've run PebbleReady | ||
627 | 90 | updated_plan = self.harness.get_container_pebble_plan("reviewbot").to_dict() | ||
628 | 91 | updated_env = updated_plan["services"]["reviewbot"]["environment"] | ||
629 | 92 | # Check the config change was effective | ||
630 | 93 | self.assertEqual(updated_env["REVIEWBOT_LP_SECRET"], "CHANGED") | ||
631 | 94 | self.assertEqual(self.harness.model.unit.status, ActiveStatus()) | ||
632 | 95 | |||
633 | 96 | def test_config_changed_valid_cannot_connect(self): | ||
634 | 97 | # Trigger a config-changed event with an updated value | ||
635 | 98 | self.harness.update_config(self.base_config) | ||
636 | 99 | # Check the charm is in WaitingStatus | ||
637 | 100 | self.assertIsInstance(self.harness.model.unit.status, WaitingStatus) | ||
638 | 101 | |||
639 | 102 | def test_config_changed_invalid(self): | ||
640 | 103 | # Ensure the simulated Pebble API is reachable | ||
641 | 104 | self.harness.set_can_connect("reviewbot", True) | ||
642 | 105 | # Trigger a config-changed event with an updated value | ||
643 | 106 | self.harness.update_config({}) | ||
644 | 107 | # Check the charm is in BlockedStatus | ||
645 | 108 | self.assertIsInstance(self.harness.model.unit.status, BlockedStatus) | ||
646 | diff --git a/tox.ini b/tox.ini | |||
647 | 0 | new file mode 100644 | 109 | new file mode 100644 |
648 | index 0000000..d4284f8 | |||
649 | --- /dev/null | |||
650 | +++ b/tox.ini | |||
651 | @@ -0,0 +1,84 @@ | |||
652 | 1 | # Copyright 2022 Ubuntu | ||
653 | 2 | # See LICENSE file for licensing details. | ||
654 | 3 | |||
655 | 4 | [tox] | ||
656 | 5 | skipsdist=True | ||
657 | 6 | skip_missing_interpreters = True | ||
658 | 7 | envlist = fmt, lint, unit | ||
659 | 8 | |||
660 | 9 | [vars] | ||
661 | 10 | src_path = {toxinidir}/src/ | ||
662 | 11 | tst_path = {toxinidir}/tests/ | ||
663 | 12 | ;lib_path = {toxinidir}/lib/charms/operator_name_with_underscores | ||
664 | 13 | all_path = {[vars]src_path} {[vars]tst_path} | ||
665 | 14 | |||
666 | 15 | [testenv] | ||
667 | 16 | setenv = | ||
668 | 17 | PYTHONPATH = {toxinidir}:{toxinidir}/lib:{[vars]src_path} | ||
669 | 18 | PYTHONBREAKPOINT=pdb.set_trace | ||
670 | 19 | PY_COLORS=1 | ||
671 | 20 | passenv = | ||
672 | 21 | PYTHONPATH | ||
673 | 22 | CHARM_BUILD_DIR | ||
674 | 23 | MODEL_SETTINGS | ||
675 | 24 | |||
676 | 25 | [testenv:fmt] | ||
677 | 26 | description = Apply coding style standards to code | ||
678 | 27 | deps = | ||
679 | 28 | black | ||
680 | 29 | ruff | ||
681 | 30 | commands = | ||
682 | 31 | black {[vars]all_path} | ||
683 | 32 | ruff --fix {[vars]all_path} | ||
684 | 33 | |||
685 | 34 | [testenv:lint] | ||
686 | 35 | description = Check code against coding style standards | ||
687 | 36 | deps = | ||
688 | 37 | black | ||
689 | 38 | ruff | ||
690 | 39 | codespell | ||
691 | 40 | commands = | ||
692 | 41 | # uncomment the following line if this charm owns a lib | ||
693 | 42 | # codespell {[vars]lib_path} | ||
694 | 43 | codespell {toxinidir} \ | ||
695 | 44 | --skip {toxinidir}/.git \ | ||
696 | 45 | --skip {toxinidir}/.tox \ | ||
697 | 46 | --skip {toxinidir}/build \ | ||
698 | 47 | --skip {toxinidir}/lib \ | ||
699 | 48 | --skip {toxinidir}/venv \ | ||
700 | 49 | --skip {toxinidir}/.mypy_cache \ | ||
701 | 50 | --skip {toxinidir}/icon.svg | ||
702 | 51 | |||
703 | 52 | ruff {[vars]all_path} | ||
704 | 53 | black --check --diff {[vars]all_path} | ||
705 | 54 | |||
706 | 55 | [testenv:unit] | ||
707 | 56 | description = Run unit tests | ||
708 | 57 | deps = | ||
709 | 58 | pytest | ||
710 | 59 | coverage[toml] | ||
711 | 60 | -r{toxinidir}/requirements.txt | ||
712 | 61 | commands = | ||
713 | 62 | coverage run --source={[vars]src_path} \ | ||
714 | 63 | -m pytest \ | ||
715 | 64 | --ignore={[vars]tst_path}integration \ | ||
716 | 65 | --tb native \ | ||
717 | 66 | -v \ | ||
718 | 67 | -s \ | ||
719 | 68 | {posargs} | ||
720 | 69 | coverage report | ||
721 | 70 | |||
722 | 71 | [testenv:integration] | ||
723 | 72 | description = Run integration tests | ||
724 | 73 | deps = | ||
725 | 74 | pytest | ||
726 | 75 | juju | ||
727 | 76 | pytest-operator | ||
728 | 77 | -r{toxinidir}/requirements.txt | ||
729 | 78 | commands = | ||
730 | 79 | pytest -v \ | ||
731 | 80 | -s \ | ||
732 | 81 | --tb native \ | ||
733 | 82 | --ignore={[vars]tst_path}unit \ | ||
734 | 83 | --log-cli-level=INFO \ | ||
735 | 84 | {posargs} |
Tiny nitpick inline, LGTM otherwise