2017-04-24 19:28:06 -04:00
|
|
|
require_relative 'error'
|
2017-05-11 16:29:42 -04:00
|
|
|
|
2017-04-11 21:36:33 -04:00
|
|
|
module Github
|
|
|
|
class Import
|
2017-04-19 21:54:09 -04:00
|
|
|
include Gitlab::ShellAdapter
|
2017-04-19 21:01:14 -04:00
|
|
|
|
2017-04-11 21:36:33 -04:00
|
|
|
class MergeRequest < ::MergeRequest
|
|
|
|
self.table_name = 'merge_requests'
|
|
|
|
|
2017-05-11 16:30:56 -04:00
|
|
|
self.reset_callbacks :create
|
2017-04-11 21:36:33 -04:00
|
|
|
self.reset_callbacks :save
|
|
|
|
self.reset_callbacks :commit
|
|
|
|
self.reset_callbacks :update
|
|
|
|
self.reset_callbacks :validate
|
|
|
|
end
|
|
|
|
|
2017-04-12 21:46:31 -04:00
|
|
|
class Issue < ::Issue
|
|
|
|
self.table_name = 'issues'
|
|
|
|
|
|
|
|
self.reset_callbacks :save
|
2017-05-11 16:30:56 -04:00
|
|
|
self.reset_callbacks :create
|
2017-04-12 21:46:31 -04:00
|
|
|
self.reset_callbacks :commit
|
|
|
|
self.reset_callbacks :update
|
|
|
|
self.reset_callbacks :validate
|
|
|
|
end
|
|
|
|
|
|
|
|
class Note < ::Note
|
|
|
|
self.table_name = 'notes'
|
|
|
|
|
|
|
|
self.reset_callbacks :save
|
|
|
|
self.reset_callbacks :commit
|
|
|
|
self.reset_callbacks :update
|
|
|
|
self.reset_callbacks :validate
|
|
|
|
end
|
|
|
|
|
2017-04-25 16:05:40 -04:00
|
|
|
class LegacyDiffNote < ::LegacyDiffNote
|
|
|
|
self.table_name = 'notes'
|
|
|
|
|
|
|
|
self.reset_callbacks :commit
|
|
|
|
self.reset_callbacks :update
|
|
|
|
self.reset_callbacks :validate
|
|
|
|
end
|
|
|
|
|
2017-04-25 22:47:49 -04:00
|
|
|
attr_reader :project, :repository, :repo, :options, :errors, :cached, :verbose
|
2017-04-11 21:36:33 -04:00
|
|
|
|
2017-04-19 19:04:58 -04:00
|
|
|
def initialize(project, options)
|
2017-04-11 21:36:33 -04:00
|
|
|
@project = project
|
|
|
|
@repository = project.repository
|
2017-04-19 20:03:46 -04:00
|
|
|
@repo = project.import_source
|
2017-04-19 19:04:58 -04:00
|
|
|
@options = options
|
2017-04-25 22:47:49 -04:00
|
|
|
@verbose = options.fetch(:verbose, false)
|
2017-04-24 19:00:11 -04:00
|
|
|
@cached = Hash.new { |hash, key| hash[key] = Hash.new }
|
2017-04-11 21:36:33 -04:00
|
|
|
@errors = []
|
|
|
|
end
|
|
|
|
|
2017-04-25 22:47:49 -04:00
|
|
|
# rubocop: disable Rails/Output
|
2017-04-19 20:03:46 -04:00
|
|
|
def execute
|
2017-04-25 22:47:49 -04:00
|
|
|
puts 'Fetching repository...'.color(:aqua) if verbose
|
2017-04-19 20:03:46 -04:00
|
|
|
fetch_repository
|
2017-04-25 22:47:49 -04:00
|
|
|
puts 'Fetching labels...'.color(:aqua) if verbose
|
2017-04-19 20:03:46 -04:00
|
|
|
fetch_labels
|
2017-04-25 22:47:49 -04:00
|
|
|
puts 'Fetching milestones...'.color(:aqua) if verbose
|
2017-04-19 20:03:46 -04:00
|
|
|
fetch_milestones
|
2017-04-25 22:47:49 -04:00
|
|
|
puts 'Fetching pull requests...'.color(:aqua) if verbose
|
2017-04-19 20:03:46 -04:00
|
|
|
fetch_pull_requests
|
2017-04-25 22:47:49 -04:00
|
|
|
puts 'Fetching issues...'.color(:aqua) if verbose
|
2017-04-19 20:03:46 -04:00
|
|
|
fetch_issues
|
2017-04-25 22:47:49 -04:00
|
|
|
puts 'Cloning wiki repository...'.color(:aqua) if verbose
|
2017-04-19 21:01:14 -04:00
|
|
|
fetch_wiki_repository
|
2017-04-25 22:47:49 -04:00
|
|
|
puts 'Expiring repository cache...'.color(:aqua) if verbose
|
2017-04-19 20:03:46 -04:00
|
|
|
expire_repository_cache
|
|
|
|
|
2017-04-24 19:28:06 -04:00
|
|
|
true
|
|
|
|
rescue Github::RepositoryFetchError
|
|
|
|
false
|
|
|
|
ensure
|
|
|
|
keep_track_of_errors
|
2017-04-19 20:03:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def fetch_repository
|
2017-04-11 21:36:33 -04:00
|
|
|
begin
|
2017-04-25 14:58:17 -04:00
|
|
|
project.create_repository unless project.repository.exists?
|
2017-05-11 16:29:42 -04:00
|
|
|
project.repository.add_remote('github', "https://#{options.fetch(:token)}@github.com/#{repo}.git")
|
2017-04-11 21:36:33 -04:00
|
|
|
project.repository.set_remote_as_mirror('github')
|
|
|
|
project.repository.fetch_remote('github', forced: true)
|
|
|
|
rescue Gitlab::Shell::Error => e
|
2017-04-19 20:03:46 -04:00
|
|
|
error(:project, "https://github.com/#{repo}.git", e.message)
|
2017-04-24 19:28:06 -04:00
|
|
|
raise Github::RepositoryFetchError
|
2017-04-11 21:36:33 -04:00
|
|
|
end
|
2017-04-19 20:03:46 -04:00
|
|
|
end
|
2017-04-11 21:36:33 -04:00
|
|
|
|
2017-04-19 21:01:14 -04:00
|
|
|
def fetch_wiki_repository
|
2017-06-12 15:37:18 -04:00
|
|
|
wiki_url = "https://#{options.fetch(:token)}@github.com/#{repo}.wiki.git"
|
2017-04-19 21:01:14 -04:00
|
|
|
wiki_path = "#{project.path_with_namespace}.wiki"
|
|
|
|
|
|
|
|
unless project.wiki.repository_exists?
|
|
|
|
gitlab_shell.import_repository(project.repository_storage_path, wiki_path, wiki_url)
|
|
|
|
end
|
|
|
|
rescue Gitlab::Shell::Error => e
|
|
|
|
# GitHub error message when the wiki repo has not been created,
|
|
|
|
# this means that repo has wiki enabled, but have no pages. So,
|
|
|
|
# we can skip the import.
|
|
|
|
if e.message !~ /repository not exported/
|
|
|
|
errors(:wiki, wiki_url, e.message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-19 20:03:46 -04:00
|
|
|
def fetch_labels
|
|
|
|
url = "/repos/#{repo}/labels"
|
2017-04-12 21:46:31 -04:00
|
|
|
|
2017-04-19 19:45:49 -04:00
|
|
|
while url
|
2017-04-19 19:04:58 -04:00
|
|
|
response = Github::Client.new(options).get(url)
|
2017-04-12 21:46:31 -04:00
|
|
|
|
|
|
|
response.body.each do |raw|
|
|
|
|
begin
|
2017-04-24 20:03:42 -04:00
|
|
|
representation = Github::Representation::Label.new(raw)
|
2017-04-12 21:46:31 -04:00
|
|
|
|
2017-04-24 20:03:42 -04:00
|
|
|
label = project.labels.find_or_create_by!(title: representation.title) do |label|
|
|
|
|
label.color = representation.color
|
|
|
|
end
|
|
|
|
|
|
|
|
cached[:label_ids][label.title] = label.id
|
2017-04-12 21:46:31 -04:00
|
|
|
rescue => e
|
2017-04-25 14:57:50 -04:00
|
|
|
error(:label, representation.url, e.message)
|
2017-04-12 21:46:31 -04:00
|
|
|
end
|
2017-04-11 21:36:33 -04:00
|
|
|
end
|
2017-04-12 21:46:31 -04:00
|
|
|
|
2017-04-19 19:45:49 -04:00
|
|
|
url = response.rels[:next]
|
2017-04-11 21:36:33 -04:00
|
|
|
end
|
2017-04-19 20:03:46 -04:00
|
|
|
end
|
2017-04-13 17:21:03 -04:00
|
|
|
|
2017-04-19 20:03:46 -04:00
|
|
|
def fetch_milestones
|
|
|
|
url = "/repos/#{repo}/milestones"
|
2017-04-12 21:46:31 -04:00
|
|
|
|
2017-04-19 19:45:49 -04:00
|
|
|
while url
|
2017-04-19 19:04:58 -04:00
|
|
|
response = Github::Client.new(options).get(url, state: :all)
|
2017-04-12 21:46:31 -04:00
|
|
|
|
|
|
|
response.body.each do |raw|
|
|
|
|
begin
|
|
|
|
milestone = Github::Representation::Milestone.new(raw)
|
|
|
|
next if project.milestones.where(iid: milestone.iid).exists?
|
|
|
|
|
|
|
|
project.milestones.create!(
|
|
|
|
iid: milestone.iid,
|
|
|
|
title: milestone.title,
|
|
|
|
description: milestone.description,
|
|
|
|
due_date: milestone.due_date,
|
|
|
|
state: milestone.state,
|
|
|
|
created_at: milestone.created_at,
|
|
|
|
updated_at: milestone.updated_at
|
|
|
|
)
|
|
|
|
rescue => e
|
|
|
|
error(:milestone, milestone.url, e.message)
|
|
|
|
end
|
2017-04-11 21:36:33 -04:00
|
|
|
end
|
2017-04-12 21:46:31 -04:00
|
|
|
|
2017-04-19 19:45:49 -04:00
|
|
|
url = response.rels[:next]
|
2017-04-11 21:36:33 -04:00
|
|
|
end
|
2017-04-19 20:03:46 -04:00
|
|
|
end
|
2017-04-11 21:36:33 -04:00
|
|
|
|
2017-04-19 20:03:46 -04:00
|
|
|
def fetch_pull_requests
|
|
|
|
url = "/repos/#{repo}/pulls"
|
2017-04-12 21:46:31 -04:00
|
|
|
|
2017-04-19 19:45:49 -04:00
|
|
|
while url
|
2017-04-19 19:04:58 -04:00
|
|
|
response = Github::Client.new(options).get(url, state: :all, sort: :created, direction: :asc)
|
2017-04-12 21:46:31 -04:00
|
|
|
|
|
|
|
response.body.each do |raw|
|
2017-04-24 20:13:51 -04:00
|
|
|
pull_request = Github::Representation::PullRequest.new(raw, options.merge(project: project))
|
2017-04-12 21:46:31 -04:00
|
|
|
merge_request = MergeRequest.find_or_initialize_by(iid: pull_request.iid, source_project_id: project.id)
|
|
|
|
next unless merge_request.new_record? && pull_request.valid?
|
|
|
|
|
|
|
|
begin
|
2017-06-20 20:39:51 -04:00
|
|
|
pull_request.restore_branches!
|
2017-04-12 21:46:31 -04:00
|
|
|
|
2017-04-26 13:35:34 -04:00
|
|
|
author_id = user_id(pull_request.author, project.creator_id)
|
|
|
|
description = format_description(pull_request.description, pull_request.author)
|
|
|
|
|
|
|
|
merge_request.attributes = {
|
|
|
|
iid: pull_request.iid,
|
|
|
|
title: pull_request.title,
|
|
|
|
description: description,
|
|
|
|
source_project: pull_request.source_project,
|
|
|
|
source_branch: pull_request.source_branch_name,
|
|
|
|
source_branch_sha: pull_request.source_branch_sha,
|
|
|
|
target_project: pull_request.target_project,
|
|
|
|
target_branch: pull_request.target_branch_name,
|
|
|
|
target_branch_sha: pull_request.target_branch_sha,
|
|
|
|
state: pull_request.state,
|
|
|
|
milestone_id: milestone_id(pull_request.milestone),
|
|
|
|
author_id: author_id,
|
|
|
|
assignee_id: user_id(pull_request.assignee),
|
|
|
|
created_at: pull_request.created_at,
|
|
|
|
updated_at: pull_request.updated_at
|
|
|
|
}
|
2017-04-12 21:46:31 -04:00
|
|
|
|
2017-04-26 13:35:34 -04:00
|
|
|
merge_request.save!(validate: false)
|
2017-04-12 21:46:31 -04:00
|
|
|
merge_request.merge_request_diffs.create
|
2017-04-13 16:05:39 -04:00
|
|
|
|
|
|
|
# Fetch review comments
|
2017-04-19 20:03:46 -04:00
|
|
|
review_comments_url = "/repos/#{repo}/pulls/#{pull_request.iid}/comments"
|
2017-04-25 16:05:40 -04:00
|
|
|
fetch_comments(merge_request, :review_comment, review_comments_url, LegacyDiffNote)
|
2017-04-13 16:05:39 -04:00
|
|
|
|
|
|
|
# Fetch comments
|
2017-04-19 20:03:46 -04:00
|
|
|
comments_url = "/repos/#{repo}/issues/#{pull_request.iid}/comments"
|
2017-04-19 18:41:17 -04:00
|
|
|
fetch_comments(merge_request, :comment, comments_url)
|
2017-04-12 21:46:31 -04:00
|
|
|
rescue => e
|
2017-04-19 21:01:14 -04:00
|
|
|
error(:pull_request, pull_request.url, e.message)
|
2017-04-12 21:46:31 -04:00
|
|
|
ensure
|
2017-06-20 20:39:51 -04:00
|
|
|
pull_request.remove_restored_branches!
|
2017-04-11 21:36:33 -04:00
|
|
|
end
|
2017-04-12 21:46:31 -04:00
|
|
|
end
|
|
|
|
|
2017-04-19 19:45:49 -04:00
|
|
|
url = response.rels[:next]
|
2017-04-12 21:46:31 -04:00
|
|
|
end
|
2017-04-19 20:03:46 -04:00
|
|
|
end
|
2017-04-11 21:36:33 -04:00
|
|
|
|
2017-04-19 20:03:46 -04:00
|
|
|
def fetch_issues
|
|
|
|
url = "/repos/#{repo}/issues"
|
2017-04-12 21:46:31 -04:00
|
|
|
|
2017-04-19 19:45:49 -04:00
|
|
|
while url
|
2017-04-19 19:04:58 -04:00
|
|
|
response = Github::Client.new(options).get(url, state: :all, sort: :created, direction: :asc)
|
2017-04-12 21:46:31 -04:00
|
|
|
|
|
|
|
response.body.each do |raw|
|
2017-04-19 19:04:58 -04:00
|
|
|
representation = Github::Representation::Issue.new(raw, options)
|
2017-04-12 21:46:31 -04:00
|
|
|
|
|
|
|
begin
|
2017-04-13 17:21:03 -04:00
|
|
|
# Every pull request is an issue, but not every issue
|
|
|
|
# is a pull request. For this reason, "shared" actions
|
|
|
|
# for both features, like manipulating assignees, labels
|
|
|
|
# and milestones, are provided within the Issues API.
|
|
|
|
if representation.pull_request?
|
|
|
|
next unless representation.has_labels?
|
|
|
|
|
|
|
|
merge_request = MergeRequest.find_by!(target_project_id: project.id, iid: representation.iid)
|
|
|
|
merge_request.update_attribute(:label_ids, label_ids(representation.labels))
|
|
|
|
else
|
|
|
|
next if Issue.where(iid: representation.iid, project_id: project.id).exists?
|
|
|
|
|
2017-04-19 19:39:04 -04:00
|
|
|
author_id = user_id(representation.author, project.creator_id)
|
2017-04-13 17:21:03 -04:00
|
|
|
issue = Issue.new
|
|
|
|
issue.iid = representation.iid
|
|
|
|
issue.project_id = project.id
|
|
|
|
issue.title = representation.title
|
2017-04-19 19:39:04 -04:00
|
|
|
issue.description = format_description(representation.description, representation.author)
|
2017-04-13 17:21:03 -04:00
|
|
|
issue.state = representation.state
|
|
|
|
issue.label_ids = label_ids(representation.labels)
|
|
|
|
issue.milestone_id = milestone_id(representation.milestone)
|
2017-04-19 19:39:04 -04:00
|
|
|
issue.author_id = author_id
|
2017-05-05 06:41:35 -04:00
|
|
|
issue.assignee_ids = [user_id(representation.assignee)]
|
2017-04-13 17:21:03 -04:00
|
|
|
issue.created_at = representation.created_at
|
|
|
|
issue.updated_at = representation.updated_at
|
2017-04-19 19:39:04 -04:00
|
|
|
issue.save!(validate: false)
|
2017-04-13 17:21:03 -04:00
|
|
|
|
2017-04-19 21:01:14 -04:00
|
|
|
# Fetch comments
|
2017-04-13 17:21:03 -04:00
|
|
|
if representation.has_comments?
|
2017-04-19 20:03:46 -04:00
|
|
|
comments_url = "/repos/#{repo}/issues/#{issue.iid}/comments"
|
2017-04-19 18:41:17 -04:00
|
|
|
fetch_comments(issue, :comment, comments_url)
|
2017-04-13 16:31:29 -04:00
|
|
|
end
|
|
|
|
end
|
2017-04-12 21:46:31 -04:00
|
|
|
rescue => e
|
|
|
|
error(:issue, representation.url, e.message)
|
|
|
|
end
|
2017-04-11 21:36:33 -04:00
|
|
|
end
|
2017-04-12 21:46:31 -04:00
|
|
|
|
2017-04-19 19:45:49 -04:00
|
|
|
url = response.rels[:next]
|
2017-04-11 21:36:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-25 16:05:40 -04:00
|
|
|
def fetch_comments(noteable, type, url, klass = Note)
|
2017-04-19 19:45:49 -04:00
|
|
|
while url
|
2017-04-19 19:04:58 -04:00
|
|
|
comments = Github::Client.new(options).get(url)
|
2017-04-19 18:41:17 -04:00
|
|
|
|
|
|
|
ActiveRecord::Base.no_touching do
|
|
|
|
comments.body.each do |raw|
|
|
|
|
begin
|
2017-04-19 19:39:04 -04:00
|
|
|
representation = Github::Representation::Comment.new(raw, options)
|
|
|
|
author_id = user_id(representation.author, project.creator_id)
|
2017-04-19 18:41:17 -04:00
|
|
|
|
2017-04-25 16:05:40 -04:00
|
|
|
note = klass.new
|
2017-04-19 18:41:17 -04:00
|
|
|
note.project_id = project.id
|
|
|
|
note.noteable = noteable
|
2017-04-19 19:39:04 -04:00
|
|
|
note.note = format_description(representation.note, representation.author)
|
2017-04-19 18:41:17 -04:00
|
|
|
note.commit_id = representation.commit_id
|
|
|
|
note.line_code = representation.line_code
|
2017-04-19 19:39:04 -04:00
|
|
|
note.author_id = author_id
|
2017-04-19 18:41:17 -04:00
|
|
|
note.created_at = representation.created_at
|
|
|
|
note.updated_at = representation.updated_at
|
|
|
|
note.save!(validate: false)
|
|
|
|
rescue => e
|
|
|
|
error(type, representation.url, e.message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-19 19:45:49 -04:00
|
|
|
url = comments.rels[:next]
|
2017-04-19 18:41:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-19 20:18:11 -04:00
|
|
|
def fetch_releases
|
|
|
|
url = "/repos/#{repo}/releases"
|
|
|
|
|
|
|
|
while url
|
|
|
|
response = Github::Client.new(options).get(url)
|
|
|
|
|
|
|
|
response.body.each do |raw|
|
|
|
|
representation = Github::Representation::Release.new(raw)
|
|
|
|
next unless representation.valid?
|
|
|
|
|
|
|
|
release = ::Release.find_or_initialize_by(project_id: project.id, tag: representation.tag)
|
|
|
|
next unless relese.new_record?
|
|
|
|
|
|
|
|
begin
|
|
|
|
release.description = representation.description
|
|
|
|
release.created_at = representation.created_at
|
|
|
|
release.updated_at = representation.updated_at
|
|
|
|
release.save!(validate: false)
|
|
|
|
rescue => e
|
|
|
|
error(:release, representation.url, e.message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
url = response.rels[:next]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-24 22:41:46 -04:00
|
|
|
def label_ids(labels)
|
|
|
|
labels.map { |attrs| cached[:label_ids][attrs.fetch('name')] }.compact
|
2017-04-13 17:21:03 -04:00
|
|
|
end
|
|
|
|
|
2017-04-11 21:36:33 -04:00
|
|
|
def milestone_id(milestone)
|
|
|
|
return unless milestone.present?
|
|
|
|
|
|
|
|
project.milestones.select(:id).find_by(iid: milestone.iid)&.id
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_id(user, fallback_id = nil)
|
|
|
|
return unless user.present?
|
2017-04-24 19:00:11 -04:00
|
|
|
return cached[:user_ids][user.id] if cached[:user_ids].key?(user.id)
|
2017-04-11 21:36:33 -04:00
|
|
|
|
2017-04-24 17:42:50 -04:00
|
|
|
gitlab_user_id = user_id_by_external_uid(user.id) || user_id_by_email(user.email)
|
2017-04-19 19:39:04 -04:00
|
|
|
|
2017-04-24 19:00:11 -04:00
|
|
|
cached[:gitlab_user_ids][user.id] = gitlab_user_id.present?
|
|
|
|
cached[:user_ids][user.id] = gitlab_user_id || fallback_id
|
2017-04-11 21:36:33 -04:00
|
|
|
end
|
|
|
|
|
2017-04-24 17:42:50 -04:00
|
|
|
def user_id_by_email(email)
|
2017-04-11 21:36:33 -04:00
|
|
|
return nil unless email
|
|
|
|
|
|
|
|
::User.find_by_any_email(email)&.id
|
|
|
|
end
|
|
|
|
|
2017-04-24 17:42:50 -04:00
|
|
|
def user_id_by_external_uid(id)
|
2017-04-11 21:36:33 -04:00
|
|
|
return nil unless id
|
|
|
|
|
|
|
|
::User.select(:id)
|
|
|
|
.joins(:identities)
|
2017-04-24 19:45:51 -04:00
|
|
|
.merge(::Identity.where(provider: :github, extern_uid: id))
|
2017-04-11 21:36:33 -04:00
|
|
|
.first&.id
|
|
|
|
end
|
|
|
|
|
2017-04-19 19:39:04 -04:00
|
|
|
def format_description(body, author)
|
2017-04-24 19:00:11 -04:00
|
|
|
return body if cached[:gitlab_user_ids][author.id]
|
2017-04-19 19:39:04 -04:00
|
|
|
|
|
|
|
"*Created by: #{author.username}*\n\n#{body}"
|
|
|
|
end
|
|
|
|
|
2017-04-19 20:03:46 -04:00
|
|
|
def expire_repository_cache
|
|
|
|
repository.expire_content_cache
|
|
|
|
end
|
|
|
|
|
2017-04-24 19:28:06 -04:00
|
|
|
def keep_track_of_errors
|
2017-04-19 21:01:37 -04:00
|
|
|
return unless errors.any?
|
|
|
|
|
|
|
|
project.update_column(:import_error, {
|
|
|
|
message: 'The remote data could not be fully imported.',
|
|
|
|
errors: errors
|
|
|
|
}.to_json)
|
|
|
|
end
|
|
|
|
|
2017-04-11 21:36:33 -04:00
|
|
|
def error(type, url, message)
|
|
|
|
errors << { type: type, url: Gitlab::UrlSanitizer.sanitize(url), error: message }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|