diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-01-03 15:08:33 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-01-03 15:08:33 +0000 |
commit | 511e761b41b81484c85e3d125f45873ce38e9201 (patch) | |
tree | 6bb98a6356de6e1d736951d2eef6ec83e6aa3dd2 /lib/gitlab/application_context.rb | |
parent | 4247e67be1faa9d52691757dad954a7fa63e8bfe (diff) | |
download | gitlab-ce-511e761b41b81484c85e3d125f45873ce38e9201.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib/gitlab/application_context.rb')
-rw-r--r-- | lib/gitlab/application_context.rb | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/lib/gitlab/application_context.rb b/lib/gitlab/application_context.rb new file mode 100644 index 00000000000..b9190b519a0 --- /dev/null +++ b/lib/gitlab/application_context.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +module Gitlab + # A GitLab-rails specific accessor for `Labkit::Logging::ApplicationContext` + class ApplicationContext + include Gitlab::Utils::LazyAttributes + + def self.with_context(args, &block) + application_context = new(**args) + Labkit::Context.with_context(application_context.to_lazy_hash, &block) + end + + def self.push(args) + application_context = new(**args) + Labkit::Context.push(application_context.to_lazy_hash) + end + + def initialize(user: nil, project: nil, namespace: nil) + @user, @project, @namespace = user, project, namespace + end + + def to_lazy_hash + { user: -> { username }, + project: -> { project_path }, + root_namespace: -> { root_namespace_path } } + end + + private + + lazy_attr_reader :user, type: User + lazy_attr_reader :project, type: Project + lazy_attr_reader :namespace, type: Namespace + + def project_path + project&.full_path + end + + def username + user&.username + end + + def root_namespace_path + if namespace + namespace.full_path_components.first + else + project&.full_path_components&.first + end + end + end +end |