Commit df808882 authored by Jarka Košanová's avatar Jarka Košanová

Use jira import owner as the issue author

- the user who runs the import is the issues author
parent a7298fff
---
title: Use Jira import owner as the issue author when importing issues from Jira
merge_request: 30504
author:
type: fixed
......@@ -3,12 +3,13 @@
module Gitlab
module JiraImport
class BaseImporter
attr_reader :project, :client, :formatter, :jira_project_key
attr_reader :project, :client, :formatter, :jira_project_key, :running_import
def initialize(project)
project.validate_jira_import_settings!
@jira_project_key = project.latest_jira_import&.jira_project_key
@running_import = project.latest_jira_import
@jira_project_key = running_import&.jira_project_key
raise Projects::ImportService::Error, _('Unable to find Jira project to import data from.') unless @jira_project_key
......
......@@ -3,11 +3,12 @@
module Gitlab
module JiraImport
class IssueSerializer
attr_reader :jira_issue, :project, :params, :formatter
attr_reader :jira_issue, :project, :import_owner_id, :params, :formatter
def initialize(project, jira_issue, params = {})
def initialize(project, jira_issue, import_owner_id, params = {})
@jira_issue = jira_issue
@project = project
@import_owner_id = import_owner_id
@params = params
@formatter = Gitlab::ImportFormatter.new
end
......@@ -71,7 +72,7 @@ module Gitlab
end
def reporter
map_user_id(jira_issue&.reporter&.emailAddress) || project.creator_id
map_user_id(jira_issue&.reporter&.emailAddress) || import_owner_id
end
def assignees
......
......@@ -57,7 +57,7 @@ module Gitlab
# For such cases we exit early if issue was already imported.
next if already_imported?(jira_issue.id)
issue_attrs = IssueSerializer.new(project, jira_issue, { iid: next_iid }).execute
issue_attrs = IssueSerializer.new(project, jira_issue, running_import.user_id, { iid: next_iid }).execute
Gitlab::JiraImport::ImportIssueWorker.perform_async(project.id, jira_issue.id, issue_attrs, job_waiter.key)
job_waiter.jobs_remaining += 1
......
......@@ -9,6 +9,7 @@ describe Gitlab::JiraImport::IssueSerializer do
let_it_be(:project_label) { create(:label, project: project, title: 'bug') }
let_it_be(:other_project_label) { create(:label, project: project, title: 'feature') }
let_it_be(:group_label) { create(:group_label, group: group, title: 'dev') }
let_it_be(:current_user) { create(:user) }
let(:iid) { 5 }
let(:key) { 'PROJECT-5' }
......@@ -51,7 +52,7 @@ describe Gitlab::JiraImport::IssueSerializer do
let(:params) { { iid: iid } }
subject { described_class.new(project, jira_issue, params).execute }
subject { described_class.new(project, jira_issue, current_user.id, params).execute }
let(:expected_description) do
<<~MD
......@@ -76,7 +77,7 @@ describe Gitlab::JiraImport::IssueSerializer do
state_id: 1,
updated_at: updated_at,
created_at: created_at,
author_id: project.creator_id,
author_id: current_user.id,
assignee_ids: nil,
label_ids: [project_label.id, group_label.id] + Label.reorder(id: :asc).last(2).pluck(:id)
)
......@@ -122,13 +123,13 @@ describe Gitlab::JiraImport::IssueSerializer do
let!(:user) { create(:user, email: 'reporter@example.com') }
it 'defaults the issue author to project creator' do
expect(subject[:author_id]).to eq(project.creator.id)
expect(subject[:author_id]).to eq(current_user.id)
end
end
context 'when reporter does not map to a GitLab user' do
it 'defaults the issue author to project creator' do
expect(subject[:author_id]).to eq(project.creator.id)
expect(subject[:author_id]).to eq(current_user.id)
end
end
......@@ -136,7 +137,7 @@ describe Gitlab::JiraImport::IssueSerializer do
let(:reporter) { nil }
it 'defaults the issue author to project creator' do
expect(subject[:author_id]).to eq(project.creator.id)
expect(subject[:author_id]).to eq(current_user.id)
end
end
......@@ -144,7 +145,7 @@ describe Gitlab::JiraImport::IssueSerializer do
let(:reporter) { double(name: 'Reporter', emailAddress: nil) }
it 'defaults the issue author to project creator' do
expect(subject[:author_id]).to eq(project.creator.id)
expect(subject[:author_id]).to eq(current_user.id)
end
end
end
......
......@@ -6,8 +6,9 @@ describe Gitlab::JiraImport::IssuesImporter do
include JiraServiceHelper
let_it_be(:user) { create(:user) }
let_it_be(:current_user) { create(:user) }
let_it_be(:project) { create(:project) }
let_it_be(:jira_import) { create(:jira_import_state, project: project) }
let_it_be(:jira_import) { create(:jira_import_state, project: project, user: current_user) }
let_it_be(:jira_service) { create(:jira_service, project: project) }
subject { described_class.new(project) }
......@@ -39,8 +40,16 @@ describe Gitlab::JiraImport::IssuesImporter do
context 'with results returned' do
JiraIssue = Struct.new(:id)
let_it_be(:jira_issue1) { JiraIssue.new(1) }
let_it_be(:jira_issue2) { JiraIssue.new(2) }
let_it_be(:jira_issues) { [JiraIssue.new(1), JiraIssue.new(2)] }
def mock_issue_serializer(count)
serializer = instance_double(Gitlab::JiraImport::IssueSerializer, execute: { key: 'data' })
count.times do |i|
expect(Gitlab::JiraImport::IssueSerializer).to receive(:new)
.with(project, jira_issues[i], current_user.id, { iid: i + 1 }).and_return(serializer)
end
end
context 'when single page of results is returned' do
before do
......@@ -48,13 +57,11 @@ describe Gitlab::JiraImport::IssuesImporter do
end
it 'schedules 2 import jobs' do
expect(subject).to receive(:fetch_issues).and_return([jira_issue1, jira_issue2])
expect(subject).to receive(:fetch_issues).with(0).and_return([jira_issues[0], jira_issues[1]])
expect(Gitlab::JiraImport::ImportIssueWorker).to receive(:perform_async).twice
expect(Gitlab::Cache::Import::Caching).to receive(:set_add).twice.and_call_original
expect(Gitlab::Cache::Import::Caching).to receive(:set_includes?).twice.and_call_original
allow_next_instance_of(Gitlab::JiraImport::IssueSerializer) do |instance|
allow(instance).to receive(:execute).and_return({ key: 'data' })
end
mock_issue_serializer(2)
job_waiter = subject.execute
......@@ -69,13 +76,11 @@ describe Gitlab::JiraImport::IssuesImporter do
end
it 'schedules 3 import jobs' do
expect(subject).to receive(:fetch_issues).with(0).and_return([jira_issue1, jira_issue2])
expect(subject).to receive(:fetch_issues).with(0).and_return([jira_issues[0], jira_issues[1]])
expect(Gitlab::JiraImport::ImportIssueWorker).to receive(:perform_async).twice.times
expect(Gitlab::Cache::Import::Caching).to receive(:set_add).twice.times.and_call_original
expect(Gitlab::Cache::Import::Caching).to receive(:set_includes?).twice.times.and_call_original
allow_next_instance_of(Gitlab::JiraImport::IssueSerializer) do |instance|
allow(instance).to receive(:execute).and_return({ key: 'data' })
end
mock_issue_serializer(2)
job_waiter = subject.execute
......@@ -90,13 +95,11 @@ describe Gitlab::JiraImport::IssuesImporter do
end
it 'schedules 2 import jobs' do
expect(subject).to receive(:fetch_issues).with(0).and_return([jira_issue1, jira_issue1])
expect(subject).to receive(:fetch_issues).with(0).and_return([jira_issues[0], jira_issues[0]])
expect(Gitlab::JiraImport::ImportIssueWorker).to receive(:perform_async).once
expect(Gitlab::Cache::Import::Caching).to receive(:set_add).once.and_call_original
expect(Gitlab::Cache::Import::Caching).to receive(:set_includes?).twice.times.and_call_original
allow_next_instance_of(Gitlab::JiraImport::IssueSerializer) do |instance|
allow(instance).to receive(:execute).and_return({ key: 'data' })
end
mock_issue_serializer(1)
job_waiter = subject.execute
......
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