Commit 170332c3 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Merge branch '37694-pullrequestimporter-should-not-use-deprecated-assignee_id-column' into 'master'

Remove deprecated assignee_id from PullRequestImporter

See merge request gitlab-org/gitlab!22144
parents 47a85761 60efea5d
...@@ -182,7 +182,6 @@ module Gitlab ...@@ -182,7 +182,6 @@ module Gitlab
target_branch_sha: target_branch_sha, target_branch_sha: target_branch_sha,
state: pull_request.state, state: pull_request.state,
author_id: gitlab_user_id(project, pull_request.author), author_id: gitlab_user_id(project, pull_request.author),
assignee_id: nil,
created_at: pull_request.created_at, created_at: pull_request.created_at,
updated_at: pull_request.updated_at updated_at: pull_request.updated_at
) )
......
...@@ -211,7 +211,6 @@ module Gitlab ...@@ -211,7 +211,6 @@ module Gitlab
target_branch_sha: pull_request.target_branch_sha, target_branch_sha: pull_request.target_branch_sha,
state_id: MergeRequest.available_states[pull_request.state], state_id: MergeRequest.available_states[pull_request.state],
author_id: author_id, author_id: author_id,
assignee_id: nil,
created_at: pull_request.created_at, created_at: pull_request.created_at,
updated_at: pull_request.updated_at updated_at: pull_request.updated_at
} }
......
...@@ -27,6 +27,7 @@ module Gitlab ...@@ -27,6 +27,7 @@ module Gitlab
mr, already_exists = create_merge_request mr, already_exists = create_merge_request
if mr if mr
set_merge_request_assignees(mr)
insert_git_data(mr, already_exists) insert_git_data(mr, already_exists)
issuable_finder.cache_database_id(mr.id) issuable_finder.cache_database_id(mr.id)
end end
...@@ -57,7 +58,6 @@ module Gitlab ...@@ -57,7 +58,6 @@ module Gitlab
state_id: ::MergeRequest.available_states[pull_request.state], state_id: ::MergeRequest.available_states[pull_request.state],
milestone_id: milestone_finder.id_for(pull_request), milestone_id: milestone_finder.id_for(pull_request),
author_id: author_id, author_id: author_id,
assignee_id: user_finder.assignee_id_for(pull_request),
created_at: pull_request.created_at, created_at: pull_request.created_at,
updated_at: pull_request.updated_at updated_at: pull_request.updated_at
} }
...@@ -65,6 +65,10 @@ module Gitlab ...@@ -65,6 +65,10 @@ module Gitlab
create_merge_request_without_hooks(project, attributes, pull_request.iid) create_merge_request_without_hooks(project, attributes, pull_request.iid)
end end
def set_merge_request_assignees(merge_request)
merge_request.assignee_ids = [user_finder.assignee_id_for(pull_request)]
end
def insert_git_data(merge_request, already_exists) def insert_git_data(merge_request, already_exists)
insert_or_replace_git_data(merge_request, pull_request.source_branch_sha, pull_request.target_branch_sha, already_exists) insert_or_replace_git_data(merge_request, pull_request.source_branch_sha, pull_request.target_branch_sha, already_exists)
# We need to create the branch after the merge request is # We need to create the branch after the merge request is
......
...@@ -49,6 +49,10 @@ describe Gitlab::GithubImport::Importer::PullRequestImporter, :clean_gitlab_redi ...@@ -49,6 +49,10 @@ describe Gitlab::GithubImport::Importer::PullRequestImporter, :clean_gitlab_redi
.to receive(:create_merge_request) .to receive(:create_merge_request)
.and_return([mr, false]) .and_return([mr, false])
expect(importer)
.to receive(:set_merge_request_assignees)
.with(mr)
expect(importer) expect(importer)
.to receive(:insert_git_data) .to receive(:insert_git_data)
.with(mr, false) .with(mr, false)
...@@ -75,11 +79,6 @@ describe Gitlab::GithubImport::Importer::PullRequestImporter, :clean_gitlab_redi ...@@ -75,11 +79,6 @@ describe Gitlab::GithubImport::Importer::PullRequestImporter, :clean_gitlab_redi
.to receive(:author_id_for) .to receive(:author_id_for)
.with(pull_request) .with(pull_request)
.and_return([user.id, true]) .and_return([user.id, true])
allow(importer.user_finder)
.to receive(:assignee_id_for)
.with(pull_request)
.and_return(user.id)
end end
it 'imports the pull request with the pull request author as the merge request author' do it 'imports the pull request with the pull request author as the merge request author' do
...@@ -97,7 +96,6 @@ describe Gitlab::GithubImport::Importer::PullRequestImporter, :clean_gitlab_redi ...@@ -97,7 +96,6 @@ describe Gitlab::GithubImport::Importer::PullRequestImporter, :clean_gitlab_redi
state_id: 3, state_id: 3,
milestone_id: milestone.id, milestone_id: milestone.id,
author_id: user.id, author_id: user.id,
assignee_id: user.id,
created_at: created_at, created_at: created_at,
updated_at: updated_at updated_at: updated_at
}, },
...@@ -114,35 +112,30 @@ describe Gitlab::GithubImport::Importer::PullRequestImporter, :clean_gitlab_redi ...@@ -114,35 +112,30 @@ describe Gitlab::GithubImport::Importer::PullRequestImporter, :clean_gitlab_redi
expect(mr).to be_instance_of(MergeRequest) expect(mr).to be_instance_of(MergeRequest)
expect(exists).to eq(false) expect(exists).to eq(false)
end end
end
context 'when the author could not be found' do
it 'imports the pull request with the project creator as the merge request author' do
allow(importer.user_finder)
.to receive(:author_id_for)
.with(pull_request)
.and_return([project.creator_id, false])
allow(importer.user_finder) context 'when the source and target branch are identical' do
.to receive(:assignee_id_for) before do
.with(pull_request) allow(pull_request).to receive_messages(
.and_return(user.id) source_repository_id: pull_request.target_repository_id,
source_branch: 'master'
)
end
it 'uses a generated source branch name for the merge request' do
expect(importer) expect(importer)
.to receive(:insert_and_return_id) .to receive(:insert_and_return_id)
.with( .with(
{ {
iid: 42, iid: 42,
title: 'My Pull Request', title: 'My Pull Request',
description: "*Created by: alice*\n\nThis is my pull request", description: 'This is my pull request',
source_project_id: project.id, source_project_id: project.id,
target_project_id: project.id, target_project_id: project.id,
source_branch: 'github/fork/alice/feature', source_branch: 'master-42',
target_branch: 'master', target_branch: 'master',
state_id: 3, state_id: 3,
milestone_id: milestone.id, milestone_id: milestone.id,
author_id: project.creator_id, author_id: user.id,
assignee_id: user.id,
created_at: created_at, created_at: created_at,
updated_at: updated_at updated_at: updated_at
}, },
...@@ -154,41 +147,51 @@ describe Gitlab::GithubImport::Importer::PullRequestImporter, :clean_gitlab_redi ...@@ -154,41 +147,51 @@ describe Gitlab::GithubImport::Importer::PullRequestImporter, :clean_gitlab_redi
end end
end end
context 'when the source and target branch are identical' do context 'when the import fails due to a foreign key error' do
it 'uses a generated source branch name for the merge request' do it 'does not raise any errors' do
allow(importer.user_finder) expect(importer)
.to receive(:author_id_for) .to receive(:insert_and_return_id)
.with(pull_request) .and_raise(ActiveRecord::InvalidForeignKey, 'invalid foreign key')
.and_return([user.id, true])
allow(importer.user_finder) expect { importer.create_merge_request }.not_to raise_error
.to receive(:assignee_id_for) end
.with(pull_request) end
.and_return(user.id)
allow(pull_request) context 'when the merge request already exists' do
.to receive(:source_repository_id) it 'returns the existing merge request' do
.and_return(pull_request.target_repository_id) mr1, exists1 = importer.create_merge_request
mr2, exists2 = importer.create_merge_request
expect(mr2).to eq(mr1)
expect(exists1).to eq(false)
expect(exists2).to eq(true)
end
end
end
allow(pull_request) context 'when the author could not be found' do
.to receive(:source_branch) before do
.and_return('master') allow(importer.user_finder)
.to receive(:author_id_for)
.with(pull_request)
.and_return([project.creator_id, false])
end
it 'imports the pull request with the project creator as the merge request author' do
expect(importer) expect(importer)
.to receive(:insert_and_return_id) .to receive(:insert_and_return_id)
.with( .with(
{ {
iid: 42, iid: 42,
title: 'My Pull Request', title: 'My Pull Request',
description: 'This is my pull request', description: "*Created by: alice*\n\nThis is my pull request",
source_project_id: project.id, source_project_id: project.id,
target_project_id: project.id, target_project_id: project.id,
source_branch: 'master-42', source_branch: 'github/fork/alice/feature',
target_branch: 'master', target_branch: 'master',
state_id: 3, state_id: 3,
milestone_id: milestone.id, milestone_id: milestone.id,
author_id: user.id, author_id: project.creator_id,
assignee_id: user.id,
created_at: created_at, created_at: created_at,
updated_at: updated_at updated_at: updated_at
}, },
...@@ -199,47 +202,33 @@ describe Gitlab::GithubImport::Importer::PullRequestImporter, :clean_gitlab_redi ...@@ -199,47 +202,33 @@ describe Gitlab::GithubImport::Importer::PullRequestImporter, :clean_gitlab_redi
importer.create_merge_request importer.create_merge_request
end end
end end
end
context 'when the import fails due to a foreign key error' do describe '#set_merge_request_assignees' do
it 'does not raise any errors' do let_it_be(:merge_request) { create(:merge_request) }
allow(importer.user_finder)
.to receive(:author_id_for)
.with(pull_request)
.and_return([user.id, true])
before do
allow(importer.user_finder) allow(importer.user_finder)
.to receive(:assignee_id_for) .to receive(:assignee_id_for)
.with(pull_request) .with(pull_request)
.and_return(user.id) .and_return(user_id)
expect(importer) importer.set_merge_request_assignees(merge_request)
.to receive(:insert_and_return_id)
.and_raise(ActiveRecord::InvalidForeignKey, 'invalid foreign key')
expect { importer.create_merge_request }.not_to raise_error
end
end end
context 'when the merge request already exists' do context 'when pull request has an assignee' do
before do let(:user_id) { user.id }
allow(importer.user_finder)
.to receive(:author_id_for)
.with(pull_request)
.and_return([user.id, true])
allow(importer.user_finder) it 'sets merge request assignees' do
.to receive(:assignee_id_for) expect(merge_request.assignee_ids).to eq [user.id]
.with(pull_request) end
.and_return(user.id)
end end
it 'returns the existing merge request' do context 'when pull request does not have any assignees' do
mr1, exists1 = importer.create_merge_request let(:user_id) { nil }
mr2, exists2 = importer.create_merge_request
expect(mr2).to eq(mr1) it 'does not set merge request assignees' do
expect(exists1).to eq(false) expect(merge_request.assignee_ids).to eq []
expect(exists2).to eq(true)
end end
end end
end end
...@@ -255,11 +244,6 @@ describe Gitlab::GithubImport::Importer::PullRequestImporter, :clean_gitlab_redi ...@@ -255,11 +244,6 @@ describe Gitlab::GithubImport::Importer::PullRequestImporter, :clean_gitlab_redi
.to receive(:author_id_for) .to receive(:author_id_for)
.with(pull_request) .with(pull_request)
.and_return([user.id, true]) .and_return([user.id, true])
allow(importer.user_finder)
.to receive(:assignee_id_for)
.with(pull_request)
.and_return(user.id)
end end
it 'does not create the source branch if merge request is merged' do it 'does not create the source branch if merge request is merged' do
......
...@@ -19,8 +19,7 @@ describe Gitlab::Import::MergeRequestHelpers, type: :helper do ...@@ -19,8 +19,7 @@ describe Gitlab::Import::MergeRequestHelpers, type: :helper do
source_branch: 'master-42', source_branch: 'master-42',
target_branch: 'master', target_branch: 'master',
state_id: 3, state_id: 3,
author_id: user.id, author_id: user.id
assignee_id: user.id
} }
end end
......
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