Module: Debci::HTMLHelpers

Includes:
ActiveSupport::NumberHelper, ERB::Util
Included in:
Admin, App, Frontend, Status
Defined in:
lib/debci/html_helpers.rb

Constant Summary collapse

ICONS =
{
  pass: 'thumbs-up',
  neutral: 'minus-circle',
  fail: 'thumbs-down',
  fail_passed_never: ['thumbs-down', 'ban'],
  fail_passed_current: ['thumbs-down', 'bolt'],
  fail_passed_old: ['thumbs-down', 'arrow-down'],
  tmpfail_pass: 'thumbs-up',
  tmpfail_fail: 'thumbs-down',
  tmpfail: 'question-circle',
  no_test_data: 'question',
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.noticeObject



156
157
158
159
160
161
162
# File 'lib/debci/html_helpers.rb', line 156

def notice
  @notice ||=
    begin
      notice_file = File.join(Debci.config.config_dir, "notice.html")
      File.exist?(notice_file) ? File.read(notice_file) : ""
    end
end

.static_url(path) ⇒ Object



145
146
147
148
149
150
151
152
153
154
# File 'lib/debci/html_helpers.rb', line 145

def static_url(path)
  @static_url_cache ||= {}
  if @static_url_cache.key?(path)
    return @static_url_cache[path]
  end

  file = File.join(Debci.config.base_dir, 'public', path)
  tag = Digest::MD5.file(file).hexdigest
  @static_url_cache[path] = "#{path}?#{tag}"
end

Instance Method Details

#ansi_to_html(str) ⇒ Object

Converts ANSI codes to HTML. Escapes any HTML before even looking for ANSI codes.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/debci/html_helpers.rb', line 110

def ansi_to_html(str)
  return str unless str
  input = str.gsub('<', '&lt;').gsub('>', '&gt;')
  return input unless input =~ /\e\[/
  begin
    result = ANSI::BBCode.ansi_to_html(input)
    result = result[0..-8] if result =~ %r{<br />\n$}
    result
  rescue StandardError
    # in case anything goes wrong with the conversion, just remove all ANSI
    # escape codes
    input.gsub(/\e\[[0-9;]+m/, "")
  end
end

#base_urlObject



65
66
67
# File 'lib/debci/html_helpers.rb', line 65

def base_url
  "#{request.scheme}://#{request.host}#{['', request.port].compact.join(':')}"
end

#download_url(job) ⇒ Object



85
86
87
88
# File 'lib/debci/html_helpers.rb', line 85

def download_url(job)
  dir = [job.suite, job.arch, job.package.prefix, job.package.name].join('/')
  "/data/autopkgtest/#{dir}/#{job.run_id}/log.gz"
end

#expand_pin_packages(test) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/debci/html_helpers.rb', line 52

def expand_pin_packages(test)
  return [] unless test.pinned?

  test.pin_packages.map do |pin|
    *packages, suite = pin
    Array(packages).map do |pkglist|
      String(pkglist).split(/\s*,\s*/).map do |pkg|
        "#{pkg} from #{suite}"
      end
    end
  end.flatten
end

#expand_url(url, suite) ⇒ Object

expand { SUITE } macro in URLs



104
105
106
# File 'lib/debci/html_helpers.rb', line 104

def expand_url(url, suite)
  url&.gsub('{SUITE}', suite)
end


31
32
33
34
# File 'lib/debci/html_helpers.rb', line 31

def file_link(size, link_pattern)
  return unless size
  link_pattern % number_to_human_size(size)
end

#history_url(job) ⇒ Object



73
74
75
# File 'lib/debci/html_helpers.rb', line 73

def history_url(job)
  pkg_history_url(job.package, job.suite, job.arch)
end

#icon(status) ⇒ Object



24
25
26
27
28
29
# File 'lib/debci/html_helpers.rb', line 24

def icon(status)
  status ||= :no_test_data
  Array(ICONS[status.to_sym]).map do |i|
    "<i class='#{status} fa fa-#{i}'></i>"
  end.join(' ')
end

#javascript_tag(path) ⇒ Object



135
136
137
138
# File 'lib/debci/html_helpers.rb', line 135

def javascript_tag(path)
  url = static_url(path)
  "<script type=\"text/javascript\" src=\"#{url}\"></script>"
end


90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/debci/html_helpers.rb', line 90

def log_links(job)
  if !job.status
    "<span class='fa fa-clock-o'></span>"
  elsif job.files_purged?
    "<span class='fa fa-trash' title='file has been removed due to data retention policy'></span>"
  else
    file_link(
      job.log_size,
      "<a href=\"#{log_url(job)}\">test log</a> &nbsp; <a href=\"#{download_url(job)}\"><span class=\"fa fa-download\"></span></a> <small>(%s)</small>"
    )
  end
end

#log_url(job) ⇒ Object



81
82
83
# File 'lib/debci/html_helpers.rb', line 81

def log_url(job)
  "#{history_url(job)}#{job.run_id}/"
end

#noticeObject



140
141
142
# File 'lib/debci/html_helpers.rb', line 140

def notice
  Debci::HTMLHelpers.notice
end

#package_url(package) ⇒ Object



77
78
79
# File 'lib/debci/html_helpers.rb', line 77

def package_url(package)
  "/packages/#{package.prefix}/#{package.name}/"
end

#pkg_history_url(package, suite, arch) ⇒ Object



69
70
71
# File 'lib/debci/html_helpers.rb', line 69

def pkg_history_url(package, suite, arch)
  "/packages/#{package.prefix}/#{package.name}/#{suite}/#{arch}/"
end

#static_url(path) ⇒ Object

Returns a content-based URL to a static file



126
127
128
# File 'lib/debci/html_helpers.rb', line 126

def static_url(path)
  Debci::HTMLHelpers.static_url(path)
end

#stylesheet_tag(path) ⇒ Object



130
131
132
133
# File 'lib/debci/html_helpers.rb', line 130

def stylesheet_tag(path)
  url = static_url(path)
  "<link rel=\"stylesheet\" type=\"text/css\" href=\"#{url}\"/>"
end

#title_test_trigger_pin(test) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/debci/html_helpers.rb', line 36

def title_test_trigger_pin(test)
  title = ''
  unless test.trigger.blank?
    title << "Trigger:\n"
    title << h(test.trigger)
  end
  if test.pinned?
    title << "\n\n" unless test.trigger.blank?
    title << "Pinned packages:\n"
    expand_pin_packages(test).each do |pin|
      title << pin << "\n"
    end
  end
  title
end