summaryrefslogtreecommitdiff
path: root/app/helpers/environment_helper.rb
blob: bd400009c96db1bdeda909e0518584bc4960e07e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# frozen_string_literal: true

module EnvironmentHelper
  # rubocop: disable CodeReuse/ActiveRecord
  def environment_for_build(project, build)
    return unless build.environment

    project.environments.find_by(name: build.expanded_environment_name)
  end
  # rubocop: enable CodeReuse/ActiveRecord

  def environment_link_for_build(project, build)
    environment = environment_for_build(project, build)
    if environment
      link_to environment.name, project_environment_path(project, environment)
    else
      content_tag :span, build.expanded_environment_name
    end
  end

  def deployment_path(deployment)
    [deployment.project.namespace.becomes(Namespace), deployment.project, deployment.deployable]
  end

  def deployment_link(deployment, text: nil)
    return unless deployment

    link_label = text || "##{deployment.iid}"

    link_to link_label, deployment_path(deployment)
  end

  def last_deployment_link_for_environment_build(project, build)
    environment = environment_for_build(project, build)
    return unless environment

    deployment_link(environment.last_deployment)
  end

  def render_deployment_status(deployment)
    status = deployment.status

    status_text =
      case status
      when 'created'
        s_('Deployment|created')
      when 'running'
        s_('Deployment|running')
      when 'success'
        s_('Deployment|success')
      when 'failed'
        s_('Deployment|failed')
      when 'canceled'
        s_('Deployment|canceled')
      end

    klass = "ci-status ci-#{status.dasherize}"
    text = "#{ci_icon_for_status(status)} #{status_text}".html_safe

    if deployment.deployable
      link_to(text, deployment_path(deployment), class: klass)
    else
      content_tag(:span, text, class: klass)
    end
  end
end