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

Add users mapping to start Jira import mutation

- add JiraUsersMappingInputType
- use that type as an argument in start import mutation
-store mapping in Redis
parent d594d490
......@@ -21,12 +21,17 @@ module Mutations
argument :jira_project_name, GraphQL::STRING_TYPE,
required: false,
description: 'Project name of the importer Jira project'
argument :users_mapping,
[Types::JiraUsersMappingInputType],
required: false,
description: 'The mapping of Jira to GitLab users'
def resolve(project_path:, jira_project_key:)
def resolve(project_path:, jira_project_key:, users_mapping:)
project = authorized_find!(full_path: project_path)
mapping = users_mapping.to_ary.map { |map| map.to_hash }
service_response = ::JiraImport::StartImportService
.new(context[:current_user], project, jira_project_key)
.new(context[:current_user], project, jira_project_key, mapping)
.execute
jira_import = service_response.success? ? service_response.payload[:import_data] : nil
......
# frozen_string_literal: true
module Types
# rubocop: disable Graphql/AuthorizeTypes
class JiraUsersMappingInputType < BaseInputObject
graphql_name 'JiraUsersMappingInputType'
argument :jira_account_id,
GraphQL::STRING_TYPE,
required: true,
description: 'Jira account id of the user'
argument :gitlab_id,
GraphQL::INT_TYPE,
required: false,
description: 'Id of the GitLab user'
end
# rubocop: enable Graphql/AuthorizeTypes
end
......@@ -2,23 +2,39 @@
module JiraImport
class StartImportService
attr_reader :user, :project, :jira_project_key
attr_reader :user, :project, :jira_project_key, :users_mapping
def initialize(user, project, jira_project_key)
def initialize(user, project, jira_project_key, users_mapping)
@user = user
@project = project
@jira_project_key = jira_project_key
@users_mapping = users_mapping
end
def execute
validation_response = validate
return validation_response if validation_response&.error?
store_users_mapping
create_and_schedule_import
end
private
def store_users_mapping
return if users_mapping.blank?
mapping = users_mapping.map do |map|
next if !map[:jira_account_id] || !map[:gitlab_id]
[map[:jira_account_id], map[:gitlab_id]]
end.compact.to_h
return if mapping.blank?
Gitlab::JiraImport.cache_users_mapping(project.id, mapping)
end
def create_and_schedule_import
jira_import = build_jira_import
project.import_type = 'jira'
......
---
title: Add Jira users mapping to start Jira import mutation
merge_request: 34609
author:
type: added
......@@ -6368,6 +6368,11 @@ input JiraImportStartInput {
The project to import the Jira project into
"""
projectPath: ID!
"""
The mapping of Jira to GitLab users
"""
usersMapping: [JiraUsersMappingInputType!]
}
"""
......@@ -6546,6 +6551,18 @@ type JiraUser {
jiraEmail: String
}
input JiraUsersMappingInputType {
"""
Id of the GitLab user
"""
gitlabId: Int
"""
Jira account id of the user
"""
jiraAccountId: String!
}
type Label {
"""
Background color of the label
......
......@@ -17605,6 +17605,24 @@
},
"defaultValue": null
},
{
"name": "usersMapping",
"description": "The mapping of Jira to GitLab users",
"type": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "INPUT_OBJECT",
"name": "JiraUsersMappingInputType",
"ofType": null
}
}
},
"defaultValue": null
},
{
"name": "clientMutationId",
"description": "A unique identifier for the client performing the mutation.",
......@@ -18167,6 +18185,41 @@
"enumValues": null,
"possibleTypes": null
},
{
"kind": "INPUT_OBJECT",
"name": "JiraUsersMappingInputType",
"description": null,
"fields": null,
"inputFields": [
{
"name": "jiraAccountId",
"description": "Jira account id of the user",
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
},
"defaultValue": null
},
{
"name": "gitlabId",
"description": "Id of the GitLab user",
"type": {
"kind": "SCALAR",
"name": "Int",
"ofType": null
},
"defaultValue": null
}
],
"interfaces": null,
"enumValues": null,
"possibleTypes": null
},
{
"kind": "OBJECT",
"name": "Label",
......@@ -14,7 +14,8 @@ RSpec.describe 'Starting a Jira Import' do
let(:mutation) do
variables = {
jira_project_key: jira_project_key,
project_path: project_path
project_path: project_path,
users_mapping: [{ jiraAccountId: 'abc', gitlabId: 5 }]
}
graphql_mutation(:jira_import_start, variables)
......
......@@ -8,8 +8,15 @@ RSpec.describe JiraImport::StartImportService do
let_it_be(:user) { create(:user) }
let_it_be(:project, reload: true) { create(:project) }
let(:key) { 'KEY' }
let(:mapping) do
[
{ jira_account_id: 'abc', gitlab_id: 12 },
{ jira_account_id: 'def', gitlab_id: nil },
{ jira_account_id: nil, gitlab_id: 1 }
]
end
subject { described_class.new(user, project, key).execute }
subject { described_class.new(user, project, key, mapping).execute }
context 'when an error is returned from the project validation' do
before do
......@@ -37,7 +44,7 @@ RSpec.describe JiraImport::StartImportService do
context 'when correct data provided' do
let(:fake_key) { 'some-key' }
subject { described_class.new(user, project, fake_key).execute }
subject { described_class.new(user, project, fake_key, mapping).execute }
context 'when import is already running' do
let_it_be(:jira_import_state) { create(:jira_import_state, :started, project: project) }
......@@ -62,35 +69,68 @@ RSpec.describe JiraImport::StartImportService do
end
context 'when everything is ok' do
it 'returns success response' do
expect(subject).to be_a(ServiceResponse)
expect(subject).to be_success
end
context 'with complete mapping' do
before do
expect(Gitlab::JiraImport).to receive(:cache_users_mapping).with(project.id, { 'abc' => 12 })
end
it 'schedules Jira import' do
subject
it 'returns success response' do
expect(subject).to be_a(ServiceResponse)
expect(subject).to be_success
end
expect(project.latest_jira_import).to be_scheduled
end
it 'schedules Jira import' do
subject
it 'creates Jira import data', :aggregate_failures do
jira_import = subject.payload[:import_data]
expect(project.latest_jira_import).to be_scheduled
end
it 'creates Jira import data', :aggregate_failures do
jira_import = subject.payload[:import_data]
expect(jira_import.jira_project_xid).to eq(0)
expect(jira_import.jira_project_name).to eq(fake_key)
expect(jira_import.jira_project_key).to eq(fake_key)
expect(jira_import.user).to eq(user)
end
it 'creates Jira import label' do
expect { subject }.to change { Label.count }.by(1)
end
it 'creates Jira label title with correct number' do
jira_import = subject.payload[:import_data]
label_title = "jira-import::#{jira_import.jira_project_key}-1"
expect(jira_import.jira_project_xid).to eq(0)
expect(jira_import.jira_project_name).to eq(fake_key)
expect(jira_import.jira_project_key).to eq(fake_key)
expect(jira_import.user).to eq(user)
expect(jira_import.label.title).to eq(label_title)
end
end
it 'creates Jira import label' do
expect { subject }.to change { Label.count }.by(1)
context 'when mapping is nil' do
let(:mapping) { nil }
it 'returns success response' do
expect(Gitlab::JiraImport).not_to receive(:cache_users_mapping)
expect(subject).to be_a(ServiceResponse)
expect(subject).to be_success
end
end
it 'creates Jira label title with correct number' do
jira_import = subject.payload[:import_data]
label_title = "jira-import::#{jira_import.jira_project_key}-1"
context 'when no mapping value is complete' do
let(:mapping) do
[
{ jira_account_id: 'def', gitlab_id: nil },
{ jira_account_id: nil, gitlab_id: 1 }
]
end
expect(jira_import.label.title).to eq(label_title)
it 'returns success response' do
expect(Gitlab::JiraImport).not_to receive(:cache_users_mapping)
expect(subject).to be_a(ServiceResponse)
expect(subject).to be_success
end
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