From 9fc4f2e5dbe97a3d26aa8f462951712d7581132e Mon Sep 17 00:00:00 2001 From: William Braeckman Date: Thu, 2 Jan 2025 12:13:39 +0100 Subject: [PATCH] [IMP] runbot: allow custom run path per user Adds a new setting on users to define a custom path to use when accessing a running build. Can be used to directly access a specific page instead of landing on the home page from a runbot run. --- runbot/controllers/frontend.py | 11 ++++++++++- runbot/models/ir_qweb.py | 5 +++++ runbot/models/res_users.py | 9 ++++++++- runbot/templates/nginx.xml | 15 +++++++++++---- runbot/templates/utils.xml | 6 ++++++ runbot/views/user.xml | 3 +++ 6 files changed, 43 insertions(+), 6 deletions(-) diff --git a/runbot/controllers/frontend.py b/runbot/controllers/frontend.py index a008ba98..ca0af45f 100644 --- a/runbot/controllers/frontend.py +++ b/runbot/controllers/frontend.py @@ -85,7 +85,7 @@ class Runbot(Controller): @o_route([ '/runbot/submit' ], type='http', auth="public", methods=['GET', 'POST'], csrf=False) - def submit(self, more=False, redirect='/', keep_search=False, category=False, filter_mode=False, update_triggers=False, **kwargs): + def submit(self, more=False, redirect='/', keep_search=False, category=False, filter_mode=False, update_triggers=False, build_url_path=None, **kwargs): assert redirect.startswith('/') response = werkzeug.utils.redirect(redirect) response.set_cookie('more', '1' if more else '0') @@ -101,6 +101,11 @@ class Runbot(Controller): response.delete_cookie(key) else: response.set_cookie(key, '-'.join(enabled_triggers)) + if build_url_path: + if request.env.user._is_public(): + response.set_cookie('build_url_path', build_url_path) + else: + request.env.user.build_url_path = build_url_path return response @route(['/', @@ -656,6 +661,10 @@ class Runbot(Controller): def access_running(self, build_id, db_suffix=None, **kwargs): build = request.env['runbot.build'].browse(int(build_id)).exists() run_url = build._get_run_url(db_suffix) + if path := request.env.user.build_url_path: + if not path.startswith('/'): + path = f'/{path}' + run_url += path _logger.info('Redirecting to %s', run_url) return werkzeug.utils.redirect(run_url) diff --git a/runbot/models/ir_qweb.py b/runbot/models/ir_qweb.py index 936d3e46..377d845e 100644 --- a/runbot/models/ir_qweb.py +++ b/runbot/models/ir_qweb.py @@ -10,4 +10,9 @@ class IrQweb(models.AbstractModel): response = super()._prepare_frontend_environment(values) values['s2human'] = s2human values['s2human_long'] = s2human_long + + values['build_url_path'] = ( + request.httprequest.cookies.get('build_url_path', self.env.user.build_url_path) + ) + return response diff --git a/runbot/models/res_users.py b/runbot/models/res_users.py index 25a1a18d..9f16155f 100644 --- a/runbot/models/res_users.py +++ b/runbot/models/res_users.py @@ -13,6 +13,7 @@ class ResUsers(models.Model): # Add default action_id action_id = fields.Many2one('ir.actions.actions', default=lambda self: self.env.ref('runbot.open_view_warning_tree', raise_if_not_found=False)) + build_url_path = fields.Char(related="res_users_settings_id.build_url_path", readonly=False) _sql_constraints = [ ( @@ -24,9 +25,15 @@ class ResUsers(models.Model): @property def SELF_WRITEABLE_FIELDS(self): - return super().SELF_WRITEABLE_FIELDS + ['github_login'] + return super().SELF_WRITEABLE_FIELDS + ['github_login', 'build_url_path'] def write(self, values): if list(values.keys()) == ['github_login'] and self.env.user.has_group('runbot.group_runbot_team_manager'): return super(ResUsers, self.sudo()).write(values) return super().write(values) + + +class ResUsersSettings(models.Model): + _inherit = 'res.users.settings' + + build_url_path = fields.Char("Build run path", default='', help="Default path when entering a running build.") diff --git a/runbot/templates/nginx.xml b/runbot/templates/nginx.xml index 6c7da41d..739d24cd 100644 --- a/runbot/templates/nginx.xml +++ b/runbot/templates/nginx.xml @@ -77,19 +77,26 @@ server { + + + + + + + server { listen 8080; - server_name ~^(-[a-z0-9_-]+)?-(-[a-z0-9_]+)\.$; + server_name ~^-\.$; - location / { + location ~ /(?<path>.*) { - return 307 http://$1.; + return 307 http://$db./$path; } } server { listen 8080; - server_name ~^(-[a-z0-9_-]+)?\.$; + server_name ~^(-[a-z0-9_-]+)?\.$; location / { proxy_pass http://127.0.0.1:; } location /longpolling { proxy_pass http://127.0.0.1:; } diff --git a/runbot/templates/utils.xml b/runbot/templates/utils.xml index ee39557e..31ebee70 100644 --- a/runbot/templates/utils.xml +++ b/runbot/templates/utils.xml @@ -139,6 +139,12 @@