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

fix_merge_request_diff_commit_users_spec.rb « background_migration « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c343ee438b8eec71e91085a0231d0f221c32abef (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
# frozen_string_literal: true

require 'spec_helper'

# The underlying migration relies on the global models (e.g. Project). This
# means we also need to use FactoryBot factories to ensure everything is
# operating using the same types. If we use `table()` and similar methods we
# would have to duplicate a lot of logic just for these tests.
#
# rubocop: disable RSpec/FactoriesInMigrationSpecs
RSpec.describe Gitlab::BackgroundMigration::FixMergeRequestDiffCommitUsers do
  let(:migration) { described_class.new }

  describe '#perform' do
    context 'when the project exists' do
      it 'processes the project' do
        project = create(:project)

        expect(migration).to receive(:process).with(project)
        expect(migration).to receive(:schedule_next_job)

        migration.perform(project.id)
      end

      it 'marks the background job as finished' do
        project = create(:project)

        Gitlab::Database::BackgroundMigrationJob.create!(
          class_name: 'FixMergeRequestDiffCommitUsers',
          arguments: [project.id]
        )

        migration.perform(project.id)

        job = Gitlab::Database::BackgroundMigrationJob
          .find_by(class_name: 'FixMergeRequestDiffCommitUsers')

        expect(job.status).to eq('succeeded')
      end
    end

    context 'when the project does not exist' do
      it 'does nothing' do
        expect(migration).not_to receive(:process)
        expect(migration).to receive(:schedule_next_job)

        migration.perform(-1)
      end
    end
  end

  describe '#process' do
    it 'processes the merge requests of the project' do
      project = create(:project, :repository)
      commit = project.commit
      mr = create(
        :merge_request_with_diffs,
        source_project: project,
        target_project: project
      )

      diff = mr.merge_request_diffs.first

      create(
        :merge_request_diff_commit,
        merge_request_diff: diff,
        sha: commit.sha,
        relative_order: 9000
      )

      migration.process(project)

      updated = diff
        .merge_request_diff_commits
        .find_by(sha: commit.sha, relative_order: 9000)

      expect(updated.commit_author_id).not_to be_nil
      expect(updated.committer_id).not_to be_nil
    end
  end

  describe '#update_commit' do
    let(:project) { create(:project, :repository) }
    let(:mr) do
      create(
        :merge_request_with_diffs,
        source_project: project,
        target_project: project
      )
    end

    let(:diff) { mr.merge_request_diffs.first }
    let(:commit) { project.commit }

    def update_row(migration, project, diff, row)
      migration.update_commit(project, row)

      diff
        .merge_request_diff_commits
        .find_by(sha: row.sha, relative_order: row.relative_order)
    end

    it 'populates missing commit authors' do
      commit_row = create(
        :merge_request_diff_commit,
        merge_request_diff: diff,
        sha: commit.sha,
        relative_order: 9000
      )

      updated = update_row(migration, project, diff, commit_row)

      expect(updated.commit_author.name).to eq(commit.to_hash[:author_name])
      expect(updated.commit_author.email).to eq(commit.to_hash[:author_email])
    end

    it 'populates missing committers' do
      commit_row = create(
        :merge_request_diff_commit,
        merge_request_diff: diff,
        sha: commit.sha,
        relative_order: 9000
      )

      updated = update_row(migration, project, diff, commit_row)

      expect(updated.committer.name).to eq(commit.to_hash[:committer_name])
      expect(updated.committer.email).to eq(commit.to_hash[:committer_email])
    end

    it 'leaves existing commit authors as-is' do
      user = create(:merge_request_diff_commit_user)
      commit_row = create(
        :merge_request_diff_commit,
        merge_request_diff: diff,
        sha: commit.sha,
        relative_order: 9000,
        commit_author: user
      )

      updated = update_row(migration, project, diff, commit_row)

      expect(updated.commit_author).to eq(user)
    end

    it 'leaves existing committers as-is' do
      user = create(:merge_request_diff_commit_user)
      commit_row = create(
        :merge_request_diff_commit,
        merge_request_diff: diff,
        sha: commit.sha,
        relative_order: 9000,
        committer: user
      )

      updated = update_row(migration, project, diff, commit_row)

      expect(updated.committer).to eq(user)
    end

    it 'does nothing when both the author and committer are present' do
      user = create(:merge_request_diff_commit_user)
      commit_row = create(
        :merge_request_diff_commit,
        merge_request_diff: diff,
        sha: commit.sha,
        relative_order: 9000,
        committer: user,
        commit_author: user
      )

      recorder = ActiveRecord::QueryRecorder.new do
        migration.update_commit(project, commit_row)
      end

      expect(recorder.count).to be_zero
    end

    it 'does nothing if the commit does not exist in Git' do
      user = create(:merge_request_diff_commit_user)
      commit_row = create(
        :merge_request_diff_commit,
        merge_request_diff: diff,
        sha: 'kittens',
        relative_order: 9000,
        committer: user,
        commit_author: user
      )

      recorder = ActiveRecord::QueryRecorder.new do
        migration.update_commit(project, commit_row)
      end

      expect(recorder.count).to be_zero
    end

    it 'does nothing when the committer/author are missing in the Git commit' do
      user = create(:merge_request_diff_commit_user)
      commit_row = create(
        :merge_request_diff_commit,
        merge_request_diff: diff,
        sha: commit.sha,
        relative_order: 9000,
        committer: user,
        commit_author: user
      )

      allow(migration).to receive(:find_or_create_user).and_return(nil)

      recorder = ActiveRecord::QueryRecorder.new do
        migration.update_commit(project, commit_row)
      end

      expect(recorder.count).to be_zero
    end
  end

  describe '#schedule_next_job' do
    it 'schedules the next background migration' do
      Gitlab::Database::BackgroundMigrationJob
        .create!(class_name: 'FixMergeRequestDiffCommitUsers', arguments: [42])

      expect(BackgroundMigrationWorker)
        .to receive(:perform_in)
        .with(2.minutes, 'FixMergeRequestDiffCommitUsers', [42])

      migration.schedule_next_job
    end

    it 'does nothing when there are no jobs' do
      expect(BackgroundMigrationWorker)
        .not_to receive(:perform_in)

      migration.schedule_next_job
    end
  end

  describe '#find_commit' do
    let(:project) { create(:project, :repository) }

    it 'finds a commit using Git' do
      commit = project.commit
      found = migration.find_commit(project, commit.sha)

      expect(found).to eq(commit.to_hash)
    end

    it 'caches the results' do
      commit = project.commit

      migration.find_commit(project, commit.sha)

      expect { migration.find_commit(project, commit.sha) }
        .not_to change { Gitlab::GitalyClient.get_request_count }
    end

    it 'returns an empty hash if the commit does not exist' do
      expect(migration.find_commit(project, 'kittens')).to eq({})
    end
  end

  describe '#find_or_create_user' do
    let(:project) { create(:project, :repository) }

    it 'creates missing users' do
      commit = project.commit.to_hash
      id = migration.find_or_create_user(commit, :author_name, :author_email)

      expect(MergeRequest::DiffCommitUser.count).to eq(1)

      created = MergeRequest::DiffCommitUser.first

      expect(created.name).to eq(commit[:author_name])
      expect(created.email).to eq(commit[:author_email])
      expect(created.id).to eq(id)
    end

    it 'returns users that already exist' do
      commit = project.commit.to_hash
      user1 = migration.find_or_create_user(commit, :author_name, :author_email)
      user2 = migration.find_or_create_user(commit, :author_name, :author_email)

      expect(user1).to eq(user2)
    end

    it 'caches the results' do
      commit = project.commit.to_hash

      migration.find_or_create_user(commit, :author_name, :author_email)

      recorder = ActiveRecord::QueryRecorder.new do
        migration.find_or_create_user(commit, :author_name, :author_email)
      end

      expect(recorder.count).to be_zero
    end

    it 'returns nil if the commit details are missing' do
      id = migration.find_or_create_user({}, :author_name, :author_email)

      expect(id).to be_nil
    end
  end

  describe '#matches_row' do
    it 'returns the query matches for the composite primary key' do
      row = double(:commit, merge_request_diff_id: 4, relative_order: 5)
      arel = migration.matches_row(row)

      expect(arel.to_sql).to eq(
        '("merge_request_diff_commits"."merge_request_diff_id", "merge_request_diff_commits"."relative_order") = (4, 5)'
      )
    end
  end
end
# rubocop: enable RSpec/FactoriesInMigrationSpecs