diff options
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/ci/build.rb | 5 | ||||
-rw-r--r-- | app/models/environment.rb | 20 | ||||
-rw-r--r-- | app/models/merge_request.rb | 10 | ||||
-rw-r--r-- | app/models/project.rb | 13 | ||||
-rw-r--r-- | app/models/repository.rb | 2 |
5 files changed, 16 insertions, 34 deletions
diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index 44d4fb9d8d8..39b0f70a5ae 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -9,6 +9,7 @@ module Ci belongs_to :erased_by, class_name: 'User' has_many :deployments, as: :deployable + has_one :last_deployment, -> { order('deployments.id DESC') }, as: :deployable, class_name: 'Deployment' # The "environment" field for builds is a String, and is the unexpanded name def persisted_environment @@ -183,10 +184,6 @@ module Ci success? && !last_deployment.try(:last?) end - def last_deployment - deployments.last - end - def depends_on_builds # Get builds of the same type latest_builds = self.pipeline.builds.latest diff --git a/app/models/environment.rb b/app/models/environment.rb index ed18e6bdea1..14787f79a36 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -6,7 +6,8 @@ class Environment < ActiveRecord::Base belongs_to :project, required: true, validate: true - has_many :deployments + has_many :deployments, dependent: :destroy + has_one :last_deployment, -> { order('deployments.id DESC') }, class_name: 'Deployment' before_validation :nullify_external_url before_validation :generate_slug, if: ->(env) { env.slug.blank? } @@ -37,6 +38,7 @@ class Environment < ActiveRecord::Base scope :available, -> { with_state(:available) } scope :stopped, -> { with_state(:stopped) } + scope :order_by_last_deployed_at, -> { order(Gitlab::Database.nulls_first_order('(SELECT MAX(id) FROM deployments WHERE deployments.environment_id = environments.id)', 'ASC')) } state_machine :state, initial: :available do event :start do @@ -51,14 +53,6 @@ class Environment < ActiveRecord::Base state :stopped end - def self.latest_for_commit(environments, commit) - environments.sort_by do |environment| - deployment = environment.first_deployment_for(commit) - - deployment.try(:created_at) || DateTime.parse('1970-01-01') - end.last - end - def predefined_variables [ { key: 'CI_ENVIRONMENT_NAME', value: name, public: true }, @@ -70,10 +64,6 @@ class Environment < ActiveRecord::Base ref.to_s == last_deployment.try(:ref) end - def last_deployment - deployments.last - end - def nullify_external_url self.external_url = nil if self.external_url.blank? end @@ -95,6 +85,10 @@ class Environment < ActiveRecord::Base last_deployment.includes_commit?(commit) end + def last_deployed_at + last_deployment.try(:created_at) + end + def update_merge_request_metrics? (environment_type || name) == "production" end diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb index 0155073a1c9..965315c42a8 100644 --- a/app/models/merge_request.rb +++ b/app/models/merge_request.rb @@ -720,21 +720,15 @@ class MergeRequest < ActiveRecord::Base @environments ||= begin target_envs = target_project.environments_for( - target_branch, commit: diff_head_commit, with_tags: true) + ref: target_branch, commit: diff_head_commit, with_tags: true) source_envs = source_project.environments_for( - source_branch, commit: diff_head_commit) if source_project + ref: source_branch, commit: diff_head_commit) if source_project (target_envs.to_a + source_envs.to_a).uniq end end - def latest_environment - return @latest_environment if defined?(@latest_environment) - - @latest_environment = Environment.latest_for_commit(environments, diff_head_commit) - end - def state_human_name if merged? "Merged" diff --git a/app/models/project.rb b/app/models/project.rb index 15c6e25e73f..ff4487b6c8c 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1306,7 +1306,7 @@ class Project < ActiveRecord::Base Gitlab::Redis.with { |redis| redis.del(pushes_since_gc_redis_key) } end - def environments_for(ref, commit: nil, with_tags: false) + def environments_for(ref: nil, commit: nil, with_tags: false) deps = if ref deployments_query = with_tags ? 'ref = ? OR tag IS TRUE' : 'ref = ?' @@ -1322,22 +1322,17 @@ class Project < ActiveRecord::Base .select(:environment_id) environments_found = environments.available - .where(id: environment_ids).to_a + .where(id: environment_ids).order_by_last_deployed_at.to_a - return environments_found unless commit + return environments_found unless ref && commit environments_found.select do |environment| environment.includes_commit?(commit) end end - def latest_environment_for(commit, ref: nil) - environments = environments_for(ref, commit: commit) - Environment.latest_for_commit(environments, commit) - end - def environments_recently_updated_on_branch(branch) - environments_for(branch).select do |environment| + environments_for(ref: branch).select do |environment| environment.recently_updated_on_branch?(branch) end end diff --git a/app/models/repository.rb b/app/models/repository.rb index ba9c038b66d..c043753507e 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -1190,6 +1190,7 @@ class Repository def route_map_for(sha) blob = blob_at(sha, ROUTE_MAP_PATH) return unless blob + blob.load_all_data!(self) blob.data end @@ -1197,6 +1198,7 @@ class Repository def gitlab_ci_yml_for(sha) blob = blob_at(sha, GITLAB_CI_YML_PATH) return unless blob + blob.load_all_data!(self) blob.data end |