Commit 40348e8f authored by Steve Abrams's avatar Steve Abrams

Merge branch 'kassio/github-importer-null-review-author-bug' into 'master'

GithubImport: Fix Review importer when the author doesn't exist anymore

See merge request gitlab-org/gitlab!61257
parents 4fc2dba1 85b133f8
---
title: 'GithubImport: Fix Review importer when the author does not exist anymore'
merge_request: 61257
author:
type: fixed
...@@ -36,12 +36,12 @@ module Gitlab ...@@ -36,12 +36,12 @@ module Gitlab
def add_complementary_review_note!(author_id) def add_complementary_review_note!(author_id)
return if review.note.empty? && !review.approval? return if review.note.empty? && !review.approval?
note = "*Created by %{login}*\n\n%{note}" % { note_body = MarkdownText.format(
note: review_note_content, review_note_content,
login: review.author.login review.author
} )
add_note!(author_id, note) add_note!(author_id, note_body)
end end
def review_note_content def review_note_content
......
...@@ -19,10 +19,10 @@ module Gitlab ...@@ -19,10 +19,10 @@ module Gitlab
end end
def to_s def to_s
if exists if author&.login.present? && !exists
text
else
"*Created by: #{author.login}*\n\n#{text}" "*Created by: #{author.login}*\n\n#{text}"
else
text
end end
end end
end end
......
...@@ -63,7 +63,7 @@ module Gitlab ...@@ -63,7 +63,7 @@ module Gitlab
# #
# user - An instance of `Gitlab::GithubImport::Representation::User`. # user - An instance of `Gitlab::GithubImport::Representation::User`.
def user_id_for(user) def user_id_for(user)
find(user.id, user.login) find(user.id, user.login) if user.present?
end end
# Returns the GitLab ID for the given GitHub ID or username. # Returns the GitLab ID for the given GitHub ID or username.
......
...@@ -130,7 +130,7 @@ RSpec.describe Gitlab::GithubImport::Importer::PullRequestReviewImporter, :clean ...@@ -130,7 +130,7 @@ RSpec.describe Gitlab::GithubImport::Importer::PullRequestReviewImporter, :clean
.to change(Note, :count).by(1) .to change(Note, :count).by(1)
last_note = merge_request.notes.last last_note = merge_request.notes.last
expect(last_note.note).to eq("*Created by author*\n\n**Review:** Approved") expect(last_note.note).to eq("*Created by: author*\n\n**Review:** Approved")
expect(last_note.author).to eq(project.creator) expect(last_note.author).to eq(project.creator)
expect(last_note.created_at).to eq(submitted_at) expect(last_note.created_at).to eq(submitted_at)
end end
...@@ -153,6 +153,20 @@ RSpec.describe Gitlab::GithubImport::Importer::PullRequestReviewImporter, :clean ...@@ -153,6 +153,20 @@ RSpec.describe Gitlab::GithubImport::Importer::PullRequestReviewImporter, :clean
end end
end end
context 'when original author was deleted in github' do
let(:review) { create_review(type: 'APPROVED', note: '', author: nil) }
it 'creates a note for the review without the author information' do
expect { subject.execute }
.to change(Note, :count).by(1)
last_note = merge_request.notes.last
expect(last_note.note).to eq('**Review:** Approved')
expect(last_note.author).to eq(project.creator)
expect(last_note.created_at).to eq(submitted_at)
end
end
context 'when the review has a note text' do context 'when the review has a note text' do
context 'when the review is "APPROVED"' do context 'when the review is "APPROVED"' do
let(:review) { create_review(type: 'APPROVED') } let(:review) { create_review(type: 'APPROVED') }
...@@ -163,7 +177,7 @@ RSpec.describe Gitlab::GithubImport::Importer::PullRequestReviewImporter, :clean ...@@ -163,7 +177,7 @@ RSpec.describe Gitlab::GithubImport::Importer::PullRequestReviewImporter, :clean
last_note = merge_request.notes.last last_note = merge_request.notes.last
expect(last_note.note).to eq("*Created by author*\n\n**Review:** Approved\n\nnote") expect(last_note.note).to eq("*Created by: author*\n\n**Review:** Approved\n\nnote")
expect(last_note.author).to eq(project.creator) expect(last_note.author).to eq(project.creator)
expect(last_note.created_at).to eq(submitted_at) expect(last_note.created_at).to eq(submitted_at)
end end
...@@ -178,7 +192,7 @@ RSpec.describe Gitlab::GithubImport::Importer::PullRequestReviewImporter, :clean ...@@ -178,7 +192,7 @@ RSpec.describe Gitlab::GithubImport::Importer::PullRequestReviewImporter, :clean
last_note = merge_request.notes.last last_note = merge_request.notes.last
expect(last_note.note).to eq("*Created by author*\n\n**Review:** Commented\n\nnote") expect(last_note.note).to eq("*Created by: author*\n\n**Review:** Commented\n\nnote")
expect(last_note.author).to eq(project.creator) expect(last_note.author).to eq(project.creator)
expect(last_note.created_at).to eq(submitted_at) expect(last_note.created_at).to eq(submitted_at)
end end
...@@ -193,7 +207,7 @@ RSpec.describe Gitlab::GithubImport::Importer::PullRequestReviewImporter, :clean ...@@ -193,7 +207,7 @@ RSpec.describe Gitlab::GithubImport::Importer::PullRequestReviewImporter, :clean
last_note = merge_request.notes.last last_note = merge_request.notes.last
expect(last_note.note).to eq("*Created by author*\n\n**Review:** Changes requested\n\nnote") expect(last_note.note).to eq("*Created by: author*\n\n**Review:** Changes requested\n\nnote")
expect(last_note.author).to eq(project.creator) expect(last_note.author).to eq(project.creator)
expect(last_note.created_at).to eq(submitted_at) expect(last_note.created_at).to eq(submitted_at)
end end
...@@ -201,13 +215,13 @@ RSpec.describe Gitlab::GithubImport::Importer::PullRequestReviewImporter, :clean ...@@ -201,13 +215,13 @@ RSpec.describe Gitlab::GithubImport::Importer::PullRequestReviewImporter, :clean
end end
end end
def create_review(type:, note: 'note') def create_review(type:, note: 'note', author: { id: 999, login: 'author' })
Gitlab::GithubImport::Representation::PullRequestReview.from_json_hash( Gitlab::GithubImport::Representation::PullRequestReview.from_json_hash(
merge_request_id: merge_request.id, merge_request_id: merge_request.id,
review_type: type, review_type: type,
note: note, note: note,
submitted_at: submitted_at.to_s, submitted_at: submitted_at.to_s,
author: { id: 999, login: 'author' } author: author
) )
end end
end end
...@@ -61,6 +61,10 @@ RSpec.describe Gitlab::GithubImport::UserFinder, :clean_gitlab_redis_cache do ...@@ -61,6 +61,10 @@ RSpec.describe Gitlab::GithubImport::UserFinder, :clean_gitlab_redis_cache do
expect(finder).to receive(:find).with(user.id, user.login).and_return(42) expect(finder).to receive(:find).with(user.id, user.login).and_return(42)
expect(finder.user_id_for(user)).to eq(42) expect(finder.user_id_for(user)).to eq(42)
end end
it 'does not fail with empty input' do
expect(finder.user_id_for(nil)).to eq(nil)
end
end end
describe '#find' do describe '#find' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment