diff --git a/src/ahriman/application/handlers/patch.py b/src/ahriman/application/handlers/patch.py index c0453db5..ef58e068 100644 --- a/src/ahriman/application/handlers/patch.py +++ b/src/ahriman/application/handlers/patch.py @@ -130,12 +130,11 @@ class Patch(Handler): variables(list[str] | None): extract patches only for specified PKGBUILD variables exit_code(bool): exit with error on empty search result """ - patches = [] - if variables is not None: - for variable in variables: - patches.extend(application.reporter.package_patches_get(package_base, variable)) - else: - patches = application.reporter.package_patches_get(package_base, variables) + patches = [ + patch + for patch in application.reporter.package_patches_get(package_base, None) + if variables is None or patch.key in variables + ] Patch.check_if_empty(exit_code, not patches) PatchPrinter(package_base, patches)(verbose=True, separator=" = ") @@ -154,4 +153,4 @@ class Patch(Handler): for variable in variables: # iterate over single variable application.reporter.package_patches_remove(package_base, variable) else: - application.reporter.package_patches_remove(package_base, variables) # just pass as is + application.reporter.package_patches_remove(package_base, None) # just pass as is diff --git a/src/ahriman/web/schemas/patch_schema.py b/src/ahriman/web/schemas/patch_schema.py index aa6cebb5..56032128 100644 --- a/src/ahriman/web/schemas/patch_schema.py +++ b/src/ahriman/web/schemas/patch_schema.py @@ -25,8 +25,8 @@ class PatchSchema(Schema): request and response patch schema """ - key = fields.String(required=True, metadata={ - "description": "environment variable name", + key = fields.String(metadata={ + "description": "environment variable name. Required in case if it is not full diff", }) value = fields.String(metadata={ "description": "environment variable value", diff --git a/src/ahriman/web/views/v1/packages/patches.py b/src/ahriman/web/views/v1/packages/patches.py index f54b7fba..8b068ed4 100644 --- a/src/ahriman/web/views/v1/packages/patches.py +++ b/src/ahriman/web/views/v1/packages/patches.py @@ -96,7 +96,7 @@ class PatchesView(StatusViewGuard, BaseView): try: data = await self.request.json() - key = data["key"] + key = data.get("key") value = data["value"] except Exception as ex: raise HTTPBadRequest(reason=str(ex)) diff --git a/tests/ahriman/application/handlers/test_handler_patch.py b/tests/ahriman/application/handlers/test_handler_patch.py index ecbf9d4e..97be1632 100644 --- a/tests/ahriman/application/handlers/test_handler_patch.py +++ b/tests/ahriman/application/handlers/test_handler_patch.py @@ -161,12 +161,12 @@ def test_patch_set_list(application: Application, mocker: MockerFixture) -> None must list available patches for the command """ get_mock = mocker.patch("ahriman.core.status.local_client.LocalClient.package_patches_get", - return_value=[PkgbuildPatch(None, "patch")]) + return_value=[PkgbuildPatch(None, "patch"), PkgbuildPatch("version", "value")]) print_mock = mocker.patch("ahriman.core.formatters.Printer.print") check_mock = mocker.patch("ahriman.application.handlers.Handler.check_if_empty") Patch.patch_set_list(application, "ahriman", ["version"], False) - get_mock.assert_called_once_with("ahriman", "version") + get_mock.assert_called_once_with("ahriman", None) print_mock.assert_called_once_with(verbose=True, log_fn=pytest.helpers.anyvar(int), separator=" = ") check_mock.assert_called_once_with(False, False) diff --git a/tests/ahriman/web/views/v1/packages/test_view_v1_packages_patches.py b/tests/ahriman/web/views/v1/packages/test_view_v1_packages_patches.py index fee9bc0f..b337e968 100644 --- a/tests/ahriman/web/views/v1/packages/test_view_v1_packages_patches.py +++ b/tests/ahriman/web/views/v1/packages/test_view_v1_packages_patches.py @@ -64,6 +64,24 @@ async def test_post(client: TestClient, package_ahriman: Package) -> None: assert patches == [payload] +async def test_post_full_diff(client: TestClient, package_ahriman: Package) -> None: + """ + must create patch from full diff + """ + await client.post(f"/api/v1/packages/{package_ahriman.base}", + json={"status": BuildStatusEnum.Success.value, "package": package_ahriman.view()}) + request_schema = pytest.helpers.schema_request(PatchesView.post) + + payload = {"value": "v"} + assert not request_schema.validate(payload) + response = await client.post(f"/api/v1/packages/{package_ahriman.base}/patches", json=payload) + assert response.status == 204 + + response = await client.get(f"/api/v1/packages/{package_ahriman.base}/patches") + patches = await response.json() + assert patches == [payload] + + async def test_post_exception(client: TestClient, package_ahriman: Package) -> None: """ must raise exception on invalid payload