BigW Consortium Gitlab

merge_requests_spec.rb 45.2 KB
Newer Older
1 2
require "spec_helper"

3
describe API::MergeRequests do
4 5 6
  let(:base_time)   { Time.now }
  let(:user)        { create(:user) }
  let(:admin)       { create(:user, :admin) }
7
  let(:non_member)  { create(:user) }
8
  let!(:project)    { create(:project, :public, :repository, creator: user, namespace: user.namespace, only_allow_merge_if_pipeline_succeeds: false) }
9
  let(:milestone)   { create(:milestone, title: '1.0.0', project: project) }
10 11 12 13 14 15 16 17 18
  let(:milestone1)   { create(:milestone, title: '0.9', project: project) }
  let!(:merge_request) { create(:merge_request, :simple, milestone: milestone1, author: user, assignee: user, source_project: project, target_project: project, title: "Test", created_at: base_time) }
  let!(:merge_request_closed) { create(:merge_request, state: "closed", milestone: milestone1, author: user, assignee: user, source_project: project, target_project: project, title: "Closed test", created_at: base_time + 1.second) }
  let!(:merge_request_merged) { create(:merge_request, state: "merged", author: user, assignee: user, source_project: project, target_project: project, title: "Merged test", created_at: base_time + 2.seconds, merge_commit_sha: '9999999999999999999999999999999999999999') }
  let!(:note)       { create(:note_on_merge_request, author: user, project: project, noteable: merge_request, note: "a comment on a MR") }
  let!(:note2)      { create(:note_on_merge_request, author: user, project: project, noteable: merge_request, note: "another comment on a MR") }
  let!(:label) do
    create(:label, title: 'label', color: '#FFAABB', project: project)
  end
19
  let!(:label2) { create(:label, title: 'a-test', color: '#FFFFFF', project: project) }
20
  let!(:label_link) { create(:label_link, label: label, target: merge_request) }
21 22 23
  let!(:label_link2) { create(:label_link, label: label2, target: merge_request) }
  let!(:downvote) { create(:award_emoji, :downvote, awardable: merge_request) }
  let!(:upvote) { create(:award_emoji, :upvote, awardable: merge_request) }
24 25

  before do
26
    project.team << [user, :reporter]
27
  end
28

29 30 31 32 33
  describe 'GET /merge_requests' do
    context 'when unauthenticated' do
      it 'returns authentication error' do
        get api('/merge_requests')

Lin Jen-Shin committed
34
        expect(response).to have_gitlab_http_status(401)
35 36 37 38
      end
    end

    context 'when authenticated' do
39
      let!(:project2) { create(:project, :public, namespace: user.namespace) }
40 41 42 43
      let!(:merge_request2) { create(:merge_request, :simple, author: user, assignee: user, source_project: project2, target_project: project2) }
      let(:user2) { create(:user) }

      it 'returns an array of all merge requests' do
44
        get api('/merge_requests', user), scope: :all
45

Lin Jen-Shin committed
46
        expect(response).to have_gitlab_http_status(200)
47 48 49 50 51 52 53
        expect(response).to include_pagination_headers
        expect(json_response).to be_an Array
        expect(json_response.map { |mr| mr['id'] })
          .to contain_exactly(merge_request.id, merge_request_closed.id, merge_request_merged.id, merge_request2.id)
      end

      it 'does not return unauthorized merge requests' do
54
        private_project = create(:project, :private)
55 56
        merge_request3 = create(:merge_request, :simple, source_project: private_project, target_project: private_project, source_branch: 'other-branch')

57
        get api('/merge_requests', user), scope: :all
58

Lin Jen-Shin committed
59
        expect(response).to have_gitlab_http_status(200)
60 61 62 63 64 65
        expect(response).to include_pagination_headers
        expect(json_response).to be_an Array
        expect(json_response.map { |mr| mr['id'] })
          .not_to include(merge_request3.id)
      end

66 67 68 69 70
      it 'returns an array of merge requests created by current user if no scope is given' do
        merge_request3 = create(:merge_request, :simple, author: user2, assignee: user, source_project: project2, target_project: project2, source_branch: 'other-branch')

        get api('/merge_requests', user2)

Lin Jen-Shin committed
71
        expect(response).to have_gitlab_http_status(200)
72 73 74 75 76
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(1)
        expect(json_response.first['id']).to eq(merge_request3.id)
      end

77 78 79
      it 'returns an array of merge requests authored by the given user' do
        merge_request3 = create(:merge_request, :simple, author: user2, assignee: user, source_project: project2, target_project: project2, source_branch: 'other-branch')

80
        get api('/merge_requests', user), author_id: user2.id, scope: :all
81

Lin Jen-Shin committed
82
        expect(response).to have_gitlab_http_status(200)
83 84 85 86 87 88 89 90
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(1)
        expect(json_response.first['id']).to eq(merge_request3.id)
      end

      it 'returns an array of merge requests assigned to the given user' do
        merge_request3 = create(:merge_request, :simple, author: user, assignee: user2, source_project: project2, target_project: project2, source_branch: 'other-branch')

91
        get api('/merge_requests', user), assignee_id: user2.id, scope: :all
92

Lin Jen-Shin committed
93
        expect(response).to have_gitlab_http_status(200)
94 95 96 97 98 99 100 101 102 103
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(1)
        expect(json_response.first['id']).to eq(merge_request3.id)
      end

      it 'returns an array of merge requests assigned to me' do
        merge_request3 = create(:merge_request, :simple, author: user, assignee: user2, source_project: project2, target_project: project2, source_branch: 'other-branch')

        get api('/merge_requests', user2), scope: 'assigned-to-me'

Lin Jen-Shin committed
104
        expect(response).to have_gitlab_http_status(200)
105 106 107 108 109 110 111 112 113 114
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(1)
        expect(json_response.first['id']).to eq(merge_request3.id)
      end

      it 'returns an array of merge requests created by me' do
        merge_request3 = create(:merge_request, :simple, author: user2, assignee: user, source_project: project2, target_project: project2, source_branch: 'other-branch')

        get api('/merge_requests', user2), scope: 'created-by-me'

Lin Jen-Shin committed
115
        expect(response).to have_gitlab_http_status(200)
116 117 118 119 120 121 122
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(1)
        expect(json_response.first['id']).to eq(merge_request3.id)
      end
    end
  end

123 124
  describe "GET /projects/:id/merge_requests" do
    context "when unauthenticated" do
125
      it "returns authentication error" do
Hiroyuki Sato committed
126
        get api("/projects/#{project.id}/merge_requests")
127

Lin Jen-Shin committed
128
        expect(response).to have_gitlab_http_status(401)
129 130 131 132
      end
    end

    context "when authenticated" do
Stan Hu committed
133 134 135 136 137 138 139 140 141 142 143 144
      it 'avoids N+1 queries' do
        control_count = ActiveRecord::QueryRecorder.new do
          get api("/projects/#{project.id}/merge_requests", user)
        end.count

        create(:merge_request, state: 'closed', milestone: milestone1, author: user, assignee: user, source_project: project, target_project: project, title: "Test", created_at: base_time)

        expect do
          get api("/projects/#{project.id}/merge_requests", user)
        end.not_to exceed_query_limit(control_count)
      end

145
      it "returns an array of all merge_requests" do
Hiroyuki Sato committed
146
        get api("/projects/#{project.id}/merge_requests", user)
147

Lin Jen-Shin committed
148
        expect(response).to have_gitlab_http_status(200)
149
        expect(response).to include_pagination_headers
150 151 152
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(3)
        expect(json_response.last['title']).to eq(merge_request.title)
153
        expect(json_response.last).to have_key('web_url')
154 155 156
        expect(json_response.last['sha']).to eq(merge_request.diff_head_sha)
        expect(json_response.last['merge_commit_sha']).to be_nil
        expect(json_response.last['merge_commit_sha']).to eq(merge_request.merge_commit_sha)
157 158 159
        expect(json_response.last['downvotes']).to eq(1)
        expect(json_response.last['upvotes']).to eq(1)
        expect(json_response.last['labels']).to eq([label2.title, label.title])
160 161 162 163
        expect(json_response.first['title']).to eq(merge_request_merged.title)
        expect(json_response.first['sha']).to eq(merge_request_merged.diff_head_sha)
        expect(json_response.first['merge_commit_sha']).not_to be_nil
        expect(json_response.first['merge_commit_sha']).to eq(merge_request_merged.merge_commit_sha)
164
      end
165

166
      it "returns an array of all merge_requests using simple mode" do
167
        get api("/projects/#{project.id}/merge_requests?view=simple", user)
168

Lin Jen-Shin committed
169
        expect(response).to have_gitlab_http_status(200)
170 171 172 173 174 175 176 177 178 179 180 181
        expect(response).to include_pagination_headers
        expect(json_response.last.keys).to match_array(%w(id iid title web_url created_at description project_id state updated_at))
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(3)
        expect(json_response.last['iid']).to eq(merge_request.iid)
        expect(json_response.last['title']).to eq(merge_request.title)
        expect(json_response.last).to have_key('web_url')
        expect(json_response.first['iid']).to eq(merge_request_merged.iid)
        expect(json_response.first['title']).to eq(merge_request_merged.title)
        expect(json_response.first).to have_key('web_url')
      end

182
      it "returns an array of all merge_requests" do
183
        get api("/projects/#{project.id}/merge_requests?state", user)
184

Lin Jen-Shin committed
185
        expect(response).to have_gitlab_http_status(200)
186
        expect(response).to include_pagination_headers
187 188 189
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(3)
        expect(json_response.last['title']).to eq(merge_request.title)
190
      end
191

192
      it "returns an array of open merge_requests" do
193
        get api("/projects/#{project.id}/merge_requests?state=opened", user)
194

Lin Jen-Shin committed
195
        expect(response).to have_gitlab_http_status(200)
196
        expect(response).to include_pagination_headers
197 198 199
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(1)
        expect(json_response.last['title']).to eq(merge_request.title)
200
      end
201

202
      it "returns an array of closed merge_requests" do
203
        get api("/projects/#{project.id}/merge_requests?state=closed", user)
204

Lin Jen-Shin committed
205
        expect(response).to have_gitlab_http_status(200)
206
        expect(response).to include_pagination_headers
207
        expect(json_response).to be_an Array
208 209
        expect(json_response.length).to eq(1)
        expect(json_response.first['title']).to eq(merge_request_closed.title)
210
      end
211

212
      it "returns an array of merged merge_requests" do
213
        get api("/projects/#{project.id}/merge_requests?state=merged", user)
214

Lin Jen-Shin committed
215
        expect(response).to have_gitlab_http_status(200)
216
        expect(response).to include_pagination_headers
217 218 219
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(1)
        expect(json_response.first['title']).to eq(merge_request_merged.title)
220
      end
221

222 223 224
      it 'returns merge_request by "iids" array' do
        get api("/projects/#{project.id}/merge_requests", user), iids: [merge_request.iid, merge_request_closed.iid]

Lin Jen-Shin committed
225
        expect(response).to have_gitlab_http_status(200)
226 227 228 229 230 231
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(2)
        expect(json_response.first['title']).to eq merge_request_closed.title
        expect(json_response.first['id']).to eq merge_request_closed.id
      end

232 233 234
      it 'matches V4 response schema' do
        get api("/projects/#{project.id}/merge_requests", user)

Lin Jen-Shin committed
235
        expect(response).to have_gitlab_http_status(200)
236 237 238
        expect(response).to match_response_schema('public_api/v4/merge_requests')
      end

239 240 241
      it 'returns an empty array if no issue matches milestone' do
        get api("/projects/#{project.id}/merge_requests", user), milestone: '1.0.0'

Lin Jen-Shin committed
242
        expect(response).to have_gitlab_http_status(200)
243 244 245 246 247 248 249
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(0)
      end

      it 'returns an empty array if milestone does not exist' do
        get api("/projects/#{project.id}/merge_requests", user), milestone: 'foo'

Lin Jen-Shin committed
250
        expect(response).to have_gitlab_http_status(200)
251 252 253 254 255 256 257 258 259 260 261 262 263 264
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(0)
      end

      it 'returns an array of merge requests in given milestone' do
        get api("/projects/#{project.id}/merge_requests", user), milestone: '0.9'

        expect(json_response.first['title']).to eq merge_request_closed.title
        expect(json_response.first['id']).to eq merge_request_closed.id
      end

      it 'returns an array of merge requests matching state in milestone' do
        get api("/projects/#{project.id}/merge_requests", user), milestone: '0.9', state: 'closed'

Lin Jen-Shin committed
265
        expect(response).to have_gitlab_http_status(200)
266 267 268 269 270 271 272 273
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(1)
        expect(json_response.first['id']).to eq(merge_request_closed.id)
      end

      it 'returns an array of labeled merge requests' do
        get api("/projects/#{project.id}/merge_requests?labels=#{label.title}", user)

Lin Jen-Shin committed
274
        expect(response).to have_gitlab_http_status(200)
275 276
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(1)
277
        expect(json_response.first['labels']).to eq([label2.title, label.title])
278 279 280 281 282
      end

      it 'returns an array of labeled merge requests where all labels match' do
        get api("/projects/#{project.id}/merge_requests?labels=#{label.title},foo,bar", user)

Lin Jen-Shin committed
283
        expect(response).to have_gitlab_http_status(200)
284 285 286 287 288 289 290
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(0)
      end

      it 'returns an empty array if no merge request matches labels' do
        get api("/projects/#{project.id}/merge_requests?labels=foo,bar", user)

Lin Jen-Shin committed
291
        expect(response).to have_gitlab_http_status(200)
292 293 294 295
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(0)
      end

296 297 298 299 300 301 302 303 304 305 306 307 308 309
      it 'returns an array of labeled merge requests that are merged for a milestone' do
        bug_label = create(:label, title: 'bug', color: '#FFAABB', project: project)

        mr1 = create(:merge_request, state: "merged", source_project: project, target_project: project, milestone: milestone)
        mr2 = create(:merge_request, state: "merged", source_project: project, target_project: project, milestone: milestone1)
        mr3 = create(:merge_request, state: "closed", source_project: project, target_project: project, milestone: milestone1)
        _mr = create(:merge_request, state: "merged", source_project: project, target_project: project, milestone: milestone1)

        create(:label_link, label: bug_label, target: mr1)
        create(:label_link, label: bug_label, target: mr2)
        create(:label_link, label: bug_label, target: mr3)

        get api("/projects/#{project.id}/merge_requests?labels=#{bug_label.title}&milestone=#{milestone1.title}&state=merged", user)

Lin Jen-Shin committed
310
        expect(response).to have_gitlab_http_status(200)
311 312 313 314 315
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(1)
        expect(json_response.first['id']).to eq(mr2.id)
      end

316 317 318 319 320 321
      context "with ordering" do
        before do
          @mr_later = mr_with_later_created_and_updated_at_time
          @mr_earlier = mr_with_earlier_created_and_updated_at_time
        end

322
        it "returns an array of merge_requests in ascending order" do
323
          get api("/projects/#{project.id}/merge_requests?sort=asc", user)
324

Lin Jen-Shin committed
325
          expect(response).to have_gitlab_http_status(200)
326
          expect(response).to include_pagination_headers
327 328
          expect(json_response).to be_an Array
          expect(json_response.length).to eq(3)
329
          response_dates = json_response.map { |merge_request| merge_request['created_at'] }
330
          expect(response_dates).to eq(response_dates.sort)
331
        end
Dmitriy Zaporozhets committed
332

333
        it "returns an array of merge_requests in descending order" do
334
          get api("/projects/#{project.id}/merge_requests?sort=desc", user)
335

Lin Jen-Shin committed
336
          expect(response).to have_gitlab_http_status(200)
337
          expect(response).to include_pagination_headers
338 339
          expect(json_response).to be_an Array
          expect(json_response.length).to eq(3)
340
          response_dates = json_response.map { |merge_request| merge_request['created_at'] }
341
          expect(response_dates).to eq(response_dates.sort.reverse)
342
        end
Dmitriy Zaporozhets committed
343

344
        it "returns an array of merge_requests ordered by updated_at" do
345
          get api("/projects/#{project.id}/merge_requests?order_by=updated_at", user)
346

Lin Jen-Shin committed
347
          expect(response).to have_gitlab_http_status(200)
348
          expect(response).to include_pagination_headers
349 350
          expect(json_response).to be_an Array
          expect(json_response.length).to eq(3)
351
          response_dates = json_response.map { |merge_request| merge_request['updated_at'] }
352
          expect(response_dates).to eq(response_dates.sort.reverse)
353
        end
Dmitriy Zaporozhets committed
354

355
        it "returns an array of merge_requests ordered by created_at" do
356
          get api("/projects/#{project.id}/merge_requests?order_by=created_at&sort=asc", user)
357

Lin Jen-Shin committed
358
          expect(response).to have_gitlab_http_status(200)
359
          expect(response).to include_pagination_headers
360 361
          expect(json_response).to be_an Array
          expect(json_response.length).to eq(3)
362
          response_dates = json_response.map { |merge_request| merge_request['created_at'] }
363
          expect(response_dates).to eq(response_dates.sort)
364
        end
365
      end
366 367 368
    end
  end

369
  describe "GET /projects/:id/merge_requests/:merge_request_iid" do
370
    it 'exposes known attributes' do
371
      get api("/projects/#{project.id}/merge_requests/#{merge_request.iid}", user)
372

Lin Jen-Shin committed
373
      expect(response).to have_gitlab_http_status(200)
374 375 376 377 378 379 380 381 382
      expect(json_response['id']).to eq(merge_request.id)
      expect(json_response['iid']).to eq(merge_request.iid)
      expect(json_response['project_id']).to eq(merge_request.project.id)
      expect(json_response['title']).to eq(merge_request.title)
      expect(json_response['description']).to eq(merge_request.description)
      expect(json_response['state']).to eq(merge_request.state)
      expect(json_response['created_at']).to be_present
      expect(json_response['updated_at']).to be_present
      expect(json_response['labels']).to eq(merge_request.label_names)
383
      expect(json_response['milestone']).to be_a Hash
384 385 386 387
      expect(json_response['assignee']).to be_a Hash
      expect(json_response['author']).to be_a Hash
      expect(json_response['target_branch']).to eq(merge_request.target_branch)
      expect(json_response['source_branch']).to eq(merge_request.source_branch)
388 389
      expect(json_response['upvotes']).to eq(1)
      expect(json_response['downvotes']).to eq(1)
390 391 392
      expect(json_response['source_project_id']).to eq(merge_request.source_project.id)
      expect(json_response['target_project_id']).to eq(merge_request.target_project.id)
      expect(json_response['work_in_progress']).to be_falsy
393
      expect(json_response['merge_when_pipeline_succeeds']).to be_falsy
394
      expect(json_response['merge_status']).to eq('can_be_merged')
395 396
      expect(json_response['should_close_merge_request']).to be_falsy
      expect(json_response['force_close_merge_request']).to be_falsy
397 398
    end

399
    it "returns merge_request" do
400
      get api("/projects/#{project.id}/merge_requests/#{merge_request.iid}", user)
Lin Jen-Shin committed
401
      expect(response).to have_gitlab_http_status(200)
402 403
      expect(json_response['title']).to eq(merge_request.title)
      expect(json_response['iid']).to eq(merge_request.iid)
404
      expect(json_response['work_in_progress']).to eq(false)
405
      expect(json_response['merge_status']).to eq('can_be_merged')
406 407
      expect(json_response['should_close_merge_request']).to be_falsy
      expect(json_response['force_close_merge_request']).to be_falsy
408
    end
409

410
    it "returns a 404 error if merge_request_iid not found" do
411
      get api("/projects/#{project.id}/merge_requests/999", user)
Lin Jen-Shin committed
412
      expect(response).to have_gitlab_http_status(404)
413
    end
414

415 416 417
    it "returns a 404 error if merge_request `id` is used instead of iid" do
      get api("/projects/#{project.id}/merge_requests/#{merge_request.id}", user)

Lin Jen-Shin committed
418
      expect(response).to have_gitlab_http_status(404)
419 420
    end

421 422 423
    context 'Work in Progress' do
      let!(:merge_request_wip) { create(:merge_request, author: user, assignee: user, source_project: project, target_project: project, title: "WIP: Test", created_at: base_time + 1.second) }

424
      it "returns merge_request" do
425
        get api("/projects/#{project.id}/merge_requests/#{merge_request_wip.iid}", user)
Lin Jen-Shin committed
426
        expect(response).to have_gitlab_http_status(200)
427 428 429
        expect(json_response['work_in_progress']).to eq(true)
      end
    end
430 431
  end

432
  describe 'GET /projects/:id/merge_requests/:merge_request_iid/commits' do
Valery Sizov committed
433
    it 'returns a 200 when merge request is valid' do
434
      get api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/commits", user)
Valery Sizov committed
435 436
      commit = merge_request.commits.first

Lin Jen-Shin committed
437
      expect(response).to have_gitlab_http_status(200)
438 439
      expect(response).to include_pagination_headers
      expect(json_response).to be_an Array
Valery Sizov committed
440 441 442
      expect(json_response.size).to eq(merge_request.commits.size)
      expect(json_response.first['id']).to eq(commit.id)
      expect(json_response.first['title']).to eq(commit.title)
443 444
    end

445
    it 'returns a 404 when merge_request_iid not found' do
446
      get api("/projects/#{project.id}/merge_requests/999/commits", user)
Lin Jen-Shin committed
447
      expect(response).to have_gitlab_http_status(404)
448
    end
449 450 451 452

    it 'returns a 404 when merge_request id is used instead of iid' do
      get api("/projects/#{project.id}/merge_requests/#{merge_request.id}/commits", user)

Lin Jen-Shin committed
453
      expect(response).to have_gitlab_http_status(404)
454
    end
455 456
  end

457
  describe 'GET /projects/:id/merge_requests/:merge_request_iid/changes' do
458
    it 'returns the change information of the merge_request' do
459
      get api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/changes", user)
460

Lin Jen-Shin committed
461
      expect(response).to have_gitlab_http_status(200)
462 463 464
      expect(json_response['changes'].size).to eq(merge_request.diffs.size)
    end

465
    it 'returns a 404 when merge_request_iid not found' do
466
      get api("/projects/#{project.id}/merge_requests/999/changes", user)
Lin Jen-Shin committed
467
      expect(response).to have_gitlab_http_status(404)
468
    end
469 470 471 472

    it 'returns a 404 when merge_request id is used instead of iid' do
      get api("/projects/#{project.id}/merge_requests/#{merge_request.id}/changes", user)

Lin Jen-Shin committed
473
      expect(response).to have_gitlab_http_status(404)
474
    end
475 476
  end

477
  describe "POST /projects/:id/merge_requests" do
478
    context 'between branches projects' do
479
      it "returns merge_request" do
Hiroyuki Sato committed
480
        post api("/projects/#{project.id}/merge_requests", user),
481
             title: 'Test merge_request',
482
             source_branch: 'feature_conflict',
483 484
             target_branch: 'master',
             author: user,
485
             labels: 'label, label2',
486
             milestone_id: milestone.id
487

Lin Jen-Shin committed
488
        expect(response).to have_gitlab_http_status(201)
489
        expect(json_response['title']).to eq('Test merge_request')
Douwe Maan committed
490
        expect(json_response['labels']).to eq(%w(label label2))
491
        expect(json_response['milestone']['id']).to eq(milestone.id)
492
        expect(json_response['force_remove_source_branch']).to be_falsy
493
      end
494

495
      it "returns 422 when source_branch equals target_branch" do
Hiroyuki Sato committed
496
        post api("/projects/#{project.id}/merge_requests", user),
497
        title: "Test merge_request", source_branch: "master", target_branch: "master", author: user
Lin Jen-Shin committed
498
        expect(response).to have_gitlab_http_status(422)
499
      end
500

501
      it "returns 400 when source_branch is missing" do
Hiroyuki Sato committed
502
        post api("/projects/#{project.id}/merge_requests", user),
503
        title: "Test merge_request", target_branch: "master", author: user
Lin Jen-Shin committed
504
        expect(response).to have_gitlab_http_status(400)
505
      end
506

507
      it "returns 400 when target_branch is missing" do
Hiroyuki Sato committed
508
        post api("/projects/#{project.id}/merge_requests", user),
509
        title: "Test merge_request", source_branch: "markdown", author: user
Lin Jen-Shin committed
510
        expect(response).to have_gitlab_http_status(400)
511 512
      end

513
      it "returns 400 when title is missing" do
Hiroyuki Sato committed
514
        post api("/projects/#{project.id}/merge_requests", user),
515
        target_branch: 'master', source_branch: 'markdown'
Lin Jen-Shin committed
516
        expect(response).to have_gitlab_http_status(400)
517
      end
518

519
      it 'allows special label names' do
520 521
        post api("/projects/#{project.id}/merge_requests", user),
             title: 'Test merge_request',
522
             source_branch: 'markdown',
523 524
             target_branch: 'master',
             author: user,
525
             labels: 'label, label?, label&foo, ?, &'
Lin Jen-Shin committed
526
        expect(response).to have_gitlab_http_status(201)
527 528 529 530 531
        expect(json_response['labels']).to include 'label'
        expect(json_response['labels']).to include 'label?'
        expect(json_response['labels']).to include 'label&foo'
        expect(json_response['labels']).to include '?'
        expect(json_response['labels']).to include '&'
532
      end
533 534 535 536 537

      context 'with existing MR' do
        before do
          post api("/projects/#{project.id}/merge_requests", user),
               title: 'Test merge_request',
538
               source_branch: 'feature_conflict',
539 540 541 542 543
               target_branch: 'master',
               author: user
          @mr = MergeRequest.all.last
        end

544
        it 'returns 409 when MR already exists for source/target' do
545 546 547
          expect do
            post api("/projects/#{project.id}/merge_requests", user),
                 title: 'New test merge_request',
548
                 source_branch: 'feature_conflict',
549 550 551
                 target_branch: 'master',
                 author: user
          end.to change { MergeRequest.count }.by(0)
Lin Jen-Shin committed
552
          expect(response).to have_gitlab_http_status(409)
553 554
        end
      end
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575

      context 'accepts remove_source_branch parameter' do
        let(:params) do
          { title: 'Test merge_request',
            source_branch: 'markdown',
            target_branch: 'master',
            author: user }
        end

        it 'sets force_remove_source_branch to false' do
          post api("/projects/#{project.id}/merge_requests", user), params.merge(remove_source_branch: false)

          expect(json_response['force_remove_source_branch']).to be_falsy
        end

        it 'sets force_remove_source_branch to true' do
          post api("/projects/#{project.id}/merge_requests", user), params.merge(remove_source_branch: true)

          expect(json_response['force_remove_source_branch']).to be_truthy
        end
      end
576
    end
577

578
    context 'forked projects' do
Dmitriy Zaporozhets committed
579
      let!(:user2) { create(:user) }
580 581
      let!(:fork_project) { create(:project, forked_from_project: project,  namespace: user2.namespace, creator_id: user2.id) }
      let!(:unrelated_project) { create(:project,  namespace: create(:user).namespace, creator_id: user2.id) }
582

583 584 585
      before do
        fork_project.add_reporter(user2)

586
        allow_any_instance_of(MergeRequest).to receive(:write_ref)
587 588
      end

589
      it "returns merge_request" do
Hiroyuki Sato committed
590
        post api("/projects/#{fork_project.id}/merge_requests", user2),
591 592
          title: 'Test merge_request', source_branch: "feature_conflict", target_branch: "master",
          author: user2, target_project_id: project.id, description: 'Test description for Test merge_request'
593
        expect(response).to have_gitlab_http_status(201)
594 595
        expect(json_response['title']).to eq('Test merge_request')
        expect(json_response['description']).to eq('Test description for Test merge_request')
596 597
      end

598
      it "does not return 422 when source_branch equals target_branch" do
599 600 601
        expect(project.id).not_to eq(fork_project.id)
        expect(fork_project.forked?).to be_truthy
        expect(fork_project.forked_from_project).to eq(project)
Hiroyuki Sato committed
602
        post api("/projects/#{fork_project.id}/merge_requests", user2),
603
        title: 'Test merge_request', source_branch: "master", target_branch: "master", author: user2, target_project_id: project.id
604
        expect(response).to have_gitlab_http_status(201)
605
        expect(json_response['title']).to eq('Test merge_request')
606 607
      end

608 609 610 611 612 613 614 615 616 617
      it 'returns 422 when target project has disabled merge requests' do
        project.project_feature.update(merge_requests_access_level: 0)

        post api("/projects/#{fork_project.id}/merge_requests", user2),
             title: 'Test',
             target_branch: 'master',
             source_branch: 'markdown',
             author: user2,
             target_project_id: project.id

Lin Jen-Shin committed
618
        expect(response).to have_gitlab_http_status(422)
619 620
      end

621
      it "returns 400 when source_branch is missing" do
Hiroyuki Sato committed
622
        post api("/projects/#{fork_project.id}/merge_requests", user2),
623
        title: 'Test merge_request', target_branch: "master", author: user2, target_project_id: project.id
Lin Jen-Shin committed
624
        expect(response).to have_gitlab_http_status(400)
625 626
      end

627
      it "returns 400 when target_branch is missing" do
Hiroyuki Sato committed
628
        post api("/projects/#{fork_project.id}/merge_requests", user2),
629
        title: 'Test merge_request', target_branch: "master", author: user2, target_project_id: project.id
Lin Jen-Shin committed
630
        expect(response).to have_gitlab_http_status(400)
631 632
      end

633
      it "returns 400 when title is missing" do
Hiroyuki Sato committed
634
        post api("/projects/#{fork_project.id}/merge_requests", user2),
635
        target_branch: 'master', source_branch: 'markdown', author: user2, target_project_id: project.id
Lin Jen-Shin committed
636
        expect(response).to have_gitlab_http_status(400)
637 638
      end

639
      context 'when target_branch is specified' do
640
        it 'returns 422 if not a forked project' do
641 642 643
          post api("/projects/#{project.id}/merge_requests", user),
               title: 'Test merge_request',
               target_branch: 'master',
644
               source_branch: 'markdown',
645 646
               author: user,
               target_project_id: fork_project.id
Lin Jen-Shin committed
647
          expect(response).to have_gitlab_http_status(422)
648
        end
Izaak Alpert committed
649

650
        it 'returns 422 if targeting a different fork' do
651 652 653
          post api("/projects/#{fork_project.id}/merge_requests", user2),
               title: 'Test merge_request',
               target_branch: 'master',
654
               source_branch: 'markdown',
655 656
               author: user2,
               target_project_id: unrelated_project.id
Lin Jen-Shin committed
657
          expect(response).to have_gitlab_http_status(422)
658
        end
Izaak Alpert committed
659 660
      end

661
      it "returns 201 when target_branch is specified and for the same project" do
Hiroyuki Sato committed
662
        post api("/projects/#{fork_project.id}/merge_requests", user2),
663
        title: 'Test merge_request', target_branch: 'master', source_branch: 'markdown', author: user2, target_project_id: fork_project.id
Lin Jen-Shin committed
664
        expect(response).to have_gitlab_http_status(201)
Izaak Alpert committed
665
      end
666
    end
667 668
  end

669
  describe "DELETE /projects/:id/merge_requests/:merge_request_iid" do
670 671
    context "when the user is developer" do
      let(:developer) { create(:user) }
672

673 674 675
      before do
        project.team << [developer, :developer]
      end
676

677
      it "denies the deletion of the merge request" do
678
        delete api("/projects/#{project.id}/merge_requests/#{merge_request.iid}", developer)
Lin Jen-Shin committed
679
        expect(response).to have_gitlab_http_status(403)
680
      end
681
    end
682

683 684
    context "when the user is project owner" do
      it "destroys the merge request owners can destroy" do
685
        delete api("/projects/#{project.id}/merge_requests/#{merge_request.iid}", user)
686

Lin Jen-Shin committed
687
        expect(response).to have_gitlab_http_status(204)
688
      end
689 690 691 692

      it "returns 404 for an invalid merge request IID" do
        delete api("/projects/#{project.id}/merge_requests/12345", user)

Lin Jen-Shin committed
693
        expect(response).to have_gitlab_http_status(404)
694 695 696 697 698
      end

      it "returns 404 if the merge request id is used instead of iid" do
        delete api("/projects/#{project.id}/merge_requests/#{merge_request.id}", user)

Lin Jen-Shin committed
699
        expect(response).to have_gitlab_http_status(404)
700
      end
701 702 703 704

      it_behaves_like '412 response' do
        let(:request) { api("/projects/#{project.id}/merge_requests/#{merge_request.iid}", user) }
      end
705
    end
706 707
  end

708
  describe "PUT /projects/:id/merge_requests/:merge_request_iid/merge" do
709
    let(:pipeline) { create(:ci_pipeline_without_jobs) }
710

711
    it "returns merge_request in case of success" do
712
      put api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/merge", user)
713

Lin Jen-Shin committed
714
      expect(response).to have_gitlab_http_status(200)
715 716
    end

717
    it "returns 406 if branch can't be merged" do
718 719
      allow_any_instance_of(MergeRequest)
        .to receive(:can_be_merged?).and_return(false)
720

721
      put api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/merge", user)
722

Lin Jen-Shin committed
723
      expect(response).to have_gitlab_http_status(406)
724
      expect(json_response['message']).to eq('Branch cannot be merged')
725
    end
726

727
    it "returns 405 if merge_request is not open" do
728
      merge_request.close
729
      put api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/merge", user)
Lin Jen-Shin committed
730
      expect(response).to have_gitlab_http_status(405)
731
      expect(json_response['message']).to eq('405 Method Not Allowed')
732 733
    end

734
    it "returns 405 if merge_request is a work in progress" do
735
      merge_request.update_attribute(:title, "WIP: #{merge_request.title}")
736
      put api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/merge", user)
Lin Jen-Shin committed
737
      expect(response).to have_gitlab_http_status(405)
738 739 740
      expect(json_response['message']).to eq('405 Method Not Allowed')
    end

741
    it 'returns 405 if the build failed for a merge request that requires success' do
742
      allow_any_instance_of(MergeRequest).to receive(:mergeable_ci_state?).and_return(false)
743

744
      put api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/merge", user)
745

Lin Jen-Shin committed
746
      expect(response).to have_gitlab_http_status(405)
747 748 749
      expect(json_response['message']).to eq('405 Method Not Allowed')
    end

750
    it "returns 401 if user has no permissions to merge" do
751 752
      user2 = create(:user)
      project.team << [user2, :reporter]
753
      put api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/merge", user2)
Lin Jen-Shin committed
754
      expect(response).to have_gitlab_http_status(401)
755
      expect(json_response['message']).to eq('401 Unauthorized')
756
    end
757

758
    it "returns 409 if the SHA parameter doesn't match" do
759
      put api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/merge", user), sha: merge_request.diff_head_sha.reverse
760

Lin Jen-Shin committed
761
      expect(response).to have_gitlab_http_status(409)
762 763 764 765
      expect(json_response['message']).to start_with('SHA does not match HEAD of source branch')
    end

    it "succeeds if the SHA parameter matches" do
766
      put api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/merge", user), sha: merge_request.diff_head_sha
767

Lin Jen-Shin committed
768
      expect(response).to have_gitlab_http_status(200)
769 770
    end

771
    it "enables merge when pipeline succeeds if the pipeline is active" do
772
      allow_any_instance_of(MergeRequest).to receive(:head_pipeline).and_return(pipeline)
773
      allow(pipeline).to receive(:active?).and_return(true)
774

775
      put api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/merge", user), merge_when_pipeline_succeeds: true
776

Lin Jen-Shin committed
777
      expect(response).to have_gitlab_http_status(200)
778
      expect(json_response['title']).to eq('Test')
779
      expect(json_response['merge_when_pipeline_succeeds']).to eq(true)
780
    end
781

782 783 784 785 786 787 788
    it "enables merge when pipeline succeeds if the pipeline is active and only_allow_merge_if_pipeline_succeeds is true" do
      allow_any_instance_of(MergeRequest).to receive(:head_pipeline).and_return(pipeline)
      allow(pipeline).to receive(:active?).and_return(true)
      project.update_attribute(:only_allow_merge_if_pipeline_succeeds, true)

      put api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/merge", user), merge_when_pipeline_succeeds: true

Lin Jen-Shin committed
789
      expect(response).to have_gitlab_http_status(200)
790 791 792 793
      expect(json_response['title']).to eq('Test')
      expect(json_response['merge_when_pipeline_succeeds']).to eq(true)
    end

794 795 796
    it "returns 404 for an invalid merge request IID" do
      put api("/projects/#{project.id}/merge_requests/12345/merge", user)

Lin Jen-Shin committed
797
      expect(response).to have_gitlab_http_status(404)
798 799 800 801 802
    end

    it "returns 404 if the merge request id is used instead of iid" do
      put api("/projects/#{project.id}/merge_requests/#{merge_request.id}/merge", user)

Lin Jen-Shin committed
803
      expect(response).to have_gitlab_http_status(404)
804
    end
805 806
  end

807
  describe "PUT /projects/:id/merge_requests/:merge_request_iid" do
808 809
    context "to close a MR" do
      it "returns merge_request" do
810
        put api("/projects/#{project.id}/merge_requests/#{merge_request.iid}", user), state_event: "close"
811

Lin Jen-Shin committed
812
        expect(response).to have_gitlab_http_status(200)
813 814 815 816
        expect(json_response['state']).to eq('closed')
      end
    end

817
    it "updates title and returns merge_request" do
818
      put api("/projects/#{project.id}/merge_requests/#{merge_request.iid}", user), title: "New title"
Lin Jen-Shin committed
819
      expect(response).to have_gitlab_http_status(200)
820
      expect(json_response['title']).to eq('New title')
821
    end
822

823
    it "updates description and returns merge_request" do
824
      put api("/projects/#{project.id}/merge_requests/#{merge_request.iid}", user), description: "New description"
Lin Jen-Shin committed
825
      expect(response).to have_gitlab_http_status(200)
826
      expect(json_response['description']).to eq('New description')
827 828
    end

829
    it "updates milestone_id and returns merge_request" do
830
      put api("/projects/#{project.id}/merge_requests/#{merge_request.iid}", user), milestone_id: milestone.id
Lin Jen-Shin committed
831
      expect(response).to have_gitlab_http_status(200)
832 833 834
      expect(json_response['milestone']['id']).to eq(milestone.id)
    end

835
    it "returns merge_request with renamed target_branch" do
836
      put api("/projects/#{project.id}/merge_requests/#{merge_request.iid}", user), target_branch: "wiki"
Lin Jen-Shin committed
837
      expect(response).to have_gitlab_http_status(200)
838
      expect(json_response['target_branch']).to eq('wiki')
839
    end
840

841
    it "returns merge_request that removes the source branch" do
842
      put api("/projects/#{project.id}/merge_requests/#{merge_request.iid}", user), remove_source_branch: true
843

Lin Jen-Shin committed
844
      expect(response).to have_gitlab_http_status(200)
845 846 847
      expect(json_response['force_remove_source_branch']).to be_truthy
    end

848
    it 'allows special label names' do
849
      put api("/projects/#{project.id}/merge_requests/#{merge_request.iid}", user),
850 851 852
        title: 'new issue',
        labels: 'label, label?, label&foo, ?, &'

853 854 855 856 857 858
      expect(response.status).to eq(200)
      expect(json_response['labels']).to include 'label'
      expect(json_response['labels']).to include 'label?'
      expect(json_response['labels']).to include 'label&foo'
      expect(json_response['labels']).to include '?'
      expect(json_response['labels']).to include '&'
859
    end
860 861

    it 'does not update state when title is empty' do
862
      put api("/projects/#{project.id}/merge_requests/#{merge_request.iid}", user), state_event: 'close', title: nil
863 864

      merge_request.reload
Lin Jen-Shin committed
865
      expect(response).to have_gitlab_http_status(400)
866 867 868 869
      expect(merge_request.state).to eq('opened')
    end

    it 'does not update state when target_branch is empty' do
870
      put api("/projects/#{project.id}/merge_requests/#{merge_request.iid}", user), state_event: 'close', target_branch: nil
871 872

      merge_request.reload
Lin Jen-Shin committed
873
      expect(response).to have_gitlab_http_status(400)
874 875
      expect(merge_request.state).to eq('opened')
    end
876 877 878 879

    it "returns 404 for an invalid merge request IID" do
      put api("/projects/#{project.id}/merge_requests/12345", user), state_event: "close"

Lin Jen-Shin committed
880
      expect(response).to have_gitlab_http_status(404)
881 882 883 884 885
    end

    it "returns 404 if the merge request id is used instead of iid" do
      put api("/projects/#{project.id}/merge_requests/#{merge_request.id}", user), state_event: "close"

Lin Jen-Shin committed
886
      expect(response).to have_gitlab_http_status(404)
887
    end
888 889
  end

890
  describe 'GET :id/merge_requests/:merge_request_iid/closes_issues' do
891 892 893 894 895
    it 'returns the issue that will be closed on merge' do
      issue = create(:issue, project: project)
      mr = merge_request.tap do |mr|
        mr.update_attribute(:description, "Closes #{issue.to_reference(mr.project)}")
      end
896

897
      get api("/projects/#{project.id}/merge_requests/#{mr.iid}/closes_issues", user)
898

Lin Jen-Shin committed
899
      expect(response).to have_gitlab_http_status(200)
900
      expect(response).to include_pagination_headers
901 902 903 904 905
      expect(json_response).to be_an Array
      expect(json_response.length).to eq(1)
      expect(json_response.first['id']).to eq(issue.id)
    end

906
    it 'returns an empty array when there are no issues to be closed' do
907
      get api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/closes_issues", user)
908

Lin Jen-Shin committed
909
      expect(response).to have_gitlab_http_status(200)
910
      expect(response).to include_pagination_headers
911 912 913
      expect(json_response).to be_an Array
      expect(json_response.length).to eq(0)
    end
914 915

    it 'handles external issues' do
916
      jira_project = create(:jira_project, :public, :repository, name: 'JIR_EXT1')
917 918 919 920 921
      ext_issue = ExternalIssue.new("#{jira_project.name}-123", jira_project)
      issue = create(:issue, project: jira_project)
      description = "Closes #{ext_issue.to_reference(jira_project)}\ncloses #{issue.to_reference}"
      merge_request = create(:merge_request,
        :simple, author: user, assignee: user, source_project: jira_project, description: description)
922

923
      get api("/projects/#{jira_project.id}/merge_requests/#{merge_request.iid}/closes_issues", user)
924

Lin Jen-Shin committed
925
      expect(response).to have_gitlab_http_status(200)
926
      expect(response).to include_pagination_headers
927
      expect(json_response).to be_an Array
928 929 930 931
      expect(json_response.length).to eq(2)
      expect(json_response.second['title']).to eq(ext_issue.title)
      expect(json_response.second['id']).to eq(ext_issue.id)
      expect(json_response.second['confidential']).to be_nil
932 933
      expect(json_response.first['title']).to eq(issue.title)
      expect(json_response.first['id']).to eq(issue.id)
934
      expect(json_response.first['confidential']).not_to be_nil
935
    end
936 937

    it 'returns 403 if the user has no access to the merge request' do
938
      project = create(:project, :private)
939 940 941 942
      merge_request = create(:merge_request, :simple, source_project: project)
      guest = create(:user)
      project.team << [guest, :guest]

943
      get api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/closes_issues", guest)
944

Lin Jen-Shin committed
945
      expect(response).to have_gitlab_http_status(403)
946
    end
947 948 949 950

    it "returns 404 for an invalid merge request IID" do
      get api("/projects/#{project.id}/merge_requests/12345/closes_issues", user)

Lin Jen-Shin committed
951
      expect(response).to have_gitlab_http_status(404)
952 953 954 955 956
    end

    it "returns 404 if the merge request id is used instead of iid" do
      get api("/projects/#{project.id}/merge_requests/#{merge_request.id}/closes_issues", user)

Lin Jen-Shin committed
957
      expect(response).to have_gitlab_http_status(404)
958
    end
959 960
  end

961
  describe 'POST :id/merge_requests/:merge_request_iid/subscribe' do
962
    it 'subscribes to a merge request' do
963
      post api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/subscribe", admin)
964

Lin Jen-Shin committed
965
      expect(response).to have_gitlab_http_status(201)
966 967 968 969
      expect(json_response['subscribed']).to eq(true)
    end

    it 'returns 304 if already subscribed' do
970
      post api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/subscribe", user)
971

Lin Jen-Shin committed
972
      expect(response).to have_gitlab_http_status(304)
973
    end
974 975

    it 'returns 404 if the merge request is not found' do
976
      post api("/projects/#{project.id}/merge_requests/123/subscribe", user)
977

Lin Jen-Shin committed
978
      expect(response).to have_gitlab_http_status(404)
979
    end
980

981 982 983
    it 'returns 404 if the merge request id is used instead of iid' do
      post api("/projects/#{project.id}/merge_requests/#{merge_request.id}/subscribe", user)

Lin Jen-Shin committed
984
      expect(response).to have_gitlab_http_status(404)
985 986
    end

987 988 989 990
    it 'returns 403 if user has no access to read code' do
      guest = create(:user)
      project.team << [guest, :guest]

991
      post api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/subscribe", guest)
992

Lin Jen-Shin committed
993
      expect(response).to have_gitlab_http_status(403)
994
    end
995 996
  end

997
  describe 'POST :id/merge_requests/:merge_request_iid/unsubscribe' do
998
    it 'unsubscribes from a merge request' do
999
      post api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/unsubscribe", user)
1000

Lin Jen-Shin committed
1001
      expect(response).to have_gitlab_http_status(201)
1002 1003 1004 1005
      expect(json_response['subscribed']).to eq(false)
    end

    it 'returns 304 if not subscribed' do
1006
      post api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/unsubscribe", admin)
1007

Lin Jen-Shin committed
1008
      expect(response).to have_gitlab_http_status(304)
1009
    end
1010 1011

    it 'returns 404 if the merge request is not found' do
1012
      post api("/projects/#{project.id}/merge_requests/123/unsubscribe", user)
1013

Lin Jen-Shin committed
1014
      expect(response).to have_gitlab_http_status(404)
1015
    end
1016

1017 1018 1019
    it 'returns 404 if the merge request id is used instead of iid' do
      post api("/projects/#{project.id}/merge_requests/#{merge_request.id}/unsubscribe", user)

Lin Jen-Shin committed
1020
      expect(response).to have_gitlab_http_status(404)
1021 1022
    end

1023 1024 1025 1026
    it 'returns 403 if user has no access to read code' do
      guest = create(:user)
      project.team << [guest, :guest]

1027
      post api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/unsubscribe", guest)
1028

Lin Jen-Shin committed
1029
      expect(response).to have_gitlab_http_status(403)
1030
    end
1031 1032
  end

1033 1034 1035 1036 1037 1038
  describe 'Time tracking' do
    let(:issuable) { merge_request }

    include_examples 'time tracking endpoints', 'merge_request'
  end

1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
  def mr_with_later_created_and_updated_at_time
    merge_request
    merge_request.created_at += 1.hour
    merge_request.updated_at += 30.minutes
    merge_request.save
    merge_request
  end

  def mr_with_earlier_created_and_updated_at_time
    merge_request_closed
    merge_request_closed.created_at -= 1.hour
    merge_request_closed.updated_at -= 30.minutes
    merge_request_closed.save
    merge_request_closed
  end
1054
end