2016-06-02 11:05:42 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
describe Gitlab::BitbucketImport::Importer do
|
2016-06-16 23:09:34 +05:30
|
|
|
include ImportSpecHelper
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
before do
|
2016-06-16 23:09:34 +05:30
|
|
|
stub_omniauth_provider('bitbucket')
|
2019-06-05 12:25:43 +05:30
|
|
|
stub_feature_flags(stricter_mr_branch_name: false)
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
let(:statuses) do
|
|
|
|
[
|
|
|
|
"open",
|
|
|
|
"resolved",
|
|
|
|
"on hold",
|
|
|
|
"invalid",
|
|
|
|
"duplicate",
|
|
|
|
"wontfix",
|
2019-03-02 22:35:43 +05:30
|
|
|
"closed" # undocumented status
|
2016-06-02 11:05:42 +05:30
|
|
|
]
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
let(:reporters) do
|
|
|
|
[
|
|
|
|
nil,
|
|
|
|
{ "username" => "reporter1" },
|
|
|
|
nil,
|
|
|
|
{ "username" => "reporter2" },
|
|
|
|
{ "username" => "reporter1" },
|
|
|
|
nil,
|
|
|
|
{ "username" => "reporter3" }
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
let(:sample_issues_statuses) do
|
|
|
|
issues = []
|
|
|
|
|
|
|
|
statuses.map.with_index do |status, index|
|
|
|
|
issues << {
|
2017-08-17 22:00:37 +05:30
|
|
|
id: index,
|
|
|
|
state: status,
|
2016-06-02 11:05:42 +05:30
|
|
|
title: "Issue #{index}",
|
2017-08-17 22:00:37 +05:30
|
|
|
kind: 'bug',
|
|
|
|
content: {
|
|
|
|
raw: "Some content to issue #{index}",
|
|
|
|
markup: "markdown",
|
|
|
|
html: "Some content to issue #{index}"
|
|
|
|
}
|
2016-06-02 11:05:42 +05:30
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
reporters.map.with_index do |reporter, index|
|
|
|
|
issues[index]['reporter'] = reporter
|
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
issues
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:project_identifier) { 'namespace/repo' }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
let(:data) do
|
|
|
|
{
|
|
|
|
'bb_session' => {
|
2017-08-17 22:00:37 +05:30
|
|
|
'bitbucket_token' => "123456",
|
|
|
|
'bitbucket_refresh_token' => "secret"
|
2016-06-02 11:05:42 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
let(:project) do
|
|
|
|
create(
|
2017-09-10 17:25:29 +05:30
|
|
|
:project,
|
2018-11-20 20:47:30 +05:30
|
|
|
:repository,
|
2016-06-02 11:05:42 +05:30
|
|
|
import_source: project_identifier,
|
2018-03-17 18:26:18 +05:30
|
|
|
import_url: "https://bitbucket.org/#{project_identifier}.git",
|
2017-09-10 17:25:29 +05:30
|
|
|
import_data_attributes: { credentials: data }
|
2016-06-02 11:05:42 +05:30
|
|
|
)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:importer) { described_class.new(project) }
|
2018-03-17 18:26:18 +05:30
|
|
|
let(:gitlab_shell) { double }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
let(:issues_statuses_sample_data) do
|
|
|
|
{
|
|
|
|
count: sample_issues_statuses.count,
|
2017-08-17 22:00:37 +05:30
|
|
|
values: sample_issues_statuses
|
2016-06-02 11:05:42 +05:30
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
let(:sample) { RepoHelpers.sample_compare }
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
before do
|
|
|
|
allow(importer).to receive(:gitlab_shell) { gitlab_shell }
|
|
|
|
end
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
subject { described_class.new(project) }
|
|
|
|
|
|
|
|
describe '#import_pull_requests' do
|
2019-05-03 19:53:19 +05:30
|
|
|
let(:source_branch_sha) { sample.commits.last }
|
|
|
|
let(:target_branch_sha) { sample.commits.first }
|
2019-09-30 21:07:59 +05:30
|
|
|
let(:pull_request) do
|
|
|
|
instance_double(
|
2018-11-20 20:47:30 +05:30
|
|
|
Bitbucket::Representation::PullRequest,
|
|
|
|
iid: 10,
|
2019-05-03 19:53:19 +05:30
|
|
|
source_branch_sha: source_branch_sha,
|
2018-11-20 20:47:30 +05:30
|
|
|
source_branch_name: Gitlab::Git::BRANCH_REF_PREFIX + sample.source_branch,
|
2019-05-03 19:53:19 +05:30
|
|
|
target_branch_sha: target_branch_sha,
|
2018-11-20 20:47:30 +05:30
|
|
|
target_branch_name: Gitlab::Git::BRANCH_REF_PREFIX + sample.target_branch,
|
|
|
|
title: 'This is a title',
|
|
|
|
description: 'This is a test pull request',
|
|
|
|
state: 'merged',
|
|
|
|
author: 'other',
|
|
|
|
created_at: Time.now,
|
|
|
|
updated_at: Time.now)
|
2019-09-30 21:07:59 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(subject).to receive(:import_wiki)
|
|
|
|
allow(subject).to receive(:import_issues)
|
2018-11-20 20:47:30 +05:30
|
|
|
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab-test/compare/c1acaa58bbcbc3eafe538cb8274ba387047b69f8...5937ac0a7beb003549fc5fd26fc247ad
|
|
|
|
@inline_note = instance_double(
|
|
|
|
Bitbucket::Representation::PullRequestComment,
|
|
|
|
iid: 2,
|
|
|
|
file_path: '.gitmodules',
|
|
|
|
old_pos: nil,
|
|
|
|
new_pos: 4,
|
|
|
|
note: 'Hello world',
|
|
|
|
author: 'root',
|
|
|
|
created_at: Time.now,
|
|
|
|
updated_at: Time.now,
|
|
|
|
inline?: true,
|
|
|
|
has_parent?: false)
|
|
|
|
|
|
|
|
@reply = instance_double(
|
|
|
|
Bitbucket::Representation::PullRequestComment,
|
|
|
|
iid: 3,
|
|
|
|
file_path: '.gitmodules',
|
|
|
|
note: 'Hello world',
|
|
|
|
author: 'root',
|
|
|
|
created_at: Time.now,
|
|
|
|
updated_at: Time.now,
|
|
|
|
inline?: true,
|
|
|
|
has_parent?: true,
|
|
|
|
parent_id: 2)
|
|
|
|
|
|
|
|
comments = [@inline_note, @reply]
|
|
|
|
|
|
|
|
allow(subject.client).to receive(:repo)
|
|
|
|
allow(subject.client).to receive(:pull_requests).and_return([pull_request])
|
|
|
|
allow(subject.client).to receive(:pull_request_comments).with(anything, pull_request.iid).and_return(comments)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'imports threaded discussions' do
|
|
|
|
expect { subject.execute }.to change { MergeRequest.count }.by(1)
|
|
|
|
|
|
|
|
merge_request = MergeRequest.first
|
|
|
|
expect(merge_request.notes.count).to eq(2)
|
|
|
|
expect(merge_request.notes.map(&:discussion_id).uniq.count).to eq(1)
|
|
|
|
|
|
|
|
notes = merge_request.notes.order(:id).to_a
|
|
|
|
start_note = notes.first
|
|
|
|
expect(start_note).to be_a(DiffNote)
|
|
|
|
expect(start_note.note).to eq(@inline_note.note)
|
|
|
|
|
|
|
|
reply_note = notes.last
|
|
|
|
expect(reply_note).to be_a(DiffNote)
|
|
|
|
expect(reply_note.note).to eq(@reply.note)
|
|
|
|
end
|
2019-05-03 19:53:19 +05:30
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
context 'when importing a pull request throws an exception' do
|
|
|
|
before do
|
|
|
|
allow(pull_request).to receive(:raw).and_return('hello world')
|
|
|
|
allow(subject.client).to receive(:pull_request_comments).and_raise(HTTParty::Error)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs an error without the backtrace' do
|
|
|
|
subject.execute
|
|
|
|
|
|
|
|
expect(subject.errors.count).to eq(1)
|
|
|
|
expect(subject.errors.first.keys).to match_array(%i(type iid errors))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-03 19:53:19 +05:30
|
|
|
context "when branches' sha is not found in the repository" do
|
|
|
|
let(:source_branch_sha) { 'a' * Commit::MIN_SHA_LENGTH }
|
|
|
|
let(:target_branch_sha) { 'b' * Commit::MIN_SHA_LENGTH }
|
|
|
|
|
|
|
|
it 'uses the pull request sha references' do
|
|
|
|
expect { subject.execute }.to change { MergeRequest.count }.by(1)
|
|
|
|
|
|
|
|
merge_request_diff = MergeRequest.first.merge_request_diff
|
|
|
|
expect(merge_request_diff.head_commit_sha).to eq source_branch_sha
|
|
|
|
expect(merge_request_diff.start_commit_sha).to eq target_branch_sha
|
|
|
|
end
|
|
|
|
end
|
2018-11-20 20:47:30 +05:30
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
context 'issues statuses' do
|
|
|
|
before do
|
2017-08-17 22:00:37 +05:30
|
|
|
# HACK: Bitbucket::Representation.const_get('Issue') seems to return ::Issue without this
|
|
|
|
Bitbucket::Representation::Issue.new({})
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
stub_request(
|
|
|
|
:get,
|
2017-08-17 22:00:37 +05:30
|
|
|
"https://api.bitbucket.org/2.0/repositories/#{project_identifier}"
|
|
|
|
).to_return(status: 200,
|
|
|
|
headers: { "Content-Type" => "application/json" },
|
|
|
|
body: { has_issues: true, full_name: project_identifier }.to_json)
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
stub_request(
|
|
|
|
:get,
|
2017-08-17 22:00:37 +05:30
|
|
|
"https://api.bitbucket.org/2.0/repositories/#{project_identifier}/issues?pagelen=50&sort=created_on"
|
|
|
|
).to_return(status: 200,
|
|
|
|
headers: { "Content-Type" => "application/json" },
|
|
|
|
body: issues_statuses_sample_data.to_json)
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
stub_request(:get, "https://api.bitbucket.org/2.0/repositories/namespace/repo?pagelen=50&sort=created_on")
|
|
|
|
.with(headers: { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization' => 'Bearer', 'User-Agent' => 'Faraday v0.9.2' })
|
|
|
|
.to_return(status: 200, body: "", headers: {})
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
sample_issues_statuses.each_with_index do |issue, index|
|
|
|
|
stub_request(
|
|
|
|
:get,
|
2017-08-17 22:00:37 +05:30
|
|
|
"https://api.bitbucket.org/2.0/repositories/#{project_identifier}/issues/#{issue[:id]}/comments?pagelen=50&sort=created_on"
|
2016-06-02 11:05:42 +05:30
|
|
|
).to_return(
|
|
|
|
status: 200,
|
2017-08-17 22:00:37 +05:30
|
|
|
headers: { "Content-Type" => "application/json" },
|
|
|
|
body: { author_info: { username: "username" }, utc_created_on: index }.to_json
|
2016-06-02 11:05:42 +05:30
|
|
|
)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
stub_request(
|
|
|
|
:get,
|
|
|
|
"https://api.bitbucket.org/2.0/repositories/#{project_identifier}/pullrequests?pagelen=50&sort=created_on&state=ALL"
|
|
|
|
).to_return(status: 200,
|
|
|
|
headers: { "Content-Type" => "application/json" },
|
|
|
|
body: {}.to_json)
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
context 'creating labels on project' do
|
|
|
|
before do
|
|
|
|
allow(importer).to receive(:import_wiki)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates labels as expected' do
|
|
|
|
expect { importer.execute }.to change { Label.count }.from(0).to(Gitlab::BitbucketImport::Importer::LABELS.size)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not fail if label is already existing' do
|
|
|
|
label = Gitlab::BitbucketImport::Importer::LABELS.first
|
|
|
|
::Labels::CreateService.new(label).execute(project: project)
|
|
|
|
|
|
|
|
expect { importer.execute }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create new labels' do
|
|
|
|
Gitlab::BitbucketImport::Importer::LABELS.each do |label|
|
|
|
|
create(:label, project: project, title: label[:title])
|
|
|
|
end
|
|
|
|
|
|
|
|
expect { importer.execute }.not_to change { Label.count }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not update existing ones' do
|
|
|
|
label_title = Gitlab::BitbucketImport::Importer::LABELS.first[:title]
|
|
|
|
existing_label = create(:label, project: project, title: label_title)
|
|
|
|
# Reload label from database so we avoid timestamp comparison issues related to time precision when comparing
|
|
|
|
# attributes later.
|
|
|
|
existing_label.reload
|
|
|
|
|
|
|
|
Timecop.freeze(Time.now + 1.minute) do
|
|
|
|
importer.execute
|
|
|
|
|
|
|
|
label_after_import = project.labels.find(existing_label.id)
|
|
|
|
expect(label_after_import.attributes).to eq(existing_label.attributes)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'maps statuses to open or closed' do
|
2018-03-17 18:26:18 +05:30
|
|
|
allow(importer).to receive(:import_wiki)
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
importer.execute
|
|
|
|
|
|
|
|
expect(project.issues.where(state: "closed").size).to eq(5)
|
|
|
|
expect(project.issues.where(state: "opened").size).to eq(2)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
describe 'wiki import' do
|
|
|
|
it 'is skipped when the wiki exists' do
|
|
|
|
expect(project.wiki).to receive(:repository_exists?) { true }
|
2019-03-02 22:35:43 +05:30
|
|
|
expect(importer.gitlab_shell).not_to receive(:import_wiki_repository)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
importer.execute
|
|
|
|
|
|
|
|
expect(importer.errors).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'imports to the project disk_path' do
|
|
|
|
expect(project.wiki).to receive(:repository_exists?) { false }
|
2019-03-02 22:35:43 +05:30
|
|
|
expect(importer.gitlab_shell).to receive(:import_wiki_repository)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
importer.execute
|
|
|
|
|
|
|
|
expect(importer.errors).to be_empty
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
describe 'issue import' do
|
|
|
|
it 'maps reporters to anonymous if bitbucket reporter is nil' do
|
|
|
|
allow(importer).to receive(:import_wiki)
|
|
|
|
importer.execute
|
|
|
|
|
|
|
|
expect(project.issues.size).to eq(7)
|
|
|
|
expect(project.issues.where("description LIKE ?", '%Anonymous%').size).to eq(3)
|
|
|
|
expect(project.issues.where("description LIKE ?", '%reporter1%').size).to eq(2)
|
|
|
|
expect(project.issues.where("description LIKE ?", '%reporter2%').size).to eq(1)
|
|
|
|
expect(project.issues.where("description LIKE ?", '%reporter3%').size).to eq(1)
|
|
|
|
expect(importer.errors).to be_empty
|
|
|
|
end
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
end
|