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 @@
}
.js-deployment-link {
display: inline-block;
}
display: inline-block;
}
.mr-widget-body {
h4 {
......
......@@ -393,33 +393,28 @@ 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 }
environment = {
name: environment.name,
id: environment.id,
url: namespace_project_environment_path(project.namespace, project, environment),
external_url: environment.external_url,
deployed_at: deployment ? deployment.created_at : nil
}
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
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),
external_url: environment.external_url,
deployed_at: deployment ? deployment.created_at : nil
}
if environment[:external_url]
environment[:external_url_formatted] = environment[:external_url].gsub(/\A.*?:\/\//, '')
end
environment
if environment[:deployed_at]
environment[:deployed_at_formatted] = environment[:deployed_at].to_time.in_time_zone.to_s(:medium)
end
end
environment
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,24 +685,18 @@ 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 = source_project.environments_for(
source_branch, diff_head_commit)
environments += target_project.environments_for(
target_branch, diff_head_commit, with_tags: true)
@environments ||=
begin
environments = source_project.environments_for(
source_branch, diff_head_commit)
environments += target_project.environments_for(
target_branch, diff_head_commit, with_tags: true)
environments.uniq
environments.uniq
end
end
def state_human_name
......
......@@ -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