From d0861e79410dc5b1c3801e69926aa048be4ee95b Mon Sep 17 00:00:00 2001 From: Vasilii Iakliushin Date: Fri, 16 Aug 2024 14:55:00 +0200 Subject: [PATCH] Fix NoMethodError for blame Contributes to https://gitlab.com/gitlab-org/gitlab/-/issues/479511 **Problem** We fetch blob data and highlight data separately. It's possible when they are misaligned. The application raises NoMethodError in this case. **Solution** Verify if the highlighted line is available before accessing it. Changelog: fixed --- lib/gitlab/blame.rb | 2 +- spec/lib/gitlab/blame_spec.rb | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/gitlab/blame.rb b/lib/gitlab/blame.rb index ac39daf375f66d..c2ddca469b90bc 100644 --- a/lib/gitlab/blame.rb +++ b/lib/gitlab/blame.rb @@ -29,7 +29,7 @@ def groups(highlight: true) current_group = { commit: commit, lines: [], previous_path: previous_path, span: span, lineno: i + 1 } end - current_group[:lines] << (highlight ? highlighted_lines[i].html_safe : line) + current_group[:lines] << (highlight && highlighted_lines[i] ? highlighted_lines[i].html_safe : line) prev_sha = commit.sha i += 1 diff --git a/spec/lib/gitlab/blame_spec.rb b/spec/lib/gitlab/blame_spec.rb index bfe2b7d1360a5a..493fe453f850d2 100644 --- a/spec/lib/gitlab/blame_spec.rb +++ b/spec/lib/gitlab/blame_spec.rb @@ -67,6 +67,17 @@ expect(groups[1][:lines][0]).to match(/LC4.*Popen/) expect(groups[1][:lines][1]).to match(/LC5.*extend/) end + + context 'when highlighed lines are misaligned' do + let(:raw_blob) { Gitlab::Git::Blob.new(data: "Test\r\nopen3", path: path, size: 6) } + let(:blob) { Blob.new(raw_blob) } + + it 'returns the correct lines' do + expect(groups.count).to eq(2) + expect(groups[0][:lines][0]).to match(/LC1.*Test/) + expect(groups[0][:lines][1]).to match(/LC2.*open3/) + end + end end end -- GitLab