Commit 6bbe2f11 authored by Valery Sizov's avatar Valery Sizov

BB importer: More advanced error handling

parent 468d575f
...@@ -6,24 +6,34 @@ module Gitlab ...@@ -6,24 +6,34 @@ module Gitlab
{ title: 'proposal', color: '#69D100' }, { title: 'proposal', color: '#69D100' },
{ title: 'task', color: '#7F8C8D' }].freeze { title: 'task', color: '#7F8C8D' }].freeze
attr_reader :project, :client attr_reader :project, :client, :errors
def initialize(project) def initialize(project)
@project = project @project = project
@client = Bitbucket::Client.new(project.import_data.credentials) @client = Bitbucket::Client.new(project.import_data.credentials)
@formatter = Gitlab::ImportFormatter.new @formatter = Gitlab::ImportFormatter.new
@labels = {} @labels = {}
@errors = []
end end
def execute def execute
import_issues import_issues
import_pull_requests import_pull_requests
handle_errors
true true
end end
private private
def handle_errors
return unless errors.any?
project.update_column(:import_error, {
message: 'The remote data could not be fully imported.',
errors: errors
}.to_json)
end
def gitlab_user_id(project, username) def gitlab_user_id(project, username)
if username if username
user = find_user(username) user = find_user(username)
...@@ -51,21 +61,25 @@ module Gitlab ...@@ -51,21 +61,25 @@ module Gitlab
create_labels create_labels
client.issues(repo).each do |issue| client.issues(repo).each do |issue|
description = '' begin
description += @formatter.author_line(issue.author) unless existing_gitlab_user?(issue.author) description = ''
description += issue.description description += @formatter.author_line(issue.author) unless existing_gitlab_user?(issue.author)
description += issue.description
label_name = issue.kind
label_name = issue.kind
issue = project.issues.create(
iid: issue.iid, issue = project.issues.create!(
title: issue.title, iid: issue.iid,
description: description, title: issue.title,
state: issue.state, description: description,
author_id: gitlab_user_id(project, issue.author), state: issue.state,
created_at: issue.created_at, author_id: gitlab_user_id(project, issue.author),
updated_at: issue.updated_at created_at: issue.created_at,
) updated_at: issue.updated_at
)
rescue StandardError => e
errors << { type: :issue, iid: issue.iid, errors: e.message }
end
issue.labels << @labels[label_name] issue.labels << @labels[label_name]
...@@ -82,18 +96,20 @@ module Gitlab ...@@ -82,18 +96,20 @@ module Gitlab
note += @formatter.author_line(comment.author) unless existing_gitlab_user?(comment.author) note += @formatter.author_line(comment.author) unless existing_gitlab_user?(comment.author)
note += comment.note note += comment.note
issue.notes.create!( begin
project: project, issue.notes.create!(
note: note, project: project,
author_id: gitlab_user_id(project, comment.author), note: note,
created_at: comment.created_at, author_id: gitlab_user_id(project, comment.author),
updated_at: comment.updated_at created_at: comment.created_at,
) updated_at: comment.updated_at
)
rescue StandardError => e
errors << { type: :issue_comment, iid: issue.iid, errors: e.message }
end
end end
end end
end end
rescue ActiveRecord::RecordInvalid => e
Rails.logger.error("Bitbucket importer ERROR in #{project.path_with_namespace}: Couldn't import record properly #{e.message}")
end end
def create_labels def create_labels
...@@ -129,8 +145,8 @@ module Gitlab ...@@ -129,8 +145,8 @@ module Gitlab
) )
import_pull_request_comments(pull_request, merge_request) if merge_request.persisted? import_pull_request_comments(pull_request, merge_request) if merge_request.persisted?
rescue ActiveRecord::RecordInvalid rescue StandardError => e
Rails.logger.error("Bitbucket importer ERROR in #{project.path_with_namespace}: Invalid pull request #{e.message}") errors << { type: :pull_request, iid: pull_request.iid, errors: e.message }
end end
end end
end end
...@@ -169,9 +185,8 @@ module Gitlab ...@@ -169,9 +185,8 @@ module Gitlab
type: 'DiffNote') type: 'DiffNote')
merge_request.notes.create!(attributes) merge_request.notes.create!(attributes)
rescue ActiveRecord::RecordInvalid => e rescue StandardError => e
Rails.logger.error("Bitbucket importer ERROR in #{project.path_with_namespace}: Invalid pull request comment #{e.message}") errors << { type: :pull_request, iid: comment.iid, errors: e.message }
nil
end end
end end
end end
...@@ -192,9 +207,8 @@ module Gitlab ...@@ -192,9 +207,8 @@ module Gitlab
pr_comments.each do |comment| pr_comments.each do |comment|
begin begin
merge_request.notes.create!(pull_request_comment_attributes(comment)) merge_request.notes.create!(pull_request_comment_attributes(comment))
rescue ActiveRecord::RecordInvalid => e rescue StandardError => e
Rails.logger.error("Bitbucket importer ERROR in #{project.path_with_namespace}: Invalid standalone pull request comment #{e.message}") errors << { type: :pull_request, iid: comment.iid, errors: e.message }
nil
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment