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

Refactor ci_status on MergeRequestController

parent 8a1064d7
...@@ -122,8 +122,8 @@ ...@@ -122,8 +122,8 @@
} }
.js-deployment-link { .js-deployment-link {
display: inline-block; display: inline-block;
} }
.mr-widget-body { .mr-widget-body {
h4 { h4 {
......
...@@ -393,33 +393,28 @@ class Projects::MergeRequestsController < Projects::ApplicationController ...@@ -393,33 +393,28 @@ class Projects::MergeRequestsController < Projects::ApplicationController
end end
end end
environments = @merge_request.environments environments = @merge_request.environments.map do |environment|
deployments = @merge_request.deployments next unless can?(current_user, :read_environment, environment)
if environments.present? deployment = environment.first_deployment_for(@merge_request.diff_head_commit)
environments = environments.select { |e| can?(current_user, :read_environment, e) }.map do |environment| environment = {
project = environment.project name: environment.name,
deployment = deployments.find { |d| d.environment == environment } id: environment.id,
url: namespace_project_environment_path(@project.namespace, @project, environment),
environment = { external_url: environment.external_url,
name: environment.name, deployed_at: deployment ? deployment.created_at : nil
id: environment.id, }
url: namespace_project_environment_path(project.namespace, project, environment),
external_url: environment.external_url, if environment[:external_url]
deployed_at: deployment ? deployment.created_at : nil environment[:external_url_formatted] = environment[:external_url].gsub(/\A.*?:\/\//, '')
} end
if environment[:external_url]
environment[:external_url_formatted] = environment[:external_url].gsub(/\A.*?:\/\//, '')
end
if environment[:deployed_at]
environment[:deployed_at_formatted] = environment[:deployed_at].to_time.in_time_zone.to_s(:medium)
end
environment if environment[:deployed_at]
environment[:deployed_at_formatted] = environment[:deployed_at].to_time.in_time_zone.to_s(:medium)
end end
end
environment
end.compact
response = { response = {
title: merge_request.title, title: merge_request.title,
......
...@@ -48,12 +48,13 @@ class Environment < ActiveRecord::Base ...@@ -48,12 +48,13 @@ class Environment < ActiveRecord::Base
self.name == "production" self.name == "production"
end end
def deployment_id_for(commit) def first_deployment_for(commit)
ref = project.repository.ref_name_for_sha(ref_path, commit.sha) ref = project.repository.ref_name_for_sha(ref_path, commit.sha)
return nil unless ref return nil unless ref
ref.split('/').last.to_i deployment_id = ref.split('/').last.to_i
deployments.find(deployment_id)
end end
def ref_path def ref_path
......
...@@ -685,24 +685,18 @@ class MergeRequest < ActiveRecord::Base ...@@ -685,24 +685,18 @@ class MergeRequest < ActiveRecord::Base
!pipeline || pipeline.success? !pipeline || pipeline.success?
end 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 def environments
return [] unless diff_head_commit return [] unless diff_head_commit
environments = source_project.environments_for( @environments ||=
source_branch, diff_head_commit) begin
environments += target_project.environments_for( environments = source_project.environments_for(
target_branch, diff_head_commit, with_tags: true) source_branch, diff_head_commit)
environments += target_project.environments_for(
target_branch, diff_head_commit, with_tags: true)
environments.uniq environments.uniq
end
end end
def state_human_name def state_human_name
......
...@@ -719,8 +719,8 @@ class Repository ...@@ -719,8 +719,8 @@ class Repository
end end
end end
def ref_name_for_sha(environment_ref_path, sha) def ref_name_for_sha(ref_path, sha)
args = %W(#{Gitlab.config.git.bin_path} for-each-ref --count=1 #{environment_ref_path} --contains #{sha}) args = %W(#{Gitlab.config.git.bin_path} for-each-ref --count=1 #{ref_path} --contains #{sha})
# Not found -> ["", 0] # Not found -> ["", 0]
# Found -> ["b8d95eb4969eefacb0a58f6a28f6803f8070e7b9 commit\trefs/environments/production/77\n", 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