Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pull_request_notes_importer_spec.rb « importers « bitbucket_server_import « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7b662c1a2c7daea7de2ea07ffe0258a5c96eca5a (plain)
1
2
3
4
5
6
7
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::BitbucketServerImport::Importers::PullRequestNotesImporter, feature_category: :importers do
  include AfterNextHelpers

  let_it_be_with_reload(:project) do
    create(:project, :repository, :import_started,
      import_data_attributes: {
        data: { 'project_key' => 'key', 'repo_slug' => 'slug' },
        credentials: { 'token' => 'token' }
      }
    )
  end

  let_it_be(:pull_request_data) { Gitlab::Json.parse(fixture_file('importers/bitbucket_server/pull_request.json')) }
  let_it_be(:pull_request) { BitbucketServer::Representation::PullRequest.new(pull_request_data) }
  let_it_be(:note_author) { create(:user, username: 'note_author', email: 'note_author@example.org') }
  let(:mentions_converter) { Gitlab::BitbucketServerImport::MentionsConverter.new(project) }

  let!(:pull_request_author) do
    create(:user, username: 'pull_request_author', email: 'pull_request_author@example.org')
  end

  let(:merge_event) do
    instance_double(
      BitbucketServer::Representation::Activity,
      id: 3,
      comment?: false,
      merge_event?: true,
      approved_event?: false,
      committer_email: pull_request_author.email,
      merge_timestamp: now,
      merge_commit: '12345678'
    )
  end

  let(:approved_event) do
    instance_double(
      BitbucketServer::Representation::Activity,
      id: 4,
      comment?: false,
      merge_event?: false,
      approved_event?: true,
      approver_username: pull_request_author.username,
      approver_email: pull_request_author.email,
      created_at: now
    )
  end

  let(:pr_note) do
    instance_double(
      BitbucketServer::Representation::Comment,
      note: 'Hello world',
      author_email: note_author.email,
      author_username: note_author.username,
      comments: [],
      created_at: now,
      updated_at: now,
      parent_comment: nil)
  end

  let(:pr_comment) do
    instance_double(
      BitbucketServer::Representation::Activity,
      id: 5,
      comment?: true,
      inline_comment?: false,
      merge_event?: false,
      comment: pr_note)
  end

  let_it_be(:sample) { RepoHelpers.sample_compare }
  let_it_be(:now) { Time.now.utc.change(usec: 0) }

  def expect_log(stage:, message:)
    allow(Gitlab::BitbucketServerImport::Logger).to receive(:info).and_call_original
    expect(Gitlab::BitbucketServerImport::Logger)
      .to receive(:info).with(include(import_stage: stage, message: message))
  end

  before do
    allow(Gitlab::BitbucketServerImport::MentionsConverter).to receive(:new).and_return(mentions_converter)
  end

  subject(:importer) { described_class.new(project.reload, pull_request.to_hash) }

  describe '#execute' do
    context 'when a matching merge request is not found' do
      it 'does nothing' do
        expect { importer.execute }.not_to change { Note.count }
      end

      it 'logs its progress' do
        expect_log(stage: 'import_pull_request_notes', message: 'starting')
        expect_log(stage: 'import_pull_request_notes', message: 'finished')

        importer.execute
      end
    end

    context 'when a matching merge request is found', :clean_gitlab_redis_cache do
      let_it_be(:merge_request) { create(:merge_request, iid: pull_request.iid, source_project: project) }

      it 'logs its progress' do
        allow_next(BitbucketServer::Client).to receive(:activities).and_return([])

        expect_log(stage: 'import_pull_request_notes', message: 'starting')
        expect_log(stage: 'import_pull_request_notes', message: 'finished')

        importer.execute
      end

      context 'when PR has comments' do
        before do
          allow_next(BitbucketServer::Client).to receive(:activities).and_return([pr_comment])
        end

        it 'imports the stand alone comments' do
          expect(mentions_converter).to receive(:convert).and_call_original

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

          expect(merge_request.notes.count).to eq(1)
          expect(merge_request.notes.first).to have_attributes(
            note: end_with(pr_note.note),
            author: note_author,
            created_at: pr_note.created_at,
            updated_at: pr_note.created_at
          )
        end

        context 'when the author is not found' do
          before do
            allow_next_instance_of(Gitlab::BitbucketServerImport::UserFinder) do |user_finder|
              allow(user_finder).to receive(:uid).and_return(nil)
            end
          end

          it 'adds a note with the author username and email' do
            subject.execute

            expect(Note.first.note).to include("*By #{note_author.username} (#{note_author.email})")
          end
        end

        context 'when the note has a parent note' do
          let(:pr_note) do
            instance_double(
              BitbucketServer::Representation::Comment,
              note: 'Note',
              author_email: note_author.email,
              author_username: note_author.username,
              comments: [],
              created_at: now,
              updated_at: now,
              parent_comment: pr_parent_note
            )
          end

          let(:pr_parent_note) do
            instance_double(
              BitbucketServer::Representation::Comment,
              note: 'Parent note',
              author_email: note_author.email,
              author_username: note_author.username,
              comments: [],
              created_at: now,
              updated_at: now,
              parent_comment: nil
            )
          end

          it 'adds the parent note before the actual note' do
            subject.execute

            expect(Note.first.note).to include("> #{pr_parent_note.note}\n\n")
          end
        end

        context 'when the `bitbucket_server_convert_mentions_to_users` flag is disabled' do
          before do
            stub_feature_flags(bitbucket_server_convert_mentions_to_users: false)
          end

          it 'does not convert mentions' do
            expect(mentions_converter).not_to receive(:convert)

            subject.execute
          end
        end

        it 'logs its progress' do
          expect_log(stage: 'import_standalone_pr_comments', message: 'starting')
          expect_log(stage: 'import_standalone_pr_comments', message: 'finished')

          importer.execute
        end
      end

      context 'when PR has threaded discussion' do
        let_it_be(:reply_author) { create(:user, username: 'reply_author', email: 'reply_author@example.org') }
        let_it_be(:inline_note_author) do
          create(:user, username: 'inline_note_author', email: 'inline_note_author@example.org')
        end

        let(:reply) do
          instance_double(
            BitbucketServer::Representation::PullRequestComment,
            author_email: reply_author.email,
            author_username: reply_author.username,
            note: 'I agree',
            created_at: now,
            updated_at: now,
            parent_comment: nil)
        end

        let(:pr_inline_note) do
          instance_double(
            BitbucketServer::Representation::PullRequestComment,
            file_type: 'ADDED',
            from_sha: pull_request.target_branch_sha,
            to_sha: pull_request.source_branch_sha,
            file_path: '.gitmodules',
            old_pos: nil,
            new_pos: 4,
            note: 'Hello world',
            author_email: inline_note_author.email,
            author_username: inline_note_author.username,
            comments: [reply],
            created_at: now,
            updated_at: now,
            parent_comment: nil)
        end

        let(:pr_inline_comment) do
          instance_double(
            BitbucketServer::Representation::Activity,
            comment?: true,
            inline_comment?: true,
            merge_event?: false,
            comment: pr_inline_note)
        end

        before do
          allow_next(BitbucketServer::Client).to receive(:activities).and_return([pr_inline_comment])
        end

        it 'imports the threaded discussion' do
          expect(mentions_converter).to receive(:convert).and_call_original.twice

          expect { subject.execute }.to change { Note.count }.by(2)

          expect(merge_request.discussions.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 end_with(pr_inline_note.note)
          expect(start_note.created_at).to eq(pr_inline_note.created_at)
          expect(start_note.updated_at).to eq(pr_inline_note.updated_at)
          expect(start_note.position.old_line).to be_nil
          expect(start_note.position.new_line).to eq(pr_inline_note.new_pos)
          expect(start_note.author).to eq(inline_note_author)

          reply_note = notes.last
          expect(reply_note.note).to eq(reply.note)
          expect(reply_note.author).to eq(reply_author)
          expect(reply_note.created_at).to eq(reply.created_at)
          expect(reply_note.updated_at).to eq(reply.created_at)
          expect(reply_note.position.old_line).to be_nil
          expect(reply_note.position.new_line).to eq(pr_inline_note.new_pos)
        end

        context 'when the `bitbucket_server_convert_mentions_to_users` flag is disabled' do
          before do
            stub_feature_flags(bitbucket_server_convert_mentions_to_users: false)
          end

          it 'does not convert mentions' do
            expect(mentions_converter).not_to receive(:convert)

            subject.execute
          end
        end

        it 'logs its progress' do
          expect_log(stage: 'import_inline_comments', message: 'starting')
          expect_log(stage: 'import_inline_comments', message: 'finished')

          importer.execute
        end
      end

      context 'when PR has a merge event' do
        before do
          allow_next(BitbucketServer::Client).to receive(:activities).and_return([merge_event])
        end

        it 'imports the merge event' do
          importer.execute

          merge_request.reload

          expect(merge_request.metrics.merged_by).to eq(pull_request_author)
          expect(merge_request.metrics.merged_at).to eq(merge_event.merge_timestamp)
          expect(merge_request.merge_commit_sha).to eq(merge_event.merge_commit)
        end
      end

      context 'when PR has an approved event' do
        before do
          allow_next(BitbucketServer::Client).to receive(:activities).and_return([approved_event])
        end

        it 'creates the approval, reviewer and approval note' do
          expect { importer.execute }
            .to change { merge_request.approvals.count }.from(0).to(1)
            .and change { merge_request.notes.count }.from(0).to(1)
            .and change { merge_request.reviewers.count }.from(0).to(1)

          approval = merge_request.approvals.first

          expect(approval.user).to eq(pull_request_author)
          expect(approval.created_at).to eq(now)

          note = merge_request.notes.first

          expect(note.note).to eq('approved this merge request')
          expect(note.author).to eq(pull_request_author)
          expect(note.system).to be_truthy
          expect(note.created_at).to eq(now)

          reviewer = merge_request.reviewers.first

          expect(reviewer.id).to eq(pull_request_author.id)
        end

        context 'when a user with a matching username does not exist' do
          before do
            pull_request_author.update!(username: 'another_username')
          end

          it 'finds the user based on email' do
            importer.execute

            approval = merge_request.approvals.first

            expect(approval.user).to eq(pull_request_author)
          end

          context 'when no users match email or username' do
            let_it_be(:another_author) { create(:user) }

            before do
              pull_request_author.destroy!
            end

            it 'does not set an approver' do
              expect { importer.execute }
                .to not_change { merge_request.approvals.count }
                .and not_change { merge_request.notes.count }
                .and not_change { merge_request.reviewers.count }

              expect(merge_request.approvals).to be_empty
            end
          end
        end

        context 'if the reviewer already existed' do
          before do
            merge_request.reviewers = [pull_request_author]
            merge_request.save!
          end

          it 'does not create the reviewer record' do
            expect { importer.execute }.not_to change { merge_request.reviewers.count }
          end
        end
      end
    end

    shared_examples 'import is skipped' do
      it 'does not log and does not import notes' do
        expect(Gitlab::BitbucketServerImport::Logger)
          .not_to receive(:info).with(include(import_stage: 'import_pull_request_notes', message: 'starting'))

        expect { importer.execute }.not_to change { Note.count }
      end
    end

    context 'when the project has been marked as failed' do
      before do
        project.import_state.mark_as_failed('error')
      end

      include_examples 'import is skipped'
    end

    context 'when the import data does not have credentials' do
      before do
        project.import_data.credentials = nil
        project.import_data.save!
      end

      include_examples 'import is skipped'
    end

    context 'when the import data does not have data' do
      before do
        project.import_data.data = nil
        project.import_data.save!
      end

      include_examples 'import is skipped'
    end
  end
end