2017-04-11 21:36:33 -04:00
|
|
|
module Github
|
|
|
|
class Import
|
|
|
|
class MergeRequest < ::MergeRequest
|
|
|
|
self.table_name = 'merge_requests'
|
|
|
|
|
|
|
|
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
|
|
|
|
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-19 20:03:46 -04:00
|
|
|
attr_reader :project, :repository, :repo, :options, :errors,
|
|
|
|
:cached_label_ids, :cached_gitlab_users, :cached_user_ids
|
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-13 17:21:03 -04:00
|
|
|
@cached_label_ids = {}
|
2017-04-11 21:36:33 -04:00
|
|
|
@cached_user_ids = {}
|
2017-04-19 19:39:04 -04:00
|
|
|
@cached_gitlab_users = {}
|
2017-04-11 21:36:33 -04:00
|
|
|
@errors = []
|
|
|
|
end
|
|
|
|
|
2017-04-19 20:03:46 -04:00
|
|
|
def execute
|
|
|
|
fetch_repository
|
|
|
|
fetch_labels
|
|
|
|
fetch_milestones
|
|
|
|
fetch_pull_requests
|
|
|
|
fetch_issues
|
|
|
|
expire_repository_cache
|
|
|
|
|
|
|
|
errors
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def fetch_repository
|
2017-04-11 21:36:33 -04:00
|
|
|
begin
|
|
|
|
project.create_repository
|
2017-04-19 20:03:46 -04:00
|
|
|
project.repository.add_remote('github', "https://{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-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_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
|
|
|
|
label = Github::Representation::Label.new(raw)
|
|
|
|
next if project.labels.where(title: label.title).exists?
|
|
|
|
|
|
|
|
project.labels.create!(title: label.title, color: label.color)
|
|
|
|
rescue => e
|
|
|
|
error(:label, label.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-13 17:21:03 -04:00
|
|
|
# Cache labels
|
|
|
|
project.labels.select(:id, :title).find_each do |label|
|
|
|
|
@cached_label_ids[label.title] = label.id
|
|
|
|
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-19 19:04:58 -04:00
|
|
|
pull_request = Github::Representation::PullRequest.new(project, raw, options)
|
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
|
|
|
|
restore_source_branch(pull_request) unless pull_request.source_branch_exists?
|
|
|
|
restore_target_branch(pull_request) unless pull_request.target_branch_exists?
|
|
|
|
|
2017-04-19 19:39:04 -04:00
|
|
|
author_id = user_id(pull_request.author, project.creator_id)
|
2017-04-12 21:46:31 -04:00
|
|
|
merge_request.iid = pull_request.iid
|
|
|
|
merge_request.title = pull_request.title
|
2017-04-19 19:39:04 -04:00
|
|
|
merge_request.description = format_description(pull_request.description, pull_request.author)
|
2017-04-12 21:46:31 -04:00
|
|
|
merge_request.source_project = pull_request.source_project
|
|
|
|
merge_request.source_branch = pull_request.source_branch_name
|
|
|
|
merge_request.source_branch_sha = pull_request.source_branch_sha
|
|
|
|
merge_request.target_project = pull_request.target_project
|
|
|
|
merge_request.target_branch = pull_request.target_branch_name
|
|
|
|
merge_request.target_branch_sha = pull_request.target_branch_sha
|
|
|
|
merge_request.state = pull_request.state
|
|
|
|
merge_request.milestone_id = milestone_id(pull_request.milestone)
|
2017-04-19 19:39:04 -04:00
|
|
|
merge_request.author_id = author_id
|
2017-04-12 21:46:31 -04:00
|
|
|
merge_request.assignee_id = user_id(pull_request.assignee)
|
|
|
|
merge_request.created_at = pull_request.created_at
|
|
|
|
merge_request.updated_at = pull_request.updated_at
|
2017-04-13 16:05:39 -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-19 18:41:17 -04:00
|
|
|
fetch_comments(merge_request, :review_comment, review_comments_url)
|
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
|
|
|
|
ensure
|
|
|
|
clean_up_restored_branches(pull_request)
|
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-04-13 17:21:03 -04:00
|
|
|
issue.assignee_id = user_id(representation.assignee)
|
|
|
|
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
|
|
|
|
|
|
|
if representation.has_comments?
|
|
|
|
# Fetch 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-19 18:41:17 -04:00
|
|
|
def fetch_comments(noteable, type, url)
|
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
|
|
|
|
|
|
|
note = Note.new
|
|
|
|
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.type = representation.type
|
|
|
|
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-11 21:36:33 -04:00
|
|
|
def restore_source_branch(pull_request)
|
|
|
|
repository.create_branch(pull_request.source_branch_name, pull_request.source_branch_sha)
|
|
|
|
end
|
|
|
|
|
|
|
|
def restore_target_branch(pull_request)
|
|
|
|
repository.create_branch(pull_request.target_branch_name, pull_request.target_branch_sha)
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_branch(name)
|
|
|
|
repository.delete_branch(name)
|
|
|
|
rescue Rugged::ReferenceError
|
|
|
|
errors << { type: :branch, url: nil, error: "Could not clean up restored branch: #{name}" }
|
|
|
|
end
|
|
|
|
|
|
|
|
def clean_up_restored_branches(pull_request)
|
|
|
|
return if pull_request.opened?
|
|
|
|
|
|
|
|
remove_branch(pull_request.source_branch_name) unless pull_request.source_branch_exists?
|
|
|
|
remove_branch(pull_request.target_branch_name) unless pull_request.target_branch_exists?
|
|
|
|
end
|
|
|
|
|
2017-04-13 17:21:03 -04:00
|
|
|
def label_ids(issuable)
|
|
|
|
issuable.map { |attrs| cached_label_ids[attrs.fetch('name')] }.compact
|
|
|
|
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?
|
|
|
|
return cached_user_ids[user.id] if cached_user_ids.key?(user.id)
|
|
|
|
|
2017-04-19 19:39:04 -04:00
|
|
|
gitlab_user_id = find_by_external_uid(user.id) || find_by_email(user.email)
|
|
|
|
|
|
|
|
cached_gitlab_users[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
|
|
|
|
|
|
|
|
def find_by_email(email)
|
|
|
|
return nil unless email
|
|
|
|
|
|
|
|
::User.find_by_any_email(email)&.id
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_by_external_uid(id)
|
|
|
|
return nil unless id
|
|
|
|
|
|
|
|
identities = ::Identity.arel_table
|
|
|
|
|
|
|
|
::User.select(:id)
|
|
|
|
.joins(:identities)
|
|
|
|
.where(identities[:provider].eq(:github).and(identities[:extern_uid].eq(id)))
|
|
|
|
.first&.id
|
|
|
|
end
|
|
|
|
|
2017-04-19 19:39:04 -04:00
|
|
|
def format_description(body, author)
|
|
|
|
return body if cached_gitlab_users[author.id]
|
|
|
|
|
|
|
|
"*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-11 21:36:33 -04:00
|
|
|
def error(type, url, message)
|
|
|
|
errors << { type: type, url: Gitlab::UrlSanitizer.sanitize(url), error: message }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|