Commit 57c9a893 authored by Stan Hu's avatar Stan Hu

Fix assorted bugs and write spec for importing merge event

parent eb482bfa
...@@ -163,6 +163,8 @@ module Gitlab ...@@ -163,6 +163,8 @@ module Gitlab
target_branch_sha = pull_request.target_branch_sha target_branch_sha = pull_request.target_branch_sha
source_branch_sha = project.repository.commit(source_branch_sha)&.sha || source_branch_sha source_branch_sha = project.repository.commit(source_branch_sha)&.sha || source_branch_sha
target_branch_sha = project.repository.commit(target_branch_sha)&.sha || target_branch_sha target_branch_sha = project.repository.commit(target_branch_sha)&.sha || target_branch_sha
author = gitlab_user_id(project, pull_request.author_email) || User.ghost
project.merge_requests.find_by(iid: pull_request.iid)&.destroy project.merge_requests.find_by(iid: pull_request.iid)&.destroy
attributes = { attributes = {
...@@ -176,7 +178,7 @@ module Gitlab ...@@ -176,7 +178,7 @@ module Gitlab
target_branch: Gitlab::Git.ref_name(pull_request.target_branch_name), target_branch: Gitlab::Git.ref_name(pull_request.target_branch_name),
target_branch_sha: target_branch_sha, target_branch_sha: target_branch_sha,
state: pull_request.state, state: pull_request.state,
author_id: gitlab_user_id(project, pull_request.author_email), author_id: author.id,
assignee_id: nil, assignee_id: nil,
created_at: pull_request.created_at, created_at: pull_request.created_at,
updated_at: pull_request.updated_at updated_at: pull_request.updated_at
...@@ -202,8 +204,8 @@ module Gitlab ...@@ -202,8 +204,8 @@ 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
user = User.ghost user = find_user_id(committer) if committer
user ||= find_user_id(committer) if committer user ||= User.ghost
timestamp = merge_event.merge_timestamp timestamp = merge_event.merge_timestamp
metric = MergeRequest::Metrics.find_or_initialize_by(merge_request: merge_request) metric = MergeRequest::Metrics.find_or_initialize_by(merge_request: merge_request)
metric.update_attributes(merged_by: user, merged_at: timestamp) metric.update_attributes(merged_by: user, merged_at: timestamp)
......
...@@ -3,7 +3,7 @@ require 'spec_helper' ...@@ -3,7 +3,7 @@ require 'spec_helper'
describe Gitlab::BitbucketServerImport::Importer do describe Gitlab::BitbucketServerImport::Importer do
include ImportSpecHelper include ImportSpecHelper
let(:project) { create(:project, import_url: 'http://my-bitbucket') } set(:project) { create(:project, :repository, import_url: 'http://my-bitbucket') }
subject { described_class.new(project) } subject { described_class.new(project) }
...@@ -32,11 +32,71 @@ describe Gitlab::BitbucketServerImport::Importer do ...@@ -32,11 +32,71 @@ describe Gitlab::BitbucketServerImport::Importer do
end end
end end
# XXX We don't handle pull requests across forks
describe '#import_pull_requests' do describe '#import_pull_requests' do
before do
allow(subject).to receive(:import_repository)
allow(subject).to receive(:delete_temp_branches)
allow(subject).to receive(:restore_branches)
sample = RepoHelpers.sample_compare
pull_request = instance_double(
BitbucketServer::Representation::PullRequest,
iid: 10,
source_branch_sha: sample.commits.last,
source_branch_name: Gitlab::Git::BRANCH_REF_PREFIX + sample.source_branch,
target_branch_sha: sample.commits.first,
target_branch_name: Gitlab::Git::BRANCH_REF_PREFIX + sample.target_branch,
title: 'This is a title',
description: 'This is a test pull request',
state: 'merged',
author: 'Test Author',
author_email: project.owner.email,
created_at: Time.now,
updated_at: Time.now,
merged?: true)
expect(subject.client).to receive(:pull_requests).and_return([pull_request])
@merge_event = instance_double(
BitbucketServer::Representation::Activity,
comment?: false,
merge_event?: true,
committer_email: project.owner.email,
merge_timestamp: Time.now)
@inline_comment = instance_double(
BitbucketServer::Representation::Activity,
comment?: true,
merge_event?: false)
@pr_comment = instance_double(
BitbucketServer::Representation::Activity,
comment?: true,
merge_event?: false)
end
it 'handles merge event' do
expect(subject.client).to receive(:activities).and_return([@merge_event])
expect { subject.execute }.to change { MergeRequest.count }.by(1)
merge_request = MergeRequest.first
expect(merge_request.metrics.merged_by).to eq(project.owner)
expect(merge_request.metrics.merged_at).to eq(@merge_event.merge_timestamp)
end
context 'handles comments' do
end
context 'handles diff comments' do
end
context 'falls back to comments if diff comments' do
end
context 'restores branches of inaccessible SHAs' do
end
end end
describe '#delete_temp_branches' do describe '#delete_temp_branches' do
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