summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/legacy_github_import/issue_formatter_spec.rb
diff options
context:
space:
mode:
authorYorick Peterse <yorickpeterse@gmail.com>2017-10-13 18:50:36 +0200
committerYorick Peterse <yorickpeterse@gmail.com>2017-11-07 23:24:59 +0100
commit4dfe26cd8b6863b7e6c81f5c280cdafe9b6e17b6 (patch)
treeea02569de0221c4ad3fe778f6ec991cdfce587bf /spec/lib/gitlab/legacy_github_import/issue_formatter_spec.rb
parent90be53c5d39bff5e371cf8a6e11a39bf5dad7bcc (diff)
downloadgitlab-ce-4dfe26cd8b6863b7e6c81f5c280cdafe9b6e17b6.tar.gz
Rewrite the GitHub importer from scratch
Prior to this MR there were two GitHub related importers: * Github::Import: the main importer used for GitHub projects * Gitlab::GithubImport: importer that's somewhat confusingly used for importing Gitea projects (apparently they have a compatible API) This MR renames the Gitea importer to Gitlab::LegacyGithubImport and introduces a new GitHub importer in the Gitlab::GithubImport namespace. This new GitHub importer uses Sidekiq for importing multiple resources in parallel, though it also has the ability to import data sequentially should this be necessary. The new code is spread across the following directories: * lib/gitlab/github_import: this directory contains most of the importer code such as the classes used for importing resources. * app/workers/gitlab/github_import: this directory contains the Sidekiq workers, most of which simply use the code from the directory above. * app/workers/concerns/gitlab/github_import: this directory provides a few modules that are included in every GitHub importer worker. == Stages The import work is divided into separate stages, with each stage importing a specific set of data. Stages will schedule the work that needs to be performed, followed by scheduling a job for the "AdvanceStageWorker" worker. This worker will periodically check if all work is completed and schedule the next stage if this is the case. If work is not yet completed this worker will reschedule itself. Using this approach we don't have to block threads by calling `sleep()`, as doing so for large projects could block the thread from doing any work for many hours. == Retrying Work Workers will reschedule themselves whenever necessary. For example, hitting the GitHub API's rate limit will result in jobs rescheduling themselves. These jobs are not processed until the rate limit has been reset. == User Lookups Part of the importing process involves looking up user details in the GitHub API so we can map them to GitLab users. The old importer used an in-memory cache, but this obviously doesn't work when the work is spread across different threads. The new importer uses a Redis cache and makes sure we only perform API/database calls if absolutely necessary. Frequently used keys are refreshed, and lookup misses are also cached; removing the need for performing API/database calls if we know we don't have the data we're looking for. == Performance & Models The new importer in various places uses raw INSERT statements (as generated by `Gitlab::Database.bulk_insert`) instead of using Rails models. This allows us to bypass any validations and callbacks, drastically reducing the number of SQL queries and Gitaly RPC calls necessary to import projects. To ensure the code produces valid data the corresponding tests check if the produced rows are valid according to the model validation rules.
Diffstat (limited to 'spec/lib/gitlab/legacy_github_import/issue_formatter_spec.rb')
-rw-r--r--spec/lib/gitlab/legacy_github_import/issue_formatter_spec.rb195
1 files changed, 195 insertions, 0 deletions
diff --git a/spec/lib/gitlab/legacy_github_import/issue_formatter_spec.rb b/spec/lib/gitlab/legacy_github_import/issue_formatter_spec.rb
new file mode 100644
index 00000000000..1a4d5dbfb70
--- /dev/null
+++ b/spec/lib/gitlab/legacy_github_import/issue_formatter_spec.rb
@@ -0,0 +1,195 @@
+require 'spec_helper'
+
+describe Gitlab::LegacyGithubImport::IssueFormatter do
+ let(:client) { double }
+ let!(:project) { create(:project, namespace: create(:namespace, path: 'octocat')) }
+ let(:octocat) { double(id: 123456, login: 'octocat', email: 'octocat@example.com') }
+ let(:created_at) { DateTime.strptime('2011-01-26T19:01:12Z') }
+ let(:updated_at) { DateTime.strptime('2011-01-27T19:01:12Z') }
+
+ let(:base_data) do
+ {
+ 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
+ }
+ end
+
+ subject(:issue) { described_class.new(project, raw_data, client) }
+
+ before do
+ allow(client).to receive(:user).and_return(octocat)
+ end
+
+ shared_examples 'Gitlab::LegacyGithubImport::IssueFormatter#attributes' do
+ context 'when issue is open' do
+ let(:raw_data) { double(base_data.merge(state: 'open')) }
+
+ it 'returns formatted attributes' do
+ expected = {
+ iid: 1347,
+ project: project,
+ milestone: nil,
+ title: 'Found a bug',
+ description: "*Created by: octocat*\n\nI'm having a problem with this.",
+ state: 'opened',
+ author_id: project.creator_id,
+ assignee_ids: [],
+ created_at: created_at,
+ updated_at: updated_at
+ }
+
+ expect(issue.attributes).to eq(expected)
+ end
+ end
+
+ context 'when issue is closed' do
+ let(:raw_data) { double(base_data.merge(state: 'closed')) }
+
+ it 'returns formatted attributes' do
+ expected = {
+ iid: 1347,
+ project: project,
+ milestone: nil,
+ title: 'Found a bug',
+ description: "*Created by: octocat*\n\nI'm having a problem with this.",
+ state: 'closed',
+ author_id: project.creator_id,
+ assignee_ids: [],
+ created_at: created_at,
+ updated_at: updated_at
+ }
+
+ expect(issue.attributes).to eq(expected)
+ end
+ end
+
+ context 'when it is assigned to someone' do
+ let(:raw_data) { double(base_data.merge(assignee: octocat)) }
+
+ it 'returns nil as assignee_id when is not a GitLab user' do
+ expect(issue.attributes.fetch(:assignee_ids)).to be_empty
+ end
+
+ it 'returns GitLab user id associated with GitHub id as assignee_id' do
+ gl_user = create(:omniauth_user, extern_uid: octocat.id, provider: 'github')
+
+ expect(issue.attributes.fetch(:assignee_ids)).to eq [gl_user.id]
+ end
+
+ it 'returns GitLab user id associated with GitHub email as assignee_id' do
+ gl_user = create(:user, email: octocat.email)
+
+ expect(issue.attributes.fetch(:assignee_ids)).to eq [gl_user.id]
+ end
+ end
+
+ context 'when it has a milestone' do
+ let(:milestone) { double(id: 42, number: 42) }
+ let(:raw_data) { double(base_data.merge(milestone: milestone)) }
+
+ it 'returns nil when milestone does not exist' do
+ expect(issue.attributes.fetch(:milestone)).to be_nil
+ end
+
+ it 'returns milestone when it exists' do
+ milestone = create(:milestone, project: project, iid: 42)
+
+ expect(issue.attributes.fetch(:milestone)).to eq milestone
+ end
+ end
+
+ context 'when author is a GitLab user' do
+ let(:raw_data) { double(base_data.merge(user: octocat)) }
+
+ it 'returns project creator_id as author_id when is not a GitLab user' do
+ expect(issue.attributes.fetch(:author_id)).to eq project.creator_id
+ end
+
+ it 'returns GitLab user id associated with GitHub id as author_id' do
+ gl_user = create(:omniauth_user, extern_uid: octocat.id, provider: 'github')
+
+ expect(issue.attributes.fetch(:author_id)).to eq gl_user.id
+ end
+
+ it 'returns GitLab user id associated with GitHub email as author_id' do
+ gl_user = create(:user, email: octocat.email)
+
+ expect(issue.attributes.fetch(:author_id)).to eq gl_user.id
+ end
+
+ it 'returns description without created at tag line' do
+ create(:omniauth_user, extern_uid: octocat.id, provider: 'github')
+
+ expect(issue.attributes.fetch(:description)).to eq("I'm having a problem with this.")
+ end
+ end
+ end
+
+ shared_examples 'Gitlab::LegacyGithubImport::IssueFormatter#number' do
+ let(:raw_data) { double(base_data.merge(number: 1347)) }
+
+ it 'returns issue number' do
+ expect(issue.number).to eq 1347
+ end
+ end
+
+ context 'when importing a GitHub project' do
+ it_behaves_like 'Gitlab::LegacyGithubImport::IssueFormatter#attributes'
+ it_behaves_like 'Gitlab::LegacyGithubImport::IssueFormatter#number'
+ end
+
+ context 'when importing a Gitea project' do
+ before do
+ project.update(import_type: 'gitea')
+ end
+
+ it_behaves_like 'Gitlab::LegacyGithubImport::IssueFormatter#attributes'
+ it_behaves_like 'Gitlab::LegacyGithubImport::IssueFormatter#number'
+ end
+
+ describe '#has_comments?' do
+ context 'when number of comments is greater than zero' do
+ let(:raw_data) { double(base_data.merge(comments: 1)) }
+
+ it 'returns true' do
+ expect(issue.has_comments?).to eq true
+ end
+ end
+
+ context 'when number of comments is equal to zero' do
+ let(:raw_data) { double(base_data.merge(comments: 0)) }
+
+ it 'returns false' do
+ expect(issue.has_comments?).to eq false
+ end
+ end
+ end
+
+ describe '#pull_request?' do
+ context 'when mention a pull request' do
+ let(:raw_data) { double(base_data.merge(pull_request: double)) }
+
+ it 'returns true' do
+ expect(issue.pull_request?).to eq true
+ end
+ end
+
+ context 'when does not mention a pull request' do
+ let(:raw_data) { double(base_data.merge(pull_request: nil)) }
+
+ it 'returns false' do
+ expect(issue.pull_request?).to eq false
+ end
+ end
+ end
+end