importer_spec.rb 11.4 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
  let(:project_key) { 'TEST' }
  let(:repo_slug) { 'rouge' }
  let(:sample) { RepoHelpers.sample_compare }
11

12
  subject { described_class.new(project, recover_missing_commits: true) }
13 14 15

  before do
    data = project.create_or_update_import_data(
16
      data: { project_key: project_key, repo_slug: repo_slug },
17 18 19 20 21 22 23
      credentials: { base_uri: 'http://my-bitbucket', user: 'bitbucket', password: 'test' }
    )
    data.save
    project.save
  end

  describe '#import_repository' do
24
    it 'adds a remote' do
25 26 27 28 29 30 31 32 33
      expect(subject).to receive(:import_pull_requests)
      expect(subject).to receive(:delete_temp_branches)
      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
34 35 36 37 38 39 40 41 42 43 44 45

    it 'raises a Gitlab::Shell exception in the fetch' do
      expect(project.repository).to receive(:fetch_as_mirror).and_raise(Gitlab::Shell::Error)

      expect { subject.execute }.to raise_error(Gitlab::Shell::Error)
    end

    it 'raises an unhandled exception in the fetch' do
      expect(project.repository).to receive(:fetch_as_mirror).and_raise(RuntimeError)

      expect { subject.execute }.to raise_error(RuntimeError)
    end
46 47 48
  end

  describe '#import_pull_requests' do
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    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 已提交
70
      allow(subject.client).to receive(:pull_requests).and_return([pull_request])
71 72 73 74 75 76

      @merge_event = instance_double(
        BitbucketServer::Representation::Activity,
        comment?: false,
        merge_event?: true,
        committer_email: project.owner.email,
S
Stan Hu 已提交
77 78 79
        merge_timestamp: now,
        merge_commit: '12345678'
      )
S
Stan Hu 已提交
80 81 82 83 84

      @pr_note = instance_double(
        BitbucketServer::Representation::Comment,
        note: 'Hello world',
        author_email: 'unknown@gmail.com',
85
        author_username: 'The Flash',
S
Stan Hu 已提交
86
        comments: [],
87
        created_at: now,
88 89 90
        updated_at: now,
        parent_comment: nil)

91 92 93
      @pr_comment = instance_double(
        BitbucketServer::Representation::Activity,
        comment?: true,
S
Stan Hu 已提交
94 95 96
        inline_comment?: false,
        merge_event?: false,
        comment: @pr_note)
97 98
    end

S
Stan Hu 已提交
99
    it 'imports merge event' do
100 101 102 103 104 105 106
      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)
S
Stan Hu 已提交
107
      expect(merge_request.merge_commit_sha).to eq('12345678')
108
    end
109

S
Stan Hu 已提交
110 111 112 113 114 115 116 117
    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
118
      expect(note.note).to end_with(@pr_note.note)
S
Stan Hu 已提交
119 120 121
      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)
122 123
    end

124 125 126 127
    it 'imports threaded discussions' do
      reply = instance_double(
        BitbucketServer::Representation::PullRequestComment,
        author_email: 'someuser@gitlab.com',
128
        author_username: 'Batman',
129 130
        note: 'I agree',
        created_at: now,
131
        updated_at: now)
S
Stan Hu 已提交
132 133 134 135 136 137 138 139 140 141 142 143

      # 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',
144
        author_username: 'Superman',
145 146
        comments: [reply],
        created_at: now,
147 148 149 150
        updated_at: now,
        parent_comment: nil)

      allow(reply).to receive(:parent_comment).and_return(inline_note)
S
Stan Hu 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163

      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
164 165 166 167 168 169
      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')
170
      expect(start_note.note).to end_with(inline_note.note)
171 172 173 174 175 176 177 178 179
      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
180
      # Make sure author and reply context is included
181
      expect(reply_note.note).to start_with("*By #{reply.author_username} (#{reply.author_email})*\n\n")
182
      expect(reply_note.note).to end_with("> #{inline_note.note}\n\n#{reply.note}")
183 184 185 186 187 188 189 190
      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)
191 192
    end

193
    it 'falls back to comments if diff comments fail to validate' do
194 195 196
      reply = instance_double(
        BitbucketServer::Representation::Comment,
        author_email: 'someuser@gitlab.com',
197
        author_username: 'Aquaman',
198 199 200 201
        note: 'I agree',
        created_at: now,
        updated_at: now)

202 203 204 205 206 207 208 209 210 211 212
      # https://gitlab.com/gitlab-org/gitlab-test/compare/c1acaa58bbcbc3eafe538cb8274ba387047b69f8...5937ac0a7beb003549fc5fd26fc247ad
      inline_note = instance_double(
        BitbucketServer::Representation::PullRequestComment,
        file_type: 'REMOVED',
        from_sha: sample.commits.first,
        to_sha: sample.commits.last,
        file_path: '.gitmodules',
        old_pos: 8,
        new_pos: 9,
        note: 'This is a note with an invalid line position.',
        author_email: project.owner.email,
213
        author_username: 'Owner',
214
        comments: [reply],
215
        created_at: now,
216 217
        updated_at: now,
        parent_comment: nil)
218 219 220 221 222 223 224 225

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

226 227
      allow(reply).to receive(:parent_comment).and_return(inline_note)

228 229 230 231 232
      expect(subject.client).to receive(:activities).and_return([inline_comment])

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

      merge_request = MergeRequest.first
233 234
      expect(merge_request.notes.count).to eq(2)
      notes = merge_request.notes
235

236 237
      expect(notes.first.note).to start_with('*Comment on .gitmodules')
      expect(notes.second.note).to start_with('*Comment on .gitmodules')
238
    end
239 240
  end

241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
  describe 'inaccessible branches' do
    let(:id) { 10 }
    let(:temp_branch_from) { "gitlab/import/pull-request/#{id}/from" }
    let(:temp_branch_to) { "gitlab/import/pull-request/#{id}/to" }

    before do
      pull_request = instance_double(
        BitbucketServer::Representation::PullRequest,
        iid: id,
        source_branch_sha: '12345678',
        source_branch_name: Gitlab::Git::BRANCH_REF_PREFIX + sample.source_branch,
        target_branch_sha: '98765432',
        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])
      expect(subject.client).to receive(:activities).and_return([])
      expect(subject).to receive(:import_repository).twice
    end

    it '#restore_branches' do
      expect(subject).to receive(:restore_branches).and_call_original
      expect(subject).to receive(:delete_temp_branches)
S
Stan Hu 已提交
271 272 273 274 275 276 277 278
      expect(subject.client).to receive(:create_branch)
                                 .with(project_key, repo_slug,
                                       temp_branch_from,
                                       '12345678')
      expect(subject.client).to receive(:create_branch)
                                 .with(project_key, repo_slug,
                                       temp_branch_to,
                                       '98765432')
279 280 281 282 283 284 285

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

    it '#delete_temp_branches' do
      expect(subject.client).to receive(:create_branch).twice
      expect(subject).to receive(:delete_temp_branches).and_call_original
S
Stan Hu 已提交
286 287 288 289 290 291 292 293
      expect(subject.client).to receive(:delete_branch)
                                 .with(project_key, repo_slug,
                                       temp_branch_from,
                                       '12345678')
      expect(subject.client).to receive(:delete_branch)
                                 .with(project_key, repo_slug,
                                       temp_branch_to,
                                       '98765432')
294 295 296 297
      expect(project.repository).to receive(:delete_branch).with(temp_branch_from)
      expect(project.repository).to receive(:delete_branch).with(temp_branch_to)

      expect { subject.execute }.to change { MergeRequest.count }.by(1)
S
Stan Hu 已提交
298
    end
299 300
  end
end