debian-mirror-gitlab/lib/gitlab/gitlab_import/importer.rb

66 lines
2 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2015-04-26 12:48:37 +05:30
module Gitlab
module GitlabImport
class Importer
attr_reader :project, :client
def initialize(project)
@project = project
import_data = project.import_data
if import_data && import_data.credentials && import_data.credentials[:password]
@client = Client.new(import_data.credentials[:password])
2016-06-02 11:05:42 +05:30
@formatter = Gitlab::ImportFormatter.new
else
raise Projects::ImportService::Error, "Unable to find project import data credentials for project ID: #{@project.id}"
end
2015-04-26 12:48:37 +05:30
end
def execute
2016-08-24 12:49:21 +05:30
ActiveRecord::Base.no_touching do
project_identifier = CGI.escape(project.import_source)
# Issues && Comments
issues = client.issues(project_identifier)
issues.each do |issue|
2019-02-15 15:39:39 +05:30
body = [@formatter.author_line(issue["author"]["name"])]
body << issue["description"]
2016-08-24 12:49:21 +05:30
2018-11-08 19:23:39 +05:30
comments = client.issue_comments(project_identifier, issue["iid"])
2016-08-24 12:49:21 +05:30
if comments.any?
2019-02-15 15:39:39 +05:30
body << @formatter.comments_header
2016-08-24 12:49:21 +05:30
end
comments.each do |comment|
2019-02-15 15:39:39 +05:30
body << @formatter.comment(comment["author"]["name"], comment["created_at"], comment["body"])
2016-08-24 12:49:21 +05:30
end
project.issues.create!(
iid: issue["iid"],
2019-02-15 15:39:39 +05:30
description: body.join,
2016-08-24 12:49:21 +05:30
title: issue["title"],
state: issue["state"],
updated_at: issue["updated_at"],
2016-09-29 09:46:39 +05:30
author_id: gitlab_user_id(project, issue["author"]["id"]),
confidential: issue["confidential"]
2016-08-24 12:49:21 +05:30
)
2015-04-26 12:48:37 +05:30
end
end
2015-09-25 12:07:36 +05:30
2015-04-26 12:48:37 +05:30
true
end
private
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2016-09-29 09:46:39 +05:30
def gitlab_user_id(project, gitlab_id)
2021-09-30 23:02:18 +05:30
user_id = User.by_provider_and_extern_uid(:gitlab, gitlab_id).select(:id).first&.id
user_id || project.creator_id
2015-04-26 12:48:37 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2015-04-26 12:48:37 +05:30
end
end
end