debian-mirror-gitlab/lib/gitlab/tree_summary.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

117 lines
3.4 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2018-11-20 20:47:30 +05:30
module Gitlab
class TreeSummary
include ::Gitlab::Utils::StrongMemoize
2020-05-24 23:13:21 +05:30
include ::MarkupHelper
CACHE_EXPIRE_IN = 1.hour
2022-10-11 01:57:18 +05:30
MAX_OFFSET = 2**31 - 1
2018-11-20 20:47:30 +05:30
2022-08-13 15:12:31 +05:30
attr_reader :commit, :project, :path, :offset, :limit, :user, :resolved_commits
2018-11-20 20:47:30 +05:30
2020-05-24 23:13:21 +05:30
def initialize(commit, project, user, params = {})
2018-11-20 20:47:30 +05:30
@commit = commit
@project = project
2020-05-24 23:13:21 +05:30
@user = user
2018-11-20 20:47:30 +05:30
@path = params.fetch(:path, nil).presence
2020-05-24 23:13:21 +05:30
@offset = [params.fetch(:offset, 0).to_i, MAX_OFFSET].min
2018-11-20 20:47:30 +05:30
@limit = (params.fetch(:limit, 25) || 25).to_i
# Ensure that if multiple tree entries share the same last commit, they share
# a ::Commit instance. This prevents us from rendering the same commit title
# multiple times
@resolved_commits = {}
end
# Creates a summary of the tree entries for a commit, within the window of
# entries defined by the offset and limit parameters. This consists of two
# return values:
#
# - An Array of Hashes containing the following keys:
# - file_name: The full path of the tree entry
# - commit: The last ::Commit to touch this entry in the tree
# - commit_path: URI of the commit in the web interface
2022-08-13 15:12:31 +05:30
# - commit_title_html: Rendered commit title
2018-11-20 20:47:30 +05:30
def summarize
2022-10-11 01:57:18 +05:30
return [] if offset < 0
2022-08-13 15:12:31 +05:30
commits_hsh = fetch_last_cached_commits_list
prerender_commit_full_titles!(commits_hsh.values)
2020-05-24 23:13:21 +05:30
2022-08-13 15:12:31 +05:30
commits_hsh.map do |path_key, commit|
commit = cache_commit(commit)
2020-05-24 23:13:21 +05:30
2022-08-13 15:12:31 +05:30
{
file_name: File.basename(path_key).force_encoding(Encoding::UTF_8),
commit: commit,
commit_path: commit_path(commit),
commit_title_html: markdown_field(commit, :full_title)
}
end
2020-05-24 23:13:21 +05:30
end
2022-08-13 15:12:31 +05:30
def fetch_logs
logs = summarize
2018-11-20 20:47:30 +05:30
2022-08-13 15:12:31 +05:30
[logs.first(limit).as_json, next_offset(logs.size)]
2018-11-20 20:47:30 +05:30
end
private
2022-08-13 15:12:31 +05:30
def next_offset(entries_count)
return if entries_count <= limit
2018-11-20 20:47:30 +05:30
2022-08-13 15:12:31 +05:30
offset + limit
2018-11-20 20:47:30 +05:30
end
def repository
project.repository
end
2021-02-11 23:33:58 +05:30
# Ensure the path is in "path/" format
def ensured_path
File.join(*[path, ""]) if path
2018-11-20 20:47:30 +05:30
end
2021-02-11 23:33:58 +05:30
def fetch_last_cached_commits_list
2022-08-13 15:12:31 +05:30
cache_key = ['projects', project.id, 'last_commits', commit.id, ensured_path, offset, limit + 1]
2021-02-11 23:33:58 +05:30
commits = Rails.cache.fetch(cache_key, expires_in: CACHE_EXPIRE_IN) do
repository
2022-08-13 15:12:31 +05:30
.list_last_commits_for_tree(commit.id, ensured_path, offset: offset, limit: limit + 1, literal_pathspec: true)
2021-04-17 20:07:23 +05:30
.transform_values! { |commit| commit_to_hash(commit) }
2021-02-11 23:33:58 +05:30
end
commits.transform_values! { |value| Commit.from_hash(value, project) }
end
2018-12-05 23:21:45 +05:30
def cache_commit(commit)
2019-07-07 11:18:12 +05:30
return unless commit.present?
2018-11-20 20:47:30 +05:30
2018-12-05 23:21:45 +05:30
resolved_commits[commit.id] ||= commit
2018-11-20 20:47:30 +05:30
end
2021-04-17 20:07:23 +05:30
def commit_to_hash(commit)
commit.to_hash.tap do |hash|
hash[:message] = hash[:message].to_s.truncate_bytes(1.kilobyte, omission: '...')
end
end
2018-11-20 20:47:30 +05:30
def commit_path(commit)
Gitlab::Routing.url_helpers.project_commit_path(project, commit)
end
2020-05-24 23:13:21 +05:30
def prerender_commit_full_titles!(commits)
# Preload commit authors as they are used in rendering
commits.each(&:lazy_author)
renderer = Banzai::ObjectRenderer.new(user: user, default_project: project)
renderer.render(commits, :full_title)
end
2018-11-20 20:47:30 +05:30
end
end
2020-05-24 23:13:21 +05:30
2021-06-08 01:23:25 +05:30
Gitlab::TreeSummary.prepend_mod_with('Gitlab::TreeSummary')