summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2019-12-10 18:08:04 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2019-12-10 18:08:04 +0000
commit115c8ea7af7ef69ca3f09c333314546e9b5712f9 (patch)
treec3b6798c11e502f7d2785649f95d2255beac3c91 /lib
parent27d91a629918e417a9e87825e838209b9ace79c1 (diff)
downloadgitlab-ce-115c8ea7af7ef69ca3f09c333314546e9b5712f9.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib')
-rw-r--r--lib/api/api_guard.rb4
-rw-r--r--lib/gitlab/auth/user_auth_finders.rb16
-rw-r--r--lib/gitlab/diff/highlight_cache.rb34
3 files changed, 43 insertions, 11 deletions
diff --git a/lib/api/api_guard.rb b/lib/api/api_guard.rb
index 02ea321df67..d1535c43808 100644
--- a/lib/api/api_guard.rb
+++ b/lib/api/api_guard.rb
@@ -63,7 +63,9 @@ module API
end
def find_user_from_sources
- find_user_from_access_token || find_user_from_warden
+ find_user_from_access_token ||
+ find_user_from_job_token ||
+ find_user_from_warden
end
private
diff --git a/lib/gitlab/auth/user_auth_finders.rb b/lib/gitlab/auth/user_auth_finders.rb
index a8869f907e6..983682baab1 100644
--- a/lib/gitlab/auth/user_auth_finders.rb
+++ b/lib/gitlab/auth/user_auth_finders.rb
@@ -24,6 +24,8 @@ module Gitlab
PRIVATE_TOKEN_HEADER = 'HTTP_PRIVATE_TOKEN'
PRIVATE_TOKEN_PARAM = :private_token
+ JOB_TOKEN_HEADER = "HTTP_JOB_TOKEN".freeze
+ JOB_TOKEN_PARAM = :job_token
# Check the Rails session for valid authentication details
def find_user_from_warden
@@ -50,6 +52,20 @@ module Gitlab
User.find_by_feed_token(token) || raise(UnauthorizedError)
end
+ def find_user_from_job_token
+ return unless route_authentication_setting[:job_token_allowed]
+
+ token = (params[JOB_TOKEN_PARAM] || env[JOB_TOKEN_HEADER]).to_s
+ return unless token.present?
+
+ job = ::Ci::Build.find_by_token(token)
+ raise ::Gitlab::Auth::UnauthorizedError unless job
+
+ @current_authenticated_job = job # rubocop:disable Gitlab/ModuleWithInstanceVariables
+
+ job.user
+ end
+
# We only allow Private Access Tokens with `api` scope to be used by web
# requests on RSS feeds or ICS files for backwards compatibility.
# It is also used by GraphQL/API requests.
diff --git a/lib/gitlab/diff/highlight_cache.rb b/lib/gitlab/diff/highlight_cache.rb
index 3d511b9a5b2..bc1baa09d39 100644
--- a/lib/gitlab/diff/highlight_cache.rb
+++ b/lib/gitlab/diff/highlight_cache.rb
@@ -3,6 +3,8 @@
module Gitlab
module Diff
class HighlightCache
+ include Gitlab::Utils::StrongMemoize
+
EXPIRATION = 1.week
VERSION = 1
@@ -30,12 +32,11 @@ module Gitlab
# IO generated by N+1's (1 writing for each highlighted line or file).
#
def write_if_empty
- return if uncached_files.empty?
+ return if cacheable_files.empty?
new_cache_content = {}
- uncached_files.each do |diff_file|
- next unless cacheable?(diff_file)
+ cacheable_files.each do |diff_file|
new_cache_content[diff_file.file_path] = diff_file.highlighted_diff_lines.map(&:to_hash)
end
@@ -49,7 +50,9 @@ module Gitlab
end
def key
- @redis_key ||= ['highlighted-diff-files', diffable.cache_key, VERSION, diff_options].join(":")
+ strong_memoize(:redis_key) do
+ ['highlighted-diff-files', diffable.cache_key, VERSION, diff_options].join(":")
+ end
end
private
@@ -60,13 +63,17 @@ module Gitlab
# See https://gitlab.com/gitlab-org/gitlab/issues/38008
#
def deprecated_cache
- @deprecated_cache ||= Gitlab::Diff::DeprecatedHighlightCache.new(@diff_collection)
+ strong_memoize(:deprecated_cache) do
+ Gitlab::Diff::DeprecatedHighlightCache.new(@diff_collection)
+ end
end
- def uncached_files
- diff_files = @diff_collection.diff_files
+ def cacheable_files
+ strong_memoize(:cacheable_files) do
+ diff_files = @diff_collection.diff_files
- diff_files.select { |file| read_cache[file.file_path].nil? }
+ diff_files.select { |file| cacheable?(file) && read_file(file).nil? }
+ end
end
# Given a hash of:
@@ -95,13 +102,20 @@ module Gitlab
end
end
+ # Subsequent read_file calls would need the latest cache.
+ #
+ clear_memoization(:cached_content)
+ clear_memoization(:cacheable_files)
+
# Clean up any deprecated hash entries
#
deprecated_cache.clear
end
def file_paths
- @file_paths ||= @diff_collection.diffs.collect(&:file_path)
+ strong_memoize(:file_paths) do
+ @diff_collection.diffs.collect(&:file_path)
+ end
end
def read_file(diff_file)
@@ -109,7 +123,7 @@ module Gitlab
end
def cached_content
- @cached_content ||= read_cache
+ strong_memoize(:cached_content) { read_cache }
end
def read_cache