importer_spec.rb 6.3 KB
Newer Older
1 2 3 4 5
require 'spec_helper'

describe Gitlab::BitbucketServerImport::Importer do
  include ImportSpecHelper

S
Stan Hu 已提交
6
  let(:project) { create(:project, :repository, import_url: 'http://my-bitbucket') }
7
  let(:now) { Time.now.utc.change(usec: 0) }
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

  subject { described_class.new(project) }

  before do
    data = project.create_or_update_import_data(
      data: { project_key: 'TEST', repo_slug: 'rouge' },
      credentials: { base_uri: 'http://my-bitbucket', user: 'bitbucket', password: 'test' }
    )
    data.save
    project.save
  end

  describe '#import_repository' do
    before do
      expect(subject).to receive(:import_pull_requests)
      expect(subject).to receive(:delete_temp_branches)
    end

    it 'adds a remote' do
      expect(project.repository).to receive(:fetch_as_mirror)
                                     .with('http://bitbucket:test@my-bitbucket',
                                           refmap: [:heads, :tags, '+refs/pull-requests/*/to:refs/merge-requests/*/head'],
                                           remote_name: 'bitbucket_server')

      subject.execute
    end
  end

  describe '#import_pull_requests' do
S
Stan Hu 已提交
37 38
    let(:sample) { RepoHelpers.sample_compare }

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    before do
      allow(subject).to receive(:import_repository)
      allow(subject).to receive(:delete_temp_branches)
      allow(subject).to receive(:restore_branches)

      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)

S
Stan Hu 已提交
60
      allow(subject.client).to receive(:pull_requests).and_return([pull_request])
61 62 63 64 65 66

      @merge_event = instance_double(
        BitbucketServer::Representation::Activity,
        comment?: false,
        merge_event?: true,
        committer_email: project.owner.email,
67
        merge_timestamp: now)
S
Stan Hu 已提交
68 69 70 71 72 73

      @pr_note = instance_double(
        BitbucketServer::Representation::Comment,
        note: 'Hello world',
        author_email: 'unknown@gmail.com',
        comments: [],
74 75
        created_at: now,
        updated_at: now)
76 77 78
      @pr_comment = instance_double(
        BitbucketServer::Representation::Activity,
        comment?: true,
S
Stan Hu 已提交
79 80 81
        inline_comment?: false,
        merge_event?: false,
        comment: @pr_note)
82 83
    end

S
Stan Hu 已提交
84
    it 'imports merge event' do
85 86 87 88 89 90 91 92
      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
93

S
Stan Hu 已提交
94 95 96 97 98 99 100 101 102 103 104 105
    it 'imports comments' do
      expect(subject.client).to receive(:activities).and_return([@pr_comment])

      expect { subject.execute }.to change { MergeRequest.count }.by(1)

      merge_request = MergeRequest.first
      expect(merge_request.notes.count).to eq(1)
      note = merge_request.notes.first
      expect(note.note).to eq(@pr_note.note)
      expect(note.author).to eq(project.owner)
      expect(note.created_at).to eq(@pr_note.created_at)
      expect(note.updated_at).to eq(@pr_note.created_at)
106 107
    end

108 109 110 111 112 113 114 115
    it 'imports threaded discussions' do
      reply = instance_double(
        BitbucketServer::Representation::PullRequestComment,
        author_email: 'someuser@gitlab.com',
        note: 'I agree',
        created_at: now,
        updated_at: now
      )
S
Stan Hu 已提交
116 117 118 119 120 121 122 123 124 125 126 127

      # https://gitlab.com/gitlab-org/gitlab-test/compare/c1acaa58bbcbc3eafe538cb8274ba387047b69f8...5937ac0a7beb003549fc5fd26fc247ad
      inline_note = instance_double(
        BitbucketServer::Representation::PullRequestComment,
        file_type: 'ADDED',
        from_sha: sample.commits.first,
        to_sha: sample.commits.last,
        file_path: '.gitmodules',
        old_pos: nil,
        new_pos: 4,
        note: 'Hello world',
        author_email: 'unknown@gmail.com',
128 129 130
        comments: [reply],
        created_at: now,
        updated_at: now)
S
Stan Hu 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143

      inline_comment = instance_double(
        BitbucketServer::Representation::Activity,
        comment?: true,
        inline_comment?: true,
        merge_event?: false,
        comment: inline_note)

      expect(subject.client).to receive(:activities).and_return([inline_comment])

      expect { subject.execute }.to change { MergeRequest.count }.by(1)

      merge_request = MergeRequest.first
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
      expect(merge_request.notes.count).to eq(2)
      expect(merge_request.notes.map(&:discussion_id).uniq.count).to eq(1)

      notes = merge_request.notes.order(:id).to_a
      start_note = notes.first
      expect(start_note.type).to eq('DiffNote')
      expect(start_note.note).to eq(inline_note.note)
      expect(start_note.created_at).to eq(inline_note.created_at)
      expect(start_note.updated_at).to eq(inline_note.updated_at)
      expect(start_note.position.base_sha).to eq(inline_note.from_sha)
      expect(start_note.position.start_sha).to eq(inline_note.from_sha)
      expect(start_note.position.head_sha).to eq(inline_note.to_sha)
      expect(start_note.position.old_line).to be_nil
      expect(start_note.position.new_line).to eq(inline_note.new_pos)

      reply_note = notes.last
      expect(reply_note.note).to eq(reply.note)
      expect(reply_note.author).to eq(project.owner)
      expect(reply_note.created_at).to eq(reply.created_at)
      expect(reply_note.updated_at).to eq(reply.created_at)
      expect(reply_note.position.base_sha).to eq(inline_note.from_sha)
      expect(reply_note.position.start_sha).to eq(inline_note.from_sha)
      expect(reply_note.position.head_sha).to eq(inline_note.to_sha)
      expect(reply_note.position.old_line).to be_nil
      expect(reply_note.position.new_line).to eq(inline_note.new_pos)
169 170
    end

S
Stan Hu 已提交
171
    it 'restores branches of inaccessible SHAs' do
172
    end
173 174 175
  end

  describe '#delete_temp_branches' do
S
Stan Hu 已提交
176 177
    it 'deletes branches' do
    end
178 179
  end
end