1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::BitbucketImport::Importers::IssueImporter, :clean_gitlab_redis_cache, feature_category: :importers do
include AfterNextHelpers
let_it_be(:project) { create(:project, :repository) }
let_it_be(:bitbucket_user) { create(:user) }
let_it_be(:identity) { create(:identity, user: bitbucket_user, extern_uid: 'bitbucket_user', provider: :bitbucket) }
let_it_be(:default_work_item_type) { create(:work_item_type) }
let_it_be(:label) { create(:label, project: project) }
let(:hash) do
{
iid: 111,
title: 'title',
description: 'description',
state: 'closed',
author: 'bitbucket_user',
milestone: 'my milestone',
issue_type_id: default_work_item_type.id,
label_id: label.id,
created_at: Date.today,
updated_at: Date.today
}
end
subject(:importer) { described_class.new(project, hash) }
before do
allow(Gitlab::Git).to receive(:ref_name).and_return('refname')
end
describe '#execute' do
it 'creates an issue' do
expect { importer.execute }.to change { project.issues.count }.from(0).to(1)
issue = project.issues.first
expect(issue.description).to eq('description')
expect(issue.author).to eq(bitbucket_user)
expect(issue.closed?).to be_truthy
expect(issue.milestone).to eq(project.milestones.first)
expect(issue.work_item_type).to eq(default_work_item_type)
expect(issue.labels).to eq([label])
expect(issue.created_at).to eq(Date.today)
expect(issue.updated_at).to eq(Date.today)
end
context 'when the author does not have a bitbucket identity' do
before do
identity.update!(provider: :github)
end
it 'sets the author to the project creator and adds the author to the description' do
importer.execute
issue = project.issues.first
expect(issue.author).to eq(project.creator)
expect(issue.description).to eq("*Created by: bitbucket_user*\n\ndescription")
end
end
context 'when a milestone with the same title exists' do
let_it_be(:milestone) { create(:milestone, project: project, title: 'my milestone') }
it 'assigns the milestone and does not create a new milestone' do
expect { importer.execute }.not_to change { project.milestones.count }
expect(project.issues.first.milestone).to eq(milestone)
end
end
context 'when a milestone with the same title does not exist' do
it 'creates a new milestone and assigns it' do
expect { importer.execute }.to change { project.milestones.count }.from(0).to(1)
expect(project.issues.first.milestone).to eq(project.milestones.first)
end
end
context 'when an error is raised' do
it 'tracks the failure and does not fail' do
expect(Gitlab::Import::ImportFailureService).to receive(:track).once
described_class.new(project, hash.except(:title)).execute
end
end
it 'logs its progress' do
allow(Gitlab::Import::MergeRequestCreator).to receive_message_chain(:new, :execute)
expect(Gitlab::BitbucketImport::Logger)
.to receive(:info).with(include(message: 'starting', iid: anything)).and_call_original
expect(Gitlab::BitbucketImport::Logger)
.to receive(:info).with(include(message: 'finished', iid: anything)).and_call_original
importer.execute
end
end
end
|