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 Feb 29, 2024
1 parent 42f90c1 commit 4e588cc
Show file tree
Hide file tree
Showing 3 changed files with 20 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
26 changes: 18 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,22 @@ 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):

Check warning

Code scanning / vcs-diff-lint

CoprDirsLogic.get_or_validate: Missing function or method docstring Warning

CoprDirsLogic.get_or_validate: Missing function or method docstring
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 4e588cc

Please sign in to comment.