summaryrefslogtreecommitdiff
path: root/lib/gitlab/etag_caching/router/restful.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2021-03-16 18:18:33 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2021-03-16 18:18:33 +0000
commitf64a639bcfa1fc2bc89ca7db268f594306edfd7c (patch)
treea2c3c2ebcc3b45e596949db485d6ed18ffaacfa1 /lib/gitlab/etag_caching/router/restful.rb
parentbfbc3e0d6583ea1a91f627528bedc3d65ba4b10f (diff)
downloadgitlab-ce-f64a639bcfa1fc2bc89ca7db268f594306edfd7c.tar.gz
Add latest changes from gitlab-org/gitlab@13-10-stable-eev13.10.0-rc40
Diffstat (limited to 'lib/gitlab/etag_caching/router/restful.rb')
-rw-r--r--lib/gitlab/etag_caching/router/restful.rb112
1 files changed, 112 insertions, 0 deletions
diff --git a/lib/gitlab/etag_caching/router/restful.rb b/lib/gitlab/etag_caching/router/restful.rb
new file mode 100644
index 00000000000..08c20e30a48
--- /dev/null
+++ b/lib/gitlab/etag_caching/router/restful.rb
@@ -0,0 +1,112 @@
+# frozen_string_literal: true
+
+module Gitlab
+ module EtagCaching
+ module Router
+ class Restful
+ extend EtagCaching::Router::Helpers
+
+ # We enable an ETag for every request matching the regex.
+ # To match a regex the path needs to match the following:
+ # - Don't contain a reserved word (expect for the words used in the
+ # regex itself)
+ # - Ending in `noteable/issue/<id>/notes` for the `issue_notes` route
+ # - Ending in `issues/id`/realtime_changes` for the `issue_title` route
+ USED_IN_ROUTES = %w[noteable issue notes issues realtime_changes
+ commit pipelines merge_requests builds
+ new environments].freeze
+ RESERVED_WORDS = Gitlab::PathRegex::ILLEGAL_PROJECT_PATH_WORDS - USED_IN_ROUTES
+ RESERVED_WORDS_REGEX = Regexp.union(*RESERVED_WORDS.map(&Regexp.method(:escape)))
+ RESERVED_WORDS_PREFIX = %Q(^(?!.*\/(#{RESERVED_WORDS_REGEX})\/).*)
+
+ ROUTES = [
+ [
+ %r(#{RESERVED_WORDS_PREFIX}/noteable/issue/\d+/notes\z),
+ 'issue_notes',
+ 'issue_tracking'
+ ],
+ [
+ %r(#{RESERVED_WORDS_PREFIX}/noteable/merge_request/\d+/notes\z),
+ 'merge_request_notes',
+ 'code_review'
+ ],
+ [
+ %r(#{RESERVED_WORDS_PREFIX}/issues/\d+/realtime_changes\z),
+ 'issue_title',
+ 'issue_tracking'
+ ],
+ [
+ %r(#{RESERVED_WORDS_PREFIX}/commit/\S+/pipelines\.json\z),
+ 'commit_pipelines',
+ 'continuous_integration'
+ ],
+ [
+ %r(#{RESERVED_WORDS_PREFIX}/merge_requests/new\.json\z),
+ 'new_merge_request_pipelines',
+ 'continuous_integration'
+ ],
+ [
+ %r(#{RESERVED_WORDS_PREFIX}/merge_requests/\d+/pipelines\.json\z),
+ 'merge_request_pipelines',
+ 'continuous_integration'
+ ],
+ [
+ %r(#{RESERVED_WORDS_PREFIX}/pipelines\.json\z),
+ 'project_pipelines',
+ 'continuous_integration'
+ ],
+ [
+ %r(#{RESERVED_WORDS_PREFIX}/pipelines/\d+\.json\z),
+ 'project_pipeline',
+ 'continuous_integration'
+ ],
+ [
+ %r(#{RESERVED_WORDS_PREFIX}/builds/\d+\.json\z),
+ 'project_build',
+ 'continuous_integration'
+ ],
+ [
+ %r(#{RESERVED_WORDS_PREFIX}/clusters/\d+/environments\z),
+ 'cluster_environments',
+ 'continuous_delivery'
+ ],
+ [
+ %r(#{RESERVED_WORDS_PREFIX}/environments\.json\z),
+ 'environments',
+ 'continuous_delivery'
+ ],
+ [
+ %r(#{RESERVED_WORDS_PREFIX}/import/github/realtime_changes\.json\z),
+ 'realtime_changes_import_github',
+ 'importers'
+ ],
+ [
+ %r(#{RESERVED_WORDS_PREFIX}/import/gitea/realtime_changes\.json\z),
+ 'realtime_changes_import_gitea',
+ 'importers'
+ ],
+ [
+ %r(#{RESERVED_WORDS_PREFIX}/merge_requests/\d+/cached_widget\.json\z),
+ 'merge_request_widget',
+ 'code_review'
+ ]
+ ].map(&method(:build_route)).freeze
+
+ # Overridden in EE to add more routes
+ def self.all_routes
+ ROUTES
+ end
+
+ def self.match(request)
+ all_routes.find { |route| route.match(request.path_info) }
+ end
+
+ def self.cache_key(request)
+ request.path
+ end
+ end
+ end
+ end
+end
+
+Gitlab::EtagCaching::Router::Restful.prepend_if_ee('EE::Gitlab::EtagCaching::Router::Restful')