BigW Consortium Gitlab

ci_status_helper.rb 2.63 KB
Newer Older
1
module CiStatusHelper
2 3
  def ci_status_path(pipeline)
    project = pipeline.project
Filipa Lacerda committed
4
    namespace_project_pipeline_path(project.namespace, project, pipeline)
Kamil Trzcinski committed
5 6
  end

7
  # Is used by Commit and Merge Request Widget
8
  def ci_label_for_status(status)
9 10 11 12
    if detailed_status?(status)
      return status.label
    end

13 14
    case status
    when 'success'
15
      'passed'
16 17
    when 'success_with_warnings'
      'passed with warnings'
18 19
    else
      status
20 21 22
    end
  end

23 24 25 26 27
  def ci_status_for_statuseable(subject)
    status = subject.try(:status) || 'not found'
    status.humanize
  end

28
  def ci_icon_for_status(status)
29 30 31 32
    if detailed_status?(status)
      return custom_icon(status.icon)
    end

33
    icon_name =
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
      case status
      when 'success'
        'icon_status_success'
      when 'success_with_warnings'
        'icon_status_warning'
      when 'failed'
        'icon_status_failed'
      when 'pending'
        'icon_status_pending'
      when 'running'
        'icon_status_running'
      when 'play'
        'icon_play'
      when 'created'
        'icon_status_created'
      when 'skipped'
        'icon_status_skipped'
51
      else
52
        'icon_status_canceled'
53 54
      end

55
    custom_icon(icon_name)
56
  end
57

58
  def render_commit_status(commit, ref: nil, tooltip_placement: 'auto left')
59
    project = commit.project
60 61 62 63 64 65 66
    path = pipelines_namespace_project_commit_path(
      project.namespace,
      project,
      commit)

    render_status_with_link(
      'commit',
67
      commit.status(ref),
68 69
      path,
      tooltip_placement: tooltip_placement)
70 71 72 73 74
  end

  def render_pipeline_status(pipeline, tooltip_placement: 'auto left')
    project = pipeline.project
    path = namespace_project_pipeline_path(project.namespace, project, pipeline)
75
    render_status_with_link('pipeline', pipeline.status, path, tooltip_placement: tooltip_placement)
76 77
  end

78 79 80 81 82
  def no_runners_for_project?(project)
    project.runners.blank? &&
      Ci::Runner.shared.blank?
  end

83
  def render_status_with_link(type, status, path = nil, tooltip_placement: 'auto left', cssclass: '', container: 'body')
84 85
    klass = "ci-status-link ci-status-icon-#{status.dasherize} #{cssclass}"
    title = "#{type.titleize}: #{ci_label_for_status(status)}"
86
    data = { toggle: 'tooltip', placement: tooltip_placement, container: container }
87

88 89 90 91 92 93 94
    if path
      link_to ci_icon_for_status(status), path,
              class: klass, title: title, data: data
    else
      content_tag :span, ci_icon_for_status(status),
              class: klass, title: title, data: data
    end
95
  end
96 97 98 99 100 101

  def detailed_status?(status)
    status.respond_to?(:text) &&
      status.respond_to?(:label) &&
      status.respond_to?(:icon)
  end
102
end