From e800943308ad0b6c1262b120758494e071c62963 Mon Sep 17 00:00:00 2001 From: Jiri Kyjovsky Date: Mon, 10 Jul 2023 13:38:34 +0200 Subject: [PATCH] common, frontend: Add ActionObjectTypeEnum for actions to prevent typos - Action.object_type is used for filtering Actions to get their type --- common/copr_common/enums.py | 15 +++++++++++++ .../coprs/logic/actions_logic.py | 22 +++++++++---------- .../coprs_frontend/coprs/logic/coprs_logic.py | 2 +- .../coprs_frontend/tests/coprs_test_case.py | 4 ++-- .../tests/test_logic/test_coprs_logic.py | 4 ++-- .../tests/test_logic/test_modules_logic.py | 4 ++-- 6 files changed, 33 insertions(+), 18 deletions(-) diff --git a/common/copr_common/enums.py b/common/copr_common/enums.py index 6e3840e29..dd8b94798 100644 --- a/common/copr_common/enums.py +++ b/common/copr_common/enums.py @@ -1,5 +1,6 @@ import random import string +from enum import Enum from six import with_metaclass @@ -137,3 +138,17 @@ class FailTypeEnum(with_metaclass(EnumType, object)): "git_checkout_error": 33, "srpm_build_error": 34, } + + +class ActionObjectTypeEnum(str, Enum): + """ + Enum for object_type attribute in Action model + """ + COPR = "copr" + REPOSITORY = "repository" + BUILD = "build" + BUILDS = "builds" + COPR_CHROOT = "copr_chroot" + CHROOT = "chroot" + MODULE = "module" + NONE = "None" diff --git a/frontend/coprs_frontend/coprs/logic/actions_logic.py b/frontend/coprs_frontend/coprs/logic/actions_logic.py index c86233c67..aa6f981ee 100644 --- a/frontend/coprs_frontend/coprs/logic/actions_logic.py +++ b/frontend/coprs_frontend/coprs/logic/actions_logic.py @@ -4,7 +4,7 @@ from sqlalchemy import and_, or_ from sqlalchemy.exc import IntegrityError -from copr_common.enums import ActionTypeEnum, BackendResultEnum +from copr_common.enums import ActionTypeEnum, BackendResultEnum, ActionObjectTypeEnum from coprs import db from coprs import models from coprs import helpers @@ -128,7 +128,7 @@ def send_createrepo(cls, copr, dirnames=None, chroots=None, devel=None, data_dict["devel"] = run == "dev" action = models.Action( action_type=ActionTypeEnum("createrepo"), - object_type="repository", + object_type=ActionObjectTypeEnum.REPOSITORY, object_id=0, data=json.dumps(data_dict), created_on=int(time.time()), @@ -146,7 +146,7 @@ def send_delete_copr(cls, copr): "project_dirnames": [copr_dir.name for copr_dir in copr.dirs], } action = models.Action(action_type=ActionTypeEnum("delete"), - object_type="copr", + object_type=ActionObjectTypeEnum.COPR, object_id=copr.id, data=json.dumps(data_dict), created_on=int(time.time()), @@ -199,7 +199,7 @@ def send_delete_build(cls, build): """ action = models.Action( action_type=ActionTypeEnum("delete"), - object_type="build", + object_type=ActionObjectTypeEnum.BUILD, object_id=build.id, data=json.dumps(cls.get_build_delete_data(build)), created_on=int(time.time()), @@ -247,7 +247,7 @@ def send_delete_multiple_builds(cls, builds): # not object_id here, we are working with multiple IDs action = models.Action( action_type=ActionTypeEnum("delete"), - object_type="builds", + object_type=ActionObjectTypeEnum.BUILDS, data=json.dumps(data), created_on=int(time.time()), copr_id=builds[0].copr.id, @@ -288,7 +288,7 @@ def send_update_comps(cls, chroot): action = models.Action( action_type=ActionTypeEnum("update_comps"), - object_type="copr_chroot", + object_type=ActionObjectTypeEnum.COPR_CHROOT, data=json.dumps(data_dict), created_on=int(time.time()), copr_id=chroot.copr.id, @@ -310,7 +310,7 @@ def send_create_gpg_key(cls, copr): action = models.Action( action_type=ActionTypeEnum("gen_gpg_key"), - object_type="copr", + object_type=ActionObjectTypeEnum.COPR, data=json.dumps(data_dict), created_on=int(time.time()), copr_id=copr.id, @@ -322,7 +322,7 @@ def send_create_gpg_key(cls, copr): def send_rawhide_to_release(cls, copr, data): action = models.Action( action_type=ActionTypeEnum("rawhide_to_release"), - object_type="None", + object_type=ActionObjectTypeEnum.NONE, data=json.dumps(data), created_on=int(time.time()), copr_id=copr.id, @@ -340,7 +340,7 @@ def send_fork_copr(cls, src, dst, builds_map): action = models.Action( action_type=ActionTypeEnum("fork"), - object_type="copr", + object_type=ActionObjectTypeEnum.COPR, old_value="{0}".format(src.full_name), new_value="{0}".format(dst.full_name), data=json.dumps({"user": dst.owner_name, "copr": dst.name, "builds_map": builds_map}), @@ -366,7 +366,7 @@ def send_build_module(cls, copr, module): action = models.Action( action_type=ActionTypeEnum("build_module"), - object_type="module", + object_type=ActionObjectTypeEnum.MODULE, object_id=module.id, old_value="", new_value="", @@ -392,7 +392,7 @@ def send_delete_chroot(cls, copr_chroot): action = models.Action( action_type=ActionTypeEnum("delete"), - object_type="chroot", + object_type=ActionObjectTypeEnum.CHROOT, object_id=None, data=json.dumps(data_dict), created_on=int(time.time()), diff --git a/frontend/coprs_frontend/coprs/logic/coprs_logic.py b/frontend/coprs_frontend/coprs/logic/coprs_logic.py index d3a41746c..b3f31b9e2 100644 --- a/frontend/coprs_frontend/coprs/logic/coprs_logic.py +++ b/frontend/coprs_frontend/coprs/logic/coprs_logic.py @@ -16,7 +16,7 @@ from sqlalchemy.orm.exc import NoResultFound from sqlalchemy.orm.attributes import get_history -from copr_common.enums import ActionTypeEnum, BackendResultEnum, ActionPriorityEnum +from copr_common.enums import ActionTypeEnum, BackendResultEnum, ActionPriorityEnum, ActionObjectTypeEnum from coprs import app, db from coprs import exceptions from coprs import helpers diff --git a/frontend/coprs_frontend/tests/coprs_test_case.py b/frontend/coprs_frontend/tests/coprs_test_case.py index 36b10cd36..0486e992c 100644 --- a/frontend/coprs_frontend/tests/coprs_test_case.py +++ b/frontend/coprs_frontend/tests/coprs_test_case.py @@ -13,7 +13,7 @@ import coprs -from copr_common.enums import ActionTypeEnum, BackendResultEnum, StatusEnum +from copr_common.enums import ActionTypeEnum, BackendResultEnum, StatusEnum, ActionObjectTypeEnum from coprs import helpers from coprs import models from coprs import cache @@ -679,7 +679,7 @@ def f_copr_more_permissions(self, f_copr_permissions): @pytest.fixture def f_actions(self, f_db): self.delete_action = models.Action(action_type=ActionTypeEnum("delete"), - object_type="copr", + object_type=ActionObjectTypeEnum.COPR, object_id=self.c1.id, old_value="asd/qwe", new_value=None, diff --git a/frontend/coprs_frontend/tests/test_logic/test_coprs_logic.py b/frontend/coprs_frontend/tests/test_logic/test_coprs_logic.py index 5d8ec81ef..675fa2c4d 100644 --- a/frontend/coprs_frontend/tests/test_logic/test_coprs_logic.py +++ b/frontend/coprs_frontend/tests/test_logic/test_coprs_logic.py @@ -7,7 +7,7 @@ from sqlalchemy import desc -from copr_common.enums import ActionTypeEnum, StatusEnum +from copr_common.enums import ActionTypeEnum, StatusEnum, ActionObjectTypeEnum from coprs import app from coprs.forms import PinnedCoprsForm, ChrootForm, ModuleEnableNameValidator from coprs.logic.actions_logic import ActionsLogic @@ -31,7 +31,7 @@ class TestCoprsLogic(CoprsTestCase): def test_legal_flag_doesnt_block_copr_functionality(self, f_users, f_coprs, f_db): self.db.session.add(self.models.Action( - object_type="copr", + object_type=ActionObjectTypeEnum.COPR, object_id=self.c1.id, action_type=ActionTypeEnum("legal-flag"))) diff --git a/frontend/coprs_frontend/tests/test_logic/test_modules_logic.py b/frontend/coprs_frontend/tests/test_logic/test_modules_logic.py index 2e1cd891d..d9ce6097d 100644 --- a/frontend/coprs_frontend/tests/test_logic/test_modules_logic.py +++ b/frontend/coprs_frontend/tests/test_logic/test_modules_logic.py @@ -7,7 +7,7 @@ from tests.coprs_test_case import CoprsTestCase from coprs.logic.modules_logic import ModulesLogic, ModuleBuildFacade, ModulemdGenerator from coprs.logic.coprs_logic import CoprChrootsLogic -from copr_common.enums import ActionTypeEnum, BackendResultEnum, ModuleStatusEnum, StatusEnum +from copr_common.enums import ActionTypeEnum, BackendResultEnum, ModuleStatusEnum, StatusEnum, ActionObjectTypeEnum from coprs.exceptions import BadRequest from coprs import models, db @@ -40,7 +40,7 @@ def test_state(self, f_users, f_coprs, f_mock_chroots, f_builds, f_modules, f_db self.b3.build_chroots[0].status = StatusEnum("succeeded") action = models.Action( action_type=ActionTypeEnum("build_module"), - object_type="module", + object_type=ActionObjectTypeEnum.MODULE, object_id=self.m1.id, ) db.session.add(action)