Commit 62a63f65 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch '216145-jira-users-mapping' into 'master'

Prepare Jira users  importer

See merge request gitlab-org/gitlab!32975
parents f69b7e67 e6caf517
# frozen_string_literal: true
module JiraImport
class UsersImporter
attr_reader :user, :project, :start_at, :result
MAX_USERS = 50
def initialize(user, project, start_at)
@project = project
@start_at = start_at
@user = user
end
def execute
project.validate_jira_import_settings!(user: user)
return ServiceResponse.success(payload: nil) if users.blank?
result = UsersMapper.new(project, users).execute
ServiceResponse.success(payload: result)
rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED, URI::InvalidURIError, JIRA::HTTPError, OpenSSL::SSL::SSLError => error
Gitlab::ErrorTracking.track_exception(error, project_id: project.id, request: url)
ServiceResponse.error(message: "There was an error when communicating to Jira: #{error.message}")
end
private
def users
@users ||= client.get(url)
end
def url
"/rest/api/2/users?maxResults=#{MAX_USERS}&startAt=#{start_at.to_i}"
end
def client
@client ||= project.jira_service.client
end
end
end
# frozen_string_literal: true
module JiraImport
class UsersMapper
attr_reader :project, :jira_users
def initialize(project, jira_users)
@project = project
@jira_users = jira_users
end
def execute
jira_users.to_a.map do |jira_user|
{
jira_account_id: jira_user['accountId'],
jira_display_name: jira_user['displayName'],
jira_email: jira_user['emailAddress'],
gitlab_id: match_user(jira_user)
}
end
end
private
# TODO: Matching user by email and displayName will be done as the part
# of follow-up issue: https://gitlab.com/gitlab-org/gitlab/-/issues/219023
def match_user(jira_user)
nil
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe JiraImport::UsersImporter do
include JiraServiceHelper
let_it_be(:user) { create(:user) }
let_it_be(:project, reload: true) { create(:project) }
let_it_be(:start_at) { 7 }
let(:importer) { described_class.new(user, project, start_at) }
subject { importer.execute }
describe '#execute' do
before do
stub_jira_service_test
project.add_maintainer(user)
end
context 'when Jira import is not configured properly' do
it 'raises an error' do
expect { subject }.to raise_error(Projects::ImportService::Error)
end
end
context 'when Jira import is configured correctly' do
let_it_be(:jira_service) { create(:jira_service, project: project, active: true) }
let(:client) { double }
before do
expect(importer).to receive(:client).and_return(client)
end
context 'when jira client raises an error' do
it 'returns an error response' do
expect(client).to receive(:get).and_raise(Timeout::Error)
expect(subject.error?).to be_truthy
expect(subject.message).to include('There was an error when communicating to Jira')
end
end
context 'when jira client returns result' do
before do
allow(client).to receive(:get).with('/rest/api/2/users?maxResults=50&startAt=7')
.and_return(jira_users)
end
context 'when jira client returns an empty array' do
let(:jira_users) { [] }
it 'retturns nil payload' do
expect(subject.success?).to be_truthy
expect(subject.payload).to be_nil
end
end
context 'when jira client returns an results' do
let(:jira_users) { [{ 'name' => 'user1' }, { 'name' => 'user2' }] }
let(:mapped_users) { [{ jira_display_name: 'user1', gitlab_id: 5 }] }
before do
expect(JiraImport::UsersMapper).to receive(:new).with(project, jira_users)
.and_return(double(execute: mapped_users))
end
it 'returns the mapped users' do
expect(subject.success?).to be_truthy
expect(subject.payload).to eq(mapped_users)
end
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe JiraImport::UsersMapper do
let_it_be(:project) { create(:project) }
subject { described_class.new(project, jira_users).execute }
describe '#execute' do
context 'jira_users is nil' do
let(:jira_users) { nil }
it 'returns an empty array' do
expect(subject).to be_empty
end
end
context 'when jira_users is present' do
let(:jira_users) do
[
{ 'accountId' => 'abcd', 'displayName' => 'user1' },
{ 'accountId' => 'efg' },
{ 'accountId' => 'hij', 'displayName' => 'user3', 'emailAddress' => 'user3@example.com' }
]
end
# TODO: now we only create an array in a proper format
# mapping is tracked in https://gitlab.com/gitlab-org/gitlab/-/issues/219023
let(:mapped_users) do
[
{ jira_account_id: 'abcd', jira_display_name: 'user1', jira_email: nil, gitlab_id: nil },
{ jira_account_id: 'efg', jira_display_name: nil, jira_email: nil, gitlab_id: nil },
{ jira_account_id: 'hij', jira_display_name: 'user3', jira_email: 'user3@example.com', gitlab_id: nil }
]
end
it 'returns users mapped to Gitlab' do
expect(subject).to eq(mapped_users)
end
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