summaryrefslogtreecommitdiff
path: root/app/components/diffs/stats_component.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/components/diffs/stats_component.rb')
-rw-r--r--app/components/diffs/stats_component.rb67
1 files changed, 67 insertions, 0 deletions
diff --git a/app/components/diffs/stats_component.rb b/app/components/diffs/stats_component.rb
new file mode 100644
index 00000000000..55589c7b015
--- /dev/null
+++ b/app/components/diffs/stats_component.rb
@@ -0,0 +1,67 @@
+# frozen_string_literal: true
+
+module Diffs
+ class StatsComponent < BaseComponent
+ attr_reader :diff_files
+
+ def initialize(diff_files:)
+ @diff_files = diff_files
+ @changed ||= diff_files.size
+ @added ||= diff_files.sum(&:added_lines)
+ @removed ||= diff_files.sum(&:removed_lines)
+ end
+
+ def diff_files_data
+ diffs_map = @diff_files.map do |f|
+ {
+ href: "##{helpers.hexdigest(f.file_path)}",
+ title: f.new_path,
+ name: f.file_path,
+ path: diff_file_path_text(f),
+ icon: diff_file_changed_icon(f),
+ iconColor: "#{diff_file_changed_icon_color(f)}",
+ added: f.added_lines,
+ removed: f.removed_lines
+ }
+ end
+
+ Gitlab::Json.dump(diffs_map)
+ end
+
+ # Disabled undercoverage reports for this method
+ # as it returns a false positive on the last line,
+ # which is covered in the tests
+ #
+ # Issue: https://gitlab.com/gitlab-org/gitlab/-/issues/357381
+ #
+ # :nocov:
+ def diff_file_path_text(diff_file, max: 60)
+ path = diff_file.new_path
+
+ return path unless path.size > max && max > 3
+
+ "...#{path[-(max - 3)..]}"
+ end
+ # :nocov:
+
+ private
+
+ def diff_file_changed_icon(diff_file)
+ if diff_file.deleted_file?
+ "file-deletion"
+ elsif diff_file.new_file?
+ "file-addition"
+ else
+ "file-modified"
+ end
+ end
+
+ def diff_file_changed_icon_color(diff_file)
+ if diff_file.deleted_file?
+ "danger"
+ elsif diff_file.new_file?
+ "success"
+ end
+ end
+ end
+end