Skip to content

Commit cff37f8

Browse files
committed
Issue repair ticket after successful patch
1 parent cfff265 commit cff37f8

4 files changed

Lines changed: 36 additions & 8 deletions

File tree

custom_components/patch/__init__.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,11 @@ async def run(self) -> None:
242242
return
243243

244244
results = await asyncio.gather(*(patch.apply() for patch in self._patches))
245-
updates = results.count(True)
245+
updates = [
246+
patch.config for index, patch in enumerate(self._patches) if results[index]
247+
]
246248
if updates:
247-
LOGGER.warning(
248-
f"{updates} core file {'s were' if updates > 1 else 'was'} patched."
249-
)
249+
self._applied(updates)
250250
if self._config[CONF_RESTART]:
251251
LOGGER.warning("Restarting HA core.")
252252
await self._hass.services.async_call(
@@ -268,9 +268,28 @@ def _repair(self, files: list[PatchType]) -> None:
268268
"patch_file_base_mismatch_" + str(int(dt_util.now().timestamp())),
269269
is_fixable=False,
270270
learn_more_url="https://github.com/amitfin/patch#configuration",
271-
severity=ir.IssueSeverity.WARNING,
271+
severity=ir.IssueSeverity.ERROR,
272272
translation_key="base_mismatch",
273273
translation_placeholders={
274274
"files": message,
275275
},
276276
)
277+
278+
def _applied(self, files: list[PatchType]) -> None:
279+
"""Report the system was patched."""
280+
count = len(files)
281+
LOGGER.warning(f"{count} core file {'s were' if count > 1 else 'was'} patched.")
282+
283+
ir.async_create_issue(
284+
self._hass,
285+
DOMAIN,
286+
"system_was_patched",
287+
is_fixable=False,
288+
is_persistent=True,
289+
learn_more_url="https://github.com/amitfin/patch#configuration",
290+
severity=ir.IssueSeverity.WARNING,
291+
translation_key="system_update",
292+
translation_placeholders={
293+
"files": ", ".join(f'"{file[CONF_DESTINATION]}"' for file in files),
294+
},
295+
)

custom_components/patch/strings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
{
22
"issues": {
3+
"system_update": {
4+
"title": "Patch Applied",
5+
"description": "The following files have been patched: {files}. You may safely dismiss this notice."
6+
},
37
"base_mismatch": {
48
"title": "Patch Base File Mismatch",
59
"description": "{files} different than the corresponding base file. To fix this issue rebase the patch, and update the base and patch files."

custom_components/patch/translations/en.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
{
22
"issues": {
3+
"system_update": {
4+
"title": "Patch Applied",
5+
"description": "The following files have been patched: {files}. You may safely dismiss this notice."
6+
},
37
"base_mismatch": {
48
"title": "Patch Base File Mismatch",
59
"description": "{files} different than the corresponding base file. To fix this issue rebase the patch, and update the base and patch files."

tests/test_init.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,13 @@ async def test_patch( # noqa: PLR0913
167167
else destination_content
168168
)
169169
assert async_call_mock.call_count == (1 if update and restart else 0)
170-
assert len(repairs) == (
171-
1 if not update and destination_content != patch_content else 0
172-
)
170+
assert len(repairs) == (1 if update or destination_content != patch_content else 0)
173171
if update:
174172
assert "was updated by the patch file" in caplog.text
175173
assert "1 core file was patched." in caplog.text
174+
assert repairs[0].data["action"] == "create"
175+
assert repairs[0].data["domain"] == DOMAIN
176+
assert repairs[0].data["issue_id"] == "system_was_patched"
176177
if restart:
177178
assert async_call_mock.await_args_list[0].args[0] == ha.DOMAIN
178179
assert (

0 commit comments

Comments
 (0)