Skip to content

Commit

Permalink
common, frontend: Add ActionObjectTypeEnum for actions
Browse files Browse the repository at this point in the history
to prevent typos - Action.object_type is used for filtering Actions
to get their type
  • Loading branch information
nikromen committed Aug 3, 2023
1 parent ef18773 commit e800943
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 18 deletions.
15 changes: 15 additions & 0 deletions common/copr_common/enums.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import random
import string
from enum import Enum

from six import with_metaclass

Expand Down Expand Up @@ -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"
22 changes: 11 additions & 11 deletions frontend/coprs_frontend/coprs/logic/actions_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()),
Expand All @@ -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()),
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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}),
Expand All @@ -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="",
Expand All @@ -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()),
Expand Down
2 changes: 1 addition & 1 deletion frontend/coprs_frontend/coprs/logic/coprs_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning

Code scanning / vcs-diff-lint

Unused ActionObjectTypeEnum imported from copr_common.enums Warning

Unused ActionObjectTypeEnum imported from copr_common.enums
from coprs import app, db
from coprs import exceptions
from coprs import helpers
Expand Down
4 changes: 2 additions & 2 deletions frontend/coprs_frontend/tests/coprs_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions frontend/coprs_frontend/tests/test_logic/test_coprs_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e800943

Please sign in to comment.