Commit cc4a39cc authored by Stan Hu's avatar Stan Hu

Reduce number of API requests and fix merge request events

parent 12b031ce
...@@ -100,13 +100,21 @@ module Gitlab ...@@ -100,13 +100,21 @@ module Gitlab
# 2. Retried import, repo is broken or not imported but +exists?+ still returns true # 2. Retried import, repo is broken or not imported but +exists?+ still returns true
project.repository.expire_content_cache if project.repository_exists? project.repository.expire_content_cache if project.repository_exists?
raise RuntimeError, e.message raise e.message
end end
def import_pull_requests def import_pull_requests
pull_requests = client.pull_requests(project_key, repository_slug) pull_requests = client.pull_requests(project_key, repository_slug)
pull_requests.each do |pull_request| pull_requests.each do |pull_request|
begin begin
import_bitbucket_pull_request(pull_request)
rescue StandardError => e
errors << { type: :pull_request, iid: pull_request.iid, errors: e.message, trace: e.backtrace.join("\n"), raw_response: pull_request.raw }
end
end
end
def import_bitbucket_pull_request(pull_request)
restore_branches(pull_request) restore_branches(pull_request)
description = '' description = ''
...@@ -139,20 +147,14 @@ module Gitlab ...@@ -139,20 +147,14 @@ module Gitlab
attributes[:merge_commit_sha] = target_branch_sha if pull_request.merged? attributes[:merge_commit_sha] = target_branch_sha if pull_request.merged?
merge_request = project.merge_requests.create!(attributes) merge_request = project.merge_requests.create!(attributes)
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 StandardError => e
errors << { type: :pull_request, iid: pull_request.iid, errors: e.message, trace: e.backtrace.join("\n"), raw_response: pull_request.raw }
end
end
end end
def import_pull_request_comments(pull_request, merge_request) def import_pull_request_comments(pull_request, merge_request)
# XXX This is inefficient since we are making multiple requests to the activities endpoint comments, other_activities = client.activities(project_key, repository_slug, pull_request.iid).partition(&:comment?)
merge_event = client.activities(project_key, repository_slug, pull_request.iid).find(&:merge_event?)
merge_event = other_activities.find(&:merge_event?)
import_merge_event(merge_request, merge_event) if merge_event import_merge_event(merge_request, merge_event) if merge_event
comments = client.activities(project_key, repository_slug, pull_request.iid).select(&:comment?)
inline_comments, pr_comments = comments.partition(&:inline_comment?) inline_comments, pr_comments = comments.partition(&:inline_comment?)
import_inline_comments(inline_comments.map(&:comment), pull_request, merge_request) import_inline_comments(inline_comments.map(&:comment), pull_request, merge_request)
...@@ -162,22 +164,11 @@ module Gitlab ...@@ -162,22 +164,11 @@ module Gitlab
def import_merge_event(merge_request, merge_event) def import_merge_event(merge_request, merge_event)
committer = merge_event.committer_email committer = merge_event.committer_email
return unless committer user = User.ghost
user ||= find_user_id(committer) if committer
user_id =
if committer
find_user_id(committer)
else
User.ghost
end
user_id = find_user_id(committer) if committer
timestamp = merge_event.merge_timestamp timestamp = merge_event.merge_timestamp
metric = MergeRequest::Metrics.find_or_initialize_by(merge_request: merge_request)
return unless user_id metric.update_attributes(merged_by: user, merged_at: timestamp)
event = Event.create(merged_by_id: user_id, merged_at: timestamp)
MergeRequestMetricsService.new(merge_request.metrics).merge(event)
end end
def import_inline_comments(inline_comments, pull_request, merge_request) def import_inline_comments(inline_comments, pull_request, merge_request)
......
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