summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/github_import/importer/diff_note_importer_spec.rb
blob: 3dc15c7c0599fc0af2107b5c95aba6a93192a5b6 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::GithubImport::Importer::DiffNoteImporter do
  let(:project) { create(:project) }
  let(:client) { double(:client) }
  let(:user) { create(:user) }
  let(:created_at) { Time.new(2017, 1, 1, 12, 00) }
  let(:updated_at) { Time.new(2017, 1, 1, 12, 15) }

  let(:hunk) do
    '@@ -1 +1 @@
    -Hello
    +Hello world'
  end

  let(:note) do
    Gitlab::GithubImport::Representation::DiffNote.new(
      noteable_type: 'MergeRequest',
      noteable_id: 1,
      commit_id: '123abc',
      original_commit_id: 'original123abc',
      file_path: 'README.md',
      diff_hunk: hunk,
      author: Gitlab::GithubImport::Representation::User
        .new(id: user.id, login: user.username),
      note: 'Hello',
      created_at: created_at,
      updated_at: updated_at,
      github_id: 1
    )
  end

  let(:importer) { described_class.new(note, project, client) }

  describe '#execute' do
    context 'when the merge request no longer exists' do
      it 'does not import anything' do
        expect(Gitlab::Database.main).not_to receive(:bulk_insert)

        importer.execute
      end
    end

    context 'when the merge request exists' do
      let!(:merge_request) do
        create(:merge_request, source_project: project, target_project: project)
      end

      before do
        allow(importer)
          .to receive(:find_merge_request_id)
          .and_return(merge_request.id)
      end

      it 'imports the note' do
        allow(importer.user_finder)
          .to receive(:author_id_for)
          .and_return([user.id, true])

        expect(Gitlab::Database.main)
          .to receive(:bulk_insert)
          .with(
            LegacyDiffNote.table_name,
            [
              {
                discussion_id: anything,
                noteable_type: 'MergeRequest',
                noteable_id: merge_request.id,
                project_id: project.id,
                author_id: user.id,
                note: 'Hello',
                system: false,
                commit_id: 'original123abc',
                line_code: note.line_code,
                type: 'LegacyDiffNote',
                created_at: created_at,
                updated_at: updated_at,
                st_diff: note.diff_hash.to_yaml
              }
            ]
          )
          .and_call_original

        importer.execute
      end

      it 'imports the note when the author could not be found' do
        allow(importer.user_finder)
          .to receive(:author_id_for)
          .and_return([project.creator_id, false])

        expect(Gitlab::Database.main)
          .to receive(:bulk_insert)
          .with(
            LegacyDiffNote.table_name,
            [
              {
                discussion_id: anything,
                noteable_type: 'MergeRequest',
                noteable_id: merge_request.id,
                project_id: project.id,
                author_id: project.creator_id,
                note: "*Created by: #{user.username}*\n\nHello",
                system: false,
                commit_id: 'original123abc',
                line_code: note.line_code,
                type: 'LegacyDiffNote',
                created_at: created_at,
                updated_at: updated_at,
                st_diff: note.diff_hash.to_yaml
              }
            ]
          )
          .and_call_original

        importer.execute
      end

      it 'produces a valid LegacyDiffNote' do
        allow(importer.user_finder)
          .to receive(:author_id_for)
          .and_return([user.id, true])

        importer.execute

        note = project.notes.diff_notes.take

        expect(note).to be_valid
        expect(note.diff).to be_an_instance_of(Gitlab::Git::Diff)
      end

      it 'does not import the note when a foreign key error is raised' do
        allow(importer.user_finder)
          .to receive(:author_id_for)
          .and_return([project.creator_id, false])

        expect(Gitlab::Database.main)
          .to receive(:bulk_insert)
          .and_raise(ActiveRecord::InvalidForeignKey, 'invalid foreign key')

        expect { importer.execute }.not_to raise_error
      end
    end
  end

  describe '#find_merge_request_id' do
    it 'returns a merge request ID' do
      expect_next_instance_of(Gitlab::GithubImport::IssuableFinder) do |instance|
        expect(instance).to receive(:database_id).and_return(10)
      end

      expect(importer.find_merge_request_id).to eq(10)
    end
  end
end