Commit fa58068b authored by Z.J. van de Weg's avatar Z.J. van de Weg

Refactor ci_status on MergeRequestController

parent 8a1064d7
......@@ -393,18 +393,14 @@ class Projects::MergeRequestsController < Projects::ApplicationController
end
end
environments = @merge_request.environments
deployments = @merge_request.deployments
if environments.present?
environments = environments.select { |e| can?(current_user, :read_environment, e) }.map do |environment|
project = environment.project
deployment = deployments.find { |d| d.environment == environment }
environments = @merge_request.environments.map do |environment|
next unless can?(current_user, :read_environment, environment)
deployment = environment.first_deployment_for(@merge_request.diff_head_commit)
environment = {
name: environment.name,
id: environment.id,
url: namespace_project_environment_path(project.namespace, project, environment),
url: namespace_project_environment_path(@project.namespace, @project, environment),
external_url: environment.external_url,
deployed_at: deployment ? deployment.created_at : nil
}
......@@ -418,8 +414,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController
end
environment
end
end
end.compact
response = {
title: merge_request.title,
......
......@@ -48,12 +48,13 @@ class Environment < ActiveRecord::Base
self.name == "production"
end
def deployment_id_for(commit)
def first_deployment_for(commit)
ref = project.repository.ref_name_for_sha(ref_path, commit.sha)
return nil unless ref
ref.split('/').last.to_i
deployment_id = ref.split('/').last.to_i
deployments.find(deployment_id)
end
def ref_path
......
......@@ -685,18 +685,11 @@ class MergeRequest < ActiveRecord::Base
!pipeline || pipeline.success?
end
def deployments
deployment_ids =
environments.map do |environment|
environment.deployment_id_for(diff_head_commit)
end.compact
Deployment.find(deployment_ids)
end
def environments
return [] unless diff_head_commit
@environments ||=
begin
environments = source_project.environments_for(
source_branch, diff_head_commit)
environments += target_project.environments_for(
......@@ -704,6 +697,7 @@ class MergeRequest < ActiveRecord::Base
environments.uniq
end
end
def state_human_name
if merged?
......
......@@ -719,8 +719,8 @@ class Repository
end
end
def ref_name_for_sha(environment_ref_path, sha)
args = %W(#{Gitlab.config.git.bin_path} for-each-ref --count=1 #{environment_ref_path} --contains #{sha})
def ref_name_for_sha(ref_path, sha)
args = %W(#{Gitlab.config.git.bin_path} for-each-ref --count=1 #{ref_path} --contains #{sha})
# Not found -> ["", 0]
# Found -> ["b8d95eb4969eefacb0a58f6a28f6803f8070e7b9 commit\trefs/environments/production/77\n", 0]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment