2020-01-01 13:55:28 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
describe Gitlab::LegacyGithubImport::Importer do
|
|
|
|
shared_examples 'Gitlab::LegacyGithubImport::Importer#execute' do
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:expected_not_called) { [] }
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
before do
|
2017-08-17 22:00:37 +05:30
|
|
|
allow(project).to receive(:import_data).and_return(double.as_null_object)
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'calls import methods' do
|
|
|
|
importer = described_class.new(project)
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
expected_called = [
|
|
|
|
:import_labels, :import_milestones, :import_pull_requests, :import_issues,
|
2019-09-04 21:01:54 +05:30
|
|
|
:import_wiki, :import_releases, :handle_errors,
|
|
|
|
[:import_comments, :issues],
|
|
|
|
[:import_comments, :pull_requests]
|
2017-08-17 22:00:37 +05:30
|
|
|
]
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
expected_called -= expected_not_called
|
|
|
|
|
|
|
|
aggregate_failures do
|
2019-09-04 21:01:54 +05:30
|
|
|
expected_called.each do |method_name, arg|
|
|
|
|
base_expectation = proc { expect(importer).to receive(method_name) }
|
|
|
|
arg ? base_expectation.call.with(arg) : base_expectation.call
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
expected_not_called.each do |method_name, arg|
|
|
|
|
base_expectation = proc { expect(importer).not_to receive(method_name) }
|
|
|
|
arg ? base_expectation.call.with(arg) : base_expectation.call
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
importer.execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
shared_examples 'Gitlab::LegacyGithubImport::Importer#execute an error occurs' do
|
2017-08-17 22:00:37 +05:30
|
|
|
before do
|
|
|
|
allow(project).to receive(:import_data).and_return(double.as_null_object)
|
|
|
|
|
|
|
|
allow(Rails).to receive(:cache).and_return(ActiveSupport::Cache::MemoryStore.new)
|
|
|
|
|
|
|
|
allow_any_instance_of(Octokit::Client).to receive(:rate_limit!).and_raise(Octokit::NotFound)
|
|
|
|
allow_any_instance_of(Gitlab::Shell).to receive(:import_repository).and_raise(Gitlab::Shell::Error)
|
|
|
|
|
|
|
|
allow_any_instance_of(Octokit::Client).to receive(:user).and_return(octocat)
|
|
|
|
allow_any_instance_of(Octokit::Client).to receive(:labels).and_return([label1, label2])
|
|
|
|
allow_any_instance_of(Octokit::Client).to receive(:milestones).and_return([milestone, milestone])
|
|
|
|
allow_any_instance_of(Octokit::Client).to receive(:issues).and_return([issue1, issue2])
|
|
|
|
allow_any_instance_of(Octokit::Client).to receive(:pull_requests).and_return([pull_request, pull_request])
|
|
|
|
allow_any_instance_of(Octokit::Client).to receive(:issues_comments).and_return([])
|
|
|
|
allow_any_instance_of(Octokit::Client).to receive(:pull_requests_comments).and_return([])
|
|
|
|
allow_any_instance_of(Octokit::Client).to receive(:last_response).and_return(double(rels: { next: nil }))
|
|
|
|
allow_any_instance_of(Octokit::Client).to receive(:releases).and_return([release1, release2])
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:label1) do
|
|
|
|
double(
|
|
|
|
name: 'Bug',
|
|
|
|
color: 'ff0000',
|
|
|
|
url: "#{api_root}/repos/octocat/Hello-World/labels/bug"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:label2) do
|
|
|
|
double(
|
|
|
|
name: nil,
|
|
|
|
color: 'ff0000',
|
|
|
|
url: "#{api_root}/repos/octocat/Hello-World/labels/bug"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:milestone) do
|
|
|
|
double(
|
|
|
|
id: 1347, # For Gitea
|
|
|
|
number: 1347,
|
|
|
|
state: 'open',
|
|
|
|
title: '1.0',
|
|
|
|
description: 'Version 1.0',
|
|
|
|
due_on: nil,
|
|
|
|
created_at: created_at,
|
|
|
|
updated_at: updated_at,
|
|
|
|
closed_at: nil,
|
|
|
|
url: "#{api_root}/repos/octocat/Hello-World/milestones/1"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:issue1) do
|
|
|
|
double(
|
|
|
|
number: 1347,
|
|
|
|
milestone: nil,
|
|
|
|
state: 'open',
|
|
|
|
title: 'Found a bug',
|
|
|
|
body: "I'm having a problem with this.",
|
|
|
|
assignee: nil,
|
|
|
|
user: octocat,
|
|
|
|
comments: 0,
|
|
|
|
pull_request: nil,
|
|
|
|
created_at: created_at,
|
|
|
|
updated_at: updated_at,
|
|
|
|
closed_at: nil,
|
|
|
|
url: "#{api_root}/repos/octocat/Hello-World/issues/1347",
|
|
|
|
labels: [double(name: 'Label #1')]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:issue2) do
|
|
|
|
double(
|
|
|
|
number: 1348,
|
|
|
|
milestone: nil,
|
|
|
|
state: 'open',
|
|
|
|
title: nil,
|
|
|
|
body: "I'm having a problem with this.",
|
|
|
|
assignee: nil,
|
|
|
|
user: octocat,
|
|
|
|
comments: 0,
|
|
|
|
pull_request: nil,
|
|
|
|
created_at: created_at,
|
|
|
|
updated_at: updated_at,
|
|
|
|
closed_at: nil,
|
|
|
|
url: "#{api_root}/repos/octocat/Hello-World/issues/1348",
|
|
|
|
labels: [double(name: 'Label #2')]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:release1) do
|
|
|
|
double(
|
|
|
|
tag_name: 'v1.0.0',
|
|
|
|
name: 'First release',
|
|
|
|
body: 'Release v1.0.0',
|
|
|
|
draft: false,
|
|
|
|
created_at: created_at,
|
2019-09-30 21:07:59 +05:30
|
|
|
published_at: created_at,
|
2017-08-17 22:00:37 +05:30
|
|
|
updated_at: updated_at,
|
|
|
|
url: "#{api_root}/repos/octocat/Hello-World/releases/1"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:release2) do
|
|
|
|
double(
|
2019-02-15 15:39:39 +05:30
|
|
|
tag_name: 'v1.1.0',
|
2017-08-17 22:00:37 +05:30
|
|
|
name: 'Second release',
|
|
|
|
body: nil,
|
|
|
|
draft: false,
|
|
|
|
created_at: created_at,
|
2019-09-30 21:07:59 +05:30
|
|
|
published_at: created_at,
|
2017-08-17 22:00:37 +05:30
|
|
|
updated_at: updated_at,
|
|
|
|
url: "#{api_root}/repos/octocat/Hello-World/releases/2"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { described_class.new(project) }
|
|
|
|
|
|
|
|
it 'returns true' do
|
|
|
|
expect(subject.execute).to eq true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not raise an error' do
|
|
|
|
expect { subject.execute }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'stores error messages' do
|
|
|
|
error = {
|
|
|
|
message: 'The remote data could not be fully imported.',
|
|
|
|
errors: [
|
|
|
|
{ type: :label, url: "#{api_root}/repos/octocat/Hello-World/labels/bug", errors: "Validation failed: Title can't be blank, Title is invalid" },
|
|
|
|
{ type: :issue, url: "#{api_root}/repos/octocat/Hello-World/issues/1348", errors: "Validation failed: Title can't be blank" },
|
|
|
|
{ type: :wiki, errors: "Gitlab::Shell::Error" }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
unless project.gitea_import?
|
|
|
|
error[:errors] << { type: :release, url: "#{api_root}/repos/octocat/Hello-World/releases/2", errors: "Validation failed: Description can't be blank" }
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
described_class.new(project).execute
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
expect(project.import_state.last_error).to eq error.to_json
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
shared_examples 'Gitlab::LegacyGithubImport unit-testing' do
|
2017-08-17 22:00:37 +05:30
|
|
|
describe '#clean_up_restored_branches' do
|
|
|
|
subject { described_class.new(project) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(gh_pull_request).to receive(:source_branch_exists?).at_least(:once) { false }
|
|
|
|
allow(gh_pull_request).to receive(:target_branch_exists?).at_least(:once) { false }
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'when pull request stills open' do
|
2018-03-17 18:26:18 +05:30
|
|
|
let(:gh_pull_request) { Gitlab::LegacyGithubImport::PullRequestFormatter.new(project, pull_request) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
it 'does not remove branches' do
|
|
|
|
expect(subject).not_to receive(:remove_branch)
|
|
|
|
subject.send(:clean_up_restored_branches, gh_pull_request)
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'when pull request is closed' do
|
2018-03-17 18:26:18 +05:30
|
|
|
let(:gh_pull_request) { Gitlab::LegacyGithubImport::PullRequestFormatter.new(project, closed_pull_request) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
it 'does remove branches' do
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(subject).to receive(:remove_branch).at_least(:twice)
|
2017-08-17 22:00:37 +05:30
|
|
|
subject.send(:clean_up_restored_branches, gh_pull_request)
|
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:project) { create(:project, :repository, :wiki_disabled, import_url: "#{repo_root}/octocat/Hello-World.git") }
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:octocat) { double(id: 123456, login: 'octocat', email: 'octocat@example.com') }
|
|
|
|
let(:credentials) { { user: 'joe' } }
|
|
|
|
|
|
|
|
let(:created_at) { DateTime.strptime('2011-01-26T19:01:12Z') }
|
|
|
|
let(:updated_at) { DateTime.strptime('2011-01-27T19:01:12Z') }
|
|
|
|
let(:repository) { double(id: 1, fork: false) }
|
|
|
|
let(:source_sha) { create(:commit, project: project).id }
|
|
|
|
let(:source_branch) { double(ref: 'branch-merged', repo: repository, sha: source_sha, user: octocat) }
|
|
|
|
let(:target_sha) { create(:commit, project: project, git_commit: RepoHelpers.another_sample_commit).id }
|
|
|
|
let(:target_branch) { double(ref: 'master', repo: repository, sha: target_sha, user: octocat) }
|
|
|
|
let(:pull_request) do
|
|
|
|
double(
|
|
|
|
number: 1347,
|
|
|
|
milestone: nil,
|
|
|
|
state: 'open',
|
|
|
|
title: 'New feature',
|
|
|
|
body: 'Please pull these awesome changes',
|
|
|
|
head: source_branch,
|
|
|
|
base: target_branch,
|
|
|
|
assignee: nil,
|
|
|
|
user: octocat,
|
|
|
|
created_at: created_at,
|
|
|
|
updated_at: updated_at,
|
|
|
|
closed_at: nil,
|
|
|
|
merged_at: nil,
|
|
|
|
url: "#{api_root}/repos/octocat/Hello-World/pulls/1347",
|
|
|
|
labels: [double(name: 'Label #2')]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
let(:closed_pull_request) do
|
|
|
|
double(
|
|
|
|
number: 1347,
|
|
|
|
milestone: nil,
|
|
|
|
state: 'closed',
|
|
|
|
title: 'New feature',
|
|
|
|
body: 'Please pull these awesome changes',
|
|
|
|
head: source_branch,
|
|
|
|
base: target_branch,
|
|
|
|
assignee: nil,
|
|
|
|
user: octocat,
|
|
|
|
created_at: created_at,
|
|
|
|
updated_at: updated_at,
|
|
|
|
closed_at: updated_at,
|
|
|
|
merged_at: nil,
|
|
|
|
url: "#{api_root}/repos/octocat/Hello-World/pulls/1347",
|
|
|
|
labels: [double(name: 'Label #2')]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when importing a GitHub project' do
|
|
|
|
let(:api_root) { 'https://api.github.com' }
|
|
|
|
let(:repo_root) { 'https://github.com' }
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
subject { described_class.new(project) }
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it_behaves_like 'Gitlab::LegacyGithubImport::Importer#execute'
|
|
|
|
it_behaves_like 'Gitlab::LegacyGithubImport::Importer#execute an error occurs'
|
|
|
|
it_behaves_like 'Gitlab::LegacyGithubImport unit-testing'
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
describe '#client' do
|
|
|
|
it 'instantiates a Client' do
|
|
|
|
allow(project).to receive(:import_data).and_return(double(credentials: credentials))
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(Gitlab::LegacyGithubImport::Client).to receive(:new).with(
|
2017-08-17 22:00:37 +05:30
|
|
|
credentials[:user],
|
|
|
|
{}
|
2016-09-29 09:46:39 +05:30
|
|
|
)
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
subject.client
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'when importing a Gitea project' do
|
|
|
|
let(:api_root) { 'https://try.gitea.io/api/v1' }
|
|
|
|
let(:repo_root) { 'https://try.gitea.io' }
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
subject { described_class.new(project) }
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
before do
|
|
|
|
project.update(import_type: 'gitea', import_url: "#{repo_root}/foo/group/project.git")
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it_behaves_like 'Gitlab::LegacyGithubImport::Importer#execute' do
|
2019-09-04 21:01:54 +05:30
|
|
|
let(:expected_not_called) { [:import_releases, [:import_comments, :pull_requests]] }
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
it_behaves_like 'Gitlab::LegacyGithubImport::Importer#execute an error occurs'
|
|
|
|
it_behaves_like 'Gitlab::LegacyGithubImport unit-testing'
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
describe '#client' do
|
|
|
|
it 'instantiates a Client' do
|
|
|
|
allow(project).to receive(:import_data).and_return(double(credentials: credentials))
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(Gitlab::LegacyGithubImport::Client).to receive(:new).with(
|
2017-08-17 22:00:37 +05:30
|
|
|
credentials[:user],
|
|
|
|
{ host: "#{repo_root}:443/foo", api_version: 'v1' }
|
|
|
|
)
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
subject.client
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|