Improve developer tooling around URLs in GitLab
Problem
We are inconsistent about how we use URLs (absolute vs relative) in GitLab and it causes a few different problems:
- URLs constructed on the frontend that do not take
relative_url_rootinto account break functionality for customers usingrelative_url_rootsetting - Some customers have reverse proxies set up where they have a different URL than what was set in the
external_urlin theirgitlab.rb. This means that using absolute URLs does not work. - Geo customers with Secondary site have issues with absolute URLs.
We have some guidance in https://docs.gitlab.com/development/fe_guide/frontend_faq/#5-should-one-use-a-full-url-or-a-full-path-when-referencing-backend-endpoints. But this guidance is hard to find in our docs and there is no tooling to enforce it so there are a lot of instances that slip through the cracks.
Proposal
Standardize web_url and web_path methods on models
- Create a new concern that has a
web_urlandweb_pathmethod - Add that concern to all models that are listed in lib/gitlab/url_builder.rb
- Remove direct usages of
Gitlab::UrlBuilder#build
Make webPath available in APIs
Any API that exposes webUrl should also expose webPath. Search through the following directories:
app/graphql/typesapp/graphql/types/app/serializers/ee/app/serializers/lib/api/entities/ee/lib/api/entities
Linting
- Add ESLint rule to prevent
webUrlin Vue and JS code.webPathshould be used instead. - Add ESLint rule for usages of "
/${}" orbase: '/' - Add RoboCop rule to prevent usage of
*_urlin HAML files and controllers (e.g.dashboard_projects_url) and instead use*_path(e.g.dashboard_projects_path)- This one could be tricky. I am not sure if we can lint for just Rails URL helpers. Also there may be instances where we need to use
*_url(emails?).
- This one could be tricky. I am not sure if we can lint for just Rails URL helpers. Also there may be instances where we need to use
- Add RoboCop rule for direct usages of
Gitlab::UrlBuilder#build. - Add RoboCop rule for APIs that expose
webUrlbut notwebPath
Documentation
Add documentation to the following locations about how to use URLs
- https://docs.gitlab.com/development/fe_guide/haml/
- https://docs.gitlab.com/development/fe_guide/vue/
- https://docs.gitlab.com/development/fe_guide/graphql/
- https://docs.gitlab.com/development/fe_guide/frontend_faq/#5-should-one-use-a-full-url-or-a-full-path-when-referencing-backend-endpoints
- https://docs.gitlab.com/development/backend/ruby_style_guide/
Edited by Peter Hegman