Skip to content

Commit

Permalink
cli, frontend: correctly check CoprDir before upload
Browse files Browse the repository at this point in the history
We don't want to start uploading when the CoprDir format is invalid.

Follow-up-for: abb1bfe
Fixes: #2786
  • Loading branch information
praiskup committed Mar 1, 2024
1 parent 26394d9 commit a1f4bcb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
5 changes: 1 addition & 4 deletions cli/copr_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,7 @@ def action_build(self, args):
self.client.build_proxy.check_before_build(
ownername=username,
projectname=projectname,
# documentation says user can create directory with copr build, but
# /build/check-before-build endpoint checks for presence of this
# dirname even before creating it in this check.
project_dirname=None,
project_dirname=project_dirname,
buildopts=buildopts,
)

Expand Down
35 changes: 27 additions & 8 deletions frontend/coprs_frontend/coprs/logic/coprs_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,16 +664,10 @@ def _valid_custom_dir_suffix(copr, dirname):
return all([c.isnumeric() for c in parts[2]])

@classmethod
def get_or_create(cls, copr, dirname):
def validate(cls, copr, dirname):
"""
Create a CoprDir on-demand, e.g. before pull-request builds is
submitted. We don't create the "main" CoprDirs here (those are created
when a new project is created.
Raise exception if dirname is invalid for copr.
"""
copr_dir = cls.get_by_copr_or_none(copr, dirname)
if copr_dir:
return copr_dir

if not dirname.startswith(copr.name+':'):
raise MalformedArgumentException(
"Copr dirname must start with '{}:' prefix".format(
Expand All @@ -691,6 +685,31 @@ def get_or_create(cls, copr, dirname):
f"Wrong directory '{dirname}' specified. Directory name can "
"consist of alpha-numeric strings separated by colons.")

@classmethod
def get_or_validate(cls, copr, dirname):
"""
Return the existing CoprDir object if it exists; otherwise, check
whether it can be created (has valid name) and return None.
Note that we first check if the directory exists, and then we perform
the name validation. This is intentional for cases where the validation
rules may need to be changed in the future. In such cases, we still
want to return existing directories that do not match the new rules.
"""
if copr_dir := cls.get_by_copr_or_none(copr, dirname):
return copr_dir
cls.validate(copr, dirname)
return None

@classmethod
def get_or_create(cls, copr, dirname):
"""
Create a CoprDir on-demand, e.g. before pull-request builds is
submitted. We don't create the "main" CoprDirs here (those are created
when a new project is created.
"""
if copr_dir := cls.get_or_validate(copr, dirname):
return copr_dir
try:
copr_dir = models.CoprDir(name=dirname, copr=copr, main=False)
db.session.add(copr_dir)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def check_before_build():

# Raises an exception if CoprDir doesn't exist
if data.get("project_dirname"):
CoprDirsLogic.get_by_copr(copr, data["project_dirname"])
CoprDirsLogic.get_or_validate(copr, data["project_dirname"])

# Permissions check
if not flask.g.user.can_build_in(copr):
Expand Down

0 comments on commit a1f4bcb

Please sign in to comment.