summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDouwe Maan <douwe@gitlab.com>2016-05-05 15:03:56 +0000
committerYorick Peterse <yorickpeterse@gmail.com>2016-05-05 19:35:54 +0200
commit4f9d82a9e3458209137eb66e43f8c3556322211b (patch)
tree6ac87eeed3c3328fb3eadcad7225c0443a90bad5
parentf3e41f00d54ea22bf3474850a9d1b3e8b1357ddd (diff)
downloadgitlab-ce-4f9d82a9e3458209137eb66e43f8c3556322211b.tar.gz
Merge branch 'gh-review-comments' into 'master'
Fix the line code when importing PR review comments from GitHub Pull Request Review Comments are comments on a portion of the unified diff. Closes #17205 See merge request !4010
-rw-r--r--CHANGELOG1
-rw-r--r--lib/gitlab/github_import/comment_formatter.rb21
-rw-r--r--spec/lib/gitlab/github_import/comment_formatter_spec.rb30
3 files changed, 31 insertions, 21 deletions
diff --git a/CHANGELOG b/CHANGELOG
index 18e2d3924a9..3ac67927b99 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -26,6 +26,7 @@ v 8.8.0 (unreleased)
v 8.7.3
- Emails, Gitlab::Email::Message, Gitlab::Diff, and Premailer::Adapter::Nokogiri are now instrumented
- Merge request widget displays TeamCity build state and code coverage correctly again.
+ - Fix the line code when importing PR review comments from GitHub. !4010
v 8.7.2
- The "New Branch" button is now loaded asynchronously
diff --git a/lib/gitlab/github_import/comment_formatter.rb b/lib/gitlab/github_import/comment_formatter.rb
index 7d58e53991a..7d679eaec6a 100644
--- a/lib/gitlab/github_import/comment_formatter.rb
+++ b/lib/gitlab/github_import/comment_formatter.rb
@@ -28,13 +28,26 @@ module Gitlab
end
def line_code
- if on_diff?
- Gitlab::Diff::LineCode.generate(raw_data.path, raw_data.position, 0)
- end
+ return unless on_diff?
+
+ parsed_lines = Gitlab::Diff::Parser.new.parse(diff_hunk.lines)
+ generate_line_code(parsed_lines.to_a.last)
+ end
+
+ def generate_line_code(line)
+ Gitlab::Diff::LineCode.generate(file_path, line.new_pos, line.old_pos)
end
def on_diff?
- raw_data.path && raw_data.position
+ diff_hunk.present?
+ end
+
+ def diff_hunk
+ raw_data.diff_hunk
+ end
+
+ def file_path
+ raw_data.path
end
def note
diff --git a/spec/lib/gitlab/github_import/comment_formatter_spec.rb b/spec/lib/gitlab/github_import/comment_formatter_spec.rb
index a324a82e69f..55e86d4ceac 100644
--- a/spec/lib/gitlab/github_import/comment_formatter_spec.rb
+++ b/spec/lib/gitlab/github_import/comment_formatter_spec.rb
@@ -2,23 +2,25 @@ require 'spec_helper'
describe Gitlab::GithubImport::CommentFormatter, lib: true do
let(:project) { create(:project) }
- let(:octocat) { OpenStruct.new(id: 123456, login: 'octocat') }
+ let(:octocat) { double(id: 123456, login: 'octocat') }
let(:created_at) { DateTime.strptime('2013-04-10T20:09:31Z') }
let(:updated_at) { DateTime.strptime('2014-03-03T18:58:10Z') }
- let(:base_data) do
+ let(:base) do
{
body: "I'm having a problem with this.",
user: octocat,
+ commit_id: nil,
+ diff_hunk: nil,
created_at: created_at,
updated_at: updated_at
}
end
- subject(:comment) { described_class.new(project, raw_data)}
+ subject(:comment) { described_class.new(project, raw)}
describe '#attributes' do
context 'when do not reference a portion of the diff' do
- let(:raw_data) { OpenStruct.new(base_data) }
+ let(:raw) { double(base) }
it 'returns formatted attributes' do
expected = {
@@ -36,24 +38,23 @@ describe Gitlab::GithubImport::CommentFormatter, lib: true do
end
context 'when on a portion of the diff' do
- let(:diff_data) do
+ let(:diff) do
{
body: 'Great stuff',
commit_id: '6dcb09b5b57875f334f61aebed695e2e4193db5e',
- diff_hunk: '@@ -16,33 +16,40 @@ public class Connection : IConnection...',
- path: 'file1.txt',
- position: 1
+ diff_hunk: "@@ -1,5 +1,9 @@\n class User\n def name\n- 'John Doe'\n+ 'Jane Doe'",
+ path: 'file1.txt'
}
end
- let(:raw_data) { OpenStruct.new(base_data.merge(diff_data)) }
+ let(:raw) { double(base.merge(diff)) }
it 'returns formatted attributes' do
expected = {
project: project,
note: "*Created by: octocat*\n\nGreat stuff",
commit_id: '6dcb09b5b57875f334f61aebed695e2e4193db5e',
- line_code: 'ce1be0ff4065a6e9415095c95f25f47a633cef2b_0_1',
+ line_code: 'ce1be0ff4065a6e9415095c95f25f47a633cef2b_4_3',
author_id: project.creator_id,
created_at: created_at,
updated_at: updated_at
@@ -64,15 +65,10 @@ describe Gitlab::GithubImport::CommentFormatter, lib: true do
end
context 'when author is a GitLab user' do
- let(:raw_data) { OpenStruct.new(base_data.merge(user: octocat)) }
+ let(:raw) { double(base.merge(user: octocat)) }
- it 'returns project#creator_id as author_id when is not a GitLab user' do
- expect(comment.attributes.fetch(:author_id)).to eq project.creator_id
- end
-
- it 'returns GitLab user id as author_id when is a GitLab user' do
+ it 'returns GitLab user id as author_id' do
gl_user = create(:omniauth_user, extern_uid: octocat.id, provider: 'github')
-
expect(comment.attributes.fetch(:author_id)).to eq gl_user.id
end
end