Class: Gitlab::Triage::Resource::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/triage/resource/base.rb

Constant Summary collapse

CONFIDENTIAL_TEXT =
'(confidential)'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, parent: nil, network: nil, redact_confidentials: true) ⇒ Base

Returns a new instance of Base.



23
24
25
26
27
28
29
# File 'lib/gitlab/triage/resource/base.rb', line 23

def initialize(
  resource, parent: nil, network: nil, redact_confidentials: true)
  @resource = resource
  @parent = parent
  @network = network
  @redact_confidentials = redact_confidentials
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



9
10
11
# File 'lib/gitlab/triage/resource/base.rb', line 9

def parent
  @parent
end

#resourceObject (readonly)

Returns the value of attribute resource.



9
10
11
# File 'lib/gitlab/triage/resource/base.rb', line 9

def resource
  @resource
end

Class Method Details

.define_field(name, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/gitlab/triage/resource/base.rb', line 13

def self.define_field(name, &block)
  define_method(name) do
    if redact_confidential_attributes?
      CONFIDENTIAL_TEXT
    else
      instance_eval(&block)
    end
  end
end

Instance Method Details

#build_url(params: {}, options: {}) ⇒ Object (private)



75
76
77
78
79
80
81
# File 'lib/gitlab/triage/resource/base.rb', line 75

def build_url(params: {}, options: {})
  UrlBuilders::UrlBuilder.new(
    url_opts
      .merge(options)
      .merge(params: { per_page: 100 }.merge(params))
  ).build
end

#expand_resource!(params: {}) ⇒ Object (private)



44
45
46
47
# File 'lib/gitlab/triage/resource/base.rb', line 44

def expand_resource!(params: {})
  resource.merge!(
    network.query_api_cached(resource_url(params: params)).first)
end

#networkObject (protected)



38
39
40
# File 'lib/gitlab/triage/resource/base.rb', line 38

def network
  parent&.network || @network
end

#redact_confidential_attributes?Boolean (protected)

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/gitlab/triage/resource/base.rb', line 33

def redact_confidential_attributes?
  parent&.redact_confidential_attributes? ||
    (@redact_confidentials && resource[:confidential])
end

#resource_idObject (private)



61
62
63
# File 'lib/gitlab/triage/resource/base.rb', line 61

def resource_id
  resource[:iid]
end

#resource_url(params: {}, sub_resource_type: nil) ⇒ Object (private)



65
66
67
68
69
70
71
72
73
# File 'lib/gitlab/triage/resource/base.rb', line 65

def resource_url(params: {}, sub_resource_type: nil)
  build_url(
    params: params,
    options: {
      resource_id: resource_id,
      sub_resource_type: sub_resource_type
    }
  )
end

#sourceObject (private)



92
93
94
95
96
97
98
# File 'lib/gitlab/triage/resource/base.rb', line 92

def source
  if resource[:project_id]
    'projects'
  elsif resource[:group_id]
    'groups'
  end
end

#source_resourceObject (private)



49
50
51
# File 'lib/gitlab/triage/resource/base.rb', line 49

def source_resource
  @source_resource ||= network.query_api_cached(source_url).first
end

#source_urlObject (private)



53
54
55
# File 'lib/gitlab/triage/resource/base.rb', line 53

def source_url
  build_url(options: { resource_type: nil })
end

#url(params = {}) ⇒ Object (private)



57
58
59
# File 'lib/gitlab/triage/resource/base.rb', line 57

def url(params = {})
  build_url(params: params)
end

#url_optsObject (private)



83
84
85
86
87
88
89
90
# File 'lib/gitlab/triage/resource/base.rb', line 83

def url_opts
  {
    network_options: network.options,
    resource_type: self.class.name.demodulize.underscore.pluralize,
    source: source,
    source_id: resource[:"#{source.singularize}_id"]
  }
end