From: Michael Tremer Date: Sat, 25 Jun 2022 14:05:12 +0000 (+0000) Subject: users: Make custom repositories available X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=258f9d20278a4ed32ac246b040dee227afc2772e;p=pbs.git users: Make custom repositories available Signed-off-by: Michael Tremer --- diff --git a/src/buildservice/distribution.py b/src/buildservice/distribution.py index 1a0340ab..7c2a8f61 100644 --- a/src/buildservice/distribution.py +++ b/src/buildservice/distribution.py @@ -155,12 +155,13 @@ class Distribution(base.DataObject): return sorted(repos) - def get_repo(self, name): + def get_repo(self, slug): repo = self.backend.repos._get_repository("SELECT * FROM repositories \ - WHERE distro_id = %s AND name = %s", self.id, name) + WHERE distro_id = %s AND slug = %s", self.id, slug) # Cache - repo.distro = self + if repo: + repo.distro = self return repo diff --git a/src/buildservice/users.py b/src/buildservice/users.py index e6b209aa..7d53e03b 100644 --- a/src/buildservice/users.py +++ b/src/buildservice/users.py @@ -392,6 +392,22 @@ class User(base.DataObject): return list(repos) + def get_repo(self, slug): + return self.backend.repos._get_repository(""" + SELECT + * + FROM + repositories + WHERE + deleted IS FALSE + AND + owner_id = %s + AND + slug = %s""", + self.id, + slug, + ) + class UserEmail(base.DataObject): table = "users_emails" diff --git a/src/web/__init__.py b/src/web/__init__.py index 5cee78b6..89172944 100644 --- a/src/web/__init__.py +++ b/src/web/__init__.py @@ -111,6 +111,14 @@ class Application(tornado.web.Application): # User profiles (r"/users", users.UsersHandler), (r"/users/(\w+)", users.ShowHandler), + (r"/users/(?P\w+)/repos/(?P[A-Za-z0-9\-]+)", + repos.ShowHandler), + (r"/users/(?P\w+)/repos/(?P[A-Za-z0-9\-]+)\.repo", + repos.ConfigHandler), + (r"/users/(?P\w+)/repos/(?P[A-Za-z0-9\-]+)/builds", + repos.BuildsHandler), + (r"/users/(?P\w+)/repos/(?P[A-Za-z0-9\-]+)/mirrorlist", + repos.MirrorlistHandler), (r"/user/(\w+)/impersonate", users.UserImpersonateHandler), (r"/user/(\w+)/delete", users.UserDeleteHandler), (r"/user/(\w+)/edit", users.UserEditHandler), @@ -155,10 +163,14 @@ class Application(tornado.web.Application): # Distributions (r"/distros", distributions.IndexHandler), (r"/distros/([A-Za-z0-9\-\.]+)", distributions.ShowHandler), - (r"/distros/([A-Za-z0-9\-\.]+)/repos/([A-Za-z0-9\-]+)", repos.ShowHandler), - (r"/distros/([A-Za-z0-9\-\.]+)/repos/([A-Za-z0-9\-]+)\.repo", repos.ConfigHandler), - (r"/distros/([A-Za-z0-9\-\.]+)/repos/([A-Za-z0-9\-]+)/builds", repos.BuildsHandler), - (r"/distros/([A-Za-z0-9\-\.]+)/repos/([A-Za-z0-9\-]+)/mirrorlist", repos.MirrorlistHandler), + (r"/distros/(?P[A-Za-z0-9\-\.]+)/repos/(?P[A-Za-z0-9\-]+)", + repos.ShowHandler), + (r"/distros/(?P[A-Za-z0-9\-\.]+)/repos/(?P[A-Za-z0-9\-]+)\.repo", + repos.ConfigHandler), + (r"/distros/(?P[A-Za-z0-9\-\.]+)/repos/(?P[A-Za-z0-9\-]+)/builds", + repos.BuildsHandler), + (r"/distros/(?P[A-Za-z0-9\-\.]+)/repos/(?P[A-Za-z0-9\-]+)/mirrorlist", + repos.MirrorlistHandler), (r"/distro/([A-Za-z0-9\-\.]+)/repo/([A-Za-z0-9\-]+)/edit", RepositoryEditHandler), diff --git a/src/web/repos.py b/src/web/repos.py index 9df0f2f9..e77d3f6e 100644 --- a/src/web/repos.py +++ b/src/web/repos.py @@ -24,47 +24,51 @@ import tornado.web from . import base from . import ui_modules -class ShowHandler(base.BaseHandler): - def get(self, distro_slug, repo_slug): +class BaseHandler(base.BaseHandler): + def _get_repo(self, repo_slug, distro_slug=None, user_slug=None): + distro = user = None + # Find the distribution - distro = self.backend.distros.get_by_slug(distro_slug) - if not distro: - raise tornado.web.HTTPError(404, "Could not find distro: %s" % distro_slug) + if distro_slug: + distro = self.backend.distros.get_by_slug(distro_slug) + if not distro: + raise tornado.web.HTTPError(404, "Could not find distro: %s" % distro_slug) + + # Find the user + if user_slug: + user = self.backend.users.get_by_name(user_slug) + if not user: + raise tornado.web.HTTPError(404, "Could not find user: %s" % user_slug) + + assert distro or user, "Neither distro nor user set" # Find the repository - repo = distro.get_repo(repo_slug) + repo = (distro or user).get_repo(repo_slug) if not repo: raise tornado.web.HTTPError(404, "Could not find repo: %s" % repo_slug) - self.render("repos/show.html", repo=repo, distro=repo.distro) + return repo +class ShowHandler(BaseHandler): + def get(self, **kwargs): + # Fetch the repository + repo = self._get_repo(**kwargs) -class BuildsHandler(base.BaseHandler): - def get(self, distro_slug, repo_slug): - # Find the distribution - distro = self.backend.distros.get_by_slug(distro_slug) - if not distro: - raise tornado.web.HTTPError(404, "Could not find distro: %s" % distro_slug) + self.render("repos/show.html", repo=repo, distro=repo.distro) - # Find the repository - repo = distro.get_repo(repo_slug) - if not repo: - raise tornado.web.HTTPError(404, "Could not find repo: %s" % repo_slug) - self.render("repos/builds.html", repo=repo, distro=repo.distro) +class BuildsHandler(BaseHandler): + def get(self, **kwargs): + # Fetch the repository + repo = self._get_repo(**kwargs) + self.render("repos/builds.html", repo=repo, distro=repo.distro) -class ConfigHandler(base.BaseHandler): - def get(self, distro_slug, repo_slug): - # Find the distribution - distro = self.backend.distros.get_by_slug(distro_slug) - if not distro: - raise tornado.web.HTTPError(404, "Could not find distro: %s" % distro_slug) - # Find the repository - repo = distro.get_repo(repo_slug) - if not repo: - raise tornado.web.HTTPError(404, "Could not find repo: %s" % repo_slug) +class ConfigHandler(BaseHandler): + def get(self, **kwargs): + # Fetch the repository + repo = self._get_repo(**kwargs) # This is plain text self.set_header("Content-Type", "text/plain") @@ -76,17 +80,10 @@ class ConfigHandler(base.BaseHandler): self.finish(config) -class MirrorlistHandler(base.BaseHandler): - def get(self, distro_slug, repo_slug): - # Find the distribution - distro = self.backend.distros.get_by_slug(distro_slug) - if not distro: - raise tornado.web.HTTPError(404, "Could not find distro: %s" % distro_slug) - - # Find the repository - repo = distro.get_repo(repo_slug) - if not repo: - raise tornado.web.HTTPError(404, "Could not find repo: %s" % repo_slug) +class MirrorlistHandler(BaseHandler): + def get(self, **kwargs): + # Fetch the repository + repo = self._get_repo(**kwargs) # Send nothing if repository isn't supposed to be mirrored if not repo.mirrored: