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

require 'spec_helper'

RSpec.describe Gitlab::GithubImport::Importer::PullRequestReviewImporter, :clean_gitlab_redis_cache do
  using RSpec::Parameterized::TableSyntax

  let_it_be(:merge_request) { create(:merge_request) }

  let(:project) { merge_request.project }
  let(:submitted_at) { Time.new(2017, 1, 1, 12, 00).utc }
  let(:client_double) do
    instance_double(
      'Gitlab::GithubImport::Client',
      user: { id: 999, login: 'author', email: 'author@email.com' }
    )
  end

  subject { described_class.new(review, project, client_double) }

  shared_examples 'imports a reviewer for the Merge Request' do
    it 'creates reviewer for the Merge Request' do
      expect { subject.execute }.to change(MergeRequestReviewer, :count).by(1)

      expect(merge_request.reviewers).to contain_exactly(author)
    end

    context 'when reviewer already exists' do
      before do
        create(
          :merge_request_reviewer,
          reviewer: author, merge_request: merge_request, state: 'unreviewed'
        )
      end

      it 'does not change Merge Request reviewers' do
        expect { subject.execute }.not_to change(MergeRequestReviewer, :count)

        expect(merge_request.reviewers).to contain_exactly(author)
      end
    end
  end

  shared_examples 'imports an approval for the Merge Request' do
    it 'creates an approval for the Merge Request' do
      expect { subject.execute }.to change(Approval, :count).by(1)

      expect(merge_request.approved_by_users.reload).to include(author)
      expect(merge_request.approvals.last.created_at).to eq(submitted_at)
    end
  end

  context 'when the review author can be mapped to a gitlab user' do
    let_it_be(:author) { create(:user, email: 'author@email.com') }

    context 'when the review has no note text' do
      context 'when the review is "APPROVED"' do
        let(:review) { create_review(type: 'APPROVED', note: '') }

        it_behaves_like 'imports an approval for the Merge Request'
        it_behaves_like 'imports a reviewer for the Merge Request'

        it 'creates a note for the review' do
          expect { subject.execute }.to change(Note, :count).by(1)

          last_note = merge_request.notes.last
          expect(last_note.note).to eq('approved this merge request')
          expect(last_note.author).to eq(author)
          expect(last_note.created_at).to eq(submitted_at)
          expect(last_note.system_note_metadata.action).to eq('approved')
        end

        context 'when the user already approved the merge request' do
          before do
            create(:approval, merge_request: merge_request, user: author)
          end

          it 'does not import second approve and note' do
            expect { subject.execute }
              .to change(Note, :count).by(0)
              .and change(Approval, :count).by(0)
          end
        end
      end

      context 'when the review is "COMMENTED"' do
        let(:review) { create_review(type: 'COMMENTED', note: '') }

        it_behaves_like 'imports a reviewer for the Merge Request'

        it 'does not create note for the review' do
          expect { subject.execute }.not_to change(Note, :count)
        end
      end

      context 'when the review is "CHANGES_REQUESTED"' do
        let(:review) { create_review(type: 'CHANGES_REQUESTED', note: '') }

        it_behaves_like 'imports a reviewer for the Merge Request'

        it 'does not create a note for the review' do
          expect { subject.execute }.not_to change(Note, :count)
        end
      end
    end

    context 'when the review has a note text' do
      context 'when the review is "APPROVED"' do
        let(:review) { create_review(type: 'APPROVED') }

        it_behaves_like 'imports an approval for the Merge Request'
        it_behaves_like 'imports a reviewer for the Merge Request'

        it 'creates a note for the review' do
          expect { subject.execute }.to change(Note, :count).by(2)

          note = merge_request.notes.where(system: false).last
          expect(note.note).to eq("**Review:** Approved\n\nnote")
          expect(note.author).to eq(author)
          expect(note.created_at).to eq(submitted_at)

          system_note = merge_request.notes.where(system: true).last
          expect(system_note.note).to eq('approved this merge request')
          expect(system_note.author).to eq(author)
          expect(system_note.created_at).to eq(submitted_at)
          expect(system_note.system_note_metadata.action).to eq('approved')
        end
      end

      context 'when the review is "COMMENTED"' do
        let(:review) { create_review(type: 'COMMENTED') }

        it 'creates a note for the review' do
          expect { subject.execute }
            .to change(Note, :count).by(1)
            .and not_change(Approval, :count)

          last_note = merge_request.notes.last

          expect(last_note.note).to eq("**Review:** Commented\n\nnote")
          expect(last_note.author).to eq(author)
          expect(last_note.created_at).to eq(submitted_at)
        end
      end

      context 'when the review is "CHANGES_REQUESTED"' do
        let(:review) { create_review(type: 'CHANGES_REQUESTED') }

        it 'creates a note for the review' do
          expect { subject.execute }
            .to change(Note, :count).by(1)
            .and not_change(Approval, :count)

          last_note = merge_request.notes.last

          expect(last_note.note).to eq("**Review:** Changes requested\n\nnote")
          expect(last_note.author).to eq(author)
          expect(last_note.created_at).to eq(submitted_at)
        end
      end
    end
  end

  context 'when the review author cannot be mapped to a gitlab user' do
    context 'when the review has no note text' do
      context 'when the review is "APPROVED"' do
        let(:review) { create_review(type: 'APPROVED', note: '') }

        it 'creates a note for the review with *Approved by by<author>*' do
          expect { subject.execute }
            .to change(Note, :count).by(1)

          last_note = merge_request.notes.last
          expect(last_note.note).to eq("*Created by: author*\n\n**Review:** Approved")
          expect(last_note.author).to eq(project.creator)
          expect(last_note.created_at).to eq(submitted_at)
        end
      end

      context 'when the review is "COMMENTED"' do
        let(:review) { create_review(type: 'COMMENTED', note: '') }

        it 'creates a note for the review with *Commented by<author>*' do
          expect { subject.execute }.not_to change(Note, :count)
        end
      end

      context 'when the review is "CHANGES_REQUESTED"' do
        let(:review) { create_review(type: 'CHANGES_REQUESTED', note: '') }

        it 'creates a note for the review with *Changes requested by <author>*' do
          expect { subject.execute }.not_to change(Note, :count)
        end
      end
    end

    context 'when original author was deleted in github' do
      let(:review) { create_review(type: 'APPROVED', note: '', author: nil) }

      it 'creates a note for the review without the author information' do
        expect { subject.execute }
          .to change(Note, :count).by(1)

        last_note = merge_request.notes.last
        expect(last_note.note).to eq('**Review:** Approved')
        expect(last_note.author).to eq(project.creator)
        expect(last_note.created_at).to eq(submitted_at)
      end
    end

    context 'when original author cannot be found on github' do
      before do
        allow(client_double).to receive(:user).and_raise(Octokit::NotFound)
      end

      let(:review) { create_review(type: 'APPROVED', note: '') }

      it 'creates a note for the review with the author username' do
        expect { subject.execute }
          .to change(Note, :count).by(1)
        last_note = merge_request.notes.last
        expect(last_note.note).to eq("*Created by: author*\n\n**Review:** Approved")
        expect(last_note.author).to eq(project.creator)
        expect(last_note.created_at).to eq(submitted_at)
      end
    end

    context 'when the submitted_at is not provided' do
      let(:review) { create_review(type: 'APPROVED', note: '', submitted_at: nil) }

      it 'creates a note for the review without the author information' do
        expect { subject.execute }.to change(Note, :count).by(1)

        last_note = merge_request.notes.last

        expect(last_note.created_at)
          .to be_within(1.second).of(merge_request.updated_at)
      end
    end

    context 'when the review has a note text' do
      context 'when the review is "APPROVED"' do
        let(:review) { create_review(type: 'APPROVED') }

        it 'creates a note for the review with *Approved by by<author>*' do
          expect { subject.execute }
            .to change(Note, :count).by(1)

          last_note = merge_request.notes.last

          expect(last_note.note).to eq("*Created by: author*\n\n**Review:** Approved\n\nnote")
          expect(last_note.author).to eq(project.creator)
          expect(last_note.created_at).to eq(submitted_at)
        end
      end

      context 'when the review is "COMMENTED"' do
        let(:review) { create_review(type: 'COMMENTED') }

        it 'creates a note for the review with *Commented by<author>*' do
          expect { subject.execute }
            .to change(Note, :count).by(1)

          last_note = merge_request.notes.last

          expect(last_note.note).to eq("*Created by: author*\n\n**Review:** Commented\n\nnote")
          expect(last_note.author).to eq(project.creator)
          expect(last_note.created_at).to eq(submitted_at)
        end
      end

      context 'when the review is "CHANGES_REQUESTED"' do
        let(:review) { create_review(type: 'CHANGES_REQUESTED') }

        it 'creates a note for the review with *Changes requested by <author>*' do
          expect { subject.execute }
            .to change(Note, :count).by(1)

          last_note = merge_request.notes.last

          expect(last_note.note).to eq("*Created by: author*\n\n**Review:** Changes requested\n\nnote")
          expect(last_note.author).to eq(project.creator)
          expect(last_note.created_at).to eq(submitted_at)
        end
      end
    end
  end

  def create_review(type:, **extra)
    Gitlab::GithubImport::Representation::PullRequestReview.from_json_hash(
      extra.reverse_merge(
        author: { id: 999, login: 'author' },
        merge_request_id: merge_request.id,
        review_type: type,
        note: 'note',
        submitted_at: submitted_at.to_s
      )
    )
  end
end