Welcome to mirror list, hosted at ThFree Co, Russian Federation.

application_context.rb « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b9190b519a0cf6b15a208c3e35712b276d656936 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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