Class: EnvironmentStatus

Inherits:
Object
  • Object
show all
Includes:
Gitlab::Utils::StrongMemoize
Defined in:
app/models/environment_status.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Constructor Details

#initialize(project, environment, merge_request, sha) ⇒ EnvironmentStatus

Returns a new instance of EnvironmentStatus.


45
46
47
48
49
50
# File 'app/models/environment_status.rb', line 45

def initialize(project, environment, merge_request, sha)
  @project = project
  @environment = environment
  @merge_request = merge_request
  @sha = sha
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment


6
7
8
# File 'app/models/environment_status.rb', line 6

def environment
  @environment
end

#merge_requestObject (readonly)

Returns the value of attribute merge_request


6
7
8
# File 'app/models/environment_status.rb', line 6

def merge_request
  @merge_request
end

#projectObject (readonly)

Returns the value of attribute project


6
7
8
# File 'app/models/environment_status.rb', line 6

def project
  @project
end

#shaObject (readonly)

Returns the value of attribute sha


6
7
8
# File 'app/models/environment_status.rb', line 6

def sha
  @sha
end

Class Method Details

.after_merge_request(mr, user) ⇒ Object


17
18
19
20
21
# File 'app/models/environment_status.rb', line 17

def self.after_merge_request(mr, user)
  return [] unless mr.merged?

  build_environments_status(mr, user, mr.merge_pipeline)
end

.for_deployed_merge_request(mr, user) ⇒ Object


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/environment_status.rb', line 23

def self.for_deployed_merge_request(mr, user)
  statuses = []

  mr.recent_visible_deployments.each do |deploy|
    env = deploy.environment

    next unless Ability.allowed?(user, :read_environment, env)

    statuses <<
      EnvironmentStatus.new(deploy.project, env, mr, deploy.sha)
  end

  # Existing projects that used deployments prior to the introduction of
  # explicitly linked merge requests won't have any data using this new
  # approach, so we fall back to retrieving deployments based on CI pipelines.
  if statuses.any?
    statuses
  else
    after_merge_request(mr, user)
  end
end

.for_merge_request(mr, user) ⇒ Object


13
14
15
# File 'app/models/environment_status.rb', line 13

def self.for_merge_request(mr, user)
  build_environments_status(mr, user, mr.actual_head_pipeline)
end

Instance Method Details

#changed_filesObject


70
71
72
73
# File 'app/models/environment_status.rb', line 70

def changed_files
  merge_request.merge_request_diff
    .merge_request_diff_files.where(deleted_file: false)
end

#changesObject


64
65
66
67
68
# File 'app/models/environment_status.rb', line 64

def changes
  strong_memoize(:changes) do
    has_route_map? ? changed_files.map { |file| build_change(file) }.compact : []
  end
end

#deploymentObject


52
53
54
55
56
# File 'app/models/environment_status.rb', line 52

def deployment
  strong_memoize(:deployment) do
    Deployment.where(environment: environment).find_by_sha(sha)
  end
end

#has_metrics?Boolean

Returns:

  • (Boolean)

58
59
60
61
62
# File 'app/models/environment_status.rb', line 58

def has_metrics?
  strong_memoize(:has_metrics) do
    deployment_metrics.has_metrics?
  end
end

#has_route_map?Boolean

Returns:

  • (Boolean)

75
76
77
# File 'app/models/environment_status.rb', line 75

def has_route_map?
  project.route_map_for(sha).present?
end