diff --git a/ee/lib/ee/gitlab/tree_summary.rb b/ee/lib/ee/gitlab/tree_summary.rb index 55edaa910f2cba3334c04e3053bd16cbe947a675..beedc2a981418fbaff032635b42c89473361015d 100644 --- a/ee/lib/ee/gitlab/tree_summary.rb +++ b/ee/lib/ee/gitlab/tree_summary.rb @@ -7,14 +7,13 @@ module TreeSummary include ::PathLocksHelper - override :summarize - def summarize - super.tap { |summary| fill_path_locks!(summary) } - end + override :fill_path_locks! private def fill_path_locks!(entries) + super + return unless project.feature_available?(:file_locks) finder = ::Gitlab::PathLocksFinder.new(project) @@ -28,10 +27,6 @@ def fill_path_locks!(entries) entry[:lock_label] = path_lock && text_label_for_lock(path_lock, path) end end - - def entry_path(entry) - File.join(*[path, entry[:file_name]].compact).force_encoding(Encoding::ASCII_8BIT) - end end end end diff --git a/lib/gitlab/tree_summary.rb b/lib/gitlab/tree_summary.rb index 08b7b1c682c16253daa0713838ecffcaa5aa6ce7..2191a6ab5d71413e88ee6b59f115f1c85fc32b91 100644 --- a/lib/gitlab/tree_summary.rb +++ b/lib/gitlab/tree_summary.rb @@ -40,7 +40,7 @@ def summarize commits_hsh = fetch_last_cached_commits_list prerender_commit_full_titles!(commits_hsh.values) - commits_hsh.map do |path_key, commit| + entries = commits_hsh.map do |path_key, commit| commit = cache_commit(commit) { @@ -50,6 +50,8 @@ def summarize commit_title_html: markdown_field(commit, :full_title) } end + + fill_path_locks!(entries) || entries end def fetch_logs @@ -60,6 +62,33 @@ def fetch_logs private + def fill_path_locks!(entries) + return unless project.lfs_enabled? + + paths = entries.map { |entry| entry_path(entry) } + lfs_locks = project.lfs_file_locks.for_paths(paths).index_by(&:path) + + entries.each do |entry| + path = lfs_entry_path(entry) + lfs_lock = lfs_locks[path] + + entry[:lock_label] = lfs_lock_label(lfs_lock) if lfs_lock + end + end + + def entry_path(entry) + File.join(*[path, entry[:file_name]].compact).force_encoding(Encoding::ASCII_8BIT) + end + + def lfs_entry_path(entry) + File.join(*[path, entry[:file_name]].compact) + end + + def lfs_lock_label(lfs_lock) + username = lfs_lock.user&.username || 'Unknown user' + "Locked by #{username}" + end + def next_offset(entries_count) return if entries_count <= limit diff --git a/spec/lib/gitlab/tree_summary_spec.rb b/spec/lib/gitlab/tree_summary_spec.rb index 6d823ed13f9d542748929dd881fef9e8b290c4fe..1f79d90fa3de0ffcc60a8df73dd26fe980572e4a 100644 --- a/spec/lib/gitlab/tree_summary_spec.rb +++ b/spec/lib/gitlab/tree_summary_spec.rb @@ -40,16 +40,72 @@ subject(:entries) { summary.summarize } - it 'returns an array of entries' do - expect(entries).to be_a(Array) - expect(entries.size).to eq(1) + context 'without LFS' do + before do + project.update!(lfs_enabled: false) + end + + it 'returns an array of entries without lock labels' do + expect(entries).to be_a(Array) + expect(entries.size).to eq(1) + + aggregate_failures do + expect(entries).to contain_exactly( + a_hash_including( + file_name: 'a.txt', + commit: have_attributes(id: commit.id) + ) + ) + expect(project.lfs_enabled?).to be false + expect(entries.first).not_to have_key(:lock_label) + expect(summary.resolved_commits.values).to match_array(entries.map { |entry| entry[:commit] }) + end + end + end + + context 'with LFS enabled' do + before do + project.update!(lfs_enabled: true) + stub_lfs_setting(enabled: true) + end - aggregate_failures do - expect(entries).to contain_exactly( - a_hash_including(file_name: 'a.txt', commit: have_attributes(id: commit.id)) - ) + context 'when file has LFS lock' do + let!(:lfs_file_lock) { create(:lfs_file_lock, project: project, path: 'a.txt') } - expect(summary.resolved_commits.values).to match_array(entries.map { |entry| entry[:commit] }) + it 'returns an array of entries with lock labels' do + expect(entries).to be_a(Array) + expect(entries.size).to eq(1) + + aggregate_failures do + expect(entries).to contain_exactly( + a_hash_including( + file_name: 'a.txt', + commit: have_attributes(id: commit.id), + lock_label: "Locked by #{lfs_file_lock.user.username}" + ) + ) + expect(project.lfs_enabled?).to be true + expect(summary.resolved_commits.values).to match_array(entries.map { |entry| entry[:commit] }) + end + end + end + + context 'when file has no LFS lock' do + it 'returns an array of entries without lock labels' do + expect(entries).to be_a(Array) + expect(entries.size).to eq(1) + + aggregate_failures do + expect(entries).to contain_exactly( + a_hash_including( + file_name: 'a.txt', + commit: have_attributes(id: commit.id) + ) + ) + + expect(entries.first).not_to have_key(:lock_label) + end + end end end